summaryrefslogtreecommitdiff
path: root/examples/complete/complete-examples
blob: baa97e393b8c2924470708380a4bfcc83608d964 (plain)
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
484
485
486
487
488
489
490
491
492
493
494
495
#
# Completion examples
#

#
# This encapsulates the default bash completion code
# call with the word to be completed as $1
#
# Since programmable completion does not use the bash default completions
# or the readline default of filename completion when the compspec does
# not generate any matches, this may be used as a `last resort' in a
# completion function to mimic the default bash completion behavior.
#
_bash_def_completion ()
{
	local h t
	COMPREPLY=()

	# command substitution
	if [[ "$1" == \$\(* ]]; then
		t=${1#??}
		COMPREPLY=( $(compgen -c -P '$(' $t) )
	fi
	# variables with a leading `${'
	if [ ${#COMPREPLY[@]} -eq 0 ] && [[ "$1" == \$\{* ]]; then
		t=${1#??}
		COMPREPLY=( $(compgen -v -P '${' -S '}' $t) )
	fi
	# variables with a leading `$'
	if [ ${#COMPREPLY[@]} -eq 0 ] && [[ "$1" == \$* ]]; then
		t=${1#?}
		COMPREPLY=( $(compgen -v -P '$' $t ) )
	fi
	# username expansion
	if [ ${#COMPREPLY[@]} -eq 0 ] && [[ "$1" == ~* ]] && [[ "$1" != */* ]]; then
		t=${1#?}
		COMPREPLY=( $( compgen -u -P '~' $t ) )
	fi
	# hostname
	if [ ${#COMPREPLY[@]} -eq 0 ] && [[ "$1" == *@* ]]; then
		h=${1%%@*}
		t=${1#*@}
		COMPREPLY=( $( compgen -A hostname -P "${h}@" $t ) )
	fi
	# glob pattern
	if [ ${#COMPREPLY[@]} -eq 0 ]; then
		# sh-style glob pattern
		if [[ $1 == *[*?[]* ]]; then
			COMPREPLY=( $( compgen -G "$1" ) )
		# ksh-style extended glob pattern - must be complete
		elif shopt -q extglob && [[ $1 == *[?*+\!@]\(*\)* ]]; then
			COMPREPLY=( $( compgen -G "$1" ) )
		fi
	fi

	# final default is filename completion
	if [ ${#COMPREPLY[@]} -eq 0 ]; then
		COMPREPLY=( $(compgen -f "$1" ) )
	fi
}

# 
# Return 1 if $1 appears to contain a redirection operator.  Handles backslash
# quoting (barely).
#
_redir_op()
{
	case "$1" in
	*\\'[\<\>]'*)	return 1;;
	*[\<\>]*)	return 0;;
	*)		return 1;;
	esac
}


# _redir_test tests the current word ($1) and the previous word ($2) for
# redirection operators and does filename completion on the current word
# if either one contains a redirection operator
_redir_test()
{
	if _redir_op "$1" ; then
		COMPREPLY=( $( compgen -f "$1" ) )
		return 0
	elif _redir_op "$2" ; then
		COMPREPLY=( $( compgen -f "$1" ) )
		return 0
	fi
	return 1
}

# optional, but without this you can't use extended glob patterns
shopt -s extglob

#
# Easy ones for the shell builtins
#
# nothing for: alias, break, continue, dirs, echo, eval, exit, getopts,
# let, logout, popd, printf, pwd, return, shift, suspend, test, times,
# umask
#

complete -f -- . source
complete -A enabled builtin
complete -d cd

# this isn't exactly right yet -- needs to skip shell functions and
# do $PATH lookup (or do compgen -c and filter out matches that also
# appear in compgen -A function)
complete -c command

# could add -S '=', but that currently screws up because readline appends
# a space unconditionally

complete -v export local readonly
complete -A helptopic help	# currently same as builtins

complete -d pushd

complete -A shopt shopt

complete -c type

complete -a unalias
complete -v unset 

#
# Job control builtins: fg, bg, disown, kill, wait
# kill not done yet
#

complete -A stopped -P '%' bg
complete -j -P '%' fg jobs disown

# this is not quite right at this point

_wait_func ()
{
	local cur
	cur=${COMP_WORDS[COMP_CWORD]}

	case "$cur" in
	%*)	COMPREPLY=( $(compgen -A running -P '%' ${cur#?} ) ) ;;
	[0-9]*)	COMPREPLY=( $(jobs -p | grep ^${cur}) ) ;;
	*)	COMPREPLY=( $(compgen -A running -P '%') $(jobs -p) )
		;;
	esac
}
complete -F _wait_func wait

#
# more complicated things, several as yet unimplemented
#

#complete -F _bind_func bind

_declare_func()
{
	local cur prev nflag opts

	cur=${COMP_WORDS[COMP_CWORD]}
	prev=${COMP_WORDS[COMP_CWORD-1]}

	COMPREPLY=()
	if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
		COMPREPLY=(-a -f -F -i -p -r -t -x)
		return 0;
	fi
	if [[ $cur == '+' ]]; then
		COMPREPLY=(+i +t +x)
		return 0;
	fi
	if [[ $prev == '-p' ]]; then
		COMPREPLY=( $(compgen -v $cur) )
		return 0;
	fi
	return 1
}
complete -F _declare_func declare typeset

_enable_func()
{
	local cur prev nflag opts

	cur=${COMP_WORDS[COMP_CWORD]}
	prev=${COMP_WORDS[COMP_CWORD-1]}

	COMPREPLY=()
	if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
		COMPREPLY=(-a -d -f -n -p -s)
		return 0;
	fi
	if [[ $prev == '-f' ]]; then
		COMPREPLY=( $( compgen -f $cur ) )
		return 0;
	fi
	for opts in "${COMP_WORDS[@]}" ; do
		if [[ $opts == -*n* ]]; then nflag=1; fi
	done

	if [ -z "$nflag" ] ; then
		COMPREPLY=( $( compgen -A enabled $cur ) )
	else
		COMPREPLY=( $( compgen -A disabled $cur ) )
	fi
	return 0;
}
complete -F _enable_func enable

_exec_func()
{
	local cur prev

	cur=${COMP_WORDS[COMP_CWORD]}
	prev=${COMP_WORDS[COMP_CWORD-1]}

	if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
		COMPREPLY=(-a -c -l)
		return 0;
	fi
	if [[ $prev != -*a* ]]; then
		COMPREPLY=( $( compgen -c $cur ) )
		return 0
	fi
	return 1;
}
complete -F _exec_func exec

_fc_func()
{
	local cur prev

	cur=${COMP_WORDS[COMP_CWORD]}
	prev=${COMP_WORDS[COMP_CWORD-1]}

	if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
		COMPREPLY=(-e -n -l -r -s)
		return 0;
	fi
	if [[ $prev == -*e ]]; then
		COMPREPLY=( $(compgen -c $cur) )
		return 0
	fi
	return 1
}
complete -F _fc_func fc

_hash_func()
{
	local cur prev

	cur=${COMP_WORDS[COMP_CWORD]}
	prev=${COMP_WORDS[COMP_CWORD-1]}

	if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
		COMPREPLY=(-p -r -t)
		return 0;
	fi

	if [[ $prev == '-p' ]]; then
		COMPREPLY=( $( compgen -f $cur ) )
		return 0;
	fi
	COMPREPLY=( $( compgen -c $cur ) )
	return 0
}
complete -F _hash_func hash

_history_func()
{
	local cur prev

	cur=${COMP_WORDS[COMP_CWORD]}
	prev=${COMP_WORDS[COMP_CWORD-1]}

	COMPREPLY=()
	if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
		COMPREPLY=(-a -c -d -n -r -w -p -s)
		return 0;
	fi
	if [[ $prev == -[anrw] ]]; then
		COMPREPLY=( $( compgen -f $cur ) )
	fi
	return 0
}
complete -F _history_func history

#complete -F _read_func read

_set_func ()
{
	local cur prev

	cur=${COMP_WORDS[COMP_CWORD]}
	prev=${COMP_WORDS[COMP_CWORD-1]}

	COMPREPLY=()

	_redir_test "$cur" "$prev" && return 0;

	if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
		COMPREPLY=(-a -b -e -f -k -m -n -o -p -t -u -v -x -B -C -H -P --)
		return 0;
	fi
	if [[ $cur == '+' ]]; then
		COMPREPLY=(+a +b +e +f +k +m +n +o +p +t +u +v +x +B +C +H +P)
		return 0;
	fi
	if [[ $prev == [+-]o ]]; then
		COMPREPLY=( $(compgen -A setopt $cur) )
		return 0;
	fi
	return 1;
}
complete -F _set_func set

_trap_func ()
{
	local cur
	cur=${COMP_WORDS[COMP_CWORD]}

	if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
		COMPREPLY=(-l -p)
		return 0;
	fi
	COMPREPLY=( $( compgen -A signal ${cur}) )
	return 0
}
complete -F _trap_func trap

#
# meta-completion (completion for complete/compgen)
#
_complete_meta_func()
{
	local cur prev cmd
	COMPREPLY=()

	cmd=$1

	cur=${COMP_WORDS[COMP_CWORD]}
	prev=${COMP_WORDS[COMP_CWORD-1]}

	_redir_test "$cur" "$prev" && return 0;

	if (( $COMP_CWORD <= 1 )) || [[ "$cur" == '-' ]]; then
		case "$cmd" in
		complete) COMPREPLY=(-a -b -c -d -e -f -j -k -s -v -u -r -p -A -G -W -P -S -X -F -C);;
		compgen)  COMPREPLY=(-a -b -c -d -e -f -j -k -s -v -u -A -G -W -P -S -X -F -C);;
		esac
		return 0
	fi

	if [[ $prev == -A ]]; then
		COMPREPLY=(alias arrayvar binding builtin command directory \
disabled enabled export file 'function' helptopic hostname job keyword \
running service setopt shopt signal stopped variable)
		return 0
	elif [[ $prev == -F ]]; then
		COMPREPLY=( $( compgen -A function $cur ) )
	elif [[ $prev == -C ]]; then
		COMPREPLY=( $( compgen -c $cur ) )
	else
		COMPREPLY=( $( compgen -c $cur ) )
	fi
	return 0
}
complete -F _complete_meta_func complete compgen

#
# some completions for shell reserved words
#
#complete -c -k time do if then else elif '{'

#
# external commands
#

complete -e printenv

complete -c nohup exec nice eval trace truss strace sotruss gdb

_make_targets ()
{
	local mdef makef gcmd cur prev i

	COMPREPLY=()
	cur=${COMP_WORDS[COMP_CWORD]}
	prev=${COMP_WORDS[COMP_CWORD-1]}

	# if prev argument is -f, return possible filename completions.
	# we could be a little smarter here and return matches against
	# `makefile Makefile *.mk', whatever exists
	case "$prev" in
	-*f)	COMPREPLY=( $(compgen -f $cur ) ); return 0;;
	esac

	# if we want an option, return the possible posix options
	case "$cur" in
	-)	COMPREPLY=(-e -f -i -k -n -p -q -r -S -s -t); return 0;;
	esac

	# make reads `makefile' before `Makefile'
	# GNU make reads `GNUmakefile' before all other makefiles, but we
	# check that we're completing `gmake' before checking for it
	if [ -f GNUmakefile ] && [ ${COMP_WORDS[0]} == gmake ]; then
		mdef=GNUmakefile
	elif [ -f makefile ]; then
		mdef=makefile
	elif [ -f Makefile ]; then
		mdef=Makefile
	else
		mdef=*.mk		# local convention
	fi

	# before we scan for targets, see if a makefile name was specified
	# with -f
	for (( i=0; i < ${#COMP_WORDS[@]}; i++ )); do
		if [[ ${COMP_WORDS[i]} == -*f ]]; then
			eval makef=${COMP_WORDS[i+1]}	# eval for tilde expansion
			break
		fi
	done

	[ -z "$makef" ] && makef=$mdef

	# if we have a partial word to complete, restrict completions to
	# matches of that word
	if [ -n "$2" ]; then gcmd='grep "^$2"' ; else gcmd=cat ; fi

	# if we don't want to use *.mk, we can take out the cat and use
	# test -f $makef and input redirection	
	COMPREPLY=( $(cat $makef 2>/dev/null | awk 'BEGIN {FS=":"} /^[^.# 	][^=]*:/ {print $1}' | tr -s ' ' '\012' | sort -u | eval $gcmd ) )
}
complete -F _make_targets -X '+($*|*.[cho])' make gmake pmake

_umount_func ()
{
	COMPREPLY=( $(mount | awk '{print $1}') )
}
complete -F _umount_func umount

_configure_func ()
{
	case "$2" in
	-*)	;;
	*)	return ;;
	esac

	case "$1" in
	\~*)	eval cmd=$1 ;;
	*)	cmd="$1" ;;
	esac

	COMPREPLY=( $("$cmd" --help | awk '{if ($1 ~ /--.*/) print $1}' | grep ^"$2" | sort -u) )
}
complete -F _configure_func configure

complete -W '"${GROUPS[@]}"' newgrp

complete -f chown ln more cat
complete -d mkdir rmdir
complete -f strip

complete -f -X '*.gz' gzip
complete -f -X '*.bz2' bzip2
complete -f -X '*.Z' compress
complete -f -X '!*.+(gz|tgz|Gz)' gunzip gzcat zcat zmore
complete -f -X '!*.Z' uncompress zmore zcat
complete -f -X '!*.bz2' bunzip2 bzcat
complete -f -X '!*.zip' unzip
complete -f -X '!*.+(gif|jpg|jpeg|GIF|JPG|JPEG|bmp)' xv

complete -f -X '!*.pl' perl perl5

complete -A hostname rsh telnet rlogin ftp ping xping host traceroute nslookup
complete -A hostname rxterm rxterm3 rxvt2

complete -u su
complete -g newgrp groupdel groupmod

complete -f -X '!*.+(ps|PS)' gs gv ghostview psselect pswrap
complete -f -X '!*.+(dvi|DVI)' dvips xdvi dviselect dvitype catdvi
complete -f -X '!*.+(pdf|PDF)' acroread4
complete -f -X '!*.texi*' makeinfo texi2dvi texi2html
complete -f -X '!*.+(tex|TEX)' tex latex slitex

complete -f -X '!*.+(mp3|MP3)' mpg123
complete -f -X '!*.+(htm|html)' links w3m lynx

#
# other possibilities, left as exercises
#
#complete -F _find_func find
#complete -F _man_func man
#complete -F _stty_func stty