about summary refs log tree commit diff
path: root/Completion/Core/_alternative
blob: 73292cb52fd3097413f79afb8631760156d3185b (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
#autoload

local tags def expl descr action mesgs nm="$compstate[nmatches]" subopts
local opt curcontext="$curcontext"

subopts=()
while getopts 'O:C:' opt; do
  case "$opt" in
  O) subopts=( "${(@P)OPTARG}" ) ;;
  C) curcontext="${curcontext%:*}:$OPTARG" ;;
  esac
done

shift OPTIND-1

[[ "$1" = -(|-) ]] && shift

mesgs=()

_tags "${(@)argv%%:*}"

while _tags; do
  for def; do
    if _requested "${def%%:*}"; then
      descr="${${def#*:}%%:*}"
      action="${def#*:*:}"

      _description "${def%%:*}" expl "$descr"

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

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

        mesgs=( "$mesgs[@]" "$descr")
      elif [[ "$action" = \(\(*\)\) ]]; then
        local ws

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

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

        _describe "$descr" ws -M 'r:|[_-]=* r:|=*' "$subopts[@]"
      elif [[ "$action" = \(*\) ]]; then

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

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

        # A string in braces is evaluated.

        eval "$action[2,-2]"
      elif [[ "$action" = \ * ]]; then

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

        eval "action=( $action )"
        "$action[@]"
      else

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

        eval "action=( $action )"
        "$action[1]" "$subopts[@]" "$expl[@]" "${(@)action[2,-1]}"
      fi
    fi
  done
  [[ nm -ne compstate[nmatches] ]] && return 0
done

for descr in "$mesgs[@]"; do
  _message "$descr"
done

return 1