40af44e3bff8e328b29a0a5b3f7457233701f853
   1# git-gui options editor
   2# Copyright (C) 2006, 2007 Shawn Pearce
   3
   4proc config_check_encodings {} {
   5        global repo_config_new global_config_new
   6
   7        set enc $global_config_new(gui.encoding)
   8        if {$enc eq {}} {
   9                set global_config_new(gui.encoding) [encoding system]
  10        } elseif {[tcl_encoding $enc] eq {}} {
  11                error_popup [mc "Invalid global encoding '%s'" $enc]
  12                return 0
  13        }
  14
  15        set enc $repo_config_new(gui.encoding)
  16        if {$enc eq {}} {
  17                set repo_config_new(gui.encoding) [encoding system]
  18        } elseif {[tcl_encoding $enc] eq {}} {
  19                error_popup [mc "Invalid repo encoding '%s'" $enc]
  20                return 0
  21        }
  22
  23        return 1
  24}
  25
  26proc save_config {} {
  27        global default_config font_descs
  28        global repo_config global_config
  29        global repo_config_new global_config_new
  30        global ui_comm_spell
  31
  32        foreach option $font_descs {
  33                set name [lindex $option 0]
  34                set font [lindex $option 1]
  35                font configure $font \
  36                        -family $global_config_new(gui.$font^^family) \
  37                        -size $global_config_new(gui.$font^^size)
  38                font configure ${font}bold \
  39                        -family $global_config_new(gui.$font^^family) \
  40                        -size $global_config_new(gui.$font^^size)
  41                font configure ${font}italic \
  42                        -family $global_config_new(gui.$font^^family) \
  43                        -size $global_config_new(gui.$font^^size)
  44                set global_config_new(gui.$name) [font configure $font]
  45                unset global_config_new(gui.$font^^family)
  46                unset global_config_new(gui.$font^^size)
  47        }
  48
  49        foreach name [array names default_config] {
  50                set value $global_config_new($name)
  51                if {$value ne $global_config($name)} {
  52                        if {$value eq $default_config($name)} {
  53                                catch {git config --global --unset $name}
  54                        } else {
  55                                regsub -all "\[{}\]" $value {"} value
  56                                git config --global $name $value
  57                        }
  58                        set global_config($name) $value
  59                        if {$value eq $repo_config($name)} {
  60                                catch {git config --unset $name}
  61                                set repo_config($name) $value
  62                        }
  63                }
  64        }
  65
  66        foreach name [array names default_config] {
  67                set value $repo_config_new($name)
  68                if {$value ne $repo_config($name)} {
  69                        if {$value eq $global_config($name)} {
  70                                catch {git config --unset $name}
  71                        } else {
  72                                regsub -all "\[{}\]" $value {"} value
  73                                git config $name $value
  74                        }
  75                        set repo_config($name) $value
  76                }
  77        }
  78
  79        if {[info exists repo_config(gui.spellingdictionary)]} {
  80                set value $repo_config(gui.spellingdictionary)
  81                if {$value eq {none}} {
  82                        if {[info exists ui_comm_spell]} {
  83                                $ui_comm_spell stop
  84                        }
  85                } elseif {[info exists ui_comm_spell]} {
  86                        $ui_comm_spell lang $value
  87                }
  88        }
  89}
  90
  91proc do_options {} {
  92        global repo_config global_config font_descs
  93        global repo_config_new global_config_new
  94        global ui_comm_spell
  95
  96        array unset repo_config_new
  97        array unset global_config_new
  98        foreach name [array names repo_config] {
  99                set repo_config_new($name) $repo_config($name)
 100        }
 101        load_config 1
 102        foreach name [array names repo_config] {
 103                switch -- $name {
 104                gui.diffcontext {continue}
 105                }
 106                set repo_config_new($name) $repo_config($name)
 107        }
 108        foreach name [array names global_config] {
 109                set global_config_new($name) $global_config($name)
 110        }
 111
 112        set w .options_editor
 113        toplevel $w
 114        wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
 115
 116        frame $w.buttons
 117        button $w.buttons.restore -text [mc "Restore Defaults"] \
 118                -default normal \
 119                -command do_restore_defaults
 120        pack $w.buttons.restore -side left
 121        button $w.buttons.save -text [mc Save] \
 122                -default active \
 123                -command [list do_save_config $w]
 124        pack $w.buttons.save -side right
 125        button $w.buttons.cancel -text [mc "Cancel"] \
 126                -default normal \
 127                -command [list destroy $w]
 128        pack $w.buttons.cancel -side right -padx 5
 129        pack $w.buttons -side bottom -fill x -pady 10 -padx 10
 130
 131        labelframe $w.repo -text [mc "%s Repository" [reponame]]
 132        labelframe $w.global -text [mc "Global (All Repositories)"]
 133        pack $w.repo -side left -fill both -expand 1 -pady 5 -padx 5
 134        pack $w.global -side right -fill both -expand 1 -pady 5 -padx 5
 135
 136        set optid 0
 137        foreach option {
 138                {t user.name {mc "User Name"}}
 139                {t user.email {mc "Email Address"}}
 140
 141                {b merge.summary {mc "Summarize Merge Commits"}}
 142                {i-1..5 merge.verbosity {mc "Merge Verbosity"}}
 143                {b merge.diffstat {mc "Show Diffstat After Merge"}}
 144                {t merge.tool {mc "Use Merge Tool"}}
 145
 146                {b gui.trustmtime  {mc "Trust File Modification Timestamps"}}
 147                {b gui.pruneduringfetch {mc "Prune Tracking Branches During Fetch"}}
 148                {b gui.matchtrackingbranch {mc "Match Tracking Branches"}}
 149                {b gui.fastcopyblame {mc "Blame Copy Only On Changed Files"}}
 150                {i-20..200 gui.copyblamethreshold {mc "Minimum Letters To Blame Copy On"}}
 151                {i-0..300 gui.blamehistoryctx {mc "Blame History Context Radius (days)"}}
 152                {i-1..99 gui.diffcontext {mc "Number of Diff Context Lines"}}
 153                {i-0..99 gui.commitmsgwidth {mc "Commit Message Text Width"}}
 154                {t gui.newbranchtemplate {mc "New Branch Name Template"}}
 155                {t gui.encoding {mc "Default File Contents Encoding"}}
 156                } {
 157                set type [lindex $option 0]
 158                set name [lindex $option 1]
 159                set text [eval [lindex $option 2]]
 160                incr optid
 161                foreach f {repo global} {
 162                        switch -glob -- $type {
 163                        b {
 164                                checkbutton $w.$f.$optid -text $text \
 165                                        -variable ${f}_config_new($name) \
 166                                        -onvalue true \
 167                                        -offvalue false
 168                                pack $w.$f.$optid -side top -anchor w
 169                        }
 170                        i-* {
 171                                regexp -- {-(\d+)\.\.(\d+)$} $type _junk min max
 172                                frame $w.$f.$optid
 173                                label $w.$f.$optid.l -text "$text:"
 174                                pack $w.$f.$optid.l -side left -anchor w -fill x
 175                                spinbox $w.$f.$optid.v \
 176                                        -textvariable ${f}_config_new($name) \
 177                                        -from $min \
 178                                        -to $max \
 179                                        -increment 1 \
 180                                        -width [expr {1 + [string length $max]}]
 181                                bind $w.$f.$optid.v <FocusIn> {%W selection range 0 end}
 182                                pack $w.$f.$optid.v -side right -anchor e -padx 5
 183                                pack $w.$f.$optid -side top -anchor w -fill x
 184                        }
 185                        t {
 186                                frame $w.$f.$optid
 187                                label $w.$f.$optid.l -text "$text:"
 188                                entry $w.$f.$optid.v \
 189                                        -borderwidth 1 \
 190                                        -relief sunken \
 191                                        -width 20 \
 192                                        -textvariable ${f}_config_new($name)
 193                                pack $w.$f.$optid.l -side left -anchor w
 194                                pack $w.$f.$optid.v -side left -anchor w \
 195                                        -fill x -expand 1 \
 196                                        -padx 5
 197                                pack $w.$f.$optid -side top -anchor w -fill x
 198                        }
 199                        }
 200                }
 201        }
 202
 203        set all_dicts [linsert \
 204                [spellcheck::available_langs] \
 205                0 \
 206                none]
 207        incr optid
 208        foreach f {repo global} {
 209                if {![info exists ${f}_config_new(gui.spellingdictionary)]} {
 210                        if {[info exists ui_comm_spell]} {
 211                                set value [$ui_comm_spell lang]
 212                        } else {
 213                                set value none
 214                        }
 215                        set ${f}_config_new(gui.spellingdictionary) $value
 216                }
 217
 218                frame $w.$f.$optid
 219                label $w.$f.$optid.l -text [mc "Spelling Dictionary:"]
 220                eval tk_optionMenu $w.$f.$optid.v \
 221                        ${f}_config_new(gui.spellingdictionary) \
 222                        $all_dicts
 223                pack $w.$f.$optid.l -side left -anchor w -fill x
 224                pack $w.$f.$optid.v -side right -anchor e -padx 5
 225                pack $w.$f.$optid -side top -anchor w -fill x
 226        }
 227        unset all_dicts
 228
 229        set all_fonts [lsort [font families]]
 230        foreach option $font_descs {
 231                set name [lindex $option 0]
 232                set font [lindex $option 1]
 233                set text [eval [lindex $option 2]]
 234
 235                set global_config_new(gui.$font^^family) \
 236                        [font configure $font -family]
 237                set global_config_new(gui.$font^^size) \
 238                        [font configure $font -size]
 239
 240                frame $w.global.$name
 241                label $w.global.$name.l -text "$text:"
 242                button $w.global.$name.b \
 243                        -text [mc "Change Font"] \
 244                        -command [list \
 245                                choose_font::pick \
 246                                $w \
 247                                [mc "Choose %s" $text] \
 248                                global_config_new(gui.$font^^family) \
 249                                global_config_new(gui.$font^^size) \
 250                                ]
 251                label $w.global.$name.f -textvariable global_config_new(gui.$font^^family)
 252                label $w.global.$name.s -textvariable global_config_new(gui.$font^^size)
 253                label $w.global.$name.pt -text [mc "pt."]
 254                pack $w.global.$name.l -side left -anchor w
 255                pack $w.global.$name.b -side right -anchor e
 256                pack $w.global.$name.pt -side right -anchor w
 257                pack $w.global.$name.s -side right -anchor w
 258                pack $w.global.$name.f -side right -anchor w
 259                pack $w.global.$name -side top -anchor w -fill x
 260        }
 261
 262        bind $w <Visibility> "grab $w; focus $w.buttons.save"
 263        bind $w <Key-Escape> "destroy $w"
 264        bind $w <Key-Return> [list do_save_config $w]
 265
 266        if {[is_MacOSX]} {
 267                set t [mc "Preferences"]
 268        } else {
 269                set t [mc "Options"]
 270        }
 271        wm title $w "[appname] ([reponame]): $t"
 272        tkwait window $w
 273}
 274
 275proc do_restore_defaults {} {
 276        global font_descs default_config repo_config
 277        global repo_config_new global_config_new
 278
 279        foreach name [array names default_config] {
 280                set repo_config_new($name) $default_config($name)
 281                set global_config_new($name) $default_config($name)
 282        }
 283
 284        foreach option $font_descs {
 285                set name [lindex $option 0]
 286                set repo_config(gui.$name) $default_config(gui.$name)
 287        }
 288        apply_config
 289
 290        foreach option $font_descs {
 291                set name [lindex $option 0]
 292                set font [lindex $option 1]
 293                set global_config_new(gui.$font^^family) \
 294                        [font configure $font -family]
 295                set global_config_new(gui.$font^^size) \
 296                        [font configure $font -size]
 297        }
 298}
 299
 300proc do_save_config {w} {
 301        if {![config_check_encodings]} return
 302        if {[catch {save_config} err]} {
 303                error_popup [strcat [mc "Failed to completely save options:"] "\n\n$err"]
 304        }
 305        reshow_diff
 306        destroy $w
 307}