Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 1 | # cdfunc - example completion function for cd |
| 2 | # |
| 3 | # based on the cd completion function from the bash_completion package |
| 4 | # |
| 5 | # Chet Ramey <chet.ramey@case.edu> |
| 6 | # |
| 7 | # Copyright 2011 Chester Ramey |
| 8 | # |
| 9 | # This program is free software; you can redistribute it and/or modify |
| 10 | # it under the terms of the GNU General Public License as published by |
| 11 | # the Free Software Foundation; either version 2, or (at your option) |
| 12 | # any later version. |
| 13 | # |
| 14 | # TThis program is distributed in the hope that it will be useful, |
| 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | # GNU General Public License for more details. |
| 18 | # |
| 19 | # You should have received a copy of the GNU General Public License |
| 20 | # along with this program; if not, write to the Free Software Foundation, |
| 21 | # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 22 | |
| 23 | _comp_cd() |
| 24 | { |
| 25 | local IFS=$' \t\n' # normalize IFS |
| 26 | local cur _skipdot _cdpath |
| 27 | local i j k |
| 28 | |
| 29 | # Tilde expansion, with side effect of expanding tilde to full pathname |
| 30 | case "$2" in |
| 31 | \~*) eval cur="$2" ;; |
| 32 | *) cur=$2 ;; |
| 33 | esac |
| 34 | |
| 35 | # no cdpath or absolute pathname -- straight directory completion |
| 36 | if [[ -z "${CDPATH:-}" ]] || [[ "$cur" == @(./*|../*|/*) ]]; then |
| 37 | # compgen prints paths one per line; could also use while loop |
| 38 | IFS=$'\n' |
| 39 | COMPREPLY=( $(compgen -d -- "$cur") ) |
| 40 | IFS=$' \t\n' |
| 41 | # CDPATH+directories in the current directory if not in CDPATH |
| 42 | else |
| 43 | IFS=$'\n' |
| 44 | _skipdot=false |
| 45 | # preprocess CDPATH to convert null directory names to . |
| 46 | _cdpath=${CDPATH/#:/.:} |
| 47 | _cdpath=${_cdpath//::/:.:} |
| 48 | _cdpath=${_cdpath/%:/:.} |
| 49 | for i in ${_cdpath//:/$'\n'}; do |
| 50 | if [[ $i -ef . ]]; then _skipdot=true; fi |
| 51 | k="${#COMPREPLY[@]}" |
| 52 | for j in $( compgen -d -- "$i/$cur" ); do |
| 53 | COMPREPLY[k++]=${j#$i/} # cut off directory |
| 54 | done |
| 55 | done |
| 56 | $_skipdot || COMPREPLY+=( $(compgen -d -- "$cur") ) |
| 57 | IFS=$' \t\n' |
| 58 | fi |
| 59 | |
| 60 | # variable names if appropriate shell option set and no completions |
| 61 | if shopt -q cdable_vars && [[ ${#COMPREPLY[@]} -eq 0 ]]; then |
| 62 | COMPREPLY=( $(compgen -v -- "$cur") ) |
| 63 | fi |
| 64 | |
| 65 | # append slash to passed directory name that is the only completion. |
| 66 | # readline will not do this if we complete from CDPATH |
| 67 | if [[ ${#COMPREPLY[@]} -eq 1 ]]; then |
| 68 | i=${COMPREPLY[0]} # shorthand |
| 69 | if [[ "$cur" == "$i" ]] && [[ "$i" != "*/" ]]; then |
| 70 | COMPREPLY[0]+=/ |
| 71 | fi |
| 72 | fi |
| 73 | return 0 |
| 74 | } |
| 75 | |
| 76 | complete -o filenames -o nospace -o bashdefault -F _comp_cd cd |