771
|
1 ;;; unicode.el --- Unicode support -*- coding: iso-2022-7bit; -*-
|
|
2
|
778
|
3 ;; Copyright (C) 2001, 2002 Ben Wing.
|
771
|
4
|
|
5 ;; Keywords: multilingual, Unicode
|
|
6
|
|
7 ;; This file is part of XEmacs.
|
|
8
|
|
9 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
10 ;; under the terms of the GNU General Public License as published by
|
|
11 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
12 ;; any later version.
|
|
13
|
|
14 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
17 ;; General Public License for more details.
|
|
18
|
|
19 ;; You should have received a copy of the GNU General Public License
|
|
20 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
21 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
22 ;; 02111-1307, USA.
|
|
23
|
|
24 ;;; Synched up with: Not in FSF.
|
|
25
|
|
26 ;;; Commentary:
|
|
27
|
|
28 ;; Lisp support for Unicode, e.g. initialize the translation tables.
|
|
29
|
|
30 ;;; Code:
|
|
31
|
3659
|
32 ;; GNU Emacs has the charsets:
|
778
|
33
|
3659
|
34 ;; mule-unicode-2500-33ff
|
|
35 ;; mule-unicode-e000-ffff
|
|
36 ;; mule-unicode-0100-24ff
|
778
|
37
|
3659
|
38 ;; built-in. This is hack--and an incomplete hack at that--against the
|
|
39 ;; spirit and the letter of standard ISO 2022 character sets. Instead of
|
|
40 ;; this, we have the jit-ucs-charset-N Mule character sets, created in
|
|
41 ;; unicode.c on encountering a Unicode code point that we don't recognise,
|
|
42 ;; and saved in ISO 2022 coding systems using the UTF-8 escape described in
|
|
43 ;; ISO-IR 196.
|
778
|
44
|
2367
|
45 ;; accessed in loadup.el, mule-cmds.el; see discussion in unicode.c
|
|
46 (defvar load-unicode-tables-at-dump-time (eq system-type 'windows-nt)
|
|
47 "[INTERNAL] Whether to load the Unicode tables at dump time.
|
|
48 Setting this at run-time does nothing.")
|
|
49
|
771
|
50 ;; NOTE: This takes only a fraction of a second on my Pentium III
|
|
51 ;; 700Mhz even with a totally optimization-disabled XEmacs.
|
|
52 (defun load-unicode-tables ()
|
|
53 "Initialize the Unicode translation tables for all standard charsets."
|
780
|
54 (let ((parse-args
|
|
55 '(("unicode/unicode-consortium"
|
877
|
56 ;; Due to the braindamaged way Mule treats the ASCII and Control-1
|
|
57 ;; charsets' types, trying to load them results in out-of-range
|
|
58 ;; warnings at unicode.c:1439. They're no-ops anyway, they're
|
|
59 ;; hardwired in unicode.c (unicode_to_ichar, ichar_to_unicode).
|
|
60 ;; ("8859-1.TXT" ascii #x00 #x7F #x0)
|
|
61 ;; ("8859-1.TXT" control-1 #x80 #x9F #x-80)
|
|
62 ;; The 8859-1.TXT G1 assignments are half no-ops, hardwired in
|
|
63 ;; unicode.c ichar_to_unicode, but not in unicode_to_ichar.
|
780
|
64 ("8859-1.TXT" latin-iso8859-1 #xA0 #xFF #x-80)
|
|
65 ;; "8859-10.TXT"
|
|
66 ;; "8859-13.TXT"
|
|
67 ("8859-14.TXT" latin-iso8859-14 #xA0 #xFF #x-80)
|
|
68 ("8859-15.TXT" latin-iso8859-15 #xA0 #xFF #x-80)
|
2575
|
69 ("8859-16.TXT" latin-iso8859-16 #xA0 #xFF #x-80)
|
780
|
70 ("8859-2.TXT" latin-iso8859-2 #xA0 #xFF #x-80)
|
|
71 ("8859-3.TXT" latin-iso8859-3 #xA0 #xFF #x-80)
|
|
72 ("8859-4.TXT" latin-iso8859-4 #xA0 #xFF #x-80)
|
|
73 ("8859-5.TXT" cyrillic-iso8859-5 #xA0 #xFF #x-80)
|
|
74 ("8859-6.TXT" arabic-iso8859-6 #xA0 #xFF #x-80)
|
|
75 ("8859-7.TXT" greek-iso8859-7 #xA0 #xFF #x-80)
|
|
76 ("8859-8.TXT" hebrew-iso8859-8 #xA0 #xFF #x-80)
|
|
77 ("8859-9.TXT" latin-iso8859-9 #xA0 #xFF #x-80)
|
|
78 ;; charset for Big5 does not matter; specifying `big5' will
|
|
79 ;; automatically make the right thing happen
|
|
80 ("BIG5.TXT" chinese-big5-1 nil nil nil big5)
|
|
81 ("CNS11643.TXT" chinese-cns11643-1 #x10000 #x1FFFF #x-10000)
|
|
82 ("CNS11643.TXT" chinese-cns11643-2 #x20000 #x2FFFF #x-20000)
|
|
83 ;; "CP1250.TXT"
|
|
84 ;; "CP1251.TXT"
|
|
85 ;; "CP1252.TXT"
|
|
86 ;; "CP1253.TXT"
|
|
87 ;; "CP1254.TXT"
|
|
88 ;; "CP1255.TXT"
|
|
89 ;; "CP1256.TXT"
|
|
90 ;; "CP1257.TXT"
|
|
91 ;; "CP1258.TXT"
|
|
92 ;; "CP874.TXT"
|
|
93 ;; "CP932.TXT"
|
|
94 ;; "CP936.TXT"
|
|
95 ;; "CP949.TXT"
|
|
96 ;; "CP950.TXT"
|
|
97 ;; "GB12345.TXT"
|
|
98 ("GB2312.TXT" chinese-gb2312)
|
2297
|
99 ;; "HANGUL.TXT"
|
|
100 ;; #### shouldn't JIS X 0201's upper limit be 7f?
|
780
|
101 ("JIS0201.TXT" latin-jisx0201 #x21 #x80)
|
|
102 ("JIS0201.TXT" katakana-jisx0201 #xA0 #xFF #x-80)
|
|
103 ("JIS0208.TXT" japanese-jisx0208 nil nil nil ignore-first-column)
|
|
104 ("JIS0212.TXT" japanese-jisx0212)
|
|
105 ;; "JOHAB.TXT"
|
|
106 ;; "KOI8-R.TXT"
|
|
107 ;; "KSC5601.TXT"
|
|
108 ;; note that KSC5601.TXT as currently distributed is NOT what
|
|
109 ;; it claims to be! see comments in KSX1001.TXT.
|
|
110 ("KSX1001.TXT" korean-ksc5601)
|
|
111 ;; "OLD5601.TXT"
|
|
112 ;; "SHIFTJIS.TXT"
|
|
113 )
|
|
114 ("unicode/mule-ucs"
|
2297
|
115 ;; #### we don't support surrogates?!??
|
780
|
116 ;; use these instead of the above ones once we support surrogates
|
|
117 ;;("chinese-cns11643-1.txt" chinese-cns11643-1)
|
|
118 ;;("chinese-cns11643-2.txt" chinese-cns11643-2)
|
|
119 ;;("chinese-cns11643-3.txt" chinese-cns11643-3)
|
|
120 ;;("chinese-cns11643-4.txt" chinese-cns11643-4)
|
|
121 ;;("chinese-cns11643-5.txt" chinese-cns11643-5)
|
|
122 ;;("chinese-cns11643-6.txt" chinese-cns11643-6)
|
|
123 ;;("chinese-cns11643-7.txt" chinese-cns11643-7)
|
|
124 ("chinese-sisheng.txt" chinese-sisheng)
|
|
125 ("ethiopic.txt" ethiopic)
|
|
126 ("indian-is13194.txt" indian-is13194)
|
|
127 ("ipa.txt" ipa)
|
|
128 ("thai-tis620.txt" thai-tis620)
|
|
129 ("tibetan.txt" tibetan)
|
|
130 ("vietnamese-viscii-lower.txt" vietnamese-viscii-lower)
|
|
131 ("vietnamese-viscii-upper.txt" vietnamese-viscii-upper)
|
|
132 )
|
|
133 ("unicode/other"
|
|
134 ("lao.txt" lao)
|
|
135 )
|
771
|
136 )))
|
780
|
137 (mapcar #'(lambda (tables)
|
|
138 (let ((undir
|
|
139 (expand-file-name (car tables) data-directory)))
|
|
140 (mapcar #'(lambda (args)
|
1318
|
141 (apply 'load-unicode-mapping-table
|
780
|
142 (expand-file-name (car args) undir)
|
|
143 (cdr args)))
|
|
144 (cdr tables))))
|
771
|
145 parse-args)))
|
|
146
|
|
147 (make-coding-system
|
|
148 'utf-16 'unicode
|
|
149 "UTF-16"
|
|
150 '(mnemonic "UTF-16"
|
3767
|
151 documentation
|
771
|
152 "UTF-16 Unicode encoding -- the standard (almost-) fixed-width
|
|
153 two-byte encoding, with surrogates. It will be fixed-width if all
|
|
154 characters are in the BMP (Basic Multilingual Plane -- first 65536
|
|
155 codepoints). Cannot represent characters with codepoints above
|
|
156 0x10FFFF (a little more than 1,000,000). Unicode and ISO guarantee
|
|
157 never to encode any characters outside this range -- all the rest are
|
|
158 for private, corporate or internal use."
|
3767
|
159 unicode-type utf-16))
|
771
|
160
|
2574
|
161 (define-coding-system-alias 'utf-16-be 'utf-16)
|
|
162
|
771
|
163 (make-coding-system
|
|
164 'utf-16-bom 'unicode
|
|
165 "UTF-16 w/BOM"
|
|
166 '(mnemonic "UTF16-BOM"
|
3767
|
167 documentation
|
771
|
168 "UTF-16 Unicode encoding with byte order mark (BOM) at the beginning.
|
|
169 The BOM is Unicode character U+FEFF -- i.e. the first two bytes are
|
|
170 0xFE and 0xFF, respectively, or reversed in a little-endian
|
|
171 representation. It has been sanctioned by the Unicode Consortium for
|
|
172 use at the beginning of a Unicode stream as a marker of the byte order
|
|
173 of the stream, and commonly appears in Unicode files under Microsoft
|
|
174 Windows, where it also functions as a magic cookie identifying a
|
|
175 Unicode file. The character is called \"ZERO WIDTH NO-BREAK SPACE\"
|
|
176 and is suitable as a byte-order marker because:
|
|
177
|
|
178 -- it has no displayable representation
|
|
179 -- due to its semantics it never normally appears at the beginning
|
|
180 of a stream
|
|
181 -- its reverse U+FFFE is not a legal Unicode character
|
|
182 -- neither byte sequence is at all likely in any other standard
|
|
183 encoding, particularly at the beginning of a stream
|
|
184
|
|
185 This coding system will insert a BOM at the beginning of a stream when
|
|
186 writing and strip it off when reading."
|
3767
|
187 unicode-type utf-16
|
771
|
188 need-bom t))
|
|
189
|
|
190 (make-coding-system
|
|
191 'utf-16-little-endian 'unicode
|
|
192 "UTF-16 Little Endian"
|
|
193 '(mnemonic "UTF16-LE"
|
|
194 documentation
|
|
195 "Little-endian version of UTF-16 Unicode encoding.
|
|
196 See `utf-16' coding system."
|
3767
|
197 unicode-type utf-16
|
771
|
198 little-endian t))
|
|
199
|
2574
|
200 (define-coding-system-alias 'utf-16-le 'utf-16-little-endian)
|
|
201
|
771
|
202 (make-coding-system
|
|
203 'utf-16-little-endian-bom 'unicode
|
|
204 "UTF-16 Little Endian w/BOM"
|
|
205 '(mnemonic "MSW-Unicode"
|
|
206 documentation
|
|
207 "Little-endian version of UTF-16 Unicode encoding, with byte order mark.
|
|
208 Standard encoding for representing Unicode under MS Windows. See
|
|
209 `utf-16-bom' coding system."
|
3767
|
210 unicode-type utf-16
|
771
|
211 little-endian t
|
|
212 need-bom t))
|
|
213
|
|
214 (make-coding-system
|
|
215 'ucs-4 'unicode
|
|
216 "UCS-4"
|
|
217 '(mnemonic "UCS4"
|
|
218 documentation
|
|
219 "UCS-4 Unicode encoding -- fully fixed-width four-byte encoding."
|
3767
|
220 unicode-type ucs-4))
|
771
|
221
|
|
222 (make-coding-system
|
|
223 'ucs-4-little-endian 'unicode
|
|
224 "UCS-4 Little Endian"
|
|
225 '(mnemonic "UCS4-LE"
|
|
226 documentation
|
2297
|
227 ;; #### I don't think this is permitted by ISO 10646, only Unicode.
|
|
228 ;; Call it UTF-32 instead?
|
771
|
229 "Little-endian version of UCS-4 Unicode encoding. See `ucs-4' coding system."
|
3767
|
230 unicode-type ucs-4
|
771
|
231 little-endian t))
|
|
232
|
|
233 (make-coding-system
|
|
234 'utf-8 'unicode
|
|
235 "UTF-8"
|
|
236 '(mnemonic "UTF8"
|
3767
|
237 documentation "
|
|
238 UTF-8 Unicode encoding -- ASCII-compatible 8-bit variable-width encoding
|
2297
|
239 sharing the following principles with the Mule-internal encoding:
|
771
|
240
|
|
241 -- All ASCII characters (codepoints 0 through 127) are represented
|
|
242 by themselves (i.e. using one byte, with the same value as the
|
|
243 ASCII codepoint), and these bytes are disjoint from bytes
|
|
244 representing non-ASCII characters.
|
|
245
|
|
246 This means that any 8-bit clean application can safely process
|
|
247 UTF-8-encoded text as it were ASCII, with no corruption (e.g. a
|
|
248 '/' byte is always a slash character, never the second byte of
|
|
249 some other character, as with Big5, so a pathname encoded in
|
|
250 UTF-8 can safely be split up into components and reassembled
|
|
251 again using standard ASCII processes).
|
|
252
|
|
253 -- Leading bytes and non-leading bytes in the encoding of a
|
|
254 character are disjoint, so moving backwards is easy.
|
|
255
|
|
256 -- Given only the leading byte, you know how many following bytes
|
|
257 are present.
|
|
258 "
|
3767
|
259 unicode-type utf-8))
|
771
|
260
|
985
|
261 (make-coding-system
|
|
262 'utf-8-bom 'unicode
|
|
263 "UTF-8 w/BOM"
|
|
264 '(mnemonic "MSW-UTF8"
|
|
265 documentation
|
|
266 "UTF-8 Unicode encoding, with byte order mark.
|
|
267 Standard encoding for representing UTF-8 under MS Windows."
|
3767
|
268 unicode-type utf-8
|
985
|
269 little-endian t
|
|
270 need-bom t))
|
|
271
|
2633
|
272 (defun decode-char (quote-ucs code &optional restriction)
|
3659
|
273 "FSF compatibility--return Mule character with Unicode codepoint CODE.
|
2633
|
274 The second argument must be 'ucs, the third argument is ignored. "
|
3506
|
275 (assert (eq quote-ucs 'ucs) t
|
2633
|
276 "Sorry, decode-char doesn't yet support anything but the UCS. ")
|
|
277 (unicode-to-char code))
|
|
278
|
|
279 (defun encode-char (char quote-ucs &optional restriction)
|
3659
|
280 "FSF compatibility--return the Unicode code point of CHAR.
|
2633
|
281 The second argument must be 'ucs, the third argument is ignored. "
|
3506
|
282 (assert (eq quote-ucs 'ucs) t
|
2633
|
283 "Sorry, encode-char doesn't yet support anything but the UCS. ")
|
|
284 (char-to-unicode char))
|
|
285
|
3667
|
286 (when (featurep 'mule)
|
|
287 ;; This CCL program is used for displaying the fallback UCS character set,
|
|
288 ;; and can be repurposed to lao and the IPA, all going well.
|
|
289 ;;
|
|
290 ;; define-ccl-program is available after mule-ccl is loaded, much later
|
|
291 ;; than this file in the build process. The below is the result of
|
|
292 ;;
|
|
293 ;; (macroexpand
|
|
294 ;; '(define-ccl-program ccl-encode-to-ucs-2
|
|
295 ;; `(1
|
4072
|
296 ;; ((r1 = (r1 << 7))
|
|
297 ;; (r1 = (r1 | r2))
|
|
298 ;; (mule-to-unicode r0 r1)
|
|
299 ;; (r1 = (r0 >> 8))
|
|
300 ;; (r2 = (r0 & #xff))))
|
3667
|
301 ;; "CCL program to transform Mule characters to UCS-2."))
|
|
302 ;;
|
|
303 ;; and it should occasionally be confirmed that the correspondence still
|
|
304 ;; holds.
|
|
305
|
4072
|
306 (let ((prog [1 10 131127 7 98872 65823 147513 8 82009 255 22]))
|
|
307 (defconst ccl-encode-to-ucs-2
|
|
308 prog
|
3667
|
309 "CCL program to transform Mule characters to UCS-2.")
|
4072
|
310 (put 'ccl-encode-to-ucs-2
|
|
311 'ccl-program-idx
|
|
312 (register-ccl-program 'ccl-encode-to-ucs-2 prog))
|
|
313 nil))
|
3667
|
314
|
771
|
315 ;; #### UTF-7 is not yet implemented, and it's tricky to do. There's
|
|
316 ;; an implementation in appendix A.1 of the Unicode Standard, Version
|
|
317 ;; 2.0, but I don't know its licensing characteristics.
|
|
318
|
|
319 ; (make-coding-system
|
|
320 ; 'utf-7 'unicode
|
|
321 ; "UTF-7"
|
|
322 ; '(mnemonic "UTF7"
|
3659
|
323 ; documentation; "UTF-7 Unicode encoding -- 7-bit-ASCII modal Internet-mail-compatible
|
771
|
324 ; encoding especially designed for headers, with the following
|
|
325 ; properties:
|
|
326
|
|
327 ; -- Only characters that are considered safe for passing through any mail
|
|
328 ; gateway without damage are used.
|
|
329
|
|
330 ; -- This is a modal encoding, with two states. The first, default
|
|
331 ; state encodes the most common Unicode characters (upper and
|
|
332 ; lowercase letters, digits, and 9 common punctuation marks) as
|
|
333 ; themselves, and the second state, entered using '+' and
|
|
334 ; terminated with '-' or any character disallowed in state 2,
|
|
335 ; encodes any Unicode characters by first converting to UTF-16,
|
|
336 ; most significant byte first, and then to a slightly modified
|
|
337 ; Base64 encoding. (Thus, UTF-7 has the same limitations on the
|
|
338 ; characters it can encode as UTF-16.)
|
|
339
|
|
340 ; -- The modified Base64 encoding deviates from standard Base64 in
|
|
341 ; that it omits the `=' pad character. This is eliminated so as to
|
|
342 ; avoid conflicts with the use of `=' as an escape in the
|
|
343 ; Quoted-Printable encoding and the related Q encoding for headers:
|
|
344 ; With this modification, non-whitespace chars in UTF-7 will be
|
|
345 ; represented in Quoted-Printable and in Q as-is, with no further
|
|
346 ; encoding.
|
|
347
|
|
348 ; For more information, see Appendix A.1 of The Unicode Standard 2.0, or
|
|
349 ; wherever it is in v3.0."
|
3767
|
350 ; unicode-type utf-7))
|