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

'compadd -P' matches $PREFIX greedily



The following completion function:

% compdef 'compadd -P p_____ papa mama nana aaaa' f

gives:

% f ma<TAB>   → p_____mama
% f na<TAB>   → p_____nana
% f pa<TAB>   → p_____aaaa
%
% f pm<TAB>   → p_____mama
% f pn<TAB>   → p_____nana
% f pp<TAB>   → p_____papa
%
% f p_p<TAB>  → p_____papa

I expected the third case to offer «p_____papa» as a completion, by
analogy with the first two cases.

The reason it doesn't is that the argument to the -P option («p_____»)
is matched with the command-line word («pa») and their common prefix is
discarded from the command-line word before completion proceeds.  For
«pa<TAB>» the common prefix is «p», leaving «a» as the command-line
word, so completion looks for matches that start with «p_____a»; while
for «ma<TAB>» the common prefix is «» (empty string) so completion looks
for matches that start with «p_____ma».

This patch makes skipping the -P prefix an all-or-nothing affair: the -P
prefix will only be skipped if it the longest common prefix it and
${PREFIX} have is equal to the -P prefix.

diff --git a/Src/Zle/compcore.c b/Src/Zle/compcore.c
index 2f9fb33..05d2706 100644
--- a/Src/Zle/compcore.c
+++ b/Src/Zle/compcore.c
@@ -2029,7 +2029,7 @@ addmatches(Cadata dat, char **argv)
     char **aign = NULL, **dparr = NULL, *oaq = autoq, *oppre = dat->ppre;
     char *oqp = qipre, *oqs = qisuf, qc, **disp = NULL, *ibuf = NULL;
     char **arrays = NULL;
-    int lpl, lsl, pl, sl, bcp = 0, bcs = 0, bpadd = 0, bsadd = 0;
+    int lpl, lsl, sl, bcp = 0, bcs = 0, bpadd = 0, bsadd = 0;
     int ppl = 0, psl = 0, ilen = 0;
     int llpl = 0, llsl = 0, nm = mnum, gflags = 0, ohp = haspattern;
     int isexact, doadd, ois = instring, oib = inbackt;
@@ -2193,9 +2193,12 @@ addmatches(Cadata dat, char **argv)
 
 	    /* Test if there is an existing -P prefix. */
 	    if (dat->pre && *dat->pre) {
-		pl = pfxlen(dat->pre, lpre);
-		llpl -= pl;
-		lpre += pl;
+		int prefix_length = pfxlen(dat->pre, lpre);
+		if (dat->pre[prefix_length] == '\0') {
+		    /* $compadd_args[-P] is a prefix of ${PREFIX}. */
+		    llpl -= prefix_length;
+		    lpre += prefix_length;
+		}
 	    }
 	}
 	/* Now duplicate the strings we have from the command line. */

(Here lpre is ${PREFIX} and llpl is its strlen().)

Thoughts?

Cheers,

Daniel



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