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

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

Closed
emmanueltouzery opened this issue Oct 5, 2023 · 2 comments
Closed

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

emmanueltouzery opened this issue Oct 5, 2023 · 2 comments

Comments

@emmanueltouzery
Copy link
Contributor

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
Copy link
Owner

stevearc commented Oct 7, 2023

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

@stevearc stevearc closed this as completed Oct 7, 2023
@emmanueltouzery
Copy link
Contributor Author

works great, thank you!!

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