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