@@ -1034,13 +1034,27 @@ _comp_variable_assignments()
10341034# -o XSPEC Passed to _filedir as first arg for other output redirections
10351035# -i XSPEC Passed to _filedir as first arg for stdin redirections
10361036# -s Split long options with _split_longopt, implies -n =
1037+ # @param $1...$3 args Original arguments specified to the completion function.
1038+ # The first argument $1 is command name. The second
1039+ # argument $2 is the string before the cursor in the
1040+ # current word. The third argument $3 is the previous
1041+ # word.
1042+ # @var[out] cur Reconstructed current word
1043+ # @var[out] prev Reconstructed previous word
1044+ # @var[out] words Reconstructed words
1045+ # @var[out] cword Current word index in `words`
1046+ # @var[out] comp_args Original arguments specified to the completion function
1047+ # are saved.
1048+ # @var[out,opt] split When "-s" is specified, `true/false` is set depending on
1049+ # whether the split happened.
10371050# @return True (0) if completion needs further processing,
10381051# False (> 0) no further processing is necessary.
10391052#
10401053_comp_initialize ()
10411054{
1042- local exclude=" " flag outx errx inx OPTIND=1
1055+ local exclude=" " outx errx inx
10431056
1057+ local flag OPTIND=1 OPTARG=' ' OPTERR=0
10441058 while getopts " n:e:o:i:s" flag " $@ " ; do
10451059 case $flag in
10461060 n) exclude+=$OPTARG ;;
@@ -1057,6 +1071,8 @@ _comp_initialize()
10571071 ;;
10581072 esac
10591073 done
1074+ shift " $(( OPTIND - 1 )) "
1075+ (( $# )) && comp_args=(" $@ " )
10601076
10611077 COMPREPLY=()
10621078 local redir=' @(?(+([0-9])|{[a-zA-Z_]*([a-zA-Z_0-9])})@(>?([>|&])|<?([>&])|<<?([-<]))|&>?(>))'
@@ -1108,6 +1124,7 @@ _comp_initialize()
11081124
11091125 return 0
11101126}
1127+ _comp_deprecate_func _init_completion _comp_initialize
11111128
11121129# Helper function for _parse_help and _parse_usage.
11131130# @return True (0) if an option was found, False (> 0) otherwise
@@ -1605,8 +1622,8 @@ _services()
16051622#
16061623_service ()
16071624{
1608- local cur prev words cword
1609- _comp_initialize || return
1625+ local cur prev words cword comp_args
1626+ _comp_initialize -- " $@ " || return
16101627
16111628 # don't complete past 2nd token
16121629 (( cword > 2 )) && return
@@ -1895,8 +1912,8 @@ _bashcomp_try_faketty()
18951912#
18961913_user_at_host ()
18971914{
1898- local cur prev words cword
1899- _comp_initialize -n : || return
1915+ local cur prev words cword comp_args
1916+ _comp_initialize -n : -- " $@ " || return
19001917
19011918 if [[ $cur == * @* ]]; then
19021919 _known_hosts_real " $cur "
@@ -1911,8 +1928,8 @@ shopt -u hostcomplete && complete -F _user_at_host talk ytalk finger
19111928# `_known_hosts_real' instead.
19121929_known_hosts ()
19131930{
1914- local cur prev words cword
1915- _comp_initialize -n : || return
1931+ local cur prev words cword comp_args
1932+ _comp_initialize -n : -- " $@ " || return
19161933
19171934 # NOTE: Using `_known_hosts' as a helper function and passing options
19181935 # to `_known_hosts' is deprecated: Use `_known_hosts_real' instead.
@@ -2193,8 +2210,8 @@ complete -F _known_hosts traceroute traceroute6 \
21932210#
21942211_cd ()
21952212{
2196- local cur prev words cword
2197- _comp_initialize || return
2213+ local cur prev words cword comp_args
2214+ _comp_initialize -- " $@ " || return
21982215
21992216 if [[ $cur == -* ]]; then
22002217 COMPREPLY=($( compgen -W ' $(_parse_help help "$1")' -- " $cur " ) )
@@ -2301,24 +2318,26 @@ _comp_command_offset()
23012318
23022319 local retry_count=0
23032320 while true ; do # loop for the retry request by status 124
2321+ local args original_cur=${comp_args[1]-$cur }
2322+ if (( ${# COMP_WORDS[@]} >= 2 )) ; then
2323+ args=(" $cmd " " $original_cur " " ${COMP_WORDS[-2]} " )
2324+ else
2325+ args=(" $cmd " " $original_cur " )
2326+ fi
2327+
23042328 if [[ ! $cspec ]]; then
23052329 if (( ${# COMPREPLY[@]} == 0 )) ; then
23062330 # XXX will probably never happen as long as completion loader loads
23072331 # *something* for every command thrown at it ($cspec != empty)
2308- _minimal
2332+ _minimal " ${args[@]} "
23092333 fi
23102334 elif [[ $cspec == * ' -F ' * ]]; then
23112335 # complete -F <function>
23122336
23132337 # get function name
23142338 local func=${cspec#* -F }
23152339 func=${func%% * }
2316-
2317- if (( ${# COMP_WORDS[@]} >= 2 )) ; then
2318- $func " $cmd " " ${COMP_WORDS[-1]} " " ${COMP_WORDS[-2]} "
2319- else
2320- $func " $cmd " " ${COMP_WORDS[-1]} "
2321- fi
2340+ $func " ${args[@]} "
23222341
23232342 # restart completion (once) if function exited with 124
23242343 if (( $? == 124 && retry_count++ == 0 )) ; then
@@ -2395,8 +2414,8 @@ _complete_as_root()
23952414
23962415_longopt ()
23972416{
2398- local cur prev words cword split
2399- _comp_initialize -s || return
2417+ local cur prev words cword split comp_args
2418+ _comp_initialize -s -- " $@ " || return
24002419
24012420 case " ${prev,,} " in
24022421 --help | --usage | --version)
@@ -2454,8 +2473,8 @@ declare -Ag _xspecs
24542473
24552474_filedir_xspec()
24562475{
2457- local cur prev words cword
2458- _comp_initialize || return
2476+ local cur prev words cword comp_args
2477+ _comp_initialize -- " $@ " || return
24592478
24602479 _tilde " $cur " || return
24612480
@@ -2576,8 +2595,8 @@ unset -f _install_xspec
25762595# Minimal completion to use as fallback in _completion_loader.
25772596_minimal()
25782597{
2579- local cur prev words cword split
2580- _comp_initialize -s || return
2598+ local cur prev words cword split comp_args
2599+ _comp_initialize -s -- " $@ " || return
25812600 $split && return
25822601 _filedir
25832602}
0 commit comments