It sounds more complex than necessary to me. I'm not sure what kind of
functions you have in mind. But there are several ways to attach functions
globally. One way is that g has a user dict, whose name I forget right
now. Let's call it g.userdict, though that's probably not its real name.
Then you can write a script the assigns
def func1():
# body of function
g.userdict['func1') = func1
# etc
Or you can just assign functions to g itself:
g.func1 = func1
For example, my mind map commands apply a monkey patch to c. The code that
constructs a mind map checks to see if the monkey patch has been applied
and if not it applies it. This look like:
# Check if we have not been run before or if the rendering
# device has been changed
_render_dev = c.config.getString('mmap_render_dev')
if not hasattr(c, 'render_dev') or c.render_dev != _render_dev:
# Install our patch
c.executeMinibufferCommand('mmap-monkeypatch')
c.render_dev = _render_dev
c.build_mmap(data)
In the monkey patch subtree:
"""This command adds a function to the outline commander c that is
used by the top-level mind mapping commands. This function
generates the svg output for a mindmap and renders it in
the specified rendering pane or program. The rendering
"device" is specified by the setting "mmap_render_dev".
"""
@command mmap-monkeypatch
# ...
def build_mmap(data):
"""Parse data and build mindmap."""
data = data.replace('&', '+').replace('<', ' ')
svg = create_map(IndentedParser, data)
find_or_create_target(c, TARGET_NODE, svg)
render_with_device(svg)
# Let others know body has changed.
editor = c.frame.body.wrapper.widget
doc = editor.document()
doc.setModified(True)
c.build_mmap = build_mmap
You can put all these function definitions in a node or nodes and install
them by running the node with CTRL-b. Or if there are so many that you
want to put them in a module, you can import them from the module, but it
doesn't have to be a plugin. You could put it into, for example,
~/.leo/functions. You can add that location to the Python system path,
then import your function definition module from there.
So there are many possibilities for adding your own functions and command
that can be stored right in subtrees in myLeoSettings.leo, or in a module,
without even needing a plugin. Or a plugin could use the same techniques
to install your functions. If you want to write them as Leo minibuffer
commands, you can create menu items for them, so that you can access them
from Leo's menubar (you do that in myLeoSettings.leo, or in particular
outlines).
Just look for the simplest thing that could work, and go on from there. I
have all the code to run my browser bookmarks system right in a subtree in
the outline that stores the bookmarks themselves. I prototyped the code to
highlight the current line, and the code to draw a right margin line, in
subtrees in the workbook. Here's how the highlighting code gets installed
(in the prototype; the highlighter code that Leo actually uses was derived
from this but is part of the core code so it doesn't need to be installed
each time Leo is run):
class Hiliter:
def __init__(self, cc):
self.c = cc
w = cc.frame.body.wrapper
self.editor = w.widget
# .....
# Install the highlighter in all commanders
for cc in g.app.commanders():
cc.hiliter = Hiliter(cc)
cc.hiliter.editor.cursorPositionChanged.connect(cc.hiliter.highlightCurrentLine)
Presto! All outlines now have current line highlighting installed.
--
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/b66beba6-bfa0-4865-91ba-afb6739d6af4n%40googlegroups.com.