Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
'compadd -P' matches $PREFIX greedily
- X-seq: zsh-workers 39372
 
- From: Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx>
 
- To: zsh-workers@xxxxxxx
 
- Subject: 'compadd -P' matches $PREFIX greedily
 
- Date: Sat, 17 Sep 2016 06:32:58 +0000
 
- Dkim-signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=	daniel.shahaf.name; h=content-transfer-encoding:content-type	:date:from:message-id:mime-version:subject:to:x-sasl-enc	:x-sasl-enc; s=mesmtp; bh=5ielzk1WiCzImvFXEybNFIRHf3c=; b=OXRu9z	435BQs9MLG9uhCwV27v7950+XsabOmnHf7LHOaXnbwiZFdpAAJcB1UwftBIDkmx7	gxTb55+5Q1SJhYnE8nBGjhW8pOdQX9PFfJycgrH6jmRMKgNgncS5LzM0oFdGG1LW	THQnGni3/Dv6HrzvKLB2gQPdq+pZafVCTmbSY=
 
- Dkim-signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=	messagingengine.com; h=content-transfer-encoding:content-type	:date:from:message-id:mime-version:subject:to:x-sasl-enc	:x-sasl-enc; s=smtpout; bh=5ielzk1WiCzImvFXEybNFIRHf3c=; b=ef324	ip2yK8IK5J3zwmfMZVg7gDjeYCJ0tm/oK4GHHtx+u+SzHznhb5rll8rpo9y4u9n1	6zFkkFhFDDGQ4rf6mq8my283setpDRJa80b1nIWi9bti8cWVJZ+OgBFg7iVQS2Qq	LRK3X/8xBHi4N/ZM6SCaT69KUrIMoNlmjp4+UA=
 
- List-help: <mailto:zsh-workers-help@zsh.org>
 
- List-id: Zsh Workers List <zsh-workers.zsh.org>
 
- List-post: <mailto:zsh-workers@zsh.org>
 
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
 
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