all repos — dotfiles @ 4957a235fb16f0929806a5a9978f3d7029a1ce5d

my *nix dotfiles

nvim: rewrite edit and buffers fzy
Anirudh Oppiliappan x@icyphox.sh
Sat, 15 Jan 2022 15:15:24 +0530
commit

4957a235fb16f0929806a5a9978f3d7029a1ce5d

parent

d9063a1e2aaeab2e231b1e12ccb8f30413743923

4 files changed, 34 insertions(+), 85 deletions(-)

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

@@ -8,55 +8,38 @@

-- list of buffers local nbuf = fn.range(1, fn.bufnr('$')) - winid = fn.win_getid() + local winid = fn.win_getid() -- filter out buffers that don't have 'buflisted' set for _, n in ipairs(nbuf) do if fn.buflisted(n) then buffers[fn.bufname(n)] = n end end - + -- write buffer names to file to feed to fzy - buffile = fn.tempname() + local buffile = fn.tempname() local f = io.open(buffile, "a") for b, _ in pairs(buffers) do f:write(b, '\n') end f:close() - -- file to store fzy selection - outfile = fn.tempname() - - shell_cmd = { - '/bin/sh', - '-c', - 'fzy -p "buf > " < ' .. buffile .. ' > ' .. outfile + fzy_cmd = { + 'fzy -p "buf > " ', + '< ' .. buffile, } - -- start a new buffer - cmd('botright 10 new') - cmd('startinsert') - - fn.termopen(shell_cmd, { on_exit = function() - -- delete buffer on exit + require('fzy/fzy').fzy_search(table.concat(fzy_cmd), function(stdout) + -- strip '\n' + local selected, _ = stdout:gsub('\n', '') cmd('bd!') - fn.win_gotoid(winid) - -- read contents of file - local f = io.open(outfile, 'r') - buf_choice = f:read('*all') - - -- strip '\n' - buf_choice, _ = string.gsub(buf_choice, '\n', '') - + cmd('buffer ' .. buffers[ selected ]) -- housekeeping - f:close() - os.remove(outfile) os.remove(buffile) + end + ) - -- switch to selected buffer - cmd(':buffer ' .. buffers[buf_choice]) - end }) end return M
A config/nvim/lua/fzy/edit.lua

@@ -0,0 +1,20 @@

+local fn = vim.fn +local cmd = vim.cmd +local M = {} + +function M.fzy_edit(ls_cmd) + fzy_cmd = { + ls_cmd, + ' | fzy -p "edit > "', + } + + require('fzy/fzy').fzy_search(table.concat(fzy_cmd), function(stdout) + -- strip '\n' + local selected, _ = stdout:gsub('\n', '') + cmd('bd!') + cmd('e ' .. selected) + end + ) +end + +return M
D config/nvim/lua/fzy/shell.lua

@@ -1,42 +0,0 @@

-local fn = vim.fn -local cmd = vim.cmd -local M = {} - -function M.fzy_shell_cmd(fzy_cmd, action) - -- save shell output to a temp file - file = fn.tempname() - shell_cmd = { - '/bin/sh', - '-c', - fzy_cmd .. ' | fzy -p "edit > " > ' .. file - } - - -- get current winid to jump back to - winid = fn.win_getid() - - -- start a new buffer - cmd('botright 10 new') - cmd('startinsert') - - -- open a term with the fzy command, - -- and run callback on exit - 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(file, 'r') - fzy_out = f:read('*all') - - -- housekeeping - f:close() - os.remove(file) - - -- run action against output - -- ex: ':e somefile' - vim.cmd(table.concat({ action, fzy_out }, ' ')) - end }) -end - -return M
M config/nvim/lua/maps.luaconfig/nvim/lua/maps.lua

@@ -20,25 +20,13 @@ cmd(':command! Wqa wqa')

cmd(':command! W w') cmd(':command! Q q') -local function fzy_ignore(patterns) - pattern_cmd = {} - for _, p in ipairs(patterns) do - table.insert(pattern_cmd, string.format("! -path '%s'", p)) - end - - return table.concat(pattern_cmd, ' ') -end - -- fzy mappings if vim.fn.executable('fzy') then - _G.fzy_shell_cmd = require('fzy.shell').fzy_shell_cmd + _G.fzy_edit = require('fzy.edit').fzy_edit map( '', '<leader>e', - string.format( - ':call v:lua.fzy_shell_cmd("find -L . -type f %s", ":e")<cr>', - fzy_ignore{'*.git/*', '*node_modules*', '*.pyc', '*migrations*'} - ), + ':call v:lua.fzy_edit("git ls-files")<cr>', { noremap=true, silent=true } )