Time ordering with on-key

I have been mulling over the temporal ordering of commands with on-key. For example:

define-command test-on-key-bad %{
    on-key %{
        echo one
    }
    on-key %{
        echo two
    }
    echo three
}

I would expect to see “one, two, three” on the echo line (in this order, after pressing one key, then another one – except, of course, that “two” would be quickly covered by “three”). What would you expect?

NB: the only way I could get the order right was to nest the on-keys:

define-command test-on-key-good %{
    on-key %{
        echo one
        on-key %{
            echo two
        }
    }
}

In this second version I have removed echo three for clarity. Note that if echo three were a command doing some serious work and depending on some option setting done by the “previous” on-keys … then the command would fail. :cry:

Apart from evaluate-commands and try, all the commands that execute other commands execute them eventually, or perhaps not at all (if the user cancels the operation). That’s obviously true for define-command and hook and provide-module, but it’s also true for on-key and prompt and menu.

The way it was explained to me was: on-key sets up a temporary callback so that the next time the client sends a key event, the callback triggers. In your first example, you execute two on-key commands in a row, which just queues up two callbacks to be triggered later, then execute an echo command which executes immediately because it isn’t conditional on any kind of callback.

1 Like