zsh-help 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #
  2. # Provides a much easier way to search and access ZSH's manual. First checks for
  3. # terms at the start of the manual, then checks if it's at start of a line allowing
  4. # whitespace.
  5. #
  6. # Authors:
  7. # Samantha McVey <samantham@posteo.net>
  8. #
  9. # function zsh-help {
  10. local usage="$(
  11. cat <<EOF
  12. usage: $0 [--help] [--zsh-help-debug] [--all] search term(s)
  13. Options:
  14. --all - search for the term anywhere, not just at the start of a line.
  15. --help - show this help message
  16. --zsh-help-debug - print out the regex search choosenq instead of searching
  17. Looks up things in the zsh documentation. --all must come after --zsh-help-debug
  18. if used together.
  19. Uses less as the pager. Press 'n' to search forward 'N' to search backwards.
  20. Case is ignored unless capital letters appear in the search term.
  21. EOF
  22. )"
  23. #function zsh-help {
  24. function _zsh-help-join { # Joins the arguments into a string delimited by $separator
  25. local separator=$1;
  26. local arr=$*;
  27. arr=${arr:${#separator}+1}; # < Line needed so result doesn't start with
  28. arr=${arr// /$separator}; # a separator.
  29. <<<$arr
  30. }
  31. local case='-i'; local section='ZSHALL'; local debug=''; local pattern=''
  32. function _zsh-help-try-query {
  33. local case="$1"; local pattern="$2"; local i=''
  34. local array=( ZSHBUILTINS ZSHALL ZSHMODULES )
  35. for i in ${array}; do
  36. if [[ ${debug} ]]; then printf "Looking in %s for: %s %s\n" "${i}" "${case}" "${pattern}" 1>&2; fi
  37. if man --pager='' ${i} | grep -E ${case} "${pattern}" > /dev/null; then
  38. printf "%s" "${i}"; return 0;
  39. fi
  40. done
  41. return 1
  42. }
  43. # By default search only things at start of line
  44. local first_prefix='^'
  45. local prefix='^\s*'
  46. if [[ ${1} == '--zsh-help-debug' ]]; then
  47. shift; debug=1
  48. fi
  49. if [[ ${1} == "--all" ]]; then
  50. shift; first_prefix='' # We're searching everything, so remove the prefix
  51. fi
  52. if [[ $# < 1 || $1 == "--help" ]]; then
  53. printf "%s\n" "${usage}"
  54. unfunction _zsh-help-join; unfunction _zsh-help-try-query; # unfunction so it's not in the global scope
  55. return 1
  56. fi
  57. if [[ ${1} == "test" && $# == 1 ]]; then
  58. case=''
  59. pattern='^CONDITIONAL EXPRESSIONS$'
  60. elif [[ ($1 == "-eq" || $1 == "-ne" || $1 == "-lt" || $1 == "-gt" || $1 == "-le" || $1 == "-ge") && $# == 1 ]]; then
  61. case=''
  62. pattern="${prefix}exp1\s+${1}\s+exp2"
  63. elif [[ $1 == 'zstyle' ]]; then
  64. pattern=$(_zsh-help-join '\s+' "$@")
  65. section=ZSHMODULES
  66. fi
  67. # If it wasn't one of the special-cased things, check ZSHBUILTINS first. If
  68. # not found there, we will search ZSHALL
  69. if [[ ${pattern} == "" ]]; then
  70. pattern="$(_zsh-help-join '\s+' "$@")"
  71. # search for sections at the start of the man page first
  72. section=$(_zsh-help-try-query "${case}" "${first_prefix}${pattern}")
  73. # If it exists there, keep ZSHBUILTINS as the section
  74. if (( $? == 0 )); then
  75. pattern="${first_prefix}${pattern}"
  76. elif [[ "${prefix}" ]]; then
  77. # if not found, search for the term preceeded by whitetext
  78. section=$(_zsh-help-try-query "${case}" "${prefix}${pattern}")
  79. if (( $? == 0 )); then
  80. pattern="${prefix}${pattern}"
  81. else
  82. pattern=""
  83. fi
  84. fi
  85. if [[ ! ${pattern} ]]; then # Otherwise we use zshall
  86. printf "Can't find term\n" 2>&1
  87. unfunction _zsh-help-join; unfunction _zsh-help-try-query; # unfunction so it's not in the global scope
  88. return 1;
  89. fi
  90. fi
  91. local command="man --pager=\"less ${case} -p '${pattern}'\" \"${section}\""
  92. if [[ ${debug} ]]; then
  93. printf "\nFinal search term is:\n"; printf "%s\n" "${command}";
  94. else
  95. eval $command
  96. fi
  97. local rtrn=$?
  98. unfunction _zsh-help-join; unfunction _zsh-help-try-query; # unfunction so it's not in the global scope
  99. return $?
  100. #}