On Wed, Jan 30, 2013 at 11:49 AM, Jacob Peck <[email protected]> wrote:

> Hello all,
>
> I'm extremely new to Leo (just picked it up yesterday, actually, but I'm
> already hooked!)
>

Glad you like it.  Welcome aboard.

>
> I'm trying to figure out how to make a @button that references another
> script in Leo.  As in, would like to do something like the following:
>
> In one script, << foo_function >>
>
> def foo(n):
>     g.es("foo! %s" % n)
>
> And in some @buttons, like @button foo1
>
> << foo_function >>
> foo(1)
>

> But I can't for the life of me make this work... I'm assuming that's
> because << ... >> only has meaning with respect to a @thin/@file node.
>

Correct.

First, I would like to advise against using section references unless you
really must, for example when using xml or html.  Imo, using sections is
bad style in python, except for hiding imports.

There are at least three ways to have scripts share code, with or without
section references:

1. Import a helper file containing common functions/classes.  This will be
best, imo, when shared code doesn't change much.

2. Use exec to "inject" shared code into the namespace of the script being
executed.   This is a cute trick, but it doesn't seem to be in the faq.
There are example of this trick in unitTest.leo:  search for "@common".

Do the following:

A. Put the common code somewhere, say in a node called "@common code".
Note that the node can be called anything: I use @common to draw they eye,
but it's just my convention.

B. Start each script using the common code with::

    exec(g.findTestScript(c,'@common code'))

g.findTestScript returns the *expanded* code of the entire subtree whose
head is the node "common code".  Thus, you can use @others and section
references in the "common code" subtree! It's cute.

3. @button and @command nodes create Leo commands.  The names of the
commands are "munged" to make typing completion work more smoothly on
them.  The (possibly truncated) munged command name appears in the button.
You can use typing completion to see the full name.

Execute the commands by name using c.k.simulateCommand(mungedCommandName)

This should be enough to get you started.  Writing interactive commands,
such as Leo's find/change commands, is an advanced topic.  For that you
will need helper code in leoKeys.py, especially k.getArg.  See the code in
LeoPy.leo::

    @file leoEditCommands.py-->Search classes-->class minibufferFind (the
findHandler)

HTH.  Please feel free to ask more questions.

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 post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/leo-editor?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to