chartab.c 27.8 KB
Newer Older
1
/* chartab.c -- char-table support
Kenichi Handa's avatar
Kenichi Handa committed
2
   Copyright (C) 2003
3 4 5 6 7 8 9
     National Institute of Advanced Industrial Science and Technology (AIST)
     Registration Number H13PRO009

This file is part of GNU Emacs.

GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Miles Bader's avatar
Miles Bader committed
10
the Free Software Foundation; either version 3, or (at your option)
11 12 13 14 15 16 17 18 19 20 21 22 23
any later version.

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
along with GNU Emacs; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

#include <config.h>
24 25 26 27
#include "lisp.h"
#include "character.h"
#include "charset.h"
#include "ccl.h"
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

/* 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.  */
const int chartab_chars[4] =
  { (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.  */
const int chartab_bits[4] =
  { (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)])


DEFUN ("make-char-table", Fmake_char_table, Smake_char_table, 1, 2, 0,
59
       doc: /* Return a newly created char-table, with purpose PURPOSE.
60 61
Each element is initialized to INIT, which defaults to nil.

62 63 64 65
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.  */)
66 67 68 69 70
     (purpose, init)
     register Lisp_Object purpose, init;
{
  Lisp_Object vector;
  Lisp_Object n;
71
  int n_extras;
72 73 74
  int size;

  CHECK_SYMBOL (purpose);
75 76 77 78
  n = Fget (purpose, Qchar_table_extra_slots);
  if (NILP (n))
    n_extras = 0;
  else
79
    {
80 81 82 83
      CHECK_NATNUM (n);
      n_extras = XINT (n);
      if (n_extras > 10)
	args_out_of_range (n, Qnil);
84 85 86 87
    }

  size = VECSIZE (struct Lisp_Char_Table) - 1 + n_extras;
  vector = Fmake_vector (make_number (size), init);
Miles Bader's avatar
Miles Bader committed
88
  XSETPVECTYPE (XVECTOR (vector), PVEC_CHAR_TABLE);
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
  XCHAR_TABLE (vector)->parent = Qnil;
  XCHAR_TABLE (vector)->purpose = purpose;
  XSETCHAR_TABLE (vector, XCHAR_TABLE (vector));
  return vector;
}

static Lisp_Object
make_sub_char_table (depth, min_char, defalt)
     int depth, min_char;
     Lisp_Object defalt;
{
  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
104
  XSETPVECTYPE (XVECTOR (table), PVEC_SUB_CHAR_TABLE);
105 106 107 108 109 110 111 112 113 114 115 116 117 118
  XSUB_CHAR_TABLE (table)->depth = make_number (depth);
  XSUB_CHAR_TABLE (table)->min_char = make_number (min_char);
  XSETSUB_CHAR_TABLE (table, XSUB_CHAR_TABLE (table));

  return table;
}

static Lisp_Object
char_table_ascii (table)
     Lisp_Object table;
{
  Lisp_Object sub;

  sub = XCHAR_TABLE (table)->contents[0];
119 120
  if (! SUB_CHAR_TABLE_P (sub))
    return sub;
121
  sub = XSUB_CHAR_TABLE (sub)->contents[0];
122 123
  if (! SUB_CHAR_TABLE_P (sub))
    return sub;
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  return XSUB_CHAR_TABLE (sub)->contents[0];
}

Lisp_Object
copy_sub_char_table (table)
     Lisp_Object table;
{
  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))
	XSUB_CHAR_TABLE (copy)->contents[i] = copy_sub_char_table (val);
      else
	XSUB_CHAR_TABLE (copy)->contents[i] = val;
    }

  return copy;
}


