mirror of
https://github.com/robbyrussell/oh-my-zsh.git
synced 2026-11-28 19:33:03 +01:00
The automatic hook searched the command exactly as typed, so `k get pods` never matched `kgp='kubectl get pods'`. It now searches the alias-expanded command zsh passes to preexec ($3). When the command started with an alias, only aliases that cover more of the command are suggested, so typing `gst` alone still prints nothing. Closes #12040 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
86 lines
2.6 KiB
Bash
86 lines
2.6 KiB
Bash
alias-finder() {
|
|
local c="" cmd=" " exact="" longer="" cheaper="" wordEnd="'?$" finder="" filter="" cmdLen=""
|
|
|
|
# build command and options
|
|
for c in "$@"; do
|
|
case $c in
|
|
# TODO: Remove backward compatibility (other than zstyle form)
|
|
# set options if exist
|
|
-e|--exact) exact=true;;
|
|
-l|--longer) longer=true;;
|
|
-c|--cheaper) cheaper=true;;
|
|
# concatenate cmd
|
|
*) cmd="$cmd$c " ;;
|
|
esac
|
|
done
|
|
|
|
zstyle -t ':omz:plugins:alias-finder' longer && longer=true
|
|
zstyle -t ':omz:plugins:alias-finder' exact && exact=true
|
|
zstyle -t ':omz:plugins:alias-finder' cheaper && cheaper=true
|
|
|
|
# format cmd for grep
|
|
## - replace newlines and multiple spaces with one space
|
|
## - trim both ends
|
|
## - add escaping character to special characters
|
|
setopt localoptions extendedglob
|
|
cmd=${cmd//[[:space:]]##/ }
|
|
cmd=${cmd# }
|
|
cmd=${cmd% }
|
|
local esc_pattern='(#m)[][.\\|$(){}?+*^]' MATCH MBEGIN MEND
|
|
cmd=${cmd//${~esc_pattern}/\\$MATCH}
|
|
|
|
if [[ $longer == true ]]; then
|
|
wordEnd="" # remove wordEnd to find longer aliases
|
|
fi
|
|
|
|
# find with alias and grep, removing last word each time until no more words
|
|
while [[ $cmd != "" ]]; do
|
|
finder="'?$cmd$wordEnd"
|
|
|
|
# make filter to find only shorter results than current cmd
|
|
if [[ $cheaper == true ]]; then
|
|
cmdLen=${#cmd}
|
|
if [[ $cmdLen -le 1 ]]; then
|
|
return
|
|
fi
|
|
|
|
filter="^'?.{1,$((cmdLen - 1))}'?=" # some aliases is surrounded by single quotes
|
|
fi
|
|
|
|
if (( $+commands[rg] )); then
|
|
alias | rg "$filter" | rg "=$finder"
|
|
else
|
|
alias | grep -E "$filter" | grep -E "=$finder"
|
|
fi
|
|
|
|
if [[ $exact == true ]]; then
|
|
break # because exact case is only one
|
|
elif [[ $longer == true ]]; then
|
|
break # because above grep command already found every longer aliases during first cycle
|
|
fi
|
|
|
|
cmd=$(sed -E 's/ {0,}[^ ]*$//' <<< "$cmd") # remove last word
|
|
done
|
|
}
|
|
|
|
preexec_alias-finder() {
|
|
# TODO: Remove backward compatibility (other than zstyle form)
|
|
zstyle -t ':omz:plugins:alias-finder' autoload || [[ $ZSH_ALIAS_FINDER_AUTOMATIC = true ]] || return
|
|
|
|
# $3 has aliases expanded, so `k get pods` can suggest `kgp`
|
|
local -a words=(${(z)1}) found=(${(f)"$(alias-finder $3)"}) keep
|
|
local used=${aliases[$words[1]]} line
|
|
|
|
# If an alias was typed, only suggest aliases that cover more of the command
|
|
if [[ -n "$used" ]]; then
|
|
for line in $found; do
|
|
(( ${#${(Q)line#*=}} > ${#used} )) && keep+=("$line")
|
|
done
|
|
found=($keep)
|
|
fi
|
|
(( $#found )) && print -rl -- $found
|
|
}
|
|
|
|
autoload -U add-zsh-hook
|
|
add-zsh-hook preexec preexec_alias-finder
|