(MIN_LEADING_BYTE): Changed to `-0x40'.
[chise/xemacs-chise.git] / src / char-ucs.h
1 /* Header for UCS-4 character representation.
2    Copyright (C) 1999 Electrotechnical Laboratory, JAPAN.
3    Licensed to the Free Software Foundation.
4
5 This file is part of XEmacs.
6
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
11
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; see the file COPYING.  If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 /* Rewritten by MORIOKA Tomohiko <tomo@m17n.org>. */
23
24 #ifndef _XEMACS_CHAR_UCS_H
25 #define _XEMACS_CHAR_UCS_H
26
27 #define valid_char_p(ch) 1
28
29 #define CHAR_ASCII_P(ch) ((ch) <= 0x7F)
30
31 \f
32 DECLARE_LRECORD (char_byte_table, struct Lisp_Char_Byte_Table);
33 #define XCHAR_BYTE_TABLE(x) \
34   XRECORD (x, char_byte_table, struct Lisp_Char_Byte_Table)
35 #define XSETCHAR_BYTE_TABLE(x, p) XSETRECORD (x, p, char_byte_table)
36 #define CHAR_BYTE_TABLE_P(x) RECORDP (x, char_byte_table)
37 #define GC_CHAR_BYTE_TABLE_P(x) GC_RECORDP (x, char_byte_table)
38 /* #define CHECK_CHAR_BYTE_TABLE(x) CHECK_RECORD (x, char_byte_table)
39    char table entries should never escape to Lisp */
40
41 struct Lisp_Char_Byte_Table
42 {
43   struct lcrecord_header header;
44
45   Lisp_Object property[256];
46 };
47
48 Lisp_Object get_char_code_table (Emchar ch, Lisp_Object table);
49
50
51 extern Lisp_Object Vcharset_ucs_bmp;
52 extern Lisp_Object Vcharset_latin_jisx0201;
53 extern Lisp_Object Vcharset_latin_iso8859_2;
54 extern Lisp_Object Vcharset_latin_iso8859_3;
55 extern Lisp_Object Vcharset_latin_iso8859_4;
56 extern Lisp_Object Vcharset_latin_iso8859_9;
57 extern Lisp_Object Vcharset_latin_viscii_lower;
58 extern Lisp_Object Vcharset_latin_viscii_upper;
59
60 \f
61 /************************************************************************/
62 /*                    Definition of leading bytes                       */
63 /************************************************************************/
64
65 typedef int Charset_ID;
66
67 #define MIN_LEADING_BYTE                -0x40
68 #define CHARSET_ID_OFFSET               0x00
69
70 /* represent normal 80-9F */
71 #define LEADING_BYTE_CONTROL_1          (CHARSET_ID_OFFSET - 1)
72
73 /* ISO/IEC 10646 BMP */
74 #define LEADING_BYTE_UCS_BMP            (CHARSET_ID_OFFSET - 2)
75
76 /* VISCII 1.1 */
77 #define LEADING_BYTE_LATIN_VISCII       (CHARSET_ID_OFFSET - 3)
78
79 #define LEADING_BYTE_HIRAGANA_JISX0208  (CHARSET_ID_OFFSET - 4)
80 #define LEADING_BYTE_KATAKANA_JISX0208  (CHARSET_ID_OFFSET - 5)
81
82 #define MIN_LEADING_BYTE_PRIVATE        MIN_LEADING_BYTE
83 #define MAX_LEADING_BYTE_PRIVATE        (CHARSET_ID_OFFSET - 6)
84
85
86 #define CHARSET_ID_OFFSET_94            (CHARSET_ID_OFFSET - '0')
87
88 #define MIN_CHARSET_ID_PRIVATE_94       (CHARSET_ID_OFFSET_94 + '0')
89 #define MAX_CHARSET_ID_PRIVATE_94       (CHARSET_ID_OFFSET_94 + '?')
90
91 /* ISO 646 IRV */
92 #define LEADING_BYTE_ASCII              (CHARSET_ID_OFFSET_94 + 'B')
93
94 /* Right half of JIS X0201-1976 */
95 #define LEADING_BYTE_KATAKANA_JISX0201  (CHARSET_ID_OFFSET_94 + 'I')
96
97 /* Left  half of JIS X0201-1976 */
98 #define LEADING_BYTE_LATIN_JISX0201     (CHARSET_ID_OFFSET_94 + 'J')
99
100
101 #define CHARSET_ID_OFFSET_96            (CHARSET_ID_OFFSET_94 + 80)
102
103 #define LEADING_BYTE_LATIN_VISCII_LOWER (CHARSET_ID_OFFSET_96 + '1')
104 #define LEADING_BYTE_LATIN_VISCII_UPPER (CHARSET_ID_OFFSET_96 + '2')
105
106 /* Right half of ISO 8859-1 */
107 #define LEADING_BYTE_LATIN_ISO8859_1    (CHARSET_ID_OFFSET_96 + 'A')
108
109 /* Right half of ISO 8859-2 */
110 #define LEADING_BYTE_LATIN_ISO8859_2    (CHARSET_ID_OFFSET_96 + 'B')
111
112 /* Right half of ISO 8859-3 */
113 #define LEADING_BYTE_LATIN_ISO8859_3    (CHARSET_ID_OFFSET_96 + 'C')
114
115 /* Right half of ISO 8859-4 */
116 #define LEADING_BYTE_LATIN_ISO8859_4    (CHARSET_ID_OFFSET_96 + 'D')
117
118 /* Right half of ISO 8859-7 */
119 #define LEADING_BYTE_GREEK_ISO8859_7    (CHARSET_ID_OFFSET_96 + 'F')
120
121 /* Right half of ISO 8859-6 */
122 #define LEADING_BYTE_ARABIC_ISO8859_6   (CHARSET_ID_OFFSET_96 + 'G')
123
124 /* Right half of ISO 8859-8 */
125 #define LEADING_BYTE_HEBREW_ISO8859_8   (CHARSET_ID_OFFSET_96 + 'H')
126
127 /* Right half of ISO 8859-5 */
128 #define LEADING_BYTE_CYRILLIC_ISO8859_5 (CHARSET_ID_OFFSET_96 + 'L')
129
130 /* Right half of ISO 8859-9 */
131 #define LEADING_BYTE_LATIN_ISO8859_9    (CHARSET_ID_OFFSET_96 + 'M')
132
133 /* TIS620-2533 */
134 #define LEADING_BYTE_THAI_TIS620        (CHARSET_ID_OFFSET_96 + 'T')
135
136
137 #define CHARSET_ID_OFFSET_94x94         (CHARSET_ID_OFFSET_96 + 80)
138
139 /* Big5 Level 1 */
140 #define LEADING_BYTE_CHINESE_BIG5_1     ('0' + CHARSET_ID_OFFSET_94x94)
141
142 /* Big5 Level 2 */
143 #define LEADING_BYTE_CHINESE_BIG5_2     ('1' + CHARSET_ID_OFFSET_94x94)
144
145 #define MIN_LEADING_BYTE_PRIVATE_2      ('0' + CHARSET_ID_OFFSET_94x94)
146 #define MAX_LEADING_BYTE_PRIVATE_2      ('?' + CHARSET_ID_OFFSET_94x94)
147
148 /* Japanese JIS X0208-1978 */
149 #define LEADING_BYTE_JAPANESE_JISX0208_1978 \
150                                         ('@' + CHARSET_ID_OFFSET_94x94)
151
152 /* Chinese Hanzi GB2312-1980 */
153 #define LEADING_BYTE_CHINESE_GB2312     ('A' + CHARSET_ID_OFFSET_94x94)
154
155 /* Japanese JIS X0208-1983 */
156 #define LEADING_BYTE_JAPANESE_JISX0208  ('B' + CHARSET_ID_OFFSET_94x94)
157
158 /* Hangul KS C5601-1987 */
159 #define LEADING_BYTE_KOREAN_KSC5601     ('C' + CHARSET_ID_OFFSET_94x94)
160
161 /* Japanese JIS X0212-1990 */
162 #define LEADING_BYTE_JAPANESE_JISX0212  ('D' + CHARSET_ID_OFFSET_94x94)
163
164 /* CCITT Extended GB */
165 #define LEADING_BYTE_CHINESE_CCITT_GB   ('E' + CHARSET_ID_OFFSET_94x94)
166
167 /* Chinese CNS11643 Set 1 */
168 #define LEADING_BYTE_CHINESE_CNS11643_1 ('G' + CHARSET_ID_OFFSET_94x94)
169
170 /* Chinese CNS11643 Set 2 */
171 #define LEADING_BYTE_CHINESE_CNS11643_2 ('H' + CHARSET_ID_OFFSET_94x94)
172
173 /* Chinese CNS11643 Set 3 */
174 #define LEADING_BYTE_CHINESE_CNS11643_3 ('I' + CHARSET_ID_OFFSET_94x94)
175
176 /* Chinese CNS11643 Set 4 */
177 #define LEADING_BYTE_CHINESE_CNS11643_4 ('J' + CHARSET_ID_OFFSET_94x94)
178
179 /* Chinese CNS11643 Set 5 */
180 #define LEADING_BYTE_CHINESE_CNS11643_5 ('K' + CHARSET_ID_OFFSET_94x94)
181
182 /* Chinese CNS11643 Set 6 */
183 #define LEADING_BYTE_CHINESE_CNS11643_6 ('L' + CHARSET_ID_OFFSET_94x94)
184
185 /* Chinese CNS11643 Set 7 */
186 #define LEADING_BYTE_CHINESE_CNS11643_7 ('M' + CHARSET_ID_OFFSET_94x94)
187
188 /* DPRK Hangul KPS 9566-1997 */
189 #define LEADING_BYTE_KOREAN_KPS9566     ('N' + CHARSET_ID_OFFSET_94x94)
190
191
192 #define NUM_LEADING_BYTES       (80 * 3 - MIN_LEADING_BYTE)
193
194 \f
195 /************************************************************************/
196 /*            Information about a particular character set              */
197 /************************************************************************/
198
199 struct Lisp_Charset
200 {
201   struct lcrecord_header header;
202
203   int id;
204   Lisp_Object name;
205   Lisp_Object doc_string, registry, short_name, long_name;
206
207   Lisp_Object reverse_direction_charset;
208
209   Lisp_Object ccl_program;
210
211   /* Final byte of this character set in ISO2022 designating escape sequence */
212   Bufbyte final;
213
214   /* Number of columns a character in this charset takes up, on TTY
215      devices.  Not used for X devices. */
216   unsigned int columns;
217
218   /* Direction of this character set */
219   unsigned int direction;
220
221   /* Type of this character set (94, 96, 94x94, 96x96) */
222   unsigned int type;
223
224   /* Number of bytes used in encoding of this character set (1 or 2) */
225   unsigned int dimension;
226
227   /* Number of chars in each dimension (usually 94 or 96) */
228   unsigned int chars;
229
230   /* Which half of font to be used to display this character set */
231   unsigned int graphic;
232
233   /* Byte->character mapping table */
234   Lisp_Object decoding_table;
235
236   /* Character->byte mapping table */
237   Lisp_Object encoding_table;
238
239   /* Range of character code */
240   Emchar ucs_min, ucs_max;
241
242   /* Offset for external code */
243   Emchar code_offset;
244
245   /* Offset for each byte */
246   Emchar byte_offset;
247 };
248
249 DECLARE_LRECORD (charset, struct Lisp_Charset);
250 #define XCHARSET(x) XRECORD (x, charset, struct Lisp_Charset)
251 #define XSETCHARSET(x, p) XSETRECORD (x, p, charset)
252 #define CHARSETP(x) RECORDP (x, charset)
253 #define GC_CHARSETP(x) GC_RECORDP (x, charset)
254 #define CHECK_CHARSET(x) CHECK_RECORD (x, charset)
255 #define CONCHECK_CHARSET(x) CONCHECK_RECORD (x, charset)
256
257 #define CHARSET_TYPE_94      0 /* This charset includes 94      characters. */
258 #define CHARSET_TYPE_94X94   1 /* This charset includes 94x94   characters. */
259 #define CHARSET_TYPE_96      2 /* This charset includes 96      characters. */
260 #define CHARSET_TYPE_96X96   3 /* This charset includes 96x96   characters. */
261 #define CHARSET_TYPE_128     4 /* This charset includes 128     characters. */
262 #define CHARSET_TYPE_128X128 5 /* This charset includes 128x128 characters. */
263 #define CHARSET_TYPE_256     6 /* This charset includes 256     characters. */
264 #define CHARSET_TYPE_256X256 7 /* This charset includes 256x256 characters. */
265
266 #define CHARSET_LEFT_TO_RIGHT   0
267 #define CHARSET_RIGHT_TO_LEFT   1
268
269 /* Leading byte and id have been regrouped. -- OG */
270 #define CHARSET_ID(cs)           ((cs)->id)
271 #define CHARSET_LEADING_BYTE(cs) (CHARSET_ID(cs))
272 #define CHARSET_NAME(cs)         ((cs)->name)
273 #define CHARSET_SHORT_NAME(cs)   ((cs)->short_name)
274 #define CHARSET_LONG_NAME(cs)    ((cs)->long_name)
275 #define CHARSET_COLUMNS(cs)      ((cs)->columns)
276 #define CHARSET_GRAPHIC(cs)      ((cs)->graphic)
277 #define CHARSET_TYPE(cs)         ((cs)->type)
278 #define CHARSET_DIRECTION(cs)    ((cs)->direction)
279 #define CHARSET_FINAL(cs)        ((cs)->final)
280 #define CHARSET_DOC_STRING(cs)   ((cs)->doc_string)
281 #define CHARSET_REGISTRY(cs)     ((cs)->registry)
282 #define CHARSET_CCL_PROGRAM(cs)  ((cs)->ccl_program)
283 #define CHARSET_DIMENSION(cs)    ((cs)->dimension)
284 #define CHARSET_CHARS(cs)        ((cs)->chars)
285 #define CHARSET_REVERSE_DIRECTION_CHARSET(cs) ((cs)->reverse_direction_charset)
286 #define CHARSET_DECODING_TABLE(cs) ((cs)->decoding_table)
287 #define CHARSET_ENCODING_TABLE(cs) ((cs)->encoding_table)
288 #define CHARSET_UCS_MIN(cs)      ((cs)->ucs_min)
289 #define CHARSET_UCS_MAX(cs)      ((cs)->ucs_max)
290 #define CHARSET_CODE_OFFSET(cs)  ((cs)->code_offset)
291 #define CHARSET_BYTE_OFFSET(cs)  ((cs)->byte_offset)
292
293
294 #define XCHARSET_ID(cs)           CHARSET_ID           (XCHARSET (cs))
295 #define XCHARSET_NAME(cs)         CHARSET_NAME         (XCHARSET (cs))
296 #define XCHARSET_SHORT_NAME(cs)   CHARSET_SHORT_NAME   (XCHARSET (cs))
297 #define XCHARSET_LONG_NAME(cs)    CHARSET_LONG_NAME    (XCHARSET (cs))
298 #define XCHARSET_GRAPHIC(cs)      CHARSET_GRAPHIC      (XCHARSET (cs))
299 #define XCHARSET_TYPE(cs)         CHARSET_TYPE         (XCHARSET (cs))
300 #define XCHARSET_DIRECTION(cs)    CHARSET_DIRECTION    (XCHARSET (cs))
301 #define XCHARSET_FINAL(cs)        CHARSET_FINAL        (XCHARSET (cs))
302 #define XCHARSET_DOC_STRING(cs)   CHARSET_DOC_STRING   (XCHARSET (cs))
303 #define XCHARSET_REGISTRY(cs)     CHARSET_REGISTRY     (XCHARSET (cs))
304 #define XCHARSET_LEADING_BYTE(cs) CHARSET_LEADING_BYTE (XCHARSET (cs))
305 #define XCHARSET_CCL_PROGRAM(cs)  CHARSET_CCL_PROGRAM  (XCHARSET (cs))
306 #define XCHARSET_DIMENSION(cs)    CHARSET_DIMENSION    (XCHARSET (cs))
307 #define XCHARSET_CHARS(cs)        CHARSET_CHARS        (XCHARSET (cs))
308 #define XCHARSET_REVERSE_DIRECTION_CHARSET(cs) \
309   CHARSET_REVERSE_DIRECTION_CHARSET (XCHARSET (cs))
310 #define XCHARSET_DECODING_TABLE(cs) CHARSET_DECODING_TABLE(XCHARSET(cs))
311 #define XCHARSET_ENCODING_TABLE(cs) CHARSET_ENCODING_TABLE(XCHARSET(cs))
312 #define XCHARSET_UCS_MIN(cs)      CHARSET_UCS_MIN(XCHARSET(cs))
313 #define XCHARSET_UCS_MAX(cs)      CHARSET_UCS_MAX(XCHARSET(cs))
314 #define XCHARSET_CODE_OFFSET(cs)  CHARSET_CODE_OFFSET(XCHARSET(cs))
315 #define XCHARSET_BYTE_OFFSET(cs)  CHARSET_BYTE_OFFSET(XCHARSET(cs))
316
317 /* Table of charsets indexed by (leading byte - 128). */
318 extern Lisp_Object charset_by_leading_byte[NUM_LEADING_BYTES];
319
320 /* Table of charsets indexed by type/final-byte. */
321 extern Lisp_Object charset_by_attributes[4][128];
322
323 #ifdef ERROR_CHECK_TYPECHECK
324 /* int not Bufbyte even though that is the actual type of a leading byte.
325    This way, out-ot-range values will get caught rather than automatically
326    truncated. */
327 INLINE Lisp_Object CHARSET_BY_LEADING_BYTE (Charset_ID lb);
328 INLINE Lisp_Object
329 CHARSET_BY_LEADING_BYTE (Charset_ID lb)
330 {
331   assert (lb >= MIN_LEADING_BYTE &&
332           lb < (MIN_LEADING_BYTE + NUM_LEADING_BYTES));
333   return charset_by_leading_byte[lb - MIN_LEADING_BYTE];
334 }
335
336 #else
337
338 #define CHARSET_BY_LEADING_BYTE(lb) \
339   (charset_by_leading_byte[(lb) - MIN_LEADING_BYTE])
340
341 #endif
342
343 #define CHARSET_BY_ATTRIBUTES(type, final, dir) \
344   (charset_by_attributes[type][final])
345
346 \f
347 /************************************************************************/
348 /*                        Dealing with characters                       */
349 /************************************************************************/
350
351 #define MAX_CHAR_BASIC_LATIN    0x007F
352
353 #define MIN_CHAR_GREEK          0x0370
354 #define MAX_CHAR_GREEK          0x03CF
355
356 #define MIN_CHAR_CYRILLIC       0x0400
357 #define MAX_CHAR_CYRILLIC       0x045F
358
359 #define MIN_CHAR_HEBREW         0x0590
360 #define MAX_CHAR_HEBREW         0x05EF
361
362 #define MIN_CHAR_THAI           0x0E00
363 #define MAX_CHAR_THAI           0x0E5F
364
365 #define MIN_CHAR_HIRAGANA       0x3041
366 #define MAX_CHAR_HIRAGANA       0x3093
367
368 #define MIN_CHAR_KATAKANA       0x30A1
369 #define MAX_CHAR_KATAKANA       0x30F6
370
371 #define MIN_CHAR_HALFWIDTH_KATAKANA     0xFF61
372 #define MAX_CHAR_HALFWIDTH_KATAKANA     0xFF9F
373
374 #define MIN_CHAR_OBS_94x94      0xE00000
375 #define MAX_CHAR_OBS_94x94      (0xE00000 + 94 * 94 * 14 - 1)
376
377 #define MIN_CHAR_94             0xE90940
378 #define MAX_CHAR_94             (MIN_CHAR_94 + 94 * 80 - 1)
379 #define MIN_CHAR_96             (MIN_CHAR_94 + 94 * 80)
380 #define MAX_CHAR_96             (MIN_CHAR_96 + 96 * 80 - 1)
381
382 #define MIN_CHAR_94x94          0xE9F6C0
383 #define MAX_CHAR_94x94          (MIN_CHAR_94x94 + 94 * 94 * 80 - 1)
384 #define MIN_CHAR_96x96          0xF4C000
385 #define MAX_CHAR_96x96          (MIN_CHAR_96x96 + 96 * 96 * 80 - 1)
386
387
388 /* Return a character whose charset is CHARSET and position-codes
389    are C1 and C2.  TYPE9N character ignores C2. */
390
391 INLINE Emchar MAKE_CHAR (Lisp_Object charset, int c1, int c2);
392 INLINE Emchar
393 MAKE_CHAR (Lisp_Object charset, int c1, int c2)
394 {
395   Lisp_Object decoding_table = XCHARSET_DECODING_TABLE (charset);
396   int idx;
397   Lisp_Object ch;
398
399   if (!EQ (decoding_table, Qnil)
400       && (0 <= (idx = c1 - XCHARSET_BYTE_OFFSET (charset)))
401       && (idx < XVECTOR_LENGTH (decoding_table))
402       && !EQ (ch = XVECTOR_DATA(decoding_table)[idx], Qnil))
403     {
404       if (VECTORP (ch))
405         {
406           if ((0 <= (idx = c2 - XCHARSET_BYTE_OFFSET (charset)))
407               && (idx < XVECTOR_LENGTH (ch))
408               && !EQ (ch = XVECTOR_DATA(ch)[idx], Qnil))
409             return XCHAR (ch);
410         }
411       else
412         return XCHAR (ch);
413     }
414   if (EQ (charset, Vcharset_katakana_jisx0201))
415     if (c1 < 0x60)
416       return c1 + MIN_CHAR_HALFWIDTH_KATAKANA - 33;
417     else
418       /* return MIN_CHAR_94 + ('I' - '0') * 94 + (c1 - 33); */
419       return ' ';
420   else if (XCHARSET_UCS_MAX (charset))
421     {
422       Emchar code
423         = (XCHARSET_DIMENSION (charset) == 1
424            ?
425            c1 - XCHARSET_BYTE_OFFSET (charset)
426            :
427            (c1 - XCHARSET_BYTE_OFFSET (charset)) * XCHARSET_CHARS (charset)
428            + c2  - XCHARSET_BYTE_OFFSET (charset))
429         - XCHARSET_CODE_OFFSET (charset) + XCHARSET_UCS_MIN (charset);
430       if ((code < XCHARSET_UCS_MIN (charset))
431           || (XCHARSET_UCS_MAX (charset) < code))
432         signal_simple_error ("Arguments makes invalid character",
433                              make_char (code));
434       return code;
435     }
436   else if (XCHARSET_DIMENSION (charset) == 1)
437     {
438       switch (XCHARSET_CHARS (charset))
439         {
440         case 94:
441           return MIN_CHAR_94
442             + (XCHARSET_FINAL (charset) - '0') * 94 + (c1 - 33);
443         case 96:
444           return MIN_CHAR_96
445             + (XCHARSET_FINAL (charset) - '0') * 96 + (c1 - 32);
446         default:
447           abort ();
448         }
449     }
450   else
451     {
452       switch (XCHARSET_CHARS (charset))
453         {
454         case 94:
455           return MIN_CHAR_94x94
456             + (XCHARSET_FINAL (charset) - '0') * 94 * 94
457             + (c1 - 33) * 94 + (c2 - 33);
458         case 96:
459           return MIN_CHAR_96x96
460             + (XCHARSET_FINAL (charset) - '0') * 96 * 96
461             + (c1 - 32) * 96 + (c2 - 32);
462         default:
463           abort ();
464         }
465     }
466 }
467
468 unsigned char charset_get_byte1 (Lisp_Object charset, Emchar ch);
469 unsigned char charset_get_byte2 (Lisp_Object charset, Emchar ch);
470
471 extern Lisp_Object Vdefault_coded_charset_priority_list;
472 EXFUN (Ffind_charset, 1);
473
474 INLINE void breakup_char_1 (Emchar c, Lisp_Object *charset, int *c1, int *c2);
475 INLINE void
476 breakup_char_1 (Emchar c, Lisp_Object *charset, int *c1, int *c2)
477 {
478   if (c < MIN_CHAR_OBS_94x94)
479     {
480       Lisp_Object charsets = Vdefault_coded_charset_priority_list;
481       while (!EQ (charsets, Qnil))
482         {
483           *charset = Ffind_charset (Fcar (charsets));
484           if (!EQ (*charset, Qnil)
485               && (*c1 = charset_get_byte1 (*charset, c)) )
486             {
487               *c2 = charset_get_byte2 (*charset, c);
488               return;
489             }
490           charsets = Fcdr (charsets);         
491         }
492       /* otherwise --- maybe for bootstrap */
493       if (c <= MAX_CHAR_BASIC_LATIN)
494         {
495           *charset = Vcharset_ascii;
496           *c1 = charset_get_byte1 (*charset, c);
497           *c2 = charset_get_byte2 (*charset, c);
498         }
499       else if (c < 0xA0)
500         {
501           *charset = Vcharset_control_1;
502           *c1 = charset_get_byte1 (*charset, c);
503           *c2 = charset_get_byte2 (*charset, c);
504         }
505       else if (c <= 0xff)
506         {
507           *charset = Vcharset_latin_iso8859_1;
508           *c1 = charset_get_byte1 (*charset, c);
509           *c2 = charset_get_byte2 (*charset, c);
510         }
511       else if ((MIN_CHAR_GREEK <= c) && (c <= MAX_CHAR_GREEK))
512         {
513           *charset = Vcharset_greek_iso8859_7;
514           *c1 = c - MIN_CHAR_GREEK + 0x20;
515           *c2 = 0;
516         }
517       else if ((MIN_CHAR_CYRILLIC <= c) && (c <= MAX_CHAR_CYRILLIC))
518         {
519           *charset = Vcharset_cyrillic_iso8859_5;
520           *c1 = c - MIN_CHAR_CYRILLIC + 0x20;
521           *c2 = 0;
522         }
523       else if ((MIN_CHAR_HEBREW <= c) && (c <= MAX_CHAR_HEBREW))
524         {
525           *charset = Vcharset_hebrew_iso8859_8;
526           *c1 = c - MIN_CHAR_HEBREW + 0x20;
527           *c2 = 0;
528         }
529       else if ((MIN_CHAR_THAI <= c) && (c <= MAX_CHAR_THAI))
530         {
531           *charset = Vcharset_thai_tis620;
532           *c1 = c - MIN_CHAR_THAI + 0x20;
533           *c2 = 0;
534         }
535       else if ((MIN_CHAR_HALFWIDTH_KATAKANA <= c)
536                && (c <= MAX_CHAR_HALFWIDTH_KATAKANA))
537         {
538           *charset = Vcharset_katakana_jisx0201;
539           *c1 = c - MIN_CHAR_HALFWIDTH_KATAKANA + 33;
540           *c2 = 0;
541         }
542       else
543         {
544           *charset = Vcharset_ucs_bmp;
545           *c1 = c >> 8;
546           *c2 = c & 0xff;
547         }
548     }
549   else if (c <= MAX_CHAR_OBS_94x94)
550     {
551       *charset
552         = CHARSET_BY_ATTRIBUTES (CHARSET_TYPE_94X94,
553                                  ((c - MIN_CHAR_OBS_94x94) / (94 * 94)) + '@',
554                                  CHARSET_LEFT_TO_RIGHT);
555       *c1 = (((c - MIN_CHAR_OBS_94x94) / 94) % 94) + 33;
556       *c2 = ((c - MIN_CHAR_OBS_94x94) % 94) + 33;
557     }
558   else if (c <= MAX_CHAR_94)
559     {
560       *charset
561         = CHARSET_BY_ATTRIBUTES (CHARSET_TYPE_94,
562                                  ((c - MIN_CHAR_94) / 94) + '0',
563                                  CHARSET_LEFT_TO_RIGHT);
564       *c1 = ((c - MIN_CHAR_94) % 94) + 33;
565       *c2 = 0;
566     }
567   else if (c <= MAX_CHAR_96)
568     {
569       *charset
570         = CHARSET_BY_ATTRIBUTES (CHARSET_TYPE_96,
571                                  ((c - MIN_CHAR_96) / 96) + '0',
572                                  CHARSET_LEFT_TO_RIGHT);
573       *c1 = ((c - MIN_CHAR_96) % 96) + 32;
574       *c2 = 0;
575     }
576   else if (c <= MAX_CHAR_94x94)
577     {
578       *charset
579         = CHARSET_BY_ATTRIBUTES (CHARSET_TYPE_94X94,
580                                  ((c - MIN_CHAR_94x94) / (94 * 94)) + '0',
581                                  CHARSET_LEFT_TO_RIGHT);
582       *c1 = (((c - MIN_CHAR_94x94) / 94) % 94) + 33;
583       *c2 = ((c - MIN_CHAR_94x94) % 94) + 33;
584     }
585   else if (c <= MAX_CHAR_96x96)
586     {
587       *charset
588         = CHARSET_BY_ATTRIBUTES (CHARSET_TYPE_96X96,
589                                  ((c - MIN_CHAR_96x96) / (96 * 96)) + '0',
590                                  CHARSET_LEFT_TO_RIGHT);
591       *c1 = (((c - MIN_CHAR_96x96) / 96) % 96) + 32;
592       *c2 = ((c - MIN_CHAR_96x96) % 96) + 32;
593     }
594   else
595     {
596       printf("u+%x", c);
597       abort();
598     }
599 }
600
601 #define BREAKUP_CHAR(ch, charset, b1, b2) \
602   breakup_char_1 (ch, &(charset), &(b1), &(b2))
603
604 INLINE Lisp_Object CHAR_CHARSET (Emchar ch);
605 INLINE Lisp_Object
606 CHAR_CHARSET (Emchar ch)
607 {
608   Lisp_Object charset;
609   int b1, b2;
610
611   BREAKUP_CHAR(ch, charset, b1, b2);
612   return charset;
613 }
614
615 #define CHAR_LEADING_BYTE(c) (XCHARSET_LEADING_BYTE(CHAR_CHARSET(c)))
616
617 #define CHAR_COLUMNS(c)     (CHARSET_COLUMNS(XCHARSET(CHAR_CHARSET(c))))
618
619 \f
620 /************************************************************************/
621 /*                            Exported functions                        */
622 /************************************************************************/
623
624 EXFUN (Fget_charset, 1);
625
626 extern Lisp_Object Vcharset_chinese_big5_1;
627 extern Lisp_Object Vcharset_chinese_big5_2;
628 extern Lisp_Object Vcharset_japanese_jisx0208;
629 extern Lisp_Object Vcharset_japanese_jisx0212;
630
631 #endif /* _XEMACS_CHAR_UCS_H */