# man(1) completion -*- shell-script -*- [[ $OSTYPE == *@(darwin|freebsd|solaris|cygwin|openbsd)* ]] || _userland GNU \ || return 1 proc _man { var cur = '', prev = '', words = '', cword = '', split = '' _init_completion -s -n : || return var comprsuffix = '".@([glx]z|bz2|lzma|Z)'" var manext = ""@([0-9lnp]|[0-9][px]|man|3?(gl|pm))?($comprsuffix)"" var mansect = '"@([0-9lnp]|[0-9][px]|3?(gl|pm))'" match $prev { with -C|--config-file _filedir conf return with -l|--local-file _filedir $manext return with -M|--manpath _filedir -d return with -P|--pager compopt -o filenames setglobal COMPREPLY = '( '$( compgen -c -- "$cur" ) ) return with -p|--preprocessor setglobal COMPREPLY = '( '$( compgen -W 'e p t g r v' -- "$cur" ) ) return with -L|--locale|-m|--systems|-e|--extension|-r|--prompt|-R|--recode|\ -E|--encoding return } $split && return if [[ $cur == -* ]] { setglobal 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 } var manpath = '' if [[ $OSTYPE == *@(darwin|linux|freebsd|cygwin)* ]] || _userland GNU { set manpath = $[ manpath !2 >/dev/null || command man -w] } else { set manpath = $MANPATH } if [[ -z $manpath ]] { setglobal COMPREPLY = '( '$( compgen -c -- "$cur" ) ) return 0 } # determine manual section to search var sect = '' [[ "$prev" == $mansect ]] && set sect = $prev || set sect = ''*'' set manpath = "$manpath:" if [[ -n $cur ]] { set manpath = ""$(manpath//://*man$sect/$cur* ) $(manpath//://*cat$sect/$cur* )"" } else { set manpath = ""$(manpath//://*man$sect/ ) $(manpath//://*cat$sect/ )"" } # redirect stderr for when path doesn't exist setglobal COMPREPLY = '( '$( eval command ls "$manpath" 2>/dev/null ) ) # weed out directory path names and paths to man pages setglobal COMPREPLY = '( '${COMPREPLY[@]##*/?(:)} ) # strip suffix from man pages setglobal COMPREPLY = '( '${COMPREPLY[@]%$comprsuffix} ) setglobal 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) var 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