chartab.c 40.6 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

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
/* 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))))

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
static void
CHECK_CHAR_TABLE (Lisp_Object x)
{
  CHECK_TYPE (CHAR_TABLE_P (x), Qchar_table_p, x);
}

static void
set_char_table_ascii (Lisp_Object table, Lisp_Object val)
{
  XCHAR_TABLE (table)->ascii = val;
}
static void
set_char_table_parent (Lisp_Object table, Lisp_Object val)
{
  XCHAR_TABLE (table)->parent = val;
}
103

Paul Eggert's avatar
Paul Eggert committed
104
DEFUN ("make-char-table", Fmake_char_table, Smake_char_table, 1, 2, 0,
105
       doc: /* Return a newly created char-table, with purpose PURPOSE.
106 107
Each element is initialized to INIT, which defaults to nil.

108 109 110 111
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
112
  (register Lisp_Object purpose, Lisp_Object init)
113 114 115
{
  Lisp_Object vector;
  Lisp_Object n;
116
  int n_extras;
117 118 119
  int size;

  CHECK_SYMBOL (purpose);
120 121 122 123
  n = Fget (purpose, Qchar_table_extra_slots);
  if (NILP (n))
    n_extras = 0;
  else
124
    {
125
      CHECK_NATNUM (n);
126
      if (XINT (n) > 10)
127
	args_out_of_range (n, Qnil);
128
      n_extras = XINT (n);
129 130
    }

131
  size = CHAR_TABLE_STANDARD_SLOTS + n_extras;
132
  vector = Fmake_vector (make_number (size), init);
Miles Bader's avatar
Miles Bader committed
133
  XSETPVECTYPE (XVECTOR (vector), PVEC_CHAR_TABLE);
Paul Eggert's avatar
Paul Eggert committed
134 135
  set_char_table_parent (vector, Qnil);
  set_char_table_purpose (vector, purpose);
136 137 138 139 140
  XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
  return vector;
}

static Lisp_Object
141
make_sub_char_table (int depth, int min_char, Lisp_Object defalt)
142 143
{
  Lisp_Object table;
144 145
  int size = (PSEUDOVECSIZE (struct Lisp_Sub_Char_Table, contents)
	      + chartab_size[depth]);
146 147

  table = Fmake_vector (make_number (size), defalt);
Miles Bader's avatar
Miles Bader committed
148
  XSETPVECTYPE (XVECTOR (table), PVEC_SUB_CHAR_TABLE);
149 150 151 152 153 154 155
  XSUB_CHAR_TABLE (table)->depth = make_number (depth);
  XSUB_CHAR_TABLE (table)->min_char = make_number (min_char);

  return table;
}

static Lisp_Object
156
char_table_ascii (Lisp_Object table)
157
{
158
  Lisp_Object sub, val;
159 160

  sub = XCHAR_TABLE (table)->contents[0];
161 162
  if (! SUB_CHAR_TABLE_P (sub))
    return sub;
163
  sub = XSUB_CHAR_TABLE (sub)->contents[0];
164 165
  if (! SUB_CHAR_TABLE_P (sub))
    return sub;
166 167 168 169
  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;
170 171
}

172
static Lisp_Object
173
copy_sub_char_table (Lisp_Object table)
174 175 176
{
  int depth = XINT (XSUB_CHAR_TABLE (table)->depth);
  int min_char = XINT (XSUB_CHAR_TABLE (table)->min_char);
177
  Lisp_Object copy = make_sub_char_table (depth, min_char, Qnil);
178 179 180 181 182
  int i;

  /* Recursively copy any sub char-tables.  */
  for (i = 0; i < chartab_size[depth]; i++)
    {
183 184 185
      Lisp_Object val = XSUB_CHAR_TABLE (table)->contents[i];
      set_sub_char_table_contents
	(copy, i, SUB_CHAR_TABLE_P (val) ? copy_sub_char_table (val) : val);
186 187 188 189 190 191 192
    }

  return copy;
}


