PR
#2655 shows how to copy an outline iteratively. Just use p.moveToThreadNext as a template! Like this:
p = c.rootPosition()
for p2 in c2.all_positions():
p.h = p2.h
p.b = p2.b
p.u = p2.u
if not p2.hasThreadNext():
break
# Like p.moveToThreadNext.
if p2.hasChildren():
p = p.insertAsLastChild()
elif p2.hasNext():
p = p.insertAfter()
else:
while p2:
p2 = p2.parent()
p = p.parent()
if p2.hasNext():
p = p.insertAfter()
break
Unlike all my recursive attempts, this iterative solution effortlessly inserts new nodes.
Summary
It was only by chance that I stumbled across this iterative solution. This discovery was shocking, disorienting, and upsetting. How could I have missed this solution all these years?
I'll soon submit a PR for a slightly more general utility method.
Edward
P. S. For reference, here is p.
moveToThreadNext:
def moveToThreadNext(self) -> "Position":
"""Move a position to threadNext position."""
p = self
if p.v:
if p.v.children:
p.moveToFirstChild()
elif p.hasNext():
p.moveToNext()
else:
p.moveToParent()
while p:
if p.hasNext():
p.moveToNext()
break #found
p.moveToParent()
# not found.
return p
EKR