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 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
54proc do_about {} {
55 global appvers copyright oguilib
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 set d {}
95 append d "git exec dir: [gitexec]\n"
96 append d "git-gui lib: $oguilib"
97
98 label $w.vers \
99 -text $v \
100 -padx 5 -pady 5 \
101 -justify left \
102 -anchor w \
103 -borderwidth 1 \
104 -relief solid
105 pack $w.vers -side top -fill x -padx 5 -pady 5
106
107 label $w.dirs \
108 -text $d \
109 -padx 5 -pady 5 \
110 -justify left \
111 -anchor w \
112 -borderwidth 1 \
113 -relief solid
114 pack $w.dirs -side top -fill x -padx 5 -pady 5
115
116 menu $w.ctxm -tearoff 0
117 $w.ctxm add command \
118 -label {Copy} \
119 -command "
120 clipboard clear
121 clipboard append -format STRING -type STRING -- \[$w.vers cget -text\]
122 "
123
124 bind $w <Visibility> "grab $w; focus $w.buttons.close"
125 bind $w <Key-Escape> "destroy $w"
126 bind $w <Key-Return> "destroy $w"
127 bind_button3 $w.vers "tk_popup $w.ctxm %X %Y; grab $w; focus $w"
128 wm title $w "About [appname]"
129 tkwait window $w
130}
131
132proc do_options {} {
133 global repo_config global_config font_descs
134 global repo_config_new global_config_new
135
136 array unset repo_config_new
137 array unset global_config_new
138 foreach name [array names repo_config] {
139 set repo_config_new($name) $repo_config($name)
140 }
141 load_config 1
142 foreach name [array names repo_config] {
143 switch -- $name {
144 gui.diffcontext {continue}
145 }
146 set repo_config_new($name) $repo_config($name)
147 }
148 foreach name [array names global_config] {
149 set global_config_new($name) $global_config($name)
150 }
151
152 set w .options_editor
153 toplevel $w
154 wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
155
156 label $w.header -text "Options" \
157 -font font_uibold
158 pack $w.header -side top -fill x
159
160 frame $w.buttons
161 button $w.buttons.restore -text {Restore Defaults} \
162 -default normal \
163 -command do_restore_defaults
164 pack $w.buttons.restore -side left
165 button $w.buttons.save -text Save \
166 -default active \
167 -command [list do_save_config $w]
168 pack $w.buttons.save -side right
169 button $w.buttons.cancel -text {Cancel} \
170 -default normal \
171 -command [list destroy $w]
172 pack $w.buttons.cancel -side right -padx 5
173 pack $w.buttons -side bottom -fill x -pady 10 -padx 10
174
175 labelframe $w.repo -text "[reponame] Repository"
176 labelframe $w.global -text {Global (All Repositories)}
177 pack $w.repo -side left -fill both -expand 1 -pady 5 -padx 5
178 pack $w.global -side right -fill both -expand 1 -pady 5 -padx 5
179
180 set optid 0
181 foreach option {
182 {t user.name {User Name}}
183 {t user.email {Email Address}}
184
185 {b merge.summary {Summarize Merge Commits}}
186 {i-1..5 merge.verbosity {Merge Verbosity}}
187
188 {b gui.trustmtime {Trust File Modification Timestamps}}
189 {b gui.pruneduringfetch {Prune Tracking Branches During Fetch}}
190 {i-1..99 gui.diffcontext {Number of Diff Context Lines}}
191 {t gui.newbranchtemplate {New Branch Name Template}}
192 } {
193 set type [lindex $option 0]
194 set name [lindex $option 1]
195 set text [lindex $option 2]
196 incr optid
197 foreach f {repo global} {
198 switch -glob -- $type {
199 b {
200 checkbutton $w.$f.$optid -text $text \
201 -variable ${f}_config_new($name) \
202 -onvalue true \
203 -offvalue false
204 pack $w.$f.$optid -side top -anchor w
205 }
206 i-* {
207 regexp -- {-(\d+)\.\.(\d+)$} $type _junk min max
208 frame $w.$f.$optid
209 label $w.$f.$optid.l -text "$text:"
210 pack $w.$f.$optid.l -side left -anchor w -fill x
211 spinbox $w.$f.$optid.v \
212 -textvariable ${f}_config_new($name) \
213 -from $min \
214 -to $max \
215 -increment 1 \
216 -width [expr {1 + [string length $max]}]
217 bind $w.$f.$optid.v <FocusIn> {%W selection range 0 end}
218 pack $w.$f.$optid.v -side right -anchor e -padx 5
219 pack $w.$f.$optid -side top -anchor w -fill x
220 }
221 t {
222 frame $w.$f.$optid
223 label $w.$f.$optid.l -text "$text:"
224 entry $w.$f.$optid.v \
225 -borderwidth 1 \
226 -relief sunken \
227 -width 20 \
228 -textvariable ${f}_config_new($name)
229 pack $w.$f.$optid.l -side left -anchor w
230 pack $w.$f.$optid.v -side left -anchor w \
231 -fill x -expand 1 \
232 -padx 5
233 pack $w.$f.$optid -side top -anchor w -fill x
234 }
235 }
236 }
237 }
238
239 set all_fonts [lsort [font families]]
240 foreach option $font_descs {
241 set name [lindex $option 0]
242 set font [lindex $option 1]
243 set text [lindex $option 2]
244
245 set global_config_new(gui.$font^^family) \
246 [font configure $font -family]
247 set global_config_new(gui.$font^^size) \
248 [font configure $font -size]
249
250 frame $w.global.$name
251 label $w.global.$name.l -text "$text:"
252 pack $w.global.$name.l -side left -anchor w -fill x
253 eval tk_optionMenu $w.global.$name.family \
254 global_config_new(gui.$font^^family) \
255 $all_fonts
256 spinbox $w.global.$name.size \
257 -textvariable global_config_new(gui.$font^^size) \
258 -from 2 -to 80 -increment 1 \
259 -width 3
260 bind $w.global.$name.size <FocusIn> {%W selection range 0 end}
261 pack $w.global.$name.size -side right -anchor e
262 pack $w.global.$name.family -side right -anchor e
263 pack $w.global.$name -side top -anchor w -fill x
264 }
265
266 bind $w <Visibility> "grab $w; focus $w.buttons.save"
267 bind $w <Key-Escape> "destroy $w"
268 bind $w <Key-Return> [list do_save_config $w]
269 wm title $w "[appname] ([reponame]): Options"
270 tkwait window $w
271}
272
273proc do_restore_defaults {} {
274 global font_descs default_config repo_config
275 global repo_config_new global_config_new
276
277 foreach name [array names default_config] {
278 set repo_config_new($name) $default_config($name)
279 set global_config_new($name) $default_config($name)
280 }
281
282 foreach option $font_descs {
283 set name [lindex $option 0]
284 set repo_config(gui.$name) $default_config(gui.$name)
285 }
286 apply_config
287
288 foreach option $font_descs {
289 set name [lindex $option 0]
290 set font [lindex $option 1]
291 set global_config_new(gui.$font^^family) \
292 [font configure $font -family]
293 set global_config_new(gui.$font^^size) \
294 [font configure $font -size]
295 }
296}
297
298proc do_save_config {w} {
299 if {[catch {save_config} err]} {
300 error_popup "Failed to completely save options:\n\n$err"
301 }
302 reshow_diff
303 destroy $w
304}