Reverse 2009-12-03T16:02:10Z!acm@muc.de "Enhance `c-parse-state' to run efficiently in
[emacs.git] / src / chartab.c
blob2b547184b21b1e816995685d785cb087cb413efc
1 /* chartab.c -- char-table support
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 National Institute of Advanced Industrial Science and Technology (AIST)
4 Registration Number H13PRO009
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <setjmp.h>
23 #include "lisp.h"
24 #include "character.h"
25 #include "charset.h"
26 #include "ccl.h"
28 /* 64/16/32/128 */
30 /* Number of elements in Nth level char-table. */
31 const int chartab_size[4] =
32 { (1 << CHARTAB_SIZE_BITS_0),
33 (1 << CHARTAB_SIZE_BITS_1),
34 (1 << CHARTAB_SIZE_BITS_2),
35 (1 << CHARTAB_SIZE_BITS_3) };
37 /* Number of characters each element of Nth level char-table
38 covers. */
39 const int chartab_chars[4] =
40 { (1 << (CHARTAB_SIZE_BITS_1 + CHARTAB_SIZE_BITS_2 + CHARTAB_SIZE_BITS_3)),
41 (1 << (CHARTAB_SIZE_BITS_2 + CHARTAB_SIZE_BITS_3)),
42 (1 << CHARTAB_SIZE_BITS_3),
43 1 };
45 /* Number of characters (in bits) each element of Nth level char-table
46 covers. */
47 const int chartab_bits[4] =
48 { (CHARTAB_SIZE_BITS_1 + CHARTAB_SIZE_BITS_2 + CHARTAB_SIZE_BITS_3),
49 (CHARTAB_SIZE_BITS_2 + CHARTAB_SIZE_BITS_3),
50 CHARTAB_SIZE_BITS_3,
51 0 };
53 #define CHARTAB_IDX(c, depth, min_char) \
54 (((c) - (min_char)) >> chartab_bits[(depth)])
57 DEFUN ("make-char-table", Fmake_char_table, Smake_char_table, 1, 2, 0,
58 doc: /* Return a newly created char-table, with purpose PURPOSE.
59 Each element is initialized to INIT, which defaults to nil.
61 PURPOSE should be a symbol. If it has a `char-table-extra-slots'
62 property, the property's value should be an integer between 0 and 10
63 that specifies how many extra slots the char-table has. Otherwise,
64 the char-table has no extra slot. */)
65 (purpose, init)
66 register Lisp_Object purpose, init;
68 Lisp_Object vector;
69 Lisp_Object n;
70 int n_extras;
71 int size;
73 CHECK_SYMBOL (purpose);
74 n = Fget (purpose, Qchar_table_extra_slots);
75 if (NILP (n))
76 n_extras = 0;
77 else
79 CHECK_NATNUM (n);
80 n_extras = XINT (n);
81 if (n_extras > 10)
82 args_out_of_range (n, Qnil);
85 size = VECSIZE (struct Lisp_Char_Table) - 1 + n_extras;
86 vector = Fmake_vector (make_number (size), init);
87 XSETPVECTYPE (XVECTOR (vector), PVEC_CHAR_TABLE);
88 XCHAR_TABLE (vector)->parent = Qnil;
89 XCHAR_TABLE (vector)->purpose = purpose;
90 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
91 return vector;
94 static Lisp_Object
95 make_sub_char_table (depth, min_char, defalt)
96 int depth, min_char;
97 Lisp_Object defalt;
99 Lisp_Object table;
100 int size = VECSIZE (struct Lisp_Sub_Char_Table) - 1 + chartab_size[depth];
102 table = Fmake_vector (make_number (size), defalt);
103 XSETPVECTYPE (XVECTOR (table), PVEC_SUB_CHAR_TABLE);
104 XSUB_CHAR_TABLE (table)->depth = make_number (depth);
105 XSUB_CHAR_TABLE (table)->min_char = make_number (min_char);
107 return table;
110 static Lisp_Object
111 char_table_ascii (table)
112 Lisp_Object table;
114 Lisp_Object sub;
116 sub = XCHAR_TABLE (table)->contents[0];
117 if (! SUB_CHAR_TABLE_P (sub))
118 return sub;
119 sub = XSUB_CHAR_TABLE (sub)->contents[0];
120 if (! SUB_CHAR_TABLE_P (sub))
121 return sub;
122 return XSUB_CHAR_TABLE (sub)->contents[0];
125 Lisp_Object
126 copy_sub_char_table (table)
127 Lisp_Object table;
129 Lisp_Object copy;
130 int depth = XINT (XSUB_CHAR_TABLE (table)->depth);
131 int min_char = XINT (XSUB_CHAR_TABLE (table)->min_char);
132 Lisp_Object val;
133 int i;
135 copy = make_sub_char_table (depth, min_char, Qnil);
136 /* Recursively copy any sub char-tables. */
137 for (i = 0; i < chartab_size[depth]; i++)
139 val = XSUB_CHAR_TABLE (table)->contents[i];
140 if (SUB_CHAR_TABLE_P (val))
141 XSUB_CHAR_TABLE (copy)->contents[i] = copy_sub_char_table (val);
142 else
143 XSUB_CHAR_TABLE (copy)->contents[i] = val;
146 return copy;
150 Lisp_Object
151 copy_char_table (table)
152 Lisp_Object table;
154 Lisp_Object copy;
155 int size = XCHAR_TABLE (table)->size & PSEUDOVECTOR_SIZE_MASK;
156 int i;
158 copy = Fmake_vector (make_number (size), Qnil);
159 XSETPVECTYPE (XVECTOR (copy), PVEC_CHAR_TABLE);
160 XCHAR_TABLE (copy)->defalt = XCHAR_TABLE (table)->defalt;
161 XCHAR_TABLE (copy)->parent = XCHAR_TABLE (table)->parent;
162 XCHAR_TABLE (copy)->purpose = XCHAR_TABLE (table)->purpose;
163 for (i = 0; i < chartab_size[0]; i++)
164 XCHAR_TABLE (copy)->contents[i]
165 = (SUB_CHAR_TABLE_P (XCHAR_TABLE (table)->contents[i])
166 ? copy_sub_char_table (XCHAR_TABLE (table)->contents[i])
167 : XCHAR_TABLE (table)->contents[i]);
168 XCHAR_TABLE (copy)->ascii = char_table_ascii (copy);
169 size -= VECSIZE (struct Lisp_Char_Table) - 1;
170 for (i = 0; i < size; i++)
171 XCHAR_TABLE (copy)->extras[i] = XCHAR_TABLE (table)->extras[i];
173 XSETCHAR_TABLE (copy, XCHAR_TABLE (copy));
174 return copy;
177 Lisp_Object
178 sub_char_table_ref (table, c)
179 Lisp_Object table;
180 int c;
182 struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
183 int depth = XINT (tbl->depth);
184 int min_char = XINT (tbl->min_char);
185 Lisp_Object val;
187 val = tbl->contents[CHARTAB_IDX (c, depth, min_char)];
188 if (SUB_CHAR_TABLE_P (val))
189 val = sub_char_table_ref (val, c);
190 return val;
193 Lisp_Object
194 char_table_ref (table, c)
195 Lisp_Object table;
196 int c;
198 struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
199 Lisp_Object val;
201 if (ASCII_CHAR_P (c))
203 val = tbl->ascii;
204 if (SUB_CHAR_TABLE_P (val))
205 val = XSUB_CHAR_TABLE (val)->contents[c];
207 else
209 val = tbl->contents[CHARTAB_IDX (c, 0, 0)];
210 if (SUB_CHAR_TABLE_P (val))
211 val = sub_char_table_ref (val, c);
213 if (NILP (val))
215 val = tbl->defalt;
216 if (NILP (val) && CHAR_TABLE_P (tbl->parent))
217 val = char_table_ref (tbl->parent, c);
219 return val;
222 static Lisp_Object
223 sub_char_table_ref_and_range (table, c, from, to, defalt)
224 Lisp_Object table;
225 int c;
226 int *from, *to;
227 Lisp_Object defalt;
229 struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
230 int depth = XINT (tbl->depth);
231 int min_char = XINT (tbl->min_char);
232 int max_char = min_char + chartab_chars[depth - 1] - 1;
233 int index = CHARTAB_IDX (c, depth, min_char), idx;
234 Lisp_Object val;
236 val = tbl->contents[index];
237 if (SUB_CHAR_TABLE_P (val))
238 val = sub_char_table_ref_and_range (val, c, from, to, defalt);
239 else if (NILP (val))
240 val = defalt;
242 idx = index;
243 while (idx > 0 && *from < min_char + idx * chartab_chars[depth])
245 Lisp_Object this_val;
247 c = min_char + idx * chartab_chars[depth] - 1;
248 idx--;
249 this_val = tbl->contents[idx];
250 if (SUB_CHAR_TABLE_P (this_val))
251 this_val = sub_char_table_ref_and_range (this_val, c, from, to, defalt);
252 else if (NILP (this_val))
253 this_val = defalt;
255 if (! EQ (this_val, val))
257 *from = c + 1;
258 break;
261 while ((c = min_char + (index + 1) * chartab_chars[depth]) <= max_char
262 && *to >= c)
264 Lisp_Object this_val;
266 index++;
267 this_val = tbl->contents[index];
268 if (SUB_CHAR_TABLE_P (this_val))
269 this_val = sub_char_table_ref_and_range (this_val, c, from, to, defalt);
270 else if (NILP (this_val))
271 this_val = defalt;
272 if (! EQ (this_val, val))
274 *to = c - 1;
275 break;
279 return val;
283 /* Return the value for C in char-table TABLE. Shrink the range *FROM
284 and *TO to cover characters (containing C) that have the same value
285 as C. It is not assured that the values of (*FROM - 1) and (*TO +
286 1) are different from that of C. */
288 Lisp_Object
289 char_table_ref_and_range (table, c, from, to)
290 Lisp_Object table;
291 int c;
292 int *from, *to;
294 struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
295 int index = CHARTAB_IDX (c, 0, 0), idx;
296 Lisp_Object val;
298 val = tbl->contents[index];
299 if (*from < 0)
300 *from = 0;
301 if (*to < 0)
302 *to = MAX_CHAR;
303 if (SUB_CHAR_TABLE_P (val))
304 val = sub_char_table_ref_and_range (val, c, from, to, tbl->defalt);
305 else if (NILP (val))
306 val = tbl->defalt;
308 idx = index;
309 while (*from < idx * chartab_chars[0])
311 Lisp_Object this_val;
313 c = idx * chartab_chars[0] - 1;
314 idx--;
315 this_val = tbl->contents[idx];
316 if (SUB_CHAR_TABLE_P (this_val))
317 this_val = sub_char_table_ref_and_range (this_val, c, from, to,
318 tbl->defalt);
319 else if (NILP (this_val))
320 this_val = tbl->defalt;
322 if (! EQ (this_val, val))
324 *from = c + 1;
325 break;
328 while (*to >= (index + 1) * chartab_chars[0])
330 Lisp_Object this_val;
332 index++;
333 c = index * chartab_chars[0];
334 this_val = tbl->contents[index];
335 if (SUB_CHAR_TABLE_P (this_val))
336 this_val = sub_char_table_ref_and_range (this_val, c, from, to,
337 tbl->defalt);
338 else if (NILP (this_val))
339 this_val = tbl->defalt;
340 if (! EQ (this_val, val))
342 *to = c - 1;
343 break;
347 return val;
351 #define ASET_RANGE(ARRAY, FROM, TO, LIMIT, VAL) \
352 do { \
353 int limit = (TO) < (LIMIT) ? (TO) : (LIMIT); \
354 for (; (FROM) < limit; (FROM)++) (ARRAY)->contents[(FROM)] = (VAL); \
355 } while (0)
357 #define GET_SUB_CHAR_TABLE(TABLE, SUBTABLE, IDX, DEPTH, MIN_CHAR) \
358 do { \
359 (SUBTABLE) = (TABLE)->contents[(IDX)]; \
360 if (!SUB_CHAR_TABLE_P (SUBTABLE)) \
361 (SUBTABLE) = make_sub_char_table ((DEPTH), (MIN_CHAR), (SUBTABLE)); \
362 } while (0)
365 static void
366 sub_char_table_set (table, c, val)
367 Lisp_Object table;
368 int c;
369 Lisp_Object val;
371 struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
372 int depth = XINT ((tbl)->depth);
373 int min_char = XINT ((tbl)->min_char);
374 int i = CHARTAB_IDX (c, depth, min_char);
375 Lisp_Object sub;
377 if (depth == 3)
378 tbl->contents[i] = val;
379 else
381 sub = tbl->contents[i];
382 if (! SUB_CHAR_TABLE_P (sub))
384 sub = make_sub_char_table (depth + 1,
385 min_char + i * chartab_chars[depth], sub);
386 tbl->contents[i] = sub;
388 sub_char_table_set (sub, c, val);
392 Lisp_Object
393 char_table_set (table, c, val)
394 Lisp_Object table;
395 int c;
396 Lisp_Object val;
398 struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
400 if (ASCII_CHAR_P (c)
401 && SUB_CHAR_TABLE_P (tbl->ascii))
403 XSUB_CHAR_TABLE (tbl->ascii)->contents[c] = val;
405 else
407 int i = CHARTAB_IDX (c, 0, 0);
408 Lisp_Object sub;
410 sub = tbl->contents[i];
411 if (! SUB_CHAR_TABLE_P (sub))
413 sub = make_sub_char_table (1, i * chartab_chars[0], sub);
414 tbl->contents[i] = sub;
416 sub_char_table_set (sub, c, val);
417 if (ASCII_CHAR_P (c))
418 tbl->ascii = char_table_ascii (table);
420 return val;
423 static void
424 sub_char_table_set_range (table, depth, min_char, from, to, val)
425 Lisp_Object *table;
426 int depth;
427 int min_char;
428 int from, to;
429 Lisp_Object val;
431 int max_char = min_char + chartab_chars[depth] - 1;
433 if (depth == 3 || (from <= min_char && to >= max_char))
434 *table = val;
435 else
437 int i, j;
439 depth++;
440 if (! SUB_CHAR_TABLE_P (*table))
441 *table = make_sub_char_table (depth, min_char, *table);
442 if (from < min_char)
443 from = min_char;
444 if (to > max_char)
445 to = max_char;
446 i = CHARTAB_IDX (from, depth, min_char);
447 j = CHARTAB_IDX (to, depth, min_char);
448 min_char += chartab_chars[depth] * i;
449 for (; i <= j; i++, min_char += chartab_chars[depth])
450 sub_char_table_set_range (XSUB_CHAR_TABLE (*table)->contents + i,
451 depth, min_char, from, to, val);
456 Lisp_Object
457 char_table_set_range (table, from, to, val)
458 Lisp_Object table;
459 int from, to;
460 Lisp_Object val;
462 struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
463 Lisp_Object *contents = tbl->contents;
464 int i, min_char;
466 if (from == to)
467 char_table_set (table, from, val);
468 else
470 for (i = CHARTAB_IDX (from, 0, 0), min_char = i * chartab_chars[0];
471 min_char <= to;
472 i++, min_char += chartab_chars[0])
473 sub_char_table_set_range (contents + i, 0, min_char, from, to, val);
474 if (ASCII_CHAR_P (from))
475 tbl->ascii = char_table_ascii (table);
477 return val;
481 DEFUN ("char-table-subtype", Fchar_table_subtype, Schar_table_subtype,
482 1, 1, 0,
483 doc: /*
484 Return the subtype of char-table CHAR-TABLE. The value is a symbol. */)
485 (char_table)
486 Lisp_Object char_table;
488 CHECK_CHAR_TABLE (char_table);
490 return XCHAR_TABLE (char_table)->purpose;
493 DEFUN ("char-table-parent", Fchar_table_parent, Schar_table_parent,
494 1, 1, 0,
495 doc: /* Return the parent char-table of CHAR-TABLE.
496 The value is either nil or another char-table.
497 If CHAR-TABLE holds nil for a given character,
498 then the actual applicable value is inherited from the parent char-table
499 \(or from its parents, if necessary). */)
500 (char_table)
501 Lisp_Object char_table;
503 CHECK_CHAR_TABLE (char_table);
505 return XCHAR_TABLE (char_table)->parent;
508 DEFUN ("set-char-table-parent", Fset_char_table_parent, Sset_char_table_parent,
509 2, 2, 0,
510 doc: /* Set the parent char-table of CHAR-TABLE to PARENT.
511 Return PARENT. PARENT must be either nil or another char-table. */)
512 (char_table, parent)
513 Lisp_Object char_table, parent;
515 Lisp_Object temp;
517 CHECK_CHAR_TABLE (char_table);
519 if (!NILP (parent))
521 CHECK_CHAR_TABLE (parent);
523 for (temp = parent; !NILP (temp); temp = XCHAR_TABLE (temp)->parent)
524 if (EQ (temp, char_table))
525 error ("Attempt to make a chartable be its own parent");
528 XCHAR_TABLE (char_table)->parent = parent;
530 return parent;
533 DEFUN ("char-table-extra-slot", Fchar_table_extra_slot, Schar_table_extra_slot,
534 2, 2, 0,
535 doc: /* Return the value of CHAR-TABLE's extra-slot number N. */)
536 (char_table, n)
537 Lisp_Object char_table, n;
539 CHECK_CHAR_TABLE (char_table);
540 CHECK_NUMBER (n);
541 if (XINT (n) < 0
542 || XINT (n) >= CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (char_table)))
543 args_out_of_range (char_table, n);
545 return XCHAR_TABLE (char_table)->extras[XINT (n)];
548 DEFUN ("set-char-table-extra-slot", Fset_char_table_extra_slot,
549 Sset_char_table_extra_slot,
550 3, 3, 0,
551 doc: /* Set CHAR-TABLE's extra-slot number N to VALUE. */)
552 (char_table, n, value)
553 Lisp_Object char_table, n, value;
555 CHECK_CHAR_TABLE (char_table);
556 CHECK_NUMBER (n);
557 if (XINT (n) < 0
558 || XINT (n) >= CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (char_table)))
559 args_out_of_range (char_table, n);
561 return XCHAR_TABLE (char_table)->extras[XINT (n)] = value;
564 DEFUN ("char-table-range", Fchar_table_range, Schar_table_range,
565 2, 2, 0,
566 doc: /* Return the value in CHAR-TABLE for a range of characters RANGE.
567 RANGE should be nil (for the default value),
568 a cons of character codes (for characters in the range), or a character code. */)
569 (char_table, range)
570 Lisp_Object char_table, range;
572 Lisp_Object val;
573 CHECK_CHAR_TABLE (char_table);
575 if (EQ (range, Qnil))
576 val = XCHAR_TABLE (char_table)->defalt;
577 else if (INTEGERP (range))
578 val = CHAR_TABLE_REF (char_table, XINT (range));
579 else if (CONSP (range))
581 int from, to;
583 CHECK_CHARACTER_CAR (range);
584 CHECK_CHARACTER_CDR (range);
585 val = char_table_ref_and_range (char_table, XINT (XCAR (range)),
586 &from, &to);
587 /* Not yet implemented. */
589 else
590 error ("Invalid RANGE argument to `char-table-range'");
591 return val;
594 DEFUN ("set-char-table-range", Fset_char_table_range, Sset_char_table_range,
595 3, 3, 0,
596 doc: /* Set the value in CHAR-TABLE for a range of characters RANGE to VALUE.
597 RANGE should be t (for all characters), nil (for the default value),
598 a cons of character codes (for characters in the range),
599 or a character code. Return VALUE. */)
600 (char_table, range, value)
601 Lisp_Object char_table, range, value;
603 CHECK_CHAR_TABLE (char_table);
604 if (EQ (range, Qt))
606 int i;
608 XCHAR_TABLE (char_table)->ascii = value;
609 for (i = 0; i < chartab_size[0]; i++)
610 XCHAR_TABLE (char_table)->contents[i] = value;
612 else if (EQ (range, Qnil))
613 XCHAR_TABLE (char_table)->defalt = value;
614 else if (INTEGERP (range))
615 char_table_set (char_table, XINT (range), value);
616 else if (CONSP (range))
618 CHECK_CHARACTER_CAR (range);
619 CHECK_CHARACTER_CDR (range);
620 char_table_set_range (char_table,
621 XINT (XCAR (range)), XINT (XCDR (range)), value);
623 else
624 error ("Invalid RANGE argument to `set-char-table-range'");
626 return value;
629 DEFUN ("set-char-table-default", Fset_char_table_default,
630 Sset_char_table_default, 3, 3, 0,
631 doc: /*
632 This function is obsolete and has no effect. */)
633 (char_table, ch, value)
634 Lisp_Object char_table, ch, value;
636 return Qnil;
639 /* Look up the element in TABLE at index CH, and return it as an
640 integer. If the element is not a character, return CH itself. */
643 char_table_translate (table, ch)
644 Lisp_Object table;
645 int ch;
647 Lisp_Object value;
648 value = Faref (table, make_number (ch));
649 if (! CHARACTERP (value))
650 return ch;
651 return XINT (value);
654 static Lisp_Object
655 optimize_sub_char_table (table, test)
656 Lisp_Object table, test;
658 struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
659 int depth = XINT (tbl->depth);
660 Lisp_Object elt, this;
661 int i, optimizable;
663 elt = XSUB_CHAR_TABLE (table)->contents[0];
664 if (SUB_CHAR_TABLE_P (elt))
665 elt = XSUB_CHAR_TABLE (table)->contents[0]
666 = optimize_sub_char_table (elt, test);
667 optimizable = SUB_CHAR_TABLE_P (elt) ? 0 : 1;
668 for (i = 1; i < chartab_size[depth]; i++)
670 this = XSUB_CHAR_TABLE (table)->contents[i];
671 if (SUB_CHAR_TABLE_P (this))
672 this = XSUB_CHAR_TABLE (table)->contents[i]
673 = optimize_sub_char_table (this, test);
674 if (optimizable
675 && (NILP (test) ? NILP (Fequal (this, elt)) /* defaults to `equal'. */
676 : EQ (test, Qeq) ? !EQ (this, elt) /* Optimize `eq' case. */
677 : NILP (call2 (test, this, elt))))
678 optimizable = 0;
681 return (optimizable ? elt : table);
684 DEFUN ("optimize-char-table", Foptimize_char_table, Soptimize_char_table,
685 1, 2, 0,
686 doc: /* Optimize CHAR-TABLE.
687 TEST is the comparison function used to decide whether two entries are
688 equivalent and can be merged. It defaults to `equal'. */)
689 (char_table, test)
690 Lisp_Object char_table, test;
692 Lisp_Object elt;
693 int i;
695 CHECK_CHAR_TABLE (char_table);
697 for (i = 0; i < chartab_size[0]; i++)
699 elt = XCHAR_TABLE (char_table)->contents[i];
700 if (SUB_CHAR_TABLE_P (elt))
701 XCHAR_TABLE (char_table)->contents[i]
702 = optimize_sub_char_table (elt, test);
704 /* Reset the `ascii' cache, in case it got optimized away. */
705 XCHAR_TABLE (char_table)->ascii = char_table_ascii (char_table);
707 return Qnil;
711 /* Map C_FUNCTION or FUNCTION over TABLE (top or sub char-table),
712 calling it for each character or group of characters that share a
713 value. RANGE is a cons (FROM . TO) specifying the range of target
714 characters, VAL is a value of FROM in TABLE, DEFAULT_VAL is the
715 default value of the char-table, PARENT is the parent of the
716 char-table.
718 ARG is passed to C_FUNCTION when that is called.
720 It returns the value of last character covered by TABLE (not the
721 value inheritted from the parent), and by side-effect, the car part
722 of RANGE is updated to the minimum character C where C and all the
723 following characters in TABLE have the same value. */
725 static Lisp_Object
726 map_sub_char_table (c_function, function, table, arg, val, range,
727 default_val, parent)
728 void (*c_function) P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
729 Lisp_Object function, table, arg, val, range, default_val, parent;
731 /* Pointer to the elements of TABLE. */
732 Lisp_Object *contents;
733 /* Depth of TABLE. */
734 int depth;
735 /* Minimum and maxinum characters covered by TABLE. */
736 int min_char, max_char;
737 /* Number of characters covered by one element of TABLE. */
738 int chars_in_block;
739 int from = XINT (XCAR (range)), to = XINT (XCDR (range));
740 int i, c;
742 if (SUB_CHAR_TABLE_P (table))
744 struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
746 depth = XINT (tbl->depth);
747 contents = tbl->contents;
748 min_char = XINT (tbl->min_char);
749 max_char = min_char + chartab_chars[depth - 1] - 1;
751 else
753 depth = 0;
754 contents = XCHAR_TABLE (table)->contents;
755 min_char = 0;
756 max_char = MAX_CHAR;
758 chars_in_block = chartab_chars[depth];
760 if (to < max_char)
761 max_char = to;
762 /* Set I to the index of the first element to check. */
763 if (from <= min_char)
764 i = 0;
765 else
766 i = (from - min_char) / chars_in_block;
767 for (c = min_char + chars_in_block * i; c <= max_char;
768 i++, c += chars_in_block)
770 Lisp_Object this = contents[i];
771 int nextc = c + chars_in_block;
773 if (SUB_CHAR_TABLE_P (this))
775 if (to >= nextc)
776 XSETCDR (range, make_number (nextc - 1));
777 val = map_sub_char_table (c_function, function, this, arg,
778 val, range, default_val, parent);
780 else
782 if (NILP (this))
783 this = default_val;
784 if (!EQ (val, this))
786 int different_value = 1;
788 if (NILP (val))
790 if (! NILP (parent))
792 Lisp_Object temp = XCHAR_TABLE (parent)->parent;
794 /* This is to get a value of FROM in PARENT
795 without checking the parent of PARENT. */
796 XCHAR_TABLE (parent)->parent = Qnil;
797 val = CHAR_TABLE_REF (parent, from);
798 XCHAR_TABLE (parent)->parent = temp;
799 XSETCDR (range, make_number (c - 1));
800 val = map_sub_char_table (c_function, function,
801 parent, arg, val, range,
802 XCHAR_TABLE (parent)->defalt,
803 XCHAR_TABLE (parent)->parent);
804 if (EQ (val, this))
805 different_value = 0;
808 if (! NILP (val) && different_value)
810 XSETCDR (range, make_number (c - 1));
811 if (EQ (XCAR (range), XCDR (range)))
813 if (c_function)
814 (*c_function) (arg, XCAR (range), val);
815 else
816 call2 (function, XCAR (range), val);
818 else
820 if (c_function)
821 (*c_function) (arg, range, val);
822 else
823 call2 (function, range, val);
826 val = this;
827 from = c;
828 XSETCAR (range, make_number (c));
831 XSETCDR (range, make_number (to));
833 return val;
837 /* Map C_FUNCTION or FUNCTION over TABLE, calling it for each
838 character or group of characters that share a value.
840 ARG is passed to C_FUNCTION when that is called. */
842 void
843 map_char_table (c_function, function, table, arg)
844 void (*c_function) P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
845 Lisp_Object function, table, arg;
847 Lisp_Object range, val;
848 struct gcpro gcpro1, gcpro2, gcpro3;
850 range = Fcons (make_number (0), make_number (MAX_CHAR));
851 GCPRO3 (table, arg, range);
852 val = XCHAR_TABLE (table)->ascii;
853 if (SUB_CHAR_TABLE_P (val))
854 val = XSUB_CHAR_TABLE (val)->contents[0];
855 val = map_sub_char_table (c_function, function, table, arg, val, range,
856 XCHAR_TABLE (table)->defalt,
857 XCHAR_TABLE (table)->parent);
858 /* If VAL is nil and TABLE has a parent, we must consult the parent
859 recursively. */
860 while (NILP (val) && ! NILP (XCHAR_TABLE (table)->parent))
862 Lisp_Object parent = XCHAR_TABLE (table)->parent;
863 Lisp_Object temp = XCHAR_TABLE (parent)->parent;
864 int from = XINT (XCAR (range));
866 /* This is to get a value of FROM in PARENT without checking the
867 parent of PARENT. */
868 XCHAR_TABLE (parent)->parent = Qnil;
869 val = CHAR_TABLE_REF (parent, from);
870 XCHAR_TABLE (parent)->parent = temp;
871 val = map_sub_char_table (c_function, function, parent, arg, val, range,
872 XCHAR_TABLE (parent)->defalt,
873 XCHAR_TABLE (parent)->parent);
874 table = parent;
877 if (! NILP (val))
879 if (EQ (XCAR (range), XCDR (range)))
881 if (c_function)
882 (*c_function) (arg, XCAR (range), val);
883 else
884 call2 (function, XCAR (range), val);
886 else
888 if (c_function)
889 (*c_function) (arg, range, val);
890 else
891 call2 (function, range, val);
895 UNGCPRO;
898 DEFUN ("map-char-table", Fmap_char_table, Smap_char_table,
899 2, 2, 0,
900 doc: /*
901 Call FUNCTION for each character in CHAR-TABLE that has non-nil value.
902 FUNCTION is called with two arguments--a key and a value.
903 The key is a character code or a cons of character codes specifying a
904 range of characters that have the same value. */)
905 (function, char_table)
906 Lisp_Object function, char_table;
908 CHECK_CHAR_TABLE (char_table);
910 map_char_table (NULL, function, char_table, char_table);
911 return Qnil;
915 static void
916 map_sub_char_table_for_charset (c_function, function, table, arg, range,
917 charset, from, to)
918 void (*c_function) P_ ((Lisp_Object, Lisp_Object));
919 Lisp_Object function, table, arg, range;
920 struct charset *charset;
921 unsigned from, to;
923 struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
924 int depth = XINT (tbl->depth);
925 int c, i;
927 if (depth < 3)
928 for (i = 0, c = XINT (tbl->min_char); i < chartab_size[depth];
929 i++, c += chartab_chars[depth])
931 Lisp_Object this;
933 this = tbl->contents[i];
934 if (SUB_CHAR_TABLE_P (this))
935 map_sub_char_table_for_charset (c_function, function, this, arg,
936 range, charset, from, to);
937 else
939 if (! NILP (XCAR (range)))
941 XSETCDR (range, make_number (c - 1));
942 if (c_function)
943 (*c_function) (arg, range);
944 else
945 call2 (function, range, arg);
947 XSETCAR (range, Qnil);
950 else
951 for (i = 0, c = XINT (tbl->min_char); i < chartab_size[depth]; i++, c ++)
953 Lisp_Object this;
954 unsigned code;
956 this = tbl->contents[i];
957 if (NILP (this)
958 || (charset
959 && (code = ENCODE_CHAR (charset, c),
960 (code < from || code > to))))
962 if (! NILP (XCAR (range)))
964 XSETCDR (range, make_number (c - 1));
965 if (c_function)
966 (*c_function) (arg, range);
967 else
968 call2 (function, range, arg);
969 XSETCAR (range, Qnil);
972 else
974 if (NILP (XCAR (range)))
975 XSETCAR (range, make_number (c));
981 /* Support function for `map-charset-chars'. Map C_FUNCTION or
982 FUNCTION over TABLE, calling it for each character or a group of
983 succeeding characters that have non-nil value in TABLE. TABLE is a
984 "mapping table" or a "deunifier table" of a certain charset.
986 If CHARSET is not NULL (this is the case that `map-charset-chars'
987 is called with non-nil FROM-CODE and TO-CODE), it is a charset who
988 owns TABLE, and the function is called only on a character in the
989 range FROM and TO. FROM and TO are not character codes, but code
990 points of a character in CHARSET.
992 This function is called in these two cases:
994 (1) A charset has a mapping file name in :map property.
996 (2) A charset has an upper code space in :offset property and a
997 mapping file name in :unify-map property. In this case, this
998 function is called only for characters in the Unicode code space.
999 Characters in upper code space are handled directly in
1000 map_charset_chars. */
1002 void
1003 map_char_table_for_charset (c_function, function, table, arg,
1004 charset, from, to)
1005 void (*c_function) P_ ((Lisp_Object, Lisp_Object));
1006 Lisp_Object function, table, arg;
1007 struct charset *charset;
1008 unsigned from, to;
1010 Lisp_Object range;
1011 int c, i;
1012 struct gcpro gcpro1;
1014 range = Fcons (Qnil, Qnil);
1015 GCPRO1 (range);
1017 for (i = 0, c = 0; i < chartab_size[0]; i++, c += chartab_chars[0])
1019 Lisp_Object this;
1021 this = XCHAR_TABLE (table)->contents[i];
1022 if (SUB_CHAR_TABLE_P (this))
1023 map_sub_char_table_for_charset (c_function, function, this, arg,
1024 range, charset, from, to);
1025 else
1027 if (! NILP (XCAR (range)))
1029 XSETCDR (range, make_number (c - 1));
1030 if (c_function)
1031 (*c_function) (arg, range);
1032 else
1033 call2 (function, range, arg);
1035 XSETCAR (range, Qnil);
1038 if (! NILP (XCAR (range)))
1040 XSETCDR (range, make_number (c - 1));
1041 if (c_function)
1042 (*c_function) (arg, range);
1043 else
1044 call2 (function, range, arg);
1047 UNGCPRO;
1051 void
1052 syms_of_chartab ()
1054 defsubr (&Smake_char_table);
1055 defsubr (&Schar_table_parent);
1056 defsubr (&Schar_table_subtype);
1057 defsubr (&Sset_char_table_parent);
1058 defsubr (&Schar_table_extra_slot);
1059 defsubr (&Sset_char_table_extra_slot);
1060 defsubr (&Schar_table_range);
1061 defsubr (&Sset_char_table_range);
1062 defsubr (&Sset_char_table_default);
1063 defsubr (&Soptimize_char_table);
1064 defsubr (&Smap_char_table);
1067 /* arch-tag: 18b5b560-7ab5-4108-b09e-d5dd65dc6fda
1068 (do not change this comment) */