Creating Child Node (from Selected Text)

110 views
Skip to first unread message

Conor White-Sullivan

unread,
Jun 3, 2016, 9:45:17 PM6/3/16
to leo-editor
Hey all,

I have a couple scripts I'd like to write -- I think they're pretty simple, but I'm new to python and leo

If anyone can help I'd appreciate it 

Script 1 : Creating Children of current based on selected text

Goal:   I'd like to 

1.  get the value of the text that is currently selected

2.  create a new node, which has that text as both its headline and body
      It was unclear from the tutorial how to just create a new node with any arbitrary content

3.   set that node be a child of the currently selected node
      requires:  getting the position of the current node


Bonus:

1.  Trigger a little popup that lets me put in the value of what I'd like to name this new child

2.   remove the selected text and replace it with the value << 'whatever I named the new child node in the prompt' >>



--------  I've used the script below to create a clone and move it to a specific place -- how do I just make a node



p = c.p.clone()
dest = g.findNodeAnywhere(c,'A
    ')
p.moveToLastChildOf(dest)
c.redraw()

----- Also, what is the best way to discover how to do these kind of things without asking others?

Conor White-Sullivan

unread,
Jun 3, 2016, 10:07:28 PM6/3/16
to leo-editor
I can now move the current node somewhere by doing this

newnode = c.p.copy()
dest = g.findNodeAnywhere(c,'A')
newnode.moveToLastChildOf(dest)
c.redraw()

but what I want is to create a new node with the same values -- I don't think p is the answer for me here

Conor White-Sullivan

unread,
Jun 3, 2016, 11:25:04 PM6/3/16
to leo-editor

On Saturday, June 4, 2016 at 7:37:28 AM UTC+5:30, Conor White-Sullivan wrote:
I can now move the current node somewhere by doing this

newnode = c.p.copy()
dest = g.findNodeAnywhere(c,'A')
newnode.moveToLastChildOf(dest)
c.redraw()

but what I want is to create a new node with the same values -- I don't think p is the answer for me here


On Saturday, June 4, 2016 at 7:15:17 AM UTC+5:30, Conor White-Sullivan wrote:
Hey all,

I have a couple scripts I'd like to write -- I think they're pretty simple, but I'm new to python and leo

If anyone can help I'd appreciate it 

Script 1 : Creating Children of current based on selected text

Goal:   I'd like to 

1.  get the value of the text that is currently selected

2.  create a new node, which has that text as both its headline and body
      It was unclear from the tutorial how to just create a new node with any arbitrary content

To create a node with arbitrary content, it seems the best thing to do is to use the MinibufferCommand

mb = c.executeMinibufferCommand
cp = c.currentPosition
t = "Here is some text to insert"
mb('insert-child')
cp().h = t
cp().b = t

Edward K. Ream

unread,
Jun 5, 2016, 7:29:36 AM6/5/16
to leo-editor
On Fri, Jun 3, 2016 at 8:45 PM, Conor White-Sullivan <cwhites...@gmail.com> wrote:


I have a couple scripts I'd like to write -- I think they're pretty simple, but I'm new to python and leo

If anyone can help I'd appreciate it 

​First, all of this, except perhaps the bonus, should be in Leo's scripting chapter.​
 
​It would be good to review this.​

Script 1 : Creating Children of current based on selected text

Goal:   I'd like to 

1.  get the value of the text that is currently selected

​c.frame.body.wrapper is the interface to Leo's body pane.

The following (tested) code prints the the selected text:

w = c.frame.body.wrapper
s = w.getSelectedText()
print(s)

2.  create a new node, which has that text as both its headline and body
      It was unclear from the tutorial how to just create a new node with any arbitrary content

​c.p is the presently selected position.​

To create a new node as the last child of c.p:

p = c.p.insertAsLastChild()

To see the change, do:

c.redraw()

To set headline and body text of the new node:

p.h = 'headline'
p.b = 'body text'
 
Bonus:

1.  Trigger a little popup that lets me put in the value of what I'd like to name this new child

​Look at the methods in leoPy.leo:

Code-->Qt gui-->@file ../plugins/qt_gui.py-->class LeoQtGui(leoGui.LeoGui)-->LeoQtGui.Dialogs & panels​
 

​Access them with g.app.gui, for instance,
    g.app.gui.​runAskOkCancelStringDialog(...)

2.   remove the selected text and replace it with the value << 'whatever I named the new child node in the prompt' >>

​I'll leave this as an exercise :-) Hints:

1. With w set as above, you can access all the methods defined here:

Code-->Qt gui-->@file ../plugins/qt_text.py-->class QTextEditWrapper(QTextMixin)

2. You get the selection range with w.getSelectionRange(), get the original text with s = w.getAllText() and then replace the text with w.setAllText().

3. You can compute the final text using python's string slices.

This is a good beginning Python exercise.  Ask for help here if you get lost.

----- Also, what is the best way to discover how to do these kind of things without asking others?

​After you have read Leo's scripting chapter, you can look at Leo's own source code in leoPy.leo (leoPyRef.leo).

Edward

Brian Theado

unread,
Jun 18, 2016, 11:11:57 PM6/18/16
to leo-editor


On Fri, Jun 3, 2016 at 9:45 PM, Conor White-Sullivan <cwhites...@gmail.com> wrote:
[...] 
Script 1 : Creating Children of current based on selected text

Goal:   I'd like to 

1.  get the value of the text that is currently selected

2.  create a new node, which has that text as both its headline and body
      It was unclear from the tutorial how to just create a new node with any arbitrary content

3.   set that node be a child of the currently selected node
      requires:  getting the position of the current node

Have you seen the menu pick Edit->Edit Body->Extract? It uses the first selected line for the headline of the child and the remaining selected lines as the body. You could track down that code and learn from it.
 

Bonus:

1.  Trigger a little popup that lets me put in the value of what I'd like to name this new child

2.   remove the selected text and replace it with the value << 'whatever I named the new child node in the prompt' >>

For #2, there is a menu pick Edit->Edit Body->Extract Names. If you have  <<whatever title>> as the first line of the selected text, then it will use that as the title and also leave that line in the original body. The child will have <<whatever title>> with the other lines of the selection as the body.

Brian
Reply all
Reply to author
Forward
0 new messages