#
[bpt/emacs.git] / src / coding.c
index 4f91bbb..534a5f7 100644 (file)
@@ -1902,6 +1902,7 @@ encode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
       switch (emacs_code_class[c1])
        {
        case EMACS_ascii_code:
+         c2 = 0;
          ENCODE_ISO_CHARACTER (CHARSET_ASCII, c1, /* dummy */ c2);
          break;
 
@@ -1943,6 +1944,7 @@ encode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
 
        case EMACS_leading_code_2:
          ONE_MORE_BYTE (c2);
+         c3 = 0;
          if (c2 < 0xA0)
            {
              /* invalid sequence */
@@ -1956,6 +1958,7 @@ encode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
 
        case EMACS_leading_code_3:
          TWO_MORE_BYTES (c2, c3);
+         c4 = 0;
          if (c2 < 0xA0 || c3 < 0xA0)
            {
              /* invalid sequence */
@@ -2152,6 +2155,8 @@ encode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
       {                                                                \
        if (sjis_p && charset_alt == charset_katakana_jisx0201) \
          *dst++ = c1;                                          \
+       else if (sjis_p && charset_alt == charset_latin_jisx0201) \
+         *dst++ = c1 & 0x7F;                                   \
        else                                                    \
          {                                                     \
            *dst++ = charset_alt, *dst++ = c1;                  \
@@ -2161,7 +2166,8 @@ encode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
     else                                                       \
       {                                                                \
        c1 &= 0x7F, c2 &= 0x7F;                                 \
-       if (sjis_p && charset_alt == charset_jisx0208)          \
+       if (sjis_p && (charset_alt == charset_jisx0208          \
+                      || charset_alt == charset_jisx0208_1978))\
          {                                                     \
            unsigned char s1, s2;                               \
                                                                \
@@ -2309,7 +2315,10 @@ decode_coding_sjis_big5 (coding, source, destination,
          coding->produced_char++;
        }
       else if (c1 < 0x80)
-       DECODE_SJIS_BIG5_CHARACTER (charset_ascii, c1, /* dummy */ c2);
+        {
+          c2 = 0;               /* avoid warning */
+          DECODE_SJIS_BIG5_CHARACTER (charset_ascii, c1, /* dummy */ c2);
+        }
       else
        {
          if (sjis_p)
@@ -2328,8 +2337,11 @@ decode_coding_sjis_big5 (coding, source, destination,
                }
              else if (c1 < 0xE0)
                /* SJIS -> JISX0201-Kana */
-               DECODE_SJIS_BIG5_CHARACTER (charset_katakana_jisx0201, c1,
-                                           /* dummy */ c2);
+               {
+                 c2 = 0;       /* avoid warning */
+                 DECODE_SJIS_BIG5_CHARACTER (charset_katakana_jisx0201, c1,
+                                             /* dummy */ c2);
+               }
              else
                goto label_invalid_code_1;
            }
@@ -2574,7 +2586,11 @@ decode_eol (coding, source, destination, src_bytes, dst_bytes)
   coding->fake_multibyte = 0;
 
   if (src_bytes <= 0)
-    return result;
+    {
+      coding->produced = coding->produced_char = 0;
+      coding->consumed = coding->consumed_char = 0;
+      return result;
+    }
 
   switch (coding->eol_type)
     {
@@ -2958,9 +2974,9 @@ setup_coding_system (coding_system, coding)
       bzero (coding->safe_charsets, MAX_CHARSET + 1);
       while (CONSP (val))
        {
-         if ((i = get_charset_id (XCONS (val)->car)) >= 0)
+         if ((i = get_charset_id (XCAR (val))) >= 0)
            coding->safe_charsets[i] = 1;
-         val = XCONS (val)->cdr;
+         val = XCDR (val);
        }
     }
 
@@ -3026,12 +3042,12 @@ setup_coding_system (coding_system, coding)
        val = Vcharset_revision_alist;
        while (CONSP (val))
          {
-           charset = get_charset_id (Fcar_safe (XCONS (val)->car));
+           charset = get_charset_id (Fcar_safe (XCAR (val)));
            if (charset >= 0
-               && (temp = Fcdr_safe (XCONS (val)->car), INTEGERP (temp))
+               && (temp = Fcdr_safe (XCAR (val)), INTEGERP (temp))
                && (i = XINT (temp), (i >= 0 && (i + '@') < 128)))
              CODING_SPEC_ISO_REVISION_NUMBER (coding, charset) = i;
-           val = XCONS (val)->cdr;
+           val = XCDR (val);
          }
 
        /* Checks FLAGS[REG] (REG = 0, 1, 2 3) and decide designations.
@@ -3068,28 +3084,28 @@ setup_coding_system (coding_system, coding)
                tail = flags[i];
 
                coding->flags |= CODING_FLAG_ISO_DESIGNATION;
-               if (INTEGERP (XCONS (tail)->car)
-                   && (charset = XINT (XCONS (tail)->car),
+               if (INTEGERP (XCAR (tail))
+                   && (charset = XINT (XCAR (tail)),
                        CHARSET_VALID_P (charset))
-                   || (charset = get_charset_id (XCONS (tail)->car)) >= 0)
+                   || (charset = get_charset_id (XCAR (tail))) >= 0)
                  {
                    CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = charset;
                    CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) =i;
                  }
                else
                  CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = -1;
-               tail = XCONS (tail)->cdr;
+               tail = XCDR (tail);
                while (CONSP (tail))
                  {
-                   if (INTEGERP (XCONS (tail)->car)
-                       && (charset = XINT (XCONS (tail)->car),
+                   if (INTEGERP (XCAR (tail))
+                       && (charset = XINT (XCAR (tail)),
                            CHARSET_VALID_P (charset))
-                       || (charset = get_charset_id (XCONS (tail)->car)) >= 0)
+                       || (charset = get_charset_id (XCAR (tail))) >= 0)
                      CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
                        = i;
-                   else if (EQ (XCONS (tail)->car, Qt))
+                   else if (EQ (XCAR (tail), Qt))
                      reg_bits |= 1 << i;
-                   tail = XCONS (tail)->cdr;
+                   tail = XCDR (tail);
                  }
              }
            else
@@ -3149,22 +3165,12 @@ setup_coding_system (coding_system, coding)
       coding->common_flags
        |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
       {
-       Lisp_Object val;
-       Lisp_Object decoder, encoder;
-
        val = XVECTOR (coding_spec)->contents[4];
-       if (CONSP  (val)
-           && SYMBOLP (XCONS (val)->car)
-           && !NILP (decoder = Fget (XCONS (val)->car, Qccl_program_idx))
-           && !NILP (decoder = Fcdr (Faref (Vccl_program_table, decoder)))
-           && SYMBOLP (XCONS (val)->cdr)
-           && !NILP (encoder = Fget (XCONS (val)->cdr, Qccl_program_idx))
-           && !NILP (encoder = Fcdr (Faref (Vccl_program_table, encoder))))
-         {
-           setup_ccl_program (&(coding->spec.ccl.decoder), decoder);
-           setup_ccl_program (&(coding->spec.ccl.encoder), encoder);
-         }
-       else
+       if (! CONSP (val)
+           || setup_ccl_program (&(coding->spec.ccl.decoder),
+                                 XCAR (val)) < 0
+           || setup_ccl_program (&(coding->spec.ccl.encoder),
+                                 XCDR (val)) < 0)
          goto label_invalid_coding_system;
 
        bzero (coding->spec.ccl.valid_codes, 256);
@@ -3173,18 +3179,18 @@ setup_coding_system (coding_system, coding)
          {
            Lisp_Object this;
 
-           for (; CONSP (val); val = XCONS (val)->cdr)
+           for (; CONSP (val); val = XCDR (val))
              {
-               this = XCONS (val)->car;
+               this = XCAR (val);
                if (INTEGERP (this)
                    && XINT (this) >= 0 && XINT (this) < 256)
                  coding->spec.ccl.valid_codes[XINT (this)] = 1;
                else if (CONSP (this)
-                        && INTEGERP (XCONS (this)->car)
-                        && INTEGERP (XCONS (this)->cdr))
+                        && INTEGERP (XCAR (this))
+                        && INTEGERP (XCDR (this)))
                  {
-                   int start = XINT (XCONS (this)->car);
-                   int end = XINT (XCONS (this)->cdr);
+                   int start = XINT (XCAR (this));
+                   int end = XINT (XCDR (this));
 
                    if (start >= 0 && start <= end && end < 256)
                      while (start <= end)
@@ -3237,6 +3243,7 @@ setup_raw_text_coding_system (coding)
            coding->symbol
              = XVECTOR (subsidiaries)->contents[coding->eol_type];
        }
+      setup_coding_system (coding->symbol, coding);
     }
   return;
 }
@@ -3726,7 +3733,17 @@ ccl_coding_driver (coding, source, destination, src_bytes, dst_bytes, encodep)
 
 /* See "GENERAL NOTES about `decode_coding_XXX ()' functions".  Before
    decoding, it may detect coding system and format of end-of-line if
-   those are not yet decided.  */
+   those are not yet decided.
+
+   This function does not make full use of DESTINATION buffer.  For
+   instance, if coding->type is coding_type_iso2022, it uses only
+   (DST_BYTES - 7) bytes of DESTINATION buffer.  In the case that
+   DST_BYTES is decided by the function decoding_buffer_size, it
+   contains extra 256 bytes (defined by CONVERSION_BUFFER_EXTRA_ROOM).
+   So, this function can decode the full SOURCE.  But, in the other
+   case, if you want to avoid carry over, you must supply at least 7
+   bytes more area in DESTINATION buffer than expected maximum bytes
+   that will be produced by this function.  */
 
 int
 decode_coding (coding, source, destination, src_bytes, dst_bytes)
@@ -3809,7 +3826,17 @@ decode_coding (coding, source, destination, src_bytes, dst_bytes)
   return result;
 }
 
-/* See "GENERAL NOTES about `encode_coding_XXX ()' functions".  */
+/* See "GENERAL NOTES about `encode_coding_XXX ()' functions".
+
+   This function does not make full use of DESTINATION buffer.  For
+   instance, if coding->type is coding_type_iso2022, it uses only
+   (DST_BYTES - 20) bytes of DESTINATION buffer.  In the case that
+   DST_BYTES is decided by the function encoding_buffer_size, it
+   contains extra 256 bytes (defined by CONVERSION_BUFFER_EXTRA_ROOM).
+   So, this function can encode the full SOURCE.  But, in the other
+   case, if you want to avoid carry over, you must supply at least 20
+   bytes more area in DESTINATION buffer than expected maximum bytes
+   that will be produced by this function.  */
 
 int
 encode_coding (coding, source, destination, src_bytes, dst_bytes)
@@ -4405,10 +4432,10 @@ code_convert_region (from, from_byte, to, to_byte, coding, encodep, replace)
   ZV_BYTE -= len_byte;
   Z_BYTE -= len_byte;
 
-  if (GPT - BEG < beg_unchanged)
-    beg_unchanged = GPT - BEG;
-  if (Z - GPT < end_unchanged)
-    end_unchanged = Z - GPT;
+  if (GPT - BEG < BEG_UNCHANGED)
+    BEG_UNCHANGED = GPT - BEG;
+  if (Z - GPT < END_UNCHANGED)
+    END_UNCHANGED = Z - GPT;
 
   for (;;)
     {
@@ -4445,6 +4472,7 @@ code_convert_region (from, from_byte, to, to_byte, coding, encodep, replace)
       if (! encodep && result == CODING_FINISH_INCONSISTENT_EOL)
        {
          unsigned char *pend = dst, *p = pend - inserted_byte;
+         Lisp_Object eol_type;
 
          /* Encode LFs back to the original eol format (CR or CRLF).  */
          if (coding->eol_type == CODING_EOL_CR)
@@ -4458,7 +4486,7 @@ code_convert_region (from, from_byte, to, to_byte, coding, encodep, replace)
              while (p < pend) if (*p++ == '\n') count++;
              if (src - dst < count)
                {
-                 /* We don't have sufficient room for putting LFs
+                 /* We don't have sufficient room for encoding LFs
                     back to CRLF.  We must record converted and
                     not-yet-converted text back to the buffer
                     content, enlarge the gap, then record them out of
@@ -4491,8 +4519,14 @@ code_convert_region (from, from_byte, to, to_byte, coding, encodep, replace)
          /* Suppress eol-format conversion in the further conversion.  */
          coding->eol_type = CODING_EOL_LF;
 
-         /* Restore the original symbol.  */
-         coding->symbol = saved_coding_symbol;
+         /* Set the coding system symbol to that for Unix-like EOL.  */
+         eol_type = Fget (saved_coding_symbol, Qeol_type);
+         if (VECTORP (eol_type)
+             && XVECTOR (eol_type)->size == 3
+             && SYMBOLP (XVECTOR (eol_type)->contents[CODING_EOL_LF]))
+           coding->symbol = XVECTOR (eol_type)->contents[CODING_EOL_LF];
+         else
+           coding->symbol = saved_coding_symbol;
          
          continue;
        }
@@ -4842,13 +4876,13 @@ detect_coding_system (src, src_bytes, highest)
 
   /* At first, gather possible coding systems in VAL.  */
   val = Qnil;
-  for (tmp = Vcoding_category_list; !NILP (tmp); tmp = XCONS (tmp)->cdr)
+  for (tmp = Vcoding_category_list; !NILP (tmp); tmp = XCDR (tmp))
     {
       int idx
-       = XFASTINT (Fget (XCONS (tmp)->car, Qcoding_category_index));
+       = XFASTINT (Fget (XCAR (tmp), Qcoding_category_index));
       if (coding_mask & (1 << idx))
        {
-         val = Fcons (Fsymbol_value (XCONS (tmp)->car), val);
+         val = Fcons (Fsymbol_value (XCAR (tmp)), val);
          if (highest)
            break;
        }
@@ -4857,18 +4891,18 @@ detect_coding_system (src, src_bytes, highest)
     val = Fnreverse (val);
 
   /* Then, replace the elements with subsidiary coding systems.  */
-  for (tmp = val; !NILP (tmp); tmp = XCONS (tmp)->cdr)
+  for (tmp = val; !NILP (tmp); tmp = XCDR (tmp))
     {
       if (eol_type != CODING_EOL_UNDECIDED
          && eol_type != CODING_EOL_INCONSISTENT)
        {
          Lisp_Object eol;
-         eol = Fget (XCONS (tmp)->car, Qeol_type);
+         eol = Fget (XCAR (tmp), Qeol_type);
          if (VECTORP (eol))
-           XCONS (tmp)->car = XVECTOR (eol)->contents[eol_type];
+           XCAR (tmp) = XVECTOR (eol)->contents[eol_type];
        }
     }
-  return (highest ? XCONS (val)->car : val);
+  return (highest ? XCAR (val) : val);
 }  
 
 DEFUN ("detect-coding-region", Fdetect_coding_region, Sdetect_coding_region,
@@ -5290,18 +5324,18 @@ which is a list of all the arguments given to this function.")
   if (NILP (chain))
     return Qnil;
 
-  for (; CONSP (chain); chain = XCONS (chain)->cdr)
+  for (; CONSP (chain); chain = XCDR (chain))
     {
       Lisp_Object elt;
-      elt = XCONS (chain)->car;
+      elt = XCAR (chain);
 
       if (CONSP (elt)
          && ((STRINGP (target)
-              && STRINGP (XCONS (elt)->car)
-              && fast_string_match (XCONS (elt)->car, target) >= 0)
-             || (INTEGERP (target) && EQ (target, XCONS (elt)->car))))
+              && STRINGP (XCAR (elt))
+              && fast_string_match (XCAR (elt), target) >= 0)
+             || (INTEGERP (target) && EQ (target, XCAR (elt)))))
        {
-         val = XCONS (elt)->cdr;
+         val = XCDR (elt);
          /* Here, if VAL is both a valid coding system and a valid
              function symbol, we return VAL as a coding system.  */
          if (CONSP (val))
@@ -5372,13 +5406,13 @@ This function is internal use only.")
 
   while (CONSP (val) && i < CODING_CATEGORY_IDX_MAX)
     {
-      if (! SYMBOLP (XCONS (val)->car))
+      if (! SYMBOLP (XCAR (val)))
        break;
-      idx = XFASTINT (Fget (XCONS (val)->car, Qcoding_category_index));
+      idx = XFASTINT (Fget (XCAR (val), Qcoding_category_index));
       if (idx >= CODING_CATEGORY_IDX_MAX)
        break;
       coding_priorities[i++] = (1 << idx);
-      val = XCONS (val)->cdr;
+      val = XCDR (val);
     }
   /* If coding-category-list is valid and contains all coding
      categories, `i' should be CODING_CATEGORY_IDX_MAX now.  If not,
@@ -5638,11 +5672,16 @@ There are three such tables, `file-coding-system-alist',\n\
 
   DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write,
     "Specify the coding system for write operations.\n\
-It is useful to bind this variable with `let', but do not set it globally.\n\
-If the value is a coding system, it is used for encoding on write operation.\n\
-If not, an appropriate element is used from one of the coding system alists:\n\
+Programs bind this variable with `let', but you should not set it globally.\n\
+If the value is a coding system, it is used for encoding of output,\n\
+when writing it to a file and when sending it to a file or subprocess.\n\
+\n\
+If this does not specify a coding system, an appropriate element\n\
+is used from one of the coding system alists:\n\
 There are three such tables, `file-coding-system-alist',\n\
-`process-coding-system-alist', and `network-coding-system-alist'.");
+`process-coding-system-alist', and `network-coding-system-alist'.\n\
+For output to files, if the above procedure does not specify a coding system,\n\
+the value of `buffer-file-coding-system' is used.");
   Vcoding_system_for_write = Qnil;
 
   DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used,