<?xml version="1.0" encoding="utf-8"?>
<!-- Created by Leo: http://leoeditor.com/leo_toc.html -->
<leo_file xmlns:leo="http://leoeditor.com/namespaces/leo-python-editor/1.1" >
<leo_header file_format="2" tnodes="0" max_tnode_index="0" clone_windows="0"/>
<globals body_outline_ratio="0.5" body_secondary_ratio="0.5">
	<global_window_position top="50" left="50" height="500" width="700"/>
	<global_log_window_position top="0" left="0" height="0" width="0"/>
</globals>
<preferences/>
<find_panel_settings/>
<vnodes>
<v t="vitalije.20180423194734.3"><vh>paste commands</vh></v>
<v t="vitalije.20180422213020.1"><vh>@button copy-json</vh>
<v t="vitalije.20180423164442.1"><vh>common code</vh>
<v t="vitalije.20180423164341.1"><vh>p2list</vh></v>
<v t="vitalije.20180423164345.1"><vh>copyOutline</vh></v>
<v t="vitalije.20180423164355.1"><vh>pasteRetainingClones</vh></v>
<v t="vitalije.20180423182939.1"><vh>pasteNoClones</vh>
<v t="vitalije.20180423164401.1"><vh>clearGnxes</vh></v>
</v>
<v t="vitalije.20180423182237.1"><vh>doPasteOutline</vh>
<v t="vitalije.20180423164350.1"><vh>createOutline</vh></v>
<v t="vitalije.20180423182845.1"><vh>uh</vh></v>
<v t="vitalije.20180423182853.1"><vh>rh</vh></v>
</v>
<v t="vitalije.20180423170632.1"><vh>gnxesInList</vh></v>
<v t="vitalije.20180423171653.1"><vh>undoData</vh></v>
<v t="vitalije.20180423183012.1"><vh>prePasteOutline</vh></v>
</v>
</v>
<v t="vitalije.20180423194853.1"><vh>@button paste-rc</vh>
<v t="vitalije.20180423164442.1"></v>
</v>
<v t="vitalije.20180423194911.1"><vh>@button paste-nc</vh>
<v t="vitalije.20180423164442.1"></v>
</v>
</vnodes>
<tnodes>
<t tx="vitalije.20180422213020.1">@others
copyOutline(p)
g.es('copied')</t>
<t tx="vitalije.20180423164341.1">def p2list(p):
    return [p.h, p.b, p.gnx, p.v.statusBits, p.v.unknownAttributes,
             [p2list(p2) for p2 in p.children()]]</t>
<t tx="vitalije.20180423164345.1">def copyOutline(p):
    g.app.gui.replaceClipboardWith(json.dumps(p2list(p)))</t>
<t tx="vitalije.20180423164350.1">def createOutline(parent_v, aList, gnxDict):
    h, b, gnx, sbits, ua, children = aList
    parent_v._headString = h
    parent_v._bodyString = b
    parent_v.statusBits = sbits
    parent_v.unknownAttributes = ua
    for child_tuple in children:
        h, b, gnx, sbits, ua, grandChildren = child_tuple
        child_v = gnxDict.get(gnx) or leoNodes.VNode(c, gnx)
        del child_v.children[:]
        child_v.parents.append(parent_v)
        parent_v.children.append(child_v)
        createOutline(child_v, child_tuple, gnxDict)</t>
<t tx="vitalije.20180423164355.1" line_numbering_root="4930310a2e">def pasteRetainingClones(c):
    try:
        txt = g.app.gui.getTextFromClipboard()
        ptuple = prePasteOutline(c, txt)
        c, gnxDict, parent_v, childIndex, aList, ubunch, nstack = ptuple
        udata = undoData(*ptuple)
        ubunch.undoType = 'Paste As Clone'
        doPasteOutline(udata, *ptuple)
    except ValueError:
        pass
</t>
<t tx="vitalije.20180423164401.1">def clearGnxes(aList):
    aList[2] = None
    for xl in aList[-1]:
        clearGnxes(xl)

def makeNewNodes(c, aList):
    v = leoNodes.VNode(c)
    aList[2] = v.gnx
    for xl in aList[-1]:
        makeNewNodes(c, xl)</t>
