Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Yank index




On 20. May 2024, at 16.17, Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx> wrote:

On 20/05/2024 12:31 BST Marlon Richert <marlon.richert@xxxxxxxxx> wrote:
Inside a ZLE widget, is there a way to get the kill ring index of> the
active yank? Is it possible to move this index?

Looking at the code, I think it works in such a way that when you use
the special array $killring, available via the zsh/zleparameter module,
the first entry is the one most recently yanked, i.e. the killring you
see in the variable effectively cycles by itself.  (There is some
relationship between $killring and $CUTBUFFER --- I'm reading the code
as saying they can be manipulated independently, however.)

You can set killring=(...) and that similarly becomes the ring with the
first entry as if you'd just yanked it, so you can cycle through it just
by assigning the array in a different order.

However, I haven't tried this out, so I may be missing subtleties --- I
doubt this ever got thought through to the level of detail you're now
investigating.

I did some testing and doing yank-pop does _not_ cycle $killring. Here’s what actually happens:

- yank inserts $CUTBUFFER
- Each successive invocation of yank-pop replaces the active yank with the next non-zero entry of $killring, until it runs out of these, at which point it inserts $CUTBUFFER and starts over.

If we implement reverse-yank-pop by modifying $CUTBUFFER and $killring, this will _not_ work correctly, because these changes will affect the next yank.

For example, given the following implementation:

reverse-rotate-killring() {
  killring=( "$CUTBUFFER" "$killring[@]" )
  CUTBUFFER=$killring[-1]
  shift -p killring
}

reverse-yank-pop() {
  zle -f yankbefore
  reverse-rotate-killring
  reverse-rotate-killring
  zle .yank-pop
}

zle -N reverse-yank-pop
bindkey '^[Y' reverse-yank-pop

The problem with this approach is that changes to $CUTBUFFER and $killring are permanent: Whenever you use this particular reverse-yank-pop widget, you don’t only change what you yank now, you also change what you will yank _next time._

For example, given the above implementation and CUTBUFFER=a killring=( b c ):

- If I yank -> yank-pop -> yank, the command line changes a -> b -> ba
- If I yank -> reverse-yank-pop -> yank, the command line changes a -> c -> cc

One would expect each yank above to insert a, but for the last yank this is no longer true, because our clipboard has changed to CUTBUFFER=c killring=( c a )

Instead, this appears to be the only way to get reverse-yank-pop to work correctly:

reverse-yank-pop() {
  zle -f yankbefore
  local nonzero=( $CUTBUFFER $killring )
  repeat $(( $#nonzero - 1 )); do zle .yank-pop; done
}

zle -N reverse-yank-pop
bindkey '^[Y' reverse-yank-pop

Being able to do something like (( YANK_INDEX-- )) or zle yank-pop -- -1 would be a lot cleaner, but at least this implementation is simple and short.



Messages sorted by: Reverse Date, Date, Thread, Author