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

Re: Does add-zle-hook-widget violate the contract of ZLE hook widgets?



On Thu, Jun 24, 2021 at 2:49 PM Marlon Richert <marlon.richert@xxxxxxxxx> wrote:
>
> That makes sense, but then, it should also be documented what that
> actually means. If your function returns zero/non-zero, what is
> expected to happen? Does this always happen? Or are there different
> classes of functions with different results for zero/non-zero?

The convention is that returning zero means success (the canonical
example being the "true" command) and nonzero means some kind of
failure ("false").  The actual numeric value of the non-zero return
might mean something.  Theoretically every command (function, builtin,
script, whatever) that "succeeds" should be returning zero; the fact
that this is sometimes ignored (and that for example the return/exit
status of the last command executed within the function body is
retained as $? instead) is programmer laziness.

> I can already think of two examples that contradict each other:
> * In hook functions, callee returning NON-zero causes the caller to
> refrain from calling more functions.
> * In completion functions, callee returning ZERO causes the caller to
> refrain from calling more functions.

These are not really contradictory; you're ignoring important context.
In both cases zero means success and nonzero means failure.  The
context determines what that signifies.

For completion functions, success means that completions have been
generated and further work is not necessary, failure means it's
reasonable to try a different approach.

For hook functions, success means that it's OK to proceed, and failure
means that something bad happened and further work is potentially
dangerous.

You can't just look at the return codes in isolation and say "these
are reversed" without considering what success or failure means in
context.

In either case we could actually extend the model to use different
nonzero values of $? to distinguish "try something else" and "danger,
abort" conditions; it just happens that for hooks, currently anything
nonzero is considered "danger, abort" and for completion it is always
"try something else".




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