(JC3-70A4): New character.
[chise/xemacs-chise.git-] / src / mule-charset.c
index 7f57ba4..6f9b9bc 100644 (file)
@@ -68,6 +68,7 @@ Lisp_Object Vcharset_ucs;
 Lisp_Object Vcharset_ucs_bmp;
 Lisp_Object Vcharset_ucs_smp;
 Lisp_Object Vcharset_ucs_sip;
+Lisp_Object Vcharset_ucs_gb;
 Lisp_Object Vcharset_ucs_cns;
 Lisp_Object Vcharset_ucs_jis;
 Lisp_Object Vcharset_ucs_ks;
@@ -76,8 +77,8 @@ Lisp_Object Vcharset_latin_viscii;
 Lisp_Object Vcharset_latin_tcvn5712;
 Lisp_Object Vcharset_latin_viscii_lower;
 Lisp_Object Vcharset_latin_viscii_upper;
+Lisp_Object Vcharset_jis_x0208;
 Lisp_Object Vcharset_chinese_big5;
-/* Lisp_Object Vcharset_chinese_big5_cdp; */
 Lisp_Object Vcharset_ideograph_hanziku_1;
 Lisp_Object Vcharset_ideograph_hanziku_2;
 Lisp_Object Vcharset_ideograph_hanziku_3;
@@ -90,8 +91,6 @@ Lisp_Object Vcharset_ideograph_hanziku_9;
 Lisp_Object Vcharset_ideograph_hanziku_10;
 Lisp_Object Vcharset_ideograph_hanziku_11;
 Lisp_Object Vcharset_ideograph_hanziku_12;
-Lisp_Object Vcharset_china3_jef;
-Lisp_Object Vcharset_ideograph_cbeta;
 Lisp_Object Vcharset_ideograph_gt;
 Lisp_Object Vcharset_ideograph_gt_pj_1;
 Lisp_Object Vcharset_ideograph_gt_pj_2;
