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

Edward K. Ream <[email protected]> 于2020年2月26日周三 上午3:12写道:

> On Tue, Feb 25, 2020 at 12:42 PM Xu Wang <[email protected]> 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
>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/leo-editor/5a1e06e9-7fa1-4102-b810-dfe0aa01a1d4%40googlegroups.com
> <https://groups.google.com/d/msgid/leo-editor/5a1e06e9-7fa1-4102-b810-dfe0aa01a1d4%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/CAEpKVv83t0pdYR7qn%2BwM%2BYmg_drfoGTYJ%3DVXQwUe4M9ic_%3Dd7g%40mail.gmail.com.

Reply via email to