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
]
38 bind $w.ent
<Return
> [cb find_next
]
39 bind $w.ent
<Shift-Return
> [cb find_prev
]
41 bind $w <Destroy
> [list delete_this
$this]
46 if {![visible
$this]} {
53 if {[visible
$this]} {
60 return [winfo ismapped
$w]
67 method _get_new_anchor
{} {
68 # use start of selection if it is visible,
69 # or the bounds of the visible area
70 set top
[$ctext index
@0,0]
71 set bottom
[$ctext index
@0,[winfo height
$ctext]]
72 set sel
[$ctext tag ranges sel
]
74 set spos
[lindex $sel 0]
75 if {[lindex $spos 0] >= [lindex $top 0] &&
76 [lindex $spos 0] <= [lindex $bottom 0]} {
80 if {$searchdirn eq
"-forwards"} {
87 method _get_wrap_anchor
{dir
} {
88 if {$dir eq
"-forwards"} {
95 method _do_search
{start
{mlenvar
{}} {dir
{}} {endbound
{}}} {
96 set cmd
[list $ctext search
]
99 lappend cmd
-count mlen
101 if {!$casesensitive} {
107 lappend cmd
$dir -- $searchstring
108 if {$endbound ne
{}} {
109 set here
[eval $cmd [list $start] [list $endbound]]
111 set here
[eval $cmd [list $start]]
113 set here
[eval $cmd [_get_wrap_anchor
$this $dir]]
119 method _incrsearch_cb
{name ix op
} {
120 after idle
[cb _incrsearch
]
123 method _incrsearch
{} {
124 $ctext tag remove found
1.0 end
125 if {[catch {$ctext index anchor
}]} {
126 $ctext mark
set anchor
[_get_new_anchor
$this]
128 if {$searchstring ne
{}} {
129 set here
[_do_search
$this anchor mlen
]
132 $ctext tag remove sel
1.0 end
133 $ctext tag add sel
$here "$here + $mlen c"
134 $w.ent configure
-background lightgreen
137 $w.ent configure
-background lightpink
142 method find_prev
{} {
143 find_next
$this -backwards
146 method find_next
{{dir
-forwards}} {
150 $ctext mark
unset anchor
151 if {$searchstring ne
{}} {
152 set start
[_get_new_anchor
$this]
153 if {$dir eq
"-forwards"} {
154 set start
"$start + 1c"
156 set match
[_do_search
$this $start mlen
]
157 $ctext tag remove sel
1.0 end
160 $ctext tag add sel
$match "$match + $mlen c"
165 method _mark_range
{first last
} {
168 set match
[_do_search
$this $mend mlen
-forwards $last.end
]
169 if {$match eq
{}} break
170 set mend
"$match + $mlen c"
171 $ctext tag add found
$match $mend
175 method _set_marks
{doall
} {
176 set topline
[lindex [split [$ctext index
@0,0] .
] 0]
177 set botline
[lindex [split [$ctext index
@0,[winfo height
$ctext]] .
] 0]
178 if {$doall ||
$botline < $smarktop ||
$topline > $smarkbot} {
179 # no overlap with previous
180 _mark_range
$this $topline $botline
181 set smarktop
$topline
182 set smarkbot
$botline
184 if {$topline < $smarktop} {
185 _mark_range
$this $topline [expr {$smarktop-1}]
186 set smarktop
$topline
188 if {$botline > $smarkbot} {
189 _mark_range
$this [expr {$smarkbot+1}] $botline
190 set smarkbot
$botline
196 if {$searchstring ne
{}} {
197 after idle
[cb _set_marks
0]