# Provide autocompletion for a Makefile targets.
#
# Usage:
#    source .bash_completion # this file
#    make[tab] # autocompletes the .PHONY targets :)
#
# See https://debian-administration.org/article/317/An_introduction_to_bash_completion_part_2
_make()
{
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    # autocomplete all the .PHONY targets, improvements are welcome!
    opts=$(cat Makefile | grep PHONY | awk '{$1=""; print $0;}')

    if [[ ${cur} == * ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
    fi
}
complete -F _make make