-
Notifications
You must be signed in to change notification settings - Fork 3
/
buffers.lua
37 lines (30 loc) · 1.13 KB
/
buffers.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
local buffers = {}
local stickybuf_util = require('stickybuf.util')
function buffers.close_current_buffer(buffer, force)
buffer = tonumber(buffer) -- Can be passed as string from command
if buffer == 0 or not buffer then
buffer = vim.api.nvim_get_current_buf()
end
if vim.api.nvim_buf_get_option(buffer, 'filetype') == 'NvimTree' then
require('nvim-tree').close()
return
end
if vim.endswith(vim.fn.bufname(), 'NEOGIT_COMMIT_EDITMSG') then
vim.cmd('close') -- To avoid conflicts with stickybuf.nvim, read more here: https://github.com/stevearc/stickybuf.nvim/issues/1#issuecomment-880107698
return
end
if stickybuf_util.is_sticky_win() then
vim.api.nvim_buf_delete(buffer, { force = force or vim.bo.buftype == 'terminal' })
return
end
if #vim.fn.getbufinfo({ buflisted = 1 }) == 1 then
-- Only one window left, create a new empty window
vim.cmd('enew')
vim.bo.bufhidden = 'wipe'
elseif vim.api.nvim_win_get_buf(0) == buffer then
-- Preserve layout only if this is a current buffer
vim.cmd('bprevious')
end
vim.cmd('bdelete ' .. (force and '! ' or ' ') .. buffer)
end
return buffers