(calendar-location-name, calendar-latitude)
[emacs.git] / src / chartab.c
blobf7a3c2ccbf5b669e7d8fcc9f86e40af51de5ce42
1 /* chartab.c -- char-table support
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
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, or (at your option)
11 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; see the file COPYING. If not, write to the
20 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 #include <config.h>
24 #include "lisp.h"
25 #include "character.h"
26 #include "charset.h"
27 #include "ccl.h"
29 /* 64/16/32/128 */
31 /* Number of elements in Nth level char-table. */
32 const int chartab_size[4] =
33 { (1 << CHARTAB_SIZE_BITS_0),
34 (1 << CHARTAB_SIZE_BITS_1),
35 (1 << CHARTAB_SIZE_BITS_2),
36 (1 << CHARTAB_SIZE_BITS_3) };
38 /* Number of characters each element of Nth level char-table
39 covers. */
40 const int chartab_chars[4] =
41 { (1 << (CHARTAB_SIZE_BITS_1 + CHARTAB_SIZE_BITS_2 + CHARTAB_SIZE_BITS_3)),
42 (1 << (CHARTAB_SIZE_BITS_2 + CHARTAB_SIZE_BITS_3)),
43 (1 << CHARTAB_SIZE_BITS_3),
44 1 };
46 /* Number of characters (in bits) each element of Nth level char-table
47 covers. */
48 const int chartab_bits[4] =
49 { (CHARTAB_SIZE_BITS_1 + CHARTAB_SIZE_BITS_2 + CHARTAB_SIZE_BITS_3),
50 (CHARTAB_SIZE_BITS_2 + CHARTAB_SIZE_BITS_3),
51 CHARTAB_SIZE_BITS_3,
52 0 };
54 #define CHARTAB_IDX(c, depth, min_char) \
55 (((c) - (min_char)) >> chartab_bits[(depth)])
58 DEFUN ("make-char-table", Fmake_char_table, Smake_char_table, 1, 2, 0,
59 doc: /* Return a newly created char-table, with purpose PURPOSE.
60 Each element is initialized to INIT, which defaults to nil.
62 PURPOSE should be a symbol. If it has a `char-table-extra-slots'
63 property, the property's value should be an integer between 0 and 10
64 that specifies how many extra slots the char-table has. Otherwise,
65 the char-table has no extra slot. */)
66 (purpose, init)
67 register Lisp_Object purpose, init;
69 Lisp_Object vector;
70 Lisp_Object n;
71 int n_extras;
72 int size;
74 CHECK_SYMBOL (purpose);
75 n = Fget (purpose, Qchar_table_extra_slots);
76 if (NILP (n))
77 n_extras = 0;
78 else
80 CHECK_NATNUM (n);
81 n_extras = XINT (n);
82 if (n_extras > 10)
83 args_out_of_range (n, Qnil);
86 size = VECSIZE (struct Lisp_Char_Table) - 1 + n_extras;
87 vector = Fmake_vector (make_number (size), init);
88 XSETPVECTYPE (XVECTOR (vector), PVEC_CHAR_TABLE);
89 XCHAR_TABLE (vector)->parent = Qnil;
90 XCHAR_TABLE (vector)->purpose = purpose;
91 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
92 return vector;
95 static Lisp_Object
96 make_sub_char_table (depth, min_char, defalt)
97 int depth, min_char;
98 Lisp_Object defalt;
100 Lisp_Object table;
101 int size = VECSIZE (struct Lisp_Sub_Char_Table) - 1 + chartab_size[depth];
103 table = Fmake_vector (make_number (size), defalt);
104 XSETPVECTYPE (XVECTOR (table), PVEC_SUB_CHAR_TABLE);
105 XSUB_CHAR_TABLE (table)->depth = make_number (depth);
106 XSUB_CHAR_TABLE (table)->min_char = make_number (min_char);
108 return table;
111 static Lisp_Object
112 char_table_ascii (table)
113 Lisp_Object table;
115 Lisp_Object sub;
117 sub = XCHAR_TABLE (table)->contents[0];
118 if (! SUB_CHAR_TABLE_P (sub))
119 return sub;
120 sub = XSUB_CHAR_TABLE (sub)->contents[0];
121 if (! SUB_CHAR_TABLE_P (sub))
122 return sub;
123 return XSUB_CHAR_TABLE (sub)->contents[0];
126 Lisp_Object
127 copy_sub_char_table (table)
128 Lisp_Object table;
130 Lisp_Object copy;
131 int depth = XINT (XSUB_CHAR_TABLE (table)->depth);
132 int min_char = XINT (XSUB_CHAR_TABLE (table)->min_char);
133 Lisp_Object val;
134 int i;
136 copy = make_sub_char_table (depth, min_char, Qnil);
137 /* Recursively copy any sub char-tables. */
138 for (i = 0; i < chartab_size[depth]; i++)
140 val = XSUB_CHAR_TABLE (table)->contents[i];
141 if (SUB_CHAR_TABLE_P (val))
142 XSUB_CHAR_TABLE (copy)->contents[i] = copy_sub_char_table (val);
143 else
144 XSUB_CHAR_TABLE (copy)->contents[i] = val;
147 return copy;
151 Lisp_Object
152 copy_char_table (table)
153 Lisp_Object table;
155 Lisp_Object copy;
156 int size = XCHAR_TABLE (table)->size & PSEUDOVECTOR_SIZE_MASK;
157 int i;
159 copy = Fmake_vector (make_number (size), Qnil);
160 XSETPVECTYPE (XVECTOR (copy), PVEC_CHAR_TABLE);
161 XCHAR_TABLE (copy)->defalt = XCHAR_TABLE (table)->defalt;
162 XCHAR_TABLE (copy)->parent = XCHAR_TABLE (table)->parent;
163 XCHAR_TABLE (copy)->purpose = XCHAR_TABLE (table)->purpose;
164 XCHAR_TABLE (copy)->ascii = XCHAR_TABLE (table)->ascii;
165 for (i = 0; i < chartab_size[0]; i++)
166 XCHAR_TABLE (copy)->contents[i]
167 = (SUB_CHAR_TABLE_P (XCHAR_TABLE (table)->contents[i])
168 ? copy_sub_char_table (XCHAR_TABLE (table)->contents[i])
169 : XCHAR_TABLE (table)->contents[i]);
170 if (SUB_CHAR_TABLE_P (XCHAR_TABLE (copy)->ascii))
171 XCHAR_TABLE (copy)->ascii = char_table_ascii (copy);
172 size -= VECSIZE (struct Lisp_Char_Table) - 1;
173 for (i = 0; i < size; i++)
174 XCHAR_TABLE (copy)->extras[i] = XCHAR_TABLE (table)->extras[i];
176 XSETCHAR_TABLE (copy, XCHAR_TABLE (copy));
177 return copy;
180 Lisp_Object
181 sub_char_table_ref (table, c)
182 Lisp_Object table;
183 int c;
185 struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
186 int depth = XINT (tbl->depth);
187 int min_char = XINT (tbl->min_char);
188 Lisp_Object val;
190 val = tbl->contents[CHARTAB_IDX (c, depth, min_char)];
191 if (SUB_CHAR_TABLE_P (val))
192 val = sub_char_table_ref (val, c);
193 return val;
196 Lisp_Object
197 char_table_ref (table, c)
198 Lisp_Object table;
199 int c;
201 struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
202 Lisp_Object val;
204 if (ASCII_CHAR_P (c))
206 val = tbl->ascii;
207 if (SUB_CHAR_TABLE_P (val))
208 val = XSUB_CHAR_TABLE (val)->contents[c];
210 else
212 val = tbl->contents[CHARTAB_IDX (c, 0, 0)];
213 if (SUB_CHAR_TABLE_P (val))
214 val = sub_char_table_ref (val, c);
216 if (NILP (val))
218 val = tbl->defalt;
219 if (NILP (val) && CHAR_TABLE_P (tbl->parent))
220 val = char_table_ref (tbl->parent, c);
222 return val;
225 static Lisp_Object
226 sub_char_table_ref_and_range (table, c, from, to, defalt)
227 Lisp_Object table;
228 int c;
229 int *from, *to;
230 Lisp_Object defalt;
232 struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
233 int depth = XINT (tbl->depth);
234 int min_char = XINT (tbl->min_char);
235 int max_char = min_char + chartab_chars[depth - 1] - 1;
236 int index = CHARTAB_IDX (c, depth, min_char);
237 Lisp_Object val;
239 val = tbl->contents[index];
240 *from = min_char + index * chartab_chars[depth];
241 *to = *from + chartab_chars[depth] - 1;
242 if (SUB_CHAR_TABLE_P (val))
243 val = sub_char_table_ref_and_range (val, c, from, to, defalt);
244 else if (NILP (val))
245 val = defalt;
247 while (*from > min_char
248 && *from == min_char + index * chartab_chars[depth])
250 Lisp_Object this_val;
251 int this_from = *from - chartab_chars[depth];
252 int this_to = *from - 1;
254 index--;
255 this_val = tbl->contents[index];
256 if (SUB_CHAR_TABLE_P (this_val))
257 this_val = sub_char_table_ref_and_range (this_val, this_to,
258 &this_from, &this_to,
259 defalt);
260 else if (NILP (this_val))
261 this_val = defalt;
263 if (! EQ (this_val, val))
264 break;
265 *from = this_from;
267 index = CHARTAB_IDX (c, depth, min_char);
268 while (*to < max_char
269 && *to == min_char + (index + 1) * chartab_chars[depth] - 1)
271 Lisp_Object this_val;
272 int this_from = *to + 1;
273 int this_to = this_from + chartab_chars[depth] - 1;
275 index++;
276 this_val = tbl->contents[index];
277 if (SUB_CHAR_TABLE_P (this_val))
278 this_val = sub_char_table_ref_and_range (this_val, this_from,
279 &this_from, &this_to,
280 defalt);
281 else if (NILP (this_val))
282 this_val = defalt;
283 if (! EQ (this_val, val))
284 break;
285 *to = this_to;
288 return val;
292 /* Return the value for C in char-table TABLE. Set *FROM and *TO to
293 the range of characters (containing C) that have the same value as
294 C. It is not assured that the value of (*FROM - 1) and (*TO + 1)
295 is different from that of C. */
297 Lisp_Object
298 char_table_ref_and_range (table, c, from, to)
299 Lisp_Object table;
300 int c;
301 int *from, *to;
303 struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
304 int index = CHARTAB_IDX (c, 0, 0);
305 Lisp_Object val;
307 val = tbl->contents[index];
308 *from = index * chartab_chars[0];
309 *to = *from + chartab_chars[0] - 1;
310 if (SUB_CHAR_TABLE_P (val))
311 val = sub_char_table_ref_and_range (val, c, from, to, tbl->defalt);
312 else if (NILP (val))
313 val = tbl->defalt;
315 while (*from > 0 && *from == index * chartab_chars[0])
317 Lisp_Object this_val;
318 int this_from = *from - chartab_chars[0];
319 int this_to = *from - 1;
321 index--;
322 this_val = tbl->contents[index];
323 if (SUB_CHAR_TABLE_P (this_val))
324 this_val = sub_char_table_ref_and_range (this_val, this_to,
325 &this_from, &this_to,
326 tbl->defalt);
327 else if (NILP (this_val))
328 this_val = tbl->defalt;
330 if (! EQ (this_val, val))
331 break;
332 *from = this_from;
334 while (*to < MAX_CHAR && *to == (index + 1) * chartab_chars[0] - 1)
336 Lisp_Object this_val;
337 int this_from = *to + 1;
338 int this_to = this_from + chartab_chars[0] - 1;
340 index++;
341 this_val = tbl->contents[index];
342 if (SUB_CHAR_TABLE_P (this_val))
343 this_val = sub_char_table_ref_and_range (this_val, this_from,
344 &this_from, &this_to,
345 tbl->defalt);
346 else if (NILP (this_val))
347 this_val = tbl->defalt;
348 if (! EQ (this_val, val))
349 break;
350 *to = this_to;
353 return val;
357 #define ASET_RANGE(ARRAY, FROM, TO, LIMIT, VAL) \
358 do { \
359 int limit = (TO) < (LIMIT) ? (TO) : (LIMIT); \
360 for (; (FROM) < limit; (FROM)++) (ARRAY)->contents[(FROM)] = (VAL); \
361 } while (0)
363 #define GET_SUB_CHAR_TABLE(TABLE, SUBTABLE, IDX, DEPTH, MIN_CHAR) \
364 do { \
365 (SUBTABLE) = (TABLE)->contents[(IDX)]; \
366 if (!SUB_CHAR_TABLE_P (SUBTABLE)) \
367 (SUBTABLE) = make_sub_char_table ((DEPTH), (MIN_CHAR), (SUBTABLE)); \
368 } while (0)
371 static void
372 sub_char_table_set (table, c, val)
373 Lisp_Object table;
374 int c;
375 Lisp_Object val;
377 struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
378 int depth = XINT ((tbl)->depth);
379 int min_char = XINT ((tbl)->min_char);
380 int i = CHARTAB_IDX (c, depth, min_char);
381 Lisp_Object sub;
383 if (depth == 3)
384 tbl->contents[i] = val;
385 else
387 sub = tbl->contents[i];
388 if (! SUB_CHAR_TABLE_P (sub))
390 sub = make_sub_char_table (depth + 1,
391 min_char + i * chartab_chars[depth], sub);
392 tbl->contents[i] = sub;
394 sub_char_table_set (sub, c, val);
398 Lisp_Object
399 char_table_set (table, c, val)
400 Lisp_Object table;
401 int c;
402 Lisp_Object val;
404 struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
406 if (ASCII_CHAR_P (c)
407 && SUB_CHAR_TABLE_P (tbl->ascii))
409 XSUB_CHAR_TABLE (tbl->ascii)->contents[c] = val;
411 else
413 int i = CHARTAB_IDX (c, 0, 0);
414 Lisp_Object sub;
416 sub = tbl->contents[i];
417 if (! SUB_CHAR_TABLE_P (sub))
419 sub = make_sub_char_table (1, i * chartab_chars[0], sub);
420 tbl->contents[i] = sub;
422 sub_char_table_set (sub, c, val);
423 if (ASCII_CHAR_P (c))
424 tbl->ascii = char_table_ascii (table);
426 return val;
429 static void
430 sub_char_table_set_range (table, depth, min_char, from, to, val)
431 Lisp_Object *table;
432 int depth;
433 int min_char;
434 int from, to;
435 Lisp_Object val;
437 int max_char = min_char + chartab_chars[depth] - 1;
439 if (depth == 3 || (from <= min_char && to >= max_char))
440 *table = val;
441 else
443 int i, j;
445 depth++;
446 if (! SUB_CHAR_TABLE_P (*table))
447 *table = make_sub_char_table (depth, min_char, *table);
448 if (from < min_char)
449 from = min_char;
450 if (to > max_char)
451 to = max_char;
452 i = CHARTAB_IDX (from, depth, min_char);
453 j = CHARTAB_IDX (to, depth, min_char);
454 min_char += chartab_chars[depth] * i;
455 for (; i <= j; i++, min_char += chartab_chars[depth])
456 sub_char_table_set_range (XSUB_CHAR_TABLE (*table)->contents + i,
457 depth, min_char, from, to, val);
462 Lisp_Object
463 char_table_set_range (table, from, to, val)
464 Lisp_Object table;
465 int from, to;
466 Lisp_Object val;
468 struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
469 Lisp_Object *contents = tbl->contents;
470 int i, min_char;
472 if (from == to)
473 char_table_set (table, from, val);
474 else
476 for (i = CHARTAB_IDX (from, 0, 0), min_char = i * chartab_chars[0];
477 min_char <= to;
478 i++, min_char += chartab_chars[0])
479 sub_char_table_set_range (contents + i, 0, min_char, from, to, val);
480 if (ASCII_CHAR_P (from))
481 tbl->ascii = char_table_ascii (table);
483 return val;
487 DEFUN ("char-table-subtype", Fchar_table_subtype, Schar_table_subtype,
488 1, 1, 0,
489 doc: /*
490 Return the subtype of char-table CHAR-TABLE. The value is a symbol. */)
491 (char_table)
492 Lisp_Object char_table;
494 CHECK_CHAR_TABLE (char_table);
496 return XCHAR_TABLE (char_table)->purpose;
499 DEFUN ("char-table-parent", Fchar_table_parent, Schar_table_parent,
500 1, 1, 0,
501 doc: /* Return the parent char-table of CHAR-TABLE.
502 The value is either nil or another char-table.
503 If CHAR-TABLE holds nil for a given character,
504 then the actual applicable value is inherited from the parent char-table
505 \(or from its parents, if necessary). */)
506 (char_table)
507 Lisp_Object char_table;
509 CHECK_CHAR_TABLE (char_table);
511 return XCHAR_TABLE (char_table)->parent;
514 DEFUN ("set-char-table-parent", Fset_char_table_parent, Sset_char_table_parent,
515 2, 2, 0,
516 doc: /* Set the parent char-table of CHAR-TABLE to PARENT.
517 Return PARENT. PARENT must be either nil or another char-table. */)
518 (char_table, parent)
519 Lisp_Object char_table, parent;
521 Lisp_Object temp;
523 CHECK_CHAR_TABLE (char_table);
525 if (!NILP (parent))
527 CHECK_CHAR_TABLE (parent);
529 for (temp = parent; !NILP (temp); temp = XCHAR_TABLE (temp)->parent)
530 if (EQ (temp, char_table))
531 error ("Attempt to make a chartable be its own parent");
534 XCHAR_TABLE (char_table)->parent = parent;
536 return parent;
539 DEFUN ("char-table-extra-slot", Fchar_table_extra_slot, Schar_table_extra_slot,
540 2, 2, 0,
541 doc: /* Return the value of CHAR-TABLE's extra-slot number N. */)
542 (char_table, n)
543 Lisp_Object char_table, n;
545 CHECK_CHAR_TABLE (char_table);
546 CHECK_NUMBER (n);
547 if (XINT (n) < 0
548 || XINT (n) >= CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (char_table)))
549 args_out_of_range (char_table, n);
551 return XCHAR_TABLE (char_table)->extras[XINT (n)];
554 DEFUN ("set-char-table-extra-slot", Fset_char_table_extra_slot,
555 Sset_char_table_extra_slot,
556 3, 3, 0,
557 doc: /* Set CHAR-TABLE's extra-slot number N to VALUE. */)
558 (char_table, n, value)
559 Lisp_Object char_table, n, value;
561 CHECK_CHAR_TABLE (char_table);
562 CHECK_NUMBER (n);
563 if (XINT (n) < 0
564 || XINT (n) >= CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (char_table)))
565 args_out_of_range (char_table, n);
567 return XCHAR_TABLE (char_table)->extras[XINT (n)] = value;
570 DEFUN ("char-table-range", Fchar_table_range, Schar_table_range,
571 2, 2, 0,
572 doc: /* Return the value in CHAR-TABLE for a range of characters RANGE.
573 RANGE should be nil (for the default value),
574 a cons of character codes (for characters in the range), or a character code. */)
575 (char_table, range)
576 Lisp_Object char_table, range;
578 Lisp_Object val;
579 CHECK_CHAR_TABLE (char_table);
581 if (EQ (range, Qnil))
582 val = XCHAR_TABLE (char_table)->defalt;
583 else if (INTEGERP (range))
584 val = CHAR_TABLE_REF (char_table, XINT (range));
585 else if (CONSP (range))
587 int from, to;
589 CHECK_CHARACTER_CAR (range);
590 CHECK_CHARACTER_CDR (range);
591 val = char_table_ref_and_range (char_table, XINT (XCAR (range)),
592 &from, &to);
593 /* Not yet implemented. */
595 else
596 error ("Invalid RANGE argument to `char-table-range'");
597 return val;
600 DEFUN ("set-char-table-range", Fset_char_table_range, Sset_char_table_range,
601 3, 3, 0,
602 doc: /* Set the value in CHAR-TABLE for a range of characters RANGE to VALUE.
603 RANGE should be t (for all characters), nil (for the default value),
604 a cons of character codes (for characters in the range),
605 or a character code. Return VALUE. */)
606 (char_table, range, value)
607 Lisp_Object char_table, range, value;
609 CHECK_CHAR_TABLE (char_table);
610 if (EQ (range, Qt))
612 int i;
614 XCHAR_TABLE (char_table)->ascii = Qnil;
615 for (i = 0; i < chartab_size[0]; i++)
616 XCHAR_TABLE (char_table)->contents[i] = Qnil;
617 XCHAR_TABLE (char_table)->defalt = value;
619 else if (EQ (range, Qnil))
620 XCHAR_TABLE (char_table)->defalt = value;
621 else if (INTEGERP (range))
622 char_table_set (char_table, XINT (range), value);
623 else if (CONSP (range))
625 CHECK_CHARACTER_CAR (range);
626 CHECK_CHARACTER_CDR (range);
627 char_table_set_range (char_table,
628 XINT (XCAR (range)), XINT (XCDR (range)), value);
630 else
631 error ("Invalid RANGE argument to `set-char-table-range'");
633 return value;
636 DEFUN ("set-char-table-default", Fset_char_table_default,
637 Sset_char_table_default, 3, 3, 0,
638 doc: /*
639 This function is obsolete and has no effect. */)
640 (char_table, ch, value)
641 Lisp_Object char_table, ch, value;
643 return Qnil;
646 /* Look up the element in TABLE at index CH, and return it as an
647 integer. If the element is not a character, return CH itself. */
650 char_table_translate (table, ch)
651 Lisp_Object table;
652 int ch;
654 Lisp_Object value;
655 value = Faref (table, make_number (ch));
656 if (! CHARACTERP (value))
657 return ch;
658 return XINT (value);
661 static Lisp_Object
662 optimize_sub_char_table (table)
663 Lisp_Object table;
665 struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
666 int depth = XINT (tbl->depth);
667 Lisp_Object elt, this;
668 int i;
670 elt = XSUB_CHAR_TABLE (table)->contents[0];
671 if (SUB_CHAR_TABLE_P (elt))
672 elt = XSUB_CHAR_TABLE (table)->contents[0] = optimize_sub_char_table (elt);
673 if (SUB_CHAR_TABLE_P (elt))
674 return table;
675 for (i = 1; i < chartab_size[depth]; i++)
677 this = XSUB_CHAR_TABLE (table)->contents[i];
678 if (SUB_CHAR_TABLE_P (this))
679 this = XSUB_CHAR_TABLE (table)->contents[i]
680 = optimize_sub_char_table (this);
681 if (SUB_CHAR_TABLE_P (this)
682 || NILP (Fequal (this, elt)))
683 break;
686 return (i < chartab_size[depth] ? table : elt);
689 DEFUN ("optimize-char-table", Foptimize_char_table, Soptimize_char_table,
690 1, 1, 0,
691 doc: /* Optimize CHAR-TABLE. */)
692 (char_table)
693 Lisp_Object char_table;
695 Lisp_Object elt;
696 int i;
698 CHECK_CHAR_TABLE (char_table);
700 for (i = 0; i < chartab_size[0]; i++)
702 elt = XCHAR_TABLE (char_table)->contents[i];
703 if (SUB_CHAR_TABLE_P (elt))
704 XCHAR_TABLE (char_table)->contents[i] = optimize_sub_char_table (elt);
706 return Qnil;
710 /* Map C_FUNCTION or FUNCTION over TABLE (top or sub char-table),
711 calling it for each character or group of characters that share a
712 value. RANGE is a cons (FROM . TO) specifying the range of target
713 characters, VAL is a value of FROM in TABLE, DEFAULT_VAL is the
714 default value of the char-table, PARENT is the parent of the
715 char-table.
717 ARG is passed to C_FUNCTION when that is called.
719 It returns the value of last character covered by TABLE (not the
720 value inheritted from the parent), and by side-effect, the car part
721 of RANGE is updated to the minimum character C where C and all the
722 following characters in TABLE have the same value. */
724 static Lisp_Object
725 map_sub_char_table (c_function, function, table, arg, val, range,
726 default_val, parent)
727 void (*c_function) P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
728 Lisp_Object function, table, arg, val, range, default_val, parent;
730 /* Pointer to the elements of TABLE. */
731 Lisp_Object *contents;
732 /* Depth of TABLE. */
733 int depth;
734 /* Minimum and maxinum characters covered by TABLE. */
735 int min_char, max_char;
736 /* Number of characters covered by one element of TABLE. */
737 int chars_in_block;
738 int from = XINT (XCAR (range)), to = XINT (XCDR (range));
739 int i, c;
741 if (SUB_CHAR_TABLE_P (table))
743 struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
745 depth = XINT (tbl->depth);
746 contents = tbl->contents;
747 min_char = XINT (tbl->min_char);
748 max_char = min_char + chartab_chars[depth - 1] - 1;
750 else
752 depth = 0;
753 contents = XCHAR_TABLE (table)->contents;
754 min_char = 0;
755 max_char = MAX_CHAR;
757 chars_in_block = chartab_chars[depth];
759 if (to < max_char)
760 max_char = to;
761 /* Set I to the index of the first element to check. */
762 if (from <= min_char)
763 i = 0;
764 else
765 i = (from - min_char) / chars_in_block;
766 for (c = min_char + chars_in_block * i; c <= max_char;
767 i++, c += chars_in_block)
769 Lisp_Object this = contents[i];
770 int nextc = c + chars_in_block;
772 if (SUB_CHAR_TABLE_P (this))
774 if (to >= nextc)
775 XSETCDR (range, make_number (nextc - 1));
776 val = map_sub_char_table (c_function, function, this, arg,
777 val, range, default_val, parent);
779 else
781 if (NILP (this))
782 this = default_val;
783 if (NILP (Fequal (val, this)))
785 int different_value = 1;
787 if (NILP (val))
789 if (! NILP (parent))
791 Lisp_Object temp = XCHAR_TABLE (parent)->parent;
793 /* This is to get a value of FROM in PARENT
794 without checking the parent of PARENT. */
795 XCHAR_TABLE (parent)->parent = Qnil;
796 val = CHAR_TABLE_REF (parent, from);
797 XCHAR_TABLE (parent)->parent = temp;
798 XSETCDR (range, make_number (c - 1));
799 val = map_sub_char_table (c_function, function,
800 parent, arg, val, range,
801 XCHAR_TABLE (parent)->defalt,
802 XCHAR_TABLE (parent)->parent);
803 if (! NILP (Fequal (val, this)))
804 different_value = 0;
807 if (! NILP (val) && different_value)
809 XSETCDR (range, make_number (c - 1));
810 if (EQ (XCAR (range), XCDR (range)))
812 if (c_function)
813 (*c_function) (arg, XCAR (range), val);
814 else
815 call2 (function, XCAR (range), val);
817 else
819 if (c_function)
820 (*c_function) (arg, range, val);
821 else
822 call2 (function, range, val);
825 val = this;
826 from = c;
827 XSETCAR (range, make_number (c));
830 XSETCDR (range, make_number (to));
832 return val;
836 /* Map C_FUNCTION or FUNCTION over TABLE, calling it for each
837 character or group of characters that share a value.
839 ARG is passed to C_FUNCTION when that is called. */
841 void
842 map_char_table (c_function, function, table, arg)
843 void (*c_function) P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
844 Lisp_Object function, table, arg;
846 Lisp_Object range, val;
847 int c, i;
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 void
982 map_char_table_for_charset (c_function, function, table, arg,
983 charset, from, to)
984 void (*c_function) P_ ((Lisp_Object, Lisp_Object));
985 Lisp_Object function, table, arg;
986 struct charset *charset;
987 unsigned from, to;
989 Lisp_Object range;
990 int c, i;
991 struct gcpro gcpro1;
993 range = Fcons (Qnil, Qnil);
994 GCPRO1 (range);
996 for (i = 0, c = 0; i < chartab_size[0]; i++, c += chartab_chars[0])
998 Lisp_Object this;
1000 this = XCHAR_TABLE (table)->contents[i];
1001 if (SUB_CHAR_TABLE_P (this))
1002 map_sub_char_table_for_charset (c_function, function, this, arg,
1003 range, charset, from, to);
1004 else
1006 if (! NILP (XCAR (range)))
1008 XSETCDR (range, make_number (c - 1));
1009 if (c_function)
1010 (*c_function) (arg, range);
1011 else
1012 call2 (function, range, arg);
1014 XSETCAR (range, Qnil);
1017 if (! NILP (XCAR (range)))
1019 XSETCDR (range, make_number (c - 1));
1020 if (c_function)
1021 (*c_function) (arg, range);
1022 else
1023 call2 (function, range, arg);
1026 UNGCPRO;
1030 void
1031 syms_of_chartab ()
1033 defsubr (&Smake_char_table);
1034 defsubr (&Schar_table_parent);
1035 defsubr (&Schar_table_subtype);
1036 defsubr (&Sset_char_table_parent);
1037 defsubr (&Schar_table_extra_slot);
1038 defsubr (&Sset_char_table_extra_slot);
1039 defsubr (&Schar_table_range);
1040 defsubr (&Sset_char_table_range);
1041 defsubr (&Sset_char_table_default);
1042 defsubr (&Soptimize_char_table);
1043 defsubr (&Smap_char_table);
1046 /* arch-tag: 18b5b560-7ab5-4108-b09e-d5dd65dc6fda
1047 (do not change this comment) */