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