#4183
plugins: add `onBufferClose()` to micro
Description
There is no easy way to know when a Buffer is closed. If you replace it by calling BufPane:OpenBuffer() directly from a plugin, it does not trigger anything.
There is no easy way to know when a Buffer is closed. If you replace it by calling BufPane:OpenBuffer() directly from a plugin, it does not trigger anything.
I had a dig through this. I think the natural spot is
Buffer.Close(), firing beforeFini()and before the buffer is dropped fromOpenBuffers:Close()is where every real close path ends up:ForceQuit,QuitAllviaCloseOpenBuffers, tab and split closes, andBufPane.OpenBuffer(), which is the case you hit. Putting it inside theif b == bufbranch also means no double-fire if something closes a buffer twice.Doing it as an
onQuitaction hook instead doesn't really work, for what it's worth. Quitting the last pane reachesruntime.Goexit()before the hook would run, and it wouldn't fire forOpenBufferor:openat all.Firing before
Fini()matters because the buffer is still fully readable at that point: path, settings, contents, cursors. And firing while it's still inOpenBuffersmakesbuf:Shared()usable inside the callback, sinceShared()skips the buffer itself. That lets a plugin tell "this view is going away" from "this file is now completely closed" rather than us picking one meaning for them.I'd make it non-cancellable (
RunPluginFn, same asonBufferOpen). By the timeClose()runs the decision is already made, and a veto duringQuitAllwould just be ignored since shutdown continues regardless.One gap worth naming: it won't fire on SIGTERM/SIGHUP, because
exit()callsFini()directly. I'd lean towards documenting that rather than running Lua from the panic recovery path, but happy to be told otherwise.Two things I'm unsure about:
RunPluginFnorRunPluginFnBool(b.Settings, ...)?onBufferOpenignores the per-buffer plugin setting whilePluginCBhonours it. I matchedonBufferOpen, but I can see the other argument.BTInfo/BTLog/BTHelpas well?onBufferOpendoes, so I assumed yes, but it does mean plugins see a close for the infobar during shutdown.If that sounds right, I'll open a PR with the hook, a
plugins.mdentry and a test.