home *** CD-ROM | disk | FTP | other *** search
/ Internet File Formats / InternetFileFormatsCD.bin / text / latex / mac / alpha60.hqx / Tcl / SystemCode / fill.tcl < prev    next >
Encoding:
Text File  |  1995-04-25  |  4.6 KB  |  200 lines

  1. #=============================================================================
  2. #    'Fill' routines.
  3. #=============================================================================
  4.  
  5. proc fillParagraph {} {
  6.     set pos [getPos]
  7.     set start [paraStart $pos] 
  8.     set finish [paraFinish $pos]
  9.     goto $start
  10.     set text [fillText $start $finish]
  11.     if {"$text\r" != [getText $start $finish]} {
  12.         replaceText $start $finish $text "\r"
  13.     }
  14.     goto $pos
  15. }
  16.  
  17.  
  18. proc sentenceParagraph {} {
  19.     set pos [getPos]
  20.     set start [paraStart $pos] 
  21.     set finish [paraFinish $pos]
  22.  
  23.     set t [string trim [getText $start $finish]]
  24.     set period [regexp {\.$} $t]
  25.     regsub -all "\[ \t\r\]+" $t " " text
  26.     regsub -all {\. } $text "╞" text
  27.     set result ""
  28.     foreach line [split [string trimright $text {.}] "╞"] {
  29.         if {[string length $line]} {
  30.             append result [breakIntoLines $line] ".\r"
  31.         }
  32.     }
  33.     if {!$period && [regexp {\.\r} $result]} {
  34.         set result [string trimright $result ".\r"]
  35.         append result "\r"
  36.     }
  37.     if {$result != [getText $start $finish]} {
  38.         replaceText $start $finish $result
  39.     }
  40.     goto $pos
  41. }
  42.  
  43. proc getEndpts {} {
  44.     if {[getPos] == [selEnd]} {
  45.         set start [getPos]
  46.         set finish [getMark]
  47.         if {$start > $finish} {
  48.             set temp $start
  49.             set start $finish
  50.             set finish $temp
  51.         }
  52.     } else {
  53.         set start [getPos]
  54.         set finish [selEnd]
  55.     }
  56.     return [list $start $finish]
  57. }
  58.  
  59.  
  60. proc fillRegion {} {
  61.         global leftFillColumn
  62.         set ends [getEndpts]
  63.         set start [lineStart [lindex $ends 0]]
  64.         set finish [lindex $ends 1]
  65.         goto $start
  66.         set text [fillText $start $finish]
  67.         replaceText $start $finish [format "%$leftFillColumn\s" ""] $text "\r"
  68. }
  69.     
  70. proc wrapParagraph {} {
  71.     set pos [getPos]
  72.     set start [paraStart $pos] 
  73.     set finish [paraFinish $pos]
  74.     goto $start
  75.     wrapText $start $finish
  76.     goto $pos
  77. }
  78.  
  79. proc wrapRegion {} {
  80.     set ends [getEndpts]
  81.     set start [lineStart [lindex $ends 0]]
  82.     set finish [lindex $ends 1]
  83.     if {$start == $finish} {
  84.         set finish [maxPos]
  85.     }
  86.     wrapText $start $finish
  87. }
  88.     
  89. proc paraStart {pos} {
  90.     if {$pos == [maxPos]} {incr pos -1}
  91.     set res [search -s -n -f 0 -r 1 -l 0 {^([ \t]*|([\\%].*))$} $pos]
  92.     if {![string length $res]} {return 0}
  93.     return [nextLineStart [lindex $res 0]]
  94. }
  95.  
  96.  
  97. proc paraFinish {pos} {
  98.     set end [maxPos]
  99.     set res [search -s -n -f 1 -r 1 -l $end {^([ \t]*|([\\%].*))$} $pos]
  100.     if {![string length $res]} {return $end}
  101.     return [lindex $res 0]
  102. }
  103.  
  104.  
  105. proc oldParaStart {pos} {
  106.     set pos [lineStart $pos]
  107.     while {$pos > 0} {
  108.         set back [lineStart [expr $pos-1]]
  109.         if {$back < 0} {return 0}
  110.         if {[regexp "^\[ \t\]*\r$" [getText $back $pos]]} {return $pos}
  111.         set pos $back
  112.     }
  113.     return 0
  114. }
  115.  
  116.  
  117. proc oldParaFinish {pos} {
  118.     set end [maxPos]
  119.     while {$pos < $end} {
  120.         set next [nextLineStart $pos]
  121.         if {$pos == "-1"} {return $end}
  122.         if {[regexp "^\[ \t\]*\r$" [getText $pos $next]]} {return $pos}
  123.         set pos $next
  124.     }
  125.     return $end
  126. }
  127.  
  128.  
  129. # Remove text from window, transform, and insert back into window.
  130. #     Alternate version of fillText. Probably better but slower.
  131. # proc fillText {from to} {
  132. #     set text [getText $from $to]
  133. #     regsub -all "\[ \t\r\]+" $text " " text
  134. #     regsub -all {\. } $text {.  } text
  135. #     set text [string trimright [breakIntoLines $text]]
  136. #     set a "a"
  137. #     set a [breakIntoLines $a]
  138. #     regsub "a" $a "" a
  139. #     set a \r$a
  140. #     set b $a
  141. #     regsub -all [append b " "] $text $a text
  142. #     return $text
  143. # }
  144. #    regsub -all {\. } $text {.  } text
  145. proc fillText {from to} {
  146.     set text [string trim [getText $from $to]]
  147.     regsub -all "\[ \t\r\]+" $text " " text
  148.     regsub -all {(\.|\?|\!) } $text {\1  } text
  149.     return [string trimright [breakIntoLines $text]]
  150. }
  151.  
  152. proc paragraphToLine {} {
  153.     global fillColumn
  154.     global leftFillColumn
  155.     set fc $fillColumn
  156.     set lc $leftFillColumn
  157.     set fillColumn 10000
  158.     set leftFillColumn 0
  159.     fillRegion
  160.     set fillColumn $fc
  161.     set leftFillColumn $lc
  162. }
  163.  
  164. proc lineToParagraph {} {
  165.     global fillColumn
  166.     global leftFillColumn
  167.     set fc $fillColumn
  168.     set fillColumn 75
  169.     set lc $leftFillColumn
  170.     set leftFillColumn 0
  171.     fillRegion
  172.     set fillColumn $fc
  173.     set leftFillColumn $lc
  174. }
  175.  
  176.  
  177. #set sentEnd {[.!?](\r| +)}
  178. set sentEnd {(\r\r|[.!?](\r| +))}
  179. set sentBeg {[\r ][A-Z]}
  180.  
  181. proc nextSentence {} {
  182.     global sentBeg sentEnd
  183.     if {![catch {search -s -f 1 -r 1 $sentEnd [getPos]} mtch]} {
  184.         if {![catch {search -s -f 1 -r 1 -i 0 $sentBeg [expr [lindex $mtch 1]-1]} mtch]} {
  185.             goto [expr [lindex $mtch 0]+1]
  186.         }
  187.     }
  188. }
  189.  
  190.  
  191. proc prevSentence {} {
  192.     global sentBeg sentEnd
  193.     if {[catch {search -s -f 0 -r 1 $sentBeg [expr [getPos]-2]} mtch]} return
  194.     if {![catch {search -s -f 0 -r 1 $sentEnd [lindex $mtch 1]} mtch]} {
  195.         if {![catch {search -s -f 1 -r 1 -i 0 $sentBeg [expr [lindex $mtch 1]-1]} mtch]} {
  196.             goto [expr [lindex $mtch 0]+1]
  197.         }
  198.     }
  199. }
  200. # 5 730 845 955