Include leim/ChangeLog in leim distribution.
[bpt/emacs.git] / src / coding.c
index 1e61804..7d5512d 100644 (file)
@@ -44,41 +44,40 @@ Boston, MA 02111-1307, USA.  */
   0. Emacs' internal format (emacs-mule)
 
   Emacs itself holds a multi-lingual character in a buffer and a string
-  in a special format.  Details are described in the section 2.
+  in a special format.  Details are described in section 2.
 
   1. ISO2022
 
   The most famous coding system for multiple character sets.  X's
-  Compound Text, various EUCs (Extended Unix Code), and such coding
-  systems used in Internet communication as ISO-2022-JP are all
-  variants of ISO2022.  Details are described in the section 3.
+  Compound Text, various EUCs (Extended Unix Code), and coding
+  systems used in Internet communication such as ISO-2022-JP are
+  all variants of ISO2022.  Details are described in section 3.
 
   2. SJIS (or Shift-JIS or MS-Kanji-Code)
    
   A coding system to encode character sets: ASCII, JISX0201, and
   JISX0208.  Widely used for PC's in Japan.  Details are described in
-  the section 4.
+  section 4.
 
   3. BIG5
 
   A coding system to encode character sets: ASCII and Big5.  Widely
   used by Chinese (mainly in Taiwan and Hong Kong).  Details are
-  described in the section 4.  In this file, when written as "BIG5"
-  (all uppercase), it means the coding system, and when written as
-  "Big5" (capitalized), it means the character set.
+  described in section 4.  In this file, when we write "BIG5"
+  (all uppercase), we mean the coding system, and when we write
+  "Big5" (capitalized), we mean the character set.
 
-  4. Else
+  4. Other
 
-  If a user want to read/write a text encoded in a coding system not
+  If a user wants to read/write a text encoded in a coding system not
   listed above, he can supply a decoder and an encoder for it in CCL
   (Code Conversion Language) programs.  Emacs executes the CCL program
   while reading/writing.
 
-  Emacs represent a coding-system by a Lisp symbol that has a property
+  Emacs represents a coding-system by a Lisp symbol that has a property
   `coding-system'.  But, before actually using the coding-system, the
   information about it is set in a structure of type `struct
-  coding_system' for rapid processing.  See the section 6 for more
-  detail.
+  coding_system' for rapid processing.  See section 6 for more details.
 
 */
 
@@ -86,14 +85,13 @@ Boston, MA 02111-1307, USA.  */
 
   How end-of-line of a text is encoded depends on a system.  For
   instance, Unix's format is just one byte of `line-feed' code,
-  whereas DOS's format is two bytes sequence of `carriage-return' and
+  whereas DOS's format is two-byte sequence of `carriage-return' and
   `line-feed' codes.  MacOS's format is one byte of `carriage-return'.
 
-  Since how characters in a text is encoded and how end-of-line is
-  encoded is independent, any coding system described above can take
+  Since text characters encoding and end-of-line encoding are
+  independent, any coding system described above can take
   any format of end-of-line.  So, Emacs has information of format of
-  end-of-line in each coding-system.  See the section 6 for more
-  detail.
+  end-of-line in each coding-system.  See section 6 for more details.
 
 */
 
@@ -117,10 +115,10 @@ detect_coding_emacs_mule (src, src_end)
 
   These functions decode SRC_BYTES length text at SOURCE encoded in
   CODING to Emacs' internal format (emacs-mule).  The resulting text
-  goes to a place pointed by DESTINATION, the length of which should
-  not exceed DST_BYTES.  The bytes actually processed is returned as
-  *CONSUMED.  The return value is the length of the decoded text.
-  Below is a template of these functions.  */
+  goes to a place pointed to by DESTINATION, the length of which should
+  not exceed DST_BYTES.  The number of bytes actually processed is
+  returned as *CONSUMED.  The return value is the length of the decoded
+  text.  Below is a template of these functions.  */
 #if 0
 decode_coding_XXX (coding, source, destination, src_bytes, dst_bytes, consumed)
      struct coding_system *coding;
@@ -136,10 +134,10 @@ decode_coding_XXX (coding, source, destination, src_bytes, dst_bytes, consumed)
 
   These functions encode SRC_BYTES length text at SOURCE of Emacs'
   internal format (emacs-mule) to CODING.  The resulting text goes to
-  a place pointed by DESTINATION, the length of which should not
-  exceed DST_BYTES.  The bytes actually processed is returned as
-  *CONSUMED.  The return value is the length of the encoded text.
-  Below is a template of these functions.  */
+  a place pointed to by DESTINATION, the length of which should not
+  exceed DST_BYTES.  The number of bytes actually processed is
+  returned as *CONSUMED.  The return value is the length of the
+  encoded text.  Below is a template of these functions.  */
 #if 0
 encode_coding_XXX (coding, source, destination, src_bytes, dst_bytes, consumed)
      struct coding_system *coding;
@@ -200,7 +198,7 @@ encode_coding_XXX (coding, source, destination, src_bytes, dst_bytes, consumed)
       *dst++ = (c);                                            \
   } while (0)
 
-/* Decode one DIMENSION1 character of which charset is CHARSET and
+/* Decode one DIMENSION1 character whose charset is CHARSET and whose
    position-code is C.  */
 
 #define DECODE_CHARACTER_DIMENSION1(charset, c)                                \
@@ -215,7 +213,7 @@ encode_coding_XXX (coding, source, destination, src_bytes, dst_bytes, consumed)
     *dst++ = (c) | 0x80;                                               \
   } while (0)
 
-/* Decode one DIMENSION2 character of which charset is CHARSET and
+/* Decode one DIMENSION2 character whose charset is CHARSET and whose
    position-codes are C1 and C2.  */
 
 #define DECODE_CHARACTER_DIMENSION2(charset, c1, c2)   \
@@ -310,7 +308,8 @@ char *coding_category_name[CODING_CATEGORY_IDX_MAX] = {
   "coding-category-iso-7",
   "coding-category-iso-8-1",
   "coding-category-iso-8-2",
-  "coding-category-iso-else",
+  "coding-category-iso-7-else",
+  "coding-category-iso-8-else",
   "coding-category-big5",
   "coding-category-binary"
 };
@@ -337,25 +336,25 @@ Lisp_Object Vdefault_process_coding_system;
 /*** 2. Emacs internal format (emacs-mule) handlers ***/
 
 /* Emacs' internal format for encoding multiple character sets is a
-   kind of multi-byte encoding, i.e. encoding a character by a sequence
-   of one-byte codes of variable length.  ASCII characters and control
-   characters (e.g. `tab', `newline') are represented by one-byte as
-   is.  It takes the range 0x00 through 0x7F.  The other characters
-   are represented by a sequence of `base leading-code', optional
-   `extended leading-code', and one or two `position-code's.  Length
-   of the sequence is decided by the base leading-code.  Leading-code
-   takes the range 0x80 through 0x9F, whereas extended leading-code
-   and position-code take the range 0xA0 through 0xFF.  See the
-   document of `charset.h' for more detail about leading-code and
-   position-code.
-
-   There's one exception in this rule.  Special leading-code
+   kind of multi-byte encoding, i.e. characters are encoded by
+   variable-length sequences of one-byte codes.  ASCII characters
+   and control characters (e.g. `tab', `newline') are represented by
+   one-byte sequences which are their ASCII codes, in the range 0x00
+   through 0x7F.  The other characters are represented by a sequence
+   of `base leading-code', optional `extended leading-code', and one
+   or two `position-code's.  The length of the sequence is determined
+   by the base leading-code.  Leading-code takes the range 0x80
+   through 0x9F, whereas extended leading-code and position-code take
+   the range 0xA0 through 0xFF.  See `charset.h' for more details
+   about leading-code and position-code.
+
+   There's one exception to this rule.  Special leading-code
    `leading-code-composition' denotes that the following several
    characters should be composed into one character.  Leading-codes of
    components (except for ASCII) are added 0x20.  An ASCII character
    component is represented by a 2-byte sequence of `0xA0' and
-   `ASCII-code + 0x80'.  See also the document in `charset.h' for the
-   detail of composite character.  Hence, we can summarize the code
+   `ASCII-code + 0x80'.  See also the comments in `charset.h' for the
+   details of composite character.  Hence, we can summarize the code
    range as follows:
 
    --- CODE RANGE of Emacs' internal format ---
@@ -447,21 +446,21 @@ detect_coding_emacs_mule (src, src_end)
 /*** 3. ISO2022 handlers ***/
 
 /* The following note describes the coding system ISO2022 briefly.
-   Since the intension of this note is to help understanding of the
-   programs in this file, some parts are NOT ACCURATE or OVERLY
+   Since the intention of this note is to help in understanding of
+   the programs in this file, some parts are NOT ACCURATE or OVERLY
    SIMPLIFIED.  For the thorough understanding, please refer to the
    original document of ISO2022.
 
    ISO2022 provides many mechanisms to encode several character sets
-   in 7-bit and 8-bit environment.  If one choose 7-bite environment,
+   in 7-bit and 8-bit environment.  If one chooses 7-bite environment,
    all text is encoded by codes of less than 128.  This may make the
-   encoded text a little bit longer, but the text get more stability
-   to pass through several gateways (some of them split MSB off).
+   encoded text a little bit longer, but the text gets more stability
+   to pass through several gateways (some of them strip off the MSB).
 
-   There are two kind of character set: control character set and
+   There are two kinds of character set: control character set and
    graphic character set.  The former contains control characters such
    as `newline' and `escape' to provide control functions (control
-   functions are provided also by escape sequence).  The latter
+   functions are provided also by escape sequences).  The latter
    contains graphic characters such as ' A' and '-'.  Emacs recognizes
    two control character sets and many graphic character sets.
 
@@ -522,7 +521,7 @@ detect_coding_emacs_mule (src, src_end)
    function            control char    escape sequence description
    ----------------------------------------------------------------------
    SI  (shift-in)              0x0F    none            invoke G0 to GL
-   SI  (shift-out)             0x0E    none            invoke G1 to GL
+   SO  (shift-out)             0x0E    none            invoke G1 to GL
    LS2 (locking-shift-2)       none    ESC 'n'         invoke G2 into GL
    LS3 (locking-shift-3)       none    ESC 'o'         invoke G3 into GL
    SS2 (single-shift-2)                0x8E    ESC 'N'         invoke G2 into GL
@@ -565,7 +564,7 @@ detect_coding_emacs_mule (src, src_end)
    '(' can be omitted.  We call this as "short-form" here after.
 
    Now you may notice that there are a lot of ways for encoding the
-   same multilingual text in ISO2022.  Actually, there exist many
+   same multilingual text in ISO2022.  Actually, there exists many
    coding systems such as Compound Text (used in X's inter client
    communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
    (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
@@ -597,7 +596,8 @@ enum iso_code_class_type iso_code_class[256];
        CODING_CATEGORY_MASK_ISO_7
        CODING_CATEGORY_MASK_ISO_8_1
        CODING_CATEGORY_MASK_ISO_8_2
-       CODING_CATEGORY_MASK_ISO_ELSE
+       CODING_CATEGORY_MASK_ISO_7_ELSE
+       CODING_CATEGORY_MASK_ISO_8_ELSE
    are set.  If a code which should never appear in ISO2022 is found,
    returns 0.  */
 
@@ -608,7 +608,9 @@ detect_coding_iso2022 (src, src_end)
   int mask = (CODING_CATEGORY_MASK_ISO_7
              | CODING_CATEGORY_MASK_ISO_8_1
              | CODING_CATEGORY_MASK_ISO_8_2
-             | CODING_CATEGORY_MASK_ISO_ELSE);
+             | CODING_CATEGORY_MASK_ISO_7_ELSE
+             | CODING_CATEGORY_MASK_ISO_8_ELSE
+             );
   int g1 = 0;                  /* 1 iff designating to G1.  */
   int c, i;
 
@@ -621,34 +623,59 @@ detect_coding_iso2022 (src, src_end)
          if (src >= src_end)
            break;
          c = *src++;
