all repos — dotfiles @ 1168566487b5910bfd2380c76ca4067d3ada04c4

my *nix dotfiles

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  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  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  -- file to store fzy selection
28  outfile = fn.tempname()
29
30  shell_cmd = {
31    '/bin/sh', 
32    '-c',
33    'fzy -p "buf > " < ' .. buffile .. ' > ' .. outfile
34  }
35
36  -- start a new buffer
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'
50    buf_choice, _ = string.gsub(buf_choice, '\n', '')
51
52    -- housekeeping
53    f:close()
54    os.remove(outfile)
55    os.remove(buffile)
56
57    -- switch to selected buffer
58    cmd(':buffer ' .. buffers[buf_choice])
59  end })
60end
61
62return M