home *** CD-ROM | disk | FTP | other *** search
/ chilidog.highland.cc.ks.us / chilidog.highland.cc.ks.us.zip / chilidog.highland.cc.ks.us / backup / bradford.20110725.etc.tar.gz / bradford.20110725.etc.tar / etc / profile.d / complete.bash < prev    next >
Text File  |  2006-04-10  |  8KB  |  282 lines

  1. # /etc/profile.d/complete.bash for SuSE Linux
  2. #
  3. #
  4. # This feature has its own file because some other shells
  5. # do not like the way how the bash assigns arrays
  6. #
  7. # REQUIRES bash 2.0 and higher
  8. #
  9.  
  10. _def=; _dir=; _file=; _nosp=
  11. if complete -o default _nullcommand &> /dev/null ; then
  12.     _def="-o default"
  13.     _dir="-o dirnames"
  14.     _file="-o filenames"
  15. fi
  16. _minusdd="-d ${_dir}"
  17. _minusdf="-d ${_file}"
  18. if complete -o nospace _nullcommand &> /dev/null ; then
  19.     _nosp="-o nospace"
  20.     _minusdd="${_nosp} ${_dir}"
  21.     _minusdf="${_nosp} ${_dir}"
  22. fi
  23. complete -r _nullcommand &> /dev/null
  24.  
  25. # Expanding shell function for directories
  26. function _cd_ ()
  27. {
  28.     local c=${COMP_WORDS[COMP_CWORD]}
  29.     local s g=0 x C
  30.     local IFS='
  31. '
  32.     shopt -q extglob && g=1
  33.     test $g -eq 0 && shopt -s extglob
  34.  
  35.     case "$(complete -p ${1##*/} 2> /dev/null)" in
  36.     mkdir)    ;;
  37.     *) s="-S/"  
  38.     esac
  39.  
  40.     case "$c" in
  41.     \$\(*\))    eval COMPREPLY=\(${c}\) ;;
  42.     \$\(*)    COMPREPLY=($(compgen -c -P '$(' -S ')'    -- ${c#??}))    ;;
  43.     \`*\`)    eval COMPREPLY=\(${c}\) ;;
  44.     \`*)    COMPREPLY=($(compgen -c -P '\`' -S '\`' -- ${c#?}))    ;;
  45.     \$\{*\})    eval COMPREPLY=\(${c}\) ;;
  46.     \$\{*)    COMPREPLY=($(compgen -v -P '${' -S '}'    -- ${c#??}))    ;;
  47.     \$*)    COMPREPLY=($(compgen -v -P '$' $s    -- ${c#?}))    ;;
  48.     \~*/*)    COMPREPLY=($(compgen -d $s        -- "${c}"))    ;;
  49.     \~*)    COMPREPLY=($(compgen -u $s        -- "${c}"))    ;;
  50.     esac
  51.  
  52.     # Escape spaces and braces in path names with `\'
  53.     for x in ${COMPREPLY[@]} ; do
  54.     x=${x// /\\ }
  55.     x=${x//(/\\(}
  56.     x=${x//)/\\)}
  57.     x=${x//\{/\\\{}
  58.     x=${x//\}/\\\}}
  59.     x=${x//\[/\\\[}
  60.     x=${x//\]/\\\]}
  61.     C=(${C[@]} $x)
  62.     done
  63.     COMPREPLY=(${C[@]})
  64.  
  65.     test $g -eq 0 && shopt -u extglob
  66. }
  67.  
  68. complete ${_minusdd} -F _cd_        cd rmdir pushd chroot chrootx
  69. complete ${_minusdf} -F _cd_        mkdir
  70.  
  71. # General expanding shell function
  72. _exp_ ()
  73. {
  74.     # bash `complete' is broken because you can not combine
  75.     # -d, -f, and -X pattern without missing directories.
  76.     local c=${COMP_WORDS[COMP_CWORD]}
  77.     local a="${COMP_LINE}"
  78.     local e s g=0 cd dc t=""
  79.     local IFS
  80.  
  81.     shopt -q extglob && g=1
  82.     test $g -eq 0 && shopt -s extglob
  83.     # Don't be fooled by the bash parser if extglob is off by default
  84.     cd='*-?(c)d*'
  85.     dc='*-d?(c)*'
  86.  
  87.     case "${1##*/}" in
  88.     compress)        e='*.Z'                    ;;
  89.     bzip2)
  90.     case "$c" in
  91.     -)        COMPREPLY=(d c)
  92.             test $g -eq 0 && shopt -u extglob
  93.             return                    ;;
  94.      -?|-??)        COMPREPLY=($c)
  95.             test $g -eq 0 && shopt -u extglob
  96.             return                    ;;
  97.     esac
  98.     case "$a" in
  99.     $cd|$dc)    e='!*.bz2'                ;;
  100.     *)        e='*.bz2'                ;;
  101.     esac                            ;;
  102.     bunzip2)        e='!*.bz2'                ;;
  103.     gzip)
  104.     case "$c" in
  105.     -)        COMPREPLY=(d c)
  106.             test $g -eq 0 && shopt -u extglob
  107.             return                    ;;
  108.      -?|-??)        COMPREPLY=($c)
  109.             test $g -eq 0 && shopt -u extglob
  110.             return                    ;;
  111.     esac
  112.     case "$a" in
  113.     $cd|$dc)    e='!*.+(gz|tgz|z|Z)'            ;;
  114.     *)        e='*.+(gz|tgz|z|Z)'            ;;
  115.     esac                            ;;
  116.     gunzip)        e='!*.+(gz|tgz|z|Z)'            ;;
  117.     uncompress)        e='!*.Z'                ;;
  118.     unzip)        e='!*.+(???)'
  119.             t="@(MS-DOS executable|Zip archive)*"    ;;
  120.     gs|ghostview)    e='!*.+(eps|EPS|ps|PS|pdf|PDF)'        ;;
  121.     gv|kghostview)    e='!*.+(eps|EPS|ps|PS|ps.gz|pdf|PDF)'    ;;
  122.     acroread|[xk]pdf)    e='!*.+(pdf|PDF)'            ;;
  123.     dvips)        e='!*.+(dvi|DVI)'            ;;
  124.     [xk]dvi)        e='!*.+(dvi|dvi.gz|DVI|DVI.gz)'        ;;
  125.     tex|latex|pdflatex)    e='!*.+(tex|TEX|texi|latex)'        ;;
  126.     export)
  127.     case "$a" in
  128.     *=*)        c=${c#*=}                ;;
  129.     *)        COMPREPLY=($(compgen -v -- ${c}))
  130.             test $g -eq 0 && shopt -u extglob
  131.             return                    ;;
  132.     esac
  133.     ;;
  134.     *)            e='!*'
  135.     esac
  136.  
  137.     case "$(complete -p ${1##*/} 2> /dev/null)" in
  138.     *-d*)    ;;
  139.     *) s="-S/"
  140.     esac
  141.  
  142.     IFS='
  143. '
  144.     case "$c" in
  145.     \$\(*\))       eval COMPREPLY=\(${c}\) ;;
  146.     \$\(*)        COMPREPLY=($(compgen -c -P '$(' -S ')'  -- ${c#??}))    ;;
  147.     \`*\`)       eval COMPREPLY=\(${c}\) ;;
  148.     \`*)        COMPREPLY=($(compgen -c -P '\`' -S '\`' -- ${c#?}))    ;;
  149.     \$\{*\})       eval COMPREPLY=\(${c}\) ;;
  150.     \$\{*)        COMPREPLY=($(compgen -v -P '${' -S '}'  -- ${c#??}))    ;;
  151.     \$*)        COMPREPLY=($(compgen -v -P '$'          -- ${c#?}))    ;;
  152.     \~*/*)        COMPREPLY=($(compgen -f -X "$e"         -- ${c}))    ;;
  153.     \~*)        COMPREPLY=($(compgen -u ${s}         -- ${c}))    ;;
  154.     *@*)        COMPREPLY=($(compgen -A hostname -P '@' -S ':' -- ${c#*@})) ;;
  155.     *[*?[]*)        COMPREPLY=($(compgen -G "${c}"))            ;;
  156.     *[?*+\!@]\(*\)*)
  157.     if test $g -eq 0 ; then
  158.             COMPREPLY=($(compgen -f -X "$e" -- $c))
  159.             test $g -eq 0 && shopt -u extglob
  160.             return
  161.     fi
  162.             COMPREPLY=($(compgen -G "${c}"))            ;;
  163.     *)
  164.     if test "$c" = ".." ; then
  165.             COMPREPLY=($(compgen -d -X "$e" -S / ${_nosp} -- $c))
  166.     else
  167.             for s in $(compgen -f -X "$e" -- $c) ; do
  168.                 if test -d $s ; then
  169.                 COMPREPLY=(${COMPREPLY[@]} $(compgen -f -X "$e" -S / -- $s))
  170.                 elif test -z "$t" ; then
  171.                 COMPREPLY=(${COMPREPLY[@]} $s)
  172.                 else
  173.                 case "$(file -b $s 2> /dev/null)" in
  174.                 $t) COMPREPLY=(${COMPREPLY[@]} $s)        ;;
  175.                 esac
  176.                 fi
  177.             done
  178.     fi                                    ;;
  179.     esac
  180.     test $g -eq 0 && shopt -u extglob
  181. }
  182.  
  183. complete -d -X '.[^./]*' -F _exp_ ${_file} \
  184.                      compress \
  185.                     bzip2 \
  186.                     bunzip2 \
  187.                     gzip \
  188.                     gunzip \
  189.                     uncompress \
  190.                     unzip \
  191.                     gs ghostview \
  192.                     gv kghostview \
  193.                     acroread xpdf kpdf \
  194.                     dvips xdvi kdvi \
  195.                     tex latex pdflatex
  196. # No clean way to hande variable expansion _and_ file/dir name expansion
  197. # with the same string. So let the default expansion on for that commands.
  198. #complete -d -F _exp_ ${_def}        chown chgrp chmod chattr ln
  199. #complete -d -F _exp_ ${_def}        more cat less strip grep vi ed
  200.  
  201. complete -A function -A alias -A command -A builtin \
  202.                     type
  203. complete -A function            function
  204. complete -A alias            alias unalias
  205. complete -A variable            unset local readonly
  206. complete -F _exp_ ${_def} ${_nosp}    export
  207. complete -A variable -A export        unset
  208. complete -A shopt            shopt
  209. complete -A setopt            set
  210. complete -A helptopic            help
  211. complete -A user            talk su login sux
  212. complete -A builtin            builtin
  213. complete -A export            printenv
  214. complete -A command ${_def}        command which nohup exec nice eval 
  215. complete -A command ${_def}        ltrace strace gdb
  216. HOSTFILE=""
  217. test -s $HOME/.hosts && HOSTFILE=$HOME/.hosts
  218. complete -A hostname            ping telnet slogin rlogin \
  219.                     traceroute nslookup
  220. complete -A hostname -A directory -A file \
  221.                     rsh ssh scp
  222. complete -A stopped -P '%'        bg
  223. complete -A job -P '%'            fg jobs disown
  224.  
  225. # Expanding shell function for manual pager
  226. _man_ ()
  227. {
  228.     local c=${COMP_WORDS[COMP_CWORD]}
  229.     local o=${COMP_WORDS[COMP_CWORD-1]}
  230.     local os="- f k P S t l"
  231.     local ol="whatis apropos pager sections troff local-file"
  232.     local m s
  233.  
  234.     if test -n "$MANPATH" ; then
  235.     m=${MANPATH//:/\/man,}
  236.     else
  237.     m="/usr/X11R6/man/man,/usr/openwin/man/man,/usr/share/man/man"
  238.     fi
  239.  
  240.     case "$c" in
  241.       -) COMPREPLY=($os)    ;;
  242.     --) COMPREPLY=($ol)     ;;
  243.      -?) COMPREPLY=($c)    ;;
  244.     [0-9n]|[0-9n]p)
  245.         COMPREPLY=($c)    ;;
  246.      *)
  247.     case "$o" in
  248.         -l|--local-file)
  249.         COMPREPLY=($(compgen -f -d -X '.*' -- $c)) ;;
  250.     [0-9n]|[0-9n]p)
  251.         s=$(eval echo {${m}}$o/)
  252.         if type -p sed &> /dev/null ; then
  253.             COMPREPLY=(\
  254.             $(ls -1fUA $s 2>/dev/null|\
  255.               sed -n "/^$c/{s@\.[0-9n].*\.gz@@g;s@.*/:@@g;p;}")\
  256.             )
  257.         else
  258.             s=($(ls -1fUA $s 2>/dev/null))
  259.             s=(${s[@]%%.[0-9n]*})
  260.             s=(${s[@]#*/:})
  261.             for m in ${s[@]} ; do
  262.             case "$m" in
  263.                 $c*) COMPREPLY=(${COMPREPLY[@]} $m)
  264.             esac
  265.             done
  266.             unset m s
  267.             COMPREPLY=(${COMPREPLY[@]%%.[0-9n]*})
  268.             COMPREPLY=(${COMPREPLY[@]#*/:})
  269.         fi                       ;;
  270.          *) COMPREPLY=($(compgen -c -- $c))           ;;
  271.     esac
  272.     esac
  273. }
  274.  
  275. complete -F _man_ ${_file}        man
  276.  
  277. unset _def _dir _file _nosp _minusdd _minusdf
  278.  
  279. #
  280. # End of /etc/profile.d/complete.bash
  281. #
  282.