5ea7bfea90d38bb2efe12f0e6fafa56c22c82ce5
1# git-gui blame viewer
2# Copyright (C) 2006, 2007 Shawn Pearce
3
4class blame {
5
6image create photo ::blame::img_back_arrow -data {R0lGODlhGAAYAIUAAPwCBEzKXFTSZIz+nGzmhGzqfGTidIT+nEzGXHTqhGzmfGzifFzadETCVES+VARWDFzWbHzyjAReDGTadFTOZDSyRDyyTCymPARaFGTedFzSbDy2TCyqRCyqPARaDAyCHES6VDy6VCyiPAR6HCSeNByWLARyFARiDARqFGTifARiFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAAAALAAAAAAYABgAAAajQIBwSCwaj8ikcsk0BppJwRPqHEypQwHBis0WDAdEFyBIKBaMAKLBdjQeSkFBYTBAIvgEoS6JmhUTEwIUDQ4VFhcMGEhyCgoZExoUaxsWHB0THkgfAXUGAhoBDSAVFR0XBnCbDRmgog0hpSIiDJpJIyEQhBUcJCIlwA22SSYVogknEg8eD82qSigdDSknY0IqJQXPYxIl1dZCGNvWw+Dm510GQQAh/mhDcmVhdGVkIGJ5IEJNUFRvR0lGIFBybyB2ZXJzaW9uIDIuNQ0KqSBEZXZlbENvciAxOTk3LDE5OTguIEFsbCByaWdodHMgcmVzZXJ2ZWQuDQpodHRwOi8vd3d3LmRldmVsY29yLmNvbQA7}
7
8# Persistant data (survives loads)
9#
10field history {}; # viewer history: {commit path}
11field header ; # array commit,key -> header field
12
13# Tk UI control paths
14#
15field w ; # top window in this viewer
16field w_back ; # our back button
17field w_path ; # label showing the current file path
18field w_columns ; # list of all column widgets in the viewer
19field w_line ; # text column: all line numbers
20field w_amov ; # text column: annotations + move tracking
21field w_asim ; # text column: annotations (simple computation)
22field w_file ; # text column: actual file data
23field w_cviewer ; # pane showing commit message
24field status ; # text variable bound to status bar
25field old_height ; # last known height of $w.file_pane
26
27# Tk UI colors
28#
29variable active_color #c0edc5
30variable group_colors {
31 #d6d6d6
32 #e1e1e1
33 #ececec
34}
35
36# Current blame data; cleared/reset on each load
37#
38field commit ; # input commit to blame
39field path ; # input filename to view in $commit
40
41field current_fd {} ; # background process running
42field highlight_line -1 ; # current line selected
43field highlight_column {} ; # current commit column selected
44field highlight_commit {} ; # sha1 of commit selected
45
46field total_lines 0 ; # total length of file
47field blame_lines 0 ; # number of lines computed
48field amov_data ; # list of {commit origfile origline}
49field asim_data ; # list of {commit origfile origline}
50
51field r_commit ; # commit currently being parsed
52field r_orig_line ; # original line number
53field r_final_line ; # final line number
54field r_line_count ; # lines in this region
55
56field tooltip_wm {} ; # Current tooltip toplevel, if open
57field tooltip_t {} ; # Text widget in $tooltip_wm
58field tooltip_timer {} ; # Current timer event for our tooltip
59field tooltip_commit {} ; # Commit(s) in tooltip
60
61constructor new {i_commit i_path} {
62 global cursor_ptr
63 variable active_color
64 variable group_colors
65
66 set commit $i_commit
67 set path $i_path
68
69 make_toplevel top w
70 wm title $top "[appname] ([reponame]): File Viewer"
71
72 frame $w.header -background orange
73 label $w.header.commit_l \
74 -text {Commit:} \
75 -background orange \
76 -anchor w \
77 -justify left
78 set w_back $w.header.commit_b
79 label $w_back \
80 -image ::blame::img_back_arrow \
81 -borderwidth 0 \
82 -relief flat \
83 -state disabled \
84 -background orange \
85 -activebackground orange
86 bind $w_back <Button-1> "
87 if {\[$w_back cget -state\] eq {normal}} {
88 [cb _history_menu]
89 }
90 "
91 label $w.header.commit \
92 -textvariable @commit \
93 -background orange \
94 -anchor w \
95 -justify left
96 label $w.header.path_l \
97 -text {File:} \
98 -background orange \
99 -anchor w \
100 -justify left
101 set w_path $w.header.path
102 label $w_path \
103 -background orange \
104 -anchor w \
105 -justify left
106 pack $w.header.commit_l -side left
107 pack $w_back -side left
108 pack $w.header.commit -side left
109 pack $w_path -fill x -side right
110 pack $w.header.path_l -side right
111
112 panedwindow $w.file_pane -orient vertical
113 frame $w.file_pane.out
114 frame $w.file_pane.cm
115 $w.file_pane add $w.file_pane.out \
116 -sticky nsew \
117 -minsize 100 \
118 -height 100 \
119 -width 100
120 $w.file_pane add $w.file_pane.cm \
121 -sticky nsew \
122 -minsize 25 \
123 -height 25 \
124 -width 100
125
126 set w_line $w.file_pane.out.linenumber_t
127 text $w_line \
128 -takefocus 0 \
129 -highlightthickness 0 \
130 -padx 0 -pady 0 \
131 -background white -borderwidth 0 \
132 -state disabled \
133 -wrap none \
134 -height 40 \
135 -width 6 \
136 -font font_diff
137 $w_line tag conf linenumber -justify right -rmargin 5
138
139 set w_amov $w.file_pane.out.amove_t
140 text $w_amov \
141 -takefocus 0 \
142 -highlightthickness 0 \
143 -padx 0 -pady 0 \
144 -background white -borderwidth 0 \
145 -state disabled \
146 -wrap none \
147 -height 40 \
148 -width 5 \
149 -font font_diff
150 $w_amov tag conf author_abbr -justify right -rmargin 5
151 $w_amov tag conf curr_commit
152 $w_amov tag conf prior_commit -foreground blue -underline 1
153 $w_amov tag bind prior_commit \
154 <Button-1> \
155 "[cb _load_commit $w_amov @amov_data @%x,%y];break"
156
157 set w_asim $w.file_pane.out.asimple_t
158 text $w_asim \
159 -takefocus 0 \
160 -highlightthickness 0 \
161 -padx 0 -pady 0 \
162 -background white -borderwidth 0 \
163 -state disabled \
164 -wrap none \
165 -height 40 \
166 -width 4 \
167 -font font_diff
168 $w_asim tag conf author_abbr -justify right
169 $w_asim tag conf curr_commit
170 $w_asim tag conf prior_commit -foreground blue -underline 1
171 $w_asim tag bind prior_commit \
172 <Button-1> \
173 "[cb _load_commit $w_asim @asim_data @%x,%y];break"
174
175 set w_file $w.file_pane.out.file_t
176 text $w_file \
177 -takefocus 0 \
178 -highlightthickness 0 \
179 -padx 0 -pady 0 \
180 -background white -borderwidth 0 \
181 -state disabled \
182 -wrap none \
183 -height 40 \
184 -width 80 \
185 -xscrollcommand [list $w.file_pane.out.sbx set] \
186 -font font_diff
187
188 set w_columns [list $w_amov $w_asim $w_line $w_file]
189
190 scrollbar $w.file_pane.out.sbx \
191 -orient h \
192 -command [list $w_file xview]
193 scrollbar $w.file_pane.out.sby \
194 -orient v \
195 -command [list scrollbar2many $w_columns yview]
196 eval grid $w_columns $w.file_pane.out.sby -sticky nsew
197 grid conf \
198 $w.file_pane.out.sbx \
199 -column [expr {[llength $w_columns] - 1}] \
200 -sticky we
201 grid columnconfigure \
202 $w.file_pane.out \
203 [expr {[llength $w_columns] - 1}] \
204 -weight 1
205 grid rowconfigure $w.file_pane.out 0 -weight 1
206
207 set w_cviewer $w.file_pane.cm.t
208 text $w_cviewer \
209 -background white -borderwidth 0 \
210 -state disabled \
211 -wrap none \
212 -height 10 \
213 -width 80 \
214 -xscrollcommand [list $w.file_pane.cm.sbx set] \
215 -yscrollcommand [list $w.file_pane.cm.sby set] \
216 -font font_diff
217 $w_cviewer tag conf still_loading \
218 -font font_uiitalic \
219 -justify center
220 $w_cviewer tag conf header_key \
221 -tabs {3c} \
222 -background $active_color \
223 -font font_uibold
224 $w_cviewer tag conf header_val \
225 -background $active_color \
226 -font font_ui
227 $w_cviewer tag raise sel
228 scrollbar $w.file_pane.cm.sbx \
229 -orient h \
230 -command [list $w_cviewer xview]
231 scrollbar $w.file_pane.cm.sby \
232 -orient v \
233 -command [list $w_cviewer yview]
234 pack $w.file_pane.cm.sby -side right -fill y
235 pack $w.file_pane.cm.sbx -side bottom -fill x
236 pack $w_cviewer -expand 1 -fill both
237
238 frame $w.status \
239 -borderwidth 1 \
240 -relief sunken
241 label $w.status.l \
242 -textvariable @status \
243 -anchor w \
244 -justify left
245 pack $w.status.l -side left
246
247 menu $w.ctxm -tearoff 0
248 $w.ctxm add command \
249 -label "Copy Commit" \
250 -command [cb _copycommit]
251
252 foreach i $w_columns {
253 for {set g 0} {$g < [llength $group_colors]} {incr g} {
254 $i tag conf color$g -background [lindex $group_colors $g]
255 }
256
257 $i conf -cursor $cursor_ptr
258 $i conf -yscrollcommand [list many2scrollbar \
259 $w_columns yview $w.file_pane.out.sby]
260 bind $i <Button-1> "
261 [cb _hide_tooltip]
262 [cb _click $i @%x,%y]
263 focus $i
264 "
265 bind $i <Any-Motion> [cb _show_tooltip $i @%x,%y]
266 bind $i <Any-Enter> [cb _hide_tooltip]
267 bind $i <Any-Leave> [cb _hide_tooltip]
268 bind_button3 $i "
269 [cb _hide_tooltip]
270 set cursorX %x
271 set cursorY %y
272 set cursorW %W
273 tk_popup $w.ctxm %X %Y
274 "
275 }
276
277 foreach i [concat $w_columns $w_cviewer] {
278 bind $i <Key-Up> {catch {%W yview scroll -1 units};break}
279 bind $i <Key-Down> {catch {%W yview scroll 1 units};break}
280 bind $i <Key-Left> {catch {%W xview scroll -1 units};break}
281 bind $i <Key-Right> {catch {%W xview scroll 1 units};break}
282 bind $i <Key-k> {catch {%W yview scroll -1 units};break}
283 bind $i <Key-j> {catch {%W yview scroll 1 units};break}
284 bind $i <Key-h> {catch {%W xview scroll -1 units};break}
285 bind $i <Key-l> {catch {%W xview scroll 1 units};break}
286 bind $i <Control-Key-b> {catch {%W yview scroll -1 pages};break}
287 bind $i <Control-Key-f> {catch {%W yview scroll 1 pages};break}
288 }
289
290 bind $w_cviewer <Button-1> [list focus $w_cviewer]
291 bind $top <Visibility> [list focus $top]
292 bind $w_file <Destroy> [list delete_this $this]
293
294 grid configure $w.header -sticky ew
295 grid configure $w.file_pane -sticky nsew
296 grid configure $w.status -sticky ew
297 grid columnconfigure $top 0 -weight 1
298 grid rowconfigure $top 0 -weight 0
299 grid rowconfigure $top 1 -weight 1
300 grid rowconfigure $top 2 -weight 0
301
302 set req_w [winfo reqwidth $top]
303 set req_h [winfo reqheight $top]
304 if {$req_w < 600} {set req_w 600}
305 if {$req_h < 400} {set req_h 400}
306 set g "${req_w}x${req_h}"
307 wm geometry $top $g
308 update
309
310 set old_height [winfo height $w.file_pane]
311 $w.file_pane sash place 0 \
312 [lindex [$w.file_pane sash coord 0] 0] \
313 [expr {int($old_height * 0.70)}]
314 bind $w.file_pane <Configure> \
315 "if {{$w.file_pane} eq {%W}} {[cb _resize %h]}"
316
317 _load $this {}
318}
319
320method _load {jump} {
321 variable group_colors
322
323 _hide_tooltip $this
324
325 if {$total_lines != 0 || $current_fd ne {}} {
326 if {$current_fd ne {}} {
327 catch {close $current_fd}
328 set current_fd {}
329 }
330
331 foreach i $w_columns {
332 $i conf -state normal
333 $i delete 0.0 end
334 foreach g [$i tag names] {
335 if {[regexp {^g[0-9a-f]{40}$} $g]} {
336 $i tag delete $g
337 }
338 }
339 $i conf -state disabled
340 }
341
342 $w_cviewer conf -state normal
343 $w_cviewer delete 0.0 end
344 $w_cviewer conf -state disabled
345
346 set highlight_line -1
347 set highlight_column {}
348 set highlight_commit {}
349 set total_lines 0
350 }
351
352 if {[winfo exists $w.status.c]} {
353 $w.status.c coords bar 0 0 0 20
354 } else {
355 canvas $w.status.c \
356 -width 100 \
357 -height [expr {int([winfo reqheight $w.status.l] * 0.6)}] \
358 -borderwidth 1 \
359 -relief groove \
360 -highlightt 0
361 $w.status.c create rectangle 0 0 0 20 -tags bar -fill navy
362 pack $w.status.c -side right
363 }
364
365 if {$history eq {}} {
366 $w_back conf -state disabled
367 } else {
368 $w_back conf -state normal
369 }
370
371 # Index 0 is always empty. There is never line 0 as
372 # we use only 1 based lines, as that matches both with
373 # git-blame output and with Tk's text widget.
374 #
375 set amov_data [list [list]]
376 set asim_data [list [list]]
377
378 set status "Loading $commit:[escape_path $path]..."
379 $w_path conf -text [escape_path $path]
380 if {$commit eq {}} {
381 set fd [open $path r]
382 } else {
383 set cmd [list git cat-file blob "$commit:$path"]
384 set fd [open "| $cmd" r]
385 }
386 fconfigure $fd -blocking 0 -translation lf -encoding binary
387 fileevent $fd readable [cb _read_file $fd $jump]
388 set current_fd $fd
389}
390
391method _history_menu {} {
392 set m $w.backmenu
393 if {[winfo exists $m]} {
394 $m delete 0 end
395 } else {
396 menu $m -tearoff 0
397 }
398
399 for {set i [expr {[llength $history] - 1}]
400 } {$i >= 0} {incr i -1} {
401 set e [lindex $history $i]
402 set c [lindex $e 0]
403 set f [lindex $e 1]
404
405 if {[regexp {^[0-9a-f]{40}$} $c]} {
406 set t [string range $c 0 8]...
407 } elseif {$c eq {}} {
408 set t {Working Directory}
409 } else {
410 set t $c
411 }
412 if {![catch {set summary $header($c,summary)}]} {
413 append t " $summary"
414 if {[string length $t] > 70} {
415 set t [string range $t 0 66]...
416 }
417 }
418
419 $m add command -label $t -command [cb _goback $i]
420 }
421 set X [winfo rootx $w_back]
422 set Y [expr {[winfo rooty $w_back] + [winfo height $w_back]}]
423 tk_popup $m $X $Y
424}
425
426method _goback {i} {
427 set dat [lindex $history $i]
428 set history [lrange $history 0 [expr {$i - 1}]]
429 set commit [lindex $dat 0]
430 set path [lindex $dat 1]
431 _load $this [lrange $dat 2 5]
432}
433
434method _read_file {fd jump} {
435 if {$fd ne $current_fd} {
436 catch {close $fd}
437 return
438 }
439
440 foreach i $w_columns {$i conf -state normal}
441 while {[gets $fd line] >= 0} {
442 regsub "\r\$" $line {} line
443 incr total_lines
444 lappend amov_data {}
445 lappend asim_data {}
446
447 if {$total_lines > 1} {
448 foreach i $w_columns {$i insert end "\n"}
449 }
450
451 $w_line insert end "$total_lines" linenumber
452 $w_file insert end "$line"
453 }
454
455 set ln_wc [expr {[string length $total_lines] + 2}]
456 if {[$w_line cget -width] < $ln_wc} {
457 $w_line conf -width $ln_wc
458 }
459
460 foreach i $w_columns {$i conf -state disabled}
461
462 if {[eof $fd]} {
463 close $fd
464
465 # If we don't force Tk to update the widgets *right now*
466 # none of our jump commands will cause a change in the UI.
467 #
468 update
469
470 if {[llength $jump] == 1} {
471 set highlight_line [lindex $jump 0]
472 $w_file see "$highlight_line.0"
473 } elseif {[llength $jump] == 4} {
474 set highlight_column [lindex $jump 0]
475 set highlight_line [lindex $jump 1]
476 $w_file xview moveto [lindex $jump 2]
477 $w_file yview moveto [lindex $jump 3]
478 }
479
480 _exec_blame $this $w_asim @asim_data [list] {}
481 }
482} ifdeleted { catch {close $fd} }
483
484method _exec_blame {cur_w cur_d options cur_s} {
485 set cmd [list nice git blame]
486 set cmd [concat $cmd $options]
487 lappend cmd --incremental
488 if {$commit eq {}} {
489 lappend cmd --contents $path
490 } else {
491 lappend cmd $commit
492 }
493 lappend cmd -- $path
494 set fd [open "| $cmd" r]
495 fconfigure $fd -blocking 0 -translation lf -encoding binary
496 fileevent $fd readable [cb _read_blame $fd $cur_w $cur_d $cur_s]
497 set current_fd $fd
498 set blame_lines 0
499 _status $this $cur_s
500}
501
502method _read_blame {fd cur_w cur_d cur_s} {
503 upvar #0 $cur_d line_data
504 variable group_colors
505
506 if {$fd ne $current_fd} {
507 catch {close $fd}
508 return
509 }
510
511 $cur_w conf -state normal
512 while {[gets $fd line] >= 0} {
513 if {[regexp {^([a-z0-9]{40}) (\d+) (\d+) (\d+)$} $line line \
514 cmit original_line final_line line_count]} {
515 set r_commit $cmit
516 set r_orig_line $original_line
517 set r_final_line $final_line
518 set r_line_count $line_count
519 } elseif {[string match {filename *} $line]} {
520 set file [string range $line 9 end]
521 set n $r_line_count
522 set lno $r_final_line
523 set oln $r_orig_line
524 set cmit $r_commit
525
526 if {[regexp {^0{40}$} $cmit]} {
527 set commit_abbr work
528 set commit_type curr_commit
529 } elseif {$cmit eq $commit} {
530 set commit_abbr this
531 set commit_type curr_commit
532 } else {
533 set commit_type prior_commit
534 set commit_abbr [string range $cmit 0 3]
535 }
536
537 set author_abbr {}
538 set a_name {}
539 catch {set a_name $header($cmit,author)}
540 while {$a_name ne {}} {
541 if {![regexp {^([[:upper:]])} $a_name _a]} break
542 append author_abbr $_a
543 unset _a
544 if {![regsub \
545 {^[[:upper:]][^\s]*\s+} \
546 $a_name {} a_name ]} break
547 }
548 if {$author_abbr eq {}} {
549 set author_abbr { |}
550 } else {
551 set author_abbr [string range $author_abbr 0 3]
552 }
553 unset a_name
554
555 set first_lno $lno
556 while {
557 $first_lno > 1
558 && $cmit eq [lindex $line_data [expr {$first_lno - 1}] 0]
559 && $file eq [lindex $line_data [expr {$first_lno - 1}] 1]
560 } {
561 incr first_lno -1
562 }
563
564 set color {}
565 if {$first_lno < $lno} {
566 foreach g [$w_file tag names $first_lno.0] {
567 if {[regexp {^color[0-9]+$} $g]} {
568 set color $g
569 break
570 }
571 }
572 } else {
573 set i [lsort [concat \
574 [$w_file tag names "[expr {$first_lno - 1}].0"] \
575 [$w_file tag names "[expr {$lno + $n}].0"] \
576 ]]
577 for {set g 0} {$g < [llength $group_colors]} {incr g} {
578 if {[lsearch -sorted -exact $i color$g] == -1} {
579 set color color$g
580 break
581 }
582 }
583 }
584 if {$color eq {}} {
585 set color color0
586 }
587
588 while {$n > 0} {
589 set lno_e "$lno.0 lineend + 1c"
590 if {[lindex $line_data $lno] ne {}} {
591 set g [lindex $line_data $lno 0]
592 foreach i $w_columns {
593 $i tag remove g$g $lno.0 $lno_e
594 }
595 }
596 lset line_data $lno [list $cmit $file $oln]
597
598 $cur_w delete $lno.0 "$lno.0 lineend"
599 if {$lno == $first_lno} {
600 $cur_w insert $lno.0 $commit_abbr $commit_type
601 } elseif {$lno == [expr {$first_lno + 1}]} {
602 $cur_w insert $lno.0 $author_abbr author_abbr
603 } else {
604 $cur_w insert $lno.0 { |}
605 }
606
607 foreach i $w_columns {
608 if {$cur_w eq $w_amov} {
609 for {set g 0} \
610 {$g < [llength $group_colors]} \
611 {incr g} {
612 $i tag remove color$g $lno.0 $lno_e
613 }
614 $i tag add $color $lno.0 $lno_e
615 }
616 $i tag add g$cmit $lno.0 $lno_e
617 }
618
619 if {$highlight_column eq $cur_w} {
620 if {$highlight_line == -1
621 && [lindex [$w_file yview] 0] == 0} {
622 $w_file see $lno.0
623 set highlight_line $lno
624 }
625 if {$highlight_line == $lno} {
626 _showcommit $this $cur_w $lno
627 }
628 }
629
630 incr n -1
631 incr lno
632 incr oln
633 incr blame_lines
634 }
635
636 while {
637 $cmit eq [lindex $line_data $lno 0]
638 && $file eq [lindex $line_data $lno 1]
639 } {
640 $cur_w delete $lno.0 "$lno.0 lineend"
641
642 if {$lno == $first_lno} {
643 $cur_w insert $lno.0 $commit_abbr $commit_type
644 } elseif {$lno == [expr {$first_lno + 1}]} {
645 $cur_w insert $lno.0 $author_abbr author_abbr
646 } else {
647 $cur_w insert $lno.0 { |}
648 }
649
650 if {$cur_w eq $w_amov} {
651 foreach i $w_columns {
652 for {set g 0} \
653 {$g < [llength $group_colors]} \
654 {incr g} {
655 $i tag remove color$g $lno.0 $lno_e
656 }
657 $i tag add $color $lno.0 $lno_e
658 }
659 }
660
661 incr lno
662 }
663
664 } elseif {[regexp {^([a-z-]+) (.*)$} $line line key data]} {
665 set header($r_commit,$key) $data
666 }
667 }
668 $cur_w conf -state disabled
669
670 if {[eof $fd]} {
671 close $fd
672 if {$cur_w eq $w_asim} {
673 _exec_blame $this $w_amov @amov_data \
674 [list -M -C -C] \
675 { move/copy tracking}
676 } else {
677 set current_fd {}
678 set status {Annotation complete.}
679 destroy $w.status.c
680 }
681 } else {
682 _status $this $cur_s
683 }
684} ifdeleted { catch {close $fd} }
685
686method _status {cur_s} {
687 set have $blame_lines
688 set total $total_lines
689 set pdone 0
690 if {$total} {set pdone [expr {100 * $have / $total}]}
691
692 set status [format \
693 "Loading%s annotations... %i of %i lines annotated (%2i%%)" \
694 $cur_s $have $total $pdone]
695 $w.status.c coords bar 0 0 $pdone 20
696}
697
698method _click {cur_w pos} {
699 set lno [lindex [split [$cur_w index $pos] .] 0]
700 _showcommit $this $cur_w $lno
701}
702
703method _load_commit {cur_w cur_d pos} {
704 upvar #0 $cur_d line_data
705 set lno [lindex [split [$cur_w index $pos] .] 0]
706 set dat [lindex $line_data $lno]
707 if {$dat ne {}} {
708 lappend history [list \
709 $commit $path \
710 $highlight_column \
711 $highlight_line \
712 [lindex [$w_file xview] 0] \
713 [lindex [$w_file yview] 0] \
714 ]
715 set commit [lindex $dat 0]
716 set path [lindex $dat 1]
717 _load $this [list [lindex $dat 2]]
718 }
719}
720
721method _showcommit {cur_w lno} {
722 global repo_config
723 variable active_color
724
725 if {$highlight_commit ne {}} {
726 foreach i $w_columns {
727 $i tag conf g$highlight_commit -background {}
728 $i tag lower g$highlight_commit
729 }
730 }
731
732 if {$cur_w eq $w_amov} {
733 set dat [lindex $amov_data $lno]
734 set highlight_column $w_amov
735 } else {
736 set dat [lindex $asim_data $lno]
737 set highlight_column $w_asim
738 }
739
740 $w_cviewer conf -state normal
741 $w_cviewer delete 0.0 end
742
743 if {$dat eq {}} {
744 set cmit {}
745 $w_cviewer insert end "Loading annotation..." still_loading
746 } else {
747 set cmit [lindex $dat 0]
748 set file [lindex $dat 1]
749
750 foreach i $w_columns {
751 $i tag conf g$cmit -background $active_color
752 $i tag raise g$cmit
753 }
754
755 set author_name {}
756 set author_email {}
757 set author_time {}
758 catch {set author_name $header($cmit,author)}
759 catch {set author_email $header($cmit,author-mail)}
760 catch {set author_time [clock format \
761 $header($cmit,author-time) \
762 -format {%Y-%m-%d %H:%M:%S}
763 ]}
764
765 set committer_name {}
766 set committer_email {}
767 set committer_time {}
768 catch {set committer_name $header($cmit,committer)}
769 catch {set committer_email $header($cmit,committer-mail)}
770 catch {set committer_time [clock format \
771 $header($cmit,committer-time) \
772 -format {%Y-%m-%d %H:%M:%S}
773 ]}
774
775 if {[catch {set msg $header($cmit,message)}]} {
776 set msg {}
777 catch {
778 set fd [open "| git cat-file commit $cmit" r]
779 fconfigure $fd -encoding binary -translation lf
780 if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
781 set enc utf-8
782 }
783 while {[gets $fd line] > 0} {
784 if {[string match {encoding *} $line]} {
785 set enc [string tolower [string range $line 9 end]]
786 }
787 }
788 set msg [encoding convertfrom $enc [read $fd]]
789 set msg [string trim $msg]
790 close $fd
791
792 set author_name [encoding convertfrom $enc $author_name]
793 set committer_name [encoding convertfrom $enc $committer_name]
794
795 set header($cmit,author) $author_name
796 set header($cmit,committer) $committer_name
797 }
798 set header($cmit,message) $msg
799 }
800
801 $w_cviewer insert end "commit $cmit\n" header_key
802 $w_cviewer insert end "Author:\t" header_key
803 $w_cviewer insert end "$author_name $author_email" header_val
804 $w_cviewer insert end " $author_time\n" header_val
805
806 $w_cviewer insert end "Committer:\t" header_key
807 $w_cviewer insert end "$committer_name $committer_email" header_val
808 $w_cviewer insert end " $committer_time\n" header_val
809
810 if {$file ne $path} {
811 $w_cviewer insert end "Original File:\t" header_key
812 $w_cviewer insert end "[escape_path $file]\n" header_val
813 }
814
815 $w_cviewer insert end "\n$msg"
816 }
817 $w_cviewer conf -state disabled
818
819 set highlight_line $lno
820 set highlight_commit $cmit
821
822 if {[lsearch -exact $tooltip_commit $highlight_commit] != -1} {
823 _hide_tooltip $this
824 }
825}
826
827method _copycommit {} {
828 set pos @$::cursorX,$::cursorY
829 set lno [lindex [split [$::cursorW index $pos] .] 0]
830 set dat [lindex $amov_data $lno]
831 if {$dat ne {}} {
832 clipboard clear
833 clipboard append \
834 -format STRING \
835 -type STRING \
836 -- [lindex $dat 0]
837 }
838}
839
840method _show_tooltip {cur_w pos} {
841 if {$tooltip_wm ne {}} {
842 _open_tooltip $this $cur_w
843 } elseif {$tooltip_timer eq {}} {
844 set tooltip_timer [after 1000 [cb _open_tooltip $cur_w]]
845 }
846}
847
848method _open_tooltip {cur_w} {
849 set tooltip_timer {}
850 set pos_x [winfo pointerx $cur_w]
851 set pos_y [winfo pointery $cur_w]
852 if {[winfo containing $pos_x $pos_y] ne $cur_w} {
853 _hide_tooltip $this
854 return
855 }
856
857 if {$tooltip_wm ne "$cur_w.tooltip"} {
858 _hide_tooltip $this
859
860 set tooltip_wm [toplevel $cur_w.tooltip -borderwidth 1]
861 wm overrideredirect $tooltip_wm 1
862 wm transient $tooltip_wm [winfo toplevel $cur_w]
863 set tooltip_t $tooltip_wm.label
864 text $tooltip_t \
865 -takefocus 0 \
866 -highlightthickness 0 \
867 -relief flat \
868 -borderwidth 0 \
869 -wrap none \
870 -background lightyellow \
871 -foreground black
872 $tooltip_t tag conf section_header -font font_uibold
873 pack $tooltip_t
874 } else {
875 $tooltip_t conf -state normal
876 $tooltip_t delete 0.0 end
877 }
878
879 set pos @[join [list \
880 [expr {$pos_x - [winfo rootx $cur_w]}] \
881 [expr {$pos_y - [winfo rooty $cur_w]}]] ,]
882 set lno [lindex [split [$cur_w index $pos] .] 0]
883 if {$cur_w eq $w_amov} {
884 set dat [lindex $amov_data $lno]
885 set org {}
886 } else {
887 set dat [lindex $asim_data $lno]
888 set org [lindex $amov_data $lno]
889 }
890
891 set cmit [lindex $dat 0]
892 set tooltip_commit [list $cmit]
893
894 set author_name {}
895 set summary {}
896 set author_time {}
897 catch {set author_name $header($cmit,author)}
898 catch {set summary $header($cmit,summary)}
899 catch {set author_time [clock format \
900 $header($cmit,author-time) \
901 -format {%Y-%m-%d %H:%M:%S}
902 ]}
903
904 $tooltip_t insert end "commit $cmit\n"
905 $tooltip_t insert end "$author_name $author_time\n"
906 $tooltip_t insert end "$summary"
907
908 if {$org ne {} && [lindex $org 0] ne $cmit} {
909 $tooltip_t insert 0.0 "Moved Here By:\n" section_header
910 set cmit [lindex $org 0]
911 set file [lindex $org 1]
912 lappend tooltip_commit $cmit
913
914 set author_name {}
915 set summary {}
916 set author_time {}
917 catch {set author_name $header($cmit,author)}
918 catch {set summary $header($cmit,summary)}
919 catch {set author_time [clock format \
920 $header($cmit,author-time) \
921 -format {%Y-%m-%d %H:%M:%S}
922 ]}
923
924 $tooltip_t insert end "\n\n"
925 $tooltip_t insert end "Originally By:\n" section_header
926 $tooltip_t insert end "commit $cmit\n"
927 $tooltip_t insert end "$author_name $author_time\n"
928 $tooltip_t insert end "$summary"
929
930 if {$file ne $path} {
931 $tooltip_t insert end "\n"
932 $tooltip_t insert end "File: " section_header
933 $tooltip_t insert end $file
934 }
935 }
936
937 $tooltip_t conf -state disabled
938 _position_tooltip $this
939}
940
941method _position_tooltip {} {
942 set max_h [lindex [split [$tooltip_t index end] .] 0]
943 set max_w 0
944 for {set i 1} {$i <= $max_h} {incr i} {
945 set c [lindex [split [$tooltip_t index "$i.0 lineend"] .] 1]
946 if {$c > $max_w} {set max_w $c}
947 }
948 $tooltip_t conf -width $max_w -height $max_h
949
950 set req_w [winfo reqwidth $tooltip_t]
951 set req_h [winfo reqheight $tooltip_t]
952 set pos_x [expr {[winfo pointerx .] + 5}]
953 set pos_y [expr {[winfo pointery .] + 10}]
954
955 set g "${req_w}x${req_h}"
956 if {$pos_x >= 0} {append g +}
957 append g $pos_x
958 if {$pos_y >= 0} {append g +}
959 append g $pos_y
960
961 wm geometry $tooltip_wm $g
962 raise $tooltip_wm
963}
964
965method _hide_tooltip {} {
966 if {$tooltip_wm ne {}} {
967 destroy $tooltip_wm
968 set tooltip_wm {}
969 set tooltip_commit {}
970 }
971 if {$tooltip_timer ne {}} {
972 after cancel $tooltip_timer
973 set tooltip_timer {}
974 }
975}
976
977method _resize {new_height} {
978 set diff [expr {$new_height - $old_height}]
979 if {$diff == 0} return
980
981 set my [expr {[winfo height $w.file_pane] - 25}]
982 set o [$w.file_pane sash coord 0]
983 set ox [lindex $o 0]
984 set oy [expr {[lindex $o 1] + $diff}]
985 if {$oy < 0} {set oy 0}
986 if {$oy > $my} {set oy $my}
987 $w.file_pane sash place 0 $ox $oy
988
989 set old_height $new_height
990}
991
992}