]> git.ipfire.org Git - thirdparty/git.git/blame - gitk-git/gitk
send-email: Cleanup smtp-domain and add config
[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
aa43561a 5# Copyright © 2005-2009 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
73b6a6cb
JH
12proc gitdir {} {
13 global env
14 if {[info exists env(GIT_DIR)]} {
15 return $env(GIT_DIR)
16 } else {
5024baa4 17 return [exec git rev-parse --git-dir]
73b6a6cb
JH
18 }
19}
20
7eb3cb9c
PM
21# A simple scheduler for compute-intensive stuff.
22# The aim is to make sure that event handlers for GUI actions can
23# run at least every 50-100 ms. Unfortunately fileevent handlers are
24# run before X event handlers, so reading from a fast source can
25# make the GUI completely unresponsive.
26proc run args {
df75e86d 27 global isonrunq runq currunq
7eb3cb9c
PM
28
29 set script $args
30 if {[info exists isonrunq($script)]} return
df75e86d 31 if {$runq eq {} && ![info exists currunq]} {
7eb3cb9c
PM
32 after idle dorunq
33 }
34 lappend runq [list {} $script]
35 set isonrunq($script) 1
36}
37
38proc filerun {fd script} {
39 fileevent $fd readable [list filereadable $fd $script]
40}
41
42proc filereadable {fd script} {
df75e86d 43 global runq currunq
7eb3cb9c
PM
44
45 fileevent $fd readable {}
df75e86d 46 if {$runq eq {} && ![info exists currunq]} {
7eb3cb9c
PM
47 after idle dorunq
48 }
49 lappend runq [list $fd $script]
50}
51
7fcc92bf
PM
52proc nukefile {fd} {
53 global runq
54
55 for {set i 0} {$i < [llength $runq]} {} {
56 if {[lindex $runq $i 0] eq $fd} {
57 set runq [lreplace $runq $i $i]
58 } else {
59 incr i
60 }
61 }
62}
63
7eb3cb9c 64proc dorunq {} {
df75e86d 65 global isonrunq runq currunq
7eb3cb9c
PM
66
67 set tstart [clock clicks -milliseconds]
68 set t0 $tstart
7fcc92bf 69 while {[llength $runq] > 0} {
7eb3cb9c
PM
70 set fd [lindex $runq 0 0]
71 set script [lindex $runq 0 1]
df75e86d
AG
72 set currunq [lindex $runq 0]
73 set runq [lrange $runq 1 end]
7eb3cb9c 74 set repeat [eval $script]
df75e86d 75 unset currunq
7eb3cb9c
PM
76 set t1 [clock clicks -milliseconds]
77 set t [expr {$t1 - $t0}]
7eb3cb9c
PM
78 if {$repeat ne {} && $repeat} {
79 if {$fd eq {} || $repeat == 2} {
80 # script returns 1 if it wants to be readded
81 # file readers return 2 if they could do more straight away
82 lappend runq [list $fd $script]
83 } else {
84 fileevent $fd readable [list filereadable $fd $script]
85 }
86 } elseif {$fd eq {}} {
87 unset isonrunq($script)
88 }
89 set t0 $t1
90 if {$t1 - $tstart >= 80} break
91 }
92 if {$runq ne {}} {
93 after idle dorunq
94 }
95}
96
e439e092
AG
97proc reg_instance {fd} {
98 global commfd leftover loginstance
99
100 set i [incr loginstance]
101 set commfd($i) $fd
102 set leftover($i) {}
103 return $i
104}
105
3ed31a81
PM
106proc unmerged_files {files} {
107 global nr_unmerged
108
109 # find the list of unmerged files
110 set mlist {}
111 set nr_unmerged 0
112 if {[catch {
113 set fd [open "| git ls-files -u" r]
114 } err]} {
115 show_error {} . "[mc "Couldn't get list of unmerged files:"] $err"
116 exit 1
117 }
118 while {[gets $fd line] >= 0} {
119 set i [string first "\t" $line]
120 if {$i < 0} continue
121 set fname [string range $line [expr {$i+1}] end]
122 if {[lsearch -exact $mlist $fname] >= 0} continue
123 incr nr_unmerged
124 if {$files eq {} || [path_filter $files $fname]} {
125 lappend mlist $fname
126 }
127 }
128 catch {close $fd}
129 return $mlist
130}
131
132proc parseviewargs {n arglist} {
c2f2dab9 133 global vdatemode vmergeonly vflags vdflags vrevs vfiltered vorigargs env
3ed31a81
PM
134
135 set vdatemode($n) 0
136 set vmergeonly($n) 0
ee66e089
PM
137 set glflags {}
138 set diffargs {}
139 set nextisval 0
140 set revargs {}
141 set origargs $arglist
142 set allknown 1
143 set filtered 0
144 set i -1
145 foreach arg $arglist {
146 incr i
147 if {$nextisval} {
148 lappend glflags $arg
149 set nextisval 0
150 continue
151 }
3ed31a81
PM
152 switch -glob -- $arg {
153 "-d" -
154 "--date-order" {
155 set vdatemode($n) 1
ee66e089
PM
156 # remove from origargs in case we hit an unknown option
157 set origargs [lreplace $origargs $i $i]
158 incr i -1
159 }
ee66e089
PM
160 "-[puabwcrRBMC]" -
161 "--no-renames" - "--full-index" - "--binary" - "--abbrev=*" -
162 "--find-copies-harder" - "-l*" - "--ext-diff" - "--no-ext-diff" -
163 "--src-prefix=*" - "--dst-prefix=*" - "--no-prefix" -
164 "-O*" - "--text" - "--full-diff" - "--ignore-space-at-eol" -
165 "--ignore-space-change" - "-U*" - "--unified=*" {
29582284
PM
166 # These request or affect diff output, which we don't want.
167 # Some could be used to set our defaults for diff display.
ee66e089
PM
168 lappend diffargs $arg
169 }
ee66e089
PM
170 "--raw" - "--patch-with-raw" - "--patch-with-stat" -
171 "--name-only" - "--name-status" - "--color" - "--color-words" -
172 "--log-size" - "--pretty=*" - "--decorate" - "--abbrev-commit" -
173 "--cc" - "-z" - "--header" - "--parents" - "--boundary" -
174 "--no-color" - "-g" - "--walk-reflogs" - "--no-walk" -
175 "--timestamp" - "relative-date" - "--date=*" - "--stdin" -
176 "--objects" - "--objects-edge" - "--reverse" {
29582284
PM
177 # These cause our parsing of git log's output to fail, or else
178 # they're options we want to set ourselves, so ignore them.
ee66e089 179 }
ee66e089
PM
180 "--stat=*" - "--numstat" - "--shortstat" - "--summary" -
181 "--check" - "--exit-code" - "--quiet" - "--topo-order" -
182 "--full-history" - "--dense" - "--sparse" -
183 "--follow" - "--left-right" - "--encoding=*" {
29582284 184 # These are harmless, and some are even useful
ee66e089
PM
185 lappend glflags $arg
186 }
ee66e089
PM
187 "--diff-filter=*" - "--no-merges" - "--unpacked" -
188 "--max-count=*" - "--skip=*" - "--since=*" - "--after=*" -
189 "--until=*" - "--before=*" - "--max-age=*" - "--min-age=*" -
190 "--author=*" - "--committer=*" - "--grep=*" - "-[iE]" -
191 "--remove-empty" - "--first-parent" - "--cherry-pick" -
f687aaa8
DS
192 "-S*" - "--pickaxe-all" - "--pickaxe-regex" -
193 "--simplify-by-decoration" {
29582284 194 # These mean that we get a subset of the commits
ee66e089
PM
195 set filtered 1
196 lappend glflags $arg
197 }
ee66e089 198 "-n" {
29582284
PM
199 # This appears to be the only one that has a value as a
200 # separate word following it
ee66e089
PM
201 set filtered 1
202 set nextisval 1
203 lappend glflags $arg
204 }
6e7e87c7 205 "--not" - "--all" {
ee66e089 206 lappend revargs $arg
3ed31a81
PM
207 }
208 "--merge" {
209 set vmergeonly($n) 1
ee66e089
PM
210 # git rev-parse doesn't understand --merge
211 lappend revargs --gitk-symmetric-diff-marker MERGE_HEAD...HEAD
212 }
c2f2dab9
CC
213 "--no-replace-objects" {
214 set env(GIT_NO_REPLACE_OBJECTS) "1"
215 }
ee66e089 216 "-*" {
29582284 217 # Other flag arguments including -<n>
ee66e089
PM
218 if {[string is digit -strict [string range $arg 1 end]]} {
219 set filtered 1
220 } else {
221 # a flag argument that we don't recognize;
222 # that means we can't optimize
223 set allknown 0
224 }
225 lappend glflags $arg
3ed31a81
PM
226 }
227 default {
29582284 228 # Non-flag arguments specify commits or ranges of commits
ee66e089
PM
229 if {[string match "*...*" $arg]} {
230 lappend revargs --gitk-symmetric-diff-marker
231 }
232 lappend revargs $arg
233 }
234 }
235 }
236 set vdflags($n) $diffargs
237 set vflags($n) $glflags
238 set vrevs($n) $revargs
239 set vfiltered($n) $filtered
240 set vorigargs($n) $origargs
241 return $allknown
242}
243
244proc parseviewrevs {view revs} {
245 global vposids vnegids
246
247 if {$revs eq {}} {
248 set revs HEAD
249 }
250 if {[catch {set ids [eval exec git rev-parse $revs]} err]} {
251 # we get stdout followed by stderr in $err
252 # for an unknown rev, git rev-parse echoes it and then errors out
253 set errlines [split $err "\n"]
254 set badrev {}
255 for {set l 0} {$l < [llength $errlines]} {incr l} {
256 set line [lindex $errlines $l]
257 if {!([string length $line] == 40 && [string is xdigit $line])} {
258 if {[string match "fatal:*" $line]} {
259 if {[string match "fatal: ambiguous argument*" $line]
260 && $badrev ne {}} {
261 if {[llength $badrev] == 1} {
262 set err "unknown revision $badrev"
263 } else {
264 set err "unknown revisions: [join $badrev ", "]"
265 }
266 } else {
267 set err [join [lrange $errlines $l end] "\n"]
268 }
269 break
270 }
271 lappend badrev $line
272 }
d93f1713 273 }
3945d2c0 274 error_popup "[mc "Error parsing revisions:"] $err"
ee66e089
PM
275 return {}
276 }
277 set ret {}
278 set pos {}
279 set neg {}
280 set sdm 0
281 foreach id [split $ids "\n"] {
282 if {$id eq "--gitk-symmetric-diff-marker"} {
283 set sdm 4
284 } elseif {[string match "^*" $id]} {
285 if {$sdm != 1} {
286 lappend ret $id
287 if {$sdm == 3} {
288 set sdm 0
289 }
290 }
291 lappend neg [string range $id 1 end]
292 } else {
293 if {$sdm != 2} {
294 lappend ret $id
295 } else {
2b1fbf90 296 lset ret end $id...[lindex $ret end]
3ed31a81 297 }
ee66e089 298 lappend pos $id
3ed31a81 299 }
ee66e089 300 incr sdm -1
3ed31a81 301 }
ee66e089
PM
302 set vposids($view) $pos
303 set vnegids($view) $neg
304 return $ret
3ed31a81
PM
305}
306
f9e0b6fb 307# Start off a git log process and arrange to read its output
da7c24dd 308proc start_rev_list {view} {
6df7403a 309 global startmsecs commitidx viewcomplete curview
e439e092 310 global tclencoding
ee66e089 311 global viewargs viewargscmd viewfiles vfilelimit
d375ef9b 312 global showlocalchanges
e439e092 313 global viewactive viewinstances vmergeonly
cdc8429c 314 global mainheadid viewmainheadid viewmainheadid_orig
ee66e089 315 global vcanopt vflags vrevs vorigargs
9ccbdfbf 316
9ccbdfbf 317 set startmsecs [clock clicks -milliseconds]
da7c24dd 318 set commitidx($view) 0
3ed31a81
PM
319 # these are set this way for the error exits
320 set viewcomplete($view) 1
321 set viewactive($view) 0
7fcc92bf
PM
322 varcinit $view
323
2d480856
YD
324 set args $viewargs($view)
325 if {$viewargscmd($view) ne {}} {
326 if {[catch {
327 set str [exec sh -c $viewargscmd($view)]
328 } err]} {
3945d2c0 329 error_popup "[mc "Error executing --argscmd command:"] $err"
3ed31a81 330 return 0
2d480856
YD
331 }
332 set args [concat $args [split $str "\n"]]
333 }
ee66e089 334 set vcanopt($view) [parseviewargs $view $args]
3ed31a81
PM
335
336 set files $viewfiles($view)
337 if {$vmergeonly($view)} {
338 set files [unmerged_files $files]
339 if {$files eq {}} {
340 global nr_unmerged
341 if {$nr_unmerged == 0} {
342 error_popup [mc "No files selected: --merge specified but\
343 no files are unmerged."]
344 } else {
345 error_popup [mc "No files selected: --merge specified but\
346 no unmerged files are within file limit."]
347 }
348 return 0
349 }
350 }
351 set vfilelimit($view) $files
352
ee66e089
PM
353 if {$vcanopt($view)} {
354 set revs [parseviewrevs $view $vrevs($view)]
355 if {$revs eq {}} {
356 return 0
357 }
358 set args [concat $vflags($view) $revs]
359 } else {
360 set args $vorigargs($view)
361 }
362
418c4c7b 363 if {[catch {
7fcc92bf 364 set fd [open [concat | git log --no-color -z --pretty=raw --parents \
3ed31a81 365 --boundary $args "--" $files] r]
418c4c7b 366 } err]} {
00abadb9 367 error_popup "[mc "Error executing git log:"] $err"
3ed31a81 368 return 0
1d10f36d 369 }
e439e092 370 set i [reg_instance $fd]
7fcc92bf 371 set viewinstances($view) [list $i]
cdc8429c
PM
372 set viewmainheadid($view) $mainheadid
373 set viewmainheadid_orig($view) $mainheadid
374 if {$files ne {} && $mainheadid ne {}} {
375 get_viewmainhead $view
376 }
377 if {$showlocalchanges && $viewmainheadid($view) ne {}} {
378 interestedin $viewmainheadid($view) dodiffindex
3e6b893f 379 }
86da5b6c 380 fconfigure $fd -blocking 0 -translation lf -eofchar {}
fd8ccbec 381 if {$tclencoding != {}} {
da7c24dd 382 fconfigure $fd -encoding $tclencoding
fd8ccbec 383 }
f806f0fb 384 filerun $fd [list getcommitlines $fd $i $view 0]
d990cedf 385 nowbusy $view [mc "Reading"]
3ed31a81
PM
386 set viewcomplete($view) 0
387 set viewactive($view) 1
388 return 1
38ad0910
PM
389}
390
e2f90ee4
AG
391proc stop_instance {inst} {
392 global commfd leftover
393
394 set fd $commfd($inst)
395 catch {
396 set pid [pid $fd]
b6326e92
AG
397
398 if {$::tcl_platform(platform) eq {windows}} {
399 exec kill -f $pid
400 } else {
401 exec kill $pid
402 }
e2f90ee4
AG
403 }
404 catch {close $fd}
405 nukefile $fd
406 unset commfd($inst)
407 unset leftover($inst)
408}
409
410proc stop_backends {} {
411 global commfd
412
413 foreach inst [array names commfd] {
414 stop_instance $inst
415 }
416}
417
7fcc92bf 418proc stop_rev_list {view} {
e2f90ee4 419 global viewinstances
22626ef4 420
7fcc92bf 421 foreach inst $viewinstances($view) {
e2f90ee4 422 stop_instance $inst
22626ef4 423 }
7fcc92bf 424 set viewinstances($view) {}
22626ef4
PM
425}
426
567c34e0 427proc reset_pending_select {selid} {
39816d60 428 global pending_select mainheadid selectheadid
567c34e0
AG
429
430 if {$selid ne {}} {
431 set pending_select $selid
39816d60
AG
432 } elseif {$selectheadid ne {}} {
433 set pending_select $selectheadid
567c34e0
AG
434 } else {
435 set pending_select $mainheadid
436 }
437}
438
439proc getcommits {selid} {
3ed31a81 440 global canv curview need_redisplay viewactive
38ad0910 441
da7c24dd 442 initlayout
3ed31a81 443 if {[start_rev_list $curview]} {
567c34e0 444 reset_pending_select $selid
3ed31a81
PM
445 show_status [mc "Reading commits..."]
446 set need_redisplay 1
447 } else {
448 show_status [mc "No commits selected"]
449 }
1d10f36d
PM
450}
451
7fcc92bf 452proc updatecommits {} {
ee66e089 453 global curview vcanopt vorigargs vfilelimit viewinstances
e439e092
AG
454 global viewactive viewcomplete tclencoding
455 global startmsecs showneartags showlocalchanges
cdc8429c 456 global mainheadid viewmainheadid viewmainheadid_orig pending_select
92e22ca0 457 global isworktree
ee66e089 458 global varcid vposids vnegids vflags vrevs
7fcc92bf 459
92e22ca0 460 set isworktree [expr {[exec git rev-parse --is-inside-work-tree] == "true"}]
fc2a256f 461 rereadrefs
cdc8429c
PM
462 set view $curview
463 if {$mainheadid ne $viewmainheadid_orig($view)} {
464 if {$showlocalchanges} {
eb5f8c9c
PM
465 dohidelocalchanges
466 }
cdc8429c
PM
467 set viewmainheadid($view) $mainheadid
468 set viewmainheadid_orig($view) $mainheadid
469 if {$vfilelimit($view) ne {}} {
470 get_viewmainhead $view
eb5f8c9c
PM
471 }
472 }
cdc8429c
PM
473 if {$showlocalchanges} {
474 doshowlocalchanges
475 }
ee66e089
PM
476 if {$vcanopt($view)} {
477 set oldpos $vposids($view)
478 set oldneg $vnegids($view)
479 set revs [parseviewrevs $view $vrevs($view)]
480 if {$revs eq {}} {
481 return
482 }
483 # note: getting the delta when negative refs change is hard,
484 # and could require multiple git log invocations, so in that
485 # case we ask git log for all the commits (not just the delta)
486 if {$oldneg eq $vnegids($view)} {
487 set newrevs {}
488 set npos 0
489 # take out positive refs that we asked for before or
490 # that we have already seen
491 foreach rev $revs {
492 if {[string length $rev] == 40} {
493 if {[lsearch -exact $oldpos $rev] < 0
494 && ![info exists varcid($view,$rev)]} {
495 lappend newrevs $rev
496 incr npos
497 }
498 } else {
499 lappend $newrevs $rev
500 }
501 }
502 if {$npos == 0} return
503 set revs $newrevs
504 set vposids($view) [lsort -unique [concat $oldpos $vposids($view)]]
505 }
506 set args [concat $vflags($view) $revs --not $oldpos]
507 } else {
508 set args $vorigargs($view)
509 }
7fcc92bf
PM
510 if {[catch {
511 set fd [open [concat | git log --no-color -z --pretty=raw --parents \
ee66e089 512 --boundary $args "--" $vfilelimit($view)] r]
7fcc92bf 513 } err]} {
3945d2c0 514 error_popup "[mc "Error executing git log:"] $err"
ee66e089 515 return
7fcc92bf
PM
516 }
517 if {$viewactive($view) == 0} {
518 set startmsecs [clock clicks -milliseconds]
519 }
e439e092 520 set i [reg_instance $fd]
7fcc92bf 521 lappend viewinstances($view) $i
7fcc92bf
PM
522 fconfigure $fd -blocking 0 -translation lf -eofchar {}
523 if {$tclencoding != {}} {
524 fconfigure $fd -encoding $tclencoding
525 }
f806f0fb 526 filerun $fd [list getcommitlines $fd $i $view 1]
7fcc92bf
PM
527 incr viewactive($view)
528 set viewcomplete($view) 0
567c34e0 529 reset_pending_select {}
b56e0a9a 530 nowbusy $view [mc "Reading"]
7fcc92bf
PM
531 if {$showneartags} {
532 getallcommits
533 }
534}
535
536proc reloadcommits {} {
537 global curview viewcomplete selectedline currentid thickerline
538 global showneartags treediffs commitinterest cached_commitrow
6df7403a 539 global targetid
7fcc92bf 540
567c34e0
AG
541 set selid {}
542 if {$selectedline ne {}} {
543 set selid $currentid
544 }
545
7fcc92bf
PM
546 if {!$viewcomplete($curview)} {
547 stop_rev_list $curview
7fcc92bf
PM
548 }
549 resetvarcs $curview
94b4a69f 550 set selectedline {}
7fcc92bf
PM
551 catch {unset currentid}
552 catch {unset thickerline}
553 catch {unset treediffs}
554 readrefs
555 changedrefs
556 if {$showneartags} {
557 getallcommits
558 }
559 clear_display
560 catch {unset commitinterest}
561 catch {unset cached_commitrow}
42a671fc 562 catch {unset targetid}
7fcc92bf 563 setcanvscroll
567c34e0 564 getcommits $selid
e7297a1c 565 return 0
7fcc92bf
PM
566}
567
6e8c8707
PM
568# This makes a string representation of a positive integer which
569# sorts as a string in numerical order
570proc strrep {n} {
571 if {$n < 16} {
572 return [format "%x" $n]
573 } elseif {$n < 256} {
574 return [format "x%.2x" $n]
575 } elseif {$n < 65536} {
576 return [format "y%.4x" $n]
577 }
578 return [format "z%.8x" $n]
579}
580
7fcc92bf
PM
581# Procedures used in reordering commits from git log (without
582# --topo-order) into the order for display.
583
584proc varcinit {view} {
f3ea5ede
PM
585 global varcstart vupptr vdownptr vleftptr vbackptr varctok varcrow
586 global vtokmod varcmod vrowmod varcix vlastins
7fcc92bf 587
7fcc92bf
PM
588 set varcstart($view) {{}}
589 set vupptr($view) {0}
590 set vdownptr($view) {0}
591 set vleftptr($view) {0}
f3ea5ede 592 set vbackptr($view) {0}
7fcc92bf
PM
593 set varctok($view) {{}}
594 set varcrow($view) {{}}
595 set vtokmod($view) {}
596 set varcmod($view) 0
e5b37ac1 597 set vrowmod($view) 0
7fcc92bf 598 set varcix($view) {{}}
f3ea5ede 599 set vlastins($view) {0}
7fcc92bf
PM
600}
601
602proc resetvarcs {view} {
603 global varcid varccommits parents children vseedcount ordertok
604
605 foreach vid [array names varcid $view,*] {
606 unset varcid($vid)
607 unset children($vid)
608 unset parents($vid)
609 }
610 # some commits might have children but haven't been seen yet
611 foreach vid [array names children $view,*] {
612 unset children($vid)
613 }
614 foreach va [array names varccommits $view,*] {
615 unset varccommits($va)
616 }
617 foreach vd [array names vseedcount $view,*] {
618 unset vseedcount($vd)
619 }
9257d8f7 620 catch {unset ordertok}
7fcc92bf
PM
621}
622
468bcaed
PM
623# returns a list of the commits with no children
624proc seeds {v} {
625 global vdownptr vleftptr varcstart
626
627 set ret {}
628 set a [lindex $vdownptr($v) 0]
629 while {$a != 0} {
630 lappend ret [lindex $varcstart($v) $a]
631 set a [lindex $vleftptr($v) $a]
632 }
633 return $ret
634}
635
7fcc92bf 636proc newvarc {view id} {
3ed31a81 637 global varcid varctok parents children vdatemode
f3ea5ede
PM
638 global vupptr vdownptr vleftptr vbackptr varcrow varcix varcstart
639 global commitdata commitinfo vseedcount varccommits vlastins
7fcc92bf
PM
640
641 set a [llength $varctok($view)]
642 set vid $view,$id
3ed31a81 643 if {[llength $children($vid)] == 0 || $vdatemode($view)} {
7fcc92bf
PM
644 if {![info exists commitinfo($id)]} {
645 parsecommit $id $commitdata($id) 1
646 }
647 set cdate [lindex $commitinfo($id) 4]
648 if {![string is integer -strict $cdate]} {
649 set cdate 0
650 }
651 if {![info exists vseedcount($view,$cdate)]} {
652 set vseedcount($view,$cdate) -1
653 }
654 set c [incr vseedcount($view,$cdate)]
655 set cdate [expr {$cdate ^ 0xffffffff}]
656 set tok "s[strrep $cdate][strrep $c]"
7fcc92bf
PM
657 } else {
658 set tok {}
f3ea5ede
PM
659 }
660 set ka 0
661 if {[llength $children($vid)] > 0} {
662 set kid [lindex $children($vid) end]
663 set k $varcid($view,$kid)
664 if {[string compare [lindex $varctok($view) $k] $tok] > 0} {
665 set ki $kid
666 set ka $k
667 set tok [lindex $varctok($view) $k]
7fcc92bf 668 }
f3ea5ede
PM
669 }
670 if {$ka != 0} {
7fcc92bf
PM
671 set i [lsearch -exact $parents($view,$ki) $id]
672 set j [expr {[llength $parents($view,$ki)] - 1 - $i}]
7fcc92bf
PM
673 append tok [strrep $j]
674 }
f3ea5ede
PM
675 set c [lindex $vlastins($view) $ka]
676 if {$c == 0 || [string compare $tok [lindex $varctok($view) $c]] < 0} {
677 set c $ka
678 set b [lindex $vdownptr($view) $ka]
679 } else {
680 set b [lindex $vleftptr($view) $c]
681 }
682 while {$b != 0 && [string compare $tok [lindex $varctok($view) $b]] >= 0} {
683 set c $b
684 set b [lindex $vleftptr($view) $c]
685 }
686 if {$c == $ka} {
687 lset vdownptr($view) $ka $a
688 lappend vbackptr($view) 0
689 } else {
690 lset vleftptr($view) $c $a
691 lappend vbackptr($view) $c
692 }
693 lset vlastins($view) $ka $a
694 lappend vupptr($view) $ka
695 lappend vleftptr($view) $b
696 if {$b != 0} {
697 lset vbackptr($view) $b $a
698 }
7fcc92bf
PM
699 lappend varctok($view) $tok
700 lappend varcstart($view) $id
701 lappend vdownptr($view) 0
702 lappend varcrow($view) {}
703 lappend varcix($view) {}
e5b37ac1 704 set varccommits($view,$a) {}
f3ea5ede 705 lappend vlastins($view) 0
7fcc92bf
PM
706 return $a
707}
708
709proc splitvarc {p v} {
52b8ea93 710 global varcid varcstart varccommits varctok vtokmod
f3ea5ede 711 global vupptr vdownptr vleftptr vbackptr varcix varcrow vlastins
7fcc92bf
PM
712
713 set oa $varcid($v,$p)
52b8ea93 714 set otok [lindex $varctok($v) $oa]
7fcc92bf
PM
715 set ac $varccommits($v,$oa)
716 set i [lsearch -exact $varccommits($v,$oa) $p]
717 if {$i <= 0} return
718 set na [llength $varctok($v)]
719 # "%" sorts before "0"...
52b8ea93 720 set tok "$otok%[strrep $i]"
7fcc92bf
PM
721 lappend varctok($v) $tok
722 lappend varcrow($v) {}
723 lappend varcix($v) {}
724 set varccommits($v,$oa) [lrange $ac 0 [expr {$i - 1}]]
725 set varccommits($v,$na) [lrange $ac $i end]
726 lappend varcstart($v) $p
727 foreach id $varccommits($v,$na) {
728 set varcid($v,$id) $na
729 }
730 lappend vdownptr($v) [lindex $vdownptr($v) $oa]
841ea824 731 lappend vlastins($v) [lindex $vlastins($v) $oa]
7fcc92bf 732 lset vdownptr($v) $oa $na
841ea824 733 lset vlastins($v) $oa 0
7fcc92bf
PM
734 lappend vupptr($v) $oa
735 lappend vleftptr($v) 0
f3ea5ede 736 lappend vbackptr($v) 0
7fcc92bf
PM
737 for {set b [lindex $vdownptr($v) $na]} {$b != 0} {set b [lindex $vleftptr($v) $b]} {
738 lset vupptr($v) $b $na
739 }
52b8ea93
PM
740 if {[string compare $otok $vtokmod($v)] <= 0} {
741 modify_arc $v $oa
742 }
7fcc92bf
PM
743}
744
745proc renumbervarc {a v} {
746 global parents children varctok varcstart varccommits
3ed31a81 747 global vupptr vdownptr vleftptr vbackptr vlastins varcid vtokmod vdatemode
7fcc92bf
PM
748
749 set t1 [clock clicks -milliseconds]
750 set todo {}
751 set isrelated($a) 1
f3ea5ede 752 set kidchanged($a) 1
7fcc92bf
PM
753 set ntot 0
754 while {$a != 0} {
755 if {[info exists isrelated($a)]} {
756 lappend todo $a
757 set id [lindex $varccommits($v,$a) end]
758 foreach p $parents($v,$id) {
759 if {[info exists varcid($v,$p)]} {
760 set isrelated($varcid($v,$p)) 1
761 }
762 }
763 }
764 incr ntot
765 set b [lindex $vdownptr($v) $a]
766 if {$b == 0} {
767 while {$a != 0} {
768 set b [lindex $vleftptr($v) $a]
769 if {$b != 0} break
770 set a [lindex $vupptr($v) $a]
771 }
772 }
773 set a $b
774 }
775 foreach a $todo {
f3ea5ede 776 if {![info exists kidchanged($a)]} continue
7fcc92bf 777 set id [lindex $varcstart($v) $a]
f3ea5ede
PM
778 if {[llength $children($v,$id)] > 1} {
779 set children($v,$id) [lsort -command [list vtokcmp $v] \
780 $children($v,$id)]
781 }
782 set oldtok [lindex $varctok($v) $a]
3ed31a81 783 if {!$vdatemode($v)} {
f3ea5ede
PM
784 set tok {}
785 } else {
786 set tok $oldtok
787 }
788 set ka 0
c8c9f3d9
PM
789 set kid [last_real_child $v,$id]
790 if {$kid ne {}} {
f3ea5ede
PM
791 set k $varcid($v,$kid)
792 if {[string compare [lindex $varctok($v) $k] $tok] > 0} {
793 set ki $kid
794 set ka $k
795 set tok [lindex $varctok($v) $k]
7fcc92bf
PM
796 }
797 }
f3ea5ede 798 if {$ka != 0} {
7fcc92bf
PM
799 set i [lsearch -exact $parents($v,$ki) $id]
800 set j [expr {[llength $parents($v,$ki)] - 1 - $i}]
801 append tok [strrep $j]
7fcc92bf 802 }
f3ea5ede
PM
803 if {$tok eq $oldtok} {
804 continue
805 }
806 set id [lindex $varccommits($v,$a) end]
807 foreach p $parents($v,$id) {
808 if {[info exists varcid($v,$p)]} {
809 set kidchanged($varcid($v,$p)) 1
810 } else {
811 set sortkids($p) 1
812 }
813 }
814 lset varctok($v) $a $tok
7fcc92bf
PM
815 set b [lindex $vupptr($v) $a]
816 if {$b != $ka} {
9257d8f7
PM
817 if {[string compare [lindex $varctok($v) $ka] $vtokmod($v)] < 0} {
818 modify_arc $v $ka
38dfe939 819 }
9257d8f7
PM
820 if {[string compare [lindex $varctok($v) $b] $vtokmod($v)] < 0} {
821 modify_arc $v $b
38dfe939 822 }
f3ea5ede
PM
823 set c [lindex $vbackptr($v) $a]
824 set d [lindex $vleftptr($v) $a]
825 if {$c == 0} {
826 lset vdownptr($v) $b $d
7fcc92bf 827 } else {
f3ea5ede
PM
828 lset vleftptr($v) $c $d
829 }
830 if {$d != 0} {
831 lset vbackptr($v) $d $c
7fcc92bf 832 }
841ea824
PM
833 if {[lindex $vlastins($v) $b] == $a} {
834 lset vlastins($v) $b $c
835 }
7fcc92bf 836 lset vupptr($v) $a $ka
f3ea5ede
PM
837 set c [lindex $vlastins($v) $ka]
838 if {$c == 0 || \
839 [string compare $tok [lindex $varctok($v) $c]] < 0} {
840 set c $ka
841 set b [lindex $vdownptr($v) $ka]
842 } else {
843 set b [lindex $vleftptr($v) $c]
844 }
845 while {$b != 0 && \
846 [string compare $tok [lindex $varctok($v) $b]] >= 0} {
847 set c $b
848 set b [lindex $vleftptr($v) $c]
7fcc92bf 849 }
f3ea5ede
PM
850 if {$c == $ka} {
851 lset vdownptr($v) $ka $a
852 lset vbackptr($v) $a 0
853 } else {
854 lset vleftptr($v) $c $a
855 lset vbackptr($v) $a $c
7fcc92bf 856 }
f3ea5ede
PM
857 lset vleftptr($v) $a $b
858 if {$b != 0} {
859 lset vbackptr($v) $b $a
860 }
861 lset vlastins($v) $ka $a
862 }
863 }
864 foreach id [array names sortkids] {
865 if {[llength $children($v,$id)] > 1} {
866 set children($v,$id) [lsort -command [list vtokcmp $v] \
867 $children($v,$id)]
7fcc92bf
PM
868 }
869 }
870 set t2 [clock clicks -milliseconds]
871 #puts "renumbervarc did [llength $todo] of $ntot arcs in [expr {$t2-$t1}]ms"
872}
873
f806f0fb
PM
874# Fix up the graph after we have found out that in view $v,
875# $p (a commit that we have already seen) is actually the parent
876# of the last commit in arc $a.
7fcc92bf 877proc fix_reversal {p a v} {
24f7a667 878 global varcid varcstart varctok vupptr
7fcc92bf
PM
879
880 set pa $varcid($v,$p)
881 if {$p ne [lindex $varcstart($v) $pa]} {
882 splitvarc $p $v
883 set pa $varcid($v,$p)
884 }
24f7a667
PM
885 # seeds always need to be renumbered
886 if {[lindex $vupptr($v) $pa] == 0 ||
887 [string compare [lindex $varctok($v) $a] \
888 [lindex $varctok($v) $pa]] > 0} {
7fcc92bf
PM
889 renumbervarc $pa $v
890 }
891}
892
893proc insertrow {id p v} {
b8a938cf
PM
894 global cmitlisted children parents varcid varctok vtokmod
895 global varccommits ordertok commitidx numcommits curview
896 global targetid targetrow
897
898 readcommit $id
899 set vid $v,$id
900 set cmitlisted($vid) 1
901 set children($vid) {}
902 set parents($vid) [list $p]
903 set a [newvarc $v $id]
904 set varcid($vid) $a
905 if {[string compare [lindex $varctok($v) $a] $vtokmod($v)] < 0} {
906 modify_arc $v $a
907 }
908 lappend varccommits($v,$a) $id
909 set vp $v,$p
910 if {[llength [lappend children($vp) $id]] > 1} {
911 set children($vp) [lsort -command [list vtokcmp $v] $children($vp)]
912 catch {unset ordertok}
913 }
914 fix_reversal $p $a $v
915 incr commitidx($v)
916 if {$v == $curview} {
917 set numcommits $commitidx($v)
918 setcanvscroll
919 if {[info exists targetid]} {
920 if {![comes_before $targetid $p]} {
921 incr targetrow
922 }
923 }
924 }
925}
926
927proc insertfakerow {id p} {
9257d8f7 928 global varcid varccommits parents children cmitlisted
b8a938cf 929 global commitidx varctok vtokmod targetid targetrow curview numcommits
7fcc92bf 930
b8a938cf 931 set v $curview
7fcc92bf
PM
932 set a $varcid($v,$p)
933 set i [lsearch -exact $varccommits($v,$a) $p]
934 if {$i < 0} {
b8a938cf 935 puts "oops: insertfakerow can't find [shortids $p] on arc $a"
7fcc92bf
PM
936 return
937 }
938 set children($v,$id) {}
939 set parents($v,$id) [list $p]
940 set varcid($v,$id) $a
9257d8f7 941 lappend children($v,$p) $id
7fcc92bf 942 set cmitlisted($v,$id) 1
b8a938cf 943 set numcommits [incr commitidx($v)]
7fcc92bf
PM
944 # note we deliberately don't update varcstart($v) even if $i == 0
945 set varccommits($v,$a) [linsert $varccommits($v,$a) $i $id]
c9cfdc96 946 modify_arc $v $a $i
42a671fc
PM
947 if {[info exists targetid]} {
948 if {![comes_before $targetid $p]} {
949 incr targetrow
950 }
951 }
b8a938cf 952 setcanvscroll
9257d8f7 953 drawvisible
7fcc92bf
PM
954}
955
b8a938cf 956proc removefakerow {id} {
9257d8f7 957 global varcid varccommits parents children commitidx
fc2a256f 958 global varctok vtokmod cmitlisted currentid selectedline
b8a938cf 959 global targetid curview numcommits
7fcc92bf 960
b8a938cf 961 set v $curview
7fcc92bf 962 if {[llength $parents($v,$id)] != 1} {
b8a938cf 963 puts "oops: removefakerow [shortids $id] has [llength $parents($v,$id)] parents"
7fcc92bf
PM
964 return
965 }
966 set p [lindex $parents($v,$id) 0]
967 set a $varcid($v,$id)
968 set i [lsearch -exact $varccommits($v,$a) $id]
969 if {$i < 0} {
b8a938cf 970 puts "oops: removefakerow can't find [shortids $id] on arc $a"
7fcc92bf
PM
971 return
972 }
973 unset varcid($v,$id)
974 set varccommits($v,$a) [lreplace $varccommits($v,$a) $i $i]
975 unset parents($v,$id)
976 unset children($v,$id)
977 unset cmitlisted($v,$id)
b8a938cf 978 set numcommits [incr commitidx($v) -1]
7fcc92bf
PM
979 set j [lsearch -exact $children($v,$p) $id]
980 if {$j >= 0} {
981 set children($v,$p) [lreplace $children($v,$p) $j $j]
982 }
c9cfdc96 983 modify_arc $v $a $i
fc2a256f
PM
984 if {[info exist currentid] && $id eq $currentid} {
985 unset currentid
94b4a69f 986 set selectedline {}
fc2a256f 987 }
42a671fc
PM
988 if {[info exists targetid] && $targetid eq $id} {
989 set targetid $p
990 }
b8a938cf 991 setcanvscroll
9257d8f7 992 drawvisible
7fcc92bf
PM
993}
994
aa43561a
PM
995proc real_children {vp} {
996 global children nullid nullid2
997
998 set kids {}
999 foreach id $children($vp) {
1000 if {$id ne $nullid && $id ne $nullid2} {
1001 lappend kids $id
1002 }
1003 }
1004 return $kids
1005}
1006
c8c9f3d9
PM
1007proc first_real_child {vp} {
1008 global children nullid nullid2
1009
1010 foreach id $children($vp) {
1011 if {$id ne $nullid && $id ne $nullid2} {
1012 return $id
1013 }
1014 }
1015 return {}
1016}
1017
1018proc last_real_child {vp} {
1019 global children nullid nullid2
1020
1021 set kids $children($vp)
1022 for {set i [llength $kids]} {[incr i -1] >= 0} {} {
1023 set id [lindex $kids $i]
1024 if {$id ne $nullid && $id ne $nullid2} {
1025 return $id
1026 }
1027 }
1028 return {}
1029}
1030
7fcc92bf
PM
1031proc vtokcmp {v a b} {
1032 global varctok varcid
1033
1034 return [string compare [lindex $varctok($v) $varcid($v,$a)] \
1035 [lindex $varctok($v) $varcid($v,$b)]]
1036}
1037
c9cfdc96
PM
1038# This assumes that if lim is not given, the caller has checked that
1039# arc a's token is less than $vtokmod($v)
e5b37ac1
PM
1040proc modify_arc {v a {lim {}}} {
1041 global varctok vtokmod varcmod varcrow vupptr curview vrowmod varccommits
9257d8f7 1042
c9cfdc96
PM
1043 if {$lim ne {}} {
1044 set c [string compare [lindex $varctok($v) $a] $vtokmod($v)]
1045 if {$c > 0} return
1046 if {$c == 0} {
1047 set r [lindex $varcrow($v) $a]
1048 if {$r ne {} && $vrowmod($v) <= $r + $lim} return
1049 }
1050 }
9257d8f7
PM
1051 set vtokmod($v) [lindex $varctok($v) $a]
1052 set varcmod($v) $a
1053 if {$v == $curview} {
1054 while {$a != 0 && [lindex $varcrow($v) $a] eq {}} {
1055 set a [lindex $vupptr($v) $a]
e5b37ac1 1056 set lim {}
9257d8f7 1057 }
e5b37ac1
PM
1058 set r 0
1059 if {$a != 0} {
1060 if {$lim eq {}} {
1061 set lim [llength $varccommits($v,$a)]
1062 }
1063 set r [expr {[lindex $varcrow($v) $a] + $lim}]
1064 }
1065 set vrowmod($v) $r
0c27886e 1066 undolayout $r
9257d8f7
PM
1067 }
1068}
1069
7fcc92bf 1070proc update_arcrows {v} {
e5b37ac1 1071 global vtokmod varcmod vrowmod varcrow commitidx currentid selectedline
24f7a667 1072 global varcid vrownum varcorder varcix varccommits
7fcc92bf 1073 global vupptr vdownptr vleftptr varctok
24f7a667 1074 global displayorder parentlist curview cached_commitrow
7fcc92bf 1075
c9cfdc96
PM
1076 if {$vrowmod($v) == $commitidx($v)} return
1077 if {$v == $curview} {
1078 if {[llength $displayorder] > $vrowmod($v)} {
1079 set displayorder [lrange $displayorder 0 [expr {$vrowmod($v) - 1}]]
1080 set parentlist [lrange $parentlist 0 [expr {$vrowmod($v) - 1}]]
1081 }
1082 catch {unset cached_commitrow}
1083 }
7fcc92bf
PM
1084 set narctot [expr {[llength $varctok($v)] - 1}]
1085 set a $varcmod($v)
1086 while {$a != 0 && [lindex $varcix($v) $a] eq {}} {
1087 # go up the tree until we find something that has a row number,
1088 # or we get to a seed
1089 set a [lindex $vupptr($v) $a]
1090 }
1091 if {$a == 0} {
1092 set a [lindex $vdownptr($v) 0]
1093 if {$a == 0} return
1094 set vrownum($v) {0}
1095 set varcorder($v) [list $a]
1096 lset varcix($v) $a 0
1097 lset varcrow($v) $a 0
1098 set arcn 0
1099 set row 0
1100 } else {
1101 set arcn [lindex $varcix($v) $a]
7fcc92bf
PM
1102 if {[llength $vrownum($v)] > $arcn + 1} {
1103 set vrownum($v) [lrange $vrownum($v) 0 $arcn]
1104 set varcorder($v) [lrange $varcorder($v) 0 $arcn]
1105 }
1106 set row [lindex $varcrow($v) $a]
1107 }
7fcc92bf
PM
1108 while {1} {
1109 set p $a
1110 incr row [llength $varccommits($v,$a)]
1111 # go down if possible
1112 set b [lindex $vdownptr($v) $a]
1113 if {$b == 0} {
1114 # if not, go left, or go up until we can go left
1115 while {$a != 0} {
1116 set b [lindex $vleftptr($v) $a]
1117 if {$b != 0} break
1118 set a [lindex $vupptr($v) $a]
1119 }
1120 if {$a == 0} break
1121 }
1122 set a $b
1123 incr arcn
1124 lappend vrownum($v) $row
1125 lappend varcorder($v) $a
1126 lset varcix($v) $a $arcn
1127 lset varcrow($v) $a $row
1128 }
e5b37ac1
PM
1129 set vtokmod($v) [lindex $varctok($v) $p]
1130 set varcmod($v) $p
1131 set vrowmod($v) $row
7fcc92bf
PM
1132 if {[info exists currentid]} {
1133 set selectedline [rowofcommit $currentid]
1134 }
7fcc92bf
PM
1135}
1136
1137# Test whether view $v contains commit $id
1138proc commitinview {id v} {
1139 global varcid
1140
1141 return [info exists varcid($v,$id)]
1142}
1143
1144# Return the row number for commit $id in the current view
1145proc rowofcommit {id} {
1146 global varcid varccommits varcrow curview cached_commitrow
9257d8f7 1147 global varctok vtokmod
7fcc92bf 1148
7fcc92bf
PM
1149 set v $curview
1150 if {![info exists varcid($v,$id)]} {
1151 puts "oops rowofcommit no arc for [shortids $id]"
1152 return {}
1153 }
1154 set a $varcid($v,$id)
fc2a256f 1155 if {[string compare [lindex $varctok($v) $a] $vtokmod($v)] >= 0} {
9257d8f7
PM
1156 update_arcrows $v
1157 }
31c0eaa8
PM
1158 if {[info exists cached_commitrow($id)]} {
1159 return $cached_commitrow($id)
1160 }
7fcc92bf
PM
1161 set i [lsearch -exact $varccommits($v,$a) $id]
1162 if {$i < 0} {
1163 puts "oops didn't find commit [shortids $id] in arc $a"
1164 return {}
1165 }
1166 incr i [lindex $varcrow($v) $a]
1167 set cached_commitrow($id) $i
1168 return $i
1169}
1170
42a671fc
PM
1171# Returns 1 if a is on an earlier row than b, otherwise 0
1172proc comes_before {a b} {
1173 global varcid varctok curview
1174
1175 set v $curview
1176 if {$a eq $b || ![info exists varcid($v,$a)] || \
1177 ![info exists varcid($v,$b)]} {
1178 return 0
1179 }
1180 if {$varcid($v,$a) != $varcid($v,$b)} {
1181 return [expr {[string compare [lindex $varctok($v) $varcid($v,$a)] \
1182 [lindex $varctok($v) $varcid($v,$b)]] < 0}]
1183 }
1184 return [expr {[rowofcommit $a] < [rowofcommit $b]}]
1185}
1186
7fcc92bf
PM
1187proc bsearch {l elt} {
1188 if {[llength $l] == 0 || $elt <= [lindex $l 0]} {
1189 return 0
1190 }
1191 set lo 0
1192 set hi [llength $l]
1193 while {$hi - $lo > 1} {
1194 set mid [expr {int(($lo + $hi) / 2)}]
1195 set t [lindex $l $mid]
1196 if {$elt < $t} {
1197 set hi $mid
1198 } elseif {$elt > $t} {
1199 set lo $mid
1200 } else {
1201 return $mid
1202 }
1203 }
1204 return $lo
1205}
1206
1207# Make sure rows $start..$end-1 are valid in displayorder and parentlist
1208proc make_disporder {start end} {
1209 global vrownum curview commitidx displayorder parentlist
e5b37ac1 1210 global varccommits varcorder parents vrowmod varcrow
7fcc92bf
PM
1211 global d_valid_start d_valid_end
1212
e5b37ac1 1213 if {$end > $vrowmod($curview)} {
9257d8f7
PM
1214 update_arcrows $curview
1215 }
7fcc92bf
PM
1216 set ai [bsearch $vrownum($curview) $start]
1217 set start [lindex $vrownum($curview) $ai]
1218 set narc [llength $vrownum($curview)]
1219 for {set r $start} {$ai < $narc && $r < $end} {incr ai} {
1220 set a [lindex $varcorder($curview) $ai]
1221 set l [llength $displayorder]
1222 set al [llength $varccommits($curview,$a)]
1223 if {$l < $r + $al} {
1224 if {$l < $r} {
1225 set pad [ntimes [expr {$r - $l}] {}]
1226 set displayorder [concat $displayorder $pad]
1227 set parentlist [concat $parentlist $pad]
1228 } elseif {$l > $r} {
1229 set displayorder [lrange $displayorder 0 [expr {$r - 1}]]
1230 set parentlist [lrange $parentlist 0 [expr {$r - 1}]]
1231 }
1232 foreach id $varccommits($curview,$a) {
1233 lappend displayorder $id
1234 lappend parentlist $parents($curview,$id)
1235 }
17529cf9 1236 } elseif {[lindex $displayorder [expr {$r + $al - 1}]] eq {}} {
7fcc92bf
PM
1237 set i $r
1238 foreach id $varccommits($curview,$a) {
1239 lset displayorder $i $id
1240 lset parentlist $i $parents($curview,$id)
1241 incr i
1242 }
1243 }
1244 incr r $al
1245 }
1246}
1247
1248proc commitonrow {row} {
1249 global displayorder
1250
1251 set id [lindex $displayorder $row]
1252 if {$id eq {}} {
1253 make_disporder $row [expr {$row + 1}]
1254 set id [lindex $displayorder $row]
1255 }
1256 return $id
1257}
1258
1259proc closevarcs {v} {
1260 global varctok varccommits varcid parents children
d375ef9b 1261 global cmitlisted commitidx vtokmod
7fcc92bf
PM
1262
1263 set missing_parents 0
1264 set scripts {}
1265 set narcs [llength $varctok($v)]
1266 for {set a 1} {$a < $narcs} {incr a} {
1267 set id [lindex $varccommits($v,$a) end]
1268 foreach p $parents($v,$id) {
1269 if {[info exists varcid($v,$p)]} continue
1270 # add p as a new commit
1271 incr missing_parents
1272 set cmitlisted($v,$p) 0
1273 set parents($v,$p) {}
1274 if {[llength $children($v,$p)] == 1 &&
1275 [llength $parents($v,$id)] == 1} {
1276 set b $a
1277 } else {
1278 set b [newvarc $v $p]
1279 }
1280 set varcid($v,$p) $b
9257d8f7
PM
1281 if {[string compare [lindex $varctok($v) $b] $vtokmod($v)] < 0} {
1282 modify_arc $v $b
7fcc92bf 1283 }
e5b37ac1 1284 lappend varccommits($v,$b) $p
7fcc92bf 1285 incr commitidx($v)
d375ef9b 1286 set scripts [check_interest $p $scripts]
7fcc92bf
PM
1287 }
1288 }
1289 if {$missing_parents > 0} {
7fcc92bf
PM
1290 foreach s $scripts {
1291 eval $s
1292 }
1293 }
1294}
1295
f806f0fb
PM
1296# Use $rwid as a substitute for $id, i.e. reparent $id's children to $rwid
1297# Assumes we already have an arc for $rwid.
1298proc rewrite_commit {v id rwid} {
1299 global children parents varcid varctok vtokmod varccommits
1300
1301 foreach ch $children($v,$id) {
1302 # make $rwid be $ch's parent in place of $id
1303 set i [lsearch -exact $parents($v,$ch) $id]
1304 if {$i < 0} {
1305 puts "oops rewrite_commit didn't find $id in parent list for $ch"
1306 }
1307 set parents($v,$ch) [lreplace $parents($v,$ch) $i $i $rwid]
1308 # add $ch to $rwid's children and sort the list if necessary
1309 if {[llength [lappend children($v,$rwid) $ch]] > 1} {
1310 set children($v,$rwid) [lsort -command [list vtokcmp $v] \
1311 $children($v,$rwid)]
1312 }
1313 # fix the graph after joining $id to $rwid
1314 set a $varcid($v,$ch)
1315 fix_reversal $rwid $a $v
c9cfdc96
PM
1316 # parentlist is wrong for the last element of arc $a
1317 # even if displayorder is right, hence the 3rd arg here
1318 modify_arc $v $a [expr {[llength $varccommits($v,$a)] - 1}]
f806f0fb
PM
1319 }
1320}
1321
d375ef9b
PM
1322# Mechanism for registering a command to be executed when we come
1323# across a particular commit. To handle the case when only the
1324# prefix of the commit is known, the commitinterest array is now
1325# indexed by the first 4 characters of the ID. Each element is a
1326# list of id, cmd pairs.
1327proc interestedin {id cmd} {
1328 global commitinterest
1329
1330 lappend commitinterest([string range $id 0 3]) $id $cmd
1331}
1332
1333proc check_interest {id scripts} {
1334 global commitinterest
1335
1336 set prefix [string range $id 0 3]
1337 if {[info exists commitinterest($prefix)]} {
1338 set newlist {}
1339 foreach {i script} $commitinterest($prefix) {
1340 if {[string match "$i*" $id]} {
1341 lappend scripts [string map [list "%I" $id "%P" $i] $script]
1342 } else {
1343 lappend newlist $i $script
1344 }
1345 }
1346 if {$newlist ne {}} {
1347 set commitinterest($prefix) $newlist
1348 } else {
1349 unset commitinterest($prefix)
1350 }
1351 }
1352 return $scripts
1353}
1354
f806f0fb 1355proc getcommitlines {fd inst view updating} {
d375ef9b 1356 global cmitlisted leftover
3ed31a81 1357 global commitidx commitdata vdatemode
7fcc92bf 1358 global parents children curview hlview
468bcaed 1359 global idpending ordertok
3ed31a81 1360 global varccommits varcid varctok vtokmod vfilelimit
9ccbdfbf 1361
d1e46756 1362 set stuff [read $fd 500000]
005a2f4e 1363 # git log doesn't terminate the last commit with a null...
7fcc92bf 1364 if {$stuff == {} && $leftover($inst) ne {} && [eof $fd]} {
005a2f4e
PM
1365 set stuff "\0"
1366 }
b490a991 1367 if {$stuff == {}} {
7eb3cb9c
PM
1368 if {![eof $fd]} {
1369 return 1
1370 }
6df7403a 1371 global commfd viewcomplete viewactive viewname
7fcc92bf
PM
1372 global viewinstances
1373 unset commfd($inst)
1374 set i [lsearch -exact $viewinstances($view) $inst]
1375 if {$i >= 0} {
1376 set viewinstances($view) [lreplace $viewinstances($view) $i $i]
b0cdca99 1377 }
f0654861 1378 # set it blocking so we wait for the process to terminate
da7c24dd 1379 fconfigure $fd -blocking 1
098dd8a3
PM
1380 if {[catch {close $fd} err]} {
1381 set fv {}
1382 if {$view != $curview} {
1383 set fv " for the \"$viewname($view)\" view"
da7c24dd 1384 }
098dd8a3
PM
1385 if {[string range $err 0 4] == "usage"} {
1386 set err "Gitk: error reading commits$fv:\
f9e0b6fb 1387 bad arguments to git log."
098dd8a3
PM
1388 if {$viewname($view) eq "Command line"} {
1389 append err \
f9e0b6fb 1390 " (Note: arguments to gitk are passed to git log\
098dd8a3
PM
1391 to allow selection of commits to be displayed.)"
1392 }
1393 } else {
1394 set err "Error reading commits$fv: $err"
1395 }
1396 error_popup $err
1d10f36d 1397 }
7fcc92bf
PM
1398 if {[incr viewactive($view) -1] <= 0} {
1399 set viewcomplete($view) 1
1400 # Check if we have seen any ids listed as parents that haven't
1401 # appeared in the list
1402 closevarcs $view
1403 notbusy $view
7fcc92bf 1404 }
098dd8a3 1405 if {$view == $curview} {
ac1276ab 1406 run chewcommits
9a40c50c 1407 }
7eb3cb9c 1408 return 0
9a40c50c 1409 }
b490a991 1410 set start 0
8f7d0cec 1411 set gotsome 0
7fcc92bf 1412 set scripts {}
b490a991
PM
1413 while 1 {
1414 set i [string first "\0" $stuff $start]
1415 if {$i < 0} {
7fcc92bf 1416 append leftover($inst) [string range $stuff $start end]
9f1afe05 1417 break
9ccbdfbf 1418 }
b490a991 1419 if {$start == 0} {
7fcc92bf 1420 set cmit $leftover($inst)
8f7d0cec 1421 append cmit [string range $stuff 0 [expr {$i - 1}]]
7fcc92bf 1422 set leftover($inst) {}
8f7d0cec
PM
1423 } else {
1424 set cmit [string range $stuff $start [expr {$i - 1}]]
b490a991
PM
1425 }
1426 set start [expr {$i + 1}]
e5ea701b
PM
1427 set j [string first "\n" $cmit]
1428 set ok 0
16c1ff96 1429 set listed 1
c961b228
PM
1430 if {$j >= 0 && [string match "commit *" $cmit]} {
1431 set ids [string range $cmit 7 [expr {$j - 1}]]
1407ade9 1432 if {[string match {[-^<>]*} $ids]} {
c961b228
PM
1433 switch -- [string index $ids 0] {
1434 "-" {set listed 0}
1407ade9
LT
1435 "^" {set listed 2}
1436 "<" {set listed 3}
1437 ">" {set listed 4}
c961b228 1438 }
16c1ff96
PM
1439 set ids [string range $ids 1 end]
1440 }
e5ea701b
PM
1441 set ok 1
1442 foreach id $ids {
8f7d0cec 1443 if {[string length $id] != 40} {
e5ea701b
PM
1444 set ok 0
1445 break
1446 }
1447 }
1448 }
1449 if {!$ok} {
7e952e79
PM
1450 set shortcmit $cmit
1451 if {[string length $shortcmit] > 80} {
1452 set shortcmit "[string range $shortcmit 0 80]..."
1453 }
d990cedf 1454 error_popup "[mc "Can't parse git log output:"] {$shortcmit}"
b490a991
PM
1455 exit 1
1456 }
e5ea701b 1457 set id [lindex $ids 0]
7fcc92bf 1458 set vid $view,$id
f806f0fb
PM
1459
1460 if {!$listed && $updating && ![info exists varcid($vid)] &&
3ed31a81 1461 $vfilelimit($view) ne {}} {
f806f0fb
PM
1462 # git log doesn't rewrite parents for unlisted commits
1463 # when doing path limiting, so work around that here
1464 # by working out the rewritten parent with git rev-list
1465 # and if we already know about it, using the rewritten
1466 # parent as a substitute parent for $id's children.
1467 if {![catch {
1468 set rwid [exec git rev-list --first-parent --max-count=1 \
3ed31a81 1469 $id -- $vfilelimit($view)]
f806f0fb
PM
1470 }]} {
1471 if {$rwid ne {} && [info exists varcid($view,$rwid)]} {
1472 # use $rwid in place of $id
1473 rewrite_commit $view $id $rwid
1474 continue
1475 }
1476 }
1477 }
1478
f1bf4ee6
PM
1479 set a 0
1480 if {[info exists varcid($vid)]} {
1481 if {$cmitlisted($vid) || !$listed} continue
1482 set a $varcid($vid)
1483 }
16c1ff96
PM
1484 if {$listed} {
1485 set olds [lrange $ids 1 end]
16c1ff96
PM
1486 } else {
1487 set olds {}
1488 }
f7a3e8d2 1489 set commitdata($id) [string range $cmit [expr {$j + 1}] end]
7fcc92bf
PM
1490 set cmitlisted($vid) $listed
1491 set parents($vid) $olds
7fcc92bf
PM
1492 if {![info exists children($vid)]} {
1493 set children($vid) {}
f1bf4ee6 1494 } elseif {$a == 0 && [llength $children($vid)] == 1} {
f3ea5ede
PM
1495 set k [lindex $children($vid) 0]
1496 if {[llength $parents($view,$k)] == 1 &&
3ed31a81 1497 (!$vdatemode($view) ||
f3ea5ede
PM
1498 $varcid($view,$k) == [llength $varctok($view)] - 1)} {
1499 set a $varcid($view,$k)
7fcc92bf 1500 }
da7c24dd 1501 }
7fcc92bf
PM
1502 if {$a == 0} {
1503 # new arc
1504 set a [newvarc $view $id]
1505 }
e5b37ac1
PM
1506 if {[string compare [lindex $varctok($view) $a] $vtokmod($view)] < 0} {
1507 modify_arc $view $a
1508 }
f1bf4ee6
PM
1509 if {![info exists varcid($vid)]} {
1510 set varcid($vid) $a
1511 lappend varccommits($view,$a) $id
1512 incr commitidx($view)
1513 }
e5b37ac1 1514
7fcc92bf
PM
1515 set i 0
1516 foreach p $olds {
1517 if {$i == 0 || [lsearch -exact $olds $p] >= $i} {
1518 set vp $view,$p
1519 if {[llength [lappend children($vp) $id]] > 1 &&
1520 [vtokcmp $view [lindex $children($vp) end-1] $id] > 0} {
1521 set children($vp) [lsort -command [list vtokcmp $view] \
1522 $children($vp)]
9257d8f7 1523 catch {unset ordertok}
7fcc92bf 1524 }
f3ea5ede
PM
1525 if {[info exists varcid($view,$p)]} {
1526 fix_reversal $p $a $view
1527 }
7fcc92bf
PM
1528 }
1529 incr i
1530 }
7fcc92bf 1531
d375ef9b 1532 set scripts [check_interest $id $scripts]
8f7d0cec
PM
1533 set gotsome 1
1534 }
1535 if {$gotsome} {
ac1276ab
PM
1536 global numcommits hlview
1537
1538 if {$view == $curview} {
1539 set numcommits $commitidx($view)
1540 run chewcommits
1541 }
1542 if {[info exists hlview] && $view == $hlview} {
1543 # we never actually get here...
1544 run vhighlightmore
1545 }
7fcc92bf
PM
1546 foreach s $scripts {
1547 eval $s
1548 }
9ccbdfbf 1549 }
7eb3cb9c 1550 return 2
9ccbdfbf
PM
1551}
1552
ac1276ab 1553proc chewcommits {} {
f5f3c2e2 1554 global curview hlview viewcomplete
7fcc92bf 1555 global pending_select
7eb3cb9c 1556
ac1276ab
PM
1557 layoutmore
1558 if {$viewcomplete($curview)} {
1559 global commitidx varctok
1560 global numcommits startmsecs
ac1276ab
PM
1561
1562 if {[info exists pending_select]} {
835e62ae
AG
1563 update
1564 reset_pending_select {}
1565
1566 if {[commitinview $pending_select $curview]} {
1567 selectline [rowofcommit $pending_select] 1
1568 } else {
1569 set row [first_real_row]
1570 selectline $row 1
1571 }
7eb3cb9c 1572 }
ac1276ab
PM
1573 if {$commitidx($curview) > 0} {
1574 #set ms [expr {[clock clicks -milliseconds] - $startmsecs}]
1575 #puts "overall $ms ms for $numcommits commits"
1576 #puts "[llength $varctok($view)] arcs, $commitidx($view) commits"
1577 } else {
1578 show_status [mc "No commits selected"]
1579 }
1580 notbusy layout
b664550c 1581 }
f5f3c2e2 1582 return 0
1db95b00
PM
1583}
1584
590915da
AG
1585proc do_readcommit {id} {
1586 global tclencoding
1587
1588 # Invoke git-log to handle automatic encoding conversion
1589 set fd [open [concat | git log --no-color --pretty=raw -1 $id] r]
1590 # Read the results using i18n.logoutputencoding
1591 fconfigure $fd -translation lf -eofchar {}
1592 if {$tclencoding != {}} {
1593 fconfigure $fd -encoding $tclencoding
1594 }
1595 set contents [read $fd]
1596 close $fd
1597 # Remove the heading line
1598 regsub {^commit [0-9a-f]+\n} $contents {} contents
1599
1600 return $contents
1601}
1602
1db95b00 1603proc readcommit {id} {
590915da
AG
1604 if {[catch {set contents [do_readcommit $id]}]} return
1605 parsecommit $id $contents 1
b490a991
PM
1606}
1607
8f7d0cec 1608proc parsecommit {id contents listed} {
b5c2f306
SV
1609 global commitinfo cdate
1610
1611 set inhdr 1
1612 set comment {}
1613 set headline {}
1614 set auname {}
1615 set audate {}
1616 set comname {}
1617 set comdate {}
232475d3
PM
1618 set hdrend [string first "\n\n" $contents]
1619 if {$hdrend < 0} {
1620 # should never happen...
1621 set hdrend [string length $contents]
1622 }
1623 set header [string range $contents 0 [expr {$hdrend - 1}]]
1624 set comment [string range $contents [expr {$hdrend + 2}] end]
1625 foreach line [split $header "\n"] {
61f57cb0 1626 set line [split $line " "]
232475d3
PM
1627 set tag [lindex $line 0]
1628 if {$tag == "author"} {
1629 set audate [lindex $line end-1]
61f57cb0 1630 set auname [join [lrange $line 1 end-2] " "]
232475d3
PM
1631 } elseif {$tag == "committer"} {
1632 set comdate [lindex $line end-1]
61f57cb0 1633 set comname [join [lrange $line 1 end-2] " "]
1db95b00
PM
1634 }
1635 }
232475d3 1636 set headline {}
43c25074
PM
1637 # take the first non-blank line of the comment as the headline
1638 set headline [string trimleft $comment]
1639 set i [string first "\n" $headline]
232475d3 1640 if {$i >= 0} {
43c25074
PM
1641 set headline [string range $headline 0 $i]
1642 }
1643 set headline [string trimright $headline]
1644 set i [string first "\r" $headline]
1645 if {$i >= 0} {
1646 set headline [string trimright [string range $headline 0 $i]]
232475d3
PM
1647 }
1648 if {!$listed} {
f9e0b6fb 1649 # git log indents the comment by 4 spaces;
8974c6f9 1650 # if we got this via git cat-file, add the indentation
232475d3
PM
1651 set newcomment {}
1652 foreach line [split $comment "\n"] {
1653 append newcomment " "
1654 append newcomment $line
f6e2869f 1655 append newcomment "\n"
232475d3
PM
1656 }
1657 set comment $newcomment
1db95b00
PM
1658 }
1659 if {$comdate != {}} {
cfb4563c 1660 set cdate($id) $comdate
1db95b00 1661 }
e5c2d856
PM
1662 set commitinfo($id) [list $headline $auname $audate \
1663 $comname $comdate $comment]
1db95b00
PM
1664}
1665
f7a3e8d2 1666proc getcommit {id} {
79b2c75e 1667 global commitdata commitinfo
8ed16484 1668
f7a3e8d2
PM
1669 if {[info exists commitdata($id)]} {
1670 parsecommit $id $commitdata($id) 1
8ed16484
PM
1671 } else {
1672 readcommit $id
1673 if {![info exists commitinfo($id)]} {
d990cedf 1674 set commitinfo($id) [list [mc "No commit information available"]]
8ed16484
PM
1675 }
1676 }
1677 return 1
1678}
1679
d375ef9b
PM
1680# Expand an abbreviated commit ID to a list of full 40-char IDs that match
1681# and are present in the current view.
1682# This is fairly slow...
1683proc longid {prefix} {
1684 global varcid curview
1685
1686 set ids {}
1687 foreach match [array names varcid "$curview,$prefix*"] {
1688 lappend ids [lindex [split $match ","] 1]
1689 }
1690 return $ids
1691}
1692
887fe3c4 1693proc readrefs {} {
62d3ea65 1694 global tagids idtags headids idheads tagobjid
219ea3a9 1695 global otherrefids idotherrefs mainhead mainheadid
39816d60 1696 global selecthead selectheadid
ffe15297 1697 global hideremotes
106288cb 1698
b5c2f306
SV
1699 foreach v {tagids idtags headids idheads otherrefids idotherrefs} {
1700 catch {unset $v}
1701 }
62d3ea65
PM
1702 set refd [open [list | git show-ref -d] r]
1703 while {[gets $refd line] >= 0} {
1704 if {[string index $line 40] ne " "} continue
1705 set id [string range $line 0 39]
1706 set ref [string range $line 41 end]
1707 if {![string match "refs/*" $ref]} continue
1708 set name [string range $ref 5 end]
1709 if {[string match "remotes/*" $name]} {
ffe15297 1710 if {![string match "*/HEAD" $name] && !$hideremotes} {
62d3ea65
PM
1711 set headids($name) $id
1712 lappend idheads($id) $name
f1d83ba3 1713 }
62d3ea65
PM
1714 } elseif {[string match "heads/*" $name]} {
1715 set name [string range $name 6 end]
36a7cad6
JH
1716 set headids($name) $id
1717 lappend idheads($id) $name
62d3ea65
PM
1718 } elseif {[string match "tags/*" $name]} {
1719 # this lets refs/tags/foo^{} overwrite refs/tags/foo,
1720 # which is what we want since the former is the commit ID
1721 set name [string range $name 5 end]
1722 if {[string match "*^{}" $name]} {
1723 set name [string range $name 0 end-3]
1724 } else {
1725 set tagobjid($name) $id
1726 }
1727 set tagids($name) $id
1728 lappend idtags($id) $name
36a7cad6
JH
1729 } else {
1730 set otherrefids($name) $id
1731 lappend idotherrefs($id) $name
f1d83ba3
PM
1732 }
1733 }
062d671f 1734 catch {close $refd}
8a48571c 1735 set mainhead {}
219ea3a9 1736 set mainheadid {}
8a48571c 1737 catch {
c11ff120 1738 set mainheadid [exec git rev-parse HEAD]
8a48571c
PM
1739 set thehead [exec git symbolic-ref HEAD]
1740 if {[string match "refs/heads/*" $thehead]} {
1741 set mainhead [string range $thehead 11 end]
1742 }
1743 }
39816d60
AG
1744 set selectheadid {}
1745 if {$selecthead ne {}} {
1746 catch {
1747 set selectheadid [exec git rev-parse --verify $selecthead]
1748 }
1749 }
887fe3c4
PM
1750}
1751
8f489363
PM
1752# skip over fake commits
1753proc first_real_row {} {
7fcc92bf 1754 global nullid nullid2 numcommits
8f489363
PM
1755
1756 for {set row 0} {$row < $numcommits} {incr row} {
7fcc92bf 1757 set id [commitonrow $row]
8f489363
PM
1758 if {$id ne $nullid && $id ne $nullid2} {
1759 break
1760 }
1761 }
1762 return $row
1763}
1764
e11f1233
PM
1765# update things for a head moved to a child of its previous location
1766proc movehead {id name} {
1767 global headids idheads
1768
1769 removehead $headids($name) $name
1770 set headids($name) $id
1771 lappend idheads($id) $name
1772}
1773
1774# update things when a head has been removed
1775proc removehead {id name} {
1776 global headids idheads
1777
1778 if {$idheads($id) eq $name} {
1779 unset idheads($id)
1780 } else {
1781 set i [lsearch -exact $idheads($id) $name]
1782 if {$i >= 0} {
1783 set idheads($id) [lreplace $idheads($id) $i $i]
1784 }
1785 }
1786 unset headids($name)
1787}
1788
d93f1713
PT
1789proc ttk_toplevel {w args} {
1790 global use_ttk
1791 eval [linsert $args 0 ::toplevel $w]
1792 if {$use_ttk} {
1793 place [ttk::frame $w._toplevel_background] -x 0 -y 0 -relwidth 1 -relheight 1
1794 }
1795 return $w
1796}
1797
e7d64008
AG
1798proc make_transient {window origin} {
1799 global have_tk85
1800
1801 # In MacOS Tk 8.4 transient appears to work by setting
1802 # overrideredirect, which is utterly useless, since the
1803 # windows get no border, and are not even kept above
1804 # the parent.
1805 if {!$have_tk85 && [tk windowingsystem] eq {aqua}} return
1806
1807 wm transient $window $origin
1808
1809 # Windows fails to place transient windows normally, so
1810 # schedule a callback to center them on the parent.
1811 if {[tk windowingsystem] eq {win32}} {
1812 after idle [list tk::PlaceWindow $window widget $origin]
1813 }
1814}
1815
8d849957 1816proc show_error {w top msg {mc mc}} {
d93f1713 1817 global NS
3cb1f9c9 1818 if {![info exists NS]} {set NS ""}
d93f1713 1819 if {[wm state $top] eq "withdrawn"} { wm deiconify $top }
df3d83b1
PM
1820 message $w.m -text $msg -justify center -aspect 400
1821 pack $w.m -side top -fill x -padx 20 -pady 20
7a0ebbf8 1822 ${NS}::button $w.ok -default active -text [$mc OK] -command "destroy $top"
df3d83b1 1823 pack $w.ok -side bottom -fill x
e54be9e3
PM
1824 bind $top <Visibility> "grab $top; focus $top"
1825 bind $top <Key-Return> "destroy $top"
76f15947
AG
1826 bind $top <Key-space> "destroy $top"
1827 bind $top <Key-Escape> "destroy $top"
e54be9e3 1828 tkwait window $top
df3d83b1
PM
1829}
1830
84a76f18 1831proc error_popup {msg {owner .}} {
d93f1713
PT
1832 if {[tk windowingsystem] eq "win32"} {
1833 tk_messageBox -icon error -type ok -title [wm title .] \
1834 -parent $owner -message $msg
1835 } else {
1836 set w .error
1837 ttk_toplevel $w
1838 make_transient $w $owner
1839 show_error $w $w $msg
1840 }
098dd8a3
PM
1841}
1842
84a76f18 1843proc confirm_popup {msg {owner .}} {
d93f1713 1844 global confirm_ok NS
10299152
PM
1845 set confirm_ok 0
1846 set w .confirm
d93f1713 1847 ttk_toplevel $w
e7d64008 1848 make_transient $w $owner
10299152
PM
1849 message $w.m -text $msg -justify center -aspect 400
1850 pack $w.m -side top -fill x -padx 20 -pady 20
d93f1713 1851 ${NS}::button $w.ok -text [mc OK] -command "set confirm_ok 1; destroy $w"
10299152 1852 pack $w.ok -side left -fill x
d93f1713 1853 ${NS}::button $w.cancel -text [mc Cancel] -command "destroy $w"
10299152
PM
1854 pack $w.cancel -side right -fill x
1855 bind $w <Visibility> "grab $w; focus $w"
76f15947
AG
1856 bind $w <Key-Return> "set confirm_ok 1; destroy $w"
1857 bind $w <Key-space> "set confirm_ok 1; destroy $w"
1858 bind $w <Key-Escape> "destroy $w"
d93f1713 1859 tk::PlaceWindow $w widget $owner
10299152
PM
1860 tkwait window $w
1861 return $confirm_ok
1862}
1863
b039f0a6 1864proc setoptions {} {
d93f1713
PT
1865 if {[tk windowingsystem] ne "win32"} {
1866 option add *Panedwindow.showHandle 1 startupFile
1867 option add *Panedwindow.sashRelief raised startupFile
1868 if {[tk windowingsystem] ne "aqua"} {
1869 option add *Menu.font uifont startupFile
1870 }
1871 } else {
1872 option add *Menu.TearOff 0 startupFile
1873 }
b039f0a6
PM
1874 option add *Button.font uifont startupFile
1875 option add *Checkbutton.font uifont startupFile
1876 option add *Radiobutton.font uifont startupFile
b039f0a6
PM
1877 option add *Menubutton.font uifont startupFile
1878 option add *Label.font uifont startupFile
1879 option add *Message.font uifont startupFile
1880 option add *Entry.font uifont startupFile
d93f1713 1881 option add *Labelframe.font uifont startupFile
b039f0a6
PM
1882}
1883
79056034
PM
1884# Make a menu and submenus.
1885# m is the window name for the menu, items is the list of menu items to add.
1886# Each item is a list {mc label type description options...}
1887# mc is ignored; it's so we can put mc there to alert xgettext
1888# label is the string that appears in the menu
1889# type is cascade, command or radiobutton (should add checkbutton)
1890# description depends on type; it's the sublist for cascade, the
1891# command to invoke for command, or {variable value} for radiobutton
f2d0bbbd
PM
1892proc makemenu {m items} {
1893 menu $m
cea07cf8
AG
1894 if {[tk windowingsystem] eq {aqua}} {
1895 set Meta1 Cmd
1896 } else {
1897 set Meta1 Ctrl
1898 }
f2d0bbbd 1899 foreach i $items {
79056034
PM
1900 set name [mc [lindex $i 1]]
1901 set type [lindex $i 2]
1902 set thing [lindex $i 3]
f2d0bbbd
PM
1903 set params [list $type]
1904 if {$name ne {}} {
1905 set u [string first "&" [string map {&& x} $name]]
1906 lappend params -label [string map {&& & & {}} $name]
1907 if {$u >= 0} {
1908 lappend params -underline $u
1909 }
1910 }
1911 switch -- $type {
1912 "cascade" {
79056034 1913 set submenu [string tolower [string map {& ""} [lindex $i 1]]]
f2d0bbbd
PM
1914 lappend params -menu $m.$submenu
1915 }
1916 "command" {
1917 lappend params -command $thing
1918 }
1919 "radiobutton" {
1920 lappend params -variable [lindex $thing 0] \
1921 -value [lindex $thing 1]
1922 }
1923 }
cea07cf8
AG
1924 set tail [lrange $i 4 end]
1925 regsub -all {\yMeta1\y} $tail $Meta1 tail
1926 eval $m add $params $tail
f2d0bbbd
PM
1927 if {$type eq "cascade"} {
1928 makemenu $m.$submenu $thing
1929 }
1930 }
1931}
1932
1933# translate string and remove ampersands
1934proc mca {str} {
1935 return [string map {&& & & {}} [mc $str]]
1936}
1937
d93f1713
PT
1938proc makedroplist {w varname args} {
1939 global use_ttk
1940 if {$use_ttk} {
3cb1f9c9
PT
1941 set width 0
1942 foreach label $args {
1943 set cx [string length $label]
1944 if {$cx > $width} {set width $cx}
1945 }
1946 set gm [ttk::combobox $w -width $width -state readonly\
d93f1713
PT
1947 -textvariable $varname -values $args]
1948 } else {
1949 set gm [eval [linsert $args 0 tk_optionMenu $w $varname]]
1950 }
1951 return $gm
1952}
1953
d94f8cd6 1954proc makewindow {} {
31c0eaa8 1955 global canv canv2 canv3 linespc charspc ctext cflist cscroll
9c311b32 1956 global tabstop
b74fd579 1957 global findtype findtypemenu findloc findstring fstring geometry
887fe3c4 1958 global entries sha1entry sha1string sha1but
890fae70 1959 global diffcontextstring diffcontext
b9b86007 1960 global ignorespace
94a2eede 1961 global maincursor textcursor curtextcursor
219ea3a9 1962 global rowctxmenu fakerowmenu mergemax wrapcomment
60f7a7dc 1963 global highlight_files gdttype
3ea06f9f 1964 global searchstring sstring
60378c0c 1965 global bgcolor fgcolor bglist fglist diffcolors selectbgcolor
bb3edc8b
PM
1966 global headctxmenu progresscanv progressitem progresscoords statusw
1967 global fprogitem fprogcoord lastprogupdate progupdatepending
6df7403a 1968 global rprogitem rprogcoord rownumsel numcommits
d93f1713 1969 global have_tk85 use_ttk NS
9a40c50c 1970
79056034
PM
1971 # The "mc" arguments here are purely so that xgettext
1972 # sees the following string as needing to be translated
5fdcbb13
DS
1973 set file {
1974 mc "File" cascade {
79056034 1975 {mc "Update" command updatecommits -accelerator F5}
cea07cf8 1976 {mc "Reload" command reloadcommits -accelerator Meta1-F5}
79056034 1977 {mc "Reread references" command rereadrefs}
cea07cf8 1978 {mc "List references" command showrefs -accelerator F2}
7fb0abb1
AG
1979 {xx "" separator}
1980 {mc "Start git gui" command {exec git gui &}}
1981 {xx "" separator}
cea07cf8 1982 {mc "Quit" command doquit -accelerator Meta1-Q}
f2d0bbbd 1983 }}
5fdcbb13
DS
1984 set edit {
1985 mc "Edit" cascade {
79056034 1986 {mc "Preferences" command doprefs}
f2d0bbbd 1987 }}
5fdcbb13
DS
1988 set view {
1989 mc "View" cascade {
cea07cf8
AG
1990 {mc "New view..." command {newview 0} -accelerator Shift-F4}
1991 {mc "Edit view..." command editview -state disabled -accelerator F4}
79056034
PM
1992 {mc "Delete view" command delview -state disabled}
1993 {xx "" separator}
1994 {mc "All files" radiobutton {selectedview 0} -command {showview 0}}
f2d0bbbd 1995 }}
5fdcbb13
DS
1996 if {[tk windowingsystem] ne "aqua"} {
1997 set help {
1998 mc "Help" cascade {
1999 {mc "About gitk" command about}
2000 {mc "Key bindings" command keys}
2001 }}
2002 set bar [list $file $edit $view $help]
2003 } else {
2004 proc ::tk::mac::ShowPreferences {} {doprefs}
2005 proc ::tk::mac::Quit {} {doquit}
2006 lset file end [lreplace [lindex $file end] end-1 end]
2007 set apple {
2008 xx "Apple" cascade {
79056034 2009 {mc "About gitk" command about}
5fdcbb13
DS
2010 {xx "" separator}
2011 }}
2012 set help {
2013 mc "Help" cascade {
79056034 2014 {mc "Key bindings" command keys}
f2d0bbbd 2015 }}
5fdcbb13 2016 set bar [list $apple $file $view $help]
f2d0bbbd 2017 }
5fdcbb13 2018 makemenu .bar $bar
9a40c50c
PM
2019 . configure -menu .bar
2020
d93f1713
PT
2021 if {$use_ttk} {
2022 # cover the non-themed toplevel with a themed frame.
2023 place [ttk::frame ._main_background] -x 0 -y 0 -relwidth 1 -relheight 1
2024 }
2025
e9937d2a 2026 # the gui has upper and lower half, parts of a paned window.
d93f1713 2027 ${NS}::panedwindow .ctop -orient vertical
e9937d2a
JH
2028
2029 # possibly use assumed geometry
9ca72f4f 2030 if {![info exists geometry(pwsash0)]} {
e9937d2a
JH
2031 set geometry(topheight) [expr {15 * $linespc}]
2032 set geometry(topwidth) [expr {80 * $charspc}]
2033 set geometry(botheight) [expr {15 * $linespc}]
2034 set geometry(botwidth) [expr {50 * $charspc}]
d93f1713
PT
2035 set geometry(pwsash0) [list [expr {40 * $charspc}] 2]
2036 set geometry(pwsash1) [list [expr {60 * $charspc}] 2]
e9937d2a
JH
2037 }
2038
2039 # the upper half will have a paned window, a scroll bar to the right, and some stuff below
d93f1713
PT
2040 ${NS}::frame .tf -height $geometry(topheight) -width $geometry(topwidth)
2041 ${NS}::frame .tf.histframe
2042 ${NS}::panedwindow .tf.histframe.pwclist -orient horizontal
2043 if {!$use_ttk} {
2044 .tf.histframe.pwclist configure -sashpad 0 -handlesize 4
2045 }
e9937d2a
JH
2046
2047 # create three canvases
2048 set cscroll .tf.histframe.csb
2049 set canv .tf.histframe.pwclist.canv
9ca72f4f 2050 canvas $canv \
60378c0c 2051 -selectbackground $selectbgcolor \
f8a2c0d1 2052 -background $bgcolor -bd 0 \
9f1afe05 2053 -yscrollincr $linespc -yscrollcommand "scrollcanv $cscroll"
e9937d2a
JH
2054 .tf.histframe.pwclist add $canv
2055 set canv2 .tf.histframe.pwclist.canv2
9ca72f4f 2056 canvas $canv2 \
60378c0c 2057 -selectbackground $selectbgcolor \
f8a2c0d1 2058 -background $bgcolor -bd 0 -yscrollincr $linespc
e9937d2a
JH
2059 .tf.histframe.pwclist add $canv2
2060 set canv3 .tf.histframe.pwclist.canv3
9ca72f4f 2061 canvas $canv3 \
60378c0c 2062 -selectbackground $selectbgcolor \
f8a2c0d1 2063 -background $bgcolor -bd 0 -yscrollincr $linespc
e9937d2a 2064 .tf.histframe.pwclist add $canv3
d93f1713
PT
2065 if {$use_ttk} {
2066 bind .tf.histframe.pwclist <Map> {
2067 bind %W <Map> {}
2068 .tf.histframe.pwclist sashpos 1 [lindex $::geometry(pwsash1) 0]
2069 .tf.histframe.pwclist sashpos 0 [lindex $::geometry(pwsash0) 0]
2070 }
2071 } else {
2072 eval .tf.histframe.pwclist sash place 0 $geometry(pwsash0)
2073 eval .tf.histframe.pwclist sash place 1 $geometry(pwsash1)
2074 }
e9937d2a
JH
2075
2076 # a scroll bar to rule them
d93f1713
PT
2077 ${NS}::scrollbar $cscroll -command {allcanvs yview}
2078 if {!$use_ttk} {$cscroll configure -highlightthickness 0}
e9937d2a
JH
2079 pack $cscroll -side right -fill y
2080 bind .tf.histframe.pwclist <Configure> {resizeclistpanes %W %w}
f8a2c0d1 2081 lappend bglist $canv $canv2 $canv3
e9937d2a 2082 pack .tf.histframe.pwclist -fill both -expand 1 -side left
98f350e5 2083
e9937d2a 2084 # we have two button bars at bottom of top frame. Bar 1
d93f1713
PT
2085 ${NS}::frame .tf.bar
2086 ${NS}::frame .tf.lbar -height 15
e9937d2a
JH
2087
2088 set sha1entry .tf.bar.sha1
887fe3c4 2089 set entries $sha1entry
e9937d2a 2090 set sha1but .tf.bar.sha1label
0359ba72 2091 button $sha1but -text "[mc "SHA1 ID:"] " -state disabled -relief flat \
b039f0a6 2092 -command gotocommit -width 8
887fe3c4 2093 $sha1but conf -disabledforeground [$sha1but cget -foreground]
e9937d2a 2094 pack .tf.bar.sha1label -side left
d93f1713 2095 ${NS}::entry $sha1entry -width 40 -font textfont -textvariable sha1string
887fe3c4 2096 trace add variable sha1string write sha1change
98f350e5 2097 pack $sha1entry -side left -pady 2
d698206c
PM
2098
2099 image create bitmap bm-left -data {
2100 #define left_width 16
2101 #define left_height 16
2102 static unsigned char left_bits[] = {
2103 0x00, 0x00, 0xc0, 0x01, 0xe0, 0x00, 0x70, 0x00, 0x38, 0x00, 0x1c, 0x00,
2104 0x0e, 0x00, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0x0e, 0x00, 0x1c, 0x00,
2105 0x38, 0x00, 0x70, 0x00, 0xe0, 0x00, 0xc0, 0x01};
2106 }
2107 image create bitmap bm-right -data {
2108 #define right_width 16
2109 #define right_height 16
2110 static unsigned char right_bits[] = {
2111 0x00, 0x00, 0xc0, 0x01, 0x80, 0x03, 0x00, 0x07, 0x00, 0x0e, 0x00, 0x1c,
2112 0x00, 0x38, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0x00, 0x38, 0x00, 0x1c,
2113 0x00, 0x0e, 0x00, 0x07, 0x80, 0x03, 0xc0, 0x01};
2114 }
d93f1713 2115 ${NS}::button .tf.bar.leftbut -image bm-left -command goback \
d698206c 2116 -state disabled -width 26
e9937d2a 2117 pack .tf.bar.leftbut -side left -fill y
d93f1713 2118 ${NS}::button .tf.bar.rightbut -image bm-right -command goforw \
d698206c 2119 -state disabled -width 26
e9937d2a 2120 pack .tf.bar.rightbut -side left -fill y
d698206c 2121
d93f1713 2122 ${NS}::label .tf.bar.rowlabel -text [mc "Row"]
6df7403a 2123 set rownumsel {}
d93f1713 2124 ${NS}::label .tf.bar.rownum -width 7 -textvariable rownumsel \
6df7403a 2125 -relief sunken -anchor e
d93f1713
PT
2126 ${NS}::label .tf.bar.rowlabel2 -text "/"
2127 ${NS}::label .tf.bar.numcommits -width 7 -textvariable numcommits \
6df7403a
PM
2128 -relief sunken -anchor e
2129 pack .tf.bar.rowlabel .tf.bar.rownum .tf.bar.rowlabel2 .tf.bar.numcommits \
2130 -side left
d93f1713
PT
2131 if {!$use_ttk} {
2132 foreach w {rownum numcommits} {.tf.bar.$w configure -font textfont}
2133 }
6df7403a 2134 global selectedline
94b4a69f 2135 trace add variable selectedline write selectedline_change
6df7403a 2136
bb3edc8b
PM
2137 # Status label and progress bar
2138 set statusw .tf.bar.status
d93f1713 2139 ${NS}::label $statusw -width 15 -relief sunken
bb3edc8b 2140 pack $statusw -side left -padx 5
d93f1713
PT
2141 if {$use_ttk} {
2142 set progresscanv [ttk::progressbar .tf.bar.progress]
2143 } else {
2144 set h [expr {[font metrics uifont -linespace] + 2}]
2145 set progresscanv .tf.bar.progress
2146 canvas $progresscanv -relief sunken -height $h -borderwidth 2
2147 set progressitem [$progresscanv create rect -1 0 0 $h -fill green]
2148 set fprogitem [$progresscanv create rect -1 0 0 $h -fill yellow]
2149 set rprogitem [$progresscanv create rect -1 0 0 $h -fill red]
2150 }
2151 pack $progresscanv -side right -expand 1 -fill x -padx {0 2}
bb3edc8b
PM
2152 set progresscoords {0 0}
2153 set fprogcoord 0
a137a90f 2154 set rprogcoord 0
bb3edc8b
PM
2155 bind $progresscanv <Configure> adjustprogress
2156 set lastprogupdate [clock clicks -milliseconds]
2157 set progupdatepending 0
2158
687c8765 2159 # build up the bottom bar of upper window
d93f1713
PT
2160 ${NS}::label .tf.lbar.flabel -text "[mc "Find"] "
2161 ${NS}::button .tf.lbar.fnext -text [mc "next"] -command {dofind 1 1}
2162 ${NS}::button .tf.lbar.fprev -text [mc "prev"] -command {dofind -1 1}
2163 ${NS}::label .tf.lbar.flab2 -text " [mc "commit"] "
687c8765
PM
2164 pack .tf.lbar.flabel .tf.lbar.fnext .tf.lbar.fprev .tf.lbar.flab2 \
2165 -side left -fill y
b007ee20 2166 set gdttype [mc "containing:"]
3cb1f9c9 2167 set gm [makedroplist .tf.lbar.gdttype gdttype \
b007ee20
CS
2168 [mc "containing:"] \
2169 [mc "touching paths:"] \
2170 [mc "adding/removing string:"]]
687c8765 2171 trace add variable gdttype write gdttype_change
687c8765
PM
2172 pack .tf.lbar.gdttype -side left -fill y
2173
98f350e5 2174 set findstring {}
687c8765 2175 set fstring .tf.lbar.findstring
887fe3c4 2176 lappend entries $fstring
d93f1713 2177 ${NS}::entry $fstring -width 30 -font textfont -textvariable findstring
60f7a7dc 2178 trace add variable findstring write find_change
b007ee20 2179 set findtype [mc "Exact"]
d93f1713
PT
2180 set findtypemenu [makedroplist .tf.lbar.findtype \
2181 findtype [mc "Exact"] [mc "IgnCase"] [mc "Regexp"]]
687c8765 2182 trace add variable findtype write findcom_change
b007ee20 2183 set findloc [mc "All fields"]
d93f1713 2184 makedroplist .tf.lbar.findloc findloc [mc "All fields"] [mc "Headline"] \
b007ee20 2185 [mc "Comments"] [mc "Author"] [mc "Committer"]
60f7a7dc 2186 trace add variable findloc write find_change
687c8765
PM
2187 pack .tf.lbar.findloc -side right
2188 pack .tf.lbar.findtype -side right
2189 pack $fstring -side left -expand 1 -fill x
e9937d2a
JH
2190
2191 # Finish putting the upper half of the viewer together
2192 pack .tf.lbar -in .tf -side bottom -fill x
2193 pack .tf.bar -in .tf -side bottom -fill x
2194 pack .tf.histframe -fill both -side top -expand 1
2195 .ctop add .tf
d93f1713
PT
2196 if {!$use_ttk} {
2197 .ctop paneconfigure .tf -height $geometry(topheight)
2198 .ctop paneconfigure .tf -width $geometry(topwidth)
2199 }
e9937d2a
JH
2200
2201 # now build up the bottom
d93f1713 2202 ${NS}::panedwindow .pwbottom -orient horizontal
e9937d2a
JH
2203
2204 # lower left, a text box over search bar, scroll bar to the right
2205 # if we know window height, then that will set the lower text height, otherwise
2206 # we set lower text height which will drive window height
2207 if {[info exists geometry(main)]} {
d93f1713 2208 ${NS}::frame .bleft -width $geometry(botwidth)
e9937d2a 2209 } else {
d93f1713 2210 ${NS}::frame .bleft -width $geometry(botwidth) -height $geometry(botheight)
e9937d2a 2211 }
d93f1713
PT
2212 ${NS}::frame .bleft.top
2213 ${NS}::frame .bleft.mid
2214 ${NS}::frame .bleft.bottom
e9937d2a 2215
d93f1713 2216 ${NS}::button .bleft.top.search -text [mc "Search"] -command dosearch
e9937d2a
JH
2217 pack .bleft.top.search -side left -padx 5
2218 set sstring .bleft.top.sstring
d93f1713
PT
2219 set searchstring ""
2220 ${NS}::entry $sstring -width 20 -font textfont -textvariable searchstring
3ea06f9f
PM
2221 lappend entries $sstring
2222 trace add variable searchstring write incrsearch
2223 pack $sstring -side left -expand 1 -fill x
d93f1713 2224 ${NS}::radiobutton .bleft.mid.diff -text [mc "Diff"] \
a8d610a2 2225 -command changediffdisp -variable diffelide -value {0 0}
d93f1713 2226 ${NS}::radiobutton .bleft.mid.old -text [mc "Old version"] \
a8d610a2 2227 -command changediffdisp -variable diffelide -value {0 1}
d93f1713 2228 ${NS}::radiobutton .bleft.mid.new -text [mc "New version"] \
a8d610a2 2229 -command changediffdisp -variable diffelide -value {1 0}
d93f1713 2230 ${NS}::label .bleft.mid.labeldiffcontext -text " [mc "Lines of context"]: "
a8d610a2 2231 pack .bleft.mid.diff .bleft.mid.old .bleft.mid.new -side left
9c311b32 2232 spinbox .bleft.mid.diffcontext -width 5 -font textfont \
a41ddbb6 2233 -from 0 -increment 1 -to 10000000 \
890fae70
SP
2234 -validate all -validatecommand "diffcontextvalidate %P" \
2235 -textvariable diffcontextstring
2236 .bleft.mid.diffcontext set $diffcontext
2237 trace add variable diffcontextstring write diffcontextchange
2238 lappend entries .bleft.mid.diffcontext
2239 pack .bleft.mid.labeldiffcontext .bleft.mid.diffcontext -side left
d93f1713 2240 ${NS}::checkbutton .bleft.mid.ignspace -text [mc "Ignore space change"] \
b9b86007
SP
2241 -command changeignorespace -variable ignorespace
2242 pack .bleft.mid.ignspace -side left -padx 5
8809d691 2243 set ctext .bleft.bottom.ctext
f8a2c0d1 2244 text $ctext -background $bgcolor -foreground $fgcolor \
9c311b32 2245 -state disabled -font textfont \
8809d691
PK
2246 -yscrollcommand scrolltext -wrap none \
2247 -xscrollcommand ".bleft.bottom.sbhorizontal set"
32f1b3e4
PM
2248 if {$have_tk85} {
2249 $ctext conf -tabstyle wordprocessor
2250 }
d93f1713
PT
2251 ${NS}::scrollbar .bleft.bottom.sb -command "$ctext yview"
2252 ${NS}::scrollbar .bleft.bottom.sbhorizontal -command "$ctext xview" -orient h
e9937d2a 2253 pack .bleft.top -side top -fill x
a8d610a2 2254 pack .bleft.mid -side top -fill x
8809d691
PK
2255 grid $ctext .bleft.bottom.sb -sticky nsew
2256 grid .bleft.bottom.sbhorizontal -sticky ew
2257 grid columnconfigure .bleft.bottom 0 -weight 1
2258 grid rowconfigure .bleft.bottom 0 -weight 1
2259 grid rowconfigure .bleft.bottom 1 -weight 0
2260 pack .bleft.bottom -side top -fill both -expand 1
f8a2c0d1
PM
2261 lappend bglist $ctext
2262 lappend fglist $ctext
d2610d11 2263
f1b86294 2264 $ctext tag conf comment -wrap $wrapcomment
9c311b32 2265 $ctext tag conf filesep -font textfontbold -back "#aaaaaa"
f8a2c0d1
PM
2266 $ctext tag conf hunksep -fore [lindex $diffcolors 2]
2267 $ctext tag conf d0 -fore [lindex $diffcolors 0]
8b07dca1 2268 $ctext tag conf dresult -fore [lindex $diffcolors 1]
712fcc08
PM
2269 $ctext tag conf m0 -fore red
2270 $ctext tag conf m1 -fore blue
2271 $ctext tag conf m2 -fore green
2272 $ctext tag conf m3 -fore purple
2273 $ctext tag conf m4 -fore brown
b77b0278
PM
2274 $ctext tag conf m5 -fore "#009090"
2275 $ctext tag conf m6 -fore magenta
2276 $ctext tag conf m7 -fore "#808000"
2277 $ctext tag conf m8 -fore "#009000"
2278 $ctext tag conf m9 -fore "#ff0080"
2279 $ctext tag conf m10 -fore cyan
2280 $ctext tag conf m11 -fore "#b07070"
2281 $ctext tag conf m12 -fore "#70b0f0"
2282 $ctext tag conf m13 -fore "#70f0b0"
2283 $ctext tag conf m14 -fore "#f0b070"
2284 $ctext tag conf m15 -fore "#ff70b0"
712fcc08 2285 $ctext tag conf mmax -fore darkgrey
b77b0278 2286 set mergemax 16
9c311b32
PM
2287 $ctext tag conf mresult -font textfontbold
2288 $ctext tag conf msep -font textfontbold
712fcc08 2289 $ctext tag conf found -back yellow
e5c2d856 2290
e9937d2a 2291 .pwbottom add .bleft
d93f1713
PT
2292 if {!$use_ttk} {
2293 .pwbottom paneconfigure .bleft -width $geometry(botwidth)
2294 }
e9937d2a
JH
2295
2296 # lower right
d93f1713
PT
2297 ${NS}::frame .bright
2298 ${NS}::frame .bright.mode
2299 ${NS}::radiobutton .bright.mode.patch -text [mc "Patch"] \
f8b28a40 2300 -command reselectline -variable cmitmode -value "patch"
d93f1713 2301 ${NS}::radiobutton .bright.mode.tree -text [mc "Tree"] \
f8b28a40 2302 -command reselectline -variable cmitmode -value "tree"
e9937d2a
JH
2303 grid .bright.mode.patch .bright.mode.tree -sticky ew
2304 pack .bright.mode -side top -fill x
2305 set cflist .bright.cfiles
9c311b32 2306 set indent [font measure mainfont "nn"]
e9937d2a 2307 text $cflist \
60378c0c 2308 -selectbackground $selectbgcolor \
f8a2c0d1 2309 -background $bgcolor -foreground $fgcolor \
9c311b32 2310 -font mainfont \
7fcceed7 2311 -tabs [list $indent [expr {2 * $indent}]] \
e9937d2a 2312 -yscrollcommand ".bright.sb set" \
7fcceed7
PM
2313 -cursor [. cget -cursor] \
2314 -spacing1 1 -spacing3 1
f8a2c0d1
PM
2315 lappend bglist $cflist
2316 lappend fglist $cflist
d93f1713 2317 ${NS}::scrollbar .bright.sb -command "$cflist yview"
e9937d2a 2318 pack .bright.sb -side right -fill y
d2610d11 2319 pack $cflist -side left -fill both -expand 1
89b11d3b
PM
2320 $cflist tag configure highlight \
2321 -background [$cflist cget -selectbackground]
9c311b32 2322 $cflist tag configure bold -font mainfontbold
d2610d11 2323
e9937d2a
JH
2324 .pwbottom add .bright
2325 .ctop add .pwbottom
1db95b00 2326
b9bee115 2327 # restore window width & height if known
e9937d2a 2328 if {[info exists geometry(main)]} {
b9bee115
PM
2329 if {[scan $geometry(main) "%dx%d" w h] >= 2} {
2330 if {$w > [winfo screenwidth .]} {
2331 set w [winfo screenwidth .]
2332 }
2333 if {$h > [winfo screenheight .]} {
2334 set h [winfo screenheight .]
2335 }
2336 wm geometry . "${w}x$h"
2337 }
e9937d2a
JH
2338 }
2339
c876dbad
PT
2340 if {[info exists geometry(state)] && $geometry(state) eq "zoomed"} {
2341 wm state . $geometry(state)
2342 }
2343
d23d98d3
SP
2344 if {[tk windowingsystem] eq {aqua}} {
2345 set M1B M1
5fdcbb13 2346 set ::BM "3"
d23d98d3
SP
2347 } else {
2348 set M1B Control
5fdcbb13 2349 set ::BM "2"
d23d98d3
SP
2350 }
2351
d93f1713
PT
2352 if {$use_ttk} {
2353 bind .ctop <Map> {
2354 bind %W <Map> {}
2355 %W sashpos 0 $::geometry(topheight)
2356 }
2357 bind .pwbottom <Map> {
2358 bind %W <Map> {}
2359 %W sashpos 0 $::geometry(botwidth)
2360 }
2361 }
2362
e9937d2a
JH
2363 bind .pwbottom <Configure> {resizecdetpanes %W %w}
2364 pack .ctop -fill both -expand 1
c8dfbcf9
PM
2365 bindall <1> {selcanvline %W %x %y}
2366 #bindall <B1-Motion> {selcanvline %W %x %y}
314c3093
ML
2367 if {[tk windowingsystem] == "win32"} {
2368 bind . <MouseWheel> { windows_mousewheel_redirector %W %X %Y %D }
2369 bind $ctext <MouseWheel> { windows_mousewheel_redirector %W %X %Y %D ; break }
2370 } else {
2371 bindall <ButtonRelease-4> "allcanvs yview scroll -5 units"
2372 bindall <ButtonRelease-5> "allcanvs yview scroll 5 units"
5dd57d51
JS
2373 if {[tk windowingsystem] eq "aqua"} {
2374 bindall <MouseWheel> {
2375 set delta [expr {- (%D)}]
2376 allcanvs yview scroll $delta units
2377 }
5fdcbb13
DS
2378 bindall <Shift-MouseWheel> {
2379 set delta [expr {- (%D)}]
2380 $canv xview scroll $delta units
2381 }
5dd57d51 2382 }
314c3093 2383 }
5fdcbb13
DS
2384 bindall <$::BM> "canvscan mark %W %x %y"
2385 bindall <B$::BM-Motion> "canvscan dragto %W %x %y"
6e5f7203
RN
2386 bindkey <Home> selfirstline
2387 bindkey <End> sellastline
17386066
PM
2388 bind . <Key-Up> "selnextline -1"
2389 bind . <Key-Down> "selnextline 1"
cca5d946
PM
2390 bind . <Shift-Key-Up> "dofind -1 0"
2391 bind . <Shift-Key-Down> "dofind 1 0"
6e5f7203
RN
2392 bindkey <Key-Right> "goforw"
2393 bindkey <Key-Left> "goback"
2394 bind . <Key-Prior> "selnextpage -1"
2395 bind . <Key-Next> "selnextpage 1"
d23d98d3
SP
2396 bind . <$M1B-Home> "allcanvs yview moveto 0.0"
2397 bind . <$M1B-End> "allcanvs yview moveto 1.0"
2398 bind . <$M1B-Key-Up> "allcanvs yview scroll -1 units"
2399 bind . <$M1B-Key-Down> "allcanvs yview scroll 1 units"
2400 bind . <$M1B-Key-Prior> "allcanvs yview scroll -1 pages"
2401 bind . <$M1B-Key-Next> "allcanvs yview scroll 1 pages"
cfb4563c
PM
2402 bindkey <Key-Delete> "$ctext yview scroll -1 pages"
2403 bindkey <Key-BackSpace> "$ctext yview scroll -1 pages"
2404 bindkey <Key-space> "$ctext yview scroll 1 pages"
df3d83b1
PM
2405 bindkey p "selnextline -1"
2406 bindkey n "selnextline 1"
6e2dda35
RS
2407 bindkey z "goback"
2408 bindkey x "goforw"
2409 bindkey i "selnextline -1"
2410 bindkey k "selnextline 1"
2411 bindkey j "goback"
2412 bindkey l "goforw"
f4c54b3c 2413 bindkey b prevfile
cfb4563c
PM
2414 bindkey d "$ctext yview scroll 18 units"
2415 bindkey u "$ctext yview scroll -18 units"
97bed034 2416 bindkey / {focus $fstring}
b6e192db 2417 bindkey <Key-KP_Divide> {focus $fstring}
cca5d946
PM
2418 bindkey <Key-Return> {dofind 1 1}
2419 bindkey ? {dofind -1 1}
39ad8570 2420 bindkey f nextfile
cea07cf8
AG
2421 bind . <F5> updatecommits
2422 bind . <$M1B-F5> reloadcommits
2423 bind . <F2> showrefs
2424 bind . <Shift-F4> {newview 0}
2425 catch { bind . <Shift-Key-XF86_Switch_VT_4> {newview 0} }
2426 bind . <F4> edit_or_newview
d23d98d3 2427 bind . <$M1B-q> doquit
cca5d946
PM
2428 bind . <$M1B-f> {dofind 1 1}
2429 bind . <$M1B-g> {dofind 1 0}
d23d98d3
SP
2430 bind . <$M1B-r> dosearchback
2431 bind . <$M1B-s> dosearch
2432 bind . <$M1B-equal> {incrfont 1}
646f3a14 2433 bind . <$M1B-plus> {incrfont 1}
d23d98d3
SP
2434 bind . <$M1B-KP_Add> {incrfont 1}
2435 bind . <$M1B-minus> {incrfont -1}
2436 bind . <$M1B-KP_Subtract> {incrfont -1}
b6047c5a 2437 wm protocol . WM_DELETE_WINDOW doquit
e2f90ee4 2438 bind . <Destroy> {stop_backends}
df3d83b1 2439 bind . <Button-1> "click %W"
cca5d946 2440 bind $fstring <Key-Return> {dofind 1 1}
968ce45c 2441 bind $sha1entry <Key-Return> {gotocommit; break}
ee3dc72e 2442 bind $sha1entry <<PasteSelection>> clearsha1
7fcceed7
PM
2443 bind $cflist <1> {sel_flist %W %x %y; break}
2444 bind $cflist <B1-Motion> {sel_flist %W %x %y; break}
f8b28a40 2445 bind $cflist <ButtonRelease-1> {treeclick %W %x %y}
d277e89f
PM
2446 global ctxbut
2447 bind $cflist $ctxbut {pop_flist_menu %W %X %Y %x %y}
7cdc3556 2448 bind $ctext $ctxbut {pop_diff_menu %W %X %Y %x %y}
ea13cba1
PM
2449
2450 set maincursor [. cget -cursor]
2451 set textcursor [$ctext cget -cursor]
94a2eede 2452 set curtextcursor $textcursor
84ba7345 2453
c8dfbcf9 2454 set rowctxmenu .rowctxmenu
f2d0bbbd 2455 makemenu $rowctxmenu {
79056034
PM
2456 {mc "Diff this -> selected" command {diffvssel 0}}
2457 {mc "Diff selected -> this" command {diffvssel 1}}
2458 {mc "Make patch" command mkpatch}
2459 {mc "Create tag" command mktag}
2460 {mc "Write commit to file" command writecommit}
2461 {mc "Create new branch" command mkbranch}
2462 {mc "Cherry-pick this commit" command cherrypick}
2463 {mc "Reset HEAD branch to here" command resethead}
b9fdba7f
PM
2464 {mc "Mark this commit" command markhere}
2465 {mc "Return to mark" command gotomark}
2466 {mc "Find descendant of this and mark" command find_common_desc}
010509f2 2467 {mc "Compare with marked commit" command compare_commits}
f2d0bbbd
PM
2468 }
2469 $rowctxmenu configure -tearoff 0
10299152 2470
219ea3a9 2471 set fakerowmenu .fakerowmenu
f2d0bbbd 2472 makemenu $fakerowmenu {
79056034
PM
2473 {mc "Diff this -> selected" command {diffvssel 0}}
2474 {mc "Diff selected -> this" command {diffvssel 1}}
2475 {mc "Make patch" command mkpatch}
f2d0bbbd
PM
2476 }
2477 $fakerowmenu configure -tearoff 0
219ea3a9 2478
10299152 2479 set headctxmenu .headctxmenu
f2d0bbbd 2480 makemenu $headctxmenu {
79056034
PM
2481 {mc "Check out this branch" command cobranch}
2482 {mc "Remove this branch" command rmbranch}
f2d0bbbd
PM
2483 }
2484 $headctxmenu configure -tearoff 0
3244729a
PM
2485
2486 global flist_menu
2487 set flist_menu .flistctxmenu
f2d0bbbd 2488 makemenu $flist_menu {
79056034
PM
2489 {mc "Highlight this too" command {flist_hl 0}}
2490 {mc "Highlight this only" command {flist_hl 1}}
2491 {mc "External diff" command {external_diff}}
2492 {mc "Blame parent commit" command {external_blame 1}}
f2d0bbbd
PM
2493 }
2494 $flist_menu configure -tearoff 0
7cdc3556
AG
2495
2496 global diff_menu
2497 set diff_menu .diffctxmenu
2498 makemenu $diff_menu {
8a897742 2499 {mc "Show origin of this line" command show_line_source}
7cdc3556
AG
2500 {mc "Run git gui blame on this line" command {external_blame_diff}}
2501 }
2502 $diff_menu configure -tearoff 0
df3d83b1
PM
2503}
2504
314c3093
ML
2505# Windows sends all mouse wheel events to the current focused window, not
2506# the one where the mouse hovers, so bind those events here and redirect
2507# to the correct window
2508proc windows_mousewheel_redirector {W X Y D} {
2509 global canv canv2 canv3
2510 set w [winfo containing -displayof $W $X $Y]
2511 if {$w ne ""} {
2512 set u [expr {$D < 0 ? 5 : -5}]
2513 if {$w == $canv || $w == $canv2 || $w == $canv3} {
2514 allcanvs yview scroll $u units
2515 } else {
2516 catch {
2517 $w yview scroll $u units
2518 }
2519 }
2520 }
2521}
2522
6df7403a
PM
2523# Update row number label when selectedline changes
2524proc selectedline_change {n1 n2 op} {
2525 global selectedline rownumsel
2526
94b4a69f 2527 if {$selectedline eq {}} {
6df7403a
PM
2528 set rownumsel {}
2529 } else {
2530 set rownumsel [expr {$selectedline + 1}]
2531 }
2532}
2533
be0cd098
PM
2534# mouse-2 makes all windows scan vertically, but only the one
2535# the cursor is in scans horizontally
2536proc canvscan {op w x y} {
2537 global canv canv2 canv3
2538 foreach c [list $canv $canv2 $canv3] {
2539 if {$c == $w} {
2540 $c scan $op $x $y
2541 } else {
2542 $c scan $op 0 $y
2543 }
2544 }
2545}
2546
9f1afe05
PM
2547proc scrollcanv {cscroll f0 f1} {
2548 $cscroll set $f0 $f1
31c0eaa8 2549 drawvisible
908c3585 2550 flushhighlights
9f1afe05
PM
2551}
2552
df3d83b1
PM
2553# when we make a key binding for the toplevel, make sure
2554# it doesn't get triggered when that key is pressed in the
2555# find string entry widget.
2556proc bindkey {ev script} {
887fe3c4 2557 global entries
df3d83b1
PM
2558 bind . $ev $script
2559 set escript [bind Entry $ev]
2560 if {$escript == {}} {
2561 set escript [bind Entry <Key>]
2562 }
887fe3c4
PM
2563 foreach e $entries {
2564 bind $e $ev "$escript; break"
2565 }
df3d83b1
PM
2566}
2567
2568# set the focus back to the toplevel for any click outside
887fe3c4 2569# the entry widgets
df3d83b1 2570proc click {w} {
bd441de4
ML
2571 global ctext entries
2572 foreach e [concat $entries $ctext] {
887fe3c4 2573 if {$w == $e} return
df3d83b1 2574 }
887fe3c4 2575 focus .
0fba86b3
PM
2576}
2577
bb3edc8b
PM
2578# Adjust the progress bar for a change in requested extent or canvas size
2579proc adjustprogress {} {
2580 global progresscanv progressitem progresscoords
2581 global fprogitem fprogcoord lastprogupdate progupdatepending
d93f1713
PT
2582 global rprogitem rprogcoord use_ttk
2583
2584 if {$use_ttk} {
2585 $progresscanv configure -value [expr {int($fprogcoord * 100)}]
2586 return
2587 }
bb3edc8b
PM
2588
2589 set w [expr {[winfo width $progresscanv] - 4}]
2590 set x0 [expr {$w * [lindex $progresscoords 0]}]
2591 set x1 [expr {$w * [lindex $progresscoords 1]}]
2592 set h [winfo height $progresscanv]
2593 $progresscanv coords $progressitem $x0 0 $x1 $h
2594 $progresscanv coords $fprogitem 0 0 [expr {$w * $fprogcoord}] $h
a137a90f 2595 $progresscanv coords $rprogitem 0 0 [expr {$w * $rprogcoord}] $h
bb3edc8b
PM
2596 set now [clock clicks -milliseconds]
2597 if {$now >= $lastprogupdate + 100} {
2598 set progupdatepending 0
2599 update
2600 } elseif {!$progupdatepending} {
2601 set progupdatepending 1
2602 after [expr {$lastprogupdate + 100 - $now}] doprogupdate
2603 }
2604}
2605
2606proc doprogupdate {} {
2607 global lastprogupdate progupdatepending
2608
2609 if {$progupdatepending} {
2610 set progupdatepending 0
2611 set lastprogupdate [clock clicks -milliseconds]
2612 update
2613 }
2614}
2615
0fba86b3 2616proc savestuff {w} {
32f1b3e4 2617 global canv canv2 canv3 mainfont textfont uifont tabstop
712fcc08 2618 global stuffsaved findmergefiles maxgraphpct
219ea3a9 2619 global maxwidth showneartags showlocalchanges
2d480856 2620 global viewname viewfiles viewargs viewargscmd viewperm nextviewnum
7a39a17a 2621 global cmitmode wrapcomment datetimeformat limitdiffs
5497f7a2 2622 global colors uicolor bgcolor fgcolor diffcolors diffcontext selectbgcolor
d93f1713 2623 global autoselect extdifftool perfile_attrs markbgcolor use_ttk
0cc08ff7 2624 global hideremotes want_ttk
4ef17537 2625
0fba86b3 2626 if {$stuffsaved} return
df3d83b1 2627 if {![winfo viewable .]} return
0fba86b3 2628 catch {
9bedb0e1 2629 if {[file exists ~/.gitk-new]} {file delete -force ~/.gitk-new}
0fba86b3 2630 set f [open "~/.gitk-new" w]
9832e4f2
PM
2631 if {$::tcl_platform(platform) eq {windows}} {
2632 file attributes "~/.gitk-new" -hidden true
2633 }
f0654861
PM
2634 puts $f [list set mainfont $mainfont]
2635 puts $f [list set textfont $textfont]
4840be66 2636 puts $f [list set uifont $uifont]
7e12f1a6 2637 puts $f [list set tabstop $tabstop]
f0654861 2638 puts $f [list set findmergefiles $findmergefiles]
8d858d1a 2639 puts $f [list set maxgraphpct $maxgraphpct]
04c13d38 2640 puts $f [list set maxwidth $maxwidth]
f8b28a40 2641 puts $f [list set cmitmode $cmitmode]
f1b86294 2642 puts $f [list set wrapcomment $wrapcomment]
95293b58 2643 puts $f [list set autoselect $autoselect]
b8ab2e17 2644 puts $f [list set showneartags $showneartags]
ffe15297 2645 puts $f [list set hideremotes $hideremotes]
219ea3a9 2646 puts $f [list set showlocalchanges $showlocalchanges]
e8b5f4be 2647 puts $f [list set datetimeformat $datetimeformat]
7a39a17a 2648 puts $f [list set limitdiffs $limitdiffs]
5497f7a2 2649 puts $f [list set uicolor $uicolor]
0cc08ff7 2650 puts $f [list set want_ttk $want_ttk]
f8a2c0d1
PM
2651 puts $f [list set bgcolor $bgcolor]
2652 puts $f [list set fgcolor $fgcolor]
2653 puts $f [list set colors $colors]
2654 puts $f [list set diffcolors $diffcolors]
e3e901be 2655 puts $f [list set markbgcolor $markbgcolor]
890fae70 2656 puts $f [list set diffcontext $diffcontext]
60378c0c 2657 puts $f [list set selectbgcolor $selectbgcolor]
314f5de1 2658 puts $f [list set extdifftool $extdifftool]
39ee47ef 2659 puts $f [list set perfile_attrs $perfile_attrs]
e9937d2a 2660
b6047c5a 2661 puts $f "set geometry(main) [wm geometry .]"
c876dbad 2662 puts $f "set geometry(state) [wm state .]"
e9937d2a
JH
2663 puts $f "set geometry(topwidth) [winfo width .tf]"
2664 puts $f "set geometry(topheight) [winfo height .tf]"
d93f1713
PT
2665 if {$use_ttk} {
2666 puts $f "set geometry(pwsash0) \"[.tf.histframe.pwclist sashpos 0] 1\""
2667 puts $f "set geometry(pwsash1) \"[.tf.histframe.pwclist sashpos 1] 1\""
2668 } else {
2669 puts $f "set geometry(pwsash0) \"[.tf.histframe.pwclist sash coord 0]\""
2670 puts $f "set geometry(pwsash1) \"[.tf.histframe.pwclist sash coord 1]\""
2671 }
e9937d2a
JH
2672 puts $f "set geometry(botwidth) [winfo width .bleft]"
2673 puts $f "set geometry(botheight) [winfo height .bleft]"
2674
a90a6d24
PM
2675 puts -nonewline $f "set permviews {"
2676 for {set v 0} {$v < $nextviewnum} {incr v} {
2677 if {$viewperm($v)} {
2d480856 2678 puts $f "{[list $viewname($v) $viewfiles($v) $viewargs($v) $viewargscmd($v)]}"
a90a6d24
PM
2679 }
2680 }
2681 puts $f "}"
0fba86b3
PM
2682 close $f
2683 file rename -force "~/.gitk-new" "~/.gitk"
2684 }
2685 set stuffsaved 1
1db95b00
PM
2686}
2687
43bddeb4 2688proc resizeclistpanes {win w} {
d93f1713 2689 global oldwidth use_ttk
418c4c7b 2690 if {[info exists oldwidth($win)]} {
d93f1713
PT
2691 if {$use_ttk} {
2692 set s0 [$win sashpos 0]
2693 set s1 [$win sashpos 1]
2694 } else {
2695 set s0 [$win sash coord 0]
2696 set s1 [$win sash coord 1]
2697 }
43bddeb4
PM
2698 if {$w < 60} {
2699 set sash0 [expr {int($w/2 - 2)}]
2700 set sash1 [expr {int($w*5/6 - 2)}]
2701 } else {
2702 set factor [expr {1.0 * $w / $oldwidth($win)}]
2703 set sash0 [expr {int($factor * [lindex $s0 0])}]
2704 set sash1 [expr {int($factor * [lindex $s1 0])}]
2705 if {$sash0 < 30} {
2706 set sash0 30
2707 }
2708 if {$sash1 < $sash0 + 20} {
2ed49d54 2709 set sash1 [expr {$sash0 + 20}]
43bddeb4
PM
2710 }
2711 if {$sash1 > $w - 10} {
2ed49d54 2712 set sash1 [expr {$w - 10}]
43bddeb4 2713 if {$sash0 > $sash1 - 20} {
2ed49d54 2714 set sash0 [expr {$sash1 - 20}]
43bddeb4
PM
2715 }
2716 }
2717 }
d93f1713
PT
2718 if {$use_ttk} {
2719 $win sashpos 0 $sash0
2720 $win sashpos 1 $sash1
2721 } else {
2722 $win sash place 0 $sash0 [lindex $s0 1]
2723 $win sash place 1 $sash1 [lindex $s1 1]
2724 }
43bddeb4
PM
2725 }
2726 set oldwidth($win) $w
2727}
2728
2729proc resizecdetpanes {win w} {
d93f1713 2730 global oldwidth use_ttk
418c4c7b 2731 if {[info exists oldwidth($win)]} {
d93f1713
PT
2732 if {$use_ttk} {
2733 set s0 [$win sashpos 0]
2734 } else {
2735 set s0 [$win sash coord 0]
2736 }
43bddeb4
PM
2737 if {$w < 60} {
2738 set sash0 [expr {int($w*3/4 - 2)}]
2739 } else {
2740 set factor [expr {1.0 * $w / $oldwidth($win)}]
2741 set sash0 [expr {int($factor * [lindex $s0 0])}]
2742 if {$sash0 < 45} {
2743 set sash0 45
2744 }
2745 if {$sash0 > $w - 15} {
2ed49d54 2746 set sash0 [expr {$w - 15}]
43bddeb4
PM
2747 }
2748 }
d93f1713
PT
2749 if {$use_ttk} {
2750 $win sashpos 0 $sash0
2751 } else {
2752 $win sash place 0 $sash0 [lindex $s0 1]
2753 }
43bddeb4
PM
2754 }
2755 set oldwidth($win) $w
2756}
2757
b5721c72
PM
2758proc allcanvs args {
2759 global canv canv2 canv3
2760 eval $canv $args
2761 eval $canv2 $args
2762 eval $canv3 $args
2763}
2764
2765proc bindall {event action} {
2766 global canv canv2 canv3
2767 bind $canv $event $action
2768 bind $canv2 $event $action
2769 bind $canv3 $event $action
2770}
2771
9a40c50c 2772proc about {} {
d93f1713 2773 global uifont NS
9a40c50c
PM
2774 set w .about
2775 if {[winfo exists $w]} {
2776 raise $w
2777 return
2778 }
d93f1713 2779 ttk_toplevel $w
d990cedf 2780 wm title $w [mc "About gitk"]
e7d64008 2781 make_transient $w .
d990cedf 2782 message $w.m -text [mc "
9f1afe05 2783Gitk - a commit viewer for git
9a40c50c 2784
7963791e 2785Copyright © 2005-2009 Paul Mackerras
9a40c50c 2786
d990cedf 2787Use and redistribute under the terms of the GNU General Public License"] \
3a950e9a
ER
2788 -justify center -aspect 400 -border 2 -bg white -relief groove
2789 pack $w.m -side top -fill x -padx 2 -pady 2
d93f1713 2790 ${NS}::button $w.ok -text [mc "Close"] -command "destroy $w" -default active
9a40c50c 2791 pack $w.ok -side bottom
3a950e9a
ER
2792 bind $w <Visibility> "focus $w.ok"
2793 bind $w <Key-Escape> "destroy $w"
2794 bind $w <Key-Return> "destroy $w"
d93f1713 2795 tk::PlaceWindow $w widget .
9a40c50c
PM
2796}
2797
4e95e1f7 2798proc keys {} {
d93f1713 2799 global NS
4e95e1f7
PM
2800 set w .keys
2801 if {[winfo exists $w]} {
2802 raise $w
2803 return
2804 }
d23d98d3
SP
2805 if {[tk windowingsystem] eq {aqua}} {
2806 set M1T Cmd
2807 } else {
2808 set M1T Ctrl
2809 }
d93f1713 2810 ttk_toplevel $w
d990cedf 2811 wm title $w [mc "Gitk key bindings"]
e7d64008 2812 make_transient $w .
3d2c998e
MB
2813 message $w.m -text "
2814[mc "Gitk key bindings:"]
2815
2816[mc "<%s-Q> Quit" $M1T]
2817[mc "<Home> Move to first commit"]
2818[mc "<End> Move to last commit"]
2819[mc "<Up>, p, i Move up one commit"]
2820[mc "<Down>, n, k Move down one commit"]
2821[mc "<Left>, z, j Go back in history list"]
2822[mc "<Right>, x, l Go forward in history list"]
2823[mc "<PageUp> Move up one page in commit list"]
2824[mc "<PageDown> Move down one page in commit list"]
2825[mc "<%s-Home> Scroll to top of commit list" $M1T]
2826[mc "<%s-End> Scroll to bottom of commit list" $M1T]
2827[mc "<%s-Up> Scroll commit list up one line" $M1T]
2828[mc "<%s-Down> Scroll commit list down one line" $M1T]
2829[mc "<%s-PageUp> Scroll commit list up one page" $M1T]
2830[mc "<%s-PageDown> Scroll commit list down one page" $M1T]
2831[mc "<Shift-Up> Find backwards (upwards, later commits)"]
2832[mc "<Shift-Down> Find forwards (downwards, earlier commits)"]
2833[mc "<Delete>, b Scroll diff view up one page"]
2834[mc "<Backspace> Scroll diff view up one page"]
2835[mc "<Space> Scroll diff view down one page"]
2836[mc "u Scroll diff view up 18 lines"]
2837[mc "d Scroll diff view down 18 lines"]
2838[mc "<%s-F> Find" $M1T]
2839[mc "<%s-G> Move to next find hit" $M1T]
2840[mc "<Return> Move to next find hit"]
97bed034 2841[mc "/ Focus the search box"]
3d2c998e
MB
2842[mc "? Move to previous find hit"]
2843[mc "f Scroll diff view to next file"]
2844[mc "<%s-S> Search for next hit in diff view" $M1T]
2845[mc "<%s-R> Search for previous hit in diff view" $M1T]
2846[mc "<%s-KP+> Increase font size" $M1T]
2847[mc "<%s-plus> Increase font size" $M1T]
2848[mc "<%s-KP-> Decrease font size" $M1T]
2849[mc "<%s-minus> Decrease font size" $M1T]
2850[mc "<F5> Update"]
2851" \
3a950e9a
ER
2852 -justify left -bg white -border 2 -relief groove
2853 pack $w.m -side top -fill both -padx 2 -pady 2
d93f1713 2854 ${NS}::button $w.ok -text [mc "Close"] -command "destroy $w" -default active
76f15947 2855 bind $w <Key-Escape> [list destroy $w]
4e95e1f7 2856 pack $w.ok -side bottom
3a950e9a
ER
2857 bind $w <Visibility> "focus $w.ok"
2858 bind $w <Key-Escape> "destroy $w"
2859 bind $w <Key-Return> "destroy $w"
4e95e1f7
PM
2860}
2861
7fcceed7
PM
2862# Procedures for manipulating the file list window at the
2863# bottom right of the overall window.
f8b28a40
PM
2864
2865proc treeview {w l openlevs} {
2866 global treecontents treediropen treeheight treeparent treeindex
2867
2868 set ix 0
2869 set treeindex() 0
2870 set lev 0
2871 set prefix {}
2872 set prefixend -1
2873 set prefendstack {}
2874 set htstack {}
2875 set ht 0
2876 set treecontents() {}
2877 $w conf -state normal
2878 foreach f $l {
2879 while {[string range $f 0 $prefixend] ne $prefix} {
2880 if {$lev <= $openlevs} {
2881 $w mark set e:$treeindex($prefix) "end -1c"
2882 $w mark gravity e:$treeindex($prefix) left
2883 }
2884 set treeheight($prefix) $ht
2885 incr ht [lindex $htstack end]
2886 set htstack [lreplace $htstack end end]
2887 set prefixend [lindex $prefendstack end]
2888 set prefendstack [lreplace $prefendstack end end]
2889 set prefix [string range $prefix 0 $prefixend]
2890 incr lev -1
2891 }
2892 set tail [string range $f [expr {$prefixend+1}] end]
2893 while {[set slash [string first "/" $tail]] >= 0} {
2894 lappend htstack $ht
2895 set ht 0
2896 lappend prefendstack $prefixend
2897 incr prefixend [expr {$slash + 1}]
2898 set d [string range $tail 0 $slash]
2899 lappend treecontents($prefix) $d
2900 set oldprefix $prefix
2901 append prefix $d
2902 set treecontents($prefix) {}
2903 set treeindex($prefix) [incr ix]
2904 set treeparent($prefix) $oldprefix
2905 set tail [string range $tail [expr {$slash+1}] end]
2906 if {$lev <= $openlevs} {
2907 set ht 1
2908 set treediropen($prefix) [expr {$lev < $openlevs}]
2909 set bm [expr {$lev == $openlevs? "tri-rt": "tri-dn"}]
2910 $w mark set d:$ix "end -1c"
2911 $w mark gravity d:$ix left
2912 set str "\n"
2913 for {set i 0} {$i < $lev} {incr i} {append str "\t"}
2914 $w insert end $str
2915 $w image create end -align center -image $bm -padx 1 \
2916 -name a:$ix
45a9d505 2917 $w insert end $d [highlight_tag $prefix]
f8b28a40
PM
2918 $w mark set s:$ix "end -1c"
2919 $w mark gravity s:$ix left
2920 }
2921 incr lev
2922 }
2923 if {$tail ne {}} {
2924 if {$lev <= $openlevs} {
2925 incr ht
2926 set str "\n"
2927 for {set i 0} {$i < $lev} {incr i} {append str "\t"}
2928 $w insert end $str
45a9d505 2929 $w insert end $tail [highlight_tag $f]
f8b28a40
PM
2930 }
2931 lappend treecontents($prefix) $tail
2932 }
2933 }
2934 while {$htstack ne {}} {
2935 set treeheight($prefix) $ht
2936 incr ht [lindex $htstack end]
2937 set htstack [lreplace $htstack end end]
096e96b4
BD
2938 set prefixend [lindex $prefendstack end]
2939 set prefendstack [lreplace $prefendstack end end]
2940 set prefix [string range $prefix 0 $prefixend]
f8b28a40
PM
2941 }
2942 $w conf -state disabled
2943}
2944
2945proc linetoelt {l} {
2946 global treeheight treecontents
2947
2948 set y 2
2949 set prefix {}
2950 while {1} {
2951 foreach e $treecontents($prefix) {
2952 if {$y == $l} {
2953 return "$prefix$e"
2954 }
2955 set n 1
2956 if {[string index $e end] eq "/"} {
2957 set n $treeheight($prefix$e)
2958 if {$y + $n > $l} {
2959 append prefix $e
2960 incr y
2961 break
2962 }
2963 }
2964 incr y $n
2965 }
2966 }
2967}
2968
45a9d505
PM
2969proc highlight_tree {y prefix} {
2970 global treeheight treecontents cflist
2971
2972 foreach e $treecontents($prefix) {
2973 set path $prefix$e
2974 if {[highlight_tag $path] ne {}} {
2975 $cflist tag add bold $y.0 "$y.0 lineend"
2976 }
2977 incr y
2978 if {[string index $e end] eq "/" && $treeheight($path) > 1} {
2979 set y [highlight_tree $y $path]
2980 }
2981 }
2982 return $y
2983}
2984
f8b28a40
PM
2985proc treeclosedir {w dir} {
2986 global treediropen treeheight treeparent treeindex
2987
2988 set ix $treeindex($dir)
2989 $w conf -state normal
2990 $w delete s:$ix e:$ix
2991 set treediropen($dir) 0
2992 $w image configure a:$ix -image tri-rt
2993 $w conf -state disabled
2994 set n [expr {1 - $treeheight($dir)}]
2995 while {$dir ne {}} {
2996 incr treeheight($dir) $n
2997 set dir $treeparent($dir)
2998 }
2999}
3000
3001proc treeopendir {w dir} {
3002 global treediropen treeheight treeparent treecontents treeindex
3003
3004 set ix $treeindex($dir)
3005 $w conf -state normal
3006 $w image configure a:$ix -image tri-dn
3007 $w mark set e:$ix s:$ix
3008 $w mark gravity e:$ix right
3009 set lev 0
3010 set str "\n"
3011 set n [llength $treecontents($dir)]
3012 for {set x $dir} {$x ne {}} {set x $treeparent($x)} {
3013 incr lev
3014 append str "\t"
3015 incr treeheight($x) $n
3016 }
3017 foreach e $treecontents($dir) {
45a9d505 3018 set de $dir$e
f8b28a40 3019 if {[string index $e end] eq "/"} {
f8b28a40
PM
3020 set iy $treeindex($de)
3021 $w mark set d:$iy e:$ix
3022 $w mark gravity d:$iy left
3023 $w insert e:$ix $str
3024 set treediropen($de) 0
3025 $w image create e:$ix -align center -image tri-rt -padx 1 \
3026 -name a:$iy
45a9d505 3027 $w insert e:$ix $e [highlight_tag $de]
f8b28a40
PM
3028 $w mark set s:$iy e:$ix
3029 $w mark gravity s:$iy left
3030 set treeheight($de) 1
3031 } else {
3032 $w insert e:$ix $str
45a9d505 3033 $w insert e:$ix $e [highlight_tag $de]
f8b28a40
PM
3034 }
3035 }
b8a640ee 3036 $w mark gravity e:$ix right
f8b28a40
PM
3037 $w conf -state disabled
3038 set treediropen($dir) 1
3039 set top [lindex [split [$w index @0,0] .] 0]
3040 set ht [$w cget -height]
3041 set l [lindex [split [$w index s:$ix] .] 0]
3042 if {$l < $top} {
3043 $w yview $l.0
3044 } elseif {$l + $n + 1 > $top + $ht} {
3045 set top [expr {$l + $n + 2 - $ht}]
3046 if {$l < $top} {
3047 set top $l
3048 }
3049 $w yview $top.0
3050 }
3051}
3052
3053proc treeclick {w x y} {
3054 global treediropen cmitmode ctext cflist cflist_top
3055
3056 if {$cmitmode ne "tree"} return
3057 if {![info exists cflist_top]} return
3058 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3059 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
3060 $cflist tag add highlight $l.0 "$l.0 lineend"
3061 set cflist_top $l
3062 if {$l == 1} {
3063 $ctext yview 1.0
3064 return
3065 }
3066 set e [linetoelt $l]
3067 if {[string index $e end] ne "/"} {
3068 showfile $e
3069 } elseif {$treediropen($e)} {
3070 treeclosedir $w $e
3071 } else {
3072 treeopendir $w $e
3073 }
3074}
3075
3076proc setfilelist {id} {
8a897742 3077 global treefilelist cflist jump_to_here
f8b28a40
PM
3078
3079 treeview $cflist $treefilelist($id) 0
8a897742
PM
3080 if {$jump_to_here ne {}} {
3081 set f [lindex $jump_to_here 0]
3082 if {[lsearch -exact $treefilelist($id) $f] >= 0} {
3083 showfile $f
3084 }
3085 }
f8b28a40
PM
3086}
3087
3088image create bitmap tri-rt -background black -foreground blue -data {
3089 #define tri-rt_width 13
3090 #define tri-rt_height 13
3091 static unsigned char tri-rt_bits[] = {
3092 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x30, 0x00, 0x70, 0x00, 0xf0, 0x00,
3093 0xf0, 0x01, 0xf0, 0x00, 0x70, 0x00, 0x30, 0x00, 0x10, 0x00, 0x00, 0x00,
3094 0x00, 0x00};
3095} -maskdata {
3096 #define tri-rt-mask_width 13
3097 #define tri-rt-mask_height 13
3098 static unsigned char tri-rt-mask_bits[] = {
3099 0x08, 0x00, 0x18, 0x00, 0x38, 0x00, 0x78, 0x00, 0xf8, 0x00, 0xf8, 0x01,
3100 0xf8, 0x03, 0xf8, 0x01, 0xf8, 0x00, 0x78, 0x00, 0x38, 0x00, 0x18, 0x00,
3101 0x08, 0x00};
3102}
3103image create bitmap tri-dn -background black -foreground blue -data {
3104 #define tri-dn_width 13
3105 #define tri-dn_height 13
3106 static unsigned char tri-dn_bits[] = {
3107 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x07, 0xf8, 0x03,
3108 0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3109 0x00, 0x00};
3110} -maskdata {
3111 #define tri-dn-mask_width 13
3112 #define tri-dn-mask_height 13
3113 static unsigned char tri-dn-mask_bits[] = {
3114 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x1f, 0xfe, 0x0f, 0xfc, 0x07,
3115 0xf8, 0x03, 0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
3116 0x00, 0x00};
3117}
3118
887c996e
PM
3119image create bitmap reficon-T -background black -foreground yellow -data {
3120 #define tagicon_width 13
3121 #define tagicon_height 9
3122 static unsigned char tagicon_bits[] = {
3123 0x00, 0x00, 0x00, 0x00, 0xf0, 0x07, 0xf8, 0x07,
3124 0xfc, 0x07, 0xf8, 0x07, 0xf0, 0x07, 0x00, 0x00, 0x00, 0x00};
3125} -maskdata {
3126 #define tagicon-mask_width 13
3127 #define tagicon-mask_height 9
3128 static unsigned char tagicon-mask_bits[] = {
3129 0x00, 0x00, 0xf0, 0x0f, 0xf8, 0x0f, 0xfc, 0x0f,
3130 0xfe, 0x0f, 0xfc, 0x0f, 0xf8, 0x0f, 0xf0, 0x0f, 0x00, 0x00};
3131}
3132set rectdata {
3133 #define headicon_width 13
3134 #define headicon_height 9
3135 static unsigned char headicon_bits[] = {
3136 0x00, 0x00, 0x00, 0x00, 0xf8, 0x07, 0xf8, 0x07,
3137 0xf8, 0x07, 0xf8, 0x07, 0xf8, 0x07, 0x00, 0x00, 0x00, 0x00};
3138}
3139set rectmask {
3140 #define headicon-mask_width 13
3141 #define headicon-mask_height 9
3142 static unsigned char headicon-mask_bits[] = {
3143 0x00, 0x00, 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f,
3144 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f, 0x00, 0x00};
3145}
3146image create bitmap reficon-H -background black -foreground green \
3147 -data $rectdata -maskdata $rectmask
3148image create bitmap reficon-o -background black -foreground "#ddddff" \
3149 -data $rectdata -maskdata $rectmask
3150
7fcceed7 3151proc init_flist {first} {
7fcc92bf 3152 global cflist cflist_top difffilestart
7fcceed7
PM
3153
3154 $cflist conf -state normal
3155 $cflist delete 0.0 end
3156 if {$first ne {}} {
3157 $cflist insert end $first
3158 set cflist_top 1
7fcceed7
PM
3159 $cflist tag add highlight 1.0 "1.0 lineend"
3160 } else {
3161 catch {unset cflist_top}
3162 }
3163 $cflist conf -state disabled
3164 set difffilestart {}
3165}
3166
63b79191
PM
3167proc highlight_tag {f} {
3168 global highlight_paths
3169
3170 foreach p $highlight_paths {
3171 if {[string match $p $f]} {
3172 return "bold"
3173 }
3174 }
3175 return {}
3176}
3177
3178proc highlight_filelist {} {
45a9d505 3179 global cmitmode cflist
63b79191 3180
45a9d505
PM
3181 $cflist conf -state normal
3182 if {$cmitmode ne "tree"} {
63b79191
PM
3183 set end [lindex [split [$cflist index end] .] 0]
3184 for {set l 2} {$l < $end} {incr l} {
3185 set line [$cflist get $l.0 "$l.0 lineend"]
3186 if {[highlight_tag $line] ne {}} {
3187 $cflist tag add bold $l.0 "$l.0 lineend"
3188 }
3189 }
45a9d505
PM
3190 } else {
3191 highlight_tree 2 {}
63b79191 3192 }
45a9d505 3193 $cflist conf -state disabled
63b79191
PM
3194}
3195
3196proc unhighlight_filelist {} {
45a9d505 3197 global cflist
63b79191 3198
45a9d505
PM
3199 $cflist conf -state normal
3200 $cflist tag remove bold 1.0 end
3201 $cflist conf -state disabled
63b79191
PM
3202}
3203
f8b28a40 3204proc add_flist {fl} {
45a9d505 3205 global cflist
7fcceed7 3206
45a9d505
PM
3207 $cflist conf -state normal
3208 foreach f $fl {
3209 $cflist insert end "\n"
3210 $cflist insert end $f [highlight_tag $f]
7fcceed7 3211 }
45a9d505 3212 $cflist conf -state disabled
7fcceed7
PM
3213}
3214
3215proc sel_flist {w x y} {
45a9d505 3216 global ctext difffilestart cflist cflist_top cmitmode
7fcceed7 3217
f8b28a40 3218 if {$cmitmode eq "tree"} return
7fcceed7
PM
3219 if {![info exists cflist_top]} return
3220 set l [lindex [split [$w index "@$x,$y"] "."] 0]
89b11d3b
PM
3221 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
3222 $cflist tag add highlight $l.0 "$l.0 lineend"
3223 set cflist_top $l
f8b28a40
PM
3224 if {$l == 1} {
3225 $ctext yview 1.0
3226 } else {
3227 catch {$ctext yview [lindex $difffilestart [expr {$l - 2}]]}
7fcceed7 3228 }
7fcceed7
PM
3229}
3230
3244729a
PM
3231proc pop_flist_menu {w X Y x y} {
3232 global ctext cflist cmitmode flist_menu flist_menu_file
3233 global treediffs diffids
3234
bb3edc8b 3235 stopfinding
3244729a
PM
3236 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3237 if {$l <= 1} return
3238 if {$cmitmode eq "tree"} {
3239 set e [linetoelt $l]
3240 if {[string index $e end] eq "/"} return
3241 } else {
3242 set e [lindex $treediffs($diffids) [expr {$l-2}]]
3243 }
3244 set flist_menu_file $e
314f5de1
TA
3245 set xdiffstate "normal"
3246 if {$cmitmode eq "tree"} {
3247 set xdiffstate "disabled"
3248 }
3249 # Disable "External diff" item in tree mode
3250 $flist_menu entryconf 2 -state $xdiffstate
3244729a
PM
3251 tk_popup $flist_menu $X $Y
3252}
3253
7cdc3556
AG
3254proc find_ctext_fileinfo {line} {
3255 global ctext_file_names ctext_file_lines
3256
3257 set ok [bsearch $ctext_file_lines $line]
3258 set tline [lindex $ctext_file_lines $ok]
3259
3260 if {$ok >= [llength $ctext_file_lines] || $line < $tline} {
3261 return {}
3262 } else {
3263 return [list [lindex $ctext_file_names $ok] $tline]
3264 }
3265}
3266
3267proc pop_diff_menu {w X Y x y} {
3268 global ctext diff_menu flist_menu_file
3269 global diff_menu_txtpos diff_menu_line
3270 global diff_menu_filebase
3271
7cdc3556
AG
3272 set diff_menu_txtpos [split [$w index "@$x,$y"] "."]
3273 set diff_menu_line [lindex $diff_menu_txtpos 0]
190ec52c
PM
3274 # don't pop up the menu on hunk-separator or file-separator lines
3275 if {[lsearch -glob [$ctext tag names $diff_menu_line.0] "*sep"] >= 0} {
3276 return
3277 }
3278 stopfinding
7cdc3556
AG
3279 set f [find_ctext_fileinfo $diff_menu_line]
3280 if {$f eq {}} return
3281 set flist_menu_file [lindex $f 0]
3282 set diff_menu_filebase [lindex $f 1]
3283 tk_popup $diff_menu $X $Y
3284}
3285
3244729a 3286proc flist_hl {only} {
bb3edc8b 3287 global flist_menu_file findstring gdttype
3244729a
PM
3288
3289 set x [shellquote $flist_menu_file]
b007ee20 3290 if {$only || $findstring eq {} || $gdttype ne [mc "touching paths:"]} {
bb3edc8b 3291 set findstring $x
3244729a 3292 } else {
bb3edc8b 3293 append findstring " " $x
3244729a 3294 }
b007ee20 3295 set gdttype [mc "touching paths:"]
3244729a
PM
3296}
3297
c21398be
PM
3298proc gitknewtmpdir {} {
3299 global diffnum gitktmpdir gitdir
3300
3301 if {![info exists gitktmpdir]} {
3302 set gitktmpdir [file join [file dirname $gitdir] \
3303 [format ".gitk-tmp.%s" [pid]]]
3304 if {[catch {file mkdir $gitktmpdir} err]} {
3305 error_popup "[mc "Error creating temporary directory %s:" $gitktmpdir] $err"
3306 unset gitktmpdir
3307 return {}
3308 }
3309 set diffnum 0
3310 }
3311 incr diffnum
3312 set diffdir [file join $gitktmpdir $diffnum]
3313 if {[catch {file mkdir $diffdir} err]} {
3314 error_popup "[mc "Error creating temporary directory %s:" $diffdir] $err"
3315 return {}
3316 }
3317 return $diffdir
3318}
3319
314f5de1
TA
3320proc save_file_from_commit {filename output what} {
3321 global nullfile
3322
3323 if {[catch {exec git show $filename -- > $output} err]} {
3324 if {[string match "fatal: bad revision *" $err]} {
3325 return $nullfile
3326 }
3945d2c0 3327 error_popup "[mc "Error getting \"%s\" from %s:" $filename $what] $err"
314f5de1
TA
3328 return {}
3329 }
3330 return $output
3331}
3332
3333proc external_diff_get_one_file {diffid filename diffdir} {
3334 global nullid nullid2 nullfile
3335 global gitdir
3336
3337 if {$diffid == $nullid} {
3338 set difffile [file join [file dirname $gitdir] $filename]
3339 if {[file exists $difffile]} {
3340 return $difffile
3341 }
3342 return $nullfile
3343 }
3344 if {$diffid == $nullid2} {
3345 set difffile [file join $diffdir "\[index\] [file tail $filename]"]
3346 return [save_file_from_commit :$filename $difffile index]
3347 }
3348 set difffile [file join $diffdir "\[$diffid\] [file tail $filename]"]
3349 return [save_file_from_commit $diffid:$filename $difffile \
3350 "revision $diffid"]
3351}
3352
3353proc external_diff {} {
c21398be 3354 global nullid nullid2
314f5de1
TA
3355 global flist_menu_file
3356 global diffids
c21398be 3357 global extdifftool
314f5de1
TA
3358
3359 if {[llength $diffids] == 1} {
3360 # no reference commit given
3361 set diffidto [lindex $diffids 0]
3362 if {$diffidto eq $nullid} {
3363 # diffing working copy with index
3364 set diffidfrom $nullid2
3365 } elseif {$diffidto eq $nullid2} {
3366 # diffing index with HEAD
3367 set diffidfrom "HEAD"
3368 } else {
3369 # use first parent commit
3370 global parentlist selectedline
3371 set diffidfrom [lindex $parentlist $selectedline 0]
3372 }
3373 } else {
3374 set diffidfrom [lindex $diffids 0]
3375 set diffidto [lindex $diffids 1]
3376 }
3377
3378 # make sure that several diffs wont collide
c21398be
PM
3379 set diffdir [gitknewtmpdir]
3380 if {$diffdir eq {}} return
314f5de1
TA
3381
3382 # gather files to diff
3383 set difffromfile [external_diff_get_one_file $diffidfrom $flist_menu_file $diffdir]
3384 set difftofile [external_diff_get_one_file $diffidto $flist_menu_file $diffdir]
3385
3386 if {$difffromfile ne {} && $difftofile ne {}} {
b575b2f1
PT
3387 set cmd [list [shellsplit $extdifftool] $difffromfile $difftofile]
3388 if {[catch {set fl [open |$cmd r]} err]} {
314f5de1 3389 file delete -force $diffdir
3945d2c0 3390 error_popup "$extdifftool: [mc "command failed:"] $err"
314f5de1
TA
3391 } else {
3392 fconfigure $fl -blocking 0
3393 filerun $fl [list delete_at_eof $fl $diffdir]
3394 }
3395 }
3396}
3397
7cdc3556
AG
3398proc find_hunk_blamespec {base line} {
3399 global ctext
3400
3401 # Find and parse the hunk header
3402 set s_lix [$ctext search -backwards -regexp ^@@ "$line.0 lineend" $base.0]
3403 if {$s_lix eq {}} return
3404
3405 set s_line [$ctext get $s_lix "$s_lix + 1 lines"]
3406 if {![regexp {^@@@*(( -\d+(,\d+)?)+) \+(\d+)(,\d+)? @@} $s_line \
3407 s_line old_specs osz osz1 new_line nsz]} {
3408 return
3409 }
3410
3411 # base lines for the parents
3412 set base_lines [list $new_line]
3413 foreach old_spec [lrange [split $old_specs " "] 1 end] {
3414 if {![regexp -- {-(\d+)(,\d+)?} $old_spec \
3415 old_spec old_line osz]} {
3416 return
3417 }
3418 lappend base_lines $old_line
3419 }
3420
3421 # Now scan the lines to determine offset within the hunk
7cdc3556
AG
3422 set max_parent [expr {[llength $base_lines]-2}]
3423 set dline 0
3424 set s_lno [lindex [split $s_lix "."] 0]
3425
190ec52c
PM
3426 # Determine if the line is removed
3427 set chunk [$ctext get $line.0 "$line.1 + $max_parent chars"]
3428 if {[string match {[-+ ]*} $chunk]} {
7cdc3556
AG
3429 set removed_idx [string first "-" $chunk]
3430 # Choose a parent index
190ec52c
PM
3431 if {$removed_idx >= 0} {
3432 set parent $removed_idx
3433 } else {
3434 set unchanged_idx [string first " " $chunk]
3435 if {$unchanged_idx >= 0} {
3436 set parent $unchanged_idx
7cdc3556 3437 } else {
190ec52c
PM
3438 # blame the current commit
3439 set parent -1
7cdc3556
AG
3440 }
3441 }
3442 # then count other lines that belong to it
190ec52c
PM
3443 for {set i $line} {[incr i -1] > $s_lno} {} {
3444 set chunk [$ctext get $i.0 "$i.1 + $max_parent chars"]
3445 # Determine if the line is removed
3446 set removed_idx [string first "-" $chunk]
3447 if {$parent >= 0} {
3448 set code [string index $chunk $parent]
3449 if {$code eq "-" || ($removed_idx < 0 && $code ne "+")} {
3450 incr dline
3451 }
3452 } else {
3453 if {$removed_idx < 0} {
3454 incr dline
3455 }
7cdc3556
AG
3456 }
3457 }
190ec52c
PM
3458 incr parent
3459 } else {
3460 set parent 0
7cdc3556
AG
3461 }
3462
7cdc3556
AG
3463 incr dline [lindex $base_lines $parent]
3464 return [list $parent $dline]
3465}
3466
3467proc external_blame_diff {} {
8b07dca1 3468 global currentid cmitmode
7cdc3556
AG
3469 global diff_menu_txtpos diff_menu_line
3470 global diff_menu_filebase flist_menu_file
3471
3472 if {$cmitmode eq "tree"} {
3473 set parent_idx 0
190ec52c 3474 set line [expr {$diff_menu_line - $diff_menu_filebase}]
7cdc3556
AG
3475 } else {
3476 set hinfo [find_hunk_blamespec $diff_menu_filebase $diff_menu_line]
3477 if {$hinfo ne {}} {
3478 set parent_idx [lindex $hinfo 0]
3479 set line [lindex $hinfo 1]
3480 } else {
3481 set parent_idx 0
3482 set line 0
3483 }
3484 }
3485
3486 external_blame $parent_idx $line
3487}
3488
fc4977e1
PM
3489# Find the SHA1 ID of the blob for file $fname in the index
3490# at stage 0 or 2
3491proc index_sha1 {fname} {
3492 set f [open [list | git ls-files -s $fname] r]
3493 while {[gets $f line] >= 0} {
3494 set info [lindex [split $line "\t"] 0]
3495 set stage [lindex $info 2]
3496 if {$stage eq "0" || $stage eq "2"} {
3497 close $f
3498 return [lindex $info 1]
3499 }
3500 }
3501 close $f
3502 return {}
3503}
3504
9712b81a
PM
3505# Turn an absolute path into one relative to the current directory
3506proc make_relative {f} {
a4390ace
MH
3507 if {[file pathtype $f] eq "relative"} {
3508 return $f
3509 }
9712b81a
PM
3510 set elts [file split $f]
3511 set here [file split [pwd]]
3512 set ei 0
3513 set hi 0
3514 set res {}
3515 foreach d $here {
3516 if {$ei < $hi || $ei >= [llength $elts] || [lindex $elts $ei] ne $d} {
3517 lappend res ".."
3518 } else {
3519 incr ei
3520 }
3521 incr hi
3522 }
3523 set elts [concat $res [lrange $elts $ei end]]
3524 return [eval file join $elts]
3525}
3526
7cdc3556 3527proc external_blame {parent_idx {line {}}} {
9712b81a 3528 global flist_menu_file gitdir
77aa0ae8
AG
3529 global nullid nullid2
3530 global parentlist selectedline currentid
3531
3532 if {$parent_idx > 0} {
3533 set base_commit [lindex $parentlist $selectedline [expr {$parent_idx-1}]]
3534 } else {
3535 set base_commit $currentid
3536 }
3537
3538 if {$base_commit eq {} || $base_commit eq $nullid || $base_commit eq $nullid2} {
3539 error_popup [mc "No such commit"]
3540 return
3541 }
3542
7cdc3556
AG
3543 set cmdline [list git gui blame]
3544 if {$line ne {} && $line > 1} {
3545 lappend cmdline "--line=$line"
3546 }
9712b81a
PM
3547 set f [file join [file dirname $gitdir] $flist_menu_file]
3548 # Unfortunately it seems git gui blame doesn't like
3549 # being given an absolute path...
3550 set f [make_relative $f]
3551 lappend cmdline $base_commit $f
7cdc3556 3552 if {[catch {eval exec $cmdline &} err]} {
3945d2c0 3553 error_popup "[mc "git gui blame: command failed:"] $err"
77aa0ae8
AG
3554 }
3555}
3556
8a897742
PM
3557proc show_line_source {} {
3558 global cmitmode currentid parents curview blamestuff blameinst
3559 global diff_menu_line diff_menu_filebase flist_menu_file
fc4977e1 3560 global nullid nullid2 gitdir
8a897742 3561
fc4977e1 3562 set from_index {}
8a897742
PM
3563 if {$cmitmode eq "tree"} {
3564 set id $currentid
3565 set line [expr {$diff_menu_line - $diff_menu_filebase}]
3566 } else {
3567 set h [find_hunk_blamespec $diff_menu_filebase $diff_menu_line]
3568 if {$h eq {}} return
3569 set pi [lindex $h 0]
3570 if {$pi == 0} {
3571 mark_ctext_line $diff_menu_line
3572 return
3573 }
fc4977e1
PM
3574 incr pi -1
3575 if {$currentid eq $nullid} {
3576 if {$pi > 0} {
3577 # must be a merge in progress...
3578 if {[catch {
3579 # get the last line from .git/MERGE_HEAD
3580 set f [open [file join $gitdir MERGE_HEAD] r]
3581 set id [lindex [split [read $f] "\n"] end-1]
3582 close $f
3583 } err]} {
3584 error_popup [mc "Couldn't read merge head: %s" $err]
3585 return
3586 }
3587 } elseif {$parents($curview,$currentid) eq $nullid2} {
3588 # need to do the blame from the index
3589 if {[catch {
3590 set from_index [index_sha1 $flist_menu_file]
3591 } err]} {
3592 error_popup [mc "Error reading index: %s" $err]
3593 return
3594 }
9712b81a
PM
3595 } else {
3596 set id $parents($curview,$currentid)
fc4977e1
PM
3597 }
3598 } else {
3599 set id [lindex $parents($curview,$currentid) $pi]
3600 }
8a897742
PM
3601 set line [lindex $h 1]
3602 }
fc4977e1
PM
3603 set blameargs {}
3604 if {$from_index ne {}} {
3605 lappend blameargs | git cat-file blob $from_index
3606 }
3607 lappend blameargs | git blame -p -L$line,+1
3608 if {$from_index ne {}} {
3609 lappend blameargs --contents -
3610 } else {
3611 lappend blameargs $id
3612 }
9712b81a 3613 lappend blameargs -- [file join [file dirname $gitdir] $flist_menu_file]
8a897742 3614 if {[catch {
fc4977e1 3615 set f [open $blameargs r]
8a897742
PM
3616 } err]} {
3617 error_popup [mc "Couldn't start git blame: %s" $err]
3618 return
3619 }
f3413079 3620 nowbusy blaming [mc "Searching"]
8a897742
PM
3621 fconfigure $f -blocking 0
3622 set i [reg_instance $f]
3623 set blamestuff($i) {}
3624 set blameinst $i
3625 filerun $f [list read_line_source $f $i]
3626}
3627
3628proc stopblaming {} {
3629 global blameinst
3630
3631 if {[info exists blameinst]} {
3632 stop_instance $blameinst
3633 unset blameinst
f3413079 3634 notbusy blaming
8a897742
PM
3635 }
3636}
3637
3638proc read_line_source {fd inst} {
fc4977e1 3639 global blamestuff curview commfd blameinst nullid nullid2
8a897742
PM
3640
3641 while {[gets $fd line] >= 0} {
3642 lappend blamestuff($inst) $line
3643 }
3644 if {![eof $fd]} {
3645 return 1
3646 }
3647 unset commfd($inst)
3648 unset blameinst
f3413079 3649 notbusy blaming
8a897742
PM
3650 fconfigure $fd -blocking 1
3651 if {[catch {close $fd} err]} {
3652 error_popup [mc "Error running git blame: %s" $err]
3653 return 0
3654 }
3655
3656 set fname {}
3657 set line [split [lindex $blamestuff($inst) 0] " "]
3658 set id [lindex $line 0]
3659 set lnum [lindex $line 1]
3660 if {[string length $id] == 40 && [string is xdigit $id] &&
3661 [string is digit -strict $lnum]} {
3662 # look for "filename" line
3663 foreach l $blamestuff($inst) {
3664 if {[string match "filename *" $l]} {
3665 set fname [string range $l 9 end]
3666 break
3667 }
3668 }
3669 }
3670 if {$fname ne {}} {
3671 # all looks good, select it
fc4977e1
PM
3672 if {$id eq $nullid} {
3673 # blame uses all-zeroes to mean not committed,
3674 # which would mean a change in the index
3675 set id $nullid2
3676 }
8a897742
PM
3677 if {[commitinview $id $curview]} {
3678 selectline [rowofcommit $id] 1 [list $fname $lnum]
3679 } else {
3680 error_popup [mc "That line comes from commit %s, \
3681 which is not in this view" [shortids $id]]
3682 }
3683 } else {
3684 puts "oops couldn't parse git blame output"
3685 }
3686 return 0
3687}
3688
314f5de1
TA
3689# delete $dir when we see eof on $f (presumably because the child has exited)
3690proc delete_at_eof {f dir} {
3691 while {[gets $f line] >= 0} {}
3692 if {[eof $f]} {
3693 if {[catch {close $f} err]} {
3945d2c0 3694 error_popup "[mc "External diff viewer failed:"] $err"
314f5de1
TA
3695 }
3696 file delete -force $dir
3697 return 0
3698 }
3699 return 1
3700}
3701
098dd8a3
PM
3702# Functions for adding and removing shell-type quoting
3703
3704proc shellquote {str} {
3705 if {![string match "*\['\"\\ \t]*" $str]} {
3706 return $str
3707 }
3708 if {![string match "*\['\"\\]*" $str]} {
3709 return "\"$str\""
3710 }
3711 if {![string match "*'*" $str]} {
3712 return "'$str'"
3713 }
3714 return "\"[string map {\" \\\" \\ \\\\} $str]\""
3715}
3716
3717proc shellarglist {l} {
3718 set str {}
3719 foreach a $l {
3720 if {$str ne {}} {
3721 append str " "
3722 }
3723 append str [shellquote $a]
3724 }
3725 return $str
3726}
3727
3728proc shelldequote {str} {
3729 set ret {}
3730 set used -1
3731 while {1} {
3732 incr used
3733 if {![regexp -start $used -indices "\['\"\\\\ \t]" $str first]} {
3734 append ret [string range $str $used end]
3735 set used [string length $str]
3736 break
3737 }
3738 set first [lindex $first 0]
3739 set ch [string index $str $first]
3740 if {$first > $used} {
3741 append ret [string range $str $used [expr {$first - 1}]]
3742 set used $first
3743 }
3744 if {$ch eq " " || $ch eq "\t"} break
3745 incr used
3746 if {$ch eq "'"} {
3747 set first [string first "'" $str $used]
3748 if {$first < 0} {
3749 error "unmatched single-quote"
3750 }
3751 append ret [string range $str $used [expr {$first - 1}]]
3752 set used $first
3753 continue
3754 }
3755 if {$ch eq "\\"} {
3756 if {$used >= [string length $str]} {
3757 error "trailing backslash"
3758 }
3759 append ret [string index $str $used]
3760 continue
3761 }
3762 # here ch == "\""
3763 while {1} {
3764 if {![regexp -start $used -indices "\[\"\\\\]" $str first]} {
3765 error "unmatched double-quote"
3766 }
3767 set first [lindex $first 0]
3768 set ch [string index $str $first]
3769 if {$first > $used} {
3770 append ret [string range $str $used [expr {$first - 1}]]
3771 set used $first
3772 }
3773 if {$ch eq "\""} break
3774 incr used
3775 append ret [string index $str $used]
3776 incr used
3777 }
3778 }
3779 return [list $used $ret]
3780}
3781
3782proc shellsplit {str} {
3783 set l {}
3784 while {1} {
3785 set str [string trimleft $str]
3786 if {$str eq {}} break
3787 set dq [shelldequote $str]
3788 set n [lindex $dq 0]
3789 set word [lindex $dq 1]
3790 set str [string range $str $n end]
3791 lappend l $word
3792 }
3793 return $l
3794}
3795
7fcceed7
PM
3796# Code to implement multiple views
3797
da7c24dd 3798proc newview {ishighlight} {
218a900b
AG
3799 global nextviewnum newviewname newishighlight
3800 global revtreeargs viewargscmd newviewopts curview
50b44ece 3801
da7c24dd 3802 set newishighlight $ishighlight
50b44ece
PM
3803 set top .gitkview
3804 if {[winfo exists $top]} {
3805 raise $top
3806 return
3807 }
a3a1f579 3808 set newviewname($nextviewnum) "[mc "View"] $nextviewnum"
218a900b
AG
3809 set newviewopts($nextviewnum,perm) 0
3810 set newviewopts($nextviewnum,cmd) $viewargscmd($curview)
3811 decode_view_opts $nextviewnum $revtreeargs
d990cedf 3812 vieweditor $top $nextviewnum [mc "Gitk view definition"]
d16c0812
PM
3813}
3814
218a900b 3815set known_view_options {
13d40b61
EN
3816 {perm b . {} {mc "Remember this view"}}
3817 {reflabel l + {} {mc "References (space separated list):"}}
3818 {refs t15 .. {} {mc "Branches & tags:"}}
3819 {allrefs b *. "--all" {mc "All refs"}}
3820 {branches b . "--branches" {mc "All (local) branches"}}
3821 {tags b . "--tags" {mc "All tags"}}
3822 {remotes b . "--remotes" {mc "All remote-tracking branches"}}
3823 {commitlbl l + {} {mc "Commit Info (regular expressions):"}}
3824 {author t15 .. "--author=*" {mc "Author:"}}
3825 {committer t15 . "--committer=*" {mc "Committer:"}}
3826 {loginfo t15 .. "--grep=*" {mc "Commit Message:"}}
3827 {allmatch b .. "--all-match" {mc "Matches all Commit Info criteria"}}
3828 {changes_l l + {} {mc "Changes to Files:"}}
3829 {pickaxe_s r0 . {} {mc "Fixed String"}}
3830 {pickaxe_t r1 . "--pickaxe-regex" {mc "Regular Expression"}}
3831 {pickaxe t15 .. "-S*" {mc "Search string:"}}
3832 {datelabel l + {} {mc "Commit Dates (\"2 weeks ago\", \"2009-03-17 15:27:38\", \"March 17, 2009 15:27:38\"):"}}
3833 {since t15 .. {"--since=*" "--after=*"} {mc "Since:"}}
3834 {until t15 . {"--until=*" "--before=*"} {mc "Until:"}}
3835 {limit_lbl l + {} {mc "Limit and/or skip a number of revisions (positive integer):"}}
3836 {limit t10 *. "--max-count=*" {mc "Number to show:"}}
3837 {skip t10 . "--skip=*" {mc "Number to skip:"}}
3838 {misc_lbl l + {} {mc "Miscellaneous options:"}}
3839 {dorder b *. {"--date-order" "-d"} {mc "Strictly sort by date"}}
3840 {lright b . "--left-right" {mc "Mark branch sides"}}
3841 {first b . "--first-parent" {mc "Limit to first parent"}}
f687aaa8 3842 {smplhst b . "--simplify-by-decoration" {mc "Simple history"}}
13d40b61
EN
3843 {args t50 *. {} {mc "Additional arguments to git log:"}}
3844 {allpaths path + {} {mc "Enter files and directories to include, one per line:"}}
3845 {cmd t50= + {} {mc "Command to generate more commits to include:"}}
218a900b
AG
3846 }
3847
3848proc encode_view_opts {n} {
3849 global known_view_options newviewopts
3850
3851 set rargs [list]
3852 foreach opt $known_view_options {
3853 set patterns [lindex $opt 3]
3854 if {$patterns eq {}} continue
3855 set pattern [lindex $patterns 0]
3856
218a900b 3857 if {[lindex $opt 1] eq "b"} {
13d40b61 3858 set val $newviewopts($n,[lindex $opt 0])
218a900b
AG
3859 if {$val} {
3860 lappend rargs $pattern
3861 }
13d40b61
EN
3862 } elseif {[regexp {^r(\d+)$} [lindex $opt 1] type value]} {
3863 regexp {^(.*_)} [lindex $opt 0] uselessvar button_id
3864 set val $newviewopts($n,$button_id)
3865 if {$val eq $value} {
3866 lappend rargs $pattern
3867 }
218a900b 3868 } else {
13d40b61 3869 set val $newviewopts($n,[lindex $opt 0])
218a900b
AG
3870 set val [string trim $val]
3871 if {$val ne {}} {
3872 set pfix [string range $pattern 0 end-1]
3873 lappend rargs $pfix$val
3874 }
3875 }
3876 }
13d40b61 3877 set rargs [concat $rargs [shellsplit $newviewopts($n,refs)]]
218a900b
AG
3878 return [concat $rargs [shellsplit $newviewopts($n,args)]]
3879}
3880
3881proc decode_view_opts {n view_args} {
3882 global known_view_options newviewopts
3883
3884 foreach opt $known_view_options {
13d40b61 3885 set id [lindex $opt 0]
218a900b 3886 if {[lindex $opt 1] eq "b"} {
13d40b61
EN
3887 # Checkboxes
3888 set val 0
3889 } elseif {[regexp {^r(\d+)$} [lindex $opt 1]]} {
3890 # Radiobuttons
3891 regexp {^(.*_)} $id uselessvar id
218a900b
AG
3892 set val 0
3893 } else {
13d40b61 3894 # Text fields
218a900b
AG
3895 set val {}
3896 }
13d40b61 3897 set newviewopts($n,$id) $val
218a900b
AG
3898 }
3899 set oargs [list]
13d40b61 3900 set refargs [list]
218a900b
AG
3901 foreach arg $view_args {
3902 if {[regexp -- {^-([0-9]+)$} $arg arg cnt]
3903 && ![info exists found(limit)]} {
3904 set newviewopts($n,limit) $cnt
3905 set found(limit) 1
3906 continue
3907 }
3908 catch { unset val }
3909 foreach opt $known_view_options {
3910 set id [lindex $opt 0]
3911 if {[info exists found($id)]} continue
3912 foreach pattern [lindex $opt 3] {
3913 if {![string match $pattern $arg]} continue
13d40b61
EN
3914 if {[lindex $opt 1] eq "b"} {
3915 # Check buttons
3916 set val 1
3917 } elseif {[regexp {^r(\d+)$} [lindex $opt 1] match num]} {
3918 # Radio buttons
3919 regexp {^(.*_)} $id uselessvar id
3920 set val $num
3921 } else {
3922 # Text input fields
218a900b
AG
3923 set size [string length $pattern]
3924 set val [string range $arg [expr {$size-1}] end]
218a900b
AG
3925 }
3926 set newviewopts($n,$id) $val
3927 set found($id) 1
3928 break
3929 }
3930 if {[info exists val]} break
3931 }
3932 if {[info exists val]} continue
13d40b61
EN
3933 if {[regexp {^-} $arg]} {
3934 lappend oargs $arg
3935 } else {
3936 lappend refargs $arg
3937 }
218a900b 3938 }
13d40b61 3939 set newviewopts($n,refs) [shellarglist $refargs]
218a900b
AG
3940 set newviewopts($n,args) [shellarglist $oargs]
3941}
3942
cea07cf8
AG
3943proc edit_or_newview {} {
3944 global curview
3945
3946 if {$curview > 0} {
3947 editview
3948 } else {
3949 newview 0
3950 }
3951}
3952
d16c0812
PM
3953proc editview {} {
3954 global curview
218a900b
AG
3955 global viewname viewperm newviewname newviewopts
3956 global viewargs viewargscmd
d16c0812
PM
3957
3958 set top .gitkvedit-$curview
3959 if {[winfo exists $top]} {
3960 raise $top
3961 return
3962 }
218a900b
AG
3963 set newviewname($curview) $viewname($curview)
3964 set newviewopts($curview,perm) $viewperm($curview)
3965 set newviewopts($curview,cmd) $viewargscmd($curview)
3966 decode_view_opts $curview $viewargs($curview)
b56e0a9a 3967 vieweditor $top $curview "[mc "Gitk: edit view"] $viewname($curview)"
d16c0812
PM
3968}
3969
3970proc vieweditor {top n title} {
218a900b 3971 global newviewname newviewopts viewfiles bgcolor
d93f1713 3972 global known_view_options NS
d16c0812 3973
d93f1713 3974 ttk_toplevel $top
e0a01995 3975 wm title $top [concat $title [mc "-- criteria for selecting revisions"]]
e7d64008 3976 make_transient $top .
218a900b
AG
3977
3978 # View name
d93f1713 3979 ${NS}::frame $top.nfr
eae7d64a 3980 ${NS}::label $top.nl -text [mc "View Name"]
d93f1713 3981 ${NS}::entry $top.name -width 20 -textvariable newviewname($n)
218a900b 3982 pack $top.nfr -in $top -fill x -pady 5 -padx 3
13d40b61
EN
3983 pack $top.nl -in $top.nfr -side left -padx {0 5}
3984 pack $top.name -in $top.nfr -side left -padx {0 25}
218a900b
AG
3985
3986 # View options
3987 set cframe $top.nfr
3988 set cexpand 0
3989 set cnt 0
3990 foreach opt $known_view_options {
3991 set id [lindex $opt 0]
3992 set type [lindex $opt 1]
3993 set flags [lindex $opt 2]
3994 set title [eval [lindex $opt 4]]
3995 set lxpad 0
3996
3997 if {$flags eq "+" || $flags eq "*"} {
3998 set cframe $top.fr$cnt
3999 incr cnt
d93f1713 4000 ${NS}::frame $cframe
218a900b
AG
4001 pack $cframe -in $top -fill x -pady 3 -padx 3
4002 set cexpand [expr {$flags eq "*"}]
13d40b61
EN
4003 } elseif {$flags eq ".." || $flags eq "*."} {
4004 set cframe $top.fr$cnt
4005 incr cnt
eae7d64a 4006 ${NS}::frame $cframe
13d40b61
EN
4007 pack $cframe -in $top -fill x -pady 3 -padx [list 15 3]
4008 set cexpand [expr {$flags eq "*."}]
218a900b
AG
4009 } else {
4010 set lxpad 5
4011 }
4012
13d40b61 4013 if {$type eq "l"} {
eae7d64a 4014 ${NS}::label $cframe.l_$id -text $title
13d40b61
EN
4015 pack $cframe.l_$id -in $cframe -side left -pady [list 3 0] -anchor w
4016 } elseif {$type eq "b"} {
d93f1713 4017 ${NS}::checkbutton $cframe.c_$id -text $title -variable newviewopts($n,$id)
218a900b
AG
4018 pack $cframe.c_$id -in $cframe -side left \
4019 -padx [list $lxpad 0] -expand $cexpand -anchor w
13d40b61
EN
4020 } elseif {[regexp {^r(\d+)$} $type type sz]} {
4021 regexp {^(.*_)} $id uselessvar button_id
eae7d64a 4022 ${NS}::radiobutton $cframe.c_$id -text $title -variable newviewopts($n,$button_id) -value $sz
13d40b61
EN
4023 pack $cframe.c_$id -in $cframe -side left \
4024 -padx [list $lxpad 0] -expand $cexpand -anchor w
218a900b 4025 } elseif {[regexp {^t(\d+)$} $type type sz]} {
d93f1713
PT
4026 ${NS}::label $cframe.l_$id -text $title
4027 ${NS}::entry $cframe.e_$id -width $sz -background $bgcolor \
218a900b
AG
4028 -textvariable newviewopts($n,$id)
4029 pack $cframe.l_$id -in $cframe -side left -padx [list $lxpad 0]
4030 pack $cframe.e_$id -in $cframe -side left -expand 1 -fill x
4031 } elseif {[regexp {^t(\d+)=$} $type type sz]} {
d93f1713
PT
4032 ${NS}::label $cframe.l_$id -text $title
4033 ${NS}::entry $cframe.e_$id -width $sz -background $bgcolor \
218a900b
AG
4034 -textvariable newviewopts($n,$id)
4035 pack $cframe.l_$id -in $cframe -side top -pady [list 3 0] -anchor w
4036 pack $cframe.e_$id -in $cframe -side top -fill x
13d40b61 4037 } elseif {$type eq "path"} {
eae7d64a 4038 ${NS}::label $top.l -text $title
13d40b61
EN
4039 pack $top.l -in $top -side top -pady [list 3 0] -anchor w -padx 3
4040 text $top.t -width 40 -height 5 -background $bgcolor -font uifont
4041 if {[info exists viewfiles($n)]} {
4042 foreach f $viewfiles($n) {
4043 $top.t insert end $f
4044 $top.t insert end "\n"
4045 }
4046 $top.t delete {end - 1c} end
4047 $top.t mark set insert 0.0
4048 }
4049 pack $top.t -in $top -side top -pady [list 0 5] -fill both -expand 1 -padx 3
218a900b
AG
4050 }
4051 }
4052
d93f1713
PT
4053 ${NS}::frame $top.buts
4054 ${NS}::button $top.buts.ok -text [mc "OK"] -command [list newviewok $top $n]
4055 ${NS}::button $top.buts.apply -text [mc "Apply (F5)"] -command [list newviewok $top $n 1]
4056 ${NS}::button $top.buts.can -text [mc "Cancel"] -command [list destroy $top]
218a900b
AG
4057 bind $top <Control-Return> [list newviewok $top $n]
4058 bind $top <F5> [list newviewok $top $n 1]
76f15947 4059 bind $top <Escape> [list destroy $top]
218a900b 4060 grid $top.buts.ok $top.buts.apply $top.buts.can
50b44ece
PM
4061 grid columnconfigure $top.buts 0 -weight 1 -uniform a
4062 grid columnconfigure $top.buts 1 -weight 1 -uniform a
218a900b
AG
4063 grid columnconfigure $top.buts 2 -weight 1 -uniform a
4064 pack $top.buts -in $top -side top -fill x
50b44ece
PM
4065 focus $top.t
4066}
4067
908c3585 4068proc doviewmenu {m first cmd op argv} {
da7c24dd
PM
4069 set nmenu [$m index end]
4070 for {set i $first} {$i <= $nmenu} {incr i} {
4071 if {[$m entrycget $i -command] eq $cmd} {
908c3585 4072 eval $m $op $i $argv
da7c24dd 4073 break
d16c0812
PM
4074 }
4075 }
da7c24dd
PM
4076}
4077
4078proc allviewmenus {n op args} {
687c8765 4079 # global viewhlmenu
908c3585 4080
3cd204e5 4081 doviewmenu .bar.view 5 [list showview $n] $op $args
687c8765 4082 # doviewmenu $viewhlmenu 1 [list addvhighlight $n] $op $args
d16c0812
PM
4083}
4084
218a900b 4085proc newviewok {top n {apply 0}} {
da7c24dd 4086 global nextviewnum newviewperm newviewname newishighlight
d16c0812 4087 global viewname viewfiles viewperm selectedview curview
218a900b 4088 global viewargs viewargscmd newviewopts viewhlmenu
50b44ece 4089
098dd8a3 4090 if {[catch {
218a900b 4091 set newargs [encode_view_opts $n]
098dd8a3 4092 } err]} {
84a76f18 4093 error_popup "[mc "Error in commit selection arguments:"] $err" $top
098dd8a3
PM
4094 return
4095 }
50b44ece 4096 set files {}
d16c0812 4097 foreach f [split [$top.t get 0.0 end] "\n"] {
50b44ece
PM
4098 set ft [string trim $f]
4099 if {$ft ne {}} {
4100 lappend files $ft
4101 }
4102 }
d16c0812
PM
4103 if {![info exists viewfiles($n)]} {
4104 # creating a new view
4105 incr nextviewnum
4106 set viewname($n) $newviewname($n)
218a900b 4107 set viewperm($n) $newviewopts($n,perm)
d16c0812 4108 set viewfiles($n) $files
098dd8a3 4109 set viewargs($n) $newargs
218a900b 4110 set viewargscmd($n) $newviewopts($n,cmd)
da7c24dd
PM
4111 addviewmenu $n
4112 if {!$newishighlight} {
7eb3cb9c 4113 run showview $n
da7c24dd 4114 } else {
7eb3cb9c 4115 run addvhighlight $n
da7c24dd 4116 }
d16c0812
PM
4117 } else {
4118 # editing an existing view
218a900b 4119 set viewperm($n) $newviewopts($n,perm)
d16c0812
PM
4120 if {$newviewname($n) ne $viewname($n)} {
4121 set viewname($n) $newviewname($n)
3cd204e5 4122 doviewmenu .bar.view 5 [list showview $n] \
908c3585 4123 entryconf [list -label $viewname($n)]
687c8765
PM
4124 # doviewmenu $viewhlmenu 1 [list addvhighlight $n] \
4125 # entryconf [list -label $viewname($n) -value $viewname($n)]
d16c0812 4126 }
2d480856 4127 if {$files ne $viewfiles($n) || $newargs ne $viewargs($n) || \
218a900b 4128 $newviewopts($n,cmd) ne $viewargscmd($n)} {
d16c0812 4129 set viewfiles($n) $files
098dd8a3 4130 set viewargs($n) $newargs
218a900b 4131 set viewargscmd($n) $newviewopts($n,cmd)
d16c0812 4132 if {$curview == $n} {
7fcc92bf 4133 run reloadcommits
d16c0812
PM
4134 }
4135 }
4136 }
218a900b 4137 if {$apply} return
d16c0812 4138 catch {destroy $top}
50b44ece
PM
4139}
4140
4141proc delview {} {
7fcc92bf 4142 global curview viewperm hlview selectedhlview
50b44ece
PM
4143
4144 if {$curview == 0} return
908c3585 4145 if {[info exists hlview] && $hlview == $curview} {
b007ee20 4146 set selectedhlview [mc "None"]
908c3585
PM
4147 unset hlview
4148 }
da7c24dd 4149 allviewmenus $curview delete
a90a6d24 4150 set viewperm($curview) 0
50b44ece
PM
4151 showview 0
4152}
4153
da7c24dd 4154proc addviewmenu {n} {
908c3585 4155 global viewname viewhlmenu
da7c24dd
PM
4156
4157 .bar.view add radiobutton -label $viewname($n) \
4158 -command [list showview $n] -variable selectedview -value $n
687c8765
PM
4159 #$viewhlmenu add radiobutton -label $viewname($n) \
4160 # -command [list addvhighlight $n] -variable selectedhlview
da7c24dd
PM
4161}
4162
50b44ece 4163proc showview {n} {
3ed31a81 4164 global curview cached_commitrow ordertok
f5f3c2e2 4165 global displayorder parentlist rowidlist rowisopt rowfinal
7fcc92bf
PM
4166 global colormap rowtextx nextcolor canvxmax
4167 global numcommits viewcomplete
50b44ece 4168 global selectedline currentid canv canvy0
4fb0fa19 4169 global treediffs
3e76608d 4170 global pending_select mainheadid
0380081c 4171 global commitidx
3e76608d 4172 global selectedview
97645683 4173 global hlview selectedhlview commitinterest
50b44ece
PM
4174
4175 if {$n == $curview} return
4176 set selid {}
7fcc92bf
PM
4177 set ymax [lindex [$canv cget -scrollregion] 3]
4178 set span [$canv yview]
4179 set ytop [expr {[lindex $span 0] * $ymax}]
4180 set ybot [expr {[lindex $span 1] * $ymax}]
4181 set yscreen [expr {($ybot - $ytop) / 2}]
94b4a69f 4182 if {$selectedline ne {}} {
50b44ece
PM
4183 set selid $currentid
4184 set y [yc $selectedline]
50b44ece
PM
4185 if {$ytop < $y && $y < $ybot} {
4186 set yscreen [expr {$y - $ytop}]
50b44ece 4187 }
e507fd48
PM
4188 } elseif {[info exists pending_select]} {
4189 set selid $pending_select
4190 unset pending_select
50b44ece
PM
4191 }
4192 unselectline
fdedbcfb 4193 normalline
50b44ece
PM
4194 catch {unset treediffs}
4195 clear_display
908c3585
PM
4196 if {[info exists hlview] && $hlview == $n} {
4197 unset hlview
b007ee20 4198 set selectedhlview [mc "None"]
908c3585 4199 }
97645683 4200 catch {unset commitinterest}
7fcc92bf 4201 catch {unset cached_commitrow}
9257d8f7 4202 catch {unset ordertok}
50b44ece
PM
4203
4204 set curview $n
a90a6d24 4205 set selectedview $n
f2d0bbbd
PM
4206 .bar.view entryconf [mca "Edit view..."] -state [expr {$n == 0? "disabled": "normal"}]
4207 .bar.view entryconf [mca "Delete view"] -state [expr {$n == 0? "disabled": "normal"}]
50b44ece 4208
df904497 4209 run refill_reflist
7fcc92bf 4210 if {![info exists viewcomplete($n)]} {
567c34e0 4211 getcommits $selid
50b44ece
PM
4212 return
4213 }
4214
7fcc92bf
PM
4215 set displayorder {}
4216 set parentlist {}
4217 set rowidlist {}
4218 set rowisopt {}
4219 set rowfinal {}
f5f3c2e2 4220 set numcommits $commitidx($n)
22626ef4 4221
50b44ece
PM
4222 catch {unset colormap}
4223 catch {unset rowtextx}
da7c24dd
PM
4224 set nextcolor 0
4225 set canvxmax [$canv cget -width]
50b44ece
PM
4226 set curview $n
4227 set row 0
50b44ece
PM
4228 setcanvscroll
4229 set yf 0
e507fd48 4230 set row {}
7fcc92bf
PM
4231 if {$selid ne {} && [commitinview $selid $n]} {
4232 set row [rowofcommit $selid]
50b44ece
PM
4233 # try to get the selected row in the same position on the screen
4234 set ymax [lindex [$canv cget -scrollregion] 3]
4235 set ytop [expr {[yc $row] - $yscreen}]
4236 if {$ytop < 0} {
4237 set ytop 0
4238 }
4239 set yf [expr {$ytop * 1.0 / $ymax}]
4240 }
4241 allcanvs yview moveto $yf
4242 drawvisible
e507fd48
PM
4243 if {$row ne {}} {
4244 selectline $row 0
3e76608d 4245 } elseif {!$viewcomplete($n)} {
567c34e0 4246 reset_pending_select $selid
e507fd48 4247 } else {
835e62ae
AG
4248 reset_pending_select {}
4249
4250 if {[commitinview $pending_select $curview]} {
4251 selectline [rowofcommit $pending_select] 1
4252 } else {
4253 set row [first_real_row]
4254 if {$row < $numcommits} {
4255 selectline $row 0
4256 }
e507fd48
PM
4257 }
4258 }
7fcc92bf
PM
4259 if {!$viewcomplete($n)} {
4260 if {$numcommits == 0} {
d990cedf 4261 show_status [mc "Reading commits..."]
d16c0812 4262 }
098dd8a3 4263 } elseif {$numcommits == 0} {
d990cedf 4264 show_status [mc "No commits selected"]
2516dae2 4265 }
50b44ece
PM
4266}
4267
908c3585
PM
4268# Stuff relating to the highlighting facility
4269
476ca63d 4270proc ishighlighted {id} {
164ff275 4271 global vhighlights fhighlights nhighlights rhighlights
908c3585 4272
476ca63d
PM
4273 if {[info exists nhighlights($id)] && $nhighlights($id) > 0} {
4274 return $nhighlights($id)
908c3585 4275 }
476ca63d
PM
4276 if {[info exists vhighlights($id)] && $vhighlights($id) > 0} {
4277 return $vhighlights($id)
908c3585 4278 }
476ca63d
PM
4279 if {[info exists fhighlights($id)] && $fhighlights($id) > 0} {
4280 return $fhighlights($id)
908c3585 4281 }
476ca63d
PM
4282 if {[info exists rhighlights($id)] && $rhighlights($id) > 0} {
4283 return $rhighlights($id)
164ff275 4284 }
908c3585
PM
4285 return 0
4286}
4287
28593d3f 4288proc bolden {id font} {
b9fdba7f 4289 global canv linehtag currentid boldids need_redisplay markedid
908c3585 4290
d98d50e2
PM
4291 # need_redisplay = 1 means the display is stale and about to be redrawn
4292 if {$need_redisplay} return
28593d3f
PM
4293 lappend boldids $id
4294 $canv itemconf $linehtag($id) -font $font
4295 if {[info exists currentid] && $id eq $currentid} {
908c3585 4296 $canv delete secsel
28593d3f 4297 set t [eval $canv create rect [$canv bbox $linehtag($id)] \
908c3585
PM
4298 -outline {{}} -tags secsel \
4299 -fill [$canv cget -selectbackground]]
4300 $canv lower $t
4301 }
b9fdba7f
PM
4302 if {[info exists markedid] && $id eq $markedid} {
4303 make_idmark $id
4304 }
908c3585
PM
4305}
4306
28593d3f
PM
4307proc bolden_name {id font} {
4308 global canv2 linentag currentid boldnameids need_redisplay
908c3585 4309
d98d50e2 4310 if {$need_redisplay} return
28593d3f
PM
4311 lappend boldnameids $id
4312 $canv2 itemconf $linentag($id) -font $font
4313 if {[info exists currentid] && $id eq $currentid} {
908c3585 4314 $canv2 delete secsel
28593d3f 4315 set t [eval $canv2 create rect [$canv2 bbox $linentag($id)] \
908c3585
PM
4316 -outline {{}} -tags secsel \
4317 -fill [$canv2 cget -selectbackground]]
4318 $canv2 lower $t
4319 }
4320}
4321
4e7d6779 4322proc unbolden {} {
28593d3f 4323 global boldids
908c3585 4324
4e7d6779 4325 set stillbold {}
28593d3f
PM
4326 foreach id $boldids {
4327 if {![ishighlighted $id]} {
4328 bolden $id mainfont
4e7d6779 4329 } else {
28593d3f 4330 lappend stillbold $id
908c3585
PM
4331 }
4332 }
28593d3f 4333 set boldids $stillbold
908c3585
PM
4334}
4335
4336proc addvhighlight {n} {
476ca63d 4337 global hlview viewcomplete curview vhl_done commitidx
da7c24dd
PM
4338
4339 if {[info exists hlview]} {
908c3585 4340 delvhighlight
da7c24dd
PM
4341 }
4342 set hlview $n
7fcc92bf 4343 if {$n != $curview && ![info exists viewcomplete($n)]} {
da7c24dd 4344 start_rev_list $n
908c3585
PM
4345 }
4346 set vhl_done $commitidx($hlview)
4347 if {$vhl_done > 0} {
4348 drawvisible
da7c24dd
PM
4349 }
4350}
4351
908c3585
PM
4352proc delvhighlight {} {
4353 global hlview vhighlights
da7c24dd
PM
4354
4355 if {![info exists hlview]} return
4356 unset hlview
4e7d6779
PM
4357 catch {unset vhighlights}
4358 unbolden
da7c24dd
PM
4359}
4360
908c3585 4361proc vhighlightmore {} {
7fcc92bf 4362 global hlview vhl_done commitidx vhighlights curview
da7c24dd 4363
da7c24dd 4364 set max $commitidx($hlview)
908c3585
PM
4365 set vr [visiblerows]
4366 set r0 [lindex $vr 0]
4367 set r1 [lindex $vr 1]
4368 for {set i $vhl_done} {$i < $max} {incr i} {
7fcc92bf
PM
4369 set id [commitonrow $i $hlview]
4370 if {[commitinview $id $curview]} {
4371 set row [rowofcommit $id]
908c3585
PM
4372 if {$r0 <= $row && $row <= $r1} {
4373 if {![highlighted $row]} {
28593d3f 4374 bolden $id mainfontbold
da7c24dd 4375 }
476ca63d 4376 set vhighlights($id) 1
da7c24dd
PM
4377 }
4378 }
4379 }
908c3585 4380 set vhl_done $max
ac1276ab 4381 return 0
908c3585
PM
4382}
4383
4384proc askvhighlight {row id} {
7fcc92bf 4385 global hlview vhighlights iddrawn
908c3585 4386
7fcc92bf 4387 if {[commitinview $id $hlview]} {
476ca63d 4388 if {[info exists iddrawn($id)] && ![ishighlighted $id]} {
28593d3f 4389 bolden $id mainfontbold
908c3585 4390 }
476ca63d 4391 set vhighlights($id) 1
908c3585 4392 } else {
476ca63d 4393 set vhighlights($id) 0
908c3585
PM
4394 }
4395}
4396
687c8765 4397proc hfiles_change {} {
908c3585 4398 global highlight_files filehighlight fhighlights fh_serial
8b39e04f 4399 global highlight_paths
908c3585
PM
4400
4401 if {[info exists filehighlight]} {
4402 # delete previous highlights
4403 catch {close $filehighlight}
4404 unset filehighlight
4e7d6779
PM
4405 catch {unset fhighlights}
4406 unbolden
63b79191 4407 unhighlight_filelist
908c3585 4408 }
63b79191 4409 set highlight_paths {}
908c3585
PM
4410 after cancel do_file_hl $fh_serial
4411 incr fh_serial
4412 if {$highlight_files ne {}} {
4413 after 300 do_file_hl $fh_serial
4414 }
4415}
4416
687c8765
PM
4417proc gdttype_change {name ix op} {
4418 global gdttype highlight_files findstring findpattern
4419
bb3edc8b 4420 stopfinding
687c8765 4421 if {$findstring ne {}} {
b007ee20 4422 if {$gdttype eq [mc "containing:"]} {
687c8765
PM
4423 if {$highlight_files ne {}} {
4424 set highlight_files {}
4425 hfiles_change
4426 }
4427 findcom_change
4428 } else {
4429 if {$findpattern ne {}} {
4430 set findpattern {}
4431 findcom_change
4432 }
4433 set highlight_files $findstring
4434 hfiles_change
4435 }
4436 drawvisible
4437 }
4438 # enable/disable findtype/findloc menus too
4439}
4440
4441proc find_change {name ix op} {
4442 global gdttype findstring highlight_files
4443
bb3edc8b 4444 stopfinding
b007ee20 4445 if {$gdttype eq [mc "containing:"]} {
687c8765
PM
4446 findcom_change
4447 } else {
4448 if {$highlight_files ne $findstring} {
4449 set highlight_files $findstring
4450 hfiles_change
4451 }
4452 }
4453 drawvisible
4454}
4455
64b5f146 4456proc findcom_change args {
28593d3f 4457 global nhighlights boldnameids
687c8765
PM
4458 global findpattern findtype findstring gdttype
4459
bb3edc8b 4460 stopfinding
687c8765 4461 # delete previous highlights, if any
28593d3f
PM
4462 foreach id $boldnameids {
4463 bolden_name $id mainfont
687c8765 4464 }
28593d3f 4465 set boldnameids {}
687c8765
PM
4466 catch {unset nhighlights}
4467 unbolden
4468 unmarkmatches
b007ee20 4469 if {$gdttype ne [mc "containing:"] || $findstring eq {}} {
687c8765 4470 set findpattern {}
b007ee20 4471 } elseif {$findtype eq [mc "Regexp"]} {
687c8765
PM
4472 set findpattern $findstring
4473 } else {
4474 set e [string map {"*" "\\*" "?" "\\?" "\[" "\\\[" "\\" "\\\\"} \
4475 $findstring]
4476 set findpattern "*$e*"
4477 }
4478}
4479
63b79191
PM
4480proc makepatterns {l} {
4481 set ret {}
4482 foreach e $l {
4483 set ee [string map {"*" "\\*" "?" "\\?" "\[" "\\\[" "\\" "\\\\"} $e]
4484 if {[string index $ee end] eq "/"} {
4485 lappend ret "$ee*"
4486 } else {
4487 lappend ret $ee
4488 lappend ret "$ee/*"
4489 }
4490 }
4491 return $ret
4492}
4493
908c3585 4494proc do_file_hl {serial} {
4e7d6779 4495 global highlight_files filehighlight highlight_paths gdttype fhl_list
908c3585 4496
b007ee20 4497 if {$gdttype eq [mc "touching paths:"]} {
60f7a7dc
PM
4498 if {[catch {set paths [shellsplit $highlight_files]}]} return
4499 set highlight_paths [makepatterns $paths]
4500 highlight_filelist
4501 set gdtargs [concat -- $paths]
b007ee20 4502 } elseif {$gdttype eq [mc "adding/removing string:"]} {
60f7a7dc 4503 set gdtargs [list "-S$highlight_files"]
687c8765
PM
4504 } else {
4505 # must be "containing:", i.e. we're searching commit info
4506 return
60f7a7dc 4507 }
1ce09dd6 4508 set cmd [concat | git diff-tree -r -s --stdin $gdtargs]
908c3585
PM
4509 set filehighlight [open $cmd r+]
4510 fconfigure $filehighlight -blocking 0
7eb3cb9c 4511 filerun $filehighlight readfhighlight
4e7d6779 4512 set fhl_list {}
908c3585
PM
4513 drawvisible
4514 flushhighlights
4515}
4516
4517proc flushhighlights {} {
4e7d6779 4518 global filehighlight fhl_list
908c3585
PM
4519
4520 if {[info exists filehighlight]} {
4e7d6779 4521 lappend fhl_list {}
908c3585
PM
4522 puts $filehighlight ""
4523 flush $filehighlight
4524 }
4525}
4526
4527proc askfilehighlight {row id} {
4e7d6779 4528 global filehighlight fhighlights fhl_list
908c3585 4529
4e7d6779 4530 lappend fhl_list $id
476ca63d 4531 set fhighlights($id) -1
908c3585
PM
4532 puts $filehighlight $id
4533}
4534
4535proc readfhighlight {} {
7fcc92bf 4536 global filehighlight fhighlights curview iddrawn
687c8765 4537 global fhl_list find_dirn
4e7d6779 4538
7eb3cb9c
PM
4539 if {![info exists filehighlight]} {
4540 return 0
4541 }
4542 set nr 0
4543 while {[incr nr] <= 100 && [gets $filehighlight line] >= 0} {
4e7d6779
PM
4544 set line [string trim $line]
4545 set i [lsearch -exact $fhl_list $line]
4546 if {$i < 0} continue
4547 for {set j 0} {$j < $i} {incr j} {
4548 set id [lindex $fhl_list $j]
476ca63d 4549 set fhighlights($id) 0
908c3585 4550 }
4e7d6779
PM
4551 set fhl_list [lrange $fhl_list [expr {$i+1}] end]
4552 if {$line eq {}} continue
7fcc92bf 4553 if {![commitinview $line $curview]} continue
476ca63d 4554 if {[info exists iddrawn($line)] && ![ishighlighted $line]} {
28593d3f 4555 bolden $line mainfontbold
4e7d6779 4556 }
476ca63d 4557 set fhighlights($line) 1
908c3585 4558 }
4e7d6779
PM
4559 if {[eof $filehighlight]} {
4560 # strange...
1ce09dd6 4561 puts "oops, git diff-tree died"
4e7d6779
PM
4562 catch {close $filehighlight}
4563 unset filehighlight
7eb3cb9c 4564 return 0
908c3585 4565 }
687c8765 4566 if {[info exists find_dirn]} {
cca5d946 4567 run findmore
908c3585 4568 }
687c8765 4569 return 1
908c3585
PM
4570}
4571
4fb0fa19 4572proc doesmatch {f} {
687c8765 4573 global findtype findpattern
4fb0fa19 4574
b007ee20 4575 if {$findtype eq [mc "Regexp"]} {
687c8765 4576 return [regexp $findpattern $f]
b007ee20 4577 } elseif {$findtype eq [mc "IgnCase"]} {
4fb0fa19
PM
4578 return [string match -nocase $findpattern $f]
4579 } else {
4580 return [string match $findpattern $f]
4581 }
4582}
4583
60f7a7dc 4584proc askfindhighlight {row id} {
9c311b32 4585 global nhighlights commitinfo iddrawn
4fb0fa19
PM
4586 global findloc
4587 global markingmatches
908c3585
PM
4588
4589 if {![info exists commitinfo($id)]} {
4590 getcommit $id
4591 }
60f7a7dc 4592 set info $commitinfo($id)
908c3585 4593 set isbold 0
b007ee20 4594 set fldtypes [list [mc Headline] [mc Author] [mc Date] [mc Committer] [mc CDate] [mc Comments]]
60f7a7dc 4595 foreach f $info ty $fldtypes {
b007ee20 4596 if {($findloc eq [mc "All fields"] || $findloc eq $ty) &&
4fb0fa19 4597 [doesmatch $f]} {
b007ee20 4598 if {$ty eq [mc "Author"]} {
60f7a7dc 4599 set isbold 2
4fb0fa19 4600 break
60f7a7dc 4601 }
4fb0fa19 4602 set isbold 1
908c3585
PM
4603 }
4604 }
4fb0fa19 4605 if {$isbold && [info exists iddrawn($id)]} {
476ca63d 4606 if {![ishighlighted $id]} {
28593d3f 4607 bolden $id mainfontbold
4fb0fa19 4608 if {$isbold > 1} {
28593d3f 4609 bolden_name $id mainfontbold
4fb0fa19 4610 }
908c3585 4611 }
4fb0fa19 4612 if {$markingmatches} {
005a2f4e 4613 markrowmatches $row $id
908c3585
PM
4614 }
4615 }
476ca63d 4616 set nhighlights($id) $isbold
da7c24dd
PM
4617}
4618
005a2f4e
PM
4619proc markrowmatches {row id} {
4620 global canv canv2 linehtag linentag commitinfo findloc
4fb0fa19 4621
005a2f4e
PM
4622 set headline [lindex $commitinfo($id) 0]
4623 set author [lindex $commitinfo($id) 1]
4fb0fa19
PM
4624 $canv delete match$row
4625 $canv2 delete match$row
b007ee20 4626 if {$findloc eq [mc "All fields"] || $findloc eq [mc "Headline"]} {
005a2f4e
PM
4627 set m [findmatches $headline]
4628 if {$m ne {}} {
28593d3f
PM
4629 markmatches $canv $row $headline $linehtag($id) $m \
4630 [$canv itemcget $linehtag($id) -font] $row
005a2f4e 4631 }
4fb0fa19 4632 }
b007ee20 4633 if {$findloc eq [mc "All fields"] || $findloc eq [mc "Author"]} {
005a2f4e
PM
4634 set m [findmatches $author]
4635 if {$m ne {}} {
28593d3f
PM
4636 markmatches $canv2 $row $author $linentag($id) $m \
4637 [$canv2 itemcget $linentag($id) -font] $row
005a2f4e 4638 }
4fb0fa19
PM
4639 }
4640}
4641
164ff275
PM
4642proc vrel_change {name ix op} {
4643 global highlight_related
4644
4645 rhighlight_none
b007ee20 4646 if {$highlight_related ne [mc "None"]} {
7eb3cb9c 4647 run drawvisible
164ff275
PM
4648 }
4649}
4650
4651# prepare for testing whether commits are descendents or ancestors of a
4652proc rhighlight_sel {a} {
4653 global descendent desc_todo ancestor anc_todo
476ca63d 4654 global highlight_related
164ff275
PM
4655
4656 catch {unset descendent}
4657 set desc_todo [list $a]
4658 catch {unset ancestor}
4659 set anc_todo [list $a]
b007ee20 4660 if {$highlight_related ne [mc "None"]} {
164ff275 4661 rhighlight_none
7eb3cb9c 4662 run drawvisible
164ff275
PM
4663 }
4664}
4665
4666proc rhighlight_none {} {
4667 global rhighlights
4668
4e7d6779
PM
4669 catch {unset rhighlights}
4670 unbolden
164ff275
PM
4671}
4672
4673proc is_descendent {a} {
7fcc92bf 4674 global curview children descendent desc_todo
164ff275
PM
4675
4676 set v $curview
7fcc92bf 4677 set la [rowofcommit $a]
164ff275
PM
4678 set todo $desc_todo
4679 set leftover {}
4680 set done 0
4681 for {set i 0} {$i < [llength $todo]} {incr i} {
4682 set do [lindex $todo $i]
7fcc92bf 4683 if {[rowofcommit $do] < $la} {
164ff275
PM
4684 lappend leftover $do
4685 continue
4686 }
4687 foreach nk $children($v,$do) {
4688 if {![info exists descendent($nk)]} {
4689 set descendent($nk) 1
4690 lappend todo $nk
4691 if {$nk eq $a} {
4692 set done 1
4693 }
4694 }
4695 }
4696 if {$done} {
4697 set desc_todo [concat $leftover [lrange $todo [expr {$i+1}] end]]
4698 return
4699 }
4700 }
4701 set descendent($a) 0
4702 set desc_todo $leftover
4703}
4704
4705proc is_ancestor {a} {
7fcc92bf 4706 global curview parents ancestor anc_todo
164ff275
PM
4707
4708 set v $curview
7fcc92bf 4709 set la [rowofcommit $a]
164ff275
PM
4710 set todo $anc_todo
4711 set leftover {}
4712 set done 0
4713 for {set i 0} {$i < [llength $todo]} {incr i} {
4714 set do [lindex $todo $i]
7fcc92bf 4715 if {![commitinview $do $v] || [rowofcommit $do] > $la} {
164ff275
PM
4716 lappend leftover $do
4717 continue
4718 }
7fcc92bf 4719 foreach np $parents($v,$do) {
164ff275
PM
4720 if {![info exists ancestor($np)]} {
4721 set ancestor($np) 1
4722 lappend todo $np
4723 if {$np eq $a} {
4724 set done 1
4725 }
4726 }
4727 }
4728 if {$done} {
4729 set anc_todo [concat $leftover [lrange $todo [expr {$i+1}] end]]
4730 return
4731 }
4732 }
4733 set ancestor($a) 0
4734 set anc_todo $leftover
4735}
4736
4737proc askrelhighlight {row id} {
9c311b32 4738 global descendent highlight_related iddrawn rhighlights
164ff275
PM
4739 global selectedline ancestor
4740
94b4a69f 4741 if {$selectedline eq {}} return
164ff275 4742 set isbold 0
55e34436
CS
4743 if {$highlight_related eq [mc "Descendant"] ||
4744 $highlight_related eq [mc "Not descendant"]} {
164ff275
PM
4745 if {![info exists descendent($id)]} {
4746 is_descendent $id
4747 }
55e34436 4748 if {$descendent($id) == ($highlight_related eq [mc "Descendant"])} {
164ff275
PM
4749 set isbold 1
4750 }
b007ee20
CS
4751 } elseif {$highlight_related eq [mc "Ancestor"] ||
4752 $highlight_related eq [mc "Not ancestor"]} {
164ff275
PM
4753 if {![info exists ancestor($id)]} {
4754 is_ancestor $id
4755 }
b007ee20 4756 if {$ancestor($id) == ($highlight_related eq [mc "Ancestor"])} {
164ff275
PM
4757 set isbold 1
4758 }
4759 }
4760 if {[info exists iddrawn($id)]} {
476ca63d 4761 if {$isbold && ![ishighlighted $id]} {
28593d3f 4762 bolden $id mainfontbold
164ff275
PM
4763 }
4764 }
476ca63d 4765 set rhighlights($id) $isbold
164ff275
PM
4766}
4767
da7c24dd
PM
4768# Graph layout functions
4769
9f1afe05
PM
4770proc shortids {ids} {
4771 set res {}
4772 foreach id $ids {
4773 if {[llength $id] > 1} {
4774 lappend res [shortids $id]
4775 } elseif {[regexp {^[0-9a-f]{40}$} $id]} {
4776 lappend res [string range $id 0 7]
4777 } else {
4778 lappend res $id
4779 }
4780 }
4781 return $res
4782}
4783
9f1afe05
PM
4784proc ntimes {n o} {
4785 set ret {}
0380081c
PM
4786 set o [list $o]
4787 for {set mask 1} {$mask <= $n} {incr mask $mask} {
4788 if {($n & $mask) != 0} {
4789 set ret [concat $ret $o]
9f1afe05 4790 }
0380081c 4791 set o [concat $o $o]
9f1afe05 4792 }
0380081c 4793 return $ret
9f1afe05
PM
4794}
4795
9257d8f7
PM
4796proc ordertoken {id} {
4797 global ordertok curview varcid varcstart varctok curview parents children
4798 global nullid nullid2
4799
4800 if {[info exists ordertok($id)]} {
4801 return $ordertok($id)
4802 }
4803 set origid $id
4804 set todo {}
4805 while {1} {
4806 if {[info exists varcid($curview,$id)]} {
4807 set a $varcid($curview,$id)
4808 set p [lindex $varcstart($curview) $a]
4809 } else {
4810 set p [lindex $children($curview,$id) 0]
4811 }
4812 if {[info exists ordertok($p)]} {
4813 set tok $ordertok($p)
4814 break
4815 }
c8c9f3d9
PM
4816 set id [first_real_child $curview,$p]
4817 if {$id eq {}} {
9257d8f7 4818 # it's a root
46308ea1 4819 set tok [lindex $varctok($curview) $varcid($curview,$p)]
9257d8f7
PM
4820 break
4821 }
9257d8f7
PM
4822 if {[llength $parents($curview,$id)] == 1} {
4823 lappend todo [list $p {}]
4824 } else {
4825 set j [lsearch -exact $parents($curview,$id) $p]
4826 if {$j < 0} {
4827 puts "oops didn't find [shortids $p] in parents of [shortids $id]"
4828 }
4829 lappend todo [list $p [strrep $j]]
4830 }
4831 }
4832 for {set i [llength $todo]} {[incr i -1] >= 0} {} {
4833 set p [lindex $todo $i 0]
4834 append tok [lindex $todo $i 1]
4835 set ordertok($p) $tok
4836 }
4837 set ordertok($origid) $tok
4838 return $tok
4839}
4840
6e8c8707
PM
4841# Work out where id should go in idlist so that order-token
4842# values increase from left to right
4843proc idcol {idlist id {i 0}} {
9257d8f7 4844 set t [ordertoken $id]
e5b37ac1
PM
4845 if {$i < 0} {
4846 set i 0
4847 }
9257d8f7 4848 if {$i >= [llength $idlist] || $t < [ordertoken [lindex $idlist $i]]} {
6e8c8707
PM
4849 if {$i > [llength $idlist]} {
4850 set i [llength $idlist]
9f1afe05 4851 }
9257d8f7 4852 while {[incr i -1] >= 0 && $t < [ordertoken [lindex $idlist $i]]} {}
6e8c8707
PM
4853 incr i
4854 } else {
9257d8f7 4855 if {$t > [ordertoken [lindex $idlist $i]]} {
6e8c8707 4856 while {[incr i] < [llength $idlist] &&
9257d8f7 4857 $t >= [ordertoken [lindex $idlist $i]]} {}
9f1afe05 4858 }
9f1afe05 4859 }
6e8c8707 4860 return $i
9f1afe05
PM
4861}
4862
4863proc initlayout {} {
7fcc92bf 4864 global rowidlist rowisopt rowfinal displayorder parentlist
da7c24dd 4865 global numcommits canvxmax canv
8f7d0cec 4866 global nextcolor
da7c24dd 4867 global colormap rowtextx
9f1afe05 4868
8f7d0cec
PM
4869 set numcommits 0
4870 set displayorder {}
79b2c75e 4871 set parentlist {}
8f7d0cec 4872 set nextcolor 0
0380081c
PM
4873 set rowidlist {}
4874 set rowisopt {}
f5f3c2e2 4875 set rowfinal {}
be0cd098 4876 set canvxmax [$canv cget -width]
50b44ece
PM
4877 catch {unset colormap}
4878 catch {unset rowtextx}
ac1276ab 4879 setcanvscroll
be0cd098
PM
4880}
4881
4882proc setcanvscroll {} {
4883 global canv canv2 canv3 numcommits linespc canvxmax canvy0
ac1276ab 4884 global lastscrollset lastscrollrows
be0cd098
PM
4885
4886 set ymax [expr {$canvy0 + ($numcommits - 0.5) * $linespc + 2}]
4887 $canv conf -scrollregion [list 0 0 $canvxmax $ymax]
4888 $canv2 conf -scrollregion [list 0 0 0 $ymax]
4889 $canv3 conf -scrollregion [list 0 0 0 $ymax]
ac1276ab
PM
4890 set lastscrollset [clock clicks -milliseconds]
4891 set lastscrollrows $numcommits
9f1afe05
PM
4892}
4893
4894proc visiblerows {} {
4895 global canv numcommits linespc
4896
4897 set ymax [lindex [$canv cget -scrollregion] 3]
4898 if {$ymax eq {} || $ymax == 0} return
4899 set f [$canv yview]
4900 set y0 [expr {int([lindex $f 0] * $ymax)}]
4901 set r0 [expr {int(($y0 - 3) / $linespc) - 1}]
4902 if {$r0 < 0} {
4903 set r0 0
4904 }
4905 set y1 [expr {int([lindex $f 1] * $ymax)}]
4906 set r1 [expr {int(($y1 - 3) / $linespc) + 1}]
4907 if {$r1 >= $numcommits} {
4908 set r1 [expr {$numcommits - 1}]
4909 }
4910 return [list $r0 $r1]
4911}
4912
f5f3c2e2 4913proc layoutmore {} {
38dfe939 4914 global commitidx viewcomplete curview
94b4a69f 4915 global numcommits pending_select curview
d375ef9b 4916 global lastscrollset lastscrollrows
ac1276ab
PM
4917
4918 if {$lastscrollrows < 100 || $viewcomplete($curview) ||
4919 [clock clicks -milliseconds] - $lastscrollset > 500} {
a2c22362
PM
4920 setcanvscroll
4921 }
d94f8cd6 4922 if {[info exists pending_select] &&
7fcc92bf 4923 [commitinview $pending_select $curview]} {
567c34e0 4924 update
7fcc92bf 4925 selectline [rowofcommit $pending_select] 1
d94f8cd6 4926 }
ac1276ab 4927 drawvisible
219ea3a9
PM
4928}
4929
cdc8429c
PM
4930# With path limiting, we mightn't get the actual HEAD commit,
4931# so ask git rev-list what is the first ancestor of HEAD that
4932# touches a file in the path limit.
4933proc get_viewmainhead {view} {
4934 global viewmainheadid vfilelimit viewinstances mainheadid
4935
4936 catch {
4937 set rfd [open [concat | git rev-list -1 $mainheadid \
4938 -- $vfilelimit($view)] r]
4939 set j [reg_instance $rfd]
4940 lappend viewinstances($view) $j
4941 fconfigure $rfd -blocking 0
4942 filerun $rfd [list getviewhead $rfd $j $view]
4943 set viewmainheadid($curview) {}
4944 }
4945}
4946
4947# git rev-list should give us just 1 line to use as viewmainheadid($view)
4948proc getviewhead {fd inst view} {
4949 global viewmainheadid commfd curview viewinstances showlocalchanges
4950
4951 set id {}
4952 if {[gets $fd line] < 0} {
4953 if {![eof $fd]} {
4954 return 1
4955 }
4956 } elseif {[string length $line] == 40 && [string is xdigit $line]} {
4957 set id $line
4958 }
4959 set viewmainheadid($view) $id
4960 close $fd
4961 unset commfd($inst)
4962 set i [lsearch -exact $viewinstances($view) $inst]
4963 if {$i >= 0} {
4964 set viewinstances($view) [lreplace $viewinstances($view) $i $i]
4965 }
4966 if {$showlocalchanges && $id ne {} && $view == $curview} {
4967 doshowlocalchanges
4968 }
4969 return 0
4970}
4971
219ea3a9 4972proc doshowlocalchanges {} {
cdc8429c 4973 global curview viewmainheadid
219ea3a9 4974
cdc8429c
PM
4975 if {$viewmainheadid($curview) eq {}} return
4976 if {[commitinview $viewmainheadid($curview) $curview]} {
219ea3a9 4977 dodiffindex
38dfe939 4978 } else {
cdc8429c 4979 interestedin $viewmainheadid($curview) dodiffindex
219ea3a9
PM
4980 }
4981}
4982
4983proc dohidelocalchanges {} {
7fcc92bf 4984 global nullid nullid2 lserial curview
219ea3a9 4985
7fcc92bf 4986 if {[commitinview $nullid $curview]} {
b8a938cf 4987 removefakerow $nullid
8f489363 4988 }
7fcc92bf 4989 if {[commitinview $nullid2 $curview]} {
b8a938cf 4990 removefakerow $nullid2
219ea3a9
PM
4991 }
4992 incr lserial
4993}
4994
8f489363 4995# spawn off a process to do git diff-index --cached HEAD
219ea3a9 4996proc dodiffindex {} {
cdc8429c 4997 global lserial showlocalchanges vfilelimit curview
cb8329aa 4998 global isworktree
219ea3a9 4999
cb8329aa 5000 if {!$showlocalchanges || !$isworktree} return
219ea3a9 5001 incr lserial
cdc8429c
PM
5002 set cmd "|git diff-index --cached HEAD"
5003 if {$vfilelimit($curview) ne {}} {
5004 set cmd [concat $cmd -- $vfilelimit($curview)]
5005 }
5006 set fd [open $cmd r]
219ea3a9 5007 fconfigure $fd -blocking 0
e439e092
AG
5008 set i [reg_instance $fd]
5009 filerun $fd [list readdiffindex $fd $lserial $i]
219ea3a9
PM
5010}
5011
e439e092 5012proc readdiffindex {fd serial inst} {
cdc8429c
PM
5013 global viewmainheadid nullid nullid2 curview commitinfo commitdata lserial
5014 global vfilelimit
219ea3a9 5015
8f489363 5016 set isdiff 1
219ea3a9 5017 if {[gets $fd line] < 0} {
8f489363
PM
5018 if {![eof $fd]} {
5019 return 1
219ea3a9 5020 }
8f489363 5021 set isdiff 0
219ea3a9
PM
5022 }
5023 # we only need to see one line and we don't really care what it says...
e439e092 5024 stop_instance $inst
219ea3a9 5025
24f7a667
PM
5026 if {$serial != $lserial} {
5027 return 0
8f489363
PM
5028 }
5029
24f7a667 5030 # now see if there are any local changes not checked in to the index
cdc8429c
PM
5031 set cmd "|git diff-files"
5032 if {$vfilelimit($curview) ne {}} {
5033 set cmd [concat $cmd -- $vfilelimit($curview)]
5034 }
5035 set fd [open $cmd r]
24f7a667 5036 fconfigure $fd -blocking 0
e439e092
AG
5037 set i [reg_instance $fd]
5038 filerun $fd [list readdifffiles $fd $serial $i]
24f7a667
PM
5039
5040 if {$isdiff && ![commitinview $nullid2 $curview]} {
8f489363 5041 # add the line for the changes in the index to the graph
d990cedf 5042 set hl [mc "Local changes checked in to index but not committed"]
8f489363
PM
5043 set commitinfo($nullid2) [list $hl {} {} {} {} " $hl\n"]
5044 set commitdata($nullid2) "\n $hl\n"
fc2a256f 5045 if {[commitinview $nullid $curview]} {
b8a938cf 5046 removefakerow $nullid
fc2a256f 5047 }
cdc8429c 5048 insertfakerow $nullid2 $viewmainheadid($curview)
24f7a667 5049 } elseif {!$isdiff && [commitinview $nullid2 $curview]} {
cdc8429c
PM
5050 if {[commitinview $nullid $curview]} {
5051 removefakerow $nullid
5052 }
b8a938cf 5053 removefakerow $nullid2
8f489363
PM
5054 }
5055 return 0
5056}
5057
e439e092 5058proc readdifffiles {fd serial inst} {
cdc8429c 5059 global viewmainheadid nullid nullid2 curview
8f489363
PM
5060 global commitinfo commitdata lserial
5061
5062 set isdiff 1
5063 if {[gets $fd line] < 0} {
5064 if {![eof $fd]} {
5065 return 1
5066 }
5067 set isdiff 0
5068 }
5069 # we only need to see one line and we don't really care what it says...
e439e092 5070 stop_instance $inst
8f489363 5071
24f7a667
PM
5072 if {$serial != $lserial} {
5073 return 0
5074 }
5075
5076 if {$isdiff && ![commitinview $nullid $curview]} {
219ea3a9 5077 # add the line for the local diff to the graph
d990cedf 5078 set hl [mc "Local uncommitted changes, not checked in to index"]
219ea3a9
PM
5079 set commitinfo($nullid) [list $hl {} {} {} {} " $hl\n"]
5080 set commitdata($nullid) "\n $hl\n"
7fcc92bf
PM
5081 if {[commitinview $nullid2 $curview]} {
5082 set p $nullid2
5083 } else {
cdc8429c 5084 set p $viewmainheadid($curview)
7fcc92bf 5085 }
b8a938cf 5086 insertfakerow $nullid $p
24f7a667 5087 } elseif {!$isdiff && [commitinview $nullid $curview]} {
b8a938cf 5088 removefakerow $nullid
219ea3a9
PM
5089 }
5090 return 0
9f1afe05
PM
5091}
5092
8f0bc7e9 5093proc nextuse {id row} {
7fcc92bf 5094 global curview children
9f1afe05 5095
8f0bc7e9
PM
5096 if {[info exists children($curview,$id)]} {
5097 foreach kid $children($curview,$id) {
7fcc92bf 5098 if {![commitinview $kid $curview]} {
0380081c
PM
5099 return -1
5100 }
7fcc92bf
PM
5101 if {[rowofcommit $kid] > $row} {
5102 return [rowofcommit $kid]
9f1afe05 5103 }
9f1afe05 5104 }
8f0bc7e9 5105 }
7fcc92bf
PM
5106 if {[commitinview $id $curview]} {
5107 return [rowofcommit $id]
8f0bc7e9
PM
5108 }
5109 return -1
5110}
5111
f5f3c2e2 5112proc prevuse {id row} {
7fcc92bf 5113 global curview children
f5f3c2e2
PM
5114
5115 set ret -1
5116 if {[info exists children($curview,$id)]} {
5117 foreach kid $children($curview,$id) {
7fcc92bf
PM
5118 if {![commitinview $kid $curview]} break
5119 if {[rowofcommit $kid] < $row} {
5120 set ret [rowofcommit $kid]
7b459a1c 5121 }
7b459a1c 5122 }
f5f3c2e2
PM
5123 }
5124 return $ret
5125}
5126
0380081c
PM
5127proc make_idlist {row} {
5128 global displayorder parentlist uparrowlen downarrowlen mingaplen
9257d8f7 5129 global commitidx curview children
9f1afe05 5130
0380081c
PM
5131 set r [expr {$row - $mingaplen - $downarrowlen - 1}]
5132 if {$r < 0} {
5133 set r 0
8f0bc7e9 5134 }
0380081c
PM
5135 set ra [expr {$row - $downarrowlen}]
5136 if {$ra < 0} {
5137 set ra 0
5138 }
5139 set rb [expr {$row + $uparrowlen}]
5140 if {$rb > $commitidx($curview)} {
5141 set rb $commitidx($curview)
5142 }
7fcc92bf 5143 make_disporder $r [expr {$rb + 1}]
0380081c
PM
5144 set ids {}
5145 for {} {$r < $ra} {incr r} {
5146 set nextid [lindex $displayorder [expr {$r + 1}]]
5147 foreach p [lindex $parentlist $r] {
5148 if {$p eq $nextid} continue
5149 set rn [nextuse $p $r]
5150 if {$rn >= $row &&
5151 $rn <= $r + $downarrowlen + $mingaplen + $uparrowlen} {
9257d8f7 5152 lappend ids [list [ordertoken $p] $p]
9f1afe05 5153 }
9f1afe05 5154 }
0380081c
PM
5155 }
5156 for {} {$r < $row} {incr r} {
5157 set nextid [lindex $displayorder [expr {$r + 1}]]
5158 foreach p [lindex $parentlist $r] {
5159 if {$p eq $nextid} continue
5160 set rn [nextuse $p $r]
5161 if {$rn < 0 || $rn >= $row} {
9257d8f7 5162 lappend ids [list [ordertoken $p] $p]
9f1afe05 5163 }
9f1afe05 5164 }
0380081c
PM
5165 }
5166 set id [lindex $displayorder $row]
9257d8f7 5167 lappend ids [list [ordertoken $id] $id]
0380081c
PM
5168 while {$r < $rb} {
5169 foreach p [lindex $parentlist $r] {
5170 set firstkid [lindex $children($curview,$p) 0]
7fcc92bf 5171 if {[rowofcommit $firstkid] < $row} {
9257d8f7 5172 lappend ids [list [ordertoken $p] $p]
9f1afe05 5173 }
9f1afe05 5174 }
0380081c
PM
5175 incr r
5176 set id [lindex $displayorder $r]
5177 if {$id ne {}} {
5178 set firstkid [lindex $children($curview,$id) 0]
7fcc92bf 5179 if {$firstkid ne {} && [rowofcommit $firstkid] < $row} {
9257d8f7 5180 lappend ids [list [ordertoken $id] $id]
0380081c 5181 }
9f1afe05 5182 }
9f1afe05 5183 }
0380081c
PM
5184 set idlist {}
5185 foreach idx [lsort -unique $ids] {
5186 lappend idlist [lindex $idx 1]
5187 }
5188 return $idlist
9f1afe05
PM
5189}
5190
f5f3c2e2
PM
5191proc rowsequal {a b} {
5192 while {[set i [lsearch -exact $a {}]] >= 0} {
5193 set a [lreplace $a $i $i]
5194 }
5195 while {[set i [lsearch -exact $b {}]] >= 0} {
5196 set b [lreplace $b $i $i]
5197 }
5198 return [expr {$a eq $b}]
9f1afe05
PM
5199}
5200
f5f3c2e2
PM
5201proc makeupline {id row rend col} {
5202 global rowidlist uparrowlen downarrowlen mingaplen
9f1afe05 5203
f5f3c2e2
PM
5204 for {set r $rend} {1} {set r $rstart} {
5205 set rstart [prevuse $id $r]
5206 if {$rstart < 0} return
5207 if {$rstart < $row} break
5208 }
5209 if {$rstart + $uparrowlen + $mingaplen + $downarrowlen < $rend} {
5210 set rstart [expr {$rend - $uparrowlen - 1}]
79b2c75e 5211 }
f5f3c2e2
PM
5212 for {set r $rstart} {[incr r] <= $row} {} {
5213 set idlist [lindex $rowidlist $r]
5214 if {$idlist ne {} && [lsearch -exact $idlist $id] < 0} {
5215 set col [idcol $idlist $id $col]
5216 lset rowidlist $r [linsert $idlist $col $id]
5217 changedrow $r
5218 }
9f1afe05
PM
5219 }
5220}
5221
0380081c 5222proc layoutrows {row endrow} {
f5f3c2e2 5223 global rowidlist rowisopt rowfinal displayorder
0380081c
PM
5224 global uparrowlen downarrowlen maxwidth mingaplen
5225 global children parentlist
7fcc92bf 5226 global commitidx viewcomplete curview
9f1afe05 5227
7fcc92bf 5228 make_disporder [expr {$row - 1}] [expr {$endrow + $uparrowlen}]
0380081c
PM
5229 set idlist {}
5230 if {$row > 0} {
f56782ae
PM
5231 set rm1 [expr {$row - 1}]
5232 foreach id [lindex $rowidlist $rm1] {
0380081c
PM
5233 if {$id ne {}} {
5234 lappend idlist $id
5235 }
5236 }
f56782ae 5237 set final [lindex $rowfinal $rm1]
79b2c75e 5238 }
0380081c
PM
5239 for {} {$row < $endrow} {incr row} {
5240 set rm1 [expr {$row - 1}]
f56782ae 5241 if {$rm1 < 0 || $idlist eq {}} {
0380081c 5242 set idlist [make_idlist $row]
f5f3c2e2 5243 set final 1
0380081c
PM
5244 } else {
5245 set id [lindex $displayorder $rm1]
5246 set col [lsearch -exact $idlist $id]
5247 set idlist [lreplace $idlist $col $col]
5248 foreach p [lindex $parentlist $rm1] {
5249 if {[lsearch -exact $idlist $p] < 0} {
5250 set col [idcol $idlist $p $col]
5251 set idlist [linsert $idlist $col $p]
f5f3c2e2
PM
5252 # if not the first child, we have to insert a line going up
5253 if {$id ne [lindex $children($curview,$p) 0]} {
5254 makeupline $p $rm1 $row $col
5255 }
0380081c
PM
5256 }
5257 }
5258 set id [lindex $displayorder $row]
5259 if {$row > $downarrowlen} {
5260 set termrow [expr {$row - $downarrowlen - 1}]
5261 foreach p [lindex $parentlist $termrow] {
5262 set i [lsearch -exact $idlist $p]
5263 if {$i < 0} continue
5264 set nr [nextuse $p $termrow]
5265 if {$nr < 0 || $nr >= $row + $mingaplen + $uparrowlen} {
5266 set idlist [lreplace $idlist $i $i]
5267 }
5268 }
5269 }
5270 set col [lsearch -exact $idlist $id]
5271 if {$col < 0} {
5272 set col [idcol $idlist $id]
5273 set idlist [linsert $idlist $col $id]
f5f3c2e2
PM
5274 if {$children($curview,$id) ne {}} {
5275 makeupline $id $rm1 $row $col
5276 }
0380081c
PM
5277 }
5278 set r [expr {$row + $uparrowlen - 1}]
5279 if {$r < $commitidx($curview)} {
5280 set x $col
5281 foreach p [lindex $parentlist $r] {
5282 if {[lsearch -exact $idlist $p] >= 0} continue
5283 set fk [lindex $children($curview,$p) 0]
7fcc92bf 5284 if {[rowofcommit $fk] < $row} {
0380081c
PM
5285 set x [idcol $idlist $p $x]
5286 set idlist [linsert $idlist $x $p]
5287 }
5288 }
5289 if {[incr r] < $commitidx($curview)} {
5290 set p [lindex $displayorder $r]
5291 if {[lsearch -exact $idlist $p] < 0} {
5292 set fk [lindex $children($curview,$p) 0]
7fcc92bf 5293 if {$fk ne {} && [rowofcommit $fk] < $row} {
0380081c
PM
5294 set x [idcol $idlist $p $x]
5295 set idlist [linsert $idlist $x $p]
5296 }
5297 }
5298 }
5299 }
5300 }
f5f3c2e2
PM
5301 if {$final && !$viewcomplete($curview) &&
5302 $row + $uparrowlen + $mingaplen + $downarrowlen
5303 >= $commitidx($curview)} {
5304 set final 0
5305 }
0380081c
PM
5306 set l [llength $rowidlist]
5307 if {$row == $l} {
5308 lappend rowidlist $idlist
5309 lappend rowisopt 0
f5f3c2e2 5310 lappend rowfinal $final
0380081c 5311 } elseif {$row < $l} {
f5f3c2e2 5312 if {![rowsequal $idlist [lindex $rowidlist $row]]} {
0380081c
PM
5313 lset rowidlist $row $idlist
5314 changedrow $row
5315 }
f56782ae 5316 lset rowfinal $row $final
0380081c 5317 } else {
f5f3c2e2
PM
5318 set pad [ntimes [expr {$row - $l}] {}]
5319 set rowidlist [concat $rowidlist $pad]
0380081c 5320 lappend rowidlist $idlist
f5f3c2e2
PM
5321 set rowfinal [concat $rowfinal $pad]
5322 lappend rowfinal $final
0380081c
PM
5323 set rowisopt [concat $rowisopt [ntimes [expr {$row - $l + 1}] 0]]
5324 }
9f1afe05 5325 }
0380081c 5326 return $row
9f1afe05
PM
5327}
5328
0380081c
PM
5329proc changedrow {row} {
5330 global displayorder iddrawn rowisopt need_redisplay
9f1afe05 5331
0380081c
PM
5332 set l [llength $rowisopt]
5333 if {$row < $l} {
5334 lset rowisopt $row 0
5335 if {$row + 1 < $l} {
5336 lset rowisopt [expr {$row + 1}] 0
5337 if {$row + 2 < $l} {
5338 lset rowisopt [expr {$row + 2}] 0
5339 }
5340 }
5341 }
5342 set id [lindex $displayorder $row]
5343 if {[info exists iddrawn($id)]} {
5344 set need_redisplay 1
9f1afe05
PM
5345 }
5346}
5347
5348proc insert_pad {row col npad} {
6e8c8707 5349 global rowidlist
9f1afe05
PM
5350
5351 set pad [ntimes $npad {}]
e341c06d
PM
5352 set idlist [lindex $rowidlist $row]
5353 set bef [lrange $idlist 0 [expr {$col - 1}]]
5354 set aft [lrange $idlist $col end]
5355 set i [lsearch -exact $aft {}]
5356 if {$i > 0} {
5357 set aft [lreplace $aft $i $i]
5358 }
5359 lset rowidlist $row [concat $bef $pad $aft]
0380081c 5360 changedrow $row
9f1afe05
PM
5361}
5362
5363proc optimize_rows {row col endrow} {
0380081c 5364 global rowidlist rowisopt displayorder curview children
9f1afe05 5365
6e8c8707
PM
5366 if {$row < 1} {
5367 set row 1
5368 }
0380081c
PM
5369 for {} {$row < $endrow} {incr row; set col 0} {
5370 if {[lindex $rowisopt $row]} continue
9f1afe05 5371 set haspad 0
6e8c8707
PM
5372 set y0 [expr {$row - 1}]
5373 set ym [expr {$row - 2}]
0380081c
PM
5374 set idlist [lindex $rowidlist $row]
5375 set previdlist [lindex $rowidlist $y0]
5376 if {$idlist eq {} || $previdlist eq {}} continue
5377 if {$ym >= 0} {
5378 set pprevidlist [lindex $rowidlist $ym]
5379 if {$pprevidlist eq {}} continue
5380 } else {
5381 set pprevidlist {}
5382 }
6e8c8707
PM
5383 set x0 -1
5384 set xm -1
5385 for {} {$col < [llength $idlist]} {incr col} {
5386 set id [lindex $idlist $col]
5387 if {[lindex $previdlist $col] eq $id} continue
5388 if {$id eq {}} {
9f1afe05
PM
5389 set haspad 1
5390 continue
5391 }
6e8c8707
PM
5392 set x0 [lsearch -exact $previdlist $id]
5393 if {$x0 < 0} continue
5394 set z [expr {$x0 - $col}]
9f1afe05 5395 set isarrow 0
6e8c8707
PM
5396 set z0 {}
5397 if {$ym >= 0} {
5398 set xm [lsearch -exact $pprevidlist $id]
5399 if {$xm >= 0} {
5400 set z0 [expr {$xm - $x0}]
5401 }
5402 }
9f1afe05 5403 if {$z0 eq {}} {
92ed666f
PM
5404 # if row y0 is the first child of $id then it's not an arrow
5405 if {[lindex $children($curview,$id) 0] ne
5406 [lindex $displayorder $y0]} {
9f1afe05
PM
5407 set isarrow 1
5408 }
5409 }
e341c06d
PM
5410 if {!$isarrow && $id ne [lindex $displayorder $row] &&
5411 [lsearch -exact [lindex $rowidlist [expr {$row+1}]] $id] < 0} {
5412 set isarrow 1
5413 }
3fc4279a
PM
5414 # Looking at lines from this row to the previous row,
5415 # make them go straight up if they end in an arrow on
5416 # the previous row; otherwise make them go straight up
5417 # or at 45 degrees.
9f1afe05 5418 if {$z < -1 || ($z < 0 && $isarrow)} {
3fc4279a
PM
5419 # Line currently goes left too much;
5420 # insert pads in the previous row, then optimize it
9f1afe05 5421 set npad [expr {-1 - $z + $isarrow}]
9f1afe05
PM
5422 insert_pad $y0 $x0 $npad
5423 if {$y0 > 0} {
5424 optimize_rows $y0 $x0 $row
5425 }
6e8c8707
PM
5426 set previdlist [lindex $rowidlist $y0]
5427 set x0 [lsearch -exact $previdlist $id]
5428 set z [expr {$x0 - $col}]
5429 if {$z0 ne {}} {
5430 set pprevidlist [lindex $rowidlist $ym]
5431 set xm [lsearch -exact $pprevidlist $id]
5432 set z0 [expr {$xm - $x0}]
5433 }
9f1afe05 5434 } elseif {$z > 1 || ($z > 0 && $isarrow)} {
3fc4279a 5435 # Line currently goes right too much;
6e8c8707 5436 # insert pads in this line
9f1afe05 5437 set npad [expr {$z - 1 + $isarrow}]
e341c06d
PM
5438 insert_pad $row $col $npad
5439 set idlist [lindex $rowidlist $row]
9f1afe05 5440 incr col $npad
6e8c8707 5441 set z [expr {$x0 - $col}]
9f1afe05
PM
5442 set haspad 1
5443 }
6e8c8707 5444 if {$z0 eq {} && !$isarrow && $ym >= 0} {
eb447a12 5445 # this line links to its first child on row $row-2
6e8c8707
PM
5446 set id [lindex $displayorder $ym]
5447 set xc [lsearch -exact $pprevidlist $id]
eb447a12
PM
5448 if {$xc >= 0} {
5449 set z0 [expr {$xc - $x0}]
5450 }
5451 }
3fc4279a 5452 # avoid lines jigging left then immediately right
9f1afe05
PM
5453 if {$z0 ne {} && $z < 0 && $z0 > 0} {
5454 insert_pad $y0 $x0 1
6e8c8707
PM
5455 incr x0
5456 optimize_rows $y0 $x0 $row
5457 set previdlist [lindex $rowidlist $y0]
9f1afe05
PM
5458 }
5459 }
5460 if {!$haspad} {
3fc4279a 5461 # Find the first column that doesn't have a line going right
9f1afe05 5462 for {set col [llength $idlist]} {[incr col -1] >= 0} {} {
6e8c8707
PM
5463 set id [lindex $idlist $col]
5464 if {$id eq {}} break
5465 set x0 [lsearch -exact $previdlist $id]
5466 if {$x0 < 0} {
eb447a12 5467 # check if this is the link to the first child
92ed666f
PM
5468 set kid [lindex $displayorder $y0]
5469 if {[lindex $children($curview,$id) 0] eq $kid} {
eb447a12 5470 # it is, work out offset to child
92ed666f 5471 set x0 [lsearch -exact $previdlist $kid]
eb447a12
PM
5472 }
5473 }
6e8c8707 5474 if {$x0 <= $col} break
9f1afe05 5475 }
3fc4279a 5476 # Insert a pad at that column as long as it has a line and
6e8c8707
PM
5477 # isn't the last column
5478 if {$x0 >= 0 && [incr col] < [llength $idlist]} {
9f1afe05 5479 set idlist [linsert $idlist $col {}]
0380081c
PM
5480 lset rowidlist $row $idlist
5481 changedrow $row
9f1afe05
PM
5482 }
5483 }
9f1afe05
PM
5484 }
5485}
5486
5487proc xc {row col} {
5488 global canvx0 linespc
5489 return [expr {$canvx0 + $col * $linespc}]
5490}
5491
5492proc yc {row} {
5493 global canvy0 linespc
5494 return [expr {$canvy0 + $row * $linespc}]
5495}
5496
c934a8a3
PM
5497proc linewidth {id} {
5498 global thickerline lthickness
5499
5500 set wid $lthickness
5501 if {[info exists thickerline] && $id eq $thickerline} {
5502 set wid [expr {2 * $lthickness}]
5503 }
5504 return $wid
5505}
5506
50b44ece 5507proc rowranges {id} {
7fcc92bf 5508 global curview children uparrowlen downarrowlen
92ed666f 5509 global rowidlist
50b44ece 5510
92ed666f
PM
5511 set kids $children($curview,$id)
5512 if {$kids eq {}} {
5513 return {}
66e46f37 5514 }
92ed666f
PM
5515 set ret {}
5516 lappend kids $id
5517 foreach child $kids {
7fcc92bf
PM
5518 if {![commitinview $child $curview]} break
5519 set row [rowofcommit $child]
92ed666f
PM
5520 if {![info exists prev]} {
5521 lappend ret [expr {$row + 1}]
322a8cc9 5522 } else {
92ed666f 5523 if {$row <= $prevrow} {
7fcc92bf 5524 puts "oops children of [shortids $id] out of order [shortids $child] $row <= [shortids $prev] $prevrow"
92ed666f
PM
5525 }
5526 # see if the line extends the whole way from prevrow to row
5527 if {$row > $prevrow + $uparrowlen + $downarrowlen &&
5528 [lsearch -exact [lindex $rowidlist \
5529 [expr {int(($row + $prevrow) / 2)}]] $id] < 0} {
5530 # it doesn't, see where it ends
5531 set r [expr {$prevrow + $downarrowlen}]
5532 if {[lsearch -exact [lindex $rowidlist $r] $id] < 0} {
5533 while {[incr r -1] > $prevrow &&
5534 [lsearch -exact [lindex $rowidlist $r] $id] < 0} {}
5535 } else {
5536 while {[incr r] <= $row &&
5537 [lsearch -exact [lindex $rowidlist $r] $id] >= 0} {}
5538 incr r -1
5539 }
5540 lappend ret $r
5541 # see where it starts up again
5542 set r [expr {$row - $uparrowlen}]
5543 if {[lsearch -exact [lindex $rowidlist $r] $id] < 0} {
5544 while {[incr r] < $row &&
5545 [lsearch -exact [lindex $rowidlist $r] $id] < 0} {}
5546 } else {
5547 while {[incr r -1] >= $prevrow &&
5548 [lsearch -exact [lindex $rowidlist $r] $id] >= 0} {}
5549 incr r
5550 }
5551 lappend ret $r
5552 }
5553 }
5554 if {$child eq $id} {
5555 lappend ret $row
322a8cc9 5556 }
7fcc92bf 5557 set prev $child
92ed666f 5558 set prevrow $row
9f1afe05 5559 }
92ed666f 5560 return $ret
322a8cc9
PM
5561}
5562
5563proc drawlineseg {id row endrow arrowlow} {
5564 global rowidlist displayorder iddrawn linesegs
e341c06d 5565 global canv colormap linespc curview maxlinelen parentlist
322a8cc9
PM
5566
5567 set cols [list [lsearch -exact [lindex $rowidlist $row] $id]]
5568 set le [expr {$row + 1}]
5569 set arrowhigh 1
9f1afe05 5570 while {1} {
322a8cc9
PM
5571 set c [lsearch -exact [lindex $rowidlist $le] $id]
5572 if {$c < 0} {
5573 incr le -1
5574 break
5575 }
5576 lappend cols $c
5577 set x [lindex $displayorder $le]
5578 if {$x eq $id} {
5579 set arrowhigh 0
5580 break
9f1afe05 5581 }
322a8cc9
PM
5582 if {[info exists iddrawn($x)] || $le == $endrow} {
5583 set c [lsearch -exact [lindex $rowidlist [expr {$le+1}]] $id]
5584 if {$c >= 0} {
5585 lappend cols $c
5586 set arrowhigh 0
5587 }
5588 break
5589 }
5590 incr le
9f1afe05 5591 }
322a8cc9
PM
5592 if {$le <= $row} {
5593 return $row
5594 }
5595
5596 set lines {}
5597 set i 0
5598 set joinhigh 0
5599 if {[info exists linesegs($id)]} {
5600 set lines $linesegs($id)
5601 foreach li $lines {
5602 set r0 [lindex $li 0]
5603 if {$r0 > $row} {
5604 if {$r0 == $le && [lindex $li 1] - $row <= $maxlinelen} {
5605 set joinhigh 1
5606 }
5607 break
5608 }
5609 incr i
5610 }
5611 }
5612 set joinlow 0
5613 if {$i > 0} {
5614 set li [lindex $lines [expr {$i-1}]]
5615 set r1 [lindex $li 1]
5616 if {$r1 == $row && $le - [lindex $li 0] <= $maxlinelen} {
5617 set joinlow 1
5618 }
5619 }
5620
5621 set x [lindex $cols [expr {$le - $row}]]
5622 set xp [lindex $cols [expr {$le - 1 - $row}]]
5623 set dir [expr {$xp - $x}]
5624 if {$joinhigh} {
5625 set ith [lindex $lines $i 2]
5626 set coords [$canv coords $ith]
5627 set ah [$canv itemcget $ith -arrow]
5628 set arrowhigh [expr {$ah eq "first" || $ah eq "both"}]
5629 set x2 [lindex $cols [expr {$le + 1 - $row}]]
5630 if {$x2 ne {} && $x - $x2 == $dir} {
5631 set coords [lrange $coords 0 end-2]
5632 }
5633 } else {
5634 set coords [list [xc $le $x] [yc $le]]
5635 }
5636 if {$joinlow} {
5637 set itl [lindex $lines [expr {$i-1}] 2]
5638 set al [$canv itemcget $itl -arrow]
5639 set arrowlow [expr {$al eq "last" || $al eq "both"}]
e341c06d
PM
5640 } elseif {$arrowlow} {
5641 if {[lsearch -exact [lindex $rowidlist [expr {$row-1}]] $id] >= 0 ||
5642 [lsearch -exact [lindex $parentlist [expr {$row-1}]] $id] >= 0} {
5643 set arrowlow 0
5644 }
322a8cc9
PM
5645 }
5646 set arrow [lindex {none first last both} [expr {$arrowhigh + 2*$arrowlow}]]
5647 for {set y $le} {[incr y -1] > $row} {} {
5648 set x $xp
5649 set xp [lindex $cols [expr {$y - 1 - $row}]]
5650 set ndir [expr {$xp - $x}]
5651 if {$dir != $ndir || $xp < 0} {
5652 lappend coords [xc $y $x] [yc $y]
5653 }
5654 set dir $ndir
5655 }
5656 if {!$joinlow} {
5657 if {$xp < 0} {
5658 # join parent line to first child
5659 set ch [lindex $displayorder $row]
5660 set xc [lsearch -exact [lindex $rowidlist $row] $ch]
5661 if {$xc < 0} {
5662 puts "oops: drawlineseg: child $ch not on row $row"
e341c06d
PM
5663 } elseif {$xc != $x} {
5664 if {($arrowhigh && $le == $row + 1) || $dir == 0} {
5665 set d [expr {int(0.5 * $linespc)}]
5666 set x1 [xc $row $x]
5667 if {$xc < $x} {
5668 set x2 [expr {$x1 - $d}]
5669 } else {
5670 set x2 [expr {$x1 + $d}]
5671 }
5672 set y2 [yc $row]
5673 set y1 [expr {$y2 + $d}]
5674 lappend coords $x1 $y1 $x2 $y2
5675 } elseif {$xc < $x - 1} {
322a8cc9
PM
5676 lappend coords [xc $row [expr {$x-1}]] [yc $row]
5677 } elseif {$xc > $x + 1} {
5678 lappend coords [xc $row [expr {$x+1}]] [yc $row]
5679 }
5680 set x $xc
eb447a12 5681 }
322a8cc9
PM
5682 lappend coords [xc $row $x] [yc $row]
5683 } else {
5684 set xn [xc $row $xp]
5685 set yn [yc $row]
e341c06d 5686 lappend coords $xn $yn
322a8cc9
PM
5687 }
5688 if {!$joinhigh} {
322a8cc9
PM
5689 assigncolor $id
5690 set t [$canv create line $coords -width [linewidth $id] \
5691 -fill $colormap($id) -tags lines.$id -arrow $arrow]
5692 $canv lower $t
5693 bindline $t $id
5694 set lines [linsert $lines $i [list $row $le $t]]
5695 } else {
5696 $canv coords $ith $coords
5697 if {$arrow ne $ah} {
5698 $canv itemconf $ith -arrow $arrow
5699 }
5700 lset lines $i 0 $row
5701 }
5702 } else {
5703 set xo [lsearch -exact [lindex $rowidlist [expr {$row - 1}]] $id]
5704 set ndir [expr {$xo - $xp}]
5705 set clow [$canv coords $itl]
5706 if {$dir == $ndir} {
5707 set clow [lrange $clow 2 end]
5708 }
5709 set coords [concat $coords $clow]
5710 if {!$joinhigh} {
5711 lset lines [expr {$i-1}] 1 $le
322a8cc9
PM
5712 } else {
5713 # coalesce two pieces
5714 $canv delete $ith
5715 set b [lindex $lines [expr {$i-1}] 0]
5716 set e [lindex $lines $i 1]
5717 set lines [lreplace $lines [expr {$i-1}] $i [list $b $e $itl]]
5718 }
5719 $canv coords $itl $coords
5720 if {$arrow ne $al} {
5721 $canv itemconf $itl -arrow $arrow
879e8b1a
PM
5722 }
5723 }
322a8cc9
PM
5724
5725 set linesegs($id) $lines
5726 return $le
9f1afe05
PM
5727}
5728
322a8cc9
PM
5729proc drawparentlinks {id row} {
5730 global rowidlist canv colormap curview parentlist
513a54dc 5731 global idpos linespc
9f1afe05 5732
322a8cc9
PM
5733 set rowids [lindex $rowidlist $row]
5734 set col [lsearch -exact $rowids $id]
5735 if {$col < 0} return
5736 set olds [lindex $parentlist $row]
9f1afe05
PM
5737 set row2 [expr {$row + 1}]
5738 set x [xc $row $col]
5739 set y [yc $row]
5740 set y2 [yc $row2]
e341c06d 5741 set d [expr {int(0.5 * $linespc)}]
513a54dc 5742 set ymid [expr {$y + $d}]
8f7d0cec 5743 set ids [lindex $rowidlist $row2]
9f1afe05
PM
5744 # rmx = right-most X coord used
5745 set rmx 0
9f1afe05 5746 foreach p $olds {
f3408449
PM
5747 set i [lsearch -exact $ids $p]
5748 if {$i < 0} {
5749 puts "oops, parent $p of $id not in list"
5750 continue
5751 }
5752 set x2 [xc $row2 $i]
5753 if {$x2 > $rmx} {
5754 set rmx $x2
5755 }
513a54dc
PM
5756 set j [lsearch -exact $rowids $p]
5757 if {$j < 0} {
eb447a12
PM
5758 # drawlineseg will do this one for us
5759 continue
5760 }
9f1afe05
PM
5761 assigncolor $p
5762 # should handle duplicated parents here...
5763 set coords [list $x $y]
513a54dc
PM
5764 if {$i != $col} {
5765 # if attaching to a vertical segment, draw a smaller
5766 # slant for visual distinctness
5767 if {$i == $j} {
5768 if {$i < $col} {
5769 lappend coords [expr {$x2 + $d}] $y $x2 $ymid
5770 } else {
5771 lappend coords [expr {$x2 - $d}] $y $x2 $ymid
5772 }
5773 } elseif {$i < $col && $i < $j} {
5774 # segment slants towards us already
5775 lappend coords [xc $row $j] $y
5776 } else {
5777 if {$i < $col - 1} {
5778 lappend coords [expr {$x2 + $linespc}] $y
5779 } elseif {$i > $col + 1} {
5780 lappend coords [expr {$x2 - $linespc}] $y
5781 }
5782 lappend coords $x2 $y2
5783 }
5784 } else {
5785 lappend coords $x2 $y2
9f1afe05 5786 }
c934a8a3 5787 set t [$canv create line $coords -width [linewidth $p] \
9f1afe05
PM
5788 -fill $colormap($p) -tags lines.$p]
5789 $canv lower $t
5790 bindline $t $p
5791 }
322a8cc9
PM
5792 if {$rmx > [lindex $idpos($id) 1]} {
5793 lset idpos($id) 1 $rmx
5794 redrawtags $id
5795 }
9f1afe05
PM
5796}
5797
c934a8a3 5798proc drawlines {id} {
322a8cc9 5799 global canv
9f1afe05 5800
322a8cc9 5801 $canv itemconf lines.$id -width [linewidth $id]
9f1afe05
PM
5802}
5803
322a8cc9 5804proc drawcmittext {id row col} {
7fcc92bf
PM
5805 global linespc canv canv2 canv3 fgcolor curview
5806 global cmitlisted commitinfo rowidlist parentlist
9f1afe05 5807 global rowtextx idpos idtags idheads idotherrefs
0380081c 5808 global linehtag linentag linedtag selectedline
b9fdba7f 5809 global canvxmax boldids boldnameids fgcolor markedid
d277e89f 5810 global mainheadid nullid nullid2 circleitem circlecolors ctxbut
9f1afe05 5811
1407ade9 5812 # listed is 0 for boundary, 1 for normal, 2 for negative, 3 for left, 4 for right
7fcc92bf 5813 set listed $cmitlisted($curview,$id)
219ea3a9
PM
5814 if {$id eq $nullid} {
5815 set ofill red
8f489363 5816 } elseif {$id eq $nullid2} {
ef3192b8 5817 set ofill green
c11ff120
PM
5818 } elseif {$id eq $mainheadid} {
5819 set ofill yellow
219ea3a9 5820 } else {
c11ff120 5821 set ofill [lindex $circlecolors $listed]
219ea3a9 5822 }
9f1afe05
PM
5823 set x [xc $row $col]
5824 set y [yc $row]
5825 set orad [expr {$linespc / 3}]
1407ade9 5826 if {$listed <= 2} {
c961b228
PM
5827 set t [$canv create oval [expr {$x - $orad}] [expr {$y - $orad}] \
5828 [expr {$x + $orad - 1}] [expr {$y + $orad - 1}] \
5829 -fill $ofill -outline $fgcolor -width 1 -tags circle]
1407ade9 5830 } elseif {$listed == 3} {
c961b228
PM
5831 # triangle pointing left for left-side commits
5832 set t [$canv create polygon \
5833 [expr {$x - $orad}] $y \
5834 [expr {$x + $orad - 1}] [expr {$y - $orad}] \
5835 [expr {$x + $orad - 1}] [expr {$y + $orad - 1}] \
5836 -fill $ofill -outline $fgcolor -width 1 -tags circle]
5837 } else {
5838 # triangle pointing right for right-side commits
5839 set t [$canv create polygon \
5840 [expr {$x + $orad - 1}] $y \
5841 [expr {$x - $orad}] [expr {$y - $orad}] \
5842 [expr {$x - $orad}] [expr {$y + $orad - 1}] \
5843 -fill $ofill -outline $fgcolor -width 1 -tags circle]
5844 }
c11ff120 5845 set circleitem($row) $t
9f1afe05
PM
5846 $canv raise $t
5847 $canv bind $t <1> {selcanvline {} %x %y}
322a8cc9
PM
5848 set rmx [llength [lindex $rowidlist $row]]
5849 set olds [lindex $parentlist $row]
5850 if {$olds ne {}} {
5851 set nextids [lindex $rowidlist [expr {$row + 1}]]
5852 foreach p $olds {
5853 set i [lsearch -exact $nextids $p]
5854 if {$i > $rmx} {
5855 set rmx $i
5856 }
5857 }
9f1afe05 5858 }
322a8cc9 5859 set xt [xc $row $rmx]
9f1afe05
PM
5860 set rowtextx($row) $xt
5861 set idpos($id) [list $x $xt $y]
5862 if {[info exists idtags($id)] || [info exists idheads($id)]
5863 || [info exists idotherrefs($id)]} {
5864 set xt [drawtags $id $x $xt $y]
5865 }
5866 set headline [lindex $commitinfo($id) 0]
5867 set name [lindex $commitinfo($id) 1]
5868 set date [lindex $commitinfo($id) 2]
5869 set date [formatdate $date]
9c311b32
PM
5870 set font mainfont
5871 set nfont mainfont
476ca63d 5872 set isbold [ishighlighted $id]
908c3585 5873 if {$isbold > 0} {
28593d3f 5874 lappend boldids $id
9c311b32 5875 set font mainfontbold
908c3585 5876 if {$isbold > 1} {
28593d3f 5877 lappend boldnameids $id
9c311b32 5878 set nfont mainfontbold
908c3585 5879 }
da7c24dd 5880 }
28593d3f
PM
5881 set linehtag($id) [$canv create text $xt $y -anchor w -fill $fgcolor \
5882 -text $headline -font $font -tags text]
5883 $canv bind $linehtag($id) $ctxbut "rowmenu %X %Y $id"
5884 set linentag($id) [$canv2 create text 3 $y -anchor w -fill $fgcolor \
5885 -text $name -font $nfont -tags text]
5886 set linedtag($id) [$canv3 create text 3 $y -anchor w -fill $fgcolor \
5887 -text $date -font mainfont -tags text]
94b4a69f 5888 if {$selectedline == $row} {
28593d3f 5889 make_secsel $id
0380081c 5890 }
b9fdba7f
PM
5891 if {[info exists markedid] && $markedid eq $id} {
5892 make_idmark $id
5893 }
9c311b32 5894 set xr [expr {$xt + [font measure $font $headline]}]
be0cd098
PM
5895 if {$xr > $canvxmax} {
5896 set canvxmax $xr
5897 setcanvscroll
5898 }
9f1afe05
PM
5899}
5900
5901proc drawcmitrow {row} {
0380081c 5902 global displayorder rowidlist nrows_drawn
005a2f4e 5903 global iddrawn markingmatches
7fcc92bf 5904 global commitinfo numcommits
687c8765 5905 global filehighlight fhighlights findpattern nhighlights
908c3585 5906 global hlview vhighlights
164ff275 5907 global highlight_related rhighlights
9f1afe05 5908
8f7d0cec 5909 if {$row >= $numcommits} return
9f1afe05
PM
5910
5911 set id [lindex $displayorder $row]
476ca63d 5912 if {[info exists hlview] && ![info exists vhighlights($id)]} {
908c3585
PM
5913 askvhighlight $row $id
5914 }
476ca63d 5915 if {[info exists filehighlight] && ![info exists fhighlights($id)]} {
908c3585
PM
5916 askfilehighlight $row $id
5917 }
476ca63d 5918 if {$findpattern ne {} && ![info exists nhighlights($id)]} {
60f7a7dc 5919 askfindhighlight $row $id
908c3585 5920 }
476ca63d 5921 if {$highlight_related ne [mc "None"] && ![info exists rhighlights($id)]} {
164ff275
PM
5922 askrelhighlight $row $id
5923 }
005a2f4e
PM
5924 if {![info exists iddrawn($id)]} {
5925 set col [lsearch -exact [lindex $rowidlist $row] $id]
5926 if {$col < 0} {
5927 puts "oops, row $row id $id not in list"
5928 return
5929 }
5930 if {![info exists commitinfo($id)]} {
5931 getcommit $id
5932 }
5933 assigncolor $id
5934 drawcmittext $id $row $col
5935 set iddrawn($id) 1
0380081c 5936 incr nrows_drawn
9f1afe05 5937 }
005a2f4e
PM
5938 if {$markingmatches} {
5939 markrowmatches $row $id
9f1afe05 5940 }
9f1afe05
PM
5941}
5942
322a8cc9 5943proc drawcommits {row {endrow {}}} {
0380081c 5944 global numcommits iddrawn displayorder curview need_redisplay
f5f3c2e2 5945 global parentlist rowidlist rowfinal uparrowlen downarrowlen nrows_drawn
9f1afe05 5946
9f1afe05
PM
5947 if {$row < 0} {
5948 set row 0
5949 }
322a8cc9
PM
5950 if {$endrow eq {}} {
5951 set endrow $row
5952 }
9f1afe05
PM
5953 if {$endrow >= $numcommits} {
5954 set endrow [expr {$numcommits - 1}]
5955 }
322a8cc9 5956
0380081c
PM
5957 set rl1 [expr {$row - $downarrowlen - 3}]
5958 if {$rl1 < 0} {
5959 set rl1 0
5960 }
5961 set ro1 [expr {$row - 3}]
5962 if {$ro1 < 0} {
5963 set ro1 0
5964 }
5965 set r2 [expr {$endrow + $uparrowlen + 3}]
5966 if {$r2 > $numcommits} {
5967 set r2 $numcommits
5968 }
5969 for {set r $rl1} {$r < $r2} {incr r} {
f5f3c2e2 5970 if {[lindex $rowidlist $r] ne {} && [lindex $rowfinal $r]} {
0380081c
PM
5971 if {$rl1 < $r} {
5972 layoutrows $rl1 $r
5973 }
5974 set rl1 [expr {$r + 1}]
5975 }
5976 }
5977 if {$rl1 < $r} {
5978 layoutrows $rl1 $r
5979 }
5980 optimize_rows $ro1 0 $r2
5981 if {$need_redisplay || $nrows_drawn > 2000} {
5982 clear_display
0380081c
PM
5983 }
5984
322a8cc9
PM
5985 # make the lines join to already-drawn rows either side
5986 set r [expr {$row - 1}]
5987 if {$r < 0 || ![info exists iddrawn([lindex $displayorder $r])]} {
5988 set r $row
5989 }
5990 set er [expr {$endrow + 1}]
5991 if {$er >= $numcommits ||
5992 ![info exists iddrawn([lindex $displayorder $er])]} {
5993 set er $endrow
5994 }
5995 for {} {$r <= $er} {incr r} {
5996 set id [lindex $displayorder $r]
5997 set wasdrawn [info exists iddrawn($id)]
4fb0fa19 5998 drawcmitrow $r
322a8cc9
PM
5999 if {$r == $er} break
6000 set nextid [lindex $displayorder [expr {$r + 1}]]
e5ef6f95 6001 if {$wasdrawn && [info exists iddrawn($nextid)]} continue
322a8cc9
PM
6002 drawparentlinks $id $r
6003
322a8cc9
PM
6004 set rowids [lindex $rowidlist $r]
6005 foreach lid $rowids {
6006 if {$lid eq {}} continue
e5ef6f95 6007 if {[info exists lineend($lid)] && $lineend($lid) > $r} continue
322a8cc9
PM
6008 if {$lid eq $id} {
6009 # see if this is the first child of any of its parents
6010 foreach p [lindex $parentlist $r] {
6011 if {[lsearch -exact $rowids $p] < 0} {
6012 # make this line extend up to the child
e5ef6f95 6013 set lineend($p) [drawlineseg $p $r $er 0]
322a8cc9
PM
6014 }
6015 }
e5ef6f95
PM
6016 } else {
6017 set lineend($lid) [drawlineseg $lid $r $er 1]
322a8cc9
PM
6018 }
6019 }
9f1afe05
PM
6020 }
6021}
6022
7fcc92bf
PM
6023proc undolayout {row} {
6024 global uparrowlen mingaplen downarrowlen
6025 global rowidlist rowisopt rowfinal need_redisplay
6026
6027 set r [expr {$row - ($uparrowlen + $mingaplen + $downarrowlen)}]
6028 if {$r < 0} {
6029 set r 0
6030 }
6031 if {[llength $rowidlist] > $r} {
6032 incr r -1
6033 set rowidlist [lrange $rowidlist 0 $r]
6034 set rowfinal [lrange $rowfinal 0 $r]
6035 set rowisopt [lrange $rowisopt 0 $r]
6036 set need_redisplay 1
6037 run drawvisible
6038 }
6039}
6040
31c0eaa8
PM
6041proc drawvisible {} {
6042 global canv linespc curview vrowmod selectedline targetrow targetid
42a671fc 6043 global need_redisplay cscroll numcommits
322a8cc9 6044
31c0eaa8 6045 set fs [$canv yview]
322a8cc9 6046 set ymax [lindex [$canv cget -scrollregion] 3]
5a7f577d 6047 if {$ymax eq {} || $ymax == 0 || $numcommits == 0} return
31c0eaa8
PM
6048 set f0 [lindex $fs 0]
6049 set f1 [lindex $fs 1]
322a8cc9 6050 set y0 [expr {int($f0 * $ymax)}]
322a8cc9 6051 set y1 [expr {int($f1 * $ymax)}]
31c0eaa8
PM
6052
6053 if {[info exists targetid]} {
42a671fc
PM
6054 if {[commitinview $targetid $curview]} {
6055 set r [rowofcommit $targetid]
6056 if {$r != $targetrow} {
6057 # Fix up the scrollregion and change the scrolling position
6058 # now that our target row has moved.
6059 set diff [expr {($r - $targetrow) * $linespc}]
6060 set targetrow $r
6061 setcanvscroll
6062 set ymax [lindex [$canv cget -scrollregion] 3]
6063 incr y0 $diff
6064 incr y1 $diff
6065 set f0 [expr {$y0 / $ymax}]
6066 set f1 [expr {$y1 / $ymax}]
6067 allcanvs yview moveto $f0
6068 $cscroll set $f0 $f1
6069 set need_redisplay 1
6070 }
6071 } else {
6072 unset targetid
31c0eaa8
PM
6073 }
6074 }
6075
6076 set row [expr {int(($y0 - 3) / $linespc) - 1}]
322a8cc9 6077 set endrow [expr {int(($y1 - 3) / $linespc) + 1}]
31c0eaa8
PM
6078 if {$endrow >= $vrowmod($curview)} {
6079 update_arcrows $curview
6080 }
94b4a69f 6081 if {$selectedline ne {} &&
31c0eaa8
PM
6082 $row <= $selectedline && $selectedline <= $endrow} {
6083 set targetrow $selectedline
ac1276ab 6084 } elseif {[info exists targetid]} {
31c0eaa8
PM
6085 set targetrow [expr {int(($row + $endrow) / 2)}]
6086 }
ac1276ab
PM
6087 if {[info exists targetrow]} {
6088 if {$targetrow >= $numcommits} {
6089 set targetrow [expr {$numcommits - 1}]
6090 }
6091 set targetid [commitonrow $targetrow]
42a671fc 6092 }
322a8cc9
PM
6093 drawcommits $row $endrow
6094}
6095
9f1afe05 6096proc clear_display {} {
0380081c 6097 global iddrawn linesegs need_redisplay nrows_drawn
164ff275 6098 global vhighlights fhighlights nhighlights rhighlights
28593d3f 6099 global linehtag linentag linedtag boldids boldnameids
9f1afe05
PM
6100
6101 allcanvs delete all
6102 catch {unset iddrawn}
322a8cc9 6103 catch {unset linesegs}
94503a66
PM
6104 catch {unset linehtag}
6105 catch {unset linentag}
6106 catch {unset linedtag}
28593d3f
PM
6107 set boldids {}
6108 set boldnameids {}
908c3585
PM
6109 catch {unset vhighlights}
6110 catch {unset fhighlights}
6111 catch {unset nhighlights}
164ff275 6112 catch {unset rhighlights}
0380081c
PM
6113 set need_redisplay 0
6114 set nrows_drawn 0
9f1afe05
PM
6115}
6116
50b44ece 6117proc findcrossings {id} {
6e8c8707 6118 global rowidlist parentlist numcommits displayorder
50b44ece
PM
6119
6120 set cross {}
6121 set ccross {}
6122 foreach {s e} [rowranges $id] {
6123 if {$e >= $numcommits} {
6124 set e [expr {$numcommits - 1}]
50b44ece 6125 }
d94f8cd6 6126 if {$e <= $s} continue
50b44ece 6127 for {set row $e} {[incr row -1] >= $s} {} {
6e8c8707
PM
6128 set x [lsearch -exact [lindex $rowidlist $row] $id]
6129 if {$x < 0} break
50b44ece
PM
6130 set olds [lindex $parentlist $row]
6131 set kid [lindex $displayorder $row]
6132 set kidx [lsearch -exact [lindex $rowidlist $row] $kid]
6133 if {$kidx < 0} continue
6134 set nextrow [lindex $rowidlist [expr {$row + 1}]]
6135 foreach p $olds {
6136 set px [lsearch -exact $nextrow $p]
6137 if {$px < 0} continue
6138 if {($kidx < $x && $x < $px) || ($px < $x && $x < $kidx)} {
6139 if {[lsearch -exact $ccross $p] >= 0} continue
6140 if {$x == $px + ($kidx < $px? -1: 1)} {
6141 lappend ccross $p
6142 } elseif {[lsearch -exact $cross $p] < 0} {
6143 lappend cross $p
6144 }
6145 }
6146 }
50b44ece
PM
6147 }
6148 }
6149 return [concat $ccross {{}} $cross]
6150}
6151
e5c2d856 6152proc assigncolor {id} {
aa81d974 6153 global colormap colors nextcolor
7fcc92bf 6154 global parents children children curview
6c20ff34 6155
418c4c7b 6156 if {[info exists colormap($id)]} return
e5c2d856 6157 set ncolors [llength $colors]
da7c24dd
PM
6158 if {[info exists children($curview,$id)]} {
6159 set kids $children($curview,$id)
79b2c75e
PM
6160 } else {
6161 set kids {}
6162 }
6163 if {[llength $kids] == 1} {
6164 set child [lindex $kids 0]
9ccbdfbf 6165 if {[info exists colormap($child)]
7fcc92bf 6166 && [llength $parents($curview,$child)] == 1} {
9ccbdfbf
PM
6167 set colormap($id) $colormap($child)
6168 return
e5c2d856 6169 }
9ccbdfbf
PM
6170 }
6171 set badcolors {}
50b44ece
PM
6172 set origbad {}
6173 foreach x [findcrossings $id] {
6174 if {$x eq {}} {
6175 # delimiter between corner crossings and other crossings
6176 if {[llength $badcolors] >= $ncolors - 1} break
6177 set origbad $badcolors
e5c2d856 6178 }
50b44ece
PM
6179 if {[info exists colormap($x)]
6180 && [lsearch -exact $badcolors $colormap($x)] < 0} {
6181 lappend badcolors $colormap($x)
6c20ff34
PM
6182 }
6183 }
50b44ece
PM
6184 if {[llength $badcolors] >= $ncolors} {
6185 set badcolors $origbad
9ccbdfbf 6186 }
50b44ece 6187 set origbad $badcolors
6c20ff34 6188 if {[llength $badcolors] < $ncolors - 1} {
79b2c75e 6189 foreach child $kids {
6c20ff34
PM
6190 if {[info exists colormap($child)]
6191 && [lsearch -exact $badcolors $colormap($child)] < 0} {
6192 lappend badcolors $colormap($child)
6193 }
7fcc92bf 6194 foreach p $parents($curview,$child) {
79b2c75e
PM
6195 if {[info exists colormap($p)]
6196 && [lsearch -exact $badcolors $colormap($p)] < 0} {
6197 lappend badcolors $colormap($p)
6c20ff34
PM
6198 }
6199 }
6200 }
6201 if {[llength $badcolors] >= $ncolors} {
6202 set badcolors $origbad
6203 }
9ccbdfbf
PM
6204 }
6205 for {set i 0} {$i <= $ncolors} {incr i} {
6206 set c [lindex $colors $nextcolor]
6207 if {[incr nextcolor] >= $ncolors} {
6208 set nextcolor 0
e5c2d856 6209 }
9ccbdfbf 6210 if {[lsearch -exact $badcolors $c]} break
e5c2d856 6211 }
9ccbdfbf 6212 set colormap($id) $c
e5c2d856
PM
6213}
6214
a823a911
PM
6215proc bindline {t id} {
6216 global canv
6217
a823a911
PM
6218 $canv bind $t <Enter> "lineenter %x %y $id"
6219 $canv bind $t <Motion> "linemotion %x %y $id"
6220 $canv bind $t <Leave> "lineleave $id"
fa4da7b3 6221 $canv bind $t <Button-1> "lineclick %x %y $id 1"
a823a911
PM
6222}
6223
bdbfbe3d 6224proc drawtags {id x xt y1} {
8a48571c 6225 global idtags idheads idotherrefs mainhead
bdbfbe3d 6226 global linespc lthickness
d277e89f 6227 global canv rowtextx curview fgcolor bgcolor ctxbut
bdbfbe3d
PM
6228
6229 set marks {}
6230 set ntags 0
f1d83ba3 6231 set nheads 0
bdbfbe3d
PM
6232 if {[info exists idtags($id)]} {
6233 set marks $idtags($id)
6234 set ntags [llength $marks]
6235 }
6236 if {[info exists idheads($id)]} {
6237 set marks [concat $marks $idheads($id)]
f1d83ba3
PM
6238 set nheads [llength $idheads($id)]
6239 }
6240 if {[info exists idotherrefs($id)]} {
6241 set marks [concat $marks $idotherrefs($id)]
bdbfbe3d
PM
6242 }
6243 if {$marks eq {}} {
6244 return $xt
6245 }
6246
6247 set delta [expr {int(0.5 * ($linespc - $lthickness))}]
2ed49d54
JH
6248 set yt [expr {$y1 - 0.5 * $linespc}]
6249 set yb [expr {$yt + $linespc - 1}]
bdbfbe3d
PM
6250 set xvals {}
6251 set wvals {}
8a48571c 6252 set i -1
bdbfbe3d 6253 foreach tag $marks {
8a48571c
PM
6254 incr i
6255 if {$i >= $ntags && $i < $ntags + $nheads && $tag eq $mainhead} {
9c311b32 6256 set wid [font measure mainfontbold $tag]
8a48571c 6257 } else {
9c311b32 6258 set wid [font measure mainfont $tag]
8a48571c 6259 }
bdbfbe3d
PM
6260 lappend xvals $xt
6261 lappend wvals $wid
6262 set xt [expr {$xt + $delta + $wid + $lthickness + $linespc}]
6263 }
6264 set t [$canv create line $x $y1 [lindex $xvals end] $y1 \
6265 -width $lthickness -fill black -tags tag.$id]
6266 $canv lower $t
6267 foreach tag $marks x $xvals wid $wvals {
2ed49d54
JH
6268 set xl [expr {$x + $delta}]
6269 set xr [expr {$x + $delta + $wid + $lthickness}]
9c311b32 6270 set font mainfont
bdbfbe3d
PM
6271 if {[incr ntags -1] >= 0} {
6272 # draw a tag
2ed49d54
JH
6273 set t [$canv create polygon $x [expr {$yt + $delta}] $xl $yt \
6274 $xr $yt $xr $yb $xl $yb $x [expr {$yb - $delta}] \
106288cb
PM
6275 -width 1 -outline black -fill yellow -tags tag.$id]
6276 $canv bind $t <1> [list showtag $tag 1]
7fcc92bf 6277 set rowtextx([rowofcommit $id]) [expr {$xr + $linespc}]
bdbfbe3d 6278 } else {
f1d83ba3
PM
6279 # draw a head or other ref
6280 if {[incr nheads -1] >= 0} {
6281 set col green
8a48571c 6282 if {$tag eq $mainhead} {
9c311b32 6283 set font mainfontbold
8a48571c 6284 }
f1d83ba3
PM
6285 } else {
6286 set col "#ddddff"
6287 }
2ed49d54 6288 set xl [expr {$xl - $delta/2}]
bdbfbe3d 6289 $canv create polygon $x $yt $xr $yt $xr $yb $x $yb \
f1d83ba3 6290 -width 1 -outline black -fill $col -tags tag.$id
a970fcf2 6291 if {[regexp {^(remotes/.*/|remotes/)} $tag match remoteprefix]} {
9c311b32 6292 set rwid [font measure mainfont $remoteprefix]
a970fcf2
JW
6293 set xi [expr {$x + 1}]
6294 set yti [expr {$yt + 1}]
6295 set xri [expr {$x + $rwid}]
6296 $canv create polygon $xi $yti $xri $yti $xri $yb $xi $yb \
6297 -width 0 -fill "#ffddaa" -tags tag.$id
6298 }
bdbfbe3d 6299 }
f8a2c0d1 6300 set t [$canv create text $xl $y1 -anchor w -text $tag -fill $fgcolor \
8a48571c 6301 -font $font -tags [list tag.$id text]]
106288cb
PM
6302 if {$ntags >= 0} {
6303 $canv bind $t <1> [list showtag $tag 1]
10299152 6304 } elseif {$nheads >= 0} {
d277e89f 6305 $canv bind $t $ctxbut [list headmenu %X %Y $id $tag]
106288cb 6306 }
bdbfbe3d
PM
6307 }
6308 return $xt
6309}
6310
8d858d1a
PM
6311proc xcoord {i level ln} {
6312 global canvx0 xspc1 xspc2
6313
6314 set x [expr {$canvx0 + $i * $xspc1($ln)}]
6315 if {$i > 0 && $i == $level} {
6316 set x [expr {$x + 0.5 * ($xspc2 - $xspc1($ln))}]
6317 } elseif {$i > $level} {
6318 set x [expr {$x + $xspc2 - $xspc1($ln)}]
6319 }
6320 return $x
6321}
9ccbdfbf 6322
098dd8a3 6323proc show_status {msg} {
9c311b32 6324 global canv fgcolor
098dd8a3
PM
6325
6326 clear_display
9c311b32 6327 $canv create text 3 3 -anchor nw -text $msg -font mainfont \
f8a2c0d1 6328 -tags text -fill $fgcolor
098dd8a3
PM
6329}
6330
94a2eede
PM
6331# Don't change the text pane cursor if it is currently the hand cursor,
6332# showing that we are over a sha1 ID link.
6333proc settextcursor {c} {
6334 global ctext curtextcursor
6335
6336 if {[$ctext cget -cursor] == $curtextcursor} {
6337 $ctext config -cursor $c
6338 }
6339 set curtextcursor $c
9ccbdfbf
PM
6340}
6341
a137a90f
PM
6342proc nowbusy {what {name {}}} {
6343 global isbusy busyname statusw
da7c24dd
PM
6344
6345 if {[array names isbusy] eq {}} {
6346 . config -cursor watch
6347 settextcursor watch
6348 }
6349 set isbusy($what) 1
a137a90f
PM
6350 set busyname($what) $name
6351 if {$name ne {}} {
6352 $statusw conf -text $name
6353 }
da7c24dd
PM
6354}
6355
6356proc notbusy {what} {
a137a90f 6357 global isbusy maincursor textcursor busyname statusw
da7c24dd 6358
a137a90f
PM
6359 catch {
6360 unset isbusy($what)
6361 if {$busyname($what) ne {} &&
6362 [$statusw cget -text] eq $busyname($what)} {
6363 $statusw conf -text {}
6364 }
6365 }
da7c24dd
PM
6366 if {[array names isbusy] eq {}} {
6367 . config -cursor $maincursor
6368 settextcursor $textcursor
6369 }
6370}
6371
df3d83b1 6372proc findmatches {f} {
4fb0fa19 6373 global findtype findstring
b007ee20 6374 if {$findtype == [mc "Regexp"]} {
4fb0fa19 6375 set matches [regexp -indices -all -inline $findstring $f]
df3d83b1 6376 } else {
4fb0fa19 6377 set fs $findstring
b007ee20 6378 if {$findtype == [mc "IgnCase"]} {
4fb0fa19
PM
6379 set f [string tolower $f]
6380 set fs [string tolower $fs]
df3d83b1
PM
6381 }
6382 set matches {}
6383 set i 0
4fb0fa19
PM
6384 set l [string length $fs]
6385 while {[set j [string first $fs $f $i]] >= 0} {
6386 lappend matches [list $j [expr {$j+$l-1}]]
6387 set i [expr {$j + $l}]
df3d83b1
PM
6388 }
6389 }
6390 return $matches
6391}
6392
cca5d946 6393proc dofind {{dirn 1} {wrap 1}} {
4fb0fa19 6394 global findstring findstartline findcurline selectedline numcommits
cca5d946 6395 global gdttype filehighlight fh_serial find_dirn findallowwrap
b74fd579 6396
cca5d946
PM
6397 if {[info exists find_dirn]} {
6398 if {$find_dirn == $dirn} return
6399 stopfinding
6400 }
df3d83b1 6401 focus .
4fb0fa19 6402 if {$findstring eq {} || $numcommits == 0} return
94b4a69f 6403 if {$selectedline eq {}} {
cca5d946 6404 set findstartline [lindex [visiblerows] [expr {$dirn < 0}]]
98f350e5 6405 } else {
4fb0fa19 6406 set findstartline $selectedline
98f350e5 6407 }
4fb0fa19 6408 set findcurline $findstartline
b007ee20
CS
6409 nowbusy finding [mc "Searching"]
6410 if {$gdttype ne [mc "containing:"] && ![info exists filehighlight]} {
687c8765
PM
6411 after cancel do_file_hl $fh_serial
6412 do_file_hl $fh_serial
98f350e5 6413 }
cca5d946
PM
6414 set find_dirn $dirn
6415 set findallowwrap $wrap
6416 run findmore
4fb0fa19
PM
6417}
6418
bb3edc8b
PM
6419proc stopfinding {} {
6420 global find_dirn findcurline fprogcoord
4fb0fa19 6421
bb3edc8b
PM
6422 if {[info exists find_dirn]} {
6423 unset find_dirn
6424 unset findcurline
6425 notbusy finding
6426 set fprogcoord 0
6427 adjustprogress
4fb0fa19 6428 }
8a897742 6429 stopblaming
4fb0fa19
PM
6430}
6431
6432proc findmore {} {
687c8765 6433 global commitdata commitinfo numcommits findpattern findloc
7fcc92bf 6434 global findstartline findcurline findallowwrap
bb3edc8b 6435 global find_dirn gdttype fhighlights fprogcoord
cd2bcae7 6436 global curview varcorder vrownum varccommits vrowmod
4fb0fa19 6437
bb3edc8b 6438 if {![info exists find_dirn]} {
4fb0fa19
PM
6439 return 0
6440 }
b007ee20 6441 set fldtypes [list [mc "Headline"] [mc "Author"] [mc "Date"] [mc "Committer"] [mc "CDate"] [mc "Comments"]]
4fb0fa19 6442 set l $findcurline
cca5d946
PM
6443 set moretodo 0
6444 if {$find_dirn > 0} {
6445 incr l
6446 if {$l >= $numcommits} {
6447 set l 0
6448 }
6449 if {$l <= $findstartline} {
6450 set lim [expr {$findstartline + 1}]
6451 } else {
6452 set lim $numcommits
6453 set moretodo $findallowwrap
8ed16484 6454 }
4fb0fa19 6455 } else {
cca5d946
PM
6456 if {$l == 0} {
6457 set l $numcommits
98f350e5 6458 }
cca5d946
PM
6459 incr l -1
6460 if {$l >= $findstartline} {
6461 set lim [expr {$findstartline - 1}]
bb3edc8b 6462 } else {
cca5d946
PM
6463 set lim -1
6464 set moretodo $findallowwrap
bb3edc8b 6465 }
687c8765 6466 }
cca5d946
PM
6467 set n [expr {($lim - $l) * $find_dirn}]
6468 if {$n > 500} {
6469 set n 500
6470 set moretodo 1
4fb0fa19 6471 }
cd2bcae7
PM
6472 if {$l + ($find_dirn > 0? $n: 1) > $vrowmod($curview)} {
6473 update_arcrows $curview
6474 }
687c8765
PM
6475 set found 0
6476 set domore 1
7fcc92bf
PM
6477 set ai [bsearch $vrownum($curview) $l]
6478 set a [lindex $varcorder($curview) $ai]
6479 set arow [lindex $vrownum($curview) $ai]
6480 set ids [lindex $varccommits($curview,$a)]
6481 set arowend [expr {$arow + [llength $ids]}]
b007ee20 6482 if {$gdttype eq [mc "containing:"]} {
cca5d946 6483 for {} {$n > 0} {incr n -1; incr l $find_dirn} {
7fcc92bf
PM
6484 if {$l < $arow || $l >= $arowend} {
6485 incr ai $find_dirn
6486 set a [lindex $varcorder($curview) $ai]
6487 set arow [lindex $vrownum($curview) $ai]
6488 set ids [lindex $varccommits($curview,$a)]
6489 set arowend [expr {$arow + [llength $ids]}]
6490 }
6491 set id [lindex $ids [expr {$l - $arow}]]
cca5d946 6492 # shouldn't happen unless git log doesn't give all the commits...
7fcc92bf
PM
6493 if {![info exists commitdata($id)] ||
6494 ![doesmatch $commitdata($id)]} {
6495 continue
6496 }
687c8765
PM
6497 if {![info exists commitinfo($id)]} {
6498 getcommit $id
6499 }
6500 set info $commitinfo($id)
6501 foreach f $info ty $fldtypes {
b007ee20 6502 if {($findloc eq [mc "All fields"] || $findloc eq $ty) &&
687c8765
PM
6503 [doesmatch $f]} {
6504 set found 1
6505 break
6506 }
6507 }
6508 if {$found} break
4fb0fa19 6509 }
687c8765 6510 } else {
cca5d946 6511 for {} {$n > 0} {incr n -1; incr l $find_dirn} {
7fcc92bf
PM
6512 if {$l < $arow || $l >= $arowend} {
6513 incr ai $find_dirn
6514 set a [lindex $varcorder($curview) $ai]
6515 set arow [lindex $vrownum($curview) $ai]
6516 set ids [lindex $varccommits($curview,$a)]
6517 set arowend [expr {$arow + [llength $ids]}]
6518 }
6519 set id [lindex $ids [expr {$l - $arow}]]
476ca63d
PM
6520 if {![info exists fhighlights($id)]} {
6521 # this sets fhighlights($id) to -1
687c8765 6522 askfilehighlight $l $id
cd2bcae7 6523 }
476ca63d 6524 if {$fhighlights($id) > 0} {
cd2bcae7
PM
6525 set found $domore
6526 break
6527 }
476ca63d 6528 if {$fhighlights($id) < 0} {
687c8765
PM
6529 if {$domore} {
6530 set domore 0
cca5d946 6531 set findcurline [expr {$l - $find_dirn}]
687c8765 6532 }
98f350e5
PM
6533 }
6534 }
6535 }
cca5d946 6536 if {$found || ($domore && !$moretodo)} {
4fb0fa19 6537 unset findcurline
687c8765 6538 unset find_dirn
4fb0fa19 6539 notbusy finding
bb3edc8b
PM
6540 set fprogcoord 0
6541 adjustprogress
6542 if {$found} {
6543 findselectline $l
6544 } else {
6545 bell
6546 }
4fb0fa19 6547 return 0
df3d83b1 6548 }
687c8765
PM
6549 if {!$domore} {
6550 flushhighlights
bb3edc8b 6551 } else {
cca5d946 6552 set findcurline [expr {$l - $find_dirn}]
687c8765 6553 }
cca5d946 6554 set n [expr {($findcurline - $findstartline) * $find_dirn - 1}]
bb3edc8b
PM
6555 if {$n < 0} {
6556 incr n $numcommits
df3d83b1 6557 }
bb3edc8b
PM
6558 set fprogcoord [expr {$n * 1.0 / $numcommits}]
6559 adjustprogress
6560 return $domore
df3d83b1
PM
6561}
6562
6563proc findselectline {l} {
687c8765 6564 global findloc commentend ctext findcurline markingmatches gdttype
005a2f4e 6565
8b39e04f 6566 set markingmatches [expr {$gdttype eq [mc "containing:"]}]
005a2f4e 6567 set findcurline $l
d698206c 6568 selectline $l 1
8b39e04f
PM
6569 if {$markingmatches &&
6570 ($findloc eq [mc "All fields"] || $findloc eq [mc "Comments"])} {
df3d83b1
PM
6571 # highlight the matches in the comments
6572 set f [$ctext get 1.0 $commentend]
6573 set matches [findmatches $f]
6574 foreach match $matches {
6575 set start [lindex $match 0]
2ed49d54 6576 set end [expr {[lindex $match 1] + 1}]
df3d83b1
PM
6577 $ctext tag add found "1.0 + $start c" "1.0 + $end c"
6578 }
98f350e5 6579 }
005a2f4e 6580 drawvisible
98f350e5
PM
6581}
6582
4fb0fa19 6583# mark the bits of a headline or author that match a find string
005a2f4e
PM
6584proc markmatches {canv l str tag matches font row} {
6585 global selectedline
6586
98f350e5
PM
6587 set bbox [$canv bbox $tag]
6588 set x0 [lindex $bbox 0]
6589 set y0 [lindex $bbox 1]
6590 set y1 [lindex $bbox 3]
6591 foreach match $matches {
6592 set start [lindex $match 0]
6593 set end [lindex $match 1]
6594 if {$start > $end} continue
2ed49d54
JH
6595 set xoff [font measure $font [string range $str 0 [expr {$start-1}]]]
6596 set xlen [font measure $font [string range $str 0 [expr {$end}]]]
6597 set t [$canv create rect [expr {$x0+$xoff}] $y0 \
6598 [expr {$x0+$xlen+2}] $y1 \
4fb0fa19 6599 -outline {} -tags [list match$l matches] -fill yellow]
98f350e5 6600 $canv lower $t
94b4a69f 6601 if {$row == $selectedline} {
005a2f4e
PM
6602 $canv raise $t secsel
6603 }
98f350e5
PM
6604 }
6605}
6606
6607proc unmarkmatches {} {
bb3edc8b 6608 global markingmatches
4fb0fa19 6609
98f350e5 6610 allcanvs delete matches
4fb0fa19 6611 set markingmatches 0
bb3edc8b 6612 stopfinding
98f350e5
PM
6613}
6614
c8dfbcf9 6615proc selcanvline {w x y} {
fa4da7b3 6616 global canv canvy0 ctext linespc
9f1afe05 6617 global rowtextx
1db95b00 6618 set ymax [lindex [$canv cget -scrollregion] 3]
cfb4563c 6619 if {$ymax == {}} return
1db95b00
PM
6620 set yfrac [lindex [$canv yview] 0]
6621 set y [expr {$y + $yfrac * $ymax}]
6622 set l [expr {int(($y - $canvy0) / $linespc + 0.5)}]
6623 if {$l < 0} {
6624 set l 0
6625 }
c8dfbcf9 6626 if {$w eq $canv} {
fc2a256f
PM
6627 set xmax [lindex [$canv cget -scrollregion] 2]
6628 set xleft [expr {[lindex [$canv xview] 0] * $xmax}]
6629 if {![info exists rowtextx($l)] || $xleft + $x < $rowtextx($l)} return
c8dfbcf9 6630 }
98f350e5 6631 unmarkmatches
d698206c 6632 selectline $l 1
5ad588de
PM
6633}
6634
b1ba39e7
LT
6635proc commit_descriptor {p} {
6636 global commitinfo
b0934489
PM
6637 if {![info exists commitinfo($p)]} {
6638 getcommit $p
6639 }
b1ba39e7 6640 set l "..."
b0934489 6641 if {[llength $commitinfo($p)] > 1} {
b1ba39e7
LT
6642 set l [lindex $commitinfo($p) 0]
6643 }
b8ab2e17 6644 return "$p ($l)\n"
b1ba39e7
LT
6645}
6646
106288cb
PM
6647# append some text to the ctext widget, and make any SHA1 ID
6648# that we know about be a clickable link.
f1b86294 6649proc appendwithlinks {text tags} {
d375ef9b 6650 global ctext linknum curview
106288cb
PM
6651
6652 set start [$ctext index "end - 1c"]
f1b86294 6653 $ctext insert end $text $tags
d375ef9b 6654 set links [regexp -indices -all -inline {\m[0-9a-f]{6,40}\M} $text]
106288cb
PM
6655 foreach l $links {
6656 set s [lindex $l 0]
6657 set e [lindex $l 1]
6658 set linkid [string range $text $s $e]
106288cb 6659 incr e
c73adce2 6660 $ctext tag delete link$linknum
106288cb 6661 $ctext tag add link$linknum "$start + $s c" "$start + $e c"
97645683 6662 setlink $linkid link$linknum
106288cb
PM
6663 incr linknum
6664 }
97645683
PM
6665}
6666
6667proc setlink {id lk} {
d375ef9b 6668 global curview ctext pendinglinks
97645683 6669
d375ef9b
PM
6670 set known 0
6671 if {[string length $id] < 40} {
6672 set matches [longid $id]
6673 if {[llength $matches] > 0} {
6674 if {[llength $matches] > 1} return
6675 set known 1
6676 set id [lindex $matches 0]
6677 }
6678 } else {
6679 set known [commitinview $id $curview]
6680 }
6681 if {$known} {
97645683 6682 $ctext tag conf $lk -foreground blue -underline 1
d375ef9b 6683 $ctext tag bind $lk <1> [list selbyid $id]
97645683
PM
6684 $ctext tag bind $lk <Enter> {linkcursor %W 1}
6685 $ctext tag bind $lk <Leave> {linkcursor %W -1}
6686 } else {
6687 lappend pendinglinks($id) $lk
d375ef9b 6688 interestedin $id {makelink %P}
97645683
PM
6689 }
6690}
6691
6f63fc18
PM
6692proc appendshortlink {id {pre {}} {post {}}} {
6693 global ctext linknum
6694
6695 $ctext insert end $pre
6696 $ctext tag delete link$linknum
6697 $ctext insert end [string range $id 0 7] link$linknum
6698 $ctext insert end $post
6699 setlink $id link$linknum
6700 incr linknum
6701}
6702
97645683
PM
6703proc makelink {id} {
6704 global pendinglinks
6705
6706 if {![info exists pendinglinks($id)]} return
6707 foreach lk $pendinglinks($id) {
6708 setlink $id $lk
6709 }
6710 unset pendinglinks($id)
6711}
6712
6713proc linkcursor {w inc} {
6714 global linkentercount curtextcursor
6715
6716 if {[incr linkentercount $inc] > 0} {
6717 $w configure -cursor hand2
6718 } else {
6719 $w configure -cursor $curtextcursor
6720 if {$linkentercount < 0} {
6721 set linkentercount 0
6722 }
6723 }
106288cb
PM
6724}
6725
6e5f7203
RN
6726proc viewnextline {dir} {
6727 global canv linespc
6728
6729 $canv delete hover
6730 set ymax [lindex [$canv cget -scrollregion] 3]
6731 set wnow [$canv yview]
6732 set wtop [expr {[lindex $wnow 0] * $ymax}]
6733 set newtop [expr {$wtop + $dir * $linespc}]
6734 if {$newtop < 0} {
6735 set newtop 0
6736 } elseif {$newtop > $ymax} {
6737 set newtop $ymax
6738 }
6739 allcanvs yview moveto [expr {$newtop * 1.0 / $ymax}]
6740}
6741
ef030b85
PM
6742# add a list of tag or branch names at position pos
6743# returns the number of names inserted
e11f1233 6744proc appendrefs {pos ids var} {
7fcc92bf 6745 global ctext linknum curview $var maxrefs
b8ab2e17 6746
ef030b85
PM
6747 if {[catch {$ctext index $pos}]} {
6748 return 0
6749 }
e11f1233
PM
6750 $ctext conf -state normal
6751 $ctext delete $pos "$pos lineend"
6752 set tags {}
6753 foreach id $ids {
6754 foreach tag [set $var\($id\)] {
6755 lappend tags [list $tag $id]
6756 }
6757 }
0a4dd8b8 6758 if {[llength $tags] > $maxrefs} {
84b4b832 6759 $ctext insert $pos "[mc "many"] ([llength $tags])"
0a4dd8b8
PM
6760 } else {
6761 set tags [lsort -index 0 -decreasing $tags]
6762 set sep {}
6763 foreach ti $tags {
6764 set id [lindex $ti 1]
6765 set lk link$linknum
6766 incr linknum
6767 $ctext tag delete $lk
6768 $ctext insert $pos $sep
6769 $ctext insert $pos [lindex $ti 0] $lk
97645683 6770 setlink $id $lk
0a4dd8b8 6771 set sep ", "
b8ab2e17 6772 }
b8ab2e17 6773 }
e11f1233 6774 $ctext conf -state disabled
ef030b85 6775 return [llength $tags]
b8ab2e17
PM
6776}
6777
e11f1233
PM
6778# called when we have finished computing the nearby tags
6779proc dispneartags {delay} {
6780 global selectedline currentid showneartags tagphase
ca6d8f58 6781
94b4a69f 6782 if {$selectedline eq {} || !$showneartags} return
e11f1233
PM
6783 after cancel dispnexttag
6784 if {$delay} {
6785 after 200 dispnexttag
6786 set tagphase -1
6787 } else {
6788 after idle dispnexttag
6789 set tagphase 0
ca6d8f58 6790 }
ca6d8f58
PM
6791}
6792
e11f1233
PM
6793proc dispnexttag {} {
6794 global selectedline currentid showneartags tagphase ctext
b8ab2e17 6795
94b4a69f 6796 if {$selectedline eq {} || !$showneartags} return
e11f1233
PM
6797 switch -- $tagphase {
6798 0 {
6799 set dtags [desctags $currentid]
6800 if {$dtags ne {}} {
6801 appendrefs precedes $dtags idtags
6802 }
6803 }
6804 1 {
6805 set atags [anctags $currentid]
6806 if {$atags ne {}} {
6807 appendrefs follows $atags idtags
6808 }
6809 }
6810 2 {
6811 set dheads [descheads $currentid]
6812 if {$dheads ne {}} {
6813 if {[appendrefs branch $dheads idheads] > 1
6814 && [$ctext get "branch -3c"] eq "h"} {
6815 # turn "Branch" into "Branches"
6816 $ctext conf -state normal
6817 $ctext insert "branch -2c" "es"
6818 $ctext conf -state disabled
6819 }
6820 }
ef030b85
PM
6821 }
6822 }
e11f1233
PM
6823 if {[incr tagphase] <= 2} {
6824 after idle dispnexttag
b8ab2e17 6825 }
b8ab2e17
PM
6826}
6827
28593d3f 6828proc make_secsel {id} {
0380081c
PM
6829 global linehtag linentag linedtag canv canv2 canv3
6830
28593d3f 6831 if {![info exists linehtag($id)]} return
0380081c 6832 $canv delete secsel
28593d3f 6833 set t [eval $canv create rect [$canv bbox $linehtag($id)] -outline {{}} \
0380081c
PM
6834 -tags secsel -fill [$canv cget -selectbackground]]
6835 $canv lower $t
6836 $canv2 delete secsel
28593d3f 6837 set t [eval $canv2 create rect [$canv2 bbox $linentag($id)] -outline {{}} \
0380081c
PM
6838 -tags secsel -fill [$canv2 cget -selectbackground]]
6839 $canv2 lower $t
6840 $canv3 delete secsel
28593d3f 6841 set t [eval $canv3 create rect [$canv3 bbox $linedtag($id)] -outline {{}} \
0380081c
PM
6842 -tags secsel -fill [$canv3 cget -selectbackground]]
6843 $canv3 lower $t
6844}
6845
b9fdba7f
PM
6846proc make_idmark {id} {
6847 global linehtag canv fgcolor
6848
6849 if {![info exists linehtag($id)]} return
6850 $canv delete markid
6851 set t [eval $canv create rect [$canv bbox $linehtag($id)] \
6852 -tags markid -outline $fgcolor]
6853 $canv raise $t
6854}
6855
8a897742 6856proc selectline {l isnew {desired_loc {}}} {
0380081c 6857 global canv ctext commitinfo selectedline
7fcc92bf 6858 global canvy0 linespc parents children curview
7fcceed7 6859 global currentid sha1entry
9f1afe05 6860 global commentend idtags linknum
d94f8cd6 6861 global mergemax numcommits pending_select
e11f1233 6862 global cmitmode showneartags allcommits
c30acc77 6863 global targetrow targetid lastscrollrows
8a897742 6864 global autoselect jump_to_here
d698206c 6865
d94f8cd6 6866 catch {unset pending_select}
84ba7345 6867 $canv delete hover
9843c307 6868 normalline
887c996e 6869 unsel_reflist
bb3edc8b 6870 stopfinding
8f7d0cec 6871 if {$l < 0 || $l >= $numcommits} return
ac1276ab
PM
6872 set id [commitonrow $l]
6873 set targetid $id
6874 set targetrow $l
c30acc77
PM
6875 set selectedline $l
6876 set currentid $id
6877 if {$lastscrollrows < $numcommits} {
6878 setcanvscroll
6879 }
ac1276ab 6880
5ad588de 6881 set y [expr {$canvy0 + $l * $linespc}]
17386066 6882 set ymax [lindex [$canv cget -scrollregion] 3]
5842215e
PM
6883 set ytop [expr {$y - $linespc - 1}]
6884 set ybot [expr {$y + $linespc + 1}]
5ad588de 6885 set wnow [$canv yview]
2ed49d54
JH
6886 set wtop [expr {[lindex $wnow 0] * $ymax}]
6887 set wbot [expr {[lindex $wnow 1] * $ymax}]
5842215e
PM
6888 set wh [expr {$wbot - $wtop}]
6889 set newtop $wtop
17386066 6890 if {$ytop < $wtop} {
5842215e
PM
6891 if {$ybot < $wtop} {
6892 set newtop [expr {$y - $wh / 2.0}]
6893 } else {
6894 set newtop $ytop
6895 if {$newtop > $wtop - $linespc} {
6896 set newtop [expr {$wtop - $linespc}]
6897 }
17386066 6898 }
5842215e
PM
6899 } elseif {$ybot > $wbot} {
6900 if {$ytop > $wbot} {
6901 set newtop [expr {$y - $wh / 2.0}]
6902 } else {
6903 set newtop [expr {$ybot - $wh}]
6904 if {$newtop < $wtop + $linespc} {
6905 set newtop [expr {$wtop + $linespc}]
6906 }
17386066 6907 }
5842215e
PM
6908 }
6909 if {$newtop != $wtop} {
6910 if {$newtop < 0} {
6911 set newtop 0
6912 }
2ed49d54 6913 allcanvs yview moveto [expr {$newtop * 1.0 / $ymax}]
9f1afe05 6914 drawvisible
5ad588de 6915 }
d698206c 6916
28593d3f 6917 make_secsel $id
9f1afe05 6918
fa4da7b3 6919 if {$isnew} {
354af6bd 6920 addtohistory [list selbyid $id 0] savecmitpos
d698206c
PM
6921 }
6922
98f350e5
PM
6923 $sha1entry delete 0 end
6924 $sha1entry insert 0 $id
95293b58 6925 if {$autoselect} {
d93f1713 6926 $sha1entry selection range 0 end
95293b58 6927 }
164ff275 6928 rhighlight_sel $id
98f350e5 6929
5ad588de 6930 $ctext conf -state normal
3ea06f9f 6931 clear_ctext
106288cb 6932 set linknum 0
d76afb15
PM
6933 if {![info exists commitinfo($id)]} {
6934 getcommit $id
6935 }
1db95b00 6936 set info $commitinfo($id)
232475d3 6937 set date [formatdate [lindex $info 2]]
d990cedf 6938 $ctext insert end "[mc "Author"]: [lindex $info 1] $date\n"
232475d3 6939 set date [formatdate [lindex $info 4]]
d990cedf 6940 $ctext insert end "[mc "Committer"]: [lindex $info 3] $date\n"
887fe3c4 6941 if {[info exists idtags($id)]} {
d990cedf 6942 $ctext insert end [mc "Tags:"]
887fe3c4
PM
6943 foreach tag $idtags($id) {
6944 $ctext insert end " $tag"
6945 }
6946 $ctext insert end "\n"
6947 }
40b87ff8 6948
f1b86294 6949 set headers {}
7fcc92bf 6950 set olds $parents($curview,$id)
79b2c75e 6951 if {[llength $olds] > 1} {
b77b0278 6952 set np 0
79b2c75e 6953 foreach p $olds {
b77b0278
PM
6954 if {$np >= $mergemax} {
6955 set tag mmax
6956 } else {
6957 set tag m$np
6958 }
d990cedf 6959 $ctext insert end "[mc "Parent"]: " $tag
f1b86294 6960 appendwithlinks [commit_descriptor $p] {}
b77b0278
PM
6961 incr np
6962 }
6963 } else {
79b2c75e 6964 foreach p $olds {
d990cedf 6965 append headers "[mc "Parent"]: [commit_descriptor $p]"
b1ba39e7
LT
6966 }
6967 }
b77b0278 6968
6a90bff1 6969 foreach c $children($curview,$id) {
d990cedf 6970 append headers "[mc "Child"]: [commit_descriptor $c]"
8b192809 6971 }
d698206c
PM
6972
6973 # make anything that looks like a SHA1 ID be a clickable link
f1b86294 6974 appendwithlinks $headers {}
b8ab2e17
PM
6975 if {$showneartags} {
6976 if {![info exists allcommits]} {
6977 getallcommits
6978 }
d990cedf 6979 $ctext insert end "[mc "Branch"]: "
ef030b85
PM
6980 $ctext mark set branch "end -1c"
6981 $ctext mark gravity branch left
d990cedf 6982 $ctext insert end "\n[mc "Follows"]: "
b8ab2e17
PM
6983 $ctext mark set follows "end -1c"
6984 $ctext mark gravity follows left
d990cedf 6985 $ctext insert end "\n[mc "Precedes"]: "
b8ab2e17
PM
6986 $ctext mark set precedes "end -1c"
6987 $ctext mark gravity precedes left
b8ab2e17 6988 $ctext insert end "\n"
e11f1233 6989 dispneartags 1
b8ab2e17
PM
6990 }
6991 $ctext insert end "\n"
43c25074
PM
6992 set comment [lindex $info 5]
6993 if {[string first "\r" $comment] >= 0} {
6994 set comment [string map {"\r" "\n "} $comment]
6995 }
6996 appendwithlinks $comment {comment}
d698206c 6997
df3d83b1 6998 $ctext tag remove found 1.0 end
5ad588de 6999 $ctext conf -state disabled
df3d83b1 7000 set commentend [$ctext index "end - 1c"]
5ad588de 7001
8a897742 7002 set jump_to_here $desired_loc
b007ee20 7003 init_flist [mc "Comments"]
f8b28a40
PM
7004 if {$cmitmode eq "tree"} {
7005 gettree $id
7006 } elseif {[llength $olds] <= 1} {
d327244a 7007 startdiff $id
7b5ff7e7 7008 } else {
7fcc92bf 7009 mergediff $id
3c461ffe
PM
7010 }
7011}
7012
6e5f7203
RN
7013proc selfirstline {} {
7014 unmarkmatches
7015 selectline 0 1
7016}
7017
7018proc sellastline {} {
7019 global numcommits
7020 unmarkmatches
7021 set l [expr {$numcommits - 1}]
7022 selectline $l 1
7023}
7024
3c461ffe
PM
7025proc selnextline {dir} {
7026 global selectedline
bd441de4 7027 focus .
94b4a69f 7028 if {$selectedline eq {}} return
2ed49d54 7029 set l [expr {$selectedline + $dir}]
3c461ffe 7030 unmarkmatches
d698206c
PM
7031 selectline $l 1
7032}
7033
6e5f7203
RN
7034proc selnextpage {dir} {
7035 global canv linespc selectedline numcommits
7036
7037 set lpp [expr {([winfo height $canv] - 2) / $linespc}]
7038 if {$lpp < 1} {
7039 set lpp 1
7040 }
7041 allcanvs yview scroll [expr {$dir * $lpp}] units
e72ee5eb 7042 drawvisible
94b4a69f 7043 if {$selectedline eq {}} return
6e5f7203
RN
7044 set l [expr {$selectedline + $dir * $lpp}]
7045 if {$l < 0} {
7046 set l 0
7047 } elseif {$l >= $numcommits} {
7048 set l [expr $numcommits - 1]
7049 }
7050 unmarkmatches
40b87ff8 7051 selectline $l 1
6e5f7203
RN
7052}
7053
fa4da7b3 7054proc unselectline {} {
50b44ece 7055 global selectedline currentid
fa4da7b3 7056
94b4a69f 7057 set selectedline {}
50b44ece 7058 catch {unset currentid}
fa4da7b3 7059 allcanvs delete secsel
164ff275 7060 rhighlight_none
fa4da7b3
PM
7061}
7062
f8b28a40
PM
7063proc reselectline {} {
7064 global selectedline
7065
94b4a69f 7066 if {$selectedline ne {}} {
f8b28a40
PM
7067 selectline $selectedline 0
7068 }
7069}
7070
354af6bd 7071proc addtohistory {cmd {saveproc {}}} {
2516dae2 7072 global history historyindex curview
fa4da7b3 7073
354af6bd
PM
7074 unset_posvars
7075 save_position
7076 set elt [list $curview $cmd $saveproc {}]
fa4da7b3 7077 if {$historyindex > 0
2516dae2 7078 && [lindex $history [expr {$historyindex - 1}]] == $elt} {
fa4da7b3
PM
7079 return
7080 }
7081
7082 if {$historyindex < [llength $history]} {
2516dae2 7083 set history [lreplace $history $historyindex end $elt]
fa4da7b3 7084 } else {
2516dae2 7085 lappend history $elt
fa4da7b3
PM
7086 }
7087 incr historyindex
7088 if {$historyindex > 1} {
e9937d2a 7089 .tf.bar.leftbut conf -state normal
fa4da7b3 7090 } else {
e9937d2a 7091 .tf.bar.leftbut conf -state disabled
fa4da7b3 7092 }
e9937d2a 7093 .tf.bar.rightbut conf -state disabled
fa4da7b3
PM
7094}
7095
354af6bd
PM
7096# save the scrolling position of the diff display pane
7097proc save_position {} {
7098 global historyindex history
7099
7100 if {$historyindex < 1} return
7101 set hi [expr {$historyindex - 1}]
7102 set fn [lindex $history $hi 2]
7103 if {$fn ne {}} {
7104 lset history $hi 3 [eval $fn]
7105 }
7106}
7107
7108proc unset_posvars {} {
7109 global last_posvars
7110
7111 if {[info exists last_posvars]} {
7112 foreach {var val} $last_posvars {
7113 global $var
7114 catch {unset $var}
7115 }
7116 unset last_posvars
7117 }
7118}
7119
2516dae2 7120proc godo {elt} {
354af6bd 7121 global curview last_posvars
2516dae2
PM
7122
7123 set view [lindex $elt 0]
7124 set cmd [lindex $elt 1]
354af6bd 7125 set pv [lindex $elt 3]
2516dae2
PM
7126 if {$curview != $view} {
7127 showview $view
7128 }
354af6bd
PM
7129 unset_posvars
7130 foreach {var val} $pv {
7131 global $var
7132 set $var $val
7133 }
7134 set last_posvars $pv
2516dae2
PM
7135 eval $cmd
7136}
7137
d698206c
PM
7138proc goback {} {
7139 global history historyindex
bd441de4 7140 focus .
d698206c
PM
7141
7142 if {$historyindex > 1} {
354af6bd 7143 save_position
d698206c 7144 incr historyindex -1
2516dae2 7145 godo [lindex $history [expr {$historyindex - 1}]]
e9937d2a 7146 .tf.bar.rightbut conf -state normal
d698206c
PM
7147 }
7148 if {$historyindex <= 1} {
e9937d2a 7149 .tf.bar.leftbut conf -state disabled
d698206c
PM
7150 }
7151}
7152
7153proc goforw {} {
7154 global history historyindex
bd441de4 7155 focus .
d698206c
PM
7156
7157 if {$historyindex < [llength $history]} {
354af6bd 7158 save_position
fa4da7b3 7159 set cmd [lindex $history $historyindex]
d698206c 7160 incr historyindex
2516dae2 7161 godo $cmd
e9937d2a 7162 .tf.bar.leftbut conf -state normal
d698206c
PM
7163 }
7164 if {$historyindex >= [llength $history]} {
e9937d2a 7165 .tf.bar.rightbut conf -state disabled
d698206c 7166 }
e2ed4324
PM
7167}
7168
f8b28a40 7169proc gettree {id} {
8f489363
PM
7170 global treefilelist treeidlist diffids diffmergeid treepending
7171 global nullid nullid2
f8b28a40
PM
7172
7173 set diffids $id
7174 catch {unset diffmergeid}
7175 if {![info exists treefilelist($id)]} {
7176 if {![info exists treepending]} {
8f489363
PM
7177 if {$id eq $nullid} {
7178 set cmd [list | git ls-files]
7179 } elseif {$id eq $nullid2} {
7180 set cmd [list | git ls-files --stage -t]
219ea3a9 7181 } else {
8f489363 7182 set cmd [list | git ls-tree -r $id]
219ea3a9
PM
7183 }
7184 if {[catch {set gtf [open $cmd r]}]} {
f8b28a40
PM
7185 return
7186 }
7187 set treepending $id
7188 set treefilelist($id) {}
7189 set treeidlist($id) {}
09c7029d 7190 fconfigure $gtf -blocking 0 -encoding binary
7eb3cb9c 7191 filerun $gtf [list gettreeline $gtf $id]
f8b28a40
PM
7192 }
7193 } else {
7194 setfilelist $id
7195 }
7196}
7197
7198proc gettreeline {gtf id} {
8f489363 7199 global treefilelist treeidlist treepending cmitmode diffids nullid nullid2
f8b28a40 7200
7eb3cb9c
PM
7201 set nl 0
7202 while {[incr nl] <= 1000 && [gets $gtf line] >= 0} {
8f489363
PM
7203 if {$diffids eq $nullid} {
7204 set fname $line
7205 } else {
9396cd38
PM
7206 set i [string first "\t" $line]
7207 if {$i < 0} continue
9396cd38 7208 set fname [string range $line [expr {$i+1}] end]
f31fa2c0
PM
7209 set line [string range $line 0 [expr {$i-1}]]
7210 if {$diffids ne $nullid2 && [lindex $line 1] ne "blob"} continue
7211 set sha1 [lindex $line 2]
219ea3a9 7212 lappend treeidlist($id) $sha1
219ea3a9 7213 }
09c7029d
AG
7214 if {[string index $fname 0] eq "\""} {
7215 set fname [lindex $fname 0]
7216 }
7217 set fname [encoding convertfrom $fname]
7eb3cb9c
PM
7218 lappend treefilelist($id) $fname
7219 }
7220 if {![eof $gtf]} {
7221 return [expr {$nl >= 1000? 2: 1}]
f8b28a40 7222 }
f8b28a40
PM
7223 close $gtf
7224 unset treepending
7225 if {$cmitmode ne "tree"} {
7226 if {![info exists diffmergeid]} {
7227 gettreediffs $diffids
7228 }
7229 } elseif {$id ne $diffids} {
7230 gettree $diffids
7231 } else {
7232 setfilelist $id
7233 }
7eb3cb9c 7234 return 0
f8b28a40
PM
7235}
7236
7237proc showfile {f} {
8f489363 7238 global treefilelist treeidlist diffids nullid nullid2
7cdc3556 7239 global ctext_file_names ctext_file_lines
f8b28a40
PM
7240 global ctext commentend
7241
7242 set i [lsearch -exact $treefilelist($diffids) $f]
7243 if {$i < 0} {
7244 puts "oops, $f not in list for id $diffids"
7245 return
7246 }
8f489363
PM
7247 if {$diffids eq $nullid} {
7248 if {[catch {set bf [open $f r]} err]} {
7249 puts "oops, can't read $f: $err"
219ea3a9
PM
7250 return
7251 }
7252 } else {
8f489363
PM
7253 set blob [lindex $treeidlist($diffids) $i]
7254 if {[catch {set bf [open [concat | git cat-file blob $blob] r]} err]} {
7255 puts "oops, error reading blob $blob: $err"
219ea3a9
PM
7256 return
7257 }
f8b28a40 7258 }
09c7029d 7259 fconfigure $bf -blocking 0 -encoding [get_path_encoding $f]
7eb3cb9c 7260 filerun $bf [list getblobline $bf $diffids]
f8b28a40 7261 $ctext config -state normal
3ea06f9f 7262 clear_ctext $commentend
7cdc3556
AG
7263 lappend ctext_file_names $f
7264 lappend ctext_file_lines [lindex [split $commentend "."] 0]
f8b28a40
PM
7265 $ctext insert end "\n"
7266 $ctext insert end "$f\n" filesep
7267 $ctext config -state disabled
7268 $ctext yview $commentend
32f1b3e4 7269 settabs 0
f8b28a40
PM
7270}
7271
7272proc getblobline {bf id} {
7273 global diffids cmitmode ctext
7274
7275 if {$id ne $diffids || $cmitmode ne "tree"} {
7276 catch {close $bf}
7eb3cb9c 7277 return 0
f8b28a40
PM
7278 }
7279 $ctext config -state normal
7eb3cb9c
PM
7280 set nl 0
7281 while {[incr nl] <= 1000 && [gets $bf line] >= 0} {
f8b28a40
PM
7282 $ctext insert end "$line\n"
7283 }
7284 if {[eof $bf]} {
8a897742
PM
7285 global jump_to_here ctext_file_names commentend
7286
f8b28a40
PM
7287 # delete last newline
7288 $ctext delete "end - 2c" "end - 1c"
7289 close $bf
8a897742
PM
7290 if {$jump_to_here ne {} &&
7291 [lindex $jump_to_here 0] eq [lindex $ctext_file_names 0]} {
7292 set lnum [expr {[lindex $jump_to_here 1] +
7293 [lindex [split $commentend .] 0]}]
7294 mark_ctext_line $lnum
7295 }
7eb3cb9c 7296 return 0
f8b28a40
PM
7297 }
7298 $ctext config -state disabled
7eb3cb9c 7299 return [expr {$nl >= 1000? 2: 1}]
f8b28a40
PM
7300}
7301
8a897742 7302proc mark_ctext_line {lnum} {
e3e901be 7303 global ctext markbgcolor
8a897742
PM
7304
7305 $ctext tag delete omark
7306 $ctext tag add omark $lnum.0 "$lnum.0 + 1 line"
e3e901be 7307 $ctext tag conf omark -background $markbgcolor
8a897742
PM
7308 $ctext see $lnum.0
7309}
7310
7fcc92bf 7311proc mergediff {id} {
8b07dca1 7312 global diffmergeid
2df6442f 7313 global diffids treediffs
8b07dca1 7314 global parents curview
e2ed4324 7315
3c461ffe 7316 set diffmergeid $id
7a1d9d14 7317 set diffids $id
2df6442f 7318 set treediffs($id) {}
7fcc92bf 7319 set np [llength $parents($curview,$id)]
32f1b3e4 7320 settabs $np
8b07dca1 7321 getblobdiffs $id
c8a4acbf
PM
7322}
7323
3c461ffe 7324proc startdiff {ids} {
8f489363 7325 global treediffs diffids treepending diffmergeid nullid nullid2
c8dfbcf9 7326
32f1b3e4 7327 settabs 1
4f2c2642 7328 set diffids $ids
3c461ffe 7329 catch {unset diffmergeid}
8f489363
PM
7330 if {![info exists treediffs($ids)] ||
7331 [lsearch -exact $ids $nullid] >= 0 ||
7332 [lsearch -exact $ids $nullid2] >= 0} {
c8dfbcf9 7333 if {![info exists treepending]} {
14c9dbd6 7334 gettreediffs $ids
c8dfbcf9
PM
7335 }
7336 } else {
14c9dbd6 7337 addtocflist $ids
c8dfbcf9
PM
7338 }
7339}
7340
7a39a17a
PM
7341proc path_filter {filter name} {
7342 foreach p $filter {
7343 set l [string length $p]
74a40c71
PM
7344 if {[string index $p end] eq "/"} {
7345 if {[string compare -length $l $p $name] == 0} {
7346 return 1
7347 }
7348 } else {
7349 if {[string compare -length $l $p $name] == 0 &&
7350 ([string length $name] == $l ||
7351 [string index $name $l] eq "/")} {
7352 return 1
7353 }
7a39a17a
PM
7354 }
7355 }
7356 return 0
7357}
7358
c8dfbcf9 7359proc addtocflist {ids} {
74a40c71 7360 global treediffs
7a39a17a 7361
74a40c71 7362 add_flist $treediffs($ids)
c8dfbcf9 7363 getblobdiffs $ids
d2610d11
PM
7364}
7365
219ea3a9 7366proc diffcmd {ids flags} {
8f489363 7367 global nullid nullid2
219ea3a9
PM
7368
7369 set i [lsearch -exact $ids $nullid]
8f489363 7370 set j [lsearch -exact $ids $nullid2]
219ea3a9 7371 if {$i >= 0} {
8f489363
PM
7372 if {[llength $ids] > 1 && $j < 0} {
7373 # comparing working directory with some specific revision
7374 set cmd [concat | git diff-index $flags]
7375 if {$i == 0} {
7376 lappend cmd -R [lindex $ids 1]
7377 } else {
7378 lappend cmd [lindex $ids 0]
7379 }
7380 } else {
7381 # comparing working directory with index
7382 set cmd [concat | git diff-files $flags]
7383 if {$j == 1} {
7384 lappend cmd -R
7385 }
7386 }
7387 } elseif {$j >= 0} {
7388 set cmd [concat | git diff-index --cached $flags]
219ea3a9 7389 if {[llength $ids] > 1} {
8f489363 7390 # comparing index with specific revision
90a77925 7391 if {$j == 0} {
219ea3a9
PM
7392 lappend cmd -R [lindex $ids 1]
7393 } else {
7394 lappend cmd [lindex $ids 0]
7395 }
7396 } else {
8f489363 7397 # comparing index with HEAD
219ea3a9
PM
7398 lappend cmd HEAD
7399 }
7400 } else {
8f489363 7401 set cmd [concat | git diff-tree -r $flags $ids]
219ea3a9
PM
7402 }
7403 return $cmd
7404}
7405
c8dfbcf9 7406proc gettreediffs {ids} {
79b2c75e 7407 global treediff treepending
219ea3a9 7408
7272131b
AG
7409 if {[catch {set gdtf [open [diffcmd $ids {--no-commit-id}] r]}]} return
7410
c8dfbcf9 7411 set treepending $ids
3c461ffe 7412 set treediff {}
09c7029d 7413 fconfigure $gdtf -blocking 0 -encoding binary
7eb3cb9c 7414 filerun $gdtf [list gettreediffline $gdtf $ids]
d2610d11
PM
7415}
7416
c8dfbcf9 7417proc gettreediffline {gdtf ids} {
3c461ffe 7418 global treediff treediffs treepending diffids diffmergeid
39ee47ef 7419 global cmitmode vfilelimit curview limitdiffs perfile_attrs
3c461ffe 7420
7eb3cb9c 7421 set nr 0
4db09304 7422 set sublist {}
39ee47ef
PM
7423 set max 1000
7424 if {$perfile_attrs} {
7425 # cache_gitattr is slow, and even slower on win32 where we
7426 # have to invoke it for only about 30 paths at a time
7427 set max 500
7428 if {[tk windowingsystem] == "win32"} {
7429 set max 120
7430 }
7431 }
7432 while {[incr nr] <= $max && [gets $gdtf line] >= 0} {
9396cd38
PM
7433 set i [string first "\t" $line]
7434 if {$i >= 0} {
7435 set file [string range $line [expr {$i+1}] end]
7436 if {[string index $file 0] eq "\""} {
7437 set file [lindex $file 0]
7438 }
09c7029d 7439 set file [encoding convertfrom $file]
48a81b7c
PM
7440 if {$file ne [lindex $treediff end]} {
7441 lappend treediff $file
7442 lappend sublist $file
7443 }
9396cd38 7444 }
7eb3cb9c 7445 }
39ee47ef
PM
7446 if {$perfile_attrs} {
7447 cache_gitattr encoding $sublist
7448 }
7eb3cb9c 7449 if {![eof $gdtf]} {
39ee47ef 7450 return [expr {$nr >= $max? 2: 1}]
7eb3cb9c
PM
7451 }
7452 close $gdtf
3ed31a81 7453 if {$limitdiffs && $vfilelimit($curview) ne {}} {
74a40c71
PM
7454 set flist {}
7455 foreach f $treediff {
3ed31a81 7456 if {[path_filter $vfilelimit($curview) $f]} {
74a40c71
PM
7457 lappend flist $f
7458 }
7459 }
7460 set treediffs($ids) $flist
7461 } else {
7462 set treediffs($ids) $treediff
7463 }
7eb3cb9c 7464 unset treepending
e1160138 7465 if {$cmitmode eq "tree" && [llength $diffids] == 1} {
7eb3cb9c
PM
7466 gettree $diffids
7467 } elseif {$ids != $diffids} {
7468 if {![info exists diffmergeid]} {
7469 gettreediffs $diffids
b74fd579 7470 }
7eb3cb9c
PM
7471 } else {
7472 addtocflist $ids
d2610d11 7473 }
7eb3cb9c 7474 return 0
d2610d11
PM
7475}
7476
890fae70
SP
7477# empty string or positive integer
7478proc diffcontextvalidate {v} {
7479 return [regexp {^(|[1-9][0-9]*)$} $v]
7480}
7481
7482proc diffcontextchange {n1 n2 op} {
7483 global diffcontextstring diffcontext
7484
7485 if {[string is integer -strict $diffcontextstring]} {
a41ddbb6 7486 if {$diffcontextstring >= 0} {
890fae70
SP
7487 set diffcontext $diffcontextstring
7488 reselectline
7489 }
7490 }
7491}
7492
b9b86007
SP
7493proc changeignorespace {} {
7494 reselectline
7495}
7496
c8dfbcf9 7497proc getblobdiffs {ids} {
8d73b242 7498 global blobdifffd diffids env
7eb3cb9c 7499 global diffinhdr treediffs
890fae70 7500 global diffcontext
b9b86007 7501 global ignorespace
3ed31a81 7502 global limitdiffs vfilelimit curview
8b07dca1 7503 global diffencoding targetline diffnparents
a8138733 7504 global git_version
c8dfbcf9 7505
a8138733
PM
7506 set textconv {}
7507 if {[package vcompare $git_version "1.6.1"] >= 0} {
7508 set textconv "--textconv"
7509 }
5c838d23
JL
7510 set submodule {}
7511 if {[package vcompare $git_version "1.6.6"] >= 0} {
7512 set submodule "--submodule"
7513 }
7514 set cmd [diffcmd $ids "-p $textconv $submodule -C --cc --no-commit-id -U$diffcontext"]
b9b86007
SP
7515 if {$ignorespace} {
7516 append cmd " -w"
7517 }
3ed31a81
PM
7518 if {$limitdiffs && $vfilelimit($curview) ne {}} {
7519 set cmd [concat $cmd -- $vfilelimit($curview)]
7a39a17a
PM
7520 }
7521 if {[catch {set bdf [open $cmd r]} err]} {
8b07dca1 7522 error_popup [mc "Error getting diffs: %s" $err]
e5c2d856
PM
7523 return
7524 }
8a897742 7525 set targetline {}
8b07dca1 7526 set diffnparents 0
4f2c2642 7527 set diffinhdr 0
09c7029d 7528 set diffencoding [get_path_encoding {}]
681c3290 7529 fconfigure $bdf -blocking 0 -encoding binary -eofchar {}
c8dfbcf9 7530 set blobdifffd($ids) $bdf
7eb3cb9c 7531 filerun $bdf [list getblobdiffline $bdf $diffids]
e5c2d856
PM
7532}
7533
354af6bd
PM
7534proc savecmitpos {} {
7535 global ctext cmitmode
7536
7537 if {$cmitmode eq "tree"} {
7538 return {}
7539 }
7540 return [list target_scrollpos [$ctext index @0,0]]
7541}
7542
7543proc savectextpos {} {
7544 global ctext
7545
7546 return [list target_scrollpos [$ctext index @0,0]]
7547}
7548
7549proc maybe_scroll_ctext {ateof} {
7550 global ctext target_scrollpos
7551
7552 if {![info exists target_scrollpos]} return
7553 if {!$ateof} {
7554 set nlines [expr {[winfo height $ctext]
7555 / [font metrics textfont -linespace]}]
7556 if {[$ctext compare "$target_scrollpos + $nlines lines" <= end]} return
7557 }
7558 $ctext yview $target_scrollpos
7559 unset target_scrollpos
7560}
7561
89b11d3b
PM
7562proc setinlist {var i val} {
7563 global $var
7564
7565 while {[llength [set $var]] < $i} {
7566 lappend $var {}
7567 }
7568 if {[llength [set $var]] == $i} {
7569 lappend $var $val
7570 } else {
7571 lset $var $i $val
7572 }
7573}
7574
9396cd38 7575proc makediffhdr {fname ids} {
8b07dca1 7576 global ctext curdiffstart treediffs diffencoding
8a897742 7577 global ctext_file_names jump_to_here targetline diffline
9396cd38 7578
8b07dca1
PM
7579 set fname [encoding convertfrom $fname]
7580 set diffencoding [get_path_encoding $fname]
9396cd38
PM
7581 set i [lsearch -exact $treediffs($ids) $fname]
7582 if {$i >= 0} {
7583 setinlist difffilestart $i $curdiffstart
7584 }
48a81b7c 7585 lset ctext_file_names end $fname
9396cd38
PM
7586 set l [expr {(78 - [string length $fname]) / 2}]
7587 set pad [string range "----------------------------------------" 1 $l]
7588 $ctext insert $curdiffstart "$pad $fname $pad" filesep
8a897742
PM
7589 set targetline {}
7590 if {$jump_to_here ne {} && [lindex $jump_to_here 0] eq $fname} {
7591 set targetline [lindex $jump_to_here 1]
7592 }
7593 set diffline 0
9396cd38
PM
7594}
7595
c8dfbcf9 7596proc getblobdiffline {bdf ids} {
9396cd38 7597 global diffids blobdifffd ctext curdiffstart
7eab2933 7598 global diffnexthead diffnextnote difffilestart
7cdc3556 7599 global ctext_file_names ctext_file_lines
8b07dca1 7600 global diffinhdr treediffs mergemax diffnparents
8a897742 7601 global diffencoding jump_to_here targetline diffline
c8dfbcf9 7602
7eb3cb9c 7603 set nr 0
e5c2d856 7604 $ctext conf -state normal
7eb3cb9c
PM
7605 while {[incr nr] <= 1000 && [gets $bdf line] >= 0} {
7606 if {$ids != $diffids || $bdf != $blobdifffd($ids)} {
c21398be 7607 catch {close $bdf}
7eb3cb9c 7608 return 0
89b11d3b 7609 }
8b07dca1
PM
7610 if {![string compare -length 5 "diff " $line]} {
7611 if {![regexp {^diff (--cc|--git) } $line m type]} {
7612 set line [encoding convertfrom $line]
7613 $ctext insert end "$line\n" hunksep
7614 continue
7615 }
7eb3cb9c 7616 # start of a new file
8b07dca1 7617 set diffinhdr 1
7eb3cb9c 7618 $ctext insert end "\n"
9396cd38 7619 set curdiffstart [$ctext index "end - 1c"]
7cdc3556
AG
7620 lappend ctext_file_names ""
7621 lappend ctext_file_lines [lindex [split $curdiffstart "."] 0]
9396cd38 7622 $ctext insert end "\n" filesep
8b07dca1
PM
7623
7624 if {$type eq "--cc"} {
7625 # start of a new file in a merge diff
7626 set fname [string range $line 10 end]
7627 if {[lsearch -exact $treediffs($ids) $fname] < 0} {
7628 lappend treediffs($ids) $fname
7629 add_flist [list $fname]
7630 }
7631
9396cd38 7632 } else {
8b07dca1
PM
7633 set line [string range $line 11 end]
7634 # If the name hasn't changed the length will be odd,
7635 # the middle char will be a space, and the two bits either
7636 # side will be a/name and b/name, or "a/name" and "b/name".
7637 # If the name has changed we'll get "rename from" and
7638 # "rename to" or "copy from" and "copy to" lines following
7639 # this, and we'll use them to get the filenames.
7640 # This complexity is necessary because spaces in the
7641 # filename(s) don't get escaped.
7642 set l [string length $line]
7643 set i [expr {$l / 2}]
7644 if {!(($l & 1) && [string index $line $i] eq " " &&
7645 [string range $line 2 [expr {$i - 1}]] eq \
7646 [string range $line [expr {$i + 3}] end])} {
7647 continue
7648 }
7649 # unescape if quoted and chop off the a/ from the front
7650 if {[string index $line 0] eq "\""} {
7651 set fname [string range [lindex $line 0] 2 end]
7652 } else {
7653 set fname [string range $line 2 [expr {$i - 1}]]
7654 }
7eb3cb9c 7655 }
9396cd38
PM
7656 makediffhdr $fname $ids
7657
48a81b7c
PM
7658 } elseif {![string compare -length 16 "* Unmerged path " $line]} {
7659 set fname [encoding convertfrom [string range $line 16 end]]
7660 $ctext insert end "\n"
7661 set curdiffstart [$ctext index "end - 1c"]
7662 lappend ctext_file_names $fname
7663 lappend ctext_file_lines [lindex [split $curdiffstart "."] 0]
7664 $ctext insert end "$line\n" filesep
7665 set i [lsearch -exact $treediffs($ids) $fname]
7666 if {$i >= 0} {
7667 setinlist difffilestart $i $curdiffstart
7668 }
7669
8b07dca1
PM
7670 } elseif {![string compare -length 2 "@@" $line]} {
7671 regexp {^@@+} $line ats
09c7029d 7672 set line [encoding convertfrom $diffencoding $line]
7eb3cb9c 7673 $ctext insert end "$line\n" hunksep
8b07dca1
PM
7674 if {[regexp { \+(\d+),\d+ @@} $line m nl]} {
7675 set diffline $nl
7676 }
7677 set diffnparents [expr {[string length $ats] - 1}]
7eb3cb9c 7678 set diffinhdr 0
9396cd38 7679
5c838d23
JL
7680 } elseif {![string compare -length 10 "Submodule " $line]} {
7681 # start of a new submodule
7682 if {[string compare [$ctext get "end - 4c" end] "\n \n\n"]} {
7683 $ctext insert end "\n"; # Add newline after commit message
7684 }
7685 set curdiffstart [$ctext index "end - 1c"]
7686 lappend ctext_file_names ""
7687 set fname [string range $line 10 [expr [string last " " $line] - 1]]
7688 lappend ctext_file_lines $fname
7689 makediffhdr $fname $ids
7690 $ctext insert end "\n$line\n" filesep
7691 } elseif {![string compare -length 3 " >" $line]} {
1f2cecfd 7692 set line [encoding convertfrom $diffencoding $line]
5c838d23
JL
7693 $ctext insert end "$line\n" dresult
7694 } elseif {![string compare -length 3 " <" $line]} {
1f2cecfd 7695 set line [encoding convertfrom $diffencoding $line]
5c838d23 7696 $ctext insert end "$line\n" d0
9396cd38 7697 } elseif {$diffinhdr} {
5e85ec4c 7698 if {![string compare -length 12 "rename from " $line]} {
d1cb298b 7699 set fname [string range $line [expr 6 + [string first " from " $line] ] end]
9396cd38
PM
7700 if {[string index $fname 0] eq "\""} {
7701 set fname [lindex $fname 0]
7702 }
09c7029d 7703 set fname [encoding convertfrom $fname]
9396cd38
PM
7704 set i [lsearch -exact $treediffs($ids) $fname]
7705 if {$i >= 0} {
7706 setinlist difffilestart $i $curdiffstart
7707 }
d1cb298b
JS
7708 } elseif {![string compare -length 10 $line "rename to "] ||
7709 ![string compare -length 8 $line "copy to "]} {
7710 set fname [string range $line [expr 4 + [string first " to " $line] ] end]
9396cd38
PM
7711 if {[string index $fname 0] eq "\""} {
7712 set fname [lindex $fname 0]
7713 }
7714 makediffhdr $fname $ids
7715 } elseif {[string compare -length 3 $line "---"] == 0} {
7716 # do nothing
7717 continue
7718 } elseif {[string compare -length 3 $line "+++"] == 0} {
7719 set diffinhdr 0
7720 continue
7721 }
7722 $ctext insert end "$line\n" filesep
7723
e5c2d856 7724 } else {
681c3290
PT
7725 set line [string map {\x1A ^Z} \
7726 [encoding convertfrom $diffencoding $line]]
8b07dca1
PM
7727 # parse the prefix - one ' ', '-' or '+' for each parent
7728 set prefix [string range $line 0 [expr {$diffnparents - 1}]]
7729 set tag [expr {$diffnparents > 1? "m": "d"}]
7730 if {[string trim $prefix " -+"] eq {}} {
7731 # prefix only has " ", "-" and "+" in it: normal diff line
7732 set num [string first "-" $prefix]
7733 if {$num >= 0} {
7734 # removed line, first parent with line is $num
7735 if {$num >= $mergemax} {
7736 set num "max"
7737 }
7738 $ctext insert end "$line\n" $tag$num
7739 } else {
7740 set tags {}
7741 if {[string first "+" $prefix] >= 0} {
7742 # added line
7743 lappend tags ${tag}result
7744 if {$diffnparents > 1} {
7745 set num [string first " " $prefix]
7746 if {$num >= 0} {
7747 if {$num >= $mergemax} {
7748 set num "max"
7749 }
7750 lappend tags m$num
7751 }
7752 }
7753 }
7754 if {$targetline ne {}} {
7755 if {$diffline == $targetline} {
7756 set seehere [$ctext index "end - 1 chars"]
7757 set targetline {}
7758 } else {
7759 incr diffline
7760 }
7761 }
7762 $ctext insert end "$line\n" $tags
7763 }
7eb3cb9c 7764 } else {
9396cd38
PM
7765 # "\ No newline at end of file",
7766 # or something else we don't recognize
7767 $ctext insert end "$line\n" hunksep
e5c2d856 7768 }
e5c2d856
PM
7769 }
7770 }
8b07dca1
PM
7771 if {[info exists seehere]} {
7772 mark_ctext_line [lindex [split $seehere .] 0]
7773 }
354af6bd 7774 maybe_scroll_ctext [eof $bdf]
e5c2d856 7775 $ctext conf -state disabled
7eb3cb9c 7776 if {[eof $bdf]} {
c21398be 7777 catch {close $bdf}
7eb3cb9c 7778 return 0
c8dfbcf9 7779 }
7eb3cb9c 7780 return [expr {$nr >= 1000? 2: 1}]
e5c2d856
PM
7781}
7782
a8d610a2
PM
7783proc changediffdisp {} {
7784 global ctext diffelide
7785
7786 $ctext tag conf d0 -elide [lindex $diffelide 0]
8b07dca1 7787 $ctext tag conf dresult -elide [lindex $diffelide 1]
a8d610a2
PM
7788}
7789
f4c54b3c
PM
7790proc highlightfile {loc cline} {
7791 global ctext cflist cflist_top
7792
7793 $ctext yview $loc
7794 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
7795 $cflist tag add highlight $cline.0 "$cline.0 lineend"
7796 $cflist see $cline.0
7797 set cflist_top $cline
7798}
7799
67c22874 7800proc prevfile {} {
f4c54b3c
PM
7801 global difffilestart ctext cmitmode
7802
7803 if {$cmitmode eq "tree"} return
7804 set prev 0.0
7805 set prevline 1
67c22874
OH
7806 set here [$ctext index @0,0]
7807 foreach loc $difffilestart {
7808 if {[$ctext compare $loc >= $here]} {
f4c54b3c 7809 highlightfile $prev $prevline
67c22874
OH
7810 return
7811 }
7812 set prev $loc
f4c54b3c 7813 incr prevline
67c22874 7814 }
f4c54b3c 7815 highlightfile $prev $prevline
67c22874
OH
7816}
7817
39ad8570 7818proc nextfile {} {
f4c54b3c
PM
7819 global difffilestart ctext cmitmode
7820
7821 if {$cmitmode eq "tree"} return
39ad8570 7822 set here [$ctext index @0,0]
f4c54b3c 7823 set line 1
7fcceed7 7824 foreach loc $difffilestart {
f4c54b3c 7825 incr line
7fcceed7 7826 if {[$ctext compare $loc > $here]} {
f4c54b3c 7827 highlightfile $loc $line
67c22874 7828 return
39ad8570
PM
7829 }
7830 }
1db95b00
PM
7831}
7832
3ea06f9f
PM
7833proc clear_ctext {{first 1.0}} {
7834 global ctext smarktop smarkbot
7cdc3556 7835 global ctext_file_names ctext_file_lines
97645683 7836 global pendinglinks
3ea06f9f 7837
1902c270
PM
7838 set l [lindex [split $first .] 0]
7839 if {![info exists smarktop] || [$ctext compare $first < $smarktop.0]} {
7840 set smarktop $l
3ea06f9f 7841 }
1902c270
PM
7842 if {![info exists smarkbot] || [$ctext compare $first < $smarkbot.0]} {
7843 set smarkbot $l
3ea06f9f
PM
7844 }
7845 $ctext delete $first end
97645683
PM
7846 if {$first eq "1.0"} {
7847 catch {unset pendinglinks}
7848 }
7cdc3556
AG
7849 set ctext_file_names {}
7850 set ctext_file_lines {}
3ea06f9f
PM
7851}
7852
32f1b3e4 7853proc settabs {{firstab {}}} {
9c311b32 7854 global firsttabstop tabstop ctext have_tk85
32f1b3e4
PM
7855
7856 if {$firstab ne {} && $have_tk85} {
7857 set firsttabstop $firstab
7858 }
9c311b32 7859 set w [font measure textfont "0"]
32f1b3e4 7860 if {$firsttabstop != 0} {
64b5f146
PM
7861 $ctext conf -tabs [list [expr {($firsttabstop + $tabstop) * $w}] \
7862 [expr {($firsttabstop + 2 * $tabstop) * $w}]]
32f1b3e4
PM
7863 } elseif {$have_tk85 || $tabstop != 8} {
7864 $ctext conf -tabs [expr {$tabstop * $w}]
7865 } else {
7866 $ctext conf -tabs {}
7867 }
3ea06f9f
PM
7868}
7869
7870proc incrsearch {name ix op} {
1902c270 7871 global ctext searchstring searchdirn
3ea06f9f
PM
7872
7873 $ctext tag remove found 1.0 end
1902c270
PM
7874 if {[catch {$ctext index anchor}]} {
7875 # no anchor set, use start of selection, or of visible area
7876 set sel [$ctext tag ranges sel]
7877 if {$sel ne {}} {
7878 $ctext mark set anchor [lindex $sel 0]
7879 } elseif {$searchdirn eq "-forwards"} {
7880 $ctext mark set anchor @0,0
7881 } else {
7882 $ctext mark set anchor @0,[winfo height $ctext]
7883 }
7884 }
3ea06f9f 7885 if {$searchstring ne {}} {
1902c270
PM
7886 set here [$ctext search $searchdirn -- $searchstring anchor]
7887 if {$here ne {}} {
7888 $ctext see $here
7889 }
3ea06f9f
PM
7890 searchmarkvisible 1
7891 }
7892}
7893
7894proc dosearch {} {
1902c270 7895 global sstring ctext searchstring searchdirn
3ea06f9f
PM
7896
7897 focus $sstring
7898 $sstring icursor end
1902c270
PM
7899 set searchdirn -forwards
7900 if {$searchstring ne {}} {
7901 set sel [$ctext tag ranges sel]
7902 if {$sel ne {}} {
7903 set start "[lindex $sel 0] + 1c"
7904 } elseif {[catch {set start [$ctext index anchor]}]} {
7905 set start "@0,0"
7906 }
7907 set match [$ctext search -count mlen -- $searchstring $start]
7908 $ctext tag remove sel 1.0 end
7909 if {$match eq {}} {
7910 bell
7911 return
7912 }
7913 $ctext see $match
7914 set mend "$match + $mlen c"
7915 $ctext tag add sel $match $mend
7916 $ctext mark unset anchor
7917 }
7918}
7919
7920proc dosearchback {} {
7921 global sstring ctext searchstring searchdirn
7922
7923 focus $sstring
7924 $sstring icursor end
7925 set searchdirn -backwards
7926 if {$searchstring ne {}} {
7927 set sel [$ctext tag ranges sel]
7928 if {$sel ne {}} {
7929 set start [lindex $sel 0]
7930 } elseif {[catch {set start [$ctext index anchor]}]} {
7931 set start @0,[winfo height $ctext]
7932 }
7933 set match [$ctext search -backwards -count ml -- $searchstring $start]
7934 $ctext tag remove sel 1.0 end
7935 if {$match eq {}} {
7936 bell
7937 return
7938 }
7939 $ctext see $match
7940 set mend "$match + $ml c"
7941 $ctext tag add sel $match $mend
7942 $ctext mark unset anchor
3ea06f9f 7943 }
3ea06f9f
PM
7944}
7945
7946proc searchmark {first last} {
7947 global ctext searchstring
7948
7949 set mend $first.0
7950 while {1} {
7951 set match [$ctext search -count mlen -- $searchstring $mend $last.end]
7952 if {$match eq {}} break
7953 set mend "$match + $mlen c"
7954 $ctext tag add found $match $mend
7955 }
7956}
7957
7958proc searchmarkvisible {doall} {
7959 global ctext smarktop smarkbot
7960
7961 set topline [lindex [split [$ctext index @0,0] .] 0]
7962 set botline [lindex [split [$ctext index @0,[winfo height $ctext]] .] 0]
7963 if {$doall || $botline < $smarktop || $topline > $smarkbot} {
7964 # no overlap with previous
7965 searchmark $topline $botline
7966 set smarktop $topline
7967 set smarkbot $botline
7968 } else {
7969 if {$topline < $smarktop} {
7970 searchmark $topline [expr {$smarktop-1}]
7971 set smarktop $topline
7972 }
7973 if {$botline > $smarkbot} {
7974 searchmark [expr {$smarkbot+1}] $botline
7975 set smarkbot $botline
7976 }
7977 }
7978}
7979
7980proc scrolltext {f0 f1} {
1902c270 7981 global searchstring
3ea06f9f 7982
8809d691 7983 .bleft.bottom.sb set $f0 $f1
3ea06f9f
PM
7984 if {$searchstring ne {}} {
7985 searchmarkvisible 0
7986 }
7987}
7988
1d10f36d 7989proc setcoords {} {
9c311b32 7990 global linespc charspc canvx0 canvy0
f6075eba 7991 global xspc1 xspc2 lthickness
8d858d1a 7992
9c311b32
PM
7993 set linespc [font metrics mainfont -linespace]
7994 set charspc [font measure mainfont "m"]
9f1afe05
PM
7995 set canvy0 [expr {int(3 + 0.5 * $linespc)}]
7996 set canvx0 [expr {int(3 + 0.5 * $linespc)}]
f6075eba 7997 set lthickness [expr {int($linespc / 9) + 1}]
8d858d1a
PM
7998 set xspc1(0) $linespc
7999 set xspc2 $linespc
9a40c50c 8000}
1db95b00 8001
1d10f36d 8002proc redisplay {} {
be0cd098 8003 global canv
9f1afe05
PM
8004 global selectedline
8005
8006 set ymax [lindex [$canv cget -scrollregion] 3]
8007 if {$ymax eq {} || $ymax == 0} return
8008 set span [$canv yview]
8009 clear_display
be0cd098 8010 setcanvscroll
9f1afe05
PM
8011 allcanvs yview moveto [lindex $span 0]
8012 drawvisible
94b4a69f 8013 if {$selectedline ne {}} {
9f1afe05 8014 selectline $selectedline 0
ca6d8f58 8015 allcanvs yview moveto [lindex $span 0]
1d10f36d
PM
8016 }
8017}
8018
0ed1dd3c
PM
8019proc parsefont {f n} {
8020 global fontattr
8021
8022 set fontattr($f,family) [lindex $n 0]
8023 set s [lindex $n 1]
8024 if {$s eq {} || $s == 0} {
8025 set s 10
8026 } elseif {$s < 0} {
8027 set s [expr {int(-$s / [winfo fpixels . 1p] + 0.5)}]
9c311b32 8028 }
0ed1dd3c
PM
8029 set fontattr($f,size) $s
8030 set fontattr($f,weight) normal
8031 set fontattr($f,slant) roman
8032 foreach style [lrange $n 2 end] {
8033 switch -- $style {
8034 "normal" -
8035 "bold" {set fontattr($f,weight) $style}
8036 "roman" -
8037 "italic" {set fontattr($f,slant) $style}
8038 }
9c311b32 8039 }
0ed1dd3c
PM
8040}
8041
8042proc fontflags {f {isbold 0}} {
8043 global fontattr
8044
8045 return [list -family $fontattr($f,family) -size $fontattr($f,size) \
8046 -weight [expr {$isbold? "bold": $fontattr($f,weight)}] \
8047 -slant $fontattr($f,slant)]
8048}
8049
8050proc fontname {f} {
8051 global fontattr
8052
8053 set n [list $fontattr($f,family) $fontattr($f,size)]
8054 if {$fontattr($f,weight) eq "bold"} {
8055 lappend n "bold"
9c311b32 8056 }
0ed1dd3c
PM
8057 if {$fontattr($f,slant) eq "italic"} {
8058 lappend n "italic"
9c311b32 8059 }
0ed1dd3c 8060 return $n
9c311b32
PM
8061}
8062
1d10f36d 8063proc incrfont {inc} {
7fcc92bf 8064 global mainfont textfont ctext canv cflist showrefstop
0ed1dd3c
PM
8065 global stopped entries fontattr
8066
1d10f36d 8067 unmarkmatches
0ed1dd3c 8068 set s $fontattr(mainfont,size)
9c311b32
PM
8069 incr s $inc
8070 if {$s < 1} {
8071 set s 1
8072 }
0ed1dd3c 8073 set fontattr(mainfont,size) $s
9c311b32
PM
8074 font config mainfont -size $s
8075 font config mainfontbold -size $s
0ed1dd3c
PM
8076 set mainfont [fontname mainfont]
8077 set s $fontattr(textfont,size)
9c311b32
PM
8078 incr s $inc
8079 if {$s < 1} {
8080 set s 1
8081 }
0ed1dd3c 8082 set fontattr(textfont,size) $s
9c311b32
PM
8083 font config textfont -size $s
8084 font config textfontbold -size $s
0ed1dd3c 8085 set textfont [fontname textfont]
1d10f36d 8086 setcoords
32f1b3e4 8087 settabs
1d10f36d
PM
8088 redisplay
8089}
1db95b00 8090
ee3dc72e
PM
8091proc clearsha1 {} {
8092 global sha1entry sha1string
8093 if {[string length $sha1string] == 40} {
8094 $sha1entry delete 0 end
8095 }
8096}
8097
887fe3c4
PM
8098proc sha1change {n1 n2 op} {
8099 global sha1string currentid sha1but
8100 if {$sha1string == {}
8101 || ([info exists currentid] && $sha1string == $currentid)} {
8102 set state disabled
8103 } else {
8104 set state normal
8105 }
8106 if {[$sha1but cget -state] == $state} return
8107 if {$state == "normal"} {
d990cedf 8108 $sha1but conf -state normal -relief raised -text "[mc "Goto:"] "
887fe3c4 8109 } else {
d990cedf 8110 $sha1but conf -state disabled -relief flat -text "[mc "SHA1 ID:"] "
887fe3c4
PM
8111 }
8112}
8113
8114proc gotocommit {} {
7fcc92bf 8115 global sha1string tagids headids curview varcid
f3b8b3ce 8116
887fe3c4
PM
8117 if {$sha1string == {}
8118 || ([info exists currentid] && $sha1string == $currentid)} return
8119 if {[info exists tagids($sha1string)]} {
8120 set id $tagids($sha1string)
e1007129
SR
8121 } elseif {[info exists headids($sha1string)]} {
8122 set id $headids($sha1string)
887fe3c4
PM
8123 } else {
8124 set id [string tolower $sha1string]
f3b8b3ce 8125 if {[regexp {^[0-9a-f]{4,39}$} $id]} {
d375ef9b 8126 set matches [longid $id]
f3b8b3ce
PM
8127 if {$matches ne {}} {
8128 if {[llength $matches] > 1} {
d990cedf 8129 error_popup [mc "Short SHA1 id %s is ambiguous" $id]
f3b8b3ce
PM
8130 return
8131 }
d375ef9b 8132 set id [lindex $matches 0]
f3b8b3ce 8133 }
9bf3acfa
TR
8134 } else {
8135 if {[catch {set id [exec git rev-parse --verify $sha1string]}]} {
8136 error_popup [mc "Revision %s is not known" $sha1string]
8137 return
8138 }
f3b8b3ce 8139 }
887fe3c4 8140 }
7fcc92bf
PM
8141 if {[commitinview $id $curview]} {
8142 selectline [rowofcommit $id] 1
887fe3c4
PM
8143 return
8144 }
f3b8b3ce 8145 if {[regexp {^[0-9a-fA-F]{4,}$} $sha1string]} {
d990cedf 8146 set msg [mc "SHA1 id %s is not known" $sha1string]
887fe3c4 8147 } else {
9bf3acfa 8148 set msg [mc "Revision %s is not in the current view" $sha1string]
887fe3c4 8149 }
d990cedf 8150 error_popup $msg
887fe3c4
PM
8151}
8152
84ba7345
PM
8153proc lineenter {x y id} {
8154 global hoverx hovery hoverid hovertimer
8155 global commitinfo canv
8156
8ed16484 8157 if {![info exists commitinfo($id)] && ![getcommit $id]} return
84ba7345
PM
8158 set hoverx $x
8159 set hovery $y
8160 set hoverid $id
8161 if {[info exists hovertimer]} {
8162 after cancel $hovertimer
8163 }
8164 set hovertimer [after 500 linehover]
8165 $canv delete hover
8166}
8167
8168proc linemotion {x y id} {
8169 global hoverx hovery hoverid hovertimer
8170
8171 if {[info exists hoverid] && $id == $hoverid} {
8172 set hoverx $x
8173 set hovery $y
8174 if {[info exists hovertimer]} {
8175 after cancel $hovertimer
8176 }
8177 set hovertimer [after 500 linehover]
8178 }
8179}
8180
8181proc lineleave {id} {
8182 global hoverid hovertimer canv
8183
8184 if {[info exists hoverid] && $id == $hoverid} {
8185 $canv delete hover
8186 if {[info exists hovertimer]} {
8187 after cancel $hovertimer
8188 unset hovertimer
8189 }
8190 unset hoverid
8191 }
8192}
8193
8194proc linehover {} {
8195 global hoverx hovery hoverid hovertimer
8196 global canv linespc lthickness
9c311b32 8197 global commitinfo
84ba7345
PM
8198
8199 set text [lindex $commitinfo($hoverid) 0]
8200 set ymax [lindex [$canv cget -scrollregion] 3]
8201 if {$ymax == {}} return
8202 set yfrac [lindex [$canv yview] 0]
8203 set x [expr {$hoverx + 2 * $linespc}]
8204 set y [expr {$hovery + $yfrac * $ymax - $linespc / 2}]
8205 set x0 [expr {$x - 2 * $lthickness}]
8206 set y0 [expr {$y - 2 * $lthickness}]
9c311b32 8207 set x1 [expr {$x + [font measure mainfont $text] + 2 * $lthickness}]
84ba7345
PM
8208 set y1 [expr {$y + $linespc + 2 * $lthickness}]
8209 set t [$canv create rectangle $x0 $y0 $x1 $y1 \
8210 -fill \#ffff80 -outline black -width 1 -tags hover]
8211 $canv raise $t
f8a2c0d1 8212 set t [$canv create text $x $y -anchor nw -text $text -tags hover \
9c311b32 8213 -font mainfont]
84ba7345
PM
8214 $canv raise $t
8215}
8216
9843c307 8217proc clickisonarrow {id y} {
50b44ece 8218 global lthickness
9843c307 8219
50b44ece 8220 set ranges [rowranges $id]
9843c307 8221 set thresh [expr {2 * $lthickness + 6}]
50b44ece 8222 set n [expr {[llength $ranges] - 1}]
f6342480 8223 for {set i 1} {$i < $n} {incr i} {
50b44ece 8224 set row [lindex $ranges $i]
f6342480
PM
8225 if {abs([yc $row] - $y) < $thresh} {
8226 return $i
9843c307
PM
8227 }
8228 }
8229 return {}
8230}
8231
f6342480 8232proc arrowjump {id n y} {
50b44ece 8233 global canv
9843c307 8234
f6342480
PM
8235 # 1 <-> 2, 3 <-> 4, etc...
8236 set n [expr {(($n - 1) ^ 1) + 1}]
50b44ece 8237 set row [lindex [rowranges $id] $n]
f6342480 8238 set yt [yc $row]
9843c307
PM
8239 set ymax [lindex [$canv cget -scrollregion] 3]
8240 if {$ymax eq {} || $ymax <= 0} return
8241 set view [$canv yview]
8242 set yspan [expr {[lindex $view 1] - [lindex $view 0]}]
8243 set yfrac [expr {$yt / $ymax - $yspan / 2}]
8244 if {$yfrac < 0} {
8245 set yfrac 0
8246 }
f6342480 8247 allcanvs yview moveto $yfrac
9843c307
PM
8248}
8249
fa4da7b3 8250proc lineclick {x y id isnew} {
7fcc92bf 8251 global ctext commitinfo children canv thickerline curview
c8dfbcf9 8252
8ed16484 8253 if {![info exists commitinfo($id)] && ![getcommit $id]} return
c8dfbcf9 8254 unmarkmatches
fa4da7b3 8255 unselectline
9843c307
PM
8256 normalline
8257 $canv delete hover
8258 # draw this line thicker than normal
9843c307 8259 set thickerline $id
c934a8a3 8260 drawlines $id
fa4da7b3 8261 if {$isnew} {
9843c307
PM
8262 set ymax [lindex [$canv cget -scrollregion] 3]
8263 if {$ymax eq {}} return
8264 set yfrac [lindex [$canv yview] 0]
8265 set y [expr {$y + $yfrac * $ymax}]
8266 }
8267 set dirn [clickisonarrow $id $y]
8268 if {$dirn ne {}} {
8269 arrowjump $id $dirn $y
8270 return
8271 }
8272
8273 if {$isnew} {
354af6bd 8274 addtohistory [list lineclick $x $y $id 0] savectextpos
fa4da7b3 8275 }
c8dfbcf9
PM
8276 # fill the details pane with info about this line
8277 $ctext conf -state normal
3ea06f9f 8278 clear_ctext
32f1b3e4 8279 settabs 0
d990cedf 8280 $ctext insert end "[mc "Parent"]:\t"
97645683
PM
8281 $ctext insert end $id link0
8282 setlink $id link0
c8dfbcf9 8283 set info $commitinfo($id)
fa4da7b3 8284 $ctext insert end "\n\t[lindex $info 0]\n"
d990cedf 8285 $ctext insert end "\t[mc "Author"]:\t[lindex $info 1]\n"
232475d3 8286 set date [formatdate [lindex $info 2]]
d990cedf 8287 $ctext insert end "\t[mc "Date"]:\t$date\n"
da7c24dd 8288 set kids $children($curview,$id)
79b2c75e 8289 if {$kids ne {}} {
d990cedf 8290 $ctext insert end "\n[mc "Children"]:"
fa4da7b3 8291 set i 0
79b2c75e 8292 foreach child $kids {
fa4da7b3 8293 incr i
8ed16484 8294 if {![info exists commitinfo($child)] && ![getcommit $child]} continue
c8dfbcf9 8295 set info $commitinfo($child)
fa4da7b3 8296 $ctext insert end "\n\t"
97645683
PM
8297 $ctext insert end $child link$i
8298 setlink $child link$i
fa4da7b3 8299 $ctext insert end "\n\t[lindex $info 0]"
d990cedf 8300 $ctext insert end "\n\t[mc "Author"]:\t[lindex $info 1]"
232475d3 8301 set date [formatdate [lindex $info 2]]
d990cedf 8302 $ctext insert end "\n\t[mc "Date"]:\t$date\n"
c8dfbcf9
PM
8303 }
8304 }
354af6bd 8305 maybe_scroll_ctext 1
c8dfbcf9 8306 $ctext conf -state disabled
7fcceed7 8307 init_flist {}
c8dfbcf9
PM
8308}
8309
9843c307
PM
8310proc normalline {} {
8311 global thickerline
8312 if {[info exists thickerline]} {
c934a8a3 8313 set id $thickerline
9843c307 8314 unset thickerline
c934a8a3 8315 drawlines $id
9843c307
PM
8316 }
8317}
8318
354af6bd 8319proc selbyid {id {isnew 1}} {
7fcc92bf
PM
8320 global curview
8321 if {[commitinview $id $curview]} {
354af6bd 8322 selectline [rowofcommit $id] $isnew
c8dfbcf9
PM
8323 }
8324}
8325
8326proc mstime {} {
8327 global startmstime
8328 if {![info exists startmstime]} {
8329 set startmstime [clock clicks -milliseconds]
8330 }
8331 return [format "%.3f" [expr {([clock click -milliseconds] - $startmstime) / 1000.0}]]
8332}
8333
8334proc rowmenu {x y id} {
7fcc92bf 8335 global rowctxmenu selectedline rowmenuid curview
b9fdba7f 8336 global nullid nullid2 fakerowmenu mainhead markedid
c8dfbcf9 8337
bb3edc8b 8338 stopfinding
219ea3a9 8339 set rowmenuid $id
94b4a69f 8340 if {$selectedline eq {} || [rowofcommit $id] eq $selectedline} {
c8dfbcf9
PM
8341 set state disabled
8342 } else {
8343 set state normal
8344 }
8f489363 8345 if {$id ne $nullid && $id ne $nullid2} {
219ea3a9 8346 set menu $rowctxmenu
5e3502da 8347 if {$mainhead ne {}} {
da12e59d 8348 $menu entryconfigure 7 -label [mc "Reset %s branch to here" $mainhead] -state normal
5e3502da
MB
8349 } else {
8350 $menu entryconfigure 7 -label [mc "Detached head: can't reset" $mainhead] -state disabled
8351 }
b9fdba7f
PM
8352 if {[info exists markedid] && $markedid ne $id} {
8353 $menu entryconfigure 9 -state normal
8354 $menu entryconfigure 10 -state normal
010509f2 8355 $menu entryconfigure 11 -state normal
b9fdba7f
PM
8356 } else {
8357 $menu entryconfigure 9 -state disabled
8358 $menu entryconfigure 10 -state disabled
010509f2 8359 $menu entryconfigure 11 -state disabled
b9fdba7f 8360 }
219ea3a9
PM
8361 } else {
8362 set menu $fakerowmenu
8363 }
f2d0bbbd
PM
8364 $menu entryconfigure [mca "Diff this -> selected"] -state $state
8365 $menu entryconfigure [mca "Diff selected -> this"] -state $state
8366 $menu entryconfigure [mca "Make patch"] -state $state
219ea3a9 8367 tk_popup $menu $x $y
c8dfbcf9
PM
8368}
8369
b9fdba7f
PM
8370proc markhere {} {
8371 global rowmenuid markedid canv
8372
8373 set markedid $rowmenuid
8374 make_idmark $markedid
8375}
8376
8377proc gotomark {} {
8378 global markedid
8379
8380 if {[info exists markedid]} {
8381 selbyid $markedid
8382 }
8383}
8384
8385proc replace_by_kids {l r} {
8386 global curview children
8387
8388 set id [commitonrow $r]
8389 set l [lreplace $l 0 0]
8390 foreach kid $children($curview,$id) {
8391 lappend l [rowofcommit $kid]
8392 }
8393 return [lsort -integer -decreasing -unique $l]
8394}
8395
8396proc find_common_desc {} {
8397 global markedid rowmenuid curview children
8398
8399 if {![info exists markedid]} return
8400 if {![commitinview $markedid $curview] ||
8401 ![commitinview $rowmenuid $curview]} return
8402 #set t1 [clock clicks -milliseconds]
8403 set l1 [list [rowofcommit $markedid]]
8404 set l2 [list [rowofcommit $rowmenuid]]
8405 while 1 {
8406 set r1 [lindex $l1 0]
8407 set r2 [lindex $l2 0]
8408 if {$r1 eq {} || $r2 eq {}} break
8409 if {$r1 == $r2} {
8410 selectline $r1 1
8411 break
8412 }
8413 if {$r1 > $r2} {
8414 set l1 [replace_by_kids $l1 $r1]
8415 } else {
8416 set l2 [replace_by_kids $l2 $r2]
8417 }
8418 }
8419 #set t2 [clock clicks -milliseconds]
8420 #puts "took [expr {$t2-$t1}]ms"
8421}
8422
010509f2
PM
8423proc compare_commits {} {
8424 global markedid rowmenuid curview children
8425
8426 if {![info exists markedid]} return
8427 if {![commitinview $markedid $curview]} return
8428 addtohistory [list do_cmp_commits $markedid $rowmenuid]
8429 do_cmp_commits $markedid $rowmenuid
8430}
8431
8432proc getpatchid {id} {
8433 global patchids
8434
8435 if {![info exists patchids($id)]} {
6f63fc18
PM
8436 set cmd [diffcmd [list $id] {-p --root}]
8437 # trim off the initial "|"
8438 set cmd [lrange $cmd 1 end]
8439 if {[catch {
8440 set x [eval exec $cmd | git patch-id]
8441 set patchids($id) [lindex $x 0]
8442 }]} {
8443 set patchids($id) "error"
8444 }
010509f2
PM
8445 }
8446 return $patchids($id)
8447}
8448
8449proc do_cmp_commits {a b} {
8450 global ctext curview parents children patchids commitinfo
8451
8452 $ctext conf -state normal
8453 clear_ctext
8454 init_flist {}
8455 for {set i 0} {$i < 100} {incr i} {
010509f2
PM
8456 set skipa 0
8457 set skipb 0
8458 if {[llength $parents($curview,$a)] > 1} {
6f63fc18 8459 appendshortlink $a [mc "Skipping merge commit "] "\n"
010509f2
PM
8460 set skipa 1
8461 } else {
8462 set patcha [getpatchid $a]
8463 }
8464 if {[llength $parents($curview,$b)] > 1} {
6f63fc18 8465 appendshortlink $b [mc "Skipping merge commit "] "\n"
010509f2
PM
8466 set skipb 1
8467 } else {
8468 set patchb [getpatchid $b]
8469 }
8470 if {!$skipa && !$skipb} {
8471 set heada [lindex $commitinfo($a) 0]
8472 set headb [lindex $commitinfo($b) 0]
6f63fc18
PM
8473 if {$patcha eq "error"} {
8474 appendshortlink $a [mc "Error getting patch ID for "] \
8475 [mc " - stopping\n"]
8476 break
8477 }
8478 if {$patchb eq "error"} {
8479 appendshortlink $b [mc "Error getting patch ID for "] \
8480 [mc " - stopping\n"]
8481 break
8482 }
010509f2
PM
8483 if {$patcha eq $patchb} {
8484 if {$heada eq $headb} {
6f63fc18
PM
8485 appendshortlink $a [mc "Commit "]
8486 appendshortlink $b " == " " $heada\n"
010509f2 8487 } else {
6f63fc18
PM
8488 appendshortlink $a [mc "Commit "] " $heada\n"
8489 appendshortlink $b [mc " is the same patch as\n "] \
8490 " $headb\n"
010509f2
PM
8491 }
8492 set skipa 1
8493 set skipb 1
8494 } else {
8495 $ctext insert end "\n"
6f63fc18
PM
8496 appendshortlink $a [mc "Commit "] " $heada\n"
8497 appendshortlink $b [mc " differs from\n "] \
8498 " $headb\n"
c21398be
PM
8499 $ctext insert end [mc "Diff of commits:\n\n"]
8500 $ctext conf -state disabled
8501 update
8502 diffcommits $a $b
8503 return
010509f2
PM
8504 }
8505 }
8506 if {$skipa} {
aa43561a
PM
8507 set kids [real_children $curview,$a]
8508 if {[llength $kids] != 1} {
010509f2 8509 $ctext insert end "\n"
6f63fc18 8510 appendshortlink $a [mc "Commit "] \
aa43561a 8511 [mc " has %s children - stopping\n" [llength $kids]]
010509f2
PM
8512 break
8513 }
aa43561a 8514 set a [lindex $kids 0]
010509f2
PM
8515 }
8516 if {$skipb} {
aa43561a
PM
8517 set kids [real_children $curview,$b]
8518 if {[llength $kids] != 1} {
6f63fc18 8519 appendshortlink $b [mc "Commit "] \
aa43561a 8520 [mc " has %s children - stopping\n" [llength $kids]]
010509f2
PM
8521 break
8522 }
aa43561a 8523 set b [lindex $kids 0]
010509f2
PM
8524 }
8525 }
8526 $ctext conf -state disabled
8527}
8528
c21398be
PM
8529proc diffcommits {a b} {
8530 global diffcontext diffids blobdifffd diffinhdr
8531
8532 set tmpdir [gitknewtmpdir]
8533 set fna [file join $tmpdir "commit-[string range $a 0 7]"]
8534 set fnb [file join $tmpdir "commit-[string range $b 0 7]"]
8535 if {[catch {
8536 exec git diff-tree -p --pretty $a >$fna
8537 exec git diff-tree -p --pretty $b >$fnb
8538 } err]} {
8539 error_popup [mc "Error writing commit to file: %s" $err]
8540 return
8541 }
8542 if {[catch {
8543 set fd [open "| diff -U$diffcontext $fna $fnb" r]
8544 } err]} {
8545 error_popup [mc "Error diffing commits: %s" $err]
8546 return
8547 }
8548 set diffids [list commits $a $b]
8549 set blobdifffd($diffids) $fd
8550 set diffinhdr 0
8551 filerun $fd [list getblobdiffline $fd $diffids]
8552}
8553
c8dfbcf9 8554proc diffvssel {dirn} {
7fcc92bf 8555 global rowmenuid selectedline
c8dfbcf9 8556
94b4a69f 8557 if {$selectedline eq {}} return
c8dfbcf9 8558 if {$dirn} {
7fcc92bf 8559 set oldid [commitonrow $selectedline]
c8dfbcf9
PM
8560 set newid $rowmenuid
8561 } else {
8562 set oldid $rowmenuid
7fcc92bf 8563 set newid [commitonrow $selectedline]
c8dfbcf9 8564 }
354af6bd 8565 addtohistory [list doseldiff $oldid $newid] savectextpos
fa4da7b3
PM
8566 doseldiff $oldid $newid
8567}
8568
8569proc doseldiff {oldid newid} {
7fcceed7 8570 global ctext
fa4da7b3
PM
8571 global commitinfo
8572
c8dfbcf9 8573 $ctext conf -state normal
3ea06f9f 8574 clear_ctext
d990cedf
CS
8575 init_flist [mc "Top"]
8576 $ctext insert end "[mc "From"] "
97645683
PM
8577 $ctext insert end $oldid link0
8578 setlink $oldid link0
fa4da7b3 8579 $ctext insert end "\n "
c8dfbcf9 8580 $ctext insert end [lindex $commitinfo($oldid) 0]
d990cedf 8581 $ctext insert end "\n\n[mc "To"] "
97645683
PM
8582 $ctext insert end $newid link1
8583 setlink $newid link1
fa4da7b3 8584 $ctext insert end "\n "
c8dfbcf9
PM
8585 $ctext insert end [lindex $commitinfo($newid) 0]
8586 $ctext insert end "\n"
8587 $ctext conf -state disabled
c8dfbcf9 8588 $ctext tag remove found 1.0 end
d327244a 8589 startdiff [list $oldid $newid]
c8dfbcf9
PM
8590}
8591
74daedb6 8592proc mkpatch {} {
d93f1713 8593 global rowmenuid currentid commitinfo patchtop patchnum NS
74daedb6
PM
8594
8595 if {![info exists currentid]} return
8596 set oldid $currentid
8597 set oldhead [lindex $commitinfo($oldid) 0]
8598 set newid $rowmenuid
8599 set newhead [lindex $commitinfo($newid) 0]
8600 set top .patch
8601 set patchtop $top
8602 catch {destroy $top}
d93f1713 8603 ttk_toplevel $top
e7d64008 8604 make_transient $top .
d93f1713 8605 ${NS}::label $top.title -text [mc "Generate patch"]
4a2139f5 8606 grid $top.title - -pady 10
d93f1713
PT
8607 ${NS}::label $top.from -text [mc "From:"]
8608 ${NS}::entry $top.fromsha1 -width 40
74daedb6
PM
8609 $top.fromsha1 insert 0 $oldid
8610 $top.fromsha1 conf -state readonly
8611 grid $top.from $top.fromsha1 -sticky w
d93f1713 8612 ${NS}::entry $top.fromhead -width 60
74daedb6
PM
8613 $top.fromhead insert 0 $oldhead
8614 $top.fromhead conf -state readonly
8615 grid x $top.fromhead -sticky w
d93f1713
PT
8616 ${NS}::label $top.to -text [mc "To:"]
8617 ${NS}::entry $top.tosha1 -width 40
74daedb6
PM
8618 $top.tosha1 insert 0 $newid
8619 $top.tosha1 conf -state readonly
8620 grid $top.to $top.tosha1 -sticky w
d93f1713 8621 ${NS}::entry $top.tohead -width 60
74daedb6
PM
8622 $top.tohead insert 0 $newhead
8623 $top.tohead conf -state readonly
8624 grid x $top.tohead -sticky w
d93f1713
PT
8625 ${NS}::button $top.rev -text [mc "Reverse"] -command mkpatchrev
8626 grid $top.rev x -pady 10 -padx 5
8627 ${NS}::label $top.flab -text [mc "Output file:"]
8628 ${NS}::entry $top.fname -width 60
74daedb6
PM
8629 $top.fname insert 0 [file normalize "patch$patchnum.patch"]
8630 incr patchnum
bdbfbe3d 8631 grid $top.flab $top.fname -sticky w
d93f1713
PT
8632 ${NS}::frame $top.buts
8633 ${NS}::button $top.buts.gen -text [mc "Generate"] -command mkpatchgo
8634 ${NS}::button $top.buts.can -text [mc "Cancel"] -command mkpatchcan
76f15947
AG
8635 bind $top <Key-Return> mkpatchgo
8636 bind $top <Key-Escape> mkpatchcan
74daedb6
PM
8637 grid $top.buts.gen $top.buts.can
8638 grid columnconfigure $top.buts 0 -weight 1 -uniform a
8639 grid columnconfigure $top.buts 1 -weight 1 -uniform a
8640 grid $top.buts - -pady 10 -sticky ew
bdbfbe3d 8641 focus $top.fname
74daedb6
PM
8642}
8643
8644proc mkpatchrev {} {
8645 global patchtop
8646
8647 set oldid [$patchtop.fromsha1 get]
8648 set oldhead [$patchtop.fromhead get]
8649 set newid [$patchtop.tosha1 get]
8650 set newhead [$patchtop.tohead get]
8651 foreach e [list fromsha1 fromhead tosha1 tohead] \
8652 v [list $newid $newhead $oldid $oldhead] {
8653 $patchtop.$e conf -state normal
8654 $patchtop.$e delete 0 end
8655 $patchtop.$e insert 0 $v
8656 $patchtop.$e conf -state readonly
8657 }
8658}
8659
8660proc mkpatchgo {} {
8f489363 8661 global patchtop nullid nullid2
74daedb6
PM
8662
8663 set oldid [$patchtop.fromsha1 get]
8664 set newid [$patchtop.tosha1 get]
8665 set fname [$patchtop.fname get]
8f489363 8666 set cmd [diffcmd [list $oldid $newid] -p]
d372e216
PM
8667 # trim off the initial "|"
8668 set cmd [lrange $cmd 1 end]
219ea3a9
PM
8669 lappend cmd >$fname &
8670 if {[catch {eval exec $cmd} err]} {
84a76f18 8671 error_popup "[mc "Error creating patch:"] $err" $patchtop
74daedb6
PM
8672 }
8673 catch {destroy $patchtop}
8674 unset patchtop
8675}
8676
8677proc mkpatchcan {} {
8678 global patchtop
8679
8680 catch {destroy $patchtop}
8681 unset patchtop
8682}
8683
bdbfbe3d 8684proc mktag {} {
d93f1713 8685 global rowmenuid mktagtop commitinfo NS
bdbfbe3d
PM
8686
8687 set top .maketag
8688 set mktagtop $top
8689 catch {destroy $top}
d93f1713 8690 ttk_toplevel $top
e7d64008 8691 make_transient $top .
d93f1713 8692 ${NS}::label $top.title -text [mc "Create tag"]
4a2139f5 8693 grid $top.title - -pady 10
d93f1713
PT
8694 ${NS}::label $top.id -text [mc "ID:"]
8695 ${NS}::entry $top.sha1 -width 40
bdbfbe3d
PM
8696 $top.sha1 insert 0 $rowmenuid
8697 $top.sha1 conf -state readonly
8698 grid $top.id $top.sha1 -sticky w
d93f1713 8699 ${NS}::entry $top.head -width 60
bdbfbe3d
PM
8700 $top.head insert 0 [lindex $commitinfo($rowmenuid) 0]
8701 $top.head conf -state readonly
8702 grid x $top.head -sticky w
d93f1713
PT
8703 ${NS}::label $top.tlab -text [mc "Tag name:"]
8704 ${NS}::entry $top.tag -width 60
bdbfbe3d 8705 grid $top.tlab $top.tag -sticky w
dfb891e3
DD
8706 ${NS}::label $top.op -text [mc "Tag message is optional"]
8707 grid $top.op -columnspan 2 -sticky we
8708 ${NS}::label $top.mlab -text [mc "Tag message:"]
8709 ${NS}::entry $top.msg -width 60
8710 grid $top.mlab $top.msg -sticky w
d93f1713
PT
8711 ${NS}::frame $top.buts
8712 ${NS}::button $top.buts.gen -text [mc "Create"] -command mktaggo
8713 ${NS}::button $top.buts.can -text [mc "Cancel"] -command mktagcan
76f15947
AG
8714 bind $top <Key-Return> mktaggo
8715 bind $top <Key-Escape> mktagcan
bdbfbe3d
PM
8716 grid $top.buts.gen $top.buts.can
8717 grid columnconfigure $top.buts 0 -weight 1 -uniform a
8718 grid columnconfigure $top.buts 1 -weight 1 -uniform a
8719 grid $top.buts - -pady 10 -sticky ew
8720 focus $top.tag
8721}
8722
8723proc domktag {} {
8724 global mktagtop env tagids idtags
bdbfbe3d
PM
8725
8726 set id [$mktagtop.sha1 get]
8727 set tag [$mktagtop.tag get]
dfb891e3 8728 set msg [$mktagtop.msg get]
bdbfbe3d 8729 if {$tag == {}} {
84a76f18
AG
8730 error_popup [mc "No tag name specified"] $mktagtop
8731 return 0
bdbfbe3d
PM
8732 }
8733 if {[info exists tagids($tag)]} {
84a76f18
AG
8734 error_popup [mc "Tag \"%s\" already exists" $tag] $mktagtop
8735 return 0
bdbfbe3d
PM
8736 }
8737 if {[catch {
dfb891e3
DD
8738 if {$msg != {}} {
8739 exec git tag -a -m $msg $tag $id
8740 } else {
8741 exec git tag $tag $id
8742 }
bdbfbe3d 8743 } err]} {
84a76f18
AG
8744 error_popup "[mc "Error creating tag:"] $err" $mktagtop
8745 return 0
bdbfbe3d
PM
8746 }
8747
8748 set tagids($tag) $id
8749 lappend idtags($id) $tag
f1d83ba3 8750 redrawtags $id
ceadfe90 8751 addedtag $id
887c996e
PM
8752 dispneartags 0
8753 run refill_reflist
84a76f18 8754 return 1
f1d83ba3
PM
8755}
8756
8757proc redrawtags {id} {
b9fdba7f 8758 global canv linehtag idpos currentid curview cmitlisted markedid
c11ff120 8759 global canvxmax iddrawn circleitem mainheadid circlecolors
f1d83ba3 8760
7fcc92bf 8761 if {![commitinview $id $curview]} return
322a8cc9 8762 if {![info exists iddrawn($id)]} return
fc2a256f 8763 set row [rowofcommit $id]
c11ff120
PM
8764 if {$id eq $mainheadid} {
8765 set ofill yellow
8766 } else {
8767 set ofill [lindex $circlecolors $cmitlisted($curview,$id)]
8768 }
8769 $canv itemconf $circleitem($row) -fill $ofill
bdbfbe3d
PM
8770 $canv delete tag.$id
8771 set xt [eval drawtags $id $idpos($id)]
28593d3f
PM
8772 $canv coords $linehtag($id) $xt [lindex $idpos($id) 2]
8773 set text [$canv itemcget $linehtag($id) -text]
8774 set font [$canv itemcget $linehtag($id) -font]
fc2a256f 8775 set xr [expr {$xt + [font measure $font $text]}]
b8ab2e17
PM
8776 if {$xr > $canvxmax} {
8777 set canvxmax $xr
8778 setcanvscroll
8779 }
fc2a256f 8780 if {[info exists currentid] && $currentid == $id} {
28593d3f 8781 make_secsel $id
bdbfbe3d 8782 }
b9fdba7f
PM
8783 if {[info exists markedid] && $markedid eq $id} {
8784 make_idmark $id
8785 }
bdbfbe3d
PM
8786}
8787
8788proc mktagcan {} {
8789 global mktagtop
8790
8791 catch {destroy $mktagtop}
8792 unset mktagtop
8793}
8794
8795proc mktaggo {} {
84a76f18 8796 if {![domktag]} return
bdbfbe3d
PM
8797 mktagcan
8798}
8799
4a2139f5 8800proc writecommit {} {
d93f1713 8801 global rowmenuid wrcomtop commitinfo wrcomcmd NS
4a2139f5
PM
8802
8803 set top .writecommit
8804 set wrcomtop $top
8805 catch {destroy $top}
d93f1713 8806 ttk_toplevel $top
e7d64008 8807 make_transient $top .
d93f1713 8808 ${NS}::label $top.title -text [mc "Write commit to file"]
4a2139f5 8809 grid $top.title - -pady 10
d93f1713
PT
8810 ${NS}::label $top.id -text [mc "ID:"]
8811 ${NS}::entry $top.sha1 -width 40
4a2139f5
PM
8812 $top.sha1 insert 0 $rowmenuid
8813 $top.sha1 conf -state readonly
8814 grid $top.id $top.sha1 -sticky w
d93f1713 8815 ${NS}::entry $top.head -width 60
4a2139f5
PM
8816 $top.head insert 0 [lindex $commitinfo($rowmenuid) 0]
8817 $top.head conf -state readonly
8818 grid x $top.head -sticky w
d93f1713
PT
8819 ${NS}::label $top.clab -text [mc "Command:"]
8820 ${NS}::entry $top.cmd -width 60 -textvariable wrcomcmd
4a2139f5 8821 grid $top.clab $top.cmd -sticky w -pady 10
d93f1713
PT
8822 ${NS}::label $top.flab -text [mc "Output file:"]
8823 ${NS}::entry $top.fname -width 60
4a2139f5
PM
8824 $top.fname insert 0 [file normalize "commit-[string range $rowmenuid 0 6]"]
8825 grid $top.flab $top.fname -sticky w
d93f1713
PT
8826 ${NS}::frame $top.buts
8827 ${NS}::button $top.buts.gen -text [mc "Write"] -command wrcomgo
8828 ${NS}::button $top.buts.can -text [mc "Cancel"] -command wrcomcan
76f15947
AG
8829 bind $top <Key-Return> wrcomgo
8830 bind $top <Key-Escape> wrcomcan
4a2139f5
PM
8831 grid $top.buts.gen $top.buts.can
8832 grid columnconfigure $top.buts 0 -weight 1 -uniform a
8833 grid columnconfigure $top.buts 1 -weight 1 -uniform a
8834 grid $top.buts - -pady 10 -sticky ew
8835 focus $top.fname
8836}
8837
8838proc wrcomgo {} {
8839 global wrcomtop
8840
8841 set id [$wrcomtop.sha1 get]
8842 set cmd "echo $id | [$wrcomtop.cmd get]"
8843 set fname [$wrcomtop.fname get]
8844 if {[catch {exec sh -c $cmd >$fname &} err]} {
84a76f18 8845 error_popup "[mc "Error writing commit:"] $err" $wrcomtop
4a2139f5
PM
8846 }
8847 catch {destroy $wrcomtop}
8848 unset wrcomtop
8849}
8850
8851proc wrcomcan {} {
8852 global wrcomtop
8853
8854 catch {destroy $wrcomtop}
8855 unset wrcomtop
8856}
8857
d6ac1a86 8858proc mkbranch {} {
d93f1713 8859 global rowmenuid mkbrtop NS
d6ac1a86
PM
8860
8861 set top .makebranch
8862 catch {destroy $top}
d93f1713 8863 ttk_toplevel $top
e7d64008 8864 make_transient $top .
d93f1713 8865 ${NS}::label $top.title -text [mc "Create new branch"]
d6ac1a86 8866 grid $top.title - -pady 10
d93f1713
PT
8867 ${NS}::label $top.id -text [mc "ID:"]
8868 ${NS}::entry $top.sha1 -width 40
d6ac1a86
PM
8869 $top.sha1 insert 0 $rowmenuid
8870 $top.sha1 conf -state readonly
8871 grid $top.id $top.sha1 -sticky w
d93f1713
PT
8872 ${NS}::label $top.nlab -text [mc "Name:"]
8873 ${NS}::entry $top.name -width 40
d6ac1a86 8874 grid $top.nlab $top.name -sticky w
d93f1713
PT
8875 ${NS}::frame $top.buts
8876 ${NS}::button $top.buts.go -text [mc "Create"] -command [list mkbrgo $top]
8877 ${NS}::button $top.buts.can -text [mc "Cancel"] -command "catch {destroy $top}"
76f15947
AG
8878 bind $top <Key-Return> [list mkbrgo $top]
8879 bind $top <Key-Escape> "catch {destroy $top}"
d6ac1a86
PM
8880 grid $top.buts.go $top.buts.can
8881 grid columnconfigure $top.buts 0 -weight 1 -uniform a
8882 grid columnconfigure $top.buts 1 -weight 1 -uniform a
8883 grid $top.buts - -pady 10 -sticky ew
8884 focus $top.name
8885}
8886
8887proc mkbrgo {top} {
8888 global headids idheads
8889
8890 set name [$top.name get]
8891 set id [$top.sha1 get]
bee866fa
AG
8892 set cmdargs {}
8893 set old_id {}
d6ac1a86 8894 if {$name eq {}} {
84a76f18 8895 error_popup [mc "Please specify a name for the new branch"] $top
d6ac1a86
PM
8896 return
8897 }
bee866fa
AG
8898 if {[info exists headids($name)]} {
8899 if {![confirm_popup [mc \
84a76f18 8900 "Branch '%s' already exists. Overwrite?" $name] $top]} {
bee866fa
AG
8901 return
8902 }
8903 set old_id $headids($name)
8904 lappend cmdargs -f
8905 }
d6ac1a86 8906 catch {destroy $top}
bee866fa 8907 lappend cmdargs $name $id
d6ac1a86
PM
8908 nowbusy newbranch
8909 update
8910 if {[catch {
bee866fa 8911 eval exec git branch $cmdargs
d6ac1a86
PM
8912 } err]} {
8913 notbusy newbranch
8914 error_popup $err
8915 } else {
d6ac1a86 8916 notbusy newbranch
bee866fa
AG
8917 if {$old_id ne {}} {
8918 movehead $id $name
8919 movedhead $id $name
8920 redrawtags $old_id
8921 redrawtags $id
8922 } else {
8923 set headids($name) $id
8924 lappend idheads($id) $name
8925 addedhead $id $name
8926 redrawtags $id
8927 }
e11f1233 8928 dispneartags 0
887c996e 8929 run refill_reflist
d6ac1a86
PM
8930 }
8931}
8932
15e35055
AG
8933proc exec_citool {tool_args {baseid {}}} {
8934 global commitinfo env
8935
8936 set save_env [array get env GIT_AUTHOR_*]
8937
8938 if {$baseid ne {}} {
8939 if {![info exists commitinfo($baseid)]} {
8940 getcommit $baseid
8941 }
8942 set author [lindex $commitinfo($baseid) 1]
8943 set date [lindex $commitinfo($baseid) 2]
8944 if {[regexp {^\s*(\S.*\S|\S)\s*<(.*)>\s*$} \
8945 $author author name email]
8946 && $date ne {}} {
8947 set env(GIT_AUTHOR_NAME) $name
8948 set env(GIT_AUTHOR_EMAIL) $email
8949 set env(GIT_AUTHOR_DATE) $date
8950 }
8951 }
8952
8953 eval exec git citool $tool_args &
8954
8955 array unset env GIT_AUTHOR_*
8956 array set env $save_env
8957}
8958
ca6d8f58 8959proc cherrypick {} {
468bcaed 8960 global rowmenuid curview
b8a938cf 8961 global mainhead mainheadid
ca6d8f58 8962
e11f1233
PM
8963 set oldhead [exec git rev-parse HEAD]
8964 set dheads [descheads $rowmenuid]
8965 if {$dheads ne {} && [lsearch -exact $dheads $oldhead] >= 0} {
d990cedf
CS
8966 set ok [confirm_popup [mc "Commit %s is already\
8967 included in branch %s -- really re-apply it?" \
8968 [string range $rowmenuid 0 7] $mainhead]]
ca6d8f58
PM
8969 if {!$ok} return
8970 }
d990cedf 8971 nowbusy cherrypick [mc "Cherry-picking"]
ca6d8f58 8972 update
ca6d8f58
PM
8973 # Unfortunately git-cherry-pick writes stuff to stderr even when
8974 # no error occurs, and exec takes that as an indication of error...
8975 if {[catch {exec sh -c "git cherry-pick -r $rowmenuid 2>&1"} err]} {
8976 notbusy cherrypick
15e35055 8977 if {[regexp -line \
887a791f
PM
8978 {Entry '(.*)' (would be overwritten by merge|not uptodate)} \
8979 $err msg fname]} {
8980 error_popup [mc "Cherry-pick failed because of local changes\
8981 to file '%s'.\nPlease commit, reset or stash\
8982 your changes and try again." $fname]
8983 } elseif {[regexp -line \
8984 {^(CONFLICT \(.*\):|Automatic cherry-pick failed)} \
8985 $err]} {
8986 if {[confirm_popup [mc "Cherry-pick failed because of merge\
8987 conflict.\nDo you wish to run git citool to\
8988 resolve it?"]]} {
8989 # Force citool to read MERGE_MSG
8990 file delete [file join [gitdir] "GITGUI_MSG"]
8991 exec_citool {} $rowmenuid
8992 }
15e35055
AG
8993 } else {
8994 error_popup $err
8995 }
887a791f 8996 run updatecommits
ca6d8f58
PM
8997 return
8998 }
8999 set newhead [exec git rev-parse HEAD]
9000 if {$newhead eq $oldhead} {
9001 notbusy cherrypick
d990cedf 9002 error_popup [mc "No changes committed"]
ca6d8f58
PM
9003 return
9004 }
e11f1233 9005 addnewchild $newhead $oldhead
7fcc92bf 9006 if {[commitinview $oldhead $curview]} {
cdc8429c 9007 # XXX this isn't right if we have a path limit...
7fcc92bf 9008 insertrow $newhead $oldhead $curview
ca6d8f58 9009 if {$mainhead ne {}} {
e11f1233 9010 movehead $newhead $mainhead
ca6d8f58
PM
9011 movedhead $newhead $mainhead
9012 }
c11ff120 9013 set mainheadid $newhead
ca6d8f58
PM
9014 redrawtags $oldhead
9015 redrawtags $newhead
46308ea1 9016 selbyid $newhead
ca6d8f58
PM
9017 }
9018 notbusy cherrypick
9019}
9020
6fb735ae 9021proc resethead {} {
d93f1713 9022 global mainhead rowmenuid confirm_ok resettype NS
6fb735ae
PM
9023
9024 set confirm_ok 0
9025 set w ".confirmreset"
d93f1713 9026 ttk_toplevel $w
e7d64008 9027 make_transient $w .
d990cedf 9028 wm title $w [mc "Confirm reset"]
d93f1713
PT
9029 ${NS}::label $w.m -text \
9030 [mc "Reset branch %s to %s?" $mainhead [string range $rowmenuid 0 7]]
6fb735ae 9031 pack $w.m -side top -fill x -padx 20 -pady 20
d93f1713 9032 ${NS}::labelframe $w.f -text [mc "Reset type:"]
6fb735ae 9033 set resettype mixed
d93f1713 9034 ${NS}::radiobutton $w.f.soft -value soft -variable resettype \
d990cedf 9035 -text [mc "Soft: Leave working tree and index untouched"]
6fb735ae 9036 grid $w.f.soft -sticky w
d93f1713 9037 ${NS}::radiobutton $w.f.mixed -value mixed -variable resettype \
d990cedf 9038 -text [mc "Mixed: Leave working tree untouched, reset index"]
6fb735ae 9039 grid $w.f.mixed -sticky w
d93f1713 9040 ${NS}::radiobutton $w.f.hard -value hard -variable resettype \
d990cedf 9041 -text [mc "Hard: Reset working tree and index\n(discard ALL local changes)"]
6fb735ae 9042 grid $w.f.hard -sticky w
d93f1713
PT
9043 pack $w.f -side top -fill x -padx 4
9044 ${NS}::button $w.ok -text [mc OK] -command "set confirm_ok 1; destroy $w"
6fb735ae 9045 pack $w.ok -side left -fill x -padx 20 -pady 20
d93f1713 9046 ${NS}::button $w.cancel -text [mc Cancel] -command "destroy $w"
76f15947 9047 bind $w <Key-Escape> [list destroy $w]
6fb735ae
PM
9048 pack $w.cancel -side right -fill x -padx 20 -pady 20
9049 bind $w <Visibility> "grab $w; focus $w"
9050 tkwait window $w
9051 if {!$confirm_ok} return
706d6c3e 9052 if {[catch {set fd [open \
08ba820f 9053 [list | git reset --$resettype $rowmenuid 2>@1] r]} err]} {
6fb735ae
PM
9054 error_popup $err
9055 } else {
706d6c3e 9056 dohidelocalchanges
a137a90f 9057 filerun $fd [list readresetstat $fd]
d990cedf 9058 nowbusy reset [mc "Resetting"]
46308ea1 9059 selbyid $rowmenuid
706d6c3e
PM
9060 }
9061}
9062
a137a90f
PM
9063proc readresetstat {fd} {
9064 global mainhead mainheadid showlocalchanges rprogcoord
706d6c3e
PM
9065
9066 if {[gets $fd line] >= 0} {
9067 if {[regexp {([0-9]+)% \(([0-9]+)/([0-9]+)\)} $line match p m n]} {
a137a90f
PM
9068 set rprogcoord [expr {1.0 * $m / $n}]
9069 adjustprogress
706d6c3e
PM
9070 }
9071 return 1
9072 }
a137a90f
PM
9073 set rprogcoord 0
9074 adjustprogress
706d6c3e
PM
9075 notbusy reset
9076 if {[catch {close $fd} err]} {
9077 error_popup $err
9078 }
9079 set oldhead $mainheadid
9080 set newhead [exec git rev-parse HEAD]
9081 if {$newhead ne $oldhead} {
9082 movehead $newhead $mainhead
9083 movedhead $newhead $mainhead
9084 set mainheadid $newhead
6fb735ae 9085 redrawtags $oldhead
706d6c3e 9086 redrawtags $newhead
6fb735ae
PM
9087 }
9088 if {$showlocalchanges} {
9089 doshowlocalchanges
9090 }
706d6c3e 9091 return 0
6fb735ae
PM
9092}
9093
10299152
PM
9094# context menu for a head
9095proc headmenu {x y id head} {
00609463 9096 global headmenuid headmenuhead headctxmenu mainhead
10299152 9097
bb3edc8b 9098 stopfinding
10299152
PM
9099 set headmenuid $id
9100 set headmenuhead $head
00609463 9101 set state normal
70a5fc44
SC
9102 if {[string match "remotes/*" $head]} {
9103 set state disabled
9104 }
00609463
PM
9105 if {$head eq $mainhead} {
9106 set state disabled
9107 }
9108 $headctxmenu entryconfigure 0 -state $state
9109 $headctxmenu entryconfigure 1 -state $state
10299152
PM
9110 tk_popup $headctxmenu $x $y
9111}
9112
9113proc cobranch {} {
c11ff120 9114 global headmenuid headmenuhead headids
cdc8429c 9115 global showlocalchanges
10299152
PM
9116
9117 # check the tree is clean first??
d990cedf 9118 nowbusy checkout [mc "Checking out"]
10299152 9119 update
219ea3a9 9120 dohidelocalchanges
10299152 9121 if {[catch {
08ba820f 9122 set fd [open [list | git checkout $headmenuhead 2>@1] r]
10299152
PM
9123 } err]} {
9124 notbusy checkout
9125 error_popup $err
08ba820f
PM
9126 if {$showlocalchanges} {
9127 dodiffindex
9128 }
10299152 9129 } else {
08ba820f
PM
9130 filerun $fd [list readcheckoutstat $fd $headmenuhead $headmenuid]
9131 }
9132}
9133
9134proc readcheckoutstat {fd newhead newheadid} {
9135 global mainhead mainheadid headids showlocalchanges progresscoords
cdc8429c 9136 global viewmainheadid curview
08ba820f
PM
9137
9138 if {[gets $fd line] >= 0} {
9139 if {[regexp {([0-9]+)% \(([0-9]+)/([0-9]+)\)} $line match p m n]} {
9140 set progresscoords [list 0 [expr {1.0 * $m / $n}]]
9141 adjustprogress
10299152 9142 }
08ba820f
PM
9143 return 1
9144 }
9145 set progresscoords {0 0}
9146 adjustprogress
9147 notbusy checkout
9148 if {[catch {close $fd} err]} {
9149 error_popup $err
9150 }
c11ff120 9151 set oldmainid $mainheadid
08ba820f
PM
9152 set mainhead $newhead
9153 set mainheadid $newheadid
cdc8429c 9154 set viewmainheadid($curview) $newheadid
c11ff120 9155 redrawtags $oldmainid
08ba820f
PM
9156 redrawtags $newheadid
9157 selbyid $newheadid
6fb735ae
PM
9158 if {$showlocalchanges} {
9159 dodiffindex
10299152
PM
9160 }
9161}
9162
9163proc rmbranch {} {
e11f1233 9164 global headmenuid headmenuhead mainhead
b1054ac9 9165 global idheads
10299152
PM
9166
9167 set head $headmenuhead
9168 set id $headmenuid
00609463 9169 # this check shouldn't be needed any more...
10299152 9170 if {$head eq $mainhead} {
d990cedf 9171 error_popup [mc "Cannot delete the currently checked-out branch"]
10299152
PM
9172 return
9173 }
e11f1233 9174 set dheads [descheads $id]
d7b16113 9175 if {[llength $dheads] == 1 && $idheads($dheads) eq $head} {
10299152 9176 # the stuff on this branch isn't on any other branch
d990cedf
CS
9177 if {![confirm_popup [mc "The commits on branch %s aren't on any other\
9178 branch.\nReally delete branch %s?" $head $head]]} return
10299152
PM
9179 }
9180 nowbusy rmbranch
9181 update
9182 if {[catch {exec git branch -D $head} err]} {
9183 notbusy rmbranch
9184 error_popup $err
9185 return
9186 }
e11f1233 9187 removehead $id $head
ca6d8f58 9188 removedhead $id $head
10299152
PM
9189 redrawtags $id
9190 notbusy rmbranch
e11f1233 9191 dispneartags 0
887c996e
PM
9192 run refill_reflist
9193}
9194
9195# Display a list of tags and heads
9196proc showrefs {} {
d93f1713 9197 global showrefstop bgcolor fgcolor selectbgcolor NS
9c311b32 9198 global bglist fglist reflistfilter reflist maincursor
887c996e
PM
9199
9200 set top .showrefs
9201 set showrefstop $top
9202 if {[winfo exists $top]} {
9203 raise $top
9204 refill_reflist
9205 return
9206 }
d93f1713 9207 ttk_toplevel $top
d990cedf 9208 wm title $top [mc "Tags and heads: %s" [file tail [pwd]]]
e7d64008 9209 make_transient $top .
887c996e 9210 text $top.list -background $bgcolor -foreground $fgcolor \
9c311b32 9211 -selectbackground $selectbgcolor -font mainfont \
887c996e
PM
9212 -xscrollcommand "$top.xsb set" -yscrollcommand "$top.ysb set" \
9213 -width 30 -height 20 -cursor $maincursor \
9214 -spacing1 1 -spacing3 1 -state disabled
9215 $top.list tag configure highlight -background $selectbgcolor
9216 lappend bglist $top.list
9217 lappend fglist $top.list
d93f1713
PT
9218 ${NS}::scrollbar $top.ysb -command "$top.list yview" -orient vertical
9219 ${NS}::scrollbar $top.xsb -command "$top.list xview" -orient horizontal
887c996e
PM
9220 grid $top.list $top.ysb -sticky nsew
9221 grid $top.xsb x -sticky ew
d93f1713
PT
9222 ${NS}::frame $top.f
9223 ${NS}::label $top.f.l -text "[mc "Filter"]: "
9224 ${NS}::entry $top.f.e -width 20 -textvariable reflistfilter
887c996e
PM
9225 set reflistfilter "*"
9226 trace add variable reflistfilter write reflistfilter_change
9227 pack $top.f.e -side right -fill x -expand 1
9228 pack $top.f.l -side left
9229 grid $top.f - -sticky ew -pady 2
d93f1713 9230 ${NS}::button $top.close -command [list destroy $top] -text [mc "Close"]
76f15947 9231 bind $top <Key-Escape> [list destroy $top]
887c996e
PM
9232 grid $top.close -
9233 grid columnconfigure $top 0 -weight 1
9234 grid rowconfigure $top 0 -weight 1
9235 bind $top.list <1> {break}
9236 bind $top.list <B1-Motion> {break}
9237 bind $top.list <ButtonRelease-1> {sel_reflist %W %x %y; break}
9238 set reflist {}
9239 refill_reflist
9240}
9241
9242proc sel_reflist {w x y} {
9243 global showrefstop reflist headids tagids otherrefids
9244
9245 if {![winfo exists $showrefstop]} return
9246 set l [lindex [split [$w index "@$x,$y"] "."] 0]
9247 set ref [lindex $reflist [expr {$l-1}]]
9248 set n [lindex $ref 0]
9249 switch -- [lindex $ref 1] {
9250 "H" {selbyid $headids($n)}
9251 "T" {selbyid $tagids($n)}
9252 "o" {selbyid $otherrefids($n)}
9253 }
9254 $showrefstop.list tag add highlight $l.0 "$l.0 lineend"
9255}
9256
9257proc unsel_reflist {} {
9258 global showrefstop
9259
9260 if {![info exists showrefstop] || ![winfo exists $showrefstop]} return
9261 $showrefstop.list tag remove highlight 0.0 end
9262}
9263
9264proc reflistfilter_change {n1 n2 op} {
9265 global reflistfilter
9266
9267 after cancel refill_reflist
9268 after 200 refill_reflist
9269}
9270
9271proc refill_reflist {} {
9272 global reflist reflistfilter showrefstop headids tagids otherrefids
d375ef9b 9273 global curview
887c996e
PM
9274
9275 if {![info exists showrefstop] || ![winfo exists $showrefstop]} return
9276 set refs {}
9277 foreach n [array names headids] {
9278 if {[string match $reflistfilter $n]} {
7fcc92bf 9279 if {[commitinview $headids($n) $curview]} {
887c996e
PM
9280 lappend refs [list $n H]
9281 } else {
d375ef9b 9282 interestedin $headids($n) {run refill_reflist}
887c996e
PM
9283 }
9284 }
9285 }
9286 foreach n [array names tagids] {
9287 if {[string match $reflistfilter $n]} {
7fcc92bf 9288 if {[commitinview $tagids($n) $curview]} {
887c996e
PM
9289 lappend refs [list $n T]
9290 } else {
d375ef9b 9291 interestedin $tagids($n) {run refill_reflist}
887c996e
PM
9292 }
9293 }
9294 }
9295 foreach n [array names otherrefids] {
9296 if {[string match $reflistfilter $n]} {
7fcc92bf 9297 if {[commitinview $otherrefids($n) $curview]} {
887c996e
PM
9298 lappend refs [list $n o]
9299 } else {
d375ef9b 9300 interestedin $otherrefids($n) {run refill_reflist}
887c996e
PM
9301 }
9302 }
9303 }
9304 set refs [lsort -index 0 $refs]
9305 if {$refs eq $reflist} return
9306
9307 # Update the contents of $showrefstop.list according to the
9308 # differences between $reflist (old) and $refs (new)
9309 $showrefstop.list conf -state normal
9310 $showrefstop.list insert end "\n"
9311 set i 0
9312 set j 0
9313 while {$i < [llength $reflist] || $j < [llength $refs]} {
9314 if {$i < [llength $reflist]} {
9315 if {$j < [llength $refs]} {
9316 set cmp [string compare [lindex $reflist $i 0] \
9317 [lindex $refs $j 0]]
9318 if {$cmp == 0} {
9319 set cmp [string compare [lindex $reflist $i 1] \
9320 [lindex $refs $j 1]]
9321 }
9322 } else {
9323 set cmp -1
9324 }
9325 } else {
9326 set cmp 1
9327 }
9328 switch -- $cmp {
9329 -1 {
9330 $showrefstop.list delete "[expr {$j+1}].0" "[expr {$j+2}].0"
9331 incr i
9332 }
9333 0 {
9334 incr i
9335 incr j
9336 }
9337 1 {
9338 set l [expr {$j + 1}]
9339 $showrefstop.list image create $l.0 -align baseline \
9340 -image reficon-[lindex $refs $j 1] -padx 2
9341 $showrefstop.list insert $l.1 "[lindex $refs $j 0]\n"
9342 incr j
9343 }
9344 }
9345 }
9346 set reflist $refs
9347 # delete last newline
9348 $showrefstop.list delete end-2c end-1c
9349 $showrefstop.list conf -state disabled
10299152
PM
9350}
9351
b8ab2e17
PM
9352# Stuff for finding nearby tags
9353proc getallcommits {} {
5cd15b6b
PM
9354 global allcommits nextarc seeds allccache allcwait cachedarcs allcupdate
9355 global idheads idtags idotherrefs allparents tagobjid
f1d83ba3 9356
a69b2d1a 9357 if {![info exists allcommits]} {
a69b2d1a
PM
9358 set nextarc 0
9359 set allcommits 0
9360 set seeds {}
5cd15b6b
PM
9361 set allcwait 0
9362 set cachedarcs 0
9363 set allccache [file join [gitdir] "gitk.cache"]
9364 if {![catch {
9365 set f [open $allccache r]
9366 set allcwait 1
9367 getcache $f
9368 }]} return
a69b2d1a 9369 }
2d71bccc 9370
5cd15b6b
PM
9371 if {$allcwait} {
9372 return
9373 }
9374 set cmd [list | git rev-list --parents]
9375 set allcupdate [expr {$seeds ne {}}]
9376 if {!$allcupdate} {
9377 set ids "--all"
9378 } else {
9379 set refs [concat [array names idheads] [array names idtags] \
9380 [array names idotherrefs]]
9381 set ids {}
9382 set tagobjs {}
9383 foreach name [array names tagobjid] {
9384 lappend tagobjs $tagobjid($name)
9385 }
9386 foreach id [lsort -unique $refs] {
9387 if {![info exists allparents($id)] &&
9388 [lsearch -exact $tagobjs $id] < 0} {
9389 lappend ids $id
9390 }
9391 }
9392 if {$ids ne {}} {
9393 foreach id $seeds {
9394 lappend ids "^$id"
9395 }
9396 }
9397 }
9398 if {$ids ne {}} {
9399 set fd [open [concat $cmd $ids] r]
9400 fconfigure $fd -blocking 0
9401 incr allcommits
9402 nowbusy allcommits
9403 filerun $fd [list getallclines $fd]
9404 } else {
9405 dispneartags 0
2d71bccc 9406 }
e11f1233
PM
9407}
9408
9409# Since most commits have 1 parent and 1 child, we group strings of
9410# such commits into "arcs" joining branch/merge points (BMPs), which
9411# are commits that either don't have 1 parent or don't have 1 child.
9412#
9413# arcnos(id) - incoming arcs for BMP, arc we're on for other nodes
9414# arcout(id) - outgoing arcs for BMP
9415# arcids(a) - list of IDs on arc including end but not start
9416# arcstart(a) - BMP ID at start of arc
9417# arcend(a) - BMP ID at end of arc
9418# growing(a) - arc a is still growing
9419# arctags(a) - IDs out of arcids (excluding end) that have tags
9420# archeads(a) - IDs out of arcids (excluding end) that have heads
9421# The start of an arc is at the descendent end, so "incoming" means
9422# coming from descendents, and "outgoing" means going towards ancestors.
9423
9424proc getallclines {fd} {
5cd15b6b 9425 global allparents allchildren idtags idheads nextarc
e11f1233 9426 global arcnos arcids arctags arcout arcend arcstart archeads growing
5cd15b6b 9427 global seeds allcommits cachedarcs allcupdate
d93f1713 9428
e11f1233 9429 set nid 0
7eb3cb9c 9430 while {[incr nid] <= 1000 && [gets $fd line] >= 0} {
e11f1233
PM
9431 set id [lindex $line 0]
9432 if {[info exists allparents($id)]} {
9433 # seen it already
9434 continue
9435 }
5cd15b6b 9436 set cachedarcs 0
e11f1233
PM
9437 set olds [lrange $line 1 end]
9438 set allparents($id) $olds
9439 if {![info exists allchildren($id)]} {
9440 set allchildren($id) {}
9441 set arcnos($id) {}
9442 lappend seeds $id
9443 } else {
9444 set a $arcnos($id)
9445 if {[llength $olds] == 1 && [llength $a] == 1} {
9446 lappend arcids($a) $id
9447 if {[info exists idtags($id)]} {
9448 lappend arctags($a) $id
b8ab2e17 9449 }
e11f1233
PM
9450 if {[info exists idheads($id)]} {
9451 lappend archeads($a) $id
9452 }
9453 if {[info exists allparents($olds)]} {
9454 # seen parent already
9455 if {![info exists arcout($olds)]} {
9456 splitarc $olds
9457 }
9458 lappend arcids($a) $olds
9459 set arcend($a) $olds
9460 unset growing($a)
9461 }
9462 lappend allchildren($olds) $id
9463 lappend arcnos($olds) $a
9464 continue
9465 }
9466 }
e11f1233
PM
9467 foreach a $arcnos($id) {
9468 lappend arcids($a) $id
9469 set arcend($a) $id
9470 unset growing($a)
9471 }
9472
9473 set ao {}
9474 foreach p $olds {
9475 lappend allchildren($p) $id
9476 set a [incr nextarc]
9477 set arcstart($a) $id
9478 set archeads($a) {}
9479 set arctags($a) {}
9480 set archeads($a) {}
9481 set arcids($a) {}
9482 lappend ao $a
9483 set growing($a) 1
9484 if {[info exists allparents($p)]} {
9485 # seen it already, may need to make a new branch
9486 if {![info exists arcout($p)]} {
9487 splitarc $p
9488 }
9489 lappend arcids($a) $p
9490 set arcend($a) $p
9491 unset growing($a)
9492 }
9493 lappend arcnos($p) $a
9494 }
9495 set arcout($id) $ao
f1d83ba3 9496 }
f3326b66
PM
9497 if {$nid > 0} {
9498 global cached_dheads cached_dtags cached_atags
9499 catch {unset cached_dheads}
9500 catch {unset cached_dtags}
9501 catch {unset cached_atags}
9502 }
7eb3cb9c
PM
9503 if {![eof $fd]} {
9504 return [expr {$nid >= 1000? 2: 1}]
9505 }
5cd15b6b
PM
9506 set cacheok 1
9507 if {[catch {
9508 fconfigure $fd -blocking 1
9509 close $fd
9510 } err]} {
9511 # got an error reading the list of commits
9512 # if we were updating, try rereading the whole thing again
9513 if {$allcupdate} {
9514 incr allcommits -1
9515 dropcache $err
9516 return
9517 }
d990cedf 9518 error_popup "[mc "Error reading commit topology information;\
5cd15b6b 9519 branch and preceding/following tag information\
d990cedf 9520 will be incomplete."]\n($err)"
5cd15b6b
PM
9521 set cacheok 0
9522 }
e11f1233
PM
9523 if {[incr allcommits -1] == 0} {
9524 notbusy allcommits
5cd15b6b
PM
9525 if {$cacheok} {
9526 run savecache
9527 }
e11f1233
PM
9528 }
9529 dispneartags 0
7eb3cb9c 9530 return 0
b8ab2e17
PM
9531}
9532
e11f1233
PM
9533proc recalcarc {a} {
9534 global arctags archeads arcids idtags idheads
b8ab2e17 9535
e11f1233
PM
9536 set at {}
9537 set ah {}
9538 foreach id [lrange $arcids($a) 0 end-1] {
9539 if {[info exists idtags($id)]} {
9540 lappend at $id
9541 }
9542 if {[info exists idheads($id)]} {
9543 lappend ah $id
b8ab2e17 9544 }
f1d83ba3 9545 }
e11f1233
PM
9546 set arctags($a) $at
9547 set archeads($a) $ah
b8ab2e17
PM
9548}
9549
e11f1233 9550proc splitarc {p} {
5cd15b6b 9551 global arcnos arcids nextarc arctags archeads idtags idheads
e11f1233 9552 global arcstart arcend arcout allparents growing
cec7bece 9553
e11f1233
PM
9554 set a $arcnos($p)
9555 if {[llength $a] != 1} {
9556 puts "oops splitarc called but [llength $a] arcs already"
9557 return
9558 }
9559 set a [lindex $a 0]
9560 set i [lsearch -exact $arcids($a) $p]
9561 if {$i < 0} {
9562 puts "oops splitarc $p not in arc $a"
9563 return
9564 }
9565 set na [incr nextarc]
9566 if {[info exists arcend($a)]} {
9567 set arcend($na) $arcend($a)
9568 } else {
9569 set l [lindex $allparents([lindex $arcids($a) end]) 0]
9570 set j [lsearch -exact $arcnos($l) $a]
9571 set arcnos($l) [lreplace $arcnos($l) $j $j $na]
9572 }
9573 set tail [lrange $arcids($a) [expr {$i+1}] end]
9574 set arcids($a) [lrange $arcids($a) 0 $i]
9575 set arcend($a) $p
9576 set arcstart($na) $p
9577 set arcout($p) $na
9578 set arcids($na) $tail
9579 if {[info exists growing($a)]} {
9580 set growing($na) 1
9581 unset growing($a)
9582 }
e11f1233
PM
9583
9584 foreach id $tail {
9585 if {[llength $arcnos($id)] == 1} {
9586 set arcnos($id) $na
cec7bece 9587 } else {
e11f1233
PM
9588 set j [lsearch -exact $arcnos($id) $a]
9589 set arcnos($id) [lreplace $arcnos($id) $j $j $na]
cec7bece 9590 }
e11f1233
PM
9591 }
9592
9593 # reconstruct tags and heads lists
9594 if {$arctags($a) ne {} || $archeads($a) ne {}} {
9595 recalcarc $a
9596 recalcarc $na
9597 } else {
9598 set arctags($na) {}
9599 set archeads($na) {}
9600 }
9601}
9602
9603# Update things for a new commit added that is a child of one
9604# existing commit. Used when cherry-picking.
9605proc addnewchild {id p} {
5cd15b6b 9606 global allparents allchildren idtags nextarc
e11f1233 9607 global arcnos arcids arctags arcout arcend arcstart archeads growing
719c2b9d 9608 global seeds allcommits
e11f1233 9609
3ebba3c7 9610 if {![info exists allcommits] || ![info exists arcnos($p)]} return
e11f1233
PM
9611 set allparents($id) [list $p]
9612 set allchildren($id) {}
9613 set arcnos($id) {}
9614 lappend seeds $id
e11f1233
PM
9615 lappend allchildren($p) $id
9616 set a [incr nextarc]
9617 set arcstart($a) $id
9618 set archeads($a) {}
9619 set arctags($a) {}
9620 set arcids($a) [list $p]
9621 set arcend($a) $p
9622 if {![info exists arcout($p)]} {
9623 splitarc $p
9624 }
9625 lappend arcnos($p) $a
9626 set arcout($id) [list $a]
9627}
9628
5cd15b6b
PM
9629# This implements a cache for the topology information.
9630# The cache saves, for each arc, the start and end of the arc,
9631# the ids on the arc, and the outgoing arcs from the end.
9632proc readcache {f} {
9633 global arcnos arcids arcout arcstart arcend arctags archeads nextarc
9634 global idtags idheads allparents cachedarcs possible_seeds seeds growing
9635 global allcwait
9636
9637 set a $nextarc
9638 set lim $cachedarcs
9639 if {$lim - $a > 500} {
9640 set lim [expr {$a + 500}]
9641 }
9642 if {[catch {
9643 if {$a == $lim} {
9644 # finish reading the cache and setting up arctags, etc.
9645 set line [gets $f]
9646 if {$line ne "1"} {error "bad final version"}
9647 close $f
9648 foreach id [array names idtags] {
9649 if {[info exists arcnos($id)] && [llength $arcnos($id)] == 1 &&
9650 [llength $allparents($id)] == 1} {
9651 set a [lindex $arcnos($id) 0]
9652 if {$arctags($a) eq {}} {
9653 recalcarc $a
9654 }
9655 }
9656 }
9657 foreach id [array names idheads] {
9658 if {[info exists arcnos($id)] && [llength $arcnos($id)] == 1 &&
9659 [llength $allparents($id)] == 1} {
9660 set a [lindex $arcnos($id) 0]
9661 if {$archeads($a) eq {}} {
9662 recalcarc $a
9663 }
9664 }
9665 }
9666 foreach id [lsort -unique $possible_seeds] {
9667 if {$arcnos($id) eq {}} {
9668 lappend seeds $id
9669 }
9670 }
9671 set allcwait 0
9672 } else {
9673 while {[incr a] <= $lim} {
9674 set line [gets $f]
9675 if {[llength $line] != 3} {error "bad line"}
9676 set s [lindex $line 0]
9677 set arcstart($a) $s
9678 lappend arcout($s) $a
9679 if {![info exists arcnos($s)]} {
9680 lappend possible_seeds $s
9681 set arcnos($s) {}
9682 }
9683 set e [lindex $line 1]
9684 if {$e eq {}} {
9685 set growing($a) 1
9686 } else {
9687 set arcend($a) $e
9688 if {![info exists arcout($e)]} {
9689 set arcout($e) {}
9690 }
9691 }
9692 set arcids($a) [lindex $line 2]
9693 foreach id $arcids($a) {
9694 lappend allparents($s) $id
9695 set s $id
9696 lappend arcnos($id) $a
9697 }
9698 if {![info exists allparents($s)]} {
9699 set allparents($s) {}
9700 }
9701 set arctags($a) {}
9702 set archeads($a) {}
9703 }
9704 set nextarc [expr {$a - 1}]
9705 }
9706 } err]} {
9707 dropcache $err
9708 return 0
9709 }
9710 if {!$allcwait} {
9711 getallcommits
9712 }
9713 return $allcwait
9714}
9715
9716proc getcache {f} {
9717 global nextarc cachedarcs possible_seeds
9718
9719 if {[catch {
9720 set line [gets $f]
9721 if {[llength $line] != 2 || [lindex $line 0] ne "1"} {error "bad version"}
9722 # make sure it's an integer
9723 set cachedarcs [expr {int([lindex $line 1])}]
9724 if {$cachedarcs < 0} {error "bad number of arcs"}
9725 set nextarc 0
9726 set possible_seeds {}
9727 run readcache $f
9728 } err]} {
9729 dropcache $err
9730 }
9731 return 0
9732}
9733
9734proc dropcache {err} {
9735 global allcwait nextarc cachedarcs seeds
9736
9737 #puts "dropping cache ($err)"
9738 foreach v {arcnos arcout arcids arcstart arcend growing \
9739 arctags archeads allparents allchildren} {
9740 global $v
9741 catch {unset $v}
9742 }
9743 set allcwait 0
9744 set nextarc 0
9745 set cachedarcs 0
9746 set seeds {}
9747 getallcommits
9748}
9749
9750proc writecache {f} {
9751 global cachearc cachedarcs allccache
9752 global arcstart arcend arcnos arcids arcout
9753
9754 set a $cachearc
9755 set lim $cachedarcs
9756 if {$lim - $a > 1000} {
9757 set lim [expr {$a + 1000}]
9758 }
9759 if {[catch {
9760 while {[incr a] <= $lim} {
9761 if {[info exists arcend($a)]} {
9762 puts $f [list $arcstart($a) $arcend($a) $arcids($a)]
9763 } else {
9764 puts $f [list $arcstart($a) {} $arcids($a)]
9765 }
9766 }
9767 } err]} {
9768 catch {close $f}
9769 catch {file delete $allccache}
9770 #puts "writing cache failed ($err)"
9771 return 0
9772 }
9773 set cachearc [expr {$a - 1}]
9774 if {$a > $cachedarcs} {
9775 puts $f "1"
9776 close $f
9777 return 0
9778 }
9779 return 1
9780}
9781
9782proc savecache {} {
9783 global nextarc cachedarcs cachearc allccache
9784
9785 if {$nextarc == $cachedarcs} return
9786 set cachearc 0
9787 set cachedarcs $nextarc
9788 catch {
9789 set f [open $allccache w]
9790 puts $f [list 1 $cachedarcs]
9791 run writecache $f
9792 }
9793}
9794
e11f1233
PM
9795# Returns 1 if a is an ancestor of b, -1 if b is an ancestor of a,
9796# or 0 if neither is true.
9797proc anc_or_desc {a b} {
9798 global arcout arcstart arcend arcnos cached_isanc
9799
9800 if {$arcnos($a) eq $arcnos($b)} {
9801 # Both are on the same arc(s); either both are the same BMP,
9802 # or if one is not a BMP, the other is also not a BMP or is
9803 # the BMP at end of the arc (and it only has 1 incoming arc).
69c0b5d2
PM
9804 # Or both can be BMPs with no incoming arcs.
9805 if {$a eq $b || $arcnos($a) eq {}} {
e11f1233 9806 return 0
cec7bece 9807 }
e11f1233
PM
9808 # assert {[llength $arcnos($a)] == 1}
9809 set arc [lindex $arcnos($a) 0]
9810 set i [lsearch -exact $arcids($arc) $a]
9811 set j [lsearch -exact $arcids($arc) $b]
9812 if {$i < 0 || $i > $j} {
9813 return 1
9814 } else {
9815 return -1
cec7bece
PM
9816 }
9817 }
e11f1233
PM
9818
9819 if {![info exists arcout($a)]} {
9820 set arc [lindex $arcnos($a) 0]
9821 if {[info exists arcend($arc)]} {
9822 set aend $arcend($arc)
9823 } else {
9824 set aend {}
cec7bece 9825 }
e11f1233
PM
9826 set a $arcstart($arc)
9827 } else {
9828 set aend $a
9829 }
9830 if {![info exists arcout($b)]} {
9831 set arc [lindex $arcnos($b) 0]
9832 if {[info exists arcend($arc)]} {
9833 set bend $arcend($arc)
9834 } else {
9835 set bend {}
cec7bece 9836 }
e11f1233
PM
9837 set b $arcstart($arc)
9838 } else {
9839 set bend $b
cec7bece 9840 }
e11f1233
PM
9841 if {$a eq $bend} {
9842 return 1
9843 }
9844 if {$b eq $aend} {
9845 return -1
9846 }
9847 if {[info exists cached_isanc($a,$bend)]} {
9848 if {$cached_isanc($a,$bend)} {
9849 return 1
9850 }
9851 }
9852 if {[info exists cached_isanc($b,$aend)]} {
9853 if {$cached_isanc($b,$aend)} {
9854 return -1
9855 }
9856 if {[info exists cached_isanc($a,$bend)]} {
9857 return 0
9858 }
cec7bece 9859 }
cec7bece 9860
e11f1233
PM
9861 set todo [list $a $b]
9862 set anc($a) a
9863 set anc($b) b
9864 for {set i 0} {$i < [llength $todo]} {incr i} {
9865 set x [lindex $todo $i]
9866 if {$anc($x) eq {}} {
9867 continue
9868 }
9869 foreach arc $arcnos($x) {
9870 set xd $arcstart($arc)
9871 if {$xd eq $bend} {
9872 set cached_isanc($a,$bend) 1
9873 set cached_isanc($b,$aend) 0
9874 return 1
9875 } elseif {$xd eq $aend} {
9876 set cached_isanc($b,$aend) 1
9877 set cached_isanc($a,$bend) 0
9878 return -1
9879 }
9880 if {![info exists anc($xd)]} {
9881 set anc($xd) $anc($x)
9882 lappend todo $xd
9883 } elseif {$anc($xd) ne $anc($x)} {
9884 set anc($xd) {}
9885 }
9886 }
9887 }
9888 set cached_isanc($a,$bend) 0
9889 set cached_isanc($b,$aend) 0
9890 return 0
9891}
b8ab2e17 9892
e11f1233
PM
9893# This identifies whether $desc has an ancestor that is
9894# a growing tip of the graph and which is not an ancestor of $anc
9895# and returns 0 if so and 1 if not.
9896# If we subsequently discover a tag on such a growing tip, and that
9897# turns out to be a descendent of $anc (which it could, since we
9898# don't necessarily see children before parents), then $desc
9899# isn't a good choice to display as a descendent tag of
9900# $anc (since it is the descendent of another tag which is
9901# a descendent of $anc). Similarly, $anc isn't a good choice to
9902# display as a ancestor tag of $desc.
9903#
9904proc is_certain {desc anc} {
9905 global arcnos arcout arcstart arcend growing problems
9906
9907 set certain {}
9908 if {[llength $arcnos($anc)] == 1} {
9909 # tags on the same arc are certain
9910 if {$arcnos($desc) eq $arcnos($anc)} {
9911 return 1
b8ab2e17 9912 }
e11f1233
PM
9913 if {![info exists arcout($anc)]} {
9914 # if $anc is partway along an arc, use the start of the arc instead
9915 set a [lindex $arcnos($anc) 0]
9916 set anc $arcstart($a)
b8ab2e17 9917 }
e11f1233
PM
9918 }
9919 if {[llength $arcnos($desc)] > 1 || [info exists arcout($desc)]} {
9920 set x $desc
9921 } else {
9922 set a [lindex $arcnos($desc) 0]
9923 set x $arcend($a)
9924 }
9925 if {$x == $anc} {
9926 return 1
9927 }
9928 set anclist [list $x]
9929 set dl($x) 1
9930 set nnh 1
9931 set ngrowanc 0
9932 for {set i 0} {$i < [llength $anclist] && ($nnh > 0 || $ngrowanc > 0)} {incr i} {
9933 set x [lindex $anclist $i]
9934 if {$dl($x)} {
9935 incr nnh -1
9936 }
9937 set done($x) 1
9938 foreach a $arcout($x) {
9939 if {[info exists growing($a)]} {
9940 if {![info exists growanc($x)] && $dl($x)} {
9941 set growanc($x) 1
9942 incr ngrowanc
9943 }
9944 } else {
9945 set y $arcend($a)
9946 if {[info exists dl($y)]} {
9947 if {$dl($y)} {
9948 if {!$dl($x)} {
9949 set dl($y) 0
9950 if {![info exists done($y)]} {
9951 incr nnh -1
9952 }
9953 if {[info exists growanc($x)]} {
9954 incr ngrowanc -1
9955 }
9956 set xl [list $y]
9957 for {set k 0} {$k < [llength $xl]} {incr k} {
9958 set z [lindex $xl $k]
9959 foreach c $arcout($z) {
9960 if {[info exists arcend($c)]} {
9961 set v $arcend($c)
9962 if {[info exists dl($v)] && $dl($v)} {
9963 set dl($v) 0
9964 if {![info exists done($v)]} {
9965 incr nnh -1
9966 }
9967 if {[info exists growanc($v)]} {
9968 incr ngrowanc -1
9969 }
9970 lappend xl $v
9971 }
9972 }
9973 }
9974 }
9975 }
9976 }
9977 } elseif {$y eq $anc || !$dl($x)} {
9978 set dl($y) 0
9979 lappend anclist $y
9980 } else {
9981 set dl($y) 1
9982 lappend anclist $y
9983 incr nnh
9984 }
9985 }
b8ab2e17
PM
9986 }
9987 }
e11f1233
PM
9988 foreach x [array names growanc] {
9989 if {$dl($x)} {
9990 return 0
b8ab2e17 9991 }
7eb3cb9c 9992 return 0
b8ab2e17 9993 }
e11f1233 9994 return 1
b8ab2e17
PM
9995}
9996
e11f1233
PM
9997proc validate_arctags {a} {
9998 global arctags idtags
b8ab2e17 9999
e11f1233
PM
10000 set i -1
10001 set na $arctags($a)
10002 foreach id $arctags($a) {
10003 incr i
10004 if {![info exists idtags($id)]} {
10005 set na [lreplace $na $i $i]
10006 incr i -1
10007 }
10008 }
10009 set arctags($a) $na
10010}
10011
10012proc validate_archeads {a} {
10013 global archeads idheads
10014
10015 set i -1
10016 set na $archeads($a)
10017 foreach id $archeads($a) {
10018 incr i
10019 if {![info exists idheads($id)]} {
10020 set na [lreplace $na $i $i]
10021 incr i -1
10022 }
10023 }
10024 set archeads($a) $na
10025}
10026
10027# Return the list of IDs that have tags that are descendents of id,
10028# ignoring IDs that are descendents of IDs already reported.
10029proc desctags {id} {
10030 global arcnos arcstart arcids arctags idtags allparents
10031 global growing cached_dtags
10032
10033 if {![info exists allparents($id)]} {
10034 return {}
10035 }
10036 set t1 [clock clicks -milliseconds]
10037 set argid $id
10038 if {[llength $arcnos($id)] == 1 && [llength $allparents($id)] == 1} {
10039 # part-way along an arc; check that arc first
10040 set a [lindex $arcnos($id) 0]
10041 if {$arctags($a) ne {}} {
10042 validate_arctags $a
10043 set i [lsearch -exact $arcids($a) $id]
10044 set tid {}
10045 foreach t $arctags($a) {
10046 set j [lsearch -exact $arcids($a) $t]
10047 if {$j >= $i} break
10048 set tid $t
b8ab2e17 10049 }
e11f1233
PM
10050 if {$tid ne {}} {
10051 return $tid
b8ab2e17
PM
10052 }
10053 }
e11f1233
PM
10054 set id $arcstart($a)
10055 if {[info exists idtags($id)]} {
10056 return $id
10057 }
10058 }
10059 if {[info exists cached_dtags($id)]} {
10060 return $cached_dtags($id)
10061 }
10062
10063 set origid $id
10064 set todo [list $id]
10065 set queued($id) 1
10066 set nc 1
10067 for {set i 0} {$i < [llength $todo] && $nc > 0} {incr i} {
10068 set id [lindex $todo $i]
10069 set done($id) 1
10070 set ta [info exists hastaggedancestor($id)]
10071 if {!$ta} {
10072 incr nc -1
10073 }
10074 # ignore tags on starting node
10075 if {!$ta && $i > 0} {
10076 if {[info exists idtags($id)]} {
10077 set tagloc($id) $id
10078 set ta 1
10079 } elseif {[info exists cached_dtags($id)]} {
10080 set tagloc($id) $cached_dtags($id)
10081 set ta 1
10082 }
10083 }
10084 foreach a $arcnos($id) {
10085 set d $arcstart($a)
10086 if {!$ta && $arctags($a) ne {}} {
10087 validate_arctags $a
10088 if {$arctags($a) ne {}} {
10089 lappend tagloc($id) [lindex $arctags($a) end]
10090 }
10091 }
10092 if {$ta || $arctags($a) ne {}} {
10093 set tomark [list $d]
10094 for {set j 0} {$j < [llength $tomark]} {incr j} {
10095 set dd [lindex $tomark $j]
10096 if {![info exists hastaggedancestor($dd)]} {
10097 if {[info exists done($dd)]} {
10098 foreach b $arcnos($dd) {
10099 lappend tomark $arcstart($b)
10100 }
10101 if {[info exists tagloc($dd)]} {
10102 unset tagloc($dd)
10103 }
10104 } elseif {[info exists queued($dd)]} {
10105 incr nc -1
10106 }
10107 set hastaggedancestor($dd) 1
10108 }
10109 }
10110 }
10111 if {![info exists queued($d)]} {
10112 lappend todo $d
10113 set queued($d) 1
10114 if {![info exists hastaggedancestor($d)]} {
10115 incr nc
10116 }
10117 }
b8ab2e17 10118 }
f1d83ba3 10119 }
e11f1233
PM
10120 set tags {}
10121 foreach id [array names tagloc] {
10122 if {![info exists hastaggedancestor($id)]} {
10123 foreach t $tagloc($id) {
10124 if {[lsearch -exact $tags $t] < 0} {
10125 lappend tags $t
10126 }
10127 }
10128 }
10129 }
10130 set t2 [clock clicks -milliseconds]
10131 set loopix $i
f1d83ba3 10132
e11f1233
PM
10133 # remove tags that are descendents of other tags
10134 for {set i 0} {$i < [llength $tags]} {incr i} {
10135 set a [lindex $tags $i]
10136 for {set j 0} {$j < $i} {incr j} {
10137 set b [lindex $tags $j]
10138 set r [anc_or_desc $a $b]
10139 if {$r == 1} {
10140 set tags [lreplace $tags $j $j]
10141 incr j -1
10142 incr i -1
10143 } elseif {$r == -1} {
10144 set tags [lreplace $tags $i $i]
10145 incr i -1
10146 break
ceadfe90
PM
10147 }
10148 }
10149 }
10150
e11f1233
PM
10151 if {[array names growing] ne {}} {
10152 # graph isn't finished, need to check if any tag could get
10153 # eclipsed by another tag coming later. Simply ignore any
10154 # tags that could later get eclipsed.
10155 set ctags {}
10156 foreach t $tags {
10157 if {[is_certain $t $origid]} {
10158 lappend ctags $t
10159 }
ceadfe90 10160 }
e11f1233
PM
10161 if {$tags eq $ctags} {
10162 set cached_dtags($origid) $tags
10163 } else {
10164 set tags $ctags
ceadfe90 10165 }
e11f1233
PM
10166 } else {
10167 set cached_dtags($origid) $tags
10168 }
10169 set t3 [clock clicks -milliseconds]
10170 if {0 && $t3 - $t1 >= 100} {
10171 puts "iterating descendents ($loopix/[llength $todo] nodes) took\
10172 [expr {$t2-$t1}]+[expr {$t3-$t2}]ms, $nc candidates left"
ceadfe90 10173 }
e11f1233
PM
10174 return $tags
10175}
ceadfe90 10176
e11f1233
PM
10177proc anctags {id} {
10178 global arcnos arcids arcout arcend arctags idtags allparents
10179 global growing cached_atags
10180
10181 if {![info exists allparents($id)]} {
10182 return {}
10183 }
10184 set t1 [clock clicks -milliseconds]
10185 set argid $id
10186 if {[llength $arcnos($id)] == 1 && [llength $allparents($id)] == 1} {
10187 # part-way along an arc; check that arc first
10188 set a [lindex $arcnos($id) 0]
10189 if {$arctags($a) ne {}} {
10190 validate_arctags $a
10191 set i [lsearch -exact $arcids($a) $id]
10192 foreach t $arctags($a) {
10193 set j [lsearch -exact $arcids($a) $t]
10194 if {$j > $i} {
10195 return $t
10196 }
10197 }
ceadfe90 10198 }
e11f1233
PM
10199 if {![info exists arcend($a)]} {
10200 return {}
10201 }
10202 set id $arcend($a)
10203 if {[info exists idtags($id)]} {
10204 return $id
10205 }
10206 }
10207 if {[info exists cached_atags($id)]} {
10208 return $cached_atags($id)
10209 }
10210
10211 set origid $id
10212 set todo [list $id]
10213 set queued($id) 1
10214 set taglist {}
10215 set nc 1
10216 for {set i 0} {$i < [llength $todo] && $nc > 0} {incr i} {
10217 set id [lindex $todo $i]
10218 set done($id) 1
10219 set td [info exists hastaggeddescendent($id)]
10220 if {!$td} {
10221 incr nc -1
10222 }
10223 # ignore tags on starting node
10224 if {!$td && $i > 0} {
10225 if {[info exists idtags($id)]} {
10226 set tagloc($id) $id
10227 set td 1
10228 } elseif {[info exists cached_atags($id)]} {
10229 set tagloc($id) $cached_atags($id)
10230 set td 1
10231 }
10232 }
10233 foreach a $arcout($id) {
10234 if {!$td && $arctags($a) ne {}} {
10235 validate_arctags $a
10236 if {$arctags($a) ne {}} {
10237 lappend tagloc($id) [lindex $arctags($a) 0]
10238 }
10239 }
10240 if {![info exists arcend($a)]} continue
10241 set d $arcend($a)
10242 if {$td || $arctags($a) ne {}} {
10243 set tomark [list $d]
10244 for {set j 0} {$j < [llength $tomark]} {incr j} {
10245 set dd [lindex $tomark $j]
10246 if {![info exists hastaggeddescendent($dd)]} {
10247 if {[info exists done($dd)]} {
10248 foreach b $arcout($dd) {
10249 if {[info exists arcend($b)]} {
10250 lappend tomark $arcend($b)
10251 }
10252 }
10253 if {[info exists tagloc($dd)]} {
10254 unset tagloc($dd)
10255 }
10256 } elseif {[info exists queued($dd)]} {
10257 incr nc -1
10258 }
10259 set hastaggeddescendent($dd) 1
10260 }
10261 }
10262 }
10263 if {![info exists queued($d)]} {
10264 lappend todo $d
10265 set queued($d) 1
10266 if {![info exists hastaggeddescendent($d)]} {
10267 incr nc
10268 }
10269 }
10270 }
10271 }
10272 set t2 [clock clicks -milliseconds]
10273 set loopix $i
10274 set tags {}
10275 foreach id [array names tagloc] {
10276 if {![info exists hastaggeddescendent($id)]} {
10277 foreach t $tagloc($id) {
10278 if {[lsearch -exact $tags $t] < 0} {
10279 lappend tags $t
10280 }
10281 }
ceadfe90
PM
10282 }
10283 }
ceadfe90 10284
e11f1233
PM
10285 # remove tags that are ancestors of other tags
10286 for {set i 0} {$i < [llength $tags]} {incr i} {
10287 set a [lindex $tags $i]
10288 for {set j 0} {$j < $i} {incr j} {
10289 set b [lindex $tags $j]
10290 set r [anc_or_desc $a $b]
10291 if {$r == -1} {
10292 set tags [lreplace $tags $j $j]
10293 incr j -1
10294 incr i -1
10295 } elseif {$r == 1} {
10296 set tags [lreplace $tags $i $i]
10297 incr i -1
10298 break
10299 }
10300 }
10301 }
10302
10303 if {[array names growing] ne {}} {
10304 # graph isn't finished, need to check if any tag could get
10305 # eclipsed by another tag coming later. Simply ignore any
10306 # tags that could later get eclipsed.
10307 set ctags {}
10308 foreach t $tags {
10309 if {[is_certain $origid $t]} {
10310 lappend ctags $t
10311 }
10312 }
10313 if {$tags eq $ctags} {
10314 set cached_atags($origid) $tags
10315 } else {
10316 set tags $ctags
d6ac1a86 10317 }
e11f1233
PM
10318 } else {
10319 set cached_atags($origid) $tags
10320 }
10321 set t3 [clock clicks -milliseconds]
10322 if {0 && $t3 - $t1 >= 100} {
10323 puts "iterating ancestors ($loopix/[llength $todo] nodes) took\
10324 [expr {$t2-$t1}]+[expr {$t3-$t2}]ms, $nc candidates left"
d6ac1a86 10325 }
e11f1233 10326 return $tags
d6ac1a86
PM
10327}
10328
e11f1233
PM
10329# Return the list of IDs that have heads that are descendents of id,
10330# including id itself if it has a head.
10331proc descheads {id} {
10332 global arcnos arcstart arcids archeads idheads cached_dheads
10333 global allparents
ca6d8f58 10334
e11f1233
PM
10335 if {![info exists allparents($id)]} {
10336 return {}
10337 }
f3326b66 10338 set aret {}
e11f1233
PM
10339 if {[llength $arcnos($id)] == 1 && [llength $allparents($id)] == 1} {
10340 # part-way along an arc; check it first
10341 set a [lindex $arcnos($id) 0]
10342 if {$archeads($a) ne {}} {
10343 validate_archeads $a
10344 set i [lsearch -exact $arcids($a) $id]
10345 foreach t $archeads($a) {
10346 set j [lsearch -exact $arcids($a) $t]
10347 if {$j > $i} break
f3326b66 10348 lappend aret $t
e11f1233 10349 }
ca6d8f58 10350 }
e11f1233 10351 set id $arcstart($a)
ca6d8f58 10352 }
e11f1233
PM
10353 set origid $id
10354 set todo [list $id]
10355 set seen($id) 1
f3326b66 10356 set ret {}
e11f1233
PM
10357 for {set i 0} {$i < [llength $todo]} {incr i} {
10358 set id [lindex $todo $i]
10359 if {[info exists cached_dheads($id)]} {
10360 set ret [concat $ret $cached_dheads($id)]
10361 } else {
10362 if {[info exists idheads($id)]} {
10363 lappend ret $id
10364 }
10365 foreach a $arcnos($id) {
10366 if {$archeads($a) ne {}} {
706d6c3e
PM
10367 validate_archeads $a
10368 if {$archeads($a) ne {}} {
10369 set ret [concat $ret $archeads($a)]
10370 }
e11f1233
PM
10371 }
10372 set d $arcstart($a)
10373 if {![info exists seen($d)]} {
10374 lappend todo $d
10375 set seen($d) 1
10376 }
10377 }
10299152 10378 }
10299152 10379 }
e11f1233
PM
10380 set ret [lsort -unique $ret]
10381 set cached_dheads($origid) $ret
f3326b66 10382 return [concat $ret $aret]
10299152
PM
10383}
10384
e11f1233
PM
10385proc addedtag {id} {
10386 global arcnos arcout cached_dtags cached_atags
ca6d8f58 10387
e11f1233
PM
10388 if {![info exists arcnos($id)]} return
10389 if {![info exists arcout($id)]} {
10390 recalcarc [lindex $arcnos($id) 0]
ca6d8f58 10391 }
e11f1233
PM
10392 catch {unset cached_dtags}
10393 catch {unset cached_atags}
ca6d8f58
PM
10394}
10395
e11f1233
PM
10396proc addedhead {hid head} {
10397 global arcnos arcout cached_dheads
10398
10399 if {![info exists arcnos($hid)]} return
10400 if {![info exists arcout($hid)]} {
10401 recalcarc [lindex $arcnos($hid) 0]
10402 }
10403 catch {unset cached_dheads}
10404}
10405
10406proc removedhead {hid head} {
10407 global cached_dheads
10408
10409 catch {unset cached_dheads}
10410}
10411
10412proc movedhead {hid head} {
10413 global arcnos arcout cached_dheads
cec7bece 10414
e11f1233
PM
10415 if {![info exists arcnos($hid)]} return
10416 if {![info exists arcout($hid)]} {
10417 recalcarc [lindex $arcnos($hid) 0]
cec7bece 10418 }
e11f1233
PM
10419 catch {unset cached_dheads}
10420}
10421
10422proc changedrefs {} {
10423 global cached_dheads cached_dtags cached_atags
10424 global arctags archeads arcnos arcout idheads idtags
10425
10426 foreach id [concat [array names idheads] [array names idtags]] {
10427 if {[info exists arcnos($id)] && ![info exists arcout($id)]} {
10428 set a [lindex $arcnos($id) 0]
10429 if {![info exists donearc($a)]} {
10430 recalcarc $a
10431 set donearc($a) 1
10432 }
cec7bece
PM
10433 }
10434 }
e11f1233
PM
10435 catch {unset cached_dtags}
10436 catch {unset cached_atags}
10437 catch {unset cached_dheads}
cec7bece
PM
10438}
10439
f1d83ba3 10440proc rereadrefs {} {
fc2a256f 10441 global idtags idheads idotherrefs mainheadid
f1d83ba3
PM
10442
10443 set refids [concat [array names idtags] \
10444 [array names idheads] [array names idotherrefs]]
10445 foreach id $refids {
10446 if {![info exists ref($id)]} {
10447 set ref($id) [listrefs $id]
10448 }
10449 }
fc2a256f 10450 set oldmainhead $mainheadid
f1d83ba3 10451 readrefs
cec7bece 10452 changedrefs
f1d83ba3
PM
10453 set refids [lsort -unique [concat $refids [array names idtags] \
10454 [array names idheads] [array names idotherrefs]]]
10455 foreach id $refids {
10456 set v [listrefs $id]
c11ff120 10457 if {![info exists ref($id)] || $ref($id) != $v} {
f1d83ba3
PM
10458 redrawtags $id
10459 }
10460 }
c11ff120
PM
10461 if {$oldmainhead ne $mainheadid} {
10462 redrawtags $oldmainhead
10463 redrawtags $mainheadid
10464 }
887c996e 10465 run refill_reflist
f1d83ba3
PM
10466}
10467
2e1ded44
JH
10468proc listrefs {id} {
10469 global idtags idheads idotherrefs
10470
10471 set x {}
10472 if {[info exists idtags($id)]} {
10473 set x $idtags($id)
10474 }
10475 set y {}
10476 if {[info exists idheads($id)]} {
10477 set y $idheads($id)
10478 }
10479 set z {}
10480 if {[info exists idotherrefs($id)]} {
10481 set z $idotherrefs($id)
10482 }
10483 return [list $x $y $z]
10484}
10485
106288cb 10486proc showtag {tag isnew} {
62d3ea65 10487 global ctext tagcontents tagids linknum tagobjid
106288cb
PM
10488
10489 if {$isnew} {
354af6bd 10490 addtohistory [list showtag $tag 0] savectextpos
106288cb
PM
10491 }
10492 $ctext conf -state normal
3ea06f9f 10493 clear_ctext
32f1b3e4 10494 settabs 0
106288cb 10495 set linknum 0
62d3ea65
PM
10496 if {![info exists tagcontents($tag)]} {
10497 catch {
fcacf489 10498 set tagcontents($tag) [exec git cat-file tag $tag]
62d3ea65
PM
10499 }
10500 }
106288cb
PM
10501 if {[info exists tagcontents($tag)]} {
10502 set text $tagcontents($tag)
10503 } else {
d990cedf 10504 set text "[mc "Tag"]: $tag\n[mc "Id"]: $tagids($tag)"
106288cb 10505 }
f1b86294 10506 appendwithlinks $text {}
a80e82f6 10507 maybe_scroll_ctext 1
106288cb 10508 $ctext conf -state disabled
7fcceed7 10509 init_flist {}
106288cb
PM
10510}
10511
1d10f36d
PM
10512proc doquit {} {
10513 global stopped
314f5de1
TA
10514 global gitktmpdir
10515
1d10f36d 10516 set stopped 100
b6047c5a 10517 savestuff .
1d10f36d 10518 destroy .
314f5de1
TA
10519
10520 if {[info exists gitktmpdir]} {
10521 catch {file delete -force $gitktmpdir}
10522 }
1d10f36d 10523}
1db95b00 10524
9a7558f3 10525proc mkfontdisp {font top which} {
d93f1713 10526 global fontattr fontpref $font NS use_ttk
9a7558f3
PM
10527
10528 set fontpref($font) [set $font]
d93f1713 10529 ${NS}::button $top.${font}but -text $which \
9a7558f3 10530 -command [list choosefont $font $which]
d93f1713
PT
10531 if {!$use_ttk} {$top.${font}but configure -font optionfont}
10532 ${NS}::label $top.$font -relief flat -font $font \
9a7558f3
PM
10533 -text $fontattr($font,family) -justify left
10534 grid x $top.${font}but $top.$font -sticky w
10535}
10536
10537proc choosefont {font which} {
10538 global fontparam fontlist fonttop fontattr
d93f1713 10539 global prefstop NS
9a7558f3
PM
10540
10541 set fontparam(which) $which
10542 set fontparam(font) $font
10543 set fontparam(family) [font actual $font -family]
10544 set fontparam(size) $fontattr($font,size)
10545 set fontparam(weight) $fontattr($font,weight)
10546 set fontparam(slant) $fontattr($font,slant)
10547 set top .gitkfont
10548 set fonttop $top
10549 if {![winfo exists $top]} {
10550 font create sample
10551 eval font config sample [font actual $font]
d93f1713 10552 ttk_toplevel $top
e7d64008 10553 make_transient $top $prefstop
d990cedf 10554 wm title $top [mc "Gitk font chooser"]
d93f1713 10555 ${NS}::label $top.l -textvariable fontparam(which)
9a7558f3
PM
10556 pack $top.l -side top
10557 set fontlist [lsort [font families]]
d93f1713 10558 ${NS}::frame $top.f
9a7558f3
PM
10559 listbox $top.f.fam -listvariable fontlist \
10560 -yscrollcommand [list $top.f.sb set]
10561 bind $top.f.fam <<ListboxSelect>> selfontfam
d93f1713 10562 ${NS}::scrollbar $top.f.sb -command [list $top.f.fam yview]
9a7558f3
PM
10563 pack $top.f.sb -side right -fill y
10564 pack $top.f.fam -side left -fill both -expand 1
10565 pack $top.f -side top -fill both -expand 1
d93f1713 10566 ${NS}::frame $top.g
9a7558f3
PM
10567 spinbox $top.g.size -from 4 -to 40 -width 4 \
10568 -textvariable fontparam(size) \
10569 -validatecommand {string is integer -strict %s}
10570 checkbutton $top.g.bold -padx 5 \
d990cedf 10571 -font {{Times New Roman} 12 bold} -text [mc "B"] -indicatoron 0 \
9a7558f3
PM
10572 -variable fontparam(weight) -onvalue bold -offvalue normal
10573 checkbutton $top.g.ital -padx 5 \
d990cedf 10574 -font {{Times New Roman} 12 italic} -text [mc "I"] -indicatoron 0 \
9a7558f3
PM
10575 -variable fontparam(slant) -onvalue italic -offvalue roman
10576 pack $top.g.size $top.g.bold $top.g.ital -side left
10577 pack $top.g -side top
10578 canvas $top.c -width 150 -height 50 -border 2 -relief sunk \
10579 -background white
10580 $top.c create text 100 25 -anchor center -text $which -font sample \
10581 -fill black -tags text
10582 bind $top.c <Configure> [list centertext $top.c]
10583 pack $top.c -side top -fill x
d93f1713
PT
10584 ${NS}::frame $top.buts
10585 ${NS}::button $top.buts.ok -text [mc "OK"] -command fontok -default active
10586 ${NS}::button $top.buts.can -text [mc "Cancel"] -command fontcan -default normal
76f15947
AG
10587 bind $top <Key-Return> fontok
10588 bind $top <Key-Escape> fontcan
9a7558f3
PM
10589 grid $top.buts.ok $top.buts.can
10590 grid columnconfigure $top.buts 0 -weight 1 -uniform a
10591 grid columnconfigure $top.buts 1 -weight 1 -uniform a
10592 pack $top.buts -side bottom -fill x
10593 trace add variable fontparam write chg_fontparam
10594 } else {
10595 raise $top
10596 $top.c itemconf text -text $which
10597 }
10598 set i [lsearch -exact $fontlist $fontparam(family)]
10599 if {$i >= 0} {
10600 $top.f.fam selection set $i
10601 $top.f.fam see $i
10602 }
10603}
10604
10605proc centertext {w} {
10606 $w coords text [expr {[winfo width $w] / 2}] [expr {[winfo height $w] / 2}]
10607}
10608
10609proc fontok {} {
10610 global fontparam fontpref prefstop
10611
10612 set f $fontparam(font)
10613 set fontpref($f) [list $fontparam(family) $fontparam(size)]
10614 if {$fontparam(weight) eq "bold"} {
10615 lappend fontpref($f) "bold"
10616 }
10617 if {$fontparam(slant) eq "italic"} {
10618 lappend fontpref($f) "italic"
10619 }
10620 set w $prefstop.$f
10621 $w conf -text $fontparam(family) -font $fontpref($f)
d93f1713 10622
9a7558f3
PM
10623 fontcan
10624}
10625
10626proc fontcan {} {
10627 global fonttop fontparam
10628
10629 if {[info exists fonttop]} {
10630 catch {destroy $fonttop}
10631 catch {font delete sample}
10632 unset fonttop
10633 unset fontparam
10634 }
10635}
10636
d93f1713
PT
10637if {[package vsatisfies [package provide Tk] 8.6]} {
10638 # In Tk 8.6 we have a native font chooser dialog. Overwrite the above
10639 # function to make use of it.
10640 proc choosefont {font which} {
10641 tk fontchooser configure -title $which -font $font \
10642 -command [list on_choosefont $font $which]
10643 tk fontchooser show
10644 }
10645 proc on_choosefont {font which newfont} {
10646 global fontparam
10647 puts stderr "$font $newfont"
10648 array set f [font actual $newfont]
10649 set fontparam(which) $which
10650 set fontparam(font) $font
10651 set fontparam(family) $f(-family)
10652 set fontparam(size) $f(-size)
10653 set fontparam(weight) $f(-weight)
10654 set fontparam(slant) $f(-slant)
10655 fontok
10656 }
10657}
10658
9a7558f3
PM
10659proc selfontfam {} {
10660 global fonttop fontparam
10661
10662 set i [$fonttop.f.fam curselection]
10663 if {$i ne {}} {
10664 set fontparam(family) [$fonttop.f.fam get $i]
10665 }
10666}
10667
10668proc chg_fontparam {v sub op} {
10669 global fontparam
10670
10671 font config sample -$sub $fontparam($sub)
10672}
10673
712fcc08 10674proc doprefs {} {
d93f1713 10675 global maxwidth maxgraphpct use_ttk NS
219ea3a9 10676 global oldprefs prefstop showneartags showlocalchanges
5497f7a2 10677 global uicolor bgcolor fgcolor ctext diffcolors selectbgcolor markbgcolor
39ee47ef 10678 global tabstop limitdiffs autoselect extdifftool perfile_attrs
0cc08ff7 10679 global hideremotes want_ttk have_ttk
232475d3 10680
712fcc08
PM
10681 set top .gitkprefs
10682 set prefstop $top
10683 if {[winfo exists $top]} {
10684 raise $top
10685 return
757f17bc 10686 }
3de07118 10687 foreach v {maxwidth maxgraphpct showneartags showlocalchanges \
0cc08ff7 10688 limitdiffs tabstop perfile_attrs hideremotes want_ttk} {
712fcc08 10689 set oldprefs($v) [set $v]
232475d3 10690 }
d93f1713 10691 ttk_toplevel $top
d990cedf 10692 wm title $top [mc "Gitk preferences"]
e7d64008 10693 make_transient $top .
d93f1713 10694 ${NS}::label $top.ldisp -text [mc "Commit list display options"]
712fcc08 10695 grid $top.ldisp - -sticky w -pady 10
d93f1713
PT
10696 ${NS}::label $top.spacer -text " "
10697 ${NS}::label $top.maxwidthl -text [mc "Maximum graph width (lines)"]
712fcc08
PM
10698 spinbox $top.maxwidth -from 0 -to 100 -width 4 -textvariable maxwidth
10699 grid $top.spacer $top.maxwidthl $top.maxwidth -sticky w
d93f1713 10700 ${NS}::label $top.maxpctl -text [mc "Maximum graph width (% of pane)"]
712fcc08
PM
10701 spinbox $top.maxpct -from 1 -to 100 -width 4 -textvariable maxgraphpct
10702 grid x $top.maxpctl $top.maxpct -sticky w
d93f1713
PT
10703 ${NS}::checkbutton $top.showlocal -text [mc "Show local changes"] \
10704 -variable showlocalchanges
219ea3a9 10705 grid x $top.showlocal -sticky w
d93f1713
PT
10706 ${NS}::checkbutton $top.autoselect -text [mc "Auto-select SHA1"] \
10707 -variable autoselect
95293b58 10708 grid x $top.autoselect -sticky w
0cc08ff7
PM
10709 ${NS}::checkbutton $top.hideremotes -text [mc "Hide remote refs"] \
10710 -variable hideremotes
10711 grid x $top.hideremotes -sticky w
f8a2c0d1 10712
d93f1713 10713 ${NS}::label $top.ddisp -text [mc "Diff display options"]
712fcc08 10714 grid $top.ddisp - -sticky w -pady 10
d93f1713 10715 ${NS}::label $top.tabstopl -text [mc "Tab spacing"]
94503918
PM
10716 spinbox $top.tabstop -from 1 -to 20 -width 4 -textvariable tabstop
10717 grid x $top.tabstopl $top.tabstop -sticky w
d93f1713
PT
10718 ${NS}::checkbutton $top.ntag -text [mc "Display nearby tags"] \
10719 -variable showneartags
b8ab2e17 10720 grid x $top.ntag -sticky w
d93f1713
PT
10721 ${NS}::checkbutton $top.ldiff -text [mc "Limit diffs to listed paths"] \
10722 -variable limitdiffs
7a39a17a 10723 grid x $top.ldiff -sticky w
d93f1713
PT
10724 ${NS}::checkbutton $top.lattr -text [mc "Support per-file encodings"] \
10725 -variable perfile_attrs
39ee47ef 10726 grid x $top.lattr -sticky w
f8a2c0d1 10727
d93f1713
PT
10728 ${NS}::entry $top.extdifft -textvariable extdifftool
10729 ${NS}::frame $top.extdifff
10730 ${NS}::label $top.extdifff.l -text [mc "External diff tool" ]
10731 ${NS}::button $top.extdifff.b -text [mc "Choose..."] -command choose_extdiff
314f5de1 10732 pack $top.extdifff.l $top.extdifff.b -side left
d93f1713
PT
10733 pack configure $top.extdifff.l -padx 10
10734 grid x $top.extdifff $top.extdifft -sticky ew
314f5de1 10735
0cc08ff7
PM
10736 ${NS}::label $top.lgen -text [mc "General options"]
10737 grid $top.lgen - -sticky w -pady 10
10738 ${NS}::checkbutton $top.want_ttk -variable want_ttk \
10739 -text [mc "Use themed widgets"]
10740 if {$have_ttk} {
10741 ${NS}::label $top.ttk_note -text [mc "(change requires restart)"]
10742 } else {
10743 ${NS}::label $top.ttk_note -text [mc "(currently unavailable)"]
10744 }
10745 grid x $top.want_ttk $top.ttk_note -sticky w
314f5de1 10746
d93f1713 10747 ${NS}::label $top.cdisp -text [mc "Colors: press to choose"]
f8a2c0d1 10748 grid $top.cdisp - -sticky w -pady 10
5497f7a2 10749 label $top.ui -padx 40 -relief sunk -background $uicolor
1924d1bc 10750 ${NS}::button $top.uibut -text [mc "Interface"] \
5497f7a2
GR
10751 -command [list choosecolor uicolor {} $top.ui [mc "interface"] setui]
10752 grid x $top.uibut $top.ui -sticky w
f8a2c0d1 10753 label $top.bg -padx 40 -relief sunk -background $bgcolor
d93f1713 10754 ${NS}::button $top.bgbut -text [mc "Background"] \
968b016a 10755 -command [list choosecolor bgcolor {} $top.bg [mc "background"] setbg]
f8a2c0d1
PM
10756 grid x $top.bgbut $top.bg -sticky w
10757 label $top.fg -padx 40 -relief sunk -background $fgcolor
d93f1713 10758 ${NS}::button $top.fgbut -text [mc "Foreground"] \
968b016a 10759 -command [list choosecolor fgcolor {} $top.fg [mc "foreground"] setfg]
f8a2c0d1
PM
10760 grid x $top.fgbut $top.fg -sticky w
10761 label $top.diffold -padx 40 -relief sunk -background [lindex $diffcolors 0]
d93f1713 10762 ${NS}::button $top.diffoldbut -text [mc "Diff: old lines"] \
968b016a 10763 -command [list choosecolor diffcolors 0 $top.diffold [mc "diff old lines"] \
f8a2c0d1
PM
10764 [list $ctext tag conf d0 -foreground]]
10765 grid x $top.diffoldbut $top.diffold -sticky w
10766 label $top.diffnew -padx 40 -relief sunk -background [lindex $diffcolors 1]
d93f1713 10767 ${NS}::button $top.diffnewbut -text [mc "Diff: new lines"] \
968b016a 10768 -command [list choosecolor diffcolors 1 $top.diffnew [mc "diff new lines"] \
8b07dca1 10769 [list $ctext tag conf dresult -foreground]]
f8a2c0d1
PM
10770 grid x $top.diffnewbut $top.diffnew -sticky w
10771 label $top.hunksep -padx 40 -relief sunk -background [lindex $diffcolors 2]
d93f1713 10772 ${NS}::button $top.hunksepbut -text [mc "Diff: hunk header"] \
f8a2c0d1 10773 -command [list choosecolor diffcolors 2 $top.hunksep \
968b016a 10774 [mc "diff hunk header"] \
f8a2c0d1
PM
10775 [list $ctext tag conf hunksep -foreground]]
10776 grid x $top.hunksepbut $top.hunksep -sticky w
e3e901be 10777 label $top.markbgsep -padx 40 -relief sunk -background $markbgcolor
d93f1713 10778 ${NS}::button $top.markbgbut -text [mc "Marked line bg"] \
e3e901be
PM
10779 -command [list choosecolor markbgcolor {} $top.markbgsep \
10780 [mc "marked line background"] \
10781 [list $ctext tag conf omark -background]]
10782 grid x $top.markbgbut $top.markbgsep -sticky w
60378c0c 10783 label $top.selbgsep -padx 40 -relief sunk -background $selectbgcolor
d93f1713 10784 ${NS}::button $top.selbgbut -text [mc "Select bg"] \
968b016a 10785 -command [list choosecolor selectbgcolor {} $top.selbgsep [mc "background"] setselbg]
60378c0c 10786 grid x $top.selbgbut $top.selbgsep -sticky w
f8a2c0d1 10787
d93f1713 10788 ${NS}::label $top.cfont -text [mc "Fonts: press to choose"]
9a7558f3 10789 grid $top.cfont - -sticky w -pady 10
d990cedf
CS
10790 mkfontdisp mainfont $top [mc "Main font"]
10791 mkfontdisp textfont $top [mc "Diff display font"]
10792 mkfontdisp uifont $top [mc "User interface font"]
9a7558f3 10793
d93f1713
PT
10794 if {!$use_ttk} {
10795 foreach w {maxpctl maxwidthl showlocal autoselect tabstopl ntag
10796 ldiff lattr extdifff.l extdifff.b bgbut fgbut
0cc08ff7
PM
10797 diffoldbut diffnewbut hunksepbut markbgbut selbgbut
10798 want_ttk ttk_note} {
d93f1713
PT
10799 $top.$w configure -font optionfont
10800 }
10801 }
10802
10803 ${NS}::frame $top.buts
10804 ${NS}::button $top.buts.ok -text [mc "OK"] -command prefsok -default active
10805 ${NS}::button $top.buts.can -text [mc "Cancel"] -command prefscan -default normal
76f15947
AG
10806 bind $top <Key-Return> prefsok
10807 bind $top <Key-Escape> prefscan
712fcc08
PM
10808 grid $top.buts.ok $top.buts.can
10809 grid columnconfigure $top.buts 0 -weight 1 -uniform a
10810 grid columnconfigure $top.buts 1 -weight 1 -uniform a
10811 grid $top.buts - - -pady 10 -sticky ew
d93f1713 10812 grid columnconfigure $top 2 -weight 1
3a950e9a 10813 bind $top <Visibility> "focus $top.buts.ok"
712fcc08
PM
10814}
10815
314f5de1
TA
10816proc choose_extdiff {} {
10817 global extdifftool
10818
b56e0a9a 10819 set prog [tk_getOpenFile -title [mc "External diff tool"] -multiple false]
314f5de1
TA
10820 if {$prog ne {}} {
10821 set extdifftool $prog
10822 }
10823}
10824
f8a2c0d1
PM
10825proc choosecolor {v vi w x cmd} {
10826 global $v
10827
10828 set c [tk_chooseColor -initialcolor [lindex [set $v] $vi] \
d990cedf 10829 -title [mc "Gitk: choose color for %s" $x]]
f8a2c0d1
PM
10830 if {$c eq {}} return
10831 $w conf -background $c
10832 lset $v $vi $c
10833 eval $cmd $c
10834}
10835
60378c0c
ML
10836proc setselbg {c} {
10837 global bglist cflist
10838 foreach w $bglist {
10839 $w configure -selectbackground $c
10840 }
10841 $cflist tag configure highlight \
10842 -background [$cflist cget -selectbackground]
10843 allcanvs itemconf secsel -fill $c
10844}
10845
51a7e8b6
PM
10846# This sets the background color and the color scheme for the whole UI.
10847# For some reason, tk_setPalette chooses a nasty dark red for selectColor
10848# if we don't specify one ourselves, which makes the checkbuttons and
10849# radiobuttons look bad. This chooses white for selectColor if the
10850# background color is light, or black if it is dark.
5497f7a2 10851proc setui {c} {
51a7e8b6
PM
10852 set bg [winfo rgb . $c]
10853 set selc black
10854 if {[lindex $bg 0] + 1.5 * [lindex $bg 1] + 0.5 * [lindex $bg 2] > 100000} {
10855 set selc white
10856 }
10857 tk_setPalette background $c selectColor $selc
5497f7a2
GR
10858}
10859
f8a2c0d1
PM
10860proc setbg {c} {
10861 global bglist
10862
10863 foreach w $bglist {
10864 $w conf -background $c
10865 }
10866}
10867
10868proc setfg {c} {
10869 global fglist canv
10870
10871 foreach w $fglist {
10872 $w conf -foreground $c
10873 }
10874 allcanvs itemconf text -fill $c
10875 $canv itemconf circle -outline $c
b9fdba7f 10876 $canv itemconf markid -outline $c
f8a2c0d1
PM
10877}
10878
712fcc08 10879proc prefscan {} {
94503918 10880 global oldprefs prefstop
712fcc08 10881
3de07118 10882 foreach v {maxwidth maxgraphpct showneartags showlocalchanges \
0cc08ff7 10883 limitdiffs tabstop perfile_attrs hideremotes want_ttk} {
94503918 10884 global $v
712fcc08
PM
10885 set $v $oldprefs($v)
10886 }
10887 catch {destroy $prefstop}
10888 unset prefstop
9a7558f3 10889 fontcan
712fcc08
PM
10890}
10891
10892proc prefsok {} {
10893 global maxwidth maxgraphpct
219ea3a9 10894 global oldprefs prefstop showneartags showlocalchanges
9a7558f3 10895 global fontpref mainfont textfont uifont
39ee47ef 10896 global limitdiffs treediffs perfile_attrs
ffe15297 10897 global hideremotes
712fcc08
PM
10898
10899 catch {destroy $prefstop}
10900 unset prefstop
9a7558f3
PM
10901 fontcan
10902 set fontchanged 0
10903 if {$mainfont ne $fontpref(mainfont)} {
10904 set mainfont $fontpref(mainfont)
10905 parsefont mainfont $mainfont
10906 eval font configure mainfont [fontflags mainfont]
10907 eval font configure mainfontbold [fontflags mainfont 1]
10908 setcoords
10909 set fontchanged 1
10910 }
10911 if {$textfont ne $fontpref(textfont)} {
10912 set textfont $fontpref(textfont)
10913 parsefont textfont $textfont
10914 eval font configure textfont [fontflags textfont]
10915 eval font configure textfontbold [fontflags textfont 1]
10916 }
10917 if {$uifont ne $fontpref(uifont)} {
10918 set uifont $fontpref(uifont)
10919 parsefont uifont $uifont
10920 eval font configure uifont [fontflags uifont]
10921 }
32f1b3e4 10922 settabs
219ea3a9
PM
10923 if {$showlocalchanges != $oldprefs(showlocalchanges)} {
10924 if {$showlocalchanges} {
10925 doshowlocalchanges
10926 } else {
10927 dohidelocalchanges
10928 }
10929 }
39ee47ef
PM
10930 if {$limitdiffs != $oldprefs(limitdiffs) ||
10931 ($perfile_attrs && !$oldprefs(perfile_attrs))} {
10932 # treediffs elements are limited by path;
10933 # won't have encodings cached if perfile_attrs was just turned on
74a40c71
PM
10934 catch {unset treediffs}
10935 }
9a7558f3 10936 if {$fontchanged || $maxwidth != $oldprefs(maxwidth)
712fcc08
PM
10937 || $maxgraphpct != $oldprefs(maxgraphpct)} {
10938 redisplay
7a39a17a
PM
10939 } elseif {$showneartags != $oldprefs(showneartags) ||
10940 $limitdiffs != $oldprefs(limitdiffs)} {
b8ab2e17 10941 reselectline
712fcc08 10942 }
ffe15297
TR
10943 if {$hideremotes != $oldprefs(hideremotes)} {
10944 rereadrefs
10945 }
712fcc08
PM
10946}
10947
10948proc formatdate {d} {
e8b5f4be 10949 global datetimeformat
219ea3a9 10950 if {$d ne {}} {
e8b5f4be 10951 set d [clock format $d -format $datetimeformat]
219ea3a9
PM
10952 }
10953 return $d
232475d3
PM
10954}
10955
fd8ccbec
PM
10956# This list of encoding names and aliases is distilled from
10957# http://www.iana.org/assignments/character-sets.
10958# Not all of them are supported by Tcl.
10959set encoding_aliases {
10960 { ANSI_X3.4-1968 iso-ir-6 ANSI_X3.4-1986 ISO_646.irv:1991 ASCII
10961 ISO646-US US-ASCII us IBM367 cp367 csASCII }
10962 { ISO-10646-UTF-1 csISO10646UTF1 }
10963 { ISO_646.basic:1983 ref csISO646basic1983 }
10964 { INVARIANT csINVARIANT }
10965 { ISO_646.irv:1983 iso-ir-2 irv csISO2IntlRefVersion }
10966 { BS_4730 iso-ir-4 ISO646-GB gb uk csISO4UnitedKingdom }
10967 { NATS-SEFI iso-ir-8-1 csNATSSEFI }
10968 { NATS-SEFI-ADD iso-ir-8-2 csNATSSEFIADD }
10969 { NATS-DANO iso-ir-9-1 csNATSDANO }
10970 { NATS-DANO-ADD iso-ir-9-2 csNATSDANOADD }
10971 { SEN_850200_B iso-ir-10 FI ISO646-FI ISO646-SE se csISO10Swedish }
10972 { SEN_850200_C iso-ir-11 ISO646-SE2 se2 csISO11SwedishForNames }
10973 { KS_C_5601-1987 iso-ir-149 KS_C_5601-1989 KSC_5601 korean csKSC56011987 }
10974 { ISO-2022-KR csISO2022KR }
10975 { EUC-KR csEUCKR }
10976 { ISO-2022-JP csISO2022JP }
10977 { ISO-2022-JP-2 csISO2022JP2 }
10978 { JIS_C6220-1969-jp JIS_C6220-1969 iso-ir-13 katakana x0201-7
10979 csISO13JISC6220jp }
10980 { JIS_C6220-1969-ro iso-ir-14 jp ISO646-JP csISO14JISC6220ro }
10981 { IT iso-ir-15 ISO646-IT csISO15Italian }
10982 { PT iso-ir-16 ISO646-PT csISO16Portuguese }
10983 { ES iso-ir-17 ISO646-ES csISO17Spanish }
10984 { greek7-old iso-ir-18 csISO18Greek7Old }
10985 { latin-greek iso-ir-19 csISO19LatinGreek }
10986 { DIN_66003 iso-ir-21 de ISO646-DE csISO21German }
10987 { NF_Z_62-010_(1973) iso-ir-25 ISO646-FR1 csISO25French }
10988 { Latin-greek-1 iso-ir-27 csISO27LatinGreek1 }
10989 { ISO_5427 iso-ir-37 csISO5427Cyrillic }
10990 { JIS_C6226-1978 iso-ir-42 csISO42JISC62261978 }
10991 { BS_viewdata iso-ir-47 csISO47BSViewdata }
10992 { INIS iso-ir-49 csISO49INIS }
10993 { INIS-8 iso-ir-50 csISO50INIS8 }
10994 { INIS-cyrillic iso-ir-51 csISO51INISCyrillic }
10995 { ISO_5427:1981 iso-ir-54 ISO5427Cyrillic1981 }
10996 { ISO_5428:1980 iso-ir-55 csISO5428Greek }
10997 { GB_1988-80 iso-ir-57 cn ISO646-CN csISO57GB1988 }
10998 { GB_2312-80 iso-ir-58 chinese csISO58GB231280 }
10999 { NS_4551-1 iso-ir-60 ISO646-NO no csISO60DanishNorwegian
11000 csISO60Norwegian1 }
11001 { NS_4551-2 ISO646-NO2 iso-ir-61 no2 csISO61Norwegian2 }
11002 { NF_Z_62-010 iso-ir-69 ISO646-FR fr csISO69French }
11003 { videotex-suppl iso-ir-70 csISO70VideotexSupp1 }
11004 { PT2 iso-ir-84 ISO646-PT2 csISO84Portuguese2 }
11005 { ES2 iso-ir-85 ISO646-ES2 csISO85Spanish2 }
11006 { MSZ_7795.3 iso-ir-86 ISO646-HU hu csISO86Hungarian }
11007 { JIS_C6226-1983 iso-ir-87 x0208 JIS_X0208-1983 csISO87JISX0208 }
11008 { greek7 iso-ir-88 csISO88Greek7 }
11009 { ASMO_449 ISO_9036 arabic7 iso-ir-89 csISO89ASMO449 }
11010 { iso-ir-90 csISO90 }
11011 { JIS_C6229-1984-a iso-ir-91 jp-ocr-a csISO91JISC62291984a }
11012 { JIS_C6229-1984-b iso-ir-92 ISO646-JP-OCR-B jp-ocr-b
11013 csISO92JISC62991984b }
11014 { JIS_C6229-1984-b-add iso-ir-93 jp-ocr-b-add csISO93JIS62291984badd }
11015 { JIS_C6229-1984-hand iso-ir-94 jp-ocr-hand csISO94JIS62291984hand }
11016 { JIS_C6229-1984-hand-add iso-ir-95 jp-ocr-hand-add
11017 csISO95JIS62291984handadd }
11018 { JIS_C6229-1984-kana iso-ir-96 csISO96JISC62291984kana }
11019 { ISO_2033-1983 iso-ir-98 e13b csISO2033 }
11020 { ANSI_X3.110-1983 iso-ir-99 CSA_T500-1983 NAPLPS csISO99NAPLPS }
11021 { ISO_8859-1:1987 iso-ir-100 ISO_8859-1 ISO-8859-1 latin1 l1 IBM819
11022 CP819 csISOLatin1 }
11023 { ISO_8859-2:1987 iso-ir-101 ISO_8859-2 ISO-8859-2 latin2 l2 csISOLatin2 }
11024 { T.61-7bit iso-ir-102 csISO102T617bit }
11025 { T.61-8bit T.61 iso-ir-103 csISO103T618bit }
11026 { ISO_8859-3:1988 iso-ir-109 ISO_8859-3 ISO-8859-3 latin3 l3 csISOLatin3 }
11027 { ISO_8859-4:1988 iso-ir-110 ISO_8859-4 ISO-8859-4 latin4 l4 csISOLatin4 }
11028 { ECMA-cyrillic iso-ir-111 KOI8-E csISO111ECMACyrillic }
11029 { CSA_Z243.4-1985-1 iso-ir-121 ISO646-CA csa7-1 ca csISO121Canadian1 }
11030 { CSA_Z243.4-1985-2 iso-ir-122 ISO646-CA2 csa7-2 csISO122Canadian2 }
11031 { CSA_Z243.4-1985-gr iso-ir-123 csISO123CSAZ24341985gr }
11032 { ISO_8859-6:1987 iso-ir-127 ISO_8859-6 ISO-8859-6 ECMA-114 ASMO-708
11033 arabic csISOLatinArabic }
11034 { ISO_8859-6-E csISO88596E ISO-8859-6-E }
11035 { ISO_8859-6-I csISO88596I ISO-8859-6-I }
11036 { ISO_8859-7:1987 iso-ir-126 ISO_8859-7 ISO-8859-7 ELOT_928 ECMA-118
11037 greek greek8 csISOLatinGreek }
11038 { T.101-G2 iso-ir-128 csISO128T101G2 }
11039 { ISO_8859-8:1988 iso-ir-138 ISO_8859-8 ISO-8859-8 hebrew
11040 csISOLatinHebrew }
11041 { ISO_8859-8-E csISO88598E ISO-8859-8-E }
11042 { ISO_8859-8-I csISO88598I ISO-8859-8-I }
11043 { CSN_369103 iso-ir-139 csISO139CSN369103 }
11044 { JUS_I.B1.002 iso-ir-141 ISO646-YU js yu csISO141JUSIB1002 }
11045 { ISO_6937-2-add iso-ir-142 csISOTextComm }
11046 { IEC_P27-1 iso-ir-143 csISO143IECP271 }
11047 { ISO_8859-5:1988 iso-ir-144 ISO_8859-5 ISO-8859-5 cyrillic
11048 csISOLatinCyrillic }
11049 { JUS_I.B1.003-serb iso-ir-146 serbian csISO146Serbian }
11050 { JUS_I.B1.003-mac macedonian iso-ir-147 csISO147Macedonian }
11051 { ISO_8859-9:1989 iso-ir-148 ISO_8859-9 ISO-8859-9 latin5 l5 csISOLatin5 }
11052 { greek-ccitt iso-ir-150 csISO150 csISO150GreekCCITT }
11053 { NC_NC00-10:81 cuba iso-ir-151 ISO646-CU csISO151Cuba }
11054 { ISO_6937-2-25 iso-ir-152 csISO6937Add }
11055 { GOST_19768-74 ST_SEV_358-88 iso-ir-153 csISO153GOST1976874 }
11056 { ISO_8859-supp iso-ir-154 latin1-2-5 csISO8859Supp }
11057 { ISO_10367-box iso-ir-155 csISO10367Box }
11058 { ISO-8859-10 iso-ir-157 l6 ISO_8859-10:1992 csISOLatin6 latin6 }
11059 { latin-lap lap iso-ir-158 csISO158Lap }
11060 { JIS_X0212-1990 x0212 iso-ir-159 csISO159JISX02121990 }
11061 { DS_2089 DS2089 ISO646-DK dk csISO646Danish }
11062 { us-dk csUSDK }
11063 { dk-us csDKUS }
11064 { JIS_X0201 X0201 csHalfWidthKatakana }
11065 { KSC5636 ISO646-KR csKSC5636 }
11066 { ISO-10646-UCS-2 csUnicode }
11067 { ISO-10646-UCS-4 csUCS4 }
11068 { DEC-MCS dec csDECMCS }
11069 { hp-roman8 roman8 r8 csHPRoman8 }
11070 { macintosh mac csMacintosh }
11071 { IBM037 cp037 ebcdic-cp-us ebcdic-cp-ca ebcdic-cp-wt ebcdic-cp-nl
11072 csIBM037 }
11073 { IBM038 EBCDIC-INT cp038 csIBM038 }
11074 { IBM273 CP273 csIBM273 }
11075 { IBM274 EBCDIC-BE CP274 csIBM274 }
11076 { IBM275 EBCDIC-BR cp275 csIBM275 }
11077 { IBM277 EBCDIC-CP-DK EBCDIC-CP-NO csIBM277 }
11078 { IBM278 CP278 ebcdic-cp-fi ebcdic-cp-se csIBM278 }
11079 { IBM280 CP280 ebcdic-cp-it csIBM280 }
11080 { IBM281 EBCDIC-JP-E cp281 csIBM281 }
11081 { IBM284 CP284 ebcdic-cp-es csIBM284 }
11082 { IBM285 CP285 ebcdic-cp-gb csIBM285 }
11083 { IBM290 cp290 EBCDIC-JP-kana csIBM290 }
11084 { IBM297 cp297 ebcdic-cp-fr csIBM297 }
11085 { IBM420 cp420 ebcdic-cp-ar1 csIBM420 }
11086 { IBM423 cp423 ebcdic-cp-gr csIBM423 }
11087 { IBM424 cp424 ebcdic-cp-he csIBM424 }
11088 { IBM437 cp437 437 csPC8CodePage437 }
11089 { IBM500 CP500 ebcdic-cp-be ebcdic-cp-ch csIBM500 }
11090 { IBM775 cp775 csPC775Baltic }
11091 { IBM850 cp850 850 csPC850Multilingual }
11092 { IBM851 cp851 851 csIBM851 }
11093 { IBM852 cp852 852 csPCp852 }
11094 { IBM855 cp855 855 csIBM855 }
11095 { IBM857 cp857 857 csIBM857 }
11096 { IBM860 cp860 860 csIBM860 }
11097 { IBM861 cp861 861 cp-is csIBM861 }
11098 { IBM862 cp862 862 csPC862LatinHebrew }
11099 { IBM863 cp863 863 csIBM863 }
11100 { IBM864 cp864 csIBM864 }
11101 { IBM865 cp865 865 csIBM865 }
11102 { IBM866 cp866 866 csIBM866 }
11103 { IBM868 CP868 cp-ar csIBM868 }
11104 { IBM869 cp869 869 cp-gr csIBM869 }
11105 { IBM870 CP870 ebcdic-cp-roece ebcdic-cp-yu csIBM870 }
11106 { IBM871 CP871 ebcdic-cp-is csIBM871 }
11107 { IBM880 cp880 EBCDIC-Cyrillic csIBM880 }
11108 { IBM891 cp891 csIBM891 }
11109 { IBM903 cp903 csIBM903 }
11110 { IBM904 cp904 904 csIBBM904 }
11111 { IBM905 CP905 ebcdic-cp-tr csIBM905 }
11112 { IBM918 CP918 ebcdic-cp-ar2 csIBM918 }
11113 { IBM1026 CP1026 csIBM1026 }
11114 { EBCDIC-AT-DE csIBMEBCDICATDE }
11115 { EBCDIC-AT-DE-A csEBCDICATDEA }
11116 { EBCDIC-CA-FR csEBCDICCAFR }
11117 { EBCDIC-DK-NO csEBCDICDKNO }
11118 { EBCDIC-DK-NO-A csEBCDICDKNOA }
11119 { EBCDIC-FI-SE csEBCDICFISE }
11120 { EBCDIC-FI-SE-A csEBCDICFISEA }
11121 { EBCDIC-FR csEBCDICFR }
11122 { EBCDIC-IT csEBCDICIT }
11123 { EBCDIC-PT csEBCDICPT }
11124 { EBCDIC-ES csEBCDICES }
11125 { EBCDIC-ES-A csEBCDICESA }
11126 { EBCDIC-ES-S csEBCDICESS }
11127 { EBCDIC-UK csEBCDICUK }
11128 { EBCDIC-US csEBCDICUS }
11129 { UNKNOWN-8BIT csUnknown8BiT }
11130 { MNEMONIC csMnemonic }
11131 { MNEM csMnem }
11132 { VISCII csVISCII }
11133 { VIQR csVIQR }
11134 { KOI8-R csKOI8R }
11135 { IBM00858 CCSID00858 CP00858 PC-Multilingual-850+euro }
11136 { IBM00924 CCSID00924 CP00924 ebcdic-Latin9--euro }
11137 { IBM01140 CCSID01140 CP01140 ebcdic-us-37+euro }
11138 { IBM01141 CCSID01141 CP01141 ebcdic-de-273+euro }
11139 { IBM01142 CCSID01142 CP01142 ebcdic-dk-277+euro ebcdic-no-277+euro }
11140 { IBM01143 CCSID01143 CP01143 ebcdic-fi-278+euro ebcdic-se-278+euro }
11141 { IBM01144 CCSID01144 CP01144 ebcdic-it-280+euro }
11142 { IBM01145 CCSID01145 CP01145 ebcdic-es-284+euro }
11143 { IBM01146 CCSID01146 CP01146 ebcdic-gb-285+euro }
11144 { IBM01147 CCSID01147 CP01147 ebcdic-fr-297+euro }
11145 { IBM01148 CCSID01148 CP01148 ebcdic-international-500+euro }
11146 { IBM01149 CCSID01149 CP01149 ebcdic-is-871+euro }
11147 { IBM1047 IBM-1047 }
11148 { PTCP154 csPTCP154 PT154 CP154 Cyrillic-Asian }
11149 { Amiga-1251 Ami1251 Amiga1251 Ami-1251 }
11150 { UNICODE-1-1 csUnicode11 }
11151 { CESU-8 csCESU-8 }
11152 { BOCU-1 csBOCU-1 }
11153 { UNICODE-1-1-UTF-7 csUnicode11UTF7 }
11154 { ISO-8859-14 iso-ir-199 ISO_8859-14:1998 ISO_8859-14 latin8 iso-celtic
11155 l8 }
11156 { ISO-8859-15 ISO_8859-15 Latin-9 }
11157 { ISO-8859-16 iso-ir-226 ISO_8859-16:2001 ISO_8859-16 latin10 l10 }
11158 { GBK CP936 MS936 windows-936 }
11159 { JIS_Encoding csJISEncoding }
09c7029d 11160 { Shift_JIS MS_Kanji csShiftJIS ShiftJIS Shift-JIS }
fd8ccbec
PM
11161 { Extended_UNIX_Code_Packed_Format_for_Japanese csEUCPkdFmtJapanese
11162 EUC-JP }
11163 { Extended_UNIX_Code_Fixed_Width_for_Japanese csEUCFixWidJapanese }
11164 { ISO-10646-UCS-Basic csUnicodeASCII }
11165 { ISO-10646-Unicode-Latin1 csUnicodeLatin1 ISO-10646 }
11166 { ISO-Unicode-IBM-1261 csUnicodeIBM1261 }
11167 { ISO-Unicode-IBM-1268 csUnicodeIBM1268 }
11168 { ISO-Unicode-IBM-1276 csUnicodeIBM1276 }
11169 { ISO-Unicode-IBM-1264 csUnicodeIBM1264 }
11170 { ISO-Unicode-IBM-1265 csUnicodeIBM1265 }
11171 { ISO-8859-1-Windows-3.0-Latin-1 csWindows30Latin1 }
11172 { ISO-8859-1-Windows-3.1-Latin-1 csWindows31Latin1 }
11173 { ISO-8859-2-Windows-Latin-2 csWindows31Latin2 }
11174 { ISO-8859-9-Windows-Latin-5 csWindows31Latin5 }
11175 { Adobe-Standard-Encoding csAdobeStandardEncoding }
11176 { Ventura-US csVenturaUS }
11177 { Ventura-International csVenturaInternational }
11178 { PC8-Danish-Norwegian csPC8DanishNorwegian }
11179 { PC8-Turkish csPC8Turkish }
11180 { IBM-Symbols csIBMSymbols }
11181 { IBM-Thai csIBMThai }
11182 { HP-Legal csHPLegal }
11183 { HP-Pi-font csHPPiFont }
11184 { HP-Math8 csHPMath8 }
11185 { Adobe-Symbol-Encoding csHPPSMath }
11186 { HP-DeskTop csHPDesktop }
11187 { Ventura-Math csVenturaMath }
11188 { Microsoft-Publishing csMicrosoftPublishing }
11189 { Windows-31J csWindows31J }
11190 { GB2312 csGB2312 }
11191 { Big5 csBig5 }
11192}
11193
11194proc tcl_encoding {enc} {
39ee47ef
PM
11195 global encoding_aliases tcl_encoding_cache
11196 if {[info exists tcl_encoding_cache($enc)]} {
11197 return $tcl_encoding_cache($enc)
11198 }
fd8ccbec
PM
11199 set names [encoding names]
11200 set lcnames [string tolower $names]
11201 set enc [string tolower $enc]
11202 set i [lsearch -exact $lcnames $enc]
11203 if {$i < 0} {
11204 # look for "isonnn" instead of "iso-nnn" or "iso_nnn"
09c7029d 11205 if {[regsub {^(iso|cp|ibm|jis)[-_]} $enc {\1} encx]} {
fd8ccbec
PM
11206 set i [lsearch -exact $lcnames $encx]
11207 }
11208 }
11209 if {$i < 0} {
11210 foreach l $encoding_aliases {
11211 set ll [string tolower $l]
11212 if {[lsearch -exact $ll $enc] < 0} continue
11213 # look through the aliases for one that tcl knows about
11214 foreach e $ll {
11215 set i [lsearch -exact $lcnames $e]
11216 if {$i < 0} {
09c7029d 11217 if {[regsub {^(iso|cp|ibm|jis)[-_]} $e {\1} ex]} {
fd8ccbec
PM
11218 set i [lsearch -exact $lcnames $ex]
11219 }
11220 }
11221 if {$i >= 0} break
11222 }
11223 break
11224 }
11225 }
39ee47ef 11226 set tclenc {}
fd8ccbec 11227 if {$i >= 0} {
39ee47ef 11228 set tclenc [lindex $names $i]
fd8ccbec 11229 }
39ee47ef
PM
11230 set tcl_encoding_cache($enc) $tclenc
11231 return $tclenc
fd8ccbec
PM
11232}
11233
09c7029d 11234proc gitattr {path attr default} {
39ee47ef
PM
11235 global path_attr_cache
11236 if {[info exists path_attr_cache($attr,$path)]} {
11237 set r $path_attr_cache($attr,$path)
11238 } else {
11239 set r "unspecified"
11240 if {![catch {set line [exec git check-attr $attr -- $path]}]} {
097e1118 11241 regexp "(.*): $attr: (.*)" $line m f r
09c7029d 11242 }
4db09304 11243 set path_attr_cache($attr,$path) $r
39ee47ef
PM
11244 }
11245 if {$r eq "unspecified"} {
11246 return $default
11247 }
11248 return $r
09c7029d
AG
11249}
11250
4db09304 11251proc cache_gitattr {attr pathlist} {
39ee47ef
PM
11252 global path_attr_cache
11253 set newlist {}
11254 foreach path $pathlist {
11255 if {![info exists path_attr_cache($attr,$path)]} {
11256 lappend newlist $path
11257 }
11258 }
11259 set lim 1000
11260 if {[tk windowingsystem] == "win32"} {
11261 # windows has a 32k limit on the arguments to a command...
11262 set lim 30
11263 }
11264 while {$newlist ne {}} {
11265 set head [lrange $newlist 0 [expr {$lim - 1}]]
11266 set newlist [lrange $newlist $lim end]
11267 if {![catch {set rlist [eval exec git check-attr $attr -- $head]}]} {
11268 foreach row [split $rlist "\n"] {
097e1118 11269 if {[regexp "(.*): $attr: (.*)" $row m path value]} {
39ee47ef
PM
11270 if {[string index $path 0] eq "\""} {
11271 set path [encoding convertfrom [lindex $path 0]]
11272 }
11273 set path_attr_cache($attr,$path) $value
4db09304 11274 }
39ee47ef 11275 }
4db09304 11276 }
39ee47ef 11277 }
4db09304
AG
11278}
11279
09c7029d 11280proc get_path_encoding {path} {
39ee47ef
PM
11281 global gui_encoding perfile_attrs
11282 set tcl_enc $gui_encoding
11283 if {$path ne {} && $perfile_attrs} {
11284 set enc2 [tcl_encoding [gitattr $path encoding $tcl_enc]]
11285 if {$enc2 ne {}} {
11286 set tcl_enc $enc2
09c7029d 11287 }
39ee47ef
PM
11288 }
11289 return $tcl_enc
09c7029d
AG
11290}
11291
5d7589d4
PM
11292# First check that Tcl/Tk is recent enough
11293if {[catch {package require Tk 8.4} err]} {
8d849957
BH
11294 show_error {} . "Sorry, gitk cannot run with this version of Tcl/Tk.\n\
11295 Gitk requires at least Tcl/Tk 8.4." list
5d7589d4
PM
11296 exit 1
11297}
11298
1d10f36d 11299# defaults...
8974c6f9 11300set wrcomcmd "git diff-tree --stdin -p --pretty"
671bc153 11301
fd8ccbec 11302set gitencoding {}
671bc153 11303catch {
27cb61ca 11304 set gitencoding [exec git config --get i18n.commitencoding]
671bc153 11305}
590915da
AG
11306catch {
11307 set gitencoding [exec git config --get i18n.logoutputencoding]
11308}
671bc153 11309if {$gitencoding == ""} {
fd8ccbec
PM
11310 set gitencoding "utf-8"
11311}
11312set tclencoding [tcl_encoding $gitencoding]
11313if {$tclencoding == {}} {
11314 puts stderr "Warning: encoding $gitencoding is not supported by Tcl/Tk"
671bc153 11315}
1db95b00 11316
09c7029d
AG
11317set gui_encoding [encoding system]
11318catch {
39ee47ef
PM
11319 set enc [exec git config --get gui.encoding]
11320 if {$enc ne {}} {
11321 set tclenc [tcl_encoding $enc]
11322 if {$tclenc ne {}} {
11323 set gui_encoding $tclenc
11324 } else {
11325 puts stderr "Warning: encoding $enc is not supported by Tcl/Tk"
11326 }
11327 }
09c7029d
AG
11328}
11329
5fdcbb13
DS
11330if {[tk windowingsystem] eq "aqua"} {
11331 set mainfont {{Lucida Grande} 9}
11332 set textfont {Monaco 9}
11333 set uifont {{Lucida Grande} 9 bold}
11334} else {
11335 set mainfont {Helvetica 9}
11336 set textfont {Courier 9}
11337 set uifont {Helvetica 9 bold}
11338}
7e12f1a6 11339set tabstop 8
b74fd579 11340set findmergefiles 0
8d858d1a 11341set maxgraphpct 50
f6075eba 11342set maxwidth 16
232475d3 11343set revlistorder 0
757f17bc 11344set fastdate 0
6e8c8707
PM
11345set uparrowlen 5
11346set downarrowlen 5
11347set mingaplen 100
f8b28a40 11348set cmitmode "patch"
f1b86294 11349set wrapcomment "none"
b8ab2e17 11350set showneartags 1
ffe15297 11351set hideremotes 0
0a4dd8b8 11352set maxrefs 20
322a8cc9 11353set maxlinelen 200
219ea3a9 11354set showlocalchanges 1
7a39a17a 11355set limitdiffs 1
e8b5f4be 11356set datetimeformat "%Y-%m-%d %H:%M:%S"
95293b58 11357set autoselect 1
39ee47ef 11358set perfile_attrs 0
0cc08ff7 11359set want_ttk 1
1d10f36d 11360
5fdcbb13
DS
11361if {[tk windowingsystem] eq "aqua"} {
11362 set extdifftool "opendiff"
11363} else {
11364 set extdifftool "meld"
11365}
314f5de1 11366
1d10f36d 11367set colors {green red blue magenta darkgrey brown orange}
1924d1bc
PT
11368if {[tk windowingsystem] eq "win32"} {
11369 set uicolor SystemButtonFace
11370 set bgcolor SystemWindow
11371 set fgcolor SystemButtonText
11372 set selectbgcolor SystemHighlight
11373} else {
11374 set uicolor grey85
11375 set bgcolor white
11376 set fgcolor black
11377 set selectbgcolor gray85
11378}
f8a2c0d1 11379set diffcolors {red "#00a000" blue}
890fae70 11380set diffcontext 3
b9b86007 11381set ignorespace 0
e3e901be 11382set markbgcolor "#e0e0ff"
1d10f36d 11383
c11ff120
PM
11384set circlecolors {white blue gray blue blue}
11385
d277e89f
PM
11386# button for popping up context menus
11387if {[tk windowingsystem] eq "aqua"} {
11388 set ctxbut <Button-2>
11389} else {
11390 set ctxbut <Button-3>
11391}
11392
663c3aa9
CS
11393## For msgcat loading, first locate the installation location.
11394if { [info exists ::env(GITK_MSGSDIR)] } {
11395 ## Msgsdir was manually set in the environment.
11396 set gitk_msgsdir $::env(GITK_MSGSDIR)
11397} else {
11398 ## Let's guess the prefix from argv0.
11399 set gitk_prefix [file dirname [file dirname [file normalize $argv0]]]
11400 set gitk_libdir [file join $gitk_prefix share gitk lib]
11401 set gitk_msgsdir [file join $gitk_libdir msgs]
11402 unset gitk_prefix
11403}
11404
11405## Internationalization (i18n) through msgcat and gettext. See
11406## http://www.gnu.org/software/gettext/manual/html_node/Tcl.html
11407package require msgcat
11408namespace import ::msgcat::mc
11409## And eventually load the actual message catalog
11410::msgcat::mcload $gitk_msgsdir
11411
1d10f36d
PM
11412catch {source ~/.gitk}
11413
712fcc08 11414font create optionfont -family sans-serif -size -12
17386066 11415
0ed1dd3c
PM
11416parsefont mainfont $mainfont
11417eval font create mainfont [fontflags mainfont]
11418eval font create mainfontbold [fontflags mainfont 1]
11419
11420parsefont textfont $textfont
11421eval font create textfont [fontflags textfont]
11422eval font create textfontbold [fontflags textfont 1]
11423
11424parsefont uifont $uifont
11425eval font create uifont [fontflags uifont]
17386066 11426
51a7e8b6 11427setui $uicolor
5497f7a2 11428
b039f0a6
PM
11429setoptions
11430
cdaee5db 11431# check that we can find a .git directory somewhere...
6c87d60c 11432if {[catch {set gitdir [gitdir]}]} {
d990cedf 11433 show_error {} . [mc "Cannot find a git repository here."]
6c87d60c
AR
11434 exit 1
11435}
cdaee5db 11436if {![file isdirectory $gitdir]} {
d990cedf 11437 show_error {} . [mc "Cannot find the git directory \"%s\"." $gitdir]
cdaee5db
PM
11438 exit 1
11439}
11440
39816d60
AG
11441set selecthead {}
11442set selectheadid {}
11443
1d10f36d 11444set revtreeargs {}
cdaee5db
PM
11445set cmdline_files {}
11446set i 0
2d480856 11447set revtreeargscmd {}
1d10f36d 11448foreach arg $argv {
2d480856 11449 switch -glob -- $arg {
6ebedabf 11450 "" { }
cdaee5db
PM
11451 "--" {
11452 set cmdline_files [lrange $argv [expr {$i + 1}] end]
11453 break
11454 }
39816d60
AG
11455 "--select-commit=*" {
11456 set selecthead [string range $arg 16 end]
11457 }
2d480856
YD
11458 "--argscmd=*" {
11459 set revtreeargscmd [string range $arg 10 end]
11460 }
1d10f36d
PM
11461 default {
11462 lappend revtreeargs $arg
11463 }
11464 }
cdaee5db 11465 incr i
1db95b00 11466}
1d10f36d 11467
39816d60
AG
11468if {$selecthead eq "HEAD"} {
11469 set selecthead {}
11470}
11471
cdaee5db 11472if {$i >= [llength $argv] && $revtreeargs ne {}} {
3ed31a81 11473 # no -- on command line, but some arguments (other than --argscmd)
098dd8a3 11474 if {[catch {
8974c6f9 11475 set f [eval exec git rev-parse --no-revs --no-flags $revtreeargs]
098dd8a3
PM
11476 set cmdline_files [split $f "\n"]
11477 set n [llength $cmdline_files]
11478 set revtreeargs [lrange $revtreeargs 0 end-$n]
cdaee5db
PM
11479 # Unfortunately git rev-parse doesn't produce an error when
11480 # something is both a revision and a filename. To be consistent
11481 # with git log and git rev-list, check revtreeargs for filenames.
11482 foreach arg $revtreeargs {
11483 if {[file exists $arg]} {
d990cedf
CS
11484 show_error {} . [mc "Ambiguous argument '%s': both revision\
11485 and filename" $arg]
cdaee5db
PM
11486 exit 1
11487 }
11488 }
098dd8a3
PM
11489 } err]} {
11490 # unfortunately we get both stdout and stderr in $err,
11491 # so look for "fatal:".
11492 set i [string first "fatal:" $err]
11493 if {$i > 0} {
b5e09633 11494 set err [string range $err [expr {$i + 6}] end]
098dd8a3 11495 }
d990cedf 11496 show_error {} . "[mc "Bad arguments to gitk:"]\n$err"
098dd8a3
PM
11497 exit 1
11498 }
11499}
11500
219ea3a9 11501set nullid "0000000000000000000000000000000000000000"
8f489363 11502set nullid2 "0000000000000000000000000000000000000001"
314f5de1 11503set nullfile "/dev/null"
8f489363 11504
32f1b3e4 11505set have_tk85 [expr {[package vcompare $tk_version "8.5"] >= 0}]
0cc08ff7
PM
11506if {![info exists have_ttk]} {
11507 set have_ttk [llength [info commands ::ttk::style]]
d93f1713 11508}
0cc08ff7 11509set use_ttk [expr {$have_ttk && $want_ttk}]
d93f1713 11510set NS [expr {$use_ttk ? "ttk" : ""}]
0cc08ff7 11511
194bbf6c 11512set git_version [join [lrange [split [lindex [exec git version] end] .] 0 2] .]
219ea3a9 11513
7eb3cb9c 11514set runq {}
d698206c
PM
11515set history {}
11516set historyindex 0
908c3585 11517set fh_serial 0
908c3585 11518set nhl_names {}
63b79191 11519set highlight_paths {}
687c8765 11520set findpattern {}
1902c270 11521set searchdirn -forwards
28593d3f
PM
11522set boldids {}
11523set boldnameids {}
a8d610a2 11524set diffelide {0 0}
4fb0fa19 11525set markingmatches 0
97645683 11526set linkentercount 0
0380081c
PM
11527set need_redisplay 0
11528set nrows_drawn 0
32f1b3e4 11529set firsttabstop 0
9f1afe05 11530
50b44ece
PM
11531set nextviewnum 1
11532set curview 0
a90a6d24 11533set selectedview 0
b007ee20
CS
11534set selectedhlview [mc "None"]
11535set highlight_related [mc "None"]
687c8765 11536set highlight_files {}
50b44ece 11537set viewfiles(0) {}
a90a6d24 11538set viewperm(0) 0
098dd8a3 11539set viewargs(0) {}
2d480856 11540set viewargscmd(0) {}
50b44ece 11541
94b4a69f 11542set selectedline {}
6df7403a 11543set numcommits 0
7fcc92bf 11544set loginstance 0
098dd8a3 11545set cmdlineok 0
1d10f36d 11546set stopped 0
0fba86b3 11547set stuffsaved 0
74daedb6 11548set patchnum 0
219ea3a9 11549set lserial 0
cb8329aa 11550set isworktree [expr {[exec git rev-parse --is-inside-work-tree] == "true"}]
1d10f36d 11551setcoords
d94f8cd6 11552makewindow
37871b73
GB
11553catch {
11554 image create photo gitlogo -width 16 -height 16
11555
11556 image create photo gitlogominus -width 4 -height 2
11557 gitlogominus put #C00000 -to 0 0 4 2
11558 gitlogo copy gitlogominus -to 1 5
11559 gitlogo copy gitlogominus -to 6 5
11560 gitlogo copy gitlogominus -to 11 5
11561 image delete gitlogominus
11562
11563 image create photo gitlogoplus -width 4 -height 4
11564 gitlogoplus put #008000 -to 1 0 3 4
11565 gitlogoplus put #008000 -to 0 1 4 3
11566 gitlogo copy gitlogoplus -to 1 9
11567 gitlogo copy gitlogoplus -to 6 9
11568 gitlogo copy gitlogoplus -to 11 9
11569 image delete gitlogoplus
11570
d38d7d49
SB
11571 image create photo gitlogo32 -width 32 -height 32
11572 gitlogo32 copy gitlogo -zoom 2 2
11573
11574 wm iconphoto . -default gitlogo gitlogo32
37871b73 11575}
0eafba14
PM
11576# wait for the window to become visible
11577tkwait visibility .
6c283328 11578wm title . "[file tail $argv0]: [file tail [pwd]]"
478afad6 11579update
887fe3c4 11580readrefs
a8aaf19c 11581
2d480856 11582if {$cmdline_files ne {} || $revtreeargs ne {} || $revtreeargscmd ne {}} {
50b44ece
PM
11583 # create a view for the files/dirs specified on the command line
11584 set curview 1
a90a6d24 11585 set selectedview 1
50b44ece 11586 set nextviewnum 2
d990cedf 11587 set viewname(1) [mc "Command line"]
50b44ece 11588 set viewfiles(1) $cmdline_files
098dd8a3 11589 set viewargs(1) $revtreeargs
2d480856 11590 set viewargscmd(1) $revtreeargscmd
a90a6d24 11591 set viewperm(1) 0
3ed31a81 11592 set vdatemode(1) 0
da7c24dd 11593 addviewmenu 1
f2d0bbbd
PM
11594 .bar.view entryconf [mca "Edit view..."] -state normal
11595 .bar.view entryconf [mca "Delete view"] -state normal
50b44ece 11596}
a90a6d24
PM
11597
11598if {[info exists permviews]} {
11599 foreach v $permviews {
11600 set n $nextviewnum
11601 incr nextviewnum
11602 set viewname($n) [lindex $v 0]
11603 set viewfiles($n) [lindex $v 1]
098dd8a3 11604 set viewargs($n) [lindex $v 2]
2d480856 11605 set viewargscmd($n) [lindex $v 3]
a90a6d24 11606 set viewperm($n) 1
da7c24dd 11607 addviewmenu $n
a90a6d24
PM
11608 }
11609}
e4df519f
JS
11610
11611if {[tk windowingsystem] eq "win32"} {
11612 focus -force .
11613}
11614
567c34e0 11615getcommits {}