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