awful.completion: add zsh completion
Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
parent
2a2166d856
commit
c635d3e2d1
|
@ -231,7 +231,7 @@ globalkeys =
|
||||||
function ()
|
function ()
|
||||||
awful.prompt.run({ prompt = "Run: " },
|
awful.prompt.run({ prompt = "Run: " },
|
||||||
mypromptbox[mouse.screen],
|
mypromptbox[mouse.screen],
|
||||||
awful.util.spawn, awful.completion.bash,
|
awful.util.spawn, awful.completion.shell,
|
||||||
awful.util.getdir("cache") .. "/history")
|
awful.util.getdir("cache") .. "/history")
|
||||||
end),
|
end),
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ local os = os
|
||||||
local table = table
|
local table = table
|
||||||
local math = math
|
local math = math
|
||||||
local print = print
|
local print = print
|
||||||
|
local util = require("awful.util")
|
||||||
|
|
||||||
--- Completion module for awful
|
--- Completion module for awful
|
||||||
module("awful.completion")
|
module("awful.completion")
|
||||||
|
@ -49,12 +50,13 @@ local function bash_escape(str)
|
||||||
return str
|
return str
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Use bash completion system to complete command and filename.
|
--- Use shell completion system to complete command and filename.
|
||||||
-- @param command The command line.
|
-- @param command The command line.
|
||||||
-- @param cur_pos The cursor position.
|
-- @param cur_pos The cursor position.
|
||||||
-- @param ncomp The element number to complete.
|
-- @param ncomp The element number to complete.
|
||||||
|
-- @param shell The shell to use for completion (bash (default) or zsh).
|
||||||
-- @return The new command and the new cursor position.
|
-- @return The new command and the new cursor position.
|
||||||
function bash(command, cur_pos, ncomp)
|
function shell(command, cur_pos, ncomp, shell)
|
||||||
local wstart = 1
|
local wstart = 1
|
||||||
local wend = 1
|
local wend = 1
|
||||||
local words = {}
|
local words = {}
|
||||||
|
@ -88,18 +90,31 @@ function bash(command, cur_pos, ncomp)
|
||||||
comptype = "command"
|
comptype = "command"
|
||||||
end
|
end
|
||||||
|
|
||||||
local bash_cmd
|
local shell_cmd
|
||||||
if bashcomp_funcs[words[1]] then
|
if shell == "zsh" or (not shell and os.getenv("SHELL"):match("zsh$")) then
|
||||||
-- fairly complex command with inline bash script to get the possible completions
|
if comptype == "file" then
|
||||||
bash_cmd = "/usr/bin/env bash -c 'source " .. bashcomp_src .. "; " ..
|
shell_cmd = "/usr/bin/env zsh -c 'local -a res; res=( " .. words[cword_index] .. "* ); print -l -- ${res[@]}'"
|
||||||
"__print_completions() { for ((i=0;i<${#COMPREPLY[*]};i++)); do echo ${COMPREPLY[i]}; done }; " ..
|
else
|
||||||
"COMP_WORDS=(" .. command .."); COMP_LINE=\"" .. command .. "\"; " ..
|
-- check commands, aliases, builtins, functions and reswords
|
||||||
"COMP_COUNT=" .. cur_pos .. "; COMP_CWORD=" .. cword_index-1 .. "; " ..
|
shell_cmd = "/usr/bin/env zsh -c 'local -a res; "..
|
||||||
bashcomp_funcs[words[1]] .. "; __print_completions | sort -u'"
|
"res=( "..
|
||||||
|
"\"${(k)commands[@]}\" \"${(k)aliases[@]}\" \"${(k)builtins[@]}\" \"${(k)functions[@]}\" \"${(k)reswords[@]}\" "..
|
||||||
|
"); "..
|
||||||
|
"print -l -- ${(M)res[@]:#"..words[cword_index].."*}'"
|
||||||
|
end
|
||||||
else
|
else
|
||||||
bash_cmd = "/usr/bin/env bash -c 'compgen -A " .. comptype .. " " .. words[cword_index] .. "'"
|
if bashcomp_funcs[words[1]] then
|
||||||
|
-- fairly complex command with inline bash script to get the possible completions
|
||||||
|
shell_cmd = "/usr/bin/env bash -c 'source " .. bashcomp_src .. "; " ..
|
||||||
|
"__print_completions() { for ((i=0;i<${#COMPREPLY[*]};i++)); do echo ${COMPREPLY[i]}; done }; " ..
|
||||||
|
"COMP_WORDS=(" .. command .."); COMP_LINE=\"" .. command .. "\"; " ..
|
||||||
|
"COMP_COUNT=" .. cur_pos .. "; COMP_CWORD=" .. cword_index-1 .. "; " ..
|
||||||
|
bashcomp_funcs[words[1]] .. "; __print_completions | sort -u'"
|
||||||
|
else
|
||||||
|
shell_cmd = "/usr/bin/env bash -c 'compgen -A " .. comptype .. " " .. words[cword_index] .. "'"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
local c, err = io.popen(bash_cmd)
|
local c, err = io.popen(shell_cmd)
|
||||||
local output = {}
|
local output = {}
|
||||||
i = 0
|
i = 0
|
||||||
if c then
|
if c then
|
||||||
|
@ -133,6 +148,11 @@ function bash(command, cur_pos, ncomp)
|
||||||
return str, cur_pos
|
return str, cur_pos
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function bash(...)
|
||||||
|
util.deprecate("awful.completion.shell")
|
||||||
|
return shell(args)
|
||||||
|
end
|
||||||
|
|
||||||
--- Run a generic completion.
|
--- Run a generic completion.
|
||||||
-- For this function to run properly the awful.completion.keyword table should
|
-- For this function to run properly the awful.completion.keyword table should
|
||||||
-- be fed up with all keywords. The completion is run against these keywords.
|
-- be fed up with all keywords. The completion is run against these keywords.
|
||||||
|
|
Loading…
Reference in New Issue