This is a small tool to indent/unindent Tcl code for this Wiki.
package require Tk
proc doIndent {indent} {
set d [string trimright [.text get 1.0 end] " \r\n"]
set nd ""
foreach line [split $d \n] {
if {$indent && ([string index $line 0] != " ")} {
set line " $line"
} elseif {(!$indent) && ([string index $line 0] == " ") && ([string index $line 1] != " ")} {
set line [string range $line 1 end]
}
append nd $line \n
}
set nd [string trimright $nd " \r\n"]
.text delete 1.0 end
.text insert end $nd
}
text .text -yscrollcommand [list .scroll set] -font {{Lucida Console} 8}
scrollbar .scroll -command [list .text yview]
grid .text .scroll -sticky news
grid columnconfigure . 0 -weight 1
grid rowconfigure . 0 -weight 1
bind all <F2> {doIndent 0}
bind all <F3> {doIndent 1}
wdb -- Nice idea! I've stolen it immediately, and here comes my improved version which not only adds the required space but additionally pretty-prints the source code -- more precisely, updates the appropriate indent level:
package require Tk
pack [text .t] -expand yes -fill both
bind .t <F2> {
set src [.t get 1.0 end-1chars]
.t delete 1.0 end-1chars
.t insert 1.0 [pp $src]
}
proc indentAtLine {str nr} {
set allLines [split $str \n]
set lines [lrange $allLines 0 [expr {$nr - 1}]]
set testStr [join $lines]
set indent 0
while {![info complete $testStr]} {
incr indent
append testStr \}
}
if {[string index [string trim [lindex $allLines $nr]] 0] eq "\}"} then {
incr indent -1
}
set indentStr [string repeat " " 4]
string repeat $indentStr $indent
}
proc pp {str} {
set result ""
set i 0
foreach line [split $str \n] {
append result " "\
[indentAtLine $str $i]\
[string trim $line]\
\n
incr i
}
string trimright $result
}
wdb Attention -- processes line-spanning braced groups { and } but hangs if line-spanning bracket groups [ and ] appear.