1 /* Coding system handler (conversion, detection, and etc).
3 Copyright (C) 1995 Free Software Foundation, Inc.
4 Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /*** TABLE OF CONTENTS ***
26 2. Emacs' internal format handlers
28 4. Shift-JIS and BIG5 handlers
29 5. End-of-line handlers
30 6. C library functions
31 7. Emacs Lisp library functions
36 /*** GENERAL NOTE on CODING SYSTEM ***
38 Coding system is an encoding mechanism of one or more character
39 sets. Here's a list of coding systems which Emacs can handle. When
40 we say "decode", it means converting some other coding system to
41 Emacs' internal format, and when we say "encode", it means
42 converting Emacs' internal format to some other coding system.
44 0. Emacs' internal format
46 Emacs itself holds a multi-lingual character in a buffer and a string
47 in a special format. Details are described in the section 2.
51 The most famous coding system for multiple character sets. X's
52 Compound Text, various EUCs (Extended Unix Code), and such coding
53 systems used in Internet communication as ISO-2022-JP are all
54 variants of ISO2022. Details are described in the section 3.
56 2. SJIS (or Shift-JIS or MS-Kanji-Code)
58 A coding system to encode character sets: ASCII, JISX0201, and
59 JISX0208. Widely used for PC's in Japan. Details are described in
64 A coding system to encode character sets: ASCII and Big5. Widely
65 used by Chinese (mainly in Taiwan and Hong Kong). Details are
66 described in the section 4. In this file, when written as "BIG5"
67 (all uppercase), it means the coding system, and when written as
68 "Big5" (capitalized), it means the character set.
72 If a user want to read/write a text encoded in a coding system not
73 listed above, he can supply a decoder and an encoder for it in CCL
74 (Code Conversion Language) programs. Emacs executes the CCL program
75 while reading/writing.
77 Emacs represent a coding-system by a Lisp symbol that has a property
78 `coding-system'. But, before actually using the coding-system, the
79 information about it is set in a structure of type `struct
80 coding_system' for rapid processing. See the section 6 for more
85 /*** GENERAL NOTES on END-OF-LINE FORMAT ***
87 How end-of-line of a text is encoded depends on a system. For
88 instance, Unix's format is just one byte of `line-feed' code,
89 whereas DOS's format is two bytes sequence of `carriage-return' and
90 `line-feed' codes. MacOS's format is one byte of `carriage-return'.
92 Since how characters in a text is encoded and how end-of-line is
93 encoded is independent, any coding system described above can take
94 any format of end-of-line. So, Emacs has information of format of
95 end-of-line in each coding-system. See the section 6 for more
100 /*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
102 These functions check if a text between SRC and SRC_END is encoded
103 in the coding system category XXX. Each returns an integer value in
104 which appropriate flag bits for the category XXX is set. The flag
105 bits are defined in macros CODING_CATEGORY_MASK_XXX. Below is the
106 template of these functions. */
109 detect_coding_internal (src
, src_end
)
110 unsigned char *src
, *src_end
;
116 /*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
118 These functions decode SRC_BYTES length text at SOURCE encoded in
119 CODING to Emacs' internal format. The resulting text goes to a
120 place pointed by DESTINATION, the length of which should not exceed
121 DST_BYTES. The bytes actually processed is returned as *CONSUMED.
122 The return value is the length of the decoded text. Below is a
123 template of these functions. */
125 decode_coding_XXX (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
126 struct coding_system
*coding
;
127 unsigned char *source
, *destination
;
128 int src_bytes
, dst_bytes
;
135 /*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
137 These functions encode SRC_BYTES length text at SOURCE of Emacs
138 internal format to CODING. The resulting text goes to a place
139 pointed by DESTINATION, the length of which should not exceed
140 DST_BYTES. The bytes actually processed is returned as *CONSUMED.
141 The return value is the length of the encoded text. Below is a
142 template of these functions. */
144 encode_coding_XXX (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
145 struct coding_system
*coding
;
146 unsigned char *source
, *destination
;
147 int src_bytes
, dst_bytes
;
154 /*** COMMONLY USED MACROS ***/
156 /* The following three macros ONE_MORE_BYTE, TWO_MORE_BYTES, and
157 THREE_MORE_BYTES safely get one, two, and three bytes from the
158 source text respectively. If there are not enough bytes in the
159 source, they jump to `label_end_of_loop'. The caller should set
160 variables `src' and `src_end' to appropriate areas in advance. */
162 #define ONE_MORE_BYTE(c1) \
167 goto label_end_of_loop; \
170 #define TWO_MORE_BYTES(c1, c2) \
172 if (src + 1 < src_end) \
173 c1 = *src++, c2 = *src++; \
175 goto label_end_of_loop; \
178 #define THREE_MORE_BYTES(c1, c2, c3) \
180 if (src + 2 < src_end) \
181 c1 = *src++, c2 = *src++, c3 = *src++; \
183 goto label_end_of_loop; \
186 /* The following three macros DECODE_CHARACTER_ASCII,
187 DECODE_CHARACTER_DIMENSION1, and DECODE_CHARACTER_DIMENSION2 put
188 the multi-byte form of a character of each class at the place
189 pointed by `dst'. The caller should set the variable `dst' to
190 point to an appropriate area and the variable `coding' to point to
191 the coding-system of the currently decoding text in advance. */
193 /* Decode one ASCII character C. */
195 #define DECODE_CHARACTER_ASCII(c) \
197 if (COMPOSING_P (coding->composing)) \
198 *dst++ = 0xA0, *dst++ = (c) | 0x80; \
203 /* Decode one DIMENSION1 character of which charset is CHARSET and
204 position-code is C. */
206 #define DECODE_CHARACTER_DIMENSION1(charset, c) \
208 unsigned char leading_code = CHARSET_LEADING_CODE_BASE (charset); \
209 if (COMPOSING_P (coding->composing)) \
210 *dst++ = leading_code + 0x20; \
212 *dst++ = leading_code; \
213 if (leading_code = CHARSET_LEADING_CODE_EXT (charset)) \
214 *dst++ = leading_code; \
215 *dst++ = (c) | 0x80; \
218 /* Decode one DIMENSION2 character of which charset is CHARSET and
219 position-codes are C1 and C2. */
221 #define DECODE_CHARACTER_DIMENSION2(charset, c1, c2) \
223 DECODE_CHARACTER_DIMENSION1 (charset, c1); \
224 *dst++ = (c2) | 0x80; \
228 /*** 1. Preamble ***/
242 #else /* not emacs */
246 #endif /* not emacs */
248 Lisp_Object Qcoding_system
, Qeol_type
;
249 Lisp_Object Qbuffer_file_coding_system
;
250 Lisp_Object Qpost_read_conversion
, Qpre_write_conversion
;
252 extern Lisp_Object Qinsert_file_contents
, Qwrite_region
;
253 Lisp_Object Qcall_process
, Qcall_process_region
, Qprocess_argument
;
254 Lisp_Object Qstart_process
, Qopen_network_stream
;
255 Lisp_Object Qtarget_idx
;
257 /* Mnemonic character of each format of end-of-line. */
258 int eol_mnemonic_unix
, eol_mnemonic_dos
, eol_mnemonic_mac
;
259 /* Mnemonic character to indicate format of end-of-line is not yet
261 int eol_mnemonic_undecided
;
265 Lisp_Object Qcoding_system_vector
, Qcoding_system_p
, Qcoding_system_error
;
267 /* Coding-systems are handed between Emacs Lisp programs and C internal
268 routines by the following three variables. */
269 /* Coding-system for reading files and receiving data from process. */
270 Lisp_Object Vcoding_system_for_read
;
271 /* Coding-system for writing files and sending data to process. */
272 Lisp_Object Vcoding_system_for_write
;
273 /* Coding-system actually used in the latest I/O. */
274 Lisp_Object Vlast_coding_system_used
;
276 /* Coding-system of what terminal accept for displaying. */
277 struct coding_system terminal_coding
;
279 /* Coding-system of what is sent from terminal keyboard. */
280 struct coding_system keyboard_coding
;
282 Lisp_Object Vcoding_system_alist
;
286 Lisp_Object Qcoding_category_index
;
288 /* List of symbols `coding-category-xxx' ordered by priority. */
289 Lisp_Object Vcoding_category_list
;
291 /* Table of coding-systems currently assigned to each coding-category. */
292 Lisp_Object coding_category_table
[CODING_CATEGORY_IDX_MAX
];
294 /* Table of names of symbol for each coding-category. */
295 char *coding_category_name
[CODING_CATEGORY_IDX_MAX
] = {
296 "coding-category-internal",
297 "coding-category-sjis",
298 "coding-category-iso-7",
299 "coding-category-iso-8-1",
300 "coding-category-iso-8-2",
301 "coding-category-iso-else",
302 "coding-category-big5",
303 "coding-category-binary"
306 /* Alist of charsets vs the alternate charsets. */
307 Lisp_Object Valternate_charset_table
;
309 /* Alist of charsets vs revision number. */
310 Lisp_Object Vcharset_revision_alist
;
313 /*** 2. Emacs internal format handlers ***/
315 /* Emacs' internal format for encoding multiple character sets is a
316 kind of multi-byte encoding, i.e. encoding a character by a sequence
317 of one-byte codes of variable length. ASCII characters and control
318 characters (e.g. `tab', `newline') are represented by one-byte as
319 is. It takes the range 0x00 through 0x7F. The other characters
320 are represented by a sequence of `base leading-code', optional
321 `extended leading-code', and one or two `position-code's. Length
322 of the sequence is decided by the base leading-code. Leading-code
323 takes the range 0x80 through 0x9F, whereas extended leading-code
324 and position-code take the range 0xA0 through 0xFF. See the
325 document of `charset.h' for more detail about leading-code and
328 There's one exception in this rule. Special leading-code
329 `leading-code-composition' denotes that the following several
330 characters should be composed into one character. Leading-codes of
331 components (except for ASCII) are added 0x20. An ASCII character
332 component is represented by a 2-byte sequence of `0xA0' and
333 `ASCII-code + 0x80'. See also the document in `charset.h' for the
334 detail of composite character. Hence, we can summarize the code
337 --- CODE RANGE of Emacs' internal format ---
338 (character set) (range)
340 ELSE (1st byte) 0x80 .. 0x9F
341 (rest bytes) 0xA0 .. 0xFF
342 ---------------------------------------------
346 enum emacs_code_class_type emacs_code_class
[256];
348 /* Go to the next statement only if *SRC is accessible and the code is
349 greater than 0xA0. */
350 #define CHECK_CODE_RANGE_A0_FF \
352 if (src >= src_end) \
353 goto label_end_of_switch; \
354 else if (*src++ < 0xA0) \
358 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
359 Check if a text is encoded in Emacs' internal format. If it is,
360 return CODING_CATEGORY_MASK_INTERNAL, else return 0. */
363 detect_coding_internal (src
, src_end
)
364 unsigned char *src
, *src_end
;
369 while (src
< src_end
)
381 switch (emacs_code_class
[c
])
383 case EMACS_ascii_code
:
384 case EMACS_linefeed_code
:
387 case EMACS_control_code
:
388 if (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
392 case EMACS_invalid_code
:
395 case EMACS_leading_code_composition
: /* c == 0x80 */
397 CHECK_CODE_RANGE_A0_FF
;
402 case EMACS_leading_code_4
:
403 CHECK_CODE_RANGE_A0_FF
;
404 /* fall down to check it two more times ... */
406 case EMACS_leading_code_3
:
407 CHECK_CODE_RANGE_A0_FF
;
408 /* fall down to check it one more time ... */
410 case EMACS_leading_code_2
:
411 CHECK_CODE_RANGE_A0_FF
;
419 return CODING_CATEGORY_MASK_INTERNAL
;
423 /*** 3. ISO2022 handlers ***/
425 /* The following note describes the coding system ISO2022 briefly.
426 Since the intension of this note is to help understanding of the
427 programs in this file, some parts are NOT ACCURATE or OVERLY
428 SIMPLIFIED. For the thorough understanding, please refer to the
429 original document of ISO2022.
431 ISO2022 provides many mechanisms to encode several character sets
432 in 7-bit and 8-bit environment. If one choose 7-bite environment,
433 all text is encoded by codes of less than 128. This may make the
434 encoded text a little bit longer, but the text get more stability
435 to pass through several gateways (some of them split MSB off).
437 There are two kind of character set: control character set and
438 graphic character set. The former contains control characters such
439 as `newline' and `escape' to provide control functions (control
440 functions are provided also by escape sequence). The latter
441 contains graphic characters such as ' A' and '-'. Emacs recognizes
442 two control character sets and many graphic character sets.
444 Graphic character sets are classified into one of the following
445 four classes, DIMENSION1_CHARS94, DIMENSION1_CHARS96,
446 DIMENSION2_CHARS94, DIMENSION2_CHARS96 according to the number of
447 bytes (DIMENSION) and the number of characters in one dimension
448 (CHARS) of the set. In addition, each character set is assigned an
449 identification tag (called "final character" and denoted as <F>
450 here after) which is unique in each class. <F> of each character
451 set is decided by ECMA(*) when it is registered in ISO. Code range
452 of <F> is 0x30..0x7F (0x30..0x3F are for private use only).
454 Note (*): ECMA = European Computer Manufacturers Association
456 Here are examples of graphic character set [NAME(<F>)]:
457 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
458 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
459 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
460 o DIMENSION2_CHARS96 -- none for the moment
462 A code area (1byte=8bits) is divided into 4 areas, C0, GL, C1, and GR.
463 C0 [0x00..0x1F] -- control character plane 0
464 GL [0x20..0x7F] -- graphic character plane 0
465 C1 [0x80..0x9F] -- control character plane 1
466 GR [0xA0..0xFF] -- graphic character plane 1
468 A control character set is directly designated and invoked to C0 or
469 C1 by an escape sequence. The most common case is that ISO646's
470 control character set is designated/invoked to C0 and ISO6429's
471 control character set is designated/invoked to C1, and usually
472 these designations/invocations are omitted in a coded text. With
473 7-bit environment, only C0 can be used, and a control character for
474 C1 is encoded by an appropriate escape sequence to fit in the
475 environment. All control characters for C1 are defined the
476 corresponding escape sequences.
478 A graphic character set is at first designated to one of four
479 graphic registers (G0 through G3), then these graphic registers are
480 invoked to GL or GR. These designations and invocations can be
481 done independently. The most common case is that G0 is invoked to
482 GL, G1 is invoked to GR, and ASCII is designated to G0, and usually
483 these invocations and designations are omitted in a coded text.
484 With 7-bit environment, only GL can be used.
486 When a graphic character set of CHARS94 is invoked to GL, code 0x20
487 and 0x7F of GL area work as control characters SPACE and DEL
488 respectively, and code 0xA0 and 0xFF of GR area should not be used.
490 There are two ways of invocation: locking-shift and single-shift.
491 With locking-shift, the invocation lasts until the next different
492 invocation, whereas with single-shift, the invocation works only
493 for the following character and doesn't affect locking-shift.
494 Invocations are done by the following control characters or escape
497 ----------------------------------------------------------------------
498 function control char escape sequence description
499 ----------------------------------------------------------------------
500 SI (shift-in) 0x0F none invoke G0 to GL
501 SI (shift-out) 0x0E none invoke G1 to GL
502 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
503 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
504 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 into GL
505 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 into GL
506 ----------------------------------------------------------------------
507 The first four are for locking-shift. Control characters for these
508 functions are defined by macros ISO_CODE_XXX in `coding.h'.
510 Designations are done by the following escape sequences.
511 ----------------------------------------------------------------------
512 escape sequence description
513 ----------------------------------------------------------------------
514 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
515 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
516 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
517 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
518 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
519 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
520 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
521 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
522 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
523 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
524 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
525 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
526 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
527 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
528 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
529 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
530 ----------------------------------------------------------------------
532 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
533 of dimension 1, chars 94, and final character <F>, and etc.
535 Note (*): Although these designations are not allowed in ISO2022,
536 Emacs accepts them on decoding, and produces them on encoding
537 CHARS96 character set in a coding system which is characterized as
538 7-bit environment, non-locking-shift, and non-single-shift.
540 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
541 '(' can be omitted. We call this as "short-form" here after.
543 Now you may notice that there are a lot of ways for encoding the
544 same multilingual text in ISO2022. Actually, there exist many
545 coding systems such as Compound Text (used in X's inter client
546 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
547 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
548 localized platforms), and all of these are variants of ISO2022.
550 In addition to the above, Emacs handles two more kinds of escape
551 sequences: ISO6429's direction specification and Emacs' private
552 sequence for specifying character composition.
554 ISO6429's direction specification takes the following format:
555 o CSI ']' -- end of the current direction
556 o CSI '0' ']' -- end of the current direction
557 o CSI '1' ']' -- start of left-to-right text
558 o CSI '2' ']' -- start of right-to-left text
559 The control character CSI (0x9B: control sequence introducer) is
560 abbreviated to the escape sequence ESC '[' in 7-bit environment.
562 Character composition specification takes the following format:
563 o ESC '0' -- start character composition
564 o ESC '1' -- end character composition
565 Since these are not standard escape sequences of any ISO, the use
566 of them for these meaning is restricted to Emacs only. */
568 enum iso_code_class_type iso_code_class
[256];
570 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
571 Check if a text is encoded in ISO2022. If it is, returns an
572 integer in which appropriate flag bits any of:
573 CODING_CATEGORY_MASK_ISO_7
574 CODING_CATEGORY_MASK_ISO_8_1
575 CODING_CATEGORY_MASK_ISO_8_2
576 CODING_CATEGORY_MASK_ISO_ELSE
577 are set. If a code which should never appear in ISO2022 is found,
581 detect_coding_iso2022 (src
, src_end
)
582 unsigned char *src
, *src_end
;
584 int mask
= CODING_CATEGORY_MASK_ANY
;
585 int g1
= 0; /* 1 iff designating to G1. */
588 while (src
< src_end
)
598 && ((c
>= '(' && c
<= '/')
599 || c
== '$' && ((*src
>= '(' && *src
<= '/')
600 || (*src
>= '@' && *src
<= 'B'))))
602 /* Valid designation sequence. */
603 mask
&= (CODING_CATEGORY_MASK_ISO_7
604 | CODING_CATEGORY_MASK_ISO_8_1
605 | CODING_CATEGORY_MASK_ISO_8_2
606 | CODING_CATEGORY_MASK_ISO_ELSE
);
607 if (c
== ')' || (c
== '$' && *src
== ')'))
610 mask
&= ~CODING_CATEGORY_MASK_ISO_7
;
615 else if (c
== 'N' || c
== 'O' || c
== 'n' || c
== 'o')
616 return CODING_CATEGORY_MASK_ISO_ELSE
;
621 return CODING_CATEGORY_MASK_ISO_ELSE
;
627 mask
&= ~CODING_CATEGORY_MASK_ISO_7
;
639 mask
&= ~CODING_CATEGORY_MASK_ISO_7
;
640 while (src
< src_end
&& *src
>= 0xA0)
642 if (count
& 1 && src
< src_end
)
643 mask
&= ~CODING_CATEGORY_MASK_ISO_8_2
;
652 /* Decode a character of which charset is CHARSET and the 1st position
653 code is C1. If dimension of CHARSET 2, the 2nd position code is
654 fetched from SRC and set to C2. If CHARSET is negative, it means
655 that we are decoding ill formed text, and what we can do is just to
658 #define DECODE_ISO_CHARACTER(charset, c1) \
660 if ((charset) >= 0 && CHARSET_DIMENSION (charset) == 2) \
661 ONE_MORE_BYTE (c2); \
662 if (COMPOSING_HEAD_P (coding->composing)) \
664 *dst++ = LEADING_CODE_COMPOSITION; \
665 if (COMPOSING_WITH_RULE_P (coding->composing)) \
666 /* To tell composition rules are embeded. */ \
668 coding->composing += 2; \
672 else if ((charset) == CHARSET_ASCII) \
673 DECODE_CHARACTER_ASCII (c1); \
674 else if (CHARSET_DIMENSION (charset) == 1) \
675 DECODE_CHARACTER_DIMENSION1 (charset, c1); \
677 DECODE_CHARACTER_DIMENSION2 (charset, c1, c2); \
678 if (COMPOSING_WITH_RULE_P (coding->composing)) \
679 /* To tell a composition rule follows. */ \
680 coding->composing = COMPOSING_WITH_RULE_RULE; \
683 /* Set designation state into CODING. */
684 #define DECODE_DESIGNATION(reg, dimension, chars, final_char) \
686 int charset = ISO_CHARSET_TABLE (dimension, chars, final_char); \
688 = Fassq (CHARSET_SYMBOL (charset), Valternate_charset_table); \
690 charset = get_charset_id (XCONS (temp)->cdr); \
693 if (coding->direction == 1 \
694 && CHARSET_REVERSE_CHARSET (charset) >= 0) \
695 charset = CHARSET_REVERSE_CHARSET (charset); \
696 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
700 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
703 decode_coding_iso2022 (coding
, source
, destination
,
704 src_bytes
, dst_bytes
, consumed
)
705 struct coding_system
*coding
;
706 unsigned char *source
, *destination
;
707 int src_bytes
, dst_bytes
;
710 unsigned char *src
= source
;
711 unsigned char *src_end
= source
+ src_bytes
;
712 unsigned char *dst
= destination
;
713 unsigned char *dst_end
= destination
+ dst_bytes
;
714 /* Since the maximum bytes produced by each loop is 7, we subtract 6
715 from DST_END to assure that overflow checking is necessary only
716 at the head of loop. */
717 unsigned char *adjusted_dst_end
= dst_end
- 6;
719 /* Charsets invoked to graphic plane 0 and 1 respectively. */
720 int charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
721 int charset1
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 1);
723 while (src
< src_end
&& dst
< adjusted_dst_end
)
725 /* SRC_BASE remembers the start position in source in each loop.
726 The loop will be exited when there's not enough source text
727 to analyze long escape sequence or 2-byte code (within macros
728 ONE_MORE_BYTE or TWO_MORE_BYTES). In that case, SRC is reset
729 to SRC_BASE before exiting. */
730 unsigned char *src_base
= src
;
731 unsigned char c1
= *src
++, c2
, cmprule
;
733 switch (iso_code_class
[c1
])
735 case ISO_0x20_or_0x7F
:
736 if (!coding
->composing
737 && (charset0
< 0 || CHARSET_CHARS (charset0
) == 94))
739 /* This is SPACE or DEL. */
743 /* This is a graphic character, we fall down ... */
745 case ISO_graphic_plane_0
:
746 if (coding
->composing
== COMPOSING_WITH_RULE_RULE
)
748 /* This is a composition rule. */
750 coding
->composing
= COMPOSING_WITH_RULE_TAIL
;
753 DECODE_ISO_CHARACTER (charset0
, c1
);
756 case ISO_0xA0_or_0xFF
:
757 if (charset1
< 0 || CHARSET_CHARS (charset1
) == 94)
763 /* This is a graphic character, we fall down ... */
765 case ISO_graphic_plane_1
:
766 DECODE_ISO_CHARACTER (charset1
, c1
);
769 case ISO_control_code
:
770 /* All ISO2022 control characters in this class have the
771 same representation in Emacs internal format. */
775 case ISO_carriage_return
:
776 if (coding
->eol_type
== CODING_EOL_CR
)
780 else if (coding
->eol_type
== CODING_EOL_CRLF
)
783 if (c1
== ISO_CODE_LF
)
798 if (CODING_SPEC_ISO_DESIGNATION (coding
, 1) < 0)
799 goto label_invalid_escape_sequence
;
800 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 1;
801 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
805 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 0;
806 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
809 case ISO_single_shift_2_7
:
810 case ISO_single_shift_2
:
811 /* SS2 is handled as an escape sequence of ESC 'N' */
813 goto label_escape_sequence
;
815 case ISO_single_shift_3
:
816 /* SS2 is handled as an escape sequence of ESC 'O' */
818 goto label_escape_sequence
;
820 case ISO_control_sequence_introducer
:
821 /* CSI is handled as an escape sequence of ESC '[' ... */
823 goto label_escape_sequence
;
827 label_escape_sequence
:
828 /* Escape sequences handled by Emacs are invocation,
829 designation, direction specification, and character
830 composition specification. */
833 case '&': /* revision of following character set */
835 if (!(c1
>= '@' && c1
<= '~'))
836 goto label_invalid_escape_sequence
;
838 if (c1
!= ISO_CODE_ESC
)
839 goto label_invalid_escape_sequence
;
841 goto label_escape_sequence
;
843 case '$': /* designation of 2-byte character set */
845 if (c1
>= '@' && c1
<= 'B')
846 { /* designation of JISX0208.1978, GB2312.1980,
848 DECODE_DESIGNATION (0, 2, 94, c1
);
850 else if (c1
>= 0x28 && c1
<= 0x2B)
851 { /* designation of DIMENSION2_CHARS94 character set */
853 DECODE_DESIGNATION (c1
- 0x28, 2, 94, c2
);
855 else if (c1
>= 0x2C && c1
<= 0x2F)
856 { /* designation of DIMENSION2_CHARS96 character set */
858 DECODE_DESIGNATION (c1
- 0x2C, 2, 96, c2
);
861 goto label_invalid_escape_sequence
;
864 case 'n': /* invocation of locking-shift-2 */
865 if (CODING_SPEC_ISO_DESIGNATION (coding
, 2) < 0)
866 goto label_invalid_escape_sequence
;
867 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 2;
868 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
871 case 'o': /* invocation of locking-shift-3 */
872 if (CODING_SPEC_ISO_DESIGNATION (coding
, 3) < 0)
873 goto label_invalid_escape_sequence
;
874 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 3;
875 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
878 case 'N': /* invocation of single-shift-2 */
879 if (CODING_SPEC_ISO_DESIGNATION (coding
, 2) < 0)
880 goto label_invalid_escape_sequence
;
882 charset
= CODING_SPEC_ISO_DESIGNATION (coding
, 2);
883 DECODE_ISO_CHARACTER (charset
, c1
);
886 case 'O': /* invocation of single-shift-3 */
887 if (CODING_SPEC_ISO_DESIGNATION (coding
, 3) < 0)
888 goto label_invalid_escape_sequence
;
890 charset
= CODING_SPEC_ISO_DESIGNATION (coding
, 3);
891 DECODE_ISO_CHARACTER (charset
, c1
);
894 case '0': /* start composing without embeded rules */
895 coding
->composing
= COMPOSING_NO_RULE_HEAD
;
898 case '1': /* end composing */
899 coding
->composing
= COMPOSING_NO
;
902 case '2': /* start composing with embeded rules */
903 coding
->composing
= COMPOSING_WITH_RULE_HEAD
;
906 case '[': /* specification of direction */
907 /* For the moment, nested direction is not supported.
908 So, the value of `coding->direction' is 0 or 1: 0
909 means left-to-right, 1 means right-to-left. */
913 case ']': /* end of the current direction */
914 coding
->direction
= 0;
916 case '0': /* end of the current direction */
917 case '1': /* start of left-to-right direction */
920 coding
->direction
= 0;
922 goto label_invalid_escape_sequence
;
925 case '2': /* start of right-to-left direction */
928 coding
->direction
= 1;
930 goto label_invalid_escape_sequence
;
934 goto label_invalid_escape_sequence
;
939 if (c1
>= 0x28 && c1
<= 0x2B)
940 { /* designation of DIMENSION1_CHARS94 character set */
942 DECODE_DESIGNATION (c1
- 0x28, 1, 94, c2
);
944 else if (c1
>= 0x2C && c1
<= 0x2F)
945 { /* designation of DIMENSION1_CHARS96 character set */
947 DECODE_DESIGNATION (c1
- 0x2C, 1, 96, c2
);
951 goto label_invalid_escape_sequence
;
954 /* We must update these variables now. */
955 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
956 charset1
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 1);
959 label_invalid_escape_sequence
:
961 int length
= src
- src_base
;
963 bcopy (src_base
, dst
, length
);
970 coding
->carryover_size
= src
- src_base
;
971 bcopy (src_base
, coding
->carryover
, coding
->carryover_size
);
976 /* If this is the last block of the text to be decoded, we had
977 better just flush out all remaining codes in the text although
978 they are not valid characters. */
979 if (coding
->last_block
)
981 bcopy (src
, dst
, src_end
- src
);
982 dst
+= (src_end
- src
);
985 *consumed
= src
- source
;
986 return dst
- destination
;
989 /* ISO2022 encoding staffs. */
992 It is not enough to say just "ISO2022" on encoding, but we have to
993 specify more details. In Emacs, each coding-system of ISO2022
994 variant has the following specifications:
995 1. Initial designation to G0 thru G3.
996 2. Allows short-form designation?
997 3. ASCII should be designated to G0 before control characters?
998 4. ASCII should be designated to G0 at end of line?
999 5. 7-bit environment or 8-bit environment?
1000 6. Use locking-shift?
1001 7. Use Single-shift?
1002 And the following two are only for Japanese:
1003 8. Use ASCII in place of JIS0201-1976-Roman?
1004 9. Use JISX0208-1983 in place of JISX0208-1978?
1005 These specifications are encoded in `coding->flags' as flag bits
1006 defined by macros CODING_FLAG_ISO_XXX. See `coding.h' for more
1010 /* Produce codes (escape sequence) for designating CHARSET to graphic
1011 register REG. If <final-char> of CHARSET is '@', 'A', or 'B' and
1012 the coding system CODING allows, produce designation sequence of
1015 #define ENCODE_DESIGNATION(charset, reg, coding) \
1017 unsigned char final_char = CHARSET_ISO_FINAL_CHAR (charset); \
1018 char *intermediate_char_94 = "()*+"; \
1019 char *intermediate_char_96 = ",-./"; \
1021 = Fassq (make_number (charset), Vcharset_revision_alist); \
1022 if (! NILP (temp)) \
1024 *dst++ = ISO_CODE_ESC; \
1026 *dst++ = XINT (XCONS (temp)->cdr) + '@'; \
1028 *dst++ = ISO_CODE_ESC; \
1029 if (CHARSET_DIMENSION (charset) == 1) \
1031 if (CHARSET_CHARS (charset) == 94) \
1032 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
1034 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
1039 if (CHARSET_CHARS (charset) == 94) \
1041 if (! (coding->flags & CODING_FLAG_ISO_SHORT_FORM) \
1043 || final_char < '@' || final_char > 'B') \
1044 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
1047 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
1049 *dst++ = final_char; \
1050 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
1053 /* The following two macros produce codes (control character or escape
1054 sequence) for ISO2022 single-shift functions (single-shift-2 and
1057 #define ENCODE_SINGLE_SHIFT_2 \
1059 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1060 *dst++ = ISO_CODE_ESC, *dst++ = 'N'; \
1062 *dst++ = ISO_CODE_SS2; \
1063 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
1066 #define ENCODE_SINGLE_SHIFT_3 \
1068 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1069 *dst++ = ISO_CODE_ESC, *dst++ = 'O'; \
1071 *dst++ = ISO_CODE_SS3; \
1072 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
1075 /* The following four macros produce codes (control character or
1076 escape sequence) for ISO2022 locking-shift functions (shift-in,
1077 shift-out, locking-shift-2, and locking-shift-3). */
1079 #define ENCODE_SHIFT_IN \
1081 *dst++ = ISO_CODE_SI; \
1082 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0; \
1085 #define ENCODE_SHIFT_OUT \
1087 *dst++ = ISO_CODE_SO; \
1088 CODING_SPEC_ISO_INVOCATION (coding, 0) = 1; \
1091 #define ENCODE_LOCKING_SHIFT_2 \
1093 *dst++ = ISO_CODE_ESC, *dst++ = 'n'; \
1094 CODING_SPEC_ISO_INVOCATION (coding, 0) = 2; \
1097 #define ENCODE_LOCKING_SHIFT_3 \
1099 *dst++ = ISO_CODE_ESC, *dst++ = 'o'; \
1100 CODING_SPEC_ISO_INVOCATION (coding, 0) = 3; \
1103 /* Produce codes for a DIMENSION1 character of which character set is
1104 CHARSET and position-code is C1. Designation and invocation
1105 sequences are also produced in advance if necessary. */
1108 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
1110 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
1112 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1113 *dst++ = c1 & 0x7F; \
1115 *dst++ = c1 | 0x80; \
1116 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
1119 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
1121 *dst++ = c1 & 0x7F; \
1124 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
1126 *dst++ = c1 | 0x80; \
1130 /* Since CHARSET is not yet invoked to any graphic planes, we \
1131 must invoke it, or, at first, designate it to some graphic \
1132 register. Then repeat the loop to actually produce the \
1134 dst = encode_invocation_designation (charset, coding, dst); \
1137 /* Produce codes for a DIMENSION2 character of which character set is
1138 CHARSET and position-codes are C1 and C2. Designation and
1139 invocation codes are also produced in advance if necessary. */
1141 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
1143 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
1145 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1146 *dst++ = c1 & 0x7F, *dst++ = c2 & 0x7F; \
1148 *dst++ = c1 | 0x80, *dst++ = c2 | 0x80; \
1149 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
1152 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
1154 *dst++ = c1 & 0x7F, *dst++= c2 & 0x7F; \
1157 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
1159 *dst++ = c1 | 0x80, *dst++= c2 | 0x80; \
1163 /* Since CHARSET is not yet invoked to any graphic planes, we \
1164 must invoke it, or, at first, designate it to some graphic \
1165 register. Then repeat the loop to actually produce the \
1167 dst = encode_invocation_designation (charset, coding, dst); \
1170 /* Produce designation and invocation codes at a place pointed by DST
1171 to use CHARSET. The element `spec.iso2022' of *CODING is updated.
1175 encode_invocation_designation (charset
, coding
, dst
)
1177 struct coding_system
*coding
;
1180 int reg
; /* graphic register number */
1182 /* At first, check designations. */
1183 for (reg
= 0; reg
< 4; reg
++)
1184 if (charset
== CODING_SPEC_ISO_DESIGNATION (coding
, reg
))
1189 /* CHARSET is not yet designated to any graphic registers. */
1190 /* At first check the requested designation. */
1191 reg
= CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
);
1193 /* Since CHARSET requests no special designation, designate to
1194 graphic register 0. */
1197 ENCODE_DESIGNATION (charset
, reg
, coding
);
1200 if (CODING_SPEC_ISO_INVOCATION (coding
, 0) != reg
1201 && CODING_SPEC_ISO_INVOCATION (coding
, 1) != reg
)
1203 /* Since the graphic register REG is not invoked to any graphic
1204 planes, invoke it to graphic plane 0. */
1207 case 0: /* graphic register 0 */
1211 case 1: /* graphic register 1 */
1215 case 2: /* graphic register 2 */
1216 if (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
1217 ENCODE_SINGLE_SHIFT_2
;
1219 ENCODE_LOCKING_SHIFT_2
;
1222 case 3: /* graphic register 3 */
1223 if (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
1224 ENCODE_SINGLE_SHIFT_3
;
1226 ENCODE_LOCKING_SHIFT_3
;
1233 /* The following two macros produce codes for indicating composition. */
1234 #define ENCODE_COMPOSITION_NO_RULE_START *dst++ = ISO_CODE_ESC, *dst++ = '0'
1235 #define ENCODE_COMPOSITION_WITH_RULE_START *dst++ = ISO_CODE_ESC, *dst++ = '2'
1236 #define ENCODE_COMPOSITION_END *dst++ = ISO_CODE_ESC, *dst++ = '1'
1238 /* The following three macros produce codes for indicating direction
1240 #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
1242 if (coding->flags == CODING_FLAG_ISO_SEVEN_BITS) \
1243 *dst++ = ISO_CODE_ESC, *dst++ = '['; \
1245 *dst++ = ISO_CODE_CSI; \
1248 #define ENCODE_DIRECTION_R2L \
1249 ENCODE_CONTROL_SEQUENCE_INTRODUCER, *dst++ = '2', *dst++ = ']'
1251 #define ENCODE_DIRECTION_L2R \
1252 ENCODE_CONTROL_SEQUENCE_INTRODUCER, *dst++ = '0', *dst++ = ']'
1254 /* Produce codes for designation and invocation to reset the graphic
1255 planes and registers to initial state. */
1256 #define ENCODE_RESET_PLANE_AND_REGISTER \
1259 if (CODING_SPEC_ISO_INVOCATION (coding, 0) != 0) \
1261 for (reg = 0; reg < 4; reg++) \
1262 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg) >= 0 \
1263 && (CODING_SPEC_ISO_DESIGNATION (coding, reg) \
1264 != CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg))) \
1265 ENCODE_DESIGNATION \
1266 (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg), reg, coding); \
1270 encode_designation_at_bol (coding
, src
, src_end
, dstp
)
1271 struct coding_system
*coding
;
1272 unsigned char *src
, *src_end
, **dstp
;
1274 int charset
, reg
, r
[4];
1275 unsigned char *dst
= *dstp
, c
;
1276 for (reg
= 0; reg
< 4; reg
++) r
[reg
] = -1;
1277 while (src
< src_end
&& (c
= *src
++) != '\n')
1279 switch (emacs_code_class
[c
])
1281 case EMACS_ascii_code
:
1282 charset
= CHARSET_ASCII
;
1284 case EMACS_leading_code_2
:
1285 if (++src
>= src_end
) continue;
1288 case EMACS_leading_code_3
:
1289 if ((src
+= 2) >= src_end
) continue;
1290 charset
= (c
< LEADING_CODE_PRIVATE_11
? c
: *(src
- 2));
1292 case EMACS_leading_code_4
:
1293 if ((src
+= 3) >= src_end
) continue;
1294 charset
= *(src
- 3);
1299 reg
= CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
);
1301 && CODING_SPEC_ISO_DESIGNATION (coding
, reg
) != charset
)
1304 if (c
!= '\n' && !coding
->last_block
)
1306 for (reg
= 0; reg
< 4; reg
++)
1308 ENCODE_DESIGNATION (r
[reg
], reg
, coding
);
1313 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
1316 encode_coding_iso2022 (coding
, source
, destination
,
1317 src_bytes
, dst_bytes
, consumed
)
1318 struct coding_system
*coding
;
1319 unsigned char *source
, *destination
;
1320 int src_bytes
, dst_bytes
;
1323 unsigned char *src
= source
;
1324 unsigned char *src_end
= source
+ src_bytes
;
1325 unsigned char *dst
= destination
;
1326 unsigned char *dst_end
= destination
+ dst_bytes
;
1327 /* Since the maximum bytes produced by each loop is 20, we subtract 19
1328 from DST_END to assure overflow checking is necessary only at the
1330 unsigned char *adjusted_dst_end
= dst_end
- 19;
1332 while (src
< src_end
&& dst
< adjusted_dst_end
)
1334 /* SRC_BASE remembers the start position in source in each loop.
1335 The loop will be exited when there's not enough source text
1336 to analyze multi-byte codes (within macros ONE_MORE_BYTE,
1337 TWO_MORE_BYTES, and THREE_MORE_BYTES). In that case, SRC is
1338 reset to SRC_BASE before exiting. */
1339 unsigned char *src_base
= src
;
1340 unsigned char c1
, c2
, c3
, c4
;
1343 if (coding
->flags
& CODING_FLAG_ISO_DESIGNATE_AT_BOL
1344 && CODING_SPEC_ISO_BOL (coding
))
1346 /* We have to produce destination sequences now. */
1347 if (encode_designation_at_bol (coding
, src
, src_end
, &dst
) < 0)
1348 /* We can't find end of line in the current block. Let's
1349 repeat encoding starting from the current position
1352 CODING_SPEC_ISO_BOL (coding
) = 0;
1356 /* If we are seeing a component of a composite character, we are
1357 seeing a leading-code specially encoded for composition, or a
1358 composition rule if composing with rule. We must set C1
1359 to a normal leading-code or an ASCII code. If we are not at
1360 a composed character, we must reset the composition state. */
1361 if (COMPOSING_P (coding
->composing
))
1365 /* We are not in a composite character any longer. */
1366 coding
->composing
= COMPOSING_NO
;
1367 ENCODE_COMPOSITION_END
;
1371 if (coding
->composing
== COMPOSING_WITH_RULE_RULE
)
1374 coding
->composing
= COMPOSING_WITH_RULE_HEAD
;
1377 else if (coding
->composing
== COMPOSING_WITH_RULE_HEAD
)
1378 coding
->composing
= COMPOSING_WITH_RULE_RULE
;
1381 /* This is an ASCII component. */
1386 /* This is a leading-code of non ASCII component. */
1391 /* Now encode one character. C1 is a control character, an
1392 ASCII character, or a leading-code of multi-byte character. */
1393 switch (emacs_code_class
[c1
])
1395 case EMACS_ascii_code
:
1396 ENCODE_ISO_CHARACTER_DIMENSION1 (CHARSET_ASCII
, c1
);
1399 case EMACS_control_code
:
1400 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_CNTL
)
1401 ENCODE_RESET_PLANE_AND_REGISTER
;
1405 case EMACS_carriage_return_code
:
1406 if (!coding
->selective
)
1408 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_CNTL
)
1409 ENCODE_RESET_PLANE_AND_REGISTER
;
1413 /* fall down to treat '\r' as '\n' ... */
1415 case EMACS_linefeed_code
:
1416 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_EOL
)
1417 ENCODE_RESET_PLANE_AND_REGISTER
;
1418 if (coding
->flags
& CODING_FLAG_ISO_INIT_AT_BOL
)
1419 bcopy (coding
->spec
.iso2022
.initial_designation
,
1420 coding
->spec
.iso2022
.current_designation
,
1421 sizeof coding
->spec
.iso2022
.initial_designation
);
1422 if (coding
->eol_type
== CODING_EOL_LF
1423 || coding
->eol_type
== CODING_EOL_AUTOMATIC
)
1424 *dst
++ = ISO_CODE_LF
;
1425 else if (coding
->eol_type
== CODING_EOL_CRLF
)
1426 *dst
++ = ISO_CODE_CR
, *dst
++ = ISO_CODE_LF
;
1428 *dst
++ = ISO_CODE_CR
;
1429 CODING_SPEC_ISO_BOL (coding
) = 1;
1432 case EMACS_leading_code_2
:
1434 ENCODE_ISO_CHARACTER_DIMENSION1 (c1
, c2
);
1437 case EMACS_leading_code_3
:
1438 TWO_MORE_BYTES (c2
, c3
);
1439 if (c1
< LEADING_CODE_PRIVATE_11
)
1440 ENCODE_ISO_CHARACTER_DIMENSION2 (c1
, c2
, c3
);
1442 ENCODE_ISO_CHARACTER_DIMENSION1 (c2
, c3
);
1445 case EMACS_leading_code_4
:
1446 THREE_MORE_BYTES (c2
, c3
, c4
);
1447 ENCODE_ISO_CHARACTER_DIMENSION2 (c2
, c3
, c4
);
1450 case EMACS_leading_code_composition
:
1454 coding
->composing
= COMPOSING_WITH_RULE_HEAD
;
1455 ENCODE_COMPOSITION_WITH_RULE_START
;
1459 /* Rewind one byte because it is a character code of
1460 composition elements. */
1462 coding
->composing
= COMPOSING_NO_RULE_HEAD
;
1463 ENCODE_COMPOSITION_NO_RULE_START
;
1467 case EMACS_invalid_code
:
1473 coding
->carryover_size
= src
- src_base
;
1474 bcopy (src_base
, coding
->carryover
, coding
->carryover_size
);
1479 /* If this is the last block of the text to be encoded, we must
1480 reset the state of graphic planes and registers to initial one.
1481 In addition, we had better just flush out all remaining codes in
1482 the text although they are not valid characters. */
1483 if (coding
->last_block
)
1485 ENCODE_RESET_PLANE_AND_REGISTER
;
1486 bcopy(src
, dst
, src_end
- src
);
1487 dst
+= (src_end
- src
);
1490 *consumed
= src
- source
;
1491 return dst
- destination
;
1495 /*** 4. SJIS and BIG5 handlers ***/
1497 /* Although SJIS and BIG5 are not ISO's coding system, They are used
1498 quite widely. So, for the moment, Emacs supports them in the bare
1499 C code. But, in the future, they may be supported only by CCL. */
1501 /* SJIS is a coding system encoding three character sets: ASCII, right
1502 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
1503 as is. A character of charset katakana-jisx0201 is encoded by
1504 "position-code + 0x80". A character of charset japanese-jisx0208
1505 is encoded in 2-byte but two position-codes are divided and shifted
1506 so that it fit in the range below.
1508 --- CODE RANGE of SJIS ---
1509 (character set) (range)
1511 KATAKANA-JISX0201 0xA0 .. 0xDF
1512 JISX0208 (1st byte) 0x80 .. 0x9F and 0xE0 .. 0xFF
1513 (2nd byte) 0x40 .. 0xFF
1514 -------------------------------
1518 /* BIG5 is a coding system encoding two character sets: ASCII and
1519 Big5. An ASCII character is encoded as is. Big5 is a two-byte
1520 character set and is encoded in two-byte.
1522 --- CODE RANGE of BIG5 ---
1523 (character set) (range)
1525 Big5 (1st byte) 0xA1 .. 0xFE
1526 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
1527 --------------------------
1529 Since the number of characters in Big5 is larger than maximum
1530 characters in Emacs' charset (96x96), it can't be handled as one
1531 charset. So, in Emacs, Big5 is divided into two: `charset-big5-1'
1532 and `charset-big5-2'. Both are DIMENSION2 and CHARS94. The former
1533 contains frequently used characters and the latter contains less
1534 frequently used characters. */
1536 /* Macros to decode or encode a character of Big5 in BIG5. B1 and B2
1537 are the 1st and 2nd position-codes of Big5 in BIG5 coding system.
1538 C1 and C2 are the 1st and 2nd position-codes of of Emacs' internal
1539 format. CHARSET is `charset_big5_1' or `charset_big5_2'. */
1541 /* Number of Big5 characters which have the same code in 1st byte. */
1542 #define BIG5_SAME_ROW (0xFF - 0xA1 + 0x7F - 0x40)
1544 #define DECODE_BIG5(b1, b2, charset, c1, c2) \
1547 = (b1 - 0xA1) * BIG5_SAME_ROW + b2 - (b2 < 0x7F ? 0x40 : 0x62); \
1549 charset = charset_big5_1; \
1552 charset = charset_big5_2; \
1553 temp -= (0xC9 - 0xA1) * BIG5_SAME_ROW; \
1555 c1 = temp / (0xFF - 0xA1) + 0x21; \
1556 c2 = temp % (0xFF - 0xA1) + 0x21; \
1559 #define ENCODE_BIG5(charset, c1, c2, b1, b2) \
1561 unsigned int temp = (c1 - 0x21) * (0xFF - 0xA1) + (c2 - 0x21); \
1562 if (charset == charset_big5_2) \
1563 temp += BIG5_SAME_ROW * (0xC9 - 0xA1); \
1564 b1 = temp / BIG5_SAME_ROW + 0xA1; \
1565 b2 = temp % BIG5_SAME_ROW; \
1566 b2 += b2 < 0x3F ? 0x40 : 0x62; \
1569 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1570 Check if a text is encoded in SJIS. If it is, return
1571 CODING_CATEGORY_MASK_SJIS, else return 0. */
1574 detect_coding_sjis (src
, src_end
)
1575 unsigned char *src
, *src_end
;
1579 while (src
< src_end
)
1582 if (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
1584 if ((c
>= 0x80 && c
< 0xA0) || c
>= 0xE0)
1586 if (src
< src_end
&& *src
++ < 0x40)
1590 return CODING_CATEGORY_MASK_SJIS
;
1593 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1594 Check if a text is encoded in BIG5. If it is, return
1595 CODING_CATEGORY_MASK_BIG5, else return 0. */
1598 detect_coding_big5 (src
, src_end
)
1599 unsigned char *src
, *src_end
;
1603 while (src
< src_end
)
1606 if (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
1613 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
1617 return CODING_CATEGORY_MASK_BIG5
;
1620 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
1621 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
1624 decode_coding_sjis_big5 (coding
, source
, destination
,
1625 src_bytes
, dst_bytes
, consumed
, sjis_p
)
1626 struct coding_system
*coding
;
1627 unsigned char *source
, *destination
;
1628 int src_bytes
, dst_bytes
;
1632 unsigned char *src
= source
;
1633 unsigned char *src_end
= source
+ src_bytes
;
1634 unsigned char *dst
= destination
;
1635 unsigned char *dst_end
= destination
+ dst_bytes
;
1636 /* Since the maximum bytes produced by each loop is 4, we subtract 3
1637 from DST_END to assure overflow checking is necessary only at the
1639 unsigned char *adjusted_dst_end
= dst_end
- 3;
1641 while (src
< src_end
&& dst
< adjusted_dst_end
)
1643 /* SRC_BASE remembers the start position in source in each loop.
1644 The loop will be exited when there's not enough source text
1645 to analyze two-byte character (within macro ONE_MORE_BYTE).
1646 In that case, SRC is reset to SRC_BASE before exiting. */
1647 unsigned char *src_base
= src
;
1648 unsigned char c1
= *src
++, c2
, c3
, c4
;
1652 if (coding
->eol_type
== CODING_EOL_CRLF
)
1658 /* To process C2 again, SRC is subtracted by 1. */
1666 else if (c1
< 0xA0 || c1
>= 0xE0)
1668 /* SJIS -> JISX0208, BIG5 -> Big5 (only if 0xE0 <= c1 < 0xFF) */
1672 DECODE_SJIS (c1
, c2
, c3
, c4
);
1673 DECODE_CHARACTER_DIMENSION2 (charset_jisx0208
, c3
, c4
);
1675 else if (c1
>= 0xE0 && c1
< 0xFF)
1680 DECODE_BIG5 (c1
, c2
, charset
, c3
, c4
);
1681 DECODE_CHARACTER_DIMENSION2 (charset
, c3
, c4
);
1683 else /* Invalid code */
1688 /* SJIS -> JISX0201-Kana, BIG5 -> Big5 */
1690 DECODE_CHARACTER_DIMENSION1 (charset_katakana_jisx0201
, c1
);
1696 DECODE_BIG5 (c1
, c2
, charset
, c3
, c4
);
1697 DECODE_CHARACTER_DIMENSION2 (charset
, c3
, c4
);
1703 coding
->carryover_size
= src
- src_base
;
1704 bcopy (src_base
, coding
->carryover
, coding
->carryover_size
);
1709 *consumed
= src
- source
;
1710 return dst
- destination
;
1713 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
1714 This function can encode `charset_ascii', `charset_katakana_jisx0201',
1715 `charset_jisx0208', `charset_big5_1', and `charset_big5-2'. We are
1716 sure that all these charsets are registered as official charset
1717 (i.e. do not have extended leading-codes). Characters of other
1718 charsets are produced without any encoding. If SJIS_P is 1, encode
1719 SJIS text, else encode BIG5 text. */
1722 encode_coding_sjis_big5 (coding
, source
, destination
,
1723 src_bytes
, dst_bytes
, consumed
, sjis_p
)
1724 struct coding_system
*coding
;
1725 unsigned char *source
, *destination
;
1726 int src_bytes
, dst_bytes
;
1730 unsigned char *src
= source
;
1731 unsigned char *src_end
= source
+ src_bytes
;
1732 unsigned char *dst
= destination
;
1733 unsigned char *dst_end
= destination
+ dst_bytes
;
1734 /* Since the maximum bytes produced by each loop is 2, we subtract 1
1735 from DST_END to assure overflow checking is necessary only at the
1737 unsigned char *adjusted_dst_end
= dst_end
- 1;
1739 while (src
< src_end
&& dst
< adjusted_dst_end
)
1741 /* SRC_BASE remembers the start position in source in each loop.
1742 The loop will be exited when there's not enough source text
1743 to analyze multi-byte codes (within macros ONE_MORE_BYTE and
1744 TWO_MORE_BYTES). In that case, SRC is reset to SRC_BASE
1746 unsigned char *src_base
= src
;
1747 unsigned char c1
= *src
++, c2
, c3
, c4
;
1749 if (coding
->composing
)
1756 else if (c1
>= 0xA0)
1759 coding
->composing
= 0;
1762 switch (emacs_code_class
[c1
])
1764 case EMACS_ascii_code
:
1765 case EMACS_control_code
:
1769 case EMACS_carriage_return_code
:
1770 if (!coding
->selective
)
1775 /* fall down to treat '\r' as '\n' ... */
1777 case EMACS_linefeed_code
:
1778 if (coding
->eol_type
== CODING_EOL_LF
1779 || coding
->eol_type
== CODING_EOL_AUTOMATIC
)
1781 else if (coding
->eol_type
== CODING_EOL_CRLF
)
1782 *dst
++ = '\r', *dst
++ = '\n';
1787 case EMACS_leading_code_2
:
1789 if (sjis_p
&& c1
== charset_katakana_jisx0201
)
1792 *dst
++ = c1
, *dst
++ = c2
;
1795 case EMACS_leading_code_3
:
1796 TWO_MORE_BYTES (c2
, c3
);
1797 c2
&= 0x7F, c3
&= 0x7F;
1798 if (sjis_p
&& c1
== charset_jisx0208
)
1800 unsigned char s1
, s2
;
1802 ENCODE_SJIS (c2
, c3
, s1
, s2
);
1803 *dst
++ = s1
, *dst
++ = s2
;
1805 else if (!sjis_p
&& (c1
== charset_big5_1
|| c1
== charset_big5_2
))
1807 unsigned char b1
, b2
;
1809 ENCODE_BIG5 (c1
, c2
, c3
, b1
, b2
);
1810 *dst
++ = b1
, *dst
++ = b2
;
1813 *dst
++ = c1
, *dst
++ = c2
, *dst
++ = c3
;
1816 case EMACS_leading_code_4
:
1817 THREE_MORE_BYTES (c2
, c3
, c4
);
1818 *dst
++ = c1
, *dst
++ = c2
, *dst
++ = c3
, *dst
++ = c4
;
1821 case EMACS_leading_code_composition
:
1822 coding
->composing
= 1;
1825 default: /* i.e. case EMACS_invalid_code: */
1831 coding
->carryover_size
= src
- src_base
;
1832 bcopy (src_base
, coding
->carryover
, coding
->carryover_size
);
1837 *consumed
= src
- source
;
1838 return dst
- destination
;
1842 /*** 5. End-of-line handlers ***/
1844 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
1845 This function is called only when `coding->eol_type' is
1846 CODING_EOL_CRLF or CODING_EOL_CR. */
1848 decode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
1849 struct coding_system
*coding
;
1850 unsigned char *source
, *destination
;
1851 int src_bytes
, dst_bytes
;
1854 unsigned char *src
= source
;
1855 unsigned char *src_end
= source
+ src_bytes
;
1856 unsigned char *dst
= destination
;
1857 unsigned char *dst_end
= destination
+ dst_bytes
;
1860 switch (coding
->eol_type
)
1862 case CODING_EOL_CRLF
:
1864 /* Since the maximum bytes produced by each loop is 2, we
1865 subtract 1 from DST_END to assure overflow checking is
1866 necessary only at the head of loop. */
1867 unsigned char *adjusted_dst_end
= dst_end
- 1;
1869 while (src
< src_end
&& dst
< adjusted_dst_end
)
1871 unsigned char *src_base
= src
;
1872 unsigned char c
= *src
++;
1885 coding
->carryover_size
= src
- src_base
;
1886 bcopy (src_base
, coding
->carryover
, coding
->carryover_size
);
1890 *consumed
= src
- source
;
1891 produced
= dst
- destination
;
1896 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
1897 bcopy (source
, destination
, produced
);
1898 dst_end
= destination
+ produced
;
1899 while (dst
< dst_end
)
1900 if (*dst
++ == '\r') dst
[-1] = '\n';
1901 *consumed
= produced
;
1904 default: /* i.e. case: CODING_EOL_LF */
1905 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
1906 bcopy (source
, destination
, produced
);
1907 *consumed
= produced
;
1914 /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". Encode
1915 format of end-of-line according to `coding->eol_type'. If
1916 `coding->selective' is 1, code '\r' in source text also means
1919 encode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
1920 struct coding_system
*coding
;
1921 unsigned char *source
, *destination
;
1922 int src_bytes
, dst_bytes
;
1925 unsigned char *src
= source
;
1926 unsigned char *dst
= destination
;
1932 switch (coding
->eol_type
)
1935 case CODING_EOL_AUTOMATIC
:
1936 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
1937 bcopy (source
, destination
, produced
);
1938 if (coding
->selective
)
1942 if (*dst
++ == '\r') dst
[-1] = '\n';
1944 *consumed
= produced
;
1946 case CODING_EOL_CRLF
:
1949 unsigned char *src_end
= source
+ src_bytes
;
1950 unsigned char *dst_end
= destination
+ dst_bytes
;
1951 /* Since the maximum bytes produced by each loop is 2, we
1952 subtract 1 from DST_END to assure overflow checking is
1953 necessary only at the head of loop. */
1954 unsigned char *adjusted_dst_end
= dst_end
- 1;
1956 while (src
< src_end
&& dst
< adjusted_dst_end
)
1959 if (c
== '\n' || (c
== '\r' && coding
->selective
))
1960 *dst
++ = '\r', *dst
++ = '\n';
1964 produced
= dst
- destination
;
1965 *consumed
= src
- source
;
1969 default: /* i.e. case CODING_EOL_CR: */
1970 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
1971 bcopy (source
, destination
, produced
);
1975 if (*dst
++ == '\n') dst
[-1] = '\r';
1977 *consumed
= produced
;
1984 /*** 6. C library functions ***/
1986 /* In Emacs Lisp, coding system is represented by a Lisp symbol which
1987 has a property `coding-system'. The value of this property is a
1988 vector of length 5 (called as coding-vector). Among elements of
1989 this vector, the first (element[0]) and the fifth (element[4])
1990 carry important information for decoding/encoding. Before
1991 decoding/encoding, this information should be set in fields of a
1992 structure of type `coding_system'.
1994 A value of property `coding-system' can be a symbol of another
1995 subsidiary coding-system. In that case, Emacs gets coding-vector
1998 `element[0]' contains information to be set in `coding->type'. The
1999 value and its meaning is as follows:
2001 0 -- coding_system_internal
2002 1 -- coding_system_sjis
2003 2 -- coding_system_iso2022
2004 3 -- coding_system_big5
2005 4 -- coding_system_ccl
2006 nil -- coding_system_no_conversion
2007 t -- coding_system_automatic
2009 `element[4]' contains information to be set in `coding->flags' and
2010 `coding->spec'. The meaning varies by `coding->type'.
2012 If `coding->type' is `coding_type_iso2022', element[4] is a vector
2013 of length 32 (of which the first 13 sub-elements are used now).
2014 Meanings of these sub-elements are:
2016 sub-element[N] where N is 0 through 3: to be set in `coding->spec.iso2022'
2017 If the value is an integer of valid charset, the charset is
2018 assumed to be designated to graphic register N initially.
2020 If the value is minus, it is a minus value of charset which
2021 reserves graphic register N, which means that the charset is
2022 not designated initially but should be designated to graphic
2023 register N just before encoding a character in that charset.
2025 If the value is nil, graphic register N is never used on
2028 sub-element[N] where N is 4 through 11: to be set in `coding->flags'
2029 Each value takes t or nil. See the section ISO2022 of
2030 `coding.h' for more information.
2032 If `coding->type' is `coding_type_big5', element[4] is t to denote
2033 BIG5-ETen or nil to denote BIG5-HKU.
2035 If `coding->type' takes the other value, element[4] is ignored.
2037 Emacs Lisp's coding system also carries information about format of
2038 end-of-line in a value of property `eol-type'. If the value is
2039 integer, 0 means CODING_EOL_LF, 1 means CODING_EOL_CRLF, and 2
2040 means CODING_EOL_CR. If it is not integer, it should be a vector
2041 of subsidiary coding systems of which property `eol-type' has one
2046 /* Extract information for decoding/encoding from CODING_SYSTEM_SYMBOL
2047 and set it in CODING. If CODING_SYSTEM_SYMBOL is invalid, CODING
2048 is setup so that no conversion is necessary and return -1, else
2052 setup_coding_system (coding_system
, coding
)
2053 Lisp_Object coding_system
;
2054 struct coding_system
*coding
;
2056 Lisp_Object type
, eol_type
;
2058 /* At first, set several fields default values. */
2059 coding
->require_flushing
= 0;
2060 coding
->last_block
= 0;
2061 coding
->selective
= 0;
2062 coding
->composing
= 0;
2063 coding
->direction
= 0;
2064 coding
->carryover_size
= 0;
2065 coding
->post_read_conversion
= coding
->pre_write_conversion
= Qnil
;
2067 Vlast_coding_system_used
= coding
->symbol
= coding_system
;
2069 /* Get value of property `coding-system' until we get a vector.
2070 While doing that, also get values of properties
2071 `post-read-conversion', `pre-write-conversion', and `eol-type'. */
2072 while (!NILP (coding_system
) && SYMBOLP (coding_system
))
2074 if (NILP (coding
->post_read_conversion
))
2075 coding
->post_read_conversion
= Fget (coding_system
,
2076 Qpost_read_conversion
);
2077 if (NILP (coding
->pre_write_conversion
))
2078 coding
->pre_write_conversion
= Fget (coding_system
,
2079 Qpre_write_conversion
);
2080 if (NILP (eol_type
))
2081 eol_type
= Fget (coding_system
, Qeol_type
);
2082 coding_system
= Fget (coding_system
, Qcoding_system
);
2084 if (!VECTORP (coding_system
)
2085 || XVECTOR (coding_system
)->size
!= 5)
2086 goto label_invalid_coding_system
;
2088 if (VECTORP (eol_type
))
2089 coding
->eol_type
= CODING_EOL_AUTOMATIC
;
2090 else if (XFASTINT (eol_type
) == 1)
2091 coding
->eol_type
= CODING_EOL_CRLF
;
2092 else if (XFASTINT (eol_type
) == 2)
2093 coding
->eol_type
= CODING_EOL_CR
;
2095 coding
->eol_type
= CODING_EOL_LF
;
2097 type
= XVECTOR (coding_system
)->contents
[0];
2098 switch (XFASTINT (type
))
2101 coding
->type
= coding_type_internal
;
2105 coding
->type
= coding_type_sjis
;
2109 coding
->type
= coding_type_iso2022
;
2111 Lisp_Object val
= XVECTOR (coding_system
)->contents
[4];
2113 int i
, charset
, default_reg_bits
= 0;
2115 if (!VECTORP (val
) || XVECTOR (val
)->size
!= 32)
2116 goto label_invalid_coding_system
;
2118 flags
= XVECTOR (val
)->contents
;
2120 = ((NILP (flags
[4]) ? 0 : CODING_FLAG_ISO_SHORT_FORM
)
2121 | (NILP (flags
[5]) ? 0 : CODING_FLAG_ISO_RESET_AT_EOL
)
2122 | (NILP (flags
[6]) ? 0 : CODING_FLAG_ISO_RESET_AT_CNTL
)
2123 | (NILP (flags
[7]) ? 0 : CODING_FLAG_ISO_SEVEN_BITS
)
2124 | (NILP (flags
[8]) ? 0 : CODING_FLAG_ISO_LOCKING_SHIFT
)
2125 | (NILP (flags
[9]) ? 0 : CODING_FLAG_ISO_SINGLE_SHIFT
)
2126 | (NILP (flags
[10]) ? 0 : CODING_FLAG_ISO_USE_ROMAN
)
2127 | (NILP (flags
[11]) ? 0 : CODING_FLAG_ISO_USE_OLDJIS
)
2128 | (NILP (flags
[12]) ? 0 : CODING_FLAG_ISO_NO_DIRECTION
)
2129 | (NILP (flags
[13]) ? 0 : CODING_FLAG_ISO_INIT_AT_BOL
)
2130 | (NILP (flags
[14]) ? 0 : CODING_FLAG_ISO_DESIGNATE_AT_BOL
));
2132 /* Invoke graphic register 0 to plane 0. */
2133 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 0;
2134 /* Invoke graphic register 1 to plane 1 if we can use full 8-bit. */
2135 CODING_SPEC_ISO_INVOCATION (coding
, 1)
2136 = (coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
? -1 : 1);
2137 /* Not single shifting at first. */
2138 CODING_SPEC_ISO_SINGLE_SHIFTING(coding
) = 0;
2139 /* Beginning of buffer should also be regarded as bol. */
2140 CODING_SPEC_ISO_BOL(coding
) = 1;
2142 /* Checks FLAGS[REG] (REG = 0, 1, 2 3) and decide designations.
2143 FLAGS[REG] can be one of below:
2144 integer CHARSET: CHARSET occupies register I,
2145 t: designate nothing to REG initially, but can be used
2147 list of integer, nil, or t: designate the first
2148 element (if integer) to REG initially, the remaining
2149 elements (if integer) is designated to REG on request,
2150 if an element is t, REG can be used by any charset,
2151 nil: REG is never used. */
2152 for (charset
= 0; charset
<= MAX_CHARSET
; charset
++)
2153 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
) = -1;
2154 for (i
= 0; i
< 4; i
++)
2156 if (INTEGERP (flags
[i
])
2157 && (charset
= XINT (flags
[i
]), CHARSET_VALID_P (charset
))
2158 || (charset
= get_charset_id (flags
[i
])) >= 0)
2160 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = charset
;
2161 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
) = i
;
2163 else if (EQ (flags
[i
], Qt
))
2165 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
2166 default_reg_bits
|= 1 << i
;
2168 else if (CONSP (flags
[i
]))
2170 Lisp_Object tail
= flags
[i
];
2172 if (INTEGERP (XCONS (tail
)->car
)
2173 && (charset
= XINT (XCONS (tail
)->car
),
2174 CHARSET_VALID_P (charset
))
2175 || (charset
= get_charset_id (XCONS (tail
)->car
)) >= 0)
2177 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = charset
;
2178 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
) =i
;
2181 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
2182 tail
= XCONS (tail
)->cdr
;
2183 while (CONSP (tail
))
2185 if (INTEGERP (XCONS (tail
)->car
)
2186 && (charset
= XINT (XCONS (tail
)->car
),
2187 CHARSET_VALID_P (charset
))
2188 || (charset
= get_charset_id (XCONS (tail
)->car
)) >= 0)
2189 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2191 else if (EQ (XCONS (tail
)->car
, Qt
))
2192 default_reg_bits
|= 1 << i
;
2193 tail
= XCONS (tail
)->cdr
;
2197 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
2199 CODING_SPEC_ISO_DESIGNATION (coding
, i
)
2200 = CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
);
2203 if (! (coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
))
2205 /* REG 1 can be used only by locking shift in 7-bit env. */
2206 if (coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
)
2207 default_reg_bits
&= ~2;
2208 if (! (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
))
2209 /* Without any shifting, only REG 0 and 1 can be used. */
2210 default_reg_bits
&= 3;
2213 for (charset
= 0; charset
<= MAX_CHARSET
; charset
++)
2214 if (CHARSET_VALID_P (charset
)
2215 && CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
) < 0)
2217 /* We have not yet decided where to designate CHARSET. */
2218 int reg_bits
= default_reg_bits
;
2220 if (CHARSET_CHARS (charset
) == 96)
2221 /* A charset of CHARS96 can't be designated to REG 0. */
2225 /* There exist some default graphic register. */
2226 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2228 ? 0 : (reg_bits
& 2 ? 1 : (reg_bits
& 4 ? 2 : 3)));
2230 /* We anyway have to designate CHARSET to somewhere. */
2231 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2232 = (CHARSET_CHARS (charset
) == 94
2234 : ((coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
2235 || ! coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
)
2237 : (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
2241 coding
->require_flushing
= 1;
2245 coding
->type
= coding_type_big5
;
2247 = (NILP (XVECTOR (coding_system
)->contents
[4])
2248 ? CODING_FLAG_BIG5_HKU
2249 : CODING_FLAG_BIG5_ETEN
);
2253 coding
->type
= coding_type_ccl
;
2255 Lisp_Object val
= XVECTOR (coding_system
)->contents
[4];
2257 && VECTORP (XCONS (val
)->car
)
2258 && VECTORP (XCONS (val
)->cdr
))
2260 setup_ccl_program (&(coding
->spec
.ccl
.decoder
), XCONS (val
)->car
);
2261 setup_ccl_program (&(coding
->spec
.ccl
.encoder
), XCONS (val
)->cdr
);
2264 goto label_invalid_coding_system
;
2266 coding
->require_flushing
= 1;
2271 coding
->type
= coding_type_automatic
;
2273 coding
->type
= coding_type_no_conversion
;
2278 label_invalid_coding_system
:
2279 coding
->type
= coding_type_no_conversion
;
2280 coding
->symbol
= coding
->pre_write_conversion
= coding
->post_read_conversion
2285 /* Emacs has a mechanism to automatically detect a coding system if it
2286 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
2287 it's impossible to distinguish some coding systems accurately
2288 because they use the same range of codes. So, at first, coding
2289 systems are categorized into 7, those are:
2291 o coding-category-internal
2293 The category for a coding system which has the same code range
2294 as Emacs' internal format. Assigned the coding-system (Lisp
2295 symbol) `internal' by default.
2297 o coding-category-sjis
2299 The category for a coding system which has the same code range
2300 as SJIS. Assigned the coding-system (Lisp
2301 symbol) `shift-jis' by default.
2303 o coding-category-iso-7
2305 The category for a coding system which has the same code range
2306 as ISO2022 of 7-bit environment. Assigned the coding-system
2307 (Lisp symbol) `iso-2022-7' by default.
2309 o coding-category-iso-8-1
2311 The category for a coding system which has the same code range
2312 as ISO2022 of 8-bit environment and graphic plane 1 used only
2313 for DIMENSION1 charset. Assigned the coding-system (Lisp
2314 symbol) `iso-8859-1' by default.
2316 o coding-category-iso-8-2
2318 The category for a coding system which has the same code range
2319 as ISO2022 of 8-bit environment and graphic plane 1 used only
2320 for DIMENSION2 charset. Assigned the coding-system (Lisp
2321 symbol) `euc-japan' by default.
2323 o coding-category-iso-else
2325 The category for a coding system which has the same code range
2326 as ISO2022 but not belongs to any of the above three
2327 categories. Assigned the coding-system (Lisp symbol)
2328 `iso-2022-ss2-7' by default.
2330 o coding-category-big5
2332 The category for a coding system which has the same code range
2333 as BIG5. Assigned the coding-system (Lisp symbol)
2334 `cn-big5' by default.
2336 o coding-category-binary
2338 The category for a coding system not categorized in any of the
2339 above. Assigned the coding-system (Lisp symbol)
2340 `no-conversion' by default.
2342 Each of them is a Lisp symbol and the value is an actual
2343 `coding-system's (this is also a Lisp symbol) assigned by a user.
2344 What Emacs does actually is to detect a category of coding system.
2345 Then, it uses a `coding-system' assigned to it. If Emacs can't
2346 decide only one possible category, it selects a category of the
2347 highest priority. Priorities of categories are also specified by a
2348 user in a Lisp variable `coding-category-list'.
2352 /* Detect how a text of length SRC_BYTES pointed by SRC is encoded.
2353 If it detects possible coding systems, return an integer in which
2354 appropriate flag bits are set. Flag bits are defined by macros
2355 CODING_CATEGORY_MASK_XXX in `coding.h'. */
2358 detect_coding_mask (src
, src_bytes
)
2362 register unsigned char c
;
2363 unsigned char *src_end
= src
+ src_bytes
;
2366 /* At first, skip all ASCII characters and control characters except
2367 for three ISO2022 specific control characters. */
2368 label_loop_detect_coding
:
2369 while (src
< src_end
)
2373 || (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
))
2379 /* We found nothing other than ASCII. There's nothing to do. */
2380 return CODING_CATEGORY_MASK_ANY
;
2382 /* The text seems to be encoded in some multilingual coding system.
2383 Now, try to find in which coding system the text is encoded. */
2386 /* i.e. (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) */
2387 /* C is an ISO2022 specific control code of C0. */
2388 mask
= detect_coding_iso2022 (src
, src_end
);
2390 if (mask
== CODING_CATEGORY_MASK_ANY
)
2391 /* No valid ISO2022 code follows C. Try again. */
2392 goto label_loop_detect_coding
;
2394 else if (c
== ISO_CODE_SS2
|| c
== ISO_CODE_SS3
|| c
== ISO_CODE_CSI
)
2395 /* C is an ISO2022 specific control code of C1,
2396 or the first byte of SJIS's 2-byte character code,
2397 or a leading code of Emacs. */
2398 mask
= (detect_coding_iso2022 (src
, src_end
)
2399 | detect_coding_sjis (src
, src_end
)
2400 | detect_coding_internal (src
, src_end
));
2403 /* C is the first byte of SJIS character code,
2404 or a leading-code of Emacs. */
2405 mask
= (detect_coding_sjis (src
, src_end
)
2406 | detect_coding_internal (src
, src_end
));
2409 /* C is a character of ISO2022 in graphic plane right,
2410 or a SJIS's 1-byte character code (i.e. JISX0201),
2411 or the first byte of BIG5's 2-byte code. */
2412 mask
= (detect_coding_iso2022 (src
, src_end
)
2413 | detect_coding_sjis (src
, src_end
)
2414 | detect_coding_big5 (src
, src_end
));
2419 /* Detect how a text of length SRC_BYTES pointed by SRC is encoded.
2420 The information of the detected coding system is set in CODING. */
2423 detect_coding (coding
, src
, src_bytes
)
2424 struct coding_system
*coding
;
2428 int mask
= detect_coding_mask (src
, src_bytes
);
2431 if (mask
== CODING_CATEGORY_MASK_ANY
)
2432 /* We found nothing other than ASCII. There's nothing to do. */
2436 /* The source text seems to be encoded in unknown coding system.
2437 Emacs regards the category of such a kind of coding system as
2438 `coding-category-binary'. We assume that a user has assigned
2439 an appropriate coding system for a `coding-category-binary'. */
2440 idx
= CODING_CATEGORY_IDX_BINARY
;
2443 /* We found some plausible coding systems. Let's use a coding
2444 system of the highest priority. */
2445 Lisp_Object val
= Vcoding_category_list
;
2450 idx
= XFASTINT (Fget (XCONS (val
)->car
, Qcoding_category_index
));
2451 if ((idx
< CODING_CATEGORY_IDX_MAX
) && (mask
& (1 << idx
)))
2453 val
= XCONS (val
)->cdr
;
2460 /* For unknown reason, `Vcoding_category_list' contains none
2461 of found categories. Let's use any of them. */
2462 for (idx
= 0; idx
< CODING_CATEGORY_IDX_MAX
; idx
++)
2463 if (mask
& (1 << idx
))
2467 setup_coding_system (XSYMBOL (coding_category_table
[idx
])->value
, coding
);
2470 /* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC
2471 is encoded. Return one of CODING_EOL_LF, CODING_EOL_CRLF,
2472 CODING_EOL_CR, and CODING_EOL_AUTOMATIC. */
2475 detect_eol_type (src
, src_bytes
)
2479 unsigned char *src_end
= src
+ src_bytes
;
2482 while (src
< src_end
)
2486 return CODING_EOL_LF
;
2489 if (src
< src_end
&& *src
== '\n')
2490 return CODING_EOL_CRLF
;
2492 return CODING_EOL_CR
;
2495 return CODING_EOL_AUTOMATIC
;
2498 /* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC
2499 is encoded. If it detects an appropriate format of end-of-line, it
2500 sets the information in *CODING. */
2503 detect_eol (coding
, src
, src_bytes
)
2504 struct coding_system
*coding
;
2509 int eol_type
= detect_eol_type (src
, src_bytes
);
2511 if (eol_type
== CODING_EOL_AUTOMATIC
)
2512 /* We found no end-of-line in the source text. */
2515 val
= Fget (coding
->symbol
, Qeol_type
);
2516 if (VECTORP (val
) && XVECTOR (val
)->size
== 3)
2517 setup_coding_system (XVECTOR (val
)->contents
[eol_type
], coding
);
2520 /* See "GENERAL NOTES about `decode_coding_XXX ()' functions". Before
2521 decoding, it may detect coding system and format of end-of-line if
2522 those are not yet decided. */
2525 decode_coding (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
2526 struct coding_system
*coding
;
2527 unsigned char *source
, *destination
;
2528 int src_bytes
, dst_bytes
;
2539 if (coding
->type
== coding_type_automatic
)
2540 detect_coding (coding
, source
, src_bytes
);
2542 if (coding
->eol_type
== CODING_EOL_AUTOMATIC
)
2543 detect_eol (coding
, source
, src_bytes
);
2545 coding
->carryover_size
= 0;
2546 switch (coding
->type
)
2548 case coding_type_no_conversion
:
2549 label_no_conversion
:
2550 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
2551 bcopy (source
, destination
, produced
);
2552 *consumed
= produced
;
2555 case coding_type_internal
:
2556 case coding_type_automatic
:
2557 if (coding
->eol_type
== CODING_EOL_LF
2558 || coding
->eol_type
== CODING_EOL_AUTOMATIC
)
2559 goto label_no_conversion
;
2560 produced
= decode_eol (coding
, source
, destination
,
2561 src_bytes
, dst_bytes
, consumed
);
2564 case coding_type_sjis
:
2565 produced
= decode_coding_sjis_big5 (coding
, source
, destination
,
2566 src_bytes
, dst_bytes
, consumed
,
2570 case coding_type_iso2022
:
2571 produced
= decode_coding_iso2022 (coding
, source
, destination
,
2572 src_bytes
, dst_bytes
, consumed
);
2575 case coding_type_big5
:
2576 produced
= decode_coding_sjis_big5 (coding
, source
, destination
,
2577 src_bytes
, dst_bytes
, consumed
,
2581 case coding_type_ccl
:
2582 produced
= ccl_driver (&coding
->spec
.ccl
.decoder
, source
, destination
,
2583 src_bytes
, dst_bytes
, consumed
);
2590 /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". */
2593 encode_coding (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
2594 struct coding_system
*coding
;
2595 unsigned char *source
, *destination
;
2596 int src_bytes
, dst_bytes
;
2601 coding
->carryover_size
= 0;
2602 switch (coding
->type
)
2604 case coding_type_no_conversion
:
2605 label_no_conversion
:
2606 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
2609 bcopy (source
, destination
, produced
);
2610 if (coding
->selective
)
2612 unsigned char *p
= destination
, *pend
= destination
+ produced
;
2614 if (*p
++ == '\015') p
[-1] = '\n';
2617 *consumed
= produced
;
2620 case coding_type_internal
:
2621 case coding_type_automatic
:
2622 if (coding
->eol_type
== CODING_EOL_LF
2623 || coding
->eol_type
== CODING_EOL_AUTOMATIC
)
2624 goto label_no_conversion
;
2625 produced
= encode_eol (coding
, source
, destination
,
2626 src_bytes
, dst_bytes
, consumed
);
2629 case coding_type_sjis
:
2630 produced
= encode_coding_sjis_big5 (coding
, source
, destination
,
2631 src_bytes
, dst_bytes
, consumed
,
2635 case coding_type_iso2022
:
2636 produced
= encode_coding_iso2022 (coding
, source
, destination
,
2637 src_bytes
, dst_bytes
, consumed
);
2640 case coding_type_big5
:
2641 produced
= encode_coding_sjis_big5 (coding
, source
, destination
,
2642 src_bytes
, dst_bytes
, consumed
,
2646 case coding_type_ccl
:
2647 produced
= ccl_driver (&coding
->spec
.ccl
.encoder
, source
, destination
,
2648 src_bytes
, dst_bytes
, consumed
);
2655 #define CONVERSION_BUFFER_EXTRA_ROOM 256
2657 /* Return maximum size (bytes) of a buffer enough for decoding
2658 SRC_BYTES of text encoded in CODING. */
2661 decoding_buffer_size (coding
, src_bytes
)
2662 struct coding_system
*coding
;
2667 if (coding
->type
== coding_type_iso2022
)
2669 else if (coding
->type
== coding_type_ccl
)
2670 magnification
= coding
->spec
.ccl
.decoder
.buf_magnification
;
2674 return (src_bytes
* magnification
+ CONVERSION_BUFFER_EXTRA_ROOM
);
2677 /* Return maximum size (bytes) of a buffer enough for encoding
2678 SRC_BYTES of text to CODING. */
2681 encoding_buffer_size (coding
, src_bytes
)
2682 struct coding_system
*coding
;
2687 if (coding
->type
== coding_type_ccl
)
2688 magnification
= coding
->spec
.ccl
.encoder
.buf_magnification
;
2692 return (src_bytes
* magnification
+ CONVERSION_BUFFER_EXTRA_ROOM
);
2695 #ifndef MINIMUM_CONVERSION_BUFFER_SIZE
2696 #define MINIMUM_CONVERSION_BUFFER_SIZE 1024
2699 char *conversion_buffer
;
2700 int conversion_buffer_size
;
2702 /* Return a pointer to a SIZE bytes of buffer to be used for encoding
2703 or decoding. Sufficient memory is allocated automatically. If we
2704 run out of memory, return NULL. */
2707 get_conversion_buffer (size
)
2710 if (size
> conversion_buffer_size
)
2713 int real_size
= conversion_buffer_size
* 2;
2715 while (real_size
< size
) real_size
*= 2;
2716 buf
= (char *) xmalloc (real_size
);
2717 xfree (conversion_buffer
);
2718 conversion_buffer
= buf
;
2719 conversion_buffer_size
= real_size
;
2721 return conversion_buffer
;
2726 /*** 7. Emacs Lisp library functions ***/
2728 DEFUN ("coding-system-vector", Fcoding_system_vector
, Scoding_system_vector
,
2730 "Return coding-vector of CODING-SYSTEM.\n\
2731 If CODING-SYSTEM is not a valid coding-system, return nil.")
2735 while (SYMBOLP (obj
) && !NILP (obj
))
2736 obj
= Fget (obj
, Qcoding_system
);
2737 return ((NILP (obj
) || !VECTORP (obj
) || XVECTOR (obj
)->size
!= 5)
2741 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
2742 "Return t if OBJECT is nil or a coding-system.\n\
2743 See document of make-coding-system for coding-system object.")
2747 return ((NILP (obj
) || !NILP (Fcoding_system_vector (obj
))) ? Qt
: Qnil
);
2750 DEFUN ("read-non-nil-coding-system",
2751 Fread_non_nil_coding_system
, Sread_non_nil_coding_system
, 1, 1, 0,
2752 "Read a coding system from the minibuffer, prompting with string PROMPT.")
2758 val
= Fcompleting_read (prompt
, Vobarray
, Qcoding_system_vector
,
2760 } while (XSTRING (val
)->size
== 0);
2761 return (Fintern (val
, Qnil
));
2764 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 1, 0,
2765 "Read a coding system or nil from the minibuffer, prompting with string PROMPT.")
2769 Lisp_Object val
= Fcompleting_read (prompt
, Vobarray
, Qcoding_system_p
,
2771 return (XSTRING (val
)->size
== 0 ? Qnil
: Fintern (val
, Qnil
));
2774 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
2776 "Check validity of CODING-SYSTEM.\n\
2777 If valid, return CODING-SYSTEM, else `coding-system-error' is signaled.\n\
2778 CODING-SYSTEM is valid if it is a symbol and has \"coding-system\" property.\n\
2779 The value of property should be a vector of length 5.")
2781 Lisp_Object coding_system
;
2783 CHECK_SYMBOL (coding_system
, 0);
2784 if (!NILP (Fcoding_system_p (coding_system
)))
2785 return coding_system
;
2787 Fsignal (Qcoding_system_error
, coding_system
);
2790 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
2792 "Detect coding-system of the text in the region between START and END.\n\
2793 Return a list of possible coding-systems ordered by priority.\n\
2794 If only ASCII characters are found, it returns `automatic-conversion'\n\
2795 or its subsidiary coding-system according to a detected end-of-line format.")
2799 int coding_mask
, eol_type
;
2803 validate_region (&b
, &e
);
2804 beg
= XINT (b
), end
= XINT (e
);
2805 if (beg
< GPT
&& end
>= GPT
) move_gap (end
);
2807 coding_mask
= detect_coding_mask (POS_ADDR (beg
), end
- beg
);
2808 eol_type
= detect_eol_type (POS_ADDR (beg
), end
- beg
);
2810 if (coding_mask
== CODING_CATEGORY_MASK_ANY
)
2812 val
= intern ("automatic-conversion");
2813 if (eol_type
!= CODING_EOL_AUTOMATIC
)
2815 Lisp_Object val2
= Fget (val
, Qeol_type
);
2817 val
= XVECTOR (val2
)->contents
[eol_type
];
2824 /* At first, gather possible coding-systems in VAL in a reverse
2827 for (val2
= Vcoding_category_list
;
2829 val2
= XCONS (val2
)->cdr
)
2832 = XFASTINT (Fget (XCONS (val2
)->car
, Qcoding_category_index
));
2833 if (coding_mask
& (1 << idx
))
2834 val
= Fcons (Fsymbol_value (XCONS (val2
)->car
), val
);
2837 /* Then, change the order of the list, while getting subsidiary
2841 for (; !NILP (val2
); val2
= XCONS (val2
)->cdr
)
2843 if (eol_type
== CODING_EOL_AUTOMATIC
)
2844 val
= Fcons (XCONS (val2
)->car
, val
);
2847 Lisp_Object val3
= Fget (XCONS (val2
)->car
, Qeol_type
);
2849 val
= Fcons (XVECTOR (val3
)->contents
[eol_type
], val
);
2851 val
= Fcons (XCONS (val2
)->car
, val
);
2859 /* Scan text in the region between *BEGP and *ENDP, skip characters
2860 which we never have to encode to (iff ENCODEP is 1) or decode from
2861 coding system CODING at the head and tail, then set BEGP and ENDP
2862 to the addresses of start and end of the text we actually convert. */
2865 shrink_conversion_area (begp
, endp
, coding
, encodep
)
2866 unsigned char **begp
, **endp
;
2867 struct coding_system
*coding
;
2870 register unsigned char *beg_addr
= *begp
, *end_addr
= *endp
;
2872 if (coding
->eol_type
!= CODING_EOL_LF
2873 && coding
->eol_type
!= CODING_EOL_AUTOMATIC
)
2874 /* Since we anyway have to convert end-of-line format, it is not
2875 worth skipping at most 100 bytes or so. */
2878 if (encodep
) /* for encoding */
2880 switch (coding
->type
)
2882 case coding_type_no_conversion
:
2883 case coding_type_internal
:
2884 case coding_type_automatic
:
2885 /* We need no conversion. */
2888 case coding_type_ccl
:
2889 /* We can't skip any data. */
2891 case coding_type_iso2022
:
2892 if (coding
->flags
& CODING_FLAG_ISO_DESIGNATE_AT_BOL
)
2894 unsigned char *bol
= beg_addr
;
2895 while (beg_addr
< end_addr
&& *beg_addr
< 0x80)
2898 if (*(beg_addr
- 1) == '\n')
2902 goto label_skip_tail
;
2906 /* We can skip all ASCII characters at the head and tail. */
2907 while (beg_addr
< end_addr
&& *beg_addr
< 0x80) beg_addr
++;
2909 while (beg_addr
< end_addr
&& *(end_addr
- 1) < 0x80) end_addr
--;
2913 else /* for decoding */
2915 switch (coding
->type
)
2917 case coding_type_no_conversion
:
2918 /* We need no conversion. */
2921 case coding_type_internal
:
2922 if (coding
->eol_type
== CODING_EOL_LF
)
2924 /* We need no conversion. */
2928 /* We can skip all but carriage-return. */
2929 while (beg_addr
< end_addr
&& *beg_addr
!= '\r') beg_addr
++;
2930 while (beg_addr
< end_addr
&& *(end_addr
- 1) != '\r') end_addr
--;
2932 case coding_type_sjis
:
2933 case coding_type_big5
:
2934 /* We can skip all ASCII characters at the head. */
2935 while (beg_addr
< end_addr
&& *beg_addr
< 0x80) beg_addr
++;
2936 /* We can skip all ASCII characters at the tail except for
2937 the second byte of SJIS or BIG5 code. */
2938 while (beg_addr
< end_addr
&& *(end_addr
- 1) < 0x80) end_addr
--;
2939 if (end_addr
!= *endp
)
2942 case coding_type_ccl
:
2943 /* We can't skip any data. */
2945 default: /* i.e. case coding_type_iso2022: */
2949 /* We can skip all ASCII characters except for a few
2950 control codes at the head. */
2951 while (beg_addr
< end_addr
&& (c
= *beg_addr
) < 0x80
2952 && c
!= ISO_CODE_CR
&& c
!= ISO_CODE_SO
2953 && c
!= ISO_CODE_SI
&& c
!= ISO_CODE_ESC
)
2964 /* Encode to (iff ENCODEP is 1) or decode form coding system CODING a
2965 text between B and E. B and E are buffer position. */
2968 code_convert_region (b
, e
, coding
, encodep
)
2970 struct coding_system
*coding
;
2973 int beg
, end
, len
, consumed
, produced
;
2975 unsigned char *begp
, *endp
;
2978 validate_region (&b
, &e
);
2979 beg
= XINT (b
), end
= XINT (e
);
2980 if (beg
< GPT
&& end
>= GPT
)
2983 if (encodep
&& !NILP (coding
->pre_write_conversion
))
2985 /* We must call a pre-conversion function which may put a new
2986 text to be converted in a new buffer. */
2987 struct buffer
*old
= current_buffer
, *new;
2990 call2 (coding
->pre_write_conversion
, b
, e
);
2991 if (old
!= current_buffer
)
2993 /* Replace the original text by the text just generated. */
2995 new = current_buffer
;
2996 set_buffer_internal (old
);
2997 del_range (beg
, end
);
2998 insert_from_buffer (new, 1, len
, 0);
3003 /* We may be able to shrink the conversion region. */
3004 begp
= POS_ADDR (beg
); endp
= begp
+ (end
- beg
);
3005 shrink_conversion_area (&begp
, &endp
, coding
, encodep
);
3008 /* We need no conversion. */
3012 beg
+= begp
- POS_ADDR (beg
);
3013 end
= beg
+ (endp
- begp
);
3016 len
= encoding_buffer_size (coding
, end
- beg
);
3018 len
= decoding_buffer_size (coding
, end
- beg
);
3019 buf
= get_conversion_buffer (len
);
3021 coding
->last_block
= 1;
3023 ? encode_coding (coding
, POS_ADDR (beg
), buf
, end
- beg
, len
,
3025 : decode_coding (coding
, POS_ADDR (beg
), buf
, end
- beg
, len
,
3028 len
= produced
+ (beg
- XINT (b
)) + (XINT (e
) - end
);
3031 insert (buf
, produced
);
3032 del_range (PT
, PT
+ end
- beg
);
3034 pos
= PT
+ (pos
- end
);
3040 if (!encodep
&& !NILP (coding
->post_read_conversion
))
3042 /* We must call a post-conversion function which may alter
3043 the text just converted. */
3048 insval
= call1 (coding
->post_read_conversion
, make_number (len
));
3049 CHECK_NUMBER (insval
, 0);
3050 len
= XINT (insval
);
3053 return make_number (len
);
3057 code_convert_string (str
, coding
, encodep
, nocopy
)
3058 Lisp_Object str
, nocopy
;
3059 struct coding_system
*coding
;
3062 int len
, consumed
, produced
;
3064 unsigned char *begp
, *endp
;
3065 int head_skip
, tail_skip
;
3066 struct gcpro gcpro1
;
3068 if (encodep
&& !NILP (coding
->pre_write_conversion
)
3069 || !encodep
&& !NILP (coding
->post_read_conversion
))
3071 /* Since we have to call Lisp functions which assume target text
3072 is in a buffer, after setting a temporary buffer, call
3073 code_convert_region. */
3074 int count
= specpdl_ptr
- specpdl
;
3075 int len
= XSTRING (str
)->size
;
3077 struct buffer
*old
= current_buffer
;
3079 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
3080 temp_output_buffer_setup (" *code-converting-work*");
3081 set_buffer_internal (XBUFFER (Vstandard_output
));
3082 insert_from_string (str
, 0, len
, 0);
3083 code_convert_region (make_number (BEGV
), make_number (ZV
),
3085 result
= make_buffer_string (BEGV
, ZV
, 0);
3086 set_buffer_internal (old
);
3087 return unbind_to (count
, result
);
3090 /* We may be able to shrink the conversion region. */
3091 begp
= XSTRING (str
)->data
;
3092 endp
= begp
+ XSTRING (str
)->size
;
3093 shrink_conversion_area (&begp
, &endp
, coding
, encodep
);
3096 /* We need no conversion. */
3097 return (NILP (nocopy
) ? Fcopy_sequence (str
) : str
);
3099 head_skip
= begp
- XSTRING (str
)->data
;
3100 tail_skip
= XSTRING (str
)->size
- head_skip
- (endp
- begp
);
3105 len
= encoding_buffer_size (coding
, endp
- begp
);
3107 len
= decoding_buffer_size (coding
, endp
- begp
);
3108 buf
= get_conversion_buffer (len
+ head_skip
+ tail_skip
);
3110 bcopy (XSTRING (str
)->data
, buf
, head_skip
);
3111 coding
->last_block
= 1;
3113 ? encode_coding (coding
, XSTRING (str
)->data
+ head_skip
,
3114 buf
+ head_skip
, endp
- begp
, len
, &consumed
)
3115 : decode_coding (coding
, XSTRING (str
)->data
+ head_skip
,
3116 buf
+ head_skip
, endp
- begp
, len
, &consumed
));
3117 bcopy (XSTRING (str
)->data
+ head_skip
+ (endp
- begp
),
3118 buf
+ head_skip
+ produced
,
3123 return make_string (buf
, head_skip
+ produced
+ tail_skip
);
3126 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
3127 3, 3, "r\nzCoding system: ",
3128 "Decode current region by specified coding system.\n\
3129 When called from a program, takes three arguments:\n\
3130 START, END, and CODING-SYSTEM. START END are buffer positions.\n\
3131 Return length of decoded text.")
3132 (b
, e
, coding_system
)
3133 Lisp_Object b
, e
, coding_system
;
3135 struct coding_system coding
;
3137 CHECK_NUMBER_COERCE_MARKER (b
, 0);
3138 CHECK_NUMBER_COERCE_MARKER (e
, 1);
3139 CHECK_SYMBOL (coding_system
, 2);
3141 if (NILP (coding_system
))
3142 return make_number (XFASTINT (e
) - XFASTINT (b
));
3143 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
3144 error ("Invalid coding-system: %s", XSYMBOL (coding_system
)->name
->data
);
3146 return code_convert_region (b
, e
, &coding
, 0);
3149 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
3150 3, 3, "r\nzCoding system: ",
3151 "Encode current region by specified coding system.\n\
3152 When called from a program, takes three arguments:\n\
3153 START, END, and CODING-SYSTEM. START END are buffer positions.\n\
3154 Return length of encoded text.")
3155 (b
, e
, coding_system
)
3156 Lisp_Object b
, e
, coding_system
;
3158 struct coding_system coding
;
3160 CHECK_NUMBER_COERCE_MARKER (b
, 0);
3161 CHECK_NUMBER_COERCE_MARKER (e
, 1);
3162 CHECK_SYMBOL (coding_system
, 2);
3164 if (NILP (coding_system
))
3165 return make_number (XFASTINT (e
) - XFASTINT (b
));
3166 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
3167 error ("Invalid coding-system: %s", XSYMBOL (coding_system
)->name
->data
);
3169 return code_convert_region (b
, e
, &coding
, 1);
3172 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
3174 "Decode STRING which is encoded in CODING-SYSTEM, and return the result.\n\
3175 Optional arg NOCOPY non-nil means return STRING itself if there's no need\n\
3177 (string
, coding_system
, nocopy
)
3178 Lisp_Object string
, coding_system
, nocopy
;
3180 struct coding_system coding
;
3182 CHECK_STRING (string
, 0);
3183 CHECK_SYMBOL (coding_system
, 1);
3185 if (NILP (coding_system
))
3186 return (NILP (nocopy
) ? Fcopy_sequence (string
) : string
);
3187 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
3188 error ("Invalid coding-system: %s", XSYMBOL (coding_system
)->name
->data
);
3190 return code_convert_string (string
, &coding
, 0, nocopy
);
3193 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
3195 "Encode STRING to CODING-SYSTEM, and return the result.\n\
3196 Optional arg NOCOPY non-nil means return STRING itself if there's no need\n\
3198 (string
, coding_system
, nocopy
)
3199 Lisp_Object string
, coding_system
, nocopy
;
3201 struct coding_system coding
;
3203 CHECK_STRING (string
, 0);
3204 CHECK_SYMBOL (coding_system
, 1);
3206 if (NILP (coding_system
))
3207 return (NILP (nocopy
) ? Fcopy_sequence (string
) : string
);
3208 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
3209 error ("Invalid coding-system: %s", XSYMBOL (coding_system
)->name
->data
);
3211 return code_convert_string (string
, &coding
, 1, nocopy
);
3214 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
3215 "Decode a JISX0208 character of shift-jis encoding.\n\
3216 CODE is the character code in SJIS.\n\
3217 Return the corresponding character.")
3221 unsigned char c1
, c2
, s1
, s2
;
3224 CHECK_NUMBER (code
, 0);
3225 s1
= (XFASTINT (code
)) >> 8, s2
= (XFASTINT (code
)) & 0xFF;
3226 DECODE_SJIS (s1
, s2
, c1
, c2
);
3227 XSETFASTINT (val
, MAKE_NON_ASCII_CHAR (charset_jisx0208
, c1
, c2
));
3231 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
3232 "Encode a JISX0208 character CHAR to SJIS coding-system.\n\
3233 Return the corresponding character code in SJIS.")
3237 int charset
, c1
, c2
, s1
, s2
;
3240 CHECK_NUMBER (ch
, 0);
3241 SPLIT_CHAR (XFASTINT (ch
), charset
, c1
, c2
);
3242 if (charset
== charset_jisx0208
)
3244 ENCODE_SJIS (c1
, c2
, s1
, s2
);
3245 XSETFASTINT (val
, (s1
<< 8) | s2
);
3248 XSETFASTINT (val
, 0);
3252 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
3253 "Decode a Big5 character CODE of BIG5 coding-system.\n\
3254 CODE is the character code in BIG5.\n\
3255 Return the corresponding character.")
3260 unsigned char b1
, b2
, c1
, c2
;
3263 CHECK_NUMBER (code
, 0);
3264 b1
= (XFASTINT (code
)) >> 8, b2
= (XFASTINT (code
)) & 0xFF;
3265 DECODE_BIG5 (b1
, b2
, charset
, c1
, c2
);
3266 XSETFASTINT (val
, MAKE_NON_ASCII_CHAR (charset
, c1
, c2
));
3270 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
3271 "Encode the Big5 character CHAR to BIG5 coding-system.\n\
3272 Return the corresponding character code in Big5.")
3276 int charset
, c1
, c2
, b1
, b2
;
3279 CHECK_NUMBER (ch
, 0);
3280 SPLIT_CHAR (XFASTINT (ch
), charset
, c1
, c2
);
3281 if (charset
== charset_big5_1
|| charset
== charset_big5_2
)
3283 ENCODE_BIG5 (charset
, c1
, c2
, b1
, b2
);
3284 XSETFASTINT (val
, (b1
<< 8) | b2
);
3287 XSETFASTINT (val
, 0);
3291 DEFUN ("set-terminal-coding-system",
3292 Fset_terminal_coding_system
, Sset_terminal_coding_system
, 1, 1,
3293 "zCoding-system for terminal display: ",
3294 "Set coding-system of your terminal to CODING-SYSTEM.\n\
3295 All outputs to terminal are encoded to this coding-system.")
3297 Lisp_Object coding_system
;
3299 CHECK_SYMBOL (coding_system
, 0);
3300 setup_coding_system (Fcheck_coding_system (coding_system
), &terminal_coding
);
3301 update_mode_lines
++;
3302 if (!NILP (Finteractive_p ()))
3307 DEFUN ("terminal-coding-system",
3308 Fterminal_coding_system
, Sterminal_coding_system
, 0, 0, 0,
3309 "Return coding-system of your terminal.")
3312 return terminal_coding
.symbol
;
3315 DEFUN ("set-keyboard-coding-system",
3316 Fset_keyboard_coding_system
, Sset_keyboard_coding_system
, 1, 1,
3317 "zCoding-system for keyboard input: ",
3318 "Set coding-system of what is sent from terminal keyboard to CODING-SYSTEM.\n\
3319 All inputs from terminal are decoded from this coding-system.")
3321 Lisp_Object coding_system
;
3323 CHECK_SYMBOL (coding_system
, 0);
3324 setup_coding_system (Fcheck_coding_system (coding_system
), &keyboard_coding
);
3328 DEFUN ("keyboard-coding-system",
3329 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 0, 0,
3330 "Return coding-system of what is sent from terminal keyboard.")
3333 return keyboard_coding
.symbol
;
3337 DEFUN ("find-coding-system", Ffind_coding_system
, Sfind_coding_system
,
3339 "Choose a coding system for a file operation based on file name.\n\
3340 The value names a pair of coding systems: (ENCODING-SYSTEM DECODING-SYSTEM).\n\
3341 ENCODING-SYSTEM is the coding system to use for encoding\n\
3342 \(in case OPERATION does encoding), and DECODING-SYSTEM is the coding system\n\
3343 for decoding (in case OPERATION does decoding).\n\
3345 The first argument OPERATION specifies an I/O primitive:\n\
3346 For file I/O, `insert-file-contents' or `write-region'.\n\
3347 For process I/O, `call-process', `call-process-region', or `start-process'.\n\
3348 For network I/O, `open-network-stream'.\n\
3350 The remaining arguments should be the same arguments that were passed\n\
3351 to the primitive. Depending on which primitive, one of those arguments\n\
3352 is selected as the TARGET. For example, if OPERATION does file I/O,\n\
3353 whichever argument specifies the file name is TARGET.\n\
3355 TARGET has a meaning which depends on OPERATION:\n\
3356 For file I/O, TARGET is a file name.\n\
3357 For process I/O, TARGET is a process name.\n\
3358 For network I/O, TARGET is a service name or a port number\n\
3360 This function looks up what `coding-system-alist' specifies for\n\
3361 OPERATION and TARGET. It may specify a cons cell which represents\n\
3362 a particular coding system or it may have a function to call.\n\
3363 In the latter case, we call the function with one argument,\n\
3364 which is a list of all the arguments given to `find-coding-system'.")
3369 Lisp_Object operation
, target_idx
, target
, val
;
3370 register Lisp_Object chain
;
3373 error ("Too few arguments");
3374 operation
= args
[0];
3375 if (!SYMBOLP (operation
)
3376 || !INTEGERP (target_idx
= Fget (operation
, Qtarget_idx
)))
3377 error ("Invalid first arguement");
3378 if (nargs
< 1 + XINT (target_idx
))
3379 error ("Too few arguments for operation: %s",
3380 XSYMBOL (operation
)->name
->data
);
3381 target
= args
[XINT (target_idx
) + 1];
3382 if (!(STRINGP (target
)
3383 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
3384 error ("Invalid %dth argument", XINT (target_idx
) + 1);
3386 chain
= Fassq (operation
, Vcoding_system_alist
);
3390 for (chain
= XCONS (chain
)->cdr
; CONSP (chain
); chain
= XCONS (chain
)->cdr
)
3392 Lisp_Object elt
= XCONS (chain
)->car
;
3395 && ((STRINGP (target
)
3396 && STRINGP (XCONS (elt
)->car
)
3397 && fast_string_match (XCONS (elt
)->car
, target
) >= 0)
3398 || (INTEGERP (target
) && EQ (target
, XCONS (elt
)->car
))))
3399 return (val
= XCONS (elt
)->cdr
, CONSP (val
)
3401 : ((SYMBOLP (val
) && !NILP (Fboundp (val
))
3402 ? call2 (val
, Flist (nargs
, args
))
3411 /*** 8. Post-amble ***/
3417 /* Emacs internal format specific initialize routine. */
3418 for (i
= 0; i
<= 0x20; i
++)
3419 emacs_code_class
[i
] = EMACS_control_code
;
3420 emacs_code_class
[0x0A] = EMACS_linefeed_code
;
3421 emacs_code_class
[0x0D] = EMACS_carriage_return_code
;
3422 for (i
= 0x21 ; i
< 0x7F; i
++)
3423 emacs_code_class
[i
] = EMACS_ascii_code
;
3424 emacs_code_class
[0x7F] = EMACS_control_code
;
3425 emacs_code_class
[0x80] = EMACS_leading_code_composition
;
3426 for (i
= 0x81; i
< 0xFF; i
++)
3427 emacs_code_class
[i
] = EMACS_invalid_code
;
3428 emacs_code_class
[LEADING_CODE_PRIVATE_11
] = EMACS_leading_code_3
;
3429 emacs_code_class
[LEADING_CODE_PRIVATE_12
] = EMACS_leading_code_3
;
3430 emacs_code_class
[LEADING_CODE_PRIVATE_21
] = EMACS_leading_code_4
;
3431 emacs_code_class
[LEADING_CODE_PRIVATE_22
] = EMACS_leading_code_4
;
3433 /* ISO2022 specific initialize routine. */
3434 for (i
= 0; i
< 0x20; i
++)
3435 iso_code_class
[i
] = ISO_control_code
;
3436 for (i
= 0x21; i
< 0x7F; i
++)
3437 iso_code_class
[i
] = ISO_graphic_plane_0
;
3438 for (i
= 0x80; i
< 0xA0; i
++)
3439 iso_code_class
[i
] = ISO_control_code
;
3440 for (i
= 0xA1; i
< 0xFF; i
++)
3441 iso_code_class
[i
] = ISO_graphic_plane_1
;
3442 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
3443 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
3444 iso_code_class
[ISO_CODE_CR
] = ISO_carriage_return
;
3445 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
3446 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
3447 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
3448 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
3449 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
3450 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
3451 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
3453 conversion_buffer_size
= MINIMUM_CONVERSION_BUFFER_SIZE
;
3454 conversion_buffer
= (char *) xmalloc (MINIMUM_CONVERSION_BUFFER_SIZE
);
3456 setup_coding_system (Qnil
, &keyboard_coding
);
3457 setup_coding_system (Qnil
, &terminal_coding
);
3464 Qtarget_idx
= intern ("target-idx");
3465 staticpro (&Qtarget_idx
);
3467 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
3468 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
3470 Qcall_process
= intern ("call-process");
3471 staticpro (&Qcall_process
);
3472 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
3474 Qcall_process_region
= intern ("call-process-region");
3475 staticpro (&Qcall_process_region
);
3476 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
3478 Qstart_process
= intern ("start-process");
3479 staticpro (&Qstart_process
);
3480 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
3482 Qopen_network_stream
= intern ("open-network-stream");
3483 staticpro (&Qopen_network_stream
);
3484 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
3486 Qcoding_system
= intern ("coding-system");
3487 staticpro (&Qcoding_system
);
3489 Qeol_type
= intern ("eol-type");
3490 staticpro (&Qeol_type
);
3492 Qbuffer_file_coding_system
= intern ("buffer-file-coding-system");
3493 staticpro (&Qbuffer_file_coding_system
);
3495 Qpost_read_conversion
= intern ("post-read-conversion");
3496 staticpro (&Qpost_read_conversion
);
3498 Qpre_write_conversion
= intern ("pre-write-conversion");
3499 staticpro (&Qpre_write_conversion
);
3501 Qcoding_system_vector
= intern ("coding-system-vector");
3502 staticpro (&Qcoding_system_vector
);
3504 Qcoding_system_p
= intern ("coding-system-p");
3505 staticpro (&Qcoding_system_p
);
3507 Qcoding_system_error
= intern ("coding-system-error");
3508 staticpro (&Qcoding_system_error
);
3510 Fput (Qcoding_system_error
, Qerror_conditions
,
3511 Fcons (Qcoding_system_error
, Fcons (Qerror
, Qnil
)));
3512 Fput (Qcoding_system_error
, Qerror_message
,
3513 build_string ("Coding-system error"));
3515 Qcoding_category_index
= intern ("coding-category-index");
3516 staticpro (&Qcoding_category_index
);
3520 for (i
= 0; i
< CODING_CATEGORY_IDX_MAX
; i
++)
3522 coding_category_table
[i
] = intern (coding_category_name
[i
]);
3523 staticpro (&coding_category_table
[i
]);
3524 Fput (coding_category_table
[i
], Qcoding_category_index
,
3529 defsubr (&Scoding_system_vector
);
3530 defsubr (&Scoding_system_p
);
3531 defsubr (&Sread_coding_system
);
3532 defsubr (&Sread_non_nil_coding_system
);
3533 defsubr (&Scheck_coding_system
);
3534 defsubr (&Sdetect_coding_region
);
3535 defsubr (&Sdecode_coding_region
);
3536 defsubr (&Sencode_coding_region
);
3537 defsubr (&Sdecode_coding_string
);
3538 defsubr (&Sencode_coding_string
);
3539 defsubr (&Sdecode_sjis_char
);
3540 defsubr (&Sencode_sjis_char
);
3541 defsubr (&Sdecode_big5_char
);
3542 defsubr (&Sencode_big5_char
);
3543 defsubr (&Sset_terminal_coding_system
);
3544 defsubr (&Sterminal_coding_system
);
3545 defsubr (&Sset_keyboard_coding_system
);
3546 defsubr (&Skeyboard_coding_system
);
3547 defsubr (&Sfind_coding_system
);
3549 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list
,
3550 "List of coding-categories (symbols) ordered by priority.");
3554 Vcoding_category_list
= Qnil
;
3555 for (i
= CODING_CATEGORY_IDX_MAX
- 1; i
>= 0; i
--)
3556 Vcoding_category_list
3557 = Fcons (coding_category_table
[i
], Vcoding_category_list
);
3560 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read
,
3561 "A variable of internal use only.\n\
3562 If the value is a coding system, it is used for decoding on read operation.\n\
3563 If not, an appropriate element in `coding-system-alist' (which see) is used.");
3564 Vcoding_system_for_read
= Qnil
;
3566 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write
,
3567 "A variable of internal use only.\n\
3568 If the value is a coding system, it is used for encoding on write operation.\n\
3569 If not, an appropriate element in `coding-system-alist' (which see) is used.");
3570 Vcoding_system_for_write
= Qnil
;
3572 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used
,
3573 "Coding-system used in the latest file or process I/O.");
3574 Vlast_coding_system_used
= Qnil
;
3576 DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist
,
3577 "Nested alist to decide a coding system for a specific I/O operation.\n\
3578 The format is ((OPERATION . ((REGEXP . CODING-SYSTEMS) ...)) ...).\n\
3580 OPERATION is one of the following Emacs I/O primitives:\n\
3581 For file I/O, insert-file-contents and write-region.\n\
3582 For process I/O, call-process, call-process-region, and start-process.\n\
3583 For network I/O, open-network-stream.\n\
3584 In addition, for process I/O, `process-argument' can be specified for\n\
3585 encoding arguments of the process.\n\
3587 REGEXP is a regular expression matching a target of OPERATION, where\n\
3588 target is a file name for file I/O operations, a process name for\n\
3589 process I/O operations, or a service name for network I/O\n\
3590 operations. REGEXP might be a port number for network I/O operation.\n\
3592 CODING-SYSTEMS is a cons of coding systems to encode and decode\n\
3593 character code on OPERATION, or a function symbol returning the cons.\n\
3594 See the documentation of `find-coding-system' for more detail.");
3595 Vcoding_system_alist
= Qnil
;
3597 DEFVAR_INT ("eol-mnemonic-unix", &eol_mnemonic_unix
,
3598 "Mnemonic character indicating UNIX-like end-of-line format (i.e. LF) .");
3599 eol_mnemonic_unix
= '.';
3601 DEFVAR_INT ("eol-mnemonic-dos", &eol_mnemonic_dos
,
3602 "Mnemonic character indicating DOS-like end-of-line format (i.e. CRLF).");
3603 eol_mnemonic_dos
= ':';
3605 DEFVAR_INT ("eol-mnemonic-mac", &eol_mnemonic_mac
,
3606 "Mnemonic character indicating MAC-like end-of-line format (i.e. CR).");
3607 eol_mnemonic_mac
= '\'';
3609 DEFVAR_INT ("eol-mnemonic-undecided", &eol_mnemonic_undecided
,
3610 "Mnemonic character indicating end-of-line format is not yet decided.");
3611 eol_mnemonic_undecided
= '-';
3613 DEFVAR_LISP ("alternate-charset-table", &Valternate_charset_table
,
3614 "Alist of charsets vs the alternate charsets.\n\
3615 While decoding, if a charset (car part of an element) is found,\n\
3616 decode it as the alternate charset (cdr part of the element).");
3617 Valternate_charset_table
= Qnil
;
3619 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_alist
,
3620 "Alist of charsets vs revision numbers.\n\
3621 While encoding, if a charset (car part of an element) is found,\n\
3622 designate it with the escape sequence identifing revision (cdr part of the element).");
3623 Vcharset_revision_alist
= Qnil
;