formatted makefile completion and used helper function

pull/1782/head
Gurkirat Singh 2021-01-10 19:29:33 +05:30
parent 9e483a9d78
commit 244c6a62b5
No known key found for this signature in database
GPG Key ID: CFD64E1DCB3DA835
1 changed files with 27 additions and 24 deletions

View File

@ -1,34 +1,37 @@
# Bash completion for Makefile # Bash completion for Makefile
# Loosely adapted from http://stackoverflow.com/a/38415982/1472048 # Loosely adapted from http://stackoverflow.com/a/38415982/1472048
_makecomplete() { if _command_exists make || _command_exists gnumake
COMPREPLY=() then
function __makefile_completions() {
COMPREPLY=()
# https://www.gnu.org/software/make/manual/html_node/Makefile-Names.html # https://www.gnu.org/software/make/manual/html_node/Makefile-Names.html
local files=() local files=()
for f in 'GNUmakefile' 'makefile' 'Makefile' ; do for f in 'GNUmakefile' 'makefile' 'Makefile' ; do
[ -f "$f" ] && files+=("$f") [ -f "$f" ] && files+=("$f")
done done
[ "${#files[@]}" -eq 0 ] && return 0 [ "${#files[@]}" -eq 0 ] && return 0
# collect all targets # collect all targets
local targets=() local targets=()
for f in "${files[@]}" ; do for f in "${files[@]}" ; do
while IFS='' read -r line ; do while IFS='' read -r line ; do
targets+=("$line") targets+=("$line")
done < <(grep -oE '^[a-zA-Z0-9_-]+:([^=]|$)' "$f" | cut -d':' -f1) done < <(grep -oE '^[a-zA-Z0-9_-]+:([^=]|$)' "$f" | cut -d':' -f1)
done done
[ "${#targets[@]}" -eq 0 ] && return 0 [ "${#targets[@]}" -eq 0 ] && return 0
# use the targets for completion # use the targets for completion
while IFS='' read -r line ; do while IFS='' read -r line ; do
COMPREPLY+=("$line") COMPREPLY+=("$line")
done < <(compgen -W "$(tr ' ' '\n' <<<"${targets[@]}" | sort -u)" -- "${COMP_WORDS[COMP_CWORD]}") done < <(compgen -W "$(tr ' ' '\n' <<<"${targets[@]}" | sort -u)" -- "${COMP_WORDS[COMP_CWORD]}")
return 0 return 0
} }
complete -o nospace -F _makecomplete make complete -o nospace -F __makefile_completions make
complete -o nospace -F _makecomplete gnumake complete -o nospace -F __makefile_completions gnumake
fi