]> git.ipfire.org Git - thirdparty/git.git/blame - git-gui/lib/option.tcl
Merge git://repo.or.cz/git-gui into pt/git-gui
[thirdparty/git.git] / git-gui / lib / option.tcl
CommitLineData
f522c9b5
SP
1# git-gui options editor
2# Copyright (C) 2006, 2007 Shawn Pearce
3
72e6b002
AG
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
f522c9b5
SP
26proc save_config {} {
27 global default_config font_descs
153ad78b 28 global repo_config global_config system_config
f522c9b5 29 global repo_config_new global_config_new
95b002ee 30 global ui_comm_spell
f522c9b5
SP
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)
debcd0fd
SP
41 font configure ${font}italic \
42 -family $global_config_new(gui.$font^^family) \
43 -size $global_config_new(gui.$font^^size)
f522c9b5
SP
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)} {
153ad78b 52 if {$value eq $system_config($name)} {
f522c9b5
SP
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 }
95b002ee
SP
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 }
f522c9b5
SP
89}
90
f522c9b5
SP
91proc do_options {} {
92 global repo_config global_config font_descs
93 global repo_config_new global_config_new
c80d7be5 94 global ui_comm_spell use_ttk NS
f522c9b5
SP
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
c80d7be5
PT
113 Dialog $w
114 wm withdraw $w
115 wm transient $w [winfo parent $w]
f522c9b5
SP
116 wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
117
c80d7be5
PT
118 ${NS}::frame $w.buttons
119 ${NS}::button $w.buttons.restore -text [mc "Restore Defaults"] \
f522c9b5
SP
120 -default normal \
121 -command do_restore_defaults
122 pack $w.buttons.restore -side left
c80d7be5 123 ${NS}::button $w.buttons.save -text [mc Save] \
f522c9b5
SP
124 -default active \
125 -command [list do_save_config $w]
126 pack $w.buttons.save -side right
c80d7be5 127 ${NS}::button $w.buttons.cancel -text [mc "Cancel"] \
f522c9b5
SP
128 -default normal \
129 -command [list destroy $w]
130 pack $w.buttons.cancel -side right -padx 5
131 pack $w.buttons -side bottom -fill x -pady 10 -padx 10
132
c80d7be5
PT
133 ${NS}::labelframe $w.repo -text [mc "%s Repository" [reponame]]
134 ${NS}::labelframe $w.global -text [mc "Global (All Repositories)"]
f522c9b5
SP
135 pack $w.repo -side left -fill both -expand 1 -pady 5 -padx 5
136 pack $w.global -side right -fill both -expand 1 -pady 5 -padx 5
137
138 set optid 0
139 foreach option {
1ac17950
CS
140 {t user.name {mc "User Name"}}
141 {t user.email {mc "Email Address"}}
142
143 {b merge.summary {mc "Summarize Merge Commits"}}
144 {i-1..5 merge.verbosity {mc "Merge Verbosity"}}
145 {b merge.diffstat {mc "Show Diffstat After Merge"}}
7e30682c 146 {t merge.tool {mc "Use Merge Tool"}}
1ac17950
CS
147
148 {b gui.trustmtime {mc "Trust File Modification Timestamps"}}
149 {b gui.pruneduringfetch {mc "Prune Tracking Branches During Fetch"}}
150 {b gui.matchtrackingbranch {mc "Match Tracking Branches"}}
1fbaccad 151 {b gui.textconv {mc "Use Textconv For Diffs and Blames"}}
57cae87b
AG
152 {b gui.fastcopyblame {mc "Blame Copy Only On Changed Files"}}
153 {i-20..200 gui.copyblamethreshold {mc "Minimum Letters To Blame Copy On"}}
a9c80b83 154 {i-0..300 gui.blamehistoryctx {mc "Blame History Context Radius (days)"}}
55ba8a34 155 {i-1..99 gui.diffcontext {mc "Number of Diff Context Lines"}}
11027d54 156 {i-0..99 gui.commitmsgwidth {mc "Commit Message Text Width"}}
1ac17950 157 {t gui.newbranchtemplate {mc "New Branch Name Template"}}
50102c56 158 {c gui.encoding {mc "Default File Contents Encoding"}}
f522c9b5
SP
159 } {
160 set type [lindex $option 0]
161 set name [lindex $option 1]
1ac17950 162 set text [eval [lindex $option 2]]
f522c9b5
SP
163 incr optid
164 foreach f {repo global} {
165 switch -glob -- $type {
166 b {
c80d7be5 167 ${NS}::checkbutton $w.$f.$optid -text $text \
f522c9b5
SP
168 -variable ${f}_config_new($name) \
169 -onvalue true \
170 -offvalue false
171 pack $w.$f.$optid -side top -anchor w
172 }
173 i-* {
174 regexp -- {-(\d+)\.\.(\d+)$} $type _junk min max
c80d7be5
PT
175 ${NS}::frame $w.$f.$optid
176 ${NS}::label $w.$f.$optid.l -text "$text:"
f522c9b5 177 pack $w.$f.$optid.l -side left -anchor w -fill x
c80d7be5 178 tspinbox $w.$f.$optid.v \
f522c9b5
SP
179 -textvariable ${f}_config_new($name) \
180 -from $min \
181 -to $max \
182 -increment 1 \
183 -width [expr {1 + [string length $max]}]
184 bind $w.$f.$optid.v <FocusIn> {%W selection range 0 end}
185 pack $w.$f.$optid.v -side right -anchor e -padx 5
186 pack $w.$f.$optid -side top -anchor w -fill x
187 }
50102c56 188 c -
f522c9b5 189 t {
c80d7be5
PT
190 ${NS}::frame $w.$f.$optid
191 ${NS}::label $w.$f.$optid.l -text "$text:"
192 ${NS}::entry $w.$f.$optid.v \
f522c9b5
SP
193 -width 20 \
194 -textvariable ${f}_config_new($name)
195 pack $w.$f.$optid.l -side left -anchor w
196 pack $w.$f.$optid.v -side left -anchor w \
197 -fill x -expand 1 \
198 -padx 5
50102c56
AG
199 if {$type eq {c}} {
200 menu $w.$f.$optid.m
201 build_encoding_menu $w.$f.$optid.m \
202 [list set ${f}_config_new($name)] 1
c80d7be5 203 ${NS}::button $w.$f.$optid.b \
50102c56
AG
204 -text [mc "Change"] \
205 -command [list popup_btn_menu \
206 $w.$f.$optid.m $w.$f.$optid.b]
207 pack $w.$f.$optid.b -side left -anchor w
208 }
f522c9b5
SP
209 pack $w.$f.$optid -side top -anchor w -fill x
210 }
211 }
212 }
213 }
214
95b002ee
SP
215 set all_dicts [linsert \
216 [spellcheck::available_langs] \
217 0 \
218 none]
219 incr optid
220 foreach f {repo global} {
221 if {![info exists ${f}_config_new(gui.spellingdictionary)]} {
222 if {[info exists ui_comm_spell]} {
223 set value [$ui_comm_spell lang]
224 } else {
225 set value none
226 }
227 set ${f}_config_new(gui.spellingdictionary) $value
228 }
229
c80d7be5
PT
230 ${NS}::frame $w.$f.$optid
231 ${NS}::label $w.$f.$optid.l -text [mc "Spelling Dictionary:"]
232 if {$use_ttk} {
233 ttk::combobox $w.$f.$optid.v \
234 -textvariable ${f}_config_new(gui.spellingdictionary) \
235 -values $all_dicts -state readonly
236 } else {
237 eval tk_optionMenu $w.$f.$optid.v \
238 ${f}_config_new(gui.spellingdictionary) \
239 $all_dicts
240 }
95b002ee 241 pack $w.$f.$optid.l -side left -anchor w -fill x
740b9b9f 242 pack $w.$f.$optid.v -side right -anchor e -padx 5
95b002ee
SP
243 pack $w.$f.$optid -side top -anchor w -fill x
244 }
245 unset all_dicts
246
f522c9b5
SP
247 set all_fonts [lsort [font families]]
248 foreach option $font_descs {
249 set name [lindex $option 0]
250 set font [lindex $option 1]
1ac17950 251 set text [eval [lindex $option 2]]
f522c9b5
SP
252
253 set global_config_new(gui.$font^^family) \
254 [font configure $font -family]
255 set global_config_new(gui.$font^^size) \
256 [font configure $font -size]
257
c80d7be5
PT
258 ${NS}::frame $w.global.$name
259 ${NS}::label $w.global.$name.l -text "$text:"
260 ${NS}::button $w.global.$name.b \
afe2098d
SP
261 -text [mc "Change Font"] \
262 -command [list \
c80d7be5 263 tchoosefont \
afe2098d
SP
264 $w \
265 [mc "Choose %s" $text] \
266 global_config_new(gui.$font^^family) \
267 global_config_new(gui.$font^^size) \
268 ]
c80d7be5
PT
269 ${NS}::label $w.global.$name.f -textvariable global_config_new(gui.$font^^family)
270 ${NS}::label $w.global.$name.s -textvariable global_config_new(gui.$font^^size)
271 ${NS}::label $w.global.$name.pt -text [mc "pt."]
afe2098d
SP
272 pack $w.global.$name.l -side left -anchor w
273 pack $w.global.$name.b -side right -anchor e
274 pack $w.global.$name.pt -side right -anchor w
275 pack $w.global.$name.s -side right -anchor w
276 pack $w.global.$name.f -side right -anchor w
f522c9b5
SP
277 pack $w.global.$name -side top -anchor w -fill x
278 }
279
280 bind $w <Visibility> "grab $w; focus $w.buttons.save"
281 bind $w <Key-Escape> "destroy $w"
282 bind $w <Key-Return> [list do_save_config $w]
13824e2d
SP
283
284 if {[is_MacOSX]} {
285 set t [mc "Preferences"]
286 } else {
287 set t [mc "Options"]
288 }
289 wm title $w "[appname] ([reponame]): $t"
c80d7be5 290 wm deiconify $w
f522c9b5
SP
291 tkwait window $w
292}
293
294proc do_restore_defaults {} {
153ad78b 295 global font_descs default_config repo_config system_config
f522c9b5
SP
296 global repo_config_new global_config_new
297
298 foreach name [array names default_config] {
153ad78b
AG
299 set repo_config_new($name) $system_config($name)
300 set global_config_new($name) $system_config($name)
f522c9b5
SP
301 }
302
303 foreach option $font_descs {
304 set name [lindex $option 0]
153ad78b 305 set repo_config(gui.$name) $system_config(gui.$name)
f522c9b5
SP
306 }
307 apply_config
308
309 foreach option $font_descs {
310 set name [lindex $option 0]
311 set font [lindex $option 1]
312 set global_config_new(gui.$font^^family) \
313 [font configure $font -family]
314 set global_config_new(gui.$font^^size) \
315 [font configure $font -size]
316 }
317}
318
319proc do_save_config {w} {
72e6b002 320 if {![config_check_encodings]} return
f522c9b5 321 if {[catch {save_config} err]} {
31bb1d1b 322 error_popup [strcat [mc "Failed to completely save options:"] "\n\n$err"]
f522c9b5
SP
323 }
324 reshow_diff
325 destroy $w
326}