Skip to content

Commit e686f85

Browse files
committed
refactor(_comp_initialize): split={true,false} => was_split={set,}
1 parent 5a1657c commit e686f85

File tree

2 files changed

+66
-13
lines changed

2 files changed

+66
-13
lines changed

bash_completion

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -983,21 +983,21 @@ _comp_variable_assignments()
983983
# argument $2 is the string before the cursor in the
984984
# current word. The third argument $3 is the previous
985985
# word.
986-
# @var[out] cur Reconstructed current word
987-
# @var[out] prev Reconstructed previous word
988-
# @var[out] words Reconstructed words
989-
# @var[out] cword Current word index in `words`
990-
# @var[out] comp_args Original arguments specified to the completion function
991-
# are saved in this array, if the arguments $1...$3 is
992-
# specified.
993-
# @var[out,opt] split When "-s" is specified, `true/false` is set depending on
994-
# whether the split happened.
986+
# @var[out] cur Reconstructed current word
987+
# @var[out] prev Reconstructed previous word
988+
# @var[out] words Reconstructed words
989+
# @var[out] cword Current word index in `words`
990+
# @var[out] comp_args Original arguments specified to the completion function
991+
# are saved in this array, if the arguments $1...$3 is
992+
# specified.
993+
# @var[out,opt] was_split When "-s" is specified, `"set"/""` is set depending
994+
# on whether the split happened.
995995
# @return True (0) if completion needs further processing,
996996
# False (> 0) no further processing is necessary.
997997
#
998998
_comp_initialize()
999999
{
1000-
local exclude="" outx errx inx
1000+
local exclude="" opt_split="" outx errx inx
10011001

10021002
local flag OPTIND=1 OPTARG="" OPTERR=0
10031003
while getopts "n:e:o:i:s" flag "$@"; do
@@ -1007,7 +1007,8 @@ _comp_initialize()
10071007
o) outx=$OPTARG ;;
10081008
i) inx=$OPTARG ;;
10091009
s)
1010-
split=false
1010+
opt_split="set"
1011+
was_split=""
10111012
exclude+="="
10121013
;;
10131014
*)
@@ -1065,7 +1066,7 @@ _comp_initialize()
10651066
((cword <= 0)) && return 1
10661067
prev=${words[cword - 1]}
10671068

1068-
[[ ${split-} ]] && _split_longopt && split=true
1069+
[[ $opt_split ]] && _split_longopt && was_split="set"
10691070

10701071
return 0
10711072
}

bash_completion.d/000_bash_completion_compat.bash

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ _comp_deprecate_func _userland _comp_userland
44
_comp_deprecate_func _sysvdirs _comp_sysvdirs
55
_comp_deprecate_func _have _comp_have_command
66
_comp_deprecate_func _rl_enabled _comp_readline_variable_on
7-
_comp_deprecate_func _init_completion _comp_initialize
87
_comp_deprecate_func _command_offset _comp_command_offset
98
_comp_deprecate_func _command _comp_command
109
_comp_deprecate_func _root_command _comp_root_command
@@ -178,6 +177,59 @@ _realcommand()
178177
return $rc
179178
}
180179

180+
# Initialize completion and deal with various general things: do file
181+
# and variable completion where appropriate, and adjust prev, words,
182+
# and cword as if no redirections exist so that completions do not
183+
# need to deal with them. Before calling this function, make sure
184+
# cur, prev, words, and cword are local, ditto split if you use -s.
185+
#
186+
# Options:
187+
# -n EXCLUDE Passed to _comp_get_words -n with redirection chars
188+
# -e XSPEC Passed to _filedir as first arg for stderr redirections
189+
# -o XSPEC Passed to _filedir as first arg for other output redirections
190+
# -i XSPEC Passed to _filedir as first arg for stdin redirections
191+
# -s Split long options with _split_longopt, implies -n =
192+
# @var[out] cur Reconstructed current word
193+
# @var[out] prev Reconstructed previous word
194+
# @var[out] words Reconstructed words
195+
# @var[out] cword Current word index in `words`
196+
# @var[out,opt] split When "-s" is specified, `"true"/"false"` is set depending
197+
# on whether the split happened.
198+
# @return True (0) if completion needs further processing,
199+
# False (> 0) no further processing is necessary.
200+
#
201+
# @deprecated Use the new interface `_comp_initialize`. The new interface
202+
# supports the same set of options. The new interface receives additional
203+
# arguments $1 (command name), $2 (part of current word before the cursor), and
204+
# $3 (previous word) that are specified to the completion function by Bash.
205+
# When `-s` is specified, instead of variable `split`, the new interface sets
206+
# variable `was_split` to the value "set"/"" when the split happened/not
207+
# happened.
208+
_init_completion()
209+
{
210+
local was_split
211+
_comp_initialize "$@"
212+
local rc=$?
213+
214+
# When -s is specified, convert "split={set,}" to "split={true,false}"
215+
local flag OPTIND=1 OPTARG="" OPTERR=0
216+
while getopts "n:e:o:i:s" flag "$@"; do
217+
case $flag in
218+
[neoi]) ;;
219+
s)
220+
if [[ $was_split ]]; then
221+
split=true
222+
else
223+
split=false
224+
fi
225+
break
226+
;;
227+
esac
228+
done
229+
230+
return "$rc"
231+
}
232+
181233
# @deprecated Use the variable `_comp_backup_glob` instead. This is the
182234
# backward-compatibility name.
183235
# shellcheck disable=SC2154 # defined in the main "bash_completion"

0 commit comments

Comments
 (0)