aboutsummaryrefslogtreecommitdiff
path: root/bash/bash_completion.d/make.bash
blob: c36a039aee813b4bc2629405246962cfe00aa88b (plain)
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
# Completion setup for Make, completing targets
_make() {

    # Find a legible Makefile according to the POSIX spec (look for "makefile"
    # first, then "Makefile"). You may want to add "GNU-makefile" after this.
    local mf
    for mf in makefile Makefile '' ; do
        [[ -f $mf ]] && break
    done
    [[ -n $mf ]] || return 1

    # Iterate through the Makefile, line by line
    local line
    while IFS= read -r line ; do
        case $line in

            # We're looking for targets but not variable assignments
            \#*) ;;
            $'\t'*) ;;
            *:=*) ;;
            *:*)

                # Break the target up with space delimiters
                local -a targets
                IFS=' ' read -rd '' -a targets < \
                    <(printf '%s\0' "${line%%:*}")

                # Iterate through the targets and add suitable ones
                local target
                for target in "${targets[@]}" ; do
                    case $target in

                        # Don't complete special targets beginning with a
                        # period
                        .*) ;;

                        # Don't complete targets with names that have
                        # characters outside of the POSIX spec (plus slashes)
                        *[^[:word:]./-]*) ;;

                        # Add targets that match what we're completing
                        ${COMP_WORDS[COMP_CWORD]}*)
                            COMPREPLY[${#COMPREPLY[@]}]=$target
                            ;;
                    esac
                done
                ;;
        esac
    done < "$mf"
}
complete -F _make -o bashdefault -o default make