diff options
| -rw-r--r-- | lua/lvim/core/bufferline.lua | 62 | ||||
| -rw-r--r-- | lua/lvim/core/commands.lua | 1 | ||||
| -rw-r--r-- | lua/lvim/core/which-key.lua | 2 | 
3 files changed, 64 insertions, 1 deletions
| diff --git a/lua/lvim/core/bufferline.lua b/lua/lvim/core/bufferline.lua index 06de3ac4..c7b5fd2d 100644 --- a/lua/lvim/core/bufferline.lua +++ b/lua/lvim/core/bufferline.lua @@ -139,4 +139,66 @@ M.setup = function()    end  end +-- Common kill function for bdelete and bwipeout +-- credits: based on bbye and nvim-bufdel +---@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) +  local bo = vim.bo +  local api = vim.api + +  if bufnr == 0 or bufnr == nil then +    bufnr = api.nvim_get_current_buf() +  end + +  kill_command = kill_command or "bd" + +  -- 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) +    ) +  end + +  -- Get list of windows IDs with the buffer to close +  local windows = vim.tbl_filter(function(win) +    return api.nvim_win_get_buf(win) == bufnr +  end, api.nvim_list_wins()) + +  if #windows == 0 then +    return +  end + +  if force then +    kill_command = kill_command .. "!" +  end + +  -- Get list of active buffers +  local buffers = vim.tbl_filter(function(buf) +    return api.nvim_buf_is_valid(buf) and bo[buf].buflisted +  end, api.nvim_list_bufs()) + +  -- If there is only one buffer (which has to be the current one), vim will +  -- create a new buffer on :bd. +  -- For more than one buffer, pick the previous buffer (wrapping around if necessary) +  if #buffers > 1 then +    for i, v in ipairs(buffers) do +      if v == bufnr then +        local prev_buf_idx = i == 1 and (#buffers - 1) or (i - 1) +        local prev_buffer = buffers[prev_buf_idx] +        for _, win in ipairs(windows) do +          api.nvim_win_set_buf(win, prev_buffer) +        end +      end +    end +  end + +  -- Check if buffer still exists, to ensure the target buffer wasn't killed +  -- due to options like bufhidden=wipe. +  if api.nvim_buf_is_valid(bufnr) and bo[bufnr].buflisted then +    vim.cmd(string.format("%s %d", kill_command, bufnr)) +  end +end +  return M diff --git a/lua/lvim/core/commands.lua b/lua/lvim/core/commands.lua index 6997795d..2d23167f 100644 --- a/lua/lvim/core/commands.lua +++ b/lua/lvim/core/commands.lua @@ -10,6 +10,7 @@ M.defaults = {      endif    endfunction    ]], +  [[ command! BufferKill lua require('user.bufferline').buf_kill('bd') ]],    -- :LvimInfo    [[ command! LvimInfo lua require('lvim.core.info').toggle_popup(vim.bo.filetype) ]],    [[ command! LvimCacheReset lua require('lvim.utils.hooks').reset_cache() ]], diff --git a/lua/lvim/core/which-key.lua b/lua/lvim/core/which-key.lua index 194cb314..75169567 100644 --- a/lua/lvim/core/which-key.lua +++ b/lua/lvim/core/which-key.lua @@ -67,7 +67,7 @@ M.config = function()        ["w"] = { "<cmd>w!<CR>", "Save" },        ["q"] = { "<cmd>q!<CR>", "Quit" },        ["/"] = { "<cmd>lua require('Comment.api').toggle_current_linewise()<CR>", "Comment" }, -      ["c"] = { "<cmd>bdelete!<CR>", "Close Buffer" }, +      ["c"] = { "<cmd>BufferKill<CR>", "Close Buffer" },        ["f"] = { require("lvim.core.telescope.custom-finders").find_project_files, "Find File" },        ["h"] = { "<cmd>nohlsearch<CR>", "No Highlight" },        b = { | 
