aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnirudh Oppiliappan <x@icyphox.sh>2022-01-15 15:15:24 +0530
committerAnirudh Oppiliappan <x@icyphox.sh>2022-01-15 15:15:24 +0530
commit4957a235fb16f0929806a5a9978f3d7029a1ce5d (patch)
tree7a44287bd356bae87143ca6d2590c8b6bcf2f0ce
parentd9063a1e2aaeab2e231b1e12ccb8f30413743923 (diff)
downloaddotfiles-4957a235fb16f0929806a5a9978f3d7029a1ce5d.tar.gz
nvim: rewrite edit and buffers fzy
-rw-r--r--config/nvim/lua/fzy/buffers.lua41
-rw-r--r--config/nvim/lua/fzy/edit.lua20
-rw-r--r--config/nvim/lua/fzy/shell.lua42
-rw-r--r--config/nvim/lua/maps.lua16
4 files changed, 34 insertions, 85 deletions
diff --git a/config/nvim/lua/fzy/buffers.lua b/config/nvim/lua/fzy/buffers.lua
index 79919d9..fc919cc 100644
--- a/config/nvim/lua/fzy/buffers.lua
+++ b/config/nvim/lua/fzy/buffers.lua
@@ -8,55 +8,38 @@ function M.fzy_buffers()
8 -- list of buffers 8 -- list of buffers
9 local nbuf = fn.range(1, fn.bufnr('$')) 9 local nbuf = fn.range(1, fn.bufnr('$'))
10 10
11 winid = fn.win_getid() 11 local winid = fn.win_getid()
12 -- filter out buffers that don't have 'buflisted' set 12 -- filter out buffers that don't have 'buflisted' set
13 for _, n in ipairs(nbuf) do 13 for _, n in ipairs(nbuf) do
14 if fn.buflisted(n) then 14 if fn.buflisted(n) then
15 buffers[fn.bufname(n)] = n 15 buffers[fn.bufname(n)] = n
16 end 16 end
17 end 17 end
18 18
19 -- write buffer names to file to feed to fzy 19 -- write buffer names to file to feed to fzy
20 buffile = fn.tempname() 20 local buffile = fn.tempname()
21 local f = io.open(buffile, "a") 21 local f = io.open(buffile, "a")
22 for b, _ in pairs(buffers) do 22 for b, _ in pairs(buffers) do
23 f:write(b, '\n') 23 f:write(b, '\n')
24 end 24 end
25 f:close() 25 f:close()
26 26
27 -- file to store fzy selection 27 fzy_cmd = {
28 outfile = fn.tempname() 28 'fzy -p "buf > " ',
29 29 '< ' .. buffile,
30 shell_cmd = {
31 '/bin/sh',
32 '-c',
33 'fzy -p "buf > " < ' .. buffile .. ' > ' .. outfile
34 } 30 }
35 31
36 -- start a new buffer 32 require('fzy/fzy').fzy_search(table.concat(fzy_cmd), function(stdout)
37 cmd('botright 10 new')
38 cmd('startinsert')
39
40 fn.termopen(shell_cmd, { on_exit = function()
41 -- delete buffer on exit
42 cmd('bd!')
43 fn.win_gotoid(winid)
44
45 -- read contents of file
46 local f = io.open(outfile, 'r')
47 buf_choice = f:read('*all')
48
49 -- strip '\n' 33 -- strip '\n'
50 buf_choice, _ = string.gsub(buf_choice, '\n', '') 34 local selected, _ = stdout:gsub('\n', '')
35 cmd('bd!')
51 36
37 cmd('buffer ' .. buffers[ selected ])
52 -- housekeeping 38 -- housekeeping
53 f:close()
54 os.remove(outfile)
55 os.remove(buffile) 39 os.remove(buffile)
40 end
41 )
56 42
57 -- switch to selected buffer
58 cmd(':buffer ' .. buffers[buf_choice])
59 end })
60end 43end
61 44
62return M 45return M
diff --git a/config/nvim/lua/fzy/edit.lua b/config/nvim/lua/fzy/edit.lua
new file mode 100644
index 0000000..d4e3c00
--- /dev/null
+++ b/config/nvim/lua/fzy/edit.lua
@@ -0,0 +1,20 @@
1local fn = vim.fn
2local cmd = vim.cmd
3local M = {}
4
5function M.fzy_edit(ls_cmd)
6 fzy_cmd = {
7 ls_cmd,
8 ' | fzy -p "edit > "',
9 }
10
11 require('fzy/fzy').fzy_search(table.concat(fzy_cmd), function(stdout)
12 -- strip '\n'
13 local selected, _ = stdout:gsub('\n', '')
14 cmd('bd!')
15 cmd('e ' .. selected)
16 end
17 )
18end
19
20return M
diff --git a/config/nvim/lua/fzy/shell.lua b/config/nvim/lua/fzy/shell.lua
deleted file mode 100644
index 4baea5e..0000000
--- a/config/nvim/lua/fzy/shell.lua
+++ /dev/null
@@ -1,42 +0,0 @@
1local fn = vim.fn
2local cmd = vim.cmd
3local M = {}
4
5function M.fzy_shell_cmd(fzy_cmd, action)
6 -- save shell output to a temp file
7 file = fn.tempname()
8 shell_cmd = {
9 '/bin/sh',
10 '-c',
11 fzy_cmd .. ' | fzy -p "edit > " > ' .. file
12 }
13
14 -- get current winid to jump back to
15 winid = fn.win_getid()
16
17 -- start a new buffer
18 cmd('botright 10 new')
19 cmd('startinsert')
20
21 -- open a term with the fzy command,
22 -- and run callback on exit
23 fn.termopen(shell_cmd, { on_exit = function()
24 -- delete buffer on exit
25 cmd('bd!')
26 fn.win_gotoid(winid)
27
28 -- read contents of file
29 local f = io.open(file, 'r')
30 fzy_out = f:read('*all')
31
32 -- housekeeping
33 f:close()
34 os.remove(file)
35
36 -- run action against output
37 -- ex: ':e somefile'
38 vim.cmd(table.concat({ action, fzy_out }, ' '))
39 end })
40end
41
42return M
diff --git a/config/nvim/lua/maps.lua b/config/nvim/lua/maps.lua
index 7a0be42..2f641be 100644
--- a/config/nvim/lua/maps.lua
+++ b/config/nvim/lua/maps.lua
@@ -20,25 +20,13 @@ cmd(':command! Wqa wqa')
20cmd(':command! W w') 20cmd(':command! W w')
21cmd(':command! Q q') 21cmd(':command! Q q')
22 22
23local function fzy_ignore(patterns)
24 pattern_cmd = {}
25 for _, p in ipairs(patterns) do
26 table.insert(pattern_cmd, string.format("! -path '%s'", p))
27 end
28
29 return table.concat(pattern_cmd, ' ')
30end
31
32-- fzy mappings 23-- fzy mappings
33if vim.fn.executable('fzy') then 24if vim.fn.executable('fzy') then
34 _G.fzy_shell_cmd = require('fzy.shell').fzy_shell_cmd 25 _G.fzy_edit = require('fzy.edit').fzy_edit
35 map( 26 map(
36 '', 27 '',
37 '<leader>e', 28 '<leader>e',
38 string.format( 29 ':call v:lua.fzy_edit("git ls-files")<cr>',
39 ':call v:lua.fzy_shell_cmd("find -L . -type f %s", ":e")<cr>',
40 fzy_ignore{'*.git/*', '*node_modules*', '*.pyc', '*migrations*'}
41 ),
42 { noremap=true, silent=true } 30 { noremap=true, silent=true }
43 ) 31 )
44 32