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
= value
;
615 for (i
= 0; i
< chartab_size
[0]; i
++)
616 XCHAR_TABLE (char_table
)->contents
[i
] = value
;
618 else if (EQ (range
, Qnil
))
619 XCHAR_TABLE (char_table
)->defalt
= value
;
620 else if (INTEGERP (range
))
621 char_table_set (char_table
, XINT (range
), value
);
622 else if (CONSP (range
))
624 CHECK_CHARACTER_CAR (range
);
625 CHECK_CHARACTER_CDR (range
);
626 char_table_set_range (char_table
,
627 XINT (XCAR (range
)), XINT (XCDR (range
)), value
);
630 error ("Invalid RANGE argument to `set-char-table-range'");
635 DEFUN ("set-char-table-default", Fset_char_table_default
,
636 Sset_char_table_default
, 3, 3, 0,
638 This function is obsolete and has no effect. */)
639 (char_table
, ch
, value
)
640 Lisp_Object char_table
, ch
, value
;
645 /* Look up the element in TABLE at index CH, and return it as an
646 integer. If the element is not a character, return CH itself. */
649 char_table_translate (table
, ch
)
654 value
= Faref (table
, make_number (ch
));
655 if (! CHARACTERP (value
))
661 optimize_sub_char_table (table
)
664 struct Lisp_Sub_Char_Table
*tbl
= XSUB_CHAR_TABLE (table
);
665 int depth
= XINT (tbl
->depth
);
666 Lisp_Object elt
, this;
669 elt
= XSUB_CHAR_TABLE (table
)->contents
[0];
670 if (SUB_CHAR_TABLE_P (elt
))
671 elt
= XSUB_CHAR_TABLE (table
)->contents
[0] = optimize_sub_char_table (elt
);
672 if (SUB_CHAR_TABLE_P (elt
))
674 for (i
= 1; i
< chartab_size
[depth
]; i
++)
676 this = XSUB_CHAR_TABLE (table
)->contents
[i
];
677 if (SUB_CHAR_TABLE_P (this))
678 this = XSUB_CHAR_TABLE (table
)->contents
[i
]
679 = optimize_sub_char_table (this);
680 if (SUB_CHAR_TABLE_P (this)
681 || NILP (Fequal (this, elt
)))
685 return (i
< chartab_size
[depth
] ? table
: elt
);
688 DEFUN ("optimize-char-table", Foptimize_char_table
, Soptimize_char_table
,
690 doc
: /* Optimize CHAR-TABLE. */)
692 Lisp_Object char_table
;
697 CHECK_CHAR_TABLE (char_table
);
699 for (i
= 0; i
< chartab_size
[0]; i
++)
701 elt
= XCHAR_TABLE (char_table
)->contents
[i
];
702 if (SUB_CHAR_TABLE_P (elt
))
703 XCHAR_TABLE (char_table
)->contents
[i
] = optimize_sub_char_table (elt
);
709 /* Map C_FUNCTION or FUNCTION over TABLE (top or sub char-table),
710 calling it for each character or group of characters that share a
711 value. RANGE is a cons (FROM . TO) specifying the range of target
712 characters, VAL is a value of FROM in TABLE, DEFAULT_VAL is the
713 default value of the char-table, PARENT is the parent of the
716 ARG is passed to C_FUNCTION when that is called.
718 It returns the value of last character covered by TABLE (not the
719 value inheritted from the parent), and by side-effect, the car part
720 of RANGE is updated to the minimum character C where C and all the
721 following characters in TABLE have the same value. */
724 map_sub_char_table (c_function
, function
, table
, arg
, val
, range
,
726 void (*c_function
) P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
));
727 Lisp_Object function
, table
, arg
, val
, range
, default_val
, parent
;
729 /* Pointer to the elements of TABLE. */
730 Lisp_Object
*contents
;
731 /* Depth of TABLE. */
733 /* Minimum and maxinum characters covered by TABLE. */
734 int min_char
, max_char
;
735 /* Number of characters covered by one element of TABLE. */
737 int from
= XINT (XCAR (range
)), to
= XINT (XCDR (range
));
740 if (SUB_CHAR_TABLE_P (table
))
742 struct Lisp_Sub_Char_Table
*tbl
= XSUB_CHAR_TABLE (table
);
744 depth
= XINT (tbl
->depth
);
745 contents
= tbl
->contents
;
746 min_char
= XINT (tbl
->min_char
);
747 max_char
= min_char
+ chartab_chars
[depth
- 1] - 1;
752 contents
= XCHAR_TABLE (table
)->contents
;
756 chars_in_block
= chartab_chars
[depth
];
760 /* Set I to the index of the first element to check. */
761 if (from
<= min_char
)
764 i
= (from
- min_char
) / chars_in_block
;
765 for (c
= min_char
+ chars_in_block
* i
; c
<= max_char
;
766 i
++, c
+= chars_in_block
)
768 Lisp_Object
this = contents
[i
];
769 int nextc
= c
+ chars_in_block
;
771 if (SUB_CHAR_TABLE_P (this))
774 XSETCDR (range
, make_number (nextc
- 1));
775 val
= map_sub_char_table (c_function
, function
, this, arg
,
776 val
, range
, default_val
, parent
);
782 if (NILP (Fequal (val
, this)))
784 int different_value
= 1;
790 Lisp_Object temp
= XCHAR_TABLE (parent
)->parent
;
792 /* This is to get a value of FROM in PARENT
793 without checking the parent of PARENT. */
794 XCHAR_TABLE (parent
)->parent
= Qnil
;
795 val
= CHAR_TABLE_REF (parent
, from
);
796 XCHAR_TABLE (parent
)->parent
= temp
;
797 XSETCDR (range
, make_number (c
- 1));
798 val
= map_sub_char_table (c_function
, function
,
799 parent
, arg
, val
, range
,
800 XCHAR_TABLE (parent
)->defalt
,
801 XCHAR_TABLE (parent
)->parent
);
802 if (! NILP (Fequal (val
, this)))
806 if (! NILP (val
) && different_value
)
808 XSETCDR (range
, make_number (c
- 1));
809 if (EQ (XCAR (range
), XCDR (range
)))
812 (*c_function
) (arg
, XCAR (range
), val
);
814 call2 (function
, XCAR (range
), val
);
819 (*c_function
) (arg
, range
, val
);
821 call2 (function
, range
, val
);
826 XSETCAR (range
, make_number (c
));
829 XSETCDR (range
, make_number (to
));
835 /* Map C_FUNCTION or FUNCTION over TABLE, calling it for each
836 character or group of characters that share a value.
838 ARG is passed to C_FUNCTION when that is called. */
841 map_char_table (c_function
, function
, table
, arg
)
842 void (*c_function
) P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
));
843 Lisp_Object function
, table
, arg
;
845 Lisp_Object range
, val
;
847 struct gcpro gcpro1
, gcpro2
, gcpro3
;
849 range
= Fcons (make_number (0), make_number (MAX_CHAR
));
850 GCPRO3 (table
, arg
, range
);
851 val
= XCHAR_TABLE (table
)->ascii
;
852 if (SUB_CHAR_TABLE_P (val
))
853 val
= XSUB_CHAR_TABLE (val
)->contents
[0];
854 val
= map_sub_char_table (c_function
, function
, table
, arg
, val
, range
,
855 XCHAR_TABLE (table
)->defalt
,
856 XCHAR_TABLE (table
)->parent
);
857 /* If VAL is nil and TABLE has a parent, we must consult the parent
859 while (NILP (val
) && ! NILP (XCHAR_TABLE (table
)->parent
))
861 Lisp_Object parent
= XCHAR_TABLE (table
)->parent
;
862 Lisp_Object temp
= XCHAR_TABLE (parent
)->parent
;
863 int from
= XINT (XCAR (range
));
865 /* This is to get a value of FROM in PARENT without checking the
867 XCHAR_TABLE (parent
)->parent
= Qnil
;
868 val
= CHAR_TABLE_REF (parent
, from
);
869 XCHAR_TABLE (parent
)->parent
= temp
;
870 val
= map_sub_char_table (c_function
, function
, parent
, arg
, val
, range
,
871 XCHAR_TABLE (parent
)->defalt
,
872 XCHAR_TABLE (parent
)->parent
);
878 if (EQ (XCAR (range
), XCDR (range
)))
881 (*c_function
) (arg
, XCAR (range
), val
);
883 call2 (function
, XCAR (range
), val
);
888 (*c_function
) (arg
, range
, val
);
890 call2 (function
, range
, val
);
897 DEFUN ("map-char-table", Fmap_char_table
, Smap_char_table
,
900 Call FUNCTION for each character in CHAR-TABLE that has non-nil value.
901 FUNCTION is called with two arguments--a key and a value.
902 The key is a character code or a cons of character codes specifying a
903 range of characters that have the same value. */)
904 (function
, char_table
)
905 Lisp_Object function
, char_table
;
907 CHECK_CHAR_TABLE (char_table
);
909 map_char_table (NULL
, function
, char_table
, char_table
);
915 map_sub_char_table_for_charset (c_function
, function
, table
, arg
, range
,
917 void (*c_function
) P_ ((Lisp_Object
, Lisp_Object
));
918 Lisp_Object function
, table
, arg
, range
;
919 struct charset
*charset
;
922 struct Lisp_Sub_Char_Table
*tbl
= XSUB_CHAR_TABLE (table
);
923 int depth
= XINT (tbl
->depth
);
927 for (i
= 0, c
= XINT (tbl
->min_char
); i
< chartab_size
[depth
];
928 i
++, c
+= chartab_chars
[depth
])
932 this = tbl
->contents
[i
];
933 if (SUB_CHAR_TABLE_P (this))
934 map_sub_char_table_for_charset (c_function
, function
, this, arg
,
935 range
, charset
, from
, to
);
938 if (! NILP (XCAR (range
)))
940 XSETCDR (range
, make_number (c
- 1));
942 (*c_function
) (arg
, range
);
944 call2 (function
, range
, arg
);
946 XSETCAR (range
, Qnil
);
950 for (i
= 0, c
= XINT (tbl
->min_char
); i
< chartab_size
[depth
]; i
++, c
++)
955 this = tbl
->contents
[i
];
958 && (code
= ENCODE_CHAR (charset
, c
),
959 (code
< from
|| code
> to
))))
961 if (! NILP (XCAR (range
)))
963 XSETCDR (range
, make_number (c
- 1));
965 (*c_function
) (arg
, range
);
967 call2 (function
, range
, arg
);
968 XSETCAR (range
, Qnil
);
973 if (NILP (XCAR (range
)))
974 XSETCAR (range
, make_number (c
));
981 map_char_table_for_charset (c_function
, function
, table
, arg
,
983 void (*c_function
) P_ ((Lisp_Object
, Lisp_Object
));
984 Lisp_Object function
, table
, arg
;
985 struct charset
*charset
;
992 range
= Fcons (Qnil
, Qnil
);
995 for (i
= 0, c
= 0; i
< chartab_size
[0]; i
++, c
+= chartab_chars
[0])
999 this = XCHAR_TABLE (table
)->contents
[i
];
1000 if (SUB_CHAR_TABLE_P (this))
1001 map_sub_char_table_for_charset (c_function
, function
, this, arg
,
1002 range
, charset
, from
, to
);
1005 if (! NILP (XCAR (range
)))
1007 XSETCDR (range
, make_number (c
- 1));
1009 (*c_function
) (arg
, range
);
1011 call2 (function
, range
, arg
);
1013 XSETCAR (range
, Qnil
);
1016 if (! NILP (XCAR (range
)))
1018 XSETCDR (range
, make_number (c
- 1));
1020 (*c_function
) (arg
, range
);
1022 call2 (function
, range
, arg
);
1032 defsubr (&Smake_char_table
);
1033 defsubr (&Schar_table_parent
);
1034 defsubr (&Schar_table_subtype
);
1035 defsubr (&Sset_char_table_parent
);
1036 defsubr (&Schar_table_extra_slot
);
1037 defsubr (&Sset_char_table_extra_slot
);
1038 defsubr (&Schar_table_range
);
1039 defsubr (&Sset_char_table_range
);
1040 defsubr (&Sset_char_table_default
);
1041 defsubr (&Soptimize_char_table
);
1042 defsubr (&Smap_char_table
);
1045 /* arch-tag: 18b5b560-7ab5-4108-b09e-d5dd65dc6fda
1046 (do not change this comment) */