#!/usr/local/bin/wish -f

text .text -relief raised -bd 2 -yscrollcommand ".scroll set"
scrollbar .scroll -command ".text yview"
pack .scroll -side right -fill y
pack .text -side left -fill both -expand yes
.text config -setgrid 1 -wrap word
if {[catch {.text config -font "*-new century schoolbook-medium-r-normal--14-*"}]} {
    puts "You don't have the font New Century Schoolbook at 14 points."
}

proc loadFile { file } {
    .text delete 1.0 end
    set f [open $file]
    while {![eof $f]} {
	.text insert end [read $f 1024]
    }
    close $f
}

proc forAllMatches { w pattern script } {
    scan [$w index end] %d numLines
    for {set i 1} {$i < $numLines} {incr i} {
	$w mark set last $i.0
	while {[regexp -indices $pattern \
	       [$w get last "last lineend"] indices]} {
	   $w mark set first "last + [lindex $indices 0] chars"
	   $w mark set last "last + 1 chars + [lindex $indices 1] chars"
	   uplevel $script
        }
    }
}

bind all <Q> "exit"

loadFile demonstration.txt

forAllMatches .text "<DEMO-IT>.*</DEMO-IT>" {
    .text delete first "first + 9 chars"
    .text delete "last - 10 chars" last
    set demo_name [.text get first last]
    .text tag bind $demo_name <Button-1> "exec omnimoni --r $demo_name &"
    .text tag bind $demo_name <Button-2> "exec omnimoni --d 4 --r $demo_name &"
    .text tag add demo-it first last
    .text tag add $demo_name first last
}

forAllMatches .text "<SECTION>.*</SECTION>" {
    .text delete first "first + 9 chars"
    .text delete "last - 10 chars" last
    set demo_name [.text get first last]
    .text tag add section first last
}

forAllMatches .text "<FX>.*" {
    .text delete first "first + 4 chars"
    .text tag add fixed first last
}

.text tag config demo-it -fore "red"

if {[catch {.text tag config fixed -font "*-courier-medium-r-normal--14-*"}]} {
    puts "You dont have the font Courier at 14 points."
}

if {[catch {.text tag config section -font "*-new century schoolbook-bold-r-normal--18-*"}]} {
    puts "You don't have the font New Century Schoolbook bold at 18 points."
}

.text config -state disabled
