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