chartab.c 40.7 KB
Newer Older
1
/* chartab.c -- char-table support
2
   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 4 5 6 7
     National Institute of Advanced Industrial Science and Technology (AIST)
     Registration Number H13PRO009

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 11
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
12 13 14 15 16 17 18

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

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/>.  */
20 21

#include <config.h>
22
#include <setjmp.h>
23 24 25 26
#include "lisp.h"
#include "character.h"
#include "charset.h"
#include "ccl.h"
27 28 29 30 31 32 33 34 35 36 37 38

/* 64/16/32/128 */

/* Number of elements in Nth level char-table.  */
const int chartab_size[4] =
  { (1 << CHARTAB_SIZE_BITS_0),
    (1 << CHARTAB_SIZE_BITS_1),
    (1 << CHARTAB_SIZE_BITS_2),
    (1 << CHARTAB_SIZE_BITS_3) };

/* Number of characters each element of Nth level char-table
   covers.  */
39
static const int chartab_chars[4] =
40 41 42 43 44 45 46
  { (1 << (CHARTAB_SIZE_BITS_1 + CHARTAB_SIZE_BITS_2 + CHARTAB_SIZE_BITS_3)),
    (1 << (CHARTAB_SIZE_BITS_2 + CHARTAB_SIZE_BITS_3)),
    (1 << CHARTAB_SIZE_BITS_3),
    1 };

/* Number of characters (in bits) each element of Nth level char-table
   covers.  */
47
static const int chartab_bits[4] =
48 49 50 51 52 53 54 55
  { (CHARTAB_SIZE_BITS_1 + CHARTAB_SIZE_BITS_2 + CHARTAB_SIZE_BITS_3),
    (CHARTAB_SIZE_BITS_2 + CHARTAB_SIZE_BITS_3),
    CHARTAB_SIZE_BITS_3,
    0 };

#define CHARTAB_IDX(c, depth, min_char)		\
  (((c) - (min_char)) >> chartab_bits[(depth)])

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

/* Preamble for uniprop (Unicode character property) tables.  See the
   comment of "Unicode character property tables".  */

/* Purpose of uniprop tables. */
static Lisp_Object Qchar_code_property_table;

/* Types of decoder and encoder functions for uniprop values.  */
typedef Lisp_Object (*uniprop_decoder_t) (Lisp_Object, Lisp_Object);
typedef Lisp_Object (*uniprop_encoder_t) (Lisp_Object, Lisp_Object);

static Lisp_Object uniprop_table_uncompress (Lisp_Object, int);
static uniprop_decoder_t uniprop_get_decoder (Lisp_Object);

/* 1 iff TABLE is a uniprop table.  */
#define UNIPROP_TABLE_P(TABLE)					\
  (EQ (XCHAR_TABLE (TABLE)->purpose, Qchar_code_property_table)	\
   && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (TABLE)) == 5)

/* Return a decoder for values in the uniprop table TABLE.  */
#define UNIPROP_GET_DECODER(TABLE)	\
  (UNIPROP_TABLE_P (TABLE) ? uniprop_get_decoder (TABLE) : NULL)
78

79 80 81 82 83 84 85 86 87
/* Nonzero iff OBJ is a string representing uniprop values of 128
   succeeding characters (the bottom level of a char-table) by a
   compressed format.  We are sure that no property value has a string
   starting with '\001' nor '\002'.  */
#define UNIPROP_COMPRESSED_FORM_P(OBJ)	\
  (STRINGP (OBJ) && SCHARS (OBJ) > 0	\
   && ((SREF (OBJ, 0) == 1 || (SREF (OBJ, 0) == 2))))


