* doc/lispref/variables.texi (Scope): Mention the availability of lexbind.
[bpt/emacs.git] / src / chartab.c
index 165fa0d..cd8aa78 100644 (file)
@@ -1,5 +1,5 @@
 /* chartab.c -- char-table support
 /* chartab.c -- char-table support
-   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
+   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
      National Institute of Advanced Industrial Science and Technology (AIST)
      Registration Number H13PRO009
 
      National Institute of Advanced Industrial Science and Technology (AIST)
      Registration Number H13PRO009
 
@@ -19,6 +19,7 @@ You should have received a copy of the GNU General Public License
 along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <config.h>
 along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <config.h>
+#include <setjmp.h>
 #include "lisp.h"
 #include "character.h"
 #include "charset.h"
 #include "lisp.h"
 #include "character.h"
 #include "charset.h"
@@ -61,8 +62,7 @@ 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.  */)
 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.  */)
-     (purpose, init)
-     register Lisp_Object purpose, init;
+  (register Lisp_Object purpose, Lisp_Object init)
 {
   Lisp_Object vector;
   Lisp_Object n;
 {
   Lisp_Object vector;
   Lisp_Object n;
@@ -91,9 +91,7 @@ the char-table has no extra slot.  */)
 }
 
 static Lisp_Object
 }
 
 static Lisp_Object
-make_sub_char_table (depth, min_char, defalt)
-     int depth, min_char;
-     Lisp_Object defalt;
+make_sub_char_table (int depth, int min_char, Lisp_Object defalt)
 {
   Lisp_Object table;
   int size = VECSIZE (struct Lisp_Sub_Char_Table) - 1 + chartab_size[depth];
 {
   Lisp_Object table;
   int size = VECSIZE (struct Lisp_Sub_Char_Table) - 1 + chartab_size[depth];
@@ -107,8 +105,7 @@ make_sub_char_table (depth, min_char, defalt)
 }
 
 static Lisp_Object
 }
 
 static Lisp_Object
-char_table_ascii (table)
-     Lisp_Object table;
+char_table_ascii (Lisp_Object table)
 {
   Lisp_Object sub;
 
 {
   Lisp_Object sub;
 
@@ -122,8 +119,7 @@ char_table_ascii (table)
 }
 
 Lisp_Object
 }
 
 Lisp_Object
-copy_sub_char_table (table)
-     Lisp_Object table;
+copy_sub_char_table (Lisp_Object table)
 {
   Lisp_Object copy;
   int depth = XINT (XSUB_CHAR_TABLE (table)->depth);
 {
   Lisp_Object copy;
   int depth = XINT (XSUB_CHAR_TABLE (table)->depth);
@@ -147,8 +143,7 @@ copy_sub_char_table (table)
 
 
 Lisp_Object
 
 
 Lisp_Object
-copy_char_table (table)
-     Lisp_Object table;
+copy_char_table (Lisp_Object table)
 {
   Lisp_Object copy;
   int size = XCHAR_TABLE (table)->size & PSEUDOVECTOR_SIZE_MASK;
 {
   Lisp_Object copy;
   int size = XCHAR_TABLE (table)->size & PSEUDOVECTOR_SIZE_MASK;
@@ -159,14 +154,12 @@ copy_char_table (table)
   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)->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]);
   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);
+  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];
   size -= VECSIZE (struct Lisp_Char_Table) - 1;
   for (i = 0; i < size; i++)
     XCHAR_TABLE (copy)->extras[i] = XCHAR_TABLE (table)->extras[i];
@@ -175,10 +168,8 @@ copy_char_table (table)
   return copy;
 }
 
   return copy;
 }
 
-Lisp_Object
-sub_char_table_ref (table, c)
-     Lisp_Object table;
-     int c;
+static Lisp_Object
+sub_char_table_ref (Lisp_Object table, int c)
 {
   struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
   int depth = XINT (tbl->depth);
 {
   struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
   int depth = XINT (tbl->depth);
@@ -192,9 +183,7 @@ sub_char_table_ref (table, c)
 }
 
 Lisp_Object
 }
 
 Lisp_Object
