Skip to content

Commit

Permalink
driver: Make it reentrant.
Browse files Browse the repository at this point in the history
This fixes an infinite recursion in zsh without zle-line-pre-redraw [≤5.2] in
the following situation:

% source zsh-syntax-highlighting.zsh
% eval "my-self-insert() { zle -M 'foobar'; ${(q)widgets[self-insert]#*:} \"\$@\" }"
% zle -N self-insert my-self-insert
% source zsh-syntax-highlighting.zsh

Fixes sorin-ionescu#305.
  • Loading branch information
danielshahaf committed Aug 12, 2016
1 parent e2f863c commit d711563
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions zsh-syntax-highlighting.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ _zsh_highlight_call_widget()
_zsh_highlight_bind_widgets()
{
setopt localoptions noksharrays
local prefix=orig-s$SECONDS-r$RANDOM # unique each time, in case we're sourced more than once

# Load ZSH module zsh/zleparameter, needed to override user defined widgets.
zmodload zsh/zleparameter 2>/dev/null || {
Expand All @@ -258,7 +259,7 @@ _zsh_highlight_bind_widgets()

# Override ZLE widgets to make them invoke _zsh_highlight.
local -U widgets_to_bind
widgets_to_bind=(${${(k)widgets}:#(.*|orig-*|run-help|which-command|beep|set-local-history|yank)})
widgets_to_bind=(${${(k)widgets}:#(.*|run-help|which-command|beep|set-local-history|yank)})

# Always wrap special zle-line-finish widget. This is needed to decide if the
# current line ends and special highlighting logic needs to be applied.
Expand All @@ -283,13 +284,13 @@ _zsh_highlight_bind_widgets()
# NO_function_argzero, regardless of the option's setting here.

# User defined widget: override and rebind old one with prefix "orig-".
user:*) zle -N orig-$cur_widget ${widgets[$cur_widget]#*:}
eval "_zsh_highlight_widget_${(q)cur_widget}() { _zsh_highlight_call_widget orig-${(q)cur_widget} -- \"\$@\" }"
user:*) zle -N $prefix-$cur_widget ${widgets[$cur_widget]#*:}
eval "_zsh_highlight_widget_${(q)cur_widget}() { _zsh_highlight_call_widget ${(q)prefix}-${(q)cur_widget} -- \"\$@\" }"
zle -N $cur_widget _zsh_highlight_widget_$cur_widget;;

# Completion widget: override and rebind old one with prefix "orig-".
completion:*) zle -C orig-$cur_widget ${${(s.:.)widgets[$cur_widget]}[2,3]}
eval "_zsh_highlight_widget_${(q)cur_widget}() { _zsh_highlight_call_widget orig-${(q)cur_widget} -- \"\$@\" }"
completion:*) zle -C $prefix-$cur_widget ${${(s.:.)widgets[$cur_widget]}[2,3]}
eval "_zsh_highlight_widget_${(q)cur_widget}() { _zsh_highlight_call_widget ${(q)prefix}-${(q)cur_widget} -- \"\$@\" }"
zle -N $cur_widget _zsh_highlight_widget_$cur_widget;;

# Builtin widget: override and make it call the builtin ".widget".
Expand Down

0 comments on commit d711563

Please sign in to comment.