all repos — dotfiles @ ca626894ecd234fe4a759ac8ffbe05becbed1c00

my *nix dotfiles

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
12
13local function annotated_input()
14  local lines = {}
15
16  -- index each line in the current buffer
17  -- for jumping to
18  --
19  for nr = 1, fn.line('$') do
20    table.insert(lines, fn.getline(nr))
21  end
22
23
24  local outfile = fn.tempname()
25  local f = io.open(outfile, 'a')
26  for n, l in pairs(lines) do
27    f:write(n .. '\t' .. l, '\n')
28  end
29  f:close()
30
31  return outfile, lines
32end
33
34function M.fzy_jmp()
35  local outfile = fn.tempname()
36  local idxfile, lines = annotated_input()
37  shell_cmd = {
38    '/bin/sh',
39    '-c',
40    'fzy -p "jmp > "  < ' ..  idxfile ..  ' > ' .. outfile
41  }
42
43  winid = fn.win_getid()
44  -- start a new buffer
45  cmd('botright 10 new')
46  cmd('startinsert')
47
48  fn.termopen(shell_cmd, { on_exit = function()
49    -- delete buffer on exit
50    cmd('bd!')
51    fn.win_gotoid(winid)
52
53    -- read contents of file
54    local f = io.open(outfile, 'r')
55    line_choice = f:read('*all')
56
57    -- strip '\n'
58    selected, _ = string.gsub(line_choice, '\n', '')
59
60    -- jump to line
61    cmd(':' .. get_line_nr(selected))
62
63    -- housekeeping
64    f:close()
65    os.remove(outfile)
66    os.remove(idxfile)
67  end })
68end
69
70return M