Esempio n. 1
0
def menu(options):
    """ Accepts a list and causes TextMate to show an inline menu.
    
    If options is a list of strings, will return the selected index.
    
    If options is a list of (key, value) tuples, will return value of the
    selected key. Note that we don't use dicts, so that key-value options
    can be ordered. If you want to use a dict, try dict.items().
    
    In either input case, a list item with value `None` causes tm_dialog to
    separator for that index.
    """
    hashed_options = False
    if not options:
        return None
    if all_are_instance(options, (unicode, str, NoneType)):
        menu = dict(menuItems=[item(val) for val in options])
    elif all_are_instance(options, (tuple, NoneType)):
        hashed_options = True
        menu = dict(menuItems=[item(pair) for pair in options])
    plist = to_plist(menu)
    cmd = 'bash -c "%s -up %s"' % (sh_escape(dialog), sh_escape(plist))
    result = from_plist(sh(cmd))
    if not 'selectedIndex' in result:
        return None
    index = int(result['selectedIndex'])
    if hashed_options:
        return options[index][1]
    return options[index]
Esempio n. 2
0
def menu(options):
    """ Accepts a list and causes TextMate to show an inline menu.
    
    If options is a list of strings, will return the selected index.
    
    If options is a list of (key, value) tuples, will return value of the
    selected key. Note that we don't use dicts, so that key-value options
    can be ordered. If you want to use a dict, try dict.items().
    
    In either input case, a list item with value `None` causes tm_dialog to
    separator for that index.
    """
    hashed_options = False
    if not options:
        return None
    if all_are_instance(options, (unicode, str, NoneType)):
        menu = dict(menuItems=[item(val) for val in options])
    elif all_are_instance(options, (tuple, NoneType)):
        hashed_options = True
        menu = dict(menuItems=[item(pair) for pair in options])
    plist = to_plist(menu)
    cmd = 'bash -c "%s -up %s"' % (sh_escape(dialog), sh_escape(plist))
    result = from_plist(sh(cmd))
    if not 'selectedIndex' in result:
        return None
    index = int(result['selectedIndex'])
    if hashed_options:
        return options[index][1]
    return options[index]
Esempio n. 3
0
def complete(choices, options={}):

    if '2' not in pytm.DIALOG:
        raise 'Dialog2 not found.'

    if 'initial_filter' not in options:
        characters = 'a-zA-Z0-9'
        if 'extra_chars' in options:
            characters += re.escape(options['extra_chars'])

        options['initial_filter'] = current_word(characters, "left")

    command = [pytm.DIALOG, "popup", "--returnChoice"]
    if "initial_filter" in options and options['initial_filter']:
        command.append("--alreadyTyped %s" %
                       sh_escape(options["initial_filter"]))
    if "static_prefix" in options and options['static_prefix']:
        command.append("--staticPrefix %s" %
                       sh_escape(options["static_prefix"]))
    if "extra_chars" in options and options['extra_chars']:
        command.append("--additionalWordCharacters %s" %
                       sh_escape(options['extra_chars']))
    if "case_insensitive" in options and options['case_insensitive']:
        command.append("--caseInsensitive")

    def formalize(choice):
        try:
            choice['display']
            return choice
        except (KeyError, IndexError, TypeError):
            return {'display': choice}

    choices = [formalize(choice) for choice in choices]

    plist = {'suggestions': choices}

    try:
        f = tempfile.NamedTemporaryFile()
        plistlib.writePlist(plist, f)
        f.seek(0)

        command = ' '.join(command).strip()

        process = subprocess.Popen(command,
                                   stdin=subprocess.PIPE,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.STDOUT,
                                   shell=True)
        process.stdin.write(f.read())
        process.stdin.close()
        f.close()

    except Exception as e:
        pytm.textmate.exit_show_tool_tip('ERROR: %s' % e)
    finally:
        f.close()
Esempio n. 4
0
def launch_pydoc_server():
    server = path.join(env["TM_BUNDLE_SUPPORT"], "DocMate/pydoc_server.py")
    python, version = tm_helpers.env_python()
    url, port = pydoc_url()
    if not accessible(url):
        # launch pydoc.
        system('/usr/bin/nohup %s %s %i %i\
                    1>> /tmp/pydoc.log 2>> /tmp/pydoc.log &' \
                    % (python, tm_helpers.sh_escape(server), port, TIMEOUT))
    return url
Esempio n. 5
0
def launch_pydoc_server():
    server = path.join(env["TM_BUNDLE_SUPPORT"], "DocMate/pydoc_server.py")
    python, version = tm_helpers.env_python()
    url, port = pydoc_url()
    if not accessible(url):
        # launch pydoc.
        system('/usr/bin/nohup %s %s %i %i\
                    1>> /tmp/pydoc.log 2>> /tmp/pydoc.log &' \
                    % (python, tm_helpers.sh_escape(server), port, TIMEOUT))
    return url
Esempio n. 6
0
def go_to(options = {}):
    if 'file' in options:
        default_line = 1
    else:
        default_line = LINE_NUMBER
    
    defaults = {
        'file': FILEPATH,
        'line': default_line,
        'column': 1
    }
    defaults.update(options)
    options = defaults
    
    command = "txmt://open?"
    if 'file' in options:
        command = "%surl=file://%s&" % (command, options['file'])
    command = "%sline=%s&column=%s" % (command, options['line'], options['column'])
    command = 'open %s' % sh_escape(command)

    import subprocess
    process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
    process.wait()
Esempio n. 7
0
def go_to(options={}):
    if 'file' in options:
        default_line = 1
    else:
        default_line = LINE_NUMBER

    defaults = {'file': FILEPATH, 'line': default_line, 'column': 1}
    defaults.update(options)
    options = defaults

    command = "txmt://open?"
    if 'file' in options:
        command = "%surl=file://%s&" % (command, options['file'])
    command = "%sline=%s&column=%s" % (command, options['line'],
                                       options['column'])
    command = 'open %s' % sh_escape(command)

    import subprocess
    process = subprocess.Popen(command,
                               stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.STDOUT,
                               shell=True)
    process.wait()
def html_header(title, subtitle):    
    return sh('source %s; html_header %s %s' % (webpreview, sh_escape(title), sh_escape(subtitle)))
# A fixed version of the one bundled with TM

# python bindings for soryu's web-preview
from os import path, environ
from tm_helpers import sh, sh_escape

webpreview = sh_escape(path.join(environ["TM_SUPPORT_PATH"], "lib/webpreview.sh"))

def html_header(title, subtitle):    
    return sh('source %s; html_header %s %s' % (webpreview, sh_escape(title), sh_escape(subtitle)))

def html_footer():
    return sh('source %s; html_footer' % webpreview)