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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
| | #autoload
# This completer function is intended to be used as the first completer
# function and allows one to say more explicitly when and how the word
# from the line should be expanded than expand-or-complete.
# This function will allow other completer functions to be called if
# the expansions done produce no result or do not change the original
# word from the line.
setopt localoptions nonomatch
[[ _matcher_num -gt 1 ]] && return 1
local -a exp subd tmpa match mbegin mend
local -i continue=0
local word sort expl pref suf=' ' force opt asp tmp opre pre epre
(( $# )) &&
while getopts agsco opt; do
force="$force$opt"
done
if [[ "$funcstack[2]" = _prefix ]]; then
word="$IPREFIX$PREFIX$SUFFIX"
else
word="$IPREFIX$PREFIX$SUFFIX$ISUFFIX"
fi
# Alias expansion:
() {
if { (( CURRENT == 1 )) && zstyle -T ":completion:${curcontext}:" regular } ||
zstyle -t ":completion:${curcontext}:" regular always; then
exp=( $aliases[$word] )
if [[ -n "$exp" ]]; then
# If the first shell word in the expansion is the same as the alias, then
# escape it, so it doesn't get re-expanded on execution.
[[ "${${(z)exp}[1]}" = "$word" ]] &&
exp=( "${(S)exp/#(#b)(*)$word/$match[1]\\$word}" )
return 0
fi
fi
if zstyle -T ":completion:${curcontext}:" global; then
exp=( $galiases[$word] )
if [[ -n "$exp" ]]; then
# Escape all occurences of the global alias in its expansion, so these
# don't get re-expanded on execution.
tmp="$exp"
tmpa=( ${(z)tmp} )
exp=()
while (( ${#tmpa[@]} )); do
exp[1]+="${(M)tmp#*$tmpa[1]}"
tmp="${tmp#*$tmpa[1]}"
[[ "$tmpa[1]" == "$word" ]] &&
exp[1]="${exp/%$word/\\$word}"
shift tmpa
done
exp[1]+="$tmp"
tmp=
return 0
fi
fi
if zstyle -t ":completion:${curcontext}:" disabled; then
exp=( $dis_aliases[$word] )
[[ -n "$exp" ]] &&
return
fi
if zstyle -T ":completion:${curcontext}:" global &&
zstyle -t ":completion:${curcontext}:" disabled; then
exp=( $dis_galiases[$word] )
[[ -n "$exp" ]] &&
return
fi
return 1
} && {
zstyle -T ":completion:${curcontext}:" add-space true yes on 1 &&
tmpa=( -S ' ' )
_description expansions expl expansions "o:$word"
if compadd "$expl[@]" -UQ $tmpa[@] -- $exp; then
! zstyle -t ":completion:${curcontext}:" accept-exact continue
return
fi
}
# If we've been called with `-a`, then don't expand anything else than aliases.
[[ "$force" = *a* ]] &&
return 1
[[ "$word" = *\$(|\{[^\}]#) ||
( "$word" = *\$[a-zA-Z0-9_]## && $+parameters[${word##*\$}] -eq 0 ) ]] &&
return 1
### I'm not sure about the pattern to use in the following test.
# It once was:
# [[ "$word" = (\~*/|\$(|[=~#^+])[a-zA-Z0-9_\[\]]##[^a-zA-Z0-9_\[\]]|\$\{*\}?)[^\$\{\}\(\)\<\>?^*#~]# ]] &&
zstyle -T ":completion:${curcontext}:" suffix &&
[[ "$word" = (\~*/*|*\$(|[=~#^+])[a-zA-Z0-9_\[\]]##[^a-zA-Z0-9_\[\]]|*\$\{*\}?) &&
"${(e)word}" != (#s)(*[^\\]|)[][^*?\(\)\<\>\{\}\|]* ]] &&
return 1
zstyle -s ":completion:${curcontext}:" accept-exact tmp ||
[[ ! -o recexact ]] || tmp=1
if [[ "$tmp" != (yes|true|on|1) ]]; then
{ [[ "$word" = \~(|[-+]) ||
( "$word" = \~[-+][1-9]## && $word[3,-1] -le $#dirstack ) ||
$word = \~\[*\]/* ]] && return 1 }
{ [[ ( "$word" = \~* && ${#userdirs[(I)${word[2,-1]}*]}+${#nameddirs[(I)${word[2,-1]}*]} -gt 1 ) ||
( "$word" = *\$[a-zA-Z0-9_]## &&
${#parameters[(I)${word##*\$}*]} -ne 1 ) ]] && continue=1 }
[[ continue -eq 1 && "$tmp" != continue ]] && return 1
fi
# In exp we will collect the expansions.
exp=("$word")
# First try substitution. That weird thing spanning multiple lines
# changes quoted spaces, tabs, and newlines into spaces and protects
# this function from aborting on parse errors in the expansion.
if [[ "$force" = *s* ]] ||
zstyle -T ":completion:${curcontext}:" substitute; then
### We once used this:
###
### [[ ! -o ignorebraces && "${#${exp}//[^\{]}" = "${#${exp}//[^\}]}" ]] &&
### eval exp\=\( ${${(q)exp}:gs/\\{/\{/:gs/\\}/\}/} \) 2>/dev/null
###
### instead of the following loop to expand braces. But that made
### parameter expressions such as ${foo} be expanded like brace
### expansions, too (and with braceccl set...).
if [[ ! $_comp_caller_options[ignorebraces] == on && "${#${exp}//[^\{]}" = "${#${exp}//[^\}]}" ]]; then
local otmp
tmp=${(q)word}
while [[ $#tmp != $#otmp ]]; do
otmp=$tmp
tmp=${tmp//(#b)\\\$\\\{(([^\{\}]|\\\\{|\\\\})#)([^\\])\\\}/\\$\\\\{${match[1]}${match[3]}\\\\}}
done
eval exp\=\( ${tmp:gs/\\{/\{/:gs/\\}/\}/} \) 2>/dev/null
fi
### There's a bug: spaces resulting from brace expansion are quoted in
### the following expression, too. We don't want that, but I have no
### idea how to fix it.
setopt aliases
eval 'exp=( ${${(e)exp//\\[
]/ }//(#b)([
])/\\$match[1]} )' 2>/dev/null
setopt NO_aliases
else
exp=( ${exp:s/\\\$/\$} )
fi
# If the array is empty, store the original string again.
[[ -z "$exp" ]] && exp=("$word")
subd=("$exp[@]")
# Now try globbing.
# We need to come out of this with consistent quoting, by hook or by crook.
integer done_quote
local orig_exp=$exp
if [[ "$force" = *g* ]] || zstyle -T ":completion:${curcontext}:" glob; then
eval 'exp=( ${~exp//(#b)\\([ \"'"\'"'
])/$match[1]} ); exp=( ${(q)exp} )' 2>/dev/null && (( $#exp )) && done_quote=1
fi
# If the globbing failed, or we didn't try globbing, we'll do
# it again without the "~" so globbing is simply omitted.
if (( ! done_quote )); then
eval 'exp=( ${orig_exp//(#b)\\([ \"'"\'"'
])/$match[1]} ); exp=( ${(q)exp} )' 2>/dev/null
fi
### Don't remember why we once used this instead of the (q) above.
# eval 'exp=( ${~exp} ); exp=( ${exp//(#b)([][()|*?^#~<>\\=])/\\${match[1]}} )' 2>/dev/null
# If we don't have any expansions or only one and that is the same
# as the original string, we let other completers run.
(( $#exp )) || exp=("$subd[@]")
[[ $#exp -eq 1 && "${exp[1]//\\}" = "${word//\\}"(|\(N\)) ]] && return 1
# With subst-globs-only we bail out if there were no glob expansions,
# regardless of any substitutions
{ [[ "$force" = *o* ]] ||
zstyle -t ":completion:${curcontext}:" subst-globs-only } &&
[[ "$subd" = "$exp"(|\(N\)) ]] && return 1
zstyle -s ":completion:${curcontext}:" keep-prefix tmp || tmp=changed
if [[ "$word" = (\~*/*|*\$*/*) && "$tmp" = (yes|true|on|1|changed) ]]; then
if [[ "$word" = *\$* ]]; then
opre="${(M)word##*\$[^/]##/}"
else
opre="${word%%/*}"
fi
eval 'epre=( ${(e)~opre} )' 2> /dev/null
if [[ -n "$epre" && $#epre -eq 1 ]]; then
pre="${(q)epre[1]}"
[[ ( "$tmp" != changed || $#exp -gt 1 ||
"${opre}${exp[1]#${pre}}" != "$word" ) && "${exp[1]}" = $pre* ]] &&
exp=( ${opre}${^exp#${pre}} )
fi
[[ $#exp -eq 1 && "$exp[1]" = "$word" ]] && return 1
fi
# Now add as matches whatever the user requested.
zstyle -s ":completion:${curcontext}:" sort sort
[[ "$sort" = (yes|true|1|on) ]] && exp=( "${(@o)exp}" )
if zstyle -s ":completion:${curcontext}:" add-space tmp; then
if [[ "$tmp" != *subst* || "$word" != *\$* || "$exp[1]" = *\$* ]]; then
[[ "$tmp" = *file* ]] && asp=file
[[ "$tmp" = *(yes|true|1|on|subst)* ]] && asp="yes$asp"
fi
else
asp=file
fi
# If there is only one expansion, add a suitable suffix
if (( $#exp == 1 )); then
if [[ -d ${exp[1]/${opre}/${pre}} && "$exp[1]" != */ ]]; then
suf=/
elif [[ "$asp" = yes* ||
( "$asp" = *file && -f "${exp[1]/${opre}/${pre}}" ) ]]; then
suf=' '
else
suf=
fi
fi
if [[ -z "$compstate[insert]" ]] ;then
if [[ "$sort" = menu ]]; then
_description expansions expl expansions "o:$word"
else
_description -V expansions expl expansions "o:$word"
fi
compadd "$expl[@]" -UQ -qS "$suf" -a exp
else
_tags all-expansions expansions original
if [[ $#exp -ge 1 ]] && _requested expansions; then
local i j normal space dir
if [[ "$sort" = menu ]]; then
_description expansions expl expansions "o:$word"
else
_description -V expansions expl expansions "o:$word"
fi
normal=()
space=()
dir=()
for i in "$exp[@]"; do
j="${i/${opre}/${pre}}"
if [[ -d "$j" && "$i" != */ ]]; then
dir=( "$dir[@]" "$i" )
elif [[ "$asp" = yes* || ( "$asp" = *file && -f "$j" ) ]]; then
space=( "$space[@]" "$i" )
else
normal=( "$normal[@]" "$i" )
fi
done
pref="${${word:#[~/]*}:+$PWD}/"
(( $#dir )) && compadd "$expl[@]" -fW "$pref" -UQ -qS/ -a dir
(( $#space )) && compadd "$expl[@]" -fW "$pref" -UQ -qS " " -a space
(( $#normal )) && compadd "$expl[@]" -fW "$pref" -UQ -qS "" -a normal
fi
if _requested all-expansions; then
local disp dstr
if [[ "$sort" = menu ]]; then
_description all-expansions expl 'all expansions' "o:$word"
else
_description -V all-expansions expl 'all expansions' "o:$word"
fi
if [[ "${#${exp}}" -ge COLUMNS ]]; then
disp=( -ld dstr )
dstr=( "${(r:COLUMNS-5:)exp} ..." )
else
disp=()
fi
[[ -o multios ]] && exp=($exp[1] $compstate[redirect]${^exp[2,-1]})
compadd "$disp[@]" "$expl[@]" -UQ -qS "$suf" - "$exp"
fi
_requested original expl original && compadd "$expl[@]" -UQ - "$word"
compstate[insert]=menu
fi
return continue
|