-char_table_ref (table, c)
-     Lisp_Object table;
-     int c;
+char_table_ref (Lisp_Object table, int c)
 {
   struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
   Lisp_Object val;
 {
   struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
   Lisp_Object val;
@@ -221,131 +210,121 @@ char_table_ref (table, c)
 }
 
 static Lisp_Object
 }
 
 static Lisp_Object
-sub_char_table_ref_and_range (table, c, from, to, defalt)
-     Lisp_Object table;
-     int c;
-     int *from, *to;
-     Lisp_Object defalt;
+sub_char_table_ref_and_range (Lisp_Object table, int c, int *from, int *to, Lisp_Object defalt)
 {
   struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
   int depth = XINT (tbl->depth);
   int min_char = XINT (tbl->min_char);
   int max_char = min_char + chartab_chars[depth - 1] - 1;
 {
   struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
   int depth = XINT (tbl->depth);
   int min_char = XINT (tbl->min_char);
   int max_char = min_char + chartab_chars[depth - 1] - 1;
-  int index = CHARTAB_IDX (c, depth, min_char);
+  int index = CHARTAB_IDX (c, depth, min_char), idx;
   Lisp_Object val;
 
   val = tbl->contents[index];
   Lisp_Object val;
 
   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;
 
   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])
+  idx = index;
+  while (idx > 0 && *from < min_char + idx * chartab_chars[depth])
     {
       Lisp_Object this_val;
     {
       Lisp_Object this_val;
-      int this_from = *from - chartab_chars[depth];
-      int this_to = *from - 1;
 
 
-      index--;
-      this_val = tbl->contents[index];
+      c = min_char + idx * chartab_chars[depth] - 1;
+      idx--;
+      this_val = tbl->contents[idx];
       if (SUB_CHAR_TABLE_P (this_val))
       if (SUB_CHAR_TABLE_P (this_val))
-       this_val = sub_char_table_ref_and_range (this_val, this_to,
-                                                &this_from, &this_to,
-                                                defalt);
+       this_val = sub_char_table_ref_and_range (this_val, c, from, to, defalt);
       else if (NILP (this_val))
        this_val = defalt;
 
       if (! EQ (this_val, val))
       else if (NILP (this_val))
        this_val = defalt;
 
       if (! EQ (this_val, val))
-       break;
-      *from = this_from;
+       {
+         *from = c + 1;
+         break;
+       }
     }
     }
-  index = CHARTAB_IDX (c, depth, min_char);
-  while (*to < max_char
-        && *to == min_char + (index + 1) * chartab_chars[depth] - 1)
+  while ((c = min_char + (index + 1) * chartab_chars[depth]) <= max_char
+        && *to >= c)
     {
       Lisp_Object this_val;
     {
       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))
 
       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);
+       this_val = sub_char_table_ref_and_range (this_val, c, from, to, defalt);
       else if (NILP (this_val))
        this_val = defalt;
       if (! EQ (this_val, val))
       else if (NILP (this_val))
        this_val = defalt;
       if (! EQ (this_val, val))
-       break;
-      *to = this_to;
+       {
+         *to = c - 1;
+         break;
+       }
     }
 
   return val;
 }
 
 
     }
 
   return val;
 }
 
 
-/* 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.  */
+/* 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.  */
 
 Lisp_Object
 
 Lisp_Object
-char_table_ref_and_range (table, c, from, to)
-     Lisp_Object table;
-     int c;
-     int *from, *to;
+char_table_ref_and_range (Lisp_Object table, int c, int *from, int *to)
 {
   struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
 {
   struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
-  int index = CHARTAB_IDX (c, 0, 0);
+  int index = CHARTAB_IDX (c, 0, 0), idx;
   Lisp_Object val;
 
   val = tbl->contents[index];
   Lisp_Object val;
 
   val = tbl->contents[index];
-  *from = index * chartab_chars[0];
-  *to = *from + chartab_chars[0] - 1;
+  if (*from < 0)
+    *from = 0;
+  if (*to < 0)
+    *to = MAX_CHAR;
   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;
 
   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])
