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

Re: [PATCH] handle options of _arguments correctly



2015/09/29 08:48, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:

> I don't see anything immediate (though this renders the name "singsub"
> for the array somewhat nonsensical).

Do you mean 'singopt'? I guess it means 'single-letter option'.
It may not be a good name because all the options of _arugments are
single-letter. It only saves the options -s -S -A -M which need be
passed to 'comparguments -i' (line 2499 of Zle/computil.c).

>    _arguments -s -w -q -s --
> and
>    _arguments -s -w : -q -s --
> 
> are equivalent becaue of the -q (even though I'd hope no one wrote the
> former in an actual completion function).  Which I think is what your
> patch accomplishes

Yes, but it was not sufficient.

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

this only offers -a. '_arguments : --' will offer both -S and -a.

When 'comparguments -i' is called, $singopt is empty and "$@" is "-S" "-a".
But comparguments considers the first -S as an options to _arguments
(line 1203 and below in computil.c).

I think we need explicitly pass ':' to comparguments.

The following is a patch against the current master and includes my previous
one. I also dropped the "if [[ long -eq 1 ]]; then ...", because this always
evaluates to false (it should have been $long, but anyway $argv[1,0] expands
to null string and long==1 need not be treated specially).


diff --git a/Completion/Base/Utility/_arguments b/Completion/Base/Utility/_arguments
index 1f35e8d..95ef621 100644
--- a/Completion/Base/Utility/_arguments
+++ b/Completion/Base/Utility/_arguments
@@ -8,15 +8,33 @@ 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
+
+[[ $1 == ':' ]] && shift
+singopt+=( ':' )  # always end with ':' to indicate the end of options
+
+[[ "$PREFIX" = [-+] ]] && alwopt=arg
+
 long=$argv[(I)--]
 if (( long )); then
   local name tmp tmpargv
 
-  if [[ long -eq 1 ]]; then
-    tmpargv=()
-  else
-    tmpargv=( "${(@)argv[1,long-1]}" )
-  fi
+  tmpargv=( "${(@)argv[1,long-1]}" )  # optspec's before --, if any
 
   name=${~words[1]}
   [[ "$name" = [^/]*/* ]] && name="$PWD/$name"
@@ -290,23 +308,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