config/nvim/lua/fzy/jump.lua (view raw)
1local fn = vim.fn
2local cmd = vim.cmd
3local M = {}
4
5local function get_line_nr(s)
6 for c in s:gmatch('%d+%s') do
7 c = string.gsub(c, '%s', '')
8 return tonumber(c)
9 end
10end
11
12local function annotated_input()
13 local lines = {}
14
15 -- index each line in the current buffer
16 -- for jumping to
17 --
18 for nr = 1, fn.line('$') do
19 table.insert(lines, fn.getline(nr))
20 end
21
22 local outfile = fn.tempname()
23 local f = io.open(outfile, 'a')
24 for n, l in pairs(lines) do
25 f:write(n .. '\t' .. l, '\n')
26 end
27 f:close()
28
29 return outfile, lines
30end
31
32function M.fzy_jmp()
33 local idxfile, lines = annotated_input()
34
35 fzy_cmd = {
36 'fzy -p "jmp > " ',
37 '< ' .. idxfile,
38 }
39
40 require('fzy/fzy').fzy_search(table.concat(fzy_cmd), function(stdout)
41 -- strip '\n'
42 local selected, _ = stdout:gsub('\n', '')
43 cmd('bd!')
44
45 -- jump to line
46 cmd(':' .. get_line_nr(selected))
47 -- housekeeping
48 os.remove(idxfile)
49 end
50 )
51
52end
53return M