mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2024-11-18 03:40:08 +00:00
style(sudo): clean code style and reorganise logic
This commit is contained in:
parent
540b2200af
commit
957dca698c
1 changed files with 29 additions and 21 deletions
|
@ -17,9 +17,13 @@
|
||||||
|
|
||||||
__sudo-replace-buffer() {
|
__sudo-replace-buffer() {
|
||||||
local old=$1 new=$2 space=${2:+ }
|
local old=$1 new=$2 space=${2:+ }
|
||||||
if [[ ${#LBUFFER} -le ${#old} ]]; then
|
|
||||||
RBUFFER="${space}${BUFFER#$old }"
|
# if the cursor is positioned in the $old part of the text, make
|
||||||
LBUFFER="${new}"
|
# the substitution and leave the cursor after the $new text
|
||||||
|
if [[ $CURSOR -le ${#old} ]]; then
|
||||||
|
BUFFER="${new}${space}${BUFFER#$old }"
|
||||||
|
CURSOR=${#new}
|
||||||
|
# otherwise just replace $old with $new in the text before the cursor
|
||||||
else
|
else
|
||||||
LBUFFER="${new}${space}${LBUFFER#$old }"
|
LBUFFER="${new}${space}${LBUFFER#$old }"
|
||||||
fi
|
fi
|
||||||
|
@ -36,6 +40,7 @@ sudo-command-line() {
|
||||||
LBUFFER="${LBUFFER:1}"
|
LBUFFER="${LBUFFER:1}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
{
|
||||||
# If $SUDO_EDITOR or $VISUAL are defined, then use that as $EDITOR
|
# If $SUDO_EDITOR or $VISUAL are defined, then use that as $EDITOR
|
||||||
# Else use the default $EDITOR
|
# Else use the default $EDITOR
|
||||||
local EDITOR=${SUDO_EDITOR:-${VISUAL:-$EDITOR}}
|
local EDITOR=${SUDO_EDITOR:-${VISUAL:-$EDITOR}}
|
||||||
|
@ -47,7 +52,9 @@ sudo-command-line() {
|
||||||
sudo\ *) __sudo-replace-buffer "sudo" "" ;;
|
sudo\ *) __sudo-replace-buffer "sudo" "" ;;
|
||||||
*) LBUFFER="sudo $LBUFFER" ;;
|
*) LBUFFER="sudo $LBUFFER" ;;
|
||||||
esac
|
esac
|
||||||
else
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
# Check if the typed command is really an alias to $EDITOR
|
# Check if the typed command is really an alias to $EDITOR
|
||||||
|
|
||||||
# Get the first part of the typed command
|
# Get the first part of the typed command
|
||||||
|
@ -72,7 +79,8 @@ sudo-command-line() {
|
||||||
if [[ "$realcmd" = (\$EDITOR|$editorcmd|${editorcmd:c}) \
|
if [[ "$realcmd" = (\$EDITOR|$editorcmd|${editorcmd:c}) \
|
||||||
|| "${realcmd:c}" = ($editorcmd|${editorcmd:c}) ]] \
|
|| "${realcmd:c}" = ($editorcmd|${editorcmd:c}) ]] \
|
||||||
|| builtin which -a "$realcmd" | command grep -Fx -q "$editorcmd"; then
|
|| builtin which -a "$realcmd" | command grep -Fx -q "$editorcmd"; then
|
||||||
editorcmd="$cmd" # replace $editorcmd with the typed command so it matches below
|
__sudo-replace-buffer "$cmd" "sudo -e"
|
||||||
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check for editor commands in the typed command and replace accordingly
|
# Check for editor commands in the typed command and replace accordingly
|
||||||
|
@ -83,13 +91,13 @@ sudo-command-line() {
|
||||||
sudo\ *) __sudo-replace-buffer "sudo" "" ;;
|
sudo\ *) __sudo-replace-buffer "sudo" "" ;;
|
||||||
*) LBUFFER="sudo $LBUFFER" ;;
|
*) LBUFFER="sudo $LBUFFER" ;;
|
||||||
esac
|
esac
|
||||||
fi
|
} always {
|
||||||
|
|
||||||
# Preserve beginning space
|
# Preserve beginning space
|
||||||
LBUFFER="${WHITESPACE}${LBUFFER}"
|
LBUFFER="${WHITESPACE}${LBUFFER}"
|
||||||
|
|
||||||
# Redisplay edit buffer (compatibility with zsh-syntax-highlighting)
|
# Redisplay edit buffer (compatibility with zsh-syntax-highlighting)
|
||||||
zle redisplay
|
zle redisplay
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
zle -N sudo-command-line
|
zle -N sudo-command-line
|
||||||
|
|
Loading…
Reference in a new issue