Skip to content

Commit 3366080

Browse files
committed
refactor(_comp_quote_compgen): store result in ret
1 parent 27eb6c2 commit 3366080

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

bash_completion

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ _comp_quote()
139139
quote_readline()
140140
{
141141
local ret
142-
_comp_quote_compgen "$1" ret
142+
_comp_quote_compgen "$1"
143143
printf %s "$ret"
144144
} # quote_readline()
145145

@@ -624,7 +624,7 @@ __ltrim_colon_completions()
624624

625625
# This function quotes the argument in a way so that readline dequoting
626626
# results in the original argument. This is necessary for at least
627-
# `compgen' which requires its arguments quoted/escaped:
627+
# `compgen` which requires its arguments quoted/escaped:
628628
#
629629
# $ ls "a'b/"
630630
# c
@@ -635,25 +635,25 @@ __ltrim_colon_completions()
635635
# See also:
636636
# - https://lists.gnu.org/archive/html/bug-bash/2009-03/msg00155.html
637637
# - https://www.mail-archive.com/[email protected]/msg01944.html
638-
# @param $1 Argument to quote
639-
# @param $2 Name of variable to return result to
638+
# @param $1 Argument to quote
639+
# @var[out] ret Quoted result is stored in this variable
640+
# shellcheck disable=SC2178 # The assignment is not intended for the global "ret"
640641
_comp_quote_compgen()
641642
{
642643
if [[ $1 == \'* ]]; then
643644
# Leave out first character
644-
printf -v "$2" %s "${1:1}"
645+
ret=${1:1}
645646
else
646-
printf -v "$2" %q "$1"
647+
printf -v ret %q "$1"
647648

648649
# If result becomes quoted like this: $'string', re-evaluate in order
649650
# to drop the additional quoting. See also:
650651
# https://www.mail-archive.com/[email protected]/msg01942.html
651-
if [[ ${!2} == \$\'*\' ]]; then
652-
local value=${!2:2:-1} # Strip beginning $' and ending '.
653-
value=${value//'%'/%%} # Escape % for printf format.
652+
if [[ $ret == \$\'*\' ]]; then
653+
local value=${ret:2:-1} # Strip beginning $' and ending '.
654+
value=${value//'%'/%%} # Escape % for printf format.
654655
# shellcheck disable=SC2059
655-
printf -v value "$value" # Decode escape sequences of \....
656-
local "$2" && _comp_upvars -v "$2" "$value"
656+
printf -v ret "$value" # Decode escape sequences of \....
657657
fi
658658
fi
659659
} # _comp_quote_compgen()
@@ -681,8 +681,9 @@ _filedir()
681681
$reset
682682
IFS=$'\n'
683683
else
684-
local quoted
685-
_comp_quote_compgen "${cur-}" quoted
684+
local ret
685+
_comp_quote_compgen "${cur-}"
686+
local quoted=$ret
686687

687688
# Munge xspec to contain uppercase version too
688689
# https://lists.gnu.org/archive/html/bug-bash/2010-09/msg00036.html

bash_completion.d/000_bash_completion_compat.bash

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ _comp_deprecate_func _upvars _comp_upvars
1313
_comp_deprecate_func __reassemble_comp_words_by_ref _comp__reassemble_words
1414
_comp_deprecate_func __get_cword_at_cursor_by_ref _comp__get_cword_at_cursor
1515
_comp_deprecate_func _get_comp_words_by_ref _comp_get_words
16-
_comp_deprecate_func _quote_readline_by_ref _comp_quote_compgen
1716

1817
# Backwards compatibility for compat completions that use have().
1918
# @deprecated should no longer be used; generally not needed with dynamically
@@ -35,6 +34,19 @@ quote()
3534
printf "'%s'" "$quoted"
3635
}
3736

37+
# This function is the same as `_comp_quote_compgen`, but receives the second
38+
# argument specifying the variable name to store the result.
39+
# @param $1 Argument to quote
40+
# @param $2 Name of variable to return result to
41+
# @deprecated Use `_comp_quote_compgen "$1"` instead. Note that
42+
# `_comp_quote_compgen` stores the result in a fixed variable `ret`.
43+
_quote_readline_by_ref()
44+
{
45+
[[ $2 == ret ]] || local ret
46+
_comp_quote_compgen "$1"
47+
[[ $2 == ret ]] || printf -v "$2" %s "$ret"
48+
}
49+
3850
# This function shell-dequotes the argument
3951
# @deprecated Use `_comp_dequote' instead. Note that `_comp_dequote` stores
4052
# the results in the array `ret` instead of writing them to stdout.

0 commit comments

Comments
 (0)