Subject: [PATCH] completion: _arguments: behave properly when description contains '='
From: m0viefreak <[email-masked]>
To: [email-masked]
Cc: m0viefreak <[email-masked]>
Date: Sun, 13 Mar 2016 23:46:46 +0100
List-Id: Zsh Workers List <zsh-workers.zsh.org>

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