--
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/fced3afd-898a-493c-8b31-c3e5dbff7adb%40googlegroups.com.
three._childIndex += 1
import time
import leo.core.leoNodes as leoNodes
def findFtlistAncestor(p):
p = p.copy()
while p and not p.h.startswith('@ftlist'):
p = p.parent()
return p
def moveToTopAndCloneToAtDay(c, p):
"""
Move the node identified by position p to the first child of
an ancestor @ftlist node and also clone it to a sibling @day
node with date matching today's date
"""
ftlist = findFtlistAncestor(p)
if not ftlist:
g.es("Not in ftlist tree")
return {}
vft = ftlist.v
before = make_snapshot(vft)
v = p.v
if vft in v.parents:
i = vft.children.index(v)
del vft.children[i]
vft.children.insert(0, v)
else:
vft.children.insert(0, v)
v.parents.append(vft)
today = "@day " + time.strftime("%Y-%m-%d", time.gmtime())
for i, ch in enumerate(vft.children):
if ch.h == today:
break
else:
i = len(vft.children)
ch = leoNodes.vnode(c)
ch.h = today
vft.children.append(ch)
ch.parents.append(vft)
if ch in v.parents:
i = ch.children.index(v)
del ch.children[i]
ch.children.insert(0, v)
else:
ch.children.insert(0, v)
v.parents.append(ch)
return ftlist, before, make_snapshot(vft)
def from_snapshot(data):
for v, children, parents in data:
v.children[:] = children
v.parents[:] = parents
def make_snapshot(v0):
def it(v):
yield v, tuple(v.children), tuple(v.parents)
# if you need to keep track of headlines and boides too
# yield v, tuple(v.children), tuple(v.parents), v.h, v.b
for ch in v.children:
yield from it(ch)
return tuple(it(v0))
def do_change():
undo_data = c.undoer.createCommonBunch(p)
undo_data.kind = 'my-special-operation'
undo_data.undoType = 'my-special-operation'
ftlist, before, after = moveToTopAndCloneToAtDay(c, p)
undo_data.before = before
undo_data.after = after
undo_data.ftlist = ftlist
undo_data.undoHelper = lambda:from_snapshot(undo_data.before) or c.redraw(undo_data.p)
undo_data.redoHelper = lambda:from_snapshot(undo_data.after) or c.redraw(undo_data.ftlist.firstChild())
c.undoer.pushBead(undo_data)
c.redraw(ftlist.firstChild())
do_change()
--
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/20942315-145c-4dff-83ea-d0b6a1a39afe%40googlegroups.com.
I'm finding the vnode code harder to read, but if it works better then it is better :-). However, is it better because you are the one who wrote it instead of me :-), or because it is easier to avoid mistakes using vnodes?
I'll take a close look at it and add the insert functionality I also had in my code. Maybe that way I will if/how out how vnodes help avoid the mistakes.About this comment:# if you need to keep track of headlines and boides too
# yield v, tuple(v.children), tuple(v.parents), v.h, v.b
def from_snapshot(data):
for v, children, parents, h, b in data:
v.children[:] = children
v.parents[:] =
parents
v.h = h
v.b = b
def make_snapshot(v0):
def it(v):
yield v, tuple(v.children), tuple(v.parents), v.h, v.b
for ch in v.children:
yield from it(ch)
return tuple(it(v0))
Well I didn't tried too hard to make it more readable. It can be improved.
But changing tree using vnodes has several benefits. First of all, vnodes are stable (immune to tree changes) - positions are not.
The second they work much faster because every change in the tree performed through positions are followed by a redraw and a lot of code that doesn't need to be executed until the complete tree change is done. You don't usually want to see intermediate versions of tree. You want to see the finished tree. When making changes using vnodes, nothing is redrawn until you explicitly call `c.redraw()`.
The function make_snapshot creates a snapshot of data needed to recreate subtree structure. In the variant where no changes to headlines nor bodies are made, this function takes enough data to undo the changes. However if you need to keep record of headlines and bodies too, then you should add them too to snapshot. Of course in that case you must change the function from_snapshot too.
This two functions can store and recreate the exact copy of the given vnode. If you want to make snapshot of the whole outline, pass the c.hiddenRootNode as the argument to the make_snapshot function. However, if you are certain that the only changes made are under one vnode (for example @ftlist node), then it is more efficient to store only subtree of @ftlist.