Paul Eggert's avatar
Paul Eggert committed
88
DEFUN ("make-char-table", Fmake_char_table, Smake_char_table, 1, 2, 0,
89
       doc: /* Return a newly created char-table, with purpose PURPOSE.
90 91
Each element is initialized to INIT, which defaults to nil.

92 93 94 95
PURPOSE should be a symbol.  If it has a `char-table-extra-slots'
property, the property's value should be an integer between 0 and 10
that specifies how many extra slots the char-table has.  Otherwise,
the char-table has no extra slot.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
96
  (register Lisp_Object purpose, Lisp_Object init)
97 98 99
{
  Lisp_Object vector;
  Lisp_Object n;
100
  int n_extras;
101 102 103
  int size;

  CHECK_SYMBOL (purpose);
104 105 106 107
  n = Fget (purpose, Qchar_table_extra_slots);
  if (NILP (n))
    n_extras = 0;
  else
108
    {
109
      CHECK_NATNUM (n);
110
      if (XINT (n) > 10)
111
	args_out_of_range (n, Qnil);
112
      n_extras = XINT (n);
113 114 115 116
    }

  size = VECSIZE (struct Lisp_Char_Table) - 1 + n_extras;
  vector = Fmake_vector (make_number (size), init);
Miles Bader's avatar
Miles Bader committed
117
  XSETPVECTYPE (XVECTOR (vector), PVEC_CHAR_TABLE);
118 119
  CSET (XCHAR_TABLE (vector), parent, Qnil);
  CSET (XCHAR_TABLE (vector), purpose, purpose);
120 121 122 123 124
  XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
  return vector;
}

static Lisp_Object
125
make_sub_char_table (int depth, int min_char, Lisp_Object defalt)
126 127 128 129 130
{
  Lisp_Object table;
  int size = VECSIZE (struct Lisp_Sub_Char_Table) - 1 + chartab_size[depth];

  table = Fmake_vector (make_number (size), defalt);
Miles Bader's avatar
Miles Bader committed
131
  XSETPVECTYPE (XVECTOR (table), PVEC_SUB_CHAR_TABLE);
132 133 134 135 136 137 138
  XSUB_CHAR_TABLE (table)->depth = make_number (depth);
  XSUB_CHAR_TABLE (table)->min_char = make_number (min_char);

  return table;
}

static Lisp_Object
139
char_table_ascii (Lisp_Object table)
140
{
141
  Lisp_Object sub, val;
142 143

  sub = XCHAR_TABLE (table)->contents[0];
144 145
  if (! SUB_CHAR_TABLE_P (sub))
    return sub;
146
  sub = XSUB_CHAR_TABLE (sub)->contents[0];
147 148
  if (! SUB_CHAR_TABLE_P (sub))
    return sub;
149 150 151 152
  val = XSUB_CHAR_TABLE (sub)->contents[0];
  if (UNIPROP_TABLE_P (table) && UNIPROP_COMPRESSED_FORM_P (val))
    val = uniprop_table_uncompress (sub, 0);
  return val;
153 154
}

155
static Lisp_Object
156
copy_sub_char_table (Lisp_Object table)
157 158 159 160 161 162 163 164 165 166 167 168 169
{
  Lisp_Object copy;
  int depth = XINT (XSUB_CHAR_TABLE (table)->depth);
  int min_char = XINT (XSUB_CHAR_TABLE (table)->min_char);
  Lisp_Object val;
  int i;

  copy = make_sub_char_table (depth, min_char, Qnil);
  /* Recursively copy any sub char-tables.  */
  for (i = 0; i < chartab_size[depth]; i++)
    {
      val = XSUB_CHAR_TABLE (table)->contents[i];
      if (SUB_CHAR_TABLE_P (val))
170
	sub_char_table_set_contents (copy, i, copy_sub_char_table (val));
171
      else
172
	sub_char_table_set_contents (copy, i, val);
173 174 175 176 177 178 179
    }

  return copy;
}


Lisp_Object
180
copy_char_table (Lisp_Object table)
181 182
{
  Lisp_Object copy;
183
  int size = XCHAR_TABLE (table)->header.size & PSEUDOVECTOR_SIZE_MASK;
184 185 186
  int i;

  copy = Fmake_vector (make_number (size), Qnil);
Miles Bader's avatar
Miles Bader committed
187
  XSETPVECTYPE (XVECTOR (copy), PVEC_CHAR_TABLE);
188 189 190
  CSET (XCHAR_TABLE (copy), defalt, XCHAR_TABLE (table)->defalt);
  CSET (XCHAR_TABLE (copy), parent, XCHAR_TABLE (table)->parent);
  CSET (XCHAR_TABLE (copy), purpose, XCHAR_TABLE (table)->purpose);
191
  for (i = 0; i < chartab_size[0]; i++)
192 193 194 195 196 197
    char_table_set_contents
      (copy, i, 
       (SUB_CHAR_TABLE_P (XCHAR_TABLE (table)->contents[i])
	? copy_sub_char_table (XCHAR_TABLE (table)->contents[i])
	: XCHAR_TABLE (table)->contents[i]));
  CSET (XCHAR_TABLE (copy), ascii, char_table_ascii (copy));
198 199
  size -= VECSIZE (struct Lisp_Char_Table) - 1;
  for (i = 0; i < size; i++)
200
    char_table_set_extras (copy, i, XCHAR_TABLE (table)->extras[i]);
201 202 203 204 205

  XSETCHAR_TABLE (copy, XCHAR_TABLE (copy));
  return copy;
}

Andreas Schwab's avatar
Andreas Schwab committed
206
static Lisp_Object
207
sub_char_table_ref (Lisp_Object table, int c, int is_uniprop)
208 209 210 211 212
{
  struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
  int depth = XINT (tbl->depth);
  int min_char = XINT (tbl->min_char);
  Lisp_Object val;
213
  int idx = CHARTAB_IDX (c, depth, min_char);
214

215 216 217
  val = tbl->contents[idx];
  if (is_uniprop && UNIPROP_COMPRESSED_FORM_P (val))
    val = uniprop_table_uncompress (table, idx);
218
  if (SUB_CHAR_TABLE_P (val))
219
    val = sub_char_table_ref (val, c, is_uniprop);
220 221 222 223
  return val;
}

Lisp_Object
224
char_table_ref (Lisp_Object table, int c)
225 226 227 228 229 230 231 232 233 234 235 236 237 238
{
  struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
  Lisp_Object val;

  if (ASCII_CHAR_P (c))
    {
      val = tbl->ascii;
      if (SUB_CHAR_TABLE_P (val))
	val = XSUB_CHAR_TABLE (val)->contents[c];
    }
  else
    {
      val = tbl->contents[CHARTAB_IDX (c, 0, 0)];
      if (SUB_CHAR_TABLE_P (val))
239
	val = sub_char_table_ref (val, c, UNIPROP_TABLE_P (table));
240 241 242 243 244 245 246 247
    }
  if (NILP (val))
    {
      val = tbl->defalt;
      if (NILP (val) && CHAR_TABLE_P (tbl->parent))
	val = char_table_ref (tbl->parent, c);
    }
  return val;
Kenichi Handa's avatar
Kenichi Handa committed
248
}
249 250

