dash.plugin.zsh 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Usage: dash [keyword:]query
  2. dash() { open -a Dash.app dash://"$*" }
  3. compdef _dash dash
  4. _dash() {
  5. # No sense doing this for anything except the 2nd position and if we haven't
  6. # specified which docset to query against
  7. if [[ $CURRENT -ne 2 || "$words[2]" =~ ":" ]]; then
  8. return
  9. fi
  10. local -aU docsets
  11. docsets=()
  12. # Use defaults to get the array of docsets from preferences
  13. # Have to smash it into one big line so that each docset is an element of our docsets array
  14. # Only output docsets that are actually enabled
  15. local -a enabled_docsets
  16. enabled_docsets=("${(@f)$(defaults read com.kapeli.dashdoc docsets \
  17. | tr -d '\n' | grep -oE '\{.*?\}' | grep -E 'isEnabled = 1;')}")
  18. local docset name keyword
  19. # Now get each docset and output each on their own line
  20. for docset in "$enabled_docsets[@]"; do
  21. keyword=''
  22. # Order of preference as explained to me by @kapeli via email
  23. for locator in keyword suggestedKeyword platform; do
  24. # Echo the docset, try to find the appropriate keyword
  25. # Strip doublequotes and colon from any keyword so that everything has the
  26. # same format when output (we'll add the colon in the completion)
  27. if [[ "$docset" =~ "$locator = ([^;]*);" ]]; then
  28. keyword="${match[1]//[\":]}"
  29. fi
  30. if [[ -z "$keyword" ]]; then
  31. continue
  32. fi
  33. # if we fall back to platform, we should do some checking per @kapeli
  34. if [[ "$locator" == "platform" ]]; then
  35. # Since these are the only special cases right now, let's not do the
  36. # expensive processing unless we have to
  37. if [[ "$keyword" = (python|java|qt|cocos2d) ]]; then
  38. if [[ "$docset" =~ "docsetName = ([^;]*);" ]]; then
  39. name="${match[1]//[\":]}"
  40. case "$keyword" in
  41. python)
  42. case "$name" in
  43. "Python 2") keyword="python2" ;;
  44. "Python 3") keyword="python3" ;;
  45. esac ;;
  46. java)
  47. case "$name" in
  48. "Java SE7") keyword="java7" ;;
  49. "Java SE6") keyword="java6" ;;
  50. "Java SE8") keyword="java8" ;;
  51. esac ;;
  52. qt)
  53. case "$name" in
  54. "Qt 5") keyword="qt5" ;;
  55. "Qt 4"|Qt) keyword="qt4" ;;
  56. esac ;;
  57. cocos2d)
  58. case "$name" in
  59. Cocos3D) keyword="cocos3d" ;;
  60. esac ;;
  61. esac
  62. fi
  63. fi
  64. fi
  65. # Bail once we have a match
  66. break
  67. done
  68. # If we have a keyword, add it to the list!
  69. if [[ -n "$keyword" ]]; then
  70. docsets+=($keyword)
  71. fi
  72. done
  73. # special thanks to [arx] on #zsh for getting me sorted on this piece
  74. compadd -qS: -- "$docsets[@]"
  75. }