All right, then, putting these pieces together:

1. In *myLeoSettings.leo*, in the *@settings/@commands tree*,  add an *@command 
*node that defines and registers the function:

@command reg-y-f1  # Node headline
# Node body:
@g.command('y-f1')  # A very simplified function for expository purposes
def y_f1(x, y):
    g.es(x * y)

When the settings are loaded, this will register the command *def-y-f1 *that 
will define and register your function when it is run.

2. In any outline, when you want to invoke the function *y_f1(2, 3)*:

cmmd = g.global_commands_dict.get('y-f1', None)
if not cmmd:
    c.k.simulateCommand('reg-y-f1')  # Register the function
    cmmd = g.global_commands_dict['y-f1']
cmmd(2,3)
# prints 6

It's a little clunky, but not too bad...  I don't know if it can be made 
simpler, because Leo doesn't support auto-run of commands on loading an 
outline.  I'm sure that's a wise design decision, since auto-run code could 
be a security risk.  I could imagine a plugin that registered and ran all 
your special commands on startup, but that's would be way overkill.
On Monday, October 31, 2022 at 2:43:08 PM UTC-4 Thomas Passin wrote:

> I don't know if Edward would approve, but this also works and seems 
> convenient:
>
> If you execute this:
>
> @g.command('y-f1')
> def y_f1(x, y):
>     g.es(x * y, c)
>
> Then in any outline you can do this:
>
> d = g.global_commands_dict
> d['y-f1'](2,3)
> # Prints 6
>
> On Monday, October 31, 2022 at 1:46:00 PM UTC-4 Thomas Passin wrote:
>
>> Using *@g.CommanderCommand* would probably be better, since once run it 
>> would be available to all outlines.
>>
>> On Monday, October 31, 2022 at 1:05:53 PM UTC-4 Thomas Passin wrote:
>>
>>> On Monday, October 31, 2022 at 11:12:14 AM UTC-4 jkn wrote:
>>>
>>>> Thanks, this looks very interesting...
>>>>
>>>> I have one question - something I have wondered about before. If I have 
>>>> your example code as a button command, say ... then where/how can I put 
>>>> the 
>>>>
>>>> def toggle_app_tab(log, tabname, top_widget):
>>>>     # ...
>>>>
>>>> code, in order for it to accessible by multiple such buttons/commands? 
>>>> I've never been sure about this.
>>>>
>>>> Thanks, J^n
>>>>
>>> [snip]
>>>
>>> For the function to be available to all outlines, I'm sure people have 
>>> various ideas.  One thing I've done in the past (with other functions) is 
>>> to assign it to some object.  I tend to use c, the outline's commander.  If 
>>> Edward ever changes it to use slots, I suppose this wouldn't work any 
>>> more.  Using this technique, your launch command would first look to see if 
>>> it has been assigned to c.  If not, it runs the def and assigns it.  Then 
>>> it would be invoked.  Something like this sketch -
>>>
>>> has_tabbed_app_toggle = hasattr(c, 'toggle_app_tab')
>>> if not has_tabbed_app_toggle:
>>>     def toggle_app_tab(log, tabname, top_widget):
>>>           # Body of function here
>>>     c.toggle_app_tab = toggle_app_tab
>>>
>>> # Invoke function here
>>>
>>> You can even make the function into a method of c.  Other people may do 
>>> that differently, but I think the easiest way is 
>>>
>>> c.has_tabbed_app_toggle = has_tabbed_app_toggle.__get__(c)
>>>
>>> If you did this, you would need to change the function to include *self*, 
>>> and you would need to use *self* instead of *c* in the code.
>>>
>>> There is also a Leo decorator, *g.CommanderCommand*, that you could use 
>>> (it would have to be run first before the function could be used, and your 
>>> function cannot use "g"):
>>>
>>> @g.CommanderCommand('z-f1')
>>> def z_f1(self, x, y):
>>>     print(x * y)
>>>
>>> Then:
>>> c.z_f1(2, 3)
>>> # prints 6
>>>
>>> (It seems to me that there was something recently about not using this 
>>> decorator, but I can't remember for sure).
>>>
>>

-- 
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/d2cfbb96-3342-4876-a6a2-69adc426e681n%40googlegroups.com.

Reply via email to