]> git.ipfire.org Git - thirdparty/git.git/blame - gitk-git/gitk
Sync with 1.7.8.5
[thirdparty/git.git] / gitk-git / gitk
CommitLineData
1db95b00
PM
1#!/bin/sh
2# Tcl ignores the next line -*- tcl -*- \
9e026d39 3exec wish "$0" -- "$@"
1db95b00 4
bb3e86a1 5# Copyright © 2005-2011 Paul Mackerras. All rights reserved.
1db95b00
PM
6# This program is free software; it may be used, copied, modified
7# and distributed under the terms of the GNU General Public Licence,
8# either version 2, or (at your option) any later version.
9
d93f1713
PT
10package require Tk
11
74cb884f
MZ
12proc hasworktree {} {
13 return [expr {[exec git rev-parse --is-bare-repository] == "false" &&
14 [exec git rev-parse --is-inside-git-dir] == "false"}]
15}
16
7eb3cb9c
PM
17# A simple scheduler for compute-intensive stuff.
18# The aim is to make sure that event handlers for GUI actions can
19# run at least every 50-100 ms. Unfortunately fileevent handlers are
20# run before X event handlers, so reading from a fast source can
21# make the GUI completely unresponsive.
22proc run args {
df75e86d 23 global isonrunq runq currunq
7eb3cb9c
PM
24
25 set script $args
26 if {[info exists isonrunq($script)]} return
df75e86d 27 if {$runq eq {} && ![info exists currunq]} {
7eb3cb9c
PM
28 after idle dorunq
29 }
30 lappend runq [list {} $script]
31 set isonrunq($script) 1
32}
33
34proc filerun {fd script} {
35 fileevent $fd readable [list filereadable $fd $script]
36}
37
38proc filereadable {fd script} {
df75e86d 39 global runq currunq
7eb3cb9c
PM
40
41 fileevent $fd readable {}
df75e86d 42 if {$runq eq {} && ![info exists currunq]} {
7eb3cb9c
PM
43 after idle dorunq
44 }
45 lappend runq [list $fd $script]
46}
47
7fcc92bf
PM
48proc nukefile {fd} {
49 global runq
50
51 for {set i 0} {$i < [llength $runq]} {} {
52 if {[lindex $runq $i 0] eq $fd} {
53 set runq [lreplace $runq $i $i]
54 } else {
55 incr i
56 }
57 }
58}
59
7eb3cb9c 60proc dorunq {} {
df75e86d 61 global isonrunq runq currunq
7eb3cb9c
PM
62
63 set tstart [clock clicks -milliseconds]
64 set t0 $tstart
7fcc92bf 65 while {[llength $runq] > 0} {
7eb3cb9c
PM
66 set fd [lindex $runq 0 0]
67 set script [lindex $runq 0 1]
df75e86d
AG
68 set currunq [lindex $runq 0]
69 set runq [lrange $runq 1 end]
7eb3cb9c 70 set repeat [eval $script]
df75e86d 71 unset currunq
7eb3cb9c
PM
72 set t1 [clock clicks -milliseconds]
73 set t [expr {$t1 - $t0}]
7eb3cb9c
PM
74 if {$repeat ne {} && $repeat} {
75 if {$fd eq {} || $repeat == 2} {
76 # script returns 1 if it wants to be readded
77 # file readers return 2 if they could do more straight away
78 lappend runq [list $fd $script]
79 } else {
80 fileevent $fd readable [list filereadable $fd $script]
81 }
82 } elseif {$fd eq {}} {
83 unset isonrunq($script)
84 }
85 set t0 $t1
86 if {$t1 - $tstart >= 80} break
87 }
88 if {$runq ne {}} {
89 after idle dorunq
90 }
91}
92
e439e092
AG
93proc reg_instance {fd} {
94 global commfd leftover loginstance
95
96 set i [incr loginstance]
97 set commfd($i) $fd
98 set leftover($i) {}
99 return $i
100}
101
3ed31a81
PM
102proc unmerged_files {files} {
103 global nr_unmerged
104
105 # find the list of unmerged files
106 set mlist {}
107 set nr_unmerged 0
108 if {[catch {
109 set fd [open "| git ls-files -u" r]
110 } err]} {
111 show_error {} . "[mc "Couldn't get list of unmerged files:"] $err"
112 exit 1
113 }
114 while {[gets $fd line] >= 0} {
115 set i [string first "\t" $line]
116 if {$i < 0} continue
117 set fname [string range $line [expr {$i+1}] end]
118 if {[lsearch -exact $mlist $fname] >= 0} continue
119 incr nr_unmerged
120 if {$files eq {} || [path_filter $files $fname]} {
121 lappend mlist $fname
122 }
123 }
124 catch {close $fd}
125 return $mlist
126}
127
128proc parseviewargs {n arglist} {
c2f2dab9 129 global vdatemode vmergeonly vflags vdflags vrevs vfiltered vorigargs env
ae4e3ff9 130 global worddiff git_version
3ed31a81
PM
131
132 set vdatemode($n) 0
133 set vmergeonly($n) 0
ee66e089
PM
134 set glflags {}
135 set diffargs {}
136 set nextisval 0
137 set revargs {}
138 set origargs $arglist
139 set allknown 1
140 set filtered 0
141 set i -1
142 foreach arg $arglist {
143 incr i
144 if {$nextisval} {
145 lappend glflags $arg
146 set nextisval 0
147 continue
148 }
3ed31a81
PM
149 switch -glob -- $arg {
150 "-d" -
151 "--date-order" {
152 set vdatemode($n) 1
ee66e089
PM
153 # remove from origargs in case we hit an unknown option
154 set origargs [lreplace $origargs $i $i]
155 incr i -1
156 }
ee66e089
PM
157 "-[puabwcrRBMC]" -
158 "--no-renames" - "--full-index" - "--binary" - "--abbrev=*" -
159 "--find-copies-harder" - "-l*" - "--ext-diff" - "--no-ext-diff" -
160 "--src-prefix=*" - "--dst-prefix=*" - "--no-prefix" -
161 "-O*" - "--text" - "--full-diff" - "--ignore-space-at-eol" -
162 "--ignore-space-change" - "-U*" - "--unified=*" {
29582284
PM
163 # These request or affect diff output, which we don't want.
164 # Some could be used to set our defaults for diff display.
ee66e089
PM
165 lappend diffargs $arg
166 }
ee66e089 167 "--raw" - "--patch-with-raw" - "--patch-with-stat" -
ae4e3ff9 168 "--name-only" - "--name-status" - "--color" -
ee66e089
PM
169 "--log-size" - "--pretty=*" - "--decorate" - "--abbrev-commit" -
170 "--cc" - "-z" - "--header" - "--parents" - "--boundary" -
171 "--no-color" - "-g" - "--walk-reflogs" - "--no-walk" -
172 "--timestamp" - "relative-date" - "--date=*" - "--stdin" -
173 "--objects" - "--objects-edge" - "--reverse" {
29582284
PM
174 # These cause our parsing of git log's output to fail, or else
175 # they're options we want to set ourselves, so ignore them.
ee66e089 176 }
ae4e3ff9
TR
177 "--color-words*" - "--word-diff=color" {
178 # These trigger a word diff in the console interface,
179 # so help the user by enabling our own support
180 if {[package vcompare $git_version "1.7.2"] >= 0} {
181 set worddiff [mc "Color words"]
182 }
183 }
184 "--word-diff*" {
185 if {[package vcompare $git_version "1.7.2"] >= 0} {
186 set worddiff [mc "Markup words"]
187 }
188 }
ee66e089
PM
189 "--stat=*" - "--numstat" - "--shortstat" - "--summary" -
190 "--check" - "--exit-code" - "--quiet" - "--topo-order" -
191 "--full-history" - "--dense" - "--sparse" -
192 "--follow" - "--left-right" - "--encoding=*" {
29582284 193 # These are harmless, and some are even useful
ee66e089
PM
194 lappend glflags $arg
195 }
ee66e089
PM
196 "--diff-filter=*" - "--no-merges" - "--unpacked" -
197 "--max-count=*" - "--skip=*" - "--since=*" - "--after=*" -
198 "--until=*" - "--before=*" - "--max-age=*" - "--min-age=*" -
199 "--author=*" - "--committer=*" - "--grep=*" - "-[iE]" -
200 "--remove-empty" - "--first-parent" - "--cherry-pick" -
f687aaa8
DS
201 "-S*" - "--pickaxe-all" - "--pickaxe-regex" -
202 "--simplify-by-decoration" {
29582284 203 # These mean that we get a subset of the commits
ee66e089
PM
204 set filtered 1
205 lappend glflags $arg
206 }
ee66e089 207 "-n" {
29582284
PM
208 # This appears to be the only one that has a value as a
209 # separate word following it
ee66e089
PM
210 set filtered 1
211 set nextisval 1
212 lappend glflags $arg
213 }
6e7e87c7 214 "--not" - "--all" {
ee66e089 215 lappend revargs $arg
3ed31a81
PM
216 }
217 "--merge" {
218 set vmergeonly($n) 1
ee66e089
PM
219 # git rev-parse doesn't understand --merge
220 lappend revargs --gitk-symmetric-diff-marker MERGE_HEAD...HEAD
221 }
c2f2dab9
CC
222 "--no-replace-objects" {
223 set env(GIT_NO_REPLACE_OBJECTS) "1"
224 }
ee66e089 225 "-*" {
29582284 226 # Other flag arguments including -<n>
ee66e089
PM
227 if {[string is digit -strict [string range $arg 1 end]]} {
228 set filtered 1
229 } else {
230 # a flag argument that we don't recognize;
231 # that means we can't optimize
232 set allknown 0
233 }
234 lappend glflags $arg
3ed31a81
PM
235 }
236 default {
29582284 237 # Non-flag arguments specify commits or ranges of commits
ee66e089
PM
238 if {[string match "*...*" $arg]} {
239 lappend revargs --gitk-symmetric-diff-marker
240 }
241 lappend revargs $arg
242 }
243 }
244 }
245 set vdflags($n) $diffargs
246 set vflags($n) $glflags
247 set vrevs($n) $revargs
248 set vfiltered($n) $filtered
249 set vorigargs($n) $origargs
250 return $allknown
251}
252
253proc parseviewrevs {view revs} {
254 global vposids vnegids
255
256 if {$revs eq {}} {
257 set revs HEAD
258 }
259 if {[catch {set ids [eval exec git rev-parse $revs]} err]} {
260 # we get stdout followed by stderr in $err
261 # for an unknown rev, git rev-parse echoes it and then errors out
262 set errlines [split $err "\n"]
263 set badrev {}
264 for {set l 0} {$l < [llength $errlines]} {incr l} {
265 set line [lindex $errlines $l]
266 if {!([string length $line] == 40 && [string is xdigit $line])} {
267 if {[string match "fatal:*" $line]} {
268 if {[string match "fatal: ambiguous argument*" $line]
269 && $badrev ne {}} {
270 if {[llength $badrev] == 1} {
271 set err "unknown revision $badrev"
272 } else {
273 set err "unknown revisions: [join $badrev ", "]"
274 }
275 } else {
276 set err [join [lrange $errlines $l end] "\n"]
277 }
278 break
279 }
280 lappend badrev $line
281 }
d93f1713 282 }
3945d2c0 283 error_popup "[mc "Error parsing revisions:"] $err"
ee66e089
PM
284 return {}
285 }
286 set ret {}
287 set pos {}
288 set neg {}
289 set sdm 0
290 foreach id [split $ids "\n"] {
291 if {$id eq "--gitk-symmetric-diff-marker"} {
292 set sdm 4
293 } elseif {[string match "^*" $id]} {
294 if {$sdm != 1} {
295 lappend ret $id
296 if {$sdm == 3} {
297 set sdm 0
298 }
299 }
300 lappend neg [string range $id 1 end]
301 } else {
302 if {$sdm != 2} {
303 lappend ret $id
304 } else {
2b1fbf90 305 lset ret end $id...[lindex $ret end]
3ed31a81 306 }
ee66e089 307 lappend pos $id
3ed31a81 308 }
ee66e089 309 incr sdm -1
3ed31a81 310 }
ee66e089
PM
311 set vposids($view) $pos
312 set vnegids($view) $neg
313 return $ret
3ed31a81
PM
314}
315
f9e0b6fb 316# Start off a git log process and arrange to read its output
da7c24dd 317proc start_rev_list {view} {
6df7403a 318 global startmsecs commitidx viewcomplete curview
e439e092 319 global tclencoding
ee66e089 320 global viewargs viewargscmd viewfiles vfilelimit
d375ef9b 321 global showlocalchanges
e439e092 322 global viewactive viewinstances vmergeonly
cdc8429c 323 global mainheadid viewmainheadid viewmainheadid_orig
ee66e089 324 global vcanopt vflags vrevs vorigargs
7defefb1 325 global show_notes
9ccbdfbf 326
9ccbdfbf 327 set startmsecs [clock clicks -milliseconds]
da7c24dd 328 set commitidx($view) 0
3ed31a81
PM
329 # these are set this way for the error exits
330 set viewcomplete($view) 1
331 set viewactive($view) 0
7fcc92bf
PM
332 varcinit $view
333
2d480856
YD
334 set args $viewargs($view)
335 if {$viewargscmd($view) ne {}} {
336 if {[catch {
337 set str [exec sh -c $viewargscmd($view)]
338 } err]} {
3945d2c0 339 error_popup "[mc "Error executing --argscmd command:"] $err"
3ed31a81 340 return 0
2d480856
YD
341 }
342 set args [concat $args [split $str "\n"]]
343 }
ee66e089 344 set vcanopt($view) [parseviewargs $view $args]
3ed31a81
PM
345
346 set files $viewfiles($view)
347 if {$vmergeonly($view)} {
348 set files [unmerged_files $files]
349 if {$files eq {}} {
350 global nr_unmerged
351 if {$nr_unmerged == 0} {
352 error_popup [mc "No files selected: --merge specified but\
353 no files are unmerged."]
354 } else {
355 error_popup [mc "No files selected: --merge specified but\
356 no unmerged files are within file limit."]
357 }
358 return 0
359 }
360 }
361 set vfilelimit($view) $files
362
ee66e089
PM
363 if {$vcanopt($view)} {
364 set revs [parseviewrevs $view $vrevs($view)]
365 if {$revs eq {}} {
366 return 0
367 }
368 set args [concat $vflags($view) $revs]
369 } else {
370 set args $vorigargs($view)
371 }
372
418c4c7b 373 if {[catch {
7defefb1
KS
374 set fd [open [concat | git log --no-color -z --pretty=raw $show_notes \
375 --parents --boundary $args "--" $files] r]
418c4c7b 376 } err]} {
00abadb9 377 error_popup "[mc "Error executing git log:"] $err"
3ed31a81 378 return 0
1d10f36d 379 }
e439e092 380 set i [reg_instance $fd]
7fcc92bf 381 set viewinstances($view) [list $i]
cdc8429c
PM
382 set viewmainheadid($view) $mainheadid
383 set viewmainheadid_orig($view) $mainheadid
384 if {$files ne {} && $mainheadid ne {}} {
385 get_viewmainhead $view
386 }
387 if {$showlocalchanges && $viewmainheadid($view) ne {}} {
388 interestedin $viewmainheadid($view) dodiffindex
3e6b893f 389 }
86da5b6c 390 fconfigure $fd -blocking 0 -translation lf -eofchar {}
fd8ccbec 391 if {$tclencoding != {}} {
da7c24dd 392 fconfigure $fd -encoding $tclencoding
fd8ccbec 393 }
f806f0fb 394 filerun $fd [list getcommitlines $fd $i $view 0]
d990cedf 395 nowbusy $view [mc "Reading"]
3ed31a81
PM
396 set viewcomplete($view) 0
397 set viewactive($view) 1
398 return 1
38ad0910
PM
399}
400
e2f90ee4
AG
401proc stop_instance {inst} {
402 global commfd leftover
403
404 set fd $commfd($inst)
405 catch {
406 set pid [pid $fd]
b6326e92
AG
407
408 if {$::tcl_platform(platform) eq {windows}} {
409 exec kill -f $pid
410 } else {
411 exec kill $pid
412 }
e2f90ee4
AG
413 }
414 catch {close $fd}
415 nukefile $fd
416 unset commfd($inst)
417 unset leftover($inst)
418}
419
420proc stop_backends {} {
421 global commfd
422
423 foreach inst [array names commfd] {
424 stop_instance $inst
425 }
426}
427
7fcc92bf 428proc stop_rev_list {view} {
e2f90ee4 429 global viewinstances
22626ef4 430
7fcc92bf 431 foreach inst $viewinstances($view) {
e2f90ee4 432 stop_instance $inst
22626ef4 433 }
7fcc92bf 434 set viewinstances($view) {}
22626ef4
PM
435}
436
567c34e0 437proc reset_pending_select {selid} {
39816d60 438 global pending_select mainheadid selectheadid
567c34e0
AG
439
440 if {$selid ne {}} {
441 set pending_select $selid
39816d60
AG
442 } elseif {$selectheadid ne {}} {
443 set pending_select $selectheadid
567c34e0
AG
444 } else {
445 set pending_select $mainheadid
446 }
447}
448
449proc getcommits {selid} {
3ed31a81 450 global canv curview need_redisplay viewactive
38ad0910 451
da7c24dd 452 initlayout
3ed31a81 453 if {[start_rev_list $curview]} {
567c34e0 454 reset_pending_select $selid
3ed31a81
PM
455 show_status [mc "Reading commits..."]
456 set need_redisplay 1
457 } else {
458 show_status [mc "No commits selected"]
459 }
1d10f36d
PM
460}
461
7fcc92bf 462proc updatecommits {} {
ee66e089 463 global curview vcanopt vorigargs vfilelimit viewinstances
e439e092
AG
464 global viewactive viewcomplete tclencoding
465 global startmsecs showneartags showlocalchanges
cdc8429c 466 global mainheadid viewmainheadid viewmainheadid_orig pending_select
74cb884f 467 global hasworktree
ee66e089 468 global varcid vposids vnegids vflags vrevs
7defefb1 469 global show_notes
7fcc92bf 470
74cb884f 471 set hasworktree [hasworktree]
fc2a256f 472 rereadrefs
cdc8429c
PM
473 set view $curview
474 if {$mainheadid ne $viewmainheadid_orig($view)} {
475 if {$showlocalchanges} {
eb5f8c9c
PM
476 dohidelocalchanges
477 }
cdc8429c
PM
478 set viewmainheadid($view) $mainheadid
479 set viewmainheadid_orig($view) $mainheadid
480 if {$vfilelimit($view) ne {}} {
481 get_viewmainhead $view
eb5f8c9c
PM
482 }
483 }
cdc8429c
PM
484 if {$showlocalchanges} {
485 doshowlocalchanges
486 }
ee66e089
PM
487 if {$vcanopt($view)} {
488 set oldpos $vposids($view)
489 set oldneg $vnegids($view)
490 set revs [parseviewrevs $view $vrevs($view)]
491 if {$revs eq {}} {
492 return
493 }
494 # note: getting the delta when negative refs change is hard,
495 # and could require multiple git log invocations, so in that
496 # case we ask git log for all the commits (not just the delta)
497 if {$oldneg eq $vnegids($view)} {
498 set newrevs {}
499 set npos 0
500 # take out positive refs that we asked for before or
501 # that we have already seen
502 foreach rev $revs {
503 if {[string length $rev] == 40} {
504 if {[lsearch -exact $oldpos $rev] < 0
505 && ![info exists varcid($view,$rev)]} {
506 lappend newrevs $rev
507 incr npos
508 }
509 } else {
510 lappend $newrevs $rev
511 }
512 }
513 if {$npos == 0} return
514 set revs $newrevs
515 set vposids($view) [lsort -unique [concat $oldpos $vposids($view)]]
516 }
517 set args [concat $vflags($view) $revs --not $oldpos]
518 } else {
519 set args $vorigargs($view)
520 }
7fcc92bf 521 if {[catch {
7defefb1
KS
522 set fd [open [concat | git log --no-color -z --pretty=raw $show_notes \
523 --parents --boundary $args "--" $vfilelimit($view)] r]
7fcc92bf 524 } err]} {
3945d2c0 525 error_popup "[mc "Error executing git log:"] $err"
ee66e089 526 return
7fcc92bf
PM
527 }
528 if {$viewactive($view) == 0} {
529 set startmsecs [clock clicks -milliseconds]
530 }
e439e092 531 set i [reg_instance $fd]
7fcc92bf 532 lappend viewinstances($view) $i
7fcc92bf
PM
533 fconfigure $fd -blocking 0 -translation lf -eofchar {}
534 if {$tclencoding != {}} {
535 fconfigure $fd -encoding $tclencoding
536 }
f806f0fb 537 filerun $fd [list getcommitlines $fd $i $view 1]
7fcc92bf
PM
538 incr viewactive($view)
539 set viewcomplete($view) 0
567c34e0 540 reset_pending_select {}
b56e0a9a 541 nowbusy $view [mc "Reading"]
7fcc92bf
PM
542 if {$showneartags} {
543 getallcommits
544 }
545}
546
547proc reloadcommits {} {
548 global curview viewcomplete selectedline currentid thickerline
549 global showneartags treediffs commitinterest cached_commitrow
6df7403a 550 global targetid
7fcc92bf 551
567c34e0
AG
552 set selid {}
553 if {$selectedline ne {}} {
554 set selid $currentid
555 }
556
7fcc92bf
PM
557 if {!$viewcomplete($curview)} {
558 stop_rev_list $curview
7fcc92bf
PM
559 }
560 resetvarcs $curview
94b4a69f 561 set selectedline {}
7fcc92bf
PM
562 catch {unset currentid}
563 catch {unset thickerline}
564 catch {unset treediffs}
565 readrefs
566 changedrefs
567 if {$showneartags} {
568 getallcommits
569 }
570 clear_display
571 catch {unset commitinterest}
572 catch {unset cached_commitrow}
42a671fc 573 catch {unset targetid}
7fcc92bf 574 setcanvscroll
567c34e0 575 getcommits $selid
e7297a1c 576 return 0
7fcc92bf
PM
577}
578
6e8c8707
PM
579# This makes a string representation of a positive integer which
580# sorts as a string in numerical order
581proc strrep {n} {
582 if {$n < 16} {
583 return [format "%x" $n]
584 } elseif {$n < 256} {
585 return [format "x%.2x" $n]
586 } elseif {$n < 65536} {
587 return [format "y%.4x" $n]
588 }
589 return [format "z%.8x" $n]
590}
591
7fcc92bf
PM
592# Procedures used in reordering commits from git log (without
593# --topo-order) into the order for display.
594
595proc varcinit {view} {
f3ea5ede
PM
596 global varcstart vupptr vdownptr vleftptr vbackptr varctok varcrow
597 global vtokmod varcmod vrowmod varcix vlastins
7fcc92bf 598
7fcc92bf
PM
599 set varcstart($view) {{}}
600 set vupptr($view) {0}
601 set vdownptr($view) {0}
602 set vleftptr($view) {0}
f3ea5ede 603 set vbackptr($view) {0}
7fcc92bf
PM
604 set varctok($view) {{}}
605 set varcrow($view) {{}}
606 set vtokmod($view) {}
607 set varcmod($view) 0
e5b37ac1 608 set vrowmod($view) 0
7fcc92bf 609 set varcix($view) {{}}
f3ea5ede 610 set vlastins($view) {0}
7fcc92bf
PM
611}
612
613proc resetvarcs {view} {
614 global varcid varccommits parents children vseedcount ordertok
615
616 foreach vid [array names varcid $view,*] {
617 unset varcid($vid)
618 unset children($vid)
619 unset parents($vid)
620 }
621 # some commits might have children but haven't been seen yet
622 foreach vid [array names children $view,*] {
623 unset children($vid)
624 }
625 foreach va [array names varccommits $view,*] {
626 unset varccommits($va)
627 }
628 foreach vd [array names vseedcount $view,*] {
629 unset vseedcount($vd)
630 }
9257d8f7 631 catch {unset ordertok}
7fcc92bf
PM
632}
633
468bcaed
PM
634# returns a list of the commits with no children
635proc seeds {v} {
636 global vdownptr vleftptr varcstart
637
638 set ret {}
639 set a [lindex $vdownptr($v) 0]
640 while {$a != 0} {
641 lappend ret [lindex $varcstart($v) $a]
642 set a [lindex $vleftptr($v) $a]
643 }
644 return $ret
645}
646
7fcc92bf 647proc newvarc {view id} {
3ed31a81 648 global varcid varctok parents children vdatemode
f3ea5ede
PM
649 global vupptr vdownptr vleftptr vbackptr varcrow varcix varcstart
650 global commitdata commitinfo vseedcount varccommits vlastins
7fcc92bf
PM
651
652 set a [llength $varctok($view)]
653 set vid $view,$id
3ed31a81 654 if {[llength $children($vid)] == 0 || $vdatemode($view)} {
7fcc92bf
PM
655 if {![info exists commitinfo($id)]} {
656 parsecommit $id $commitdata($id) 1
657 }
f5974d97 658 set cdate [lindex [lindex $commitinfo($id) 4] 0]
7fcc92bf
PM
659 if {![string is integer -strict $cdate]} {
660 set cdate 0
661 }
662 if {![info exists vseedcount($view,$cdate)]} {
663 set vseedcount($view,$cdate) -1
664 }
665 set c [incr vseedcount($view,$cdate)]
666 set cdate [expr {$cdate ^ 0xffffffff}]
667 set tok "s[strrep $cdate][strrep $c]"
7fcc92bf
PM
668 } else {
669 set tok {}
f3ea5ede
PM
670 }
671 set ka 0
672 if {[llength $children($vid)] > 0} {
673 set kid [lindex $children($vid) end]
674 set k $varcid($view,$kid)
675 if {[string compare [lindex $varctok($view) $k] $tok] > 0} {
676 set ki $kid
677 set ka $k
678 set tok [lindex $varctok($view) $k]
7fcc92bf 679 }
f3ea5ede
PM
680 }
681 if {$ka != 0} {
7fcc92bf
PM
682 set i [lsearch -exact $parents($view,$ki) $id]
683 set j [expr {[llength $parents($view,$ki)] - 1 - $i}]
7fcc92bf
PM
684 append tok [strrep $j]
685 }
f3ea5ede
PM
686 set c [lindex $vlastins($view) $ka]
687 if {$c == 0 || [string compare $tok [lindex $varctok($view) $c]] < 0} {
688 set c $ka
689 set b [lindex $vdownptr($view) $ka]
690 } else {
691 set b [lindex $vleftptr($view) $c]
692 }
693 while {$b != 0 && [string compare $tok [lindex $varctok($view) $b]] >= 0} {
694 set c $b
695 set b [lindex $vleftptr($view) $c]
696 }
697 if {$c == $ka} {
698 lset vdownptr($view) $ka $a
699 lappend vbackptr($view) 0
700 } else {
701 lset vleftptr($view) $c $a
702 lappend vbackptr($view) $c
703 }
704 lset vlastins($view) $ka $a
705 lappend vupptr($view) $ka
706 lappend vleftptr($view) $b
707 if {$b != 0} {
708 lset vbackptr($view) $b $a
709 }
7fcc92bf
PM
710 lappend varctok($view) $tok
711 lappend varcstart($view) $id
712 lappend vdownptr($view) 0
713 lappend varcrow($view) {}
714 lappend varcix($view) {}
e5b37ac1 715 set varccommits($view,$a) {}
f3ea5ede 716 lappend vlastins($view) 0
7fcc92bf
PM
717 return $a
718}
719
720proc splitvarc {p v} {
52b8ea93 721 global varcid varcstart varccommits varctok vtokmod
f3ea5ede 722 global vupptr vdownptr vleftptr vbackptr varcix varcrow vlastins
7fcc92bf
PM
723
724 set oa $varcid($v,$p)
52b8ea93 725 set otok [lindex $varctok($v) $oa]
7fcc92bf
PM
726 set ac $varccommits($v,$oa)
727 set i [lsearch -exact $varccommits($v,$oa) $p]
728 if {$i <= 0} return
729 set na [llength $varctok($v)]
730 # "%" sorts before "0"...
52b8ea93 731 set tok "$otok%[strrep $i]"
7fcc92bf
PM
732 lappend varctok($v) $tok
733 lappend varcrow($v) {}
734 lappend varcix($v) {}
735 set varccommits($v,$oa) [lrange $ac 0 [expr {$i - 1}]]
736 set varccommits($v,$na) [lrange $ac $i end]
737 lappend varcstart($v) $p
738 foreach id $varccommits($v,$na) {
739 set varcid($v,$id) $na
740 }
741 lappend vdownptr($v) [lindex $vdownptr($v) $oa]
841ea824 742 lappend vlastins($v) [lindex $vlastins($v) $oa]
7fcc92bf 743 lset vdownptr($v) $oa $na
841ea824 744 lset vlastins($v) $oa 0
7fcc92bf
PM
745 lappend vupptr($v) $oa
746 lappend vleftptr($v) 0
f3ea5ede 747 lappend vbackptr($v) 0
7fcc92bf
PM
748 for {set b [lindex $vdownptr($v) $na]} {$b != 0} {set b [lindex $vleftptr($v) $b]} {
749 lset vupptr($v) $b $na
750 }
52b8ea93
PM
751 if {[string compare $otok $vtokmod($v)] <= 0} {
752 modify_arc $v $oa
753 }
7fcc92bf
PM
754}
755
756proc renumbervarc {a v} {
757 global parents children varctok varcstart varccommits
3ed31a81 758 global vupptr vdownptr vleftptr vbackptr vlastins varcid vtokmod vdatemode
7fcc92bf
PM
759
760 set t1 [clock clicks -milliseconds]
761 set todo {}
762 set isrelated($a) 1
f3ea5ede 763 set kidchanged($a) 1
7fcc92bf
PM
764 set ntot 0
765 while {$a != 0} {
766 if {[info exists isrelated($a)]} {
767 lappend todo $a
768 set id [lindex $varccommits($v,$a) end]
769 foreach p $parents($v,$id) {
770 if {[info exists varcid($v,$p)]} {
771 set isrelated($varcid($v,$p)) 1
772 }
773 }
774 }
775 incr ntot
776 set b [lindex $vdownptr($v) $a]
777 if {$b == 0} {
778 while {$a != 0} {
779 set b [lindex $vleftptr($v) $a]
780 if {$b != 0} break
781 set a [lindex $vupptr($v) $a]
782 }
783 }
784 set a $b
785 }
786 foreach a $todo {
f3ea5ede 787 if {![info exists kidchanged($a)]} continue
7fcc92bf 788 set id [lindex $varcstart($v) $a]
f3ea5ede
PM
789 if {[llength $children($v,$id)] > 1} {
790 set children($v,$id) [lsort -command [list vtokcmp $v] \
791 $children($v,$id)]
792 }
793 set oldtok [lindex $varctok($v) $a]
3ed31a81 794 if {!$vdatemode($v)} {
f3ea5ede
PM
795 set tok {}
796 } else {
797 set tok $oldtok
798 }
799 set ka 0
c8c9f3d9
PM
800 set kid [last_real_child $v,$id]
801 if {$kid ne {}} {
f3ea5ede
PM
802 set k $varcid($v,$kid)
803 if {[string compare [lindex $varctok($v) $k] $tok] > 0} {
804 set ki $kid
805 set ka $k
806 set tok [lindex $varctok($v) $k]
7fcc92bf
PM
807 }
808 }
f3ea5ede 809 if {$ka != 0} {
7fcc92bf
PM
810 set i [lsearch -exact $parents($v,$ki) $id]
811 set j [expr {[llength $parents($v,$ki)] - 1 - $i}]
812 append tok [strrep $j]
7fcc92bf 813 }
f3ea5ede
PM
814 if {$tok eq $oldtok} {
815 continue
816 }
817 set id [lindex $varccommits($v,$a) end]
818 foreach p $parents($v,$id) {
819 if {[info exists varcid($v,$p)]} {
820 set kidchanged($varcid($v,$p)) 1
821 } else {
822 set sortkids($p) 1
823 }
824 }
825 lset varctok($v) $a $tok
7fcc92bf
PM
826 set b [lindex $vupptr($v) $a]
827 if {$b != $ka} {
9257d8f7
PM
828 if {[string compare [lindex $varctok($v) $ka] $vtokmod($v)] < 0} {
829 modify_arc $v $ka
38dfe939 830 }
9257d8f7
PM
831 if {[string compare [lindex $varctok($v) $b] $vtokmod($v)] < 0} {
832 modify_arc $v $b
38dfe939 833 }
f3ea5ede
PM
834 set c [lindex $vbackptr($v) $a]
835 set d [lindex $vleftptr($v) $a]
836 if {$c == 0} {
837 lset vdownptr($v) $b $d
7fcc92bf 838 } else {
f3ea5ede
PM
839 lset vleftptr($v) $c $d
840 }
841 if {$d != 0} {
842 lset vbackptr($v) $d $c
7fcc92bf 843 }
841ea824
PM
844 if {[lindex $vlastins($v) $b] == $a} {
845 lset vlastins($v) $b $c
846 }
7fcc92bf 847 lset vupptr($v) $a $ka
f3ea5ede
PM
848 set c [lindex $vlastins($v) $ka]
849 if {$c == 0 || \
850 [string compare $tok [lindex $varctok($v) $c]] < 0} {
851 set c $ka
852 set b [lindex $vdownptr($v) $ka]
853 } else {
854 set b [lindex $vleftptr($v) $c]
855 }
856 while {$b != 0 && \
857 [string compare $tok [lindex $varctok($v) $b]] >= 0} {
858 set c $b
859 set b [lindex $vleftptr($v) $c]
7fcc92bf 860 }
f3ea5ede
PM
861 if {$c == $ka} {
862 lset vdownptr($v) $ka $a
863 lset vbackptr($v) $a 0
864 } else {
865 lset vleftptr($v) $c $a
866 lset vbackptr($v) $a $c
7fcc92bf 867 }
f3ea5ede
PM
868 lset vleftptr($v) $a $b
869 if {$b != 0} {
870 lset vbackptr($v) $b $a
871 }
872 lset vlastins($v) $ka $a
873 }
874 }
875 foreach id [array names sortkids] {
876 if {[llength $children($v,$id)] > 1} {
877 set children($v,$id) [lsort -command [list vtokcmp $v] \
878 $children($v,$id)]
7fcc92bf
PM
879 }
880 }
881 set t2 [clock clicks -milliseconds]
882 #puts "renumbervarc did [llength $todo] of $ntot arcs in [expr {$t2-$t1}]ms"
883}
884
f806f0fb
PM
885# Fix up the graph after we have found out that in view $v,
886# $p (a commit that we have already seen) is actually the parent
887# of the last commit in arc $a.
7fcc92bf 888proc fix_reversal {p a v} {
24f7a667 889 global varcid varcstart varctok vupptr
7fcc92bf
PM
890
891 set pa $varcid($v,$p)
892 if {$p ne [lindex $varcstart($v) $pa]} {
893 splitvarc $p $v
894 set pa $varcid($v,$p)
895 }
24f7a667
PM
896 # seeds always need to be renumbered
897 if {[lindex $vupptr($v) $pa] == 0 ||
898 [string compare [lindex $varctok($v) $a] \
899 [lindex $varctok($v) $pa]] > 0} {
7fcc92bf
PM
900 renumbervarc $pa $v
901 }
902}
903
904proc insertrow {id p v} {
b8a938cf
PM
905 global cmitlisted children parents varcid varctok vtokmod
906 global varccommits ordertok commitidx numcommits curview
907 global targetid targetrow
908
909 readcommit $id
910 set vid $v,$id
911 set cmitlisted($vid) 1
912 set children($vid) {}
913 set parents($vid) [list $p]
914 set a [newvarc $v $id]
915 set varcid($vid) $a
916 if {[string compare [lindex $varctok($v) $a] $vtokmod($v)] < 0} {
917 modify_arc $v $a
918 }
919 lappend varccommits($v,$a) $id
920 set vp $v,$p
921 if {[llength [lappend children($vp) $id]] > 1} {
922 set children($vp) [lsort -command [list vtokcmp $v] $children($vp)]
923 catch {unset ordertok}
924 }
925 fix_reversal $p $a $v
926 incr commitidx($v)
927 if {$v == $curview} {
928 set numcommits $commitidx($v)
929 setcanvscroll
930 if {[info exists targetid]} {
931 if {![comes_before $targetid $p]} {
932 incr targetrow
933 }
934 }
935 }
936}
937
938proc insertfakerow {id p} {
9257d8f7 939 global varcid varccommits parents children cmitlisted
b8a938cf 940 global commitidx varctok vtokmod targetid targetrow curview numcommits
7fcc92bf 941
b8a938cf 942 set v $curview
7fcc92bf
PM
943 set a $varcid($v,$p)
944 set i [lsearch -exact $varccommits($v,$a) $p]
945 if {$i < 0} {
b8a938cf 946 puts "oops: insertfakerow can't find [shortids $p] on arc $a"
7fcc92bf
PM
947 return
948 }
949 set children($v,$id) {}
950 set parents($v,$id) [list $p]
951 set varcid($v,$id) $a
9257d8f7 952 lappend children($v,$p) $id
7fcc92bf 953 set cmitlisted($v,$id) 1
b8a938cf 954 set numcommits [incr commitidx($v)]
7fcc92bf
PM
955 # note we deliberately don't update varcstart($v) even if $i == 0
956 set varccommits($v,$a) [linsert $varccommits($v,$a) $i $id]
c9cfdc96 957 modify_arc $v $a $i
42a671fc
PM
958 if {[info exists targetid]} {
959 if {![comes_before $targetid $p]} {
960 incr targetrow
961 }
962 }
b8a938cf 963 setcanvscroll
9257d8f7 964 drawvisible
7fcc92bf
PM
965}
966
b8a938cf 967proc removefakerow {id} {
9257d8f7 968 global varcid varccommits parents children commitidx
fc2a256f 969 global varctok vtokmod cmitlisted currentid selectedline
b8a938cf 970 global targetid curview numcommits
7fcc92bf 971
b8a938cf 972 set v $curview
7fcc92bf 973 if {[llength $parents($v,$id)] != 1} {
b8a938cf 974 puts "oops: removefakerow [shortids $id] has [llength $parents($v,$id)] parents"
7fcc92bf
PM
975 return
976 }
977 set p [lindex $parents($v,$id) 0]
978 set a $varcid($v,$id)
979 set i [lsearch -exact $varccommits($v,$a) $id]
980 if {$i < 0} {
b8a938cf 981 puts "oops: removefakerow can't find [shortids $id] on arc $a"
7fcc92bf
PM
982 return
983 }
984 unset varcid($v,$id)
985 set varccommits($v,$a) [lreplace $varccommits($v,$a) $i $i]
986 unset parents($v,$id)
987 unset children($v,$id)
988 unset cmitlisted($v,$id)
b8a938cf 989 set numcommits [incr commitidx($v) -1]
7fcc92bf
PM
990 set j [lsearch -exact $children($v,$p) $id]
991 if {$j >= 0} {
992 set children($v,$p) [lreplace $children($v,$p) $j $j]
993 }
c9cfdc96 994 modify_arc $v $a $i
fc2a256f
PM
995 if {[info exist currentid] && $id eq $currentid} {
996 unset currentid
94b4a69f 997 set selectedline {}
fc2a256f 998 }
42a671fc
PM
999 if {[info exists targetid] && $targetid eq $id} {
1000 set targetid $p
1001 }
b8a938cf 1002 setcanvscroll
9257d8f7 1003 drawvisible
7fcc92bf
PM
1004}
1005
aa43561a
PM
1006proc real_children {vp} {
1007 global children nullid nullid2
1008
1009 set kids {}
1010 foreach id $children($vp) {
1011 if {$id ne $nullid && $id ne $nullid2} {
1012 lappend kids $id
1013 }
1014 }
1015 return $kids
1016}
1017
c8c9f3d9
PM
1018proc first_real_child {vp} {
1019 global children nullid nullid2
1020
1021 foreach id $children($vp) {
1022 if {$id ne $nullid && $id ne $nullid2} {
1023 return $id
1024 }
1025 }
1026 return {}
1027}
1028
1029proc last_real_child {vp} {
1030 global children nullid nullid2
1031
1032 set kids $children($vp)
1033 for {set i [llength $kids]} {[incr i -1] >= 0} {} {
1034 set id [lindex $kids $i]
1035 if {$id ne $nullid && $id ne $nullid2} {
1036 return $id
1037 }
1038 }
1039 return {}
1040}
1041
7fcc92bf
PM
1042proc vtokcmp {v a b} {
1043 global varctok varcid
1044
1045 return [string compare [lindex $varctok($v) $varcid($v,$a)] \
1046 [lindex $varctok($v) $varcid($v,$b)]]
1047}
1048
c9cfdc96
PM
1049# This assumes that if lim is not given, the caller has checked that
1050# arc a's token is less than $vtokmod($v)
e5b37ac1
PM
1051proc modify_arc {v a {lim {}}} {
1052 global varctok vtokmod varcmod varcrow vupptr curview vrowmod varccommits
9257d8f7 1053
c9cfdc96
PM
1054 if {$lim ne {}} {
1055 set c [string compare [lindex $varctok($v) $a] $vtokmod($v)]
1056 if {$c > 0} return
1057 if {$c == 0} {
1058 set r [lindex $varcrow($v) $a]
1059 if {$r ne {} && $vrowmod($v) <= $r + $lim} return
1060 }
1061 }
9257d8f7
PM
1062 set vtokmod($v) [lindex $varctok($v) $a]
1063 set varcmod($v) $a
1064 if {$v == $curview} {
1065 while {$a != 0 && [lindex $varcrow($v) $a] eq {}} {
1066 set a [lindex $vupptr($v) $a]
e5b37ac1 1067 set lim {}
9257d8f7 1068 }
e5b37ac1
PM
1069 set r 0
1070 if {$a != 0} {
1071 if {$lim eq {}} {
1072 set lim [llength $varccommits($v,$a)]
1073 }
1074 set r [expr {[lindex $varcrow($v) $a] + $lim}]
1075 }
1076 set vrowmod($v) $r
0c27886e 1077 undolayout $r
9257d8f7
PM
1078 }
1079}
1080
7fcc92bf 1081proc update_arcrows {v} {
e5b37ac1 1082 global vtokmod varcmod vrowmod varcrow commitidx currentid selectedline
24f7a667 1083 global varcid vrownum varcorder varcix varccommits
7fcc92bf 1084 global vupptr vdownptr vleftptr varctok
24f7a667 1085 global displayorder parentlist curview cached_commitrow
7fcc92bf 1086
c9cfdc96
PM
1087 if {$vrowmod($v) == $commitidx($v)} return
1088 if {$v == $curview} {
1089 if {[llength $displayorder] > $vrowmod($v)} {
1090 set displayorder [lrange $displayorder 0 [expr {$vrowmod($v) - 1}]]
1091 set parentlist [lrange $parentlist 0 [expr {$vrowmod($v) - 1}]]
1092 }
1093 catch {unset cached_commitrow}
1094 }
7fcc92bf
PM
1095 set narctot [expr {[llength $varctok($v)] - 1}]
1096 set a $varcmod($v)
1097 while {$a != 0 && [lindex $varcix($v) $a] eq {}} {
1098 # go up the tree until we find something that has a row number,
1099 # or we get to a seed
1100 set a [lindex $vupptr($v) $a]
1101 }
1102 if {$a == 0} {
1103 set a [lindex $vdownptr($v) 0]
1104 if {$a == 0} return
1105 set vrownum($v) {0}
1106 set varcorder($v) [list $a]
1107 lset varcix($v) $a 0
1108 lset varcrow($v) $a 0
1109 set arcn 0
1110 set row 0
1111 } else {
1112 set arcn [lindex $varcix($v) $a]
7fcc92bf
PM
1113 if {[llength $vrownum($v)] > $arcn + 1} {
1114 set vrownum($v) [lrange $vrownum($v) 0 $arcn]
1115 set varcorder($v) [lrange $varcorder($v) 0 $arcn]
1116 }
1117 set row [lindex $varcrow($v) $a]
1118 }
7fcc92bf
PM
1119 while {1} {
1120 set p $a
1121 incr row [llength $varccommits($v,$a)]
1122 # go down if possible
1123 set b [lindex $vdownptr($v) $a]
1124 if {$b == 0} {
1125 # if not, go left, or go up until we can go left
1126 while {$a != 0} {
1127 set b [lindex $vleftptr($v) $a]
1128 if {$b != 0} break
1129 set a [lindex $vupptr($v) $a]
1130 }
1131 if {$a == 0} break
1132 }
1133 set a $b
1134 incr arcn
1135 lappend vrownum($v) $row
1136 lappend varcorder($v) $a
1137 lset varcix($v) $a $arcn
1138 lset varcrow($v) $a $row
1139 }
e5b37ac1
PM
1140 set vtokmod($v) [lindex $varctok($v) $p]
1141 set varcmod($v) $p
1142 set vrowmod($v) $row
7fcc92bf
PM
1143 if {[info exists currentid]} {
1144 set selectedline [rowofcommit $currentid]
1145 }
7fcc92bf
PM
1146}
1147
1148# Test whether view $v contains commit $id
1149proc commitinview {id v} {
1150 global varcid
1151
1152 return [info exists varcid($v,$id)]
1153}
1154
1155# Return the row number for commit $id in the current view
1156proc rowofcommit {id} {
1157 global varcid varccommits varcrow curview cached_commitrow
9257d8f7 1158 global varctok vtokmod
7fcc92bf 1159
7fcc92bf
PM
1160 set v $curview
1161 if {![info exists varcid($v,$id)]} {
1162 puts "oops rowofcommit no arc for [shortids $id]"
1163 return {}
1164 }
1165 set a $varcid($v,$id)
fc2a256f 1166 if {[string compare [lindex $varctok($v) $a] $vtokmod($v)] >= 0} {
9257d8f7
PM
1167 update_arcrows $v
1168 }
31c0eaa8
PM
1169 if {[info exists cached_commitrow($id)]} {
1170 return $cached_commitrow($id)
1171 }
7fcc92bf
PM
1172 set i [lsearch -exact $varccommits($v,$a) $id]
1173 if {$i < 0} {
1174 puts "oops didn't find commit [shortids $id] in arc $a"
1175 return {}
1176 }
1177 incr i [lindex $varcrow($v) $a]
1178 set cached_commitrow($id) $i
1179 return $i
1180}
1181
42a671fc
PM
1182# Returns 1 if a is on an earlier row than b, otherwise 0
1183proc comes_before {a b} {
1184 global varcid varctok curview
1185
1186 set v $curview
1187 if {$a eq $b || ![info exists varcid($v,$a)] || \
1188 ![info exists varcid($v,$b)]} {
1189 return 0
1190 }
1191 if {$varcid($v,$a) != $varcid($v,$b)} {
1192 return [expr {[string compare [lindex $varctok($v) $varcid($v,$a)] \
1193 [lindex $varctok($v) $varcid($v,$b)]] < 0}]
1194 }
1195 return [expr {[rowofcommit $a] < [rowofcommit $b]}]
1196}
1197
7fcc92bf
PM
1198proc bsearch {l elt} {
1199 if {[llength $l] == 0 || $elt <= [lindex $l 0]} {
1200 return 0
1201 }
1202 set lo 0
1203 set hi [llength $l]
1204 while {$hi - $lo > 1} {
1205 set mid [expr {int(($lo + $hi) / 2)}]
1206 set t [lindex $l $mid]
1207 if {$elt < $t} {
1208 set hi $mid
1209 } elseif {$elt > $t} {
1210 set lo $mid
1211 } else {
1212 return $mid
1213 }
1214 }
1215 return $lo
1216}
1217
1218# Make sure rows $start..$end-1 are valid in displayorder and parentlist
1219proc make_disporder {start end} {
1220 global vrownum curview commitidx displayorder parentlist
e5b37ac1 1221 global varccommits varcorder parents vrowmod varcrow
7fcc92bf
PM
1222 global d_valid_start d_valid_end
1223
e5b37ac1 1224 if {$end > $vrowmod($curview)} {
9257d8f7
PM
1225 update_arcrows $curview
1226 }
7fcc92bf
PM
1227 set ai [bsearch $vrownum($curview) $start]
1228 set start [lindex $vrownum($curview) $ai]
1229 set narc [llength $vrownum($curview)]
1230 for {set r $start} {$ai < $narc && $r < $end} {incr ai} {
1231 set a [lindex $varcorder($curview) $ai]
1232 set l [llength $displayorder]
1233 set al [llength $varccommits($curview,$a)]
1234 if {$l < $r + $al} {
1235 if {$l < $r} {
1236 set pad [ntimes [expr {$r - $l}] {}]
1237 set displayorder [concat $displayorder $pad]
1238 set parentlist [concat $parentlist $pad]
1239 } elseif {$l > $r} {
1240 set displayorder [lrange $displayorder 0 [expr {$r - 1}]]
1241 set parentlist [lrange $parentlist 0 [expr {$r - 1}]]
1242 }
1243 foreach id $varccommits($curview,$a) {
1244 lappend displayorder $id
1245 lappend parentlist $parents($curview,$id)
1246 }
17529cf9 1247 } elseif {[lindex $displayorder [expr {$r + $al - 1}]] eq {}} {
7fcc92bf
PM
1248 set i $r
1249 foreach id $varccommits($curview,$a) {
1250 lset displayorder $i $id
1251 lset parentlist $i $parents($curview,$id)
1252 incr i
1253 }
1254 }
1255 incr r $al
1256 }
1257}
1258
1259proc commitonrow {row} {
1260 global displayorder
1261
1262 set id [lindex $displayorder $row]
1263 if {$id eq {}} {
1264 make_disporder $row [expr {$row + 1}]
1265 set id [lindex $displayorder $row]
1266 }
1267 return $id
1268}
1269
1270proc closevarcs {v} {
1271 global varctok varccommits varcid parents children
d375ef9b 1272 global cmitlisted commitidx vtokmod
7fcc92bf
PM
1273
1274 set missing_parents 0
1275 set scripts {}
1276 set narcs [llength $varctok($v)]
1277 for {set a 1} {$a < $narcs} {incr a} {
1278 set id [lindex $varccommits($v,$a) end]
1279 foreach p $parents($v,$id) {
1280 if {[info exists varcid($v,$p)]} continue
1281 # add p as a new commit
1282 incr missing_parents
1283 set cmitlisted($v,$p) 0
1284 set parents($v,$p) {}
1285 if {[llength $children($v,$p)] == 1 &&
1286 [llength $parents($v,$id)] == 1} {
1287 set b $a
1288 } else {
1289 set b [newvarc $v $p]
1290 }
1291 set varcid($v,$p) $b
9257d8f7
PM
1292 if {[string compare [lindex $varctok($v) $b] $vtokmod($v)] < 0} {
1293 modify_arc $v $b
7fcc92bf 1294 }
e5b37ac1 1295 lappend varccommits($v,$b) $p
7fcc92bf 1296 incr commitidx($v)
d375ef9b 1297 set scripts [check_interest $p $scripts]
7fcc92bf
PM
1298 }
1299 }
1300 if {$missing_parents > 0} {
7fcc92bf
PM
1301 foreach s $scripts {
1302 eval $s
1303 }
1304 }
1305}
1306
f806f0fb
PM
1307# Use $rwid as a substitute for $id, i.e. reparent $id's children to $rwid
1308# Assumes we already have an arc for $rwid.
1309proc rewrite_commit {v id rwid} {
1310 global children parents varcid varctok vtokmod varccommits
1311
1312 foreach ch $children($v,$id) {
1313 # make $rwid be $ch's parent in place of $id
1314 set i [lsearch -exact $parents($v,$ch) $id]
1315 if {$i < 0} {
1316 puts "oops rewrite_commit didn't find $id in parent list for $ch"
1317 }
1318 set parents($v,$ch) [lreplace $parents($v,$ch) $i $i $rwid]
1319 # add $ch to $rwid's children and sort the list if necessary
1320 if {[llength [lappend children($v,$rwid) $ch]] > 1} {
1321 set children($v,$rwid) [lsort -command [list vtokcmp $v] \
1322 $children($v,$rwid)]
1323 }
1324 # fix the graph after joining $id to $rwid
1325 set a $varcid($v,$ch)
1326 fix_reversal $rwid $a $v
c9cfdc96
PM
1327 # parentlist is wrong for the last element of arc $a
1328 # even if displayorder is right, hence the 3rd arg here
1329 modify_arc $v $a [expr {[llength $varccommits($v,$a)] - 1}]
f806f0fb
PM
1330 }
1331}
1332
d375ef9b
PM
1333# Mechanism for registering a command to be executed when we come
1334# across a particular commit. To handle the case when only the
1335# prefix of the commit is known, the commitinterest array is now
1336# indexed by the first 4 characters of the ID. Each element is a
1337# list of id, cmd pairs.
1338proc interestedin {id cmd} {
1339 global commitinterest
1340
1341 lappend commitinterest([string range $id 0 3]) $id $cmd
1342}
1343
1344proc check_interest {id scripts} {
1345 global commitinterest
1346
1347 set prefix [string range $id 0 3]
1348 if {[info exists commitinterest($prefix)]} {
1349 set newlist {}
1350 foreach {i script} $commitinterest($prefix) {
1351 if {[string match "$i*" $id]} {
1352 lappend scripts [string map [list "%I" $id "%P" $i] $script]
1353 } else {
1354 lappend newlist $i $script
1355 }
1356 }
1357 if {$newlist ne {}} {
1358 set commitinterest($prefix) $newlist
1359 } else {
1360 unset commitinterest($prefix)
1361 }
1362 }
1363 return $scripts
1364}
1365
f806f0fb 1366proc getcommitlines {fd inst view updating} {
d375ef9b 1367 global cmitlisted leftover
3ed31a81 1368 global commitidx commitdata vdatemode
7fcc92bf 1369 global parents children curview hlview
468bcaed 1370 global idpending ordertok
3ed31a81 1371 global varccommits varcid varctok vtokmod vfilelimit
9ccbdfbf 1372
d1e46756 1373 set stuff [read $fd 500000]
005a2f4e 1374 # git log doesn't terminate the last commit with a null...
7fcc92bf 1375 if {$stuff == {} && $leftover($inst) ne {} && [eof $fd]} {
005a2f4e
PM
1376 set stuff "\0"
1377 }
b490a991 1378 if {$stuff == {}} {
7eb3cb9c
PM
1379 if {![eof $fd]} {
1380 return 1
1381 }
6df7403a 1382 global commfd viewcomplete viewactive viewname
7fcc92bf
PM
1383 global viewinstances
1384 unset commfd($inst)
1385 set i [lsearch -exact $viewinstances($view) $inst]
1386 if {$i >= 0} {
1387 set viewinstances($view) [lreplace $viewinstances($view) $i $i]
b0cdca99 1388 }
f0654861 1389 # set it blocking so we wait for the process to terminate
da7c24dd 1390 fconfigure $fd -blocking 1
098dd8a3
PM
1391 if {[catch {close $fd} err]} {
1392 set fv {}
1393 if {$view != $curview} {
1394 set fv " for the \"$viewname($view)\" view"
da7c24dd 1395 }
098dd8a3
PM
1396 if {[string range $err 0 4] == "usage"} {
1397 set err "Gitk: error reading commits$fv:\
f9e0b6fb 1398 bad arguments to git log."
098dd8a3
PM
1399 if {$viewname($view) eq "Command line"} {
1400 append err \
f9e0b6fb 1401 " (Note: arguments to gitk are passed to git log\
098dd8a3
PM
1402 to allow selection of commits to be displayed.)"
1403 }
1404 } else {
1405 set err "Error reading commits$fv: $err"
1406 }
1407 error_popup $err
1d10f36d 1408 }
7fcc92bf
PM
1409 if {[incr viewactive($view) -1] <= 0} {
1410 set viewcomplete($view) 1
1411 # Check if we have seen any ids listed as parents that haven't
1412 # appeared in the list
1413 closevarcs $view
1414 notbusy $view
7fcc92bf 1415 }
098dd8a3 1416 if {$view == $curview} {
ac1276ab 1417 run chewcommits
9a40c50c 1418 }
7eb3cb9c 1419 return 0
9a40c50c 1420 }
b490a991 1421 set start 0
8f7d0cec 1422 set gotsome 0
7fcc92bf 1423 set scripts {}
b490a991
PM
1424 while 1 {
1425 set i [string first "\0" $stuff $start]
1426 if {$i < 0} {
7fcc92bf 1427 append leftover($inst) [string range $stuff $start end]
9f1afe05 1428 break
9ccbdfbf 1429 }
b490a991 1430 if {$start == 0} {
7fcc92bf 1431 set cmit $leftover($inst)
8f7d0cec 1432 append cmit [string range $stuff 0 [expr {$i - 1}]]
7fcc92bf 1433 set leftover($inst) {}
8f7d0cec
PM
1434 } else {
1435 set cmit [string range $stuff $start [expr {$i - 1}]]
b490a991
PM
1436 }
1437 set start [expr {$i + 1}]
e5ea701b
PM
1438 set j [string first "\n" $cmit]
1439 set ok 0
16c1ff96 1440 set listed 1
c961b228
PM
1441 if {$j >= 0 && [string match "commit *" $cmit]} {
1442 set ids [string range $cmit 7 [expr {$j - 1}]]
1407ade9 1443 if {[string match {[-^<>]*} $ids]} {
c961b228
PM
1444 switch -- [string index $ids 0] {
1445 "-" {set listed 0}
1407ade9
LT
1446 "^" {set listed 2}
1447 "<" {set listed 3}
1448 ">" {set listed 4}
c961b228 1449 }
16c1ff96
PM
1450 set ids [string range $ids 1 end]
1451 }
e5ea701b
PM
1452 set ok 1
1453 foreach id $ids {
8f7d0cec 1454 if {[string length $id] != 40} {
e5ea701b
PM
1455 set ok 0
1456 break
1457 }
1458 }
1459 }
1460 if {!$ok} {
7e952e79
PM
1461 set shortcmit $cmit
1462 if {[string length $shortcmit] > 80} {
1463 set shortcmit "[string range $shortcmit 0 80]..."
1464 }
d990cedf 1465 error_popup "[mc "Can't parse git log output:"] {$shortcmit}"
b490a991
PM
1466 exit 1
1467 }
e5ea701b 1468 set id [lindex $ids 0]
7fcc92bf 1469 set vid $view,$id
f806f0fb
PM
1470
1471 if {!$listed && $updating && ![info exists varcid($vid)] &&
3ed31a81 1472 $vfilelimit($view) ne {}} {
f806f0fb
PM
1473 # git log doesn't rewrite parents for unlisted commits
1474 # when doing path limiting, so work around that here
1475 # by working out the rewritten parent with git rev-list
1476 # and if we already know about it, using the rewritten
1477 # parent as a substitute parent for $id's children.
1478 if {![catch {
1479 set rwid [exec git rev-list --first-parent --max-count=1 \
3ed31a81 1480 $id -- $vfilelimit($view)]
f806f0fb
PM
1481 }]} {
1482 if {$rwid ne {} && [info exists varcid($view,$rwid)]} {
1483 # use $rwid in place of $id
1484 rewrite_commit $view $id $rwid
1485 continue
1486 }
1487 }
1488 }
1489
f1bf4ee6
PM
1490 set a 0
1491 if {[info exists varcid($vid)]} {
1492 if {$cmitlisted($vid) || !$listed} continue
1493 set a $varcid($vid)
1494 }
16c1ff96
PM
1495 if {$listed} {
1496 set olds [lrange $ids 1 end]
16c1ff96
PM
1497 } else {
1498 set olds {}
1499 }
f7a3e8d2 1500 set commitdata($id) [string range $cmit [expr {$j + 1}] end]
7fcc92bf
PM
1501 set cmitlisted($vid) $listed
1502 set parents($vid) $olds
7fcc92bf
PM
1503 if {![info exists children($vid)]} {
1504 set children($vid) {}
f1bf4ee6 1505 } elseif {$a == 0 && [llength $children($vid)] == 1} {
f3ea5ede
PM
1506 set k [lindex $children($vid) 0]
1507 if {[llength $parents($view,$k)] == 1 &&
3ed31a81 1508 (!$vdatemode($view) ||
f3ea5ede
PM
1509 $varcid($view,$k) == [llength $varctok($view)] - 1)} {
1510 set a $varcid($view,$k)
7fcc92bf 1511 }
da7c24dd 1512 }
7fcc92bf
PM
1513 if {$a == 0} {
1514 # new arc
1515 set a [newvarc $view $id]
1516 }
e5b37ac1
PM
1517 if {[string compare [lindex $varctok($view) $a] $vtokmod($view)] < 0} {
1518 modify_arc $view $a
1519 }
f1bf4ee6
PM
1520 if {![info exists varcid($vid)]} {
1521 set varcid($vid) $a
1522 lappend varccommits($view,$a) $id
1523 incr commitidx($view)
1524 }
e5b37ac1 1525
7fcc92bf
PM
1526 set i 0
1527 foreach p $olds {
1528 if {$i == 0 || [lsearch -exact $olds $p] >= $i} {
1529 set vp $view,$p
1530 if {[llength [lappend children($vp) $id]] > 1 &&
1531 [vtokcmp $view [lindex $children($vp) end-1] $id] > 0} {
1532 set children($vp) [lsort -command [list vtokcmp $view] \
1533 $children($vp)]
9257d8f7 1534 catch {unset ordertok}
7fcc92bf 1535 }
f3ea5ede
PM
1536 if {[info exists varcid($view,$p)]} {
1537 fix_reversal $p $a $view
1538 }
7fcc92bf
PM
1539 }
1540 incr i
1541 }
7fcc92bf 1542
d375ef9b 1543 set scripts [check_interest $id $scripts]
8f7d0cec
PM
1544 set gotsome 1
1545 }
1546 if {$gotsome} {
ac1276ab
PM
1547 global numcommits hlview
1548
1549 if {$view == $curview} {
1550 set numcommits $commitidx($view)
1551 run chewcommits
1552 }
1553 if {[info exists hlview] && $view == $hlview} {
1554 # we never actually get here...
1555 run vhighlightmore
1556 }
7fcc92bf
PM
1557 foreach s $scripts {
1558 eval $s
1559 }
9ccbdfbf 1560 }
7eb3cb9c 1561 return 2
9ccbdfbf
PM
1562}
1563
ac1276ab 1564proc chewcommits {} {
f5f3c2e2 1565 global curview hlview viewcomplete
7fcc92bf 1566 global pending_select
7eb3cb9c 1567
ac1276ab
PM
1568 layoutmore
1569 if {$viewcomplete($curview)} {
1570 global commitidx varctok
1571 global numcommits startmsecs
ac1276ab
PM
1572
1573 if {[info exists pending_select]} {
835e62ae
AG
1574 update
1575 reset_pending_select {}
1576
1577 if {[commitinview $pending_select $curview]} {
1578 selectline [rowofcommit $pending_select] 1
1579 } else {
1580 set row [first_real_row]
1581 selectline $row 1
1582 }
7eb3cb9c 1583 }
ac1276ab
PM
1584 if {$commitidx($curview) > 0} {
1585 #set ms [expr {[clock clicks -milliseconds] - $startmsecs}]
1586 #puts "overall $ms ms for $numcommits commits"
1587 #puts "[llength $varctok($view)] arcs, $commitidx($view) commits"
1588 } else {
1589 show_status [mc "No commits selected"]
1590 }
1591 notbusy layout
b664550c 1592 }
f5f3c2e2 1593 return 0
1db95b00
PM
1594}
1595
590915da
AG
1596proc do_readcommit {id} {
1597 global tclencoding
1598
1599 # Invoke git-log to handle automatic encoding conversion
1600 set fd [open [concat | git log --no-color --pretty=raw -1 $id] r]
1601 # Read the results using i18n.logoutputencoding
1602 fconfigure $fd -translation lf -eofchar {}
1603 if {$tclencoding != {}} {
1604 fconfigure $fd -encoding $tclencoding
1605 }
1606 set contents [read $fd]
1607 close $fd
1608 # Remove the heading line
1609 regsub {^commit [0-9a-f]+\n} $contents {} contents
1610
1611 return $contents
1612}
1613
1db95b00 1614proc readcommit {id} {
590915da
AG
1615 if {[catch {set contents [do_readcommit $id]}]} return
1616 parsecommit $id $contents 1
b490a991
PM
1617}
1618
8f7d0cec 1619proc parsecommit {id contents listed} {
ef73896b 1620 global commitinfo
b5c2f306
SV
1621
1622 set inhdr 1
1623 set comment {}
1624 set headline {}
1625 set auname {}
1626 set audate {}
1627 set comname {}
1628 set comdate {}
232475d3
PM
1629 set hdrend [string first "\n\n" $contents]
1630 if {$hdrend < 0} {
1631 # should never happen...
1632 set hdrend [string length $contents]
1633 }
1634 set header [string range $contents 0 [expr {$hdrend - 1}]]
1635 set comment [string range $contents [expr {$hdrend + 2}] end]
1636 foreach line [split $header "\n"] {
61f57cb0 1637 set line [split $line " "]
232475d3
PM
1638 set tag [lindex $line 0]
1639 if {$tag == "author"} {
f5974d97 1640 set audate [lrange $line end-1 end]
61f57cb0 1641 set auname [join [lrange $line 1 end-2] " "]
232475d3 1642 } elseif {$tag == "committer"} {
f5974d97 1643 set comdate [lrange $line end-1 end]
61f57cb0 1644 set comname [join [lrange $line 1 end-2] " "]
1db95b00
PM
1645 }
1646 }
232475d3 1647 set headline {}
43c25074
PM
1648 # take the first non-blank line of the comment as the headline
1649 set headline [string trimleft $comment]
1650 set i [string first "\n" $headline]
232475d3 1651 if {$i >= 0} {
43c25074
PM
1652 set headline [string range $headline 0 $i]
1653 }
1654 set headline [string trimright $headline]
1655 set i [string first "\r" $headline]
1656 if {$i >= 0} {
1657 set headline [string trimright [string range $headline 0 $i]]
232475d3
PM
1658 }
1659 if {!$listed} {
f9e0b6fb 1660 # git log indents the comment by 4 spaces;
8974c6f9 1661 # if we got this via git cat-file, add the indentation
232475d3
PM
1662 set newcomment {}
1663 foreach line [split $comment "\n"] {
1664 append newcomment " "
1665 append newcomment $line
f6e2869f 1666 append newcomment "\n"
232475d3
PM
1667 }
1668 set comment $newcomment
1db95b00 1669 }
36242490 1670 set hasnote [string first "\nNotes:\n" $contents]
e5c2d856 1671 set commitinfo($id) [list $headline $auname $audate \
36242490 1672 $comname $comdate $comment $hasnote]
1db95b00
PM
1673}
1674
f7a3e8d2 1675proc getcommit {id} {
79b2c75e 1676 global commitdata commitinfo
8ed16484 1677
f7a3e8d2
PM
1678 if {[info exists commitdata($id)]} {
1679 parsecommit $id $commitdata($id) 1
8ed16484
PM
1680 } else {
1681 readcommit $id
1682 if {![info exists commitinfo($id)]} {
d990cedf 1683 set commitinfo($id) [list [mc "No commit information available"]]
8ed16484
PM
1684 }
1685 }
1686 return 1
1687}
1688
d375ef9b
PM
1689# Expand an abbreviated commit ID to a list of full 40-char IDs that match
1690# and are present in the current view.
1691# This is fairly slow...
1692proc longid {prefix} {
1693 global varcid curview
1694
1695 set ids {}
1696 foreach match [array names varcid "$curview,$prefix*"] {
1697 lappend ids [lindex [split $match ","] 1]
1698 }
1699 return $ids
1700}
1701
887fe3c4 1702proc readrefs {} {
62d3ea65 1703 global tagids idtags headids idheads tagobjid
219ea3a9 1704 global otherrefids idotherrefs mainhead mainheadid
39816d60 1705 global selecthead selectheadid
ffe15297 1706 global hideremotes
106288cb 1707
b5c2f306
SV
1708 foreach v {tagids idtags headids idheads otherrefids idotherrefs} {
1709 catch {unset $v}
1710 }
62d3ea65
PM
1711 set refd [open [list | git show-ref -d] r]
1712 while {[gets $refd line] >= 0} {
1713 if {[string index $line 40] ne " "} continue
1714 set id [string range $line 0 39]
1715 set ref [string range $line 41 end]
1716 if {![string match "refs/*" $ref]} continue
1717 set name [string range $ref 5 end]
1718 if {[string match "remotes/*" $name]} {
ffe15297 1719 if {![string match "*/HEAD" $name] && !$hideremotes} {
62d3ea65
PM
1720 set headids($name) $id
1721 lappend idheads($id) $name
f1d83ba3 1722 }
62d3ea65
PM
1723 } elseif {[string match "heads/*" $name]} {
1724 set name [string range $name 6 end]
36a7cad6
JH
1725 set headids($name) $id
1726 lappend idheads($id) $name
62d3ea65
PM
1727 } elseif {[string match "tags/*" $name]} {
1728 # this lets refs/tags/foo^{} overwrite refs/tags/foo,
1729 # which is what we want since the former is the commit ID
1730 set name [string range $name 5 end]
1731 if {[string match "*^{}" $name]} {
1732 set name [string range $name 0 end-3]
1733 } else {
1734 set tagobjid($name) $id
1735 }
1736 set tagids($name) $id
1737 lappend idtags($id) $name
36a7cad6
JH
1738 } else {
1739 set otherrefids($name) $id
1740 lappend idotherrefs($id) $name
f1d83ba3
PM
1741 }
1742 }
062d671f 1743 catch {close $refd}
8a48571c 1744 set mainhead {}
219ea3a9 1745 set mainheadid {}
8a48571c 1746 catch {
c11ff120 1747 set mainheadid [exec git rev-parse HEAD]
8a48571c
PM
1748 set thehead [exec git symbolic-ref HEAD]
1749 if {[string match "refs/heads/*" $thehead]} {
1750 set mainhead [string range $thehead 11 end]
1751 }
1752 }
39816d60
AG
1753 set selectheadid {}
1754 if {$selecthead ne {}} {
1755 catch {
1756 set selectheadid [exec git rev-parse --verify $selecthead]
1757 }
1758 }
887fe3c4
PM
1759}
1760
8f489363
PM
1761# skip over fake commits
1762proc first_real_row {} {
7fcc92bf 1763 global nullid nullid2 numcommits
8f489363
PM
1764
1765 for {set row 0} {$row < $numcommits} {incr row} {
7fcc92bf 1766 set id [commitonrow $row]
8f489363
PM
1767 if {$id ne $nullid && $id ne $nullid2} {
1768 break
1769 }
1770 }
1771 return $row
1772}
1773
e11f1233
PM
1774# update things for a head moved to a child of its previous location
1775proc movehead {id name} {
1776 global headids idheads
1777
1778 removehead $headids($name) $name
1779 set headids($name) $id
1780 lappend idheads($id) $name
1781}
1782
1783# update things when a head has been removed
1784proc removehead {id name} {
1785 global headids idheads
1786
1787 if {$idheads($id) eq $name} {
1788 unset idheads($id)
1789 } else {
1790 set i [lsearch -exact $idheads($id) $name]
1791 if {$i >= 0} {
1792 set idheads($id) [lreplace $idheads($id) $i $i]
1793 }
1794 }
1795 unset headids($name)
1796}
1797
d93f1713
PT
1798proc ttk_toplevel {w args} {
1799 global use_ttk
1800 eval [linsert $args 0 ::toplevel $w]
1801 if {$use_ttk} {
1802 place [ttk::frame $w._toplevel_background] -x 0 -y 0 -relwidth 1 -relheight 1
1803 }
1804 return $w
1805}
1806
e7d64008
AG
1807proc make_transient {window origin} {
1808 global have_tk85
1809
1810 # In MacOS Tk 8.4 transient appears to work by setting
1811 # overrideredirect, which is utterly useless, since the
1812 # windows get no border, and are not even kept above
1813 # the parent.
1814 if {!$have_tk85 && [tk windowingsystem] eq {aqua}} return
1815
1816 wm transient $window $origin
1817
1818 # Windows fails to place transient windows normally, so
1819 # schedule a callback to center them on the parent.
1820 if {[tk windowingsystem] eq {win32}} {
1821 after idle [list tk::PlaceWindow $window widget $origin]
1822 }
1823}
1824
8d849957 1825proc show_error {w top msg {mc mc}} {
d93f1713 1826 global NS
3cb1f9c9 1827 if {![info exists NS]} {set NS ""}
d93f1713 1828 if {[wm state $top] eq "withdrawn"} { wm deiconify $top }
df3d83b1
PM
1829 message $w.m -text $msg -justify center -aspect 400
1830 pack $w.m -side top -fill x -padx 20 -pady 20
7a0ebbf8 1831 ${NS}::button $w.ok -default active -text [$mc OK] -command "destroy $top"
df3d83b1 1832 pack $w.ok -side bottom -fill x
e54be9e3
PM
1833 bind $top <Visibility> "grab $top; focus $top"
1834 bind $top <Key-Return> "destroy $top"
76f15947
AG
1835 bind $top <Key-space> "destroy $top"
1836 bind $top <Key-Escape> "destroy $top"
e54be9e3 1837 tkwait window $top
df3d83b1
PM
1838}
1839
84a76f18 1840proc error_popup {msg {owner .}} {
d93f1713
PT
1841 if {[tk windowingsystem] eq "win32"} {
1842 tk_messageBox -icon error -type ok -title [wm title .] \
1843 -parent $owner -message $msg
1844 } else {
1845 set w .error
1846 ttk_toplevel $w
1847 make_transient $w $owner
1848 show_error $w $w $msg
1849 }
098dd8a3
PM
1850}
1851
84a76f18 1852proc confirm_popup {msg {owner .}} {
d93f1713 1853 global confirm_ok NS
10299152
PM
1854 set confirm_ok 0
1855 set w .confirm
d93f1713 1856 ttk_toplevel $w
e7d64008 1857 make_transient $w $owner
10299152
PM
1858 message $w.m -text $msg -justify center -aspect 400
1859 pack $w.m -side top -fill x -padx 20 -pady 20
d93f1713 1860 ${NS}::button $w.ok -text [mc OK] -command "set confirm_ok 1; destroy $w"
10299152 1861 pack $w.ok -side left -fill x
d93f1713 1862 ${NS}::button $w.cancel -text [mc Cancel] -command "destroy $w"
10299152
PM
1863 pack $w.cancel -side right -fill x
1864 bind $w <Visibility> "grab $w; focus $w"
76f15947
AG
1865 bind $w <Key-Return> "set confirm_ok 1; destroy $w"
1866 bind $w <Key-space> "set confirm_ok 1; destroy $w"
1867 bind $w <Key-Escape> "destroy $w"
d93f1713 1868 tk::PlaceWindow $w widget $owner
10299152
PM
1869 tkwait window $w
1870 return $confirm_ok
1871}
1872
b039f0a6 1873proc setoptions {} {
d93f1713
PT
1874 if {[tk windowingsystem] ne "win32"} {
1875 option add *Panedwindow.showHandle 1 startupFile
1876 option add *Panedwindow.sashRelief raised startupFile
1877 if {[tk windowingsystem] ne "aqua"} {
1878 option add *Menu.font uifont startupFile
1879 }
1880 } else {
1881 option add *Menu.TearOff 0 startupFile
1882 }
b039f0a6
PM
1883 option add *Button.font uifont startupFile
1884 option add *Checkbutton.font uifont startupFile
1885 option add *Radiobutton.font uifont startupFile
b039f0a6
PM
1886 option add *Menubutton.font uifont startupFile
1887 option add *Label.font uifont startupFile
1888 option add *Message.font uifont startupFile
b9b142ff
MH
1889 option add *Entry.font textfont startupFile
1890 option add *Text.font textfont startupFile
d93f1713 1891 option add *Labelframe.font uifont startupFile
0933b04e 1892 option add *Spinbox.font textfont startupFile
207ad7b8 1893 option add *Listbox.font mainfont startupFile
b039f0a6
PM
1894}
1895
79056034
PM
1896# Make a menu and submenus.
1897# m is the window name for the menu, items is the list of menu items to add.
1898# Each item is a list {mc label type description options...}
1899# mc is ignored; it's so we can put mc there to alert xgettext
1900# label is the string that appears in the menu
1901# type is cascade, command or radiobutton (should add checkbutton)
1902# description depends on type; it's the sublist for cascade, the
1903# command to invoke for command, or {variable value} for radiobutton
f2d0bbbd
PM
1904proc makemenu {m items} {
1905 menu $m
cea07cf8
AG
1906 if {[tk windowingsystem] eq {aqua}} {
1907 set Meta1 Cmd
1908 } else {
1909 set Meta1 Ctrl
1910 }
f2d0bbbd 1911 foreach i $items {
79056034
PM
1912 set name [mc [lindex $i 1]]
1913 set type [lindex $i 2]
1914 set thing [lindex $i 3]
f2d0bbbd
PM
1915 set params [list $type]
1916 if {$name ne {}} {
1917 set u [string first "&" [string map {&& x} $name]]
1918 lappend params -label [string map {&& & & {}} $name]
1919 if {$u >= 0} {
1920 lappend params -underline $u
1921 }
1922 }
1923 switch -- $type {
1924 "cascade" {
79056034 1925 set submenu [string tolower [string map {& ""} [lindex $i 1]]]
f2d0bbbd
PM
1926 lappend params -menu $m.$submenu
1927 }
1928 "command" {
1929 lappend params -command $thing
1930 }
1931 "radiobutton" {
1932 lappend params -variable [lindex $thing 0] \
1933 -value [lindex $thing 1]
1934 }
1935 }
cea07cf8
AG
1936 set tail [lrange $i 4 end]
1937 regsub -all {\yMeta1\y} $tail $Meta1 tail
1938 eval $m add $params $tail
f2d0bbbd
PM
1939 if {$type eq "cascade"} {
1940 makemenu $m.$submenu $thing
1941 }
1942 }
1943}
1944
1945# translate string and remove ampersands
1946proc mca {str} {
1947 return [string map {&& & & {}} [mc $str]]
1948}
1949
d93f1713
PT
1950proc makedroplist {w varname args} {
1951 global use_ttk
1952 if {$use_ttk} {
3cb1f9c9
PT
1953 set width 0
1954 foreach label $args {
1955 set cx [string length $label]
1956 if {$cx > $width} {set width $cx}
1957 }
1958 set gm [ttk::combobox $w -width $width -state readonly\
d93f1713
PT
1959 -textvariable $varname -values $args]
1960 } else {
1961 set gm [eval [linsert $args 0 tk_optionMenu $w $varname]]
1962 }
1963 return $gm
1964}
1965
d94f8cd6 1966proc makewindow {} {
31c0eaa8 1967 global canv canv2 canv3 linespc charspc ctext cflist cscroll
9c311b32 1968 global tabstop
b74fd579 1969 global findtype findtypemenu findloc findstring fstring geometry
887fe3c4 1970 global entries sha1entry sha1string sha1but
890fae70 1971 global diffcontextstring diffcontext
b9b86007 1972 global ignorespace
94a2eede 1973 global maincursor textcursor curtextcursor
219ea3a9 1974 global rowctxmenu fakerowmenu mergemax wrapcomment
60f7a7dc 1975 global highlight_files gdttype
3ea06f9f 1976 global searchstring sstring
60378c0c 1977 global bgcolor fgcolor bglist fglist diffcolors selectbgcolor
bb3edc8b
PM
1978 global headctxmenu progresscanv progressitem progresscoords statusw
1979 global fprogitem fprogcoord lastprogupdate progupdatepending
6df7403a 1980 global rprogitem rprogcoord rownumsel numcommits
d93f1713 1981 global have_tk85 use_ttk NS
ae4e3ff9
TR
1982 global git_version
1983 global worddiff
9a40c50c 1984
79056034
PM
1985 # The "mc" arguments here are purely so that xgettext
1986 # sees the following string as needing to be translated
5fdcbb13
DS
1987 set file {
1988 mc "File" cascade {
79056034 1989 {mc "Update" command updatecommits -accelerator F5}
cea07cf8 1990 {mc "Reload" command reloadcommits -accelerator Meta1-F5}
79056034 1991 {mc "Reread references" command rereadrefs}
cea07cf8 1992 {mc "List references" command showrefs -accelerator F2}
7fb0abb1
AG
1993 {xx "" separator}
1994 {mc "Start git gui" command {exec git gui &}}
1995 {xx "" separator}
cea07cf8 1996 {mc "Quit" command doquit -accelerator Meta1-Q}
f2d0bbbd 1997 }}
5fdcbb13
DS
1998 set edit {
1999 mc "Edit" cascade {
79056034 2000 {mc "Preferences" command doprefs}
f2d0bbbd 2001 }}
5fdcbb13
DS
2002 set view {
2003 mc "View" cascade {
cea07cf8
AG
2004 {mc "New view..." command {newview 0} -accelerator Shift-F4}
2005 {mc "Edit view..." command editview -state disabled -accelerator F4}
79056034
PM
2006 {mc "Delete view" command delview -state disabled}
2007 {xx "" separator}
2008 {mc "All files" radiobutton {selectedview 0} -command {showview 0}}
f2d0bbbd 2009 }}
5fdcbb13
DS
2010 if {[tk windowingsystem] ne "aqua"} {
2011 set help {
2012 mc "Help" cascade {
2013 {mc "About gitk" command about}
2014 {mc "Key bindings" command keys}
2015 }}
2016 set bar [list $file $edit $view $help]
2017 } else {
2018 proc ::tk::mac::ShowPreferences {} {doprefs}
2019 proc ::tk::mac::Quit {} {doquit}
2020 lset file end [lreplace [lindex $file end] end-1 end]
2021 set apple {
2022 xx "Apple" cascade {
79056034 2023 {mc "About gitk" command about}
5fdcbb13
DS
2024 {xx "" separator}
2025 }}
2026 set help {
2027 mc "Help" cascade {
79056034 2028 {mc "Key bindings" command keys}
f2d0bbbd 2029 }}
5fdcbb13 2030 set bar [list $apple $file $view $help]
f2d0bbbd 2031 }
5fdcbb13 2032 makemenu .bar $bar
9a40c50c
PM
2033 . configure -menu .bar
2034
d93f1713
PT
2035 if {$use_ttk} {
2036 # cover the non-themed toplevel with a themed frame.
2037 place [ttk::frame ._main_background] -x 0 -y 0 -relwidth 1 -relheight 1
2038 }
2039
e9937d2a 2040 # the gui has upper and lower half, parts of a paned window.
d93f1713 2041 ${NS}::panedwindow .ctop -orient vertical
e9937d2a
JH
2042
2043 # possibly use assumed geometry
9ca72f4f 2044 if {![info exists geometry(pwsash0)]} {
e9937d2a
JH
2045 set geometry(topheight) [expr {15 * $linespc}]
2046 set geometry(topwidth) [expr {80 * $charspc}]
2047 set geometry(botheight) [expr {15 * $linespc}]
2048 set geometry(botwidth) [expr {50 * $charspc}]
d93f1713
PT
2049 set geometry(pwsash0) [list [expr {40 * $charspc}] 2]
2050 set geometry(pwsash1) [list [expr {60 * $charspc}] 2]
e9937d2a
JH
2051 }
2052
2053 # the upper half will have a paned window, a scroll bar to the right, and some stuff below
d93f1713
PT
2054 ${NS}::frame .tf -height $geometry(topheight) -width $geometry(topwidth)
2055 ${NS}::frame .tf.histframe
2056 ${NS}::panedwindow .tf.histframe.pwclist -orient horizontal
2057 if {!$use_ttk} {
2058 .tf.histframe.pwclist configure -sashpad 0 -handlesize 4
2059 }
e9937d2a
JH
2060
2061 # create three canvases
2062 set cscroll .tf.histframe.csb
2063 set canv .tf.histframe.pwclist.canv
9ca72f4f 2064 canvas $canv \
60378c0c 2065 -selectbackground $selectbgcolor \
f8a2c0d1 2066 -background $bgcolor -bd 0 \
9f1afe05 2067 -yscrollincr $linespc -yscrollcommand "scrollcanv $cscroll"
e9937d2a
JH
2068 .tf.histframe.pwclist add $canv
2069 set canv2 .tf.histframe.pwclist.canv2
9ca72f4f 2070 canvas $canv2 \
60378c0c 2071 -selectbackground $selectbgcolor \
f8a2c0d1 2072 -background $bgcolor -bd 0 -yscrollincr $linespc
e9937d2a
JH
2073 .tf.histframe.pwclist add $canv2
2074 set canv3 .tf.histframe.pwclist.canv3
9ca72f4f 2075 canvas $canv3 \
60378c0c 2076 -selectbackground $selectbgcolor \
f8a2c0d1 2077 -background $bgcolor -bd 0 -yscrollincr $linespc
e9937d2a 2078 .tf.histframe.pwclist add $canv3
d93f1713
PT
2079 if {$use_ttk} {
2080 bind .tf.histframe.pwclist <Map> {
2081 bind %W <Map> {}
2082 .tf.histframe.pwclist sashpos 1 [lindex $::geometry(pwsash1) 0]
2083 .tf.histframe.pwclist sashpos 0 [lindex $::geometry(pwsash0) 0]
2084 }
2085 } else {
2086 eval .tf.histframe.pwclist sash place 0 $geometry(pwsash0)
2087 eval .tf.histframe.pwclist sash place 1 $geometry(pwsash1)
2088 }
e9937d2a
JH
2089
2090 # a scroll bar to rule them
d93f1713
PT
2091 ${NS}::scrollbar $cscroll -command {allcanvs yview}
2092 if {!$use_ttk} {$cscroll configure -highlightthickness 0}
e9937d2a
JH
2093 pack $cscroll -side right -fill y
2094 bind .tf.histframe.pwclist <Configure> {resizeclistpanes %W %w}
f8a2c0d1 2095 lappend bglist $canv $canv2 $canv3
e9937d2a 2096 pack .tf.histframe.pwclist -fill both -expand 1 -side left
98f350e5 2097
e9937d2a 2098 # we have two button bars at bottom of top frame. Bar 1
d93f1713
PT
2099 ${NS}::frame .tf.bar
2100 ${NS}::frame .tf.lbar -height 15
e9937d2a
JH
2101
2102 set sha1entry .tf.bar.sha1
887fe3c4 2103 set entries $sha1entry
e9937d2a 2104 set sha1but .tf.bar.sha1label
0359ba72 2105 button $sha1but -text "[mc "SHA1 ID:"] " -state disabled -relief flat \
b039f0a6 2106 -command gotocommit -width 8
887fe3c4 2107 $sha1but conf -disabledforeground [$sha1but cget -foreground]
e9937d2a 2108 pack .tf.bar.sha1label -side left
d93f1713 2109 ${NS}::entry $sha1entry -width 40 -font textfont -textvariable sha1string
887fe3c4 2110 trace add variable sha1string write sha1change
98f350e5 2111 pack $sha1entry -side left -pady 2
d698206c
PM
2112
2113 image create bitmap bm-left -data {
2114 #define left_width 16
2115 #define left_height 16
2116 static unsigned char left_bits[] = {
2117 0x00, 0x00, 0xc0, 0x01, 0xe0, 0x00, 0x70, 0x00, 0x38, 0x00, 0x1c, 0x00,
2118 0x0e, 0x00, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0x0e, 0x00, 0x1c, 0x00,
2119 0x38, 0x00, 0x70, 0x00, 0xe0, 0x00, 0xc0, 0x01};
2120 }
2121 image create bitmap bm-right -data {
2122 #define right_width 16
2123 #define right_height 16
2124 static unsigned char right_bits[] = {
2125 0x00, 0x00, 0xc0, 0x01, 0x80, 0x03, 0x00, 0x07, 0x00, 0x0e, 0x00, 0x1c,
2126 0x00, 0x38, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0x00, 0x38, 0x00, 0x1c,
2127 0x00, 0x0e, 0x00, 0x07, 0x80, 0x03, 0xc0, 0x01};
2128 }
d93f1713 2129 ${NS}::button .tf.bar.leftbut -image bm-left -command goback \
d698206c 2130 -state disabled -width 26
e9937d2a 2131 pack .tf.bar.leftbut -side left -fill y
d93f1713 2132 ${NS}::button .tf.bar.rightbut -image bm-right -command goforw \
d698206c 2133 -state disabled -width 26
e9937d2a 2134 pack .tf.bar.rightbut -side left -fill y
d698206c 2135
d93f1713 2136 ${NS}::label .tf.bar.rowlabel -text [mc "Row"]
6df7403a 2137 set rownumsel {}
d93f1713 2138 ${NS}::label .tf.bar.rownum -width 7 -textvariable rownumsel \
6df7403a 2139 -relief sunken -anchor e
d93f1713
PT
2140 ${NS}::label .tf.bar.rowlabel2 -text "/"
2141 ${NS}::label .tf.bar.numcommits -width 7 -textvariable numcommits \
6df7403a
PM
2142 -relief sunken -anchor e
2143 pack .tf.bar.rowlabel .tf.bar.rownum .tf.bar.rowlabel2 .tf.bar.numcommits \
2144 -side left
d93f1713
PT
2145 if {!$use_ttk} {
2146 foreach w {rownum numcommits} {.tf.bar.$w configure -font textfont}
2147 }
6df7403a 2148 global selectedline
94b4a69f 2149 trace add variable selectedline write selectedline_change
6df7403a 2150
bb3edc8b
PM
2151 # Status label and progress bar
2152 set statusw .tf.bar.status
d93f1713 2153 ${NS}::label $statusw -width 15 -relief sunken
bb3edc8b 2154 pack $statusw -side left -padx 5
d93f1713
PT
2155 if {$use_ttk} {
2156 set progresscanv [ttk::progressbar .tf.bar.progress]
2157 } else {
2158 set h [expr {[font metrics uifont -linespace] + 2}]
2159 set progresscanv .tf.bar.progress
2160 canvas $progresscanv -relief sunken -height $h -borderwidth 2
2161 set progressitem [$progresscanv create rect -1 0 0 $h -fill green]
2162 set fprogitem [$progresscanv create rect -1 0 0 $h -fill yellow]
2163 set rprogitem [$progresscanv create rect -1 0 0 $h -fill red]
2164 }
2165 pack $progresscanv -side right -expand 1 -fill x -padx {0 2}
bb3edc8b
PM
2166 set progresscoords {0 0}
2167 set fprogcoord 0
a137a90f 2168 set rprogcoord 0
bb3edc8b
PM
2169 bind $progresscanv <Configure> adjustprogress
2170 set lastprogupdate [clock clicks -milliseconds]
2171 set progupdatepending 0
2172
687c8765 2173 # build up the bottom bar of upper window
d93f1713
PT
2174 ${NS}::label .tf.lbar.flabel -text "[mc "Find"] "
2175 ${NS}::button .tf.lbar.fnext -text [mc "next"] -command {dofind 1 1}
2176 ${NS}::button .tf.lbar.fprev -text [mc "prev"] -command {dofind -1 1}
2177 ${NS}::label .tf.lbar.flab2 -text " [mc "commit"] "
687c8765
PM
2178 pack .tf.lbar.flabel .tf.lbar.fnext .tf.lbar.fprev .tf.lbar.flab2 \
2179 -side left -fill y
b007ee20 2180 set gdttype [mc "containing:"]
3cb1f9c9 2181 set gm [makedroplist .tf.lbar.gdttype gdttype \
b007ee20
CS
2182 [mc "containing:"] \
2183 [mc "touching paths:"] \
2184 [mc "adding/removing string:"]]
687c8765 2185 trace add variable gdttype write gdttype_change
687c8765
PM
2186 pack .tf.lbar.gdttype -side left -fill y
2187
98f350e5 2188 set findstring {}
687c8765 2189 set fstring .tf.lbar.findstring
887fe3c4 2190 lappend entries $fstring
b9b142ff 2191 ${NS}::entry $fstring -width 30 -textvariable findstring
60f7a7dc 2192 trace add variable findstring write find_change
b007ee20 2193 set findtype [mc "Exact"]
d93f1713
PT
2194 set findtypemenu [makedroplist .tf.lbar.findtype \
2195 findtype [mc "Exact"] [mc "IgnCase"] [mc "Regexp"]]
687c8765 2196 trace add variable findtype write findcom_change
b007ee20 2197 set findloc [mc "All fields"]
d93f1713 2198 makedroplist .tf.lbar.findloc findloc [mc "All fields"] [mc "Headline"] \
b007ee20 2199 [mc "Comments"] [mc "Author"] [mc "Committer"]
60f7a7dc 2200 trace add variable findloc write find_change
687c8765
PM
2201 pack .tf.lbar.findloc -side right
2202 pack .tf.lbar.findtype -side right
2203 pack $fstring -side left -expand 1 -fill x
e9937d2a
JH
2204
2205 # Finish putting the upper half of the viewer together
2206 pack .tf.lbar -in .tf -side bottom -fill x
2207 pack .tf.bar -in .tf -side bottom -fill x
2208 pack .tf.histframe -fill both -side top -expand 1
2209 .ctop add .tf
d93f1713
PT
2210 if {!$use_ttk} {
2211 .ctop paneconfigure .tf -height $geometry(topheight)
2212 .ctop paneconfigure .tf -width $geometry(topwidth)
2213 }
e9937d2a
JH
2214
2215 # now build up the bottom
d93f1713 2216 ${NS}::panedwindow .pwbottom -orient horizontal
e9937d2a
JH
2217
2218 # lower left, a text box over search bar, scroll bar to the right
2219 # if we know window height, then that will set the lower text height, otherwise
2220 # we set lower text height which will drive window height
2221 if {[info exists geometry(main)]} {
d93f1713 2222 ${NS}::frame .bleft -width $geometry(botwidth)
e9937d2a 2223 } else {
d93f1713 2224 ${NS}::frame .bleft -width $geometry(botwidth) -height $geometry(botheight)
e9937d2a 2225 }
d93f1713
PT
2226 ${NS}::frame .bleft.top
2227 ${NS}::frame .bleft.mid
2228 ${NS}::frame .bleft.bottom
e9937d2a 2229
d93f1713 2230 ${NS}::button .bleft.top.search -text [mc "Search"] -command dosearch
e9937d2a
JH
2231 pack .bleft.top.search -side left -padx 5
2232 set sstring .bleft.top.sstring
d93f1713 2233 set searchstring ""
b9b142ff 2234 ${NS}::entry $sstring -width 20 -textvariable searchstring
3ea06f9f
PM
2235 lappend entries $sstring
2236 trace add variable searchstring write incrsearch
2237 pack $sstring -side left -expand 1 -fill x
d93f1713 2238 ${NS}::radiobutton .bleft.mid.diff -text [mc "Diff"] \
a8d610a2 2239 -command changediffdisp -variable diffelide -value {0 0}
d93f1713 2240 ${NS}::radiobutton .bleft.mid.old -text [mc "Old version"] \
a8d610a2 2241 -command changediffdisp -variable diffelide -value {0 1}
d93f1713 2242 ${NS}::radiobutton .bleft.mid.new -text [mc "New version"] \
a8d610a2 2243 -command changediffdisp -variable diffelide -value {1 0}
d93f1713 2244 ${NS}::label .bleft.mid.labeldiffcontext -text " [mc "Lines of context"]: "
a8d610a2 2245 pack .bleft.mid.diff .bleft.mid.old .bleft.mid.new -side left
0933b04e 2246 spinbox .bleft.mid.diffcontext -width 5 \
a41ddbb6 2247 -from 0 -increment 1 -to 10000000 \
890fae70
SP
2248 -validate all -validatecommand "diffcontextvalidate %P" \
2249 -textvariable diffcontextstring
2250 .bleft.mid.diffcontext set $diffcontext
2251 trace add variable diffcontextstring write diffcontextchange
2252 lappend entries .bleft.mid.diffcontext
2253 pack .bleft.mid.labeldiffcontext .bleft.mid.diffcontext -side left
d93f1713 2254 ${NS}::checkbutton .bleft.mid.ignspace -text [mc "Ignore space change"] \
b9b86007
SP
2255 -command changeignorespace -variable ignorespace
2256 pack .bleft.mid.ignspace -side left -padx 5
ae4e3ff9
TR
2257
2258 set worddiff [mc "Line diff"]
2259 if {[package vcompare $git_version "1.7.2"] >= 0} {
2260 makedroplist .bleft.mid.worddiff worddiff [mc "Line diff"] \
2261 [mc "Markup words"] [mc "Color words"]
2262 trace add variable worddiff write changeworddiff
2263 pack .bleft.mid.worddiff -side left -padx 5
2264 }
2265
8809d691 2266 set ctext .bleft.bottom.ctext
f8a2c0d1 2267 text $ctext -background $bgcolor -foreground $fgcolor \
9c311b32 2268 -state disabled -font textfont \
8809d691
PK
2269 -yscrollcommand scrolltext -wrap none \
2270 -xscrollcommand ".bleft.bottom.sbhorizontal set"
32f1b3e4
PM
2271 if {$have_tk85} {
2272 $ctext conf -tabstyle wordprocessor
2273 }
d93f1713
PT
2274 ${NS}::scrollbar .bleft.bottom.sb -command "$ctext yview"
2275 ${NS}::scrollbar .bleft.bottom.sbhorizontal -command "$ctext xview" -orient h
e9937d2a 2276 pack .bleft.top -side top -fill x
a8d610a2 2277 pack .bleft.mid -side top -fill x
8809d691
PK
2278 grid $ctext .bleft.bottom.sb -sticky nsew
2279 grid .bleft.bottom.sbhorizontal -sticky ew
2280 grid columnconfigure .bleft.bottom 0 -weight 1
2281 grid rowconfigure .bleft.bottom 0 -weight 1
2282 grid rowconfigure .bleft.bottom 1 -weight 0
2283 pack .bleft.bottom -side top -fill both -expand 1
f8a2c0d1
PM
2284 lappend bglist $ctext
2285 lappend fglist $ctext
d2610d11 2286
f1b86294 2287 $ctext tag conf comment -wrap $wrapcomment
9c311b32 2288 $ctext tag conf filesep -font textfontbold -back "#aaaaaa"
f8a2c0d1
PM
2289 $ctext tag conf hunksep -fore [lindex $diffcolors 2]
2290 $ctext tag conf d0 -fore [lindex $diffcolors 0]
8b07dca1 2291 $ctext tag conf dresult -fore [lindex $diffcolors 1]
712fcc08
PM
2292 $ctext tag conf m0 -fore red
2293 $ctext tag conf m1 -fore blue
2294 $ctext tag conf m2 -fore green
2295 $ctext tag conf m3 -fore purple
2296 $ctext tag conf m4 -fore brown
b77b0278
PM
2297 $ctext tag conf m5 -fore "#009090"
2298 $ctext tag conf m6 -fore magenta
2299 $ctext tag conf m7 -fore "#808000"
2300 $ctext tag conf m8 -fore "#009000"
2301 $ctext tag conf m9 -fore "#ff0080"
2302 $ctext tag conf m10 -fore cyan
2303 $ctext tag conf m11 -fore "#b07070"
2304 $ctext tag conf m12 -fore "#70b0f0"
2305 $ctext tag conf m13 -fore "#70f0b0"
2306 $ctext tag conf m14 -fore "#f0b070"
2307 $ctext tag conf m15 -fore "#ff70b0"
712fcc08 2308 $ctext tag conf mmax -fore darkgrey
b77b0278 2309 set mergemax 16
9c311b32
PM
2310 $ctext tag conf mresult -font textfontbold
2311 $ctext tag conf msep -font textfontbold
712fcc08 2312 $ctext tag conf found -back yellow
e5c2d856 2313
e9937d2a 2314 .pwbottom add .bleft
d93f1713
PT
2315 if {!$use_ttk} {
2316 .pwbottom paneconfigure .bleft -width $geometry(botwidth)
2317 }
e9937d2a
JH
2318
2319 # lower right
d93f1713
PT
2320 ${NS}::frame .bright
2321 ${NS}::frame .bright.mode
2322 ${NS}::radiobutton .bright.mode.patch -text [mc "Patch"] \
f8b28a40 2323 -command reselectline -variable cmitmode -value "patch"
d93f1713 2324 ${NS}::radiobutton .bright.mode.tree -text [mc "Tree"] \
f8b28a40 2325 -command reselectline -variable cmitmode -value "tree"
e9937d2a
JH
2326 grid .bright.mode.patch .bright.mode.tree -sticky ew
2327 pack .bright.mode -side top -fill x
2328 set cflist .bright.cfiles
9c311b32 2329 set indent [font measure mainfont "nn"]
e9937d2a 2330 text $cflist \
60378c0c 2331 -selectbackground $selectbgcolor \
f8a2c0d1 2332 -background $bgcolor -foreground $fgcolor \
9c311b32 2333 -font mainfont \
7fcceed7 2334 -tabs [list $indent [expr {2 * $indent}]] \
e9937d2a 2335 -yscrollcommand ".bright.sb set" \
7fcceed7
PM
2336 -cursor [. cget -cursor] \
2337 -spacing1 1 -spacing3 1
f8a2c0d1
PM
2338 lappend bglist $cflist
2339 lappend fglist $cflist
d93f1713 2340 ${NS}::scrollbar .bright.sb -command "$cflist yview"
e9937d2a 2341 pack .bright.sb -side right -fill y
d2610d11 2342 pack $cflist -side left -fill both -expand 1
89b11d3b
PM
2343 $cflist tag configure highlight \
2344 -background [$cflist cget -selectbackground]
9c311b32 2345 $cflist tag configure bold -font mainfontbold
d2610d11 2346
e9937d2a
JH
2347 .pwbottom add .bright
2348 .ctop add .pwbottom
1db95b00 2349
b9bee115 2350 # restore window width & height if known
e9937d2a 2351 if {[info exists geometry(main)]} {
b9bee115
PM
2352 if {[scan $geometry(main) "%dx%d" w h] >= 2} {
2353 if {$w > [winfo screenwidth .]} {
2354 set w [winfo screenwidth .]
2355 }
2356 if {$h > [winfo screenheight .]} {
2357 set h [winfo screenheight .]
2358 }
2359 wm geometry . "${w}x$h"
2360 }
e9937d2a
JH
2361 }
2362
c876dbad
PT
2363 if {[info exists geometry(state)] && $geometry(state) eq "zoomed"} {
2364 wm state . $geometry(state)
2365 }
2366
d23d98d3
SP
2367 if {[tk windowingsystem] eq {aqua}} {
2368 set M1B M1
5fdcbb13 2369 set ::BM "3"
d23d98d3
SP
2370 } else {
2371 set M1B Control
5fdcbb13 2372 set ::BM "2"
d23d98d3
SP
2373 }
2374
d93f1713
PT
2375 if {$use_ttk} {
2376 bind .ctop <Map> {
2377 bind %W <Map> {}
2378 %W sashpos 0 $::geometry(topheight)
2379 }
2380 bind .pwbottom <Map> {
2381 bind %W <Map> {}
2382 %W sashpos 0 $::geometry(botwidth)
2383 }
2384 }
2385
e9937d2a
JH
2386 bind .pwbottom <Configure> {resizecdetpanes %W %w}
2387 pack .ctop -fill both -expand 1
c8dfbcf9
PM
2388 bindall <1> {selcanvline %W %x %y}
2389 #bindall <B1-Motion> {selcanvline %W %x %y}
314c3093
ML
2390 if {[tk windowingsystem] == "win32"} {
2391 bind . <MouseWheel> { windows_mousewheel_redirector %W %X %Y %D }
2392 bind $ctext <MouseWheel> { windows_mousewheel_redirector %W %X %Y %D ; break }
2393 } else {
2394 bindall <ButtonRelease-4> "allcanvs yview scroll -5 units"
2395 bindall <ButtonRelease-5> "allcanvs yview scroll 5 units"
5dd57d51
JS
2396 if {[tk windowingsystem] eq "aqua"} {
2397 bindall <MouseWheel> {
2398 set delta [expr {- (%D)}]
2399 allcanvs yview scroll $delta units
2400 }
5fdcbb13
DS
2401 bindall <Shift-MouseWheel> {
2402 set delta [expr {- (%D)}]
2403 $canv xview scroll $delta units
2404 }
5dd57d51 2405 }
314c3093 2406 }
5fdcbb13
DS
2407 bindall <$::BM> "canvscan mark %W %x %y"
2408 bindall <B$::BM-Motion> "canvscan dragto %W %x %y"
decd0a1e
JL
2409 bind all <$M1B-Key-w> {destroy [winfo toplevel %W]}
2410 bind . <$M1B-Key-w> doquit
6e5f7203
RN
2411 bindkey <Home> selfirstline
2412 bindkey <End> sellastline
17386066
PM
2413 bind . <Key-Up> "selnextline -1"
2414 bind . <Key-Down> "selnextline 1"
cca5d946
PM
2415 bind . <Shift-Key-Up> "dofind -1 0"
2416 bind . <Shift-Key-Down> "dofind 1 0"
6e5f7203
RN
2417 bindkey <Key-Right> "goforw"
2418 bindkey <Key-Left> "goback"
2419 bind . <Key-Prior> "selnextpage -1"
2420 bind . <Key-Next> "selnextpage 1"
d23d98d3
SP
2421 bind . <$M1B-Home> "allcanvs yview moveto 0.0"
2422 bind . <$M1B-End> "allcanvs yview moveto 1.0"
2423 bind . <$M1B-Key-Up> "allcanvs yview scroll -1 units"
2424 bind . <$M1B-Key-Down> "allcanvs yview scroll 1 units"
2425 bind . <$M1B-Key-Prior> "allcanvs yview scroll -1 pages"
2426 bind . <$M1B-Key-Next> "allcanvs yview scroll 1 pages"
cfb4563c
PM
2427 bindkey <Key-Delete> "$ctext yview scroll -1 pages"
2428 bindkey <Key-BackSpace> "$ctext yview scroll -1 pages"
2429 bindkey <Key-space> "$ctext yview scroll 1 pages"
df3d83b1
PM
2430 bindkey p "selnextline -1"
2431 bindkey n "selnextline 1"
6e2dda35
RS
2432 bindkey z "goback"
2433 bindkey x "goforw"
811c70fc
JN
2434 bindkey k "selnextline -1"
2435 bindkey j "selnextline 1"
2436 bindkey h "goback"
6e2dda35 2437 bindkey l "goforw"
f4c54b3c 2438 bindkey b prevfile
cfb4563c
PM
2439 bindkey d "$ctext yview scroll 18 units"
2440 bindkey u "$ctext yview scroll -18 units"
97bed034 2441 bindkey / {focus $fstring}
b6e192db 2442 bindkey <Key-KP_Divide> {focus $fstring}
cca5d946
PM
2443 bindkey <Key-Return> {dofind 1 1}
2444 bindkey ? {dofind -1 1}
39ad8570 2445 bindkey f nextfile
cea07cf8
AG
2446 bind . <F5> updatecommits
2447 bind . <$M1B-F5> reloadcommits
2448 bind . <F2> showrefs
2449 bind . <Shift-F4> {newview 0}
2450 catch { bind . <Shift-Key-XF86_Switch_VT_4> {newview 0} }
2451 bind . <F4> edit_or_newview
d23d98d3 2452 bind . <$M1B-q> doquit
cca5d946
PM
2453 bind . <$M1B-f> {dofind 1 1}
2454 bind . <$M1B-g> {dofind 1 0}
d23d98d3
SP
2455 bind . <$M1B-r> dosearchback
2456 bind . <$M1B-s> dosearch
2457 bind . <$M1B-equal> {incrfont 1}
646f3a14 2458 bind . <$M1B-plus> {incrfont 1}
d23d98d3
SP
2459 bind . <$M1B-KP_Add> {incrfont 1}
2460 bind . <$M1B-minus> {incrfont -1}
2461 bind . <$M1B-KP_Subtract> {incrfont -1}
b6047c5a 2462 wm protocol . WM_DELETE_WINDOW doquit
e2f90ee4 2463 bind . <Destroy> {stop_backends}
df3d83b1 2464 bind . <Button-1> "click %W"
cca5d946 2465 bind $fstring <Key-Return> {dofind 1 1}
968ce45c 2466 bind $sha1entry <Key-Return> {gotocommit; break}
ee3dc72e 2467 bind $sha1entry <<PasteSelection>> clearsha1
7fcceed7
PM
2468 bind $cflist <1> {sel_flist %W %x %y; break}
2469 bind $cflist <B1-Motion> {sel_flist %W %x %y; break}
f8b28a40 2470 bind $cflist <ButtonRelease-1> {treeclick %W %x %y}
d277e89f
PM
2471 global ctxbut
2472 bind $cflist $ctxbut {pop_flist_menu %W %X %Y %x %y}
7cdc3556 2473 bind $ctext $ctxbut {pop_diff_menu %W %X %Y %x %y}
4adcbea0 2474 bind $ctext <Button-1> {focus %W}
ea13cba1
PM
2475
2476 set maincursor [. cget -cursor]
2477 set textcursor [$ctext cget -cursor]
94a2eede 2478 set curtextcursor $textcursor
84ba7345 2479
c8dfbcf9 2480 set rowctxmenu .rowctxmenu
f2d0bbbd 2481 makemenu $rowctxmenu {
79056034
PM
2482 {mc "Diff this -> selected" command {diffvssel 0}}
2483 {mc "Diff selected -> this" command {diffvssel 1}}
2484 {mc "Make patch" command mkpatch}
2485 {mc "Create tag" command mktag}
2486 {mc "Write commit to file" command writecommit}
2487 {mc "Create new branch" command mkbranch}
2488 {mc "Cherry-pick this commit" command cherrypick}
2489 {mc "Reset HEAD branch to here" command resethead}
b9fdba7f
PM
2490 {mc "Mark this commit" command markhere}
2491 {mc "Return to mark" command gotomark}
2492 {mc "Find descendant of this and mark" command find_common_desc}
010509f2 2493 {mc "Compare with marked commit" command compare_commits}
f2d0bbbd
PM
2494 }
2495 $rowctxmenu configure -tearoff 0
10299152 2496
219ea3a9 2497 set fakerowmenu .fakerowmenu
f2d0bbbd 2498 makemenu $fakerowmenu {
79056034
PM
2499 {mc "Diff this -> selected" command {diffvssel 0}}
2500 {mc "Diff selected -> this" command {diffvssel 1}}
2501 {mc "Make patch" command mkpatch}
f2d0bbbd
PM
2502 }
2503 $fakerowmenu configure -tearoff 0
219ea3a9 2504
10299152 2505 set headctxmenu .headctxmenu
f2d0bbbd 2506 makemenu $headctxmenu {
79056034
PM
2507 {mc "Check out this branch" command cobranch}
2508 {mc "Remove this branch" command rmbranch}
f2d0bbbd
PM
2509 }
2510 $headctxmenu configure -tearoff 0
3244729a
PM
2511
2512 global flist_menu
2513 set flist_menu .flistctxmenu
f2d0bbbd 2514 makemenu $flist_menu {
79056034
PM
2515 {mc "Highlight this too" command {flist_hl 0}}
2516 {mc "Highlight this only" command {flist_hl 1}}
2517 {mc "External diff" command {external_diff}}
2518 {mc "Blame parent commit" command {external_blame 1}}
f2d0bbbd
PM
2519 }
2520 $flist_menu configure -tearoff 0
7cdc3556
AG
2521
2522 global diff_menu
2523 set diff_menu .diffctxmenu
2524 makemenu $diff_menu {
8a897742 2525 {mc "Show origin of this line" command show_line_source}
7cdc3556
AG
2526 {mc "Run git gui blame on this line" command {external_blame_diff}}
2527 }
2528 $diff_menu configure -tearoff 0
df3d83b1
PM
2529}
2530
314c3093
ML
2531# Windows sends all mouse wheel events to the current focused window, not
2532# the one where the mouse hovers, so bind those events here and redirect
2533# to the correct window
2534proc windows_mousewheel_redirector {W X Y D} {
2535 global canv canv2 canv3
2536 set w [winfo containing -displayof $W $X $Y]
2537 if {$w ne ""} {
2538 set u [expr {$D < 0 ? 5 : -5}]
2539 if {$w == $canv || $w == $canv2 || $w == $canv3} {
2540 allcanvs yview scroll $u units
2541 } else {
2542 catch {
2543 $w yview scroll $u units
2544 }
2545 }
2546 }
2547}
2548
6df7403a
PM
2549# Update row number label when selectedline changes
2550proc selectedline_change {n1 n2 op} {
2551 global selectedline rownumsel
2552
94b4a69f 2553 if {$selectedline eq {}} {
6df7403a
PM
2554 set rownumsel {}
2555 } else {
2556 set rownumsel [expr {$selectedline + 1}]
2557 }
2558}
2559
be0cd098
PM
2560# mouse-2 makes all windows scan vertically, but only the one
2561# the cursor is in scans horizontally
2562proc canvscan {op w x y} {
2563 global canv canv2 canv3
2564 foreach c [list $canv $canv2 $canv3] {
2565 if {$c == $w} {
2566 $c scan $op $x $y
2567 } else {
2568 $c scan $op 0 $y
2569 }
2570 }
2571}
2572
9f1afe05
PM
2573proc scrollcanv {cscroll f0 f1} {
2574 $cscroll set $f0 $f1
31c0eaa8 2575 drawvisible
908c3585 2576 flushhighlights
9f1afe05
PM
2577}
2578
df3d83b1
PM
2579# when we make a key binding for the toplevel, make sure
2580# it doesn't get triggered when that key is pressed in the
2581# find string entry widget.
2582proc bindkey {ev script} {
887fe3c4 2583 global entries
df3d83b1
PM
2584 bind . $ev $script
2585 set escript [bind Entry $ev]
2586 if {$escript == {}} {
2587 set escript [bind Entry <Key>]
2588 }
887fe3c4
PM
2589 foreach e $entries {
2590 bind $e $ev "$escript; break"
2591 }
df3d83b1
PM
2592}
2593
2594# set the focus back to the toplevel for any click outside
887fe3c4 2595# the entry widgets
df3d83b1 2596proc click {w} {
bd441de4
ML
2597 global ctext entries
2598 foreach e [concat $entries $ctext] {
887fe3c4 2599 if {$w == $e} return
df3d83b1 2600 }
887fe3c4 2601 focus .
0fba86b3
PM
2602}
2603
bb3edc8b
PM
2604# Adjust the progress bar for a change in requested extent or canvas size
2605proc adjustprogress {} {
2606 global progresscanv progressitem progresscoords
2607 global fprogitem fprogcoord lastprogupdate progupdatepending
d93f1713
PT
2608 global rprogitem rprogcoord use_ttk
2609
2610 if {$use_ttk} {
2611 $progresscanv configure -value [expr {int($fprogcoord * 100)}]
2612 return
2613 }
bb3edc8b
PM
2614
2615 set w [expr {[winfo width $progresscanv] - 4}]
2616 set x0 [expr {$w * [lindex $progresscoords 0]}]
2617 set x1 [expr {$w * [lindex $progresscoords 1]}]
2618 set h [winfo height $progresscanv]
2619 $progresscanv coords $progressitem $x0 0 $x1 $h
2620 $progresscanv coords $fprogitem 0 0 [expr {$w * $fprogcoord}] $h
a137a90f 2621 $progresscanv coords $rprogitem 0 0 [expr {$w * $rprogcoord}] $h
bb3edc8b
PM
2622 set now [clock clicks -milliseconds]
2623 if {$now >= $lastprogupdate + 100} {
2624 set progupdatepending 0
2625 update
2626 } elseif {!$progupdatepending} {
2627 set progupdatepending 1
2628 after [expr {$lastprogupdate + 100 - $now}] doprogupdate
2629 }
2630}
2631
2632proc doprogupdate {} {
2633 global lastprogupdate progupdatepending
2634
2635 if {$progupdatepending} {
2636 set progupdatepending 0
2637 set lastprogupdate [clock clicks -milliseconds]
2638 update
2639 }
2640}
2641
0fba86b3 2642proc savestuff {w} {
32f1b3e4 2643 global canv canv2 canv3 mainfont textfont uifont tabstop
712fcc08 2644 global stuffsaved findmergefiles maxgraphpct
219ea3a9 2645 global maxwidth showneartags showlocalchanges
2d480856 2646 global viewname viewfiles viewargs viewargscmd viewperm nextviewnum
7a39a17a 2647 global cmitmode wrapcomment datetimeformat limitdiffs
5497f7a2 2648 global colors uicolor bgcolor fgcolor diffcolors diffcontext selectbgcolor
21ac8a8d 2649 global autoselect autosellen extdifftool perfile_attrs markbgcolor use_ttk
0cc08ff7 2650 global hideremotes want_ttk
4ef17537 2651
0fba86b3 2652 if {$stuffsaved} return
df3d83b1 2653 if {![winfo viewable .]} return
0fba86b3 2654 catch {
9bedb0e1 2655 if {[file exists ~/.gitk-new]} {file delete -force ~/.gitk-new}
0fba86b3 2656 set f [open "~/.gitk-new" w]
9832e4f2
PM
2657 if {$::tcl_platform(platform) eq {windows}} {
2658 file attributes "~/.gitk-new" -hidden true
2659 }
f0654861
PM
2660 puts $f [list set mainfont $mainfont]
2661 puts $f [list set textfont $textfont]
4840be66 2662 puts $f [list set uifont $uifont]
7e12f1a6 2663 puts $f [list set tabstop $tabstop]
f0654861 2664 puts $f [list set findmergefiles $findmergefiles]
8d858d1a 2665 puts $f [list set maxgraphpct $maxgraphpct]
04c13d38 2666 puts $f [list set maxwidth $maxwidth]
f8b28a40 2667 puts $f [list set cmitmode $cmitmode]
f1b86294 2668 puts $f [list set wrapcomment $wrapcomment]
95293b58 2669 puts $f [list set autoselect $autoselect]
21ac8a8d 2670 puts $f [list set autosellen $autosellen]
b8ab2e17 2671 puts $f [list set showneartags $showneartags]
ffe15297 2672 puts $f [list set hideremotes $hideremotes]
219ea3a9 2673 puts $f [list set showlocalchanges $showlocalchanges]
e8b5f4be 2674 puts $f [list set datetimeformat $datetimeformat]
7a39a17a 2675 puts $f [list set limitdiffs $limitdiffs]
5497f7a2 2676 puts $f [list set uicolor $uicolor]
0cc08ff7 2677 puts $f [list set want_ttk $want_ttk]
f8a2c0d1
PM
2678 puts $f [list set bgcolor $bgcolor]
2679 puts $f [list set fgcolor $fgcolor]
2680 puts $f [list set colors $colors]
2681 puts $f [list set diffcolors $diffcolors]
e3e901be 2682 puts $f [list set markbgcolor $markbgcolor]
890fae70 2683 puts $f [list set diffcontext $diffcontext]
60378c0c 2684 puts $f [list set selectbgcolor $selectbgcolor]
314f5de1 2685 puts $f [list set extdifftool $extdifftool]
39ee47ef 2686 puts $f [list set perfile_attrs $perfile_attrs]
e9937d2a 2687
b6047c5a 2688 puts $f "set geometry(main) [wm geometry .]"
c876dbad 2689 puts $f "set geometry(state) [wm state .]"
e9937d2a
JH
2690 puts $f "set geometry(topwidth) [winfo width .tf]"
2691 puts $f "set geometry(topheight) [winfo height .tf]"
d93f1713
PT
2692 if {$use_ttk} {
2693 puts $f "set geometry(pwsash0) \"[.tf.histframe.pwclist sashpos 0] 1\""
2694 puts $f "set geometry(pwsash1) \"[.tf.histframe.pwclist sashpos 1] 1\""
2695 } else {
2696 puts $f "set geometry(pwsash0) \"[.tf.histframe.pwclist sash coord 0]\""
2697 puts $f "set geometry(pwsash1) \"[.tf.histframe.pwclist sash coord 1]\""
2698 }
e9937d2a
JH
2699 puts $f "set geometry(botwidth) [winfo width .bleft]"
2700 puts $f "set geometry(botheight) [winfo height .bleft]"
2701
a90a6d24
PM
2702 puts -nonewline $f "set permviews {"
2703 for {set v 0} {$v < $nextviewnum} {incr v} {
2704 if {$viewperm($v)} {
2d480856 2705 puts $f "{[list $viewname($v) $viewfiles($v) $viewargs($v) $viewargscmd($v)]}"
a90a6d24
PM
2706 }
2707 }
2708 puts $f "}"
0fba86b3
PM
2709 close $f
2710 file rename -force "~/.gitk-new" "~/.gitk"
2711 }
2712 set stuffsaved 1
1db95b00
PM
2713}
2714
43bddeb4 2715proc resizeclistpanes {win w} {
d93f1713 2716 global oldwidth use_ttk
418c4c7b 2717 if {[info exists oldwidth($win)]} {
d93f1713
PT
2718 if {$use_ttk} {
2719 set s0 [$win sashpos 0]
2720 set s1 [$win sashpos 1]
2721 } else {
2722 set s0 [$win sash coord 0]
2723 set s1 [$win sash coord 1]
2724 }
43bddeb4
PM
2725 if {$w < 60} {
2726 set sash0 [expr {int($w/2 - 2)}]
2727 set sash1 [expr {int($w*5/6 - 2)}]
2728 } else {
2729 set factor [expr {1.0 * $w / $oldwidth($win)}]
2730 set sash0 [expr {int($factor * [lindex $s0 0])}]
2731 set sash1 [expr {int($factor * [lindex $s1 0])}]
2732 if {$sash0 < 30} {
2733 set sash0 30
2734 }
2735 if {$sash1 < $sash0 + 20} {
2ed49d54 2736 set sash1 [expr {$sash0 + 20}]
43bddeb4
PM
2737 }
2738 if {$sash1 > $w - 10} {
2ed49d54 2739 set sash1 [expr {$w - 10}]
43bddeb4 2740 if {$sash0 > $sash1 - 20} {
2ed49d54 2741 set sash0 [expr {$sash1 - 20}]
43bddeb4
PM
2742 }
2743 }
2744 }
d93f1713
PT
2745 if {$use_ttk} {
2746 $win sashpos 0 $sash0
2747 $win sashpos 1 $sash1
2748 } else {
2749 $win sash place 0 $sash0 [lindex $s0 1]
2750 $win sash place 1 $sash1 [lindex $s1 1]
2751 }
43bddeb4
PM
2752 }
2753 set oldwidth($win) $w
2754}
2755
2756proc resizecdetpanes {win w} {
d93f1713 2757 global oldwidth use_ttk
418c4c7b 2758 if {[info exists oldwidth($win)]} {
d93f1713
PT
2759 if {$use_ttk} {
2760 set s0 [$win sashpos 0]
2761 } else {
2762 set s0 [$win sash coord 0]
2763 }
43bddeb4
PM
2764 if {$w < 60} {
2765 set sash0 [expr {int($w*3/4 - 2)}]
2766 } else {
2767 set factor [expr {1.0 * $w / $oldwidth($win)}]
2768 set sash0 [expr {int($factor * [lindex $s0 0])}]
2769 if {$sash0 < 45} {
2770 set sash0 45
2771 }
2772 if {$sash0 > $w - 15} {
2ed49d54 2773 set sash0 [expr {$w - 15}]
43bddeb4
PM
2774 }
2775 }
d93f1713
PT
2776 if {$use_ttk} {
2777 $win sashpos 0 $sash0
2778 } else {
2779 $win sash place 0 $sash0 [lindex $s0 1]
2780 }
43bddeb4
PM
2781 }
2782 set oldwidth($win) $w
2783}
2784
b5721c72
PM
2785proc allcanvs args {
2786 global canv canv2 canv3
2787 eval $canv $args
2788 eval $canv2 $args
2789 eval $canv3 $args
2790}
2791
2792proc bindall {event action} {
2793 global canv canv2 canv3
2794 bind $canv $event $action
2795 bind $canv2 $event $action
2796 bind $canv3 $event $action
2797}
2798
9a40c50c 2799proc about {} {
d93f1713 2800 global uifont NS
9a40c50c
PM
2801 set w .about
2802 if {[winfo exists $w]} {
2803 raise $w
2804 return
2805 }
d93f1713 2806 ttk_toplevel $w
d990cedf 2807 wm title $w [mc "About gitk"]
e7d64008 2808 make_transient $w .
d990cedf 2809 message $w.m -text [mc "
9f1afe05 2810Gitk - a commit viewer for git
9a40c50c 2811
bb3e86a1 2812Copyright \u00a9 2005-2011 Paul Mackerras
9a40c50c 2813
d990cedf 2814Use and redistribute under the terms of the GNU General Public License"] \
3a950e9a
ER
2815 -justify center -aspect 400 -border 2 -bg white -relief groove
2816 pack $w.m -side top -fill x -padx 2 -pady 2
d93f1713 2817 ${NS}::button $w.ok -text [mc "Close"] -command "destroy $w" -default active
9a40c50c 2818 pack $w.ok -side bottom
3a950e9a
ER
2819 bind $w <Visibility> "focus $w.ok"
2820 bind $w <Key-Escape> "destroy $w"
2821 bind $w <Key-Return> "destroy $w"
d93f1713 2822 tk::PlaceWindow $w widget .
9a40c50c
PM
2823}
2824
4e95e1f7 2825proc keys {} {
d93f1713 2826 global NS
4e95e1f7
PM
2827 set w .keys
2828 if {[winfo exists $w]} {
2829 raise $w
2830 return
2831 }
d23d98d3
SP
2832 if {[tk windowingsystem] eq {aqua}} {
2833 set M1T Cmd
2834 } else {
2835 set M1T Ctrl
2836 }
d93f1713 2837 ttk_toplevel $w
d990cedf 2838 wm title $w [mc "Gitk key bindings"]
e7d64008 2839 make_transient $w .
3d2c998e
MB
2840 message $w.m -text "
2841[mc "Gitk key bindings:"]
2842
2843[mc "<%s-Q> Quit" $M1T]
decd0a1e 2844[mc "<%s-W> Close window" $M1T]
3d2c998e
MB
2845[mc "<Home> Move to first commit"]
2846[mc "<End> Move to last commit"]
811c70fc
JN
2847[mc "<Up>, p, k Move up one commit"]
2848[mc "<Down>, n, j Move down one commit"]
2849[mc "<Left>, z, h Go back in history list"]
3d2c998e
MB
2850[mc "<Right>, x, l Go forward in history list"]
2851[mc "<PageUp> Move up one page in commit list"]
2852[mc "<PageDown> Move down one page in commit list"]
2853[mc "<%s-Home> Scroll to top of commit list" $M1T]
2854[mc "<%s-End> Scroll to bottom of commit list" $M1T]
2855[mc "<%s-Up> Scroll commit list up one line" $M1T]
2856[mc "<%s-Down> Scroll commit list down one line" $M1T]
2857[mc "<%s-PageUp> Scroll commit list up one page" $M1T]
2858[mc "<%s-PageDown> Scroll commit list down one page" $M1T]
2859[mc "<Shift-Up> Find backwards (upwards, later commits)"]
2860[mc "<Shift-Down> Find forwards (downwards, earlier commits)"]
2861[mc "<Delete>, b Scroll diff view up one page"]
2862[mc "<Backspace> Scroll diff view up one page"]
2863[mc "<Space> Scroll diff view down one page"]
2864[mc "u Scroll diff view up 18 lines"]
2865[mc "d Scroll diff view down 18 lines"]
2866[mc "<%s-F> Find" $M1T]
2867[mc "<%s-G> Move to next find hit" $M1T]
2868[mc "<Return> Move to next find hit"]
97bed034 2869[mc "/ Focus the search box"]
3d2c998e
MB
2870[mc "? Move to previous find hit"]
2871[mc "f Scroll diff view to next file"]
2872[mc "<%s-S> Search for next hit in diff view" $M1T]
2873[mc "<%s-R> Search for previous hit in diff view" $M1T]
2874[mc "<%s-KP+> Increase font size" $M1T]
2875[mc "<%s-plus> Increase font size" $M1T]
2876[mc "<%s-KP-> Decrease font size" $M1T]
2877[mc "<%s-minus> Decrease font size" $M1T]
2878[mc "<F5> Update"]
2879" \
3a950e9a
ER
2880 -justify left -bg white -border 2 -relief groove
2881 pack $w.m -side top -fill both -padx 2 -pady 2
d93f1713 2882 ${NS}::button $w.ok -text [mc "Close"] -command "destroy $w" -default active
76f15947 2883 bind $w <Key-Escape> [list destroy $w]
4e95e1f7 2884 pack $w.ok -side bottom
3a950e9a
ER
2885 bind $w <Visibility> "focus $w.ok"
2886 bind $w <Key-Escape> "destroy $w"
2887 bind $w <Key-Return> "destroy $w"
4e95e1f7
PM
2888}
2889
7fcceed7
PM
2890# Procedures for manipulating the file list window at the
2891# bottom right of the overall window.
f8b28a40
PM
2892
2893proc treeview {w l openlevs} {
2894 global treecontents treediropen treeheight treeparent treeindex
2895
2896 set ix 0
2897 set treeindex() 0
2898 set lev 0
2899 set prefix {}
2900 set prefixend -1
2901 set prefendstack {}
2902 set htstack {}
2903 set ht 0
2904 set treecontents() {}
2905 $w conf -state normal
2906 foreach f $l {
2907 while {[string range $f 0 $prefixend] ne $prefix} {
2908 if {$lev <= $openlevs} {
2909 $w mark set e:$treeindex($prefix) "end -1c"
2910 $w mark gravity e:$treeindex($prefix) left
2911 }
2912 set treeheight($prefix) $ht
2913 incr ht [lindex $htstack end]
2914 set htstack [lreplace $htstack end end]
2915 set prefixend [lindex $prefendstack end]
2916 set prefendstack [lreplace $prefendstack end end]
2917 set prefix [string range $prefix 0 $prefixend]
2918 incr lev -1
2919 }
2920 set tail [string range $f [expr {$prefixend+1}] end]
2921 while {[set slash [string first "/" $tail]] >= 0} {
2922 lappend htstack $ht
2923 set ht 0
2924 lappend prefendstack $prefixend
2925 incr prefixend [expr {$slash + 1}]
2926 set d [string range $tail 0 $slash]
2927 lappend treecontents($prefix) $d
2928 set oldprefix $prefix
2929 append prefix $d
2930 set treecontents($prefix) {}
2931 set treeindex($prefix) [incr ix]
2932 set treeparent($prefix) $oldprefix
2933 set tail [string range $tail [expr {$slash+1}] end]
2934 if {$lev <= $openlevs} {
2935 set ht 1
2936 set treediropen($prefix) [expr {$lev < $openlevs}]
2937 set bm [expr {$lev == $openlevs? "tri-rt": "tri-dn"}]
2938 $w mark set d:$ix "end -1c"
2939 $w mark gravity d:$ix left
2940 set str "\n"
2941 for {set i 0} {$i < $lev} {incr i} {append str "\t"}
2942 $w insert end $str
2943 $w image create end -align center -image $bm -padx 1 \
2944 -name a:$ix
45a9d505 2945 $w insert end $d [highlight_tag $prefix]
f8b28a40
PM
2946 $w mark set s:$ix "end -1c"
2947 $w mark gravity s:$ix left
2948 }
2949 incr lev
2950 }
2951 if {$tail ne {}} {
2952 if {$lev <= $openlevs} {
2953 incr ht
2954 set str "\n"
2955 for {set i 0} {$i < $lev} {incr i} {append str "\t"}
2956 $w insert end $str
45a9d505 2957 $w insert end $tail [highlight_tag $f]
f8b28a40
PM
2958 }
2959 lappend treecontents($prefix) $tail
2960 }
2961 }
2962 while {$htstack ne {}} {
2963 set treeheight($prefix) $ht
2964 incr ht [lindex $htstack end]
2965 set htstack [lreplace $htstack end end]
096e96b4
BD
2966 set prefixend [lindex $prefendstack end]
2967 set prefendstack [lreplace $prefendstack end end]
2968 set prefix [string range $prefix 0 $prefixend]
f8b28a40
PM
2969 }
2970 $w conf -state disabled
2971}
2972
2973proc linetoelt {l} {
2974 global treeheight treecontents
2975
2976 set y 2
2977 set prefix {}
2978 while {1} {
2979 foreach e $treecontents($prefix) {
2980 if {$y == $l} {
2981 return "$prefix$e"
2982 }
2983 set n 1
2984 if {[string index $e end] eq "/"} {
2985 set n $treeheight($prefix$e)
2986 if {$y + $n > $l} {
2987 append prefix $e
2988 incr y
2989 break
2990 }
2991 }
2992 incr y $n
2993 }
2994 }
2995}
2996
45a9d505
PM
2997proc highlight_tree {y prefix} {
2998 global treeheight treecontents cflist
2999
3000 foreach e $treecontents($prefix) {
3001 set path $prefix$e
3002 if {[highlight_tag $path] ne {}} {
3003 $cflist tag add bold $y.0 "$y.0 lineend"
3004 }
3005 incr y
3006 if {[string index $e end] eq "/" && $treeheight($path) > 1} {
3007 set y [highlight_tree $y $path]
3008 }
3009 }
3010 return $y
3011}
3012
f8b28a40
PM
3013proc treeclosedir {w dir} {
3014 global treediropen treeheight treeparent treeindex
3015
3016 set ix $treeindex($dir)
3017 $w conf -state normal
3018 $w delete s:$ix e:$ix
3019 set treediropen($dir) 0
3020 $w image configure a:$ix -image tri-rt
3021 $w conf -state disabled
3022 set n [expr {1 - $treeheight($dir)}]
3023 while {$dir ne {}} {
3024 incr treeheight($dir) $n
3025 set dir $treeparent($dir)
3026 }
3027}
3028
3029proc treeopendir {w dir} {
3030 global treediropen treeheight treeparent treecontents treeindex
3031
3032 set ix $treeindex($dir)
3033 $w conf -state normal
3034 $w image configure a:$ix -image tri-dn
3035 $w mark set e:$ix s:$ix
3036 $w mark gravity e:$ix right
3037 set lev 0
3038 set str "\n"
3039 set n [llength $treecontents($dir)]
3040 for {set x $dir} {$x ne {}} {set x $treeparent($x)} {
3041 incr lev
3042 append str "\t"
3043 incr treeheight($x) $n
3044 }
3045 foreach e $treecontents($dir) {
45a9d505 3046 set de $dir$e
f8b28a40 3047 if {[string index $e end] eq "/"} {
f8b28a40
PM
3048 set iy $treeindex($de)
3049 $w mark set d:$iy e:$ix
3050 $w mark gravity d:$iy left
3051 $w insert e:$ix $str
3052 set treediropen($de) 0
3053 $w image create e:$ix -align center -image tri-rt -padx 1 \
3054 -name a:$iy
45a9d505 3055 $w insert e:$ix $e [highlight_tag $de]
f8b28a40
PM
3056 $w mark set s:$iy e:$ix
3057 $w mark gravity s:$iy left
3058 set treeheight($de) 1
3059 } else {
3060 $w insert e:$ix $str
45a9d505 3061 $w insert e:$ix $e [highlight_tag $de]
f8b28a40
PM
3062 }
3063 }
b8a640ee 3064 $w mark gravity e:$ix right
f8b28a40
PM
3065 $w conf -state disabled
3066 set treediropen($dir) 1
3067 set top [lindex [split [$w index @0,0] .] 0]
3068 set ht [$w cget -height]
3069 set l [lindex [split [$w index s:$ix] .] 0]
3070 if {$l < $top} {
3071 $w yview $l.0
3072 } elseif {$l + $n + 1 > $top + $ht} {
3073 set top [expr {$l + $n + 2 - $ht}]
3074 if {$l < $top} {
3075 set top $l
3076 }
3077 $w yview $top.0
3078 }
3079}
3080
3081proc treeclick {w x y} {
3082 global treediropen cmitmode ctext cflist cflist_top
3083
3084 if {$cmitmode ne "tree"} return
3085 if {![info exists cflist_top]} return
3086 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3087 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
3088 $cflist tag add highlight $l.0 "$l.0 lineend"
3089 set cflist_top $l
3090 if {$l == 1} {
3091 $ctext yview 1.0
3092 return
3093 }
3094 set e [linetoelt $l]
3095 if {[string index $e end] ne "/"} {
3096 showfile $e
3097 } elseif {$treediropen($e)} {
3098 treeclosedir $w $e
3099 } else {
3100 treeopendir $w $e
3101 }
3102}
3103
3104proc setfilelist {id} {
8a897742 3105 global treefilelist cflist jump_to_here
f8b28a40
PM
3106
3107 treeview $cflist $treefilelist($id) 0
8a897742
PM
3108 if {$jump_to_here ne {}} {
3109 set f [lindex $jump_to_here 0]
3110 if {[lsearch -exact $treefilelist($id) $f] >= 0} {
3111 showfile $f
3112 }
3113 }
f8b28a40
PM
3114}
3115
3116image create bitmap tri-rt -background black -foreground blue -data {
3117 #define tri-rt_width 13
3118 #define tri-rt_height 13
3119 static unsigned char tri-rt_bits[] = {
3120 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x30, 0x00, 0x70, 0x00, 0xf0, 0x00,
3121 0xf0, 0x01, 0xf0, 0x00, 0x70, 0x00, 0x30, 0x00, 0x10, 0x00, 0x00, 0x00,
3122 0x00, 0x00};
3123} -maskdata {
3124 #define tri-rt-mask_width 13
3125 #define tri-rt-mask_height 13
3126 static unsigned char tri-rt-mask_bits[] = {
3127 0x08, 0x00, 0x18, 0x00, 0x38, 0x00, 0x78, 0x00, 0xf8, 0x00, 0xf8, 0x01,
3128 0xf8, 0x03, 0xf8, 0x01, 0xf8, 0x00, 0x78, 0x00, 0x38, 0x00, 0x18, 0x00,
3129 0x08, 0x00};
3130}
3131image create bitmap tri-dn -background black -foreground blue -data {
3132 #define tri-dn_width 13
3133 #define tri-dn_height 13
3134 static unsigned char tri-dn_bits[] = {
3135 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x07, 0xf8, 0x03,
3136 0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3137 0x00, 0x00};
3138} -maskdata {
3139 #define tri-dn-mask_width 13
3140 #define tri-dn-mask_height 13
3141 static unsigned char tri-dn-mask_bits[] = {
3142 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x1f, 0xfe, 0x0f, 0xfc, 0x07,
3143 0xf8, 0x03, 0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
3144 0x00, 0x00};
3145}
3146
887c996e
PM
3147image create bitmap reficon-T -background black -foreground yellow -data {
3148 #define tagicon_width 13
3149 #define tagicon_height 9
3150 static unsigned char tagicon_bits[] = {
3151 0x00, 0x00, 0x00, 0x00, 0xf0, 0x07, 0xf8, 0x07,
3152 0xfc, 0x07, 0xf8, 0x07, 0xf0, 0x07, 0x00, 0x00, 0x00, 0x00};
3153} -maskdata {
3154 #define tagicon-mask_width 13
3155 #define tagicon-mask_height 9
3156 static unsigned char tagicon-mask_bits[] = {
3157 0x00, 0x00, 0xf0, 0x0f, 0xf8, 0x0f, 0xfc, 0x0f,
3158 0xfe, 0x0f, 0xfc, 0x0f, 0xf8, 0x0f, 0xf0, 0x0f, 0x00, 0x00};
3159}
3160set rectdata {
3161 #define headicon_width 13
3162 #define headicon_height 9
3163 static unsigned char headicon_bits[] = {
3164 0x00, 0x00, 0x00, 0x00, 0xf8, 0x07, 0xf8, 0x07,
3165 0xf8, 0x07, 0xf8, 0x07, 0xf8, 0x07, 0x00, 0x00, 0x00, 0x00};
3166}
3167set rectmask {
3168 #define headicon-mask_width 13
3169 #define headicon-mask_height 9
3170 static unsigned char headicon-mask_bits[] = {
3171 0x00, 0x00, 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f,
3172 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f, 0x00, 0x00};
3173}
3174image create bitmap reficon-H -background black -foreground green \
3175 -data $rectdata -maskdata $rectmask
3176image create bitmap reficon-o -background black -foreground "#ddddff" \
3177 -data $rectdata -maskdata $rectmask
3178
7fcceed7 3179proc init_flist {first} {
7fcc92bf 3180 global cflist cflist_top difffilestart
7fcceed7
PM
3181
3182 $cflist conf -state normal
3183 $cflist delete 0.0 end
3184 if {$first ne {}} {
3185 $cflist insert end $first
3186 set cflist_top 1
7fcceed7
PM
3187 $cflist tag add highlight 1.0 "1.0 lineend"
3188 } else {
3189 catch {unset cflist_top}
3190 }
3191 $cflist conf -state disabled
3192 set difffilestart {}
3193}
3194
63b79191
PM
3195proc highlight_tag {f} {
3196 global highlight_paths
3197
3198 foreach p $highlight_paths {
3199 if {[string match $p $f]} {
3200 return "bold"
3201 }
3202 }
3203 return {}
3204}
3205
3206proc highlight_filelist {} {
45a9d505 3207 global cmitmode cflist
63b79191 3208
45a9d505
PM
3209 $cflist conf -state normal
3210 if {$cmitmode ne "tree"} {
63b79191
PM
3211 set end [lindex [split [$cflist index end] .] 0]
3212 for {set l 2} {$l < $end} {incr l} {
3213 set line [$cflist get $l.0 "$l.0 lineend"]
3214 if {[highlight_tag $line] ne {}} {
3215 $cflist tag add bold $l.0 "$l.0 lineend"
3216 }
3217 }
45a9d505
PM
3218 } else {
3219 highlight_tree 2 {}
63b79191 3220 }
45a9d505 3221 $cflist conf -state disabled
63b79191
PM
3222}
3223
3224proc unhighlight_filelist {} {
45a9d505 3225 global cflist
63b79191 3226
45a9d505
PM
3227 $cflist conf -state normal
3228 $cflist tag remove bold 1.0 end
3229 $cflist conf -state disabled
63b79191
PM
3230}
3231
f8b28a40 3232proc add_flist {fl} {
45a9d505 3233 global cflist
7fcceed7 3234
45a9d505
PM
3235 $cflist conf -state normal
3236 foreach f $fl {
3237 $cflist insert end "\n"
3238 $cflist insert end $f [highlight_tag $f]
7fcceed7 3239 }
45a9d505 3240 $cflist conf -state disabled
7fcceed7
PM
3241}
3242
3243proc sel_flist {w x y} {
45a9d505 3244 global ctext difffilestart cflist cflist_top cmitmode
7fcceed7 3245
f8b28a40 3246 if {$cmitmode eq "tree"} return
7fcceed7
PM
3247 if {![info exists cflist_top]} return
3248 set l [lindex [split [$w index "@$x,$y"] "."] 0]
89b11d3b
PM
3249 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
3250 $cflist tag add highlight $l.0 "$l.0 lineend"
3251 set cflist_top $l
f8b28a40
PM
3252 if {$l == 1} {
3253 $ctext yview 1.0
3254 } else {
3255 catch {$ctext yview [lindex $difffilestart [expr {$l - 2}]]}
7fcceed7 3256 }
7fcceed7
PM
3257}
3258
3244729a
PM
3259proc pop_flist_menu {w X Y x y} {
3260 global ctext cflist cmitmode flist_menu flist_menu_file
3261 global treediffs diffids
3262
bb3edc8b 3263 stopfinding
3244729a
PM
3264 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3265 if {$l <= 1} return
3266 if {$cmitmode eq "tree"} {
3267 set e [linetoelt $l]
3268 if {[string index $e end] eq "/"} return
3269 } else {
3270 set e [lindex $treediffs($diffids) [expr {$l-2}]]
3271 }
3272 set flist_menu_file $e
314f5de1
TA
3273 set xdiffstate "normal"
3274 if {$cmitmode eq "tree"} {
3275 set xdiffstate "disabled"
3276 }
3277 # Disable "External diff" item in tree mode
3278 $flist_menu entryconf 2 -state $xdiffstate
3244729a
PM
3279 tk_popup $flist_menu $X $Y
3280}
3281
7cdc3556
AG
3282proc find_ctext_fileinfo {line} {
3283 global ctext_file_names ctext_file_lines
3284
3285 set ok [bsearch $ctext_file_lines $line]
3286 set tline [lindex $ctext_file_lines $ok]
3287
3288 if {$ok >= [llength $ctext_file_lines] || $line < $tline} {
3289 return {}
3290 } else {
3291 return [list [lindex $ctext_file_names $ok] $tline]
3292 }
3293}
3294
3295proc pop_diff_menu {w X Y x y} {
3296 global ctext diff_menu flist_menu_file
3297 global diff_menu_txtpos diff_menu_line
3298 global diff_menu_filebase
3299
7cdc3556
AG
3300 set diff_menu_txtpos [split [$w index "@$x,$y"] "."]
3301 set diff_menu_line [lindex $diff_menu_txtpos 0]
190ec52c
PM
3302 # don't pop up the menu on hunk-separator or file-separator lines
3303 if {[lsearch -glob [$ctext tag names $diff_menu_line.0] "*sep"] >= 0} {
3304 return
3305 }
3306 stopfinding
7cdc3556
AG
3307 set f [find_ctext_fileinfo $diff_menu_line]
3308 if {$f eq {}} return
3309 set flist_menu_file [lindex $f 0]
3310 set diff_menu_filebase [lindex $f 1]
3311 tk_popup $diff_menu $X $Y
3312}
3313
3244729a 3314proc flist_hl {only} {
bb3edc8b 3315 global flist_menu_file findstring gdttype
3244729a
PM
3316
3317 set x [shellquote $flist_menu_file]
b007ee20 3318 if {$only || $findstring eq {} || $gdttype ne [mc "touching paths:"]} {
bb3edc8b 3319 set findstring $x
3244729a 3320 } else {
bb3edc8b 3321 append findstring " " $x
3244729a 3322 }
b007ee20 3323 set gdttype [mc "touching paths:"]
3244729a
PM
3324}
3325
c21398be
PM
3326proc gitknewtmpdir {} {
3327 global diffnum gitktmpdir gitdir
3328
3329 if {![info exists gitktmpdir]} {
929f577e 3330 set gitktmpdir [file join $gitdir [format ".gitk-tmp.%s" [pid]]]
c21398be
PM
3331 if {[catch {file mkdir $gitktmpdir} err]} {
3332 error_popup "[mc "Error creating temporary directory %s:" $gitktmpdir] $err"
3333 unset gitktmpdir
3334 return {}
3335 }
3336 set diffnum 0
3337 }
3338 incr diffnum
3339 set diffdir [file join $gitktmpdir $diffnum]
3340 if {[catch {file mkdir $diffdir} err]} {
3341 error_popup "[mc "Error creating temporary directory %s:" $diffdir] $err"
3342 return {}
3343 }
3344 return $diffdir
3345}
3346
314f5de1
TA
3347proc save_file_from_commit {filename output what} {
3348 global nullfile
3349
3350 if {[catch {exec git show $filename -- > $output} err]} {
3351 if {[string match "fatal: bad revision *" $err]} {
3352 return $nullfile
3353 }
3945d2c0 3354 error_popup "[mc "Error getting \"%s\" from %s:" $filename $what] $err"
314f5de1
TA
3355 return {}
3356 }
3357 return $output
3358}
3359
3360proc external_diff_get_one_file {diffid filename diffdir} {
3361 global nullid nullid2 nullfile
784b7e2f 3362 global worktree
314f5de1
TA
3363
3364 if {$diffid == $nullid} {
784b7e2f 3365 set difffile [file join $worktree $filename]
314f5de1
TA
3366 if {[file exists $difffile]} {
3367 return $difffile
3368 }
3369 return $nullfile
3370 }
3371 if {$diffid == $nullid2} {
3372 set difffile [file join $diffdir "\[index\] [file tail $filename]"]
3373 return [save_file_from_commit :$filename $difffile index]
3374 }
3375 set difffile [file join $diffdir "\[$diffid\] [file tail $filename]"]
3376 return [save_file_from_commit $diffid:$filename $difffile \
3377 "revision $diffid"]
3378}
3379
3380proc external_diff {} {
c21398be 3381 global nullid nullid2
314f5de1
TA
3382 global flist_menu_file
3383 global diffids
c21398be 3384 global extdifftool
314f5de1
TA
3385
3386 if {[llength $diffids] == 1} {
3387 # no reference commit given
3388 set diffidto [lindex $diffids 0]
3389 if {$diffidto eq $nullid} {
3390 # diffing working copy with index
3391 set diffidfrom $nullid2
3392 } elseif {$diffidto eq $nullid2} {
3393 # diffing index with HEAD
3394 set diffidfrom "HEAD"
3395 } else {
3396 # use first parent commit
3397 global parentlist selectedline
3398 set diffidfrom [lindex $parentlist $selectedline 0]
3399 }
3400 } else {
3401 set diffidfrom [lindex $diffids 0]
3402 set diffidto [lindex $diffids 1]
3403 }
3404
3405 # make sure that several diffs wont collide
c21398be
PM
3406 set diffdir [gitknewtmpdir]
3407 if {$diffdir eq {}} return
314f5de1
TA
3408
3409 # gather files to diff
3410 set difffromfile [external_diff_get_one_file $diffidfrom $flist_menu_file $diffdir]
3411 set difftofile [external_diff_get_one_file $diffidto $flist_menu_file $diffdir]
3412
3413 if {$difffromfile ne {} && $difftofile ne {}} {
b575b2f1
PT
3414 set cmd [list [shellsplit $extdifftool] $difffromfile $difftofile]
3415 if {[catch {set fl [open |$cmd r]} err]} {
314f5de1 3416 file delete -force $diffdir
3945d2c0 3417 error_popup "$extdifftool: [mc "command failed:"] $err"
314f5de1
TA
3418 } else {
3419 fconfigure $fl -blocking 0
3420 filerun $fl [list delete_at_eof $fl $diffdir]
3421 }
3422 }
3423}
3424
7cdc3556
AG
3425proc find_hunk_blamespec {base line} {
3426 global ctext
3427
3428 # Find and parse the hunk header
3429 set s_lix [$ctext search -backwards -regexp ^@@ "$line.0 lineend" $base.0]
3430 if {$s_lix eq {}} return
3431
3432 set s_line [$ctext get $s_lix "$s_lix + 1 lines"]
3433 if {![regexp {^@@@*(( -\d+(,\d+)?)+) \+(\d+)(,\d+)? @@} $s_line \
3434 s_line old_specs osz osz1 new_line nsz]} {
3435 return
3436 }
3437
3438 # base lines for the parents
3439 set base_lines [list $new_line]
3440 foreach old_spec [lrange [split $old_specs " "] 1 end] {
3441 if {![regexp -- {-(\d+)(,\d+)?} $old_spec \
3442 old_spec old_line osz]} {
3443 return
3444 }
3445 lappend base_lines $old_line
3446 }
3447
3448 # Now scan the lines to determine offset within the hunk
7cdc3556
AG
3449 set max_parent [expr {[llength $base_lines]-2}]
3450 set dline 0
3451 set s_lno [lindex [split $s_lix "."] 0]
3452
190ec52c
PM
3453 # Determine if the line is removed
3454 set chunk [$ctext get $line.0 "$line.1 + $max_parent chars"]
3455 if {[string match {[-+ ]*} $chunk]} {
7cdc3556
AG
3456 set removed_idx [string first "-" $chunk]
3457 # Choose a parent index
190ec52c
PM
3458 if {$removed_idx >= 0} {
3459 set parent $removed_idx
3460 } else {
3461 set unchanged_idx [string first " " $chunk]
3462 if {$unchanged_idx >= 0} {
3463 set parent $unchanged_idx
7cdc3556 3464 } else {
190ec52c
PM
3465 # blame the current commit
3466 set parent -1
7cdc3556
AG
3467 }
3468 }
3469 # then count other lines that belong to it
190ec52c
PM
3470 for {set i $line} {[incr i -1] > $s_lno} {} {
3471 set chunk [$ctext get $i.0 "$i.1 + $max_parent chars"]
3472 # Determine if the line is removed
3473 set removed_idx [string first "-" $chunk]
3474 if {$parent >= 0} {
3475 set code [string index $chunk $parent]
3476 if {$code eq "-" || ($removed_idx < 0 && $code ne "+")} {
3477 incr dline
3478 }
3479 } else {
3480 if {$removed_idx < 0} {
3481 incr dline
3482 }
7cdc3556
AG
3483 }
3484 }
190ec52c
PM
3485 incr parent
3486 } else {
3487 set parent 0
7cdc3556
AG
3488 }
3489
7cdc3556
AG
3490 incr dline [lindex $base_lines $parent]
3491 return [list $parent $dline]
3492}
3493
3494proc external_blame_diff {} {
8b07dca1 3495 global currentid cmitmode
7cdc3556
AG
3496 global diff_menu_txtpos diff_menu_line
3497 global diff_menu_filebase flist_menu_file
3498
3499 if {$cmitmode eq "tree"} {
3500 set parent_idx 0
190ec52c 3501 set line [expr {$diff_menu_line - $diff_menu_filebase}]
7cdc3556
AG
3502 } else {
3503 set hinfo [find_hunk_blamespec $diff_menu_filebase $diff_menu_line]
3504 if {$hinfo ne {}} {
3505 set parent_idx [lindex $hinfo 0]
3506 set line [lindex $hinfo 1]
3507 } else {
3508 set parent_idx 0
3509 set line 0
3510 }
3511 }
3512
3513 external_blame $parent_idx $line
3514}
3515
fc4977e1
PM
3516# Find the SHA1 ID of the blob for file $fname in the index
3517# at stage 0 or 2
3518proc index_sha1 {fname} {
3519 set f [open [list | git ls-files -s $fname] r]
3520 while {[gets $f line] >= 0} {
3521 set info [lindex [split $line "\t"] 0]
3522 set stage [lindex $info 2]
3523 if {$stage eq "0" || $stage eq "2"} {
3524 close $f
3525 return [lindex $info 1]
3526 }
3527 }
3528 close $f
3529 return {}
3530}
3531
9712b81a
PM
3532# Turn an absolute path into one relative to the current directory
3533proc make_relative {f} {
a4390ace
MH
3534 if {[file pathtype $f] eq "relative"} {
3535 return $f
3536 }
9712b81a
PM
3537 set elts [file split $f]
3538 set here [file split [pwd]]
3539 set ei 0
3540 set hi 0
3541 set res {}
3542 foreach d $here {
3543 if {$ei < $hi || $ei >= [llength $elts] || [lindex $elts $ei] ne $d} {
3544 lappend res ".."
3545 } else {
3546 incr ei
3547 }
3548 incr hi
3549 }
3550 set elts [concat $res [lrange $elts $ei end]]
3551 return [eval file join $elts]
3552}
3553
7cdc3556 3554proc external_blame {parent_idx {line {}}} {
0a2a9793 3555 global flist_menu_file cdup
77aa0ae8
AG
3556 global nullid nullid2
3557 global parentlist selectedline currentid
3558
3559 if {$parent_idx > 0} {
3560 set base_commit [lindex $parentlist $selectedline [expr {$parent_idx-1}]]
3561 } else {
3562 set base_commit $currentid
3563 }
3564
3565 if {$base_commit eq {} || $base_commit eq $nullid || $base_commit eq $nullid2} {
3566 error_popup [mc "No such commit"]
3567 return
3568 }
3569
7cdc3556
AG
3570 set cmdline [list git gui blame]
3571 if {$line ne {} && $line > 1} {
3572 lappend cmdline "--line=$line"
3573 }
0a2a9793 3574 set f [file join $cdup $flist_menu_file]
9712b81a
PM
3575 # Unfortunately it seems git gui blame doesn't like
3576 # being given an absolute path...
3577 set f [make_relative $f]
3578 lappend cmdline $base_commit $f
7cdc3556 3579 if {[catch {eval exec $cmdline &} err]} {
3945d2c0 3580 error_popup "[mc "git gui blame: command failed:"] $err"
77aa0ae8
AG
3581 }
3582}
3583
8a897742
PM
3584proc show_line_source {} {
3585 global cmitmode currentid parents curview blamestuff blameinst
3586 global diff_menu_line diff_menu_filebase flist_menu_file
9b6adf34 3587 global nullid nullid2 gitdir cdup
8a897742 3588
fc4977e1 3589 set from_index {}
8a897742
PM
3590 if {$cmitmode eq "tree"} {
3591 set id $currentid
3592 set line [expr {$diff_menu_line - $diff_menu_filebase}]
3593 } else {
3594 set h [find_hunk_blamespec $diff_menu_filebase $diff_menu_line]
3595 if {$h eq {}} return
3596 set pi [lindex $h 0]
3597 if {$pi == 0} {
3598 mark_ctext_line $diff_menu_line
3599 return
3600 }
fc4977e1
PM
3601 incr pi -1
3602 if {$currentid eq $nullid} {
3603 if {$pi > 0} {
3604 # must be a merge in progress...
3605 if {[catch {
3606 # get the last line from .git/MERGE_HEAD
3607 set f [open [file join $gitdir MERGE_HEAD] r]
3608 set id [lindex [split [read $f] "\n"] end-1]
3609 close $f
3610 } err]} {
3611 error_popup [mc "Couldn't read merge head: %s" $err]
3612 return
3613 }
3614 } elseif {$parents($curview,$currentid) eq $nullid2} {
3615 # need to do the blame from the index
3616 if {[catch {
3617 set from_index [index_sha1 $flist_menu_file]
3618 } err]} {
3619 error_popup [mc "Error reading index: %s" $err]
3620 return
3621 }
9712b81a
PM
3622 } else {
3623 set id $parents($curview,$currentid)
fc4977e1
PM
3624 }
3625 } else {
3626 set id [lindex $parents($curview,$currentid) $pi]
3627 }
8a897742
PM
3628 set line [lindex $h 1]
3629 }
fc4977e1
PM
3630 set blameargs {}
3631 if {$from_index ne {}} {
3632 lappend blameargs | git cat-file blob $from_index
3633 }
3634 lappend blameargs | git blame -p -L$line,+1
3635 if {$from_index ne {}} {
3636 lappend blameargs --contents -
3637 } else {
3638 lappend blameargs $id
3639 }
9b6adf34 3640 lappend blameargs -- [file join $cdup $flist_menu_file]
8a897742 3641 if {[catch {
fc4977e1 3642 set f [open $blameargs r]
8a897742
PM
3643 } err]} {
3644 error_popup [mc "Couldn't start git blame: %s" $err]
3645 return
3646 }
f3413079 3647 nowbusy blaming [mc "Searching"]
8a897742
PM
3648 fconfigure $f -blocking 0
3649 set i [reg_instance $f]
3650 set blamestuff($i) {}
3651 set blameinst $i
3652 filerun $f [list read_line_source $f $i]
3653}
3654
3655proc stopblaming {} {
3656 global blameinst
3657
3658 if {[info exists blameinst]} {
3659 stop_instance $blameinst
3660 unset blameinst
f3413079 3661 notbusy blaming
8a897742
PM
3662 }
3663}
3664
3665proc read_line_source {fd inst} {
fc4977e1 3666 global blamestuff curview commfd blameinst nullid nullid2
8a897742
PM
3667
3668 while {[gets $fd line] >= 0} {
3669 lappend blamestuff($inst) $line
3670 }
3671 if {![eof $fd]} {
3672 return 1
3673 }
3674 unset commfd($inst)
3675 unset blameinst
f3413079 3676 notbusy blaming
8a897742
PM
3677 fconfigure $fd -blocking 1
3678 if {[catch {close $fd} err]} {
3679 error_popup [mc "Error running git blame: %s" $err]
3680 return 0
3681 }
3682
3683 set fname {}
3684 set line [split [lindex $blamestuff($inst) 0] " "]
3685 set id [lindex $line 0]
3686 set lnum [lindex $line 1]
3687 if {[string length $id] == 40 && [string is xdigit $id] &&
3688 [string is digit -strict $lnum]} {
3689 # look for "filename" line
3690 foreach l $blamestuff($inst) {
3691 if {[string match "filename *" $l]} {
3692 set fname [string range $l 9 end]
3693 break
3694 }
3695 }
3696 }
3697 if {$fname ne {}} {
3698 # all looks good, select it
fc4977e1
PM
3699 if {$id eq $nullid} {
3700 # blame uses all-zeroes to mean not committed,
3701 # which would mean a change in the index
3702 set id $nullid2
3703 }
8a897742
PM
3704 if {[commitinview $id $curview]} {
3705 selectline [rowofcommit $id] 1 [list $fname $lnum]
3706 } else {
3707 error_popup [mc "That line comes from commit %s, \
3708 which is not in this view" [shortids $id]]
3709 }
3710 } else {
3711 puts "oops couldn't parse git blame output"
3712 }
3713 return 0
3714}
3715
314f5de1
TA
3716# delete $dir when we see eof on $f (presumably because the child has exited)
3717proc delete_at_eof {f dir} {
3718 while {[gets $f line] >= 0} {}
3719 if {[eof $f]} {
3720 if {[catch {close $f} err]} {
3945d2c0 3721 error_popup "[mc "External diff viewer failed:"] $err"
314f5de1
TA
3722 }
3723 file delete -force $dir
3724 return 0
3725 }
3726 return 1
3727}
3728
098dd8a3
PM
3729# Functions for adding and removing shell-type quoting
3730
3731proc shellquote {str} {
3732 if {![string match "*\['\"\\ \t]*" $str]} {
3733 return $str
3734 }
3735 if {![string match "*\['\"\\]*" $str]} {
3736 return "\"$str\""
3737 }
3738 if {![string match "*'*" $str]} {
3739 return "'$str'"
3740 }
3741 return "\"[string map {\" \\\" \\ \\\\} $str]\""
3742}
3743
3744proc shellarglist {l} {
3745 set str {}
3746 foreach a $l {
3747 if {$str ne {}} {
3748 append str " "
3749 }
3750 append str [shellquote $a]
3751 }
3752 return $str
3753}
3754
3755proc shelldequote {str} {
3756 set ret {}
3757 set used -1
3758 while {1} {
3759 incr used
3760 if {![regexp -start $used -indices "\['\"\\\\ \t]" $str first]} {
3761 append ret [string range $str $used end]
3762 set used [string length $str]
3763 break
3764 }
3765 set first [lindex $first 0]
3766 set ch [string index $str $first]
3767 if {$first > $used} {
3768 append ret [string range $str $used [expr {$first - 1}]]
3769 set used $first
3770 }
3771 if {$ch eq " " || $ch eq "\t"} break
3772 incr used
3773 if {$ch eq "'"} {
3774 set first [string first "'" $str $used]
3775 if {$first < 0} {
3776 error "unmatched single-quote"
3777 }
3778 append ret [string range $str $used [expr {$first - 1}]]
3779 set used $first
3780 continue
3781 }
3782 if {$ch eq "\\"} {
3783 if {$used >= [string length $str]} {
3784 error "trailing backslash"
3785 }
3786 append ret [string index $str $used]
3787 continue
3788 }
3789 # here ch == "\""
3790 while {1} {
3791 if {![regexp -start $used -indices "\[\"\\\\]" $str first]} {
3792 error "unmatched double-quote"
3793 }
3794 set first [lindex $first 0]
3795 set ch [string index $str $first]
3796 if {$first > $used} {
3797 append ret [string range $str $used [expr {$first - 1}]]
3798 set used $first
3799 }
3800 if {$ch eq "\""} break
3801 incr used
3802 append ret [string index $str $used]
3803 incr used
3804 }
3805 }
3806 return [list $used $ret]
3807}
3808
3809proc shellsplit {str} {
3810 set l {}
3811 while {1} {
3812 set str [string trimleft $str]
3813 if {$str eq {}} break
3814 set dq [shelldequote $str]
3815 set n [lindex $dq 0]
3816 set word [lindex $dq 1]
3817 set str [string range $str $n end]
3818 lappend l $word
3819 }
3820 return $l
3821}
3822
7fcceed7
PM
3823# Code to implement multiple views
3824
da7c24dd 3825proc newview {ishighlight} {
218a900b
AG
3826 global nextviewnum newviewname newishighlight
3827 global revtreeargs viewargscmd newviewopts curview
50b44ece 3828
da7c24dd 3829 set newishighlight $ishighlight
50b44ece
PM
3830 set top .gitkview
3831 if {[winfo exists $top]} {
3832 raise $top
3833 return
3834 }
5d11f794 3835 decode_view_opts $nextviewnum $revtreeargs
a3a1f579 3836 set newviewname($nextviewnum) "[mc "View"] $nextviewnum"
218a900b
AG
3837 set newviewopts($nextviewnum,perm) 0
3838 set newviewopts($nextviewnum,cmd) $viewargscmd($curview)
d990cedf 3839 vieweditor $top $nextviewnum [mc "Gitk view definition"]
d16c0812
PM
3840}
3841
218a900b 3842set known_view_options {
13d40b61
EN
3843 {perm b . {} {mc "Remember this view"}}
3844 {reflabel l + {} {mc "References (space separated list):"}}
3845 {refs t15 .. {} {mc "Branches & tags:"}}
3846 {allrefs b *. "--all" {mc "All refs"}}
3847 {branches b . "--branches" {mc "All (local) branches"}}
3848 {tags b . "--tags" {mc "All tags"}}
3849 {remotes b . "--remotes" {mc "All remote-tracking branches"}}
3850 {commitlbl l + {} {mc "Commit Info (regular expressions):"}}
3851 {author t15 .. "--author=*" {mc "Author:"}}
3852 {committer t15 . "--committer=*" {mc "Committer:"}}
3853 {loginfo t15 .. "--grep=*" {mc "Commit Message:"}}
3854 {allmatch b .. "--all-match" {mc "Matches all Commit Info criteria"}}
3855 {changes_l l + {} {mc "Changes to Files:"}}
3856 {pickaxe_s r0 . {} {mc "Fixed String"}}
3857 {pickaxe_t r1 . "--pickaxe-regex" {mc "Regular Expression"}}
3858 {pickaxe t15 .. "-S*" {mc "Search string:"}}
3859 {datelabel l + {} {mc "Commit Dates (\"2 weeks ago\", \"2009-03-17 15:27:38\", \"March 17, 2009 15:27:38\"):"}}
3860 {since t15 .. {"--since=*" "--after=*"} {mc "Since:"}}
3861 {until t15 . {"--until=*" "--before=*"} {mc "Until:"}}
3862 {limit_lbl l + {} {mc "Limit and/or skip a number of revisions (positive integer):"}}
3863 {limit t10 *. "--max-count=*" {mc "Number to show:"}}
3864 {skip t10 . "--skip=*" {mc "Number to skip:"}}
3865 {misc_lbl l + {} {mc "Miscellaneous options:"}}
3866 {dorder b *. {"--date-order" "-d"} {mc "Strictly sort by date"}}
3867 {lright b . "--left-right" {mc "Mark branch sides"}}
3868 {first b . "--first-parent" {mc "Limit to first parent"}}
f687aaa8 3869 {smplhst b . "--simplify-by-decoration" {mc "Simple history"}}
13d40b61
EN
3870 {args t50 *. {} {mc "Additional arguments to git log:"}}
3871 {allpaths path + {} {mc "Enter files and directories to include, one per line:"}}
3872 {cmd t50= + {} {mc "Command to generate more commits to include:"}}
218a900b
AG
3873 }
3874
e7feb695 3875# Convert $newviewopts($n, ...) into args for git log.
218a900b
AG
3876proc encode_view_opts {n} {
3877 global known_view_options newviewopts
3878
3879 set rargs [list]
3880 foreach opt $known_view_options {
3881 set patterns [lindex $opt 3]
3882 if {$patterns eq {}} continue
3883 set pattern [lindex $patterns 0]
3884
218a900b 3885 if {[lindex $opt 1] eq "b"} {
13d40b61 3886 set val $newviewopts($n,[lindex $opt 0])
218a900b
AG
3887 if {$val} {
3888 lappend rargs $pattern
3889 }
13d40b61
EN
3890 } elseif {[regexp {^r(\d+)$} [lindex $opt 1] type value]} {
3891 regexp {^(.*_)} [lindex $opt 0] uselessvar button_id
3892 set val $newviewopts($n,$button_id)
3893 if {$val eq $value} {
3894 lappend rargs $pattern
3895 }
218a900b 3896 } else {
13d40b61 3897 set val $newviewopts($n,[lindex $opt 0])
218a900b
AG
3898 set val [string trim $val]
3899 if {$val ne {}} {
3900 set pfix [string range $pattern 0 end-1]
3901 lappend rargs $pfix$val
3902 }
3903 }
3904 }
13d40b61 3905 set rargs [concat $rargs [shellsplit $newviewopts($n,refs)]]
218a900b
AG
3906 return [concat $rargs [shellsplit $newviewopts($n,args)]]
3907}
3908
e7feb695 3909# Fill $newviewopts($n, ...) based on args for git log.
218a900b
AG
3910proc decode_view_opts {n view_args} {
3911 global known_view_options newviewopts
3912
3913 foreach opt $known_view_options {
13d40b61 3914 set id [lindex $opt 0]
218a900b 3915 if {[lindex $opt 1] eq "b"} {
13d40b61
EN
3916 # Checkboxes
3917 set val 0
3918 } elseif {[regexp {^r(\d+)$} [lindex $opt 1]]} {
3919 # Radiobuttons
3920 regexp {^(.*_)} $id uselessvar id
218a900b
AG
3921 set val 0
3922 } else {
13d40b61 3923 # Text fields
218a900b
AG
3924 set val {}
3925 }
13d40b61 3926 set newviewopts($n,$id) $val
218a900b
AG
3927 }
3928 set oargs [list]
13d40b61 3929 set refargs [list]
218a900b
AG
3930 foreach arg $view_args {
3931 if {[regexp -- {^-([0-9]+)$} $arg arg cnt]
3932 && ![info exists found(limit)]} {
3933 set newviewopts($n,limit) $cnt
3934 set found(limit) 1
3935 continue
3936 }
3937 catch { unset val }
3938 foreach opt $known_view_options {
3939 set id [lindex $opt 0]
3940 if {[info exists found($id)]} continue
3941 foreach pattern [lindex $opt 3] {
3942 if {![string match $pattern $arg]} continue
13d40b61
EN
3943 if {[lindex $opt 1] eq "b"} {
3944 # Check buttons
3945 set val 1
3946 } elseif {[regexp {^r(\d+)$} [lindex $opt 1] match num]} {
3947 # Radio buttons
3948 regexp {^(.*_)} $id uselessvar id
3949 set val $num
3950 } else {
3951 # Text input fields
218a900b
AG
3952 set size [string length $pattern]
3953 set val [string range $arg [expr {$size-1}] end]
218a900b
AG
3954 }
3955 set newviewopts($n,$id) $val
3956 set found($id) 1
3957 break
3958 }
3959 if {[info exists val]} break
3960 }
3961 if {[info exists val]} continue
13d40b61
EN
3962 if {[regexp {^-} $arg]} {
3963 lappend oargs $arg
3964 } else {
3965 lappend refargs $arg
3966 }
218a900b 3967 }
13d40b61 3968 set newviewopts($n,refs) [shellarglist $refargs]
218a900b
AG
3969 set newviewopts($n,args) [shellarglist $oargs]
3970}
3971
cea07cf8
AG
3972proc edit_or_newview {} {
3973 global curview
3974
3975 if {$curview > 0} {
3976 editview
3977 } else {
3978 newview 0
3979 }
3980}
3981
d16c0812
PM
3982proc editview {} {
3983 global curview
218a900b
AG
3984 global viewname viewperm newviewname newviewopts
3985 global viewargs viewargscmd
d16c0812
PM
3986
3987 set top .gitkvedit-$curview
3988 if {[winfo exists $top]} {
3989 raise $top
3990 return
3991 }
5d11f794 3992 decode_view_opts $curview $viewargs($curview)
218a900b
AG
3993 set newviewname($curview) $viewname($curview)
3994 set newviewopts($curview,perm) $viewperm($curview)
3995 set newviewopts($curview,cmd) $viewargscmd($curview)
b56e0a9a 3996 vieweditor $top $curview "[mc "Gitk: edit view"] $viewname($curview)"
d16c0812
PM
3997}
3998
3999proc vieweditor {top n title} {
218a900b 4000 global newviewname newviewopts viewfiles bgcolor
d93f1713 4001 global known_view_options NS
d16c0812 4002
d93f1713 4003 ttk_toplevel $top
e0a01995 4004 wm title $top [concat $title [mc "-- criteria for selecting revisions"]]
e7d64008 4005 make_transient $top .
218a900b
AG
4006
4007 # View name
d93f1713 4008 ${NS}::frame $top.nfr
eae7d64a 4009 ${NS}::label $top.nl -text [mc "View Name"]
d93f1713 4010 ${NS}::entry $top.name -width 20 -textvariable newviewname($n)
218a900b 4011 pack $top.nfr -in $top -fill x -pady 5 -padx 3
13d40b61
EN
4012 pack $top.nl -in $top.nfr -side left -padx {0 5}
4013 pack $top.name -in $top.nfr -side left -padx {0 25}
218a900b
AG
4014
4015 # View options
4016 set cframe $top.nfr
4017 set cexpand 0
4018 set cnt 0
4019 foreach opt $known_view_options {
4020 set id [lindex $opt 0]
4021 set type [lindex $opt 1]
4022 set flags [lindex $opt 2]
4023 set title [eval [lindex $opt 4]]
4024 set lxpad 0
4025
4026 if {$flags eq "+" || $flags eq "*"} {
4027 set cframe $top.fr$cnt
4028 incr cnt
d93f1713 4029 ${NS}::frame $cframe
218a900b
AG
4030 pack $cframe -in $top -fill x -pady 3 -padx 3
4031 set cexpand [expr {$flags eq "*"}]
13d40b61
EN
4032 } elseif {$flags eq ".." || $flags eq "*."} {
4033 set cframe $top.fr$cnt
4034 incr cnt
eae7d64a 4035 ${NS}::frame $cframe
13d40b61
EN
4036 pack $cframe -in $top -fill x -pady 3 -padx [list 15 3]
4037 set cexpand [expr {$flags eq "*."}]
218a900b
AG
4038 } else {
4039 set lxpad 5
4040 }
4041
13d40b61 4042 if {$type eq "l"} {
eae7d64a 4043 ${NS}::label $cframe.l_$id -text $title
13d40b61
EN
4044 pack $cframe.l_$id -in $cframe -side left -pady [list 3 0] -anchor w
4045 } elseif {$type eq "b"} {
d93f1713 4046 ${NS}::checkbutton $cframe.c_$id -text $title -variable newviewopts($n,$id)
218a900b
AG
4047 pack $cframe.c_$id -in $cframe -side left \
4048 -padx [list $lxpad 0] -expand $cexpand -anchor w
13d40b61
EN
4049 } elseif {[regexp {^r(\d+)$} $type type sz]} {
4050 regexp {^(.*_)} $id uselessvar button_id
eae7d64a 4051 ${NS}::radiobutton $cframe.c_$id -text $title -variable newviewopts($n,$button_id) -value $sz
13d40b61
EN
4052 pack $cframe.c_$id -in $cframe -side left \
4053 -padx [list $lxpad 0] -expand $cexpand -anchor w
218a900b 4054 } elseif {[regexp {^t(\d+)$} $type type sz]} {
d93f1713
PT
4055 ${NS}::label $cframe.l_$id -text $title
4056 ${NS}::entry $cframe.e_$id -width $sz -background $bgcolor \
218a900b
AG
4057 -textvariable newviewopts($n,$id)
4058 pack $cframe.l_$id -in $cframe -side left -padx [list $lxpad 0]
4059 pack $cframe.e_$id -in $cframe -side left -expand 1 -fill x
4060 } elseif {[regexp {^t(\d+)=$} $type type sz]} {
d93f1713
PT
4061 ${NS}::label $cframe.l_$id -text $title
4062 ${NS}::entry $cframe.e_$id -width $sz -background $bgcolor \
218a900b
AG
4063 -textvariable newviewopts($n,$id)
4064 pack $cframe.l_$id -in $cframe -side top -pady [list 3 0] -anchor w
4065 pack $cframe.e_$id -in $cframe -side top -fill x
13d40b61 4066 } elseif {$type eq "path"} {
eae7d64a 4067 ${NS}::label $top.l -text $title
13d40b61 4068 pack $top.l -in $top -side top -pady [list 3 0] -anchor w -padx 3
b9b142ff 4069 text $top.t -width 40 -height 5 -background $bgcolor
13d40b61
EN
4070 if {[info exists viewfiles($n)]} {
4071 foreach f $viewfiles($n) {
4072 $top.t insert end $f
4073 $top.t insert end "\n"
4074 }
4075 $top.t delete {end - 1c} end
4076 $top.t mark set insert 0.0
4077 }
4078 pack $top.t -in $top -side top -pady [list 0 5] -fill both -expand 1 -padx 3
218a900b
AG
4079 }
4080 }
4081
d93f1713
PT
4082 ${NS}::frame $top.buts
4083 ${NS}::button $top.buts.ok -text [mc "OK"] -command [list newviewok $top $n]
4084 ${NS}::button $top.buts.apply -text [mc "Apply (F5)"] -command [list newviewok $top $n 1]
4085 ${NS}::button $top.buts.can -text [mc "Cancel"] -command [list destroy $top]
218a900b
AG
4086 bind $top <Control-Return> [list newviewok $top $n]
4087 bind $top <F5> [list newviewok $top $n 1]
76f15947 4088 bind $top <Escape> [list destroy $top]
218a900b 4089 grid $top.buts.ok $top.buts.apply $top.buts.can
50b44ece
PM
4090 grid columnconfigure $top.buts 0 -weight 1 -uniform a
4091 grid columnconfigure $top.buts 1 -weight 1 -uniform a
218a900b
AG
4092 grid columnconfigure $top.buts 2 -weight 1 -uniform a
4093 pack $top.buts -in $top -side top -fill x
50b44ece
PM
4094 focus $top.t
4095}
4096
908c3585 4097proc doviewmenu {m first cmd op argv} {
da7c24dd
PM
4098 set nmenu [$m index end]
4099 for {set i $first} {$i <= $nmenu} {incr i} {
4100 if {[$m entrycget $i -command] eq $cmd} {
908c3585 4101 eval $m $op $i $argv
da7c24dd 4102 break
d16c0812
PM
4103 }
4104 }
da7c24dd
PM
4105}
4106
4107proc allviewmenus {n op args} {
687c8765 4108 # global viewhlmenu
908c3585 4109
3cd204e5 4110 doviewmenu .bar.view 5 [list showview $n] $op $args
687c8765 4111 # doviewmenu $viewhlmenu 1 [list addvhighlight $n] $op $args
d16c0812
PM
4112}
4113
218a900b 4114proc newviewok {top n {apply 0}} {
da7c24dd 4115 global nextviewnum newviewperm newviewname newishighlight
d16c0812 4116 global viewname viewfiles viewperm selectedview curview
218a900b 4117 global viewargs viewargscmd newviewopts viewhlmenu
50b44ece 4118
098dd8a3 4119 if {[catch {
218a900b 4120 set newargs [encode_view_opts $n]
098dd8a3 4121 } err]} {
84a76f18 4122 error_popup "[mc "Error in commit selection arguments:"] $err" $top
098dd8a3
PM
4123 return
4124 }
50b44ece 4125 set files {}
d16c0812 4126 foreach f [split [$top.t get 0.0 end] "\n"] {
50b44ece
PM
4127 set ft [string trim $f]
4128 if {$ft ne {}} {
4129 lappend files $ft
4130 }
4131 }
d16c0812
PM
4132 if {![info exists viewfiles($n)]} {
4133 # creating a new view
4134 incr nextviewnum
4135 set viewname($n) $newviewname($n)
218a900b 4136 set viewperm($n) $newviewopts($n,perm)
d16c0812 4137 set viewfiles($n) $files
098dd8a3 4138 set viewargs($n) $newargs
218a900b 4139 set viewargscmd($n) $newviewopts($n,cmd)
da7c24dd
PM
4140 addviewmenu $n
4141 if {!$newishighlight} {
7eb3cb9c 4142 run showview $n
da7c24dd 4143 } else {
7eb3cb9c 4144 run addvhighlight $n
da7c24dd 4145 }
d16c0812
PM
4146 } else {
4147 # editing an existing view
218a900b 4148 set viewperm($n) $newviewopts($n,perm)
d16c0812
PM
4149 if {$newviewname($n) ne $viewname($n)} {
4150 set viewname($n) $newviewname($n)
3cd204e5 4151 doviewmenu .bar.view 5 [list showview $n] \
908c3585 4152 entryconf [list -label $viewname($n)]
687c8765
PM
4153 # doviewmenu $viewhlmenu 1 [list addvhighlight $n] \
4154 # entryconf [list -label $viewname($n) -value $viewname($n)]
d16c0812 4155 }
2d480856 4156 if {$files ne $viewfiles($n) || $newargs ne $viewargs($n) || \
218a900b 4157 $newviewopts($n,cmd) ne $viewargscmd($n)} {
d16c0812 4158 set viewfiles($n) $files
098dd8a3 4159 set viewargs($n) $newargs
218a900b 4160 set viewargscmd($n) $newviewopts($n,cmd)
d16c0812 4161 if {$curview == $n} {
7fcc92bf 4162 run reloadcommits
d16c0812
PM
4163 }
4164 }
4165 }
218a900b 4166 if {$apply} return
d16c0812 4167 catch {destroy $top}
50b44ece
PM
4168}
4169
4170proc delview {} {
7fcc92bf 4171 global curview viewperm hlview selectedhlview
50b44ece
PM
4172
4173 if {$curview == 0} return
908c3585 4174 if {[info exists hlview] && $hlview == $curview} {
b007ee20 4175 set selectedhlview [mc "None"]
908c3585
PM
4176 unset hlview
4177 }
da7c24dd 4178 allviewmenus $curview delete
a90a6d24 4179 set viewperm($curview) 0
50b44ece
PM
4180 showview 0
4181}
4182
da7c24dd 4183proc addviewmenu {n} {
908c3585 4184 global viewname viewhlmenu
da7c24dd
PM
4185
4186 .bar.view add radiobutton -label $viewname($n) \
4187 -command [list showview $n] -variable selectedview -value $n
687c8765
PM
4188 #$viewhlmenu add radiobutton -label $viewname($n) \
4189 # -command [list addvhighlight $n] -variable selectedhlview
da7c24dd
PM
4190}
4191
50b44ece 4192proc showview {n} {
3ed31a81 4193 global curview cached_commitrow ordertok
f5f3c2e2 4194 global displayorder parentlist rowidlist rowisopt rowfinal
7fcc92bf
PM
4195 global colormap rowtextx nextcolor canvxmax
4196 global numcommits viewcomplete
50b44ece 4197 global selectedline currentid canv canvy0
4fb0fa19 4198 global treediffs
3e76608d 4199 global pending_select mainheadid
0380081c 4200 global commitidx
3e76608d 4201 global selectedview
97645683 4202 global hlview selectedhlview commitinterest
50b44ece
PM
4203
4204 if {$n == $curview} return
4205 set selid {}
7fcc92bf
PM
4206 set ymax [lindex [$canv cget -scrollregion] 3]
4207 set span [$canv yview]
4208 set ytop [expr {[lindex $span 0] * $ymax}]
4209 set ybot [expr {[lindex $span 1] * $ymax}]
4210 set yscreen [expr {($ybot - $ytop) / 2}]
94b4a69f 4211 if {$selectedline ne {}} {
50b44ece
PM
4212 set selid $currentid
4213 set y [yc $selectedline]
50b44ece
PM
4214 if {$ytop < $y && $y < $ybot} {
4215 set yscreen [expr {$y - $ytop}]
50b44ece 4216 }
e507fd48
PM
4217 } elseif {[info exists pending_select]} {
4218 set selid $pending_select
4219 unset pending_select
50b44ece
PM
4220 }
4221 unselectline
fdedbcfb 4222 normalline
50b44ece
PM
4223 catch {unset treediffs}
4224 clear_display
908c3585
PM
4225 if {[info exists hlview] && $hlview == $n} {
4226 unset hlview
b007ee20 4227 set selectedhlview [mc "None"]
908c3585 4228 }
97645683 4229 catch {unset commitinterest}
7fcc92bf 4230 catch {unset cached_commitrow}
9257d8f7 4231 catch {unset ordertok}
50b44ece
PM
4232
4233 set curview $n
a90a6d24 4234 set selectedview $n
f2d0bbbd
PM
4235 .bar.view entryconf [mca "Edit view..."] -state [expr {$n == 0? "disabled": "normal"}]
4236 .bar.view entryconf [mca "Delete view"] -state [expr {$n == 0? "disabled": "normal"}]
50b44ece 4237
df904497 4238 run refill_reflist
7fcc92bf 4239 if {![info exists viewcomplete($n)]} {
567c34e0 4240 getcommits $selid
50b44ece
PM
4241 return
4242 }
4243
7fcc92bf
PM
4244 set displayorder {}
4245 set parentlist {}
4246 set rowidlist {}
4247 set rowisopt {}
4248 set rowfinal {}
f5f3c2e2 4249 set numcommits $commitidx($n)
22626ef4 4250
50b44ece
PM
4251 catch {unset colormap}
4252 catch {unset rowtextx}
da7c24dd
PM
4253 set nextcolor 0
4254 set canvxmax [$canv cget -width]
50b44ece
PM
4255 set curview $n
4256 set row 0
50b44ece
PM
4257 setcanvscroll
4258 set yf 0
e507fd48 4259 set row {}
7fcc92bf
PM
4260 if {$selid ne {} && [commitinview $selid $n]} {
4261 set row [rowofcommit $selid]
50b44ece
PM
4262 # try to get the selected row in the same position on the screen
4263 set ymax [lindex [$canv cget -scrollregion] 3]
4264 set ytop [expr {[yc $row] - $yscreen}]
4265 if {$ytop < 0} {
4266 set ytop 0
4267 }
4268 set yf [expr {$ytop * 1.0 / $ymax}]
4269 }
4270 allcanvs yview moveto $yf
4271 drawvisible
e507fd48
PM
4272 if {$row ne {}} {
4273 selectline $row 0
3e76608d 4274 } elseif {!$viewcomplete($n)} {
567c34e0 4275 reset_pending_select $selid
e507fd48 4276 } else {
835e62ae
AG
4277 reset_pending_select {}
4278
4279 if {[commitinview $pending_select $curview]} {
4280 selectline [rowofcommit $pending_select] 1
4281 } else {
4282 set row [first_real_row]
4283 if {$row < $numcommits} {
4284 selectline $row 0
4285 }
e507fd48
PM
4286 }
4287 }
7fcc92bf
PM
4288 if {!$viewcomplete($n)} {
4289 if {$numcommits == 0} {
d990cedf 4290 show_status [mc "Reading commits..."]
d16c0812 4291 }
098dd8a3 4292 } elseif {$numcommits == 0} {
d990cedf 4293 show_status [mc "No commits selected"]
2516dae2 4294 }
50b44ece
PM
4295}
4296
908c3585
PM
4297# Stuff relating to the highlighting facility
4298
476ca63d 4299proc ishighlighted {id} {
164ff275 4300 global vhighlights fhighlights nhighlights rhighlights
908c3585 4301
476ca63d
PM
4302 if {[info exists nhighlights($id)] && $nhighlights($id) > 0} {
4303 return $nhighlights($id)
908c3585 4304 }
476ca63d
PM
4305 if {[info exists vhighlights($id)] && $vhighlights($id) > 0} {
4306 return $vhighlights($id)
908c3585 4307 }
476ca63d
PM
4308 if {[info exists fhighlights($id)] && $fhighlights($id) > 0} {
4309 return $fhighlights($id)
908c3585 4310 }
476ca63d
PM
4311 if {[info exists rhighlights($id)] && $rhighlights($id) > 0} {
4312 return $rhighlights($id)
164ff275 4313 }
908c3585
PM
4314 return 0
4315}
4316
28593d3f 4317proc bolden {id font} {
b9fdba7f 4318 global canv linehtag currentid boldids need_redisplay markedid
908c3585 4319
d98d50e2
PM
4320 # need_redisplay = 1 means the display is stale and about to be redrawn
4321 if {$need_redisplay} return
28593d3f
PM
4322 lappend boldids $id
4323 $canv itemconf $linehtag($id) -font $font
4324 if {[info exists currentid] && $id eq $currentid} {
908c3585 4325 $canv delete secsel
28593d3f 4326 set t [eval $canv create rect [$canv bbox $linehtag($id)] \
908c3585
PM
4327 -outline {{}} -tags secsel \
4328 -fill [$canv cget -selectbackground]]
4329 $canv lower $t
4330 }
b9fdba7f
PM
4331 if {[info exists markedid] && $id eq $markedid} {
4332 make_idmark $id
4333 }
908c3585
PM
4334}
4335
28593d3f
PM
4336proc bolden_name {id font} {
4337 global canv2 linentag currentid boldnameids need_redisplay
908c3585 4338
d98d50e2 4339 if {$need_redisplay} return
28593d3f
PM
4340 lappend boldnameids $id
4341 $canv2 itemconf $linentag($id) -font $font
4342 if {[info exists currentid] && $id eq $currentid} {
908c3585 4343 $canv2 delete secsel
28593d3f 4344 set t [eval $canv2 create rect [$canv2 bbox $linentag($id)] \
908c3585
PM
4345 -outline {{}} -tags secsel \
4346 -fill [$canv2 cget -selectbackground]]
4347 $canv2 lower $t
4348 }
4349}
4350
4e7d6779 4351proc unbolden {} {
28593d3f 4352 global boldids
908c3585 4353
4e7d6779 4354 set stillbold {}
28593d3f
PM
4355 foreach id $boldids {
4356 if {![ishighlighted $id]} {
4357 bolden $id mainfont
4e7d6779 4358 } else {
28593d3f 4359 lappend stillbold $id
908c3585
PM
4360 }
4361 }
28593d3f 4362 set boldids $stillbold
908c3585
PM
4363}
4364
4365proc addvhighlight {n} {
476ca63d 4366 global hlview viewcomplete curview vhl_done commitidx
da7c24dd
PM
4367
4368 if {[info exists hlview]} {
908c3585 4369 delvhighlight
da7c24dd
PM
4370 }
4371 set hlview $n
7fcc92bf 4372 if {$n != $curview && ![info exists viewcomplete($n)]} {
da7c24dd 4373 start_rev_list $n
908c3585
PM
4374 }
4375 set vhl_done $commitidx($hlview)
4376 if {$vhl_done > 0} {
4377 drawvisible
da7c24dd
PM
4378 }
4379}
4380
908c3585
PM
4381proc delvhighlight {} {
4382 global hlview vhighlights
da7c24dd
PM
4383
4384 if {![info exists hlview]} return
4385 unset hlview
4e7d6779
PM
4386 catch {unset vhighlights}
4387 unbolden
da7c24dd
PM
4388}
4389
908c3585 4390proc vhighlightmore {} {
7fcc92bf 4391 global hlview vhl_done commitidx vhighlights curview
da7c24dd 4392
da7c24dd 4393 set max $commitidx($hlview)
908c3585
PM
4394 set vr [visiblerows]
4395 set r0 [lindex $vr 0]
4396 set r1 [lindex $vr 1]
4397 for {set i $vhl_done} {$i < $max} {incr i} {
7fcc92bf
PM
4398 set id [commitonrow $i $hlview]
4399 if {[commitinview $id $curview]} {
4400 set row [rowofcommit $id]
908c3585
PM
4401 if {$r0 <= $row && $row <= $r1} {
4402 if {![highlighted $row]} {
28593d3f 4403 bolden $id mainfontbold
da7c24dd 4404 }
476ca63d 4405 set vhighlights($id) 1
da7c24dd
PM
4406 }
4407 }
4408 }
908c3585 4409 set vhl_done $max
ac1276ab 4410 return 0
908c3585
PM
4411}
4412
4413proc askvhighlight {row id} {
7fcc92bf 4414 global hlview vhighlights iddrawn
908c3585 4415
7fcc92bf 4416 if {[commitinview $id $hlview]} {
476ca63d 4417 if {[info exists iddrawn($id)] && ![ishighlighted $id]} {
28593d3f 4418 bolden $id mainfontbold
908c3585 4419 }
476ca63d 4420 set vhighlights($id) 1
908c3585 4421 } else {
476ca63d 4422 set vhighlights($id) 0
908c3585
PM
4423 }
4424}
4425
687c8765 4426proc hfiles_change {} {
908c3585 4427 global highlight_files filehighlight fhighlights fh_serial
8b39e04f 4428 global highlight_paths
908c3585
PM
4429
4430 if {[info exists filehighlight]} {
4431 # delete previous highlights
4432 catch {close $filehighlight}
4433 unset filehighlight
4e7d6779
PM
4434 catch {unset fhighlights}
4435 unbolden
63b79191 4436 unhighlight_filelist
908c3585 4437 }
63b79191 4438 set highlight_paths {}
908c3585
PM
4439 after cancel do_file_hl $fh_serial
4440 incr fh_serial
4441 if {$highlight_files ne {}} {
4442 after 300 do_file_hl $fh_serial
4443 }
4444}
4445
687c8765
PM
4446proc gdttype_change {name ix op} {
4447 global gdttype highlight_files findstring findpattern
4448
bb3edc8b 4449 stopfinding
687c8765 4450 if {$findstring ne {}} {
b007ee20 4451 if {$gdttype eq [mc "containing:"]} {
687c8765
PM
4452 if {$highlight_files ne {}} {
4453 set highlight_files {}
4454 hfiles_change
4455 }
4456 findcom_change
4457 } else {
4458 if {$findpattern ne {}} {
4459 set findpattern {}
4460 findcom_change
4461 }
4462 set highlight_files $findstring
4463 hfiles_change
4464 }
4465 drawvisible
4466 }
4467 # enable/disable findtype/findloc menus too
4468}
4469
4470proc find_change {name ix op} {
4471 global gdttype findstring highlight_files
4472
bb3edc8b 4473 stopfinding
b007ee20 4474 if {$gdttype eq [mc "containing:"]} {
687c8765
PM
4475 findcom_change
4476 } else {
4477 if {$highlight_files ne $findstring} {
4478 set highlight_files $findstring
4479 hfiles_change
4480 }
4481 }
4482 drawvisible
4483}
4484
64b5f146 4485proc findcom_change args {
28593d3f 4486 global nhighlights boldnameids
687c8765
PM
4487 global findpattern findtype findstring gdttype
4488
bb3edc8b 4489 stopfinding
687c8765 4490 # delete previous highlights, if any
28593d3f
PM
4491 foreach id $boldnameids {
4492 bolden_name $id mainfont
687c8765 4493 }
28593d3f 4494 set boldnameids {}
687c8765
PM
4495 catch {unset nhighlights}
4496 unbolden
4497 unmarkmatches
b007ee20 4498 if {$gdttype ne [mc "containing:"] || $findstring eq {}} {
687c8765 4499 set findpattern {}
b007ee20 4500 } elseif {$findtype eq [mc "Regexp"]} {
687c8765
PM
4501 set findpattern $findstring
4502 } else {
4503 set e [string map {"*" "\\*" "?" "\\?" "\[" "\\\[" "\\" "\\\\"} \
4504 $findstring]
4505 set findpattern "*$e*"
4506 }
4507}
4508
63b79191
PM
4509proc makepatterns {l} {
4510 set ret {}
4511 foreach e $l {
4512 set ee [string map {"*" "\\*" "?" "\\?" "\[" "\\\[" "\\" "\\\\"} $e]
4513 if {[string index $ee end] eq "/"} {
4514 lappend ret "$ee*"
4515 } else {
4516 lappend ret $ee
4517 lappend ret "$ee/*"
4518 }
4519 }
4520 return $ret
4521}
4522
908c3585 4523proc do_file_hl {serial} {
4e7d6779 4524 global highlight_files filehighlight highlight_paths gdttype fhl_list
de665fd3 4525 global cdup findtype
908c3585 4526
b007ee20 4527 if {$gdttype eq [mc "touching paths:"]} {
de665fd3
YK
4528 # If "exact" match then convert backslashes to forward slashes.
4529 # Most useful to support Windows-flavoured file paths.
4530 if {$findtype eq [mc "Exact"]} {
4531 set highlight_files [string map {"\\" "/"} $highlight_files]
4532 }
60f7a7dc
PM
4533 if {[catch {set paths [shellsplit $highlight_files]}]} return
4534 set highlight_paths [makepatterns $paths]
4535 highlight_filelist
c332f445
MZ
4536 set relative_paths {}
4537 foreach path $paths {
4538 lappend relative_paths [file join $cdup $path]
4539 }
4540 set gdtargs [concat -- $relative_paths]
b007ee20 4541 } elseif {$gdttype eq [mc "adding/removing string:"]} {
60f7a7dc 4542 set gdtargs [list "-S$highlight_files"]
687c8765
PM
4543 } else {
4544 # must be "containing:", i.e. we're searching commit info
4545 return
60f7a7dc 4546 }
1ce09dd6 4547 set cmd [concat | git diff-tree -r -s --stdin $gdtargs]
908c3585
PM
4548 set filehighlight [open $cmd r+]
4549 fconfigure $filehighlight -blocking 0
7eb3cb9c 4550 filerun $filehighlight readfhighlight
4e7d6779 4551 set fhl_list {}
908c3585
PM
4552 drawvisible
4553 flushhighlights
4554}
4555
4556proc flushhighlights {} {
4e7d6779 4557 global filehighlight fhl_list
908c3585
PM
4558
4559 if {[info exists filehighlight]} {
4e7d6779 4560 lappend fhl_list {}
908c3585
PM
4561 puts $filehighlight ""
4562 flush $filehighlight
4563 }
4564}
4565
4566proc askfilehighlight {row id} {
4e7d6779 4567 global filehighlight fhighlights fhl_list
908c3585 4568
4e7d6779 4569 lappend fhl_list $id
476ca63d 4570 set fhighlights($id) -1
908c3585
PM
4571 puts $filehighlight $id
4572}
4573
4574proc readfhighlight {} {
7fcc92bf 4575 global filehighlight fhighlights curview iddrawn
687c8765 4576 global fhl_list find_dirn
4e7d6779 4577
7eb3cb9c
PM
4578 if {![info exists filehighlight]} {
4579 return 0
4580 }
4581 set nr 0
4582 while {[incr nr] <= 100 && [gets $filehighlight line] >= 0} {
4e7d6779
PM
4583 set line [string trim $line]
4584 set i [lsearch -exact $fhl_list $line]
4585 if {$i < 0} continue
4586 for {set j 0} {$j < $i} {incr j} {
4587 set id [lindex $fhl_list $j]
476ca63d 4588 set fhighlights($id) 0
908c3585 4589 }
4e7d6779
PM
4590 set fhl_list [lrange $fhl_list [expr {$i+1}] end]
4591 if {$line eq {}} continue
7fcc92bf 4592 if {![commitinview $line $curview]} continue
476ca63d 4593 if {[info exists iddrawn($line)] && ![ishighlighted $line]} {
28593d3f 4594 bolden $line mainfontbold
4e7d6779 4595 }
476ca63d 4596 set fhighlights($line) 1
908c3585 4597 }
4e7d6779
PM
4598 if {[eof $filehighlight]} {
4599 # strange...
1ce09dd6 4600 puts "oops, git diff-tree died"
4e7d6779
PM
4601 catch {close $filehighlight}
4602 unset filehighlight
7eb3cb9c 4603 return 0
908c3585 4604 }
687c8765 4605 if {[info exists find_dirn]} {
cca5d946 4606 run findmore
908c3585 4607 }
687c8765 4608 return 1
908c3585
PM
4609}
4610
4fb0fa19 4611proc doesmatch {f} {
687c8765 4612 global findtype findpattern
4fb0fa19 4613
b007ee20 4614 if {$findtype eq [mc "Regexp"]} {
687c8765 4615 return [regexp $findpattern $f]
b007ee20 4616 } elseif {$findtype eq [mc "IgnCase"]} {
4fb0fa19
PM
4617 return [string match -nocase $findpattern $f]
4618 } else {
4619 return [string match $findpattern $f]
4620 }
4621}
4622
60f7a7dc 4623proc askfindhighlight {row id} {
9c311b32 4624 global nhighlights commitinfo iddrawn
4fb0fa19
PM
4625 global findloc
4626 global markingmatches
908c3585
PM
4627
4628 if {![info exists commitinfo($id)]} {
4629 getcommit $id
4630 }
60f7a7dc 4631 set info $commitinfo($id)
908c3585 4632 set isbold 0
b007ee20 4633 set fldtypes [list [mc Headline] [mc Author] [mc Date] [mc Committer] [mc CDate] [mc Comments]]
60f7a7dc 4634 foreach f $info ty $fldtypes {
b007ee20 4635 if {($findloc eq [mc "All fields"] || $findloc eq $ty) &&
4fb0fa19 4636 [doesmatch $f]} {
b007ee20 4637 if {$ty eq [mc "Author"]} {
60f7a7dc 4638 set isbold 2
4fb0fa19 4639 break
60f7a7dc 4640 }
4fb0fa19 4641 set isbold 1
908c3585
PM
4642 }
4643 }
4fb0fa19 4644 if {$isbold && [info exists iddrawn($id)]} {
476ca63d 4645 if {![ishighlighted $id]} {
28593d3f 4646 bolden $id mainfontbold
4fb0fa19 4647 if {$isbold > 1} {
28593d3f 4648 bolden_name $id mainfontbold
4fb0fa19 4649 }
908c3585 4650 }
4fb0fa19 4651 if {$markingmatches} {
005a2f4e 4652 markrowmatches $row $id
908c3585
PM
4653 }
4654 }
476ca63d 4655 set nhighlights($id) $isbold
da7c24dd
PM
4656}
4657
005a2f4e
PM
4658proc markrowmatches {row id} {
4659 global canv canv2 linehtag linentag commitinfo findloc
4fb0fa19 4660
005a2f4e
PM
4661 set headline [lindex $commitinfo($id) 0]
4662 set author [lindex $commitinfo($id) 1]
4fb0fa19
PM
4663 $canv delete match$row
4664 $canv2 delete match$row
b007ee20 4665 if {$findloc eq [mc "All fields"] || $findloc eq [mc "Headline"]} {
005a2f4e
PM
4666 set m [findmatches $headline]
4667 if {$m ne {}} {
28593d3f
PM
4668 markmatches $canv $row $headline $linehtag($id) $m \
4669 [$canv itemcget $linehtag($id) -font] $row
005a2f4e 4670 }
4fb0fa19 4671 }
b007ee20 4672 if {$findloc eq [mc "All fields"] || $findloc eq [mc "Author"]} {
005a2f4e
PM
4673 set m [findmatches $author]
4674 if {$m ne {}} {
28593d3f
PM
4675 markmatches $canv2 $row $author $linentag($id) $m \
4676 [$canv2 itemcget $linentag($id) -font] $row
005a2f4e 4677 }
4fb0fa19
PM
4678 }
4679}
4680
164ff275
PM
4681proc vrel_change {name ix op} {
4682 global highlight_related
4683
4684 rhighlight_none
b007ee20 4685 if {$highlight_related ne [mc "None"]} {
7eb3cb9c 4686 run drawvisible
164ff275
PM
4687 }
4688}
4689
4690# prepare for testing whether commits are descendents or ancestors of a
4691proc rhighlight_sel {a} {
4692 global descendent desc_todo ancestor anc_todo
476ca63d 4693 global highlight_related
164ff275
PM
4694
4695 catch {unset descendent}
4696 set desc_todo [list $a]
4697 catch {unset ancestor}
4698 set anc_todo [list $a]
b007ee20 4699 if {$highlight_related ne [mc "None"]} {
164ff275 4700 rhighlight_none
7eb3cb9c 4701 run drawvisible
164ff275
PM
4702 }
4703}
4704
4705proc rhighlight_none {} {
4706 global rhighlights
4707
4e7d6779
PM
4708 catch {unset rhighlights}
4709 unbolden
164ff275
PM
4710}
4711
4712proc is_descendent {a} {
7fcc92bf 4713 global curview children descendent desc_todo
164ff275
PM
4714
4715 set v $curview
7fcc92bf 4716 set la [rowofcommit $a]
164ff275
PM
4717 set todo $desc_todo
4718 set leftover {}
4719 set done 0
4720 for {set i 0} {$i < [llength $todo]} {incr i} {
4721 set do [lindex $todo $i]
7fcc92bf 4722 if {[rowofcommit $do] < $la} {
164ff275
PM
4723 lappend leftover $do
4724 continue
4725 }
4726 foreach nk $children($v,$do) {
4727 if {![info exists descendent($nk)]} {
4728 set descendent($nk) 1
4729 lappend todo $nk
4730 if {$nk eq $a} {
4731 set done 1
4732 }
4733 }
4734 }
4735 if {$done} {
4736 set desc_todo [concat $leftover [lrange $todo [expr {$i+1}] end]]
4737 return
4738 }
4739 }
4740 set descendent($a) 0
4741 set desc_todo $leftover
4742}
4743
4744proc is_ancestor {a} {
7fcc92bf 4745 global curview parents ancestor anc_todo
164ff275
PM
4746
4747 set v $curview
7fcc92bf 4748 set la [rowofcommit $a]
164ff275
PM
4749 set todo $anc_todo
4750 set leftover {}
4751 set done 0
4752 for {set i 0} {$i < [llength $todo]} {incr i} {
4753 set do [lindex $todo $i]
7fcc92bf 4754 if {![commitinview $do $v] || [rowofcommit $do] > $la} {
164ff275
PM
4755 lappend leftover $do
4756 continue
4757 }
7fcc92bf 4758 foreach np $parents($v,$do) {
164ff275
PM
4759 if {![info exists ancestor($np)]} {
4760 set ancestor($np) 1
4761 lappend todo $np
4762 if {$np eq $a} {
4763 set done 1
4764 }
4765 }
4766 }
4767 if {$done} {
4768 set anc_todo [concat $leftover [lrange $todo [expr {$i+1}] end]]
4769 return
4770 }
4771 }
4772 set ancestor($a) 0
4773 set anc_todo $leftover
4774}
4775
4776proc askrelhighlight {row id} {
9c311b32 4777 global descendent highlight_related iddrawn rhighlights
164ff275
PM
4778 global selectedline ancestor
4779
94b4a69f 4780 if {$selectedline eq {}} return
164ff275 4781 set isbold 0
55e34436
CS
4782 if {$highlight_related eq [mc "Descendant"] ||
4783 $highlight_related eq [mc "Not descendant"]} {
164ff275
PM
4784 if {![info exists descendent($id)]} {
4785 is_descendent $id
4786 }
55e34436 4787 if {$descendent($id) == ($highlight_related eq [mc "Descendant"])} {
164ff275
PM
4788 set isbold 1
4789 }
b007ee20
CS
4790 } elseif {$highlight_related eq [mc "Ancestor"] ||
4791 $highlight_related eq [mc "Not ancestor"]} {
164ff275
PM
4792 if {![info exists ancestor($id)]} {
4793 is_ancestor $id
4794 }
b007ee20 4795 if {$ancestor($id) == ($highlight_related eq [mc "Ancestor"])} {
164ff275
PM
4796 set isbold 1
4797 }
4798 }
4799 if {[info exists iddrawn($id)]} {
476ca63d 4800 if {$isbold && ![ishighlighted $id]} {
28593d3f 4801 bolden $id mainfontbold
164ff275
PM
4802 }
4803 }
476ca63d 4804 set rhighlights($id) $isbold
164ff275
PM
4805}
4806
da7c24dd
PM
4807# Graph layout functions
4808
9f1afe05
PM
4809proc shortids {ids} {
4810 set res {}
4811 foreach id $ids {
4812 if {[llength $id] > 1} {
4813 lappend res [shortids $id]
4814 } elseif {[regexp {^[0-9a-f]{40}$} $id]} {
4815 lappend res [string range $id 0 7]
4816 } else {
4817 lappend res $id
4818 }
4819 }
4820 return $res
4821}
4822
9f1afe05
PM
4823proc ntimes {n o} {
4824 set ret {}
0380081c
PM
4825 set o [list $o]
4826 for {set mask 1} {$mask <= $n} {incr mask $mask} {
4827 if {($n & $mask) != 0} {
4828 set ret [concat $ret $o]
9f1afe05 4829 }
0380081c 4830 set o [concat $o $o]
9f1afe05 4831 }
0380081c 4832 return $ret
9f1afe05
PM
4833}
4834
9257d8f7
PM
4835proc ordertoken {id} {
4836 global ordertok curview varcid varcstart varctok curview parents children
4837 global nullid nullid2
4838
4839 if {[info exists ordertok($id)]} {
4840 return $ordertok($id)
4841 }
4842 set origid $id
4843 set todo {}
4844 while {1} {
4845 if {[info exists varcid($curview,$id)]} {
4846 set a $varcid($curview,$id)
4847 set p [lindex $varcstart($curview) $a]
4848 } else {
4849 set p [lindex $children($curview,$id) 0]
4850 }
4851 if {[info exists ordertok($p)]} {
4852 set tok $ordertok($p)
4853 break
4854 }
c8c9f3d9
PM
4855 set id [first_real_child $curview,$p]
4856 if {$id eq {}} {
9257d8f7 4857 # it's a root
46308ea1 4858 set tok [lindex $varctok($curview) $varcid($curview,$p)]
9257d8f7
PM
4859 break
4860 }
9257d8f7
PM
4861 if {[llength $parents($curview,$id)] == 1} {
4862 lappend todo [list $p {}]
4863 } else {
4864 set j [lsearch -exact $parents($curview,$id) $p]
4865 if {$j < 0} {
4866 puts "oops didn't find [shortids $p] in parents of [shortids $id]"
4867 }
4868 lappend todo [list $p [strrep $j]]
4869 }
4870 }
4871 for {set i [llength $todo]} {[incr i -1] >= 0} {} {
4872 set p [lindex $todo $i 0]
4873 append tok [lindex $todo $i 1]
4874 set ordertok($p) $tok
4875 }
4876 set ordertok($origid) $tok
4877 return $tok
4878}
4879
6e8c8707
PM
4880# Work out where id should go in idlist so that order-token
4881# values increase from left to right
4882proc idcol {idlist id {i 0}} {
9257d8f7 4883 set t [ordertoken $id]
e5b37ac1
PM
4884 if {$i < 0} {
4885 set i 0
4886 }
9257d8f7 4887 if {$i >= [llength $idlist] || $t < [ordertoken [lindex $idlist $i]]} {
6e8c8707
PM
4888 if {$i > [llength $idlist]} {
4889 set i [llength $idlist]
9f1afe05 4890 }
9257d8f7 4891 while {[incr i -1] >= 0 && $t < [ordertoken [lindex $idlist $i]]} {}
6e8c8707
PM
4892 incr i
4893 } else {
9257d8f7 4894 if {$t > [ordertoken [lindex $idlist $i]]} {
6e8c8707 4895 while {[incr i] < [llength $idlist] &&
9257d8f7 4896 $t >= [ordertoken [lindex $idlist $i]]} {}
9f1afe05 4897 }
9f1afe05 4898 }
6e8c8707 4899 return $i
9f1afe05
PM
4900}
4901
4902proc initlayout {} {
7fcc92bf 4903 global rowidlist rowisopt rowfinal displayorder parentlist
da7c24dd 4904 global numcommits canvxmax canv
8f7d0cec 4905 global nextcolor
da7c24dd 4906 global colormap rowtextx
9f1afe05 4907
8f7d0cec
PM
4908 set numcommits 0
4909 set displayorder {}
79b2c75e 4910 set parentlist {}
8f7d0cec 4911 set nextcolor 0
0380081c
PM
4912 set rowidlist {}
4913 set rowisopt {}
f5f3c2e2 4914 set rowfinal {}
be0cd098 4915 set canvxmax [$canv cget -width]
50b44ece
PM
4916 catch {unset colormap}
4917 catch {unset rowtextx}
ac1276ab 4918 setcanvscroll
be0cd098
PM
4919}
4920
4921proc setcanvscroll {} {
4922 global canv canv2 canv3 numcommits linespc canvxmax canvy0
ac1276ab 4923 global lastscrollset lastscrollrows
be0cd098
PM
4924
4925 set ymax [expr {$canvy0 + ($numcommits - 0.5) * $linespc + 2}]
4926 $canv conf -scrollregion [list 0 0 $canvxmax $ymax]
4927 $canv2 conf -scrollregion [list 0 0 0 $ymax]
4928 $canv3 conf -scrollregion [list 0 0 0 $ymax]
ac1276ab
PM
4929 set lastscrollset [clock clicks -milliseconds]
4930 set lastscrollrows $numcommits
9f1afe05
PM
4931}
4932
4933proc visiblerows {} {
4934 global canv numcommits linespc
4935
4936 set ymax [lindex [$canv cget -scrollregion] 3]
4937 if {$ymax eq {} || $ymax == 0} return
4938 set f [$canv yview]
4939 set y0 [expr {int([lindex $f 0] * $ymax)}]
4940 set r0 [expr {int(($y0 - 3) / $linespc) - 1}]
4941 if {$r0 < 0} {
4942 set r0 0
4943 }
4944 set y1 [expr {int([lindex $f 1] * $ymax)}]
4945 set r1 [expr {int(($y1 - 3) / $linespc) + 1}]
4946 if {$r1 >= $numcommits} {
4947 set r1 [expr {$numcommits - 1}]
4948 }
4949 return [list $r0 $r1]
4950}
4951
f5f3c2e2 4952proc layoutmore {} {
38dfe939 4953 global commitidx viewcomplete curview
94b4a69f 4954 global numcommits pending_select curview
d375ef9b 4955 global lastscrollset lastscrollrows
ac1276ab
PM
4956
4957 if {$lastscrollrows < 100 || $viewcomplete($curview) ||
4958 [clock clicks -milliseconds] - $lastscrollset > 500} {
a2c22362
PM
4959 setcanvscroll
4960 }
d94f8cd6 4961 if {[info exists pending_select] &&
7fcc92bf 4962 [commitinview $pending_select $curview]} {
567c34e0 4963 update
7fcc92bf 4964 selectline [rowofcommit $pending_select] 1
d94f8cd6 4965 }
ac1276ab 4966 drawvisible
219ea3a9
PM
4967}
4968
cdc8429c
PM
4969# With path limiting, we mightn't get the actual HEAD commit,
4970# so ask git rev-list what is the first ancestor of HEAD that
4971# touches a file in the path limit.
4972proc get_viewmainhead {view} {
4973 global viewmainheadid vfilelimit viewinstances mainheadid
4974
4975 catch {
4976 set rfd [open [concat | git rev-list -1 $mainheadid \
4977 -- $vfilelimit($view)] r]
4978 set j [reg_instance $rfd]
4979 lappend viewinstances($view) $j
4980 fconfigure $rfd -blocking 0
4981 filerun $rfd [list getviewhead $rfd $j $view]
4982 set viewmainheadid($curview) {}
4983 }
4984}
4985
4986# git rev-list should give us just 1 line to use as viewmainheadid($view)
4987proc getviewhead {fd inst view} {
4988 global viewmainheadid commfd curview viewinstances showlocalchanges
4989
4990 set id {}
4991 if {[gets $fd line] < 0} {
4992 if {![eof $fd]} {
4993 return 1
4994 }
4995 } elseif {[string length $line] == 40 && [string is xdigit $line]} {
4996 set id $line
4997 }
4998 set viewmainheadid($view) $id
4999 close $fd
5000 unset commfd($inst)
5001 set i [lsearch -exact $viewinstances($view) $inst]
5002 if {$i >= 0} {
5003 set viewinstances($view) [lreplace $viewinstances($view) $i $i]
5004 }
5005 if {$showlocalchanges && $id ne {} && $view == $curview} {
5006 doshowlocalchanges
5007 }
5008 return 0
5009}
5010
219ea3a9 5011proc doshowlocalchanges {} {
cdc8429c 5012 global curview viewmainheadid
219ea3a9 5013
cdc8429c
PM
5014 if {$viewmainheadid($curview) eq {}} return
5015 if {[commitinview $viewmainheadid($curview) $curview]} {
219ea3a9 5016 dodiffindex
38dfe939 5017 } else {
cdc8429c 5018 interestedin $viewmainheadid($curview) dodiffindex
219ea3a9
PM
5019 }
5020}
5021
5022proc dohidelocalchanges {} {
7fcc92bf 5023 global nullid nullid2 lserial curview
219ea3a9 5024
7fcc92bf 5025 if {[commitinview $nullid $curview]} {
b8a938cf 5026 removefakerow $nullid
8f489363 5027 }
7fcc92bf 5028 if {[commitinview $nullid2 $curview]} {
b8a938cf 5029 removefakerow $nullid2
219ea3a9
PM
5030 }
5031 incr lserial
5032}
5033
8f489363 5034# spawn off a process to do git diff-index --cached HEAD
219ea3a9 5035proc dodiffindex {} {
cdc8429c 5036 global lserial showlocalchanges vfilelimit curview
74cb884f 5037 global hasworktree
219ea3a9 5038
74cb884f 5039 if {!$showlocalchanges || !$hasworktree} return
219ea3a9 5040 incr lserial
cdc8429c
PM
5041 set cmd "|git diff-index --cached HEAD"
5042 if {$vfilelimit($curview) ne {}} {
5043 set cmd [concat $cmd -- $vfilelimit($curview)]
5044 }
5045 set fd [open $cmd r]
219ea3a9 5046 fconfigure $fd -blocking 0
e439e092
AG
5047 set i [reg_instance $fd]
5048 filerun $fd [list readdiffindex $fd $lserial $i]
219ea3a9
PM
5049}
5050
e439e092 5051proc readdiffindex {fd serial inst} {
cdc8429c
PM
5052 global viewmainheadid nullid nullid2 curview commitinfo commitdata lserial
5053 global vfilelimit
219ea3a9 5054
8f489363 5055 set isdiff 1
219ea3a9 5056 if {[gets $fd line] < 0} {
8f489363
PM
5057 if {![eof $fd]} {
5058 return 1
219ea3a9 5059 }
8f489363 5060 set isdiff 0
219ea3a9
PM
5061 }
5062 # we only need to see one line and we don't really care what it says...
e439e092 5063 stop_instance $inst
219ea3a9 5064
24f7a667
PM
5065 if {$serial != $lserial} {
5066 return 0
8f489363
PM
5067 }
5068
24f7a667 5069 # now see if there are any local changes not checked in to the index
cdc8429c
PM
5070 set cmd "|git diff-files"
5071 if {$vfilelimit($curview) ne {}} {
5072 set cmd [concat $cmd -- $vfilelimit($curview)]
5073 }
5074 set fd [open $cmd r]
24f7a667 5075 fconfigure $fd -blocking 0
e439e092
AG
5076 set i [reg_instance $fd]
5077 filerun $fd [list readdifffiles $fd $serial $i]
24f7a667
PM
5078
5079 if {$isdiff && ![commitinview $nullid2 $curview]} {
8f489363 5080 # add the line for the changes in the index to the graph
d990cedf 5081 set hl [mc "Local changes checked in to index but not committed"]
8f489363
PM
5082 set commitinfo($nullid2) [list $hl {} {} {} {} " $hl\n"]
5083 set commitdata($nullid2) "\n $hl\n"
fc2a256f 5084 if {[commitinview $nullid $curview]} {
b8a938cf 5085 removefakerow $nullid
fc2a256f 5086 }
cdc8429c 5087 insertfakerow $nullid2 $viewmainheadid($curview)
24f7a667 5088 } elseif {!$isdiff && [commitinview $nullid2 $curview]} {
cdc8429c
PM
5089 if {[commitinview $nullid $curview]} {
5090 removefakerow $nullid
5091 }
b8a938cf 5092 removefakerow $nullid2
8f489363
PM
5093 }
5094 return 0
5095}
5096
e439e092 5097proc readdifffiles {fd serial inst} {
cdc8429c 5098 global viewmainheadid nullid nullid2 curview
8f489363
PM
5099 global commitinfo commitdata lserial
5100
5101 set isdiff 1
5102 if {[gets $fd line] < 0} {
5103 if {![eof $fd]} {
5104 return 1
5105 }
5106 set isdiff 0
5107 }
5108 # we only need to see one line and we don't really care what it says...
e439e092 5109 stop_instance $inst
8f489363 5110
24f7a667
PM
5111 if {$serial != $lserial} {
5112 return 0
5113 }
5114
5115 if {$isdiff && ![commitinview $nullid $curview]} {
219ea3a9 5116 # add the line for the local diff to the graph
d990cedf 5117 set hl [mc "Local uncommitted changes, not checked in to index"]
219ea3a9
PM
5118 set commitinfo($nullid) [list $hl {} {} {} {} " $hl\n"]
5119 set commitdata($nullid) "\n $hl\n"
7fcc92bf
PM
5120 if {[commitinview $nullid2 $curview]} {
5121 set p $nullid2
5122 } else {
cdc8429c 5123 set p $viewmainheadid($curview)
7fcc92bf 5124 }
b8a938cf 5125 insertfakerow $nullid $p
24f7a667 5126 } elseif {!$isdiff && [commitinview $nullid $curview]} {
b8a938cf 5127 removefakerow $nullid
219ea3a9
PM
5128 }
5129 return 0
9f1afe05
PM
5130}
5131
8f0bc7e9 5132proc nextuse {id row} {
7fcc92bf 5133 global curview children
9f1afe05 5134
8f0bc7e9
PM
5135 if {[info exists children($curview,$id)]} {
5136 foreach kid $children($curview,$id) {
7fcc92bf 5137 if {![commitinview $kid $curview]} {
0380081c
PM
5138 return -1
5139 }
7fcc92bf
PM
5140 if {[rowofcommit $kid] > $row} {
5141 return [rowofcommit $kid]
9f1afe05 5142 }
9f1afe05 5143 }
8f0bc7e9 5144 }
7fcc92bf
PM
5145 if {[commitinview $id $curview]} {
5146 return [rowofcommit $id]
8f0bc7e9
PM
5147 }
5148 return -1
5149}
5150
f5f3c2e2 5151proc prevuse {id row} {
7fcc92bf 5152 global curview children
f5f3c2e2
PM
5153
5154 set ret -1
5155 if {[info exists children($curview,$id)]} {
5156 foreach kid $children($curview,$id) {
7fcc92bf
PM
5157 if {![commitinview $kid $curview]} break
5158 if {[rowofcommit $kid] < $row} {
5159 set ret [rowofcommit $kid]
7b459a1c 5160 }
7b459a1c 5161 }
f5f3c2e2
PM
5162 }
5163 return $ret
5164}
5165
0380081c
PM
5166proc make_idlist {row} {
5167 global displayorder parentlist uparrowlen downarrowlen mingaplen
9257d8f7 5168 global commitidx curview children
9f1afe05 5169
0380081c
PM
5170 set r [expr {$row - $mingaplen - $downarrowlen - 1}]
5171 if {$r < 0} {
5172 set r 0
8f0bc7e9 5173 }
0380081c
PM
5174 set ra [expr {$row - $downarrowlen}]
5175 if {$ra < 0} {
5176 set ra 0
5177 }
5178 set rb [expr {$row + $uparrowlen}]
5179 if {$rb > $commitidx($curview)} {
5180 set rb $commitidx($curview)
5181 }
7fcc92bf 5182 make_disporder $r [expr {$rb + 1}]
0380081c
PM
5183 set ids {}
5184 for {} {$r < $ra} {incr r} {
5185 set nextid [lindex $displayorder [expr {$r + 1}]]
5186 foreach p [lindex $parentlist $r] {
5187 if {$p eq $nextid} continue
5188 set rn [nextuse $p $r]
5189 if {$rn >= $row &&
5190 $rn <= $r + $downarrowlen + $mingaplen + $uparrowlen} {
9257d8f7 5191 lappend ids [list [ordertoken $p] $p]
9f1afe05 5192 }
9f1afe05 5193 }
0380081c
PM
5194 }
5195 for {} {$r < $row} {incr r} {
5196 set nextid [lindex $displayorder [expr {$r + 1}]]
5197 foreach p [lindex $parentlist $r] {
5198 if {$p eq $nextid} continue
5199 set rn [nextuse $p $r]
5200 if {$rn < 0 || $rn >= $row} {
9257d8f7 5201 lappend ids [list [ordertoken $p] $p]
9f1afe05 5202 }
9f1afe05 5203 }
0380081c
PM
5204 }
5205 set id [lindex $displayorder $row]
9257d8f7 5206 lappend ids [list [ordertoken $id] $id]
0380081c
PM
5207 while {$r < $rb} {
5208 foreach p [lindex $parentlist $r] {
5209 set firstkid [lindex $children($curview,$p) 0]
7fcc92bf 5210 if {[rowofcommit $firstkid] < $row} {
9257d8f7 5211 lappend ids [list [ordertoken $p] $p]
9f1afe05 5212 }
9f1afe05 5213 }
0380081c
PM
5214 incr r
5215 set id [lindex $displayorder $r]
5216 if {$id ne {}} {
5217 set firstkid [lindex $children($curview,$id) 0]
7fcc92bf 5218 if {$firstkid ne {} && [rowofcommit $firstkid] < $row} {
9257d8f7 5219 lappend ids [list [ordertoken $id] $id]
0380081c 5220 }
9f1afe05 5221 }
9f1afe05 5222 }
0380081c
PM
5223 set idlist {}
5224 foreach idx [lsort -unique $ids] {
5225 lappend idlist [lindex $idx 1]
5226 }
5227 return $idlist
9f1afe05
PM
5228}
5229
f5f3c2e2
PM
5230proc rowsequal {a b} {
5231 while {[set i [lsearch -exact $a {}]] >= 0} {
5232 set a [lreplace $a $i $i]
5233 }
5234 while {[set i [lsearch -exact $b {}]] >= 0} {
5235 set b [lreplace $b $i $i]
5236 }
5237 return [expr {$a eq $b}]
9f1afe05
PM
5238}
5239
f5f3c2e2
PM
5240proc makeupline {id row rend col} {
5241 global rowidlist uparrowlen downarrowlen mingaplen
9f1afe05 5242
f5f3c2e2
PM
5243 for {set r $rend} {1} {set r $rstart} {
5244 set rstart [prevuse $id $r]
5245 if {$rstart < 0} return
5246 if {$rstart < $row} break
5247 }
5248 if {$rstart + $uparrowlen + $mingaplen + $downarrowlen < $rend} {
5249 set rstart [expr {$rend - $uparrowlen - 1}]
79b2c75e 5250 }
f5f3c2e2
PM
5251 for {set r $rstart} {[incr r] <= $row} {} {
5252 set idlist [lindex $rowidlist $r]
5253 if {$idlist ne {} && [lsearch -exact $idlist $id] < 0} {
5254 set col [idcol $idlist $id $col]
5255 lset rowidlist $r [linsert $idlist $col $id]
5256 changedrow $r
5257 }
9f1afe05
PM
5258 }
5259}
5260
0380081c 5261proc layoutrows {row endrow} {
f5f3c2e2 5262 global rowidlist rowisopt rowfinal displayorder
0380081c
PM
5263 global uparrowlen downarrowlen maxwidth mingaplen
5264 global children parentlist
7fcc92bf 5265 global commitidx viewcomplete curview
9f1afe05 5266
7fcc92bf 5267 make_disporder [expr {$row - 1}] [expr {$endrow + $uparrowlen}]
0380081c
PM
5268 set idlist {}
5269 if {$row > 0} {
f56782ae
PM
5270 set rm1 [expr {$row - 1}]
5271 foreach id [lindex $rowidlist $rm1] {
0380081c
PM
5272 if {$id ne {}} {
5273 lappend idlist $id
5274 }
5275 }
f56782ae 5276 set final [lindex $rowfinal $rm1]
79b2c75e 5277 }
0380081c
PM
5278 for {} {$row < $endrow} {incr row} {
5279 set rm1 [expr {$row - 1}]
f56782ae 5280 if {$rm1 < 0 || $idlist eq {}} {
0380081c 5281 set idlist [make_idlist $row]
f5f3c2e2 5282 set final 1
0380081c
PM
5283 } else {
5284 set id [lindex $displayorder $rm1]
5285 set col [lsearch -exact $idlist $id]
5286 set idlist [lreplace $idlist $col $col]
5287 foreach p [lindex $parentlist $rm1] {
5288 if {[lsearch -exact $idlist $p] < 0} {
5289 set col [idcol $idlist $p $col]
5290 set idlist [linsert $idlist $col $p]
f5f3c2e2
PM
5291 # if not the first child, we have to insert a line going up
5292 if {$id ne [lindex $children($curview,$p) 0]} {
5293 makeupline $p $rm1 $row $col
5294 }
0380081c
PM
5295 }
5296 }
5297 set id [lindex $displayorder $row]
5298 if {$row > $downarrowlen} {
5299 set termrow [expr {$row - $downarrowlen - 1}]
5300 foreach p [lindex $parentlist $termrow] {
5301 set i [lsearch -exact $idlist $p]
5302 if {$i < 0} continue
5303 set nr [nextuse $p $termrow]
5304 if {$nr < 0 || $nr >= $row + $mingaplen + $uparrowlen} {
5305 set idlist [lreplace $idlist $i $i]
5306 }
5307 }
5308 }
5309 set col [lsearch -exact $idlist $id]
5310 if {$col < 0} {
5311 set col [idcol $idlist $id]
5312 set idlist [linsert $idlist $col $id]
f5f3c2e2
PM
5313 if {$children($curview,$id) ne {}} {
5314 makeupline $id $rm1 $row $col
5315 }
0380081c
PM
5316 }
5317 set r [expr {$row + $uparrowlen - 1}]
5318 if {$r < $commitidx($curview)} {
5319 set x $col
5320 foreach p [lindex $parentlist $r] {
5321 if {[lsearch -exact $idlist $p] >= 0} continue
5322 set fk [lindex $children($curview,$p) 0]
7fcc92bf 5323 if {[rowofcommit $fk] < $row} {
0380081c
PM
5324 set x [idcol $idlist $p $x]
5325 set idlist [linsert $idlist $x $p]
5326 }
5327 }
5328 if {[incr r] < $commitidx($curview)} {
5329 set p [lindex $displayorder $r]
5330 if {[lsearch -exact $idlist $p] < 0} {
5331 set fk [lindex $children($curview,$p) 0]
7fcc92bf 5332 if {$fk ne {} && [rowofcommit $fk] < $row} {
0380081c
PM
5333 set x [idcol $idlist $p $x]
5334 set idlist [linsert $idlist $x $p]
5335 }
5336 }
5337 }
5338 }
5339 }
f5f3c2e2
PM
5340 if {$final && !$viewcomplete($curview) &&
5341 $row + $uparrowlen + $mingaplen + $downarrowlen
5342 >= $commitidx($curview)} {
5343 set final 0
5344 }
0380081c
PM
5345 set l [llength $rowidlist]
5346 if {$row == $l} {
5347 lappend rowidlist $idlist
5348 lappend rowisopt 0
f5f3c2e2 5349 lappend rowfinal $final
0380081c 5350 } elseif {$row < $l} {
f5f3c2e2 5351 if {![rowsequal $idlist [lindex $rowidlist $row]]} {
0380081c
PM
5352 lset rowidlist $row $idlist
5353 changedrow $row
5354 }
f56782ae 5355 lset rowfinal $row $final
0380081c 5356 } else {
f5f3c2e2
PM
5357 set pad [ntimes [expr {$row - $l}] {}]
5358 set rowidlist [concat $rowidlist $pad]
0380081c 5359 lappend rowidlist $idlist
f5f3c2e2
PM
5360 set rowfinal [concat $rowfinal $pad]
5361 lappend rowfinal $final
0380081c
PM
5362 set rowisopt [concat $rowisopt [ntimes [expr {$row - $l + 1}] 0]]
5363 }
9f1afe05 5364 }
0380081c 5365 return $row
9f1afe05
PM
5366}
5367
0380081c
PM
5368proc changedrow {row} {
5369 global displayorder iddrawn rowisopt need_redisplay
9f1afe05 5370
0380081c
PM
5371 set l [llength $rowisopt]
5372 if {$row < $l} {
5373 lset rowisopt $row 0
5374 if {$row + 1 < $l} {
5375 lset rowisopt [expr {$row + 1}] 0
5376 if {$row + 2 < $l} {
5377 lset rowisopt [expr {$row + 2}] 0
5378 }
5379 }
5380 }
5381 set id [lindex $displayorder $row]
5382 if {[info exists iddrawn($id)]} {
5383 set need_redisplay 1
9f1afe05
PM
5384 }
5385}
5386
5387proc insert_pad {row col npad} {
6e8c8707 5388 global rowidlist
9f1afe05
PM
5389
5390 set pad [ntimes $npad {}]
e341c06d
PM
5391 set idlist [lindex $rowidlist $row]
5392 set bef [lrange $idlist 0 [expr {$col - 1}]]
5393 set aft [lrange $idlist $col end]
5394 set i [lsearch -exact $aft {}]
5395 if {$i > 0} {
5396 set aft [lreplace $aft $i $i]
5397 }
5398 lset rowidlist $row [concat $bef $pad $aft]
0380081c 5399 changedrow $row
9f1afe05
PM
5400}
5401
5402proc optimize_rows {row col endrow} {
0380081c 5403 global rowidlist rowisopt displayorder curview children
9f1afe05 5404
6e8c8707
PM
5405 if {$row < 1} {
5406 set row 1
5407 }
0380081c
PM
5408 for {} {$row < $endrow} {incr row; set col 0} {
5409 if {[lindex $rowisopt $row]} continue
9f1afe05 5410 set haspad 0
6e8c8707
PM
5411 set y0 [expr {$row - 1}]
5412 set ym [expr {$row - 2}]
0380081c
PM
5413 set idlist [lindex $rowidlist $row]
5414 set previdlist [lindex $rowidlist $y0]
5415 if {$idlist eq {} || $previdlist eq {}} continue
5416 if {$ym >= 0} {
5417 set pprevidlist [lindex $rowidlist $ym]
5418 if {$pprevidlist eq {}} continue
5419 } else {
5420 set pprevidlist {}
5421 }
6e8c8707
PM
5422 set x0 -1
5423 set xm -1
5424 for {} {$col < [llength $idlist]} {incr col} {
5425 set id [lindex $idlist $col]
5426 if {[lindex $previdlist $col] eq $id} continue
5427 if {$id eq {}} {
9f1afe05
PM
5428 set haspad 1
5429 continue
5430 }
6e8c8707
PM
5431 set x0 [lsearch -exact $previdlist $id]
5432 if {$x0 < 0} continue
5433 set z [expr {$x0 - $col}]
9f1afe05 5434 set isarrow 0
6e8c8707
PM
5435 set z0 {}
5436 if {$ym >= 0} {
5437 set xm [lsearch -exact $pprevidlist $id]
5438 if {$xm >= 0} {
5439 set z0 [expr {$xm - $x0}]
5440 }
5441 }
9f1afe05 5442 if {$z0 eq {}} {
92ed666f
PM
5443 # if row y0 is the first child of $id then it's not an arrow
5444 if {[lindex $children($curview,$id) 0] ne
5445 [lindex $displayorder $y0]} {
9f1afe05
PM
5446 set isarrow 1
5447 }
5448 }
e341c06d
PM
5449 if {!$isarrow && $id ne [lindex $displayorder $row] &&
5450 [lsearch -exact [lindex $rowidlist [expr {$row+1}]] $id] < 0} {
5451 set isarrow 1
5452 }
3fc4279a
PM
5453 # Looking at lines from this row to the previous row,
5454 # make them go straight up if they end in an arrow on
5455 # the previous row; otherwise make them go straight up
5456 # or at 45 degrees.
9f1afe05 5457 if {$z < -1 || ($z < 0 && $isarrow)} {
3fc4279a
PM
5458 # Line currently goes left too much;
5459 # insert pads in the previous row, then optimize it
9f1afe05 5460 set npad [expr {-1 - $z + $isarrow}]
9f1afe05
PM
5461 insert_pad $y0 $x0 $npad
5462 if {$y0 > 0} {
5463 optimize_rows $y0 $x0 $row
5464 }
6e8c8707
PM
5465 set previdlist [lindex $rowidlist $y0]
5466 set x0 [lsearch -exact $previdlist $id]
5467 set z [expr {$x0 - $col}]
5468 if {$z0 ne {}} {
5469 set pprevidlist [lindex $rowidlist $ym]
5470 set xm [lsearch -exact $pprevidlist $id]
5471 set z0 [expr {$xm - $x0}]
5472 }
9f1afe05 5473 } elseif {$z > 1 || ($z > 0 && $isarrow)} {
3fc4279a 5474 # Line currently goes right too much;
6e8c8707 5475 # insert pads in this line
9f1afe05 5476 set npad [expr {$z - 1 + $isarrow}]
e341c06d
PM
5477 insert_pad $row $col $npad
5478 set idlist [lindex $rowidlist $row]
9f1afe05 5479 incr col $npad
6e8c8707 5480 set z [expr {$x0 - $col}]
9f1afe05
PM
5481 set haspad 1
5482 }
6e8c8707 5483 if {$z0 eq {} && !$isarrow && $ym >= 0} {
eb447a12 5484 # this line links to its first child on row $row-2
6e8c8707
PM
5485 set id [lindex $displayorder $ym]
5486 set xc [lsearch -exact $pprevidlist $id]
eb447a12
PM
5487 if {$xc >= 0} {
5488 set z0 [expr {$xc - $x0}]
5489 }
5490 }
3fc4279a 5491 # avoid lines jigging left then immediately right
9f1afe05
PM
5492 if {$z0 ne {} && $z < 0 && $z0 > 0} {
5493 insert_pad $y0 $x0 1
6e8c8707
PM
5494 incr x0
5495 optimize_rows $y0 $x0 $row
5496 set previdlist [lindex $rowidlist $y0]
9f1afe05
PM
5497 }
5498 }
5499 if {!$haspad} {
3fc4279a 5500 # Find the first column that doesn't have a line going right
9f1afe05 5501 for {set col [llength $idlist]} {[incr col -1] >= 0} {} {
6e8c8707
PM
5502 set id [lindex $idlist $col]
5503 if {$id eq {}} break
5504 set x0 [lsearch -exact $previdlist $id]
5505 if {$x0 < 0} {
eb447a12 5506 # check if this is the link to the first child
92ed666f
PM
5507 set kid [lindex $displayorder $y0]
5508 if {[lindex $children($curview,$id) 0] eq $kid} {
eb447a12 5509 # it is, work out offset to child
92ed666f 5510 set x0 [lsearch -exact $previdlist $kid]
eb447a12
PM
5511 }
5512 }
6e8c8707 5513 if {$x0 <= $col} break
9f1afe05 5514 }
3fc4279a 5515 # Insert a pad at that column as long as it has a line and
6e8c8707
PM
5516 # isn't the last column
5517 if {$x0 >= 0 && [incr col] < [llength $idlist]} {
9f1afe05 5518 set idlist [linsert $idlist $col {}]
0380081c
PM
5519 lset rowidlist $row $idlist
5520 changedrow $row
9f1afe05
PM
5521 }
5522 }
9f1afe05
PM
5523 }
5524}
5525
5526proc xc {row col} {
5527 global canvx0 linespc
5528 return [expr {$canvx0 + $col * $linespc}]
5529}
5530
5531proc yc {row} {
5532 global canvy0 linespc
5533 return [expr {$canvy0 + $row * $linespc}]
5534}
5535
c934a8a3
PM
5536proc linewidth {id} {
5537 global thickerline lthickness
5538
5539 set wid $lthickness
5540 if {[info exists thickerline] && $id eq $thickerline} {
5541 set wid [expr {2 * $lthickness}]
5542 }
5543 return $wid
5544}
5545
50b44ece 5546proc rowranges {id} {
7fcc92bf 5547 global curview children uparrowlen downarrowlen
92ed666f 5548 global rowidlist
50b44ece 5549
92ed666f
PM
5550 set kids $children($curview,$id)
5551 if {$kids eq {}} {
5552 return {}
66e46f37 5553 }
92ed666f
PM
5554 set ret {}
5555 lappend kids $id
5556 foreach child $kids {
7fcc92bf
PM
5557 if {![commitinview $child $curview]} break
5558 set row [rowofcommit $child]
92ed666f
PM
5559 if {![info exists prev]} {
5560 lappend ret [expr {$row + 1}]
322a8cc9 5561 } else {
92ed666f 5562 if {$row <= $prevrow} {
7fcc92bf 5563 puts "oops children of [shortids $id] out of order [shortids $child] $row <= [shortids $prev] $prevrow"
92ed666f
PM
5564 }
5565 # see if the line extends the whole way from prevrow to row
5566 if {$row > $prevrow + $uparrowlen + $downarrowlen &&
5567 [lsearch -exact [lindex $rowidlist \
5568 [expr {int(($row + $prevrow) / 2)}]] $id] < 0} {
5569 # it doesn't, see where it ends
5570 set r [expr {$prevrow + $downarrowlen}]
5571 if {[lsearch -exact [lindex $rowidlist $r] $id] < 0} {
5572 while {[incr r -1] > $prevrow &&
5573 [lsearch -exact [lindex $rowidlist $r] $id] < 0} {}
5574 } else {
5575 while {[incr r] <= $row &&
5576 [lsearch -exact [lindex $rowidlist $r] $id] >= 0} {}
5577 incr r -1
5578 }
5579 lappend ret $r
5580 # see where it starts up again
5581 set r [expr {$row - $uparrowlen}]
5582 if {[lsearch -exact [lindex $rowidlist $r] $id] < 0} {
5583 while {[incr r] < $row &&
5584 [lsearch -exact [lindex $rowidlist $r] $id] < 0} {}
5585 } else {
5586 while {[incr r -1] >= $prevrow &&
5587 [lsearch -exact [lindex $rowidlist $r] $id] >= 0} {}
5588 incr r
5589 }
5590 lappend ret $r
5591 }
5592 }
5593 if {$child eq $id} {
5594 lappend ret $row
322a8cc9 5595 }
7fcc92bf 5596 set prev $child
92ed666f 5597 set prevrow $row
9f1afe05 5598 }
92ed666f 5599 return $ret
322a8cc9
PM
5600}
5601
5602proc drawlineseg {id row endrow arrowlow} {
5603 global rowidlist displayorder iddrawn linesegs
e341c06d 5604 global canv colormap linespc curview maxlinelen parentlist
322a8cc9
PM
5605
5606 set cols [list [lsearch -exact [lindex $rowidlist $row] $id]]
5607 set le [expr {$row + 1}]
5608 set arrowhigh 1
9f1afe05 5609 while {1} {
322a8cc9
PM
5610 set c [lsearch -exact [lindex $rowidlist $le] $id]
5611 if {$c < 0} {
5612 incr le -1
5613 break
5614 }
5615 lappend cols $c
5616 set x [lindex $displayorder $le]
5617 if {$x eq $id} {
5618 set arrowhigh 0
5619 break
9f1afe05 5620 }
322a8cc9
PM
5621 if {[info exists iddrawn($x)] || $le == $endrow} {
5622 set c [lsearch -exact [lindex $rowidlist [expr {$le+1}]] $id]
5623 if {$c >= 0} {
5624 lappend cols $c
5625 set arrowhigh 0
5626 }
5627 break
5628 }
5629 incr le
9f1afe05 5630 }
322a8cc9
PM
5631 if {$le <= $row} {
5632 return $row
5633 }
5634
5635 set lines {}
5636 set i 0
5637 set joinhigh 0
5638 if {[info exists linesegs($id)]} {
5639 set lines $linesegs($id)
5640 foreach li $lines {
5641 set r0 [lindex $li 0]
5642 if {$r0 > $row} {
5643 if {$r0 == $le && [lindex $li 1] - $row <= $maxlinelen} {
5644 set joinhigh 1
5645 }
5646 break
5647 }
5648 incr i
5649 }
5650 }
5651 set joinlow 0
5652 if {$i > 0} {
5653 set li [lindex $lines [expr {$i-1}]]
5654 set r1 [lindex $li 1]
5655 if {$r1 == $row && $le - [lindex $li 0] <= $maxlinelen} {
5656 set joinlow 1
5657 }
5658 }
5659
5660 set x [lindex $cols [expr {$le - $row}]]
5661 set xp [lindex $cols [expr {$le - 1 - $row}]]
5662 set dir [expr {$xp - $x}]
5663 if {$joinhigh} {
5664 set ith [lindex $lines $i 2]
5665 set coords [$canv coords $ith]
5666 set ah [$canv itemcget $ith -arrow]
5667 set arrowhigh [expr {$ah eq "first" || $ah eq "both"}]
5668 set x2 [lindex $cols [expr {$le + 1 - $row}]]
5669 if {$x2 ne {} && $x - $x2 == $dir} {
5670 set coords [lrange $coords 0 end-2]
5671 }
5672 } else {
5673 set coords [list [xc $le $x] [yc $le]]
5674 }
5675 if {$joinlow} {
5676 set itl [lindex $lines [expr {$i-1}] 2]
5677 set al [$canv itemcget $itl -arrow]
5678 set arrowlow [expr {$al eq "last" || $al eq "both"}]
e341c06d
PM
5679 } elseif {$arrowlow} {
5680 if {[lsearch -exact [lindex $rowidlist [expr {$row-1}]] $id] >= 0 ||
5681 [lsearch -exact [lindex $parentlist [expr {$row-1}]] $id] >= 0} {
5682 set arrowlow 0
5683 }
322a8cc9
PM
5684 }
5685 set arrow [lindex {none first last both} [expr {$arrowhigh + 2*$arrowlow}]]
5686 for {set y $le} {[incr y -1] > $row} {} {
5687 set x $xp
5688 set xp [lindex $cols [expr {$y - 1 - $row}]]
5689 set ndir [expr {$xp - $x}]
5690 if {$dir != $ndir || $xp < 0} {
5691 lappend coords [xc $y $x] [yc $y]
5692 }
5693 set dir $ndir
5694 }
5695 if {!$joinlow} {
5696 if {$xp < 0} {
5697 # join parent line to first child
5698 set ch [lindex $displayorder $row]
5699 set xc [lsearch -exact [lindex $rowidlist $row] $ch]
5700 if {$xc < 0} {
5701 puts "oops: drawlineseg: child $ch not on row $row"
e341c06d
PM
5702 } elseif {$xc != $x} {
5703 if {($arrowhigh && $le == $row + 1) || $dir == 0} {
5704 set d [expr {int(0.5 * $linespc)}]
5705 set x1 [xc $row $x]
5706 if {$xc < $x} {
5707 set x2 [expr {$x1 - $d}]
5708 } else {
5709 set x2 [expr {$x1 + $d}]
5710 }
5711 set y2 [yc $row]
5712 set y1 [expr {$y2 + $d}]
5713 lappend coords $x1 $y1 $x2 $y2
5714 } elseif {$xc < $x - 1} {
322a8cc9
PM
5715 lappend coords [xc $row [expr {$x-1}]] [yc $row]
5716 } elseif {$xc > $x + 1} {
5717 lappend coords [xc $row [expr {$x+1}]] [yc $row]
5718 }
5719 set x $xc
eb447a12 5720 }
322a8cc9
PM
5721 lappend coords [xc $row $x] [yc $row]
5722 } else {
5723 set xn [xc $row $xp]
5724 set yn [yc $row]
e341c06d 5725 lappend coords $xn $yn
322a8cc9
PM
5726 }
5727 if {!$joinhigh} {
322a8cc9
PM
5728 assigncolor $id
5729 set t [$canv create line $coords -width [linewidth $id] \
5730 -fill $colormap($id) -tags lines.$id -arrow $arrow]
5731 $canv lower $t
5732 bindline $t $id
5733 set lines [linsert $lines $i [list $row $le $t]]
5734 } else {
5735 $canv coords $ith $coords
5736 if {$arrow ne $ah} {
5737 $canv itemconf $ith -arrow $arrow
5738 }
5739 lset lines $i 0 $row
5740 }
5741 } else {
5742 set xo [lsearch -exact [lindex $rowidlist [expr {$row - 1}]] $id]
5743 set ndir [expr {$xo - $xp}]
5744 set clow [$canv coords $itl]
5745 if {$dir == $ndir} {
5746 set clow [lrange $clow 2 end]
5747 }
5748 set coords [concat $coords $clow]
5749 if {!$joinhigh} {
5750 lset lines [expr {$i-1}] 1 $le
322a8cc9
PM
5751 } else {
5752 # coalesce two pieces
5753 $canv delete $ith
5754 set b [lindex $lines [expr {$i-1}] 0]
5755 set e [lindex $lines $i 1]
5756 set lines [lreplace $lines [expr {$i-1}] $i [list $b $e $itl]]
5757 }
5758 $canv coords $itl $coords
5759 if {$arrow ne $al} {
5760 $canv itemconf $itl -arrow $arrow
879e8b1a
PM
5761 }
5762 }
322a8cc9
PM
5763
5764 set linesegs($id) $lines
5765 return $le
9f1afe05
PM
5766}
5767
322a8cc9
PM
5768proc drawparentlinks {id row} {
5769 global rowidlist canv colormap curview parentlist
513a54dc 5770 global idpos linespc
9f1afe05 5771
322a8cc9
PM
5772 set rowids [lindex $rowidlist $row]
5773 set col [lsearch -exact $rowids $id]
5774 if {$col < 0} return
5775 set olds [lindex $parentlist $row]
9f1afe05
PM
5776 set row2 [expr {$row + 1}]
5777 set x [xc $row $col]
5778 set y [yc $row]
5779 set y2 [yc $row2]
e341c06d 5780 set d [expr {int(0.5 * $linespc)}]
513a54dc 5781 set ymid [expr {$y + $d}]
8f7d0cec 5782 set ids [lindex $rowidlist $row2]
9f1afe05
PM
5783 # rmx = right-most X coord used
5784 set rmx 0
9f1afe05 5785 foreach p $olds {
f3408449
PM
5786 set i [lsearch -exact $ids $p]
5787 if {$i < 0} {
5788 puts "oops, parent $p of $id not in list"
5789 continue
5790 }
5791 set x2 [xc $row2 $i]
5792 if {$x2 > $rmx} {
5793 set rmx $x2
5794 }
513a54dc
PM
5795 set j [lsearch -exact $rowids $p]
5796 if {$j < 0} {
eb447a12
PM
5797 # drawlineseg will do this one for us
5798 continue
5799 }
9f1afe05
PM
5800 assigncolor $p
5801 # should handle duplicated parents here...
5802 set coords [list $x $y]
513a54dc
PM
5803 if {$i != $col} {
5804 # if attaching to a vertical segment, draw a smaller
5805 # slant for visual distinctness
5806 if {$i == $j} {
5807 if {$i < $col} {
5808 lappend coords [expr {$x2 + $d}] $y $x2 $ymid
5809 } else {
5810 lappend coords [expr {$x2 - $d}] $y $x2 $ymid
5811 }
5812 } elseif {$i < $col && $i < $j} {
5813 # segment slants towards us already
5814 lappend coords [xc $row $j] $y
5815 } else {
5816 if {$i < $col - 1} {
5817 lappend coords [expr {$x2 + $linespc}] $y
5818 } elseif {$i > $col + 1} {
5819 lappend coords [expr {$x2 - $linespc}] $y
5820 }
5821 lappend coords $x2 $y2
5822 }
5823 } else {
5824 lappend coords $x2 $y2
9f1afe05 5825 }
c934a8a3 5826 set t [$canv create line $coords -width [linewidth $p] \
9f1afe05
PM
5827 -fill $colormap($p) -tags lines.$p]
5828 $canv lower $t
5829 bindline $t $p
5830 }
322a8cc9
PM
5831 if {$rmx > [lindex $idpos($id) 1]} {
5832 lset idpos($id) 1 $rmx
5833 redrawtags $id
5834 }
9f1afe05
PM
5835}
5836
c934a8a3 5837proc drawlines {id} {
322a8cc9 5838 global canv
9f1afe05 5839
322a8cc9 5840 $canv itemconf lines.$id -width [linewidth $id]
9f1afe05
PM
5841}
5842
322a8cc9 5843proc drawcmittext {id row col} {
7fcc92bf
PM
5844 global linespc canv canv2 canv3 fgcolor curview
5845 global cmitlisted commitinfo rowidlist parentlist
9f1afe05 5846 global rowtextx idpos idtags idheads idotherrefs
0380081c 5847 global linehtag linentag linedtag selectedline
b9fdba7f 5848 global canvxmax boldids boldnameids fgcolor markedid
d277e89f 5849 global mainheadid nullid nullid2 circleitem circlecolors ctxbut
9f1afe05 5850
1407ade9 5851 # listed is 0 for boundary, 1 for normal, 2 for negative, 3 for left, 4 for right
7fcc92bf 5852 set listed $cmitlisted($curview,$id)
219ea3a9
PM
5853 if {$id eq $nullid} {
5854 set ofill red
8f489363 5855 } elseif {$id eq $nullid2} {
ef3192b8 5856 set ofill green
c11ff120
PM
5857 } elseif {$id eq $mainheadid} {
5858 set ofill yellow
219ea3a9 5859 } else {
c11ff120 5860 set ofill [lindex $circlecolors $listed]
219ea3a9 5861 }
9f1afe05
PM
5862 set x [xc $row $col]
5863 set y [yc $row]
5864 set orad [expr {$linespc / 3}]
1407ade9 5865 if {$listed <= 2} {
c961b228
PM
5866 set t [$canv create oval [expr {$x - $orad}] [expr {$y - $orad}] \
5867 [expr {$x + $orad - 1}] [expr {$y + $orad - 1}] \
5868 -fill $ofill -outline $fgcolor -width 1 -tags circle]
1407ade9 5869 } elseif {$listed == 3} {
c961b228
PM
5870 # triangle pointing left for left-side commits
5871 set t [$canv create polygon \
5872 [expr {$x - $orad}] $y \
5873 [expr {$x + $orad - 1}] [expr {$y - $orad}] \
5874 [expr {$x + $orad - 1}] [expr {$y + $orad - 1}] \
5875 -fill $ofill -outline $fgcolor -width 1 -tags circle]
5876 } else {
5877 # triangle pointing right for right-side commits
5878 set t [$canv create polygon \
5879 [expr {$x + $orad - 1}] $y \
5880 [expr {$x - $orad}] [expr {$y - $orad}] \
5881 [expr {$x - $orad}] [expr {$y + $orad - 1}] \
5882 -fill $ofill -outline $fgcolor -width 1 -tags circle]
5883 }
c11ff120 5884 set circleitem($row) $t
9f1afe05
PM
5885 $canv raise $t
5886 $canv bind $t <1> {selcanvline {} %x %y}
322a8cc9
PM
5887 set rmx [llength [lindex $rowidlist $row]]
5888 set olds [lindex $parentlist $row]
5889 if {$olds ne {}} {
5890 set nextids [lindex $rowidlist [expr {$row + 1}]]
5891 foreach p $olds {
5892 set i [lsearch -exact $nextids $p]
5893 if {$i > $rmx} {
5894 set rmx $i
5895 }
5896 }
9f1afe05 5897 }
322a8cc9 5898 set xt [xc $row $rmx]
9f1afe05
PM
5899 set rowtextx($row) $xt
5900 set idpos($id) [list $x $xt $y]
5901 if {[info exists idtags($id)] || [info exists idheads($id)]
5902 || [info exists idotherrefs($id)]} {
5903 set xt [drawtags $id $x $xt $y]
5904 }
36242490
RZ
5905 if {[lindex $commitinfo($id) 6] > 0} {
5906 set xt [drawnotesign $xt $y]
5907 }
9f1afe05
PM
5908 set headline [lindex $commitinfo($id) 0]
5909 set name [lindex $commitinfo($id) 1]
5910 set date [lindex $commitinfo($id) 2]
5911 set date [formatdate $date]
9c311b32
PM
5912 set font mainfont
5913 set nfont mainfont
476ca63d 5914 set isbold [ishighlighted $id]
908c3585 5915 if {$isbold > 0} {
28593d3f 5916 lappend boldids $id
9c311b32 5917 set font mainfontbold
908c3585 5918 if {$isbold > 1} {
28593d3f 5919 lappend boldnameids $id
9c311b32 5920 set nfont mainfontbold
908c3585 5921 }
da7c24dd 5922 }
28593d3f
PM
5923 set linehtag($id) [$canv create text $xt $y -anchor w -fill $fgcolor \
5924 -text $headline -font $font -tags text]
5925 $canv bind $linehtag($id) $ctxbut "rowmenu %X %Y $id"
5926 set linentag($id) [$canv2 create text 3 $y -anchor w -fill $fgcolor \
5927 -text $name -font $nfont -tags text]
5928 set linedtag($id) [$canv3 create text 3 $y -anchor w -fill $fgcolor \
5929 -text $date -font mainfont -tags text]
94b4a69f 5930 if {$selectedline == $row} {
28593d3f 5931 make_secsel $id
0380081c 5932 }
b9fdba7f
PM
5933 if {[info exists markedid] && $markedid eq $id} {
5934 make_idmark $id
5935 }
9c311b32 5936 set xr [expr {$xt + [font measure $font $headline]}]
be0cd098
PM
5937 if {$xr > $canvxmax} {
5938 set canvxmax $xr
5939 setcanvscroll
5940 }
9f1afe05
PM
5941}
5942
5943proc drawcmitrow {row} {
0380081c 5944 global displayorder rowidlist nrows_drawn
005a2f4e 5945 global iddrawn markingmatches
7fcc92bf 5946 global commitinfo numcommits
687c8765 5947 global filehighlight fhighlights findpattern nhighlights
908c3585 5948 global hlview vhighlights
164ff275 5949 global highlight_related rhighlights
9f1afe05 5950
8f7d0cec 5951 if {$row >= $numcommits} return
9f1afe05
PM
5952
5953 set id [lindex $displayorder $row]
476ca63d 5954 if {[info exists hlview] && ![info exists vhighlights($id)]} {
908c3585
PM
5955 askvhighlight $row $id
5956 }
476ca63d 5957 if {[info exists filehighlight] && ![info exists fhighlights($id)]} {
908c3585
PM
5958 askfilehighlight $row $id
5959 }
476ca63d 5960 if {$findpattern ne {} && ![info exists nhighlights($id)]} {
60f7a7dc 5961 askfindhighlight $row $id
908c3585 5962 }
476ca63d 5963 if {$highlight_related ne [mc "None"] && ![info exists rhighlights($id)]} {
164ff275
PM
5964 askrelhighlight $row $id
5965 }
005a2f4e
PM
5966 if {![info exists iddrawn($id)]} {
5967 set col [lsearch -exact [lindex $rowidlist $row] $id]
5968 if {$col < 0} {
5969 puts "oops, row $row id $id not in list"
5970 return
5971 }
5972 if {![info exists commitinfo($id)]} {
5973 getcommit $id
5974 }
5975 assigncolor $id
5976 drawcmittext $id $row $col
5977 set iddrawn($id) 1
0380081c 5978 incr nrows_drawn
9f1afe05 5979 }
005a2f4e
PM
5980 if {$markingmatches} {
5981 markrowmatches $row $id
9f1afe05 5982 }
9f1afe05
PM
5983}
5984
322a8cc9 5985proc drawcommits {row {endrow {}}} {
0380081c 5986 global numcommits iddrawn displayorder curview need_redisplay
f5f3c2e2 5987 global parentlist rowidlist rowfinal uparrowlen downarrowlen nrows_drawn
9f1afe05 5988
9f1afe05
PM
5989 if {$row < 0} {
5990 set row 0
5991 }
322a8cc9
PM
5992 if {$endrow eq {}} {
5993 set endrow $row
5994 }
9f1afe05
PM
5995 if {$endrow >= $numcommits} {
5996 set endrow [expr {$numcommits - 1}]
5997 }
322a8cc9 5998
0380081c
PM
5999 set rl1 [expr {$row - $downarrowlen - 3}]
6000 if {$rl1 < 0} {
6001 set rl1 0
6002 }
6003 set ro1 [expr {$row - 3}]
6004 if {$ro1 < 0} {
6005 set ro1 0
6006 }
6007 set r2 [expr {$endrow + $uparrowlen + 3}]
6008 if {$r2 > $numcommits} {
6009 set r2 $numcommits
6010 }
6011 for {set r $rl1} {$r < $r2} {incr r} {
f5f3c2e2 6012 if {[lindex $rowidlist $r] ne {} && [lindex $rowfinal $r]} {
0380081c
PM
6013 if {$rl1 < $r} {
6014 layoutrows $rl1 $r
6015 }
6016 set rl1 [expr {$r + 1}]
6017 }
6018 }
6019 if {$rl1 < $r} {
6020 layoutrows $rl1 $r
6021 }
6022 optimize_rows $ro1 0 $r2
6023 if {$need_redisplay || $nrows_drawn > 2000} {
6024 clear_display
0380081c
PM
6025 }
6026
322a8cc9
PM
6027 # make the lines join to already-drawn rows either side
6028 set r [expr {$row - 1}]
6029 if {$r < 0 || ![info exists iddrawn([lindex $displayorder $r])]} {
6030 set r $row
6031 }
6032 set er [expr {$endrow + 1}]
6033 if {$er >= $numcommits ||
6034 ![info exists iddrawn([lindex $displayorder $er])]} {
6035 set er $endrow
6036 }
6037 for {} {$r <= $er} {incr r} {
6038 set id [lindex $displayorder $r]
6039 set wasdrawn [info exists iddrawn($id)]
4fb0fa19 6040 drawcmitrow $r
322a8cc9
PM
6041 if {$r == $er} break
6042 set nextid [lindex $displayorder [expr {$r + 1}]]
e5ef6f95 6043 if {$wasdrawn && [info exists iddrawn($nextid)]} continue
322a8cc9
PM
6044 drawparentlinks $id $r
6045
322a8cc9
PM
6046 set rowids [lindex $rowidlist $r]
6047 foreach lid $rowids {
6048 if {$lid eq {}} continue
e5ef6f95 6049 if {[info exists lineend($lid)] && $lineend($lid) > $r} continue
322a8cc9
PM
6050 if {$lid eq $id} {
6051 # see if this is the first child of any of its parents
6052 foreach p [lindex $parentlist $r] {
6053 if {[lsearch -exact $rowids $p] < 0} {
6054 # make this line extend up to the child
e5ef6f95 6055 set lineend($p) [drawlineseg $p $r $er 0]
322a8cc9
PM
6056 }
6057 }
e5ef6f95
PM
6058 } else {
6059 set lineend($lid) [drawlineseg $lid $r $er 1]
322a8cc9
PM
6060 }
6061 }
9f1afe05
PM
6062 }
6063}
6064
7fcc92bf
PM
6065proc undolayout {row} {
6066 global uparrowlen mingaplen downarrowlen
6067 global rowidlist rowisopt rowfinal need_redisplay
6068
6069 set r [expr {$row - ($uparrowlen + $mingaplen + $downarrowlen)}]
6070 if {$r < 0} {
6071 set r 0
6072 }
6073 if {[llength $rowidlist] > $r} {
6074 incr r -1
6075 set rowidlist [lrange $rowidlist 0 $r]
6076 set rowfinal [lrange $rowfinal 0 $r]
6077 set rowisopt [lrange $rowisopt 0 $r]
6078 set need_redisplay 1
6079 run drawvisible
6080 }
6081}
6082
31c0eaa8
PM
6083proc drawvisible {} {
6084 global canv linespc curview vrowmod selectedline targetrow targetid
42a671fc 6085 global need_redisplay cscroll numcommits
322a8cc9 6086
31c0eaa8 6087 set fs [$canv yview]
322a8cc9 6088 set ymax [lindex [$canv cget -scrollregion] 3]
5a7f577d 6089 if {$ymax eq {} || $ymax == 0 || $numcommits == 0} return
31c0eaa8
PM
6090 set f0 [lindex $fs 0]
6091 set f1 [lindex $fs 1]
322a8cc9 6092 set y0 [expr {int($f0 * $ymax)}]
322a8cc9 6093 set y1 [expr {int($f1 * $ymax)}]
31c0eaa8
PM
6094
6095 if {[info exists targetid]} {
42a671fc
PM
6096 if {[commitinview $targetid $curview]} {
6097 set r [rowofcommit $targetid]
6098 if {$r != $targetrow} {
6099 # Fix up the scrollregion and change the scrolling position
6100 # now that our target row has moved.
6101 set diff [expr {($r - $targetrow) * $linespc}]
6102 set targetrow $r
6103 setcanvscroll
6104 set ymax [lindex [$canv cget -scrollregion] 3]
6105 incr y0 $diff
6106 incr y1 $diff
6107 set f0 [expr {$y0 / $ymax}]
6108 set f1 [expr {$y1 / $ymax}]
6109 allcanvs yview moveto $f0
6110 $cscroll set $f0 $f1
6111 set need_redisplay 1
6112 }
6113 } else {
6114 unset targetid
31c0eaa8
PM
6115 }
6116 }
6117
6118 set row [expr {int(($y0 - 3) / $linespc) - 1}]
322a8cc9 6119 set endrow [expr {int(($y1 - 3) / $linespc) + 1}]
31c0eaa8
PM
6120 if {$endrow >= $vrowmod($curview)} {
6121 update_arcrows $curview
6122 }
94b4a69f 6123 if {$selectedline ne {} &&
31c0eaa8
PM
6124 $row <= $selectedline && $selectedline <= $endrow} {
6125 set targetrow $selectedline
ac1276ab 6126 } elseif {[info exists targetid]} {
31c0eaa8
PM
6127 set targetrow [expr {int(($row + $endrow) / 2)}]
6128 }
ac1276ab
PM
6129 if {[info exists targetrow]} {
6130 if {$targetrow >= $numcommits} {
6131 set targetrow [expr {$numcommits - 1}]
6132 }
6133 set targetid [commitonrow $targetrow]
42a671fc 6134 }
322a8cc9
PM
6135 drawcommits $row $endrow
6136}
6137
9f1afe05 6138proc clear_display {} {
0380081c 6139 global iddrawn linesegs need_redisplay nrows_drawn
164ff275 6140 global vhighlights fhighlights nhighlights rhighlights
28593d3f 6141 global linehtag linentag linedtag boldids boldnameids
9f1afe05
PM
6142
6143 allcanvs delete all
6144 catch {unset iddrawn}
322a8cc9 6145 catch {unset linesegs}
94503a66
PM
6146 catch {unset linehtag}
6147 catch {unset linentag}
6148 catch {unset linedtag}
28593d3f
PM
6149 set boldids {}
6150 set boldnameids {}
908c3585
PM
6151 catch {unset vhighlights}
6152 catch {unset fhighlights}
6153 catch {unset nhighlights}
164ff275 6154 catch {unset rhighlights}
0380081c
PM
6155 set need_redisplay 0
6156 set nrows_drawn 0
9f1afe05
PM
6157}
6158
50b44ece 6159proc findcrossings {id} {
6e8c8707 6160 global rowidlist parentlist numcommits displayorder
50b44ece
PM
6161
6162 set cross {}
6163 set ccross {}
6164 foreach {s e} [rowranges $id] {
6165 if {$e >= $numcommits} {
6166 set e [expr {$numcommits - 1}]
50b44ece 6167 }
d94f8cd6 6168 if {$e <= $s} continue
50b44ece 6169 for {set row $e} {[incr row -1] >= $s} {} {
6e8c8707
PM
6170 set x [lsearch -exact [lindex $rowidlist $row] $id]
6171 if {$x < 0} break
50b44ece
PM
6172 set olds [lindex $parentlist $row]
6173 set kid [lindex $displayorder $row]
6174 set kidx [lsearch -exact [lindex $rowidlist $row] $kid]
6175 if {$kidx < 0} continue
6176 set nextrow [lindex $rowidlist [expr {$row + 1}]]
6177 foreach p $olds {
6178 set px [lsearch -exact $nextrow $p]
6179 if {$px < 0} continue
6180 if {($kidx < $x && $x < $px) || ($px < $x && $x < $kidx)} {
6181 if {[lsearch -exact $ccross $p] >= 0} continue
6182 if {$x == $px + ($kidx < $px? -1: 1)} {
6183 lappend ccross $p
6184 } elseif {[lsearch -exact $cross $p] < 0} {
6185 lappend cross $p
6186 }
6187 }
6188 }
50b44ece
PM
6189 }
6190 }
6191 return [concat $ccross {{}} $cross]
6192}
6193
e5c2d856 6194proc assigncolor {id} {
aa81d974 6195 global colormap colors nextcolor
7fcc92bf 6196 global parents children children curview
6c20ff34 6197
418c4c7b 6198 if {[info exists colormap($id)]} return
e5c2d856 6199 set ncolors [llength $colors]
da7c24dd
PM
6200 if {[info exists children($curview,$id)]} {
6201 set kids $children($curview,$id)
79b2c75e
PM
6202 } else {
6203 set kids {}
6204 }
6205 if {[llength $kids] == 1} {
6206 set child [lindex $kids 0]
9ccbdfbf 6207 if {[info exists colormap($child)]
7fcc92bf 6208 && [llength $parents($curview,$child)] == 1} {
9ccbdfbf
PM
6209 set colormap($id) $colormap($child)
6210 return
e5c2d856 6211 }
9ccbdfbf
PM
6212 }
6213 set badcolors {}
50b44ece
PM
6214 set origbad {}
6215 foreach x [findcrossings $id] {
6216 if {$x eq {}} {
6217 # delimiter between corner crossings and other crossings
6218 if {[llength $badcolors] >= $ncolors - 1} break
6219 set origbad $badcolors
e5c2d856 6220 }
50b44ece
PM
6221 if {[info exists colormap($x)]
6222 && [lsearch -exact $badcolors $colormap($x)] < 0} {
6223 lappend badcolors $colormap($x)
6c20ff34
PM
6224 }
6225 }
50b44ece
PM
6226 if {[llength $badcolors] >= $ncolors} {
6227 set badcolors $origbad
9ccbdfbf 6228 }
50b44ece 6229 set origbad $badcolors
6c20ff34 6230 if {[llength $badcolors] < $ncolors - 1} {
79b2c75e 6231 foreach child $kids {
6c20ff34
PM
6232 if {[info exists colormap($child)]
6233 && [lsearch -exact $badcolors $colormap($child)] < 0} {
6234 lappend badcolors $colormap($child)
6235 }
7fcc92bf 6236 foreach p $parents($curview,$child) {
79b2c75e
PM
6237 if {[info exists colormap($p)]
6238 && [lsearch -exact $badcolors $colormap($p)] < 0} {
6239 lappend badcolors $colormap($p)
6c20ff34
PM
6240 }
6241 }
6242 }
6243 if {[llength $badcolors] >= $ncolors} {
6244 set badcolors $origbad
6245 }
9ccbdfbf
PM
6246 }
6247 for {set i 0} {$i <= $ncolors} {incr i} {
6248 set c [lindex $colors $nextcolor]
6249 if {[incr nextcolor] >= $ncolors} {
6250 set nextcolor 0
e5c2d856 6251 }
9ccbdfbf 6252 if {[lsearch -exact $badcolors $c]} break
e5c2d856 6253 }
9ccbdfbf 6254 set colormap($id) $c
e5c2d856
PM
6255}
6256
a823a911
PM
6257proc bindline {t id} {
6258 global canv
6259
a823a911
PM
6260 $canv bind $t <Enter> "lineenter %x %y $id"
6261 $canv bind $t <Motion> "linemotion %x %y $id"
6262 $canv bind $t <Leave> "lineleave $id"
fa4da7b3 6263 $canv bind $t <Button-1> "lineclick %x %y $id 1"
a823a911
PM
6264}
6265
bdbfbe3d 6266proc drawtags {id x xt y1} {
8a48571c 6267 global idtags idheads idotherrefs mainhead
bdbfbe3d 6268 global linespc lthickness
d277e89f 6269 global canv rowtextx curview fgcolor bgcolor ctxbut
bdbfbe3d
PM
6270
6271 set marks {}
6272 set ntags 0
f1d83ba3 6273 set nheads 0
bdbfbe3d
PM
6274 if {[info exists idtags($id)]} {
6275 set marks $idtags($id)
6276 set ntags [llength $marks]
6277 }
6278 if {[info exists idheads($id)]} {
6279 set marks [concat $marks $idheads($id)]
f1d83ba3
PM
6280 set nheads [llength $idheads($id)]
6281 }
6282 if {[info exists idotherrefs($id)]} {
6283 set marks [concat $marks $idotherrefs($id)]
bdbfbe3d
PM
6284 }
6285 if {$marks eq {}} {
6286 return $xt
6287 }
6288
6289 set delta [expr {int(0.5 * ($linespc - $lthickness))}]
2ed49d54
JH
6290 set yt [expr {$y1 - 0.5 * $linespc}]
6291 set yb [expr {$yt + $linespc - 1}]
bdbfbe3d
PM
6292 set xvals {}
6293 set wvals {}
8a48571c 6294 set i -1
bdbfbe3d 6295 foreach tag $marks {
8a48571c
PM
6296 incr i
6297 if {$i >= $ntags && $i < $ntags + $nheads && $tag eq $mainhead} {
9c311b32 6298 set wid [font measure mainfontbold $tag]
8a48571c 6299 } else {
9c311b32 6300 set wid [font measure mainfont $tag]
8a48571c 6301 }
bdbfbe3d
PM
6302 lappend xvals $xt
6303 lappend wvals $wid
6304 set xt [expr {$xt + $delta + $wid + $lthickness + $linespc}]
6305 }
6306 set t [$canv create line $x $y1 [lindex $xvals end] $y1 \
6307 -width $lthickness -fill black -tags tag.$id]
6308 $canv lower $t
6309 foreach tag $marks x $xvals wid $wvals {
8dd60f54 6310 set tag_quoted [string map {% %%} $tag]
2ed49d54
JH
6311 set xl [expr {$x + $delta}]
6312 set xr [expr {$x + $delta + $wid + $lthickness}]
9c311b32 6313 set font mainfont
bdbfbe3d
PM
6314 if {[incr ntags -1] >= 0} {
6315 # draw a tag
2ed49d54
JH
6316 set t [$canv create polygon $x [expr {$yt + $delta}] $xl $yt \
6317 $xr $yt $xr $yb $xl $yb $x [expr {$yb - $delta}] \
106288cb 6318 -width 1 -outline black -fill yellow -tags tag.$id]
8dd60f54 6319 $canv bind $t <1> [list showtag $tag_quoted 1]
7fcc92bf 6320 set rowtextx([rowofcommit $id]) [expr {$xr + $linespc}]
bdbfbe3d 6321 } else {
f1d83ba3
PM
6322 # draw a head or other ref
6323 if {[incr nheads -1] >= 0} {
6324 set col green
8a48571c 6325 if {$tag eq $mainhead} {
9c311b32 6326 set font mainfontbold
8a48571c 6327 }
f1d83ba3
PM
6328 } else {
6329 set col "#ddddff"
6330 }
2ed49d54 6331 set xl [expr {$xl - $delta/2}]
bdbfbe3d 6332 $canv create polygon $x $yt $xr $yt $xr $yb $x $yb \
f1d83ba3 6333 -width 1 -outline black -fill $col -tags tag.$id
a970fcf2 6334 if {[regexp {^(remotes/.*/|remotes/)} $tag match remoteprefix]} {
9c311b32 6335 set rwid [font measure mainfont $remoteprefix]
a970fcf2
JW
6336 set xi [expr {$x + 1}]
6337 set yti [expr {$yt + 1}]
6338 set xri [expr {$x + $rwid}]
6339 $canv create polygon $xi $yti $xri $yti $xri $yb $xi $yb \
6340 -width 0 -fill "#ffddaa" -tags tag.$id
6341 }
bdbfbe3d 6342 }
f8a2c0d1 6343 set t [$canv create text $xl $y1 -anchor w -text $tag -fill $fgcolor \
8a48571c 6344 -font $font -tags [list tag.$id text]]
106288cb 6345 if {$ntags >= 0} {
8dd60f54 6346 $canv bind $t <1> [list showtag $tag_quoted 1]
10299152 6347 } elseif {$nheads >= 0} {
8dd60f54 6348 $canv bind $t $ctxbut [list headmenu %X %Y $id $tag_quoted]
106288cb 6349 }
bdbfbe3d
PM
6350 }
6351 return $xt
6352}
6353
36242490
RZ
6354proc drawnotesign {xt y} {
6355 global linespc canv fgcolor
6356
6357 set orad [expr {$linespc / 3}]
6358 set t [$canv create rectangle [expr {$xt - $orad}] [expr {$y - $orad}] \
6359 [expr {$xt + $orad - 1}] [expr {$y + $orad - 1}] \
6360 -fill yellow -outline $fgcolor -width 1 -tags circle]
6361 set xt [expr {$xt + $orad * 3}]
6362 return $xt
6363}
6364
8d858d1a
PM
6365proc xcoord {i level ln} {
6366 global canvx0 xspc1 xspc2
6367
6368 set x [expr {$canvx0 + $i * $xspc1($ln)}]
6369 if {$i > 0 && $i == $level} {
6370 set x [expr {$x + 0.5 * ($xspc2 - $xspc1($ln))}]
6371 } elseif {$i > $level} {
6372 set x [expr {$x + $xspc2 - $xspc1($ln)}]
6373 }
6374 return $x
6375}
9ccbdfbf 6376
098dd8a3 6377proc show_status {msg} {
9c311b32 6378 global canv fgcolor
098dd8a3
PM
6379
6380 clear_display
9c311b32 6381 $canv create text 3 3 -anchor nw -text $msg -font mainfont \
f8a2c0d1 6382 -tags text -fill $fgcolor
098dd8a3
PM
6383}
6384
94a2eede
PM
6385# Don't change the text pane cursor if it is currently the hand cursor,
6386# showing that we are over a sha1 ID link.
6387proc settextcursor {c} {
6388 global ctext curtextcursor
6389
6390 if {[$ctext cget -cursor] == $curtextcursor} {
6391 $ctext config -cursor $c
6392 }
6393 set curtextcursor $c
9ccbdfbf
PM
6394}
6395
a137a90f
PM
6396proc nowbusy {what {name {}}} {
6397 global isbusy busyname statusw
da7c24dd
PM
6398
6399 if {[array names isbusy] eq {}} {
6400 . config -cursor watch
6401 settextcursor watch
6402 }
6403 set isbusy($what) 1
a137a90f
PM
6404 set busyname($what) $name
6405 if {$name ne {}} {
6406 $statusw conf -text $name
6407 }
da7c24dd
PM
6408}
6409
6410proc notbusy {what} {
a137a90f 6411 global isbusy maincursor textcursor busyname statusw
da7c24dd 6412
a137a90f
PM
6413 catch {
6414 unset isbusy($what)
6415 if {$busyname($what) ne {} &&
6416 [$statusw cget -text] eq $busyname($what)} {
6417 $statusw conf -text {}
6418 }
6419 }
da7c24dd
PM
6420 if {[array names isbusy] eq {}} {
6421 . config -cursor $maincursor
6422 settextcursor $textcursor
6423 }
6424}
6425
df3d83b1 6426proc findmatches {f} {
4fb0fa19 6427 global findtype findstring
b007ee20 6428 if {$findtype == [mc "Regexp"]} {
4fb0fa19 6429 set matches [regexp -indices -all -inline $findstring $f]
df3d83b1 6430 } else {
4fb0fa19 6431 set fs $findstring
b007ee20 6432 if {$findtype == [mc "IgnCase"]} {
4fb0fa19
PM
6433 set f [string tolower $f]
6434 set fs [string tolower $fs]
df3d83b1
PM
6435 }
6436 set matches {}
6437 set i 0
4fb0fa19
PM
6438 set l [string length $fs]
6439 while {[set j [string first $fs $f $i]] >= 0} {
6440 lappend matches [list $j [expr {$j+$l-1}]]
6441 set i [expr {$j + $l}]
df3d83b1
PM
6442 }
6443 }
6444 return $matches
6445}
6446
cca5d946 6447proc dofind {{dirn 1} {wrap 1}} {
4fb0fa19 6448 global findstring findstartline findcurline selectedline numcommits
cca5d946 6449 global gdttype filehighlight fh_serial find_dirn findallowwrap
b74fd579 6450
cca5d946
PM
6451 if {[info exists find_dirn]} {
6452 if {$find_dirn == $dirn} return
6453 stopfinding
6454 }
df3d83b1 6455 focus .
4fb0fa19 6456 if {$findstring eq {} || $numcommits == 0} return
94b4a69f 6457 if {$selectedline eq {}} {
cca5d946 6458 set findstartline [lindex [visiblerows] [expr {$dirn < 0}]]
98f350e5 6459 } else {
4fb0fa19 6460 set findstartline $selectedline
98f350e5 6461 }
4fb0fa19 6462 set findcurline $findstartline
b007ee20
CS
6463 nowbusy finding [mc "Searching"]
6464 if {$gdttype ne [mc "containing:"] && ![info exists filehighlight]} {
687c8765
PM
6465 after cancel do_file_hl $fh_serial
6466 do_file_hl $fh_serial
98f350e5 6467 }
cca5d946
PM
6468 set find_dirn $dirn
6469 set findallowwrap $wrap
6470 run findmore
4fb0fa19
PM
6471}
6472
bb3edc8b
PM
6473proc stopfinding {} {
6474 global find_dirn findcurline fprogcoord
4fb0fa19 6475
bb3edc8b
PM
6476 if {[info exists find_dirn]} {
6477 unset find_dirn
6478 unset findcurline
6479 notbusy finding
6480 set fprogcoord 0
6481 adjustprogress
4fb0fa19 6482 }
8a897742 6483 stopblaming
4fb0fa19
PM
6484}
6485
6486proc findmore {} {
687c8765 6487 global commitdata commitinfo numcommits findpattern findloc
7fcc92bf 6488 global findstartline findcurline findallowwrap
bb3edc8b 6489 global find_dirn gdttype fhighlights fprogcoord
cd2bcae7 6490 global curview varcorder vrownum varccommits vrowmod
4fb0fa19 6491
bb3edc8b 6492 if {![info exists find_dirn]} {
4fb0fa19
PM
6493 return 0
6494 }
b007ee20 6495 set fldtypes [list [mc "Headline"] [mc "Author"] [mc "Date"] [mc "Committer"] [mc "CDate"] [mc "Comments"]]
4fb0fa19 6496 set l $findcurline
cca5d946
PM
6497 set moretodo 0
6498 if {$find_dirn > 0} {
6499 incr l
6500 if {$l >= $numcommits} {
6501 set l 0
6502 }
6503 if {$l <= $findstartline} {
6504 set lim [expr {$findstartline + 1}]
6505 } else {
6506 set lim $numcommits
6507 set moretodo $findallowwrap
8ed16484 6508 }
4fb0fa19 6509 } else {
cca5d946
PM
6510 if {$l == 0} {
6511 set l $numcommits
98f350e5 6512 }
cca5d946
PM
6513 incr l -1
6514 if {$l >= $findstartline} {
6515 set lim [expr {$findstartline - 1}]
bb3edc8b 6516 } else {
cca5d946
PM
6517 set lim -1
6518 set moretodo $findallowwrap
bb3edc8b 6519 }
687c8765 6520 }
cca5d946
PM
6521 set n [expr {($lim - $l) * $find_dirn}]
6522 if {$n > 500} {
6523 set n 500
6524 set moretodo 1
4fb0fa19 6525 }
cd2bcae7
PM
6526 if {$l + ($find_dirn > 0? $n: 1) > $vrowmod($curview)} {
6527 update_arcrows $curview
6528 }
687c8765
PM
6529 set found 0
6530 set domore 1
7fcc92bf
PM
6531 set ai [bsearch $vrownum($curview) $l]
6532 set a [lindex $varcorder($curview) $ai]
6533 set arow [lindex $vrownum($curview) $ai]
6534 set ids [lindex $varccommits($curview,$a)]
6535 set arowend [expr {$arow + [llength $ids]}]
b007ee20 6536 if {$gdttype eq [mc "containing:"]} {
cca5d946 6537 for {} {$n > 0} {incr n -1; incr l $find_dirn} {
7fcc92bf
PM
6538 if {$l < $arow || $l >= $arowend} {
6539 incr ai $find_dirn
6540 set a [lindex $varcorder($curview) $ai]
6541 set arow [lindex $vrownum($curview) $ai]
6542 set ids [lindex $varccommits($curview,$a)]
6543 set arowend [expr {$arow + [llength $ids]}]
6544 }
6545 set id [lindex $ids [expr {$l - $arow}]]
cca5d946 6546 # shouldn't happen unless git log doesn't give all the commits...
7fcc92bf
PM
6547 if {![info exists commitdata($id)] ||
6548 ![doesmatch $commitdata($id)]} {
6549 continue
6550 }
687c8765
PM
6551 if {![info exists commitinfo($id)]} {
6552 getcommit $id
6553 }
6554 set info $commitinfo($id)
6555 foreach f $info ty $fldtypes {
b007ee20 6556 if {($findloc eq [mc "All fields"] || $findloc eq $ty) &&
687c8765
PM
6557 [doesmatch $f]} {
6558 set found 1
6559 break
6560 }
6561 }
6562 if {$found} break
4fb0fa19 6563 }
687c8765 6564 } else {
cca5d946 6565 for {} {$n > 0} {incr n -1; incr l $find_dirn} {
7fcc92bf
PM
6566 if {$l < $arow || $l >= $arowend} {
6567 incr ai $find_dirn
6568 set a [lindex $varcorder($curview) $ai]
6569 set arow [lindex $vrownum($curview) $ai]
6570 set ids [lindex $varccommits($curview,$a)]
6571 set arowend [expr {$arow + [llength $ids]}]
6572 }
6573 set id [lindex $ids [expr {$l - $arow}]]
476ca63d
PM
6574 if {![info exists fhighlights($id)]} {
6575 # this sets fhighlights($id) to -1
687c8765 6576 askfilehighlight $l $id
cd2bcae7 6577 }
476ca63d 6578 if {$fhighlights($id) > 0} {
cd2bcae7
PM
6579 set found $domore
6580 break
6581 }
476ca63d 6582 if {$fhighlights($id) < 0} {
687c8765
PM
6583 if {$domore} {
6584 set domore 0
cca5d946 6585 set findcurline [expr {$l - $find_dirn}]
687c8765 6586 }
98f350e5
PM
6587 }
6588 }
6589 }
cca5d946 6590 if {$found || ($domore && !$moretodo)} {
4fb0fa19 6591 unset findcurline
687c8765 6592 unset find_dirn
4fb0fa19 6593 notbusy finding
bb3edc8b
PM
6594 set fprogcoord 0
6595 adjustprogress
6596 if {$found} {
6597 findselectline $l
6598 } else {
6599 bell
6600 }
4fb0fa19 6601 return 0
df3d83b1 6602 }
687c8765
PM
6603 if {!$domore} {
6604 flushhighlights
bb3edc8b 6605 } else {
cca5d946 6606 set findcurline [expr {$l - $find_dirn}]
687c8765 6607 }
cca5d946 6608 set n [expr {($findcurline - $findstartline) * $find_dirn - 1}]
bb3edc8b
PM
6609 if {$n < 0} {
6610 incr n $numcommits
df3d83b1 6611 }
bb3edc8b
PM
6612 set fprogcoord [expr {$n * 1.0 / $numcommits}]
6613 adjustprogress
6614 return $domore
df3d83b1
PM
6615}
6616
6617proc findselectline {l} {
687c8765 6618 global findloc commentend ctext findcurline markingmatches gdttype
005a2f4e 6619
8b39e04f 6620 set markingmatches [expr {$gdttype eq [mc "containing:"]}]
005a2f4e 6621 set findcurline $l
d698206c 6622 selectline $l 1
8b39e04f
PM
6623 if {$markingmatches &&
6624 ($findloc eq [mc "All fields"] || $findloc eq [mc "Comments"])} {
df3d83b1
PM
6625 # highlight the matches in the comments
6626 set f [$ctext get 1.0 $commentend]
6627 set matches [findmatches $f]
6628 foreach match $matches {
6629 set start [lindex $match 0]
2ed49d54 6630 set end [expr {[lindex $match 1] + 1}]
df3d83b1
PM
6631 $ctext tag add found "1.0 + $start c" "1.0 + $end c"
6632 }
98f350e5 6633 }
005a2f4e 6634 drawvisible
98f350e5
PM
6635}
6636
4fb0fa19 6637# mark the bits of a headline or author that match a find string
005a2f4e
PM
6638proc markmatches {canv l str tag matches font row} {
6639 global selectedline
6640
98f350e5
PM
6641 set bbox [$canv bbox $tag]
6642 set x0 [lindex $bbox 0]
6643 set y0 [lindex $bbox 1]
6644 set y1 [lindex $bbox 3]
6645 foreach match $matches {
6646 set start [lindex $match 0]
6647 set end [lindex $match 1]
6648 if {$start > $end} continue
2ed49d54
JH
6649 set xoff [font measure $font [string range $str 0 [expr {$start-1}]]]
6650 set xlen [font measure $font [string range $str 0 [expr {$end}]]]
6651 set t [$canv create rect [expr {$x0+$xoff}] $y0 \
6652 [expr {$x0+$xlen+2}] $y1 \
4fb0fa19 6653 -outline {} -tags [list match$l matches] -fill yellow]
98f350e5 6654 $canv lower $t
94b4a69f 6655 if {$row == $selectedline} {
005a2f4e
PM
6656 $canv raise $t secsel
6657 }
98f350e5
PM
6658 }
6659}
6660
6661proc unmarkmatches {} {
bb3edc8b 6662 global markingmatches
4fb0fa19 6663
98f350e5 6664 allcanvs delete matches
4fb0fa19 6665 set markingmatches 0
bb3edc8b 6666 stopfinding
98f350e5
PM
6667}
6668
c8dfbcf9 6669proc selcanvline {w x y} {
fa4da7b3 6670 global canv canvy0 ctext linespc
9f1afe05 6671 global rowtextx
1db95b00 6672 set ymax [lindex [$canv cget -scrollregion] 3]
cfb4563c 6673 if {$ymax == {}} return
1db95b00
PM
6674 set yfrac [lindex [$canv yview] 0]
6675 set y [expr {$y + $yfrac * $ymax}]
6676 set l [expr {int(($y - $canvy0) / $linespc + 0.5)}]
6677 if {$l < 0} {
6678 set l 0
6679 }
c8dfbcf9 6680 if {$w eq $canv} {
fc2a256f
PM
6681 set xmax [lindex [$canv cget -scrollregion] 2]
6682 set xleft [expr {[lindex [$canv xview] 0] * $xmax}]
6683 if {![info exists rowtextx($l)] || $xleft + $x < $rowtextx($l)} return
c8dfbcf9 6684 }
98f350e5 6685 unmarkmatches
d698206c 6686 selectline $l 1
5ad588de
PM
6687}
6688
b1ba39e7
LT
6689proc commit_descriptor {p} {
6690 global commitinfo
b0934489
PM
6691 if {![info exists commitinfo($p)]} {
6692 getcommit $p
6693 }
b1ba39e7 6694 set l "..."
b0934489 6695 if {[llength $commitinfo($p)] > 1} {
b1ba39e7
LT
6696 set l [lindex $commitinfo($p) 0]
6697 }
b8ab2e17 6698 return "$p ($l)\n"
b1ba39e7
LT
6699}
6700
106288cb
PM
6701# append some text to the ctext widget, and make any SHA1 ID
6702# that we know about be a clickable link.
f1b86294 6703proc appendwithlinks {text tags} {
d375ef9b 6704 global ctext linknum curview
106288cb
PM
6705
6706 set start [$ctext index "end - 1c"]
f1b86294 6707 $ctext insert end $text $tags
d375ef9b 6708 set links [regexp -indices -all -inline {\m[0-9a-f]{6,40}\M} $text]
106288cb
PM
6709 foreach l $links {
6710 set s [lindex $l 0]
6711 set e [lindex $l 1]
6712 set linkid [string range $text $s $e]
106288cb 6713 incr e
c73adce2 6714 $ctext tag delete link$linknum
106288cb 6715 $ctext tag add link$linknum "$start + $s c" "$start + $e c"
97645683 6716 setlink $linkid link$linknum
106288cb
PM
6717 incr linknum
6718 }
97645683
PM
6719}
6720
6721proc setlink {id lk} {
d375ef9b 6722 global curview ctext pendinglinks
97645683 6723
d375ef9b
PM
6724 set known 0
6725 if {[string length $id] < 40} {
6726 set matches [longid $id]
6727 if {[llength $matches] > 0} {
6728 if {[llength $matches] > 1} return
6729 set known 1
6730 set id [lindex $matches 0]
6731 }
6732 } else {
6733 set known [commitinview $id $curview]
6734 }
6735 if {$known} {
97645683 6736 $ctext tag conf $lk -foreground blue -underline 1
d375ef9b 6737 $ctext tag bind $lk <1> [list selbyid $id]
97645683
PM
6738 $ctext tag bind $lk <Enter> {linkcursor %W 1}
6739 $ctext tag bind $lk <Leave> {linkcursor %W -1}
6740 } else {
6741 lappend pendinglinks($id) $lk
d375ef9b 6742 interestedin $id {makelink %P}
97645683
PM
6743 }
6744}
6745
6f63fc18
PM
6746proc appendshortlink {id {pre {}} {post {}}} {
6747 global ctext linknum
6748
6749 $ctext insert end $pre
6750 $ctext tag delete link$linknum
6751 $ctext insert end [string range $id 0 7] link$linknum
6752 $ctext insert end $post
6753 setlink $id link$linknum
6754 incr linknum
6755}
6756
97645683
PM
6757proc makelink {id} {
6758 global pendinglinks
6759
6760 if {![info exists pendinglinks($id)]} return
6761 foreach lk $pendinglinks($id) {
6762 setlink $id $lk
6763 }
6764 unset pendinglinks($id)
6765}
6766
6767proc linkcursor {w inc} {
6768 global linkentercount curtextcursor
6769
6770 if {[incr linkentercount $inc] > 0} {
6771 $w configure -cursor hand2
6772 } else {
6773 $w configure -cursor $curtextcursor
6774 if {$linkentercount < 0} {
6775 set linkentercount 0
6776 }
6777 }
106288cb
PM
6778}
6779
6e5f7203
RN
6780proc viewnextline {dir} {
6781 global canv linespc
6782
6783 $canv delete hover
6784 set ymax [lindex [$canv cget -scrollregion] 3]
6785 set wnow [$canv yview]
6786 set wtop [expr {[lindex $wnow 0] * $ymax}]
6787 set newtop [expr {$wtop + $dir * $linespc}]
6788 if {$newtop < 0} {
6789 set newtop 0
6790 } elseif {$newtop > $ymax} {
6791 set newtop $ymax
6792 }
6793 allcanvs yview moveto [expr {$newtop * 1.0 / $ymax}]
6794}
6795
ef030b85
PM
6796# add a list of tag or branch names at position pos
6797# returns the number of names inserted
e11f1233 6798proc appendrefs {pos ids var} {
7fcc92bf 6799 global ctext linknum curview $var maxrefs
b8ab2e17 6800
ef030b85
PM
6801 if {[catch {$ctext index $pos}]} {
6802 return 0
6803 }
e11f1233
PM
6804 $ctext conf -state normal
6805 $ctext delete $pos "$pos lineend"
6806 set tags {}
6807 foreach id $ids {
6808 foreach tag [set $var\($id\)] {
6809 lappend tags [list $tag $id]
6810 }
6811 }
0a4dd8b8 6812 if {[llength $tags] > $maxrefs} {
84b4b832 6813 $ctext insert $pos "[mc "many"] ([llength $tags])"
0a4dd8b8
PM
6814 } else {
6815 set tags [lsort -index 0 -decreasing $tags]
6816 set sep {}
6817 foreach ti $tags {
6818 set id [lindex $ti 1]
6819 set lk link$linknum
6820 incr linknum
6821 $ctext tag delete $lk
6822 $ctext insert $pos $sep
6823 $ctext insert $pos [lindex $ti 0] $lk
97645683 6824 setlink $id $lk
0a4dd8b8 6825 set sep ", "
b8ab2e17 6826 }
b8ab2e17 6827 }
e11f1233 6828 $ctext conf -state disabled
ef030b85 6829 return [llength $tags]
b8ab2e17
PM
6830}
6831
e11f1233
PM
6832# called when we have finished computing the nearby tags
6833proc dispneartags {delay} {
6834 global selectedline currentid showneartags tagphase
ca6d8f58 6835
94b4a69f 6836 if {$selectedline eq {} || !$showneartags} return
e11f1233
PM
6837 after cancel dispnexttag
6838 if {$delay} {
6839 after 200 dispnexttag
6840 set tagphase -1
6841 } else {
6842 after idle dispnexttag
6843 set tagphase 0
ca6d8f58 6844 }
ca6d8f58
PM
6845}
6846
e11f1233
PM
6847proc dispnexttag {} {
6848 global selectedline currentid showneartags tagphase ctext
b8ab2e17 6849
94b4a69f 6850 if {$selectedline eq {} || !$showneartags} return
e11f1233
PM
6851 switch -- $tagphase {
6852 0 {
6853 set dtags [desctags $currentid]
6854 if {$dtags ne {}} {
6855 appendrefs precedes $dtags idtags
6856 }
6857 }
6858 1 {
6859 set atags [anctags $currentid]
6860 if {$atags ne {}} {
6861 appendrefs follows $atags idtags
6862 }
6863 }
6864 2 {
6865 set dheads [descheads $currentid]
6866 if {$dheads ne {}} {
6867 if {[appendrefs branch $dheads idheads] > 1
6868 && [$ctext get "branch -3c"] eq "h"} {
6869 # turn "Branch" into "Branches"
6870 $ctext conf -state normal
6871 $ctext insert "branch -2c" "es"
6872 $ctext conf -state disabled
6873 }
6874 }
ef030b85
PM
6875 }
6876 }
e11f1233
PM
6877 if {[incr tagphase] <= 2} {
6878 after idle dispnexttag
b8ab2e17 6879 }
b8ab2e17
PM
6880}
6881
28593d3f 6882proc make_secsel {id} {
0380081c
PM
6883 global linehtag linentag linedtag canv canv2 canv3
6884
28593d3f 6885 if {![info exists linehtag($id)]} return
0380081c 6886 $canv delete secsel
28593d3f 6887 set t [eval $canv create rect [$canv bbox $linehtag($id)] -outline {{}} \
0380081c
PM
6888 -tags secsel -fill [$canv cget -selectbackground]]
6889 $canv lower $t
6890 $canv2 delete secsel
28593d3f 6891 set t [eval $canv2 create rect [$canv2 bbox $linentag($id)] -outline {{}} \
0380081c
PM
6892 -tags secsel -fill [$canv2 cget -selectbackground]]
6893 $canv2 lower $t
6894 $canv3 delete secsel
28593d3f 6895 set t [eval $canv3 create rect [$canv3 bbox $linedtag($id)] -outline {{}} \
0380081c
PM
6896 -tags secsel -fill [$canv3 cget -selectbackground]]
6897 $canv3 lower $t
6898}
6899
b9fdba7f
PM
6900proc make_idmark {id} {
6901 global linehtag canv fgcolor
6902
6903 if {![info exists linehtag($id)]} return
6904 $canv delete markid
6905 set t [eval $canv create rect [$canv bbox $linehtag($id)] \
6906 -tags markid -outline $fgcolor]
6907 $canv raise $t
6908}
6909
8a897742 6910proc selectline {l isnew {desired_loc {}}} {
0380081c 6911 global canv ctext commitinfo selectedline
7fcc92bf 6912 global canvy0 linespc parents children curview
7fcceed7 6913 global currentid sha1entry
9f1afe05 6914 global commentend idtags linknum
d94f8cd6 6915 global mergemax numcommits pending_select
e11f1233 6916 global cmitmode showneartags allcommits
c30acc77 6917 global targetrow targetid lastscrollrows
21ac8a8d 6918 global autoselect autosellen jump_to_here
d698206c 6919
d94f8cd6 6920 catch {unset pending_select}
84ba7345 6921 $canv delete hover
9843c307 6922 normalline
887c996e 6923 unsel_reflist
bb3edc8b 6924 stopfinding
8f7d0cec 6925 if {$l < 0 || $l >= $numcommits} return
ac1276ab
PM
6926 set id [commitonrow $l]
6927 set targetid $id
6928 set targetrow $l
c30acc77
PM
6929 set selectedline $l
6930 set currentid $id
6931 if {$lastscrollrows < $numcommits} {
6932 setcanvscroll
6933 }
ac1276ab 6934
5ad588de 6935 set y [expr {$canvy0 + $l * $linespc}]
17386066 6936 set ymax [lindex [$canv cget -scrollregion] 3]
5842215e
PM
6937 set ytop [expr {$y - $linespc - 1}]
6938 set ybot [expr {$y + $linespc + 1}]
5ad588de 6939 set wnow [$canv yview]
2ed49d54
JH
6940 set wtop [expr {[lindex $wnow 0] * $ymax}]
6941 set wbot [expr {[lindex $wnow 1] * $ymax}]
5842215e
PM
6942 set wh [expr {$wbot - $wtop}]
6943 set newtop $wtop
17386066 6944 if {$ytop < $wtop} {
5842215e
PM
6945 if {$ybot < $wtop} {
6946 set newtop [expr {$y - $wh / 2.0}]
6947 } else {
6948 set newtop $ytop
6949 if {$newtop > $wtop - $linespc} {
6950 set newtop [expr {$wtop - $linespc}]
6951 }
17386066 6952 }
5842215e
PM
6953 } elseif {$ybot > $wbot} {
6954 if {$ytop > $wbot} {
6955 set newtop [expr {$y - $wh / 2.0}]
6956 } else {
6957 set newtop [expr {$ybot - $wh}]
6958 if {$newtop < $wtop + $linespc} {
6959 set newtop [expr {$wtop + $linespc}]
6960 }
17386066 6961 }
5842215e
PM
6962 }
6963 if {$newtop != $wtop} {
6964 if {$newtop < 0} {
6965 set newtop 0
6966 }
2ed49d54 6967 allcanvs yview moveto [expr {$newtop * 1.0 / $ymax}]
9f1afe05 6968 drawvisible
5ad588de 6969 }
d698206c 6970
28593d3f 6971 make_secsel $id
9f1afe05 6972
fa4da7b3 6973 if {$isnew} {
354af6bd 6974 addtohistory [list selbyid $id 0] savecmitpos
d698206c
PM
6975 }
6976
98f350e5
PM
6977 $sha1entry delete 0 end
6978 $sha1entry insert 0 $id
95293b58 6979 if {$autoselect} {
21ac8a8d 6980 $sha1entry selection range 0 $autosellen
95293b58 6981 }
164ff275 6982 rhighlight_sel $id
98f350e5 6983
5ad588de 6984 $ctext conf -state normal
3ea06f9f 6985 clear_ctext
106288cb 6986 set linknum 0
d76afb15
PM
6987 if {![info exists commitinfo($id)]} {
6988 getcommit $id
6989 }
1db95b00 6990 set info $commitinfo($id)
232475d3 6991 set date [formatdate [lindex $info 2]]
d990cedf 6992 $ctext insert end "[mc "Author"]: [lindex $info 1] $date\n"
232475d3 6993 set date [formatdate [lindex $info 4]]
d990cedf 6994 $ctext insert end "[mc "Committer"]: [lindex $info 3] $date\n"
887fe3c4 6995 if {[info exists idtags($id)]} {
d990cedf 6996 $ctext insert end [mc "Tags:"]
887fe3c4
PM
6997 foreach tag $idtags($id) {
6998 $ctext insert end " $tag"
6999 }
7000 $ctext insert end "\n"
7001 }
40b87ff8 7002
f1b86294 7003 set headers {}
7fcc92bf 7004 set olds $parents($curview,$id)
79b2c75e 7005 if {[llength $olds] > 1} {
b77b0278 7006 set np 0
79b2c75e 7007 foreach p $olds {
b77b0278
PM
7008 if {$np >= $mergemax} {
7009 set tag mmax
7010 } else {
7011 set tag m$np
7012 }
d990cedf 7013 $ctext insert end "[mc "Parent"]: " $tag
f1b86294 7014 appendwithlinks [commit_descriptor $p] {}
b77b0278
PM
7015 incr np
7016 }
7017 } else {
79b2c75e 7018 foreach p $olds {
d990cedf 7019 append headers "[mc "Parent"]: [commit_descriptor $p]"
b1ba39e7
LT
7020 }
7021 }
b77b0278 7022
6a90bff1 7023 foreach c $children($curview,$id) {
d990cedf 7024 append headers "[mc "Child"]: [commit_descriptor $c]"
8b192809 7025 }
d698206c
PM
7026
7027 # make anything that looks like a SHA1 ID be a clickable link
f1b86294 7028 appendwithlinks $headers {}
b8ab2e17
PM
7029 if {$showneartags} {
7030 if {![info exists allcommits]} {
7031 getallcommits
7032 }
d990cedf 7033 $ctext insert end "[mc "Branch"]: "
ef030b85
PM
7034 $ctext mark set branch "end -1c"
7035 $ctext mark gravity branch left
d990cedf 7036 $ctext insert end "\n[mc "Follows"]: "
b8ab2e17
PM
7037 $ctext mark set follows "end -1c"
7038 $ctext mark gravity follows left
d990cedf 7039 $ctext insert end "\n[mc "Precedes"]: "
b8ab2e17
PM
7040 $ctext mark set precedes "end -1c"
7041 $ctext mark gravity precedes left
b8ab2e17 7042 $ctext insert end "\n"
e11f1233 7043 dispneartags 1
b8ab2e17
PM
7044 }
7045 $ctext insert end "\n"
43c25074
PM
7046 set comment [lindex $info 5]
7047 if {[string first "\r" $comment] >= 0} {
7048 set comment [string map {"\r" "\n "} $comment]
7049 }
7050 appendwithlinks $comment {comment}
d698206c 7051
df3d83b1 7052 $ctext tag remove found 1.0 end
5ad588de 7053 $ctext conf -state disabled
df3d83b1 7054 set commentend [$ctext index "end - 1c"]
5ad588de 7055
8a897742 7056 set jump_to_here $desired_loc
b007ee20 7057 init_flist [mc "Comments"]
f8b28a40
PM
7058 if {$cmitmode eq "tree"} {
7059 gettree $id
7060 } elseif {[llength $olds] <= 1} {
d327244a 7061 startdiff $id
7b5ff7e7 7062 } else {
7fcc92bf 7063 mergediff $id
3c461ffe
PM
7064 }
7065}
7066
6e5f7203
RN
7067proc selfirstline {} {
7068 unmarkmatches
7069 selectline 0 1
7070}
7071
7072proc sellastline {} {
7073 global numcommits
7074 unmarkmatches
7075 set l [expr {$numcommits - 1}]
7076 selectline $l 1
7077}
7078
3c461ffe
PM
7079proc selnextline {dir} {
7080 global selectedline
bd441de4 7081 focus .
94b4a69f 7082 if {$selectedline eq {}} return
2ed49d54 7083 set l [expr {$selectedline + $dir}]
3c461ffe 7084 unmarkmatches
d698206c
PM
7085 selectline $l 1
7086}
7087
6e5f7203
RN
7088proc selnextpage {dir} {
7089 global canv linespc selectedline numcommits
7090
7091 set lpp [expr {([winfo height $canv] - 2) / $linespc}]
7092 if {$lpp < 1} {
7093 set lpp 1
7094 }
7095 allcanvs yview scroll [expr {$dir * $lpp}] units
e72ee5eb 7096 drawvisible
94b4a69f 7097 if {$selectedline eq {}} return
6e5f7203
RN
7098 set l [expr {$selectedline + $dir * $lpp}]
7099 if {$l < 0} {
7100 set l 0
7101 } elseif {$l >= $numcommits} {
7102 set l [expr $numcommits - 1]
7103 }
7104 unmarkmatches
40b87ff8 7105 selectline $l 1
6e5f7203
RN
7106}
7107
fa4da7b3 7108proc unselectline {} {
50b44ece 7109 global selectedline currentid
fa4da7b3 7110
94b4a69f 7111 set selectedline {}
50b44ece 7112 catch {unset currentid}
fa4da7b3 7113 allcanvs delete secsel
164ff275 7114 rhighlight_none
fa4da7b3
PM
7115}
7116
f8b28a40
PM
7117proc reselectline {} {
7118 global selectedline
7119
94b4a69f 7120 if {$selectedline ne {}} {
f8b28a40
PM
7121 selectline $selectedline 0
7122 }
7123}
7124
354af6bd 7125proc addtohistory {cmd {saveproc {}}} {
2516dae2 7126 global history historyindex curview
fa4da7b3 7127
354af6bd
PM
7128 unset_posvars
7129 save_position
7130 set elt [list $curview $cmd $saveproc {}]
fa4da7b3 7131 if {$historyindex > 0
2516dae2 7132 && [lindex $history [expr {$historyindex - 1}]] == $elt} {
fa4da7b3
PM
7133 return
7134 }
7135
7136 if {$historyindex < [llength $history]} {
2516dae2 7137 set history [lreplace $history $historyindex end $elt]
fa4da7b3 7138 } else {
2516dae2 7139 lappend history $elt
fa4da7b3
PM
7140 }
7141 incr historyindex
7142 if {$historyindex > 1} {
e9937d2a 7143 .tf.bar.leftbut conf -state normal
fa4da7b3 7144 } else {
e9937d2a 7145 .tf.bar.leftbut conf -state disabled
fa4da7b3 7146 }
e9937d2a 7147 .tf.bar.rightbut conf -state disabled
fa4da7b3
PM
7148}
7149
354af6bd
PM
7150# save the scrolling position of the diff display pane
7151proc save_position {} {
7152 global historyindex history
7153
7154 if {$historyindex < 1} return
7155 set hi [expr {$historyindex - 1}]
7156 set fn [lindex $history $hi 2]
7157 if {$fn ne {}} {
7158 lset history $hi 3 [eval $fn]
7159 }
7160}
7161
7162proc unset_posvars {} {
7163 global last_posvars
7164
7165 if {[info exists last_posvars]} {
7166 foreach {var val} $last_posvars {
7167 global $var
7168 catch {unset $var}
7169 }
7170 unset last_posvars
7171 }
7172}
7173
2516dae2 7174proc godo {elt} {
354af6bd 7175 global curview last_posvars
2516dae2
PM
7176
7177 set view [lindex $elt 0]
7178 set cmd [lindex $elt 1]
354af6bd 7179 set pv [lindex $elt 3]
2516dae2
PM
7180 if {$curview != $view} {
7181 showview $view
7182 }
354af6bd
PM
7183 unset_posvars
7184 foreach {var val} $pv {
7185 global $var
7186 set $var $val
7187 }
7188 set last_posvars $pv
2516dae2
PM
7189 eval $cmd
7190}
7191
d698206c
PM
7192proc goback {} {
7193 global history historyindex
bd441de4 7194 focus .
d698206c
PM
7195
7196 if {$historyindex > 1} {
354af6bd 7197 save_position
d698206c 7198 incr historyindex -1
2516dae2 7199 godo [lindex $history [expr {$historyindex - 1}]]
e9937d2a 7200 .tf.bar.rightbut conf -state normal
d698206c
PM
7201 }
7202 if {$historyindex <= 1} {
e9937d2a 7203 .tf.bar.leftbut conf -state disabled
d698206c
PM
7204 }
7205}
7206
7207proc goforw {} {
7208 global history historyindex
bd441de4 7209 focus .
d698206c
PM
7210
7211 if {$historyindex < [llength $history]} {
354af6bd 7212 save_position
fa4da7b3 7213 set cmd [lindex $history $historyindex]
d698206c 7214 incr historyindex
2516dae2 7215 godo $cmd
e9937d2a 7216 .tf.bar.leftbut conf -state normal
d698206c
PM
7217 }
7218 if {$historyindex >= [llength $history]} {
e9937d2a 7219 .tf.bar.rightbut conf -state disabled
d698206c 7220 }
e2ed4324
PM
7221}
7222
f8b28a40 7223proc gettree {id} {
8f489363
PM
7224 global treefilelist treeidlist diffids diffmergeid treepending
7225 global nullid nullid2
f8b28a40
PM
7226
7227 set diffids $id
7228 catch {unset diffmergeid}
7229 if {![info exists treefilelist($id)]} {
7230 if {![info exists treepending]} {
8f489363
PM
7231 if {$id eq $nullid} {
7232 set cmd [list | git ls-files]
7233 } elseif {$id eq $nullid2} {
7234 set cmd [list | git ls-files --stage -t]
219ea3a9 7235 } else {
8f489363 7236 set cmd [list | git ls-tree -r $id]
219ea3a9
PM
7237 }
7238 if {[catch {set gtf [open $cmd r]}]} {
f8b28a40
PM
7239 return
7240 }
7241 set treepending $id
7242 set treefilelist($id) {}
7243 set treeidlist($id) {}
09c7029d 7244 fconfigure $gtf -blocking 0 -encoding binary
7eb3cb9c 7245 filerun $gtf [list gettreeline $gtf $id]
f8b28a40
PM
7246 }
7247 } else {
7248 setfilelist $id
7249 }
7250}
7251
7252proc gettreeline {gtf id} {
8f489363 7253 global treefilelist treeidlist treepending cmitmode diffids nullid nullid2
f8b28a40 7254
7eb3cb9c
PM
7255 set nl 0
7256 while {[incr nl] <= 1000 && [gets $gtf line] >= 0} {
8f489363
PM
7257 if {$diffids eq $nullid} {
7258 set fname $line
7259 } else {
9396cd38
PM
7260 set i [string first "\t" $line]
7261 if {$i < 0} continue
9396cd38 7262 set fname [string range $line [expr {$i+1}] end]
f31fa2c0
PM
7263 set line [string range $line 0 [expr {$i-1}]]
7264 if {$diffids ne $nullid2 && [lindex $line 1] ne "blob"} continue
7265 set sha1 [lindex $line 2]
219ea3a9 7266 lappend treeidlist($id) $sha1
219ea3a9 7267 }
09c7029d
AG
7268 if {[string index $fname 0] eq "\""} {
7269 set fname [lindex $fname 0]
7270 }
7271 set fname [encoding convertfrom $fname]
7eb3cb9c
PM
7272 lappend treefilelist($id) $fname
7273 }
7274 if {![eof $gtf]} {
7275 return [expr {$nl >= 1000? 2: 1}]
f8b28a40 7276 }
f8b28a40
PM
7277 close $gtf
7278 unset treepending
7279 if {$cmitmode ne "tree"} {
7280 if {![info exists diffmergeid]} {
7281 gettreediffs $diffids
7282 }
7283 } elseif {$id ne $diffids} {
7284 gettree $diffids
7285 } else {
7286 setfilelist $id
7287 }
7eb3cb9c 7288 return 0
f8b28a40
PM
7289}
7290
7291proc showfile {f} {
8f489363 7292 global treefilelist treeidlist diffids nullid nullid2
7cdc3556 7293 global ctext_file_names ctext_file_lines
f8b28a40
PM
7294 global ctext commentend
7295
7296 set i [lsearch -exact $treefilelist($diffids) $f]
7297 if {$i < 0} {
7298 puts "oops, $f not in list for id $diffids"
7299 return
7300 }
8f489363
PM
7301 if {$diffids eq $nullid} {
7302 if {[catch {set bf [open $f r]} err]} {
7303 puts "oops, can't read $f: $err"
219ea3a9
PM
7304 return
7305 }
7306 } else {
8f489363
PM
7307 set blob [lindex $treeidlist($diffids) $i]
7308 if {[catch {set bf [open [concat | git cat-file blob $blob] r]} err]} {
7309 puts "oops, error reading blob $blob: $err"
219ea3a9
PM
7310 return
7311 }
f8b28a40 7312 }
09c7029d 7313 fconfigure $bf -blocking 0 -encoding [get_path_encoding $f]
7eb3cb9c 7314 filerun $bf [list getblobline $bf $diffids]
f8b28a40 7315 $ctext config -state normal
3ea06f9f 7316 clear_ctext $commentend
7cdc3556
AG
7317 lappend ctext_file_names $f
7318 lappend ctext_file_lines [lindex [split $commentend "."] 0]
f8b28a40
PM
7319 $ctext insert end "\n"
7320 $ctext insert end "$f\n" filesep
7321 $ctext config -state disabled
7322 $ctext yview $commentend
32f1b3e4 7323 settabs 0
f8b28a40
PM
7324}
7325
7326proc getblobline {bf id} {
7327 global diffids cmitmode ctext
7328
7329 if {$id ne $diffids || $cmitmode ne "tree"} {
7330 catch {close $bf}
7eb3cb9c 7331 return 0
f8b28a40
PM
7332 }
7333 $ctext config -state normal
7eb3cb9c
PM
7334 set nl 0
7335 while {[incr nl] <= 1000 && [gets $bf line] >= 0} {
f8b28a40
PM
7336 $ctext insert end "$line\n"
7337 }
7338 if {[eof $bf]} {
8a897742
PM
7339 global jump_to_here ctext_file_names commentend
7340
f8b28a40
PM
7341 # delete last newline
7342 $ctext delete "end - 2c" "end - 1c"
7343 close $bf
8a897742
PM
7344 if {$jump_to_here ne {} &&
7345 [lindex $jump_to_here 0] eq [lindex $ctext_file_names 0]} {
7346 set lnum [expr {[lindex $jump_to_here 1] +
7347 [lindex [split $commentend .] 0]}]
7348 mark_ctext_line $lnum
7349 }
120ea892 7350 $ctext config -state disabled
7eb3cb9c 7351 return 0
f8b28a40
PM
7352 }
7353 $ctext config -state disabled
7eb3cb9c 7354 return [expr {$nl >= 1000? 2: 1}]
f8b28a40
PM
7355}
7356
8a897742 7357proc mark_ctext_line {lnum} {
e3e901be 7358 global ctext markbgcolor
8a897742
PM
7359
7360 $ctext tag delete omark
7361 $ctext tag add omark $lnum.0 "$lnum.0 + 1 line"
e3e901be 7362 $ctext tag conf omark -background $markbgcolor
8a897742
PM
7363 $ctext see $lnum.0
7364}
7365
7fcc92bf 7366proc mergediff {id} {
8b07dca1 7367 global diffmergeid
2df6442f 7368 global diffids treediffs
8b07dca1 7369 global parents curview
e2ed4324 7370
3c461ffe 7371 set diffmergeid $id
7a1d9d14 7372 set diffids $id
2df6442f 7373 set treediffs($id) {}
7fcc92bf 7374 set np [llength $parents($curview,$id)]
32f1b3e4 7375 settabs $np
8b07dca1 7376 getblobdiffs $id
c8a4acbf
PM
7377}
7378
3c461ffe 7379proc startdiff {ids} {
8f489363 7380 global treediffs diffids treepending diffmergeid nullid nullid2
c8dfbcf9 7381
32f1b3e4 7382 settabs 1
4f2c2642 7383 set diffids $ids
3c461ffe 7384 catch {unset diffmergeid}
8f489363
PM
7385 if {![info exists treediffs($ids)] ||
7386 [lsearch -exact $ids $nullid] >= 0 ||
7387 [lsearch -exact $ids $nullid2] >= 0} {
c8dfbcf9 7388 if {![info exists treepending]} {
14c9dbd6 7389 gettreediffs $ids
c8dfbcf9
PM
7390 }
7391 } else {
14c9dbd6 7392 addtocflist $ids
c8dfbcf9
PM
7393 }
7394}
7395
7a39a17a
PM
7396proc path_filter {filter name} {
7397 foreach p $filter {
7398 set l [string length $p]
74a40c71
PM
7399 if {[string index $p end] eq "/"} {
7400 if {[string compare -length $l $p $name] == 0} {
7401 return 1
7402 }
7403 } else {
7404 if {[string compare -length $l $p $name] == 0 &&
7405 ([string length $name] == $l ||
7406 [string index $name $l] eq "/")} {
7407 return 1
7408 }
7a39a17a
PM
7409 }
7410 }
7411 return 0
7412}
7413
c8dfbcf9 7414proc addtocflist {ids} {
74a40c71 7415 global treediffs
7a39a17a 7416
74a40c71 7417 add_flist $treediffs($ids)
c8dfbcf9 7418 getblobdiffs $ids
d2610d11
PM
7419}
7420
219ea3a9 7421proc diffcmd {ids flags} {
8f489363 7422 global nullid nullid2
219ea3a9
PM
7423
7424 set i [lsearch -exact $ids $nullid]
8f489363 7425 set j [lsearch -exact $ids $nullid2]
219ea3a9 7426 if {$i >= 0} {
8f489363
PM
7427 if {[llength $ids] > 1 && $j < 0} {
7428 # comparing working directory with some specific revision
7429 set cmd [concat | git diff-index $flags]
7430 if {$i == 0} {
7431 lappend cmd -R [lindex $ids 1]
7432 } else {
7433 lappend cmd [lindex $ids 0]
7434 }
7435 } else {
7436 # comparing working directory with index
7437 set cmd [concat | git diff-files $flags]
7438 if {$j == 1} {
7439 lappend cmd -R
7440 }
7441 }
7442 } elseif {$j >= 0} {
7443 set cmd [concat | git diff-index --cached $flags]
219ea3a9 7444 if {[llength $ids] > 1} {
8f489363 7445 # comparing index with specific revision
90a77925 7446 if {$j == 0} {
219ea3a9
PM
7447 lappend cmd -R [lindex $ids 1]
7448 } else {
7449 lappend cmd [lindex $ids 0]
7450 }
7451 } else {
8f489363 7452 # comparing index with HEAD
219ea3a9
PM
7453 lappend cmd HEAD
7454 }
7455 } else {
8f489363 7456 set cmd [concat | git diff-tree -r $flags $ids]
219ea3a9
PM
7457 }
7458 return $cmd
7459}
7460
c8dfbcf9 7461proc gettreediffs {ids} {
79b2c75e 7462 global treediff treepending
219ea3a9 7463
7272131b
AG
7464 if {[catch {set gdtf [open [diffcmd $ids {--no-commit-id}] r]}]} return
7465
c8dfbcf9 7466 set treepending $ids
3c461ffe 7467 set treediff {}
09c7029d 7468 fconfigure $gdtf -blocking 0 -encoding binary
7eb3cb9c 7469 filerun $gdtf [list gettreediffline $gdtf $ids]
d2610d11
PM
7470}
7471
c8dfbcf9 7472proc gettreediffline {gdtf ids} {
3c461ffe 7473 global treediff treediffs treepending diffids diffmergeid
39ee47ef 7474 global cmitmode vfilelimit curview limitdiffs perfile_attrs
3c461ffe 7475
7eb3cb9c 7476 set nr 0
4db09304 7477 set sublist {}
39ee47ef
PM
7478 set max 1000
7479 if {$perfile_attrs} {
7480 # cache_gitattr is slow, and even slower on win32 where we
7481 # have to invoke it for only about 30 paths at a time
7482 set max 500
7483 if {[tk windowingsystem] == "win32"} {
7484 set max 120
7485 }
7486 }
7487 while {[incr nr] <= $max && [gets $gdtf line] >= 0} {
9396cd38
PM
7488 set i [string first "\t" $line]
7489 if {$i >= 0} {
7490 set file [string range $line [expr {$i+1}] end]
7491 if {[string index $file 0] eq "\""} {
7492 set file [lindex $file 0]
7493 }
09c7029d 7494 set file [encoding convertfrom $file]
48a81b7c
PM
7495 if {$file ne [lindex $treediff end]} {
7496 lappend treediff $file
7497 lappend sublist $file
7498 }
9396cd38 7499 }
7eb3cb9c 7500 }
39ee47ef
PM
7501 if {$perfile_attrs} {
7502 cache_gitattr encoding $sublist
7503 }
7eb3cb9c 7504 if {![eof $gdtf]} {
39ee47ef 7505 return [expr {$nr >= $max? 2: 1}]
7eb3cb9c
PM
7506 }
7507 close $gdtf
3ed31a81 7508 if {$limitdiffs && $vfilelimit($curview) ne {}} {
74a40c71
PM
7509 set flist {}
7510 foreach f $treediff {
3ed31a81 7511 if {[path_filter $vfilelimit($curview) $f]} {
74a40c71
PM
7512 lappend flist $f
7513 }
7514 }
7515 set treediffs($ids) $flist
7516 } else {
7517 set treediffs($ids) $treediff
7518 }
7eb3cb9c 7519 unset treepending
e1160138 7520 if {$cmitmode eq "tree" && [llength $diffids] == 1} {
7eb3cb9c
PM
7521 gettree $diffids
7522 } elseif {$ids != $diffids} {
7523 if {![info exists diffmergeid]} {
7524 gettreediffs $diffids
b74fd579 7525 }
7eb3cb9c
PM
7526 } else {
7527 addtocflist $ids
d2610d11 7528 }
7eb3cb9c 7529 return 0
d2610d11
PM
7530}
7531
890fae70
SP
7532# empty string or positive integer
7533proc diffcontextvalidate {v} {
7534 return [regexp {^(|[1-9][0-9]*)$} $v]
7535}
7536
7537proc diffcontextchange {n1 n2 op} {
7538 global diffcontextstring diffcontext
7539
7540 if {[string is integer -strict $diffcontextstring]} {
a41ddbb6 7541 if {$diffcontextstring >= 0} {
890fae70
SP
7542 set diffcontext $diffcontextstring
7543 reselectline
7544 }
7545 }
7546}
7547
b9b86007
SP
7548proc changeignorespace {} {
7549 reselectline
7550}
7551
ae4e3ff9
TR
7552proc changeworddiff {name ix op} {
7553 reselectline
7554}
7555
c8dfbcf9 7556proc getblobdiffs {ids} {
8d73b242 7557 global blobdifffd diffids env
7eb3cb9c 7558 global diffinhdr treediffs
890fae70 7559 global diffcontext
b9b86007 7560 global ignorespace
ae4e3ff9 7561 global worddiff
3ed31a81 7562 global limitdiffs vfilelimit curview
8b07dca1 7563 global diffencoding targetline diffnparents
a1d383c5 7564 global git_version currdiffsubmod
c8dfbcf9 7565
a8138733
PM
7566 set textconv {}
7567 if {[package vcompare $git_version "1.6.1"] >= 0} {
7568 set textconv "--textconv"
7569 }
5c838d23
JL
7570 set submodule {}
7571 if {[package vcompare $git_version "1.6.6"] >= 0} {
7572 set submodule "--submodule"
7573 }
7574 set cmd [diffcmd $ids "-p $textconv $submodule -C --cc --no-commit-id -U$diffcontext"]
b9b86007
SP
7575 if {$ignorespace} {
7576 append cmd " -w"
7577 }
ae4e3ff9
TR
7578 if {$worddiff ne [mc "Line diff"]} {
7579 append cmd " --word-diff=porcelain"
7580 }
3ed31a81
PM
7581 if {$limitdiffs && $vfilelimit($curview) ne {}} {
7582 set cmd [concat $cmd -- $vfilelimit($curview)]
7a39a17a
PM
7583 }
7584 if {[catch {set bdf [open $cmd r]} err]} {
8b07dca1 7585 error_popup [mc "Error getting diffs: %s" $err]
e5c2d856
PM
7586 return
7587 }
8a897742 7588 set targetline {}
8b07dca1 7589 set diffnparents 0
4f2c2642 7590 set diffinhdr 0
09c7029d 7591 set diffencoding [get_path_encoding {}]
681c3290 7592 fconfigure $bdf -blocking 0 -encoding binary -eofchar {}
c8dfbcf9 7593 set blobdifffd($ids) $bdf
a1d383c5 7594 set currdiffsubmod ""
7eb3cb9c 7595 filerun $bdf [list getblobdiffline $bdf $diffids]
e5c2d856
PM
7596}
7597
354af6bd
PM
7598proc savecmitpos {} {
7599 global ctext cmitmode
7600
7601 if {$cmitmode eq "tree"} {
7602 return {}
7603 }
7604 return [list target_scrollpos [$ctext index @0,0]]
7605}
7606
7607proc savectextpos {} {
7608 global ctext
7609
7610 return [list target_scrollpos [$ctext index @0,0]]
7611}
7612
7613proc maybe_scroll_ctext {ateof} {
7614 global ctext target_scrollpos
7615
7616 if {![info exists target_scrollpos]} return
7617 if {!$ateof} {
7618 set nlines [expr {[winfo height $ctext]
7619 / [font metrics textfont -linespace]}]
7620 if {[$ctext compare "$target_scrollpos + $nlines lines" <= end]} return
7621 }
7622 $ctext yview $target_scrollpos
7623 unset target_scrollpos
7624}
7625
89b11d3b
PM
7626proc setinlist {var i val} {
7627 global $var
7628
7629 while {[llength [set $var]] < $i} {
7630 lappend $var {}
7631 }
7632 if {[llength [set $var]] == $i} {
7633 lappend $var $val
7634 } else {
7635 lset $var $i $val
7636 }
7637}
7638
9396cd38 7639proc makediffhdr {fname ids} {
8b07dca1 7640 global ctext curdiffstart treediffs diffencoding
8a897742 7641 global ctext_file_names jump_to_here targetline diffline
9396cd38 7642
8b07dca1
PM
7643 set fname [encoding convertfrom $fname]
7644 set diffencoding [get_path_encoding $fname]
9396cd38
PM
7645 set i [lsearch -exact $treediffs($ids) $fname]
7646 if {$i >= 0} {
7647 setinlist difffilestart $i $curdiffstart
7648 }
48a81b7c 7649 lset ctext_file_names end $fname
9396cd38
PM
7650 set l [expr {(78 - [string length $fname]) / 2}]
7651 set pad [string range "----------------------------------------" 1 $l]
7652 $ctext insert $curdiffstart "$pad $fname $pad" filesep
8a897742
PM
7653 set targetline {}
7654 if {$jump_to_here ne {} && [lindex $jump_to_here 0] eq $fname} {
7655 set targetline [lindex $jump_to_here 1]
7656 }
7657 set diffline 0
9396cd38
PM
7658}
7659
c8dfbcf9 7660proc getblobdiffline {bdf ids} {
9396cd38 7661 global diffids blobdifffd ctext curdiffstart
7eab2933 7662 global diffnexthead diffnextnote difffilestart
7cdc3556 7663 global ctext_file_names ctext_file_lines
8b07dca1 7664 global diffinhdr treediffs mergemax diffnparents
a1d383c5 7665 global diffencoding jump_to_here targetline diffline currdiffsubmod
ae4e3ff9 7666 global worddiff
c8dfbcf9 7667
7eb3cb9c 7668 set nr 0
e5c2d856 7669 $ctext conf -state normal
7eb3cb9c
PM
7670 while {[incr nr] <= 1000 && [gets $bdf line] >= 0} {
7671 if {$ids != $diffids || $bdf != $blobdifffd($ids)} {
c21398be 7672 catch {close $bdf}
7eb3cb9c 7673 return 0
89b11d3b 7674 }
8b07dca1
PM
7675 if {![string compare -length 5 "diff " $line]} {
7676 if {![regexp {^diff (--cc|--git) } $line m type]} {
7677 set line [encoding convertfrom $line]
7678 $ctext insert end "$line\n" hunksep
7679 continue
7680 }
7eb3cb9c 7681 # start of a new file
8b07dca1 7682 set diffinhdr 1
7eb3cb9c 7683 $ctext insert end "\n"
9396cd38 7684 set curdiffstart [$ctext index "end - 1c"]
7cdc3556
AG
7685 lappend ctext_file_names ""
7686 lappend ctext_file_lines [lindex [split $curdiffstart "."] 0]
9396cd38 7687 $ctext insert end "\n" filesep
8b07dca1
PM
7688
7689 if {$type eq "--cc"} {
7690 # start of a new file in a merge diff
7691 set fname [string range $line 10 end]
7692 if {[lsearch -exact $treediffs($ids) $fname] < 0} {
7693 lappend treediffs($ids) $fname
7694 add_flist [list $fname]
7695 }
7696
9396cd38 7697 } else {
8b07dca1
PM
7698 set line [string range $line 11 end]
7699 # If the name hasn't changed the length will be odd,
7700 # the middle char will be a space, and the two bits either
7701 # side will be a/name and b/name, or "a/name" and "b/name".
7702 # If the name has changed we'll get "rename from" and
7703 # "rename to" or "copy from" and "copy to" lines following
7704 # this, and we'll use them to get the filenames.
7705 # This complexity is necessary because spaces in the
7706 # filename(s) don't get escaped.
7707 set l [string length $line]
7708 set i [expr {$l / 2}]
7709 if {!(($l & 1) && [string index $line $i] eq " " &&
7710 [string range $line 2 [expr {$i - 1}]] eq \
7711 [string range $line [expr {$i + 3}] end])} {
7712 continue
7713 }
7714 # unescape if quoted and chop off the a/ from the front
7715 if {[string index $line 0] eq "\""} {
7716 set fname [string range [lindex $line 0] 2 end]
7717 } else {
7718 set fname [string range $line 2 [expr {$i - 1}]]
7719 }
7eb3cb9c 7720 }
9396cd38
PM
7721 makediffhdr $fname $ids
7722
48a81b7c
PM
7723 } elseif {![string compare -length 16 "* Unmerged path " $line]} {
7724 set fname [encoding convertfrom [string range $line 16 end]]
7725 $ctext insert end "\n"
7726 set curdiffstart [$ctext index "end - 1c"]
7727 lappend ctext_file_names $fname
7728 lappend ctext_file_lines [lindex [split $curdiffstart "."] 0]
7729 $ctext insert end "$line\n" filesep
7730 set i [lsearch -exact $treediffs($ids) $fname]
7731 if {$i >= 0} {
7732 setinlist difffilestart $i $curdiffstart
7733 }
7734
8b07dca1
PM
7735 } elseif {![string compare -length 2 "@@" $line]} {
7736 regexp {^@@+} $line ats
09c7029d 7737 set line [encoding convertfrom $diffencoding $line]
7eb3cb9c 7738 $ctext insert end "$line\n" hunksep
8b07dca1
PM
7739 if {[regexp { \+(\d+),\d+ @@} $line m nl]} {
7740 set diffline $nl
7741 }
7742 set diffnparents [expr {[string length $ats] - 1}]
7eb3cb9c 7743 set diffinhdr 0
9396cd38 7744
5c838d23
JL
7745 } elseif {![string compare -length 10 "Submodule " $line]} {
7746 # start of a new submodule
a1d383c5
JL
7747 if {[regexp -indices "\[0-9a-f\]+\\.\\." $line nameend]} {
7748 set fname [string range $line 10 [expr [lindex $nameend 0] - 2]]
7749 } else {
7750 set fname [string range $line 10 [expr [string first "contains " $line] - 2]]
7751 }
7752 if {$currdiffsubmod != $fname} {
5c838d23
JL
7753 $ctext insert end "\n"; # Add newline after commit message
7754 }
7755 set curdiffstart [$ctext index "end - 1c"]
7756 lappend ctext_file_names ""
a1d383c5
JL
7757 if {$currdiffsubmod != $fname} {
7758 lappend ctext_file_lines $fname
7759 makediffhdr $fname $ids
7760 set currdiffsubmod $fname
7761 $ctext insert end "\n$line\n" filesep
7762 } else {
7763 $ctext insert end "$line\n" filesep
7764 }
5c838d23 7765 } elseif {![string compare -length 3 " >" $line]} {
a1d383c5 7766 set $currdiffsubmod ""
1f2cecfd 7767 set line [encoding convertfrom $diffencoding $line]
5c838d23
JL
7768 $ctext insert end "$line\n" dresult
7769 } elseif {![string compare -length 3 " <" $line]} {
a1d383c5 7770 set $currdiffsubmod ""
1f2cecfd 7771 set line [encoding convertfrom $diffencoding $line]
5c838d23 7772 $ctext insert end "$line\n" d0
9396cd38 7773 } elseif {$diffinhdr} {
5e85ec4c 7774 if {![string compare -length 12 "rename from " $line]} {
d1cb298b 7775 set fname [string range $line [expr 6 + [string first " from " $line] ] end]
9396cd38
PM
7776 if {[string index $fname 0] eq "\""} {
7777 set fname [lindex $fname 0]
7778 }
09c7029d 7779 set fname [encoding convertfrom $fname]
9396cd38
PM
7780 set i [lsearch -exact $treediffs($ids) $fname]
7781 if {$i >= 0} {
7782 setinlist difffilestart $i $curdiffstart
7783 }
d1cb298b
JS
7784 } elseif {![string compare -length 10 $line "rename to "] ||
7785 ![string compare -length 8 $line "copy to "]} {
7786 set fname [string range $line [expr 4 + [string first " to " $line] ] end]
9396cd38
PM
7787 if {[string index $fname 0] eq "\""} {
7788 set fname [lindex $fname 0]
7789 }
7790 makediffhdr $fname $ids
7791 } elseif {[string compare -length 3 $line "---"] == 0} {
7792 # do nothing
7793 continue
7794 } elseif {[string compare -length 3 $line "+++"] == 0} {
7795 set diffinhdr 0
7796 continue
7797 }
7798 $ctext insert end "$line\n" filesep
7799
e5c2d856 7800 } else {
681c3290
PT
7801 set line [string map {\x1A ^Z} \
7802 [encoding convertfrom $diffencoding $line]]
8b07dca1
PM
7803 # parse the prefix - one ' ', '-' or '+' for each parent
7804 set prefix [string range $line 0 [expr {$diffnparents - 1}]]
7805 set tag [expr {$diffnparents > 1? "m": "d"}]
ae4e3ff9
TR
7806 set dowords [expr {$worddiff ne [mc "Line diff"] && $diffnparents == 1}]
7807 set words_pre_markup ""
7808 set words_post_markup ""
8b07dca1
PM
7809 if {[string trim $prefix " -+"] eq {}} {
7810 # prefix only has " ", "-" and "+" in it: normal diff line
7811 set num [string first "-" $prefix]
ae4e3ff9
TR
7812 if {$dowords} {
7813 set line [string range $line 1 end]
7814 }
8b07dca1
PM
7815 if {$num >= 0} {
7816 # removed line, first parent with line is $num
7817 if {$num >= $mergemax} {
7818 set num "max"
7819 }
ae4e3ff9
TR
7820 if {$dowords && $worddiff eq [mc "Markup words"]} {
7821 $ctext insert end "\[-$line-\]" $tag$num
7822 } else {
7823 $ctext insert end "$line" $tag$num
7824 }
7825 if {!$dowords} {
7826 $ctext insert end "\n" $tag$num
7827 }
8b07dca1
PM
7828 } else {
7829 set tags {}
7830 if {[string first "+" $prefix] >= 0} {
7831 # added line
7832 lappend tags ${tag}result
7833 if {$diffnparents > 1} {
7834 set num [string first " " $prefix]
7835 if {$num >= 0} {
7836 if {$num >= $mergemax} {
7837 set num "max"
7838 }
7839 lappend tags m$num
7840 }
7841 }
ae4e3ff9
TR
7842 set words_pre_markup "{+"
7843 set words_post_markup "+}"
8b07dca1
PM
7844 }
7845 if {$targetline ne {}} {
7846 if {$diffline == $targetline} {
7847 set seehere [$ctext index "end - 1 chars"]
7848 set targetline {}
7849 } else {
7850 incr diffline
7851 }
7852 }
ae4e3ff9
TR
7853 if {$dowords && $worddiff eq [mc "Markup words"]} {
7854 $ctext insert end "$words_pre_markup$line$words_post_markup" $tags
7855 } else {
7856 $ctext insert end "$line" $tags
7857 }
7858 if {!$dowords} {
7859 $ctext insert end "\n" $tags
7860 }
8b07dca1 7861 }
ae4e3ff9
TR
7862 } elseif {$dowords && $prefix eq "~"} {
7863 $ctext insert end "\n" {}
7eb3cb9c 7864 } else {
9396cd38
PM
7865 # "\ No newline at end of file",
7866 # or something else we don't recognize
7867 $ctext insert end "$line\n" hunksep
e5c2d856 7868 }
e5c2d856
PM
7869 }
7870 }
8b07dca1
PM
7871 if {[info exists seehere]} {
7872 mark_ctext_line [lindex [split $seehere .] 0]
7873 }
354af6bd 7874 maybe_scroll_ctext [eof $bdf]
e5c2d856 7875 $ctext conf -state disabled
7eb3cb9c 7876 if {[eof $bdf]} {
c21398be 7877 catch {close $bdf}
7eb3cb9c 7878 return 0
c8dfbcf9 7879 }
7eb3cb9c 7880 return [expr {$nr >= 1000? 2: 1}]
e5c2d856
PM
7881}
7882
a8d610a2
PM
7883proc changediffdisp {} {
7884 global ctext diffelide
7885
7886 $ctext tag conf d0 -elide [lindex $diffelide 0]
8b07dca1 7887 $ctext tag conf dresult -elide [lindex $diffelide 1]
a8d610a2
PM
7888}
7889
f4c54b3c
PM
7890proc highlightfile {loc cline} {
7891 global ctext cflist cflist_top
7892
7893 $ctext yview $loc
7894 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
7895 $cflist tag add highlight $cline.0 "$cline.0 lineend"
7896 $cflist see $cline.0
7897 set cflist_top $cline
7898}
7899
67c22874 7900proc prevfile {} {
f4c54b3c
PM
7901 global difffilestart ctext cmitmode
7902
7903 if {$cmitmode eq "tree"} return
7904 set prev 0.0
7905 set prevline 1
67c22874
OH
7906 set here [$ctext index @0,0]
7907 foreach loc $difffilestart {
7908 if {[$ctext compare $loc >= $here]} {
f4c54b3c 7909 highlightfile $prev $prevline
67c22874
OH
7910 return
7911 }
7912 set prev $loc
f4c54b3c 7913 incr prevline
67c22874 7914 }
f4c54b3c 7915 highlightfile $prev $prevline
67c22874
OH
7916}
7917
39ad8570 7918proc nextfile {} {
f4c54b3c
PM
7919 global difffilestart ctext cmitmode
7920
7921 if {$cmitmode eq "tree"} return
39ad8570 7922 set here [$ctext index @0,0]
f4c54b3c 7923 set line 1
7fcceed7 7924 foreach loc $difffilestart {
f4c54b3c 7925 incr line
7fcceed7 7926 if {[$ctext compare $loc > $here]} {
f4c54b3c 7927 highlightfile $loc $line
67c22874 7928 return
39ad8570
PM
7929 }
7930 }
1db95b00
PM
7931}
7932
3ea06f9f
PM
7933proc clear_ctext {{first 1.0}} {
7934 global ctext smarktop smarkbot
7cdc3556 7935 global ctext_file_names ctext_file_lines
97645683 7936 global pendinglinks
3ea06f9f 7937
1902c270
PM
7938 set l [lindex [split $first .] 0]
7939 if {![info exists smarktop] || [$ctext compare $first < $smarktop.0]} {
7940 set smarktop $l
3ea06f9f 7941 }
1902c270
PM
7942 if {![info exists smarkbot] || [$ctext compare $first < $smarkbot.0]} {
7943 set smarkbot $l
3ea06f9f
PM
7944 }
7945 $ctext delete $first end
97645683
PM
7946 if {$first eq "1.0"} {
7947 catch {unset pendinglinks}
7948 }
7cdc3556
AG
7949 set ctext_file_names {}
7950 set ctext_file_lines {}
3ea06f9f
PM
7951}
7952
32f1b3e4 7953proc settabs {{firstab {}}} {
9c311b32 7954 global firsttabstop tabstop ctext have_tk85
32f1b3e4
PM
7955
7956 if {$firstab ne {} && $have_tk85} {
7957 set firsttabstop $firstab
7958 }
9c311b32 7959 set w [font measure textfont "0"]
32f1b3e4 7960 if {$firsttabstop != 0} {
64b5f146
PM
7961 $ctext conf -tabs [list [expr {($firsttabstop + $tabstop) * $w}] \
7962 [expr {($firsttabstop + 2 * $tabstop) * $w}]]
32f1b3e4
PM
7963 } elseif {$have_tk85 || $tabstop != 8} {
7964 $ctext conf -tabs [expr {$tabstop * $w}]
7965 } else {
7966 $ctext conf -tabs {}
7967 }
3ea06f9f
PM
7968}
7969
7970proc incrsearch {name ix op} {
1902c270 7971 global ctext searchstring searchdirn
3ea06f9f
PM
7972
7973 $ctext tag remove found 1.0 end
1902c270
PM
7974 if {[catch {$ctext index anchor}]} {
7975 # no anchor set, use start of selection, or of visible area
7976 set sel [$ctext tag ranges sel]
7977 if {$sel ne {}} {
7978 $ctext mark set anchor [lindex $sel 0]
7979 } elseif {$searchdirn eq "-forwards"} {
7980 $ctext mark set anchor @0,0
7981 } else {
7982 $ctext mark set anchor @0,[winfo height $ctext]
7983 }
7984 }
3ea06f9f 7985 if {$searchstring ne {}} {
1902c270
PM
7986 set here [$ctext search $searchdirn -- $searchstring anchor]
7987 if {$here ne {}} {
7988 $ctext see $here
7989 }
3ea06f9f
PM
7990 searchmarkvisible 1
7991 }
7992}
7993
7994proc dosearch {} {
1902c270 7995 global sstring ctext searchstring searchdirn
3ea06f9f
PM
7996
7997 focus $sstring
7998 $sstring icursor end
1902c270
PM
7999 set searchdirn -forwards
8000 if {$searchstring ne {}} {
8001 set sel [$ctext tag ranges sel]
8002 if {$sel ne {}} {
8003 set start "[lindex $sel 0] + 1c"
8004 } elseif {[catch {set start [$ctext index anchor]}]} {
8005 set start "@0,0"
8006 }
8007 set match [$ctext search -count mlen -- $searchstring $start]
8008 $ctext tag remove sel 1.0 end
8009 if {$match eq {}} {
8010 bell
8011 return
8012 }
8013 $ctext see $match
8014 set mend "$match + $mlen c"
8015 $ctext tag add sel $match $mend
8016 $ctext mark unset anchor
8017 }
8018}
8019
8020proc dosearchback {} {
8021 global sstring ctext searchstring searchdirn
8022
8023 focus $sstring
8024 $sstring icursor end
8025 set searchdirn -backwards
8026 if {$searchstring ne {}} {
8027 set sel [$ctext tag ranges sel]
8028 if {$sel ne {}} {
8029 set start [lindex $sel 0]
8030 } elseif {[catch {set start [$ctext index anchor]}]} {
8031 set start @0,[winfo height $ctext]
8032 }
8033 set match [$ctext search -backwards -count ml -- $searchstring $start]
8034 $ctext tag remove sel 1.0 end
8035 if {$match eq {}} {
8036 bell
8037 return
8038 }
8039 $ctext see $match
8040 set mend "$match + $ml c"
8041 $ctext tag add sel $match $mend
8042 $ctext mark unset anchor
3ea06f9f 8043 }
3ea06f9f
PM
8044}
8045
8046proc searchmark {first last} {
8047 global ctext searchstring
8048
8049 set mend $first.0
8050 while {1} {
8051 set match [$ctext search -count mlen -- $searchstring $mend $last.end]
8052 if {$match eq {}} break
8053 set mend "$match + $mlen c"
8054 $ctext tag add found $match $mend
8055 }
8056}
8057
8058proc searchmarkvisible {doall} {
8059 global ctext smarktop smarkbot
8060
8061 set topline [lindex [split [$ctext index @0,0] .] 0]
8062 set botline [lindex [split [$ctext index @0,[winfo height $ctext]] .] 0]
8063 if {$doall || $botline < $smarktop || $topline > $smarkbot} {
8064 # no overlap with previous
8065 searchmark $topline $botline
8066 set smarktop $topline
8067 set smarkbot $botline
8068 } else {
8069 if {$topline < $smarktop} {
8070 searchmark $topline [expr {$smarktop-1}]
8071 set smarktop $topline
8072 }
8073 if {$botline > $smarkbot} {
8074 searchmark [expr {$smarkbot+1}] $botline
8075 set smarkbot $botline
8076 }
8077 }
8078}
8079
8080proc scrolltext {f0 f1} {
1902c270 8081 global searchstring
3ea06f9f 8082
8809d691 8083 .bleft.bottom.sb set $f0 $f1
3ea06f9f
PM
8084 if {$searchstring ne {}} {
8085 searchmarkvisible 0
8086 }
8087}
8088
1d10f36d 8089proc setcoords {} {
9c311b32 8090 global linespc charspc canvx0 canvy0
f6075eba 8091 global xspc1 xspc2 lthickness
8d858d1a 8092
9c311b32
PM
8093 set linespc [font metrics mainfont -linespace]
8094 set charspc [font measure mainfont "m"]
9f1afe05
PM
8095 set canvy0 [expr {int(3 + 0.5 * $linespc)}]
8096 set canvx0 [expr {int(3 + 0.5 * $linespc)}]
f6075eba 8097 set lthickness [expr {int($linespc / 9) + 1}]
8d858d1a
PM
8098 set xspc1(0) $linespc
8099 set xspc2 $linespc
9a40c50c 8100}
1db95b00 8101
1d10f36d 8102proc redisplay {} {
be0cd098 8103 global canv
9f1afe05
PM
8104 global selectedline
8105
8106 set ymax [lindex [$canv cget -scrollregion] 3]
8107 if {$ymax eq {} || $ymax == 0} return
8108 set span [$canv yview]
8109 clear_display
be0cd098 8110 setcanvscroll
9f1afe05
PM
8111 allcanvs yview moveto [lindex $span 0]
8112 drawvisible
94b4a69f 8113 if {$selectedline ne {}} {
9f1afe05 8114 selectline $selectedline 0
ca6d8f58 8115 allcanvs yview moveto [lindex $span 0]
1d10f36d
PM
8116 }
8117}
8118
0ed1dd3c
PM
8119proc parsefont {f n} {
8120 global fontattr
8121
8122 set fontattr($f,family) [lindex $n 0]
8123 set s [lindex $n 1]
8124 if {$s eq {} || $s == 0} {
8125 set s 10
8126 } elseif {$s < 0} {
8127 set s [expr {int(-$s / [winfo fpixels . 1p] + 0.5)}]
9c311b32 8128 }
0ed1dd3c
PM
8129 set fontattr($f,size) $s
8130 set fontattr($f,weight) normal
8131 set fontattr($f,slant) roman
8132 foreach style [lrange $n 2 end] {
8133 switch -- $style {
8134 "normal" -
8135 "bold" {set fontattr($f,weight) $style}
8136 "roman" -
8137 "italic" {set fontattr($f,slant) $style}
8138 }
9c311b32 8139 }
0ed1dd3c
PM
8140}
8141
8142proc fontflags {f {isbold 0}} {
8143 global fontattr
8144
8145 return [list -family $fontattr($f,family) -size $fontattr($f,size) \
8146 -weight [expr {$isbold? "bold": $fontattr($f,weight)}] \
8147 -slant $fontattr($f,slant)]
8148}
8149
8150proc fontname {f} {
8151 global fontattr
8152
8153 set n [list $fontattr($f,family) $fontattr($f,size)]
8154 if {$fontattr($f,weight) eq "bold"} {
8155 lappend n "bold"
9c311b32 8156 }
0ed1dd3c
PM
8157 if {$fontattr($f,slant) eq "italic"} {
8158 lappend n "italic"
9c311b32 8159 }
0ed1dd3c 8160 return $n
9c311b32
PM
8161}
8162
1d10f36d 8163proc incrfont {inc} {
7fcc92bf 8164 global mainfont textfont ctext canv cflist showrefstop
0ed1dd3c
PM
8165 global stopped entries fontattr
8166
1d10f36d 8167 unmarkmatches
0ed1dd3c 8168 set s $fontattr(mainfont,size)
9c311b32
PM
8169 incr s $inc
8170 if {$s < 1} {
8171 set s 1
8172 }
0ed1dd3c 8173 set fontattr(mainfont,size) $s
9c311b32
PM
8174 font config mainfont -size $s
8175 font config mainfontbold -size $s
0ed1dd3c
PM
8176 set mainfont [fontname mainfont]
8177 set s $fontattr(textfont,size)
9c311b32
PM
8178 incr s $inc
8179 if {$s < 1} {
8180 set s 1
8181 }
0ed1dd3c 8182 set fontattr(textfont,size) $s
9c311b32
PM
8183 font config textfont -size $s
8184 font config textfontbold -size $s
0ed1dd3c 8185 set textfont [fontname textfont]
1d10f36d 8186 setcoords
32f1b3e4 8187 settabs
1d10f36d
PM
8188 redisplay
8189}
1db95b00 8190
ee3dc72e
PM
8191proc clearsha1 {} {
8192 global sha1entry sha1string
8193 if {[string length $sha1string] == 40} {
8194 $sha1entry delete 0 end
8195 }
8196}
8197
887fe3c4
PM
8198proc sha1change {n1 n2 op} {
8199 global sha1string currentid sha1but
8200 if {$sha1string == {}
8201 || ([info exists currentid] && $sha1string == $currentid)} {
8202 set state disabled
8203 } else {
8204 set state normal
8205 }
8206 if {[$sha1but cget -state] == $state} return
8207 if {$state == "normal"} {
d990cedf 8208 $sha1but conf -state normal -relief raised -text "[mc "Goto:"] "
887fe3c4 8209 } else {
d990cedf 8210 $sha1but conf -state disabled -relief flat -text "[mc "SHA1 ID:"] "
887fe3c4
PM
8211 }
8212}
8213
8214proc gotocommit {} {
7fcc92bf 8215 global sha1string tagids headids curview varcid
f3b8b3ce 8216
887fe3c4
PM
8217 if {$sha1string == {}
8218 || ([info exists currentid] && $sha1string == $currentid)} return
8219 if {[info exists tagids($sha1string)]} {
8220 set id $tagids($sha1string)
e1007129
SR
8221 } elseif {[info exists headids($sha1string)]} {
8222 set id $headids($sha1string)
887fe3c4
PM
8223 } else {
8224 set id [string tolower $sha1string]
f3b8b3ce 8225 if {[regexp {^[0-9a-f]{4,39}$} $id]} {
d375ef9b 8226 set matches [longid $id]
f3b8b3ce
PM
8227 if {$matches ne {}} {
8228 if {[llength $matches] > 1} {
d990cedf 8229 error_popup [mc "Short SHA1 id %s is ambiguous" $id]
f3b8b3ce
PM
8230 return
8231 }
d375ef9b 8232 set id [lindex $matches 0]
f3b8b3ce 8233 }
9bf3acfa
TR
8234 } else {
8235 if {[catch {set id [exec git rev-parse --verify $sha1string]}]} {
8236 error_popup [mc "Revision %s is not known" $sha1string]
8237 return
8238 }
f3b8b3ce 8239 }
887fe3c4 8240 }
7fcc92bf
PM
8241 if {[commitinview $id $curview]} {
8242 selectline [rowofcommit $id] 1
887fe3c4
PM
8243 return
8244 }
f3b8b3ce 8245 if {[regexp {^[0-9a-fA-F]{4,}$} $sha1string]} {
d990cedf 8246 set msg [mc "SHA1 id %s is not known" $sha1string]
887fe3c4 8247 } else {
9bf3acfa 8248 set msg [mc "Revision %s is not in the current view" $sha1string]
887fe3c4 8249 }
d990cedf 8250 error_popup $msg
887fe3c4
PM
8251}
8252
84ba7345
PM
8253proc lineenter {x y id} {
8254 global hoverx hovery hoverid hovertimer
8255 global commitinfo canv
8256
8ed16484 8257 if {![info exists commitinfo($id)] && ![getcommit $id]} return
84ba7345
PM
8258 set hoverx $x
8259 set hovery $y
8260 set hoverid $id
8261 if {[info exists hovertimer]} {
8262 after cancel $hovertimer
8263 }
8264 set hovertimer [after 500 linehover]
8265 $canv delete hover
8266}
8267
8268proc linemotion {x y id} {
8269 global hoverx hovery hoverid hovertimer
8270
8271 if {[info exists hoverid] && $id == $hoverid} {
8272 set hoverx $x
8273 set hovery $y
8274 if {[info exists hovertimer]} {
8275 after cancel $hovertimer
8276 }
8277 set hovertimer [after 500 linehover]
8278 }
8279}
8280
8281proc lineleave {id} {
8282 global hoverid hovertimer canv
8283
8284 if {[info exists hoverid] && $id == $hoverid} {
8285 $canv delete hover
8286 if {[info exists hovertimer]} {
8287 after cancel $hovertimer
8288 unset hovertimer
8289 }
8290 unset hoverid
8291 }
8292}
8293
8294proc linehover {} {
8295 global hoverx hovery hoverid hovertimer
8296 global canv linespc lthickness
9c311b32 8297 global commitinfo
84ba7345
PM
8298
8299 set text [lindex $commitinfo($hoverid) 0]
8300 set ymax [lindex [$canv cget -scrollregion] 3]
8301 if {$ymax == {}} return
8302 set yfrac [lindex [$canv yview] 0]
8303 set x [expr {$hoverx + 2 * $linespc}]
8304 set y [expr {$hovery + $yfrac * $ymax - $linespc / 2}]
8305 set x0 [expr {$x - 2 * $lthickness}]
8306 set y0 [expr {$y - 2 * $lthickness}]
9c311b32 8307 set x1 [expr {$x + [font measure mainfont $text] + 2 * $lthickness}]
84ba7345
PM
8308 set y1 [expr {$y + $linespc + 2 * $lthickness}]
8309 set t [$canv create rectangle $x0 $y0 $x1 $y1 \
8310 -fill \#ffff80 -outline black -width 1 -tags hover]
8311 $canv raise $t
f8a2c0d1 8312 set t [$canv create text $x $y -anchor nw -text $text -tags hover \
9c311b32 8313 -font mainfont]
84ba7345
PM
8314 $canv raise $t
8315}
8316
9843c307 8317proc clickisonarrow {id y} {
50b44ece 8318 global lthickness
9843c307 8319
50b44ece 8320 set ranges [rowranges $id]
9843c307 8321 set thresh [expr {2 * $lthickness + 6}]
50b44ece 8322 set n [expr {[llength $ranges] - 1}]
f6342480 8323 for {set i 1} {$i < $n} {incr i} {
50b44ece 8324 set row [lindex $ranges $i]
f6342480
PM
8325 if {abs([yc $row] - $y) < $thresh} {
8326 return $i
9843c307
PM
8327 }
8328 }
8329 return {}
8330}
8331
f6342480 8332proc arrowjump {id n y} {
50b44ece 8333 global canv
9843c307 8334
f6342480
PM
8335 # 1 <-> 2, 3 <-> 4, etc...
8336 set n [expr {(($n - 1) ^ 1) + 1}]
50b44ece 8337 set row [lindex [rowranges $id] $n]
f6342480 8338 set yt [yc $row]
9843c307
PM
8339 set ymax [lindex [$canv cget -scrollregion] 3]
8340 if {$ymax eq {} || $ymax <= 0} return
8341 set view [$canv yview]
8342 set yspan [expr {[lindex $view 1] - [lindex $view 0]}]
8343 set yfrac [expr {$yt / $ymax - $yspan / 2}]
8344 if {$yfrac < 0} {
8345 set yfrac 0
8346 }
f6342480 8347 allcanvs yview moveto $yfrac
9843c307
PM
8348}
8349
fa4da7b3 8350proc lineclick {x y id isnew} {
7fcc92bf 8351 global ctext commitinfo children canv thickerline curview
c8dfbcf9 8352
8ed16484 8353 if {![info exists commitinfo($id)] && ![getcommit $id]} return
c8dfbcf9 8354 unmarkmatches
fa4da7b3 8355 unselectline
9843c307
PM
8356 normalline
8357 $canv delete hover
8358 # draw this line thicker than normal
9843c307 8359 set thickerline $id
c934a8a3 8360 drawlines $id
fa4da7b3 8361 if {$isnew} {
9843c307
PM
8362 set ymax [lindex [$canv cget -scrollregion] 3]
8363 if {$ymax eq {}} return
8364 set yfrac [lindex [$canv yview] 0]
8365 set y [expr {$y + $yfrac * $ymax}]
8366 }
8367 set dirn [clickisonarrow $id $y]
8368 if {$dirn ne {}} {
8369 arrowjump $id $dirn $y
8370 return
8371 }
8372
8373 if {$isnew} {
354af6bd 8374 addtohistory [list lineclick $x $y $id 0] savectextpos
fa4da7b3 8375 }
c8dfbcf9
PM
8376 # fill the details pane with info about this line
8377 $ctext conf -state normal
3ea06f9f 8378 clear_ctext
32f1b3e4 8379 settabs 0
d990cedf 8380 $ctext insert end "[mc "Parent"]:\t"
97645683
PM
8381 $ctext insert end $id link0
8382 setlink $id link0
c8dfbcf9 8383 set info $commitinfo($id)
fa4da7b3 8384 $ctext insert end "\n\t[lindex $info 0]\n"
d990cedf 8385 $ctext insert end "\t[mc "Author"]:\t[lindex $info 1]\n"
232475d3 8386 set date [formatdate [lindex $info 2]]
d990cedf 8387 $ctext insert end "\t[mc "Date"]:\t$date\n"
da7c24dd 8388 set kids $children($curview,$id)
79b2c75e 8389 if {$kids ne {}} {
d990cedf 8390 $ctext insert end "\n[mc "Children"]:"
fa4da7b3 8391 set i 0
79b2c75e 8392 foreach child $kids {
fa4da7b3 8393 incr i
8ed16484 8394 if {![info exists commitinfo($child)] && ![getcommit $child]} continue
c8dfbcf9 8395 set info $commitinfo($child)
fa4da7b3 8396 $ctext insert end "\n\t"
97645683
PM
8397 $ctext insert end $child link$i
8398 setlink $child link$i
fa4da7b3 8399 $ctext insert end "\n\t[lindex $info 0]"
d990cedf 8400 $ctext insert end "\n\t[mc "Author"]:\t[lindex $info 1]"
232475d3 8401 set date [formatdate [lindex $info 2]]
d990cedf 8402 $ctext insert end "\n\t[mc "Date"]:\t$date\n"
c8dfbcf9
PM
8403 }
8404 }
354af6bd 8405 maybe_scroll_ctext 1
c8dfbcf9 8406 $ctext conf -state disabled
7fcceed7 8407 init_flist {}
c8dfbcf9
PM
8408}
8409
9843c307
PM
8410proc normalline {} {
8411 global thickerline
8412 if {[info exists thickerline]} {
c934a8a3 8413 set id $thickerline
9843c307 8414 unset thickerline
c934a8a3 8415 drawlines $id
9843c307
PM
8416 }
8417}
8418
354af6bd 8419proc selbyid {id {isnew 1}} {
7fcc92bf
PM
8420 global curview
8421 if {[commitinview $id $curview]} {
354af6bd 8422 selectline [rowofcommit $id] $isnew
c8dfbcf9
PM
8423 }
8424}
8425
8426proc mstime {} {
8427 global startmstime
8428 if {![info exists startmstime]} {
8429 set startmstime [clock clicks -milliseconds]
8430 }
8431 return [format "%.3f" [expr {([clock click -milliseconds] - $startmstime) / 1000.0}]]
8432}
8433
8434proc rowmenu {x y id} {
7fcc92bf 8435 global rowctxmenu selectedline rowmenuid curview
b9fdba7f 8436 global nullid nullid2 fakerowmenu mainhead markedid
c8dfbcf9 8437
bb3edc8b 8438 stopfinding
219ea3a9 8439 set rowmenuid $id
94b4a69f 8440 if {$selectedline eq {} || [rowofcommit $id] eq $selectedline} {
c8dfbcf9
PM
8441 set state disabled
8442 } else {
8443 set state normal
8444 }
8f489363 8445 if {$id ne $nullid && $id ne $nullid2} {
219ea3a9 8446 set menu $rowctxmenu
5e3502da 8447 if {$mainhead ne {}} {
da12e59d 8448 $menu entryconfigure 7 -label [mc "Reset %s branch to here" $mainhead] -state normal
5e3502da
MB
8449 } else {
8450 $menu entryconfigure 7 -label [mc "Detached head: can't reset" $mainhead] -state disabled
8451 }
b9fdba7f
PM
8452 if {[info exists markedid] && $markedid ne $id} {
8453 $menu entryconfigure 9 -state normal
8454 $menu entryconfigure 10 -state normal
010509f2 8455 $menu entryconfigure 11 -state normal
b9fdba7f
PM
8456 } else {
8457 $menu entryconfigure 9 -state disabled
8458 $menu entryconfigure 10 -state disabled
010509f2 8459 $menu entryconfigure 11 -state disabled
b9fdba7f 8460 }
219ea3a9
PM
8461 } else {
8462 set menu $fakerowmenu
8463 }
f2d0bbbd
PM
8464 $menu entryconfigure [mca "Diff this -> selected"] -state $state
8465 $menu entryconfigure [mca "Diff selected -> this"] -state $state
8466 $menu entryconfigure [mca "Make patch"] -state $state
219ea3a9 8467 tk_popup $menu $x $y
c8dfbcf9
PM
8468}
8469
b9fdba7f
PM
8470proc markhere {} {
8471 global rowmenuid markedid canv
8472
8473 set markedid $rowmenuid
8474 make_idmark $markedid
8475}
8476
8477proc gotomark {} {
8478 global markedid
8479
8480 if {[info exists markedid]} {
8481 selbyid $markedid
8482 }
8483}
8484
8485proc replace_by_kids {l r} {
8486 global curview children
8487
8488 set id [commitonrow $r]
8489 set l [lreplace $l 0 0]
8490 foreach kid $children($curview,$id) {
8491 lappend l [rowofcommit $kid]
8492 }
8493 return [lsort -integer -decreasing -unique $l]
8494}
8495
8496proc find_common_desc {} {
8497 global markedid rowmenuid curview children
8498
8499 if {![info exists markedid]} return
8500 if {![commitinview $markedid $curview] ||
8501 ![commitinview $rowmenuid $curview]} return
8502 #set t1 [clock clicks -milliseconds]
8503 set l1 [list [rowofcommit $markedid]]
8504 set l2 [list [rowofcommit $rowmenuid]]
8505 while 1 {
8506 set r1 [lindex $l1 0]
8507 set r2 [lindex $l2 0]
8508 if {$r1 eq {} || $r2 eq {}} break
8509 if {$r1 == $r2} {
8510 selectline $r1 1
8511 break
8512 }
8513 if {$r1 > $r2} {
8514 set l1 [replace_by_kids $l1 $r1]
8515 } else {
8516 set l2 [replace_by_kids $l2 $r2]
8517 }
8518 }
8519 #set t2 [clock clicks -milliseconds]
8520 #puts "took [expr {$t2-$t1}]ms"
8521}
8522
010509f2
PM
8523proc compare_commits {} {
8524 global markedid rowmenuid curview children
8525
8526 if {![info exists markedid]} return
8527 if {![commitinview $markedid $curview]} return
8528 addtohistory [list do_cmp_commits $markedid $rowmenuid]
8529 do_cmp_commits $markedid $rowmenuid
8530}
8531
8532proc getpatchid {id} {
8533 global patchids
8534
8535 if {![info exists patchids($id)]} {
6f63fc18
PM
8536 set cmd [diffcmd [list $id] {-p --root}]
8537 # trim off the initial "|"
8538 set cmd [lrange $cmd 1 end]
8539 if {[catch {
8540 set x [eval exec $cmd | git patch-id]
8541 set patchids($id) [lindex $x 0]
8542 }]} {
8543 set patchids($id) "error"
8544 }
010509f2
PM
8545 }
8546 return $patchids($id)
8547}
8548
8549proc do_cmp_commits {a b} {
8550 global ctext curview parents children patchids commitinfo
8551
8552 $ctext conf -state normal
8553 clear_ctext
8554 init_flist {}
8555 for {set i 0} {$i < 100} {incr i} {
010509f2
PM
8556 set skipa 0
8557 set skipb 0
8558 if {[llength $parents($curview,$a)] > 1} {
6f63fc18 8559 appendshortlink $a [mc "Skipping merge commit "] "\n"
010509f2
PM
8560 set skipa 1
8561 } else {
8562 set patcha [getpatchid $a]
8563 }
8564 if {[llength $parents($curview,$b)] > 1} {
6f63fc18 8565 appendshortlink $b [mc "Skipping merge commit "] "\n"
010509f2
PM
8566 set skipb 1
8567 } else {
8568 set patchb [getpatchid $b]
8569 }
8570 if {!$skipa && !$skipb} {
8571 set heada [lindex $commitinfo($a) 0]
8572 set headb [lindex $commitinfo($b) 0]
6f63fc18
PM
8573 if {$patcha eq "error"} {
8574 appendshortlink $a [mc "Error getting patch ID for "] \
8575 [mc " - stopping\n"]
8576 break
8577 }
8578 if {$patchb eq "error"} {
8579 appendshortlink $b [mc "Error getting patch ID for "] \
8580 [mc " - stopping\n"]
8581 break
8582 }
010509f2
PM
8583 if {$patcha eq $patchb} {
8584 if {$heada eq $headb} {
6f63fc18
PM
8585 appendshortlink $a [mc "Commit "]
8586 appendshortlink $b " == " " $heada\n"
010509f2 8587 } else {
6f63fc18
PM
8588 appendshortlink $a [mc "Commit "] " $heada\n"
8589 appendshortlink $b [mc " is the same patch as\n "] \
8590 " $headb\n"
010509f2
PM
8591 }
8592 set skipa 1
8593 set skipb 1
8594 } else {
8595 $ctext insert end "\n"
6f63fc18
PM
8596 appendshortlink $a [mc "Commit "] " $heada\n"
8597 appendshortlink $b [mc " differs from\n "] \
8598 " $headb\n"
c21398be
PM
8599 $ctext insert end [mc "Diff of commits:\n\n"]
8600 $ctext conf -state disabled
8601 update
8602 diffcommits $a $b
8603 return
010509f2
PM
8604 }
8605 }
8606 if {$skipa} {
aa43561a
PM
8607 set kids [real_children $curview,$a]
8608 if {[llength $kids] != 1} {
010509f2 8609 $ctext insert end "\n"
6f63fc18 8610 appendshortlink $a [mc "Commit "] \
aa43561a 8611 [mc " has %s children - stopping\n" [llength $kids]]
010509f2
PM
8612 break
8613 }
aa43561a 8614 set a [lindex $kids 0]
010509f2
PM
8615 }
8616 if {$skipb} {
aa43561a
PM
8617 set kids [real_children $curview,$b]
8618 if {[llength $kids] != 1} {
6f63fc18 8619 appendshortlink $b [mc "Commit "] \
aa43561a 8620 [mc " has %s children - stopping\n" [llength $kids]]
010509f2
PM
8621 break
8622 }
aa43561a 8623 set b [lindex $kids 0]
010509f2
PM
8624 }
8625 }
8626 $ctext conf -state disabled
8627}
8628
c21398be 8629proc diffcommits {a b} {
a1d383c5 8630 global diffcontext diffids blobdifffd diffinhdr currdiffsubmod
c21398be
PM
8631
8632 set tmpdir [gitknewtmpdir]
8633 set fna [file join $tmpdir "commit-[string range $a 0 7]"]
8634 set fnb [file join $tmpdir "commit-[string range $b 0 7]"]
8635 if {[catch {
8636 exec git diff-tree -p --pretty $a >$fna
8637 exec git diff-tree -p --pretty $b >$fnb
8638 } err]} {
8639 error_popup [mc "Error writing commit to file: %s" $err]
8640 return
8641 }
8642 if {[catch {
8643 set fd [open "| diff -U$diffcontext $fna $fnb" r]
8644 } err]} {
8645 error_popup [mc "Error diffing commits: %s" $err]
8646 return
8647 }
8648 set diffids [list commits $a $b]
8649 set blobdifffd($diffids) $fd
8650 set diffinhdr 0
a1d383c5 8651 set currdiffsubmod ""
c21398be
PM
8652 filerun $fd [list getblobdiffline $fd $diffids]
8653}
8654
c8dfbcf9 8655proc diffvssel {dirn} {
7fcc92bf 8656 global rowmenuid selectedline
c8dfbcf9 8657
94b4a69f 8658 if {$selectedline eq {}} return
c8dfbcf9 8659 if {$dirn} {
7fcc92bf 8660 set oldid [commitonrow $selectedline]
c8dfbcf9
PM
8661 set newid $rowmenuid
8662 } else {
8663 set oldid $rowmenuid
7fcc92bf 8664 set newid [commitonrow $selectedline]
c8dfbcf9 8665 }
354af6bd 8666 addtohistory [list doseldiff $oldid $newid] savectextpos
fa4da7b3
PM
8667 doseldiff $oldid $newid
8668}
8669
8670proc doseldiff {oldid newid} {
7fcceed7 8671 global ctext
fa4da7b3
PM
8672 global commitinfo
8673
c8dfbcf9 8674 $ctext conf -state normal
3ea06f9f 8675 clear_ctext
d990cedf
CS
8676 init_flist [mc "Top"]
8677 $ctext insert end "[mc "From"] "
97645683
PM
8678 $ctext insert end $oldid link0
8679 setlink $oldid link0
fa4da7b3 8680 $ctext insert end "\n "
c8dfbcf9 8681 $ctext insert end [lindex $commitinfo($oldid) 0]
d990cedf 8682 $ctext insert end "\n\n[mc "To"] "
97645683
PM
8683 $ctext insert end $newid link1
8684 setlink $newid link1
fa4da7b3 8685 $ctext insert end "\n "
c8dfbcf9
PM
8686 $ctext insert end [lindex $commitinfo($newid) 0]
8687 $ctext insert end "\n"
8688 $ctext conf -state disabled
c8dfbcf9 8689 $ctext tag remove found 1.0 end
d327244a 8690 startdiff [list $oldid $newid]
c8dfbcf9
PM
8691}
8692
74daedb6 8693proc mkpatch {} {
d93f1713 8694 global rowmenuid currentid commitinfo patchtop patchnum NS
74daedb6
PM
8695
8696 if {![info exists currentid]} return
8697 set oldid $currentid
8698 set oldhead [lindex $commitinfo($oldid) 0]
8699 set newid $rowmenuid
8700 set newhead [lindex $commitinfo($newid) 0]
8701 set top .patch
8702 set patchtop $top
8703 catch {destroy $top}
d93f1713 8704 ttk_toplevel $top
e7d64008 8705 make_transient $top .
d93f1713 8706 ${NS}::label $top.title -text [mc "Generate patch"]
4a2139f5 8707 grid $top.title - -pady 10
d93f1713
PT
8708 ${NS}::label $top.from -text [mc "From:"]
8709 ${NS}::entry $top.fromsha1 -width 40
74daedb6
PM
8710 $top.fromsha1 insert 0 $oldid
8711 $top.fromsha1 conf -state readonly
8712 grid $top.from $top.fromsha1 -sticky w
d93f1713 8713 ${NS}::entry $top.fromhead -width 60
74daedb6
PM
8714 $top.fromhead insert 0 $oldhead
8715 $top.fromhead conf -state readonly
8716 grid x $top.fromhead -sticky w
d93f1713
PT
8717 ${NS}::label $top.to -text [mc "To:"]
8718 ${NS}::entry $top.tosha1 -width 40
74daedb6
PM
8719 $top.tosha1 insert 0 $newid
8720 $top.tosha1 conf -state readonly
8721 grid $top.to $top.tosha1 -sticky w
d93f1713 8722 ${NS}::entry $top.tohead -width 60
74daedb6
PM
8723 $top.tohead insert 0 $newhead
8724 $top.tohead conf -state readonly
8725 grid x $top.tohead -sticky w
d93f1713
PT
8726 ${NS}::button $top.rev -text [mc "Reverse"] -command mkpatchrev
8727 grid $top.rev x -pady 10 -padx 5
8728 ${NS}::label $top.flab -text [mc "Output file:"]
8729 ${NS}::entry $top.fname -width 60
74daedb6
PM
8730 $top.fname insert 0 [file normalize "patch$patchnum.patch"]
8731 incr patchnum
bdbfbe3d 8732 grid $top.flab $top.fname -sticky w
d93f1713
PT
8733 ${NS}::frame $top.buts
8734 ${NS}::button $top.buts.gen -text [mc "Generate"] -command mkpatchgo
8735 ${NS}::button $top.buts.can -text [mc "Cancel"] -command mkpatchcan
76f15947
AG
8736 bind $top <Key-Return> mkpatchgo
8737 bind $top <Key-Escape> mkpatchcan
74daedb6
PM
8738 grid $top.buts.gen $top.buts.can
8739 grid columnconfigure $top.buts 0 -weight 1 -uniform a
8740 grid columnconfigure $top.buts 1 -weight 1 -uniform a
8741 grid $top.buts - -pady 10 -sticky ew
bdbfbe3d 8742 focus $top.fname
74daedb6
PM
8743}
8744
8745proc mkpatchrev {} {
8746 global patchtop
8747
8748 set oldid [$patchtop.fromsha1 get]
8749 set oldhead [$patchtop.fromhead get]
8750 set newid [$patchtop.tosha1 get]
8751 set newhead [$patchtop.tohead get]
8752 foreach e [list fromsha1 fromhead tosha1 tohead] \
8753 v [list $newid $newhead $oldid $oldhead] {
8754 $patchtop.$e conf -state normal
8755 $patchtop.$e delete 0 end
8756 $patchtop.$e insert 0 $v
8757 $patchtop.$e conf -state readonly
8758 }
8759}
8760
8761proc mkpatchgo {} {
8f489363 8762 global patchtop nullid nullid2
74daedb6
PM
8763
8764 set oldid [$patchtop.fromsha1 get]
8765 set newid [$patchtop.tosha1 get]
8766 set fname [$patchtop.fname get]
8f489363 8767 set cmd [diffcmd [list $oldid $newid] -p]
d372e216
PM
8768 # trim off the initial "|"
8769 set cmd [lrange $cmd 1 end]
219ea3a9
PM
8770 lappend cmd >$fname &
8771 if {[catch {eval exec $cmd} err]} {
84a76f18 8772 error_popup "[mc "Error creating patch:"] $err" $patchtop
74daedb6
PM
8773 }
8774 catch {destroy $patchtop}
8775 unset patchtop
8776}
8777
8778proc mkpatchcan {} {
8779 global patchtop
8780
8781 catch {destroy $patchtop}
8782 unset patchtop
8783}
8784
bdbfbe3d 8785proc mktag {} {
d93f1713 8786 global rowmenuid mktagtop commitinfo NS
bdbfbe3d
PM
8787
8788 set top .maketag
8789 set mktagtop $top
8790 catch {destroy $top}
d93f1713 8791 ttk_toplevel $top
e7d64008 8792 make_transient $top .
d93f1713 8793 ${NS}::label $top.title -text [mc "Create tag"]
4a2139f5 8794 grid $top.title - -pady 10
d93f1713
PT
8795 ${NS}::label $top.id -text [mc "ID:"]
8796 ${NS}::entry $top.sha1 -width 40
bdbfbe3d
PM
8797 $top.sha1 insert 0 $rowmenuid
8798 $top.sha1 conf -state readonly
8799 grid $top.id $top.sha1 -sticky w
d93f1713 8800 ${NS}::entry $top.head -width 60
bdbfbe3d
PM
8801 $top.head insert 0 [lindex $commitinfo($rowmenuid) 0]
8802 $top.head conf -state readonly
8803 grid x $top.head -sticky w
d93f1713
PT
8804 ${NS}::label $top.tlab -text [mc "Tag name:"]
8805 ${NS}::entry $top.tag -width 60
bdbfbe3d 8806 grid $top.tlab $top.tag -sticky w
dfb891e3
DD
8807 ${NS}::label $top.op -text [mc "Tag message is optional"]
8808 grid $top.op -columnspan 2 -sticky we
8809 ${NS}::label $top.mlab -text [mc "Tag message:"]
8810 ${NS}::entry $top.msg -width 60
8811 grid $top.mlab $top.msg -sticky w
d93f1713
PT
8812 ${NS}::frame $top.buts
8813 ${NS}::button $top.buts.gen -text [mc "Create"] -command mktaggo
8814 ${NS}::button $top.buts.can -text [mc "Cancel"] -command mktagcan
76f15947
AG
8815 bind $top <Key-Return> mktaggo
8816 bind $top <Key-Escape> mktagcan
bdbfbe3d
PM
8817 grid $top.buts.gen $top.buts.can
8818 grid columnconfigure $top.buts 0 -weight 1 -uniform a
8819 grid columnconfigure $top.buts 1 -weight 1 -uniform a
8820 grid $top.buts - -pady 10 -sticky ew
8821 focus $top.tag
8822}
8823
8824proc domktag {} {
8825 global mktagtop env tagids idtags
bdbfbe3d
PM
8826
8827 set id [$mktagtop.sha1 get]
8828 set tag [$mktagtop.tag get]
dfb891e3 8829 set msg [$mktagtop.msg get]
bdbfbe3d 8830 if {$tag == {}} {
84a76f18
AG
8831 error_popup [mc "No tag name specified"] $mktagtop
8832 return 0
bdbfbe3d
PM
8833 }
8834 if {[info exists tagids($tag)]} {
84a76f18
AG
8835 error_popup [mc "Tag \"%s\" already exists" $tag] $mktagtop
8836 return 0
bdbfbe3d
PM
8837 }
8838 if {[catch {
dfb891e3
DD
8839 if {$msg != {}} {
8840 exec git tag -a -m $msg $tag $id
8841 } else {
8842 exec git tag $tag $id
8843 }
bdbfbe3d 8844 } err]} {
84a76f18
AG
8845 error_popup "[mc "Error creating tag:"] $err" $mktagtop
8846 return 0
bdbfbe3d
PM
8847 }
8848
8849 set tagids($tag) $id
8850 lappend idtags($id) $tag
f1d83ba3 8851 redrawtags $id
ceadfe90 8852 addedtag $id
887c996e
PM
8853 dispneartags 0
8854 run refill_reflist
84a76f18 8855 return 1
f1d83ba3
PM
8856}
8857
8858proc redrawtags {id} {
b9fdba7f 8859 global canv linehtag idpos currentid curview cmitlisted markedid
c11ff120 8860 global canvxmax iddrawn circleitem mainheadid circlecolors
f1d83ba3 8861
7fcc92bf 8862 if {![commitinview $id $curview]} return
322a8cc9 8863 if {![info exists iddrawn($id)]} return
fc2a256f 8864 set row [rowofcommit $id]
c11ff120
PM
8865 if {$id eq $mainheadid} {
8866 set ofill yellow
8867 } else {
8868 set ofill [lindex $circlecolors $cmitlisted($curview,$id)]
8869 }
8870 $canv itemconf $circleitem($row) -fill $ofill
bdbfbe3d
PM
8871 $canv delete tag.$id
8872 set xt [eval drawtags $id $idpos($id)]
28593d3f
PM
8873 $canv coords $linehtag($id) $xt [lindex $idpos($id) 2]
8874 set text [$canv itemcget $linehtag($id) -text]
8875 set font [$canv itemcget $linehtag($id) -font]
fc2a256f 8876 set xr [expr {$xt + [font measure $font $text]}]
b8ab2e17
PM
8877 if {$xr > $canvxmax} {
8878 set canvxmax $xr
8879 setcanvscroll
8880 }
fc2a256f 8881 if {[info exists currentid] && $currentid == $id} {
28593d3f 8882 make_secsel $id
bdbfbe3d 8883 }
b9fdba7f
PM
8884 if {[info exists markedid] && $markedid eq $id} {
8885 make_idmark $id
8886 }
bdbfbe3d
PM
8887}
8888
8889proc mktagcan {} {
8890 global mktagtop
8891
8892 catch {destroy $mktagtop}
8893 unset mktagtop
8894}
8895
8896proc mktaggo {} {
84a76f18 8897 if {![domktag]} return
bdbfbe3d
PM
8898 mktagcan
8899}
8900
4a2139f5 8901proc writecommit {} {
d93f1713 8902 global rowmenuid wrcomtop commitinfo wrcomcmd NS
4a2139f5
PM
8903
8904 set top .writecommit
8905 set wrcomtop $top
8906 catch {destroy $top}
d93f1713 8907 ttk_toplevel $top
e7d64008 8908 make_transient $top .
d93f1713 8909 ${NS}::label $top.title -text [mc "Write commit to file"]
4a2139f5 8910 grid $top.title - -pady 10
d93f1713
PT
8911 ${NS}::label $top.id -text [mc "ID:"]
8912 ${NS}::entry $top.sha1 -width 40
4a2139f5
PM
8913 $top.sha1 insert 0 $rowmenuid
8914 $top.sha1 conf -state readonly
8915 grid $top.id $top.sha1 -sticky w
d93f1713 8916 ${NS}::entry $top.head -width 60
4a2139f5
PM
8917 $top.head insert 0 [lindex $commitinfo($rowmenuid) 0]
8918 $top.head conf -state readonly
8919 grid x $top.head -sticky w
d93f1713
PT
8920 ${NS}::label $top.clab -text [mc "Command:"]
8921 ${NS}::entry $top.cmd -width 60 -textvariable wrcomcmd
4a2139f5 8922 grid $top.clab $top.cmd -sticky w -pady 10
d93f1713
PT
8923 ${NS}::label $top.flab -text [mc "Output file:"]
8924 ${NS}::entry $top.fname -width 60
4a2139f5
PM
8925 $top.fname insert 0 [file normalize "commit-[string range $rowmenuid 0 6]"]
8926 grid $top.flab $top.fname -sticky w
d93f1713
PT
8927 ${NS}::frame $top.buts
8928 ${NS}::button $top.buts.gen -text [mc "Write"] -command wrcomgo
8929 ${NS}::button $top.buts.can -text [mc "Cancel"] -command wrcomcan
76f15947
AG
8930 bind $top <Key-Return> wrcomgo
8931 bind $top <Key-Escape> wrcomcan
4a2139f5
PM
8932 grid $top.buts.gen $top.buts.can
8933 grid columnconfigure $top.buts 0 -weight 1 -uniform a
8934 grid columnconfigure $top.buts 1 -weight 1 -uniform a
8935 grid $top.buts - -pady 10 -sticky ew
8936 focus $top.fname
8937}
8938
8939proc wrcomgo {} {
8940 global wrcomtop
8941
8942 set id [$wrcomtop.sha1 get]
8943 set cmd "echo $id | [$wrcomtop.cmd get]"
8944 set fname [$wrcomtop.fname get]
8945 if {[catch {exec sh -c $cmd >$fname &} err]} {
84a76f18 8946 error_popup "[mc "Error writing commit:"] $err" $wrcomtop
4a2139f5
PM
8947 }
8948 catch {destroy $wrcomtop}
8949 unset wrcomtop
8950}
8951
8952proc wrcomcan {} {
8953 global wrcomtop
8954
8955 catch {destroy $wrcomtop}
8956 unset wrcomtop
8957}
8958
d6ac1a86 8959proc mkbranch {} {
d93f1713 8960 global rowmenuid mkbrtop NS
d6ac1a86
PM
8961
8962 set top .makebranch
8963 catch {destroy $top}
d93f1713 8964 ttk_toplevel $top
e7d64008 8965 make_transient $top .
d93f1713 8966 ${NS}::label $top.title -text [mc "Create new branch"]
d6ac1a86 8967 grid $top.title - -pady 10
d93f1713
PT
8968 ${NS}::label $top.id -text [mc "ID:"]
8969 ${NS}::entry $top.sha1 -width 40
d6ac1a86
PM
8970 $top.sha1 insert 0 $rowmenuid
8971 $top.sha1 conf -state readonly
8972 grid $top.id $top.sha1 -sticky w
d93f1713
PT
8973 ${NS}::label $top.nlab -text [mc "Name:"]
8974 ${NS}::entry $top.name -width 40
d6ac1a86 8975 grid $top.nlab $top.name -sticky w
d93f1713
PT
8976 ${NS}::frame $top.buts
8977 ${NS}::button $top.buts.go -text [mc "Create"] -command [list mkbrgo $top]
8978 ${NS}::button $top.buts.can -text [mc "Cancel"] -command "catch {destroy $top}"
76f15947
AG
8979 bind $top <Key-Return> [list mkbrgo $top]
8980 bind $top <Key-Escape> "catch {destroy $top}"
d6ac1a86
PM
8981 grid $top.buts.go $top.buts.can
8982 grid columnconfigure $top.buts 0 -weight 1 -uniform a
8983 grid columnconfigure $top.buts 1 -weight 1 -uniform a
8984 grid $top.buts - -pady 10 -sticky ew
8985 focus $top.name
8986}
8987
8988proc mkbrgo {top} {
8989 global headids idheads
8990
8991 set name [$top.name get]
8992 set id [$top.sha1 get]
bee866fa
AG
8993 set cmdargs {}
8994 set old_id {}
d6ac1a86 8995 if {$name eq {}} {
84a76f18 8996 error_popup [mc "Please specify a name for the new branch"] $top
d6ac1a86
PM
8997 return
8998 }
bee866fa
AG
8999 if {[info exists headids($name)]} {
9000 if {![confirm_popup [mc \
84a76f18 9001 "Branch '%s' already exists. Overwrite?" $name] $top]} {
bee866fa
AG
9002 return
9003 }
9004 set old_id $headids($name)
9005 lappend cmdargs -f
9006 }
d6ac1a86 9007 catch {destroy $top}
bee866fa 9008 lappend cmdargs $name $id
d6ac1a86
PM
9009 nowbusy newbranch
9010 update
9011 if {[catch {
bee866fa 9012 eval exec git branch $cmdargs
d6ac1a86
PM
9013 } err]} {
9014 notbusy newbranch
9015 error_popup $err
9016 } else {
d6ac1a86 9017 notbusy newbranch
bee866fa
AG
9018 if {$old_id ne {}} {
9019 movehead $id $name
9020 movedhead $id $name
9021 redrawtags $old_id
9022 redrawtags $id
9023 } else {
9024 set headids($name) $id
9025 lappend idheads($id) $name
9026 addedhead $id $name
9027 redrawtags $id
9028 }
e11f1233 9029 dispneartags 0
887c996e 9030 run refill_reflist
d6ac1a86
PM
9031 }
9032}
9033
15e35055
AG
9034proc exec_citool {tool_args {baseid {}}} {
9035 global commitinfo env
9036
9037 set save_env [array get env GIT_AUTHOR_*]
9038
9039 if {$baseid ne {}} {
9040 if {![info exists commitinfo($baseid)]} {
9041 getcommit $baseid
9042 }
9043 set author [lindex $commitinfo($baseid) 1]
9044 set date [lindex $commitinfo($baseid) 2]
9045 if {[regexp {^\s*(\S.*\S|\S)\s*<(.*)>\s*$} \
9046 $author author name email]
9047 && $date ne {}} {
9048 set env(GIT_AUTHOR_NAME) $name
9049 set env(GIT_AUTHOR_EMAIL) $email
9050 set env(GIT_AUTHOR_DATE) $date
9051 }
9052 }
9053
9054 eval exec git citool $tool_args &
9055
9056 array unset env GIT_AUTHOR_*
9057 array set env $save_env
9058}
9059
ca6d8f58 9060proc cherrypick {} {
468bcaed 9061 global rowmenuid curview
b8a938cf 9062 global mainhead mainheadid
da616db5 9063 global gitdir
ca6d8f58 9064
e11f1233
PM
9065 set oldhead [exec git rev-parse HEAD]
9066 set dheads [descheads $rowmenuid]
9067 if {$dheads ne {} && [lsearch -exact $dheads $oldhead] >= 0} {
d990cedf
CS
9068 set ok [confirm_popup [mc "Commit %s is already\
9069 included in branch %s -- really re-apply it?" \
9070 [string range $rowmenuid 0 7] $mainhead]]
ca6d8f58
PM
9071 if {!$ok} return
9072 }
d990cedf 9073 nowbusy cherrypick [mc "Cherry-picking"]
ca6d8f58 9074 update
ca6d8f58
PM
9075 # Unfortunately git-cherry-pick writes stuff to stderr even when
9076 # no error occurs, and exec takes that as an indication of error...
9077 if {[catch {exec sh -c "git cherry-pick -r $rowmenuid 2>&1"} err]} {
9078 notbusy cherrypick
15e35055 9079 if {[regexp -line \
887a791f
PM
9080 {Entry '(.*)' (would be overwritten by merge|not uptodate)} \
9081 $err msg fname]} {
9082 error_popup [mc "Cherry-pick failed because of local changes\
9083 to file '%s'.\nPlease commit, reset or stash\
9084 your changes and try again." $fname]
9085 } elseif {[regexp -line \
b74307f6 9086 {^(CONFLICT \(.*\):|Automatic cherry-pick failed|error: could not apply)} \
887a791f
PM
9087 $err]} {
9088 if {[confirm_popup [mc "Cherry-pick failed because of merge\
9089 conflict.\nDo you wish to run git citool to\
9090 resolve it?"]]} {
9091 # Force citool to read MERGE_MSG
da616db5 9092 file delete [file join $gitdir "GITGUI_MSG"]
887a791f
PM
9093 exec_citool {} $rowmenuid
9094 }
15e35055
AG
9095 } else {
9096 error_popup $err
9097 }
887a791f 9098 run updatecommits
ca6d8f58
PM
9099 return
9100 }
9101 set newhead [exec git rev-parse HEAD]
9102 if {$newhead eq $oldhead} {
9103 notbusy cherrypick
d990cedf 9104 error_popup [mc "No changes committed"]
ca6d8f58
PM
9105 return
9106 }
e11f1233 9107 addnewchild $newhead $oldhead
7fcc92bf 9108 if {[commitinview $oldhead $curview]} {
cdc8429c 9109 # XXX this isn't right if we have a path limit...
7fcc92bf 9110 insertrow $newhead $oldhead $curview
ca6d8f58 9111 if {$mainhead ne {}} {
e11f1233 9112 movehead $newhead $mainhead
ca6d8f58
PM
9113 movedhead $newhead $mainhead
9114 }
c11ff120 9115 set mainheadid $newhead
ca6d8f58
PM
9116 redrawtags $oldhead
9117 redrawtags $newhead
46308ea1 9118 selbyid $newhead
ca6d8f58
PM
9119 }
9120 notbusy cherrypick
9121}
9122
6fb735ae 9123proc resethead {} {
d93f1713 9124 global mainhead rowmenuid confirm_ok resettype NS
6fb735ae
PM
9125
9126 set confirm_ok 0
9127 set w ".confirmreset"
d93f1713 9128 ttk_toplevel $w
e7d64008 9129 make_transient $w .
d990cedf 9130 wm title $w [mc "Confirm reset"]
d93f1713
PT
9131 ${NS}::label $w.m -text \
9132 [mc "Reset branch %s to %s?" $mainhead [string range $rowmenuid 0 7]]
6fb735ae 9133 pack $w.m -side top -fill x -padx 20 -pady 20
d93f1713 9134 ${NS}::labelframe $w.f -text [mc "Reset type:"]
6fb735ae 9135 set resettype mixed
d93f1713 9136 ${NS}::radiobutton $w.f.soft -value soft -variable resettype \
d990cedf 9137 -text [mc "Soft: Leave working tree and index untouched"]
6fb735ae 9138 grid $w.f.soft -sticky w
d93f1713 9139 ${NS}::radiobutton $w.f.mixed -value mixed -variable resettype \
d990cedf 9140 -text [mc "Mixed: Leave working tree untouched, reset index"]
6fb735ae 9141 grid $w.f.mixed -sticky w
d93f1713 9142 ${NS}::radiobutton $w.f.hard -value hard -variable resettype \
d990cedf 9143 -text [mc "Hard: Reset working tree and index\n(discard ALL local changes)"]
6fb735ae 9144 grid $w.f.hard -sticky w
d93f1713
PT
9145 pack $w.f -side top -fill x -padx 4
9146 ${NS}::button $w.ok -text [mc OK] -command "set confirm_ok 1; destroy $w"
6fb735ae 9147 pack $w.ok -side left -fill x -padx 20 -pady 20
d93f1713 9148 ${NS}::button $w.cancel -text [mc Cancel] -command "destroy $w"
76f15947 9149 bind $w <Key-Escape> [list destroy $w]
6fb735ae
PM
9150 pack $w.cancel -side right -fill x -padx 20 -pady 20
9151 bind $w <Visibility> "grab $w; focus $w"
9152 tkwait window $w
9153 if {!$confirm_ok} return
706d6c3e 9154 if {[catch {set fd [open \
08ba820f 9155 [list | git reset --$resettype $rowmenuid 2>@1] r]} err]} {
6fb735ae
PM
9156 error_popup $err
9157 } else {
706d6c3e 9158 dohidelocalchanges
a137a90f 9159 filerun $fd [list readresetstat $fd]
d990cedf 9160 nowbusy reset [mc "Resetting"]
46308ea1 9161 selbyid $rowmenuid
706d6c3e
PM
9162 }
9163}
9164
a137a90f
PM
9165proc readresetstat {fd} {
9166 global mainhead mainheadid showlocalchanges rprogcoord
706d6c3e
PM
9167
9168 if {[gets $fd line] >= 0} {
9169 if {[regexp {([0-9]+)% \(([0-9]+)/([0-9]+)\)} $line match p m n]} {
a137a90f
PM
9170 set rprogcoord [expr {1.0 * $m / $n}]
9171 adjustprogress
706d6c3e
PM
9172 }
9173 return 1
9174 }
a137a90f
PM
9175 set rprogcoord 0
9176 adjustprogress
706d6c3e
PM
9177 notbusy reset
9178 if {[catch {close $fd} err]} {
9179 error_popup $err
9180 }
9181 set oldhead $mainheadid
9182 set newhead [exec git rev-parse HEAD]
9183 if {$newhead ne $oldhead} {
9184 movehead $newhead $mainhead
9185 movedhead $newhead $mainhead
9186 set mainheadid $newhead
6fb735ae 9187 redrawtags $oldhead
706d6c3e 9188 redrawtags $newhead
6fb735ae
PM
9189 }
9190 if {$showlocalchanges} {
9191 doshowlocalchanges
9192 }
706d6c3e 9193 return 0
6fb735ae
PM
9194}
9195
10299152
PM
9196# context menu for a head
9197proc headmenu {x y id head} {
00609463 9198 global headmenuid headmenuhead headctxmenu mainhead
10299152 9199
bb3edc8b 9200 stopfinding
10299152
PM
9201 set headmenuid $id
9202 set headmenuhead $head
00609463 9203 set state normal
70a5fc44
SC
9204 if {[string match "remotes/*" $head]} {
9205 set state disabled
9206 }
00609463
PM
9207 if {$head eq $mainhead} {
9208 set state disabled
9209 }
9210 $headctxmenu entryconfigure 0 -state $state
9211 $headctxmenu entryconfigure 1 -state $state
10299152
PM
9212 tk_popup $headctxmenu $x $y
9213}
9214
9215proc cobranch {} {
c11ff120 9216 global headmenuid headmenuhead headids
cdc8429c 9217 global showlocalchanges
10299152
PM
9218
9219 # check the tree is clean first??
d990cedf 9220 nowbusy checkout [mc "Checking out"]
10299152 9221 update
219ea3a9 9222 dohidelocalchanges
10299152 9223 if {[catch {
08ba820f 9224 set fd [open [list | git checkout $headmenuhead 2>@1] r]
10299152
PM
9225 } err]} {
9226 notbusy checkout
9227 error_popup $err
08ba820f
PM
9228 if {$showlocalchanges} {
9229 dodiffindex
9230 }
10299152 9231 } else {
08ba820f
PM
9232 filerun $fd [list readcheckoutstat $fd $headmenuhead $headmenuid]
9233 }
9234}
9235
9236proc readcheckoutstat {fd newhead newheadid} {
9237 global mainhead mainheadid headids showlocalchanges progresscoords
cdc8429c 9238 global viewmainheadid curview
08ba820f
PM
9239
9240 if {[gets $fd line] >= 0} {
9241 if {[regexp {([0-9]+)% \(([0-9]+)/([0-9]+)\)} $line match p m n]} {
9242 set progresscoords [list 0 [expr {1.0 * $m / $n}]]
9243 adjustprogress
10299152 9244 }
08ba820f
PM
9245 return 1
9246 }
9247 set progresscoords {0 0}
9248 adjustprogress
9249 notbusy checkout
9250 if {[catch {close $fd} err]} {
9251 error_popup $err
9252 }
c11ff120 9253 set oldmainid $mainheadid
08ba820f
PM
9254 set mainhead $newhead
9255 set mainheadid $newheadid
cdc8429c 9256 set viewmainheadid($curview) $newheadid
c11ff120 9257 redrawtags $oldmainid
08ba820f
PM
9258 redrawtags $newheadid
9259 selbyid $newheadid
6fb735ae
PM
9260 if {$showlocalchanges} {
9261 dodiffindex
10299152
PM
9262 }
9263}
9264
9265proc rmbranch {} {
e11f1233 9266 global headmenuid headmenuhead mainhead
b1054ac9 9267 global idheads
10299152
PM
9268
9269 set head $headmenuhead
9270 set id $headmenuid
00609463 9271 # this check shouldn't be needed any more...
10299152 9272 if {$head eq $mainhead} {
d990cedf 9273 error_popup [mc "Cannot delete the currently checked-out branch"]
10299152
PM
9274 return
9275 }
e11f1233 9276 set dheads [descheads $id]
d7b16113 9277 if {[llength $dheads] == 1 && $idheads($dheads) eq $head} {
10299152 9278 # the stuff on this branch isn't on any other branch
d990cedf
CS
9279 if {![confirm_popup [mc "The commits on branch %s aren't on any other\
9280 branch.\nReally delete branch %s?" $head $head]]} return
10299152
PM
9281 }
9282 nowbusy rmbranch
9283 update
9284 if {[catch {exec git branch -D $head} err]} {
9285 notbusy rmbranch
9286 error_popup $err
9287 return
9288 }
e11f1233 9289 removehead $id $head
ca6d8f58 9290 removedhead $id $head
10299152
PM
9291 redrawtags $id
9292 notbusy rmbranch
e11f1233 9293 dispneartags 0
887c996e
PM
9294 run refill_reflist
9295}
9296
9297# Display a list of tags and heads
9298proc showrefs {} {
d93f1713 9299 global showrefstop bgcolor fgcolor selectbgcolor NS
9c311b32 9300 global bglist fglist reflistfilter reflist maincursor
887c996e
PM
9301
9302 set top .showrefs
9303 set showrefstop $top
9304 if {[winfo exists $top]} {
9305 raise $top
9306 refill_reflist
9307 return
9308 }
d93f1713 9309 ttk_toplevel $top
d990cedf 9310 wm title $top [mc "Tags and heads: %s" [file tail [pwd]]]
e7d64008 9311 make_transient $top .
887c996e 9312 text $top.list -background $bgcolor -foreground $fgcolor \
9c311b32 9313 -selectbackground $selectbgcolor -font mainfont \
887c996e
PM
9314 -xscrollcommand "$top.xsb set" -yscrollcommand "$top.ysb set" \
9315 -width 30 -height 20 -cursor $maincursor \
9316 -spacing1 1 -spacing3 1 -state disabled
9317 $top.list tag configure highlight -background $selectbgcolor
9318 lappend bglist $top.list
9319 lappend fglist $top.list
d93f1713
PT
9320 ${NS}::scrollbar $top.ysb -command "$top.list yview" -orient vertical
9321 ${NS}::scrollbar $top.xsb -command "$top.list xview" -orient horizontal
887c996e
PM
9322 grid $top.list $top.ysb -sticky nsew
9323 grid $top.xsb x -sticky ew
d93f1713
PT
9324 ${NS}::frame $top.f
9325 ${NS}::label $top.f.l -text "[mc "Filter"]: "
9326 ${NS}::entry $top.f.e -width 20 -textvariable reflistfilter
887c996e
PM
9327 set reflistfilter "*"
9328 trace add variable reflistfilter write reflistfilter_change
9329 pack $top.f.e -side right -fill x -expand 1
9330 pack $top.f.l -side left
9331 grid $top.f - -sticky ew -pady 2
d93f1713 9332 ${NS}::button $top.close -command [list destroy $top] -text [mc "Close"]
76f15947 9333 bind $top <Key-Escape> [list destroy $top]
887c996e
PM
9334 grid $top.close -
9335 grid columnconfigure $top 0 -weight 1
9336 grid rowconfigure $top 0 -weight 1
9337 bind $top.list <1> {break}
9338 bind $top.list <B1-Motion> {break}
9339 bind $top.list <ButtonRelease-1> {sel_reflist %W %x %y; break}
9340 set reflist {}
9341 refill_reflist
9342}
9343
9344proc sel_reflist {w x y} {
9345 global showrefstop reflist headids tagids otherrefids
9346
9347 if {![winfo exists $showrefstop]} return
9348 set l [lindex [split [$w index "@$x,$y"] "."] 0]
9349 set ref [lindex $reflist [expr {$l-1}]]
9350 set n [lindex $ref 0]
9351 switch -- [lindex $ref 1] {
9352 "H" {selbyid $headids($n)}
9353 "T" {selbyid $tagids($n)}
9354 "o" {selbyid $otherrefids($n)}
9355 }
9356 $showrefstop.list tag add highlight $l.0 "$l.0 lineend"
9357}
9358
9359proc unsel_reflist {} {
9360 global showrefstop
9361
9362 if {![info exists showrefstop] || ![winfo exists $showrefstop]} return
9363 $showrefstop.list tag remove highlight 0.0 end
9364}
9365
9366proc reflistfilter_change {n1 n2 op} {
9367 global reflistfilter
9368
9369 after cancel refill_reflist
9370 after 200 refill_reflist
9371}
9372
9373proc refill_reflist {} {
9374 global reflist reflistfilter showrefstop headids tagids otherrefids
d375ef9b 9375 global curview
887c996e
PM
9376
9377 if {![info exists showrefstop] || ![winfo exists $showrefstop]} return
9378 set refs {}
9379 foreach n [array names headids] {
9380 if {[string match $reflistfilter $n]} {
7fcc92bf 9381 if {[commitinview $headids($n) $curview]} {
887c996e
PM
9382 lappend refs [list $n H]
9383 } else {
d375ef9b 9384 interestedin $headids($n) {run refill_reflist}
887c996e
PM
9385 }
9386 }
9387 }
9388 foreach n [array names tagids] {
9389 if {[string match $reflistfilter $n]} {
7fcc92bf 9390 if {[commitinview $tagids($n) $curview]} {
887c996e
PM
9391 lappend refs [list $n T]
9392 } else {
d375ef9b 9393 interestedin $tagids($n) {run refill_reflist}
887c996e
PM
9394 }
9395 }
9396 }
9397 foreach n [array names otherrefids] {
9398 if {[string match $reflistfilter $n]} {
7fcc92bf 9399 if {[commitinview $otherrefids($n) $curview]} {
887c996e
PM
9400 lappend refs [list $n o]
9401 } else {
d375ef9b 9402 interestedin $otherrefids($n) {run refill_reflist}
887c996e
PM
9403 }
9404 }
9405 }
9406 set refs [lsort -index 0 $refs]
9407 if {$refs eq $reflist} return
9408
9409 # Update the contents of $showrefstop.list according to the
9410 # differences between $reflist (old) and $refs (new)
9411 $showrefstop.list conf -state normal
9412 $showrefstop.list insert end "\n"
9413 set i 0
9414 set j 0
9415 while {$i < [llength $reflist] || $j < [llength $refs]} {
9416 if {$i < [llength $reflist]} {
9417 if {$j < [llength $refs]} {
9418 set cmp [string compare [lindex $reflist $i 0] \
9419 [lindex $refs $j 0]]
9420 if {$cmp == 0} {
9421 set cmp [string compare [lindex $reflist $i 1] \
9422 [lindex $refs $j 1]]
9423 }
9424 } else {
9425 set cmp -1
9426 }
9427 } else {
9428 set cmp 1
9429 }
9430 switch -- $cmp {
9431 -1 {
9432 $showrefstop.list delete "[expr {$j+1}].0" "[expr {$j+2}].0"
9433 incr i
9434 }
9435 0 {
9436 incr i
9437 incr j
9438 }
9439 1 {
9440 set l [expr {$j + 1}]
9441 $showrefstop.list image create $l.0 -align baseline \
9442 -image reficon-[lindex $refs $j 1] -padx 2
9443 $showrefstop.list insert $l.1 "[lindex $refs $j 0]\n"
9444 incr j
9445 }
9446 }
9447 }
9448 set reflist $refs
9449 # delete last newline
9450 $showrefstop.list delete end-2c end-1c
9451 $showrefstop.list conf -state disabled
10299152
PM
9452}
9453
b8ab2e17
PM
9454# Stuff for finding nearby tags
9455proc getallcommits {} {
5cd15b6b
PM
9456 global allcommits nextarc seeds allccache allcwait cachedarcs allcupdate
9457 global idheads idtags idotherrefs allparents tagobjid
da616db5 9458 global gitdir
f1d83ba3 9459
a69b2d1a 9460 if {![info exists allcommits]} {
a69b2d1a
PM
9461 set nextarc 0
9462 set allcommits 0
9463 set seeds {}
5cd15b6b
PM
9464 set allcwait 0
9465 set cachedarcs 0
da616db5 9466 set allccache [file join $gitdir "gitk.cache"]
5cd15b6b
PM
9467 if {![catch {
9468 set f [open $allccache r]
9469 set allcwait 1
9470 getcache $f
9471 }]} return
a69b2d1a 9472 }
2d71bccc 9473
5cd15b6b
PM
9474 if {$allcwait} {
9475 return
9476 }
9477 set cmd [list | git rev-list --parents]
9478 set allcupdate [expr {$seeds ne {}}]
9479 if {!$allcupdate} {
9480 set ids "--all"
9481 } else {
9482 set refs [concat [array names idheads] [array names idtags] \
9483 [array names idotherrefs]]
9484 set ids {}
9485 set tagobjs {}
9486 foreach name [array names tagobjid] {
9487 lappend tagobjs $tagobjid($name)
9488 }
9489 foreach id [lsort -unique $refs] {
9490 if {![info exists allparents($id)] &&
9491 [lsearch -exact $tagobjs $id] < 0} {
9492 lappend ids $id
9493 }
9494 }
9495 if {$ids ne {}} {
9496 foreach id $seeds {
9497 lappend ids "^$id"
9498 }
9499 }
9500 }
9501 if {$ids ne {}} {
9502 set fd [open [concat $cmd $ids] r]
9503 fconfigure $fd -blocking 0
9504 incr allcommits
9505 nowbusy allcommits
9506 filerun $fd [list getallclines $fd]
9507 } else {
9508 dispneartags 0
2d71bccc 9509 }
e11f1233
PM
9510}
9511
9512# Since most commits have 1 parent and 1 child, we group strings of
9513# such commits into "arcs" joining branch/merge points (BMPs), which
9514# are commits that either don't have 1 parent or don't have 1 child.
9515#
9516# arcnos(id) - incoming arcs for BMP, arc we're on for other nodes
9517# arcout(id) - outgoing arcs for BMP
9518# arcids(a) - list of IDs on arc including end but not start
9519# arcstart(a) - BMP ID at start of arc
9520# arcend(a) - BMP ID at end of arc
9521# growing(a) - arc a is still growing
9522# arctags(a) - IDs out of arcids (excluding end) that have tags
9523# archeads(a) - IDs out of arcids (excluding end) that have heads
9524# The start of an arc is at the descendent end, so "incoming" means
9525# coming from descendents, and "outgoing" means going towards ancestors.
9526
9527proc getallclines {fd} {
5cd15b6b 9528 global allparents allchildren idtags idheads nextarc
e11f1233 9529 global arcnos arcids arctags arcout arcend arcstart archeads growing
5cd15b6b 9530 global seeds allcommits cachedarcs allcupdate
d93f1713 9531
e11f1233 9532 set nid 0
7eb3cb9c 9533 while {[incr nid] <= 1000 && [gets $fd line] >= 0} {
e11f1233
PM
9534 set id [lindex $line 0]
9535 if {[info exists allparents($id)]} {
9536 # seen it already
9537 continue
9538 }
5cd15b6b 9539 set cachedarcs 0
e11f1233
PM
9540 set olds [lrange $line 1 end]
9541 set allparents($id) $olds
9542 if {![info exists allchildren($id)]} {
9543 set allchildren($id) {}
9544 set arcnos($id) {}
9545 lappend seeds $id
9546 } else {
9547 set a $arcnos($id)
9548 if {[llength $olds] == 1 && [llength $a] == 1} {
9549 lappend arcids($a) $id
9550 if {[info exists idtags($id)]} {
9551 lappend arctags($a) $id
b8ab2e17 9552 }
e11f1233
PM
9553 if {[info exists idheads($id)]} {
9554 lappend archeads($a) $id
9555 }
9556 if {[info exists allparents($olds)]} {
9557 # seen parent already
9558 if {![info exists arcout($olds)]} {
9559 splitarc $olds
9560 }
9561 lappend arcids($a) $olds
9562 set arcend($a) $olds
9563 unset growing($a)
9564 }
9565 lappend allchildren($olds) $id
9566 lappend arcnos($olds) $a
9567 continue
9568 }
9569 }
e11f1233
PM
9570 foreach a $arcnos($id) {
9571 lappend arcids($a) $id
9572 set arcend($a) $id
9573 unset growing($a)
9574 }
9575
9576 set ao {}
9577 foreach p $olds {
9578 lappend allchildren($p) $id
9579 set a [incr nextarc]
9580 set arcstart($a) $id
9581 set archeads($a) {}
9582 set arctags($a) {}
9583 set archeads($a) {}
9584 set arcids($a) {}
9585 lappend ao $a
9586 set growing($a) 1
9587 if {[info exists allparents($p)]} {
9588 # seen it already, may need to make a new branch
9589 if {![info exists arcout($p)]} {
9590 splitarc $p
9591 }
9592 lappend arcids($a) $p
9593 set arcend($a) $p
9594 unset growing($a)
9595 }
9596 lappend arcnos($p) $a
9597 }
9598 set arcout($id) $ao
f1d83ba3 9599 }
f3326b66
PM
9600 if {$nid > 0} {
9601 global cached_dheads cached_dtags cached_atags
9602 catch {unset cached_dheads}
9603 catch {unset cached_dtags}
9604 catch {unset cached_atags}
9605 }
7eb3cb9c
PM
9606 if {![eof $fd]} {
9607 return [expr {$nid >= 1000? 2: 1}]
9608 }
5cd15b6b
PM
9609 set cacheok 1
9610 if {[catch {
9611 fconfigure $fd -blocking 1
9612 close $fd
9613 } err]} {
9614 # got an error reading the list of commits
9615 # if we were updating, try rereading the whole thing again
9616 if {$allcupdate} {
9617 incr allcommits -1
9618 dropcache $err
9619 return
9620 }
d990cedf 9621 error_popup "[mc "Error reading commit topology information;\
5cd15b6b 9622 branch and preceding/following tag information\
d990cedf 9623 will be incomplete."]\n($err)"
5cd15b6b
PM
9624 set cacheok 0
9625 }
e11f1233
PM
9626 if {[incr allcommits -1] == 0} {
9627 notbusy allcommits
5cd15b6b
PM
9628 if {$cacheok} {
9629 run savecache
9630 }
e11f1233
PM
9631 }
9632 dispneartags 0
7eb3cb9c 9633 return 0
b8ab2e17
PM
9634}
9635
e11f1233
PM
9636proc recalcarc {a} {
9637 global arctags archeads arcids idtags idheads
b8ab2e17 9638
e11f1233
PM
9639 set at {}
9640 set ah {}
9641 foreach id [lrange $arcids($a) 0 end-1] {
9642 if {[info exists idtags($id)]} {
9643 lappend at $id
9644 }
9645 if {[info exists idheads($id)]} {
9646 lappend ah $id
b8ab2e17 9647 }
f1d83ba3 9648 }
e11f1233
PM
9649 set arctags($a) $at
9650 set archeads($a) $ah
b8ab2e17
PM
9651}
9652
e11f1233 9653proc splitarc {p} {
5cd15b6b 9654 global arcnos arcids nextarc arctags archeads idtags idheads
e11f1233 9655 global arcstart arcend arcout allparents growing
cec7bece 9656
e11f1233
PM
9657 set a $arcnos($p)
9658 if {[llength $a] != 1} {
9659 puts "oops splitarc called but [llength $a] arcs already"
9660 return
9661 }
9662 set a [lindex $a 0]
9663 set i [lsearch -exact $arcids($a) $p]
9664 if {$i < 0} {
9665 puts "oops splitarc $p not in arc $a"
9666 return
9667 }
9668 set na [incr nextarc]
9669 if {[info exists arcend($a)]} {
9670 set arcend($na) $arcend($a)
9671 } else {
9672 set l [lindex $allparents([lindex $arcids($a) end]) 0]
9673 set j [lsearch -exact $arcnos($l) $a]
9674 set arcnos($l) [lreplace $arcnos($l) $j $j $na]
9675 }
9676 set tail [lrange $arcids($a) [expr {$i+1}] end]
9677 set arcids($a) [lrange $arcids($a) 0 $i]
9678 set arcend($a) $p
9679 set arcstart($na) $p
9680 set arcout($p) $na
9681 set arcids($na) $tail
9682 if {[info exists growing($a)]} {
9683 set growing($na) 1
9684 unset growing($a)
9685 }
e11f1233
PM
9686
9687 foreach id $tail {
9688 if {[llength $arcnos($id)] == 1} {
9689 set arcnos($id) $na
cec7bece 9690 } else {
e11f1233
PM
9691 set j [lsearch -exact $arcnos($id) $a]
9692 set arcnos($id) [lreplace $arcnos($id) $j $j $na]
cec7bece 9693 }
e11f1233
PM
9694 }
9695
9696 # reconstruct tags and heads lists
9697 if {$arctags($a) ne {} || $archeads($a) ne {}} {
9698 recalcarc $a
9699 recalcarc $na
9700 } else {
9701 set arctags($na) {}
9702 set archeads($na) {}
9703 }
9704}
9705
9706# Update things for a new commit added that is a child of one
9707# existing commit. Used when cherry-picking.
9708proc addnewchild {id p} {
5cd15b6b 9709 global allparents allchildren idtags nextarc
e11f1233 9710 global arcnos arcids arctags arcout arcend arcstart archeads growing
719c2b9d 9711 global seeds allcommits
e11f1233 9712
3ebba3c7 9713 if {![info exists allcommits] || ![info exists arcnos($p)]} return
e11f1233
PM
9714 set allparents($id) [list $p]
9715 set allchildren($id) {}
9716 set arcnos($id) {}
9717 lappend seeds $id
e11f1233
PM
9718 lappend allchildren($p) $id
9719 set a [incr nextarc]
9720 set arcstart($a) $id
9721 set archeads($a) {}
9722 set arctags($a) {}
9723 set arcids($a) [list $p]
9724 set arcend($a) $p
9725 if {![info exists arcout($p)]} {
9726 splitarc $p
9727 }
9728 lappend arcnos($p) $a
9729 set arcout($id) [list $a]
9730}
9731
5cd15b6b
PM
9732# This implements a cache for the topology information.
9733# The cache saves, for each arc, the start and end of the arc,
9734# the ids on the arc, and the outgoing arcs from the end.
9735proc readcache {f} {
9736 global arcnos arcids arcout arcstart arcend arctags archeads nextarc
9737 global idtags idheads allparents cachedarcs possible_seeds seeds growing
9738 global allcwait
9739
9740 set a $nextarc
9741 set lim $cachedarcs
9742 if {$lim - $a > 500} {
9743 set lim [expr {$a + 500}]
9744 }
9745 if {[catch {
9746 if {$a == $lim} {
9747 # finish reading the cache and setting up arctags, etc.
9748 set line [gets $f]
9749 if {$line ne "1"} {error "bad final version"}
9750 close $f
9751 foreach id [array names idtags] {
9752 if {[info exists arcnos($id)] && [llength $arcnos($id)] == 1 &&
9753 [llength $allparents($id)] == 1} {
9754 set a [lindex $arcnos($id) 0]
9755 if {$arctags($a) eq {}} {
9756 recalcarc $a
9757 }
9758 }
9759 }
9760 foreach id [array names idheads] {
9761 if {[info exists arcnos($id)] && [llength $arcnos($id)] == 1 &&
9762 [llength $allparents($id)] == 1} {
9763 set a [lindex $arcnos($id) 0]
9764 if {$archeads($a) eq {}} {
9765 recalcarc $a
9766 }
9767 }
9768 }
9769 foreach id [lsort -unique $possible_seeds] {
9770 if {$arcnos($id) eq {}} {
9771 lappend seeds $id
9772 }
9773 }
9774 set allcwait 0
9775 } else {
9776 while {[incr a] <= $lim} {
9777 set line [gets $f]
9778 if {[llength $line] != 3} {error "bad line"}
9779 set s [lindex $line 0]
9780 set arcstart($a) $s
9781 lappend arcout($s) $a
9782 if {![info exists arcnos($s)]} {
9783 lappend possible_seeds $s
9784 set arcnos($s) {}
9785 }
9786 set e [lindex $line 1]
9787 if {$e eq {}} {
9788 set growing($a) 1
9789 } else {
9790 set arcend($a) $e
9791 if {![info exists arcout($e)]} {
9792 set arcout($e) {}
9793 }
9794 }
9795 set arcids($a) [lindex $line 2]
9796 foreach id $arcids($a) {
9797 lappend allparents($s) $id
9798 set s $id
9799 lappend arcnos($id) $a
9800 }
9801 if {![info exists allparents($s)]} {
9802 set allparents($s) {}
9803 }
9804 set arctags($a) {}
9805 set archeads($a) {}
9806 }
9807 set nextarc [expr {$a - 1}]
9808 }
9809 } err]} {
9810 dropcache $err
9811 return 0
9812 }
9813 if {!$allcwait} {
9814 getallcommits
9815 }
9816 return $allcwait
9817}
9818
9819proc getcache {f} {
9820 global nextarc cachedarcs possible_seeds
9821
9822 if {[catch {
9823 set line [gets $f]
9824 if {[llength $line] != 2 || [lindex $line 0] ne "1"} {error "bad version"}
9825 # make sure it's an integer
9826 set cachedarcs [expr {int([lindex $line 1])}]
9827 if {$cachedarcs < 0} {error "bad number of arcs"}
9828 set nextarc 0
9829 set possible_seeds {}
9830 run readcache $f
9831 } err]} {
9832 dropcache $err
9833 }
9834 return 0
9835}
9836
9837proc dropcache {err} {
9838 global allcwait nextarc cachedarcs seeds
9839
9840 #puts "dropping cache ($err)"
9841 foreach v {arcnos arcout arcids arcstart arcend growing \
9842 arctags archeads allparents allchildren} {
9843 global $v
9844 catch {unset $v}
9845 }
9846 set allcwait 0
9847 set nextarc 0
9848 set cachedarcs 0
9849 set seeds {}
9850 getallcommits
9851}
9852
9853proc writecache {f} {
9854 global cachearc cachedarcs allccache
9855 global arcstart arcend arcnos arcids arcout
9856
9857 set a $cachearc
9858 set lim $cachedarcs
9859 if {$lim - $a > 1000} {
9860 set lim [expr {$a + 1000}]
9861 }
9862 if {[catch {
9863 while {[incr a] <= $lim} {
9864 if {[info exists arcend($a)]} {
9865 puts $f [list $arcstart($a) $arcend($a) $arcids($a)]
9866 } else {
9867 puts $f [list $arcstart($a) {} $arcids($a)]
9868 }
9869 }
9870 } err]} {
9871 catch {close $f}
9872 catch {file delete $allccache}
9873 #puts "writing cache failed ($err)"
9874 return 0
9875 }
9876 set cachearc [expr {$a - 1}]
9877 if {$a > $cachedarcs} {
9878 puts $f "1"
9879 close $f
9880 return 0
9881 }
9882 return 1
9883}
9884
9885proc savecache {} {
9886 global nextarc cachedarcs cachearc allccache
9887
9888 if {$nextarc == $cachedarcs} return
9889 set cachearc 0
9890 set cachedarcs $nextarc
9891 catch {
9892 set f [open $allccache w]
9893 puts $f [list 1 $cachedarcs]
9894 run writecache $f
9895 }
9896}
9897
e11f1233
PM
9898# Returns 1 if a is an ancestor of b, -1 if b is an ancestor of a,
9899# or 0 if neither is true.
9900proc anc_or_desc {a b} {
9901 global arcout arcstart arcend arcnos cached_isanc
9902
9903 if {$arcnos($a) eq $arcnos($b)} {
9904 # Both are on the same arc(s); either both are the same BMP,
9905 # or if one is not a BMP, the other is also not a BMP or is
9906 # the BMP at end of the arc (and it only has 1 incoming arc).
69c0b5d2
PM
9907 # Or both can be BMPs with no incoming arcs.
9908 if {$a eq $b || $arcnos($a) eq {}} {
e11f1233 9909 return 0
cec7bece 9910 }
e11f1233
PM
9911 # assert {[llength $arcnos($a)] == 1}
9912 set arc [lindex $arcnos($a) 0]
9913 set i [lsearch -exact $arcids($arc) $a]
9914 set j [lsearch -exact $arcids($arc) $b]
9915 if {$i < 0 || $i > $j} {
9916 return 1
9917 } else {
9918 return -1
cec7bece
PM
9919 }
9920 }
e11f1233
PM
9921
9922 if {![info exists arcout($a)]} {
9923 set arc [lindex $arcnos($a) 0]
9924 if {[info exists arcend($arc)]} {
9925 set aend $arcend($arc)
9926 } else {
9927 set aend {}
cec7bece 9928 }
e11f1233
PM
9929 set a $arcstart($arc)
9930 } else {
9931 set aend $a
9932 }
9933 if {![info exists arcout($b)]} {
9934 set arc [lindex $arcnos($b) 0]
9935 if {[info exists arcend($arc)]} {
9936 set bend $arcend($arc)
9937 } else {
9938 set bend {}
cec7bece 9939 }
e11f1233
PM
9940 set b $arcstart($arc)
9941 } else {
9942 set bend $b
cec7bece 9943 }
e11f1233
PM
9944 if {$a eq $bend} {
9945 return 1
9946 }
9947 if {$b eq $aend} {
9948 return -1
9949 }
9950 if {[info exists cached_isanc($a,$bend)]} {
9951 if {$cached_isanc($a,$bend)} {
9952 return 1
9953 }
9954 }
9955 if {[info exists cached_isanc($b,$aend)]} {
9956 if {$cached_isanc($b,$aend)} {
9957 return -1
9958 }
9959 if {[info exists cached_isanc($a,$bend)]} {
9960 return 0
9961 }
cec7bece 9962 }
cec7bece 9963
e11f1233
PM
9964 set todo [list $a $b]
9965 set anc($a) a
9966 set anc($b) b
9967 for {set i 0} {$i < [llength $todo]} {incr i} {
9968 set x [lindex $todo $i]
9969 if {$anc($x) eq {}} {
9970 continue
9971 }
9972 foreach arc $arcnos($x) {
9973 set xd $arcstart($arc)
9974 if {$xd eq $bend} {
9975 set cached_isanc($a,$bend) 1
9976 set cached_isanc($b,$aend) 0
9977 return 1
9978 } elseif {$xd eq $aend} {
9979 set cached_isanc($b,$aend) 1
9980 set cached_isanc($a,$bend) 0
9981 return -1
9982 }
9983 if {![info exists anc($xd)]} {
9984 set anc($xd) $anc($x)
9985 lappend todo $xd
9986 } elseif {$anc($xd) ne $anc($x)} {
9987 set anc($xd) {}
9988 }
9989 }
9990 }
9991 set cached_isanc($a,$bend) 0
9992 set cached_isanc($b,$aend) 0
9993 return 0
9994}
b8ab2e17 9995
e11f1233
PM
9996# This identifies whether $desc has an ancestor that is
9997# a growing tip of the graph and which is not an ancestor of $anc
9998# and returns 0 if so and 1 if not.
9999# If we subsequently discover a tag on such a growing tip, and that
10000# turns out to be a descendent of $anc (which it could, since we
10001# don't necessarily see children before parents), then $desc
10002# isn't a good choice to display as a descendent tag of
10003# $anc (since it is the descendent of another tag which is
10004# a descendent of $anc). Similarly, $anc isn't a good choice to
10005# display as a ancestor tag of $desc.
10006#
10007proc is_certain {desc anc} {
10008 global arcnos arcout arcstart arcend growing problems
10009
10010 set certain {}
10011 if {[llength $arcnos($anc)] == 1} {
10012 # tags on the same arc are certain
10013 if {$arcnos($desc) eq $arcnos($anc)} {
10014 return 1
b8ab2e17 10015 }
e11f1233
PM
10016 if {![info exists arcout($anc)]} {
10017 # if $anc is partway along an arc, use the start of the arc instead
10018 set a [lindex $arcnos($anc) 0]
10019 set anc $arcstart($a)
b8ab2e17 10020 }
e11f1233
PM
10021 }
10022 if {[llength $arcnos($desc)] > 1 || [info exists arcout($desc)]} {
10023 set x $desc
10024 } else {
10025 set a [lindex $arcnos($desc) 0]
10026 set x $arcend($a)
10027 }
10028 if {$x == $anc} {
10029 return 1
10030 }
10031 set anclist [list $x]
10032 set dl($x) 1
10033 set nnh 1
10034 set ngrowanc 0
10035 for {set i 0} {$i < [llength $anclist] && ($nnh > 0 || $ngrowanc > 0)} {incr i} {
10036 set x [lindex $anclist $i]
10037 if {$dl($x)} {
10038 incr nnh -1
10039 }
10040 set done($x) 1
10041 foreach a $arcout($x) {
10042 if {[info exists growing($a)]} {
10043 if {![info exists growanc($x)] && $dl($x)} {
10044 set growanc($x) 1
10045 incr ngrowanc
10046 }
10047 } else {
10048 set y $arcend($a)
10049 if {[info exists dl($y)]} {
10050 if {$dl($y)} {
10051 if {!$dl($x)} {
10052 set dl($y) 0
10053 if {![info exists done($y)]} {
10054 incr nnh -1
10055 }
10056 if {[info exists growanc($x)]} {
10057 incr ngrowanc -1
10058 }
10059 set xl [list $y]
10060 for {set k 0} {$k < [llength $xl]} {incr k} {
10061 set z [lindex $xl $k]
10062 foreach c $arcout($z) {
10063 if {[info exists arcend($c)]} {
10064 set v $arcend($c)
10065 if {[info exists dl($v)] && $dl($v)} {
10066 set dl($v) 0
10067 if {![info exists done($v)]} {
10068 incr nnh -1
10069 }
10070 if {[info exists growanc($v)]} {
10071 incr ngrowanc -1
10072 }
10073 lappend xl $v
10074 }
10075 }
10076 }
10077 }
10078 }
10079 }
10080 } elseif {$y eq $anc || !$dl($x)} {
10081 set dl($y) 0
10082 lappend anclist $y
10083 } else {
10084 set dl($y) 1
10085 lappend anclist $y
10086 incr nnh
10087 }
10088 }
b8ab2e17
PM
10089 }
10090 }
e11f1233
PM
10091 foreach x [array names growanc] {
10092 if {$dl($x)} {
10093 return 0
b8ab2e17 10094 }
7eb3cb9c 10095 return 0
b8ab2e17 10096 }
e11f1233 10097 return 1
b8ab2e17
PM
10098}
10099
e11f1233
PM
10100proc validate_arctags {a} {
10101 global arctags idtags
b8ab2e17 10102
e11f1233
PM
10103 set i -1
10104 set na $arctags($a)
10105 foreach id $arctags($a) {
10106 incr i
10107 if {![info exists idtags($id)]} {
10108 set na [lreplace $na $i $i]
10109 incr i -1
10110 }
10111 }
10112 set arctags($a) $na
10113}
10114
10115proc validate_archeads {a} {
10116 global archeads idheads
10117
10118 set i -1
10119 set na $archeads($a)
10120 foreach id $archeads($a) {
10121 incr i
10122 if {![info exists idheads($id)]} {
10123 set na [lreplace $na $i $i]
10124 incr i -1
10125 }
10126 }
10127 set archeads($a) $na
10128}
10129
10130# Return the list of IDs that have tags that are descendents of id,
10131# ignoring IDs that are descendents of IDs already reported.
10132proc desctags {id} {
10133 global arcnos arcstart arcids arctags idtags allparents
10134 global growing cached_dtags
10135
10136 if {![info exists allparents($id)]} {
10137 return {}
10138 }
10139 set t1 [clock clicks -milliseconds]
10140 set argid $id
10141 if {[llength $arcnos($id)] == 1 && [llength $allparents($id)] == 1} {
10142 # part-way along an arc; check that arc first
10143 set a [lindex $arcnos($id) 0]
10144 if {$arctags($a) ne {}} {
10145 validate_arctags $a
10146 set i [lsearch -exact $arcids($a) $id]
10147 set tid {}
10148 foreach t $arctags($a) {
10149 set j [lsearch -exact $arcids($a) $t]
10150 if {$j >= $i} break
10151 set tid $t
b8ab2e17 10152 }
e11f1233
PM
10153 if {$tid ne {}} {
10154 return $tid
b8ab2e17
PM
10155 }
10156 }
e11f1233
PM
10157 set id $arcstart($a)
10158 if {[info exists idtags($id)]} {
10159 return $id
10160 }
10161 }
10162 if {[info exists cached_dtags($id)]} {
10163 return $cached_dtags($id)
10164 }
10165
10166 set origid $id
10167 set todo [list $id]
10168 set queued($id) 1
10169 set nc 1
10170 for {set i 0} {$i < [llength $todo] && $nc > 0} {incr i} {
10171 set id [lindex $todo $i]
10172 set done($id) 1
10173 set ta [info exists hastaggedancestor($id)]
10174 if {!$ta} {
10175 incr nc -1
10176 }
10177 # ignore tags on starting node
10178 if {!$ta && $i > 0} {
10179 if {[info exists idtags($id)]} {
10180 set tagloc($id) $id
10181 set ta 1
10182 } elseif {[info exists cached_dtags($id)]} {
10183 set tagloc($id) $cached_dtags($id)
10184 set ta 1
10185 }
10186 }
10187 foreach a $arcnos($id) {
10188 set d $arcstart($a)
10189 if {!$ta && $arctags($a) ne {}} {
10190 validate_arctags $a
10191 if {$arctags($a) ne {}} {
10192 lappend tagloc($id) [lindex $arctags($a) end]
10193 }
10194 }
10195 if {$ta || $arctags($a) ne {}} {
10196 set tomark [list $d]
10197 for {set j 0} {$j < [llength $tomark]} {incr j} {
10198 set dd [lindex $tomark $j]
10199 if {![info exists hastaggedancestor($dd)]} {
10200 if {[info exists done($dd)]} {
10201 foreach b $arcnos($dd) {
10202 lappend tomark $arcstart($b)
10203 }
10204 if {[info exists tagloc($dd)]} {
10205 unset tagloc($dd)
10206 }
10207 } elseif {[info exists queued($dd)]} {
10208 incr nc -1
10209 }
10210 set hastaggedancestor($dd) 1
10211 }
10212 }
10213 }
10214 if {![info exists queued($d)]} {
10215 lappend todo $d
10216 set queued($d) 1
10217 if {![info exists hastaggedancestor($d)]} {
10218 incr nc
10219 }
10220 }
b8ab2e17 10221 }
f1d83ba3 10222 }
e11f1233
PM
10223 set tags {}
10224 foreach id [array names tagloc] {
10225 if {![info exists hastaggedancestor($id)]} {
10226 foreach t $tagloc($id) {
10227 if {[lsearch -exact $tags $t] < 0} {
10228 lappend tags $t
10229 }
10230 }
10231 }
10232 }
10233 set t2 [clock clicks -milliseconds]
10234 set loopix $i
f1d83ba3 10235
e11f1233
PM
10236 # remove tags that are descendents of other tags
10237 for {set i 0} {$i < [llength $tags]} {incr i} {
10238 set a [lindex $tags $i]
10239 for {set j 0} {$j < $i} {incr j} {
10240 set b [lindex $tags $j]
10241 set r [anc_or_desc $a $b]
10242 if {$r == 1} {
10243 set tags [lreplace $tags $j $j]
10244 incr j -1
10245 incr i -1
10246 } elseif {$r == -1} {
10247 set tags [lreplace $tags $i $i]
10248 incr i -1
10249 break
ceadfe90
PM
10250 }
10251 }
10252 }
10253
e11f1233
PM
10254 if {[array names growing] ne {}} {
10255 # graph isn't finished, need to check if any tag could get
10256 # eclipsed by another tag coming later. Simply ignore any
10257 # tags that could later get eclipsed.
10258 set ctags {}
10259 foreach t $tags {
10260 if {[is_certain $t $origid]} {
10261 lappend ctags $t
10262 }
ceadfe90 10263 }
e11f1233
PM
10264 if {$tags eq $ctags} {
10265 set cached_dtags($origid) $tags
10266 } else {
10267 set tags $ctags
ceadfe90 10268 }
e11f1233
PM
10269 } else {
10270 set cached_dtags($origid) $tags
10271 }
10272 set t3 [clock clicks -milliseconds]
10273 if {0 && $t3 - $t1 >= 100} {
10274 puts "iterating descendents ($loopix/[llength $todo] nodes) took\
10275 [expr {$t2-$t1}]+[expr {$t3-$t2}]ms, $nc candidates left"
ceadfe90 10276 }
e11f1233
PM
10277 return $tags
10278}
ceadfe90 10279
e11f1233
PM
10280proc anctags {id} {
10281 global arcnos arcids arcout arcend arctags idtags allparents
10282 global growing cached_atags
10283
10284 if {![info exists allparents($id)]} {
10285 return {}
10286 }
10287 set t1 [clock clicks -milliseconds]
10288 set argid $id
10289 if {[llength $arcnos($id)] == 1 && [llength $allparents($id)] == 1} {
10290 # part-way along an arc; check that arc first
10291 set a [lindex $arcnos($id) 0]
10292 if {$arctags($a) ne {}} {
10293 validate_arctags $a
10294 set i [lsearch -exact $arcids($a) $id]
10295 foreach t $arctags($a) {
10296 set j [lsearch -exact $arcids($a) $t]
10297 if {$j > $i} {
10298 return $t
10299 }
10300 }
ceadfe90 10301 }
e11f1233
PM
10302 if {![info exists arcend($a)]} {
10303 return {}
10304 }
10305 set id $arcend($a)
10306 if {[info exists idtags($id)]} {
10307 return $id
10308 }
10309 }
10310 if {[info exists cached_atags($id)]} {
10311 return $cached_atags($id)
10312 }
10313
10314 set origid $id
10315 set todo [list $id]
10316 set queued($id) 1
10317 set taglist {}
10318 set nc 1
10319 for {set i 0} {$i < [llength $todo] && $nc > 0} {incr i} {
10320 set id [lindex $todo $i]
10321 set done($id) 1
10322 set td [info exists hastaggeddescendent($id)]
10323 if {!$td} {
10324 incr nc -1
10325 }
10326 # ignore tags on starting node
10327 if {!$td && $i > 0} {
10328 if {[info exists idtags($id)]} {
10329 set tagloc($id) $id
10330 set td 1
10331 } elseif {[info exists cached_atags($id)]} {
10332 set tagloc($id) $cached_atags($id)
10333 set td 1
10334 }
10335 }
10336 foreach a $arcout($id) {
10337 if {!$td && $arctags($a) ne {}} {
10338 validate_arctags $a
10339 if {$arctags($a) ne {}} {
10340 lappend tagloc($id) [lindex $arctags($a) 0]
10341 }
10342 }
10343 if {![info exists arcend($a)]} continue
10344 set d $arcend($a)
10345 if {$td || $arctags($a) ne {}} {
10346 set tomark [list $d]
10347 for {set j 0} {$j < [llength $tomark]} {incr j} {
10348 set dd [lindex $tomark $j]
10349 if {![info exists hastaggeddescendent($dd)]} {
10350 if {[info exists done($dd)]} {
10351 foreach b $arcout($dd) {
10352 if {[info exists arcend($b)]} {
10353 lappend tomark $arcend($b)
10354 }
10355 }
10356 if {[info exists tagloc($dd)]} {
10357 unset tagloc($dd)
10358 }
10359 } elseif {[info exists queued($dd)]} {
10360 incr nc -1
10361 }
10362 set hastaggeddescendent($dd) 1
10363 }
10364 }
10365 }
10366 if {![info exists queued($d)]} {
10367 lappend todo $d
10368 set queued($d) 1
10369 if {![info exists hastaggeddescendent($d)]} {
10370 incr nc
10371 }
10372 }
10373 }
10374 }
10375 set t2 [clock clicks -milliseconds]
10376 set loopix $i
10377 set tags {}
10378 foreach id [array names tagloc] {
10379 if {![info exists hastaggeddescendent($id)]} {
10380 foreach t $tagloc($id) {
10381 if {[lsearch -exact $tags $t] < 0} {
10382 lappend tags $t
10383 }
10384 }
ceadfe90
PM
10385 }
10386 }
ceadfe90 10387
e11f1233
PM
10388 # remove tags that are ancestors of other tags
10389 for {set i 0} {$i < [llength $tags]} {incr i} {
10390 set a [lindex $tags $i]
10391 for {set j 0} {$j < $i} {incr j} {
10392 set b [lindex $tags $j]
10393 set r [anc_or_desc $a $b]
10394 if {$r == -1} {
10395 set tags [lreplace $tags $j $j]
10396 incr j -1
10397 incr i -1
10398 } elseif {$r == 1} {
10399 set tags [lreplace $tags $i $i]
10400 incr i -1
10401 break
10402 }
10403 }
10404 }
10405
10406 if {[array names growing] ne {}} {
10407 # graph isn't finished, need to check if any tag could get
10408 # eclipsed by another tag coming later. Simply ignore any
10409 # tags that could later get eclipsed.
10410 set ctags {}
10411 foreach t $tags {
10412 if {[is_certain $origid $t]} {
10413 lappend ctags $t
10414 }
10415 }
10416 if {$tags eq $ctags} {
10417 set cached_atags($origid) $tags
10418 } else {
10419 set tags $ctags
d6ac1a86 10420 }
e11f1233
PM
10421 } else {
10422 set cached_atags($origid) $tags
10423 }
10424 set t3 [clock clicks -milliseconds]
10425 if {0 && $t3 - $t1 >= 100} {
10426 puts "iterating ancestors ($loopix/[llength $todo] nodes) took\
10427 [expr {$t2-$t1}]+[expr {$t3-$t2}]ms, $nc candidates left"
d6ac1a86 10428 }
e11f1233 10429 return $tags
d6ac1a86
PM
10430}
10431
e11f1233
PM
10432# Return the list of IDs that have heads that are descendents of id,
10433# including id itself if it has a head.
10434proc descheads {id} {
10435 global arcnos arcstart arcids archeads idheads cached_dheads
10436 global allparents
ca6d8f58 10437
e11f1233
PM
10438 if {![info exists allparents($id)]} {
10439 return {}
10440 }
f3326b66 10441 set aret {}
e11f1233
PM
10442 if {[llength $arcnos($id)] == 1 && [llength $allparents($id)] == 1} {
10443 # part-way along an arc; check it first
10444 set a [lindex $arcnos($id) 0]
10445 if {$archeads($a) ne {}} {
10446 validate_archeads $a
10447 set i [lsearch -exact $arcids($a) $id]
10448 foreach t $archeads($a) {
10449 set j [lsearch -exact $arcids($a) $t]
10450 if {$j > $i} break
f3326b66 10451 lappend aret $t
e11f1233 10452 }
ca6d8f58 10453 }
e11f1233 10454 set id $arcstart($a)
ca6d8f58 10455 }
e11f1233
PM
10456 set origid $id
10457 set todo [list $id]
10458 set seen($id) 1
f3326b66 10459 set ret {}
e11f1233
PM
10460 for {set i 0} {$i < [llength $todo]} {incr i} {
10461 set id [lindex $todo $i]
10462 if {[info exists cached_dheads($id)]} {
10463 set ret [concat $ret $cached_dheads($id)]
10464 } else {
10465 if {[info exists idheads($id)]} {
10466 lappend ret $id
10467 }
10468 foreach a $arcnos($id) {
10469 if {$archeads($a) ne {}} {
706d6c3e
PM
10470 validate_archeads $a
10471 if {$archeads($a) ne {}} {
10472 set ret [concat $ret $archeads($a)]
10473 }
e11f1233
PM
10474 }
10475 set d $arcstart($a)
10476 if {![info exists seen($d)]} {
10477 lappend todo $d
10478 set seen($d) 1
10479 }
10480 }
10299152 10481 }
10299152 10482 }
e11f1233
PM
10483 set ret [lsort -unique $ret]
10484 set cached_dheads($origid) $ret
f3326b66 10485 return [concat $ret $aret]
10299152
PM
10486}
10487
e11f1233
PM
10488proc addedtag {id} {
10489 global arcnos arcout cached_dtags cached_atags
ca6d8f58 10490
e11f1233
PM
10491 if {![info exists arcnos($id)]} return
10492 if {![info exists arcout($id)]} {
10493 recalcarc [lindex $arcnos($id) 0]
ca6d8f58 10494 }
e11f1233
PM
10495 catch {unset cached_dtags}
10496 catch {unset cached_atags}
ca6d8f58
PM
10497}
10498
e11f1233
PM
10499proc addedhead {hid head} {
10500 global arcnos arcout cached_dheads
10501
10502 if {![info exists arcnos($hid)]} return
10503 if {![info exists arcout($hid)]} {
10504 recalcarc [lindex $arcnos($hid) 0]
10505 }
10506 catch {unset cached_dheads}
10507}
10508
10509proc removedhead {hid head} {
10510 global cached_dheads
10511
10512 catch {unset cached_dheads}
10513}
10514
10515proc movedhead {hid head} {
10516 global arcnos arcout cached_dheads
cec7bece 10517
e11f1233
PM
10518 if {![info exists arcnos($hid)]} return
10519 if {![info exists arcout($hid)]} {
10520 recalcarc [lindex $arcnos($hid) 0]
cec7bece 10521 }
e11f1233
PM
10522 catch {unset cached_dheads}
10523}
10524
10525proc changedrefs {} {
10526 global cached_dheads cached_dtags cached_atags
10527 global arctags archeads arcnos arcout idheads idtags
10528
10529 foreach id [concat [array names idheads] [array names idtags]] {
10530 if {[info exists arcnos($id)] && ![info exists arcout($id)]} {
10531 set a [lindex $arcnos($id) 0]
10532 if {![info exists donearc($a)]} {
10533 recalcarc $a
10534 set donearc($a) 1
10535 }
cec7bece
PM
10536 }
10537 }
e11f1233
PM
10538 catch {unset cached_dtags}
10539 catch {unset cached_atags}
10540 catch {unset cached_dheads}
cec7bece
PM
10541}
10542
f1d83ba3 10543proc rereadrefs {} {
fc2a256f 10544 global idtags idheads idotherrefs mainheadid
f1d83ba3
PM
10545
10546 set refids [concat [array names idtags] \
10547 [array names idheads] [array names idotherrefs]]
10548 foreach id $refids {
10549 if {![info exists ref($id)]} {
10550 set ref($id) [listrefs $id]
10551 }
10552 }
fc2a256f 10553 set oldmainhead $mainheadid
f1d83ba3 10554 readrefs
cec7bece 10555 changedrefs
f1d83ba3
PM
10556 set refids [lsort -unique [concat $refids [array names idtags] \
10557 [array names idheads] [array names idotherrefs]]]
10558 foreach id $refids {
10559 set v [listrefs $id]
c11ff120 10560 if {![info exists ref($id)] || $ref($id) != $v} {
f1d83ba3
PM
10561 redrawtags $id
10562 }
10563 }
c11ff120
PM
10564 if {$oldmainhead ne $mainheadid} {
10565 redrawtags $oldmainhead
10566 redrawtags $mainheadid
10567 }
887c996e 10568 run refill_reflist
f1d83ba3
PM
10569}
10570
2e1ded44
JH
10571proc listrefs {id} {
10572 global idtags idheads idotherrefs
10573
10574 set x {}
10575 if {[info exists idtags($id)]} {
10576 set x $idtags($id)
10577 }
10578 set y {}
10579 if {[info exists idheads($id)]} {
10580 set y $idheads($id)
10581 }
10582 set z {}
10583 if {[info exists idotherrefs($id)]} {
10584 set z $idotherrefs($id)
10585 }
10586 return [list $x $y $z]
10587}
10588
106288cb 10589proc showtag {tag isnew} {
62d3ea65 10590 global ctext tagcontents tagids linknum tagobjid
106288cb
PM
10591
10592 if {$isnew} {
354af6bd 10593 addtohistory [list showtag $tag 0] savectextpos
106288cb
PM
10594 }
10595 $ctext conf -state normal
3ea06f9f 10596 clear_ctext
32f1b3e4 10597 settabs 0
106288cb 10598 set linknum 0
62d3ea65
PM
10599 if {![info exists tagcontents($tag)]} {
10600 catch {
fcacf489 10601 set tagcontents($tag) [exec git cat-file tag $tag]
62d3ea65
PM
10602 }
10603 }
106288cb
PM
10604 if {[info exists tagcontents($tag)]} {
10605 set text $tagcontents($tag)
10606 } else {
d990cedf 10607 set text "[mc "Tag"]: $tag\n[mc "Id"]: $tagids($tag)"
106288cb 10608 }
f1b86294 10609 appendwithlinks $text {}
a80e82f6 10610 maybe_scroll_ctext 1
106288cb 10611 $ctext conf -state disabled
7fcceed7 10612 init_flist {}
106288cb
PM
10613}
10614
1d10f36d
PM
10615proc doquit {} {
10616 global stopped
314f5de1
TA
10617 global gitktmpdir
10618
1d10f36d 10619 set stopped 100
b6047c5a 10620 savestuff .
1d10f36d 10621 destroy .
314f5de1
TA
10622
10623 if {[info exists gitktmpdir]} {
10624 catch {file delete -force $gitktmpdir}
10625 }
1d10f36d 10626}
1db95b00 10627
9a7558f3 10628proc mkfontdisp {font top which} {
d93f1713 10629 global fontattr fontpref $font NS use_ttk
9a7558f3
PM
10630
10631 set fontpref($font) [set $font]
d93f1713 10632 ${NS}::button $top.${font}but -text $which \
9a7558f3 10633 -command [list choosefont $font $which]
d93f1713 10634 ${NS}::label $top.$font -relief flat -font $font \
9a7558f3
PM
10635 -text $fontattr($font,family) -justify left
10636 grid x $top.${font}but $top.$font -sticky w
10637}
10638
10639proc choosefont {font which} {
10640 global fontparam fontlist fonttop fontattr
d93f1713 10641 global prefstop NS
9a7558f3
PM
10642
10643 set fontparam(which) $which
10644 set fontparam(font) $font
10645 set fontparam(family) [font actual $font -family]
10646 set fontparam(size) $fontattr($font,size)
10647 set fontparam(weight) $fontattr($font,weight)
10648 set fontparam(slant) $fontattr($font,slant)
10649 set top .gitkfont
10650 set fonttop $top
10651 if {![winfo exists $top]} {
10652 font create sample
10653 eval font config sample [font actual $font]
d93f1713 10654 ttk_toplevel $top
e7d64008 10655 make_transient $top $prefstop
d990cedf 10656 wm title $top [mc "Gitk font chooser"]
d93f1713 10657 ${NS}::label $top.l -textvariable fontparam(which)
9a7558f3
PM
10658 pack $top.l -side top
10659 set fontlist [lsort [font families]]
d93f1713 10660 ${NS}::frame $top.f
9a7558f3
PM
10661 listbox $top.f.fam -listvariable fontlist \
10662 -yscrollcommand [list $top.f.sb set]
10663 bind $top.f.fam <<ListboxSelect>> selfontfam
d93f1713 10664 ${NS}::scrollbar $top.f.sb -command [list $top.f.fam yview]
9a7558f3
PM
10665 pack $top.f.sb -side right -fill y
10666 pack $top.f.fam -side left -fill both -expand 1
10667 pack $top.f -side top -fill both -expand 1
d93f1713 10668 ${NS}::frame $top.g
9a7558f3
PM
10669 spinbox $top.g.size -from 4 -to 40 -width 4 \
10670 -textvariable fontparam(size) \
10671 -validatecommand {string is integer -strict %s}
10672 checkbutton $top.g.bold -padx 5 \
d990cedf 10673 -font {{Times New Roman} 12 bold} -text [mc "B"] -indicatoron 0 \
9a7558f3
PM
10674 -variable fontparam(weight) -onvalue bold -offvalue normal
10675 checkbutton $top.g.ital -padx 5 \
d990cedf 10676 -font {{Times New Roman} 12 italic} -text [mc "I"] -indicatoron 0 \
9a7558f3
PM
10677 -variable fontparam(slant) -onvalue italic -offvalue roman
10678 pack $top.g.size $top.g.bold $top.g.ital -side left
10679 pack $top.g -side top
10680 canvas $top.c -width 150 -height 50 -border 2 -relief sunk \
10681 -background white
10682 $top.c create text 100 25 -anchor center -text $which -font sample \
10683 -fill black -tags text
10684 bind $top.c <Configure> [list centertext $top.c]
10685 pack $top.c -side top -fill x
d93f1713
PT
10686 ${NS}::frame $top.buts
10687 ${NS}::button $top.buts.ok -text [mc "OK"] -command fontok -default active
10688 ${NS}::button $top.buts.can -text [mc "Cancel"] -command fontcan -default normal
76f15947
AG
10689 bind $top <Key-Return> fontok
10690 bind $top <Key-Escape> fontcan
9a7558f3
PM
10691 grid $top.buts.ok $top.buts.can
10692 grid columnconfigure $top.buts 0 -weight 1 -uniform a
10693 grid columnconfigure $top.buts 1 -weight 1 -uniform a
10694 pack $top.buts -side bottom -fill x
10695 trace add variable fontparam write chg_fontparam
10696 } else {
10697 raise $top
10698 $top.c itemconf text -text $which
10699 }
10700 set i [lsearch -exact $fontlist $fontparam(family)]
10701 if {$i >= 0} {
10702 $top.f.fam selection set $i
10703 $top.f.fam see $i
10704 }
10705}
10706
10707proc centertext {w} {
10708 $w coords text [expr {[winfo width $w] / 2}] [expr {[winfo height $w] / 2}]
10709}
10710
10711proc fontok {} {
10712 global fontparam fontpref prefstop
10713
10714 set f $fontparam(font)
10715 set fontpref($f) [list $fontparam(family) $fontparam(size)]
10716 if {$fontparam(weight) eq "bold"} {
10717 lappend fontpref($f) "bold"
10718 }
10719 if {$fontparam(slant) eq "italic"} {
10720 lappend fontpref($f) "italic"
10721 }
10722 set w $prefstop.$f
10723 $w conf -text $fontparam(family) -font $fontpref($f)
d93f1713 10724
9a7558f3
PM
10725 fontcan
10726}
10727
10728proc fontcan {} {
10729 global fonttop fontparam
10730
10731 if {[info exists fonttop]} {
10732 catch {destroy $fonttop}
10733 catch {font delete sample}
10734 unset fonttop
10735 unset fontparam
10736 }
10737}
10738
d93f1713
PT
10739if {[package vsatisfies [package provide Tk] 8.6]} {
10740 # In Tk 8.6 we have a native font chooser dialog. Overwrite the above
10741 # function to make use of it.
10742 proc choosefont {font which} {
10743 tk fontchooser configure -title $which -font $font \
10744 -command [list on_choosefont $font $which]
10745 tk fontchooser show
10746 }
10747 proc on_choosefont {font which newfont} {
10748 global fontparam
10749 puts stderr "$font $newfont"
10750 array set f [font actual $newfont]
10751 set fontparam(which) $which
10752 set fontparam(font) $font
10753 set fontparam(family) $f(-family)
10754 set fontparam(size) $f(-size)
10755 set fontparam(weight) $f(-weight)
10756 set fontparam(slant) $f(-slant)
10757 fontok
10758 }
10759}
10760
9a7558f3
PM
10761proc selfontfam {} {
10762 global fonttop fontparam
10763
10764 set i [$fonttop.f.fam curselection]
10765 if {$i ne {}} {
10766 set fontparam(family) [$fonttop.f.fam get $i]
10767 }
10768}
10769
10770proc chg_fontparam {v sub op} {
10771 global fontparam
10772
10773 font config sample -$sub $fontparam($sub)
10774}
10775
712fcc08 10776proc doprefs {} {
d93f1713 10777 global maxwidth maxgraphpct use_ttk NS
219ea3a9 10778 global oldprefs prefstop showneartags showlocalchanges
5497f7a2 10779 global uicolor bgcolor fgcolor ctext diffcolors selectbgcolor markbgcolor
21ac8a8d 10780 global tabstop limitdiffs autoselect autosellen extdifftool perfile_attrs
0cc08ff7 10781 global hideremotes want_ttk have_ttk
232475d3 10782
712fcc08
PM
10783 set top .gitkprefs
10784 set prefstop $top
10785 if {[winfo exists $top]} {
10786 raise $top
10787 return
757f17bc 10788 }
3de07118 10789 foreach v {maxwidth maxgraphpct showneartags showlocalchanges \
0cc08ff7 10790 limitdiffs tabstop perfile_attrs hideremotes want_ttk} {
712fcc08 10791 set oldprefs($v) [set $v]
232475d3 10792 }
d93f1713 10793 ttk_toplevel $top
d990cedf 10794 wm title $top [mc "Gitk preferences"]
e7d64008 10795 make_transient $top .
d93f1713 10796 ${NS}::label $top.ldisp -text [mc "Commit list display options"]
712fcc08 10797 grid $top.ldisp - -sticky w -pady 10
d93f1713
PT
10798 ${NS}::label $top.spacer -text " "
10799 ${NS}::label $top.maxwidthl -text [mc "Maximum graph width (lines)"]
712fcc08
PM
10800 spinbox $top.maxwidth -from 0 -to 100 -width 4 -textvariable maxwidth
10801 grid $top.spacer $top.maxwidthl $top.maxwidth -sticky w
d93f1713 10802 ${NS}::label $top.maxpctl -text [mc "Maximum graph width (% of pane)"]
712fcc08
PM
10803 spinbox $top.maxpct -from 1 -to 100 -width 4 -textvariable maxgraphpct
10804 grid x $top.maxpctl $top.maxpct -sticky w
d93f1713
PT
10805 ${NS}::checkbutton $top.showlocal -text [mc "Show local changes"] \
10806 -variable showlocalchanges
219ea3a9 10807 grid x $top.showlocal -sticky w
21ac8a8d 10808 ${NS}::checkbutton $top.autoselect -text [mc "Auto-select SHA1 (length)"] \
d93f1713 10809 -variable autoselect
21ac8a8d
PM
10810 spinbox $top.autosellen -from 1 -to 40 -width 4 -textvariable autosellen
10811 grid x $top.autoselect $top.autosellen -sticky w
0cc08ff7
PM
10812 ${NS}::checkbutton $top.hideremotes -text [mc "Hide remote refs"] \
10813 -variable hideremotes
10814 grid x $top.hideremotes -sticky w
f8a2c0d1 10815
d93f1713 10816 ${NS}::label $top.ddisp -text [mc "Diff display options"]
712fcc08 10817 grid $top.ddisp - -sticky w -pady 10
d93f1713 10818 ${NS}::label $top.tabstopl -text [mc "Tab spacing"]
94503918
PM
10819 spinbox $top.tabstop -from 1 -to 20 -width 4 -textvariable tabstop
10820 grid x $top.tabstopl $top.tabstop -sticky w
d93f1713
PT
10821 ${NS}::checkbutton $top.ntag -text [mc "Display nearby tags"] \
10822 -variable showneartags
b8ab2e17 10823 grid x $top.ntag -sticky w
d93f1713
PT
10824 ${NS}::checkbutton $top.ldiff -text [mc "Limit diffs to listed paths"] \
10825 -variable limitdiffs
7a39a17a 10826 grid x $top.ldiff -sticky w
d93f1713
PT
10827 ${NS}::checkbutton $top.lattr -text [mc "Support per-file encodings"] \
10828 -variable perfile_attrs
39ee47ef 10829 grid x $top.lattr -sticky w
f8a2c0d1 10830
d93f1713
PT
10831 ${NS}::entry $top.extdifft -textvariable extdifftool
10832 ${NS}::frame $top.extdifff
10833 ${NS}::label $top.extdifff.l -text [mc "External diff tool" ]
10834 ${NS}::button $top.extdifff.b -text [mc "Choose..."] -command choose_extdiff
314f5de1 10835 pack $top.extdifff.l $top.extdifff.b -side left
d93f1713
PT
10836 pack configure $top.extdifff.l -padx 10
10837 grid x $top.extdifff $top.extdifft -sticky ew
314f5de1 10838
0cc08ff7
PM
10839 ${NS}::label $top.lgen -text [mc "General options"]
10840 grid $top.lgen - -sticky w -pady 10
10841 ${NS}::checkbutton $top.want_ttk -variable want_ttk \
10842 -text [mc "Use themed widgets"]
10843 if {$have_ttk} {
10844 ${NS}::label $top.ttk_note -text [mc "(change requires restart)"]
10845 } else {
10846 ${NS}::label $top.ttk_note -text [mc "(currently unavailable)"]
10847 }
10848 grid x $top.want_ttk $top.ttk_note -sticky w
314f5de1 10849
d93f1713 10850 ${NS}::label $top.cdisp -text [mc "Colors: press to choose"]
f8a2c0d1 10851 grid $top.cdisp - -sticky w -pady 10
5497f7a2 10852 label $top.ui -padx 40 -relief sunk -background $uicolor
1924d1bc 10853 ${NS}::button $top.uibut -text [mc "Interface"] \
5497f7a2
GR
10854 -command [list choosecolor uicolor {} $top.ui [mc "interface"] setui]
10855 grid x $top.uibut $top.ui -sticky w
f8a2c0d1 10856 label $top.bg -padx 40 -relief sunk -background $bgcolor
d93f1713 10857 ${NS}::button $top.bgbut -text [mc "Background"] \
968b016a 10858 -command [list choosecolor bgcolor {} $top.bg [mc "background"] setbg]
f8a2c0d1
PM
10859 grid x $top.bgbut $top.bg -sticky w
10860 label $top.fg -padx 40 -relief sunk -background $fgcolor
d93f1713 10861 ${NS}::button $top.fgbut -text [mc "Foreground"] \
968b016a 10862 -command [list choosecolor fgcolor {} $top.fg [mc "foreground"] setfg]
f8a2c0d1
PM
10863 grid x $top.fgbut $top.fg -sticky w
10864 label $top.diffold -padx 40 -relief sunk -background [lindex $diffcolors 0]
d93f1713 10865 ${NS}::button $top.diffoldbut -text [mc "Diff: old lines"] \
968b016a 10866 -command [list choosecolor diffcolors 0 $top.diffold [mc "diff old lines"] \
f8a2c0d1
PM
10867 [list $ctext tag conf d0 -foreground]]
10868 grid x $top.diffoldbut $top.diffold -sticky w
10869 label $top.diffnew -padx 40 -relief sunk -background [lindex $diffcolors 1]
d93f1713 10870 ${NS}::button $top.diffnewbut -text [mc "Diff: new lines"] \
968b016a 10871 -command [list choosecolor diffcolors 1 $top.diffnew [mc "diff new lines"] \
8b07dca1 10872 [list $ctext tag conf dresult -foreground]]
f8a2c0d1
PM
10873 grid x $top.diffnewbut $top.diffnew -sticky w
10874 label $top.hunksep -padx 40 -relief sunk -background [lindex $diffcolors 2]
d93f1713 10875 ${NS}::button $top.hunksepbut -text [mc "Diff: hunk header"] \
f8a2c0d1 10876 -command [list choosecolor diffcolors 2 $top.hunksep \
968b016a 10877 [mc "diff hunk header"] \
f8a2c0d1
PM
10878 [list $ctext tag conf hunksep -foreground]]
10879 grid x $top.hunksepbut $top.hunksep -sticky w
e3e901be 10880 label $top.markbgsep -padx 40 -relief sunk -background $markbgcolor
d93f1713 10881 ${NS}::button $top.markbgbut -text [mc "Marked line bg"] \
e3e901be
PM
10882 -command [list choosecolor markbgcolor {} $top.markbgsep \
10883 [mc "marked line background"] \
10884 [list $ctext tag conf omark -background]]
10885 grid x $top.markbgbut $top.markbgsep -sticky w
60378c0c 10886 label $top.selbgsep -padx 40 -relief sunk -background $selectbgcolor
d93f1713 10887 ${NS}::button $top.selbgbut -text [mc "Select bg"] \
968b016a 10888 -command [list choosecolor selectbgcolor {} $top.selbgsep [mc "background"] setselbg]
60378c0c 10889 grid x $top.selbgbut $top.selbgsep -sticky w
f8a2c0d1 10890
d93f1713 10891 ${NS}::label $top.cfont -text [mc "Fonts: press to choose"]
9a7558f3 10892 grid $top.cfont - -sticky w -pady 10
d990cedf
CS
10893 mkfontdisp mainfont $top [mc "Main font"]
10894 mkfontdisp textfont $top [mc "Diff display font"]
10895 mkfontdisp uifont $top [mc "User interface font"]
9a7558f3 10896
d93f1713
PT
10897 ${NS}::frame $top.buts
10898 ${NS}::button $top.buts.ok -text [mc "OK"] -command prefsok -default active
10899 ${NS}::button $top.buts.can -text [mc "Cancel"] -command prefscan -default normal
76f15947
AG
10900 bind $top <Key-Return> prefsok
10901 bind $top <Key-Escape> prefscan
712fcc08
PM
10902 grid $top.buts.ok $top.buts.can
10903 grid columnconfigure $top.buts 0 -weight 1 -uniform a
10904 grid columnconfigure $top.buts 1 -weight 1 -uniform a
10905 grid $top.buts - - -pady 10 -sticky ew
d93f1713 10906 grid columnconfigure $top 2 -weight 1
3a950e9a 10907 bind $top <Visibility> "focus $top.buts.ok"
712fcc08
PM
10908}
10909
314f5de1
TA
10910proc choose_extdiff {} {
10911 global extdifftool
10912
b56e0a9a 10913 set prog [tk_getOpenFile -title [mc "External diff tool"] -multiple false]
314f5de1
TA
10914 if {$prog ne {}} {
10915 set extdifftool $prog
10916 }
10917}
10918
f8a2c0d1
PM
10919proc choosecolor {v vi w x cmd} {
10920 global $v
10921
10922 set c [tk_chooseColor -initialcolor [lindex [set $v] $vi] \
d990cedf 10923 -title [mc "Gitk: choose color for %s" $x]]
f8a2c0d1
PM
10924 if {$c eq {}} return
10925 $w conf -background $c
10926 lset $v $vi $c
10927 eval $cmd $c
10928}
10929
60378c0c
ML
10930proc setselbg {c} {
10931 global bglist cflist
10932 foreach w $bglist {
10933 $w configure -selectbackground $c
10934 }
10935 $cflist tag configure highlight \
10936 -background [$cflist cget -selectbackground]
10937 allcanvs itemconf secsel -fill $c
10938}
10939
51a7e8b6
PM
10940# This sets the background color and the color scheme for the whole UI.
10941# For some reason, tk_setPalette chooses a nasty dark red for selectColor
10942# if we don't specify one ourselves, which makes the checkbuttons and
10943# radiobuttons look bad. This chooses white for selectColor if the
10944# background color is light, or black if it is dark.
5497f7a2 10945proc setui {c} {
2e58c944 10946 if {[tk windowingsystem] eq "win32"} { return }
51a7e8b6
PM
10947 set bg [winfo rgb . $c]
10948 set selc black
10949 if {[lindex $bg 0] + 1.5 * [lindex $bg 1] + 0.5 * [lindex $bg 2] > 100000} {
10950 set selc white
10951 }
10952 tk_setPalette background $c selectColor $selc
5497f7a2
GR
10953}
10954
f8a2c0d1
PM
10955proc setbg {c} {
10956 global bglist
10957
10958 foreach w $bglist {
10959 $w conf -background $c
10960 }
10961}
10962
10963proc setfg {c} {
10964 global fglist canv
10965
10966 foreach w $fglist {
10967 $w conf -foreground $c
10968 }
10969 allcanvs itemconf text -fill $c
10970 $canv itemconf circle -outline $c
b9fdba7f 10971 $canv itemconf markid -outline $c
f8a2c0d1
PM
10972}
10973
712fcc08 10974proc prefscan {} {
94503918 10975 global oldprefs prefstop
712fcc08 10976
3de07118 10977 foreach v {maxwidth maxgraphpct showneartags showlocalchanges \
0cc08ff7 10978 limitdiffs tabstop perfile_attrs hideremotes want_ttk} {
94503918 10979 global $v
712fcc08
PM
10980 set $v $oldprefs($v)
10981 }
10982 catch {destroy $prefstop}
10983 unset prefstop
9a7558f3 10984 fontcan
712fcc08
PM
10985}
10986
10987proc prefsok {} {
10988 global maxwidth maxgraphpct
219ea3a9 10989 global oldprefs prefstop showneartags showlocalchanges
9a7558f3 10990 global fontpref mainfont textfont uifont
39ee47ef 10991 global limitdiffs treediffs perfile_attrs
ffe15297 10992 global hideremotes
712fcc08
PM
10993
10994 catch {destroy $prefstop}
10995 unset prefstop
9a7558f3
PM
10996 fontcan
10997 set fontchanged 0
10998 if {$mainfont ne $fontpref(mainfont)} {
10999 set mainfont $fontpref(mainfont)
11000 parsefont mainfont $mainfont
11001 eval font configure mainfont [fontflags mainfont]
11002 eval font configure mainfontbold [fontflags mainfont 1]
11003 setcoords
11004 set fontchanged 1
11005 }
11006 if {$textfont ne $fontpref(textfont)} {
11007 set textfont $fontpref(textfont)
11008 parsefont textfont $textfont
11009 eval font configure textfont [fontflags textfont]
11010 eval font configure textfontbold [fontflags textfont 1]
11011 }
11012 if {$uifont ne $fontpref(uifont)} {
11013 set uifont $fontpref(uifont)
11014 parsefont uifont $uifont
11015 eval font configure uifont [fontflags uifont]
11016 }
32f1b3e4 11017 settabs
219ea3a9
PM
11018 if {$showlocalchanges != $oldprefs(showlocalchanges)} {
11019 if {$showlocalchanges} {
11020 doshowlocalchanges
11021 } else {
11022 dohidelocalchanges
11023 }
11024 }
39ee47ef
PM
11025 if {$limitdiffs != $oldprefs(limitdiffs) ||
11026 ($perfile_attrs && !$oldprefs(perfile_attrs))} {
11027 # treediffs elements are limited by path;
11028 # won't have encodings cached if perfile_attrs was just turned on
74a40c71
PM
11029 catch {unset treediffs}
11030 }
9a7558f3 11031 if {$fontchanged || $maxwidth != $oldprefs(maxwidth)
712fcc08
PM
11032 || $maxgraphpct != $oldprefs(maxgraphpct)} {
11033 redisplay
7a39a17a
PM
11034 } elseif {$showneartags != $oldprefs(showneartags) ||
11035 $limitdiffs != $oldprefs(limitdiffs)} {
b8ab2e17 11036 reselectline
712fcc08 11037 }
ffe15297
TR
11038 if {$hideremotes != $oldprefs(hideremotes)} {
11039 rereadrefs
11040 }
712fcc08
PM
11041}
11042
11043proc formatdate {d} {
e8b5f4be 11044 global datetimeformat
219ea3a9 11045 if {$d ne {}} {
f5974d97 11046 set d [clock format [lindex $d 0] -format $datetimeformat]
219ea3a9
PM
11047 }
11048 return $d
232475d3
PM
11049}
11050
fd8ccbec
PM
11051# This list of encoding names and aliases is distilled from
11052# http://www.iana.org/assignments/character-sets.
11053# Not all of them are supported by Tcl.
11054set encoding_aliases {
11055 { ANSI_X3.4-1968 iso-ir-6 ANSI_X3.4-1986 ISO_646.irv:1991 ASCII
11056 ISO646-US US-ASCII us IBM367 cp367 csASCII }
11057 { ISO-10646-UTF-1 csISO10646UTF1 }
11058 { ISO_646.basic:1983 ref csISO646basic1983 }
11059 { INVARIANT csINVARIANT }
11060 { ISO_646.irv:1983 iso-ir-2 irv csISO2IntlRefVersion }
11061 { BS_4730 iso-ir-4 ISO646-GB gb uk csISO4UnitedKingdom }
11062 { NATS-SEFI iso-ir-8-1 csNATSSEFI }
11063 { NATS-SEFI-ADD iso-ir-8-2 csNATSSEFIADD }
11064 { NATS-DANO iso-ir-9-1 csNATSDANO }
11065 { NATS-DANO-ADD iso-ir-9-2 csNATSDANOADD }
11066 { SEN_850200_B iso-ir-10 FI ISO646-FI ISO646-SE se csISO10Swedish }
11067 { SEN_850200_C iso-ir-11 ISO646-SE2 se2 csISO11SwedishForNames }
11068 { KS_C_5601-1987 iso-ir-149 KS_C_5601-1989 KSC_5601 korean csKSC56011987 }
11069 { ISO-2022-KR csISO2022KR }
11070 { EUC-KR csEUCKR }
11071 { ISO-2022-JP csISO2022JP }
11072 { ISO-2022-JP-2 csISO2022JP2 }
11073 { JIS_C6220-1969-jp JIS_C6220-1969 iso-ir-13 katakana x0201-7
11074 csISO13JISC6220jp }
11075 { JIS_C6220-1969-ro iso-ir-14 jp ISO646-JP csISO14JISC6220ro }
11076 { IT iso-ir-15 ISO646-IT csISO15Italian }
11077 { PT iso-ir-16 ISO646-PT csISO16Portuguese }
11078 { ES iso-ir-17 ISO646-ES csISO17Spanish }
11079 { greek7-old iso-ir-18 csISO18Greek7Old }
11080 { latin-greek iso-ir-19 csISO19LatinGreek }
11081 { DIN_66003 iso-ir-21 de ISO646-DE csISO21German }
11082 { NF_Z_62-010_(1973) iso-ir-25 ISO646-FR1 csISO25French }
11083 { Latin-greek-1 iso-ir-27 csISO27LatinGreek1 }
11084 { ISO_5427 iso-ir-37 csISO5427Cyrillic }
11085 { JIS_C6226-1978 iso-ir-42 csISO42JISC62261978 }
11086 { BS_viewdata iso-ir-47 csISO47BSViewdata }
11087 { INIS iso-ir-49 csISO49INIS }
11088 { INIS-8 iso-ir-50 csISO50INIS8 }
11089 { INIS-cyrillic iso-ir-51 csISO51INISCyrillic }
11090 { ISO_5427:1981 iso-ir-54 ISO5427Cyrillic1981 }
11091 { ISO_5428:1980 iso-ir-55 csISO5428Greek }
11092 { GB_1988-80 iso-ir-57 cn ISO646-CN csISO57GB1988 }
11093 { GB_2312-80 iso-ir-58 chinese csISO58GB231280 }
11094 { NS_4551-1 iso-ir-60 ISO646-NO no csISO60DanishNorwegian
11095 csISO60Norwegian1 }
11096 { NS_4551-2 ISO646-NO2 iso-ir-61 no2 csISO61Norwegian2 }
11097 { NF_Z_62-010 iso-ir-69 ISO646-FR fr csISO69French }
11098 { videotex-suppl iso-ir-70 csISO70VideotexSupp1 }
11099 { PT2 iso-ir-84 ISO646-PT2 csISO84Portuguese2 }
11100 { ES2 iso-ir-85 ISO646-ES2 csISO85Spanish2 }
11101 { MSZ_7795.3 iso-ir-86 ISO646-HU hu csISO86Hungarian }
11102 { JIS_C6226-1983 iso-ir-87 x0208 JIS_X0208-1983 csISO87JISX0208 }
11103 { greek7 iso-ir-88 csISO88Greek7 }
11104 { ASMO_449 ISO_9036 arabic7 iso-ir-89 csISO89ASMO449 }
11105 { iso-ir-90 csISO90 }
11106 { JIS_C6229-1984-a iso-ir-91 jp-ocr-a csISO91JISC62291984a }
11107 { JIS_C6229-1984-b iso-ir-92 ISO646-JP-OCR-B jp-ocr-b
11108 csISO92JISC62991984b }
11109 { JIS_C6229-1984-b-add iso-ir-93 jp-ocr-b-add csISO93JIS62291984badd }
11110 { JIS_C6229-1984-hand iso-ir-94 jp-ocr-hand csISO94JIS62291984hand }
11111 { JIS_C6229-1984-hand-add iso-ir-95 jp-ocr-hand-add
11112 csISO95JIS62291984handadd }
11113 { JIS_C6229-1984-kana iso-ir-96 csISO96JISC62291984kana }
11114 { ISO_2033-1983 iso-ir-98 e13b csISO2033 }
11115 { ANSI_X3.110-1983 iso-ir-99 CSA_T500-1983 NAPLPS csISO99NAPLPS }
11116 { ISO_8859-1:1987 iso-ir-100 ISO_8859-1 ISO-8859-1 latin1 l1 IBM819
11117 CP819 csISOLatin1 }
11118 { ISO_8859-2:1987 iso-ir-101 ISO_8859-2 ISO-8859-2 latin2 l2 csISOLatin2 }
11119 { T.61-7bit iso-ir-102 csISO102T617bit }
11120 { T.61-8bit T.61 iso-ir-103 csISO103T618bit }
11121 { ISO_8859-3:1988 iso-ir-109 ISO_8859-3 ISO-8859-3 latin3 l3 csISOLatin3 }
11122 { ISO_8859-4:1988 iso-ir-110 ISO_8859-4 ISO-8859-4 latin4 l4 csISOLatin4 }
11123 { ECMA-cyrillic iso-ir-111 KOI8-E csISO111ECMACyrillic }
11124 { CSA_Z243.4-1985-1 iso-ir-121 ISO646-CA csa7-1 ca csISO121Canadian1 }
11125 { CSA_Z243.4-1985-2 iso-ir-122 ISO646-CA2 csa7-2 csISO122Canadian2 }
11126 { CSA_Z243.4-1985-gr iso-ir-123 csISO123CSAZ24341985gr }
11127 { ISO_8859-6:1987 iso-ir-127 ISO_8859-6 ISO-8859-6 ECMA-114 ASMO-708
11128 arabic csISOLatinArabic }
11129 { ISO_8859-6-E csISO88596E ISO-8859-6-E }
11130 { ISO_8859-6-I csISO88596I ISO-8859-6-I }
11131 { ISO_8859-7:1987 iso-ir-126 ISO_8859-7 ISO-8859-7 ELOT_928 ECMA-118
11132 greek greek8 csISOLatinGreek }
11133 { T.101-G2 iso-ir-128 csISO128T101G2 }
11134 { ISO_8859-8:1988 iso-ir-138 ISO_8859-8 ISO-8859-8 hebrew
11135 csISOLatinHebrew }
11136 { ISO_8859-8-E csISO88598E ISO-8859-8-E }
11137 { ISO_8859-8-I csISO88598I ISO-8859-8-I }
11138 { CSN_369103 iso-ir-139 csISO139CSN369103 }
11139 { JUS_I.B1.002 iso-ir-141 ISO646-YU js yu csISO141JUSIB1002 }
11140 { ISO_6937-2-add iso-ir-142 csISOTextComm }
11141 { IEC_P27-1 iso-ir-143 csISO143IECP271 }
11142 { ISO_8859-5:1988 iso-ir-144 ISO_8859-5 ISO-8859-5 cyrillic
11143 csISOLatinCyrillic }
11144 { JUS_I.B1.003-serb iso-ir-146 serbian csISO146Serbian }
11145 { JUS_I.B1.003-mac macedonian iso-ir-147 csISO147Macedonian }
11146 { ISO_8859-9:1989 iso-ir-148 ISO_8859-9 ISO-8859-9 latin5 l5 csISOLatin5 }
11147 { greek-ccitt iso-ir-150 csISO150 csISO150GreekCCITT }
11148 { NC_NC00-10:81 cuba iso-ir-151 ISO646-CU csISO151Cuba }
11149 { ISO_6937-2-25 iso-ir-152 csISO6937Add }
11150 { GOST_19768-74 ST_SEV_358-88 iso-ir-153 csISO153GOST1976874 }
11151 { ISO_8859-supp iso-ir-154 latin1-2-5 csISO8859Supp }
11152 { ISO_10367-box iso-ir-155 csISO10367Box }
11153 { ISO-8859-10 iso-ir-157 l6 ISO_8859-10:1992 csISOLatin6 latin6 }
11154 { latin-lap lap iso-ir-158 csISO158Lap }
11155 { JIS_X0212-1990 x0212 iso-ir-159 csISO159JISX02121990 }
11156 { DS_2089 DS2089 ISO646-DK dk csISO646Danish }
11157 { us-dk csUSDK }
11158 { dk-us csDKUS }
11159 { JIS_X0201 X0201 csHalfWidthKatakana }
11160 { KSC5636 ISO646-KR csKSC5636 }
11161 { ISO-10646-UCS-2 csUnicode }
11162 { ISO-10646-UCS-4 csUCS4 }
11163 { DEC-MCS dec csDECMCS }
11164 { hp-roman8 roman8 r8 csHPRoman8 }
11165 { macintosh mac csMacintosh }
11166 { IBM037 cp037 ebcdic-cp-us ebcdic-cp-ca ebcdic-cp-wt ebcdic-cp-nl
11167 csIBM037 }
11168 { IBM038 EBCDIC-INT cp038 csIBM038 }
11169 { IBM273 CP273 csIBM273 }
11170 { IBM274 EBCDIC-BE CP274 csIBM274 }
11171 { IBM275 EBCDIC-BR cp275 csIBM275 }
11172 { IBM277 EBCDIC-CP-DK EBCDIC-CP-NO csIBM277 }
11173 { IBM278 CP278 ebcdic-cp-fi ebcdic-cp-se csIBM278 }
11174 { IBM280 CP280 ebcdic-cp-it csIBM280 }
11175 { IBM281 EBCDIC-JP-E cp281 csIBM281 }
11176 { IBM284 CP284 ebcdic-cp-es csIBM284 }
11177 { IBM285 CP285 ebcdic-cp-gb csIBM285 }
11178 { IBM290 cp290 EBCDIC-JP-kana csIBM290 }
11179 { IBM297 cp297 ebcdic-cp-fr csIBM297 }
11180 { IBM420 cp420 ebcdic-cp-ar1 csIBM420 }
11181 { IBM423 cp423 ebcdic-cp-gr csIBM423 }
11182 { IBM424 cp424 ebcdic-cp-he csIBM424 }
11183 { IBM437 cp437 437 csPC8CodePage437 }
11184 { IBM500 CP500 ebcdic-cp-be ebcdic-cp-ch csIBM500 }
11185 { IBM775 cp775 csPC775Baltic }
11186 { IBM850 cp850 850 csPC850Multilingual }
11187 { IBM851 cp851 851 csIBM851 }
11188 { IBM852 cp852 852 csPCp852 }
11189 { IBM855 cp855 855 csIBM855 }
11190 { IBM857 cp857 857 csIBM857 }
11191 { IBM860 cp860 860 csIBM860 }
11192 { IBM861 cp861 861 cp-is csIBM861 }
11193 { IBM862 cp862 862 csPC862LatinHebrew }
11194 { IBM863 cp863 863 csIBM863 }
11195 { IBM864 cp864 csIBM864 }
11196 { IBM865 cp865 865 csIBM865 }
11197 { IBM866 cp866 866 csIBM866 }
11198 { IBM868 CP868 cp-ar csIBM868 }
11199 { IBM869 cp869 869 cp-gr csIBM869 }
11200 { IBM870 CP870 ebcdic-cp-roece ebcdic-cp-yu csIBM870 }
11201 { IBM871 CP871 ebcdic-cp-is csIBM871 }
11202 { IBM880 cp880 EBCDIC-Cyrillic csIBM880 }
11203 { IBM891 cp891 csIBM891 }
11204 { IBM903 cp903 csIBM903 }
11205 { IBM904 cp904 904 csIBBM904 }
11206 { IBM905 CP905 ebcdic-cp-tr csIBM905 }
11207 { IBM918 CP918 ebcdic-cp-ar2 csIBM918 }
11208 { IBM1026 CP1026 csIBM1026 }
11209 { EBCDIC-AT-DE csIBMEBCDICATDE }
11210 { EBCDIC-AT-DE-A csEBCDICATDEA }
11211 { EBCDIC-CA-FR csEBCDICCAFR }
11212 { EBCDIC-DK-NO csEBCDICDKNO }
11213 { EBCDIC-DK-NO-A csEBCDICDKNOA }
11214 { EBCDIC-FI-SE csEBCDICFISE }
11215 { EBCDIC-FI-SE-A csEBCDICFISEA }
11216 { EBCDIC-FR csEBCDICFR }
11217 { EBCDIC-IT csEBCDICIT }
11218 { EBCDIC-PT csEBCDICPT }
11219 { EBCDIC-ES csEBCDICES }
11220 { EBCDIC-ES-A csEBCDICESA }
11221 { EBCDIC-ES-S csEBCDICESS }
11222 { EBCDIC-UK csEBCDICUK }
11223 { EBCDIC-US csEBCDICUS }
11224 { UNKNOWN-8BIT csUnknown8BiT }
11225 { MNEMONIC csMnemonic }
11226 { MNEM csMnem }
11227 { VISCII csVISCII }
11228 { VIQR csVIQR }
11229 { KOI8-R csKOI8R }
11230 { IBM00858 CCSID00858 CP00858 PC-Multilingual-850+euro }
11231 { IBM00924 CCSID00924 CP00924 ebcdic-Latin9--euro }
11232 { IBM01140 CCSID01140 CP01140 ebcdic-us-37+euro }
11233 { IBM01141 CCSID01141 CP01141 ebcdic-de-273+euro }
11234 { IBM01142 CCSID01142 CP01142 ebcdic-dk-277+euro ebcdic-no-277+euro }
11235 { IBM01143 CCSID01143 CP01143 ebcdic-fi-278+euro ebcdic-se-278+euro }
11236 { IBM01144 CCSID01144 CP01144 ebcdic-it-280+euro }
11237 { IBM01145 CCSID01145 CP01145 ebcdic-es-284+euro }
11238 { IBM01146 CCSID01146 CP01146 ebcdic-gb-285+euro }
11239 { IBM01147 CCSID01147 CP01147 ebcdic-fr-297+euro }
11240 { IBM01148 CCSID01148 CP01148 ebcdic-international-500+euro }
11241 { IBM01149 CCSID01149 CP01149 ebcdic-is-871+euro }
11242 { IBM1047 IBM-1047 }
11243 { PTCP154 csPTCP154 PT154 CP154 Cyrillic-Asian }
11244 { Amiga-1251 Ami1251 Amiga1251 Ami-1251 }
11245 { UNICODE-1-1 csUnicode11 }
11246 { CESU-8 csCESU-8 }
11247 { BOCU-1 csBOCU-1 }
11248 { UNICODE-1-1-UTF-7 csUnicode11UTF7 }
11249 { ISO-8859-14 iso-ir-199 ISO_8859-14:1998 ISO_8859-14 latin8 iso-celtic
11250 l8 }
11251 { ISO-8859-15 ISO_8859-15 Latin-9 }
11252 { ISO-8859-16 iso-ir-226 ISO_8859-16:2001 ISO_8859-16 latin10 l10 }
11253 { GBK CP936 MS936 windows-936 }
11254 { JIS_Encoding csJISEncoding }
09c7029d 11255 { Shift_JIS MS_Kanji csShiftJIS ShiftJIS Shift-JIS }
fd8ccbec
PM
11256 { Extended_UNIX_Code_Packed_Format_for_Japanese csEUCPkdFmtJapanese
11257 EUC-JP }
11258 { Extended_UNIX_Code_Fixed_Width_for_Japanese csEUCFixWidJapanese }
11259 { ISO-10646-UCS-Basic csUnicodeASCII }
11260 { ISO-10646-Unicode-Latin1 csUnicodeLatin1 ISO-10646 }
11261 { ISO-Unicode-IBM-1261 csUnicodeIBM1261 }
11262 { ISO-Unicode-IBM-1268 csUnicodeIBM1268 }
11263 { ISO-Unicode-IBM-1276 csUnicodeIBM1276 }
11264 { ISO-Unicode-IBM-1264 csUnicodeIBM1264 }
11265 { ISO-Unicode-IBM-1265 csUnicodeIBM1265 }
11266 { ISO-8859-1-Windows-3.0-Latin-1 csWindows30Latin1 }
11267 { ISO-8859-1-Windows-3.1-Latin-1 csWindows31Latin1 }
11268 { ISO-8859-2-Windows-Latin-2 csWindows31Latin2 }
11269 { ISO-8859-9-Windows-Latin-5 csWindows31Latin5 }
11270 { Adobe-Standard-Encoding csAdobeStandardEncoding }
11271 { Ventura-US csVenturaUS }
11272 { Ventura-International csVenturaInternational }
11273 { PC8-Danish-Norwegian csPC8DanishNorwegian }
11274 { PC8-Turkish csPC8Turkish }
11275 { IBM-Symbols csIBMSymbols }
11276 { IBM-Thai csIBMThai }
11277 { HP-Legal csHPLegal }
11278 { HP-Pi-font csHPPiFont }
11279 { HP-Math8 csHPMath8 }
11280 { Adobe-Symbol-Encoding csHPPSMath }
11281 { HP-DeskTop csHPDesktop }
11282 { Ventura-Math csVenturaMath }
11283 { Microsoft-Publishing csMicrosoftPublishing }
11284 { Windows-31J csWindows31J }
11285 { GB2312 csGB2312 }
11286 { Big5 csBig5 }
11287}
11288
11289proc tcl_encoding {enc} {
39ee47ef
PM
11290 global encoding_aliases tcl_encoding_cache
11291 if {[info exists tcl_encoding_cache($enc)]} {
11292 return $tcl_encoding_cache($enc)
11293 }
fd8ccbec
PM
11294 set names [encoding names]
11295 set lcnames [string tolower $names]
11296 set enc [string tolower $enc]
11297 set i [lsearch -exact $lcnames $enc]
11298 if {$i < 0} {
11299 # look for "isonnn" instead of "iso-nnn" or "iso_nnn"
09c7029d 11300 if {[regsub {^(iso|cp|ibm|jis)[-_]} $enc {\1} encx]} {
fd8ccbec
PM
11301 set i [lsearch -exact $lcnames $encx]
11302 }
11303 }
11304 if {$i < 0} {
11305 foreach l $encoding_aliases {
11306 set ll [string tolower $l]
11307 if {[lsearch -exact $ll $enc] < 0} continue
11308 # look through the aliases for one that tcl knows about
11309 foreach e $ll {
11310 set i [lsearch -exact $lcnames $e]
11311 if {$i < 0} {
09c7029d 11312 if {[regsub {^(iso|cp|ibm|jis)[-_]} $e {\1} ex]} {
fd8ccbec
PM
11313 set i [lsearch -exact $lcnames $ex]
11314 }
11315 }
11316 if {$i >= 0} break
11317 }
11318 break
11319 }
11320 }
39ee47ef 11321 set tclenc {}
fd8ccbec 11322 if {$i >= 0} {
39ee47ef 11323 set tclenc [lindex $names $i]
fd8ccbec 11324 }
39ee47ef
PM
11325 set tcl_encoding_cache($enc) $tclenc
11326 return $tclenc
fd8ccbec
PM
11327}
11328
09c7029d 11329proc gitattr {path attr default} {
39ee47ef
PM
11330 global path_attr_cache
11331 if {[info exists path_attr_cache($attr,$path)]} {
11332 set r $path_attr_cache($attr,$path)
11333 } else {
11334 set r "unspecified"
11335 if {![catch {set line [exec git check-attr $attr -- $path]}]} {
097e1118 11336 regexp "(.*): $attr: (.*)" $line m f r
09c7029d 11337 }
4db09304 11338 set path_attr_cache($attr,$path) $r
39ee47ef
PM
11339 }
11340 if {$r eq "unspecified"} {
11341 return $default
11342 }
11343 return $r
09c7029d
AG
11344}
11345
4db09304 11346proc cache_gitattr {attr pathlist} {
39ee47ef
PM
11347 global path_attr_cache
11348 set newlist {}
11349 foreach path $pathlist {
11350 if {![info exists path_attr_cache($attr,$path)]} {
11351 lappend newlist $path
11352 }
11353 }
11354 set lim 1000
11355 if {[tk windowingsystem] == "win32"} {
11356 # windows has a 32k limit on the arguments to a command...
11357 set lim 30
11358 }
11359 while {$newlist ne {}} {
11360 set head [lrange $newlist 0 [expr {$lim - 1}]]
11361 set newlist [lrange $newlist $lim end]
11362 if {![catch {set rlist [eval exec git check-attr $attr -- $head]}]} {
11363 foreach row [split $rlist "\n"] {
097e1118 11364 if {[regexp "(.*): $attr: (.*)" $row m path value]} {
39ee47ef
PM
11365 if {[string index $path 0] eq "\""} {
11366 set path [encoding convertfrom [lindex $path 0]]
11367 }
11368 set path_attr_cache($attr,$path) $value
4db09304 11369 }
39ee47ef 11370 }
4db09304 11371 }
39ee47ef 11372 }
4db09304
AG
11373}
11374
09c7029d 11375proc get_path_encoding {path} {
39ee47ef
PM
11376 global gui_encoding perfile_attrs
11377 set tcl_enc $gui_encoding
11378 if {$path ne {} && $perfile_attrs} {
11379 set enc2 [tcl_encoding [gitattr $path encoding $tcl_enc]]
11380 if {$enc2 ne {}} {
11381 set tcl_enc $enc2
09c7029d 11382 }
39ee47ef
PM
11383 }
11384 return $tcl_enc
09c7029d
AG
11385}
11386
5d7589d4
PM
11387# First check that Tcl/Tk is recent enough
11388if {[catch {package require Tk 8.4} err]} {
8d849957
BH
11389 show_error {} . "Sorry, gitk cannot run with this version of Tcl/Tk.\n\
11390 Gitk requires at least Tcl/Tk 8.4." list
5d7589d4
PM
11391 exit 1
11392}
11393
1d10f36d 11394# defaults...
8974c6f9 11395set wrcomcmd "git diff-tree --stdin -p --pretty"
671bc153 11396
fd8ccbec 11397set gitencoding {}
671bc153 11398catch {
27cb61ca 11399 set gitencoding [exec git config --get i18n.commitencoding]
671bc153 11400}
590915da
AG
11401catch {
11402 set gitencoding [exec git config --get i18n.logoutputencoding]
11403}
671bc153 11404if {$gitencoding == ""} {
fd8ccbec
PM
11405 set gitencoding "utf-8"
11406}
11407set tclencoding [tcl_encoding $gitencoding]
11408if {$tclencoding == {}} {
11409 puts stderr "Warning: encoding $gitencoding is not supported by Tcl/Tk"
671bc153 11410}
1db95b00 11411
09c7029d
AG
11412set gui_encoding [encoding system]
11413catch {
39ee47ef
PM
11414 set enc [exec git config --get gui.encoding]
11415 if {$enc ne {}} {
11416 set tclenc [tcl_encoding $enc]
11417 if {$tclenc ne {}} {
11418 set gui_encoding $tclenc
11419 } else {
11420 puts stderr "Warning: encoding $enc is not supported by Tcl/Tk"
11421 }
11422 }
09c7029d
AG
11423}
11424
5fdcbb13
DS
11425if {[tk windowingsystem] eq "aqua"} {
11426 set mainfont {{Lucida Grande} 9}
11427 set textfont {Monaco 9}
11428 set uifont {{Lucida Grande} 9 bold}
11429} else {
11430 set mainfont {Helvetica 9}
11431 set textfont {Courier 9}
11432 set uifont {Helvetica 9 bold}
11433}
7e12f1a6 11434set tabstop 8
b74fd579 11435set findmergefiles 0
8d858d1a 11436set maxgraphpct 50
f6075eba 11437set maxwidth 16
232475d3 11438set revlistorder 0
757f17bc 11439set fastdate 0
6e8c8707
PM
11440set uparrowlen 5
11441set downarrowlen 5
11442set mingaplen 100
f8b28a40 11443set cmitmode "patch"
f1b86294 11444set wrapcomment "none"
b8ab2e17 11445set showneartags 1
ffe15297 11446set hideremotes 0
0a4dd8b8 11447set maxrefs 20
322a8cc9 11448set maxlinelen 200
219ea3a9 11449set showlocalchanges 1
7a39a17a 11450set limitdiffs 1
e8b5f4be 11451set datetimeformat "%Y-%m-%d %H:%M:%S"
95293b58 11452set autoselect 1
21ac8a8d 11453set autosellen 40
39ee47ef 11454set perfile_attrs 0
0cc08ff7 11455set want_ttk 1
1d10f36d 11456
5fdcbb13
DS
11457if {[tk windowingsystem] eq "aqua"} {
11458 set extdifftool "opendiff"
11459} else {
11460 set extdifftool "meld"
11461}
314f5de1 11462
1d10f36d 11463set colors {green red blue magenta darkgrey brown orange}
1924d1bc
PT
11464if {[tk windowingsystem] eq "win32"} {
11465 set uicolor SystemButtonFace
11466 set bgcolor SystemWindow
11467 set fgcolor SystemButtonText
11468 set selectbgcolor SystemHighlight
11469} else {
11470 set uicolor grey85
11471 set bgcolor white
11472 set fgcolor black
11473 set selectbgcolor gray85
11474}
f8a2c0d1 11475set diffcolors {red "#00a000" blue}
890fae70 11476set diffcontext 3
b9b86007 11477set ignorespace 0
ae4e3ff9 11478set worddiff ""
e3e901be 11479set markbgcolor "#e0e0ff"
1d10f36d 11480
c11ff120
PM
11481set circlecolors {white blue gray blue blue}
11482
d277e89f
PM
11483# button for popping up context menus
11484if {[tk windowingsystem] eq "aqua"} {
11485 set ctxbut <Button-2>
11486} else {
11487 set ctxbut <Button-3>
11488}
11489
663c3aa9
CS
11490## For msgcat loading, first locate the installation location.
11491if { [info exists ::env(GITK_MSGSDIR)] } {
11492 ## Msgsdir was manually set in the environment.
11493 set gitk_msgsdir $::env(GITK_MSGSDIR)
11494} else {
11495 ## Let's guess the prefix from argv0.
11496 set gitk_prefix [file dirname [file dirname [file normalize $argv0]]]
11497 set gitk_libdir [file join $gitk_prefix share gitk lib]
11498 set gitk_msgsdir [file join $gitk_libdir msgs]
11499 unset gitk_prefix
11500}
11501
11502## Internationalization (i18n) through msgcat and gettext. See
11503## http://www.gnu.org/software/gettext/manual/html_node/Tcl.html
11504package require msgcat
11505namespace import ::msgcat::mc
11506## And eventually load the actual message catalog
11507::msgcat::mcload $gitk_msgsdir
11508
1d10f36d
PM
11509catch {source ~/.gitk}
11510
0ed1dd3c
PM
11511parsefont mainfont $mainfont
11512eval font create mainfont [fontflags mainfont]
11513eval font create mainfontbold [fontflags mainfont 1]
11514
11515parsefont textfont $textfont
11516eval font create textfont [fontflags textfont]
11517eval font create textfontbold [fontflags textfont 1]
11518
11519parsefont uifont $uifont
11520eval font create uifont [fontflags uifont]
17386066 11521
51a7e8b6 11522setui $uicolor
5497f7a2 11523
b039f0a6
PM
11524setoptions
11525
cdaee5db 11526# check that we can find a .git directory somewhere...
86e847bc 11527if {[catch {set gitdir [exec git rev-parse --git-dir]}]} {
d990cedf 11528 show_error {} . [mc "Cannot find a git repository here."]
6c87d60c
AR
11529 exit 1
11530}
cdaee5db 11531
39816d60
AG
11532set selecthead {}
11533set selectheadid {}
11534
1d10f36d 11535set revtreeargs {}
cdaee5db
PM
11536set cmdline_files {}
11537set i 0
2d480856 11538set revtreeargscmd {}
1d10f36d 11539foreach arg $argv {
2d480856 11540 switch -glob -- $arg {
6ebedabf 11541 "" { }
cdaee5db
PM
11542 "--" {
11543 set cmdline_files [lrange $argv [expr {$i + 1}] end]
11544 break
11545 }
39816d60
AG
11546 "--select-commit=*" {
11547 set selecthead [string range $arg 16 end]
11548 }
2d480856
YD
11549 "--argscmd=*" {
11550 set revtreeargscmd [string range $arg 10 end]
11551 }
1d10f36d
PM
11552 default {
11553 lappend revtreeargs $arg
11554 }
11555 }
cdaee5db 11556 incr i
1db95b00 11557}
1d10f36d 11558
39816d60
AG
11559if {$selecthead eq "HEAD"} {
11560 set selecthead {}
11561}
11562
cdaee5db 11563if {$i >= [llength $argv] && $revtreeargs ne {}} {
3ed31a81 11564 # no -- on command line, but some arguments (other than --argscmd)
098dd8a3 11565 if {[catch {
8974c6f9 11566 set f [eval exec git rev-parse --no-revs --no-flags $revtreeargs]
098dd8a3
PM
11567 set cmdline_files [split $f "\n"]
11568 set n [llength $cmdline_files]
11569 set revtreeargs [lrange $revtreeargs 0 end-$n]
cdaee5db
PM
11570 # Unfortunately git rev-parse doesn't produce an error when
11571 # something is both a revision and a filename. To be consistent
11572 # with git log and git rev-list, check revtreeargs for filenames.
11573 foreach arg $revtreeargs {
11574 if {[file exists $arg]} {
d990cedf
CS
11575 show_error {} . [mc "Ambiguous argument '%s': both revision\
11576 and filename" $arg]
cdaee5db
PM
11577 exit 1
11578 }
11579 }
098dd8a3
PM
11580 } err]} {
11581 # unfortunately we get both stdout and stderr in $err,
11582 # so look for "fatal:".
11583 set i [string first "fatal:" $err]
11584 if {$i > 0} {
b5e09633 11585 set err [string range $err [expr {$i + 6}] end]
098dd8a3 11586 }
d990cedf 11587 show_error {} . "[mc "Bad arguments to gitk:"]\n$err"
098dd8a3
PM
11588 exit 1
11589 }
11590}
11591
219ea3a9 11592set nullid "0000000000000000000000000000000000000000"
8f489363 11593set nullid2 "0000000000000000000000000000000000000001"
314f5de1 11594set nullfile "/dev/null"
8f489363 11595
32f1b3e4 11596set have_tk85 [expr {[package vcompare $tk_version "8.5"] >= 0}]
0cc08ff7
PM
11597if {![info exists have_ttk]} {
11598 set have_ttk [llength [info commands ::ttk::style]]
d93f1713 11599}
0cc08ff7 11600set use_ttk [expr {$have_ttk && $want_ttk}]
d93f1713 11601set NS [expr {$use_ttk ? "ttk" : ""}]
0cc08ff7 11602
7add5aff 11603regexp {^git version ([\d.]*\d)} [exec git version] _ git_version
219ea3a9 11604
7defefb1
KS
11605set show_notes {}
11606if {[package vcompare $git_version "1.6.6.2"] >= 0} {
11607 set show_notes "--show-notes"
11608}
11609
7eb3cb9c 11610set runq {}
d698206c
PM
11611set history {}
11612set historyindex 0
908c3585 11613set fh_serial 0
908c3585 11614set nhl_names {}
63b79191 11615set highlight_paths {}
687c8765 11616set findpattern {}
1902c270 11617set searchdirn -forwards
28593d3f
PM
11618set boldids {}
11619set boldnameids {}
a8d610a2 11620set diffelide {0 0}
4fb0fa19 11621set markingmatches 0
97645683 11622set linkentercount 0
0380081c
PM
11623set need_redisplay 0
11624set nrows_drawn 0
32f1b3e4 11625set firsttabstop 0
9f1afe05 11626
50b44ece
PM
11627set nextviewnum 1
11628set curview 0
a90a6d24 11629set selectedview 0
b007ee20
CS
11630set selectedhlview [mc "None"]
11631set highlight_related [mc "None"]
687c8765 11632set highlight_files {}
50b44ece 11633set viewfiles(0) {}
a90a6d24 11634set viewperm(0) 0
098dd8a3 11635set viewargs(0) {}
2d480856 11636set viewargscmd(0) {}
50b44ece 11637
94b4a69f 11638set selectedline {}
6df7403a 11639set numcommits 0
7fcc92bf 11640set loginstance 0
098dd8a3 11641set cmdlineok 0
1d10f36d 11642set stopped 0
0fba86b3 11643set stuffsaved 0
74daedb6 11644set patchnum 0
219ea3a9 11645set lserial 0
74cb884f 11646set hasworktree [hasworktree]
c332f445 11647set cdup {}
74cb884f 11648if {[expr {[exec git rev-parse --is-inside-work-tree] == "true"}]} {
c332f445
MZ
11649 set cdup [exec git rev-parse --show-cdup]
11650}
784b7e2f 11651set worktree [exec git rev-parse --show-toplevel]
1d10f36d 11652setcoords
d94f8cd6 11653makewindow
37871b73
GB
11654catch {
11655 image create photo gitlogo -width 16 -height 16
11656
11657 image create photo gitlogominus -width 4 -height 2
11658 gitlogominus put #C00000 -to 0 0 4 2
11659 gitlogo copy gitlogominus -to 1 5
11660 gitlogo copy gitlogominus -to 6 5
11661 gitlogo copy gitlogominus -to 11 5
11662 image delete gitlogominus
11663
11664 image create photo gitlogoplus -width 4 -height 4
11665 gitlogoplus put #008000 -to 1 0 3 4
11666 gitlogoplus put #008000 -to 0 1 4 3
11667 gitlogo copy gitlogoplus -to 1 9
11668 gitlogo copy gitlogoplus -to 6 9
11669 gitlogo copy gitlogoplus -to 11 9
11670 image delete gitlogoplus
11671
d38d7d49
SB
11672 image create photo gitlogo32 -width 32 -height 32
11673 gitlogo32 copy gitlogo -zoom 2 2
11674
11675 wm iconphoto . -default gitlogo gitlogo32
37871b73 11676}
0eafba14
PM
11677# wait for the window to become visible
11678tkwait visibility .
6c283328 11679wm title . "[file tail $argv0]: [file tail [pwd]]"
478afad6 11680update
887fe3c4 11681readrefs
a8aaf19c 11682
2d480856 11683if {$cmdline_files ne {} || $revtreeargs ne {} || $revtreeargscmd ne {}} {
50b44ece
PM
11684 # create a view for the files/dirs specified on the command line
11685 set curview 1
a90a6d24 11686 set selectedview 1
50b44ece 11687 set nextviewnum 2
d990cedf 11688 set viewname(1) [mc "Command line"]
50b44ece 11689 set viewfiles(1) $cmdline_files
098dd8a3 11690 set viewargs(1) $revtreeargs
2d480856 11691 set viewargscmd(1) $revtreeargscmd
a90a6d24 11692 set viewperm(1) 0
3ed31a81 11693 set vdatemode(1) 0
da7c24dd 11694 addviewmenu 1
f2d0bbbd
PM
11695 .bar.view entryconf [mca "Edit view..."] -state normal
11696 .bar.view entryconf [mca "Delete view"] -state normal
50b44ece 11697}
a90a6d24
PM
11698
11699if {[info exists permviews]} {
11700 foreach v $permviews {
11701 set n $nextviewnum
11702 incr nextviewnum
11703 set viewname($n) [lindex $v 0]
11704 set viewfiles($n) [lindex $v 1]
098dd8a3 11705 set viewargs($n) [lindex $v 2]
2d480856 11706 set viewargscmd($n) [lindex $v 3]
a90a6d24 11707 set viewperm($n) 1
da7c24dd 11708 addviewmenu $n
a90a6d24
PM
11709 }
11710}
e4df519f
JS
11711
11712if {[tk windowingsystem] eq "win32"} {
11713 focus -force .
11714}
11715
567c34e0 11716getcommits {}
adab0dab
PT
11717
11718# Local variables:
11719# mode: tcl
11720# indent-tabs-mode: t
11721# tab-width: 8
11722# End: