Skip to content
Myr

[bash] Refactor access to "_fzf_orig_complete_${cmd//[^A-Za-z0-9_]/_}"

Before adoption: this commit came in with the history the project adopted, so no landing records it.

In the current codebase, for the original completion settings, the pieces of the codes to determine the variable name and to access the stored data are scattered. In this patch, we define functions to access these variables. Those functions will be used in a coming patch. * This patch 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: $ COMP_WORDBREAKS=${COMP_WORDBREAKS//=} $ _test1() { COMPREPLY=(); } $ complete -vF _test1 cmd.v=1.0 $ _fzf_setup_completion path cmd.v=1.0 $ cmd.v=1.0 [TAB] bash: _fzf_orig_completion_cmd_v=1_0: invalid variable name The behavior of leaving "=" was present from the beginning when saving the original completion is introduced in commit 91401514, and this does not seem to be a specific reasoning. In this patch, we replace "=" as well as the other non-identifier characters. * Note: In this patch, the variable REPLY is used to return values from functions. This design is to make it useful with the value substitutions, a new Bash feature of the next release 5.3, which is taken from mksh.

authored by Koichi Murase · committed by Junegunn Choi · parent c70f0ea · Browse files · 1 file+31 added−14 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+31 added−14 removed

  • other:+31 added−14 removed

1 file+31 added−14 removed

modified:shell/completion.bash+31 added-14 removed
61 unmodified lines
62 fi63 done64}6566# @param $1 cmd - Command name for which the original completion is searched67# @var[out] REPLY - Original function name is returned68__fzf_orig_completion_get_orig_func() {69 local cmd orig_var orig70 cmd=$171 orig_var="_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}"72 orig="${!orig_var-}"73 REPLY="${orig##*#}"74 [[ $REPLY ]] && type "$REPLY" &> /dev/null75}7677# @param $1 cmd - Command name for which the original completion is searched78# @param $2 func - Fzf's completion function to replace the original function79# @var[out] REPLY - Completion setting is returned as a string to "eval"80__fzf_orig_completion_instantiate() {81 local cmd func orig_var orig82 cmd=$183 func=$284 orig_var="_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}"85 orig="${!orig_var-}"86 orig="${orig%#*}"87 [[ $orig == *' %s '* ]] || return 188 printf -v REPLY "$orig" "$func"89}9091_fzf_opts_completion() {92 local cur prev opts93 COMPREPLY=()94 cur="${COMP_WORDS[COMP_CWORD]}"
190 unmodified lines
285 return 0286}287288_fzf_handle_dynamic_completion() {264 local cmd orig_var orig ret orig_cmd orig_complete289 local cmd ret REPLY orig_cmd orig_complete290 cmd="$1"291 shift292 orig_cmd="$1"268 orig_var="_fzf_orig_completion_$cmd"269 orig="${!orig_var-}"270 orig="${orig##*#}"271 if [[ -n "$orig" ]] && type "$orig" > /dev/null 2>&1; then272 $orig "$@"293 if __fzf_orig_completion_get_orig_func "$cmd"; then294 "$REPLY" "$@"295 elif [[ -n "${_fzf_completion_loader-}" ]]; then296 orig_complete=$(complete -p "$orig_cmd" 2> /dev/null)297 _completion_loader "$@"298 ret=$?
15 unmodified lines
314 cmd="${COMP_WORDS[0]}"315 if [[ $cmd == \\* ]]; then316 cmd="${cmd:1}"317 fi296 cmd="${cmd//[^A-Za-z0-9_=]/_}"318 COMPREPLY=()319 trigger=${FZF_COMPLETION_TRIGGER-'**'}320 cur="${COMP_WORDS[COMP_CWORD]}"321 if [[ "$cur" == *"$trigger" ]] && [[ $cur != *'$('* ]] && [[ $cur != *':='* ]] && [[ $cur != *'`'* ]]; then
57 unmodified lines
379 local cur selected trigger cmd post380 post="$(caller 0 | command awk '{print $2}')_post"381 type -t "$post" > /dev/null 2>&1 || post='command cat'382362 cmd="${COMP_WORDS[0]//[^A-Za-z0-9_=]/_}"383 trigger=${FZF_COMPLETION_TRIGGER-'**'}384 cur="${COMP_WORDS[COMP_CWORD]}"385 if [[ "$cur" == *"$trigger" ]] && [[ $cur != *'$('* ]] && [[ $cur != *':='* ]] && [[ $cur != *'`'* ]]; then386 cur=${cur:0:${#cur}-${#trigger}}
120 unmodified lines
507 _fzf_completion_loader=1508fi509510__fzf_defc() {491 local cmd func opts orig_var orig def511 local cmd func opts REPLY512 cmd="$1"513 func="$2"514 opts="$3"495 orig_var="_fzf_orig_completion_${cmd//[^A-Za-z0-9_]/_}"496 orig="${!orig_var-}"497 if [[ -n "$orig" ]]; then498 printf -v def "$orig" "$func"499 eval "$def"515 if __fzf_orig_completion_instantiate "$cmd" "$func"; then516 eval "$REPLY"517 else518 complete -F "$func" $opts "$cmd"519 fi520}
41 unmodified lines