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

[PATCH] handle options of _arguments correctly



Among the options of _arguments, -S, -A and -M are not handled
by the while loop at lines 295-306 of _arguments. This causes
the following problem:

% compdef '_arguments -S -C : -a' foo
% foo -<TAB>

this offers -C in addition to -a.
The reason is that, in _arguments, since -S is not handled by the while
loop, both -S and -C remain in "$@" and passed to

   comparguments -i "$autod" "$singopt[@]" "$@"   (line 321)

Then "comparguments -i" interprets -S as an option to _arguments,
but treats -C as an optspec (i.e., as an option of foo). Moreover,
$usecc is not set to "yes" and -C does not have the expeced effect.
So we need to handle -S -A and -M in the while loop and move them
to $singopt (so that $usecc is set to yes and -C is removed from "$@").

But even with this change, there is another problem:

% bar() { echo '\t-a\n\t-s' }
% compdef '_arguments --' bar
% bar -<TAB>

this correctly offers -a and -s. But

% unset _args_cache_bar              # or restart another zsh
% compdef '_arguments -s --' bar     # or '_arguments -s : --'
% bar -<TAB>

now -s is not offered.
This is because, at line 18 of _arguments, $tmpargv is set to '-s',
which is used at line 144 to remove -s from $lopts; i.e., -s is
considered as an optspec, not an option to _arguments itself.
A simple fix would be to move the while loop (lines 295-306) to the top
of _arguments.

This leads to the following patch. Are there any unexpected side
effects of this?


diff --git a/Completion/Base/Utility/_arguments b/Completion/Base/Utility/_arguments
index 1f35e8d..551587b 100644
--- a/Completion/Base/Utility/_arguments
+++ b/Completion/Base/Utility/_arguments
@@ -8,6 +8,25 @@ local oldcontext="$curcontext" hasopts rawret optarg singopt alwopt
 local setnormarg start rest
 local -a match mbegin mend
 
+subopts=()
+singopt=()
+while [[ "$1" = -([AMO]*|[CRSWnsw]) ]]; do
+  case "$1" in
+  -C)  usecc=yes; shift ;;
+  -O)  subopts=( "${(@P)2}" ); shift 2 ;;
+  -O*) subopts=( "${(@P)${1[3,-1]}}" ); shift ;;
+  -R)  rawret=yes; shift;;
+  -n)  setnormarg=yes; NORMARG=-1; shift;;
+  -w)  optarg=yes; shift;;
+  -W)  alwopt=arg; shift;;
+  -[Ss])  singopt+=( $1 ); shift;;
+  -[AM])  singopt+=( $1 $2 ); shift 2 ;;
+  -[AM]*) singopt+=( $1 ); shift ;;
+  esac
+done
+
+[[ "$PREFIX" = [-+] ]] && alwopt=arg
+
 long=$argv[(I)--]
 if (( long )); then
   local name tmp tmpargv
@@ -290,23 +309,6 @@ if (( long )); then
   set -- "$tmpargv[@]" "${(@P)name}"
 fi
 
-subopts=()
-singopt=()
-while [[ "$1" = -(O*|[CRWnsw]) ]]; do
-  case "$1" in
-  -C)  usecc=yes; shift ;;
-  -O)  subopts=( "${(@P)2}" ); shift 2 ;;
-  -O*) subopts=( "${(@P)${1[3,-1]}}" ); shift ;;
-  -R)  rawret=yes; shift;;
-  -n)  setnormarg=yes; NORMARG=-1; shift;;
-  -w)  optarg=yes; shift;;
-  -s)  singopt=(-s); shift;;
-  -W)  alwopt=arg; shift;;
-  esac
-done
-
-[[ "$PREFIX" = [-+] ]] && alwopt=arg
-
 zstyle -s ":completion:${curcontext}:options" auto-description autod
 
 if (( $# )) && comparguments -i "$autod" "$singopt[@]" "$@"; then





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