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