zsh-users
 help / color / mirror / code / Atom feed
From: "Daniel Shahaf" <d.s@daniel.shahaf.name>
To: "Daniel Parks" <danielrparks@gmail.com>, zsh-users@zsh.org
Subject: Re: Strange behavior when calling select-a-shell-word multiple times from the same user-defined widget
Date: Wed, 22 Dec 2021 22:08:10 +0000	[thread overview]
Message-ID: <6e3644fd-07ad-4251-9310-b6899c41a233@www.fastmail.com> (raw)
In-Reply-To: <2495135b4cf73e7add73e3fcd5fd6165a76b3cfa.camel@gmail.com>

[ -workers@: see at the end ]

Daniel Parks wrote on Wed, 22 Dec 2021 21:20 +00:00:
> I'm trying to write a function that selects the nth argument in the
> buffer. I would use it like `3gva` to select the 3rd argument.
>
> Here's the widget I have so far:
>
> function select-n-shell-word {
> 	if [[ -z "$NUMERIC" ]]; then NUMERIC=1; fi
> 	zle vi-beginning-of-line
> 	zle visual-mode
> 	zle select-a-shell-word
> 	for ((i = 1; i < $NUMERIC; i = i + 1)); do
> 		zle deactivate-region
> 		zle vi-forward-char -n 2
> 		zle visual-mode
> 		zle select-a-shell-word
> 	done
> }
> zle -N select-n-shell-word
> bindkey -M vicmd "gva" select-n-shell-word
>

Adding «-N» to both select-a-shell-word calls does the trick.

While here, you can simplify the function a bit.  For one, you could use
a «repeat» loop — its repeat count is a math expression, so you can
actually do «repeat "NUMERIC - 1"; do …; done» — but I'm not sure why you
select-a-shell-word more than once.  Why not just this:

select-n-shell-word() {
  CURSOR=0
  repeat "${NUMERIC:-1} - 1" zle vi-forward-word
  zle visual-mode
  zle select-a-shell-word -N
}

«CURSOR=0» is not equivalent to «zle vi-beginning-of-line» when there's
a literal newline in $BUFFER.  Pick what you prefer.

Using «repeat» rather than «zle … -n» to handle the case (( NUMERIC == 1 ))
correctly.

>

Aside: «repeat 'foo+1'» works, but «repeat '$foo+1'» (with single quotes) errors:
.
    zsh: bad math expression: operator expected at `foo + 1'
.
So, apparently, the repeat count word does not undergo parameter
interpolation before being parsed as a math expression, the way
«$((…))» expansions do.  [To see the last bit, try «s='1+1'; echo $((s*2)) $(($s*2))».]


  reply	other threads:[~2021-12-22 22:09 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-22 21:20 Daniel Parks
2021-12-22 22:08 ` Daniel Shahaf [this message]
2021-12-22 23:09   ` Daniel Parks
2021-12-23 13:24     ` Oliver Kiddle

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=6e3644fd-07ad-4251-9310-b6899c41a233@www.fastmail.com \
    --to=d.s@daniel.shahaf.name \
    --cc=danielrparks@gmail.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).