106 lines
2.9 KiB
Plaintext
Raw Normal View History

# man(1) completion -*- shell-script -*-
2011-10-16 10:56:44 +03:00
[[ $OSTYPE == *@(darwin|freebsd|solaris|cygwin|openbsd)* ]] || _userland GNU \
|| return 1
2009-06-05 08:43:11 +02:00
_man()
{
local cur prev words cword split
_init_completion -s -n : || return
2012-07-11 18:17:22 +03:00
local comprsuffix=".@([glx]z|bz2|lzma|Z)"
local manext="@([0-9lnp]|[0-9][px]|man|3?(gl|pm))?($comprsuffix)"
local mansect="@([0-9lnp]|[0-9][px]|3?(gl|pm))"
2009-06-05 08:43:11 +02:00
case $prev in
-C|--config-file)
_filedir conf
return
;;
-l|--local-file)
_filedir "$manext"
return
;;
-M|--manpath)
_filedir -d
return
;;
-P|--pager)
compopt -o filenames
COMPREPLY=( $( compgen -c -- "$cur" ) )
return
;;
-p|--preprocessor)
COMPREPLY=( $( compgen -W 'e p t g r v' -- "$cur" ) )
return
;;
-L|--locale|-m|--systems|-e|--extension|-r|--prompt|-R|--recode|\
-E|--encoding)
return
;;
esac
$split && return
if [[ $cur == -* ]]; then
COMPREPLY=( $( compgen -W '$( _parse_help "$1" -h )' -- "$cur" ) )
[[ $COMPREPLY == *= ]] && compopt -o nospace
return
2009-10-04 19:42:50 +02:00
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
# file based completion if parameter looks like a path
if [[ "$cur" == @(*/|[.~])* ]]; then
_filedir "$manext"
2009-10-04 19:42:50 +02:00
return 0
fi
2009-06-05 08:43:11 +02:00
2012-07-11 18:17:22 +03:00
local manpath
if [[ $OSTYPE == *@(darwin|linux|freebsd|cygwin)* ]] || _userland GNU; then
manpath=$( manpath 2>/dev/null || command man -w )
2009-10-04 19:42:50 +02:00
else
manpath=$MANPATH
fi
2009-06-05 08:43:11 +02:00
2011-11-09 23:28:11 +02:00
if [[ -z $manpath ]]; then
2009-10-04 19:42:50 +02:00
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
2012-07-11 18:17:22 +03:00
local sect
2009-10-04 19:42:50 +02:00
[[ "$prev" == $mansect ]] && sect=$prev || sect='*'
2009-06-05 08:43:11 +02:00
2009-10-04 19:42:50 +02:00
manpath=$manpath:
2011-11-09 23:28:11 +02:00
if [[ -n $cur ]]; then
2009-10-04 19:42:50 +02:00
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
COMPREPLY=( ${COMPREPLY[@]%$comprsuffix} )
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 i start=${#COMPREPLY[@]}
_filedir "$manext"
2009-10-04 19:42:50 +02:00
for (( i=$start; i < ${#COMPREPLY[@]}; i++ )); do
[[ ${COMPREPLY[i]} == */* ]] || COMPREPLY[i]=./${COMPREPLY[i]}
done
fi
2009-06-05 08:43:11 +02:00
__ltrim_colon_completions "$cur"
2009-10-04 19:42:50 +02:00
return 0
2010-10-23 23:05:56 +03:00
} &&
complete -F _man man apropos whatis
2009-10-04 19:42:50 +02:00
# ex: ts=4 sw=4 et filetype=sh