22
33function _make_target_extract_script()
44{
5- local prefix=$( printf " %s\n" " $1 " | sed ' s/[][\.*^$(){}?+|/]/\\&/g' )
5+ local mode=" $1 "
6+ shift
7+
8+ local PREFIX=" $1 "
9+ local PREFIX_PAT=$( printf " %s\n" " $PREFIX " | \
10+ sed ' s/[][\,.*^$(){}?+|/]/\\&/g' )
11+ local BASENAME=${PREFIX##*/ }
12+ local DIRNAME_LEN=$(( ${# PREFIX} - ${# BASENAME} ))
13+
14+ if [[ $mode == -d ]]; then
15+ # display mode, only output current path component to the next slash
16+ local OUTPUT=" \2"
17+ else
18+ # completion mode, output full path to the next slash
19+ local OUTPUT=" \1\2"
20+ fi
621
722 cat << EOF
823 /^# Make data base/,/^# Files/d # skip until files section
924 /^# Not a target/,/^$/ d # skip not target blocks
10- /^${prefix } /,/^$/! d # skip anything user dont want
25+ /^${PREFIX_PAT } /,/^$/! d # skip anything user dont want
1126
1227 # The stuff above here describes lines that are not
1328 # explicit targets or not targets other than special ones
@@ -21,8 +36,8 @@ function _make_target_extract_script()
2136
2237 /^$/ { # end of target block
2338 x # unhold target
24- s/^( ${prefix} [^:/]*\/).*:.*$/\1/p # write targets for subdirs
25- s/: .*$/ /p # write complete targets
39+ /^$/d # dont print blanks
40+ s,^(.{ ${DIRNAME_LEN} })(.{ ${ # BASENAME} }[^:/]*/?)[^:]*: .*$, ${OUTPUT} ,p
2641 d # hide any bugs
2742 }
2843
@@ -119,10 +134,22 @@ _make()
119134 fi
120135 done
121136
122- COMPREPLY=( $( compgen -W " $( LC_ALL=C \
137+ # recognise that possible completions are only going to be displayed
138+ # so only the base name is shown
139+ mode=--
140+ if (( COMP_TYPE != 9 )) ; then
141+ mode=-d # display-only mode
142+ fi
143+
144+ COMPREPLY=( $( LC_ALL=C \
123145 make -npq " ${makef[@]} " " ${makef_dir[@]} " .DEFAULT 2> /dev/null | \
124- sed -n -r -f <( _make_target_extract_script " $cur " ) ) " -- " $cur " ) )
125- [[ $COMPREPLY == * / ]] && compopt -o nospace
146+ sed -nrf <( _make_target_extract_script $mode " $cur " ) ) )
147+
148+ if [[ $mode != -d ]]; then
149+ # Completion will occur if there is only one suggestion
150+ # so set options for completion based on the first one
151+ [[ $COMPREPLY == * / ]] && compopt -o nospace
152+ fi
126153
127154 fi
128155} &&
0 commit comments