From d05feb1f8ef82ec44e1de8cd77a74159a01b9106 Mon Sep 17 00:00:00 2001 From: Frederick Zhang Date: Sat, 21 Sep 2019 16:14:31 +1000 Subject: [PATCH] do_sudo: new sudo alias to handle alias expansion --- plugins/do_sudo/README.md | 6 ++++++ plugins/do_sudo/do_sudo.plugin.zsh | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 plugins/do_sudo/README.md create mode 100644 plugins/do_sudo/do_sudo.plugin.zsh diff --git a/plugins/do_sudo/README.md b/plugins/do_sudo/README.md new file mode 100644 index 000000000..1d1b9e7d5 --- /dev/null +++ b/plugins/do_sudo/README.md @@ -0,0 +1,6 @@ +# `do_sudo` plugin + +This plugin provides a sudo wrapper that handles alias expansion and avoids +being broken by `nocorrect` `noglob` from other aliases. + +Modified from the script by [Wayne Davison](https://www.zsh.org/mla/users/2008/msg01229.html). diff --git a/plugins/do_sudo/do_sudo.plugin.zsh b/plugins/do_sudo/do_sudo.plugin.zsh new file mode 100644 index 000000000..b59f3ac6a --- /dev/null +++ b/plugins/do_sudo/do_sudo.plugin.zsh @@ -0,0 +1,27 @@ +if [[ "$ENABLE_CORRECTION" == "true" ]]; then + alias sudo='nocorrect noglob _do_sudo ' +else + alias sudo='noglob _do_sudo ' +fi + +function _do_sudo() { + integer glob=1 + local -a run + run=( command sudo ) + while (($#)); do + case "$1" in + command|exec|-) shift; break ;; + nocorrect) shift ;; + noglob) glob=0; shift ;; + *) break ;; + esac + done + if ((glob)); then + PATH="/sbin:/usr/sbin:/usr/local/sbin:$PATH" $run $~==* + else + PATH="/sbin:/usr/sbin:/usr/local/sbin:$PATH" $run $==* + fi +} + +command -v _sudo >/dev/null 2>&1 +[[ $? -eq 0 ]] && compdef _sudo '_do_sudo'