# Start of cdmatch.
# Save in your functions directory and autoload, then do
#   compctl -K cdmatch -S '/' cd pushd
# or if you prefer
#   compctl -K cdmatch -S '/' -x 'S[/][~]' -g '*(-/)' -- cd pushd
# (to use ordinary globbing for absolute paths).
#
# Completes directories for cd, pushd, ... anything which knows about cdpath.
# Note that . is NOT automatically included.  It's up to you to put it in
# cdpath somewhere.

local dir nword args pref ngtrue

[[ -o nullglob ]] && ngtrue=1
setopt nullglob

read -nc nword
read -Ac args
pref=$args[$nword]

if [[ $pref[1] = [/\~] ]]; then
  eval "reply=($pref*(-/))"
else
  reply=()

  for dir in $cdpath
  do
    eval "reply=(\$reply $dir/$pref*(-/:s,$dir/,,))"
  done
fi

[[ $ngtrue = 1 ]] || unsetopt nullglob

return
# End of cdmatch.
