Help with a Latex line ender script

56 views
Skip to first unread message

Israel Hands

unread,
Nov 3, 2022, 1:11:05 PM11/3/22
to leo-editor
I'd like to make a script that adds Latex end of line characters  \\ to the end of each line in a block of body text that I have highlighted in Leo. I know this probably isn't complicated, but then neither am I. 

Thanks

IH

Luka

unread,
Nov 4, 2022, 4:25:47 AM11/4/22
to leo-e...@googlegroups.com
Dear Israel,

There are plenty of ways to do it in LEO but this is how I do it.

Create a @button node with script, select text in body, and run:

@language python

#get selected text from body
w = c.frame.body.wrapper
s = w.getSelectedText()
start,end=w.getSelectionRange()
b=p.b

#do something
s=s.replace('\n','\\\\\n')

#insert edited text back
p.b=b[:start]+s+b[end:]
c.redraw()

____________________
With Best Regards,
Luka


чт, 3 нояб. 2022 г. в 20:11, Israel Hands <alis...@mcgh.ee>:
I'd like to make a script that adds Latex end of line characters  \\ to the end of each line in a block of body text that I have highlighted in Leo. I know this probably isn't complicated, but then neither am I. 

Thanks

IH

--
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 leo-editor+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/leo-editor/2fab05c1-21f9-48be-a402-90af2b21b638n%40googlegroups.com.

Edward K. Ream

unread,
Nov 4, 2022, 6:48:50 AM11/4/22
to leo-e...@googlegroups.com
On Fri, Nov 4, 2022 at 3:25 AM Luka <luka.go...@gmail.com> wrote:

There are plenty of ways to do it in LEO but this is how I do it.

Create a @button node with script, select text in body, and run:

@language python

#get selected text from body
w = c.frame.body.wrapper
s = w.getSelectedText()
start,end=w.getSelectionRange()
b=p.b

#do something
s=s.replace('\n','\\\\\n')

#insert edited text back
p.b=b[:start]+s+b[end:]
c.redraw()

Thanks, Luka, for this tip.

Leo itself typically expands the selection to include entire lines. Like this (tested) code:

w = c.frame.body.wrapper
s = w.getAllText()
i, j = w.getSelectionRange()
start, junk = g.getLine(s, i)
junk, end = g.getLine(s, j)

Edward

Thomas Passin

unread,
Nov 4, 2022, 8:01:10 AM11/4/22
to leo-editor
Don't forget to set up for undo.

Israel Hands

unread,
Nov 4, 2022, 8:17:01 AM11/4/22
to leo-editor
Many thanks for such prompt and helpful advice. A significant part of what makes Leo so great. I'm off to try Luka's and Edward's solutions. Sorry to be so slow tbp1, but how do I set up undo?

Thanks again from momentarily sunny Wales.

IH

Thomas Passin

unread,
Nov 4, 2022, 8:38:55 AM11/4/22
to leo-editor
No need to apologize!  Leo can do so many things that it's hard to find information, or even know there is something to look for.  I looked in LeoDocs.leo (you can open it from the Help menu).   Search for _Undoably changing body text_ (I originally just searched for _undo_).  It says

To undo a single change to body text::

    command = 'my-command-name'
    b = c.undoer.beforeChangeNodeContents(p, oldYScroll=ypos)
    # Change p's body text.
    c.undoer.afterChangeNodeContents(p,
        command=command, bunch=b, dirtyVnodeList=[])


Someone else can correct me, but I don't think that the exact value of the string command matters.  Your script will probably have a command name, though, so might as well use it.

Luka

unread,
Nov 4, 2022, 10:07:44 AM11/4/22
to leo-e...@googlegroups.com
Edward, Thomas, thanks for advice! 

Putting it all together, I think it could be a nice beginner's template to do something with the selected text in the body. I also use the power of regex for complex text-editing related tasks.

command = 'add_line_end'

# Undo begin
b = c.undoer.beforeChangeNodeContents(p)

#get selected text from body
w = c.frame.body.wrapper
s = w.getAllText()
i, j = w.getSelectionRange()

# to get entire lines:
start, junk = g.getLine(s, i)
junk, end = g.getLine(s, j)

# or just selected text:
# start, end = i,j

txt=s[start:end]

#do something
txt=txt.replace('\n','\\\\\n')

#insert edited text back
p.b=p.b[:start]+txt+p.b[end:]
c.redraw()

# Undo End
c.undoer.afterChangeNodeContents(p,
    command=command, bunch=b)


пт, 4 нояб. 2022 г. в 15:38, Thomas Passin <tbp1...@gmail.com>:
--
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 leo-editor+...@googlegroups.com.

Israel Hands

unread,
Nov 6, 2022, 2:04:02 PM11/6/22
to leo-editor
Hi Luka.

Thanks for wrapping this up in one solution and the prompt to explore this area further!

ta

IH

Reply all
Reply to author
Forward
0 new messages