<t tx="vitalije.20180423164442.1">import json
import leo.core.leoNodes as leoNodes
@others</t>
<t tx="vitalije.20180423170632.1">def gnxesInList(aList):
    acc = []
    def _collect(xs):
        acc.append(xs[2])
        for x in xs[-1]:
            _collect(x)
    _collect(aList)
    return acc</t>
<t tx="vitalije.20180423171653.1">def undoData(*ptuple):
    c, gnxDict, parent_v, childIndex, aList, ubunch, nstack = ptuple
    rgnx = c.hiddenRootNode.gnx
    gnxes = gnxesInList(aList)
    gnxes.append(parent_v.gnx)
    def vdata(x):
        v = gnxDict.get(x, c.hiddenRootNode)
        return [v.gnx, v.h, v.b, v.statusBits,
                getattr(v, 'unknownAttributes', None),
                [x.gnx for x in v.children],
                [x.gnx for x in v.parents]]
    vnodes = [vdata(x) for x in gnxes if x in gnxDict or x == rgnx]
    return dict(
        vnodes = vnodes,
        missing = [x for x in gnxes if x not in gnxDict and x != rgnx])
</t>
<t tx="vitalije.20180423182237.1">def doPasteOutline(udata, *ptuple):
    c, gnxDict, parent_v, childIndex, aList, ubunch, nstack = ptuple
    @others
    ubunch.undoHelper = uh
    ubunch.redoHelper = rh
    rh()
    c.undoer.pushBead(ubunch)
    c.redraw()</t>
<t tx="vitalije.20180423182845.1">def uh(*args):
    for gnx, h, b, sbits, ua, chdrn, prnts in udata['vnodes']:
        v = gnxDict.get(gnx, c.hiddenRootNode)
        v._headString = h
        v._bodyString = b
        v.statusBits = sbits
        v.unknownAttributes = ua
        del v.children[:]
        v.children.extend(gnxDict[x] for x in chdrn)
        del v.parents[:]
        v.parents.extend(gnxDict.get(x, c.hiddenRootNode) for x in prnts)
    for x in udata['missing']:
        gnxDict.pop(x, None)
    c.selectPosition(ubunch.p)</t>
<t tx="vitalije.20180423182853.1">def rh(*args):
    gnx = aList[2]
    v1 = gnxDict.get(gnx) or leoNodes.VNode(c, gnx)
    del v1.children[:]
    parent_v.children.insert(childIndex, v1)
    v1.parents.append(parent_v)
    createOutline(v1, aList, gnxDict)
    p1 = leoNodes.Position(v1, childIndex, nstack)
    c.selectPosition(p1)
</t>
<t tx="vitalije.20180423182939.1">def pasteNoClones(c):
    @others
    try:
        txt = g.app.gui.getTextFromClipboard()
        ptuple = prePasteOutline(c, txt)
        c, gnxDict, parent_v, childIndex, aList, ubunch, nstack = ptuple
        ubunch.undoType = 'Paste Node'
        clearGnxes(aList)
        makeNewNodes(c, aList)
        udata = undoData(*ptuple)
        udata['missing'].extend(x[0] for x in udata['vnodes'][:-1])
        del udata['vnodes'][:-1]
        doPasteOutline(udata, *ptuple)
    except ValueError:
        pass</t>
<t tx="vitalije.20180423183012.1">def prePasteOutline(c, txt):
    c.endEditing()
    p = c.p
    if p.hasChildren() and p.isExpanded():
        childIndex = 0
        parent_v = p.v
        nstack = p.firstChild().stack
    else:
        parent_v = p._parentVnode()
        childIndex = p.childIndex() + 1
        nstack = p.stack[:-1]
    aList = json.loads(txt)
    ubunch = c.undoer.createCommonBunch(p)
    ubunch.parent_v = parent_v
    ubunch.childIndex = childIndex
    ubunch.aList = aList
    return (c, c.fileCommands.gnxDict, parent_v,
            childIndex, aList, ubunch, nstack)</t>
<t tx="vitalije.20180423194734.3">copy-json button copies selected node to clipboard in json format

paste-rc button performs paste-retaining-clones
paste-nc button performs paste-outline

both of this scripts make their actions undoable.

</t>
<t tx="vitalije.20180423194853.1" line_numbering_root="4930310a2e">@others
pasteRetainingClones(c)</t>
<t tx="vitalije.20180423194911.1">@others
pasteNoClones(c)</t>
</tnodes>
</leo_file>
