stevearc / stickybuf.nvim

Neovim plugin for locking a buffer to a window
MIT License
246 stars 7 forks source link

[feature] ability to prevent opening in a new window #21

Closed emmanueltouzery closed 1 year ago

emmanueltouzery commented 1 year ago

I was trying to set up stickybuf in my config for diffview https://github.com/sindrets/diffview.nvim

Diffview takes over a whole tab, and it wouldn't make sense to replace any buffer in any of its windows.

This is nice:

    require("stickybuf").setup({
       get_auto_pin = function(bufnr)
         -- pin if any of the windows of the current tab have a DiffviewFiles filetype
         -- (that's the filetype of the diffview sidebar, no switching buffers in that tab)
         for _, win in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
           local buf = vim.api.nvim_win_get_buf(win)
           local buf_ft = vim.api.nvim_buf_get_option(buf, "ft")
           if buf_ft == "DiffviewFiles" then
             return true
           end
         end
         return require("stickybuf").should_auto_pin(bufnr)
       end
     })

However I then hit this code from stickybuf:

  -- If none exists, open the buffer in a vsplit from the first window
  vim.fn.win_execute(vim.fn.win_getid(1), string.format("vertical rightbelow sbuffer %d", bufnr))
  vim.cmd.wincmd({ count = 2, args = { "w" } })

and a new window is opened in the diffview tab.

I would ideally like a way to dynamically say "in case X and Y, if no window is available, don't open a split window". I guess it would be possible to have that as a global setting (never open new windows), but maybe it'll be needed to make a dynamic decision for that. Maybe get_auto_pin could return instead of true/false, "pin_with_fallback" or "pin_without_fallback", or maybe it could be possible to pass a new function in the options, something like open_buffer_in_newsplit = function(bufnr) ... end...

stevearc commented 1 year ago

You can now pass handle_foreign_buffer = function() end to stickybuf.pin and that will do what you want. I also added the ability for get_auto_pin to return a table, which will then be passed in as options to stickybuf.pin. So you can return the table with that value from get_auto_pin

emmanueltouzery commented 1 year ago

works great, thank you!!