summaryrefslogtreecommitdiff
path: root/plugins/sudo
diff options
context:
space:
mode:
authorMarc Cornellà <hello@mcornella.com>2021-08-19 18:20:57 +0200
committerMarc Cornellà <hello@mcornella.com>2021-08-27 18:08:51 +0200
commit190325049ef93731ab28295dbedf36d44ab33d7a (patch)
treeff7c73c504141c7e5a234492c8babc4d504a2487 /plugins/sudo
parenta1847dc860b85a0f0d97126ffe4b9b3a5276d1c1 (diff)
downloadzsh-190325049ef93731ab28295dbedf36d44ab33d7a.tar.gz
zsh-190325049ef93731ab28295dbedf36d44ab33d7a.tar.bz2
zsh-190325049ef93731ab28295dbedf36d44ab33d7a.zip
fix(sudo): allow different $EDITOR settings and fix zsh-syntax-highlighting redraw
Diffstat (limited to 'plugins/sudo')
-rw-r--r--plugins/sudo/sudo.plugin.zsh60
1 files changed, 44 insertions, 16 deletions
diff --git a/plugins/sudo/sudo.plugin.zsh b/plugins/sudo/sudo.plugin.zsh
index f0a01bc1f..e02f88a87 100644
--- a/plugins/sudo/sudo.plugin.zsh
+++ b/plugins/sudo/sudo.plugin.zsh
@@ -25,6 +25,7 @@ __sudo-replace-buffer() {
}
sudo-command-line() {
+ # If line is empty, get the last run command from history
[[ -z $BUFFER ]] && LBUFFER="$(fc -ln -1)"
# Save beginning space
@@ -34,29 +35,56 @@ sudo-command-line() {
LBUFFER="${LBUFFER:1}"
fi
- # Get the first part of the typed command and check if it's an alias to $EDITOR
- # If so, locally change $EDITOR to the alias so that it matches below
- if [[ -n "$EDITOR" ]]; then
+ # If $EDITOR is not set, just toggle the sudo prefix on and off
+ if [[ -z "$EDITOR" ]]; then
+ case "$BUFFER" in
+ sudoedit\ *) __sudo-replace-buffer "sudoedit" "" ;;
+ sudo\ *) __sudo-replace-buffer "sudo" "" ;;
+ *) LBUFFER="sudo $LBUFFER" ;;
+ esac
+ else
+ # Check if the typed command is really an alias to $EDITOR
+
+ # Get the first part of the typed command
local cmd="${${(Az)BUFFER}[1]}"
- if [[ "${aliases[$cmd]} " = (\$EDITOR|$EDITOR)\ * ]]; then
- local EDITOR="$cmd"
+ # Get the first part of the alias of the same name as $cmd, or $cmd if no alias matches
+ local realcmd="${${(Az)aliases[$cmd]}[1]:-$cmd}"
+ # Get the first part of the $EDITOR command ($EDITOR may have arguments after it)
+ local editorcmd="${${(Az)EDITOR}[1]}"
+
+ # Note: ${var:c} makes a $PATH search and expands $var to the full path
+ # The if condition is met when:
+ # - $realcmd is '$EDITOR'
+ # - $realcmd is "cmd" and $EDITOR is "cmd"
+ # - $realcmd is "cmd" and $EDITOR is "cmd --with --arguments"
+ # - $realcmd is "/path/to/cmd" and $EDITOR is "cmd"
+ # - $realcmd is "/path/to/cmd" and $EDITOR is "/path/to/cmd"
+ # or
+ # - $realcmd is "cmd" and $EDITOR is "cmd"
+ # - $realcmd is "cmd" and $EDITOR is "/path/to/cmd"
+ # or
+ # - $realcmd is "cmd" and $EDITOR is /alternative/path/to/cmd that appears in $PATH
+ if [[ "$realcmd" = (\$EDITOR|$editorcmd|${editorcmd:c}) \
+ || "${realcmd:c}" = ($editorcmd|${editorcmd:c}) ]] \
+ || builtin which -a "$realcmd" | command grep -Fx -q "$editorcmd"; then
+ editorcmd="$cmd" # replace $editorcmd with the typed command so it matches below
fi
- fi
- if [[ -n $EDITOR && $BUFFER = $EDITOR\ * ]]; then
- __sudo-replace-buffer "$EDITOR" "sudoedit"
- elif [[ -n $EDITOR && $BUFFER = \$EDITOR\ * ]]; then
- __sudo-replace-buffer "\$EDITOR" "sudoedit"
- elif [[ $BUFFER = sudoedit\ * ]]; then
- __sudo-replace-buffer "sudoedit" "$EDITOR"
- elif [[ $BUFFER = sudo\ * ]]; then
- __sudo-replace-buffer "sudo" ""
- else
- LBUFFER="sudo $LBUFFER"
+ # Check for editor commands in the typed command and replace accordingly
+ case "$BUFFER" in
+ $editorcmd\ *) __sudo-replace-buffer "$editorcmd" "sudoedit" ;;
+ \$EDITOR\ *) __sudo-replace-buffer '$EDITOR' "sudoedit" ;;
+ sudoedit\ *) __sudo-replace-buffer "sudoedit" "$EDITOR" ;;
+ sudo\ *) __sudo-replace-buffer "sudo" "" ;;
+ *) LBUFFER="sudo $LBUFFER" ;;
+ esac
fi
# Preserve beginning space
LBUFFER="${WHITESPACE}${LBUFFER}"
+
+ # Redisplay edit buffer (compatibility with zsh-syntax-highlighting)
+ zle redisplay
}
zle -N sudo-command-line