From 808d9d3579270898b17b7dbf9d1ec91145aefd03 Mon Sep 17 00:00:00 2001 From: Samantha McVey Date: Wed, 6 Sep 2017 11:11:19 -0700 Subject: [PATCH] Add zsh-help function for easily searching the zsh documentation (#1360) * Add zsh-help function for easily searching the zsh documentation Looks up things in the zsh documentation. Usage: zsh-help [--all] search term(s) Option --all will seach for the term anywhere, not just at the start of a line. When not using --all it will search nicely for terms at the beginning of the line, which in the zsh man pages is where terms that are explained are located, allowing you to search the zsh man pages easily. * Improve zsh-help to search section headings before other text Provides a much easier way to search and access ZSH's manual. First checks for terms at the start of the manual, then checks if it's at start of a line allowing whitespace. Clean up some of the code a bit and format it to have a proper header for the zprezto project with author/email and description of the function. --- modules/utility/functions/zsh-help | 102 +++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 modules/utility/functions/zsh-help diff --git a/modules/utility/functions/zsh-help b/modules/utility/functions/zsh-help new file mode 100644 index 00000000..f6605ea0 --- /dev/null +++ b/modules/utility/functions/zsh-help @@ -0,0 +1,102 @@ +# +# Provides a much easier way to search and access ZSH's manual. First checks for +# terms at the start of the manual, then checks if it's at start of a line allowing +# whitespace. +# +# Authors: +# Samantha McVey +# + +# function zsh-help { + +local usage="$( +cat <&2; fi + if man --pager='' ${i} | grep -E ${case} "${pattern}" > /dev/null; then + printf "%s" "${i}"; return 0; + fi + done + return 1 + } + # By default search only things at start of line + local first_prefix='^' + local prefix='^\s*' + if [[ ${1} == '--zsh-help-debug' ]]; then + shift; debug=1 + fi + if [[ ${1} == "--all" ]]; then + shift; first_prefix='' # We're searching everything, so remove the prefix + fi + if [[ $# < 1 || $1 == "--help" ]]; then + printf "%s\n" "${usage}" + unfunction _zsh-help-join; unfunction _zsh-help-try-query; # unfunction so it's not in the global scope + return 1 + fi + if [[ ${1} == "test" && $# == 1 ]]; then + case='' + pattern='^CONDITIONAL EXPRESSIONS$' + elif [[ ($1 == "-eq" || $1 == "-ne" || $1 == "-lt" || $1 == "-gt" || $1 == "-le" || $1 == "-ge") && $# == 1 ]]; then + case='' + pattern="${prefix}exp1\s+${1}\s+exp2" + elif [[ $1 == 'zstyle' ]]; then + pattern=$(_zsh-help-join '\s+' "$@") + section=ZSHMODULES + fi + # If it wasn't one of the special-cased things, check ZSHBUILTINS first. If + # not found there, we will search ZSHALL + if [[ ${pattern} == "" ]]; then + pattern="$(_zsh-help-join '\s+' "$@")" + # search for sections at the start of the man page first + section=$(_zsh-help-try-query "${case}" "${first_prefix}${pattern}") + # If it exists there, keep ZSHBUILTINS as the section + if (( $? == 0 )); then + pattern="${first_prefix}${pattern}" + elif [[ "${prefix}" ]]; then + # if not found, search for the term preceeded by whitetext + section=$(_zsh-help-try-query "${case}" "${prefix}${pattern}") + if (( $? == 0 )); then + pattern="${prefix}${pattern}" + else + pattern="" + fi + fi + if [[ ! ${pattern} ]]; then # Otherwise we use zshall + printf "Can't find term\n" 2>&1 + unfunction _zsh-help-join; unfunction _zsh-help-try-query; # unfunction so it's not in the global scope + return 1; + fi + fi + local command="man --pager=\"less ${case} -p '${pattern}'\" \"${section}\"" + if [[ ${debug} ]]; then + printf "\nFinal search term is:\n"; printf "%s\n" "${command}"; + else + eval $command + fi + local rtrn=$? + unfunction _zsh-help-join; unfunction _zsh-help-try-query; # unfunction so it's not in the global scope + return $? +#}