Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

saving window contents and wordwrap

2 views
Skip to first unread message

John L

unread,
Dec 20, 2001, 10:53:26 AM12/20/01
to
My application has a simple text edit window with word wrap. When I grab
the contents to save to a file, I loose the wrap formatting (and get long
lines). I want save the window contents to a file... just as they appear in
the window. Does anyone know how to do this, other than writing a seperate
"wrap algorithm" to call when saving?

Have tried using: "set data [$window get 1.0 end]" and also using "dump".

Thanks in advance,
John
(remove NOSPAM in email address to reply)


John LaGue

unread,
Dec 20, 2001, 11:04:52 AM12/20/01
to

Richard Suchenwirth

unread,
Dec 20, 2001, 1:12:23 PM12/20/01
to John L
John L <john_...@NOSPAMhotmail.com> wrote:
> My application has a simple text edit window with word wrap. When I grab
> the contents to save to a file, I loose the wrap formatting (and get long
> lines). I want save the window contents to a file... just as they appear in
> the window. Does anyone know how to do this, other than writing a seperate
> "wrap algorithm" to call when saving?

I'm afraid there is no other way, as the text widget wraps dynamically
(you can see that when you drag the window wider or narrower). The wrap
formatting is never part of the text content itself, just of the
rendering (similar to e.g. Netscape/IE on well-behaved HTML pages).

Inserting explicit newlines means possibly ugly looks in other viewers.
But you might add a switch (e.g. checkbutton) to turn word wrapping on
or off (in that case a horizontal scrollbar would also be nice), so the
user can see what he's doing and insert explicit newlines where he wants
them...
--
Schoene Gruesse/best regards, Richard Suchenwirth - +49-7531-86 2703
Siemens Dematic AG, PA RC D2, Buecklestr.1-5, 78467 Konstanz,Germany
Personal opinions expressed only unless explicitly stated otherwise.

Michael Delaney

unread,
Dec 20, 2001, 6:02:57 PM12/20/01
to
>> Does anyone know how to do this, other than writing a seperate
>> "wrap algorithm" to call when saving?

If you do decide you need a separate wrap procedure, here's a
procedure I was using to try solving a similar
problem--formatting a text file to fit into a column of a given
width. It starts by making the text one long string, then returns a
list of lines that fit into the specified width. For what I was doing
I didn't need to care about paragraphing; if you do, you might need
to start by splitting the original text at newlines first and then
process each paragraph.

proc wrap {data width} {
set column ""
regsub -all {[ \t\n]+} $data " " data

while {[string length $data] >= $width} {
set breakpoint [expr $width + 1]
while {[string index $data $breakpoint] != " "} {
incr breakpoint -1
}
incr breakpoint -1
lappend column [pad [string range $data 0 $breakpoint] [expr
$width + 2]]

set data [string range $data [incr breakpoint] end]
}
lappend column $data
return $column

}

>

Jeff Hobbs

unread,
Dec 20, 2001, 6:32:23 PM12/20/01
to
John L wrote:
>
> My application has a simple text edit window with word wrap. When I grab
> the contents to save to a file, I loose the wrap formatting (and get long
> lines). I want save the window contents to a file... just as they appear in
> the window. Does anyone know how to do this, other than writing a seperate
> "wrap algorithm" to call when saving?

As the -word wrap option in text widgets is for visual purposes
only, you'll have to figure out how many cols are showing, and
use a text wrapping command (they exist in tcllib) to do the
wrapping yourself before saving.

--
Jeff Hobbs The Tcl Guy
Senior Developer http://www.ActiveState.com/
Tcl Support and Productivity Solutions
http://www.ActiveState.com/Products/ASPN_Tcl/

Richard Suchenwirth

unread,
Dec 21, 2001, 6:42:16 AM12/21/01
to
Michael Delaney <mdel...@qis.net> wrote:

>proc wrap {data width} {
> set column ""
> regsub -all {[ \t\n]+} $data " " data
>
> while {[string length $data] >= $width} {
> set breakpoint [expr $width + 1]
> while {[string index $data $breakpoint] != " "} {
> incr breakpoint -1
> }
> incr breakpoint -1
> lappend column [pad [string range $data 0 $breakpoint] [expr $width + 2]]
>
> set data [string range $data [incr breakpoint] end]
> }
> lappend column $data
> return $column
>}

If you can do without the padding (where does that come from? it's not
in my Tcl ;-), the following code should have similar effect:

proc linebreak {s {width 80}} {
set res {}
while {[string length $s]>$width} {
set pos [string wordstart $s $width]
lappend res [string range $s 0 [expr $pos-1]]
set s [string range $s $pos end]
}
lappend res $s
} ;# RS

[string wordstart] is quite powerful (does also hyphens gracefully), and
built in...

Michael Delaney

unread,
Dec 21, 2001, 5:44:48 PM12/21/01
to
Thanks! Will try that instead. (sorry, "pad" refers to another
procedure in the same program; I should have cleaned that
up a bit before posting it.)
0 new messages