From a11c46c29ac23b367dae2b8ff2887879fb9cdbe2 Mon Sep 17 00:00:00 2001 From: kylo252 <59826753+kylo252@users.noreply.github.com> Date: Thu, 26 May 2022 13:28:04 +0200 Subject: feat: prompt when closing modified/term buffers (#2658) --- lua/lvim/core/bufferline.lua | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'lua/lvim/core/bufferline.lua') diff --git a/lua/lvim/core/bufferline.lua b/lua/lvim/core/bufferline.lua index 2df1e514..28e0f06d 100644 --- a/lua/lvim/core/bufferline.lua +++ b/lua/lvim/core/bufferline.lua @@ -142,26 +142,42 @@ M.setup = function() end end +--stylua: ignore + -- Common kill function for bdelete and bwipeout -- credits: based on bbye and nvim-bufdel ----@param kill_command string defaults to "bd" +---@param kill_command? string defaults to "bd" ---@param bufnr? number defaults to the current buffer ---@param force? boolean defaults to false function M.buf_kill(kill_command, bufnr, force) + kill_command = kill_command or "bd" + local bo = vim.bo local api = vim.api + local fmt = string.format + local fnamemodify = vim.fn.fnamemodify if bufnr == 0 or bufnr == nil then bufnr = api.nvim_get_current_buf() end - kill_command = kill_command or "bd" + local bufname = api.nvim_buf_get_name(bufnr) - -- If buffer is modified and force isn't true, print error and abort - if not force and bo[bufnr].modified then - return api.nvim_err_writeln( - string.format("No write since last change for buffer %d (set force to true to override)", bufnr) - ) + if not force then + local warning + if bo[bufnr].modified then + warning = fmt([[No write since last change for (%s)]], fnamemodify(bufname, ":t")) + elseif api.nvim_buf_get_option(bufnr, "buftype") == "terminal" then + warning = fmt([[Terminal %s will be killed]], bufname) + end + if warning then + vim.ui.input({ + prompt = string.format([[%s. Close it anyway? [y]es or [n]o (default: no): ]], warning), + }, function(choice) + if choice:match "ye?s?" then force = true end + end) + if not force then return end + end end -- Get list of windows IDs with the buffer to close @@ -169,9 +185,7 @@ function M.buf_kill(kill_command, bufnr, force) return api.nvim_win_get_buf(win) == bufnr end, api.nvim_list_wins()) - if #windows == 0 then - return - end + if #windows == 0 then return end if force then kill_command = kill_command .. "!" -- cgit v1.2.3