static Lisp_Object
251 252
sub_char_table_ref_and_range (Lisp_Object table, int c, int *from, int *to,
			      Lisp_Object defalt, int is_uniprop)
253 254 255 256
{
  struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
  int depth = XINT (tbl->depth);
  int min_char = XINT (tbl->min_char);
257
  int chartab_idx = CHARTAB_IDX (c, depth, min_char), idx;
258
  Lisp_Object val;
Kenichi Handa's avatar
Kenichi Handa committed
259

260
  val = tbl->contents[chartab_idx];
261 262
  if (is_uniprop && UNIPROP_COMPRESSED_FORM_P (val))
    val = uniprop_table_uncompress (table, chartab_idx);
263
  if (SUB_CHAR_TABLE_P (val))
264
    val = sub_char_table_ref_and_range (val, c, from, to, defalt, is_uniprop);
265 266 267
  else if (NILP (val))
    val = defalt;

268
  idx = chartab_idx;
269
  while (idx > 0 && *from < min_char + idx * chartab_chars[depth])
270
    {
271 272
      Lisp_Object this_val;

273 274 275
      c = min_char + idx * chartab_chars[depth] - 1;
      idx--;
      this_val = tbl->contents[idx];
276 277
      if (is_uniprop && UNIPROP_COMPRESSED_FORM_P (this_val))
	this_val = uniprop_table_uncompress (table, idx);
278
      if (SUB_CHAR_TABLE_P (this_val))
279 280
	this_val = sub_char_table_ref_and_range (this_val, c, from, to, defalt,
						 is_uniprop);
281 282 283 284
      else if (NILP (this_val))
	this_val = defalt;

      if (! EQ (this_val, val))
285 286 287 288
	{
	  *from = c + 1;
	  break;
	}
289
    }
290 291 292
  while (((c = (chartab_idx + 1) * chartab_chars[depth])
	  < chartab_chars[depth - 1])
	 && (c += min_char) <= *to)
293
    {
294 295
      Lisp_Object this_val;

296 297
      chartab_idx++;
      this_val = tbl->contents[chartab_idx];
298 299
      if (is_uniprop && UNIPROP_COMPRESSED_FORM_P (this_val))
	this_val = uniprop_table_uncompress (table, chartab_idx);
300
      if (SUB_CHAR_TABLE_P (this_val))
301 302
	this_val = sub_char_table_ref_and_range (this_val, c, from, to, defalt,
						 is_uniprop);
303 304 305
      else if (NILP (this_val))
	this_val = defalt;
      if (! EQ (this_val, val))
306 307 308 309
	{
	  *to = c - 1;
	  break;
	}
310
    }
311

312 313 314 315
  return val;
}


316 317 318 319
/* Return the value for C in char-table TABLE.  Shrink the range *FROM
   and *TO to cover characters (containing C) that have the same value
   as C.  It is not assured that the values of (*FROM - 1) and (*TO +
   1) are different from that of C.  */
320

321
Lisp_Object
322
char_table_ref_and_range (Lisp_Object table, int c, int *from, int *to)
323 324
{
  struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
325
  int chartab_idx = CHARTAB_IDX (c, 0, 0), idx;
326
  Lisp_Object val;
327
  int is_uniprop = UNIPROP_TABLE_P (table);
328

329
  val = tbl->contents[chartab_idx];
330 331 332 333
  if (*from < 0)
    *from = 0;
  if (*to < 0)
    *to = MAX_CHAR;
334 335
  if (is_uniprop && UNIPROP_COMPRESSED_FORM_P (val))
    val = uniprop_table_uncompress (table, chartab_idx);
336
  if (SUB_CHAR_TABLE_P (val))
337 338
    val = sub_char_table_ref_and_range (val, c, from, to, tbl->defalt,
					is_uniprop);
339 340
  else if (NILP (val))
    val = tbl->defalt;
341
  idx = chartab_idx;
342
  while (*from < idx * chartab_chars[0])
343
    {
344 345
      Lisp_Object this_val;

346 347 348
      c = idx * chartab_chars[0] - 1;
      idx--;
      this_val = tbl->contents[idx];
349 350
      if (is_uniprop && UNIPROP_COMPRESSED_FORM_P (this_val))
	this_val = uniprop_table_uncompress (table, idx);
351
      if (SUB_CHAR_TABLE_P (this_val))
352
	this_val = sub_char_table_ref_and_range (this_val, c, from, to,
353
						 tbl->defalt, is_uniprop);
354 355 356 357
      else if (NILP (this_val))
	this_val = tbl->defalt;

      if (! EQ (this_val, val))
358 359 360 361
	{
	  *from = c + 1;
	  break;
	}
362
    }
363
  while (*to >= (chartab_idx + 1) * chartab_chars[0])
364
    {
365 366
      Lisp_Object this_val;

367 368 369
      chartab_idx++;
      c = chartab_idx * chartab_chars[0];
      this_val = tbl->contents[chartab_idx];
370 371
      if (is_uniprop && UNIPROP_COMPRESSED_FORM_P (this_val))
	this_val = uniprop_table_uncompress (table, chartab_idx);
372
      if (SUB_CHAR_TABLE_P (this_val))
373
	this_val = sub_char_table_ref_and_range (this_val, c, from, to,
374
						 tbl->defalt, is_uniprop);
375 376 377
      else if (NILP (this_val))
	this_val = tbl->defalt;
      if (! EQ (this_val, val))
378 379 380 381
	{
	  *to = c - 1;
	  break;
	}
382 383 384
    }

  return val;
385
}
386 387 388


