* lib-src/fakemail.c (action): Convert function definitions to standard C.
[bpt/emacs.git] / src / chartab.c
CommitLineData
1ee5d538 1/* chartab.c -- char-table support
114f9c96 2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
1ee5d538
KH
3 National Institute of Advanced Industrial Science and Technology (AIST)
4 Registration Number H13PRO009
5
6This file is part of GNU Emacs.
7
9ec0b715 8GNU Emacs is free software: you can redistribute it and/or modify
1ee5d538 9it under the terms of the GNU General Public License as published by
9ec0b715
GM
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
1ee5d538
KH
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
9ec0b715 19along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
1ee5d538
KH
20
21#include <config.h>
d7306fe6 22#include <setjmp.h>
b4a12c67
DL
23#include "lisp.h"
24#include "character.h"
25#include "charset.h"
26#include "ccl.h"
1ee5d538
KH
27
28/* 64/16/32/128 */
29
30/* Number of elements in Nth level char-table. */
31const 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) };
36
37/* Number of characters each element of Nth level char-table
38 covers. */
39const 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),
43 1 };
44
45/* Number of characters (in bits) each element of Nth level char-table
46 covers. */
47const 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),
50 CHARTAB_SIZE_BITS_3,
51 0 };
52
53#define CHARTAB_IDX(c, depth, min_char) \
54 (((c) - (min_char)) >> chartab_bits[(depth)])
55
56
57DEFUN ("make-char-table", Fmake_char_table, Smake_char_table, 1, 2, 0,
eaa3b0e0 58 doc: /* Return a newly created char-table, with purpose PURPOSE.
1ee5d538
KH
59Each element is initialized to INIT, which defaults to nil.
60
eaa3b0e0
KH
61PURPOSE should be a symbol. If it has a `char-table-extra-slots'
62property, the property's value should be an integer between 0 and 10
63that specifies how many extra slots the char-table has. Otherwise,
64the char-table has no extra slot. */)
1ee5d538
KH
65 (purpose, init)
66 register Lisp_Object purpose, init;
67{
68 Lisp_Object vector;
69 Lisp_Object n;
eaa3b0e0 70 int n_extras;
1ee5d538
KH
71 int size;
72
73 CHECK_SYMBOL (purpose);
eaa3b0e0
KH
74 n = Fget (purpose, Qchar_table_extra_slots);
75 if (NILP (n))
76 n_extras = 0;
77 else
1ee5d538 78 {
eaa3b0e0
KH
79 CHECK_NATNUM (n);
80 n_extras = XINT (n);
81 if (n_extras > 10)
82 args_out_of_range (n, Qnil);
1ee5d538
KH
83 }
84
85 size = VECSIZE (struct Lisp_Char_Table) - 1 + n_extras;
86 vector = Fmake_vector (make_number (size), init);
985773c9 87 XSETPVECTYPE (XVECTOR (vector), PVEC_CHAR_TABLE);
1ee5d538
KH
88 XCHAR_TABLE (vector)->parent = Qnil;
89 XCHAR_TABLE (vector)->purpose = purpose;
90 XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
91 return vector;
92}
93
94static Lisp_Object
95make_sub_char_table (depth, min_char, defalt)
96 int depth, min_char;
97 Lisp_Object defalt;
98{
99 Lisp_Object table;
100 int size = VECSIZE (struct Lisp_Sub_Char_Table) - 1 + chartab_size[depth];
1ee5d538
KH
101
102 table = Fmake_vector (make_number (size), defalt);
985773c9 103 XSETPVECTYPE (XVECTOR (table), PVEC_SUB_CHAR_TABLE);
1ee5d538
KH
104 XSUB_CHAR_TABLE (table)->depth = make_number (depth);
105 XSUB_CHAR_TABLE (table)->min_char = make_number (min_char);
1ee5d538
KH
106
107 return table;
108}
109
110static Lisp_Object
111char_table_ascii (table)
112 Lisp_Object table;
113{
114 Lisp_Object sub;
115
116 sub = XCHAR_TABLE (table)->contents[0];
40033db7
KH
117 if (! SUB_CHAR_TABLE_P (sub))
118 return sub;
1ee5d538 119 sub = XSUB_CHAR_TABLE (sub)->contents[0];
40033db7
KH
120 if (! SUB_CHAR_TABLE_P (sub))
121 return sub;
1ee5d538
KH
122 return XSUB_CHAR_TABLE (sub)->contents[0];
123}
124
125Lisp_Object
126copy_sub_char_table (table)
127 Lisp_Object table;
128{
129 Lisp_Object copy;
130 int depth = XINT (XSUB_CHAR_TABLE (table)->depth);
131 int min_char = XINT (XSUB_CHAR_TABLE (table)->min_char);
132 Lisp_Object val;
133 int i;
134
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++)
138 {
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);
142 else
143 XSUB_CHAR_TABLE (copy)->contents[i] = val;
144 }
145
146 return copy;
147}
148
149
150Lisp_Object
151copy_char_table (table)
152 Lisp_Object table;
153{
154 Lisp_Object copy;
155 int size = XCHAR_TABLE (table)->size & PSEUDOVECTOR_SIZE_MASK;
156 int i;
157
158 copy = Fmake_vector (make_number (size), Qnil);
985773c9 159 XSETPVECTYPE (XVECTOR (copy), PVEC_CHAR_TABLE);
1ee5d538
KH
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;
1ee5d538
KH
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]);
d0827857 168 XCHAR_TABLE (copy)->ascii = char_table_ascii (copy);
1ee5d538
KH
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];
172
173 XSETCHAR_TABLE (copy, XCHAR_TABLE (copy));
174 return copy;
175}
176
177Lisp_Object
178sub_char_table_ref (table, c)
179 Lisp_Object table;
180 int c;
181{
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);
185 Lisp_Object val;
186
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);
190 return val;
191}
192
193Lisp_Object
194char_table_ref (table, c)
195 Lisp_Object table;
196 int c;
197{
198 struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
199 Lisp_Object val;
200
201 if (ASCII_CHAR_P (c))
202 {
203 val = tbl->ascii;
204 if (SUB_CHAR_TABLE_P (val))
205 val = XSUB_CHAR_TABLE (val)->contents[c];
206 }
207 else
208 {
209 val = tbl->contents[CHARTAB_IDX (c, 0, 0)];
210 if (SUB_CHAR_TABLE_P (val))
211 val = sub_char_table_ref (val, c);
212 }
213 if (NILP (val))
214 {
215 val = tbl->defalt;
216 if (NILP (val) && CHAR_TABLE_P (tbl->parent))
217 val = char_table_ref (tbl->parent, c);
218 }
219 return val;
8f924df7 220}
1ee5d538
KH
221
222static Lisp_Object
e15009d9 223sub_char_table_ref_and_range (table, c, from, to, defalt)
1ee5d538
KH
224 Lisp_Object table;
225 int c;
226 int *from, *to;
e15009d9 227 Lisp_Object defalt;
1ee5d538
KH
228{
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);
e15009d9 232 int max_char = min_char + chartab_chars[depth - 1] - 1;
7ef1f5d1 233 int index = CHARTAB_IDX (c, depth, min_char), idx;
1ee5d538 234 Lisp_Object val;
8f924df7 235
e15009d9 236 val = tbl->contents[index];
e15009d9
KH
237 if (SUB_CHAR_TABLE_P (val))
238 val = sub_char_table_ref_and_range (val, c, from, to, defalt);
239 else if (NILP (val))
240 val = defalt;
241
7ef1f5d1
KH
242 idx = index;
243 while (idx > 0 && *from < min_char + idx * chartab_chars[depth])
1ee5d538 244 {
e15009d9 245 Lisp_Object this_val;
e15009d9 246
7ef1f5d1
KH
247 c = min_char + idx * chartab_chars[depth] - 1;
248 idx--;
249 this_val = tbl->contents[idx];
e15009d9 250 if (SUB_CHAR_TABLE_P (this_val))
7ef1f5d1 251 this_val = sub_char_table_ref_and_range (this_val, c, from, to, defalt);
e15009d9
KH
252 else if (NILP (this_val))
253 this_val = defalt;
254
255 if (! EQ (this_val, val))
7ef1f5d1
KH
256 {
257 *from = c + 1;
258 break;
259 }
1ee5d538 260 }
05756f21 261 while ((c = min_char + (index + 1) * chartab_chars[depth]) <= max_char
7ef1f5d1 262 && *to >= c)
1ee5d538 263 {
e15009d9 264 Lisp_Object this_val;
e15009d9
KH
265
266 index++;
267 this_val = tbl->contents[index];
268 if (SUB_CHAR_TABLE_P (this_val))
7ef1f5d1 269 this_val = sub_char_table_ref_and_range (this_val, c, from, to, defalt);
e15009d9
KH
270 else if (NILP (this_val))
271 this_val = defalt;
272 if (! EQ (this_val, val))
7ef1f5d1
KH
273 {
274 *to = c - 1;
275 break;
276 }
1ee5d538 277 }
e15009d9 278
1ee5d538
KH
279 return val;
280}
281
282
7ef1f5d1
KH
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. */
e15009d9 287
1ee5d538
KH
288Lisp_Object
289char_table_ref_and_range (table, c, from, to)
290 Lisp_Object table;
291 int c;
292 int *from, *to;
293{
294 struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
7ef1f5d1 295 int index = CHARTAB_IDX (c, 0, 0), idx;
1ee5d538
KH
296 Lisp_Object val;
297
e15009d9 298 val = tbl->contents[index];
7ef1f5d1
KH
299 if (*from < 0)
300 *from = 0;
301 if (*to < 0)
302 *to = MAX_CHAR;
e15009d9
KH
303 if (SUB_CHAR_TABLE_P (val))
304 val = sub_char_table_ref_and_range (val, c, from, to, tbl->defalt);
305 else if (NILP (val))
306 val = tbl->defalt;
307
7ef1f5d1
KH
308 idx = index;
309 while (*from < idx * chartab_chars[0])
1ee5d538 310 {
e15009d9 311 Lisp_Object this_val;
e15009d9 312
7ef1f5d1
KH
313 c = idx * chartab_chars[0] - 1;
314 idx--;
315 this_val = tbl->contents[idx];
e15009d9 316 if (SUB_CHAR_TABLE_P (this_val))
7ef1f5d1 317 this_val = sub_char_table_ref_and_range (this_val, c, from, to,
e15009d9
KH
318 tbl->defalt);
319 else if (NILP (this_val))
320 this_val = tbl->defalt;
321
322 if (! EQ (this_val, val))
7ef1f5d1
KH
323 {
324 *from = c + 1;
325 break;
326 }
1ee5d538 327 }
7ef1f5d1 328 while (*to >= (index + 1) * chartab_chars[0])
1ee5d538 329 {
e15009d9 330 Lisp_Object this_val;
e15009d9
KH
331
332 index++;
7ef1f5d1 333 c = index * chartab_chars[0];
e15009d9
KH
334 this_val = tbl->contents[index];
335 if (SUB_CHAR_TABLE_P (this_val))
7ef1f5d1 336 this_val = sub_char_table_ref_and_range (this_val, c, from, to,
e15009d9
KH
337 tbl->defalt);
338 else if (NILP (this_val))
339 this_val = tbl->defalt;
340 if (! EQ (this_val, val))
7ef1f5d1
KH
341 {
342 *to = c - 1;
343 break;
344 }
1ee5d538
KH
345 }
346
1ee5d538 347 return val;
e15009d9 348}
1ee5d538
KH
349
350
351#define ASET_RANGE(ARRAY, FROM, TO, LIMIT, VAL) \
352 do { \
353 int limit = (TO) < (LIMIT) ? (TO) : (LIMIT); \
354 for (; (FROM) < limit; (FROM)++) (ARRAY)->contents[(FROM)] = (VAL); \
355 } while (0)
356
357#define GET_SUB_CHAR_TABLE(TABLE, SUBTABLE, IDX, DEPTH, MIN_CHAR) \
358 do { \
359 (SUBTABLE) = (TABLE)->contents[(IDX)]; \
360 if (!SUB_CHAR_TABLE_P (SUBTABLE)) \
361 (SUBTABLE) = make_sub_char_table ((DEPTH), (MIN_CHAR), (SUBTABLE)); \
362 } while (0)
363
364
365static void
366sub_char_table_set (table, c, val)
367 Lisp_Object table;
368 int c;
369 Lisp_Object val;
370{
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);
375 Lisp_Object sub;
8f924df7 376
1ee5d538
KH
377 if (depth == 3)
378 tbl->contents[i] = val;
379 else
380 {
381 sub = tbl->contents[i];
382 if (! SUB_CHAR_TABLE_P (sub))
383 {
384 sub = make_sub_char_table (depth + 1,
385 min_char + i * chartab_chars[depth], sub);
386 tbl->contents[i] = sub;
387 }
388 sub_char_table_set (sub, c, val);
389 }
390}
391
392Lisp_Object
393char_table_set (table, c, val)
394 Lisp_Object table;
395 int c;
396 Lisp_Object val;
397{
398 struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
399
400 if (ASCII_CHAR_P (c)
401 && SUB_CHAR_TABLE_P (tbl->ascii))
402 {
403 XSUB_CHAR_TABLE (tbl->ascii)->contents[c] = val;
404 }
405 else
406 {
407 int i = CHARTAB_IDX (c, 0, 0);
408 Lisp_Object sub;
409
410 sub = tbl->contents[i];
411 if (! SUB_CHAR_TABLE_P (sub))
412 {
413 sub = make_sub_char_table (1, i * chartab_chars[0], sub);
414 tbl->contents[i] = sub;
415 }
416 sub_char_table_set (sub, c, val);
417 if (ASCII_CHAR_P (c))
40033db7 418 tbl->ascii = char_table_ascii (table);
1ee5d538
KH
419 }
420 return val;
421}
422
423static void
424sub_char_table_set_range (table, depth, min_char, from, to, val)
425 Lisp_Object *table;
426 int depth;
427 int min_char;
428 int from, to;
429 Lisp_Object val;
430{
431 int max_char = min_char + chartab_chars[depth] - 1;
432
ed09a18b 433 if (depth == 3 || (from <= min_char && to >= max_char))
1ee5d538
KH
434 *table = val;
435 else
436 {
437 int i, j;
438
439 depth++;
440 if (! SUB_CHAR_TABLE_P (*table))
441 *table = make_sub_char_table (depth, min_char, *table);
442 if (from < min_char)
443 from = min_char;
444 if (to > max_char)
445 to = max_char;
22d49f94 446 i = CHARTAB_IDX (from, depth, min_char);
1ee5d538 447 j = CHARTAB_IDX (to, depth, min_char);
22d49f94
KH
448 min_char += chartab_chars[depth] * i;
449 for (; i <= j; i++, min_char += chartab_chars[depth])
1ee5d538 450 sub_char_table_set_range (XSUB_CHAR_TABLE (*table)->contents + i,
22d49f94 451 depth, min_char, from, to, val);
1ee5d538
KH
452 }
453}
454
455
456Lisp_Object
457char_table_set_range (table, from, to, val)
458 Lisp_Object table;
459 int from, to;
460 Lisp_Object val;
461{
462 struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
463 Lisp_Object *contents = tbl->contents;
464 int i, min_char;
465
466 if (from == to)
467 char_table_set (table, from, val);
468 else
469 {
470 for (i = CHARTAB_IDX (from, 0, 0), min_char = i * chartab_chars[0];
471 min_char <= to;
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))
40033db7 475 tbl->ascii = char_table_ascii (table);
1ee5d538
KH
476 }
477 return val;
478}
479
480\f
481DEFUN ("char-table-subtype", Fchar_table_subtype, Schar_table_subtype,
482 1, 1, 0,
483 doc: /*
484Return the subtype of char-table CHAR-TABLE. The value is a symbol. */)
485 (char_table)
486 Lisp_Object char_table;
487{
488 CHECK_CHAR_TABLE (char_table);
489
490 return XCHAR_TABLE (char_table)->purpose;
491}
492
493DEFUN ("char-table-parent", Fchar_table_parent, Schar_table_parent,
494 1, 1, 0,
495 doc: /* Return the parent char-table of CHAR-TABLE.
496The value is either nil or another char-table.
497If CHAR-TABLE holds nil for a given character,
498then the actual applicable value is inherited from the parent char-table
499\(or from its parents, if necessary). */)
500 (char_table)
501 Lisp_Object char_table;
502{
503 CHECK_CHAR_TABLE (char_table);
504
505 return XCHAR_TABLE (char_table)->parent;
506}
507
508DEFUN ("set-char-table-parent", Fset_char_table_parent, Sset_char_table_parent,
509 2, 2, 0,
510 doc: /* Set the parent char-table of CHAR-TABLE to PARENT.
6b61353c 511Return PARENT. PARENT must be either nil or another char-table. */)
1ee5d538
KH
512 (char_table, parent)
513 Lisp_Object char_table, parent;
514{
515 Lisp_Object temp;
516
517 CHECK_CHAR_TABLE (char_table);
518
519 if (!NILP (parent))
520 {
521 CHECK_CHAR_TABLE (parent);
522
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");
526 }
527
528 XCHAR_TABLE (char_table)->parent = parent;
529
530 return parent;
531}
532
533DEFUN ("char-table-extra-slot", Fchar_table_extra_slot, Schar_table_extra_slot,
534 2, 2, 0,
535 doc: /* Return the value of CHAR-TABLE's extra-slot number N. */)
536 (char_table, n)
537 Lisp_Object char_table, n;
538{
539 CHECK_CHAR_TABLE (char_table);
540 CHECK_NUMBER (n);
541 if (XINT (n) < 0
542 || XINT (n) >= CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (char_table)))
543 args_out_of_range (char_table, n);
544
545 return XCHAR_TABLE (char_table)->extras[XINT (n)];
546}
547
548DEFUN ("set-char-table-extra-slot", Fset_char_table_extra_slot,
549 Sset_char_table_extra_slot,
550 3, 3, 0,
551 doc: /* Set CHAR-TABLE's extra-slot number N to VALUE. */)
552 (char_table, n, value)
553 Lisp_Object char_table, n, value;
554{
555 CHECK_CHAR_TABLE (char_table);
556 CHECK_NUMBER (n);
557 if (XINT (n) < 0
558 || XINT (n) >= CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (char_table)))
559 args_out_of_range (char_table, n);
560
561 return XCHAR_TABLE (char_table)->extras[XINT (n)] = value;
562}
563\f
564DEFUN ("char-table-range", Fchar_table_range, Schar_table_range,
565 2, 2, 0,
566 doc: /* Return the value in CHAR-TABLE for a range of characters RANGE.
567RANGE should be nil (for the default value),
568a cons of character codes (for characters in the range), or a character code. */)
569 (char_table, range)
570 Lisp_Object char_table, range;
571{
572 Lisp_Object val;
573 CHECK_CHAR_TABLE (char_table);
574
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))
580 {
581 int from, to;
582
8f924df7
KH
583 CHECK_CHARACTER_CAR (range);
584 CHECK_CHARACTER_CDR (range);
1ee5d538
KH
585 val = char_table_ref_and_range (char_table, XINT (XCAR (range)),
586 &from, &to);
587 /* Not yet implemented. */
588 }
589 else
590 error ("Invalid RANGE argument to `char-table-range'");
591 return val;
592}
593
594DEFUN ("set-char-table-range", Fset_char_table_range, Sset_char_table_range,
595 3, 3, 0,
6b61353c 596 doc: /* Set the value in CHAR-TABLE for a range of characters RANGE to VALUE.
1ee5d538 597RANGE should be t (for all characters), nil (for the default value),
6b61353c
KH
598a cons of character codes (for characters in the range),
599or a character code. Return VALUE. */)
1ee5d538
KH
600 (char_table, range, value)
601 Lisp_Object char_table, range, value;
602{
603 CHECK_CHAR_TABLE (char_table);
604 if (EQ (range, Qt))
605 {
606 int i;
607
c6bff69e 608 XCHAR_TABLE (char_table)->ascii = value;
1ee5d538 609 for (i = 0; i < chartab_size[0]; i++)
c6bff69e 610 XCHAR_TABLE (char_table)->contents[i] = value;
1ee5d538
KH
611 }
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))
617 {
8f924df7
KH
618 CHECK_CHARACTER_CAR (range);
619 CHECK_CHARACTER_CDR (range);
1ee5d538
KH
620 char_table_set_range (char_table,
621 XINT (XCAR (range)), XINT (XCDR (range)), value);
622 }
623 else
624 error ("Invalid RANGE argument to `set-char-table-range'");
625
626 return value;
627}
628
629DEFUN ("set-char-table-default", Fset_char_table_default,
630 Sset_char_table_default, 3, 3, 0,
631 doc: /*
f6e5cae0 632This function is obsolete and has no effect. */)
1ee5d538
KH
633 (char_table, ch, value)
634 Lisp_Object char_table, ch, value;
635{
636 return Qnil;
637}
638
639/* Look up the element in TABLE at index CH, and return it as an
05d6275c 640 integer. If the element is not a character, return CH itself. */
1ee5d538
KH
641
642int
643char_table_translate (table, ch)
644 Lisp_Object table;
645 int ch;
646{
647 Lisp_Object value;
648 value = Faref (table, make_number (ch));
05d6275c 649 if (! CHARACTERP (value))
1ee5d538
KH
650 return ch;
651 return XINT (value);
652}
653
654static Lisp_Object
d0827857
SM
655optimize_sub_char_table (table, test)
656 Lisp_Object table, test;
1ee5d538
KH
657{
658 struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
659 int depth = XINT (tbl->depth);
660 Lisp_Object elt, this;
c3b57f23 661 int i, optimizable;
1ee5d538
KH
662
663 elt = XSUB_CHAR_TABLE (table)->contents[0];
664 if (SUB_CHAR_TABLE_P (elt))
d0827857
SM
665 elt = XSUB_CHAR_TABLE (table)->contents[0]
666 = optimize_sub_char_table (elt, test);
c3b57f23 667 optimizable = SUB_CHAR_TABLE_P (elt) ? 0 : 1;
1ee5d538
KH
668 for (i = 1; i < chartab_size[depth]; i++)
669 {
670 this = XSUB_CHAR_TABLE (table)->contents[i];
671 if (SUB_CHAR_TABLE_P (this))
672 this = XSUB_CHAR_TABLE (table)->contents[i]
d0827857 673 = optimize_sub_char_table (this, test);
c3b57f23
KH
674 if (optimizable
675 && (NILP (test) ? NILP (Fequal (this, elt)) /* defaults to `equal'. */
d0827857
SM
676 : EQ (test, Qeq) ? !EQ (this, elt) /* Optimize `eq' case. */
677 : NILP (call2 (test, this, elt))))
c3b57f23 678 optimizable = 0;
1ee5d538
KH
679 }
680
c3b57f23 681 return (optimizable ? elt : table);
1ee5d538
KH
682}
683
684DEFUN ("optimize-char-table", Foptimize_char_table, Soptimize_char_table,
d0827857
SM
685 1, 2, 0,
686 doc: /* Optimize CHAR-TABLE.
687TEST is the comparison function used to decide whether two entries are
688equivalent and can be merged. It defaults to `equal'. */)
689 (char_table, test)
690 Lisp_Object char_table, test;
1ee5d538
KH
691{
692 Lisp_Object elt;
693 int i;
694
695 CHECK_CHAR_TABLE (char_table);
696
697 for (i = 0; i < chartab_size[0]; i++)
698 {
699 elt = XCHAR_TABLE (char_table)->contents[i];
700 if (SUB_CHAR_TABLE_P (elt))
d0827857
SM
701 XCHAR_TABLE (char_table)->contents[i]
702 = optimize_sub_char_table (elt, test);
1ee5d538 703 }
4d632321
SM
704 /* Reset the `ascii' cache, in case it got optimized away. */
705 XCHAR_TABLE (char_table)->ascii = char_table_ascii (char_table);
706
1ee5d538
KH
707 return Qnil;
708}
709
710\f
57d53d1b
KH
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
716 char-table.
717
718 ARG is passed to C_FUNCTION when that is called.
719
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. */
724
1ee5d538 725static Lisp_Object
8f924df7
KH
726map_sub_char_table (c_function, function, table, arg, val, range,
727 default_val, parent)
f57e2426 728 void (*c_function) (Lisp_Object, Lisp_Object, Lisp_Object);
8f924df7 729 Lisp_Object function, table, arg, val, range, default_val, parent;
1ee5d538 730{
57d53d1b
KH
731 /* Pointer to the elements of TABLE. */
732 Lisp_Object *contents;
733 /* Depth of TABLE. */
734 int depth;
735 /* Minimum and maxinum characters covered by TABLE. */
736 int min_char, max_char;
737 /* Number of characters covered by one element of TABLE. */
738 int chars_in_block;
739 int from = XINT (XCAR (range)), to = XINT (XCDR (range));
1ee5d538
KH
740 int i, c;
741
57d53d1b
KH
742 if (SUB_CHAR_TABLE_P (table))
743 {
744 struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
745
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;
750 }
751 else
752 {
753 depth = 0;
754 contents = XCHAR_TABLE (table)->contents;
755 min_char = 0;
756 max_char = MAX_CHAR;
757 }
758 chars_in_block = chartab_chars[depth];
759
760 if (to < max_char)
761 max_char = to;
762 /* Set I to the index of the first element to check. */
763 if (from <= min_char)
764 i = 0;
765 else
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)
1ee5d538 769 {
57d53d1b
KH
770 Lisp_Object this = contents[i];
771 int nextc = c + chars_in_block;
1ee5d538 772
1ee5d538 773 if (SUB_CHAR_TABLE_P (this))
57d53d1b
KH
774 {
775 if (to >= nextc)
776 XSETCDR (range, make_number (nextc - 1));
777 val = map_sub_char_table (c_function, function, this, arg,
778 val, range, default_val, parent);
779 }
2f76e15e 780 else
1ee5d538 781 {
2f76e15e
KH
782 if (NILP (this))
783 this = default_val;
d0827857 784 if (!EQ (val, this))
1ee5d538 785 {
57d53d1b
KH
786 int different_value = 1;
787
788 if (NILP (val))
789 {
790 if (! NILP (parent))
791 {
792 Lisp_Object temp = XCHAR_TABLE (parent)->parent;
793
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);
d0827857 804 if (EQ (val, this))
57d53d1b
KH
805 different_value = 0;
806 }
807 }
808 if (! NILP (val) && different_value)
1ee5d538 809 {
8f924df7 810 XSETCDR (range, make_number (c - 1));
0a4bacdc 811 if (EQ (XCAR (range), XCDR (range)))
2f76e15e
KH
812 {
813 if (c_function)
814 (*c_function) (arg, XCAR (range), val);
815 else
816 call2 (function, XCAR (range), val);
817 }
1ee5d538 818 else
2f76e15e
KH
819 {
820 if (c_function)
821 (*c_function) (arg, range, val);
822 else
823 call2 (function, range, val);
824 }
1ee5d538 825 }
2f76e15e 826 val = this;
57d53d1b 827 from = c;
8f924df7 828 XSETCAR (range, make_number (c));
1ee5d538 829 }
1ee5d538 830 }
57d53d1b 831 XSETCDR (range, make_number (to));
1ee5d538
KH
832 }
833 return val;
834}
835
836
837/* Map C_FUNCTION or FUNCTION over TABLE, calling it for each
838 character or group of characters that share a value.
839
8f924df7 840 ARG is passed to C_FUNCTION when that is called. */
1ee5d538
KH
841
842void
8f924df7 843map_char_table (c_function, function, table, arg)
f57e2426 844 void (*c_function) (Lisp_Object, Lisp_Object, Lisp_Object);
8f924df7 845 Lisp_Object function, table, arg;
1ee5d538
KH
846{
847 Lisp_Object range, val;
239191f2 848 struct gcpro gcpro1, gcpro2, gcpro3;
1ee5d538 849
57d53d1b 850 range = Fcons (make_number (0), make_number (MAX_CHAR));
239191f2 851 GCPRO3 (table, arg, range);
2f76e15e
KH
852 val = XCHAR_TABLE (table)->ascii;
853 if (SUB_CHAR_TABLE_P (val))
854 val = XSUB_CHAR_TABLE (val)->contents[0];
57d53d1b
KH
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))
1ee5d538 861 {
57d53d1b
KH
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;
1ee5d538 875 }
2f76e15e
KH
876
877 if (! NILP (val))
878 {
0a4bacdc
KH
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 }
2f76e15e 886 else
0a4bacdc
KH
887 {
888 if (c_function)
889 (*c_function) (arg, range, val);
890 else
891 call2 (function, range, val);
892 }
2f76e15e 893 }
26132fb5
AS
894
895 UNGCPRO;
1ee5d538
KH
896}
897
898DEFUN ("map-char-table", Fmap_char_table, Smap_char_table,
899 2, 2, 0,
900 doc: /*
ed7219f8 901Call FUNCTION for each character in CHAR-TABLE that has non-nil value.
1ee5d538 902FUNCTION is called with two arguments--a key and a value.
2f76e15e
KH
903The key is a character code or a cons of character codes specifying a
904range of characters that have the same value. */)
1ee5d538
KH
905 (function, char_table)
906 Lisp_Object function, char_table;
907{
908 CHECK_CHAR_TABLE (char_table);
909
8f924df7 910 map_char_table (NULL, function, char_table, char_table);
1ee5d538
KH
911 return Qnil;
912}
913
e15009d9
KH
914
915static void
916map_sub_char_table_for_charset (c_function, function, table, arg, range,
917 charset, from, to)
f57e2426 918 void (*c_function) (Lisp_Object, Lisp_Object);
e15009d9
KH
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)
bc85ac78 966 (*c_function) (arg, range);
e15009d9
KH
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
a6805333 981/* Support function for `map-charset-chars'. Map C_FUNCTION or
60612c8f 982 FUNCTION over TABLE, calling it for each character or a group of
a6805333
KH
983 succeeding characters that have non-nil value in TABLE. TABLE is a
984 "mapping table" or a "deunifier table" of a certain charset.
985
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.
991
992 This function is called in these two cases:
993
994 (1) A charset has a mapping file name in :map property.
995
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. */
1001
e15009d9
KH
1002void
1003map_char_table_for_charset (c_function, function, table, arg,
1004 charset, from, to)
f57e2426 1005 void (*c_function) (Lisp_Object, Lisp_Object);
e15009d9
KH
1006 Lisp_Object function, table, arg;
1007 struct charset *charset;
1008 unsigned from, to;
1009{
1010 Lisp_Object range;
1011 int c, i;
26132fb5 1012 struct gcpro gcpro1;
e15009d9 1013
8f924df7 1014 range = Fcons (Qnil, Qnil);
26132fb5 1015 GCPRO1 (range);
e15009d9
KH
1016
1017 for (i = 0, c = 0; i < chartab_size[0]; i++, c += chartab_chars[0])
1018 {
1019 Lisp_Object this;
1020
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);
1025 else
1026 {
1027 if (! NILP (XCAR (range)))
1028 {
1029 XSETCDR (range, make_number (c - 1));
1030 if (c_function)
1031 (*c_function) (arg, range);
1032 else
1033 call2 (function, range, arg);
1034 }
1035 XSETCAR (range, Qnil);
1036 }
1037 }
1038 if (! NILP (XCAR (range)))
1039 {
1040 XSETCDR (range, make_number (c - 1));
1041 if (c_function)
1042 (*c_function) (arg, range);
1043 else
1044 call2 (function, range, arg);
1045 }
26132fb5
AS
1046
1047 UNGCPRO;
e15009d9
KH
1048}
1049
1ee5d538
KH
1050\f
1051void
1052syms_of_chartab ()
1053{
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);
1065}
fbaf0946
MB
1066
1067/* arch-tag: 18b5b560-7ab5-4108-b09e-d5dd65dc6fda
1068 (do not change this comment) */