1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
# (C) 2010-2025 magicant
# Completion script for the "cd" built-in command.
# Completion function "completion/cd" is used for the "dirs", "pushd", "popd"
# built-ins as well.
function completion/cd {
typeset OPTIONS ARGOPT PREFIX
OPTIONS=( #>#
"--help"
) #<#
case ${WORDS[1]} in
(cd|pushd)
OPTIONS=("$OPTIONS" #>#
"e --ensure-pwd; exit with status 1 if the new PWD cannot be determined"
"L --logical; keep symbolic links in the \$PWD variable as is"
"P --physical; resolve symbolic links in the \$PWD variable"
"--default-directory:; specify the default directory to change to"
) #<#
if [ "${WORDS[1]}" = "pushd" ]; then
OPTIONS=("$OPTIONS" #>#
"--remove-duplicates; remove duplicates in the directory stack"
) #<#
fi
;;
(dirs)
OPTIONS=("$OPTIONS" #>#
"c --clear; clear the directory stack completely"
"v --verbose; print stack entries with their indices"
) #<#
;;
esac
typeset dirstack=false
command -f completion//parseoptions -es
case $ARGOPT in
(-)
case $TARGETWORD in
(-*[[:digit:]]*)
;;
(*)
command -f completion//completeoptions
;;
esac
dirstack=true
;;
(--default-directory)
command -f completion/cd::completedir
;;
(*)
case ${WORDS[1]} in (cd|pushd)
command -f completion/cd::completedir
esac
dirstack=true
;;
esac
if $dirstack; then
case ${WORDS[1]} in (dirs|pushd|popd)
complete -P "$PREFIX" --dirstack-index
esac
fi
}
function completion/cd::completedir {
# Part 1: complete directories relative to $PWD
# -L or -P?
typeset logical=true option
for option in "${WORDS[2,-1]}"; do
case $option in
(-L|--logical) logical=true ;;
(-P|--physical) logical=false ;;
(--) break ;;
esac
done
typeset saveopts="$(set +o)"
set +o glob
typeset word="${TARGETWORD#"$PREFIX"}"
typeset basename="${word##*/}"
typeset dirname="${word%"$basename"}"
if $logical; then
typeset IFS=/
complete -T -P "$PREFIX$dirname" -S / -- $(
set -o errreturn -o glob -o nullglob
typeset CDPATH=
if [ "$dirname" ]; then
command cd -L -- "$dirname" >/dev/null 2>&1
fi
candidates=("$basename"*/)
candidates=("${candidates%/}")
printf %s "${candidates[*]}"
)
else
complete -T -P "$PREFIX" -S / -d
fi
# Part 2: complete directories relative to $CDPATH
case $word in (/*|./*|../*)
eval "$saveopts"
return
esac
set -o noglob
typeset IFS=: cdpath candidates
for cdpath in ${CDPATH-}; do
if [ "$cdpath" = "" ]; then
cdpath=.
fi
# list candidates by globbing
set -o glob -o nullglob
candidates=("${cdpath%/}/$word"*/)
# remove $cdpath and trailing slash
candidates=("${candidates%/}")
candidates=("${candidates##"${cdpath%/}/"}")
complete -T -P "$PREFIX$dirname" -S / \
-- "${candidates#"$dirname"}"
done
eval "$saveopts"
}
# vim: set ft=sh ts=8 sts=8 sw=8 et:
|