all repos — dotfiles @ 5ea2a035b94666898f92727e65f21a527baf1cc5

my *nix dotfiles

nvim: Implement in-buffer jumping
Anirudh Oppiliappan x@icyphox.sh
Sat, 06 Feb 2021 14:05:52 +0530
commit

5ea2a035b94666898f92727e65f21a527baf1cc5

parent

a80f1ed0a43626deaa73f789ed683816f2404579

M config/nvim/lua/fzy/buffers.luaconfig/nvim/lua/fzy/buffers.lua

@@ -30,7 +30,7 @@

shell_cmd = { '/bin/sh', '-c', - 'fzy < ' .. buffile .. ' > ' .. outfile + 'fzy -p "buf > " < ' .. buffile .. ' > ' .. outfile } -- start a new buffer
A config/nvim/lua/fzy/jump.lua

@@ -0,0 +1,61 @@

+local fn = vim.fn +local cmd = vim.cmd +local M = {} + + +local function build_index() + local lines = {} + + -- index each line in the current buffer + -- for jumping to + for line = 1, fn.line('$') do + lines[fn.getline(line)] = line + end + + local outfile = fn.tempname() + local f = io.open(outfile, 'a') + for l, _ in pairs(lines) do + f:write(l, '\n') + end + f:close() + + return outfile, lines +end + +function M.fzy_jmp() + local outfile = fn.tempname() + local idxfile, lines = build_index() + 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' + line_choice, _ = string.gsub(line_choice, '\n', '') + + -- jump to line + cmd(':' .. lines[line_choice]) + + -- housekeeping + f:close() + os.remove(outfile) + os.remove(idxfile) + end }) +end + +return M
M config/nvim/lua/fzy/shell.luaconfig/nvim/lua/fzy/shell.lua

@@ -8,7 +8,7 @@ file = fn.tempname()

shell_cmd = { '/bin/sh', '-c', - fzy_cmd .. ' | fzy > ' .. file + fzy_cmd .. ' | fzy -p "edit > " > ' .. file } -- get current winid to jump back to
M config/nvim/lua/maps.luaconfig/nvim/lua/maps.lua

@@ -45,7 +45,15 @@ _G.fzy_buffers = require('fzy.buffers').fzy_buffers

map('', '<leader>b', ':call v:lua.fzy_buffers()<cr>', - { noremap=true, silent=true }) + { noremap=true, silent=true } + ) + + _G.fzy_jmp = require('fzy.jump').fzy_jmp + map('', + '<leader>f', + ':call v:lua.fzy_jmp()<cr>', + { noremap=true, silent=true} + ) else print('fzy not in PATH!') end