commit
26f30697ea
@ -17,6 +17,7 @@ This highlighter defines the following styles:
|
|||||||
|
|
||||||
* `bracket-error` - unmatched brackets
|
* `bracket-error` - unmatched brackets
|
||||||
* `bracket-level-N` - brackets with nest level N
|
* `bracket-level-N` - brackets with nest level N
|
||||||
|
* `cursor-matchingbracket` - the matching bracket, if cursor is on a bracket
|
||||||
|
|
||||||
To override one of those styles, change its entry in `ZSH_HIGHLIGHT_STYLES`, for example in `~/.zshrc`:
|
To override one of those styles, change its entry in `ZSH_HIGHLIGHT_STYLES`, for example in `~/.zshrc`:
|
||||||
|
|
||||||
|
@ -36,6 +36,7 @@
|
|||||||
: ${ZSH_HIGHLIGHT_STYLES[bracket-level-3]:=fg=magenta,bold}
|
: ${ZSH_HIGHLIGHT_STYLES[bracket-level-3]:=fg=magenta,bold}
|
||||||
: ${ZSH_HIGHLIGHT_STYLES[bracket-level-4]:=fg=yellow,bold}
|
: ${ZSH_HIGHLIGHT_STYLES[bracket-level-4]:=fg=yellow,bold}
|
||||||
: ${ZSH_HIGHLIGHT_STYLES[bracket-level-5]:=fg=cyan,bold}
|
: ${ZSH_HIGHLIGHT_STYLES[bracket-level-5]:=fg=cyan,bold}
|
||||||
|
: ${ZSH_HIGHLIGHT_STYLES[cursor-matchingbracket]:=standout}
|
||||||
|
|
||||||
# Whether the brackets highlighter should be called or not.
|
# Whether the brackets highlighter should be called or not.
|
||||||
_zsh_highlight_brackets_highlighter_predicate()
|
_zsh_highlight_brackets_highlighter_predicate()
|
||||||
@ -46,53 +47,64 @@ _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]`
|
_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]`
|
_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 [[ -n $matching[$pos] ]] && [[ $typepos[$pos] == $typepos[$matching[$pos]] ]]; then
|
||||||
region_highlight+=("$((pos - 1)) $pos "$ZSH_HIGHLIGHT_STYLES[bracket-error])
|
|
||||||
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 bracket_color_level=bracket-level-$(( (levelpos[$pos] - 1) % bracket_color_size + 1 ))
|
||||||
region_highlight+=("$((pos - 1)) $pos "$ZSH_HIGHLIGHT_STYLES[$style])
|
local style=$ZSH_HIGHLIGHT_STYLES[$bracket_color_level]
|
||||||
|
region_highlight+=("$pos $((pos + 1)) $style")
|
||||||
|
else
|
||||||
|
local style=$ZSH_HIGHLIGHT_STYLES[bracket-error]
|
||||||
|
region_highlight+=("$pos $((pos + 1)) $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")
|
local style=$ZSH_HIGHLIGHT_STYLES[cursor-matchingbracket]
|
||||||
|
region_highlight+=("$otherpos $((otherpos + 1)) $style")
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Helper function to differentiate type
|
||||||
_zsh_highlight_brackets_highlighter_brackettype()
|
_zsh_highlight_brackets_highlighter_brackettype()
|
||||||
{
|
{
|
||||||
case $1 in
|
case $1 in
|
||||||
["()"]) print round;;
|
["()"]) typepos[$pos]=round;;
|
||||||
["[]"]) print bracket;;
|
["[]"]) typepos[$pos]=bracket;;
|
||||||
["{}"]) print curly;;
|
["{}"]) typepos[$pos]=curly;;
|
||||||
*) ;;
|
*) ;;
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@
|
|||||||
# vim: ft=zsh sw=2 ts=2 et
|
# vim: ft=zsh sw=2 ts=2 et
|
||||||
# -------------------------------------------------------------------------------------------------
|
# -------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
BUFFER='echo $(echo x{(z)array})'
|
BUFFER='echo $(echo ${(z)array})'
|
||||||
|
|
||||||
expected_region_highlight=(
|
expected_region_highlight=(
|
||||||
"6 6 $ZSH_HIGHLIGHT_STYLES[bracket-level-1]" # (
|
"6 6 $ZSH_HIGHLIGHT_STYLES[bracket-level-1]" # (
|
||||||
|
Loading…
x
Reference in New Issue
Block a user