@@ -130,7 +129,7 @@ struct charset_lookup *chlook;
 static const struct lrecord_description charset_lookup_description_1[] = {
   { XD_LISP_OBJECT_ARRAY, offsetof (struct charset_lookup, charset_by_leading_byte),
 #ifdef UTF2000
-    128+4*128
+    NUM_LEADING_BYTES+4*128
 #else
     128+4*128*2 
 #endif
@@ -308,7 +307,7 @@ Lisp_Object Qleading_byte;
 Lisp_Object Qshort_name, Qlong_name;
 #ifdef UTF2000
 Lisp_Object Qmin_code, Qmax_code, Qcode_offset;
-Lisp_Object Qmother, Qconversion, Q94x60, Q94x94x60;
+Lisp_Object Qmother, Qconversion, Q94x60, Q94x94x60, Qbig5_1, Qbig5_2;
 #endif
 
 Lisp_Object Qascii,
@@ -339,6 +338,7 @@ Lisp_Object Qascii,
   Qucs_bmp,
   Qucs_smp,
   Qucs_sip,
+  Qucs_gb,
   Qucs_cns,
   Qucs_jis,
   Qucs_ks,
@@ -349,6 +349,7 @@ Lisp_Object Qascii,
   Qlatin_viscii_upper,
   Qvietnamese_viscii_lower,
   Qvietnamese_viscii_upper,
+  Qjis_x0208,
   Qchinese_big5,
   /*  Qchinese_big5_cdp, */
   Qideograph_hanziku_1,
@@ -363,8 +364,6 @@ Lisp_Object Qascii,
   Qideograph_hanziku_10,
   Qideograph_hanziku_11,
   Qideograph_hanziku_12,
-  Qchina3_jef,
-  Qideograph_cbeta,
   Qideograph_daikanwa_2,
   Qideograph_daikanwa,
   Qideograph_gt,
@@ -906,79 +905,157 @@ get_unallocated_leading_byte (int dimension)
 #define BIG5_SAME_ROW (0xFF - 0xA1 + 0x7F - 0x40)
 
 Emchar
-decode_builtin_char (Lisp_Object charset, int code_point)
+decode_defined_char (Lisp_Object ccs, int code_point)
 {
-  Lisp_Object mother = XCHARSET_MOTHER (charset);
-  int final;
+  int dim = XCHARSET_DIMENSION (ccs);
+  Lisp_Object decoding_table = XCHARSET_DECODING_TABLE (ccs);
+  Emchar char_id = -1;
+  Lisp_Object mother;
 
-  if ( CHARSETP (mother) )
+  while (dim > 0)
     {
-      int code = code_point;
-
-      if ( XCHARSET_CONVERSION (charset) == CONVERSION_94x60 )
+      dim--;
+      decoding_table
+       = get_ccs_octet_table (decoding_table, ccs,
+                              (code_point >> (dim * 8)) & 255);
+    }
+  if (CHARP (decoding_table))
+    return XCHAR (decoding_table);
+#ifdef HAVE_CHISE_CLIENT
+  if (EQ (decoding_table, Qunloaded))
+    {
+      char_id = load_char_decoding_entry_maybe (ccs, code_point);
+    }
+#endif
+  if (char_id >= 0)
+    return char_id;
+  else if ( CHARSETP (mother = XCHARSET_MOTHER (ccs)) )
+    {
+      if ( XCHARSET_CONVERSION (ccs) == CONVERSION_IDENTICAL )
        {
-         int row = code_point >> 8;
-         int cell = code_point & 255;    
-
-         if (row < 16 + 32)
-           return -1;
-         else if (row < 16 + 32 + 30)
-           code = (row - (16 + 32)) * 94 + cell - 33;
-         else if (row < 18 + 32 + 30)
-           return -1;
-         else if (row < 18 + 32 + 60)
-           code = (row - (18 + 32)) * 94 + cell - 33;
+         if ( EQ (mother, Vcharset_ucs) )
+           return DECODE_CHAR (mother, code_point);
+         else
+           return decode_defined_char (mother, code_point);
        }
-      else if ( XCHARSET_CONVERSION (charset) == CONVERSION_94x94x60 )
+      else if ( XCHARSET_CONVERSION (ccs) == CONVERSION_BIG5_1 )
        {
-         int plane = code_point >> 16;
-         int row = (code_point >> 8) & 255;
-         int cell = code_point & 255;    
-
-         if (row < 16 + 32)
-           return -1;
-         else if (row < 16 + 32 + 30)
-           code
-             = (plane - 33) * 94 * 60
-             + (row - (16 + 32)) * 94
-             + cell - 33;
-         else if (row < 18 + 32 + 30)
-           return -1;
-         else if (row < 18 + 32 + 60)
-           code
-             = (plane - 33) * 94 * 60
-             + (row - (18 + 32)) * 94
-             + cell - 33;
+         unsigned int I
+           = (((code_point >> 8) & 0x7F) - 33) * 94
+           + (( code_point       & 0x7F) - 33);
+         unsigned char b1 = I / (0xFF - 0xA1 + 0x7F - 0x40) + 0xA1;
+         unsigned char b2 = I % (0xFF - 0xA1 + 0x7F - 0x40);
+
+         b2 += b2 < 0x3F ? 0x40 : 0x62;
+         return decode_defined_char (mother, (b1 << 8) | b2);
+       }
+      else if ( XCHARSET_CONVERSION (ccs) == CONVERSION_BIG5_2 )
+       {
+         unsigned int I
+           = (((code_point >> 8) & 0x7F) - 33) * 94
+           + (( code_point       & 0x7F) - 33)
+           + BIG5_SAME_ROW * (0xC9 - 0xA1);
+         unsigned char b1 = I / (0xFF - 0xA1 + 0x7F - 0x40) + 0xA1;
+         unsigned char b2 = I % (0xFF - 0xA1 + 0x7F - 0x40);
+
+         b2 += b2 < 0x3F ? 0x40 : 0x62;
+         return decode_defined_char (mother, (b1 << 8) | b2);
        }
-      return DECODE_CHAR (mother, code + XCHARSET_CODE_OFFSET(charset));
     }
-  else if (EQ (charset, Vcharset_chinese_big5))
+  return -1;
+}
+
+Emchar
+decode_builtin_char (Lisp_Object charset, int code_point)
+{
+  Lisp_Object mother = XCHARSET_MOTHER (charset);
+  int final;
+
+  if ( XCHARSET_MAX_CODE (charset) > 0 )
     {
-      int c1 = code_point >> 8;
-      int c2 = code_point & 0xFF;
-      unsigned int I;
-
-      if ( (  (0xA1 <= c1) && (c1 <= 0xFE)  )
-          &&
-          ( ((0x40 <= c2) && (c2 <= 0x7E)) ||
-            ((0xA1 <= c2) && (c2 <= 0xFE)) ) )
+      if ( CHARSETP (mother) )
        {
-         I = (c1 - 0xA1) * BIG5_SAME_ROW
-           + c2 - (c2 < 0x7F ? 0x40 : 0x62);
+         int code = code_point;
+
+         if ( XCHARSET_CONVERSION (charset) == CONVERSION_94x60 )
+           {
+             int row = code_point >> 8;
+             int cell = code_point & 255;        
+
+             if (row < 16 + 32)
+               return -1;
+             else if (row < 16 + 32 + 30)
+               code = (row - (16 + 32)) * 94 + cell - 33;
+             else if (row < 18 + 32 + 30)
+               return -1;
+             else if (row < 18 + 32 + 60)
+               code = (row - (18 + 32)) * 94 + cell - 33;
+           }
+         else if ( XCHARSET_CONVERSION (charset) == CONVERSION_94x94x60 )
+           {
+             int plane = code_point >> 16;
+             int row = (code_point >> 8) & 255;
+             int cell = code_point & 255;        
 
-         if (c1 < 0xC9)
+             if (row < 16 + 32)
+               return -1;
+             else if (row < 16 + 32 + 30)
+               code
+                 = (plane - 33) * 94 * 60
+                 + (row - (16 + 32)) * 94
+                 + cell - 33;
+             else if (row < 18 + 32 + 30)
+               return -1;
+             else if (row < 18 + 32 + 60)
+               code
+                 = (plane - 33) * 94 * 60
+                 + (row - (18 + 32)) * 94
+                 + cell - 33;
+           }
+         else if ( XCHARSET_CONVERSION (charset) == CONVERSION_BIG5_1 )
            {
-             charset = Vcharset_chinese_big5_1;
+             unsigned int I
+               = (((code_point >> 8) & 0x7F) - 33) * 94
+               + (( code_point       & 0x7F) - 33);
+             unsigned char b1 = I / (0xFF - 0xA1 + 0x7F - 0x40) + 0xA1;
+             unsigned char b2 = I % (0xFF - 0xA1 + 0x7F - 0x40);
+
+             b2 += b2 < 0x3F ? 0x40 : 0x62;
+             code = (b1 << 8) | b2;
            }
-         else
+         else if ( XCHARSET_CONVERSION (charset) == CONVERSION_BIG5_2 )
            {
-             charset = Vcharset_chinese_big5_2;
-             I -= (BIG5_SAME_ROW) * (0xC9 - 0xA1);
+             unsigned int I
+               = (((code_point >> 8) & 0x7F) - 33) * 94
+               + (( code_point       & 0x7F) - 33)
+               + BIG5_SAME_ROW * (0xC9 - 0xA1);
+             unsigned char b1 = I / (0xFF - 0xA1 + 0x7F - 0x40) + 0xA1;
+             unsigned char b2 = I % (0xFF - 0xA1 + 0x7F - 0x40);
+
+             b2 += b2 < 0x3F ? 0x40 : 0x62;
+             code = (b1 << 8) | b2;
            }
-         code_point = ((I / 94 + 33) << 8) | (I % 94 + 33);
+         return
+           decode_builtin_char (mother, code + XCHARSET_CODE_OFFSET(charset));
+       }
+      else
+       {
+         Emchar cid
+           = (XCHARSET_DIMENSION (charset) == 1
+              ?
+              code_point - XCHARSET_BYTE_OFFSET (charset)
+              :
+              ((code_point >> 8) - XCHARSET_BYTE_OFFSET (charset))
+              * XCHARSET_CHARS (charset)
+              + (code_point & 0xFF) - XCHARSET_BYTE_OFFSET (charset))
+           + XCHARSET_CODE_OFFSET (charset);
+         if ((cid < XCHARSET_MIN_CODE (charset))
+             || (XCHARSET_MAX_CODE (charset) < cid))
+           return -1;
+         return cid;
        }
     }
-  if ((final = XCHARSET_FINAL (charset)) >= '0')
+  else if ((final = XCHARSET_FINAL (charset)) >= '0')
     {
       if (XCHARSET_DIMENSION (charset) == 1)
        {
@@ -1015,28 +1092,12 @@ decode_builtin_char (Lisp_Object charset, int code_point)
            }
        }
     }
-  else if (XCHARSET_MAX_CODE (charset))
-    {
-      Emchar cid
-       = (XCHARSET_DIMENSION (charset) == 1
-          ?
-          code_point - XCHARSET_BYTE_OFFSET (charset)
-          :
-          ((code_point >> 8) - XCHARSET_BYTE_OFFSET (charset))
-          * XCHARSET_CHARS (charset)
-          + (code_point & 0xFF) - XCHARSET_BYTE_OFFSET (charset))
-       + XCHARSET_CODE_OFFSET (charset);
-      if ((cid < XCHARSET_MIN_CODE (charset))
-         || (XCHARSET_MAX_CODE (charset) < cid))
-       return -1;
-      return cid;
-    }
   else
     return -1;
 }
 
 int
-charset_code_point (Lisp_Object charset, Emchar ch)
+charset_code_point (Lisp_Object charset, Emchar ch, int defined_only)
 {
   Lisp_Object encoding_table = XCHARSET_ENCODING_TABLE (charset);
   Lisp_Object ret;
@@ -1050,17 +1111,33 @@ charset_code_point (Lisp_Object charset, Emchar ch)
       Lisp_Object mother = XCHARSET_MOTHER (charset);
       int min = XCHARSET_MIN_CODE (charset);
       int max = XCHARSET_MAX_CODE (charset);
-      int code;
+      int code = -1;
 
       if ( CHARSETP (mother) )
-       code = charset_code_point (mother, ch);
-      else
+       {
+         if (XCHARSET_FINAL (charset) >= '0')
+           code = charset_code_point (mother, ch, 1);
+         else
+           code = charset_code_point (mother, ch, defined_only);
+       }
+      else if (defined_only)
+       return -1;
+      else if ( ((max == 0) && CHARSETP (mother)
+                && (XCHARSET_FINAL (charset) == 0))
+               || ((min <= ch) && (ch <= max)) )
        code = ch;
-      if ( (min <= code) && (code <= max) )
+      if ( ((max == 0) && CHARSETP (mother) && (code >= 0))
+          || ((min <= code) && (code <= max)) )
        {
          int d = code - XCHARSET_CODE_OFFSET (charset);
 
-         if ( XCHARSET_CONVERSION (charset) == CONVERSION_94x60 )
+         if ( XCHARSET_CONVERSION (charset) == CONVERSION_IDENTICAL )
+           return d;
+         else if ( XCHARSET_CONVERSION (charset) == CONVERSION_94 )
+           return d + 33;
+         else if ( XCHARSET_CONVERSION (charset) == CONVERSION_96 )
+           return d + 32;
+         else if ( XCHARSET_CONVERSION (charset) == CONVERSION_94x60 )
            {
              int row  = d / 94;
              int cell = d % 94 + 33;
@@ -1071,6 +1148,35 @@ charset_code_point (Lisp_Object charset, Emchar ch)
                row += 18 + 32;
              return (row << 8) | cell;
            }
+         else if ( XCHARSET_CONVERSION (charset) == CONVERSION_BIG5_1 )
+           {
+             int B1 = d >> 8, B2 = d & 0xFF;
+             unsigned int I
+               = (B1 - 0xA1) * BIG5_SAME_ROW + B2
+               - (B2 < 0x7F ? 0x40 : 0x62);
+
+             if (B1 < 0xC9)
+               {
+                 return ((I / 94 + 33) << 8) | (I % 94 + 33);
+               }
+           }
+         else if ( XCHARSET_CONVERSION (charset) == CONVERSION_BIG5_2 )
+           {
+             int B1 = d >> 8, B2 = d & 0xFF;
+             unsigned int I
+               = (B1 - 0xA1) * BIG5_SAME_ROW + B2
+               - (B2 < 0x7F ? 0x40 : 0x62);
+
+             if (B1 >= 0xC9)
+               {
+                 I -= (BIG5_SAME_ROW) * (0xC9 - 0xA1);
+                 return ((I / 94 + 33) << 8) | (I % 94 + 33);
+               }
+           }
+         else if ( XCHARSET_CONVERSION (charset) == CONVERSION_94x94 )
+           return ((d / 94 + 33) << 8) | (d % 94 + 33);
+         else if ( XCHARSET_CONVERSION (charset) == CONVERSION_96x96 )
+           return ((d / 96 + 32) << 8) | (d % 96 + 32);
          else if ( XCHARSET_CONVERSION (charset) == CONVERSION_94x94x60 )
            {
              int plane =  d / (94 * 60) + 33;
@@ -1083,48 +1189,42 @@ charset_code_point (Lisp_Object charset, Emchar ch)
                row += 18 + 32;
              return (plane << 16) | (row << 8) | cell;
            }
-         else if (XCHARSET_CHARS (charset) == 94)
-           {
-             if (XCHARSET_DIMENSION (charset) == 1)
-               return d + 33;
-             else if (XCHARSET_DIMENSION (charset) == 2)
-               return ((d / 94 + 33) << 8) | (d % 94 + 33);
-             else if (XCHARSET_DIMENSION (charset) == 3)
-               return
-                 (   (d / (94 * 94) + 33) << 16)
-                 |  ((d / 94 % 94   + 33) <<  8)
-                 |   (d % 94        + 33);
-             else /* if (XCHARSET_DIMENSION (charset) == 4) */
-               return
-                 (  (d / (94 * 94 * 94) + 33) << 24)
-                 | ((d / (94 * 94) % 94 + 33) << 16)
-                 | ((d / 94 % 94        + 33) <<  8)
-                 |  (d % 94             + 33);
-           }
-         else if (XCHARSET_CHARS (charset) == 96)
+         else if ( XCHARSET_CONVERSION (charset) == CONVERSION_94x94x94 )
+           return
+             (   (d / (94 * 94) + 33) << 16)
+             |  ((d / 94 % 94   + 33) <<  8)
+             |   (d % 94        + 33);
+         else if ( XCHARSET_CONVERSION (charset) == CONVERSION_96x96x96 )
+           return
+             (   (d / (96 * 96) + 32) << 16)
+             |  ((d / 96 % 96   + 32) <<  8)
+             |   (d % 96        + 32);
+         else if ( XCHARSET_CONVERSION (charset) == CONVERSION_94x94x94x94 )
+           return
+             (  (d / (94 * 94 * 94) + 33) << 24)
+             | ((d / (94 * 94) % 94 + 33) << 16)
+             | ((d / 94 % 94        + 33) <<  8)
+             |  (d % 94             + 33);
+         else if ( XCHARSET_CONVERSION (charset) == CONVERSION_96x96x96x96 )
+           return
+             (  (d / (96 * 96 * 96) + 32) << 24)
+             | ((d / (96 * 96) % 96 + 32) << 16)
+             | ((d / 96 % 96        + 32) <<  8)
+             |  (d % 96             + 32);
+         else
            {
-             if (XCHARSET_DIMENSION (charset) == 1)
-               return d + 32;
-             else if (XCHARSET_DIMENSION (charset) == 2)
-               return ((d / 96 + 32) << 8) | (d % 96 + 32);
-             else if (XCHARSET_DIMENSION (charset) == 3)
-               return
-                 (   (d / (96 * 96) + 32) << 16)
-                 |  ((d / 96 % 96   + 32) <<  8)
-                 |   (d % 96        + 32);
-             else /* if (XCHARSET_DIMENSION (charset) == 4) */
-               return
-                 (  (d / (96 * 96 * 96) + 32) << 24)
-                 | ((d / (96 * 96) % 96 + 32) << 16)
-                 | ((d / 96 % 96        + 32) <<  8)
-                 |  (d % 96             + 32);
+             printf ("Unknown CCS-conversion %d is specified!",
+                     XCHARSET_CONVERSION (charset));
+             exit (-1);
            }
-         else
-           return code - XCHARSET_CODE_OFFSET (charset);
        }
-      else if ( (XCHARSET_CODE_OFFSET (charset) == 0) ||
+      else if ( ( XCHARSET_FINAL (charset) >= '0' ) &&
+               ( XCHARSET_MIN_CODE (charset) == 0 )
+              /*
+               (XCHARSET_CODE_OFFSET (charset) == 0) ||
                (XCHARSET_CODE_OFFSET (charset)
-                == XCHARSET_MIN_CODE (charset)) )
+                == XCHARSET_MIN_CODE (charset))
+              */ )
        {
          int d;
 
@@ -1455,7 +1555,7 @@ character set.  Recognized properties are:
 'code-offset   [UTF-2000 only] Offset for a code-point of a base
                coded-charset.
 'conversion    [UTF-2000 only] Conversion for a code-point of a base
-               coded-charset (94x60 or 94x94x60).
+               coded-charset (94x60, 94x94x60, big5-1 or big5-2).
 */
        (name, doc_string, props))
 {
@@ -1487,7 +1587,7 @@ character set.  Recognized properties are:
            short_name = value;
          }
 
-       if (EQ (keyword, Qlong_name))
+       else if (EQ (keyword, Qlong_name))
          {
            CHECK_STRING (value);
            long_name = value;
@@ -1595,6 +1695,10 @@ character set.  Recognized properties are:
              conversion = CONVERSION_94x60;
            else if (EQ (value, Q94x94x60))
              conversion = CONVERSION_94x94x60;
+           else if (EQ (value, Qbig5_1))
+             conversion = CONVERSION_BIG5_1;
+           else if (EQ (value, Qbig5_2))
+             conversion = CONVERSION_BIG5_2;
            else
              signal_simple_error ("Unrecognized conversion", value);
          }
@@ -2024,12 +2128,12 @@ Set mapping-table of CHARSET to TABLE.
   return table;
 }
 
+#ifdef HAVE_CHISE_CLIENT
 DEFUN ("save-charset-mapping-table", Fsave_charset_mapping_table, 1, 1, 0, /*
 Save mapping-table of CHARSET.
 */
        (charset))
 {
-#ifdef HAVE_DATABASE
   struct Lisp_Charset *cs;
   int byte_min, byte_max;
   Lisp_Object db;
@@ -2040,8 +2144,8 @@ Save mapping-table of CHARSET.
 
   db_file = char_attribute_system_db_file (CHARSET_NAME (cs),
                                           Qsystem_char_id, 1);
-  db = Fopen_database (db_file, Qnil, Qnil, Qnil, Qnil);
-      
+  db = Fopen_database (db_file, Qnil, Qnil, build_string ("w+"), Qnil);
+
   byte_min = CHARSET_BYTE_OFFSET (cs);
   byte_max = byte_min + CHARSET_BYTE_SIZE (cs);
   switch (CHARSET_DIMENSION (cs))
@@ -2164,12 +2268,27 @@ Save mapping-table of CHARSET.
       }
     }
   return Fclose_database (db);
-#else
+}
+
+DEFUN ("reset-charset-mapping-table", Freset_charset_mapping_table, 1, 1, 0, /*
+Reset mapping-table of CCS with database file.
+*/
+       (ccs))
+{
+  Lisp_Object db_file;
+
+  ccs = Fget_charset (ccs);
+  db_file = char_attribute_system_db_file (XCHARSET_NAME(ccs),
+                                          Qsystem_char_id, 0);
+
+  if (!NILP (Ffile_exists_p (db_file)))
+    {
+      XCHARSET_DECODING_TABLE(ccs) = Qunloaded;
+      return Qt;
+    }
   return Qnil;
-#endif
 }
 
