Skip to content

Commit

Permalink
make: incremental completion for make with compact display
Browse files Browse the repository at this point in the history
  • Loading branch information
code5hot authored and GArik committed Sep 12, 2012
1 parent 23ac383 commit 39f00f9
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions completions/make
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@

function _make_target_extract_script()
{
local prefix=$(printf "%s\n" "$1" | sed 's/[][\.*^$(){}?+|/]/\\&/g')
local mode="$1"
shift

local PREFIX="$1"
local PREFIX_PAT=$( printf "%s\n" "$PREFIX" | \
sed 's/[][\,.*^$(){}?+|/]/\\&/g' )
local BASENAME=${PREFIX##*/}
local DIRNAME_LEN=$(( ${#PREFIX} - ${#BASENAME} ))

if [[ $mode == -d ]]; then
# display mode, only output current path component to the next slash
local OUTPUT="\2"
else
# completion mode, output full path to the next slash
local OUTPUT="\1\2"
fi

cat <<EOF
/^# Make data base/,/^# Files/d # skip until files section
/^# Not a target/,/^$/ d # skip not target blocks
/^${prefix}/,/^$/! d # skip anything user dont want
/^${PREFIX_PAT}/,/^$/! d # skip anything user dont want
# The stuff above here describes lines that are not
# explicit targets or not targets other than special ones
Expand All @@ -21,8 +36,8 @@ function _make_target_extract_script()
/^$/ { # end of target block
x # unhold target
s/^(${prefix}[^:/]*\/).*:.*$/\1/p # write targets for subdirs
s/:.*$/ /p # write complete targets
/^$/d # dont print blanks
s,^(.{${DIRNAME_LEN}})(.{${#BASENAME}}[^:/]*/?)[^:]*:.*$,${OUTPUT},p
d # hide any bugs
}
Expand Down Expand Up @@ -119,10 +134,22 @@ _make()
fi
done

COMPREPLY=( $( compgen -W "$( LC_ALL=C \
# recognise that possible completions are only going to be displayed
# so only the base name is shown
mode=--
if (( COMP_TYPE != 9 )); then
mode=-d # display-only mode
fi

COMPREPLY=( $( LC_ALL=C \
make -npq "${makef[@]}" "${makef_dir[@]}" .DEFAULT 2>/dev/null | \
sed -n -r -f <(_make_target_extract_script "$cur") )" -- "$cur" ) )
[[ $COMPREPLY == */ ]] && compopt -o nospace
sed -nrf <(_make_target_extract_script $mode "$cur") ) )

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
} &&
Expand Down

0 comments on commit 39f00f9

Please sign in to comment.