From b70c02c4aed66aa2320fa52075639b2244842ddc Mon Sep 17 00:00:00 2001 From: cornfeedhobo Date: Fri, 21 Feb 2020 12:16:57 -0600 Subject: [PATCH 1/5] enhance makefile completion to support multiple files and gnumake --- completion/available/makefile.completion.bash | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/completion/available/makefile.completion.bash b/completion/available/makefile.completion.bash index c2a833ac..9f70958a 100644 --- a/completion/available/makefile.completion.bash +++ b/completion/available/makefile.completion.bash @@ -1,3 +1,24 @@ -# Add completion for Makefile -# see http://stackoverflow.com/a/38415982/1472048 -complete -W "\`grep -oE '^[a-zA-Z0-9_-]+:([^=]|$)' Makefile | sed 's/[^a-zA-Z0-9_-]*$//'\`" make +#!/usr/bin/env bash +# Bash completion for Makefile + +# Loosely adapted from http://stackoverflow.com/a/38415982/1472048 + +_makecomplete() { + # https://www.gnu.org/software/make/manual/html_node/Makefile-Names.html + local files=( $(find . -maxdepth 1 -regextype posix-extended -regex '.*(GNU)?[Mm]akefile$' -printf '%f ') ) + + # collect all targets + local targets='' + for f in ${files[@]} ; do + for t in $(grep -oE '^[a-zA-Z0-9_-]+:([^=]|$)' $f | cut -d':' -f1) ; do + targets+="$t\n" + done + done + + # flatten the array for completion + COMPREPLY=($(compgen -W "$(echo -e "$targets" | head -c -1 | sort -u)" -- ${COMP_WORDS[COMP_CWORD]})) + return 0 +} + +complete -o nospace -F _makecomplete make +complete -o nospace -F _makecomplete gnumake From 9d97532f8ee6a60f8ff7cc0db65c4fcabc047bb8 Mon Sep 17 00:00:00 2001 From: cornfeedhobo Date: Fri, 21 Feb 2020 12:56:00 -0600 Subject: [PATCH 2/5] enhance makefile completion and make shellcheck happy --- completion/available/makefile.completion.bash | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/completion/available/makefile.completion.bash b/completion/available/makefile.completion.bash index 9f70958a..d53e18fb 100644 --- a/completion/available/makefile.completion.bash +++ b/completion/available/makefile.completion.bash @@ -5,18 +5,24 @@ _makecomplete() { # https://www.gnu.org/software/make/manual/html_node/Makefile-Names.html - local files=( $(find . -maxdepth 1 -regextype posix-extended -regex '.*(GNU)?[Mm]akefile$' -printf '%f ') ) + local files=() + while IFS='' read -r line; do + files+=("$line") + done < <(find . -maxdepth 1 -regextype posix-extended -regex '.*(GNU)?[Mm]akefile$' -printf '%f\n') # collect all targets - local targets='' - for f in ${files[@]} ; do - for t in $(grep -oE '^[a-zA-Z0-9_-]+:([^=]|$)' $f | cut -d':' -f1) ; do - targets+="$t\n" - done + local targets=() + for f in "${files[@]}" ; do + while IFS='' read -r line ; do + targets+=("$line") + done < <(grep -oE '^[a-zA-Z0-9_-]+:([^=]|$)' "$f" | cut -d':' -f1) done # flatten the array for completion - COMPREPLY=($(compgen -W "$(echo -e "$targets" | head -c -1 | sort -u)" -- ${COMP_WORDS[COMP_CWORD]})) + COMPREPLY=() + while IFS='' read -r line ; do + COMPREPLY+=("$line") + done < <(compgen -W "$(tr ' ' '\n' <<<"${targets[@]}" | sort -u)" -- "${COMP_WORDS[COMP_CWORD]}") return 0 } From 552cd89851a331811fcf40888b22b63960215703 Mon Sep 17 00:00:00 2001 From: cornfeedhobo Date: Fri, 21 Feb 2020 13:09:08 -0600 Subject: [PATCH 3/5] enhance makefile completion - shortcircuit early when nothing is found --- completion/available/makefile.completion.bash | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/completion/available/makefile.completion.bash b/completion/available/makefile.completion.bash index d53e18fb..021dd484 100644 --- a/completion/available/makefile.completion.bash +++ b/completion/available/makefile.completion.bash @@ -4,12 +4,16 @@ # Loosely adapted from http://stackoverflow.com/a/38415982/1472048 _makecomplete() { + COMPREPLY=() + # https://www.gnu.org/software/make/manual/html_node/Makefile-Names.html local files=() while IFS='' read -r line; do files+=("$line") done < <(find . -maxdepth 1 -regextype posix-extended -regex '.*(GNU)?[Mm]akefile$' -printf '%f\n') + [ "${#files[@]}" -eq 0 ] && return 0 + # collect all targets local targets=() for f in "${files[@]}" ; do @@ -18,11 +22,13 @@ _makecomplete() { done < <(grep -oE '^[a-zA-Z0-9_-]+:([^=]|$)' "$f" | cut -d':' -f1) done - # flatten the array for completion - COMPREPLY=() + [ "${#targets[@]}" -eq 0 ] && return 0 + + # use the targets for completion while IFS='' read -r line ; do COMPREPLY+=("$line") done < <(compgen -W "$(tr ' ' '\n' <<<"${targets[@]}" | sort -u)" -- "${COMP_WORDS[COMP_CWORD]}") + return 0 } From 239f7086379ddbe6274ff0f9fce1d53cd4806f13 Mon Sep 17 00:00:00 2001 From: cornfeedhobo Date: Fri, 21 Feb 2020 22:27:56 -0600 Subject: [PATCH 4/5] enhance makefile completion - remove use of find --- completion/available/makefile.completion.bash | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/completion/available/makefile.completion.bash b/completion/available/makefile.completion.bash index 021dd484..4bc51639 100644 --- a/completion/available/makefile.completion.bash +++ b/completion/available/makefile.completion.bash @@ -8,9 +8,9 @@ _makecomplete() { # https://www.gnu.org/software/make/manual/html_node/Makefile-Names.html local files=() - while IFS='' read -r line; do - files+=("$line") - done < <(find . -maxdepth 1 -regextype posix-extended -regex '.*(GNU)?[Mm]akefile$' -printf '%f\n') + for f in 'GNUmakefile' 'makefile' 'Makefile' ; do + [ -f "$f" ] && files+=("$f") + done [ "${#files[@]}" -eq 0 ] && return 0 From 2e4c4ad29aa56679b4fabaa890dc3fc4e9e08b01 Mon Sep 17 00:00:00 2001 From: cornfeedhobo Date: Fri, 21 Feb 2020 22:29:14 -0600 Subject: [PATCH 5/5] enhance makefile completion - remove hash bang --- completion/available/makefile.completion.bash | 2 -- 1 file changed, 2 deletions(-) diff --git a/completion/available/makefile.completion.bash b/completion/available/makefile.completion.bash index 4bc51639..e72ba6fd 100644 --- a/completion/available/makefile.completion.bash +++ b/completion/available/makefile.completion.bash @@ -1,6 +1,4 @@ -#!/usr/bin/env bash # Bash completion for Makefile - # Loosely adapted from http://stackoverflow.com/a/38415982/1472048 _makecomplete() {