dotfiles

personal dotfiles
git clone anongit@rnpnr.xyz:dotfiles.git
Log | Files | Refs | Feed | Submodules

goto-ref.lua (1538B)


      1 local M = {}
      2 
      3 local focus_file = function(name)
      4 	local realpath = io.popen("realpath " .. name):read("*a"):sub(1, -2)
      5 	for win in vis:windows() do
      6 		if win.file and win.file.path == realpath then
      7 			vis.win = win
      8 			return
      9 		end
     10 	end
     11 	vis:command(":o " .. realpath)
     12 end
     13 
     14 M.generate_iterators = function(file_index_table)
     15 	local current_index = 1;
     16 
     17 	local iterate = function(inc)
     18 		local file, line, col = table.unpack(file_index_table[current_index])
     19 		focus_file(file)
     20 		if type(col) == 'string' then col = tonumber(col) end
     21 		vis.win.selection:to(line, col and col or 1)
     22 		current_index = current_index + inc
     23 		if current_index > #file_index_table then
     24 			current_index = 1
     25 		end
     26 		if current_index < 1 then
     27 			current_index = #file_index_table
     28 		end
     29 	end
     30 
     31 	local forward  = function() iterate(1)  end
     32 	local backward = function() iterate(-1) end
     33 	return forward, backward
     34 end
     35 
     36 M.generate_line_indices = function(data, filter)
     37 	local ret = {}
     38 	for s in data:gmatch("[^\n]*") do
     39 		local skip = filter and filter(s)
     40 		if not skip then
     41 			local found, _, file, line, col = s:find('^([^:]+):([%d]+):?([%d]*):?')
     42 			if found then table.insert(ret, {file, line, col}) end
     43 		end
     44 	end
     45 	return ret
     46 end
     47 
     48 M.setup_iterators_from_text = function(text, filter)
     49 	if text == nil or #text == 0 then return end
     50 	local filepairs = M.generate_line_indices(text, filter)
     51 	if #filepairs then
     52 		local forward, backward = M.generate_iterators(filepairs)
     53 		vis:map(vis.modes.NORMAL, "gn", forward)
     54 		vis:map(vis.modes.NORMAL, "gp", backward)
     55 	end
     56 end
     57 
     58 return M