clone one node as a child of current position

38 views
Skip to first unread message

Xu Wang

unread,
Feb 25, 2020, 1:42:51 PM2/25/20
to leo-e...@googlegroups.com
Dear Leo Developer,

If I know the GNX of one node, how shall I clone it to the position under current node(as a child of current position)?

BR,Austin



Edward K. Ream

unread,
Feb 25, 2020, 3:12:14 PM2/25/20
to leo-editor
​On Tue, Feb 25, 2020 at 12:42 PM Xu Wang <xu4...@gmail.com> wrote:
Dear Leo Developer,

If I know the GNX of one node, how shall I clone it to the position under current node(as a child of current position)?

​Good question.

gnx's belong to vnodes, not positions. Your question is equivalent to asking, given a vnodes, how do I create the desired clone.

Most of the time it will be easier to use positions rather than vnodes. For example, here is tested code that, given a position, moves a clone of that position to the last node of c.p:

# Get the position of a node whose headline is 'Target Node'
child_p
= g.findNodeAnywhere(c, 'Target Node')
assert child_p
# Clone the node.
clone
= child_p.clone()
# Move the node
p
= c.p
n
= p.numberOfChildren()
clone
.moveToNthChildOf(p, n)
# Make all children visible and redraw.
p
.expand()
c
.redraw()

If all you have is a gnx, then you can search for any position whose vnode has that gnx. Here is tested code.

child_p = None
for p in c.all_positions():
   
if p.gnx == 'ekr.20200225134818.1': # The gnx of my target node.
        child_p
= p
       
break
assert child_p
clone
= child_p.clone()

And then you can proceed as before:

p = c.p  # Don't use the p in the search loop!
n
= p.numberOfChildren()
clone
.moveToNthChildOf(p, n)
p
.expand()
c
.redraw()

HTH.

Edward

P.S. If performance is a big consideration, you might be able to use one of Leo's dictionaries that associates vnodes with gnx's. But I'll leave that complication for (much) later :-)

EKR

Xu Wang

unread,
Feb 27, 2020, 9:01:15 PM2/27/20
to leo-e...@googlegroups.com
Thanks for the detailed demo.. it's very helpful for me to getting familiar with Leo scripting.

Edward K. Ream <edre...@gmail.com> 于2020年2月26日周三 上午3:12写道:
--
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/5a1e06e9-7fa1-4102-b810-dfe0aa01a1d4%40googlegroups.com.

Edward K. Ream

unread,
Feb 28, 2020, 6:19:33 AM2/28/20
to leo-editor
On Thu, Feb 27, 2020 at 8:01 PM Xu Wang wrote:

Thanks for the detailed demo.. it's very helpful for me to getting familiar with Leo scripting.

You're welcome. Glad the example helped.

Edward
Reply all
Reply to author
Forward
0 new messages