zsh-users
 help / color / mirror / code / Atom feed
* Yank index
@ 2024-05-21 20:17 Marlon Richert
  2024-05-21 20:59 ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Marlon Richert @ 2024-05-21 20:17 UTC (permalink / raw)
  To: Zsh Users

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?

For example, let’s say that I yanked the $CUTBUFFER, then cycled with yank-pop to $killring[2]. Inside my widget, I would like to be able to tell that I currently have $killring[2] on the command line instead of $CUTBUFFER.

Another example: yank-pop normally cycles from $CUTBUFFER to $killring and then steps through $killring from lowest to highest index, until it wraps around to $CUTBUFFER. I would like to cycle in the opposite direction.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Yank index
  2024-05-21 20:17 Yank index Marlon Richert
@ 2024-05-21 20:59 ` Bart Schaefer
  2024-05-22  4:58   ` Marlon Richert
  0 siblings, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2024-05-21 20:59 UTC (permalink / raw)
  To: Marlon Richert; +Cc: Zsh Users

Did you not get Peter's reply to the last time you asked this (yesterday)?

On Tue, May 21, 2024 at 1:17 PM Marlon Richert <marlon.richert@gmail.com> 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?

The killring rotates when you yank, so $killring[1] is always the
"active yank" and $killring[-1] is always the previous yank -- unless
you use a negative prefix (e.g., ESC - 1 before yank).

Presently (as I understand it from PWS's response yesterday), the only
way to "move the index" is to make a copy of $killring, rearrange it
however you want, and then assign it back via killring=(...).

Oliver may have additional insight.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Yank index
  2024-05-21 20:59 ` Bart Schaefer
@ 2024-05-22  4:58   ` Marlon Richert
  0 siblings, 0 replies; 6+ messages in thread
From: Marlon Richert @ 2024-05-22  4:58 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users


> On 21. May 2024, at 23.59, Bart Schaefer <schaefer@brasslantern.com> wrote:
> 
> Did you not get Peter's reply to the last time you asked this (yesterday)?

Oh, my bad.  Somehow my mail client still showed my email in Drafts. So, I thought I hadn’t sent it yet and pressed Send.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Yank index
  2024-05-20 13:17 ` Peter Stephenson
@ 2024-05-27  7:31   ` Marlon
  0 siblings, 0 replies; 6+ messages in thread
From: Marlon @ 2024-05-27  7:31 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 3017 bytes --]


> On 20. May 2024, at 16.17, Peter Stephenson <p.w.stephenson@ntlworld.com> wrote:
> 
>> On 20/05/2024 12:31 BST Marlon Richert <marlon.richert@gmail.com> 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.


[-- Attachment #2: Type: text/html, Size: 4883 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Yank index
  2024-05-20 11:31 Marlon Richert
@ 2024-05-20 13:17 ` Peter Stephenson
  2024-05-27  7:31   ` Marlon
  0 siblings, 1 reply; 6+ messages in thread
From: Peter Stephenson @ 2024-05-20 13:17 UTC (permalink / raw)
  To: Zsh Users

> On 20/05/2024 12:31 BST Marlon Richert <marlon.richert@gmail.com> 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.

pws


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Yank index
@ 2024-05-20 11:31 Marlon Richert
  2024-05-20 13:17 ` Peter Stephenson
  0 siblings, 1 reply; 6+ messages in thread
From: Marlon Richert @ 2024-05-20 11:31 UTC (permalink / raw)
  To: Zsh Users

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?

For example, let’s say that I yanked the $CUTBUFFER, then cycled with yank-pop to $killring[2]. Inside my widget, I would like to be able to tell that I currently have $killring[2] on the command line instead of $CUTBUFFER.

Another example: yank-pop normally cycles from $CUTBUFFER to $killring and then steps through $killring from lowest to highest index, until it wraps around to $CUTBUFFER. I would like to cycle in the opposite direction.



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-05-27  7:32 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-21 20:17 Yank index Marlon Richert
2024-05-21 20:59 ` Bart Schaefer
2024-05-22  4:58   ` Marlon Richert
  -- strict thread matches above, loose matches on Subject: below --
2024-05-20 11:31 Marlon Richert
2024-05-20 13:17 ` Peter Stephenson
2024-05-27  7:31   ` Marlon

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).