[bash] Update the completion setting after _completion_loader
#3667
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Original problem
This is a fix for an issue reported at scop/bash-completion#1123. The original problem is that the fzf completion setting does not fully preserve the completion settings of
cd
set bybash-completion-2.12
. More specifically,bash-completion
completes also variable names through the-v
option ofcomplete
whenshopt -s autocd
shopt -s cdable_vars
is specified, but the-v
option is lost for bash-completion 2.12 when Fzf's completion setting is present. See the original thread for the details.Description of the problem
The problem is that when the completion setting is loaded by
_fzf_handle_dynamic_completion
usingbash-completion
's_completion_loader
, only the function specified by-F
is used byfzf
. The completion settings specified by othercomplete
options are ignored.The problem does not arise when the completion setting already exists when Fzf's
completion.bash
is loaded or when_fzf_setup_completion
is called. This is because__fzf_defc
preserves the othercomplete
options. The reason that the problem was not observed with bash-completion 2.11 is that bash-completion 2.12 started to load the completion setting ofcd
dynamically.Solution
When the completion settings are loaded using
_completion_loader
, the othercomplete
options should be applied to the completion setting in the same way as__fzf_defc
does.The main fix is in commit c732298.
Associated refactoring
To reuse existing codes, there is a refactoring commit (0214e50) before the main commit. In the current codebase, for the original completion settings, the pieces of the codes to determine the variable name and access the stored data are scattered. In this commit, we define functions to access these variables. The functions are used in the main fix of this PR.
This refactoring also resolves an inconsistent escaping of
$cmd
:$cmd
is escaped as${...//[^A-Za-z0-9_]/_}
in some places, but it is escaped as${...//[^A-Za-z0-9_=]/_}
in some other places. The latter leaves the character=
in the command name, which causes an issue because=
cannot be a part of a variable name. For example, the following test case produces an error message:The behavior of leaving
=
was present from the beginning when saving the original completion is introduced in commit 9140151, and this does not seem to have specific reasoning. We replace=
as well as the other non-identifier characters.Note: In the new functions, the variable
REPLY
is used to return values. This design is to make it useful with the value substitutions, which is a new Bash feature of the next release 5.3, and was originally the feature introduced bymksh
.