diff --git a/lib/awful/completion.lua b/lib/awful/completion.lua index 30f430ee9..ef819cb80 100644 --- a/lib/awful/completion.lua +++ b/lib/awful/completion.lua @@ -18,6 +18,8 @@ local print = print local pairs = pairs local string = string +local gears_debug = require("gears.debug") + local completion = {} -- mapping of command/completion function @@ -54,6 +56,8 @@ local function bash_escape(str) return str end +completion.default_shell = nil + --- Use shell completion system to complete commands and filenames. -- @tparam string command The command line. -- @tparam number cur_pos The cursor position. @@ -98,7 +102,21 @@ function completion.shell(command, cur_pos, ncomp, shell) end local shell_cmd - if shell == "zsh" or (not shell and os.getenv("SHELL"):match("zsh$")) then + if not shell then + if not completion.default_shell then + local env_shell = os.getenv('SHELL') + if not env_shell then + gears_debug.print_warning('SHELL not set in environment, falling back to bash.') + completion.default_shell = 'bash' + elseif env_shell:match('zsh$') then + completion.default_shell = 'zsh' + else + completion.default_shell = 'bash' + end + end + shell = completion.default_shell + end + if shell == 'zsh' then if comptype == "file" then -- NOTE: ${~:-"..."} turns on GLOB_SUBST, useful for expansion of -- "~/" ($HOME). ${:-"foo"} is the string "foo" as var. diff --git a/spec/awful/completion_spec.lua b/spec/awful/completion_spec.lua index 1f6fbfea0..2c1507fc4 100644 --- a/spec/awful/completion_spec.lua +++ b/spec/awful/completion_spec.lua @@ -60,4 +60,55 @@ describe("awful.completion.shell", function() end) end) +describe("awful.completion.shell handles $SHELL", function() + local orig_getenv = os.getenv + local gdebug = require("gears.debug") + local orig_print_warning + local print_warning_message + local os_getenv_shell + + setup(function() + os.getenv = function(name) -- luacheck: ignore + if name == 'SHELL' then return os_getenv_shell end + return orig_getenv(name) + end + + orig_print_warning = gdebug.print_warning + gdebug.print_warning = function(message) + print_warning_message = message + end + end) + + teardown(function() + os.getenv = orig_getenv --luacheck: ignore + gdebug.print_warning = orig_print_warning + end) + + before_each(function() + compl.default_shell = nil + print_warning_message = nil + end) + + it("falls back to bash if unset", function() + assert.same(shell('true', 5, 1, nil), {'true', 5, {'true'}}) + assert.same(print_warning_message, + 'SHELL not set in environment, falling back to bash.') + assert.same(compl.default_shell, "bash") + end) + + it("uses zsh from path", function() + os_getenv_shell = '/opt/bin/zsh' + assert.same(shell('true', 5, 1, nil), {'true', 5, {'true'}}) + assert.same(compl.default_shell, "zsh") + assert.is_nil(print_warning_message) + end) + + it("uses bash for unknown", function() + os_getenv_shell = '/dev/null' + assert.same(shell('true', 5, 1, nil), {'true', 5, {'true'}}) + assert.same(compl.default_shell, "bash") + assert.is_nil(print_warning_message) + end) +end) + -- vim: ft=lua:et:sw=4:ts=8:sts=4:tw=80