Skip to content

Commit

Permalink
Fix bug in _approximate patch
Browse files Browse the repository at this point in the history
  • Loading branch information
marlonrichert committed May 27, 2023
1 parent d62808d commit 57fbd04
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions scripts/.autocomplete.compinit
Original file line number Diff line number Diff line change
Expand Up @@ -178,44 +178,44 @@ EOF
(( compstate[nmatches] > nmatches ))
}


##
# WORKAROUND: _approximate won't do corrections if there already is a function called 'compadd'.
#

.autocomplete.patch _approximate
_approximate() {
[[ -z $words[CURRENT] || -v compstate[quote] ]] &&
return 1
[[ -o banghist && $words[CURRENT] == [$histchars]* ]] &&
return 1
_autocomplete.is_glob &&
return 1

local -Pi ret=1
{
[[ -v functions[compadd] ]] &&
functions[autocomplete:_approximate:compadd:old]=$functions[compadd]
functions[autocomplete:compadd:old]="$functions[compadd]"
functions[compadd]="$functions[autocomplete:approximate:compadd]"

compadd() {
local -P ppre="$argv[(I)-p]"
autocomplete:_approximate:old
} always {
unfunction compadd 2> /dev/null
if [[ -v functions[autocomplete:compadd:old] ]]; then
functions[compadd]="$functions[autocomplete:compadd:old]"
unfunction autocomplete:compadd:old
fi
}
}

autocomplete:approximate:compadd() {
local ppre="$argv[(I)-p]"

[[ ${argv[(I)-[a-zA-Z]#U[a-zA-Z]#]} -eq 0 && "${#:-$PREFIX$SUFFIX}" -le _comp_correct ]] &&
return
[[ ${argv[(I)-[a-zA-Z]#U[a-zA-Z]#]} -eq 0 &&
"${#:-$PREFIX$SUFFIX}" -le _comp_correct ]] && return

if [[ "$PREFIX" = \~* && ( $ppre -eq 0 || "$argv[ppre+1]" != \~* ) ]]; then
PREFIX="~(#a$_comp_correct)$PREFIX[2,-1]"
if [[ "$PREFIX" = \~* && ( ppre -eq 0 || "$argv[ppre+1]" != \~* ) ]]; then
PREFIX="~(#a${_comp_correct})${PREFIX[2,-1]}"
else
PREFIX="(#a$_comp_correct)$PREFIX"
PREFIX="(#a${_comp_correct})$PREFIX"
fi

if [[ -v functions[autocomplete:compadd:old] ]]; then
autocomplete:compadd:old "$@"
else
builtin compadd "$@"
}

autocomplete:_approximate:old "$@"
ret=$?
_lastdescr=( ${_lastdescr[@]:#corrections} )
} always {
if [[ -v functions[autocomplete:_approximate:compadd:old] ]]; then
functions[compadd]=$functions[autocomplete:_approximate:compadd:old]
unfunction autocomplete:_approximate:compadd:old
fi
}
return ret
}
}

0 comments on commit 57fbd04

Please sign in to comment.