Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Refael Ackermann committed Oct 3, 2014
1 parent d0b80b7 commit 818a030
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions clink/dll/clink_inputrc_base
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ C-v: paste-from-clipboard
C-q: reload-lua-state
C-z: undo
M-h: show-rl-help
M-C-h: show-rl-history
M-C-c: copy-line-to-clipboard
C-c: ctrl-c
M-a: "..\\"
Expand Down
16 changes: 16 additions & 0 deletions clink/dll/exec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,20 @@ end
--------------------------------------------------------------------------------
clink.register_match_generator(exec_match_generator, 50)


local function history_match_generator(text, first, last)
leading = rl_state.line_buffer:sub(1, last):lower()
if not leading:find("^!") then
return false
end

idx = tonumber(leading:sub(2))
line = clink.get_history_at(idx)
clink.add_match(line)

return true
end
--------------------------------------------------------------------------------
clink.register_match_generator(history_match_generator, 1)

-- vim: expandtab
25 changes: 25 additions & 0 deletions clink/dll/lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,30 @@ static int get_env(lua_State* state)
return 1;
}

//------------------------------------------------------------------------------
static int get_history_at(lua_State* state)
{
char* buffer;

if (lua_gettop(state) == 0)
{
return 0;
}

if (lua_isnil(state, 1))
{
return 0;
}

unsigned idx = lua_tonumber(state, 1);
HIST_ENTRY * hist = history_get(idx);
buffer = strdup(hist->line);
lua_pushstring(state, buffer);
free(buffer);

return 1;
}

//------------------------------------------------------------------------------
static int get_env_var_names(lua_State* state)
{
Expand Down Expand Up @@ -662,6 +686,7 @@ lua_State* initialise_lua()
{ "get_console_aliases", get_console_aliases },
{ "get_cwd", get_cwd },
{ "get_env", get_env },
{ "get_history_at", get_history_at },
{ "get_env_var_names", get_env_var_names },
{ "get_host_process", get_host_process },
{ "get_rl_variable", get_rl_variable },
Expand Down
2 changes: 2 additions & 0 deletions clink/dll/rl_funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
//------------------------------------------------------------------------------
void enter_scroll_mode(int);
int show_rl_help(int, int);
int show_rl_history(int, int);
int get_clink_setting_int(const char*);

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -254,6 +255,7 @@ void clink_register_rl_funcs()
rl_add_funmap_entry("page-up", page_up);
rl_add_funmap_entry("up-directory", up_directory);
rl_add_funmap_entry("show-rl-help", show_rl_help);
rl_add_funmap_entry("show-rl-history", show_rl_history);
rl_add_funmap_entry("copy-line-to-clipboard", copy_line_to_clipboard);
rl_add_funmap_entry("expand-env-vars", expand_env_vars);
}
Expand Down
29 changes: 29 additions & 0 deletions clink/dll/rl_help.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,32 @@ int show_rl_help(int count, int invoking_key)
free(collector);
return 0;
}

#define MAX_HIST_LENGTH 1000
//------------------------------------------------------------------------------
int show_rl_history(int count, int invoking_key)
{
HISTORY_STATE *hist = history_get_history_state();
int max = 16;
int length = min(hist->length, MAX_HIST_LENGTH);
char* collector[MAX_HIST_LENGTH];

int longest = 1;
for (int i = 0; i < length; ++i) {
HIST_ENTRY* h = hist->entries[i];
size_t line_len = strlen(h->line) + strlen("1000: ") + 1;
char * line = malloc(line_len);
sprintf_s(line, line_len, "%4d: %s", i + 1, h->line);
collector[i] = line;
longest = max(longest, strlen(h->line));
}

// Display the matches.
rl_display_match_list(collector, length - 1, longest);
rl_forced_update_display();

for (int i = 0; i < length; ++i) {
free(collector[i]);
}
return 0;
}

0 comments on commit 818a030

Please sign in to comment.