static void
389
sub_char_table_set (Lisp_Object table, int c, Lisp_Object val, int is_uniprop)
390 391 392 393 394 395
{
  struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
  int depth = XINT ((tbl)->depth);
  int min_char = XINT ((tbl)->min_char);
  int i = CHARTAB_IDX (c, depth, min_char);
  Lisp_Object sub;
Kenichi Handa's avatar
Kenichi Handa committed
396

397
  if (depth == 3)
398
    sub_char_table_set_contents (table, i, val);
399 400 401 402 403
  else
    {
      sub = tbl->contents[i];
      if (! SUB_CHAR_TABLE_P (sub))
	{
404 405 406 407 408 409 410
	  if (is_uniprop && UNIPROP_COMPRESSED_FORM_P (sub))
	    sub = uniprop_table_uncompress (table, i);
	  else
	    {
	      sub = make_sub_char_table (depth + 1,
					 min_char + i * chartab_chars[depth],
					 sub);
411
	      sub_char_table_set_contents (table, i, sub);
412
	    }
413
	}
414
      sub_char_table_set (sub, c, val, is_uniprop);
415 416 417 418
    }
}

Lisp_Object
419
char_table_set (Lisp_Object table, int c, Lisp_Object val)
420 421 422 423 424
{
  struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);

  if (ASCII_CHAR_P (c)
      && SUB_CHAR_TABLE_P (tbl->ascii))
425
    sub_char_table_set_contents (tbl->ascii, c, val);
426 427 428 429 430 431 432 433 434
  else
    {
      int i = CHARTAB_IDX (c, 0, 0);
      Lisp_Object sub;

      sub = tbl->contents[i];
      if (! SUB_CHAR_TABLE_P (sub))
	{
	  sub = make_sub_char_table (1, i * chartab_chars[0], sub);
435
	  char_table_set_contents (table, i, sub);
436
	}
437
      sub_char_table_set (sub, c, val, UNIPROP_TABLE_P (table));
438
      if (ASCII_CHAR_P (c))
439
	CSET (tbl, ascii, char_table_ascii (table));
440 441 442 443 444
    }
  return val;
}

static void
445 446
sub_char_table_set_range (Lisp_Object table, int from, int to, Lisp_Object val,
			  int is_uniprop)
447
{
448 449 450 451 452 453 454 455 456 457
  struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
  int depth = XINT ((tbl)->depth);
  int min_char = XINT ((tbl)->min_char);
  int chars_in_block = chartab_chars[depth];
  int i, c, lim = chartab_size[depth];

  if (from < min_char)
    from = min_char;
  i = CHARTAB_IDX (from, depth, min_char);
  c = min_char + chars_in_block * i;
458
  for (; i < lim; i++, c += chars_in_block)
459
    {
460 461 462
      if (c > to)
	break;
      if (from <= c && c + chars_in_block - 1 <= to)
463
	sub_char_table_set_contents (table, i, val);
464 465 466 467 468 469 470 471 472 473
      else
	{
	  Lisp_Object sub = tbl->contents[i];
	  if (! SUB_CHAR_TABLE_P (sub))
	    {
	      if (is_uniprop && UNIPROP_COMPRESSED_FORM_P (sub))
		sub = uniprop_table_uncompress (table, i);
	      else
		{
		  sub = make_sub_char_table (depth + 1, c, sub);
474
		  sub_char_table_set_contents (table, i, sub);
475 476 477 478
		}
	    }
	  sub_char_table_set_range (sub, from, to, val, is_uniprop);
	}
479 480 481 482 483
    }
}


Lisp_Object
484
char_table_set_range (Lisp_Object table, int from, int to, Lisp_Object val)
485 486 487 488 489 490 491
{
  struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);

  if (from == to)
    char_table_set (table, from, val);
  else
    {
492 493 494 495 496 497 498 499 500 501
      int is_uniprop = UNIPROP_TABLE_P (table);
      int lim = CHARTAB_IDX (to, 0, 0);
      int i, c;

      for (i = CHARTAB_IDX (from, 0, 0), c = 0; i <= lim;
	   i++, c += chartab_chars[0])
	{
	  if (c > to)
	    break;
	  if (from <= c && c + chartab_chars[0] - 1 <= to)
502
	    char_table_set_contents (table, i, val);
503 504 505 506 507 508
	  else
	    {
	      Lisp_Object sub = tbl->contents[i];
	      if (! SUB_CHAR_TABLE_P (sub))
		{
		  sub = make_sub_char_table (1, i * chartab_chars[0], sub);
509
		  char_table_set_contents (table, i, sub);
510 511 512 513
		}
	      sub_char_table_set_range (sub, from, to, val, is_uniprop);
	    }
	}
514
      if (ASCII_CHAR_P (from))
515
	CSET (tbl, ascii, char_table_ascii (table));
516 517 518 519 520 521 522 523 524
    }
  return val;
}


