search in prompt history

Signed-off-by: Massimiliano Brocchini <massimiliano.brocchini@gmail.com>
Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Massimiliano Brocchini 2011-10-24 18:54:55 +02:00 committed by Uli Schlachter
parent 2785d695ce
commit bb3fc70fb3
1 changed files with 47 additions and 0 deletions

View File

@ -26,6 +26,13 @@ module("awful.prompt")
local data = {} local data = {}
data.history = {} data.history = {}
local search_term = nil
local function itera (inc,a, i)
i = i + inc
local v = a[i]
if v then return i,v end
end
-- Load history file in history table -- Load history file in history table
-- @param id The data.history identifier which is the path to the filename -- @param id The data.history identifier which is the path to the filename
-- @param max Optional parameter, the maximum number of entries in file -- @param max Optional parameter, the maximum number of entries in file
@ -166,6 +173,8 @@ function run(args, textbox, exe_callback, completion_callback, history_path, his
local font = args.font or theme.font local font = args.font or theme.font
local selectall = args.selectall local selectall = args.selectall
search_term=nil
history_check_load(history_path, history_max) history_check_load(history_path, history_max)
local history_index = history_items(history_path) + 1 local history_index = history_items(history_path) + 1
-- The cursor position -- The cursor position
@ -224,6 +233,26 @@ function run(args, textbox, exe_callback, completion_callback, history_path, his
end end
elseif key == "e" then elseif key == "e" then
cur_pos = #command + 1 cur_pos = #command + 1
elseif key == "r" then
search_term = search_term or command:sub(1, cur_pos - 1)
for i,v in (function(a,i) return itera(-1,a,i) end), data.history[history_path].table, history_index do
if v:find(search_term) ~= nil then
command=v
history_index=i
cur_pos=#command+1
break
end
end
elseif key == "s" then
search_term = search_term or command:sub(1, cur_pos - 1)
for i,v in (function(a,i) return itera(1,a,i) end), data.history[history_path].table, history_index do
if v:find(search_term) ~= nil then
command=v
history_index=i
cur_pos=#command+1
break
end
end
elseif key == "f" then elseif key == "f" then
if cur_pos <= #command then if cur_pos <= #command then
cur_pos = cur_pos + 1 cur_pos = cur_pos + 1
@ -238,6 +267,24 @@ function run(args, textbox, exe_callback, completion_callback, history_path, his
elseif key == "u" then elseif key == "u" then
command = command:sub(cur_pos, #command) command = command:sub(cur_pos, #command)
cur_pos = 1 cur_pos = 1
elseif key == "Up" then
search_term = search_term or command:sub(1, cur_pos - 1)
for i,v in (function(a,i) return itera(-1,a,i) end), data.history[history_path].table, history_index do
if v:find('^'..search_term) ~= nil then
command=v
history_index=i
break
end
end
elseif key == "Down" then
search_term = search_term or command:sub(1, cur_pos - 1)
for i,v in (function(a,i) return itera(1,a,i) end), data.history[history_path].table, history_index do
if v:find('^'..search_term) ~= nil then
command=v
history_index=i
break
end
end
elseif key == "w" or key == "BackSpace" then elseif key == "w" or key == "BackSpace" then
local wstart = 1 local wstart = 1
local wend = 1 local wend = 1