On 1/31/2013 6:22 AM, Edward K. Ream wrote:
On Wed, Jan 30, 2013 at 11:49 AM, Jacob Peck <[email protected]
<mailto:[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.
Thanks :)
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 <http://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.
Good to know. I was really just trying to get the hang of it.
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.
I was trying to avoid an external file for this particular usecase -- I
wanted the .leo to be standalone. But I'll keep this in mind in the future.
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.
This is crazy powerful, just like the rest of Leo. I'll probably end up
doing it this way.
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)
Thanks for the pointers! I was curious how Leo did that, and now I know
where to find the code.
One other way I found of doing it, after I posted my question, seems
very dirty and hacky, but it works. I have a @script node that has the
common code, and using the leoPlugin hooks, adds my common functions to
c, giving me a place to call them from. But this requires scripting to
be enabled, modifications to c, etc.
I'll definitely redo this .leo with the style suggested by point #2.
HTH. Please feel free to ask more questions.
Edward
I do have one last question, since I'm here... aside from LeoPy.leo, is
there any annotated guide to the source? I'd really like to dig in and
explore a bit.
Thanks!
-->Jake
--
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.