zsh-users
 help / color / mirror / code / Atom feed
* Strange behavior when calling select-a-shell-word multiple times from the same user-defined widget
@ 2021-12-22 21:20 Daniel Parks
  2021-12-22 22:08 ` Daniel Shahaf
  0 siblings, 1 reply; 4+ messages in thread
From: Daniel Parks @ 2021-12-22 21:20 UTC (permalink / raw)
  To: zsh-users

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

Hello zsh-users,

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

Given the command line `echo these are arguments`, I would expect the
command `3gva` to select like this:

echo these[ are] arguments

and indeed that's what happens if I run the following commands
(emualating the for loop myself):

 - :vi-beginning-of-line
 - :visual-mode
 - :select-a-shell-word
 - :deactivate-region
 - 2:vi-forward-char
 - :visual-mode
 - :select-a-shell-word
 - :deactivate-region
 - 2:vi-forward-char
 - :visual-mode
 - :select-a-shell-word

Nevertheless, when I run the command `3gva` it selects like this:

[echo these are] arguments

Does anybody know what's going on here?

Thanks,
Daniel

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: Strange behavior when calling select-a-shell-word multiple times from the same user-defined widget
  2021-12-22 21:20 Strange behavior when calling select-a-shell-word multiple times from the same user-defined widget Daniel Parks
@ 2021-12-22 22:08 ` Daniel Shahaf
  2021-12-22 23:09   ` Daniel Parks
  0 siblings, 1 reply; 4+ messages in thread
From: Daniel Shahaf @ 2021-12-22 22:08 UTC (permalink / raw)
  To: Daniel Parks, zsh-users

[ -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))».]


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

* Re: Strange behavior when calling select-a-shell-word multiple times from the same user-defined widget
  2021-12-22 22:08 ` Daniel Shahaf
@ 2021-12-22 23:09   ` Daniel Parks
  2021-12-23 13:24     ` Oliver Kiddle
  0 siblings, 1 reply; 4+ messages in thread
From: Daniel Parks @ 2021-12-22 23:09 UTC (permalink / raw)
  To: zsh-users

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

On Wed, 2021-12-22 at 22:08 +0000, Daniel Shahaf wrote:
> Adding «-N» to both select-a-shell-word calls does the trick.

Thanks, that fixed it! I do still think it's pretty strange, though,
because select-a-shell-word does not normally seem to respond to the
numeric argument in a command like `v3aa` (still only selects one
word).

> 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»

Nice, thanks for pointing that out.

>  — 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
> }

I want to select the nth argument on the command line, taking into
account spaces, so using vi-forward-word is not a good choice. For
example, on the command `echo "these are all one" third` I want
3:select-n-shell-word to select

echo "these are all one"[ third]

not

echo[ "these are all one"] third

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

Oops, thank you for that as well.

I appreciate all the help! It's working perfectly now.



[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: Strange behavior when calling select-a-shell-word multiple times from the same user-defined widget
  2021-12-22 23:09   ` Daniel Parks
@ 2021-12-23 13:24     ` Oliver Kiddle
  0 siblings, 0 replies; 4+ messages in thread
From: Oliver Kiddle @ 2021-12-23 13:24 UTC (permalink / raw)
  To: Daniel Parks; +Cc: zsh-users

Daniel Parks wrote:
> On Wed, 2021-12-22 at 22:08 +0000, Daniel Shahaf wrote:
> > Adding «-N» to both select-a-shell-word calls does the trick.
>
> Thanks, that fixed it! I do still think it's pretty strange, though,
> because select-a-shell-word does not normally seem to respond to the
> numeric argument in a command like `v3aa` (still only selects one
> word).

It does select three words but goes backwards from the cursor rather
than forwards which is perhaps not what you expected.

Oliver



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

end of thread, other threads:[~2021-12-23 13:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-22 21:20 Strange behavior when calling select-a-shell-word multiple times from the same user-defined widget Daniel Parks
2021-12-22 22:08 ` Daniel Shahaf
2021-12-22 23:09   ` Daniel Parks
2021-12-23 13:24     ` Oliver Kiddle

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