dash.plugin.zsh 3.0 KB

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