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