957dca698cd0a0cafc6d2551eeff19fe223f41bd

Author
Marc Cornellà <hello@mcornella.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

style(sudo): clean code style and reorganise logic

Diff

 1diff --git a/plugins/sudo/sudo.plugin.zsh b/plugins/sudo/sudo.plugin.zsh
 2index e8d183414d14e6aa47b7f1833b5149153f8a8809..2a0b3bfc47c65a236c3f6dfbc4d3e38392462d8f 100644
 3--- a/plugins/sudo/sudo.plugin.zsh
 4+++ b/plugins/sudo/sudo.plugin.zsh
 5@@ -17,9 +17,13 @@
 6 
 7 __sudo-replace-buffer() {
 8   local old=$1 new=$2 space=${2:+ }
 9-  if [[ ${#LBUFFER} -le ${#old} ]]; then
10-    RBUFFER="${space}${BUFFER#$old }"
11-    LBUFFER="${new}"
12+
13+  # if the cursor is positioned in the $old part of the text, make
14+  # the substitution and leave the cursor after the $new text
15+  if [[ $CURSOR -le ${#old} ]]; then
16+    BUFFER="${new}${space}${BUFFER#$old }"
17+    CURSOR=${#new}
18+  # otherwise just replace $old with $new in the text before the cursor
19   else
20     LBUFFER="${new}${space}${LBUFFER#$old }"
21   fi
22@@ -36,18 +40,21 @@ sudo-command-line() {
23     LBUFFER="${LBUFFER:1}"
24   fi
25 
26-  # If $SUDO_EDITOR or $VISUAL are defined, then use that as $EDITOR
27-  # Else use the default $EDITOR
28-  local EDITOR=${SUDO_EDITOR:-${VISUAL:-$EDITOR}}
29+  {
30+    # If $SUDO_EDITOR or $VISUAL are defined, then use that as $EDITOR
31+    # Else use the default $EDITOR
32+    local EDITOR=${SUDO_EDITOR:-${VISUAL:-$EDITOR}}
33+
34+    # If $EDITOR is not set, just toggle the sudo prefix on and off
35+    if [[ -z "$EDITOR" ]]; then
36+      case "$BUFFER" in
37+        sudo\ -e\ *) __sudo-replace-buffer "sudo -e" "" ;;
38+        sudo\ *) __sudo-replace-buffer "sudo" "" ;;
39+        *) LBUFFER="sudo $LBUFFER" ;;
40+      esac
41+      return
42+    fi
43 
44-  # If $EDITOR is not set, just toggle the sudo prefix on and off
45-  if [[ -z "$EDITOR" ]]; then
46-    case "$BUFFER" in
47-      sudo\ -e\ *) __sudo-replace-buffer "sudo -e" "" ;;
48-      sudo\ *) __sudo-replace-buffer "sudo" "" ;;
49-      *) LBUFFER="sudo $LBUFFER" ;;
50-    esac
51-  else
52     # Check if the typed command is really an alias to $EDITOR
53 
54     # Get the first part of the typed command
55@@ -72,7 +79,8 @@ sudo-command-line() {
56     if [[ "$realcmd" = (\$EDITOR|$editorcmd|${editorcmd:c}) \
57       || "${realcmd:c}" = ($editorcmd|${editorcmd:c}) ]] \
58       || builtin which -a "$realcmd" | command grep -Fx -q "$editorcmd"; then
59-      editorcmd="$cmd" # replace $editorcmd with the typed command so it matches below
60+      __sudo-replace-buffer "$cmd" "sudo -e"
61+      return
62     fi
63 
64     # Check for editor commands in the typed command and replace accordingly
65@@ -83,13 +91,13 @@ sudo-command-line() {
66       sudo\ *) __sudo-replace-buffer "sudo" "" ;;
67       *) LBUFFER="sudo $LBUFFER" ;;
68     esac
69-  fi
70-
71-  # Preserve beginning space
72-  LBUFFER="${WHITESPACE}${LBUFFER}"
73+  } always {
74+    # Preserve beginning space
75+    LBUFFER="${WHITESPACE}${LBUFFER}"
76 
77-  # Redisplay edit buffer (compatibility with zsh-syntax-highlighting)
78-  zle redisplay
79+    # Redisplay edit buffer (compatibility with zsh-syntax-highlighting)
80+    zle redisplay
81+  }
82 }
83 
84 zle -N sudo-command-line