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