From ef74d2b611a743683e8b1fb7d01b3838d7a330dd Mon Sep 17 00:00:00 2001 From: garinger Date: Fri, 8 Jul 2011 12:25:09 -0700 Subject: [PATCH 1/6] Only match brackets of the same type --- highlighters/brackets/brackets-highlighter.zsh | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/highlighters/brackets/brackets-highlighter.zsh b/highlighters/brackets/brackets-highlighter.zsh index 2a2130e..4454442 100644 --- a/highlighters/brackets/brackets-highlighter.zsh +++ b/highlighters/brackets/brackets-highlighter.zsh @@ -49,24 +49,33 @@ _zsh_highlight_brackets_highlighter() local bracket_color_size=${#ZSH_HIGHLIGHT_STYLES[(I)bracket-level-*]} if ((bracket_color_size > 0)); then local c level pos - typeset -A levelpos lastoflevel matching revmatching + typeset -A levelpos lastoflevel matching typepos revmatching ((level = 0)) for pos in {1..${#BUFFER}}; do case $BUFFER[pos] in "("|"["|"{") levelpos[$pos]=$((++level)) lastoflevel[$level]=$pos + typepos[$pos]=$BUFFER[$pos] ;; ")"|"]"|"}") matching[$lastoflevel[$level]]=$pos revmatching[$pos]=$lastoflevel[$level] levelpos[$pos]=$((level--)) + typepos[$pos]=$BUFFER[$pos] ;; esac done + for pos in ${(k)typepos}; do + typepos[$pos]=${typepos[${pos}]/["()"]/round} + typepos[$pos]=${typepos[${pos}]/["[]"]/square} + typepos[$pos]=${typepos[${pos}]/["{}"]/curly} + done for pos in ${(k)levelpos}; do if [[ -z $matching[$pos] ]] && [[ -z $revmatching[$pos] ]]; then region_highlight+=("$((pos - 1)) $pos "$ZSH_HIGHLIGHT_STYLES[bracket-error]) + elif [[ $typepos[$pos] != $typepos[$matching[$pos]] ]] && [[ $typepos[$pos] != $typepos[$revmatching[$pos]] ]]; then + region_highlight+=("$((pos - 1)) $pos "$ZSH_HIGHLIGHT_STYLES[bracket-error]) else local style=bracket-level-$(( (levelpos[$pos] - 1) % bracket_color_size + 1 )) region_highlight+=("$((pos - 1)) $pos "$ZSH_HIGHLIGHT_STYLES[$style]) From d4acac98372eefb2c96a09fd5ff803afdddf5a87 Mon Sep 17 00:00:00 2001 From: garinger Date: Fri, 8 Jul 2011 12:43:59 -0700 Subject: [PATCH 2/6] eliminate a variable and simplify --- highlighters/brackets/brackets-highlighter.zsh | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/highlighters/brackets/brackets-highlighter.zsh b/highlighters/brackets/brackets-highlighter.zsh index 4454442..a1e59cd 100644 --- a/highlighters/brackets/brackets-highlighter.zsh +++ b/highlighters/brackets/brackets-highlighter.zsh @@ -60,7 +60,7 @@ _zsh_highlight_brackets_highlighter() ;; ")"|"]"|"}") matching[$lastoflevel[$level]]=$pos - revmatching[$pos]=$lastoflevel[$level] + matching[$pos]=$lastoflevel[$level] levelpos[$pos]=$((level--)) typepos[$pos]=$BUFFER[$pos] ;; @@ -72,9 +72,7 @@ _zsh_highlight_brackets_highlighter() typepos[$pos]=${typepos[${pos}]/["{}"]/curly} done for pos in ${(k)levelpos}; do - if [[ -z $matching[$pos] ]] && [[ -z $revmatching[$pos] ]]; then - region_highlight+=("$((pos - 1)) $pos "$ZSH_HIGHLIGHT_STYLES[bracket-error]) - elif [[ $typepos[$pos] != $typepos[$matching[$pos]] ]] && [[ $typepos[$pos] != $typepos[$revmatching[$pos]] ]]; then + if [[ -z $matching[$pos] ]] || [[ $typepos[$pos] != $typepos[$matching[$pos]] ]]; then region_highlight+=("$((pos - 1)) $pos "$ZSH_HIGHLIGHT_STYLES[bracket-error]) else local style=bracket-level-$(( (levelpos[$pos] - 1) % bracket_color_size + 1 )) @@ -86,7 +84,6 @@ _zsh_highlight_brackets_highlighter() local otherpos ((otherpos = -1)) [[ -n $matching[$c] ]] && otherpos=$matching[$c] - [[ -n $revmatching[$c] ]] && otherpos=$revmatching[$c] region_highlight+=("$((otherpos - 1)) $otherpos standout") fi fi From 05e4a031c7e7fdeed7975f6904dea86bdfda97db Mon Sep 17 00:00:00 2001 From: garinger Date: Tue, 12 Jul 2011 07:56:12 -0700 Subject: [PATCH 3/6] * remove a safety belt - no need to check if array has elements, as defaults are provided * reuse $pos instead of using another variable * remove declaration forgotten in last commit --- .../brackets/brackets-highlighter.zsh | 76 +++++++++---------- 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/highlighters/brackets/brackets-highlighter.zsh b/highlighters/brackets/brackets-highlighter.zsh index a1e59cd..46ab7bd 100644 --- a/highlighters/brackets/brackets-highlighter.zsh +++ b/highlighters/brackets/brackets-highlighter.zsh @@ -46,45 +46,43 @@ _zsh_highlight_brackets_highlighter_predicate() # Brackets highlighting function. _zsh_highlight_brackets_highlighter() { - local bracket_color_size=${#ZSH_HIGHLIGHT_STYLES[(I)bracket-level-*]} - if ((bracket_color_size > 0)); then - local c level pos - typeset -A levelpos lastoflevel matching typepos revmatching - ((level = 0)) - for pos in {1..${#BUFFER}}; do - case $BUFFER[pos] in - "("|"["|"{") - levelpos[$pos]=$((++level)) - lastoflevel[$level]=$pos - typepos[$pos]=$BUFFER[$pos] - ;; - ")"|"]"|"}") - matching[$lastoflevel[$level]]=$pos - matching[$pos]=$lastoflevel[$level] - levelpos[$pos]=$((level--)) - typepos[$pos]=$BUFFER[$pos] - ;; - esac - done - for pos in ${(k)typepos}; do - typepos[$pos]=${typepos[${pos}]/["()"]/round} - typepos[$pos]=${typepos[${pos}]/["[]"]/square} - typepos[$pos]=${typepos[${pos}]/["{}"]/curly} - done - for pos in ${(k)levelpos}; do - if [[ -z $matching[$pos] ]] || [[ $typepos[$pos] != $typepos[$matching[$pos]] ]]; then - region_highlight+=("$((pos - 1)) $pos "$ZSH_HIGHLIGHT_STYLES[bracket-error]) - else - local style=bracket-level-$(( (levelpos[$pos] - 1) % bracket_color_size + 1 )) - region_highlight+=("$((pos - 1)) $pos "$ZSH_HIGHLIGHT_STYLES[$style]) - fi - done - ((c = CURSOR + 1)) - if [[ -n $levelpos[$c] ]]; then - local otherpos - ((otherpos = -1)) - [[ -n $matching[$c] ]] && otherpos=$matching[$c] - region_highlight+=("$((otherpos - 1)) $otherpos standout") + local level pos + local -A levelpos lastoflevel matching typepos + ((level = 0)) + for pos in {1..${#BUFFER}}; do + case $BUFFER[pos] in + "("|"["|"{") + levelpos[$pos]=$((++level)) + lastoflevel[$level]=$pos + typepos[$pos]=$BUFFER[$pos] + ;; + ")"|"]"|"}") + matching[$lastoflevel[$level]]=$pos + matching[$pos]=$lastoflevel[$level] + levelpos[$pos]=$((level--)) + typepos[$pos]=$BUFFER[$pos] + ;; + esac + done + for pos in ${(k)typepos}; do + typepos[$pos]=${typepos[${pos}]/["()"]/round} + typepos[$pos]=${typepos[${pos}]/["[]"]/square} + typepos[$pos]=${typepos[${pos}]/["{}"]/curly} + done + for pos in ${(k)levelpos}; do + if [[ -z $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 style=bracket-level-$(( (levelpos[$pos] - 1) % bracket_color_size + 1 )) + region_highlight+=("$((pos - 1)) $pos "$ZSH_HIGHLIGHT_STYLES[$style]) fi + done + ((pos = CURSOR + 1)) + if [[ -n $levelpos[$pos] ]]; then + local otherpos + ((otherpos = -1)) + [[ -n $matching[$pos] ]] && otherpos=$matching[$pos] + region_highlight+=("$((otherpos - 1)) $otherpos standout") fi } From 2fe95056471071ff716e2efebfc0701ff310dcfc Mon Sep 17 00:00:00 2001 From: garinger Date: Tue, 12 Jul 2011 08:07:08 -0700 Subject: [PATCH 4/6] simplify check for bracket under cursor --- highlighters/brackets/brackets-highlighter.zsh | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/highlighters/brackets/brackets-highlighter.zsh b/highlighters/brackets/brackets-highlighter.zsh index 46ab7bd..675b4cd 100644 --- a/highlighters/brackets/brackets-highlighter.zsh +++ b/highlighters/brackets/brackets-highlighter.zsh @@ -79,10 +79,8 @@ _zsh_highlight_brackets_highlighter() fi done ((pos = CURSOR + 1)) - if [[ -n $levelpos[$pos] ]]; then - local otherpos - ((otherpos = -1)) - [[ -n $matching[$pos] ]] && otherpos=$matching[$pos] + if [[ -n $levelpos[$pos] ]] && [[ -n $matching[$pos] ]]; then + local otherpos=$matching[$pos] region_highlight+=("$((otherpos - 1)) $otherpos standout") fi } From f1eb5aeef7cf86d69e31deeb915522fef55bfb51 Mon Sep 17 00:00:00 2001 From: garinger Date: Tue, 12 Jul 2011 08:11:21 -0700 Subject: [PATCH 5/6] skip all brackets inside quotes (" and ') --- highlighters/brackets/brackets-highlighter.zsh | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/highlighters/brackets/brackets-highlighter.zsh b/highlighters/brackets/brackets-highlighter.zsh index 675b4cd..91d114c 100644 --- a/highlighters/brackets/brackets-highlighter.zsh +++ b/highlighters/brackets/brackets-highlighter.zsh @@ -49,19 +49,26 @@ _zsh_highlight_brackets_highlighter() local level pos local -A levelpos lastoflevel matching typepos ((level = 0)) - for pos in {1..${#BUFFER}}; do + for ((pos = 1; $pos <= ${#BUFFER}; pos++ )) ; do case $BUFFER[pos] in - "("|"["|"{") + ["([{"]) levelpos[$pos]=$((++level)) lastoflevel[$level]=$pos typepos[$pos]=$BUFFER[$pos] ;; - ")"|"]"|"}") + [")]}"]) matching[$lastoflevel[$level]]=$pos matching[$pos]=$lastoflevel[$level] levelpos[$pos]=$((level--)) typepos[$pos]=$BUFFER[$pos] ;; + ['"'\']) + local temp=$BUFFER[$pos] + while (( $pos < ${#BUFFER} )) ; do + ((++pos)) + [[ $BUFFER[$pos] == $temp ]] && break + done + ;; esac done for pos in ${(k)typepos}; do From e7ab165310033beb36c78d237b8af698484a2895 Mon Sep 17 00:00:00 2001 From: garinger Date: Wed, 13 Jul 2011 12:47:27 -0700 Subject: [PATCH 6/6] * optimize discrimination of bracket-type * use a nicer variable name --- .../brackets/brackets-highlighter.zsh | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/highlighters/brackets/brackets-highlighter.zsh b/highlighters/brackets/brackets-highlighter.zsh index 91d114c..e1ffdae 100644 --- a/highlighters/brackets/brackets-highlighter.zsh +++ b/highlighters/brackets/brackets-highlighter.zsh @@ -54,28 +54,23 @@ _zsh_highlight_brackets_highlighter() ["([{"]) levelpos[$pos]=$((++level)) lastoflevel[$level]=$pos - typepos[$pos]=$BUFFER[$pos] + typepos[$pos]=`_zsh_highlight_brackets_highlighter_brackettype $BUFFER[$pos]` ;; [")]}"]) matching[$lastoflevel[$level]]=$pos matching[$pos]=$lastoflevel[$level] levelpos[$pos]=$((level--)) - typepos[$pos]=$BUFFER[$pos] + typepos[$pos]=`_zsh_highlight_brackets_highlighter_brackettype $BUFFER[$pos]` ;; ['"'\']) - local temp=$BUFFER[$pos] + local quotetype=$BUFFER[$pos] while (( $pos < ${#BUFFER} )) ; do ((++pos)) - [[ $BUFFER[$pos] == $temp ]] && break + [[ $BUFFER[$pos] == $quotetype ]] && break done ;; esac done - for pos in ${(k)typepos}; do - typepos[$pos]=${typepos[${pos}]/["()"]/round} - typepos[$pos]=${typepos[${pos}]/["[]"]/square} - typepos[$pos]=${typepos[${pos}]/["{}"]/curly} - done for pos in ${(k)levelpos}; do if [[ -z $matching[$pos] ]] || [[ $typepos[$pos] != $typepos[$matching[$pos]] ]]; then region_highlight+=("$((pos - 1)) $pos "$ZSH_HIGHLIGHT_STYLES[bracket-error]) @@ -91,3 +86,13 @@ _zsh_highlight_brackets_highlighter() region_highlight+=("$((otherpos - 1)) $otherpos standout") fi } + +_zsh_highlight_brackets_highlighter_brackettype() +{ + case $1 in + ["()"]) print round;; + ["[]"]) print bracket;; + ["{}"]) print curly;; + *) ;; + esac +}