Skip to content

Commit 835fec7

Browse files
committed
workaround for PAT_STATIC bug in zsh
ZSH versions less than 5.3.2 (or 5.4) have a bug that prevents `history-incremental-pattern-search-backward` for working correctly (the history stops searching after the first found item). Closes #407
1 parent 9523d6d commit 835fec7

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

README.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ syntax highlighting.
4646

4747
### Does syntax highlighting work during incremental history search?
4848

49-
Highlighting the command line during an incremental history search
50-
(with the `history-incremental-search-backward` widget, which is
51-
bound by default to <kbd>Ctrl+R</kbd> in zsh's emacs keymap) requires zsh 5.3
52-
or newer.
49+
Yes!
50+
51+
If you're using `history-incremental-search-backward` (by default bound to <kbd>Ctrl+R</kbd>
52+
in zsh's emacs keymap) then it works with _zsh version 5.3 and newer_.
53+
54+
If you're using `history-incremental-pattern-search-backward`, then syntax highlighting works
55+
in _zsh 5.3.2 and newer_ due to [a bug in zsh](http://www.zsh.org/cgi-bin/mla/redirect?WORKERNUMBER=40285).
5356

5457
Under zsh 5.2 and older, the zsh-default [underlining][zshzle-Character-Highlighting]
5558
of the matched portion of the buffer remains available, but zsh-syntax-highlighting's

zsh-syntax-highlighting.zsh

+12-3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ fi
5353
# Core highlighting update system
5454
# -------------------------------------------------------------------------------------------------
5555

56+
# Use workaround for bug in ZSH?
57+
# zsh-users/zsh@48cadf4 http://www.zsh.org/mla/workers//2017/msg00034.html
58+
autoload -U is-at-least
59+
if is-at-least 5.3.2; then
60+
zsh_highlight__pat_static_bug=false
61+
else
62+
zsh_highlight__pat_static_bug=true
63+
fi
64+
5665
# Array declaring active highlighters names.
5766
typeset -ga ZSH_HIGHLIGHT_HIGHLIGHTERS
5867

@@ -67,7 +76,9 @@ _zsh_highlight()
6776

6877
# Remove all highlighting in isearch, so that only the underlining done by zsh itself remains.
6978
# For details see FAQ entry 'Why does syntax highlighting not work while searching history?'.
70-
if [[ $WIDGET == zle-isearch-update ]] && ! (( $+ISEARCHMATCH_ACTIVE )); then
79+
# This disables highlighting during isearch (for reasons explained in README.md) unless zsh is new enough
80+
# and doesn't have the 5.3.1 bug
81+
if [[ $WIDGET == zle-isearch-update ]] && { $zsh_highlight__pat_static_bug || ! (( $+ISEARCHMATCH_ACTIVE )) }; then
7182
region_highlight=()
7283
return $ret
7384
fi
@@ -394,8 +405,6 @@ add-zsh-hook preexec _zsh_highlight_preexec_hook 2>/dev/null || {
394405
# Load zsh/parameter module if available
395406
zmodload zsh/parameter 2>/dev/null || true
396407

397-
autoload -U is-at-least
398-
399408
# Initialize the array of active highlighters if needed.
400409
[[ $#ZSH_HIGHLIGHT_HIGHLIGHTERS -eq 0 ]] && ZSH_HIGHLIGHT_HIGHLIGHTERS=(main)
401410

0 commit comments

Comments
 (0)