summaryrefslogtreecommitdiff
path: root/git-gui/lib/encoding.tcl
blob: 32668fc9c6debee6de9882719c305392c1e4791a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# git-gui encoding support
# Copyright (C) 2005 Paul Mackerras <paulus@samba.org>
# (Copied from gitk, commit fd8ccbec4f0161)

# This list of encoding names and aliases is distilled from
# http://www.iana.org/assignments/character-sets.
# Not all of them are supported by Tcl."" ""
	{"Unicode" UTF-8}
	{"Western" ISO-8859-1}}
    {we "West European"
	{"Western" ISO-8859-15 CP-437 CP-850 MacRoman CP-1252 Windows-1252}
	{"Celtic" ISO-8859-14}
	{"Greek" ISO-8859-14 ISO-8859-7 CP-737 CP-869 MacGreek CP-1253 Windows-1253}
	{"Icelandic" MacIceland MacIcelandic CP-861}
	{"Nordic" ISO-8859-10 CP-865}
	{"Portuguese" CP-860}
	{"South European" ISO-8859-3}}
    {ee "East European"
	{"Baltic" CP-775 ISO-8859-4 ISO-8859-13 CP-1257 Windows-1257}
	{"Central European" CP-852 ISO-8859-2 MacCE CP-1250 Windows-1250}
	{"Croatian" MacCroatian}
	{"Cyrillic" CP-855 ISO-8859-5 ISO-IR-111 KOI8-R MacCyrillic CP-1251 Windows-1251}
	{"Russian" CP-866}
	{"Ukrainian" KOI8-U MacUkraine MacUkrainian}
	{"Romanian" ISO-8859-16 MacRomania MacRomanian}}
    {ea "East Asian"
	{"Generic" ISO-2022}
	{"Chinese Simplified" GB2312 GB1988 GB12345 GB2312-RAW GBK EUC-CN GB18030 HZ ISO-2022-CN}
	{"Chinese Traditional" Big5 Big5-HKSCS EUC-TW CP-950}
	{"Japanese" EUC-JP ISO-2022-JP Shift-JIS JIS-0212 JIS-0208 JIS-0201 CP-932 MacJapan}
	{"Korean" EUC-KR UHC JOHAB ISO-2022-KR CP-949 KSC5601}}
    {sa "SE & SW Asian"
	{"Armenian" ARMSCII-8}
	{"Georgian" GEOSTD8}
	{"Thai" TIS-620 ISO-8859-11 CP-874 Windows-874 MacThai}
	{"Turkish" CP-857 CP857 ISO-8859-9 MacTurkish CP-1254 Windows-1254}
	{"Vietnamese" TCVN VISCII VPS CP-1258 Windows-1258}
	{"Hindi" MacDevanagari}
	{"Gujarati" MacGujarati}
	{"Gurmukhi" MacGurmukhi}}
    {me "Middle Eastern"
	{"Arabic" ISO-8859-6 Windows-1256 CP-1256 CP-864 MacArabic}
	{"Farsi" MacFarsi}
	{"Hebrew" ISO-8859-8-I Windows-1255 CP-1255 ISO-8859-8 CP-862 MacHebrew}}
    {mi "Misc"
	{"7-bit" ASCII}
	{"16-bit" Unicode}
	{"Legacy" CP-863 EBCDIC}
	{"Symbol" Symbol Dingbats MacDingbats MacCentEuro}}
}
 
proc build_encoding_table {} {
	global encoding_aliases encoding_lookup_table

	# Prepare the lookup list; cannot use lsort -nocase because
	# of compatibility issues with older Tcl (e.g. in msysgit)
	set names [list]
	foreach item [encoding names] {
		lappend names [list [string tolower $item] $item]
	}
	set names [lsort -ascii -index 0 $names]
	# neither can we use lsearch -index
	set lnames [list]
	foreach item $names {
		lappend lnames [lindex $item 0]
	}
 
	foreach grp $encoding_aliases {
		set target {}
		foreach item $grp {
			set i [lsearch -sorted -ascii $lnames \
					[string tolower $item]]
			if {$i >= 0} {
				set target [lindex $names $i 1]
				break
			}
		}
		if {$target eq {}} continue
		foreach item $grp {
			set encoding_lookup_table([string tolower $item]) $target
		}
	}
 
	foreach item $names {
		set encoding_lookup_table([lindex $item 0]) [lindex $item 1]
	}
}
 
