Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
Ohmyzsh
Commits
19032504
Unverified
Commit
19032504
authored
Aug 19, 2021
by
Marc Cornellà
Browse files
fix(sudo): allow different $EDITOR settings and fix zsh-syntax-highlighting redraw
parent
a1847dc8
Changes
1
Show whitespace changes
Inline
Side-by-side
plugins/sudo/sudo.plugin.zsh
View file @
19032504
...
@@ -25,6 +25,7 @@ __sudo-replace-buffer() {
...
@@ -25,6 +25,7 @@ __sudo-replace-buffer() {
}
}
sudo-command-line
()
{
sudo-command-line
()
{
# If line is empty, get the last run command from history
[[
-z
$BUFFER
]]
&&
LBUFFER
=
"
$(
fc
-ln
-1
)
"
[[
-z
$BUFFER
]]
&&
LBUFFER
=
"
$(
fc
-ln
-1
)
"
# Save beginning space
# Save beginning space
...
@@ -34,29 +35,56 @@ sudo-command-line() {
...
@@ -34,29 +35,56 @@ sudo-command-line() {
LBUFFER
=
"
${
LBUFFER
:1
}
"
LBUFFER
=
"
${
LBUFFER
:1
}
"
fi
fi
# Get the first part of the typed command and check if it's an alias to $EDITOR
# If $EDITOR is not set, just toggle the sudo prefix on and off
# If so, locally change $EDITOR to the alias so that it matches below
if
[[
-z
"
$EDITOR
"
]]
;
then
if
[[
-n
"
$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]
}
"
local
cmd
=
"
${${
(Az)BUFFER
}
[1]
}
"
if
[[
"
${
aliases
[
$cmd
]
}
"
=
(
\$
EDITOR|
$EDITOR
)
\
*
]]
;
then
# Get the first part of the alias of the same name as $cmd, or $cmd if no alias matches
local
EDITOR
=
"
$cmd
"
local
realcmd
=
"
${${
(Az)aliases[
$cmd
]
}
[1]
:-
$cmd
}
"
fi
# 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
# Check for editor commands in the typed command and replace accordingly
__sudo-replace-buffer
"
$EDITOR
"
"sudoedit"
case
"
$BUFFER
"
in
elif
[[
-n
$EDITOR
&&
$BUFFER
=
\$
EDITOR
\
*
]]
;
then
$editorcmd
\
*
)
__sudo-replace-buffer
"
$editorcmd
"
"sudoedit"
;;
__sudo-replace-buffer
"
\$
EDITOR"
"sudoedit"
\$
EDITOR
\
*
)
__sudo-replace-buffer
'$EDITOR'
"sudoedit"
;;
elif
[[
$BUFFER
=
sudoedit
\
*
]]
;
then
sudoedit
\
*
)
__sudo-replace-buffer
"sudoedit"
"
$EDITOR
"
;;
__sudo-replace-buffer
"sudoedit"
"
$EDITOR
"
sudo
\
*
)
__sudo-replace-buffer
"sudo"
""
;;
elif
[[
$BUFFER
=
sudo
\
*
]]
;
then
*
)
LBUFFER
=
"sudo
$LBUFFER
"
;;
__sudo-replace-buffer
"sudo"
""
esac
else
LBUFFER
=
"sudo
$LBUFFER
"
fi
fi
# Preserve beginning space
# Preserve beginning space
LBUFFER
=
"
${
WHITESPACE
}${
LBUFFER
}
"
LBUFFER
=
"
${
WHITESPACE
}${
LBUFFER
}
"
# Redisplay edit buffer (compatibility with zsh-syntax-highlighting)
zle redisplay
}
}
zle
-N
sudo-command-line
zle
-N
sudo-command-line
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment