home *** CD-ROM | disk | FTP | other *** search
/ MacWorld UK 2000 March / MW_UK_2000_03.iso / Shareware World / Utilities / Text Processing / Alpha / Tcl / SystemCode / textFill.tcl < prev    next >
Encoding:
Text File  |  1999-10-29  |  7.0 KB  |  244 lines  |  [TEXT/ALFA]

  1. ####################################################################
  2. # Much by Vince Darley.
  3. #                                    created: 26/11/96 {7:08:34 pm} 
  4. #                                last update: 10/29/1999 {14:31:28 PM}
  5. #  Author: Vince Darley
  6. #  E-mail: <vince@santafe.edu>
  7. #    mail: 317 Paseo de Peralta, Santa Fe, NM 87501, USA
  8. #     www: <http://www.santafe.edu/~vince/>
  9. #  
  10. ####################################################################
  11.  
  12. ## 
  13.  #       
  14.  # 'rememberWhereYouAre'
  15.  # 
  16.  #  Given the start of a paragraph and the point to remember, this returns a
  17.  #  record which must be passed to the following function so that it can
  18.  #  find the spot later, even after the paragraph has had
  19.  #  space/tabs/new-lines meddled with.  An optional last argument is a list
  20.  #  of other characters quoted so they are regexp insensitive, which should
  21.  #  also be ignored.  This is used so we can remember positions in text
  22.  #  which has cosmetic characters on the left/right which are not wrapped
  23.  #  (such as the hashes to the left here!).
  24.  #       
  25.  # 'goBackToWhereYouWere'
  26.  # 
  27.  #  Given the beginning and end of a selection, and a previous record, where
  28.  #  the beginning, and record correspond to a previous call of
  29.  #  'rememberWhereYouAre', this procedure will move the insertion point to
  30.  #  the correct place.
  31.  #       
  32.  ##
  33.  
  34. proc rememberWhereYouAre {startPara pos endPara {commentReg ""}} {
  35.     set start [pos::math $pos -20]
  36.     if {[pos::compare $start < $startPara]} {
  37.     set start $startPara
  38.     }
  39.     set __g_remember_str [getText $start $pos]
  40.     if {[string length [string trim $__g_remember_str]] < 3} {
  41.     # there wasn't much to remember; try the other way
  42.     set end [pos::math $pos +20]
  43.     if {[pos::compare $end > $endPara]} {
  44.         set end $endPara
  45.     }
  46.     set __g_remember_str [getText $pos $end]
  47.     set __g_remember_dir 0
  48.     } else {
  49.     set __g_remember_dir 1
  50.     }
  51.     
  52.     set __g_remember_str [quote::Regfind $__g_remember_str]
  53.     regsub -all "\[ \t\r\n${commentReg}\]+" $__g_remember_str \
  54.       {[ \t\r\n${commentReg}]+} __g_remember_str
  55.     return [list $__g_remember_str $__g_remember_dir]
  56. }
  57.  
  58. proc goBackToWhereYouWere {start end memory} {
  59.     if {[lindex $memory 0] != "" } {
  60.     regexp -indices ".*([lindex $memory 0]).*" [getText $start $end] \
  61.       "" submatch
  62.     if {[info exists submatch]} {
  63.         set p [pos::math $start + [lindex $memory 1] + \
  64.           [lindex $submatch [lindex $memory 1]]]
  65.     } else {
  66.         set p $end
  67.     }
  68.     goto [expr {[pos::compare $p >= $end] ? [pos::math $end - 1] : $p}]
  69.     } else {
  70.     goto $start
  71.     }
  72. }
  73.  
  74. ## 
  75.  # -------------------------------------------------------------------------
  76.  #     
  77.  #    "getLeadingIndent" --
  78.  #    
  79.  #  Find the indentation of the line containing 'pos', and convert it to a
  80.  #  minimal form of tabs followed by spaces.  If 'size' is given, then the
  81.  #  variable of that name is set to the length of the indent.  Similarly
  82.  #  'halftab' can be set to half a tab. 
  83.  # -------------------------------------------------------------------------
  84.  ##
  85. proc getLeadingIndent { pos {size ""} {halftab ""} } {
  86.     # get the leading whitespace of the current line
  87.     set res [search -s -n -f 1 -r 1 "^\[ \t\]*" [lineStart $pos]]
  88.     
  89.     # convert it to minimal form: tabs then spaces, stored in 'front'
  90.     getWinInfo a
  91.     set sp [string range "              " 1 $a(tabsize) ]
  92.     regsub -all "($sp| +\t)" [eval getText $res] "\t" front
  93.     if { $size != "" } {
  94.     upvar $size ind
  95.     # get the length of the indent
  96.     regsub -all "\t" $front $sp lfront
  97.     set ind [string length $lfront]
  98.     }
  99.     if { $halftab != "" } {
  100.     upvar $halftab ht
  101.     # get the length of half a tab
  102.     set ht [string range "            " 1 [expr {$a(tabsize)/2}]]
  103.     }
  104.     
  105.     return $front
  106. }
  107.  
  108.  
  109. proc getEndpts {} {
  110.     if {[pos::compare [getPos] == [selEnd]]} {
  111.     set start [getPos]
  112.     set finish [getMark]
  113.     if {[pos::compare $start > $finish]} {
  114.         set temp $start
  115.         set start $finish
  116.         set finish $temp
  117.     }
  118.     } else {
  119.     set start [getPos]
  120.     set finish [selEnd]
  121.     }
  122.     return [list $start $finish]
  123. }
  124.  
  125.  
  126. proc fillRegion {} {
  127.     global leftFillColumn
  128.     set ends [getEndpts]
  129.     set start [lineStart [lindex $ends 0]]
  130.     set finish [lindex $ends 1]
  131.     goto $start
  132.     set text [fillText $start $finish]
  133.     replaceText $start $finish [format "%$leftFillColumn\s" ""] $text "\r"
  134. }
  135.     
  136. proc wrapParagraph {} {
  137.     set pos [getPos]
  138.     set start [paragraph::start $pos] 
  139.     set finish [paragraph::finish $pos]
  140.     goto $start
  141.     wrapText $start $finish
  142.     goto $pos
  143. }
  144.  
  145. proc wrapRegion {} {
  146.     set ends [getEndpts]
  147.     set start [lineStart [lindex $ends 0]]
  148.     set finish [lindex $ends 1]
  149.     if {[pos::compare $start == $finish]} {
  150.     set finish [maxPos]
  151.     }
  152.     wrapText $start $finish
  153. }
  154.     
  155.  
  156.  
  157. # Remove text from window, transform, and insert back into window.
  158. proc fillText {from to} {
  159.     global doubleSpaces
  160.     set text [getText $from $to]
  161.     regexp "^\[ \t\]*" $text front
  162.     regsub -all "\[ \t\n\r\]+" [string trim $text] " " text
  163.     if {$doubleSpaces} {regsub -all {(([^.][a-z]|[^a-zA-Z@]|\\@)[.?!]("|'|'')?([])])?) } $text {\1  } text}
  164.     regsub -all " ?\[\r\n\]" [string trimright [breakIntoLines $text]] "\r${front}" text
  165.     return $front$text
  166. }
  167.  
  168. proc paragraphToLine {} {
  169.     global fillColumn
  170.     global leftFillColumn
  171.     set fc $fillColumn
  172.     set lc $leftFillColumn
  173.     set fillColumn 10000
  174.     set leftFillColumn 0
  175.     fillRegion
  176.     set fillColumn $fc
  177.     set leftFillColumn $lc
  178. }
  179.  
  180. proc lineToParagraph {} {
  181.     global fillColumn
  182.     global leftFillColumn
  183.     set fc $fillColumn
  184.     set fillColumn 75
  185.     set lc $leftFillColumn
  186.     set leftFillColumn 0
  187.     fillRegion
  188.     set fillColumn $fc
  189.     set leftFillColumn $lc
  190. }
  191.  
  192.  
  193. #set sentEnd {[.!?](\r|\n| +)}
  194. set sentEnd {(\r\r|\n\n|[.!?](\r|\n| +))}
  195. set sentBeg {[\r\n ][A-Z]}
  196.  
  197. proc nextSentence {} {
  198.     global sentBeg sentEnd
  199.     if {![catch {search -s -f 1 -r 1 $sentEnd [getPos]} mtch]} {
  200.     if {![catch {search -s -f 1 -r 1 -i 0 $sentBeg [pos::math [lindex $mtch 1] - 1]} mtch]} {
  201.         goto [pos::math [lindex $mtch 0] + 1]
  202.     }
  203.     }
  204. }
  205.  
  206.  
  207. proc prevSentence {} {
  208.     global sentBeg sentEnd
  209.     if {[catch {search -s -f 0 -r 1 $sentBeg [pos::math [getPos] - 2]} mtch]} return
  210.     if {![catch {search -s -f 0 -r 1 $sentEnd [lindex $mtch 1]} mtch]} {
  211.     if {![catch {search -s -f 1 -r 1 -i 0 $sentBeg [pos::math [lindex $mtch 1] - 1]} mtch]} {
  212.         goto [pos::math [lindex $mtch 0] + 1]
  213.     }
  214.     }
  215. }
  216.  
  217. #===============================================================================
  218. # Called by Alpha to do "soft wrapping"
  219. proc softProc {pos start next} {
  220.     global leftFillColumn
  221.     goto $start
  222.     set finish [paraFinish $start]
  223.     set text [fillText $start $finish]
  224.     if {"${text}\r" != [getText $start $finish]} {
  225.     replaceText $start $finish [format "%$leftFillColumn\s" ""] $text "\r"
  226.     return 1
  227.     } else {
  228.     return 0
  229.     }
  230. }
  231.  
  232. proc dividingLine {} {
  233.     global mode
  234.     global ${mode}modeVars
  235.     if {[info exists ${mode}modeVars(prefixString)]} {
  236.     set a [string trim [set ${mode}modeVars(prefixString)]]
  237.     } else {
  238.     set a "#"
  239.     }
  240.     insertText "${a}===============================================================================\r"
  241. }
  242.