git-gui: add search history to searchbar
Use the up/down keys to browse the history. Signed-off-by: Bert Wesarg <bert.wesarg@googlemail.com> Signed-off-by: Pat Thoyts <patthoyts@users.sourceforge.net>
Bert Wesarg committed
Oct 14, 2011 at 10:14 UTC
1159971baa28ed3a9934cf92f4b22e9d0681bdce
1 file changed
+60
lib/search.tcl
+60
@@ -13,6 +13,9 @@ field casesensitive
13
field default_casesensitive
14
field searchdirn -forwards
15
16
+field history
17
+field history_index
18
+
19
field smarktop
20
field smarkbot
21
@@ -28,6 +31,8 @@ constructor new {i_w i_text args} {
31
set default_casesensitive 1
32
}
33
34
+ set history [list]
35
+
36
${NS}::frame $w
37
${NS}::label $w.l -text [mc Find:]
38
entry $w.ent -textvariable ${__this}::searchstring -background lightgreen
@@ -50,6 +55,8 @@ constructor new {i_w i_text args} {
55
trace add variable searchstring write [cb _incrsearch_cb]
56
bind $w.ent <Return> [cb find_next]
57
bind $w.ent <Shift-Return> [cb find_prev]
58
+ bind $w.ent <Key-Up> [cb _prev_search]
59
+ bind $w.ent <Key-Down> [cb _next_search]
60
61
bind $w <Destroy> [list delete_this $this]
62
return $this
@@ -58,8 +65,10 @@ constructor new {i_w i_text args} {
65
method show {} {
66
if {![visible $this]} {
67
grid $w
68
+ $w.ent delete 0 end
69
set regexpsearch $default_regexpsearch
70
set casesensitive $default_casesensitive
71
+ set history_index [llength $history]
72
}
73
focus -force $w.ent
74
}
@@ -68,6 +77,7 @@ method hide {} {
77
if {[visible $this]} {
78
focus $ctext
79
grid remove $w
80
+ _save_search $this
81
}
82
}
83
@@ -160,6 +170,55 @@ method _incrsearch {} {
170
}
171
}
172
173
+method _save_search {} {
174
+ if {$searchstring eq {}} {
175
+ return
176
+ }
177
+ if {[llength $history] > 0} {
178
+ foreach {s_regexp s_case s_expr} [lindex $history end] break
179
+ } else {
180
+ set s_regexp $regexpsearch
181
+ set s_case $casesensitive
182
+ set s_expr ""
183
+ }
184
+ if {$searchstring eq $s_expr} {
185
+ # update modes
186
+ set history [lreplace $history end end \
187
+ [list $regexpsearch $casesensitive $searchstring]]
188
+ } else {
189
+ lappend history [list $regexpsearch $casesensitive $searchstring]
190
+ }
191
+ set history_index [llength $history]
192
+}
193
+
194
+method _prev_search {} {
195
+ if {$history_index > 0} {
196
+ incr history_index -1
197
+ foreach {s_regexp s_case s_expr} [lindex $history $history_index] break
198
+ $w.ent delete 0 end
199
+ $w.ent insert 0 $s_expr
200
+ set regexpsearch $s_regexp
201
+ set casesensitive $s_case
202
+ }
203
+}
204
+
205
+method _next_search {} {
206
+ if {$history_index < [llength $history]} {
207
+ incr history_index
208
+ }
209
+ if {$history_index < [llength $history]} {
210
+ foreach {s_regexp s_case s_expr} [lindex $history $history_index] break
211
+ } else {
212
+ set s_regexp $default_regexpsearch
213
+ set s_case $default_casesensitive
214
+ set s_expr ""
215
+ }
216
+ $w.ent delete 0 end
217
+ $w.ent insert 0 $s_expr
218
+ set regexpsearch $s_regexp
219
+ set casesensitive $s_case
220
+}
221
+
222
method find_prev {} {
223
find_next $this -backwards
224
}
@@ -170,6 +229,7 @@ method find_next {{dir -forwards}} {
229
set searchdirn $dir
230
$ctext mark unset anchor
231
if {$searchstring ne {}} {
232
+ _save_search $this
233
set start [_get_new_anchor $this]
234
if {$dir eq "-forwards"} {
235
set start "$start + 1c"