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

Re: Questions about completion matchers



On Sat, Oct 9, 2021 at 7:23 PM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
>
> I think what you've missed is that there are two things being
> examined:  The word on the command line, and the "trial completion",
> that is, the word passed to compadd that might replace the one on the
> command line.  It's not merely (choosing the first of your seeming
> duplications)
>
> > *              b:$lpat=$tpat -> ${word/#$~lpat/$~tpat}
> > *             l:|$lpat=$tpat -> ${word/#$~lpat/$~tpat}
>
> Rather it's
>
> *              b:$lpat=$tpat -> [[ $trial = ${~lpat}* ]] &&
> ${word/#$~lpat/$~tpat}
> *             l:|$lpat=$tpat -> [[ $trial = *${~lpat}* ]] &&
> ${word/#$~lpat/$~tpat}

Perhaps I'm mistaken, but aren't you mixing up $lpat and $lanchor
here? In the docs, it says:

> Matching for lpat and tpat is as for m and M, but the pattern lpat matched on the command line must be preceded by the pattern lanchor. The lanchor can be blank to anchor the match to the start of the command line string; otherwise the anchor can occur anywhere, but must match in both the command line and trial completion strings.

Above, $lanchor is blank and thus needs to match only the start of the
command line string, whereas anything to the right of $lanchor that
matches $~lpat is simply replaced with $~tpat, just as in
m:$lpat=$tpat. Are the docs wrong or am I understanding them wrong?

Thanks, though, for this example, because it does help me understand
what the documentation means with:

> If no lpat is given but a ranchor is, this matches the gap between substrings matched by lanchor and ranchor. Unlike lanchor, the ranchor only needs to match the trial completion string.

Before, it was unclear to me how I should interpret "only needs to
match the trial completion string", but now, I suppose it would be
like this:

l:$lanchor||$ranchor=$tpat -> [[ $trial == *$~lanchor$~tpat$~ranchor*
]] && ${word//(#m)($~lanchor)/$MATCH$~tpat}

However, isn't that equivalent to the following?

l:$lanchor||$ranchor=$tpat -> ${word//(#m)($~lanchor)/$MATCH$~tpat$~ranchor}

Correct me if I'm wrong, but this seems to match the exact same trial
strings. But then, the above transformation would be equivalent to
this one:

l:$lanchor|=$tpat$ranchor -> ${word//(#b)($~lanchor)/$match[1]$~tpat$~ranchor}

Again, something doesn't seem right here.


On Sat, Oct 9, 2021 at 7:23 PM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> In the cases with r: and R:, $ranchor is only compared to $trial, it
> is not used when replacing into $word.

Are you sure? The phrase "the ranchor only needs to match the trial
completion string" is listed in the docs for l and L. Conversely, in
the docs for r and R, it says "As l, L, b and B, with the difference
that the command line and trial completion patterns are anchored on
the right side." This makes me believe that, in the case of r and R,
it is in fact $lanchor that is compared only to $trial, not $ranchor.
If your interpretation were correct, l:$lanchor||$ranchor=$tpat and
r:$lanchor||$ranchor=$tpat would be completely equivalent.


Perhaps I should consider the _examples_ in the docs to be the truth
and just ignore the ambiguous wording given earlier on in the docs. If
I do that, then Oliver's examples now start to make sense to me and I
can deduce the transformations as follows:

Given 'r:|.=* r:|=*', c.u becomes c(^*.*).u* and c.s.u becomes
c(^*.*).s(^*.*).u*. Ergo:

* r:$lpat|$ranchor=$tpat  ->
${word//(#b)$~lpat($~ranchor)/($~tpat~*$~ranchor*)$match[1]}
* l:$lanchor|$lpat=$tpat  ->
${word//(#b)($~lanchor)$~lpat/$match[1]($~tpat~*$~lanchor*)}
* r:$lpat|=$tpat  ->  ${word%$~lpat}$~tpat
* l:|$lpat=$tpat  ->  $~tpat${word#$~lpat}

Given  'r:|.=** r:|=*', c.u becomes c*.u*.
Given 'r:|[[:upper:]0-9]=** r:|=*', H becomes *H* and 2 becomes *2*.
Ergo:

* r:$lpat|$ranchor=**  ->  ${word//(#b)$~lpat($~ranchor)/*$match[1]}
* l:$lanchor|$lpat=**  ->  ${word//(#b)($~lanchor)$~lpat/$match[1]*}

Given 'r:[^[:upper:]0-9]||[[:upper:]0-9]=** r:|=*', H becomes
*[^[:upper:]0-9]H* and 2 becomes *[^[:upper:]0-9]2*. Ergo:

* r:$lanchor||$ranchor=**  ->  ${word//(#m)($~ranchor)/*$~lanchor$MATCH}
* l:$lanchor||$ranchor=**  ->  ${word//(#m)($~lanchor)/$MATCH$~lanchor*}

Given 'B:[nN][oO]= M:_= M:{[:upper:]}={[:lower:]}', both _NO_f and
NONO_f become f. Ergo:

* b:$lpat=$tpat  ->  ${word/#(#b)(|?)($~lpat)##/$match[1]$~tpat}
* e:$lpat=$tpat  ->  ${word/%(#b)($~lpat)##(|?)/$~tpat$match[-1]}


How about changing the docs to just literally state the transformation
that each matcher applies? It would be clearer than the prose it
currently contains, which is ambiguous and open to interpretation.




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