Lisp_Object
193
copy_char_table (Lisp_Object table)
194 195
{
  Lisp_Object copy;
196
  int size = XCHAR_TABLE (table)->header.size & PSEUDOVECTOR_SIZE_MASK;
197 198 199
  int i;

  copy = Fmake_vector (make_number (size), Qnil);
Miles Bader's avatar
Miles Bader committed
200
  XSETPVECTYPE (XVECTOR (copy), PVEC_CHAR_TABLE);
Paul Eggert's avatar
Paul Eggert committed
201 202 203
  set_char_table_defalt (copy, XCHAR_TABLE (table)->defalt);
  set_char_table_parent (copy, XCHAR_TABLE (table)->parent);
  set_char_table_purpose (copy, XCHAR_TABLE (table)->purpose);
204
  for (i = 0; i < chartab_size[0]; i++)
205
    set_char_table_contents
Paul Eggert's avatar
Paul Eggert committed
206
      (copy, i,
207 208 209
       (SUB_CHAR_TABLE_P (XCHAR_TABLE (table)->contents[i])
	? copy_sub_char_table (XCHAR_TABLE (table)->contents[i])
	: XCHAR_TABLE (table)->contents[i]));
Paul Eggert's avatar
Paul Eggert committed
210
  set_char_table_ascii (copy, char_table_ascii (copy));
211
  size -= CHAR_TABLE_STANDARD_SLOTS;
212
  for (i = 0; i < size; i++)
213
    set_char_table_extras (copy, i, XCHAR_TABLE (table)->extras[i]);
214 215 216 217 218

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

Andreas Schwab's avatar
Andreas Schwab committed
219
static Lisp_Object
220
sub_char_table_ref (Lisp_Object table, int c, bool is_uniprop)
221 222 223 224 225
{
  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;
226
  int idx = CHARTAB_IDX (c, depth, min_char);
227

228 229 230
  val = tbl->contents[idx];
  if (is_uniprop && UNIPROP_COMPRESSED_FORM_P (val))
    val = uniprop_table_uncompress (table, idx);
231
  if (SUB_CHAR_TABLE_P (val))
232
    val = sub_char_table_ref (val, c, is_uniprop);
233 234 235 236
  return val;
}

Lisp_Object
237
char_table_ref (Lisp_Object table, int c)
238 239 240 241 242 243 244 245 246 247 248 249 250 251
{
  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))
252
	val = sub_char_table_ref (val, c, UNIPROP_TABLE_P (table));
253 254 255 256 257 258 259 260
    }
  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
261
}
262 263

static Lisp_Object
264
sub_char_table_ref_and_range (Lisp_Object table, int c, int *from, int *to,
265
			      Lisp_Object defalt, bool is_uniprop)
266 267 268 269
{
  struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
  int depth = XINT (tbl->depth);
  int min_char = XINT (tbl->min_char);
270
  int chartab_idx = CHARTAB_IDX (c, depth, min_char), idx;
271
  Lisp_Object val;
Kenichi Handa's avatar
Kenichi Handa committed
272

273
  val = tbl->contents[chartab_idx];
274 275
  if (is_uniprop && UNIPROP_COMPRESSED_FORM_P (val))
    val = uniprop_table_uncompress (table, chartab_idx);
276
  if (SUB_CHAR_TABLE_P (val))
277
    val = sub_char_table_ref_and_range (val, c, from, to, defalt, is_uniprop);
278 279 280
  else if (NILP (val))
    val = defalt;

281
  idx = chartab_idx;
282
  while (idx > 0 && *from < min_char + idx * chartab_chars[depth])
283
    {
284 285
      Lisp_Object this_val;

286 287 288
      c = min_char + idx * chartab_chars[depth] - 1;
      idx--;
      this_val = tbl->contents[idx];
289 290
      if (is_uniprop && UNIPROP_COMPRESSED_FORM_P (this_val))
	this_val = uniprop_table_uncompress (table, idx);
291
      if (SUB_CHAR_TABLE_P (this_val))
292 293
	this_val = sub_char_table_ref_and_range (this_val, c, from, to, defalt,
						 is_uniprop);
294 295 296 297
      else if (NILP (this_val))
	this_val = defalt;

      if (! EQ (this_val, val))
298 299 300 301
	{
	  *from = c + 1;
	  break;
	}
302
    }
303 304 305
  while (((c = (chartab_idx + 1) * chartab_chars[depth])
	  < chartab_chars[depth - 1])
	 && (c += min_char) <= *to)
306
    {
307 308
      Lisp_Object this_val;

309 310
      chartab_idx++;
      this_val = tbl->contents[chartab_idx];
311 312
      if (is_uniprop && UNIPROP_COMPRESSED_FORM_P (this_val))
	this_val = uniprop_table_uncompress (table, chartab_idx);
313
      if (SUB_CHAR_TABLE_P (this_val))
314 315
	this_val = sub_char_table_ref_and_range (this_val, c, from, to, defalt,
						 is_uniprop);
316 317 318
      else if (NILP (this_val))
	this_val = defalt;
      if (! EQ (this_val, val))
319 320 321 322
	{
	  *to = c - 1;
	  break;
	}
323
    }
324

325 326 327 328
  return val;
}