Lisp_Object
copy_char_table (table)
     Lisp_Object table;
{
  Lisp_Object copy;
  int size = XCHAR_TABLE (table)->size & PSEUDOVECTOR_SIZE_MASK;
  int i;

  copy = Fmake_vector (make_number (size), Qnil);
Miles Bader's avatar
Miles Bader committed
161
  XSETPVECTYPE (XVECTOR (copy), PVEC_CHAR_TABLE);
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
  XCHAR_TABLE (copy)->defalt = XCHAR_TABLE (table)->defalt;
  XCHAR_TABLE (copy)->parent = XCHAR_TABLE (table)->parent;
  XCHAR_TABLE (copy)->purpose = XCHAR_TABLE (table)->purpose;
  XCHAR_TABLE (copy)->ascii = XCHAR_TABLE (table)->ascii;
  for (i = 0; i < chartab_size[0]; i++)
    XCHAR_TABLE (copy)->contents[i]
      = (SUB_CHAR_TABLE_P (XCHAR_TABLE (table)->contents[i])
	 ? copy_sub_char_table (XCHAR_TABLE (table)->contents[i])
	 : XCHAR_TABLE (table)->contents[i]);
  if (SUB_CHAR_TABLE_P (XCHAR_TABLE (copy)->ascii))
    XCHAR_TABLE (copy)->ascii = char_table_ascii (copy);
  size -= VECSIZE (struct Lisp_Char_Table) - 1;
  for (i = 0; i < size; i++)
    XCHAR_TABLE (copy)->extras[i] = XCHAR_TABLE (table)->extras[i];

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

Lisp_Object
sub_char_table_ref (table, c)
     Lisp_Object table;
     int c;
{
  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;

  val = tbl->contents[CHARTAB_IDX (c, depth, min_char)];
  if (SUB_CHAR_TABLE_P (val))
    val = sub_char_table_ref (val, c);
  return val;
}

Lisp_Object
char_table_ref (table, c)
     Lisp_Object table;
     int c;
{
  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))
	val = sub_char_table_ref (val, c);
    }
  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
224
}
225 226

static Lisp_Object
227
sub_char_table_ref_and_range (table, c, from, to, defalt)
228 229 230
     Lisp_Object table;
     int c;
     int *from, *to;
231
     Lisp_Object defalt;
232 233 234 235
{
  struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
  int depth = XINT (tbl->depth);
  int min_char = XINT (tbl->min_char);
236 237
  int max_char = min_char + chartab_chars[depth - 1] - 1;
  int index = CHARTAB_IDX (c, depth, min_char);
238
  Lisp_Object val;
Kenichi Handa's avatar
Kenichi Handa committed
239

240 241 242 243 244 245 246 247 248 249
  val = tbl->contents[index];
  *from = min_char + index * chartab_chars[depth];
  *to = *from + chartab_chars[depth] - 1;
  if (SUB_CHAR_TABLE_P (val))
    val = sub_char_table_ref_and_range (val, c, from, to, defalt);
  else if (NILP (val))
    val = defalt;

  while (*from > min_char
	 && *from == min_char + index * chartab_chars[depth])
250
    {
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
      Lisp_Object this_val;
      int this_from = *from - chartab_chars[depth];
      int this_to = *from - 1;

      index--;
      this_val = tbl->contents[index];
      if (SUB_CHAR_TABLE_P (this_val))
	this_val = sub_char_table_ref_and_range (this_val, this_to,
						 &this_from, &this_to,
						 defalt);
      else if (NILP (this_val))
	this_val = defalt;

      if (! EQ (this_val, val))
	break;
      *from = this_from;
267
    }
268 269 270
  index = CHARTAB_IDX (c, depth, min_char);
  while (*to < max_char
	 && *to == min_char + (index + 1) * chartab_chars[depth] - 1)
271
    {
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
      Lisp_Object this_val;
      int this_from = *to + 1;
      int this_to = this_from + chartab_chars[depth] - 1;

      index++;
      this_val = tbl->contents[index];
      if (SUB_CHAR_TABLE_P (this_val))
	this_val = sub_char_table_ref_and_range (this_val, this_from,
						 &this_from, &this_to,
						 defalt);
      else if (NILP (this_val))
	this_val = defalt;
      if (! EQ (this_val, val))
	break;
      *to = this_to;
287
    }
288

289 290 291 292
  return val;
}


293 294 295 296 297
/* Return the value for C in char-table TABLE.  Set *FROM and *TO to
   the range of characters (containing C) that have the same value as
   C.  It is not assured that the value of (*FROM - 1) and (*TO + 1)
   is different from that of C.  */

