Hi,
As discussed in #838, it'd be useful to have a hook to know when registers are modified, with filtering based on which register is modified.
Huh, I should have know there would already be an issue for this. Here's the text of the issue I was about to create:
In the style of hooks.asciidoc:
RegisterWrite: Triggered after text has been written to a register, filtering text is the register name. While this hook is being executed, no further RegisterRead or RegisterWrite hooks for this register will be triggered.
RegisterRead: Triggered before text is read from a register, filtering text is the register name. While this hook is being executed, no further RegisterRead or RegisterWrite hooks for this register will be triggered.
There's lots of potential uses for this functionality:
xclip or tmux set-buffer."% and "_ registers in kakoune's standard configuration files, instead of hard-coding them in C++.It would be a convenient way to access generated data. For example:
hook global RegisterRead d reg d %sh{date --rfc-3339 seconds}
...so that pasting from register d always pastes the current date and time.
"a and "A registers as identical, as opposed to the way Vim uses uppercase to mean "append to the existing value". This feature would allow people who miss that functionality to add it themselves, instead of adding it to the C++ core.The Registers & Clipboard wiki page already has some suggestions for integrating kakoune with the system clipboard, but honestly I would much rather hit y than <a-|>xsel --input --clipboard <ret>. There are also example hooks to make copy and paste work with y, d and c, but of course they don't help with <c-r> on the command-line, the %reg{} expansion or the :reg command, making them very leaky abstractions.
The vis editor does not support register hooks, but it has special "* and "+ registers that call vis to invoke the vis-clipboard command. People who want to customize copy/paste behaviour are expected to write their own implementation of vis-clipboard and put it on $PATH ahead of the standard version.
Vim also does not support register hooks, and completely hard-codes the behaviour of its "* and "+ registers.
ironzorg: but the hook should probably work as the SetOption does, if all registers are monitored and a single hook fired consequently, it's going to slow things down
ironzorg: the index register changes often, so does the mark one, plus the integers when doing capturing searches…
ironzorg: hook global RegisterChanged \+=.* %{ %sh{ printf 'old value: %s\n' "${kak_hook_param#*=}" >&2 } }
An implementation of the RegisterChanged hook can be found here: https://github.com/doppioandante/kakoune/tree/register-hooks
It is buggy (as @Screwtapello says it shouldn't fire anymore in case of changes made by the hook itself).
Something like RegisterRead wasn't implemented but it should be easy.
I would like this functionality as well to generalize the [Yank Ring] to all kind of register; for example, refreshing an old search with yank-ring /.
Would make integrating with OS clipboard a lot easier as it can be automatically injected to all yank operations.
This also seems useful to me, but my only actual use case is also clipboard. I currently copy %reg{"'} to the system on NormalIdle when it changes, and copy from system to %reg{"} on FocusIn if it changed.
Nice!
Most helpful comment
Huh, I should have know there would already be an issue for this. Here's the text of the issue I was about to create:
Description
In the style of
hooks.asciidoc:RegisterWrite: Triggered after text has been written to a register, filtering text is the register name. While this hook is being executed, no further RegisterRead or RegisterWrite hooks for this register will be triggered.
RegisterRead: Triggered before text is read from a register, filtering text is the register name. While this hook is being executed, no further RegisterRead or RegisterWrite hooks for this register will be triggered.
Rationale
There's lots of potential uses for this functionality:
xcliportmux set-buffer."%and"_registers in kakoune's standard configuration files, instead of hard-coding them in C++.It would be a convenient way to access generated data. For example:
...so that pasting from register
dalways pastes the current date and time."aand"Aregisters as identical, as opposed to the way Vim uses uppercase to mean "append to the existing value". This feature would allow people who miss that functionality to add it themselves, instead of adding it to the C++ core.Alternatives
The Registers & Clipboard wiki page already has some suggestions for integrating kakoune with the system clipboard, but honestly I would much rather hit
ythan<a-|>xsel --input --clipboard <ret>. There are also example hooks to make copy and paste work withy,dandc, but of course they don't help with<c-r>on the command-line, the%reg{}expansion or the:regcommand, making them very leaky abstractions.The vis editor does not support register hooks, but it has special
"*and"+registers that call vis to invoke thevis-clipboardcommand. People who want to customize copy/paste behaviour are expected to write their own implementation ofvis-clipboardand put it on$PATHahead of the standard version.Vim also does not support register hooks, and completely hard-codes the behaviour of its
"*and"+registers.