> Hi,
>
> I am new to Leo editor and find it very interesting. I have been
> using it for writing my slides (presentations) in Beamer/LaTeX. Its
> structure editing is very useful.
>
> I would like to create a few buttons to insert template LaTeX code,
> for example code for a new frame (i.e. slide), code for columns, etc.
> However, I don't know how to insert text at the current cursor
> position in the body pane?
Below is some code for closing XML tags in the body pane - if you can
read around the XML stuff it manipulates the body text along the lines
you're asking. I recently suggested this part of the API be cleaned
up, as it can add significant power from a text editor / macroish point
of view.
Actually my example might not completely cover what you're after, but
you can introspect the opjects it's using for other methods.
> Also, will the function g.app.gui.runAskOkCancelStringDialog() returns
> None if the user clicked Cancel? Or will it return an empty string?
> How about runAskOkCancelNumberDialog()?
Sometime's it easiest just to read the code, by opening
the .../leo/core/LeoPyRef.leo file
Cheers -Terry
from xml import sax
class Stacky(sax.handler.ContentHandler):
def __init__(self): self.stack = []
def startElement(self, name, attrs):
self.stack.append(name)
def endElement(self, name):
self.stack.pop()
stacky = Stacky()
txt = p.bodyString()
w = c.frame.body.bodyCtrl
pnt = w.getInsertPoint()
xml = txt[:pnt]
try:
sax.parseString(str(xml), stacky)
except sax.SAXParseException, descrip:
if 'no element found' not in str(descrip):
g.es("Error in context '%s'" % '->'.join(stacky.stack))
raise
if stacky.stack:
etag = '</%s>' % stacky.stack[-1]
c.setBodyString(p, xml + etag + txt[pnt:])
# insert point moves to end of buffer... so move it back
w.setInsertPoint(pnt+len(etag)) # unicode safe?
else:
raise
> Also, will the function g.app.gui.runAskOkCancelStringDialog() returns
> None if the user clicked Cancel? Or will it return an empty string?
> How about runAskOkCancelNumberDialog()?
>
> It'd be great if someone can help me with those questions. I know
> Python, however I could not find those details in Leo's docs.
Not everything is in the docs. That's just the way it is.
Since you know Python, it is best to open leoPy.leo (or LeoPyRef.leo)
and search for "def runAskOkCancelStringDialog"
You will find the actual code in the node:
Code-->Qt gui-->@file ../plugins/qtGui.py-->Gui wrapper-->class
leoQtGui-->Dialogs & panels (qtGui)-->runAskOkCancelStringDialog
You can see that None sometimes is returned. Or you could simply run
the code :-) Just put the following in a node and do Ctrl-B
(execute-script)
g.es(g.app.gui.runAskOkCancelStringDialog(None,'my title','my message'))
I don't mind answering these questions, but it will be good for you to
become comfortable with answering them for yourself.
Edward