1 /* chartab.c -- char-table support
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 National Institute of Advanced Industrial Science and Technology (AIST)
4 Registration Number H13PRO009
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
24 #include "character.h"
30 /* Number of elements in Nth level char-table. */
31 const int chartab_size
[4] =
32 { (1 << CHARTAB_SIZE_BITS_0
),
33 (1 << CHARTAB_SIZE_BITS_1
),
34 (1 << CHARTAB_SIZE_BITS_2
),
35 (1 << CHARTAB_SIZE_BITS_3
) };
37 /* Number of characters each element of Nth level char-table
39 const int chartab_chars
[4] =
40 { (1 << (CHARTAB_SIZE_BITS_1
+ CHARTAB_SIZE_BITS_2
+ CHARTAB_SIZE_BITS_3
)),
41 (1 << (CHARTAB_SIZE_BITS_2
+ CHARTAB_SIZE_BITS_3
)),
42 (1 << CHARTAB_SIZE_BITS_3
),
45 /* Number of characters (in bits) each element of Nth level char-table
47 const int chartab_bits
[4] =
48 { (CHARTAB_SIZE_BITS_1
+ CHARTAB_SIZE_BITS_2
+ CHARTAB_SIZE_BITS_3
),
49 (CHARTAB_SIZE_BITS_2
+ CHARTAB_SIZE_BITS_3
),
53 #define CHARTAB_IDX(c, depth, min_char) \
54 (((c) - (min_char)) >> chartab_bits[(depth)])
57 DEFUN ("make-char-table", Fmake_char_table
, Smake_char_table
, 1, 2, 0,
58 doc
: /* Return a newly created char-table, with purpose PURPOSE.
59 Each element is initialized to INIT, which defaults to nil.
61 PURPOSE should be a symbol. If it has a `char-table-extra-slots'
62 property, the property's value should be an integer between 0 and 10
63 that specifies how many extra slots the char-table has. Otherwise,
64 the char-table has no extra slot. */)
66 register Lisp_Object purpose
, init
;
73 CHECK_SYMBOL (purpose
);
74 n
= Fget (purpose
, Qchar_table_extra_slots
);
82 args_out_of_range (n
, Qnil
);
85 size
= VECSIZE (struct Lisp_Char_Table
) - 1 + n_extras
;
86 vector
= Fmake_vector (make_number (size
), init
);
87 XSETPVECTYPE (XVECTOR (vector
), PVEC_CHAR_TABLE
);
88 XCHAR_TABLE (vector
)->parent
= Qnil
;
89 XCHAR_TABLE (vector
)->purpose
= purpose
;
90 XSETCHAR_TABLE (vector
, XCHAR_TABLE (vector
));
95 make_sub_char_table (depth
, min_char
, defalt
)
100 int size
= VECSIZE (struct Lisp_Sub_Char_Table
) - 1 + chartab_size
[depth
];
102 table
= Fmake_vector (make_number (size
), defalt
);
103 XSETPVECTYPE (XVECTOR (table
), PVEC_SUB_CHAR_TABLE
);
104 XSUB_CHAR_TABLE (table
)->depth
= make_number (depth
);
105 XSUB_CHAR_TABLE (table
)->min_char
= make_number (min_char
);
111 char_table_ascii (table
)
116 sub
= XCHAR_TABLE (table
)->contents
[0];
117 if (! SUB_CHAR_TABLE_P (sub
))
119 sub
= XSUB_CHAR_TABLE (sub
)->contents
[0];
120 if (! SUB_CHAR_TABLE_P (sub
))
122 return XSUB_CHAR_TABLE (sub
)->contents
[0];
126 copy_sub_char_table (table
)
130 int depth
= XINT (XSUB_CHAR_TABLE (table
)->depth
);
131 int min_char
= XINT (XSUB_CHAR_TABLE (table
)->min_char
);
135 copy
= make_sub_char_table (depth
, min_char
, Qnil
);
136 /* Recursively copy any sub char-tables. */
137 for (i
= 0; i
< chartab_size
[depth
]; i
++)
139 val
= XSUB_CHAR_TABLE (table
)->contents
[i
];
140 if (SUB_CHAR_TABLE_P (val
))
141 XSUB_CHAR_TABLE (copy
)->contents
[i
] = copy_sub_char_table (val
);
143 XSUB_CHAR_TABLE (copy
)->contents
[i
] = val
;
151 copy_char_table (table
)
155 int size
= XCHAR_TABLE (table
)->size
& PSEUDOVECTOR_SIZE_MASK
;
158 copy
= Fmake_vector (make_number (size
), Qnil
);
159 XSETPVECTYPE (XVECTOR (copy
), PVEC_CHAR_TABLE
);
160 XCHAR_TABLE (copy
)->defalt
= XCHAR_TABLE (table
)->defalt
;
161 XCHAR_TABLE (copy
)->parent
= XCHAR_TABLE (table
)->parent
;
162 XCHAR_TABLE (copy
)->purpose
= XCHAR_TABLE (table
)->purpose
;
163 for (i
= 0; i
< chartab_size
[0]; i
++)
164 XCHAR_TABLE (copy
)->contents
[i
]
165 = (SUB_CHAR_TABLE_P (XCHAR_TABLE (table
)->contents
[i
])
166 ? copy_sub_char_table (XCHAR_TABLE (table
)->contents
[i
])
167 : XCHAR_TABLE (table
)->contents
[i
]);
168 XCHAR_TABLE (copy
)->ascii
= char_table_ascii (copy
);
169 size
-= VECSIZE (struct Lisp_Char_Table
) - 1;
170 for (i
= 0; i
< size
; i
++)
171 XCHAR_TABLE (copy
)->extras
[i
] = XCHAR_TABLE (table
)->extras
[i
];
173 XSETCHAR_TABLE (copy
, XCHAR_TABLE (copy
));
178 sub_char_table_ref (table
, c
)
182 struct Lisp_Sub_Char_Table
*tbl
= XSUB_CHAR_TABLE (table
);
183 int depth
= XINT (tbl
->depth
);
184 int min_char
= XINT (tbl
->min_char
);
187 val
= tbl
->contents
[CHARTAB_IDX (c
, depth
, min_char
)];
188 if (SUB_CHAR_TABLE_P (val
))
189 val
= sub_char_table_ref (val
, c
);
194 char_table_ref (table
, c
)
198 struct Lisp_Char_Table
*tbl
= XCHAR_TABLE (table
);
201 if (ASCII_CHAR_P (c
))
204 if (SUB_CHAR_TABLE_P (val
))
205 val
= XSUB_CHAR_TABLE (val
)->contents
[c
];
209 val
= tbl
->contents
[CHARTAB_IDX (c
, 0, 0)];
210 if (SUB_CHAR_TABLE_P (val
))
211 val
= sub_char_table_ref (val
, c
);
216 if (NILP (val
) && CHAR_TABLE_P (tbl
->parent
))
217 val
= char_table_ref (tbl
->parent
, c
);
223 sub_char_table_ref_and_range (table
, c
, from
, to
, defalt
)
229 struct Lisp_Sub_Char_Table
*tbl
= XSUB_CHAR_TABLE (table
);
230 int depth
= XINT (tbl
->depth
);
231 int min_char
= XINT (tbl
->min_char
);
232 int max_char
= min_char
+ chartab_chars
[depth
- 1] - 1;
233 int index
= CHARTAB_IDX (c
, depth
, min_char
), idx
;
236 val
= tbl
->contents
[index
];
237 if (SUB_CHAR_TABLE_P (val
))
238 val
= sub_char_table_ref_and_range (val
, c
, from
, to
, defalt
);
243 while (idx
> 0 && *from
< min_char
+ idx
* chartab_chars
[depth
])
245 Lisp_Object this_val
;
247 c
= min_char
+ idx
* chartab_chars
[depth
] - 1;
249 this_val
= tbl
->contents
[idx
];
250 if (SUB_CHAR_TABLE_P (this_val
))
251 this_val
= sub_char_table_ref_and_range (this_val
, c
, from
, to
, defalt
);
252 else if (NILP (this_val
))
255 if (! EQ (this_val
, val
))
261 while ((c
= min_char
+ (index
+ 1) * chartab_chars
[depth
]) <= max_char
264 Lisp_Object this_val
;
267 this_val
= tbl
->contents
[index
];
268 if (SUB_CHAR_TABLE_P (this_val
))
269 this_val
= sub_char_table_ref_and_range (this_val
, c
, from
, to
, defalt
);
270 else if (NILP (this_val
))
272 if (! EQ (this_val
, val
))
283 /* Return the value for C in char-table TABLE. Shrink the range *FROM
284 and *TO to cover characters (containing C) that have the same value
285 as C. It is not assured that the values of (*FROM - 1) and (*TO +
286 1) are different from that of C. */
289 char_table_ref_and_range (table
, c
, from
, to
)
294 struct Lisp_Char_Table
*tbl
= XCHAR_TABLE (table
);
295 int index
= CHARTAB_IDX (c
, 0, 0), idx
;
298 val
= tbl
->contents
[index
];
303 if (SUB_CHAR_TABLE_P (val
))
304 val
= sub_char_table_ref_and_range (val
, c
, from
, to
, tbl
->defalt
);
309 while (*from
< idx
* chartab_chars
[0])
311 Lisp_Object this_val
;
313 c
= idx
* chartab_chars
[0] - 1;
315 this_val
= tbl
->contents
[idx
];
316 if (SUB_CHAR_TABLE_P (this_val
))
317 this_val
= sub_char_table_ref_and_range (this_val
, c
, from
, to
,
319 else if (NILP (this_val
))
320 this_val
= tbl
->defalt
;
322 if (! EQ (this_val
, val
))
328 while (*to
>= (index
+ 1) * chartab_chars
[0])
330 Lisp_Object this_val
;
333 c
= index
* chartab_chars
[0];
334 this_val
= tbl
->contents
[index
];
335 if (SUB_CHAR_TABLE_P (this_val
))
336 this_val
= sub_char_table_ref_and_range (this_val
, c
, from
, to
,
338 else if (NILP (this_val
))
339 this_val
= tbl
->defalt
;
340 if (! EQ (this_val
, val
))
351 #define ASET_RANGE(ARRAY, FROM, TO, LIMIT, VAL) \
353 int limit = (TO) < (LIMIT) ? (TO) : (LIMIT); \
354 for (; (FROM) < limit; (FROM)++) (ARRAY)->contents[(FROM)] = (VAL); \
357 #define GET_SUB_CHAR_TABLE(TABLE, SUBTABLE, IDX, DEPTH, MIN_CHAR) \
359 (SUBTABLE) = (TABLE)->contents[(IDX)]; \
360 if (!SUB_CHAR_TABLE_P (SUBTABLE)) \
361 (SUBTABLE) = make_sub_char_table ((DEPTH), (MIN_CHAR), (SUBTABLE)); \
366 sub_char_table_set (table
, c
, val
)
371 struct Lisp_Sub_Char_Table
*tbl
= XSUB_CHAR_TABLE (table
);
372 int depth
= XINT ((tbl
)->depth
);
373 int min_char
= XINT ((tbl
)->min_char
);
374 int i
= CHARTAB_IDX (c
, depth
, min_char
);
378 tbl
->contents
[i
] = val
;
381 sub
= tbl
->contents
[i
];
382 if (! SUB_CHAR_TABLE_P (sub
))
384 sub
= make_sub_char_table (depth
+ 1,
385 min_char
+ i
* chartab_chars
[depth
], sub
);
386 tbl
->contents
[i
] = sub
;
388 sub_char_table_set (sub
, c
, val
);
393 char_table_set (table
, c
, val
)
398 struct Lisp_Char_Table
*tbl
= XCHAR_TABLE (table
);
401 && SUB_CHAR_TABLE_P (tbl
->ascii
))
403 XSUB_CHAR_TABLE (tbl
->ascii
)->contents
[c
] = val
;
407 int i
= CHARTAB_IDX (c
, 0, 0);
410 sub
= tbl
->contents
[i
];
411 if (! SUB_CHAR_TABLE_P (sub
))
413 sub
= make_sub_char_table (1, i
* chartab_chars
[0], sub
);
414 tbl
->contents
[i
] = sub
;
416 sub_char_table_set (sub
, c
, val
);
417 if (ASCII_CHAR_P (c
))
418 tbl
->ascii
= char_table_ascii (table
);
424 sub_char_table_set_range (table
, depth
, min_char
, from
, to
, val
)
431 int max_char
= min_char
+ chartab_chars
[depth
] - 1;
433 if (depth
== 3 || (from
<= min_char
&& to
>= max_char
))
440 if (! SUB_CHAR_TABLE_P (*table
))
441 *table
= make_sub_char_table (depth
, min_char
, *table
);
446 i
= CHARTAB_IDX (from
, depth
, min_char
);
447 j
= CHARTAB_IDX (to
, depth
, min_char
);
448 min_char
+= chartab_chars
[depth
] * i
;
449 for (; i
<= j
; i
++, min_char
+= chartab_chars
[depth
])
450 sub_char_table_set_range (XSUB_CHAR_TABLE (*table
)->contents
+ i
,
451 depth
, min_char
, from
, to
, val
);
457 char_table_set_range (table
, from
, to
, val
)
462 struct Lisp_Char_Table
*tbl
= XCHAR_TABLE (table
);
463 Lisp_Object
*contents
= tbl
->contents
;
467 char_table_set (table
, from
, val
);
470 for (i
= CHARTAB_IDX (from
, 0, 0), min_char
= i
* chartab_chars
[0];
472 i
++, min_char
+= chartab_chars
[0])
473 sub_char_table_set_range (contents
+ i
, 0, min_char
, from
, to
, val
);
474 if (ASCII_CHAR_P (from
))
475 tbl
->ascii
= char_table_ascii (table
);
481 DEFUN ("char-table-subtype", Fchar_table_subtype
, Schar_table_subtype
,
484 Return the subtype of char-table CHAR-TABLE. The value is a symbol. */)
486 Lisp_Object char_table
;
488 CHECK_CHAR_TABLE (char_table
);
490 return XCHAR_TABLE (char_table
)->purpose
;
493 DEFUN ("char-table-parent", Fchar_table_parent
, Schar_table_parent
,
495 doc
: /* Return the parent char-table of CHAR-TABLE.
496 The value is either nil or another char-table.
497 If CHAR-TABLE holds nil for a given character,
498 then the actual applicable value is inherited from the parent char-table
499 \(or from its parents, if necessary). */)
501 Lisp_Object char_table
;
503 CHECK_CHAR_TABLE (char_table
);
505 return XCHAR_TABLE (char_table
)->parent
;
508 DEFUN ("set-char-table-parent", Fset_char_table_parent
, Sset_char_table_parent
,
510 doc
: /* Set the parent char-table of CHAR-TABLE to PARENT.
511 Return PARENT. PARENT must be either nil or another char-table. */)
513 Lisp_Object char_table
, parent
;
517 CHECK_CHAR_TABLE (char_table
);
521 CHECK_CHAR_TABLE (parent
);
523 for (temp
= parent
; !NILP (temp
); temp
= XCHAR_TABLE (temp
)->parent
)
524 if (EQ (temp
, char_table
))
525 error ("Attempt to make a chartable be its own parent");
528 XCHAR_TABLE (char_table
)->parent
= parent
;
533 DEFUN ("char-table-extra-slot", Fchar_table_extra_slot
, Schar_table_extra_slot
,
535 doc
: /* Return the value of CHAR-TABLE's extra-slot number N. */)
537 Lisp_Object char_table
, n
;
539 CHECK_CHAR_TABLE (char_table
);
542 || XINT (n
) >= CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (char_table
)))
543 args_out_of_range (char_table
, n
);
545 return XCHAR_TABLE (char_table
)->extras
[XINT (n
)];
548 DEFUN ("set-char-table-extra-slot", Fset_char_table_extra_slot
,
549 Sset_char_table_extra_slot
,
551 doc
: /* Set CHAR-TABLE's extra-slot number N to VALUE. */)
552 (char_table
, n
, value
)
553 Lisp_Object char_table
, n
, value
;
555 CHECK_CHAR_TABLE (char_table
);
558 || XINT (n
) >= CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (char_table
)))
559 args_out_of_range (char_table
, n
);
561 return XCHAR_TABLE (char_table
)->extras
[XINT (n
)] = value
;
564 DEFUN ("char-table-range", Fchar_table_range
, Schar_table_range
,
566 doc
: /* Return the value in CHAR-TABLE for a range of characters RANGE.
567 RANGE should be nil (for the default value),
568 a cons of character codes (for characters in the range), or a character code. */)
570 Lisp_Object char_table
, range
;
573 CHECK_CHAR_TABLE (char_table
);
575 if (EQ (range
, Qnil
))
576 val
= XCHAR_TABLE (char_table
)->defalt
;
577 else if (INTEGERP (range
))
578 val
= CHAR_TABLE_REF (char_table
, XINT (range
));
579 else if (CONSP (range
))
583 CHECK_CHARACTER_CAR (range
);
584 CHECK_CHARACTER_CDR (range
);
585 val
= char_table_ref_and_range (char_table
, XINT (XCAR (range
)),
587 /* Not yet implemented. */
590 error ("Invalid RANGE argument to `char-table-range'");
594 DEFUN ("set-char-table-range", Fset_char_table_range
, Sset_char_table_range
,
596 doc
: /* Set the value in CHAR-TABLE for a range of characters RANGE to VALUE.
597 RANGE should be t (for all characters), nil (for the default value),
598 a cons of character codes (for characters in the range),
599 or a character code. Return VALUE. */)
600 (char_table
, range
, value
)
601 Lisp_Object char_table
, range
, value
;
603 CHECK_CHAR_TABLE (char_table
);
608 XCHAR_TABLE (char_table
)->ascii
= value
;
609 for (i
= 0; i
< chartab_size
[0]; i
++)
610 XCHAR_TABLE (char_table
)->contents
[i
] = value
;
612 else if (EQ (range
, Qnil
))
613 XCHAR_TABLE (char_table
)->defalt
= value
;
614 else if (INTEGERP (range
))
615 char_table_set (char_table
, XINT (range
), value
);
616 else if (CONSP (range
))
618 CHECK_CHARACTER_CAR (range
);
619 CHECK_CHARACTER_CDR (range
);
620 char_table_set_range (char_table
,
621 XINT (XCAR (range
)), XINT (XCDR (range
)), value
);
624 error ("Invalid RANGE argument to `set-char-table-range'");
629 DEFUN ("set-char-table-default", Fset_char_table_default
,
630 Sset_char_table_default
, 3, 3, 0,
632 This function is obsolete and has no effect. */)
633 (char_table
, ch
, value
)
634 Lisp_Object char_table
, ch
, value
;
639 /* Look up the element in TABLE at index CH, and return it as an
640 integer. If the element is not a character, return CH itself. */
643 char_table_translate (table
, ch
)
648 value
= Faref (table
, make_number (ch
));
649 if (! CHARACTERP (value
))
655 optimize_sub_char_table (table
, test
)
656 Lisp_Object table
, test
;
658 struct Lisp_Sub_Char_Table
*tbl
= XSUB_CHAR_TABLE (table
);
659 int depth
= XINT (tbl
->depth
);
660 Lisp_Object elt
, this;
663 elt
= XSUB_CHAR_TABLE (table
)->contents
[0];
664 if (SUB_CHAR_TABLE_P (elt
))
665 elt
= XSUB_CHAR_TABLE (table
)->contents
[0]
666 = optimize_sub_char_table (elt
, test
);
667 optimizable
= SUB_CHAR_TABLE_P (elt
) ? 0 : 1;
668 for (i
= 1; i
< chartab_size
[depth
]; i
++)
670 this = XSUB_CHAR_TABLE (table
)->contents
[i
];
671 if (SUB_CHAR_TABLE_P (this))
672 this = XSUB_CHAR_TABLE (table
)->contents
[i
]
673 = optimize_sub_char_table (this, test
);
675 && (NILP (test
) ? NILP (Fequal (this, elt
)) /* defaults to `equal'. */
676 : EQ (test
, Qeq
) ? !EQ (this, elt
) /* Optimize `eq' case. */
677 : NILP (call2 (test
, this, elt
))))
681 return (optimizable
? elt
: table
);
684 DEFUN ("optimize-char-table", Foptimize_char_table
, Soptimize_char_table
,
686 doc
: /* Optimize CHAR-TABLE.
687 TEST is the comparison function used to decide whether two entries are
688 equivalent and can be merged. It defaults to `equal'. */)
690 Lisp_Object char_table
, test
;
695 CHECK_CHAR_TABLE (char_table
);
697 for (i
= 0; i
< chartab_size
[0]; i
++)
699 elt
= XCHAR_TABLE (char_table
)->contents
[i
];
700 if (SUB_CHAR_TABLE_P (elt
))
701 XCHAR_TABLE (char_table
)->contents
[i
]
702 = optimize_sub_char_table (elt
, test
);
704 /* Reset the `ascii' cache, in case it got optimized away. */
705 XCHAR_TABLE (char_table
)->ascii
= char_table_ascii (char_table
);
711 /* Map C_FUNCTION or FUNCTION over TABLE (top or sub char-table),
712 calling it for each character or group of characters that share a
713 value. RANGE is a cons (FROM . TO) specifying the range of target
714 characters, VAL is a value of FROM in TABLE, DEFAULT_VAL is the
715 default value of the char-table, PARENT is the parent of the
718 ARG is passed to C_FUNCTION when that is called.
720 It returns the value of last character covered by TABLE (not the
721 value inheritted from the parent), and by side-effect, the car part
722 of RANGE is updated to the minimum character C where C and all the
723 following characters in TABLE have the same value. */
726 map_sub_char_table (c_function
, function
, table
, arg
, val
, range
,
728 void (*c_function
) P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
));
729 Lisp_Object function
, table
, arg
, val
, range
, default_val
, parent
;
731 /* Pointer to the elements of TABLE. */
732 Lisp_Object
*contents
;
733 /* Depth of TABLE. */
735 /* Minimum and maxinum characters covered by TABLE. */
736 int min_char
, max_char
;
737 /* Number of characters covered by one element of TABLE. */
739 int from
= XINT (XCAR (range
)), to
= XINT (XCDR (range
));
742 if (SUB_CHAR_TABLE_P (table
))
744 struct Lisp_Sub_Char_Table
*tbl
= XSUB_CHAR_TABLE (table
);
746 depth
= XINT (tbl
->depth
);
747 contents
= tbl
->contents
;
748 min_char
= XINT (tbl
->min_char
);
749 max_char
= min_char
+ chartab_chars
[depth
- 1] - 1;
754 contents
= XCHAR_TABLE (table
)->contents
;
758 chars_in_block
= chartab_chars
[depth
];
762 /* Set I to the index of the first element to check. */
763 if (from
<= min_char
)
766 i
= (from
- min_char
) / chars_in_block
;
767 for (c
= min_char
+ chars_in_block
* i
; c
<= max_char
;
768 i
++, c
+= chars_in_block
)
770 Lisp_Object
this = contents
[i
];
771 int nextc
= c
+ chars_in_block
;
773 if (SUB_CHAR_TABLE_P (this))
776 XSETCDR (range
, make_number (nextc
- 1));
777 val
= map_sub_char_table (c_function
, function
, this, arg
,
778 val
, range
, default_val
, parent
);
786 int different_value
= 1;
792 Lisp_Object temp
= XCHAR_TABLE (parent
)->parent
;
794 /* This is to get a value of FROM in PARENT
795 without checking the parent of PARENT. */
796 XCHAR_TABLE (parent
)->parent
= Qnil
;
797 val
= CHAR_TABLE_REF (parent
, from
);
798 XCHAR_TABLE (parent
)->parent
= temp
;
799 XSETCDR (range
, make_number (c
- 1));
800 val
= map_sub_char_table (c_function
, function
,
801 parent
, arg
, val
, range
,
802 XCHAR_TABLE (parent
)->defalt
,
803 XCHAR_TABLE (parent
)->parent
);
808 if (! NILP (val
) && different_value
)
810 XSETCDR (range
, make_number (c
- 1));
811 if (EQ (XCAR (range
), XCDR (range
)))
814 (*c_function
) (arg
, XCAR (range
), val
);
816 call2 (function
, XCAR (range
), val
);
821 (*c_function
) (arg
, range
, val
);
823 call2 (function
, range
, val
);
828 XSETCAR (range
, make_number (c
));
831 XSETCDR (range
, make_number (to
));
837 /* Map C_FUNCTION or FUNCTION over TABLE, calling it for each
838 character or group of characters that share a value.
840 ARG is passed to C_FUNCTION when that is called. */
843 map_char_table (c_function
, function
, table
, arg
)
844 void (*c_function
) P_ ((Lisp_Object
, Lisp_Object
, Lisp_Object
));
845 Lisp_Object function
, table
, arg
;
847 Lisp_Object range
, val
;
848 struct gcpro gcpro1
, gcpro2
, gcpro3
;
850 range
= Fcons (make_number (0), make_number (MAX_CHAR
));
851 GCPRO3 (table
, arg
, range
);
852 val
= XCHAR_TABLE (table
)->ascii
;
853 if (SUB_CHAR_TABLE_P (val
))
854 val
= XSUB_CHAR_TABLE (val
)->contents
[0];
855 val
= map_sub_char_table (c_function
, function
, table
, arg
, val
, range
,
856 XCHAR_TABLE (table
)->defalt
,
857 XCHAR_TABLE (table
)->parent
);
858 /* If VAL is nil and TABLE has a parent, we must consult the parent
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
));
981 /* Support function for `map-charset-chars'. Map C_FUNCTION or
982 FUNCTION over TABLE, calling it for each character or a group of
983 succeeding characters that have non-nil value in TABLE. TABLE is a
984 "mapping table" or a "deunifier table" of a certain charset.
986 If CHARSET is not NULL (this is the case that `map-charset-chars'
987 is called with non-nil FROM-CODE and TO-CODE), it is a charset who
988 owns TABLE, and the function is called only on a character in the
989 range FROM and TO. FROM and TO are not character codes, but code
990 points of a character in CHARSET.
992 This function is called in these two cases:
994 (1) A charset has a mapping file name in :map property.
996 (2) A charset has an upper code space in :offset property and a
997 mapping file name in :unify-map property. In this case, this
998 function is called only for characters in the Unicode code space.
999 Characters in upper code space are handled directly in
1000 map_charset_chars. */
1003 map_char_table_for_charset (c_function
, function
, table
, arg
,
1005 void (*c_function
) P_ ((Lisp_Object
, Lisp_Object
));
1006 Lisp_Object function
, table
, arg
;
1007 struct charset
*charset
;
1012 struct gcpro gcpro1
;
1014 range
= Fcons (Qnil
, Qnil
);
1017 for (i
= 0, c
= 0; i
< chartab_size
[0]; i
++, c
+= chartab_chars
[0])
1021 this = XCHAR_TABLE (table
)->contents
[i
];
1022 if (SUB_CHAR_TABLE_P (this))
1023 map_sub_char_table_for_charset (c_function
, function
, this, arg
,
1024 range
, charset
, from
, to
);
1027 if (! NILP (XCAR (range
)))
1029 XSETCDR (range
, make_number (c
- 1));
1031 (*c_function
) (arg
, range
);
1033 call2 (function
, range
, arg
);
1035 XSETCAR (range
, Qnil
);
1038 if (! NILP (XCAR (range
)))
1040 XSETCDR (range
, make_number (c
- 1));
1042 (*c_function
) (arg
, range
);
1044 call2 (function
, range
, arg
);
1054 defsubr (&Smake_char_table
);
1055 defsubr (&Schar_table_parent
);
1056 defsubr (&Schar_table_subtype
);
1057 defsubr (&Sset_char_table_parent
);
1058 defsubr (&Schar_table_extra_slot
);
1059 defsubr (&Sset_char_table_extra_slot
);
1060 defsubr (&Schar_table_range
);
1061 defsubr (&Sset_char_table_range
);
1062 defsubr (&Sset_char_table_default
);
1063 defsubr (&Soptimize_char_table
);
1064 defsubr (&Smap_char_table
);
1067 /* arch-tag: 18b5b560-7ab5-4108-b09e-d5dd65dc6fda
1068 (do not change this comment) */