329 330 331 332
/* 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.  */
333

334
Lisp_Object
335
char_table_ref_and_range (Lisp_Object table, int c, int *from, int *to)
336 337
{
  struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
338
  int chartab_idx = CHARTAB_IDX (c, 0, 0), idx;
339
  Lisp_Object val;
340
  bool is_uniprop = UNIPROP_TABLE_P (table);
341

342
  val = tbl->contents[chartab_idx];
343 344 345 346
  if (*from < 0)
    *from = 0;
  if (*to < 0)
    *to = MAX_CHAR;
347 348
  if (is_uniprop && UNIPROP_COMPRESSED_FORM_P (val))
    val = uniprop_table_uncompress (table, chartab_idx);
349
  if (SUB_CHAR_TABLE_P (val))
350 351
    val = sub_char_table_ref_and_range (val, c, from, to, tbl->defalt,
					is_uniprop);
352 353
  else if (NILP (val))
    val = tbl->defalt;
354
  idx = chartab_idx;
355
  while (*from < idx * chartab_chars[0])
356
    {
357 358
      Lisp_Object this_val;

359 360 361
      c = idx * chartab_chars[0] - 1;
      idx--;
      this_val = tbl->contents[idx];
362 363
      if (is_uniprop && UNIPROP_COMPRESSED_FORM_P (this_val))
	this_val = uniprop_table_uncompress (table, idx);
364
      if (SUB_CHAR_TABLE_P (this_val))
365
	this_val = sub_char_table_ref_and_range (this_val, c, from, to,
366
						 tbl->defalt, is_uniprop);
367 368 369 370
      else if (NILP (this_val))
	this_val = tbl->defalt;

      if (! EQ (this_val, val))
371 372 373 374
	{
	  *from = c + 1;
	  break;
	}
375
    }
376
  while (*to >= (chartab_idx + 1) * chartab_chars[0])
377
    {
378 379
      Lisp_Object this_val;

380 381 382
      chartab_idx++;
      c = chartab_idx * chartab_chars[0];
      this_val = tbl->contents[chartab_idx];
383 384
      if (is_uniprop && UNIPROP_COMPRESSED_FORM_P (this_val))
	this_val = uniprop_table_uncompress (table, chartab_idx);
385
      if (SUB_CHAR_TABLE_P (this_val))
386
	this_val = sub_char_table_ref_and_range (this_val, c, from, to,
387
						 tbl->defalt, is_uniprop);
388 389 390
      else if (NILP (this_val))
	this_val = tbl->defalt;
      if (! EQ (this_val, val))
391 392 393 394
	{
	  *to = c - 1;
	  break;
	}
395 396 397
    }

  return val;
398
}
399 400 401


static void
402
sub_char_table_set (Lisp_Object table, int c, Lisp_Object val, bool is_uniprop)
403 404 405 406 407 408
{
  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
409

410
  if (depth == 3)
411
    set_sub_char_table_contents (table, i, val);
412 413 414 415 416
  else
    {
      sub = tbl->contents[i];
      if (! SUB_CHAR_TABLE_P (sub))
	{
417 418 419 420 421 422 423
	  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);
424
	      set_sub_char_table_contents (table, i, sub);
425
	    }
426
	}
427
      sub_char_table_set (sub, c, val, is_uniprop);
428 429 430
    }
}

431
void
432
char_table_set (Lisp_Object table, int c, Lisp_Object val)
433 434 435 436 437
{
  struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);

  if (ASCII_CHAR_P (c)
      && SUB_CHAR_TABLE_P (tbl->ascii))
438
    set_sub_char_table_contents (tbl->ascii, c, val);
439 440 441 442 443 444 445 446 447
  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);
448
	  set_char_table_contents (table, i, sub);
449
	}
450
      sub_char_table_set (sub, c, val, UNIPROP_TABLE_P (table));
451
      if (ASCII_CHAR_P (c))
Paul Eggert's avatar
Paul Eggert committed
452
	set_char_table_ascii (table, char_table_ascii (table));
453 454 455 456
    }
}

static void
457
sub_char_table_set_range (Lisp_Object table, int from, int to, Lisp_Object val,
458
			  bool is_uniprop)
