_wd.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #compdef wd
  2. zstyle ':completion:*: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 CONFIG=$HOME/.warprc
  9. local ret=1
  10. local -a commands
  11. local -a warp_points
  12. warp_points=( "${(f)mapfile[$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 < $CONFIG
  23. commands=(
  24. 'add:Adds the current working directory to your warp points'
  25. 'add!:Overwrites existing warp point'
  26. 'rm:Removes the given warp point'
  27. 'list:Outputs all stored warp points'
  28. 'ls:Show files from given warp point'
  29. 'path:Show path to given warp point'
  30. 'show:Outputs all warp points that point to the current directory or shows a specific target directory for a point'
  31. 'help:Show this extremely helpful text'
  32. 'clean:Remove points warping to nonexistent directories'
  33. 'clean!:Remove nonexistent directories without confirmation'
  34. '..:Go back to last directory'
  35. )
  36. _arguments -C \
  37. '1: :->first_arg' \
  38. '2: :->second_arg' && ret=0
  39. local target=$words[2]
  40. case $state in
  41. first_arg)
  42. _describe -t warp_points "Warp points" warp_points && ret=0
  43. _describe -t commands "Commands" commands && ret=0
  44. ;;
  45. second_arg)
  46. case $target in
  47. add\!|rm)
  48. _describe -t points "Warp points" warp_points && ret=0
  49. ;;
  50. add)
  51. _message 'Write the name of your warp point' && ret=0
  52. ;;
  53. show)
  54. _describe -t points "Warp points" warp_points && ret=0
  55. ;;
  56. ls)
  57. _describe -t points "Warp points" warp_points && ret=0
  58. ;;
  59. path)
  60. _describe -t points "Warp points" warp_points && ret=0
  61. ;;
  62. *)
  63. # complete sub directories from the warp point
  64. _path_files -W "(${points[$target]})" -/ && ret=0
  65. ;;
  66. esac
  67. ;;
  68. esac
  69. return $ret
  70. }
  71. _wd "$@"
  72. # Local Variables:
  73. # mode: Shell-Script
  74. # sh-indentation: 2
  75. # indent-tabs-mode: nil
  76. # sh-basic-offset: 2
  77. # End:
  78. # vim: ft=zsh sw=2 ts=2 et