DEFUN ("char-table-subtype", Fchar_table_subtype, Schar_table_subtype,
       1, 1, 0,
       doc: /*
Return the subtype of char-table CHAR-TABLE.  The value is a symbol.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
525
  (Lisp_Object char_table)
526 527 528 529 530 531 532 533 534 535 536 537 538
{
  CHECK_CHAR_TABLE (char_table);

  return XCHAR_TABLE (char_table)->purpose;
}

DEFUN ("char-table-parent", Fchar_table_parent, Schar_table_parent,
       1, 1, 0,
       doc: /* Return the parent char-table of CHAR-TABLE.
The value is either nil or another char-table.
If CHAR-TABLE holds nil for a given character,
then the actual applicable value is inherited from the parent char-table
\(or from its parents, if necessary).  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
539
  (Lisp_Object char_table)
540 541 542 543 544 545
{
  CHECK_CHAR_TABLE (char_table);

  return XCHAR_TABLE (char_table)->parent;
}

Paul Eggert's avatar
Paul Eggert committed
546
DEFUN ("set-char-table-parent", Fset_char_table_parent, Sset_char_table_parent,
547 548
       2, 2, 0,
       doc: /* Set the parent char-table of CHAR-TABLE to PARENT.
Kenichi Handa's avatar
Kenichi Handa committed
549
Return PARENT.  PARENT must be either nil or another char-table.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
550
  (Lisp_Object char_table, Lisp_Object parent)
551 552 553 554 555 556 557 558 559 560 561 562 563 564
{
  Lisp_Object temp;

  CHECK_CHAR_TABLE (char_table);

  if (!NILP (parent))
    {
      CHECK_CHAR_TABLE (parent);

      for (temp = parent; !NILP (temp); temp = XCHAR_TABLE (temp)->parent)
	if (EQ (temp, char_table))
	  error ("Attempt to make a chartable be its own parent");
    }

565
  CSET (XCHAR_TABLE (char_table), parent, parent);
566 567 568 569

  return parent;
}

Paul Eggert's avatar
Paul Eggert committed
570
DEFUN ("char-table-extra-slot", Fchar_table_extra_slot, Schar_table_extra_slot,
571 572
       2, 2, 0,
       doc: /* Return the value of CHAR-TABLE's extra-slot number N.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
573
  (Lisp_Object char_table, Lisp_Object n)
574 575 576 577 578 579 580 581 582 583
{
  CHECK_CHAR_TABLE (char_table);
  CHECK_NUMBER (n);
  if (XINT (n) < 0
      || XINT (n) >= CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (char_table)))
    args_out_of_range (char_table, n);

  return XCHAR_TABLE (char_table)->extras[XINT (n)];
}

Paul Eggert's avatar
Paul Eggert committed
584
DEFUN ("set-char-table-extra-slot", Fset_char_table_extra_slot,
585 586 587
       Sset_char_table_extra_slot,
       3, 3, 0,
       doc: /* Set CHAR-TABLE's extra-slot number N to VALUE.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
588
  (Lisp_Object char_table, Lisp_Object n, Lisp_Object value)
589 590 591 592 593 594 595
{
  CHECK_CHAR_TABLE (char_table);
  CHECK_NUMBER (n);
  if (XINT (n) < 0
      || XINT (n) >= CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (char_table)))
    args_out_of_range (char_table, n);

596 597
  char_table_set_extras (char_table, XINT (n), value);
  return value;
598 599 600 601 602 603 604
}

DEFUN ("char-table-range", Fchar_table_range, Schar_table_range,
       2, 2, 0,
       doc: /* Return the value in CHAR-TABLE for a range of characters RANGE.
RANGE should be nil (for the default value),
a cons of character codes (for characters in the range), or a character code.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
605
  (Lisp_Object char_table, Lisp_Object range)
606 607 608 609 610 611
{
  Lisp_Object val;
  CHECK_CHAR_TABLE (char_table);

  if (EQ (range, Qnil))
    val = XCHAR_TABLE (char_table)->defalt;
612 613
  else if (CHARACTERP (range))
    val = CHAR_TABLE_REF (char_table, XFASTINT (range));
614 615 616 617
  else if (CONSP (range))
    {
      int from, to;

Kenichi Handa's avatar
Kenichi Handa committed
618 619
      CHECK_CHARACTER_CAR (range);
      CHECK_CHARACTER_CDR (range);
620 621 622
      from = XFASTINT (XCAR (range));
      to = XFASTINT (XCDR (range));
      val = char_table_ref_and_range (char_table, from, &from, &to);
623 624 625 626 627 628 629
      /* Not yet implemented. */
    }
  else
    error ("Invalid RANGE argument to `char-table-range'");
  return val;
}

