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