Rename `chinese-cns11643-6' to `=cns11643-6'.
[chise/xemacs-chise.git.1] / src / chartab.h
1 /* Declarations having to do with Mule char tables.
2    Copyright (C) 1992 Free Software Foundation, Inc.
3    Copyright (C) 1995 Sun Microsystems, Inc.
4    Copyright (C) 1999,2000,2001,2002 MORIOKA Tomohiko
5
6 This file is part of XEmacs.
7
8 XEmacs is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
12
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with XEmacs; see the file COPYING.  If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23 /* Synched up with: Mule 2.3.  Not synched with FSF.
24
25    This file was written independently of the FSF implementation,
26    and is not compatible. */
27
28 #ifndef INCLUDED_chartab_h_
29 #define INCLUDED_chartab_h_
30
31
32 #ifdef UTF2000
33
34 #ifdef HAVE_CHISE_CLIENT
35 #include "database.h"
36 #endif
37
38 EXFUN (Fmake_char, 3);
39 EXFUN (Fdecode_char, 3);
40
41 EXFUN (Fput_char_attribute, 3);
42
43 EXFUN (Ffind_char, 1);
44
45 extern Lisp_Object Qdowncase, Qflippedcase, Q_lowercase, Q_uppercase;
46
47
48 /************************************************************************/
49 /*                          Char-ID Tables                              */
50 /************************************************************************/
51
52 struct Lisp_Uint8_Byte_Table
53 {
54   struct lcrecord_header header;
55
56   unsigned char property[256];
57 };
58 typedef struct Lisp_Uint8_Byte_Table Lisp_Uint8_Byte_Table;
59
60 DECLARE_LRECORD (uint8_byte_table, Lisp_Uint8_Byte_Table);
61 #define XUINT8_BYTE_TABLE(x) \
62    XRECORD (x, uint8_byte_table, Lisp_Uint8_Byte_Table)
63 #define XSETUINT8_BYTE_TABLE(x, p) XSETRECORD (x, p, uint8_byte_table)
64 #define UINT8_BYTE_TABLE_P(x) RECORDP (x, uint8_byte_table)
65 #define GC_UINT8_BYTE_TABLE_P(x) GC_RECORDP (x, uint8_byte_table)
66 /* #define CHECK_UINT8_BYTE_TABLE(x) CHECK_RECORD (x, uint8_byte_table)
67    char table entries should never escape to Lisp */
68
69
70 struct Lisp_Uint16_Byte_Table
71 {
72   struct lcrecord_header header;
73
74   unsigned short property[256];
75 };
76 typedef struct Lisp_Uint16_Byte_Table Lisp_Uint16_Byte_Table;
77
78 DECLARE_LRECORD (uint16_byte_table, Lisp_Uint16_Byte_Table);
79 #define XUINT16_BYTE_TABLE(x) \
80    XRECORD (x, uint16_byte_table, Lisp_Uint16_Byte_Table)
81 #define XSETUINT16_BYTE_TABLE(x, p) XSETRECORD (x, p, uint16_byte_table)
82 #define UINT16_BYTE_TABLE_P(x) RECORDP (x, uint16_byte_table)
83 #define GC_UINT16_BYTE_TABLE_P(x) GC_RECORDP (x, uint16_byte_table)
84 /* #define CHECK_UINT16_BYTE_TABLE(x) CHECK_RECORD (x, uint16_byte_table)
85    char table entries should never escape to Lisp */
86
87
88 struct Lisp_Byte_Table
89 {
90   struct lcrecord_header header;
91
92   Lisp_Object property[256];
93 };
94 typedef struct Lisp_Byte_Table Lisp_Byte_Table;
95
96 DECLARE_LRECORD (byte_table, Lisp_Byte_Table);
97 #define XBYTE_TABLE(x) XRECORD (x, byte_table, Lisp_Byte_Table)
98 #define XSETBYTE_TABLE(x, p) XSETRECORD (x, p, byte_table)
99 #define BYTE_TABLE_P(x) RECORDP (x, byte_table)
100 #define GC_BYTE_TABLE_P(x) GC_RECORDP (x, byte_table)
101 /* #define CHECK_BYTE_TABLE(x) CHECK_RECORD (x, byte_table)
102    char table entries should never escape to Lisp */
103
104 Lisp_Object get_byte_table (Lisp_Object table, unsigned char idx);
105
106 Lisp_Object put_byte_table (Lisp_Object table, unsigned char idx,
107                             Lisp_Object value);
108
109
110 Lisp_Object make_char_id_table (Lisp_Object initval);
111
112 #endif
113
114
115 /************************************************************************/
116 /*                               Char Tables                            */
117 /************************************************************************/
118
119 /* Under Mule, we use a complex representation (see below).
120    When not under Mule, there are only 256 possible characters
121    so we just represent them directly. */
122
123 #if defined(MULE)&&!defined(UTF2000)
124
125 struct Lisp_Char_Table_Entry
126 {
127   struct lcrecord_header header;
128
129   /* In the interests of simplicity, we just use a fixed 96-entry
130      table.  If we felt like being smarter, we could make this
131      variable-size and add an offset value into this structure. */
132   Lisp_Object level2[96];
133 };
134 typedef struct Lisp_Char_Table_Entry Lisp_Char_Table_Entry;
135
136 DECLARE_LRECORD (char_table_entry, Lisp_Char_Table_Entry);
137 #define XCHAR_TABLE_ENTRY(x) \
138   XRECORD (x, char_table_entry, Lisp_Char_Table_Entry)
139 #define XSETCHAR_TABLE_ENTRY(x, p) XSETRECORD (x, p, char_table_entry)
140 #define CHAR_TABLE_ENTRYP(x) RECORDP (x, char_table_entry)
141 /* #define CHECK_CHAR_TABLE_ENTRY(x) CHECK_RECORD (x, char_table_entry)
142    char table entries should never escape to Lisp */
143
144 #endif /* MULE */
145
146 enum char_table_type
147 {
148   CHAR_TABLE_TYPE_GENERIC,
149 #ifdef MULE
150   CHAR_TABLE_TYPE_CATEGORY,
151 #endif
152   CHAR_TABLE_TYPE_SYNTAX,
153   CHAR_TABLE_TYPE_DISPLAY,
154   CHAR_TABLE_TYPE_CHAR
155 };
156
157 #ifndef UTF2000
158 #ifdef MULE
159 #define NUM_ASCII_CHARS 160
160 #else
161 #define NUM_ASCII_CHARS 256
162 #endif
163 #endif
164
165 struct Lisp_Char_Table
166 {
167   struct lcrecord_header header;
168
169 #ifdef UTF2000
170   Lisp_Object table;
171   Lisp_Object default_value;
172   Lisp_Object name;
173   Lisp_Object db;
174   unsigned char unloaded;
175 #else
176   Lisp_Object ascii[NUM_ASCII_CHARS];
177
178 #ifdef MULE
179   /* We basically duplicate the Mule vectors-of-vectors implementation.
180      We can do this because we know a great deal about the sorts of
181      things we are going to be indexing.
182
183      The current implementation is as follows:
184
185      ascii[0-159] is used for ASCII and Control-1 characters.
186
187      level1[0 .. (NUM_LEADING_BYTES-1)] indexes charsets by leading
188      byte (subtract MIN_LEADING_BYTE from the leading byte).  If the
189      value of this is not an opaque, then it specifies a value for all
190      characters in the charset.  Otherwise, it will be a
191      96-Lisp-Object opaque that we created, specifying a value for
192      each row.  If the value of this is not an opaque, then it
193      specifies a value for all characters in the row.  Otherwise, it
194      will be a 96-Lisp-Object opaque that we created, specifying a
195      value for each character.
196
197      NOTE: 1) This will fail if some C routine passes an opaque to
198               Fput_char_table().  Currently this is not a problem
199               since all char tables that are created are Lisp-visible
200               and thus no one should ever be putting an opaque in
201               a char table.  Another possibility is to consider
202               adding a type to */
203
204   Lisp_Object level1[NUM_LEADING_BYTES];
205
206 #endif /* MULE */
207 #endif /* non UTF2000 */
208
209   enum char_table_type type;
210
211 #ifndef UTF2000
212   /* stuff used for syntax tables */
213   Lisp_Object mirror_table;
214 #endif
215   Lisp_Object next_table; /* DO NOT mark through this. */
216 };
217 typedef struct Lisp_Char_Table Lisp_Char_Table;
218
219 DECLARE_LRECORD (char_table, Lisp_Char_Table);
220 #define XCHAR_TABLE(x) XRECORD (x, char_table, Lisp_Char_Table)
221 #define XSETCHAR_TABLE(x, p) XSETRECORD (x, p, char_table)
222 #define CHAR_TABLEP(x) RECORDP (x, char_table)
223 #define CHECK_CHAR_TABLE(x) CHECK_RECORD (x, char_table)
224 #define CONCHECK_CHAR_TABLE(x) CONCHECK_RECORD (x, char_table)
225
226 #define CHAR_TABLE_TYPE(ct) ((ct)->type)
227 #define XCHAR_TABLE_TYPE(ct) CHAR_TABLE_TYPE (XCHAR_TABLE (ct))
228
229 #ifdef UTF2000
230
231 #define CHAR_TABLE_NAME(ct) ((ct)->name)
232 #define XCHAR_TABLE_NAME(ct) CHAR_TABLE_NAME (XCHAR_TABLE (ct))
233
234 #define CHAR_TABLE_UNLOADED(ct) ((ct)->unloaded)
235 #define XCHAR_TABLE_UNLOADED(ct) CHAR_TABLE_UNLOADED (XCHAR_TABLE (ct))
236
237 INLINE_HEADER Lisp_Object
238 CHAR_TABLE_VALUE_UNSAFE (Lisp_Char_Table *ct, Emchar ch);
239 INLINE_HEADER Lisp_Object
240 CHAR_TABLE_VALUE_UNSAFE (Lisp_Char_Table *ct, Emchar ch)
241 {
242   Lisp_Object val = get_byte_table (get_byte_table
243                                     (get_byte_table
244                                      (get_byte_table
245                                       (ct->table,
246                                        (unsigned char)(ch >> 24)),
247                                       (unsigned char) (ch >> 16)),
248                                      (unsigned char)  (ch >> 8)),
249                                     (unsigned char)    ch);
250   if (UNBOUNDP (val))
251     return ct->default_value;
252   else
253     return val;
254 }
255
256 #elif defined(MULE)
257
258 Lisp_Object get_non_ascii_char_table_value (Lisp_Char_Table *ct,
259                                             Charset_ID leading_byte,
260                                             Emchar c);
261
262 INLINE_HEADER Lisp_Object
263 CHAR_TABLE_NON_ASCII_VALUE_UNSAFE (Lisp_Char_Table *ct, Emchar ch);
264 INLINE_HEADER Lisp_Object
265 CHAR_TABLE_NON_ASCII_VALUE_UNSAFE (Lisp_Char_Table *ct, Emchar ch)
266 {
267 #ifdef UTF2000
268   Charset_ID lb = CHAR_CHARSET_ID (ch);
269 #else
270   Charset_ID lb = CHAR_LEADING_BYTE (ch);
271 #endif
272   if (!CHAR_TABLE_ENTRYP ((ct)->level1[lb - MIN_LEADING_BYTE]))
273     return (ct)->level1[lb - MIN_LEADING_BYTE];
274   else
275     return get_non_ascii_char_table_value (ct, lb, ch);
276 }
277
278 #define CHAR_TABLE_VALUE_UNSAFE(ct, ch)         \
279   ((ch) < NUM_ASCII_CHARS                       \
280    ? (ct)->ascii[ch]                            \
281    : CHAR_TABLE_NON_ASCII_VALUE_UNSAFE (ct, ch))
282
283 #else /* not MULE */
284
285 #define CHAR_TABLE_VALUE_UNSAFE(ct, ch) ((ct)->ascii[(unsigned char) (ch)])
286
287 #endif /* not MULE */
288
289 #define XCHAR_TABLE_VALUE_UNSAFE(ct, ch) \
290   CHAR_TABLE_VALUE_UNSAFE (XCHAR_TABLE (ct), ch)
291
292 enum chartab_range_type
293 {
294   CHARTAB_RANGE_ALL,
295 #ifdef UTF2000
296   CHARTAB_RANGE_DEFAULT,
297 #endif
298 #ifdef MULE
299   CHARTAB_RANGE_CHARSET,
300   CHARTAB_RANGE_ROW,
301 #endif
302   CHARTAB_RANGE_CHAR
303 };
304
305 struct chartab_range
306 {
307   enum chartab_range_type type;
308   Emchar ch;
309   Lisp_Object charset;
310   int row;
311 };
312
313 void fill_char_table (Lisp_Char_Table *ct, Lisp_Object value);
314 void put_char_table (Lisp_Char_Table *ct, struct chartab_range *range,
315                      Lisp_Object val);
316 Lisp_Object get_char_table (Emchar, Lisp_Char_Table *);
317 int map_char_table (Lisp_Char_Table *ct,
318                     struct chartab_range *range,
319                     int (*fn) (struct chartab_range *range,
320                                Lisp_Object val, void *arg),
321                     void *arg);
322 void prune_syntax_tables (void);
323
324 EXFUN (Fcopy_char_table, 1);
325 EXFUN (Fmake_char_table, 1);
326 EXFUN (Fput_char_table, 3);
327 EXFUN (Fget_char_table, 2);
328
329 extern Lisp_Object Vall_syntax_tables;
330
331 \f
332 #ifdef UTF2000
333
334 INLINE_HEADER void
335 put_char_id_table_0 (Lisp_Char_Table* cit, Emchar code, Lisp_Object value);
336 INLINE_HEADER void
337 put_char_id_table_0 (Lisp_Char_Table* cit, Emchar code, Lisp_Object value)
338 {
339   Lisp_Object table1, table2, table3, table4;
340         
341   table1 = cit->table;
342   table2 = get_byte_table (table1, (unsigned char)(code >> 24));
343   table3 = get_byte_table (table2, (unsigned char)(code >> 16));
344   table4 = get_byte_table (table3, (unsigned char)(code >>  8));
345
346   table4     = put_byte_table (table4, (unsigned char) code, value);
347   table3     = put_byte_table (table3, (unsigned char)(code >>  8), table4);
348   table2     = put_byte_table (table2, (unsigned char)(code >> 16), table3);
349   cit->table = put_byte_table (table1, (unsigned char)(code >> 24), table2);
350 }
351
352 #ifdef HAVE_CHISE_CLIENT
353 extern Lisp_Object Qsystem_char_id;
354
355 Lisp_Object
356 char_attribute_system_db_file (Lisp_Object key_type, Lisp_Object attribute,
357                                int writing_mode);
358
359 Lisp_Object load_char_attribute_maybe (Lisp_Char_Table* cit, Emchar ch);
360 #endif
361
362 INLINE_HEADER Lisp_Object
363 get_char_id_table_0 (Lisp_Char_Table* cit, Emchar ch);
364 INLINE_HEADER Lisp_Object
365 get_char_id_table_0 (Lisp_Char_Table* cit, Emchar ch)
366 {
367   return get_byte_table (get_byte_table
368                          (get_byte_table
369                           (get_byte_table
370                            (cit->table,
371                             (unsigned char)(ch >> 24)),
372                            (unsigned char) (ch >> 16)),
373                           (unsigned char)  (ch >> 8)),
374                          (unsigned char)    ch);
375 }
376
377 INLINE_HEADER Lisp_Object
378 get_char_id_table (Lisp_Char_Table* cit, Emchar ch);
379 INLINE_HEADER Lisp_Object
380 get_char_id_table (Lisp_Char_Table* cit, Emchar ch)
381 {
382   Lisp_Object val = get_char_id_table_0 (cit, ch);
383
384 #ifdef HAVE_CHISE_CLIENT
385   if (EQ (val, Qunloaded))
386     {
387       val = load_char_attribute_maybe (cit, ch);
388       put_char_id_table_0 (cit, ch, val);
389     }
390 #endif
391   if (UNBOUNDP (val))
392     return cit->default_value;
393   else
394     return val;
395 }
396
397 void
398 decode_char_table_range (Lisp_Object range, struct chartab_range *outrange);
399
400 INLINE_HEADER void
401 put_char_id_table (Lisp_Char_Table* table,
402                    Lisp_Object character, Lisp_Object value);
403 INLINE_HEADER void
404 put_char_id_table (Lisp_Char_Table* table,
405                    Lisp_Object character, Lisp_Object value)
406 {
407   struct chartab_range range;
408
409   decode_char_table_range (character, &range);
410   put_char_table (table, &range, value);
411 }
412
413
414 EXFUN (Fget_char_attribute, 3);
415
416 #endif
417 \f
418
419 #ifdef MULE
420 int check_category_char(Emchar ch, Lisp_Object ctbl,
421                         unsigned int designator, unsigned int not_p);
422
423 extern Lisp_Object Vstandard_category_table;
424
425 #define CATEGORY_DESIGNATORP(x) \
426  (CHARP (x) && XCHAR (x) >= 32 && XCHAR (x) <= 126)
427
428 #define CHECK_CATEGORY_DESIGNATOR(x) do {                       \
429   if (!CATEGORY_DESIGNATORP (x))                                \
430     dead_wrong_type_argument (Qcategory_designator_p, x);       \
431 } while (0)
432
433 #define CONCHECK_CATEGORY_DESIGNATOR(x) do {                    \
434   if (!CATEGORY_DESIGNATORP (x))                                \
435     x = wrong_type_argument (Qcategory_designator_p, x);        \
436 } while (0)
437
438 #define CATEGORY_TABLE_VALUEP(x) \
439  (NILP (x) || (BIT_VECTORP (x) && (bit_vector_length (XBIT_VECTOR (x)) == 95)))
440
441 #define CHECK_CATEGORY_TABLE_VALUE(x) do {                      \
442   if (!CATEGORY_TABLE_VALUEP (x))                               \
443     dead_wrong_type_argument (Qcategory_table_value_p, x);      \
444 } while (0)
445
446 #define CONCHECK_CATEGORY_TABLE_VALUE(x) do {                   \
447   if (!CATEGORY_TABLE_VALUEP (x))                               \
448     x = wrong_type_argument (Qcategory_table_value_p, x);       \
449 } while (0)
450
451 #endif /* MULE */
452
453 #endif /* INCLUDED_chartab_h_ */