mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2025-12-12 01:52:31 +01:00
Merge a4816cab0d into 72acd2ca90
This commit is contained in:
commit
a2657c233c
1 changed files with 18 additions and 22 deletions
|
|
@ -1,19 +1,18 @@
|
||||||
alias-finder() {
|
alias-finder() {
|
||||||
local cmd=" " exact="" longer="" cheaper="" wordEnd="'{0,1}$" finder="" filter=""
|
local cmd=" " exact=false longer=false cheaper=false wordStart="^'?" wordEnd="'?$" finder="" filter=""
|
||||||
|
|
||||||
# build command and options
|
# setup options
|
||||||
|
# XXX: This logic has flaw. If user enable options with zstyle, there's no way to disable it.
|
||||||
|
# It's because same function is used for autoload hook and manual execution.
|
||||||
|
# I believe manual execution is very minor in use, so I'll keep it as is for now.
|
||||||
for c in "$@"; do
|
for c in "$@"; do
|
||||||
case $c in
|
case $c in
|
||||||
# TODO: Remove backward compatibility (other than zstyle form)
|
|
||||||
# set options if exist
|
|
||||||
-e|--exact) exact=true;;
|
-e|--exact) exact=true;;
|
||||||
-l|--longer) longer=true;;
|
-l|--longer) longer=true;;
|
||||||
-c|--cheaper) cheaper=true;;
|
-c|--cheaper) cheaper=true;;
|
||||||
# concatenate cmd
|
|
||||||
*) cmd="$cmd$c " ;;
|
*) cmd="$cmd$c " ;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
zstyle -t ':omz:plugins:alias-finder' longer && longer=true
|
zstyle -t ':omz:plugins:alias-finder' longer && longer=true
|
||||||
zstyle -t ':omz:plugins:alias-finder' exact && exact=true
|
zstyle -t ':omz:plugins:alias-finder' exact && exact=true
|
||||||
zstyle -t ':omz:plugins:alias-finder' cheaper && cheaper=true
|
zstyle -t ':omz:plugins:alias-finder' cheaper && cheaper=true
|
||||||
|
|
@ -25,13 +24,11 @@ alias-finder() {
|
||||||
## - add escaping character to special characters
|
## - add escaping character to special characters
|
||||||
cmd=$(echo -n "$cmd" | tr '\n' ' ' | xargs | tr -s '[:space:]' | sed 's/[].\|$(){}?+*^[]/\\&/g')
|
cmd=$(echo -n "$cmd" | tr '\n' ' ' | xargs | tr -s '[:space:]' | sed 's/[].\|$(){}?+*^[]/\\&/g')
|
||||||
|
|
||||||
if [[ $longer == true ]]; then
|
$longer && wordEnd=".*$"
|
||||||
wordEnd="" # remove wordEnd to find longer aliases
|
|
||||||
fi
|
|
||||||
|
|
||||||
# find with alias and grep, removing last word each time until no more words
|
# find with alias and grep, removing last word each time until no more words
|
||||||
while [[ $cmd != "" ]]; do
|
while [[ $cmd != "" ]]; do
|
||||||
finder="'{0,1}$cmd$wordEnd"
|
finder="$wordStart$cmd$wordEnd"
|
||||||
|
|
||||||
# make filter to find only shorter results than current cmd
|
# make filter to find only shorter results than current cmd
|
||||||
if [[ $cheaper == true ]]; then
|
if [[ $cheaper == true ]]; then
|
||||||
|
|
@ -49,22 +46,21 @@ alias-finder() {
|
||||||
alias | grep -E "$filter" | grep -E "=$finder"
|
alias | grep -E "$filter" | grep -E "=$finder"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ $exact == true ]]; then
|
$exact && break # because exact case is only one
|
||||||
break # because exact case is only one
|
$longer && break # because above grep command already found every longer aliases during first cycle
|
||||||
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
|
cmd=$(sed -E 's/ {0,}[^ ]*$//' <<< "$cmd") # remove last word
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# add hook to run alias-finder before each command
|
||||||
preexec_alias-finder() {
|
preexec_alias-finder() {
|
||||||
# TODO: Remove backward compatibility (other than zstyle form)
|
alias-finder "$1"
|
||||||
zstyle -t ':omz:plugins:alias-finder' autoload && alias-finder $1 || if [[ $ZSH_ALIAS_FINDER_AUTOMATIC = true ]]; then
|
|
||||||
alias-finder $1
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
if zstyle -t ':omz:plugins:alias-finder' autoload ; then
|
||||||
autoload -U add-zsh-hook
|
autoload -Uz alias-finder
|
||||||
add-zsh-hook preexec preexec_alias-finder
|
add-zsh-hook preexec preexec_alias-finder
|
||||||
|
elif [[ $ZSH_ALIAS_FINDER_AUTOMATIC = true ]]; then # TODO: remove this legacy style support
|
||||||
|
autoload -Uz alias-finder
|
||||||
|
add-zsh-hook preexec preexec_alias-finder
|
||||||
|
fi
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue