Updated 2012-09-27 10:53:22 by LkpPo

MG Apr 29th 2004 - This little program (called WikiIndent, until I typoed the name when adding this page and decided to rename the program;) is extremely basic; it opens a file of your choice, then replicates in a file named 'wi<original>'; the only difference is that each line will have a single white space added to the beginning of it. The point? When I code in Tcl, I don't put a space at the left-hand line of anything not inside a call to proc, if, etc (including the call to proc itself), so when I go to paste things into the Wiki, I need to add spaces to each line to have it register as Tcl code. And after writing three scripts like this in the last week, I decided to stick with one and publish it. ;)

You should be able to specify the file-name on the command line, too (ie, wish wikindent.tcl ./mycode.tcl). It still needs wish, rather than tclsh, though, because it has calls to tk_messageBox.

The code:
 # 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 == "" } {
 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" 
 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"
 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.txt

glennj: 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