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