Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: _arguments optspecs (was Re: Editing the history in the shell)

Bart wrote:
> On Sat, May 23, 2020 at 2:30 PM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> >
> > I'm not entirely sure about some
> > of the _zed optspecs, but all the desired combinations seem to work
> > and all the undesired ones seem not to.

How about the following combination:

zed -f -x2 -- <tab>

> My uncertainty is around these three lines:
> >      '(- 2):file:_files' \
> > +    '(-f -x 1)-h[edit history]:history file:_files' \
> > +    '2:history size: ';;
> The first spec causes "file" to be offered (as the first non-option
> argument)  if there is no option yet, and will not offer a second
> argument.

The first thing to note is that if you leave the number out as in the
first spec, it will assign it the next number. So the 1 in the exclusion
list for -h does apply to the first spec. I try to avoid leaving the
number out for any spec that is the target of an exclusion.

> The second spec offers -h followed by "history file" as the argument
> to -h, and won't offer -f or -x or a first non-option argument.

Yes. And in excluding the first non-option argument, it will move on and
offer the second one in the first position. So you get "history size" in
position 1 despite it being labelled as 2.

> What's strange is why "history size" is the second non-option
> argument.  If I leave (1) out of the exclusion list on the second
> spec, I get offered "file", so in that case the argument to -h is
> being taken as an option argument; but after filling in that position
> I next get "no more arguments" instead of "history files".  So it
> appears that sometimes the argument of -h is not counted as a
> non-option argument, and other times it is so counted, which is why
> it's "2:" in the third optspec and the (1) is required in the second
> optspec.

I can't find any arrangement of words on a command-line that gives the
argument after -h as being anything other than "history file". Neither
is it being counted as one of the non-option arguments.

> Does anybody actually understand this?  Is it explainable (or already
> explained, where?) in the doc for _arguments?

It seems clear enough to me but I know enough of the implementation's
workings to be approaching it from that angle. Which may not mean I'm
best able to explain it.

There's a list of possible states. In each state, individual specs
are disabled if an option is found which would disable it. If you
exclude positional arguments, subsequent ones become active at an
earlier position. So you can exclude lots of them and there is always
something to complete. Having gaps is fairly useless as is apparent if
you try: compdef "_arguments '3:three'" foo
This approach may not be the most readable but I think it works:

	'(-h 1 3 4)-f[edit function]' \
	'(-h 1 3 4)-x+[specify spaces to use for indentation in function expansion]:spaces' \
	'(-f -x 1 2)-h[edit history]' \
	'(- 2 3 4)1:file:_files' \
	'(3 4)2:shell function:_functions' \
	'3:history file:_files' \
	'4:history size';;


Messages sorted by: Reverse Date, Date, Thread, Author