190325049ef93731ab28295dbedf36d44ab33d7a

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

Message

fix(sudo): allow different $EDITOR settings and fix zsh-syntax-highlighting redraw

Diff

 1diff --git a/plugins/sudo/sudo.plugin.zsh b/plugins/sudo/sudo.plugin.zsh
 2index f0a01bc1f37e1ca132b923a21affcf0e48e69bee..e02f88a872f2d337495627c3f3af53da22dc4b31 100644
 3--- a/plugins/sudo/sudo.plugin.zsh
 4+++ b/plugins/sudo/sudo.plugin.zsh
 5@@ -25,6 +25,7 @@ __sudo-replace-buffer() {
 6 }
 7 
 8 sudo-command-line() {
 9+  # If line is empty, get the last run command from history
10   [[ -z $BUFFER ]] && LBUFFER="$(fc -ln -1)"
11 
12   # Save beginning space
13@@ -34,29 +35,56 @@ sudo-command-line() {
14     LBUFFER="${LBUFFER:1}"
15   fi
16 
17-  # Get the first part of the typed command and check if it's an alias to $EDITOR
18-  # If so, locally change $EDITOR to the alias so that it matches below
19-  if [[ -n "$EDITOR" ]]; then
20+  # If $EDITOR is not set, just toggle the sudo prefix on and off
21+  if [[ -z "$EDITOR" ]]; then
22+    case "$BUFFER" in
23+      sudoedit\ *) __sudo-replace-buffer "sudoedit" "" ;;
24+      sudo\ *) __sudo-replace-buffer "sudo" "" ;;
25+      *) LBUFFER="sudo $LBUFFER" ;;
26+    esac
27+  else
28+    # Check if the typed command is really an alias to $EDITOR
29+
30+    # Get the first part of the typed command
31     local cmd="${${(Az)BUFFER}[1]}"
32-    if [[ "${aliases[$cmd]} " = (\$EDITOR|$EDITOR)\ * ]]; then
33-      local EDITOR="$cmd"
34+    # Get the first part of the alias of the same name as $cmd, or $cmd if no alias matches
35+    local realcmd="${${(Az)aliases[$cmd]}[1]:-$cmd}"
36+    # Get the first part of the $EDITOR command ($EDITOR may have arguments after it)
37+    local editorcmd="${${(Az)EDITOR}[1]}"
38+
39+    # Note: ${var:c} makes a $PATH search and expands $var to the full path
40+    # The if condition is met when:
41+    # - $realcmd is '$EDITOR'
42+    # - $realcmd is "cmd" and $EDITOR is "cmd"
43+    # - $realcmd is "cmd" and $EDITOR is "cmd --with --arguments"
44+    # - $realcmd is "/path/to/cmd" and $EDITOR is "cmd"
45+    # - $realcmd is "/path/to/cmd" and $EDITOR is "/path/to/cmd"
46+    # or
47+    # - $realcmd is "cmd" and $EDITOR is "cmd"
48+    # - $realcmd is "cmd" and $EDITOR is "/path/to/cmd"
49+    # or
50+    # - $realcmd is "cmd" and $EDITOR is /alternative/path/to/cmd that appears in $PATH
51+    if [[ "$realcmd" = (\$EDITOR|$editorcmd|${editorcmd:c}) \
52+      || "${realcmd:c}" = ($editorcmd|${editorcmd:c}) ]] \
53+      || builtin which -a "$realcmd" | command grep -Fx -q "$editorcmd"; then
54+      editorcmd="$cmd" # replace $editorcmd with the typed command so it matches below
55     fi
56-  fi
57 
58-  if [[ -n $EDITOR && $BUFFER = $EDITOR\ * ]]; then
59-    __sudo-replace-buffer "$EDITOR" "sudoedit"
60-  elif [[ -n $EDITOR && $BUFFER = \$EDITOR\ * ]]; then
61-    __sudo-replace-buffer "\$EDITOR" "sudoedit"
62-  elif [[ $BUFFER = sudoedit\ * ]]; then
63-    __sudo-replace-buffer "sudoedit" "$EDITOR"
64-  elif [[ $BUFFER = sudo\ * ]]; then
65-    __sudo-replace-buffer "sudo" ""
66-  else
67-    LBUFFER="sudo $LBUFFER"
68+    # Check for editor commands in the typed command and replace accordingly
69+    case "$BUFFER" in
70+      $editorcmd\ *) __sudo-replace-buffer "$editorcmd" "sudoedit" ;;
71+      \$EDITOR\ *) __sudo-replace-buffer '$EDITOR' "sudoedit" ;;
72+      sudoedit\ *) __sudo-replace-buffer "sudoedit" "$EDITOR" ;;
73+      sudo\ *) __sudo-replace-buffer "sudo" "" ;;
74+      *) LBUFFER="sudo $LBUFFER" ;;
75+    esac
76   fi
77 
78   # Preserve beginning space
79   LBUFFER="${WHITESPACE}${LBUFFER}"
80+
81+  # Redisplay edit buffer (compatibility with zsh-syntax-highlighting)
82+  zle redisplay
83 }
84 
85 zle -N sudo-command-line