vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

commit 0d3a998c649f14dc356a297fd78b112cecac87dd
parent 3d6cf2177f5c690910cf00b930633259a1008a0f
Author: Silvan Jegen <s.jegen@gmail.com>
Date:   Mon, 16 May 2016 13:00:59 +0200

vis: add completion for file names in current directory via <C-x><C-f>

Diffstat:
Mconfig.def.h | 1+
Mmain.c | 22++++++++++++++++++++++
2 files changed, 23 insertions(+), 0 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -320,6 +320,7 @@ static const KeyBinding bindings_insert[] = { { "<C-i>", ALIAS("<Tab>") }, { "<C-j>", ALIAS("<Enter>") }, { "<C-n>", ACTION(COMPLETE_WORD) }, + { "<C-x><C-f>", ACTION(COMPLETE_FILENAME) }, { "<C-m>", ALIAS("<Enter>") }, { "<C-o>", ACTION(MODE_OPERATOR_PENDING) }, { "<C-r>", ACTION(INSERT_REGISTER) }, diff --git a/main.c b/main.c @@ -127,6 +127,8 @@ static const char *number_increment_decrement(Vis*, const char *keys, const Arg static const char *open_file_under_cursor(Vis*, const char *keys, const Arg *arg); /* complete input text at cursor based on the words in the current file */ static const char *complete_word(Vis*, const char *keys, const Arg *arg); +/* complete input text at cursor based on file names of the current directory */ +static const char *complete_filename(Vis*, const char *keys, const Arg *arg); enum { VIS_ACTION_EDITOR_SUSPEND, @@ -310,6 +312,7 @@ enum { VIS_ACTION_OPEN_FILE_UNDER_CURSOR, VIS_ACTION_OPEN_FILE_UNDER_CURSOR_NEW_WINDOW, VIS_ACTION_COMPLETE_WORD, + VIS_ACTION_COMPLETE_FILENAME, VIS_ACTION_NOP, }; @@ -1219,6 +1222,11 @@ static const KeyAction vis_action[] = { "Complete word in file", complete_word, }, + [VIS_ACTION_COMPLETE_FILENAME] = { + "complete-filename", + "Complete file name", + complete_filename, + }, [VIS_ACTION_NOP] = { "nop", "Ignore key, do nothing", @@ -2149,6 +2157,20 @@ static const char *complete_word(Vis *vis, const char *keys, const Arg *arg) { return keys; } +static const char *complete_filename(Vis *vis, const char *keys, const Arg *arg) { + Buffer cmd; + buffer_init(&cmd); + char *prefix = get_completion_prefix(vis); + if (prefix && buffer_printf(&cmd, "ls | grep '^%s' | sort | " VIS_MENU + " | tr -d '\n' | sed 's/%s//'", prefix, prefix)) { + Filerange empty = text_range_new(0, 0); + insert_dialog_selection(vis, &empty, (const char*[]){ buffer_content0(&cmd), NULL }); + } + buffer_release(&cmd); + free(prefix); + return keys; +} + static Vis *vis; static void signal_handler(int signum, siginfo_t *siginfo, void *context) {