Insert text at cursor position

111 views
Skip to first unread message

Truong Nghiem

unread,
Aug 24, 2011, 10:16:36 AM8/24/11
to leo-editor
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?

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.

Thanks.
Truong.

Terry Brown

unread,
Aug 24, 2011, 11:09:50 AM8/24/11
to leo-e...@googlegroups.com
On Wed, 24 Aug 2011 07:16:36 -0700 (PDT)
Truong Nghiem <truong...@gmail.com> wrote:

> 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

Truong Nghiem

unread,
Aug 24, 2011, 11:44:55 PM8/24/11
to leo-editor
I will answer myself.

> 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?

s = "The string to insert"
w = c.frame.body.bodyCtrl
ip = w.getInsertPoint()
w.insert(ip, s)
w.setInsertPoint(ip + len(s)) # If you want to move the cursor to the
end of the new string


> 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()?

Returns None if Cancel was clicked.

Truong.

Edward K. Ream

unread,
Sep 7, 2011, 7:04:21 AM9/7/11
to leo-e...@googlegroups.com
On Wed, Aug 24, 2011 at 9:16 AM, Truong Nghiem <truong...@gmail.com> wrote:

> 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

Reply all
Reply to author
Forward
0 new messages