1 # incremental search panel
2 # based on code from gitk, Copyright (C) Paul Mackerras
11 field default_regexpsearch
13 field default_casesensitive
15 field searchdirn -forwards
23 constructor new {i_w i_text args} {
28 set default_regexpsearch [is_config_true gui.search.regexp]
29 switch -- [get_config gui.search.case] {
31 set default_casesensitive 0
35 set default_casesensitive 0
40 set default_casesensitive 1
48 ${NS}::label $w.l -text [mc Find:]
49 tentry $w.ent -textvariable ${__this}::searchstring -background lightgreen
50 ${NS}::button $w.bn -text [mc Next] -command [cb find_next]
51 ${NS}::button $w.bp -text [mc Prev] -command [cb find_prev]
52 ${NS}::checkbutton $w.re -text [mc RegExp] \
53 -variable ${__this}::regexpsearch -command [cb _incrsearch]
54 ${NS}::checkbutton $w.cs -text [mc Case] \
55 -variable ${__this}::casesensitive -command [cb _incrsearch]
57 pack $w.cs -side right
58 pack $w.re -side right
59 pack $w.bp -side right
60 pack $w.bn -side right
61 pack $w.ent -side left -expand 1 -fill x
63 eval grid conf $w -sticky we $args
66 trace add variable searchstring write [cb _incrsearch_cb]
67 bind $w.ent <Return> [cb find_next]
68 bind $w.ent <Shift-Return> [cb find_prev]
69 bind $w.ent <Key-Up> [cb _prev_search]
70 bind $w.ent <Key-Down> [cb _next_search]
72 bind $w <Destroy> [list delete_this $this]
77 if {![visible $this]} {
80 set regexpsearch $default_regexpsearch
81 set casesensitive $default_casesensitive
82 set history_index [llength $history]
88 if {[visible $this]} {
96 return [winfo ismapped $w]
103 method _get_new_anchor {} {
104 # use start of selection if it is visible,
105 # or the bounds of the visible area
106 set top [$ctext index @0,0]
107 set bottom [$ctext index @0,[winfo height $ctext]]
108 set sel [$ctext tag ranges sel]
110 set spos [lindex $sel 0]
111 if {[lindex $spos 0] >= [lindex $top 0] &&
112 [lindex $spos 0] <= [lindex $bottom 0]} {
116 if {$searchdirn eq "-forwards"} {
123 method _get_wrap_anchor {dir} {
124 if {$dir eq "-forwards"} {
131 method _do_search {start {mlenvar {}} {dir {}} {endbound {}}} {
132 set cmd [list $ctext search]
133 if {$mlenvar ne {}} {
135 lappend cmd -count mlen
140 if {!$casesensitive} {
146 lappend cmd $dir -- $searchstring
148 if {$endbound ne {}} {
149 set here [eval $cmd [list $start] [list $endbound]]
151 set here [eval $cmd [list $start]]
153 set here [eval $cmd [_get_wrap_anchor $this $dir]]
156 } err]} { set here {} }
160 method _incrsearch_cb {name ix op} {
161 after idle [cb _incrsearch]
164 method _incrsearch {} {
165 $ctext tag remove found 1.0 end
166 if {[catch {$ctext index anchor}]} {
167 $ctext mark set anchor [_get_new_anchor $this]
169 if {$searchstring ne {}} {
170 if {$smartcase && [regexp {[[:upper:]]} $searchstring]} {
173 set here [_do_search $this anchor mlen]
176 $ctext tag remove sel 1.0 end
177 $ctext tag add sel $here "$here + $mlen c"
178 #$w.ent configure -background lightgreen
179 $w.ent state !pressed
182 #$w.ent configure -background lightpink
185 } elseif {$smartcase} {
186 # clearing the field resets the smart case detection
191 method _save_search {} {
192 if {$searchstring eq {}} {
195 if {[llength $history] > 0} {
196 foreach {s_regexp s_case s_expr} [lindex $history end] break
198 set s_regexp $regexpsearch
199 set s_case $casesensitive
202 if {$searchstring eq $s_expr} {
204 set history [lreplace $history end end \
205 [list $regexpsearch $casesensitive $searchstring]]
207 lappend history [list $regexpsearch $casesensitive $searchstring]
209 set history_index [llength $history]
212 method _prev_search {} {
213 if {$history_index > 0} {
214 incr history_index -1
215 foreach {s_regexp s_case s_expr} [lindex $history $history_index] break
217 $w.ent insert 0 $s_expr
218 set regexpsearch $s_regexp
219 set casesensitive $s_case
223 method _next_search {} {
224 if {$history_index < [llength $history]} {
227 if {$history_index < [llength $history]} {
228 foreach {s_regexp s_case s_expr} [lindex $history $history_index] break
230 set s_regexp $default_regexpsearch
231 set s_case $default_casesensitive
235 $w.ent insert 0 $s_expr
236 set regexpsearch $s_regexp
237 set casesensitive $s_case
240 method find_prev {} {
241 find_next $this -backwards
244 method find_next {{dir -forwards}} {
248 $ctext mark unset anchor
249 if {$searchstring ne {}} {
251 set start [_get_new_anchor $this]
252 if {$dir eq "-forwards"} {
253 set start "$start + 1c"
255 set match [_do_search $this $start mlen]
256 $ctext tag remove sel 1.0 end
259 $ctext tag add sel $match "$match + $mlen c"
264 method _mark_range {first last} {
267 set match [_do_search $this $mend mlen -forwards $last.end]
268 if {$match eq {}} break
269 set mend "$match + $mlen c"
270 $ctext tag add found $match $mend
274 method _set_marks {doall} {
275 set topline [lindex [split [$ctext index @0,0] .] 0]
276 set botline [lindex [split [$ctext index @0,[winfo height $ctext]] .] 0]
277 if {$doall || $botline < $smarktop || $topline > $smarkbot} {
278 # no overlap with previous
279 _mark_range $this $topline $botline
280 set smarktop $topline
281 set smarkbot $botline
283 if {$topline < $smarktop} {
284 _mark_range $this $topline [expr {$smarktop-1}]
285 set smarktop $topline
287 if {$botline > $smarkbot} {
288 _mark_range $this [expr {$smarkbot+1}] $botline
289 set smarkbot $botline
295 if {$searchstring ne {}} {
296 after idle [cb _set_marks 0]