summary refs log tree commit diff
path: root/Completion/Base/_arguments
blob: ed7bd98d7ebf2e1a15ce8d7edc11a0a97c742367 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#autoload

# Complete the arguments of the current command according to the
# descriptions given as arguments to this function.

local long cmd="$words[1]" descr mesg subopts opt usecc autod
local oldcontext="$curcontext" hasopts multi ismulti

long=$argv[(I)--]
if (( long )); then
  local name tmp tmpargv

  if [[ long -eq 1 ]]; then
    tmpargv=()
  else
    tmpargv=( "${(@)argv[1,long-1]}" )
  fi

  name=${~words[1]}
  [[ "$name" = [^/]*/* ]] && name="$PWD/$name"

  name="_args_cache_${name}"
  name="${name//[^a-zA-Z0-9_]/_}"

  if (( ! ${(P)+name} )); then
    local iopts sopts pattern tmpo cur cache
    typeset -U lopts

    cache=()

    # We have to build a new long-option cache, get the `-i' and
    # `-s' options.

    set -- "${(@)argv[long+1,-1]}"

    iopts=()
    sopts=()
    while [[ "$1" = -[is]* ]]; do
      if [[ "$1" = -??* ]]; then
        tmp="${1[3,-1]}"
        cur=1
      else
        tmp="$2"
	cur=2
      fi
      if [[ "$tmp[1]" = '(' ]]; then
	tmp=( ${=tmp[2,-2]} )
      else
	tmp=( "${(@P)tmp}" )
      fi
      if [[ "$1" = -i* ]]; then
        iopts=( "$iopts[@]" "$tmp[@]" )
      else
        sopts=( "$sopts[@]" "$tmp[@]" )
      fi
      shift cur
    done

    # Now get the long option names by calling the command with `--help'.
    # The parameter expansion trickery first gets the lines as separate
    # array elements. Then we select all lines whose first non-blank
    # character is a hyphen. Since some commands document more than one
    # option per line, separated by commas, we convert commas into
    # newlines and then split the result again at newlines after joining 
    # the old array elements with newlines between them. Then we select
    # those elements that start with two hyphens, remove anything up to
    # those hyphens and anything from the space or tab after the
    # option up to the end.

    lopts=("--${(@)^${(@)${(@)${(@M)${(@ps:\n:j:\n:)${(@)${(@M)${(@f)$(_call options ${~words[1]} --help 2>&1)//\[--/
--}:#[ 	]#-*}//,/
}}:#[ 	]#--*}#*--}%%[]	 ]*}:#}")
    lopts=( "${(@)lopts:#--}" )

    # Now remove all ignored options ...

    while (( $#iopts )); do
      lopts=( ${lopts:#$~iopts[1]} )
      shift iopts
    done

    # ... and add "same" options

    while (( $#sopts )); do
      lopts=( $lopts ${lopts/$~sopts[1]/$sopts[2]} )
      shift 2 sopts
    done

    # Then we walk through the descriptions plus a few builtin ones.

    set -- "$@" '*=FILE*:file:_files' \
           '*=(DIR|PATH)*:directory:_files -/' '*: :'

    while (( $# )); do

      # First, we get the pattern and the action to use and take them
      # from the positional parameters.

      pattern="${${${(M)1#*[^\\]:}[1,-2]}//\\\\:/:}"
      descr="${1#${pattern}}"
      shift

      # We get all options matching the pattern and take them from the
      # list we have built. If no option matches the pattern, we
      # continue with the next.

      tmp=("${(@M)lopts:##$~pattern}")
      lopts=("${(@)lopts:##$~pattern}")

      (( $#tmp )) || continue

      opt=''

      # If there are option strings with a `[=', we take these get an
      # optional argument.

      tmpo=("${(@M)tmp:#*\[\=*}")
      if (( $#tmpo )); then
        tmp=("${(@)tmp:#*\[\=*}")
        tmpo=("${(@)${(@)tmpo%%\=*}//[^a-z0-9-]}")

        if [[ "$descr" = ::* ]]; then
	  cache=( "$cache[@]" "${(@)^tmpo}=${descr}" )
        else
	  cache=( "$cache[@]" "${(@)^tmpo}=:${descr}" )
        fi
      fi

      # Descriptions with `=': mandatory argument.

      tmpo=("${(@M)tmp:#*\=*}")
      if (( $#tmpo )); then
        tmp=("${(@)tmp:#*\=*}")
        tmpo=("${(@)${(@)tmpo%%\=*}//[^a-z0-9-]}")

	cache=( "$cache[@]" "${(@)^tmpo}=${descr}" )
      fi

      # Everything else is just added as a option without arguments.

      if (( $#tmp )); then
        tmp=("${(@)tmp//[^a-zA-Z0-9-]}")
	cache=( "$cache[@]" "$tmp[@]" )
      fi
    done
    eval "${name}=( \"\${(@)cache:# #}\" )"
  fi
  set -- "$tmpargv[@]" "${(@P)name}"
fi

multi=(-i)
subopts=()
while [[ "$1" = -(O*|C|m*) ]]; do
  case "$1" in
  -C) usecc=yes; shift ;;
  -O) subopts=( "${(@P)2}" ); shift 2 ;;
  -O*)  subopts=( "${(@P)1[3,-1]}" ); shift ;;
  -m) ismulti=yes multi=(-I "$2" "$3"); shift 3 ;;
  -m*) ismulti=yes multi=(-I "${1[3,-1]}" "$2"); shift 2 ;;
  esac
done

zstyle -s ":completion:${curcontext}:options" auto-description autod

if (( $# )) && comparguments "$multi[@]" "$autod" "$@"; then
  local action noargs aret expl local
  local next direct odirect equal single match matched ws tmp1 tmp2 tmp3
  local opts subc tc prefix suffix descrs actions subcs
  local origpre="$PREFIX" origipre="$IPREFIX"

  [[ -z "$ismulti" ]] && local nm="$compstate[nmatches]"

  if comparguments -D descrs actions subcs; then
    if comparguments -O next direct odirect equal; then
      opts=yes
      _tags "$subcs[@]" options
    else
      _tags "$subcs[@]"
    fi
  else
    if comparguments -a; then
      noargs='no more arguments'
      had_args=yes
    else
      noargs='no arguments'
    fi
    if ! comparguments -O next direct odirect equal; then
      _message "$noargs"
      return 1
    fi
    opts=yes
    _tags options
  fi

  context=()
  state=()

  while true; do
    while _tags; do
      while (( $#descrs )); do

	action="$actions[1]"
	descr="$descrs[1]"
	subc="$subcs[1]"

        if [[ -n "$matched" ]] || _requested "$subc"; then

          curcontext="${oldcontext%:*}:$subc"

          _description "$subc" expl "$descr"

          if [[ "$action" = \=\ * ]]; then
            action="$action[3,-1]"
            words=( "$subc" "$words[@]" )
	    (( CURRENT++ ))
          fi

          if [[ "$action" = -\>* ]]; then
            comparguments -W line opt_args
            state=( "$state[@]" "${${action[3,-1]##[ 	]#}%%[ 	]#}" )
	    if [[ -n "$usecc" ]]; then
	      curcontext="${oldcontext%:*}:$subc"
	    else
	      context=( "$context[@]" "$subc" )
	    fi
            compstate[restore]=''
            aret=yes
          else
            if [[ -z "$local" ]]; then
              local line
              typeset -A opt_args
              local=yes
            fi

            comparguments -W line opt_args

            if [[ "$action" = \ # ]]; then

              # An empty action means that we should just display a message.

	      _message "$descr"
	      mesg=yes

            elif [[ "$action" = \(\(*\)\) ]]; then

              # ((...)) contains literal strings with descriptions.

              eval ws\=\( "${action[3,-3]}" \)

              _describe -t "$subc" "$descr" ws -M "$match" "$subopts[@]"

            elif [[ "$action" = \(*\) ]]; then

              # Anything inside `(...)' is added directly.

              _all_labels "$subc" expl "$descr" \
                  compadd "$subopts[@]" - ${=action[2,-2]}
            elif [[ "$action" = \{*\} ]]; then

              # A string in braces is evaluated.

              while _next_label "$subc" expl "$descr"; do
                eval "$action[2,-2]"
              done
            elif [[ "$action" = \ * ]]; then

              # If the action starts with a space, we just call it.

	      eval "action=( $action )"
              while _next_label "$subc" expl "$descr"; do
                "$action[@]"
              done
            else

              # Otherwise we call it with the description-arguments.

              eval "action=( $action )"
              while _next_label "$subc" expl "$descr"; do
                "$action[1]" "$subopts[@]" "$expl[@]" "${(@)action[2,-1]}"
	      done
            fi
          fi
        fi
        shift 1 descrs
        shift 1 actions
        shift 1 subcs
      done

      if [[ -z "$matched$hasopts" ]] && _requested options &&
          { ! zstyle -T ":completion:${curcontext}:options" prefix-needed ||
            [[ "$origpre" = [-+]* ||
            ( -z "$aret$mesg" && nm -eq compstate[nmatches] ) ]] } ; then
	local prevpre="$PREFIX" previpre="$IPREFIX"

	hasopts=yes

	PREFIX="$origpre"
	IPREFIX="$origipre"

        comparguments -M match
	
        if comparguments -s single; then

          if [[ "$single" = direct ]]; then
            _all_labels options expl option \
	        compadd -QS '' - "${PREFIX}${SUFFIX}"
          elif [[ "$single" = next ]]; then
            _all_labels options expl option \
	        compadd -Q - "${PREFIX}${SUFFIX}"
          elif [[ "$single" = equal ]]; then
            _all_labels options expl option \
	        compadd -QqS= - "${PREFIX}${SUFFIX}"
          else
	    tmp1=( "$next[@]" "$direct[@]" "$odirect[@]" "$equal[@]" )
	    tmp3=( "${(M@)tmp1:#[-+]?[^:]*}" )
	    tmp1=( "${(M@)tmp1:#[-+]?(|:*)}" )
	    tmp2=( "${PREFIX}${(@M)^${(@)${(@)tmp1%%:*}#[-+]}:#?}" )

	    if [[ -n "$ismulti" ]]; then
	      _ms_opt=yes
	      _ms_soptmid=( "$_ms_soptmid[@]" "$tmp1[@]" )
	      _ms_soptmidadd=( "$_ms_soptmidadd[@]" "$tmp2[@]" )
	      _ms_soptend=( "$_ms_soptend[@]" "$tmp3[@]" )
	    else
              _describe -o option \
                        tmp1 tmp2 -Q -S '' -- \
		        tmp3 -Q
            fi
          fi
          single=yes
        else
          next=( "$next[@]" "$odirect[@]" )
	  if [[ -n "$ismulti" ]]; then
	    _ms_opt=yes
	    _ms_match="$_ms_match $match"
	    _ms_optnext=( "$_ms_optnext[@]" "$next[@]" )
	    _ms_optdirect=( "$_ms_optdirect[@]" "$direct[@]" )
	    _ms_optequal=( "$_ms_optequal[@]" "$equal[@]" )
	  else
            _describe -o option \
                      next -Q -M "$match" -- \
                      direct -QS '' -M "$match" -- \
                      equal -QqS= -M "$match"
          fi
        fi
	PREFIX="$prevpre"
	IPREFIX="$previpre"
      fi
    done
    if [[ -n "$opts" && -z "$aret$matched$mesg" &&
          nm -eq compstate[nmatches] ]]; then

      PREFIX="$origpre"
      IPREFIX="$origipre"

      prefix="${PREFIX#*\=}"
      suffix="$SUFFIX"
      PREFIX="${PREFIX%%\=*}"
      SUFFIX=''
      compadd -M "$match" -D equal - "${(@)equal%%:*}"

      if [[ $#equal -eq 1 ]]; then
        PREFIX="$prefix"
	SUFFIX="$suffix"
	IPREFIX="${IPREFIX}${equal[1]%%:*}="
	matched=yes

	comparguments -L "${equal[1]%%:*}" descrs actions subcs

	_tags "$subcs[@]"

	continue
      fi
    fi
    break
  done

  [[ -z "$aret" || -z "$usecc" ]] && curcontext="$oldcontext"

  [[ -n "$aret" ]] && return 300

  if [[ -n "$noargs" ]]; then
    [[ -z "$ismulti" && nm -eq "$compstate[nmatches]" ]] && _message "$noargs"
  else
    has_args=yes
  fi

  # Set the return value.

  [[ nm -ne "$compstate[nmatches]" ]]
else
  return 1
fi