2013-07-04 11:18:52 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Description
|
|
|
|
# -----------
|
|
|
|
#
|
2016-08-15 00:45:00 +00:00
|
|
|
# sudo or sudoedit will be inserted before the command
|
2013-07-04 11:18:52 +00:00
|
|
|
#
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Authors
|
|
|
|
# -------
|
|
|
|
#
|
|
|
|
# * Dongweiming <ciici123@gmail.com>
|
2020-11-11 20:25:32 +00:00
|
|
|
# * Subhaditya Nath <github.com/subnut>
|
|
|
|
# * Marc Cornellà <github.com/mcornella>
|
2013-07-04 11:18:52 +00:00
|
|
|
#
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2020-11-11 20:25:32 +00:00
|
|
|
__sudo-replace-buffer() {
|
2021-08-19 15:20:52 +00:00
|
|
|
local old=$1 new=$2 space=${2:+ }
|
|
|
|
if [[ ${#LBUFFER} -le ${#old} ]]; then
|
|
|
|
RBUFFER="${space}${BUFFER#$old }"
|
|
|
|
LBUFFER="${new}"
|
|
|
|
else
|
|
|
|
LBUFFER="${new}${space}${LBUFFER#$old }"
|
|
|
|
fi
|
2020-11-11 20:25:32 +00:00
|
|
|
}
|
|
|
|
|
2013-07-04 11:18:52 +00:00
|
|
|
sudo-command-line() {
|
2021-08-19 16:20:57 +00:00
|
|
|
# If line is empty, get the last run command from history
|
2021-08-19 15:20:52 +00:00
|
|
|
[[ -z $BUFFER ]] && LBUFFER="$(fc -ln -1)"
|
2020-08-23 20:58:08 +00:00
|
|
|
|
2021-08-19 15:20:52 +00:00
|
|
|
# Save beginning space
|
|
|
|
local WHITESPACE=""
|
|
|
|
if [[ ${LBUFFER:0:1} = " " ]]; then
|
|
|
|
WHITESPACE=" "
|
|
|
|
LBUFFER="${LBUFFER:1}"
|
|
|
|
fi
|
2020-08-23 20:58:08 +00:00
|
|
|
|
2021-08-19 16:20:57 +00:00
|
|
|
# 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
|
2021-08-19 15:20:52 +00:00
|
|
|
local cmd="${${(Az)BUFFER}[1]}"
|
2021-08-19 16:20:57 +00:00
|
|
|
# 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
|
2020-11-11 20:25:32 +00:00
|
|
|
fi
|
|
|
|
|
2021-08-19 16:20:57 +00:00
|
|
|
# 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
|
2021-08-19 15:20:52 +00:00
|
|
|
fi
|
2020-08-23 20:58:08 +00:00
|
|
|
|
2021-08-19 15:20:52 +00:00
|
|
|
# Preserve beginning space
|
|
|
|
LBUFFER="${WHITESPACE}${LBUFFER}"
|
2021-08-19 16:20:57 +00:00
|
|
|
|
|
|
|
# Redisplay edit buffer (compatibility with zsh-syntax-highlighting)
|
|
|
|
zle redisplay
|
2013-07-04 11:18:52 +00:00
|
|
|
}
|
2020-11-11 20:25:32 +00:00
|
|
|
|
2013-07-04 11:18:52 +00:00
|
|
|
zle -N sudo-command-line
|
2020-11-11 20:25:32 +00:00
|
|
|
|
2013-07-04 11:18:52 +00:00
|
|
|
# Defined shortcut keys: [Esc] [Esc]
|
2019-08-19 10:14:22 +00:00
|
|
|
bindkey -M emacs '\e\e' sudo-command-line
|
|
|
|
bindkey -M vicmd '\e\e' sudo-command-line
|
|
|
|
bindkey -M viins '\e\e' sudo-command-line
|