2
2
3
3
function _make_target_extract_script()
4
4
{
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
6
21
7
22
cat << EOF
8
23
/^# Make data base/,/^# Files/d # skip until files section
9
24
/^# 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
11
26
12
27
# The stuff above here describes lines that are not
13
28
# explicit targets or not targets other than special ones
@@ -21,8 +36,8 @@ function _make_target_extract_script()
21
36
22
37
/^$/ { # end of target block
23
38
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
26
41
d # hide any bugs
27
42
}
28
43
@@ -119,10 +134,22 @@ _make()
119
134
fi
120
135
done
121
136
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 \
123
145
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
126
153
127
154
fi
128
155
} &&
0 commit comments