#@+leo-ver=4-thin
#@+node:ekr.20051027111215:@thin buttons.txt
#@+all
#@+node:ekr.20060531093331:@command clones-tab
@
Ever have a clone that is difficult to understand outside the context of its
original parent? Here's some code to help. It displays the headline of the
current node plus the headlines of all the parents of all the clones of the
current node. Selecting a displayed parent headline moves the current node to
the corresponding clone in the outline.

The idea is to be able to quickly see the context of all the clones of the
current node and to be able to easily navigate from one clone instance to the
next.
@c

@others
c.cn = cloneNavigator(c)
c.cn.displayClones(c)
#@+node:ekr.20060531093331.1:class cloneNavigator
class cloneNavigator:
    '''
       Displays the headline of the current node plus the headlines of
       all the parents of all the clones of the current node.  Selecting
       a displayed parent headline moves the current node to the
       corresponding clone in the outline.
       
       The idea is to be able to quickly see the context of all the clones
       of the current node and to be able to easily navigate from one clone
       instance to the next.
    '''
    @others
#@nonl
#@+node:ekr.20060531093331.2:init
def __init__ (self,c):
    self.c = c
    import Tkinter as Tk
    if 0:
        f = Tk.Toplevel()
    else:
        log = c.frame.log
        log.selectTab('Clones')
        f = log.tabFrame
        for w in f.winfo_children():
            w.destroy()
    
    # Create and pack empty label and listbox
    self.title = Tk.Label(f)
    self.title.pack(anchor="nw")
    self.lb = Tk.Listbox(f)
    self.lb.pack(expand=1,fill="both")
#@nonl
#@-node:ekr.20060531093331.2:init
#@+node:ekr.20060531093331.3:getAllClones
def getAllClones(self,p):
    c = self.c
    def clonesOf(p,p1=p):
        return p.v.t == p1.v.t
    return filter(clonesOf, c.allNodes_iter(copy=True))
#@nonl
#@-node:ekr.20060531093331.3:getAllClones
#@+node:ekr.20060531093331.4:displayClones
def displayClones(self,c):
    '''Displays the parent headline for all the clones of the current position'''
    cp = c.currentPosition()
    
    # "Title" is the headline of the current node
    self.title.configure(text=cp.headString())
    
    # Initialize listbox and clone list
    clones = self.getAllClones(cp)
    self.lb.delete(0,self.lb.size()-1)
    
    <<Fill listbox with clone parent headlines>>    
    <<Goto selected position when listbox selection changes>>
#@+node:ekr.20060531093331.5:<<Fill listbox with clone parent headlines>>
# Add the headlines of all the clone parents to the listbox
for p in clones:
    if p.parent():
        text = p.parent().headString()
    else:
        text = "<root>"
    self.lb.insert(self.lb.size(),text)
    
    # Initial listbox selection corresponds to current position
    if p.v == cp.v:
        self.lb.selection_set(self.lb.size()-1)
#@nonl
#@-node:ekr.20060531093331.5:<<Fill listbox with clone parent headlines>>
#@+node:ekr.20060531093331.6:<<Goto selected position when listbox selection changes>>
# Callback for when a listbox entry is selected            
def gotoSelectedPosition(event,lb=self.lb,c=c,positions=clones):
    idx = int(lb.curselection()[0])
    p = positions[idx]
    c.frame.tree.expandAllAncestors(p)
    c.selectPosition(p)
    return
self.lb.bind(g.angleBrackets("ListboxSelect"), gotoSelectedPosition)
#@nonl
#@-node:ekr.20060531093331.6:<<Goto selected position when listbox selection changes>>
#@-node:ekr.20060531093331.4:displayClones
#@-node:ekr.20060531093331.1:class cloneNavigator
#@-node:ekr.20060531093331:@command clones-tab
#@+node:ekr.20050123202520.1:@@button settings.leo
# Open leoSettings.leo
c.openLeoSettings()
#@nonl
#@-node:ekr.20050123202520.1:@@button settings.leo
#@+node:ekr.20050130152155:@@button plugins.leo
name = "leoPlugins.leo"
fileName = g.os_path_join(g.app.loadDir,"..","plugins",name)
ok,frame = g.openWithFileName(fileName,c)
if not ok:
    g.es("not found: %s" % name)
#@nonl
#@-node:ekr.20050130152155:@@button plugins.leo
#@+node:ekr.20051111075012:@@button scripts.leo
name = "scripts.leo"
fileName = g.os_path_join(g.app.loadDir,"..","scripts",name)
ok,frame = g.openWithFileName(fileName,c)
if not ok:
    g.es("not found: %s" % name)
#@nonl
#@-node:ekr.20051111075012:@@button scripts.leo
#@+node:ekr.20060329100658:@@button docs.leo
name = "LeoDocs.leo"
fileName = g.os_path_join(g.app.loadDir,"..","doc",name)
ok,frame = g.openWithFileName(fileName,c)
if not ok:
    g.es("not found: %s" % name)
#@nonl
#@-node:ekr.20060329100658:@@button docs.leo
#@+node:ekr.20051025070441:@@button Beautify
c.prettyPrintPythonNode()
#@nonl
#@-node:ekr.20051025070441:@@button Beautify
#@+node:ekr.20041226080857:@@button Spell
c.spellCommands.openSpellTab()
#@nonl
#@-node:ekr.20041226080857:@@button Spell
#@+node:ekr.20051013103117:@@button Save
c.save()
#@nonl
#@-node:ekr.20051013103117:@@button Save
#@+node:ekr.20060202153509.1:@@button Stats
c.debugCommands.printStats()
#@nonl
#@-node:ekr.20060202153509.1:@@button Stats
#@+node:ekr.20060202154734.1:@@button Gc
c.debugCommands.enableGcTrace()
g.collectGarbage()
#@nonl
#@-node:ekr.20060202154734.1:@@button Gc
#@+node:ekr.20060202154734.2:@@button No-Gc
g.app.trace_gc = False
#@nonl
#@-node:ekr.20060202154734.2:@@button No-Gc
#@+node:ekr.20060202162439:@@button *New-obj
c.debugCommands.verboseDumpObjects()
#@nonl
#@-node:ekr.20060202162439:@@button *New-obj
#@+node:ekr.20060202162439.1:@@button New-obj
c.debugCommands.dumpNewObjects()
#@nonl
#@-node:ekr.20060202162439.1:@@button New-obj
#@+node:ekr.20060202162439.2:@@button All-obj
c.debugCommands.dumpAllObjects()
#@nonl
#@-node:ekr.20060202162439.2:@@button All-obj
#@+node:ekr.20060815164203:@@button shadow
"""
Look for @thin files in the current subtree.
Convert those thin files into a file with a shadow file,
if this shadow file does not exist already.

FIXME: the line end convention is currently changed:
      unix lineendings are converted to DOS lineendings,
      if files are converted on Windows.
      Not sure if that is a probem or not.
"""

import mod_shadow_core, os, shutil

def marker_from_extension(filename):
    marker = g.comment_delims_from_extension(filename)[0]
    return marker and marker + '@'

shadow_subdir = c.config.getString("shadow_subdir").strip()
if not shadow_subdir: assert False,'No shadow_subdir setting'
prefix = c.config.getString("shadow_prefix")

for p in p.self_and_subtree_iter():
   h = p.headString().strip()
   if h.startswith("@thin"):
       start = h.find("@thin") + len("@thin")
       leofiledir = os.path.split(c.mFileName)[0]
       filename = h[start:].strip()
       fullfilename = os.path.join(leofiledir, filename)
       theDir = os.path.split(fullfilename)[0]
       leoFolder = os.path.join(leofiledir, theDir, shadow_subdir)
       if not os.path.exists(leoFolder):
           os.mkdir(leoFolder)
           assert os.path.exists(leoFolder)
       else:
           assert os.path.isdir(leoFolder)
       junk, name = os.path.split(filename)
       newname = os.path.join(leoFolder, prefix + name)
       if os.path.exists(newname):
           continue
       g.es( "renaming %s to %s" % (filename, newname))
       shutil.copy2(fullfilename, newname)
       os.unlink(fullfilename)
       f = file(fullfilename, "w")
       f.close()
       mod_shadow_core.copy_file_removing_sentinels(
           sourcefilename=newname,
           targetfilename=fullfilename,
           marker_from_extension = marker_from_extension)
       g.es("File %s is now shadowed" % filename)
#@nonl
#@-node:ekr.20060815164203:@@button shadow
#@+node:ekr.20060910141935:ZODB plugin buttons
#@+node:ekr.20060910141935.1:@@button zodb-read
# c.k.simulateCommand('read-zodb-file')

path = r'c:\prog\tigris-cvs\leo\test\test2.leo'

if g.os_path_exists(path):
    c.zodbCommands.readFile()
else:
    g.es_print('not found: %s' % path)
#@nonl
#@-node:ekr.20060910141935.1:@@button zodb-read
#@+node:ekr.20060910141935.2:@@button zodb-write
# c.k.simulateCommand('write-zodb-file')

path = c.fileName()
# path = r'c:\prog\tigris-cvs\leo\test\test2.leo'

if g.os_path_exists(path):
    c.zodbCommands.writeFile()
else:
    g.es_print('not found: %s' % path)
#@nonl
#@-node:ekr.20060910141935.2:@@button zodb-write
#@-node:ekr.20060910141935:ZODB plugin buttons
#@-all
#@nonl
#@-node:ekr.20051027111215:@thin buttons.txt
#@-leo
