2
0

_wd.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #compdef wd
  2. zstyle ':completion::complete:wd:*:descriptions' format '%B%d%b'
  3. zstyle ':completion::complete:wd:*:commands' group-name commands
  4. zstyle ':completion::complete:wd:*:warp_points' group-name warp_points
  5. zstyle ':completion::complete:wd::' list-grouped
  6. zmodload zsh/mapfile
  7. function _wd() {
  8. local WD_CONFIG=${WD_CONFIG:-$HOME/.warprc}
  9. local ret=1
  10. local -a commands
  11. local -a warp_points
  12. warp_points=( "${(f)mapfile[$WD_CONFIG]//$HOME/~}" )
  13. typeset -A points
  14. while read -r line
  15. do
  16. arr=(${(s,:,)line})
  17. name=${arr[1]}
  18. target_path=${arr[2]}
  19. # replace ~ from path to fix completion (#17)
  20. target_path=${target_path/#\~/$HOME}
  21. points[$name]=$target_path
  22. done < $WD_CONFIG
  23. commands=(
  24. 'add:Adds the current working directory to your warp points'
  25. 'addcd:Adds a directory to your warp points'
  26. 'add!:Overwrites existing warp point'
  27. 'export:Export warp points as static named directories'
  28. 'rm:Removes the given warp point'
  29. 'list:Outputs all stored warp points'
  30. 'ls:Show files from given warp point'
  31. 'path:Show path to given warp point'
  32. 'show:Outputs all warp points that point to the current directory or shows a specific target directory for a point'
  33. 'help:Show this extremely helpful text'
  34. 'clean:Remove points warping to nonexistent directories'
  35. 'clean!:Remove nonexistent directories without confirmation'
  36. '..:Go back to last directory'
  37. )
  38. _arguments -C \
  39. '1: :->first_arg' \
  40. '2: :->second_arg' && ret=0
  41. local target=$words[2]
  42. case $state in
  43. first_arg)
  44. _describe -t warp_points "Warp points" warp_points && ret=0
  45. _describe -t commands "Commands" commands && ret=0
  46. ;;
  47. second_arg)
  48. case $target in
  49. add\!|rm)
  50. _describe -t points "Warp points" warp_points && ret=0
  51. ;;
  52. add)
  53. _message 'Write the name of your warp point' && ret=0
  54. ;;
  55. addcd)
  56. _message 'Write the name of your path' && ret=0
  57. ;;
  58. show)
  59. _describe -t points "Warp points" warp_points && ret=0
  60. ;;
  61. ls)
  62. _describe -t points "Warp points" warp_points && ret=0
  63. ;;
  64. path)
  65. _describe -t points "Warp points" warp_points && ret=0
  66. ;;
  67. *)
  68. if [[ -v points[$target] ]]; then
  69. # complete sub directories from the warp point
  70. _path_files -W "(${points[$target]})" -/ && ret=0
  71. fi
  72. # don't complete anything if warp point is not valid
  73. ;;
  74. esac
  75. ;;
  76. esac
  77. return $ret
  78. }
  79. _wd "$@"
  80. # Local Variables:
  81. # mode: Shell-Script
  82. # sh-indentation: 2
  83. # indent-tabs-mode: nil
  84. # sh-basic-offset: 2
  85. # End:
  86. # vim: ft=zsh sw=2 ts=2 et