459
{
460 461 462 463 464 465 466 467 468 469
  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;
470
  for (; i < lim; i++, c += chars_in_block)
471
    {
472 473 474
      if (c > to)
	break;
      if (from <= c && c + chars_in_block - 1 <= to)
475
	set_sub_char_table_contents (table, i, val);
476 477 478 479 480 481 482 483 484 485
      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);
486
		  set_sub_char_table_contents (table, i, sub);
487 488 489 490
		}
	    }
	  sub_char_table_set_range (sub, from, to, val, is_uniprop);
	}
491 492 493 494
    }
}


495
void
496
char_table_set_range (Lisp_Object table, int from, int to, Lisp_Object val)
497 498 499 500 501 502 503
{
  struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);

  if (from == to)
    char_table_set (table, from, val);
  else
    {
504
      bool is_uniprop = UNIPROP_TABLE_P (table);
505 506 507 508 509 510 511 512 513
      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)
514
	    set_char_table_contents (table, i, val);
515 516 517 518 519 520
	  else
	    {
	      Lisp_Object sub = tbl->contents[i];
	      if (! SUB_CHAR_TABLE_P (sub))
		{
		  sub = make_sub_char_table (1, i * chartab_chars[0], sub);
521
		  set_char_table_contents (table, i, sub);
522 523 524 525
		}
	      sub_char_table_set_range (sub, from, to, val, is_uniprop);
	    }
	}
526
      if (ASCII_CHAR_P (from))
Paul Eggert's avatar
Paul Eggert committed
527
	set_char_table_ascii (table, char_table_ascii (table));
528 529 530 531 532 533 534 535
    }
}


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
536
  (Lisp_Object char_table)
537 538 539 540 541 542 543 544 545 546 547 548 549
{
  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
550
  (Lisp_Object char_table)
551 552 553 554 555 556
{
  CHECK_CHAR_TABLE (char_table);

  return XCHAR_TABLE (char_table)->parent;
}

Paul Eggert's avatar
Paul Eggert committed
557
DEFUN ("set-char-table-parent", Fset_char_table_parent, Sset_char_table_parent,
558 559
       2, 2, 0,
       doc: /* Set the parent char-table of CHAR-TABLE to PARENT.
Kenichi Handa's avatar
Kenichi Handa committed
560
Return PARENT.  PARENT must be either nil or another char-table.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
561
  (Lisp_Object char_table, Lisp_Object parent)
562 563 564 565 566 567 568 569 570 571 572 573 574 575
{
  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");
    }

Paul Eggert's avatar
Paul Eggert committed
576
  set_char_table_parent (char_table, parent);
577 578 579 580

  return parent;
}

Paul Eggert's avatar
Paul Eggert committed
581
DEFUN ("char-table-extra-slot", Fchar_table_extra_slot, Schar_table_extra_slot,
582 583
       2, 2, 0,
       doc: /* Return the value of CHAR-TABLE's extra-slot number N.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
584
  (Lisp_Object char_table, Lisp_Object n)
585 586 587 588 589 590 591 592 593 594
{
  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
595
DEFUN ("set-char-table-extra-slot", Fset_char_table_extra_slot,
596 597 598
       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
599
  (Lisp_Object char_table, Lisp_Object n, Lisp_Object value)
600 601 602 603 604 605 606
{
  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);

607
  set_char_table_extras (char_table, XINT (n), value);
608
  return value;
609 610 611 612 613 614 615
}

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
616
  (Lisp_Object char_table, Lisp_Object range)
617 618 619 620 621 622
{
  Lisp_Object val;
  CHECK_CHAR_TABLE (char_table);

  if (EQ (range, Qnil))
    val = XCHAR_TABLE (char_table)->defalt;
623 624
  else if (CHARACTERP (range))
    val = CHAR_TABLE_REF (char_table, XFASTINT (range));
625 626 627 628
  else if (CONSP (range))
    {
      int from, to;

Kenichi Handa's avatar
Kenichi Handa committed
629 630
      CHECK_CHARACTER_CAR (range);
      CHECK_CHARACTER_CDR (range);
631 632 633
      from = XFASTINT (XCAR (range));
      to = XFASTINT (XCDR (range));
      val = char_table_ref_and_range (char_table, from, &from, &to);
634 635 636 637 638 639 640
      /* Not yet implemented. */
    }
  else
    error ("Invalid RANGE argument to `char-table-range'");
  return val;
}

