aboutsummaryrefslogtreecommitdiff
path: root/bash/bashrc.d/make.bash
blob: 397ba9444d4775a5cabae410bbdfb3af9905e8dd (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
# Unset my GCC_COLORS vars when invoking make(1) if terminal doesn't have
# color; I have to do this because my wrapper function is ignored by the
# /bin/sh fork make(1) does, and gcc(1) always uses colors if the variable is
# set, as opposed to ls(1) and grep(1) which only do it if the --color option
# is specified appropriately.
make() {
    local -i colors
    colors=$( {
        tput Co || tput colors
    } 2>/dev/null )
    if ((colors >= 8)) ; then
        command make "$@"
    else
        ( unset -v GCC_COLORS ; command make "$@" )
    fi
}

# Completion setup for Make, completing targets
_make() {
    local word
    word=${COMP_WORDS[COMP_CWORD]}

    # Quietly bail if no legible Makefile
    if [[ ! -r Makefile ]] ; then
        return 1
    fi

    # Build a list of targets by parsing the Makefile
    local -a targets tokens
    local target line
        while read -r line ; do
        if [[ $line == *:* ]] ; then
            target=$line
            target=${target%%:*}
            target=${target% }
            if [[ $target != *[^[:alnum:][:space:]_-]* ]] ; then
                IFS=' ' read -a tokens \
                    < <(printf '%s\n' "$target")
                targets=("${targets[@]}" "${tokens[@]}")
            fi
        fi
    done < Makefile

    # Complete with matching targets
    COMPREPLY=( $(compgen -W "${targets[*]}" -- "$word") )
}
complete -F _make -o default make