2022-01-10 09:55:15 -06:00
|
|
|
local U = {}
|
|
|
|
U.map = function(mode, lhs, rhs, opts)
|
2022-05-11 21:00:57 -05:00
|
|
|
local options = { noremap = true, silent = true }
|
|
|
|
if opts then
|
|
|
|
options = vim.tbl_extend('force', options, opts)
|
|
|
|
end
|
|
|
|
vim.api.nvim_set_keymap(mode, lhs, rhs, options)
|
2022-01-10 09:55:15 -06:00
|
|
|
end
|
|
|
|
|
2022-05-17 15:49:48 -05:00
|
|
|
U.buffer_map = function (mode, lhs, rhs, opts)
|
|
|
|
local options = { noremap = true, silent = true }
|
|
|
|
if opts then
|
|
|
|
options = vim.tbl_extend('force', options, opts)
|
|
|
|
end
|
2022-05-17 15:51:40 -05:00
|
|
|
vim.api.nvim_buf_set_keymap(0, mode, lhs, rhs, options)
|
2022-05-17 15:49:48 -05:00
|
|
|
end
|
|
|
|
|
2022-01-10 09:55:15 -06:00
|
|
|
U.close_buffer = function(force)
|
2022-05-11 21:00:57 -05:00
|
|
|
-- Yanked from NvChad, thanks to them. This saves
|
|
|
|
-- a lot of pain
|
2022-01-10 09:55:15 -06:00
|
|
|
|
2022-05-11 21:00:57 -05:00
|
|
|
-- This is a modification of a NeoVim plugin from
|
|
|
|
-- Author: ojroques - Olivier Roques
|
|
|
|
-- Src: https://github.com/ojroques/nvim-bufdel
|
2022-01-10 09:55:15 -06:00
|
|
|
|
2022-05-11 21:00:57 -05:00
|
|
|
-- Options
|
|
|
|
local opts = {
|
|
|
|
next = 'cycle', -- how to retrieve the next buffer
|
|
|
|
quit = false, -- exit when last buffer is deleted
|
|
|
|
--TODO make this a chadrc flag/option
|
|
|
|
}
|
2022-01-10 09:55:15 -06:00
|
|
|
|
2022-05-11 21:00:57 -05:00
|
|
|
-- ----------------
|
|
|
|
-- Helper functions
|
|
|
|
-- ----------------
|
2022-01-10 09:55:15 -06:00
|
|
|
|
2022-05-11 21:00:57 -05:00
|
|
|
-- Switch to buffer 'buf' on each window from list 'windows'
|
|
|
|
local function switch_buffer(windows, buf)
|
|
|
|
local cur_win = vim.fn.winnr()
|
|
|
|
for _, winid in ipairs(windows) do
|
|
|
|
vim.cmd(string.format('%d wincmd w', vim.fn.win_id2win(winid)))
|
|
|
|
vim.cmd(string.format('buffer %d', buf))
|
|
|
|
end
|
|
|
|
vim.cmd(string.format('%d wincmd w', cur_win)) -- return to original window
|
|
|
|
end
|
2022-01-10 09:55:15 -06:00
|
|
|
|
2022-05-11 21:00:57 -05:00
|
|
|
-- Select the first buffer with a number greater than given buffer
|
|
|
|
local function get_next_buf(buf)
|
|
|
|
local next = vim.fn.bufnr('#')
|
|
|
|
if opts.next == 'alternate' and vim.fn.buflisted(next) == 1 then
|
|
|
|
return next
|
|
|
|
end
|
|
|
|
for i = 0, vim.fn.bufnr('$') - 1 do
|
|
|
|
next = (buf + i) % vim.fn.bufnr('$') + 1 -- will loop back to 1
|
|
|
|
if vim.fn.buflisted(next) == 1 then
|
|
|
|
return next
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-01-10 09:55:15 -06:00
|
|
|
|
2022-05-11 21:00:57 -05:00
|
|
|
-- ----------------
|
|
|
|
-- End helper functions
|
|
|
|
-- ----------------
|
2022-01-10 09:55:15 -06:00
|
|
|
|
2022-05-11 21:00:57 -05:00
|
|
|
local buf = vim.fn.bufnr()
|
|
|
|
if vim.fn.buflisted(buf) == 0 then -- exit if buffer number is invalid
|
|
|
|
vim.cmd('close')
|
|
|
|
return
|
|
|
|
end
|
2022-01-10 09:55:15 -06:00
|
|
|
|
2022-05-11 21:00:57 -05:00
|
|
|
if #vim.fn.getbufinfo({ buflisted = 1 }) < 2 then
|
|
|
|
if opts.quit then
|
|
|
|
-- exit when there is only one buffer left
|
|
|
|
if force then
|
|
|
|
vim.cmd('qall!')
|
|
|
|
else
|
|
|
|
vim.cmd('confirm qall')
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
2022-01-10 09:55:15 -06:00
|
|
|
|
2022-05-11 21:00:57 -05:00
|
|
|
local chad_term, _ = pcall(function()
|
|
|
|
return vim.api.nvim_buf_get_var(buf, 'term_type')
|
|
|
|
end)
|
2022-01-10 09:55:15 -06:00
|
|
|
|
2022-05-11 21:00:57 -05:00
|
|
|
if chad_term then
|
|
|
|
-- Must be a window type
|
|
|
|
vim.cmd(string.format('setlocal nobl', buf))
|
|
|
|
vim.cmd('enew')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
-- don't exit and create a new empty buffer
|
|
|
|
vim.cmd('enew')
|
|
|
|
vim.cmd('bp')
|
|
|
|
end
|
2022-01-10 09:55:15 -06:00
|
|
|
|
2022-05-11 21:00:57 -05:00
|
|
|
local next_buf = get_next_buf(buf)
|
|
|
|
local windows = vim.fn.getbufinfo(buf)[1].windows
|
2022-01-10 09:55:15 -06:00
|
|
|
|
2022-05-11 21:00:57 -05:00
|
|
|
-- force deletion of terminal buffers to avoid the prompt
|
|
|
|
if force or vim.fn.getbufvar(buf, '&buftype') == 'terminal' then
|
|
|
|
local chad_term, type = pcall(function()
|
|
|
|
return vim.api.nvim_buf_get_var(buf, 'term_type')
|
|
|
|
end)
|
2022-01-10 09:55:15 -06:00
|
|
|
|
2022-05-11 21:00:57 -05:00
|
|
|
-- TODO this scope is error prone, make resilient
|
|
|
|
if chad_term then
|
|
|
|
if type == 'wind' then
|
|
|
|
-- hide from bufferline
|
|
|
|
vim.cmd(string.format('%d bufdo setlocal nobl', buf))
|
|
|
|
-- swtich to another buff
|
|
|
|
-- TODO switch to next bufffer, this works too
|
|
|
|
vim.cmd('BufferLineCycleNext')
|
|
|
|
else
|
|
|
|
local cur_win = vim.fn.winnr()
|
|
|
|
-- we can close this window
|
|
|
|
vim.cmd(string.format('%d wincmd c', cur_win))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
else
|
|
|
|
switch_buffer(windows, next_buf)
|
|
|
|
vim.cmd(string.format('bd! %d', buf))
|
|
|
|
end
|
|
|
|
else
|
|
|
|
switch_buffer(windows, next_buf)
|
|
|
|
vim.cmd(string.format('silent! confirm bd %d', buf))
|
|
|
|
end
|
|
|
|
-- revert buffer switches if user has canceled deletion
|
|
|
|
if vim.fn.buflisted(buf) == 1 then
|
|
|
|
switch_buffer(windows, buf)
|
|
|
|
end
|
2022-01-10 09:55:15 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
return U
|