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

local subopts opt usecc

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

if compvalues -i "$@"; then

  local noargs args opts descr action expl sep subc
  local oldcontext="$curcontext"

  if ! compvalues -D descr action; then

    _tags values || return 1

    curcontext="${oldcontext}:values"

    compvalues -V noargs args opts

    if [[ "$PREFIX" = *\=* ]]; then
      local name

      name="${PREFIX%%\=*}"
      if compvalues -L "$name" descr action; then
        IPREFIX="${IPREFIX}${name}="
        PREFIX="${PREFIX#*\=}"
      else
        local prefix suffix

	prefix="${PREFIX#*\=}"
	suffix="$SUFFIX"
	PREFIX="$name"
	SUFFIX=''
	args=( "$args[@]" "$opts[@]" )
	compadd -M 'r:|[_-]=* r:|=*' -D args - "${(@)args[@]%%:*}"

	[[ $#args -ne 1 ]] && return 1

        PREFIX="$prefix"
	SUFFIX="$suffix"
        IPREFIX="${IPREFIX}${args[1]%%:*}="
	compvalues -L "${args[1]%%:*}" descr action subc
	curcontext="${oldcontext}:$subc"
      fi
    else
      compvalues -d descr
      if [[ ${#noargs}+${#args}+${#opts} -ne 1 ]] && compvalues -s sep; then
        sep=( "-qQS" "$sep" )
      else
        sep=()
      fi

      _describe "$descr" \
        noargs "$sep[@]" -M 'r:|[_-]=* r:|=*' -- \
        args -S= -M 'r:|[_-]=* r:|=*' -- \
        opts -qS= -M 'r:|[_-]=* r:|=*'

      curcontext="$oldcontext"

      return
    fi
  else
    compvalues -C subc
    curcontext="${oldcontext}:$subc"
  fi

  if ! _tags arguments; then
    curcontext="$oldcontext"
    return 1
  fi

  _description arguments expl "$descr"

  # We add the separator character as a autoremovable suffix unless
  # we have only one possible value left.

  [[ ${#snames}+${#names}+${#onames} -ne 1 ]] && compvalues -s sep &&
      expl=( "-qS$sep" "$expl[@]" )

  if [[ "$action" = -\>* ]]; then
    compvalues -v val_args
    state="${${action[3,-1]##[ 	]#}%%[ 	]#}"
    if [[ -n "$usecc" ]]; then
      curcontext="$subc"
    else
      context="$subc"
    fi
    compstate[restore]=''
    return 1
  else
    typeset -A val_args

    compvalues -v val_args

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

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

      _message "$descr"
      return 1

    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.

      ${(e)=~action}
    else

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

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

  curcontext="$oldcontext"

  [[ nm -ne "$compstate[nmatches]" ]]
else
  curcontext="$oldcontext"

  return 1;
fi