# man(1) completion -*- shell-script -*- [[ $OSTYPE == *@(darwin|freebsd|solaris|cygwin|openbsd)* ]] || _userland GNU \ || return 1 proc _man { local cur prev words cword split _init_completion -s -n : || return 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))" case (prev) { -C|--config-file { _filedir conf return } -l|--local-file { _filedir $manext return } -M|--manpath { _filedir -d return } -P|--pager { compopt -o filenames setvar COMPREPLY = ''( $( compgen -c -- "$cur" ) ) return } -p|--preprocessor { setvar COMPREPLY = ''( $( compgen -W 'e p t g r v' -- "$cur" ) ) return } -L|--locale|-m|--systems|-e|--extension|-r|--prompt|-R|--recode|\ -E|--encoding { return } } $split && return if [[ $cur == -* ]] { setvar COMPREPLY = ''( $( compgen -W '$( _parse_help "$1" -h )' -- "$cur" ) ) [[ $COMPREPLY == *= ]] && compopt -o nospace return } _expand || return 0 # file based completion if parameter looks like a path if [[ "$cur" == @(*/|[.~])* ]] { _filedir $manext return 0 } local manpath if [[ $OSTYPE == *@(darwin|linux|freebsd|cygwin)* ]] || _userland GNU { setvar manpath = $( manpath 2>/dev/null || command man -w ) } else { setvar manpath = "$MANPATH" } if [[ -z $manpath ]] { setvar COMPREPLY = ''( $( compgen -c -- "$cur" ) ) return 0 } # determine manual section to search local sect [[ "$prev" == $mansect ]] && setvar sect = "$prev" || setvar sect = ''*'' setvar manpath = "$manpath:" if [[ -n $cur ]] { setvar manpath = ""${manpath//://*man$sect/$cur* } ${manpath//://*cat$sect/$cur* }"" } else { setvar manpath = ""${manpath//://*man$sect/ } ${manpath//://*cat$sect/ }"" } # redirect stderr for when path doesn't exist setvar COMPREPLY = ''( $( eval command ls "$manpath" 2>/dev/null ) ) # weed out directory path names and paths to man pages setvar COMPREPLY = ''( ${COMPREPLY[@]##*/?(:)} ) # strip suffix from man pages setvar COMPREPLY = ''( ${COMPREPLY[@]%$comprsuffix} ) setvar COMPREPLY = ''( $( compgen -W '${COMPREPLY[@]%.*}' -- "${cur//\\\\/}" ) ) if [[ "$prev" != $mansect ]] { # 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 for (( i=$start; i < ${#COMPREPLY[@]}; i++ )); do [[ ${COMPREPLY[i]} == */* ]] || COMPREPLY[i]=./${COMPREPLY[i]} done } __ltrim_colon_completions $cur return 0 } && complete -F _man man apropos whatis # ex: ts=4 sw=4 et filetype=sh