Merge branch 'kb/path-max-must-go'
[git] / git-gui / lib / option.tcl
1 # git-gui options editor
2 # Copyright (C) 2006, 2007 Shawn Pearce
3
4 proc 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
26 proc save_config {} {
27         global default_config font_descs
28         global repo_config global_config system_config
29         global repo_config_new global_config_new
30         global ui_comm_spell
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)
41                 font configure ${font}italic \
42                         -family $global_config_new(gui.$font^^family) \
43                         -size $global_config_new(gui.$font^^size)
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)} {
52                         if {$value eq $system_config($name)} {
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         }
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         }
89 }
90
91 proc do_options {} {
92         global repo_config global_config font_descs
93         global repo_config_new global_config_new
94         global ui_comm_spell use_ttk NS
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
113         Dialog $w
114         wm withdraw $w
115         wm transient $w [winfo parent $w]
116         wm geometry $w "+[winfo rootx .]+[winfo rooty .]"
117
118         ${NS}::frame $w.buttons
119         ${NS}::button $w.buttons.restore -text [mc "Restore Defaults"] \
120                 -default normal \
121                 -command do_restore_defaults
122         pack $w.buttons.restore -side left
123         ${NS}::button $w.buttons.save -text [mc Save] \
124                 -default active \
125                 -command [list do_save_config $w]
126         pack $w.buttons.save -side right
127         ${NS}::button $w.buttons.cancel -text [mc "Cancel"] \
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
133         ${NS}::labelframe $w.repo -text [mc "%s Repository" [reponame]]
134         ${NS}::labelframe $w.global -text [mc "Global (All Repositories)"]
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 {
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"}}
146                 {t merge.tool {mc "Use Merge Tool"}}
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"}}
151                 {b gui.textconv {mc "Use Textconv For Diffs and Blames"}}
152                 {b gui.fastcopyblame {mc "Blame Copy Only On Changed Files"}}
153                 {i-0..100 gui.maxrecentrepo {mc "Maximum Length of Recent Repositories List"}}
154                 {i-20..200 gui.copyblamethreshold {mc "Minimum Letters To Blame Copy On"}}
155                 {i-0..300 gui.blamehistoryctx {mc "Blame History Context Radius (days)"}}
156                 {i-1..99 gui.diffcontext {mc "Number of Diff Context Lines"}}
157                 {t gui.diffopts {mc "Additional Diff Parameters"}}
158                 {i-0..99 gui.commitmsgwidth {mc "Commit Message Text Width"}}
159                 {t gui.newbranchtemplate {mc "New Branch Name Template"}}
160                 {c gui.encoding {mc "Default File Contents Encoding"}}
161                 {b gui.warndetachedcommit {mc "Warn before committing to a detached head"}}
162                 {s gui.stageuntracked {mc "Staging of untracked files"} {list "yes" "no" "ask"}}
163                 {b gui.displayuntracked {mc "Show untracked files"}}
164                 } {
165                 set type [lindex $option 0]
166                 set name [lindex $option 1]
167                 set text [eval [lindex $option 2]]
168                 incr optid
169                 foreach f {repo global} {
170                         switch -glob -- $type {
171                         b {
172                                 ${NS}::checkbutton $w.$f.$optid -text $text \
173                                         -variable ${f}_config_new($name) \
174                                         -onvalue true \
175                                         -offvalue false
176                                 pack $w.$f.$optid -side top -anchor w
177                         }
178                         i-* {
179                                 regexp -- {-(\d+)\.\.(\d+)$} $type _junk min max
180                                 ${NS}::frame $w.$f.$optid
181                                 ${NS}::label $w.$f.$optid.l -text "$text:"
182                                 pack $w.$f.$optid.l -side left -anchor w -fill x
183                                 tspinbox $w.$f.$optid.v \
184                                         -textvariable ${f}_config_new($name) \
185                                         -from $min \
186                                         -to $max \
187                                         -increment 1 \
188                                         -width [expr {1 + [string length $max]}]
189                                 bind $w.$f.$optid.v <FocusIn> {%W selection range 0 end}
190                                 pack $w.$f.$optid.v -side right -anchor e -padx 5
191                                 pack $w.$f.$optid -side top -anchor w -fill x
192                         }
193                         c -
194                         t {
195                                 ${NS}::frame $w.$f.$optid
196                                 ${NS}::label $w.$f.$optid.l -text "$text:"
197                                 ${NS}::entry $w.$f.$optid.v \
198                                         -width 20 \
199                                         -textvariable ${f}_config_new($name)
200                                 pack $w.$f.$optid.l -side left -anchor w
201                                 pack $w.$f.$optid.v -side left -anchor w \
202                                         -fill x -expand 1 \
203                                         -padx 5
204                                 if {$type eq {c}} {
205                                         menu $w.$f.$optid.m
206                                         build_encoding_menu $w.$f.$optid.m \
207                                                 [list set ${f}_config_new($name)] 1
208                                         ${NS}::button $w.$f.$optid.b \
209                                                 -text [mc "Change"] \
210                                                 -command [list popup_btn_menu \
211                                                         $w.$f.$optid.m $w.$f.$optid.b]
212                                         pack $w.$f.$optid.b -side left -anchor w
213                                 }
214                                 pack $w.$f.$optid -side top -anchor w -fill x
215                         }
216                         s {
217                                 set opts [eval [lindex $option 3]]
218                                 ${NS}::frame $w.$f.$optid
219                                 ${NS}::label $w.$f.$optid.l -text "$text:"
220                                 if {$use_ttk} {
221                                         ttk::combobox $w.$f.$optid.v \
222                                                 -textvariable ${f}_config_new($name) \
223                                                 -values $opts -state readonly
224                                 } else {
225                                         eval tk_optionMenu $w.$f.$optid.v \
226                                                 ${f}_config_new($name) \
227                                                 $opts
228                                 }
229                                 pack $w.$f.$optid.l -side left -anchor w -fill x
230                                 pack $w.$f.$optid.v -side right -anchor e -padx 5
231                                 pack $w.$f.$optid -side top -anchor w -fill x
232                         }
233                         }
234                 }
235         }
236
237         set all_dicts [linsert \
238                 [spellcheck::available_langs] \
239                 0 \
240                 none]
241         incr optid
242         foreach f {repo global} {
243                 if {![info exists ${f}_config_new(gui.spellingdictionary)]} {
244                         if {[info exists ui_comm_spell]} {
245                                 set value [$ui_comm_spell lang]
246                         } else {
247                                 set value none
248                         }
249                         set ${f}_config_new(gui.spellingdictionary) $value
250                 }
251
252                 ${NS}::frame $w.$f.$optid
253                 ${NS}::label $w.$f.$optid.l -text [mc "Spelling Dictionary:"]
254                 if {$use_ttk} {
255                         ttk::combobox $w.$f.$optid.v \
256                                 -textvariable ${f}_config_new(gui.spellingdictionary) \
257                                 -values $all_dicts -state readonly
258                 } else {
259                         eval tk_optionMenu $w.$f.$optid.v \
260                                 ${f}_config_new(gui.spellingdictionary) \
261                                 $all_dicts
262                 }
263                 pack $w.$f.$optid.l -side left -anchor w -fill x
264                 pack $w.$f.$optid.v -side right -anchor e -padx 5
265                 pack $w.$f.$optid -side top -anchor w -fill x
266         }
267         unset all_dicts
268
269         set all_fonts [lsort [font families]]
270         foreach option $font_descs {
271                 set name [lindex $option 0]
272                 set font [lindex $option 1]
273                 set text [eval [lindex $option 2]]
274
275                 set global_config_new(gui.$font^^family) \
276                         [font configure $font -family]
277                 set global_config_new(gui.$font^^size) \
278                         [font configure $font -size]
279
280                 ${NS}::frame $w.global.$name
281                 ${NS}::label $w.global.$name.l -text "$text:"
282                 ${NS}::button $w.global.$name.b \
283                         -text [mc "Change Font"] \
284                         -command [list \
285                                 tchoosefont \
286                                 $w \
287                                 [mc "Choose %s" $text] \
288                                 global_config_new(gui.$font^^family) \
289                                 global_config_new(gui.$font^^size) \
290                                 ]
291                 ${NS}::label $w.global.$name.f -textvariable global_config_new(gui.$font^^family)
292                 ${NS}::label $w.global.$name.s -textvariable global_config_new(gui.$font^^size)
293                 ${NS}::label $w.global.$name.pt -text [mc "pt."]
294                 pack $w.global.$name.l -side left -anchor w
295                 pack $w.global.$name.b -side right -anchor e
296                 pack $w.global.$name.pt -side right -anchor w
297                 pack $w.global.$name.s -side right -anchor w
298                 pack $w.global.$name.f -side right -anchor w
299                 pack $w.global.$name -side top -anchor w -fill x
300         }
301
302         bind $w <Visibility> "grab $w; focus $w.buttons.save"
303         bind $w <Key-Escape> "destroy $w"
304         bind $w <Key-Return> [list do_save_config $w]
305
306         if {[is_MacOSX]} {
307                 set t [mc "Preferences"]
308         } else {
309                 set t [mc "Options"]
310         }
311         wm title $w "[appname] ([reponame]): $t"
312         wm deiconify $w
313         tkwait window $w
314 }
315
316 proc do_restore_defaults {} {
317         global font_descs default_config repo_config system_config
318         global repo_config_new global_config_new
319
320         foreach name [array names default_config] {
321                 set repo_config_new($name) $system_config($name)
322                 set global_config_new($name) $system_config($name)
323         }
324
325         foreach option $font_descs {
326                 set name [lindex $option 0]
327                 set repo_config(gui.$name) $system_config(gui.$name)
328         }
329         apply_config
330
331         foreach option $font_descs {
332                 set name [lindex $option 0]
333                 set font [lindex $option 1]
334                 set global_config_new(gui.$font^^family) \
335                         [font configure $font -family]
336                 set global_config_new(gui.$font^^size) \
337                         [font configure $font -size]
338         }
339 }
340
341 proc do_save_config {w} {
342         if {![config_check_encodings]} return
343         if {[catch {save_config} err]} {
344                 error_popup [strcat [mc "Failed to completely save options:"] "\n\n$err"]
345         }
346         reshow_diff
347         destroy $w
348 }