Parent directory

dash.plugin.zsh

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