home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / metamail / contrib / tk / richtext.tcl next >
Encoding:
Tcl/Tk script  |  1992-12-29  |  5.0 KB  |  226 lines

  1. #!/usr/local/bin/wish -f
  2. # Here is a simple TCL-based richtext interpreter.  It doesn't do a lot yet.
  3. # (It also crashes the OpenWindows server on my Sun, so I don't know if it
  4. # will crash yours.  I'm still looking into the crash thing.)  Once the
  5. # crash thing is fixed, I'll add more to it...
  6. # Anyway, try this:
  7. #     wish -f richtext.tcl < file.rt
  8. # and see what you think!
  9. # /mtr
  10. proc    richtext        {{w .richtext}} {
  11.     catch    {destroy $w}
  12.     toplevel    $w
  13.     wm title    $w        "Richtext"
  14.     wm iconname    $w        "rt"
  15.  
  16.     text    $w.text        -borderwidth 2 \
  17.                 -height 34 \
  18.                 -relief raised \
  19.                 -setgrid true \
  20.                 -width 80 \
  21.                 -wrap word \
  22.                 -yscrollcommand "$w.scroll set"
  23.     scrollbar    $w.scroll    -command "$w.text yview" \
  24.                 -relief flat
  25.     frame    $w.bot        -borderwidth 1
  26.     pack append    $w.bot        [button $w.bot.button \
  27.                     -command "destroy $w;destroy ." \
  28.                     -text Dismiss] \
  29.                     {top padx 5 pady 5 expand}
  30.  
  31.     pack append    $w        $w.bot \
  32.                     {bottom fillx} \
  33.                 $w.scroll \
  34.                     {right filly} \
  35.                 $w.text \
  36.                     {expand fill}
  37.  
  38.     $w.text tag    configure    bold \
  39.                     -font -*-courier-bold-r-normal-*-120-*
  40.     $w.text tag configure    italic \
  41.                     -font -*-courier-medium-o-normal-*-120-*
  42.     $w.text tag configure    bolditalic \
  43.                     -font -*-courier-bold-o-normal-*-120-*
  44.     $w.text tag configure    fixed \
  45.                     -font -*-courier-medium-r-normal-*-120-*
  46.  
  47.     $w.text tag configure    bigbold \
  48.                     -font -*-courier-bold-r-normal-*-140-*
  49.     $w.text tag configure    bigitalic \
  50.                     -font -*-courier-medium-o-normal-*-140-*
  51.     $w.text tag configure    bigbolditalic \
  52.                     -font -*-courier-bold-o-normal-*-140-*
  53.     $w.text tag configure    bigfixed \
  54.                     -font -*-courier-medium-r-normal-*-140-*
  55.  
  56.     $w.text tag configure    smallbold \
  57.                     -font -*-courier-bold-r-normal-*-100-*
  58.     $w.text tag configure    smallitalic \
  59.                     -font -*-courier-medium-o-normal-*-100-*
  60.     $w.text tag configure    smallbolditalic \
  61.                     -font -*-courier-bold-o-normal-*-100-*
  62.     $w.text tag configure    smallfixed \
  63.                     -font -*-courier-medium-r-normal-*-100-*
  64.  
  65.     $w.text tag configure    underline \
  66.                     -underline on
  67.  
  68.  
  69.     set        env(bold)    0
  70.     set        env(italic)    0
  71.     set        env(fixed)    0
  72.     set        env(smaller)    0
  73.     set        env(bigger)    0
  74.     set        env(underline)    0
  75.  
  76.     set        env(center)    0
  77.     set        env(flushleft)    0
  78.     set        env(flushright)    0
  79.     set        env(indent)    0
  80.     set        env(indentright) 0
  81.     set        env(outdent)    0
  82.     set        env(outdentright) 0
  83.     set        env(samepage)    0
  84.     set        env(superscript) 0
  85.     set        env(subscript)    0
  86.     set        env(heading)    0
  87.     set        env(footing)    0
  88.     set        env(excerpt)    0
  89.     set        env(paragraph)    0
  90.     set        env(signature)    0
  91.  
  92.     set        intoken        0
  93.     set        token        ""
  94.  
  95.     set        incomment    0
  96.  
  97.     set        output        ""
  98.     set        lasto        " "
  99.  
  100.  
  101.     while {[gets stdin buffer] > -1} {
  102.     set    len        [string length $buffer]
  103.  
  104.     if {(!$intoken) && ($lasto != " ")} {
  105.         append    output    [set lasto " "]
  106.     }
  107.  
  108.     for {set i 0} {$i < $len} {incr i} {
  109.         set    c        [string index $buffer $i]
  110.         if {$intoken} {
  111.         if {([string length $token] == 0) && ($c == "/")} {
  112.             set    intoken    -1
  113.             continue
  114.         }
  115.         if {$c != ">"} {
  116.             append    token    $c
  117.             continue
  118.         }
  119.  
  120.         set    command    [string tolower $token]
  121.         set    pos    $intoken
  122.  
  123.         set    intoken    0
  124.         set    token    ""
  125.  
  126.         if {$command == "comment"} {
  127.             if {[incr incomment $pos] < 0} {
  128.             set    incomment    0
  129.             }
  130.             if {$incomment > 0} {    continue }
  131.         }
  132.  
  133.         if {$command == "lt"} {
  134.             append    output    [set lasto "<"]
  135.             continue
  136.         }
  137.         if {($command == "nl") || ($command == "np")} {
  138.             append    output    "\n"
  139.             set        lasto    " "
  140.             continue
  141.         }
  142.  
  143.         if {[string length $output]} {
  144.             set    size    [expr $env(bigger)-$env(smaller)]
  145.             if {$size > 0} {
  146.                 set    font    "big"
  147.             } else {
  148.                 if {$size < 0} {
  149.                 set    font    "small"
  150.                 } else {
  151.                 set    font    ""
  152.                 }
  153.             }
  154.             if {$env(fixed) > 0} {
  155.                 append    font    "fixed"
  156.             } else {
  157.                 if {$env(bold) > 0} {
  158.                 append    font    "bold"
  159.                 }
  160.                 if {$env(italic) > 0} {
  161.                 append    font    "italic"
  162.                 }
  163.             }
  164.             set    tags    [list $font]
  165.             if {$env(underline) > 0} {
  166.                 append    tags    underline
  167.             }
  168.  
  169.             insertWithTags    $w.text $output $tags
  170.  
  171.             set    output        ""
  172.             set    lasto        " "
  173.         }
  174.  
  175.         catch    {
  176.             if {[incr env($command) $pos] < 0} {
  177.             set $env($command) 0
  178.             }
  179.         }
  180.         continue
  181.         }
  182.  
  183.         if {$c != "<"} {
  184.         if {!$incomment} {
  185.             append    output    [set lasto $c]
  186.         }
  187.         continue
  188.         }
  189.  
  190.         set    intoken        1
  191.     }
  192.     }
  193.  
  194.     insertWithTags $w.text    $output
  195. }
  196.  
  197. # The procedure below inserts text into a given text widget and
  198. # applies one or more tags to that text.  The arguments are:
  199. #
  200. # w        Window in which to insert
  201. # text        Text to insert (it's inserted at the "insert" mark)
  202. # args        One or more tags to apply to text.  If this is empty
  203. #        then all tags are removed from the text.
  204.  
  205. proc insertWithTags {w text args} {
  206.     set start [$w index insert]
  207.     $w insert insert $text
  208.     foreach tag [$w tag names $start] {
  209.     $w tag remove $tag $start insert
  210.     }
  211.     foreach i $args {
  212.     $w tag add $i $start insert
  213.     }
  214. }
  215.  
  216.  
  217. richtext
  218. wm withdraw .
  219.