@@ -2274,33 +2274,83 @@ _command_offset()
2274
2274
# *something* for every command thrown at it ($cspec != empty)
2275
2275
_minimal
2276
2276
fi
2277
- elif [[ $cspec == * ' -F ' * ]]; then
2278
- # complete -F <function>
2277
+ elif [[ $cspec == * \ -[CF]\ * ]]; then
2278
+ if [[ $cspec == * ' -F ' * ]]; then
2279
+ # complete -F <function>
2279
2280
2280
- # get function name
2281
- local func=${cspec#* -F }
2282
- func=${func%% * }
2281
+ # get function name
2282
+ local func=${cspec#* -F }
2283
+ func=${func%% * }
2283
2284
2284
- if (( ${# COMP_WORDS[@]} >= 2 )) ; then
2285
- $func " $cmd " " ${COMP_WORDS[-1]} " " ${COMP_WORDS[-2]} "
2285
+ if (( ${# COMP_WORDS[@]} >= 2 )) ; then
2286
+ $func " $cmd " " ${COMP_WORDS[-1]} " " ${COMP_WORDS[-2]} "
2287
+ else
2288
+ $func " $cmd " " ${COMP_WORDS[-1]} "
2289
+ fi
2290
+
2291
+ # restart completion (once) if function exited with 124
2292
+ if (( $? == 124 && retry_count++ == 0 )) ; then
2293
+ # Note: When the completion function returns 124, the
2294
+ # state of COMPREPLY is discarded.
2295
+ COMPREPLY=()
2296
+
2297
+ cspec=$( complete -p " $compcmd " 2> /dev/null)
2298
+
2299
+ # Note: When completion spec is removed after 124, we
2300
+ # do not generate any completions including the default
2301
+ # ones. This is the behavior of the original Bash
2302
+ # progcomp.
2303
+ [[ $cspec ]] || break
2304
+
2305
+ continue
2306
+ fi
2286
2307
else
2287
- $func " $cmd " " ${COMP_WORDS[-1]} "
2288
- fi
2308
+ # complete -C <command>
2289
2309
2290
- # restart completion (once) if function exited with 124
2291
- if (( $? == 124 && retry_count++ == 0 )) ; then
2292
- # Note: When the completion function returns 124, the state
2293
- # of COMPREPLY is discarded.
2294
- COMPREPLY=()
2310
+ # get command name
2311
+ local completer=${cspec#* -C \' }
2295
2312
2296
- cspec=$( complete -p " $compcmd " 2> /dev/null)
2313
+ # completer commands are always single-quoted
2314
+ if ! _comp_dequote " '$completer " ; then
2315
+ _minimal
2316
+ break
2317
+ fi
2318
+ completer=${ret[0]}
2319
+
2320
+ local -a suggestions
2321
+
2322
+ local reset_monitor=$( shopt -po monitor) reset_lastpipe=$( shopt -p lastpipe)
2323
+ set +o monitor
2324
+ shopt -s lastpipe
2325
+
2326
+ (
2327
+ export COMP_KEY COMP_LINE COMP_POINT COMP_TYPE
2297
2328
2298
- # Note: When completion spec is removed after 124, we do
2299
- # not generate any completions including the default ones.
2300
- # This is the behavior of the original Bash progcomp.
2301
- [[ $cspec ]] || break
2329
+ if (( ${# COMP_WORDS[@]} >= 2 )) ; then
2330
+ $completer " $cmd " " ${COMP_WORDS[-1]} " \
2331
+ " ${COMP_WORDS[-2]} "
2332
+ else
2333
+ $completer " $cmd " " ${COMP_WORDS[-1]} "
2334
+ fi
2335
+ ) | mapfile suggestions
2302
2336
2303
- continue
2337
+ $reset_monitor
2338
+ $reset_lastpipe
2339
+
2340
+ local suggestion
2341
+ local i=0
2342
+ COMPREPLY=()
2343
+ for suggestion in " ${suggestions[@]} " ; do
2344
+ # `mapfile -t` was added in bash 4.4, so we have to
2345
+ # strip trailing newlines manually
2346
+ suggestion=" ${suggestion% $' \n ' } "
2347
+
2348
+ COMPREPLY[i]+=${COMPREPLY[i]+$' \n ' } $suggestion
2349
+
2350
+ if [[ $suggestion != * \\ ]]; then
2351
+ (( i++ ))
2352
+ fi
2353
+ done
2304
2354
fi
2305
2355
2306
2356
# restore initial compopts
0 commit comments