prompt: Add the ability to add new shortcuts to awful.prompt

Example:

awful.prompt.run({ prompt = "Run: ", hooks = {
    {{         },"Return",function(command)
         local result = awful.util.spawn(command)
         mypromptbox[mouse.screen].widget:set_text(type(result) == "string" and result or "")
         return true
    end},
    {{"Mod1"   },"Return",function(command)
         local result = awful.util.spawn(command,{sticky=true})
         mypromptbox[mouse.screen].widget:set_text(type(result) == "string" and result or "")
         return true
    end},
    {{"Shift"  },"Return",function(command)
         local result = awful.util.spawn(command,{ontop=true,floating=true})
         mypromptbox[mouse.screen].widget:set_text(type(result) == "string" and result or "")
         return true
    end}
    }
},...)
This commit is contained in:
Emmanuel Lepage Vallee 2014-10-13 17:14:33 -04:00 committed by Daniel Hahler
parent bef7dfad47
commit ce2d31cbde
1 changed files with 60 additions and 7 deletions

View File

@ -224,7 +224,26 @@ end
-- user editing. -- user editing.
-- --
-- @tparam table args A table with optional arguments: `fg_cursor`, `bg_cursor`, -- @tparam table args A table with optional arguments: `fg_cursor`, `bg_cursor`,
-- `ul_cursor`, `prompt`, `text`, `selectall`, `font`, `autoexec`. -- `ul_cursor`, `prompt`, `text`, `selectall`, `font`, `autoexec`, `hooks`.
-- @tparam[opt] table args.hooks The "hooks" argument uses a syntax similar to
-- `awful.key`. It will call a function for the matching modifiers + key.
-- It receives the command (widget text/input) as an argument.
-- hooks = {
-- -- Apply startup notification properties with Shift-Return.
-- {{"Shift" }, "Return", function(command)
-- mypromptbox[awful.screen.focused()]:spawn_and_handle_error(
-- command, {floating=true})
-- end},
-- -- Override default behavior of "Return": launch commands prefixed
-- -- with ":" in a terminal.
-- {{}, "Return", function(command)
-- if command:sub(1,1) == ":" then
-- command = terminal .. ' -e ' .. command:sub(2)
-- end
-- mypromptbox[awful.screen.focused()]:spawn_and_handle_error(
-- command)
-- end}
-- }
-- @param textbox The textbox to use for the prompt. -- @param textbox The textbox to use for the prompt.
-- @param exe_callback The callback function to call with command as argument -- @param exe_callback The callback function to call with command as argument
-- when finished. -- when finished.
@ -238,7 +257,8 @@ end
-- @param[opt] changed_callback The callback function to call -- @param[opt] changed_callback The callback function to call
-- with command as argument when a command was changed. -- with command as argument when a command was changed.
-- @param[opt] keypressed_callback The callback function to call -- @param[opt] keypressed_callback The callback function to call
-- with mod table, key and command as arguments when a key was pressed. -- with mod table, key and command as arguments when a key was pressed.
function prompt.run(args, textbox, exe_callback, completion_callback, history_path, history_max, done_callback, changed_callback, keypressed_callback) function prompt.run(args, textbox, exe_callback, completion_callback, history_path, history_max, done_callback, changed_callback, keypressed_callback)
local grabber local grabber
local theme = beautiful.get() local theme = beautiful.get()
@ -253,6 +273,7 @@ function prompt.run(args, textbox, exe_callback, completion_callback, history_pa
local text = args.text or "" local text = args.text or ""
local font = args.font or theme.font local font = args.font or theme.font
local selectall = args.selectall local selectall = args.selectall
local hooks = {}
search_term=nil search_term=nil
@ -262,20 +283,36 @@ function prompt.run(args, textbox, exe_callback, completion_callback, history_pa
local cur_pos = (selectall and 1) or text:wlen() + 1 local cur_pos = (selectall and 1) or text:wlen() + 1
-- The completion element to use on completion request. -- The completion element to use on completion request.
local ncomp = 1 local ncomp = 1
if not textbox or not exe_callback then if not textbox or not (exe_callback or args.hooks) then
return return
end end
-- Build the hook map
for k,v in ipairs(args.hooks or {}) do
if #v == 3 then
local mods,key,callback = unpack(v)
if type(callback) == "function" then
hooks[key] = hooks[key] or {}
hooks[key][#hooks[key]+1] = v
else
assert("The hook's 3rd parameter has to be a function.")
end
else
assert("The hook has to have 3 parameters.")
end
end
textbox:set_font(font) textbox:set_font(font)
textbox:set_markup(prompt_text_with_cursor{ textbox:set_markup(prompt_text_with_cursor{
text = text, text_color = inv_col, cursor_color = cur_col, text = text, text_color = inv_col, cursor_color = cur_col,
cursor_pos = cur_pos, cursor_ul = cur_ul, selectall = selectall, cursor_pos = cur_pos, cursor_ul = cur_ul, selectall = selectall,
prompt = prettyprompt }) prompt = prettyprompt })
local exec = function() local function exec(cb)
textbox:set_markup("") textbox:set_markup("")
history_add(history_path, command) history_add(history_path, command)
keygrabber.stop(grabber) keygrabber.stop(grabber)
exe_callback(command) cb(command)
if done_callback then done_callback() end if done_callback then done_callback() end
end end
@ -319,6 +356,22 @@ function prompt.run(args, textbox, exe_callback, completion_callback, history_pa
end end
end end
-- User defined cases
if hooks[key] then
for k,v in ipairs(hooks[key]) do
if #modifiers == #v[1] then
local match = true
for k2,v2 in ipairs(v[1]) do
match = match and mod[v2]
end
if match or #modifiers == 0 then
exec(v[3])
return
end
end
end
end
-- Get out cases -- Get out cases
if (mod.Control and (key == "c" or key == "g")) if (mod.Control and (key == "c" or key == "g"))
or (not mod.Control and key == "Escape") then or (not mod.Control and key == "Escape") then
@ -330,7 +383,7 @@ function prompt.run(args, textbox, exe_callback, completion_callback, history_pa
elseif (mod.Control and (key == "j" or key == "m")) elseif (mod.Control and (key == "j" or key == "m"))
or (not mod.Control and key == "Return") or (not mod.Control and key == "Return")
or (not mod.Control and key == "KP_Enter") then or (not mod.Control and key == "KP_Enter") then
exec() exec(exe_callback)
-- We already unregistered ourselves so we don't want to return -- We already unregistered ourselves so we don't want to return
-- true, otherwise we may unregister someone else. -- true, otherwise we may unregister someone else.
return return
@ -497,7 +550,7 @@ function prompt.run(args, textbox, exe_callback, completion_callback, history_pa
key = "" key = ""
-- execute if only one match found and autoexec flag set -- execute if only one match found and autoexec flag set
if matches and #matches == 1 and args.autoexec then if matches and #matches == 1 and args.autoexec then
exec() exec(exe_callback)
return return
end end
else else