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] Close floating window when cursor leaves buffer #112

Open
Leandros opened this issue Oct 30, 2023 · 3 comments
Open

[FEATURE] Close floating window when cursor leaves buffer #112

Leandros opened this issue Oct 30, 2023 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@Leandros
Copy link

Leandros commented Oct 30, 2023

Is your feature request related to a problem? Please describe.
Often I'd like to take a peek at an implementation, and close the opened floating window from goto-preview when I don't have it focused anymore. It would be great if unfocusing it could close it automatically.

Describe the solution you'd like
An option in the config to close floating buffers when leaving focus.

Describe alternatives you've considered
I've tried implementing it myself using the post_open_hook, but I didn't manage to get a working solution.

post_open_hook = function(bufid, win)
  print('post_open_hook | bufid: ' .. vim.inspect(bufid) .. ' | win: ' .. vim.inspect(win))
  vim.api.nvim_create_autocmd({'WinLeave'}, {
    buffer = bufid,
    callback = function(ev)
      vim.api.nvim_win_close(win, false)
    end,
  })
end,

Thank you!

@Leandros Leandros added the enhancement New feature or request label Oct 30, 2023
@Leandros
Copy link
Author

Leandros commented Oct 30, 2023

The above version works the first time, but the second time the following errors occur:

image

@vladimir-popov
Copy link

@Leandros try to return true from your callback. It should delete autocmd.

And also, I think it's useful to mark the buffer as nomodifiable in the preview window. Together with closing the preview window on WinLeave event, it works well for me:

post_open_hook = function(buf, win)
            local orig_state = vim.api.nvim_buf_get_option(buf, 'modifiable')
            vim.api.nvim_buf_set_option(buf, 'modifiable', false)
            vim.api.nvim_create_autocmd({ 'WinLeave' }, {
                buffer = buf,
                callback = function()
                    vim.api.nvim_win_close(win, false)
                    vim.api.nvim_buf_set_option(buf, 'modifiable', orig_state)
                    return true
                end,
            })
        end

@rmagatti
Copy link
Owner

@vladimir-popov's solution is the way to go. The current ergonomics of goto-preview are exactly how I want them to be for my use-case. Hooks are provided so everyone else can implement their own flavour of behaviours. An idea would be to add community hooks to the repo that are maintained by the community that can be more easily set than copying over hook implementations all over. I'll give this some more thought

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

No branches or pull requests

3 participants