zsh-workers
 help / color / mirror / code / Atom feed
From: Daniel Shahaf <d.s@daniel.shahaf.name>
To: zsh-workers@zsh.org
Subject: 'compadd -P' matches $PREFIX greedily
Date: Sat, 17 Sep 2016 06:32:58 +0000	[thread overview]
Message-ID: <20160917063258.GA26826@fujitsu.shahaf.local2> (raw)

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


             reply	other threads:[~2016-09-17  6:33 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-17  6:32 Daniel Shahaf [this message]
2016-09-17 13:50 ` Bart Schaefer
2016-09-18  1:28 ` Oliver Kiddle
2016-09-19  7:00   ` Daniel Shahaf
2016-09-19  8:51     ` [PATCH] _bts: Complete more argument types for 'cache' and 'show' Daniel Shahaf

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=20160917063258.GA26826@fujitsu.shahaf.local2 \
    --to=d.s@daniel.shahaf.name \
    --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).