zsh-users
 help / color / mirror / code / Atom feed
From: Marlon <marlon.richert@gmail.com>
To: Peter Stephenson <p.w.stephenson@ntlworld.com>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: Yank index
Date: Mon, 27 May 2024 10:31:18 +0300	[thread overview]
Message-ID: <4E7F7B4B-F105-4258-8E94-5E2E1814825B@gmail.com> (raw)
In-Reply-To: <576626392.1946457.1716211026926@mail.virginmedia.com>

[-- 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 --]

  reply	other threads:[~2024-05-27  7:32 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-20 11:31 Marlon Richert
2024-05-20 13:17 ` Peter Stephenson
2024-05-27  7:31   ` Marlon [this message]
2024-05-21 20:17 Marlon Richert
2024-05-21 20:59 ` Bart Schaefer
2024-05-22  4:58   ` Marlon Richert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4E7F7B4B-F105-4258-8E94-5E2E1814825B@gmail.com \
    --to=marlon.richert@gmail.com \
    --cc=p.w.stephenson@ntlworld.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).