all repos — dotfiles @ 4ca050a4d5afdcd0692a8f4857b24b9e2d4b0658

my *nix dotfiles

nvim/lua/fzy/jump.lua (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
local fn = vim.fn
local cmd = vim.cmd
local M = {}

local function get_line_nr(s)
  for c in s:gmatch('%d+%s') do
    c = string.gsub(c, '%s', '')
    return tonumber(c)
  end
end

local function annotated_input()
  local lines = {}

  -- index each line in the current buffer
  -- for jumping to
  --
  for nr = 1, fn.line('$') do
    table.insert(lines, fn.getline(nr))
  end

  local outfile = fn.tempname()
  local f = io.open(outfile, 'a')
  for n, l in pairs(lines) do
    f:write(n .. '\t' .. l, '\n')
  end
  f:close()

  return outfile, lines
end

function M.fzy_jmp()
  local idxfile, lines = annotated_input()

  fzy_cmd = {
    'fzy -p "jmp > " ',
    '< ' .. idxfile,
  }

  require('fzy/fzy').fzy_search(table.concat(fzy_cmd), function(stdout)
    -- strip '\n'
    local selected, _ = stdout:gsub('\n', '')
    cmd('bd!')

    -- jump to line
    cmd(':' .. get_line_nr(selected))
    -- housekeeping
    os.remove(idxfile)
  end
  )

end
return M