From 57574a037b4faff9a3f36df12519d2ee4b479824 Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Sun, 26 Feb 2017 16:47:12 +0100 Subject: [PATCH 1/6] vis: reimplement `gf` and `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 `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, "gf", "gf") --- a/lua/vis-std.lua +++ b/lua/vis-std.lua @@ -144,3 +144,5 @@ require('plugins/digraph') require('plugins/number-inc-dec') require('plugins/complete-word') require('plugins/complete-filename') +require('plugins/open-file-under-cursor') +