wanted: tcl proc to wrap lines of text 
Author Message
 wanted: tcl proc to wrap lines of text

I'm looking for a tcl proc (ie: *not* tk) that can wrap lines of
text to a specific width. For example, I want to be able to pass it a
line of, say, 200 characters with no newlines, and have it return a
string with newlines embedded such that no line is longer than some
given width (eg: 80)

Lazy is as lazy does ... any pointers would be appreciated.



Mon, 27 Jul 1998 03:00:00 GMT  
 wanted: tcl proc to wrap lines of text

Quote:

>I'm looking for a tcl proc (ie: *not* tk) that can wrap lines of

As requested:

proc wrap_lines {txt len} {
  regsub -all "\n" $txt { } txt
  incr len -1
  set out {}
  while {[string len $txt]>$len} {
    set e [string last { } [string range $txt 0 $len]]
    if {$e == -1} {
      set e [string first { } $txt]
      if {$e == -1} break
    }
    append out [string trim [string range $txt 0 [incr e -1]]]\n
    set txt [string range $txt [incr e 2] end]
  }
  return $out$txt

Quote:
}

It's sad that this can be done in perl5 in just a couple of lines...  (Oh,
how I wish for the perl regexp sometimes).  Anyway, this works, but may have
some weird behavior.  It first shrinks the entire given text down to one
line, the chops up the line into the most optimal pieces.  It doesn't keep 2
spaces after '.'s, but it does remove spurious whitespace around each line
(for better or worse), and compresses multiple paragraphs (maybe not good).
If you want more explanation, just ask.  I kind of just hacked it up, but I
put _some_ thought into it.  There may be a better way to do it, but this
chops up 1K in about 0.03 seconds.
--
     Jeffrey Hobbs                           Office: 541/346-3998

                URL: http://www.cs.uoregon.edu/~jhobbs/


Mon, 27 Jul 1998 03:00:00 GMT  
 wanted: tcl proc to wrap lines of text
 [courtesy cc of this posting sent to cited author via email]


:It's sad that this can be done in perl5 in just a couple of lines...  (Oh,
:how I wish for the perl regexp sometimes).  

Talk to Nick, the perl/tk guy.  He understands how perl maintains
regexps internally, and might be able to help.  In theory you
could have a -lperlre to link to that would make this possible.

--tom

ps: did you see my "Irregular Expressions" posting?
--

            It's a dark abyss or tunnel:
            Strewn with stones, stowed, solace, gunwale,



Mon, 03 Aug 1998 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Text widget --screen lines vs. text lines

2. How Do You Wrap Around Text in a Text Pane

3. No text wrapping in text indicator

4. How to insert text in a text widget from a proc

5. Calling a proc within a proc in tcl/tk8.0

6. WANTED: Example of adding line numbers to a text widget

7. how to wrap ANY proc

8. how to wrap any proc

9. Tcl based on line biological text miner

10. Deleting the last lines in a text file with tcl

11. Locating init.tcl in wrapped (Tcl) app

12. how to read text files line by line?

 

 
Powered by phpBB® Forum Software