Re: When scripting with Leo, how to call a command by name?

2023-09-04 Thread Edward K. Ream
On Monday, September 4, 2023 at 5:01:51 AM UTC-5 Edward K. Ream wrote:

On Sun, Sep 3, 2023 at 7:41 PM Félix wrote:

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

...

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?

 
PR #3541  has been 
merged into devel.

k.simulateCommand is deprecated, but will remain for compatibility.

The PR changes k.simulateCommand to c.doCommandByName everywhere, including 
in plugins and unit tests.

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 leo-editor+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/84e5ace2-c61f-433e-9d30-b653b33a18b8n%40googlegroups.com.


Re: Quick Survey: When running script in leojs, where should the 'vscode' object be accessible?

2023-09-04 Thread Edward K. Ream
On Sun, Sep 3, 2023 at 8:33 PM Félix  wrote:

I thought I'd share this :)
>
> Just a small example script that is possible to run in leojs, (with also
> having the vscode API available in g.app.vscode)
>
> Incredibly, when executing the script, the global objects you'd expect to
> see in javascript ,(console, fetch, ect...) are available, as shown below!


That's very cool. And being able to run Leo from the browser is beyond cool
:-)

Hoping to release a beta before the autumn starts!
>

That's "approximately" Sept. 22, according to NOAA, hehe.  I can barely
contain my excitement.

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 leo-editor+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/CAMF8tS1nG4%3D0ZA_jx_XYRNJ%3Dm1up9yj9U1ZfoT9eEC2dZ0%2BG4A%40mail.gmail.com.


Re: When scripting with Leo, how to call a command by name?

2023-09-04 Thread Edward K. Ream
On Sun, Sep 3, 2023 at 7:41 PM Félix  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 leo-editor+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/CAMF8tS3MdyH0aQSAwwQHmCKCxtF_WQUhk14jkoDTL4Jx%3DCHerg%40mail.gmail.com.


Re: When scripting with Leo, how to call a command by name?

2023-09-04 Thread jkn
FWIW the current options for doing this have always seemed a bit 
sub-optimal to me, due to the reasons you suggest. 
c.executeMinibufferCommand('name-of-command') seem like the wrong level to 
me, I should be able to call something 'lower down' than this...


On Monday, September 4, 2023 at 4:34:01 AM UTC+1 Félix wrote:

> I agree, respecting existing parameter orders and signatures in general is 
> very important. But making it optional does not interfere with normal 
> usage. 
>
> On Sunday, September 3, 2023 at 11:06:54 PM UTC-4 tbp1...@gmail.com wrote:
>
>> I've never encountered or used that method.  But it's another case where 
>> I would resist changing the signature of an existing command.  If it's only 
>> a matter of making and argument optional, that would be more palatable.  
>> The "events" in question here are not Python things but Leo objects.  They 
>> often carry the "c" parameter, for example, so the command can access it.
>>
>> On Sunday, September 3, 2023 at 9:24:35 PM UTC-4 Félix wrote:
>>
>>> Having in mind a fresh new user's perspective, I wonder if 
>>> *doCommandByName*, *the method with the most intuitive name to use for 
>>> such a task to perform*, could not be relatively easily modified to 
>>> support not having an 'event' passed to it? 
>>>
>>> ...I'm not familiar with those 'events' concepts in python so I'm 
>>> curious about Edwards thought on this matter. 
>>>
>>> Hoping it can be changed easily ! :)
>>>
>>> Félix
>>>
>>> On Sunday, September 3, 2023 at 9:13:39 PM UTC-4 tbp1...@gmail.com 
>>> wrote:
>>>
 There's also c.k.simulateCommand('name-of-command').  I'm not sure why 
 there are both, since they seem to do the same thing.  With either one, 
 you 
 don't need to supply a fake event.  The method takes care of that. I use 
 whichever one I remember first.

 On Sunday, September 3, 2023 at 9:08:31 PM UTC-4 gates...@gmail.com 
 wrote:

> I tend to use c.executeMinibufferCommand('name-of-command') -- doesn't 
> need any extra parameters, and Just Works TM.
>
> Jake
>
> On Sun, Sep 3, 2023 at 8:41 PM Félix  wrote:
>
>> Making script in Leo is great, with the globally defined vars g, c 
>> and p anything is possible. 
>>
>> But what is the recommended way of doing a simple command by name in 
>> a script?
>>
>> The *c.doCommandByName* method exists, but it insists on having an 
>> event as a second parameter. 
>>
>> 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?
>>
>> Félix
>>
>> -- 
>> 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 leo-editor+...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/leo-editor/68b44f92-c2fd-403b-97aa-58fba041d366n%40googlegroups.com
>>  
>> 
>> .
>>
>

-- 
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 leo-editor+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/1583b47e-000f-45e8-ac4f-0c0e085fb09en%40googlegroups.com.