Skip to content

Commit 171a4ee

Browse files
committed
Merge remote-tracking branch 'danielsh/m0vie-i288-v2' (revised version of upstream/pr/288)
* danielsh/m0vie-i288-v2: driver: Don't highlight in isearch driver: Always bind zle-line-finish and use it instead of accept-* driver: Widget binding: Support binding incomplete/nonexistent widgets
2 parents add6825 + a8fe22d commit 171a4ee

File tree

5 files changed

+53
-11
lines changed

5 files changed

+53
-11
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ custom widgets have been created (i.e., after all `zle -N` calls and after
3232
running `compinit`). Widgets created later will work, but will not update the
3333
syntax highlighting.
3434

35+
### Why does syntax highlighting not work while searching history?
36+
37+
_This problem is fixed in zsh 5.3 and newer._
38+
39+
Highlighting the command line during an incremental history search
40+
(with the `history-incremental-search-backward` widget, which is
41+
bound by default to <kbd>Ctrl+R</kbd> in zsh's emacs keymap) requires zsh 5.3
42+
or newer.
43+
44+
Under zsh 5.2 and older, the zsh-default underlining of the matched portion
45+
of the buffer remains available, but zsh-syntax-highlighting's additional
46+
highlighting is unavailable. (Those versions of zsh do not provide enough
47+
information to allow computing the highlighting correctly.)
48+
49+
See [issue #288][i288] for details.
50+
51+
[i288]: https://github.com/zsh-users/zsh-syntax-highlighting/pull/288
52+
3553
### How are new releases announced?
3654

3755
There is currently no "push" announcements channel. However, the following

highlighters/cursor/cursor-highlighter.zsh

+3-4
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,14 @@
3434
# Whether the cursor highlighter should be called or not.
3535
_zsh_highlight_cursor_highlighter_predicate()
3636
{
37-
# accept-* may trigger removal of cursor highlighting
38-
[[ $WIDGET == accept-* ]] ||
39-
_zsh_highlight_cursor_moved
37+
# remove cursor highlighting when the line is finished
38+
[[ $WIDGET == zle-line-finish ]] || _zsh_highlight_cursor_moved
4039
}
4140

4241
# Cursor highlighting function.
4342
_zsh_highlight_cursor_highlighter()
4443
{
45-
[[ $WIDGET == accept-* ]] && return
44+
[[ $WIDGET == zle-line-finish ]] && return
4645

4746
_zsh_highlight_add_highlight $CURSOR $(( $CURSOR + 1 )) cursor
4847
}

highlighters/main/main-highlighter.zsh

+3-4
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,8 @@
6262
# Whether the highlighter should be called or not.
6363
_zsh_highlight_main_highlighter_predicate()
6464
{
65-
# accept-* may trigger removal of path_prefix highlighting
66-
[[ $WIDGET == accept-* ]] ||
67-
_zsh_highlight_buffer_modified
65+
# may need to remove path_prefix highlighting when the line ends
66+
[[ $WIDGET == zle-line-finish ]] || _zsh_highlight_buffer_modified
6867
}
6968

7069
# Helper to deal with tokens crossing line boundaries.
@@ -596,7 +595,7 @@ _zsh_highlight_main_highlighter_check_path()
596595

597596
# If this word ends the buffer, check if it's the prefix of a valid path.
598597
if [[ ${BUFFER[1]} != "-" && ${#BUFFER} == $end_pos ]] &&
599-
[[ $WIDGET != accept-* ]]; then
598+
[[ $WIDGET != zle-line-finish ]]; then
600599
local -a tmp
601600
tmp=( ${expanded_path}*(N) )
602601
(( $#tmp > 0 )) && REPLY=path_prefix && return 0

highlighters/main/test-data/path_prefix2.zsh

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
# Related to path_prefix.zsh
3232

3333
BUFFER='ls /bin/s'
34-
WIDGET=accept-line
34+
WIDGET=zle-line-finish
3535

3636
expected_region_highlight=(
3737
"4 9 default" # /bin/s

zsh-syntax-highlighting.zsh

+28-2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ _zsh_highlight()
5858
# Store the previous command return code to restore it whatever happens.
5959
local ret=$?
6060

61+
# Remove all highlighting in isearch, so that only the underlining done by zsh itself remains.
62+
# For details see FAQ entry 'Why does syntax highlighting not work while searching history?'.
63+
if [[ $WIDGET == zle-isearch-update ]]; then
64+
region_highlight=()
65+
return $ret
66+
fi
67+
6168
setopt localoptions warncreateglobal
6269
setopt localoptions noksharrays
6370
local REPLY # don't leak $REPLY into global scope
@@ -250,8 +257,20 @@ _zsh_highlight_bind_widgets()
250257
}
251258

252259
# Override ZLE widgets to make them invoke _zsh_highlight.
260+
local -U widgets_to_bind
261+
widgets_to_bind=(${${(k)widgets}:#(.*|orig-*|run-help|which-command|beep|set-local-history|yank)})
262+
263+
# Always wrap special zle-line-finish widget. This is needed to decide if the
264+
# current line ends and special highlighting logic needs to be applied.
265+
# E.g. remove cursor imprint, don't highlight partial paths, ...
266+
widgets_to_bind+=(zle-line-finish)
267+
268+
# Always wrap special zle-isearch-update widget to be notified of updates in isearch.
269+
# This is needed because we need to disable highlighting in that case.
270+
widgets_to_bind+=(zle-isearch-update)
271+
253272
local cur_widget
254-
for cur_widget in ${${(k)widgets}:#(.*|orig-*|run-help|which-command|beep|set-local-history|yank)}; do
273+
for cur_widget in $widgets_to_bind; do
255274
case $widgets[$cur_widget] in
256275

257276
# Already rebound event: do nothing.
@@ -277,8 +296,15 @@ _zsh_highlight_bind_widgets()
277296
builtin) eval "_zsh_highlight_widget_${(q)cur_widget}() { _zsh_highlight_call_widget .${(q)cur_widget} -- \"\$@\" }"
278297
zle -N $cur_widget _zsh_highlight_widget_$cur_widget;;
279298

299+
# Incomplete or nonexistent widget: Bind to z-sy-h directly.
300+
*)
301+
if [[ $cur_widget == zle-* ]] && [[ -z $widgets[$cur_widget] ]]; then
302+
_zsh_highlight_widget_${cur_widget}() { :; _zsh_highlight }
303+
zle -N $cur_widget _zsh_highlight_widget_$cur_widget
304+
else
280305
# Default: unhandled case.
281-
*) print -r -- >&2 "zsh-syntax-highlighting: unhandled ZLE widget '$cur_widget'" ;;
306+
print -r -- >&2 "zsh-syntax-highlighting: unhandled ZLE widget '$cur_widget'"
307+
fi
282308
esac
283309
done
284310
}

0 commit comments

Comments
 (0)