(Vdecomposition_feature_list): New variable when UTF2000 is defined.
[chise/xemacs-chise.git.1] / src / text-coding.c
index 970a7bf..6485b64 100644 (file)
@@ -1,7 +1,8 @@
 /* Code conversion functions.
    Copyright (C) 1991, 1995 Free Software Foundation, Inc.
    Copyright (C) 1995 Sun Microsystems, Inc.
-   Copyright (C) 1999,2000,2001,2002,2003 MORIOKA Tomohiko
+   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2011
+     MORIOKA Tomohiko
 
 This file is part of XEmacs.
 
@@ -107,8 +108,10 @@ Lisp_Object Qshort, Qno_ascii_eol, Qno_ascii_cntl, Qseven, Qlock_shift;
 #ifdef UTF2000
 Lisp_Object Qutf_8_mcs;
 Lisp_Object Qdisable_composition;
+Lisp_Object Qccs_priority_list;
 Lisp_Object Quse_entity_reference;
 Lisp_Object Qd, Qx, QX;
+Lisp_Object Vdecomposition_feature_list;
 #endif
 Lisp_Object Qencode, Qdecode;
 
@@ -1052,6 +1055,12 @@ if TYPE is 'ccl:
                parse_charset_conversion_specs (codesys->iso2022.output_conv,
                                                value);
              }
+#ifdef UTF2000
+           else if (EQ (key, Qccs_priority_list))
+             {
+               codesys->ccs_priority_list = value;
+             }
+#endif
            else
              signal_simple_error ("Unrecognized property", key);
          }
@@ -1484,6 +1493,8 @@ Return the PROP property of CODING-SYSTEM.
     return XCODING_SYSTEM_DISABLE_COMPOSITION (coding_system) ? Qt : Qnil;
   else if (EQ (prop, Quse_entity_reference))
     return XCODING_SYSTEM_USE_ENTITY_REFERENCE (coding_system) ? Qt : Qnil;
+  else if (EQ (prop, Qccs_priority_list))
+    return XCODING_SYSTEM_CCS_PRIORITY_LIST (coding_system);
 #endif
   else if (type == CODESYS_ISO2022)
     {
@@ -2269,6 +2280,8 @@ do {                                      \
 
 #define DECODING_STREAM_DATA(stream) LSTREAM_TYPE_DATA (stream, decoding)
 
+#define ER_BUF_SIZE 24
+
 struct decoding_stream
 {
   /* Coding system that governs the conversion. */
@@ -2309,8 +2322,9 @@ struct decoding_stream
   unsigned char counter;
 #endif
 #ifdef UTF2000
+  char bom_flag;
   unsigned char er_counter;
-  unsigned char er_buf[16];
+  unsigned char er_buf[ER_BUF_SIZE];
 
   unsigned combined_char_count;
   Emchar combined_chars[16];
@@ -2453,6 +2467,7 @@ reset_decoding_stream (struct decoding_stream *str)
   str->counter = 0;
 #endif /* MULE */
 #ifdef UTF2000
+  str->bom_flag = 0;
   str->er_counter = 0;
   str->combined_char_count = 0;
   str->combining_table = Qnil;
@@ -3244,6 +3259,8 @@ decode_flush_er_chars (struct decoding_stream *str, unsigned_char_dynarr* dst)
     }
 }
 
+EXFUN (Fregexp_quote, 1);
+
 void decode_add_er_char (struct decoding_stream *str, Emchar character,
                         unsigned_char_dynarr* dst);
 void
