File: init.lua

package info (click to toggle)
vifm 0.14.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,252 kB
  • sloc: ansic: 179,567; sh: 5,445; makefile: 723; perl: 347; python: 76; xml: 26
file content (66 lines) | stat: -rw-r--r-- 1,572 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
--[[

Adds the :Run command that uses Zsh for shell commands' completion.

--]]

local escaped_plugin_path = vifm.escape(vifm.plugin.path)

local function zsh_complete(info)
    local env = ''
    if vifm.expand('$ZSH_CAPTURE_COMPDUMP') == '$ZSH_CAPTURE_COMPDUMP' then
        env = 'ZSH_CAPTURE_COMPDUMP=' .. escaped_plugin_path .. '/zcompdump '
    end

    local job = vifm.startjob {
        cmd = string.format('%s %s %s',
                            env,
                            escaped_plugin_path .. '/capture.zsh',
                            vifm.escape(info.args))
    }

    local matches = {}
    for line in job:stdout():lines() do
        line = line:gsub('\r$', '')
        local match, desc
        local i, j = line:find(' -- ', 1, true)
        if i == nil then
            match = line
        else
            match, desc = line:sub(1, i - 1), line:sub(j + 1)
        end
        matches[#matches + 1] = {match = match, description = desc}
    end

    local exitcode = job:exitcode()
    if exitcode ~= 0 then
        vifm.errordialog('Error', job:errors())
        return false
    end

    return {
        offset = info.args:len() - info.arg:len(),
        matches = matches,
    }
end

local function run(info)
    vifm.run {
        cmd = vifm.expand(info.args),
    }
end

local added = vifm.cmds.add {
    name = 'Run',
    description = 'complete shell commands using Zsh',
    handler = run,
    complete = zsh_complete,
    minargs = 1,
    maxargs = -1,
}

if not added then
    vifm.sb.error('Failed to register :Run')
end

return {}