handle highlighting for precommands; handle highlighting for commands separators

This commit is contained in:
Andreas Gahr 2011-09-12 16:48:18 +02:00
parent f84f9319d8
commit 99b4c930d2
2 changed files with 25 additions and 5 deletions

View File

@ -28,6 +28,8 @@ This highlighter defines the following styles:
* `builtin` - shell builtin commands * `builtin` - shell builtin commands
* `function` - functions * `function` - functions
* `command` - commands * `command` - commands
* `precommand` - precommands (i.e. exec, builtin, ...)
* `commandseparator` - command separation tokens
* `hashed-command` - hashed commands * `hashed-command` - hashed commands
* `path` - paths * `path` - paths
* `globbing` - globbing expressions * `globbing` - globbing expressions

View File

@ -37,6 +37,8 @@
: ${ZSH_HIGHLIGHT_STYLES[builtin]:=fg=green} : ${ZSH_HIGHLIGHT_STYLES[builtin]:=fg=green}
: ${ZSH_HIGHLIGHT_STYLES[function]:=fg=green} : ${ZSH_HIGHLIGHT_STYLES[function]:=fg=green}
: ${ZSH_HIGHLIGHT_STYLES[command]:=fg=green} : ${ZSH_HIGHLIGHT_STYLES[command]:=fg=green}
: ${ZSH_HIGHLIGHT_STYLES[precommand]:=fg=green,underline}
: ${ZSH_HIGHLIGHT_STYLES[commandseparator]:=fg=green,bold}
: ${ZSH_HIGHLIGHT_STYLES[hashed-command]:=fg=green} : ${ZSH_HIGHLIGHT_STYLES[hashed-command]:=fg=green}
: ${ZSH_HIGHLIGHT_STYLES[path]:=underline} : ${ZSH_HIGHLIGHT_STYLES[path]:=underline}
: ${ZSH_HIGHLIGHT_STYLES[globbing]:=fg=blue} : ${ZSH_HIGHLIGHT_STYLES[globbing]:=fg=blue}
@ -50,11 +52,6 @@
: ${ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]:=fg=cyan} : ${ZSH_HIGHLIGHT_STYLES[back-double-quoted-argument]:=fg=cyan}
: ${ZSH_HIGHLIGHT_STYLES[assign]:=none} : ${ZSH_HIGHLIGHT_STYLES[assign]:=none}
# Tokens that are always immediately followed by a command.
ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS=(
'|' '||' ';' '&' '&&' 'noglob' 'nocorrect' 'builtin' 'exec'
)
# Whether the highlighter should be called or not. # Whether the highlighter should be called or not.
_zsh_highlight_main_highlighter_predicate() _zsh_highlight_main_highlighter_predicate()
{ {
@ -66,7 +63,22 @@ _zsh_highlight_main_highlighter()
{ {
setopt localoptions extendedglob bareglobqual setopt localoptions extendedglob bareglobqual
local start_pos=0 end_pos highlight_glob=true new_expression=true arg style local start_pos=0 end_pos highlight_glob=true new_expression=true arg style
typeset -a ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR
typeset -a ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS
typeset -a ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS
region_highlight=() region_highlight=()
ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR=(
'|' '||' ';' '&' '&&'
)
ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS=(
'builtin' 'command' 'exec' 'nocorrect' 'noglob'
)
# Tokens that are always immediately followed by a command.
ZSH_HIGHLIGHT_TOKENS_FOLLOWED_BY_COMMANDS=(
$ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR $ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS
)
for arg in ${(z)BUFFER}; do for arg in ${(z)BUFFER}; do
local substr_color=0 local substr_color=0
[[ $start_pos -eq 0 && $arg = 'noglob' ]] && highlight_glob=false [[ $start_pos -eq 0 && $arg = 'noglob' ]] && highlight_glob=false
@ -74,6 +86,9 @@ _zsh_highlight_main_highlighter()
((end_pos=$start_pos+${#arg})) ((end_pos=$start_pos+${#arg}))
if $new_expression; then if $new_expression; then
new_expression=false new_expression=false
if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]]; then
style=$ZSH_HIGHLIGHT_STYLES[precommand]
else
res=$(LC_ALL=C builtin type -w $arg 2>/dev/null) res=$(LC_ALL=C builtin type -w $arg 2>/dev/null)
case $res in case $res in
*': reserved') style=$ZSH_HIGHLIGHT_STYLES[reserved-word];; *': reserved') style=$ZSH_HIGHLIGHT_STYLES[reserved-word];;
@ -97,6 +112,7 @@ _zsh_highlight_main_highlighter()
fi fi
;; ;;
esac esac
fi
else else
case $arg in case $arg in
'--'*) style=$ZSH_HIGHLIGHT_STYLES[double-hyphen-option];; '--'*) style=$ZSH_HIGHLIGHT_STYLES[double-hyphen-option];;
@ -113,6 +129,8 @@ _zsh_highlight_main_highlighter()
style=$ZSH_HIGHLIGHT_STYLES[path] style=$ZSH_HIGHLIGHT_STYLES[path]
elif [[ $arg[0,1] = $histchars[0,1] ]]; then elif [[ $arg[0,1] = $histchars[0,1] ]]; then
style=$ZSH_HIGHLIGHT_STYLES[history-expansion] style=$ZSH_HIGHLIGHT_STYLES[history-expansion]
elif [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR:#"$arg"} ]]; then
style=$ZSH_HIGHLIGHT_STYLES[commandseparator]
else else
style=$ZSH_HIGHLIGHT_STYLES[default] style=$ZSH_HIGHLIGHT_STYLES[default]
fi fi