-#ifdef HAVE_CHISE_CLIENT
 Emchar
 load_char_decoding_entry_maybe (Lisp_Object ccs, int code_point)
 {
@@ -2178,7 +2297,7 @@ load_char_decoding_entry_maybe (Lisp_Object ccs, int code_point)
     = char_attribute_system_db_file (XCHARSET_NAME(ccs), Qsystem_char_id,
                                     0);
 
-  db = Fopen_database (db_file, Qnil, Qnil, Qnil, Qnil);
+  db = Fopen_database (db_file, Qnil, Qnil, build_string ("r"), Qnil);
   if (!NILP (db))
     {
       Lisp_Object ret
@@ -2194,6 +2313,7 @@ load_char_decoding_entry_maybe (Lisp_Object ccs, int code_point)
              return XCHAR (ret);
            }
        }
+      decoding_table_put_char (ccs, code_point, Qnil);
       Fclose_database (db);
     }
   return -1;
@@ -2224,7 +2344,7 @@ If corresponding character is not found, nil is returned.
   if (NILP (defined_only))
     c = DECODE_CHAR (charset, c);
   else
-    c = DECODE_DEFINED_CHAR (charset, c);
+    c = decode_defined_char (charset, c);
   return c >= 0 ? make_char (c) : Qnil;
 }
 
@@ -2364,16 +2484,17 @@ N defaults to 0 if omitted.
 }
 
 #ifdef UTF2000
-DEFUN ("encode-char", Fencode_char, 2, 2, 0, /*
+DEFUN ("encode-char", Fencode_char, 2, 3, 0, /*
 Return code-point of CHARACTER in specified CHARSET.
 */
-       (character, charset))
+       (character, charset, defined_only))
 {
   int code_point;
 
   CHECK_CHAR_COERCE_INT (character);
   charset = Fget_charset (charset);
-  code_point = charset_code_point (charset, XCHAR (character));
+  code_point = charset_code_point (charset, XCHAR (character),
+                                  !NILP (defined_only));
   if (code_point >= 0)
     return make_int (code_point);
   else
@@ -2532,13 +2653,14 @@ syms_of_mule_charset (void)
 #ifdef UTF2000
   DEFSUBR (Fcharset_mapping_table);
   DEFSUBR (Fset_charset_mapping_table);
+#ifdef HAVE_CHISE_CLIENT
+  DEFSUBR (Fsave_charset_mapping_table);
+  DEFSUBR (Freset_charset_mapping_table);
 #endif
 
-#ifdef UTF2000
   DEFSUBR (Fdecode_char);
   DEFSUBR (Fdecode_builtin_char);
   DEFSUBR (Fencode_char);
-  DEFSUBR (Fsave_charset_mapping_table);
 #endif
   DEFSUBR (Fmake_char);
   DEFSUBR (Fchar_charset);
@@ -2566,6 +2688,8 @@ syms_of_mule_charset (void)
   defsymbol (&Qconversion, "conversion");
   defsymbol (&Q94x60, "94x60");
   defsymbol (&Q94x94x60, "94x94x60");
+  defsymbol (&Qbig5_1, "big5-1");
+  defsymbol (&Qbig5_2, "big5-2");
 #endif
 
   defsymbol (&Ql2r, "l2r");
@@ -2601,6 +2725,7 @@ syms_of_mule_charset (void)
   defsymbol (&Qucs_bmp,                        "ucs-bmp");
   defsymbol (&Qucs_smp,                        "ucs-smp");
   defsymbol (&Qucs_sip,                        "ucs-sip");
+  defsymbol (&Qucs_gb,                 "ucs-gb");
   defsymbol (&Qucs_cns,                        "ucs-cns");
   defsymbol (&Qucs_jis,                        "ucs-jis");
   defsymbol (&Qucs_ks,                 "ucs-ks");
@@ -2611,6 +2736,7 @@ syms_of_mule_charset (void)
   defsymbol (&Qlatin_viscii_upper,     "latin-viscii-upper");
   defsymbol (&Qvietnamese_viscii_lower,        "vietnamese-viscii-lower");
   defsymbol (&Qvietnamese_viscii_upper,        "vietnamese-viscii-upper");
+  defsymbol (&Qjis_x0208,              "=jis-x0208");
   defsymbol (&Qideograph_gt,           "ideograph-gt");
   defsymbol (&Qideograph_gt_pj_1,      "ideograph-gt-pj-1");
   defsymbol (&Qideograph_gt_pj_2,      "ideograph-gt-pj-2");
@@ -2639,8 +2765,6 @@ syms_of_mule_charset (void)
   defsymbol (&Qideograph_hanziku_10,   "ideograph-hanziku-10");
   defsymbol (&Qideograph_hanziku_11,   "ideograph-hanziku-11");
   defsymbol (&Qideograph_hanziku_12,   "ideograph-hanziku-12");
-  defsymbol (&Qchina3_jef,             "china3-jef");
-  defsymbol (&Qideograph_cbeta,                "ideograph-cbeta");
   defsymbol (&Qethiopic_ucs,           "ethiopic-ucs");
 #endif
   defsymbol (&Qchinese_big5_1,         "chinese-big5-1");
@@ -2720,7 +2844,7 @@ complex_vars_of_mule_charset (void)
                  build_string ("UCS"),
                  build_string ("ISO/IEC 10646"),
                  build_string (""),
-                 Qnil, 0, 0xFFFFFFF, 0, 0, Qnil, CONVERSION_IDENTICAL);
+                 Qnil, 0, 0x7FFFFFFF, 0, 0, Qnil, CONVERSION_IDENTICAL);
   staticpro (&Vcharset_ucs_bmp);
   Vcharset_ucs_bmp =
     make_charset (LEADING_BYTE_UCS_BMP, Qucs_bmp, 256, 2,
@@ -2751,6 +2875,15 @@ complex_vars_of_mule_charset (void)
                  build_string ("\\(ISO10646.*-2\\|UCS00-2\\)"),
                  Qnil, MIN_CHAR_SIP, MAX_CHAR_SIP,
                  MIN_CHAR_SIP, 0, Qnil, CONVERSION_IDENTICAL);
+  staticpro (&Vcharset_ucs_gb);
+  Vcharset_ucs_gb =
+    make_charset (LEADING_BYTE_UCS_GB, Qucs_gb, 256, 3,
+                 2, 2, 0, CHARSET_LEFT_TO_RIGHT,
+                 build_string ("UCS for GB"),
+                 build_string ("UCS for GB"),
+                 build_string ("ISO/IEC 10646 for GB"),
+                 build_string (""),
+                 Qnil, 0, 0, 0, 0, Vcharset_ucs, CONVERSION_IDENTICAL);
   staticpro (&Vcharset_ucs_cns);
   Vcharset_ucs_cns =
     make_charset (LEADING_BYTE_UCS_CNS, Qucs_cns, 256, 3,
@@ -2759,17 +2892,17 @@ complex_vars_of_mule_charset (void)
                  build_string ("UCS for CNS 11643"),
                  build_string ("ISO/IEC 10646 for CNS 11643"),
                  build_string (""),
-                 Qnil, 0, 0, 0, 0,
-                 Qnil, CONVERSION_IDENTICAL);
+                 Qnil, 0, 0, 0, 0, Vcharset_ucs, CONVERSION_IDENTICAL);
   staticpro (&Vcharset_ucs_jis);
   Vcharset_ucs_jis =
     make_charset (LEADING_BYTE_UCS_JIS, Qucs_jis, 256, 3,
                  2, 2, 0, CHARSET_LEFT_TO_RIGHT,
                  build_string ("UCS for JIS"),
                  build_string ("UCS for JIS X 0208, 0212 and 0213"),
-                 build_string ("ISO/IEC 10646 for JIS X 0208, 0212 and 0213"),
+                 build_string
+                 ("ISO/IEC 10646 for JIS X 0208, 0212 and 0213"),
                  build_string (""),
-                 Qnil, 0, 0, 0, 0, Qnil, CONVERSION_IDENTICAL);
+                 Qnil, 0, 0, 0, 0, Vcharset_ucs, CONVERSION_IDENTICAL);
   staticpro (&Vcharset_ucs_ks);
   Vcharset_ucs_ks =
     make_charset (LEADING_BYTE_UCS_KS, Qucs_ks, 256, 3,
@@ -2778,7 +2911,7 @@ complex_vars_of_mule_charset (void)
                  build_string ("UCS for CCS defined by KS"),
                  build_string ("ISO/IEC 10646 for Korean Standards"),
                  build_string (""),
-                 Qnil, 0, 0, 0, 0, Qnil, CONVERSION_IDENTICAL);
+                 Qnil, 0, 0, 0, 0, Vcharset_ucs, CONVERSION_IDENTICAL);
   staticpro (&Vcharset_ucs_big5);
   Vcharset_ucs_big5 =
     make_charset (LEADING_BYTE_UCS_BIG5, Qucs_big5, 256, 3,
@@ -2787,7 +2920,7 @@ complex_vars_of_mule_charset (void)
                  build_string ("UCS for Big5"),
                  build_string ("ISO/IEC 10646 for Big5"),
                  build_string (""),
-                 Qnil, 0, 0, 0, 0, Qnil, CONVERSION_IDENTICAL);
+                 Qnil, 0, 0, 0, 0, Vcharset_ucs, CONVERSION_IDENTICAL);
 #else
 # define MIN_CHAR_THAI 0
 # define MAX_CHAR_THAI 0
