+proc clear_ctext {{first 1.0}} {
+ global ctext smarktop smarkbot
+
+ if {![info exists smarktop] || [$ctext compare $first < $smarktop]} {
+ set smarktop $first
+ }
+ if {![info exists smarkbot] || [$ctext compare $first < $smarkbot]} {
+ set smarkbot $first
+ }
+ $ctext delete $first end
+}
+
+proc incrsearch {name ix op} {
+ global ctext searchstring
+
+ $ctext tag remove found 1.0 end
+ if {$searchstring ne {}} {
+ searchmarkvisible 1
+ }
+}
+
+proc dosearch {} {
+ global sstring ctext searchstring
+
+ focus $sstring
+ $sstring icursor end
+ $ctext tag remove sel 1.0 end
+ if {$searchstring eq {}} return
+ set here [$ctext index insert]
+ set match [$ctext search -count mlen -- $searchstring $here]
+ if {$match eq {}} {
+ bell
+ return
+ }
+ $ctext see $match
+ set mend "$match + $mlen c"
+ $ctext tag add sel $match $mend
+ $ctext mark set insert $mend
+}
+
+proc searchmark {first last} {
+ global ctext searchstring
+
+ set mend $first.0
+ while {1} {
+ set match [$ctext search -count mlen -- $searchstring $mend $last.end]
+ if {$match eq {}} break
+ set mend "$match + $mlen c"
+ $ctext tag add found $match $mend
+ }
+}
+
+proc searchmarkvisible {doall} {
+ global ctext smarktop smarkbot
+
+ set topline [lindex [split [$ctext index @0,0] .] 0]
+ set botline [lindex [split [$ctext index @0,[winfo height $ctext]] .] 0]
+ if {$doall || $botline < $smarktop || $topline > $smarkbot} {
+ # no overlap with previous
+ searchmark $topline $botline
+ set smarktop $topline
+ set smarkbot $botline
+ } else {
+ if {$topline < $smarktop} {
+ searchmark $topline [expr {$smarktop-1}]
+ set smarktop $topline
+ }
+ if {$botline > $smarkbot} {
+ searchmark [expr {$smarkbot+1}] $botline
+ set smarkbot $botline
+ }
+ }
+}
+
+proc scrolltext {f0 f1} {
+ global ctext smarktop smarkbot searchstring
+
+ .ctop.cdet.left.sb set $f0 $f1
+ if {$searchstring ne {}} {
+ searchmarkvisible 0
+ }
+}
+