dash.plugin.zsh 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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" || "$keyword" == "java" || \
  35. "$keyword" == "qt" || "$keyword" == "cocs2d" ]]; then
  36. docsetName=`echo $doc | grep -Eo "docsetName = .*?;" | sed -e "s/docsetName = \(.*\);/\1/" -e "s/[\":]//g"`
  37. if [[ "$keyword" == "python" ]]; then
  38. if [[ "$docsetName" == "Python 2" ]]; then
  39. keyword="python2"
  40. elif [[ "$docsetName" == "Python 3" ]]; then
  41. keyword="python3"
  42. fi
  43. elif [[ "$keyword" == "java" ]]; then
  44. if [[ "$docsetName" == "Java SE7" ]]; then
  45. keyword="java7"
  46. elif [[ "$docsetName" == "Java SE6" ]]; then
  47. keyword="java6"
  48. elif [[ "$docsetName" == "Java SE8" ]]; then
  49. keyword="java8"
  50. fi
  51. elif [[ "$keyword" == "qt" ]]; then
  52. if [[ "$docsetName" == "Qt 5" ]]; then
  53. keyword="qt5"
  54. elif [[ "$docsetName" == "Qt 4" ]]; then
  55. keyword="qt4"
  56. elif [[ "$docsetName" == "Qt" ]]; then
  57. keyword="qt4"
  58. fi
  59. elif [[ "$keyword" == "cocos2d" ]]; then
  60. if [[ "$docsetName" == "Cocos3D" ]]; then
  61. keyword="cocos3d"
  62. fi
  63. fi
  64. fi
  65. fi
  66. # Bail once we have a match
  67. break
  68. fi
  69. done
  70. # If we have a keyword, add it to the list!
  71. if [[ ! -z "$keyword" ]]; then
  72. _all_docsets+=($keyword)
  73. fi
  74. done
  75. # special thanks to [arx] on #zsh for getting me sorted on this piece
  76. compadd -qS: -- "$_all_docsets[@]"
  77. return
  78. fi
  79. }