Paul Eggert's avatar
Paul Eggert committed
641
DEFUN ("set-char-table-range", Fset_char_table_range, Sset_char_table_range,
642
       3, 3, 0,
Kenichi Handa's avatar
Kenichi Handa committed
643
       doc: /* Set the value in CHAR-TABLE for a range of characters RANGE to VALUE.
644
RANGE should be t (for all characters), nil (for the default value),
Kenichi Handa's avatar
Kenichi Handa committed
645 646
a cons of character codes (for characters in the range),
or a character code.  Return VALUE.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
647
  (Lisp_Object char_table, Lisp_Object range, Lisp_Object value)
648 649 650 651 652 653
{
  CHECK_CHAR_TABLE (char_table);
  if (EQ (range, Qt))
    {
      int i;

Paul Eggert's avatar
Paul Eggert committed
654
      set_char_table_ascii (char_table, value);
655
      for (i = 0; i < chartab_size[0]; i++)
656
	set_char_table_contents (char_table, i, value);
657 658
    }
  else if (EQ (range, Qnil))
Paul Eggert's avatar
Paul Eggert committed
659
    set_char_table_defalt (char_table, value);
660
  else if (CHARACTERP (range))
661 662 663
    char_table_set (char_table, XINT (range), value);
  else if (CONSP (range))
    {
Kenichi Handa's avatar
Kenichi Handa committed
664 665
      CHECK_CHARACTER_CAR (range);
      CHECK_CHARACTER_CDR (range);
666 667 668 669 670 671 672 673 674 675
      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;
}

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

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

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

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

719
  return (optimizable ? elt : table);
720 721
}

Paul Eggert's avatar
Paul Eggert committed
722
DEFUN ("optimize-char-table", Foptimize_char_table, Soptimize_char_table,
723 724 725 726
       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
727
  (Lisp_Object char_table, Lisp_Object test)
728 729 730 731 732 733 734 735 736 737
{
  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))
738
	set_char_table_contents
739
	  (char_table, i, optimize_sub_char_table (elt, test));
740
    }
741
  /* Reset the `ascii' cache, in case it got optimized away.  */
Paul Eggert's avatar
Paul Eggert committed
742
  set_char_table_ascii (char_table, char_table_ascii (char_table));
743

744 745 746 747
  return Qnil;
}


748 749 750
/* 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
751
   characters, VAL is a value of FROM in TABLE, TOP is the top
752 753 754 755 756
   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
757
   value inherited from the parent), and by side-effect, the car part
758 759 760
   of RANGE is updated to the minimum character C where C and all the
   following characters in TABLE have the same value.  */

761
static Lisp_Object
762 763
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,
764
		    Lisp_Object range, Lisp_Object top)
765
{
766 767
  /* Depth of TABLE.  */
  int depth;
Juanma Barranquero's avatar
Juanma Barranquero committed
768
  /* Minimum and maximum characters covered by TABLE. */
769 770 771 772
  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));
773
  int i, c;
774
  bool is_uniprop = UNIPROP_TABLE_P (top);
775
  uniprop_decoder_t decoder = UNIPROP_GET_DECODER (top);
776

777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
  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)
802
    {
803 804 805
      Lisp_Object this = (SUB_CHAR_TABLE_P (table)
			  ? XSUB_CHAR_TABLE (table)->contents[i]
			  : XCHAR_TABLE (table)->contents[i]);
806
      int nextc = c + chars_in_block;
807

808 809
      if (is_uniprop && UNIPROP_COMPRESSED_FORM_P (this))
	this = uniprop_table_uncompress (table, i);
810
      if (SUB_CHAR_TABLE_P (this))
811 812 813 814
	{
	  if (to >= nextc)
	    XSETCDR (range, make_number (nextc - 1));
	  val = map_sub_char_table (c_function, function, this, arg,
815
				    val, range, top);
816
	}
817
      else
818
	{
819
	  if (NILP (this))
820
	    this = XCHAR_TABLE (top)->defalt;
821
	  if (!EQ (val, this))
822
	    {
823
	      bool different_value = 1;
824 825 826

	      if (NILP (val))
		{
827
		  if (! NILP (XCHAR_TABLE (top)->parent))
828
		    {
829
		      Lisp_Object parent = XCHAR_TABLE (top)->parent;
830 831 832 833
		      Lisp_Object temp = XCHAR_TABLE (parent)->parent;

		      /* This is to get a value of FROM in PARENT
			 without checking the parent of PARENT.  */
Paul Eggert's avatar
Paul Eggert committed
834
		      set_char_table_parent (parent, Qnil);
835
		      val = CHAR_TABLE_REF (parent, from);
Paul Eggert's avatar
Paul Eggert committed
836
		      set_char_table_parent (parent, temp);
837 838 839
		      XSETCDR (range, make_number (c - 1));
		      val = map_sub_char_table (c_function, function,
						parent, arg, val, range,
840
						parent);
841
		      if (EQ (val, this))
842 843 844 845
			different_value = 0;
		    }
		}
	      if (! NILP (val) && different_value)
846
		{
Kenichi Handa's avatar
Kenichi Handa committed
847
		  XSETCDR (range, make_number (c - 1));
848
		  if (EQ (XCAR (range), XCDR (range)))
849 850 851 852
		    {
		      if (c_function)
			(*c_function) (arg, XCAR (range), val);
		      else
853 854 855 856 857
			{
			  if (decoder)
			    val = decoder (top, val);
			  call2 (function, XCAR (range), val);
			}
858
		    }
859
		  else
860 861 862 863
		    {
		      if (c_function)
			(*c_function) (arg, range, val);
		      else
864 865 866 867 868
			{
			  if (decoder)
			    val = decoder (top, val);
			  call2 (function, range, val);
			}
869
		    }
870
		}
871
	      val = this;
872
	      from = c;
Kenichi Handa's avatar
Kenichi Handa committed
873
	      XSETCAR (range, make_number (c));
874 875
	    }
	}
876
      XSETCDR (range, make_number (to));
877 878 879 880 881 882 883 884
    }
  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
