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

# This function can be used to separately complete parts of strings
# where each part may be one of a set of matches and different parts
# have different sets.
# Arguments are alternatingly arrays and separator strings. Arrays may
# be given by name or literally as words separated by white space in
# parentheses, e.g.:
#
#  _sep_parts '(foo bar)' @ hosts
#
# This will make this function complete the strings in the array
# `friends'. If the string on the line contains a `@', the substring
# after it will be completed from the array `hosts'. Of course more
# arrays may be given, each preceded by another separator string.
#
# This function understands the `-J group', `-V group', and
# `-X explanation' options.
#
# This function does part of the matching itself and calls the functions
# `_match_test' and `_match_pattern' for this.

local str arr sep test testarr tmparr prefix suffixes matchers autosuffix
local matchflags opt group expl nm=$compstate[nmatches]

# Test if we should use this function for the global matcher in use.

_match_test _sep_parts || return 1

# Get the options.

group=()
expl=()
while getopts "J:V:X:" opt; do
  case "$opt" in
  [JV]) group=("-$opt" "$OPTARG");;
  X)    expl=(-X "$OPTARG");;
  esac
done
shift OPTIND-1

# Get the string from the line.

str="$PREFIX$SUFFIX"
[[ $#compstate[pattern_match] -ne 0 ]] || str="$str:q"
prefix=""

# Walk through the arguments to find the longest unambiguous prefix.

while [[ $# -gt 1 ]]; do
  # Get the next array and separator.
  arr="$1"
  sep="$2"

  if [[ "$arr[1]" == '(' ]]; then
    tmparr=( ${=arr[2,-2]} )
    arr=tmparr
  fi
  # Is the separator on the line?
  [[ "$str" != *${sep}* ]] && break

  # Build a pattern matching the possible matches and get all these
  # matches in an array.

  test="${str%%${sep}*}"
  [[ -n "$_comp_correct" && $#test -le _comp_correct ]] && return 1

  matchflags=""
  _match_pattern _sep_parts test matchflags
  [[ -n "$_comp_correct" ]] && matchflags="$matchflags(#a$_comp_correct)"

  test="${matchflags}${test}"
  testarr=( "${(@M)${(@P)arr}:#${~test}*}" )
  testarr=( "${(@)testarr:#}" )

  # If there are no matches we give up. If there is more than one
  # match, this is the part we will complete.
  (( $#testarr )) || return 1
  [[ $#testarr -gt 1 ]] && break

  # Only one match, add it to the prefix and skip over it in `str',
  # continuing with the next array and separator.
  prefix="${prefix}${testarr[1]}${sep}"
  str="${str#*${sep}}"
  shift 2
done

# Get the array to work upon.
arr="$1"
if [[ "$arr[1]" == '(' ]]; then
  tmparr=( ${=arr[2,-2]} )
  arr=tmparr
fi
if [[ $# -le 1 || "$str" != *${2}* ]]; then
  # No more separators, build the matches.

  test="$str"
  [[ -n "$_comp_correct" && $#test -le _comp_correct ]] && return 1

  matchflags=""
  _match_pattern _sep_parts test matchflags
  [[ -n "$_comp_correct" ]] && matchflags="$matchflags(#a$_comp_correct)"

  test="${matchflags}${test}"
  testarr=( "${(@M)${(@P)arr}:#${~test}*}" )
  testarr=( "${(@)testarr:#}" )
fi

[[ $#testarr -eq 0 || ${#testarr[1]} -eq 0 ]] && return 1

# Now we build the suffixes to give to the completion code.
shift
matchers=()
suffixes=("")
autosuffix=()

while [[ $# -gt 0 && "$str" == *${1}* ]]; do
  # Remove anything up to the the suffix.
  str="${str#*${1}}"

  # Again, we get the string from the line up to the next separator
  # and build a pattern from it.
  if [[ $# -gt 2 ]]; then
    test="${str%%${3}*}"
  else
    test="$str"
  fi

  [[ -n "$_comp_correct" && $#test -le _comp_correct ]] && return 1

  matchflags=""
  _match_pattern _sep_parts test matchflags
  [[ -n "$_comp_correct" ]] && matchflags="$matchflags(#a$_comp_correct)"
  test="${matchflags}${test}"

  # We incrementally add suffixes by appending to them the seperators
  # and the strings from the next array that match the pattern we built.

  arr="$2"
  if [[ "$arr[1]" == '(' ]]; then
    tmparr=( ${=arr[2,-2]} )
    arr=tmparr
  fi
  tmparr=( "${(@M)${(@P)arr}:#${~test}*}" )
  tmparr=( "${(@)tmparr:#}" )
  suffixes=("${(@)^suffixes[@]}${1}${(@)^tmparr}")

  # We want the completion code to generate the most specific suffix
  # for us, so we collect matching specifications that allow partial
  # word matching before the separators on the fly.
  matchers=("$matchers[@]" "r:|${1:q}=*")
  shift 2
done

# If we were given at least one more separator we make the completion
# code offer it by appending it as a autoremovable suffix.
(( $# )) && autosuffix=(-qS "$1")

# If we have collected matching specifications, we build an array
# from it that can be used as arguments to `compadd'.
[[ $#matchers -gt 0 ]] && matchers=(-M "$matchers")

# Add the matches for each of the suffixes.
for i in "$suffixes[@]"; do
  compadd -U "$group[@]" "$expl[@]" "$matchers[@]" "$autosuffix[@]" \
          -i "$IPREFIX" -p "$prefix" -s "$i" - "$testarr[@]"
done

# This sets the return value to indicate that we added matches (or not).

[[ nm -ne compstate[nmatches] ]]