summaryrefslogtreecommitdiff
path: root/plugins/gnu-utils
diff options
context:
space:
mode:
authorTuowen Zhao <ztuowen@gmail.com>2022-01-01 02:26:11 -0600
committerTuowen Zhao <ztuowen@gmail.com>2022-01-01 02:26:11 -0600
commit49edbf438ed690c76e6b2af80368c59404cf0167 (patch)
tree129b3adb2f5f39a1329a426a3b7d51ed2c2290c1 /plugins/gnu-utils
parent1bc186dabe12b3d01b2257e82f3a104c48b8b3c7 (diff)
parent78c91ccbf99c77bd4d9cdb74279a40776721f66d (diff)
downloadzsh-49edbf438ed690c76e6b2af80368c59404cf0167.tar.gz
zsh-49edbf438ed690c76e6b2af80368c59404cf0167.tar.bz2
zsh-49edbf438ed690c76e6b2af80368c59404cf0167.zip
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'plugins/gnu-utils')
-rw-r--r--plugins/gnu-utils/gnu-utils.plugin.zsh43
1 files changed, 10 insertions, 33 deletions
diff --git a/plugins/gnu-utils/gnu-utils.plugin.zsh b/plugins/gnu-utils/gnu-utils.plugin.zsh
index 539e7fe4d..6ede9e3a7 100644
--- a/plugins/gnu-utils/gnu-utils.plugin.zsh
+++ b/plugins/gnu-utils/gnu-utils.plugin.zsh
@@ -11,11 +11,8 @@ if [[ ! -x "${commands[gwhoami]}" ]]; then
fi
__gnu_utils() {
- emulate -L zsh
- local gcmds
+ local -a gcmds
local gcmd
- local cmd
- local prefix
# coreutils
gcmds=('g[' 'gbase64' 'gbasename' 'gcat' 'gchcon' 'gchgrp' 'gchmod'
@@ -43,41 +40,21 @@ __gnu_utils() {
(( ${+commands[$gcmd]} )) || continue
# This method allows for builtin commands to be primary but it's
- # lost if hash -r or rehash -f is executed. Thus, those two
- # functions have to be wrapped.
+ # lost if hash -r or rehash is executed, or if $PATH is updated.
+ # Thus, a preexec hook is needed, which will only run if whoami
+ # is not already rehashed.
#
hash ${gcmd[2,-1]}=${commands[$gcmd]}
-
- # This method generates wrapper functions.
- # It will override shell builtins.
- #
- # eval "function $gcmd[2,-1]() { \"${prefix}/${gcmd//"["/"\\["}\" \"\$@\"; }"
-
- # This method is inflexible since the aliases are at risk of being
- # overridden resulting in the BSD coreutils being called.
- #
- # alias "$gcmd[2,-1]"="${prefix}/${gcmd//"["/"\\["}"
done
return 0
}
-__gnu_utils
-
-function hash() {
- if [[ "$*" =~ "-(r|f)" ]]; then
- builtin hash "$@"
- __gnu_utils
- else
- builtin hash "$@"
- fi
-}
-function rehash() {
- if [[ "$*" =~ "-f" ]]; then
- builtin rehash "$@"
- __gnu_utils
- else
- builtin rehash "$@"
- fi
+__gnu_utils_preexec() {
+ # Run __gnu_utils when the whoami command is not already rehashed.
+ # This acts as a sign that we need to rehash all GNU utils.
+ [[ "${commands[whoami]}" = "${commands[gwhoami]}" ]] || __gnu_utils
}
+autoload -Uz add-zsh-hook
+add-zsh-hook preexec __gnu_utils_preexec