Skip to content

Commit fc4af85

Browse files
committed
fix(_comp_initialize): save original completion arguments
Now `_comp_initialize` receives in its arguments the original arguments passed to the completion function. Arguments of other functions such as `_minimal`, `_longopt`, `_filedir_xspec`, `_known_hosts`, and `_user_at_host` are also changed.
1 parent e061eb5 commit fc4af85

File tree

479 files changed

+1175
-1155
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

479 files changed

+1175
-1155
lines changed

bash_completion

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

completions/2to3

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
_2to3()
44
{
5-
local cur prev words cword split
6-
_comp_initialize -s || return
5+
local cur prev words cword split comp_args
6+
_comp_initialize -s -- "$@" || return
77

88
case $prev in
99
-h | --help | --add-suffix)

completions/7z

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
_7z()
44
{
5-
local cur prev words cword
6-
_comp_initialize -n = || return
5+
local cur prev words cword comp_args
6+
_comp_initialize -n = -- "$@" || return
77

88
if ((cword == 1)); then
99
COMPREPLY=($(compgen -W 'a b d e h i l rn t u x' -- "$cur"))
@@ -99,7 +99,7 @@ _7z()
9999
local args
100100
_count_args "="
101101
if ((args == 2)); then
102-
_filedir_xspec unzip
102+
_filedir_xspec unzip "${@:2}"
103103
# TODO: parsing 7z i output?
104104
# - how to figure out if the format is input or output?
105105
# - find string Formats:, read until next empty line

completions/_adb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ _adb_command_usage()
1212

1313
_adb()
1414
{
15-
local cur prev words cword
16-
_comp_initialize || return
15+
local cur prev words cword comp_args
16+
_comp_initialize -- "$@" || return
1717

1818
case $prev in
1919
-s | -p | --algo | --key | --iv)

completions/_cal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
_cal()
77
{
8-
local cur prev words cword
9-
_comp_initialize || return
8+
local cur prev words cword comp_args
9+
_comp_initialize -- "$@" || return
1010

1111
case $prev in
1212
-m)

completions/_chsh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
_chsh()
77
{
8-
local cur prev words cword
9-
_comp_initialize || return
8+
local cur prev words cword comp_args
9+
_comp_initialize -- "$@" || return
1010

1111
local word chroot
1212
for word in "${words[@]}"; do

completions/_dmesg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ _dmesg()
77
{
88
[[ $OSTYPE == *solaris* ]] && return # no args there
99

10-
local cur prev words cword
11-
_comp_initialize || return
10+
local cur prev words cword comp_args
11+
_comp_initialize -- "$@" || return
1212

1313
case $prev in
1414
-h | --help | -V | --version | -s | --buffer-size | -M | -N)

completions/_eject

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
_eject()
77
{
8-
local cur prev words cword
9-
_comp_initialize || return
8+
local cur prev words cword comp_args
9+
_comp_initialize -- "$@" || return
1010

1111
case $prev in
1212
-h | --help | -V | --version | -c | --changerslot | -x | --cdspeed)

completions/_hexdump

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
_hexdump()
77
{
8-
local cur prev words cword
9-
_comp_initialize || return
8+
local cur prev words cword comp_args
9+
_comp_initialize -- "$@" || return
1010

1111
case $prev in
1212
-V | -e | -n | -s)

completions/_hwclock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
_hwclock()
77
{
8-
local cur prev words cword
9-
_comp_initialize || return
8+
local cur prev words cword comp_args
9+
_comp_initialize -- "$@" || return
1010

1111
case $prev in
1212
-h | --help | -V | --version | --date | --epoch)

completions/_ionice

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
_ionice()
77
{
8-
local cur prev words cword
9-
_comp_initialize || return
8+
local cur prev words cword comp_args
9+
_comp_initialize -- "$@" || return
1010

1111
local offset=0 i
1212
for ((i = 1; i <= cword; i++)); do

completions/_look

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
_look()
77
{
8-
local cur prev words cword
9-
_comp_initialize || return
8+
local cur prev words cword comp_args
9+
_comp_initialize -- "$@" || return
1010

1111
if ((cword == 1)); then
1212
COMPREPLY=($(compgen -W '$(look "$cur" 2>/dev/null)' -- "$cur"))

completions/_mock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
_mock()
77
{
8-
local cur prev words cword split
9-
_comp_initialize -s || return
8+
local cur prev words cword split comp_args
9+
_comp_initialize -s -- "$@" || return
1010

1111
local plugins='tmpfs root_cache yum_cache bind_mount ccache'
1212
local cfgdir=/etc/mock count=0 i

completions/_modules

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ _module_avail()
4747
# A completion function for the module alias
4848
_module()
4949
{
50-
local cur prev words cword
51-
_comp_initialize || return
50+
local cur prev words cword comp_args
51+
_comp_initialize -- "$@" || return
5252

5353
if ((cword == 1)); then
5454
# First parameter on line -- we expect it to be a mode selection

completions/_mount

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ fi
1616
#
1717
_mount()
1818
{
19-
local cur prev words cword
20-
_comp_initialize -n : || return
19+
local cur prev words cword comp_args
20+
_comp_initialize -n : -- "$@" || return
2121

2222
local sm host
2323

completions/_mount.linux

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
_mount()
77
{
8-
local cur prev words cword
9-
_comp_initialize -n =: || return
8+
local cur prev words cword comp_args
9+
_comp_initialize -n =: -- "$@" || return
1010

1111
local split=false
1212
case "$prev" in

completions/_newgrp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
_newgrp()
77
{
8-
local cur prev words cword
9-
_comp_initialize || return
8+
local cur prev words cword comp_args
9+
_comp_initialize -- "$@" || return
1010

1111
if [[ $cur == "-" ]]; then
1212
COMPREPLY=(-)

completions/_nmcli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ _nmcli_ab_bssid()
3636

3737
_nmcli()
3838
{
39-
local cur prev words cword
40-
_comp_initialize || return
39+
local cur prev words cword comp_args
40+
_comp_initialize -- "$@" || return
4141

4242
case $prev in
4343
-m | --mode)

completions/_op

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ _op_command_options()
2727

2828
_op()
2929
{
30-
local cur prev words cword split
31-
_comp_initialize -s || return
30+
local cur prev words cword split comp_args
31+
_comp_initialize -s -- "$@" || return
3232

3333
local command i
3434
for ((i = 1; i < cword; i++)); do

completions/_renice

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
_renice()
77
{
8-
local cur prev words cword
9-
_comp_initialize || return
8+
local cur prev words cword comp_args
9+
_comp_initialize -- "$@" || return
1010

1111
local command=$1 curopt i=0
1212

completions/_repomanage

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
_repomanage()
77
{
8-
local cur prev words cword split
9-
_comp_initialize -s || return
8+
local cur prev words cword split comp_args
9+
_comp_initialize -s -- "$@" || return
1010

1111
[[ $prev == -@([hk]|-help|-keep) ]] && return
1212

completions/_reptyr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
_reptyr()
77
{
8-
local cur prev words cword
9-
_comp_initialize || return
8+
local cur prev words cword comp_args
9+
_comp_initialize -- "$@" || return
1010

1111
case $prev in
1212
-l)

0 commit comments

Comments
 (0)