states work: Convert sudo parsing.

This commit is contained in:
Daniel Shahaf 2015-10-27 12:19:51 +02:00
parent 80d692c400
commit 10b1da64e6

View File

@ -84,7 +84,7 @@ _zsh_highlight_main_highlighter()
{ {
emulate -L zsh emulate -L zsh
setopt localoptions extendedglob bareglobqual setopt localoptions extendedglob bareglobqual
local start_pos=0 end_pos highlight_glob=true new_expression=true arg style sudo=false sudo_arg=false local start_pos=0 end_pos highlight_glob=true new_expression=true arg style
local redirection=false # true when we've seen a redirection operator before seeing the command word local redirection=false # true when we've seen a redirection operator before seeing the command word
typeset -a ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR typeset -a ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR
typeset -a ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS typeset -a ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS
@ -103,7 +103,9 @@ _zsh_highlight_main_highlighter()
$ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR $ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS $ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR $ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS
) )
local this_word=':start:' next_word
for arg in ${(z)buf}; do for arg in ${(z)buf}; do
next_word=':regular:'
# $already_added is set to 1 to disable adding an entry to region_highlight # $already_added is set to 1 to disable adding an entry to region_highlight
# for this iteration. Currently, that is done for "" and $'' strings, # for this iteration. Currently, that is done for "" and $'' strings,
# which add the entry early so escape sequences within the string override # which add the entry early so escape sequences within the string override
@ -134,20 +136,16 @@ _zsh_highlight_main_highlighter()
fi fi
# Parse the sudo command line # Parse the sudo command line
if $sudo; then if [[ $this_word == *':sudo_opt:'* ]]; then
case "$arg" in case "$arg" in
# Flag that requires an argument # Flag that requires an argument
'-'[Cgprtu]) sudo_arg=true;; '-'[Cgprtu]) next_word=':sudo_arg:';;
# This prevents misbehavior with sudo -u -otherargument # This prevents misbehavior with sudo -u -otherargument
'-'*) sudo_arg=false;; '-'*) next_word+=':sudo_opt:';;
*) if $sudo_arg; then *) this_word+=':start:'; new_expression=true;;
sudo_arg=false
else
sudo=false
new_expression=true
fi
;;
esac esac
elif [[ $this_word == *':sudo_arg:'* ]]; then
next_word+=':sudo_opt:'
fi fi
if $new_expression && ! $redirection; then # $arg is the command word if $new_expression && ! $redirection; then # $arg is the command word
new_expression=false new_expression=false
@ -155,7 +153,7 @@ _zsh_highlight_main_highlighter()
style=$ZSH_HIGHLIGHT_STYLES[precommand] style=$ZSH_HIGHLIGHT_STYLES[precommand]
elif [[ "$arg" = "sudo" ]]; then elif [[ "$arg" = "sudo" ]]; then
style=$ZSH_HIGHLIGHT_STYLES[precommand] style=$ZSH_HIGHLIGHT_STYLES[precommand]
sudo=true next_word+=':sudo_opt:'
else else
_zsh_highlight_main_highlighter_expand_path $arg _zsh_highlight_main_highlighter_expand_path $arg
local expanded_arg="$REPLY" local expanded_arg="$REPLY"
@ -253,6 +251,7 @@ _zsh_highlight_main_highlighter()
[[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS:#"$arg"} ]] && new_expression=true [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS:#"$arg"} ]] && new_expression=true
[[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]] && highlight_glob=true [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]] && highlight_glob=true
start_pos=$end_pos start_pos=$end_pos
this_word=$next_word
done done
} }