@@ -2858,8 +2991,7 @@ complex_vars_of_mule_charset (void)
                  build_string ("TIS620 (Thai)"),
                  build_string ("TIS620.2529 (Thai)"),
                  build_string ("tis620"),
-                 Qnil, MIN_CHAR_THAI, MAX_CHAR_THAI,
-                 MIN_CHAR_THAI, 32, Qnil, CONVERSION_IDENTICAL);
+                 Qnil, 0, 0, 0, 32, Qnil, CONVERSION_IDENTICAL);
   staticpro (&Vcharset_greek_iso8859_7);
   Vcharset_greek_iso8859_7 =
     make_charset (LEADING_BYTE_GREEK_ISO8859_7, Qgreek_iso8859_7, 96, 1,
@@ -2926,6 +3058,21 @@ complex_vars_of_mule_charset (void)
                  build_string ("ISO8859-9 (Latin-5)"),
                  build_string ("iso8859-9"),
                  Qnil, 0, 0, 0, 32, Qnil, CONVERSION_IDENTICAL);
+#ifdef UTF2000
+  staticpro (&Vcharset_jis_x0208);
+  Vcharset_jis_x0208 =
+    make_charset (LEADING_BYTE_JIS_X0208,
+                 Qjis_x0208, 94, 2,
+                 2, 0, 'B', CHARSET_LEFT_TO_RIGHT,
+                 build_string ("JIS X0208"),
+                 build_string ("JIS X0208 Common"),
+                 build_string ("JIS X0208 Common part"),
+                 build_string ("jisx0208\\.1990"),
+                 Qnil,
+                 MIN_CHAR_JIS_X0208_1990,
+                 MAX_CHAR_JIS_X0208_1990, MIN_CHAR_JIS_X0208_1990, 33,
+                 Qnil, CONVERSION_94x94);
+#endif
   staticpro (&Vcharset_japanese_jisx0208_1978);
   Vcharset_japanese_jisx0208_1978 =
     make_charset (LEADING_BYTE_JAPANESE_JISX0208_1978,
@@ -2936,7 +3083,13 @@ complex_vars_of_mule_charset (void)
                  build_string
                  ("JIS X0208:1978 Japanese Kanji (so called \"old JIS\")"),
                  build_string ("\\(jisx0208\\|jisc6226\\)\\.1978"),
-                 Qnil, 0, 0, 0, 33, Qnil, CONVERSION_IDENTICAL);
+                 Qnil, 0, 0, 0, 33,
+#ifdef UTF2000
+                 Vcharset_jis_x0208,
+#else
+                 Qnil,
+#endif
+                 CONVERSION_IDENTICAL);
   staticpro (&Vcharset_chinese_gb2312);
   Vcharset_chinese_gb2312 =
     make_charset (LEADING_BYTE_CHINESE_GB2312, Qchinese_gb2312, 94, 2,
@@ -2963,7 +3116,13 @@ complex_vars_of_mule_charset (void)
                  build_string ("JIS X0208:1983 (Japanese)"),
                  build_string ("JIS X0208:1983 Japanese Kanji"),
                  build_string ("jisx0208\\.1983"),
-                 Qnil, 0, 0, 0, 33, Qnil, CONVERSION_IDENTICAL);
+                 Qnil, 0, 0, 0, 33,
+#ifdef UTF2000
+                 Vcharset_jis_x0208,
+#else
+                 Qnil,
+#endif
+                 CONVERSION_IDENTICAL);
 #ifdef UTF2000
   staticpro (&Vcharset_japanese_jisx0208_1990);
   Vcharset_japanese_jisx0208_1990 =
