Expanding outline links

Here is some code to expand links to other outlines. Place this code in your startup script (Edit->Edit Startup Script) and then you will be able to hit Ctrl-u to expand links in the current outline. Only headlines that have no descendants and whose text consists solely of a link will be expanded. This is kind of like the reverse of the File->Subtree->Extract and Link menu pick. The expansion will not take place in the current outline and instead a new outline will be opened and it will be expanded. Only one level of expansion will take place and repeatedly hitting Ctrl-u on the output of the previous Ctrl-u will allow several levels of expansion to take place. Depending how large your outline is, this operation can be quite slow and there is no feedback on progress.

Only works with version 0.93 due to changes after that version. Can be fixed pretty easily for later versions.

 # Returns whether or not the node is a leaf and consists only of an outline link
 proc leaflinks {t n} {
    set title [$t set $n title]
    expr {[$t isleaf $n] && [string match {\[*} $title] && [string match {*]} $title]}
    }

 # This is an error in tkoutline 0.93, and it is necessary for current
 # development tkoutline versions that have new tcllib struct library code
 catch {package require struct::list}

 proc getNodesToExpand tree {
    # TODO: why is the namespace code required here? struct::tree bug? or a bug in tkoutline?
    return [$tree children -all [$tree rootname] filter [namespace code leaflinks]]

    # Descendants method only available in newer tcllib struct versions so don't use it
    #return [$tree descendants [$tree rootname] filter [namespace code leaflinks]]
    }
 proc expandLinks outline {
    foreach {node title} [$outline tree attr title -nodes [getNodesToExpand [$outline treecmd]]] {
        set file [string range $title 1 end-1]
        if {[file exists $file]} {
            ::tkoutline::loadFileAsSubtree  $outline $node $file
            }
        }
    }
 proc loadFileAsSubtree2 {tree node {fileName ""}} {
    set subtree [::tkoutline::getTreeFromFile $fileName]
    set savedHeadline [$tree set $node title]
    outlinewidget::copySubtree $subtree [$tree rootname] $tree $node
    $tree set $node title $savedHeadline
    $subtree destroy
    return
 }
 proc expandLinks tree {
    set tree2 [struct::tree]; $tree2 = $tree
    foreach {node title} [$tree2 attr title -nodes [getNodesToExpand $tree2]] {
        set file [string range $title 1 end-1]
        if {[file exists $file]} {
            loadFileAsSubtree2  $tree2 $node $file
            }
        }
    return $tree2
    }
 proc expandOutline tree {
    set tree [expandLinks $tree]
    ::tkoutline::browser addOutline -tree $tree
    }
 bind Outline <Control-u> [namespace code {expandOutline [[winfo parent %W] treecmd]}]


Updated 28 Apr 2005, 01:24 GMT
Search - Recent Changes - 2 References - About WiKit - Go to Tkoutline - Help
Sourceforge logo