summaryrefslogtreecommitdiffstats
path: root/558-gf_reimplementation.patch
blob: a2e8658b8eea099181128f3af788359f9fd73ff2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
From 57574a037b4faff9a3f36df12519d2ee4b479824 Mon Sep 17 00:00:00 2001
From: Silvan Jegen <s.jegen@gmail.com>
Date: Sun, 26 Feb 2017 16:47:12 +0100
Subject: [PATCH 1/6] vis: reimplement `gf` and `<C-w>gf` functionality in lua

The functionality is not exactly identical since both bindings open the
file name under the cursor in a new window. The older implementation
opened the file in the same window with `gf` and in a new one with
`<C-w>gf`.
---
 lua/plugins/open-file-under-cursor.lua |   38 +++++++++++++++++++++++++++++++++
 lua/vis-std.lua                        |    2 +
 2 files changed, 40 insertions(+)

--- /dev/null
+++ b/lua/plugins/open-file-under-cursor.lua
@@ -0,0 +1,38 @@
+-- open file at primary cursor location
+
+local lpeg = vis.lpeg
+local l = vis.lexers
+local dq_str = l.delimited_range('"', true)
+local sq_str = l.delimited_range("'", true)
+local include = l.delimited_range("<>", true, true, true)
+local filename = dq_str + sq_str + include + (1 - lpeg.S('"\'\t\v\f\r()[]{} \n'))^1
+
+vis:map(vis.modes.NORMAL, "gf", function(keys)
+	local mstart, mend = vis.win.file:match_at(filename, vis.win.selection.pos, 200)
+	if not mstart or not mend then
+		vis:info("No filename found under the cursor.")
+		return #keys
+	end
+	local fnoffstr = vis.win.file:content(mstart, mend-mstart)
+	local offsetcmd
+	local fn = fnoffstr
+	local offset = fnoffstr:find(":")
+	if not offset then
+		local offset = fnoffstr:find("/")
+	end
+	if offset then
+		offsetcmd = fnoffstr:sub(offset)
+		fn = fnoffstr:sub(1, offset-1)
+	end
+	local ok = vis:command(string.format("open %s", fn))
+	if not ok then
+		vis:info("Could not open file " .. fn)
+	return #keys
+	end
+	if offsetcmd then
+		vis:command(offsetcmd)
+	end
+	return #keys
+end, "Open file under cursor in a new window")
+
+vis:map(vis.modes.NORMAL, "<C-w>gf", "gf")
--- a/lua/vis-std.lua
+++ b/lua/vis-std.lua
@@ -165,3 +165,5 @@ require('plugins/digraph')
 require('plugins/number-inc-dec')
 require('plugins/complete-word')
 require('plugins/complete-filename')
+require('plugins/open-file-under-cursor')
+