298 299 300 301 302 303 304
Lisp_Object
char_table_ref_and_range (table, c, from, to)
     Lisp_Object table;
     int c;
     int *from, *to;
{
  struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
305
  int index = CHARTAB_IDX (c, 0, 0);
306 307
  Lisp_Object val;

308 309 310 311 312 313 314 315 316
  val = tbl->contents[index];
  *from = index * chartab_chars[0];
  *to = *from + chartab_chars[0] - 1;
  if (SUB_CHAR_TABLE_P (val))
    val = sub_char_table_ref_and_range (val, c, from, to, tbl->defalt);
  else if (NILP (val))
    val = tbl->defalt;

  while (*from > 0 && *from == index * chartab_chars[0])
317
    {
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
      Lisp_Object this_val;
      int this_from = *from - chartab_chars[0];
      int this_to = *from - 1;

      index--;
      this_val = tbl->contents[index];
      if (SUB_CHAR_TABLE_P (this_val))
	this_val = sub_char_table_ref_and_range (this_val, this_to,
						 &this_from, &this_to,
						 tbl->defalt);
      else if (NILP (this_val))
	this_val = tbl->defalt;

      if (! EQ (this_val, val))
	break;
      *from = this_from;
334
    }
335
  while (*to < MAX_CHAR && *to == (index + 1) * chartab_chars[0] - 1)
336
    {
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
      Lisp_Object this_val;
      int this_from = *to + 1;
      int this_to = this_from + chartab_chars[0] - 1;

      index++;
      this_val = tbl->contents[index];
      if (SUB_CHAR_TABLE_P (this_val))
	this_val = sub_char_table_ref_and_range (this_val, this_from,
						 &this_from, &this_to,
						 tbl->defalt);
      else if (NILP (this_val))
	this_val = tbl->defalt;
      if (! EQ (this_val, val))
	break;
      *to = this_to;
352 353 354
    }

  return val;
355
}
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382


#define ASET_RANGE(ARRAY, FROM, TO, LIMIT, VAL)				\
  do {									\
    int limit = (TO) < (LIMIT) ? (TO) : (LIMIT);			\
    for (; (FROM) < limit; (FROM)++) (ARRAY)->contents[(FROM)] = (VAL);	\
  } while (0)

#define GET_SUB_CHAR_TABLE(TABLE, SUBTABLE, IDX, DEPTH, MIN_CHAR)	  \
  do {									  \
    (SUBTABLE) = (TABLE)->contents[(IDX)];				  \
    if (!SUB_CHAR_TABLE_P (SUBTABLE))					  \
      (SUBTABLE) = make_sub_char_table ((DEPTH), (MIN_CHAR), (SUBTABLE)); \
  } while (0)


static void
sub_char_table_set (table, c, val)
     Lisp_Object table;
     int c;
     Lisp_Object val;
{
  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
383

384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
  if (depth == 3)
    tbl->contents[i] = val;
  else
    {
      sub = tbl->contents[i];
      if (! SUB_CHAR_TABLE_P (sub))
	{
	  sub = make_sub_char_table (depth + 1,
				     min_char + i * chartab_chars[depth], sub);
	  tbl->contents[i] = sub;
	}
      sub_char_table_set (sub, c, val);
    }
}

Lisp_Object
char_table_set (table, c, val)
     Lisp_Object table;
     int c;
     Lisp_Object val;
{
  struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);

  if (ASCII_CHAR_P (c)
      && SUB_CHAR_TABLE_P (tbl->ascii))
    {
      XSUB_CHAR_TABLE (tbl->ascii)->contents[c] = val;
    }
  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);
	  tbl->contents[i] = sub;
	}
      sub_char_table_set (sub, c, val);
      if (ASCII_CHAR_P (c))
425
	tbl->ascii = char_table_ascii (table);
426 427 428 429 430 431 432 433 434 435 436 437 438 439
    }
  return val;
}