proc tcl_encoding {enc} {
	global encoding_lookup_table
	if {$enc eq {}} {
		return {}
	}
	if {![info exists encoding_lookup_table]} {
		build_encoding_table
	}
	set enc [string tolower $enc]
	if {![info exists encoding_lookup_table($enc)]} {
		# look for "isonnn" instead of "iso-nnn" or "iso_nnn"
		if {[regsub {^(iso|cp|ibm|jis)[-_]} $enc {\1} encx]} {
			set enc $encx
		}
	}
	if {[info exists encoding_lookup_table($enc)]} {
		return $encoding_lookup_table($enc)
	} else {
		return {}
	}
}
 
proc force_path_encoding {path enc} {
	global path_encoding_overrides last_encoding_override
 
	set enc [tcl_encoding $enc]
	if {$enc eq {}} {
		catch { unset last_encoding_override }
		catch { unset path_encoding_overrides($path) }
	} else {
		set last_encoding_override $enc
		if {$path ne {}} {
			set path_encoding_overrides($path) $enc
		}
	}
}
 
proc get_path_encoding {path} {
	global path_encoding_overrides last_encoding_override
 
	if {[info exists last_encoding_override]} {
		set tcl_enc $last_encoding_override
	} else {
		set tcl_enc [tcl_encoding [get_config gui.encoding]]
	}
	if {$tcl_enc eq {}} {
		set tcl_enc [encoding system]
	}
	if {$path ne {}} {
		if {[info exists path_encoding_overrides($path)]} {
			set enc2 $path_encoding_overrides($path)
		} else {
			set enc2 [tcl_encoding [gitattr $path encoding $tcl_enc]]
		}
		if {$enc2 ne {}} {
			set tcl_enc $enc2
		}
	}
	return $tcl_enc
}
 
proc build_encoding_submenu {parent grp cmd} {
	global used_encodings
 
	set mid [lindex $grp 0]
	set gname [mc [lindex $grp 1]]
 
	set smenu {}
	foreach subset [lrange $grp 2 end] {
		set name [mc [lindex $subset 0]]
 
		foreach enc [lrange $subset 1 end] {
			set tcl_enc [tcl_encoding $enc]
			if {$tcl_enc eq {}} continue
 
			if {$smenu eq {}} {
				if {$mid eq {}} {
					set smenu $parent
				} else {
					set smenu "$parent.$mid"
					menu $smenu
					$parent add cascade \
						-label $gname \
						-menu $smenu
				}
			}
 
			if {$name ne {}} {
				set lbl "$name ($enc)"
			} else {
				set lbl $enc
			}
			$smenu add command \
				-label $lbl \
				-command [concat $cmd [list $tcl_enc]]
 
			lappend used_encodings $tcl_enc
		}
	}
}
 
proc popup_btn_menu {m b} {
	tk_popup $m [winfo pointerx $b] [winfo pointery $b]
}
 
proc build_encoding_menu {emenu cmd {nodef 0}} {
	$emenu configure -postcommand \
		[list do_build_encoding_menu $emenu $cmd $nodef]
}
 
proc do_build_encoding_menu {emenu cmd {nodef 0}} {
	global used_encodings encoding_groups
 
	$emenu configure -postcommand {}
 
	if {!$nodef} {
		$emenu add command \
			-label [mc "Default"] \
			-command [concat $cmd [list {}]]
	}
	set sysenc [encoding system]
	$emenu add command \
		-label [mc "System (%s)" $sysenc] \
		-command [concat $cmd [list $sysenc]]

	# Main encoding tree
	set used_encodings [list identity]
	$emenu add separator
	foreach grp $encoding_groups {
		build_encoding_submenu $emenu $grp $cmd
	}

	# Add unclassified encodings
	set unused_grp [list [mc Other]]
	foreach enc [encoding names] {
		if {[lsearch -exact $used_encodings $enc] < 0} {
			lappend unused_grp $enc
		}
	}
	build_encoding_submenu $emenu [list other [mc Other] $unused_grp] $cmd
}