git-gui: fix exception when trying to stage with empty file list
[gitweb.git] / git-gui.sh
index 5d399555e6f43b1b9d622a486cf6def6b7ae24ac..b3c14acbdf740b2073954bdc8ef49496db228b65 100755 (executable)
@@ -10,8 +10,8 @@
  exec wish "$argv0" -- "$@"
 
 set appvers {@@GITGUI_VERSION@@}
-set copyright [encoding convertfrom utf-8 {
-Copyright © 2006, 2007 Shawn Pearce, et. al.
+set copyright [string map [list (c) \u00a9] {
+Copyright (c) 2006-2010 Shawn Pearce, et. al.
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -38,7 +38,7 @@ if {[catch {package require Tcl 8.4} err]
        tk_messageBox \
                -icon error \
                -type ok \
-               -title [mc "git-gui: fatal error"] \
+               -title "git-gui: fatal error" \
                -message $err
        exit 1
 }
@@ -49,7 +49,11 @@ catch {rename send {}} ; # What an evil concept...
 ##
 ## locate our library
 
-set oguilib {@@GITGUI_LIBDIR@@}
+if { [info exists ::env(GIT_GUI_LIB_DIR) ] } {
+       set oguilib $::env(GIT_GUI_LIB_DIR)
+} else {
+       set oguilib {@@GITGUI_LIBDIR@@}
+}
 set oguirel {@@GITGUI_RELATIVE@@}
 if {$oguirel eq {1}} {
        set oguilib [file dirname [file normalize $argv0]]
@@ -79,10 +83,11 @@ if {![catch {set _verbose $env(GITGUI_VERBOSE)}]} {
                return [uplevel 1 real__auto_load $name $args]
        }
        rename source real__source
-       proc source {name} {
-               puts stderr "source    $name"
-               uplevel 1 real__source $name
+       proc source {args} {
+               puts stderr "source    $args"
+               uplevel 1 [linsert $args 0 real__source]
        }
+       if {[tk windowingsystem] eq "win32"} { console show }
 }
 
 ######################################################################
@@ -92,6 +97,25 @@ if {![catch {set _verbose $env(GITGUI_VERBOSE)}]} {
 
 package require msgcat
 
+# Check for Windows 7 MUI language pack (missed by msgcat < 1.4.4)
+if {[tk windowingsystem] eq "win32"
+       && [package vcompare [package provide msgcat] 1.4.4] < 0
+} then {
+       proc _mc_update_locale {} {
+               set key {HKEY_CURRENT_USER\Control Panel\Desktop}
+               if {![catch {
+                       package require registry
+                       set uilocale [registry get $key "PreferredUILanguages"]
+                       msgcat::ConvertLocale [string map {- _} [lindex $uilocale 0]]
+               } uilocale]} {
+                       if {[string length $uilocale] > 0} {
+                               msgcat::mclocale $uilocale
+                       }
+               }
+       }
+       _mc_update_locale
+}
+
 proc _mc_trim {fmt} {
        set cmk [string first @@ $fmt]
        if {$cmk > 0} {
@@ -115,6 +139,20 @@ proc strcat {args} {
 ::msgcat::mcload $oguimsg
 unset oguimsg
 
+######################################################################
+##
+## On Mac, bring the current Wish process window to front
+
+if {[tk windowingsystem] eq "aqua"} {
+       catch {
+               exec osascript -e [format {
+                       tell application "System Events"
+                               set frontmost of processes whose unix id is %d to true
+                       end tell
+               } [pid]]
+       }
+}
+
 ######################################################################
 ##
 ## read only globals
@@ -128,15 +166,33 @@ set _githtmldir {}
 set _reponame {}
 set _iscygwin {}
 set _search_path {}
+set _shellpath {@@SHELL_PATH@@}
 
 set _trace [lsearch -exact $argv --trace]
 if {$_trace >= 0} {
        set argv [lreplace $argv $_trace $_trace]
        set _trace 1
+       if {[tk windowingsystem] eq "win32"} { console show }
 } else {
        set _trace 0
 }
 
+# variable for the last merged branch (useful for a default when deleting
+# branches).
+set _last_merged_branch {}
+
+proc shellpath {} {
+       global _shellpath env
+       if {[string match @@* $_shellpath]} {
+               if {[info exists env(SHELL)]} {
+                       return $env(SHELL)
+               } else {
+                       return /bin/sh
+               }
+       }
+       return $_shellpath
+}
+
 proc appname {} {
        global _appname
        return $_appname
@@ -219,6 +275,10 @@ proc is_Cygwin {} {
                                set _iscygwin 0
                        } else {
                                set _iscygwin 1
+                               # Handle MSys2 which is only cygwin when MSYSTEM is MSYS.
+                               if {[info exists ::env(MSYSTEM)] && $::env(MSYSTEM) ne "MSYS"} {
+                                       set _iscygwin 0
+                               }
                        }
                } else {
                        set _iscygwin 0
@@ -262,7 +322,22 @@ proc is_config_true {name} {
        global repo_config
        if {[catch {set v $repo_config($name)}]} {
                return 0
-       } elseif {$v eq {true} || $v eq {1} || $v eq {yes}} {
+       }
+       set v [string tolower $v]
+       if {$v eq {} || $v eq {true} || $v eq {1} || $v eq {yes} || $v eq {on}} {
+               return 1
+       } else {
+               return 0
+       }
+}
+
+proc is_config_false {name} {
+       global repo_config
+       if {[catch {set v $repo_config($name)}]} {
+               return 0
+       }
+       set v [string tolower $v]
+       if {$v eq {false} || $v eq {0} || $v eq {no} || $v eq {off}} {
                return 1
        } else {
                return 0
@@ -323,6 +398,8 @@ proc _trace_exec {cmd} {
        puts stderr $d
 }
 
+#'"  fix poor old emacs font-lock mode
+
 proc _git_cmd {name} {
        global _git_cmd_path
 
@@ -410,12 +487,46 @@ proc _which {what args} {
        return {}
 }
 
+# Test a file for a hashbang to identify executable scripts on Windows.
+proc is_shellscript {filename} {
+       if {![file exists $filename]} {return 0}
+       set f [open $filename r]
+       fconfigure $f -encoding binary
+       set magic [read $f 2]
+       close $f
+       return [expr {$magic eq "#!"}]
+}
+
+# Run a command connected via pipes on stdout.
+# This is for use with textconv filters and uses sh -c "..." to allow it to
+# contain a command with arguments. On windows we must check for shell
+# scripts specifically otherwise just call the filter command.
+proc open_cmd_pipe {cmd path} {
+       global env
+       if {![file executable [shellpath]]} {
+               set exe [auto_execok [lindex $cmd 0]]
+               if {[is_shellscript [lindex $exe 0]]} {
+                       set run [linsert [auto_execok sh] end -c "$cmd \"\$0\"" $path]
+               } else {
+                       set run [concat $exe [lrange $cmd 1 end] $path]
+               }
+       } else {
+               set run [list [shellpath] -c "$cmd \"\$0\"" $path]
+       }
+       return [open |$run r]
+}
+
 proc _lappend_nice {cmd_var} {
        global _nice
        upvar $cmd_var cmd
 
        if {![info exists _nice]} {
                set _nice [_which nice]
+               if {[catch {exec $_nice git version}]} {
+                       set _nice {}
+               } elseif {[is_Windows] && [file dirname $_nice] ne [file dirname $::_git]} {
+                       set _nice {}
+               }
        }
        if {$_nice ne {}} {
                lappend cmd $_nice
@@ -423,28 +534,10 @@ proc _lappend_nice {cmd_var} {
 }
 
 proc git {args} {
-       set opt [list]
-
-       while {1} {
-               switch -- [lindex $args 0] {
-               --nice {
-                       _lappend_nice opt
-               }
-
-               default {
-                       break
-               }
-
-               }
-
-               set args [lrange $args 1 end]
-       }
-
-       set cmdp [_git_cmd [lindex $args 0]]
-       set args [lrange $args 1 end]
-
-       _trace_exec [concat $opt $cmdp $args]
-       set result [eval exec $opt $cmdp $args]
+       set fd [eval [list git_read] $args]
+       fconfigure $fd -translation binary -encoding utf-8
+       set result [string trimright [read $fd] "\n"]
+       close $fd
        if {$::_trace} {
                puts stderr "< $result"
        }
@@ -563,9 +656,7 @@ proc kill_file_process {fd} {
 
        catch {
                if {[is_Windows]} {
-                       # Use a Cygwin-specific flag to allow killing
-                       # native Windows processes
-                       exec kill -f $process
+                       exec taskkill /pid $process
                } else {
                        exec kill $process
                }
@@ -634,6 +725,7 @@ proc rmsel_tag {text} {
        return $text
 }
 
+wm withdraw .
 set root_exists 0
 bind . <Visibility> {
        bind . <Visibility> {}
@@ -643,6 +735,7 @@ bind . <Visibility> {
 if {[is_Windows]} {
        wm iconbitmap . -default $oguilib/git-gui.ico
        set ::tk::AlwaysShowSelection 1
+       bind . <Control-F2> {console show}
 
        # Spoof an X11 display for SSH
        if {![info exists env(DISPLAY)]} {
@@ -668,7 +761,10 @@ if {[is_Windows]} {
                gitlogo put gray26  -to  5 15 11 16
                gitlogo redither
 
-               wm iconphoto . -default gitlogo
+               image create photo gitlogo32 -width 32 -height 32
+               gitlogo32 copy gitlogo -zoom 2 2
+
+               wm iconphoto . -default gitlogo gitlogo32
        }
 }
 
@@ -677,12 +773,17 @@ if {[is_Windows]} {
 ## config defaults
 
 set cursor_ptr arrow
-font create font_diff -family Courier -size 10
 font create font_ui
-catch {
-       label .dummy
-       eval font configure font_ui [font actual [.dummy cget -font]]
-       destroy .dummy
+if {[lsearch -exact [font names] TkDefaultFont] != -1} {
+       eval [linsert [font actual TkDefaultFont] 0 font configure font_ui]
+       eval [linsert [font actual TkFixedFont] 0 font create font_diff]
+} else {
+       font create font_diff -family Courier -size 10
+       catch {
+               label .dummy
+               eval font configure font_ui [font actual [.dummy cget -font]]
+               destroy .dummy
+       }
 }
 
 font create font_uiitalic
@@ -697,6 +798,9 @@ foreach class {Button Checkbutton Entry Label
 }
 if {![is_MacOSX]} {
        option add *Menu.font font_ui
+       option add *Entry.borderWidth 1 startupFile
+       option add *Entry.relief sunken startupFile
+       option add *RadioButton.anchor w startupFile
 }
 unset class
 
@@ -749,6 +853,18 @@ proc apply_config {} {
                font configure ${font}bold -weight bold
                font configure ${font}italic -slant italic
        }
+
+       global use_ttk NS
+       set use_ttk 0
+       set NS {}
+       if {$repo_config(gui.usettk)} {
+               set use_ttk [package vsatisfies [package provide Tk] 8.5]
+               if {$use_ttk} {
+                       set NS ttk
+                       bind [winfo class .] <<ThemeChanged>> [list InitTheme]
+                       pave_toplevel .
+               }
+       }
 }
 
 set default_config(branch.autosetupmerge) true
@@ -762,12 +878,15 @@ set default_config(user.email) {}
 
 set default_config(gui.encoding) [encoding system]
 set default_config(gui.matchtrackingbranch) false
+set default_config(gui.textconv) true
 set default_config(gui.pruneduringfetch) false
 set default_config(gui.trustmtime) false
 set default_config(gui.fastcopyblame) false
+set default_config(gui.maxrecentrepo) 10
 set default_config(gui.copyblamethreshold) 40
 set default_config(gui.blamehistoryctx) 7
 set default_config(gui.diffcontext) 5
+set default_config(gui.diffopts) {}
 set default_config(gui.commitmsgwidth) 75
 set default_config(gui.newbranchtemplate) {}
 set default_config(gui.spellingdictionary) {}
@@ -775,10 +894,15 @@ set default_config(gui.fontui) [font configure font_ui]
 set default_config(gui.fontdiff) [font configure font_diff]
 # TODO: this option should be added to the git-config documentation
 set default_config(gui.maxfilesdisplayed) 5000
+set default_config(gui.usettk) 1
+set default_config(gui.warndetachedcommit) 1
+set default_config(gui.tabsize) 8
 set font_descs {
        {fontui   font_ui   {mc "Main Font"}}
        {fontdiff font_diff {mc "Diff/Console Font"}}
 }
+set default_config(gui.stageuntracked) ask
+set default_config(gui.displayuntracked) true
 
 ######################################################################
 ##
@@ -822,12 +946,19 @@ if {![regsub {^git version } $_git_version {} _git_version]} {
        exit 1
 }
 
+proc get_trimmed_version {s} {
+    set r {}
+    foreach x [split $s -._] {
+        if {[string is integer -strict $x]} {
+            lappend r $x
+        } else {
+            break
+        }
+    }
+    return [join $r .]
+}
 set _real_git_version $_git_version
-regsub -- {[\-\.]dirty$} $_git_version {} _git_version
-regsub {\.[0-9]+\.g[0-9a-f]+$} $_git_version {} _git_version
-regsub {\.[a-zA-Z]+\.?[0-9]+$} $_git_version {} _git_version
-regsub {\.GIT$} $_git_version {} _git_version
-regsub {\.[a-zA-Z]+\.?[0-9]+$} $_git_version {} _git_version
+set _git_version [get_trimmed_version $_git_version]
 
 if {![regexp {^[1-9]+(\.[0-9]+)+$} $_git_version]} {
        catch {wm withdraw .}
@@ -962,7 +1093,7 @@ git-version proc _parse_config {arr_name args} {
                                [list git_read config] \
                                $args \
                                [list --null --list]]
-                       fconfigure $fd_rc -translation binary
+                       fconfigure $fd_rc -translation binary -encoding utf-8
                        set buf [read $fd_rc]
                        close $fd_rc
                }
@@ -973,6 +1104,10 @@ git-version proc _parse_config {arr_name args} {
                                } else {
                                        set arr($name) $value
                                }
+                       } elseif {[regexp {^([^\n]+)$} $line line name]} {
+                               # no value given, but interpreting them as
+                               # boolean will be handled as true
+                               set arr($name) {}
                        }
                }
        }
@@ -988,6 +1123,10 @@ git-version proc _parse_config {arr_name args} {
                                        } else {
                                                set arr($name) $value
                                        }
+                               } elseif {[regexp {^([^=]+)$} $line line name]} {
+                                       # no value given, but interpreting them as
+                                       # boolean will be handled as true
+                                       set arr($name) {}
                                }
                        }
                        close $fd_rc
@@ -1131,10 +1270,26 @@ if {![file isdirectory $_gitdir]} {
 # _gitdir exists, so try loading the config
 load_config 0
 apply_config
-# try to set work tree from environment, falling back to core.worktree
-if {[catch { set _gitworktree $env(GIT_WORK_TREE) }]} {
-       set _gitworktree [get_config core.worktree]
+
+# v1.7.0 introduced --show-toplevel to return the canonical work-tree
+if {[package vcompare $_git_version 1.7.0] >= 0} {
+       if { [is_Cygwin] } {
+               catch {set _gitworktree [exec cygpath --windows [git rev-parse --show-toplevel]]}
+       } else {
+               set _gitworktree [git rev-parse --show-toplevel]
+       }
+} else {
+       # try to set work tree from environment, core.worktree or use
+       # cdup to obtain a relative path to the top of the worktree. If
+       # run from the top, the ./ prefix ensures normalize expands pwd.
+       if {[catch { set _gitworktree $env(GIT_WORK_TREE) }]} {
+               set _gitworktree [get_config core.worktree]
+               if {$_gitworktree eq ""} {
+                       set _gitworktree [file normalize ./[git rev-parse --show-cdup]]
+               }
+       }
 }
+
 if {$_prefix ne {}} {
        if {$_gitworktree eq {}} {
                regsub -all {[^/]+/} $_prefix ../ cdup
@@ -1171,6 +1326,9 @@ if {[lindex $_reponame end] eq {.git}} {
        set _reponame [lindex $_reponame end]
 }
 
+set env(GIT_DIR) $_gitdir
+set env(GIT_WORK_TREE) $_gitworktree
+
 ######################################################################
 ##
 ## global init
@@ -1315,7 +1473,7 @@ proc rescan {after {honor_trustmtime 1}} {
                (![$ui_comm edit modified]
                || [string trim [$ui_comm get 0.0 end]] eq {})} {
                if {[string match amend* $commit_type]} {
-               } elseif {[load_message GITGUI_MSG]} {
+               } elseif {[load_message GITGUI_MSG utf-8]} {
                } elseif {[run_prepare_commit_msg_hook]} {
                } elseif {[load_message MERGE_MSG]} {
                } elseif {[load_message SQUASH_MSG]} {
@@ -1370,34 +1528,47 @@ proc rescan_stage2 {fd after} {
                close $fd
        }
 
-       set ls_others [list --exclude-per-directory=.gitignore]
-       if {[have_info_exclude]} {
-               lappend ls_others "--exclude-from=[gitdir info exclude]"
-       }
-       set user_exclude [get_config core.excludesfile]
-       if {$user_exclude ne {} && [file readable $user_exclude]} {
-               lappend ls_others "--exclude-from=$user_exclude"
+       if {[package vcompare $::_git_version 1.6.3] >= 0} {
+               set ls_others [list --exclude-standard]
+       } else {
+               set ls_others [list --exclude-per-directory=.gitignore]
+               if {[have_info_exclude]} {
+                       lappend ls_others "--exclude-from=[gitdir info exclude]"
+               }
+               set user_exclude [get_config core.excludesfile]
+               if {$user_exclude ne {} && [file readable $user_exclude]} {
+                       lappend ls_others "--exclude-from=[file normalize $user_exclude]"
+               }
        }
 
        set buf_rdi {}
        set buf_rdf {}
        set buf_rlo {}
 
-       set rescan_active 3
+       set rescan_active 2
        ui_status [mc "Scanning for modified files ..."]
-       set fd_di [git_read diff-index --cached -z [PARENT]]
+       if {[git-version >= "1.7.2"]} {
+               set fd_di [git_read diff-index --cached --ignore-submodules=dirty -z [PARENT]]
+       } else {
+               set fd_di [git_read diff-index --cached -z [PARENT]]
+       }
        set fd_df [git_read diff-files -z]
-       set fd_lo [eval git_read ls-files --others -z $ls_others]
 
        fconfigure $fd_di -blocking 0 -translation binary -encoding binary
        fconfigure $fd_df -blocking 0 -translation binary -encoding binary
-       fconfigure $fd_lo -blocking 0 -translation binary -encoding binary
+
        fileevent $fd_di readable [list read_diff_index $fd_di $after]
        fileevent $fd_df readable [list read_diff_files $fd_df $after]
-       fileevent $fd_lo readable [list read_ls_others $fd_lo $after]
+
+       if {[is_config_true gui.displayuntracked]} {
+               set fd_lo [eval git_read ls-files --others -z $ls_others]
+               fconfigure $fd_lo -blocking 0 -translation binary -encoding binary
+               fileevent $fd_lo readable [list read_ls_others $fd_lo $after]
+               incr rescan_active
+       }
 }
 
-proc load_message {file} {
+proc load_message {file {encoding {}}} {
        global ui_comm
 
        set f [gitdir $file]
@@ -1406,6 +1577,9 @@ proc load_message {file} {
                        return 0
                }
                fconfigure $fd -eofchar {}
+               if {$encoding ne {}} {
+                       fconfigure $fd -encoding $encoding
+               }
                set content [string trim [read $fd]]
                close $fd
                regsub -all -line {[ \r\t]+$} $content {} content
@@ -1421,18 +1595,20 @@ proc run_prepare_commit_msg_hook {} {
 
        # prepare-commit-msg requires PREPARE_COMMIT_MSG exist.  From git-gui
        # it will be .git/MERGE_MSG (merge), .git/SQUASH_MSG (squash), or an
-       # empty file but existant file.
+       # empty file but existent file.
 
        set fd_pcm [open [gitdir PREPARE_COMMIT_MSG] a]
 
        if {[file isfile [gitdir MERGE_MSG]]} {
                set pcm_source "merge"
                set fd_mm [open [gitdir MERGE_MSG] r]
+               fconfigure $fd_mm -encoding utf-8
                puts -nonewline $fd_pcm [read $fd_mm]
                close $fd_mm
        } elseif {[file isfile [gitdir SQUASH_MSG]]} {
                set pcm_source "squash"
                set fd_sm [open [gitdir SQUASH_MSG] r]
+               fconfigure $fd_sm -encoding utf-8
                puts -nonewline $fd_pcm [read $fd_sm]
                close $fd_sm
        } else {
@@ -1497,7 +1673,7 @@ proc read_diff_index {fd after} {
                set i [split [string range $buf_rdi $c [expr {$z1 - 2}]] { }]
                set p [string range $buf_rdi $z1 [expr {$z2 - 1}]]
                merge_state \
-                       [encoding convertfrom $p] \
+                       [encoding convertfrom utf-8 $p] \
                        [lindex $i 4]? \
                        [list [lindex $i 0] [lindex $i 2]] \
                        [list]
@@ -1530,7 +1706,7 @@ proc read_diff_files {fd after} {
                set i [split [string range $buf_rdf $c [expr {$z1 - 2}]] { }]
                set p [string range $buf_rdf $z1 [expr {$z2 - 1}]]
                merge_state \
-                       [encoding convertfrom $p] \
+                       [encoding convertfrom utf-8 $p] \
                        ?[lindex $i 4] \
                        [list] \
                        [list [lindex $i 0] [lindex $i 2]]
@@ -1553,7 +1729,7 @@ proc read_ls_others {fd after} {
        set pck [split $buf_rlo "\0"]
        set buf_rlo [lindex $pck end]
        foreach p [lrange $pck 0 end-1] {
-               set p [encoding convertfrom $p]
+               set p [encoding convertfrom utf-8 $p]
                if {[string index $p end] eq {/}} {
                        set p [string range $p 0 end-1]
                }
@@ -1777,20 +1953,22 @@ proc display_all_files {} {
 
        set to_display [lsort [array names file_states]]
        set display_limit [get_config gui.maxfilesdisplayed]
-       if {[llength $to_display] > $display_limit} {
-               if {!$files_warning} {
-                       # do not repeatedly warn:
-                       set files_warning 1
-                       info_popup [mc "Displaying only %s of %s files." \
-                               $display_limit [llength $to_display]]
-               }
-               set to_display [lrange $to_display 0 [expr {$display_limit-1}]]
-       }
+       set displayed 0
        foreach path $to_display {
                set s $file_states($path)
                set m [lindex $s 0]
                set icon_name [lindex $s 1]
 
+               if {$displayed > $display_limit && [string index $m 1] eq {O} } {
+                       if {!$files_warning} {
+                               # do not repeatedly warn:
+                               set files_warning 1
+                               info_popup [mc "Display limit (gui.maxfilesdisplayed = %s) reached, not showing all %s files." \
+                                       $display_limit [llength $to_display]]
+                       }
+                       continue
+               }
+
                set s [string index $m 0]
                if {$s ne {U} && $s ne {_}} {
                        display_all_files_helper $ui_index $path \
@@ -1805,6 +1983,7 @@ proc display_all_files {} {
                if {$s ne {_}} {
                        display_all_files_helper $ui_workdir $path \
                                $icon_name $s
+                       incr displayed
                }
        }
 
@@ -1852,15 +2031,6 @@ static unsigned char file_fulltick_bits[] = {
    0x62, 0x10, 0x02, 0x10, 0xfe, 0x1f};
 } -maskdata $filemask
 
-image create bitmap file_parttick -background white -foreground "#005050" -data {
-#define parttick_width 14
-#define parttick_height 15
-static unsigned char parttick_bits[] = {
-   0xfe, 0x01, 0x02, 0x03, 0x7a, 0x05, 0x02, 0x09, 0x7a, 0x1f, 0x02, 0x10,
-   0x7a, 0x14, 0x02, 0x16, 0x02, 0x13, 0x8a, 0x11, 0xda, 0x10, 0x72, 0x10,
-   0x22, 0x10, 0x02, 0x10, 0xfe, 0x1f};
-} -maskdata $filemask
-
 image create bitmap file_question -background white -foreground black -data {
 #define file_question_width 14
 #define file_question_height 15
@@ -1889,8 +2059,8 @@ static unsigned char file_merge_bits[] = {
 } -maskdata $filemask
 
 image create bitmap file_statechange -background white -foreground green -data {
-#define file_merge_width 14
-#define file_merge_height 15
+#define file_statechange_width 14
+#define file_statechange_height 15
 static unsigned char file_statechange_bits[] = {
    0xfe, 0x01, 0x02, 0x03, 0x02, 0x05, 0x02, 0x09, 0x02, 0x1f, 0x62, 0x10,
    0x62, 0x10, 0xba, 0x11, 0xba, 0x11, 0x62, 0x10, 0x62, 0x10, 0x02, 0x10,
@@ -1901,7 +2071,7 @@ set ui_index .vpane.files.index.list
 set ui_workdir .vpane.files.workdir.list
 
 set all_icons(_$ui_index)   file_plain
-set all_icons(A$ui_index)   file_fulltick
+set all_icons(A$ui_index)   file_plain
 set all_icons(M$ui_index)   file_fulltick
 set all_icons(D$ui_index)   file_removed
 set all_icons(U$ui_index)   file_merge
@@ -1924,7 +2094,11 @@ foreach i {
                {MD {mc "Staged for commit, missing"}}
 
                {_T {mc "File type changed, not staged"}}
+               {MT {mc "File type changed, old type staged for commit"}}
+               {AT {mc "File type changed, old type staged for commit"}}
                {T_ {mc "File type changed, staged"}}
+               {TM {mc "File type change staged, modification not staged"}}
+               {TD {mc "File type change staged, file missing"}}
 
                {_O {mc "Untracked, not staged"}}
                {A_ {mc "Staged for commit"}}
@@ -1979,7 +2153,7 @@ set starting_gitk_msg [mc "Starting gitk... please wait..."]
 
 proc do_gitk {revs {is_submodule false}} {
        global current_diff_path file_states current_diff_side ui_index
-       global _gitworktree
+       global _gitdir _gitworktree
 
        # -- Always start gitk through whatever we were loaded with.  This
        #    lets us bypass using shell process on Windows systems.
@@ -1991,19 +2165,12 @@ proc do_gitk {revs {is_submodule false}} {
        } else {
                global env
 
-               if {[info exists env(GIT_DIR)]} {
-                       set old_GIT_DIR $env(GIT_DIR)
-               } else {
-                       set old_GIT_DIR {}
-               }
-
                set pwd [pwd]
 
                if {!$is_submodule} {
                        if {![is_bare]} {
                                cd $_gitworktree
                        }
-                       set env(GIT_DIR) [file normalize [gitdir]]
                } else {
                        cd $current_diff_path
                        if {$revs eq {--}} {
@@ -2024,15 +2191,18 @@ proc do_gitk {revs {is_submodule false}} {
                                }
                                set revs $old_sha1...$new_sha1
                        }
-                       if {[info exists env(GIT_DIR)]} {
-                               unset env(GIT_DIR)
-                       }
+                       # GIT_DIR and GIT_WORK_TREE for the submodule are not the ones
+                       # we've been using for the main repository, so unset them.
+                       # TODO we could make life easier (start up faster?) for gitk
+                       # by setting these to the appropriate values to allow gitk
+                       # to skip the heuristics to find their proper value
+                       unset env(GIT_DIR)
+                       unset env(GIT_WORK_TREE)
                }
                eval exec $cmd $revs "--" "--" &
 
-               if {$old_GIT_DIR ne {}} {
-                       set env(GIT_DIR) $old_GIT_DIR
-               }
+               set env(GIT_DIR) $_gitdir
+               set env(GIT_WORK_TREE) $_gitworktree
                cd $pwd
 
                ui_status $::starting_gitk_msg
@@ -2048,27 +2218,25 @@ proc do_git_gui {} {
        # -- Always start git gui through whatever we were loaded with.  This
        #    lets us bypass using shell process on Windows systems.
        #
-       set exe [_which git]
+       set exe [list [_which git]]
        if {$exe eq {}} {
                error_popup [mc "Couldn't find git gui in PATH"]
        } else {
                global env
+               global _gitdir _gitworktree
 
-               if {[info exists env(GIT_DIR)]} {
-                       set old_GIT_DIR $env(GIT_DIR)
-                       unset env(GIT_DIR)
-               } else {
-                       set old_GIT_DIR {}
-               }
+               # see note in do_gitk about unsetting these vars when
+               # running tools in a submodule
+               unset env(GIT_DIR)
+               unset env(GIT_WORK_TREE)
 
                set pwd [pwd]
                cd $current_diff_path
 
                eval exec $exe gui &
 
-               if {$old_GIT_DIR ne {}} {
-                       set env(GIT_DIR) $old_GIT_DIR
-               }
+               set env(GIT_DIR) $_gitdir
+               set env(GIT_WORK_TREE) $_gitworktree
                cd $pwd
 
                ui_status $::starting_gitk_msg
@@ -2089,7 +2257,7 @@ proc do_explore {} {
                # freedesktop.org-conforming system is our best shot
                set explorer "xdg-open"
        }
-       eval exec $explorer $_gitworktree &
+       eval exec $explorer [list [file nativename $_gitworktree]] &
 }
 
 set is_quitting 0
@@ -2105,7 +2273,7 @@ proc do_quit {{rc {1}}} {
        global ui_comm is_quitting repo_config commit_type
        global GITGUI_BCK_exists GITGUI_BCK_i
        global ui_comm_spell
-       global ret_code
+       global ret_code use_ttk
 
        if {$is_quitting} return
        set is_quitting 1
@@ -2125,6 +2293,7 @@ proc do_quit {{rc {1}}} {
                                && $msg ne {}} {
                                catch {
                                        set fd [open $save w]
+                                       fconfigure $fd -encoding utf-8
                                        puts -nonewline $fd $msg
                                        close $fd
                                }
@@ -2163,8 +2332,13 @@ proc do_quit {{rc {1}}} {
                }
                set cfg_geometry [list]
                lappend cfg_geometry [wm geometry .]
-               lappend cfg_geometry [lindex [.vpane sash coord 0] 0]
-               lappend cfg_geometry [lindex [.vpane.files sash coord 0] 1]
+               if {$use_ttk} {
+                       lappend cfg_geometry [.vpane sashpos 0]
+                       lappend cfg_geometry [.vpane.files sashpos 0]
+               } else {
+                       lappend cfg_geometry [lindex [.vpane sash coord 0] 0]
+                       lappend cfg_geometry [lindex [.vpane.files sash coord 0] 1]
+               }
                if {[catch {set rc_geometry $repo_config(gui.geometry)}]} {
                        set rc_geometry {}
                }
@@ -2319,20 +2493,48 @@ proc force_first_diff {after} {
        }
 }
 
-proc toggle_or_diff {w x y} {
+proc toggle_or_diff {mode w args} {
        global file_states file_lists current_diff_path ui_index ui_workdir
        global last_clicked selected_paths
 
-       set pos [split [$w index @$x,$y] .]
-       set lno [lindex $pos 0]
-       set col [lindex $pos 1]
-       set path [lindex $file_lists($w) [expr {$lno - 1}]]
+       if {$mode eq "click"} {
+               foreach {x y} $args break
+               set pos [split [$w index @$x,$y] .]
+               foreach {lno col} $pos break
+       } else {
+               if {$last_clicked ne {}} {
+                       set lno [lindex $last_clicked 1]
+               } else {
+                       if {![info exists file_lists]
+                               || ![info exists file_lists($w)]
+                               || [llength $file_lists($w)] == 0} {
+                               set last_clicked {}
+                               return
+                       }
+                       set lno [expr {int([lindex [$w tag ranges in_diff] 0])}]
+               }
+               if {$mode eq "toggle"} {
+                       set col 0; set y 2
+               } else {
+                       incr lno [expr {$mode eq "up" ? -1 : 1}]
+                       set col 1
+               }
+       }
+
+       if {![info exists file_lists]
+               || ![info exists file_lists($w)]
+               || [llength $file_lists($w)] < $lno - 1} {
+               set path {}
+       } else {
+               set path [lindex $file_lists($w) [expr {$lno - 1}]]
+       }
        if {$path eq {}} {
                set last_clicked {}
                return
        }
 
        set last_clicked [list $w $lno]
+       focus $w
        array unset selected_paths
        $ui_index tag remove in_sel 0.0 end
        $ui_workdir tag remove in_sel 0.0 end
@@ -2374,6 +2576,7 @@ proc toggle_or_diff {w x y} {
                                [concat $after [list ui_ready]]
                }
        } else {
+               set selected_paths($path) 1
                show_diff $path $w $lno
        }
 }
@@ -2411,7 +2614,7 @@ proc add_range_to_selection {w x y} {
        global file_lists last_clicked selected_paths
 
        if {[lindex $last_clicked 0] ne $w} {
-               toggle_or_diff $w $x $y
+               toggle_or_diff click $w $x $y
                return
        }
 
@@ -2488,6 +2691,16 @@ if {![is_bare]} {
        .mbar.repository add command \
                -label [mc "Explore Working Copy"] \
                -command {do_explore}
+}
+
+if {[is_Windows]} {
+       .mbar.repository add command \
+               -label [mc "Git Bash"] \
+               -command {eval exec [auto_execok start] \
+                                         [list "Git Bash" bash --login -l &]}
+}
+
+if {[is_Windows] || ![is_bare]} {
        .mbar.repository add separator
 }
 
@@ -2810,7 +3023,14 @@ bind all <$M1B-Key-W> {destroy [winfo toplevel %W]}
 
 set subcommand_args {}
 proc usage {} {
-       puts stderr "usage: $::argv0 $::subcommand $::subcommand_args"
+       set s "[mc usage:] $::argv0 $::subcommand $::subcommand_args"
+       if {[tk windowingsystem] eq "win32"} {
+               wm withdraw .
+               tk_messageBox -icon info -message $s \
+                       -title [mc "Usage"]
+       } else {
+               puts stderr $s
+       }
        exit 1
 }
 
@@ -2844,9 +3064,11 @@ blame {
        set jump_spec {}
        set is_path 0
        foreach a $argv {
-               if {$is_path || [file exists $_prefix$a]} {
+               set p [file join $_prefix $a]
+
+               if {$is_path || [file exists $p]} {
                        if {$path ne {}} usage
-                       set path [normalize_relpath $_prefix$a]
+                       set path [normalize_relpath $p]
                        break
                } elseif {$a eq {--}} {
                        if {$path ne {}} {
@@ -2869,8 +3091,13 @@ blame {
        unset is_path
 
        if {$head ne {} && $path eq {}} {
-               set path [normalize_relpath $_prefix$head]
-               set head {}
+               if {[string index $head 0] eq {/}} {
+                       set path [normalize_relpath $head]
+                       set head {}
+               } else {
+                       set path [normalize_relpath $_prefix$head]
+                       set head {}
+               }
        }
 
        if {$head eq {}} {
@@ -2880,13 +3107,18 @@ blame {
                        if {[catch {
                                        set head [git rev-parse --verify $head]
                                } err]} {
-                               puts stderr $err
+                               if {[tk windowingsystem] eq "win32"} {
+                                       tk_messageBox -icon error -title [mc Error] -message $err
+                               } else {
+                                       puts stderr $err
+                               }
                                exit 1
                        }
                }
                set current_branch $head
        }
 
+       wm deiconify .
        switch -- $subcommand {
        browser {
                if {$jump_spec ne {}} usage
@@ -2902,7 +3134,12 @@ blame {
        }
        blame   {
                if {$head eq {} && ![file exists $path]} {
-                       puts stderr [mc "fatal: cannot stat path %s: No such file or directory" $path]
+                       catch {wm withdraw .}
+                       tk_messageBox \
+                               -icon error \
+                               -type ok \
+                               -title [mc "git-gui: fatal error"] \
+                               -message [mc "fatal: cannot stat path %s: No such file or directory" $path]
                        exit 1
                }
                blame::new $head $path $jump_spec
@@ -2913,32 +3150,32 @@ blame {
 citool -
 gui {
        if {[llength $argv] != 0} {
-               puts -nonewline stderr "usage: $argv0"
-               if {$subcommand ne {gui}
-                       && [file tail $argv0] ne "git-$subcommand"} {
-                       puts -nonewline stderr " $subcommand"
-               }
-               puts stderr {}
-               exit 1
+               usage
        }
        # fall through to setup UI for commits
 }
 default {
-       puts stderr "usage: $argv0 \[{blame|browser|citool}\]"
+       set err "[mc usage:] $argv0 \[{blame|browser|citool}\]"
+       if {[tk windowingsystem] eq "win32"} {
+               wm withdraw .
+               tk_messageBox -icon error -message $err \
+                       -title [mc "Usage"]
+       } else {
+               puts stderr $err
+       }
        exit 1
 }
 }
 
 # -- Branch Control
 #
-frame .branch \
-       -borderwidth 1 \
-       -relief sunken
-label .branch.l1 \
+${NS}::frame .branch
+if {!$use_ttk} {.branch configure -borderwidth 1 -relief sunken}
+${NS}::label .branch.l1 \
        -text [mc "Current Branch:"] \
        -anchor w \
        -justify left
-label .branch.cb \
+${NS}::label .branch.cb \
        -textvariable current_branch \
        -anchor w \
        -justify left
@@ -2948,53 +3185,66 @@ pack .branch -side top -fill x
 
 # -- Main Window Layout
 #
-panedwindow .vpane -orient horizontal
-panedwindow .vpane.files -orient vertical
-.vpane add .vpane.files -sticky nsew -height 100 -width 200
+${NS}::panedwindow .vpane -orient horizontal
+${NS}::panedwindow .vpane.files -orient vertical
+if {$use_ttk} {
+       .vpane add .vpane.files
+} else {
+       .vpane add .vpane.files -sticky nsew -height 100 -width 200
+}
 pack .vpane -anchor n -side top -fill both -expand 1
 
+# -- Working Directory File List
+
+textframe .vpane.files.workdir -height 100 -width 200
+tlabel .vpane.files.workdir.title -text [mc "Unstaged Changes"] \
+       -background lightsalmon -foreground black
+ttext $ui_workdir -background white -foreground black \
+       -borderwidth 0 \
+       -width 20 -height 10 \
+       -wrap none \
+       -takefocus 1 -highlightthickness 1\
+       -cursor $cursor_ptr \
+       -xscrollcommand {.vpane.files.workdir.sx set} \
+       -yscrollcommand {.vpane.files.workdir.sy set} \
+       -state disabled
+${NS}::scrollbar .vpane.files.workdir.sx -orient h -command [list $ui_workdir xview]
+${NS}::scrollbar .vpane.files.workdir.sy -orient v -command [list $ui_workdir yview]
+pack .vpane.files.workdir.title -side top -fill x
+pack .vpane.files.workdir.sx -side bottom -fill x
+pack .vpane.files.workdir.sy -side right -fill y
+pack $ui_workdir -side left -fill both -expand 1
+
 # -- Index File List
 #
-frame .vpane.files.index -height 100 -width 200
-label .vpane.files.index.title -text [mc "Staged Changes (Will Commit)"] \
+textframe .vpane.files.index -height 100 -width 200
+tlabel .vpane.files.index.title \
+       -text [mc "Staged Changes (Will Commit)"] \
        -background lightgreen -foreground black
-text $ui_index -background white -foreground black \
+ttext $ui_index -background white -foreground black \
        -borderwidth 0 \
        -width 20 -height 10 \
        -wrap none \
+       -takefocus 1 -highlightthickness 1\
        -cursor $cursor_ptr \
        -xscrollcommand {.vpane.files.index.sx set} \
        -yscrollcommand {.vpane.files.index.sy set} \
        -state disabled
-scrollbar .vpane.files.index.sx -orient h -command [list $ui_index xview]
-scrollbar .vpane.files.index.sy -orient v -command [list $ui_index yview]
+${NS}::scrollbar .vpane.files.index.sx -orient h -command [list $ui_index xview]
+${NS}::scrollbar .vpane.files.index.sy -orient v -command [list $ui_index yview]
 pack .vpane.files.index.title -side top -fill x
 pack .vpane.files.index.sx -side bottom -fill x
 pack .vpane.files.index.sy -side right -fill y
 pack $ui_index -side left -fill both -expand 1
 
-# -- Working Directory File List
+# -- Insert the workdir and index into the panes
 #
-frame .vpane.files.workdir -height 100 -width 200
-label .vpane.files.workdir.title -text [mc "Unstaged Changes"] \
-       -background lightsalmon -foreground black
-text $ui_workdir -background white -foreground black \
-       -borderwidth 0 \
-       -width 20 -height 10 \
-       -wrap none \
-       -cursor $cursor_ptr \
-       -xscrollcommand {.vpane.files.workdir.sx set} \
-       -yscrollcommand {.vpane.files.workdir.sy set} \
-       -state disabled
-scrollbar .vpane.files.workdir.sx -orient h -command [list $ui_workdir xview]
-scrollbar .vpane.files.workdir.sy -orient v -command [list $ui_workdir yview]
-pack .vpane.files.workdir.title -side top -fill x
-pack .vpane.files.workdir.sx -side bottom -fill x
-pack .vpane.files.workdir.sy -side right -fill y
-pack $ui_workdir -side left -fill both -expand 1
-
-.vpane.files add .vpane.files.workdir -sticky nsew
-.vpane.files add .vpane.files.index -sticky nsew
+.vpane.files add .vpane.files.workdir
+.vpane.files add .vpane.files.index
+if {!$use_ttk} {
+       .vpane.files paneconfigure .vpane.files.workdir -sticky news
+       .vpane.files paneconfigure .vpane.files.index -sticky news
+}
 
 foreach i [list $ui_index $ui_workdir] {
        rmsel_tag $i
@@ -3004,68 +3254,85 @@ unset i
 
 # -- Diff and Commit Area
 #
-frame .vpane.lower -height 300 -width 400
-frame .vpane.lower.commarea
-frame .vpane.lower.diff -relief sunken -borderwidth 1
-pack .vpane.lower.diff -fill both -expand 1
-pack .vpane.lower.commarea -side bottom -fill x
-.vpane add .vpane.lower -sticky nsew
+if {$have_tk85} {
+       ${NS}::panedwindow .vpane.lower -orient vertical
+       ${NS}::frame .vpane.lower.commarea
+       ${NS}::frame .vpane.lower.diff -relief sunken -borderwidth 1 -height 500
+       .vpane.lower add .vpane.lower.diff
+       .vpane.lower add .vpane.lower.commarea
+       .vpane add .vpane.lower
+       if {$use_ttk} {
+               .vpane.lower pane .vpane.lower.diff -weight 1
+               .vpane.lower pane .vpane.lower.commarea -weight 0
+       } else {
+               .vpane.lower paneconfigure .vpane.lower.diff -stretch always
+               .vpane.lower paneconfigure .vpane.lower.commarea -stretch never
+       }
+} else {
+       frame .vpane.lower -height 300 -width 400
+       frame .vpane.lower.commarea
+       frame .vpane.lower.diff -relief sunken -borderwidth 1
+       pack .vpane.lower.diff -fill both -expand 1
+       pack .vpane.lower.commarea -side bottom -fill x
+       .vpane add .vpane.lower
+       .vpane paneconfigure .vpane.lower -sticky nsew
+}
 
 # -- Commit Area Buttons
 #
-frame .vpane.lower.commarea.buttons
-label .vpane.lower.commarea.buttons.l -text {} \
+${NS}::frame .vpane.lower.commarea.buttons
+${NS}::label .vpane.lower.commarea.buttons.l -text {} \
        -anchor w \
        -justify left
 pack .vpane.lower.commarea.buttons.l -side top -fill x
 pack .vpane.lower.commarea.buttons -side left -fill y
 
-button .vpane.lower.commarea.buttons.rescan -text [mc Rescan] \
+${NS}::button .vpane.lower.commarea.buttons.rescan -text [mc Rescan] \
        -command ui_do_rescan
 pack .vpane.lower.commarea.buttons.rescan -side top -fill x
 lappend disable_on_lock \
        {.vpane.lower.commarea.buttons.rescan conf -state}
 
-button .vpane.lower.commarea.buttons.incall -text [mc "Stage Changed"] \
+${NS}::button .vpane.lower.commarea.buttons.incall -text [mc "Stage Changed"] \
        -command do_add_all
 pack .vpane.lower.commarea.buttons.incall -side top -fill x
 lappend disable_on_lock \
        {.vpane.lower.commarea.buttons.incall conf -state}
 
 if {![is_enabled nocommitmsg]} {
-       button .vpane.lower.commarea.buttons.signoff -text [mc "Sign Off"] \
+       ${NS}::button .vpane.lower.commarea.buttons.signoff -text [mc "Sign Off"] \
                -command do_signoff
        pack .vpane.lower.commarea.buttons.signoff -side top -fill x
 }
 
-button .vpane.lower.commarea.buttons.commit -text [commit_btn_caption] \
+${NS}::button .vpane.lower.commarea.buttons.commit -text [commit_btn_caption] \
        -command do_commit
 pack .vpane.lower.commarea.buttons.commit -side top -fill x
 lappend disable_on_lock \
        {.vpane.lower.commarea.buttons.commit conf -state}
 
 if {![is_enabled nocommit]} {
-       button .vpane.lower.commarea.buttons.push -text [mc Push] \
+       ${NS}::button .vpane.lower.commarea.buttons.push -text [mc Push] \
                -command do_push_anywhere
        pack .vpane.lower.commarea.buttons.push -side top -fill x
 }
 
 # -- Commit Message Buffer
 #
-frame .vpane.lower.commarea.buffer
-frame .vpane.lower.commarea.buffer.header
-set ui_comm .vpane.lower.commarea.buffer.t
+${NS}::frame .vpane.lower.commarea.buffer
+${NS}::frame .vpane.lower.commarea.buffer.header
+set ui_comm .vpane.lower.commarea.buffer.frame.t
 set ui_coml .vpane.lower.commarea.buffer.header.l
 
 if {![is_enabled nocommit]} {
-       radiobutton .vpane.lower.commarea.buffer.header.new \
+       ${NS}::radiobutton .vpane.lower.commarea.buffer.header.new \
                -text [mc "New Commit"] \
                -command do_select_commit_type \
                -variable selected_commit_type \
                -value new
        lappend disable_on_lock \
                [list .vpane.lower.commarea.buffer.header.new conf -state]
-       radiobutton .vpane.lower.commarea.buffer.header.amend \
+       ${NS}::radiobutton .vpane.lower.commarea.buffer.header.amend \
                -text [mc "Amend Last Commit"] \
                -command do_select_commit_type \
                -variable selected_commit_type \
@@ -3074,7 +3341,7 @@ if {![is_enabled nocommit]} {
                [list .vpane.lower.commarea.buffer.header.amend conf -state]
 }
 
-label $ui_coml \
+${NS}::label $ui_coml \
        -anchor w \
        -justify left
 proc trace_commit_type {varname args} {
@@ -3097,20 +3364,25 @@ if {![is_enabled nocommit]} {
        pack .vpane.lower.commarea.buffer.header.new -side right
 }
 
-text $ui_comm -background white -foreground black \
+textframe .vpane.lower.commarea.buffer.frame
+ttext $ui_comm -background white -foreground black \
        -borderwidth 1 \
        -undo true \
        -maxundo 20 \
        -autoseparators true \
+       -takefocus 1 \
+       -highlightthickness 1 \
        -relief sunken \
        -width $repo_config(gui.commitmsgwidth) -height 9 -wrap none \
        -font font_diff \
-       -yscrollcommand {.vpane.lower.commarea.buffer.sby set}
-scrollbar .vpane.lower.commarea.buffer.sby \
+       -yscrollcommand {.vpane.lower.commarea.buffer.frame.sby set}
+${NS}::scrollbar .vpane.lower.commarea.buffer.frame.sby \
        -command [list $ui_comm yview]
-pack .vpane.lower.commarea.buffer.header -side top -fill x
-pack .vpane.lower.commarea.buffer.sby -side right -fill y
+
+pack .vpane.lower.commarea.buffer.frame.sby -side right -fill y
 pack $ui_comm -side left -fill y
+pack .vpane.lower.commarea.buffer.header -side top -fill x
+pack .vpane.lower.commarea.buffer.frame -side left -fill y
 pack .vpane.lower.commarea.buffer -side left -fill y
 
 # -- Commit Message Buffer Context Menu
@@ -3172,19 +3444,19 @@ proc trace_current_diff_path {varname args} {
 }
 trace add variable current_diff_path write trace_current_diff_path
 
-frame .vpane.lower.diff.header -background gold
-label .vpane.lower.diff.header.status \
+gold_frame .vpane.lower.diff.header
+tlabel .vpane.lower.diff.header.status \
        -background gold \
        -foreground black \
        -width $max_status_desc \
        -anchor w \
        -justify left
-label .vpane.lower.diff.header.file \
+tlabel .vpane.lower.diff.header.file \
        -background gold \
        -foreground black \
        -anchor w \
        -justify left
-label .vpane.lower.diff.header.path \
+tlabel .vpane.lower.diff.header.path \
        -background gold \
        -foreground black \
        -anchor w \
@@ -3208,18 +3480,20 @@ bind_button3 .vpane.lower.diff.header.path "tk_popup $ctxm %X %Y"
 
 # -- Diff Body
 #
-frame .vpane.lower.diff.body
+textframe .vpane.lower.diff.body
 set ui_diff .vpane.lower.diff.body.t
-text $ui_diff -background white -foreground black \
+ttext $ui_diff -background white -foreground black \
        -borderwidth 0 \
        -width 80 -height 5 -wrap none \
        -font font_diff \
+       -takefocus 1 -highlightthickness 1 \
        -xscrollcommand {.vpane.lower.diff.body.sbx set} \
        -yscrollcommand {.vpane.lower.diff.body.sby set} \
        -state disabled
-scrollbar .vpane.lower.diff.body.sbx -orient horizontal \
+catch {$ui_diff configure -tabstyle wordprocessor}
+${NS}::scrollbar .vpane.lower.diff.body.sbx -orient horizontal \
        -command [list $ui_diff xview]
-scrollbar .vpane.lower.diff.body.sby -orient vertical \
+${NS}::scrollbar .vpane.lower.diff.body.sby -orient vertical \
        -command [list $ui_diff yview]
 pack .vpane.lower.diff.body.sbx -side bottom -fill x
 pack .vpane.lower.diff.body.sby -side right -fill y
@@ -3227,8 +3501,19 @@ pack $ui_diff -side left -fill both -expand 1
 pack .vpane.lower.diff.header -side top -fill x
 pack .vpane.lower.diff.body -side bottom -fill both -expand 1
 
+foreach {n c} {0 black 1 red4 2 green4 3 yellow4 4 blue4 5 magenta4 6 cyan4 7 grey60} {
+       $ui_diff tag configure clr4$n -background $c
+       $ui_diff tag configure clri4$n -foreground $c
+       $ui_diff tag configure clr3$n -foreground $c
+       $ui_diff tag configure clri3$n -background $c
+}
+$ui_diff tag configure clr1 -font font_diffbold
+$ui_diff tag configure clr4 -underline 1
+
+$ui_diff tag conf d_info -foreground blue -font font_diffbold
+
 $ui_diff tag conf d_cr -elide true
-$ui_diff tag conf d_@ -foreground blue -font font_diffbold
+$ui_diff tag conf d_@ -font font_diffbold
 $ui_diff tag conf d_+ -foreground {#00a000}
 $ui_diff tag conf d_- -foreground red
 
@@ -3247,13 +3532,13 @@ $ui_diff tag conf d_s- \
        -foreground red \
        -background ivory1
 
-$ui_diff tag conf d<<<<<<< \
+$ui_diff tag conf d< \
        -foreground orange \
        -font font_diffbold
-$ui_diff tag conf d======= \
+$ui_diff tag conf d= \
        -foreground orange \
        -font font_diffbold
-$ui_diff tag conf d>>>>>>> \
+$ui_diff tag conf d> \
        -foreground orange \
        -font font_diffbold
 
@@ -3382,6 +3667,19 @@ lappend diff_actions [list $ctxmsm entryconf [$ctxmsm index last] -state]
 $ctxmsm add separator
 create_common_diff_popup $ctxmsm
 
+proc has_textconv {path} {
+       if {[is_config_false gui.textconv]} {
+               return 0
+       }
+       set filter [gitattr $path diff set]
+       set textconv [get_config [join [list diff $filter textconv] .]]
+       if {$filter ne {set} && $textconv ne {}} {
+               return 1
+       } else {
+               return 0
+       }
+}
+
 proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} {
        global current_diff_path file_states
        set ::cursorX $x
@@ -3416,8 +3714,9 @@ proc popup_diff_menu {ctxm ctxmmg ctxmsm x y X Y} {
                        || $current_diff_path eq {}
                        || {__} eq $state
                        || {_O} eq $state
-                       || {_T} eq $state
-                       || {T_} eq $state} {
+                       || [string match {?T} $state]
+                       || [string match {T?} $state]
+                       || [has_textconv $current_diff_path]} {
                        set s disabled
                } else {
                        set s normal
@@ -3437,24 +3736,44 @@ $main_status show [mc "Initializing..."]
 
 # -- Load geometry
 #
-catch {
-set gm $repo_config(gui.geometry)
-wm geometry . [lindex $gm 0]
-.vpane sash place 0 \
-       [lindex $gm 1] \
-       [lindex [.vpane sash coord 0] 1]
-.vpane.files sash place 0 \
-       [lindex [.vpane.files sash coord 0] 0] \
-       [lindex $gm 2]
-unset gm
+proc on_ttk_pane_mapped {w pane pos} {
+       bind $w <Map> {}
+       after 0 [list after idle [list $w sashpos $pane $pos]]
+}
+proc on_tk_pane_mapped {w pane x y} {
+       bind $w <Map> {}
+       after 0 [list after idle [list $w sash place $pane $x $y]]
+}
+proc on_application_mapped {} {
+       global repo_config use_ttk
+       bind . <Map> {}
+       set gm $repo_config(gui.geometry)
+       if {$use_ttk} {
+               bind .vpane <Map> \
+                   [list on_ttk_pane_mapped %W 0 [lindex $gm 1]]
+               bind .vpane.files <Map> \
+                   [list on_ttk_pane_mapped %W 0 [lindex $gm 2]]
+       } else {
+               bind .vpane <Map> \
+                   [list on_tk_pane_mapped %W 0 \
+                        [lindex $gm 1] \
+                        [lindex [.vpane sash coord 0] 1]]
+               bind .vpane.files <Map> \
+                   [list on_tk_pane_mapped %W 0 \
+                        [lindex [.vpane.files sash coord 0] 0] \
+                        [lindex $gm 2]]
+       }
+       wm geometry . [lindex $gm 0]
+}
+if {[info exists repo_config(gui.geometry)]} {
+       bind . <Map> [list on_application_mapped]
+       wm geometry . [lindex $repo_config(gui.geometry) 0]
 }
 
 # -- Load window state
 #
-catch {
-set gws $repo_config(gui.wmstate)
-wm state . $gws
-unset gws
+if {[info exists repo_config(gui.wmstate)]} {
+       catch {wm state . $repo_config(gui.wmstate)}
 }
 
 # -- Key Bindings
@@ -3490,6 +3809,8 @@ bind $ui_diff <$M1B-Key-v> {break}
 bind $ui_diff <$M1B-Key-V> {break}
 bind $ui_diff <$M1B-Key-a> {%W tag add sel 0.0 end;break}
 bind $ui_diff <$M1B-Key-A> {%W tag add sel 0.0 end;break}
+bind $ui_diff <$M1B-Key-j> {do_revert_selection;break}
+bind $ui_diff <$M1B-Key-J> {do_revert_selection;break}
 bind $ui_diff <Key-Up>     {catch {%W yview scroll -1 units};break}
 bind $ui_diff <Key-Down>   {catch {%W yview scroll  1 units};break}
 bind $ui_diff <Key-Left>   {catch {%W xview scroll -1 units};break}
@@ -3520,8 +3841,12 @@ bind .   <$M1B-Key-r> ui_do_rescan
 bind .   <$M1B-Key-R> ui_do_rescan
 bind .   <$M1B-Key-s> do_signoff
 bind .   <$M1B-Key-S> do_signoff
-bind .   <$M1B-Key-t> do_add_selection
-bind .   <$M1B-Key-T> do_add_selection
+bind .   <$M1B-Key-t> { toggle_or_diff toggle %W }
+bind .   <$M1B-Key-T> { toggle_or_diff toggle %W }
+bind .   <$M1B-Key-u> { toggle_or_diff toggle %W }
+bind .   <$M1B-Key-U> { toggle_or_diff toggle %W }
+bind .   <$M1B-Key-j> do_revert_selection
+bind .   <$M1B-Key-J> do_revert_selection
 bind .   <$M1B-Key-i> do_add_all
 bind .   <$M1B-Key-I> do_add_all
 bind .   <$M1B-Key-minus> {show_less_context;break}
@@ -3531,9 +3856,11 @@ bind .   <$M1B-Key-plus> {show_more_context;break}
 bind .   <$M1B-Key-KP_Add> {show_more_context;break}
 bind .   <$M1B-Key-Return> do_commit
 foreach i [list $ui_index $ui_workdir] {
-       bind $i <Button-1>       "toggle_or_diff         $i %x %y; break"
-       bind $i <$M1B-Button-1>  "add_one_to_selection   $i %x %y; break"
-       bind $i <Shift-Button-1> "add_range_to_selection $i %x %y; break"
+       bind $i <Button-1>       { toggle_or_diff click %W %x %y; break }
+       bind $i <$M1B-Button-1>  { add_one_to_selection %W %x %y; break }
+       bind $i <Shift-Button-1> { add_range_to_selection %W %x %y; break }
+       bind $i <Key-Up>         { toggle_or_diff up %W; break }
+       bind $i <Key-Down>       { toggle_or_diff down %W; break }
 }
 unset i
 
@@ -3613,7 +3940,7 @@ if {[is_enabled transport]} {
 }
 
 if {[winfo exists $ui_comm]} {
-       set GITGUI_BCK_exists [load_message GITGUI_BCK]
+       set GITGUI_BCK_exists [load_message GITGUI_BCK utf-8]
 
        # -- If both our backup and message files exist use the
        #    newer of the two files to initialize the buffer.
@@ -3650,6 +3977,7 @@ if {[winfo exists $ui_comm]} {
                        } elseif {$m} {
                                catch {
                                        set fd [open [gitdir GITGUI_BCK] w]
+                                       fconfigure $fd -encoding utf-8
                                        puts -nonewline $fd $msg
                                        close $fd
                                        set GITGUI_BCK_exists 1
@@ -3704,7 +4032,7 @@ after 1 {
                $ui_comm configure -state disabled -background gray
        }
 }
-if {[is_enabled multicommit]} {
+if {[is_enabled multicommit] && ![is_config_false gui.gcwarning]} {
        after 1000 hint_gc
 }
 if {[is_enabled retcode]} {
@@ -3713,3 +4041,9 @@ if {[is_enabled retcode]} {
 if {$picked && [is_config_true gui.autoexplore]} {
        do_explore
 }
+
+# Local variables:
+# mode: tcl
+# indent-tabs-mode: t
+# tab-width: 4
+# End: