6 # This encapsulates the default bash completion code
7 # call with the word to be completed as $1
9 # Since programmable completion does not use the bash default completions
10 # or the readline default of filename completion when the compspec does
11 # not generate any matches, this may be used as a `last resort' in a
12 # completion function to mimic the default bash completion behavior.
14 _bash_def_completion ()
19 # command substitution
20 if [[ "$1" == \$\(* ]]; then
22 COMPREPLY=( $(compgen -c -P '$(' $t) )
24 # variables with a leading `${'
25 if [ ${#COMPREPLY[@]} -eq 0 ] && [[ "$1" == \$\{* ]]; then
27 COMPREPLY=( $(compgen -v -P '${' -S '}' $t) )
29 # variables with a leading `$'
30 if [ ${#COMPREPLY[@]} -eq 0 ] && [[ "$1" == \$* ]]; then
32 COMPREPLY=( $(compgen -v -P '$' $t ) )
35 if [ ${#COMPREPLY[@]} -eq 0 ] && [[ "$1" == ~* ]] && [[ "$1" != */* ]]; then
37 COMPREPLY=( $( compgen -u -P '~' $t ) )
40 if [ ${#COMPREPLY[@]} -eq 0 ] && [[ "$1" == *@* ]]; then
43 COMPREPLY=( $( compgen -A hostname -P "${h}@" $t ) )
46 if [ ${#COMPREPLY[@]} -eq 0 ]; then
47 # sh-style glob pattern
48 if [[ $1 == *[*?[]* ]]; then
49 COMPREPLY=( $( compgen -G "$1" ) )
50 # ksh-style extended glob pattern - must be complete
51 elif shopt -q extglob && [[ $1 == *[?*+\!@]\(*\)* ]]; then
52 COMPREPLY=( $( compgen -G "$1" ) )
56 # final default is filename completion
57 if [ ${#COMPREPLY[@]} -eq 0 ]; then
58 COMPREPLY=( $(compgen -f "$1" ) )
63 # Return 1 if $1 appears to contain a redirection operator. Handles backslash
69 *\\'[\<\>]'*) return 1;;
76 # _redir_test tests the current word ($1) and the previous word ($2) for
77 # redirection operators and does filename completion on the current word
78 # if either one contains a redirection operator
81 if _redir_op "$1" ; then
82 COMPREPLY=( $( compgen -f "$1" ) )
84 elif _redir_op "$2" ; then
85 COMPREPLY=( $( compgen -f "$1" ) )
91 # optional, but without this you can't use extended glob patterns
95 # Easy ones for the shell builtins
97 # nothing for: alias, break, continue, dirs, echo, eval, exit, getopts,
98 # let, logout, popd, printf, pwd, return, shift, suspend, test, times,
102 complete -f -- . source
103 complete -A enabled builtin
106 # this isn't exactly right yet -- needs to skip shell functions and
107 # do $PATH lookup (or do compgen -c and filter out matches that also
108 # appear in compgen -A function)
111 # could add -S '=', but that currently screws up because readline appends
112 # a space unconditionally
114 complete -v export local readonly
115 complete -A helptopic help # currently same as builtins
119 complete -A shopt shopt
127 # Job control builtins: fg, bg, disown, kill, wait
131 complete -A stopped -P '%' bg
132 complete -j -P '%' fg jobs disown
134 # this is not quite right at this point
139 cur=${COMP_WORDS[COMP_CWORD]}
142 %*) COMPREPLY=( $(compgen -A running -P '%' ${cur#?} ) ) ;;
143 [0-9]*) COMPREPLY=( $(jobs -p | grep ^${cur}) ) ;;
144 *) COMPREPLY=( $(compgen -A running -P '%') $(jobs -p) )
148 complete -F _wait_func wait
151 # more complicated things, several as yet unimplemented
154 #complete -F _bind_func bind
158 local cur prev nflag opts
160 cur=${COMP_WORDS[COMP_CWORD]}
161 prev=${COMP_WORDS[COMP_CWORD-1]}
164 if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
165 COMPREPLY=(-a -f -F -i -p -r -t -x)
168 if [[ $cur == '+' ]]; then
172 if [[ $prev == '-p' ]]; then
173 COMPREPLY=( $(compgen -v $cur) )
178 complete -F _declare_func declare typeset
182 local cur prev nflag opts
184 cur=${COMP_WORDS[COMP_CWORD]}
185 prev=${COMP_WORDS[COMP_CWORD-1]}
188 if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
189 COMPREPLY=(-a -d -f -n -p -s)
192 if [[ $prev == '-f' ]]; then
193 COMPREPLY=( $( compgen -f $cur ) )
196 for opts in "${COMP_WORDS[@]}" ; do
197 if [[ $opts == -*n* ]]; then nflag=1; fi
200 if [ -z "$nflag" ] ; then
201 COMPREPLY=( $( compgen -A enabled $cur ) )
203 COMPREPLY=( $( compgen -A disabled $cur ) )
207 complete -F _enable_func enable
213 cur=${COMP_WORDS[COMP_CWORD]}
214 prev=${COMP_WORDS[COMP_CWORD-1]}
216 if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
220 if [[ $prev != -*a* ]]; then
221 COMPREPLY=( $( compgen -c $cur ) )
226 complete -F _exec_func exec
232 cur=${COMP_WORDS[COMP_CWORD]}
233 prev=${COMP_WORDS[COMP_CWORD-1]}
235 if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
236 COMPREPLY=(-e -n -l -r -s)
239 if [[ $prev == -*e ]]; then
240 COMPREPLY=( $(compgen -c $cur) )
245 complete -F _fc_func fc
251 cur=${COMP_WORDS[COMP_CWORD]}
252 prev=${COMP_WORDS[COMP_CWORD-1]}
254 if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
259 if [[ $prev == '-p' ]]; then
260 COMPREPLY=( $( compgen -f $cur ) )
263 COMPREPLY=( $( compgen -c $cur ) )
266 complete -F _hash_func hash
272 cur=${COMP_WORDS[COMP_CWORD]}
273 prev=${COMP_WORDS[COMP_CWORD-1]}
276 if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
277 COMPREPLY=(-a -c -d -n -r -w -p -s)
280 if [[ $prev == -[anrw] ]]; then
281 COMPREPLY=( $( compgen -f $cur ) )
285 complete -F _history_func history
287 #complete -F _read_func read
293 cur=${COMP_WORDS[COMP_CWORD]}
294 prev=${COMP_WORDS[COMP_CWORD-1]}
298 _redir_test "$cur" "$prev" && return 0;
300 if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
301 COMPREPLY=(-a -b -e -f -k -m -n -o -p -t -u -v -x -B -C -H -P --)
304 if [[ $cur == '+' ]]; then
305 COMPREPLY=(+a +b +e +f +k +m +n +o +p +t +u +v +x +B +C +H +P)
308 if [[ $prev == [+-]o ]]; then
309 COMPREPLY=( $(compgen -A setopt $cur) )
314 complete -F _set_func set
319 cur=${COMP_WORDS[COMP_CWORD]}
321 if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
325 COMPREPLY=( $( compgen -A signal ${cur}) )
328 complete -F _trap_func trap
331 # meta-completion (completion for complete/compgen)
333 _complete_meta_func()
340 cur=${COMP_WORDS[COMP_CWORD]}
341 prev=${COMP_WORDS[COMP_CWORD-1]}
343 _redir_test "$cur" "$prev" && return 0;
345 if (( $COMP_CWORD <= 1 )) || [[ "$cur" == '-' ]]; then
347 complete) COMPREPLY=(-a -b -c -d -e -f -j -k -s -v -u -r -p -A -G -W -P -S -X -F -C);;
348 compgen) COMPREPLY=(-a -b -c -d -e -f -j -k -s -v -u -A -G -W -P -S -X -F -C);;
353 if [[ $prev == -A ]]; then
354 COMPREPLY=(alias arrayvar binding builtin command directory \
355 disabled enabled export file 'function' helptopic hostname job keyword \
356 running service setopt shopt signal stopped variable)
358 elif [[ $prev == -F ]]; then
359 COMPREPLY=( $( compgen -A function $cur ) )
360 elif [[ $prev == -C ]]; then
361 COMPREPLY=( $( compgen -c $cur ) )
363 COMPREPLY=( $( compgen -c $cur ) )
367 complete -F _complete_meta_func complete compgen
370 # some completions for shell reserved words
372 #complete -c -k time do if then else elif '{'
380 complete -c nohup exec nice eval trace truss strace sotruss gdb
384 local mdef makef gcmd cur prev i
387 cur=${COMP_WORDS[COMP_CWORD]}
388 prev=${COMP_WORDS[COMP_CWORD-1]}
390 # if prev argument is -f, return possible filename completions.
391 # we could be a little smarter here and return matches against
392 # `makefile Makefile *.mk', whatever exists
394 -*f) COMPREPLY=( $(compgen -f $cur ) ); return 0;;
397 # if we want an option, return the possible posix options
399 -) COMPREPLY=(-e -f -i -k -n -p -q -r -S -s -t); return 0;;
402 # make reads `makefile' before `Makefile'
403 # GNU make reads `GNUmakefile' before all other makefiles, but we
404 # check that we're completing `gmake' before checking for it
405 if [ -f GNUmakefile ] && [ ${COMP_WORDS[0]} == gmake ]; then
407 elif [ -f makefile ]; then
409 elif [ -f Makefile ]; then
412 mdef=*.mk # local convention
415 # before we scan for targets, see if a makefile name was specified
417 for (( i=0; i < ${#COMP_WORDS[@]}; i++ )); do
418 if [[ ${COMP_WORDS[i]} == -*f ]]; then
419 eval makef=${COMP_WORDS[i+1]} # eval for tilde expansion
424 [ -z "$makef" ] && makef=$mdef
426 # if we have a partial word to complete, restrict completions to
427 # matches of that word
428 if [ -n "$2" ]; then gcmd='grep "^$2"' ; else gcmd=cat ; fi
430 # if we don't want to use *.mk, we can take out the cat and use
431 # test -f $makef and input redirection
432 COMPREPLY=( $(cat $makef 2>/dev/null | awk 'BEGIN {FS=":"} /^[^.# ][^=]*:/ {print $1}' | tr -s ' ' '\012' | sort -u | eval $gcmd ) )
434 complete -F _make_targets -X '+($*|*.[cho])' make gmake pmake
438 COMPREPLY=( $(mount | awk '{print $1}') )
440 complete -F _umount_func umount
454 COMPREPLY=( $("$cmd" --help | awk '{if ($1 ~ /--.*/) print $1}' | grep ^"$2" | sort -u) )
456 complete -F _configure_func configure
458 complete -W '"${GROUPS[@]}"' newgrp
460 complete -f chown ln more cat
461 complete -d mkdir rmdir
464 complete -f -X '*.gz' gzip
465 complete -f -X '*.bz2' bzip2
466 complete -f -X '*.Z' compress
467 complete -f -X '!*.+(gz|tgz|Gz)' gunzip gzcat zcat zmore
468 complete -f -X '!*.Z' uncompress zmore zcat
469 complete -f -X '!*.bz2' bunzip2 bzcat
470 complete -f -X '!*.zip' unzip
471 complete -f -X '!*.+(gif|jpg|jpeg|GIF|JPG|JPEG|bmp)' xv
473 complete -f -X '!*.pl' perl perl5
475 complete -A hostname rsh telnet rlogin ftp ping xping host traceroute nslookup
476 complete -A hostname rxterm rxterm3 rxvt2
479 complete -g newgrp groupdel groupmod
481 complete -f -X '!*.+(ps|PS)' gs gv ghostview psselect pswrap
482 complete -f -X '!*.+(dvi|DVI)' dvips xdvi dviselect dvitype catdvi
483 complete -f -X '!*.+(pdf|PDF)' acroread4
484 complete -f -X '!*.texi*' makeinfo texi2dvi texi2html
485 complete -f -X '!*.+(tex|TEX)' tex latex slitex
487 complete -f -X '!*.+(mp3|MP3)' mpg123
488 complete -f -X '!*.+(htm|html)' links w3m lynx
491 # other possibilities, left as exercises
493 #complete -F _find_func find
494 #complete -F _man_func man
495 #complete -F _stty_func stty