diff --git a/Makefile b/Makefile index f454324..2150946 100644 --- a/Makefile +++ b/Makefile @@ -39,7 +39,7 @@ test: for test in highlighters/*; do \ if [ -d $$test/test-data ]; then \ echo "Running test $${test##*/}"; \ - $(ZSH) -f tests/test-highlighting.zsh "$${test##*/}"; \ + $(ZSH) -fu tests/test-highlighting.zsh "$${test##*/}"; \ : $$(( result |= $$? )); \ fi \ done; \ diff --git a/highlighters/brackets/brackets-highlighter.zsh b/highlighters/brackets/brackets-highlighter.zsh index 1bdd1f9..335f7a8 100644 --- a/highlighters/brackets/brackets-highlighter.zsh +++ b/highlighters/brackets/brackets-highlighter.zsh @@ -40,7 +40,7 @@ # Whether the brackets highlighter should be called or not. _zsh_highlight_highlighter_brackets_predicate() { - [[ $WIDGET == zle-line-finish ]] || _zsh_highlight_cursor_moved || _zsh_highlight_buffer_modified + [[ ${WIDGET-} == zle-line-finish ]] || _zsh_highlight_cursor_moved || _zsh_highlight_buffer_modified } # Brackets highlighting function. @@ -59,7 +59,7 @@ _zsh_highlight_highlighter_brackets_paint() lastoflevel[$level]=$pos ;; [")]}"]) - matchingpos=$lastoflevel[$level] + matchingpos=${lastoflevel[$level]-} levelpos[$pos]=$((level--)) if _zsh_highlight_brackets_match $matchingpos $pos; then matching[$matchingpos]=$pos @@ -86,9 +86,9 @@ _zsh_highlight_highlighter_brackets_paint() done # If cursor is on a bracket, then highlight corresponding bracket, if any. - if [[ $WIDGET != zle-line-finish ]]; then + if [[ ${WIDGET-} != zle-line-finish ]]; then pos=$((CURSOR + 1)) - if [[ -n $levelpos[$pos] ]] && [[ -n $matching[$pos] ]]; then + if [[ -n ${levelpos[$pos]-} && -n $matching[$pos] ]]; then local -i otherpos=$matching[$pos] _zsh_highlight_add_highlight $((otherpos - 1)) $otherpos cursor-matchingbracket fi @@ -98,7 +98,7 @@ _zsh_highlight_highlighter_brackets_paint() # Helper function to differentiate type _zsh_highlight_brackets_match() { - case $BUFFER[$1] in + case ${BUFFER[$1]-} in \() [[ $BUFFER[$2] == \) ]];; \[) [[ $BUFFER[$2] == \] ]];; \{) [[ $BUFFER[$2] == \} ]];; diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 1bbc3cb..276964c 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -58,7 +58,7 @@ _zsh_highlight_highlighter_main_predicate() { # may need to remove path_prefix highlighting when the line ends - [[ $WIDGET == zle-line-finish ]] || _zsh_highlight_buffer_modified + [[ ${WIDGET-} == zle-line-finish ]] || _zsh_highlight_buffer_modified } # Helper to deal with tokens crossing line boundaries. diff --git a/tests/test-highlighting.zsh b/tests/test-highlighting.zsh index 243f846..9cedef4 100755 --- a/tests/test-highlighting.zsh +++ b/tests/test-highlighting.zsh @@ -56,7 +56,7 @@ zmodload zsh/zle # Overwrite _zsh_highlight_add_highlight so we get the key itself instead of the style _zsh_highlight_add_highlight() { - region_highlight+=("$1 $2 $3") + region_highlight+=("$1 $2 ${3-}") } # Activate the highlighter. @@ -96,7 +96,7 @@ run_test_internal() { (( --end )) # convert to closed range, like expected_region_highlight (( ++start, ++end )) # region_highlight is 0-indexed; expected_region_highlight is 1-indexed for j in {$start..$end}; do - observed_result[$j]=$highlight_zone[3] + observed_result[$j]=${highlight_zone[3]-} done else # noop range; ignore. @@ -115,10 +115,10 @@ run_test_internal() { local desc="[$start,$end] «${BUFFER[$start,$end]//'#'/♯}»" # Match the emptiness of observed_result if no highlighting is expected [[ $highlight_zone[3] == NONE ]] && highlight_zone[3]= - [[ -n "$highlight_zone[4]" ]] && todo="# TODO $highlight_zone[4]" + [[ -n ${highlight_zone[4]-} ]] && todo="# TODO $highlight_zone[4]" for j in {$start..$end}; do - if [[ "$observed_result[$j]" != "$highlight_zone[3]" ]]; then - print -r -- "not ok $i - $desc - expected ${(qqq)highlight_zone[3]}, observed ${(qqq)observed_result[$j]}. $todo" + if [[ ${observed_result[$j]-} != $highlight_zone[3] ]]; then + print -r -- "not ok $i - $desc - expected ${(qqq)highlight_zone[3]}, observed ${(qqq)observed_result[$j]-}. $todo" continue 2 fi done @@ -162,7 +162,7 @@ run_test() { # Set up results_filter local results_filter -if [[ $QUIET == y ]]; then +if [[ ${QUIET-} == y ]]; then if type -w perl >/dev/null; then results_filter=${0:A:h}/tap-filter else diff --git a/zsh-syntax-highlighting.zsh b/zsh-syntax-highlighting.zsh index 7e1cdc6..5db4b8a 100644 --- a/zsh-syntax-highlighting.zsh +++ b/zsh-syntax-highlighting.zsh @@ -104,7 +104,7 @@ _zsh_highlight() # Remove all highlighting in isearch, so that only the underlining done by zsh itself remains. # For details see FAQ entry 'Why does syntax highlighting not work while searching history?'. - if [[ $WIDGET == zle-isearch-update ]] && ! (( $+ISEARCHMATCH_ACTIVE )); then + if [[ ${WIDGET-} == zle-isearch-update ]] && ! (( $+ISEARCHMATCH_ACTIVE )); then region_highlight=() return $ret fi @@ -118,7 +118,7 @@ _zsh_highlight() [[ -n ${ZSH_HIGHLIGHT_MAXLENGTH:-} ]] && [[ $#BUFFER -gt $ZSH_HIGHLIGHT_MAXLENGTH ]] && return $ret # Do not highlight if there are pending inputs (copy/paste). - [[ $PENDING -gt 0 ]] && return $ret + [[ ${PENDING-} -gt 0 ]] && return $ret # Reset region highlight to build it from scratch typeset -ga region_highlight @@ -198,7 +198,7 @@ _zsh_highlight() } always { typeset -g _ZSH_HIGHLIGHT_PRIOR_BUFFER="$BUFFER" - typeset -gi _ZSH_HIGHLIGHT_PRIOR_CURSOR=$CURSOR + typeset -gi _ZSH_HIGHLIGHT_PRIOR_CURSOR=${CURSOR-} } } @@ -213,10 +213,8 @@ _zsh_highlight_apply_zle_highlight() { local entry="$1" default="$2" integer first="$3" second="$4" - setopt localoptions unset # Is it bug or feature that nounset will often abort this function? - # read the relevant entry from zle_highlight - local region="${zle_highlight[(r)${entry}:*]}" + local region="${zle_highlight[(r)${entry}:*]-}" if [[ -z "$region" ]]; then # entry not specified at all, use default value @@ -261,7 +259,7 @@ _zsh_highlight_buffer_modified() # Returns 0 if the cursor has moved since _zsh_highlight was last called. _zsh_highlight_cursor_moved() { - [[ -n $CURSOR ]] && [[ -n ${_ZSH_HIGHLIGHT_PRIOR_CURSOR-} ]] && (($_ZSH_HIGHLIGHT_PRIOR_CURSOR != $CURSOR)) + [[ -n ${CURSOR-} && -n ${_ZSH_HIGHLIGHT_PRIOR_CURSOR-} ]] && (($_ZSH_HIGHLIGHT_PRIOR_CURSOR != $CURSOR)) } # Add a highlight defined by ZSH_HIGHLIGHT_STYLES. @@ -309,8 +307,17 @@ then } "$@" } _zsh_highlight_bind_widgets(){} + + local prevunsetstate= + [[ -o unset ]] || prevunsetstate=NO_ + setopt localoptions UNSET # for following two add-zle-hook-widget calls... + # TODO: figure out why only 5.3 with no_unset gives following error + # add-zle-hook-widget:84: widgets[$hook]: parameter not set + add-zle-hook-widget zle-line-pre-redraw _zsh_highlight add-zle-hook-widget zle-line-finish _zsh_highlight__zle-line-finish + + setopt noxtrace ${prevunsetstate}UNSET # put back as before else # Rebind all ZLE widgets to make them invoke _zsh_highlights. _zsh_highlight_bind_widgets() @@ -340,7 +347,7 @@ else local cur_widget for cur_widget in $widgets_to_bind; do - case $widgets[$cur_widget] in + case ${widgets[$cur_widget]-} in # Already rebound event: do nothing. user:_zsh_highlight_widget_*);; @@ -367,7 +374,7 @@ else # Incomplete or nonexistent widget: Bind to z-sy-h directly. *) - if [[ $cur_widget == zle-* ]] && [[ -z $widgets[$cur_widget] ]]; then + if [[ $cur_widget == zle-* && -z ${widgets[$cur_widget]-} ]]; then _zsh_highlight_widget_${cur_widget}() { :; _zsh_highlight } zle -N $cur_widget _zsh_highlight_widget_$cur_widget else