On Sun, Sep 3, 2023 at 7:41 PM Félix <[email protected]> wrote:

The *c.doCommandByName* method exists, but it insists on having an event as
> a second parameter.


Hah. I had completely forgotten about c.doCommandByName. Furthermore, I had
completely mis-remembered the complexity of k.simulateCommand. Maybe
somebody (me?) refactored simulateCommand.

I discovered that I can make it work by passing a fake event such as :
> {"c": c}, or even a better one: g.app.gui.create_key_event(c),  but this is
> quite unintuitive. Could it not default to a valid default event if the
> event is not passed?
>

Imo, the answer to your question is obvious: Create the default event in
c.doCommandByName instead of k.sumulateCommand:

1. Define k.simulateCommand as:

def simulateCommand(self, commandName: str, event: Event = None) -> None:
    """Execute a Leo command by name."""
    c = self.c
    c.doCommandByName(commandName, event)

2. Change the signature of c.doCommandByName to:

def doCommandByName(self, command_name: Any, event: Event = None) -> Any:

As you say, the new signature is in no way a breaking change.

3. At the start of c.doCommandByName, create an event if necessary:

    if not event:
        # Create a default key event.
        event = g.app.gui.create_key_event(c)

4. Update Leo's docs, especially the cheat sheet to mention
c.doCommandByName instead of k.simulateCommand.

Obviously, k.simulateCommand must remain for compatibility.

5. Update Leo's unit tests to use c.doCommandByName instead of
k.simulateCommand.

I'll create a PR immediately.

Edward

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/CAMF8tS3MdyH0aQSAwwQHmCKCxtF_WQUhk14jkoDTL4Jx%3DCHerg%40mail.gmail.com.

Reply via email to