Highlighting bracket pair, when one is under the cursor (Issue 24)
This commit is contained in:
parent
f78e444451
commit
701cc303fe
@ -101,8 +101,9 @@ _zsh_highlight-string() {
|
|||||||
# Recolorize the current ZLE buffer.
|
# Recolorize the current ZLE buffer.
|
||||||
_zsh_highlight-zle-buffer() {
|
_zsh_highlight-zle-buffer() {
|
||||||
# Avoid doing the same work over and over
|
# Avoid doing the same work over and over
|
||||||
[[ ${ZSH_PRIOR_HIGHLIGHTED_BUFFER:-} == $BUFFER ]] && [[ ${#region_highlight} -gt 0 ]] && return
|
[[ ${ZSH_PRIOR_HIGHLIGHTED_BUFFER:-} == $BUFFER ]] && [[ ${#region_highlight} -gt 0 ]] && (( ZSH_PRIOR_CURSOR == CURSOR )) && return
|
||||||
ZSH_PRIOR_HIGHLIGHTED_BUFFER=$BUFFER
|
ZSH_PRIOR_HIGHLIGHTED_BUFFER=$BUFFER
|
||||||
|
ZSH_PRIOR_CURSOR=$CURSOR
|
||||||
|
|
||||||
setopt localoptions extendedglob bareglobqual
|
setopt localoptions extendedglob bareglobqual
|
||||||
local new_expression=true
|
local new_expression=true
|
||||||
@ -168,23 +169,39 @@ _zsh_highlight-zle-buffer() {
|
|||||||
# Bracket matching
|
# Bracket matching
|
||||||
bracket_color_size=${#ZSH_MATCHING_BRACKETS}
|
bracket_color_size=${#ZSH_MATCHING_BRACKETS}
|
||||||
if ((bracket_color_size > 0)); then
|
if ((bracket_color_size > 0)); then
|
||||||
|
typeset -A levelpos
|
||||||
|
typeset -A lastoflevel
|
||||||
|
typeset -A matching
|
||||||
|
typeset -A revmatching
|
||||||
((level = 0))
|
((level = 0))
|
||||||
for pos in {1..${#BUFFER}}; do
|
for pos in {1..${#BUFFER}}; do
|
||||||
case $BUFFER[pos] in
|
case $BUFFER[pos] in
|
||||||
"("|"["|"{")
|
"("|"["|"{")
|
||||||
((level++))
|
levelpos[$pos]=$((++level))
|
||||||
region_highlight+=("$((pos - 1)) $pos "$ZSH_MATCHING_BRACKETS[(( (level - 1) % bracket_color_size + 1 ))])
|
lastoflevel[$level]=$pos
|
||||||
;;
|
;;
|
||||||
")"|"]"|"}")
|
")"|"]"|"}")
|
||||||
if ((level < 1)); then
|
matching[$lastoflevel[$level]]=$pos
|
||||||
region_highlight+=("$((pos - 1)) $pos "$ZSH_HIGHLIGHT_STYLES[bracket-error])
|
revmatching[$pos]=$lastoflevel[$level]
|
||||||
else
|
levelpos[$pos]=$((level--))
|
||||||
region_highlight+=("$((pos - 1)) $pos "$ZSH_MATCHING_BRACKETS[(( (level - 1) % bracket_color_size + 1 ))])
|
|
||||||
fi
|
|
||||||
((level--))
|
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
for pos in ${(k)levelpos}; do
|
||||||
|
level=$levelpos[$pos]
|
||||||
|
if ((level < 1)); then
|
||||||
|
region_highlight+=("$((pos - 1)) $pos "$ZSH_HIGHLIGHT_STYLES[bracket-error])
|
||||||
|
else
|
||||||
|
region_highlight+=("$((pos - 1)) $pos "$ZSH_MATCHING_BRACKETS[(( (level - 1) % bracket_color_size + 1 ))])
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
((c = CURSOR + 1))
|
||||||
|
if [[ -n $levelpos[$c] ]]; then
|
||||||
|
((otherpos = -1))
|
||||||
|
[[ -n $matching[$c] ]] && otherpos=$matching[$c]
|
||||||
|
[[ -n $revmatching[$c] ]] && otherpos=$revmatching[$c]
|
||||||
|
region_highlight+=("$((otherpos - 1)) $otherpos standout")
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user