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