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)
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. */
25 #include "character.h"
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
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
),
46 /* Number of characters (in bits) each element of Nth level char-table
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
),
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. */)
67 register Lisp_Object purpose
, init
;
74 CHECK_SYMBOL (purpose
);
75 n
= Fget (purpose
, Qchar_table_extra_slots
);
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
));
96 make_sub_char_table (depth
, min_char
, defalt
)
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
);
112 char_table_ascii (table
)
117 sub
= XCHAR_TABLE (table
)->contents
[0];
118 if (! SUB_CHAR_TABLE_P (sub
))
120 sub
= XSUB_CHAR_TABLE (sub
)->contents
[0];
121 if (! SUB_CHAR_TABLE_P (sub
))
123 return XSUB_CHAR_TABLE (sub
)->contents
[0];
127 copy_sub_char_table (table
)
131 int depth
= XINT (XSUB_CHAR_TABLE (table
)->depth
);
132 int min_char
= XINT (XSUB_CHAR_TABLE (table
)->min_char
);
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
);
144 XSUB_CHAR_TABLE (copy
)->contents
[i
] = val
;
152 copy_char_table (table
)
156 int size
= XCHAR_TABLE (table
)->size
& PSEUDOVECTOR_SIZE_MASK
;
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
));
181 sub_char_table_ref (table
, 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
);
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
);
197 char_table_ref (table
, c
)
201 struct Lisp_Char_Table
*tbl
= XCHAR_TABLE (table
);
204 if (ASCII_CHAR_P (c
))
207 if (SUB_CHAR_TABLE_P (val
))
208 val
= XSUB_CHAR_TABLE (val
)->contents
[c
];
212 val
= tbl
->contents
[CHARTAB_IDX (c
, 0, 0)];
213 if (SUB_CHAR_TABLE_P (val
))
214 val
= sub_char_table_ref (val
, c
);
219 if (NILP (val
) && CHAR_TABLE_P (tbl
->parent
))
220 val
= char_table_ref (tbl
->parent
, c
);
226 sub_char_table_ref_and_range (table
, c
, from
, to
, 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
);
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
);
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;
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
,
260 else if (NILP (this_val
))
263 if (! EQ (this_val
, val
))
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;
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
,
281 else if (NILP (this_val
))
283 if (! EQ (this_val
, 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. */
298 char_table_ref_and_range (table
, c
, from
, to
)
303 struct Lisp_Char_Table
*tbl
= XCHAR_TABLE (table
);
304 int index
= CHARTAB_IDX (c
, 0, 0);
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
);
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;
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
,
327 else if (NILP (this_val
))
328 this_val
= tbl
->defalt
;
330 if (! EQ (this_val
, val
))
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;
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
,
346 else if (NILP (this_val
))
347 this_val
= tbl
->defalt
;
348 if (! EQ (this_val
, val
))
357 #define ASET_RANGE(ARRAY, FROM, TO, LIMIT, VAL) \
359 int limit = (TO) < (LIMIT) ? (TO) : (LIMIT); \
360 for (; (FROM) < limit; (FROM)++) (ARRAY)->contents[(FROM)] = (VAL); \
363 #define GET_SUB_CHAR_TABLE(TABLE, SUBTABLE, IDX, DEPTH, MIN_CHAR) \
365 (SUBTABLE) = (TABLE)->contents[(IDX)]; \
366 if (!SUB_CHAR_TABLE_P (SUBTABLE)) \
367 (SUBTABLE) = make_sub_char_table ((DEPTH), (MIN_CHAR), (SUBTABLE)); \
372 sub_char_table_set (table
, c
, 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
);
384 tbl
->contents
[i
] = val
;
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
);
399 char_table_set (table
, c
, val
)
404 struct Lisp_Char_Table
*tbl
= XCHAR_TABLE (table
);
407 && SUB_CHAR_TABLE_P (tbl
->ascii
))
409 XSUB_CHAR_TABLE (tbl
->ascii
)->contents
[c
] = val
;
413 int i
= CHARTAB_IDX (c
, 0, 0);
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
);
430 sub_char_table_set_range (table
, depth
, min_char
, from
, to
, val
)
437 int max_char
= min_char
+ chartab_chars
[depth
] - 1;
439 if (depth
== 3 || (from
<= min_char
&& to
>= max_char
))
446 if (! SUB_CHAR_TABLE_P (*table
))
447 *table
= make_sub_char_table (depth
, min_char
, *table
);
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
);
463 char_table_set_range (table
, from
, to
, val
)
468 struct Lisp_Char_Table
*tbl
= XCHAR_TABLE (table
);
469 Lisp_Object
*contents
= tbl
->contents
;
473 char_table_set (table
, from
, val
);
476 for (i
= CHARTAB_IDX (from
, 0, 0), min_char
= i
* chartab_chars
[0];
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
);
487 DEFUN ("char-table-subtype", Fchar_table_subtype
, Schar_table_subtype
,
490 Return the subtype of char-table CHAR-TABLE. The value is a symbol. */)
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
,
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). */)
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
,
516 doc
: /* Set the parent char-table of CHAR-TABLE to PARENT.
517 Return PARENT. PARENT must be either nil or another char-table. */)
519 Lisp_Object char_table
, parent
;
523 CHECK_CHAR_TABLE (char_table
);
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
;
539 DEFUN ("char-table-extra-slot", Fchar_table_extra_slot
, Schar_table_extra_slot
,
541 doc
: /* Return the value of CHAR-TABLE's extra-slot number N. */)
543 Lisp_Object char_table
, n
;
545 CHECK_CHAR_TABLE (char_table
);
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
,
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
);
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
,
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. */)
576 Lisp_Object char_table
, range
;
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
))
589 CHECK_CHARACTER_CAR (range
);
590 CHECK_CHARACTER_CDR (range
);
591 val
= char_table_ref_and_range (char_table
, XINT (XCAR (range
)),
593 /* Not yet implemented. */
596 error ("Invalid RANGE argument to `char-table-range'");
600 DEFUN ("set-char-table-range", Fset_char_table_range
, Sset_char_table_range
,
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
);
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
);
631 error ("Invalid RANGE argument to `set-char-table-range'");
636 DEFUN ("set-char-table-default", Fset_char_table_default
,
637 Sset_char_table_default
, 3, 3, 0,
639 This function is obsolete and has no effect. */)
640 (char_table
, ch
, value
)
641 Lisp_Object char_table
, ch
, value
;
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
)
655 value
= Faref (table
, make_number (ch
));
656 if (! CHARACTERP (value
))
662 optimize_sub_char_table (table
)
665 struct Lisp_Sub_Char_Table
*tbl
= XSUB_CHAR_TABLE (table
);
666 int depth
= XINT (tbl
->depth
);
667 Lisp_Object elt
, this;
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
))
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
)))
686 return (i
< chartab_size
[depth
] ? table
: elt
);
689 DEFUN ("optimize-char-table", Foptimize_char_table
, Soptimize_char_table
,
691 doc
: /* Optimize CHAR-TABLE. */)
693 Lisp_Object char_table
;
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
);
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
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. */
725 map_sub_char_table (c_function
, function
, table
, arg
, val
, range
,
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. */
734 /* Minimum and maxinum characters covered by TABLE. */
735 int min_char
, max_char
;
736 /* Number of characters covered by one element of TABLE. */
738 int from
= XINT (XCAR (range
)), to
= XINT (XCDR (range
));
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;
753 contents
= XCHAR_TABLE (table
)->contents
;
757 chars_in_block
= chartab_chars
[depth
];
761 /* Set I to the index of the first element to check. */
762 if (from
<= min_char
)
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))
775 XSETCDR (range
, make_number (nextc
- 1));
776 val
= map_sub_char_table (c_function
, function
, this, arg
,
777 val
, range
, default_val
, parent
);
783 if (NILP (Fequal (val
, this)))
785 int different_value
= 1;
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)))
807 if (! NILP (val
) && different_value
)
809 XSETCDR (range
, make_number (c
- 1));
810 if (EQ (XCAR (range
), XCDR (range
)))
813 (*c_function
) (arg
, XCAR (range
), val
);
815 call2 (function
, XCAR (range
), val
);
820 (*c_function
) (arg
, range
, val
);
822 call2 (function
, range
, val
);
827 XSETCAR (range
, make_number (c
));
830 XSETCDR (range
, make_number (to
));
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. */
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
;
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
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
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
);
879 if (EQ (XCAR (range
), XCDR (range
)))
882 (*c_function
) (arg
, XCAR (range
), val
);
884 call2 (function
, XCAR (range
), val
);
889 (*c_function
) (arg
, range
, val
);
891 call2 (function
, range
, val
);
898 DEFUN ("map-char-table", Fmap_char_table
, Smap_char_table
,
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
);
916 map_sub_char_table_for_charset (c_function
, function
, table
, arg
, range
,
918 void (*c_function
) P_ ((Lisp_Object
, Lisp_Object
));
919 Lisp_Object function
, table
, arg
, range
;
920 struct charset
*charset
;
923 struct Lisp_Sub_Char_Table
*tbl
= XSUB_CHAR_TABLE (table
);
924 int depth
= XINT (tbl
->depth
);
928 for (i
= 0, c
= XINT (tbl
->min_char
); i
< chartab_size
[depth
];
929 i
++, c
+= chartab_chars
[depth
])
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
);
939 if (! NILP (XCAR (range
)))
941 XSETCDR (range
, make_number (c
- 1));
943 (*c_function
) (arg
, range
);
945 call2 (function
, range
, arg
);
947 XSETCAR (range
, Qnil
);
951 for (i
= 0, c
= XINT (tbl
->min_char
); i
< chartab_size
[depth
]; i
++, c
++)
956 this = tbl
->contents
[i
];
959 && (code
= ENCODE_CHAR (charset
, c
),
960 (code
< from
|| code
> to
))))
962 if (! NILP (XCAR (range
)))
964 XSETCDR (range
, make_number (c
- 1));
966 (*c_function
) (arg
, range
);
968 call2 (function
, range
, arg
);
969 XSETCAR (range
, Qnil
);
974 if (NILP (XCAR (range
)))
975 XSETCAR (range
, make_number (c
));
982 map_char_table_for_charset (c_function
, function
, table
, arg
,
984 void (*c_function
) P_ ((Lisp_Object
, Lisp_Object
));
985 Lisp_Object function
, table
, arg
;
986 struct charset
*charset
;
993 range
= Fcons (Qnil
, Qnil
);
996 for (i
= 0, c
= 0; i
< chartab_size
[0]; i
++, c
+= chartab_chars
[0])
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
);
1006 if (! NILP (XCAR (range
)))
1008 XSETCDR (range
, make_number (c
- 1));
1010 (*c_function
) (arg
, range
);
1012 call2 (function
, range
, arg
);
1014 XSETCAR (range
, Qnil
);
1017 if (! NILP (XCAR (range
)))
1019 XSETCDR (range
, make_number (c
- 1));
1021 (*c_function
) (arg
, range
);
1023 call2 (function
, range
, arg
);
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) */