Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm
Precedence: bulk
X-No-Archive: yes
List-Id: Zsh Workers List <zsh-workers.zsh.org>
List-Post: <mailto:zsh-workers@zsh.org>
List-Help: <mailto:zsh-workers-help@zsh.org>
X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au
X-Spam-Level: 
X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM,
	T_DKIM_INVALID autolearn=ham autolearn_force=no version=3.4.1
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=googlemail.com; s=20120113;
        h=from:to:cc:subject:date:message-id;
        bh=jrd4MVsF00qUyNWonnbXHyf1vPRJ9Vd4OF3kGphnZ24=;
        b=iMWI7B6IBxFP6oQF7U9vtogwlGGLQGBvynN0xGit+FLVewkugMhOYKS1ppc6WkNQYQ
         lUFhxkyqE7igHQ9MLxu1Wh8NgE4bmFai5ijmK6x0++v8or7E6LIERABHYvNUYm4LhDy8
         ZOib78wePfAQ3xHo4o/fLd00gMpT4S8SMnqYGNrt3070NQwrq5a9KuRfo8jkwyf9Rk5k
         WtEYGzc9Im5qplSkRnGkOplMvYq5YMvMW0NxBKLd/itZDbYXwsqiDjbZHohmcVUOdEqJ
         tuHLVgNHLmuALRMb51K9/AU2QQEa09yAo+t/pGEX+zZYssBRe8vPTdAqoO+LBi8E3Pg7
         HBIA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:from:to:cc:subject:date:message-id;
        bh=jrd4MVsF00qUyNWonnbXHyf1vPRJ9Vd4OF3kGphnZ24=;
        b=iXN941Pt+R8SIuYDUrLOjqULXjPes1hGE6VVmzwHYgJq8R90efMUalTwK4E1z31iBD
         z7v6bANf7xpYtQCxdKeFtxfvDyeQks4vmqYES3o+6FTVJC958szRZONgSesKWa309y3/
         6EKPuAvKeiBkG0inyx+oJ/ztvBx8yPPz5vEJzWGybqTNQDn/sSn8CI2JKC4JMJN5MS61
         rrV5WFCy9/MiruTLYKbIK21DnzJU75P+iRbzqR++c1RFWhdFTSy96q49PFScB0+ySA8X
         EX38JRtOuzXKSp7vnb5Fu2ymEkozhJQ6FIZRNn+L9wrATql69VhW1G7UTUvb8hTrqVvr
         wW4w==
X-Gm-Message-State: AD7BkJKG8l39YXAKY0RhVU0rkruoIRqVqgzB7wCKKlTKup4zQPIzz5+HZU72vArReiG3Eg==
X-Received: by 10.194.90.137 with SMTP id bw9mr20873747wjb.120.1457909212156;
        Sun, 13 Mar 2016 15:46:52 -0700 (PDT)
From: m0viefreak <m0viefreak.cm@googlemail.com>
To: zsh-workers@zsh.org
Cc: m0viefreak <m0viefreak.cm@googlemail.com>
Subject: [PATCH] completion: _arguments: behave properly when description contains '='
Date: Sun, 13 Mar 2016 23:46:46 +0100
Message-Id: <1457909206-9452-1-git-send-email-m0viefreak.cm@googlemail.com>
X-Mailer: git-send-email 2.5.0.234.gefc8a62
X-Seq: zsh-workers 38153

In some cases wrong options were generated. Example:
command --help output:
  --foo           use with --bar=baz

In this case it would wrongly interpret the option as taking arguments:
  --foo=[use with --bar=baz]:--foo

After parsing the --help output, zsh internally stores that option as
  --foo:some option (useful with --bar=baz)

Later it filters the options using the following:
  tmp=("${(@M)lopts:##$~pattern(|:*)}")

The (|:*) part is supposed to be limiting the matching to the part
before the ':'. This does however not work at all.
It will simply use the empty variant and match anyways.

Fix:
- Instead of (|:*) only use :* so that it can't fall back to the
  empty variant.
- For that to work a : must be added even if the option has no
  description.
- In this case, remove the appended : after the filtering.
- When checking for = and [= arguments, change the pattern
  #*\[\=*}" to #[^:]##\[\=*}"
  to make sure the matching occurs in front of the :

Now the completion above correctly creates
  --foo[use with --bar=baz]
---
 Completion/Base/Utility/_arguments | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/Completion/Base/Utility/_arguments b/Completion/Base/Utility/_arguments
index 687c0c4..82c9696 100644
--- a/Completion/Base/Utility/_arguments
+++ b/Completion/Base/Utility/_arguments
@@ -105,7 +105,10 @@ if (( long )); then
 	 continue
        else
 	 # Still no comment, add the previous options anyway.
-	 lopts+=("${tmp[@]}")
+         # Add a ':' after the option anyways, to make the the matching of
+         # the options lateron work as intended.
+         # It will be removed again later.
+	 lopts+=("${^tmp[@]}":)
 	 tmp=()
        fi
      fi
@@ -147,7 +150,7 @@ if (( long )); then
    done
    # Tidy up any remaining uncommented options.
    if (( ${#tmp} )); then
-     lopts+=("${tmp[@]}")
+     lopts+=("${^tmp[@]}":)
    fi
 
     # Remove options also described by user-defined specs.
@@ -220,19 +223,22 @@ if (( long )); then
 
       # Ignore :descriptions at the ends of lopts for matching this;
       # they aren't in the patterns.
-      tmp=("${(@M)lopts:##$~pattern(|:*)}")
-      lopts=("${(@)lopts:##$~pattern(|:*)}")
+      tmp=("${(@M)lopts:##$~pattern:*}")
+      lopts=("${(@)lopts:##$~pattern:*}")
 
       (( $#tmp )) || continue
 
       opt=''
 
+      # Clean suffix ':' added earlier
+      tmp=("${(@)tmp%:}")
+
       # If there are option strings with a `[=', we take these to get an
       # optional argument.
 
-      tmpo=("${(@M)tmp:#*\[\=*}")
+      tmpo=("${(@M)tmp:#[^:]##\[\=*}")
       if (( $#tmpo )); then
-        tmp=("${(@)tmp:#*\[\=*}")
+        tmp=("${(@)tmp:#[^:]##\[\=*}")
 
 	for opt in "$tmpo[@]"; do
 	  # Look for --option:description and turn it into
@@ -263,9 +269,9 @@ if (( long )); then
       # Basically the same as the foregoing.
       # TODO: could they be combined?
 
-      tmpo=("${(@M)tmp:#*\=*}")
+      tmpo=("${(@M)tmp:#[^:]##\=*}")
       if (( $#tmpo )); then
-        tmp=("${(@)tmp:#*\=*}")
+        tmp=("${(@)tmp:#[^:]##\=*}")
 
 	for opt in "$tmpo[@]"; do
 	  if [[ $opt = (#b)(*):([^:]#) ]]; then
-- 
2.5.0.234.gefc8a62