Paul Eggert's avatar
Paul Eggert committed
630
DEFUN ("set-char-table-range", Fset_char_table_range, Sset_char_table_range,
631
       3, 3, 0,
Kenichi Handa's avatar
Kenichi Handa committed
632
       doc: /* Set the value in CHAR-TABLE for a range of characters RANGE to VALUE.
633
RANGE should be t (for all characters), nil (for the default value),
Kenichi Handa's avatar
Kenichi Handa committed
634 635
a cons of character codes (for characters in the range),
or a character code.  Return VALUE.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
636
  (Lisp_Object char_table, Lisp_Object range, Lisp_Object value)
637 638 639 640 641 642
{
  CHECK_CHAR_TABLE (char_table);
  if (EQ (range, Qt))
    {
      int i;

643
      CSET (XCHAR_TABLE (char_table), ascii, value);
644
      for (i = 0; i < chartab_size[0]; i++)
645
	char_table_set_contents (char_table, i, value);
646 647
    }
  else if (EQ (range, Qnil))
648
    CSET (XCHAR_TABLE (char_table), defalt, value);
649
  else if (CHARACTERP (range))
650 651 652
    char_table_set (char_table, XINT (range), value);
  else if (CONSP (range))
    {
Kenichi Handa's avatar
Kenichi Handa committed
653 654
      CHECK_CHARACTER_CAR (range);
      CHECK_CHARACTER_CDR (range);
655 656 657 658 659 660 661 662 663 664 665 666
      char_table_set_range (char_table,
			    XINT (XCAR (range)), XINT (XCDR (range)), value);
    }
  else
    error ("Invalid RANGE argument to `set-char-table-range'");

  return value;
}

DEFUN ("set-char-table-default", Fset_char_table_default,
       Sset_char_table_default, 3, 3, 0,
       doc: /*
667
This function is obsolete and has no effect.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
668
  (Lisp_Object char_table, Lisp_Object ch, Lisp_Object value)
669 670 671 672 673
{
  return Qnil;
}

/* Look up the element in TABLE at index CH, and return it as an
674
   integer.  If the element is not a character, return CH itself.  */
675 676

int
677
char_table_translate (Lisp_Object table, int ch)
678 679 680
{
  Lisp_Object value;
  value = Faref (table, make_number (ch));
681
  if (! CHARACTERP (value))
682 683 684 685 686
    return ch;
  return XINT (value);
}

static Lisp_Object
687
optimize_sub_char_table (Lisp_Object table, Lisp_Object test)
688 689 690 691
{
  struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
  int depth = XINT (tbl->depth);
  Lisp_Object elt, this;
692
  int i, optimizable;
693 694 695

  elt = XSUB_CHAR_TABLE (table)->contents[0];
  if (SUB_CHAR_TABLE_P (elt))
696 697
    elt = XSUB_CHAR_TABLE (table)->contents[0]
      = optimize_sub_char_table (elt, test);
698
  optimizable = SUB_CHAR_TABLE_P (elt) ? 0 : 1;
699 700 701 702 703
  for (i = 1; i < chartab_size[depth]; i++)
    {
      this = XSUB_CHAR_TABLE (table)->contents[i];
      if (SUB_CHAR_TABLE_P (this))
	this = XSUB_CHAR_TABLE (table)->contents[i]
704
	  = optimize_sub_char_table (this, test);
705 706
      if (optimizable
	  && (NILP (test) ? NILP (Fequal (this, elt)) /* defaults to `equal'. */
707 708
	      : EQ (test, Qeq) ? !EQ (this, elt)      /* Optimize `eq' case.  */
	      : NILP (call2 (test, this, elt))))
709
	optimizable = 0;
710 711
    }

712
  return (optimizable ? elt : table);
713 714
}

Paul Eggert's avatar
Paul Eggert committed
715
DEFUN ("optimize-char-table", Foptimize_char_table, Soptimize_char_table,
716 717 718 719
       1, 2, 0,
       doc: /* Optimize CHAR-TABLE.
TEST is the comparison function used to decide whether two entries are
equivalent and can be merged.  It defaults to `equal'.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
720
  (Lisp_Object char_table, Lisp_Object test)
721 722 723 724 725 726 727 728 729 730
{
  Lisp_Object elt;
  int i;

  CHECK_CHAR_TABLE (char_table);

  for (i = 0; i < chartab_size[0]; i++)
    {
      elt = XCHAR_TABLE (char_table)->contents[i];
      if (SUB_CHAR_TABLE_P (elt))
731 732
	char_table_set_contents
	  (char_table, i, optimize_sub_char_table (elt, test));
733
    }
734
  /* Reset the `ascii' cache, in case it got optimized away.  */
735
  CSET (XCHAR_TABLE (char_table), ascii, char_table_ascii (char_table));
736

737 738 739 740
  return Qnil;
}


741 742 743
/* Map C_FUNCTION or FUNCTION over TABLE (top or sub char-table),
   calling it for each character or group of characters that share a
   value.  RANGE is a cons (FROM . TO) specifying the range of target
744
   characters, VAL is a value of FROM in TABLE, TOP is the top
745 746 747 748 749
   char-table.

   ARG is passed to C_FUNCTION when that is called.

   It returns the value of last character covered by TABLE (not the
Paul Eggert's avatar
Paul Eggert committed
750
   value inherited from the parent), and by side-effect, the car part
751 752 753
   of RANGE is updated to the minimum character C where C and all the
   following characters in TABLE have the same value.  */

754
static Lisp_Object
755 756
map_sub_char_table (void (*c_function) (Lisp_Object, Lisp_Object, Lisp_Object),
		    Lisp_Object function, Lisp_Object table, Lisp_Object arg, Lisp_Object val,
757
		    Lisp_Object range, Lisp_Object top)
758
{
759 760
  /* Depth of TABLE.  */
  int depth;
Juanma Barranquero's avatar
Juanma Barranquero committed
761
  /* Minimum and maximum characters covered by TABLE. */
762 763 764 765
  int min_char, max_char;
  /* Number of characters covered by one element of TABLE.  */
  int chars_in_block;
  int from = XINT (XCAR (range)), to = XINT (XCDR (range));
766
  int i, c;
767 768
  int is_uniprop = UNIPROP_TABLE_P (top);
  uniprop_decoder_t decoder = UNIPROP_GET_DECODER (top);
769

770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
  if (SUB_CHAR_TABLE_P (table))
    {
      struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);

      depth = XINT (tbl->depth);
      min_char = XINT (tbl->min_char);
      max_char = min_char + chartab_chars[depth - 1] - 1;
    }
  else
    {
      depth = 0;
      min_char = 0;
      max_char = MAX_CHAR;
    }
  chars_in_block = chartab_chars[depth];

  if (to < max_char)
    max_char = to;
  /* Set I to the index of the first element to check.  */
  if (from <= min_char)
    i = 0;
  else
    i = (from - min_char) / chars_in_block;
  for (c = min_char + chars_in_block * i; c <= max_char;
       i++, c += chars_in_block)
795
    {
796 797 798
      Lisp_Object this = (SUB_CHAR_TABLE_P (table)
			  ? XSUB_CHAR_TABLE (table)->contents[i]
			  : XCHAR_TABLE (table)->contents[i]);
799
      int nextc = c + chars_in_block;
800

801 802
      if (is_uniprop && UNIPROP_COMPRESSED_FORM_P (this))
	this = uniprop_table_uncompress (table, i);
803
      if (SUB_CHAR_TABLE_P (this))
804 805 806 807
	{
	  if (to >= nextc)
	    XSETCDR (range, make_number (nextc - 1));
	  val = map_sub_char_table (c_function, function, this, arg,
808
				    val, range, top);
809
	}
810
      else
811
	{
812
	  if (NILP (this))
813
	    this = XCHAR_TABLE (top)->defalt;
814
	  if (!EQ (val, this))
815
	    {
816 817 818 819
	      int different_value = 1;

	      if (NILP (val))
		{
820
		  if (! NILP (XCHAR_TABLE (top)->parent))
821
		    {
822
		      Lisp_Object parent = XCHAR_TABLE (top)->parent;
823 824 825 826
		      Lisp_Object temp = XCHAR_TABLE (parent)->parent;

		      /* This is to get a value of FROM in PARENT
			 without checking the parent of PARENT.  */
827
		      CSET (XCHAR_TABLE (parent), parent, Qnil);
828
		      val = CHAR_TABLE_REF (parent, from);
829
		      CSET (XCHAR_TABLE (parent), parent, temp);
830 831 832
		      XSETCDR (range, make_number (c - 1));
		      val = map_sub_char_table (c_function, function,
						parent, arg, val, range,
833
						parent);
834
		      if (EQ (val, this))
835 836 837 838
			different_value = 0;
		    }
		}
	      if (! NILP (val) && different_value)
839
		{
Kenichi Handa's avatar
Kenichi Handa committed
840
		  XSETCDR (range, make_number (c - 1));
841
		  if (EQ (XCAR (range), XCDR (range)))
842 843 844 845
		    {
		      if (c_function)
			(*c_function) (arg, XCAR (range), val);
		      else
846 847 848 849 850
			{
			  if (decoder)
			    val = decoder (top, val);
			  call2 (function, XCAR (range), val);
			}
851
		    }
852
		  else
853 854 855 856
		    {
		      if (c_function)
			(*c_function) (arg, range, val);
		      else
857 858 859 860 861
			{
			  if (decoder)
			    val = decoder (top, val);
			  call2 (function, range, val);
			}
862
		    }
863
		}
864
	      val = this;
865
	      from = c;
Kenichi Handa's avatar
Kenichi Handa committed
866
	      XSETCAR (range, make_number (c));
867 868
	    }
	}
869
      XSETCDR (range, make_number (to));
870 871 872 873 874 875 876 877
    }
  return val;
}


