Skip to content

Commit 810c2dc

Browse files
committed
Use the new, unreleased zsh 'memo=' feature to remove only our own entries from $region_highlight.
Fixes #418 (interoperability issue with other plugins).
1 parent 075c852 commit 810c2dc

File tree

6 files changed

+97
-9
lines changed

6 files changed

+97
-9
lines changed

HACKING.md

+16
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ expected_region_highlight=(
6767
)
6868
```
6969

70+
Memos and commas
71+
----------------
72+
73+
We append to `region_highlight` as follows:
74+
75+
76+
```zsh
77+
region_highlight+=("$start $end $spec, memo=zsh-syntax-highlighting")
78+
```
79+
80+
That comma is required to cause zsh 5.8 and older to ignore the memo without
81+
ignoring the `$spec`. It's a hack, but given that no further 5.8.x patch
82+
releases are planned, it's been deemed acceptable. See issue #418 and the
83+
cross-referenced issues.
84+
85+
7086
Miscellany
7187
----------
7288

changelog.md

+24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
# Changes in HEAD
22

3+
## Notice about an improbable-but-not-impossible forward incompatibility
4+
5+
Everyone can probably skip this section.
6+
7+
The `master` branch of zsh-syntax-highlighting uses a zsh feature that has not
8+
yet appeared in a zsh release: the `memo=` feature, added to zsh in commit
9+
zsh-5.8-172-gdd6e702ee (after zsh 5.8, before zsh 5.9). In the unlikely event
10+
that this zsh feature should change in an incompatible way before the next
11+
stable zsh release, set `zsh_highlight__memo_feature=0` in your .zshrc files to
12+
disable use of the new feature.
13+
14+
z-sy-h dogfoods the new, unreleased zsh feature because that feature was
15+
added to zsh at z-sy-h's initiative. The new feature is used in the fix
16+
to issue #418.
17+
18+
19+
## Other changes:
20+
321
- Document `$ZSH_HIGHLIGHT_MAXLENGTH`.
422
[#698]
523

@@ -89,6 +107,12 @@
89107
The `assign` style remains supported and has precedence.
90108
[#585]
91109

110+
- Fix interoperability issue with other plugins that use highlighting. The fix
111+
requires zsh 5.8.0.3 or newer. (zsh 5.8.0.2-dev from the `master` branch,
112+
revision zsh-5.8-172-gdd6e702ee or newer is also fine.)
113+
[#418, https://github.com/okapia/zsh-viexchange/issues/1]
114+
115+
92116
# Changes in version 0.7.1
93117

94118
- Remove out-of-date information from the 0.7.0 changelog.

highlighters/pattern/pattern-highlighter.zsh

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ _zsh_highlight_pattern_highlighter_loop()
5454
local -a match mbegin mend
5555
local MATCH; integer MBEGIN MEND
5656
if [[ "$buf" == (#b)(*)(${~pat})* ]]; then
57-
region_highlight+=("$((mbegin[2] - 1)) $mend[2] $ZSH_HIGHLIGHT_PATTERNS[$pat]")
57+
region_highlight+=("$((mbegin[2] - 1)) $mend[2] $ZSH_HIGHLIGHT_PATTERNS[$pat], memo=zsh-syntax-highlighting")
5858
"$0" "$match[1]" "$pat"; return $?
5959
fi
6060
}

highlighters/regexp/regexp-highlighter.zsh

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ _zsh_highlight_regexp_highlighter_loop()
5555
local -a match mbegin mend
5656
while true; do
5757
[[ "$buf" =~ "$pat" ]] || return;
58-
region_highlight+=("$((MBEGIN - 1 + OFFSET)) $((MEND + OFFSET)) $ZSH_HIGHLIGHT_REGEXP[$pat]")
58+
region_highlight+=("$((MBEGIN - 1 + OFFSET)) $((MEND + OFFSET)) $ZSH_HIGHLIGHT_REGEXP[$pat], memo=zsh-syntax-highlighting")
5959
buf="$buf[$(($MEND+1)),-1]"
6060
OFFSET=$((MEND+OFFSET));
6161
done

tests/test-highlighting.zsh

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ run_test_internal() {
190190
if
191191
[[ $start != $exp_start ]] ||
192192
[[ $end != $exp_end ]] ||
193-
[[ $highlight_zone[3] != $expected_highlight_zone[3] ]]
193+
[[ ${highlight_zone[3]%,} != ${expected_highlight_zone[3]} ]] # remove the comma that's before the memo field
194194
then
195195
print -r -- "not ok $i - $desc - expected ($exp_start $exp_end ${(qqq)expected_highlight_zone[3]}), observed ($start $end ${(qqq)highlight_zone[3]}). $todo"
196196
if [[ -z $todo ]]; then (( ++print_expected_and_actual )); fi

zsh-syntax-highlighting.zsh

+54-6
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,63 @@ _zsh_highlight()
8383
return $ret
8484
}
8585

86+
# Probe the memo= feature, once.
87+
(( ${+zsh_highlight__memo_feature} )) || {
88+
region_highlight+=( " 0 0 fg=red, memo=zsh-syntax-highlighting" )
89+
case ${region_highlight[-1]} in
90+
("0 0 fg=red")
91+
# zsh 5.8 or earlier
92+
integer -gr zsh_highlight__memo_feature=0
93+
;;
94+
("0 0 fg=red memo=zsh-syntax-highlighting")
95+
# zsh 5.9 or later
96+
integer -gr zsh_highlight__memo_feature=1
97+
;;
98+
(" 0 0 fg=red, memo=zsh-syntax-highlighting") ;&
99+
(*)
100+
# We can get here in two ways:
101+
#
102+
# 1. When not running as a widget. In that case, $region_highlight is
103+
# not a special variable (= one with custom getter/setter functions
104+
# written in C) but an ordinary one, so the third case pattern matches
105+
# and we fall through to this block. (The test suite uses this codepath.)
106+
#
107+
# 2. When running under a future version of zsh that will have changed
108+
# the serialization of $region_highlight elements from their underlying
109+
# C structs, so that none of the previous case patterns will match.
110+
#
111+
# In either case, fall back to a version check.
112+
#
113+
# The memo= feature was added to zsh in commit zsh-5.8-172-gdd6e702ee.
114+
# The version number at the time was 5.8.0.2-dev (see Config/version.mk).
115+
# Therefore, on 5.8.0.3 and newer the memo= feature is available.
116+
#
117+
# On zsh version 5.8.0.2 between the aforementioned commit and the
118+
# first Config/version.mk bump after it (which, at the time of writing,
119+
# is yet to come), this condition will false negative.
120+
if is-at-least 5.8.0.3; then
121+
integer -gr zsh_highlight__memo_feature=1
122+
else
123+
integer -gr zsh_highlight__memo_feature=0
124+
fi
125+
;;
126+
esac
127+
region_highlight[-1]=()
128+
}
129+
130+
# Reset region_highlight to build it from scratch
131+
if (( zsh_highlight__memo_feature )); then
132+
region_highlight=( "${(@)region_highlight:#*memo=zsh-syntax-highlighting*}" )
133+
else
134+
# Legacy codepath. Not very interoperable with other plugins (issue #418).
135+
region_highlight=()
136+
fi
137+
86138
# Remove all highlighting in isearch, so that only the underlining done by zsh itself remains.
87139
# For details see FAQ entry 'Why does syntax highlighting not work while searching history?'.
88140
# This disables highlighting during isearch (for reasons explained in README.md) unless zsh is new enough
89141
# and doesn't have the pattern matching bug
90142
if [[ $WIDGET == zle-isearch-update ]] && { $zsh_highlight__pat_static_bug || ! (( $+ISEARCHMATCH_ACTIVE )) }; then
91-
region_highlight=()
92143
return $ret
93144
fi
94145

@@ -124,9 +175,6 @@ _zsh_highlight()
124175
# Do not highlight if there are pending inputs (copy/paste).
125176
[[ $PENDING -gt 0 ]] && return $ret
126177

127-
# Reset region highlight to build it from scratch
128-
region_highlight=();
129-
130178
{
131179
local cache_place
132180
local -a region_highlight_copy
@@ -245,7 +293,7 @@ _zsh_highlight_apply_zle_highlight() {
245293
else
246294
start=$second end=$first
247295
fi
248-
region_highlight+=("$start $end $region")
296+
region_highlight+=("$start $end $region, memo=zsh-syntax-highlighting")
249297
}
250298

251299

@@ -285,7 +333,7 @@ _zsh_highlight_add_highlight()
285333
shift 2
286334
for highlight; do
287335
if (( $+ZSH_HIGHLIGHT_STYLES[$highlight] )); then
288-
region_highlight+=("$start $end $ZSH_HIGHLIGHT_STYLES[$highlight]")
336+
region_highlight+=("$start $end $ZSH_HIGHLIGHT_STYLES[$highlight], memo=zsh-syntax-highlighting")
289337
break
290338
fi
291339
done

0 commit comments

Comments
 (0)