Exemplo n.º 1
0
def create_executors():
    not_empty = lambda val : val not in ("", "None", None)
    for executor in filter(not_empty, pantondoc_executors):
        _executor = 'call pantondoc_executors#Execute("' + \
                    " ".join(executor.command) + '", "' \
                    + executor.type + '", "<bang>")'
        if not_empty(executor.name):
			ex("command!","-buffer", "-bang", executor.name, "exec '" + _executor + "'")
        if not_empty(executor.mapping):
            ex("map","<buffer><silent>", executor.mapping, ":" + _executor + "<cr>")
Exemplo n.º 2
0
def create_executors():
    not_empty = lambda val: val not in ("", "None", None)
    for executor in filter(not_empty, pantondoc_executors):
        _executor = 'call pantondoc_executors#Execute("' + \
                    " ".join(executor.command) + '", "' \
                    + executor.type + '", "<bang>")'
        if not_empty(executor.name):
            ex("command!", "-buffer", "-bang", executor.name,
               "exec '" + _executor + "'")
        if not_empty(executor.mapping):
            ex("map", "<buffer><silent>", executor.mapping,
               ":" + _executor + "<cr>")
Exemplo n.º 3
0
def execute(command, output_type="html", open_when_done=False):
    # first, we parse the command description as a list of strings
    steps = []
    for step in command.split("|"):
        step = step.strip()
        # we expand some values using vim's expand function.
        # check :help filename-modifiers
        step = re.sub("%<?((:[8~.hpret])?)*",
                      lambda i: vim.eval('expand("' + i.group() + '")'), step)
        # we substitute some variables
        for sub in variable_substitutions:
            step = re.sub(sub, variable_substitutions[sub], step)
        steps.append(step)

    out = os.path.splitext(vim.current.buffer.name)[0] + "." + output_type

    # now, we run the pipe
    procs = {}
    procs[0] = sp.Popen(shlex.split(steps[0]), stdout=sp.PIPE)
    if len(steps) > 1:
        i = 1
        for p in steps[1:]:
            procs[i] = sp.Popen(shlex.split(p),
                                stdin=procs[i - 1].stdout,
                                stdout=sp.PIPE)
            procs[i - 1].stdout.close()
    output = procs[len(procs) - 1].communicate()[0]

    # we create a temporary buffer where the command and its output will be shown

    # we always splitbelow
    splitbelow = bool(int(vim.eval("&splitbelow")))
    if not splitbelow:
        ex("set splitbelow")

    ex("5new")
    vim.current.buffer[0] = "# Press <Esc> to close this"
    vim.current.buffer.append("▶ " + " | ".join(steps))
    try:
        for line in output.split("\n"):
            vim.current.buffer.append(line)
    except:
        pass
    ex("setlocal nomodified")
    ex("setlocal nomodifiable")
    # pressing <esc> on the buffer will delete it
    ex("map <buffer> <esc> :bd<cr>")
    # we will highlight some elements in the buffer
    ex("syn match PandocOutputMarks /^>>/")
    ex("syn match PandocCommand /^▶.*$/hs=s+1")
    ex("syn match PandocInstructions /^#.*$/")
    ex("hi! link PandocOutputMarks Operator")
    ex("hi! link PandocCommand Statement")
    ex("hi! link PandocInstructions Comment")

    # we revert splitbelow to its original value
    if not splitbelow:
        ex("set nosplitbelow")

    # finally, we open the created file
    if os.path.exists(out) and open_when_done:
        if sys.platform == "darwin":
            pandoc_open_command = "open"  #OSX
        elif sys.platform.startswith("linux"):
            pandoc_open_command = "xdg-open"  # freedesktop/linux
        elif sys.platform.startswith("win"):
            pandoc_open_command = 'cmd /x \"start'  # Windows
        # On windows, we pass commands as an argument to `start`,
        # which is a cmd.exe builtin, so we have to quote it
        if sys.platform.startswith("win"):
            pandoc_open_command_tail = '"'
        else:
            pandoc_open_command_tail = ''

        sp.Popen([pandoc_open_command, out + pandoc_open_command_tail],
                 stdout=sp.PIPE,
                 stderr=sp.PIPE)
Exemplo n.º 4
0
def execute(command, output_type="html", open_when_done=False):
    # first, we parse the command description as a list of strings
    steps = []
    for step in command.split("|"):
        step = step.strip()
        # we expand some values using vim's expand function.
        # check :help filename-modifiers
        step = re.sub("%<?((:[8~.hpret])?)*", lambda i: vim.eval('expand("' + i.group() + '")'), step)
        # we substitute some variables
        for sub in variable_substitutions:
            step = re.sub(sub, variable_substitutions[sub], step)
        steps.append(step)

    out = os.path.splitext(vim.current.buffer.name)[0] + "." + output_type

    # now, we run the pipe
    procs = {}
    procs[0] = sp.Popen(shlex.split(steps[0]), stdout=sp.PIPE)
    if len(steps) > 1:
        i = 1
        for p in steps[1:]:
            procs[i] = sp.Popen(shlex.split(p), stdin=procs[i-1].stdout, stdout=sp.PIPE)
            procs[i-1].stdout.close()
    output = procs[len(procs) - 1].communicate()[0]

    # we create a temporary buffer where the command and its output will be shown

    # we always splitbelow
    splitbelow = bool(int(vim.eval("&splitbelow")))
    if not splitbelow:
        ex("set splitbelow")

    ex("5new")
    vim.current.buffer[0] = "# Press <Esc> to close this"
    vim.current.buffer.append("▶ " + " | ".join(steps))
    try:
        for line in output.split("\n"):
            vim.current.buffer.append(line)
    except:
        pass
    ex("setlocal nomodified")
    ex("setlocal nomodifiable")
    # pressing <esc> on the buffer will delete it
    ex("map <buffer> <esc> :bd<cr>")
    # we will highlight some elements in the buffer
    ex("syn match PandocOutputMarks /^>>/")
    ex("syn match PandocCommand /^▶.*$/hs=s+1")
    ex("syn match PandocInstructions /^#.*$/")
    ex("hi! link PandocOutputMarks Operator")
    ex("hi! link PandocCommand Statement")
    ex("hi! link PandocInstructions Comment")

    # we revert splitbelow to its original value
    if not splitbelow:
        ex("set nosplitbelow")

    # finally, we open the created file
    if os.path.exists(out) and open_when_done:
        if sys.platform == "darwin":
            pandoc_open_command = "open" #OSX
        elif sys.platform.startswith("linux"):
            pandoc_open_command = "xdg-open" # freedesktop/linux
        elif sys.platform.startswith("win"):
            pandoc_open_command = 'cmd /x \"start' # Windows
        # On windows, we pass commands as an argument to `start`,
        # which is a cmd.exe builtin, so we have to quote it
        if sys.platform.startswith("win"):
            pandoc_open_command_tail = '"'
        else:
            pandoc_open_command_tail = ''

        sp.Popen([pandoc_open_command,  out + pandoc_open_command_tail], stdout=sp.PIPE, stderr=sp.PIPE)