static void
sub_char_table_set_range (table, depth, min_char, from, to, val)
     Lisp_Object *table;
     int depth;
     int min_char;
     int from, to;
     Lisp_Object val;
{
  int max_char = min_char + chartab_chars[depth] - 1;

Kenichi Handa's avatar
Kenichi Handa committed
440
  if (depth == 3 || (from <= min_char && to >= max_char))
441 442 443 444 445 446 447 448 449 450 451 452
    *table = val;
  else
    {
      int i, j;

      depth++;
      if (! SUB_CHAR_TABLE_P (*table))
	*table = make_sub_char_table (depth, min_char, *table);
      if (from < min_char)
	from = min_char;
      if (to > max_char)
	to = max_char;
453
      i = CHARTAB_IDX (from, depth, min_char);
454
      j = CHARTAB_IDX (to, depth, min_char);
455 456
      min_char += chartab_chars[depth] * i;
      for (; i <= j; i++, min_char += chartab_chars[depth])
457
	sub_char_table_set_range (XSUB_CHAR_TABLE (*table)->contents + i,
458
				  depth, min_char, from, to, val);
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
    }
}


Lisp_Object
char_table_set_range (table, from, to, val)
     Lisp_Object table;
     int from, to;
     Lisp_Object val;
{
  struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
  Lisp_Object *contents = tbl->contents;
  int i, min_char;

  if (from == to)
    char_table_set (table, from, val);
  else
    {
      for (i = CHARTAB_IDX (from, 0, 0), min_char = i * chartab_chars[0];
	   min_char <= to;
	   i++, min_char += chartab_chars[0])
	sub_char_table_set_range (contents + i, 0, min_char, from, to, val);
      if (ASCII_CHAR_P (from))
482
	tbl->ascii = char_table_ascii (table);
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
    }
  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.  */)
     (char_table)
     Lisp_Object char_table;
{
  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).  */)
  (char_table)
     Lisp_Object char_table;
{
  CHECK_CHAR_TABLE (char_table);

  return XCHAR_TABLE (char_table)->parent;
}

DEFUN ("set-char-table-parent", Fset_char_table_parent, Sset_char_table_parent,
       2, 2, 0,
       doc: /* Set the parent char-table of CHAR-TABLE to PARENT.
Kenichi Handa's avatar
Kenichi Handa committed
518
Return PARENT.  PARENT must be either nil or another char-table.  */)
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
     (char_table, parent)
     Lisp_Object char_table, parent;
{
  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");
    }

  XCHAR_TABLE (char_table)->parent = parent;

  return parent;
}

DEFUN ("char-table-extra-slot", Fchar_table_extra_slot, Schar_table_extra_slot,
       2, 2, 0,
       doc: /* Return the value of CHAR-TABLE's extra-slot number N.  */)
     (char_table, n)
     Lisp_Object char_table, n;
{
  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)];
}

DEFUN ("set-char-table-extra-slot", Fset_char_table_extra_slot,
       Sset_char_table_extra_slot,
       3, 3, 0,
       doc: /* Set CHAR-TABLE's extra-slot number N to VALUE.  */)
     (char_table, n, value)
     Lisp_Object char_table, n, value;
{
  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)] = value;
}

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.  */)
     (char_table, range)
     Lisp_Object char_table, range;
{
  Lisp_Object val;
  CHECK_CHAR_TABLE (char_table);

  if (EQ (range, Qnil))
    val = XCHAR_TABLE (char_table)->defalt;
  else if (INTEGERP (range))
    val = CHAR_TABLE_REF (char_table, XINT (range));
  else if (CONSP (range))
    {
      int from, to;

Kenichi Handa's avatar
Kenichi Handa committed
590 591
      CHECK_CHARACTER_CAR (range);
      CHECK_CHARACTER_CDR (range);
592 593 594 595 596 597 598 599 600 601 602
      val = char_table_ref_and_range (char_table, XINT (XCAR (range)),
				      &from, &to);
      /* Not yet implemented. */
    }
  else
    error ("Invalid RANGE argument to `char-table-range'");
  return val;
}

