[hackers][vis][RFC][PATCH 2/2] Add autocompletion for file names

From: Silvan Jegen <s.jegen_AT_gmail.com>
Date: Mon, 16 May 2016 13:00:59 +0200

We use the new infrastructure for autocompletion to implement
autocompletion of file names within the working directory of vis.
---
 config.def.h |  1 +
 main.c       | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+)
diff --git a/config.def.h b/config.def.h
index 9e52102..5219d00 100644
--- a/config.def.h
+++ b/config.def.h
_AT_@ -320,6 +320,7 @@ static const KeyBinding bindings_insert[] = {
 	{ "<C-i>",              ALIAS("<Tab>")                              },
 	{ "<C-j>",              ALIAS("<Enter>")                            },
 	{ "<C-n>",              ACTION(FILE_TEXT_AUTOCOMPLETE)              },
+	{ "<C-x><C-f>",         ACTION(FILE_NAME_AUTOCOMPLETE)              },
 	{ "<C-m>",              ALIAS("<Enter>")                            },
 	{ "<C-o>",              ACTION(MODE_OPERATOR_PENDING)               },
 	{ "<C-r>",              ACTION(INSERT_REGISTER)                     },
diff --git a/main.c b/main.c
index 9e224d1..e5807c9 100644
--- a/main.c
+++ b/main.c
_AT_@ -131,6 +131,8 @@ static void insert_dialog_selection(Vis*, const char *cmdline, ...);
 static char *get_output_of_external_command(Vis*, const char *argv[]);
 /* Autocomplete input text at cursor based on the words in the current file */
 static const char *autocomplete_file_text(Vis*, const char *keys, const Arg *arg);
+/* Autocomplete input text at cursor based on file names of the current directory */
+static const char *autocomplete_file_name(Vis*, const char *keys, const Arg *arg);
 
 enum {
 	VIS_ACTION_EDITOR_SUSPEND,
_AT_@ -314,6 +316,7 @@ enum {
 	VIS_ACTION_OPEN_FILE_UNDER_CURSOR,
 	VIS_ACTION_OPEN_FILE_UNDER_CURSOR_NEW_WINDOW,
 	VIS_ACTION_FILE_TEXT_AUTOCOMPLETE,
+	VIS_ACTION_FILE_NAME_AUTOCOMPLETE,
 	VIS_ACTION_NOP,
 };
 
_AT_@ -1223,6 +1226,11 @@ static const KeyAction vis_action[] = {
 		"Autocomplete text in file",
 		autocomplete_file_text,
 	},
+	[VIS_ACTION_FILE_NAME_AUTOCOMPLETE] = {
+		"autocomplete-file-name",
+		"Autocomplete file name",
+		autocomplete_file_name,
+	},
 	[VIS_ACTION_NOP] = {
 		"nop",
 		"Ignore key, do nothing",
_AT_@ -2158,6 +2166,18 @@ static char *get_prefix_for_autocomplete(Vis *vis) {
 	return prefix;
 }
 
+static const char *autocomplete_file_name(Vis *vis, const char *keys, const Arg *arg) {
+	char *prefix = get_prefix_for_autocomplete(vis);
+	if (!prefix)
+		return keys;
+
+	// TODO: get menu/dialog program to use from config?
+	insert_dialog_selection(vis, "ls | grep \"^%s\" | sort | dmenu | tr -d '\n' | sed \"s/%s//\"", prefix, prefix);
+
+	free(prefix);
+	return keys;
+}
+
 static const char *autocomplete_file_text(Vis *vis, const char *keys, const Arg *arg) {
 	Win *win = vis_window(vis);
 	const char *fn = vis_window_filename(win);
-- 
2.8.2
Received on Mon May 16 2016 - 13:00:59 CEST

This archive was generated by hypermail 2.3.0 : Mon May 16 2016 - 13:12:18 CEST