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