* completion: _normal after argument
@ 2014-07-28 12:16 Martin Vaeth
2014-07-28 20:12 ` Peter Stephenson
0 siblings, 1 reply; 5+ messages in thread
From: Martin Vaeth @ 2014-07-28 12:16 UTC (permalink / raw)
To: zsh-users
Hi,
for some scripts I am currently writing some completion functions.
The syntax of the call should be
foo [lots of complicated options] CMD [shell-command with options]
Naturally, since the options can be treated with _arguments in
a standard way, I want to use something like
_arguments -C -s -S -A '-*' : \
--optA=.... \
--optB... \
-q... \
...
'1:command:->cmds' \
'*::command:->normal'
It is clear to me what to do in case $state == cmds
But what to do in case $state == normal?
Essentially, I want to call of course _normal
(after cutting the "options+CMD" part from the words array and
decreasing the CURRENT parameter correspondingly).
But how to find where the "options+CMD" part has finished?
Is it really necessary to "manually" parse all the options again
(checking what is argument etc, that is, essentially to re-programm
manually what should already have been done by _arguments)
or is there another possibility?
In case the above description is unclear:
Here is a link to the [yet unsatisfactory] completion file
https://github.com/vaeth/schedule/blob/master/zsh/_schedule
(Currently, I always remove everything up to CURRENT, but this is
"correct" only for the first argument after CMD, of course)
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: completion: _normal after argument
2014-07-28 12:16 completion: _normal after argument Martin Vaeth
@ 2014-07-28 20:12 ` Peter Stephenson
2014-07-29 2:54 ` Bart Schaefer
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Peter Stephenson @ 2014-07-28 20:12 UTC (permalink / raw)
To: zsh-users
On Mon, 28 Jul 2014 12:16:10 +0000 (UTC)
Martin Vaeth <martin@mvath.de> wrote:
> for some scripts I am currently writing some completion functions.
> The syntax of the call should be
>
> foo [lots of complicated options] CMD [shell-command with options]
>
> Naturally, since the options can be treated with _arguments in
> a standard way, I want to use something like
>
> _arguments -C -s -S -A '-*' : \
> --optA=.... \
> --optB... \
> -q... \
> ...
> '1:command:->cmds' \
> '*::command:->normal'
>
> It is clear to me what to do in case $state == cmds
> But what to do in case $state == normal?
> Essentially, I want to call of course _normal
> (after cutting the "options+CMD" part from the words array and
> decreasing the CURRENT parameter correspondingly).
> But how to find where the "options+CMD" part has finished?
You can use _arguments -n.
With the option -n, _arguments sets the parameter NORMARG to the
position of the first normal argument in the $words array, i.e.
the position after the end of the options. If that argument has
not been reached, NORMARG is set to -1. The caller should
declare `integer NORMARG' if the -n option is passed; otherwise
the parameter is not used.
Supposedly you don't even need to do that, however:
*:::message:action
This describes how arguments (usually non-option argu‐
ments, those not beginning with - or +) are to be com‐
pleted when neither of the first two forms was provided.
Any number of arguments can be completed in this fashion.
With two colons before the message, the words special
array and the CURRENT special parameter are modified to
refer only to the normal arguments when the action is
executed or evaluated. With three colons before the mes‐
sage they are modified to refer only to the normal argu‐
ments covered by this description.
I have a feeling I've had trouble with that in the past though (and,
obviously, no one in their senses would try to understand _arguments).
pws
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: completion: _normal after argument
2014-07-28 20:12 ` Peter Stephenson
@ 2014-07-29 2:54 ` Bart Schaefer
2014-07-29 7:37 ` Oliver Kiddle
2014-07-29 8:50 ` Martin Vaeth
2 siblings, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2014-07-29 2:54 UTC (permalink / raw)
To: zsh-users
On Jul 28, 9:12pm, Peter Stephenson wrote:
} Subject: Re: completion: _normal after argument
}
} On Mon, 28 Jul 2014 12:16:10 +0000 (UTC)
} > Essentially, I want to call of course _normal
} > (after cutting the "options+CMD" part from the words array and
} > decreasing the CURRENT parameter correspondingly).
} > But how to find where the "options+CMD" part has finished?
}
} You can use _arguments -n.
}
} With the option -n, _arguments sets the parameter NORMARG to the
} position of the first normal argument in the $words array
I found two examples of this in Completion/**/: _todo.sh and _zmodload.
Neither of them uses it to call _normal.
} Supposedly you don't even need to do that, however:
}
} *:::message:action
} With two colons before the message, the words special
} array and the CURRENT special parameter are modified to
} refer only to the normal arguments when the action is
} executed or evaluated. With three colons before the mes-
} sage they are modified to refer only to the normal argu-
} ments covered by this description.
Many examples of the '*::' form to call _normal. None of the '*:::'
variety, for any purpose.
} I have a feeling I've had trouble with that in the past though (and,
} obviously, no one in their senses would try to understand _arguments).
This is all handled in C code in bin_comparguments anyway ... to be
specific, in parse_cadef. Which looks like it should be doing the
right thing ...
Given the original usage, I'd start by trying '*:::command:->normal'
and see what happens.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: completion: _normal after argument
2014-07-28 20:12 ` Peter Stephenson
2014-07-29 2:54 ` Bart Schaefer
@ 2014-07-29 7:37 ` Oliver Kiddle
2014-07-29 8:50 ` Martin Vaeth
2 siblings, 0 replies; 5+ messages in thread
From: Oliver Kiddle @ 2014-07-29 7:37 UTC (permalink / raw)
To: zsh-users
Peter Stephenson wrote:
> Supposedly you don't even need to do that, however:
> *:::message:action
> executed or evaluated. With three colons before the mes‐
> sage they are modified to refer only to the normal argu‐
> ments covered by this description.
The three colon form should be right in this case. Shouldn't even need a
state: '*:::command:_normal' should work.
> I have a feeling I've had trouble with that in the past though (and,
> obviously, no one in their senses would try to understand _arguments).
Most of the troubles come when you're trying to call _arguments again.
Message 32848 on zsh-workers is also related. It can be useful to use a
small function to print $CURRENT and $words on a different tty to
understand things, and remember that _arguments expects words to start
with a command name.
Oliver
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: completion: _normal after argument
2014-07-28 20:12 ` Peter Stephenson
2014-07-29 2:54 ` Bart Schaefer
2014-07-29 7:37 ` Oliver Kiddle
@ 2014-07-29 8:50 ` Martin Vaeth
2 siblings, 0 replies; 5+ messages in thread
From: Martin Vaeth @ 2014-07-29 8:50 UTC (permalink / raw)
To: zsh-users
Although the reply is only to one posting,
my thank goes to everybody who replied.
Peter Stephenson <p.w.stephenson@ntlworld.com> wrote:
>>
>> foo [lots of complicated options] CMD [shell-command with options]
>
> You can use _arguments -n.
> [...]
> Supposedly you don't even need to do that, however:
>
> *:::message:action
Thanks a lot.
Indeed, the latter works in my case for the original question
(I just tried) but -n will be much more handy since it will allow
me to make the _normal completion conditional depending on CMD.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2014-07-29 8:51 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-28 12:16 completion: _normal after argument Martin Vaeth
2014-07-28 20:12 ` Peter Stephenson
2014-07-29 2:54 ` Bart Schaefer
2014-07-29 7:37 ` Oliver Kiddle
2014-07-29 8:50 ` Martin Vaeth
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).