2f7503ad6b099f0b0938aa44d5c94a2f63eddc40
   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                }
 394
 395                $m add command -label $t -command [cb _goback $i $c $f]
 396        }
 397        set X [winfo rootx $w_back]
 398        set Y [expr {[winfo rooty $w_back] + [winfo height $w_back]}]
 399        tk_popup $m $X $Y
 400}
 401
 402method _goback {i c f} {
 403        set history [lrange $history 0 [expr {$i - 1}]]
 404        set commit $c
 405        set path $f
 406        _load $this
 407}
 408
 409method _read_file {fd} {
 410        if {$fd ne $current_fd} {
 411                catch {close $fd}
 412                return
 413        }
 414
 415        $w_load conf -state normal
 416        $w_cgrp conf -state normal
 417        $w_line conf -state normal
 418        $w_file conf -state normal
 419        while {[gets $fd line] >= 0} {
 420                regsub "\r\$" $line {} line
 421                incr total_lines
 422
 423                if {$total_lines > 1} {
 424                        $w_load insert end "\n"
 425                        $w_cgrp insert end "\n"
 426                        $w_line insert end "\n"
 427                        $w_file insert end "\n"
 428                }
 429
 430                $w_line insert end "$total_lines" linenumber
 431                $w_file insert end "$line"
 432        }
 433        $w_load conf -state disabled
 434        $w_cgrp conf -state disabled
 435        $w_line conf -state disabled
 436        $w_file conf -state disabled
 437
 438        if {[eof $fd]} {
 439                close $fd
 440                _status $this
 441                set cmd {nice git blame -M -C --incremental}
 442                if {$commit eq {}} {
 443                        lappend cmd --contents $path
 444                } else {
 445                        lappend cmd $commit
 446                }
 447                lappend cmd -- $path
 448                set fd [open "| $cmd" r]
 449                fconfigure $fd -blocking 0 -translation lf -encoding binary
 450                fileevent $fd readable [cb _read_blame $fd]
 451                set current_fd $fd
 452        }
 453} ifdeleted { catch {close $fd} }
 454
 455method _read_blame {fd} {
 456        variable group_colors
 457
 458        if {$fd ne $current_fd} {
 459                catch {close $fd}
 460                return
 461        }
 462
 463        $w_cgrp conf -state normal
 464        while {[gets $fd line] >= 0} {
 465                if {[regexp {^([a-z0-9]{40}) (\d+) (\d+) (\d+)$} $line line \
 466                        cmit original_line final_line line_count]} {
 467                        set r_commit     $cmit
 468                        set r_orig_line  $original_line
 469                        set r_final_line $final_line
 470                        set r_line_count $line_count
 471
 472                        if {[catch {set g $order($cmit)}]} {
 473                                set bg [lindex $group_colors 0]
 474                                set group_colors [lrange $group_colors 1 end]
 475                                lappend group_colors $bg
 476
 477                                $w_cgrp tag conf g$cmit -background $bg
 478                                $w_line tag conf g$cmit -background $bg
 479                                $w_file tag conf g$cmit -background $bg
 480
 481                                set order($cmit) $commit_count
 482                                incr commit_count
 483                                lappend commit_list $cmit
 484                        }
 485                } elseif {[string match {filename *} $line]} {
 486                        set file [string range $line 9 end]
 487                        set n    $r_line_count
 488                        set lno  $r_final_line
 489                        set cmit $r_commit
 490
 491                        if {[regexp {^0{40}$} $cmit]} {
 492                                set commit_abbr work
 493                                set commit_type curr_commit
 494                        } elseif {$cmit eq $commit} {
 495                                set commit_abbr this
 496                                set commit_type curr_commit
 497                        } else {
 498                                set commit_type prior_commit
 499                                set commit_abbr [string range $cmit 0 4]
 500                        }
 501
 502                        set author_abbr {}
 503                        set a_name {}
 504                        catch {set a_name $header($cmit,author)}
 505                        while {$a_name ne {}} {
 506                                if {![regexp {^([[:upper:]])} $a_name _a]} break
 507                                append author_abbr $_a
 508                                unset _a
 509                                if {![regsub \
 510                                        {^[[:upper:]][^\s]*\s+} \
 511                                        $a_name {} a_name ]} break
 512                        }
 513                        if {$author_abbr eq {}} {
 514                                set author_abbr { |}
 515                        } else {
 516                                set author_abbr [string range $author_abbr 0 3]
 517                                while {[string length $author_abbr] < 4} {
 518                                        set author_abbr " $author_abbr"
 519                                }
 520                        }
 521                        unset a_name
 522
 523                        set first_lno $lno
 524                        while {
 525                           ![catch {set ncmit $line_commit([expr {$first_lno - 1}])}]
 526                        && ![catch {set nfile $line_file([expr {$first_lno - 1}])}]
 527                        && $ncmit eq $cmit
 528                        && $nfile eq $file
 529                        } {
 530                                incr first_lno -1
 531                        }
 532
 533                        while {$n > 0} {
 534                                set lno_e "$lno.0 lineend + 1c"
 535                                if {[catch {set g g$line_commit($lno)}]} {
 536                                        $w_load tag add annotated $lno.0 $lno_e
 537                                } else {
 538                                        $w_cgrp tag remove g$g $lno.0 $lno_e
 539                                        $w_line tag remove g$g $lno.0 $lno_e
 540                                        $w_file tag remove g$g $lno.0 $lno_e
 541
 542                                        $w_cgrp tag remove a$g $lno.0 $lno_e
 543                                        $w_line tag remove a$g $lno.0 $lno_e
 544                                        $w_file tag remove a$g $lno.0 $lno_e
 545                                }
 546
 547                                set line_commit($lno) $cmit
 548                                set line_file($lno)   $file
 549
 550                                $w_cgrp delete $lno.0 "$lno.0 lineend"
 551                                if {$lno == $first_lno} {
 552                                        $w_cgrp insert $lno.0 $commit_abbr $commit_type
 553                                } elseif {$lno == [expr {$first_lno + 1}]} {
 554                                        $w_cgrp insert $lno.0 $author_abbr
 555                                } else {
 556                                        $w_cgrp insert $lno.0 { |}
 557                                }
 558
 559                                $w_cgrp tag add g$cmit $lno.0 $lno_e
 560                                $w_line tag add g$cmit $lno.0 $lno_e
 561                                $w_file tag add g$cmit $lno.0 $lno_e
 562
 563                                $w_cgrp tag add a$cmit $lno.0 $lno_e
 564                                $w_line tag add a$cmit $lno.0 $lno_e
 565                                $w_file tag add a$cmit $lno.0 $lno_e
 566
 567                                if {$highlight_line == -1} {
 568                                        if {[lindex [$w_file yview] 0] == 0} {
 569                                                $w_file see $lno.0
 570                                                _showcommit $this $lno
 571                                        }
 572                                } elseif {$highlight_line == $lno} {
 573                                        _showcommit $this $lno
 574                                }
 575
 576                                incr n -1
 577                                incr lno
 578                                incr blame_lines
 579                        }
 580
 581                        while {
 582                           ![catch {set ncmit $line_commit($lno)}]
 583                        && ![catch {set nfile $line_file($lno)}]
 584                        && $ncmit eq $cmit
 585                        && $nfile eq $file
 586                        } {
 587                                $w_cgrp delete $lno.0 "$lno.0 lineend"
 588
 589                                if {$lno == $first_lno} {
 590                                        $w_cgrp insert $lno.0 $commit_abbr $commit_type
 591                                } elseif {$lno == [expr {$first_lno + 1}]} {
 592                                        $w_cgrp insert $lno.0 $author_abbr
 593                                } else {
 594                                        $w_cgrp insert $lno.0 { |}
 595                                }
 596                                incr lno
 597                        }
 598
 599                } elseif {[regexp {^([a-z-]+) (.*)$} $line line key data]} {
 600                        set header($r_commit,$key) $data
 601                }
 602        }
 603        $w_cgrp conf -state disabled
 604
 605        if {[eof $fd]} {
 606                close $fd
 607                set current_fd {}
 608                set status {Annotation complete.}
 609                destroy $w.status.c
 610        } else {
 611                _status $this
 612        }
 613} ifdeleted { catch {close $fd} }
 614
 615method _status {} {
 616        set have  $blame_lines
 617        set total $total_lines
 618        set pdone 0
 619        if {$total} {set pdone [expr {100 * $have / $total}]}
 620
 621        set status [format \
 622                "Loading annotations... %i of %i lines annotated (%2i%%)" \
 623                $have $total $pdone]
 624        $w.status.c coords bar 0 0 $pdone 20
 625}
 626
 627method _click {cur_w pos} {
 628        set lno [lindex [split [$cur_w index $pos] .] 0]
 629        if {$lno eq {}} return
 630        _showcommit $this $lno
 631}
 632
 633method _load_commit {pos} {
 634        set lno [lindex [split [$w_cgrp index $pos] .] 0]
 635        if {[catch {set cmit $line_commit($lno)}]} return
 636        if {[catch {set file $line_file($lno)  }]} return
 637
 638        set commit $cmit
 639        set path $file
 640        _load $this
 641}
 642
 643method _showcommit {lno} {
 644        global repo_config
 645        variable active_color
 646
 647        if {$highlight_commit ne {}} {
 648                set cmit $highlight_commit
 649                $w_cgrp tag conf a$cmit -background {}
 650                $w_line tag conf a$cmit -background {}
 651                $w_file tag conf a$cmit -background {}
 652        }
 653
 654        $w_cmit conf -state normal
 655        $w_cmit delete 0.0 end
 656        if {[catch {set cmit $line_commit($lno)}]} {
 657                set cmit {}
 658                $w_cmit insert end "Loading annotation..."
 659        } else {
 660                $w_cgrp tag conf a$cmit -background $active_color
 661                $w_line tag conf a$cmit -background $active_color
 662                $w_file tag conf a$cmit -background $active_color
 663
 664                set author_name {}
 665                set author_email {}
 666                set author_time {}
 667                catch {set author_name $header($cmit,author)}
 668                catch {set author_email $header($cmit,author-mail)}
 669                catch {set author_time [clock format \
 670                        $header($cmit,author-time) \
 671                        -format {%Y-%m-%d %H:%M:%S}
 672                ]}
 673
 674                set committer_name {}
 675                set committer_email {}
 676                set committer_time {}
 677                catch {set committer_name $header($cmit,committer)}
 678                catch {set committer_email $header($cmit,committer-mail)}
 679                catch {set committer_time [clock format \
 680                        $header($cmit,committer-time) \
 681                        -format {%Y-%m-%d %H:%M:%S}
 682                ]}
 683
 684                if {[catch {set msg $header($cmit,message)}]} {
 685                        set msg {}
 686                        catch {
 687                                set fd [open "| git cat-file commit $cmit" r]
 688                                fconfigure $fd -encoding binary -translation lf
 689                                if {[catch {set enc $repo_config(i18n.commitencoding)}]} {
 690                                        set enc utf-8
 691                                }
 692                                while {[gets $fd line] > 0} {
 693                                        if {[string match {encoding *} $line]} {
 694                                                set enc [string tolower [string range $line 9 end]]
 695                                        }
 696                                }
 697                                set msg [encoding convertfrom $enc [read $fd]]
 698                                set msg [string trim $msg]
 699                                close $fd
 700
 701                                set author_name [encoding convertfrom $enc $author_name]
 702                                set committer_name [encoding convertfrom $enc $committer_name]
 703
 704                                set header($cmit,author) $author_name
 705                                set header($cmit,committer) $committer_name
 706                        }
 707                        set header($cmit,message) $msg
 708                }
 709
 710                $w_cmit insert end "commit $cmit\n" header_key
 711                $w_cmit insert end "Author:\t" header_key
 712                $w_cmit insert end "$author_name $author_email" header_val
 713                $w_cmit insert end "$author_time\n" header_val
 714
 715                $w_cmit insert end "Committer:\t" header_key
 716                $w_cmit insert end "$committer_name $committer_email" header_val
 717                $w_cmit insert end "$committer_time\n" header_val
 718
 719                if {$line_file($lno) ne $path} {
 720                        $w_cmit insert end "Original File:\t" header_key
 721                        $w_cmit insert end "[escape_path $line_file($lno)]\n" header_val
 722                }
 723
 724                $w_cmit insert end "\n$msg"
 725        }
 726        $w_cmit conf -state disabled
 727
 728        set highlight_line $lno
 729        set highlight_commit $cmit
 730
 731        if {$highlight_commit eq $tooltip_commit} {
 732                _hide_tooltip $this
 733        }
 734}
 735
 736method _copycommit {} {
 737        set pos @$::cursorX,$::cursorY
 738        set lno [lindex [split [$::cursorW index $pos] .] 0]
 739        if {![catch {set commit $line_commit($lno)}]} {
 740                clipboard clear
 741                clipboard append \
 742                        -format STRING \
 743                        -type STRING \
 744                        -- $commit
 745        }
 746}
 747
 748method _show_tooltip {cur_w pos} {
 749        set lno [lindex [split [$cur_w index $pos] .] 0]
 750        if {[catch {set cmit $line_commit($lno)}]} {
 751                _hide_tooltip $this
 752                return
 753        }
 754
 755        if {$cmit eq $highlight_commit} {
 756                _hide_tooltip $this
 757                return
 758        }
 759
 760        if {$cmit eq $tooltip_commit} {
 761                _position_tooltip $this
 762        } elseif {$tooltip_wm ne {}} {
 763                _open_tooltip $this $cur_w
 764        } elseif {$tooltip_timer eq {}} {
 765                set tooltip_timer [after 1000 [cb _open_tooltip $cur_w]]
 766        }
 767}
 768
 769method _open_tooltip {cur_w} {
 770        set tooltip_timer {}
 771        set pos_x [winfo pointerx $cur_w]
 772        set pos_y [winfo pointery $cur_w]
 773        if {[winfo containing $pos_x $pos_y] ne $cur_w} {
 774                _hide_tooltip $this
 775                return
 776        }
 777
 778        set pos @[join [list \
 779                [expr {$pos_x - [winfo rootx $cur_w]}] \
 780                [expr {$pos_y - [winfo rooty $cur_w]}]] ,]
 781        set lno [lindex [split [$cur_w index $pos] .] 0]
 782        set cmit $line_commit($lno)
 783
 784        set author_name {}
 785        set author_email {}
 786        set author_time {}
 787        catch {set author_name $header($cmit,author)}
 788        catch {set author_email $header($cmit,author-mail)}
 789        catch {set author_time [clock format \
 790                $header($cmit,author-time) \
 791                -format {%Y-%m-%d %H:%M:%S}
 792        ]}
 793
 794        set committer_name {}
 795        set committer_email {}
 796        set committer_time {}
 797        catch {set committer_name $header($cmit,committer)}
 798        catch {set committer_email $header($cmit,committer-mail)}
 799        catch {set committer_time [clock format \
 800                $header($cmit,committer-time) \
 801                -format {%Y-%m-%d %H:%M:%S}
 802        ]}
 803
 804        set summary {}
 805        catch {set summary $header($cmit,summary)}
 806
 807        set tooltip_commit $cmit
 808        set tooltip_text "commit $cmit
 809$author_name $author_email  $author_time
 810$summary"
 811
 812        set file $line_file($lno)
 813        if {$file ne $path} {
 814                append tooltip_text "
 815
 816Original File: $file"
 817        }
 818
 819        if {$tooltip_wm ne "$cur_w.tooltip"} {
 820                _hide_tooltip $this
 821
 822                set tooltip_wm [toplevel $cur_w.tooltip -borderwidth 1]
 823                wm overrideredirect $tooltip_wm 1
 824                wm transient $tooltip_wm [winfo toplevel $cur_w]
 825                pack [label $tooltip_wm.label \
 826                        -background lightyellow \
 827                        -foreground black \
 828                        -textvariable @tooltip_text \
 829                        -justify left]
 830        }
 831        _position_tooltip $this
 832}
 833
 834method _position_tooltip {} {
 835        set req_w [winfo reqwidth  $tooltip_wm.label]
 836        set req_h [winfo reqheight $tooltip_wm.label]
 837        set pos_x [expr {[winfo pointerx .] +  5}]
 838        set pos_y [expr {[winfo pointery .] + 10}]
 839
 840        set g "${req_w}x${req_h}"
 841        if {$pos_x >= 0} {append g +}
 842        append g $pos_x
 843        if {$pos_y >= 0} {append g +}
 844        append g $pos_y
 845
 846        wm geometry $tooltip_wm $g
 847        raise $tooltip_wm
 848}
 849
 850method _hide_tooltip {} {
 851        if {$tooltip_wm ne {}} {
 852                destroy $tooltip_wm
 853                set tooltip_wm {}
 854                set tooltip_commit {}
 855        }
 856        if {$tooltip_timer ne {}} {
 857                after cancel $tooltip_timer
 858                set tooltip_timer {}
 859        }
 860}
 861
 862method _resize {new_height} {
 863        set diff [expr {$new_height - $old_height}]
 864        if {$diff == 0} return
 865
 866        set my [expr {[winfo height $w.file_pane] - 25}]
 867        set o [$w.file_pane sash coord 0]
 868        set ox [lindex $o 0]
 869        set oy [expr {[lindex $o 1] + $diff}]
 870        if {$oy < 0}   {set oy 0}
 871        if {$oy > $my} {set oy $my}
 872        $w.file_pane sash place 0 $ox $oy
 873
 874        set old_height $new_height
 875}
 876
 877}