Skip to content

[WIP] Implemented custom auto-completion for Makefiles [Feedback wanted] #491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions completions/make
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,41 @@ _make()
mode=-d # display-only mode
fi

local IFS=$' \t\n' script=$(_make_target_extract_script $mode "$cur")
COMPREPLY=($(LC_ALL=C \
$1 -npq __BASH_MAKE_COMPLETION__=1 \
${makef+"${makef[@]}"} "${makef_dir[@]}" .DEFAULT 2>/dev/null |
command sed -ne "$script"))
local use_fallback=yes
if [[ ${BASH_COMPLETION_MAKE_ENABLE_MAKEFILE_TARGET:=0} == 1 ]]; then
local makefile_targets exit_status

# Execute the makefile target and interpret the output as completion info
makefile_targets=$($1 -sS __BASH_MAKE_COMPLETION__=1 \
${makef+"${makef[@]}"} "${makef_dir[@]}" \
${BASH_COMPLETION_MAKE_MAKEFILE_TARGET:=.BASH-COMPLETION} 2>/dev/null)
exit_status=$?

if [[ $exit_status -eq 0 ]]; then
use_fallback=no

local prefix="$2"
local prefix_pat=$(command sed 's/[][\,.*^$(){}?+|/]/\\&/g' <<<"$prefix")
local filtered_output=$(command grep -E "^${prefix_pat}" <<<"${makefile_targets// /$'\n'}")
local IFS=$' \t\n'
COMPREPLY=($filtered_output)
fi
fi

if [[ $use_fallback == yes ]]; then
# Parse the output of "make -npq .DEFAULT" (or similar) command
local IFS=$' \t\n' script=$(_make_target_extract_script $mode "$cur")
COMPREPLY=($(LC_ALL=C \
$1 -npq __BASH_MAKE_COMPLETION__=1 \
${makef+"${makef[@]}"} "${makef_dir[@]}" .DEFAULT 2>/dev/null |
command sed -ne "$script"))
fi

if [[ $mode != -d ]]; then
# Completion will occur if there is only one suggestion
# so set options for completion based on the first one
[[ ${COMPREPLY-} == */ ]] && compopt -o nospace
fi

fi
} &&
complete -F _make make gmake gnumake pmake colormake bmake
Expand Down