From f361d5d638806455626ed3d7df72d7de36371f05 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Thu, 14 Nov 2024 11:17:10 -0600 Subject: [PATCH] refactor(nvim): add a hacky `bwdelete` function wrapping snacks bdelete --- .../nvim/lua/plugins/configs/snacks.lua | 67 ++++++++++++++++--- 1 file changed, 58 insertions(+), 9 deletions(-) diff --git a/users/price/dots/.config/nvim/lua/plugins/configs/snacks.lua b/users/price/dots/.config/nvim/lua/plugins/configs/snacks.lua index b3cd4885..6fb99c13 100644 --- a/users/price/dots/.config/nvim/lua/plugins/configs/snacks.lua +++ b/users/price/dots/.config/nvim/lua/plugins/configs/snacks.lua @@ -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) - vim.keymap.set("n", "q", function() - require("snacks").bufdelete.delete({ force = true }) - end, { silent = true, buffer = args.buf, remap = true, desc = "Close Terminal Buffer" }) + if args.event == "TermOpen" then + map_quick_close(args.buf) + 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, }) @@ -14,9 +65,7 @@ return { keys = { { "", - function() - require("snacks").bufdelete.delete() - end, + bwdelete, desc = "Close Buffer", mode = { "", "!", "v" }, }, @@ -24,7 +73,7 @@ return { { "", function() - require("snacks").bufdelete.delete({ force = true }) + bwdelete({ force = true }) end, desc = "Close Buffer", mode = { "t" }, @@ -61,7 +110,7 @@ return { }) snacks.config.styles["notification.history"] = { title = { { "Notification History", "@markup.heading.4" } }, - border = { { " ", "INVALIDHIGHLIGHTHERE" } }, + border = { " " }, } _G.bt = snacks.debug.backtrace _G.dd = snacks.debug.inspect