DEFUN ("set-char-table-range", Fset_char_table_range, Sset_char_table_range,
       3, 3, 0,
Kenichi Handa's avatar
Kenichi Handa committed
603
       doc: /* Set the value in CHAR-TABLE for a range of characters RANGE to VALUE.
604
RANGE should be t (for all characters), nil (for the default value),
Kenichi Handa's avatar
Kenichi Handa committed
605 606
a cons of character codes (for characters in the range),
or a character code.  Return VALUE.  */)
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
     (char_table, range, value)
     Lisp_Object char_table, range, value;
{
  CHECK_CHAR_TABLE (char_table);
  if (EQ (range, Qt))
    {
      int i;

      XCHAR_TABLE (char_table)->ascii = Qnil;
      for (i = 0; i < chartab_size[0]; i++)
	XCHAR_TABLE (char_table)->contents[i] = Qnil;
      XCHAR_TABLE (char_table)->defalt = value;
    }
  else if (EQ (range, Qnil))
    XCHAR_TABLE (char_table)->defalt = value;
  else if (INTEGERP (range))
    char_table_set (char_table, XINT (range), value);
  else if (CONSP (range))
    {
Kenichi Handa's avatar
Kenichi Handa committed
626 627
      CHECK_CHARACTER_CAR (range);
      CHECK_CHARACTER_CDR (range);
628 629 630 631 632 633 634 635 636 637 638 639
      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: /*
640
This function is obsolete and has no effect.  */)
641 642 643 644 645 646 647
     (char_table, ch, value)
     Lisp_Object char_table, ch, value;
{
  return Qnil;
}

/* Look up the element in TABLE at index CH, and return it as an
648
   integer.  If the element is not a character, return CH itself.  */
649 650 651 652 653 654 655 656

int
char_table_translate (table, ch)
     Lisp_Object table;
     int ch;
{
  Lisp_Object value;
  value = Faref (table, make_number (ch));
657
  if (! CHARACTERP (value))
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
    return ch;
  return XINT (value);
}

static Lisp_Object
optimize_sub_char_table (table)
     Lisp_Object table;
{
  struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
  int depth = XINT (tbl->depth);
  Lisp_Object elt, this;
  int i;

  elt = XSUB_CHAR_TABLE (table)->contents[0];
  if (SUB_CHAR_TABLE_P (elt))
    elt = XSUB_CHAR_TABLE (table)->contents[0] = optimize_sub_char_table (elt);
  if (SUB_CHAR_TABLE_P (elt))
    return table;
  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]
	  = optimize_sub_char_table (this);
      if (SUB_CHAR_TABLE_P (this)
	  || NILP (Fequal (this, elt)))
	break;
    }

  return (i < chartab_size[depth] ? table : elt);
}

DEFUN ("optimize-char-table", Foptimize_char_table, Soptimize_char_table,
       1, 1, 0,
       doc: /* Optimize CHAR-TABLE.  */)
     (char_table)
     Lisp_Object char_table;
{
  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))
	XCHAR_TABLE (char_table)->contents[i] = optimize_sub_char_table (elt);
    }
  return Qnil;
}


711 712 713 714 715 716 717 718 719 720 721 722 723 724
/* 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
   characters, VAL is a value of FROM in TABLE, DEFAULT_VAL is the
   default value of the char-table, PARENT is the parent of the
   char-table.

   ARG is passed to C_FUNCTION when that is called.

   It returns the value of last character covered by TABLE (not the
   value inheritted from the parent), and by side-effect, the car part
   of RANGE is updated to the minimum character C where C and all the
   following characters in TABLE have the same value.  */

725
static Lisp_Object
Kenichi Handa's avatar
Kenichi Handa committed
726 727
map_sub_char_table (c_function, function, table, arg, val, range,
		    default_val, parent)
728
     void (*c_function) P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
Kenichi Handa's avatar
Kenichi Handa committed
729
     Lisp_Object function, table, arg, val, range, default_val, parent;
730
{
731 732 733 734 735 736 737 738 739
  /* Pointer to the elements of TABLE. */
  Lisp_Object *contents;
  /* Depth of TABLE.  */
  int depth;
  /* Minimum and maxinum characters covered by TABLE. */
  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));
