From 8696abbe1df9bcc3abe3926a0ea6c9788ae94f58 Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Mon, 16 May 2016 13:00:59 +0200 Subject: vis: add completion for file names in current directory via --- config.def.h | 1 + main.c | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/config.def.h b/config.def.h index c6285e1..af5d8e3 100644 --- a/config.def.h +++ b/config.def.h @@ -320,6 +320,7 @@ static const KeyBinding bindings_insert[] = { { "", ALIAS("") }, { "", ALIAS("") }, { "", ACTION(COMPLETE_WORD) }, + { "", ACTION(COMPLETE_FILENAME) }, { "", ALIAS("") }, { "", ACTION(MODE_OPERATOR_PENDING) }, { "", ACTION(INSERT_REGISTER) }, diff --git a/main.c b/main.c index 995ac72..6baff3b 100644 --- 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) { -- cgit v1.2.3