PageRenderTime 80ms CodeModel.GetById 78ms app.highlight 0ms RepoModel.GetById 1ms app.codeStats 0ms

/bash_completion.d/man

http://github.com/brinkman83/bashrc
#! | 83 lines | 69 code | 14 blank | 0 comment | 0 complexity | 4692850fbf6e1f245ec4dbe062e5ebef MD5 | raw file
 1# man(1) completion
 2
 3[ $USERLAND = GNU -o $UNAME = Darwin \
 4  -o $UNAME = FreeBSD -o $UNAME = SunOS -o $UNAME = Cygwin \
 5  -o $UNAME = OpenBSD ] &&
 6_man()
 7{
 8    local cur i prev sect manpath manext mansect uname
 9
10    manext="@([0-9lnp]|[0-9][px]|man|3pm)?(.@(gz|bz2|lzma))"
11    mansect="@([0-9lnp]|[0-9][px]|3pm)"
12
13    COMPREPLY=()
14    cur=`_get_cword`
15    prev=${COMP_WORDS[COMP_CWORD-1]}
16
17    if [[ "$prev" == -l ]]; then
18        _filedir $manext
19        return 0
20    fi
21
22    _expand || return 0
23
24    # file based completion if parameter contains /
25    if [[ "$cur" == */* ]]; then
26        _filedir $manext
27        return 0
28    fi
29
30    uname=$( uname -s )
31    if [[ $uname == @(Linux|GNU|GNU/*|FreeBSD|Cygwin|CYGWIN_*) ]]; then
32        manpath=$( manpath 2>/dev/null || command man --path )
33    else
34        manpath=$MANPATH
35    fi
36
37    if [ -z "$manpath" ]; then
38        COMPREPLY=( $( compgen -c -- "$cur" ) )
39        return 0
40    fi
41
42    # determine manual section to search
43    [[ "$prev" == $mansect ]] && sect=$prev || sect='*'
44
45    manpath=$manpath:
46    if [ -n "$cur" ]; then
47        manpath="${manpath//://*man$sect/$cur* } ${manpath//://*cat$sect/$cur* }"
48    else
49        manpath="${manpath//://*man$sect/ } ${manpath//://*cat$sect/ }"
50    fi
51
52    # redirect stderr for when path doesn't exist
53    COMPREPLY=( $( eval command ls "$manpath" 2>/dev/null ) )
54    # weed out directory path names and paths to man pages
55    COMPREPLY=( ${COMPREPLY[@]##*/?(:)} )
56    # strip suffix from man pages
57    COMPREPLY=( ${COMPREPLY[@]%.@(gz|bz2|lzma)} )
58    COMPREPLY=( $( compgen -W '${COMPREPLY[@]%.*}' -- "${cur//\\\\/}" ) )
59
60    if [[ "$prev" != $mansect ]]; then
61        # File based completion for the rest, prepending ./ if needed
62        # (man 1.6f needs that for man pages in current dir)
63        local start=${#COMPREPLY[@]}
64        _filedir $manext
65        for (( i=$start; i < ${#COMPREPLY[@]}; i++ )); do
66            [[ ${COMPREPLY[i]} == */* ]] || COMPREPLY[i]=./${COMPREPLY[i]}
67        done
68    fi
69
70    return 0
71}
72[ $USERLAND = GNU -o $UNAME = Darwin \
73  -o $UNAME = FreeBSD -o $UNAME = SunOS -o $UNAME = Cygwin \
74  -o $UNAME = OpenBSD ] && \
75complete -F _man $filenames man apropos whatis
76
77# Local variables:
78# mode: shell-script
79# sh-basic-offset: 4
80# sh-indent-comment: t
81# indent-tabs-mode: nil
82# End:
83# ex: ts=4 sw=4 et filetype=sh