+  idx = index;
+  while (*from < idx * chartab_chars[0])
     {
       Lisp_Object this_val;
     {
       Lisp_Object this_val;
-      int this_from = *from - chartab_chars[0];
-      int this_to = *from - 1;
 
 
-      index--;
-      this_val = tbl->contents[index];
+      c = idx * chartab_chars[0] - 1;
+      idx--;
+      this_val = tbl->contents[idx];
       if (SUB_CHAR_TABLE_P (this_val))
       if (SUB_CHAR_TABLE_P (this_val))
-       this_val = sub_char_table_ref_and_range (this_val, this_to,
-                                                &this_from, &this_to,
+       this_val = sub_char_table_ref_and_range (this_val, c, from, to,
                                                 tbl->defalt);
       else if (NILP (this_val))
        this_val = tbl->defalt;
 
       if (! EQ (this_val, val))
                                                 tbl->defalt);
       else if (NILP (this_val))
        this_val = tbl->defalt;
 
       if (! EQ (this_val, val))
-       break;
-      *from = this_from;
+       {
+         *from = c + 1;
+         break;
+       }
     }
     }
-  while (*to < MAX_CHAR && *to == (index + 1) * chartab_chars[0] - 1)
+  while (*to >= (index + 1) * chartab_chars[0])
     {
       Lisp_Object this_val;
     {
       Lisp_Object this_val;
-      int this_from = *to + 1;
-      int this_to = this_from + chartab_chars[0] - 1;
 
       index++;
 
       index++;
+      c = index * chartab_chars[0];
       this_val = tbl->contents[index];
       if (SUB_CHAR_TABLE_P (this_val))
       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,
+       this_val = sub_char_table_ref_and_range (this_val, c, from, to,
                                                 tbl->defalt);
       else if (NILP (this_val))
        this_val = tbl->defalt;
       if (! EQ (this_val, val))
                                                 tbl->defalt);
       else if (NILP (this_val))
        this_val = tbl->defalt;
       if (! EQ (this_val, val))
-       break;
-      *to = this_to;
+       {
+         *to = c - 1;
+         break;
+       }
     }
 
   return val;
     }
 
   return val;
@@ -367,10 +346,7 @@ char_table_ref_and_range (table, c, from, to)
 
 
 static void
 
 
 static void
-sub_char_table_set (table, c, val)
-     Lisp_Object table;
-     int c;
-     Lisp_Object val;
+sub_char_table_set (Lisp_Object table, int c, Lisp_Object val)
 {
   struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
   int depth = XINT ((tbl)->depth);
 {
   struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
   int depth = XINT ((tbl)->depth);
@@ -394,10 +370,7 @@ sub_char_table_set (table, c, val)
 }
 
 Lisp_Object
 }
 
 Lisp_Object
-char_table_set (table, c, val)
-     Lisp_Object table;
-     int c;
-     Lisp_Object val;
+char_table_set (Lisp_Object table, int c, Lisp_Object val)
 {
   struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
 
 {
   struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
 
@@ -425,12 +398,7 @@ char_table_set (table, c, val)
 }
 
 static void
 }
 
 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;