740 741
  int i, c;

742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
  if (SUB_CHAR_TABLE_P (table))
    {
      struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);

      depth = XINT (tbl->depth);
      contents = tbl->contents;
      min_char = XINT (tbl->min_char);
      max_char = min_char + chartab_chars[depth - 1] - 1;
    }
  else
    {
      depth = 0;
      contents = XCHAR_TABLE (table)->contents;
      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)
769
    {
770 771
      Lisp_Object this = contents[i];
      int nextc = c + chars_in_block;
772 773

      if (SUB_CHAR_TABLE_P (this))
774 775 776 777 778 779
	{
	  if (to >= nextc)
	    XSETCDR (range, make_number (nextc - 1));
	  val = map_sub_char_table (c_function, function, this, arg,
				    val, range, default_val, parent);
	}
780
      else
781
	{
782 783 784
	  if (NILP (this))
	    this = default_val;
	  if (NILP (Fequal (val, this)))
785
	    {
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
	      int different_value = 1;

	      if (NILP (val))
		{
		  if (! NILP (parent))
		    {
		      Lisp_Object temp = XCHAR_TABLE (parent)->parent;

		      /* This is to get a value of FROM in PARENT
			 without checking the parent of PARENT.  */
		      XCHAR_TABLE (parent)->parent = Qnil;
		      val = CHAR_TABLE_REF (parent, from);
		      XCHAR_TABLE (parent)->parent = temp;
		      XSETCDR (range, make_number (c - 1));
		      val = map_sub_char_table (c_function, function,
						parent, arg, val, range,
						XCHAR_TABLE (parent)->defalt,
						XCHAR_TABLE (parent)->parent);
		      if (! NILP (Fequal (val, this)))
			different_value = 0;
		    }
		}
	      if (! NILP (val) && different_value)
809
		{
Kenichi Handa's avatar
Kenichi Handa committed
810
		  XSETCDR (range, make_number (c - 1));
811
		  if (EQ (XCAR (range), XCDR (range)))
812 813 814 815 816 817
		    {
		      if (c_function)
			(*c_function) (arg, XCAR (range), val);
		      else
			call2 (function, XCAR (range), val);
		    }
818
		  else
819 820 821 822 823 824
		    {
		      if (c_function)
			(*c_function) (arg, range, val);
		      else
			call2 (function, range, val);
		    }
825
		}
826
	      val = this;
827
	      from = c;
Kenichi Handa's avatar
Kenichi Handa committed
828
	      XSETCAR (range, make_number (c));
829 830
	    }
	}
831
      XSETCDR (range, make_number (to));
832 833 834 835 836 837 838 839
    }
  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
840
   ARG is passed to C_FUNCTION when that is called.  */
841 842

void
Kenichi Handa's avatar
Kenichi Handa committed
843
map_char_table (c_function, function, table, arg)
844
     void (*c_function) P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
Kenichi Handa's avatar
Kenichi Handa committed
845
     Lisp_Object function, table, arg;
