Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[request] Callback or event for when there's no next buffer #18

Closed
bennypowers opened this issue May 22, 2022 · 6 comments
Closed

[request] Callback or event for when there's no next buffer #18

bennypowers opened this issue May 22, 2022 · 6 comments

Comments

@bennypowers
Copy link

Please expose a config field or aucmd event to customize the behaviour when the last buffer is closed. For example, a user may want to open their dashboard plugin when closing all buffers.

Current:

README.md

If no buffer other than the target buffer was open, bufdelete.nvim creates an empty buffer and switches to it instead.

Desired

require'bufdelete'.setup {
  -- string | function
  on_close_last_buffer = ':Alpha'
}

or

vim.api.nvim_create_augroup('bufdelete', { clear = true })
vim.api.nvim_create_autocmd('User', {
  group = 'bufdelete',
  pattern = 'bufdelete_empty',
  command = ':Alpha',
})

Alternatives

This is possible without changing bufdelete.nvim:

---@author kikito
---@see https://codereview.stackexchange.com/questions/268130/get-list-of-buffers-from-current-neovim-instance
local function get_listed_buffers()
  local buffers = {}
  local len = 0
  for buffer = 1, vim.fn.bufnr('$') do
    if vim.fn.buflisted(buffer) == 1 then
      len = len + 1
      buffers[len] = buffer
    end
  end

  return buffers
end

function M.bufdelete(bufnum)
  require 'bufdelete'.bufdelete(bufnum, true)
  if #M.get_listed_buffers() == 1 and vim.api.nvim_buf_get_name(0) == '' then
    vim.cmd [[:Alpha]]
  end
end

But then the user needs to import and call that function anywhere they want this behaviour, for example in bufferline.nvim config:

bufferline.setup {
  options = {
    right_mouse_command = U.bufdelete,
    close_command = U.bufdelete,
  }
}

But this means the user has to remember each callsite. An event or callback field would allow the user to write that function once and use it automatically wherever.

@famiu
Copy link
Owner

famiu commented May 22, 2022

Allowing that sort of configuration is hard since this plugin doesn't use a setup() function nor do I plan to add one since it would be a massive breaking change to require calling a setup() function to use the plugin.

@bennypowers
Copy link
Author

bennypowers commented May 22, 2022

How about an event? something like BDeletePre would let the user open a 'default' buffer

@famiu
Copy link
Owner

famiu commented May 22, 2022

How about an event? something like BDeletePre would let the user open a 'default' buffer

Why not just use the builtin BufDelete autocmd

@bennypowers
Copy link
Author

That will also be called when renaming a buffer, which screws up alpha-nvim (dashboard plugin). Probably there's a workaround for that 🤷

This request is just to add some ergonomics to this plugin though

@famiu
Copy link
Owner

famiu commented May 22, 2022

Done, it's been added

@famiu famiu closed this as completed May 22, 2022
@bennypowers
Copy link
Author

Thank you!

an implementation: goolord/alpha-nvim#85 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants