1 # incremental search panel
2 # based on code from gitk, Copyright (C) Paul Mackerras
11 field searchdirn -forwards
16 constructor new {i_w i_text args} {
22 ${NS}::label $w.l -text [mc Find:]
23 entry $w.ent -textvariable ${__this}::searchstring -background lightgreen
24 ${NS}::button $w.bn -text [mc Next] -command [cb find_next]
25 ${NS}::button $w.bp -text [mc Prev] -command [cb find_prev]
26 ${NS}::checkbutton $w.cs -text [mc Case-Sensitive] \
27 -variable ${__this}::casesensitive -command [cb _incrsearch]
29 pack $w.cs -side right
30 pack $w.bp -side right
31 pack $w.bn -side right
32 pack $w.ent -side left -expand 1 -fill x
34 eval grid conf $w -sticky we $args
37 trace add variable searchstring write [cb _incrsearch_cb]
39 bind $w <Destroy> [list delete_this $this]
44 if {![visible $this]} {
51 if {[visible $this]} {
58 return [winfo ismapped $w]
65 method _get_new_anchor {} {
66 # use start of selection if it is visible,
67 # or the bounds of the visible area
68 set top [$ctext index @0,0]
69 set bottom [$ctext index @0,[winfo height $ctext]]
70 set sel [$ctext tag ranges sel]
72 set spos [lindex $sel 0]
73 if {[lindex $spos 0] >= [lindex $top 0] &&
74 [lindex $spos 0] <= [lindex $bottom 0]} {
78 if {$searchdirn eq "-forwards"} {
85 method _get_wrap_anchor {dir} {
86 if {$dir eq "-forwards"} {
93 method _do_search {start {mlenvar {}} {dir {}} {endbound {}}} {
94 set cmd [list $ctext search]
97 lappend cmd -count mlen
99 if {!$casesensitive} {
105 lappend cmd $dir -- $searchstring
106 if {$endbound ne {}} {
107 set here [eval $cmd [list $start] [list $endbound]]
109 set here [eval $cmd [list $start]]
111 set here [eval $cmd [_get_wrap_anchor $this $dir]]
117 method _incrsearch_cb {name ix op} {
118 after idle [cb _incrsearch]
121 method _incrsearch {} {
122 $ctext tag remove found 1.0 end
123 if {[catch {$ctext index anchor}]} {
124 $ctext mark set anchor [_get_new_anchor $this]
126 if {$searchstring ne {}} {
127 set here [_do_search $this anchor mlen]
130 $ctext tag remove sel 1.0 end
131 $ctext tag add sel $here "$here + $mlen c"
132 $w.ent configure -background lightgreen
135 $w.ent configure -background lightpink
140 method find_prev {} {
141 find_next $this -backwards
144 method find_next {{dir -forwards}} {
148 $ctext mark unset anchor
149 if {$searchstring ne {}} {
150 set start [_get_new_anchor $this]
151 if {$dir eq "-forwards"} {
152 set start "$start + 1c"
154 set match [_do_search $this $start mlen]
155 $ctext tag remove sel 1.0 end
158 $ctext tag add sel $match "$match + $mlen c"
163 method _mark_range {first last} {
166 set match [_do_search $this $mend mlen -forwards $last.end]
167 if {$match eq {}} break
168 set mend "$match + $mlen c"
169 $ctext tag add found $match $mend
173 method _set_marks {doall} {
174 set topline [lindex [split [$ctext index @0,0] .] 0]
175 set botline [lindex [split [$ctext index @0,[winfo height $ctext]] .] 0]
176 if {$doall || $botline < $smarktop || $topline > $smarkbot} {
177 # no overlap with previous
178 _mark_range $this $topline $botline
179 set smarktop $topline
180 set smarkbot $botline
182 if {$topline < $smarktop} {
183 _mark_range $this $topline [expr {$smarktop-1}]
184 set smarktop $topline
186 if {$botline > $smarkbot} {
187 _mark_range $this [expr {$smarkbot+1}] $botline
188 set smarkbot $botline
194 if {$searchstring ne {}} {
195 after idle [cb _set_marks 0]