From aa99b19c088324b17c0c5e1caee4fc3b5777deab Mon Sep 17 00:00:00 2001 From: Sven Wischnowsky Date: Thu, 26 Apr 2001 12:13:36 +0000 Subject: rename -W option to -w, add new -W option which makes _arguments complete options even after options that get their argument in the same word; new _guard function for conditionally displaying messages (14105) --- ChangeLog | 9 +++++++ Completion/Base/Utility/_arguments | 34 ++++++++++++++++----------- Completion/Base/Utility/_guard | 17 ++++++++++++++ Completion/X/Utility/_x_arguments | 2 +- Completion/X/Utility/_xt_arguments | 2 +- Doc/Zsh/compsys.yo | 48 ++++++++++++++++++++++++++++++++++++-- Src/Zle/computil.c | 7 +++++- 7 files changed, 101 insertions(+), 18 deletions(-) create mode 100644 Completion/Base/Utility/_guard diff --git a/ChangeLog b/ChangeLog index 590907a13..bbb19d4d4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,14 @@ 2001-04-26 Sven Wischnowsky + * 14105: Completion/Base/Utility/_arguments, + Completion/Base/Utility/_guard, + Completion/X/Utility/_x_arguments, + Completion/X/Utility/_xt_arguments, Doc/Zsh/compsys.yo, + Src/Zle/computil.c: rename -W option to -w, add new -W option + which makes _arguments complete options even after options that + get their argument in the same word; new _guard function for + conditionally displaying messages + * 14092: Completion/Base/Completer/_expand: make keep-prefix keep everything up to the first component with a parameter expansion, not only if that is at the beginning of the string diff --git a/Completion/Base/Utility/_arguments b/Completion/Base/Utility/_arguments index 1c5411fe3..c68700763 100644 --- a/Completion/Base/Utility/_arguments +++ b/Completion/Base/Utility/_arguments @@ -4,7 +4,7 @@ # descriptions given as arguments to this function. local long cmd="$words[1]" descr mesg subopts opt usecc autod -local oldcontext="$curcontext" hasopts rawret optarg singopt +local oldcontext="$curcontext" hasopts rawret optarg singopt alwopt long=$argv[(I)--] if (( long )); then @@ -174,21 +174,22 @@ fi subopts=() singopt=() -while [[ "$1" = -(O*|[CRWs]) ]]; do +while [[ "$1" = -(O*|[CRWsw]) ]]; 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;; - -W) optarg=yes; shift;; + -w) optarg=yes; shift;; -s) singopt=(-s); shift;; + -W) alwopt=yes; shift;; esac done zstyle -s ":completion:${curcontext}:options" auto-description autod if (( $# )) && comparguments -i "$autod" "$singopt[@]" "$@"; then - local action noargs aret expl local tried + local action noargs aret expl local tried ret=1 local next direct odirect equal single matcher matched ws tmp1 tmp2 tmp3 local opts subc tc prefix suffix descrs actions subcs anum local origpre="$PREFIX" origipre="$IPREFIX" nm="$compstate[nmatches]" @@ -280,7 +281,8 @@ if (( $# )) && comparguments -i "$autod" "$singopt[@]" "$@"; then eval ws\=\( "${action[3,-3]}" \) - _describe -t "$subc" "$descr" ws -M "$matcher" "$subopts[@]" + _describe -t "$subc" "$descr" ws -M "$matcher" "$subopts[@]" || + alwopt=yes tried=yes elif [[ "$action" = \(*\) ]]; then @@ -289,15 +291,17 @@ if (( $# )) && comparguments -i "$autod" "$singopt[@]" "$@"; then eval ws\=\( "${action[2,-2]}" \) - _all_labels "$subc" expl "$descr" compadd "$subopts[@]" -a - ws + _all_labels "$subc" expl "$descr" compadd "$subopts[@]" -a - ws || + alwopt=yes tried=yes elif [[ "$action" = \{*\} ]]; then # A string in braces is evaluated. while _next_label "$subc" expl "$descr"; do - eval "$action[2,-2]" + eval "$action[2,-2]" && ret=0 done + (( ret )) && alwopt=yes tried=yes elif [[ "$action" = \ * ]]; then @@ -305,8 +309,9 @@ if (( $# )) && comparguments -i "$autod" "$singopt[@]" "$@"; then eval "action=( $action )" while _next_label "$subc" expl "$descr"; do - "$action[@]" + "$action[@]" && ret=0 done + (( ret )) && alwopt=yes tried=yes else @@ -314,15 +319,17 @@ if (( $# )) && comparguments -i "$autod" "$singopt[@]" "$@"; then eval "action=( $action )" while _next_label "$subc" expl "$descr"; do - "$action[1]" "$subopts[@]" "$expl[@]" "${(@)action[2,-1]}" + "$action[1]" "$subopts[@]" "$expl[@]" "${(@)action[2,-1]}" && ret=0 done + (( ret )) && alwopt=yes tried=yes fi fi fi done - - if [[ -z "$matched$hasopts" && ( -z "$aret" || "$PREFIX" = "$origpre" ) ]] && + if [[ -z "$hasopts" && + ( -z "$matched" || -n "$alwopt" ) && + ( -z "$aret" || "$PREFIX" = "$origpre" ) ]] && _requested options && { ! zstyle -T ":completion:${curcontext}:options" prefix-needed || [[ "$origpre" = [-+]* || -z "$aret$mesg$tried" ]] } ; then @@ -378,9 +385,10 @@ if (( $# )) && comparguments -i "$autod" "$singopt[@]" "$@"; then PREFIX="$prevpre" IPREFIX="$previpre" fi - [[ -n "$tried" && "$PREFIX" != [-+]* ]] && break + [[ -n "$tried" && "${${alwopt:+$origpre}:-$PREFIX}" != [-+]* ]] && break done - if [[ -n "$opts" && -z "$aret$matched$mesg" && + if [[ -n "$opts" && -z "$aret" && + ( -z "$matched$mesg" || -n "$alwopt" ) && nm -eq compstate[nmatches] ]]; then PREFIX="$origpre" diff --git a/Completion/Base/Utility/_guard b/Completion/Base/Utility/_guard new file mode 100644 index 000000000..4c07fe1b9 --- /dev/null +++ b/Completion/Base/Utility/_guard @@ -0,0 +1,17 @@ +#autoload + +local mesg pat garbage + +mesg=() +zparseopts -K -D -a garbage M: J: V: 1 2 n F: X:=mesg + +[[ "$PREFIX$SUFFIX" != $~1 ]] && return 1 + +if [[ $# -gt 1 || $#mesg -eq 0 ]]; then + shift + _message "$*" +else + _message -r "$mesg[2]" +fi + +[[ -n "$PREFIX$SUFFIX" ]] diff --git a/Completion/X/Utility/_x_arguments b/Completion/X/Utility/_x_arguments index ba6dd2a68..6bda63076 100644 --- a/Completion/X/Utility/_x_arguments +++ b/Completion/X/Utility/_x_arguments @@ -17,7 +17,7 @@ else fi opts=() -while [[ $1 = -(O*|[CRWs]) ]]; do +while [[ $1 = -(O*|[CRWsw]) ]]; do opts=($opts $1) [[ $1 = -R ]] && rawret=yes shift diff --git a/Completion/X/Utility/_xt_arguments b/Completion/X/Utility/_xt_arguments index e17653251..e2efda61c 100644 --- a/Completion/X/Utility/_xt_arguments +++ b/Completion/X/Utility/_xt_arguments @@ -53,7 +53,7 @@ else fi opts=() -while [[ $1 = -(O*|[CRWs]) ]]; do +while [[ $1 = -(O*|[CRWsw]) ]]; do opts=($opts $1) [[ $1 = -R ]] && rawret=yes shift diff --git a/Doc/Zsh/compsys.yo b/Doc/Zsh/compsys.yo index 129cabdb5..96d0d010d 100644 --- a/Doc/Zsh/compsys.yo +++ b/Doc/Zsh/compsys.yo @@ -2930,12 +2930,24 @@ two hyphens (like `tt(-)tt(-prefix)') are still considered to contain only one option name. This allows the use of the `tt(-s)' option to describe single-letter options together with such long option names. -The tt(-s) option may be combined with the option tt(-W) to say that more +The tt(-s) option may be combined with the option tt(-w) to say that more option characters are to be expected even after an option that takes an argument. For example, if a command takes the options `tt(a)' and `tt(b)', where `tt(a)' takes an argument in the next word, tt(_arguments) would normally not complete the other option directly after `tt(-a)', but it would -allow that if given the tt(-W) option. +allow that if given the tt(-w) option. + +Similarly, the option tt(-W) may be given to force completion of options +even after options that get an argument in the same word. For example, +if a command takes the options `tt(a)' and `tt(b)', where `tt(a)' needs +an argument in the same word, directly after the option character, +tt(_arguments) would normally only execute the action for that argument +and not offer other options as possible completions. If given the +tt(-W) option, it will offer other options as possible completions after +executing the action for the argument. Note that, depending on the +action, this may mean that the other options can't really be completed, +but at least they will be listed. For more control, use an utility +function like tt(_guard) in the argument's action. The forms of var(optspec) are: @@ -3499,6 +3511,38 @@ from the called function is stored in it. The return value of tt(_call_function) itself is zero if the function var(name) exists and was called and non-zero otherwise. ) +findex(_guard) +item(tt(_guard) [ var(options) ] var(pattern) [ var(descr) ])( +This function is intended to be used in an action of functions like +tt(_arguments). It returns immediately with a non-zero return value if +the string to be completed does not match the var(pattern). If the +pattern matches, the var(descr) is displayed and the function returns +zero if the word to complete is not empty and non-zero otherwise. + +The var(pattern) may be preceded by those options understood by +tt(compadd) that are passed down from tt(_description), namely tt(-M), +tt(-J), tt(-V), tt(-1), tt(-2), tt(-n), tt(-F) and tt(-X). All of these +options, except tt(-X), will be ignored. If the tt(-X) option appears, +the description following it will be used as the string to display if +the var(pattern) matches, unless the option var(descr) is given to +tt(_guard) itself, which will then take precedence. + +As an example, consider a command taking the options tt(-n) and +tt(-none), where tt(-n) has to be followed by a numeric value in the +same word. By using either of: + +example(_argument '-n-:numeric value:_guard "[0-9]#"' '-none') + +or + +example(_argument '-n-: :_guard "[0-9]#" "numeric value"' '-none') + +tt(_arguments) can be made to both display the message `tt(numeric +value)' and complete options after `tt(-n)'. If the `tt(-n)' is +already followed by one or more digits (matching the pattern given to +tt(_guard)), only the message will be displayed and if the `tt(-n)' is +followed by another character, only options are completed. +) findex(_message) item(tt(_message) [ -r ] var(descr))( The var(descr) is used like the third diff --git a/Src/Zle/computil.c b/Src/Zle/computil.c index e927081f7..29ea5a298 100644 --- a/Src/Zle/computil.c +++ b/Src/Zle/computil.c @@ -1941,7 +1941,12 @@ bin_comparguments(char *nam, char **args, char *ops, int func) case 's': for (; lstate; lstate = lstate->snext) if (lstate->d->single && lstate->singles && - lstate->actopts && lstate->opt) { + lstate->actopts +#if 0 + /* let's try without, for the -W option of _arguments */ + && lstate->opt +#endif + ) { setsparam(args[1], ztrdup((lstate->ddef && lstate->dopt) ? (lstate->dopt->type == CAO_DIRECT ? -- cgit 1.4.1