885
   ARG is passed to C_FUNCTION when that is called.  */
886 887

void
888 889
map_char_table (void (*c_function) (Lisp_Object, Lisp_Object, Lisp_Object),
		Lisp_Object function, Lisp_Object table, Lisp_Object arg)
890
{
891 892 893
  Lisp_Object range, val, parent;
  struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
  uniprop_decoder_t decoder = UNIPROP_GET_DECODER (table);
894

895
  range = Fcons (make_number (0), make_number (MAX_CHAR));
896 897 898
  parent = XCHAR_TABLE (table)->parent;

  GCPRO4 (table, arg, range, parent);
899 900 901
  val = XCHAR_TABLE (table)->ascii;
  if (SUB_CHAR_TABLE_P (val))
    val = XSUB_CHAR_TABLE (val)->contents[0];
902
  val = map_sub_char_table (c_function, function, table, arg, val, range,
903 904
			    table);

905 906 907
  /* If VAL is nil and TABLE has a parent, we must consult the parent
     recursively.  */
  while (NILP (val) && ! NILP (XCHAR_TABLE (table)->parent))
908
    {
909
      Lisp_Object temp;
910 911
      int from = XINT (XCAR (range));

912 913
      parent = XCHAR_TABLE (table)->parent;
      temp = XCHAR_TABLE (parent)->parent;
914 915
      /* This is to get a value of FROM in PARENT without checking the
	 parent of PARENT.  */
Paul Eggert's avatar
Paul Eggert committed
916
      set_char_table_parent (parent, Qnil);
917
      val = CHAR_TABLE_REF (parent, from);
Paul Eggert's avatar
Paul Eggert committed
918
      set_char_table_parent (parent, temp);
919
      val = map_sub_char_table (c_function, function, parent, arg, val, range,
920
				parent);
921
      table = parent;
922
    }
923 924 925

  if (! NILP (val))
    {
926 927 928 929 930
      if (EQ (XCAR (range), XCDR (range)))
	{
	  if (c_function)
	    (*c_function) (arg, XCAR (range), val);
	  else
931 932 933 934 935
	    {
	      if (decoder)
		val = decoder (table, val);
	      call2 (function, XCAR (range), val);
	    }
936
	}
937
      else
938 939 940 941
	{
	  if (c_function)
	    (*c_function) (arg, range, val);
	  else
942 943 944 945 946
	    {
	      if (decoder)
		val = decoder (table, val);
	      call2 (function, range, val);
	    }
947
	}
948
    }
949 950

  UNGCPRO;
951 952 953 954
}

DEFUN ("map-char-table", Fmap_char_table, Smap_char_table,
  2, 2, 0,
Chong Yidong's avatar
Chong Yidong committed
955 956 957 958 959
       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
960
  (Lisp_Object function, Lisp_Object char_table)
961 962 963
{
  CHECK_CHAR_TABLE (char_table);

Kenichi Handa's avatar
Kenichi Handa committed
964
  map_char_table (NULL, function, char_table, char_table);
965 966 967
  return Qnil;
}

968 969

static void
970 971 972 973
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)
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 1011 1012 1013 1014 1015 1016 1017
{
  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;