/* Map C_FUNCTION or FUNCTION over TABLE, calling it for each
   character or group of characters that share a value.

Kenichi Handa's avatar
Kenichi Handa committed
878
   ARG is passed to C_FUNCTION when that is called.  */
879 880

void
881 882
map_char_table (void (*c_function) (Lisp_Object, Lisp_Object, Lisp_Object),
		Lisp_Object function, Lisp_Object table, Lisp_Object arg)
883
{
884 885 886
  Lisp_Object range, val, parent;
  struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
  uniprop_decoder_t decoder = UNIPROP_GET_DECODER (table);
887

888
  range = Fcons (make_number (0), make_number (MAX_CHAR));
889 890 891
  parent = XCHAR_TABLE (table)->parent;

  GCPRO4 (table, arg, range, parent);
892 893 894
  val = XCHAR_TABLE (table)->ascii;
  if (SUB_CHAR_TABLE_P (val))
    val = XSUB_CHAR_TABLE (val)->contents[0];
895
  val = map_sub_char_table (c_function, function, table, arg, val, range,
896 897
			    table);

898 899 900
  /* If VAL is nil and TABLE has a parent, we must consult the parent
     recursively.  */
  while (NILP (val) && ! NILP (XCHAR_TABLE (table)->parent))
901
    {
902
      Lisp_Object temp;
903 904
      int from = XINT (XCAR (range));

905 906
      parent = XCHAR_TABLE (table)->parent;
      temp = XCHAR_TABLE (parent)->parent;
907 908
      /* This is to get a value of FROM in PARENT without checking the
	 parent of PARENT.  */
909
      CSET (XCHAR_TABLE (parent), parent, Qnil);
910
      val = CHAR_TABLE_REF (parent, from);
911
      CSET (XCHAR_TABLE (parent), parent, temp);
912
      val = map_sub_char_table (c_function, function, parent, arg, val, range,
913
				parent);
914
      table = parent;
915
    }
916 917 918

  if (! NILP (val))
    {
919 920 921 922 923
      if (EQ (XCAR (range), XCDR (range)))
	{
	  if (c_function)
	    (*c_function) (arg, XCAR (range), val);
	  else
924 925 926 927 928
	    {
	      if (decoder)
		val = decoder (table, val);
	      call2 (function, XCAR (range), val);
	    }
929
	}
930
      else
931 932 933 934
	{
	  if (c_function)
	    (*c_function) (arg, range, val);
	  else
935 936 937 938 939
	    {
	      if (decoder)
		val = decoder (table, val);
	      call2 (function, range, val);
	    }
940
	}
941
    }
942 943

  UNGCPRO;
944 945 946 947
}

