mirror of
https://github.com/pnx/dotfiles
synced 2026-06-16 03:14:55 +02:00
nvim/lua/utils/buffers.lua: Adding M.Close() that uses mini.bufremove if it exists.
This commit is contained in:
parent
f58a850456
commit
dbd4ae759c
1 changed files with 26 additions and 2 deletions
|
|
@ -1,11 +1,35 @@
|
|||
-- Helper functions for dealing with buffers
|
||||
local M = {}
|
||||
|
||||
function M.Close(buf)
|
||||
-- check if mini is installed.
|
||||
local hasmini, mini = pcall(require,"mini.bufremove")
|
||||
if hasmini then
|
||||
local bd = mini.delete
|
||||
if vim.bo[buf].modified then
|
||||
local bufname = vim.fn.bufname(buf)
|
||||
local choice = vim.fn.confirm(("Save changes to %q?"):format(bufname), "&Yes\n&No\n&Cancel")
|
||||
if choice == 1 then -- Yes
|
||||
vim.cmd(("%i,%ibufdo w"):format(buf, buf))
|
||||
bd(buf)
|
||||
elseif choice == 2 then -- No
|
||||
bd(buf, true)
|
||||
end
|
||||
else
|
||||
bd(buf)
|
||||
end
|
||||
-- fallback to native nvim api
|
||||
else
|
||||
vim.api.nvim_buf_delete(buf, {})
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-- Close all but current buffer
|
||||
function M.CloseOthers()
|
||||
for _, i in ipairs(vim.api.nvim_list_bufs()) do
|
||||
if i ~= vim.api.nvim_get_current_buf() then
|
||||
vim.api.nvim_buf_delete(i, {})
|
||||
M.Close(i)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -13,7 +37,7 @@ end
|
|||
-- Close all open buffers
|
||||
function M.CloseAll()
|
||||
for _, i in ipairs(vim.api.nvim_list_bufs()) do
|
||||
vim.api.nvim_buf_delete(i, {})
|
||||
M.Close(i)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue