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