+sub_char_table_set_range (Lisp_Object *table, int depth, int min_char, int from, int to, Lisp_Object val)
 {
   int max_char = min_char + chartab_chars[depth] - 1;
 
 {
   int max_char = min_char + chartab_chars[depth] - 1;
 
@@ -458,10 +426,7 @@ sub_char_table_set_range (table, depth, min_char, from, to, val)
 
 
 Lisp_Object
 
 
 Lisp_Object
-char_table_set_range (table, from, to, val)
-     Lisp_Object table;
-     int from, to;
-     Lisp_Object val;
+char_table_set_range (Lisp_Object table, int from, int to, Lisp_Object val)
 {
   struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
   Lisp_Object *contents = tbl->contents;
 {
   struct Lisp_Char_Table *tbl = XCHAR_TABLE (table);
   Lisp_Object *contents = tbl->contents;
@@ -486,8 +451,7 @@ 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.  */)
        1, 1, 0,
        doc: /*
 Return the subtype of char-table CHAR-TABLE.  The value is a symbol.  */)
-     (char_table)
-     Lisp_Object char_table;
+  (Lisp_Object char_table)
 {
   CHECK_CHAR_TABLE (char_table);
 
 {
   CHECK_CHAR_TABLE (char_table);
 
@@ -501,8 +465,7 @@ 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).  */)
 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;
+  (Lisp_Object char_table)
 {
   CHECK_CHAR_TABLE (char_table);
 
 {
   CHECK_CHAR_TABLE (char_table);
 
@@ -513,8 +476,7 @@ 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.
 Return PARENT.  PARENT must be either nil or another char-table.  */)
        2, 2, 0,
        doc: /* Set the parent char-table of CHAR-TABLE to PARENT.
 Return PARENT.  PARENT must be either nil or another char-table.  */)
-     (char_table, parent)
-     Lisp_Object char_table, parent;
+  (Lisp_Object char_table, Lisp_Object parent)
 {
   Lisp_Object temp;
 
 {
   Lisp_Object temp;
 
@@ -537,8 +499,7 @@ Return PARENT.  PARENT must be either nil or another char-table.  */)
 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.  */)
 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;
+  (Lisp_Object char_table, Lisp_Object n)
 {
   CHECK_CHAR_TABLE (char_table);
   CHECK_NUMBER (n);
 {
   CHECK_CHAR_TABLE (char_table);
   CHECK_NUMBER (n);
@@ -553,8 +514,7 @@ 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.  */)
        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;
+  (Lisp_Object char_table, Lisp_Object n, Lisp_Object value)
 {
   CHECK_CHAR_TABLE (char_table);
   CHECK_NUMBER (n);
 {
   CHECK_CHAR_TABLE (char_table);
   CHECK_NUMBER (n);
@@ -570,8 +530,7 @@ DEFUN ("char-table-range", Fchar_table_range, Schar_table_range,
        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.  */)
        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 char_table, Lisp_Object range)
 {
   Lisp_Object val;
   CHECK_CHAR_TABLE (char_table);
 {
   Lisp_Object val;
   CHECK_CHAR_TABLE (char_table);
@@ -601,8 +560,7 @@ DEFUN ("set-char-table-range", Fset_char_table_range, Sset_char_table_range,
 RANGE should be t (for all characters), nil (for the default value),
 a cons of character codes (for characters in the range),
 or a character code.  Return VALUE.  */)
 RANGE should be t (for all characters), nil (for the default value),
 a cons of character codes (for characters in the range),
 or a character code.  Return VALUE.  */)
-     (char_table, range, value)
-     Lisp_Object char_table, range, value;
+  (Lisp_Object char_table, Lisp_Object range, Lisp_Object value)
 {
   CHECK_CHAR_TABLE (char_table);
   if (EQ (range, Qt))
 {
   CHECK_CHAR_TABLE (char_table);
   if (EQ (range, Qt))
@@ -634,8 +592,7 @@ DEFUN ("set-char-table-default", Fset_char_table_default,
        Sset_char_table_default, 3, 3, 0,
        doc: /*
 This function is obsolete and has no effect.  */)
        Sset_char_table_default, 3, 3, 0,
        doc: /*
 This function is obsolete and has no effect.  */)
-     (char_table, ch, value)
-     Lisp_Object char_table, ch, value;
+  (Lisp_Object char_table, Lisp_Object ch, Lisp_Object value)
 {
   return Qnil;
 }
 {
   return Qnil;
 }
@@ -644,9 +601,7 @@ This function is obsolete and has no effect.  */)
    integer.  If the element is not a character, return CH itself.  */
 
 int
    integer.  If the element is not a character, return CH itself.  */
 
 int
-char_table_translate (table, ch)
-     Lisp_Object table;
-     int ch;
+char_table_translate (Lisp_Object table, int ch)
 {
   Lisp_Object value;
   value = Faref (table, make_number (ch));
 {
   Lisp_Object value;
   value = Faref (table, make_number (ch));
@@ -656,38 +611,40 @@ char_table_translate (table, ch)
 }
 
 static Lisp_Object
 }
 
 static Lisp_Object
-optimize_sub_char_table (table)
-     Lisp_Object table;
+optimize_sub_char_table (Lisp_Object table, Lisp_Object test)
 {
   struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
   int depth = XINT (tbl->depth);
   Lisp_Object elt, this;
 {
   struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
   int depth = XINT (tbl->depth);
   Lisp_Object elt, this;
-  int i;
+  int i, optimizable;
 
   elt = XSUB_CHAR_TABLE (table)->contents[0];
   if (SUB_CHAR_TABLE_P (elt))
 
   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;
+    elt = XSUB_CHAR_TABLE (table)->contents[0]
+      = optimize_sub_char_table (elt, test);
+  optimizable = SUB_CHAR_TABLE_P (elt) ? 0 : 1;
   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]
   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;
+         = optimize_sub_char_table (this, test);
+      if (optimizable
+         && (NILP (test) ? NILP (Fequal (this, elt)) /* defaults to `equal'. */
+             : EQ (test, Qeq) ? !EQ (this, elt)      /* Optimize `eq' case.  */
+             : NILP (call2 (test, this, elt))))
+       optimizable = 0;
     }
 
     }
 
-  return (i < chartab_size[depth] ? table : elt);
+  return (optimizable ? elt : table);
 }
 
 DEFUN ("optimize-char-table", Foptimize_char_table, Soptimize_char_table,
 }
 
 DEFUN ("optimize-char-table", Foptimize_char_table, Soptimize_char_table,
-       1, 1, 0,
-       doc: /* Optimize CHAR-TABLE.  */)
-     (char_table)
-     Lisp_Object char_table;
+       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'.  */)
+  (Lisp_Object char_table, Lisp_Object test)
 {
   Lisp_Object elt;
   int i;
 {
   Lisp_Object elt;
   int i;
@@ -698,8 +655,12 @@ DEFUN ("optimize-char-table", Foptimize_char_table, Soptimize_char_table,
     {
       elt = XCHAR_TABLE (char_table)->contents[i];
       if (SUB_CHAR_TABLE_P (elt))
     {
       elt = XCHAR_TABLE (char_table)->contents[i];
       if (SUB_CHAR_TABLE_P (elt))
-       XCHAR_TABLE (char_table)->contents[i] = optimize_sub_char_table (elt);
+       XCHAR_TABLE (char_table)->contents[i]
+         = optimize_sub_char_table (elt, test);
     }
     }
+  /* Reset the `ascii' cache, in case it got optimized away.  */
+  XCHAR_TABLE (char_table)->ascii = char_table_ascii (char_table);
+
   return Qnil;
 }
 
   return Qnil;
 }
 
@@ -719,10 +680,9 @@ DEFUN ("optimize-char-table", Foptimize_char_table, Soptimize_char_table,
    following characters in TABLE have the same value.  */
 
 static Lisp_Object
    following characters in TABLE have the same value.  */
 
 static Lisp_Object
-map_sub_char_table (c_function, function, table, arg, val, range,
-                   default_val, parent)
-     void (*c_function) P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
-     Lisp_Object function, table, arg, val, range, default_val, parent;
+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,
+                   Lisp_Object range, Lisp_Object default_val, Lisp_Object parent)
 {
   /* Pointer to the elements of TABLE. */
   Lisp_Object *contents;
 {
   /* Pointer to the elements of TABLE. */
   Lisp_Object *contents;
@@ -777,7 +737,7 @@ map_sub_char_table (c_function, function, table, arg, val, range,
        {
          if (NILP (this))
            this = default_val;
        {
          if (NILP (this))
            this = default_val;
-         if (NILP (Fequal (val, this)))
+         if (!EQ (val, this))
            {
              int different_value = 1;
 
            {
              int different_value = 1;
 
@@ -797,7 +757,7 @@ map_sub_char_table (c_function, function, table, arg, val, range,
                                                parent, arg, val, range,
                                                XCHAR_TABLE (parent)->defalt,
                                                XCHAR_TABLE (parent)->parent);
                                                parent, arg, val, range,
                                                XCHAR_TABLE (parent)->defalt,
                                                XCHAR_TABLE (parent)->parent);
-                     if (! NILP (Fequal (val, this)))
+                     if (EQ (val, this))
                        different_value = 0;
                    }
                }
                        different_value = 0;
                    }
                }
@@ -836,12 +796,9 @@ map_sub_char_table (c_function, function, table, arg, val, range,
    ARG is passed to C_FUNCTION when that is called.  */
 
 void
    ARG is passed to C_FUNCTION when that is called.  */
 
 void
-map_char_table (c_function, function, table, arg)
-     void (*c_function) P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
-     Lisp_Object function, table, arg;
+map_char_table (void (*c_function) (Lisp_Object, Lisp_Object, Lisp_Object), Lisp_Object function, Lisp_Object table, Lisp_Object arg)
 {
   Lisp_Object range, val;
 {
   Lisp_Object range, val;
-  int c, i;
   struct gcpro gcpro1, gcpro2, gcpro3;
 
   range = Fcons (make_number (0), make_number (MAX_CHAR));
   struct gcpro gcpro1, gcpro2, gcpro3;
 
   range = Fcons (make_number (0), make_number (MAX_CHAR));
@@ -899,8 +856,7 @@ Call FUNCTION for each character in CHAR-TABLE that has non-nil value.
 FUNCTION is called with two arguments--a key and a value.
 The key is a character code or a cons of character codes specifying a
 range of characters that have the same value.  */)
 FUNCTION is called with two arguments--a key and a value.
 The key is a character code or a cons of character codes specifying a
 range of characters that have the same value.  */)
-     (function, char_table)
-     Lisp_Object function, char_table;
+  (Lisp_Object function, Lisp_Object char_table)
 {
   CHECK_CHAR_TABLE (char_table);
 
 {
   CHECK_CHAR_TABLE (char_table);
 
@@ -910,12 +866,10 @@ range of characters that have the same value.  */)
 
 
 static void
 
 
 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;
+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)
 {
   struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
   int depth = XINT (tbl->depth);
 {
   struct Lisp_Sub_Char_Table *tbl = XSUB_CHAR_TABLE (table);
   int depth = XINT (tbl->depth);
@@ -975,13 +929,32 @@ map_sub_char_table_for_charset (c_function, function, table, arg, range,
 }
 
 
 }
 
 
+/* Support function for `map-charset-chars'.  Map C_FUNCTION or
+   FUNCTION over TABLE, calling it for each character or a group of
+   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.  */
+
 void
 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;
+map_char_table_for_charset (void (*c_function) (Lisp_Object, Lisp_Object), 
+                           Lisp_Object function, Lisp_Object table, Lisp_Object arg,
+                           struct charset *charset,
+                           unsigned from, unsigned to)
 {
   Lisp_Object range;
   int c, i;
 {
   Lisp_Object range;
   int c, i;
@@ -1025,7 +998,7 @@ map_char_table_for_charset (c_function, function, table, arg,
 
 \f
 void
 
 \f
 void
-syms_of_chartab ()
+syms_of_chartab (void)
 {
   defsubr (&Smake_char_table);
   defsubr (&Schar_table_parent);
 {
   defsubr (&Smake_char_table);
   defsubr (&Schar_table_parent);
@@ -1040,5 +1013,3 @@ syms_of_chartab ()
   defsubr (&Smap_char_table);
 }
 
   defsubr (&Smap_char_table);
 }
 
-/* arch-tag: 18b5b560-7ab5-4108-b09e-d5dd65dc6fda
-   (do not change this comment) */