about summary refs log tree commit diff
path: root/Completion/Base/_describe
blob: 2bb32ad357761ade8b36912cfac2b0e8b1d7d6f5 (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

# This can be used to add options or values with descriptions as matches.

setopt localoptions extendedglob

local isopt cmd opt expl tmps tmpd tmpmd tmpms ret=1 showd _nm hide

cmd="$words[1]"

# Get the options.

while getopts 'oc:' opt; do
  if [[ "$opt" = o ]]; then
    isopt=yes
  else
    cmd="$OPTARG"
  fi
done
shift OPTIND-1

# Do the tests. `showd' is set if the descriptions should be shown.

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

  # We take the value to test the number of matches from a non-local
  # parameter `nm' if that exists and contains only digits. It's a hack.

  if [[ "$nm" = [0-9]## ]]; then
    _nm="$nm"
  else
    _nm=0
  fi
  [[ -n "$compconfig[option_prefix]" &&
     "$compconfig[option_prefix]" != *\!${cmd}* &&
     "$PREFIX" != [-+]* &&
     ( "$compconfig[option_prefix]" = *nodefault* ||
       _nm -ne compstate[nmatches] ) ]] && return 1

  [[ -n "$compconfig[describe_options]" &&
     "$compconfig[describe_options]" != *\!${cmd}* ]] && showd=yes
else
  [[ -n "$compconfig[describe_values]" &&
     "$compconfig[describe_values]" != *\!${cmd}* ]] && showd=yes
fi

_description expl "$1"
shift

if [[ -n "$showd" ]]; then
  compdescribe -I ' -- ' "$@"
else
  compdescribe -i "$@"
fi

[[ -n "$isopt" && "$compconfig[option_prefix]" = hide* ]] && hide=yes

while compdescribe -g args tmpd tmpmd tmps tmpms; do

  # See if we should remove the option prefix characters.

  if [[ -n "$hide" ]]; then
    if [[ "$PREFIX" = --* ]]; then
      tmpd=( "${(@)tmpd#--}" )
      tmps=( "${(@)tmps#--}" )
    elif [[ "$PREFIX" = [-+]* ]]; then
      tmpd=( "${(@)tmpd#[-+]}" )
      tmps=( "${(@)tmps#[-+]}" )
    fi
  fi
  compadd "$args[@]" "$expl[@]" -ld tmpd - "$tmpmd[@]" && ret=0
  compadd "$args[@]" "$expl[@]" -d tmps - "$tmpms[@]" && ret=0
done

return ret