(utf-8-post-read-conversion): If the
[bpt/emacs.git] / lisp / international / utf-8.el
1 ;;; utf-8.el --- UTF-8 decoding/encoding support -*- coding: iso-2022-7bit -*-
2
3 ;; Copyright (C) 2001, 2004 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5 ;; Copyright (C) 2001, 2002 Free Software Foundation, Inc.
6
7 ;; Author: TAKAHASHI Naoto <ntakahas@m17n.org>
8 ;; Maintainer: FSF
9 ;; Keywords: multilingual, Unicode, UTF-8, i18n
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29
30 ;; The coding-system `mule-utf-8' basically supports encoding/decoding
31 ;; of the following character sets to and from UTF-8:
32 ;;
33 ;; ascii
34 ;; eight-bit-control
35 ;; latin-iso8859-1
36 ;; mule-unicode-0100-24ff
37 ;; mule-unicode-2500-33ff
38 ;; mule-unicode-e000-ffff
39 ;;
40 ;; On decoding, Unicode characters that do not fit into the above
41 ;; character sets are handled as `eight-bit-control' or
42 ;; `eight-bit-graphic' characters to retain the information about the
43 ;; original byte sequence and text properties record the corresponding
44 ;; unicode.
45 ;;
46 ;; Fixme: note that reading and writing invalid utf-8 may not be
47 ;; idempotent -- to represent the bytes to fix that needs a new charset.
48 ;;
49 ;; Characters from other character sets can be encoded with mule-utf-8
50 ;; by populating the translation table
51 ;; `utf-translation-table-for-encode'. Hash tables
52 ;; `utf-subst-table-for-decode' and `utf-subst-table-for-encode' are
53 ;; used to support encoding and decoding of about a quarter of the CJK
54 ;; space between U+3400 and U+DFFF.
55
56 ;; UTF-8 is defined in RFC 3629. A sketch of the encoding is:
57
58 ;; scalar | utf-8
59 ;; value | 1st byte | 2nd byte | 3rd byte
60 ;; --------------------+-----------+-----------+----------
61 ;; 0000 0000 0xxx xxxx | 0xxx xxxx | |
62 ;; 0000 0yyy yyxx xxxx | 110y yyyy | 10xx xxxx |
63 ;; zzzz yyyy yyxx xxxx | 1110 zzzz | 10yy yyyy | 10xx xxxx
64
65 ;;; Code:
66
67 (defvar ucs-mule-to-mule-unicode (make-char-table 'translation-table nil)
68 "Char table mapping characters to latin-iso8859-1 or mule-unicode-*.
69
70 If `unify-8859-on-encoding-mode' is non-nil, this table populates the
71 translation-table named `utf-translation-table-for-encode'.")
72
73 (define-translation-table 'utf-translation-table-for-encode)
74
75
76 ;; Map Cyrillic and Greek to iso-8859 charsets, which take half the
77 ;; space of mule-unicode. For Latin scripts this isn't very
78 ;; important. Hebrew and Arabic might go here too when there's proper
79 ;; support for them.
80
81 (defvar utf-fragmentation-table (make-char-table 'translation-table nil)
82 "Char-table normally mapping non-Latin mule-unicode-* chars to iso-8859-*.
83
84 If `utf-fragment-on-decoding' is non-nil, this table populates the
85 translation-table named `utf-translation-table-for-decode'")
86
87 (defvar utf-defragmentation-table (make-char-table 'translation-table nil)
88 "Char-table for reverse mapping of `utf-fragmentation-table'.
89
90 If `utf-fragment-on-decoding' is non-nil and
91 `unify-8859-on-encoding-mode' is nil, this table populates the
92 translation-table named `utf-translation-table-for-encode'")
93
94 (define-translation-table 'utf-translation-table-for-decode)
95
96
97 (defvar ucs-mule-cjk-to-unicode (make-hash-table :test 'eq)
98 "Hash table mapping Emacs CJK character sets to Unicode code points.
99
100 If `utf-translate-cjk-mode' is non-nil, this table populates the
101 translation-hash-table named `utf-subst-table-for-encode'.")
102
103 (define-translation-hash-table 'utf-subst-table-for-encode
104 ucs-mule-cjk-to-unicode)
105
106 (defvar ucs-unicode-to-mule-cjk (make-hash-table :test 'eq)
107 "Hash table mapping Unicode code points to Emacs CJK character sets.
108
109 If `utf-translate-cjk-mode' is non-nil, this table populates the
110 translation-hash-table named `utf-subst-table-for-decode'.")
111
112 (define-translation-hash-table 'utf-subst-table-for-decode
113 ucs-unicode-to-mule-cjk)
114
115 (mapc
116 (lambda (pair)
117 (aset utf-fragmentation-table (car pair) (cdr pair))
118 (aset utf-defragmentation-table (cdr pair) (car pair)))
119 '((?\e$,1&d\e(B . ?\e,F4\e(B) (?\e$,1&e\e(B . ?\e,F5\e(B) (?\e$,1&f\e(B . ?\e,F6\e(B) (?\e$,1&h\e(B . ?\e,F8\e(B) (?\e$,1&i\e(B . ?\e,F9\e(B)
120 (?\e$,1&j\e(B . ?\e,F:\e(B) (?\e$,1&l\e(B . ?\e,F<\e(B) (?\e$,1&n\e(B . ?\e,F>\e(B) (?\e$,1&o\e(B . ?\e,F?\e(B) (?\e$,1&p\e(B . ?\e,F@\e(B)
121 (?\e$,1&q\e(B . ?\e,FA\e(B) (?\e$,1&r\e(B . ?\e,FB\e(B) (?\e$,1&s\e(B . ?\e,FC\e(B) (?\e$,1&t\e(B . ?\e,FD\e(B) (?\e$,1&u\e(B . ?\e,FE\e(B)
122 (?\e$,1&v\e(B . ?\e,FF\e(B) (?\e$,1&w\e(B . ?\e,FG\e(B) (?\e$,1&x\e(B . ?\e,FH\e(B) (?\e$,1&y\e(B . ?\e,FI\e(B) (?\e$,1&z\e(B . ?\e,FJ\e(B)
123 (?\e$,1&{\e(B . ?\e,FK\e(B) (?\e$,1&|\e(B . ?\e,FL\e(B) (?\e$,1&}\e(B . ?\e,FM\e(B) (?\e$,1&~\e(B . ?\e,FN\e(B) (?\e$,1&\7f\e(B . ?\e,FO\e(B)
124 (?\e$,1' \e(B . ?\e,FP\e(B) (?\e$,1'!\e(B . ?\e,FQ\e(B) (?\e$,1'#\e(B . ?\e,FS\e(B)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(i\e(B . ?\e,Li\e(B) (?\e$,1(j\e(B . ?\e,Lj\e(B) (?\e$,1(k\e(B . ?\e,Lk\e(B)
150 (?\e$,1(l\e(B . ?\e,Ll\e(B) (?\e$,1(m\e(B . ?\e,Lm\e(B) (?\e$,1(n\e(B . ?\e,Ln\e(B) (?\e$,1(o\e(B . ?\e,Lo\e(B) (?\e$,1(q\e(B . ?\e,Lq\e(B)
151 (?\e$,1(r\e(B . ?\e,Lr\e(B) (?\e$,1(s\e(B . ?\e,Ls\e(B) (?\e$,1(t\e(B . ?\e,Lt\e(B) (?\e$,1(u\e(B . ?\e,Lu\e(B) (?\e$,1(v\e(B . ?\e,Lv\e(B)
152 (?\e$,1(w\e(B . ?\e,Lw\e(B) (?\e$,1(x\e(B . ?\e,Lx\e(B) (?\e$,1(y\e(B . ?\e,Ly\e(B) (?\e$,1(z\e(B . ?\e,Lz\e(B) (?\e$,1({\e(B . ?\e,L{\e(B)
153 (?\e$,1(|\e(B . ?\e,L|\e(B) (?\e$,1(~\e(B . ?\e,L~\e(B) (?\e$,1(\7f\e(B . ?\e,L\7f\e(B)))
154
155
156 (defcustom utf-fragment-on-decoding nil
157 "Whether or not to decode some chars in UTF-8/16 text into iso8859 charsets.
158 Setting this means that the relevant Cyrillic and Greek characters are
159 decoded into the iso8859 charsets rather than into
160 mule-unicode-0100-24ff. The iso8859 charsets take half as much space
161 in the buffer, but using them may affect how the buffer can be re-encoded
162 and may require a different input method to search for them, for instance.
163 See `unify-8859-on-decoding-mode' and `unify-8859-on-encoding-mode'
164 for mechanisms to make this largely transparent.
165
166 Setting this variable outside customize has no effect."
167 :set (lambda (s v)
168 (if v
169 (progn
170 (define-translation-table 'utf-translation-table-for-decode
171 utf-fragmentation-table)
172 ;; Even if unify-8859-on-encoding-mode is off, make
173 ;; mule-utf-* encode characters in
174 ;; utf-fragmentation-table.
175 (unless (eq (get 'utf-translation-table-for-encode
176 'translation-table)
177 ucs-mule-to-mule-unicode)
178 (define-translation-table 'utf-translation-table-for-encode
179 utf-defragmentation-table)))
180 (define-translation-table 'utf-translation-table-for-decode)
181 ;; When unify-8859-on-encoding-mode is off, be sure to make
182 ;; mule-utf-* disabled for characters in
183 ;; utf-fragmentation-table.
184 (unless (eq (get 'utf-translation-table-for-encode
185 'translation-table)
186 ucs-mule-to-mule-unicode)
187 (define-translation-table 'utf-translation-table-for-encode)))
188 (set-default s v))
189 :version "21.4"
190 :type 'boolean
191 :group 'mule)
192
193
194 (defconst utf-translate-cjk-charsets '(chinese-gb2312
195 chinese-big5-1 chinese-big5-2
196 japanese-jisx0208 japanese-jisx0212
197 korean-ksc5601)
198 "List of charsets supported by `utf-translate-cjk-mode'.")
199
200 (defconst utf-translate-cjk-unicode-range
201 '((#x2e80 . #xd7a3)
202 (#xff00 . #xffef))
203 "List of Unicode code ranges supported by `utf-translate-cjk-mode'.")
204
205 ;; Return non-nil if CODE-POINT is in `utf-translate-cjk-unicode-range'.
206 (defsubst utf-translate-cjk-substitutable-p (code-point)
207 (let ((tail utf-translate-cjk-unicode-range)
208 elt)
209 (while tail
210 (setq elt (car tail) tail (cdr tail))
211 (if (and (>= code-point (car elt)) (<= code-point (cdr elt)))
212 (setq tail nil)
213 (setq elt nil)))
214 elt))
215
216 (defvar utf-translate-cjk-lang-env nil
217 "Language environment in which tables for `utf-translate-cjk-mode' is loaded.
218 The value nil means that the tables are not yet loaded.")
219
220 (defun utf-translate-cjk-load-tables ()
221 "Load tables for `utf-translate-cjk-mode'."
222 ;; Fixme: Allow the use of the CJK charsets to be
223 ;; customized by reordering and possible omission.
224 (let ((redefined (< (hash-table-size ucs-mule-cjk-to-unicode) 43000)))
225 (if redefined
226 ;; Redefine them with realistic initial sizes and a
227 ;; smallish rehash size to avoid wasting significant
228 ;; space after they're built.
229 (setq ucs-mule-cjk-to-unicode
230 (make-hash-table :test 'eq :size 43000 :rehash-size 1000)
231 ucs-unicode-to-mule-cjk
232 (make-hash-table :test 'eq :size 21500 :rehash-size 1000)))
233
234 ;; Load the files explicitly, to avoid having to keep
235 ;; around the large tables they contain (as well as the
236 ;; ones which get built).
237 (cond ((string= "Korean" current-language-environment)
238 (load "subst-jis")
239 (load "subst-big5")
240 (load "subst-gb2312")
241 (load "subst-ksc"))
242 ((string= "Chinese-BIG5" current-language-environment)
243 (load "subst-jis")
244 (load "subst-ksc")
245 (load "subst-gb2312")
246 (load "subst-big5"))
247 ((string= "Chinese-GB" current-language-environment)
248 (load "subst-jis")
249 (load "subst-ksc")
250 (load "subst-big5")
251 (load "subst-gb2312"))
252 (t
253 (load "subst-ksc")
254 (load "subst-gb2312")
255 (load "subst-big5")
256 (load "subst-jis"))) ; jis covers as much as big5, gb2312
257
258 (when redefined
259 (define-translation-hash-table 'utf-subst-table-for-decode
260 ucs-unicode-to-mule-cjk)
261 (define-translation-hash-table 'utf-subst-table-for-encode
262 ucs-mule-cjk-to-unicode)
263 (set-char-table-extra-slot (get 'utf-translation-table-for-encode
264 'translation-table)
265 1 ucs-mule-cjk-to-unicode))
266
267 (setq utf-translate-cjk-lang-env current-language-environment)))
268
269 (defun utf-lookup-subst-table-for-decode (code-point)
270 (if (and utf-translate-cjk-mode
271 (not utf-translate-cjk-lang-env)
272 (utf-translate-cjk-substitutable-p code-point))
273 (utf-translate-cjk-load-tables))
274 (gethash code-point
275 (get 'utf-subst-table-for-decode 'translation-hash-table)))
276
277
278 (defun utf-lookup-subst-table-for-encode (char)
279 (if (and utf-translate-cjk-mode
280 (not utf-translate-cjk-lang-env)
281 (memq (char-charset char) utf-translate-cjk-charsets))
282 (utf-translate-cjk-load-tables))
283 (gethash char
284 (get 'utf-subst-table-for-encode 'translation-hash-table)))
285
286 (define-minor-mode utf-translate-cjk-mode
287 "Toggle whether UTF based coding systems de/encode CJK characters.
288 If ARG is an integer, enable if ARG is positive and disable if
289 zero or negative. This is a minor mode.
290 Enabling this allows the coding systems mule-utf-8,
291 mule-utf-16le and mule-utf-16be to encode characters in the charsets
292 `korean-ksc5601', `chinese-gb2312', `chinese-big5-1',
293 `chinese-big5-2', `japanese-jisx0208' and `japanese-jisx0212', and to
294 decode the corresponding unicodes into such characters.
295
296 Where the charsets overlap, the one preferred for decoding is chosen
297 according to the language environment in effect when this option is
298 turned on: ksc5601 for Korean, gb2312 for Chinese-GB, big5 for
299 Chinese-Big5 and jisx for other environments.
300
301 This mode is on by default. If you are not interested in CJK
302 characters and want to avoid some overhead on encoding/decoding
303 by the above coding systems, you can customize the user option
304 `utf-translate-cjk-mode' to nil."
305 :init-value t
306 :version "21.4"
307 :type 'boolean
308 :group 'mule
309 :global t
310 (if utf-translate-cjk-mode
311 (progn
312 (define-translation-hash-table 'utf-subst-table-for-decode
313 ucs-unicode-to-mule-cjk)
314 (define-translation-hash-table 'utf-subst-table-for-encode
315 ucs-mule-cjk-to-unicode)
316 (set-char-table-extra-slot (get 'utf-translation-table-for-encode
317 'translation-table)
318 1 ucs-mule-cjk-to-unicode))
319 (define-translation-hash-table 'utf-subst-table-for-decode
320 (make-hash-table :test 'eq))
321 (define-translation-hash-table 'utf-subst-table-for-encode
322 (make-hash-table :test 'eq))
323 (set-char-table-extra-slot (get 'utf-translation-table-for-encode
324 'translation-table)
325 1 nil))
326
327 ;; Update safe-chars of mule-utf-* coding systems.
328 (dolist (elt (coding-system-list t))
329 (if (string-match "^mule-utf" (symbol-name elt))
330 (let ((safe-charsets (coding-system-get elt 'safe-charsets))
331 (safe-chars (coding-system-get elt 'safe-chars))
332 (need-update nil))
333 (dolist (charset utf-translate-cjk-charsets)
334 (unless (eq utf-translate-cjk-mode (memq charset safe-charsets))
335 (setq safe-charsets
336 (if utf-translate-cjk-mode
337 (cons charset safe-charsets)
338 (delq charset safe-charsets))
339 need-update t)
340 (aset safe-chars (make-char charset) utf-translate-cjk-mode)))
341 (when need-update
342 (coding-system-put elt 'safe-charsets safe-charsets)
343 (define-coding-system-internal elt))))))
344
345 (define-ccl-program ccl-mule-utf-untrans
346 ;; R0 is an untranslatable Unicode code-point (U+3500..U+DFFF or
347 ;; U+10000..U+10FFFF) or an invaid byte (#x00..#xFF). Write
348 ;; eight-bit-control/graphic sequence (2 to 4 chars) representing
349 ;; UTF-8 sequence of r0. Registers r4, r5, r6 are modified.
350 ;;
351 ;; This is a subrountine because we assume that this is called very
352 ;; rarely (so we don't have to worry about the overhead of the
353 ;; call).
354 `(0
355 ((r5 = ,(charset-id 'eight-bit-control))
356 (r6 = ,(charset-id 'eight-bit-graphic))
357 (if (r0 < #x100)
358 ((r4 = ((r0 >> 6) | #xC0))
359 (write-multibyte-character r6 r4))
360 ((if (r0 < #x10000)
361 ((r4 = ((r0 >> 12) | #xE0))
362 (write-multibyte-character r6 r4))
363 ((r4 = ((r0 >> 18) | #xF0))
364 (write-multibyte-character r6 r4)
365 (r4 = (((r0 >> 12) & #x3F) | #x80))
366 (if (r4 < #xA0)
367 (write-multibyte-character r5 r4)
368 (write-multibyte-character r6 r4))))
369 (r4 = (((r0 >> 6) & #x3F) | #x80))
370 (if (r4 < #xA0)
371 (write-multibyte-character r5 r4)
372 (write-multibyte-character r6 r4))))
373 (r4 = ((r0 & #x3F) | #x80))
374 (if (r4 < #xA0)
375 (write-multibyte-character r5 r4)
376 (write-multibyte-character r6 r4)))))
377
378 (define-ccl-program ccl-decode-mule-utf-8
379 ;;
380 ;; charset | bytes in utf-8 | bytes in emacs
381 ;; -----------------------+----------------+---------------
382 ;; ascii | 1 | 1
383 ;; -----------------------+----------------+---------------
384 ;; eight-bit-control | 2 | 2
385 ;; eight-bit-graphic | 2 | 1
386 ;; latin-iso8859-1 | 2 | 2
387 ;; -----------------------+----------------+---------------
388 ;; mule-unicode-0100-24ff | 2 | 4
389 ;; (< 0800) | |
390 ;; -----------------------+----------------+---------------
391 ;; mule-unicode-0100-24ff | 3 | 4
392 ;; (>= 8000) | |
393 ;; mule-unicode-2500-33ff | 3 | 4
394 ;; mule-unicode-e000-ffff | 3 | 4
395 ;; -----------------------+----------------+---------------
396 ;; invalid byte | 1 | 2
397 ;;
398 ;; Thus magnification factor is two.
399 ;;
400 `(2
401 ((r6 = ,(charset-id 'latin-iso8859-1))
402 (read r0)
403 (loop
404 (if (r0 < #x80)
405 ;; 1-byte encoding, i.e., ascii
406 (write-read-repeat r0))
407 (if (r0 < #xc2)
408 ;; continuation byte (invalid here) or 1st byte of overlong
409 ;; 2-byte sequence.
410 ((call ccl-mule-utf-untrans)
411 (r6 = ,(charset-id 'latin-iso8859-1))
412 (read r0)
413 (repeat)))
414
415 ;; Read the 2nd byte.
416 (read r1)
417 (if ((r1 & #b11000000) != #b10000000) ; Invalid 2nd byte
418 ((call ccl-mule-utf-untrans)
419 (r6 = ,(charset-id 'latin-iso8859-1))
420 ;; Handle it in the next loop.
421 (r0 = r1)
422 (repeat)))
423
424 (if (r0 < #xe0)
425 ;; 2-byte encoding 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
426 ((r1 &= #x3F)
427 (r1 |= ((r0 & #x1F) << 6))
428 ;; Now r2 holds scalar value. We don't have to check
429 ;; `overlong sequence' because r0 >= 0xC2.
430
431 (if (r1 >= 256)
432 ;; mule-unicode-0100-24ff (< 0800)
433 ((r0 = ,(charset-id 'mule-unicode-0100-24ff))
434 (r1 -= #x0100)
435 (r2 = (((r1 / 96) + 32) << 7))
436 (r1 %= 96)
437 (r1 += (r2 + 32))
438 (translate-character
439 utf-translation-table-for-decode r0 r1)
440 (write-multibyte-character r0 r1)
441 (read r0)
442 (repeat))
443 (if (r1 >= 160)
444 ;; latin-iso8859-1
445 ((r1 -= 128)
446 (write-multibyte-character r6 r1)
447 (read r0)
448 (repeat))
449 ;; eight-bit-control
450 ((r0 = ,(charset-id 'eight-bit-control))
451 (write-multibyte-character r0 r1)
452 (read r0)
453 (repeat))))))
454
455 ;; Read the 3rd bytes.
456 (read r2)
457 (if ((r2 & #b11000000) != #b10000000) ; Invalid 3rd byte
458 ((call ccl-mule-utf-untrans)
459 (r0 = r1)
460 (call ccl-mule-utf-untrans)
461 (r6 = ,(charset-id 'latin-iso8859-1))
462 ;; Handle it in the next loop.
463 (r0 = r2)
464 (repeat)))
465
466 (if (r0 < #xF0)
467 ;; 3byte encoding
468 ;; zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
469 ((r3 = ((r0 & #xF) << 12))
470 (r3 |= ((r1 & #x3F) << 6))
471 (r3 |= (r2 & #x3F))
472
473 (if (r3 < #x800) ; `overlong sequence'
474 ((call ccl-mule-utf-untrans)
475 (r0 = r1)
476 (call ccl-mule-utf-untrans)
477 (r0 = r2)
478 (call ccl-mule-utf-untrans)
479 (r6 = ,(charset-id 'latin-iso8859-1))
480 (read r0)
481 (repeat)))
482
483 (if (r3 < #x2500)
484 ;; mule-unicode-0100-24ff (>= 0800)
485 ((r0 = ,(charset-id 'mule-unicode-0100-24ff))
486 (r3 -= #x0100)
487 (r3 //= 96)
488 (r1 = (r7 + 32))
489 (r1 += ((r3 + 32) << 7))
490 (translate-character
491 utf-translation-table-for-decode r0 r1)
492 (write-multibyte-character r0 r1)
493 (read r0)
494 (repeat)))
495
496 (if (r3 < #x3400)
497 ;; mule-unicode-2500-33ff
498 ((r0 = r3) ; don't zap r3
499 (lookup-integer utf-subst-table-for-decode r0 r1)
500 (if (r7 == 0)
501 ((r0 = ,(charset-id 'mule-unicode-2500-33ff))
502 (r3 -= #x2500)
503 (r3 //= 96)
504 (r1 = (r7 + 32))
505 (r1 += ((r3 + 32) << 7))))
506 (write-multibyte-character r0 r1)
507 (read r0)
508 (repeat)))
509
510 (if (r3 < #xE000)
511 ;; Try to convert to CJK chars, else
512 ;; keep them as eight-bit-{control|graphic}.
513 ((r0 = r3)
514 (lookup-integer utf-subst-table-for-decode r3 r1)
515 (if r7
516 ;; got a translation
517 ((write-multibyte-character r3 r1)
518 (read r0)
519 (repeat))
520 ((call ccl-mule-utf-untrans)
521 (r6 = ,(charset-id 'latin-iso8859-1))
522 (read r0)
523 (repeat)))))
524
525 ;; mule-unicode-e000-ffff
526 ;; Fixme: fffe and ffff are invalid.
527 (r0 = r3) ; don't zap r3
528 (lookup-integer utf-subst-table-for-decode r0 r1)
529 (if (r7 == 0)
530 ((r0 = ,(charset-id 'mule-unicode-e000-ffff))
531 (r3 -= #xe000)
532 (r3 //= 96)
533 (r1 = (r7 + 32))
534 (r1 += ((r3 + 32) << 7))))
535 (write-multibyte-character r0 r1)
536 (read r0)
537 (repeat)))
538
539 ;; Read the 4th bytes.
540 (read r3)
541 (if ((r3 & #b11000000) != #b10000000) ; Invalid 4th byte
542 ((call ccl-mule-utf-untrans)
543 (r0 = r1)
544 (call ccl-mule-utf-untrans)
545 (r0 = r2)
546 (call ccl-mule-utf-untrans)
547 (r6 = ,(charset-id 'latin-iso8859-1))
548 ;; Handle it in the next loop.
549 (r0 = r3)
550 (repeat)))
551
552 (if (r0 < #xF8)
553 ;; 4-byte encoding:
554 ;; wwwzzzzzzyyyyyyxxxxxx = 11110www 10zzzzzz 10yyyyyy 10xxxxxx
555 ;; keep those bytes as eight-bit-{control|graphic}
556 ;; Fixme: allow lookup in utf-subst-table-for-decode.
557 ((r4 = ((r0 & #x7) << 18))
558 (r4 |= ((r1 & #x3F) << 12))
559 (r4 |= ((r2 & #x3F) << 6))
560 (r4 |= (r3 & #x3F))
561
562 (if (r4 < #x10000) ; `overlong sequence'
563 ((call ccl-mule-utf-untrans)
564 (r0 = r1)
565 (call ccl-mule-utf-untrans)
566 (r0 = r2)
567 (call ccl-mule-utf-untrans)
568 (r0 = r3)
569 (call ccl-mule-utf-untrans))
570 ((r0 = r4)
571 (call ccl-mule-utf-untrans))))
572
573 ;; Unsupported sequence.
574 ((call ccl-mule-utf-untrans)
575 (r0 = r1)
576 (call ccl-mule-utf-untrans)
577 (r0 = r2)
578 (call ccl-mule-utf-untrans)
579 (r0 = r3)
580 (call ccl-mule-utf-untrans)))
581 (r6 = ,(charset-id 'latin-iso8859-1))
582 (read r0)
583 (repeat)))
584
585
586 ;; At EOF...
587 (if (r0 >= 0)
588 ;; r0 >= #x80
589 ((call ccl-mule-utf-untrans)
590 (if (r1 >= 0)
591 ((r0 = r1)
592 (call ccl-mule-utf-untrans)
593 (if (r2 >= 0)
594 ((r0 = r2)
595 (call ccl-mule-utf-untrans)
596 (if (r3 >= 0)
597 ((r0 = r3)
598 (call ccl-mule-utf-untrans))))))))))
599
600 "CCL program to decode UTF-8.
601 Basic decoding is done into the charsets ascii, latin-iso8859-1 and
602 mule-unicode-*, but see also `utf-fragmentation-table' and
603 `ucs-mule-cjk-to-unicode'.
604 Encodings of un-representable Unicode characters are decoded asis into
605 eight-bit-control and eight-bit-graphic characters.")
606
607 (define-ccl-program ccl-mule-utf-8-encode-untrans
608 ;; UTF-8 decoder generates an UTF-8 sequence represented by a
609 ;; sequence eight-bit-control/graphic chars for an untranslatable
610 ;; character and an invalid byte.
611 ;;
612 ;; This CCL parses that sequence (the first byte is already in r1),
613 ;; writes out the original bytes of that sequence, and sets r5 to
614 ;; -1.
615 ;;
616 ;; If the eight-bit-control/graphic sequence is shorter than what r1
617 ;; suggests, it sets r5 and r6 to the last character read that
618 ;; should be handled by the next loop of a caller.
619 ;;
620 ;; Note: For UTF-8 validation, we only check if a character is
621 ;; eight-bit-control/graphic or not. It may result in incorrect
622 ;; handling of random binary data, but such a data can't be encoded
623 ;; by UTF-8 anyway. At least, UTF-8 decoders doesn't generate such
624 ;; a sequence even if a source contains invalid byte-sequence.
625 `(0
626 (;; Read the 2nd byte.
627 (read-multibyte-character r5 r6)
628 (r0 = (r5 != ,(charset-id 'eight-bit-control)))
629 (if ((r5 != ,(charset-id 'eight-bit-graphic)) & r0)
630 ((write r1) ; invalid UTF-8
631 (r1 = -1)
632 (end)))
633
634 (if (r1 <= #xC3)
635 ;; 2-byte sequence for an originally invalid byte.
636 ((r6 &= #x3F)
637 (r6 |= ((r1 & #x1F) << 6))
638 (write r6)
639 (r5 = -1)
640 (end)))
641
642 (write r1 r6)
643 (r2 = r1)
644 (r1 = -1)
645 ;; Read the 3rd byte.
646 (read-multibyte-character r5 r6)
647 (r0 = (r5 != ,(charset-id 'eight-bit-control)))
648 (if ((r5 != ,(charset-id 'eight-bit-graphic)) & r0)
649 (end)) ; invalid UTF-8
650 (write r6)
651 (if (r2 < #xF0)
652 ;; 3-byte sequence for an untranslated character.
653 ((r5 = -1)
654 (end)))
655 ;; Read the 4th byte.
656 (read-multibyte-character r5 r6)
657 (r0 = (r5 != ,(charset-id 'eight-bit-control)))
658 (if ((r5 != ,(charset-id 'eight-bit-graphic)) & r0)
659 (end)) ; invalid UTF-8
660 ;; 4-byte sequence for an untranslated character.
661 (write r6)
662 (r5 = -1)
663 (end))
664
665 ;; At EOF...
666 ((r5 = -1)
667 (if (r1 >= 0)
668 (write r1)))))
669
670 (define-ccl-program ccl-encode-mule-utf-8
671 `(1
672 ((r5 = -1)
673 (loop
674 (if (r5 < 0)
675 (read-multibyte-character r0 r1)
676 ;; Pre-read character is in r5 (charset-ID) and r6 (code-point).
677 ((r0 = r5)
678 (r1 = r6)
679 (r5 = -1)))
680 (translate-character utf-translation-table-for-encode r0 r1)
681
682 (if (r0 == ,(charset-id 'ascii))
683 (write-repeat r1))
684
685 (if (r0 == ,(charset-id 'latin-iso8859-1))
686 ;; r1 scalar utf-8
687 ;; 0000 0yyy yyxx xxxx 110y yyyy 10xx xxxx
688 ;; 20 0000 0000 1010 0000 1100 0010 1010 0000
689 ;; 7f 0000 0000 1111 1111 1100 0011 1011 1111
690 ((write ((r1 >> 6) | #xc2))
691 (r1 &= #x3f)
692 (r1 |= #x80)
693 (write-repeat r1)))
694
695 (if (r0 == ,(charset-id 'mule-unicode-0100-24ff))
696 ((r0 = ((((r1 & #x3f80) >> 7) - 32) * 96))
697 ;; #x3f80 == (0011 1111 1000 0000)b
698 (r1 &= #x7f)
699 (r1 += (r0 + 224)) ; 240 == -32 + #x0100
700 ;; now r1 holds scalar value
701 (if (r1 < #x0800)
702 ;; 2byte encoding
703 ((write ((r1 >> 6) | #xC0))
704 (r1 &= #x3F)
705 (r1 |= #x80)
706 (write-repeat r1))
707 ;; 3byte encoding
708 ((write ((r1 >> 12) | #xE0))
709 (write (((r1 & #x0FC0) >> 6) | #x80))
710 (r1 &= #x3F)
711 (r1 |= #x80)
712 (write-repeat r1)))))
713
714 (if (r0 == ,(charset-id 'mule-unicode-2500-33ff))
715 ((r0 = ((((r1 & #x3f80) >> 7) - 32) * 96))
716 (r1 &= #x7f)
717 (r1 += (r0 + 9440)) ; 9440 == -32 + #x2500
718 ;; now r1 holds scalar value
719 (write ((r1 >> 12) | #xE0))
720 (write (((r1 & #x0FC0) >> 6) | #x80))
721 (r1 &= #x3F)
722 (r1 |= #x80)
723 (write-repeat r1)))
724
725 (if (r0 == ,(charset-id 'mule-unicode-e000-ffff))
726 ((r0 = ((((r1 & #x3f80) >> 7) - 32) * 96))
727 (r1 &= #x7f)
728 (r1 += (r0 + 57312)) ; 57312 == -32 + #xe000
729 ;; now r1 holds scalar value
730 (write ((r1 >> 12) | #xE0))
731 (write (((r1 & #x0FC0) >> 6) | #x80))
732 (r1 &= #x3F)
733 (r1 |= #x80)
734 (write-repeat r1)))
735
736 (if (r0 == ,(charset-id 'eight-bit-control))
737 ;; r1 scalar utf-8
738 ;; 0000 0yyy yyxx xxxx 110y yyyy 10xx xxxx
739 ;; 80 0000 0000 1000 0000 1100 0010 1000 0000
740 ;; 9f 0000 0000 1001 1111 1100 0010 1001 1111
741 ((write #xC2)
742 (write-repeat r1)))
743
744 (if (r0 == ,(charset-id 'eight-bit-graphic))
745 ;; r1 scalar utf-8
746 ;; 0000 0yyy yyxx xxxx 110y yyyy 10xx xxxx
747 ;; a0 0000 0000 1010 0000 1100 0010 1010 0000
748 ;; ff 0000 0000 1111 1111 1101 1111 1011 1111
749 ((r0 = (r1 >= #xC0))
750 (r0 &= (r1 <= #xC3))
751 (r4 = (r1 >= #xE1))
752 (r4 &= (r1 <= #xF7))
753 (r0 |= r4)
754 (if r0
755 ((call ccl-mule-utf-8-encode-untrans)
756 (repeat))
757 (write-repeat r1))))
758
759 (lookup-character utf-subst-table-for-encode r0 r1)
760 (if r7 ; lookup succeeded
761 (if (r0 < #x800)
762 ;; 2byte encoding
763 ((write ((r0 >> 6) | #xC0))
764 (r0 = ((r0 & #x3F) | #x80))
765 (write-repeat r0))
766 ;; 3byte encoding
767 ((write ((r0 >> 12) | #xE0))
768 (write (((r0 & #x0FC0) >> 6) | #x80))
769 (r0 = ((r0 & #x3F) | #x80))
770 (write-repeat r0))))
771
772 ;; Unsupported character.
773 ;; Output U+FFFD, which is `ef bf bd' in UTF-8.
774 (write #xef)
775 (write #xbf)
776 (write-repeat #xbd))))
777 "CCL program to encode into UTF-8.")
778
779
780 (define-ccl-program ccl-untranslated-to-ucs
781 `(0
782 (if (r1 == 0)
783 nil
784 (if (r0 <= #xC3) ; 2-byte encoding
785 ((r0 = ((r0 & #x3) << 6))
786 (r0 |= (r1 & #x3F))
787 (r1 = 2))
788 (if (r2 == 0)
789 (r1 = 0)
790 (if (r0 < #xF0) ; 3-byte encoding, as above
791 ((r0 = ((r0 & #xF) << 12))
792 (r0 |= ((r1 & #x3F) << 6))
793 (r0 |= (r2 & #x3F))
794 (r1 = 3))
795 (if (r3 == 0)
796 (r1 = 0)
797 ((r0 = ((r0 & #x7) << 18))
798 (r0 |= ((r1 & #x3F) << 12))
799 (r0 |= ((r2 & #x3F) << 6))
800 (r0 |= (r3 & #x3F))
801 (r1 = 4))))))))
802 "Decode 2-, 3-, or 4-byte sequences in r0, r1, r2 [,r3] to unicodes in r0.
803 Set r1 to the byte length. r0 == 0 for invalid sequence.")
804
805 (defvar utf-8-ccl-regs (make-vector 8 0))
806
807 (defsubst utf-8-untranslated-to-ucs ()
808 "Return the UCS code for an untranslated sequence of raw bytes t point.
809 Only for 3- or 4-byte sequences."
810 (aset utf-8-ccl-regs 0 (or (char-after) 0))
811 (aset utf-8-ccl-regs 1 (or (char-after (1+ (point))) 0))
812 (aset utf-8-ccl-regs 2 (or (char-after (+ 2 (point))) 0))
813 (aset utf-8-ccl-regs 3 (or (char-after (+ 3 (point))) 0))
814 (ccl-execute 'ccl-untranslated-to-ucs utf-8-ccl-regs))
815
816 (defun utf-8-help-echo (window object position)
817 (format "Untranslated Unicode U+%04X"
818 (get-char-property position 'untranslated-utf-8 object)))
819
820 ;; We compose the untranslatable sequences into a single character,
821 ;; and move point to the next character.
822 ;; This is infelicitous for editing, because there's currently no
823 ;; mechanism for treating compositions as atomic, but is OK for
824 ;; display. They are composed to U+FFFD with help-echo which
825 ;; indicates the unicodes they represent. This function GCs too much.
826
827 ;; If utf-translate-cjk-mode is non-nil, this function is called with
828 ;; HASH-TABLE which translates CJK characters into some of CJK
829 ;; charsets.
830
831 (defsubst utf-8-compose (hash-table)
832 "Put a suitable composition on an untranslatable sequence at point.
833 If HASH-TABLE is non-nil, try to translate CJK characters by it at first.
834 Move point to the end of the sequence."
835 (utf-8-untranslated-to-ucs)
836 (let ((l (aref utf-8-ccl-regs 1))
837 ch)
838 (if (> l 0)
839 (if (and hash-table
840 (setq ch (gethash (aref utf-8-ccl-regs 0) hash-table)))
841 (progn
842 (insert ch)
843 (delete-region (point) (min (point-max) (+ l (point)))))
844 (setq ch (aref utf-8-ccl-regs 0))
845 (put-text-property (point) (min (point-max) (+ l (point)))
846 'untranslated-utf-8 ch)
847 (put-text-property (point) (min (point-max) (+ l (point)))
848 'help-echo 'utf-8-help-echo)
849 (if (= l 2)
850 (put-text-property (point) (min (point-max) (+ l (point)))
851 'display (format "\\%03o" ch))
852 (compose-region (point) (+ l (point)) ?\e$,3u=\e(B))
853 (forward-char l))
854 (forward-char 1))))
855
856 (defcustom utf-8-compose-scripts nil
857 "*Non-nil means compose various scripts on decoding utf-8 text."
858 :group 'mule
859 :version "21.4"
860 :type 'boolean)
861
862 (defun utf-8-post-read-conversion (length)
863 "Compose untranslated utf-8 sequences into single characters.
864 If `utf-translate-cjk-mode' is non-nil, tries to translate CJK characters.
865 Also compose particular scripts if `utf-8-compose-scripts' is non-nil."
866 (save-excursion
867 (save-restriction
868 (narrow-to-region (point) (+ (point) length))
869 ;; Can't do eval-when-compile to insert a multibyte constant
870 ;; version of the string in the loop, since it's always loaded as
871 ;; unibyte from a byte-compiled file.
872 (let ((range (string-as-multibyte "^\xc0-\xc3\xe1-\xf7"))
873 (buffer-multibyte enable-multibyte-characters)
874 hash-table ch)
875 (set-buffer-multibyte t)
876 (when utf-translate-cjk-mode
877 (if (not utf-translate-cjk-lang-env)
878 ;; Check these characters:
879 ;; "U+2e80-U+33ff", "U+ff00-U+ffef"
880 ;; We may have to translate them to CJK charsets.
881 (let ((range2 "\e$,29@\e(B-\e$,2G\7f\e$,3r`\e(B-\e$,3u/\e(B"))
882 (skip-chars-forward (concat range range2))
883 (unless (eobp)
884 (utf-translate-cjk-load-tables)
885 (setq range (concat range range2)))
886 (setq hash-table (get 'utf-subst-table-for-decode
887 'translation-hash-table)))))
888 (while (and (skip-chars-forward range)
889 (not (eobp)))
890 (setq ch (following-char))
891 (if (< ch 256)
892 (utf-8-compose hash-table)
893 (if (and hash-table
894 (setq ch (gethash (encode-char ch 'ucs) hash-table)))
895 (progn
896 (insert ch)
897 (delete-char 1))
898 (forward-char 1))))
899 (or buffer-multibyte
900 (set-buffer-multibyte nil)))
901
902 (when (and utf-8-compose-scripts (> length 1))
903 ;; These currently have definitions which cover the relevant
904 ;; unicodes. We could avoid loading thai-util &c by checking
905 ;; whether the region contains any characters with the appropriate
906 ;; categories. There aren't yet Unicode-based rules for Tibetan.
907 (diacritic-compose-region (point-max) (point-min))
908 (thai-compose-region (point-max) (point-min))
909 (lao-compose-region (point-max) (point-min))
910 (devanagari-compose-region (point-max) (point-min))
911 (malayalam-compose-region (point-max) (point-min))
912 (tamil-compose-region (point-max) (point-min)))
913 (- (point-max) (point-min)))))
914
915 (defun utf-8-pre-write-conversion (beg end)
916 "Prepare for `utf-translate-cjk-mode' to encode text between BEG and END.
917 This is used as a post-read-conversion of utf-8 coding system."
918 (if (and utf-translate-cjk-mode
919 (not utf-translate-cjk-lang-env)
920 (save-excursion
921 (goto-char beg)
922 (re-search-forward "\\cc\\|\\cj\\|\\ch" end t)))
923 (utf-translate-cjk-load-tables))
924 nil)
925
926 (make-coding-system
927 'mule-utf-8 4 ?u
928 "UTF-8 encoding for Emacs-supported Unicode characters.
929 It supports Unicode characters of these ranges:
930 U+0000..U+33FF, U+E000..U+FFFF.
931 They correspond to these Emacs character sets:
932 ascii, latin-iso8859-1, mule-unicode-0100-24ff,
933 mule-unicode-2500-33ff, mule-unicode-e000-ffff
934
935 On decoding (e.g. reading a file), Unicode characters not in the above
936 ranges are decoded into sequences of eight-bit-control and
937 eight-bit-graphic characters to preserve their byte sequences. The
938 byte sequence is preserved on i/o for valid utf-8, but not necessarily
939 for invalid utf-8.
940
941 On encoding (e.g. writing a file), Emacs characters not belonging to
942 any of the character sets listed above are encoded into the UTF-8 byte
943 sequence representing U+FFFD (REPLACEMENT CHARACTER)."
944
945 '(ccl-decode-mule-utf-8 . ccl-encode-mule-utf-8)
946 `((safe-charsets
947 ascii
948 eight-bit-control
949 eight-bit-graphic
950 latin-iso8859-1
951 mule-unicode-0100-24ff
952 mule-unicode-2500-33ff
953 mule-unicode-e000-ffff
954 ,@(if utf-translate-cjk-mode
955 utf-translate-cjk-charsets))
956 (mime-charset . utf-8)
957 (coding-category . coding-category-utf-8)
958 (valid-codes (0 . 255))
959 (pre-write-conversion . utf-8-pre-write-conversion)
960 (post-read-conversion . utf-8-post-read-conversion)
961 (translation-table-for-encode . utf-translation-table-for-encode)
962 (dependency unify-8859-on-encoding-mode
963 unify-8859-on-decoding-mode
964 utf-fragment-on-decoding
965 utf-translate-cjk-mode)))
966
967 (define-coding-system-alias 'utf-8 'mule-utf-8)
968
969 ;; I think this needs special private charsets defined for the
970 ;; untranslated sequences, if it's going to work well.
971
972 ;;; (defun utf-8-compose-function (pos to pattern &optional string)
973 ;;; (let* ((prop (get-char-property pos 'composition string))
974 ;;; (l (and prop (- (cadr prop) (car prop)))))
975 ;;; (cond ((and l (> l (- to pos)))
976 ;;; (delete-region pos to))
977 ;;; ((and (> (char-after pos) 224)
978 ;;; (< (char-after pos) 256)
979 ;;; (save-restriction
980 ;;; (narrow-to-region pos to)
981 ;;; (utf-8-compose)))
982 ;;; t))))
983
984 ;;; (dotimes (i 96)
985 ;;; (aset composition-function-table
986 ;;; (+ 128 i)
987 ;;; `((,(string-as-multibyte "[\200-\237\240-\377]")
988 ;;; . utf-8-compose-function))))
989
990 ;;; arch-tag: b08735b7-753b-4ae6-b754-0f3efe4515c5
991 ;;; utf-8.el ends here