@@ -3265,7 +3282,7 @@ decode_add_er_char (struct decoding_stream *str, Emchar c,
     {
       Lisp_Object string = make_string (str->er_buf,
                                        str->er_counter);
-      Lisp_Object rest = Vcoded_charset_entity_reference_alist;
+      Lisp_Object rest;
       Lisp_Object cell;
       Lisp_Object ret;
       Lisp_Object pat;
@@ -3273,7 +3290,8 @@ decode_add_er_char (struct decoding_stream *str, Emchar c,
       Lisp_Object char_type;
       int base;
 
-      while (!NILP (rest))
+      for ( rest = Vcoded_charset_entity_reference_alist;
+           !NILP (rest); rest = Fcdr (rest) )
        {                     
          cell = Fcar (rest);
          ccs = Fcar (cell);
@@ -3293,6 +3311,7 @@ decode_add_er_char (struct decoding_stream *str, Emchar c,
            pat = ret;
          else
            continue;
+         pat = Fregexp_quote (pat);
 
          cell = Fcdr (cell);
          cell = Fcdr (cell);
@@ -3331,10 +3350,16 @@ decode_add_er_char (struct decoding_stream *str, Emchar c,
                ? DECODE_CHAR (ccs, code, 0)
                : decode_builtin_char (ccs, code);
 
-             DECODE_ADD_UCS_CHAR (chr, dst);
+             if ( chr >= 0 )
+               DECODE_ADD_UCS_CHAR (chr, dst);
+             else
+               {
+                 Dynarr_add_many (dst, str->er_buf, str->er_counter);
+                 Dynarr_add (dst, ';');
+               }
+
              goto decoded;
            }
-         rest = Fcdr (rest);
        }
       if (!NILP (Fstring_match (build_string ("^&MCS-\\([0-9A-F]+\\)$"),
                                string, Qnil, Qnil)))
@@ -3356,7 +3381,7 @@ decode_add_er_char (struct decoding_stream *str, Emchar c,
     decoded:
       str->er_counter = 0;
     }
-  else if ( (str->er_counter >= 16) || (c >= 0x7F) )
+  else if ( (str->er_counter >= ER_BUF_SIZE) || (c >= 0x7F) )
     {
       Dynarr_add_many (dst, str->er_buf, str->er_counter);
       str->er_counter = 0;
@@ -3375,7 +3400,7 @@ char_encode_as_entity_reference (Emchar ch, char* buf)
   Lisp_Object ccs;
   Lisp_Object char_type;
   int format_columns, idx;
-  char format[18];
+  char format[ER_BUF_SIZE];
 
   while (!NILP (rest))
     {
@@ -3400,14 +3425,15 @@ char_encode_as_entity_reference (Emchar ch, char* buf)
 
              cell = Fcdr (cell);
              ret = Fcar (cell);
-             if (STRINGP (ret) && ((idx = XSTRING_LENGTH (ret)) <= 6))
+             if ( STRINGP (ret) &&
+                  ( (idx = XSTRING_LENGTH (ret)) <= (ER_BUF_SIZE - 4) ) )
                {
                  format[0] = '&';
                  strncpy (&format[1], XSTRING_DATA (ret), idx);
                  idx++;
                }
              else
-               continue;
+               goto try_next;
 
              cell = Fcdr (cell);
              ret = Fcar (cell);
@@ -3415,12 +3441,15 @@ char_encode_as_entity_reference (Emchar ch, char* buf)
                {
                  format[idx++] = '%';
                  format_columns = XINT (ret);
-                 if ( (2 <= format_columns) && (format_columns <= 8) )
+                 if ( (2 <= format_columns) && (format_columns <= 8)
+                      && (idx + format_columns <= ER_BUF_SIZE - 1) )
                    {
                      format [idx++] = '0';
                      format [idx++] = '0' + format_columns;
                    }
                }
+             else
+               goto try_next;
 
              cell = Fcdr (cell);
              ret = Fcar (cell);
@@ -3431,7 +3460,7 @@ char_encode_as_entity_reference (Emchar ch, char* buf)
              else if (EQ (ret, QX))
                format [idx++] = 'X';
              else
-               continue;
+               goto try_next;
              format [idx++] = ';';
              format [idx++] = 0;
 
@@ -3439,6 +3468,7 @@ char_encode_as_entity_reference (Emchar ch, char* buf)
              return;
            }
        }
+    try_next:
       rest = Fcdr (rest);
     }
   sprintf (buf, "&MCS-%08X;", ch);
@@ -3448,7 +3478,7 @@ char_encode_as_entity_reference (Emchar ch, char* buf)
 /************************************************************************/
 /*                          character composition                       */
 /************************************************************************/
-extern Lisp_Object Qcomposition;
+extern Lisp_Object Qcomposition, Qrep_decomposition;
 
 INLINE_HEADER void
 COMPOSE_FLUSH_CHARS (struct decoding_stream *str, unsigned_char_dynarr* dst);
@@ -3474,7 +3504,8 @@ COMPOSE_ADD_CHAR (struct decoding_stream *str,
   else if (!CONSP (str->combining_table))
     {
       Lisp_Object ret
-       = Fget_char_attribute (make_char (character), Qcomposition, Qnil);
+       = Fchar_feature (make_char (character), Qcomposition, Qnil,
+                        Qnil, Qnil);
 
       if (NILP (ret))
        decode_add_er_char (str, character, dst);
@@ -3493,7 +3524,8 @@ COMPOSE_ADD_CHAR (struct decoding_stream *str,
       if (CHARP (ret))
        {
          Emchar char2 = XCHARVAL (ret);
-         Lisp_Object ret2 = Fget_char_attribute (ret, Qcomposition, Qnil);
+         Lisp_Object ret2 = Fchar_feature (ret, Qcomposition, Qnil,
+                                           Qnil, Qnil);
 
          if (NILP (ret2))
            {
@@ -3510,8 +3542,8 @@ COMPOSE_ADD_CHAR (struct decoding_stream *str,
        }
       else
        {
-         ret = Fget_char_attribute (make_char (character), Qcomposition,
-                                    Qnil);
+         ret = Fchar_feature (make_char (character), Qcomposition, Qnil,
+                              Qnil, Qnil);
 
          COMPOSE_FLUSH_CHARS (str, dst);
          if (NILP (ret))
@@ -4476,6 +4508,7 @@ decode_coding_utf8 (Lstream *decoding, const Extbyte *src,
   eol_type_t eol_type  = str->eol_type;
   unsigned char counter        = str->counter;
 #ifdef UTF2000
+  int bom_flag = str->bom_flag;
   Lisp_Object ccs
     = CODING_SYSTEM_ISO2022_INITIAL_CHARSET (DECODING_STREAM_DATA
                                             (decoding)->codesys, 0);
@@ -4491,11 +4524,20 @@ decode_coding_utf8 (Lstream *decoding, const Extbyte *src,
              COMPOSE_FLUSH_CHARS (str, dst);
              decode_flush_er_chars (str, dst);
              DECODE_HANDLE_EOL_TYPE (eol_type, c, flags, dst);
+
+             if ( bom_flag == 0 )
+               bom_flag = -1;
+
              DECODE_ADD_UCS_CHAR (c, dst);
            }
          else if ( c < 0xC0 )
-           /* decode_add_er_char (str, c, dst); */
-           COMPOSE_ADD_CHAR (str, c, dst);
+           {         
+             if ( bom_flag == 0 )
+               bom_flag = -1;
+
+             /* decode_add_er_char (str, c, dst); */
+             COMPOSE_ADD_CHAR (str, c, dst);
+           }
          else
            {
              /* decode_flush_er_chars (str, dst); */
@@ -4533,6 +4575,17 @@ decode_coding_utf8 (Lstream *decoding, const Extbyte *src,
            {
              Emchar char_id;
 
+             if ( bom_flag == 0 )
+               {
+                 if ( cpos == 0xFEFF )
+                   {
+                     bom_flag = 1;
+                     goto decoded;
+                   }
+                 else
+                   bom_flag = -1;
+               }
+
              if (!NILP (ccs))
                {
                  char_id = decode_defined_char (ccs, cpos, 0);
@@ -4543,6 +4596,7 @@ decode_coding_utf8 (Lstream *decoding, const Extbyte *src,
              else
                char_id = cpos;
              COMPOSE_ADD_CHAR (str, char_id, dst);
+           decoded:
              cpos = 0;
              counter = 0;
            }
@@ -4575,6 +4629,9 @@ decode_coding_utf8 (Lstream *decoding, const Extbyte *src,
   str->flags   = flags;
   str->cpos    = cpos;
   str->counter = counter;
+#ifdef UTF2000
+  str->bom_flag = bom_flag;
+#endif
 }
 
 void
@@ -4600,21 +4657,56 @@ char_encode_utf8 (struct encoding_stream *str, Emchar ch,
        = CODING_SYSTEM_ISO2022_INITIAL_CHARSET (str->codesys, 0);
       int code_point = charset_code_point (ucs_ccs, ch, 0);
 
-      if ( (code_point < 0) || (code_point > 0x10FFFF) )
+      if ( (code_point < 0) || (code_point > 0xEFFFF) )
        {
-         Lisp_Object map
-           = CODING_SYSTEM_ISO2022_INITIAL_CHARSET (str->codesys, 1);
-         Lisp_Object ret;
+         Lisp_Object rest = Vdecomposition_feature_list;
+         Lisp_Object decomp_f;
+         Lisp_Object seq = Qnil;
+         Lisp_Object map, ret;
+         struct gcpro gcpro1;
+
+         while ( CONSP (rest) )
+           {
+             decomp_f = XCAR (rest);
+             GCPRO1 (rest);
+             seq = Fchar_feature (make_char (ch), decomp_f, Qnil,
+                                  Qnil, Qnil);
+             UNGCPRO;
+             if ( !NILP (seq) )
+               break;
+             rest = XCDR (rest);
+           }
+
+         if ( CONSP (seq) )
+           {
+             Lisp_Object base = Fcar (seq);
+
+             seq = Fcdr (seq);
+             if ( CHARP (base) && CONSP (seq) )
+               {
+                 Lisp_Object comb = Fcar (seq);
+
+                 if ( CHARP (comb) )
+                   {
+                     char_encode_utf8 (str, XCHAR (base), dst, flags);
+                     char_encode_utf8 (str, XCHAR (comb), dst, flags);
+                     return;
+                   }
+               }
+           }
 
+         map = CODING_SYSTEM_ISO2022_INITIAL_CHARSET (str->codesys, 1);
          if ( !NILP (map)
-              && INTP (ret = Fget_char_attribute (make_char (ch),
-                                                  map, Qnil)) )
+              && INTP (ret = Fchar_feature (make_char (ch),
+                                            map, Qnil,
+                                            Qnil, Qnil)) )
            code_point = XINT (ret);
          else if ( !NILP (map =
                           CODING_SYSTEM_ISO2022_INITIAL_CHARSET
                           (str->codesys, 2))
-                   && INTP (ret = Fget_char_attribute (make_char (ch),
-                                                       map, Qnil)) )
+                   && INTP (ret = Fchar_feature (make_char (ch),
+                                                 map, Qnil,
+                                                 Qnil, Qnil)) )
            code_point = XINT (ret);
          else if (CODING_SYSTEM_USE_ENTITY_REFERENCE (str->codesys))
            {
@@ -5959,7 +6051,19 @@ char_encode_iso2022 (struct encoding_stream *str, Emchar ch,
        {
          Lisp_Object original_default_coded_charset_priority_list
            = Vdefault_coded_charset_priority_list;
-
+         Vdefault_coded_charset_priority_list
+           = CODING_SYSTEM_CCS_PRIORITY_LIST (codesys);
+         while (!EQ (Vdefault_coded_charset_priority_list, Qnil))
+           {
+             code_point = ENCODE_CHAR (ch, charset);
+             if (XCHARSET_FINAL (charset))
+               goto found;
+             Vdefault_coded_charset_priority_list
+               = Fcdr (Fmemq (XCHARSET_NAME (charset),
+                              Vdefault_coded_charset_priority_list));
+           }
+         Vdefault_coded_charset_priority_list
+           = original_default_coded_charset_priority_list;
          while (!EQ (Vdefault_coded_charset_priority_list, Qnil))
            {
              code_point = ENCODE_CHAR (ch, charset);
@@ -6336,6 +6440,7 @@ syms_of_file_coding (void)
 #ifdef UTF2000
   defsymbol (&Qutf_8_mcs, "utf-8-mcs");
   defsymbol (&Qdisable_composition, "disable-composition");
+  defsymbol (&Qccs_priority_list, "ccs-priority-list");
   defsymbol (&Quse_entity_reference, "use-entity-reference");
   defsymbol (&Qd, "d");
   defsymbol (&Qx, "x");
@@ -6464,6 +6569,14 @@ and behaviors of various editing commands.
 Setting this to nil does not do anything.
 */ );
   enable_multibyte_characters = 1;
+
+#ifdef UTF2000
+  DEFVAR_LISP ("decomposition-feature-list",
+              &Vdecomposition_feature_list /*
+List of `=decomposition@FOO' feature to encode characters as IVS.
+*/ );
+  Vdecomposition_feature_list = Qnil;
+#endif
 }
 
 void
@@ -6509,6 +6622,9 @@ complex_vars_of_file_coding (void)
   DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qescape_quoted);
   DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qinput_charset_conversion);
   DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qoutput_charset_conversion);
+#ifdef UTF2000
+  DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qccs_priority_list);
+#endif
 
   DEFINE_CODESYS_PROP (CODESYS_PROP_CCL,     Qencode);
   DEFINE_CODESYS_PROP (CODESYS_PROP_CCL,     Qdecode);