about summary refs log tree commit diff
path: root/Functions/Completion/__normal
blob: 7750563d11966a9fa45ed55c9d3675f516756ced (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
#helper

local comp cmd1 cmd2 pat val name

# Completing in command position? If not we set up `cmd1' and `cmd2' as
# two strings we have search in the completion definition arrays (e.g.
# a path and the last path name component).

if [[ $CONTEXT == command ]]; then
  comp="$comps[--command--]"
  [[ -z "$comp" ]] || callcomplete comps --command-- "$@" || return 1
  return 0
elif [[ "$COMMAND[1]" == '=' ]]; then
  eval cmd1\=$COMMAND
  cmd2="$COMMAND[2,-1]"
elif [[ "$COMMAND" == */* ]]; then
  cmd1="$COMMAND"
  cmd2="${COMMAND:t}"
else
  cmd1="$COMMAND"
  eval cmd2=$(whence -p $COMMAND)
fi

# See if there are any matching pattern completions.

if (( $#patcomps )); then
  for i in "$patcomps[@]"; do
    pat="${i% *}"
    val="${i#* }"
    if [[ "$cmd1" == $~pat || "$cmd2" == $~pat ]]; then
      callcomplete patcomps "$pat" "$@" || return 1
    fi
  done
fi

# Now look up the two names in the normal completion array.

name="$cmd1"
comp="$comps[$cmd1]"

if [[ -z "$comp" ]]; then
  name="$cmd2"
  comp="$comps[$cmd2]"
fi

# And generate the matches, probably using default completion.

if [[ -z "$comp" ]]; then
  name=--default--
  comp="$comps[--default--]"
fi
[[ -z "$comp" ]] || callcomplete comps "$name" "$@" || return 1

return 0