Commit: 4c84d673fb875d80c79d3adf37a687d40dd2aa86
Parent: 093013644afa4ddca5aa349c23b3450d2a882483
Author: Randy Palamar
Date: Tue, 28 May 2024 06:08:15 -0600
vis: add method of jumping to refrences with ag/build output etc.
Diffstat:
3 files changed, 77 insertions(+), 5 deletions(-)
diff --git a/.config/vis/build.lua b/.config/vis/build.lua
@@ -1,3 +1,4 @@
+local gf = require('goto-ref')
local util = require('util')
vis.events.subscribe(vis.events.FILE_SAVE_PRE, function(file)
@@ -54,6 +55,7 @@ local function build_files(win)
return true, info
end
+
local build_python = function (f)
local _, ostr, estr = vis:pipe('python ' .. f.name)
if estr then
@@ -68,7 +70,23 @@ local function build_files(win)
return true
end
+ local build_c = function (f)
+ local _, ostr, estr = vis:pipe('./build.sh')
+ if estr then
+ filepairs = gf.generate_line_indices(estr)
+ if #filepairs then
+ local forward, backward = gf.generate_iterators(filepairs)
+ vis:map(vis.modes.NORMAL, "gn", forward)
+ vis:map(vis.modes.NORMAL, "gp", backward)
+ end
+ util.message_clear(vis)
+ vis:message(tostring(estr))
+ end
+ return true
+ end
+
local lang = {}
+ lang["ansi_c"] = build_c
lang["latex"] = build_tex
lang["python"] = build_python
diff --git a/.config/vis/goto-ref.lua b/.config/vis/goto-ref.lua
@@ -0,0 +1,43 @@
+local M = {}
+
+local focus_file = function(name)
+ for win in vis:windows() do
+ if win.file and win.file.name == name then
+ vis.win = win
+ return
+ end
+ end
+ vis:command(":o " .. name)
+end
+
+M.generate_iterators = function(file_index_table)
+ local current_index = 1;
+
+ local iterate = function(inc)
+ local file, line = table.unpack(file_index_table[current_index])
+ focus_file(file)
+ vis.win.selection:to(line, 1)
+ current_index = current_index + inc
+ if current_index > #file_index_table then
+ current_index = 1
+ end
+ if current_index < 1 then
+ current_index = #file_index_table
+ end
+ end
+
+ local forward = function() iterate(1) end
+ local backward = function() iterate(-1) end
+ return forward, backward
+end
+
+M.generate_line_indices = function(data)
+ local ret = {}
+ for s in data:gmatch("[^\n]*") do
+ found, _, file, line = s:find('^([^:]+):([%d]+):')
+ if found then table.insert(ret, {file, line}) end
+ end
+ return #ret > 0 and ret or nil
+end
+
+return M
diff --git a/.config/vis/visrc.lua b/.config/vis/visrc.lua
@@ -6,6 +6,7 @@ require('plugins/vis-gpg')
require('plugins/vis-spellcheck')
local lint = require('plugins/vis-lint')
+local gf = require('goto-ref')
local util = require('util')
local highlight = require('highlight')
highlight.keywords = {
@@ -62,12 +63,22 @@ vis.events.subscribe(vis.events.INIT, function()
end, "dump info to message window")
end)
-vis:command_register("aq", function(argv)
+vis:command_register("ag", function(argv)
+ local filepairs = {}
for _, arg in ipairs(argv) do
- local cmd = "ag -Q " .. arg
- vis:message(cmd .. ":")
- local _, out = vis:pipe(cmd)
- vis:message(tostring(out))
+ local _, out = vis:pipe("ag -Q " .. arg)
+ if out then
+ vis:message(tostring(out))
+ newpairs = gf.generate_line_indices(out)
+ for i = 1, #newpairs do
+ table.insert(filepairs, newpairs[i])
+ end
+ end
+ end
+ if #filepairs then
+ local forward, backward = gf.generate_iterators(filepairs)
+ vis:map(vis.modes.NORMAL, "gn", forward)
+ vis:map(vis.modes.NORMAL, "gp", backward)
end
end, "Search for each literal in argv with the_silver_searcher")