1#!/bin/zsh
2#
3# Figure out where to get the best help, and get it.
4#
5# Install this function by placing it in your FPATH and then
6# adding to your .zshrc the lines:
7#	unalias run-help
8#	autoload -Uz run-help
9#
10
11emulate -RL zsh
12
13local HELPDIR="${HELPDIR:-@runhelpdir@}"
14
15[[ $1 == "." ]] && 1="dot"
16[[ $1 == ":" ]] && 1="colon"
17
18# Check whether Util/helpfiles has been used to generate zsh help
19if [[ $# == 0 || $1 == "-l" ]]
20then
21    if [[ -d $HELPDIR ]]
22    then
23	echo "Here is a list of topics for which special help is available:"
24	echo ""
25	print -rc $HELPDIR/*(:t)
26    else
27	echo "There is no list of special help topics available at this time."
28    fi
29    return 0
30elif [[ -n "${HELPDIR:-}" && -r $HELPDIR/$1 && $1 != compctl ]]
31then
32    ${=PAGER:-more} $HELPDIR/$1
33    return $?
34fi
35
36# No zsh help; use "whence" to figure out where else we might look
37local what places noalias newline='
38'
39integer i=0 didman=0
40
41places=( "${(@f)$(builtin whence -va $1)}" )
42if [[ $places = *"not found"* && $1 != ${(Q)1} ]]; then
43  # Different when unquoted, so try stripping quotes.
44  places=( "${(@f)$(builtin whence -va ${(Q)1})}" )
45  if (( ${#places} )); then
46      set -- "${(Q)@}"
47  fi
48  # Quotation is significant to aliases, so suppress lookup.
49  noalias=1
50fi
51
52{
53while ((i++ < $#places))
54do
55    what=$places[$i]
56    [[ -n $noalias && $what = *" is an alias "* ]] && continue
57    builtin print -r $what
58    case $what in
59    (*( is an alias for (noglob|nocorrect))*)
60	[[ ${what[(w)7]:t} != ${what[(w)1]} ]] &&
61	  run_help_orig_cmd=${what[(w)1]} run-help ${what[(w)7]:t}
62	;;
63    (*( is an alias)*)
64	[[ ${what[(w)6]:t} != ${what[(w)1]} ]] &&
65	  run_help_orig_cmd=${what[(w)1]} run-help ${what[(w)6]:t}
66	;;
67    (*( is a * function))
68	case ${what[(w)1]} in
69	(comp*) man zshcompsys;;
70	(zf*) man zshftpsys;;
71	(run-help) man zshcontrib;;
72	(*) builtin functions ${what[(w)1]} | ${=PAGER:-more};;
73	esac;;
74    (*( is a * builtin))
75	case ${what[(w)1]} in
76	(compctl) man zshcompctl;;
77	(comp*) man zshcompwid;;
78	(bindkey|vared|zle) man zshzle;;
79	(*setopt) man zshoptions;;
80	(cap|getcap|setcap) ;&
81	(clone) ;&
82	(ln|mkdir|mv|rm|rmdir|sync) ;&
83	(sched) ;&
84	(echotc|echoti|sched|stat|zprof|zpty|zsocket|zstyle|ztcp) man zshmodules;;
85	(zftp) man zshftpsys;;
86	(*) man zshbuiltins;;
87	esac
88	;;
89    (*( is hashed to *))
90	man ${what[(w)-1]:t}
91	;;
92    (*( is a reserved word))
93	man zshmisc
94	;;
95    (*)
96	if ((! didman++))
97	then
98	    if whence "run-help-$1:t" >/dev/null
99	    then
100		local cmd_args
101		builtin getln cmd_args
102		builtin print -z "$cmd_args"
103		cmd_args=( ${(z)cmd_args} )
104		# Discard environment assignments, etc.
105		while [[ $cmd_args[1] != ${run_help_orig_cmd:-$1} ]]
106		do
107		    shift cmd_args || return 1
108		done
109		eval "run-help-$1:t ${(q@)cmd_args[2,-1]}"
110	    else
111		POSIXLY_CORRECT=1 man $@:t
112	    fi
113	fi
114	;;
115    esac
116    if ((i < $#places && ! didman))
117    then
118	builtin print -nP "%SPress any key for more help or q to quit%s"
119	builtin read -k what
120	[[ $what != $newline ]] && echo
121	[[ $what == [qQ] ]] && break
122    fi
123done
124} always {
125  unset run_help_orig_cmd
126}
127