Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
oh-my-zsh
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
github
oh-my-zsh
Commits
957dca69
Unverified
Commit
957dca69
authored
Jan 18, 2022
by
Marc Cornellà
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
style(sudo): clean code style and reorganise logic
parent
540b2200
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
21 deletions
+29
-21
sudo.plugin.zsh
plugins/sudo/sudo.plugin.zsh
+29
-21
No files found.
plugins/sudo/sudo.plugin.zsh
View file @
957dca69
...
@@ -17,9 +17,13 @@
...
@@ -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,18 +40,21 @@ sudo-command-line() {
...
@@ -36,18 +40,21 @@ sudo-command-line() {
LBUFFER
=
"
${
LBUFFER
:1
}
"
LBUFFER
=
"
${
LBUFFER
:1
}
"
fi
fi
# If $SUDO_EDITOR or $VISUAL are defined, then use that as $EDITOR
{
# Else use the default $EDITOR
# If $SUDO_EDITOR or $VISUAL are defined, then use that as $EDITOR
local
EDITOR
=
${
SUDO_EDITOR
:-${
VISUAL
:-
$EDITOR
}}
# Else use the default $EDITOR
local
EDITOR
=
${
SUDO_EDITOR
:-${
VISUAL
:-
$EDITOR
}}
# If $EDITOR is not set, just toggle the sudo prefix on and off
if
[[
-z
"
$EDITOR
"
]]
;
then
case
"
$BUFFER
"
in
sudo
\
-e
\
*
)
__sudo-replace-buffer
"sudo -e"
""
;;
sudo
\
*
)
__sudo-replace-buffer
"sudo"
""
;;
*
)
LBUFFER
=
"sudo
$LBUFFER
"
;;
esac
return
fi
# If $EDITOR is not set, just toggle the sudo prefix on and off
if
[[
-z
"
$EDITOR
"
]]
;
then
case
"
$BUFFER
"
in
sudo
\
-e
\
*
)
__sudo-replace-buffer
"sudo -e"
""
;;
sudo
\
*
)
__sudo-replace-buffer
"sudo"
""
;;
*
)
LBUFFER
=
"sudo
$LBUFFER
"
;;
esac
else
# 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() {
...
@@ -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() {
...
@@ -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
...
...
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