2c190482df10dafb9bcef8116ea42c931d553efb
   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        wm protocol $top WM_DELETE_WINDOW "destroy $top"
 330        bind $top <Destroy> [cb _kill]
 331
 332        _load $this {}
 333}
 334
 335method _kill {} {
 336        if {$current_fd ne {}} {
 337                kill_file_process $current_fd
 338                catch {close $current_fd}
 339                set current_fd {}
 340        }
 341}
 342
 343method _load {jump} {
 344        variable group_colors
 345
 346        _hide_tooltip $this
 347
 348        if {$total_lines != 0 || $current_fd ne {}} {
 349                _kill $this
 350
 351                foreach i $w_columns {
 352                        $i conf -state normal
 353                        $i delete 0.0 end
 354                        foreach g [$i tag names] {
 355                                if {[regexp {^g[0-9a-f]{40}$} $g]} {
 356                                        $i tag delete $g
 357                                }
 358                        }
 359                        $i conf -state disabled
 360                }
 361
 362                $w_cviewer conf -state normal
 363                $w_cviewer delete 0.0 end
 364                $w_cviewer conf -state disabled
 365
 366                set highlight_line -1
 367                set highlight_column {}
 368                set highlight_commit {}
 369                set total_lines 0
 370        }
 371
 372        if {$history eq {}} {
 373                $w_back conf -state disabled
 374        } else {
 375                $w_back conf -state normal
 376        }
 377
 378        # Index 0 is always empty.  There is never line 0 as
 379        # we use only 1 based lines, as that matches both with
 380        # git-blame output and with Tk's text widget.
 381        #
 382        set amov_data [list [list]]
 383        set asim_data [list [list]]
 384
 385        $status show [mc "Reading %s..." "$commit:[escape_path $path]"]
 386        $w_path conf -text [escape_path $path]
 387        if {$commit eq {}} {
 388                set fd [open $path r]
 389                fconfigure $fd -eofchar {}
 390        } else {
 391                set fd [git_read cat-file blob "$commit:$path"]
 392        }
 393        fconfigure $fd -blocking 0 -translation lf -encoding binary
 394        fileevent $fd readable [cb _read_file $fd $jump]
 395        set current_fd $fd
 396}
 397
 398method _history_menu {} {
 399        set m $w.backmenu
 400        if {[winfo exists $m]} {
 401                $m delete 0 end
 402        } else {
 403                menu $m -tearoff 0
 404        }
 405
 406        for {set i [expr {[llength $history] - 1}]
 407                } {$i >= 0} {incr i -1} {
 408                set e [lindex $history $i]
 409                set c [lindex $e 0]
 410                set f [lindex $e 1]
 411
 412                if {[regexp {^[0-9a-f]{40}$} $c]} {
 413                        set t [string range $c 0 8]...
 414                } elseif {$c eq {}} {
 415                        set t {Working Directory}
 416                } else {
 417                        set t $c
 418                }
 419                if {![catch {set summary $header($c,summary)}]} {
 420                        append t " $summary"
 421                        if {[string length $t] > 70} {
 422                                set t [string range $t 0 66]...
 423                        }
 424                }
 425
 426                $m add command -label $t -command [cb _goback $i]
 427        }
 428        set X [winfo rootx $w_back]
 429        set Y [expr {[winfo rooty $w_back] + [winfo height $w_back]}]
 430        tk_popup $m $X $Y
 431}
 432
 433method _goback {i} {
 434        set dat [lindex $history $i]
 435        set history [lrange $history 0 [expr {$i - 1}]]
 436        set commit [lindex $dat 0]
 437        set path [lindex $dat 1]
 438        _load $this [lrange $dat 2 5]
 439}
 440
 441method _read_file {fd jump} {
 442        if {$fd ne $current_fd} {
 443                catch {close $fd}
 444                return
 445        }
 446
 447        foreach i $w_columns {$i conf -state normal}
 448        while {[gets $fd line] >= 0} {
 449                regsub "\r\$" $line {} line
 450                incr total_lines
 451                lappend amov_data {}
 452                lappend asim_data {}
 453
 454                if {$total_lines > 1} {
 455                        foreach i $w_columns {$i insert end "\n"}
 456                }
 457
 458                $w_line insert end "$total_lines" linenumber
 459                $w_file insert end "$line"
 460        }
 461
 462        set ln_wc [expr {[string length $total_lines] + 2}]
 463        if {[$w_line cget -width] < $ln_wc} {
 464                $w_line conf -width $ln_wc
 465        }
 466
 467        foreach i $w_columns {$i conf -state disabled}
 468
 469        if {[eof $fd]} {
 470                close $fd
 471
 472                # If we don't force Tk to update the widgets *right now*
 473                # none of our jump commands will cause a change in the UI.
 474                #
 475                update
 476
 477                if {[llength $jump] == 1} {
 478                        set highlight_line [lindex $jump 0]
 479                        $w_file see "$highlight_line.0"
 480                } elseif {[llength $jump] == 4} {
 481                        set highlight_column [lindex $jump 0]
 482                        set highlight_line [lindex $jump 1]
 483                        $w_file xview moveto [lindex $jump 2]
 484                        $w_file yview moveto [lindex $jump 3]
 485                }
 486
 487                _exec_blame $this $w_asim @asim_data \
 488                        [list] \
 489                        [mc "Loading copy/move tracking annotations..."]
 490        }
 491} ifdeleted { catch {close $fd} }
 492
 493method _exec_blame {cur_w cur_d options cur_s} {
 494        lappend options --incremental
 495        if {$commit eq {}} {
 496                lappend options --contents $path
 497        } else {
 498                lappend options $commit
 499        }
 500        lappend options -- $path
 501        set fd [eval git_read --nice blame $options]
 502        fconfigure $fd -blocking 0 -translation lf -encoding binary
 503        fileevent $fd readable [cb _read_blame $fd $cur_w $cur_d]
 504        set current_fd $fd
 505        set blame_lines 0
 506
 507        $status start \
 508                $cur_s \
 509                [mc "lines annotated"]
 510}
 511
 512method _read_blame {fd cur_w cur_d} {
 513        upvar #0 $cur_d line_data
 514        variable group_colors
 515
 516        if {$fd ne $current_fd} {
 517                catch {close $fd}
 518                return
 519        }
 520
 521        $cur_w conf -state normal
 522        while {[gets $fd line] >= 0} {
 523                if {[regexp {^([a-z0-9]{40}) (\d+) (\d+) (\d+)$} $line line \
 524                        cmit original_line final_line line_count]} {
 525                        set r_commit     $cmit
 526                        set r_orig_line  $original_line
 527                        set r_final_line $final_line
 528                        set r_line_count $line_count
 529                } elseif {[string match {filename *} $line]} {
 530                        set file [string range $line 9 end]
 531                        set n    $r_line_count
 532                        set lno  $r_final_line
 533                        set oln  $r_orig_line
 534                        set cmit $r_commit
 535
 536                        if {[regexp {^0{40}$} $cmit]} {
 537                                set commit_abbr work
 538                                set commit_type curr_commit
 539                        } elseif {$cmit eq $commit} {
 540                                set commit_abbr this
 541                                set commit_type curr_commit
 542                        } else {
 543                                set commit_type prior_commit
 544                                set commit_abbr [string range $cmit 0 3]
 545                        }
 546
 547                        set author_abbr {}
 548                        set a_name {}
 549                        catch {set a_name $header($cmit,author)}
 550                        while {$a_name ne {}} {
 551                                if {$author_abbr ne {}
 552                                        && [string index $a_name 0] eq {'}} {
 553                                        regsub {^'[^']+'\s+} $a_name {} a_name
 554                                }
 555                                if {![regexp {^([[:upper:]])} $a_name _a]} break
 556                                append author_abbr $_a
 557                                unset _a
 558                                if {![regsub \
 559                                        {^[[:upper:]][^\s]*\s+} \
 560                                        $a_name {} a_name ]} break
 561                        }
 562                        if {$author_abbr eq {}} {
 563                                set author_abbr { |}
 564                        } else {
 565                                set author_abbr [string range $author_abbr 0 3]
 566                        }
 567                        unset a_name
 568
 569                        set first_lno $lno
 570                        while {
 571                           $first_lno > 1
 572                        && $cmit eq [lindex $line_data [expr {$first_lno - 1}] 0]
 573                        && $file eq [lindex $line_data [expr {$first_lno - 1}] 1]
 574                        } {
 575                                incr first_lno -1
 576                        }
 577
 578                        set color {}
 579                        if {$first_lno < $lno} {
 580                                foreach g [$w_file tag names $first_lno.0] {
 581                                        if {[regexp {^color[0-9]+$} $g]} {
 582                                                set color $g
 583                                                break
 584                                        }
 585                                }
 586                        } else {
 587                                set i [lsort [concat \
 588                                        [$w_file tag names "[expr {$first_lno - 1}].0"] \
 589                                        [$w_file tag names "[expr {$lno + $n}].0"] \
 590                                        ]]
 591                                for {set g 0} {$g < [llength $group_colors]} {incr g} {
 592                                        if {[lsearch -sorted -exact $i color$g] == -1} {
 593                                                set color color$g
 594                                                break
 595                                        }
 596                                }
 597                        }
 598                        if {$color eq {}} {
 599                                set color color0
 600                        }
 601
 602                        while {$n > 0} {
 603                                set lno_e "$lno.0 lineend + 1c"
 604                                if {[lindex $line_data $lno] ne {}} {
 605                                        set g [lindex $line_data $lno 0]
 606                                        foreach i $w_columns {
 607                                                $i tag remove g$g $lno.0 $lno_e
 608                                        }
 609                                }
 610                                lset line_data $lno [list $cmit $file $oln]
 611
 612                                $cur_w delete $lno.0 "$lno.0 lineend"
 613                                if {$lno == $first_lno} {
 614                                        $cur_w insert $lno.0 $commit_abbr $commit_type
 615                                } elseif {$lno == [expr {$first_lno + 1}]} {
 616                                        $cur_w insert $lno.0 $author_abbr author_abbr
 617                                } else {
 618                                        $cur_w insert $lno.0 { |}
 619                                }
 620
 621                                foreach i $w_columns {
 622                                        if {$cur_w eq $w_amov} {
 623                                                for {set g 0} \
 624                                                        {$g < [llength $group_colors]} \
 625                                                        {incr g} {
 626                                                        $i tag remove color$g $lno.0 $lno_e
 627                                                }
 628                                                $i tag add $color $lno.0 $lno_e
 629                                        }
 630                                        $i tag add g$cmit $lno.0 $lno_e
 631                                }
 632
 633                                if {$highlight_column eq $cur_w} {
 634                                        if {$highlight_line == -1
 635                                         && [lindex [$w_file yview] 0] == 0} {
 636                                                $w_file see $lno.0
 637                                                set highlight_line $lno
 638                                        }
 639                                        if {$highlight_line == $lno} {
 640                                                _showcommit $this $cur_w $lno
 641                                        }
 642                                }
 643
 644                                incr n -1
 645                                incr lno
 646                                incr oln
 647                                incr blame_lines
 648                        }
 649
 650                        while {
 651                           $cmit eq [lindex $line_data $lno 0]
 652                        && $file eq [lindex $line_data $lno 1]
 653                        } {
 654                                $cur_w delete $lno.0 "$lno.0 lineend"
 655
 656                                if {$lno == $first_lno} {
 657                                        $cur_w insert $lno.0 $commit_abbr $commit_type
 658                                } elseif {$lno == [expr {$first_lno + 1}]} {
 659                                        $cur_w insert $lno.0 $author_abbr author_abbr
 660                                } else {
 661                                        $cur_w insert $lno.0 { |}
 662                                }
 663
 664                                if {$cur_w eq $w_amov} {
 665                                        foreach i $w_columns {
 666                                                for {set g 0} \
 667                                                        {$g < [llength $group_colors]} \
 668                                                        {incr g} {
 669                                                        $i tag remove color$g $lno.0 $lno_e
 670                                                }
 671                                                $i tag add $color $lno.0 $lno_e
 672                                        }
 673                                }
 674
 675                                incr lno
 676                        }
 677
 678                } elseif {[regexp {^([a-z-]+) (.*)$} $line line key data]} {
 679                        set header($r_commit,$key) $data
 680                }
 681        }
 682        $cur_w conf -state disabled
 683
 684        if {[eof $fd]} {
 685                close $fd
 686                if {$cur_w eq $w_asim} {
 687                        # Switches for original location detection
 688                        set threshold [get_config gui.copyblamethreshold]
 689                        set original_options [list "-C$threshold"]
 690
 691                        if {![is_config_true gui.fastcopyblame]} {
 692                                # thorough copy search; insert before the threshold
 693                                set original_options [linsert $original_options 0 -C]
 694                        }
 695                        if {[git-version >= 1.5.3]} {
 696                                lappend original_options -w ; # ignore indentation changes
 697                        }
 698
 699                        _exec_blame $this $w_amov @amov_data \
 700                                $original_options \
 701                                [mc "Loading original location annotations..."]
 702                } else {
 703                        set current_fd {}
 704                        $status stop [mc "Annotation complete."]
 705                }
 706        } else {
 707                $status update $blame_lines $total_lines
 708        }
 709} ifdeleted { catch {close $fd} }
 710
 711method _click {cur_w pos} {
 712        set lno [lindex [split [$cur_w index $pos] .] 0]
 713        _showcommit $this $cur_w $lno
 714}
 715
 716method _load_commit {cur_w cur_d pos} {
 717        upvar #0 $cur_d line_data
 718        set lno [lindex [split [$cur_w index $pos] .] 0]
 719        set dat [lindex $line_data $lno]
 720        if {$dat ne {}} {
 721                lappend history [list \
 722                        $commit $path \
 723                        $highlight_column \
 724                        $highlight_line \
 725                        [lindex [$w_file xview] 0] \
 726                        [lindex [$w_file yview] 0] \
 727                        ]
 728                set commit [lindex $dat 0]
 729                set path   [lindex $dat 1]
 730                _load $this [list [lindex $dat 2]]
 731        }
 732}
 733
 734method _showcommit {cur_w lno} {
 735        global repo_config
 736        variable active_color
 737
 738        if {$highlight_commit ne {}} {
 739                foreach i $w_columns {
 740                        $i tag conf g$highlight_commit -background {}
 741                        $i tag lower g$highlight_commit
 742                }
 743        }
 744
 745        if {$cur_w eq $w_asim} {
 746                set dat [lindex $asim_data $lno]
 747                set highlight_column $w_asim
 748        } else {
 749                set dat [lindex $amov_data $lno]
 750                set highlight_column $w_amov
 751        }
 752
 753        $w_cviewer conf -state normal
 754        $w_cviewer delete 0.0 end
 755
 756        if {$dat eq {}} {
 757                set cmit {}
 758                $w_cviewer insert end [mc "Loading annotation..."] still_loading
 759        } else {
 760                set cmit [lindex $dat 0]
 761                set file [lindex $dat 1]
 762
 763                foreach i $w_columns {
 764                        $i tag conf g$cmit -background $active_color
 765                        $i tag raise g$cmit
 766                }
 767
 768                set author_name {}
 769                set author_email {}
 770                set author_time {}
 771                catch {set author_name $header($cmit,author)}
 772                catch {set author_email $header($cmit,author-mail)}
 773                catch {set author_time [format_date $header($cmit,author-time)]}
 774
 775                set committer_name {}
 776                set committer_email {}
 777                set committer_time {}
 778                catch {set committer_name $header($cmit,committer)}
 779                catch {set committer_email $header($cmit,committer-mail)}
 780                catch {set committer_time [format_date $header($cmit,committer-time)]}
 781
 782                if {[catch {set msg $header($cmit,message)}]} {
 783                        set msg {}
 784                        catch {
 785                                set fd [git_read cat-file commit $cmit]
 786                                fconfigure $fd -encoding binary -translation lf
 787                                if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
 788                                        set enc utf-8
 789                                }
 790                                while {[gets $fd line] > 0} {
 791                                        if {[string match {encoding *} $line]} {
 792                                                set enc [string tolower [string range $line 9 end]]
 793                                        }
 794                                }
 795                                set msg [read $fd]
 796                                close $fd
 797
 798                                set enc [tcl_encoding $enc]
 799                                if {$enc ne {}} {
 800                                        set msg [encoding convertfrom $enc $msg]
 801                                        set author_name [encoding convertfrom $enc $author_name]
 802                                        set committer_name [encoding convertfrom $enc $committer_name]
 803                                        set header($cmit,author) $author_name
 804                                        set header($cmit,committer) $committer_name
 805                                        set header($cmit,summary) \
 806                                        [encoding convertfrom $enc $header($cmit,summary)]
 807                                }
 808                                set msg [string trim $msg]
 809                        }
 810                        set header($cmit,message) $msg
 811                }
 812
 813                $w_cviewer insert end "commit $cmit\n" header_key
 814                $w_cviewer insert end [strcat [mc "Author:"] "\t"] header_key
 815                $w_cviewer insert end "$author_name $author_email" header_val
 816                $w_cviewer insert end "  $author_time\n" header_val
 817
 818                $w_cviewer insert end [strcat [mc "Committer:"] "\t"] header_key
 819                $w_cviewer insert end "$committer_name $committer_email" header_val
 820                $w_cviewer insert end "  $committer_time\n" header_val
 821
 822                if {$file ne $path} {
 823                        $w_cviewer insert end [strcat [mc "Original File:"] "\t"] header_key
 824                        $w_cviewer insert end "[escape_path $file]\n" header_val
 825                }
 826
 827                $w_cviewer insert end "\n$msg"
 828        }
 829        $w_cviewer conf -state disabled
 830
 831        set highlight_line $lno
 832        set highlight_commit $cmit
 833
 834        if {[lsearch -exact $tooltip_commit $highlight_commit] != -1} {
 835                _hide_tooltip $this
 836        }
 837}
 838
 839method _copycommit {} {
 840        set pos @$::cursorX,$::cursorY
 841        set lno [lindex [split [$::cursorW index $pos] .] 0]
 842        set dat [lindex $amov_data $lno]
 843        if {$dat ne {}} {
 844                clipboard clear
 845                clipboard append \
 846                        -format STRING \
 847                        -type STRING \
 848                        -- [lindex $dat 0]
 849        }
 850}
 851
 852method _show_tooltip {cur_w pos} {
 853        if {$tooltip_wm ne {}} {
 854                _open_tooltip $this $cur_w
 855        } elseif {$tooltip_timer eq {}} {
 856                set tooltip_timer [after 1000 [cb _open_tooltip $cur_w]]
 857        }
 858}
 859
 860method _open_tooltip {cur_w} {
 861        set tooltip_timer {}
 862        set pos_x [winfo pointerx $cur_w]
 863        set pos_y [winfo pointery $cur_w]
 864        if {[winfo containing $pos_x $pos_y] ne $cur_w} {
 865                _hide_tooltip $this
 866                return
 867        }
 868
 869        if {$tooltip_wm ne "$cur_w.tooltip"} {
 870                _hide_tooltip $this
 871
 872                set tooltip_wm [toplevel $cur_w.tooltip -borderwidth 1]
 873                wm overrideredirect $tooltip_wm 1
 874                wm transient $tooltip_wm [winfo toplevel $cur_w]
 875                set tooltip_t $tooltip_wm.label
 876                text $tooltip_t \
 877                        -takefocus 0 \
 878                        -highlightthickness 0 \
 879                        -relief flat \
 880                        -borderwidth 0 \
 881                        -wrap none \
 882                        -background lightyellow \
 883                        -foreground black
 884                $tooltip_t tag conf section_header -font font_uibold
 885                pack $tooltip_t
 886        } else {
 887                $tooltip_t conf -state normal
 888                $tooltip_t delete 0.0 end
 889        }
 890
 891        set pos @[join [list \
 892                [expr {$pos_x - [winfo rootx $cur_w]}] \
 893                [expr {$pos_y - [winfo rooty $cur_w]}]] ,]
 894        set lno [lindex [split [$cur_w index $pos] .] 0]
 895        if {$cur_w eq $w_amov} {
 896                set dat [lindex $amov_data $lno]
 897                set org {}
 898        } else {
 899                set dat [lindex $asim_data $lno]
 900                set org [lindex $amov_data $lno]
 901        }
 902
 903        if {$dat eq {}} {
 904                _hide_tooltip $this
 905                return
 906        }
 907
 908        set cmit [lindex $dat 0]
 909        set tooltip_commit [list $cmit]
 910
 911        set author_name {}
 912        set summary     {}
 913        set author_time {}
 914        catch {set author_name $header($cmit,author)}
 915        catch {set summary     $header($cmit,summary)}
 916        catch {set author_time [format_date $header($cmit,author-time)]}
 917
 918        $tooltip_t insert end "commit $cmit\n"
 919        $tooltip_t insert end "$author_name  $author_time\n"
 920        $tooltip_t insert end "$summary"
 921
 922        if {$org ne {} && [lindex $org 0] ne $cmit} {
 923                set save [$tooltip_t get 0.0 end]
 924                $tooltip_t delete 0.0 end
 925
 926                set cmit [lindex $org 0]
 927                set file [lindex $org 1]
 928                lappend tooltip_commit $cmit
 929
 930                set author_name {}
 931                set summary     {}
 932                set author_time {}
 933                catch {set author_name $header($cmit,author)}
 934                catch {set summary     $header($cmit,summary)}
 935                catch {set author_time [format_date $header($cmit,author-time)]}
 936
 937                $tooltip_t insert end [strcat [mc "Originally By:"] "\n"] section_header
 938                $tooltip_t insert end "commit $cmit\n"
 939                $tooltip_t insert end "$author_name  $author_time\n"
 940                $tooltip_t insert end "$summary\n"
 941
 942                if {$file ne $path} {
 943                        $tooltip_t insert end [strcat [mc "In File:"] " "] section_header
 944                        $tooltip_t insert end "$file\n"
 945                }
 946
 947                $tooltip_t insert end "\n"
 948                $tooltip_t insert end [strcat [mc "Copied Or Moved Here By:"] "\n"] section_header
 949                $tooltip_t insert end $save
 950        }
 951
 952        $tooltip_t conf -state disabled
 953        _position_tooltip $this
 954}
 955
 956method _position_tooltip {} {
 957        set max_h [lindex [split [$tooltip_t index end] .] 0]
 958        set max_w 0
 959        for {set i 1} {$i <= $max_h} {incr i} {
 960                set c [lindex [split [$tooltip_t index "$i.0 lineend"] .] 1]
 961                if {$c > $max_w} {set max_w $c}
 962        }
 963        $tooltip_t conf -width $max_w -height $max_h
 964
 965        set req_w [winfo reqwidth  $tooltip_t]
 966        set req_h [winfo reqheight $tooltip_t]
 967        set pos_x [expr {[winfo pointerx .] +  5}]
 968        set pos_y [expr {[winfo pointery .] + 10}]
 969
 970        set g "${req_w}x${req_h}"
 971        if {$pos_x >= 0} {append g +}
 972        append g $pos_x
 973        if {$pos_y >= 0} {append g +}
 974        append g $pos_y
 975
 976        wm geometry $tooltip_wm $g
 977        raise $tooltip_wm
 978}
 979
 980method _hide_tooltip {} {
 981        if {$tooltip_wm ne {}} {
 982                destroy $tooltip_wm
 983                set tooltip_wm {}
 984                set tooltip_commit {}
 985        }
 986        if {$tooltip_timer ne {}} {
 987                after cancel $tooltip_timer
 988                set tooltip_timer {}
 989        }
 990}
 991
 992method _resize {new_height} {
 993        set diff [expr {$new_height - $old_height}]
 994        if {$diff == 0} return
 995
 996        set my [expr {[winfo height $w.file_pane] - 25}]
 997        set o [$w.file_pane sash coord 0]
 998        set ox [lindex $o 0]
 999        set oy [expr {[lindex $o 1] + $diff}]
1000        if {$oy < 0}   {set oy 0}
1001        if {$oy > $my} {set oy $my}
1002        $w.file_pane sash place 0 $ox $oy
1003
1004        set old_height $new_height
1005}
1006
1007}