_scd 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #compdef scd
  2. #description smart change directory
  3. local curcontext="$curcontext" state line expl ret=1
  4. typeset -A opt_args
  5. local -a indexopts myargs
  6. indexopts=( --add -a --unindex )
  7. myargs=(
  8. # common options
  9. "(--help -h)"{--help,-h}"[print help and exit]"
  10. # options for manipulating directory index
  11. - index
  12. "(--recursive -r)"{--recursive,-r}"[use recursive --add or --unindex]"
  13. "($indexopts)"{--add,-a}"[add specified directories to the index]"
  14. "($indexopts)--unindex[remove specified directories from the index]"
  15. "*:directory:{ (( ${words[(I)-a|--add|--unindex]} )) && _path_files -/ }"
  16. # define new directory alias
  17. - alias
  18. "--alias=[create alias for this or given directory]:directory-alias:()"
  19. '1:directory:{ (( words[(I)--alias*] )) && _path_files -/ }'
  20. # remove definition of directory alias
  21. - unalias
  22. "--unalias[remove definition of directory alias]"
  23. "*::directory alias:->scd-alias-target"
  24. # act on the directory change
  25. - scd
  26. "(--all -A)"{--all,-A}"[include less likely and ignored paths]"
  27. "--list[print matching directories and exit]"
  28. "(--verbose -v)"{--verbose,-v}"[show directory ranking and full paths]"
  29. "(--push -p)"{--push,-p}"[change directory with 'pushd']"
  30. "1::directory alias:->scd-alias-target"
  31. "*:patterns:()"
  32. )
  33. _arguments -S -C $myargs && ret=0
  34. if [[ "$state" == scd-alias-target && -s ~/.scdalias.zsh ]]; then
  35. local -a scdaliases
  36. scdaliases=( )
  37. eval "$(setopt extendedglob
  38. phome="(#b)(#s)${HOME}(/*)#(#e)"
  39. builtin hash -dr
  40. source ~/.scdalias.zsh &&
  41. for k v in ${(kv)nameddirs}; do
  42. scdaliases+=( $k:${v/${~phome}/"~"${match[1]}} )
  43. done
  44. complete_unalias=${+opt_args[unalias---unalias]}
  45. if (( complete_unalias && ! ${+nameddirs[OLD]} )); then
  46. scdaliases+=( 'OLD:all aliases to non-existent paths' )
  47. fi
  48. typeset -p scdaliases )"
  49. _describe -t scdaliases scdalias scdaliases
  50. fi