DEFUN ("map-char-table", Fmap_char_table, Smap_char_table,
  2, 2, 0,
Chong Yidong's avatar
Chong Yidong committed
948 949 950 951 952
       doc: /* Call FUNCTION for each character in CHAR-TABLE that has non-nil value.
FUNCTION is called with two arguments, KEY and VALUE.
KEY is a character code or a cons of character codes specifying a
range of characters that have the same value.
VALUE is what (char-table-range CHAR-TABLE KEY) returns.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
953
  (Lisp_Object function, Lisp_Object char_table)
954 955 956
{
  CHECK_CHAR_TABLE (char_table);

Kenichi Handa's avatar
Kenichi Handa committed
957
  map_char_table (NULL, function, char_table, char_table);
958 959 960
  return Qnil;
}

961 962

static void
963 964 965 966
map_sub_char_table_for_charset (void (*c_function) (Lisp_Object, Lisp_Object),
				Lisp_Object function, Lisp_Object table, Lisp_Object arg,
				Lisp_Object range, struct charset *charset,
				unsigned from, unsigned to)
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
{
  struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
  int depth = XINT (tbl->depth);
  int c, i;

  if (depth < 3)
    for (i = 0, c = XINT (tbl->min_char); i < chartab_size[depth];
	 i++, c += chartab_chars[depth])
      {
	Lisp_Object this;

	this = tbl->contents[i];
	if (SUB_CHAR_TABLE_P (this))
	  map_sub_char_table_for_charset (c_function, function, this, arg,
					  range, charset, from, to);
	else
	  {
	    if (! NILP (XCAR (range)))
	      {
		XSETCDR (range, make_number (c - 1));
		if (c_function)
		  (*c_function) (arg, range);
		else
		  call2 (function, range, arg);
	      }
	    XSETCAR (range, Qnil);
	  }
      }
  else
    for (i = 0, c = XINT (tbl->min_char); i < chartab_size[depth]; i++, c ++)
      {
	Lisp_Object this;
	unsigned code;

	this = tbl->contents[i];
	if (NILP (this)
	    || (charset
		&& (code = ENCODE_CHAR (charset, c),
		    (code < from || code > to))))
	  {
	    if (! NILP (XCAR (range)))
	      {
		XSETCDR (range, make_number (c - 1));
		if (c_function)
1011
		  (*c_function) (arg, range);
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
		else
		  call2 (function, range, arg);
		XSETCAR (range, Qnil);
	      }
	  }
	else
	  {
	    if (NILP (XCAR (range)))
	      XSETCAR (range, make_number (c));
	  }
      }
}


1026
/* Support function for `map-charset-chars'.  Map C_FUNCTION or
Juanma Barranquero's avatar
Juanma Barranquero committed
1027
   FUNCTION over TABLE, calling it for each character or a group of
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
   succeeding characters that have non-nil value in TABLE.  TABLE is a
   "mapping table" or a "deunifier table" of a certain charset.

   If CHARSET is not NULL (this is the case that `map-charset-chars'
   is called with non-nil FROM-CODE and TO-CODE), it is a charset who
   owns TABLE, and the function is called only on a character in the
   range FROM and TO.  FROM and TO are not character codes, but code
   points of a character in CHARSET.

   This function is called in these two cases:

   (1) A charset has a mapping file name in :map property.

   (2) A charset has an upper code space in :offset property and a
   mapping file name in :unify-map property.  In this case, this
   function is called only for characters in the Unicode code space.
   Characters in upper code space are handled directly in
   map_charset_chars.  */

1047
void
1048
map_char_table_for_charset (void (*c_function) (Lisp_Object, Lisp_Object),
1049 1050 1051
			    Lisp_Object