From 956ac3c50db7c139086c7f4dd62ec6a1727d2008 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 18 Apr 2017 00:05:33 +0200 Subject: [PATCH 1/4] awful.completion.shell: fix cur_pos return value --- lib/awful/completion.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/awful/completion.lua b/lib/awful/completion.lua index d87e4e629..630929d1f 100644 --- a/lib/awful/completion.lua +++ b/lib/awful/completion.lua @@ -156,7 +156,7 @@ function completion.shell(command, cur_pos, ncomp, shell) end local str = command:sub(1, cword_start - 1) .. output[ncomp] .. command:sub(cword_end) - cur_pos = cword_end + #output[ncomp] + 1 + cur_pos = cword_start + #output[ncomp] return str, cur_pos, output end From 2a69ffa374c1429e10f91aa80ceda06d6accbec4 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 17 Apr 2017 13:28:40 +0200 Subject: [PATCH 2/4] awful.completion: zsh: fix command completion from PWD Fixes https://github.com/awesomeWM/awesome/issues/1734. --- lib/awful/completion.lua | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/awful/completion.lua b/lib/awful/completion.lua index 630929d1f..c5e640f8b 100644 --- a/lib/awful/completion.lua +++ b/lib/awful/completion.lua @@ -103,15 +103,17 @@ function completion.shell(command, cur_pos, ncomp, shell) -- NOTE: ${~:-"..."} turns on GLOB_SUBST, useful for expansion of -- "~/" ($HOME). ${:-"foo"} is the string "foo" as var. shell_cmd = "/usr/bin/env zsh -c 'local -a res; res=( ${~:-" - .. string.format('%q', words[cword_index]) .. "}* ); " + .. string.format('%q', words[cword_index]) .. "}*(N) ); " .. "print -ln -- ${res[@]}'" else - -- check commands, aliases, builtins, functions and reswords - shell_cmd = "/usr/bin/env zsh -c 'local -a res; ".. + -- Check commands, aliases, builtins, functions and reswords. + -- Adds executables and non-empty dirs from $PWD (pwd_exe). + shell_cmd = "/usr/bin/env zsh -c 'local -a res pwd_exe; ".. + "pwd_exe=(*(N*:t) *(NF:t)); ".. "res=( ".. "\"${(k)commands[@]}\" \"${(k)aliases[@]}\" \"${(k)builtins[@]}\" \"${(k)functions[@]}\" ".. "\"${(k)reswords[@]}\" ".. - "${PWD}/*(:t)".. + "./${^${pwd_exe}} ".. "); ".. "print -ln -- ${(M)res[@]:#" .. string.format('%q', words[cword_index]) .. "*}'" end From 4c792b49a977b8b4ae1830996e691e72235ce0ac Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 17 Apr 2017 14:10:38 +0200 Subject: [PATCH 3/4] awful.completion: handle first word as command always This is necessary for `./*`, but should apply in general. It was changed in 3986409e, but there is no argument for it in the commit (message) really. --- lib/awful/completion.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/awful/completion.lua b/lib/awful/completion.lua index c5e640f8b..30f430ee9 100644 --- a/lib/awful/completion.lua +++ b/lib/awful/completion.lua @@ -93,7 +93,7 @@ function completion.shell(command, cur_pos, ncomp, shell) i = i + 1 end - if cword_index == 1 and not string.find(words[cword_index], "/") then + if cword_index == 1 then comptype = "command" end From 0c97d1aa95484b7c8da95b9c4157f1b289ef83ca Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 18 Apr 2017 00:05:06 +0200 Subject: [PATCH 4/4] Add spec/awful/completion_spec.lua --- .travis.yml | 4 +-- spec/awful/completion_spec.lua | 63 ++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 spec/awful/completion_spec.lua diff --git a/.travis.yml b/.travis.yml index 91a80c9e2..0e4f161e0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -40,8 +40,8 @@ install: # See also `apt-cache showsrc awesome | grep -E '^(Version|Build-Depends)'`. - sudo apt-get install -y libcairo2-dev gir1.2-gtk-3.0 xmlto asciidoc libpango1.0-dev libxcb-xtest0-dev libxcb-icccm4-dev libxcb-randr0-dev libxcb-keysyms1-dev libxcb-xinerama0-dev libdbus-1-dev libxdg-basedir-dev libstartup-notification0-dev imagemagick libxcb1-dev libxcb-shape0-dev libxcb-util0-dev libx11-xcb-dev libxcb-cursor-dev libxcb-xkb-dev libxkbcommon-dev libxkbcommon-x11-dev - # Deps for functional tests. - - sudo apt-get install -y dbus-x11 xterm xdotool xterm xvfb + # Deps for tests. + - sudo apt-get install -y dbus-x11 xterm xdotool xterm xvfb zsh # Need xorg-macros - sudo apt-get install -y xutils-dev diff --git a/spec/awful/completion_spec.lua b/spec/awful/completion_spec.lua new file mode 100644 index 000000000..1f6fbfea0 --- /dev/null +++ b/spec/awful/completion_spec.lua @@ -0,0 +1,63 @@ +local compl = require("awful.completion") +local shell = function(...) + local command, pos, matches = compl.shell(...) + return {command, pos, matches} +end +local gfs = require("gears.filesystem") + +describe("awful.completion.shell in empty directory", function() + local orig_popen = io.popen + + setup(function() + gfs.make_directories('/tmp/awesome-tests/empty') + io.popen = function(...) --luacheck: ignore + return orig_popen('cd /tmp/awesome-tests/empty && ' .. ...) + end + end) + + teardown(function() + os.remove('/tmp/awesome-tests/empty') + io.popen = orig_popen --luacheck: ignore + end) + + it("handles completion of true", function() + assert.same(shell('true', 5, 1, 'zsh'), {'true', 5, {'true'}}) + assert.same(shell('true', 5, 1, 'bash'), {'true', 5, {'true'}}) + assert.same(shell('true', 5, 1, nil), {'true', 5, {'true'}}) + end) +end) + +describe("awful.completion.shell", function() + local orig_popen = io.popen + + setup(function() + gfs.make_directories('/tmp/awesome-tests/dir') + os.execute('cd /tmp/awesome-tests/dir && touch localcommand && chmod +x localcommand') + io.popen = function(...) --luacheck: ignore + return orig_popen('cd /tmp/awesome-tests/dir && ' .. ...) + end + end) + + teardown(function() + os.remove('/tmp/awesome-tests/dir') + io.popen = orig_popen --luacheck: ignore + end) + + it("handles empty input", function() + assert.same(shell('', 1, 1, 'zsh'), {'', 1}) + assert.same(shell('', 1, 1, 'bash'), {'', 1}) + assert.same(shell('', 1, 1, nil), {'', 1}) + end) + + it("completes local command", function() + assert.same(shell('./localcomm', 12, 1, 'zsh'), {'./localcommand', 15, {'./localcommand'}}) + assert.same(shell('./localcomm', 12, 1, 'bash'), {'./localcommand', 15, {'./localcommand'}}) + end) + + it("completes local file", function() + assert.same(shell('ls ', 4, 1, 'zsh'), {'ls localcommand', 16, {'localcommand'}}) + assert.same(shell('ls ', 4, 1, 'bash'), {'ls localcommand', 16, {'localcommand'}}) + end) +end) + +-- vim: ft=lua:et:sw=4:ts=8:sts=4:tw=80