all repos — dotfiles @ bb585d691af4e25ded7832bbaebe335ebad9d08e

my *nix dotfiles

config/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
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
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 outfile = fn.tempname()
  local idxfile, lines = annotated_input()
  shell_cmd = {
    '/bin/sh',
    '-c',
    'fzy -p "jmp > "  < ' ..  idxfile ..  ' > ' .. outfile
  }

  winid = fn.win_getid()
  -- start a new buffer
  cmd('botright 10 new')
  cmd('startinsert')

  fn.termopen(shell_cmd, { on_exit = function()
    -- delete buffer on exit
    cmd('bd!')
    fn.win_gotoid(winid)

    -- read contents of file
    local f = io.open(outfile, 'r')
    line_choice = f:read('*all')

    -- strip '\n'
    selected, _ = string.gsub(line_choice, '\n', '')

    -- jump to line
    cmd(':' .. get_line_nr(selected))

    -- housekeeping
    f:close()
    os.remove(outfile)
    os.remove(idxfile)
  end })
end

return M