popup.nvim icon indicating copy to clipboard operation
popup.nvim copied to clipboard

Give plugin authors an "on_close" callback to use when we close down their buffer

Open tjdevries opened this issue 4 years ago • 12 comments

This would prevent a lot of pain that I have experienced recently.

tjdevries avatar Jul 22 '20 03:07 tjdevries

I'd like to work on this

anihm136 avatar Oct 01 '20 02:10 anihm136

How would I be notified of a specific window closing?

anihm136 avatar Oct 01 '20 05:10 anihm136

Hey!

I think the best way would probably add an autocmd on/before window close for the window that calls a lua function that was registered at startup.

tjdevries avatar Oct 06 '20 17:10 tjdevries

Ah, didn't think of that. Few questions about the implementation -

  1. Do I just add the autocommand using nvim_command?
  2. How would the callback be passed to the function? New parameter? Within vim_options?
  3. If we need to pass arguments to the callback, how would they be passed?

anihm136 avatar Oct 07 '20 02:10 anihm136

Additionally, should the callback be a vimscript function or a lua function?

anihm136 avatar Oct 07 '20 02:10 anihm136

Lua functions and vim script functions are interchangeable, so that parts fine :)

Yes, autocommands are added with nvim_command.

I think on_close is a parameter for the vim style of popup_* arguments. So you should check the docs for vim. The idea is that popup.nvim should resemble vim's implementation.

If that doesn't make sense, I can explain more later :smile:

tjdevries avatar Oct 07 '20 03:10 tjdevries

It does make sense, thanks. I read the vim popup api docs, and one of the options is a callback as you said, so that's fine

anihm136 avatar Oct 07 '20 05:10 anihm136

From the vim popup api docs, it seems that the callback works this way -

  • It is used only when a filter is also specified
  • It is called with the ID of the popup window and the output of the filter, only when the popup closes Is this the functionality you're looking for? That would mean that we would also have to handle the filter function

anihm136 avatar Oct 07 '20 05:10 anihm136

I haven't read the docs in a bit for this, but yeah, if you wanted to start implementing the features needed to eventually get on_close to work, that'd be fantastic.

I want this to be basically the exact end result of the vim api. I can take a deeper look if you need some help on the issue or want some guidance. Just let me know.

tjdevries avatar Oct 08 '20 02:10 tjdevries

Oh, I forgot to mention some items may require PRs sent to neovim core. I'd be happy to guide you or help out there.

tjdevries avatar Oct 08 '20 02:10 tjdevries

Any ideas on how to implement filter? My first idea was to map all the keys to a wrapper, which calls the function for the pressed key and returns the key if the filter function did not handle it. But this seems to be a very ugly solution. Once filter is implemented I think the on_close callback should be much easier Edit: Something along these lines

fun Filter(char) abort
	if a:char=='a'
		echo a:char	
		return v:true
	endif
	return v:false
endf

fun Wrapper(char) abort
	let l:captured = Filter(a:char)
	if !l:captured
		return a:char
	endif
endf

for i in range(97,122)
	exe 'map <expr> '.nr2char(i).' Wrapper("'.nr2char(i).'")'
endfor

anihm136 avatar Oct 08 '20 03:10 anihm136

@tjdevries could you take a look at this when you're free? I'm a little busy rn but I will get back to it. Just let me know what you think of this, and if there's a better way to handle the keystrokes

anihm136 avatar Oct 28 '20 11:10 anihm136