bracket highlighter: start loops with 0 which is more natural and also more in line whith the other highlighters

This commit is contained in:
Andreas Gahr 2011-08-08 14:48:16 +02:00
parent 0d55207c78
commit e1ed255c0a

View File

@ -46,47 +46,55 @@ _zsh_highlight_brackets_highlighter_predicate()
# Brackets highlighting function. # Brackets highlighting function.
_zsh_highlight_brackets_highlighter() _zsh_highlight_brackets_highlighter()
{ {
local level pos local level=0 pos
local -A levelpos lastoflevel matching typepos local -A levelpos lastoflevel matching typepos
((level = 0))
for ((pos = 1; $pos <= ${#BUFFER}; pos++ )) ; do # Find all brackets and remember which one is matching
case $BUFFER[pos] in for (( pos = 0; $pos < ${#BUFFER}; pos++ )) ; do
local char=$BUFFER[pos+1]
case $char in
["([{"]) ["([{"])
levelpos[$pos]=$((++level)) levelpos[$pos]=$((++level))
lastoflevel[$level]=$pos lastoflevel[$level]=$pos
typepos[$pos]=`_zsh_highlight_brackets_highlighter_brackettype $BUFFER[$pos]` typepos[$pos]=`_zsh_highlight_brackets_highlighter_brackettype $char`
;; ;;
[")]}"]) [")]}"])
matching[$lastoflevel[$level]]=$pos matching[$lastoflevel[$level]]=$pos
matching[$pos]=$lastoflevel[$level] matching[$pos]=$lastoflevel[$level]
levelpos[$pos]=$((level--)) levelpos[$pos]=$((level--))
typepos[$pos]=`_zsh_highlight_brackets_highlighter_brackettype $BUFFER[$pos]` typepos[$pos]=`_zsh_highlight_brackets_highlighter_brackettype $char`
;; ;;
['"'\']) ['"'\'])
local quotetype=$BUFFER[$pos] # Skip everything inside quotes
local quotetype=$char
while (( $pos < ${#BUFFER} )) ; do while (( $pos < ${#BUFFER} )) ; do
((++pos)) (( pos++ ))
[[ $BUFFER[$pos] == $quotetype ]] && break [[ $BUFFER[$pos+1] == $quotetype ]] && break
done done
;; ;;
esac esac
done done
# Now highlight all found brackets
for pos in ${(k)levelpos}; do for pos in ${(k)levelpos}; do
if [[ -z $matching[$pos] ]] || [[ $typepos[$pos] != $typepos[$matching[$pos]] ]]; then if [[ -z $matching[$pos] ]] || [[ $typepos[$pos] != $typepos[$matching[$pos]] ]]; then
region_highlight+=("$((pos - 1)) $pos "$ZSH_HIGHLIGHT_STYLES[bracket-error]) region_highlight+=("$pos $((pos + 1)) "$ZSH_HIGHLIGHT_STYLES[bracket-error])
else else
local bracket_color_size=${#ZSH_HIGHLIGHT_STYLES[(I)bracket-level-*]} local bracket_color_size=${#ZSH_HIGHLIGHT_STYLES[(I)bracket-level-*]}
local style=bracket-level-$(( (levelpos[$pos] - 1) % bracket_color_size + 1 )) local style=bracket-level-$(( (levelpos[$pos] - 1) % bracket_color_size + 1 ))
region_highlight+=("$((pos - 1)) $pos "$ZSH_HIGHLIGHT_STYLES[$style]) region_highlight+=("$pos $((pos + 1)) "$ZSH_HIGHLIGHT_STYLES[$style])
fi fi
done done
((pos = CURSOR + 1))
# If cursor is on a bracket, then highlight corresponding bracket, if any
pos=CURSOR
if [[ -n $levelpos[$pos] ]] && [[ -n $matching[$pos] ]]; then if [[ -n $levelpos[$pos] ]] && [[ -n $matching[$pos] ]]; then
local otherpos=$matching[$pos] local otherpos=$matching[$pos]
region_highlight+=("$((otherpos - 1)) $otherpos standout") region_highlight+=("$otherpos $((otherpos + 1)) standout")
fi fi
} }
# Helper function to differentiate type
_zsh_highlight_brackets_highlighter_brackettype() _zsh_highlight_brackets_highlighter_brackettype()
{ {
case $1 in case $1 in