config/nvim/lua/fzy/buffers.lua (view raw)
1local fn = vim.fn
2local cmd = vim.cmd
3local M = {}
4
5function M.fzy_buffers()
6 local buffers = {}
7
8 -- list of buffers
9 local nbuf = fn.range(1, fn.bufnr('$'))
10
11 local winid = fn.win_getid()
12 -- filter out buffers that don't have 'buflisted' set
13 for _, n in ipairs(nbuf) do
14 if fn.buflisted(n) then
15 buffers[fn.bufname(n)] = n
16 end
17 end
18
19 -- write buffer names to file to feed to fzy
20 local buffile = fn.tempname()
21 local f = io.open(buffile, "a")
22 for b, _ in pairs(buffers) do
23 f:write(b, '\n')
24 end
25 f:close()
26
27 fzy_cmd = {
28 'fzy -p "buf > " ',
29 '< ' .. buffile,
30 }
31
32 require('fzy/fzy').fzy_search(table.concat(fzy_cmd), function(stdout)
33 -- strip '\n'
34 local selected, _ = stdout:gsub('\n', '')
35 cmd('bd!')
36
37 cmd('buffer ' .. buffers[ selected ])
38 -- housekeeping
39 os.remove(buffile)
40 end
41 )
42
43end
44
45return M