def create_sublime_url(fn=None, row=1, col=1, commands=[]):
    """
    Creates a sblm:// url with the `file`:`row`:`col` urlencoded as the `path`.
    It urlencodes JSON encoded commands into the query string.

    `commands` must be a sequence of length 2 sequences which will be encoded as
    a JSON array.

        [[command_name"", command_args{}], ...]

    The first item a command name and the next a JSON object for the command
    arguments.

    >>> create_sublime_url('C:\\example.txt', 25, 10, [['show_panel', {'panel': 'replace'}]])
    'sblm:///C/example.txt%3A25%3A10?show_panel=%7B%22panel%22%3A+%22replace%22%7D'

    """
    sblm     = 'sblm://%s?%s'

    if fn:
        # In a format window.open_file(f, sublime.ENCODED_POSITION) understands
        path = quote('%s:%s:%s' % (open_file_path(fn), row, col))
    else:
        # Just send commands to run on the currently active view
        path = ''

    return sblm % (path, urlencode(dict(commands=dumpsj(commands))))
def encode_for_command_line(command=None, args=None, **kw):
    """
    Formats a command as expected by --command. Does NOT escape. This is the
    same format that sublime.log_commands(True) will output to the console.

    eg.
        `command: show_panel {"panel": "console"}`

        This command will format the `show_panel {"panel": "console"}` part.

    May be used to create the command to register the Protocol Handler with.

    eg.

        >>> repr(subprocess.list2cmdline ([
        ... sys.executable, '--command',
        ... encode_for_command_line('open_protocol_url', url="%1")] )))
        '"C:\\Program Files\\Sublime Text 2\\sublime_text.exe" --command "open_protocol_url {\\"url\\": \\"%1\\"}"'
    """
    if isinstance(command, dict):
        args    = command['args']
        command = command['command']

    if kw:
        if args: args.update(kw)
        else: args = kw

    return '%s %s' % (command, dumpsj(args))
Esempio n. 3
0
def create_sublime_url(fn=None, row=1, col=1, commands=[]):
    """
    Creates a sblm:// url with the `file`:`row`:`col` urlencoded as the `path`.
    It urlencodes JSON encoded commands into the query string.

    `commands` must be a sequence of length 2 sequences.

        [(cmd_type"", json_array[]), ...]

    `cmd_type` is a key for the type of command:

        {'cmd': TextCommand,  'wcmd': WindowCommand }

    `json_array` expects a sequence, which will be encoded as a JSON array, the
    first item representing a command name and the next a JSON object for the
    command arguments.

        [command_name"", command_args{}]

    >>> create_sublime_url('C:\\example.txt', 25, 10, [('wcmd', ['show_panel', {'panel': 'replace'}])])
    'sblm:///C/example.txt%3A26%3A11?wcmd=%5B%22show_panel%22%2C+%7B%22panel%22%3A+%22replace%22%7D%5D'
    """
    sblm = 'sblm://%s?%s'
    if DEBUG:
        commands = [('wcmd', ['show_panel', {'panel': 'replace'}])]

    if fn:
        # In a format window.open_file(f, sublime.ENCODED_POSITION) understands
        path = quote('%s:%s:%s' % (open_file_path(fn), row, col))
    else:
        # Just send commands to run on the currently active view
        path = ''

    return sblm % (path, urlencode([(k, dumpsj(v)) for (k, v) in commands]))
def create_sublime_url(fn=None, row=1, col=1, commands=[]):
    """
    Creates a sblm:// url with the `file`:`row`:`col` urlencoded as the `path`.
    It urlencodes JSON encoded commands into the query string.

    `commands` must be a sequence of length 2 sequences.

        [(cmd_type"", json_array[]), ...]

    `cmd_type` is a key for the type of command:

        {'cmd': TextCommand,  'wcmd': WindowCommand }

    `json_array` expects a sequence, which will be encoded as a JSON array, the
    first item representing a command name and the next a JSON object for the
    command arguments.

        [command_name"", command_args{}]

    >>> create_sublime_url('C:\\example.txt', 25, 10, [('wcmd', ['show_panel', {'panel': 'replace'}])])
    'sblm:///C/example.txt%3A26%3A11?wcmd=%5B%22show_panel%22%2C+%7B%22panel%22%3A+%22replace%22%7D%5D'
    """
    sblm     = 'sblm://%s?%s'
    if DEBUG:
        commands = [('wcmd', ['show_panel', {'panel': 'replace'}])]

    if fn:
        # In a format window.open_file(f, sublime.ENCODED_POSITION) understands
        path = quote('%s:%s:%s' % (open_file_path(fn), row, col))
    else:
        # Just send commands to run on the currently active view
        path = ''

    return sblm % (path, urlencode([(k, dumpsj(v)) for (k,v) in  commands]))
Esempio n. 5
0
def encode_for_command_line(command=None, args=None, **kw):
    """
    Formats a command as expected by --command. Does NOT escape. This is the
    same format that sublime.log_commands(True) will output to the console.

    eg.
        `command: show_panel {"panel": "console"}`

        This command will format the `show_panel {"panel": "console"}` part.

    May be used to create the command to register the Protocol Handler with.

    eg.

        >>> repr(subprocess.list2cmdline ([
        ... sys.executable, '--command',
        ... encode_for_command_line('open_protocol_url', url="%1")] )))
        '"C:\\Program Files\\Sublime Text 2\\sublime_text.exe" --command "open_protocol_url {\\"url\\": \\"%1\\"}"'
    """
    if isinstance(command, dict):
        args = command['args']
        command = command['command']

    if kw:
        if args: args.update(kw)
        else: args = kw

    return '%s %s' % (command, dumpsj(args))