about summary refs log tree commit diff
path: root/Completion/Base/_arguments
blob: 5170acb84756505b5a179d2284ffd0336ed43cae (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
#autoload

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

local long args rest ws cur nth def nm expl descr action opt arg tmp

# Associative arrays used to collect information about the options.

typeset -A opts mopts dopts dmopts odopts odmopts

# See if we support long options, too.

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

# Now parse the arguments...

args=()
nth=1
while (( $# )); do

  # This describes a one-shot option.

  if [[ "$1" = [-+]* ]]; then
    if [[ "$1" = *:* ]]; then

      # If the option name ends in a `-', the first argument comes
      # directly after the option, if it ends in a `+', the first
      # argument *may* come directly after the option, otherwise it
      # is in the next word.

      if [[ "$1" = [^:]##-:* ]]; then
        dopts[${${1%%:*}[1,-2]}]="${1#*:}"
      elif [[ "$1" = [^:]##+:* ]]; then
        odopts[${${1%%:*}[1,-2]}]="${1#*:}"
      else
        opts[${1%%:*}]="${1#*:}"
      fi
    else
      opts[$1]=''
    fi
  elif [[ "$1" = \*[-+]* ]]; then

    # The same for options that may appear more than once.

    if [[ "$1" = *:* ]]; then
      if [[ "$1" = [^:]##-:* ]]; then
        dmopts[${${1[2,-1]%%:*}[1,-2]}]="${1#*:}"
      elif [[ "$1" = [^:]##+:* ]]; then
        odmopts[${${1[2,-1]%%:*}[1,-2]}]="${1#*:}"
      else
        mopts[${1[2,-1]%%:*}]="${1#*:}"
      fi
    else
      mopts[${1[2,-1]}]=''
    fi
  elif [[ "$1" = \*:* ]]; then

    # This is `*:...', describing `all other arguments'.

    rest="${1[3,-1]}"
  elif [[ "$1" = :* ]]; then

    # This is `:...', describing `the next argument'.

    args[nth++]="${1#*:}"
  else

    # And this is `n:...', describing the `n'th argument.

    args[${1%%:*}]="${1#*:}"
    nth=$(( ${1%%:*} + 1 ))
  fi
  shift
done

if [[ $#long -ne 0 && "$PREFIX" = --* ]]; then

   # If the current words starts with `--' and we should use long
   # options, just call...

  _long_options "$long[@]"
else

  # Otherwise parse the command line...

  ws=( "${(@)words[2,-1]}" )
  cur=$(( CURRENT-2 ))
  nth=1

  # ...until the current word is reached.

  while [[ cur -gt 0 ]]; do

    # `def' holds the description for the option we are currently after.
    # Check if the next argument for the option is optional.

    if [[ "$def" = :* ]]; then
      opt=yes
    else
      opt=''
    fi
    arg=''

    # Remove one description/action pair from `def' if that isn't empty.

    if [[ -n "$def" ]]; then
      if [[ "$def" = ?*:*:* ]]; then
        def="${def#?*:*:}"
      else
        def=''
      fi
    else

      # If it is empty, and the word starts with `--' and we should
      # complete long options, just ignore this word, otherwise make sure
      # we test for options below and handle normal arguments.

      if [[ $#long -eq 0 || "$ws[1]" != --* ]]; then
        opt=yes
	arg=yes
      else
        def=''
      fi
    fi

    if [[ -n "$opt" ]]; then

      # `opt' was set above if we have to test if the word is an option.
      # We first test for the simple options -- those without arguments or
      # those whose arguments have to be given as separate words.

      if (( $+opts[$ws[1]] )); then

        # Options that may only be given once are removed from the
        # associative array so that we are not offered them again.

        def="$opts[$ws[1]]"
        unset "opts[$ws[1]]"
      elif (( $+mopts[$ws[1]] )); then
        def="$mopts[$ws[1]]"
      else

        # If the word is none of the simple options, test for those
        # whose first argument has to or may come directly after the
        # option. This is done in four loops looking very much alike.

        if (( $#dopts )); then

	  # First we get the option names.

	  tmp=( "${(@k)dopts}" )

	  # Then we loop over them and see if the current word begins
	  # with one of the option names.

	  while (( $#tmp )); do
	    [[ "$ws[1]" = ${tmp[1]}* ]] && break
	    shift 1 tmp
	  done

	  if (( $#tmp )); then

	    # It does. So use the description for it, but only from
	    # the second argument on, because we are searching the
	    # description for the next command line argument.

	    opt=''
	    def="$dopts[$tmp[1]]"
	    unset "dopts[$tmp[1]]"
	    if [[ "$def" = ?*:*:* ]]; then
              def="${def#?*:*:}"
            else
              def=''
	    fi
          fi
        fi
	if [[ -n "$opt" && $#dmopts -ne 0 ]]; then
	  tmp=( "${(@k)dmopts}" )
	  while (( $#tmp )); do
	    [[ "$ws[1]" = ${tmp[1]}* ]] && break
	    shift 1 tmp
	  done

	  if (( $#tmp )); then
	    opt=''
	    def="$dmopts[$tmp[1]]"
	    if [[ "$def" = ?*:*:* ]]; then
              def="${def#?*:*:}"
            else
              def=''
            fi
          fi
	fi
        if [[ -n "$opt" && $#odopts -ne 0 ]]; then
	  tmp=( "${(@k)odopts}" )
	  while (( $#tmp )); do
	    [[ "$ws[1]" = ${tmp[1]}* ]] && break
	    shift 1 tmp
	  done

	  if (( $#tmp )); then
	    opt=''
	    def="$odopts[$tmp[1]]"
	    unset "odopts[$tmp[1]]"

	    # For options whose first argument *may* come after the
	    # option, we skip over the first description only if there
	    # is something after the option name on the line.

	    if [[ "$ws[1]" != "$tmp[1]" ]]; then
	      if [[ "$def" = ?*:*:* ]]; then
                def="${def#?*:*:}"
              else
                def=''
              fi
	    fi
          fi
        fi
	if [[ -n "$opt" && $#odmopts -ne 0 ]]; then
	  tmp=( "${(@k)odmopts}" )
	  while (( $#tmp )); do
	    [[ "$ws[1]" = ${tmp[1]}* ]] && break
	    shift 1 tmp
	  done

	  if (( $#tmp )); then
	    opt=''
	    def="$odmopts[$tmp[1]]"
	    if [[ "$ws[1]" != "$tmp[1]" ]]; then
	      if [[ "$def" = ?*:*:* ]]; then
                def="${def#?*:*:}"
              else
                def=''
              fi
            fi
          fi
	fi

	# If we didn't find a matching option description and we were
	# told to use normal argument descriptions, just increase
	# our counter `nth'.

        if [[ -n "$opt" && -n "$arg" ]]; then
          def=''
	  (( nth++ ))
        fi
      fi
    fi

    shift 1 ws
    (( cur-- ))
  done

  # Now generate the matches.

  nm="$compstate[nmatches]"

  if [[ -z "$def" || "$def" = :* ]]; then

    # We either don't have a description for an argument of an option
    # or we have a description for a optional argument.

    if [[ -z "$def" ]]; then

      # If we have none at all, use the one for this argument position.

      def="$args[nth]"
      [[ -z "$def" ]] && def="$rest"
    fi

    # In any case, we have to complete option names here, but we may
    # be in a string that starts with an option names and continues with
    # the first argument, test that (again, four loops).

    opt=yes
    if (( $#dopts )); then

      # Get the option names.

      tmp=( "${(@k)dopts}" )
      while (( $#tmp )); do
        if compset -P "$tmp[1]"; then

	  # The current string starts with the option name, so ignore
	  # that and complete the rest of the string.

	  def="$dopts[$tmp[1]]"
	  opt=''
	  break
        fi
	shift 1 tmp
      done
    fi
    if [[ -n "$opt" && $#dmopts -ne 0 ]]; then
      tmp=( "${(@k)dmopts}" )
      while (( $#tmp )); do
        if compset -P "$tmp[1]"; then
	  def="$dmopts[$tmp[1]]"
	  opt=''
	  break
        fi
	shift 1 tmp
      done
    fi
    if [[ -n "$opt" && $#odopts -ne 0 ]]; then
      tmp=( "${(@k)odopts}" )
      while (( $#tmp )); do
        if compset -P "$tmp[1]"; then
	  def="$odopts[$tmp[1]]"
	  opt=''
	  break
        fi
	shift 1 tmp
      done
    fi
    if [[ -n "$opt" && $#odmopts -ne 0 ]]; then
      tmp=( "${(@k)odmopts}" )
      while (( $#tmp )); do
        if compset -P "$tmp[1]"; then
	  def="$odmopts[$tmp[1]]"
	  opt=''
	  break
        fi
	shift 1 tmp
      done
    fi
    if [[ -n "$opt" ]]; then

      # We aren't in an argument directly after a option name, so
      # all option names are possible matches.

      _description expl option
      compadd "$expl[@]" - "${(@k)opts}" "${(@k)mopts}" \
                           "${(@k)dopts}" "${(@k)dmopts}" \
			   "${(@k)odopts}" "${(@k)odmopts}"
    fi
  fi

  # Now add the matches from the description, if any.

  if [[ -n "$def" ]]; then

    # Ignore the leading colon describing optional arguments.

    [[ "$def" = :* ]] && def="$def[2,-1]"

    # Get the description and the action.

    descr="${def%%:*}"
    action="${${def#*:}%%:*}"

    _description expl "$descr"

    if [[ -z "$action" ]]; then

      # An empty action means that we should just display a message.
      _message "$descr"
      return 1
    elif [[ "$action[1]" = \( ]]; then

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

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

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

      $=action
    else

      # Otherwise we call it with the description-arguments built above.

      action=( $=action )
      "$action[1]" "$expl[@]" "${(@)action[2,-1]}"
    fi
  fi

  # Set the return value.

  [[  nm -ne "$compstate[nmatches]" ]]
fi