85e9e0dc32e08361a755bea65811274cff984d39
   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
   8field commit    ; # input commit to blame
   9field path      ; # input filename to view in $commit
  10field history {}; # viewer history: {commit path}
  11
  12field w          ; # top window in this viewer
  13field w_back     ; # our back button
  14field w_path     ; # label showing the current file path
  15field w_line     ; # text column: all line numbers
  16field w_cgrp     ; # text column: abbreviated commit SHA-1s
  17field w_load     ; # text column: loaded indicator
  18field w_file     ; # text column: actual file data
  19field w_cmit     ; # pane showing commit message
  20field status     ; # text variable bound to status bar
  21field old_height ; # last known height of $w.file_pane
  22
  23field current_fd       {} ; # background process running
  24field highlight_line   -1 ; # current line selected
  25field highlight_commit {} ; # sha1 of commit selected
  26
  27field total_lines       0  ; # total length of file
  28field blame_lines       0  ; # number of lines computed
  29field commit_count      0  ; # number of commits in $commit_list
  30field commit_list      {}  ; # list of commit sha1 in receipt order
  31field order                ; # array commit -> receipt order
  32field header               ; # array commit,key -> header field
  33field line_commit          ; # array line -> sha1 commit
  34field line_file            ; # array line -> file name
  35
  36field r_commit      ; # commit currently being parsed
  37field r_orig_line   ; # original line number
  38field r_final_line  ; # final line number
  39field r_line_count  ; # lines in this region
  40
  41field tooltip_wm     {} ; # Current tooltip toplevel, if open
  42field tooltip_timer  {} ; # Current timer event for our tooltip
  43field tooltip_commit {} ; # Commit in tooltip
  44field tooltip_text   {} ; # Text in current tooltip
  45
  46variable active_color #98e1a0
  47variable group_colors {
  48        #cbcbcb
  49        #e1e1e1
  50}
  51
  52constructor new {i_commit i_path} {
  53        variable active_color
  54        global cursor_ptr
  55
  56        set commit $i_commit
  57        set path   $i_path
  58
  59        make_toplevel top w
  60        wm title $top "[appname] ([reponame]): File Viewer"
  61
  62        frame $w.header -background orange
  63        label $w.header.commit_l \
  64                -text {Commit:} \
  65                -background orange \
  66                -anchor w \
  67                -justify left
  68        set w_back $w.header.commit_b
  69        label $w_back \
  70                -image ::blame::img_back_arrow \
  71                -borderwidth 0 \
  72                -relief flat \
  73                -state disabled \
  74                -background orange \
  75                -activebackground orange
  76        bind $w_back <Button-1> "
  77                if {\[$w_back cget -state\] eq {normal}} {
  78                        [cb _history_menu]
  79                }
  80                "
  81        label $w.header.commit \
  82                -textvariable @commit \
  83                -background orange \
  84                -anchor w \
  85                -justify left
  86        label $w.header.path_l \
  87                -text {File:} \
  88                -background orange \
  89                -anchor w \
  90                -justify left
  91        set w_path $w.header.path
  92        label $w_path \
  93                -background orange \
  94                -anchor w \
  95                -justify left
  96        pack $w.header.commit_l -side left
  97        pack $w_back -side left
  98        pack $w.header.commit -side left
  99        pack $w_path -fill x -side right
 100        pack $w.header.path_l -side right
 101
 102        panedwindow $w.file_pane -orient vertical
 103        frame $w.file_pane.out
 104        frame $w.file_pane.cm
 105        $w.file_pane add $w.file_pane.out \
 106                -sticky nsew \
 107                -minsize 100 \
 108                -height 100 \
 109                -width 100
 110        $w.file_pane add $w.file_pane.cm \
 111                -sticky nsew \
 112                -minsize 25 \
 113                -height 25 \
 114                -width 100
 115
 116        set w_load $w.file_pane.out.loaded_t
 117        text $w_load \
 118                -background white -borderwidth 0 \
 119                -state disabled \
 120                -wrap none \
 121                -height 40 \
 122                -width 1 \
 123                -font font_diff
 124        $w_load tag conf annotated -background grey
 125
 126        set w_line $w.file_pane.out.linenumber_t
 127        text $w_line \
 128                -background white -borderwidth 0 \
 129                -state disabled \
 130                -wrap none \
 131                -height 40 \
 132                -width 5 \
 133                -font font_diff
 134        $w_line tag conf linenumber -justify right
 135
 136        set w_cgrp $w.file_pane.out.commit_t
 137        text $w_cgrp \
 138                -background white -borderwidth 0 \
 139                -state disabled \
 140                -wrap none \
 141                -height 40 \
 142                -width 4 \
 143                -font font_diff
 144        $w_cgrp tag conf curr_commit
 145        $w_cgrp tag conf prior_commit \
 146                -foreground blue \
 147                -underline 1
 148        $w_cgrp tag bind prior_commit \
 149                <Button-1> \
 150                "[cb _load_commit @%x,%y];break"
 151
 152        set w_file $w.file_pane.out.file_t
 153        text $w_file \
 154                -background white -borderwidth 0 \
 155                -state disabled \
 156                -wrap none \
 157                -height 40 \
 158                -width 80 \
 159                -xscrollcommand [list $w.file_pane.out.sbx set] \
 160                -font font_diff
 161
 162        scrollbar $w.file_pane.out.sbx \
 163                -orient h \
 164                -command [list $w_file xview]
 165        scrollbar $w.file_pane.out.sby \
 166                -orient v \
 167                -command [list scrollbar2many [list \
 168                $w_load \
 169                $w_line \
 170                $w_cgrp \
 171                $w_file \
 172                ] yview]
 173        grid \
 174                $w_cgrp \
 175                $w_line \
 176                $w_load \
 177                $w_file \
 178                $w.file_pane.out.sby \
 179                -sticky nsew
 180        grid conf $w.file_pane.out.sbx -column 3 -sticky we
 181        grid columnconfigure $w.file_pane.out 3 -weight 1
 182        grid rowconfigure $w.file_pane.out 0 -weight 1
 183
 184        set w_cmit $w.file_pane.cm.t
 185        text $w_cmit \
 186                -background white -borderwidth 0 \
 187                -state disabled \
 188                -wrap none \
 189                -height 10 \
 190                -width 80 \
 191                -xscrollcommand [list $w.file_pane.cm.sbx set] \
 192                -yscrollcommand [list $w.file_pane.cm.sby set] \
 193                -font font_diff
 194        $w_cmit tag conf header_key \
 195                -tabs {3c} \
 196                -background $active_color \
 197                -font font_uibold
 198        $w_cmit tag conf header_val \
 199                -background $active_color \
 200                -font font_ui
 201        $w_cmit tag raise sel
 202        scrollbar $w.file_pane.cm.sbx \
 203                -orient h \
 204                -command [list $w_cmit xview]
 205        scrollbar $w.file_pane.cm.sby \
 206                -orient v \
 207                -command [list $w_cmit yview]
 208        pack $w.file_pane.cm.sby -side right -fill y
 209        pack $w.file_pane.cm.sbx -side bottom -fill x
 210        pack $w_cmit -expand 1 -fill both
 211
 212        frame $w.status \
 213                -borderwidth 1 \
 214                -relief sunken
 215        label $w.status.l \
 216                -textvariable @status \
 217                -anchor w \
 218                -justify left
 219        pack $w.status.l -side left
 220
 221        menu $w.ctxm -tearoff 0
 222        $w.ctxm add command \
 223                -label "Copy Commit" \
 224                -command [cb _copycommit]
 225
 226        foreach i [list \
 227                $w_cgrp \
 228                $w_load \
 229                $w_line \
 230                $w_file] {
 231                $i conf -cursor $cursor_ptr
 232                $i conf -yscrollcommand \
 233                        [list many2scrollbar [list \
 234                        $w_cgrp \
 235                        $w_load \
 236                        $w_line \
 237                        $w_file \
 238                        ] yview $w.file_pane.out.sby]
 239                bind $i <Button-1> "
 240                        [cb _hide_tooltip]
 241                        [cb _click $i @%x,%y]
 242                        focus $i
 243                "
 244                bind $i <Any-Motion>  [cb _show_tooltip $i @%x,%y]
 245                bind $i <Any-Enter>   [cb _hide_tooltip]
 246                bind $i <Any-Leave>   [cb _hide_tooltip]
 247                bind_button3 $i "
 248                        [cb _hide_tooltip]
 249                        set cursorX %x
 250                        set cursorY %y
 251                        set cursorW %W
 252                        tk_popup $w.ctxm %X %Y
 253                "
 254        }
 255
 256        foreach i [list \
 257                $w_cgrp \
 258                $w_load \
 259                $w_line \
 260                $w_file \
 261                $w_cmit] {
 262                bind $i <Key-Up>        {catch {%W yview scroll -1 units};break}
 263                bind $i <Key-Down>      {catch {%W yview scroll  1 units};break}
 264                bind $i <Key-Left>      {catch {%W xview scroll -1 units};break}
 265                bind $i <Key-Right>     {catch {%W xview scroll  1 units};break}
 266                bind $i <Key-k>         {catch {%W yview scroll -1 units};break}
 267                bind $i <Key-j>         {catch {%W yview scroll  1 units};break}
 268                bind $i <Key-h>         {catch {%W xview scroll -1 units};break}
 269                bind $i <Key-l>         {catch {%W xview scroll  1 units};break}
 270                bind $i <Control-Key-b> {catch {%W yview scroll -1 pages};break}
 271                bind $i <Control-Key-f> {catch {%W yview scroll  1 pages};break}
 272        }
 273
 274        bind $w_cmit <Button-1> [list focus $w_cmit]
 275        bind $top <Visibility> [list focus $top]
 276        bind $w_file <Destroy> [list delete_this $this]
 277
 278        grid configure $w.header -sticky ew
 279        grid configure $w.file_pane -sticky nsew
 280        grid configure $w.status -sticky ew
 281        grid columnconfigure $top 0 -weight 1
 282        grid rowconfigure $top 0 -weight 0
 283        grid rowconfigure $top 1 -weight 1
 284        grid rowconfigure $top 2 -weight 0
 285
 286        set req_w [winfo reqwidth  $top]
 287        set req_h [winfo reqheight $top]
 288        if {$req_w < 600} {set req_w 600}
 289        if {$req_h < 400} {set req_h 400}
 290        set g "${req_w}x${req_h}"
 291        wm geometry $top $g
 292        update
 293
 294        set old_height [winfo height $w.file_pane]
 295        $w.file_pane sash place 0 \
 296                [lindex [$w.file_pane sash coord 0] 0] \
 297                [expr {int($old_height * 0.70)}]
 298        bind $w.file_pane <Configure> \
 299        "if {{$w.file_pane} eq {%W}} {[cb _resize %h]}"
 300
 301        _load $this
 302}
 303
 304method _load {} {
 305        _hide_tooltip $this
 306
 307        if {$total_lines != 0 || $current_fd ne {}} {
 308                if {$current_fd ne {}} {
 309                        catch {close $current_fd}
 310                        set current_fd {}
 311                }
 312
 313                set highlight_line -1
 314                set highlight_commit {}
 315                set total_lines 0
 316                set blame_lines 0
 317                set commit_count 0
 318                set commit_list {}
 319                array unset order
 320                array unset line_commit
 321                array unset line_file
 322
 323                $w_load conf -state normal
 324                $w_cgrp conf -state normal
 325                $w_line conf -state normal
 326                $w_file conf -state normal
 327
 328                $w_load delete 0.0 end
 329                $w_cgrp delete 0.0 end
 330                $w_line delete 0.0 end
 331                $w_file delete 0.0 end
 332
 333                $w_load conf -state disabled
 334                $w_cgrp conf -state disabled
 335                $w_line conf -state disabled
 336                $w_file conf -state disabled
 337        }
 338
 339        if {[winfo exists $w.status.c]} {
 340                $w.status.c coords bar 0 0 0 20
 341        } else {
 342                canvas $w.status.c \
 343                        -width 100 \
 344                        -height [expr {int([winfo reqheight $w.status.l] * 0.6)}] \
 345                        -borderwidth 1 \
 346                        -relief groove \
 347                        -highlightt 0
 348                $w.status.c create rectangle 0 0 0 20 -tags bar -fill navy
 349                pack $w.status.c -side right
 350        }
 351
 352        if {$history eq {}} {
 353                $w_back conf -state disabled
 354        } else {
 355                $w_back conf -state normal
 356        }
 357        lappend history [list $commit $path]
 358
 359        set status "Loading $commit:[escape_path $path]..."
 360        $w_path conf -text [escape_path $path]
 361        if {$commit eq {}} {
 362                set fd [open $path r]
 363        } else {
 364                set cmd [list git cat-file blob "$commit:$path"]
 365                set fd [open "| $cmd" r]
 366        }
 367        fconfigure $fd -blocking 0 -translation lf -encoding binary
 368        fileevent $fd readable [cb _read_file $fd]
 369        set current_fd $fd
 370}
 371
 372method _history_menu {} {
 373        set m $w.backmenu
 374        if {[winfo exists $m]} {
 375                $m delete 0 end
 376        } else {
 377                menu $m -tearoff 0
 378        }
 379
 380        for {set i [expr {[llength $history] - 2}]
 381                } {$i >= 0} {incr i -1} {
 382                set e [lindex $history $i]
 383                set c [lindex $e 0]
 384                set f [lindex $e 1]
 385
 386                if {[regexp {^[0-9a-f]{40}$} $c]} {
 387                        set t [string range $c 0 8]...
 388                } else {
 389                        set t $c
 390                }
 391                if {![catch {set summary $header($c,summary)}]} {
 392                        append t " $summary"
 393                        if {[string length $t] > 70} {
 394                                set t [string range $t 0 66]...
 395                        }
 396                }
 397
 398                $m add command -label $t -command [cb _goback $i $c $f]
 399        }
 400        set X [winfo rootx $w_back]
 401        set Y [expr {[winfo rooty $w_back] + [winfo height $w_back]}]
 402        tk_popup $m $X $Y
 403}
 404
 405method _goback {i c f} {
 406        set history [lrange $history 0 [expr {$i - 1}]]
 407        set commit $c
 408        set path $f
 409        _load $this
 410}
 411
 412method _read_file {fd} {
 413        if {$fd ne $current_fd} {
 414                catch {close $fd}
 415                return
 416        }
 417
 418        $w_load conf -state normal
 419        $w_cgrp conf -state normal
 420        $w_line conf -state normal
 421        $w_file conf -state normal
 422        while {[gets $fd line] >= 0} {
 423                regsub "\r\$" $line {} line
 424                incr total_lines
 425
 426                if {$total_lines > 1} {
 427                        $w_load insert end "\n"
 428                        $w_cgrp insert end "\n"
 429                        $w_line insert end "\n"
 430                        $w_file insert end "\n"
 431                }
 432
 433                $w_line insert end "$total_lines" linenumber
 434                $w_file insert end "$line"
 435        }
 436        $w_load conf -state disabled
 437        $w_cgrp conf -state disabled
 438        $w_line conf -state disabled
 439        $w_file conf -state disabled
 440
 441        if {[eof $fd]} {
 442                close $fd
 443                _status $this
 444                set cmd {nice git blame -M -C --incremental}
 445                if {$commit eq {}} {
 446                        lappend cmd --contents $path
 447                } else {
 448                        lappend cmd $commit
 449                }
 450                lappend cmd -- $path
 451                set fd [open "| $cmd" r]
 452                fconfigure $fd -blocking 0 -translation lf -encoding binary
 453                fileevent $fd readable [cb _read_blame $fd]
 454                set current_fd $fd
 455        }
 456} ifdeleted { catch {close $fd} }
 457
 458method _read_blame {fd} {
 459        variable group_colors
 460
 461        if {$fd ne $current_fd} {
 462                catch {close $fd}
 463                return
 464        }
 465
 466        $w_cgrp conf -state normal
 467        while {[gets $fd line] >= 0} {
 468                if {[regexp {^([a-z0-9]{40}) (\d+) (\d+) (\d+)$} $line line \
 469                        cmit original_line final_line line_count]} {
 470                        set r_commit     $cmit
 471                        set r_orig_line  $original_line
 472                        set r_final_line $final_line
 473                        set r_line_count $line_count
 474
 475                        if {[catch {set g $order($cmit)}]} {
 476                                set bg [lindex $group_colors 0]
 477                                set group_colors [lrange $group_colors 1 end]
 478                                lappend group_colors $bg
 479
 480                                $w_cgrp tag conf g$cmit -background $bg
 481                                $w_line tag conf g$cmit -background $bg
 482                                $w_file tag conf g$cmit -background $bg
 483
 484                                set order($cmit) $commit_count
 485                                incr commit_count
 486                                lappend commit_list $cmit
 487                        }
 488                } elseif {[string match {filename *} $line]} {
 489                        set file [string range $line 9 end]
 490                        set n    $r_line_count
 491                        set lno  $r_final_line
 492                        set cmit $r_commit
 493
 494                        if {[regexp {^0{40}$} $cmit]} {
 495                                set commit_abbr work
 496                                set commit_type curr_commit
 497                        } elseif {$cmit eq $commit} {
 498                                set commit_abbr this
 499                                set commit_type curr_commit
 500                        } else {
 501                                set commit_type prior_commit
 502                                set commit_abbr [string range $cmit 0 4]
 503                        }
 504
 505                        set author_abbr {}
 506                        set a_name {}
 507                        catch {set a_name $header($cmit,author)}
 508                        while {$a_name ne {}} {
 509                                if {![regexp {^([[:upper:]])} $a_name _a]} break
 510                                append author_abbr $_a
 511                                unset _a
 512                                if {![regsub \
 513                                        {^[[:upper:]][^\s]*\s+} \
 514                                        $a_name {} a_name ]} break
 515                        }
 516                        if {$author_abbr eq {}} {
 517                                set author_abbr { |}
 518                        } else {
 519                                set author_abbr [string range $author_abbr 0 3]
 520                                while {[string length $author_abbr] < 4} {
 521                                        set author_abbr " $author_abbr"
 522                                }
 523                        }
 524                        unset a_name
 525
 526                        set first_lno $lno
 527                        while {
 528                           ![catch {set ncmit $line_commit([expr {$first_lno - 1}])}]
 529                        && ![catch {set nfile $line_file([expr {$first_lno - 1}])}]
 530                        && $ncmit eq $cmit
 531                        && $nfile eq $file
 532                        } {
 533                                incr first_lno -1
 534                        }
 535
 536                        while {$n > 0} {
 537                                set lno_e "$lno.0 lineend + 1c"
 538                                if {[catch {set g g$line_commit($lno)}]} {
 539                                        $w_load tag add annotated $lno.0 $lno_e
 540                                } else {
 541                                        $w_cgrp tag remove g$g $lno.0 $lno_e
 542                                        $w_line tag remove g$g $lno.0 $lno_e
 543                                        $w_file tag remove g$g $lno.0 $lno_e
 544
 545                                        $w_cgrp tag remove a$g $lno.0 $lno_e
 546                                        $w_line tag remove a$g $lno.0 $lno_e
 547                                        $w_file tag remove a$g $lno.0 $lno_e
 548                                }
 549
 550                                set line_commit($lno) $cmit
 551                                set line_file($lno)   $file
 552
 553                                $w_cgrp delete $lno.0 "$lno.0 lineend"
 554                                if {$lno == $first_lno} {
 555                                        $w_cgrp insert $lno.0 $commit_abbr $commit_type
 556                                } elseif {$lno == [expr {$first_lno + 1}]} {
 557                                        $w_cgrp insert $lno.0 $author_abbr
 558                                } else {
 559                                        $w_cgrp insert $lno.0 { |}
 560                                }
 561
 562                                $w_cgrp tag add g$cmit $lno.0 $lno_e
 563                                $w_line tag add g$cmit $lno.0 $lno_e
 564                                $w_file tag add g$cmit $lno.0 $lno_e
 565
 566                                $w_cgrp tag add a$cmit $lno.0 $lno_e
 567                                $w_line tag add a$cmit $lno.0 $lno_e
 568                                $w_file tag add a$cmit $lno.0 $lno_e
 569
 570                                if {$highlight_line == -1} {
 571                                        if {[lindex [$w_file yview] 0] == 0} {
 572                                                $w_file see $lno.0
 573                                                _showcommit $this $lno
 574                                        }
 575                                } elseif {$highlight_line == $lno} {
 576                                        _showcommit $this $lno
 577                                }
 578
 579                                incr n -1
 580                                incr lno
 581                                incr blame_lines
 582                        }
 583
 584                        while {
 585                           ![catch {set ncmit $line_commit($lno)}]
 586                        && ![catch {set nfile $line_file($lno)}]
 587                        && $ncmit eq $cmit
 588                        && $nfile eq $file
 589                        } {
 590                                $w_cgrp delete $lno.0 "$lno.0 lineend"
 591
 592                                if {$lno == $first_lno} {
 593                                        $w_cgrp insert $lno.0 $commit_abbr $commit_type
 594                                } elseif {$lno == [expr {$first_lno + 1}]} {
 595                                        $w_cgrp insert $lno.0 $author_abbr
 596                                } else {
 597                                        $w_cgrp insert $lno.0 { |}
 598                                }
 599                                incr lno
 600                        }
 601
 602                } elseif {[regexp {^([a-z-]+) (.*)$} $line line key data]} {
 603                        set header($r_commit,$key) $data
 604                }
 605        }
 606        $w_cgrp conf -state disabled
 607
 608        if {[eof $fd]} {
 609                close $fd
 610                set current_fd {}
 611                set status {Annotation complete.}
 612                destroy $w.status.c
 613        } else {
 614                _status $this
 615        }
 616} ifdeleted { catch {close $fd} }
 617
 618method _status {} {
 619        set have  $blame_lines
 620        set total $total_lines
 621        set pdone 0
 622        if {$total} {set pdone [expr {100 * $have / $total}]}
 623
 624        set status [format \
 625                "Loading annotations... %i of %i lines annotated (%2i%%)" \
 626                $have $total $pdone]
 627        $w.status.c coords bar 0 0 $pdone 20
 628}
 629
 630method _click {cur_w pos} {
 631        set lno [lindex [split [$cur_w index $pos] .] 0]
 632        if {$lno eq {}} return
 633        _showcommit $this $lno
 634}
 635
 636method _load_commit {pos} {
 637        set lno [lindex [split [$w_cgrp index $pos] .] 0]
 638        if {[catch {set cmit $line_commit($lno)}]} return
 639        if {[catch {set file $line_file($lno)  }]} return
 640
 641        set commit $cmit
 642        set path $file
 643        _load $this
 644}
 645
 646method _showcommit {lno} {
 647        global repo_config
 648        variable active_color
 649
 650        if {$highlight_commit ne {}} {
 651                set cmit $highlight_commit
 652                $w_cgrp tag conf a$cmit -background {}
 653                $w_line tag conf a$cmit -background {}
 654                $w_file tag conf a$cmit -background {}
 655        }
 656
 657        $w_cmit conf -state normal
 658        $w_cmit delete 0.0 end
 659        if {[catch {set cmit $line_commit($lno)}]} {
 660                set cmit {}
 661                $w_cmit insert end "Loading annotation..."
 662        } else {
 663                $w_cgrp tag conf a$cmit -background $active_color
 664                $w_line tag conf a$cmit -background $active_color
 665                $w_file tag conf a$cmit -background $active_color
 666
 667                set author_name {}
 668                set author_email {}
 669                set author_time {}
 670                catch {set author_name $header($cmit,author)}
 671                catch {set author_email $header($cmit,author-mail)}
 672                catch {set author_time [clock format \
 673                        $header($cmit,author-time) \
 674                        -format {%Y-%m-%d %H:%M:%S}
 675                ]}
 676
 677                set committer_name {}
 678                set committer_email {}
 679                set committer_time {}
 680                catch {set committer_name $header($cmit,committer)}
 681                catch {set committer_email $header($cmit,committer-mail)}
 682                catch {set committer_time [clock format \
 683                        $header($cmit,committer-time) \
 684                        -format {%Y-%m-%d %H:%M:%S}
 685                ]}
 686
 687                if {[catch {set msg $header($cmit,message)}]} {
 688                        set msg {}
 689                        catch {
 690                                set fd [open "| git cat-file commit $cmit" r]
 691                                fconfigure $fd -encoding binary -translation lf
 692                                if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
 693                                        set enc utf-8
 694                                }
 695                                while {[gets $fd line] > 0} {
 696                                        if {[string match {encoding *} $line]} {
 697                                                set enc [string tolower [string range $line 9 end]]
 698                                        }
 699                                }
 700                                set msg [encoding convertfrom $enc [read $fd]]
 701                                set msg [string trim $msg]
 702                                close $fd
 703
 704                                set author_name [encoding convertfrom $enc $author_name]
 705                                set committer_name [encoding convertfrom $enc $committer_name]
 706
 707                                set header($cmit,author) $author_name
 708                                set header($cmit,committer) $committer_name
 709                        }
 710                        set header($cmit,message) $msg
 711                }
 712
 713                $w_cmit insert end "commit $cmit\n" header_key
 714                $w_cmit insert end "Author:\t" header_key
 715                $w_cmit insert end "$author_name $author_email" header_val
 716                $w_cmit insert end "$author_time\n" header_val
 717
 718                $w_cmit insert end "Committer:\t" header_key
 719                $w_cmit insert end "$committer_name $committer_email" header_val
 720                $w_cmit insert end "$committer_time\n" header_val
 721
 722                if {$line_file($lno) ne $path} {
 723                        $w_cmit insert end "Original File:\t" header_key
 724                        $w_cmit insert end "[escape_path $line_file($lno)]\n" header_val
 725                }
 726
 727                $w_cmit insert end "\n$msg"
 728        }
 729        $w_cmit conf -state disabled
 730
 731        set highlight_line $lno
 732        set highlight_commit $cmit
 733
 734        if {$highlight_commit eq $tooltip_commit} {
 735                _hide_tooltip $this
 736        }
 737}
 738
 739method _copycommit {} {
 740        set pos @$::cursorX,$::cursorY
 741        set lno [lindex [split [$::cursorW index $pos] .] 0]
 742        if {![catch {set commit $line_commit($lno)}]} {
 743                clipboard clear
 744                clipboard append \
 745                        -format STRING \
 746                        -type STRING \
 747                        -- $commit
 748        }
 749}
 750
 751method _show_tooltip {cur_w pos} {
 752        set lno [lindex [split [$cur_w index $pos] .] 0]
 753        if {[catch {set cmit $line_commit($lno)}]} {
 754                _hide_tooltip $this
 755                return
 756        }
 757
 758        if {$cmit eq $highlight_commit} {
 759                _hide_tooltip $this
 760                return
 761        }
 762
 763        if {$cmit eq $tooltip_commit} {
 764                _position_tooltip $this
 765        } elseif {$tooltip_wm ne {}} {
 766                _open_tooltip $this $cur_w
 767        } elseif {$tooltip_timer eq {}} {
 768                set tooltip_timer [after 1000 [cb _open_tooltip $cur_w]]
 769        }
 770}
 771
 772method _open_tooltip {cur_w} {
 773        set tooltip_timer {}
 774        set pos_x [winfo pointerx $cur_w]
 775        set pos_y [winfo pointery $cur_w]
 776        if {[winfo containing $pos_x $pos_y] ne $cur_w} {
 777                _hide_tooltip $this
 778                return
 779        }
 780
 781        set pos @[join [list \
 782                [expr {$pos_x - [winfo rootx $cur_w]}] \
 783                [expr {$pos_y - [winfo rooty $cur_w]}]] ,]
 784        set lno [lindex [split [$cur_w index $pos] .] 0]
 785        set cmit $line_commit($lno)
 786
 787        set author_name {}
 788        set author_email {}
 789        set author_time {}
 790        catch {set author_name $header($cmit,author)}
 791        catch {set author_email $header($cmit,author-mail)}
 792        catch {set author_time [clock format \
 793                $header($cmit,author-time) \
 794                -format {%Y-%m-%d %H:%M:%S}
 795        ]}
 796
 797        set committer_name {}
 798        set committer_email {}
 799        set committer_time {}
 800        catch {set committer_name $header($cmit,committer)}
 801        catch {set committer_email $header($cmit,committer-mail)}
 802        catch {set committer_time [clock format \
 803                $header($cmit,committer-time) \
 804                -format {%Y-%m-%d %H:%M:%S}
 805        ]}
 806
 807        set summary {}
 808        catch {set summary $header($cmit,summary)}
 809
 810        set tooltip_commit $cmit
 811        set tooltip_text "commit $cmit
 812$author_name $author_email  $author_time
 813$summary"
 814
 815        set file $line_file($lno)
 816        if {$file ne $path} {
 817                append tooltip_text "
 818
 819Original File: $file"
 820        }
 821
 822        if {$tooltip_wm ne "$cur_w.tooltip"} {
 823                _hide_tooltip $this
 824
 825                set tooltip_wm [toplevel $cur_w.tooltip -borderwidth 1]
 826                wm overrideredirect $tooltip_wm 1
 827                wm transient $tooltip_wm [winfo toplevel $cur_w]
 828                pack [label $tooltip_wm.label \
 829                        -background lightyellow \
 830                        -foreground black \
 831                        -textvariable @tooltip_text \
 832                        -justify left]
 833        }
 834        _position_tooltip $this
 835}
 836
 837method _position_tooltip {} {
 838        set req_w [winfo reqwidth  $tooltip_wm.label]
 839        set req_h [winfo reqheight $tooltip_wm.label]
 840        set pos_x [expr {[winfo pointerx .] +  5}]
 841        set pos_y [expr {[winfo pointery .] + 10}]
 842
 843        set g "${req_w}x${req_h}"
 844        if {$pos_x >= 0} {append g +}
 845        append g $pos_x
 846        if {$pos_y >= 0} {append g +}
 847        append g $pos_y
 848
 849        wm geometry $tooltip_wm $g
 850        raise $tooltip_wm
 851}
 852
 853method _hide_tooltip {} {
 854        if {$tooltip_wm ne {}} {
 855                destroy $tooltip_wm
 856                set tooltip_wm {}
 857                set tooltip_commit {}
 858        }
 859        if {$tooltip_timer ne {}} {
 860                after cancel $tooltip_timer
 861                set tooltip_timer {}
 862        }
 863}
 864
 865method _resize {new_height} {
 866        set diff [expr {$new_height - $old_height}]
 867        if {$diff == 0} return
 868
 869        set my [expr {[winfo height $w.file_pane] - 25}]
 870        set o [$w.file_pane sash coord 0]
 871        set ox [lindex $o 0]
 872        set oy [expr {[lindex $o 1] + $diff}]
 873        if {$oy < 0}   {set oy 0}
 874        if {$oy > $my} {set oy $my}
 875        $w.file_pane sash place 0 $ox $oy
 876
 877        set old_height $new_height
 878}
 879
 880}