# Here's the tcl loop for scanning a text widget for a pattern. At # every match, the script is run with the "first" and "last" marks set # to delimit the matched string. 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 -nocase -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 } } }