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