(insert-char-data): Fix format of `iso-10646-comment'; swap order of
[chise/xemacs-chise.git] / src / text-coding.c
index f1dd835..fd1dc8f 100644 (file)
@@ -163,10 +163,11 @@ static void decode_coding_sjis (Lstream *decoding,
                                CONST unsigned char *src,
                                unsigned_char_dynarr *dst,
                                unsigned int n);
-static void encode_coding_sjis (Lstream *encoding,
-                               CONST unsigned char *src,
-                               unsigned_char_dynarr *dst,
-                               unsigned int n);
+void char_encode_shift_jis (struct encoding_stream *str, Emchar c,
+                           unsigned_char_dynarr *dst, unsigned int *flags);
+void char_finish_shift_jis (struct encoding_stream *str,
+                           unsigned_char_dynarr *dst, unsigned int *flags);
+
 static int detect_coding_big5 (struct detection_state *st,
                               CONST unsigned char *src,
                               unsigned int n);
@@ -182,9 +183,11 @@ static int detect_coding_ucs4 (struct detection_state *st,
 static void decode_coding_ucs4 (Lstream *decoding,
                                CONST unsigned char *src,
                                unsigned_char_dynarr *dst, unsigned int n);
-static void encode_coding_ucs4 (Lstream *encoding,
-                               CONST unsigned char *src,
-                               unsigned_char_dynarr *dst, unsigned int n);
+void char_encode_ucs4 (struct encoding_stream *str, Emchar c,
+                      unsigned_char_dynarr *dst, unsigned int *flags);
+void char_finish_ucs4 (struct encoding_stream *str,
+                      unsigned_char_dynarr *dst, unsigned int *flags);
+
 static int detect_coding_utf8 (struct detection_state *st,
                               CONST unsigned char *src,
                               unsigned int n);
@@ -866,10 +869,6 @@ if TYPE is 'ccl:
     CHECK_STRING (doc_string);
   CODING_SYSTEM_DOC_STRING (codesys) = doc_string;
 
-#ifdef UTF2000
-  if (ty == CODESYS_NO_CONVERSION)
-    codesys->fixed.size = 1;
-#endif
   EXTERNAL_PROPERTY_LIST_LOOP (rest, key, value, props)
     {
       if (EQ (key, Qmnemonic))
@@ -2559,6 +2558,15 @@ reset_encoding_stream (struct encoding_stream *str)
     case CODESYS_UTF8:
       str->encode_char = &char_encode_utf8;
       str->finish = &char_finish_utf8;
+      break;
+    case CODESYS_UCS4:
+      str->encode_char = &char_encode_ucs4;
+      str->finish = &char_finish_ucs4;
+      break;
+    case CODESYS_SHIFT_JIS:
+      str->encode_char = &char_encode_shift_jis;
+      str->finish = &char_finish_shift_jis;
+      break;
     default:
       break;
     }
@@ -2674,15 +2682,9 @@ mule_encode (Lstream *encoding, CONST unsigned char *src,
       encode_coding_no_conversion (encoding, src, dst, n);
       break;
 #ifdef MULE
-    case CODESYS_SHIFT_JIS:
-      encode_coding_sjis (encoding, src, dst, n);
-      break;
     case CODESYS_BIG5:
       encode_coding_big5 (encoding, src, dst, n);
       break;
-    case CODESYS_UCS4:
-      encode_coding_ucs4 (encoding, src, dst, n);
-      break;
     case CODESYS_CCL:
       str->ccl.last_block = str->flags & CODING_STATE_END;
       ccl_driver (&str->ccl, src, dst, n, 0, CCL_MODE_ENCODING);
@@ -2769,64 +2771,57 @@ text_encode_generic (Lstream *encoding, CONST unsigned char *src,
   struct encoding_stream *str = ENCODING_STREAM_DATA (encoding);
   unsigned int flags          = str->flags;
   Emchar ch                   = str->ch;
-  Lisp_Object charset;
-  int half;
 
   char_boundary = str->iso2022.current_char_boundary;
-  charset = str->iso2022.current_charset;
-  half = str->iso2022.current_half;
 
   while (n--)
     {
       c = *src++;
 
-      switch (char_boundary)
+      if (char_boundary == 0)
        {
-       case 0:
-         if ( c >= 0xfc )
+         if (c >= 0xfc)
            {
              ch = c & 0x01;
              char_boundary = 5;
            }
-         else if ( c >= 0xf8 )
+         else if (c >= 0xf8)
            {
              ch = c & 0x03;
              char_boundary = 4;
            }
-         else if ( c >= 0xf0 )
+         else if (c >= 0xf0)
            {
              ch = c & 0x07;
              char_boundary = 3;
            }
-         else if ( c >= 0xe0 )
+         else if (c >= 0xe0)
            {
              ch = c & 0x0f;
              char_boundary = 2;
            }
-         else if ( c >= 0xc0 )
+         else if (c >= 0xc0)
            {
              ch = c & 0x1f;
              char_boundary = 1;
            }
          else
-           {
-             (*str->encode_char) (str, c, dst, &flags);
-             ch = 0;
-             char_boundary = 0;
-           }
-         break;
-       case 1:
+           (*str->encode_char) (str, c, dst, &flags);
+       }
+      else if (char_boundary == 1)
+       {
          (*str->encode_char) (str, (ch << 6) | (c & 0x3f), dst, &flags);
          ch =0;
          char_boundary = 0;
-         break;
-       default:
-         ch = ( ch << 6 ) | ( c & 0x3f );
+       }
+      else
+       {
+         ch = (ch << 6) | (c & 0x3f);
          char_boundary--;
        }
     }
 
-  if ( (char_boundary == 0) && flags & CODING_STATE_END)
+  if ((char_boundary == 0) && (flags & CODING_STATE_END))
     {
       (*str->finish) (str, dst, &flags);
     }
@@ -2834,10 +2829,6 @@ text_encode_generic (Lstream *encoding, CONST unsigned char *src,
   str->flags = flags;
   str->ch    = ch;
   str->iso2022.current_char_boundary = char_boundary;
-  str->iso2022.current_charset = charset;
-  str->iso2022.current_half = half;
-
-  /* Verbum caro factum est! */
 }
 
 \f
@@ -2955,6 +2946,11 @@ decode_coding_sjis (Lstream *decoding, CONST unsigned char *src,
              Dynarr_add (dst, c);
 #endif
            }
+#ifdef UTF2000
+         else if (c > 32)
+           DECODE_ADD_UCS_CHAR(MAKE_CHAR(Vcharset_latin_jisx0201,
+                                         c, 0), dst);
+#endif
          else
            DECODE_ADD_BINARY_CHAR (c, dst);
        }
@@ -2967,137 +2963,62 @@ decode_coding_sjis (Lstream *decoding, CONST unsigned char *src,
   str->ch    = ch;
 }
 
-/* Convert internally-formatted data to Shift-JIS. */
+/* Convert internal character representation to Shift_JIS. */
 
-static void
-encode_coding_sjis (Lstream *encoding, CONST unsigned char *src,
-                   unsigned_char_dynarr *dst, unsigned int n)
+void
+char_encode_shift_jis (struct encoding_stream *str, Emchar ch,
+                      unsigned_char_dynarr *dst, unsigned int *flags)
 {
-  unsigned char c;
-  struct encoding_stream *str = ENCODING_STREAM_DATA (encoding);
-  unsigned int flags  = str->flags;
-  unsigned int ch     = str->ch;
   eol_type_t eol_type = CODING_SYSTEM_EOL_TYPE (str->codesys);
-#ifdef UTF2000
-  unsigned char char_boundary = str->iso2022.current_char_boundary;
-#endif
 
-  while (n--)
+  if (ch == '\n')
     {
-      c = *src++;
+      if (eol_type != EOL_LF && eol_type != EOL_AUTODETECT)
+       Dynarr_add (dst, '\r');
+      if (eol_type != EOL_CR)
+       Dynarr_add (dst, ch);
+    }
+  else
+    {
+      Lisp_Object charset;
+      unsigned int c1, c2, s1, s2;
 #ifdef UTF2000
-      switch (char_boundary)
+      Lisp_Object value = charset_code_point (Vcharset_latin_jisx0201, ch);
+      Lisp_Object ret = Fcar (value);
+
+      if (INTP (ret))
        {
-       case 0:
-         if ( c >= 0xfc )
-           {
-             ch = c & 0x01;
-             char_boundary = 5;
-           }
-         else if ( c >= 0xf8 )
-           {
-             ch = c & 0x03;
-             char_boundary = 4;
-           }
-         else if ( c >= 0xf0 )
-           {
-             ch = c & 0x07;
-             char_boundary = 3;
-           }
-         else if ( c >= 0xe0 )
-           {
-             ch = c & 0x0f;
-             char_boundary = 2;
-           }
-         else if ( c >= 0xc0 )
-           {
-             ch = c & 0x1f;
-             char_boundary = 1;
-           }
-         else
-           {
-             ch = 0;
-             if (c == '\n')
-               {
-                 if (eol_type != EOL_LF && eol_type != EOL_AUTODETECT)
-                   Dynarr_add (dst, '\r');
-                 if (eol_type != EOL_CR)
-                   Dynarr_add (dst, c);
-               }
-             else
-               Dynarr_add (dst, c);
-             char_boundary = 0;
-           }
-         break;
-       case 1:
-         ch = ( ch << 6 ) | ( c & 0x3f );
-         {
-           Lisp_Object charset;
-           unsigned int c1, c2, s1, s2;
-           
-           BREAKUP_CHAR (ch, charset, c1, c2);
-           if (EQ(charset, Vcharset_katakana_jisx0201))
-             {
-               Dynarr_add (dst, c1 | 0x80);
-             }
-           else if (EQ(charset, Vcharset_japanese_jisx0208))
-             {
-               ENCODE_SJIS (c1 | 0x80, c2 | 0x80, s1, s2);
-               Dynarr_add (dst, s1);
-               Dynarr_add (dst, s2);
-             }
-         }
-         char_boundary = 0;
-         break;
-       default:
-         ch = ( ch << 6 ) | ( c & 0x3f );
-         char_boundary--;
+         charset = Vcharset_latin_jisx0201;
+         c1 = XINT (ret);
+         c2 = 0;
        }
-#else
-      if (c == '\n')
+      else
+#endif
+       BREAKUP_CHAR (ch, charset, c1, c2);
+         
+      if (EQ(charset, Vcharset_katakana_jisx0201))
        {
-         if (eol_type != EOL_LF && eol_type != EOL_AUTODETECT)
-           Dynarr_add (dst, '\r');
-         if (eol_type != EOL_CR)
-           Dynarr_add (dst, '\n');
-         ch = 0;
+         Dynarr_add (dst, c1 | 0x80);
        }
-      else if (BYTE_ASCII_P (c))
+      else if (c2 == 0)
        {
-         Dynarr_add (dst, c);
-         ch = 0;
+         Dynarr_add (dst, c1);
        }
-      else if (BUFBYTE_LEADING_BYTE_P (c))
-       ch = (c == LEADING_BYTE_KATAKANA_JISX0201 ||
-             c == LEADING_BYTE_JAPANESE_JISX0208_1978 ||
-             c == LEADING_BYTE_JAPANESE_JISX0208) ? c : 0;
-      else if (ch)
+      else if (EQ(charset, Vcharset_japanese_jisx0208))
        {
-         if (ch == LEADING_BYTE_KATAKANA_JISX0201)
-           {
-             Dynarr_add (dst, c);
-             ch = 0;
-           }
-         else if (ch == LEADING_BYTE_JAPANESE_JISX0208_1978 ||
-                  ch == LEADING_BYTE_JAPANESE_JISX0208)
-           ch = c;
-         else
-           {
-             unsigned char j1, j2;
-             ENCODE_SJIS (ch, c, j1, j2);
-             Dynarr_add (dst, j1);
-             Dynarr_add (dst, j2);
-             ch = 0;
-           }
+         ENCODE_SJIS (c1 | 0x80, c2 | 0x80, s1, s2);
+         Dynarr_add (dst, s1);
+         Dynarr_add (dst, s2);
        }
-#endif
+      else
+       Dynarr_add (dst, '?');
     }
+}
 
-  str->flags = flags;
-  str->ch    = ch;
-#ifdef UTF2000
-  str->iso2022.current_char_boundary = char_boundary;
-#endif
+void
+char_finish_shift_jis (struct encoding_stream *str, unsigned_char_dynarr *dst,
+                      unsigned int *flags)
+{
 }
 
 DEFUN ("decode-shift-jis-char", Fdecode_shift_jis_char, 1, 1, 0, /*
@@ -3440,166 +3361,8 @@ Return the corresponding character code in Big5.
 \f
 /************************************************************************/
 /*                           UCS-4 methods                              */
-/*                                                                      */
-/*  UCS-4 character codes are implemented as nonnegative integers.      */
-/*                                                                      */
 /************************************************************************/
 
-Lisp_Object ucs_to_mule_table[65536];
-Lisp_Object mule_to_ucs_table;
-
-DEFUN ("set-ucs-char", Fset_ucs_char, 2, 2, 0, /*
-Map UCS-4 code CODE to Mule character CHARACTER.
-
-Return T on success, NIL on failure.
-*/
-       (code, character))
-{
-  unsigned int c;
-
-  CHECK_CHAR (character);
-  CHECK_INT (code);
-  c = XINT (code);
-
-  if (c < sizeof (ucs_to_mule_table))
-    {
-      ucs_to_mule_table[c] = character;
-      return Qt;
-    }
-  else
-    return Qnil;
-}
-
-static Lisp_Object
-ucs_to_char (unsigned long code)
-{
-  if (code < sizeof (ucs_to_mule_table))
-    {
-      return ucs_to_mule_table[code];
-    }
-  else if ((0xe00000 <= code) && (code <= 0xe00000 + 94 * 94 * 14))
-    {
-      unsigned int c;
-
-      code -= 0xe00000;
-      c = code % (94 * 94);
-      return make_char
-       (MAKE_CHAR (CHARSET_BY_ATTRIBUTES
-                   (CHARSET_TYPE_94X94, code / (94 * 94) + '@',
-                    CHARSET_LEFT_TO_RIGHT),
-                   c / 94 + 33, c % 94 + 33));
-    }
-  else
-    return Qnil;
-}
-
-DEFUN ("ucs-char", Fucs_char, 1, 1, 0, /*
-Return Mule character corresponding to UCS code CODE (a positive integer).
-*/
-       (code))
-{
-  CHECK_NATNUM (code);
-  return ucs_to_char (XINT (code));
-}
-
-DEFUN ("set-char-ucs", Fset_char_ucs, 2, 2, 0, /*
-Map Mule character CHARACTER to UCS code CODE (a positive integer).
-*/
-       (character, code))
-{
-  /* #### Isn't this gilding the lily?  Fput_char_table checks its args.
-          Fset_char_ucs is more restrictive on index arg, but should
-          check code arg in a char_table method. */
-  CHECK_CHAR (character);
-  CHECK_NATNUM (code);
-  return Fput_char_table (character, code, mule_to_ucs_table);
-}
-
-DEFUN ("char-ucs", Fchar_ucs, 1, 1, 0, /*
-Return the UCS code (a positive integer) corresponding to CHARACTER.
-*/
-       (character))
-{
-  return Fget_char_table (character, mule_to_ucs_table);
-}
-
-#ifdef UTF2000
-#define decode_ucs4 DECODE_ADD_UCS_CHAR
-#else
-/* Decode a UCS-4 character into a buffer.  If the lookup fails, use
-   <GETA MARK> (U+3013) of JIS X 0208, which means correct character
-   is not found, instead.
-   #### do something more appropriate (use blob?)
-        Danger, Will Robinson!  Data loss.  Should we signal user? */
-static void
-decode_ucs4 (unsigned long ch, unsigned_char_dynarr *dst)
-{
-  Lisp_Object chr = ucs_to_char (ch);
-
-  if (! NILP (chr))
-    {
-      Bufbyte work[MAX_EMCHAR_LEN];
-      int len;
-
-      ch = XCHAR (chr);
-      len = (ch < 128) ?
-       simple_set_charptr_emchar (work, ch) :
-       non_ascii_set_charptr_emchar (work, ch);
-      Dynarr_add_many (dst, work, len);
-    }
-  else
-    {
-      Dynarr_add (dst, LEADING_BYTE_JAPANESE_JISX0208);
-      Dynarr_add (dst, 34 + 128);
-      Dynarr_add (dst, 46 + 128);
-    }
-}
-#endif
-
-static unsigned long
-mule_char_to_ucs4 (Lisp_Object charset,
-                  unsigned char h, unsigned char l)
-{
-  Lisp_Object code
-    = Fget_char_table (make_char (MAKE_CHAR (charset, h & 127, l & 127)),
-                      mule_to_ucs_table);
-
-  if (INTP (code))
-    {
-      return XINT (code);
-    }
-  else if ( (XCHARSET_DIMENSION (charset) == 2) &&
-           (XCHARSET_CHARS (charset) == 94) )
-    {
-      unsigned char final = XCHARSET_FINAL (charset);
-
-      if ( ('@' <= final) && (final < 0x7f) )
-       {
-         return 0xe00000 + (final - '@') * 94 * 94
-           + ((h & 127) - 33) * 94 + (l & 127) - 33;
-       }
-      else
-       {
-         return '?';
-       }
-    }
-  else
-    {
-      return '?';
-    }
-}
-
-static void
-encode_ucs4 (Lisp_Object charset,
-            unsigned char h, unsigned char l, unsigned_char_dynarr *dst)
-{
-  unsigned long code = mule_char_to_ucs4 (charset, h, l);
-  Dynarr_add (dst,  code >> 24);
-  Dynarr_add (dst, (code >> 16) & 255);
-  Dynarr_add (dst, (code >>  8) & 255);
-  Dynarr_add (dst,  code        & 255);
-}
-
 static int
 detect_coding_ucs4 (struct detection_state *st, CONST unsigned char *src,
                    unsigned int n)
@@ -3644,7 +3407,7 @@ decode_coding_ucs4 (Lstream *decoding, CONST unsigned char *src,
          counter = 3;
          break;
        case 1:
-         decode_ucs4 ( ( ch << 8 ) | c, dst);
+         DECODE_ADD_UCS_CHAR ((ch << 8) | c, dst);
          ch = 0;
          counter = 0;
          break;
@@ -3661,140 +3424,20 @@ decode_coding_ucs4 (Lstream *decoding, CONST unsigned char *src,
   str->counter = counter;
 }
 
-static void
-encode_coding_ucs4 (Lstream *encoding, CONST unsigned char *src,
-                   unsigned_char_dynarr *dst, unsigned int n)
+void
+char_encode_ucs4 (struct encoding_stream *str, Emchar ch,
+                 unsigned_char_dynarr *dst, unsigned int *flags)
 {
-#ifndef UTF2000
-  struct encoding_stream *str = ENCODING_STREAM_DATA (encoding);
-  unsigned int flags = str->flags;
-  unsigned int ch = str->ch;
-  unsigned char char_boundary = str->iso2022.current_char_boundary;
-  Lisp_Object charset = str->iso2022.current_charset;
-
-#ifdef ENABLE_COMPOSITE_CHARS
-  /* flags for handling composite chars.  We do a little switcharoo
-     on the source while we're outputting the composite char. */
-  unsigned int saved_n = 0;
-  CONST unsigned char *saved_src = NULL;
-  int in_composite = 0;
-
- back_to_square_n:
-#endif
-
-  while (n--)
-    {
-      unsigned char c = *src++;
-
-      if (BYTE_ASCII_P (c))
-       {               /* Processing ASCII character */
-         ch = 0;
-         encode_ucs4 (Vcharset_ascii, c, 0, dst);
-         char_boundary = 1;
-       }
-      else if (BUFBYTE_LEADING_BYTE_P (c) || BUFBYTE_LEADING_BYTE_P (ch))
-       { /* Processing Leading Byte */
-         ch = 0;
-         charset = CHARSET_BY_LEADING_BYTE (c);
-         if (LEADING_BYTE_PREFIX_P(c))
-           ch = c;
-         char_boundary = 0;
-       }
-      else
-       {                       /* Processing Non-ASCII character */
-         char_boundary = 1;
-         if (EQ (charset, Vcharset_control_1))
-           {
-             encode_ucs4 (Vcharset_control_1, c, 0, dst);
-           }
-         else
-           {
-             switch (XCHARSET_REP_BYTES (charset))
-               {
-               case 2:
-                 encode_ucs4 (charset, c, 0, dst);
-                 break;
-               case 3:
-                 if (XCHARSET_PRIVATE_P (charset))
-                   {
-                     encode_ucs4 (charset, c, 0, dst);
-                     ch = 0;
-                   }
-                 else if (ch)
-                   {
-#ifdef ENABLE_COMPOSITE_CHARS
-                     if (EQ (charset, Vcharset_composite))
-                       {
-                         if (in_composite)
-                           {
-                             /* #### Bother! We don't know how to
-                                handle this yet. */
-                             Dynarr_add (dst, 0);
-                             Dynarr_add (dst, 0);
-                             Dynarr_add (dst, 0);
-                             Dynarr_add (dst, '~');
-                           }
-                         else
-                           {
-                             Emchar emch = MAKE_CHAR (Vcharset_composite,
-                                                      ch & 0x7F, c & 0x7F);
-                             Lisp_Object lstr = composite_char_string (emch);
-                             saved_n = n;
-                             saved_src = src;
-                             in_composite = 1;
-                             src = XSTRING_DATA   (lstr);
-                             n   = XSTRING_LENGTH (lstr);
-                           }
-                       }
-                     else
-#endif /* ENABLE_COMPOSITE_CHARS */
-                       {
-                         encode_ucs4(charset, ch, c, dst);
-                       }
-                     ch = 0;
-                   }
-                 else
-                   {
-                     ch = c;
-                     char_boundary = 0;
-                   }
-                 break;
-               case 4:
-                 if (ch)
-                   {
-                     encode_ucs4 (charset, ch, c, dst);
-                     ch = 0;
-                   }
-                 else
-                   {
-                     ch = c;
-                     char_boundary = 0;
-                   }
-                 break;
-               default:
-                 abort ();
-               }
-           }
-       }
-    }
-
-#ifdef ENABLE_COMPOSITE_CHARS
-  if (in_composite)
-    {
-      n = saved_n;
-      src = saved_src;
-      in_composite = 0;
-      goto back_to_square_n; /* Wheeeeeeeee ..... */
-    }
-#endif /* ENABLE_COMPOSITE_CHARS */
-
-  str->flags = flags;
-  str->ch = ch;
-  str->iso2022.current_char_boundary = char_boundary;
-  str->iso2022.current_charset = charset;
+  Dynarr_add (dst, ch >> 24);
+  Dynarr_add (dst, ch >> 16);
+  Dynarr_add (dst, ch >>  8);
+  Dynarr_add (dst, ch      );
+}
 
-  /* Verbum caro factum est! */
-#endif
+void
+char_finish_ucs4 (struct encoding_stream *str, unsigned_char_dynarr *dst,
+                 unsigned int *flags)
+{
 }
 
 \f
@@ -3881,12 +3524,12 @@ decode_coding_utf8 (Lstream *decoding, CONST unsigned char *src,
          else
            {
              DECODE_HANDLE_EOL_TYPE (eol_type, c, flags, dst);
-             decode_ucs4 (c, dst);
+             DECODE_ADD_UCS_CHAR (c, dst);
            }
          break;
        case 1:
          ch = ( ch << 6 ) | ( c & 0x3f );
-         decode_ucs4 (ch, dst);
+         DECODE_ADD_UCS_CHAR (ch, dst);
          ch = 0;
          counter = 0;
          break;
@@ -3906,47 +3549,56 @@ decode_coding_utf8 (Lstream *decoding, CONST unsigned char *src,
 }
 
 void
-char_encode_utf8 (struct encoding_stream *str, Emchar code,
+char_encode_utf8 (struct encoding_stream *str, Emchar ch,
                  unsigned_char_dynarr *dst, unsigned int *flags)
 {
-  if ( code <= 0x7f )
+  eol_type_t eol_type = CODING_SYSTEM_EOL_TYPE (str->codesys);
+
+  if (ch == '\n')
     {
-      Dynarr_add (dst, code);
+      if (eol_type != EOL_LF && eol_type != EOL_AUTODETECT)
+       Dynarr_add (dst, '\r');
+      if (eol_type != EOL_CR)
+       Dynarr_add (dst, ch);
     }
-  else if ( code <= 0x7ff )
+  else if (ch <= 0x7f)
     {
-      Dynarr_add (dst, (code >> 6) | 0xc0);
-      Dynarr_add (dst, (code & 0x3f) | 0x80);
+      Dynarr_add (dst, ch);
     }
-  else if ( code <= 0xffff )
+  else if (ch <= 0x7ff)
     {
-      Dynarr_add (dst,  (code >> 12) | 0xe0);
-      Dynarr_add (dst, ((code >>  6) & 0x3f) | 0x80);
-      Dynarr_add (dst,  (code        & 0x3f) | 0x80);
+      Dynarr_add (dst, (ch >> 6) | 0xc0);
+      Dynarr_add (dst, (ch & 0x3f) | 0x80);
     }
-  else if ( code <= 0x1fffff )
+  else if (ch <= 0xffff)
     {
-      Dynarr_add (dst,  (code >> 18) | 0xf0);
-      Dynarr_add (dst, ((code >> 12) & 0x3f) | 0x80);
-      Dynarr_add (dst, ((code >>  6) & 0x3f) | 0x80);
-      Dynarr_add (dst,  (code        & 0x3f) | 0x80);
+      Dynarr_add (dst,  (ch >> 12) | 0xe0);
+      Dynarr_add (dst, ((ch >>  6) & 0x3f) | 0x80);
+      Dynarr_add (dst,  (ch        & 0x3f) | 0x80);
     }
-  else if ( code <= 0x3ffffff )
+  else if (ch <= 0x1fffff)
     {
-      Dynarr_add (dst,  (code >> 24) | 0xf8);
-      Dynarr_add (dst, ((code >> 18) & 0x3f) | 0x80);
-      Dynarr_add (dst, ((code >> 12) & 0x3f) | 0x80);
-      Dynarr_add (dst, ((code >>  6) & 0x3f) | 0x80);
-      Dynarr_add (dst,  (code        & 0x3f) | 0x80);
+      Dynarr_add (dst,  (ch >> 18) | 0xf0);
+      Dynarr_add (dst, ((ch >> 12) & 0x3f) | 0x80);
+      Dynarr_add (dst, ((ch >>  6) & 0x3f) | 0x80);
+      Dynarr_add (dst,  (ch        & 0x3f) | 0x80);
+    }
+  else if (ch <= 0x3ffffff)
+    {
+      Dynarr_add (dst,  (ch >> 24) | 0xf8);
+      Dynarr_add (dst, ((ch >> 18) & 0x3f) | 0x80);
+      Dynarr_add (dst, ((ch >> 12) & 0x3f) | 0x80);
+      Dynarr_add (dst, ((ch >>  6) & 0x3f) | 0x80);
+      Dynarr_add (dst,  (ch        & 0x3f) | 0x80);
     }
   else
     {
-      Dynarr_add (dst,  (code >> 30) | 0xfc);
-      Dynarr_add (dst, ((code >> 24) & 0x3f) | 0x80);
-      Dynarr_add (dst, ((code >> 18) & 0x3f) | 0x80);
-      Dynarr_add (dst, ((code >> 12) & 0x3f) | 0x80);
-      Dynarr_add (dst, ((code >>  6) & 0x3f) | 0x80);
-      Dynarr_add (dst,  (code        & 0x3f) | 0x80);
+      Dynarr_add (dst,  (ch >> 30) | 0xfc);
+      Dynarr_add (dst, ((ch >> 24) & 0x3f) | 0x80);
+      Dynarr_add (dst, ((ch >> 18) & 0x3f) | 0x80);
+      Dynarr_add (dst, ((ch >> 12) & 0x3f) | 0x80);
+      Dynarr_add (dst, ((ch >>  6) & 0x3f) | 0x80);
+      Dynarr_add (dst,  (ch        & 0x3f) | 0x80);
     }
 }
 
@@ -4896,9 +4548,23 @@ decode_coding_iso2022 (Lstream *decoding, CONST unsigned char *src,
                    charset = new_charset;
                }
 
-#ifndef UTF2000
+#ifdef UTF2000
+             if (XCHARSET_DIMENSION (charset) == 1)
+               {
+                 DECODE_OUTPUT_PARTIAL_CHAR (ch);
+                 DECODE_ADD_UCS_CHAR
+                   (MAKE_CHAR (charset, c & 0x7F, 0), dst);
+               }
+             else if (ch)
+               {
+                 DECODE_ADD_UCS_CHAR
+                   (MAKE_CHAR (charset, ch & 0x7F, c & 0x7F), dst);
+                 ch = 0;
+               }
+             else
+               ch = c;
+#else
              lb = XCHARSET_LEADING_BYTE (charset);
-#endif
              switch (XCHARSET_REP_BYTES (charset))
                {
                case 1: /* ASCII */
@@ -4908,44 +4574,25 @@ decode_coding_iso2022 (Lstream *decoding, CONST unsigned char *src,
 
                case 2: /* one-byte official */
                  DECODE_OUTPUT_PARTIAL_CHAR (ch);
-#ifdef UTF2000
-                 DECODE_ADD_UCS_CHAR(MAKE_CHAR(charset, c & 0x7F, 0), dst);
-#else
                  Dynarr_add (dst, lb);
                  Dynarr_add (dst, c | 0x80);
-#endif
                  break;
 
                case 3: /* one-byte private or two-byte official */
-#ifdef UTF2000
-                 if (XCHARSET_DIMENSION (charset) == 1)
-#else
                  if (XCHARSET_PRIVATE_P (charset))
-#endif
                    {
                      DECODE_OUTPUT_PARTIAL_CHAR (ch);
-#ifdef UTF2000
-                     DECODE_ADD_UCS_CHAR(MAKE_CHAR(charset, c & 0x7F, 0),
-                                         dst);
-#else
                      Dynarr_add (dst, PRE_LEADING_BYTE_PRIVATE_1);
                      Dynarr_add (dst, lb);
                      Dynarr_add (dst, c | 0x80);
-#endif
                    }
                  else
                    {
                      if (ch)
                        {
-#ifdef UTF2000
-                         DECODE_ADD_UCS_CHAR(MAKE_CHAR(charset,
-                                                       ch & 0x7F,
-                                                       c & 0x7F), dst);
-#else
                          Dynarr_add (dst, lb);
                          Dynarr_add (dst, ch | 0x80);
                          Dynarr_add (dst, c | 0x80);
-#endif
                          ch = 0;
                        }
                      else
@@ -4956,21 +4603,16 @@ decode_coding_iso2022 (Lstream *decoding, CONST unsigned char *src,
                default:        /* two-byte private */
                  if (ch)
                    {
-#ifdef UTF2000
-                     DECODE_ADD_UCS_CHAR(MAKE_CHAR(charset,
-                                                   ch & 0x7F,
-                                                   c & 0x7F), dst);
-#else
                      Dynarr_add (dst, PRE_LEADING_BYTE_PRIVATE_2);
                      Dynarr_add (dst, lb);
                      Dynarr_add (dst, ch | 0x80);
                      Dynarr_add (dst, c | 0x80);
-#endif
                      ch = 0;
                    }
                  else
                    ch = c;
                }
+#endif
            }
 
          if (!ch)
@@ -5086,8 +4728,8 @@ char_encode_iso2022 (struct encoding_stream *str, Emchar ch,
   Lisp_Coding_System* codesys = str->codesys;
   eol_type_t eol_type         = CODING_SYSTEM_EOL_TYPE (str->codesys);
   int i;
-  Lisp_Object charset;
-  int half;
+  Lisp_Object charset = str->iso2022.current_charset;
+  int half = str->iso2022.current_half;
   unsigned int byte1, byte2;
 
   if (ch <= 0x7F)
@@ -5146,23 +4788,62 @@ char_encode_iso2022 (struct encoding_stream *str, Emchar ch,
     {
       int reg;
 
-      BREAKUP_CHAR (ch, charset, byte1, byte2);
-      ensure_correct_direction (XCHARSET_DIRECTION (charset),
-                               codesys, dst, flags, 0);
-
       /* Now determine which register to use. */
       reg = -1;
       for (i = 0; i < 4; i++)
        {
-         if (EQ (charset, str->iso2022.charset[i]) ||
-             EQ (charset,
-                 CODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, i)))
+         Lisp_Object code_point;
+
+         if ((CHARSETP (charset = str->iso2022.charset[i])
+              && !EQ (code_point = charset_code_point (charset, ch), Qnil))
+             ||
+             (CHARSETP
+              (charset
+               = CODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, i))
+              && !EQ (code_point = charset_code_point (charset, ch), Qnil)))
            {
+             Lisp_Object ret = Fcar (code_point);
+
+             if (INTP (ret))
+               {
+                 byte1 = XINT (ret);
+                 ret = Fcar (Fcdr (code_point));
+                 if (INTP (ret))
+                   byte2 = XINT (ret);
+                 else
+                   byte2 = 0;
+               }
+             else
+               {
+                 byte1 = 0;
+                 byte2 = 0;
+               }
              reg = i;
              break;
            }
        }
-             
+      if (reg == -1)
+       {
+         Lisp_Object original_default_coded_charset_priority_list
+           = Vdefault_coded_charset_priority_list;
+
+         while (!EQ (Vdefault_coded_charset_priority_list, Qnil))
+           {
+             BREAKUP_CHAR (ch, charset, byte1, byte2);
+             if (XCHARSET_FINAL (charset))
+               goto found;
+             Vdefault_coded_charset_priority_list
+               = Fcdr (Fmemq (XCHARSET_NAME (charset),
+                              Vdefault_coded_charset_priority_list));
+           }
+         BREAKUP_CHAR (ch, charset, byte1, byte2);
+       found:
+         Vdefault_coded_charset_priority_list
+           = original_default_coded_charset_priority_list;
+       }
+      ensure_correct_direction (XCHARSET_DIRECTION (charset),
+                               codesys, dst, flags, 0);
+      
       if (reg == -1)
        {
          if (XCHARSET_GRAPHIC (charset) != 0)
@@ -5245,6 +4926,8 @@ char_encode_iso2022 (struct encoding_stream *str, Emchar ch,
          abort ();
        }
     }
+  str->iso2022.current_charset = charset;
+  str->iso2022.current_half = half;
 }
 
 void
@@ -5314,80 +4997,54 @@ encode_coding_no_conversion (Lstream *encoding, CONST unsigned char *src,
     {
       c = *src++;        
 #ifdef UTF2000
-      switch (char_boundary)
+      if (char_boundary == 0)
+       if ( c >= 0xfc )
+         {
+           ch = c & 0x01;
+           char_boundary = 5;
+         }
+       else if ( c >= 0xf8 )
+         {
+           ch = c & 0x03;
+           char_boundary = 4;
+         }
+       else if ( c >= 0xf0 )
+         {
+           ch = c & 0x07;
+           char_boundary = 3;
+         }
+       else if ( c >= 0xe0 )
+         {
+           ch = c & 0x0f;
+           char_boundary = 2;
+         }
+       else if ( c >= 0xc0 )
+         {
+           ch = c & 0x1f;
+           char_boundary = 1;
+         }
+       else
+         {
+           ch = 0;
+           if (c == '\n')
+             {
+               if (eol_type != EOL_LF && eol_type != EOL_AUTODETECT)
+                 Dynarr_add (dst, '\r');
+               if (eol_type != EOL_CR)
+                 Dynarr_add (dst, c);
+             }
+           else
+             Dynarr_add (dst, c);
+           char_boundary = 0;
+         }
+      else if (char_boundary == 1)
        {
-       case 0:
-         if ( c >= 0xfc )
-           {
-             ch = c & 0x01;
-             char_boundary = 5;
-           }
-         else if ( c >= 0xf8 )
-           {
-             ch = c & 0x03;
-             char_boundary = 4;
-           }
-         else if ( c >= 0xf0 )
-           {
-             ch = c & 0x07;
-             char_boundary = 3;
-           }
-         else if ( c >= 0xe0 )
-           {
-             ch = c & 0x0f;
-             char_boundary = 2;
-           }
-         else if ( c >= 0xc0 )
-           {
-             ch = c & 0x1f;
-             char_boundary = 1;
-           }
-         else
-           {
-             ch = 0;
-
-             if (c == '\n')
-               {
-                 if (eol_type != EOL_LF && eol_type != EOL_AUTODETECT)
-                   Dynarr_add (dst, '\r');
-                 if (eol_type != EOL_CR)
-                   Dynarr_add (dst, c);
-               }
-             else
-               Dynarr_add (dst, c);
-             char_boundary = 0;
-           }
-         break;
-       case 1:
          ch = ( ch << 6 ) | ( c & 0x3f );
-         switch ( str->codesys->fixed.size )
-           {
-           case 1:
-             Dynarr_add (dst, ch & 0xff);
-             break;
-           case 2:
-             Dynarr_add (dst, (ch >> 8) & 0xff);
-             Dynarr_add (dst,  ch       & 0xff);
-             break;
-           case 3:
-             Dynarr_add (dst, (ch >> 16) & 0xff);
-             Dynarr_add (dst, (ch >>  8) & 0xff);
-             Dynarr_add (dst,  ch        & 0xff);
-             break;
-           case 4:
-             Dynarr_add (dst, (ch >> 24) & 0xff);
-             Dynarr_add (dst, (ch >> 16) & 0xff);
-             Dynarr_add (dst, (ch >>  8) & 0xff);
-             Dynarr_add (dst,  ch        & 0xff);
-             break;
-           default:
-             fprintf(stderr, "It seems %d bytes stream.\n",
-                     str->codesys->fixed.size);
-             abort ();
-           }
+         Dynarr_add (dst, ch & 0xff);
          char_boundary = 0;
-         break;
-       default:
+       }
+      else
+       {
          ch = ( ch << 6 ) | ( c & 0x3f );
          char_boundary--;
        }
@@ -5642,10 +5299,6 @@ syms_of_file_coding (void)
   DEFSUBR (Fencode_shift_jis_char);
   DEFSUBR (Fdecode_big5_char);
   DEFSUBR (Fencode_big5_char);
-  DEFSUBR (Fset_ucs_char);
-  DEFSUBR (Fucs_char);
-  DEFSUBR (Fset_char_ucs);
-  DEFSUBR (Fchar_ucs);
 #endif /* MULE */
   defsymbol (&Qcoding_system_p, "coding-system-p");
   defsymbol (&Qno_conversion, "no-conversion");
@@ -5874,15 +5527,4 @@ complex_vars_of_file_coding (void)
   coding_category_system[CODING_CATEGORY_UTF8]
    = Fget_coding_system (Qutf8);
 #endif
-
-#ifdef MULE
-  {
-    unsigned int i;
-
-    for (i = 0; i < 65536; i++)
-      ucs_to_mule_table[i] = Qnil;
-  }
-  staticpro (&mule_to_ucs_table);
-  mule_to_ucs_table = Fmake_char_table(Qgeneric);
-#endif /* MULE */
 }