From 4f865e66ed701d675152eb5bb5c93d7114f7abe4 Mon Sep 17 00:00:00 2001 From: bjp Date: Fri, 3 Jul 2015 22:23:35 +0300 Subject: [PATCH] Add more keyboard shortcuts to the prompt This adds the following readline/emacs style shortcuts to the prompt: Ctrl+p, Ctrl+n, Alt+b, Alt+f, Alt+d, Alt+BackSpace. Closes https://github.com/awesomeWM/awesome/pull/282. --- lib/awful/prompt.lua | 67 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/lib/awful/prompt.lua b/lib/awful/prompt.lua index 9ba889b0..cf133193 100644 --- a/lib/awful/prompt.lua +++ b/lib/awful/prompt.lua @@ -64,6 +64,42 @@ local function history_check_load(id, max) end end +local function is_word_char(c) + if string.find("[{[(,.:;_-+=@/ ]", c) then + return false + else + return true + end +end + +local function cword_start(s, pos) + local i = pos + if i > 1 then + i = i - 1 + end + while i >= 1 and not is_word_char(s:sub(i, i)) do + i = i - 1 + end + while i >= 1 and is_word_char(s:sub(i, i)) do + i = i - 1 + end + if i <= #s then + i = i + 1 + end + return i +end + +local function cword_end(s, pos) + local i = pos + while i <= #s and not is_word_char(s:sub(i, i)) do + i = i + 1 + end + while i <= #s and is_word_char(s:sub(i, i)) do + i = i + 1 + end + return i +end + --- Save history table in history file -- @param id The data.history identifier local function history_save(id) @@ -308,6 +344,25 @@ function prompt.run(args, textbox, exe_callback, completion_callback, history_pa if cur_pos <= #command then command = command:sub(1, cur_pos - 1) .. command:sub(cur_pos + 1) end + elseif key == "p" then + if history_index > 1 then + history_index = history_index - 1 + + command = data.history[history_path].table[history_index] + cur_pos = #command + 2 + end + elseif key == "n" then + if history_index < history_items(history_path) then + history_index = history_index + 1 + + command = data.history[history_path].table[history_index] + cur_pos = #command + 2 + elseif history_index == history_items(history_path) then + history_index = history_index + 1 + + command = "" + cur_pos = 1 + end elseif key == "e" then cur_pos = #command + 1 elseif key == "r" then @@ -399,6 +454,18 @@ function prompt.run(args, textbox, exe_callback, completion_callback, history_pa end end end + elseif mod.Mod1 or mod.Mod3 then + if key == "b" then + cur_pos = cword_start(command, cur_pos) + elseif key == "f" then + cur_pos = cword_end(command, cur_pos) + elseif key == "d" then + command = command:sub(1, cur_pos - 1) .. command:sub(cword_end(command, cur_pos)) + elseif key == "BackSpace" then + wstart = cword_start(command, cur_pos) + command = command:sub(1, wstart - 1) .. command:sub(cur_pos) + cur_pos = wstart + end else if completion_callback then if key == "Tab" or key == "ISO_Left_Tab" then