@@ -2975,9 +3134,11 @@ complex_vars_of_mule_charset (void)
                  build_string ("JIS X0208:1990 Japanese Kanji"),
                  build_string ("jisx0208\\.1990"),
                  Qnil,
-                 MIN_CHAR_JIS_X0208_1990,
-                 MAX_CHAR_JIS_X0208_1990, MIN_CHAR_JIS_X0208_1990, 33,
-                 Qnil, CONVERSION_IDENTICAL);
+                 0x2121 /* MIN_CHAR_JIS_X0208_1990 */,
+                 0x7426 /* MAX_CHAR_JIS_X0208_1990 */,
+                 0 /* MIN_CHAR_JIS_X0208_1990 */, 33,
+                 Vcharset_jis_x0208 /* Qnil */,
+                 CONVERSION_IDENTICAL /* CONVERSION_94x94 */);
 #endif
   staticpro (&Vcharset_korean_ksc5601);
   Vcharset_korean_ksc5601 =
@@ -3063,11 +3224,10 @@ complex_vars_of_mule_charset (void)
                  build_string ("Big5"),
                  build_string ("Big5"),
                  build_string ("Big5 Chinese traditional"),
-                 build_string ("big5"),
+                 build_string ("big5-0"),
                  Qnil,
-                 0 /* MIN_CHAR_BIG5_CDP */,
-                 0 /* MAX_CHAR_BIG5_CDP */, 0, 0,
-                 Qnil, CONVERSION_IDENTICAL);
+                 MIN_CHAR_BIG5_CDP, MAX_CHAR_BIG5_CDP,
+                 MIN_CHAR_BIG5_CDP, 0, Qnil, CONVERSION_IDENTICAL);
 #if 0
   staticpro (&Vcharset_chinese_big5_cdp);
   Vcharset_chinese_big5_cdp =
