zsh-workers
 help / color / mirror / code / Atom feed
From: "Jun T." <takimoto-j@kba.biglobe.ne.jp>
To: zsh-workers@zsh.org
Subject: [PATCH] handle options of _arguments correctly
Date: Tue, 29 Sep 2015 01:55:22 +0900	[thread overview]
Message-ID: <DBF4507F-647B-4DE3-BCD9-CE194BA1BA41@kba.biglobe.ne.jp> (raw)

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




             reply	other threads:[~2015-09-28 17:49 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-28 16:55 Jun T. [this message]
2015-09-28 23:48 ` Bart Schaefer
2015-09-29 16:12   ` Jun T.
2015-09-29 20:41     ` Bart Schaefer

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=DBF4507F-647B-4DE3-BCD9-CE194BA1BA41@kba.biglobe.ne.jp \
    --to=takimoto-j@kba.biglobe.ne.jp \
    --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).