prompt: CTRL+DELETE deletes history entries

Pressing CTRL+DELETE removes the visible history entry, if any, then moves to the next history entry (like pressing DOWN would do).
If the last history entry is removed the previous one is shown in the prompt (like pressing UP would do).
CTRL+DELETE works on history entries only: i.e. it has no effect on a command entered but not executed yet.
To implement above behaviour I added saving history table to file on Escape key press.

Signed-off-by: Massimiliano Brocchini <massimiliano.brocchini@gmail.com>
Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Massimiliano Brocchini 2014-03-09 15:18:56 +01:00 committed by Uli Schlachter
parent 620732a015
commit b965adc33f
1 changed files with 49 additions and 0 deletions

View File

@ -166,6 +166,35 @@ end
-- @param done_callback Optional parameter: the callback function to always call without arguments, regardless of whether the prompt was cancelled. -- @param done_callback Optional parameter: the callback function to always call without arguments, regardless of whether the prompt was cancelled.
-- @param changed_callback Optional parameter: the callback function to call with command as argument when a command was changed. -- @param changed_callback Optional parameter: the callback function to call with command as argument when a command was changed.
-- @param keypressed_callback Optional parameter: the callback function to call with mod table, key and command as arguments when a key was pressed. -- @param keypressed_callback Optional parameter: the callback function to call with mod table, key and command as arguments when a key was pressed.
-- @usage The following readline keyboard shortcuts are implemented as expected:
-- <ul>
-- <li><code>CTRL+A</code></li>
-- <li><code>CTRL+B</code></li>
-- <li><code>CTRL+C</code></li>
-- <li><code>CTRL+D</code></li>
-- <li><code>CTRL+E</code></li>
-- <li><code>CTRL+J</code></li>
-- <li><code>CTRL+M</code></li>
-- <li><code>CTRL+F</code></li>
-- <li><code>CTRL+H</code></li>
-- <li><code>CTRL+K</code></li>
-- <li><code>CTRL+U</code></li>
-- <li><code>CTRL+W</code></li>
-- <li><code>CTRL+BASKPACE</code></li>
-- <li><code>SHIFT+INSERT</code></li>
-- <li><code>HOME</code></li>
-- <li><code>END</code></li>
-- <li>arrow keys</li>
-- </ul>
-- <br/>
-- The following shortcuts implement additional history manipulation commands where the search term is defined as the substring of command from first character to cursor position
-- <ul>
-- <li><code>CTRL+R</code>: reverse history search, matches any history entry containing search term</li>
-- <li><code>CTRL+S</code>: forward history search, matches any history entry containing search term</li>
-- <li><code>CTRL+UP</code>: ZSH up line or search, matches any history entry starting with search term</li>
-- <li><code>CTRL+DOWN</code>: ZSH down line or search, matches any history entry starting with search term</li>
-- <li><code>CTRL+DELETE</code>: delete the currently visible history entry from history file. <br/>Does not delete new commands or history entries under user editing</li>
-- </ul>
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()
@ -251,6 +280,7 @@ function prompt.run(args, textbox, exe_callback, completion_callback, history_pa
or (not mod.Control and key == "Escape") then or (not mod.Control and key == "Escape") then
keygrabber.stop(grabber) keygrabber.stop(grabber)
textbox:set_markup("") textbox:set_markup("")
history_save(history_path)
if done_callback then done_callback() end if done_callback then done_callback() end
return false return false
elseif (mod.Control and (key == "j" or key == "m")) elseif (mod.Control and (key == "j" or key == "m"))
@ -346,6 +376,25 @@ function prompt.run(args, textbox, exe_callback, completion_callback, history_pa
end end
command = command:sub(1, cword_start - 1) .. command:sub(cword_end + 1) command = command:sub(1, cword_start - 1) .. command:sub(cword_end + 1)
cur_pos = cword_start cur_pos = cword_start
elseif key == "Delete" then
-- delete from history only if:
-- we are not dealing with a new command
-- the user has not edited an existing entry
if command == data.history[history_path].table[history_index] then
table.remove(data.history[history_path].table, history_index)
if history_index <= history_items(history_path) then
command = data.history[history_path].table[history_index]
cur_pos = #command + 2
elseif history_index > 1 then
history_index = history_index - 1
command = data.history[history_path].table[history_index]
cur_pos = #command + 2
else
command = ""
cur_pos = 1
end
end
end end
else else
if completion_callback then if completion_callback then