blob: 1d55b49c9bd8182cad2066ecdb81630ca6ad24a6 [file] [log] [blame]
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -04001# git-gui options editor
2# Copyright (C) 2006, 2007 Shawn Pearce
3
Alexander Gavrilov72e6b002008-09-18 01:07:32 +04004proc 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
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040026proc save_config {} {
27 global default_config font_descs
Alexander Gavrilov153ad782008-11-16 21:46:47 +030028 global repo_config global_config system_config
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040029 global repo_config_new global_config_new
Shawn O. Pearce95b002e2008-02-07 02:35:25 -050030 global ui_comm_spell
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040031
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)
Shawn O. Pearcedebcd0f2007-06-02 17:15:56 -040041 font configure ${font}italic \
42 -family $global_config_new(gui.$font^^family) \
43 -size $global_config_new(gui.$font^^size)
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040044 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)} {
Alexander Gavrilov153ad782008-11-16 21:46:47 +030052 if {$value eq $system_config($name)} {
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040053 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 }
Shawn O. Pearce95b002e2008-02-07 02:35:25 -050078
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 }
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040089}
90
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040091proc do_options {} {
92 global repo_config global_config font_descs
93 global repo_config_new global_config_new
Shawn O. Pearce95b002e2008-02-07 02:35:25 -050094 global ui_comm_spell
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -040095
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
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400116 frame $w.buttons
Christian Stimming1ac17952007-07-21 14:21:34 +0200117 button $w.buttons.restore -text [mc "Restore Defaults"] \
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400118 -default normal \
119 -command do_restore_defaults
120 pack $w.buttons.restore -side left
Christian Stimming1ac17952007-07-21 14:21:34 +0200121 button $w.buttons.save -text [mc Save] \
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400122 -default active \
123 -command [list do_save_config $w]
124 pack $w.buttons.save -side right
Christian Stimming1ac17952007-07-21 14:21:34 +0200125 button $w.buttons.cancel -text [mc "Cancel"] \
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400126 -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
Christian Stimming1ac17952007-07-21 14:21:34 +0200131 labelframe $w.repo -text [mc "%s Repository" [reponame]]
132 labelframe $w.global -text [mc "Global (All Repositories)"]
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400133 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 {
Christian Stimming1ac17952007-07-21 14:21:34 +0200138 {t user.name {mc "User Name"}}
139 {t user.email {mc "Email Address"}}
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400140
Christian Stimming1ac17952007-07-21 14:21:34 +0200141 {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"}}
Alexander Gavrilov7e306822008-08-31 00:56:51 +0400144 {t merge.tool {mc "Use Merge Tool"}}
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400145
Christian Stimming1ac17952007-07-21 14:21:34 +0200146 {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"}}
Alexander Gavrilov57cae872008-07-17 00:43:48 +0400149 {b gui.fastcopyblame {mc "Blame Copy Only On Changed Files"}}
150 {i-20..200 gui.copyblamethreshold {mc "Minimum Letters To Blame Copy On"}}
Alexander Gavrilova9c80b82008-08-23 12:30:00 +0400151 {i-0..300 gui.blamehistoryctx {mc "Blame History Context Radius (days)"}}
Clemens Buchacher55ba8a32008-08-30 18:45:27 +0200152 {i-1..99 gui.diffcontext {mc "Number of Diff Context Lines"}}
Adam PiÄ…tyszek11027d52008-03-06 20:38:40 +0100153 {i-0..99 gui.commitmsgwidth {mc "Commit Message Text Width"}}
Christian Stimming1ac17952007-07-21 14:21:34 +0200154 {t gui.newbranchtemplate {mc "New Branch Name Template"}}
Alexander Gavrilov50102c52008-09-18 01:07:33 +0400155 {c gui.encoding {mc "Default File Contents Encoding"}}
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400156 } {
157 set type [lindex $option 0]
158 set name [lindex $option 1]
Christian Stimming1ac17952007-07-21 14:21:34 +0200159 set text [eval [lindex $option 2]]
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400160 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 }
Alexander Gavrilov50102c52008-09-18 01:07:33 +0400185 c -
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400186 t {
187 frame $w.$f.$optid
188 label $w.$f.$optid.l -text "$text:"
189 entry $w.$f.$optid.v \
190 -borderwidth 1 \
191 -relief sunken \
192 -width 20 \
193 -textvariable ${f}_config_new($name)
194 pack $w.$f.$optid.l -side left -anchor w
195 pack $w.$f.$optid.v -side left -anchor w \
196 -fill x -expand 1 \
197 -padx 5
Alexander Gavrilov50102c52008-09-18 01:07:33 +0400198 if {$type eq {c}} {
199 menu $w.$f.$optid.m
200 build_encoding_menu $w.$f.$optid.m \
201 [list set ${f}_config_new($name)] 1
202 button $w.$f.$optid.b \
203 -text [mc "Change"] \
204 -command [list popup_btn_menu \
205 $w.$f.$optid.m $w.$f.$optid.b]
206 pack $w.$f.$optid.b -side left -anchor w
207 }
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400208 pack $w.$f.$optid -side top -anchor w -fill x
209 }
210 }
211 }
212 }
213
Shawn O. Pearce95b002e2008-02-07 02:35:25 -0500214 set all_dicts [linsert \
215 [spellcheck::available_langs] \
216 0 \
217 none]
218 incr optid
219 foreach f {repo global} {
220 if {![info exists ${f}_config_new(gui.spellingdictionary)]} {
221 if {[info exists ui_comm_spell]} {
222 set value [$ui_comm_spell lang]
223 } else {
224 set value none
225 }
226 set ${f}_config_new(gui.spellingdictionary) $value
227 }
228
229 frame $w.$f.$optid
230 label $w.$f.$optid.l -text [mc "Spelling Dictionary:"]
231 eval tk_optionMenu $w.$f.$optid.v \
232 ${f}_config_new(gui.spellingdictionary) \
233 $all_dicts
234 pack $w.$f.$optid.l -side left -anchor w -fill x
Shawn O. Pearce740b9b92008-02-14 01:07:39 -0500235 pack $w.$f.$optid.v -side right -anchor e -padx 5
Shawn O. Pearce95b002e2008-02-07 02:35:25 -0500236 pack $w.$f.$optid -side top -anchor w -fill x
237 }
238 unset all_dicts
239
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400240 set all_fonts [lsort [font families]]
241 foreach option $font_descs {
242 set name [lindex $option 0]
243 set font [lindex $option 1]
Christian Stimming1ac17952007-07-21 14:21:34 +0200244 set text [eval [lindex $option 2]]
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400245
246 set global_config_new(gui.$font^^family) \
247 [font configure $font -family]
248 set global_config_new(gui.$font^^size) \
249 [font configure $font -size]
250
251 frame $w.global.$name
252 label $w.global.$name.l -text "$text:"
Shawn O. Pearceafe20982007-09-13 19:07:46 -0400253 button $w.global.$name.b \
254 -text [mc "Change Font"] \
255 -command [list \
256 choose_font::pick \
257 $w \
258 [mc "Choose %s" $text] \
259 global_config_new(gui.$font^^family) \
260 global_config_new(gui.$font^^size) \
261 ]
262 label $w.global.$name.f -textvariable global_config_new(gui.$font^^family)
263 label $w.global.$name.s -textvariable global_config_new(gui.$font^^size)
264 label $w.global.$name.pt -text [mc "pt."]
265 pack $w.global.$name.l -side left -anchor w
266 pack $w.global.$name.b -side right -anchor e
267 pack $w.global.$name.pt -side right -anchor w
268 pack $w.global.$name.s -side right -anchor w
269 pack $w.global.$name.f -side right -anchor w
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400270 pack $w.global.$name -side top -anchor w -fill x
271 }
272
273 bind $w <Visibility> "grab $w; focus $w.buttons.save"
274 bind $w <Key-Escape> "destroy $w"
275 bind $w <Key-Return> [list do_save_config $w]
Shawn O. Pearce13824e22007-10-07 22:39:08 -0700276
277 if {[is_MacOSX]} {
278 set t [mc "Preferences"]
279 } else {
280 set t [mc "Options"]
281 }
282 wm title $w "[appname] ([reponame]): $t"
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400283 tkwait window $w
284}
285
286proc do_restore_defaults {} {
Alexander Gavrilov153ad782008-11-16 21:46:47 +0300287 global font_descs default_config repo_config system_config
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400288 global repo_config_new global_config_new
289
290 foreach name [array names default_config] {
Alexander Gavrilov153ad782008-11-16 21:46:47 +0300291 set repo_config_new($name) $system_config($name)
292 set global_config_new($name) $system_config($name)
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400293 }
294
295 foreach option $font_descs {
296 set name [lindex $option 0]
Alexander Gavrilov153ad782008-11-16 21:46:47 +0300297 set repo_config(gui.$name) $system_config(gui.$name)
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400298 }
299 apply_config
300
301 foreach option $font_descs {
302 set name [lindex $option 0]
303 set font [lindex $option 1]
304 set global_config_new(gui.$font^^family) \
305 [font configure $font -family]
306 set global_config_new(gui.$font^^size) \
307 [font configure $font -size]
308 }
309}
310
311proc do_save_config {w} {
Alexander Gavrilov72e6b002008-09-18 01:07:32 +0400312 if {![config_check_encodings]} return
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400313 if {[catch {save_config} err]} {
Shawn O. Pearce31bb1d12007-09-14 01:50:09 -0400314 error_popup [strcat [mc "Failed to completely save options:"] "\n\n$err"]
Shawn O. Pearcef522c9b2007-05-07 23:35:48 -0400315 }
316 reshow_diff
317 destroy $w
318}