4 # Chet Ramey <chet.ramey@case.edu>
6 # Copyright 2002 Chester Ramey
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2, or (at your option)
13 # TThis program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software Foundation,
20 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 # This encapsulates the default bash completion code
24 # call with the word to be completed as $1
26 # Since programmable completion does not use the bash default completions
27 # or the readline default of filename completion when the compspec does
28 # not generate any matches, this may be used as a `last resort' in a
29 # completion function to mimic the default bash completion behavior.
31 _bash_def_completion ()
36 # command substitution
37 if [[ "$1" == \$\(* ]]; then
39 COMPREPLY=( $(compgen -c -P '$(' $t) )
41 # variables with a leading `${'
42 if [ ${#COMPREPLY[@]} -eq 0 ] && [[ "$1" == \$\{* ]]; then
44 COMPREPLY=( $(compgen -v -P '${' -S '}' $t) )
46 # variables with a leading `$'
47 if [ ${#COMPREPLY[@]} -eq 0 ] && [[ "$1" == \$* ]]; then
49 COMPREPLY=( $(compgen -v -P '$' $t ) )
52 if [ ${#COMPREPLY[@]} -eq 0 ] && [[ "$1" == ~* ]] && [[ "$1" != */* ]]; then
54 COMPREPLY=( $( compgen -u -P '~' $t ) )
57 if [ ${#COMPREPLY[@]} -eq 0 ] && [[ "$1" == *@* ]]; then
60 COMPREPLY=( $( compgen -A hostname -P "${h}@" $t ) )
63 if [ ${#COMPREPLY[@]} -eq 0 ]; then
64 # sh-style glob pattern
65 if [[ $1 == *[*?[]* ]]; then
66 COMPREPLY=( $( compgen -G "$1" ) )
67 # ksh-style extended glob pattern - must be complete
68 elif shopt -q extglob && [[ $1 == *[?*+\!@]\(*\)* ]]; then
69 COMPREPLY=( $( compgen -G "$1" ) )
73 # final default is filename completion
74 if [ ${#COMPREPLY[@]} -eq 0 ]; then
75 COMPREPLY=( $(compgen -f "$1" ) )
80 # Return 1 if $1 appears to contain a redirection operator. Handles backslash
86 *\\'[\<\>]'*) return 1;;
93 # _redir_test tests the current word ($1) and the previous word ($2) for
94 # redirection operators and does filename completion on the current word
95 # if either one contains a redirection operator
98 if _redir_op "$1" ; then
99 COMPREPLY=( $( compgen -f "$1" ) )
101 elif _redir_op "$2" ; then
102 COMPREPLY=( $( compgen -f "$1" ) )
108 # optional, but without this you can't use extended glob patterns
112 # Easy ones for the shell builtins
114 # nothing for: alias, break, continue, dirs, echo, eval, exit, getopts,
115 # let, logout, popd, printf, pwd, return, shift, suspend, test, times,
119 complete -f -- . source
120 complete -A enabled builtin
123 # this isn't exactly right yet -- needs to skip shell functions and
124 # do $PATH lookup (or do compgen -c and filter out matches that also
125 # appear in compgen -A function)
128 # could add -S '=', but that currently screws up because readline appends
129 # a space unconditionally
131 complete -v export local readonly
132 complete -A helptopic help # currently same as builtins
136 complete -A shopt shopt
144 # Job control builtins: fg, bg, disown, kill, wait
148 complete -A stopped -P '%' bg
149 complete -j -P '%' fg jobs disown
151 # this is not quite right at this point
156 cur=${COMP_WORDS[COMP_CWORD]}
159 %*) COMPREPLY=( $(compgen -A running -P '%' ${cur#?} ) ) ;;
160 [0-9]*) COMPREPLY=( $(jobs -p | grep ^${cur}) ) ;;
161 *) COMPREPLY=( $(compgen -A running -P '%') $(jobs -p) )
165 complete -F _wait_func wait
168 # more complicated things, several as yet unimplemented
171 #complete -F _bind_func bind
175 local cur prev nflag opts
177 cur=${COMP_WORDS[COMP_CWORD]}
178 prev=${COMP_WORDS[COMP_CWORD-1]}
181 if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
182 COMPREPLY=(-a -f -F -i -p -r -t -x)
185 if [[ $cur == '+' ]]; then
189 if [[ $prev == '-p' ]]; then
190 COMPREPLY=( $(compgen -v $cur) )
195 complete -F _declare_func declare typeset
199 local cur prev nflag opts
201 cur=${COMP_WORDS[COMP_CWORD]}
202 prev=${COMP_WORDS[COMP_CWORD-1]}
205 if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
206 COMPREPLY=(-a -d -f -n -p -s)
209 if [[ $prev == '-f' ]]; then
210 COMPREPLY=( $( compgen -f $cur ) )
213 for opts in "${COMP_WORDS[@]}" ; do
214 if [[ $opts == -*n* ]]; then nflag=1; fi
217 if [ -z "$nflag" ] ; then
218 COMPREPLY=( $( compgen -A enabled $cur ) )
220 COMPREPLY=( $( compgen -A disabled $cur ) )
224 complete -F _enable_func enable
230 cur=${COMP_WORDS[COMP_CWORD]}
231 prev=${COMP_WORDS[COMP_CWORD-1]}
233 if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
237 if [[ $prev != -*a* ]]; then
238 COMPREPLY=( $( compgen -c $cur ) )
243 complete -F _exec_func exec
249 cur=${COMP_WORDS[COMP_CWORD]}
250 prev=${COMP_WORDS[COMP_CWORD-1]}
252 if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
253 COMPREPLY=(-e -n -l -r -s)
256 if [[ $prev == -*e ]]; then
257 COMPREPLY=( $(compgen -c $cur) )
262 complete -F _fc_func fc
268 cur=${COMP_WORDS[COMP_CWORD]}
269 prev=${COMP_WORDS[COMP_CWORD-1]}
271 if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
276 if [[ $prev == '-p' ]]; then
277 COMPREPLY=( $( compgen -f $cur ) )
280 COMPREPLY=( $( compgen -c $cur ) )
283 complete -F _hash_func hash
289 cur=${COMP_WORDS[COMP_CWORD]}
290 prev=${COMP_WORDS[COMP_CWORD-1]}
293 if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
294 COMPREPLY=(-a -c -d -n -r -w -p -s)
297 if [[ $prev == -[anrw] ]]; then
298 COMPREPLY=( $( compgen -f $cur ) )
302 complete -F _history_func history
304 #complete -F _read_func read
310 cur=${COMP_WORDS[COMP_CWORD]}
311 prev=${COMP_WORDS[COMP_CWORD-1]}
315 _redir_test "$cur" "$prev" && return 0;
317 if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
318 COMPREPLY=(-a -b -e -f -k -m -n -o -p -t -u -v -x -B -C -H -P --)
321 if [[ $cur == '+' ]]; then
322 COMPREPLY=(+a +b +e +f +k +m +n +o +p +t +u +v +x +B +C +H +P)
325 if [[ $prev == [+-]o ]]; then
326 COMPREPLY=( $(compgen -A setopt $cur) )
331 complete -F _set_func set
336 cur=${COMP_WORDS[COMP_CWORD]}
338 if (( $COMP_CWORD <= 1 )) || [[ $cur == '-' ]]; then
342 COMPREPLY=( $( compgen -A signal ${cur}) )
345 complete -F _trap_func trap
348 # meta-completion (completion for complete/compgen)
350 _complete_meta_func()
357 cur=${COMP_WORDS[COMP_CWORD]}
358 prev=${COMP_WORDS[COMP_CWORD-1]}
360 _redir_test "$cur" "$prev" && return 0;
362 if (( $COMP_CWORD <= 1 )) || [[ "$cur" == '-' ]]; then
364 complete) COMPREPLY=(-a -b -c -d -e -f -j -k -s -v -u -r -p -A -G -W -P -S -X -F -C);;
365 compgen) COMPREPLY=(-a -b -c -d -e -f -j -k -s -v -u -A -G -W -P -S -X -F -C);;
370 if [[ $prev == -A ]]; then
371 COMPREPLY=(alias arrayvar binding builtin command directory \
372 disabled enabled export file 'function' helptopic hostname job keyword \
373 running service setopt shopt signal stopped variable)
375 elif [[ $prev == -F ]]; then
376 COMPREPLY=( $( compgen -A function $cur ) )
377 elif [[ $prev == -C ]]; then
378 COMPREPLY=( $( compgen -c $cur ) )
380 COMPREPLY=( $( compgen -c $cur ) )
384 complete -F _complete_meta_func complete compgen
387 # some completions for shell reserved words
389 #complete -c -k time do if then else elif '{'
397 complete -c nohup exec nice eval trace truss strace sotruss gdb
401 local mdef makef gcmd cur prev i
404 cur=${COMP_WORDS[COMP_CWORD]}
405 prev=${COMP_WORDS[COMP_CWORD-1]}
407 # if prev argument is -f, return possible filename completions.
408 # we could be a little smarter here and return matches against
409 # `makefile Makefile *.mk', whatever exists
411 -*f) COMPREPLY=( $(compgen -f $cur ) ); return 0;;
414 # if we want an option, return the possible posix options
416 -) COMPREPLY=(-e -f -i -k -n -p -q -r -S -s -t); return 0;;
419 # make reads `makefile' before `Makefile'
420 # GNU make reads `GNUmakefile' before all other makefiles, but we
421 # check that we're completing `gmake' before checking for it
422 if [ -f GNUmakefile ] && [ ${COMP_WORDS[0]} == gmake ]; then
424 elif [ -f makefile ]; then
426 elif [ -f Makefile ]; then
429 mdef=*.mk # local convention
432 # before we scan for targets, see if a makefile name was specified
434 for (( i=0; i < ${#COMP_WORDS[@]}; i++ )); do
435 if [[ ${COMP_WORDS[i]} == -*f ]]; then
436 eval makef=${COMP_WORDS[i+1]} # eval for tilde expansion
441 [ -z "$makef" ] && makef=$mdef
443 # if we have a partial word to complete, restrict completions to
444 # matches of that word
445 if [ -n "$2" ]; then gcmd='grep "^$2"' ; else gcmd=cat ; fi
447 # if we don't want to use *.mk, we can take out the cat and use
448 # test -f $makef and input redirection
449 COMPREPLY=( $(cat $makef 2>/dev/null | awk 'BEGIN {FS=":"} /^[^.# ][^=]*:/ {print $1}' | tr -s ' ' '\012' | sort -u | eval $gcmd ) )
451 complete -F _make_targets -X '+($*|*.[cho])' make gmake pmake
455 COMPREPLY=( $(mount | awk '{print $1}') )
457 complete -F _umount_func umount
471 COMPREPLY=( $("$cmd" --help | awk '{if ($1 ~ /--.*/) print $1}' | grep ^"$2" | sort -u) )
473 complete -F _configure_func configure
475 complete -W '"${GROUPS[@]}"' newgrp
477 complete -f chown ln more cat
478 complete -d mkdir rmdir
481 complete -f -X '*.gz' gzip
482 complete -f -X '*.bz2' bzip2
483 complete -f -X '*.Z' compress
484 complete -f -X '!*.+(gz|tgz|Gz)' gunzip gzcat zcat zmore
485 complete -f -X '!*.Z' uncompress zmore zcat
486 complete -f -X '!*.bz2' bunzip2 bzcat
487 complete -f -X '!*.zip' unzip
488 complete -f -X '!*.+(gif|jpg|jpeg|GIF|JPG|JPEG|bmp)' xv
490 complete -f -X '!*.pl' perl perl5
492 complete -A hostname rsh telnet rlogin ftp ping xping host traceroute nslookup
493 complete -A hostname rxterm rxterm3 rxvt2
496 complete -g newgrp groupdel groupmod
498 complete -f -X '!*.+(ps|PS)' gs gv ghostview psselect pswrap
499 complete -f -X '!*.+(dvi|DVI)' dvips xdvi dviselect dvitype catdvi
500 complete -f -X '!*.+(pdf|PDF)' acroread4
501 complete -f -X '!*.texi*' makeinfo texi2dvi texi2html
502 complete -f -X '!*.+(tex|TEX)' tex latex slitex
504 complete -f -X '!*.+(mp3|MP3)' mpg123
505 complete -f -X '!*.+(htm|html)' links w3m lynx
508 # other possibilities, left as exercises
510 #complete -F _find_func find
511 #complete -F _man_func man
512 #complete -F _stty_func stty