-         if (src < src_end
-             && ((c >= '(' && c <= '/')
-                 || c == '$' && ((*src >= '(' && *src <= '/')
-                                 || (*src >= '@' && *src <= 'B'))))
+         if ((c >= '(' && c <= '/'))
            {
-             /* Valid designation sequence.  */
-             if (c == ')' || (c == '$' && *src == ')'))
+             /* Designation sequence for a charset of dimension 1.  */
+             if (src >= src_end)
+               break;
+             c = *src++;
+             if (c < ' ' || c >= 0x80)
+               /* Invalid designation sequence.  */
+               return 0;
+           }
+         else if (c == '$')
+           {
+             /* Designation sequence for a charset of dimension 2.  */
+             if (src >= src_end)
+               break;
+             c = *src++;
+             if (c >= '@' && c <= 'B')
+               /* Designation for JISX0208.1978, GB2312, or JISX0208.  */
+               ;
+             else if (c >= '(' && c <= '/')
                {
-                 g1 = 1;
-                 mask &= ~CODING_CATEGORY_MASK_ISO_7;
+                 if (src >= src_end)
+                   break;
+                 c = *src++;
+                 if (c < ' ' || c >= 0x80)
+                   /* Invalid designation sequence.  */
+                   return 0;
                }
-             src++;
-             break;
+             else
+               /* Invalid designation sequence.  */
+               return 0;
            }
          else if (c == 'N' || c == 'O' || c == 'n' || c == 'o')
-           return CODING_CATEGORY_MASK_ISO_ELSE;
+           /* Locking shift.  */
+           mask &= (CODING_CATEGORY_MASK_ISO_7_ELSE
+                    | CODING_CATEGORY_MASK_ISO_8_ELSE);
+         else if (c == '0' || c == '1' || c == '2')
+           /* Start/end composition.  */
+           ;
+         else
+           /* Invalid escape sequence.  */
+           return 0;
          break;
 
        case ISO_CODE_SO:
-         if (g1)
-           return CODING_CATEGORY_MASK_ISO_ELSE;
+         mask &= (CODING_CATEGORY_MASK_ISO_7_ELSE
+                  | CODING_CATEGORY_MASK_ISO_8_ELSE);
          break;
          
        case ISO_CODE_CSI:
        case ISO_CODE_SS2:
        case ISO_CODE_SS3:
-         mask &= ~CODING_CATEGORY_MASK_ISO_7;
-         break;
+         return CODING_CATEGORY_MASK_ISO_8_ELSE;
 
        default:
          if (c < 0x80)
@@ -657,12 +684,13 @@ detect_coding_iso2022 (src, src_end)
            return 0;
          else
            {
-             int count = 1;
+             unsigned char *src_begin = src;
 
-             mask &= ~CODING_CATEGORY_MASK_ISO_7;
+             mask &= ~(CODING_CATEGORY_MASK_ISO_7
+                       | CODING_CATEGORY_MASK_ISO_7_ELSE);
              while (src < src_end && *src >= 0xA0)
-               count++, src++;
-             if (count & 1 && src < src_end)
+               src++;
+             if ((src - src_begin - 1) & 1 && src < src_end)
                mask &= ~CODING_CATEGORY_MASK_ISO_8_2;
            }
          break;
@@ -1018,10 +1046,10 @@ decode_coding_iso2022 (coding, source, destination,
   return dst - destination;
 }
 
-/* ISO2022 encoding staffs.  */
+/* ISO2022 encoding stuff.  */
 
 /*
-   It is not enough to say just "ISO2022" on encoding, but we have to
+   It is not enough to say just "ISO2022" on encoding, we have to
    specify more details.  In Emacs, each coding-system of ISO2022
    variant has the following specifications:
        1. Initial designation to G0 thru G3.
@@ -1036,7 +1064,7 @@ decode_coding_iso2022 (coding, source, destination,
        9. Use JISX0208-1983 in place of JISX0208-1978?
    These specifications are encoded in `coding->flags' as flag bits
    defined by macros CODING_FLAG_ISO_XXX.  See `coding.h' for more
-   detail.
+   details.
 */
 
 /* Produce codes (escape sequence) for designating CHARSET to graphic
@@ -1132,8 +1160,8 @@ decode_coding_iso2022 (coding, source, destination,
     CODING_SPEC_ISO_INVOCATION (coding, 0) = 3;        \
   } while (0)
 
-/* Produce codes for a DIMENSION1 character of which character set is
-   CHARSET and position-code is C1.  Designation and invocation
+/* Produce codes for a DIMENSION1 character whose character set is
+   CHARSET and whose position-code is C1.  Designation and invocation
    sequences are also produced in advance if necessary.  */
 
 
@@ -1166,8 +1194,8 @@ decode_coding_iso2022 (coding, source, destination,
       dst = encode_invocation_designation (charset, coding, dst);      \
   } while (1)
 
