 ---------------------------------------------------------------
 Copyright (C) 1993,1994 by John Heidemann. All rights reserved.
 <johnh@ficus.cs.ucla.edu>

 Redistribution and use in source and binary forms, with or
 without modification, are permitted provided that the following
 conditions are met:
 1. Redistributions of source code must retain the above
    copyright notice, this list of conditions and the following
    disclaimer.
 2. Redistributions in binary form must reproduce the above
    copyright notice, this list of conditions and the following
    disclaimer in the documentation and/or other materials
    provided with the distribution. 
 3. The name of John Heidemann may not be used to endorse or
    promote products derived from this software without specific
    prior written permission.

 THIS SOFTWARE IS PROVIDED BY JOHN HEIDEMANN ``AS IS'' AND ANY
 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL JOHN
 HEIDEMANN BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


proc put-text {tw txt} {

    $tw configure -font -*-Times-Medium-R-Normal-*-140-*
    $tw tag configure bld -font -*-Times-Bold-R-Normal-*-140-*
    $tw tag configure cmp -font -*-Courier-Bold-R-Normal-*-120-*
    $tw tag configure hdr -font -*-Times-Bold-R-Normal-*-180-* \
    -underline 1
    $tw tag configure itl -font -*-Times-Medium-I-Normal-*-140-*
    $tw tag configure rev -foreground white -background black
    $tw tag configure btn \
            -font -*-Courier-Medium-R-Normal-*-120-* \
            -foreground black -background white \
            -relief groove -borderwidth 2
    $tw mark set insert 0.0
    set t $txt
    while {[regexp -indices {<([^@>]*)>} $t match inds] == 1} {
        set start [lindex $inds 0]
        set end [lindex $inds 1]
        set keyword [string range $t $start $end]
        set oldend [$tw index end]
        $tw insert end [string range $t 0 [expr $start - 2]]
        purge-all-tags $tw $oldend insert
        if {[string range $keyword 0 0] == "/"} {
            set keyword [string trimleft $keyword "/"]
            if {[info exists tags($keyword)] == 0} {
                error "end tag $keyword without beginning"
            }
            $tw tag add $keyword $tags($keyword) insert
            unset tags($keyword)
        } else {
            if {[info exists tags($keyword)] == 1} {
                error "nesting of begin tag $keyword"
            }
            set tags($keyword) [$tw index insert]
        }
        set t [string range $t [expr $end + 2] end]
    }
    set oldend [$tw index end]
    $tw insert end $t
    purge-all-tags $tw $oldend insert
}

proc purge-all-tags {w start end} {
    foreach tag [$w tag names $start] {
        $w tag remove $tag $start $end
    }
}
