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