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

Add hook to notify that a register has been modified #859

Closed
occivink opened this issue Oct 10, 2016 · 6 comments
Closed

Add hook to notify that a register has been modified #859

occivink opened this issue Oct 10, 2016 · 6 comments

Comments

@occivink
Copy link
Contributor

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.

@Screwtapello
Copy link
Contributor

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:

  • This would make it easy to integrate with an external clipboard tool, like xclip or tmux set-buffer.

  • It would be possible to implement at least the "% 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.

  • This would also allow plugins to implement more sophisticated copy/paste semantics, like a LIFO or FIFO buffer instead of just straight replacement

  • So far as I can tell, kakoune treats the "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.

  • It would make it very convenient to edit code and repeatedly send it to an external REPL to be executed. For certain development tasks that require rapid iteration of complex code (such as tweaking a SQL query), the fewer keystrokes between making a change and seeing if it works, the better.

Alternatives

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.

@doppioandante
Copy link
Contributor

doppioandante commented Oct 13, 2017

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.

@alexherbo2
Copy link
Contributor

alexherbo2 commented Mar 29, 2019

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 /.

@absolutejam
Copy link

Would make integrating with OS clipboard a lot easier as it can be automatically injected to all yank operations.

@eraserhd
Copy link
Contributor

eraserhd commented Mar 7, 2020

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.

@mawww mawww closed this as completed in 47ba36c Jul 19, 2020
@eraserhd
Copy link
Contributor

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants