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