1
0
Fork 0
mirror of git://git.code.sf.net/p/zsh/code synced 2025-09-17 15:01:40 +02:00
zsh/Functions/Misc/run-help
Phil Pennock 180c4c049c 31634: run-help compat with alias to noglob/nocorrect
Given `alias fetch='noglob fetch'`, without this change `run-help fetch`
recurses to invoke itself on the noglob command, bringing up the help
for noglob.  Similarly for nocorrect.  Thus the user would have to quit
out of the pager, then avoid quitting out of the loop, so that they can
go into the second iteration and see the help for the second instance
found by `whence -a`, which happens to be the target of the alias.
With common pagers, that's thus 'q', 'not-q'.  Somewhat frustrating.

Without this change, `alias foo='noglob bar'` where `foo` is not
otherwise a command would _never_ show the help for `bar`, since it only
showed the help for `noglob` and there was no second line of whence
output to trigger the second pass.

With this change, aliases to `noglob|nocorrect` of a command somewhat
ignore the presence of that precommand modifier; if a command is aliased
to `noglob $itself`, then the result is that the first line of whence
output is shown, no pager is used, the user can immediately see a prompt
to continue and press something other-than-q to see the help for the
command.

If given `alias foo='noglob bar'` then `run-help foo` will immediately
show the help for bar.

This behaviour was chosen to be consistent with the existing alias
support, but just working better with the precommand modifier.
2013-08-07 04:11:56 -04:00

126 lines
3 KiB
Bash

#!/bin/zsh
#
# Figure out where to get the best help, and get it.
#
# Install this function by placing it in your FPATH and then
# adding to your .zshrc the lines:
# unalias run-help
# autoload -Uz run-help
#
emulate -RL zsh
local HELPDIR="${HELPDIR:-/usr/share/zsh/$ZSH_VERSION/help}"
[[ $1 == "." ]] && 1="dot"
[[ $1 == ":" ]] && 1="colon"
# Check whether Util/helpfiles has been used to generate zsh help
if [[ $# == 0 || $1 == "-l" ]]
then
if [[ -d $HELPDIR ]]
then
echo "Here is a list of topics for which special help is available:"
echo ""
print -rc $HELPDIR/*(:t)
else
echo "There is no list of special help topics available at this time."
fi
return 0
elif [[ -n "${HELPDIR:-}" && -r $HELPDIR/$1 && $1 != compctl ]]
then
${=PAGER:-more} $HELPDIR/$1
return $?
fi
# No zsh help; use "whence" to figure out where else we might look
local what places noalias newline='
'
integer i=0 didman=0
places=( "${(@f)$(builtin whence -va $1)}" )
if [[ $places = *"not found"* && $1 != ${(Q)1} ]]; then
# Different when unquoted, so try stripping quotes.
places=( "${(@f)$(builtin whence -va ${(Q)1})}" )
if (( ${#places} )); then
set -- "${(Q)@}"
fi
# Quotation is significant to aliases, so suppress lookup.
noalias=1
fi
{
while ((i++ < $#places))
do
what=$places[$i]
[[ -n $noalias && $what = *" is an alias "* ]] && continue
builtin print -r $what
case $what in
(*( is an alias for (noglob|nocorrect))*)
[[ ${what[(w)7]:t} != ${what[(w)1]} ]] &&
run_help_orig_cmd=${what[(w)1]} run-help ${what[(w)7]:t}
;;
(*( is an alias)*)
[[ ${what[(w)6]:t} != ${what[(w)1]} ]] &&
run_help_orig_cmd=${what[(w)1]} run-help ${what[(w)6]:t}
;;
(*( is a * function))
case ${what[(w)1]} in
(comp*) man zshcompsys;;
(zf*) man zshftpsys;;
(run-help) man zshcontrib;;
(*) builtin functions ${what[(w)1]} | ${=PAGER:-more};;
esac;;
(*( is a * builtin))
case ${what[(w)1]} in
(compctl) man zshcompctl;;
(comp*) man zshcompwid;;
(bindkey|vared|zle) man zshzle;;
(*setopt) man zshoptions;;
(cap|getcap|setcap) ;&
(clone) ;&
(ln|mkdir|mv|rm|rmdir|sync) ;&
(sched) ;&
(echotc|echoti|sched|stat|zprof|zpty|zsocket|zstyle|ztcp) man zshmodules;;
(zftp) man zshftpsys;;
(*) man zshbuiltins;;
esac
;;
(*( is hashed to *))
man ${what[(w)-1]:t}
;;
(*( is a reserved word))
man zshmisc
;;
(*)
if ((! didman++))
then
if whence "run-help-$1:t" >/dev/null
then
local cmd_args
builtin getln cmd_args
builtin print -z "$cmd_args"
cmd_args=( ${(z)cmd_args} )
# Discard environment assignments, etc.
while [[ $cmd_args[1] != ${run_help_orig_cmd:-$1} ]]
do
shift cmd_args || return 1
done
eval "run-help-$1:t ${(q@)cmd_args[2,-1]}"
else
POSIXLY_CORRECT=1 man $@:t
fi
fi
;;
esac
if ((i < $#places && ! didman))
then
builtin print -nP "%SPress any key for more help or q to quit%s"
builtin read -k what
[[ $what != $newline ]] && echo
[[ $what == [qQ] ]] && break
fi
done
} always {
unset run_help_orig_cmd
}