@@ -3104,26 +3264,6 @@ complex_vars_of_mule_charset (void)
   DEF_HANZIKU (10);
   DEF_HANZIKU (11);
   DEF_HANZIKU (12);
-  staticpro (&Vcharset_china3_jef);
-  Vcharset_china3_jef =
-    make_charset (LEADING_BYTE_CHINA3_JEF, Qchina3_jef, 256, 2,
-                 2, 2, 0, CHARSET_LEFT_TO_RIGHT,
-                 build_string ("JC3"),
-                 build_string ("JEF + CHINA3"),
-                 build_string ("JEF + CHINA3 private characters"),
-                 build_string ("china3jef-0"),
-                 Qnil, MIN_CHAR_CHINA3_JEF, MAX_CHAR_CHINA3_JEF,
-                 MIN_CHAR_CHINA3_JEF, 0, Qnil, CONVERSION_IDENTICAL);
-  staticpro (&Vcharset_ideograph_cbeta);
-  Vcharset_ideograph_cbeta =
-    make_charset (LEADING_BYTE_CBETA, Qideograph_cbeta, 256, 2,
-                 2, 2, 0, CHARSET_LEFT_TO_RIGHT,
-                 build_string ("CB"),
-                 build_string ("CBETA"),
-                 build_string ("CBETA private characters"),
-                 build_string ("cbeta-0"),
-                 Qnil, MIN_CHAR_CBETA, MAX_CHAR_CBETA,
-                 MIN_CHAR_CBETA, 0, Qnil, CONVERSION_IDENTICAL);
   staticpro (&Vcharset_ideograph_gt);
   Vcharset_ideograph_gt =
     make_charset (LEADING_BYTE_GT, Qideograph_gt, 256, 3,
@@ -3199,7 +3339,8 @@ complex_vars_of_mule_charset (void)
                  build_string
                  ("Big5 Level-1 Chinese traditional"),
                  build_string ("big5"),
-                 Qnil, 0, 0, 0, 33, Qnil, CONVERSION_IDENTICAL);
+                 Qnil, 0, 0, 0, 33, /* Qnil, CONVERSION_IDENTICAL */
+                 Vcharset_chinese_big5, CONVERSION_BIG5_1);
   staticpro (&Vcharset_chinese_big5_2);
   Vcharset_chinese_big5_2 =
     make_charset (LEADING_BYTE_CHINESE_BIG5_2, Qchinese_big5_2, 94, 2,
@@ -3209,7 +3350,8 @@ complex_vars_of_mule_charset (void)
                  build_string
                  ("Big5 Level-2 Chinese traditional"),
                  build_string ("big5"),
-                 Qnil, 0, 0, 0, 33, Qnil, CONVERSION_IDENTICAL);
+                 Qnil, 0, 0, 0, 33, /* Qnil, CONVERSION_IDENTICAL */
+                 Vcharset_chinese_big5, CONVERSION_BIG5_2);
 
 #ifdef ENABLE_COMPOSITE_CHARS
   /* #### For simplicity, we put composite chars into a 96x96 charset.