[PATCH 2/2] vis: implement gf and <C-w>gf to open filename under cursor

From: Marc André Tanner <mat_AT_brain-dump.org>
Date: Sun, 7 Feb 2016 16:07:44 +0100

Based on a patch by Silvan Jegen.
---
 config.def.h |  2 ++
 main.c       | 40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+)
diff --git a/config.def.h b/config.def.h
index 47ed326..774871b 100644
--- a/config.def.h
+++ b/config.def.h
_AT_@ -233,6 +233,8 @@ static const KeyBinding bindings_normal[] = {
 	{ "gP",                 ACTION(PUT_BEFORE_END)                      },
 	{ "~",                  ALIAS("<vis-operator-case-swap>l")          },
 	{ "<End>",              ALIAS("$")                                  },
+	{ "gf",                 ACTION(OPEN_FILE_UNDER_CURSOR)              },
+	{ "<C-w>gf",            ACTION(OPEN_FILE_UNDER_CURSOR_NEW_WINDOW)   },
 	{ 0 /* empty last element, array terminator */                      },
 };
 
diff --git a/main.c b/main.c
index c3ae7a3..21a1066 100644
--- a/main.c
+++ b/main.c
_AT_@ -111,6 +111,8 @@ static const char *unicode_info(Vis*, const char *keys, const Arg *arg);
 static const char *percent(Vis*, const char *keys, const Arg *arg);
 /* either increment (arg->i > 0) or decrement (arg->i < 0) number under cursor */
 static const char *number_increment_decrement(Vis*, const char *keys, const Arg *arg);
+/* open a filename under cursor in same (!arg->b) or new (arg->b) window */
+static const char *open_file_under_cursor(Vis*, const char *keys, const Arg *arg);
 
 enum {
 	VIS_ACTION_EDITOR_SUSPEND,
_AT_@ -272,6 +274,8 @@ enum {
 	VIS_ACTION_UNICODE_INFO,
 	VIS_ACTION_NUMBER_INCREMENT,
 	VIS_ACTION_NUMBER_DECREMENT,
+	VIS_ACTION_OPEN_FILE_UNDER_CURSOR,
+	VIS_ACTION_OPEN_FILE_UNDER_CURSOR_NEW_WINDOW,
 	VIS_ACTION_NOP,
 };
 
_AT_@ -1071,6 +1075,16 @@ static KeyAction vis_action[] = {
 		"Decrement number under cursor",
 		number_increment_decrement, { .i = -1 }
 	},
+	[VIS_ACTION_OPEN_FILE_UNDER_CURSOR] = {
+		"open-file-under-cursor",
+		"Open file under the cursor",
+		open_file_under_cursor, { .b = false }
+	},
+	[VIS_ACTION_OPEN_FILE_UNDER_CURSOR_NEW_WINDOW] = {
+		"open-file-under-cursor-new-cursor",
+		"Open file under the cursor in a new window",
+		open_file_under_cursor, { .b = true }
+	},
 	[VIS_ACTION_NOP] = {
 		"nop",
 		"Ignore key, do nothing",
_AT_@ -1677,6 +1691,32 @@ static const char *number_increment_decrement(Vis *vis, const char *keys, const
 	return keys;
 }
 
+static const char *open_file_under_cursor(Vis *vis, const char *keys, const Arg *arg) {
+	Win *win = vis_window(vis);
+	View *view = vis_view(vis);
+	Text *txt = vis_text(vis);
+
+	if (!arg->b && !vis_window_closable(win)) {
+		vis_info_show(vis, "No write since last change");
+		return keys;
+	}
+
+	for (Cursor *c = view_cursors(view); c; c = view_cursors_next(c)) {
+		Filerange r = text_object_filename(txt, view_cursors_pos(c));
+		if (!text_range_valid(&r))
+			continue;
+		char *name = text_bytes_alloc0(txt, r.start, text_range_size(&r));
+		if (name && vis_window_new(vis, name) && !arg->b) {
+			vis_window_close(win);
+			free(name);
+			return keys;
+		}
+		free(name);
+	}
+
+	return keys;
+}
+
 static Vis *vis;
 
 static void signal_handler(int signum, siginfo_t *siginfo, void *context) {
-- 
2.1.4
--BXVAT5kNtrzKuDFl--
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Mon Feb 08 2016 - 03:12:10 CET