Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Wrapping special widget causes infinite recursion in add-zle-hook-widget



On Fri, Jun 18, 2021 at 3:32 PM Marlon Richert <marlon.richert@xxxxxxxxx> wrote:
>
> I originally reported this
> [here](https://github.com/zsh-users/zsh-syntax-highlighting/issues/816)
> but Daniel was of the opinion it should be fixed in
> add-zle-hook-widget.
>
> Steps to reproduce are as follows:
>
> % foo bar () { }
> % autoload -Uz add-zle-hook-widget
> % add-zle-hook-widget line-init foo
> % zle -N old-zle-line-init azhw:zle-line-init
> % new-zle-line-init() { zle old-zle-line-init }
> % zle -N zle-line-init new-zle-line-init
> % add-zle-hook-widget line-init bar
> [Process completed]
>
> The test case here uses zle-line-init, but it's not specific to that
> widget. It can be reproduced with any of the special widgets that
> add-zle-hook-widget handles.

I've bumped into similar issues with add-zle-hook-widget:
https://github.com/romkatv/powerlevel10k/issues/1205#issuecomment-767496102

The first test case:

    autoload -Uz add-zsh-hook-widget add-zle-hook-widget

    # Add `foo` hook to zle-line-finish.
    function foo() {}
    add-zle-hook-widget line-finish foo

    # Invoke `bar` on zle-line-finish, which in turn invokes the
original widget.
    zle -A zle-line-finish orig-zle-line-finish
    function bar() zle orig-zle-line-finish
    zle -N zle-line-finish bar

    # Add `baz` hook to zle-line-finish.
    function baz() {}
    add-zle-hook-widget line-finish baz

This produces `bar: job table full or recursion limit exceeded` on
zle-line-finish.

Here's a similar test case but with a different effect.

    autoload -Uz add-zsh-hook-widget add-zle-hook-widget

    # Add `foo` hook to zle-line-finish.
    function foo() print -rl '' foo
    add-zle-hook-widget line-finish foo

    # Invoke `bar` on zle-line-finish, which in turn invokes the
original widget.
    zle -A zle-line-finish orig-zle-line-finish
    function bar() { zle orig-zle-line-finish; print bar; }
    zle -N zle-line-finish bar

    # Add `foo` hook to zle-line-finish for the second time.
    add-zle-hook-widget line-finish foo

This results in `foo` being called on zle-line-finish but `bar`
doesn't get called.

These look like bugs in add-zle-hook-widget to me. Both can be fixed.
There is some choice w.r.t. the desired behavior of these test cases.
I think it would be reasonable for the first to invoke `bar`, `foo`,
`baz` (in this order) and for the second to invoke `bar`, `foo` (in
this order). Here's one more test case to complete the picture:

    autoload -Uz add-zsh-hook-widget add-zle-hook-widget

    # Add `foo` hook to zle-line-finish.
    function foo() print -rl '' foo
    add-zle-hook-widget line-finish foo

    # Invoke `bar` on zle-line-finish, which does nothing.
    function bar() {}
    zle -N zle-line-finish bar

    # Add `foo` hook to zle-line-finish for the second time.
    add-zle-hook-widget line-finish foo

I think it reasonable to require that `bar` and `foo` get called here
(in this order).

It's not too difficult to implement add-zle-hook-widget in a way that
it conforms to these requirements and doesn't break the existing code
that checks for `[[ $widgets[zle-line-finish] ==
user:azhw:zle-line-finish ]]`.

Roman.




Messages sorted by: Reverse Date, Date, Thread, Author