83 lines
2.3 KiB
Plaintext
Raw Normal View History

2009-06-05 08:43:11 +02:00
# man(1) completion
2009-06-05 08:43:11 +02:00
[ $USERLAND = GNU -o $UNAME = Darwin \
-o $UNAME = FreeBSD -o $UNAME = SunOS -o $UNAME = Cygwin \
-o $UNAME = OpenBSD ] &&
_man()
{
2009-10-04 19:42:50 +02:00
local cur i prev sect manpath manext mansect uname
2009-06-05 08:43:11 +02:00
2010-03-30 22:04:07 +03:00
manext="@([0-9lnp]|[0-9][px]|man|3pm)?(.@([gx]z|bz2|lzma|Z))"
2009-10-04 19:42:50 +02:00
mansect="@([0-9lnp]|[0-9][px]|3pm)"
2009-06-05 08:43:11 +02:00
2009-10-04 19:42:50 +02:00
COMPREPLY=()
_get_comp_words_by_ref cur prev
2009-10-04 19:42:50 +02:00
if [[ "$prev" == -l ]]; then
_filedir $manext
return 0
fi
2009-06-05 08:43:11 +02:00
2009-10-04 19:42:50 +02:00
_expand || return 0
2009-06-05 08:43:11 +02:00
2009-10-04 19:42:50 +02:00
# file based completion if parameter contains /
if [[ "$cur" == */* ]]; then
_filedir $manext
return 0
fi
2009-06-05 08:43:11 +02:00
2009-10-04 19:42:50 +02:00
uname=$( uname -s )
if [[ $uname == @(Linux|GNU|GNU/*|FreeBSD|Cygwin|CYGWIN_*) ]]; then
manpath=$( manpath 2>/dev/null || command man --path )
else
manpath=$MANPATH
fi
2009-06-05 08:43:11 +02:00
2009-10-04 19:42:50 +02:00
if [ -z "$manpath" ]; then
COMPREPLY=( $( compgen -c -- "$cur" ) )
return 0
fi
2009-06-05 08:43:11 +02:00
2009-10-04 19:42:50 +02:00
# determine manual section to search
[[ "$prev" == $mansect ]] && sect=$prev || sect='*'
2009-06-05 08:43:11 +02:00
2009-10-04 19:42:50 +02:00
manpath=$manpath:
if [ -n "$cur" ]; then
manpath="${manpath//://*man$sect/$cur* } ${manpath//://*cat$sect/$cur* }"
else
manpath="${manpath//://*man$sect/ } ${manpath//://*cat$sect/ }"
fi
2009-06-05 08:43:11 +02:00
2009-10-04 19:42:50 +02:00
# redirect stderr for when path doesn't exist
COMPREPLY=( $( eval command ls "$manpath" 2>/dev/null ) )
# weed out directory path names and paths to man pages
COMPREPLY=( ${COMPREPLY[@]##*/?(:)} )
# strip suffix from man pages
2010-03-30 22:04:07 +03:00
COMPREPLY=( ${COMPREPLY[@]%.@([gx]z|bz2|lzma|Z)} )
2009-10-04 19:42:50 +02:00
COMPREPLY=( $( compgen -W '${COMPREPLY[@]%.*}' -- "${cur//\\\\/}" ) )
2009-06-05 08:43:11 +02:00
2009-10-04 19:42:50 +02:00
if [[ "$prev" != $mansect ]]; then
# File based completion for the rest, prepending ./ if needed
# (man 1.6f needs that for man pages in current dir)
local start=${#COMPREPLY[@]}
_filedir $manext
for (( i=$start; i < ${#COMPREPLY[@]}; i++ )); do
[[ ${COMPREPLY[i]} == */* ]] || COMPREPLY[i]=./${COMPREPLY[i]}
done
fi
2009-06-05 08:43:11 +02:00
2009-10-04 19:42:50 +02:00
return 0
2009-06-05 08:43:11 +02:00
}
[ $USERLAND = GNU -o $UNAME = Darwin \
-o $UNAME = FreeBSD -o $UNAME = SunOS -o $UNAME = Cygwin \
-o $UNAME = OpenBSD ] && \
complete -F _man -o filenames man apropos whatis
# Local variables:
# mode: shell-script
2009-10-04 19:42:50 +02:00
# sh-basic-offset: 4
# sh-indent-comment: t
2009-10-04 19:42:50 +02:00
# indent-tabs-mode: nil
# End:
2009-10-04 19:42:50 +02:00
# ex: ts=4 sw=4 et filetype=sh