summaryrefslogtreecommitdiff
path: root/plugins/alias-finder/alias-finder.plugin.zsh
diff options
context:
space:
mode:
authorTuowen Zhao <ztuowen@gmail.com>2023-11-04 18:38:46 -0700
committerTuowen Zhao <ztuowen@gmail.com>2023-11-04 18:38:46 -0700
commit4d908094fdc2a0c0e9a0a072eba213fab7adef43 (patch)
tree7c17e70bcdeebbe96c84d849bdf17882007480d8 /plugins/alias-finder/alias-finder.plugin.zsh
parent4b0bbc0b263a150eb9a9b59f196914629be06a9b (diff)
parent632ed413a9ce62747ded83d7736491b081be4b49 (diff)
downloadzsh-4d908094fdc2a0c0e9a0a072eba213fab7adef43.tar.gz
zsh-4d908094fdc2a0c0e9a0a072eba213fab7adef43.tar.bz2
zsh-4d908094fdc2a0c0e9a0a072eba213fab7adef43.zip
Merge remote-tracking branch 'github/master'HEADmaster
Diffstat (limited to 'plugins/alias-finder/alias-finder.plugin.zsh')
-rw-r--r--plugins/alias-finder/alias-finder.plugin.zsh79
1 files changed, 47 insertions, 32 deletions
diff --git a/plugins/alias-finder/alias-finder.plugin.zsh b/plugins/alias-finder/alias-finder.plugin.zsh
index caee9b5a3..5fdfbc835 100644
--- a/plugins/alias-finder/alias-finder.plugin.zsh
+++ b/plugins/alias-finder/alias-finder.plugin.zsh
@@ -1,44 +1,59 @@
alias-finder() {
- local cmd="" exact="" longer="" wordStart="" wordEnd="" multiWordEnd=""
- for i in $@; do
- case $i in
+ local cmd=" " exact="" longer="" cheaper="" wordEnd="'{0,1}$" finder="" filter=""
+
+ # 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;;
- *)
- if [[ -z $cmd ]]; then
- cmd=$i
- else
- cmd="$cmd $i"
- fi
- ;;
+ -c|--cheaper) cheaper=true;;
+ # concatenate cmd
+ *) cmd="$cmd$c " ;;
esac
done
- cmd=$(sed 's/[].\|$(){}?+*^[]/\\&/g' <<< $cmd) # adds escaping for grep
- if (( $(wc -l <<< $cmd) == 1 )); then
- while [[ $cmd != "" ]]; do
- if [[ $longer = true ]]; then
- wordStart="'{0,1}"
- else
- wordEnd="$"
- multiWordEnd="'$"
- fi
- if [[ $cmd == *" "* ]]; then
- local finder="'$cmd$multiWordEnd"
- else
- local finder=$wordStart$cmd$wordEnd
- fi
- alias | grep -E "=$finder"
- if [[ $exact = true || $longer = true ]]; then
- break
- else
- cmd=$(sed -E 's/ {0,1}[^ ]*$//' <<< $cmd) # removes last word
- fi
- 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 with spaces
+ ## - trim both ends
+ ## - replace multiple spaces with one space
+ ## - add escaping character to special characters
+ cmd=$(echo -n "$cmd" | tr '\n' ' ' | xargs | tr -s '[:space:]' | sed 's/[].\|$(){}?+*^[]/\\&/g')
+
+ 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="'{0,1}$cmd$wordEnd"
+
+ # make filter to find only shorter results than current cmd
+ if [[ $cheaper == true ]]; then
+ cmdLen=$(echo -n "$cmd" | wc -c)
+ filter="^'{0,1}.{0,$((cmdLen - 1))}="
+ fi
+
+ alias | grep -E "$filter" | grep -E "=$finder"
+
+ 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() {
- if [[ $ZSH_ALIAS_FINDER_AUTOMATIC = true ]]; then
+ # TODO: Remove backward compatibility (other than zstyle form)
+ zstyle -t ':omz:plugins:alias-finder' autoload && alias-finder $1 || if [[ $ZSH_ALIAS_FINDER_AUTOMATIC = true ]]; then
alias-finder $1
fi
}