aboutsummaryrefslogtreecommitdiff
path: root/bash/bash_completion.d/man.bash
blob: f861602997f1ab18d2a8a0701e03b2a7970d8b66 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Autocompletion for man(1)
_man() {

    # Don't interfere with a user typing a path
    case $2 in
        */*) return 1 ;;
    esac

    # If previous word started with a number, we'll assume that's a section to
    # search
    local sec
    case $3 in
        [0-9]*) sec=$3 ;;
    esac

    # Cut completion short if we have neither section nor word; there will
    # probably be too many results
    [[ -n $sec ]] || [[ -n $2 ]] || return

    # Read completion results from a subshell and add them to the COMPREPLY
    # array individually
    local ci comp
    while IFS= read -d / -r comp ; do
        COMPREPLY[ci++]=$comp
    done < <(

        # Make globs expand appropriately
        shopt -u dotglob
        shopt -s nullglob
        if _completion_ignore_case ; then
            shopt -s nocaseglob
        fi

        # Figure out the manual paths to search
        if hash manpath 2>/dev/null ; then

            # manpath(1) exists, run it to find what to search
            IFS=: read -a manpaths -r \
                < <(manpath 2>/dev/null)
        else

            # Fall back on some typical paths
            manpaths=( \
                "$HOME"/.local/man \
                "$HOME"/.local/share/man \
                /usr/man \
                /usr/share/man \
                /usr/local/man \
                /usr/local/share/man \
            )
        fi

        # Add pages from each manual directory
        local pages pi
        for mp in "${manpaths[@]}" ; do
            [[ -n $mp ]] || continue

            # Which pattern? Depends on section specification
            if [[ -n $sec ]] ; then

                # Section requested; quoted value in glob
                for page in "$mp"/man"${sec%%[!0-9]*}"/"$2"*."$sec"* ; do
                    pages[pi++]=${page##*/}
                done
            else

                # No section;
                for page in "$mp"/man[0-9]*/"$2"*.[0-9]* ; do
                    pages[pi++]=${page##*/}
                done
            fi
        done

        # Bail if there are no pages
        ((pi)) || exit

        # Strip section suffixes
        pages=("${pages[@]%.[0-9]*}")

        # Print quoted entries, slash-delimited
        printf '%q/' "${pages[@]}"
    )
}
complete -F _man -o bashdefault -o default man