846 847 848
{
  Lisp_Object range, val;
  int c, i;
849
  struct gcpro gcpro1, gcpro2, gcpro3;
850

851
  range = Fcons (make_number (0), make_number (MAX_CHAR));
852
  GCPRO3 (table, arg, range);
853 854 855
  val = XCHAR_TABLE (table)->ascii;
  if (SUB_CHAR_TABLE_P (val))
    val = XSUB_CHAR_TABLE (val)->contents[0];
856 857 858 859 860 861
  val = map_sub_char_table (c_function, function, table, arg, val, range,
			    XCHAR_TABLE (table)->defalt,
			    XCHAR_TABLE (table)->parent);
  /* If VAL is nil and TABLE has a parent, we must consult the parent
     recursively.  */
  while (NILP (val) && ! NILP (XCHAR_TABLE (table)->parent))
862
    {
863 864 865 866 867 868 869 870 871 872 873 874 875
      Lisp_Object parent = XCHAR_TABLE (table)->parent;
      Lisp_Object temp = XCHAR_TABLE (parent)->parent;
      int from = XINT (XCAR (range));

      /* This is to get a value of FROM in PARENT without checking the
	 parent of PARENT.  */
      XCHAR_TABLE (parent)->parent = Qnil;
      val = CHAR_TABLE_REF (parent, from);
      XCHAR_TABLE (parent)->parent = temp;
      val = map_sub_char_table (c_function, function, parent, arg, val, range,
				XCHAR_TABLE (parent)->defalt,
				XCHAR_TABLE (parent)->parent);
      table = parent;
876
    }
877 878 879

  if (! NILP (val))
    {
880 881 882 883 884 885 886
      if (EQ (XCAR (range), XCDR (range)))
	{
	  if (c_function)
	    (*c_function) (arg, XCAR (range), val);
	  else
	    call2 (function, XCAR (range), val);
	}
887
      else
888 889 890 891 892 893
	{
	  if (c_function)
	    (*c_function) (arg, range, val);
	  else
	    call2 (function, range, val);
	}
894
    }
895 896

  UNGCPRO;
897 898 899 900 901
}

DEFUN ("map-char-table", Fmap_char_table, Smap_char_table,
  2, 2, 0,
       doc: /*
Kenichi Handa's avatar
Kenichi Handa committed
902
Call FUNCTION for each character in CHAR-TABLE that has non-nil value.
903
FUNCTION is called with two arguments--a key and a value.
904 905
The key is a character code or a cons of character codes specifying a
range of characters that have the same value.  */)
906 907 908 909 910
     (function, char_table)
     Lisp_Object function, char_table;
{
  CHECK_CHAR_TABLE (char_table);

Kenichi Handa's avatar
Kenichi Handa committed
911
  map_char_table (NULL, function, char_table, char_table);
912 913 914
  return Qnil;
}

915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966

static void
map_sub_char_table_for_charset (c_function, function, table, arg, range,
				charset, from, to)
     void (*c_function) P_ ((Lisp_Object, Lisp_Object));
     Lisp_Object function, table, arg, range;
     struct charset *charset;
     unsigned from, to;
{
  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)
967
		  (*c_function) (arg, range);
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
		else
		  call2 (function, range, arg);
		XSETCAR (range, Qnil);
	      }
	  }
	else
	  {
	    if (NILP (XCAR (range)))
	      XSETCAR (range, make_number (c));
	  }
      }
}


void
map_char_table_for_charset (c_function, function, table, arg,
			    charset, from, to)
     void (*c_function) P_ ((Lisp_Object, Lisp_Object));
     Lisp_Object function, table, arg;
     struct charset *charset;
     unsigned from, to;
{
  Lisp_Object range;
  int c, i;
992
  struct gcpro gcpro1;
993

Kenichi Handa's avatar
Kenichi Handa committed
994
  range = Fcons (Qnil, Qnil);
995
  GCPRO1 (range);
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025

  for (i = 0, c = 0; i < chartab_size[0]; i++, c += chartab_chars[0])
    {
      Lisp_Object this;

      this = XCHAR_TABLE (table)->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);
	}
    }
  if (! NILP (XCAR (range)))
    {
      XSETCDR (range, make_number (c - 1));
      if (c_function)
	(*c_function) (arg, range);
      else
	call2 (function, range, arg);
    }
1026 1027

  UNGCPRO;
1028 1029
}

1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045

void
syms_of_chartab ()
{
  defsubr (&Smake_char_table);
  defsubr (&Schar_table_parent);
  defsubr (&Schar_table_subtype);
  defsubr (&Sset_char_table_parent);
  defsubr (&Schar_table_extra_slot);
  defsubr (&Sset_char_table_extra_slot);
  defsubr (&Schar_table_range);
  defsubr (&Sset_char_table_range);
  defsubr (&Sset_char_table_default);
  defsubr (&Soptimize_char_table);
  defsubr (&Smap_char_table);
}
Miles Bader's avatar
Miles Bader committed
1046 1047 1048

/* arch-tag: 18b5b560-7ab5-4108-b09e-d5dd65dc6fda
   (do not change this comment) */