refactor(nvim): add a hacky bwdelete
function wrapping snacks bdelete
This commit is contained in:
parent
7f028d5cf9
commit
f361d5d638
@ -1,8 +1,59 @@
|
|||||||
vim.api.nvim_create_autocmd("TermOpen", {
|
--- Fancy wrapper for deleting the current buffer based on the window type, tab statuses, etc.
|
||||||
|
---
|
||||||
|
--- Kinda cursed... definitely better ways of doing this, but by god its my trash! Mfers ask "You
|
||||||
|
--- really live like this?" and the answer is YES! Yes I do.
|
||||||
|
---@param opts number|snacks.bufdelete.Opts?
|
||||||
|
local bwdelete = function(opts)
|
||||||
|
opts = opts or {}
|
||||||
|
opts.buf = opts.buf or vim.api.nvim_get_current_buf()
|
||||||
|
|
||||||
|
local nuke = false
|
||||||
|
|
||||||
|
if vim.fn.win_gettype() ~= '' and not vim.bo[opts.buf].modified then
|
||||||
|
opts.force = true
|
||||||
|
nuke = true
|
||||||
|
end
|
||||||
|
|
||||||
|
if
|
||||||
|
nuke
|
||||||
|
or (
|
||||||
|
#vim.api.nvim_list_tabpages() > 1
|
||||||
|
and #vim.api.nvim_tabpage_list_wins(vim.api.nvim_get_current_tabpage()) == 1
|
||||||
|
)
|
||||||
|
then
|
||||||
|
local cmd = opts.wipe and vim.cmd.bwipeout or vim.cmd.bdelete
|
||||||
|
cmd({ args = { opts.buf }, bang = opts.force })
|
||||||
|
else
|
||||||
|
require("snacks").bufdelete.delete(opts)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Set a mapping to quickly close the current buffer
|
||||||
|
---@param bufnr integer A buffer id
|
||||||
|
local map_quick_close = function(bufnr)
|
||||||
|
vim.iter({
|
||||||
|
"q/",
|
||||||
|
"q?",
|
||||||
|
"q:",
|
||||||
|
}):each(function(lhs)
|
||||||
|
pcall(vim.keymap.del, "n", lhs)
|
||||||
|
end)
|
||||||
|
vim.keymap.set("n", "q", function()
|
||||||
|
bwdelete({ buf = bufnr, force = true })
|
||||||
|
end, { silent = true, buffer = bufnr, desc = "Close Terminal Buffer" })
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.api.nvim_create_autocmd({ "BufEnter", "TermOpen" }, {
|
||||||
callback = function(args)
|
callback = function(args)
|
||||||
vim.keymap.set("n", "q", function()
|
if args.event == "TermOpen" then
|
||||||
require("snacks").bufdelete.delete({ force = true })
|
map_quick_close(args.buf)
|
||||||
end, { silent = true, buffer = args.buf, remap = true, desc = "Close Terminal Buffer" })
|
return
|
||||||
|
end
|
||||||
|
local bo = vim.bo[args.buf]
|
||||||
|
if vim.list_contains({ "nofile", "terminal", "nowrite", "help" }, bo.buftype) then
|
||||||
|
map_quick_close(args.buf)
|
||||||
|
return
|
||||||
|
end
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -14,9 +65,7 @@ return {
|
|||||||
keys = {
|
keys = {
|
||||||
{
|
{
|
||||||
"<A-x>",
|
"<A-x>",
|
||||||
function()
|
bwdelete,
|
||||||
require("snacks").bufdelete.delete()
|
|
||||||
end,
|
|
||||||
desc = "Close Buffer",
|
desc = "Close Buffer",
|
||||||
mode = { "", "!", "v" },
|
mode = { "", "!", "v" },
|
||||||
},
|
},
|
||||||
@ -24,7 +73,7 @@ return {
|
|||||||
{
|
{
|
||||||
"<A-x>",
|
"<A-x>",
|
||||||
function()
|
function()
|
||||||
require("snacks").bufdelete.delete({ force = true })
|
bwdelete({ force = true })
|
||||||
end,
|
end,
|
||||||
desc = "Close Buffer",
|
desc = "Close Buffer",
|
||||||
mode = { "t" },
|
mode = { "t" },
|
||||||
@ -61,7 +110,7 @@ return {
|
|||||||
})
|
})
|
||||||
snacks.config.styles["notification.history"] = {
|
snacks.config.styles["notification.history"] = {
|
||||||
title = { { "Notification History", "@markup.heading.4" } },
|
title = { { "Notification History", "@markup.heading.4" } },
|
||||||
border = { { " ", "INVALIDHIGHLIGHTHERE" } },
|
border = { " " },
|
||||||
}
|
}
|
||||||
_G.bt = snacks.debug.backtrace
|
_G.bt = snacks.debug.backtrace
|
||||||
_G.dd = snacks.debug.inspect
|
_G.dd = snacks.debug.inspect
|
||||||
|
Loading…
Reference in New Issue
Block a user