Hullo, To celebrate hdd crash and some cleanups, I'd like to push these two patches that I have lying around.
Basic idea is to have a command execute automatically on completion if it's the only match. So instead of "mod+r fire<tab> <enter>" we can have "mod+r fire<tab>" and there's firefox on our screen :) 2nd patch adds support for autoexec in awful.completion by returning the table of matches from its callback functions. This could also be used for displaying a list of available completions (think dmenu). koniu
From 9e5cad8b3b3a45b4af1d919ef20e166325f0db35 Mon Sep 17 00:00:00 2001 From: koniu <gkusni...@gmail.com> Date: Mon, 11 Oct 2010 11:33:38 +0100 Subject: [PATCH 1/2] awful.prompt: add 'autoexec' argument If set the prompt will execute the command upon completion which returns only one match. Signed-off-by: koniu <gkusni...@gmail.com> --- lib/awful/prompt.lua.in | 24 +++++++++++++++++------- 1 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/awful/prompt.lua.in b/lib/awful/prompt.lua.in index 2275e6d..a229660 100644 --- a/lib/awful/prompt.lua.in +++ b/lib/awful/prompt.lua.in @@ -143,7 +143,7 @@ local function prompt_text_with_cursor(args) end --- Run a prompt in a box. --- @param args A table with optional arguments: fg_cursor, bg_cursor, ul_cursor, prompt, text, selectall, font. +-- @param args A table with optional arguments: fg_cursor, bg_cursor, ul_cursor, prompt, text, selectall, font, autoexec. -- @param textbox The textbox to use for the prompt. -- @param exe_callback The callback function to call with command as argument when finished. -- @param completion_callback The callback function to call to get completion. @@ -178,6 +178,14 @@ function run(args, textbox, exe_callback, completion_callback, history_path, his cursor_pos = cur_pos, cursor_ul = cur_ul, selectall = selectall, font = font, prompt = prettyprompt } + local exec = function() + textbox.text = "" + history_add(history_path, command) + capi.keygrabber.stop() + exe_callback(command) + if done_callback then done_callback() end + end + capi.keygrabber.run( function (modifiers, key, event) if event ~= "press" then return true end @@ -193,11 +201,7 @@ function run(args, textbox, exe_callback, completion_callback, history_path, his elseif (mod.Control and (key == "j" or key == "m")) or (not mod.Control and key == "Return") or (not mod.Control and key == "KP_Enter") then - textbox.text = "" - history_add(history_path, command) - capi.keygrabber.stop() - exe_callback(command) - if done_callback then done_callback() end + exec() -- We already unregistered ourselves so we don't want to return -- true, otherwise we may unregister someone else. return true @@ -269,9 +273,15 @@ function run(args, textbox, exe_callback, completion_callback, history_path, his command_before_comp = command cur_pos_before_comp = cur_pos end - command, cur_pos = completion_callback(command_before_comp, cur_pos_before_comp, ncomp) + local matches + command, cur_pos, matches = completion_callback(command_before_comp, cur_pos_before_comp, ncomp) ncomp = ncomp + 1 key = "" + -- execute if only one match found and autoexec flag set + if matches and #matches == 1 and args.autoexec then + exec() + return true + end else ncomp = 1 end -- 1.7.1
From 32bc9356126a334ee7eff3d8a2a5774e8a366956 Mon Sep 17 00:00:00 2001 From: koniu <gkusni...@gmail.com> Date: Mon, 11 Oct 2010 11:34:58 +0100 Subject: [PATCH 2/2] awful.completion: callback functions return table of matches Signed-off-by: koniu <gkusni...@gmail.com> --- lib/awful/completion.lua.in | 10 +++++----- 1 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/awful/completion.lua.in b/lib/awful/completion.lua.in index db0391d..e964b53 100644 --- a/lib/awful/completion.lua.in +++ b/lib/awful/completion.lua.in @@ -56,7 +56,7 @@ end -- @param cur_pos The cursor position. -- @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, the new cursor position, the table of all matches. function shell(command, cur_pos, ncomp, shell) local wstart = 1 local wend = 1 @@ -146,7 +146,7 @@ function shell(command, cur_pos, ncomp, shell) local str = command:sub(1, cword_start - 1) .. output[ncomp] .. command:sub(cword_end) cur_pos = cword_end + #output[ncomp] + 1 - return str, cur_pos + return str, cur_pos, output end --- Run a generic completion. @@ -156,7 +156,7 @@ end -- @param cur_pos The current cursor position. -- @param ncomp The number of yet requested completion using current text. -- @param keywords The keywords table uised for completion. --- @return The new match and the new cursor position. +-- @return The new match, the new cursor position, the table of all matches. function generic(text, cur_pos, ncomp, keywords) -- The keywords table may be empty if #keywords == 0 then @@ -180,12 +180,12 @@ function generic(text, cur_pos, ncomp, keywords) -- if there are no matches just leave out with the current text and position if #matches == 0 then - return text, #text + 1 + return text, #text + 1, matches end -- cycle around all matches ncomp = math.mod(ncomp - 1, #matches) + 1 - return matches[ncomp], #matches[ncomp] + 1 + return matches[ncomp], #matches[ncomp] + 1, matches end -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 -- 1.7.1