diff options
| author | kylo252 <[email protected]> | 2022-05-26 13:28:04 +0200 | 
|---|---|---|
| committer | GitHub <[email protected]> | 2022-05-26 13:28:04 +0200 | 
| commit | a11c46c29ac23b367dae2b8ff2887879fb9cdbe2 (patch) | |
| tree | fda103b02bcab51260f4bd3632c5950ebf791f3c /lua/lvim/core/bufferline.lua | |
| parent | 6dbba1f9597e0ba090702aea41357c29874aab7c (diff) | |
feat: prompt when closing modified/term buffers (#2658)
Diffstat (limited to 'lua/lvim/core/bufferline.lua')
| -rw-r--r-- | lua/lvim/core/bufferline.lua | 34 | 
1 files changed, 24 insertions, 10 deletions
| 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 .. "!" | 
