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