From: Arseny Maslennikov <ar@cs.msu.ru>
To: zsh-workers@zsh.org
Subject: Re: Q: _alternative's `(( w1:d1 w2:d2 ))' form
Date: Fri, 12 Nov 2021 22:58:56 +0300 [thread overview]
Message-ID: <YY7HgPwD8FT+WvpR@cello> (raw)
In-Reply-To: <YY7BaE0FV2UW3Cg1@cello>
[-- Attachment #1: Type: text/plain, Size: 2254 bytes --]
On Fri, Nov 12, 2021 at 10:32:56PM +0300, Arseny Maslennikov wrote:
> Hi!
>
> The doc page for _alternative (citing `info zsh`, menu item `Completion
> Functions') states:
> _alternative [ -O NAME ] [ -C NAME ] SPEC ...
> <...>
> The tags to use and the action to perform if a tag is requested are
> described using the SPECs which are of the form:
> 'TAG:DESCR:ACTION'. The TAGs are offered using _tags and if the
> tag is requested, the ACTION is executed with the given description
> DESCR. The ACTIONs are those accepted by the _arguments function
> (described below), excluding the '->STATE' and '=...' forms.
> <...>
>
> Later on, the text for _arguments advises to escape the separating
> colons between the match candidate and its description in a (( ))
> action spec, due to more complex spec parsing and further optional
> fields. A thorough reader of the docs might think the same is
> required by _alternative as well.
>
> I've been looking at the implementation of _alternative lately; the
> function splits its specs into exactly 3 fields, delimited by `:'. The
> following seems to be true:
>
> * the first field is delimited by the first colon;
> * the second field spans from after the first field's delimiter to the
> nearest colon;
> * the rest of the spec is interpreted as the action field, unlike
> the specs for _arguments.
>
On a related note: this looks so obvious with hindsight but I've found
that the following way to avoid escaping the `:' works for _arguments too:
function _ooo {
local __ndescs=(
'0:0' '1:1' '2:2' '3:3'
'4:4' '5:5' '6:6' '7:7'
'8:8' '9:9' 'A:10' 'B:11'
'C:12' 'D:13' 'E:14' 'F:15'
)
_arguments '1:letter:(a b c y z)' '2:digit:(( "${(@)__ndescs}" ))'
}
The expansion of `__ndescs' happens during the action eval, and the
expression does not contain any colons.
This approach is surprisingly quite unpopular, especially in third-party
completions — instead, the authors prefer to use a function as the
action, which then calls _describe by itself (here goes another while
tags; do while _next_label; do done; done loop) and does not introduce
new tags.
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2021-11-12 19:59 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-12 19:32 Arseny Maslennikov
2021-11-12 19:58 ` Arseny Maslennikov [this message]
2021-11-12 20:09 ` Oliver Kiddle
2021-11-12 21:19 ` [PATCH] Doc/Zsh/compsys.yo: clarify _alternative's action syntax Arseny Maslennikov
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=YY7HgPwD8FT+WvpR@cello \
--to=ar@cs.msu.ru \
--cc=zsh-workers@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).