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