# # ~/share/bash/jhg-completion --- # # Author: # URL: http://www.mahalito.net/~harley/sw/bash/jhg-completion # $Id: jhg-completion,v 1.22 2005/04/07 22:25:24 harley Exp $ # # Commentary: Tab completion in a shell is a great thing. # The following bash functions customize tab completion to # be even better. Refer to "man bash" for more info -- # these are examples. # "Its tab-ariffic!" ### check to see if completion is available in this bash. if ! type -p complete then return # dont source the rest. fi ### Basic shell completion # Unset a var (all or '-A export') complete -o default -A variable unset # only untar tarfiles complete -o default -A file -X "!*.tar*" untar # acroread only works with pdfs complete -o default -A file -X "!*.pdf" acroread ### more complicated completions # the simple version # FIGNORE is too broad a brush for CVS & RCS # it affects things other than "cd" completion. # export FIGNORE=CVS:RCS complete -o default -A directory cd # you can only cd to directories # '-X CVS' skips that dir -- type it when needed # (When completing long paths -X wont remove CVS.) function jhg-complete-cd () { local word=${COMP_WORDS[$COMP_CWORD]} if [ "${word:0:1}" = '$' ] ; then COMPREPLY=($(compgen -A variable -P '$' "${word:1}")) else COMPREPLY=($(compgen -A directory -S "/" "${word}")) #local dir=${word%/*} #echo "dir=$dir" #COMPREPLY=(${COMPREPLY[@]/#${dir}}) # remove undesireable endings (need better regexps) # or a better compgen program COMPREPLY=(${COMPREPLY[@]//%CVS\/}) COMPREPLY=(${COMPREPLY[@]//%RCS\/}) fi } # "nospace" so the directories run together # complete -o dirnames -o nospace -F jhg-complete-cd cd # CVS = The full command list is commented out in favor of the short list. #jhg_complete_cvs_cmds="add admin checkout commit diff export history \ # import log rdiff release remove rtag status tag update" jhg_complete_cvs_cmds="add checkout commit diff history log status update" function jhg-complete-cvs () { local word=${COMP_WORDS[$COMP_CWORD]} # one command, then files if [ "$COMP_CWORD" = "1" ] ; then COMPREPLY=($(compgen -W "${jhg_complete_cvs_cmds}" "${word}")) else COMPREPLY=($(compgen -A file "${word}")) # Files fi } complete -F jhg-complete-cvs cvs # Signal a job function jhg-complete-kill () { local word=${COMP_WORDS[$COMP_CWORD]} if [ "$COMP_CWORD" = "1" ] ; then # first arg is the signal -- change dash to SIG word=${word//-/SIG} word=$(echo $word | tr "[:lower:]" "[:upper:]") COMPREPLY=$(compgen -A signal ${word}) COMPREPLY=(${COMPREPLY[@]/SIG/-}) # and back else # rest are jobs word=${word/#%} # rip off leading "%" COMPREPLY=($(compgen -A job -P '%' "${word}")) fi } complete -F jhg-complete-kill kill # Take a peek in the Makefile for targets. jhg_complete_makefile="Makefile" # name to use jhg_complete_make_speed="slow" # slow or fast jhg_complete_make_pgm="make" # name of make jhg_complete_make_filter='if (/^(\w[^ :=]+):/) { print "$1 " }' # function jhg-complete-make () { local word=${COMP_WORDS[$COMP_CWORD]} local targets if [ -f "${jhg_complete_makefile}" ] ; then if [ "${jhg_complete_make_speed}" = "slow" ] ; then # runs Makefile through "make" to get the full list. targets=$("${jhg_complete_make_pgm}" -p -r -n \ -f "${jhg_complete_makefile}" | \ perl -n -e "${jhg_complete_make_filter}" ) else # do it fast (no expansion) targets=$(cat "${jhg_complete_makefile}" | \ perl -n -e "${jhg_complete_make_filter}") fi fi COMPREPLY=($(compgen -W "$targets" "$word")) } # associate it with make complete -o default -F jhg-complete-make make ### jhg specific... edit my elisp. jhg_complete_elisp_dir=~/share/emacs/jhg/ # complete on the name -- without the path function jhg-complete-edit-elisp () { local word=${COMP_WORDS[$COMP_CWORD]} if [ "${word}" != "" ] ; then COMPREPLY=( ${jhg_complete_elisp_dir}$word* ) else COMPREPLY=( ${jhg_complete_elisp_dir}/*.el ) fi COMPREPLY=( ${COMPREPLY[@]//${jhg_complete_elisp_dir}} ) } # supply the path function jhg-edit-elisp () { emacs -nw "${jhg_complete_elisp_dir}/$1" } complete -F jhg-complete-edit-elisp jhg-edit-elisp # Local Variables: # mode: ksh # End: