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