@matheusbsbs / matheus-bagliano / commits / 4fb0fa197e
Fork 0

gitk: Fix the find and highlight functions

This reworks the way that the "Find" button (and the /, ?, ^F, and ^G keys) works. Previously, pressing the "Find" button would cause gitk to go off and scan through every commit to see which commits matched, and the user interface was completely unreponsive during that time. Now the searching is done in chunks using the scheduler, so the UI still responds, and the search stops as soon as a matching commit is found. The highlighting of matches using a yellow background is now done in the commit-drawing code and the highlighting code. This ensures that all the commits that are visible that match are highlighted without the search code having to find them all. This also fixes a bug where previously-drawn commits that need to be highlighted were not being highlighted. Signed-off-by: Paul Mackerras <paulus@samba.org>

Paul Mackerras committed Jul 4, 2007 at 19:43 UTC 4fb0fa197e14c82d64adb292320f9444d7ac46c5
1 file changed +185 -138
gitk
+185 -138
@@ -1758,7 +1758,7 @@ proc showview {n} {
1758 global colormap rowtextx commitrow nextcolor canvxmax
1759 global numcommits rowrangelist commitlisted idrowranges rowchk
1760 global selectedline currentid canv canvy0
1761 - global matchinglines treediffs
1761 + global treediffs
1762 global pending_select phase
1763 global commitidx rowlaidout rowoptim
1764 global commfd
@@ -1802,7 +1802,6 @@ proc showview {n} {
1802 [list {} $rowidlist $rowoffsets $rowrangelist]
1803 }
1804 }
1805 - catch {unset matchinglines}
1805 catch {unset treediffs}
1806 clear_display
1807 if {[info exists hlview] && $hlview == $n} {
@@ -2132,7 +2131,7 @@ proc readfhighlight {} {
2131
2132 proc find_change {name ix op} {
2133 global nhighlights mainfont boldnamerows
2135 - global findstring findpattern findtype
2134 + global findstring findpattern findtype markingmatches
2135
2136 # delete previous highlights, if any
2137 foreach row $boldnamerows {
@@ -2141,17 +2140,32 @@ proc find_change {name ix op} {
2140 set boldnamerows {}
2141 catch {unset nhighlights}
2142 unbolden
2143 + unmarkmatches
2144 if {$findtype ne "Regexp"} {
2145 set e [string map {"*" "\\*" "?" "\\?" "\[" "\\\[" "\\" "\\\\"} \
2146 $findstring]
2147 set findpattern "*$e*"
2148 }
2149 + set markingmatches [expr {$findstring ne {}}]
2150 drawvisible
2151 }
2152
2153 +proc doesmatch {f} {
2154 + global findtype findstring findpattern
2155 +
2156 + if {$findtype eq "Regexp"} {
2157 + return [regexp $findstring $f]
2158 + } elseif {$findtype eq "IgnCase"} {
2159 + return [string match -nocase $findpattern $f]
2160 + } else {
2161 + return [string match $findpattern $f]
2162 + }
2163 +}
2164 +
2165 proc askfindhighlight {row id} {
2166 global nhighlights commitinfo iddrawn mainfont
2154 - global findstring findtype findloc findpattern
2167 + global findloc
2168 + global markingmatches
2169
2170 if {![info exists commitinfo($id)]} {
2171 getcommit $id
@@ -2160,35 +2174,47 @@ proc askfindhighlight {row id} {
2174 set isbold 0
2175 set fldtypes {Headline Author Date Committer CDate Comments}
2176 foreach f $info ty $fldtypes {
2163 - if {$findloc ne "All fields" && $findloc ne $ty} {
2164 - continue
2165 - }
2166 - if {$findtype eq "Regexp"} {
2167 - set doesmatch [regexp $findstring $f]
2168 - } elseif {$findtype eq "IgnCase"} {
2169 - set doesmatch [string match -nocase $findpattern $f]
2170 - } else {
2171 - set doesmatch [string match $findpattern $f]
2172 - }
2173 - if {$doesmatch} {
2177 + if {($findloc eq "All fields" || $findloc eq $ty) &&
2178 + [doesmatch $f]} {
2179 if {$ty eq "Author"} {
2180 set isbold 2
2176 - } else {
2177 - set isbold 1
2181 + break
2182 }
2183 + set isbold 1
2184 }
2185 }
2181 - if {[info exists iddrawn($id)]} {
2182 - if {$isbold && ![ishighlighted $row]} {
2183 - bolden $row [concat $mainfont bold]
2186 + if {$isbold && [info exists iddrawn($id)]} {
2187 + set f [concat $mainfont bold]
2188 + if {![ishighlighted $row]} {
2189 + bolden $row $f
2190 + if {$isbold > 1} {
2191 + bolden_name $row $f
2192 + }
2193 }
2185 - if {$isbold >= 2} {
2186 - bolden_name $row [concat $mainfont bold]
2194 + if {$markingmatches} {
2195 + markrowmatches $row [lindex $info 0] [lindex $info 1]
2196 }
2197 }
2198 set nhighlights($row) $isbold
2199 }
2200
2201 +proc markrowmatches {row headline author} {
2202 + global canv canv2 linehtag linentag
2203 +
2204 + $canv delete match$row
2205 + $canv2 delete match$row
2206 + set m [findmatches $headline]
2207 + if {$m ne {}} {
2208 + markmatches $canv $row $headline $linehtag($row) $m \
2209 + [$canv itemcget $linehtag($row) -font]
2210 + }
2211 + set m [findmatches $author]
2212 + if {$m ne {}} {
2213 + markmatches $canv2 $row $author $linentag($row) $m \
2214 + [$canv2 itemcget $linentag($row) -font]
2215 + }
2216 +}
2217 +
2218 proc vrel_change {name ix op} {
2219 global highlight_related
2220
@@ -3309,7 +3335,7 @@ proc drawcmittext {id row col} {
3335 global linespc canv canv2 canv3 canvy0 fgcolor
3336 global commitlisted commitinfo rowidlist parentlist
3337 global rowtextx idpos idtags idheads idotherrefs
3312 - global linehtag linentag linedtag
3338 + global linehtag linentag linedtag markingmatches
3339 global mainfont canvxmax boldrows boldnamerows fgcolor nullid
3340
3341 if {$id eq $nullid} {
@@ -3366,6 +3392,9 @@ proc drawcmittext {id row col} {
3392 set linedtag($row) [$canv3 create text 3 $y -anchor w -fill $fgcolor \
3393 -text $date -font $mainfont -tags text]
3394 set xr [expr {$xt + [font measure $mainfont $headline]}]
3395 + if {$markingmatches} {
3396 + markrowmatches $row $headline $name
3397 + }
3398 if {$xr > $canvxmax} {
3399 set canvxmax $xr
3400 setcanvscroll
@@ -3436,9 +3465,7 @@ proc drawcommits {row {endrow {}}} {
3465 for {} {$r <= $er} {incr r} {
3466 set id [lindex $displayorder $r]
3467 set wasdrawn [info exists iddrawn($id)]
3439 - if {!$wasdrawn} {
3440 - drawcmitrow $r
3441 - }
3468 + drawcmitrow $r
3469 if {$r == $er} break
3470 set nextid [lindex $displayorder [expr {$r + 1}]]
3471 if {$wasdrawn && [info exists iddrawn($nextid)]} {
@@ -3889,101 +3916,166 @@ proc notbusy {what} {
3916 }
3917
3918 proc findmatches {f} {
3892 - global findtype foundstring foundstrlen
3919 + global findtype findstring
3920 if {$findtype == "Regexp"} {
3894 - set matches [regexp -indices -all -inline $foundstring $f]
3921 + set matches [regexp -indices -all -inline $findstring $f]
3922 } else {
3923 + set fs $findstring
3924 if {$findtype == "IgnCase"} {
3897 - set str [string tolower $f]
3898 - } else {
3899 - set str $f
3925 + set f [string tolower $f]
3926 + set fs [string tolower $fs]
3927 }
3928 set matches {}
3929 set i 0
3903 - while {[set j [string first $foundstring $str $i]] >= 0} {
3904 - lappend matches [list $j [expr {$j+$foundstrlen-1}]]
3905 - set i [expr {$j + $foundstrlen}]
3930 + set l [string length $fs]
3931 + while {[set j [string first $fs $f $i]] >= 0} {
3932 + lappend matches [list $j [expr {$j+$l-1}]]
3933 + set i [expr {$j + $l}]
3934 }
3935 }
3936 return $matches
3937 }
3938
3911 -proc dofind {} {
3912 - global findtype findloc findstring markedmatches commitinfo
3913 - global numcommits displayorder linehtag linentag linedtag
3914 - global mainfont canv canv2 canv3 selectedline
3915 - global matchinglines foundstring foundstrlen matchstring
3916 - global commitdata
3939 +proc dofind {{rev 0}} {
3940 + global findstring findstartline findcurline selectedline numcommits
3941
3942 stopfindproc
3943 unmarkmatches
3944 cancel_next_highlight
3945 focus .
3922 - set matchinglines {}
3923 - if {$findtype == "IgnCase"} {
3924 - set foundstring [string tolower $findstring]
3946 + if {$findstring eq {} || $numcommits == 0} return
3947 + if {![info exists selectedline]} {
3948 + set findstartline [lindex [visiblerows] $rev]
3949 } else {
3926 - set foundstring $findstring
3950 + set findstartline $selectedline
3951 }
3928 - set foundstrlen [string length $findstring]
3929 - if {$foundstrlen == 0} return
3930 - regsub -all {[*?\[\\]} $foundstring {\\&} matchstring
3931 - set matchstring "*$matchstring*"
3932 - if {![info exists selectedline]} {
3933 - set oldsel -1
3952 + set findcurline $findstartline
3953 + nowbusy finding
3954 + if {!$rev} {
3955 + run findmore
3956 } else {
3935 - set oldsel $selectedline
3957 + set findcurline $findstartline
3958 + if {$findcurline == 0} {
3959 + set findcurline $numcommits
3960 + }
3961 + incr findcurline -1
3962 + run findmorerev
3963 }
3937 - set didsel 0
3938 - set fldtypes {Headline Author Date Committer CDate Comments}
3939 - set l -1
3940 - foreach id $displayorder {
3941 - set d $commitdata($id)
3942 - incr l
3943 - if {$findtype == "Regexp"} {
3944 - set doesmatch [regexp $foundstring $d]
3945 - } elseif {$findtype == "IgnCase"} {
3946 - set doesmatch [string match -nocase $matchstring $d]
3964 +}
3965 +
3966 +proc findnext {restart} {
3967 + global findcurline
3968 + if {![info exists findcurline]} {
3969 + if {$restart} {
3970 + dofind
3971 } else {
3948 - set doesmatch [string match $matchstring $d]
3972 + bell
3973 }
3950 - if {!$doesmatch} continue
3974 + } else {
3975 + run findmore
3976 + nowbusy finding
3977 + }
3978 +}
3979 +
3980 +proc findprev {} {
3981 + global findcurline
3982 + if {![info exists findcurline]} {
3983 + dofind 1
3984 + } else {
3985 + run findmorerev
3986 + nowbusy finding
3987 + }
3988 +}
3989 +
3990 +proc findmore {} {
3991 + global commitdata commitinfo numcommits findstring findpattern findloc
3992 + global findstartline findcurline markingmatches displayorder
3993 +
3994 + set fldtypes {Headline Author Date Committer CDate Comments}
3995 + set l [expr {$findcurline + 1}]
3996 + if {$l >= $numcommits} {
3997 + set l 0
3998 + }
3999 + if {$l <= $findstartline} {
4000 + set lim [expr {$findstartline + 1}]
4001 + } else {
4002 + set lim $numcommits
4003 + }
4004 + if {$lim - $l > 500} {
4005 + set lim [expr {$l + 500}]
4006 + }
4007 + set last 0
4008 + for {} {$l < $lim} {incr l} {
4009 + set id [lindex $displayorder $l]
4010 + if {![doesmatch $commitdata($id)]} continue
4011 if {![info exists commitinfo($id)]} {
4012 getcommit $id
4013 }
4014 set info $commitinfo($id)
3955 - set doesmatch 0
4015 foreach f $info ty $fldtypes {
3957 - if {$findloc != "All fields" && $findloc != $ty} {
3958 - continue
3959 - }
3960 - set matches [findmatches $f]
3961 - if {$matches == {}} continue
3962 - set doesmatch 1
3963 - if {$ty == "Headline"} {
3964 - drawcommits $l
3965 - markmatches $canv $l $f $linehtag($l) $matches $mainfont
3966 - } elseif {$ty == "Author"} {
3967 - drawcommits $l
3968 - markmatches $canv2 $l $f $linentag($l) $matches $mainfont
3969 - } elseif {$ty == "Date"} {
3970 - drawcommits $l
3971 - markmatches $canv3 $l $f $linedtag($l) $matches $mainfont
4016 + if {($findloc eq "All fields" || $findloc eq $ty) &&
4017 + [doesmatch $f]} {
4018 + set markingmatches 1
4019 + findselectline $l
4020 + notbusy finding
4021 + return 0
4022 }
4023 }
3974 - if {$doesmatch} {
3975 - lappend matchinglines $l
3976 - if {!$didsel && $l > $oldsel} {
4024 + }
4025 + if {$l == $findstartline + 1} {
4026 + bell
4027 + unset findcurline
4028 + notbusy finding
4029 + return 0
4030 + }
4031 + set findcurline [expr {$l - 1}]
4032 + return 1
4033 +}
4034 +
4035 +proc findmorerev {} {
4036 + global commitdata commitinfo numcommits findstring findpattern findloc
4037 + global findstartline findcurline markingmatches displayorder
4038 +
4039 + set fldtypes {Headline Author Date Committer CDate Comments}
4040 + set l $findcurline
4041 + if {$l == 0} {
4042 + set l $numcommits
4043 + }
4044 + incr l -1
4045 + if {$l >= $findstartline} {
4046 + set lim [expr {$findstartline - 1}]
4047 + } else {
4048 + set lim -1
4049 + }
4050 + if {$l - $lim > 500} {
4051 + set lim [expr {$l - 500}]
4052 + }
4053 + set last 0
4054 + for {} {$l > $lim} {incr l -1} {
4055 + set id [lindex $displayorder $l]
4056 + if {![doesmatch $commitdata($id)]} continue
4057 + if {![info exists commitinfo($id)]} {
4058 + getcommit $id
4059 + }
4060 + set info $commitinfo($id)
4061 + foreach f $info ty $fldtypes {
4062 + if {($findloc eq "All fields" || $findloc eq $ty) &&
4063 + [doesmatch $f]} {
4064 + set markingmatches 1
4065 findselectline $l
3978 - set didsel 1
4066 + notbusy finding
4067 + return 0
4068 }
4069 }
4070 }
3982 - if {$matchinglines == {}} {
4071 + if {$l == -1} {
4072 bell
3984 - } elseif {!$didsel} {
3985 - findselectline [lindex $matchinglines 0]
4073 + unset findcurline
4074 + notbusy finding
4075 + return 0
4076 }
4077 + set findcurline [expr {$l + 1}]
4078 + return 1
4079 }
4080
4081 proc findselectline {l} {
@@ -4001,43 +4093,6 @@ proc findselectline {l} {
4093 }
4094 }
4095
4004 -proc findnext {restart} {
4005 - global matchinglines selectedline
4006 - if {![info exists matchinglines]} {
4007 - if {$restart} {
4008 - dofind
4009 - }
4010 - return
4011 - }
4012 - if {![info exists selectedline]} return
4013 - foreach l $matchinglines {
4014 - if {$l > $selectedline} {
4015 - findselectline $l
4016 - return
4017 - }
4018 - }
4019 - bell
4020 -}
4021 -
4022 -proc findprev {} {
4023 - global matchinglines selectedline
4024 - if {![info exists matchinglines]} {
4025 - dofind
4026 - return
4027 - }
4028 - if {![info exists selectedline]} return
4029 - set prev {}
4030 - foreach l $matchinglines {
4031 - if {$l >= $selectedline} break
4032 - set prev $l
4033 - }
4034 - if {$prev != {}} {
4035 - findselectline $prev
4036 - } else {
4037 - bell
4038 - }
4039 -}
4040 -
4096 proc stopfindproc {{done 0}} {
4097 global findprocpid findprocfile findids
4098 global ctext findoldcursor phase maincursor textcursor
@@ -4055,18 +4110,7 @@ proc stopfindproc {{done 0}} {
4110 notbusy find
4111 }
4112
4058 -# mark a commit as matching by putting a yellow background
4059 -# behind the headline
4060 -proc markheadline {l id} {
4061 - global canv mainfont linehtag
4062 -
4063 - drawcommits $l
4064 - set bbox [$canv bbox $linehtag($l)]
4065 - set t [$canv create rect $bbox -outline {} -tags matches -fill yellow]
4066 - $canv lower $t
4067 -}
4068 -
4069 -# mark the bits of a headline, author or date that match a find string
4113 +# mark the bits of a headline or author that match a find string
4114 proc markmatches {canv l str tag matches font} {
4115 set bbox [$canv bbox $tag]
4116 set x0 [lindex $bbox 0]
@@ -4080,16 +4124,18 @@ proc markmatches {canv l str tag matches font} {
4124 set xlen [font measure $font [string range $str 0 [expr {$end}]]]
4125 set t [$canv create rect [expr {$x0+$xoff}] $y0 \
4126 [expr {$x0+$xlen+2}] $y1 \
4083 - -outline {} -tags matches -fill yellow]
4127 + -outline {} -tags [list match$l matches] -fill yellow]
4128 $canv lower $t
4129 }
4130 }
4131
4132 proc unmarkmatches {} {
4089 - global matchinglines findids
4133 + global findids markingmatches findcurline
4134 +
4135 allcanvs delete matches
4091 - catch {unset matchinglines}
4136 catch {unset findids}
4137 + set markingmatches 0
4138 + catch {unset findcurline}
4139 }
4140
4141 proc selcanvline {w x y} {
@@ -7471,6 +7517,7 @@ set searchdirn -forwards
7517 set boldrows {}
7518 set boldnamerows {}
7519 set diffelide {0 0}
7520 +set markingmatches 0
7521
7522 set optim_delay 16
7523