Skip to content

Commit 9d07bf8

Browse files
committed
Implemented auto-completion for makefiles with custom bash-completion target
1 parent 16115f4 commit 9d07bf8

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

completions/make

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,41 @@ _make()
157157
mode=-d # display-only mode
158158
fi
159159

160-
local IFS=$' \t\n' script=$(_make_target_extract_script $mode "$cur")
161-
COMPREPLY=($(LC_ALL=C \
162-
$1 -npq __BASH_MAKE_COMPLETION__=1 \
163-
${makef+"${makef[@]}"} "${makef_dir[@]}" .DEFAULT 2>/dev/null |
164-
command sed -ne "$script"))
160+
local use_fallback=yes
161+
if [[ ${BASH_COMPLETION_MAKE_ENABLE_MAKEFILE_TARGET:=0} == 1 ]]; then
162+
local makefile_targets exit_status
163+
164+
# Execute the makefile target and interpret the output as completion info
165+
makefile_targets=$($1 -sS __BASH_MAKE_COMPLETION__=1 \
166+
${makef+"${makef[@]}"} "${makef_dir[@]}" \
167+
${BASH_COMPLETION_MAKE_MAKEFILE_TARGET:=.BASH-COMPLETION} 2>/dev/null)
168+
exit_status=$?
169+
170+
if [[ $exit_status -eq 0 ]]; then
171+
use_fallback=no
172+
173+
local prefix="$2"
174+
local prefix_pat=$(command sed 's/[][\,.*^$(){}?+|/]/\\&/g' <<<"$prefix")
175+
local filtered_output=$(command grep -E "^${prefix_pat}" <<<"${makefile_targets// /$'\n'}")
176+
local IFS=$' \t\n'
177+
COMPREPLY=($filtered_output)
178+
fi
179+
fi
180+
181+
if [[ $use_fallback == yes ]]; then
182+
# Parse the output of "make -npq .DEFAULT" (or similar) command
183+
local IFS=$' \t\n' script=$(_make_target_extract_script $mode "$cur")
184+
COMPREPLY=($(LC_ALL=C \
185+
$1 -npq __BASH_MAKE_COMPLETION__=1 \
186+
${makef+"${makef[@]}"} "${makef_dir[@]}" .DEFAULT 2>/dev/null |
187+
command sed -ne "$script"))
188+
fi
165189

166190
if [[ $mode != -d ]]; then
167191
# Completion will occur if there is only one suggestion
168192
# so set options for completion based on the first one
169193
[[ ${COMPREPLY-} == */ ]] && compopt -o nospace
170194
fi
171-
172195
fi
173196
} &&
174197
complete -F _make make gmake gnumake pmake colormake bmake

0 commit comments

Comments
 (0)