Syntactic sugar for conditional control: defining an IF command

There has been some discussion of whether Kakoune would benefit from conditional control:

As of now the consensus is negative: extending the “language” would go against Kakoune’s scripting philosophy. Yet moving to the shell for conditional control seems annoying when each branch of the conditional comprises many kak commands–which implies one uses long lists of printf '%s;' "...." (unless one uses a here-doc maybe? but a here-doc is another layer of complications). In this case, a little sugary if command might help:

define-command \
-params 3 \
-docstring \
"if <variable> <operator> <value>: test an expression. <operator> can be one of
: (left-anchored regex match), <, <=, =, !=, >=, or > (numerical comparisons)

This will work like an if-then-else conditional when included in the 'try'
part of a try ... catch command" \
if %{ evaluate-commands %sh{
    if ! expr "$variable" "$operator" "$value" >/dev/null; then
        printf fail

Here is small example of use:

define-command \
test-if %{
    set-register a 33
    try %[
        if %reg(a) > 22
        echo 'This is right'
    ] \
    catch %[
        echo 'This is wrong'

which will echo ‘This is right.’

Of course instead of a register one can test any variable, such as a previously set option.

Now the if command is just a wrapper around a shell call to expr with a negated condition to send the fail instruction to Kakoune (and therefore jump to the catch branch of the try command). But I think it looks better, being much more legible. YMMV.

I’m using this If else control flow operator