@@ -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