Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
compadd: with two calls, the first using a -M matcher with an anchor, TAB removes the opening quote of the word
- X-seq: zsh-workers 55308
- From: Yufei Pan <discodization@xxxxxxxxx>
- To: zsh-workers@xxxxxxx
- Subject: compadd: with two calls, the first using a -M matcher with an anchor, TAB removes the opening quote of the word
- Date: Tue, 01 Dec 2026 01:09:04 -0800
- Archived-at: <https://zsh.org/workers/55308>
- List-id: <zsh-workers.zsh.org>
Hello,
When a completion function calls `compadd` twice, the first call with a `-M` matcher that has an anchor (`r:|-=*`), the second
without a matcher, and the word being completed is quoted and starts with the anchor character, TAB takes the opening quote off
the word, or deletes what was typed. With the two calls the other way round, or with the same matcher on both, the line is fine.
For a quoted argument this changes what the shell reads: the argument falls apart into words.
To reproduce, with no rc files:
% zsh -f
% autoload -Uz compinit; compinit -u
% _zzq() { compadd -M 'r:|-=*' -- -color; compadd -- -colother }
% compdef _zzq zzq
% zzq "-c<TAB>
Expected: `zzq "-colo` (both names start with `-colo`). That is what zsh gives when the two calls are swapped, or when both
carry the same `-M 'r:|-=*'`.
Actual: `zzq -c`. The opening double quote is gone, and `-colo` is not inserted.
Variations (the line left after TAB, from a pty):
zzq "-<TAB> zzq " (the typed - is gone; expected zzq "-colo)
zzq '-c<TAB> zzq -c (a single quote goes the same way)
zzq -c<TAB> zzq -c (no quote: nothing is inserted, but nothing is lost)
Seen alike, every time, on zsh 5.0.2, 5.5.1, 5.8, 5.9, 5.9.2 and on master fcfdef8b62c7.
A reading of where it comes from, not a fix, and I have not read all of it. For `zzq "-c`, `compstate[unambiguous]` after the
two calls is just `"`, where `"-colo` is expected, so the common start that zsh inserts is empty apart from the quote. It looks as
if the matches of the two calls are joined one with the next (`add_match_data` in compcore.c calls `join_clines` and
`cmp_anchors` in compmatch.c) after each is cut at its own call's anchors, and the parts that do not pair are dropped. The comment
in `do_ambiguous` (compresult.c) describes this case: "Sometimes the different match specs used may result in a cline that gives
an empty string. If that happened, we re-insert the old string. Unless there were matches added with -U, that is." Here the old
string that comes back seems to be the word without its opening quote. That the typed text is otherwise dropped can be seen on
master by making the second call `compadd -U -- -colother`, which turns that re-insert off: `zzq "-c<TAB>` then gives `zzq "`,
and unquoted `zzq -c<TAB>` gives `zzq `.
Thank you for zsh.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author