XEmacs 21.2.28 "Hermes".
[chise/xemacs-chise.git.1] / src / chartab.c
1 /* XEmacs routines to deal with char tables.
2    Copyright (C) 1992, 1995 Free Software Foundation, Inc.
3    Copyright (C) 1995 Sun Microsystems, Inc.
4    Copyright (C) 1995, 1996 Ben Wing.
5    Copyright (C) 1995, 1997, 1999 Electrotechnical Laboratory, JAPAN.
6    Licensed to the Free Software Foundation.
7
8 This file is part of XEmacs.
9
10 XEmacs is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published by the
12 Free Software Foundation; either version 2, or (at your option) any
13 later version.
14
15 XEmacs is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with XEmacs; see the file COPYING.  If not, write to
22 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA.  */
24
25 /* Synched up with: Mule 2.3.  Not synched with FSF.
26
27    This file was written independently of the FSF implementation,
28    and is not compatible. */
29
30 /* Authorship:
31
32    Ben Wing: wrote, for 19.13 (Mule).  Some category table stuff
33              loosely based on the original Mule.
34    Jareth Hein: fixed a couple of bugs in the implementation, and
35              added regex support for categories with check_category_at
36  */
37
38 #include <config.h>
39 #include "lisp.h"
40
41 #include "buffer.h"
42 #include "chartab.h"
43 #include "syntax.h"
44
45 Lisp_Object Qchar_tablep, Qchar_table;
46
47 Lisp_Object Vall_syntax_tables;
48
49 #ifdef MULE
50 Lisp_Object Qcategory_table_p;
51 Lisp_Object Qcategory_designator_p;
52 Lisp_Object Qcategory_table_value_p;
53
54 Lisp_Object Vstandard_category_table;
55
56 /* Variables to determine word boundary.  */
57 Lisp_Object Vword_combining_categories, Vword_separating_categories;
58 #endif /* MULE */
59
60 \f
61 /* A char table maps from ranges of characters to values.
62
63    Implementing a general data structure that maps from arbitrary
64    ranges of numbers to values is tricky to do efficiently.  As it
65    happens, it should suffice (and is usually more convenient, anyway)
66    when dealing with characters to restrict the sorts of ranges that
67    can be assigned values, as follows:
68
69    1) All characters.
70    2) All characters in a charset.
71    3) All characters in a particular row of a charset, where a "row"
72       means all characters with the same first byte.
73    4) A particular character in a charset.
74
75    We use char tables to generalize the 256-element vectors now
76    littering the Emacs code.
77
78    Possible uses (all should be converted at some point):
79
80    1) category tables
81    2) syntax tables
82    3) display tables
83    4) case tables
84    5) keyboard-translate-table?
85
86    We provide an
87    abstract type to generalize the Emacs vectors and Mule
88    vectors-of-vectors goo.
89    */
90
91 /************************************************************************/
92 /*                         Char Table object                            */
93 /************************************************************************/
94
95 #ifdef MULE
96
97 static Lisp_Object
98 mark_char_table_entry (Lisp_Object obj)
99 {
100   Lisp_Char_Table_Entry *cte = XCHAR_TABLE_ENTRY (obj);
101   int i;
102
103   for (i = 0; i < 96; i++)
104     {
105       mark_object (cte->level2[i]);
106     }
107   return Qnil;
108 }
109
110 static int
111 char_table_entry_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
112 {
113   Lisp_Char_Table_Entry *cte1 = XCHAR_TABLE_ENTRY (obj1);
114   Lisp_Char_Table_Entry *cte2 = XCHAR_TABLE_ENTRY (obj2);
115   int i;
116
117   for (i = 0; i < 96; i++)
118     if (!internal_equal (cte1->level2[i], cte2->level2[i], depth + 1))
119       return 0;
120
121   return 1;
122 }
123
124 static unsigned long
125 char_table_entry_hash (Lisp_Object obj, int depth)
126 {
127   Lisp_Char_Table_Entry *cte = XCHAR_TABLE_ENTRY (obj);
128
129   return internal_array_hash (cte->level2, 96, depth);
130 }
131
132 static const struct lrecord_description char_table_entry_description[] = {
133   { XD_LISP_OBJECT_ARRAY, offsetof (Lisp_Char_Table_Entry, level2), 96 },
134   { XD_END }
135 };
136
137 DEFINE_LRECORD_IMPLEMENTATION ("char-table-entry", char_table_entry,
138                                mark_char_table_entry, internal_object_printer,
139                                0, char_table_entry_equal,
140                                char_table_entry_hash,
141                                char_table_entry_description,
142                                Lisp_Char_Table_Entry);
143 #endif /* MULE */
144
145 static Lisp_Object
146 mark_char_table (Lisp_Object obj)
147 {
148   Lisp_Char_Table *ct = XCHAR_TABLE (obj);
149   int i;
150
151   for (i = 0; i < NUM_ASCII_CHARS; i++)
152     mark_object (ct->ascii[i]);
153 #ifdef MULE
154   for (i = 0; i < NUM_LEADING_BYTES; i++)
155     mark_object (ct->level1[i]);
156 #endif
157   return ct->mirror_table;
158 }
159
160 /* WARNING: All functions of this nature need to be written extremely
161    carefully to avoid crashes during GC.  Cf. prune_specifiers()
162    and prune_weak_hash_tables(). */
163
164 void
165 prune_syntax_tables (void)
166 {
167   Lisp_Object rest, prev = Qnil;
168
169   for (rest = Vall_syntax_tables;
170        !NILP (rest);
171        rest = XCHAR_TABLE (rest)->next_table)
172     {
173       if (! marked_p (rest))
174         {
175           /* This table is garbage.  Remove it from the list. */
176           if (NILP (prev))
177             Vall_syntax_tables = XCHAR_TABLE (rest)->next_table;
178           else
179             XCHAR_TABLE (prev)->next_table =
180               XCHAR_TABLE (rest)->next_table;
181         }
182     }
183 }
184
185 static Lisp_Object
186 char_table_type_to_symbol (enum char_table_type type)
187 {
188   switch (type)
189   {
190   default: abort();
191   case CHAR_TABLE_TYPE_GENERIC:  return Qgeneric;
192   case CHAR_TABLE_TYPE_SYNTAX:   return Qsyntax;
193   case CHAR_TABLE_TYPE_DISPLAY:  return Qdisplay;
194   case CHAR_TABLE_TYPE_CHAR:     return Qchar;
195 #ifdef MULE
196   case CHAR_TABLE_TYPE_CATEGORY: return Qcategory;
197 #endif
198   }
199 }
200
201 static enum char_table_type
202 symbol_to_char_table_type (Lisp_Object symbol)
203 {
204   CHECK_SYMBOL (symbol);
205
206   if (EQ (symbol, Qgeneric))  return CHAR_TABLE_TYPE_GENERIC;
207   if (EQ (symbol, Qsyntax))   return CHAR_TABLE_TYPE_SYNTAX;
208   if (EQ (symbol, Qdisplay))  return CHAR_TABLE_TYPE_DISPLAY;
209   if (EQ (symbol, Qchar))     return CHAR_TABLE_TYPE_CHAR;
210 #ifdef MULE
211   if (EQ (symbol, Qcategory)) return CHAR_TABLE_TYPE_CATEGORY;
212 #endif
213
214   signal_simple_error ("Unrecognized char table type", symbol);
215   return CHAR_TABLE_TYPE_GENERIC; /* not reached */
216 }
217
218 static void
219 print_chartab_range (Emchar first, Emchar last, Lisp_Object val,
220                      Lisp_Object printcharfun)
221 {
222   if (first != last)
223     {
224       write_c_string (" (", printcharfun);
225       print_internal (make_char (first), printcharfun, 0);
226       write_c_string (" ", printcharfun);
227       print_internal (make_char (last), printcharfun, 0);
228       write_c_string (") ", printcharfun);
229     }
230   else
231     {
232       write_c_string (" ", printcharfun);
233       print_internal (make_char (first), printcharfun, 0);
234       write_c_string (" ", printcharfun);
235     }
236   print_internal (val, printcharfun, 1);
237 }
238
239 #ifdef MULE
240
241 static void
242 print_chartab_charset_row (Lisp_Object charset,
243                            int row,
244                            Lisp_Char_Table_Entry *cte,
245                            Lisp_Object printcharfun)
246 {
247   int i;
248   Lisp_Object cat = Qunbound;
249   int first = -1;
250
251   for (i = 32; i < 128; i++)
252     {
253       Lisp_Object pam = cte->level2[i - 32];
254
255       if (first == -1)
256         {
257           first = i;
258           cat = pam;
259           continue;
260         }
261
262       if (!EQ (cat, pam))
263         {
264           if (row == -1)
265             print_chartab_range (MAKE_CHAR (charset, first, 0),
266                                  MAKE_CHAR (charset, i - 1, 0),
267                                  cat, printcharfun);
268           else
269             print_chartab_range (MAKE_CHAR (charset, row, first),
270                                  MAKE_CHAR (charset, row, i - 1),
271                                  cat, printcharfun);
272           first = -1;
273           i--;
274         }
275     }
276
277   if (first != -1)
278     {
279       if (row == -1)
280         print_chartab_range (MAKE_CHAR (charset, first, 0),
281                              MAKE_CHAR (charset, i - 1, 0),
282                              cat, printcharfun);
283       else
284         print_chartab_range (MAKE_CHAR (charset, row, first),
285                              MAKE_CHAR (charset, row, i - 1),
286                              cat, printcharfun);
287     }
288 }
289
290 static void
291 print_chartab_two_byte_charset (Lisp_Object charset,
292                                 Lisp_Char_Table_Entry *cte,
293                                 Lisp_Object printcharfun)
294 {
295   int i;
296
297   for (i = 32; i < 128; i++)
298     {
299       Lisp_Object jen = cte->level2[i - 32];
300
301       if (!CHAR_TABLE_ENTRYP (jen))
302         {
303           char buf[100];
304
305           write_c_string (" [", printcharfun);
306           print_internal (XCHARSET_NAME (charset), printcharfun, 0);
307           sprintf (buf, " %d] ", i);
308           write_c_string (buf, printcharfun);
309           print_internal (jen, printcharfun, 0);
310         }
311       else
312         print_chartab_charset_row (charset, i, XCHAR_TABLE_ENTRY (jen),
313                                    printcharfun);
314     }
315 }
316
317 #endif /* MULE */
318
319 static void
320 print_char_table (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
321 {
322   Lisp_Char_Table *ct = XCHAR_TABLE (obj);
323   char buf[200];
324
325   sprintf (buf, "#s(char-table type %s data (",
326            string_data (symbol_name (XSYMBOL
327                                      (char_table_type_to_symbol (ct->type)))));
328   write_c_string (buf, printcharfun);
329
330   /* Now write out the ASCII/Control-1 stuff. */
331   {
332     int i;
333     int first = -1;
334     Lisp_Object val = Qunbound;
335
336     for (i = 0; i < NUM_ASCII_CHARS; i++)
337       {
338         if (first == -1)
339           {
340             first = i;
341             val = ct->ascii[i];
342             continue;
343           }
344
345         if (!EQ (ct->ascii[i], val))
346           {
347             print_chartab_range (first, i - 1, val, printcharfun);
348             first = -1;
349             i--;
350           }
351       }
352
353     if (first != -1)
354       print_chartab_range (first, i - 1, val, printcharfun);
355   }
356
357 #ifdef MULE
358   {
359     int i;
360
361     for (i = MIN_LEADING_BYTE; i < MIN_LEADING_BYTE + NUM_LEADING_BYTES;
362          i++)
363       {
364         Lisp_Object ann = ct->level1[i - MIN_LEADING_BYTE];
365         Lisp_Object charset = CHARSET_BY_LEADING_BYTE (i);
366
367         if (!CHARSETP (charset) || i == LEADING_BYTE_ASCII
368             || i == LEADING_BYTE_CONTROL_1)
369           continue;
370         if (!CHAR_TABLE_ENTRYP (ann))
371           {
372             write_c_string (" ", printcharfun);
373             print_internal (XCHARSET_NAME (charset),
374                             printcharfun, 0);
375             write_c_string (" ", printcharfun);
376             print_internal (ann, printcharfun, 0);
377           }
378         else
379           {
380             Lisp_Char_Table_Entry *cte = XCHAR_TABLE_ENTRY (ann);
381             if (XCHARSET_DIMENSION (charset) == 1)
382               print_chartab_charset_row (charset, -1, cte, printcharfun);
383             else
384               print_chartab_two_byte_charset (charset, cte, printcharfun);
385           }
386       }
387   }
388 #endif /* MULE */
389
390   write_c_string ("))", printcharfun);
391 }
392
393 static int
394 char_table_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
395 {
396   Lisp_Char_Table *ct1 = XCHAR_TABLE (obj1);
397   Lisp_Char_Table *ct2 = XCHAR_TABLE (obj2);
398   int i;
399
400   if (CHAR_TABLE_TYPE (ct1) != CHAR_TABLE_TYPE (ct2))
401     return 0;
402
403   for (i = 0; i < NUM_ASCII_CHARS; i++)
404     if (!internal_equal (ct1->ascii[i], ct2->ascii[i], depth + 1))
405       return 0;
406
407 #ifdef MULE
408   for (i = 0; i < NUM_LEADING_BYTES; i++)
409     if (!internal_equal (ct1->level1[i], ct2->level1[i], depth + 1))
410       return 0;
411 #endif /* MULE */
412
413   return 1;
414 }
415
416 static unsigned long
417 char_table_hash (Lisp_Object obj, int depth)
418 {
419   Lisp_Char_Table *ct = XCHAR_TABLE (obj);
420   unsigned long hashval = internal_array_hash (ct->ascii, NUM_ASCII_CHARS,
421                                                depth);
422 #ifdef MULE
423   hashval = HASH2 (hashval,
424                    internal_array_hash (ct->level1, NUM_LEADING_BYTES, depth));
425 #endif /* MULE */
426   return hashval;
427 }
428
429 static const struct lrecord_description char_table_description[] = {
430   { XD_LISP_OBJECT_ARRAY, offsetof (Lisp_Char_Table, ascii), NUM_ASCII_CHARS },
431 #ifdef MULE
432   { XD_LISP_OBJECT_ARRAY, offsetof (Lisp_Char_Table, level1), NUM_LEADING_BYTES },
433 #endif
434   { XD_LISP_OBJECT, offsetof (Lisp_Char_Table, mirror_table) },
435   { XD_LO_LINK,     offsetof (Lisp_Char_Table, next_table) },
436   { XD_END }
437 };
438
439 DEFINE_LRECORD_IMPLEMENTATION ("char-table", char_table,
440                                mark_char_table, print_char_table, 0,
441                                char_table_equal, char_table_hash,
442                                char_table_description,
443                                Lisp_Char_Table);
444
445 DEFUN ("char-table-p", Fchar_table_p, 1, 1, 0, /*
446 Return non-nil if OBJECT is a char table.
447
448 A char table is a table that maps characters (or ranges of characters)
449 to values.  Char tables are specialized for characters, only allowing
450 particular sorts of ranges to be assigned values.  Although this
451 loses in generality, it makes for extremely fast (constant-time)
452 lookups, and thus is feasible for applications that do an extremely
453 large number of lookups (e.g. scanning a buffer for a character in
454 a particular syntax, where a lookup in the syntax table must occur
455 once per character).
456
457 When Mule support exists, the types of ranges that can be assigned
458 values are
459
460 -- all characters
461 -- an entire charset
462 -- a single row in a two-octet charset
463 -- a single character
464
465 When Mule support is not present, the types of ranges that can be
466 assigned values are
467
468 -- all characters
469 -- a single character
470
471 To create a char table, use `make-char-table'.  To modify a char
472 table, use `put-char-table' or `remove-char-table'.  To retrieve the
473 value for a particular character, use `get-char-table'.  See also
474 `map-char-table', `clear-char-table', `copy-char-table',
475 `valid-char-table-type-p', `char-table-type-list', `valid-char-table-value-p',
476 and `check-char-table-value'.
477 */
478        (object))
479 {
480   return CHAR_TABLEP (object) ? Qt : Qnil;
481 }
482
483 DEFUN ("char-table-type-list", Fchar_table_type_list, 0, 0, 0, /*
484 Return a list of the recognized char table types.
485 See `valid-char-table-type-p'.
486 */
487        ())
488 {
489 #ifdef MULE
490   return list5 (Qchar, Qcategory, Qdisplay, Qgeneric, Qsyntax);
491 #else
492   return list4 (Qchar, Qdisplay, Qgeneric, Qsyntax);
493 #endif
494 }
495
496 DEFUN ("valid-char-table-type-p", Fvalid_char_table_type_p, 1, 1, 0, /*
497 Return t if TYPE if a recognized char table type.
498
499 Each char table type is used for a different purpose and allows different
500 sorts of values.  The different char table types are
501
502 `category'
503         Used for category tables, which specify the regexp categories
504         that a character is in.  The valid values are nil or a
505         bit vector of 95 elements.  Higher-level Lisp functions are
506         provided for working with category tables.  Currently categories
507         and category tables only exist when Mule support is present.
508 `char'
509         A generalized char table, for mapping from one character to
510         another.  Used for case tables, syntax matching tables,
511         `keyboard-translate-table', etc.  The valid values are characters.
512 `generic'
513         An even more generalized char table, for mapping from a
514         character to anything.
515 `display'
516         Used for display tables, which specify how a particular character
517         is to appear when displayed.  #### Not yet implemented.
518 `syntax'
519         Used for syntax tables, which specify the syntax of a particular
520         character.  Higher-level Lisp functions are provided for
521         working with syntax tables.  The valid values are integers.
522
523 */
524        (type))
525 {
526   return (EQ (type, Qchar)     ||
527 #ifdef MULE
528           EQ (type, Qcategory) ||
529 #endif
530           EQ (type, Qdisplay)  ||
531           EQ (type, Qgeneric)  ||
532           EQ (type, Qsyntax)) ? Qt : Qnil;
533 }
534
535 DEFUN ("char-table-type", Fchar_table_type, 1, 1, 0, /*
536 Return the type of char table TABLE.
537 See `valid-char-table-type-p'.
538 */
539        (table))
540 {
541   CHECK_CHAR_TABLE (table);
542   return char_table_type_to_symbol (XCHAR_TABLE (table)->type);
543 }
544
545 void
546 fill_char_table (Lisp_Char_Table *ct, Lisp_Object value)
547 {
548   int i;
549
550   for (i = 0; i < NUM_ASCII_CHARS; i++)
551     ct->ascii[i] = value;
552 #ifdef MULE
553   for (i = 0; i < NUM_LEADING_BYTES; i++)
554     ct->level1[i] = value;
555 #endif /* MULE */
556
557   if (ct->type == CHAR_TABLE_TYPE_SYNTAX)
558     update_syntax_table (ct);
559 }
560
561 DEFUN ("reset-char-table", Freset_char_table, 1, 1, 0, /*
562 Reset a char table to its default state.
563 */
564        (table))
565 {
566   Lisp_Char_Table *ct;
567
568   CHECK_CHAR_TABLE (table);
569   ct = XCHAR_TABLE (table);
570
571   switch (ct->type)
572     {
573     case CHAR_TABLE_TYPE_CHAR:
574       fill_char_table (ct, make_char (0));
575       break;
576     case CHAR_TABLE_TYPE_DISPLAY:
577     case CHAR_TABLE_TYPE_GENERIC:
578 #ifdef MULE
579     case CHAR_TABLE_TYPE_CATEGORY:
580 #endif /* MULE */
581       fill_char_table (ct, Qnil);
582       break;
583
584     case CHAR_TABLE_TYPE_SYNTAX:
585       fill_char_table (ct, make_int (Sinherit));
586       break;
587
588     default:
589       abort ();
590     }
591
592   return Qnil;
593 }
594
595 DEFUN ("make-char-table", Fmake_char_table, 1, 1, 0, /*
596 Return a new, empty char table of type TYPE.
597 Currently recognized types are 'char, 'category, 'display, 'generic,
598 and 'syntax.  See `valid-char-table-type-p'.
599 */
600        (type))
601 {
602   Lisp_Char_Table *ct;
603   Lisp_Object obj;
604   enum char_table_type ty = symbol_to_char_table_type (type);
605
606   ct = alloc_lcrecord_type (Lisp_Char_Table, &lrecord_char_table);
607   ct->type = ty;
608   if (ty == CHAR_TABLE_TYPE_SYNTAX)
609     {
610       ct->mirror_table = Fmake_char_table (Qgeneric);
611       fill_char_table (XCHAR_TABLE (ct->mirror_table),
612                        make_int (Spunct));
613     }
614   else
615     ct->mirror_table = Qnil;
616   ct->next_table = Qnil;
617   XSETCHAR_TABLE (obj, ct);
618   if (ty == CHAR_TABLE_TYPE_SYNTAX)
619     {
620       ct->next_table = Vall_syntax_tables;
621       Vall_syntax_tables = obj;
622     }
623   Freset_char_table (obj);
624   return obj;
625 }
626
627 #ifdef MULE
628
629 static Lisp_Object
630 make_char_table_entry (Lisp_Object initval)
631 {
632   Lisp_Object obj;
633   int i;
634   Lisp_Char_Table_Entry *cte =
635     alloc_lcrecord_type (Lisp_Char_Table_Entry, &lrecord_char_table_entry);
636
637   for (i = 0; i < 96; i++)
638     cte->level2[i] = initval;
639
640   XSETCHAR_TABLE_ENTRY (obj, cte);
641   return obj;
642 }
643
644 static Lisp_Object
645 copy_char_table_entry (Lisp_Object entry)
646 {
647   Lisp_Char_Table_Entry *cte = XCHAR_TABLE_ENTRY (entry);
648   Lisp_Object obj;
649   int i;
650   Lisp_Char_Table_Entry *ctenew =
651     alloc_lcrecord_type (Lisp_Char_Table_Entry, &lrecord_char_table_entry);
652
653   for (i = 0; i < 96; i++)
654     {
655       Lisp_Object new = cte->level2[i];
656       if (CHAR_TABLE_ENTRYP (new))
657         ctenew->level2[i] = copy_char_table_entry (new);
658       else
659         ctenew->level2[i] = new;
660     }
661
662   XSETCHAR_TABLE_ENTRY (obj, ctenew);
663   return obj;
664 }
665
666 #endif /* MULE */
667
668 DEFUN ("copy-char-table", Fcopy_char_table, 1, 1, 0, /*
669 Make a new char table which is a copy of OLD-TABLE.
670 It will contain the same values for the same characters and ranges
671 as OLD-TABLE.  The values will not themselves be copied.
672 */
673        (old_table))
674 {
675   Lisp_Char_Table *ct, *ctnew;
676   Lisp_Object obj;
677   int i;
678
679   CHECK_CHAR_TABLE (old_table);
680   ct = XCHAR_TABLE (old_table);
681   ctnew = alloc_lcrecord_type (Lisp_Char_Table, &lrecord_char_table);
682   ctnew->type = ct->type;
683
684   for (i = 0; i < NUM_ASCII_CHARS; i++)
685     {
686       Lisp_Object new = ct->ascii[i];
687 #ifdef MULE
688       assert (! (CHAR_TABLE_ENTRYP (new)));
689 #endif /* MULE */
690       ctnew->ascii[i] = new;
691     }
692
693 #ifdef MULE
694
695   for (i = 0; i < NUM_LEADING_BYTES; i++)
696     {
697       Lisp_Object new = ct->level1[i];
698       if (CHAR_TABLE_ENTRYP (new))
699         ctnew->level1[i] = copy_char_table_entry (new);
700       else
701         ctnew->level1[i] = new;
702     }
703
704 #endif /* MULE */
705
706   if (CHAR_TABLEP (ct->mirror_table))
707     ctnew->mirror_table = Fcopy_char_table (ct->mirror_table);
708   else
709     ctnew->mirror_table = ct->mirror_table;
710   ctnew->next_table = Qnil;
711   XSETCHAR_TABLE (obj, ctnew);
712   if (ctnew->type == CHAR_TABLE_TYPE_SYNTAX)
713     {
714       ctnew->next_table = Vall_syntax_tables;
715       Vall_syntax_tables = obj;
716     }
717   return obj;
718 }
719
720 static void
721 decode_char_table_range (Lisp_Object range, struct chartab_range *outrange)
722 {
723   if (EQ (range, Qt))
724     outrange->type = CHARTAB_RANGE_ALL;
725   else if (CHAR_OR_CHAR_INTP (range))
726     {
727       outrange->type = CHARTAB_RANGE_CHAR;
728       outrange->ch = XCHAR_OR_CHAR_INT (range);
729     }
730 #ifndef MULE
731   else
732     signal_simple_error ("Range must be t or a character", range);
733 #else /* MULE */
734   else if (VECTORP (range))
735     {
736       Lisp_Vector *vec = XVECTOR (range);
737       Lisp_Object *elts = vector_data (vec);
738       if (vector_length (vec) != 2)
739         signal_simple_error ("Length of charset row vector must be 2",
740                              range);
741       outrange->type = CHARTAB_RANGE_ROW;
742       outrange->charset = Fget_charset (elts[0]);
743       CHECK_INT (elts[1]);
744       outrange->row = XINT (elts[1]);
745       switch (XCHARSET_TYPE (outrange->charset))
746         {
747         case CHARSET_TYPE_94:
748         case CHARSET_TYPE_96:
749           signal_simple_error ("Charset in row vector must be multi-byte",
750                                outrange->charset);
751         case CHARSET_TYPE_94X94:
752           check_int_range (outrange->row, 33, 126);
753           break;
754         case CHARSET_TYPE_96X96:
755           check_int_range (outrange->row, 32, 127);
756           break;
757         default:
758           abort ();
759         }
760     }
761   else
762     {
763       if (!CHARSETP (range) && !SYMBOLP (range))
764         signal_simple_error
765           ("Char table range must be t, charset, char, or vector", range);
766       outrange->type = CHARTAB_RANGE_CHARSET;
767       outrange->charset = Fget_charset (range);
768     }
769 #endif /* MULE */
770 }
771
772 #ifdef MULE
773
774 /* called from CHAR_TABLE_VALUE(). */
775 Lisp_Object
776 get_non_ascii_char_table_value (Lisp_Char_Table *ct, int leading_byte,
777                                Emchar c)
778 {
779   Lisp_Object val;
780   Lisp_Object charset = CHARSET_BY_LEADING_BYTE (leading_byte);
781   int byte1, byte2;
782
783   BREAKUP_CHAR_1_UNSAFE (c, charset, byte1, byte2);
784   val = ct->level1[leading_byte - MIN_LEADING_BYTE];
785   if (CHAR_TABLE_ENTRYP (val))
786     {
787       Lisp_Char_Table_Entry *cte = XCHAR_TABLE_ENTRY (val);
788       val = cte->level2[byte1 - 32];
789       if (CHAR_TABLE_ENTRYP (val))
790         {
791           cte = XCHAR_TABLE_ENTRY (val);
792           assert (byte2 >= 32);
793           val = cte->level2[byte2 - 32];
794           assert (!CHAR_TABLE_ENTRYP (val));
795         }
796     }
797
798   return val;
799 }
800
801 #endif /* MULE */
802
803 Lisp_Object
804 get_char_table (Emchar ch, Lisp_Char_Table *ct)
805 {
806 #ifdef MULE
807   {
808     Lisp_Object charset;
809     int byte1, byte2;
810     Lisp_Object val;
811
812     BREAKUP_CHAR (ch, charset, byte1, byte2);
813
814     if (EQ (charset, Vcharset_ascii))
815       val = ct->ascii[byte1];
816     else if (EQ (charset, Vcharset_control_1))
817       val = ct->ascii[byte1 + 128];
818     else
819       {
820         int lb = XCHARSET_LEADING_BYTE (charset) - MIN_LEADING_BYTE;
821         val = ct->level1[lb];
822         if (CHAR_TABLE_ENTRYP (val))
823           {
824             Lisp_Char_Table_Entry *cte = XCHAR_TABLE_ENTRY (val);
825             val = cte->level2[byte1 - 32];
826             if (CHAR_TABLE_ENTRYP (val))
827               {
828                 cte = XCHAR_TABLE_ENTRY (val);
829                 assert (byte2 >= 32);
830                 val = cte->level2[byte2 - 32];
831                 assert (!CHAR_TABLE_ENTRYP (val));
832               }
833           }
834       }
835
836     return val;
837   }
838 #else /* not MULE */
839   return ct->ascii[(unsigned char)ch];
840 #endif /* not MULE */
841 }
842
843
844 DEFUN ("get-char-table", Fget_char_table, 2, 2, 0, /*
845 Find value for char CH in TABLE.
846 */
847        (ch, table))
848 {
849   Lisp_Char_Table *ct;
850
851   CHECK_CHAR_TABLE (table);
852   ct = XCHAR_TABLE (table);
853   CHECK_CHAR_COERCE_INT (ch);
854
855   return get_char_table (XCHAR (ch), ct);
856 }
857
858 DEFUN ("get-range-char-table", Fget_range_char_table, 2, 3, 0, /*
859 Find value for a range in TABLE.
860 If there is more than one value, return MULTI (defaults to nil).
861 */
862        (range, table, multi))
863 {
864   Lisp_Char_Table *ct;
865   struct chartab_range rainj;
866
867   if (CHAR_OR_CHAR_INTP (range))
868     return Fget_char_table (range, table);
869   CHECK_CHAR_TABLE (table);
870   ct = XCHAR_TABLE (table);
871
872   decode_char_table_range (range, &rainj);
873   switch (rainj.type)
874     {
875     case CHARTAB_RANGE_ALL:
876       {
877         int i;
878         Lisp_Object first = ct->ascii[0];
879
880         for (i = 1; i < NUM_ASCII_CHARS; i++)
881           if (!EQ (first, ct->ascii[i]))
882             return multi;
883
884 #ifdef MULE
885         for (i = MIN_LEADING_BYTE; i < MIN_LEADING_BYTE + NUM_LEADING_BYTES;
886              i++)
887           {
888             if (!CHARSETP (CHARSET_BY_LEADING_BYTE (i))
889                 || i == LEADING_BYTE_ASCII
890                 || i == LEADING_BYTE_CONTROL_1)
891               continue;
892             if (!EQ (first, ct->level1[i - MIN_LEADING_BYTE]))
893               return multi;
894           }
895 #endif /* MULE */
896
897         return first;
898       }
899
900 #ifdef MULE
901     case CHARTAB_RANGE_CHARSET:
902       if (EQ (rainj.charset, Vcharset_ascii))
903         {
904           int i;
905           Lisp_Object first = ct->ascii[0];
906
907           for (i = 1; i < 128; i++)
908             if (!EQ (first, ct->ascii[i]))
909               return multi;
910           return first;
911         }
912
913       if (EQ (rainj.charset, Vcharset_control_1))
914         {
915           int i;
916           Lisp_Object first = ct->ascii[128];
917
918           for (i = 129; i < 160; i++)
919             if (!EQ (first, ct->ascii[i]))
920               return multi;
921           return first;
922         }
923
924       {
925         Lisp_Object val = ct->level1[XCHARSET_LEADING_BYTE (rainj.charset) -
926                                      MIN_LEADING_BYTE];
927         if (CHAR_TABLE_ENTRYP (val))
928           return multi;
929         return val;
930       }
931
932     case CHARTAB_RANGE_ROW:
933       {
934         Lisp_Object val = ct->level1[XCHARSET_LEADING_BYTE (rainj.charset) -
935                                      MIN_LEADING_BYTE];
936         if (!CHAR_TABLE_ENTRYP (val))
937           return val;
938         val = XCHAR_TABLE_ENTRY (val)->level2[rainj.row - 32];
939         if (CHAR_TABLE_ENTRYP (val))
940           return multi;
941         return val;
942       }
943 #endif /* not MULE */
944
945     default:
946       abort ();
947     }
948
949   return Qnil; /* not reached */
950 }
951
952 static int
953 check_valid_char_table_value (Lisp_Object value, enum char_table_type type,
954                               Error_behavior errb)
955 {
956   switch (type)
957     {
958     case CHAR_TABLE_TYPE_SYNTAX:
959       if (!ERRB_EQ (errb, ERROR_ME))
960         return INTP (value) || (CONSP (value) && INTP (XCAR (value))
961                                 && CHAR_OR_CHAR_INTP (XCDR (value)));
962       if (CONSP (value))
963         {
964           Lisp_Object cdr = XCDR (value);
965           CHECK_INT (XCAR (value));
966           CHECK_CHAR_COERCE_INT (cdr);
967          }
968       else
969         CHECK_INT (value);
970       break;
971
972 #ifdef MULE
973     case CHAR_TABLE_TYPE_CATEGORY:
974       if (!ERRB_EQ (errb, ERROR_ME))
975         return CATEGORY_TABLE_VALUEP (value);
976       CHECK_CATEGORY_TABLE_VALUE (value);
977       break;
978 #endif /* MULE */
979
980     case CHAR_TABLE_TYPE_GENERIC:
981       return 1;
982
983     case CHAR_TABLE_TYPE_DISPLAY:
984       /* #### fix this */
985       maybe_signal_simple_error ("Display char tables not yet implemented",
986                                  value, Qchar_table, errb);
987       return 0;
988
989     case CHAR_TABLE_TYPE_CHAR:
990       if (!ERRB_EQ (errb, ERROR_ME))
991         return CHAR_OR_CHAR_INTP (value);
992       CHECK_CHAR_COERCE_INT (value);
993       break;
994
995     default:
996       abort ();
997     }
998
999   return 0; /* not reached */
1000 }
1001
1002 static Lisp_Object
1003 canonicalize_char_table_value (Lisp_Object value, enum char_table_type type)
1004 {
1005   switch (type)
1006     {
1007     case CHAR_TABLE_TYPE_SYNTAX:
1008       if (CONSP (value))
1009         {
1010           Lisp_Object car = XCAR (value);
1011           Lisp_Object cdr = XCDR (value);
1012           CHECK_CHAR_COERCE_INT (cdr);
1013           return Fcons (car, cdr);
1014         }
1015       break;
1016     case CHAR_TABLE_TYPE_CHAR:
1017       CHECK_CHAR_COERCE_INT (value);
1018       break;
1019     default:
1020       break;
1021     }
1022   return value;
1023 }
1024
1025 DEFUN ("valid-char-table-value-p", Fvalid_char_table_value_p, 2, 2, 0, /*
1026 Return non-nil if VALUE is a valid value for CHAR-TABLE-TYPE.
1027 */
1028        (value, char_table_type))
1029 {
1030   enum char_table_type type = symbol_to_char_table_type (char_table_type);
1031
1032   return check_valid_char_table_value (value, type, ERROR_ME_NOT) ? Qt : Qnil;
1033 }
1034
1035 DEFUN ("check-valid-char-table-value", Fcheck_valid_char_table_value, 2, 2, 0, /*
1036 Signal an error if VALUE is not a valid value for CHAR-TABLE-TYPE.
1037 */
1038        (value, char_table_type))
1039 {
1040   enum char_table_type type = symbol_to_char_table_type (char_table_type);
1041
1042   check_valid_char_table_value (value, type, ERROR_ME);
1043   return Qnil;
1044 }
1045
1046 /* Assign VAL to all characters in RANGE in char table CT. */
1047
1048 void
1049 put_char_table (Lisp_Char_Table *ct, struct chartab_range *range,
1050                 Lisp_Object val)
1051 {
1052   switch (range->type)
1053     {
1054     case CHARTAB_RANGE_ALL:
1055       fill_char_table (ct, val);
1056       return; /* avoid the duplicate call to update_syntax_table() below,
1057                  since fill_char_table() also did that. */
1058
1059 #ifdef MULE
1060     case CHARTAB_RANGE_CHARSET:
1061       if (EQ (range->charset, Vcharset_ascii))
1062         {
1063           int i;
1064           for (i = 0; i < 128; i++)
1065             ct->ascii[i] = val;
1066         }
1067       else if (EQ (range->charset, Vcharset_control_1))
1068         {
1069           int i;
1070           for (i = 128; i < 160; i++)
1071             ct->ascii[i] = val;
1072         }
1073       else
1074         {
1075           int lb = XCHARSET_LEADING_BYTE (range->charset) - MIN_LEADING_BYTE;
1076           ct->level1[lb] = val;
1077         }
1078       break;
1079
1080     case CHARTAB_RANGE_ROW:
1081       {
1082         Lisp_Char_Table_Entry *cte;
1083         int lb = XCHARSET_LEADING_BYTE (range->charset) - MIN_LEADING_BYTE;
1084         /* make sure that there is a separate entry for the row. */
1085         if (!CHAR_TABLE_ENTRYP (ct->level1[lb]))
1086           ct->level1[lb] = make_char_table_entry (ct->level1[lb]);
1087         cte = XCHAR_TABLE_ENTRY (ct->level1[lb]);
1088         cte->level2[range->row - 32] = val;
1089       }
1090       break;
1091 #endif /* MULE */
1092
1093     case CHARTAB_RANGE_CHAR:
1094 #ifdef MULE
1095       {
1096         Lisp_Object charset;
1097         int byte1, byte2;
1098
1099         BREAKUP_CHAR (range->ch, charset, byte1, byte2);
1100         if (EQ (charset, Vcharset_ascii))
1101           ct->ascii[byte1] = val;
1102         else if (EQ (charset, Vcharset_control_1))
1103           ct->ascii[byte1 + 128] = val;
1104         else
1105           {
1106             Lisp_Char_Table_Entry *cte;
1107             int lb = XCHARSET_LEADING_BYTE (charset) - MIN_LEADING_BYTE;
1108             /* make sure that there is a separate entry for the row. */
1109             if (!CHAR_TABLE_ENTRYP (ct->level1[lb]))
1110               ct->level1[lb] = make_char_table_entry (ct->level1[lb]);
1111             cte = XCHAR_TABLE_ENTRY (ct->level1[lb]);
1112             /* now CTE is a char table entry for the charset;
1113                each entry is for a single row (or character of
1114                a one-octet charset). */
1115             if (XCHARSET_DIMENSION (charset) == 1)
1116               cte->level2[byte1 - 32] = val;
1117             else
1118               {
1119                 /* assigning to one character in a two-octet charset. */
1120                 /* make sure that the charset row contains a separate
1121                    entry for each character. */
1122                 if (!CHAR_TABLE_ENTRYP (cte->level2[byte1 - 32]))
1123                   cte->level2[byte1 - 32] =
1124                     make_char_table_entry (cte->level2[byte1 - 32]);
1125                 cte = XCHAR_TABLE_ENTRY (cte->level2[byte1 - 32]);
1126                 cte->level2[byte2 - 32] = val;
1127               }
1128           }
1129       }
1130 #else /* not MULE */
1131       ct->ascii[(unsigned char) (range->ch)] = val;
1132       break;
1133 #endif /* not MULE */
1134     }
1135
1136   if (ct->type == CHAR_TABLE_TYPE_SYNTAX)
1137     update_syntax_table (ct);
1138 }
1139
1140 DEFUN ("put-char-table", Fput_char_table, 3, 3, 0, /*
1141 Set the value for chars in RANGE to be VAL in TABLE.
1142
1143 RANGE specifies one or more characters to be affected and should be
1144 one of the following:
1145
1146 -- t (all characters are affected)
1147 -- A charset (only allowed when Mule support is present)
1148 -- A vector of two elements: a two-octet charset and a row number
1149    (only allowed when Mule support is present)
1150 -- A single character
1151
1152 VAL must be a value appropriate for the type of TABLE.
1153 See `valid-char-table-type-p'.
1154 */
1155        (range, val, table))
1156 {
1157   Lisp_Char_Table *ct;
1158   struct chartab_range rainj;
1159
1160   CHECK_CHAR_TABLE (table);
1161   ct = XCHAR_TABLE (table);
1162   check_valid_char_table_value (val, ct->type, ERROR_ME);
1163   decode_char_table_range (range, &rainj);
1164   val = canonicalize_char_table_value (val, ct->type);
1165   put_char_table (ct, &rainj, val);
1166   return Qnil;
1167 }
1168
1169 /* Map FN over the ASCII chars in CT. */
1170
1171 static int
1172 map_over_charset_ascii (Lisp_Char_Table *ct,
1173                         int (*fn) (struct chartab_range *range,
1174                                    Lisp_Object val, void *arg),
1175                         void *arg)
1176 {
1177   struct chartab_range rainj;
1178   int i, retval;
1179   int start = 0;
1180 #ifdef MULE
1181   int stop = 128;
1182 #else
1183   int stop = 256;
1184 #endif
1185
1186   rainj.type = CHARTAB_RANGE_CHAR;
1187
1188   for (i = start, retval = 0; i < stop && retval == 0; i++)
1189     {
1190       rainj.ch = (Emchar) i;
1191       retval = (fn) (&rainj, ct->ascii[i], arg);
1192     }
1193
1194   return retval;
1195 }
1196
1197 #ifdef MULE
1198
1199 /* Map FN over the Control-1 chars in CT. */
1200
1201 static int
1202 map_over_charset_control_1 (Lisp_Char_Table *ct,
1203                             int (*fn) (struct chartab_range *range,
1204                                        Lisp_Object val, void *arg),
1205                             void *arg)
1206 {
1207   struct chartab_range rainj;
1208   int i, retval;
1209   int start = 128;
1210   int stop  = start + 32;
1211
1212   rainj.type = CHARTAB_RANGE_CHAR;
1213
1214   for (i = start, retval = 0; i < stop && retval == 0; i++)
1215     {
1216       rainj.ch = (Emchar) (i);
1217       retval = (fn) (&rainj, ct->ascii[i], arg);
1218     }
1219
1220   return retval;
1221 }
1222
1223 /* Map FN over the row ROW of two-byte charset CHARSET.
1224    There must be a separate value for that row in the char table.
1225    CTE specifies the char table entry for CHARSET. */
1226
1227 static int
1228 map_over_charset_row (Lisp_Char_Table_Entry *cte,
1229                       Lisp_Object charset, int row,
1230                       int (*fn) (struct chartab_range *range,
1231                                  Lisp_Object val, void *arg),
1232                       void *arg)
1233 {
1234   Lisp_Object val = cte->level2[row - 32];
1235
1236   if (!CHAR_TABLE_ENTRYP (val))
1237     {
1238       struct chartab_range rainj;
1239
1240       rainj.type = CHARTAB_RANGE_ROW;
1241       rainj.charset = charset;
1242       rainj.row = row;
1243       return (fn) (&rainj, val, arg);
1244     }
1245   else
1246     {
1247       struct chartab_range rainj;
1248       int i, retval;
1249       int charset94_p = (XCHARSET_CHARS (charset) == 94);
1250       int start = charset94_p ?  33 :  32;
1251       int stop  = charset94_p ? 127 : 128;
1252
1253       cte = XCHAR_TABLE_ENTRY (val);
1254
1255       rainj.type = CHARTAB_RANGE_CHAR;
1256
1257       for (i = start, retval = 0; i < stop && retval == 0; i++)
1258         {
1259           rainj.ch = MAKE_CHAR (charset, row, i);
1260           retval = (fn) (&rainj, cte->level2[i - 32], arg);
1261         }
1262       return retval;
1263     }
1264 }
1265
1266
1267 static int
1268 map_over_other_charset (Lisp_Char_Table *ct, int lb,
1269                         int (*fn) (struct chartab_range *range,
1270                                    Lisp_Object val, void *arg),
1271                         void *arg)
1272 {
1273   Lisp_Object val = ct->level1[lb - MIN_LEADING_BYTE];
1274   Lisp_Object charset = CHARSET_BY_LEADING_BYTE (lb);
1275
1276   if (!CHARSETP (charset)
1277       || lb == LEADING_BYTE_ASCII
1278       || lb == LEADING_BYTE_CONTROL_1)
1279     return 0;
1280
1281   if (!CHAR_TABLE_ENTRYP (val))
1282     {
1283       struct chartab_range rainj;
1284
1285       rainj.type = CHARTAB_RANGE_CHARSET;
1286       rainj.charset = charset;
1287       return (fn) (&rainj, val, arg);
1288     }
1289
1290   {
1291     Lisp_Char_Table_Entry *cte = XCHAR_TABLE_ENTRY (val);
1292     int charset94_p = (XCHARSET_CHARS (charset) == 94);
1293     int start = charset94_p ?  33 :  32;
1294     int stop  = charset94_p ? 127 : 128;
1295     int i, retval;
1296
1297     if (XCHARSET_DIMENSION (charset) == 1)
1298       {
1299         struct chartab_range rainj;
1300         rainj.type = CHARTAB_RANGE_CHAR;
1301
1302         for (i = start, retval = 0; i < stop && retval == 0; i++)
1303           {
1304             rainj.ch = MAKE_CHAR (charset, i, 0);
1305             retval = (fn) (&rainj, cte->level2[i - 32], arg);
1306           }
1307       }
1308     else
1309       {
1310         for (i = start, retval = 0; i < stop && retval == 0; i++)
1311           retval = map_over_charset_row (cte, charset, i, fn, arg);
1312       }
1313
1314     return retval;
1315   }
1316 }
1317
1318 #endif /* MULE */
1319
1320 /* Map FN (with client data ARG) over range RANGE in char table CT.
1321    Mapping stops the first time FN returns non-zero, and that value
1322    becomes the return value of map_char_table(). */
1323
1324 int
1325 map_char_table (Lisp_Char_Table *ct,
1326                 struct chartab_range *range,
1327                 int (*fn) (struct chartab_range *range,
1328                            Lisp_Object val, void *arg),
1329                 void *arg)
1330 {
1331   switch (range->type)
1332     {
1333     case CHARTAB_RANGE_ALL:
1334       {
1335         int retval;
1336
1337         retval = map_over_charset_ascii (ct, fn, arg);
1338         if (retval)
1339           return retval;
1340 #ifdef MULE
1341         retval = map_over_charset_control_1 (ct, fn, arg);
1342         if (retval)
1343           return retval;
1344         {
1345           int i;
1346           int start = MIN_LEADING_BYTE;
1347           int stop  = start + NUM_LEADING_BYTES;
1348
1349           for (i = start, retval = 0; i < stop && retval == 0; i++)
1350             {
1351               retval = map_over_other_charset (ct, i, fn, arg);
1352             }
1353         }
1354 #endif /* MULE */
1355         return retval;
1356       }
1357
1358 #ifdef MULE
1359     case CHARTAB_RANGE_CHARSET:
1360       return map_over_other_charset (ct,
1361                                      XCHARSET_LEADING_BYTE (range->charset),
1362                                      fn, arg);
1363
1364     case CHARTAB_RANGE_ROW:
1365       {
1366         Lisp_Object val = ct->level1[XCHARSET_LEADING_BYTE (range->charset) - MIN_LEADING_BYTE];
1367         if (!CHAR_TABLE_ENTRYP (val))
1368           {
1369             struct chartab_range rainj;
1370
1371             rainj.type = CHARTAB_RANGE_ROW;
1372             rainj.charset = range->charset;
1373             rainj.row = range->row;
1374             return (fn) (&rainj, val, arg);
1375           }
1376         else
1377           return map_over_charset_row (XCHAR_TABLE_ENTRY (val),
1378                                        range->charset, range->row,
1379                                        fn, arg);
1380       }
1381 #endif /* MULE */
1382
1383     case CHARTAB_RANGE_CHAR:
1384       {
1385         Emchar ch = range->ch;
1386         Lisp_Object val = CHAR_TABLE_VALUE_UNSAFE (ct, ch);
1387         struct chartab_range rainj;
1388
1389         rainj.type = CHARTAB_RANGE_CHAR;
1390         rainj.ch = ch;
1391         return (fn) (&rainj, val, arg);
1392       }
1393
1394     default:
1395       abort ();
1396     }
1397
1398   return 0;
1399 }
1400
1401 struct slow_map_char_table_arg
1402 {
1403   Lisp_Object function;
1404   Lisp_Object retval;
1405 };
1406
1407 static int
1408 slow_map_char_table_fun (struct chartab_range *range,
1409                          Lisp_Object val, void *arg)
1410 {
1411   Lisp_Object ranjarg = Qnil;
1412   struct slow_map_char_table_arg *closure =
1413     (struct slow_map_char_table_arg *) arg;
1414
1415   switch (range->type)
1416     {
1417     case CHARTAB_RANGE_ALL:
1418       ranjarg = Qt;
1419       break;
1420
1421 #ifdef MULE
1422     case CHARTAB_RANGE_CHARSET:
1423       ranjarg = XCHARSET_NAME (range->charset);
1424       break;
1425
1426     case CHARTAB_RANGE_ROW:
1427       ranjarg = vector2 (XCHARSET_NAME (range->charset),
1428                          make_int (range->row));
1429       break;
1430 #endif /* MULE */
1431     case CHARTAB_RANGE_CHAR:
1432       ranjarg = make_char (range->ch);
1433       break;
1434     default:
1435       abort ();
1436     }
1437
1438   closure->retval = call2 (closure->function, ranjarg, val);
1439   return !NILP (closure->retval);
1440 }
1441
1442 DEFUN ("map-char-table", Fmap_char_table, 2, 3, 0, /*
1443 Map FUNCTION over entries in TABLE, calling it with two args,
1444 each key and value in the table.
1445
1446 RANGE specifies a subrange to map over and is in the same format as
1447 the RANGE argument to `put-range-table'.  If omitted or t, it defaults to
1448 the entire table.
1449 */
1450        (function, table, range))
1451 {
1452   Lisp_Char_Table *ct;
1453   struct slow_map_char_table_arg slarg;
1454   struct gcpro gcpro1, gcpro2;
1455   struct chartab_range rainj;
1456
1457   CHECK_CHAR_TABLE (table);
1458   ct = XCHAR_TABLE (table);
1459   if (NILP (range))
1460     range = Qt;
1461   decode_char_table_range (range, &rainj);
1462   slarg.function = function;
1463   slarg.retval = Qnil;
1464   GCPRO2 (slarg.function, slarg.retval);
1465   map_char_table (ct, &rainj, slow_map_char_table_fun, &slarg);
1466   UNGCPRO;
1467
1468   return slarg.retval;
1469 }
1470
1471
1472 \f
1473 /************************************************************************/
1474 /*                         Char table read syntax                       */
1475 /************************************************************************/
1476
1477 static int
1478 chartab_type_validate (Lisp_Object keyword, Lisp_Object value,
1479                        Error_behavior errb)
1480 {
1481   /* #### should deal with ERRB */
1482   symbol_to_char_table_type (value);
1483   return 1;
1484 }
1485
1486 static int
1487 chartab_data_validate (Lisp_Object keyword, Lisp_Object value,
1488                        Error_behavior errb)
1489 {
1490   Lisp_Object rest;
1491
1492   /* #### should deal with ERRB */
1493   EXTERNAL_LIST_LOOP (rest, value)
1494     {
1495       Lisp_Object range = XCAR (rest);
1496       struct chartab_range dummy;
1497
1498       rest = XCDR (rest);
1499       if (!CONSP (rest))
1500         signal_simple_error ("Invalid list format", value);
1501       if (CONSP (range))
1502         {
1503           if (!CONSP (XCDR (range))
1504               || !NILP (XCDR (XCDR (range))))
1505             signal_simple_error ("Invalid range format", range);
1506           decode_char_table_range (XCAR (range), &dummy);
1507           decode_char_table_range (XCAR (XCDR (range)), &dummy);
1508         }
1509       else
1510         decode_char_table_range (range, &dummy);
1511     }
1512
1513   return 1;
1514 }
1515
1516 static Lisp_Object
1517 chartab_instantiate (Lisp_Object data)
1518 {
1519   Lisp_Object chartab;
1520   Lisp_Object type = Qgeneric;
1521   Lisp_Object dataval = Qnil;
1522
1523   while (!NILP (data))
1524     {
1525       Lisp_Object keyw = Fcar (data);
1526       Lisp_Object valw;
1527
1528       data = Fcdr (data);
1529       valw = Fcar (data);
1530       data = Fcdr (data);
1531       if (EQ (keyw, Qtype))
1532         type = valw;
1533       else if (EQ (keyw, Qdata))
1534         dataval = valw;
1535     }
1536
1537   chartab = Fmake_char_table (type);
1538
1539   data = dataval;
1540   while (!NILP (data))
1541     {
1542       Lisp_Object range = Fcar (data);
1543       Lisp_Object val = Fcar (Fcdr (data));
1544
1545       data = Fcdr (Fcdr (data));
1546       if (CONSP (range))
1547         {
1548           if (CHAR_OR_CHAR_INTP (XCAR (range)))
1549             {
1550               Emchar first = XCHAR_OR_CHAR_INT (Fcar (range));
1551               Emchar last = XCHAR_OR_CHAR_INT (Fcar (Fcdr (range)));
1552               Emchar i;
1553
1554               for (i = first; i <= last; i++)
1555                  Fput_char_table (make_char (i), val, chartab);
1556             }
1557           else
1558             abort ();
1559         }
1560       else
1561         Fput_char_table (range, val, chartab);
1562     }
1563
1564   return chartab;
1565 }
1566
1567 #ifdef MULE
1568
1569 \f
1570 /************************************************************************/
1571 /*                     Category Tables, specifically                    */
1572 /************************************************************************/
1573
1574 DEFUN ("category-table-p", Fcategory_table_p, 1, 1, 0, /*
1575 Return t if ARG is a category table.
1576 A category table is a type of char table used for keeping track of
1577 categories.  Categories are used for classifying characters for use
1578 in regexps -- you can refer to a category rather than having to use
1579 a complicated [] expression (and category lookups are significantly
1580 faster).
1581
1582 There are 95 different categories available, one for each printable
1583 character (including space) in the ASCII charset.  Each category
1584 is designated by one such character, called a "category designator".
1585 They are specified in a regexp using the syntax "\\cX", where X is
1586 a category designator.
1587
1588 A category table specifies, for each character, the categories that
1589 the character is in.  Note that a character can be in more than one
1590 category.  More specifically, a category table maps from a character
1591 to either the value nil (meaning the character is in no categories)
1592 or a 95-element bit vector, specifying for each of the 95 categories
1593 whether the character is in that category.
1594
1595 Special Lisp functions are provided that abstract this, so you do not
1596 have to directly manipulate bit vectors.
1597 */
1598        (obj))
1599 {
1600   return (CHAR_TABLEP (obj) &&
1601           XCHAR_TABLE_TYPE (obj) == CHAR_TABLE_TYPE_CATEGORY) ?
1602     Qt : Qnil;
1603 }
1604
1605 static Lisp_Object
1606 check_category_table (Lisp_Object obj, Lisp_Object def)
1607 {
1608   if (NILP (obj))
1609     obj = def;
1610   while (NILP (Fcategory_table_p (obj)))
1611     obj = wrong_type_argument (Qcategory_table_p, obj);
1612   return obj;
1613 }
1614
1615 int
1616 check_category_char (Emchar ch, Lisp_Object table,
1617                      unsigned int designator, unsigned int not)
1618 {
1619   REGISTER Lisp_Object temp;
1620   Lisp_Char_Table *ctbl;
1621 #ifdef ERROR_CHECK_TYPECHECK
1622   if (NILP (Fcategory_table_p (table)))
1623     signal_simple_error ("Expected category table", table);
1624 #endif
1625   ctbl = XCHAR_TABLE (table);
1626   temp = get_char_table (ch, ctbl);
1627   if (NILP (temp))
1628     return not;
1629
1630   designator -= ' ';
1631   return bit_vector_bit (XBIT_VECTOR (temp), designator) ? !not : not;
1632 }
1633
1634 DEFUN ("check-category-at", Fcheck_category_at, 2, 4, 0, /*
1635 Return t if category of a character at POS includes DESIGNATOR,
1636 else return nil. Optional third arg specifies which buffer
1637 \(defaulting to current), and fourth specifies the CATEGORY-TABLE,
1638 \(defaulting to the buffer's category table).
1639 */
1640        (pos, designator, buffer, category_table))
1641 {
1642   Lisp_Object ctbl;
1643   Emchar ch;
1644   unsigned int des;
1645   struct buffer *buf = decode_buffer (buffer, 0);
1646
1647   CHECK_INT (pos);
1648   CHECK_CATEGORY_DESIGNATOR (designator);
1649   des = XCHAR (designator);
1650   ctbl = check_category_table (category_table, Vstandard_category_table);
1651   ch = BUF_FETCH_CHAR (buf, XINT (pos));
1652   return check_category_char (ch, ctbl, des, 0) ? Qt : Qnil;
1653 }
1654
1655 DEFUN ("char-in-category-p", Fchar_in_category_p, 2, 3, 0, /*
1656 Return t if category of character CHR includes DESIGNATOR, else nil.
1657 Optional third arg specifies the CATEGORY-TABLE to use,
1658 which defaults to the system default table.
1659 */
1660        (chr, designator, category_table))
1661 {
1662   Lisp_Object ctbl;
1663   Emchar ch;
1664   unsigned int des;
1665
1666   CHECK_CATEGORY_DESIGNATOR (designator);
1667   des = XCHAR (designator);
1668   CHECK_CHAR (chr);
1669   ch = XCHAR (chr);
1670   ctbl = check_category_table (category_table, Vstandard_category_table);
1671   return check_category_char (ch, ctbl, des, 0) ? Qt : Qnil;
1672 }
1673
1674 DEFUN ("category-table", Fcategory_table, 0, 1, 0, /*
1675 Return the current category table.
1676 This is the one specified by the current buffer, or by BUFFER if it
1677 is non-nil.
1678 */
1679        (buffer))
1680 {
1681   return decode_buffer (buffer, 0)->category_table;
1682 }
1683
1684 DEFUN ("standard-category-table", Fstandard_category_table, 0, 0, 0, /*
1685 Return the standard category table.
1686 This is the one used for new buffers.
1687 */
1688        ())
1689 {
1690   return Vstandard_category_table;
1691 }
1692
1693 DEFUN ("copy-category-table", Fcopy_category_table, 0, 1, 0, /*
1694 Construct a new category table and return it.
1695 It is a copy of the TABLE, which defaults to the standard category table.
1696 */
1697        (table))
1698 {
1699   if (NILP (Vstandard_category_table))
1700     return Fmake_char_table (Qcategory);
1701
1702   table = check_category_table (table, Vstandard_category_table);
1703   return Fcopy_char_table (table);
1704 }
1705
1706 DEFUN ("set-category-table", Fset_category_table, 1, 2, 0, /*
1707 Select a new category table for BUFFER.
1708 One argument, a category table.
1709 BUFFER defaults to the current buffer if omitted.
1710 */
1711        (table, buffer))
1712 {
1713   struct buffer *buf = decode_buffer (buffer, 0);
1714   table = check_category_table (table, Qnil);
1715   buf->category_table = table;
1716   /* Indicate that this buffer now has a specified category table.  */
1717   buf->local_var_flags |= XINT (buffer_local_flags.category_table);
1718   return table;
1719 }
1720
1721 DEFUN ("category-designator-p", Fcategory_designator_p, 1, 1, 0, /*
1722 Return t if ARG is a category designator (a char in the range ' ' to '~').
1723 */
1724        (obj))
1725 {
1726   return CATEGORY_DESIGNATORP (obj) ? Qt : Qnil;
1727 }
1728
1729 DEFUN ("category-table-value-p", Fcategory_table_value_p, 1, 1, 0, /*
1730 Return t if ARG is a category table value.
1731 Valid values are nil or a bit vector of size 95.
1732 */
1733        (obj))
1734 {
1735   return CATEGORY_TABLE_VALUEP (obj) ? Qt : Qnil;
1736 }
1737
1738
1739 #define CATEGORYP(x) \
1740   (CHARP (x) && XCHAR (x) >= 0x20 && XCHAR (x) <= 0x7E)
1741
1742 #define CATEGORY_SET(c)                                         \
1743   (get_char_table(c, XCHAR_TABLE(current_buffer->category_table)))
1744
1745 /* Return 1 if CATEGORY_SET contains CATEGORY, else return 0.
1746    The faster version of `!NILP (Faref (category_set, category))'.  */
1747 #define CATEGORY_MEMBER(category, category_set)                 \
1748   (bit_vector_bit(XBIT_VECTOR (category_set), category - 32))
1749
1750 /* Return 1 if there is a word boundary between two word-constituent
1751    characters C1 and C2 if they appear in this order, else return 0.
1752    Use the macro WORD_BOUNDARY_P instead of calling this function
1753    directly.  */
1754
1755 int word_boundary_p (Emchar c1, Emchar c2);
1756 int
1757 word_boundary_p (Emchar c1, Emchar c2)
1758 {
1759   Lisp_Object category_set1, category_set2;
1760   Lisp_Object tail;
1761   int default_result;
1762
1763 #if 0
1764   if (COMPOSITE_CHAR_P (c1))
1765     c1 = cmpchar_component (c1, 0, 1);
1766   if (COMPOSITE_CHAR_P (c2))
1767     c2 = cmpchar_component (c2, 0, 1);
1768 #endif
1769
1770   if (EQ (CHAR_CHARSET (c1), CHAR_CHARSET (c2)))
1771     {
1772       tail = Vword_separating_categories;
1773       default_result = 0;
1774     }
1775   else
1776     {
1777       tail = Vword_combining_categories;
1778       default_result = 1;
1779     }
1780
1781   category_set1 = CATEGORY_SET (c1);
1782   if (NILP (category_set1))
1783     return default_result;
1784   category_set2 = CATEGORY_SET (c2);
1785   if (NILP (category_set2))
1786     return default_result;
1787
1788   for (; CONSP (tail); tail = XCONS (tail)->cdr)
1789     {
1790       Lisp_Object elt = XCONS(tail)->car;
1791
1792       if (CONSP (elt)
1793           && CATEGORYP (XCONS (elt)->car)
1794           && CATEGORYP (XCONS (elt)->cdr)
1795           && CATEGORY_MEMBER (XCHAR (XCONS (elt)->car), category_set1)
1796           && CATEGORY_MEMBER (XCHAR (XCONS (elt)->cdr), category_set2))
1797         return !default_result;
1798     }
1799   return default_result;
1800 }
1801 #endif /* MULE */
1802
1803 \f
1804 void
1805 syms_of_chartab (void)
1806 {
1807 #ifdef MULE
1808   defsymbol (&Qcategory_table_p, "category-table-p");
1809   defsymbol (&Qcategory_designator_p, "category-designator-p");
1810   defsymbol (&Qcategory_table_value_p, "category-table-value-p");
1811 #endif /* MULE */
1812
1813   defsymbol (&Qchar_table, "char-table");
1814   defsymbol (&Qchar_tablep, "char-table-p");
1815
1816   DEFSUBR (Fchar_table_p);
1817   DEFSUBR (Fchar_table_type_list);
1818   DEFSUBR (Fvalid_char_table_type_p);
1819   DEFSUBR (Fchar_table_type);
1820   DEFSUBR (Freset_char_table);
1821   DEFSUBR (Fmake_char_table);
1822   DEFSUBR (Fcopy_char_table);
1823   DEFSUBR (Fget_char_table);
1824   DEFSUBR (Fget_range_char_table);
1825   DEFSUBR (Fvalid_char_table_value_p);
1826   DEFSUBR (Fcheck_valid_char_table_value);
1827   DEFSUBR (Fput_char_table);
1828   DEFSUBR (Fmap_char_table);
1829
1830 #ifdef MULE
1831   DEFSUBR (Fcategory_table_p);
1832   DEFSUBR (Fcategory_table);
1833   DEFSUBR (Fstandard_category_table);
1834   DEFSUBR (Fcopy_category_table);
1835   DEFSUBR (Fset_category_table);
1836   DEFSUBR (Fcheck_category_at);
1837   DEFSUBR (Fchar_in_category_p);
1838   DEFSUBR (Fcategory_designator_p);
1839   DEFSUBR (Fcategory_table_value_p);
1840 #endif /* MULE */
1841
1842 }
1843
1844 void
1845 vars_of_chartab (void)
1846 {
1847   /* DO NOT staticpro this.  It works just like Vweak_hash_tables. */
1848   Vall_syntax_tables = Qnil;
1849   pdump_wire_list (&Vall_syntax_tables);
1850 }
1851
1852 void
1853 structure_type_create_chartab (void)
1854 {
1855   struct structure_type *st;
1856
1857   st = define_structure_type (Qchar_table, 0, chartab_instantiate);
1858
1859   define_structure_type_keyword (st, Qtype, chartab_type_validate);
1860   define_structure_type_keyword (st, Qdata, chartab_data_validate);
1861 }
1862
1863 void
1864 complex_vars_of_chartab (void)
1865 {
1866 #ifdef MULE
1867   /* Set this now, so first buffer creation can refer to it. */
1868   /* Make it nil before calling copy-category-table
1869      so that copy-category-table will know not to try to copy from garbage */
1870   Vstandard_category_table = Qnil;
1871   Vstandard_category_table = Fcopy_category_table (Qnil);
1872   staticpro (&Vstandard_category_table);
1873
1874   DEFVAR_LISP ("word-combining-categories", &Vword_combining_categories /*
1875 List of pair (cons) of categories to determine word boundary.
1876
1877 Emacs treats a sequence of word constituent characters as a single
1878 word (i.e. finds no word boundary between them) iff they belongs to
1879 the same charset.  But, exceptions are allowed in the following cases.
1880
1881 (1) The case that characters are in different charsets is controlled
1882 by the variable `word-combining-categories'.
1883
1884 Emacs finds no word boundary between characters of different charsets
1885 if they have categories matching some element of this list.
1886
1887 More precisely, if an element of this list is a cons of category CAT1
1888 and CAT2, and a multibyte character C1 which has CAT1 is followed by
1889 C2 which has CAT2, there's no word boundary between C1 and C2.
1890
1891 For instance, to tell that ASCII characters and Latin-1 characters can
1892 form a single word, the element `(?l . ?l)' should be in this list
1893 because both characters have the category `l' (Latin characters).
1894
1895 (2) The case that character are in the same charset is controlled by
1896 the variable `word-separating-categories'.
1897
1898 Emacs find a word boundary between characters of the same charset
1899 if they have categories matching some element of this list.
1900
1901 More precisely, if an element of this list is a cons of category CAT1
1902 and CAT2, and a multibyte character C1 which has CAT1 is followed by
1903 C2 which has CAT2, there's a word boundary between C1 and C2.
1904
1905 For instance, to tell that there's a word boundary between Japanese
1906 Hiragana and Japanese Kanji (both are in the same charset), the
1907 element `(?H . ?C) should be in this list.
1908 */ );
1909
1910   Vword_combining_categories = Qnil;
1911
1912   DEFVAR_LISP ("word-separating-categories", &Vword_separating_categories /*
1913 List of pair (cons) of categories to determine word boundary.
1914 See the documentation of the variable `word-combining-categories'.
1915 */ );
1916
1917   Vword_separating_categories = Qnil;
1918 #endif /* MULE */
1919 }