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

From: Silvan Jegen <s.jegen_AT_gmail.com>
Date: Tue, 17 May 2016 21:30:44 +0200

We use the new infrastructure for autocompletion to implement
autocompletion of file names within the working directory of vis.
---
v2 changes:
	- pass empty Filerange
 config.def.h |  1 +
 main.c       | 19 +++++++++++++++++++
 2 files changed, 20 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 34cd81d..1a8893c 100644
--- a/main.c
+++ b/main.c
_AT_@ -131,6 +131,8 @@ static void insert_dialog_selection(Vis*, Filerange range, const char *cmdline,
 static char *get_output_of_external_command(Vis*, Filerange range, 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_@ -2160,6 +2168,17 @@ 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;
+
+	insert_dialog_selection(vis, text_range_empty(), "ls | grep \"^%s\" | sort | vis-menu | 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) {
 	Text *txt = vis_text(vis);
 	size_t txtsize = text_size(txt);
-- 
2.8.2
Received on Tue May 17 2016 - 21:30:44 CEST

This archive was generated by hypermail 2.3.0 : Tue May 17 2016 - 21:36:17 CEST