Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tresni
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tresni/dff8d00e3237016d26ef to your computer and use it in GitHub Desktop.
Save tresni/dff8d00e3237016d26ef to your computer and use it in GitHub Desktop.
# Usage: dash [keyword:]query
dash() { open dash://"$*" }
compdef _dash dash
# special thanks to [arx] in #zsh for helping me get this working
_dash() {
# No sense doing this for anything except the 2nd position
if (( CURRENT == 2 )); then
local -a _all_docsets
_all_docsets=()
# Use defaults to get the array of docsets from preferences
# Have to smash it into one big line so that each docset is an element of
# our DOCSETS array
DOCSETS=("${(@f)$(defaults read com.kapeli.dash docsets | tr -d '\n' | grep -oE '\{.*?\}')}")
# remove all newlines since defaults prints so pretty like
# Now get each docset and output each on their own line
for doc in "$DOCSETS[@]"; do
# Only output docsets that are actually enabled
if [[ "`echo $doc | grep -Eo \"isEnabled = .*?;\" | sed 's/[^01]//g'`" == "0" ]]; then
continue
fi
keyword=''
# In order of preference, keyword will be set if the user specifies a custom
# handler, pluginKeyword is for cheatsheets, docsetBundle seems to work
KEYWORD_LOCATORS=(keyword pluginKeyword docsetBundle)
for i in "$KEYWORD_LOCATORS[@]"; do
# Echo the docset, try to find the appropriate keyword
# Strip doublequotes and colon from any keyword so that everything has the
# same format when output (we'll add the colon in the completion)
keyword=`echo $doc | grep -Eo "$i = .*?;" | sed -e "s/$i = \(.*\);/\1/" -e "s/[\":]//g"`
if [[ ! -z "$keyword" ]]; then
# oddity: Python 2/Python 3 docsets both report "python"
if [[ "$i" == "docsetBundle" && "$keyword" == "python" ]]; then
version=`echo $doc | grep -Eo 'docsetName = "Python [0-9]+' | sed 's/[^0-9]//g'`
keyword="$keyword$version"
fi
#echo "found $i: $keyword"
break
fi
done
# If we have a keyword, add it to the list!
if [[ ! -z "$keyword" ]]; then
_all_docsets+=($keyword)
fi
done
compadd -X 'dash docsets' -qS: -- "$_all_docsets[@]"
return
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment