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
|
4083
|
45 (eval-when-compile (when (featurep 'mule) (require 'ccl)))
|
|
46
|
2367
|
47 ;; accessed in loadup.el, mule-cmds.el; see discussion in unicode.c
|
|
48 (defvar load-unicode-tables-at-dump-time (eq system-type 'windows-nt)
|
|
49 "[INTERNAL] Whether to load the Unicode tables at dump time.
|
|
50 Setting this at run-time does nothing.")
|
|
51
|
771
|
52 ;; NOTE: This takes only a fraction of a second on my Pentium III
|
|
53 ;; 700Mhz even with a totally optimization-disabled XEmacs.
|
|
54 (defun load-unicode-tables ()
|
|
55 "Initialize the Unicode translation tables for all standard charsets."
|
780
|
56 (let ((parse-args
|
|
57 '(("unicode/unicode-consortium"
|
877
|
58 ;; Due to the braindamaged way Mule treats the ASCII and Control-1
|
|
59 ;; charsets' types, trying to load them results in out-of-range
|
|
60 ;; warnings at unicode.c:1439. They're no-ops anyway, they're
|
|
61 ;; hardwired in unicode.c (unicode_to_ichar, ichar_to_unicode).
|
|
62 ;; ("8859-1.TXT" ascii #x00 #x7F #x0)
|
|
63 ;; ("8859-1.TXT" control-1 #x80 #x9F #x-80)
|
|
64 ;; The 8859-1.TXT G1 assignments are half no-ops, hardwired in
|
|
65 ;; unicode.c ichar_to_unicode, but not in unicode_to_ichar.
|
780
|
66 ("8859-1.TXT" latin-iso8859-1 #xA0 #xFF #x-80)
|
|
67 ;; "8859-10.TXT"
|
|
68 ;; "8859-13.TXT"
|
|
69 ("8859-14.TXT" latin-iso8859-14 #xA0 #xFF #x-80)
|
|
70 ("8859-15.TXT" latin-iso8859-15 #xA0 #xFF #x-80)
|
2575
|
71 ("8859-16.TXT" latin-iso8859-16 #xA0 #xFF #x-80)
|
780
|
72 ("8859-2.TXT" latin-iso8859-2 #xA0 #xFF #x-80)
|
|
73 ("8859-3.TXT" latin-iso8859-3 #xA0 #xFF #x-80)
|
|
74 ("8859-4.TXT" latin-iso8859-4 #xA0 #xFF #x-80)
|
|
75 ("8859-5.TXT" cyrillic-iso8859-5 #xA0 #xFF #x-80)
|
|
76 ("8859-6.TXT" arabic-iso8859-6 #xA0 #xFF #x-80)
|
|
77 ("8859-7.TXT" greek-iso8859-7 #xA0 #xFF #x-80)
|
|
78 ("8859-8.TXT" hebrew-iso8859-8 #xA0 #xFF #x-80)
|
|
79 ("8859-9.TXT" latin-iso8859-9 #xA0 #xFF #x-80)
|
|
80 ;; charset for Big5 does not matter; specifying `big5' will
|
|
81 ;; automatically make the right thing happen
|
|
82 ("BIG5.TXT" chinese-big5-1 nil nil nil big5)
|
|
83 ("CNS11643.TXT" chinese-cns11643-1 #x10000 #x1FFFF #x-10000)
|
|
84 ("CNS11643.TXT" chinese-cns11643-2 #x20000 #x2FFFF #x-20000)
|
|
85 ;; "CP1250.TXT"
|
|
86 ;; "CP1251.TXT"
|
|
87 ;; "CP1252.TXT"
|
|
88 ;; "CP1253.TXT"
|
|
89 ;; "CP1254.TXT"
|
|
90 ;; "CP1255.TXT"
|
|
91 ;; "CP1256.TXT"
|
|
92 ;; "CP1257.TXT"
|
|
93 ;; "CP1258.TXT"
|
|
94 ;; "CP874.TXT"
|
|
95 ;; "CP932.TXT"
|
|
96 ;; "CP936.TXT"
|
|
97 ;; "CP949.TXT"
|
|
98 ;; "CP950.TXT"
|
|
99 ;; "GB12345.TXT"
|
|
100 ("GB2312.TXT" chinese-gb2312)
|
2297
|
101 ;; "HANGUL.TXT"
|
|
102 ;; #### shouldn't JIS X 0201's upper limit be 7f?
|
780
|
103 ("JIS0201.TXT" latin-jisx0201 #x21 #x80)
|
|
104 ("JIS0201.TXT" katakana-jisx0201 #xA0 #xFF #x-80)
|
|
105 ("JIS0208.TXT" japanese-jisx0208 nil nil nil ignore-first-column)
|
|
106 ("JIS0212.TXT" japanese-jisx0212)
|
|
107 ;; "JOHAB.TXT"
|
|
108 ;; "KOI8-R.TXT"
|
|
109 ;; "KSC5601.TXT"
|
|
110 ;; note that KSC5601.TXT as currently distributed is NOT what
|
|
111 ;; it claims to be! see comments in KSX1001.TXT.
|
|
112 ("KSX1001.TXT" korean-ksc5601)
|
|
113 ;; "OLD5601.TXT"
|
|
114 ;; "SHIFTJIS.TXT"
|
|
115 )
|
|
116 ("unicode/mule-ucs"
|
2297
|
117 ;; #### we don't support surrogates?!??
|
780
|
118 ;; use these instead of the above ones once we support surrogates
|
|
119 ;;("chinese-cns11643-1.txt" chinese-cns11643-1)
|
|
120 ;;("chinese-cns11643-2.txt" chinese-cns11643-2)
|
|
121 ;;("chinese-cns11643-3.txt" chinese-cns11643-3)
|
|
122 ;;("chinese-cns11643-4.txt" chinese-cns11643-4)
|
|
123 ;;("chinese-cns11643-5.txt" chinese-cns11643-5)
|
|
124 ;;("chinese-cns11643-6.txt" chinese-cns11643-6)
|
|
125 ;;("chinese-cns11643-7.txt" chinese-cns11643-7)
|
|
126 ("chinese-sisheng.txt" chinese-sisheng)
|
|
127 ("ethiopic.txt" ethiopic)
|
|
128 ("indian-is13194.txt" indian-is13194)
|
|
129 ("ipa.txt" ipa)
|
|
130 ("thai-tis620.txt" thai-tis620)
|
|
131 ("tibetan.txt" tibetan)
|
|
132 ("vietnamese-viscii-lower.txt" vietnamese-viscii-lower)
|
|
133 ("vietnamese-viscii-upper.txt" vietnamese-viscii-upper)
|
|
134 )
|
|
135 ("unicode/other"
|
|
136 ("lao.txt" lao)
|
|
137 )
|
771
|
138 )))
|
780
|
139 (mapcar #'(lambda (tables)
|
|
140 (let ((undir
|
|
141 (expand-file-name (car tables) data-directory)))
|
|
142 (mapcar #'(lambda (args)
|
1318
|
143 (apply 'load-unicode-mapping-table
|
780
|
144 (expand-file-name (car args) undir)
|
|
145 (cdr args)))
|
|
146 (cdr tables))))
|
771
|
147 parse-args)))
|
|
148
|
|
149 (make-coding-system
|
|
150 'utf-16 'unicode
|
|
151 "UTF-16"
|
|
152 '(mnemonic "UTF-16"
|
3767
|
153 documentation
|
771
|
154 "UTF-16 Unicode encoding -- the standard (almost-) fixed-width
|
|
155 two-byte encoding, with surrogates. It will be fixed-width if all
|
|
156 characters are in the BMP (Basic Multilingual Plane -- first 65536
|
|
157 codepoints). Cannot represent characters with codepoints above
|
|
158 0x10FFFF (a little more than 1,000,000). Unicode and ISO guarantee
|
|
159 never to encode any characters outside this range -- all the rest are
|
|
160 for private, corporate or internal use."
|
3767
|
161 unicode-type utf-16))
|
771
|
162
|
2574
|
163 (define-coding-system-alias 'utf-16-be 'utf-16)
|
|
164
|
771
|
165 (make-coding-system
|
|
166 'utf-16-bom 'unicode
|
|
167 "UTF-16 w/BOM"
|
|
168 '(mnemonic "UTF16-BOM"
|
3767
|
169 documentation
|
771
|
170 "UTF-16 Unicode encoding with byte order mark (BOM) at the beginning.
|
|
171 The BOM is Unicode character U+FEFF -- i.e. the first two bytes are
|
|
172 0xFE and 0xFF, respectively, or reversed in a little-endian
|
|
173 representation. It has been sanctioned by the Unicode Consortium for
|
|
174 use at the beginning of a Unicode stream as a marker of the byte order
|
|
175 of the stream, and commonly appears in Unicode files under Microsoft
|
|
176 Windows, where it also functions as a magic cookie identifying a
|
|
177 Unicode file. The character is called \"ZERO WIDTH NO-BREAK SPACE\"
|
|
178 and is suitable as a byte-order marker because:
|
|
179
|
|
180 -- it has no displayable representation
|
|
181 -- due to its semantics it never normally appears at the beginning
|
|
182 of a stream
|
|
183 -- its reverse U+FFFE is not a legal Unicode character
|
|
184 -- neither byte sequence is at all likely in any other standard
|
|
185 encoding, particularly at the beginning of a stream
|
|
186
|
|
187 This coding system will insert a BOM at the beginning of a stream when
|
|
188 writing and strip it off when reading."
|
3767
|
189 unicode-type utf-16
|
771
|
190 need-bom t))
|
|
191
|
|
192 (make-coding-system
|
|
193 'utf-16-little-endian 'unicode
|
|
194 "UTF-16 Little Endian"
|
|
195 '(mnemonic "UTF16-LE"
|
|
196 documentation
|
|
197 "Little-endian version of UTF-16 Unicode encoding.
|
|
198 See `utf-16' coding system."
|
3767
|
199 unicode-type utf-16
|
771
|
200 little-endian t))
|
|
201
|
2574
|
202 (define-coding-system-alias 'utf-16-le 'utf-16-little-endian)
|
|
203
|
771
|
204 (make-coding-system
|
|
205 'utf-16-little-endian-bom 'unicode
|
|
206 "UTF-16 Little Endian w/BOM"
|
|
207 '(mnemonic "MSW-Unicode"
|
|
208 documentation
|
|
209 "Little-endian version of UTF-16 Unicode encoding, with byte order mark.
|
|
210 Standard encoding for representing Unicode under MS Windows. See
|
|
211 `utf-16-bom' coding system."
|
3767
|
212 unicode-type utf-16
|
771
|
213 little-endian t
|
|
214 need-bom t))
|
|
215
|
|
216 (make-coding-system
|
|
217 'ucs-4 'unicode
|
|
218 "UCS-4"
|
|
219 '(mnemonic "UCS4"
|
|
220 documentation
|
|
221 "UCS-4 Unicode encoding -- fully fixed-width four-byte encoding."
|
3767
|
222 unicode-type ucs-4))
|
771
|
223
|
|
224 (make-coding-system
|
|
225 'ucs-4-little-endian 'unicode
|
|
226 "UCS-4 Little Endian"
|
|
227 '(mnemonic "UCS4-LE"
|
|
228 documentation
|
2297
|
229 ;; #### I don't think this is permitted by ISO 10646, only Unicode.
|
|
230 ;; Call it UTF-32 instead?
|
771
|
231 "Little-endian version of UCS-4 Unicode encoding. See `ucs-4' coding system."
|
3767
|
232 unicode-type ucs-4
|
771
|
233 little-endian t))
|
|
234
|
|
235 (make-coding-system
|
4096
|
236 'utf-32 'unicode
|
|
237 "UTF-32"
|
|
238 '(mnemonic "UTF32"
|
|
239 documentation
|
|
240 "UTF-32 Unicode encoding -- fixed-width four-byte encoding,
|
|
241 characters less than #x10FFFF are not supported. "
|
|
242 unicode-type utf-32))
|
|
243
|
|
244 (make-coding-system
|
|
245 'utf-32-little-endian 'unicode
|
|
246 "UTF-32 Little Endian"
|
|
247 '(mnemonic "UTF32-LE"
|
|
248 documentation
|
|
249 "Little-endian version of UTF-32 Unicode encoding.
|
|
250
|
|
251 A fixed-width four-byte encoding, characters less than #x10FFFF are not
|
|
252 supported. "
|
|
253 unicode-type ucs-4 little-endian t))
|
|
254
|
|
255 (make-coding-system
|
771
|
256 'utf-8 'unicode
|
|
257 "UTF-8"
|
|
258 '(mnemonic "UTF8"
|
3767
|
259 documentation "
|
|
260 UTF-8 Unicode encoding -- ASCII-compatible 8-bit variable-width encoding
|
2297
|
261 sharing the following principles with the Mule-internal encoding:
|
771
|
262
|
|
263 -- All ASCII characters (codepoints 0 through 127) are represented
|
|
264 by themselves (i.e. using one byte, with the same value as the
|
|
265 ASCII codepoint), and these bytes are disjoint from bytes
|
|
266 representing non-ASCII characters.
|
|
267
|
|
268 This means that any 8-bit clean application can safely process
|
|
269 UTF-8-encoded text as it were ASCII, with no corruption (e.g. a
|
|
270 '/' byte is always a slash character, never the second byte of
|
|
271 some other character, as with Big5, so a pathname encoded in
|
|
272 UTF-8 can safely be split up into components and reassembled
|
|
273 again using standard ASCII processes).
|
|
274
|
|
275 -- Leading bytes and non-leading bytes in the encoding of a
|
|
276 character are disjoint, so moving backwards is easy.
|
|
277
|
|
278 -- Given only the leading byte, you know how many following bytes
|
|
279 are present.
|
|
280 "
|
3767
|
281 unicode-type utf-8))
|
771
|
282
|
985
|
283 (make-coding-system
|
|
284 'utf-8-bom 'unicode
|
|
285 "UTF-8 w/BOM"
|
|
286 '(mnemonic "MSW-UTF8"
|
|
287 documentation
|
|
288 "UTF-8 Unicode encoding, with byte order mark.
|
|
289 Standard encoding for representing UTF-8 under MS Windows."
|
3767
|
290 unicode-type utf-8
|
985
|
291 little-endian t
|
|
292 need-bom t))
|
|
293
|
2633
|
294 (defun decode-char (quote-ucs code &optional restriction)
|
3659
|
295 "FSF compatibility--return Mule character with Unicode codepoint CODE.
|
2633
|
296 The second argument must be 'ucs, the third argument is ignored. "
|
4096
|
297 ;; We're prepared to accept invalid Unicode in unicode-to-char, but not in
|
|
298 ;; this function, which is the API that should actually be used, since
|
|
299 ;; it's available in GNU and in Mule-UCS.
|
|
300 (check-argument-range code #x0 #x10FFFF)
|
3506
|
301 (assert (eq quote-ucs 'ucs) t
|
2633
|
302 "Sorry, decode-char doesn't yet support anything but the UCS. ")
|
|
303 (unicode-to-char code))
|
|
304
|
|
305 (defun encode-char (char quote-ucs &optional restriction)
|
3659
|
306 "FSF compatibility--return the Unicode code point of CHAR.
|
2633
|
307 The second argument must be 'ucs, the third argument is ignored. "
|
3506
|
308 (assert (eq quote-ucs 'ucs) t
|
2633
|
309 "Sorry, encode-char doesn't yet support anything but the UCS. ")
|
|
310 (char-to-unicode char))
|
|
311
|
3667
|
312 (when (featurep 'mule)
|
4083
|
313 (let ((prog
|
|
314 (eval-when-compile
|
|
315 (let ((pre-existing
|
|
316 ;; This is the compiled CCL program from the assert
|
|
317 ;; below. Since this file is dumped and ccl.el isn't (and
|
|
318 ;; even when it was, it was dumped much later than this
|
|
319 ;; one), we can't compile the program at dump time. We can
|
|
320 ;; check at byte compile time that the program is as
|
|
321 ;; expected, though.
|
|
322 [1 16 131127 7 98872 65823 1307 5 -65536 65313 64833 1028 147513
|
|
323 8 82009 255 22]))
|
|
324 (when (featurep 'mule)
|
|
325 ;; Check that the pre-existing constant reflects the intended
|
|
326 ;; CCL program.
|
|
327 (assert
|
|
328 (equal pre-existing
|
|
329 (ccl-compile
|
|
330 `(1
|
|
331 (;; mule-to-unicode's first argument is the
|
|
332 ;; charset ID, the second its first byte
|
|
333 ;; left shifted by 7 bits masked with its
|
|
334 ;; second byte.
|
|
335 (r1 = (r1 << 7))
|
|
336 (r1 = (r1 | r2))
|
|
337 (mule-to-unicode r0 r1)
|
|
338 (if (r0 & ,(lognot #xFFFF))
|
|
339 ;; Redisplay looks in r1 and r2 for the first
|
|
340 ;; and second bytes of the X11 font,
|
|
341 ;; respectively. For non-BMP characters we
|
|
342 ;; display U+FFFD.
|
|
343 ((r1 = #xFF)
|
|
344 (r2 = #xFD))
|
|
345 ((r1 = (r0 >> 8))
|
|
346 (r2 = (r0 & #xFF))))))))
|
|
347 nil
|
|
348 "The pre-compiled CCL program appears broken. "))
|
|
349 pre-existing))))
|
|
350 (defconst ccl-encode-to-ucs-2 prog
|
3667
|
351 "CCL program to transform Mule characters to UCS-2.")
|
4083
|
352 (put 'ccl-encode-to-ucs-2 'ccl-program-idx
|
|
353 (register-ccl-program 'ccl-encode-to-ucs-2 prog))))
|
3667
|
354
|
771
|
355 ;; #### UTF-7 is not yet implemented, and it's tricky to do. There's
|
|
356 ;; an implementation in appendix A.1 of the Unicode Standard, Version
|
|
357 ;; 2.0, but I don't know its licensing characteristics.
|
|
358
|
|
359 ; (make-coding-system
|
|
360 ; 'utf-7 'unicode
|
|
361 ; "UTF-7"
|
|
362 ; '(mnemonic "UTF7"
|
3659
|
363 ; documentation; "UTF-7 Unicode encoding -- 7-bit-ASCII modal Internet-mail-compatible
|
771
|
364 ; encoding especially designed for headers, with the following
|
|
365 ; properties:
|
|
366
|
|
367 ; -- Only characters that are considered safe for passing through any mail
|
|
368 ; gateway without damage are used.
|
|
369
|
|
370 ; -- This is a modal encoding, with two states. The first, default
|
|
371 ; state encodes the most common Unicode characters (upper and
|
|
372 ; lowercase letters, digits, and 9 common punctuation marks) as
|
|
373 ; themselves, and the second state, entered using '+' and
|
|
374 ; terminated with '-' or any character disallowed in state 2,
|
|
375 ; encodes any Unicode characters by first converting to UTF-16,
|
|
376 ; most significant byte first, and then to a slightly modified
|
|
377 ; Base64 encoding. (Thus, UTF-7 has the same limitations on the
|
|
378 ; characters it can encode as UTF-16.)
|
|
379
|
|
380 ; -- The modified Base64 encoding deviates from standard Base64 in
|
|
381 ; that it omits the `=' pad character. This is eliminated so as to
|
|
382 ; avoid conflicts with the use of `=' as an escape in the
|
|
383 ; Quoted-Printable encoding and the related Q encoding for headers:
|
|
384 ; With this modification, non-whitespace chars in UTF-7 will be
|
|
385 ; represented in Quoted-Printable and in Q as-is, with no further
|
|
386 ; encoding.
|
|
387
|
|
388 ; For more information, see Appendix A.1 of The Unicode Standard 2.0, or
|
|
389 ; wherever it is in v3.0."
|
3767
|
390 ; unicode-type utf-7))
|