]> git.ipfire.org Git - thirdparty/git.git/blame - gitk
gitk: On OSX, bring the gitk window to front
[thirdparty/git.git] / gitk
CommitLineData
1db95b00
PM
1#!/bin/sh
2# Tcl ignores the next line -*- tcl -*- \
9e026d39 3exec wish "$0" -- "$@"
1db95b00 4
bb3e86a1 5# Copyright © 2005-2011 Paul Mackerras. All rights reserved.
1db95b00
PM
6# This program is free software; it may be used, copied, modified
7# and distributed under the terms of the GNU General Public Licence,
8# either version 2, or (at your option) any later version.
9
d93f1713
PT
10package require Tk
11
74cb884f
MZ
12proc hasworktree {} {
13 return [expr {[exec git rev-parse --is-bare-repository] == "false" &&
14 [exec git rev-parse --is-inside-git-dir] == "false"}]
15}
16
3878e636
ZJS
17proc reponame {} {
18 global gitdir
19 set n [file normalize $gitdir]
20 if {[string match "*/.git" $n]} {
21 set n [string range $n 0 end-5]
22 }
23 return [file tail $n]
24}
25
65bb0bda
PT
26proc gitworktree {} {
27 variable _gitworktree
28 if {[info exists _gitworktree]} {
29 return $_gitworktree
30 }
31 # v1.7.0 introduced --show-toplevel to return the canonical work-tree
32 if {[catch {set _gitworktree [exec git rev-parse --show-toplevel]}]} {
33 # try to set work tree from environment, core.worktree or use
34 # cdup to obtain a relative path to the top of the worktree. If
35 # run from the top, the ./ prefix ensures normalize expands pwd.
36 if {[catch { set _gitworktree $env(GIT_WORK_TREE) }]} {
37 catch {set _gitworktree [exec git config --get core.worktree]}
38 if {$_gitworktree eq ""} {
39 set _gitworktree [file normalize ./[exec git rev-parse --show-cdup]]
40 }
41 }
42 }
43 return $_gitworktree
44}
45
7eb3cb9c
PM
46# A simple scheduler for compute-intensive stuff.
47# The aim is to make sure that event handlers for GUI actions can
48# run at least every 50-100 ms. Unfortunately fileevent handlers are
49# run before X event handlers, so reading from a fast source can
50# make the GUI completely unresponsive.
51proc run args {
df75e86d 52 global isonrunq runq currunq
7eb3cb9c
PM
53
54 set script $args
55 if {[info exists isonrunq($script)]} return
df75e86d 56 if {$runq eq {} && ![info exists currunq]} {
7eb3cb9c
PM
57 after idle dorunq
58 }
59 lappend runq [list {} $script]
60 set isonrunq($script) 1
61}
62
63proc filerun {fd script} {
64 fileevent $fd readable [list filereadable $fd $script]
65}
66
67proc filereadable {fd script} {
df75e86d 68 global runq currunq
7eb3cb9c
PM
69
70 fileevent $fd readable {}
df75e86d 71 if {$runq eq {} && ![info exists currunq]} {
7eb3cb9c
PM
72 after idle dorunq
73 }
74 lappend runq [list $fd $script]
75}
76
7fcc92bf
PM
77proc nukefile {fd} {
78 global runq
79
80 for {set i 0} {$i < [llength $runq]} {} {
81 if {[lindex $runq $i 0] eq $fd} {
82 set runq [lreplace $runq $i $i]
83 } else {
84 incr i
85 }
86 }
87}
88
7eb3cb9c 89proc dorunq {} {
df75e86d 90 global isonrunq runq currunq
7eb3cb9c
PM
91
92 set tstart [clock clicks -milliseconds]
93 set t0 $tstart
7fcc92bf 94 while {[llength $runq] > 0} {
7eb3cb9c
PM
95 set fd [lindex $runq 0 0]
96 set script [lindex $runq 0 1]
df75e86d
AG
97 set currunq [lindex $runq 0]
98 set runq [lrange $runq 1 end]
7eb3cb9c 99 set repeat [eval $script]
df75e86d 100 unset currunq
7eb3cb9c
PM
101 set t1 [clock clicks -milliseconds]
102 set t [expr {$t1 - $t0}]
7eb3cb9c
PM
103 if {$repeat ne {} && $repeat} {
104 if {$fd eq {} || $repeat == 2} {
105 # script returns 1 if it wants to be readded
106 # file readers return 2 if they could do more straight away
107 lappend runq [list $fd $script]
108 } else {
109 fileevent $fd readable [list filereadable $fd $script]
110 }
111 } elseif {$fd eq {}} {
112 unset isonrunq($script)
113 }
114 set t0 $t1
115 if {$t1 - $tstart >= 80} break
116 }
117 if {$runq ne {}} {
118 after idle dorunq
119 }
120}
121
e439e092
AG
122proc reg_instance {fd} {
123 global commfd leftover loginstance
124
125 set i [incr loginstance]
126 set commfd($i) $fd
127 set leftover($i) {}
128 return $i
129}
130
3ed31a81
PM
131proc unmerged_files {files} {
132 global nr_unmerged
133
134 # find the list of unmerged files
135 set mlist {}
136 set nr_unmerged 0
137 if {[catch {
138 set fd [open "| git ls-files -u" r]
139 } err]} {
140 show_error {} . "[mc "Couldn't get list of unmerged files:"] $err"
141 exit 1
142 }
143 while {[gets $fd line] >= 0} {
144 set i [string first "\t" $line]
145 if {$i < 0} continue
146 set fname [string range $line [expr {$i+1}] end]
147 if {[lsearch -exact $mlist $fname] >= 0} continue
148 incr nr_unmerged
149 if {$files eq {} || [path_filter $files $fname]} {
150 lappend mlist $fname
151 }
152 }
153 catch {close $fd}
154 return $mlist
155}
156
157proc parseviewargs {n arglist} {
c2f2dab9 158 global vdatemode vmergeonly vflags vdflags vrevs vfiltered vorigargs env
ae4e3ff9 159 global worddiff git_version
3ed31a81
PM
160
161 set vdatemode($n) 0
162 set vmergeonly($n) 0
ee66e089
PM
163 set glflags {}
164 set diffargs {}
165 set nextisval 0
166 set revargs {}
167 set origargs $arglist
168 set allknown 1
169 set filtered 0
170 set i -1
171 foreach arg $arglist {
172 incr i
173 if {$nextisval} {
174 lappend glflags $arg
175 set nextisval 0
176 continue
177 }
3ed31a81
PM
178 switch -glob -- $arg {
179 "-d" -
180 "--date-order" {
181 set vdatemode($n) 1
ee66e089
PM
182 # remove from origargs in case we hit an unknown option
183 set origargs [lreplace $origargs $i $i]
184 incr i -1
185 }
ee66e089
PM
186 "-[puabwcrRBMC]" -
187 "--no-renames" - "--full-index" - "--binary" - "--abbrev=*" -
188 "--find-copies-harder" - "-l*" - "--ext-diff" - "--no-ext-diff" -
189 "--src-prefix=*" - "--dst-prefix=*" - "--no-prefix" -
190 "-O*" - "--text" - "--full-diff" - "--ignore-space-at-eol" -
191 "--ignore-space-change" - "-U*" - "--unified=*" {
29582284
PM
192 # These request or affect diff output, which we don't want.
193 # Some could be used to set our defaults for diff display.
ee66e089
PM
194 lappend diffargs $arg
195 }
ee66e089 196 "--raw" - "--patch-with-raw" - "--patch-with-stat" -
ae4e3ff9 197 "--name-only" - "--name-status" - "--color" -
ee66e089
PM
198 "--log-size" - "--pretty=*" - "--decorate" - "--abbrev-commit" -
199 "--cc" - "-z" - "--header" - "--parents" - "--boundary" -
200 "--no-color" - "-g" - "--walk-reflogs" - "--no-walk" -
201 "--timestamp" - "relative-date" - "--date=*" - "--stdin" -
202 "--objects" - "--objects-edge" - "--reverse" {
29582284
PM
203 # These cause our parsing of git log's output to fail, or else
204 # they're options we want to set ourselves, so ignore them.
ee66e089 205 }
ae4e3ff9
TR
206 "--color-words*" - "--word-diff=color" {
207 # These trigger a word diff in the console interface,
208 # so help the user by enabling our own support
209 if {[package vcompare $git_version "1.7.2"] >= 0} {
210 set worddiff [mc "Color words"]
211 }
212 }
213 "--word-diff*" {
214 if {[package vcompare $git_version "1.7.2"] >= 0} {
215 set worddiff [mc "Markup words"]
216 }
217 }
ee66e089
PM
218 "--stat=*" - "--numstat" - "--shortstat" - "--summary" -
219 "--check" - "--exit-code" - "--quiet" - "--topo-order" -
220 "--full-history" - "--dense" - "--sparse" -
221 "--follow" - "--left-right" - "--encoding=*" {
29582284 222 # These are harmless, and some are even useful
ee66e089
PM
223 lappend glflags $arg
224 }
ee66e089
PM
225 "--diff-filter=*" - "--no-merges" - "--unpacked" -
226 "--max-count=*" - "--skip=*" - "--since=*" - "--after=*" -
227 "--until=*" - "--before=*" - "--max-age=*" - "--min-age=*" -
228 "--author=*" - "--committer=*" - "--grep=*" - "-[iE]" -
229 "--remove-empty" - "--first-parent" - "--cherry-pick" -
f687aaa8
DS
230 "-S*" - "--pickaxe-all" - "--pickaxe-regex" -
231 "--simplify-by-decoration" {
29582284 232 # These mean that we get a subset of the commits
ee66e089
PM
233 set filtered 1
234 lappend glflags $arg
235 }
ee66e089 236 "-n" {
29582284
PM
237 # This appears to be the only one that has a value as a
238 # separate word following it
ee66e089
PM
239 set filtered 1
240 set nextisval 1
241 lappend glflags $arg
242 }
6e7e87c7 243 "--not" - "--all" {
ee66e089 244 lappend revargs $arg
3ed31a81
PM
245 }
246 "--merge" {
247 set vmergeonly($n) 1
ee66e089
PM
248 # git rev-parse doesn't understand --merge
249 lappend revargs --gitk-symmetric-diff-marker MERGE_HEAD...HEAD
250 }
c2f2dab9
CC
251 "--no-replace-objects" {
252 set env(GIT_NO_REPLACE_OBJECTS) "1"
253 }
ee66e089 254 "-*" {
29582284 255 # Other flag arguments including -<n>
ee66e089
PM
256 if {[string is digit -strict [string range $arg 1 end]]} {
257 set filtered 1
258 } else {
259 # a flag argument that we don't recognize;
260 # that means we can't optimize
261 set allknown 0
262 }
263 lappend glflags $arg
3ed31a81
PM
264 }
265 default {
29582284 266 # Non-flag arguments specify commits or ranges of commits
ee66e089
PM
267 if {[string match "*...*" $arg]} {
268 lappend revargs --gitk-symmetric-diff-marker
269 }
270 lappend revargs $arg
271 }
272 }
273 }
274 set vdflags($n) $diffargs
275 set vflags($n) $glflags
276 set vrevs($n) $revargs
277 set vfiltered($n) $filtered
278 set vorigargs($n) $origargs
279 return $allknown
280}
281
282proc parseviewrevs {view revs} {
283 global vposids vnegids
284
285 if {$revs eq {}} {
286 set revs HEAD
287 }
288 if {[catch {set ids [eval exec git rev-parse $revs]} err]} {
289 # we get stdout followed by stderr in $err
290 # for an unknown rev, git rev-parse echoes it and then errors out
291 set errlines [split $err "\n"]
292 set badrev {}
293 for {set l 0} {$l < [llength $errlines]} {incr l} {
294 set line [lindex $errlines $l]
295 if {!([string length $line] == 40 && [string is xdigit $line])} {
296 if {[string match "fatal:*" $line]} {
297 if {[string match "fatal: ambiguous argument*" $line]
298 && $badrev ne {}} {
299 if {[llength $badrev] == 1} {
300 set err "unknown revision $badrev"
301 } else {
302 set err "unknown revisions: [join $badrev ", "]"
303 }
304 } else {
305 set err [join [lrange $errlines $l end] "\n"]
306 }
307 break
308 }
309 lappend badrev $line
310 }
d93f1713 311 }
3945d2c0 312 error_popup "[mc "Error parsing revisions:"] $err"
ee66e089
PM
313 return {}
314 }
315 set ret {}
316 set pos {}
317 set neg {}
318 set sdm 0
319 foreach id [split $ids "\n"] {
320 if {$id eq "--gitk-symmetric-diff-marker"} {
321 set sdm 4
322 } elseif {[string match "^*" $id]} {
323 if {$sdm != 1} {
324 lappend ret $id
325 if {$sdm == 3} {
326 set sdm 0
327 }
328 }
329 lappend neg [string range $id 1 end]
330 } else {
331 if {$sdm != 2} {
332 lappend ret $id
333 } else {
2b1fbf90 334 lset ret end $id...[lindex $ret end]
3ed31a81 335 }
ee66e089 336 lappend pos $id
3ed31a81 337 }
ee66e089 338 incr sdm -1
3ed31a81 339 }
ee66e089
PM
340 set vposids($view) $pos
341 set vnegids($view) $neg
342 return $ret
3ed31a81
PM
343}
344
f9e0b6fb 345# Start off a git log process and arrange to read its output
da7c24dd 346proc start_rev_list {view} {
6df7403a 347 global startmsecs commitidx viewcomplete curview
e439e092 348 global tclencoding
ee66e089 349 global viewargs viewargscmd viewfiles vfilelimit
d375ef9b 350 global showlocalchanges
e439e092 351 global viewactive viewinstances vmergeonly
cdc8429c 352 global mainheadid viewmainheadid viewmainheadid_orig
ee66e089 353 global vcanopt vflags vrevs vorigargs
7defefb1 354 global show_notes
9ccbdfbf 355
9ccbdfbf 356 set startmsecs [clock clicks -milliseconds]
da7c24dd 357 set commitidx($view) 0
3ed31a81
PM
358 # these are set this way for the error exits
359 set viewcomplete($view) 1
360 set viewactive($view) 0
7fcc92bf
PM
361 varcinit $view
362
2d480856
YD
363 set args $viewargs($view)
364 if {$viewargscmd($view) ne {}} {
365 if {[catch {
366 set str [exec sh -c $viewargscmd($view)]
367 } err]} {
3945d2c0 368 error_popup "[mc "Error executing --argscmd command:"] $err"
3ed31a81 369 return 0
2d480856
YD
370 }
371 set args [concat $args [split $str "\n"]]
372 }
ee66e089 373 set vcanopt($view) [parseviewargs $view $args]
3ed31a81
PM
374
375 set files $viewfiles($view)
376 if {$vmergeonly($view)} {
377 set files [unmerged_files $files]
378 if {$files eq {}} {
379 global nr_unmerged
380 if {$nr_unmerged == 0} {
381 error_popup [mc "No files selected: --merge specified but\
382 no files are unmerged."]
383 } else {
384 error_popup [mc "No files selected: --merge specified but\
385 no unmerged files are within file limit."]
386 }
387 return 0
388 }
389 }
390 set vfilelimit($view) $files
391
ee66e089
PM
392 if {$vcanopt($view)} {
393 set revs [parseviewrevs $view $vrevs($view)]
394 if {$revs eq {}} {
395 return 0
396 }
397 set args [concat $vflags($view) $revs]
398 } else {
399 set args $vorigargs($view)
400 }
401
418c4c7b 402 if {[catch {
7defefb1
KS
403 set fd [open [concat | git log --no-color -z --pretty=raw $show_notes \
404 --parents --boundary $args "--" $files] r]
418c4c7b 405 } err]} {
00abadb9 406 error_popup "[mc "Error executing git log:"] $err"
3ed31a81 407 return 0
1d10f36d 408 }
e439e092 409 set i [reg_instance $fd]
7fcc92bf 410 set viewinstances($view) [list $i]
cdc8429c
PM
411 set viewmainheadid($view) $mainheadid
412 set viewmainheadid_orig($view) $mainheadid
413 if {$files ne {} && $mainheadid ne {}} {
414 get_viewmainhead $view
415 }
416 if {$showlocalchanges && $viewmainheadid($view) ne {}} {
417 interestedin $viewmainheadid($view) dodiffindex
3e6b893f 418 }
86da5b6c 419 fconfigure $fd -blocking 0 -translation lf -eofchar {}
fd8ccbec 420 if {$tclencoding != {}} {
da7c24dd 421 fconfigure $fd -encoding $tclencoding
fd8ccbec 422 }
f806f0fb 423 filerun $fd [list getcommitlines $fd $i $view 0]
d990cedf 424 nowbusy $view [mc "Reading"]
3ed31a81
PM
425 set viewcomplete($view) 0
426 set viewactive($view) 1
427 return 1
38ad0910
PM
428}
429
e2f90ee4
AG
430proc stop_instance {inst} {
431 global commfd leftover
432
433 set fd $commfd($inst)
434 catch {
435 set pid [pid $fd]
b6326e92
AG
436
437 if {$::tcl_platform(platform) eq {windows}} {
438 exec kill -f $pid
439 } else {
440 exec kill $pid
441 }
e2f90ee4
AG
442 }
443 catch {close $fd}
444 nukefile $fd
445 unset commfd($inst)
446 unset leftover($inst)
447}
448
449proc stop_backends {} {
450 global commfd
451
452 foreach inst [array names commfd] {
453 stop_instance $inst
454 }
455}
456
7fcc92bf 457proc stop_rev_list {view} {
e2f90ee4 458 global viewinstances
22626ef4 459
7fcc92bf 460 foreach inst $viewinstances($view) {
e2f90ee4 461 stop_instance $inst
22626ef4 462 }
7fcc92bf 463 set viewinstances($view) {}
22626ef4
PM
464}
465
567c34e0 466proc reset_pending_select {selid} {
39816d60 467 global pending_select mainheadid selectheadid
567c34e0
AG
468
469 if {$selid ne {}} {
470 set pending_select $selid
39816d60
AG
471 } elseif {$selectheadid ne {}} {
472 set pending_select $selectheadid
567c34e0
AG
473 } else {
474 set pending_select $mainheadid
475 }
476}
477
478proc getcommits {selid} {
3ed31a81 479 global canv curview need_redisplay viewactive
38ad0910 480
da7c24dd 481 initlayout
3ed31a81 482 if {[start_rev_list $curview]} {
567c34e0 483 reset_pending_select $selid
3ed31a81
PM
484 show_status [mc "Reading commits..."]
485 set need_redisplay 1
486 } else {
487 show_status [mc "No commits selected"]
488 }
1d10f36d
PM
489}
490
7fcc92bf 491proc updatecommits {} {
ee66e089 492 global curview vcanopt vorigargs vfilelimit viewinstances
e439e092
AG
493 global viewactive viewcomplete tclencoding
494 global startmsecs showneartags showlocalchanges
cdc8429c 495 global mainheadid viewmainheadid viewmainheadid_orig pending_select
74cb884f 496 global hasworktree
ee66e089 497 global varcid vposids vnegids vflags vrevs
7defefb1 498 global show_notes
7fcc92bf 499
74cb884f 500 set hasworktree [hasworktree]
fc2a256f 501 rereadrefs
cdc8429c
PM
502 set view $curview
503 if {$mainheadid ne $viewmainheadid_orig($view)} {
504 if {$showlocalchanges} {
eb5f8c9c
PM
505 dohidelocalchanges
506 }
cdc8429c
PM
507 set viewmainheadid($view) $mainheadid
508 set viewmainheadid_orig($view) $mainheadid
509 if {$vfilelimit($view) ne {}} {
510 get_viewmainhead $view
eb5f8c9c
PM
511 }
512 }
cdc8429c
PM
513 if {$showlocalchanges} {
514 doshowlocalchanges
515 }
ee66e089
PM
516 if {$vcanopt($view)} {
517 set oldpos $vposids($view)
518 set oldneg $vnegids($view)
519 set revs [parseviewrevs $view $vrevs($view)]
520 if {$revs eq {}} {
521 return
522 }
523 # note: getting the delta when negative refs change is hard,
524 # and could require multiple git log invocations, so in that
525 # case we ask git log for all the commits (not just the delta)
526 if {$oldneg eq $vnegids($view)} {
527 set newrevs {}
528 set npos 0
529 # take out positive refs that we asked for before or
530 # that we have already seen
531 foreach rev $revs {
532 if {[string length $rev] == 40} {
533 if {[lsearch -exact $oldpos $rev] < 0
534 && ![info exists varcid($view,$rev)]} {
535 lappend newrevs $rev
536 incr npos
537 }
538 } else {
539 lappend $newrevs $rev
540 }
541 }
542 if {$npos == 0} return
543 set revs $newrevs
544 set vposids($view) [lsort -unique [concat $oldpos $vposids($view)]]
545 }
546 set args [concat $vflags($view) $revs --not $oldpos]
547 } else {
548 set args $vorigargs($view)
549 }
7fcc92bf 550 if {[catch {
7defefb1
KS
551 set fd [open [concat | git log --no-color -z --pretty=raw $show_notes \
552 --parents --boundary $args "--" $vfilelimit($view)] r]
7fcc92bf 553 } err]} {
3945d2c0 554 error_popup "[mc "Error executing git log:"] $err"
ee66e089 555 return
7fcc92bf
PM
556 }
557 if {$viewactive($view) == 0} {
558 set startmsecs [clock clicks -milliseconds]
559 }
e439e092 560 set i [reg_instance $fd]
7fcc92bf 561 lappend viewinstances($view) $i
7fcc92bf
PM
562 fconfigure $fd -blocking 0 -translation lf -eofchar {}
563 if {$tclencoding != {}} {
564 fconfigure $fd -encoding $tclencoding
565 }
f806f0fb 566 filerun $fd [list getcommitlines $fd $i $view 1]
7fcc92bf
PM
567 incr viewactive($view)
568 set viewcomplete($view) 0
567c34e0 569 reset_pending_select {}
b56e0a9a 570 nowbusy $view [mc "Reading"]
7fcc92bf
PM
571 if {$showneartags} {
572 getallcommits
573 }
574}
575
576proc reloadcommits {} {
577 global curview viewcomplete selectedline currentid thickerline
578 global showneartags treediffs commitinterest cached_commitrow
6df7403a 579 global targetid
7fcc92bf 580
567c34e0
AG
581 set selid {}
582 if {$selectedline ne {}} {
583 set selid $currentid
584 }
585
7fcc92bf
PM
586 if {!$viewcomplete($curview)} {
587 stop_rev_list $curview
7fcc92bf
PM
588 }
589 resetvarcs $curview
94b4a69f 590 set selectedline {}
7fcc92bf
PM
591 catch {unset currentid}
592 catch {unset thickerline}
593 catch {unset treediffs}
594 readrefs
595 changedrefs
596 if {$showneartags} {
597 getallcommits
598 }
599 clear_display
600 catch {unset commitinterest}
601 catch {unset cached_commitrow}
42a671fc 602 catch {unset targetid}
7fcc92bf 603 setcanvscroll
567c34e0 604 getcommits $selid
e7297a1c 605 return 0
7fcc92bf
PM
606}
607
6e8c8707
PM
608# This makes a string representation of a positive integer which
609# sorts as a string in numerical order
610proc strrep {n} {
611 if {$n < 16} {
612 return [format "%x" $n]
613 } elseif {$n < 256} {
614 return [format "x%.2x" $n]
615 } elseif {$n < 65536} {
616 return [format "y%.4x" $n]
617 }
618 return [format "z%.8x" $n]
619}
620
7fcc92bf
PM
621# Procedures used in reordering commits from git log (without
622# --topo-order) into the order for display.
623
624proc varcinit {view} {
f3ea5ede
PM
625 global varcstart vupptr vdownptr vleftptr vbackptr varctok varcrow
626 global vtokmod varcmod vrowmod varcix vlastins
7fcc92bf 627
7fcc92bf
PM
628 set varcstart($view) {{}}
629 set vupptr($view) {0}
630 set vdownptr($view) {0}
631 set vleftptr($view) {0}
f3ea5ede 632 set vbackptr($view) {0}
7fcc92bf
PM
633 set varctok($view) {{}}
634 set varcrow($view) {{}}
635 set vtokmod($view) {}
636 set varcmod($view) 0
e5b37ac1 637 set vrowmod($view) 0
7fcc92bf 638 set varcix($view) {{}}
f3ea5ede 639 set vlastins($view) {0}
7fcc92bf
PM
640}
641
642proc resetvarcs {view} {
643 global varcid varccommits parents children vseedcount ordertok
22387f23 644 global vshortids
7fcc92bf
PM
645
646 foreach vid [array names varcid $view,*] {
647 unset varcid($vid)
648 unset children($vid)
649 unset parents($vid)
650 }
22387f23
PM
651 foreach vid [array names vshortids $view,*] {
652 unset vshortids($vid)
653 }
7fcc92bf
PM
654 # some commits might have children but haven't been seen yet
655 foreach vid [array names children $view,*] {
656 unset children($vid)
657 }
658 foreach va [array names varccommits $view,*] {
659 unset varccommits($va)
660 }
661 foreach vd [array names vseedcount $view,*] {
662 unset vseedcount($vd)
663 }
9257d8f7 664 catch {unset ordertok}
7fcc92bf
PM
665}
666
468bcaed
PM
667# returns a list of the commits with no children
668proc seeds {v} {
669 global vdownptr vleftptr varcstart
670
671 set ret {}
672 set a [lindex $vdownptr($v) 0]
673 while {$a != 0} {
674 lappend ret [lindex $varcstart($v) $a]
675 set a [lindex $vleftptr($v) $a]
676 }
677 return $ret
678}
679
7fcc92bf 680proc newvarc {view id} {
3ed31a81 681 global varcid varctok parents children vdatemode
f3ea5ede
PM
682 global vupptr vdownptr vleftptr vbackptr varcrow varcix varcstart
683 global commitdata commitinfo vseedcount varccommits vlastins
7fcc92bf
PM
684
685 set a [llength $varctok($view)]
686 set vid $view,$id
3ed31a81 687 if {[llength $children($vid)] == 0 || $vdatemode($view)} {
7fcc92bf
PM
688 if {![info exists commitinfo($id)]} {
689 parsecommit $id $commitdata($id) 1
690 }
f5974d97 691 set cdate [lindex [lindex $commitinfo($id) 4] 0]
7fcc92bf
PM
692 if {![string is integer -strict $cdate]} {
693 set cdate 0
694 }
695 if {![info exists vseedcount($view,$cdate)]} {
696 set vseedcount($view,$cdate) -1
697 }
698 set c [incr vseedcount($view,$cdate)]
699 set cdate [expr {$cdate ^ 0xffffffff}]
700 set tok "s[strrep $cdate][strrep $c]"
7fcc92bf
PM
701 } else {
702 set tok {}
f3ea5ede
PM
703 }
704 set ka 0
705 if {[llength $children($vid)] > 0} {
706 set kid [lindex $children($vid) end]
707 set k $varcid($view,$kid)
708 if {[string compare [lindex $varctok($view) $k] $tok] > 0} {
709 set ki $kid
710 set ka $k
711 set tok [lindex $varctok($view) $k]
7fcc92bf 712 }
f3ea5ede
PM
713 }
714 if {$ka != 0} {
7fcc92bf
PM
715 set i [lsearch -exact $parents($view,$ki) $id]
716 set j [expr {[llength $parents($view,$ki)] - 1 - $i}]
7fcc92bf
PM
717 append tok [strrep $j]
718 }
f3ea5ede
PM
719 set c [lindex $vlastins($view) $ka]
720 if {$c == 0 || [string compare $tok [lindex $varctok($view) $c]] < 0} {
721 set c $ka
722 set b [lindex $vdownptr($view) $ka]
723 } else {
724 set b [lindex $vleftptr($view) $c]
725 }
726 while {$b != 0 && [string compare $tok [lindex $varctok($view) $b]] >= 0} {
727 set c $b
728 set b [lindex $vleftptr($view) $c]
729 }
730 if {$c == $ka} {
731 lset vdownptr($view) $ka $a
732 lappend vbackptr($view) 0
733 } else {
734 lset vleftptr($view) $c $a
735 lappend vbackptr($view) $c
736 }
737 lset vlastins($view) $ka $a
738 lappend vupptr($view) $ka
739 lappend vleftptr($view) $b
740 if {$b != 0} {
741 lset vbackptr($view) $b $a
742 }
7fcc92bf
PM
743 lappend varctok($view) $tok
744 lappend varcstart($view) $id
745 lappend vdownptr($view) 0
746 lappend varcrow($view) {}
747 lappend varcix($view) {}
e5b37ac1 748 set varccommits($view,$a) {}
f3ea5ede 749 lappend vlastins($view) 0
7fcc92bf
PM
750 return $a
751}
752
753proc splitvarc {p v} {
52b8ea93 754 global varcid varcstart varccommits varctok vtokmod
f3ea5ede 755 global vupptr vdownptr vleftptr vbackptr varcix varcrow vlastins
7fcc92bf
PM
756
757 set oa $varcid($v,$p)
52b8ea93 758 set otok [lindex $varctok($v) $oa]
7fcc92bf
PM
759 set ac $varccommits($v,$oa)
760 set i [lsearch -exact $varccommits($v,$oa) $p]
761 if {$i <= 0} return
762 set na [llength $varctok($v)]
763 # "%" sorts before "0"...
52b8ea93 764 set tok "$otok%[strrep $i]"
7fcc92bf
PM
765 lappend varctok($v) $tok
766 lappend varcrow($v) {}
767 lappend varcix($v) {}
768 set varccommits($v,$oa) [lrange $ac 0 [expr {$i - 1}]]
769 set varccommits($v,$na) [lrange $ac $i end]
770 lappend varcstart($v) $p
771 foreach id $varccommits($v,$na) {
772 set varcid($v,$id) $na
773 }
774 lappend vdownptr($v) [lindex $vdownptr($v) $oa]
841ea824 775 lappend vlastins($v) [lindex $vlastins($v) $oa]
7fcc92bf 776 lset vdownptr($v) $oa $na
841ea824 777 lset vlastins($v) $oa 0
7fcc92bf
PM
778 lappend vupptr($v) $oa
779 lappend vleftptr($v) 0
f3ea5ede 780 lappend vbackptr($v) 0
7fcc92bf
PM
781 for {set b [lindex $vdownptr($v) $na]} {$b != 0} {set b [lindex $vleftptr($v) $b]} {
782 lset vupptr($v) $b $na
783 }
52b8ea93
PM
784 if {[string compare $otok $vtokmod($v)] <= 0} {
785 modify_arc $v $oa
786 }
7fcc92bf
PM
787}
788
789proc renumbervarc {a v} {
790 global parents children varctok varcstart varccommits
3ed31a81 791 global vupptr vdownptr vleftptr vbackptr vlastins varcid vtokmod vdatemode
7fcc92bf
PM
792
793 set t1 [clock clicks -milliseconds]
794 set todo {}
795 set isrelated($a) 1
f3ea5ede 796 set kidchanged($a) 1
7fcc92bf
PM
797 set ntot 0
798 while {$a != 0} {
799 if {[info exists isrelated($a)]} {
800 lappend todo $a
801 set id [lindex $varccommits($v,$a) end]
802 foreach p $parents($v,$id) {
803 if {[info exists varcid($v,$p)]} {
804 set isrelated($varcid($v,$p)) 1
805 }
806 }
807 }
808 incr ntot
809 set b [lindex $vdownptr($v) $a]
810 if {$b == 0} {
811 while {$a != 0} {
812 set b [lindex $vleftptr($v) $a]
813 if {$b != 0} break
814 set a [lindex $vupptr($v) $a]
815 }
816 }
817 set a $b
818 }
819 foreach a $todo {
f3ea5ede 820 if {![info exists kidchanged($a)]} continue
7fcc92bf 821 set id [lindex $varcstart($v) $a]
f3ea5ede
PM
822 if {[llength $children($v,$id)] > 1} {
823 set children($v,$id) [lsort -command [list vtokcmp $v] \
824 $children($v,$id)]
825 }
826 set oldtok [lindex $varctok($v) $a]
3ed31a81 827 if {!$vdatemode($v)} {
f3ea5ede
PM
828 set tok {}
829 } else {
830 set tok $oldtok
831 }
832 set ka 0
c8c9f3d9
PM
833 set kid [last_real_child $v,$id]
834 if {$kid ne {}} {
f3ea5ede
PM
835 set k $varcid($v,$kid)
836 if {[string compare [lindex $varctok($v) $k] $tok] > 0} {
837 set ki $kid
838 set ka $k
839 set tok [lindex $varctok($v) $k]
7fcc92bf
PM
840 }
841 }
f3ea5ede 842 if {$ka != 0} {
7fcc92bf
PM
843 set i [lsearch -exact $parents($v,$ki) $id]
844 set j [expr {[llength $parents($v,$ki)] - 1 - $i}]
845 append tok [strrep $j]
7fcc92bf 846 }
f3ea5ede
PM
847 if {$tok eq $oldtok} {
848 continue
849 }
850 set id [lindex $varccommits($v,$a) end]
851 foreach p $parents($v,$id) {
852 if {[info exists varcid($v,$p)]} {
853 set kidchanged($varcid($v,$p)) 1
854 } else {
855 set sortkids($p) 1
856 }
857 }
858 lset varctok($v) $a $tok
7fcc92bf
PM
859 set b [lindex $vupptr($v) $a]
860 if {$b != $ka} {
9257d8f7
PM
861 if {[string compare [lindex $varctok($v) $ka] $vtokmod($v)] < 0} {
862 modify_arc $v $ka
38dfe939 863 }
9257d8f7
PM
864 if {[string compare [lindex $varctok($v) $b] $vtokmod($v)] < 0} {
865 modify_arc $v $b
38dfe939 866 }
f3ea5ede
PM
867 set c [lindex $vbackptr($v) $a]
868 set d [lindex $vleftptr($v) $a]
869 if {$c == 0} {
870 lset vdownptr($v) $b $d
7fcc92bf 871 } else {
f3ea5ede
PM
872 lset vleftptr($v) $c $d
873 }
874 if {$d != 0} {
875 lset vbackptr($v) $d $c
7fcc92bf 876 }
841ea824
PM
877 if {[lindex $vlastins($v) $b] == $a} {
878 lset vlastins($v) $b $c
879 }
7fcc92bf 880 lset vupptr($v) $a $ka
f3ea5ede
PM
881 set c [lindex $vlastins($v) $ka]
882 if {$c == 0 || \
883 [string compare $tok [lindex $varctok($v) $c]] < 0} {
884 set c $ka
885 set b [lindex $vdownptr($v) $ka]
886 } else {
887 set b [lindex $vleftptr($v) $c]
888 }
889 while {$b != 0 && \
890 [string compare $tok [lindex $varctok($v) $b]] >= 0} {
891 set c $b
892 set b [lindex $vleftptr($v) $c]
7fcc92bf 893 }
f3ea5ede
PM
894 if {$c == $ka} {
895 lset vdownptr($v) $ka $a
896 lset vbackptr($v) $a 0
897 } else {
898 lset vleftptr($v) $c $a
899 lset vbackptr($v) $a $c
7fcc92bf 900 }
f3ea5ede
PM
901 lset vleftptr($v) $a $b
902 if {$b != 0} {
903 lset vbackptr($v) $b $a
904 }
905 lset vlastins($v) $ka $a
906 }
907 }
908 foreach id [array names sortkids] {
909 if {[llength $children($v,$id)] > 1} {
910 set children($v,$id) [lsort -command [list vtokcmp $v] \
911 $children($v,$id)]
7fcc92bf
PM
912 }
913 }
914 set t2 [clock clicks -milliseconds]
915 #puts "renumbervarc did [llength $todo] of $ntot arcs in [expr {$t2-$t1}]ms"
916}
917
f806f0fb
PM
918# Fix up the graph after we have found out that in view $v,
919# $p (a commit that we have already seen) is actually the parent
920# of the last commit in arc $a.
7fcc92bf 921proc fix_reversal {p a v} {
24f7a667 922 global varcid varcstart varctok vupptr
7fcc92bf
PM
923
924 set pa $varcid($v,$p)
925 if {$p ne [lindex $varcstart($v) $pa]} {
926 splitvarc $p $v
927 set pa $varcid($v,$p)
928 }
24f7a667
PM
929 # seeds always need to be renumbered
930 if {[lindex $vupptr($v) $pa] == 0 ||
931 [string compare [lindex $varctok($v) $a] \
932 [lindex $varctok($v) $pa]] > 0} {
7fcc92bf
PM
933 renumbervarc $pa $v
934 }
935}
936
937proc insertrow {id p v} {
b8a938cf
PM
938 global cmitlisted children parents varcid varctok vtokmod
939 global varccommits ordertok commitidx numcommits curview
22387f23 940 global targetid targetrow vshortids
b8a938cf
PM
941
942 readcommit $id
943 set vid $v,$id
944 set cmitlisted($vid) 1
945 set children($vid) {}
946 set parents($vid) [list $p]
947 set a [newvarc $v $id]
948 set varcid($vid) $a
22387f23 949 lappend vshortids($v,[string range $id 0 3]) $id
b8a938cf
PM
950 if {[string compare [lindex $varctok($v) $a] $vtokmod($v)] < 0} {
951 modify_arc $v $a
952 }
953 lappend varccommits($v,$a) $id
954 set vp $v,$p
955 if {[llength [lappend children($vp) $id]] > 1} {
956 set children($vp) [lsort -command [list vtokcmp $v] $children($vp)]
957 catch {unset ordertok}
958 }
959 fix_reversal $p $a $v
960 incr commitidx($v)
961 if {$v == $curview} {
962 set numcommits $commitidx($v)
963 setcanvscroll
964 if {[info exists targetid]} {
965 if {![comes_before $targetid $p]} {
966 incr targetrow
967 }
968 }
969 }
970}
971
972proc insertfakerow {id p} {
9257d8f7 973 global varcid varccommits parents children cmitlisted
b8a938cf 974 global commitidx varctok vtokmod targetid targetrow curview numcommits
7fcc92bf 975
b8a938cf 976 set v $curview
7fcc92bf
PM
977 set a $varcid($v,$p)
978 set i [lsearch -exact $varccommits($v,$a) $p]
979 if {$i < 0} {
b8a938cf 980 puts "oops: insertfakerow can't find [shortids $p] on arc $a"
7fcc92bf
PM
981 return
982 }
983 set children($v,$id) {}
984 set parents($v,$id) [list $p]
985 set varcid($v,$id) $a
9257d8f7 986 lappend children($v,$p) $id
7fcc92bf 987 set cmitlisted($v,$id) 1
b8a938cf 988 set numcommits [incr commitidx($v)]
7fcc92bf
PM
989 # note we deliberately don't update varcstart($v) even if $i == 0
990 set varccommits($v,$a) [linsert $varccommits($v,$a) $i $id]
c9cfdc96 991 modify_arc $v $a $i
42a671fc
PM
992 if {[info exists targetid]} {
993 if {![comes_before $targetid $p]} {
994 incr targetrow
995 }
996 }
b8a938cf 997 setcanvscroll
9257d8f7 998 drawvisible
7fcc92bf
PM
999}
1000
b8a938cf 1001proc removefakerow {id} {
9257d8f7 1002 global varcid varccommits parents children commitidx
fc2a256f 1003 global varctok vtokmod cmitlisted currentid selectedline
b8a938cf 1004 global targetid curview numcommits
7fcc92bf 1005
b8a938cf 1006 set v $curview
7fcc92bf 1007 if {[llength $parents($v,$id)] != 1} {
b8a938cf 1008 puts "oops: removefakerow [shortids $id] has [llength $parents($v,$id)] parents"
7fcc92bf
PM
1009 return
1010 }
1011 set p [lindex $parents($v,$id) 0]
1012 set a $varcid($v,$id)
1013 set i [lsearch -exact $varccommits($v,$a) $id]
1014 if {$i < 0} {
b8a938cf 1015 puts "oops: removefakerow can't find [shortids $id] on arc $a"
7fcc92bf
PM
1016 return
1017 }
1018 unset varcid($v,$id)
1019 set varccommits($v,$a) [lreplace $varccommits($v,$a) $i $i]
1020 unset parents($v,$id)
1021 unset children($v,$id)
1022 unset cmitlisted($v,$id)
b8a938cf 1023 set numcommits [incr commitidx($v) -1]
7fcc92bf
PM
1024 set j [lsearch -exact $children($v,$p) $id]
1025 if {$j >= 0} {
1026 set children($v,$p) [lreplace $children($v,$p) $j $j]
1027 }
c9cfdc96 1028 modify_arc $v $a $i
fc2a256f
PM
1029 if {[info exist currentid] && $id eq $currentid} {
1030 unset currentid
94b4a69f 1031 set selectedline {}
fc2a256f 1032 }
42a671fc
PM
1033 if {[info exists targetid] && $targetid eq $id} {
1034 set targetid $p
1035 }
b8a938cf 1036 setcanvscroll
9257d8f7 1037 drawvisible
7fcc92bf
PM
1038}
1039
aa43561a
PM
1040proc real_children {vp} {
1041 global children nullid nullid2
1042
1043 set kids {}
1044 foreach id $children($vp) {
1045 if {$id ne $nullid && $id ne $nullid2} {
1046 lappend kids $id
1047 }
1048 }
1049 return $kids
1050}
1051
c8c9f3d9
PM
1052proc first_real_child {vp} {
1053 global children nullid nullid2
1054
1055 foreach id $children($vp) {
1056 if {$id ne $nullid && $id ne $nullid2} {
1057 return $id
1058 }
1059 }
1060 return {}
1061}
1062
1063proc last_real_child {vp} {
1064 global children nullid nullid2
1065
1066 set kids $children($vp)
1067 for {set i [llength $kids]} {[incr i -1] >= 0} {} {
1068 set id [lindex $kids $i]
1069 if {$id ne $nullid && $id ne $nullid2} {
1070 return $id
1071 }
1072 }
1073 return {}
1074}
1075
7fcc92bf
PM
1076proc vtokcmp {v a b} {
1077 global varctok varcid
1078
1079 return [string compare [lindex $varctok($v) $varcid($v,$a)] \
1080 [lindex $varctok($v) $varcid($v,$b)]]
1081}
1082
c9cfdc96
PM
1083# This assumes that if lim is not given, the caller has checked that
1084# arc a's token is less than $vtokmod($v)
e5b37ac1
PM
1085proc modify_arc {v a {lim {}}} {
1086 global varctok vtokmod varcmod varcrow vupptr curview vrowmod varccommits
9257d8f7 1087
c9cfdc96
PM
1088 if {$lim ne {}} {
1089 set c [string compare [lindex $varctok($v) $a] $vtokmod($v)]
1090 if {$c > 0} return
1091 if {$c == 0} {
1092 set r [lindex $varcrow($v) $a]
1093 if {$r ne {} && $vrowmod($v) <= $r + $lim} return
1094 }
1095 }
9257d8f7
PM
1096 set vtokmod($v) [lindex $varctok($v) $a]
1097 set varcmod($v) $a
1098 if {$v == $curview} {
1099 while {$a != 0 && [lindex $varcrow($v) $a] eq {}} {
1100 set a [lindex $vupptr($v) $a]
e5b37ac1 1101 set lim {}
9257d8f7 1102 }
e5b37ac1
PM
1103 set r 0
1104 if {$a != 0} {
1105 if {$lim eq {}} {
1106 set lim [llength $varccommits($v,$a)]
1107 }
1108 set r [expr {[lindex $varcrow($v) $a] + $lim}]
1109 }
1110 set vrowmod($v) $r
0c27886e 1111 undolayout $r
9257d8f7
PM
1112 }
1113}
1114
7fcc92bf 1115proc update_arcrows {v} {
e5b37ac1 1116 global vtokmod varcmod vrowmod varcrow commitidx currentid selectedline
24f7a667 1117 global varcid vrownum varcorder varcix varccommits
7fcc92bf 1118 global vupptr vdownptr vleftptr varctok
24f7a667 1119 global displayorder parentlist curview cached_commitrow
7fcc92bf 1120
c9cfdc96
PM
1121 if {$vrowmod($v) == $commitidx($v)} return
1122 if {$v == $curview} {
1123 if {[llength $displayorder] > $vrowmod($v)} {
1124 set displayorder [lrange $displayorder 0 [expr {$vrowmod($v) - 1}]]
1125 set parentlist [lrange $parentlist 0 [expr {$vrowmod($v) - 1}]]
1126 }
1127 catch {unset cached_commitrow}
1128 }
7fcc92bf
PM
1129 set narctot [expr {[llength $varctok($v)] - 1}]
1130 set a $varcmod($v)
1131 while {$a != 0 && [lindex $varcix($v) $a] eq {}} {
1132 # go up the tree until we find something that has a row number,
1133 # or we get to a seed
1134 set a [lindex $vupptr($v) $a]
1135 }
1136 if {$a == 0} {
1137 set a [lindex $vdownptr($v) 0]
1138 if {$a == 0} return
1139 set vrownum($v) {0}
1140 set varcorder($v) [list $a]
1141 lset varcix($v) $a 0
1142 lset varcrow($v) $a 0
1143 set arcn 0
1144 set row 0
1145 } else {
1146 set arcn [lindex $varcix($v) $a]
7fcc92bf
PM
1147 if {[llength $vrownum($v)] > $arcn + 1} {
1148 set vrownum($v) [lrange $vrownum($v) 0 $arcn]
1149 set varcorder($v) [lrange $varcorder($v) 0 $arcn]
1150 }
1151 set row [lindex $varcrow($v) $a]
1152 }
7fcc92bf
PM
1153 while {1} {
1154 set p $a
1155 incr row [llength $varccommits($v,$a)]
1156 # go down if possible
1157 set b [lindex $vdownptr($v) $a]
1158 if {$b == 0} {
1159 # if not, go left, or go up until we can go left
1160 while {$a != 0} {
1161 set b [lindex $vleftptr($v) $a]
1162 if {$b != 0} break
1163 set a [lindex $vupptr($v) $a]
1164 }
1165 if {$a == 0} break
1166 }
1167 set a $b
1168 incr arcn
1169 lappend vrownum($v) $row
1170 lappend varcorder($v) $a
1171 lset varcix($v) $a $arcn
1172 lset varcrow($v) $a $row
1173 }
e5b37ac1
PM
1174 set vtokmod($v) [lindex $varctok($v) $p]
1175 set varcmod($v) $p
1176 set vrowmod($v) $row
7fcc92bf
PM
1177 if {[info exists currentid]} {
1178 set selectedline [rowofcommit $currentid]
1179 }
7fcc92bf
PM
1180}
1181
1182# Test whether view $v contains commit $id
1183proc commitinview {id v} {
1184 global varcid
1185
1186 return [info exists varcid($v,$id)]
1187}
1188
1189# Return the row number for commit $id in the current view
1190proc rowofcommit {id} {
1191 global varcid varccommits varcrow curview cached_commitrow
9257d8f7 1192 global varctok vtokmod
7fcc92bf 1193
7fcc92bf
PM
1194 set v $curview
1195 if {![info exists varcid($v,$id)]} {
1196 puts "oops rowofcommit no arc for [shortids $id]"
1197 return {}
1198 }
1199 set a $varcid($v,$id)
fc2a256f 1200 if {[string compare [lindex $varctok($v) $a] $vtokmod($v)] >= 0} {
9257d8f7
PM
1201 update_arcrows $v
1202 }
31c0eaa8
PM
1203 if {[info exists cached_commitrow($id)]} {
1204 return $cached_commitrow($id)
1205 }
7fcc92bf
PM
1206 set i [lsearch -exact $varccommits($v,$a) $id]
1207 if {$i < 0} {
1208 puts "oops didn't find commit [shortids $id] in arc $a"
1209 return {}
1210 }
1211 incr i [lindex $varcrow($v) $a]
1212 set cached_commitrow($id) $i
1213 return $i
1214}
1215
42a671fc
PM
1216# Returns 1 if a is on an earlier row than b, otherwise 0
1217proc comes_before {a b} {
1218 global varcid varctok curview
1219
1220 set v $curview
1221 if {$a eq $b || ![info exists varcid($v,$a)] || \
1222 ![info exists varcid($v,$b)]} {
1223 return 0
1224 }
1225 if {$varcid($v,$a) != $varcid($v,$b)} {
1226 return [expr {[string compare [lindex $varctok($v) $varcid($v,$a)] \
1227 [lindex $varctok($v) $varcid($v,$b)]] < 0}]
1228 }
1229 return [expr {[rowofcommit $a] < [rowofcommit $b]}]
1230}
1231
7fcc92bf
PM
1232proc bsearch {l elt} {
1233 if {[llength $l] == 0 || $elt <= [lindex $l 0]} {
1234 return 0
1235 }
1236 set lo 0
1237 set hi [llength $l]
1238 while {$hi - $lo > 1} {
1239 set mid [expr {int(($lo + $hi) / 2)}]
1240 set t [lindex $l $mid]
1241 if {$elt < $t} {
1242 set hi $mid
1243 } elseif {$elt > $t} {
1244 set lo $mid
1245 } else {
1246 return $mid
1247 }
1248 }
1249 return $lo
1250}
1251
1252# Make sure rows $start..$end-1 are valid in displayorder and parentlist
1253proc make_disporder {start end} {
1254 global vrownum curview commitidx displayorder parentlist
e5b37ac1 1255 global varccommits varcorder parents vrowmod varcrow
7fcc92bf
PM
1256 global d_valid_start d_valid_end
1257
e5b37ac1 1258 if {$end > $vrowmod($curview)} {
9257d8f7
PM
1259 update_arcrows $curview
1260 }
7fcc92bf
PM
1261 set ai [bsearch $vrownum($curview) $start]
1262 set start [lindex $vrownum($curview) $ai]
1263 set narc [llength $vrownum($curview)]
1264 for {set r $start} {$ai < $narc && $r < $end} {incr ai} {
1265 set a [lindex $varcorder($curview) $ai]
1266 set l [llength $displayorder]
1267 set al [llength $varccommits($curview,$a)]
1268 if {$l < $r + $al} {
1269 if {$l < $r} {
1270 set pad [ntimes [expr {$r - $l}] {}]
1271 set displayorder [concat $displayorder $pad]
1272 set parentlist [concat $parentlist $pad]
1273 } elseif {$l > $r} {
1274 set displayorder [lrange $displayorder 0 [expr {$r - 1}]]
1275 set parentlist [lrange $parentlist 0 [expr {$r - 1}]]
1276 }
1277 foreach id $varccommits($curview,$a) {
1278 lappend displayorder $id
1279 lappend parentlist $parents($curview,$id)
1280 }
17529cf9 1281 } elseif {[lindex $displayorder [expr {$r + $al - 1}]] eq {}} {
7fcc92bf
PM
1282 set i $r
1283 foreach id $varccommits($curview,$a) {
1284 lset displayorder $i $id
1285 lset parentlist $i $parents($curview,$id)
1286 incr i
1287 }
1288 }
1289 incr r $al
1290 }
1291}
1292
1293proc commitonrow {row} {
1294 global displayorder
1295
1296 set id [lindex $displayorder $row]
1297 if {$id eq {}} {
1298 make_disporder $row [expr {$row + 1}]
1299 set id [lindex $displayorder $row]
1300 }
1301 return $id
1302}
1303
1304proc closevarcs {v} {
1305 global varctok varccommits varcid parents children
d375ef9b 1306 global cmitlisted commitidx vtokmod
7fcc92bf
PM
1307
1308 set missing_parents 0
1309 set scripts {}
1310 set narcs [llength $varctok($v)]
1311 for {set a 1} {$a < $narcs} {incr a} {
1312 set id [lindex $varccommits($v,$a) end]
1313 foreach p $parents($v,$id) {
1314 if {[info exists varcid($v,$p)]} continue
1315 # add p as a new commit
1316 incr missing_parents
1317 set cmitlisted($v,$p) 0
1318 set parents($v,$p) {}
1319 if {[llength $children($v,$p)] == 1 &&
1320 [llength $parents($v,$id)] == 1} {
1321 set b $a
1322 } else {
1323 set b [newvarc $v $p]
1324 }
1325 set varcid($v,$p) $b
9257d8f7
PM
1326 if {[string compare [lindex $varctok($v) $b] $vtokmod($v)] < 0} {
1327 modify_arc $v $b
7fcc92bf 1328 }
e5b37ac1 1329 lappend varccommits($v,$b) $p
7fcc92bf 1330 incr commitidx($v)
d375ef9b 1331 set scripts [check_interest $p $scripts]
7fcc92bf
PM
1332 }
1333 }
1334 if {$missing_parents > 0} {
7fcc92bf
PM
1335 foreach s $scripts {
1336 eval $s
1337 }
1338 }
1339}
1340
f806f0fb
PM
1341# Use $rwid as a substitute for $id, i.e. reparent $id's children to $rwid
1342# Assumes we already have an arc for $rwid.
1343proc rewrite_commit {v id rwid} {
1344 global children parents varcid varctok vtokmod varccommits
1345
1346 foreach ch $children($v,$id) {
1347 # make $rwid be $ch's parent in place of $id
1348 set i [lsearch -exact $parents($v,$ch) $id]
1349 if {$i < 0} {
1350 puts "oops rewrite_commit didn't find $id in parent list for $ch"
1351 }
1352 set parents($v,$ch) [lreplace $parents($v,$ch) $i $i $rwid]
1353 # add $ch to $rwid's children and sort the list if necessary
1354 if {[llength [lappend children($v,$rwid) $ch]] > 1} {
1355 set children($v,$rwid) [lsort -command [list vtokcmp $v] \
1356 $children($v,$rwid)]
1357 }
1358 # fix the graph after joining $id to $rwid
1359 set a $varcid($v,$ch)
1360 fix_reversal $rwid $a $v
c9cfdc96
PM
1361 # parentlist is wrong for the last element of arc $a
1362 # even if displayorder is right, hence the 3rd arg here
1363 modify_arc $v $a [expr {[llength $varccommits($v,$a)] - 1}]
f806f0fb
PM
1364 }
1365}
1366
d375ef9b
PM
1367# Mechanism for registering a command to be executed when we come
1368# across a particular commit. To handle the case when only the
1369# prefix of the commit is known, the commitinterest array is now
1370# indexed by the first 4 characters of the ID. Each element is a
1371# list of id, cmd pairs.
1372proc interestedin {id cmd} {
1373 global commitinterest
1374
1375 lappend commitinterest([string range $id 0 3]) $id $cmd
1376}
1377
1378proc check_interest {id scripts} {
1379 global commitinterest
1380
1381 set prefix [string range $id 0 3]
1382 if {[info exists commitinterest($prefix)]} {
1383 set newlist {}
1384 foreach {i script} $commitinterest($prefix) {
1385 if {[string match "$i*" $id]} {
1386 lappend scripts [string map [list "%I" $id "%P" $i] $script]
1387 } else {
1388 lappend newlist $i $script
1389 }
1390 }
1391 if {$newlist ne {}} {
1392 set commitinterest($prefix) $newlist
1393 } else {
1394 unset commitinterest($prefix)
1395 }
1396 }
1397 return $scripts
1398}
1399
f806f0fb 1400proc getcommitlines {fd inst view updating} {
d375ef9b 1401 global cmitlisted leftover
3ed31a81 1402 global commitidx commitdata vdatemode
7fcc92bf 1403 global parents children curview hlview
468bcaed 1404 global idpending ordertok
22387f23 1405 global varccommits varcid varctok vtokmod vfilelimit vshortids
9ccbdfbf 1406
d1e46756 1407 set stuff [read $fd 500000]
005a2f4e 1408 # git log doesn't terminate the last commit with a null...
7fcc92bf 1409 if {$stuff == {} && $leftover($inst) ne {} && [eof $fd]} {
005a2f4e
PM
1410 set stuff "\0"
1411 }
b490a991 1412 if {$stuff == {}} {
7eb3cb9c
PM
1413 if {![eof $fd]} {
1414 return 1
1415 }
6df7403a 1416 global commfd viewcomplete viewactive viewname
7fcc92bf
PM
1417 global viewinstances
1418 unset commfd($inst)
1419 set i [lsearch -exact $viewinstances($view) $inst]
1420 if {$i >= 0} {
1421 set viewinstances($view) [lreplace $viewinstances($view) $i $i]
b0cdca99 1422 }
f0654861 1423 # set it blocking so we wait for the process to terminate
da7c24dd 1424 fconfigure $fd -blocking 1
098dd8a3
PM
1425 if {[catch {close $fd} err]} {
1426 set fv {}
1427 if {$view != $curview} {
1428 set fv " for the \"$viewname($view)\" view"
da7c24dd 1429 }
098dd8a3
PM
1430 if {[string range $err 0 4] == "usage"} {
1431 set err "Gitk: error reading commits$fv:\
f9e0b6fb 1432 bad arguments to git log."
098dd8a3
PM
1433 if {$viewname($view) eq "Command line"} {
1434 append err \
f9e0b6fb 1435 " (Note: arguments to gitk are passed to git log\
098dd8a3
PM
1436 to allow selection of commits to be displayed.)"
1437 }
1438 } else {
1439 set err "Error reading commits$fv: $err"
1440 }
1441 error_popup $err
1d10f36d 1442 }
7fcc92bf
PM
1443 if {[incr viewactive($view) -1] <= 0} {
1444 set viewcomplete($view) 1
1445 # Check if we have seen any ids listed as parents that haven't
1446 # appeared in the list
1447 closevarcs $view
1448 notbusy $view
7fcc92bf 1449 }
098dd8a3 1450 if {$view == $curview} {
ac1276ab 1451 run chewcommits
9a40c50c 1452 }
7eb3cb9c 1453 return 0
9a40c50c 1454 }
b490a991 1455 set start 0
8f7d0cec 1456 set gotsome 0
7fcc92bf 1457 set scripts {}
b490a991
PM
1458 while 1 {
1459 set i [string first "\0" $stuff $start]
1460 if {$i < 0} {
7fcc92bf 1461 append leftover($inst) [string range $stuff $start end]
9f1afe05 1462 break
9ccbdfbf 1463 }
b490a991 1464 if {$start == 0} {
7fcc92bf 1465 set cmit $leftover($inst)
8f7d0cec 1466 append cmit [string range $stuff 0 [expr {$i - 1}]]
7fcc92bf 1467 set leftover($inst) {}
8f7d0cec
PM
1468 } else {
1469 set cmit [string range $stuff $start [expr {$i - 1}]]
b490a991
PM
1470 }
1471 set start [expr {$i + 1}]
e5ea701b
PM
1472 set j [string first "\n" $cmit]
1473 set ok 0
16c1ff96 1474 set listed 1
c961b228
PM
1475 if {$j >= 0 && [string match "commit *" $cmit]} {
1476 set ids [string range $cmit 7 [expr {$j - 1}]]
1407ade9 1477 if {[string match {[-^<>]*} $ids]} {
c961b228
PM
1478 switch -- [string index $ids 0] {
1479 "-" {set listed 0}
1407ade9
LT
1480 "^" {set listed 2}
1481 "<" {set listed 3}
1482 ">" {set listed 4}
c961b228 1483 }
16c1ff96
PM
1484 set ids [string range $ids 1 end]
1485 }
e5ea701b
PM
1486 set ok 1
1487 foreach id $ids {
8f7d0cec 1488 if {[string length $id] != 40} {
e5ea701b
PM
1489 set ok 0
1490 break
1491 }
1492 }
1493 }
1494 if {!$ok} {
7e952e79
PM
1495 set shortcmit $cmit
1496 if {[string length $shortcmit] > 80} {
1497 set shortcmit "[string range $shortcmit 0 80]..."
1498 }
d990cedf 1499 error_popup "[mc "Can't parse git log output:"] {$shortcmit}"
b490a991
PM
1500 exit 1
1501 }
e5ea701b 1502 set id [lindex $ids 0]
7fcc92bf 1503 set vid $view,$id
f806f0fb 1504
22387f23
PM
1505 lappend vshortids($view,[string range $id 0 3]) $id
1506
f806f0fb 1507 if {!$listed && $updating && ![info exists varcid($vid)] &&
3ed31a81 1508 $vfilelimit($view) ne {}} {
f806f0fb
PM
1509 # git log doesn't rewrite parents for unlisted commits
1510 # when doing path limiting, so work around that here
1511 # by working out the rewritten parent with git rev-list
1512 # and if we already know about it, using the rewritten
1513 # parent as a substitute parent for $id's children.
1514 if {![catch {
1515 set rwid [exec git rev-list --first-parent --max-count=1 \
3ed31a81 1516 $id -- $vfilelimit($view)]
f806f0fb
PM
1517 }]} {
1518 if {$rwid ne {} && [info exists varcid($view,$rwid)]} {
1519 # use $rwid in place of $id
1520 rewrite_commit $view $id $rwid
1521 continue
1522 }
1523 }
1524 }
1525
f1bf4ee6
PM
1526 set a 0
1527 if {[info exists varcid($vid)]} {
1528 if {$cmitlisted($vid) || !$listed} continue
1529 set a $varcid($vid)
1530 }
16c1ff96
PM
1531 if {$listed} {
1532 set olds [lrange $ids 1 end]
16c1ff96
PM
1533 } else {
1534 set olds {}
1535 }
f7a3e8d2 1536 set commitdata($id) [string range $cmit [expr {$j + 1}] end]
7fcc92bf
PM
1537 set cmitlisted($vid) $listed
1538 set parents($vid) $olds
7fcc92bf
PM
1539 if {![info exists children($vid)]} {
1540 set children($vid) {}
f1bf4ee6 1541 } elseif {$a == 0 && [llength $children($vid)] == 1} {
f3ea5ede
PM
1542 set k [lindex $children($vid) 0]
1543 if {[llength $parents($view,$k)] == 1 &&
3ed31a81 1544 (!$vdatemode($view) ||
f3ea5ede
PM
1545 $varcid($view,$k) == [llength $varctok($view)] - 1)} {
1546 set a $varcid($view,$k)
7fcc92bf 1547 }
da7c24dd 1548 }
7fcc92bf
PM
1549 if {$a == 0} {
1550 # new arc
1551 set a [newvarc $view $id]
1552 }
e5b37ac1
PM
1553 if {[string compare [lindex $varctok($view) $a] $vtokmod($view)] < 0} {
1554 modify_arc $view $a
1555 }
f1bf4ee6
PM
1556 if {![info exists varcid($vid)]} {
1557 set varcid($vid) $a
1558 lappend varccommits($view,$a) $id
1559 incr commitidx($view)
1560 }
e5b37ac1 1561
7fcc92bf
PM
1562 set i 0
1563 foreach p $olds {
1564 if {$i == 0 || [lsearch -exact $olds $p] >= $i} {
1565 set vp $view,$p
1566 if {[llength [lappend children($vp) $id]] > 1 &&
1567 [vtokcmp $view [lindex $children($vp) end-1] $id] > 0} {
1568 set children($vp) [lsort -command [list vtokcmp $view] \
1569 $children($vp)]
9257d8f7 1570 catch {unset ordertok}
7fcc92bf 1571 }
f3ea5ede
PM
1572 if {[info exists varcid($view,$p)]} {
1573 fix_reversal $p $a $view
1574 }
7fcc92bf
PM
1575 }
1576 incr i
1577 }
7fcc92bf 1578
d375ef9b 1579 set scripts [check_interest $id $scripts]
8f7d0cec
PM
1580 set gotsome 1
1581 }
1582 if {$gotsome} {
ac1276ab
PM
1583 global numcommits hlview
1584
1585 if {$view == $curview} {
1586 set numcommits $commitidx($view)
1587 run chewcommits
1588 }
1589 if {[info exists hlview] && $view == $hlview} {
1590 # we never actually get here...
1591 run vhighlightmore
1592 }
7fcc92bf
PM
1593 foreach s $scripts {
1594 eval $s
1595 }
9ccbdfbf 1596 }
7eb3cb9c 1597 return 2
9ccbdfbf
PM
1598}
1599
ac1276ab 1600proc chewcommits {} {
f5f3c2e2 1601 global curview hlview viewcomplete
7fcc92bf 1602 global pending_select
7eb3cb9c 1603
ac1276ab
PM
1604 layoutmore
1605 if {$viewcomplete($curview)} {
1606 global commitidx varctok
1607 global numcommits startmsecs
ac1276ab
PM
1608
1609 if {[info exists pending_select]} {
835e62ae
AG
1610 update
1611 reset_pending_select {}
1612
1613 if {[commitinview $pending_select $curview]} {
1614 selectline [rowofcommit $pending_select] 1
1615 } else {
1616 set row [first_real_row]
1617 selectline $row 1
1618 }
7eb3cb9c 1619 }
ac1276ab
PM
1620 if {$commitidx($curview) > 0} {
1621 #set ms [expr {[clock clicks -milliseconds] - $startmsecs}]
1622 #puts "overall $ms ms for $numcommits commits"
1623 #puts "[llength $varctok($view)] arcs, $commitidx($view) commits"
1624 } else {
1625 show_status [mc "No commits selected"]
1626 }
1627 notbusy layout
b664550c 1628 }
f5f3c2e2 1629 return 0
1db95b00
PM
1630}
1631
590915da
AG
1632proc do_readcommit {id} {
1633 global tclencoding
1634
1635 # Invoke git-log to handle automatic encoding conversion
1636 set fd [open [concat | git log --no-color --pretty=raw -1 $id] r]
1637 # Read the results using i18n.logoutputencoding
1638 fconfigure $fd -translation lf -eofchar {}
1639 if {$tclencoding != {}} {
1640 fconfigure $fd -encoding $tclencoding
1641 }
1642 set contents [read $fd]
1643 close $fd
1644 # Remove the heading line
1645 regsub {^commit [0-9a-f]+\n} $contents {} contents
1646
1647 return $contents
1648}
1649
1db95b00 1650proc readcommit {id} {
590915da
AG
1651 if {[catch {set contents [do_readcommit $id]}]} return
1652 parsecommit $id $contents 1
b490a991
PM
1653}
1654
8f7d0cec 1655proc parsecommit {id contents listed} {
ef73896b 1656 global commitinfo
b5c2f306
SV
1657
1658 set inhdr 1
1659 set comment {}
1660 set headline {}
1661 set auname {}
1662 set audate {}
1663 set comname {}
1664 set comdate {}
232475d3
PM
1665 set hdrend [string first "\n\n" $contents]
1666 if {$hdrend < 0} {
1667 # should never happen...
1668 set hdrend [string length $contents]
1669 }
1670 set header [string range $contents 0 [expr {$hdrend - 1}]]
1671 set comment [string range $contents [expr {$hdrend + 2}] end]
1672 foreach line [split $header "\n"] {
61f57cb0 1673 set line [split $line " "]
232475d3
PM
1674 set tag [lindex $line 0]
1675 if {$tag == "author"} {
f5974d97 1676 set audate [lrange $line end-1 end]
61f57cb0 1677 set auname [join [lrange $line 1 end-2] " "]
232475d3 1678 } elseif {$tag == "committer"} {
f5974d97 1679 set comdate [lrange $line end-1 end]
61f57cb0 1680 set comname [join [lrange $line 1 end-2] " "]
1db95b00
PM
1681 }
1682 }
232475d3 1683 set headline {}
43c25074
PM
1684 # take the first non-blank line of the comment as the headline
1685 set headline [string trimleft $comment]
1686 set i [string first "\n" $headline]
232475d3 1687 if {$i >= 0} {
43c25074
PM
1688 set headline [string range $headline 0 $i]
1689 }
1690 set headline [string trimright $headline]
1691 set i [string first "\r" $headline]
1692 if {$i >= 0} {
1693 set headline [string trimright [string range $headline 0 $i]]
232475d3
PM
1694 }
1695 if {!$listed} {
f9e0b6fb 1696 # git log indents the comment by 4 spaces;
8974c6f9 1697 # if we got this via git cat-file, add the indentation
232475d3
PM
1698 set newcomment {}
1699 foreach line [split $comment "\n"] {
1700 append newcomment " "
1701 append newcomment $line
f6e2869f 1702 append newcomment "\n"
232475d3
PM
1703 }
1704 set comment $newcomment
1db95b00 1705 }
36242490 1706 set hasnote [string first "\nNotes:\n" $contents]
e5c2d856 1707 set commitinfo($id) [list $headline $auname $audate \
36242490 1708 $comname $comdate $comment $hasnote]
1db95b00
PM
1709}
1710
f7a3e8d2 1711proc getcommit {id} {
79b2c75e 1712 global commitdata commitinfo
8ed16484 1713
f7a3e8d2
PM
1714 if {[info exists commitdata($id)]} {
1715 parsecommit $id $commitdata($id) 1
8ed16484
PM
1716 } else {
1717 readcommit $id
1718 if {![info exists commitinfo($id)]} {
d990cedf 1719 set commitinfo($id) [list [mc "No commit information available"]]
8ed16484
PM
1720 }
1721 }
1722 return 1
1723}
1724
d375ef9b
PM
1725# Expand an abbreviated commit ID to a list of full 40-char IDs that match
1726# and are present in the current view.
1727# This is fairly slow...
1728proc longid {prefix} {
22387f23 1729 global varcid curview vshortids
d375ef9b
PM
1730
1731 set ids {}
22387f23
PM
1732 if {[string length $prefix] >= 4} {
1733 set vshortid $curview,[string range $prefix 0 3]
1734 if {[info exists vshortids($vshortid)]} {
1735 foreach id $vshortids($vshortid) {
1736 if {[string match "$prefix*" $id]} {
1737 if {[lsearch -exact $ids $id] < 0} {
1738 lappend ids $id
1739 if {[llength $ids] >= 2} break
1740 }
1741 }
1742 }
1743 }
1744 } else {
1745 foreach match [array names varcid "$curview,$prefix*"] {
1746 lappend ids [lindex [split $match ","] 1]
1747 if {[llength $ids] >= 2} break
1748 }
d375ef9b
PM
1749 }
1750 return $ids
1751}
1752
887fe3c4 1753proc readrefs {} {
62d3ea65 1754 global tagids idtags headids idheads tagobjid
219ea3a9 1755 global otherrefids idotherrefs mainhead mainheadid
39816d60 1756 global selecthead selectheadid
ffe15297 1757 global hideremotes
106288cb 1758
b5c2f306
SV
1759 foreach v {tagids idtags headids idheads otherrefids idotherrefs} {
1760 catch {unset $v}
1761 }
62d3ea65
PM
1762 set refd [open [list | git show-ref -d] r]
1763 while {[gets $refd line] >= 0} {
1764 if {[string index $line 40] ne " "} continue
1765 set id [string range $line 0 39]
1766 set ref [string range $line 41 end]
1767 if {![string match "refs/*" $ref]} continue
1768 set name [string range $ref 5 end]
1769 if {[string match "remotes/*" $name]} {
ffe15297 1770 if {![string match "*/HEAD" $name] && !$hideremotes} {
62d3ea65
PM
1771 set headids($name) $id
1772 lappend idheads($id) $name
f1d83ba3 1773 }
62d3ea65
PM
1774 } elseif {[string match "heads/*" $name]} {
1775 set name [string range $name 6 end]
36a7cad6
JH
1776 set headids($name) $id
1777 lappend idheads($id) $name
62d3ea65
PM
1778 } elseif {[string match "tags/*" $name]} {
1779 # this lets refs/tags/foo^{} overwrite refs/tags/foo,
1780 # which is what we want since the former is the commit ID
1781 set name [string range $name 5 end]
1782 if {[string match "*^{}" $name]} {
1783 set name [string range $name 0 end-3]
1784 } else {
1785 set tagobjid($name) $id
1786 }
1787 set tagids($name) $id
1788 lappend idtags($id) $name
36a7cad6
JH
1789 } else {
1790 set otherrefids($name) $id
1791 lappend idotherrefs($id) $name
f1d83ba3
PM
1792 }
1793 }
062d671f 1794 catch {close $refd}
8a48571c 1795 set mainhead {}
219ea3a9 1796 set mainheadid {}
8a48571c 1797 catch {
c11ff120 1798 set mainheadid [exec git rev-parse HEAD]
8a48571c
PM
1799 set thehead [exec git symbolic-ref HEAD]
1800 if {[string match "refs/heads/*" $thehead]} {
1801 set mainhead [string range $thehead 11 end]
1802 }
1803 }
39816d60
AG
1804 set selectheadid {}
1805 if {$selecthead ne {}} {
1806 catch {
1807 set selectheadid [exec git rev-parse --verify $selecthead]
1808 }
1809 }
887fe3c4
PM
1810}
1811
8f489363
PM
1812# skip over fake commits
1813proc first_real_row {} {
7fcc92bf 1814 global nullid nullid2 numcommits
8f489363
PM
1815
1816 for {set row 0} {$row < $numcommits} {incr row} {
7fcc92bf 1817 set id [commitonrow $row]
8f489363
PM
1818 if {$id ne $nullid && $id ne $nullid2} {
1819 break
1820 }
1821 }
1822 return $row
1823}
1824
e11f1233
PM
1825# update things for a head moved to a child of its previous location
1826proc movehead {id name} {
1827 global headids idheads
1828
1829 removehead $headids($name) $name
1830 set headids($name) $id
1831 lappend idheads($id) $name
1832}
1833
1834# update things when a head has been removed
1835proc removehead {id name} {
1836 global headids idheads
1837
1838 if {$idheads($id) eq $name} {
1839 unset idheads($id)
1840 } else {
1841 set i [lsearch -exact $idheads($id) $name]
1842 if {$i >= 0} {
1843 set idheads($id) [lreplace $idheads($id) $i $i]
1844 }
1845 }
1846 unset headids($name)
1847}
1848
d93f1713
PT
1849proc ttk_toplevel {w args} {
1850 global use_ttk
1851 eval [linsert $args 0 ::toplevel $w]
1852 if {$use_ttk} {
1853 place [ttk::frame $w._toplevel_background] -x 0 -y 0 -relwidth 1 -relheight 1
1854 }
1855 return $w
1856}
1857
e7d64008
AG
1858proc make_transient {window origin} {
1859 global have_tk85
1860
1861 # In MacOS Tk 8.4 transient appears to work by setting
1862 # overrideredirect, which is utterly useless, since the
1863 # windows get no border, and are not even kept above
1864 # the parent.
1865 if {!$have_tk85 && [tk windowingsystem] eq {aqua}} return
1866
1867 wm transient $window $origin
1868
1869 # Windows fails to place transient windows normally, so
1870 # schedule a callback to center them on the parent.
1871 if {[tk windowingsystem] eq {win32}} {
1872 after idle [list tk::PlaceWindow $window widget $origin]
1873 }
1874}
1875
8d849957 1876proc show_error {w top msg {mc mc}} {
d93f1713 1877 global NS
3cb1f9c9 1878 if {![info exists NS]} {set NS ""}
d93f1713 1879 if {[wm state $top] eq "withdrawn"} { wm deiconify $top }
df3d83b1
PM
1880 message $w.m -text $msg -justify center -aspect 400
1881 pack $w.m -side top -fill x -padx 20 -pady 20
7a0ebbf8 1882 ${NS}::button $w.ok -default active -text [$mc OK] -command "destroy $top"
df3d83b1 1883 pack $w.ok -side bottom -fill x
e54be9e3
PM
1884 bind $top <Visibility> "grab $top; focus $top"
1885 bind $top <Key-Return> "destroy $top"
76f15947
AG
1886 bind $top <Key-space> "destroy $top"
1887 bind $top <Key-Escape> "destroy $top"
e54be9e3 1888 tkwait window $top
df3d83b1
PM
1889}
1890
84a76f18 1891proc error_popup {msg {owner .}} {
d93f1713
PT
1892 if {[tk windowingsystem] eq "win32"} {
1893 tk_messageBox -icon error -type ok -title [wm title .] \
1894 -parent $owner -message $msg
1895 } else {
1896 set w .error
1897 ttk_toplevel $w
1898 make_transient $w $owner
1899 show_error $w $w $msg
1900 }
098dd8a3
PM
1901}
1902
84a76f18 1903proc confirm_popup {msg {owner .}} {
d93f1713 1904 global confirm_ok NS
10299152
PM
1905 set confirm_ok 0
1906 set w .confirm
d93f1713 1907 ttk_toplevel $w
e7d64008 1908 make_transient $w $owner
10299152
PM
1909 message $w.m -text $msg -justify center -aspect 400
1910 pack $w.m -side top -fill x -padx 20 -pady 20
d93f1713 1911 ${NS}::button $w.ok -text [mc OK] -command "set confirm_ok 1; destroy $w"
10299152 1912 pack $w.ok -side left -fill x
d93f1713 1913 ${NS}::button $w.cancel -text [mc Cancel] -command "destroy $w"
10299152
PM
1914 pack $w.cancel -side right -fill x
1915 bind $w <Visibility> "grab $w; focus $w"
76f15947
AG
1916 bind $w <Key-Return> "set confirm_ok 1; destroy $w"
1917 bind $w <Key-space> "set confirm_ok 1; destroy $w"
1918 bind $w <Key-Escape> "destroy $w"
d93f1713 1919 tk::PlaceWindow $w widget $owner
10299152
PM
1920 tkwait window $w
1921 return $confirm_ok
1922}
1923
b039f0a6 1924proc setoptions {} {
d93f1713
PT
1925 if {[tk windowingsystem] ne "win32"} {
1926 option add *Panedwindow.showHandle 1 startupFile
1927 option add *Panedwindow.sashRelief raised startupFile
1928 if {[tk windowingsystem] ne "aqua"} {
1929 option add *Menu.font uifont startupFile
1930 }
1931 } else {
1932 option add *Menu.TearOff 0 startupFile
1933 }
b039f0a6
PM
1934 option add *Button.font uifont startupFile
1935 option add *Checkbutton.font uifont startupFile
1936 option add *Radiobutton.font uifont startupFile
b039f0a6
PM
1937 option add *Menubutton.font uifont startupFile
1938 option add *Label.font uifont startupFile
1939 option add *Message.font uifont startupFile
b9b142ff
MH
1940 option add *Entry.font textfont startupFile
1941 option add *Text.font textfont startupFile
d93f1713 1942 option add *Labelframe.font uifont startupFile
0933b04e 1943 option add *Spinbox.font textfont startupFile
207ad7b8 1944 option add *Listbox.font mainfont startupFile
b039f0a6
PM
1945}
1946
79056034
PM
1947# Make a menu and submenus.
1948# m is the window name for the menu, items is the list of menu items to add.
1949# Each item is a list {mc label type description options...}
1950# mc is ignored; it's so we can put mc there to alert xgettext
1951# label is the string that appears in the menu
1952# type is cascade, command or radiobutton (should add checkbutton)
1953# description depends on type; it's the sublist for cascade, the
1954# command to invoke for command, or {variable value} for radiobutton
f2d0bbbd
PM
1955proc makemenu {m items} {
1956 menu $m
cea07cf8
AG
1957 if {[tk windowingsystem] eq {aqua}} {
1958 set Meta1 Cmd
1959 } else {
1960 set Meta1 Ctrl
1961 }
f2d0bbbd 1962 foreach i $items {
79056034
PM
1963 set name [mc [lindex $i 1]]
1964 set type [lindex $i 2]
1965 set thing [lindex $i 3]
f2d0bbbd
PM
1966 set params [list $type]
1967 if {$name ne {}} {
1968 set u [string first "&" [string map {&& x} $name]]
1969 lappend params -label [string map {&& & & {}} $name]
1970 if {$u >= 0} {
1971 lappend params -underline $u
1972 }
1973 }
1974 switch -- $type {
1975 "cascade" {
79056034 1976 set submenu [string tolower [string map {& ""} [lindex $i 1]]]
f2d0bbbd
PM
1977 lappend params -menu $m.$submenu
1978 }
1979 "command" {
1980 lappend params -command $thing
1981 }
1982 "radiobutton" {
1983 lappend params -variable [lindex $thing 0] \
1984 -value [lindex $thing 1]
1985 }
1986 }
cea07cf8
AG
1987 set tail [lrange $i 4 end]
1988 regsub -all {\yMeta1\y} $tail $Meta1 tail
1989 eval $m add $params $tail
f2d0bbbd
PM
1990 if {$type eq "cascade"} {
1991 makemenu $m.$submenu $thing
1992 }
1993 }
1994}
1995
1996# translate string and remove ampersands
1997proc mca {str} {
1998 return [string map {&& & & {}} [mc $str]]
1999}
2000
39c12691
PM
2001proc cleardropsel {w} {
2002 $w selection clear
2003}
d93f1713
PT
2004proc makedroplist {w varname args} {
2005 global use_ttk
2006 if {$use_ttk} {
3cb1f9c9
PT
2007 set width 0
2008 foreach label $args {
2009 set cx [string length $label]
2010 if {$cx > $width} {set width $cx}
2011 }
2012 set gm [ttk::combobox $w -width $width -state readonly\
39c12691
PM
2013 -textvariable $varname -values $args \
2014 -exportselection false]
2015 bind $gm <<ComboboxSelected>> [list $gm selection clear]
d93f1713
PT
2016 } else {
2017 set gm [eval [linsert $args 0 tk_optionMenu $w $varname]]
2018 }
2019 return $gm
2020}
2021
d94f8cd6 2022proc makewindow {} {
31c0eaa8 2023 global canv canv2 canv3 linespc charspc ctext cflist cscroll
9c311b32 2024 global tabstop
b74fd579 2025 global findtype findtypemenu findloc findstring fstring geometry
887fe3c4 2026 global entries sha1entry sha1string sha1but
890fae70 2027 global diffcontextstring diffcontext
b9b86007 2028 global ignorespace
94a2eede 2029 global maincursor textcursor curtextcursor
219ea3a9 2030 global rowctxmenu fakerowmenu mergemax wrapcomment
60f7a7dc 2031 global highlight_files gdttype
3ea06f9f 2032 global searchstring sstring
60378c0c 2033 global bgcolor fgcolor bglist fglist diffcolors selectbgcolor
252c52df
2034 global uifgcolor uifgdisabledcolor
2035 global filesepbgcolor filesepfgcolor
2036 global mergecolors foundbgcolor currentsearchhitbgcolor
bb3edc8b
PM
2037 global headctxmenu progresscanv progressitem progresscoords statusw
2038 global fprogitem fprogcoord lastprogupdate progupdatepending
6df7403a 2039 global rprogitem rprogcoord rownumsel numcommits
d93f1713 2040 global have_tk85 use_ttk NS
ae4e3ff9
TR
2041 global git_version
2042 global worddiff
9a40c50c 2043
79056034
PM
2044 # The "mc" arguments here are purely so that xgettext
2045 # sees the following string as needing to be translated
5fdcbb13
DS
2046 set file {
2047 mc "File" cascade {
79056034 2048 {mc "Update" command updatecommits -accelerator F5}
a135f214 2049 {mc "Reload" command reloadcommits -accelerator Shift-F5}
79056034 2050 {mc "Reread references" command rereadrefs}
cea07cf8 2051 {mc "List references" command showrefs -accelerator F2}
7fb0abb1
AG
2052 {xx "" separator}
2053 {mc "Start git gui" command {exec git gui &}}
2054 {xx "" separator}
cea07cf8 2055 {mc "Quit" command doquit -accelerator Meta1-Q}
f2d0bbbd 2056 }}
5fdcbb13
DS
2057 set edit {
2058 mc "Edit" cascade {
79056034 2059 {mc "Preferences" command doprefs}
f2d0bbbd 2060 }}
5fdcbb13
DS
2061 set view {
2062 mc "View" cascade {
cea07cf8
AG
2063 {mc "New view..." command {newview 0} -accelerator Shift-F4}
2064 {mc "Edit view..." command editview -state disabled -accelerator F4}
79056034
PM
2065 {mc "Delete view" command delview -state disabled}
2066 {xx "" separator}
2067 {mc "All files" radiobutton {selectedview 0} -command {showview 0}}
f2d0bbbd 2068 }}
5fdcbb13
DS
2069 if {[tk windowingsystem] ne "aqua"} {
2070 set help {
2071 mc "Help" cascade {
2072 {mc "About gitk" command about}
2073 {mc "Key bindings" command keys}
2074 }}
2075 set bar [list $file $edit $view $help]
2076 } else {
2077 proc ::tk::mac::ShowPreferences {} {doprefs}
2078 proc ::tk::mac::Quit {} {doquit}
2079 lset file end [lreplace [lindex $file end] end-1 end]
2080 set apple {
2081 xx "Apple" cascade {
79056034 2082 {mc "About gitk" command about}
5fdcbb13
DS
2083 {xx "" separator}
2084 }}
2085 set help {
2086 mc "Help" cascade {
79056034 2087 {mc "Key bindings" command keys}
f2d0bbbd 2088 }}
5fdcbb13 2089 set bar [list $apple $file $view $help]
f2d0bbbd 2090 }
5fdcbb13 2091 makemenu .bar $bar
9a40c50c
PM
2092 . configure -menu .bar
2093
d93f1713
PT
2094 if {$use_ttk} {
2095 # cover the non-themed toplevel with a themed frame.
2096 place [ttk::frame ._main_background] -x 0 -y 0 -relwidth 1 -relheight 1
2097 }
2098
e9937d2a 2099 # the gui has upper and lower half, parts of a paned window.
d93f1713 2100 ${NS}::panedwindow .ctop -orient vertical
e9937d2a
JH
2101
2102 # possibly use assumed geometry
9ca72f4f 2103 if {![info exists geometry(pwsash0)]} {
e9937d2a
JH
2104 set geometry(topheight) [expr {15 * $linespc}]
2105 set geometry(topwidth) [expr {80 * $charspc}]
2106 set geometry(botheight) [expr {15 * $linespc}]
2107 set geometry(botwidth) [expr {50 * $charspc}]
d93f1713
PT
2108 set geometry(pwsash0) [list [expr {40 * $charspc}] 2]
2109 set geometry(pwsash1) [list [expr {60 * $charspc}] 2]
e9937d2a
JH
2110 }
2111
2112 # the upper half will have a paned window, a scroll bar to the right, and some stuff below
d93f1713
PT
2113 ${NS}::frame .tf -height $geometry(topheight) -width $geometry(topwidth)
2114 ${NS}::frame .tf.histframe
2115 ${NS}::panedwindow .tf.histframe.pwclist -orient horizontal
2116 if {!$use_ttk} {
2117 .tf.histframe.pwclist configure -sashpad 0 -handlesize 4
2118 }
e9937d2a
JH
2119
2120 # create three canvases
2121 set cscroll .tf.histframe.csb
2122 set canv .tf.histframe.pwclist.canv
9ca72f4f 2123 canvas $canv \
60378c0c 2124 -selectbackground $selectbgcolor \
f8a2c0d1 2125 -background $bgcolor -bd 0 \
9f1afe05 2126 -yscrollincr $linespc -yscrollcommand "scrollcanv $cscroll"
e9937d2a
JH
2127 .tf.histframe.pwclist add $canv
2128 set canv2 .tf.histframe.pwclist.canv2
9ca72f4f 2129 canvas $canv2 \
60378c0c 2130 -selectbackground $selectbgcolor \
f8a2c0d1 2131 -background $bgcolor -bd 0 -yscrollincr $linespc
e9937d2a
JH
2132 .tf.histframe.pwclist add $canv2
2133 set canv3 .tf.histframe.pwclist.canv3
9ca72f4f 2134 canvas $canv3 \
60378c0c 2135 -selectbackground $selectbgcolor \
f8a2c0d1 2136 -background $bgcolor -bd 0 -yscrollincr $linespc
e9937d2a 2137 .tf.histframe.pwclist add $canv3
d93f1713
PT
2138 if {$use_ttk} {
2139 bind .tf.histframe.pwclist <Map> {
2140 bind %W <Map> {}
2141 .tf.histframe.pwclist sashpos 1 [lindex $::geometry(pwsash1) 0]
2142 .tf.histframe.pwclist sashpos 0 [lindex $::geometry(pwsash0) 0]
2143 }
2144 } else {
2145 eval .tf.histframe.pwclist sash place 0 $geometry(pwsash0)
2146 eval .tf.histframe.pwclist sash place 1 $geometry(pwsash1)
2147 }
e9937d2a
JH
2148
2149 # a scroll bar to rule them
d93f1713
PT
2150 ${NS}::scrollbar $cscroll -command {allcanvs yview}
2151 if {!$use_ttk} {$cscroll configure -highlightthickness 0}
e9937d2a
JH
2152 pack $cscroll -side right -fill y
2153 bind .tf.histframe.pwclist <Configure> {resizeclistpanes %W %w}
f8a2c0d1 2154 lappend bglist $canv $canv2 $canv3
e9937d2a 2155 pack .tf.histframe.pwclist -fill both -expand 1 -side left
98f350e5 2156
e9937d2a 2157 # we have two button bars at bottom of top frame. Bar 1
d93f1713
PT
2158 ${NS}::frame .tf.bar
2159 ${NS}::frame .tf.lbar -height 15
e9937d2a
JH
2160
2161 set sha1entry .tf.bar.sha1
887fe3c4 2162 set entries $sha1entry
e9937d2a 2163 set sha1but .tf.bar.sha1label
0359ba72 2164 button $sha1but -text "[mc "SHA1 ID:"] " -state disabled -relief flat \
b039f0a6 2165 -command gotocommit -width 8
887fe3c4 2166 $sha1but conf -disabledforeground [$sha1but cget -foreground]
e9937d2a 2167 pack .tf.bar.sha1label -side left
d93f1713 2168 ${NS}::entry $sha1entry -width 40 -font textfont -textvariable sha1string
887fe3c4 2169 trace add variable sha1string write sha1change
98f350e5 2170 pack $sha1entry -side left -pady 2
d698206c 2171
f062e50f 2172 set bm_left_data {
d698206c
PM
2173 #define left_width 16
2174 #define left_height 16
2175 static unsigned char left_bits[] = {
2176 0x00, 0x00, 0xc0, 0x01, 0xe0, 0x00, 0x70, 0x00, 0x38, 0x00, 0x1c, 0x00,
2177 0x0e, 0x00, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0x0e, 0x00, 0x1c, 0x00,
2178 0x38, 0x00, 0x70, 0x00, 0xe0, 0x00, 0xc0, 0x01};
2179 }
f062e50f 2180 set bm_right_data {
d698206c
PM
2181 #define right_width 16
2182 #define right_height 16
2183 static unsigned char right_bits[] = {
2184 0x00, 0x00, 0xc0, 0x01, 0x80, 0x03, 0x00, 0x07, 0x00, 0x0e, 0x00, 0x1c,
2185 0x00, 0x38, 0xff, 0x7f, 0xff, 0x7f, 0xff, 0x7f, 0x00, 0x38, 0x00, 0x1c,
2186 0x00, 0x0e, 0x00, 0x07, 0x80, 0x03, 0xc0, 0x01};
2187 }
252c52df
2188 image create bitmap bm-left -data $bm_left_data -foreground $uifgcolor
2189 image create bitmap bm-left-gray -data $bm_left_data -foreground $uifgdisabledcolor
2190 image create bitmap bm-right -data $bm_right_data -foreground $uifgcolor
2191 image create bitmap bm-right-gray -data $bm_right_data -foreground $uifgdisabledcolor
f062e50f 2192
62e9ac5e
MK
2193 ${NS}::button .tf.bar.leftbut -command goback -state disabled -width 26
2194 if {$use_ttk} {
2195 .tf.bar.leftbut configure -image [list bm-left disabled bm-left-gray]
2196 } else {
2197 .tf.bar.leftbut configure -image bm-left
2198 }
e9937d2a 2199 pack .tf.bar.leftbut -side left -fill y
62e9ac5e
MK
2200 ${NS}::button .tf.bar.rightbut -command goforw -state disabled -width 26
2201 if {$use_ttk} {
2202 .tf.bar.rightbut configure -image [list bm-right disabled bm-right-gray]
2203 } else {
2204 .tf.bar.rightbut configure -image bm-right
2205 }
e9937d2a 2206 pack .tf.bar.rightbut -side left -fill y
d698206c 2207
d93f1713 2208 ${NS}::label .tf.bar.rowlabel -text [mc "Row"]
6df7403a 2209 set rownumsel {}
d93f1713 2210 ${NS}::label .tf.bar.rownum -width 7 -textvariable rownumsel \
6df7403a 2211 -relief sunken -anchor e
d93f1713
PT
2212 ${NS}::label .tf.bar.rowlabel2 -text "/"
2213 ${NS}::label .tf.bar.numcommits -width 7 -textvariable numcommits \
6df7403a
PM
2214 -relief sunken -anchor e
2215 pack .tf.bar.rowlabel .tf.bar.rownum .tf.bar.rowlabel2 .tf.bar.numcommits \
2216 -side left
d93f1713
PT
2217 if {!$use_ttk} {
2218 foreach w {rownum numcommits} {.tf.bar.$w configure -font textfont}
2219 }
6df7403a 2220 global selectedline
94b4a69f 2221 trace add variable selectedline write selectedline_change
6df7403a 2222
bb3edc8b
PM
2223 # Status label and progress bar
2224 set statusw .tf.bar.status
d93f1713 2225 ${NS}::label $statusw -width 15 -relief sunken
bb3edc8b 2226 pack $statusw -side left -padx 5
d93f1713
PT
2227 if {$use_ttk} {
2228 set progresscanv [ttk::progressbar .tf.bar.progress]
2229 } else {
2230 set h [expr {[font metrics uifont -linespace] + 2}]
2231 set progresscanv .tf.bar.progress
2232 canvas $progresscanv -relief sunken -height $h -borderwidth 2
2233 set progressitem [$progresscanv create rect -1 0 0 $h -fill green]
2234 set fprogitem [$progresscanv create rect -1 0 0 $h -fill yellow]
2235 set rprogitem [$progresscanv create rect -1 0 0 $h -fill red]
2236 }
2237 pack $progresscanv -side right -expand 1 -fill x -padx {0 2}
bb3edc8b
PM
2238 set progresscoords {0 0}
2239 set fprogcoord 0
a137a90f 2240 set rprogcoord 0
bb3edc8b
PM
2241 bind $progresscanv <Configure> adjustprogress
2242 set lastprogupdate [clock clicks -milliseconds]
2243 set progupdatepending 0
2244
687c8765 2245 # build up the bottom bar of upper window
d93f1713
PT
2246 ${NS}::label .tf.lbar.flabel -text "[mc "Find"] "
2247 ${NS}::button .tf.lbar.fnext -text [mc "next"] -command {dofind 1 1}
2248 ${NS}::button .tf.lbar.fprev -text [mc "prev"] -command {dofind -1 1}
2249 ${NS}::label .tf.lbar.flab2 -text " [mc "commit"] "
687c8765
PM
2250 pack .tf.lbar.flabel .tf.lbar.fnext .tf.lbar.fprev .tf.lbar.flab2 \
2251 -side left -fill y
b007ee20 2252 set gdttype [mc "containing:"]
3cb1f9c9 2253 set gm [makedroplist .tf.lbar.gdttype gdttype \
b007ee20
CS
2254 [mc "containing:"] \
2255 [mc "touching paths:"] \
c33cb908
ML
2256 [mc "adding/removing string:"] \
2257 [mc "changing lines matching:"]]
687c8765 2258 trace add variable gdttype write gdttype_change
687c8765
PM
2259 pack .tf.lbar.gdttype -side left -fill y
2260
98f350e5 2261 set findstring {}
687c8765 2262 set fstring .tf.lbar.findstring
887fe3c4 2263 lappend entries $fstring
b9b142ff 2264 ${NS}::entry $fstring -width 30 -textvariable findstring
60f7a7dc 2265 trace add variable findstring write find_change
b007ee20 2266 set findtype [mc "Exact"]
d93f1713
PT
2267 set findtypemenu [makedroplist .tf.lbar.findtype \
2268 findtype [mc "Exact"] [mc "IgnCase"] [mc "Regexp"]]
687c8765 2269 trace add variable findtype write findcom_change
b007ee20 2270 set findloc [mc "All fields"]
d93f1713 2271 makedroplist .tf.lbar.findloc findloc [mc "All fields"] [mc "Headline"] \
b007ee20 2272 [mc "Comments"] [mc "Author"] [mc "Committer"]
60f7a7dc 2273 trace add variable findloc write find_change
687c8765
PM
2274 pack .tf.lbar.findloc -side right
2275 pack .tf.lbar.findtype -side right
2276 pack $fstring -side left -expand 1 -fill x
e9937d2a
JH
2277
2278 # Finish putting the upper half of the viewer together
2279 pack .tf.lbar -in .tf -side bottom -fill x
2280 pack .tf.bar -in .tf -side bottom -fill x
2281 pack .tf.histframe -fill both -side top -expand 1
2282 .ctop add .tf
d93f1713
PT
2283 if {!$use_ttk} {
2284 .ctop paneconfigure .tf -height $geometry(topheight)
2285 .ctop paneconfigure .tf -width $geometry(topwidth)
2286 }
e9937d2a
JH
2287
2288 # now build up the bottom
d93f1713 2289 ${NS}::panedwindow .pwbottom -orient horizontal
e9937d2a
JH
2290
2291 # lower left, a text box over search bar, scroll bar to the right
2292 # if we know window height, then that will set the lower text height, otherwise
2293 # we set lower text height which will drive window height
2294 if {[info exists geometry(main)]} {
d93f1713 2295 ${NS}::frame .bleft -width $geometry(botwidth)
e9937d2a 2296 } else {
d93f1713 2297 ${NS}::frame .bleft -width $geometry(botwidth) -height $geometry(botheight)
e9937d2a 2298 }
d93f1713
PT
2299 ${NS}::frame .bleft.top
2300 ${NS}::frame .bleft.mid
2301 ${NS}::frame .bleft.bottom
e9937d2a 2302
d93f1713 2303 ${NS}::button .bleft.top.search -text [mc "Search"] -command dosearch
e9937d2a
JH
2304 pack .bleft.top.search -side left -padx 5
2305 set sstring .bleft.top.sstring
d93f1713 2306 set searchstring ""
b9b142ff 2307 ${NS}::entry $sstring -width 20 -textvariable searchstring
3ea06f9f
PM
2308 lappend entries $sstring
2309 trace add variable searchstring write incrsearch
2310 pack $sstring -side left -expand 1 -fill x
d93f1713 2311 ${NS}::radiobutton .bleft.mid.diff -text [mc "Diff"] \
a8d610a2 2312 -command changediffdisp -variable diffelide -value {0 0}
d93f1713 2313 ${NS}::radiobutton .bleft.mid.old -text [mc "Old version"] \
a8d610a2 2314 -command changediffdisp -variable diffelide -value {0 1}
d93f1713 2315 ${NS}::radiobutton .bleft.mid.new -text [mc "New version"] \
a8d610a2 2316 -command changediffdisp -variable diffelide -value {1 0}
d93f1713 2317 ${NS}::label .bleft.mid.labeldiffcontext -text " [mc "Lines of context"]: "
a8d610a2 2318 pack .bleft.mid.diff .bleft.mid.old .bleft.mid.new -side left
0933b04e 2319 spinbox .bleft.mid.diffcontext -width 5 \
a41ddbb6 2320 -from 0 -increment 1 -to 10000000 \
890fae70
SP
2321 -validate all -validatecommand "diffcontextvalidate %P" \
2322 -textvariable diffcontextstring
2323 .bleft.mid.diffcontext set $diffcontext
2324 trace add variable diffcontextstring write diffcontextchange
2325 lappend entries .bleft.mid.diffcontext
2326 pack .bleft.mid.labeldiffcontext .bleft.mid.diffcontext -side left
d93f1713 2327 ${NS}::checkbutton .bleft.mid.ignspace -text [mc "Ignore space change"] \
b9b86007
SP
2328 -command changeignorespace -variable ignorespace
2329 pack .bleft.mid.ignspace -side left -padx 5
ae4e3ff9
TR
2330
2331 set worddiff [mc "Line diff"]
2332 if {[package vcompare $git_version "1.7.2"] >= 0} {
2333 makedroplist .bleft.mid.worddiff worddiff [mc "Line diff"] \
2334 [mc "Markup words"] [mc "Color words"]
2335 trace add variable worddiff write changeworddiff
2336 pack .bleft.mid.worddiff -side left -padx 5
2337 }
2338
8809d691 2339 set ctext .bleft.bottom.ctext
f8a2c0d1 2340 text $ctext -background $bgcolor -foreground $fgcolor \
9c311b32 2341 -state disabled -font textfont \
8809d691
PK
2342 -yscrollcommand scrolltext -wrap none \
2343 -xscrollcommand ".bleft.bottom.sbhorizontal set"
32f1b3e4
PM
2344 if {$have_tk85} {
2345 $ctext conf -tabstyle wordprocessor
2346 }
d93f1713
PT
2347 ${NS}::scrollbar .bleft.bottom.sb -command "$ctext yview"
2348 ${NS}::scrollbar .bleft.bottom.sbhorizontal -command "$ctext xview" -orient h
e9937d2a 2349 pack .bleft.top -side top -fill x
a8d610a2 2350 pack .bleft.mid -side top -fill x
8809d691
PK
2351 grid $ctext .bleft.bottom.sb -sticky nsew
2352 grid .bleft.bottom.sbhorizontal -sticky ew
2353 grid columnconfigure .bleft.bottom 0 -weight 1
2354 grid rowconfigure .bleft.bottom 0 -weight 1
2355 grid rowconfigure .bleft.bottom 1 -weight 0
2356 pack .bleft.bottom -side top -fill both -expand 1
f8a2c0d1
PM
2357 lappend bglist $ctext
2358 lappend fglist $ctext
d2610d11 2359
f1b86294 2360 $ctext tag conf comment -wrap $wrapcomment
252c52df 2361 $ctext tag conf filesep -font textfontbold -fore $filesepfgcolor -back $filesepbgcolor
f8a2c0d1
PM
2362 $ctext tag conf hunksep -fore [lindex $diffcolors 2]
2363 $ctext tag conf d0 -fore [lindex $diffcolors 0]
8b07dca1 2364 $ctext tag conf dresult -fore [lindex $diffcolors 1]
252c52df
2365 $ctext tag conf m0 -fore [lindex $mergecolors 0]
2366 $ctext tag conf m1 -fore [lindex $mergecolors 1]
2367 $ctext tag conf m2 -fore [lindex $mergecolors 2]
2368 $ctext tag conf m3 -fore [lindex $mergecolors 3]
2369 $ctext tag conf m4 -fore [lindex $mergecolors 4]
2370 $ctext tag conf m5 -fore [lindex $mergecolors 5]
2371 $ctext tag conf m6 -fore [lindex $mergecolors 6]
2372 $ctext tag conf m7 -fore [lindex $mergecolors 7]
2373 $ctext tag conf m8 -fore [lindex $mergecolors 8]
2374 $ctext tag conf m9 -fore [lindex $mergecolors 9]
2375 $ctext tag conf m10 -fore [lindex $mergecolors 10]
2376 $ctext tag conf m11 -fore [lindex $mergecolors 11]
2377 $ctext tag conf m12 -fore [lindex $mergecolors 12]
2378 $ctext tag conf m13 -fore [lindex $mergecolors 13]
2379 $ctext tag conf m14 -fore [lindex $mergecolors 14]
2380 $ctext tag conf m15 -fore [lindex $mergecolors 15]
712fcc08 2381 $ctext tag conf mmax -fore darkgrey
b77b0278 2382 set mergemax 16
9c311b32
PM
2383 $ctext tag conf mresult -font textfontbold
2384 $ctext tag conf msep -font textfontbold
252c52df
2385 $ctext tag conf found -back $foundbgcolor
2386 $ctext tag conf currentsearchhit -back $currentsearchhitbgcolor
d34835c9 2387 $ctext tag conf wwrap -wrap word
e5c2d856 2388
e9937d2a 2389 .pwbottom add .bleft
d93f1713
PT
2390 if {!$use_ttk} {
2391 .pwbottom paneconfigure .bleft -width $geometry(botwidth)
2392 }
e9937d2a
JH
2393
2394 # lower right
d93f1713
PT
2395 ${NS}::frame .bright
2396 ${NS}::frame .bright.mode
2397 ${NS}::radiobutton .bright.mode.patch -text [mc "Patch"] \
f8b28a40 2398 -command reselectline -variable cmitmode -value "patch"
d93f1713 2399 ${NS}::radiobutton .bright.mode.tree -text [mc "Tree"] \
f8b28a40 2400 -command reselectline -variable cmitmode -value "tree"
e9937d2a
JH
2401 grid .bright.mode.patch .bright.mode.tree -sticky ew
2402 pack .bright.mode -side top -fill x
2403 set cflist .bright.cfiles
9c311b32 2404 set indent [font measure mainfont "nn"]
e9937d2a 2405 text $cflist \
60378c0c 2406 -selectbackground $selectbgcolor \
f8a2c0d1 2407 -background $bgcolor -foreground $fgcolor \
9c311b32 2408 -font mainfont \
7fcceed7 2409 -tabs [list $indent [expr {2 * $indent}]] \
e9937d2a 2410 -yscrollcommand ".bright.sb set" \
7fcceed7
PM
2411 -cursor [. cget -cursor] \
2412 -spacing1 1 -spacing3 1
f8a2c0d1
PM
2413 lappend bglist $cflist
2414 lappend fglist $cflist
d93f1713 2415 ${NS}::scrollbar .bright.sb -command "$cflist yview"
e9937d2a 2416 pack .bright.sb -side right -fill y
d2610d11 2417 pack $cflist -side left -fill both -expand 1
89b11d3b
PM
2418 $cflist tag configure highlight \
2419 -background [$cflist cget -selectbackground]
9c311b32 2420 $cflist tag configure bold -font mainfontbold
d2610d11 2421
e9937d2a
JH
2422 .pwbottom add .bright
2423 .ctop add .pwbottom
1db95b00 2424
b9bee115 2425 # restore window width & height if known
e9937d2a 2426 if {[info exists geometry(main)]} {
b9bee115
PM
2427 if {[scan $geometry(main) "%dx%d" w h] >= 2} {
2428 if {$w > [winfo screenwidth .]} {
2429 set w [winfo screenwidth .]
2430 }
2431 if {$h > [winfo screenheight .]} {
2432 set h [winfo screenheight .]
2433 }
2434 wm geometry . "${w}x$h"
2435 }
e9937d2a
JH
2436 }
2437
c876dbad
PT
2438 if {[info exists geometry(state)] && $geometry(state) eq "zoomed"} {
2439 wm state . $geometry(state)
2440 }
2441
d23d98d3
SP
2442 if {[tk windowingsystem] eq {aqua}} {
2443 set M1B M1
5fdcbb13 2444 set ::BM "3"
d23d98d3
SP
2445 } else {
2446 set M1B Control
5fdcbb13 2447 set ::BM "2"
d23d98d3
SP
2448 }
2449
d93f1713
PT
2450 if {$use_ttk} {
2451 bind .ctop <Map> {
2452 bind %W <Map> {}
2453 %W sashpos 0 $::geometry(topheight)
2454 }
2455 bind .pwbottom <Map> {
2456 bind %W <Map> {}
2457 %W sashpos 0 $::geometry(botwidth)
2458 }
2459 }
2460
e9937d2a
JH
2461 bind .pwbottom <Configure> {resizecdetpanes %W %w}
2462 pack .ctop -fill both -expand 1
c8dfbcf9
PM
2463 bindall <1> {selcanvline %W %x %y}
2464 #bindall <B1-Motion> {selcanvline %W %x %y}
314c3093
ML
2465 if {[tk windowingsystem] == "win32"} {
2466 bind . <MouseWheel> { windows_mousewheel_redirector %W %X %Y %D }
2467 bind $ctext <MouseWheel> { windows_mousewheel_redirector %W %X %Y %D ; break }
2468 } else {
2469 bindall <ButtonRelease-4> "allcanvs yview scroll -5 units"
2470 bindall <ButtonRelease-5> "allcanvs yview scroll 5 units"
5dd57d51
JS
2471 if {[tk windowingsystem] eq "aqua"} {
2472 bindall <MouseWheel> {
2473 set delta [expr {- (%D)}]
2474 allcanvs yview scroll $delta units
2475 }
5fdcbb13
DS
2476 bindall <Shift-MouseWheel> {
2477 set delta [expr {- (%D)}]
2478 $canv xview scroll $delta units
2479 }
5dd57d51 2480 }
314c3093 2481 }
5fdcbb13
DS
2482 bindall <$::BM> "canvscan mark %W %x %y"
2483 bindall <B$::BM-Motion> "canvscan dragto %W %x %y"
decd0a1e
JL
2484 bind all <$M1B-Key-w> {destroy [winfo toplevel %W]}
2485 bind . <$M1B-Key-w> doquit
6e5f7203
RN
2486 bindkey <Home> selfirstline
2487 bindkey <End> sellastline
17386066
PM
2488 bind . <Key-Up> "selnextline -1"
2489 bind . <Key-Down> "selnextline 1"
cca5d946
PM
2490 bind . <Shift-Key-Up> "dofind -1 0"
2491 bind . <Shift-Key-Down> "dofind 1 0"
6e5f7203
RN
2492 bindkey <Key-Right> "goforw"
2493 bindkey <Key-Left> "goback"
2494 bind . <Key-Prior> "selnextpage -1"
2495 bind . <Key-Next> "selnextpage 1"
d23d98d3
SP
2496 bind . <$M1B-Home> "allcanvs yview moveto 0.0"
2497 bind . <$M1B-End> "allcanvs yview moveto 1.0"
2498 bind . <$M1B-Key-Up> "allcanvs yview scroll -1 units"
2499 bind . <$M1B-Key-Down> "allcanvs yview scroll 1 units"
2500 bind . <$M1B-Key-Prior> "allcanvs yview scroll -1 pages"
2501 bind . <$M1B-Key-Next> "allcanvs yview scroll 1 pages"
cfb4563c
PM
2502 bindkey <Key-Delete> "$ctext yview scroll -1 pages"
2503 bindkey <Key-BackSpace> "$ctext yview scroll -1 pages"
2504 bindkey <Key-space> "$ctext yview scroll 1 pages"
df3d83b1
PM
2505 bindkey p "selnextline -1"
2506 bindkey n "selnextline 1"
6e2dda35
RS
2507 bindkey z "goback"
2508 bindkey x "goforw"
811c70fc
JN
2509 bindkey k "selnextline -1"
2510 bindkey j "selnextline 1"
2511 bindkey h "goback"
6e2dda35 2512 bindkey l "goforw"
f4c54b3c 2513 bindkey b prevfile
cfb4563c
PM
2514 bindkey d "$ctext yview scroll 18 units"
2515 bindkey u "$ctext yview scroll -18 units"
97bed034 2516 bindkey / {focus $fstring}
b6e192db 2517 bindkey <Key-KP_Divide> {focus $fstring}
cca5d946
PM
2518 bindkey <Key-Return> {dofind 1 1}
2519 bindkey ? {dofind -1 1}
39ad8570 2520 bindkey f nextfile
cea07cf8 2521 bind . <F5> updatecommits
ebb91db8 2522 bindmodfunctionkey Shift 5 reloadcommits
cea07cf8 2523 bind . <F2> showrefs
69ecfcd6 2524 bindmodfunctionkey Shift 4 {newview 0}
cea07cf8 2525 bind . <F4> edit_or_newview
d23d98d3 2526 bind . <$M1B-q> doquit
cca5d946
PM
2527 bind . <$M1B-f> {dofind 1 1}
2528 bind . <$M1B-g> {dofind 1 0}
d23d98d3
SP
2529 bind . <$M1B-r> dosearchback
2530 bind . <$M1B-s> dosearch
2531 bind . <$M1B-equal> {incrfont 1}
646f3a14 2532 bind . <$M1B-plus> {incrfont 1}
d23d98d3
SP
2533 bind . <$M1B-KP_Add> {incrfont 1}
2534 bind . <$M1B-minus> {incrfont -1}
2535 bind . <$M1B-KP_Subtract> {incrfont -1}
b6047c5a 2536 wm protocol . WM_DELETE_WINDOW doquit
e2f90ee4 2537 bind . <Destroy> {stop_backends}
df3d83b1 2538 bind . <Button-1> "click %W"
cca5d946 2539 bind $fstring <Key-Return> {dofind 1 1}
968ce45c 2540 bind $sha1entry <Key-Return> {gotocommit; break}
ee3dc72e 2541 bind $sha1entry <<PasteSelection>> clearsha1
7fcceed7
PM
2542 bind $cflist <1> {sel_flist %W %x %y; break}
2543 bind $cflist <B1-Motion> {sel_flist %W %x %y; break}
f8b28a40 2544 bind $cflist <ButtonRelease-1> {treeclick %W %x %y}
d277e89f
PM
2545 global ctxbut
2546 bind $cflist $ctxbut {pop_flist_menu %W %X %Y %x %y}
7cdc3556 2547 bind $ctext $ctxbut {pop_diff_menu %W %X %Y %x %y}
4adcbea0 2548 bind $ctext <Button-1> {focus %W}
c4614994 2549 bind $ctext <<Selection>> rehighlight_search_results
ea13cba1
PM
2550
2551 set maincursor [. cget -cursor]
2552 set textcursor [$ctext cget -cursor]
94a2eede 2553 set curtextcursor $textcursor
84ba7345 2554
c8dfbcf9 2555 set rowctxmenu .rowctxmenu
f2d0bbbd 2556 makemenu $rowctxmenu {
79056034
PM
2557 {mc "Diff this -> selected" command {diffvssel 0}}
2558 {mc "Diff selected -> this" command {diffvssel 1}}
2559 {mc "Make patch" command mkpatch}
2560 {mc "Create tag" command mktag}
2561 {mc "Write commit to file" command writecommit}
2562 {mc "Create new branch" command mkbranch}
2563 {mc "Cherry-pick this commit" command cherrypick}
2564 {mc "Reset HEAD branch to here" command resethead}
b9fdba7f
PM
2565 {mc "Mark this commit" command markhere}
2566 {mc "Return to mark" command gotomark}
2567 {mc "Find descendant of this and mark" command find_common_desc}
010509f2 2568 {mc "Compare with marked commit" command compare_commits}
6febdede
PM
2569 {mc "Diff this -> marked commit" command {diffvsmark 0}}
2570 {mc "Diff marked commit -> this" command {diffvsmark 1}}
8f3ff933 2571 {mc "Revert this commit" command revert}
f2d0bbbd
PM
2572 }
2573 $rowctxmenu configure -tearoff 0
10299152 2574
219ea3a9 2575 set fakerowmenu .fakerowmenu
f2d0bbbd 2576 makemenu $fakerowmenu {
79056034
PM
2577 {mc "Diff this -> selected" command {diffvssel 0}}
2578 {mc "Diff selected -> this" command {diffvssel 1}}
2579 {mc "Make patch" command mkpatch}
6febdede
PM
2580 {mc "Diff this -> marked commit" command {diffvsmark 0}}
2581 {mc "Diff marked commit -> this" command {diffvsmark 1}}
f2d0bbbd
PM
2582 }
2583 $fakerowmenu configure -tearoff 0
219ea3a9 2584
10299152 2585 set headctxmenu .headctxmenu
f2d0bbbd 2586 makemenu $headctxmenu {
79056034
PM
2587 {mc "Check out this branch" command cobranch}
2588 {mc "Remove this branch" command rmbranch}
f2d0bbbd
PM
2589 }
2590 $headctxmenu configure -tearoff 0
3244729a
PM
2591
2592 global flist_menu
2593 set flist_menu .flistctxmenu
f2d0bbbd 2594 makemenu $flist_menu {
79056034
PM
2595 {mc "Highlight this too" command {flist_hl 0}}
2596 {mc "Highlight this only" command {flist_hl 1}}
2597 {mc "External diff" command {external_diff}}
2598 {mc "Blame parent commit" command {external_blame 1}}
f2d0bbbd
PM
2599 }
2600 $flist_menu configure -tearoff 0
7cdc3556
AG
2601
2602 global diff_menu
2603 set diff_menu .diffctxmenu
2604 makemenu $diff_menu {
8a897742 2605 {mc "Show origin of this line" command show_line_source}
7cdc3556
AG
2606 {mc "Run git gui blame on this line" command {external_blame_diff}}
2607 }
2608 $diff_menu configure -tearoff 0
df3d83b1
PM
2609}
2610
314c3093
ML
2611# Windows sends all mouse wheel events to the current focused window, not
2612# the one where the mouse hovers, so bind those events here and redirect
2613# to the correct window
2614proc windows_mousewheel_redirector {W X Y D} {
2615 global canv canv2 canv3
2616 set w [winfo containing -displayof $W $X $Y]
2617 if {$w ne ""} {
2618 set u [expr {$D < 0 ? 5 : -5}]
2619 if {$w == $canv || $w == $canv2 || $w == $canv3} {
2620 allcanvs yview scroll $u units
2621 } else {
2622 catch {
2623 $w yview scroll $u units
2624 }
2625 }
2626 }
2627}
2628
6df7403a
PM
2629# Update row number label when selectedline changes
2630proc selectedline_change {n1 n2 op} {
2631 global selectedline rownumsel
2632
94b4a69f 2633 if {$selectedline eq {}} {
6df7403a
PM
2634 set rownumsel {}
2635 } else {
2636 set rownumsel [expr {$selectedline + 1}]
2637 }
2638}
2639
be0cd098
PM
2640# mouse-2 makes all windows scan vertically, but only the one
2641# the cursor is in scans horizontally
2642proc canvscan {op w x y} {
2643 global canv canv2 canv3
2644 foreach c [list $canv $canv2 $canv3] {
2645 if {$c == $w} {
2646 $c scan $op $x $y
2647 } else {
2648 $c scan $op 0 $y
2649 }
2650 }
2651}
2652
9f1afe05
PM
2653proc scrollcanv {cscroll f0 f1} {
2654 $cscroll set $f0 $f1
31c0eaa8 2655 drawvisible
908c3585 2656 flushhighlights
9f1afe05
PM
2657}
2658
df3d83b1
PM
2659# when we make a key binding for the toplevel, make sure
2660# it doesn't get triggered when that key is pressed in the
2661# find string entry widget.
2662proc bindkey {ev script} {
887fe3c4 2663 global entries
df3d83b1
PM
2664 bind . $ev $script
2665 set escript [bind Entry $ev]
2666 if {$escript == {}} {
2667 set escript [bind Entry <Key>]
2668 }
887fe3c4
PM
2669 foreach e $entries {
2670 bind $e $ev "$escript; break"
2671 }
df3d83b1
PM
2672}
2673
69ecfcd6
AW
2674proc bindmodfunctionkey {mod n script} {
2675 bind . <$mod-F$n> $script
2676 catch { bind . <$mod-XF86_Switch_VT_$n> $script }
2677}
2678
df3d83b1 2679# set the focus back to the toplevel for any click outside
887fe3c4 2680# the entry widgets
df3d83b1 2681proc click {w} {
bd441de4
ML
2682 global ctext entries
2683 foreach e [concat $entries $ctext] {
887fe3c4 2684 if {$w == $e} return
df3d83b1 2685 }
887fe3c4 2686 focus .
0fba86b3
PM
2687}
2688
bb3edc8b
PM
2689# Adjust the progress bar for a change in requested extent or canvas size
2690proc adjustprogress {} {
2691 global progresscanv progressitem progresscoords
2692 global fprogitem fprogcoord lastprogupdate progupdatepending
d93f1713
PT
2693 global rprogitem rprogcoord use_ttk
2694
2695 if {$use_ttk} {
2696 $progresscanv configure -value [expr {int($fprogcoord * 100)}]
2697 return
2698 }
bb3edc8b
PM
2699
2700 set w [expr {[winfo width $progresscanv] - 4}]
2701 set x0 [expr {$w * [lindex $progresscoords 0]}]
2702 set x1 [expr {$w * [lindex $progresscoords 1]}]
2703 set h [winfo height $progresscanv]
2704 $progresscanv coords $progressitem $x0 0 $x1 $h
2705 $progresscanv coords $fprogitem 0 0 [expr {$w * $fprogcoord}] $h
a137a90f 2706 $progresscanv coords $rprogitem 0 0 [expr {$w * $rprogcoord}] $h
bb3edc8b
PM
2707 set now [clock clicks -milliseconds]
2708 if {$now >= $lastprogupdate + 100} {
2709 set progupdatepending 0
2710 update
2711 } elseif {!$progupdatepending} {
2712 set progupdatepending 1
2713 after [expr {$lastprogupdate + 100 - $now}] doprogupdate
2714 }
2715}
2716
2717proc doprogupdate {} {
2718 global lastprogupdate progupdatepending
2719
2720 if {$progupdatepending} {
2721 set progupdatepending 0
2722 set lastprogupdate [clock clicks -milliseconds]
2723 update
2724 }
2725}
2726
0fba86b3 2727proc savestuff {w} {
32f1b3e4 2728 global canv canv2 canv3 mainfont textfont uifont tabstop
712fcc08 2729 global stuffsaved findmergefiles maxgraphpct
219ea3a9 2730 global maxwidth showneartags showlocalchanges
2d480856 2731 global viewname viewfiles viewargs viewargscmd viewperm nextviewnum
7a39a17a 2732 global cmitmode wrapcomment datetimeformat limitdiffs
5497f7a2 2733 global colors uicolor bgcolor fgcolor diffcolors diffcontext selectbgcolor
252c52df
2734 global uifgcolor uifgdisabledcolor
2735 global headbgcolor headfgcolor headoutlinecolor remotebgcolor
2736 global tagbgcolor tagfgcolor tagoutlinecolor
2737 global reflinecolor filesepbgcolor filesepfgcolor
2738 global mergecolors foundbgcolor currentsearchhitbgcolor
2739 global linehoverbgcolor linehoverfgcolor linehoveroutlinecolor circlecolors
2740 global mainheadcirclecolor workingfilescirclecolor indexcirclecolor
2741 global linkfgcolor circleoutlinecolor
21ac8a8d 2742 global autoselect autosellen extdifftool perfile_attrs markbgcolor use_ttk
d34835c9 2743 global hideremotes want_ttk maxrefs
4ef17537 2744
0fba86b3 2745 if {$stuffsaved} return
df3d83b1 2746 if {![winfo viewable .]} return
0fba86b3 2747 catch {
9bedb0e1 2748 if {[file exists ~/.gitk-new]} {file delete -force ~/.gitk-new}
0fba86b3 2749 set f [open "~/.gitk-new" w]
9832e4f2
PM
2750 if {$::tcl_platform(platform) eq {windows}} {
2751 file attributes "~/.gitk-new" -hidden true
2752 }
f0654861
PM
2753 puts $f [list set mainfont $mainfont]
2754 puts $f [list set textfont $textfont]
4840be66 2755 puts $f [list set uifont $uifont]
7e12f1a6 2756 puts $f [list set tabstop $tabstop]
f0654861 2757 puts $f [list set findmergefiles $findmergefiles]
8d858d1a 2758 puts $f [list set maxgraphpct $maxgraphpct]
04c13d38 2759 puts $f [list set maxwidth $maxwidth]
f8b28a40 2760 puts $f [list set cmitmode $cmitmode]
f1b86294 2761 puts $f [list set wrapcomment $wrapcomment]
95293b58 2762 puts $f [list set autoselect $autoselect]
21ac8a8d 2763 puts $f [list set autosellen $autosellen]
b8ab2e17 2764 puts $f [list set showneartags $showneartags]
d34835c9 2765 puts $f [list set maxrefs $maxrefs]
ffe15297 2766 puts $f [list set hideremotes $hideremotes]
219ea3a9 2767 puts $f [list set showlocalchanges $showlocalchanges]
e8b5f4be 2768 puts $f [list set datetimeformat $datetimeformat]
7a39a17a 2769 puts $f [list set limitdiffs $limitdiffs]
5497f7a2 2770 puts $f [list set uicolor $uicolor]
0cc08ff7 2771 puts $f [list set want_ttk $want_ttk]
f8a2c0d1
PM
2772 puts $f [list set bgcolor $bgcolor]
2773 puts $f [list set fgcolor $fgcolor]
252c52df
2774 puts $f [list set uifgcolor $uifgcolor]
2775 puts $f [list set uifgdisabledcolor $uifgdisabledcolor]
f8a2c0d1
PM
2776 puts $f [list set colors $colors]
2777 puts $f [list set diffcolors $diffcolors]
252c52df 2778 puts $f [list set mergecolors $mergecolors]
e3e901be 2779 puts $f [list set markbgcolor $markbgcolor]
890fae70 2780 puts $f [list set diffcontext $diffcontext]
60378c0c 2781 puts $f [list set selectbgcolor $selectbgcolor]
252c52df
2782 puts $f [list set foundbgcolor $foundbgcolor]
2783 puts $f [list set currentsearchhitbgcolor $currentsearchhitbgcolor]
314f5de1 2784 puts $f [list set extdifftool $extdifftool]
39ee47ef 2785 puts $f [list set perfile_attrs $perfile_attrs]
252c52df
2786 puts $f [list set headbgcolor $headbgcolor]
2787 puts $f [list set headfgcolor $headfgcolor]
2788 puts $f [list set headoutlinecolor $headoutlinecolor]
2789 puts $f [list set remotebgcolor $remotebgcolor]
2790 puts $f [list set tagbgcolor $tagbgcolor]
2791 puts $f [list set tagfgcolor $tagfgcolor]
2792 puts $f [list set tagoutlinecolor $tagoutlinecolor]
2793 puts $f [list set reflinecolor $reflinecolor]
2794 puts $f [list set filesepbgcolor $filesepbgcolor]
2795 puts $f [list set filesepfgcolor $filesepfgcolor]
2796 puts $f [list set linehoverbgcolor $linehoverbgcolor]
2797 puts $f [list set linehoverfgcolor $linehoverfgcolor]
2798 puts $f [list set linehoveroutlinecolor $linehoveroutlinecolor]
2799 puts $f [list set mainheadcirclecolor $mainheadcirclecolor]
2800 puts $f [list set workingfilescirclecolor $workingfilescirclecolor]
2801 puts $f [list set indexcirclecolor $indexcirclecolor]
2802 puts $f [list set circlecolors $circlecolors]
2803 puts $f [list set linkfgcolor $linkfgcolor]
2804 puts $f [list set circleoutlinecolor $circleoutlinecolor]
e9937d2a 2805
b6047c5a 2806 puts $f "set geometry(main) [wm geometry .]"
c876dbad 2807 puts $f "set geometry(state) [wm state .]"
e9937d2a
JH
2808 puts $f "set geometry(topwidth) [winfo width .tf]"
2809 puts $f "set geometry(topheight) [winfo height .tf]"
d93f1713
PT
2810 if {$use_ttk} {
2811 puts $f "set geometry(pwsash0) \"[.tf.histframe.pwclist sashpos 0] 1\""
2812 puts $f "set geometry(pwsash1) \"[.tf.histframe.pwclist sashpos 1] 1\""
2813 } else {
2814 puts $f "set geometry(pwsash0) \"[.tf.histframe.pwclist sash coord 0]\""
2815 puts $f "set geometry(pwsash1) \"[.tf.histframe.pwclist sash coord 1]\""
2816 }
e9937d2a
JH
2817 puts $f "set geometry(botwidth) [winfo width .bleft]"
2818 puts $f "set geometry(botheight) [winfo height .bleft]"
2819
a90a6d24
PM
2820 puts -nonewline $f "set permviews {"
2821 for {set v 0} {$v < $nextviewnum} {incr v} {
2822 if {$viewperm($v)} {
2d480856 2823 puts $f "{[list $viewname($v) $viewfiles($v) $viewargs($v) $viewargscmd($v)]}"
a90a6d24
PM
2824 }
2825 }
2826 puts $f "}"
0fba86b3
PM
2827 close $f
2828 file rename -force "~/.gitk-new" "~/.gitk"
2829 }
2830 set stuffsaved 1
1db95b00
PM
2831}
2832
43bddeb4 2833proc resizeclistpanes {win w} {
d93f1713 2834 global oldwidth use_ttk
418c4c7b 2835 if {[info exists oldwidth($win)]} {
d93f1713
PT
2836 if {$use_ttk} {
2837 set s0 [$win sashpos 0]
2838 set s1 [$win sashpos 1]
2839 } else {
2840 set s0 [$win sash coord 0]
2841 set s1 [$win sash coord 1]
2842 }
43bddeb4
PM
2843 if {$w < 60} {
2844 set sash0 [expr {int($w/2 - 2)}]
2845 set sash1 [expr {int($w*5/6 - 2)}]
2846 } else {
2847 set factor [expr {1.0 * $w / $oldwidth($win)}]
2848 set sash0 [expr {int($factor * [lindex $s0 0])}]
2849 set sash1 [expr {int($factor * [lindex $s1 0])}]
2850 if {$sash0 < 30} {
2851 set sash0 30
2852 }
2853 if {$sash1 < $sash0 + 20} {
2ed49d54 2854 set sash1 [expr {$sash0 + 20}]
43bddeb4
PM
2855 }
2856 if {$sash1 > $w - 10} {
2ed49d54 2857 set sash1 [expr {$w - 10}]
43bddeb4 2858 if {$sash0 > $sash1 - 20} {
2ed49d54 2859 set sash0 [expr {$sash1 - 20}]
43bddeb4
PM
2860 }
2861 }
2862 }
d93f1713
PT
2863 if {$use_ttk} {
2864 $win sashpos 0 $sash0
2865 $win sashpos 1 $sash1
2866 } else {
2867 $win sash place 0 $sash0 [lindex $s0 1]
2868 $win sash place 1 $sash1 [lindex $s1 1]
2869 }
43bddeb4
PM
2870 }
2871 set oldwidth($win) $w
2872}
2873
2874proc resizecdetpanes {win w} {
d93f1713 2875 global oldwidth use_ttk
418c4c7b 2876 if {[info exists oldwidth($win)]} {
d93f1713
PT
2877 if {$use_ttk} {
2878 set s0 [$win sashpos 0]
2879 } else {
2880 set s0 [$win sash coord 0]
2881 }
43bddeb4
PM
2882 if {$w < 60} {
2883 set sash0 [expr {int($w*3/4 - 2)}]
2884 } else {
2885 set factor [expr {1.0 * $w / $oldwidth($win)}]
2886 set sash0 [expr {int($factor * [lindex $s0 0])}]
2887 if {$sash0 < 45} {
2888 set sash0 45
2889 }
2890 if {$sash0 > $w - 15} {
2ed49d54 2891 set sash0 [expr {$w - 15}]
43bddeb4
PM
2892 }
2893 }
d93f1713
PT
2894 if {$use_ttk} {
2895 $win sashpos 0 $sash0
2896 } else {
2897 $win sash place 0 $sash0 [lindex $s0 1]
2898 }
43bddeb4
PM
2899 }
2900 set oldwidth($win) $w
2901}
2902
b5721c72
PM
2903proc allcanvs args {
2904 global canv canv2 canv3
2905 eval $canv $args
2906 eval $canv2 $args
2907 eval $canv3 $args
2908}
2909
2910proc bindall {event action} {
2911 global canv canv2 canv3
2912 bind $canv $event $action
2913 bind $canv2 $event $action
2914 bind $canv3 $event $action
2915}
2916
9a40c50c 2917proc about {} {
d93f1713 2918 global uifont NS
9a40c50c
PM
2919 set w .about
2920 if {[winfo exists $w]} {
2921 raise $w
2922 return
2923 }
d93f1713 2924 ttk_toplevel $w
d990cedf 2925 wm title $w [mc "About gitk"]
e7d64008 2926 make_transient $w .
d990cedf 2927 message $w.m -text [mc "
9f1afe05 2928Gitk - a commit viewer for git
9a40c50c 2929
bb3e86a1 2930Copyright \u00a9 2005-2011 Paul Mackerras
9a40c50c 2931
d990cedf 2932Use and redistribute under the terms of the GNU General Public License"] \
3a950e9a
ER
2933 -justify center -aspect 400 -border 2 -bg white -relief groove
2934 pack $w.m -side top -fill x -padx 2 -pady 2
d93f1713 2935 ${NS}::button $w.ok -text [mc "Close"] -command "destroy $w" -default active
9a40c50c 2936 pack $w.ok -side bottom
3a950e9a
ER
2937 bind $w <Visibility> "focus $w.ok"
2938 bind $w <Key-Escape> "destroy $w"
2939 bind $w <Key-Return> "destroy $w"
d93f1713 2940 tk::PlaceWindow $w widget .
9a40c50c
PM
2941}
2942
4e95e1f7 2943proc keys {} {
d93f1713 2944 global NS
4e95e1f7
PM
2945 set w .keys
2946 if {[winfo exists $w]} {
2947 raise $w
2948 return
2949 }
d23d98d3
SP
2950 if {[tk windowingsystem] eq {aqua}} {
2951 set M1T Cmd
2952 } else {
2953 set M1T Ctrl
2954 }
d93f1713 2955 ttk_toplevel $w
d990cedf 2956 wm title $w [mc "Gitk key bindings"]
e7d64008 2957 make_transient $w .
3d2c998e
MB
2958 message $w.m -text "
2959[mc "Gitk key bindings:"]
2960
2961[mc "<%s-Q> Quit" $M1T]
decd0a1e 2962[mc "<%s-W> Close window" $M1T]
3d2c998e
MB
2963[mc "<Home> Move to first commit"]
2964[mc "<End> Move to last commit"]
811c70fc
JN
2965[mc "<Up>, p, k Move up one commit"]
2966[mc "<Down>, n, j Move down one commit"]
2967[mc "<Left>, z, h Go back in history list"]
3d2c998e
MB
2968[mc "<Right>, x, l Go forward in history list"]
2969[mc "<PageUp> Move up one page in commit list"]
2970[mc "<PageDown> Move down one page in commit list"]
2971[mc "<%s-Home> Scroll to top of commit list" $M1T]
2972[mc "<%s-End> Scroll to bottom of commit list" $M1T]
2973[mc "<%s-Up> Scroll commit list up one line" $M1T]
2974[mc "<%s-Down> Scroll commit list down one line" $M1T]
2975[mc "<%s-PageUp> Scroll commit list up one page" $M1T]
2976[mc "<%s-PageDown> Scroll commit list down one page" $M1T]
2977[mc "<Shift-Up> Find backwards (upwards, later commits)"]
2978[mc "<Shift-Down> Find forwards (downwards, earlier commits)"]
2979[mc "<Delete>, b Scroll diff view up one page"]
2980[mc "<Backspace> Scroll diff view up one page"]
2981[mc "<Space> Scroll diff view down one page"]
2982[mc "u Scroll diff view up 18 lines"]
2983[mc "d Scroll diff view down 18 lines"]
2984[mc "<%s-F> Find" $M1T]
2985[mc "<%s-G> Move to next find hit" $M1T]
2986[mc "<Return> Move to next find hit"]
97bed034 2987[mc "/ Focus the search box"]
3d2c998e
MB
2988[mc "? Move to previous find hit"]
2989[mc "f Scroll diff view to next file"]
2990[mc "<%s-S> Search for next hit in diff view" $M1T]
2991[mc "<%s-R> Search for previous hit in diff view" $M1T]
2992[mc "<%s-KP+> Increase font size" $M1T]
2993[mc "<%s-plus> Increase font size" $M1T]
2994[mc "<%s-KP-> Decrease font size" $M1T]
2995[mc "<%s-minus> Decrease font size" $M1T]
2996[mc "<F5> Update"]
2997" \
3a950e9a
ER
2998 -justify left -bg white -border 2 -relief groove
2999 pack $w.m -side top -fill both -padx 2 -pady 2
d93f1713 3000 ${NS}::button $w.ok -text [mc "Close"] -command "destroy $w" -default active
76f15947 3001 bind $w <Key-Escape> [list destroy $w]
4e95e1f7 3002 pack $w.ok -side bottom
3a950e9a
ER
3003 bind $w <Visibility> "focus $w.ok"
3004 bind $w <Key-Escape> "destroy $w"
3005 bind $w <Key-Return> "destroy $w"
4e95e1f7
PM
3006}
3007
7fcceed7
PM
3008# Procedures for manipulating the file list window at the
3009# bottom right of the overall window.
f8b28a40
PM
3010
3011proc treeview {w l openlevs} {
3012 global treecontents treediropen treeheight treeparent treeindex
3013
3014 set ix 0
3015 set treeindex() 0
3016 set lev 0
3017 set prefix {}
3018 set prefixend -1
3019 set prefendstack {}
3020 set htstack {}
3021 set ht 0
3022 set treecontents() {}
3023 $w conf -state normal
3024 foreach f $l {
3025 while {[string range $f 0 $prefixend] ne $prefix} {
3026 if {$lev <= $openlevs} {
3027 $w mark set e:$treeindex($prefix) "end -1c"
3028 $w mark gravity e:$treeindex($prefix) left
3029 }
3030 set treeheight($prefix) $ht
3031 incr ht [lindex $htstack end]
3032 set htstack [lreplace $htstack end end]
3033 set prefixend [lindex $prefendstack end]
3034 set prefendstack [lreplace $prefendstack end end]
3035 set prefix [string range $prefix 0 $prefixend]
3036 incr lev -1
3037 }
3038 set tail [string range $f [expr {$prefixend+1}] end]
3039 while {[set slash [string first "/" $tail]] >= 0} {
3040 lappend htstack $ht
3041 set ht 0
3042 lappend prefendstack $prefixend
3043 incr prefixend [expr {$slash + 1}]
3044 set d [string range $tail 0 $slash]
3045 lappend treecontents($prefix) $d
3046 set oldprefix $prefix
3047 append prefix $d
3048 set treecontents($prefix) {}
3049 set treeindex($prefix) [incr ix]
3050 set treeparent($prefix) $oldprefix
3051 set tail [string range $tail [expr {$slash+1}] end]
3052 if {$lev <= $openlevs} {
3053 set ht 1
3054 set treediropen($prefix) [expr {$lev < $openlevs}]
3055 set bm [expr {$lev == $openlevs? "tri-rt": "tri-dn"}]
3056 $w mark set d:$ix "end -1c"
3057 $w mark gravity d:$ix left
3058 set str "\n"
3059 for {set i 0} {$i < $lev} {incr i} {append str "\t"}
3060 $w insert end $str
3061 $w image create end -align center -image $bm -padx 1 \
3062 -name a:$ix
45a9d505 3063 $w insert end $d [highlight_tag $prefix]
f8b28a40
PM
3064 $w mark set s:$ix "end -1c"
3065 $w mark gravity s:$ix left
3066 }
3067 incr lev
3068 }
3069 if {$tail ne {}} {
3070 if {$lev <= $openlevs} {
3071 incr ht
3072 set str "\n"
3073 for {set i 0} {$i < $lev} {incr i} {append str "\t"}
3074 $w insert end $str
45a9d505 3075 $w insert end $tail [highlight_tag $f]
f8b28a40
PM
3076 }
3077 lappend treecontents($prefix) $tail
3078 }
3079 }
3080 while {$htstack ne {}} {
3081 set treeheight($prefix) $ht
3082 incr ht [lindex $htstack end]
3083 set htstack [lreplace $htstack end end]
096e96b4
BD
3084 set prefixend [lindex $prefendstack end]
3085 set prefendstack [lreplace $prefendstack end end]
3086 set prefix [string range $prefix 0 $prefixend]
f8b28a40
PM
3087 }
3088 $w conf -state disabled
3089}
3090
3091proc linetoelt {l} {
3092 global treeheight treecontents
3093
3094 set y 2
3095 set prefix {}
3096 while {1} {
3097 foreach e $treecontents($prefix) {
3098 if {$y == $l} {
3099 return "$prefix$e"
3100 }
3101 set n 1
3102 if {[string index $e end] eq "/"} {
3103 set n $treeheight($prefix$e)
3104 if {$y + $n > $l} {
3105 append prefix $e
3106 incr y
3107 break
3108 }
3109 }
3110 incr y $n
3111 }
3112 }
3113}
3114
45a9d505
PM
3115proc highlight_tree {y prefix} {
3116 global treeheight treecontents cflist
3117
3118 foreach e $treecontents($prefix) {
3119 set path $prefix$e
3120 if {[highlight_tag $path] ne {}} {
3121 $cflist tag add bold $y.0 "$y.0 lineend"
3122 }
3123 incr y
3124 if {[string index $e end] eq "/" && $treeheight($path) > 1} {
3125 set y [highlight_tree $y $path]
3126 }
3127 }
3128 return $y
3129}
3130
f8b28a40
PM
3131proc treeclosedir {w dir} {
3132 global treediropen treeheight treeparent treeindex
3133
3134 set ix $treeindex($dir)
3135 $w conf -state normal
3136 $w delete s:$ix e:$ix
3137 set treediropen($dir) 0
3138 $w image configure a:$ix -image tri-rt
3139 $w conf -state disabled
3140 set n [expr {1 - $treeheight($dir)}]
3141 while {$dir ne {}} {
3142 incr treeheight($dir) $n
3143 set dir $treeparent($dir)
3144 }
3145}
3146
3147proc treeopendir {w dir} {
3148 global treediropen treeheight treeparent treecontents treeindex
3149
3150 set ix $treeindex($dir)
3151 $w conf -state normal
3152 $w image configure a:$ix -image tri-dn
3153 $w mark set e:$ix s:$ix
3154 $w mark gravity e:$ix right
3155 set lev 0
3156 set str "\n"
3157 set n [llength $treecontents($dir)]
3158 for {set x $dir} {$x ne {}} {set x $treeparent($x)} {
3159 incr lev
3160 append str "\t"
3161 incr treeheight($x) $n
3162 }
3163 foreach e $treecontents($dir) {
45a9d505 3164 set de $dir$e
f8b28a40 3165 if {[string index $e end] eq "/"} {
f8b28a40
PM
3166 set iy $treeindex($de)
3167 $w mark set d:$iy e:$ix
3168 $w mark gravity d:$iy left
3169 $w insert e:$ix $str
3170 set treediropen($de) 0
3171 $w image create e:$ix -align center -image tri-rt -padx 1 \
3172 -name a:$iy
45a9d505 3173 $w insert e:$ix $e [highlight_tag $de]
f8b28a40
PM
3174 $w mark set s:$iy e:$ix
3175 $w mark gravity s:$iy left
3176 set treeheight($de) 1
3177 } else {
3178 $w insert e:$ix $str
45a9d505 3179 $w insert e:$ix $e [highlight_tag $de]
f8b28a40
PM
3180 }
3181 }
b8a640ee 3182 $w mark gravity e:$ix right
f8b28a40
PM
3183 $w conf -state disabled
3184 set treediropen($dir) 1
3185 set top [lindex [split [$w index @0,0] .] 0]
3186 set ht [$w cget -height]
3187 set l [lindex [split [$w index s:$ix] .] 0]
3188 if {$l < $top} {
3189 $w yview $l.0
3190 } elseif {$l + $n + 1 > $top + $ht} {
3191 set top [expr {$l + $n + 2 - $ht}]
3192 if {$l < $top} {
3193 set top $l
3194 }
3195 $w yview $top.0
3196 }
3197}
3198
3199proc treeclick {w x y} {
3200 global treediropen cmitmode ctext cflist cflist_top
3201
3202 if {$cmitmode ne "tree"} return
3203 if {![info exists cflist_top]} return
3204 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3205 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
3206 $cflist tag add highlight $l.0 "$l.0 lineend"
3207 set cflist_top $l
3208 if {$l == 1} {
3209 $ctext yview 1.0
3210 return
3211 }
3212 set e [linetoelt $l]
3213 if {[string index $e end] ne "/"} {
3214 showfile $e
3215 } elseif {$treediropen($e)} {
3216 treeclosedir $w $e
3217 } else {
3218 treeopendir $w $e
3219 }
3220}
3221
3222proc setfilelist {id} {
8a897742 3223 global treefilelist cflist jump_to_here
f8b28a40
PM
3224
3225 treeview $cflist $treefilelist($id) 0
8a897742
PM
3226 if {$jump_to_here ne {}} {
3227 set f [lindex $jump_to_here 0]
3228 if {[lsearch -exact $treefilelist($id) $f] >= 0} {
3229 showfile $f
3230 }
3231 }
f8b28a40
PM
3232}
3233
3234image create bitmap tri-rt -background black -foreground blue -data {
3235 #define tri-rt_width 13
3236 #define tri-rt_height 13
3237 static unsigned char tri-rt_bits[] = {
3238 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x30, 0x00, 0x70, 0x00, 0xf0, 0x00,
3239 0xf0, 0x01, 0xf0, 0x00, 0x70, 0x00, 0x30, 0x00, 0x10, 0x00, 0x00, 0x00,
3240 0x00, 0x00};
3241} -maskdata {
3242 #define tri-rt-mask_width 13
3243 #define tri-rt-mask_height 13
3244 static unsigned char tri-rt-mask_bits[] = {
3245 0x08, 0x00, 0x18, 0x00, 0x38, 0x00, 0x78, 0x00, 0xf8, 0x00, 0xf8, 0x01,
3246 0xf8, 0x03, 0xf8, 0x01, 0xf8, 0x00, 0x78, 0x00, 0x38, 0x00, 0x18, 0x00,
3247 0x08, 0x00};
3248}
3249image create bitmap tri-dn -background black -foreground blue -data {
3250 #define tri-dn_width 13
3251 #define tri-dn_height 13
3252 static unsigned char tri-dn_bits[] = {
3253 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x07, 0xf8, 0x03,
3254 0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3255 0x00, 0x00};
3256} -maskdata {
3257 #define tri-dn-mask_width 13
3258 #define tri-dn-mask_height 13
3259 static unsigned char tri-dn-mask_bits[] = {
3260 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x1f, 0xfe, 0x0f, 0xfc, 0x07,
3261 0xf8, 0x03, 0xf0, 0x01, 0xe0, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
3262 0x00, 0x00};
3263}
3264
887c996e
PM
3265image create bitmap reficon-T -background black -foreground yellow -data {
3266 #define tagicon_width 13
3267 #define tagicon_height 9
3268 static unsigned char tagicon_bits[] = {
3269 0x00, 0x00, 0x00, 0x00, 0xf0, 0x07, 0xf8, 0x07,
3270 0xfc, 0x07, 0xf8, 0x07, 0xf0, 0x07, 0x00, 0x00, 0x00, 0x00};
3271} -maskdata {
3272 #define tagicon-mask_width 13
3273 #define tagicon-mask_height 9
3274 static unsigned char tagicon-mask_bits[] = {
3275 0x00, 0x00, 0xf0, 0x0f, 0xf8, 0x0f, 0xfc, 0x0f,
3276 0xfe, 0x0f, 0xfc, 0x0f, 0xf8, 0x0f, 0xf0, 0x0f, 0x00, 0x00};
3277}
3278set rectdata {
3279 #define headicon_width 13
3280 #define headicon_height 9
3281 static unsigned char headicon_bits[] = {
3282 0x00, 0x00, 0x00, 0x00, 0xf8, 0x07, 0xf8, 0x07,
3283 0xf8, 0x07, 0xf8, 0x07, 0xf8, 0x07, 0x00, 0x00, 0x00, 0x00};
3284}
3285set rectmask {
3286 #define headicon-mask_width 13
3287 #define headicon-mask_height 9
3288 static unsigned char headicon-mask_bits[] = {
3289 0x00, 0x00, 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f,
3290 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f, 0xfc, 0x0f, 0x00, 0x00};
3291}
3292image create bitmap reficon-H -background black -foreground green \
3293 -data $rectdata -maskdata $rectmask
3294image create bitmap reficon-o -background black -foreground "#ddddff" \
3295 -data $rectdata -maskdata $rectmask
3296
7fcceed7 3297proc init_flist {first} {
7fcc92bf 3298 global cflist cflist_top difffilestart
7fcceed7
PM
3299
3300 $cflist conf -state normal
3301 $cflist delete 0.0 end
3302 if {$first ne {}} {
3303 $cflist insert end $first
3304 set cflist_top 1
7fcceed7
PM
3305 $cflist tag add highlight 1.0 "1.0 lineend"
3306 } else {
3307 catch {unset cflist_top}
3308 }
3309 $cflist conf -state disabled
3310 set difffilestart {}
3311}
3312
63b79191
PM
3313proc highlight_tag {f} {
3314 global highlight_paths
3315
3316 foreach p $highlight_paths {
3317 if {[string match $p $f]} {
3318 return "bold"
3319 }
3320 }
3321 return {}
3322}
3323
3324proc highlight_filelist {} {
45a9d505 3325 global cmitmode cflist
63b79191 3326
45a9d505
PM
3327 $cflist conf -state normal
3328 if {$cmitmode ne "tree"} {
63b79191
PM
3329 set end [lindex [split [$cflist index end] .] 0]
3330 for {set l 2} {$l < $end} {incr l} {
3331 set line [$cflist get $l.0 "$l.0 lineend"]
3332 if {[highlight_tag $line] ne {}} {
3333 $cflist tag add bold $l.0 "$l.0 lineend"
3334 }
3335 }
45a9d505
PM
3336 } else {
3337 highlight_tree 2 {}
63b79191 3338 }
45a9d505 3339 $cflist conf -state disabled
63b79191
PM
3340}
3341
3342proc unhighlight_filelist {} {
45a9d505 3343 global cflist
63b79191 3344
45a9d505
PM
3345 $cflist conf -state normal
3346 $cflist tag remove bold 1.0 end
3347 $cflist conf -state disabled
63b79191
PM
3348}
3349
f8b28a40 3350proc add_flist {fl} {
45a9d505 3351 global cflist
7fcceed7 3352
45a9d505
PM
3353 $cflist conf -state normal
3354 foreach f $fl {
3355 $cflist insert end "\n"
3356 $cflist insert end $f [highlight_tag $f]
7fcceed7 3357 }
45a9d505 3358 $cflist conf -state disabled
7fcceed7
PM
3359}
3360
3361proc sel_flist {w x y} {
45a9d505 3362 global ctext difffilestart cflist cflist_top cmitmode
7fcceed7 3363
f8b28a40 3364 if {$cmitmode eq "tree"} return
7fcceed7
PM
3365 if {![info exists cflist_top]} return
3366 set l [lindex [split [$w index "@$x,$y"] "."] 0]
89b11d3b
PM
3367 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
3368 $cflist tag add highlight $l.0 "$l.0 lineend"
3369 set cflist_top $l
f8b28a40
PM
3370 if {$l == 1} {
3371 $ctext yview 1.0
3372 } else {
3373 catch {$ctext yview [lindex $difffilestart [expr {$l - 2}]]}
7fcceed7 3374 }
b967135d 3375 suppress_highlighting_file_for_current_scrollpos
7fcceed7
PM
3376}
3377
3244729a
PM
3378proc pop_flist_menu {w X Y x y} {
3379 global ctext cflist cmitmode flist_menu flist_menu_file
3380 global treediffs diffids
3381
bb3edc8b 3382 stopfinding
3244729a
PM
3383 set l [lindex [split [$w index "@$x,$y"] "."] 0]
3384 if {$l <= 1} return
3385 if {$cmitmode eq "tree"} {
3386 set e [linetoelt $l]
3387 if {[string index $e end] eq "/"} return
3388 } else {
3389 set e [lindex $treediffs($diffids) [expr {$l-2}]]
3390 }
3391 set flist_menu_file $e
314f5de1
TA
3392 set xdiffstate "normal"
3393 if {$cmitmode eq "tree"} {
3394 set xdiffstate "disabled"
3395 }
3396 # Disable "External diff" item in tree mode
3397 $flist_menu entryconf 2 -state $xdiffstate
3244729a
PM
3398 tk_popup $flist_menu $X $Y
3399}
3400
7cdc3556
AG
3401proc find_ctext_fileinfo {line} {
3402 global ctext_file_names ctext_file_lines
3403
3404 set ok [bsearch $ctext_file_lines $line]
3405 set tline [lindex $ctext_file_lines $ok]
3406
3407 if {$ok >= [llength $ctext_file_lines] || $line < $tline} {
3408 return {}
3409 } else {
3410 return [list [lindex $ctext_file_names $ok] $tline]
3411 }
3412}
3413
3414proc pop_diff_menu {w X Y x y} {
3415 global ctext diff_menu flist_menu_file
3416 global diff_menu_txtpos diff_menu_line
3417 global diff_menu_filebase
3418
7cdc3556
AG
3419 set diff_menu_txtpos [split [$w index "@$x,$y"] "."]
3420 set diff_menu_line [lindex $diff_menu_txtpos 0]
190ec52c
PM
3421 # don't pop up the menu on hunk-separator or file-separator lines
3422 if {[lsearch -glob [$ctext tag names $diff_menu_line.0] "*sep"] >= 0} {
3423 return
3424 }
3425 stopfinding
7cdc3556
AG
3426 set f [find_ctext_fileinfo $diff_menu_line]
3427 if {$f eq {}} return
3428 set flist_menu_file [lindex $f 0]
3429 set diff_menu_filebase [lindex $f 1]
3430 tk_popup $diff_menu $X $Y
3431}
3432
3244729a 3433proc flist_hl {only} {
bb3edc8b 3434 global flist_menu_file findstring gdttype
3244729a
PM
3435
3436 set x [shellquote $flist_menu_file]
b007ee20 3437 if {$only || $findstring eq {} || $gdttype ne [mc "touching paths:"]} {
bb3edc8b 3438 set findstring $x
3244729a 3439 } else {
bb3edc8b 3440 append findstring " " $x
3244729a 3441 }
b007ee20 3442 set gdttype [mc "touching paths:"]
3244729a
PM
3443}
3444
c21398be
PM
3445proc gitknewtmpdir {} {
3446 global diffnum gitktmpdir gitdir
3447
3448 if {![info exists gitktmpdir]} {
929f577e 3449 set gitktmpdir [file join $gitdir [format ".gitk-tmp.%s" [pid]]]
c21398be
PM
3450 if {[catch {file mkdir $gitktmpdir} err]} {
3451 error_popup "[mc "Error creating temporary directory %s:" $gitktmpdir] $err"
3452 unset gitktmpdir
3453 return {}
3454 }
3455 set diffnum 0
3456 }
3457 incr diffnum
3458 set diffdir [file join $gitktmpdir $diffnum]
3459 if {[catch {file mkdir $diffdir} err]} {
3460 error_popup "[mc "Error creating temporary directory %s:" $diffdir] $err"
3461 return {}
3462 }
3463 return $diffdir
3464}
3465
314f5de1
TA
3466proc save_file_from_commit {filename output what} {
3467 global nullfile
3468
3469 if {[catch {exec git show $filename -- > $output} err]} {
3470 if {[string match "fatal: bad revision *" $err]} {
3471 return $nullfile
3472 }
3945d2c0 3473 error_popup "[mc "Error getting \"%s\" from %s:" $filename $what] $err"
314f5de1
TA
3474 return {}
3475 }
3476 return $output
3477}
3478
3479proc external_diff_get_one_file {diffid filename diffdir} {
3480 global nullid nullid2 nullfile
784b7e2f 3481 global worktree
314f5de1
TA
3482
3483 if {$diffid == $nullid} {
784b7e2f 3484 set difffile [file join $worktree $filename]
314f5de1
TA
3485 if {[file exists $difffile]} {
3486 return $difffile
3487 }
3488 return $nullfile
3489 }
3490 if {$diffid == $nullid2} {
3491 set difffile [file join $diffdir "\[index\] [file tail $filename]"]
3492 return [save_file_from_commit :$filename $difffile index]
3493 }
3494 set difffile [file join $diffdir "\[$diffid\] [file tail $filename]"]
3495 return [save_file_from_commit $diffid:$filename $difffile \
3496 "revision $diffid"]
3497}
3498
3499proc external_diff {} {
c21398be 3500 global nullid nullid2
314f5de1
TA
3501 global flist_menu_file
3502 global diffids
c21398be 3503 global extdifftool
314f5de1
TA
3504
3505 if {[llength $diffids] == 1} {
3506 # no reference commit given
3507 set diffidto [lindex $diffids 0]
3508 if {$diffidto eq $nullid} {
3509 # diffing working copy with index
3510 set diffidfrom $nullid2
3511 } elseif {$diffidto eq $nullid2} {
3512 # diffing index with HEAD
3513 set diffidfrom "HEAD"
3514 } else {
3515 # use first parent commit
3516 global parentlist selectedline
3517 set diffidfrom [lindex $parentlist $selectedline 0]
3518 }
3519 } else {
3520 set diffidfrom [lindex $diffids 0]
3521 set diffidto [lindex $diffids 1]
3522 }
3523
3524 # make sure that several diffs wont collide
c21398be
PM
3525 set diffdir [gitknewtmpdir]
3526 if {$diffdir eq {}} return
314f5de1
TA
3527
3528 # gather files to diff
3529 set difffromfile [external_diff_get_one_file $diffidfrom $flist_menu_file $diffdir]
3530 set difftofile [external_diff_get_one_file $diffidto $flist_menu_file $diffdir]
3531
3532 if {$difffromfile ne {} && $difftofile ne {}} {
b575b2f1
PT
3533 set cmd [list [shellsplit $extdifftool] $difffromfile $difftofile]
3534 if {[catch {set fl [open |$cmd r]} err]} {
314f5de1 3535 file delete -force $diffdir
3945d2c0 3536 error_popup "$extdifftool: [mc "command failed:"] $err"
314f5de1
TA
3537 } else {
3538 fconfigure $fl -blocking 0
3539 filerun $fl [list delete_at_eof $fl $diffdir]
3540 }
3541 }
3542}
3543
7cdc3556
AG
3544proc find_hunk_blamespec {base line} {
3545 global ctext
3546
3547 # Find and parse the hunk header
3548 set s_lix [$ctext search -backwards -regexp ^@@ "$line.0 lineend" $base.0]
3549 if {$s_lix eq {}} return
3550
3551 set s_line [$ctext get $s_lix "$s_lix + 1 lines"]
3552 if {![regexp {^@@@*(( -\d+(,\d+)?)+) \+(\d+)(,\d+)? @@} $s_line \
3553 s_line old_specs osz osz1 new_line nsz]} {
3554 return
3555 }
3556
3557 # base lines for the parents
3558 set base_lines [list $new_line]
3559 foreach old_spec [lrange [split $old_specs " "] 1 end] {
3560 if {![regexp -- {-(\d+)(,\d+)?} $old_spec \
3561 old_spec old_line osz]} {
3562 return
3563 }
3564 lappend base_lines $old_line
3565 }
3566
3567 # Now scan the lines to determine offset within the hunk
7cdc3556
AG
3568 set max_parent [expr {[llength $base_lines]-2}]
3569 set dline 0
3570 set s_lno [lindex [split $s_lix "."] 0]
3571
190ec52c
PM
3572 # Determine if the line is removed
3573 set chunk [$ctext get $line.0 "$line.1 + $max_parent chars"]
3574 if {[string match {[-+ ]*} $chunk]} {
7cdc3556
AG
3575 set removed_idx [string first "-" $chunk]
3576 # Choose a parent index
190ec52c
PM
3577 if {$removed_idx >= 0} {
3578 set parent $removed_idx
3579 } else {
3580 set unchanged_idx [string first " " $chunk]
3581 if {$unchanged_idx >= 0} {
3582 set parent $unchanged_idx
7cdc3556 3583 } else {
190ec52c
PM
3584 # blame the current commit
3585 set parent -1
7cdc3556
AG
3586 }
3587 }
3588 # then count other lines that belong to it
190ec52c
PM
3589 for {set i $line} {[incr i -1] > $s_lno} {} {
3590 set chunk [$ctext get $i.0 "$i.1 + $max_parent chars"]
3591 # Determine if the line is removed
3592 set removed_idx [string first "-" $chunk]
3593 if {$parent >= 0} {
3594 set code [string index $chunk $parent]
3595 if {$code eq "-" || ($removed_idx < 0 && $code ne "+")} {
3596 incr dline
3597 }
3598 } else {
3599 if {$removed_idx < 0} {
3600 incr dline
3601 }
7cdc3556
AG
3602 }
3603 }
190ec52c
PM
3604 incr parent
3605 } else {
3606 set parent 0
7cdc3556
AG
3607 }
3608
7cdc3556
AG
3609 incr dline [lindex $base_lines $parent]
3610 return [list $parent $dline]
3611}
3612
3613proc external_blame_diff {} {
8b07dca1 3614 global currentid cmitmode
7cdc3556
AG
3615 global diff_menu_txtpos diff_menu_line
3616 global diff_menu_filebase flist_menu_file
3617
3618 if {$cmitmode eq "tree"} {
3619 set parent_idx 0
190ec52c 3620 set line [expr {$diff_menu_line - $diff_menu_filebase}]
7cdc3556
AG
3621 } else {
3622 set hinfo [find_hunk_blamespec $diff_menu_filebase $diff_menu_line]
3623 if {$hinfo ne {}} {
3624 set parent_idx [lindex $hinfo 0]
3625 set line [lindex $hinfo 1]
3626 } else {
3627 set parent_idx 0
3628 set line 0
3629 }
3630 }
3631
3632 external_blame $parent_idx $line
3633}
3634
fc4977e1
PM
3635# Find the SHA1 ID of the blob for file $fname in the index
3636# at stage 0 or 2
3637proc index_sha1 {fname} {
3638 set f [open [list | git ls-files -s $fname] r]
3639 while {[gets $f line] >= 0} {
3640 set info [lindex [split $line "\t"] 0]
3641 set stage [lindex $info 2]
3642 if {$stage eq "0" || $stage eq "2"} {
3643 close $f
3644 return [lindex $info 1]
3645 }
3646 }
3647 close $f
3648 return {}
3649}
3650
9712b81a
PM
3651# Turn an absolute path into one relative to the current directory
3652proc make_relative {f} {
a4390ace
MH
3653 if {[file pathtype $f] eq "relative"} {
3654 return $f
3655 }
9712b81a
PM
3656 set elts [file split $f]
3657 set here [file split [pwd]]
3658 set ei 0
3659 set hi 0
3660 set res {}
3661 foreach d $here {
3662 if {$ei < $hi || $ei >= [llength $elts] || [lindex $elts $ei] ne $d} {
3663 lappend res ".."
3664 } else {
3665 incr ei
3666 }
3667 incr hi
3668 }
3669 set elts [concat $res [lrange $elts $ei end]]
3670 return [eval file join $elts]
3671}
3672
7cdc3556 3673proc external_blame {parent_idx {line {}}} {
0a2a9793 3674 global flist_menu_file cdup
77aa0ae8
AG
3675 global nullid nullid2
3676 global parentlist selectedline currentid
3677
3678 if {$parent_idx > 0} {
3679 set base_commit [lindex $parentlist $selectedline [expr {$parent_idx-1}]]
3680 } else {
3681 set base_commit $currentid
3682 }
3683
3684 if {$base_commit eq {} || $base_commit eq $nullid || $base_commit eq $nullid2} {
3685 error_popup [mc "No such commit"]
3686 return
3687 }
3688
7cdc3556
AG
3689 set cmdline [list git gui blame]
3690 if {$line ne {} && $line > 1} {
3691 lappend cmdline "--line=$line"
3692 }
0a2a9793 3693 set f [file join $cdup $flist_menu_file]
9712b81a
PM
3694 # Unfortunately it seems git gui blame doesn't like
3695 # being given an absolute path...
3696 set f [make_relative $f]
3697 lappend cmdline $base_commit $f
7cdc3556 3698 if {[catch {eval exec $cmdline &} err]} {
3945d2c0 3699 error_popup "[mc "git gui blame: command failed:"] $err"
77aa0ae8
AG
3700 }
3701}
3702
8a897742
PM
3703proc show_line_source {} {
3704 global cmitmode currentid parents curview blamestuff blameinst
3705 global diff_menu_line diff_menu_filebase flist_menu_file
9b6adf34 3706 global nullid nullid2 gitdir cdup
8a897742 3707
fc4977e1 3708 set from_index {}
8a897742
PM
3709 if {$cmitmode eq "tree"} {
3710 set id $currentid
3711 set line [expr {$diff_menu_line - $diff_menu_filebase}]
3712 } else {
3713 set h [find_hunk_blamespec $diff_menu_filebase $diff_menu_line]
3714 if {$h eq {}} return
3715 set pi [lindex $h 0]
3716 if {$pi == 0} {
3717 mark_ctext_line $diff_menu_line
3718 return
3719 }
fc4977e1
PM
3720 incr pi -1
3721 if {$currentid eq $nullid} {
3722 if {$pi > 0} {
3723 # must be a merge in progress...
3724 if {[catch {
3725 # get the last line from .git/MERGE_HEAD
3726 set f [open [file join $gitdir MERGE_HEAD] r]
3727 set id [lindex [split [read $f] "\n"] end-1]
3728 close $f
3729 } err]} {
3730 error_popup [mc "Couldn't read merge head: %s" $err]
3731 return
3732 }
3733 } elseif {$parents($curview,$currentid) eq $nullid2} {
3734 # need to do the blame from the index
3735 if {[catch {
3736 set from_index [index_sha1 $flist_menu_file]
3737 } err]} {
3738 error_popup [mc "Error reading index: %s" $err]
3739 return
3740 }
9712b81a
PM
3741 } else {
3742 set id $parents($curview,$currentid)
fc4977e1
PM
3743 }
3744 } else {
3745 set id [lindex $parents($curview,$currentid) $pi]
3746 }
8a897742
PM
3747 set line [lindex $h 1]
3748 }
fc4977e1
PM
3749 set blameargs {}
3750 if {$from_index ne {}} {
3751 lappend blameargs | git cat-file blob $from_index
3752 }
3753 lappend blameargs | git blame -p -L$line,+1
3754 if {$from_index ne {}} {
3755 lappend blameargs --contents -
3756 } else {
3757 lappend blameargs $id
3758 }
9b6adf34 3759 lappend blameargs -- [file join $cdup $flist_menu_file]
8a897742 3760 if {[catch {
fc4977e1 3761 set f [open $blameargs r]
8a897742
PM
3762 } err]} {
3763 error_popup [mc "Couldn't start git blame: %s" $err]
3764 return
3765 }
f3413079 3766 nowbusy blaming [mc "Searching"]
8a897742
PM
3767 fconfigure $f -blocking 0
3768 set i [reg_instance $f]
3769 set blamestuff($i) {}
3770 set blameinst $i
3771 filerun $f [list read_line_source $f $i]
3772}
3773
3774proc stopblaming {} {
3775 global blameinst
3776
3777 if {[info exists blameinst]} {
3778 stop_instance $blameinst
3779 unset blameinst
f3413079 3780 notbusy blaming
8a897742
PM
3781 }
3782}
3783
3784proc read_line_source {fd inst} {
fc4977e1 3785 global blamestuff curview commfd blameinst nullid nullid2
8a897742
PM
3786
3787 while {[gets $fd line] >= 0} {
3788 lappend blamestuff($inst) $line
3789 }
3790 if {![eof $fd]} {
3791 return 1
3792 }
3793 unset commfd($inst)
3794 unset blameinst
f3413079 3795 notbusy blaming
8a897742
PM
3796 fconfigure $fd -blocking 1
3797 if {[catch {close $fd} err]} {
3798 error_popup [mc "Error running git blame: %s" $err]
3799 return 0
3800 }
3801
3802 set fname {}
3803 set line [split [lindex $blamestuff($inst) 0] " "]
3804 set id [lindex $line 0]
3805 set lnum [lindex $line 1]
3806 if {[string length $id] == 40 && [string is xdigit $id] &&
3807 [string is digit -strict $lnum]} {
3808 # look for "filename" line
3809 foreach l $blamestuff($inst) {
3810 if {[string match "filename *" $l]} {
3811 set fname [string range $l 9 end]
3812 break
3813 }
3814 }
3815 }
3816 if {$fname ne {}} {
3817 # all looks good, select it
fc4977e1
PM
3818 if {$id eq $nullid} {
3819 # blame uses all-zeroes to mean not committed,
3820 # which would mean a change in the index
3821 set id $nullid2
3822 }
8a897742
PM
3823 if {[commitinview $id $curview]} {
3824 selectline [rowofcommit $id] 1 [list $fname $lnum]
3825 } else {
3826 error_popup [mc "That line comes from commit %s, \
3827 which is not in this view" [shortids $id]]
3828 }
3829 } else {
3830 puts "oops couldn't parse git blame output"
3831 }
3832 return 0
3833}
3834
314f5de1
TA
3835# delete $dir when we see eof on $f (presumably because the child has exited)
3836proc delete_at_eof {f dir} {
3837 while {[gets $f line] >= 0} {}
3838 if {[eof $f]} {
3839 if {[catch {close $f} err]} {
3945d2c0 3840 error_popup "[mc "External diff viewer failed:"] $err"
314f5de1
TA
3841 }
3842 file delete -force $dir
3843 return 0
3844 }
3845 return 1
3846}
3847
098dd8a3
PM
3848# Functions for adding and removing shell-type quoting
3849
3850proc shellquote {str} {
3851 if {![string match "*\['\"\\ \t]*" $str]} {
3852 return $str
3853 }
3854 if {![string match "*\['\"\\]*" $str]} {
3855 return "\"$str\""
3856 }
3857 if {![string match "*'*" $str]} {
3858 return "'$str'"
3859 }
3860 return "\"[string map {\" \\\" \\ \\\\} $str]\""
3861}
3862
3863proc shellarglist {l} {
3864 set str {}
3865 foreach a $l {
3866 if {$str ne {}} {
3867 append str " "
3868 }
3869 append str [shellquote $a]
3870 }
3871 return $str
3872}
3873
3874proc shelldequote {str} {
3875 set ret {}
3876 set used -1
3877 while {1} {
3878 incr used
3879 if {![regexp -start $used -indices "\['\"\\\\ \t]" $str first]} {
3880 append ret [string range $str $used end]
3881 set used [string length $str]
3882 break
3883 }
3884 set first [lindex $first 0]
3885 set ch [string index $str $first]
3886 if {$first > $used} {
3887 append ret [string range $str $used [expr {$first - 1}]]
3888 set used $first
3889 }
3890 if {$ch eq " " || $ch eq "\t"} break
3891 incr used
3892 if {$ch eq "'"} {
3893 set first [string first "'" $str $used]
3894 if {$first < 0} {
3895 error "unmatched single-quote"
3896 }
3897 append ret [string range $str $used [expr {$first - 1}]]
3898 set used $first
3899 continue
3900 }
3901 if {$ch eq "\\"} {
3902 if {$used >= [string length $str]} {
3903 error "trailing backslash"
3904 }
3905 append ret [string index $str $used]
3906 continue
3907 }
3908 # here ch == "\""
3909 while {1} {
3910 if {![regexp -start $used -indices "\[\"\\\\]" $str first]} {
3911 error "unmatched double-quote"
3912 }
3913 set first [lindex $first 0]
3914 set ch [string index $str $first]
3915 if {$first > $used} {
3916 append ret [string range $str $used [expr {$first - 1}]]
3917 set used $first
3918 }
3919 if {$ch eq "\""} break
3920 incr used
3921 append ret [string index $str $used]
3922 incr used
3923 }
3924 }
3925 return [list $used $ret]
3926}
3927
3928proc shellsplit {str} {
3929 set l {}
3930 while {1} {
3931 set str [string trimleft $str]
3932 if {$str eq {}} break
3933 set dq [shelldequote $str]
3934 set n [lindex $dq 0]
3935 set word [lindex $dq 1]
3936 set str [string range $str $n end]
3937 lappend l $word
3938 }
3939 return $l
3940}
3941
7fcceed7
PM
3942# Code to implement multiple views
3943
da7c24dd 3944proc newview {ishighlight} {
218a900b
AG
3945 global nextviewnum newviewname newishighlight
3946 global revtreeargs viewargscmd newviewopts curview
50b44ece 3947
da7c24dd 3948 set newishighlight $ishighlight
50b44ece
PM
3949 set top .gitkview
3950 if {[winfo exists $top]} {
3951 raise $top
3952 return
3953 }
5d11f794 3954 decode_view_opts $nextviewnum $revtreeargs
a3a1f579 3955 set newviewname($nextviewnum) "[mc "View"] $nextviewnum"
218a900b
AG
3956 set newviewopts($nextviewnum,perm) 0
3957 set newviewopts($nextviewnum,cmd) $viewargscmd($curview)
d990cedf 3958 vieweditor $top $nextviewnum [mc "Gitk view definition"]
d16c0812
PM
3959}
3960
218a900b 3961set known_view_options {
13d40b61
EN
3962 {perm b . {} {mc "Remember this view"}}
3963 {reflabel l + {} {mc "References (space separated list):"}}
3964 {refs t15 .. {} {mc "Branches & tags:"}}
3965 {allrefs b *. "--all" {mc "All refs"}}
3966 {branches b . "--branches" {mc "All (local) branches"}}
3967 {tags b . "--tags" {mc "All tags"}}
3968 {remotes b . "--remotes" {mc "All remote-tracking branches"}}
3969 {commitlbl l + {} {mc "Commit Info (regular expressions):"}}
3970 {author t15 .. "--author=*" {mc "Author:"}}
3971 {committer t15 . "--committer=*" {mc "Committer:"}}
3972 {loginfo t15 .. "--grep=*" {mc "Commit Message:"}}
3973 {allmatch b .. "--all-match" {mc "Matches all Commit Info criteria"}}
3974 {changes_l l + {} {mc "Changes to Files:"}}
3975 {pickaxe_s r0 . {} {mc "Fixed String"}}
3976 {pickaxe_t r1 . "--pickaxe-regex" {mc "Regular Expression"}}
3977 {pickaxe t15 .. "-S*" {mc "Search string:"}}
3978 {datelabel l + {} {mc "Commit Dates (\"2 weeks ago\", \"2009-03-17 15:27:38\", \"March 17, 2009 15:27:38\"):"}}
3979 {since t15 .. {"--since=*" "--after=*"} {mc "Since:"}}
3980 {until t15 . {"--until=*" "--before=*"} {mc "Until:"}}
3981 {limit_lbl l + {} {mc "Limit and/or skip a number of revisions (positive integer):"}}
3982 {limit t10 *. "--max-count=*" {mc "Number to show:"}}
3983 {skip t10 . "--skip=*" {mc "Number to skip:"}}
3984 {misc_lbl l + {} {mc "Miscellaneous options:"}}
3985 {dorder b *. {"--date-order" "-d"} {mc "Strictly sort by date"}}
3986 {lright b . "--left-right" {mc "Mark branch sides"}}
3987 {first b . "--first-parent" {mc "Limit to first parent"}}
f687aaa8 3988 {smplhst b . "--simplify-by-decoration" {mc "Simple history"}}
13d40b61
EN
3989 {args t50 *. {} {mc "Additional arguments to git log:"}}
3990 {allpaths path + {} {mc "Enter files and directories to include, one per line:"}}
3991 {cmd t50= + {} {mc "Command to generate more commits to include:"}}
218a900b
AG
3992 }
3993
e7feb695 3994# Convert $newviewopts($n, ...) into args for git log.
218a900b
AG
3995proc encode_view_opts {n} {
3996 global known_view_options newviewopts
3997
3998 set rargs [list]
3999 foreach opt $known_view_options {
4000 set patterns [lindex $opt 3]
4001 if {$patterns eq {}} continue
4002 set pattern [lindex $patterns 0]
4003
218a900b 4004 if {[lindex $opt 1] eq "b"} {
13d40b61 4005 set val $newviewopts($n,[lindex $opt 0])
218a900b
AG
4006 if {$val} {
4007 lappend rargs $pattern
4008 }
13d40b61
EN
4009 } elseif {[regexp {^r(\d+)$} [lindex $opt 1] type value]} {
4010 regexp {^(.*_)} [lindex $opt 0] uselessvar button_id
4011 set val $newviewopts($n,$button_id)
4012 if {$val eq $value} {
4013 lappend rargs $pattern
4014 }
218a900b 4015 } else {
13d40b61 4016 set val $newviewopts($n,[lindex $opt 0])
218a900b
AG
4017 set val [string trim $val]
4018 if {$val ne {}} {
4019 set pfix [string range $pattern 0 end-1]
4020 lappend rargs $pfix$val
4021 }
4022 }
4023 }
13d40b61 4024 set rargs [concat $rargs [shellsplit $newviewopts($n,refs)]]
218a900b
AG
4025 return [concat $rargs [shellsplit $newviewopts($n,args)]]
4026}
4027
e7feb695 4028# Fill $newviewopts($n, ...) based on args for git log.
218a900b
AG
4029proc decode_view_opts {n view_args} {
4030 global known_view_options newviewopts
4031
4032 foreach opt $known_view_options {
13d40b61 4033 set id [lindex $opt 0]
218a900b 4034 if {[lindex $opt 1] eq "b"} {
13d40b61
EN
4035 # Checkboxes
4036 set val 0
4037 } elseif {[regexp {^r(\d+)$} [lindex $opt 1]]} {
4038 # Radiobuttons
4039 regexp {^(.*_)} $id uselessvar id
218a900b
AG
4040 set val 0
4041 } else {
13d40b61 4042 # Text fields
218a900b
AG
4043 set val {}
4044 }
13d40b61 4045 set newviewopts($n,$id) $val
218a900b
AG
4046 }
4047 set oargs [list]
13d40b61 4048 set refargs [list]
218a900b
AG
4049 foreach arg $view_args {
4050 if {[regexp -- {^-([0-9]+)$} $arg arg cnt]
4051 && ![info exists found(limit)]} {
4052 set newviewopts($n,limit) $cnt
4053 set found(limit) 1
4054 continue
4055 }
4056 catch { unset val }
4057 foreach opt $known_view_options {
4058 set id [lindex $opt 0]
4059 if {[info exists found($id)]} continue
4060 foreach pattern [lindex $opt 3] {
4061 if {![string match $pattern $arg]} continue
13d40b61
EN
4062 if {[lindex $opt 1] eq "b"} {
4063 # Check buttons
4064 set val 1
4065 } elseif {[regexp {^r(\d+)$} [lindex $opt 1] match num]} {
4066 # Radio buttons
4067 regexp {^(.*_)} $id uselessvar id
4068 set val $num
4069 } else {
4070 # Text input fields
218a900b
AG
4071 set size [string length $pattern]
4072 set val [string range $arg [expr {$size-1}] end]
218a900b
AG
4073 }
4074 set newviewopts($n,$id) $val
4075 set found($id) 1
4076 break
4077 }
4078 if {[info exists val]} break
4079 }
4080 if {[info exists val]} continue
13d40b61
EN
4081 if {[regexp {^-} $arg]} {
4082 lappend oargs $arg
4083 } else {
4084 lappend refargs $arg
4085 }
218a900b 4086 }
13d40b61 4087 set newviewopts($n,refs) [shellarglist $refargs]
218a900b
AG
4088 set newviewopts($n,args) [shellarglist $oargs]
4089}
4090
cea07cf8
AG
4091proc edit_or_newview {} {
4092 global curview
4093
4094 if {$curview > 0} {
4095 editview
4096 } else {
4097 newview 0
4098 }
4099}
4100
d16c0812
PM
4101proc editview {} {
4102 global curview
218a900b
AG
4103 global viewname viewperm newviewname newviewopts
4104 global viewargs viewargscmd
d16c0812
PM
4105
4106 set top .gitkvedit-$curview
4107 if {[winfo exists $top]} {
4108 raise $top
4109 return
4110 }
5d11f794 4111 decode_view_opts $curview $viewargs($curview)
218a900b
AG
4112 set newviewname($curview) $viewname($curview)
4113 set newviewopts($curview,perm) $viewperm($curview)
4114 set newviewopts($curview,cmd) $viewargscmd($curview)
b56e0a9a 4115 vieweditor $top $curview "[mc "Gitk: edit view"] $viewname($curview)"
d16c0812
PM
4116}
4117
4118proc vieweditor {top n title} {
218a900b 4119 global newviewname newviewopts viewfiles bgcolor
d93f1713 4120 global known_view_options NS
d16c0812 4121
d93f1713 4122 ttk_toplevel $top
e0a01995 4123 wm title $top [concat $title [mc "-- criteria for selecting revisions"]]
e7d64008 4124 make_transient $top .
218a900b
AG
4125
4126 # View name
d93f1713 4127 ${NS}::frame $top.nfr
eae7d64a 4128 ${NS}::label $top.nl -text [mc "View Name"]
d93f1713 4129 ${NS}::entry $top.name -width 20 -textvariable newviewname($n)
218a900b 4130 pack $top.nfr -in $top -fill x -pady 5 -padx 3
13d40b61
EN
4131 pack $top.nl -in $top.nfr -side left -padx {0 5}
4132 pack $top.name -in $top.nfr -side left -padx {0 25}
218a900b
AG
4133
4134 # View options
4135 set cframe $top.nfr
4136 set cexpand 0
4137 set cnt 0
4138 foreach opt $known_view_options {
4139 set id [lindex $opt 0]
4140 set type [lindex $opt 1]
4141 set flags [lindex $opt 2]
4142 set title [eval [lindex $opt 4]]
4143 set lxpad 0
4144
4145 if {$flags eq "+" || $flags eq "*"} {
4146 set cframe $top.fr$cnt
4147 incr cnt
d93f1713 4148 ${NS}::frame $cframe
218a900b
AG
4149 pack $cframe -in $top -fill x -pady 3 -padx 3
4150 set cexpand [expr {$flags eq "*"}]
13d40b61
EN
4151 } elseif {$flags eq ".." || $flags eq "*."} {
4152 set cframe $top.fr$cnt
4153 incr cnt
eae7d64a 4154 ${NS}::frame $cframe
13d40b61
EN
4155 pack $cframe -in $top -fill x -pady 3 -padx [list 15 3]
4156 set cexpand [expr {$flags eq "*."}]
218a900b
AG
4157 } else {
4158 set lxpad 5
4159 }
4160
13d40b61 4161 if {$type eq "l"} {
eae7d64a 4162 ${NS}::label $cframe.l_$id -text $title
13d40b61
EN
4163 pack $cframe.l_$id -in $cframe -side left -pady [list 3 0] -anchor w
4164 } elseif {$type eq "b"} {
d93f1713 4165 ${NS}::checkbutton $cframe.c_$id -text $title -variable newviewopts($n,$id)
218a900b
AG
4166 pack $cframe.c_$id -in $cframe -side left \
4167 -padx [list $lxpad 0] -expand $cexpand -anchor w
13d40b61
EN
4168 } elseif {[regexp {^r(\d+)$} $type type sz]} {
4169 regexp {^(.*_)} $id uselessvar button_id
eae7d64a 4170 ${NS}::radiobutton $cframe.c_$id -text $title -variable newviewopts($n,$button_id) -value $sz
13d40b61
EN
4171 pack $cframe.c_$id -in $cframe -side left \
4172 -padx [list $lxpad 0] -expand $cexpand -anchor w
218a900b 4173 } elseif {[regexp {^t(\d+)$} $type type sz]} {
d93f1713
PT
4174 ${NS}::label $cframe.l_$id -text $title
4175 ${NS}::entry $cframe.e_$id -width $sz -background $bgcolor \
218a900b
AG
4176 -textvariable newviewopts($n,$id)
4177 pack $cframe.l_$id -in $cframe -side left -padx [list $lxpad 0]
4178 pack $cframe.e_$id -in $cframe -side left -expand 1 -fill x
4179 } elseif {[regexp {^t(\d+)=$} $type type sz]} {
d93f1713
PT
4180 ${NS}::label $cframe.l_$id -text $title
4181 ${NS}::entry $cframe.e_$id -width $sz -background $bgcolor \
218a900b
AG
4182 -textvariable newviewopts($n,$id)
4183 pack $cframe.l_$id -in $cframe -side top -pady [list 3 0] -anchor w
4184 pack $cframe.e_$id -in $cframe -side top -fill x
13d40b61 4185 } elseif {$type eq "path"} {
eae7d64a 4186 ${NS}::label $top.l -text $title
13d40b61 4187 pack $top.l -in $top -side top -pady [list 3 0] -anchor w -padx 3
b9b142ff 4188 text $top.t -width 40 -height 5 -background $bgcolor
13d40b61
EN
4189 if {[info exists viewfiles($n)]} {
4190 foreach f $viewfiles($n) {
4191 $top.t insert end $f
4192 $top.t insert end "\n"
4193 }
4194 $top.t delete {end - 1c} end
4195 $top.t mark set insert 0.0
4196 }
4197 pack $top.t -in $top -side top -pady [list 0 5] -fill both -expand 1 -padx 3
218a900b
AG
4198 }
4199 }
4200
d93f1713
PT
4201 ${NS}::frame $top.buts
4202 ${NS}::button $top.buts.ok -text [mc "OK"] -command [list newviewok $top $n]
4203 ${NS}::button $top.buts.apply -text [mc "Apply (F5)"] -command [list newviewok $top $n 1]
4204 ${NS}::button $top.buts.can -text [mc "Cancel"] -command [list destroy $top]
218a900b
AG
4205 bind $top <Control-Return> [list newviewok $top $n]
4206 bind $top <F5> [list newviewok $top $n 1]
76f15947 4207 bind $top <Escape> [list destroy $top]
218a900b 4208 grid $top.buts.ok $top.buts.apply $top.buts.can
50b44ece
PM
4209 grid columnconfigure $top.buts 0 -weight 1 -uniform a
4210 grid columnconfigure $top.buts 1 -weight 1 -uniform a
218a900b
AG
4211 grid columnconfigure $top.buts 2 -weight 1 -uniform a
4212 pack $top.buts -in $top -side top -fill x
50b44ece
PM
4213 focus $top.t
4214}
4215
908c3585 4216proc doviewmenu {m first cmd op argv} {
da7c24dd
PM
4217 set nmenu [$m index end]
4218 for {set i $first} {$i <= $nmenu} {incr i} {
4219 if {[$m entrycget $i -command] eq $cmd} {
908c3585 4220 eval $m $op $i $argv
da7c24dd 4221 break
d16c0812
PM
4222 }
4223 }
da7c24dd
PM
4224}
4225
4226proc allviewmenus {n op args} {
687c8765 4227 # global viewhlmenu
908c3585 4228
3cd204e5 4229 doviewmenu .bar.view 5 [list showview $n] $op $args
687c8765 4230 # doviewmenu $viewhlmenu 1 [list addvhighlight $n] $op $args
d16c0812
PM
4231}
4232
218a900b 4233proc newviewok {top n {apply 0}} {
da7c24dd 4234 global nextviewnum newviewperm newviewname newishighlight
d16c0812 4235 global viewname viewfiles viewperm selectedview curview
218a900b 4236 global viewargs viewargscmd newviewopts viewhlmenu
50b44ece 4237
098dd8a3 4238 if {[catch {
218a900b 4239 set newargs [encode_view_opts $n]
098dd8a3 4240 } err]} {
84a76f18 4241 error_popup "[mc "Error in commit selection arguments:"] $err" $top
098dd8a3
PM
4242 return
4243 }
50b44ece 4244 set files {}
d16c0812 4245 foreach f [split [$top.t get 0.0 end] "\n"] {
50b44ece
PM
4246 set ft [string trim $f]
4247 if {$ft ne {}} {
4248 lappend files $ft
4249 }
4250 }
d16c0812
PM
4251 if {![info exists viewfiles($n)]} {
4252 # creating a new view
4253 incr nextviewnum
4254 set viewname($n) $newviewname($n)
218a900b 4255 set viewperm($n) $newviewopts($n,perm)
d16c0812 4256 set viewfiles($n) $files
098dd8a3 4257 set viewargs($n) $newargs
218a900b 4258 set viewargscmd($n) $newviewopts($n,cmd)
da7c24dd
PM
4259 addviewmenu $n
4260 if {!$newishighlight} {
7eb3cb9c 4261 run showview $n
da7c24dd 4262 } else {
7eb3cb9c 4263 run addvhighlight $n
da7c24dd 4264 }
d16c0812
PM
4265 } else {
4266 # editing an existing view
218a900b 4267 set viewperm($n) $newviewopts($n,perm)
d16c0812
PM
4268 if {$newviewname($n) ne $viewname($n)} {
4269 set viewname($n) $newviewname($n)
3cd204e5 4270 doviewmenu .bar.view 5 [list showview $n] \
908c3585 4271 entryconf [list -label $viewname($n)]
687c8765
PM
4272 # doviewmenu $viewhlmenu 1 [list addvhighlight $n] \
4273 # entryconf [list -label $viewname($n) -value $viewname($n)]
d16c0812 4274 }
2d480856 4275 if {$files ne $viewfiles($n) || $newargs ne $viewargs($n) || \
218a900b 4276 $newviewopts($n,cmd) ne $viewargscmd($n)} {
d16c0812 4277 set viewfiles($n) $files
098dd8a3 4278 set viewargs($n) $newargs
218a900b 4279 set viewargscmd($n) $newviewopts($n,cmd)
d16c0812 4280 if {$curview == $n} {
7fcc92bf 4281 run reloadcommits
d16c0812
PM
4282 }
4283 }
4284 }
218a900b 4285 if {$apply} return
d16c0812 4286 catch {destroy $top}
50b44ece
PM
4287}
4288
4289proc delview {} {
7fcc92bf 4290 global curview viewperm hlview selectedhlview
50b44ece
PM
4291
4292 if {$curview == 0} return
908c3585 4293 if {[info exists hlview] && $hlview == $curview} {
b007ee20 4294 set selectedhlview [mc "None"]
908c3585
PM
4295 unset hlview
4296 }
da7c24dd 4297 allviewmenus $curview delete
a90a6d24 4298 set viewperm($curview) 0
50b44ece
PM
4299 showview 0
4300}
4301
da7c24dd 4302proc addviewmenu {n} {
908c3585 4303 global viewname viewhlmenu
da7c24dd
PM
4304
4305 .bar.view add radiobutton -label $viewname($n) \
4306 -command [list showview $n] -variable selectedview -value $n
687c8765
PM
4307 #$viewhlmenu add radiobutton -label $viewname($n) \
4308 # -command [list addvhighlight $n] -variable selectedhlview
da7c24dd
PM
4309}
4310
50b44ece 4311proc showview {n} {
3ed31a81 4312 global curview cached_commitrow ordertok
f5f3c2e2 4313 global displayorder parentlist rowidlist rowisopt rowfinal
7fcc92bf
PM
4314 global colormap rowtextx nextcolor canvxmax
4315 global numcommits viewcomplete
50b44ece 4316 global selectedline currentid canv canvy0
4fb0fa19 4317 global treediffs
3e76608d 4318 global pending_select mainheadid
0380081c 4319 global commitidx
3e76608d 4320 global selectedview
97645683 4321 global hlview selectedhlview commitinterest
50b44ece
PM
4322
4323 if {$n == $curview} return
4324 set selid {}
7fcc92bf
PM
4325 set ymax [lindex [$canv cget -scrollregion] 3]
4326 set span [$canv yview]
4327 set ytop [expr {[lindex $span 0] * $ymax}]
4328 set ybot [expr {[lindex $span 1] * $ymax}]
4329 set yscreen [expr {($ybot - $ytop) / 2}]
94b4a69f 4330 if {$selectedline ne {}} {
50b44ece
PM
4331 set selid $currentid
4332 set y [yc $selectedline]
50b44ece
PM
4333 if {$ytop < $y && $y < $ybot} {
4334 set yscreen [expr {$y - $ytop}]
50b44ece 4335 }
e507fd48
PM
4336 } elseif {[info exists pending_select]} {
4337 set selid $pending_select
4338 unset pending_select
50b44ece
PM
4339 }
4340 unselectline
fdedbcfb 4341 normalline
50b44ece
PM
4342 catch {unset treediffs}
4343 clear_display
908c3585
PM
4344 if {[info exists hlview] && $hlview == $n} {
4345 unset hlview
b007ee20 4346 set selectedhlview [mc "None"]
908c3585 4347 }
97645683 4348 catch {unset commitinterest}
7fcc92bf 4349 catch {unset cached_commitrow}
9257d8f7 4350 catch {unset ordertok}
50b44ece
PM
4351
4352 set curview $n
a90a6d24 4353 set selectedview $n
f2d0bbbd
PM
4354 .bar.view entryconf [mca "Edit view..."] -state [expr {$n == 0? "disabled": "normal"}]
4355 .bar.view entryconf [mca "Delete view"] -state [expr {$n == 0? "disabled": "normal"}]
50b44ece 4356
df904497 4357 run refill_reflist
7fcc92bf 4358 if {![info exists viewcomplete($n)]} {
567c34e0 4359 getcommits $selid
50b44ece
PM
4360 return
4361 }
4362
7fcc92bf
PM
4363 set displayorder {}
4364 set parentlist {}
4365 set rowidlist {}
4366 set rowisopt {}
4367 set rowfinal {}
f5f3c2e2 4368 set numcommits $commitidx($n)
22626ef4 4369
50b44ece
PM
4370 catch {unset colormap}
4371 catch {unset rowtextx}
da7c24dd
PM
4372 set nextcolor 0
4373 set canvxmax [$canv cget -width]
50b44ece
PM
4374 set curview $n
4375 set row 0
50b44ece
PM
4376 setcanvscroll
4377 set yf 0
e507fd48 4378 set row {}
7fcc92bf
PM
4379 if {$selid ne {} && [commitinview $selid $n]} {
4380 set row [rowofcommit $selid]
50b44ece
PM
4381 # try to get the selected row in the same position on the screen
4382 set ymax [lindex [$canv cget -scrollregion] 3]
4383 set ytop [expr {[yc $row] - $yscreen}]
4384 if {$ytop < 0} {
4385 set ytop 0
4386 }
4387 set yf [expr {$ytop * 1.0 / $ymax}]
4388 }
4389 allcanvs yview moveto $yf
4390 drawvisible
e507fd48
PM
4391 if {$row ne {}} {
4392 selectline $row 0
3e76608d 4393 } elseif {!$viewcomplete($n)} {
567c34e0 4394 reset_pending_select $selid
e507fd48 4395 } else {
835e62ae
AG
4396 reset_pending_select {}
4397
4398 if {[commitinview $pending_select $curview]} {
4399 selectline [rowofcommit $pending_select] 1
4400 } else {
4401 set row [first_real_row]
4402 if {$row < $numcommits} {
4403 selectline $row 0
4404 }
e507fd48
PM
4405 }
4406 }
7fcc92bf
PM
4407 if {!$viewcomplete($n)} {
4408 if {$numcommits == 0} {
d990cedf 4409 show_status [mc "Reading commits..."]
d16c0812 4410 }
098dd8a3 4411 } elseif {$numcommits == 0} {
d990cedf 4412 show_status [mc "No commits selected"]
2516dae2 4413 }
50b44ece
PM
4414}
4415
908c3585
PM
4416# Stuff relating to the highlighting facility
4417
476ca63d 4418proc ishighlighted {id} {
164ff275 4419 global vhighlights fhighlights nhighlights rhighlights
908c3585 4420
476ca63d
PM
4421 if {[info exists nhighlights($id)] && $nhighlights($id) > 0} {
4422 return $nhighlights($id)
908c3585 4423 }
476ca63d
PM
4424 if {[info exists vhighlights($id)] && $vhighlights($id) > 0} {
4425 return $vhighlights($id)
908c3585 4426 }
476ca63d
PM
4427 if {[info exists fhighlights($id)] && $fhighlights($id) > 0} {
4428 return $fhighlights($id)
908c3585 4429 }
476ca63d
PM
4430 if {[info exists rhighlights($id)] && $rhighlights($id) > 0} {
4431 return $rhighlights($id)
164ff275 4432 }
908c3585
PM
4433 return 0
4434}
4435
28593d3f 4436proc bolden {id font} {
b9fdba7f 4437 global canv linehtag currentid boldids need_redisplay markedid
908c3585 4438
d98d50e2
PM
4439 # need_redisplay = 1 means the display is stale and about to be redrawn
4440 if {$need_redisplay} return
28593d3f
PM
4441 lappend boldids $id
4442 $canv itemconf $linehtag($id) -font $font
4443 if {[info exists currentid] && $id eq $currentid} {
908c3585 4444 $canv delete secsel
28593d3f 4445 set t [eval $canv create rect [$canv bbox $linehtag($id)] \
908c3585
PM
4446 -outline {{}} -tags secsel \
4447 -fill [$canv cget -selectbackground]]
4448 $canv lower $t
4449 }
b9fdba7f
PM
4450 if {[info exists markedid] && $id eq $markedid} {
4451 make_idmark $id
4452 }
908c3585
PM
4453}
4454
28593d3f
PM
4455proc bolden_name {id font} {
4456 global canv2 linentag currentid boldnameids need_redisplay
908c3585 4457
d98d50e2 4458 if {$need_redisplay} return
28593d3f
PM
4459 lappend boldnameids $id
4460 $canv2 itemconf $linentag($id) -font $font
4461 if {[info exists currentid] && $id eq $currentid} {
908c3585 4462 $canv2 delete secsel
28593d3f 4463 set t [eval $canv2 create rect [$canv2 bbox $linentag($id)] \
908c3585
PM
4464 -outline {{}} -tags secsel \
4465 -fill [$canv2 cget -selectbackground]]
4466 $canv2 lower $t
4467 }
4468}
4469
4e7d6779 4470proc unbolden {} {
28593d3f 4471 global boldids
908c3585 4472
4e7d6779 4473 set stillbold {}
28593d3f
PM
4474 foreach id $boldids {
4475 if {![ishighlighted $id]} {
4476 bolden $id mainfont
4e7d6779 4477 } else {
28593d3f 4478 lappend stillbold $id
908c3585
PM
4479 }
4480 }
28593d3f 4481 set boldids $stillbold
908c3585
PM
4482}
4483
4484proc addvhighlight {n} {
476ca63d 4485 global hlview viewcomplete curview vhl_done commitidx
da7c24dd
PM
4486
4487 if {[info exists hlview]} {
908c3585 4488 delvhighlight
da7c24dd
PM
4489 }
4490 set hlview $n
7fcc92bf 4491 if {$n != $curview && ![info exists viewcomplete($n)]} {
da7c24dd 4492 start_rev_list $n
908c3585
PM
4493 }
4494 set vhl_done $commitidx($hlview)
4495 if {$vhl_done > 0} {
4496 drawvisible
da7c24dd
PM
4497 }
4498}
4499
908c3585
PM
4500proc delvhighlight {} {
4501 global hlview vhighlights
da7c24dd
PM
4502
4503 if {![info exists hlview]} return
4504 unset hlview
4e7d6779
PM
4505 catch {unset vhighlights}
4506 unbolden
da7c24dd
PM
4507}
4508
908c3585 4509proc vhighlightmore {} {
7fcc92bf 4510 global hlview vhl_done commitidx vhighlights curview
da7c24dd 4511
da7c24dd 4512 set max $commitidx($hlview)
908c3585
PM
4513 set vr [visiblerows]
4514 set r0 [lindex $vr 0]
4515 set r1 [lindex $vr 1]
4516 for {set i $vhl_done} {$i < $max} {incr i} {
7fcc92bf
PM
4517 set id [commitonrow $i $hlview]
4518 if {[commitinview $id $curview]} {
4519 set row [rowofcommit $id]
908c3585
PM
4520 if {$r0 <= $row && $row <= $r1} {
4521 if {![highlighted $row]} {
28593d3f 4522 bolden $id mainfontbold
da7c24dd 4523 }
476ca63d 4524 set vhighlights($id) 1
da7c24dd
PM
4525 }
4526 }
4527 }
908c3585 4528 set vhl_done $max
ac1276ab 4529 return 0
908c3585
PM
4530}
4531
4532proc askvhighlight {row id} {
7fcc92bf 4533 global hlview vhighlights iddrawn
908c3585 4534
7fcc92bf 4535 if {[commitinview $id $hlview]} {
476ca63d 4536 if {[info exists iddrawn($id)] && ![ishighlighted $id]} {
28593d3f 4537 bolden $id mainfontbold
908c3585 4538 }
476ca63d 4539 set vhighlights($id) 1
908c3585 4540 } else {
476ca63d 4541 set vhighlights($id) 0
908c3585
PM
4542 }
4543}
4544
687c8765 4545proc hfiles_change {} {
908c3585 4546 global highlight_files filehighlight fhighlights fh_serial
8b39e04f 4547 global highlight_paths
908c3585
PM
4548
4549 if {[info exists filehighlight]} {
4550 # delete previous highlights
4551 catch {close $filehighlight}
4552 unset filehighlight
4e7d6779
PM
4553 catch {unset fhighlights}
4554 unbolden
63b79191 4555 unhighlight_filelist
908c3585 4556 }
63b79191 4557 set highlight_paths {}
908c3585
PM
4558 after cancel do_file_hl $fh_serial
4559 incr fh_serial
4560 if {$highlight_files ne {}} {
4561 after 300 do_file_hl $fh_serial
4562 }
4563}
4564
687c8765
PM
4565proc gdttype_change {name ix op} {
4566 global gdttype highlight_files findstring findpattern
4567
bb3edc8b 4568 stopfinding
687c8765 4569 if {$findstring ne {}} {
b007ee20 4570 if {$gdttype eq [mc "containing:"]} {
687c8765
PM
4571 if {$highlight_files ne {}} {
4572 set highlight_files {}
4573 hfiles_change
4574 }
4575 findcom_change
4576 } else {
4577 if {$findpattern ne {}} {
4578 set findpattern {}
4579 findcom_change
4580 }
4581 set highlight_files $findstring
4582 hfiles_change
4583 }
4584 drawvisible
4585 }
4586 # enable/disable findtype/findloc menus too
4587}
4588
4589proc find_change {name ix op} {
4590 global gdttype findstring highlight_files
4591
bb3edc8b 4592 stopfinding
b007ee20 4593 if {$gdttype eq [mc "containing:"]} {
687c8765
PM
4594 findcom_change
4595 } else {
4596 if {$highlight_files ne $findstring} {
4597 set highlight_files $findstring
4598 hfiles_change
4599 }
4600 }
4601 drawvisible
4602}
4603
64b5f146 4604proc findcom_change args {
28593d3f 4605 global nhighlights boldnameids
687c8765
PM
4606 global findpattern findtype findstring gdttype
4607
bb3edc8b 4608 stopfinding
687c8765 4609 # delete previous highlights, if any
28593d3f
PM
4610 foreach id $boldnameids {
4611 bolden_name $id mainfont
687c8765 4612 }
28593d3f 4613 set boldnameids {}
687c8765
PM
4614 catch {unset nhighlights}
4615 unbolden
4616 unmarkmatches
b007ee20 4617 if {$gdttype ne [mc "containing:"] || $findstring eq {}} {
687c8765 4618 set findpattern {}
b007ee20 4619 } elseif {$findtype eq [mc "Regexp"]} {
687c8765
PM
4620 set findpattern $findstring
4621 } else {
4622 set e [string map {"*" "\\*" "?" "\\?" "\[" "\\\[" "\\" "\\\\"} \
4623 $findstring]
4624 set findpattern "*$e*"
4625 }
4626}
4627
63b79191
PM
4628proc makepatterns {l} {
4629 set ret {}
4630 foreach e $l {
4631 set ee [string map {"*" "\\*" "?" "\\?" "\[" "\\\[" "\\" "\\\\"} $e]
4632 if {[string index $ee end] eq "/"} {
4633 lappend ret "$ee*"
4634 } else {
4635 lappend ret $ee
4636 lappend ret "$ee/*"
4637 }
4638 }
4639 return $ret
4640}
4641
908c3585 4642proc do_file_hl {serial} {
4e7d6779 4643 global highlight_files filehighlight highlight_paths gdttype fhl_list
de665fd3 4644 global cdup findtype
908c3585 4645
b007ee20 4646 if {$gdttype eq [mc "touching paths:"]} {
de665fd3
YK
4647 # If "exact" match then convert backslashes to forward slashes.
4648 # Most useful to support Windows-flavoured file paths.
4649 if {$findtype eq [mc "Exact"]} {
4650 set highlight_files [string map {"\\" "/"} $highlight_files]
4651 }
60f7a7dc
PM
4652 if {[catch {set paths [shellsplit $highlight_files]}]} return
4653 set highlight_paths [makepatterns $paths]
4654 highlight_filelist
c332f445
MZ
4655 set relative_paths {}
4656 foreach path $paths {
4657 lappend relative_paths [file join $cdup $path]
4658 }
4659 set gdtargs [concat -- $relative_paths]
b007ee20 4660 } elseif {$gdttype eq [mc "adding/removing string:"]} {
60f7a7dc 4661 set gdtargs [list "-S$highlight_files"]
c33cb908
ML
4662 } elseif {$gdttype eq [mc "changing lines matching:"]} {
4663 set gdtargs [list "-G$highlight_files"]
687c8765
PM
4664 } else {
4665 # must be "containing:", i.e. we're searching commit info
4666 return
60f7a7dc 4667 }
1ce09dd6 4668 set cmd [concat | git diff-tree -r -s --stdin $gdtargs]
908c3585
PM
4669 set filehighlight [open $cmd r+]
4670 fconfigure $filehighlight -blocking 0
7eb3cb9c 4671 filerun $filehighlight readfhighlight
4e7d6779 4672 set fhl_list {}
908c3585
PM
4673 drawvisible
4674 flushhighlights
4675}
4676
4677proc flushhighlights {} {
4e7d6779 4678 global filehighlight fhl_list
908c3585
PM
4679
4680 if {[info exists filehighlight]} {
4e7d6779 4681 lappend fhl_list {}
908c3585
PM
4682 puts $filehighlight ""
4683 flush $filehighlight
4684 }
4685}
4686
4687proc askfilehighlight {row id} {
4e7d6779 4688 global filehighlight fhighlights fhl_list
908c3585 4689
4e7d6779 4690 lappend fhl_list $id
476ca63d 4691 set fhighlights($id) -1
908c3585
PM
4692 puts $filehighlight $id
4693}
4694
4695proc readfhighlight {} {
7fcc92bf 4696 global filehighlight fhighlights curview iddrawn
687c8765 4697 global fhl_list find_dirn
4e7d6779 4698
7eb3cb9c
PM
4699 if {![info exists filehighlight]} {
4700 return 0
4701 }
4702 set nr 0
4703 while {[incr nr] <= 100 && [gets $filehighlight line] >= 0} {
4e7d6779
PM
4704 set line [string trim $line]
4705 set i [lsearch -exact $fhl_list $line]
4706 if {$i < 0} continue
4707 for {set j 0} {$j < $i} {incr j} {
4708 set id [lindex $fhl_list $j]
476ca63d 4709 set fhighlights($id) 0
908c3585 4710 }
4e7d6779
PM
4711 set fhl_list [lrange $fhl_list [expr {$i+1}] end]
4712 if {$line eq {}} continue
7fcc92bf 4713 if {![commitinview $line $curview]} continue
476ca63d 4714 if {[info exists iddrawn($line)] && ![ishighlighted $line]} {
28593d3f 4715 bolden $line mainfontbold
4e7d6779 4716 }
476ca63d 4717 set fhighlights($line) 1
908c3585 4718 }
4e7d6779
PM
4719 if {[eof $filehighlight]} {
4720 # strange...
1ce09dd6 4721 puts "oops, git diff-tree died"
4e7d6779
PM
4722 catch {close $filehighlight}
4723 unset filehighlight
7eb3cb9c 4724 return 0
908c3585 4725 }
687c8765 4726 if {[info exists find_dirn]} {
cca5d946 4727 run findmore
908c3585 4728 }
687c8765 4729 return 1
908c3585
PM
4730}
4731
4fb0fa19 4732proc doesmatch {f} {
687c8765 4733 global findtype findpattern
4fb0fa19 4734
b007ee20 4735 if {$findtype eq [mc "Regexp"]} {
687c8765 4736 return [regexp $findpattern $f]
b007ee20 4737 } elseif {$findtype eq [mc "IgnCase"]} {
4fb0fa19
PM
4738 return [string match -nocase $findpattern $f]
4739 } else {
4740 return [string match $findpattern $f]
4741 }
4742}
4743
60f7a7dc 4744proc askfindhighlight {row id} {
9c311b32 4745 global nhighlights commitinfo iddrawn
4fb0fa19
PM
4746 global findloc
4747 global markingmatches
908c3585
PM
4748
4749 if {![info exists commitinfo($id)]} {
4750 getcommit $id
4751 }
60f7a7dc 4752 set info $commitinfo($id)
908c3585 4753 set isbold 0
585c27cb 4754 set fldtypes [list [mc Headline] [mc Author] "" [mc Committer] "" [mc Comments]]
60f7a7dc 4755 foreach f $info ty $fldtypes {
585c27cb 4756 if {$ty eq ""} continue
b007ee20 4757 if {($findloc eq [mc "All fields"] || $findloc eq $ty) &&
4fb0fa19 4758 [doesmatch $f]} {
b007ee20 4759 if {$ty eq [mc "Author"]} {
60f7a7dc 4760 set isbold 2
4fb0fa19 4761 break
60f7a7dc 4762 }
4fb0fa19 4763 set isbold 1
908c3585
PM
4764 }
4765 }
4fb0fa19 4766 if {$isbold && [info exists iddrawn($id)]} {
476ca63d 4767 if {![ishighlighted $id]} {
28593d3f 4768 bolden $id mainfontbold
4fb0fa19 4769 if {$isbold > 1} {
28593d3f 4770 bolden_name $id mainfontbold
4fb0fa19 4771 }
908c3585 4772 }
4fb0fa19 4773 if {$markingmatches} {
005a2f4e 4774 markrowmatches $row $id
908c3585
PM
4775 }
4776 }
476ca63d 4777 set nhighlights($id) $isbold
da7c24dd
PM
4778}
4779
005a2f4e
PM
4780proc markrowmatches {row id} {
4781 global canv canv2 linehtag linentag commitinfo findloc
4fb0fa19 4782
005a2f4e
PM
4783 set headline [lindex $commitinfo($id) 0]
4784 set author [lindex $commitinfo($id) 1]
4fb0fa19
PM
4785 $canv delete match$row
4786 $canv2 delete match$row
b007ee20 4787 if {$findloc eq [mc "All fields"] || $findloc eq [mc "Headline"]} {
005a2f4e
PM
4788 set m [findmatches $headline]
4789 if {$m ne {}} {
28593d3f
PM
4790 markmatches $canv $row $headline $linehtag($id) $m \
4791 [$canv itemcget $linehtag($id) -font] $row
005a2f4e 4792 }
4fb0fa19 4793 }
b007ee20 4794 if {$findloc eq [mc "All fields"] || $findloc eq [mc "Author"]} {
005a2f4e
PM
4795 set m [findmatches $author]
4796 if {$m ne {}} {
28593d3f
PM
4797 markmatches $canv2 $row $author $linentag($id) $m \
4798 [$canv2 itemcget $linentag($id) -font] $row
005a2f4e 4799 }
4fb0fa19
PM
4800 }
4801}
4802
164ff275
PM
4803proc vrel_change {name ix op} {
4804 global highlight_related
4805
4806 rhighlight_none
b007ee20 4807 if {$highlight_related ne [mc "None"]} {
7eb3cb9c 4808 run drawvisible
164ff275
PM
4809 }
4810}
4811
4812# prepare for testing whether commits are descendents or ancestors of a
4813proc rhighlight_sel {a} {
4814 global descendent desc_todo ancestor anc_todo
476ca63d 4815 global highlight_related
164ff275
PM
4816
4817 catch {unset descendent}
4818 set desc_todo [list $a]
4819 catch {unset ancestor}
4820 set anc_todo [list $a]
b007ee20 4821 if {$highlight_related ne [mc "None"]} {
164ff275 4822 rhighlight_none
7eb3cb9c 4823 run drawvisible
164ff275
PM
4824 }
4825}
4826
4827proc rhighlight_none {} {
4828 global rhighlights
4829
4e7d6779
PM
4830 catch {unset rhighlights}
4831 unbolden
164ff275
PM
4832}
4833
4834proc is_descendent {a} {
7fcc92bf 4835 global curview children descendent desc_todo
164ff275
PM
4836
4837 set v $curview
7fcc92bf 4838 set la [rowofcommit $a]
164ff275
PM
4839 set todo $desc_todo
4840 set leftover {}
4841 set done 0
4842 for {set i 0} {$i < [llength $todo]} {incr i} {
4843 set do [lindex $todo $i]
7fcc92bf 4844 if {[rowofcommit $do] < $la} {
164ff275
PM
4845 lappend leftover $do
4846 continue
4847 }
4848 foreach nk $children($v,$do) {
4849 if {![info exists descendent($nk)]} {
4850 set descendent($nk) 1
4851 lappend todo $nk
4852 if {$nk eq $a} {
4853 set done 1
4854 }
4855 }
4856 }
4857 if {$done} {
4858 set desc_todo [concat $leftover [lrange $todo [expr {$i+1}] end]]
4859 return
4860 }
4861 }
4862 set descendent($a) 0
4863 set desc_todo $leftover
4864}
4865
4866proc is_ancestor {a} {
7fcc92bf 4867 global curview parents ancestor anc_todo
164ff275
PM
4868
4869 set v $curview
7fcc92bf 4870 set la [rowofcommit $a]
164ff275
PM
4871 set todo $anc_todo
4872 set leftover {}
4873 set done 0
4874 for {set i 0} {$i < [llength $todo]} {incr i} {
4875 set do [lindex $todo $i]
7fcc92bf 4876 if {![commitinview $do $v] || [rowofcommit $do] > $la} {
164ff275
PM
4877 lappend leftover $do
4878 continue
4879 }
7fcc92bf 4880 foreach np $parents($v,$do) {
164ff275
PM
4881 if {![info exists ancestor($np)]} {
4882 set ancestor($np) 1
4883 lappend todo $np
4884 if {$np eq $a} {
4885 set done 1
4886 }
4887 }
4888 }
4889 if {$done} {
4890 set anc_todo [concat $leftover [lrange $todo [expr {$i+1}] end]]
4891 return
4892 }
4893 }
4894 set ancestor($a) 0
4895 set anc_todo $leftover
4896}
4897
4898proc askrelhighlight {row id} {
9c311b32 4899 global descendent highlight_related iddrawn rhighlights
164ff275
PM
4900 global selectedline ancestor
4901
94b4a69f 4902 if {$selectedline eq {}} return
164ff275 4903 set isbold 0
55e34436
CS
4904 if {$highlight_related eq [mc "Descendant"] ||
4905 $highlight_related eq [mc "Not descendant"]} {
164ff275
PM
4906 if {![info exists descendent($id)]} {
4907 is_descendent $id
4908 }
55e34436 4909 if {$descendent($id) == ($highlight_related eq [mc "Descendant"])} {
164ff275
PM
4910 set isbold 1
4911 }
b007ee20
CS
4912 } elseif {$highlight_related eq [mc "Ancestor"] ||
4913 $highlight_related eq [mc "Not ancestor"]} {
164ff275
PM
4914 if {![info exists ancestor($id)]} {
4915 is_ancestor $id
4916 }
b007ee20 4917 if {$ancestor($id) == ($highlight_related eq [mc "Ancestor"])} {
164ff275
PM
4918 set isbold 1
4919 }
4920 }
4921 if {[info exists iddrawn($id)]} {
476ca63d 4922 if {$isbold && ![ishighlighted $id]} {
28593d3f 4923 bolden $id mainfontbold
164ff275
PM
4924 }
4925 }
476ca63d 4926 set rhighlights($id) $isbold
164ff275
PM
4927}
4928
da7c24dd
PM
4929# Graph layout functions
4930
9f1afe05
PM
4931proc shortids {ids} {
4932 set res {}
4933 foreach id $ids {
4934 if {[llength $id] > 1} {
4935 lappend res [shortids $id]
4936 } elseif {[regexp {^[0-9a-f]{40}$} $id]} {
4937 lappend res [string range $id 0 7]
4938 } else {
4939 lappend res $id
4940 }
4941 }
4942 return $res
4943}
4944
9f1afe05
PM
4945proc ntimes {n o} {
4946 set ret {}
0380081c
PM
4947 set o [list $o]
4948 for {set mask 1} {$mask <= $n} {incr mask $mask} {
4949 if {($n & $mask) != 0} {
4950 set ret [concat $ret $o]
9f1afe05 4951 }
0380081c 4952 set o [concat $o $o]
9f1afe05 4953 }
0380081c 4954 return $ret
9f1afe05
PM
4955}
4956
9257d8f7
PM
4957proc ordertoken {id} {
4958 global ordertok curview varcid varcstart varctok curview parents children
4959 global nullid nullid2
4960
4961 if {[info exists ordertok($id)]} {
4962 return $ordertok($id)
4963 }
4964 set origid $id
4965 set todo {}
4966 while {1} {
4967 if {[info exists varcid($curview,$id)]} {
4968 set a $varcid($curview,$id)
4969 set p [lindex $varcstart($curview) $a]
4970 } else {
4971 set p [lindex $children($curview,$id) 0]
4972 }
4973 if {[info exists ordertok($p)]} {
4974 set tok $ordertok($p)
4975 break
4976 }
c8c9f3d9
PM
4977 set id [first_real_child $curview,$p]
4978 if {$id eq {}} {
9257d8f7 4979 # it's a root
46308ea1 4980 set tok [lindex $varctok($curview) $varcid($curview,$p)]
9257d8f7
PM
4981 break
4982 }
9257d8f7
PM
4983 if {[llength $parents($curview,$id)] == 1} {
4984 lappend todo [list $p {}]
4985 } else {
4986 set j [lsearch -exact $parents($curview,$id) $p]
4987 if {$j < 0} {
4988 puts "oops didn't find [shortids $p] in parents of [shortids $id]"
4989 }
4990 lappend todo [list $p [strrep $j]]
4991 }
4992 }
4993 for {set i [llength $todo]} {[incr i -1] >= 0} {} {
4994 set p [lindex $todo $i 0]
4995 append tok [lindex $todo $i 1]
4996 set ordertok($p) $tok
4997 }
4998 set ordertok($origid) $tok
4999 return $tok
5000}
5001
6e8c8707
PM
5002# Work out where id should go in idlist so that order-token
5003# values increase from left to right
5004proc idcol {idlist id {i 0}} {
9257d8f7 5005 set t [ordertoken $id]
e5b37ac1
PM
5006 if {$i < 0} {
5007 set i 0
5008 }
9257d8f7 5009 if {$i >= [llength $idlist] || $t < [ordertoken [lindex $idlist $i]]} {
6e8c8707
PM
5010 if {$i > [llength $idlist]} {
5011 set i [llength $idlist]
9f1afe05 5012 }
9257d8f7 5013 while {[incr i -1] >= 0 && $t < [ordertoken [lindex $idlist $i]]} {}
6e8c8707
PM
5014 incr i
5015 } else {
9257d8f7 5016 if {$t > [ordertoken [lindex $idlist $i]]} {
6e8c8707 5017 while {[incr i] < [llength $idlist] &&
9257d8f7 5018 $t >= [ordertoken [lindex $idlist $i]]} {}
9f1afe05 5019 }
9f1afe05 5020 }
6e8c8707 5021 return $i
9f1afe05
PM
5022}
5023
5024proc initlayout {} {
7fcc92bf 5025 global rowidlist rowisopt rowfinal displayorder parentlist
da7c24dd 5026 global numcommits canvxmax canv
8f7d0cec 5027 global nextcolor
da7c24dd 5028 global colormap rowtextx
9f1afe05 5029
8f7d0cec
PM
5030 set numcommits 0
5031 set displayorder {}
79b2c75e 5032 set parentlist {}
8f7d0cec 5033 set nextcolor 0
0380081c
PM
5034 set rowidlist {}
5035 set rowisopt {}
f5f3c2e2 5036 set rowfinal {}
be0cd098 5037 set canvxmax [$canv cget -width]
50b44ece
PM
5038 catch {unset colormap}
5039 catch {unset rowtextx}
ac1276ab 5040 setcanvscroll
be0cd098
PM
5041}
5042
5043proc setcanvscroll {} {
5044 global canv canv2 canv3 numcommits linespc canvxmax canvy0
ac1276ab 5045 global lastscrollset lastscrollrows
be0cd098
PM
5046
5047 set ymax [expr {$canvy0 + ($numcommits - 0.5) * $linespc + 2}]
5048 $canv conf -scrollregion [list 0 0 $canvxmax $ymax]
5049 $canv2 conf -scrollregion [list 0 0 0 $ymax]
5050 $canv3 conf -scrollregion [list 0 0 0 $ymax]
ac1276ab
PM
5051 set lastscrollset [clock clicks -milliseconds]
5052 set lastscrollrows $numcommits
9f1afe05
PM
5053}
5054
5055proc visiblerows {} {
5056 global canv numcommits linespc
5057
5058 set ymax [lindex [$canv cget -scrollregion] 3]
5059 if {$ymax eq {} || $ymax == 0} return
5060 set f [$canv yview]
5061 set y0 [expr {int([lindex $f 0] * $ymax)}]
5062 set r0 [expr {int(($y0 - 3) / $linespc) - 1}]
5063 if {$r0 < 0} {
5064 set r0 0
5065 }
5066 set y1 [expr {int([lindex $f 1] * $ymax)}]
5067 set r1 [expr {int(($y1 - 3) / $linespc) + 1}]
5068 if {$r1 >= $numcommits} {
5069 set r1 [expr {$numcommits - 1}]
5070 }
5071 return [list $r0 $r1]
5072}
5073
f5f3c2e2 5074proc layoutmore {} {
38dfe939 5075 global commitidx viewcomplete curview
94b4a69f 5076 global numcommits pending_select curview
d375ef9b 5077 global lastscrollset lastscrollrows
ac1276ab
PM
5078
5079 if {$lastscrollrows < 100 || $viewcomplete($curview) ||
5080 [clock clicks -milliseconds] - $lastscrollset > 500} {
a2c22362
PM
5081 setcanvscroll
5082 }
d94f8cd6 5083 if {[info exists pending_select] &&
7fcc92bf 5084 [commitinview $pending_select $curview]} {
567c34e0 5085 update
7fcc92bf 5086 selectline [rowofcommit $pending_select] 1
d94f8cd6 5087 }
ac1276ab 5088 drawvisible
219ea3a9
PM
5089}
5090
cdc8429c
PM
5091# With path limiting, we mightn't get the actual HEAD commit,
5092# so ask git rev-list what is the first ancestor of HEAD that
5093# touches a file in the path limit.
5094proc get_viewmainhead {view} {
5095 global viewmainheadid vfilelimit viewinstances mainheadid
5096
5097 catch {
5098 set rfd [open [concat | git rev-list -1 $mainheadid \
5099 -- $vfilelimit($view)] r]
5100 set j [reg_instance $rfd]
5101 lappend viewinstances($view) $j
5102 fconfigure $rfd -blocking 0
5103 filerun $rfd [list getviewhead $rfd $j $view]
5104 set viewmainheadid($curview) {}
5105 }
5106}
5107
5108# git rev-list should give us just 1 line to use as viewmainheadid($view)
5109proc getviewhead {fd inst view} {
5110 global viewmainheadid commfd curview viewinstances showlocalchanges
5111
5112 set id {}
5113 if {[gets $fd line] < 0} {
5114 if {![eof $fd]} {
5115 return 1
5116 }
5117 } elseif {[string length $line] == 40 && [string is xdigit $line]} {
5118 set id $line
5119 }
5120 set viewmainheadid($view) $id
5121 close $fd
5122 unset commfd($inst)
5123 set i [lsearch -exact $viewinstances($view) $inst]
5124 if {$i >= 0} {
5125 set viewinstances($view) [lreplace $viewinstances($view) $i $i]
5126 }
5127 if {$showlocalchanges && $id ne {} && $view == $curview} {
5128 doshowlocalchanges
5129 }
5130 return 0
5131}
5132
219ea3a9 5133proc doshowlocalchanges {} {
cdc8429c 5134 global curview viewmainheadid
219ea3a9 5135
cdc8429c
PM
5136 if {$viewmainheadid($curview) eq {}} return
5137 if {[commitinview $viewmainheadid($curview) $curview]} {
219ea3a9 5138 dodiffindex
38dfe939 5139 } else {
cdc8429c 5140 interestedin $viewmainheadid($curview) dodiffindex
219ea3a9
PM
5141 }
5142}
5143
5144proc dohidelocalchanges {} {
7fcc92bf 5145 global nullid nullid2 lserial curview
219ea3a9 5146
7fcc92bf 5147 if {[commitinview $nullid $curview]} {
b8a938cf 5148 removefakerow $nullid
8f489363 5149 }
7fcc92bf 5150 if {[commitinview $nullid2 $curview]} {
b8a938cf 5151 removefakerow $nullid2
219ea3a9
PM
5152 }
5153 incr lserial
5154}
5155
8f489363 5156# spawn off a process to do git diff-index --cached HEAD
219ea3a9 5157proc dodiffindex {} {
cdc8429c 5158 global lserial showlocalchanges vfilelimit curview
74cb884f 5159 global hasworktree
219ea3a9 5160
74cb884f 5161 if {!$showlocalchanges || !$hasworktree} return
219ea3a9 5162 incr lserial
cdc8429c
PM
5163 set cmd "|git diff-index --cached HEAD"
5164 if {$vfilelimit($curview) ne {}} {
5165 set cmd [concat $cmd -- $vfilelimit($curview)]
5166 }
5167 set fd [open $cmd r]
219ea3a9 5168 fconfigure $fd -blocking 0
e439e092
AG
5169 set i [reg_instance $fd]
5170 filerun $fd [list readdiffindex $fd $lserial $i]
219ea3a9
PM
5171}
5172
e439e092 5173proc readdiffindex {fd serial inst} {
cdc8429c
PM
5174 global viewmainheadid nullid nullid2 curview commitinfo commitdata lserial
5175 global vfilelimit
219ea3a9 5176
8f489363 5177 set isdiff 1
219ea3a9 5178 if {[gets $fd line] < 0} {
8f489363
PM
5179 if {![eof $fd]} {
5180 return 1
219ea3a9 5181 }
8f489363 5182 set isdiff 0
219ea3a9
PM
5183 }
5184 # we only need to see one line and we don't really care what it says...
e439e092 5185 stop_instance $inst
219ea3a9 5186
24f7a667
PM
5187 if {$serial != $lserial} {
5188 return 0
8f489363
PM
5189 }
5190
24f7a667 5191 # now see if there are any local changes not checked in to the index
cdc8429c
PM
5192 set cmd "|git diff-files"
5193 if {$vfilelimit($curview) ne {}} {
5194 set cmd [concat $cmd -- $vfilelimit($curview)]
5195 }
5196 set fd [open $cmd r]
24f7a667 5197 fconfigure $fd -blocking 0
e439e092
AG
5198 set i [reg_instance $fd]
5199 filerun $fd [list readdifffiles $fd $serial $i]
24f7a667
PM
5200
5201 if {$isdiff && ![commitinview $nullid2 $curview]} {
8f489363 5202 # add the line for the changes in the index to the graph
d990cedf 5203 set hl [mc "Local changes checked in to index but not committed"]
8f489363
PM
5204 set commitinfo($nullid2) [list $hl {} {} {} {} " $hl\n"]
5205 set commitdata($nullid2) "\n $hl\n"
fc2a256f 5206 if {[commitinview $nullid $curview]} {
b8a938cf 5207 removefakerow $nullid
fc2a256f 5208 }
cdc8429c 5209 insertfakerow $nullid2 $viewmainheadid($curview)
24f7a667 5210 } elseif {!$isdiff && [commitinview $nullid2 $curview]} {
cdc8429c
PM
5211 if {[commitinview $nullid $curview]} {
5212 removefakerow $nullid
5213 }
b8a938cf 5214 removefakerow $nullid2
8f489363
PM
5215 }
5216 return 0
5217}
5218
e439e092 5219proc readdifffiles {fd serial inst} {
cdc8429c 5220 global viewmainheadid nullid nullid2 curview
8f489363
PM
5221 global commitinfo commitdata lserial
5222
5223 set isdiff 1
5224 if {[gets $fd line] < 0} {
5225 if {![eof $fd]} {
5226 return 1
5227 }
5228 set isdiff 0
5229 }
5230 # we only need to see one line and we don't really care what it says...
e439e092 5231 stop_instance $inst
8f489363 5232
24f7a667
PM
5233 if {$serial != $lserial} {
5234 return 0
5235 }
5236
5237 if {$isdiff && ![commitinview $nullid $curview]} {
219ea3a9 5238 # add the line for the local diff to the graph
d990cedf 5239 set hl [mc "Local uncommitted changes, not checked in to index"]
219ea3a9
PM
5240 set commitinfo($nullid) [list $hl {} {} {} {} " $hl\n"]
5241 set commitdata($nullid) "\n $hl\n"
7fcc92bf
PM
5242 if {[commitinview $nullid2 $curview]} {
5243 set p $nullid2
5244 } else {
cdc8429c 5245 set p $viewmainheadid($curview)
7fcc92bf 5246 }
b8a938cf 5247 insertfakerow $nullid $p
24f7a667 5248 } elseif {!$isdiff && [commitinview $nullid $curview]} {
b8a938cf 5249 removefakerow $nullid
219ea3a9
PM
5250 }
5251 return 0
9f1afe05
PM
5252}
5253
8f0bc7e9 5254proc nextuse {id row} {
7fcc92bf 5255 global curview children
9f1afe05 5256
8f0bc7e9
PM
5257 if {[info exists children($curview,$id)]} {
5258 foreach kid $children($curview,$id) {
7fcc92bf 5259 if {![commitinview $kid $curview]} {
0380081c
PM
5260 return -1
5261 }
7fcc92bf
PM
5262 if {[rowofcommit $kid] > $row} {
5263 return [rowofcommit $kid]
9f1afe05 5264 }
9f1afe05 5265 }
8f0bc7e9 5266 }
7fcc92bf
PM
5267 if {[commitinview $id $curview]} {
5268 return [rowofcommit $id]
8f0bc7e9
PM
5269 }
5270 return -1
5271}
5272
f5f3c2e2 5273proc prevuse {id row} {
7fcc92bf 5274 global curview children
f5f3c2e2
PM
5275
5276 set ret -1
5277 if {[info exists children($curview,$id)]} {
5278 foreach kid $children($curview,$id) {
7fcc92bf
PM
5279 if {![commitinview $kid $curview]} break
5280 if {[rowofcommit $kid] < $row} {
5281 set ret [rowofcommit $kid]
7b459a1c 5282 }
7b459a1c 5283 }
f5f3c2e2
PM
5284 }
5285 return $ret
5286}
5287
0380081c
PM
5288proc make_idlist {row} {
5289 global displayorder parentlist uparrowlen downarrowlen mingaplen
9257d8f7 5290 global commitidx curview children
9f1afe05 5291
0380081c
PM
5292 set r [expr {$row - $mingaplen - $downarrowlen - 1}]
5293 if {$r < 0} {
5294 set r 0
8f0bc7e9 5295 }
0380081c
PM
5296 set ra [expr {$row - $downarrowlen}]
5297 if {$ra < 0} {
5298 set ra 0
5299 }
5300 set rb [expr {$row + $uparrowlen}]
5301 if {$rb > $commitidx($curview)} {
5302 set rb $commitidx($curview)
5303 }
7fcc92bf 5304 make_disporder $r [expr {$rb + 1}]
0380081c
PM
5305 set ids {}
5306 for {} {$r < $ra} {incr r} {
5307 set nextid [lindex $displayorder [expr {$r + 1}]]
5308 foreach p [lindex $parentlist $r] {
5309 if {$p eq $nextid} continue
5310 set rn [nextuse $p $r]
5311 if {$rn >= $row &&
5312 $rn <= $r + $downarrowlen + $mingaplen + $uparrowlen} {
9257d8f7 5313 lappend ids [list [ordertoken $p] $p]
9f1afe05 5314 }
9f1afe05 5315 }
0380081c
PM
5316 }
5317 for {} {$r < $row} {incr r} {
5318 set nextid [lindex $displayorder [expr {$r + 1}]]
5319 foreach p [lindex $parentlist $r] {
5320 if {$p eq $nextid} continue
5321 set rn [nextuse $p $r]
5322 if {$rn < 0 || $rn >= $row} {
9257d8f7 5323 lappend ids [list [ordertoken $p] $p]
9f1afe05 5324 }
9f1afe05 5325 }
0380081c
PM
5326 }
5327 set id [lindex $displayorder $row]
9257d8f7 5328 lappend ids [list [ordertoken $id] $id]
0380081c
PM
5329 while {$r < $rb} {
5330 foreach p [lindex $parentlist $r] {
5331 set firstkid [lindex $children($curview,$p) 0]
7fcc92bf 5332 if {[rowofcommit $firstkid] < $row} {
9257d8f7 5333 lappend ids [list [ordertoken $p] $p]
9f1afe05 5334 }
9f1afe05 5335 }
0380081c
PM
5336 incr r
5337 set id [lindex $displayorder $r]
5338 if {$id ne {}} {
5339 set firstkid [lindex $children($curview,$id) 0]
7fcc92bf 5340 if {$firstkid ne {} && [rowofcommit $firstkid] < $row} {
9257d8f7 5341 lappend ids [list [ordertoken $id] $id]
0380081c 5342 }
9f1afe05 5343 }
9f1afe05 5344 }
0380081c
PM
5345 set idlist {}
5346 foreach idx [lsort -unique $ids] {
5347 lappend idlist [lindex $idx 1]
5348 }
5349 return $idlist
9f1afe05
PM
5350}
5351
f5f3c2e2
PM
5352proc rowsequal {a b} {
5353 while {[set i [lsearch -exact $a {}]] >= 0} {
5354 set a [lreplace $a $i $i]
5355 }
5356 while {[set i [lsearch -exact $b {}]] >= 0} {
5357 set b [lreplace $b $i $i]
5358 }
5359 return [expr {$a eq $b}]
9f1afe05
PM
5360}
5361
f5f3c2e2
PM
5362proc makeupline {id row rend col} {
5363 global rowidlist uparrowlen downarrowlen mingaplen
9f1afe05 5364
f5f3c2e2
PM
5365 for {set r $rend} {1} {set r $rstart} {
5366 set rstart [prevuse $id $r]
5367 if {$rstart < 0} return
5368 if {$rstart < $row} break
5369 }
5370 if {$rstart + $uparrowlen + $mingaplen + $downarrowlen < $rend} {
5371 set rstart [expr {$rend - $uparrowlen - 1}]
79b2c75e 5372 }
f5f3c2e2
PM
5373 for {set r $rstart} {[incr r] <= $row} {} {
5374 set idlist [lindex $rowidlist $r]
5375 if {$idlist ne {} && [lsearch -exact $idlist $id] < 0} {
5376 set col [idcol $idlist $id $col]
5377 lset rowidlist $r [linsert $idlist $col $id]
5378 changedrow $r
5379 }
9f1afe05
PM
5380 }
5381}
5382
0380081c 5383proc layoutrows {row endrow} {
f5f3c2e2 5384 global rowidlist rowisopt rowfinal displayorder
0380081c
PM
5385 global uparrowlen downarrowlen maxwidth mingaplen
5386 global children parentlist
7fcc92bf 5387 global commitidx viewcomplete curview
9f1afe05 5388
7fcc92bf 5389 make_disporder [expr {$row - 1}] [expr {$endrow + $uparrowlen}]
0380081c
PM
5390 set idlist {}
5391 if {$row > 0} {
f56782ae
PM
5392 set rm1 [expr {$row - 1}]
5393 foreach id [lindex $rowidlist $rm1] {
0380081c
PM
5394 if {$id ne {}} {
5395 lappend idlist $id
5396 }
5397 }
f56782ae 5398 set final [lindex $rowfinal $rm1]
79b2c75e 5399 }
0380081c
PM
5400 for {} {$row < $endrow} {incr row} {
5401 set rm1 [expr {$row - 1}]
f56782ae 5402 if {$rm1 < 0 || $idlist eq {}} {
0380081c 5403 set idlist [make_idlist $row]
f5f3c2e2 5404 set final 1
0380081c
PM
5405 } else {
5406 set id [lindex $displayorder $rm1]
5407 set col [lsearch -exact $idlist $id]
5408 set idlist [lreplace $idlist $col $col]
5409 foreach p [lindex $parentlist $rm1] {
5410 if {[lsearch -exact $idlist $p] < 0} {
5411 set col [idcol $idlist $p $col]
5412 set idlist [linsert $idlist $col $p]
f5f3c2e2
PM
5413 # if not the first child, we have to insert a line going up
5414 if {$id ne [lindex $children($curview,$p) 0]} {
5415 makeupline $p $rm1 $row $col
5416 }
0380081c
PM
5417 }
5418 }
5419 set id [lindex $displayorder $row]
5420 if {$row > $downarrowlen} {
5421 set termrow [expr {$row - $downarrowlen - 1}]
5422 foreach p [lindex $parentlist $termrow] {
5423 set i [lsearch -exact $idlist $p]
5424 if {$i < 0} continue
5425 set nr [nextuse $p $termrow]
5426 if {$nr < 0 || $nr >= $row + $mingaplen + $uparrowlen} {
5427 set idlist [lreplace $idlist $i $i]
5428 }
5429 }
5430 }
5431 set col [lsearch -exact $idlist $id]
5432 if {$col < 0} {
5433 set col [idcol $idlist $id]
5434 set idlist [linsert $idlist $col $id]
f5f3c2e2
PM
5435 if {$children($curview,$id) ne {}} {
5436 makeupline $id $rm1 $row $col
5437 }
0380081c
PM
5438 }
5439 set r [expr {$row + $uparrowlen - 1}]
5440 if {$r < $commitidx($curview)} {
5441 set x $col
5442 foreach p [lindex $parentlist $r] {
5443 if {[lsearch -exact $idlist $p] >= 0} continue
5444 set fk [lindex $children($curview,$p) 0]
7fcc92bf 5445 if {[rowofcommit $fk] < $row} {
0380081c
PM
5446 set x [idcol $idlist $p $x]
5447 set idlist [linsert $idlist $x $p]
5448 }
5449 }
5450 if {[incr r] < $commitidx($curview)} {
5451 set p [lindex $displayorder $r]
5452 if {[lsearch -exact $idlist $p] < 0} {
5453 set fk [lindex $children($curview,$p) 0]
7fcc92bf 5454 if {$fk ne {} && [rowofcommit $fk] < $row} {
0380081c
PM
5455 set x [idcol $idlist $p $x]
5456 set idlist [linsert $idlist $x $p]
5457 }
5458 }
5459 }
5460 }
5461 }
f5f3c2e2
PM
5462 if {$final && !$viewcomplete($curview) &&
5463 $row + $uparrowlen + $mingaplen + $downarrowlen
5464 >= $commitidx($curview)} {
5465 set final 0
5466 }
0380081c
PM
5467 set l [llength $rowidlist]
5468 if {$row == $l} {
5469 lappend rowidlist $idlist
5470 lappend rowisopt 0
f5f3c2e2 5471 lappend rowfinal $final
0380081c 5472 } elseif {$row < $l} {
f5f3c2e2 5473 if {![rowsequal $idlist [lindex $rowidlist $row]]} {
0380081c
PM
5474 lset rowidlist $row $idlist
5475 changedrow $row
5476 }
f56782ae 5477 lset rowfinal $row $final
0380081c 5478 } else {
f5f3c2e2
PM
5479 set pad [ntimes [expr {$row - $l}] {}]
5480 set rowidlist [concat $rowidlist $pad]
0380081c 5481 lappend rowidlist $idlist
f5f3c2e2
PM
5482 set rowfinal [concat $rowfinal $pad]
5483 lappend rowfinal $final
0380081c
PM
5484 set rowisopt [concat $rowisopt [ntimes [expr {$row - $l + 1}] 0]]
5485 }
9f1afe05 5486 }
0380081c 5487 return $row
9f1afe05
PM
5488}
5489
0380081c
PM
5490proc changedrow {row} {
5491 global displayorder iddrawn rowisopt need_redisplay
9f1afe05 5492
0380081c
PM
5493 set l [llength $rowisopt]
5494 if {$row < $l} {
5495 lset rowisopt $row 0
5496 if {$row + 1 < $l} {
5497 lset rowisopt [expr {$row + 1}] 0
5498 if {$row + 2 < $l} {
5499 lset rowisopt [expr {$row + 2}] 0
5500 }
5501 }
5502 }
5503 set id [lindex $displayorder $row]
5504 if {[info exists iddrawn($id)]} {
5505 set need_redisplay 1
9f1afe05
PM
5506 }
5507}
5508
5509proc insert_pad {row col npad} {
6e8c8707 5510 global rowidlist
9f1afe05
PM
5511
5512 set pad [ntimes $npad {}]
e341c06d
PM
5513 set idlist [lindex $rowidlist $row]
5514 set bef [lrange $idlist 0 [expr {$col - 1}]]
5515 set aft [lrange $idlist $col end]
5516 set i [lsearch -exact $aft {}]
5517 if {$i > 0} {
5518 set aft [lreplace $aft $i $i]
5519 }
5520 lset rowidlist $row [concat $bef $pad $aft]
0380081c 5521 changedrow $row
9f1afe05
PM
5522}
5523
5524proc optimize_rows {row col endrow} {
0380081c 5525 global rowidlist rowisopt displayorder curview children
9f1afe05 5526
6e8c8707
PM
5527 if {$row < 1} {
5528 set row 1
5529 }
0380081c
PM
5530 for {} {$row < $endrow} {incr row; set col 0} {
5531 if {[lindex $rowisopt $row]} continue
9f1afe05 5532 set haspad 0
6e8c8707
PM
5533 set y0 [expr {$row - 1}]
5534 set ym [expr {$row - 2}]
0380081c
PM
5535 set idlist [lindex $rowidlist $row]
5536 set previdlist [lindex $rowidlist $y0]
5537 if {$idlist eq {} || $previdlist eq {}} continue
5538 if {$ym >= 0} {
5539 set pprevidlist [lindex $rowidlist $ym]
5540 if {$pprevidlist eq {}} continue
5541 } else {
5542 set pprevidlist {}
5543 }
6e8c8707
PM
5544 set x0 -1
5545 set xm -1
5546 for {} {$col < [llength $idlist]} {incr col} {
5547 set id [lindex $idlist $col]
5548 if {[lindex $previdlist $col] eq $id} continue
5549 if {$id eq {}} {
9f1afe05
PM
5550 set haspad 1
5551 continue
5552 }
6e8c8707
PM
5553 set x0 [lsearch -exact $previdlist $id]
5554 if {$x0 < 0} continue
5555 set z [expr {$x0 - $col}]
9f1afe05 5556 set isarrow 0
6e8c8707
PM
5557 set z0 {}
5558 if {$ym >= 0} {
5559 set xm [lsearch -exact $pprevidlist $id]
5560 if {$xm >= 0} {
5561 set z0 [expr {$xm - $x0}]
5562 }
5563 }
9f1afe05 5564 if {$z0 eq {}} {
92ed666f
PM
5565 # if row y0 is the first child of $id then it's not an arrow
5566 if {[lindex $children($curview,$id) 0] ne
5567 [lindex $displayorder $y0]} {
9f1afe05
PM
5568 set isarrow 1
5569 }
5570 }
e341c06d
PM
5571 if {!$isarrow && $id ne [lindex $displayorder $row] &&
5572 [lsearch -exact [lindex $rowidlist [expr {$row+1}]] $id] < 0} {
5573 set isarrow 1
5574 }
3fc4279a
PM
5575 # Looking at lines from this row to the previous row,
5576 # make them go straight up if they end in an arrow on
5577 # the previous row; otherwise make them go straight up
5578 # or at 45 degrees.
9f1afe05 5579 if {$z < -1 || ($z < 0 && $isarrow)} {
3fc4279a
PM
5580 # Line currently goes left too much;
5581 # insert pads in the previous row, then optimize it
9f1afe05 5582 set npad [expr {-1 - $z + $isarrow}]
9f1afe05
PM
5583 insert_pad $y0 $x0 $npad
5584 if {$y0 > 0} {
5585 optimize_rows $y0 $x0 $row
5586 }
6e8c8707
PM
5587 set previdlist [lindex $rowidlist $y0]
5588 set x0 [lsearch -exact $previdlist $id]
5589 set z [expr {$x0 - $col}]
5590 if {$z0 ne {}} {
5591 set pprevidlist [lindex $rowidlist $ym]
5592 set xm [lsearch -exact $pprevidlist $id]
5593 set z0 [expr {$xm - $x0}]
5594 }
9f1afe05 5595 } elseif {$z > 1 || ($z > 0 && $isarrow)} {
3fc4279a 5596 # Line currently goes right too much;
6e8c8707 5597 # insert pads in this line
9f1afe05 5598 set npad [expr {$z - 1 + $isarrow}]
e341c06d
PM
5599 insert_pad $row $col $npad
5600 set idlist [lindex $rowidlist $row]
9f1afe05 5601 incr col $npad
6e8c8707 5602 set z [expr {$x0 - $col}]
9f1afe05
PM
5603 set haspad 1
5604 }
6e8c8707 5605 if {$z0 eq {} && !$isarrow && $ym >= 0} {
eb447a12 5606 # this line links to its first child on row $row-2
6e8c8707
PM
5607 set id [lindex $displayorder $ym]
5608 set xc [lsearch -exact $pprevidlist $id]
eb447a12
PM
5609 if {$xc >= 0} {
5610 set z0 [expr {$xc - $x0}]
5611 }
5612 }
3fc4279a 5613 # avoid lines jigging left then immediately right
9f1afe05
PM
5614 if {$z0 ne {} && $z < 0 && $z0 > 0} {
5615 insert_pad $y0 $x0 1
6e8c8707
PM
5616 incr x0
5617 optimize_rows $y0 $x0 $row
5618 set previdlist [lindex $rowidlist $y0]
9f1afe05
PM
5619 }
5620 }
5621 if {!$haspad} {
3fc4279a 5622 # Find the first column that doesn't have a line going right
9f1afe05 5623 for {set col [llength $idlist]} {[incr col -1] >= 0} {} {
6e8c8707
PM
5624 set id [lindex $idlist $col]
5625 if {$id eq {}} break
5626 set x0 [lsearch -exact $previdlist $id]
5627 if {$x0 < 0} {
eb447a12 5628 # check if this is the link to the first child
92ed666f
PM
5629 set kid [lindex $displayorder $y0]
5630 if {[lindex $children($curview,$id) 0] eq $kid} {
eb447a12 5631 # it is, work out offset to child
92ed666f 5632 set x0 [lsearch -exact $previdlist $kid]
eb447a12
PM
5633 }
5634 }
6e8c8707 5635 if {$x0 <= $col} break
9f1afe05 5636 }
3fc4279a 5637 # Insert a pad at that column as long as it has a line and
6e8c8707
PM
5638 # isn't the last column
5639 if {$x0 >= 0 && [incr col] < [llength $idlist]} {
9f1afe05 5640 set idlist [linsert $idlist $col {}]
0380081c
PM
5641 lset rowidlist $row $idlist
5642 changedrow $row
9f1afe05
PM
5643 }
5644 }
9f1afe05
PM
5645 }
5646}
5647
5648proc xc {row col} {
5649 global canvx0 linespc
5650 return [expr {$canvx0 + $col * $linespc}]
5651}
5652
5653proc yc {row} {
5654 global canvy0 linespc
5655 return [expr {$canvy0 + $row * $linespc}]
5656}
5657
c934a8a3
PM
5658proc linewidth {id} {
5659 global thickerline lthickness
5660
5661 set wid $lthickness
5662 if {[info exists thickerline] && $id eq $thickerline} {
5663 set wid [expr {2 * $lthickness}]
5664 }
5665 return $wid
5666}
5667
50b44ece 5668proc rowranges {id} {
7fcc92bf 5669 global curview children uparrowlen downarrowlen
92ed666f 5670 global rowidlist
50b44ece 5671
92ed666f
PM
5672 set kids $children($curview,$id)
5673 if {$kids eq {}} {
5674 return {}
66e46f37 5675 }
92ed666f
PM
5676 set ret {}
5677 lappend kids $id
5678 foreach child $kids {
7fcc92bf
PM
5679 if {![commitinview $child $curview]} break
5680 set row [rowofcommit $child]
92ed666f
PM
5681 if {![info exists prev]} {
5682 lappend ret [expr {$row + 1}]
322a8cc9 5683 } else {
92ed666f 5684 if {$row <= $prevrow} {
7fcc92bf 5685 puts "oops children of [shortids $id] out of order [shortids $child] $row <= [shortids $prev] $prevrow"
92ed666f
PM
5686 }
5687 # see if the line extends the whole way from prevrow to row
5688 if {$row > $prevrow + $uparrowlen + $downarrowlen &&
5689 [lsearch -exact [lindex $rowidlist \
5690 [expr {int(($row + $prevrow) / 2)}]] $id] < 0} {
5691 # it doesn't, see where it ends
5692 set r [expr {$prevrow + $downarrowlen}]
5693 if {[lsearch -exact [lindex $rowidlist $r] $id] < 0} {
5694 while {[incr r -1] > $prevrow &&
5695 [lsearch -exact [lindex $rowidlist $r] $id] < 0} {}
5696 } else {
5697 while {[incr r] <= $row &&
5698 [lsearch -exact [lindex $rowidlist $r] $id] >= 0} {}
5699 incr r -1
5700 }
5701 lappend ret $r
5702 # see where it starts up again
5703 set r [expr {$row - $uparrowlen}]
5704 if {[lsearch -exact [lindex $rowidlist $r] $id] < 0} {
5705 while {[incr r] < $row &&
5706 [lsearch -exact [lindex $rowidlist $r] $id] < 0} {}
5707 } else {
5708 while {[incr r -1] >= $prevrow &&
5709 [lsearch -exact [lindex $rowidlist $r] $id] >= 0} {}
5710 incr r
5711 }
5712 lappend ret $r
5713 }
5714 }
5715 if {$child eq $id} {
5716 lappend ret $row
322a8cc9 5717 }
7fcc92bf 5718 set prev $child
92ed666f 5719 set prevrow $row
9f1afe05 5720 }
92ed666f 5721 return $ret
322a8cc9
PM
5722}
5723
5724proc drawlineseg {id row endrow arrowlow} {
5725 global rowidlist displayorder iddrawn linesegs
e341c06d 5726 global canv colormap linespc curview maxlinelen parentlist
322a8cc9
PM
5727
5728 set cols [list [lsearch -exact [lindex $rowidlist $row] $id]]
5729 set le [expr {$row + 1}]
5730 set arrowhigh 1
9f1afe05 5731 while {1} {
322a8cc9
PM
5732 set c [lsearch -exact [lindex $rowidlist $le] $id]
5733 if {$c < 0} {
5734 incr le -1
5735 break
5736 }
5737 lappend cols $c
5738 set x [lindex $displayorder $le]
5739 if {$x eq $id} {
5740 set arrowhigh 0
5741 break
9f1afe05 5742 }
322a8cc9
PM
5743 if {[info exists iddrawn($x)] || $le == $endrow} {
5744 set c [lsearch -exact [lindex $rowidlist [expr {$le+1}]] $id]
5745 if {$c >= 0} {
5746 lappend cols $c
5747 set arrowhigh 0
5748 }
5749 break
5750 }
5751 incr le
9f1afe05 5752 }
322a8cc9
PM
5753 if {$le <= $row} {
5754 return $row
5755 }
5756
5757 set lines {}
5758 set i 0
5759 set joinhigh 0
5760 if {[info exists linesegs($id)]} {
5761 set lines $linesegs($id)
5762 foreach li $lines {
5763 set r0 [lindex $li 0]
5764 if {$r0 > $row} {
5765 if {$r0 == $le && [lindex $li 1] - $row <= $maxlinelen} {
5766 set joinhigh 1
5767 }
5768 break
5769 }
5770 incr i
5771 }
5772 }
5773 set joinlow 0
5774 if {$i > 0} {
5775 set li [lindex $lines [expr {$i-1}]]
5776 set r1 [lindex $li 1]
5777 if {$r1 == $row && $le - [lindex $li 0] <= $maxlinelen} {
5778 set joinlow 1
5779 }
5780 }
5781
5782 set x [lindex $cols [expr {$le - $row}]]
5783 set xp [lindex $cols [expr {$le - 1 - $row}]]
5784 set dir [expr {$xp - $x}]
5785 if {$joinhigh} {
5786 set ith [lindex $lines $i 2]
5787 set coords [$canv coords $ith]
5788 set ah [$canv itemcget $ith -arrow]
5789 set arrowhigh [expr {$ah eq "first" || $ah eq "both"}]
5790 set x2 [lindex $cols [expr {$le + 1 - $row}]]
5791 if {$x2 ne {} && $x - $x2 == $dir} {
5792 set coords [lrange $coords 0 end-2]
5793 }
5794 } else {
5795 set coords [list [xc $le $x] [yc $le]]
5796 }
5797 if {$joinlow} {
5798 set itl [lindex $lines [expr {$i-1}] 2]
5799 set al [$canv itemcget $itl -arrow]
5800 set arrowlow [expr {$al eq "last" || $al eq "both"}]
e341c06d
PM
5801 } elseif {$arrowlow} {
5802 if {[lsearch -exact [lindex $rowidlist [expr {$row-1}]] $id] >= 0 ||
5803 [lsearch -exact [lindex $parentlist [expr {$row-1}]] $id] >= 0} {
5804 set arrowlow 0
5805 }
322a8cc9
PM
5806 }
5807 set arrow [lindex {none first last both} [expr {$arrowhigh + 2*$arrowlow}]]
5808 for {set y $le} {[incr y -1] > $row} {} {
5809 set x $xp
5810 set xp [lindex $cols [expr {$y - 1 - $row}]]
5811 set ndir [expr {$xp - $x}]
5812 if {$dir != $ndir || $xp < 0} {
5813 lappend coords [xc $y $x] [yc $y]
5814 }
5815 set dir $ndir
5816 }
5817 if {!$joinlow} {
5818 if {$xp < 0} {
5819 # join parent line to first child
5820 set ch [lindex $displayorder $row]
5821 set xc [lsearch -exact [lindex $rowidlist $row] $ch]
5822 if {$xc < 0} {
5823 puts "oops: drawlineseg: child $ch not on row $row"
e341c06d
PM
5824 } elseif {$xc != $x} {
5825 if {($arrowhigh && $le == $row + 1) || $dir == 0} {
5826 set d [expr {int(0.5 * $linespc)}]
5827 set x1 [xc $row $x]
5828 if {$xc < $x} {
5829 set x2 [expr {$x1 - $d}]
5830 } else {
5831 set x2 [expr {$x1 + $d}]
5832 }
5833 set y2 [yc $row]
5834 set y1 [expr {$y2 + $d}]
5835 lappend coords $x1 $y1 $x2 $y2
5836 } elseif {$xc < $x - 1} {
322a8cc9
PM
5837 lappend coords [xc $row [expr {$x-1}]] [yc $row]
5838 } elseif {$xc > $x + 1} {
5839 lappend coords [xc $row [expr {$x+1}]] [yc $row]
5840 }
5841 set x $xc
eb447a12 5842 }
322a8cc9
PM
5843 lappend coords [xc $row $x] [yc $row]
5844 } else {
5845 set xn [xc $row $xp]
5846 set yn [yc $row]
e341c06d 5847 lappend coords $xn $yn
322a8cc9
PM
5848 }
5849 if {!$joinhigh} {
322a8cc9
PM
5850 assigncolor $id
5851 set t [$canv create line $coords -width [linewidth $id] \
5852 -fill $colormap($id) -tags lines.$id -arrow $arrow]
5853 $canv lower $t
5854 bindline $t $id
5855 set lines [linsert $lines $i [list $row $le $t]]
5856 } else {
5857 $canv coords $ith $coords
5858 if {$arrow ne $ah} {
5859 $canv itemconf $ith -arrow $arrow
5860 }
5861 lset lines $i 0 $row
5862 }
5863 } else {
5864 set xo [lsearch -exact [lindex $rowidlist [expr {$row - 1}]] $id]
5865 set ndir [expr {$xo - $xp}]
5866 set clow [$canv coords $itl]
5867 if {$dir == $ndir} {
5868 set clow [lrange $clow 2 end]
5869 }
5870 set coords [concat $coords $clow]
5871 if {!$joinhigh} {
5872 lset lines [expr {$i-1}] 1 $le
322a8cc9
PM
5873 } else {
5874 # coalesce two pieces
5875 $canv delete $ith
5876 set b [lindex $lines [expr {$i-1}] 0]
5877 set e [lindex $lines $i 1]
5878 set lines [lreplace $lines [expr {$i-1}] $i [list $b $e $itl]]
5879 }
5880 $canv coords $itl $coords
5881 if {$arrow ne $al} {
5882 $canv itemconf $itl -arrow $arrow
879e8b1a
PM
5883 }
5884 }
322a8cc9
PM
5885
5886 set linesegs($id) $lines
5887 return $le
9f1afe05
PM
5888}
5889
322a8cc9
PM
5890proc drawparentlinks {id row} {
5891 global rowidlist canv colormap curview parentlist
513a54dc 5892 global idpos linespc
9f1afe05 5893
322a8cc9
PM
5894 set rowids [lindex $rowidlist $row]
5895 set col [lsearch -exact $rowids $id]
5896 if {$col < 0} return
5897 set olds [lindex $parentlist $row]
9f1afe05
PM
5898 set row2 [expr {$row + 1}]
5899 set x [xc $row $col]
5900 set y [yc $row]
5901 set y2 [yc $row2]
e341c06d 5902 set d [expr {int(0.5 * $linespc)}]
513a54dc 5903 set ymid [expr {$y + $d}]
8f7d0cec 5904 set ids [lindex $rowidlist $row2]
9f1afe05
PM
5905 # rmx = right-most X coord used
5906 set rmx 0
9f1afe05 5907 foreach p $olds {
f3408449
PM
5908 set i [lsearch -exact $ids $p]
5909 if {$i < 0} {
5910 puts "oops, parent $p of $id not in list"
5911 continue
5912 }
5913 set x2 [xc $row2 $i]
5914 if {$x2 > $rmx} {
5915 set rmx $x2
5916 }
513a54dc
PM
5917 set j [lsearch -exact $rowids $p]
5918 if {$j < 0} {
eb447a12
PM
5919 # drawlineseg will do this one for us
5920 continue
5921 }
9f1afe05
PM
5922 assigncolor $p
5923 # should handle duplicated parents here...
5924 set coords [list $x $y]
513a54dc
PM
5925 if {$i != $col} {
5926 # if attaching to a vertical segment, draw a smaller
5927 # slant for visual distinctness
5928 if {$i == $j} {
5929 if {$i < $col} {
5930 lappend coords [expr {$x2 + $d}] $y $x2 $ymid
5931 } else {
5932 lappend coords [expr {$x2 - $d}] $y $x2 $ymid
5933 }
5934 } elseif {$i < $col && $i < $j} {
5935 # segment slants towards us already
5936 lappend coords [xc $row $j] $y
5937 } else {
5938 if {$i < $col - 1} {
5939 lappend coords [expr {$x2 + $linespc}] $y
5940 } elseif {$i > $col + 1} {
5941 lappend coords [expr {$x2 - $linespc}] $y
5942 }
5943 lappend coords $x2 $y2
5944 }
5945 } else {
5946 lappend coords $x2 $y2
9f1afe05 5947 }
c934a8a3 5948 set t [$canv create line $coords -width [linewidth $p] \
9f1afe05
PM
5949 -fill $colormap($p) -tags lines.$p]
5950 $canv lower $t
5951 bindline $t $p
5952 }
322a8cc9
PM
5953 if {$rmx > [lindex $idpos($id) 1]} {
5954 lset idpos($id) 1 $rmx
5955 redrawtags $id
5956 }
9f1afe05
PM
5957}
5958
c934a8a3 5959proc drawlines {id} {
322a8cc9 5960 global canv
9f1afe05 5961
322a8cc9 5962 $canv itemconf lines.$id -width [linewidth $id]
9f1afe05
PM
5963}
5964
322a8cc9 5965proc drawcmittext {id row col} {
7fcc92bf
PM
5966 global linespc canv canv2 canv3 fgcolor curview
5967 global cmitlisted commitinfo rowidlist parentlist
9f1afe05 5968 global rowtextx idpos idtags idheads idotherrefs
0380081c 5969 global linehtag linentag linedtag selectedline
b9fdba7f 5970 global canvxmax boldids boldnameids fgcolor markedid
d277e89f 5971 global mainheadid nullid nullid2 circleitem circlecolors ctxbut
252c52df
5972 global mainheadcirclecolor workingfilescirclecolor indexcirclecolor
5973 global circleoutlinecolor
9f1afe05 5974
1407ade9 5975 # listed is 0 for boundary, 1 for normal, 2 for negative, 3 for left, 4 for right
7fcc92bf 5976 set listed $cmitlisted($curview,$id)
219ea3a9 5977 if {$id eq $nullid} {
252c52df 5978 set ofill $workingfilescirclecolor
8f489363 5979 } elseif {$id eq $nullid2} {
252c52df 5980 set ofill $indexcirclecolor
c11ff120 5981 } elseif {$id eq $mainheadid} {
252c52df 5982 set ofill $mainheadcirclecolor
219ea3a9 5983 } else {
c11ff120 5984 set ofill [lindex $circlecolors $listed]
219ea3a9 5985 }
9f1afe05
PM
5986 set x [xc $row $col]
5987 set y [yc $row]
5988 set orad [expr {$linespc / 3}]
1407ade9 5989 if {$listed <= 2} {
c961b228
PM
5990 set t [$canv create oval [expr {$x - $orad}] [expr {$y - $orad}] \
5991 [expr {$x + $orad - 1}] [expr {$y + $orad - 1}] \
252c52df 5992 -fill $ofill -outline $circleoutlinecolor -width 1 -tags circle]
1407ade9 5993 } elseif {$listed == 3} {
c961b228
PM
5994 # triangle pointing left for left-side commits
5995 set t [$canv create polygon \
5996 [expr {$x - $orad}] $y \
5997 [expr {$x + $orad - 1}] [expr {$y - $orad}] \
5998 [expr {$x + $orad - 1}] [expr {$y + $orad - 1}] \
252c52df 5999 -fill $ofill -outline $circleoutlinecolor -width 1 -tags circle]
c961b228
PM
6000 } else {
6001 # triangle pointing right for right-side commits
6002 set t [$canv create polygon \
6003 [expr {$x + $orad - 1}] $y \
6004 [expr {$x - $orad}] [expr {$y - $orad}] \
6005 [expr {$x - $orad}] [expr {$y + $orad - 1}] \
252c52df 6006 -fill $ofill -outline $circleoutlinecolor -width 1 -tags circle]
c961b228 6007 }
c11ff120 6008 set circleitem($row) $t
9f1afe05
PM
6009 $canv raise $t
6010 $canv bind $t <1> {selcanvline {} %x %y}
322a8cc9
PM
6011 set rmx [llength [lindex $rowidlist $row]]
6012 set olds [lindex $parentlist $row]
6013 if {$olds ne {}} {
6014 set nextids [lindex $rowidlist [expr {$row + 1}]]
6015 foreach p $olds {
6016 set i [lsearch -exact $nextids $p]
6017 if {$i > $rmx} {
6018 set rmx $i
6019 }
6020 }
9f1afe05 6021 }
322a8cc9 6022 set xt [xc $row $rmx]
9f1afe05
PM
6023 set rowtextx($row) $xt
6024 set idpos($id) [list $x $xt $y]
6025 if {[info exists idtags($id)] || [info exists idheads($id)]
6026 || [info exists idotherrefs($id)]} {
6027 set xt [drawtags $id $x $xt $y]
6028 }
36242490
RZ
6029 if {[lindex $commitinfo($id) 6] > 0} {
6030 set xt [drawnotesign $xt $y]
6031 }
9f1afe05
PM
6032 set headline [lindex $commitinfo($id) 0]
6033 set name [lindex $commitinfo($id) 1]
6034 set date [lindex $commitinfo($id) 2]
6035 set date [formatdate $date]
9c311b32
PM
6036 set font mainfont
6037 set nfont mainfont
476ca63d 6038 set isbold [ishighlighted $id]
908c3585 6039 if {$isbold > 0} {
28593d3f 6040 lappend boldids $id
9c311b32 6041 set font mainfontbold
908c3585 6042 if {$isbold > 1} {
28593d3f 6043 lappend boldnameids $id
9c311b32 6044 set nfont mainfontbold
908c3585 6045 }
da7c24dd 6046 }
28593d3f
PM
6047 set linehtag($id) [$canv create text $xt $y -anchor w -fill $fgcolor \
6048 -text $headline -font $font -tags text]
6049 $canv bind $linehtag($id) $ctxbut "rowmenu %X %Y $id"
6050 set linentag($id) [$canv2 create text 3 $y -anchor w -fill $fgcolor \
6051 -text $name -font $nfont -tags text]
6052 set linedtag($id) [$canv3 create text 3 $y -anchor w -fill $fgcolor \
6053 -text $date -font mainfont -tags text]
94b4a69f 6054 if {$selectedline == $row} {
28593d3f 6055 make_secsel $id
0380081c 6056 }
b9fdba7f
PM
6057 if {[info exists markedid] && $markedid eq $id} {
6058 make_idmark $id
6059 }
9c311b32 6060 set xr [expr {$xt + [font measure $font $headline]}]
be0cd098
PM
6061 if {$xr > $canvxmax} {
6062 set canvxmax $xr
6063 setcanvscroll
6064 }
9f1afe05
PM
6065}
6066
6067proc drawcmitrow {row} {
0380081c 6068 global displayorder rowidlist nrows_drawn
005a2f4e 6069 global iddrawn markingmatches
7fcc92bf 6070 global commitinfo numcommits
687c8765 6071 global filehighlight fhighlights findpattern nhighlights
908c3585 6072 global hlview vhighlights
164ff275 6073 global highlight_related rhighlights
9f1afe05 6074
8f7d0cec 6075 if {$row >= $numcommits} return
9f1afe05
PM
6076
6077 set id [lindex $displayorder $row]
476ca63d 6078 if {[info exists hlview] && ![info exists vhighlights($id)]} {
908c3585
PM
6079 askvhighlight $row $id
6080 }
476ca63d 6081 if {[info exists filehighlight] && ![info exists fhighlights($id)]} {
908c3585
PM
6082 askfilehighlight $row $id
6083 }
476ca63d 6084 if {$findpattern ne {} && ![info exists nhighlights($id)]} {
60f7a7dc 6085 askfindhighlight $row $id
908c3585 6086 }
476ca63d 6087 if {$highlight_related ne [mc "None"] && ![info exists rhighlights($id)]} {
164ff275
PM
6088 askrelhighlight $row $id
6089 }
005a2f4e
PM
6090 if {![info exists iddrawn($id)]} {
6091 set col [lsearch -exact [lindex $rowidlist $row] $id]
6092 if {$col < 0} {
6093 puts "oops, row $row id $id not in list"
6094 return
6095 }
6096 if {![info exists commitinfo($id)]} {
6097 getcommit $id
6098 }
6099 assigncolor $id
6100 drawcmittext $id $row $col
6101 set iddrawn($id) 1
0380081c 6102 incr nrows_drawn
9f1afe05 6103 }
005a2f4e
PM
6104 if {$markingmatches} {
6105 markrowmatches $row $id
9f1afe05 6106 }
9f1afe05
PM
6107}
6108
322a8cc9 6109proc drawcommits {row {endrow {}}} {
0380081c 6110 global numcommits iddrawn displayorder curview need_redisplay
f5f3c2e2 6111 global parentlist rowidlist rowfinal uparrowlen downarrowlen nrows_drawn
9f1afe05 6112
9f1afe05
PM
6113 if {$row < 0} {
6114 set row 0
6115 }
322a8cc9
PM
6116 if {$endrow eq {}} {
6117 set endrow $row
6118 }
9f1afe05
PM
6119 if {$endrow >= $numcommits} {
6120 set endrow [expr {$numcommits - 1}]
6121 }
322a8cc9 6122
0380081c
PM
6123 set rl1 [expr {$row - $downarrowlen - 3}]
6124 if {$rl1 < 0} {
6125 set rl1 0
6126 }
6127 set ro1 [expr {$row - 3}]
6128 if {$ro1 < 0} {
6129 set ro1 0
6130 }
6131 set r2 [expr {$endrow + $uparrowlen + 3}]
6132 if {$r2 > $numcommits} {
6133 set r2 $numcommits
6134 }
6135 for {set r $rl1} {$r < $r2} {incr r} {
f5f3c2e2 6136 if {[lindex $rowidlist $r] ne {} && [lindex $rowfinal $r]} {
0380081c
PM
6137 if {$rl1 < $r} {
6138 layoutrows $rl1 $r
6139 }
6140 set rl1 [expr {$r + 1}]
6141 }
6142 }
6143 if {$rl1 < $r} {
6144 layoutrows $rl1 $r
6145 }
6146 optimize_rows $ro1 0 $r2
6147 if {$need_redisplay || $nrows_drawn > 2000} {
6148 clear_display
0380081c
PM
6149 }
6150
322a8cc9
PM
6151 # make the lines join to already-drawn rows either side
6152 set r [expr {$row - 1}]
6153 if {$r < 0 || ![info exists iddrawn([lindex $displayorder $r])]} {
6154 set r $row
6155 }
6156 set er [expr {$endrow + 1}]
6157 if {$er >= $numcommits ||
6158 ![info exists iddrawn([lindex $displayorder $er])]} {
6159 set er $endrow
6160 }
6161 for {} {$r <= $er} {incr r} {
6162 set id [lindex $displayorder $r]
6163 set wasdrawn [info exists iddrawn($id)]
4fb0fa19 6164 drawcmitrow $r
322a8cc9
PM
6165 if {$r == $er} break
6166 set nextid [lindex $displayorder [expr {$r + 1}]]
e5ef6f95 6167 if {$wasdrawn && [info exists iddrawn($nextid)]} continue
322a8cc9
PM
6168 drawparentlinks $id $r
6169
322a8cc9
PM
6170 set rowids [lindex $rowidlist $r]
6171 foreach lid $rowids {
6172 if {$lid eq {}} continue
e5ef6f95 6173 if {[info exists lineend($lid)] && $lineend($lid) > $r} continue
322a8cc9
PM
6174 if {$lid eq $id} {
6175 # see if this is the first child of any of its parents
6176 foreach p [lindex $parentlist $r] {
6177 if {[lsearch -exact $rowids $p] < 0} {
6178 # make this line extend up to the child
e5ef6f95 6179 set lineend($p) [drawlineseg $p $r $er 0]
322a8cc9
PM
6180 }
6181 }
e5ef6f95
PM
6182 } else {
6183 set lineend($lid) [drawlineseg $lid $r $er 1]
322a8cc9
PM
6184 }
6185 }
9f1afe05
PM
6186 }
6187}
6188
7fcc92bf
PM
6189proc undolayout {row} {
6190 global uparrowlen mingaplen downarrowlen
6191 global rowidlist rowisopt rowfinal need_redisplay
6192
6193 set r [expr {$row - ($uparrowlen + $mingaplen + $downarrowlen)}]
6194 if {$r < 0} {
6195 set r 0
6196 }
6197 if {[llength $rowidlist] > $r} {
6198 incr r -1
6199 set rowidlist [lrange $rowidlist 0 $r]
6200 set rowfinal [lrange $rowfinal 0 $r]
6201 set rowisopt [lrange $rowisopt 0 $r]
6202 set need_redisplay 1
6203 run drawvisible
6204 }
6205}
6206
31c0eaa8
PM
6207proc drawvisible {} {
6208 global canv linespc curview vrowmod selectedline targetrow targetid
42a671fc 6209 global need_redisplay cscroll numcommits
322a8cc9 6210
31c0eaa8 6211 set fs [$canv yview]
322a8cc9 6212 set ymax [lindex [$canv cget -scrollregion] 3]
5a7f577d 6213 if {$ymax eq {} || $ymax == 0 || $numcommits == 0} return
31c0eaa8
PM
6214 set f0 [lindex $fs 0]
6215 set f1 [lindex $fs 1]
322a8cc9 6216 set y0 [expr {int($f0 * $ymax)}]
322a8cc9 6217 set y1 [expr {int($f1 * $ymax)}]
31c0eaa8
PM
6218
6219 if {[info exists targetid]} {
42a671fc
PM
6220 if {[commitinview $targetid $curview]} {
6221 set r [rowofcommit $targetid]
6222 if {$r != $targetrow} {
6223 # Fix up the scrollregion and change the scrolling position
6224 # now that our target row has moved.
6225 set diff [expr {($r - $targetrow) * $linespc}]
6226 set targetrow $r
6227 setcanvscroll
6228 set ymax [lindex [$canv cget -scrollregion] 3]
6229 incr y0 $diff
6230 incr y1 $diff
6231 set f0 [expr {$y0 / $ymax}]
6232 set f1 [expr {$y1 / $ymax}]
6233 allcanvs yview moveto $f0
6234 $cscroll set $f0 $f1
6235 set need_redisplay 1
6236 }
6237 } else {
6238 unset targetid
31c0eaa8
PM
6239 }
6240 }
6241
6242 set row [expr {int(($y0 - 3) / $linespc) - 1}]
322a8cc9 6243 set endrow [expr {int(($y1 - 3) / $linespc) + 1}]
31c0eaa8
PM
6244 if {$endrow >= $vrowmod($curview)} {
6245 update_arcrows $curview
6246 }
94b4a69f 6247 if {$selectedline ne {} &&
31c0eaa8
PM
6248 $row <= $selectedline && $selectedline <= $endrow} {
6249 set targetrow $selectedline
ac1276ab 6250 } elseif {[info exists targetid]} {
31c0eaa8
PM
6251 set targetrow [expr {int(($row + $endrow) / 2)}]
6252 }
ac1276ab
PM
6253 if {[info exists targetrow]} {
6254 if {$targetrow >= $numcommits} {
6255 set targetrow [expr {$numcommits - 1}]
6256 }
6257 set targetid [commitonrow $targetrow]
42a671fc 6258 }
322a8cc9
PM
6259 drawcommits $row $endrow
6260}
6261
9f1afe05 6262proc clear_display {} {
0380081c 6263 global iddrawn linesegs need_redisplay nrows_drawn
164ff275 6264 global vhighlights fhighlights nhighlights rhighlights
28593d3f 6265 global linehtag linentag linedtag boldids boldnameids
9f1afe05
PM
6266
6267 allcanvs delete all
6268 catch {unset iddrawn}
322a8cc9 6269 catch {unset linesegs}
94503a66
PM
6270 catch {unset linehtag}
6271 catch {unset linentag}
6272 catch {unset linedtag}
28593d3f
PM
6273 set boldids {}
6274 set boldnameids {}
908c3585
PM
6275 catch {unset vhighlights}
6276 catch {unset fhighlights}
6277 catch {unset nhighlights}
164ff275 6278 catch {unset rhighlights}
0380081c
PM
6279 set need_redisplay 0
6280 set nrows_drawn 0
9f1afe05
PM
6281}
6282
50b44ece 6283proc findcrossings {id} {
6e8c8707 6284 global rowidlist parentlist numcommits displayorder
50b44ece
PM
6285
6286 set cross {}
6287 set ccross {}
6288 foreach {s e} [rowranges $id] {
6289 if {$e >= $numcommits} {
6290 set e [expr {$numcommits - 1}]
50b44ece 6291 }
d94f8cd6 6292 if {$e <= $s} continue
50b44ece 6293 for {set row $e} {[incr row -1] >= $s} {} {
6e8c8707
PM
6294 set x [lsearch -exact [lindex $rowidlist $row] $id]
6295 if {$x < 0} break
50b44ece
PM
6296 set olds [lindex $parentlist $row]
6297 set kid [lindex $displayorder $row]
6298 set kidx [lsearch -exact [lindex $rowidlist $row] $kid]
6299 if {$kidx < 0} continue
6300 set nextrow [lindex $rowidlist [expr {$row + 1}]]
6301 foreach p $olds {
6302 set px [lsearch -exact $nextrow $p]
6303 if {$px < 0} continue
6304 if {($kidx < $x && $x < $px) || ($px < $x && $x < $kidx)} {
6305 if {[lsearch -exact $ccross $p] >= 0} continue
6306 if {$x == $px + ($kidx < $px? -1: 1)} {
6307 lappend ccross $p
6308 } elseif {[lsearch -exact $cross $p] < 0} {
6309 lappend cross $p
6310 }
6311 }
6312 }
50b44ece
PM
6313 }
6314 }
6315 return [concat $ccross {{}} $cross]
6316}
6317
e5c2d856 6318proc assigncolor {id} {
aa81d974 6319 global colormap colors nextcolor
7fcc92bf 6320 global parents children children curview
6c20ff34 6321
418c4c7b 6322 if {[info exists colormap($id)]} return
e5c2d856 6323 set ncolors [llength $colors]
da7c24dd
PM
6324 if {[info exists children($curview,$id)]} {
6325 set kids $children($curview,$id)
79b2c75e
PM
6326 } else {
6327 set kids {}
6328 }
6329 if {[llength $kids] == 1} {
6330 set child [lindex $kids 0]
9ccbdfbf 6331 if {[info exists colormap($child)]
7fcc92bf 6332 && [llength $parents($curview,$child)] == 1} {
9ccbdfbf
PM
6333 set colormap($id) $colormap($child)
6334 return
e5c2d856 6335 }
9ccbdfbf
PM
6336 }
6337 set badcolors {}
50b44ece
PM
6338 set origbad {}
6339 foreach x [findcrossings $id] {
6340 if {$x eq {}} {
6341 # delimiter between corner crossings and other crossings
6342 if {[llength $badcolors] >= $ncolors - 1} break
6343 set origbad $badcolors
e5c2d856 6344 }
50b44ece
PM
6345 if {[info exists colormap($x)]
6346 && [lsearch -exact $badcolors $colormap($x)] < 0} {
6347 lappend badcolors $colormap($x)
6c20ff34
PM
6348 }
6349 }
50b44ece
PM
6350 if {[llength $badcolors] >= $ncolors} {
6351 set badcolors $origbad
9ccbdfbf 6352 }
50b44ece 6353 set origbad $badcolors
6c20ff34 6354 if {[llength $badcolors] < $ncolors - 1} {
79b2c75e 6355 foreach child $kids {
6c20ff34
PM
6356 if {[info exists colormap($child)]
6357 && [lsearch -exact $badcolors $colormap($child)] < 0} {
6358 lappend badcolors $colormap($child)
6359 }
7fcc92bf 6360 foreach p $parents($curview,$child) {
79b2c75e
PM
6361 if {[info exists colormap($p)]
6362 && [lsearch -exact $badcolors $colormap($p)] < 0} {
6363 lappend badcolors $colormap($p)
6c20ff34
PM
6364 }
6365 }
6366 }
6367 if {[llength $badcolors] >= $ncolors} {
6368 set badcolors $origbad
6369 }
9ccbdfbf
PM
6370 }
6371 for {set i 0} {$i <= $ncolors} {incr i} {
6372 set c [lindex $colors $nextcolor]
6373 if {[incr nextcolor] >= $ncolors} {
6374 set nextcolor 0
e5c2d856 6375 }
9ccbdfbf 6376 if {[lsearch -exact $badcolors $c]} break
e5c2d856 6377 }
9ccbdfbf 6378 set colormap($id) $c
e5c2d856
PM
6379}
6380
a823a911
PM
6381proc bindline {t id} {
6382 global canv
6383
a823a911
PM
6384 $canv bind $t <Enter> "lineenter %x %y $id"
6385 $canv bind $t <Motion> "linemotion %x %y $id"
6386 $canv bind $t <Leave> "lineleave $id"
fa4da7b3 6387 $canv bind $t <Button-1> "lineclick %x %y $id 1"
a823a911
PM
6388}
6389
bdbfbe3d 6390proc drawtags {id x xt y1} {
8a48571c 6391 global idtags idheads idotherrefs mainhead
bdbfbe3d 6392 global linespc lthickness
d277e89f 6393 global canv rowtextx curview fgcolor bgcolor ctxbut
252c52df
6394 global headbgcolor headfgcolor headoutlinecolor remotebgcolor
6395 global tagbgcolor tagfgcolor tagoutlinecolor
6396 global reflinecolor
bdbfbe3d
PM
6397
6398 set marks {}
6399 set ntags 0
f1d83ba3 6400 set nheads 0
bdbfbe3d
PM
6401 if {[info exists idtags($id)]} {
6402 set marks $idtags($id)
6403 set ntags [llength $marks]
6404 }
6405 if {[info exists idheads($id)]} {
6406 set marks [concat $marks $idheads($id)]
f1d83ba3
PM
6407 set nheads [llength $idheads($id)]
6408 }
6409 if {[info exists idotherrefs($id)]} {
6410 set marks [concat $marks $idotherrefs($id)]
bdbfbe3d
PM
6411 }
6412 if {$marks eq {}} {
6413 return $xt
6414 }
6415
6416 set delta [expr {int(0.5 * ($linespc - $lthickness))}]
2ed49d54
JH
6417 set yt [expr {$y1 - 0.5 * $linespc}]
6418 set yb [expr {$yt + $linespc - 1}]
bdbfbe3d
PM
6419 set xvals {}
6420 set wvals {}
8a48571c 6421 set i -1
bdbfbe3d 6422 foreach tag $marks {
8a48571c
PM
6423 incr i
6424 if {$i >= $ntags && $i < $ntags + $nheads && $tag eq $mainhead} {
9c311b32 6425 set wid [font measure mainfontbold $tag]
8a48571c 6426 } else {
9c311b32 6427 set wid [font measure mainfont $tag]
8a48571c 6428 }
bdbfbe3d
PM
6429 lappend xvals $xt
6430 lappend wvals $wid
6431 set xt [expr {$xt + $delta + $wid + $lthickness + $linespc}]
6432 }
6433 set t [$canv create line $x $y1 [lindex $xvals end] $y1 \
252c52df 6434 -width $lthickness -fill $reflinecolor -tags tag.$id]
bdbfbe3d
PM
6435 $canv lower $t
6436 foreach tag $marks x $xvals wid $wvals {
8dd60f54 6437 set tag_quoted [string map {% %%} $tag]
2ed49d54
JH
6438 set xl [expr {$x + $delta}]
6439 set xr [expr {$x + $delta + $wid + $lthickness}]
9c311b32 6440 set font mainfont
bdbfbe3d
PM
6441 if {[incr ntags -1] >= 0} {
6442 # draw a tag
2ed49d54
JH
6443 set t [$canv create polygon $x [expr {$yt + $delta}] $xl $yt \
6444 $xr $yt $xr $yb $xl $yb $x [expr {$yb - $delta}] \
252c52df
6445 -width 1 -outline $tagoutlinecolor -fill $tagbgcolor \
6446 -tags tag.$id]
8dd60f54 6447 $canv bind $t <1> [list showtag $tag_quoted 1]
7fcc92bf 6448 set rowtextx([rowofcommit $id]) [expr {$xr + $linespc}]
bdbfbe3d 6449 } else {
f1d83ba3
PM
6450 # draw a head or other ref
6451 if {[incr nheads -1] >= 0} {
252c52df 6452 set col $headbgcolor
8a48571c 6453 if {$tag eq $mainhead} {
9c311b32 6454 set font mainfontbold
8a48571c 6455 }
f1d83ba3
PM
6456 } else {
6457 set col "#ddddff"
6458 }
2ed49d54 6459 set xl [expr {$xl - $delta/2}]
bdbfbe3d 6460 $canv create polygon $x $yt $xr $yt $xr $yb $x $yb \
f1d83ba3 6461 -width 1 -outline black -fill $col -tags tag.$id
a970fcf2 6462 if {[regexp {^(remotes/.*/|remotes/)} $tag match remoteprefix]} {
9c311b32 6463 set rwid [font measure mainfont $remoteprefix]
a970fcf2
JW
6464 set xi [expr {$x + 1}]
6465 set yti [expr {$yt + 1}]
6466 set xri [expr {$x + $rwid}]
6467 $canv create polygon $xi $yti $xri $yti $xri $yb $xi $yb \
252c52df 6468 -width 0 -fill $remotebgcolor -tags tag.$id
a970fcf2 6469 }
bdbfbe3d 6470 }
252c52df 6471 set t [$canv create text $xl $y1 -anchor w -text $tag -fill $headfgcolor \
8a48571c 6472 -font $font -tags [list tag.$id text]]
106288cb 6473 if {$ntags >= 0} {
8dd60f54 6474 $canv bind $t <1> [list showtag $tag_quoted 1]
10299152 6475 } elseif {$nheads >= 0} {
8dd60f54 6476 $canv bind $t $ctxbut [list headmenu %X %Y $id $tag_quoted]
106288cb 6477 }
bdbfbe3d
PM
6478 }
6479 return $xt
6480}
6481
36242490
RZ
6482proc drawnotesign {xt y} {
6483 global linespc canv fgcolor
6484
6485 set orad [expr {$linespc / 3}]
6486 set t [$canv create rectangle [expr {$xt - $orad}] [expr {$y - $orad}] \
6487 [expr {$xt + $orad - 1}] [expr {$y + $orad - 1}] \
6488 -fill yellow -outline $fgcolor -width 1 -tags circle]
6489 set xt [expr {$xt + $orad * 3}]
6490 return $xt
6491}
6492
8d858d1a
PM
6493proc xcoord {i level ln} {
6494 global canvx0 xspc1 xspc2
6495
6496 set x [expr {$canvx0 + $i * $xspc1($ln)}]
6497 if {$i > 0 && $i == $level} {
6498 set x [expr {$x + 0.5 * ($xspc2 - $xspc1($ln))}]
6499 } elseif {$i > $level} {
6500 set x [expr {$x + $xspc2 - $xspc1($ln)}]
6501 }
6502 return $x
6503}
9ccbdfbf 6504
098dd8a3 6505proc show_status {msg} {
9c311b32 6506 global canv fgcolor
098dd8a3
PM
6507
6508 clear_display
9c311b32 6509 $canv create text 3 3 -anchor nw -text $msg -font mainfont \
f8a2c0d1 6510 -tags text -fill $fgcolor
098dd8a3
PM
6511}
6512
94a2eede
PM
6513# Don't change the text pane cursor if it is currently the hand cursor,
6514# showing that we are over a sha1 ID link.
6515proc settextcursor {c} {
6516 global ctext curtextcursor
6517
6518 if {[$ctext cget -cursor] == $curtextcursor} {
6519 $ctext config -cursor $c
6520 }
6521 set curtextcursor $c
9ccbdfbf
PM
6522}
6523
a137a90f
PM
6524proc nowbusy {what {name {}}} {
6525 global isbusy busyname statusw
da7c24dd
PM
6526
6527 if {[array names isbusy] eq {}} {
6528 . config -cursor watch
6529 settextcursor watch
6530 }
6531 set isbusy($what) 1
a137a90f
PM
6532 set busyname($what) $name
6533 if {$name ne {}} {
6534 $statusw conf -text $name
6535 }
da7c24dd
PM
6536}
6537
6538proc notbusy {what} {
a137a90f 6539 global isbusy maincursor textcursor busyname statusw
da7c24dd 6540
a137a90f
PM
6541 catch {
6542 unset isbusy($what)
6543 if {$busyname($what) ne {} &&
6544 [$statusw cget -text] eq $busyname($what)} {
6545 $statusw conf -text {}
6546 }
6547 }
da7c24dd
PM
6548 if {[array names isbusy] eq {}} {
6549 . config -cursor $maincursor
6550 settextcursor $textcursor
6551 }
6552}
6553
df3d83b1 6554proc findmatches {f} {
4fb0fa19 6555 global findtype findstring
b007ee20 6556 if {$findtype == [mc "Regexp"]} {
4fb0fa19 6557 set matches [regexp -indices -all -inline $findstring $f]
df3d83b1 6558 } else {
4fb0fa19 6559 set fs $findstring
b007ee20 6560 if {$findtype == [mc "IgnCase"]} {
4fb0fa19
PM
6561 set f [string tolower $f]
6562 set fs [string tolower $fs]
df3d83b1
PM
6563 }
6564 set matches {}
6565 set i 0
4fb0fa19
PM
6566 set l [string length $fs]
6567 while {[set j [string first $fs $f $i]] >= 0} {
6568 lappend matches [list $j [expr {$j+$l-1}]]
6569 set i [expr {$j + $l}]
df3d83b1
PM
6570 }
6571 }
6572 return $matches
6573}
6574
cca5d946 6575proc dofind {{dirn 1} {wrap 1}} {
4fb0fa19 6576 global findstring findstartline findcurline selectedline numcommits
cca5d946 6577 global gdttype filehighlight fh_serial find_dirn findallowwrap
b74fd579 6578
cca5d946
PM
6579 if {[info exists find_dirn]} {
6580 if {$find_dirn == $dirn} return
6581 stopfinding
6582 }
df3d83b1 6583 focus .
4fb0fa19 6584 if {$findstring eq {} || $numcommits == 0} return
94b4a69f 6585 if {$selectedline eq {}} {
cca5d946 6586 set findstartline [lindex [visiblerows] [expr {$dirn < 0}]]
98f350e5 6587 } else {
4fb0fa19 6588 set findstartline $selectedline
98f350e5 6589 }
4fb0fa19 6590 set findcurline $findstartline
b007ee20
CS
6591 nowbusy finding [mc "Searching"]
6592 if {$gdttype ne [mc "containing:"] && ![info exists filehighlight]} {
687c8765
PM
6593 after cancel do_file_hl $fh_serial
6594 do_file_hl $fh_serial
98f350e5 6595 }
cca5d946
PM
6596 set find_dirn $dirn
6597 set findallowwrap $wrap
6598 run findmore
4fb0fa19
PM
6599}
6600
bb3edc8b
PM
6601proc stopfinding {} {
6602 global find_dirn findcurline fprogcoord
4fb0fa19 6603
bb3edc8b
PM
6604 if {[info exists find_dirn]} {
6605 unset find_dirn
6606 unset findcurline
6607 notbusy finding
6608 set fprogcoord 0
6609 adjustprogress
4fb0fa19 6610 }
8a897742 6611 stopblaming
4fb0fa19
PM
6612}
6613
6614proc findmore {} {
687c8765 6615 global commitdata commitinfo numcommits findpattern findloc
7fcc92bf 6616 global findstartline findcurline findallowwrap
bb3edc8b 6617 global find_dirn gdttype fhighlights fprogcoord
cd2bcae7 6618 global curview varcorder vrownum varccommits vrowmod
4fb0fa19 6619
bb3edc8b 6620 if {![info exists find_dirn]} {
4fb0fa19
PM
6621 return 0
6622 }
585c27cb 6623 set fldtypes [list [mc "Headline"] [mc "Author"] "" [mc "Committer"] "" [mc "Comments"]]
4fb0fa19 6624 set l $findcurline
cca5d946
PM
6625 set moretodo 0
6626 if {$find_dirn > 0} {
6627 incr l
6628 if {$l >= $numcommits} {
6629 set l 0
6630 }
6631 if {$l <= $findstartline} {
6632 set lim [expr {$findstartline + 1}]
6633 } else {
6634 set lim $numcommits
6635 set moretodo $findallowwrap
8ed16484 6636 }
4fb0fa19 6637 } else {
cca5d946
PM
6638 if {$l == 0} {
6639 set l $numcommits
98f350e5 6640 }
cca5d946
PM
6641 incr l -1
6642 if {$l >= $findstartline} {
6643 set lim [expr {$findstartline - 1}]
bb3edc8b 6644 } else {
cca5d946
PM
6645 set lim -1
6646 set moretodo $findallowwrap
bb3edc8b 6647 }
687c8765 6648 }
cca5d946
PM
6649 set n [expr {($lim - $l) * $find_dirn}]
6650 if {$n > 500} {
6651 set n 500
6652 set moretodo 1
4fb0fa19 6653 }
cd2bcae7
PM
6654 if {$l + ($find_dirn > 0? $n: 1) > $vrowmod($curview)} {
6655 update_arcrows $curview
6656 }
687c8765
PM
6657 set found 0
6658 set domore 1
7fcc92bf
PM
6659 set ai [bsearch $vrownum($curview) $l]
6660 set a [lindex $varcorder($curview) $ai]
6661 set arow [lindex $vrownum($curview) $ai]
6662 set ids [lindex $varccommits($curview,$a)]
6663 set arowend [expr {$arow + [llength $ids]}]
b007ee20 6664 if {$gdttype eq [mc "containing:"]} {
cca5d946 6665 for {} {$n > 0} {incr n -1; incr l $find_dirn} {
7fcc92bf
PM
6666 if {$l < $arow || $l >= $arowend} {
6667 incr ai $find_dirn
6668 set a [lindex $varcorder($curview) $ai]
6669 set arow [lindex $vrownum($curview) $ai]
6670 set ids [lindex $varccommits($curview,$a)]
6671 set arowend [expr {$arow + [llength $ids]}]
6672 }
6673 set id [lindex $ids [expr {$l - $arow}]]
cca5d946 6674 # shouldn't happen unless git log doesn't give all the commits...
7fcc92bf
PM
6675 if {![info exists commitdata($id)] ||
6676 ![doesmatch $commitdata($id)]} {
6677 continue
6678 }
687c8765
PM
6679 if {![info exists commitinfo($id)]} {
6680 getcommit $id
6681 }
6682 set info $commitinfo($id)
6683 foreach f $info ty $fldtypes {
585c27cb 6684 if {$ty eq ""} continue
b007ee20 6685 if {($findloc eq [mc "All fields"] || $findloc eq $ty) &&
687c8765
PM
6686 [doesmatch $f]} {
6687 set found 1
6688 break
6689 }
6690 }
6691 if {$found} break
4fb0fa19 6692 }
687c8765 6693 } else {
cca5d946 6694 for {} {$n > 0} {incr n -1; incr l $find_dirn} {
7fcc92bf
PM
6695 if {$l < $arow || $l >= $arowend} {
6696 incr ai $find_dirn
6697 set a [lindex $varcorder($curview) $ai]
6698 set arow [lindex $vrownum($curview) $ai]
6699 set ids [lindex $varccommits($curview,$a)]
6700 set arowend [expr {$arow + [llength $ids]}]
6701 }
6702 set id [lindex $ids [expr {$l - $arow}]]
476ca63d
PM
6703 if {![info exists fhighlights($id)]} {
6704 # this sets fhighlights($id) to -1
687c8765 6705 askfilehighlight $l $id
cd2bcae7 6706 }
476ca63d 6707 if {$fhighlights($id) > 0} {
cd2bcae7
PM
6708 set found $domore
6709 break
6710 }
476ca63d 6711 if {$fhighlights($id) < 0} {
687c8765
PM
6712 if {$domore} {
6713 set domore 0
cca5d946 6714 set findcurline [expr {$l - $find_dirn}]
687c8765 6715 }
98f350e5
PM
6716 }
6717 }
6718 }
cca5d946 6719 if {$found || ($domore && !$moretodo)} {
4fb0fa19 6720 unset findcurline
687c8765 6721 unset find_dirn
4fb0fa19 6722 notbusy finding
bb3edc8b
PM
6723 set fprogcoord 0
6724 adjustprogress
6725 if {$found} {
6726 findselectline $l
6727 } else {
6728 bell
6729 }
4fb0fa19 6730 return 0
df3d83b1 6731 }
687c8765
PM
6732 if {!$domore} {
6733 flushhighlights
bb3edc8b 6734 } else {
cca5d946 6735 set findcurline [expr {$l - $find_dirn}]
687c8765 6736 }
cca5d946 6737 set n [expr {($findcurline - $findstartline) * $find_dirn - 1}]
bb3edc8b
PM
6738 if {$n < 0} {
6739 incr n $numcommits
df3d83b1 6740 }
bb3edc8b
PM
6741 set fprogcoord [expr {$n * 1.0 / $numcommits}]
6742 adjustprogress
6743 return $domore
df3d83b1
PM
6744}
6745
6746proc findselectline {l} {
687c8765 6747 global findloc commentend ctext findcurline markingmatches gdttype
005a2f4e 6748
8b39e04f 6749 set markingmatches [expr {$gdttype eq [mc "containing:"]}]
005a2f4e 6750 set findcurline $l
d698206c 6751 selectline $l 1
8b39e04f
PM
6752 if {$markingmatches &&
6753 ($findloc eq [mc "All fields"] || $findloc eq [mc "Comments"])} {
df3d83b1
PM
6754 # highlight the matches in the comments
6755 set f [$ctext get 1.0 $commentend]
6756 set matches [findmatches $f]
6757 foreach match $matches {
6758 set start [lindex $match 0]
2ed49d54 6759 set end [expr {[lindex $match 1] + 1}]
df3d83b1
PM
6760 $ctext tag add found "1.0 + $start c" "1.0 + $end c"
6761 }
98f350e5 6762 }
005a2f4e 6763 drawvisible
98f350e5
PM
6764}
6765
4fb0fa19 6766# mark the bits of a headline or author that match a find string
005a2f4e
PM
6767proc markmatches {canv l str tag matches font row} {
6768 global selectedline
6769
98f350e5
PM
6770 set bbox [$canv bbox $tag]
6771 set x0 [lindex $bbox 0]
6772 set y0 [lindex $bbox 1]
6773 set y1 [lindex $bbox 3]
6774 foreach match $matches {
6775 set start [lindex $match 0]
6776 set end [lindex $match 1]
6777 if {$start > $end} continue
2ed49d54
JH
6778 set xoff [font measure $font [string range $str 0 [expr {$start-1}]]]
6779 set xlen [font measure $font [string range $str 0 [expr {$end}]]]
6780 set t [$canv create rect [expr {$x0+$xoff}] $y0 \
6781 [expr {$x0+$xlen+2}] $y1 \
4fb0fa19 6782 -outline {} -tags [list match$l matches] -fill yellow]
98f350e5 6783 $canv lower $t
94b4a69f 6784 if {$row == $selectedline} {
005a2f4e
PM
6785 $canv raise $t secsel
6786 }
98f350e5
PM
6787 }
6788}
6789
6790proc unmarkmatches {} {
bb3edc8b 6791 global markingmatches
4fb0fa19 6792
98f350e5 6793 allcanvs delete matches
4fb0fa19 6794 set markingmatches 0
bb3edc8b 6795 stopfinding
98f350e5
PM
6796}
6797
c8dfbcf9 6798proc selcanvline {w x y} {
fa4da7b3 6799 global canv canvy0 ctext linespc
9f1afe05 6800 global rowtextx
1db95b00 6801 set ymax [lindex [$canv cget -scrollregion] 3]
cfb4563c 6802 if {$ymax == {}} return
1db95b00
PM
6803 set yfrac [lindex [$canv yview] 0]
6804 set y [expr {$y + $yfrac * $ymax}]
6805 set l [expr {int(($y - $canvy0) / $linespc + 0.5)}]
6806 if {$l < 0} {
6807 set l 0
6808 }
c8dfbcf9 6809 if {$w eq $canv} {
fc2a256f
PM
6810 set xmax [lindex [$canv cget -scrollregion] 2]
6811 set xleft [expr {[lindex [$canv xview] 0] * $xmax}]
6812 if {![info exists rowtextx($l)] || $xleft + $x < $rowtextx($l)} return
c8dfbcf9 6813 }
98f350e5 6814 unmarkmatches
d698206c 6815 selectline $l 1
5ad588de
PM
6816}
6817
b1ba39e7
LT
6818proc commit_descriptor {p} {
6819 global commitinfo
b0934489
PM
6820 if {![info exists commitinfo($p)]} {
6821 getcommit $p
6822 }
b1ba39e7 6823 set l "..."
b0934489 6824 if {[llength $commitinfo($p)] > 1} {
b1ba39e7
LT
6825 set l [lindex $commitinfo($p) 0]
6826 }
b8ab2e17 6827 return "$p ($l)\n"
b1ba39e7
LT
6828}
6829
106288cb
PM
6830# append some text to the ctext widget, and make any SHA1 ID
6831# that we know about be a clickable link.
f1b86294 6832proc appendwithlinks {text tags} {
d375ef9b 6833 global ctext linknum curview
106288cb
PM
6834
6835 set start [$ctext index "end - 1c"]
f1b86294 6836 $ctext insert end $text $tags
6c9e2d18 6837 set links [regexp -indices -all -inline {(?:\m|-g)[0-9a-f]{6,40}\M} $text]
106288cb
PM
6838 foreach l $links {
6839 set s [lindex $l 0]
6840 set e [lindex $l 1]
6841 set linkid [string range $text $s $e]
106288cb 6842 incr e
c73adce2 6843 $ctext tag delete link$linknum
106288cb 6844 $ctext tag add link$linknum "$start + $s c" "$start + $e c"
97645683 6845 setlink $linkid link$linknum
106288cb
PM
6846 incr linknum
6847 }
97645683
PM
6848}
6849
6850proc setlink {id lk} {
d375ef9b 6851 global curview ctext pendinglinks
252c52df 6852 global linkfgcolor
97645683 6853
6c9e2d18
JM
6854 if {[string range $id 0 1] eq "-g"} {
6855 set id [string range $id 2 end]
6856 }
6857
d375ef9b
PM
6858 set known 0
6859 if {[string length $id] < 40} {
6860 set matches [longid $id]
6861 if {[llength $matches] > 0} {
6862 if {[llength $matches] > 1} return
6863 set known 1
6864 set id [lindex $matches 0]
6865 }
6866 } else {
6867 set known [commitinview $id $curview]
6868 }
6869 if {$known} {
252c52df 6870 $ctext tag conf $lk -foreground $linkfgcolor -underline 1
d375ef9b 6871 $ctext tag bind $lk <1> [list selbyid $id]
97645683
PM
6872 $ctext tag bind $lk <Enter> {linkcursor %W 1}
6873 $ctext tag bind $lk <Leave> {linkcursor %W -1}
6874 } else {
6875 lappend pendinglinks($id) $lk
d375ef9b 6876 interestedin $id {makelink %P}
97645683
PM
6877 }
6878}
6879
6f63fc18
PM
6880proc appendshortlink {id {pre {}} {post {}}} {
6881 global ctext linknum
6882
6883 $ctext insert end $pre
6884 $ctext tag delete link$linknum
6885 $ctext insert end [string range $id 0 7] link$linknum
6886 $ctext insert end $post
6887 setlink $id link$linknum
6888 incr linknum
6889}
6890
97645683
PM
6891proc makelink {id} {
6892 global pendinglinks
6893
6894 if {![info exists pendinglinks($id)]} return
6895 foreach lk $pendinglinks($id) {
6896 setlink $id $lk
6897 }
6898 unset pendinglinks($id)
6899}
6900
6901proc linkcursor {w inc} {
6902 global linkentercount curtextcursor
6903
6904 if {[incr linkentercount $inc] > 0} {
6905 $w configure -cursor hand2
6906 } else {
6907 $w configure -cursor $curtextcursor
6908 if {$linkentercount < 0} {
6909 set linkentercount 0
6910 }
6911 }
106288cb
PM
6912}
6913
6e5f7203
RN
6914proc viewnextline {dir} {
6915 global canv linespc
6916
6917 $canv delete hover
6918 set ymax [lindex [$canv cget -scrollregion] 3]
6919 set wnow [$canv yview]
6920 set wtop [expr {[lindex $wnow 0] * $ymax}]
6921 set newtop [expr {$wtop + $dir * $linespc}]
6922 if {$newtop < 0} {
6923 set newtop 0
6924 } elseif {$newtop > $ymax} {
6925 set newtop $ymax
6926 }
6927 allcanvs yview moveto [expr {$newtop * 1.0 / $ymax}]
6928}
6929
ef030b85
PM
6930# add a list of tag or branch names at position pos
6931# returns the number of names inserted
e11f1233 6932proc appendrefs {pos ids var} {
386befb7 6933 global ctext linknum curview $var maxrefs mainheadid
b8ab2e17 6934
ef030b85
PM
6935 if {[catch {$ctext index $pos}]} {
6936 return 0
6937 }
e11f1233
PM
6938 $ctext conf -state normal
6939 $ctext delete $pos "$pos lineend"
6940 set tags {}
6941 foreach id $ids {
6942 foreach tag [set $var\($id\)] {
6943 lappend tags [list $tag $id]
6944 }
6945 }
386befb7
PM
6946
6947 set sep {}
6948 set tags [lsort -index 0 -decreasing $tags]
6949 set nutags 0
6950
0a4dd8b8 6951 if {[llength $tags] > $maxrefs} {
386befb7
PM
6952 # If we are displaying heads, and there are too many,
6953 # see if there are some important heads to display.
6954 # Currently this means "master" and the current head.
6955 set itags {}
6956 if {$var eq "idheads"} {
6957 set utags {}
6958 foreach ti $tags {
6959 set hname [lindex $ti 0]
6960 set id [lindex $ti 1]
6961 if {($hname eq "master" || $id eq $mainheadid) &&
6962 [llength $itags] < $maxrefs} {
6963 lappend itags $ti
6964 } else {
6965 lappend utags $ti
6966 }
6967 }
6968 set tags $utags
b8ab2e17 6969 }
386befb7
PM
6970 if {$itags ne {}} {
6971 set str [mc "and many more"]
6972 set sep " "
6973 } else {
6974 set str [mc "many"]
6975 }
6976 $ctext insert $pos "$str ([llength $tags])"
6977 set nutags [llength $tags]
6978 set tags $itags
6979 }
6980
6981 foreach ti $tags {
6982 set id [lindex $ti 1]
6983 set lk link$linknum
6984 incr linknum
6985 $ctext tag delete $lk
6986 $ctext insert $pos $sep
6987 $ctext insert $pos [lindex $ti 0] $lk
6988 setlink $id $lk
6989 set sep ", "
b8ab2e17 6990 }
d34835c9 6991 $ctext tag add wwrap "$pos linestart" "$pos lineend"
e11f1233 6992 $ctext conf -state disabled
386befb7 6993 return [expr {[llength $tags] + $nutags}]
b8ab2e17
PM
6994}
6995
e11f1233
PM
6996# called when we have finished computing the nearby tags
6997proc dispneartags {delay} {
6998 global selectedline currentid showneartags tagphase
ca6d8f58 6999
94b4a69f 7000 if {$selectedline eq {} || !$showneartags} return
e11f1233
PM
7001 after cancel dispnexttag
7002 if {$delay} {
7003 after 200 dispnexttag
7004 set tagphase -1
7005 } else {
7006 after idle dispnexttag
7007 set tagphase 0
ca6d8f58 7008 }
ca6d8f58
PM
7009}
7010
e11f1233
PM
7011proc dispnexttag {} {
7012 global selectedline currentid showneartags tagphase ctext
b8ab2e17 7013
94b4a69f 7014 if {$selectedline eq {} || !$showneartags} return
e11f1233
PM
7015 switch -- $tagphase {
7016 0 {
7017 set dtags [desctags $currentid]
7018 if {$dtags ne {}} {
7019 appendrefs precedes $dtags idtags
7020 }
7021 }
7022 1 {
7023 set atags [anctags $currentid]
7024 if {$atags ne {}} {
7025 appendrefs follows $atags idtags
7026 }
7027 }
7028 2 {
7029 set dheads [descheads $currentid]
7030 if {$dheads ne {}} {
7031 if {[appendrefs branch $dheads idheads] > 1
7032 && [$ctext get "branch -3c"] eq "h"} {
7033 # turn "Branch" into "Branches"
7034 $ctext conf -state normal
7035 $ctext insert "branch -2c" "es"
7036 $ctext conf -state disabled
7037 }
7038 }
ef030b85
PM
7039 }
7040 }
e11f1233
PM
7041 if {[incr tagphase] <= 2} {
7042 after idle dispnexttag
b8ab2e17 7043 }
b8ab2e17
PM
7044}
7045
28593d3f 7046proc make_secsel {id} {
0380081c
PM
7047 global linehtag linentag linedtag canv canv2 canv3
7048
28593d3f 7049 if {![info exists linehtag($id)]} return
0380081c 7050 $canv delete secsel
28593d3f 7051 set t [eval $canv create rect [$canv bbox $linehtag($id)] -outline {{}} \
0380081c
PM
7052 -tags secsel -fill [$canv cget -selectbackground]]
7053 $canv lower $t
7054 $canv2 delete secsel
28593d3f 7055 set t [eval $canv2 create rect [$canv2 bbox $linentag($id)] -outline {{}} \
0380081c
PM
7056 -tags secsel -fill [$canv2 cget -selectbackground]]
7057 $canv2 lower $t
7058 $canv3 delete secsel
28593d3f 7059 set t [eval $canv3 create rect [$canv3 bbox $linedtag($id)] -outline {{}} \
0380081c
PM
7060 -tags secsel -fill [$canv3 cget -selectbackground]]
7061 $canv3 lower $t
7062}
7063
b9fdba7f
PM
7064proc make_idmark {id} {
7065 global linehtag canv fgcolor
7066
7067 if {![info exists linehtag($id)]} return
7068 $canv delete markid
7069 set t [eval $canv create rect [$canv bbox $linehtag($id)] \
7070 -tags markid -outline $fgcolor]
7071 $canv raise $t
7072}
7073
8a897742 7074proc selectline {l isnew {desired_loc {}}} {
0380081c 7075 global canv ctext commitinfo selectedline
7fcc92bf 7076 global canvy0 linespc parents children curview
7fcceed7 7077 global currentid sha1entry
9f1afe05 7078 global commentend idtags linknum
d94f8cd6 7079 global mergemax numcommits pending_select
e11f1233 7080 global cmitmode showneartags allcommits
c30acc77 7081 global targetrow targetid lastscrollrows
21ac8a8d 7082 global autoselect autosellen jump_to_here
d698206c 7083
d94f8cd6 7084 catch {unset pending_select}
84ba7345 7085 $canv delete hover
9843c307 7086 normalline
887c996e 7087 unsel_reflist
bb3edc8b 7088 stopfinding
8f7d0cec 7089 if {$l < 0 || $l >= $numcommits} return
ac1276ab
PM
7090 set id [commitonrow $l]
7091 set targetid $id
7092 set targetrow $l
c30acc77
PM
7093 set selectedline $l
7094 set currentid $id
7095 if {$lastscrollrows < $numcommits} {
7096 setcanvscroll
7097 }
ac1276ab 7098
5ad588de 7099 set y [expr {$canvy0 + $l * $linespc}]
17386066 7100 set ymax [lindex [$canv cget -scrollregion] 3]
5842215e
PM
7101 set ytop [expr {$y - $linespc - 1}]
7102 set ybot [expr {$y + $linespc + 1}]
5ad588de 7103 set wnow [$canv yview]
2ed49d54
JH
7104 set wtop [expr {[lindex $wnow 0] * $ymax}]
7105 set wbot [expr {[lindex $wnow 1] * $ymax}]
5842215e
PM
7106 set wh [expr {$wbot - $wtop}]
7107 set newtop $wtop
17386066 7108 if {$ytop < $wtop} {
5842215e
PM
7109 if {$ybot < $wtop} {
7110 set newtop [expr {$y - $wh / 2.0}]
7111 } else {
7112 set newtop $ytop
7113 if {$newtop > $wtop - $linespc} {
7114 set newtop [expr {$wtop - $linespc}]
7115 }
17386066 7116 }
5842215e
PM
7117 } elseif {$ybot > $wbot} {
7118 if {$ytop > $wbot} {
7119 set newtop [expr {$y - $wh / 2.0}]
7120 } else {
7121 set newtop [expr {$ybot - $wh}]
7122 if {$newtop < $wtop + $linespc} {
7123 set newtop [expr {$wtop + $linespc}]
7124 }
17386066 7125 }
5842215e
PM
7126 }
7127 if {$newtop != $wtop} {
7128 if {$newtop < 0} {
7129 set newtop 0
7130 }
2ed49d54 7131 allcanvs yview moveto [expr {$newtop * 1.0 / $ymax}]
9f1afe05 7132 drawvisible
5ad588de 7133 }
d698206c 7134
28593d3f 7135 make_secsel $id
9f1afe05 7136
fa4da7b3 7137 if {$isnew} {
354af6bd 7138 addtohistory [list selbyid $id 0] savecmitpos
d698206c
PM
7139 }
7140
98f350e5
PM
7141 $sha1entry delete 0 end
7142 $sha1entry insert 0 $id
95293b58 7143 if {$autoselect} {
21ac8a8d 7144 $sha1entry selection range 0 $autosellen
95293b58 7145 }
164ff275 7146 rhighlight_sel $id
98f350e5 7147
5ad588de 7148 $ctext conf -state normal
3ea06f9f 7149 clear_ctext
106288cb 7150 set linknum 0
d76afb15
PM
7151 if {![info exists commitinfo($id)]} {
7152 getcommit $id
7153 }
1db95b00 7154 set info $commitinfo($id)
232475d3 7155 set date [formatdate [lindex $info 2]]
d990cedf 7156 $ctext insert end "[mc "Author"]: [lindex $info 1] $date\n"
232475d3 7157 set date [formatdate [lindex $info 4]]
d990cedf 7158 $ctext insert end "[mc "Committer"]: [lindex $info 3] $date\n"
887fe3c4 7159 if {[info exists idtags($id)]} {
d990cedf 7160 $ctext insert end [mc "Tags:"]
887fe3c4
PM
7161 foreach tag $idtags($id) {
7162 $ctext insert end " $tag"
7163 }
7164 $ctext insert end "\n"
7165 }
40b87ff8 7166
f1b86294 7167 set headers {}
7fcc92bf 7168 set olds $parents($curview,$id)
79b2c75e 7169 if {[llength $olds] > 1} {
b77b0278 7170 set np 0
79b2c75e 7171 foreach p $olds {
b77b0278
PM
7172 if {$np >= $mergemax} {
7173 set tag mmax
7174 } else {
7175 set tag m$np
7176 }
d990cedf 7177 $ctext insert end "[mc "Parent"]: " $tag
f1b86294 7178 appendwithlinks [commit_descriptor $p] {}
b77b0278
PM
7179 incr np
7180 }
7181 } else {
79b2c75e 7182 foreach p $olds {
d990cedf 7183 append headers "[mc "Parent"]: [commit_descriptor $p]"
b1ba39e7
LT
7184 }
7185 }
b77b0278 7186
6a90bff1 7187 foreach c $children($curview,$id) {
d990cedf 7188 append headers "[mc "Child"]: [commit_descriptor $c]"
8b192809 7189 }
d698206c
PM
7190
7191 # make anything that looks like a SHA1 ID be a clickable link
f1b86294 7192 appendwithlinks $headers {}
b8ab2e17
PM
7193 if {$showneartags} {
7194 if {![info exists allcommits]} {
7195 getallcommits
7196 }
d990cedf 7197 $ctext insert end "[mc "Branch"]: "
ef030b85
PM
7198 $ctext mark set branch "end -1c"
7199 $ctext mark gravity branch left
d990cedf 7200 $ctext insert end "\n[mc "Follows"]: "
b8ab2e17
PM
7201 $ctext mark set follows "end -1c"
7202 $ctext mark gravity follows left
d990cedf 7203 $ctext insert end "\n[mc "Precedes"]: "
b8ab2e17
PM
7204 $ctext mark set precedes "end -1c"
7205 $ctext mark gravity precedes left
b8ab2e17 7206 $ctext insert end "\n"
e11f1233 7207 dispneartags 1
b8ab2e17
PM
7208 }
7209 $ctext insert end "\n"
43c25074
PM
7210 set comment [lindex $info 5]
7211 if {[string first "\r" $comment] >= 0} {
7212 set comment [string map {"\r" "\n "} $comment]
7213 }
7214 appendwithlinks $comment {comment}
d698206c 7215
df3d83b1 7216 $ctext tag remove found 1.0 end
5ad588de 7217 $ctext conf -state disabled
df3d83b1 7218 set commentend [$ctext index "end - 1c"]
5ad588de 7219
8a897742 7220 set jump_to_here $desired_loc
b007ee20 7221 init_flist [mc "Comments"]
f8b28a40
PM
7222 if {$cmitmode eq "tree"} {
7223 gettree $id
7224 } elseif {[llength $olds] <= 1} {
d327244a 7225 startdiff $id
7b5ff7e7 7226 } else {
7fcc92bf 7227 mergediff $id
3c461ffe
PM
7228 }
7229}
7230
6e5f7203
RN
7231proc selfirstline {} {
7232 unmarkmatches
7233 selectline 0 1
7234}
7235
7236proc sellastline {} {
7237 global numcommits
7238 unmarkmatches
7239 set l [expr {$numcommits - 1}]
7240 selectline $l 1
7241}
7242
3c461ffe
PM
7243proc selnextline {dir} {
7244 global selectedline
bd441de4 7245 focus .
94b4a69f 7246 if {$selectedline eq {}} return
2ed49d54 7247 set l [expr {$selectedline + $dir}]
3c461ffe 7248 unmarkmatches
d698206c
PM
7249 selectline $l 1
7250}
7251
6e5f7203
RN
7252proc selnextpage {dir} {
7253 global canv linespc selectedline numcommits
7254
7255 set lpp [expr {([winfo height $canv] - 2) / $linespc}]
7256 if {$lpp < 1} {
7257 set lpp 1
7258 }
7259 allcanvs yview scroll [expr {$dir * $lpp}] units
e72ee5eb 7260 drawvisible
94b4a69f 7261 if {$selectedline eq {}} return
6e5f7203
RN
7262 set l [expr {$selectedline + $dir * $lpp}]
7263 if {$l < 0} {
7264 set l 0
7265 } elseif {$l >= $numcommits} {
7266 set l [expr $numcommits - 1]
7267 }
7268 unmarkmatches
40b87ff8 7269 selectline $l 1
6e5f7203
RN
7270}
7271
fa4da7b3 7272proc unselectline {} {
50b44ece 7273 global selectedline currentid
fa4da7b3 7274
94b4a69f 7275 set selectedline {}
50b44ece 7276 catch {unset currentid}
fa4da7b3 7277 allcanvs delete secsel
164ff275 7278 rhighlight_none
fa4da7b3
PM
7279}
7280
f8b28a40
PM
7281proc reselectline {} {
7282 global selectedline
7283
94b4a69f 7284 if {$selectedline ne {}} {
f8b28a40
PM
7285 selectline $selectedline 0
7286 }
7287}
7288
354af6bd 7289proc addtohistory {cmd {saveproc {}}} {
2516dae2 7290 global history historyindex curview
fa4da7b3 7291
354af6bd
PM
7292 unset_posvars
7293 save_position
7294 set elt [list $curview $cmd $saveproc {}]
fa4da7b3 7295 if {$historyindex > 0
2516dae2 7296 && [lindex $history [expr {$historyindex - 1}]] == $elt} {
fa4da7b3
PM
7297 return
7298 }
7299
7300 if {$historyindex < [llength $history]} {
2516dae2 7301 set history [lreplace $history $historyindex end $elt]
fa4da7b3 7302 } else {
2516dae2 7303 lappend history $elt
fa4da7b3
PM
7304 }
7305 incr historyindex
7306 if {$historyindex > 1} {
e9937d2a 7307 .tf.bar.leftbut conf -state normal
fa4da7b3 7308 } else {
e9937d2a 7309 .tf.bar.leftbut conf -state disabled
fa4da7b3 7310 }
e9937d2a 7311 .tf.bar.rightbut conf -state disabled
fa4da7b3
PM
7312}
7313
354af6bd
PM
7314# save the scrolling position of the diff display pane
7315proc save_position {} {
7316 global historyindex history
7317
7318 if {$historyindex < 1} return
7319 set hi [expr {$historyindex - 1}]
7320 set fn [lindex $history $hi 2]
7321 if {$fn ne {}} {
7322 lset history $hi 3 [eval $fn]
7323 }
7324}
7325
7326proc unset_posvars {} {
7327 global last_posvars
7328
7329 if {[info exists last_posvars]} {
7330 foreach {var val} $last_posvars {
7331 global $var
7332 catch {unset $var}
7333 }
7334 unset last_posvars
7335 }
7336}
7337
2516dae2 7338proc godo {elt} {
354af6bd 7339 global curview last_posvars
2516dae2
PM
7340
7341 set view [lindex $elt 0]
7342 set cmd [lindex $elt 1]
354af6bd 7343 set pv [lindex $elt 3]
2516dae2
PM
7344 if {$curview != $view} {
7345 showview $view
7346 }
354af6bd
PM
7347 unset_posvars
7348 foreach {var val} $pv {
7349 global $var
7350 set $var $val
7351 }
7352 set last_posvars $pv
2516dae2
PM
7353 eval $cmd
7354}
7355
d698206c
PM
7356proc goback {} {
7357 global history historyindex
bd441de4 7358 focus .
d698206c
PM
7359
7360 if {$historyindex > 1} {
354af6bd 7361 save_position
d698206c 7362 incr historyindex -1
2516dae2 7363 godo [lindex $history [expr {$historyindex - 1}]]
e9937d2a 7364 .tf.bar.rightbut conf -state normal
d698206c
PM
7365 }
7366 if {$historyindex <= 1} {
e9937d2a 7367 .tf.bar.leftbut conf -state disabled
d698206c
PM
7368 }
7369}
7370
7371proc goforw {} {
7372 global history historyindex
bd441de4 7373 focus .
d698206c
PM
7374
7375 if {$historyindex < [llength $history]} {
354af6bd 7376 save_position
fa4da7b3 7377 set cmd [lindex $history $historyindex]
d698206c 7378 incr historyindex
2516dae2 7379 godo $cmd
e9937d2a 7380 .tf.bar.leftbut conf -state normal
d698206c
PM
7381 }
7382 if {$historyindex >= [llength $history]} {
e9937d2a 7383 .tf.bar.rightbut conf -state disabled
d698206c 7384 }
e2ed4324
PM
7385}
7386
f8b28a40 7387proc gettree {id} {
8f489363
PM
7388 global treefilelist treeidlist diffids diffmergeid treepending
7389 global nullid nullid2
f8b28a40
PM
7390
7391 set diffids $id
7392 catch {unset diffmergeid}
7393 if {![info exists treefilelist($id)]} {
7394 if {![info exists treepending]} {
8f489363
PM
7395 if {$id eq $nullid} {
7396 set cmd [list | git ls-files]
7397 } elseif {$id eq $nullid2} {
7398 set cmd [list | git ls-files --stage -t]
219ea3a9 7399 } else {
8f489363 7400 set cmd [list | git ls-tree -r $id]
219ea3a9
PM
7401 }
7402 if {[catch {set gtf [open $cmd r]}]} {
f8b28a40
PM
7403 return
7404 }
7405 set treepending $id
7406 set treefilelist($id) {}
7407 set treeidlist($id) {}
09c7029d 7408 fconfigure $gtf -blocking 0 -encoding binary
7eb3cb9c 7409 filerun $gtf [list gettreeline $gtf $id]
f8b28a40
PM
7410 }
7411 } else {
7412 setfilelist $id
7413 }
7414}
7415
7416proc gettreeline {gtf id} {
8f489363 7417 global treefilelist treeidlist treepending cmitmode diffids nullid nullid2
f8b28a40 7418
7eb3cb9c
PM
7419 set nl 0
7420 while {[incr nl] <= 1000 && [gets $gtf line] >= 0} {
8f489363
PM
7421 if {$diffids eq $nullid} {
7422 set fname $line
7423 } else {
9396cd38
PM
7424 set i [string first "\t" $line]
7425 if {$i < 0} continue
9396cd38 7426 set fname [string range $line [expr {$i+1}] end]
f31fa2c0
PM
7427 set line [string range $line 0 [expr {$i-1}]]
7428 if {$diffids ne $nullid2 && [lindex $line 1] ne "blob"} continue
7429 set sha1 [lindex $line 2]
219ea3a9 7430 lappend treeidlist($id) $sha1
219ea3a9 7431 }
09c7029d
AG
7432 if {[string index $fname 0] eq "\""} {
7433 set fname [lindex $fname 0]
7434 }
7435 set fname [encoding convertfrom $fname]
7eb3cb9c
PM
7436 lappend treefilelist($id) $fname
7437 }
7438 if {![eof $gtf]} {
7439 return [expr {$nl >= 1000? 2: 1}]
f8b28a40 7440 }
f8b28a40
PM
7441 close $gtf
7442 unset treepending
7443 if {$cmitmode ne "tree"} {
7444 if {![info exists diffmergeid]} {
7445 gettreediffs $diffids
7446 }
7447 } elseif {$id ne $diffids} {
7448 gettree $diffids
7449 } else {
7450 setfilelist $id
7451 }
7eb3cb9c 7452 return 0
f8b28a40
PM
7453}
7454
7455proc showfile {f} {
8f489363 7456 global treefilelist treeidlist diffids nullid nullid2
7cdc3556 7457 global ctext_file_names ctext_file_lines
f8b28a40
PM
7458 global ctext commentend
7459
7460 set i [lsearch -exact $treefilelist($diffids) $f]
7461 if {$i < 0} {
7462 puts "oops, $f not in list for id $diffids"
7463 return
7464 }
8f489363
PM
7465 if {$diffids eq $nullid} {
7466 if {[catch {set bf [open $f r]} err]} {
7467 puts "oops, can't read $f: $err"
219ea3a9
PM
7468 return
7469 }
7470 } else {
8f489363
PM
7471 set blob [lindex $treeidlist($diffids) $i]
7472 if {[catch {set bf [open [concat | git cat-file blob $blob] r]} err]} {
7473 puts "oops, error reading blob $blob: $err"
219ea3a9
PM
7474 return
7475 }
f8b28a40 7476 }
09c7029d 7477 fconfigure $bf -blocking 0 -encoding [get_path_encoding $f]
7eb3cb9c 7478 filerun $bf [list getblobline $bf $diffids]
f8b28a40 7479 $ctext config -state normal
3ea06f9f 7480 clear_ctext $commentend
7cdc3556
AG
7481 lappend ctext_file_names $f
7482 lappend ctext_file_lines [lindex [split $commentend "."] 0]
f8b28a40
PM
7483 $ctext insert end "\n"
7484 $ctext insert end "$f\n" filesep
7485 $ctext config -state disabled
7486 $ctext yview $commentend
32f1b3e4 7487 settabs 0
f8b28a40
PM
7488}
7489
7490proc getblobline {bf id} {
7491 global diffids cmitmode ctext
7492
7493 if {$id ne $diffids || $cmitmode ne "tree"} {
7494 catch {close $bf}
7eb3cb9c 7495 return 0
f8b28a40
PM
7496 }
7497 $ctext config -state normal
7eb3cb9c
PM
7498 set nl 0
7499 while {[incr nl] <= 1000 && [gets $bf line] >= 0} {
f8b28a40
PM
7500 $ctext insert end "$line\n"
7501 }
7502 if {[eof $bf]} {
8a897742
PM
7503 global jump_to_here ctext_file_names commentend
7504
f8b28a40
PM
7505 # delete last newline
7506 $ctext delete "end - 2c" "end - 1c"
7507 close $bf
8a897742
PM
7508 if {$jump_to_here ne {} &&
7509 [lindex $jump_to_here 0] eq [lindex $ctext_file_names 0]} {
7510 set lnum [expr {[lindex $jump_to_here 1] +
7511 [lindex [split $commentend .] 0]}]
7512 mark_ctext_line $lnum
7513 }
120ea892 7514 $ctext config -state disabled
7eb3cb9c 7515 return 0
f8b28a40
PM
7516 }
7517 $ctext config -state disabled
7eb3cb9c 7518 return [expr {$nl >= 1000? 2: 1}]
f8b28a40
PM
7519}
7520
8a897742 7521proc mark_ctext_line {lnum} {
e3e901be 7522 global ctext markbgcolor
8a897742
PM
7523
7524 $ctext tag delete omark
7525 $ctext tag add omark $lnum.0 "$lnum.0 + 1 line"
e3e901be 7526 $ctext tag conf omark -background $markbgcolor
8a897742
PM
7527 $ctext see $lnum.0
7528}
7529
7fcc92bf 7530proc mergediff {id} {
8b07dca1 7531 global diffmergeid
2df6442f 7532 global diffids treediffs
8b07dca1 7533 global parents curview
e2ed4324 7534
3c461ffe 7535 set diffmergeid $id
7a1d9d14 7536 set diffids $id
2df6442f 7537 set treediffs($id) {}
7fcc92bf 7538 set np [llength $parents($curview,$id)]
32f1b3e4 7539 settabs $np
8b07dca1 7540 getblobdiffs $id
c8a4acbf
PM
7541}
7542
3c461ffe 7543proc startdiff {ids} {
8f489363 7544 global treediffs diffids treepending diffmergeid nullid nullid2
c8dfbcf9 7545
32f1b3e4 7546 settabs 1
4f2c2642 7547 set diffids $ids
3c461ffe 7548 catch {unset diffmergeid}
8f489363
PM
7549 if {![info exists treediffs($ids)] ||
7550 [lsearch -exact $ids $nullid] >= 0 ||
7551 [lsearch -exact $ids $nullid2] >= 0} {
c8dfbcf9 7552 if {![info exists treepending]} {
14c9dbd6 7553 gettreediffs $ids
c8dfbcf9
PM
7554 }
7555 } else {
14c9dbd6 7556 addtocflist $ids
c8dfbcf9
PM
7557 }
7558}
7559
65bb0bda
PT
7560# If the filename (name) is under any of the passed filter paths
7561# then return true to include the file in the listing.
7a39a17a 7562proc path_filter {filter name} {
65bb0bda 7563 set worktree [gitworktree]
7a39a17a 7564 foreach p $filter {
65bb0bda
PT
7565 set fq_p [file normalize $p]
7566 set fq_n [file normalize [file join $worktree $name]]
7567 if {[string match [file normalize $fq_p]* $fq_n]} {
7568 return 1
7a39a17a
PM
7569 }
7570 }
7571 return 0
7572}
7573
c8dfbcf9 7574proc addtocflist {ids} {
74a40c71 7575 global treediffs
7a39a17a 7576
74a40c71 7577 add_flist $treediffs($ids)
c8dfbcf9 7578 getblobdiffs $ids
d2610d11
PM
7579}
7580
219ea3a9 7581proc diffcmd {ids flags} {
b2b76d10 7582 global log_showroot nullid nullid2
219ea3a9
PM
7583
7584 set i [lsearch -exact $ids $nullid]
8f489363 7585 set j [lsearch -exact $ids $nullid2]
219ea3a9 7586 if {$i >= 0} {
8f489363
PM
7587 if {[llength $ids] > 1 && $j < 0} {
7588 # comparing working directory with some specific revision
7589 set cmd [concat | git diff-index $flags]
7590 if {$i == 0} {
7591 lappend cmd -R [lindex $ids 1]
7592 } else {
7593 lappend cmd [lindex $ids 0]
7594 }
7595 } else {
7596 # comparing working directory with index
7597 set cmd [concat | git diff-files $flags]
7598 if {$j == 1} {
7599 lappend cmd -R
7600 }
7601 }
7602 } elseif {$j >= 0} {
7603 set cmd [concat | git diff-index --cached $flags]
219ea3a9 7604 if {[llength $ids] > 1} {
8f489363 7605 # comparing index with specific revision
90a77925 7606 if {$j == 0} {
219ea3a9
PM
7607 lappend cmd -R [lindex $ids 1]
7608 } else {
7609 lappend cmd [lindex $ids 0]
7610 }
7611 } else {
8f489363 7612 # comparing index with HEAD
219ea3a9
PM
7613 lappend cmd HEAD
7614 }
7615 } else {
b2b76d10
MK
7616 if {$log_showroot} {
7617 lappend flags --root
7618 }
8f489363 7619 set cmd [concat | git diff-tree -r $flags $ids]
219ea3a9
PM
7620 }
7621 return $cmd
7622}
7623
c8dfbcf9 7624proc gettreediffs {ids} {
2c8cd905 7625 global treediff treepending limitdiffs vfilelimit curview
219ea3a9 7626
2c8cd905
FC
7627 set cmd [diffcmd $ids {--no-commit-id}]
7628 if {$limitdiffs && $vfilelimit($curview) ne {}} {
7629 set cmd [concat $cmd -- $vfilelimit($curview)]
7630 }
7631 if {[catch {set gdtf [open $cmd r]}]} return
7272131b 7632
c8dfbcf9 7633 set treepending $ids
3c461ffe 7634 set treediff {}
09c7029d 7635 fconfigure $gdtf -blocking 0 -encoding binary
7eb3cb9c 7636 filerun $gdtf [list gettreediffline $gdtf $ids]
d2610d11
PM
7637}
7638
c8dfbcf9 7639proc gettreediffline {gdtf ids} {
3c461ffe 7640 global treediff treediffs treepending diffids diffmergeid
39ee47ef 7641 global cmitmode vfilelimit curview limitdiffs perfile_attrs
3c461ffe 7642
7eb3cb9c 7643 set nr 0
4db09304 7644 set sublist {}
39ee47ef
PM
7645 set max 1000
7646 if {$perfile_attrs} {
7647 # cache_gitattr is slow, and even slower on win32 where we
7648 # have to invoke it for only about 30 paths at a time
7649 set max 500
7650 if {[tk windowingsystem] == "win32"} {
7651 set max 120
7652 }
7653 }
7654 while {[incr nr] <= $max && [gets $gdtf line] >= 0} {
9396cd38
PM
7655 set i [string first "\t" $line]
7656 if {$i >= 0} {
7657 set file [string range $line [expr {$i+1}] end]
7658 if {[string index $file 0] eq "\""} {
7659 set file [lindex $file 0]
7660 }
09c7029d 7661 set file [encoding convertfrom $file]
48a81b7c
PM
7662 if {$file ne [lindex $treediff end]} {
7663 lappend treediff $file
7664 lappend sublist $file
7665 }
9396cd38 7666 }
7eb3cb9c 7667 }
39ee47ef
PM
7668 if {$perfile_attrs} {
7669 cache_gitattr encoding $sublist
7670 }
7eb3cb9c 7671 if {![eof $gdtf]} {
39ee47ef 7672 return [expr {$nr >= $max? 2: 1}]
7eb3cb9c
PM
7673 }
7674 close $gdtf
2c8cd905 7675 set treediffs($ids) $treediff
7eb3cb9c 7676 unset treepending
e1160138 7677 if {$cmitmode eq "tree" && [llength $diffids] == 1} {
7eb3cb9c
PM
7678 gettree $diffids
7679 } elseif {$ids != $diffids} {
7680 if {![info exists diffmergeid]} {
7681 gettreediffs $diffids
b74fd579 7682 }
7eb3cb9c
PM
7683 } else {
7684 addtocflist $ids
d2610d11 7685 }
7eb3cb9c 7686 return 0
d2610d11
PM
7687}
7688
890fae70
SP
7689# empty string or positive integer
7690proc diffcontextvalidate {v} {
7691 return [regexp {^(|[1-9][0-9]*)$} $v]
7692}
7693
7694proc diffcontextchange {n1 n2 op} {
7695 global diffcontextstring diffcontext
7696
7697 if {[string is integer -strict $diffcontextstring]} {
a41ddbb6 7698 if {$diffcontextstring >= 0} {
890fae70
SP
7699 set diffcontext $diffcontextstring
7700 reselectline
7701 }
7702 }
7703}
7704
b9b86007
SP
7705proc changeignorespace {} {
7706 reselectline
7707}
7708
ae4e3ff9
TR
7709proc changeworddiff {name ix op} {
7710 reselectline
7711}
7712
c8dfbcf9 7713proc getblobdiffs {ids} {
8d73b242 7714 global blobdifffd diffids env
7eb3cb9c 7715 global diffinhdr treediffs
890fae70 7716 global diffcontext
b9b86007 7717 global ignorespace
ae4e3ff9 7718 global worddiff
3ed31a81 7719 global limitdiffs vfilelimit curview
8b07dca1 7720 global diffencoding targetline diffnparents
a1d383c5 7721 global git_version currdiffsubmod
c8dfbcf9 7722
a8138733
PM
7723 set textconv {}
7724 if {[package vcompare $git_version "1.6.1"] >= 0} {
7725 set textconv "--textconv"
7726 }
5c838d23
JL
7727 set submodule {}
7728 if {[package vcompare $git_version "1.6.6"] >= 0} {
7729 set submodule "--submodule"
7730 }
7731 set cmd [diffcmd $ids "-p $textconv $submodule -C --cc --no-commit-id -U$diffcontext"]
b9b86007
SP
7732 if {$ignorespace} {
7733 append cmd " -w"
7734 }
ae4e3ff9
TR
7735 if {$worddiff ne [mc "Line diff"]} {
7736 append cmd " --word-diff=porcelain"
7737 }
3ed31a81
PM
7738 if {$limitdiffs && $vfilelimit($curview) ne {}} {
7739 set cmd [concat $cmd -- $vfilelimit($curview)]
7a39a17a
PM
7740 }
7741 if {[catch {set bdf [open $cmd r]} err]} {
8b07dca1 7742 error_popup [mc "Error getting diffs: %s" $err]
e5c2d856
PM
7743 return
7744 }
8a897742 7745 set targetline {}
8b07dca1 7746 set diffnparents 0
4f2c2642 7747 set diffinhdr 0
09c7029d 7748 set diffencoding [get_path_encoding {}]
681c3290 7749 fconfigure $bdf -blocking 0 -encoding binary -eofchar {}
c8dfbcf9 7750 set blobdifffd($ids) $bdf
a1d383c5 7751 set currdiffsubmod ""
7eb3cb9c 7752 filerun $bdf [list getblobdiffline $bdf $diffids]
e5c2d856
PM
7753}
7754
354af6bd
PM
7755proc savecmitpos {} {
7756 global ctext cmitmode
7757
7758 if {$cmitmode eq "tree"} {
7759 return {}
7760 }
7761 return [list target_scrollpos [$ctext index @0,0]]
7762}
7763
7764proc savectextpos {} {
7765 global ctext
7766
7767 return [list target_scrollpos [$ctext index @0,0]]
7768}
7769
7770proc maybe_scroll_ctext {ateof} {
7771 global ctext target_scrollpos
7772
7773 if {![info exists target_scrollpos]} return
7774 if {!$ateof} {
7775 set nlines [expr {[winfo height $ctext]
7776 / [font metrics textfont -linespace]}]
7777 if {[$ctext compare "$target_scrollpos + $nlines lines" <= end]} return
7778 }
7779 $ctext yview $target_scrollpos
7780 unset target_scrollpos
7781}
7782
89b11d3b
PM
7783proc setinlist {var i val} {
7784 global $var
7785
7786 while {[llength [set $var]] < $i} {
7787 lappend $var {}
7788 }
7789 if {[llength [set $var]] == $i} {
7790 lappend $var $val
7791 } else {
7792 lset $var $i $val
7793 }
7794}
7795
9396cd38 7796proc makediffhdr {fname ids} {
8b07dca1 7797 global ctext curdiffstart treediffs diffencoding
8a897742 7798 global ctext_file_names jump_to_here targetline diffline
9396cd38 7799
8b07dca1
PM
7800 set fname [encoding convertfrom $fname]
7801 set diffencoding [get_path_encoding $fname]
9396cd38
PM
7802 set i [lsearch -exact $treediffs($ids) $fname]
7803 if {$i >= 0} {
7804 setinlist difffilestart $i $curdiffstart
7805 }
48a81b7c 7806 lset ctext_file_names end $fname
9396cd38
PM
7807 set l [expr {(78 - [string length $fname]) / 2}]
7808 set pad [string range "----------------------------------------" 1 $l]
7809 $ctext insert $curdiffstart "$pad $fname $pad" filesep
8a897742
PM
7810 set targetline {}
7811 if {$jump_to_here ne {} && [lindex $jump_to_here 0] eq $fname} {
7812 set targetline [lindex $jump_to_here 1]
7813 }
7814 set diffline 0
9396cd38
PM
7815}
7816
c8dfbcf9 7817proc getblobdiffline {bdf ids} {
9396cd38 7818 global diffids blobdifffd ctext curdiffstart
7eab2933 7819 global diffnexthead diffnextnote difffilestart
7cdc3556 7820 global ctext_file_names ctext_file_lines
8b07dca1 7821 global diffinhdr treediffs mergemax diffnparents
a1d383c5 7822 global diffencoding jump_to_here targetline diffline currdiffsubmod
ae4e3ff9 7823 global worddiff
c8dfbcf9 7824
7eb3cb9c 7825 set nr 0
e5c2d856 7826 $ctext conf -state normal
7eb3cb9c
PM
7827 while {[incr nr] <= 1000 && [gets $bdf line] >= 0} {
7828 if {$ids != $diffids || $bdf != $blobdifffd($ids)} {
c21398be 7829 catch {close $bdf}
7eb3cb9c 7830 return 0
89b11d3b 7831 }
8b07dca1
PM
7832 if {![string compare -length 5 "diff " $line]} {
7833 if {![regexp {^diff (--cc|--git) } $line m type]} {
7834 set line [encoding convertfrom $line]
7835 $ctext insert end "$line\n" hunksep
7836 continue
7837 }
7eb3cb9c 7838 # start of a new file
8b07dca1 7839 set diffinhdr 1
7eb3cb9c 7840 $ctext insert end "\n"
9396cd38 7841 set curdiffstart [$ctext index "end - 1c"]
7cdc3556
AG
7842 lappend ctext_file_names ""
7843 lappend ctext_file_lines [lindex [split $curdiffstart "."] 0]
9396cd38 7844 $ctext insert end "\n" filesep
8b07dca1
PM
7845
7846 if {$type eq "--cc"} {
7847 # start of a new file in a merge diff
7848 set fname [string range $line 10 end]
7849 if {[lsearch -exact $treediffs($ids) $fname] < 0} {
7850 lappend treediffs($ids) $fname
7851 add_flist [list $fname]
7852 }
7853
9396cd38 7854 } else {
8b07dca1
PM
7855 set line [string range $line 11 end]
7856 # If the name hasn't changed the length will be odd,
7857 # the middle char will be a space, and the two bits either
7858 # side will be a/name and b/name, or "a/name" and "b/name".
7859 # If the name has changed we'll get "rename from" and
7860 # "rename to" or "copy from" and "copy to" lines following
7861 # this, and we'll use them to get the filenames.
7862 # This complexity is necessary because spaces in the
7863 # filename(s) don't get escaped.
7864 set l [string length $line]
7865 set i [expr {$l / 2}]
7866 if {!(($l & 1) && [string index $line $i] eq " " &&
7867 [string range $line 2 [expr {$i - 1}]] eq \
7868 [string range $line [expr {$i + 3}] end])} {
7869 continue
7870 }
7871 # unescape if quoted and chop off the a/ from the front
7872 if {[string index $line 0] eq "\""} {
7873 set fname [string range [lindex $line 0] 2 end]
7874 } else {
7875 set fname [string range $line 2 [expr {$i - 1}]]
7876 }
7eb3cb9c 7877 }
9396cd38
PM
7878 makediffhdr $fname $ids
7879
48a81b7c
PM
7880 } elseif {![string compare -length 16 "* Unmerged path " $line]} {
7881 set fname [encoding convertfrom [string range $line 16 end]]
7882 $ctext insert end "\n"
7883 set curdiffstart [$ctext index "end - 1c"]
7884 lappend ctext_file_names $fname
7885 lappend ctext_file_lines [lindex [split $curdiffstart "."] 0]
7886 $ctext insert end "$line\n" filesep
7887 set i [lsearch -exact $treediffs($ids) $fname]
7888 if {$i >= 0} {
7889 setinlist difffilestart $i $curdiffstart
7890 }
7891
8b07dca1
PM
7892 } elseif {![string compare -length 2 "@@" $line]} {
7893 regexp {^@@+} $line ats
09c7029d 7894 set line [encoding convertfrom $diffencoding $line]
7eb3cb9c 7895 $ctext insert end "$line\n" hunksep
8b07dca1
PM
7896 if {[regexp { \+(\d+),\d+ @@} $line m nl]} {
7897 set diffline $nl
7898 }
7899 set diffnparents [expr {[string length $ats] - 1}]
7eb3cb9c 7900 set diffinhdr 0
9396cd38 7901
5c838d23
JL
7902 } elseif {![string compare -length 10 "Submodule " $line]} {
7903 # start of a new submodule
a1d383c5
JL
7904 if {[regexp -indices "\[0-9a-f\]+\\.\\." $line nameend]} {
7905 set fname [string range $line 10 [expr [lindex $nameend 0] - 2]]
7906 } else {
7907 set fname [string range $line 10 [expr [string first "contains " $line] - 2]]
7908 }
7909 if {$currdiffsubmod != $fname} {
5c838d23
JL
7910 $ctext insert end "\n"; # Add newline after commit message
7911 }
7912 set curdiffstart [$ctext index "end - 1c"]
7913 lappend ctext_file_names ""
a1d383c5
JL
7914 if {$currdiffsubmod != $fname} {
7915 lappend ctext_file_lines $fname
7916 makediffhdr $fname $ids
7917 set currdiffsubmod $fname
7918 $ctext insert end "\n$line\n" filesep
7919 } else {
7920 $ctext insert end "$line\n" filesep
7921 }
5c838d23 7922 } elseif {![string compare -length 3 " >" $line]} {
a1d383c5 7923 set $currdiffsubmod ""
1f2cecfd 7924 set line [encoding convertfrom $diffencoding $line]
5c838d23
JL
7925 $ctext insert end "$line\n" dresult
7926 } elseif {![string compare -length 3 " <" $line]} {
a1d383c5 7927 set $currdiffsubmod ""
1f2cecfd 7928 set line [encoding convertfrom $diffencoding $line]
5c838d23 7929 $ctext insert end "$line\n" d0
9396cd38 7930 } elseif {$diffinhdr} {
5e85ec4c 7931 if {![string compare -length 12 "rename from " $line]} {
d1cb298b 7932 set fname [string range $line [expr 6 + [string first " from " $line] ] end]
9396cd38
PM
7933 if {[string index $fname 0] eq "\""} {
7934 set fname [lindex $fname 0]
7935 }
09c7029d 7936 set fname [encoding convertfrom $fname]
9396cd38
PM
7937 set i [lsearch -exact $treediffs($ids) $fname]
7938 if {$i >= 0} {
7939 setinlist difffilestart $i $curdiffstart
7940 }
d1cb298b
JS
7941 } elseif {![string compare -length 10 $line "rename to "] ||
7942 ![string compare -length 8 $line "copy to "]} {
7943 set fname [string range $line [expr 4 + [string first " to " $line] ] end]
9396cd38
PM
7944 if {[string index $fname 0] eq "\""} {
7945 set fname [lindex $fname 0]
7946 }
7947 makediffhdr $fname $ids
7948 } elseif {[string compare -length 3 $line "---"] == 0} {
7949 # do nothing
7950 continue
7951 } elseif {[string compare -length 3 $line "+++"] == 0} {
7952 set diffinhdr 0
7953 continue
7954 }
7955 $ctext insert end "$line\n" filesep
7956
e5c2d856 7957 } else {
681c3290
PT
7958 set line [string map {\x1A ^Z} \
7959 [encoding convertfrom $diffencoding $line]]
8b07dca1
PM
7960 # parse the prefix - one ' ', '-' or '+' for each parent
7961 set prefix [string range $line 0 [expr {$diffnparents - 1}]]
7962 set tag [expr {$diffnparents > 1? "m": "d"}]
ae4e3ff9
TR
7963 set dowords [expr {$worddiff ne [mc "Line diff"] && $diffnparents == 1}]
7964 set words_pre_markup ""
7965 set words_post_markup ""
8b07dca1
PM
7966 if {[string trim $prefix " -+"] eq {}} {
7967 # prefix only has " ", "-" and "+" in it: normal diff line
7968 set num [string first "-" $prefix]
ae4e3ff9
TR
7969 if {$dowords} {
7970 set line [string range $line 1 end]
7971 }
8b07dca1
PM
7972 if {$num >= 0} {
7973 # removed line, first parent with line is $num
7974 if {$num >= $mergemax} {
7975 set num "max"
7976 }
ae4e3ff9
TR
7977 if {$dowords && $worddiff eq [mc "Markup words"]} {
7978 $ctext insert end "\[-$line-\]" $tag$num
7979 } else {
7980 $ctext insert end "$line" $tag$num
7981 }
7982 if {!$dowords} {
7983 $ctext insert end "\n" $tag$num
7984 }
8b07dca1
PM
7985 } else {
7986 set tags {}
7987 if {[string first "+" $prefix] >= 0} {
7988 # added line
7989 lappend tags ${tag}result
7990 if {$diffnparents > 1} {
7991 set num [string first " " $prefix]
7992 if {$num >= 0} {
7993 if {$num >= $mergemax} {
7994 set num "max"
7995 }
7996 lappend tags m$num
7997 }
7998 }
ae4e3ff9
TR
7999 set words_pre_markup "{+"
8000 set words_post_markup "+}"
8b07dca1
PM
8001 }
8002 if {$targetline ne {}} {
8003 if {$diffline == $targetline} {
8004 set seehere [$ctext index "end - 1 chars"]
8005 set targetline {}
8006 } else {
8007 incr diffline
8008 }
8009 }
ae4e3ff9
TR
8010 if {$dowords && $worddiff eq [mc "Markup words"]} {
8011 $ctext insert end "$words_pre_markup$line$words_post_markup" $tags
8012 } else {
8013 $ctext insert end "$line" $tags
8014 }
8015 if {!$dowords} {
8016 $ctext insert end "\n" $tags
8017 }
8b07dca1 8018 }
ae4e3ff9
TR
8019 } elseif {$dowords && $prefix eq "~"} {
8020 $ctext insert end "\n" {}
7eb3cb9c 8021 } else {
9396cd38
PM
8022 # "\ No newline at end of file",
8023 # or something else we don't recognize
8024 $ctext insert end "$line\n" hunksep
e5c2d856 8025 }
e5c2d856
PM
8026 }
8027 }
8b07dca1
PM
8028 if {[info exists seehere]} {
8029 mark_ctext_line [lindex [split $seehere .] 0]
8030 }
354af6bd 8031 maybe_scroll_ctext [eof $bdf]
e5c2d856 8032 $ctext conf -state disabled
7eb3cb9c 8033 if {[eof $bdf]} {
c21398be 8034 catch {close $bdf}
7eb3cb9c 8035 return 0
c8dfbcf9 8036 }
7eb3cb9c 8037 return [expr {$nr >= 1000? 2: 1}]
e5c2d856
PM
8038}
8039
a8d610a2
PM
8040proc changediffdisp {} {
8041 global ctext diffelide
8042
8043 $ctext tag conf d0 -elide [lindex $diffelide 0]
8b07dca1 8044 $ctext tag conf dresult -elide [lindex $diffelide 1]
a8d610a2
PM
8045}
8046
b967135d
SH
8047proc highlightfile {cline} {
8048 global cflist cflist_top
f4c54b3c 8049
ce837c9d
SH
8050 if {![info exists cflist_top]} return
8051
f4c54b3c
PM
8052 $cflist tag remove highlight $cflist_top.0 "$cflist_top.0 lineend"
8053 $cflist tag add highlight $cline.0 "$cline.0 lineend"
8054 $cflist see $cline.0
8055 set cflist_top $cline
8056}
8057
b967135d 8058proc highlightfile_for_scrollpos {topidx} {
978904bf 8059 global cmitmode difffilestart
b967135d 8060
978904bf 8061 if {$cmitmode eq "tree"} return
b967135d
SH
8062 if {![info exists difffilestart]} return
8063
8064 set top [lindex [split $topidx .] 0]
8065 if {$difffilestart eq {} || $top < [lindex $difffilestart 0]} {
8066 highlightfile 0
8067 } else {
8068 highlightfile [expr {[bsearch $difffilestart $top] + 2}]
8069 }
8070}
8071
67c22874 8072proc prevfile {} {
f4c54b3c
PM
8073 global difffilestart ctext cmitmode
8074
8075 if {$cmitmode eq "tree"} return
8076 set prev 0.0
67c22874
OH
8077 set here [$ctext index @0,0]
8078 foreach loc $difffilestart {
8079 if {[$ctext compare $loc >= $here]} {
b967135d 8080 $ctext yview $prev
67c22874
OH
8081 return
8082 }
8083 set prev $loc
8084 }
b967135d 8085 $ctext yview $prev
67c22874
OH
8086}
8087
39ad8570 8088proc nextfile {} {
f4c54b3c
PM
8089 global difffilestart ctext cmitmode
8090
8091 if {$cmitmode eq "tree"} return
39ad8570 8092 set here [$ctext index @0,0]
7fcceed7
PM
8093 foreach loc $difffilestart {
8094 if {[$ctext compare $loc > $here]} {
b967135d 8095 $ctext yview $loc
67c22874 8096 return
39ad8570
PM
8097 }
8098 }
1db95b00
PM
8099}
8100
3ea06f9f
PM
8101proc clear_ctext {{first 1.0}} {
8102 global ctext smarktop smarkbot
7cdc3556 8103 global ctext_file_names ctext_file_lines
97645683 8104 global pendinglinks
3ea06f9f 8105
1902c270
PM
8106 set l [lindex [split $first .] 0]
8107 if {![info exists smarktop] || [$ctext compare $first < $smarktop.0]} {
8108 set smarktop $l
3ea06f9f 8109 }
1902c270
PM
8110 if {![info exists smarkbot] || [$ctext compare $first < $smarkbot.0]} {
8111 set smarkbot $l
3ea06f9f
PM
8112 }
8113 $ctext delete $first end
97645683
PM
8114 if {$first eq "1.0"} {
8115 catch {unset pendinglinks}
8116 }
7cdc3556
AG
8117 set ctext_file_names {}
8118 set ctext_file_lines {}
3ea06f9f
PM
8119}
8120
32f1b3e4 8121proc settabs {{firstab {}}} {
9c311b32 8122 global firsttabstop tabstop ctext have_tk85
32f1b3e4
PM
8123
8124 if {$firstab ne {} && $have_tk85} {
8125 set firsttabstop $firstab
8126 }
9c311b32 8127 set w [font measure textfont "0"]
32f1b3e4 8128 if {$firsttabstop != 0} {
64b5f146
PM
8129 $ctext conf -tabs [list [expr {($firsttabstop + $tabstop) * $w}] \
8130 [expr {($firsttabstop + 2 * $tabstop) * $w}]]
32f1b3e4
PM
8131 } elseif {$have_tk85 || $tabstop != 8} {
8132 $ctext conf -tabs [expr {$tabstop * $w}]
8133 } else {
8134 $ctext conf -tabs {}
8135 }
3ea06f9f
PM
8136}
8137
8138proc incrsearch {name ix op} {
1902c270 8139 global ctext searchstring searchdirn
3ea06f9f 8140
1902c270
PM
8141 if {[catch {$ctext index anchor}]} {
8142 # no anchor set, use start of selection, or of visible area
8143 set sel [$ctext tag ranges sel]
8144 if {$sel ne {}} {
8145 $ctext mark set anchor [lindex $sel 0]
8146 } elseif {$searchdirn eq "-forwards"} {
8147 $ctext mark set anchor @0,0
8148 } else {
8149 $ctext mark set anchor @0,[winfo height $ctext]
8150 }
8151 }
3ea06f9f 8152 if {$searchstring ne {}} {
30441a6f 8153 set here [$ctext search -count mlen $searchdirn -- $searchstring anchor]
1902c270
PM
8154 if {$here ne {}} {
8155 $ctext see $here
30441a6f
SH
8156 set mend "$here + $mlen c"
8157 $ctext tag remove sel 1.0 end
8158 $ctext tag add sel $here $mend
b967135d
SH
8159 suppress_highlighting_file_for_current_scrollpos
8160 highlightfile_for_scrollpos $here
1902c270 8161 }
3ea06f9f 8162 }
c4614994 8163 rehighlight_search_results
3ea06f9f
PM
8164}
8165
8166proc dosearch {} {
1902c270 8167 global sstring ctext searchstring searchdirn
3ea06f9f
PM
8168
8169 focus $sstring
8170 $sstring icursor end
1902c270
PM
8171 set searchdirn -forwards
8172 if {$searchstring ne {}} {
8173 set sel [$ctext tag ranges sel]
8174 if {$sel ne {}} {
8175 set start "[lindex $sel 0] + 1c"
8176 } elseif {[catch {set start [$ctext index anchor]}]} {
8177 set start "@0,0"
8178 }
8179 set match [$ctext search -count mlen -- $searchstring $start]
8180 $ctext tag remove sel 1.0 end
8181 if {$match eq {}} {
8182 bell
8183 return
8184 }
8185 $ctext see $match
b967135d
SH
8186 suppress_highlighting_file_for_current_scrollpos
8187 highlightfile_for_scrollpos $match
1902c270
PM
8188 set mend "$match + $mlen c"
8189 $ctext tag add sel $match $mend
8190 $ctext mark unset anchor
c4614994 8191 rehighlight_search_results
1902c270
PM
8192 }
8193}
8194
8195proc dosearchback {} {
8196 global sstring ctext searchstring searchdirn
8197
8198 focus $sstring
8199 $sstring icursor end
8200 set searchdirn -backwards
8201 if {$searchstring ne {}} {
8202 set sel [$ctext tag ranges sel]
8203 if {$sel ne {}} {
8204 set start [lindex $sel 0]
8205 } elseif {[catch {set start [$ctext index anchor]}]} {
8206 set start @0,[winfo height $ctext]
8207 }
8208 set match [$ctext search -backwards -count ml -- $searchstring $start]
8209 $ctext tag remove sel 1.0 end
8210 if {$match eq {}} {
8211 bell
8212 return
8213 }
8214 $ctext see $match
b967135d
SH
8215 suppress_highlighting_file_for_current_scrollpos
8216 highlightfile_for_scrollpos $match
1902c270
PM
8217 set mend "$match + $ml c"
8218 $ctext tag add sel $match $mend
8219 $ctext mark unset anchor
c4614994
SH
8220 rehighlight_search_results
8221 }
8222}
8223
8224proc rehighlight_search_results {} {
8225 global ctext searchstring
8226
8227 $ctext tag remove found 1.0 end
8228 $ctext tag remove currentsearchhit 1.0 end
8229
8230 if {$searchstring ne {}} {
8231 searchmarkvisible 1
3ea06f9f 8232 }
3ea06f9f
PM
8233}
8234
8235proc searchmark {first last} {
8236 global ctext searchstring
8237
c4614994
SH
8238 set sel [$ctext tag ranges sel]
8239
3ea06f9f
PM
8240 set mend $first.0
8241 while {1} {
8242 set match [$ctext search -count mlen -- $searchstring $mend $last.end]
8243 if {$match eq {}} break
8244 set mend "$match + $mlen c"
c4614994
SH
8245 if {$sel ne {} && [$ctext compare $match == [lindex $sel 0]]} {
8246 $ctext tag add currentsearchhit $match $mend
8247 } else {
8248 $ctext tag add found $match $mend
8249 }
3ea06f9f
PM
8250 }
8251}
8252
8253proc searchmarkvisible {doall} {
8254 global ctext smarktop smarkbot
8255
8256 set topline [lindex [split [$ctext index @0,0] .] 0]
8257 set botline [lindex [split [$ctext index @0,[winfo height $ctext]] .] 0]
8258 if {$doall || $botline < $smarktop || $topline > $smarkbot} {
8259 # no overlap with previous
8260 searchmark $topline $botline
8261 set smarktop $topline
8262 set smarkbot $botline
8263 } else {
8264 if {$topline < $smarktop} {
8265 searchmark $topline [expr {$smarktop-1}]
8266 set smarktop $topline
8267 }
8268 if {$botline > $smarkbot} {
8269 searchmark [expr {$smarkbot+1}] $botline
8270 set smarkbot $botline
8271 }
8272 }
8273}
8274
b967135d
SH
8275proc suppress_highlighting_file_for_current_scrollpos {} {
8276 global ctext suppress_highlighting_file_for_this_scrollpos
8277
8278 set suppress_highlighting_file_for_this_scrollpos [$ctext index @0,0]
8279}
8280
3ea06f9f 8281proc scrolltext {f0 f1} {
b967135d
SH
8282 global searchstring cmitmode ctext
8283 global suppress_highlighting_file_for_this_scrollpos
8284
978904bf
SH
8285 set topidx [$ctext index @0,0]
8286 if {![info exists suppress_highlighting_file_for_this_scrollpos]
8287 || $topidx ne $suppress_highlighting_file_for_this_scrollpos} {
8288 highlightfile_for_scrollpos $topidx
b967135d
SH
8289 }
8290
8291 catch {unset suppress_highlighting_file_for_this_scrollpos}
3ea06f9f 8292
8809d691 8293 .bleft.bottom.sb set $f0 $f1
3ea06f9f
PM
8294 if {$searchstring ne {}} {
8295 searchmarkvisible 0
8296 }
8297}
8298
1d10f36d 8299proc setcoords {} {
9c311b32 8300 global linespc charspc canvx0 canvy0
f6075eba 8301 global xspc1 xspc2 lthickness
8d858d1a 8302
9c311b32
PM
8303 set linespc [font metrics mainfont -linespace]
8304 set charspc [font measure mainfont "m"]
9f1afe05
PM
8305 set canvy0 [expr {int(3 + 0.5 * $linespc)}]
8306 set canvx0 [expr {int(3 + 0.5 * $linespc)}]
f6075eba 8307 set lthickness [expr {int($linespc / 9) + 1}]
8d858d1a
PM
8308 set xspc1(0) $linespc
8309 set xspc2 $linespc
9a40c50c 8310}
1db95b00 8311
1d10f36d 8312proc redisplay {} {
be0cd098 8313 global canv
9f1afe05
PM
8314 global selectedline
8315
8316 set ymax [lindex [$canv cget -scrollregion] 3]
8317 if {$ymax eq {} || $ymax == 0} return
8318 set span [$canv yview]
8319 clear_display
be0cd098 8320 setcanvscroll
9f1afe05
PM
8321 allcanvs yview moveto [lindex $span 0]
8322 drawvisible
94b4a69f 8323 if {$selectedline ne {}} {
9f1afe05 8324 selectline $selectedline 0
ca6d8f58 8325 allcanvs yview moveto [lindex $span 0]
1d10f36d
PM
8326 }
8327}
8328
0ed1dd3c
PM
8329proc parsefont {f n} {
8330 global fontattr
8331
8332 set fontattr($f,family) [lindex $n 0]
8333 set s [lindex $n 1]
8334 if {$s eq {} || $s == 0} {
8335 set s 10
8336 } elseif {$s < 0} {
8337 set s [expr {int(-$s / [winfo fpixels . 1p] + 0.5)}]
9c311b32 8338 }
0ed1dd3c
PM
8339 set fontattr($f,size) $s
8340 set fontattr($f,weight) normal
8341 set fontattr($f,slant) roman
8342 foreach style [lrange $n 2 end] {
8343 switch -- $style {
8344 "normal" -
8345 "bold" {set fontattr($f,weight) $style}
8346 "roman" -
8347 "italic" {set fontattr($f,slant) $style}
8348 }
9c311b32 8349 }
0ed1dd3c
PM
8350}
8351
8352proc fontflags {f {isbold 0}} {
8353 global fontattr
8354
8355 return [list -family $fontattr($f,family) -size $fontattr($f,size) \
8356 -weight [expr {$isbold? "bold": $fontattr($f,weight)}] \
8357 -slant $fontattr($f,slant)]
8358}
8359
8360proc fontname {f} {
8361 global fontattr
8362
8363 set n [list $fontattr($f,family) $fontattr($f,size)]
8364 if {$fontattr($f,weight) eq "bold"} {
8365 lappend n "bold"
9c311b32 8366 }
0ed1dd3c
PM
8367 if {$fontattr($f,slant) eq "italic"} {
8368 lappend n "italic"
9c311b32 8369 }
0ed1dd3c 8370 return $n
9c311b32
PM
8371}
8372
1d10f36d 8373proc incrfont {inc} {
7fcc92bf 8374 global mainfont textfont ctext canv cflist showrefstop
0ed1dd3c
PM
8375 global stopped entries fontattr
8376
1d10f36d 8377 unmarkmatches
0ed1dd3c 8378 set s $fontattr(mainfont,size)
9c311b32
PM
8379 incr s $inc
8380 if {$s < 1} {
8381 set s 1
8382 }
0ed1dd3c 8383 set fontattr(mainfont,size) $s
9c311b32
PM
8384 font config mainfont -size $s
8385 font config mainfontbold -size $s
0ed1dd3c
PM
8386 set mainfont [fontname mainfont]
8387 set s $fontattr(textfont,size)
9c311b32
PM
8388 incr s $inc
8389 if {$s < 1} {
8390 set s 1
8391 }
0ed1dd3c 8392 set fontattr(textfont,size) $s
9c311b32
PM
8393 font config textfont -size $s
8394 font config textfontbold -size $s
0ed1dd3c 8395 set textfont [fontname textfont]
1d10f36d 8396 setcoords
32f1b3e4 8397 settabs
1d10f36d
PM
8398 redisplay
8399}
1db95b00 8400
ee3dc72e
PM
8401proc clearsha1 {} {
8402 global sha1entry sha1string
8403 if {[string length $sha1string] == 40} {
8404 $sha1entry delete 0 end
8405 }
8406}
8407
887fe3c4
PM
8408proc sha1change {n1 n2 op} {
8409 global sha1string currentid sha1but
8410 if {$sha1string == {}
8411 || ([info exists currentid] && $sha1string == $currentid)} {
8412 set state disabled
8413 } else {
8414 set state normal
8415 }
8416 if {[$sha1but cget -state] == $state} return
8417 if {$state == "normal"} {
d990cedf 8418 $sha1but conf -state normal -relief raised -text "[mc "Goto:"] "
887fe3c4 8419 } else {
d990cedf 8420 $sha1but conf -state disabled -relief flat -text "[mc "SHA1 ID:"] "
887fe3c4
PM
8421 }
8422}
8423
8424proc gotocommit {} {
7fcc92bf 8425 global sha1string tagids headids curview varcid
f3b8b3ce 8426
887fe3c4
PM
8427 if {$sha1string == {}
8428 || ([info exists currentid] && $sha1string == $currentid)} return
8429 if {[info exists tagids($sha1string)]} {
8430 set id $tagids($sha1string)
e1007129
SR
8431 } elseif {[info exists headids($sha1string)]} {
8432 set id $headids($sha1string)
887fe3c4
PM
8433 } else {
8434 set id [string tolower $sha1string]
f3b8b3ce 8435 if {[regexp {^[0-9a-f]{4,39}$} $id]} {
d375ef9b 8436 set matches [longid $id]
f3b8b3ce
PM
8437 if {$matches ne {}} {
8438 if {[llength $matches] > 1} {
d990cedf 8439 error_popup [mc "Short SHA1 id %s is ambiguous" $id]
f3b8b3ce
PM
8440 return
8441 }
d375ef9b 8442 set id [lindex $matches 0]
f3b8b3ce 8443 }
9bf3acfa
TR
8444 } else {
8445 if {[catch {set id [exec git rev-parse --verify $sha1string]}]} {
8446 error_popup [mc "Revision %s is not known" $sha1string]
8447 return
8448 }
f3b8b3ce 8449 }
887fe3c4 8450 }
7fcc92bf
PM
8451 if {[commitinview $id $curview]} {
8452 selectline [rowofcommit $id] 1
887fe3c4
PM
8453 return
8454 }
f3b8b3ce 8455 if {[regexp {^[0-9a-fA-F]{4,}$} $sha1string]} {
d990cedf 8456 set msg [mc "SHA1 id %s is not known" $sha1string]
887fe3c4 8457 } else {
9bf3acfa 8458 set msg [mc "Revision %s is not in the current view" $sha1string]
887fe3c4 8459 }
d990cedf 8460 error_popup $msg
887fe3c4
PM
8461}
8462
84ba7345
PM
8463proc lineenter {x y id} {
8464 global hoverx hovery hoverid hovertimer
8465 global commitinfo canv
8466
8ed16484 8467 if {![info exists commitinfo($id)] && ![getcommit $id]} return
84ba7345
PM
8468 set hoverx $x
8469 set hovery $y
8470 set hoverid $id
8471 if {[info exists hovertimer]} {
8472 after cancel $hovertimer
8473 }
8474 set hovertimer [after 500 linehover]
8475 $canv delete hover
8476}
8477
8478proc linemotion {x y id} {
8479 global hoverx hovery hoverid hovertimer
8480
8481 if {[info exists hoverid] && $id == $hoverid} {
8482 set hoverx $x
8483 set hovery $y
8484 if {[info exists hovertimer]} {
8485 after cancel $hovertimer
8486 }
8487 set hovertimer [after 500 linehover]
8488 }
8489}
8490
8491proc lineleave {id} {
8492 global hoverid hovertimer canv
8493
8494 if {[info exists hoverid] && $id == $hoverid} {
8495 $canv delete hover
8496 if {[info exists hovertimer]} {
8497 after cancel $hovertimer
8498 unset hovertimer
8499 }
8500 unset hoverid
8501 }
8502}
8503
8504proc linehover {} {
8505 global hoverx hovery hoverid hovertimer
8506 global canv linespc lthickness
252c52df
8507 global linehoverbgcolor linehoverfgcolor linehoveroutlinecolor
8508
9c311b32 8509 global commitinfo
84ba7345
PM
8510
8511 set text [lindex $commitinfo($hoverid) 0]
8512 set ymax [lindex [$canv cget -scrollregion] 3]
8513 if {$ymax == {}} return
8514 set yfrac [lindex [$canv yview] 0]
8515 set x [expr {$hoverx + 2 * $linespc}]
8516 set y [expr {$hovery + $yfrac * $ymax - $linespc / 2}]
8517 set x0 [expr {$x - 2 * $lthickness}]
8518 set y0 [expr {$y - 2 * $lthickness}]
9c311b32 8519 set x1 [expr {$x + [font measure mainfont $text] + 2 * $lthickness}]
84ba7345
PM
8520 set y1 [expr {$y + $linespc + 2 * $lthickness}]
8521 set t [$canv create rectangle $x0 $y0 $x1 $y1 \
252c52df
8522 -fill $linehoverbgcolor -outline $linehoveroutlinecolor \
8523 -width 1 -tags hover]
84ba7345 8524 $canv raise $t
f8a2c0d1 8525 set t [$canv create text $x $y -anchor nw -text $text -tags hover \
252c52df 8526 -font mainfont -fill $linehoverfgcolor]
84ba7345
PM
8527 $canv raise $t
8528}
8529
9843c307 8530proc clickisonarrow {id y} {
50b44ece 8531 global lthickness
9843c307 8532
50b44ece 8533 set ranges [rowranges $id]
9843c307 8534 set thresh [expr {2 * $lthickness + 6}]
50b44ece 8535 set n [expr {[llength $ranges] - 1}]
f6342480 8536 for {set i 1} {$i < $n} {incr i} {
50b44ece 8537 set row [lindex $ranges $i]
f6342480
PM
8538 if {abs([yc $row] - $y) < $thresh} {
8539 return $i
9843c307
PM
8540 }
8541 }
8542 return {}
8543}
8544
f6342480 8545proc arrowjump {id n y} {
50b44ece 8546 global canv
9843c307 8547
f6342480
PM
8548 # 1 <-> 2, 3 <-> 4, etc...
8549 set n [expr {(($n - 1) ^ 1) + 1}]
50b44ece 8550 set row [lindex [rowranges $id] $n]
f6342480 8551 set yt [yc $row]
9843c307
PM
8552 set ymax [lindex [$canv cget -scrollregion] 3]
8553 if {$ymax eq {} || $ymax <= 0} return
8554 set view [$canv yview]
8555 set yspan [expr {[lindex $view 1] - [lindex $view 0]}]
8556 set yfrac [expr {$yt / $ymax - $yspan / 2}]
8557 if {$yfrac < 0} {
8558 set yfrac 0
8559 }
f6342480 8560 allcanvs yview moveto $yfrac
9843c307
PM
8561}
8562
fa4da7b3 8563proc lineclick {x y id isnew} {
7fcc92bf 8564 global ctext commitinfo children canv thickerline curview
c8dfbcf9 8565
8ed16484 8566 if {![info exists commitinfo($id)] && ![getcommit $id]} return
c8dfbcf9 8567 unmarkmatches
fa4da7b3 8568 unselectline
9843c307
PM
8569 normalline
8570 $canv delete hover
8571 # draw this line thicker than normal
9843c307 8572 set thickerline $id
c934a8a3 8573 drawlines $id
fa4da7b3 8574 if {$isnew} {
9843c307
PM
8575 set ymax [lindex [$canv cget -scrollregion] 3]
8576 if {$ymax eq {}} return
8577 set yfrac [lindex [$canv yview] 0]
8578 set y [expr {$y + $yfrac * $ymax}]
8579 }
8580 set dirn [clickisonarrow $id $y]
8581 if {$dirn ne {}} {
8582 arrowjump $id $dirn $y
8583 return
8584 }
8585
8586 if {$isnew} {
354af6bd 8587 addtohistory [list lineclick $x $y $id 0] savectextpos
fa4da7b3 8588 }
c8dfbcf9
PM
8589 # fill the details pane with info about this line
8590 $ctext conf -state normal
3ea06f9f 8591 clear_ctext
32f1b3e4 8592 settabs 0
d990cedf 8593 $ctext insert end "[mc "Parent"]:\t"
97645683
PM
8594 $ctext insert end $id link0
8595 setlink $id link0
c8dfbcf9 8596 set info $commitinfo($id)
fa4da7b3 8597 $ctext insert end "\n\t[lindex $info 0]\n"
d990cedf 8598 $ctext insert end "\t[mc "Author"]:\t[lindex $info 1]\n"
232475d3 8599 set date [formatdate [lindex $info 2]]
d990cedf 8600 $ctext insert end "\t[mc "Date"]:\t$date\n"
da7c24dd 8601 set kids $children($curview,$id)
79b2c75e 8602 if {$kids ne {}} {
d990cedf 8603 $ctext insert end "\n[mc "Children"]:"
fa4da7b3 8604 set i 0
79b2c75e 8605 foreach child $kids {
fa4da7b3 8606 incr i
8ed16484 8607 if {![info exists commitinfo($child)] && ![getcommit $child]} continue
c8dfbcf9 8608 set info $commitinfo($child)
fa4da7b3 8609 $ctext insert end "\n\t"
97645683
PM
8610 $ctext insert end $child link$i
8611 setlink $child link$i
fa4da7b3 8612 $ctext insert end "\n\t[lindex $info 0]"
d990cedf 8613 $ctext insert end "\n\t[mc "Author"]:\t[lindex $info 1]"
232475d3 8614 set date [formatdate [lindex $info 2]]
d990cedf 8615 $ctext insert end "\n\t[mc "Date"]:\t$date\n"
c8dfbcf9
PM
8616 }
8617 }
354af6bd 8618 maybe_scroll_ctext 1
c8dfbcf9 8619 $ctext conf -state disabled
7fcceed7 8620 init_flist {}
c8dfbcf9
PM
8621}
8622
9843c307
PM
8623proc normalline {} {
8624 global thickerline
8625 if {[info exists thickerline]} {
c934a8a3 8626 set id $thickerline
9843c307 8627 unset thickerline
c934a8a3 8628 drawlines $id
9843c307
PM
8629 }
8630}
8631
354af6bd 8632proc selbyid {id {isnew 1}} {
7fcc92bf
PM
8633 global curview
8634 if {[commitinview $id $curview]} {
354af6bd 8635 selectline [rowofcommit $id] $isnew
c8dfbcf9
PM
8636 }
8637}
8638
8639proc mstime {} {
8640 global startmstime
8641 if {![info exists startmstime]} {
8642 set startmstime [clock clicks -milliseconds]
8643 }
8644 return [format "%.3f" [expr {([clock click -milliseconds] - $startmstime) / 1000.0}]]
8645}
8646
8647proc rowmenu {x y id} {
7fcc92bf 8648 global rowctxmenu selectedline rowmenuid curview
b9fdba7f 8649 global nullid nullid2 fakerowmenu mainhead markedid
c8dfbcf9 8650
bb3edc8b 8651 stopfinding
219ea3a9 8652 set rowmenuid $id
94b4a69f 8653 if {$selectedline eq {} || [rowofcommit $id] eq $selectedline} {
c8dfbcf9
PM
8654 set state disabled
8655 } else {
8656 set state normal
8657 }
6febdede
PM
8658 if {[info exists markedid] && $markedid ne $id} {
8659 set mstate normal
8660 } else {
8661 set mstate disabled
8662 }
8f489363 8663 if {$id ne $nullid && $id ne $nullid2} {
219ea3a9 8664 set menu $rowctxmenu
5e3502da 8665 if {$mainhead ne {}} {
da12e59d 8666 $menu entryconfigure 7 -label [mc "Reset %s branch to here" $mainhead] -state normal
5e3502da
MB
8667 } else {
8668 $menu entryconfigure 7 -label [mc "Detached head: can't reset" $mainhead] -state disabled
8669 }
6febdede
PM
8670 $menu entryconfigure 9 -state $mstate
8671 $menu entryconfigure 10 -state $mstate
8672 $menu entryconfigure 11 -state $mstate
219ea3a9
PM
8673 } else {
8674 set menu $fakerowmenu
8675 }
f2d0bbbd
PM
8676 $menu entryconfigure [mca "Diff this -> selected"] -state $state
8677 $menu entryconfigure [mca "Diff selected -> this"] -state $state
8678 $menu entryconfigure [mca "Make patch"] -state $state
6febdede
PM
8679 $menu entryconfigure [mca "Diff this -> marked commit"] -state $mstate
8680 $menu entryconfigure [mca "Diff marked commit -> this"] -state $mstate
219ea3a9 8681 tk_popup $menu $x $y
c8dfbcf9
PM
8682}
8683
b9fdba7f
PM
8684proc markhere {} {
8685 global rowmenuid markedid canv
8686
8687 set markedid $rowmenuid
8688 make_idmark $markedid
8689}
8690
8691proc gotomark {} {
8692 global markedid
8693
8694 if {[info exists markedid]} {
8695 selbyid $markedid
8696 }
8697}
8698
8699proc replace_by_kids {l r} {
8700 global curview children
8701
8702 set id [commitonrow $r]
8703 set l [lreplace $l 0 0]
8704 foreach kid $children($curview,$id) {
8705 lappend l [rowofcommit $kid]
8706 }
8707 return [lsort -integer -decreasing -unique $l]
8708}
8709
8710proc find_common_desc {} {
8711 global markedid rowmenuid curview children
8712
8713 if {![info exists markedid]} return
8714 if {![commitinview $markedid $curview] ||
8715 ![commitinview $rowmenuid $curview]} return
8716 #set t1 [clock clicks -milliseconds]
8717 set l1 [list [rowofcommit $markedid]]
8718 set l2 [list [rowofcommit $rowmenuid]]
8719 while 1 {
8720 set r1 [lindex $l1 0]
8721 set r2 [lindex $l2 0]
8722 if {$r1 eq {} || $r2 eq {}} break
8723 if {$r1 == $r2} {
8724 selectline $r1 1
8725 break
8726 }
8727 if {$r1 > $r2} {
8728 set l1 [replace_by_kids $l1 $r1]
8729 } else {
8730 set l2 [replace_by_kids $l2 $r2]
8731 }
8732 }
8733 #set t2 [clock clicks -milliseconds]
8734 #puts "took [expr {$t2-$t1}]ms"
8735}
8736
010509f2
PM
8737proc compare_commits {} {
8738 global markedid rowmenuid curview children
8739
8740 if {![info exists markedid]} return
8741 if {![commitinview $markedid $curview]} return
8742 addtohistory [list do_cmp_commits $markedid $rowmenuid]
8743 do_cmp_commits $markedid $rowmenuid
8744}
8745
8746proc getpatchid {id} {
8747 global patchids
8748
8749 if {![info exists patchids($id)]} {
6f63fc18
PM
8750 set cmd [diffcmd [list $id] {-p --root}]
8751 # trim off the initial "|"
8752 set cmd [lrange $cmd 1 end]
8753 if {[catch {
8754 set x [eval exec $cmd | git patch-id]
8755 set patchids($id) [lindex $x 0]
8756 }]} {
8757 set patchids($id) "error"
8758 }
010509f2
PM
8759 }
8760 return $patchids($id)
8761}
8762
8763proc do_cmp_commits {a b} {
8764 global ctext curview parents children patchids commitinfo
8765
8766 $ctext conf -state normal
8767 clear_ctext
8768 init_flist {}
8769 for {set i 0} {$i < 100} {incr i} {
010509f2
PM
8770 set skipa 0
8771 set skipb 0
8772 if {[llength $parents($curview,$a)] > 1} {
6f63fc18 8773 appendshortlink $a [mc "Skipping merge commit "] "\n"
010509f2
PM
8774 set skipa 1
8775 } else {
8776 set patcha [getpatchid $a]
8777 }
8778 if {[llength $parents($curview,$b)] > 1} {
6f63fc18 8779 appendshortlink $b [mc "Skipping merge commit "] "\n"
010509f2
PM
8780 set skipb 1
8781 } else {
8782 set patchb [getpatchid $b]
8783 }
8784 if {!$skipa && !$skipb} {
8785 set heada [lindex $commitinfo($a) 0]
8786 set headb [lindex $commitinfo($b) 0]
6f63fc18
PM
8787 if {$patcha eq "error"} {
8788 appendshortlink $a [mc "Error getting patch ID for "] \
8789 [mc " - stopping\n"]
8790 break
8791 }
8792 if {$patchb eq "error"} {
8793 appendshortlink $b [mc "Error getting patch ID for "] \
8794 [mc " - stopping\n"]
8795 break
8796 }
010509f2
PM
8797 if {$patcha eq $patchb} {
8798 if {$heada eq $headb} {
6f63fc18
PM
8799 appendshortlink $a [mc "Commit "]
8800 appendshortlink $b " == " " $heada\n"
010509f2 8801 } else {
6f63fc18
PM
8802 appendshortlink $a [mc "Commit "] " $heada\n"
8803 appendshortlink $b [mc " is the same patch as\n "] \
8804 " $headb\n"
010509f2
PM
8805 }
8806 set skipa 1
8807 set skipb 1
8808 } else {
8809 $ctext insert end "\n"
6f63fc18
PM
8810 appendshortlink $a [mc "Commit "] " $heada\n"
8811 appendshortlink $b [mc " differs from\n "] \
8812 " $headb\n"
c21398be
PM
8813 $ctext insert end [mc "Diff of commits:\n\n"]
8814 $ctext conf -state disabled
8815 update
8816 diffcommits $a $b
8817 return
010509f2
PM
8818 }
8819 }
8820 if {$skipa} {
aa43561a
PM
8821 set kids [real_children $curview,$a]
8822 if {[llength $kids] != 1} {
010509f2 8823 $ctext insert end "\n"
6f63fc18 8824 appendshortlink $a [mc "Commit "] \
aa43561a 8825 [mc " has %s children - stopping\n" [llength $kids]]
010509f2
PM
8826 break
8827 }
aa43561a 8828 set a [lindex $kids 0]
010509f2
PM
8829 }
8830 if {$skipb} {
aa43561a
PM
8831 set kids [real_children $curview,$b]
8832 if {[llength $kids] != 1} {
6f63fc18 8833 appendshortlink $b [mc "Commit "] \
aa43561a 8834 [mc " has %s children - stopping\n" [llength $kids]]
010509f2
PM
8835 break
8836 }
aa43561a 8837 set b [lindex $kids 0]
010509f2
PM
8838 }
8839 }
8840 $ctext conf -state disabled
8841}
8842
c21398be 8843proc diffcommits {a b} {
a1d383c5 8844 global diffcontext diffids blobdifffd diffinhdr currdiffsubmod
c21398be
PM
8845
8846 set tmpdir [gitknewtmpdir]
8847 set fna [file join $tmpdir "commit-[string range $a 0 7]"]
8848 set fnb [file join $tmpdir "commit-[string range $b 0 7]"]
8849 if {[catch {
8850 exec git diff-tree -p --pretty $a >$fna
8851 exec git diff-tree -p --pretty $b >$fnb
8852 } err]} {
8853 error_popup [mc "Error writing commit to file: %s" $err]
8854 return
8855 }
8856 if {[catch {
8857 set fd [open "| diff -U$diffcontext $fna $fnb" r]
8858 } err]} {
8859 error_popup [mc "Error diffing commits: %s" $err]
8860 return
8861 }
8862 set diffids [list commits $a $b]
8863 set blobdifffd($diffids) $fd
8864 set diffinhdr 0
a1d383c5 8865 set currdiffsubmod ""
c21398be
PM
8866 filerun $fd [list getblobdiffline $fd $diffids]
8867}
8868
c8dfbcf9 8869proc diffvssel {dirn} {
7fcc92bf 8870 global rowmenuid selectedline
c8dfbcf9 8871
94b4a69f 8872 if {$selectedline eq {}} return
c8dfbcf9 8873 if {$dirn} {
7fcc92bf 8874 set oldid [commitonrow $selectedline]
c8dfbcf9
PM
8875 set newid $rowmenuid
8876 } else {
8877 set oldid $rowmenuid
7fcc92bf 8878 set newid [commitonrow $selectedline]
c8dfbcf9 8879 }
354af6bd 8880 addtohistory [list doseldiff $oldid $newid] savectextpos
fa4da7b3
PM
8881 doseldiff $oldid $newid
8882}
8883
6febdede
PM
8884proc diffvsmark {dirn} {
8885 global rowmenuid markedid
8886
8887 if {![info exists markedid]} return
8888 if {$dirn} {
8889 set oldid $markedid
8890 set newid $rowmenuid
8891 } else {
8892 set oldid $rowmenuid
8893 set newid $markedid
8894 }
8895 addtohistory [list doseldiff $oldid $newid] savectextpos
8896 doseldiff $oldid $newid
8897}
8898
fa4da7b3 8899proc doseldiff {oldid newid} {
7fcceed7 8900 global ctext
fa4da7b3
PM
8901 global commitinfo
8902
c8dfbcf9 8903 $ctext conf -state normal
3ea06f9f 8904 clear_ctext
d990cedf
CS
8905 init_flist [mc "Top"]
8906 $ctext insert end "[mc "From"] "
97645683
PM
8907 $ctext insert end $oldid link0
8908 setlink $oldid link0
fa4da7b3 8909 $ctext insert end "\n "
c8dfbcf9 8910 $ctext insert end [lindex $commitinfo($oldid) 0]
d990cedf 8911 $ctext insert end "\n\n[mc "To"] "
97645683
PM
8912 $ctext insert end $newid link1
8913 setlink $newid link1
fa4da7b3 8914 $ctext insert end "\n "
c8dfbcf9
PM
8915 $ctext insert end [lindex $commitinfo($newid) 0]
8916 $ctext insert end "\n"
8917 $ctext conf -state disabled
c8dfbcf9 8918 $ctext tag remove found 1.0 end
d327244a 8919 startdiff [list $oldid $newid]
c8dfbcf9
PM
8920}
8921
74daedb6 8922proc mkpatch {} {
d93f1713 8923 global rowmenuid currentid commitinfo patchtop patchnum NS
74daedb6
PM
8924
8925 if {![info exists currentid]} return
8926 set oldid $currentid
8927 set oldhead [lindex $commitinfo($oldid) 0]
8928 set newid $rowmenuid
8929 set newhead [lindex $commitinfo($newid) 0]
8930 set top .patch
8931 set patchtop $top
8932 catch {destroy $top}
d93f1713 8933 ttk_toplevel $top
e7d64008 8934 make_transient $top .
d93f1713 8935 ${NS}::label $top.title -text [mc "Generate patch"]
4a2139f5 8936 grid $top.title - -pady 10
d93f1713
PT
8937 ${NS}::label $top.from -text [mc "From:"]
8938 ${NS}::entry $top.fromsha1 -width 40
74daedb6
PM
8939 $top.fromsha1 insert 0 $oldid
8940 $top.fromsha1 conf -state readonly
8941 grid $top.from $top.fromsha1 -sticky w
d93f1713 8942 ${NS}::entry $top.fromhead -width 60
74daedb6
PM
8943 $top.fromhead insert 0 $oldhead
8944 $top.fromhead conf -state readonly
8945 grid x $top.fromhead -sticky w
d93f1713
PT
8946 ${NS}::label $top.to -text [mc "To:"]
8947 ${NS}::entry $top.tosha1 -width 40
74daedb6
PM
8948 $top.tosha1 insert 0 $newid
8949 $top.tosha1 conf -state readonly
8950 grid $top.to $top.tosha1 -sticky w
d93f1713 8951 ${NS}::entry $top.tohead -width 60
74daedb6
PM
8952 $top.tohead insert 0 $newhead
8953 $top.tohead conf -state readonly
8954 grid x $top.tohead -sticky w
d93f1713
PT
8955 ${NS}::button $top.rev -text [mc "Reverse"] -command mkpatchrev
8956 grid $top.rev x -pady 10 -padx 5
8957 ${NS}::label $top.flab -text [mc "Output file:"]
8958 ${NS}::entry $top.fname -width 60
74daedb6
PM
8959 $top.fname insert 0 [file normalize "patch$patchnum.patch"]
8960 incr patchnum
bdbfbe3d 8961 grid $top.flab $top.fname -sticky w
d93f1713
PT
8962 ${NS}::frame $top.buts
8963 ${NS}::button $top.buts.gen -text [mc "Generate"] -command mkpatchgo
8964 ${NS}::button $top.buts.can -text [mc "Cancel"] -command mkpatchcan
76f15947
AG
8965 bind $top <Key-Return> mkpatchgo
8966 bind $top <Key-Escape> mkpatchcan
74daedb6
PM
8967 grid $top.buts.gen $top.buts.can
8968 grid columnconfigure $top.buts 0 -weight 1 -uniform a
8969 grid columnconfigure $top.buts 1 -weight 1 -uniform a
8970 grid $top.buts - -pady 10 -sticky ew
bdbfbe3d 8971 focus $top.fname
74daedb6
PM
8972}
8973
8974proc mkpatchrev {} {
8975 global patchtop
8976
8977 set oldid [$patchtop.fromsha1 get]
8978 set oldhead [$patchtop.fromhead get]
8979 set newid [$patchtop.tosha1 get]
8980 set newhead [$patchtop.tohead get]
8981 foreach e [list fromsha1 fromhead tosha1 tohead] \
8982 v [list $newid $newhead $oldid $oldhead] {
8983 $patchtop.$e conf -state normal
8984 $patchtop.$e delete 0 end
8985 $patchtop.$e insert 0 $v
8986 $patchtop.$e conf -state readonly
8987 }
8988}
8989
8990proc mkpatchgo {} {
8f489363 8991 global patchtop nullid nullid2
74daedb6
PM
8992
8993 set oldid [$patchtop.fromsha1 get]
8994 set newid [$patchtop.tosha1 get]
8995 set fname [$patchtop.fname get]
8f489363 8996 set cmd [diffcmd [list $oldid $newid] -p]
d372e216
PM
8997 # trim off the initial "|"
8998 set cmd [lrange $cmd 1 end]
219ea3a9
PM
8999 lappend cmd >$fname &
9000 if {[catch {eval exec $cmd} err]} {
84a76f18 9001 error_popup "[mc "Error creating patch:"] $err" $patchtop
74daedb6
PM
9002 }
9003 catch {destroy $patchtop}
9004 unset patchtop
9005}
9006
9007proc mkpatchcan {} {
9008 global patchtop
9009
9010 catch {destroy $patchtop}
9011 unset patchtop
9012}
9013
bdbfbe3d 9014proc mktag {} {
d93f1713 9015 global rowmenuid mktagtop commitinfo NS
bdbfbe3d
PM
9016
9017 set top .maketag
9018 set mktagtop $top
9019 catch {destroy $top}
d93f1713 9020 ttk_toplevel $top
e7d64008 9021 make_transient $top .
d93f1713 9022 ${NS}::label $top.title -text [mc "Create tag"]
4a2139f5 9023 grid $top.title - -pady 10
d93f1713
PT
9024 ${NS}::label $top.id -text [mc "ID:"]
9025 ${NS}::entry $top.sha1 -width 40
bdbfbe3d
PM
9026 $top.sha1 insert 0 $rowmenuid
9027 $top.sha1 conf -state readonly
9028 grid $top.id $top.sha1 -sticky w
d93f1713 9029 ${NS}::entry $top.head -width 60
bdbfbe3d
PM
9030 $top.head insert 0 [lindex $commitinfo($rowmenuid) 0]
9031 $top.head conf -state readonly
9032 grid x $top.head -sticky w
d93f1713
PT
9033 ${NS}::label $top.tlab -text [mc "Tag name:"]
9034 ${NS}::entry $top.tag -width 60
bdbfbe3d 9035 grid $top.tlab $top.tag -sticky w
dfb891e3
DD
9036 ${NS}::label $top.op -text [mc "Tag message is optional"]
9037 grid $top.op -columnspan 2 -sticky we
9038 ${NS}::label $top.mlab -text [mc "Tag message:"]
9039 ${NS}::entry $top.msg -width 60
9040 grid $top.mlab $top.msg -sticky w
d93f1713
PT
9041 ${NS}::frame $top.buts
9042 ${NS}::button $top.buts.gen -text [mc "Create"] -command mktaggo
9043 ${NS}::button $top.buts.can -text [mc "Cancel"] -command mktagcan
76f15947
AG
9044 bind $top <Key-Return> mktaggo
9045 bind $top <Key-Escape> mktagcan
bdbfbe3d
PM
9046 grid $top.buts.gen $top.buts.can
9047 grid columnconfigure $top.buts 0 -weight 1 -uniform a
9048 grid columnconfigure $top.buts 1 -weight 1 -uniform a
9049 grid $top.buts - -pady 10 -sticky ew
9050 focus $top.tag
9051}
9052
9053proc domktag {} {
9054 global mktagtop env tagids idtags
bdbfbe3d
PM
9055
9056 set id [$mktagtop.sha1 get]
9057 set tag [$mktagtop.tag get]
dfb891e3 9058 set msg [$mktagtop.msg get]
bdbfbe3d 9059 if {$tag == {}} {
84a76f18
AG
9060 error_popup [mc "No tag name specified"] $mktagtop
9061 return 0
bdbfbe3d
PM
9062 }
9063 if {[info exists tagids($tag)]} {
84a76f18
AG
9064 error_popup [mc "Tag \"%s\" already exists" $tag] $mktagtop
9065 return 0
bdbfbe3d
PM
9066 }
9067 if {[catch {
dfb891e3
DD
9068 if {$msg != {}} {
9069 exec git tag -a -m $msg $tag $id
9070 } else {
9071 exec git tag $tag $id
9072 }
bdbfbe3d 9073 } err]} {
84a76f18
AG
9074 error_popup "[mc "Error creating tag:"] $err" $mktagtop
9075 return 0
bdbfbe3d
PM
9076 }
9077
9078 set tagids($tag) $id
9079 lappend idtags($id) $tag
f1d83ba3 9080 redrawtags $id
ceadfe90 9081 addedtag $id
887c996e
PM
9082 dispneartags 0
9083 run refill_reflist
84a76f18 9084 return 1
f1d83ba3
PM
9085}
9086
9087proc redrawtags {id} {
b9fdba7f 9088 global canv linehtag idpos currentid curview cmitlisted markedid
c11ff120 9089 global canvxmax iddrawn circleitem mainheadid circlecolors
252c52df 9090 global mainheadcirclecolor
f1d83ba3 9091
7fcc92bf 9092 if {![commitinview $id $curview]} return
322a8cc9 9093 if {![info exists iddrawn($id)]} return
fc2a256f 9094 set row [rowofcommit $id]
c11ff120 9095 if {$id eq $mainheadid} {
252c52df 9096 set ofill $mainheadcirclecolor
c11ff120
PM
9097 } else {
9098 set ofill [lindex $circlecolors $cmitlisted($curview,$id)]
9099 }
9100 $canv itemconf $circleitem($row) -fill $ofill
bdbfbe3d
PM
9101 $canv delete tag.$id
9102 set xt [eval drawtags $id $idpos($id)]
28593d3f
PM
9103 $canv coords $linehtag($id) $xt [lindex $idpos($id) 2]
9104 set text [$canv itemcget $linehtag($id) -text]
9105 set font [$canv itemcget $linehtag($id) -font]
fc2a256f 9106 set xr [expr {$xt + [font measure $font $text]}]
b8ab2e17
PM
9107 if {$xr > $canvxmax} {
9108 set canvxmax $xr
9109 setcanvscroll
9110 }
fc2a256f 9111 if {[info exists currentid] && $currentid == $id} {
28593d3f 9112 make_secsel $id
bdbfbe3d 9113 }
b9fdba7f
PM
9114 if {[info exists markedid] && $markedid eq $id} {
9115 make_idmark $id
9116 }
bdbfbe3d
PM
9117}
9118
9119proc mktagcan {} {
9120 global mktagtop
9121
9122 catch {destroy $mktagtop}
9123 unset mktagtop
9124}
9125
9126proc mktaggo {} {
84a76f18 9127 if {![domktag]} return
bdbfbe3d
PM
9128 mktagcan
9129}
9130
4a2139f5 9131proc writecommit {} {
d93f1713 9132 global rowmenuid wrcomtop commitinfo wrcomcmd NS
4a2139f5
PM
9133
9134 set top .writecommit
9135 set wrcomtop $top
9136 catch {destroy $top}
d93f1713 9137 ttk_toplevel $top
e7d64008 9138 make_transient $top .
d93f1713 9139 ${NS}::label $top.title -text [mc "Write commit to file"]
4a2139f5 9140 grid $top.title - -pady 10
d93f1713
PT
9141 ${NS}::label $top.id -text [mc "ID:"]
9142 ${NS}::entry $top.sha1 -width 40
4a2139f5
PM
9143 $top.sha1 insert 0 $rowmenuid
9144 $top.sha1 conf -state readonly
9145 grid $top.id $top.sha1 -sticky w
d93f1713 9146 ${NS}::entry $top.head -width 60
4a2139f5
PM
9147 $top.head insert 0 [lindex $commitinfo($rowmenuid) 0]
9148 $top.head conf -state readonly
9149 grid x $top.head -sticky w
d93f1713
PT
9150 ${NS}::label $top.clab -text [mc "Command:"]
9151 ${NS}::entry $top.cmd -width 60 -textvariable wrcomcmd
4a2139f5 9152 grid $top.clab $top.cmd -sticky w -pady 10
d93f1713
PT
9153 ${NS}::label $top.flab -text [mc "Output file:"]
9154 ${NS}::entry $top.fname -width 60
4a2139f5
PM
9155 $top.fname insert 0 [file normalize "commit-[string range $rowmenuid 0 6]"]
9156 grid $top.flab $top.fname -sticky w
d93f1713
PT
9157 ${NS}::frame $top.buts
9158 ${NS}::button $top.buts.gen -text [mc "Write"] -command wrcomgo
9159 ${NS}::button $top.buts.can -text [mc "Cancel"] -command wrcomcan
76f15947
AG
9160 bind $top <Key-Return> wrcomgo
9161 bind $top <Key-Escape> wrcomcan
4a2139f5
PM
9162 grid $top.buts.gen $top.buts.can
9163 grid columnconfigure $top.buts 0 -weight 1 -uniform a
9164 grid columnconfigure $top.buts 1 -weight 1 -uniform a
9165 grid $top.buts - -pady 10 -sticky ew
9166 focus $top.fname
9167}
9168
9169proc wrcomgo {} {
9170 global wrcomtop
9171
9172 set id [$wrcomtop.sha1 get]
9173 set cmd "echo $id | [$wrcomtop.cmd get]"
9174 set fname [$wrcomtop.fname get]
9175 if {[catch {exec sh -c $cmd >$fname &} err]} {
84a76f18 9176 error_popup "[mc "Error writing commit:"] $err" $wrcomtop
4a2139f5
PM
9177 }
9178 catch {destroy $wrcomtop}
9179 unset wrcomtop
9180}
9181
9182proc wrcomcan {} {
9183 global wrcomtop
9184
9185 catch {destroy $wrcomtop}
9186 unset wrcomtop
9187}
9188
d6ac1a86 9189proc mkbranch {} {
d93f1713 9190 global rowmenuid mkbrtop NS
d6ac1a86
PM
9191
9192 set top .makebranch
9193 catch {destroy $top}
d93f1713 9194 ttk_toplevel $top
e7d64008 9195 make_transient $top .
d93f1713 9196 ${NS}::label $top.title -text [mc "Create new branch"]
d6ac1a86 9197 grid $top.title - -pady 10
d93f1713
PT
9198 ${NS}::label $top.id -text [mc "ID:"]
9199 ${NS}::entry $top.sha1 -width 40
d6ac1a86
PM
9200 $top.sha1 insert 0 $rowmenuid
9201 $top.sha1 conf -state readonly
9202 grid $top.id $top.sha1 -sticky w
d93f1713
PT
9203 ${NS}::label $top.nlab -text [mc "Name:"]
9204 ${NS}::entry $top.name -width 40
d6ac1a86 9205 grid $top.nlab $top.name -sticky w
d93f1713
PT
9206 ${NS}::frame $top.buts
9207 ${NS}::button $top.buts.go -text [mc "Create"] -command [list mkbrgo $top]
9208 ${NS}::button $top.buts.can -text [mc "Cancel"] -command "catch {destroy $top}"
76f15947
AG
9209 bind $top <Key-Return> [list mkbrgo $top]
9210 bind $top <Key-Escape> "catch {destroy $top}"
d6ac1a86
PM
9211 grid $top.buts.go $top.buts.can
9212 grid columnconfigure $top.buts 0 -weight 1 -uniform a
9213 grid columnconfigure $top.buts 1 -weight 1 -uniform a
9214 grid $top.buts - -pady 10 -sticky ew
9215 focus $top.name
9216}
9217
9218proc mkbrgo {top} {
9219 global headids idheads
9220
9221 set name [$top.name get]
9222 set id [$top.sha1 get]
bee866fa
AG
9223 set cmdargs {}
9224 set old_id {}
d6ac1a86 9225 if {$name eq {}} {
84a76f18 9226 error_popup [mc "Please specify a name for the new branch"] $top
d6ac1a86
PM
9227 return
9228 }
bee866fa
AG
9229 if {[info exists headids($name)]} {
9230 if {![confirm_popup [mc \
84a76f18 9231 "Branch '%s' already exists. Overwrite?" $name] $top]} {
bee866fa
AG
9232 return
9233 }
9234 set old_id $headids($name)
9235 lappend cmdargs -f
9236 }
d6ac1a86 9237 catch {destroy $top}
bee866fa 9238 lappend cmdargs $name $id
d6ac1a86
PM
9239 nowbusy newbranch
9240 update
9241 if {[catch {
bee866fa 9242 eval exec git branch $cmdargs
d6ac1a86
PM
9243 } err]} {
9244 notbusy newbranch
9245 error_popup $err
9246 } else {
d6ac1a86 9247 notbusy newbranch
bee866fa
AG
9248 if {$old_id ne {}} {
9249 movehead $id $name
9250 movedhead $id $name
9251 redrawtags $old_id
9252 redrawtags $id
9253 } else {
9254 set headids($name) $id
9255 lappend idheads($id) $name
9256 addedhead $id $name
9257 redrawtags $id
9258 }
e11f1233 9259 dispneartags 0
887c996e 9260 run refill_reflist
d6ac1a86
PM
9261 }
9262}
9263
15e35055
AG
9264proc exec_citool {tool_args {baseid {}}} {
9265 global commitinfo env
9266
9267 set save_env [array get env GIT_AUTHOR_*]
9268
9269 if {$baseid ne {}} {
9270 if {![info exists commitinfo($baseid)]} {
9271 getcommit $baseid
9272 }
9273 set author [lindex $commitinfo($baseid) 1]
9274 set date [lindex $commitinfo($baseid) 2]
9275 if {[regexp {^\s*(\S.*\S|\S)\s*<(.*)>\s*$} \
9276 $author author name email]
9277 && $date ne {}} {
9278 set env(GIT_AUTHOR_NAME) $name
9279 set env(GIT_AUTHOR_EMAIL) $email
9280 set env(GIT_AUTHOR_DATE) $date
9281 }
9282 }
9283
9284 eval exec git citool $tool_args &
9285
9286 array unset env GIT_AUTHOR_*
9287 array set env $save_env
9288}
9289
ca6d8f58 9290proc cherrypick {} {
468bcaed 9291 global rowmenuid curview
b8a938cf 9292 global mainhead mainheadid
da616db5 9293 global gitdir
ca6d8f58 9294
e11f1233
PM
9295 set oldhead [exec git rev-parse HEAD]
9296 set dheads [descheads $rowmenuid]
9297 if {$dheads ne {} && [lsearch -exact $dheads $oldhead] >= 0} {
d990cedf
CS
9298 set ok [confirm_popup [mc "Commit %s is already\
9299 included in branch %s -- really re-apply it?" \
9300 [string range $rowmenuid 0 7] $mainhead]]
ca6d8f58
PM
9301 if {!$ok} return
9302 }
d990cedf 9303 nowbusy cherrypick [mc "Cherry-picking"]
ca6d8f58 9304 update
ca6d8f58
PM
9305 # Unfortunately git-cherry-pick writes stuff to stderr even when
9306 # no error occurs, and exec takes that as an indication of error...
9307 if {[catch {exec sh -c "git cherry-pick -r $rowmenuid 2>&1"} err]} {
9308 notbusy cherrypick
15e35055 9309 if {[regexp -line \
887a791f
PM
9310 {Entry '(.*)' (would be overwritten by merge|not uptodate)} \
9311 $err msg fname]} {
9312 error_popup [mc "Cherry-pick failed because of local changes\
9313 to file '%s'.\nPlease commit, reset or stash\
9314 your changes and try again." $fname]
9315 } elseif {[regexp -line \
b74307f6 9316 {^(CONFLICT \(.*\):|Automatic cherry-pick failed|error: could not apply)} \
887a791f
PM
9317 $err]} {
9318 if {[confirm_popup [mc "Cherry-pick failed because of merge\
9319 conflict.\nDo you wish to run git citool to\
9320 resolve it?"]]} {
9321 # Force citool to read MERGE_MSG
da616db5 9322 file delete [file join $gitdir "GITGUI_MSG"]
887a791f
PM
9323 exec_citool {} $rowmenuid
9324 }
15e35055
AG
9325 } else {
9326 error_popup $err
9327 }
887a791f 9328 run updatecommits
ca6d8f58
PM
9329 return
9330 }
9331 set newhead [exec git rev-parse HEAD]
9332 if {$newhead eq $oldhead} {
9333 notbusy cherrypick
d990cedf 9334 error_popup [mc "No changes committed"]
ca6d8f58
PM
9335 return
9336 }
e11f1233 9337 addnewchild $newhead $oldhead
7fcc92bf 9338 if {[commitinview $oldhead $curview]} {
cdc8429c 9339 # XXX this isn't right if we have a path limit...
7fcc92bf 9340 insertrow $newhead $oldhead $curview
ca6d8f58 9341 if {$mainhead ne {}} {
e11f1233 9342 movehead $newhead $mainhead
ca6d8f58
PM
9343 movedhead $newhead $mainhead
9344 }
c11ff120 9345 set mainheadid $newhead
ca6d8f58
PM
9346 redrawtags $oldhead
9347 redrawtags $newhead
46308ea1 9348 selbyid $newhead
ca6d8f58
PM
9349 }
9350 notbusy cherrypick
9351}
9352
8f3ff933
KF
9353proc revert {} {
9354 global rowmenuid curview
9355 global mainhead mainheadid
9356 global gitdir
9357
9358 set oldhead [exec git rev-parse HEAD]
9359 set dheads [descheads $rowmenuid]
9360 if { $dheads eq {} || [lsearch -exact $dheads $oldhead] == -1 } {
9361 set ok [confirm_popup [mc "Commit %s is not\
9362 included in branch %s -- really revert it?" \
9363 [string range $rowmenuid 0 7] $mainhead]]
9364 if {!$ok} return
9365 }
9366 nowbusy revert [mc "Reverting"]
9367 update
9368
9369 if [catch {exec git revert --no-edit $rowmenuid} err] {
9370 notbusy revert
9371 if [regexp {files would be overwritten by merge:(\n(( |\t)+[^\n]+\n)+)}\
9372 $err match files] {
9373 regsub {\n( |\t)+} $files "\n" files
9374 error_popup [mc "Revert failed because of local changes to\
9375 the following files:%s Please commit, reset or stash \
9376 your changes and try again." $files]
9377 } elseif [regexp {error: could not revert} $err] {
9378 if [confirm_popup [mc "Revert failed because of merge conflict.\n\
9379 Do you wish to run git citool to resolve it?"]] {
9380 # Force citool to read MERGE_MSG
9381 file delete [file join $gitdir "GITGUI_MSG"]
9382 exec_citool {} $rowmenuid
9383 }
9384 } else { error_popup $err }
9385 run updatecommits
9386 return
9387 }
9388
9389 set newhead [exec git rev-parse HEAD]
9390 if { $newhead eq $oldhead } {
9391 notbusy revert
9392 error_popup [mc "No changes committed"]
9393 return
9394 }
9395
9396 addnewchild $newhead $oldhead
9397
9398 if [commitinview $oldhead $curview] {
9399 # XXX this isn't right if we have a path limit...
9400 insertrow $newhead $oldhead $curview
9401 if {$mainhead ne {}} {
9402 movehead $newhead $mainhead
9403 movedhead $newhead $mainhead
9404 }
9405 set mainheadid $newhead
9406 redrawtags $oldhead
9407 redrawtags $newhead
9408 selbyid $newhead
9409 }
9410
9411 notbusy revert
9412}
9413
6fb735ae 9414proc resethead {} {
d93f1713 9415 global mainhead rowmenuid confirm_ok resettype NS
6fb735ae
PM
9416
9417 set confirm_ok 0
9418 set w ".confirmreset"
d93f1713 9419 ttk_toplevel $w
e7d64008 9420 make_transient $w .
d990cedf 9421 wm title $w [mc "Confirm reset"]
d93f1713
PT
9422 ${NS}::label $w.m -text \
9423 [mc "Reset branch %s to %s?" $mainhead [string range $rowmenuid 0 7]]
6fb735ae 9424 pack $w.m -side top -fill x -padx 20 -pady 20
d93f1713 9425 ${NS}::labelframe $w.f -text [mc "Reset type:"]
6fb735ae 9426 set resettype mixed
d93f1713 9427 ${NS}::radiobutton $w.f.soft -value soft -variable resettype \
d990cedf 9428 -text [mc "Soft: Leave working tree and index untouched"]
6fb735ae 9429 grid $w.f.soft -sticky w
d93f1713 9430 ${NS}::radiobutton $w.f.mixed -value mixed -variable resettype \
d990cedf 9431 -text [mc "Mixed: Leave working tree untouched, reset index"]
6fb735ae 9432 grid $w.f.mixed -sticky w
d93f1713 9433 ${NS}::radiobutton $w.f.hard -value hard -variable resettype \
d990cedf 9434 -text [mc "Hard: Reset working tree and index\n(discard ALL local changes)"]
6fb735ae 9435 grid $w.f.hard -sticky w
d93f1713
PT
9436 pack $w.f -side top -fill x -padx 4
9437 ${NS}::button $w.ok -text [mc OK] -command "set confirm_ok 1; destroy $w"
6fb735ae 9438 pack $w.ok -side left -fill x -padx 20 -pady 20
d93f1713 9439 ${NS}::button $w.cancel -text [mc Cancel] -command "destroy $w"
76f15947 9440 bind $w <Key-Escape> [list destroy $w]
6fb735ae
PM
9441 pack $w.cancel -side right -fill x -padx 20 -pady 20
9442 bind $w <Visibility> "grab $w; focus $w"
9443 tkwait window $w
9444 if {!$confirm_ok} return
706d6c3e 9445 if {[catch {set fd [open \
08ba820f 9446 [list | git reset --$resettype $rowmenuid 2>@1] r]} err]} {
6fb735ae
PM
9447 error_popup $err
9448 } else {
706d6c3e 9449 dohidelocalchanges
a137a90f 9450 filerun $fd [list readresetstat $fd]
d990cedf 9451 nowbusy reset [mc "Resetting"]
46308ea1 9452 selbyid $rowmenuid
706d6c3e
PM
9453 }
9454}
9455
a137a90f
PM
9456proc readresetstat {fd} {
9457 global mainhead mainheadid showlocalchanges rprogcoord
706d6c3e
PM
9458
9459 if {[gets $fd line] >= 0} {
9460 if {[regexp {([0-9]+)% \(([0-9]+)/([0-9]+)\)} $line match p m n]} {
a137a90f
PM
9461 set rprogcoord [expr {1.0 * $m / $n}]
9462 adjustprogress
706d6c3e
PM
9463 }
9464 return 1
9465 }
a137a90f
PM
9466 set rprogcoord 0
9467 adjustprogress
706d6c3e
PM
9468 notbusy reset
9469 if {[catch {close $fd} err]} {
9470 error_popup $err
9471 }
9472 set oldhead $mainheadid
9473 set newhead [exec git rev-parse HEAD]
9474 if {$newhead ne $oldhead} {
9475 movehead $newhead $mainhead
9476 movedhead $newhead $mainhead
9477 set mainheadid $newhead
6fb735ae 9478 redrawtags $oldhead
706d6c3e 9479 redrawtags $newhead
6fb735ae
PM
9480 }
9481 if {$showlocalchanges} {
9482 doshowlocalchanges
9483 }
706d6c3e 9484 return 0
6fb735ae
PM
9485}
9486
10299152
PM
9487# context menu for a head
9488proc headmenu {x y id head} {
00609463 9489 global headmenuid headmenuhead headctxmenu mainhead
10299152 9490
bb3edc8b 9491 stopfinding
10299152
PM
9492 set headmenuid $id
9493 set headmenuhead $head
00609463 9494 set state normal
70a5fc44
SC
9495 if {[string match "remotes/*" $head]} {
9496 set state disabled
9497 }
00609463
PM
9498 if {$head eq $mainhead} {
9499 set state disabled
9500 }
9501 $headctxmenu entryconfigure 0 -state $state
9502 $headctxmenu entryconfigure 1 -state $state
10299152
PM
9503 tk_popup $headctxmenu $x $y
9504}
9505
9506proc cobranch {} {
c11ff120 9507 global headmenuid headmenuhead headids
cdc8429c 9508 global showlocalchanges
10299152
PM
9509
9510 # check the tree is clean first??
d990cedf 9511 nowbusy checkout [mc "Checking out"]
10299152 9512 update
219ea3a9 9513 dohidelocalchanges
10299152 9514 if {[catch {
08ba820f 9515 set fd [open [list | git checkout $headmenuhead 2>@1] r]
10299152
PM
9516 } err]} {
9517 notbusy checkout
9518 error_popup $err
08ba820f
PM
9519 if {$showlocalchanges} {
9520 dodiffindex
9521 }
10299152 9522 } else {
08ba820f
PM
9523 filerun $fd [list readcheckoutstat $fd $headmenuhead $headmenuid]
9524 }
9525}
9526
9527proc readcheckoutstat {fd newhead newheadid} {
9528 global mainhead mainheadid headids showlocalchanges progresscoords
cdc8429c 9529 global viewmainheadid curview
08ba820f
PM
9530
9531 if {[gets $fd line] >= 0} {
9532 if {[regexp {([0-9]+)% \(([0-9]+)/([0-9]+)\)} $line match p m n]} {
9533 set progresscoords [list 0 [expr {1.0 * $m / $n}]]
9534 adjustprogress
10299152 9535 }
08ba820f
PM
9536 return 1
9537 }
9538 set progresscoords {0 0}
9539 adjustprogress
9540 notbusy checkout
9541 if {[catch {close $fd} err]} {
9542 error_popup $err
9543 }
c11ff120 9544 set oldmainid $mainheadid
08ba820f
PM
9545 set mainhead $newhead
9546 set mainheadid $newheadid
cdc8429c 9547 set viewmainheadid($curview) $newheadid
c11ff120 9548 redrawtags $oldmainid
08ba820f
PM
9549 redrawtags $newheadid
9550 selbyid $newheadid
6fb735ae
PM
9551 if {$showlocalchanges} {
9552 dodiffindex
10299152
PM
9553 }
9554}
9555
9556proc rmbranch {} {
e11f1233 9557 global headmenuid headmenuhead mainhead
b1054ac9 9558 global idheads
10299152
PM
9559
9560 set head $headmenuhead
9561 set id $headmenuid
00609463 9562 # this check shouldn't be needed any more...
10299152 9563 if {$head eq $mainhead} {
d990cedf 9564 error_popup [mc "Cannot delete the currently checked-out branch"]
10299152
PM
9565 return
9566 }
e11f1233 9567 set dheads [descheads $id]
d7b16113 9568 if {[llength $dheads] == 1 && $idheads($dheads) eq $head} {
10299152 9569 # the stuff on this branch isn't on any other branch
d990cedf
CS
9570 if {![confirm_popup [mc "The commits on branch %s aren't on any other\
9571 branch.\nReally delete branch %s?" $head $head]]} return
10299152
PM
9572 }
9573 nowbusy rmbranch
9574 update
9575 if {[catch {exec git branch -D $head} err]} {
9576 notbusy rmbranch
9577 error_popup $err
9578 return
9579 }
e11f1233 9580 removehead $id $head
ca6d8f58 9581 removedhead $id $head
10299152
PM
9582 redrawtags $id
9583 notbusy rmbranch
e11f1233 9584 dispneartags 0
887c996e
PM
9585 run refill_reflist
9586}
9587
9588# Display a list of tags and heads
9589proc showrefs {} {
d93f1713 9590 global showrefstop bgcolor fgcolor selectbgcolor NS
9c311b32 9591 global bglist fglist reflistfilter reflist maincursor
887c996e
PM
9592
9593 set top .showrefs
9594 set showrefstop $top
9595 if {[winfo exists $top]} {
9596 raise $top
9597 refill_reflist
9598 return
9599 }
d93f1713 9600 ttk_toplevel $top
d990cedf 9601 wm title $top [mc "Tags and heads: %s" [file tail [pwd]]]
e7d64008 9602 make_transient $top .
887c996e 9603 text $top.list -background $bgcolor -foreground $fgcolor \
9c311b32 9604 -selectbackground $selectbgcolor -font mainfont \
887c996e
PM
9605 -xscrollcommand "$top.xsb set" -yscrollcommand "$top.ysb set" \
9606 -width 30 -height 20 -cursor $maincursor \
9607 -spacing1 1 -spacing3 1 -state disabled
9608 $top.list tag configure highlight -background $selectbgcolor
9609 lappend bglist $top.list
9610 lappend fglist $top.list
d93f1713
PT
9611 ${NS}::scrollbar $top.ysb -command "$top.list yview" -orient vertical
9612 ${NS}::scrollbar $top.xsb -command "$top.list xview" -orient horizontal
887c996e
PM
9613 grid $top.list $top.ysb -sticky nsew
9614 grid $top.xsb x -sticky ew
d93f1713
PT
9615 ${NS}::frame $top.f
9616 ${NS}::label $top.f.l -text "[mc "Filter"]: "
9617 ${NS}::entry $top.f.e -width 20 -textvariable reflistfilter
887c996e
PM
9618 set reflistfilter "*"
9619 trace add variable reflistfilter write reflistfilter_change
9620 pack $top.f.e -side right -fill x -expand 1
9621 pack $top.f.l -side left
9622 grid $top.f - -sticky ew -pady 2
d93f1713 9623 ${NS}::button $top.close -command [list destroy $top] -text [mc "Close"]
76f15947 9624 bind $top <Key-Escape> [list destroy $top]
887c996e
PM
9625 grid $top.close -
9626 grid columnconfigure $top 0 -weight 1
9627 grid rowconfigure $top 0 -weight 1
9628 bind $top.list <1> {break}
9629 bind $top.list <B1-Motion> {break}
9630 bind $top.list <ButtonRelease-1> {sel_reflist %W %x %y; break}
9631 set reflist {}
9632 refill_reflist
9633}
9634
9635proc sel_reflist {w x y} {
9636 global showrefstop reflist headids tagids otherrefids
9637
9638 if {![winfo exists $showrefstop]} return
9639 set l [lindex [split [$w index "@$x,$y"] "."] 0]
9640 set ref [lindex $reflist [expr {$l-1}]]
9641 set n [lindex $ref 0]
9642 switch -- [lindex $ref 1] {
9643 "H" {selbyid $headids($n)}
9644 "T" {selbyid $tagids($n)}
9645 "o" {selbyid $otherrefids($n)}
9646 }
9647 $showrefstop.list tag add highlight $l.0 "$l.0 lineend"
9648}
9649
9650proc unsel_reflist {} {
9651 global showrefstop
9652
9653 if {![info exists showrefstop] || ![winfo exists $showrefstop]} return
9654 $showrefstop.list tag remove highlight 0.0 end
9655}
9656
9657proc reflistfilter_change {n1 n2 op} {
9658 global reflistfilter
9659
9660 after cancel refill_reflist
9661 after 200 refill_reflist
9662}
9663
9664proc refill_reflist {} {
9665 global reflist reflistfilter showrefstop headids tagids otherrefids
d375ef9b 9666 global curview
887c996e
PM
9667
9668 if {![info exists showrefstop] || ![winfo exists $showrefstop]} return
9669 set refs {}
9670 foreach n [array names headids] {
9671 if {[string match $reflistfilter $n]} {
7fcc92bf 9672 if {[commitinview $headids($n) $curview]} {
887c996e
PM
9673 lappend refs [list $n H]
9674 } else {
d375ef9b 9675 interestedin $headids($n) {run refill_reflist}
887c996e
PM
9676 }
9677 }
9678 }
9679 foreach n [array names tagids] {
9680 if {[string match $reflistfilter $n]} {
7fcc92bf 9681 if {[commitinview $tagids($n) $curview]} {
887c996e
PM
9682 lappend refs [list $n T]
9683 } else {
d375ef9b 9684 interestedin $tagids($n) {run refill_reflist}
887c996e
PM
9685 }
9686 }
9687 }
9688 foreach n [array names otherrefids] {
9689 if {[string match $reflistfilter $n]} {
7fcc92bf 9690 if {[commitinview $otherrefids($n) $curview]} {
887c996e
PM
9691 lappend refs [list $n o]
9692 } else {
d375ef9b 9693 interestedin $otherrefids($n) {run refill_reflist}
887c996e
PM
9694 }
9695 }
9696 }
9697 set refs [lsort -index 0 $refs]
9698 if {$refs eq $reflist} return
9699
9700 # Update the contents of $showrefstop.list according to the
9701 # differences between $reflist (old) and $refs (new)
9702 $showrefstop.list conf -state normal
9703 $showrefstop.list insert end "\n"
9704 set i 0
9705 set j 0
9706 while {$i < [llength $reflist] || $j < [llength $refs]} {
9707 if {$i < [llength $reflist]} {
9708 if {$j < [llength $refs]} {
9709 set cmp [string compare [lindex $reflist $i 0] \
9710 [lindex $refs $j 0]]
9711 if {$cmp == 0} {
9712 set cmp [string compare [lindex $reflist $i 1] \
9713 [lindex $refs $j 1]]
9714 }
9715 } else {
9716 set cmp -1
9717 }
9718 } else {
9719 set cmp 1
9720 }
9721 switch -- $cmp {
9722 -1 {
9723 $showrefstop.list delete "[expr {$j+1}].0" "[expr {$j+2}].0"
9724 incr i
9725 }
9726 0 {
9727 incr i
9728 incr j
9729 }
9730 1 {
9731 set l [expr {$j + 1}]
9732 $showrefstop.list image create $l.0 -align baseline \
9733 -image reficon-[lindex $refs $j 1] -padx 2
9734 $showrefstop.list insert $l.1 "[lindex $refs $j 0]\n"
9735 incr j
9736 }
9737 }
9738 }
9739 set reflist $refs
9740 # delete last newline
9741 $showrefstop.list delete end-2c end-1c
9742 $showrefstop.list conf -state disabled
10299152
PM
9743}
9744
b8ab2e17
PM
9745# Stuff for finding nearby tags
9746proc getallcommits {} {
5cd15b6b
PM
9747 global allcommits nextarc seeds allccache allcwait cachedarcs allcupdate
9748 global idheads idtags idotherrefs allparents tagobjid
da616db5 9749 global gitdir
f1d83ba3 9750
a69b2d1a 9751 if {![info exists allcommits]} {
a69b2d1a
PM
9752 set nextarc 0
9753 set allcommits 0
9754 set seeds {}
5cd15b6b
PM
9755 set allcwait 0
9756 set cachedarcs 0
da616db5 9757 set allccache [file join $gitdir "gitk.cache"]
5cd15b6b
PM
9758 if {![catch {
9759 set f [open $allccache r]
9760 set allcwait 1
9761 getcache $f
9762 }]} return
a69b2d1a 9763 }
2d71bccc 9764
5cd15b6b
PM
9765 if {$allcwait} {
9766 return
9767 }
9768 set cmd [list | git rev-list --parents]
9769 set allcupdate [expr {$seeds ne {}}]
9770 if {!$allcupdate} {
9771 set ids "--all"
9772 } else {
9773 set refs [concat [array names idheads] [array names idtags] \
9774 [array names idotherrefs]]
9775 set ids {}
9776 set tagobjs {}
9777 foreach name [array names tagobjid] {
9778 lappend tagobjs $tagobjid($name)
9779 }
9780 foreach id [lsort -unique $refs] {
9781 if {![info exists allparents($id)] &&
9782 [lsearch -exact $tagobjs $id] < 0} {
9783 lappend ids $id
9784 }
9785 }
9786 if {$ids ne {}} {
9787 foreach id $seeds {
9788 lappend ids "^$id"
9789 }
9790 }
9791 }
9792 if {$ids ne {}} {
9793 set fd [open [concat $cmd $ids] r]
9794 fconfigure $fd -blocking 0
9795 incr allcommits
9796 nowbusy allcommits
9797 filerun $fd [list getallclines $fd]
9798 } else {
9799 dispneartags 0
2d71bccc 9800 }
e11f1233
PM
9801}
9802
9803# Since most commits have 1 parent and 1 child, we group strings of
9804# such commits into "arcs" joining branch/merge points (BMPs), which
9805# are commits that either don't have 1 parent or don't have 1 child.
9806#
9807# arcnos(id) - incoming arcs for BMP, arc we're on for other nodes
9808# arcout(id) - outgoing arcs for BMP
9809# arcids(a) - list of IDs on arc including end but not start
9810# arcstart(a) - BMP ID at start of arc
9811# arcend(a) - BMP ID at end of arc
9812# growing(a) - arc a is still growing
9813# arctags(a) - IDs out of arcids (excluding end) that have tags
9814# archeads(a) - IDs out of arcids (excluding end) that have heads
9815# The start of an arc is at the descendent end, so "incoming" means
9816# coming from descendents, and "outgoing" means going towards ancestors.
9817
9818proc getallclines {fd} {
5cd15b6b 9819 global allparents allchildren idtags idheads nextarc
e11f1233 9820 global arcnos arcids arctags arcout arcend arcstart archeads growing
5cd15b6b 9821 global seeds allcommits cachedarcs allcupdate
d93f1713 9822
e11f1233 9823 set nid 0
7eb3cb9c 9824 while {[incr nid] <= 1000 && [gets $fd line] >= 0} {
e11f1233
PM
9825 set id [lindex $line 0]
9826 if {[info exists allparents($id)]} {
9827 # seen it already
9828 continue
9829 }
5cd15b6b 9830 set cachedarcs 0
e11f1233
PM
9831 set olds [lrange $line 1 end]
9832 set allparents($id) $olds
9833 if {![info exists allchildren($id)]} {
9834 set allchildren($id) {}
9835 set arcnos($id) {}
9836 lappend seeds $id
9837 } else {
9838 set a $arcnos($id)
9839 if {[llength $olds] == 1 && [llength $a] == 1} {
9840 lappend arcids($a) $id
9841 if {[info exists idtags($id)]} {
9842 lappend arctags($a) $id
b8ab2e17 9843 }
e11f1233
PM
9844 if {[info exists idheads($id)]} {
9845 lappend archeads($a) $id
9846 }
9847 if {[info exists allparents($olds)]} {
9848 # seen parent already
9849 if {![info exists arcout($olds)]} {
9850 splitarc $olds
9851 }
9852 lappend arcids($a) $olds
9853 set arcend($a) $olds
9854 unset growing($a)
9855 }
9856 lappend allchildren($olds) $id
9857 lappend arcnos($olds) $a
9858 continue
9859 }
9860 }
e11f1233
PM
9861 foreach a $arcnos($id) {
9862 lappend arcids($a) $id
9863 set arcend($a) $id
9864 unset growing($a)
9865 }
9866
9867 set ao {}
9868 foreach p $olds {
9869 lappend allchildren($p) $id
9870 set a [incr nextarc]
9871 set arcstart($a) $id
9872 set archeads($a) {}
9873 set arctags($a) {}
9874 set archeads($a) {}
9875 set arcids($a) {}
9876 lappend ao $a
9877 set growing($a) 1
9878 if {[info exists allparents($p)]} {
9879 # seen it already, may need to make a new branch
9880 if {![info exists arcout($p)]} {
9881 splitarc $p
9882 }
9883 lappend arcids($a) $p
9884 set arcend($a) $p
9885 unset growing($a)
9886 }
9887 lappend arcnos($p) $a
9888 }
9889 set arcout($id) $ao
f1d83ba3 9890 }
f3326b66
PM
9891 if {$nid > 0} {
9892 global cached_dheads cached_dtags cached_atags
9893 catch {unset cached_dheads}
9894 catch {unset cached_dtags}
9895 catch {unset cached_atags}
9896 }
7eb3cb9c
PM
9897 if {![eof $fd]} {
9898 return [expr {$nid >= 1000? 2: 1}]
9899 }
5cd15b6b
PM
9900 set cacheok 1
9901 if {[catch {
9902 fconfigure $fd -blocking 1
9903 close $fd
9904 } err]} {
9905 # got an error reading the list of commits
9906 # if we were updating, try rereading the whole thing again
9907 if {$allcupdate} {
9908 incr allcommits -1
9909 dropcache $err
9910 return
9911 }
d990cedf 9912 error_popup "[mc "Error reading commit topology information;\
5cd15b6b 9913 branch and preceding/following tag information\
d990cedf 9914 will be incomplete."]\n($err)"
5cd15b6b
PM
9915 set cacheok 0
9916 }
e11f1233
PM
9917 if {[incr allcommits -1] == 0} {
9918 notbusy allcommits
5cd15b6b
PM
9919 if {$cacheok} {
9920 run savecache
9921 }
e11f1233
PM
9922 }
9923 dispneartags 0
7eb3cb9c 9924 return 0
b8ab2e17
PM
9925}
9926
e11f1233
PM
9927proc recalcarc {a} {
9928 global arctags archeads arcids idtags idheads
b8ab2e17 9929
e11f1233
PM
9930 set at {}
9931 set ah {}
9932 foreach id [lrange $arcids($a) 0 end-1] {
9933 if {[info exists idtags($id)]} {
9934 lappend at $id
9935 }
9936 if {[info exists idheads($id)]} {
9937 lappend ah $id
b8ab2e17 9938 }
f1d83ba3 9939 }
e11f1233
PM
9940 set arctags($a) $at
9941 set archeads($a) $ah
b8ab2e17
PM
9942}
9943
e11f1233 9944proc splitarc {p} {
5cd15b6b 9945 global arcnos arcids nextarc arctags archeads idtags idheads
e11f1233 9946 global arcstart arcend arcout allparents growing
cec7bece 9947
e11f1233
PM
9948 set a $arcnos($p)
9949 if {[llength $a] != 1} {
9950 puts "oops splitarc called but [llength $a] arcs already"
9951 return
9952 }
9953 set a [lindex $a 0]
9954 set i [lsearch -exact $arcids($a) $p]
9955 if {$i < 0} {
9956 puts "oops splitarc $p not in arc $a"
9957 return
9958 }
9959 set na [incr nextarc]
9960 if {[info exists arcend($a)]} {
9961 set arcend($na) $arcend($a)
9962 } else {
9963 set l [lindex $allparents([lindex $arcids($a) end]) 0]
9964 set j [lsearch -exact $arcnos($l) $a]
9965 set arcnos($l) [lreplace $arcnos($l) $j $j $na]
9966 }
9967 set tail [lrange $arcids($a) [expr {$i+1}] end]
9968 set arcids($a) [lrange $arcids($a) 0 $i]
9969 set arcend($a) $p
9970 set arcstart($na) $p
9971 set arcout($p) $na
9972 set arcids($na) $tail
9973 if {[info exists growing($a)]} {
9974 set growing($na) 1
9975 unset growing($a)
9976 }
e11f1233
PM
9977
9978 foreach id $tail {
9979 if {[llength $arcnos($id)] == 1} {
9980 set arcnos($id) $na
cec7bece 9981 } else {
e11f1233
PM
9982 set j [lsearch -exact $arcnos($id) $a]
9983 set arcnos($id) [lreplace $arcnos($id) $j $j $na]
cec7bece 9984 }
e11f1233
PM
9985 }
9986
9987 # reconstruct tags and heads lists
9988 if {$arctags($a) ne {} || $archeads($a) ne {}} {
9989 recalcarc $a
9990 recalcarc $na
9991 } else {
9992 set arctags($na) {}
9993 set archeads($na) {}
9994 }
9995}
9996
9997# Update things for a new commit added that is a child of one
9998# existing commit. Used when cherry-picking.
9999proc addnewchild {id p} {
5cd15b6b 10000 global allparents allchildren idtags nextarc
e11f1233 10001 global arcnos arcids arctags arcout arcend arcstart archeads growing
719c2b9d 10002 global seeds allcommits
e11f1233 10003
3ebba3c7 10004 if {![info exists allcommits] || ![info exists arcnos($p)]} return
e11f1233
PM
10005 set allparents($id) [list $p]
10006 set allchildren($id) {}
10007 set arcnos($id) {}
10008 lappend seeds $id
e11f1233
PM
10009 lappend allchildren($p) $id
10010 set a [incr nextarc]
10011 set arcstart($a) $id
10012 set archeads($a) {}
10013 set arctags($a) {}
10014 set arcids($a) [list $p]
10015 set arcend($a) $p
10016 if {![info exists arcout($p)]} {
10017 splitarc $p
10018 }
10019 lappend arcnos($p) $a
10020 set arcout($id) [list $a]
10021}
10022
5cd15b6b
PM
10023# This implements a cache for the topology information.
10024# The cache saves, for each arc, the start and end of the arc,
10025# the ids on the arc, and the outgoing arcs from the end.
10026proc readcache {f} {
10027 global arcnos arcids arcout arcstart arcend arctags archeads nextarc
10028 global idtags idheads allparents cachedarcs possible_seeds seeds growing
10029 global allcwait
10030
10031 set a $nextarc
10032 set lim $cachedarcs
10033 if {$lim - $a > 500} {
10034 set lim [expr {$a + 500}]
10035 }
10036 if {[catch {
10037 if {$a == $lim} {
10038 # finish reading the cache and setting up arctags, etc.
10039 set line [gets $f]
10040 if {$line ne "1"} {error "bad final version"}
10041 close $f
10042 foreach id [array names idtags] {
10043 if {[info exists arcnos($id)] && [llength $arcnos($id)] == 1 &&
10044 [llength $allparents($id)] == 1} {
10045 set a [lindex $arcnos($id) 0]
10046 if {$arctags($a) eq {}} {
10047 recalcarc $a
10048 }
10049 }
10050 }
10051 foreach id [array names idheads] {
10052 if {[info exists arcnos($id)] && [llength $arcnos($id)] == 1 &&
10053 [llength $allparents($id)] == 1} {
10054 set a [lindex $arcnos($id) 0]
10055 if {$archeads($a) eq {}} {
10056 recalcarc $a
10057 }
10058 }
10059 }
10060 foreach id [lsort -unique $possible_seeds] {
10061 if {$arcnos($id) eq {}} {
10062 lappend seeds $id
10063 }
10064 }
10065 set allcwait 0
10066 } else {
10067 while {[incr a] <= $lim} {
10068 set line [gets $f]
10069 if {[llength $line] != 3} {error "bad line"}
10070 set s [lindex $line 0]
10071 set arcstart($a) $s
10072 lappend arcout($s) $a
10073 if {![info exists arcnos($s)]} {
10074 lappend possible_seeds $s
10075 set arcnos($s) {}
10076 }
10077 set e [lindex $line 1]
10078 if {$e eq {}} {
10079 set growing($a) 1
10080 } else {
10081 set arcend($a) $e
10082 if {![info exists arcout($e)]} {
10083 set arcout($e) {}
10084 }
10085 }
10086 set arcids($a) [lindex $line 2]
10087 foreach id $arcids($a) {
10088 lappend allparents($s) $id
10089 set s $id
10090 lappend arcnos($id) $a
10091 }
10092 if {![info exists allparents($s)]} {
10093 set allparents($s) {}
10094 }
10095 set arctags($a) {}
10096 set archeads($a) {}
10097 }
10098 set nextarc [expr {$a - 1}]
10099 }
10100 } err]} {
10101 dropcache $err
10102 return 0
10103 }
10104 if {!$allcwait} {
10105 getallcommits
10106 }
10107 return $allcwait
10108}
10109
10110proc getcache {f} {
10111 global nextarc cachedarcs possible_seeds
10112
10113 if {[catch {
10114 set line [gets $f]
10115 if {[llength $line] != 2 || [lindex $line 0] ne "1"} {error "bad version"}
10116 # make sure it's an integer
10117 set cachedarcs [expr {int([lindex $line 1])}]
10118 if {$cachedarcs < 0} {error "bad number of arcs"}
10119 set nextarc 0
10120 set possible_seeds {}
10121 run readcache $f
10122 } err]} {
10123 dropcache $err
10124 }
10125 return 0
10126}
10127
10128proc dropcache {err} {
10129 global allcwait nextarc cachedarcs seeds
10130
10131 #puts "dropping cache ($err)"
10132 foreach v {arcnos arcout arcids arcstart arcend growing \
10133 arctags archeads allparents allchildren} {
10134 global $v
10135 catch {unset $v}
10136 }
10137 set allcwait 0
10138 set nextarc 0
10139 set cachedarcs 0
10140 set seeds {}
10141 getallcommits
10142}
10143
10144proc writecache {f} {
10145 global cachearc cachedarcs allccache
10146 global arcstart arcend arcnos arcids arcout
10147
10148 set a $cachearc
10149 set lim $cachedarcs
10150 if {$lim - $a > 1000} {
10151 set lim [expr {$a + 1000}]
10152 }
10153 if {[catch {
10154 while {[incr a] <= $lim} {
10155 if {[info exists arcend($a)]} {
10156 puts $f [list $arcstart($a) $arcend($a) $arcids($a)]
10157 } else {
10158 puts $f [list $arcstart($a) {} $arcids($a)]
10159 }
10160 }
10161 } err]} {
10162 catch {close $f}
10163 catch {file delete $allccache}
10164 #puts "writing cache failed ($err)"
10165 return 0
10166 }
10167 set cachearc [expr {$a - 1}]
10168 if {$a > $cachedarcs} {
10169 puts $f "1"
10170 close $f
10171 return 0
10172 }
10173 return 1
10174}
10175
10176proc savecache {} {
10177 global nextarc cachedarcs cachearc allccache
10178
10179 if {$nextarc == $cachedarcs} return
10180 set cachearc 0
10181 set cachedarcs $nextarc
10182 catch {
10183 set f [open $allccache w]
10184 puts $f [list 1 $cachedarcs]
10185 run writecache $f
10186 }
10187}
10188
e11f1233
PM
10189# Returns 1 if a is an ancestor of b, -1 if b is an ancestor of a,
10190# or 0 if neither is true.
10191proc anc_or_desc {a b} {
10192 global arcout arcstart arcend arcnos cached_isanc
10193
10194 if {$arcnos($a) eq $arcnos($b)} {
10195 # Both are on the same arc(s); either both are the same BMP,
10196 # or if one is not a BMP, the other is also not a BMP or is
10197 # the BMP at end of the arc (and it only has 1 incoming arc).
69c0b5d2
PM
10198 # Or both can be BMPs with no incoming arcs.
10199 if {$a eq $b || $arcnos($a) eq {}} {
e11f1233 10200 return 0
cec7bece 10201 }
e11f1233
PM
10202 # assert {[llength $arcnos($a)] == 1}
10203 set arc [lindex $arcnos($a) 0]
10204 set i [lsearch -exact $arcids($arc) $a]
10205 set j [lsearch -exact $arcids($arc) $b]
10206 if {$i < 0 || $i > $j} {
10207 return 1
10208 } else {
10209 return -1
cec7bece
PM
10210 }
10211 }
e11f1233
PM
10212
10213 if {![info exists arcout($a)]} {
10214 set arc [lindex $arcnos($a) 0]
10215 if {[info exists arcend($arc)]} {
10216 set aend $arcend($arc)
10217 } else {
10218 set aend {}
cec7bece 10219 }
e11f1233
PM
10220 set a $arcstart($arc)
10221 } else {
10222 set aend $a
10223 }
10224 if {![info exists arcout($b)]} {
10225 set arc [lindex $arcnos($b) 0]
10226 if {[info exists arcend($arc)]} {
10227 set bend $arcend($arc)
10228 } else {
10229 set bend {}
cec7bece 10230 }
e11f1233
PM
10231 set b $arcstart($arc)
10232 } else {
10233 set bend $b
cec7bece 10234 }
e11f1233
PM
10235 if {$a eq $bend} {
10236 return 1
10237 }
10238 if {$b eq $aend} {
10239 return -1
10240 }
10241 if {[info exists cached_isanc($a,$bend)]} {
10242 if {$cached_isanc($a,$bend)} {
10243 return 1
10244 }
10245 }
10246 if {[info exists cached_isanc($b,$aend)]} {
10247 if {$cached_isanc($b,$aend)} {
10248 return -1
10249 }
10250 if {[info exists cached_isanc($a,$bend)]} {
10251 return 0
10252 }
cec7bece 10253 }
cec7bece 10254
e11f1233
PM
10255 set todo [list $a $b]
10256 set anc($a) a
10257 set anc($b) b
10258 for {set i 0} {$i < [llength $todo]} {incr i} {
10259 set x [lindex $todo $i]
10260 if {$anc($x) eq {}} {
10261 continue
10262 }
10263 foreach arc $arcnos($x) {
10264 set xd $arcstart($arc)
10265 if {$xd eq $bend} {
10266 set cached_isanc($a,$bend) 1
10267 set cached_isanc($b,$aend) 0
10268 return 1
10269 } elseif {$xd eq $aend} {
10270 set cached_isanc($b,$aend) 1
10271 set cached_isanc($a,$bend) 0
10272 return -1
10273 }
10274 if {![info exists anc($xd)]} {
10275 set anc($xd) $anc($x)
10276 lappend todo $xd
10277 } elseif {$anc($xd) ne $anc($x)} {
10278 set anc($xd) {}
10279 }
10280 }
10281 }
10282 set cached_isanc($a,$bend) 0
10283 set cached_isanc($b,$aend) 0
10284 return 0
10285}
b8ab2e17 10286
e11f1233
PM
10287# This identifies whether $desc has an ancestor that is
10288# a growing tip of the graph and which is not an ancestor of $anc
10289# and returns 0 if so and 1 if not.
10290# If we subsequently discover a tag on such a growing tip, and that
10291# turns out to be a descendent of $anc (which it could, since we
10292# don't necessarily see children before parents), then $desc
10293# isn't a good choice to display as a descendent tag of
10294# $anc (since it is the descendent of another tag which is
10295# a descendent of $anc). Similarly, $anc isn't a good choice to
10296# display as a ancestor tag of $desc.
10297#
10298proc is_certain {desc anc} {
10299 global arcnos arcout arcstart arcend growing problems
10300
10301 set certain {}
10302 if {[llength $arcnos($anc)] == 1} {
10303 # tags on the same arc are certain
10304 if {$arcnos($desc) eq $arcnos($anc)} {
10305 return 1
b8ab2e17 10306 }
e11f1233
PM
10307 if {![info exists arcout($anc)]} {
10308 # if $anc is partway along an arc, use the start of the arc instead
10309 set a [lindex $arcnos($anc) 0]
10310 set anc $arcstart($a)
b8ab2e17 10311 }
e11f1233
PM
10312 }
10313 if {[llength $arcnos($desc)] > 1 || [info exists arcout($desc)]} {
10314 set x $desc
10315 } else {
10316 set a [lindex $arcnos($desc) 0]
10317 set x $arcend($a)
10318 }
10319 if {$x == $anc} {
10320 return 1
10321 }
10322 set anclist [list $x]
10323 set dl($x) 1
10324 set nnh 1
10325 set ngrowanc 0
10326 for {set i 0} {$i < [llength $anclist] && ($nnh > 0 || $ngrowanc > 0)} {incr i} {
10327 set x [lindex $anclist $i]
10328 if {$dl($x)} {
10329 incr nnh -1
10330 }
10331 set done($x) 1
10332 foreach a $arcout($x) {
10333 if {[info exists growing($a)]} {
10334 if {![info exists growanc($x)] && $dl($x)} {
10335 set growanc($x) 1
10336 incr ngrowanc
10337 }
10338 } else {
10339 set y $arcend($a)
10340 if {[info exists dl($y)]} {
10341 if {$dl($y)} {
10342 if {!$dl($x)} {
10343 set dl($y) 0
10344 if {![info exists done($y)]} {
10345 incr nnh -1
10346 }
10347 if {[info exists growanc($x)]} {
10348 incr ngrowanc -1
10349 }
10350 set xl [list $y]
10351 for {set k 0} {$k < [llength $xl]} {incr k} {
10352 set z [lindex $xl $k]
10353 foreach c $arcout($z) {
10354 if {[info exists arcend($c)]} {
10355 set v $arcend($c)
10356 if {[info exists dl($v)] && $dl($v)} {
10357 set dl($v) 0
10358 if {![info exists done($v)]} {
10359 incr nnh -1
10360 }
10361 if {[info exists growanc($v)]} {
10362 incr ngrowanc -1
10363 }
10364 lappend xl $v
10365 }
10366 }
10367 }
10368 }
10369 }
10370 }
10371 } elseif {$y eq $anc || !$dl($x)} {
10372 set dl($y) 0
10373 lappend anclist $y
10374 } else {
10375 set dl($y) 1
10376 lappend anclist $y
10377 incr nnh
10378 }
10379 }
b8ab2e17
PM
10380 }
10381 }
e11f1233
PM
10382 foreach x [array names growanc] {
10383 if {$dl($x)} {
10384 return 0
b8ab2e17 10385 }
7eb3cb9c 10386 return 0
b8ab2e17 10387 }
e11f1233 10388 return 1
b8ab2e17
PM
10389}
10390
e11f1233
PM
10391proc validate_arctags {a} {
10392 global arctags idtags
b8ab2e17 10393
e11f1233
PM
10394 set i -1
10395 set na $arctags($a)
10396 foreach id $arctags($a) {
10397 incr i
10398 if {![info exists idtags($id)]} {
10399 set na [lreplace $na $i $i]
10400 incr i -1
10401 }
10402 }
10403 set arctags($a) $na
10404}
10405
10406proc validate_archeads {a} {
10407 global archeads idheads
10408
10409 set i -1
10410 set na $archeads($a)
10411 foreach id $archeads($a) {
10412 incr i
10413 if {![info exists idheads($id)]} {
10414 set na [lreplace $na $i $i]
10415 incr i -1
10416 }
10417 }
10418 set archeads($a) $na
10419}
10420
10421# Return the list of IDs that have tags that are descendents of id,
10422# ignoring IDs that are descendents of IDs already reported.
10423proc desctags {id} {
10424 global arcnos arcstart arcids arctags idtags allparents
10425 global growing cached_dtags
10426
10427 if {![info exists allparents($id)]} {
10428 return {}
10429 }
10430 set t1 [clock clicks -milliseconds]
10431 set argid $id
10432 if {[llength $arcnos($id)] == 1 && [llength $allparents($id)] == 1} {
10433 # part-way along an arc; check that arc first
10434 set a [lindex $arcnos($id) 0]
10435 if {$arctags($a) ne {}} {
10436 validate_arctags $a
10437 set i [lsearch -exact $arcids($a) $id]
10438 set tid {}
10439 foreach t $arctags($a) {
10440 set j [lsearch -exact $arcids($a) $t]
10441 if {$j >= $i} break
10442 set tid $t
b8ab2e17 10443 }
e11f1233
PM
10444 if {$tid ne {}} {
10445 return $tid
b8ab2e17
PM
10446 }
10447 }
e11f1233
PM
10448 set id $arcstart($a)
10449 if {[info exists idtags($id)]} {
10450 return $id
10451 }
10452 }
10453 if {[info exists cached_dtags($id)]} {
10454 return $cached_dtags($id)
10455 }
10456
10457 set origid $id
10458 set todo [list $id]
10459 set queued($id) 1
10460 set nc 1
10461 for {set i 0} {$i < [llength $todo] && $nc > 0} {incr i} {
10462 set id [lindex $todo $i]
10463 set done($id) 1
10464 set ta [info exists hastaggedancestor($id)]
10465 if {!$ta} {
10466 incr nc -1
10467 }
10468 # ignore tags on starting node
10469 if {!$ta && $i > 0} {
10470 if {[info exists idtags($id)]} {
10471 set tagloc($id) $id
10472 set ta 1
10473 } elseif {[info exists cached_dtags($id)]} {
10474 set tagloc($id) $cached_dtags($id)
10475 set ta 1
10476 }
10477 }
10478 foreach a $arcnos($id) {
10479 set d $arcstart($a)
10480 if {!$ta && $arctags($a) ne {}} {
10481 validate_arctags $a
10482 if {$arctags($a) ne {}} {
10483 lappend tagloc($id) [lindex $arctags($a) end]
10484 }
10485 }
10486 if {$ta || $arctags($a) ne {}} {
10487 set tomark [list $d]
10488 for {set j 0} {$j < [llength $tomark]} {incr j} {
10489 set dd [lindex $tomark $j]
10490 if {![info exists hastaggedancestor($dd)]} {
10491 if {[info exists done($dd)]} {
10492 foreach b $arcnos($dd) {
10493 lappend tomark $arcstart($b)
10494 }
10495 if {[info exists tagloc($dd)]} {
10496 unset tagloc($dd)
10497 }
10498 } elseif {[info exists queued($dd)]} {
10499 incr nc -1
10500 }
10501 set hastaggedancestor($dd) 1
10502 }
10503 }
10504 }
10505 if {![info exists queued($d)]} {
10506 lappend todo $d
10507 set queued($d) 1
10508 if {![info exists hastaggedancestor($d)]} {
10509 incr nc
10510 }
10511 }
b8ab2e17 10512 }
f1d83ba3 10513 }
e11f1233
PM
10514 set tags {}
10515 foreach id [array names tagloc] {
10516 if {![info exists hastaggedancestor($id)]} {
10517 foreach t $tagloc($id) {
10518 if {[lsearch -exact $tags $t] < 0} {
10519 lappend tags $t
10520 }
10521 }
10522 }
10523 }
10524 set t2 [clock clicks -milliseconds]
10525 set loopix $i
f1d83ba3 10526
e11f1233
PM
10527 # remove tags that are descendents of other tags
10528 for {set i 0} {$i < [llength $tags]} {incr i} {
10529 set a [lindex $tags $i]
10530 for {set j 0} {$j < $i} {incr j} {
10531 set b [lindex $tags $j]
10532 set r [anc_or_desc $a $b]
10533 if {$r == 1} {
10534 set tags [lreplace $tags $j $j]
10535 incr j -1
10536 incr i -1
10537 } elseif {$r == -1} {
10538 set tags [lreplace $tags $i $i]
10539 incr i -1
10540 break
ceadfe90
PM
10541 }
10542 }
10543 }
10544
e11f1233
PM
10545 if {[array names growing] ne {}} {
10546 # graph isn't finished, need to check if any tag could get
10547 # eclipsed by another tag coming later. Simply ignore any
10548 # tags that could later get eclipsed.
10549 set ctags {}
10550 foreach t $tags {
10551 if {[is_certain $t $origid]} {
10552 lappend ctags $t
10553 }
ceadfe90 10554 }
e11f1233
PM
10555 if {$tags eq $ctags} {
10556 set cached_dtags($origid) $tags
10557 } else {
10558 set tags $ctags
ceadfe90 10559 }
e11f1233
PM
10560 } else {
10561 set cached_dtags($origid) $tags
10562 }
10563 set t3 [clock clicks -milliseconds]
10564 if {0 && $t3 - $t1 >= 100} {
10565 puts "iterating descendents ($loopix/[llength $todo] nodes) took\
10566 [expr {$t2-$t1}]+[expr {$t3-$t2}]ms, $nc candidates left"
ceadfe90 10567 }
e11f1233
PM
10568 return $tags
10569}
ceadfe90 10570
e11f1233
PM
10571proc anctags {id} {
10572 global arcnos arcids arcout arcend arctags idtags allparents
10573 global growing cached_atags
10574
10575 if {![info exists allparents($id)]} {
10576 return {}
10577 }
10578 set t1 [clock clicks -milliseconds]
10579 set argid $id
10580 if {[llength $arcnos($id)] == 1 && [llength $allparents($id)] == 1} {
10581 # part-way along an arc; check that arc first
10582 set a [lindex $arcnos($id) 0]
10583 if {$arctags($a) ne {}} {
10584 validate_arctags $a
10585 set i [lsearch -exact $arcids($a) $id]
10586 foreach t $arctags($a) {
10587 set j [lsearch -exact $arcids($a) $t]
10588 if {$j > $i} {
10589 return $t
10590 }
10591 }
ceadfe90 10592 }
e11f1233
PM
10593 if {![info exists arcend($a)]} {
10594 return {}
10595 }
10596 set id $arcend($a)
10597 if {[info exists idtags($id)]} {
10598 return $id
10599 }
10600 }
10601 if {[info exists cached_atags($id)]} {
10602 return $cached_atags($id)
10603 }
10604
10605 set origid $id
10606 set todo [list $id]
10607 set queued($id) 1
10608 set taglist {}
10609 set nc 1
10610 for {set i 0} {$i < [llength $todo] && $nc > 0} {incr i} {
10611 set id [lindex $todo $i]
10612 set done($id) 1
10613 set td [info exists hastaggeddescendent($id)]
10614 if {!$td} {
10615 incr nc -1
10616 }
10617 # ignore tags on starting node
10618 if {!$td && $i > 0} {
10619 if {[info exists idtags($id)]} {
10620 set tagloc($id) $id
10621 set td 1
10622 } elseif {[info exists cached_atags($id)]} {
10623 set tagloc($id) $cached_atags($id)
10624 set td 1
10625 }
10626 }
10627 foreach a $arcout($id) {
10628 if {!$td && $arctags($a) ne {}} {
10629 validate_arctags $a
10630 if {$arctags($a) ne {}} {
10631 lappend tagloc($id) [lindex $arctags($a) 0]
10632 }
10633 }
10634 if {![info exists arcend($a)]} continue
10635 set d $arcend($a)
10636 if {$td || $arctags($a) ne {}} {
10637 set tomark [list $d]
10638 for {set j 0} {$j < [llength $tomark]} {incr j} {
10639 set dd [lindex $tomark $j]
10640 if {![info exists hastaggeddescendent($dd)]} {
10641 if {[info exists done($dd)]} {
10642 foreach b $arcout($dd) {
10643 if {[info exists arcend($b)]} {
10644 lappend tomark $arcend($b)
10645 }
10646 }
10647 if {[info exists tagloc($dd)]} {
10648 unset tagloc($dd)
10649 }
10650 } elseif {[info exists queued($dd)]} {
10651 incr nc -1
10652 }
10653 set hastaggeddescendent($dd) 1
10654 }
10655 }
10656 }
10657 if {![info exists queued($d)]} {
10658 lappend todo $d
10659 set queued($d) 1
10660 if {![info exists hastaggeddescendent($d)]} {
10661 incr nc
10662 }
10663 }
10664 }
10665 }
10666 set t2 [clock clicks -milliseconds]
10667 set loopix $i
10668 set tags {}
10669 foreach id [array names tagloc] {
10670 if {![info exists hastaggeddescendent($id)]} {
10671 foreach t $tagloc($id) {
10672 if {[lsearch -exact $tags $t] < 0} {
10673 lappend tags $t
10674 }
10675 }
ceadfe90
PM
10676 }
10677 }
ceadfe90 10678
e11f1233
PM
10679 # remove tags that are ancestors of other tags
10680 for {set i 0} {$i < [llength $tags]} {incr i} {
10681 set a [lindex $tags $i]
10682 for {set j 0} {$j < $i} {incr j} {
10683 set b [lindex $tags $j]
10684 set r [anc_or_desc $a $b]
10685 if {$r == -1} {
10686 set tags [lreplace $tags $j $j]
10687 incr j -1
10688 incr i -1
10689 } elseif {$r == 1} {
10690 set tags [lreplace $tags $i $i]
10691 incr i -1
10692 break
10693 }
10694 }
10695 }
10696
10697 if {[array names growing] ne {}} {
10698 # graph isn't finished, need to check if any tag could get
10699 # eclipsed by another tag coming later. Simply ignore any
10700 # tags that could later get eclipsed.
10701 set ctags {}
10702 foreach t $tags {
10703 if {[is_certain $origid $t]} {
10704 lappend ctags $t
10705 }
10706 }
10707 if {$tags eq $ctags} {
10708 set cached_atags($origid) $tags
10709 } else {
10710 set tags $ctags
d6ac1a86 10711 }
e11f1233
PM
10712 } else {
10713 set cached_atags($origid) $tags
10714 }
10715 set t3 [clock clicks -milliseconds]
10716 if {0 && $t3 - $t1 >= 100} {
10717 puts "iterating ancestors ($loopix/[llength $todo] nodes) took\
10718 [expr {$t2-$t1}]+[expr {$t3-$t2}]ms, $nc candidates left"
d6ac1a86 10719 }
e11f1233 10720 return $tags
d6ac1a86
PM
10721}
10722
e11f1233
PM
10723# Return the list of IDs that have heads that are descendents of id,
10724# including id itself if it has a head.
10725proc descheads {id} {
10726 global arcnos arcstart arcids archeads idheads cached_dheads
d809fb17 10727 global allparents arcout
ca6d8f58 10728
e11f1233
PM
10729 if {![info exists allparents($id)]} {
10730 return {}
10731 }
f3326b66 10732 set aret {}
d809fb17 10733 if {![info exists arcout($id)]} {
e11f1233
PM
10734 # part-way along an arc; check it first
10735 set a [lindex $arcnos($id) 0]
10736 if {$archeads($a) ne {}} {
10737 validate_archeads $a
10738 set i [lsearch -exact $arcids($a) $id]
10739 foreach t $archeads($a) {
10740 set j [lsearch -exact $arcids($a) $t]
10741 if {$j > $i} break
f3326b66 10742 lappend aret $t
e11f1233 10743 }
ca6d8f58 10744 }
e11f1233 10745 set id $arcstart($a)
ca6d8f58 10746 }
e11f1233
PM
10747 set origid $id
10748 set todo [list $id]
10749 set seen($id) 1
f3326b66 10750 set ret {}
e11f1233
PM
10751 for {set i 0} {$i < [llength $todo]} {incr i} {
10752 set id [lindex $todo $i]
10753 if {[info exists cached_dheads($id)]} {
10754 set ret [concat $ret $cached_dheads($id)]
10755 } else {
10756 if {[info exists idheads($id)]} {
10757 lappend ret $id
10758 }
10759 foreach a $arcnos($id) {
10760 if {$archeads($a) ne {}} {
706d6c3e
PM
10761 validate_archeads $a
10762 if {$archeads($a) ne {}} {
10763 set ret [concat $ret $archeads($a)]
10764 }
e11f1233
PM
10765 }
10766 set d $arcstart($a)
10767 if {![info exists seen($d)]} {
10768 lappend todo $d
10769 set seen($d) 1
10770 }
10771 }
10299152 10772 }
10299152 10773 }
e11f1233
PM
10774 set ret [lsort -unique $ret]
10775 set cached_dheads($origid) $ret
f3326b66 10776 return [concat $ret $aret]
10299152
PM
10777}
10778
e11f1233
PM
10779proc addedtag {id} {
10780 global arcnos arcout cached_dtags cached_atags
ca6d8f58 10781
e11f1233
PM
10782 if {![info exists arcnos($id)]} return
10783 if {![info exists arcout($id)]} {
10784 recalcarc [lindex $arcnos($id) 0]
ca6d8f58 10785 }
e11f1233
PM
10786 catch {unset cached_dtags}
10787 catch {unset cached_atags}
ca6d8f58
PM
10788}
10789
e11f1233
PM
10790proc addedhead {hid head} {
10791 global arcnos arcout cached_dheads
10792
10793 if {![info exists arcnos($hid)]} return
10794 if {![info exists arcout($hid)]} {
10795 recalcarc [lindex $arcnos($hid) 0]
10796 }
10797 catch {unset cached_dheads}
10798}
10799
10800proc removedhead {hid head} {
10801 global cached_dheads
10802
10803 catch {unset cached_dheads}
10804}
10805
10806proc movedhead {hid head} {
10807 global arcnos arcout cached_dheads
cec7bece 10808
e11f1233
PM
10809 if {![info exists arcnos($hid)]} return
10810 if {![info exists arcout($hid)]} {
10811 recalcarc [lindex $arcnos($hid) 0]
cec7bece 10812 }
e11f1233
PM
10813 catch {unset cached_dheads}
10814}
10815
10816proc changedrefs {} {
587277fe 10817 global cached_dheads cached_dtags cached_atags cached_tagcontent
e11f1233
PM
10818 global arctags archeads arcnos arcout idheads idtags
10819
10820 foreach id [concat [array names idheads] [array names idtags]] {
10821 if {[info exists arcnos($id)] && ![info exists arcout($id)]} {
10822 set a [lindex $arcnos($id) 0]
10823 if {![info exists donearc($a)]} {
10824 recalcarc $a
10825 set donearc($a) 1
10826 }
cec7bece
PM
10827 }
10828 }
587277fe 10829 catch {unset cached_tagcontent}
e11f1233
PM
10830 catch {unset cached_dtags}
10831 catch {unset cached_atags}
10832 catch {unset cached_dheads}
cec7bece
PM
10833}
10834
f1d83ba3 10835proc rereadrefs {} {
fc2a256f 10836 global idtags idheads idotherrefs mainheadid
f1d83ba3
PM
10837
10838 set refids [concat [array names idtags] \
10839 [array names idheads] [array names idotherrefs]]
10840 foreach id $refids {
10841 if {![info exists ref($id)]} {
10842 set ref($id) [listrefs $id]
10843 }
10844 }
fc2a256f 10845 set oldmainhead $mainheadid
f1d83ba3 10846 readrefs
cec7bece 10847 changedrefs
f1d83ba3
PM
10848 set refids [lsort -unique [concat $refids [array names idtags] \
10849 [array names idheads] [array names idotherrefs]]]
10850 foreach id $refids {
10851 set v [listrefs $id]
c11ff120 10852 if {![info exists ref($id)] || $ref($id) != $v} {
f1d83ba3
PM
10853 redrawtags $id
10854 }
10855 }
c11ff120
PM
10856 if {$oldmainhead ne $mainheadid} {
10857 redrawtags $oldmainhead
10858 redrawtags $mainheadid
10859 }
887c996e 10860 run refill_reflist
f1d83ba3
PM
10861}
10862
2e1ded44
JH
10863proc listrefs {id} {
10864 global idtags idheads idotherrefs
10865
10866 set x {}
10867 if {[info exists idtags($id)]} {
10868 set x $idtags($id)
10869 }
10870 set y {}
10871 if {[info exists idheads($id)]} {
10872 set y $idheads($id)
10873 }
10874 set z {}
10875 if {[info exists idotherrefs($id)]} {
10876 set z $idotherrefs($id)
10877 }
10878 return [list $x $y $z]
10879}
10880
106288cb 10881proc showtag {tag isnew} {
587277fe 10882 global ctext cached_tagcontent tagids linknum tagobjid
106288cb
PM
10883
10884 if {$isnew} {
354af6bd 10885 addtohistory [list showtag $tag 0] savectextpos
106288cb
PM
10886 }
10887 $ctext conf -state normal
3ea06f9f 10888 clear_ctext
32f1b3e4 10889 settabs 0
106288cb 10890 set linknum 0
587277fe 10891 if {![info exists cached_tagcontent($tag)]} {
62d3ea65 10892 catch {
685316c4 10893 set cached_tagcontent($tag) [exec git cat-file -p $tag]
62d3ea65
PM
10894 }
10895 }
587277fe
DA
10896 if {[info exists cached_tagcontent($tag)]} {
10897 set text $cached_tagcontent($tag)
106288cb 10898 } else {
d990cedf 10899 set text "[mc "Tag"]: $tag\n[mc "Id"]: $tagids($tag)"
106288cb 10900 }
f1b86294 10901 appendwithlinks $text {}
a80e82f6 10902 maybe_scroll_ctext 1
106288cb 10903 $ctext conf -state disabled
7fcceed7 10904 init_flist {}
106288cb
PM
10905}
10906
1d10f36d
PM
10907proc doquit {} {
10908 global stopped
314f5de1
TA
10909 global gitktmpdir
10910
1d10f36d 10911 set stopped 100
b6047c5a 10912 savestuff .
1d10f36d 10913 destroy .
314f5de1
TA
10914
10915 if {[info exists gitktmpdir]} {
10916 catch {file delete -force $gitktmpdir}
10917 }
1d10f36d 10918}
1db95b00 10919
9a7558f3 10920proc mkfontdisp {font top which} {
d93f1713 10921 global fontattr fontpref $font NS use_ttk
9a7558f3
PM
10922
10923 set fontpref($font) [set $font]
d93f1713 10924 ${NS}::button $top.${font}but -text $which \
9a7558f3 10925 -command [list choosefont $font $which]
d93f1713 10926 ${NS}::label $top.$font -relief flat -font $font \
9a7558f3
PM
10927 -text $fontattr($font,family) -justify left
10928 grid x $top.${font}but $top.$font -sticky w
10929}
10930
10931proc choosefont {font which} {
10932 global fontparam fontlist fonttop fontattr
d93f1713 10933 global prefstop NS
9a7558f3
PM
10934
10935 set fontparam(which) $which
10936 set fontparam(font) $font
10937 set fontparam(family) [font actual $font -family]
10938 set fontparam(size) $fontattr($font,size)
10939 set fontparam(weight) $fontattr($font,weight)
10940 set fontparam(slant) $fontattr($font,slant)
10941 set top .gitkfont
10942 set fonttop $top
10943 if {![winfo exists $top]} {
10944 font create sample
10945 eval font config sample [font actual $font]
d93f1713 10946 ttk_toplevel $top
e7d64008 10947 make_transient $top $prefstop
d990cedf 10948 wm title $top [mc "Gitk font chooser"]
d93f1713 10949 ${NS}::label $top.l -textvariable fontparam(which)
9a7558f3
PM
10950 pack $top.l -side top
10951 set fontlist [lsort [font families]]
d93f1713 10952 ${NS}::frame $top.f
9a7558f3
PM
10953 listbox $top.f.fam -listvariable fontlist \
10954 -yscrollcommand [list $top.f.sb set]
10955 bind $top.f.fam <<ListboxSelect>> selfontfam
d93f1713 10956 ${NS}::scrollbar $top.f.sb -command [list $top.f.fam yview]
9a7558f3
PM
10957 pack $top.f.sb -side right -fill y
10958 pack $top.f.fam -side left -fill both -expand 1
10959 pack $top.f -side top -fill both -expand 1
d93f1713 10960 ${NS}::frame $top.g
9a7558f3
PM
10961 spinbox $top.g.size -from 4 -to 40 -width 4 \
10962 -textvariable fontparam(size) \
10963 -validatecommand {string is integer -strict %s}
10964 checkbutton $top.g.bold -padx 5 \
d990cedf 10965 -font {{Times New Roman} 12 bold} -text [mc "B"] -indicatoron 0 \
9a7558f3
PM
10966 -variable fontparam(weight) -onvalue bold -offvalue normal
10967 checkbutton $top.g.ital -padx 5 \
d990cedf 10968 -font {{Times New Roman} 12 italic} -text [mc "I"] -indicatoron 0 \
9a7558f3
PM
10969 -variable fontparam(slant) -onvalue italic -offvalue roman
10970 pack $top.g.size $top.g.bold $top.g.ital -side left
10971 pack $top.g -side top
10972 canvas $top.c -width 150 -height 50 -border 2 -relief sunk \
10973 -background white
10974 $top.c create text 100 25 -anchor center -text $which -font sample \
10975 -fill black -tags text
10976 bind $top.c <Configure> [list centertext $top.c]
10977 pack $top.c -side top -fill x
d93f1713
PT
10978 ${NS}::frame $top.buts
10979 ${NS}::button $top.buts.ok -text [mc "OK"] -command fontok -default active
10980 ${NS}::button $top.buts.can -text [mc "Cancel"] -command fontcan -default normal
76f15947
AG
10981 bind $top <Key-Return> fontok
10982 bind $top <Key-Escape> fontcan
9a7558f3
PM
10983 grid $top.buts.ok $top.buts.can
10984 grid columnconfigure $top.buts 0 -weight 1 -uniform a
10985 grid columnconfigure $top.buts 1 -weight 1 -uniform a
10986 pack $top.buts -side bottom -fill x
10987 trace add variable fontparam write chg_fontparam
10988 } else {
10989 raise $top
10990 $top.c itemconf text -text $which
10991 }
10992 set i [lsearch -exact $fontlist $fontparam(family)]
10993 if {$i >= 0} {
10994 $top.f.fam selection set $i
10995 $top.f.fam see $i
10996 }
10997}
10998
10999proc centertext {w} {
11000 $w coords text [expr {[winfo width $w] / 2}] [expr {[winfo height $w] / 2}]
11001}
11002
11003proc fontok {} {
11004 global fontparam fontpref prefstop
11005
11006 set f $fontparam(font)
11007 set fontpref($f) [list $fontparam(family) $fontparam(size)]
11008 if {$fontparam(weight) eq "bold"} {
11009 lappend fontpref($f) "bold"
11010 }
11011 if {$fontparam(slant) eq "italic"} {
11012 lappend fontpref($f) "italic"
11013 }
39ddf99c 11014 set w $prefstop.notebook.fonts.$f
9a7558f3 11015 $w conf -text $fontparam(family) -font $fontpref($f)
d93f1713 11016
9a7558f3
PM
11017 fontcan
11018}
11019
11020proc fontcan {} {
11021 global fonttop fontparam
11022
11023 if {[info exists fonttop]} {
11024 catch {destroy $fonttop}
11025 catch {font delete sample}
11026 unset fonttop
11027 unset fontparam
11028 }
11029}
11030
d93f1713
PT
11031if {[package vsatisfies [package provide Tk] 8.6]} {
11032 # In Tk 8.6 we have a native font chooser dialog. Overwrite the above
11033 # function to make use of it.
11034 proc choosefont {font which} {
11035 tk fontchooser configure -title $which -font $font \
11036 -command [list on_choosefont $font $which]
11037 tk fontchooser show
11038 }
11039 proc on_choosefont {font which newfont} {
11040 global fontparam
11041 puts stderr "$font $newfont"
11042 array set f [font actual $newfont]
11043 set fontparam(which) $which
11044 set fontparam(font) $font
11045 set fontparam(family) $f(-family)
11046 set fontparam(size) $f(-size)
11047 set fontparam(weight) $f(-weight)
11048 set fontparam(slant) $f(-slant)
11049 fontok
11050 }
11051}
11052
9a7558f3
PM
11053proc selfontfam {} {
11054 global fonttop fontparam
11055
11056 set i [$fonttop.f.fam curselection]
11057 if {$i ne {}} {
11058 set fontparam(family) [$fonttop.f.fam get $i]
11059 }
11060}
11061
11062proc chg_fontparam {v sub op} {
11063 global fontparam
11064
11065 font config sample -$sub $fontparam($sub)
11066}
11067
44acce0b
PT
11068# Create a property sheet tab page
11069proc create_prefs_page {w} {
11070 global NS
11071 set parent [join [lrange [split $w .] 0 end-1] .]
11072 if {[winfo class $parent] eq "TNotebook"} {
11073 ${NS}::frame $w
11074 } else {
11075 ${NS}::labelframe $w
11076 }
11077}
11078
11079proc prefspage_general {notebook} {
11080 global NS maxwidth maxgraphpct showneartags showlocalchanges
11081 global tabstop limitdiffs autoselect autosellen extdifftool perfile_attrs
d34835c9 11082 global hideremotes want_ttk have_ttk maxrefs
44acce0b
PT
11083
11084 set page [create_prefs_page $notebook.general]
11085
11086 ${NS}::label $page.ldisp -text [mc "Commit list display options"]
11087 grid $page.ldisp - -sticky w -pady 10
11088 ${NS}::label $page.spacer -text " "
11089 ${NS}::label $page.maxwidthl -text [mc "Maximum graph width (lines)"]
11090 spinbox $page.maxwidth -from 0 -to 100 -width 4 -textvariable maxwidth
11091 grid $page.spacer $page.maxwidthl $page.maxwidth -sticky w
11092 ${NS}::label $page.maxpctl -text [mc "Maximum graph width (% of pane)"]
11093 spinbox $page.maxpct -from 1 -to 100 -width 4 -textvariable maxgraphpct
11094 grid x $page.maxpctl $page.maxpct -sticky w
11095 ${NS}::checkbutton $page.showlocal -text [mc "Show local changes"] \
11096 -variable showlocalchanges
11097 grid x $page.showlocal -sticky w
11098 ${NS}::checkbutton $page.autoselect -text [mc "Auto-select SHA1 (length)"] \
11099 -variable autoselect
11100 spinbox $page.autosellen -from 1 -to 40 -width 4 -textvariable autosellen
11101 grid x $page.autoselect $page.autosellen -sticky w
11102 ${NS}::checkbutton $page.hideremotes -text [mc "Hide remote refs"] \
11103 -variable hideremotes
11104 grid x $page.hideremotes -sticky w
11105
11106 ${NS}::label $page.ddisp -text [mc "Diff display options"]
11107 grid $page.ddisp - -sticky w -pady 10
11108 ${NS}::label $page.tabstopl -text [mc "Tab spacing"]
11109 spinbox $page.tabstop -from 1 -to 20 -width 4 -textvariable tabstop
11110 grid x $page.tabstopl $page.tabstop -sticky w
d34835c9 11111 ${NS}::checkbutton $page.ntag -text [mc "Display nearby tags/heads"] \
44acce0b
PT
11112 -variable showneartags
11113 grid x $page.ntag -sticky w
d34835c9
PM
11114 ${NS}::label $page.maxrefsl -text [mc "Maximum # tags/heads to show"]
11115 spinbox $page.maxrefs -from 1 -to 1000 -width 4 -textvariable maxrefs
11116 grid x $page.maxrefsl $page.maxrefs -sticky w
44acce0b
PT
11117 ${NS}::checkbutton $page.ldiff -text [mc "Limit diffs to listed paths"] \
11118 -variable limitdiffs
11119 grid x $page.ldiff -sticky w
11120 ${NS}::checkbutton $page.lattr -text [mc "Support per-file encodings"] \
11121 -variable perfile_attrs
11122 grid x $page.lattr -sticky w
11123
11124 ${NS}::entry $page.extdifft -textvariable extdifftool
11125 ${NS}::frame $page.extdifff
11126 ${NS}::label $page.extdifff.l -text [mc "External diff tool" ]
11127 ${NS}::button $page.extdifff.b -text [mc "Choose..."] -command choose_extdiff
11128 pack $page.extdifff.l $page.extdifff.b -side left
11129 pack configure $page.extdifff.l -padx 10
11130 grid x $page.extdifff $page.extdifft -sticky ew
11131
11132 ${NS}::label $page.lgen -text [mc "General options"]
11133 grid $page.lgen - -sticky w -pady 10
11134 ${NS}::checkbutton $page.want_ttk -variable want_ttk \
11135 -text [mc "Use themed widgets"]
11136 if {$have_ttk} {
11137 ${NS}::label $page.ttk_note -text [mc "(change requires restart)"]
11138 } else {
11139 ${NS}::label $page.ttk_note -text [mc "(currently unavailable)"]
11140 }
11141 grid x $page.want_ttk $page.ttk_note -sticky w
11142 return $page
11143}
11144
11145proc prefspage_colors {notebook} {
11146 global NS uicolor bgcolor fgcolor ctext diffcolors selectbgcolor markbgcolor
11147
11148 set page [create_prefs_page $notebook.colors]
11149
11150 ${NS}::label $page.cdisp -text [mc "Colors: press to choose"]
11151 grid $page.cdisp - -sticky w -pady 10
11152 label $page.ui -padx 40 -relief sunk -background $uicolor
11153 ${NS}::button $page.uibut -text [mc "Interface"] \
11154 -command [list choosecolor uicolor {} $page.ui [mc "interface"] setui]
11155 grid x $page.uibut $page.ui -sticky w
11156 label $page.bg -padx 40 -relief sunk -background $bgcolor
11157 ${NS}::button $page.bgbut -text [mc "Background"] \
11158 -command [list choosecolor bgcolor {} $page.bg [mc "background"] setbg]
11159 grid x $page.bgbut $page.bg -sticky w
11160 label $page.fg -padx 40 -relief sunk -background $fgcolor
11161 ${NS}::button $page.fgbut -text [mc "Foreground"] \
11162 -command [list choosecolor fgcolor {} $page.fg [mc "foreground"] setfg]
11163 grid x $page.fgbut $page.fg -sticky w
11164 label $page.diffold -padx 40 -relief sunk -background [lindex $diffcolors 0]
11165 ${NS}::button $page.diffoldbut -text [mc "Diff: old lines"] \
11166 -command [list choosecolor diffcolors 0 $page.diffold [mc "diff old lines"] \
11167 [list $ctext tag conf d0 -foreground]]
11168 grid x $page.diffoldbut $page.diffold -sticky w
11169 label $page.diffnew -padx 40 -relief sunk -background [lindex $diffcolors 1]
11170 ${NS}::button $page.diffnewbut -text [mc "Diff: new lines"] \
11171 -command [list choosecolor diffcolors 1 $page.diffnew [mc "diff new lines"] \
11172 [list $ctext tag conf dresult -foreground]]
11173 grid x $page.diffnewbut $page.diffnew -sticky w
11174 label $page.hunksep -padx 40 -relief sunk -background [lindex $diffcolors 2]
11175 ${NS}::button $page.hunksepbut -text [mc "Diff: hunk header"] \
11176 -command [list choosecolor diffcolors 2 $page.hunksep \
11177 [mc "diff hunk header"] \
11178 [list $ctext tag conf hunksep -foreground]]
11179 grid x $page.hunksepbut $page.hunksep -sticky w
11180 label $page.markbgsep -padx 40 -relief sunk -background $markbgcolor
11181 ${NS}::button $page.markbgbut -text [mc "Marked line bg"] \
11182 -command [list choosecolor markbgcolor {} $page.markbgsep \
11183 [mc "marked line background"] \
11184 [list $ctext tag conf omark -background]]
11185 grid x $page.markbgbut $page.markbgsep -sticky w
11186 label $page.selbgsep -padx 40 -relief sunk -background $selectbgcolor
11187 ${NS}::button $page.selbgbut -text [mc "Select bg"] \
11188 -command [list choosecolor selectbgcolor {} $page.selbgsep [mc "background"] setselbg]
11189 grid x $page.selbgbut $page.selbgsep -sticky w
11190 return $page
11191}
11192
11193proc prefspage_fonts {notebook} {
11194 global NS
11195 set page [create_prefs_page $notebook.fonts]
11196 ${NS}::label $page.cfont -text [mc "Fonts: press to choose"]
11197 grid $page.cfont - -sticky w -pady 10
11198 mkfontdisp mainfont $page [mc "Main font"]
11199 mkfontdisp textfont $page [mc "Diff display font"]
11200 mkfontdisp uifont $page [mc "User interface font"]
11201 return $page
11202}
11203
712fcc08 11204proc doprefs {} {
d93f1713 11205 global maxwidth maxgraphpct use_ttk NS
219ea3a9 11206 global oldprefs prefstop showneartags showlocalchanges
5497f7a2 11207 global uicolor bgcolor fgcolor ctext diffcolors selectbgcolor markbgcolor
21ac8a8d 11208 global tabstop limitdiffs autoselect autosellen extdifftool perfile_attrs
0cc08ff7 11209 global hideremotes want_ttk have_ttk
232475d3 11210
712fcc08
PM
11211 set top .gitkprefs
11212 set prefstop $top
11213 if {[winfo exists $top]} {
11214 raise $top
11215 return
757f17bc 11216 }
3de07118 11217 foreach v {maxwidth maxgraphpct showneartags showlocalchanges \
0cc08ff7 11218 limitdiffs tabstop perfile_attrs hideremotes want_ttk} {
712fcc08 11219 set oldprefs($v) [set $v]
232475d3 11220 }
d93f1713 11221 ttk_toplevel $top
d990cedf 11222 wm title $top [mc "Gitk preferences"]
e7d64008 11223 make_transient $top .
44acce0b
PT
11224
11225 if {[set use_notebook [expr {$use_ttk && [info command ::ttk::notebook] ne ""}]]} {
11226 set notebook [ttk::notebook $top.notebook]
0cc08ff7 11227 } else {
44acce0b
PT
11228 set notebook [${NS}::frame $top.notebook -borderwidth 0 -relief flat]
11229 }
11230
11231 lappend pages [prefspage_general $notebook] [mc "General"]
11232 lappend pages [prefspage_colors $notebook] [mc "Colors"]
11233 lappend pages [prefspage_fonts $notebook] [mc "Fonts"]
28cb7074 11234 set col 0
44acce0b
PT
11235 foreach {page title} $pages {
11236 if {$use_notebook} {
11237 $notebook add $page -text $title
11238 } else {
11239 set btn [${NS}::button $notebook.b_[string map {. X} $page] \
11240 -text $title -command [list raise $page]]
11241 $page configure -text $title
11242 grid $btn -row 0 -column [incr col] -sticky w
11243 grid $page -row 1 -column 0 -sticky news -columnspan 100
11244 }
11245 }
11246
11247 if {!$use_notebook} {
11248 grid columnconfigure $notebook 0 -weight 1
11249 grid rowconfigure $notebook 1 -weight 1
11250 raise [lindex $pages 0]
11251 }
11252
11253 grid $notebook -sticky news -padx 2 -pady 2
11254 grid rowconfigure $top 0 -weight 1
11255 grid columnconfigure $top 0 -weight 1
9a7558f3 11256
d93f1713
PT
11257 ${NS}::frame $top.buts
11258 ${NS}::button $top.buts.ok -text [mc "OK"] -command prefsok -default active
11259 ${NS}::button $top.buts.can -text [mc "Cancel"] -command prefscan -default normal
76f15947
AG
11260 bind $top <Key-Return> prefsok
11261 bind $top <Key-Escape> prefscan
712fcc08
PM
11262 grid $top.buts.ok $top.buts.can
11263 grid columnconfigure $top.buts 0 -weight 1 -uniform a
11264 grid columnconfigure $top.buts 1 -weight 1 -uniform a
11265 grid $top.buts - - -pady 10 -sticky ew
d93f1713 11266 grid columnconfigure $top 2 -weight 1
44acce0b 11267 bind $top <Visibility> [list focus $top.buts.ok]
712fcc08
PM
11268}
11269
314f5de1
TA
11270proc choose_extdiff {} {
11271 global extdifftool
11272
b56e0a9a 11273 set prog [tk_getOpenFile -title [mc "External diff tool"] -multiple false]
314f5de1
TA
11274 if {$prog ne {}} {
11275 set extdifftool $prog
11276 }
11277}
11278
f8a2c0d1
PM
11279proc choosecolor {v vi w x cmd} {
11280 global $v
11281
11282 set c [tk_chooseColor -initialcolor [lindex [set $v] $vi] \
d990cedf 11283 -title [mc "Gitk: choose color for %s" $x]]
f8a2c0d1
PM
11284 if {$c eq {}} return
11285 $w conf -background $c
11286 lset $v $vi $c
11287 eval $cmd $c
11288}
11289
60378c0c
ML
11290proc setselbg {c} {
11291 global bglist cflist
11292 foreach w $bglist {
11293 $w configure -selectbackground $c
11294 }
11295 $cflist tag configure highlight \
11296 -background [$cflist cget -selectbackground]
11297 allcanvs itemconf secsel -fill $c
11298}
11299
51a7e8b6
PM
11300# This sets the background color and the color scheme for the whole UI.
11301# For some reason, tk_setPalette chooses a nasty dark red for selectColor
11302# if we don't specify one ourselves, which makes the checkbuttons and
11303# radiobuttons look bad. This chooses white for selectColor if the
11304# background color is light, or black if it is dark.
5497f7a2 11305proc setui {c} {
2e58c944 11306 if {[tk windowingsystem] eq "win32"} { return }
51a7e8b6
PM
11307 set bg [winfo rgb . $c]
11308 set selc black
11309 if {[lindex $bg 0] + 1.5 * [lindex $bg 1] + 0.5 * [lindex $bg 2] > 100000} {
11310 set selc white
11311 }
11312 tk_setPalette background $c selectColor $selc
5497f7a2
GR
11313}
11314
f8a2c0d1
PM
11315proc setbg {c} {
11316 global bglist
11317
11318 foreach w $bglist {
11319 $w conf -background $c
11320 }
11321}
11322
11323proc setfg {c} {
11324 global fglist canv
11325
11326 foreach w $fglist {
11327 $w conf -foreground $c
11328 }
11329 allcanvs itemconf text -fill $c
11330 $canv itemconf circle -outline $c
b9fdba7f 11331 $canv itemconf markid -outline $c
f8a2c0d1
PM
11332}
11333
712fcc08 11334proc prefscan {} {
94503918 11335 global oldprefs prefstop
712fcc08 11336
3de07118 11337 foreach v {maxwidth maxgraphpct showneartags showlocalchanges \
0cc08ff7 11338 limitdiffs tabstop perfile_attrs hideremotes want_ttk} {
94503918 11339 global $v
712fcc08
PM
11340 set $v $oldprefs($v)
11341 }
11342 catch {destroy $prefstop}
11343 unset prefstop
9a7558f3 11344 fontcan
712fcc08
PM
11345}
11346
11347proc prefsok {} {
11348 global maxwidth maxgraphpct
219ea3a9 11349 global oldprefs prefstop showneartags showlocalchanges
9a7558f3 11350 global fontpref mainfont textfont uifont
39ee47ef 11351 global limitdiffs treediffs perfile_attrs
ffe15297 11352 global hideremotes
712fcc08
PM
11353
11354 catch {destroy $prefstop}
11355 unset prefstop
9a7558f3
PM
11356 fontcan
11357 set fontchanged 0
11358 if {$mainfont ne $fontpref(mainfont)} {
11359 set mainfont $fontpref(mainfont)
11360 parsefont mainfont $mainfont
11361 eval font configure mainfont [fontflags mainfont]
11362 eval font configure mainfontbold [fontflags mainfont 1]
11363 setcoords
11364 set fontchanged 1
11365 }
11366 if {$textfont ne $fontpref(textfont)} {
11367 set textfont $fontpref(textfont)
11368 parsefont textfont $textfont
11369 eval font configure textfont [fontflags textfont]
11370 eval font configure textfontbold [fontflags textfont 1]
11371 }
11372 if {$uifont ne $fontpref(uifont)} {
11373 set uifont $fontpref(uifont)
11374 parsefont uifont $uifont
11375 eval font configure uifont [fontflags uifont]
11376 }
32f1b3e4 11377 settabs
219ea3a9
PM
11378 if {$showlocalchanges != $oldprefs(showlocalchanges)} {
11379 if {$showlocalchanges} {
11380 doshowlocalchanges
11381 } else {
11382 dohidelocalchanges
11383 }
11384 }
39ee47ef
PM
11385 if {$limitdiffs != $oldprefs(limitdiffs) ||
11386 ($perfile_attrs && !$oldprefs(perfile_attrs))} {
11387 # treediffs elements are limited by path;
11388 # won't have encodings cached if perfile_attrs was just turned on
74a40c71
PM
11389 catch {unset treediffs}
11390 }
9a7558f3 11391 if {$fontchanged || $maxwidth != $oldprefs(maxwidth)
712fcc08
PM
11392 || $maxgraphpct != $oldprefs(maxgraphpct)} {
11393 redisplay
7a39a17a
PM
11394 } elseif {$showneartags != $oldprefs(showneartags) ||
11395 $limitdiffs != $oldprefs(limitdiffs)} {
b8ab2e17 11396 reselectline
712fcc08 11397 }
ffe15297
TR
11398 if {$hideremotes != $oldprefs(hideremotes)} {
11399 rereadrefs
11400 }
712fcc08
PM
11401}
11402
11403proc formatdate {d} {
e8b5f4be 11404 global datetimeformat
219ea3a9 11405 if {$d ne {}} {
f5974d97 11406 set d [clock format [lindex $d 0] -format $datetimeformat]
219ea3a9
PM
11407 }
11408 return $d
232475d3
PM
11409}
11410
fd8ccbec
PM
11411# This list of encoding names and aliases is distilled from
11412# http://www.iana.org/assignments/character-sets.
11413# Not all of them are supported by Tcl.
11414set encoding_aliases {
11415 { ANSI_X3.4-1968 iso-ir-6 ANSI_X3.4-1986 ISO_646.irv:1991 ASCII
11416 ISO646-US US-ASCII us IBM367 cp367 csASCII }
11417 { ISO-10646-UTF-1 csISO10646UTF1 }
11418 { ISO_646.basic:1983 ref csISO646basic1983 }
11419 { INVARIANT csINVARIANT }
11420 { ISO_646.irv:1983 iso-ir-2 irv csISO2IntlRefVersion }
11421 { BS_4730 iso-ir-4 ISO646-GB gb uk csISO4UnitedKingdom }
11422 { NATS-SEFI iso-ir-8-1 csNATSSEFI }
11423 { NATS-SEFI-ADD iso-ir-8-2 csNATSSEFIADD }
11424 { NATS-DANO iso-ir-9-1 csNATSDANO }
11425 { NATS-DANO-ADD iso-ir-9-2 csNATSDANOADD }
11426 { SEN_850200_B iso-ir-10 FI ISO646-FI ISO646-SE se csISO10Swedish }
11427 { SEN_850200_C iso-ir-11 ISO646-SE2 se2 csISO11SwedishForNames }
11428 { KS_C_5601-1987 iso-ir-149 KS_C_5601-1989 KSC_5601 korean csKSC56011987 }
11429 { ISO-2022-KR csISO2022KR }
11430 { EUC-KR csEUCKR }
11431 { ISO-2022-JP csISO2022JP }
11432 { ISO-2022-JP-2 csISO2022JP2 }
11433 { JIS_C6220-1969-jp JIS_C6220-1969 iso-ir-13 katakana x0201-7
11434 csISO13JISC6220jp }
11435 { JIS_C6220-1969-ro iso-ir-14 jp ISO646-JP csISO14JISC6220ro }
11436 { IT iso-ir-15 ISO646-IT csISO15Italian }
11437 { PT iso-ir-16 ISO646-PT csISO16Portuguese }
11438 { ES iso-ir-17 ISO646-ES csISO17Spanish }
11439 { greek7-old iso-ir-18 csISO18Greek7Old }
11440 { latin-greek iso-ir-19 csISO19LatinGreek }
11441 { DIN_66003 iso-ir-21 de ISO646-DE csISO21German }
11442 { NF_Z_62-010_(1973) iso-ir-25 ISO646-FR1 csISO25French }
11443 { Latin-greek-1 iso-ir-27 csISO27LatinGreek1 }
11444 { ISO_5427 iso-ir-37 csISO5427Cyrillic }
11445 { JIS_C6226-1978 iso-ir-42 csISO42JISC62261978 }
11446 { BS_viewdata iso-ir-47 csISO47BSViewdata }
11447 { INIS iso-ir-49 csISO49INIS }
11448 { INIS-8 iso-ir-50 csISO50INIS8 }
11449 { INIS-cyrillic iso-ir-51 csISO51INISCyrillic }
11450 { ISO_5427:1981 iso-ir-54 ISO5427Cyrillic1981 }
11451 { ISO_5428:1980 iso-ir-55 csISO5428Greek }
11452 { GB_1988-80 iso-ir-57 cn ISO646-CN csISO57GB1988 }
11453 { GB_2312-80 iso-ir-58 chinese csISO58GB231280 }
11454 { NS_4551-1 iso-ir-60 ISO646-NO no csISO60DanishNorwegian
11455 csISO60Norwegian1 }
11456 { NS_4551-2 ISO646-NO2 iso-ir-61 no2 csISO61Norwegian2 }
11457 { NF_Z_62-010 iso-ir-69 ISO646-FR fr csISO69French }
11458 { videotex-suppl iso-ir-70 csISO70VideotexSupp1 }
11459 { PT2 iso-ir-84 ISO646-PT2 csISO84Portuguese2 }
11460 { ES2 iso-ir-85 ISO646-ES2 csISO85Spanish2 }
11461 { MSZ_7795.3 iso-ir-86 ISO646-HU hu csISO86Hungarian }
11462 { JIS_C6226-1983 iso-ir-87 x0208 JIS_X0208-1983 csISO87JISX0208 }
11463 { greek7 iso-ir-88 csISO88Greek7 }
11464 { ASMO_449 ISO_9036 arabic7 iso-ir-89 csISO89ASMO449 }
11465 { iso-ir-90 csISO90 }
11466 { JIS_C6229-1984-a iso-ir-91 jp-ocr-a csISO91JISC62291984a }
11467 { JIS_C6229-1984-b iso-ir-92 ISO646-JP-OCR-B jp-ocr-b
11468 csISO92JISC62991984b }
11469 { JIS_C6229-1984-b-add iso-ir-93 jp-ocr-b-add csISO93JIS62291984badd }
11470 { JIS_C6229-1984-hand iso-ir-94 jp-ocr-hand csISO94JIS62291984hand }
11471 { JIS_C6229-1984-hand-add iso-ir-95 jp-ocr-hand-add
11472 csISO95JIS62291984handadd }
11473 { JIS_C6229-1984-kana iso-ir-96 csISO96JISC62291984kana }
11474 { ISO_2033-1983 iso-ir-98 e13b csISO2033 }
11475 { ANSI_X3.110-1983 iso-ir-99 CSA_T500-1983 NAPLPS csISO99NAPLPS }
11476 { ISO_8859-1:1987 iso-ir-100 ISO_8859-1 ISO-8859-1 latin1 l1 IBM819
11477 CP819 csISOLatin1 }
11478 { ISO_8859-2:1987 iso-ir-101 ISO_8859-2 ISO-8859-2 latin2 l2 csISOLatin2 }
11479 { T.61-7bit iso-ir-102 csISO102T617bit }
11480 { T.61-8bit T.61 iso-ir-103 csISO103T618bit }
11481 { ISO_8859-3:1988 iso-ir-109 ISO_8859-3 ISO-8859-3 latin3 l3 csISOLatin3 }
11482 { ISO_8859-4:1988 iso-ir-110 ISO_8859-4 ISO-8859-4 latin4 l4 csISOLatin4 }
11483 { ECMA-cyrillic iso-ir-111 KOI8-E csISO111ECMACyrillic }
11484 { CSA_Z243.4-1985-1 iso-ir-121 ISO646-CA csa7-1 ca csISO121Canadian1 }
11485 { CSA_Z243.4-1985-2 iso-ir-122 ISO646-CA2 csa7-2 csISO122Canadian2 }
11486 { CSA_Z243.4-1985-gr iso-ir-123 csISO123CSAZ24341985gr }
11487 { ISO_8859-6:1987 iso-ir-127 ISO_8859-6 ISO-8859-6 ECMA-114 ASMO-708
11488 arabic csISOLatinArabic }
11489 { ISO_8859-6-E csISO88596E ISO-8859-6-E }
11490 { ISO_8859-6-I csISO88596I ISO-8859-6-I }
11491 { ISO_8859-7:1987 iso-ir-126 ISO_8859-7 ISO-8859-7 ELOT_928 ECMA-118
11492 greek greek8 csISOLatinGreek }
11493 { T.101-G2 iso-ir-128 csISO128T101G2 }
11494 { ISO_8859-8:1988 iso-ir-138 ISO_8859-8 ISO-8859-8 hebrew
11495 csISOLatinHebrew }
11496 { ISO_8859-8-E csISO88598E ISO-8859-8-E }
11497 { ISO_8859-8-I csISO88598I ISO-8859-8-I }
11498 { CSN_369103 iso-ir-139 csISO139CSN369103 }
11499 { JUS_I.B1.002 iso-ir-141 ISO646-YU js yu csISO141JUSIB1002 }
11500 { ISO_6937-2-add iso-ir-142 csISOTextComm }
11501 { IEC_P27-1 iso-ir-143 csISO143IECP271 }
11502 { ISO_8859-5:1988 iso-ir-144 ISO_8859-5 ISO-8859-5 cyrillic
11503 csISOLatinCyrillic }
11504 { JUS_I.B1.003-serb iso-ir-146 serbian csISO146Serbian }
11505 { JUS_I.B1.003-mac macedonian iso-ir-147 csISO147Macedonian }
11506 { ISO_8859-9:1989 iso-ir-148 ISO_8859-9 ISO-8859-9 latin5 l5 csISOLatin5 }
11507 { greek-ccitt iso-ir-150 csISO150 csISO150GreekCCITT }
11508 { NC_NC00-10:81 cuba iso-ir-151 ISO646-CU csISO151Cuba }
11509 { ISO_6937-2-25 iso-ir-152 csISO6937Add }
11510 { GOST_19768-74 ST_SEV_358-88 iso-ir-153 csISO153GOST1976874 }
11511 { ISO_8859-supp iso-ir-154 latin1-2-5 csISO8859Supp }
11512 { ISO_10367-box iso-ir-155 csISO10367Box }
11513 { ISO-8859-10 iso-ir-157 l6 ISO_8859-10:1992 csISOLatin6 latin6 }
11514 { latin-lap lap iso-ir-158 csISO158Lap }
11515 { JIS_X0212-1990 x0212 iso-ir-159 csISO159JISX02121990 }
11516 { DS_2089 DS2089 ISO646-DK dk csISO646Danish }
11517 { us-dk csUSDK }
11518 { dk-us csDKUS }
11519 { JIS_X0201 X0201 csHalfWidthKatakana }
11520 { KSC5636 ISO646-KR csKSC5636 }
11521 { ISO-10646-UCS-2 csUnicode }
11522 { ISO-10646-UCS-4 csUCS4 }
11523 { DEC-MCS dec csDECMCS }
11524 { hp-roman8 roman8 r8 csHPRoman8 }
11525 { macintosh mac csMacintosh }
11526 { IBM037 cp037 ebcdic-cp-us ebcdic-cp-ca ebcdic-cp-wt ebcdic-cp-nl
11527 csIBM037 }
11528 { IBM038 EBCDIC-INT cp038 csIBM038 }
11529 { IBM273 CP273 csIBM273 }
11530 { IBM274 EBCDIC-BE CP274 csIBM274 }
11531 { IBM275 EBCDIC-BR cp275 csIBM275 }
11532 { IBM277 EBCDIC-CP-DK EBCDIC-CP-NO csIBM277 }
11533 { IBM278 CP278 ebcdic-cp-fi ebcdic-cp-se csIBM278 }
11534 { IBM280 CP280 ebcdic-cp-it csIBM280 }
11535 { IBM281 EBCDIC-JP-E cp281 csIBM281 }
11536 { IBM284 CP284 ebcdic-cp-es csIBM284 }
11537 { IBM285 CP285 ebcdic-cp-gb csIBM285 }
11538 { IBM290 cp290 EBCDIC-JP-kana csIBM290 }
11539 { IBM297 cp297 ebcdic-cp-fr csIBM297 }
11540 { IBM420 cp420 ebcdic-cp-ar1 csIBM420 }
11541 { IBM423 cp423 ebcdic-cp-gr csIBM423 }
11542 { IBM424 cp424 ebcdic-cp-he csIBM424 }
11543 { IBM437 cp437 437 csPC8CodePage437 }
11544 { IBM500 CP500 ebcdic-cp-be ebcdic-cp-ch csIBM500 }
11545 { IBM775 cp775 csPC775Baltic }
11546 { IBM850 cp850 850 csPC850Multilingual }
11547 { IBM851 cp851 851 csIBM851 }
11548 { IBM852 cp852 852 csPCp852 }
11549 { IBM855 cp855 855 csIBM855 }
11550 { IBM857 cp857 857 csIBM857 }
11551 { IBM860 cp860 860 csIBM860 }
11552 { IBM861 cp861 861 cp-is csIBM861 }
11553 { IBM862 cp862 862 csPC862LatinHebrew }
11554 { IBM863 cp863 863 csIBM863 }
11555 { IBM864 cp864 csIBM864 }
11556 { IBM865 cp865 865 csIBM865 }
11557 { IBM866 cp866 866 csIBM866 }
11558 { IBM868 CP868 cp-ar csIBM868 }
11559 { IBM869 cp869 869 cp-gr csIBM869 }
11560 { IBM870 CP870 ebcdic-cp-roece ebcdic-cp-yu csIBM870 }
11561 { IBM871 CP871 ebcdic-cp-is csIBM871 }
11562 { IBM880 cp880 EBCDIC-Cyrillic csIBM880 }
11563 { IBM891 cp891 csIBM891 }
11564 { IBM903 cp903 csIBM903 }
11565 { IBM904 cp904 904 csIBBM904 }
11566 { IBM905 CP905 ebcdic-cp-tr csIBM905 }
11567 { IBM918 CP918 ebcdic-cp-ar2 csIBM918 }
11568 { IBM1026 CP1026 csIBM1026 }
11569 { EBCDIC-AT-DE csIBMEBCDICATDE }
11570 { EBCDIC-AT-DE-A csEBCDICATDEA }
11571 { EBCDIC-CA-FR csEBCDICCAFR }
11572 { EBCDIC-DK-NO csEBCDICDKNO }
11573 { EBCDIC-DK-NO-A csEBCDICDKNOA }
11574 { EBCDIC-FI-SE csEBCDICFISE }
11575 { EBCDIC-FI-SE-A csEBCDICFISEA }
11576 { EBCDIC-FR csEBCDICFR }
11577 { EBCDIC-IT csEBCDICIT }
11578 { EBCDIC-PT csEBCDICPT }
11579 { EBCDIC-ES csEBCDICES }
11580 { EBCDIC-ES-A csEBCDICESA }
11581 { EBCDIC-ES-S csEBCDICESS }
11582 { EBCDIC-UK csEBCDICUK }
11583 { EBCDIC-US csEBCDICUS }
11584 { UNKNOWN-8BIT csUnknown8BiT }
11585 { MNEMONIC csMnemonic }
11586 { MNEM csMnem }
11587 { VISCII csVISCII }
11588 { VIQR csVIQR }
11589 { KOI8-R csKOI8R }
11590 { IBM00858 CCSID00858 CP00858 PC-Multilingual-850+euro }
11591 { IBM00924 CCSID00924 CP00924 ebcdic-Latin9--euro }
11592 { IBM01140 CCSID01140 CP01140 ebcdic-us-37+euro }
11593 { IBM01141 CCSID01141 CP01141 ebcdic-de-273+euro }
11594 { IBM01142 CCSID01142 CP01142 ebcdic-dk-277+euro ebcdic-no-277+euro }
11595 { IBM01143 CCSID01143 CP01143 ebcdic-fi-278+euro ebcdic-se-278+euro }
11596 { IBM01144 CCSID01144 CP01144 ebcdic-it-280+euro }
11597 { IBM01145 CCSID01145 CP01145 ebcdic-es-284+euro }
11598 { IBM01146 CCSID01146 CP01146 ebcdic-gb-285+euro }
11599 { IBM01147 CCSID01147 CP01147 ebcdic-fr-297+euro }
11600 { IBM01148 CCSID01148 CP01148 ebcdic-international-500+euro }
11601 { IBM01149 CCSID01149 CP01149 ebcdic-is-871+euro }
11602 { IBM1047 IBM-1047 }
11603 { PTCP154 csPTCP154 PT154 CP154 Cyrillic-Asian }
11604 { Amiga-1251 Ami1251 Amiga1251 Ami-1251 }
11605 { UNICODE-1-1 csUnicode11 }
11606 { CESU-8 csCESU-8 }
11607 { BOCU-1 csBOCU-1 }
11608 { UNICODE-1-1-UTF-7 csUnicode11UTF7 }
11609 { ISO-8859-14 iso-ir-199 ISO_8859-14:1998 ISO_8859-14 latin8 iso-celtic
11610 l8 }
11611 { ISO-8859-15 ISO_8859-15 Latin-9 }
11612 { ISO-8859-16 iso-ir-226 ISO_8859-16:2001 ISO_8859-16 latin10 l10 }
11613 { GBK CP936 MS936 windows-936 }
11614 { JIS_Encoding csJISEncoding }
09c7029d 11615 { Shift_JIS MS_Kanji csShiftJIS ShiftJIS Shift-JIS }
fd8ccbec
PM
11616 { Extended_UNIX_Code_Packed_Format_for_Japanese csEUCPkdFmtJapanese
11617 EUC-JP }
11618 { Extended_UNIX_Code_Fixed_Width_for_Japanese csEUCFixWidJapanese }
11619 { ISO-10646-UCS-Basic csUnicodeASCII }
11620 { ISO-10646-Unicode-Latin1 csUnicodeLatin1 ISO-10646 }
11621 { ISO-Unicode-IBM-1261 csUnicodeIBM1261 }
11622 { ISO-Unicode-IBM-1268 csUnicodeIBM1268 }
11623 { ISO-Unicode-IBM-1276 csUnicodeIBM1276 }
11624 { ISO-Unicode-IBM-1264 csUnicodeIBM1264 }
11625 { ISO-Unicode-IBM-1265 csUnicodeIBM1265 }
11626 { ISO-8859-1-Windows-3.0-Latin-1 csWindows30Latin1 }
11627 { ISO-8859-1-Windows-3.1-Latin-1 csWindows31Latin1 }
11628 { ISO-8859-2-Windows-Latin-2 csWindows31Latin2 }
11629 { ISO-8859-9-Windows-Latin-5 csWindows31Latin5 }
11630 { Adobe-Standard-Encoding csAdobeStandardEncoding }
11631 { Ventura-US csVenturaUS }
11632 { Ventura-International csVenturaInternational }
11633 { PC8-Danish-Norwegian csPC8DanishNorwegian }
11634 { PC8-Turkish csPC8Turkish }
11635 { IBM-Symbols csIBMSymbols }
11636 { IBM-Thai csIBMThai }
11637 { HP-Legal csHPLegal }
11638 { HP-Pi-font csHPPiFont }
11639 { HP-Math8 csHPMath8 }
11640 { Adobe-Symbol-Encoding csHPPSMath }
11641 { HP-DeskTop csHPDesktop }
11642 { Ventura-Math csVenturaMath }
11643 { Microsoft-Publishing csMicrosoftPublishing }
11644 { Windows-31J csWindows31J }
11645 { GB2312 csGB2312 }
11646 { Big5 csBig5 }
11647}
11648
11649proc tcl_encoding {enc} {
39ee47ef
PM
11650 global encoding_aliases tcl_encoding_cache
11651 if {[info exists tcl_encoding_cache($enc)]} {
11652 return $tcl_encoding_cache($enc)
11653 }
fd8ccbec
PM
11654 set names [encoding names]
11655 set lcnames [string tolower $names]
11656 set enc [string tolower $enc]
11657 set i [lsearch -exact $lcnames $enc]
11658 if {$i < 0} {
11659 # look for "isonnn" instead of "iso-nnn" or "iso_nnn"
09c7029d 11660 if {[regsub {^(iso|cp|ibm|jis)[-_]} $enc {\1} encx]} {
fd8ccbec
PM
11661 set i [lsearch -exact $lcnames $encx]
11662 }
11663 }
11664 if {$i < 0} {
11665 foreach l $encoding_aliases {
11666 set ll [string tolower $l]
11667 if {[lsearch -exact $ll $enc] < 0} continue
11668 # look through the aliases for one that tcl knows about
11669 foreach e $ll {
11670 set i [lsearch -exact $lcnames $e]
11671 if {$i < 0} {
09c7029d 11672 if {[regsub {^(iso|cp|ibm|jis)[-_]} $e {\1} ex]} {
fd8ccbec
PM
11673 set i [lsearch -exact $lcnames $ex]
11674 }
11675 }
11676 if {$i >= 0} break
11677 }
11678 break
11679 }
11680 }
39ee47ef 11681 set tclenc {}
fd8ccbec 11682 if {$i >= 0} {
39ee47ef 11683 set tclenc [lindex $names $i]
fd8ccbec 11684 }
39ee47ef
PM
11685 set tcl_encoding_cache($enc) $tclenc
11686 return $tclenc
fd8ccbec
PM
11687}
11688
09c7029d 11689proc gitattr {path attr default} {
39ee47ef
PM
11690 global path_attr_cache
11691 if {[info exists path_attr_cache($attr,$path)]} {
11692 set r $path_attr_cache($attr,$path)
11693 } else {
11694 set r "unspecified"
11695 if {![catch {set line [exec git check-attr $attr -- $path]}]} {
097e1118 11696 regexp "(.*): $attr: (.*)" $line m f r
09c7029d 11697 }
4db09304 11698 set path_attr_cache($attr,$path) $r
39ee47ef
PM
11699 }
11700 if {$r eq "unspecified"} {
11701 return $default
11702 }
11703 return $r
09c7029d
AG
11704}
11705
4db09304 11706proc cache_gitattr {attr pathlist} {
39ee47ef
PM
11707 global path_attr_cache
11708 set newlist {}
11709 foreach path $pathlist {
11710 if {![info exists path_attr_cache($attr,$path)]} {
11711 lappend newlist $path
11712 }
11713 }
11714 set lim 1000
11715 if {[tk windowingsystem] == "win32"} {
11716 # windows has a 32k limit on the arguments to a command...
11717 set lim 30
11718 }
11719 while {$newlist ne {}} {
11720 set head [lrange $newlist 0 [expr {$lim - 1}]]
11721 set newlist [lrange $newlist $lim end]
11722 if {![catch {set rlist [eval exec git check-attr $attr -- $head]}]} {
11723 foreach row [split $rlist "\n"] {
097e1118 11724 if {[regexp "(.*): $attr: (.*)" $row m path value]} {
39ee47ef
PM
11725 if {[string index $path 0] eq "\""} {
11726 set path [encoding convertfrom [lindex $path 0]]
11727 }
11728 set path_attr_cache($attr,$path) $value
4db09304 11729 }
39ee47ef 11730 }
4db09304 11731 }
39ee47ef 11732 }
4db09304
AG
11733}
11734
09c7029d 11735proc get_path_encoding {path} {
39ee47ef
PM
11736 global gui_encoding perfile_attrs
11737 set tcl_enc $gui_encoding
11738 if {$path ne {} && $perfile_attrs} {
11739 set enc2 [tcl_encoding [gitattr $path encoding $tcl_enc]]
11740 if {$enc2 ne {}} {
11741 set tcl_enc $enc2
09c7029d 11742 }
39ee47ef
PM
11743 }
11744 return $tcl_enc
09c7029d
AG
11745}
11746
5d7589d4
PM
11747# First check that Tcl/Tk is recent enough
11748if {[catch {package require Tk 8.4} err]} {
8d849957
BH
11749 show_error {} . "Sorry, gitk cannot run with this version of Tcl/Tk.\n\
11750 Gitk requires at least Tcl/Tk 8.4." list
5d7589d4
PM
11751 exit 1
11752}
11753
76bf6ff9
TS
11754# on OSX bring the current Wish process window to front
11755if {[tk windowingsystem] eq "aqua"} {
11756 exec osascript -e [format {
11757 tell application "System Events"
11758 set frontmost of processes whose unix id is %d to true
11759 end tell
11760 } [pid] ]
11761}
11762
0ae10357
AO
11763# Unset GIT_TRACE var if set
11764if { [info exists ::env(GIT_TRACE)] } {
11765 unset ::env(GIT_TRACE)
11766}
11767
1d10f36d 11768# defaults...
8974c6f9 11769set wrcomcmd "git diff-tree --stdin -p --pretty"
671bc153 11770
fd8ccbec 11771set gitencoding {}
671bc153 11772catch {
27cb61ca 11773 set gitencoding [exec git config --get i18n.commitencoding]
671bc153 11774}
590915da
AG
11775catch {
11776 set gitencoding [exec git config --get i18n.logoutputencoding]
11777}
671bc153 11778if {$gitencoding == ""} {
fd8ccbec
PM
11779 set gitencoding "utf-8"
11780}
11781set tclencoding [tcl_encoding $gitencoding]
11782if {$tclencoding == {}} {
11783 puts stderr "Warning: encoding $gitencoding is not supported by Tcl/Tk"
671bc153 11784}
1db95b00 11785
09c7029d
AG
11786set gui_encoding [encoding system]
11787catch {
39ee47ef
PM
11788 set enc [exec git config --get gui.encoding]
11789 if {$enc ne {}} {
11790 set tclenc [tcl_encoding $enc]
11791 if {$tclenc ne {}} {
11792 set gui_encoding $tclenc
11793 } else {
11794 puts stderr "Warning: encoding $enc is not supported by Tcl/Tk"
11795 }
11796 }
09c7029d
AG
11797}
11798
b2b76d10
MK
11799set log_showroot true
11800catch {
11801 set log_showroot [exec git config --bool --get log.showroot]
11802}
11803
5fdcbb13
DS
11804if {[tk windowingsystem] eq "aqua"} {
11805 set mainfont {{Lucida Grande} 9}
11806 set textfont {Monaco 9}
11807 set uifont {{Lucida Grande} 9 bold}
5c9096f7
JN
11808} elseif {![catch {::tk::pkgconfig get fontsystem} xft] && $xft eq "xft"} {
11809 # fontconfig!
11810 set mainfont {sans 9}
11811 set textfont {monospace 9}
11812 set uifont {sans 9 bold}
5fdcbb13
DS
11813} else {
11814 set mainfont {Helvetica 9}
11815 set textfont {Courier 9}
11816 set uifont {Helvetica 9 bold}
11817}
7e12f1a6 11818set tabstop 8
b74fd579 11819set findmergefiles 0
8d858d1a 11820set maxgraphpct 50
f6075eba 11821set maxwidth 16
232475d3 11822set revlistorder 0
757f17bc 11823set fastdate 0
6e8c8707
PM
11824set uparrowlen 5
11825set downarrowlen 5
11826set mingaplen 100
f8b28a40 11827set cmitmode "patch"
f1b86294 11828set wrapcomment "none"
b8ab2e17 11829set showneartags 1
ffe15297 11830set hideremotes 0
0a4dd8b8 11831set maxrefs 20
322a8cc9 11832set maxlinelen 200
219ea3a9 11833set showlocalchanges 1
7a39a17a 11834set limitdiffs 1
e8b5f4be 11835set datetimeformat "%Y-%m-%d %H:%M:%S"
95293b58 11836set autoselect 1
21ac8a8d 11837set autosellen 40
39ee47ef 11838set perfile_attrs 0
0cc08ff7 11839set want_ttk 1
1d10f36d 11840
5fdcbb13
DS
11841if {[tk windowingsystem] eq "aqua"} {
11842 set extdifftool "opendiff"
11843} else {
11844 set extdifftool "meld"
11845}
314f5de1 11846
1d10f36d 11847set colors {green red blue magenta darkgrey brown orange}
1924d1bc
PT
11848if {[tk windowingsystem] eq "win32"} {
11849 set uicolor SystemButtonFace
252c52df
11850 set uifgcolor SystemButtonText
11851 set uifgdisabledcolor SystemDisabledText
1924d1bc 11852 set bgcolor SystemWindow
252c52df 11853 set fgcolor SystemWindowText
1924d1bc
PT
11854 set selectbgcolor SystemHighlight
11855} else {
11856 set uicolor grey85
252c52df
11857 set uifgcolor black
11858 set uifgdisabledcolor "#999"
1924d1bc
PT
11859 set bgcolor white
11860 set fgcolor black
11861 set selectbgcolor gray85
11862}
f8a2c0d1 11863set diffcolors {red "#00a000" blue}
890fae70 11864set diffcontext 3
252c52df 11865set mergecolors {red blue green purple brown "#009090" magenta "#808000" "#009000" "#ff0080" cyan "#b07070" "#70b0f0" "#70f0b0" "#f0b070" "#ff70b0"}
b9b86007 11866set ignorespace 0
ae4e3ff9 11867set worddiff ""
e3e901be 11868set markbgcolor "#e0e0ff"
1d10f36d 11869
252c52df
11870set headbgcolor green
11871set headfgcolor black
11872set headoutlinecolor black
11873set remotebgcolor #ffddaa
11874set tagbgcolor yellow
11875set tagfgcolor black
11876set tagoutlinecolor black
11877set reflinecolor black
11878set filesepbgcolor #aaaaaa
11879set filesepfgcolor black
11880set linehoverbgcolor #ffff80
11881set linehoverfgcolor black
11882set linehoveroutlinecolor black
11883set mainheadcirclecolor yellow
11884set workingfilescirclecolor red
11885set indexcirclecolor green
c11ff120 11886set circlecolors {white blue gray blue blue}
252c52df
11887set linkfgcolor blue
11888set circleoutlinecolor $fgcolor
11889set foundbgcolor yellow
11890set currentsearchhitbgcolor orange
c11ff120 11891
d277e89f
PM
11892# button for popping up context menus
11893if {[tk windowingsystem] eq "aqua"} {
11894 set ctxbut <Button-2>
11895} else {
11896 set ctxbut <Button-3>
11897}
11898
663c3aa9
CS
11899## For msgcat loading, first locate the installation location.
11900if { [info exists ::env(GITK_MSGSDIR)] } {
11901 ## Msgsdir was manually set in the environment.
11902 set gitk_msgsdir $::env(GITK_MSGSDIR)
11903} else {
11904 ## Let's guess the prefix from argv0.
11905 set gitk_prefix [file dirname [file dirname [file normalize $argv0]]]
11906 set gitk_libdir [file join $gitk_prefix share gitk lib]
11907 set gitk_msgsdir [file join $gitk_libdir msgs]
11908 unset gitk_prefix
11909}
11910
11911## Internationalization (i18n) through msgcat and gettext. See
11912## http://www.gnu.org/software/gettext/manual/html_node/Tcl.html
11913package require msgcat
11914namespace import ::msgcat::mc
11915## And eventually load the actual message catalog
11916::msgcat::mcload $gitk_msgsdir
11917
1d10f36d
PM
11918catch {source ~/.gitk}
11919
0ed1dd3c
PM
11920parsefont mainfont $mainfont
11921eval font create mainfont [fontflags mainfont]
11922eval font create mainfontbold [fontflags mainfont 1]
11923
11924parsefont textfont $textfont
11925eval font create textfont [fontflags textfont]
11926eval font create textfontbold [fontflags textfont 1]
11927
11928parsefont uifont $uifont
11929eval font create uifont [fontflags uifont]
17386066 11930
51a7e8b6 11931setui $uicolor
5497f7a2 11932
b039f0a6
PM
11933setoptions
11934
cdaee5db 11935# check that we can find a .git directory somewhere...
86e847bc 11936if {[catch {set gitdir [exec git rev-parse --git-dir]}]} {
d990cedf 11937 show_error {} . [mc "Cannot find a git repository here."]
6c87d60c
AR
11938 exit 1
11939}
cdaee5db 11940
39816d60
AG
11941set selecthead {}
11942set selectheadid {}
11943
1d10f36d 11944set revtreeargs {}
cdaee5db
PM
11945set cmdline_files {}
11946set i 0
2d480856 11947set revtreeargscmd {}
1d10f36d 11948foreach arg $argv {
2d480856 11949 switch -glob -- $arg {
6ebedabf 11950 "" { }
cdaee5db
PM
11951 "--" {
11952 set cmdline_files [lrange $argv [expr {$i + 1}] end]
11953 break
11954 }
39816d60
AG
11955 "--select-commit=*" {
11956 set selecthead [string range $arg 16 end]
11957 }
2d480856
YD
11958 "--argscmd=*" {
11959 set revtreeargscmd [string range $arg 10 end]
11960 }
1d10f36d
PM
11961 default {
11962 lappend revtreeargs $arg
11963 }
11964 }
cdaee5db 11965 incr i
1db95b00 11966}
1d10f36d 11967
39816d60
AG
11968if {$selecthead eq "HEAD"} {
11969 set selecthead {}
11970}
11971
cdaee5db 11972if {$i >= [llength $argv] && $revtreeargs ne {}} {
3ed31a81 11973 # no -- on command line, but some arguments (other than --argscmd)
098dd8a3 11974 if {[catch {
8974c6f9 11975 set f [eval exec git rev-parse --no-revs --no-flags $revtreeargs]
098dd8a3
PM
11976 set cmdline_files [split $f "\n"]
11977 set n [llength $cmdline_files]
11978 set revtreeargs [lrange $revtreeargs 0 end-$n]
cdaee5db
PM
11979 # Unfortunately git rev-parse doesn't produce an error when
11980 # something is both a revision and a filename. To be consistent
11981 # with git log and git rev-list, check revtreeargs for filenames.
11982 foreach arg $revtreeargs {
11983 if {[file exists $arg]} {
d990cedf
CS
11984 show_error {} . [mc "Ambiguous argument '%s': both revision\
11985 and filename" $arg]
cdaee5db
PM
11986 exit 1
11987 }
11988 }
098dd8a3
PM
11989 } err]} {
11990 # unfortunately we get both stdout and stderr in $err,
11991 # so look for "fatal:".
11992 set i [string first "fatal:" $err]
11993 if {$i > 0} {
b5e09633 11994 set err [string range $err [expr {$i + 6}] end]
098dd8a3 11995 }
d990cedf 11996 show_error {} . "[mc "Bad arguments to gitk:"]\n$err"
098dd8a3
PM
11997 exit 1
11998 }
11999}
12000
219ea3a9 12001set nullid "0000000000000000000000000000000000000000"
8f489363 12002set nullid2 "0000000000000000000000000000000000000001"
314f5de1 12003set nullfile "/dev/null"
8f489363 12004
32f1b3e4 12005set have_tk85 [expr {[package vcompare $tk_version "8.5"] >= 0}]
0cc08ff7
PM
12006if {![info exists have_ttk]} {
12007 set have_ttk [llength [info commands ::ttk::style]]
d93f1713 12008}
0cc08ff7 12009set use_ttk [expr {$have_ttk && $want_ttk}]
d93f1713 12010set NS [expr {$use_ttk ? "ttk" : ""}]
0cc08ff7 12011
7add5aff 12012regexp {^git version ([\d.]*\d)} [exec git version] _ git_version
219ea3a9 12013
7defefb1
KS
12014set show_notes {}
12015if {[package vcompare $git_version "1.6.6.2"] >= 0} {
12016 set show_notes "--show-notes"
12017}
12018
3878e636
ZJS
12019set appname "gitk"
12020
7eb3cb9c 12021set runq {}
d698206c
PM
12022set history {}
12023set historyindex 0
908c3585 12024set fh_serial 0
908c3585 12025set nhl_names {}
63b79191 12026set highlight_paths {}
687c8765 12027set findpattern {}
1902c270 12028set searchdirn -forwards
28593d3f
PM
12029set boldids {}
12030set boldnameids {}
a8d610a2 12031set diffelide {0 0}
4fb0fa19 12032set markingmatches 0
97645683 12033set linkentercount 0
0380081c
PM
12034set need_redisplay 0
12035set nrows_drawn 0
32f1b3e4 12036set firsttabstop 0
9f1afe05 12037
50b44ece
PM
12038set nextviewnum 1
12039set curview 0
a90a6d24 12040set selectedview 0
b007ee20
CS
12041set selectedhlview [mc "None"]
12042set highlight_related [mc "None"]
687c8765 12043set highlight_files {}
50b44ece 12044set viewfiles(0) {}
a90a6d24 12045set viewperm(0) 0
098dd8a3 12046set viewargs(0) {}
2d480856 12047set viewargscmd(0) {}
50b44ece 12048
94b4a69f 12049set selectedline {}
6df7403a 12050set numcommits 0
7fcc92bf 12051set loginstance 0
098dd8a3 12052set cmdlineok 0
1d10f36d 12053set stopped 0
0fba86b3 12054set stuffsaved 0
74daedb6 12055set patchnum 0
219ea3a9 12056set lserial 0
74cb884f 12057set hasworktree [hasworktree]
c332f445 12058set cdup {}
74cb884f 12059if {[expr {[exec git rev-parse --is-inside-work-tree] == "true"}]} {
c332f445
MZ
12060 set cdup [exec git rev-parse --show-cdup]
12061}
784b7e2f 12062set worktree [exec git rev-parse --show-toplevel]
1d10f36d 12063setcoords
d94f8cd6 12064makewindow
37871b73
GB
12065catch {
12066 image create photo gitlogo -width 16 -height 16
12067
12068 image create photo gitlogominus -width 4 -height 2
12069 gitlogominus put #C00000 -to 0 0 4 2
12070 gitlogo copy gitlogominus -to 1 5
12071 gitlogo copy gitlogominus -to 6 5
12072 gitlogo copy gitlogominus -to 11 5
12073 image delete gitlogominus
12074
12075 image create photo gitlogoplus -width 4 -height 4
12076 gitlogoplus put #008000 -to 1 0 3 4
12077 gitlogoplus put #008000 -to 0 1 4 3
12078 gitlogo copy gitlogoplus -to 1 9
12079 gitlogo copy gitlogoplus -to 6 9
12080 gitlogo copy gitlogoplus -to 11 9
12081 image delete gitlogoplus
12082
d38d7d49
SB
12083 image create photo gitlogo32 -width 32 -height 32
12084 gitlogo32 copy gitlogo -zoom 2 2
12085
12086 wm iconphoto . -default gitlogo gitlogo32
37871b73 12087}
0eafba14
PM
12088# wait for the window to become visible
12089tkwait visibility .
3878e636 12090wm title . "$appname: [reponame]"
478afad6 12091update
887fe3c4 12092readrefs
a8aaf19c 12093
2d480856 12094if {$cmdline_files ne {} || $revtreeargs ne {} || $revtreeargscmd ne {}} {
50b44ece
PM
12095 # create a view for the files/dirs specified on the command line
12096 set curview 1
a90a6d24 12097 set selectedview 1
50b44ece 12098 set nextviewnum 2
d990cedf 12099 set viewname(1) [mc "Command line"]
50b44ece 12100 set viewfiles(1) $cmdline_files
098dd8a3 12101 set viewargs(1) $revtreeargs
2d480856 12102 set viewargscmd(1) $revtreeargscmd
a90a6d24 12103 set viewperm(1) 0
3ed31a81 12104 set vdatemode(1) 0
da7c24dd 12105 addviewmenu 1
f2d0bbbd
PM
12106 .bar.view entryconf [mca "Edit view..."] -state normal
12107 .bar.view entryconf [mca "Delete view"] -state normal
50b44ece 12108}
a90a6d24
PM
12109
12110if {[info exists permviews]} {
12111 foreach v $permviews {
12112 set n $nextviewnum
12113 incr nextviewnum
12114 set viewname($n) [lindex $v 0]
12115 set viewfiles($n) [lindex $v 1]
098dd8a3 12116 set viewargs($n) [lindex $v 2]
2d480856 12117 set viewargscmd($n) [lindex $v 3]
a90a6d24 12118 set viewperm($n) 1
da7c24dd 12119 addviewmenu $n
a90a6d24
PM
12120 }
12121}
e4df519f
JS
12122
12123if {[tk windowingsystem] eq "win32"} {
12124 focus -force .
12125}
12126
567c34e0 12127getcommits {}
adab0dab
PT
12128
12129# Local variables:
12130# mode: tcl
12131# indent-tabs-mode: t
12132# tab-width: 8
12133# End: