Merge from emacs--rel--22
[bpt/emacs.git] / src / chartab.c
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
5
6 This file is part of GNU Emacs.
7
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.
12
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.
17
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. */
22
23 #include <config.h>
24 #include "lisp.h"
25 #include "character.h"
26 #include "charset.h"
27 #include "ccl.h"
28
29 /* 64/16/32/128 */
30
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) };
37
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 };
45
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 };
53
54 #define CHARTAB_IDX(c, depth, min_char) \
55 (((c) - (min_char)) >> chartab_bits[(depth)])
56
57
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.
61
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;
68 {
69 Lisp_Object vector;
70 Lisp_Object n;
71 int n_extras;
72 int size;
73
74 CHECK_SYMBOL (purpose);
75 n = Fget (purpose, Qchar_table_extra_slots);
76 if (NILP (n))
77 n_extras = 0;
78 else
79 {
80 CHECK_NATNUM (n);
81 n_extras = XINT (n);
82 if (n_extras > 10)
83 args_out_of_range (n, Qnil);
84 }
85
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;
93 }
94
95 static Lisp_Object
96 make_sub_char_table (depth, min_char, defalt)
97 int depth, min_char;
98 Lisp_Object defalt;
99 {
100 Lisp_Object table;
101 int size = VECSIZE (struct Lisp_Sub_Char_Table) - 1 + chartab_size[depth];
102
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);
107
108 return table;
109 }
110
111 static Lisp_Object
112 char_table_ascii (table)
113 Lisp_Object table;
114 {
115 Lisp_Object sub;
116
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];
124 }
125
126 Lisp_Object
127 copy_sub_char_table (table)
128 Lisp_Object table;
129 {
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;
135
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++)
139 {
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;
145 }
146
147 return copy;
148 }
149
150
151 Lisp_Object
152 copy_char_table (table)
153 Lisp_Object table;
154 {
155 Lisp_Object copy;
156 int size = XCHAR_TABLE (table)->size & PSEUDOVECTOR_SIZE_MASK;
157 int i;
158
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];
175
176 XSETCHAR_TABLE (copy, XCHAR_TABLE (copy));
177 return copy;
178 }
179
180 Lisp_Object
181 sub_char_table_ref (table, c)
182 Lisp_Object table;
183 int c;
184 {
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;
189
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;
194 }
195
196 Lisp_Object
197 char_table_ref (table, c)
198 Lisp_Object table;
199 int c;
200 {
201 struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
202 Lisp_Object val;
203
204 if (ASCII_CHAR_P (c))
205 {
206 val = tbl->ascii;
207 if (SUB_CHAR_TABLE_P (val))
208 val = XSUB_CHAR_TABLE (val)->contents[c];
209 }
210 else
211 {
212 val = tbl->contents[CHARTAB_IDX (c, 0, 0)];
213 if (SUB_CHAR_TABLE_P (val))
214 val = sub_char_table_ref (val, c);
215 }
216 if (NILP (val))
217 {
218 val = tbl->defalt;
219 if (NILP (val) && CHAR_TABLE_P (tbl->parent))
220 val = char_table_ref (tbl->parent, c);
221 }
222 return val;
223 }
224
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;
231 {
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;
238
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;
246
247 while (*from > min_char
248 && *from == min_char + index * chartab_chars[depth])
249 {
250 Lisp_Object this_val;
251 int this_from = *from - chartab_chars[depth];
252 int this_to = *from - 1;
253
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;
262
263 if (! EQ (this_val, val))
264 break;
265 *from = this_from;
266 }
267 index = CHARTAB_IDX (c, depth, min_char);
268 while (*to < max_char
269 && *to == min_char + (index + 1) * chartab_chars[depth] - 1)
270 {
271 Lisp_Object this_val;
272 int this_from = *to + 1;
273 int this_to = this_from + chartab_chars[depth] - 1;
274
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;
286 }
287
288 return val;
289 }
290
291
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. */
296
297 Lisp_Object
298 char_table_ref_and_range (table, c, from, to)
299 Lisp_Object table;
300 int c;
301 int *from, *to;
302 {
303 struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
304 int index = CHARTAB_IDX (c, 0, 0);
305 Lisp_Object val;
306
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;
314
315 while (*from > 0 && *from == index * chartab_chars[0])
316 {
317 Lisp_Object this_val;
318 int this_from = *from - chartab_chars[0];
319 int this_to = *from - 1;
320
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;
329
330 if (! EQ (this_val, val))
331 break;
332 *from = this_from;
333 }
334 while (*to < MAX_CHAR && *to == (index + 1) * chartab_chars[0] - 1)
335 {
336 Lisp_Object this_val;
337 int this_from = *to + 1;
338 int this_to = this_from + chartab_chars[0] - 1;
339
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;
351 }
352
353 return val;
354 }
355
356
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)
362
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)
369
370
371 static void
372 sub_char_table_set (table, c, val)
373 Lisp_Object table;
374 int c;
375 Lisp_Object val;
376 {
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;
382
383 if (depth == 3)
384 tbl->contents[i] = val;
385 else
386 {
387 sub = tbl->contents[i];
388 if (! SUB_CHAR_TABLE_P (sub))
389 {
390 sub = make_sub_char_table (depth + 1,
391 min_char + i * chartab_chars[depth], sub);
392 tbl->contents[i] = sub;
393 }
394 sub_char_table_set (sub, c, val);
395 }
396 }
397
398 Lisp_Object
399 char_table_set (table, c, val)
400 Lisp_Object table;
401 int c;
402 Lisp_Object val;
403 {
404 struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
405
406 if (ASCII_CHAR_P (c)
407 && SUB_CHAR_TABLE_P (tbl->ascii))
408 {
409 XSUB_CHAR_TABLE (tbl->ascii)->contents[c] = val;
410 }
411 else
412 {
413 int i = CHARTAB_IDX (c, 0, 0);
414 Lisp_Object sub;
415
416 sub = tbl->contents[i];
417 if (! SUB_CHAR_TABLE_P (sub))
418 {
419 sub = make_sub_char_table (1, i * chartab_chars[0], sub);
420 tbl->contents[i] = sub;
421 }
422 sub_char_table_set (sub, c, val);
423 if (ASCII_CHAR_P (c))
424 tbl->ascii = char_table_ascii (table);
425 }
426 return val;
427 }
428
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;
436 {
437 int max_char = min_char + chartab_chars[depth] - 1;
438
439 if (depth == 3 || (from <= min_char && to >= max_char))
440 *table = val;
441 else
442 {
443 int i, j;
444
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);
458 }
459 }
460
461
462 Lisp_Object
463 char_table_set_range (table, from, to, val)
464 Lisp_Object table;
465 int from, to;
466 Lisp_Object val;
467 {
468 struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
469 Lisp_Object *contents = tbl->contents;
470 int i, min_char;
471
472 if (from == to)
473 char_table_set (table, from, val);
474 else
475 {
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);
482 }
483 return val;
484 }
485
486 \f
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;
493 {
494 CHECK_CHAR_TABLE (char_table);
495
496 return XCHAR_TABLE (char_table)->purpose;
497 }
498
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;
508 {
509 CHECK_CHAR_TABLE (char_table);
510
511 return XCHAR_TABLE (char_table)->parent;
512 }
513
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;
520 {
521 Lisp_Object temp;
522
523 CHECK_CHAR_TABLE (char_table);
524
525 if (!NILP (parent))
526 {
527 CHECK_CHAR_TABLE (parent);
528
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");
532 }
533
534 XCHAR_TABLE (char_table)->parent = parent;
535
536 return parent;
537 }
538
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;
544 {
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);
550
551 return XCHAR_TABLE (char_table)->extras[XINT (n)];
552 }
553
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;
560 {
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);
566
567 return XCHAR_TABLE (char_table)->extras[XINT (n)] = value;
568 }
569 \f
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;
577 {
578 Lisp_Object val;
579 CHECK_CHAR_TABLE (char_table);
580
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))
586 {
587 int from, to;
588
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. */
594 }
595 else
596 error ("Invalid RANGE argument to `char-table-range'");
597 return val;
598 }
599
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;
608 {
609 CHECK_CHAR_TABLE (char_table);
610 if (EQ (range, Qt))
611 {
612 int i;
613
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;
618 }
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))
624 {
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);
629 }
630 else
631 error ("Invalid RANGE argument to `set-char-table-range'");
632
633 return value;
634 }
635
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;
642 {
643 return Qnil;
644 }
645
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. */
648
649 int
650 char_table_translate (table, ch)
651 Lisp_Object table;
652 int ch;
653 {
654 Lisp_Object value;
655 value = Faref (table, make_number (ch));
656 if (! CHARACTERP (value))
657 return ch;
658 return XINT (value);
659 }
660
661 static Lisp_Object
662 optimize_sub_char_table (table)
663 Lisp_Object table;
664 {
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;
669
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++)
676 {
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;
684 }
685
686 return (i < chartab_size[depth] ? table : elt);
687 }
688
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;
694 {
695 Lisp_Object elt;
696 int i;
697
698 CHECK_CHAR_TABLE (char_table);
699
700 for (i = 0; i < chartab_size[0]; i++)
701 {
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);
705 }
706 return Qnil;
707 }
708
709 \f
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.
716
717 ARG is passed to C_FUNCTION when that is called.
718
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. */
723
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;
729 {
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;
740
741 if (SUB_CHAR_TABLE_P (table))
742 {
743 struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
744
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;
749 }
750 else
751 {
752 depth = 0;
753 contents = XCHAR_TABLE (table)->contents;
754 min_char = 0;
755 max_char = MAX_CHAR;
756 }
757 chars_in_block = chartab_chars[depth];
758
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)
768 {
769 Lisp_Object this = contents[i];
770 int nextc = c + chars_in_block;
771
772 if (SUB_CHAR_TABLE_P (this))
773 {
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);
778 }
779 else
780 {
781 if (NILP (this))
782 this = default_val;
783 if (NILP (Fequal (val, this)))
784 {
785 int different_value = 1;
786
787 if (NILP (val))
788 {
789 if (! NILP (parent))
790 {
791 Lisp_Object temp = XCHAR_TABLE (parent)->parent;
792
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;
805 }
806 }
807 if (! NILP (val) && different_value)
808 {
809 XSETCDR (range, make_number (c - 1));
810 if (EQ (XCAR (range), XCDR (range)))
811 {
812 if (c_function)
813 (*c_function) (arg, XCAR (range), val);
814 else
815 call2 (function, XCAR (range), val);
816 }
817 else
818 {
819 if (c_function)
820 (*c_function) (arg, range, val);
821 else
822 call2 (function, range, val);
823 }
824 }
825 val = this;
826 from = c;
827 XSETCAR (range, make_number (c));
828 }
829 }
830 XSETCDR (range, make_number (to));
831 }
832 return val;
833 }
834
835
836 /* Map C_FUNCTION or FUNCTION over TABLE, calling it for each
837 character or group of characters that share a value.
838
839 ARG is passed to C_FUNCTION when that is called. */
840
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;
845 {
846 Lisp_Object range, val;
847 int c, i;
848 struct gcpro gcpro1, gcpro2, gcpro3;
849
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))
861 {
862 Lisp_Object parent = XCHAR_TABLE (table)->parent;
863 Lisp_Object temp = XCHAR_TABLE (parent)->parent;
864 int from = XINT (XCAR (range));
865
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;
875 }
876
877 if (! NILP (val))
878 {
879 if (EQ (XCAR (range), XCDR (range)))
880 {
881 if (c_function)
882 (*c_function) (arg, XCAR (range), val);
883 else
884 call2 (function, XCAR (range), val);
885 }
886 else
887 {
888 if (c_function)
889 (*c_function) (arg, range, val);
890 else
891 call2 (function, range, val);
892 }
893 }
894
895 UNGCPRO;
896 }
897
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;
907 {
908 CHECK_CHAR_TABLE (char_table);
909
910 map_char_table (NULL, function, char_table, char_table);
911 return Qnil;
912 }
913
914
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;
922 {
923 struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
924 int depth = XINT (tbl->depth);
925 int c, i;
926
927 if (depth < 3)
928 for (i = 0, c = XINT (tbl->min_char); i < chartab_size[depth];
929 i++, c += chartab_chars[depth])
930 {
931 Lisp_Object this;
932
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
938 {
939 if (! NILP (XCAR (range)))
940 {
941 XSETCDR (range, make_number (c - 1));
942 if (c_function)
943 (*c_function) (arg, range);
944 else
945 call2 (function, range, arg);
946 }
947 XSETCAR (range, Qnil);
948 }
949 }
950 else
951 for (i = 0, c = XINT (tbl->min_char); i < chartab_size[depth]; i++, c ++)
952 {
953 Lisp_Object this;
954 unsigned code;
955
956 this = tbl->contents[i];
957 if (NILP (this)
958 || (charset
959 && (code = ENCODE_CHAR (charset, c),
960 (code < from || code > to))))
961 {
962 if (! NILP (XCAR (range)))
963 {
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);
970 }
971 }
972 else
973 {
974 if (NILP (XCAR (range)))
975 XSETCAR (range, make_number (c));
976 }
977 }
978 }
979
980
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;
988 {
989 Lisp_Object range;
990 int c, i;
991 struct gcpro gcpro1;
992
993 range = Fcons (Qnil, Qnil);
994 GCPRO1 (range);
995
996 for (i = 0, c = 0; i < chartab_size[0]; i++, c += chartab_chars[0])
997 {
998 Lisp_Object this;
999
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
1005 {
1006 if (! NILP (XCAR (range)))
1007 {
1008 XSETCDR (range, make_number (c - 1));
1009 if (c_function)
1010 (*c_function) (arg, range);
1011 else
1012 call2 (function, range, arg);
1013 }
1014 XSETCAR (range, Qnil);
1015 }
1016 }
1017 if (! NILP (XCAR (range)))
1018 {
1019 XSETCDR (range, make_number (c - 1));
1020 if (c_function)
1021 (*c_function) (arg, range);
1022 else
1023 call2 (function, range, arg);
1024 }
1025
1026 UNGCPRO;
1027 }
1028
1029 \f
1030 void
1031 syms_of_chartab ()
1032 {
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);
1044 }
1045
1046 /* arch-tag: 18b5b560-7ab5-4108-b09e-d5dd65dc6fda
1047 (do not change this comment) */