From e1ed255c0a5f6db94bd709b0019a1e6b923f5049 Mon Sep 17 00:00:00 2001 From: Andreas Gahr Date: Mon, 8 Aug 2011 14:48:16 +0200 Subject: [PATCH 1/5] bracket highlighter: start loops with 0 which is more natural and also more in line whith the other highlighters --- .../brackets/brackets-highlighter.zsh | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/highlighters/brackets/brackets-highlighter.zsh b/highlighters/brackets/brackets-highlighter.zsh index e1ffdae..49c3909 100644 --- a/highlighters/brackets/brackets-highlighter.zsh +++ b/highlighters/brackets/brackets-highlighter.zsh @@ -46,47 +46,55 @@ _zsh_highlight_brackets_highlighter_predicate() # Brackets highlighting function. _zsh_highlight_brackets_highlighter() { - local level pos + local level=0 pos local -A levelpos lastoflevel matching typepos - ((level = 0)) - for ((pos = 1; $pos <= ${#BUFFER}; pos++ )) ; do - case $BUFFER[pos] in + + # Find all brackets and remember which one is matching + for (( pos = 0; $pos < ${#BUFFER}; pos++ )) ; do + local char=$BUFFER[pos+1] + case $char in ["([{"]) levelpos[$pos]=$((++level)) 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[$pos]=$lastoflevel[$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 - ((++pos)) - [[ $BUFFER[$pos] == $quotetype ]] && break + (( pos++ )) + [[ $BUFFER[$pos+1] == $quotetype ]] && break done ;; esac done + + # Now highlight all found brackets 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]) + region_highlight+=("$pos $((pos + 1)) "$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]) + region_highlight+=("$pos $((pos + 1)) "$ZSH_HIGHLIGHT_STYLES[$style]) fi 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 local otherpos=$matching[$pos] - region_highlight+=("$((otherpos - 1)) $otherpos standout") + region_highlight+=("$otherpos $((otherpos + 1)) standout") fi } +# Helper function to differentiate type _zsh_highlight_brackets_highlighter_brackettype() { case $1 in From 24243becba10200cf57e2e18c9d99831a542f81a Mon Sep 17 00:00:00 2001 From: Andreas Gahr Date: Mon, 8 Aug 2011 15:51:40 +0200 Subject: [PATCH 2/5] bracket highlighter: fix typo from last commit --- highlighters/brackets/brackets-highlighter.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/highlighters/brackets/brackets-highlighter.zsh b/highlighters/brackets/brackets-highlighter.zsh index 49c3909..6ea2b70 100644 --- a/highlighters/brackets/brackets-highlighter.zsh +++ b/highlighters/brackets/brackets-highlighter.zsh @@ -87,7 +87,7 @@ _zsh_highlight_brackets_highlighter() done # If cursor is on a bracket, then highlight corresponding bracket, if any - pos=CURSOR + pos=$CURSOR if [[ -n $levelpos[$pos] ]] && [[ -n $matching[$pos] ]]; then local otherpos=$matching[$pos] region_highlight+=("$otherpos $((otherpos + 1)) standout") From 8375ef6e7221119ba175a0f5ae4ef555e818e482 Mon Sep 17 00:00:00 2001 From: Andreas Gahr Date: Mon, 8 Aug 2011 15:55:47 +0200 Subject: [PATCH 3/5] bracket highlighter: make the style configurable, when cursor is on bracket --- highlighters/brackets/brackets-highlighter.zsh | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/highlighters/brackets/brackets-highlighter.zsh b/highlighters/brackets/brackets-highlighter.zsh index 6ea2b70..893377b 100644 --- a/highlighters/brackets/brackets-highlighter.zsh +++ b/highlighters/brackets/brackets-highlighter.zsh @@ -36,6 +36,7 @@ : ${ZSH_HIGHLIGHT_STYLES[bracket-level-3]:=fg=magenta,bold} : ${ZSH_HIGHLIGHT_STYLES[bracket-level-4]:=fg=yellow,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. _zsh_highlight_brackets_highlighter_predicate() @@ -78,11 +79,13 @@ _zsh_highlight_brackets_highlighter() # Now highlight all found brackets for pos in ${(k)levelpos}; do if [[ -z $matching[$pos] ]] || [[ $typepos[$pos] != $typepos[$matching[$pos]] ]]; then - region_highlight+=("$pos $((pos + 1)) "$ZSH_HIGHLIGHT_STYLES[bracket-error]) + local style=$ZSH_HIGHLIGHT_STYLES[bracket-error] + region_highlight+=("$pos $((pos + 1)) $style") 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 $((pos + 1)) "$ZSH_HIGHLIGHT_STYLES[$style]) + local bracket_color_level=bracket-level-$(( (levelpos[$pos] - 1) % bracket_color_size + 1 )) + local style=$ZSH_HIGHLIGHT_STYLES[$bracket_color_level] + region_highlight+=("$pos $((pos + 1)) $style") fi done @@ -90,7 +93,8 @@ _zsh_highlight_brackets_highlighter() pos=$CURSOR if [[ -n $levelpos[$pos] ]] && [[ -n $matching[$pos] ]]; then local otherpos=$matching[$pos] - region_highlight+=("$otherpos $((otherpos + 1)) standout") + local style=$ZSH_HIGHLIGHT_STYLES[cursor-matchingbracket] + region_highlight+=("$otherpos $((otherpos + 1)) $style") fi } From 06ad03e32caf11b4b02e13ae3a74c5ab87e5ceb4 Mon Sep 17 00:00:00 2001 From: Andreas Gahr Date: Mon, 8 Aug 2011 16:01:53 +0200 Subject: [PATCH 4/5] bracket highlighter: simplify --- highlighters/brackets/brackets-highlighter.zsh | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/highlighters/brackets/brackets-highlighter.zsh b/highlighters/brackets/brackets-highlighter.zsh index 893377b..5166122 100644 --- a/highlighters/brackets/brackets-highlighter.zsh +++ b/highlighters/brackets/brackets-highlighter.zsh @@ -57,13 +57,13 @@ _zsh_highlight_brackets_highlighter() ["([{"]) levelpos[$pos]=$((++level)) lastoflevel[$level]=$pos - typepos[$pos]=`_zsh_highlight_brackets_highlighter_brackettype $char` + _zsh_highlight_brackets_highlighter_brackettype $char ;; [")]}"]) matching[$lastoflevel[$level]]=$pos matching[$pos]=$lastoflevel[$level] levelpos[$pos]=$((level--)) - typepos[$pos]=`_zsh_highlight_brackets_highlighter_brackettype $char` + _zsh_highlight_brackets_highlighter_brackettype $char ;; ['"'\']) # Skip everything inside quotes @@ -78,14 +78,14 @@ _zsh_highlight_brackets_highlighter() # Now highlight all found brackets for pos in ${(k)levelpos}; do - if [[ -z $matching[$pos] ]] || [[ $typepos[$pos] != $typepos[$matching[$pos]] ]]; then - local style=$ZSH_HIGHLIGHT_STYLES[bracket-error] - region_highlight+=("$pos $((pos + 1)) $style") - else + if [[ -n $matching[$pos] ]] && [[ $typepos[$pos] == $typepos[$matching[$pos]] ]]; then local bracket_color_size=${#ZSH_HIGHLIGHT_STYLES[(I)bracket-level-*]} local bracket_color_level=bracket-level-$(( (levelpos[$pos] - 1) % bracket_color_size + 1 )) 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 done @@ -102,9 +102,9 @@ _zsh_highlight_brackets_highlighter() _zsh_highlight_brackets_highlighter_brackettype() { case $1 in - ["()"]) print round;; - ["[]"]) print bracket;; - ["{}"]) print curly;; + ["()"]) typepos[$pos]=round;; + ["[]"]) typepos[$pos]=bracket;; + ["{}"]) typepos[$pos]=curly;; *) ;; esac } From b1b65dc486db3ab3a4bce618c1b11d8a2e1fdff6 Mon Sep 17 00:00:00 2001 From: Andreas Gahr Date: Mon, 8 Aug 2011 17:01:51 +0200 Subject: [PATCH 5/5] bracket highlighter: small typo in testcase - document new style --- highlighters/brackets/README.md | 1 + highlighters/brackets/test-data/nested-parentheses.zsh | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/highlighters/brackets/README.md b/highlighters/brackets/README.md index b0a3d30..9fd4f96 100644 --- a/highlighters/brackets/README.md +++ b/highlighters/brackets/README.md @@ -17,6 +17,7 @@ This highlighter defines the following styles: * `bracket-error` - unmatched brackets * `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`: diff --git a/highlighters/brackets/test-data/nested-parentheses.zsh b/highlighters/brackets/test-data/nested-parentheses.zsh index 08fe1fb..68683e5 100644 --- a/highlighters/brackets/test-data/nested-parentheses.zsh +++ b/highlighters/brackets/test-data/nested-parentheses.zsh @@ -28,7 +28,7 @@ # vim: ft=zsh sw=2 ts=2 et # ------------------------------------------------------------------------------------------------- -BUFFER='echo $(echo x{(z)array})' +BUFFER='echo $(echo ${(z)array})' expected_region_highlight=( "6 6 $ZSH_HIGHLIGHT_STYLES[bracket-level-1]" # (