# WikIndent, by Mike Griffiths, Apr 29 2004
# Put a space before every line in a (text/tcl code) file.
set t WikIndent
wm withdraw .
set types {
{{TCL Scripts} {.tcl} }
{{Text Files} {.txt} }
{{All Files} * }
}
set f [lindex $argv 0]
if { $f == "" } {
set f [tk_getOpenFile -filetypes $types -title "Select a Tcl Code File"]
}
if { $f == "" } {
exit;
}
if { ![file exists $f] || ![file isfile $f] } {
tk_messageBox -icon error -title $t -message "No such file '$f'!"
}
if { ![file readable $f] || [catch {open $f r} fid] } {
tk_messageBox -icon error -title $t -message "Can't read file '$f': $fid"
exit;
}
set out [file join [file dirname $f] "wi[file tail $f]"]
if { [catch {open "$out" w+} fid2]} {
tk_messageBox -icon error -title $t -message "Can't open output file '$out': $fid2"
exit;
}
gets $fid str
while { ![eof $fid] } {
puts $fid2 " $str"
gets $fid str
}
close $fid
close $fid2
tk_messageBox -icon info -title $t -message "Done!"Oh, boy. cat sample.txt | sed 's/^/ /' > sample2.txtglennj: useless use of cat: sed 's/^/ /' sample.txt > sample2.txt- touché!MG I'm on Windows, so don't really have that option available. That's one of the main reasons I love Tcl; write some code, and almost any computer can run it, as-is, so long as you write it properly to start with./me is on Windows too. Pick Cygwin or the GNU native Windows ports and another excuse. ;-)MG *laughs* Good point. ;) But it's still a lot easier to just fire off a Tcl script under Windows than to launch up Cygwin to do this (easier = quicker. I'm a teenager, therefore inherently lazy;). Besides, in the last few months I've fallen in love with Tcl too much to use something else, when Tcl can do the job so easily ;)
RS Here's how I would do it, given Tcl only:
set in [open $filename]
set out [open wi$filename w]
while {[gets $in line]>=0} {
puts $out " $line"
}
close $in
close $out MG returns to this page nearly a year later, and groans. ;)This one has a simple GUI for cut-and-paste. Improvements are welcome!
proc convert {} {
.out delete @0,0 end
set x [.in get @0,0 end]
set y {}
foreach l [split $x "\n"] {
set y "$y $l\n"
}
.out insert @0,0 $y
}
text .in
text .out
button .but -command convert -text "Convert"
pack .in .but .out[gg] - 2006-12-30
