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