annotate lisp/international/utf-8.el @ 36381:4993e5072bcb

2001-02-24 Michael Kifer <kifer@cs.sunysb.edu> * ediff.texi: Fixed some typos
author Michael Kifer <kifer@cs.stonybrook.edu>
date Sun, 25 Feb 2001 02:02:08 +0000
parents f6bb3ed752b4
children aa776838b660
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35542
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
1 ;;; utf-8.el --- Limited UTF-8 decoding/encoding support
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
2
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
3 ;; Copyright (C) 2001 Electrotechnical Laboratory, JAPAN.
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
4 ;; Licensed to the Free Software Foundation.
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
5
36243
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
6 ;; Keywords: multilingual, Unicode, UTF-8, i18n
35542
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
7
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
8 ;; This file is part of GNU Emacs.
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
9
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
11 ;; it under the terms of the GNU General Public License as published by
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
12 ;; the Free Software Foundation; either version 2, or (at your option)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
13 ;; any later version.
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
14
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
15 ;; GNU Emacs is distributed in the hope that it will be useful,
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
18 ;; GNU General Public License for more details.
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
19
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
20 ;; You should have received a copy of the GNU General Public License
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
23 ;; Boston, MA 02111-1307, USA.
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
24
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
25 ;;; Commentary:
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
26
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
27 ;; The coding-system `mule-utf-8' supports encoding/decoding of the
36243
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
28 ;; following character sets to and from UTF-8:
35542
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
29 ;;
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
30 ;; ascii
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
31 ;; eight-bit-control
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
32 ;; latin-iso8859-1
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
33 ;; mule-unicode-0100-24ff
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
34 ;; mule-unicode-2500-33ff
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
35 ;; mule-unicode-e000-ffff
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
36 ;;
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
37 ;; Characters of other character sets cannot be encoded with
36243
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
38 ;; mule-utf-8. Note that the mule-unicode charsets currently lack
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
39 ;; case and syntax information, so things like `downcase' will only
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
40 ;; work for characters from ASCII and Latin-1.
35542
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
41 ;;
36243
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
42 ;; On decoding, Unicode characters that do not fit into the above
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
43 ;; character sets are handled as `eight-bit-control' or
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
44 ;; `eight-bit-graphic' characters to retain the information about the
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
45 ;; original byte sequence.
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
46
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
47 ;; UTF-8 is defined in RFC 2279. A sketch of the encoding is:
35542
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
48
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
49 ;; scalar | utf-8
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
50 ;; value | 1st byte | 2nd byte | 3rd byte
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
51 ;; --------------------+-----------+-----------+----------
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
52 ;; 0000 0000 0xxx xxxx | 0xxx xxxx | |
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
53 ;; 0000 0yyy yyxx xxxx | 110y yyyy | 10xx xxxx |
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
54 ;; zzzz yyyy yyxx xxxx | 1110 zzzz | 10yy yyyy | 10xx xxxx
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
55
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
56 ;;; Code:
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
57
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
58 (define-ccl-program ccl-decode-mule-utf-8
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
59 ;;
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
60 ;; charset | bytes in utf-8 | bytes in emacs
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
61 ;; -----------------------+----------------+---------------
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
62 ;; ascii | 1 | 1
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
63 ;; -----------------------+----------------+---------------
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
64 ;; eight-bit-control | 2 | 2
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
65 ;; latin-iso8859-1 | 2 | 2
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
66 ;; -----------------------+----------------+---------------
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
67 ;; mule-unicode-0100-24ff | 2 | 4
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
68 ;; (< 0800) | |
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
69 ;; -----------------------+----------------+---------------
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
70 ;; mule-unicode-0100-24ff | 3 | 4
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
71 ;; (>= 8000) | |
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
72 ;; mule-unicode-2500-33ff | 3 | 4
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
73 ;; mule-unicode-e000-ffff | 3 | 4
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
74 ;;
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
75 ;; Thus magnification factor is two.
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
76 ;;
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
77 `(2
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
78 ((loop
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
79 (read r0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
80
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
81 ;; 1byte encoding, i.e., ascii
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
82 (if (r0 < #x80)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
83 (write r0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
84
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
85 ;; 2byte encoding
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
86 (if (r0 < #xe0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
87 ((read r1)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
88 (r0 &= #x1f)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
89 (r0 <<= 6)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
90 (r1 &= #x3f)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
91 (r1 += r0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
92 ;; now r1 holds scalar value
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
93
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
94 ;; eight-bit-control
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
95 (if (r1 < 160)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
96 ((r0 = ,(charset-id 'eight-bit-control))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
97 (write-multibyte-character r0 r1))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
98
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
99 ;; latin-iso8859-1
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
100 (if (r1 < 256)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
101 ((r0 = ,(charset-id 'latin-iso8859-1))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
102 (r1 -= 128)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
103 (write-multibyte-character r0 r1))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
104
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
105 ;; mule-unicode-0100-24ff (< 0800)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
106 ((r0 = ,(charset-id 'mule-unicode-0100-24ff))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
107 (r1 -= #x0100)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
108 (r2 = (((r1 / 96) + 32) << 7))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
109 (r1 %= 96)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
110 (r1 += (r2 + 32))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
111 (write-multibyte-character r0 r1)))))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
112
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
113 ;; 3byte encoding
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
114 (if (r0 < #xf0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
115 ((read r1 r2)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
116 (r3 = ((r0 & #x0f) << 12))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
117 (r3 += ((r1 & #x3f) << 6))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
118 (r3 += (r2 & #x3f))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
119 ;; now r3 holds scalar value
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
120
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
121 ;; mule-unicode-0100-24ff (>= 0800)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
122 (if (r3 < #x2500)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
123 ((r0 = ,(charset-id 'mule-unicode-0100-24ff))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
124 (r3 -= #x0100)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
125 (r3 //= 96)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
126 (r1 = (r7 + 32))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
127 (r1 += ((r3 + 32) << 7))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
128 (write-multibyte-character r0 r1))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
129
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
130 ;; mule-unicode-2500-33ff
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
131 (if (r3 < #x3400)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
132 ((r0 = ,(charset-id 'mule-unicode-2500-33ff))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
133 (r3 -= #x2500)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
134 (r3 //= 96)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
135 (r1 = (r7 + 32))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
136 (r1 += ((r3 + 32) << 7))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
137 (write-multibyte-character r0 r1))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
138
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
139 ;; U+3400 .. U+DFFF
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
140 ;; keep those bytes as eight-bit-{control|graphic}
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
141 (if (r3 < #xe000)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
142 (;; #xe0 < r0 < #xf0, so r0 is eight-bit-graphic
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
143 (r3 = ,(charset-id 'eight-bit-graphic))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
144 (write-multibyte-character r3 r0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
145 (if (r1 < #xa0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
146 (r3 = ,(charset-id 'eight-bit-control)))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
147 (write-multibyte-character r3 r1)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
148 (if (r2 < #xa0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
149 (r3 = ,(charset-id 'eight-bit-control))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
150 (r3 = ,(charset-id 'eight-bit-graphic)))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
151 (write-multibyte-character r3 r2))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
152
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
153 ;; mule-unicode-e000-ffff
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
154 ((r0 = ,(charset-id 'mule-unicode-e000-ffff))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
155 (r3 -= #xe000)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
156 (r3 //= 96)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
157 (r1 = (r7 + 32))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
158 (r1 += ((r3 + 32) << 7))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
159 (write-multibyte-character r0 r1))))))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
160
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
161 ;; 4byte encoding
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
162 ;; keep those bytes as eight-bit-{control|graphic}
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
163 ((read r1 r2 r3)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
164 ;; r0 > #xf0, thus eight-bit-graphic
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
165 (r4 = ,(charset-id 'eight-bit-graphic))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
166 (write-multibyte-character r4 r0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
167 (if (r1 < #xa0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
168 (r4 = ,(charset-id 'eight-bit-control)))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
169 (write-multibyte-character r4 r1)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
170 (if (r2 < #xa0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
171 (r4 = ,(charset-id 'eight-bit-control))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
172 (r4 = ,(charset-id 'eight-bit-graphic)))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
173 (write-multibyte-character r4 r2)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
174 (if (r3 < #xa0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
175 (r4 = ,(charset-id 'eight-bit-control))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
176 (r4 = ,(charset-id 'eight-bit-graphic)))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
177 (write-multibyte-character r4 r3)))))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
178
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
179 (repeat))))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
180
36243
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
181 "CCL program to decode UTF-8.
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
182 Decoding is done into the charsets ascii, eight-bit-control,
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
183 latin-iso8859-1 and mule-unicode-* only.")
35542
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
184
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
185 (define-ccl-program ccl-encode-mule-utf-8
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
186 `(1
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
187 (loop
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
188 (read-multibyte-character r0 r1)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
189
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
190 (if (r0 == ,(charset-id 'ascii))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
191 (write r1)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
192
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
193 (if (r0 == ,(charset-id 'latin-iso8859-1))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
194 ;; r1 scalar utf-8
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
195 ;; 0000 0yyy yyxx xxxx 110y yyyy 10xx xxxx
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
196 ;; 20 0000 0000 1010 0000 1100 0010 1010 0000
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
197 ;; 7f 0000 0000 1111 1111 1100 0011 1011 1111
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
198 ((r0 = (((r1 & #x40) >> 6) | #xc2))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
199 (r1 &= #x3f)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
200 (r1 |= #x80)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
201 (write r0 r1))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
202
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
203 (if (r0 == ,(charset-id 'mule-unicode-0100-24ff))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
204 ((r0 = ((((r1 & #x3f80) >> 7) - 32) * 96))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
205 ;; #x3f80 == (0011 1111 1000 0000)b
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
206 (r1 &= #x7f)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
207 (r1 += (r0 + 224)) ; 240 == -32 + #x0100
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
208 ;; now r1 holds scalar value
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
209 (if (r1 < #x0800)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
210 ;; 2byte encoding
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
211 ((r0 = (((r1 & #x07c0) >> 6) | #xc0))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
212 ;; #x07c0 == (0000 0111 1100 0000)b
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
213 (r1 &= #x3f)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
214 (r1 |= #x80)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
215 (write r0 r1))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
216 ;; 3byte encoding
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
217 ((r0 = (((r1 & #xf000) >> 12) | #xe0))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
218 (r2 = ((r1 & #x3f) | #x80))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
219 (r1 &= #x0fc0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
220 (r1 >>= 6)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
221 (r1 |= #x80)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
222 (write r0 r1 r2))))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
223
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
224 (if (r0 == ,(charset-id 'mule-unicode-2500-33ff))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
225 ((r0 = ((((r1 & #x3f80) >> 7) - 32) * 96))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
226 (r1 &= #x7f)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
227 (r1 += (r0 + 9440)) ; 9440 == -32 + #x2500
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
228 (r0 = (((r1 & #xf000) >> 12) | #xe0))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
229 (r2 = ((r1 & #x3f) | #x80))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
230 (r1 &= #x0fc0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
231 (r1 >>= 6)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
232 (r1 |= #x80)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
233 (write r0 r1 r2))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
234
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
235 (if (r0 == ,(charset-id 'mule-unicode-e000-ffff))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
236 ((r0 = ((((r1 & #x3f80) >> 7) - 32) * 96))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
237 (r1 &= #x7f)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
238 (r1 += (r0 + 57312)) ; 57312 == -160 + #xe000
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
239 (r0 = (((r1 & #xf000) >> 12) | #xe0))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
240 (r2 = ((r1 & #x3f) | #x80))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
241 (r1 &= #x0fc0)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
242 (r1 >>= 6)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
243 (r1 |= #x80)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
244 (write r0 r1 r2))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
245
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
246 (if (r0 == ,(charset-id 'eight-bit-control))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
247 ;; r1 scalar utf-8
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
248 ;; 0000 0yyy yyxx xxxx 110y yyyy 10xx xxxx
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
249 ;; 80 0000 0000 1000 0000 1100 0010 1000 0000
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
250 ;; 9f 0000 0000 1001 1111 1100 0010 1001 1111
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
251 (write r1)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
252
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
253 (if (r0 == ,(charset-id 'eight-bit-graphic))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
254 ;; r1 scalar utf-8
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
255 ;; 0000 0yyy yyxx xxxx 110y yyyy 10xx xxxx
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
256 ;; a0 0000 0000 1010 0000 1100 0010 1010 0000
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
257 ;; ff 0000 0000 1111 1111 1101 1111 1011 1111
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
258 (write r1)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
259
36243
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
260 ;; Unsupported character.
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
261 ;; Output U+FFFD, which is `ef bf bd' in UTF-8.
35542
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
262 ((write #xef)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
263 (write #xbf)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
264 (write #xbd)))))))))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
265 (repeat)))
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
266
36243
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
267 "CCL program to encode into UTF-8.
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
268 Only characters from the charsets ascii, eight-bit-control,
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
269 latin-iso8859-1 and mule-unicode-* are recognized. Others are encoded
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
270 as U+FFFD.")
35542
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
271
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
272 (make-coding-system
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
273 'mule-utf-8 4 ?u
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
274 "UTF-8 encoding for Emacs-supported Unicode characters.
36243
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
275 The supported Emacs character sets are:
35542
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
276 ascii
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
277 eight-bit-control
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
278 eight-bit-graphic
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
279 latin-iso8859-1
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
280 mule-unicode-0100-24ff
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
281 mule-unicode-2500-33ff
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
282 mule-unicode-e000-ffff
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
283
36243
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
284 Unicode characters out of the ranges U+0000-U+33FF and U+E200-U+FFFF
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
285 are decoded into sequences of eight-bit-control and eight-bit-graphic
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
286 characters to preserve their byte sequences. Emacs characters out of
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
287 these ranges are encoded into U+FFFD.
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
288
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
289 Note that, currently, characters in the mule-unicode charsets have no
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
290 syntax and case information. Thus, for instance, upper- and
a05ae5420f85 Doc and commentary fixes.
Dave Love <fx@gnu.org>
parents: 35542
diff changeset
291 lower-casing commands won't work with them."
35542
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
292
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
293 '(ccl-decode-mule-utf-8 . ccl-encode-mule-utf-8)
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
294 '((safe-charsets
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
295 ascii
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
296 eight-bit-control
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
297 eight-bit-graphic
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
298 latin-iso8859-1
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
299 mule-unicode-0100-24ff
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
300 mule-unicode-2500-33ff
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
301 mule-unicode-e000-ffff)
36371
f6bb3ed752b4 (mule-utf-8): Set correct value for valid-codes property.
Kenichi Handa <handa@m17n.org>
parents: 36243
diff changeset
302 (mime-charset . utf-8)
f6bb3ed752b4 (mule-utf-8): Set correct value for valid-codes property.
Kenichi Handa <handa@m17n.org>
parents: 36243
diff changeset
303 (valid-codes (0 . 255))))
35542
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
304
e4a75e66ee46 new file
Kenichi Handa <handa@m17n.org>
parents:
diff changeset
305 (define-coding-system-alias 'utf-8 'mule-utf-8)