-/* Produce codes for a DIMENSION2 character of which character set is
-   CHARSET and position-codes are C1 and C2.  Designation and
+/* Produce codes for a DIMENSION2 character whose character set is
+   CHARSET and whose position-codes are C1 and C2.  Designation and
    invocation codes are also produced in advance if necessary.  */
 
 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2)               \
@@ -1488,12 +1516,26 @@ encode_coding_iso2022 (coding, source, destination,
 
        case EMACS_leading_code_2:
          ONE_MORE_BYTE (c2);
-         ENCODE_ISO_CHARACTER (c1, c2, /* dummy */ c3);
+         if (c2 < 0xA0)
+           {
+             /* invalid sequence */
+             *dst++ = c1;
+             *dst++ = c2;
+           }
+         else
+           ENCODE_ISO_CHARACTER (c1, c2, /* dummy */ c3);
          break;
 
        case EMACS_leading_code_3:
          TWO_MORE_BYTES (c2, c3);
-         if (c1 < LEADING_CODE_PRIVATE_11)
+         if (c2 < 0xA0 || c3 < 0xA0)
+           {
+             /* invalid sequence */
+             *dst++ = c1;
+             *dst++ = c2;
+             *dst++ = c3;
+           }
+         else if (c1 < LEADING_CODE_PRIVATE_11)
            ENCODE_ISO_CHARACTER (c1, c2, c3);
          else
            ENCODE_ISO_CHARACTER (c2, c3, /* dummy */ c4);
@@ -1501,12 +1543,27 @@ encode_coding_iso2022 (coding, source, destination,
 
        case EMACS_leading_code_4:
          THREE_MORE_BYTES (c2, c3, c4);
-         ENCODE_ISO_CHARACTER (c2, c3, c4);
+         if (c2 < 0xA0 || c3 < 0xA0 || c4 < 0xA0)
+           {
+             /* invalid sequence */
+             *dst++ = c1;
+             *dst++ = c2;
+             *dst++ = c3;
+             *dst++ = c4;
+           }
+         else
+           ENCODE_ISO_CHARACTER (c2, c3, c4);
          break;
 
        case EMACS_leading_code_composition:
-         ONE_MORE_BYTE (c1);
-         if (c1 == 0xFF)
+         ONE_MORE_BYTE (c2);
+         if (c2 < 0xA0)
+           {
+             /* invalid sequence */
+             *dst++ = c1;
+             *dst++ = c2;
+           }
+         else if (c2 == 0xFF)
            {
              coding->composing = COMPOSING_WITH_RULE_HEAD;
              ENCODE_COMPOSITION_WITH_RULE_START;
@@ -1527,8 +1584,11 @@ encode_coding_iso2022 (coding, source, destination,
        }
       continue;
     label_end_of_loop:
-      coding->carryover_size = src - src_base;
+      /* We reach here because the source date ends not at character
+        boundary.  */
+      coding->carryover_size = src_end - src_base;
       bcopy (src_base, coding->carryover, coding->carryover_size);
+      src = src_end;
       break;
     }
 
@@ -1552,7 +1612,7 @@ encode_coding_iso2022 (coding, source, destination,
 \f
 /*** 4. SJIS and BIG5 handlers ***/
 
-/* Although SJIS and BIG5 are not ISO's coding system, They are used
+/* Although SJIS and BIG5 are not ISO's coding system, they are used
    quite widely.  So, for the moment, Emacs supports them in the bare
    C code.  But, in the future, they may be supported only by CCL.  */
 
@@ -1939,9 +1999,9 @@ encode_coding_sjis_big5 (coding, source, destination,
       continue;
 
     label_end_of_loop:
-      coding->carryover_size = src - src_base;
+      coding->carryover_size = src_end - src_base;
       bcopy (src_base, coding->carryover, coding->carryover_size);
-      src = src_base;
+      src = src_end;
       break;
     }
 
@@ -2167,7 +2227,7 @@ setup_coding_system (coding_system, coding)
 {
   Lisp_Object type, eol_type;
 
-  /* At first, set several fields default values.  */
+  /* At first, set several fields to default values.  */
   coding->require_flushing = 0;
   coding->last_block = 0;
   coding->selective = 0;
@@ -2445,34 +2505,44 @@ setup_coding_system (coding_system, coding)
 
        The category for a coding system which has the same code range
        as SJIS.  Assigned the coding-system (Lisp
-       symbol) `shift-jis' by default.
+       symbol) `japanese-shift-jis' by default.
 
    o coding-category-iso-7
 
        The category for a coding system which has the same code range
-       as ISO2022 of 7-bit environment.  Assigned the coding-system
-       (Lisp symbol) `iso-2022-7' by default.
+       as ISO2022 of 7-bit environment.  This doesn't use any locking
+       shift and single shift functions.  Assigned the coding-system
+       (Lisp symbol) `iso-2022-7bit' by default.
 
    o coding-category-iso-8-1
 
        The category for a coding system which has the same code range
        as ISO2022 of 8-bit environment and graphic plane 1 used only
-       for DIMENSION1 charset.  Assigned the coding-system (Lisp
-       symbol) `iso-8859-1' by default.
+       for DIMENSION1 charset.  This doesn't use any locking shift
+       and single shift functions.  Assigned the coding-system (Lisp
+       symbol) `iso-latin-1' by default.
 
    o coding-category-iso-8-2
 
        The category for a coding system which has the same code range
        as ISO2022 of 8-bit environment and graphic plane 1 used only
-       for DIMENSION2 charset.  Assigned the coding-system (Lisp
-       symbol) `euc-japan' by default.
+       for DIMENSION2 charset.  This doesn't use any locking shift
+       and single shift functions.  Assigned the coding-system (Lisp
+       symbol) `japanese-iso-8bit' by default.
+
+   o coding-category-iso-7-else
+
+       The category for a coding system which has the same code range
+       as ISO2022 of 7-bit environemnt but uses locking shift or
+       single shift functions.  Assigned the coding-system (Lisp
+       symbol) `iso-2022-7bit-lock' by default.
 
-   o coding-category-iso-else
+   o coding-category-iso-8-else
 
        The category for a coding system which has the same code range
-       as ISO2022 but not belongs to any of the above three
-       categories.  Assigned the coding-system (Lisp symbol)
-       `iso-2022-ss2-7' by default.
+       as ISO2022 of 8-bit environemnt but uses locking shift or
+       single shift functions.  Assigned the coding-system (Lisp
+       symbol) `iso-2022-8bit-ss2' by default.
 
    o coding-category-big5
 
@@ -2538,19 +2608,33 @@ detect_coding_mask (src, src_bytes)
        /* No valid ISO2022 code follows C.  Try again.  */
        goto label_loop_detect_coding;
     }
-  else if (c == ISO_CODE_SS2 || c == ISO_CODE_SS3 || c == ISO_CODE_CSI)
+  else if (c == ISO_CODE_SS2 || c == ISO_CODE_SS3)
     /* C is an ISO2022 specific control code of C1,
        or the first byte of SJIS's 2-byte character code,
        or a leading code of Emacs.  */
     mask = (detect_coding_iso2022 (src, src_end)
            | detect_coding_sjis (src, src_end)
-           | detect_coding_emacs_mule (src, src_end));
-
+           | detect_coding_emacs_mule (src, src_end)
+           | CODING_CATEGORY_MASK_BINARY);
+
+  else if (c == ISO_CODE_CSI
+          && (src < src_end
+             && (*src == ']'
+                 || (src + 1 < src_end
+                     && src[1] == ']'
+                     && (*src == '0' || *src == '1' || *src == '2')))))
+    /* C is an ISO2022's control-sequence-introducer.  */
+    mask = (detect_coding_iso2022 (src, src_end)
+           | detect_coding_sjis (src, src_end)
+           | detect_coding_emacs_mule (src, src_end)
+           | CODING_CATEGORY_MASK_BINARY);
+    
   else if (c < 0xA0)
     /* C is the first byte of SJIS character code,
        or a leading-code of Emacs.  */
     mask = (detect_coding_sjis (src, src_end)
-           | detect_coding_emacs_mule (src, src_end));
+           | detect_coding_emacs_mule (src, src_end)
+           | CODING_CATEGORY_MASK_BINARY);
 
   else
     /* C is a character of ISO2022 in graphic plane right,
@@ -2558,7 +2642,8 @@ detect_coding_mask (src, src_bytes)
        or the first byte of BIG5's 2-byte code.  */
     mask = (detect_coding_iso2022 (src, src_end)
            | detect_coding_sjis (src, src_end)
-           | detect_coding_big5 (src, src_end));
+           | detect_coding_big5 (src, src_end)
+           | CODING_CATEGORY_MASK_BINARY);
 
   return mask;
 }
@@ -2618,6 +2703,8 @@ detect_coding (coding, src, src_bytes)
    is encoded.  Return one of CODING_EOL_LF, CODING_EOL_CRLF,
    CODING_EOL_CR, and CODING_EOL_UNDECIDED.  */
 
+#define MAX_EOL_CHECK_COUNT 3
+
 int
 detect_eol_type (src, src_bytes)
      unsigned char *src;
@@ -2625,21 +2712,34 @@ detect_eol_type (src, src_bytes)
 {
   unsigned char *src_end = src + src_bytes;
   unsigned char c;
+  int total = 0;               /* How many end-of-lines are found so far.  */
+  int eol_type = CODING_EOL_UNDECIDED;
+  int this_eol_type;
 
-  while (src < src_end)
+  while (src < src_end && total < MAX_EOL_CHECK_COUNT)
     {
       c = *src++;
-      if (c == '\n')
-       return CODING_EOL_LF;
-      else if (c == '\r')
+      if (c == '\n' || c == '\r')
        {
-         if (src < src_end && *src == '\n')
-           return CODING_EOL_CRLF;
+         total++;
+         if (c == '\n')
+           this_eol_type = CODING_EOL_LF;
+         else if (src >= src_end || *src != '\n')
+           this_eol_type = CODING_EOL_CR;
          else
-           return CODING_EOL_CR;
+           this_eol_type = CODING_EOL_CRLF, src++;
+
+         if (eol_type == CODING_EOL_UNDECIDED)
+           /* This is the first end-of-line.  */
+           eol_type = this_eol_type;
+         else if (eol_type != this_eol_type)
+           /* The found type is different from what found before.
+              We had better not decode end-of-line.  */
+           return CODING_EOL_LF;
        }
     }
-  return CODING_EOL_UNDECIDED;
+
+  return eol_type;
 }
 
 /* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC
@@ -2745,7 +2845,6 @@ encode_coding (coding, source, destination, src_bytes, dst_bytes, consumed)
 {
   int produced;
 
-  coding->carryover_size = 0;
   switch (coding->type)
     {
     case coding_type_no_conversion:
@@ -2938,10 +3037,10 @@ The value of property should be a vector of length 5.")
 
 DEFUN ("detect-coding-region", Fdetect_coding_region, Sdetect_coding_region,
        2, 2, 0,
-  "Detect coding-system of the text in the region between START and END.\n\
-Return a list of possible coding-systems ordered by priority.\n\
+  "Detect coding system of the text in the region between START and END.\n\
+Return a list of possible coding systems ordered by priority.\n\
 If only ASCII characters are found, it returns `undecided'\n\
- or its subsidiary coding-system according to a detected end-of-line format.")
+ or its subsidiary coding system according to a detected end-of-line format.")
   (b, e)
      Lisp_Object b, e;
 {
@@ -3553,8 +3652,8 @@ which is a list of all the arguments given to this function.")
            return Qnil;
          if (! NILP (Fcoding_system_p (val)))
            return Fcons (val, val);
-         if (!NILP (Fboundp (val)))
-           return call2 (val, Flist (nargs, args));
+         if (!NILP (Ffboundp (val)))
+           return call1 (val, Flist (nargs, args));
          return Qnil;
        }
     }
@@ -3742,19 +3841,25 @@ syms_of_coding ()
   }
 
   DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read,
-    "A variable of internal use only.\n\
+    "Specify the coding system for read operations.\n\
+It is useful to bind this variable with `let', but do not set it globally.
 If the value is a coding system, it is used for decoding on read operation.\n\
-If not, an appropriate element in `coding-system-alist' (which see) is used.");
+If not, an appropriate element 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'.");
   Vcoding_system_for_read = Qnil;
 
   DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write,
-    "A variable of internal use only.\n\
+    "Specify the coding system for write operations.\n\
+It is useful to bind this variable with `let', but do not set it globally.
 If the value is a coding system, it is used for encoding on write operation.\n\
-If not, an appropriate element in `coding-system-alist' (which see) is used.");
+If not, an appropriate element 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'.");
   Vcoding_system_for_write = Qnil;
 
   DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used,
-    "Coding-system used in the latest file or process I/O.");
+    "Coding system used in the latest file or process I/O.");
   Vlast_coding_system_used = Qnil;
 
   DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion,