-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bash_completion
483 lines (441 loc) · 15.9 KB
/
.bash_completion
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#!/bin/bash
# @ref https://github.com/cykerway/complete-alias
## ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
## programmable completion function for shell aliases;
## ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
## ============================================================================
## Copyright (C) 2016-2018 Cyker Way
##
## This program is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by the Free
## Software Foundation, either version 3 of the License, or (at your option)
## any later version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
## more details.
##
## You should have received a copy of the GNU General Public License along with
## this program. If not, see <http://www.gnu.org/licenses/>.
## ============================================================================
## ============================================================================
## variable;
## ============================================================================
## register for keeping function return value;
_retval=
## refcnt for alias expansion; expand aliases iff `_refcnt == 0`;
_refcnt=0
## ============================================================================
## function;
## ============================================================================
## debug;
_debug() {
echo
echo "#COMP_WORDS=${#COMP_WORDS[@]}"
echo "COMP_WORDS=("
for x in "${COMP_WORDS[@]}"; do
echo "'$x'"
done
echo ")"
echo "COMP_CWORD=${COMP_CWORD}"
echo "COMP_LINE='${COMP_LINE}'"
echo "COMP_POINT=${COMP_POINT}"
echo
}
## test whether element is in array;
##
## $@
## : ( elem arr[0] arr[1] ... )
_inarr() {
for e in "${@:2}"; do
[[ "$e" == "$1" ]] && return 0
done
return 1
}
## split command line into words;
##
## $1
## : command line string;
_split_cmd_line() {
## command line string;
local str="$1"
## an array that will contain words after split;
local words=()
## alloc a temp stack to track open and close chars when splitting;
local sta=()
## examine each char of `str`;
local i=0 j=0
for (( ; j < ${#str}; j++ )); do
if (( ${#sta[@]} == 0 )); then
if [[ '$(' == "${str:j:2}" ]]; then
sta+=( ')' )
(( j++ ))
elif [[ '`' == "${str:j:1}" ]]; then
sta+=( '`' )
elif [[ '(' == "${str:j:1}" ]]; then
sta+=( ')' )
elif [[ '{' == "${str:j:1}" ]]; then
sta+=( '}' )
elif [[ '"' == "${str:j:1}" ]]; then
sta+=( '"' )
elif [[ "'" == "${str:j:1}" ]]; then
sta+=( "'" )
elif [[ '\$' == "${str:j:2}" ]]; then
(( j++ ))
elif [[ '\`' == "${str:j:2}" ]]; then
(( j++ ))
elif [[ '\"' == "${str:j:2}" ]]; then
(( j++ ))
elif [[ '\\' == "${str:j:2}" ]]; then
(( j++ ))
elif [[ "\'" == "${str:j:2}" ]]; then
(( j++ ))
elif [[ $' \t\n' == *"${str:j:1}"* ]]; then
(( i < j )) && words+=( "${str:i:j-i}" )
(( i = j + 1 ))
elif [[ "><=;|&:" == *"${str:j:1}"* ]]; then
(( i < j )) && words+=( "${str:i:j-i}" )
words+=( "${str:j:1}" )
(( i = j + 1 ))
fi
elif [[ "${sta[-1]}" == ')' ]]; then
if [[ ')' == "${str:j:1}" ]]; then
unset sta[-1]
elif [[ '$(' == "${str:j:2}" ]]; then
sta+=( ')' )
(( j++ ))
elif [[ '`' == "${str:j:1}" ]]; then
sta+=( '`' )
elif [[ '(' == "${str:j:1}" ]]; then
sta+=( ')' )
elif [[ '{' == "${str:j:1}" ]]; then
sta+=( '}' )
elif [[ '"' == "${str:j:1}" ]]; then
sta+=( '"' )
elif [[ "'" == "${str:j:1}" ]]; then
sta+=( "'" )
elif [[ '\$' == "${str:j:2}" ]]; then
(( j++ ))
elif [[ '\`' == "${str:j:2}" ]]; then
(( j++ ))
elif [[ '\"' == "${str:j:2}" ]]; then
(( j++ ))
elif [[ '\\' == "${str:j:2}" ]]; then
(( j++ ))
elif [[ "\'" == "${str:j:2}" ]]; then
(( j++ ))
fi
elif [[ "${sta[-1]}" == '}' ]]; then
if [[ '}' == "${str:j:1}" ]]; then
unset sta[-1]
elif [[ '$(' == "${str:j:2}" ]]; then
sta+=( ')' )
(( j++ ))
elif [[ '`' == "${str:j:1}" ]]; then
sta+=( '`' )
elif [[ '(' == "${str:j:1}" ]]; then
sta+=( ')' )
elif [[ '{' == "${str:j:1}" ]]; then
sta+=( '}' )
elif [[ '"' == "${str:j:1}" ]]; then
sta+=( '"' )
elif [[ "'" == "${str:j:1}" ]]; then
sta+=( "'" )
elif [[ '\$' == "${str:j:2}" ]]; then
(( j++ ))
elif [[ '\`' == "${str:j:2}" ]]; then
(( j++ ))
elif [[ '\"' == "${str:j:2}" ]]; then
(( j++ ))
elif [[ '\\' == "${str:j:2}" ]]; then
(( j++ ))
elif [[ "\'" == "${str:j:2}" ]]; then
(( j++ ))
fi
elif [[ "${sta[-1]}" == '`' ]]; then
if [[ '`' == "${str:j:1}" ]]; then
unset sta[-1]
elif [[ '$(' == "${str:j:2}" ]]; then
sta+=( ')' )
(( j++ ))
elif [[ '(' == "${str:j:1}" ]]; then
sta+=( ')' )
elif [[ '{' == "${str:j:1}" ]]; then
sta+=( '}' )
elif [[ '"' == "${str:j:1}" ]]; then
sta+=( '"' )
elif [[ "'" == "${str:j:1}" ]]; then
sta+=( "'" )
elif [[ '\$' == "${str:j:2}" ]]; then
(( j++ ))
elif [[ '\`' == "${str:j:2}" ]]; then
(( j++ ))
elif [[ '\"' == "${str:j:2}" ]]; then
(( j++ ))
elif [[ '\\' == "${str:j:2}" ]]; then
(( j++ ))
elif [[ "\'" == "${str:j:2}" ]]; then
(( j++ ))
fi
elif [[ "${sta[-1]}" == "'" ]]; then
if [[ "'" == "${str:j:1}" ]]; then
unset sta[-1]
fi
elif [[ "${sta[-1]}" == '"' ]]; then
if [[ '"' == "${str:j:1}" ]]; then
unset sta[-1]
elif [[ '$(' == "${str:j:2}" ]]; then
sta+=( ')' )
(( j++ ))
elif [[ '`' == "${str:j:1}" ]]; then
sta+=( '`' )
elif [[ '\$' == "${str:j:2}" ]]; then
(( j++ ))
elif [[ '\`' == "${str:j:2}" ]]; then
(( j++ ))
elif [[ '\"' == "${str:j:2}" ]]; then
(( j++ ))
elif [[ '\\' == "${str:j:2}" ]]; then
(( j++ ))
fi
fi
done
## append the last word;
(( i < j )) && words+=( "${str:i:j-i}" )
## unset the temp stack;
unset sta
## return value;
_retval=( "${words[@]}" )
}
## expand aliases in command line;
##
## $1
## : beg word index;
## $2
## : end word index;
## $3
## : ignored word index (can be null);
## $4
## : number of used aliases;
## ${@:4}
## : used aliases;
## $?
## : difference of `${#COMP_WORDS}` before and after expansion;
_expand_alias() {
local beg="$1" end="$2" ignore="$3" n_used="$4"; shift 4
local used=( "${@:1:$n_used}" ); shift "$n_used"
if (( $beg == $end )) ; then
## case 1: range is empty;
_retval=0
elif [[ -n "$ignore" ]] && (( $beg == $ignore )); then
## case 2: beg index is ignored; pass it;
_expand_alias \
"$(( $beg + 1 ))" \
"$end" \
"$ignore" \
"${#used[@]}" \
"${used[@]}"
elif ! alias "${COMP_WORDS[$beg]}" &>/dev/null; then
## case 3: command is not an alias;
_retval=0
elif ( _inarr "${COMP_WORDS[$beg]}" "${used[@]}" ); then
## case 4: command is an used alias;
_retval=0
else
## case 5: command is an unused alias;
## get alias name;
local cmd="${COMP_WORDS[$beg]}"
## get alias body;
local str0="$( alias "$cmd" | sed -E 's/[^=]*=//' | xargs )"
## split alias body into words;
_split_cmd_line "$str0"
local words0=( "${_retval[@]}" )
## find index range of word `$COMP_WORDS[$beg]` in string `$COMP_LINE`;
local i=0 j=0
for (( i = 0; i <= $beg; i++ )); do
for (( ; j <= ${#COMP_LINE}; j++ )); do
[[ "${COMP_LINE:j}" == "${COMP_WORDS[i]}"* ]] && break
done
(( i == $beg )) && break
(( j += ${#COMP_WORDS[i]} ))
done
## now `j` is at the beginning of word `$COMP_WORDS[$beg]`; and we know
## the index range is `[j, j+${#cmd})`; update `$COMP_LINE` and
## `$COMP_POINT`;
COMP_LINE="${COMP_LINE:0:j}${str0}${COMP_LINE:j+${#cmd}}"
if (( $COMP_POINT < j )); then
:
elif (( $COMP_POINT < j + ${#cmd} )); then
## set current cursor position to the end of replacement string;
(( COMP_POINT = j + ${#str0} ))
else
(( COMP_POINT += ${#str0} - ${#cmd} ))
fi
## update `$COMP_WORDS` and `$COMP_CWORD`;
COMP_WORDS=(
"${COMP_WORDS[@]:0:beg}"
"${words0[@]}"
"${COMP_WORDS[@]:beg+1}"
)
if (( $COMP_CWORD < $beg )); then
:
elif (( $COMP_CWORD < $beg + 1 )); then
## set current word index to the last of replacement words;
(( COMP_CWORD = $beg + ${#words0[@]} - 1 ))
else
(( COMP_CWORD += ${#words0[@]} - 1 ))
fi
## update `$ignore` if it is not empty; if `$ignore` is not empty, then
## we know it is not equal to `$beg` because we checked that in case 2;
if [[ -n "$ignore" ]] && (( $ignore > $beg )); then
(( ignore += ${#words0[@]} - 1 ))
fi
## recursively expand part 0;
local used0=( "${used[@]}" "$cmd" )
_expand_alias \
"$beg" \
"$(( $beg + ${#words0[@]} ))" \
"$ignore" \
"${#used0[@]}" \
"${used0[@]}"
local diff0="$_retval"
## recursively expand part 1;
if [[ -n "$str0" ]] && [[ "${str0: -1}" == ' ' ]]; then
local used1=( "${used[@]}" )
_expand_alias \
"$(( $beg + ${#words0[@]} + $diff0 ))" \
"$(( $end + ${#words0[@]} - 1 + $diff0 ))" \
"$ignore" \
"${#used1[@]}" \
"${used1[@]}"
local diff1="$_retval"
else
local diff1=0
fi
## return value;
_retval=$(( ${#words0[@]} - 1 + diff0 + diff1 ))
fi
}
## set a command completion function to the default one; users may edit this
## function to fit their own needs;
_set_default_completion() {
local cmd="$1"
case "$cmd" in
bind)
complete -A binding "$cmd"
;;
help)
complete -A helptopic "$cmd"
;;
set)
complete -A setopt "$cmd"
;;
shopt)
complete -A shopt "$cmd"
;;
bg)
complete -A stopped -P '"%' -S '"' "$cmd"
;;
service)
complete -F _service "$cmd"
;;
unalias)
complete -a "$cmd"
;;
builtin)
complete -b "$cmd"
;;
command|type|which)
complete -c "$cmd"
;;
fg|jobs|disown)
complete -j -P '"%' -S '"' "$cmd"
;;
groups|slay|w|sux)
complete -u "$cmd"
;;
readonly|unset)
complete -v "$cmd"
;;
traceroute|traceroute6|tracepath|tracepath6|fping|fping6|telnet|rsh|\
rlogin|ftp|dig|mtr|ssh-installkeys|showmount)
complete -F _known_hosts "$cmd"
;;
aoss|command|do|else|eval|exec|ltrace|nice|nohup|padsp|then|time|\
tsocks|vsound|xargs)
complete -F _command "$cmd"
;;
fakeroot|gksu|gksudo|kdesudo|really)
complete -F _root_command "$cmd"
;;
a2ps|awk|base64|bash|bc|bison|cat|chroot|colordiff|cp|csplit|cut|date|\
df|diff|dir|du|enscript|env|expand|fmt|fold|gperf|grep|grub|head|\
irb|ld|ldd|less|ln|ls|m4|md5sum|mkdir|mkfifo|mknod|mv|netstat|nl|\
nm|objcopy|objdump|od|paste|pr|ptx|readelf|rm|rmdir|sed|seq|\
sha{,1,224,256,384,512}sum|shar|sort|split|strip|sum|tac|tail|tee|\
texindex|touch|tr|uname|unexpand|uniq|units|vdir|wc|who)
complete -F _longopt "$cmd"
;;
*)
_completion_loader "$cmd"
;;
esac
}
## programmable completion function for aliases; this is the function to be set
## with `complete -F`;
_complete_alias() {
## get command;
local cmd="${COMP_WORDS[0]}"
## we expand aliases only for the original command line (ie: the command
## line after which user pressed `<tab>`); this means we expand aliases
## only in the outmost call of this function; we ensure this by using a
## refcnt and expand aliases iff the refcnt is equal to 0;
if (( _refcnt == 0 )); then
## find index range of word `$COMP_WORDS[$COMP_CWORD]` in string
## `$COMP_LINE`; dont expand this word if `$COMP_POINT` (cursor
## position) lies in this range because the word may be incomplete;
local i=0 j=0
for (( ; i <= $COMP_CWORD; i++ )); do
for (( ; j <= ${#COMP_LINE}; j++ )); do
[[ "${COMP_LINE:j}" == "${COMP_WORDS[i]}"* ]] && break
done
(( i == $COMP_CWORD )) && break
(( j += ${#COMP_WORDS[i]} ))
done
## now `j` is at the beginning of word `$COMP_WORDS[$COMP_CWORD]`; and
## we know the index range is `[j, j+${#COMP_WORDS[$COMP_CWORD]}]`; we
## include the right endpoint to cover the case where cursor is at the
## exact end of the word; compare the index range with `$COMP_POINT`;
if (( j <= $COMP_POINT )) && \
(( $COMP_POINT <= j + ${#COMP_WORDS[$COMP_CWORD]} )); then
local ignore="$COMP_CWORD"
else
local ignore=""
fi
## expand aliases;
_expand_alias 0 "${#COMP_WORDS[@]}" "$ignore" 0
fi
## increase refcnt;
(( _refcnt++ ))
## since aliases have been fully expanded, we no longer need to consider
## aliases in the resulting command line; so we now set this command
## completion function to the default one (which is alias-agnostic); this
## avoids infinite recursion when a command is aliased to itself (ie:
## `alias ls='ls -a'`);
_set_default_completion "$cmd"
## do actual completion;
_command_offset 0
## reset this command completion function to `_complete_alias`;
complete -F _complete_alias "$cmd"
## decrease refcnt;
(( _refcnt-- ))
}
## ============================================================================
## complete user-defined aliases;
##
## uncomment and edit these lines to complete your aliases;
## ============================================================================
complete -F _complete_alias k
complete -F _complete_alias kx
complete -F _complete_alias kn