Use the new, unreleased zsh 'memo=' feature to remove only our own entries from $region_highlight.

Fixes #418 (interoperability issue with other plugins).
This commit is contained in:
Daniel Shahaf 2020-07-14 00:24:04 +00:00
parent 075c852924
commit 810c2dcede
6 changed files with 97 additions and 9 deletions

View File

@ -67,6 +67,22 @@ expected_region_highlight=(
)
```
Memos and commas
----------------
We append to `region_highlight` as follows:
```zsh
region_highlight+=("$start $end $spec, memo=zsh-syntax-highlighting")
```
That comma is required to cause zsh 5.8 and older to ignore the memo without
ignoring the `$spec`. It's a hack, but given that no further 5.8.x patch
releases are planned, it's been deemed acceptable. See issue #418 and the
cross-referenced issues.
Miscellany
----------

View File

@ -1,5 +1,23 @@
# Changes in HEAD
## Notice about an improbable-but-not-impossible forward incompatibility
Everyone can probably skip this section.
The `master` branch of zsh-syntax-highlighting uses a zsh feature that has not
yet appeared in a zsh release: the `memo=` feature, added to zsh in commit
zsh-5.8-172-gdd6e702ee (after zsh 5.8, before zsh 5.9). In the unlikely event
that this zsh feature should change in an incompatible way before the next
stable zsh release, set `zsh_highlight__memo_feature=0` in your .zshrc files to
disable use of the new feature.
z-sy-h dogfoods the new, unreleased zsh feature because that feature was
added to zsh at z-sy-h's initiative. The new feature is used in the fix
to issue #418.
## Other changes:
- Document `$ZSH_HIGHLIGHT_MAXLENGTH`.
[#698]
@ -89,6 +107,12 @@
The `assign` style remains supported and has precedence.
[#585]
- Fix interoperability issue with other plugins that use highlighting. The fix
requires zsh 5.8.0.3 or newer. (zsh 5.8.0.2-dev from the `master` branch,
revision zsh-5.8-172-gdd6e702ee or newer is also fine.)
[#418, https://github.com/okapia/zsh-viexchange/issues/1]
# Changes in version 0.7.1
- Remove out-of-date information from the 0.7.0 changelog.

View File

@ -54,7 +54,7 @@ _zsh_highlight_pattern_highlighter_loop()
local -a match mbegin mend
local MATCH; integer MBEGIN MEND
if [[ "$buf" == (#b)(*)(${~pat})* ]]; then
region_highlight+=("$((mbegin[2] - 1)) $mend[2] $ZSH_HIGHLIGHT_PATTERNS[$pat]")
region_highlight+=("$((mbegin[2] - 1)) $mend[2] $ZSH_HIGHLIGHT_PATTERNS[$pat], memo=zsh-syntax-highlighting")
"$0" "$match[1]" "$pat"; return $?
fi
}

View File

@ -55,7 +55,7 @@ _zsh_highlight_regexp_highlighter_loop()
local -a match mbegin mend
while true; do
[[ "$buf" =~ "$pat" ]] || return;
region_highlight+=("$((MBEGIN - 1 + OFFSET)) $((MEND + OFFSET)) $ZSH_HIGHLIGHT_REGEXP[$pat]")
region_highlight+=("$((MBEGIN - 1 + OFFSET)) $((MEND + OFFSET)) $ZSH_HIGHLIGHT_REGEXP[$pat], memo=zsh-syntax-highlighting")
buf="$buf[$(($MEND+1)),-1]"
OFFSET=$((MEND+OFFSET));
done

View File

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

View File

@ -83,12 +83,63 @@ _zsh_highlight()
return $ret
}
# Probe the memo= feature, once.
(( ${+zsh_highlight__memo_feature} )) || {
region_highlight+=( " 0 0 fg=red, memo=zsh-syntax-highlighting" )
case ${region_highlight[-1]} in
("0 0 fg=red")
# zsh 5.8 or earlier
integer -gr zsh_highlight__memo_feature=0
;;
("0 0 fg=red memo=zsh-syntax-highlighting")
# zsh 5.9 or later
integer -gr zsh_highlight__memo_feature=1
;;
(" 0 0 fg=red, memo=zsh-syntax-highlighting") ;&
(*)
# We can get here in two ways:
#
# 1. When not running as a widget. In that case, $region_highlight is
# not a special variable (= one with custom getter/setter functions
# written in C) but an ordinary one, so the third case pattern matches
# and we fall through to this block. (The test suite uses this codepath.)
#
# 2. When running under a future version of zsh that will have changed
# the serialization of $region_highlight elements from their underlying
# C structs, so that none of the previous case patterns will match.
#
# In either case, fall back to a version check.
#
# The memo= feature was added to zsh in commit zsh-5.8-172-gdd6e702ee.
# The version number at the time was 5.8.0.2-dev (see Config/version.mk).
# Therefore, on 5.8.0.3 and newer the memo= feature is available.
#
# On zsh version 5.8.0.2 between the aforementioned commit and the
# first Config/version.mk bump after it (which, at the time of writing,
# is yet to come), this condition will false negative.
if is-at-least 5.8.0.3; then
integer -gr zsh_highlight__memo_feature=1
else
integer -gr zsh_highlight__memo_feature=0
fi
;;
esac
region_highlight[-1]=()
}
# Reset region_highlight to build it from scratch
if (( zsh_highlight__memo_feature )); then
region_highlight=( "${(@)region_highlight:#*memo=zsh-syntax-highlighting*}" )
else
# Legacy codepath. Not very interoperable with other plugins (issue #418).
region_highlight=()
fi
# 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?'.
# This disables highlighting during isearch (for reasons explained in README.md) unless zsh is new enough
# and doesn't have the pattern matching bug
if [[ $WIDGET == zle-isearch-update ]] && { $zsh_highlight__pat_static_bug || ! (( $+ISEARCHMATCH_ACTIVE )) }; then
region_highlight=()
return $ret
fi
@ -124,9 +175,6 @@ _zsh_highlight()
# Do not highlight if there are pending inputs (copy/paste).
[[ $PENDING -gt 0 ]] && return $ret
# Reset region highlight to build it from scratch
region_highlight=();
{
local cache_place
local -a region_highlight_copy
@ -245,7 +293,7 @@ _zsh_highlight_apply_zle_highlight() {
else
start=$second end=$first
fi
region_highlight+=("$start $end $region")
region_highlight+=("$start $end $region, memo=zsh-syntax-highlighting")
}
@ -285,7 +333,7 @@ _zsh_highlight_add_highlight()
shift 2
for highlight; do
if (( $+ZSH_HIGHLIGHT_STYLES[$highlight] )); then
region_highlight+=("$start $end $ZSH_HIGHLIGHT_STYLES[$highlight]")
region_highlight+=("$start $end $ZSH_HIGHLIGHT_STYLES[$highlight], memo=zsh-syntax-highlighting")
break
fi
done