1 /* Coding system handler (conversion, detection, and etc).
4 Copyright (C) 1995 Free Software Foundation, Inc.
5 Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /*** TABLE OF CONTENTS ***
24 2. Emacs' internal format handlers
26 4. Shift-JIS and BIG5 handlers
27 5. End-of-line handlers
28 6. C library functions
29 7. Emacs Lisp library functions
34 /*** GENERAL NOTE on CODING SYSTEM ***
36 Coding system is an encoding mechanism of one or more character
37 sets. Here's a list of coding systems which Emacs can handle. When
38 we say "decode", it means converting some other coding system to
39 Emacs' internal format, and when we say "encode", it means
40 converting Emacs' internal format to some other coding system.
42 0. Emacs' internal format
44 Emacs itself holds a multi-lingual character in a buffer and a string
45 in a special format. Details are described in the section 2.
49 The most famous coding system for multiple character sets. X's
50 Compound Text, various EUCs (Extended Unix Code), and such coding
51 systems used in Internet communication as ISO-2022-JP are all
52 variants of ISO2022. Details are described in the section 3.
54 2. SJIS (or Shift-JIS or MS-Kanji-Code)
56 A coding system to encode character sets: ASCII, JISX0201, and
57 JISX0208. Widely used for PC's in Japan. Details are described in
62 A coding system to encode character sets: ASCII and Big5. Widely
63 used by Chinese (mainly in Taiwan and Hong Kong). Details are
64 described in the section 4. In this file, when written as "BIG5"
65 (all uppercase), it means the coding system, and when written as
66 "Big5" (capitalized), it means the character set.
70 If a user want to read/write a text encoded in a coding system not
71 listed above, he can supply a decoder and an encoder for it in CCL
72 (Code Conversion Language) programs. Emacs executes the CCL program
73 while reading/writing.
75 Emacs represent a coding-system by a Lisp symbol that has a property
76 `coding-system'. But, before actually using the coding-system, the
77 information about it is set in a structure of type `struct
78 coding_system' for rapid processing. See the section 6 for more
83 /*** GENERAL NOTES on END-OF-LINE FORMAT ***
85 How end-of-line of a text is encoded depends on a system. For
86 instance, Unix's format is just one byte of `line-feed' code,
87 whereas DOS's format is two bytes sequence of `carriage-return' and
88 `line-feed' codes. MacOS's format is one byte of `carriage-return'.
90 Since how characters in a text is encoded and how end-of-line is
91 encoded is independent, any coding system described above can take
92 any format of end-of-line. So, Emacs has information of format of
93 end-of-line in each coding-system. See the section 6 for more
98 /*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
100 These functions check if a text between SRC and SRC_END is encoded
101 in the coding system category XXX. Each returns an integer value in
102 which appropriate flag bits for the category XXX is set. The flag
103 bits are defined in macros CODING_CATEGORY_MASK_XXX. Below is the
104 template of these functions. */
107 detect_coding_internal (src
, src_end
)
108 unsigned char *src
, *src_end
;
114 /*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
116 These functions decode SRC_BYTES length text at SOURCE encoded in
117 CODING to Emacs' internal format. The resulting text goes to a
118 place pointed by DESTINATION, the length of which should not exceed
119 DST_BYTES. The bytes actually processed is returned as *CONSUMED.
120 The return value is the length of the decoded text. Below is a
121 template of these functions. */
123 decode_coding_XXX (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
124 struct coding_system
*coding
;
125 unsigned char *source
, *destination
;
126 int src_bytes
, dst_bytes
;
133 /*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
135 These functions encode SRC_BYTES length text at SOURCE of Emacs
136 internal format to CODING. The resulting text goes to a place
137 pointed by DESTINATION, the length of which should not exceed
138 DST_BYTES. The bytes actually processed is returned as *CONSUMED.
139 The return value is the length of the encoded text. Below is a
140 template of these functions. */
142 encode_coding_XXX (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
143 struct coding_system
*coding
;
144 unsigned char *source
, *destination
;
145 int src_bytes
, dst_bytes
;
152 /*** COMMONLY USED MACROS ***/
154 /* The following three macros ONE_MORE_BYTE, TWO_MORE_BYTES, and
155 THREE_MORE_BYTES safely get one, two, and three bytes from the
156 source text respectively. If there are not enough bytes in the
157 source, they jump to `label_end_of_loop'. The caller should set
158 variables `src' and `src_end' to appropriate areas in advance. */
160 #define ONE_MORE_BYTE(c1) \
165 goto label_end_of_loop; \
168 #define TWO_MORE_BYTES(c1, c2) \
170 if (src + 1 < src_end) \
171 c1 = *src++, c2 = *src++; \
173 goto label_end_of_loop; \
176 #define THREE_MORE_BYTES(c1, c2, c3) \
178 if (src + 2 < src_end) \
179 c1 = *src++, c2 = *src++, c3 = *src++; \
181 goto label_end_of_loop; \
184 /* The following three macros DECODE_CHARACTER_ASCII,
185 DECODE_CHARACTER_DIMENSION1, and DECODE_CHARACTER_DIMENSION2 put
186 the multi-byte form of a character of each class at the place
187 pointed by `dst'. The caller should set the variable `dst' to
188 point to an appropriate area and the variable `coding' to point to
189 the coding-system of the currently decoding text in advance. */
191 /* Decode one ASCII character C. */
193 #define DECODE_CHARACTER_ASCII(c) \
195 if (COMPOSING_P (coding->composing)) \
196 *dst++ = 0xA0, *dst++ = (c) | 0x80; \
201 /* Decode one DIMENSION1 character of which charset is CHARSET and
202 position-code is C. */
204 #define DECODE_CHARACTER_DIMENSION1(charset, c) \
206 unsigned char leading_code = CHARSET_LEADING_CODE_BASE (charset); \
207 if (COMPOSING_P (coding->composing)) \
208 *dst++ = leading_code + 0x20; \
210 *dst++ = leading_code; \
211 if (leading_code = CHARSET_LEADING_CODE_EXT (charset)) \
212 *dst++ = leading_code; \
213 *dst++ = (c) | 0x80; \
216 /* Decode one DIMENSION2 character of which charset is CHARSET and
217 position-codes are C1 and C2. */
219 #define DECODE_CHARACTER_DIMENSION2(charset, c1, c2) \
221 DECODE_CHARACTER_DIMENSION1 (charset, c1); \
222 *dst++ = (c2) | 0x80; \
226 /*** 1. Preamble ***/
240 #else /* not emacs */
244 #endif /* not emacs */
246 Lisp_Object Qcoding_system
, Qeol_type
;
247 Lisp_Object Qbuffer_file_coding_system
;
248 Lisp_Object Qpost_read_conversion
, Qpre_write_conversion
;
250 extern Lisp_Object Qinsert_file_contents
, Qwrite_region
;
251 Lisp_Object Qcall_process
, Qcall_process_region
, Qprocess_argument
;
252 Lisp_Object Qstart_process
, Qopen_network_stream
;
253 Lisp_Object Qtarget_idx
;
255 /* Mnemonic character of each format of end-of-line. */
256 int eol_mnemonic_unix
, eol_mnemonic_dos
, eol_mnemonic_mac
;
257 /* Mnemonic character to indicate format of end-of-line is not yet
259 int eol_mnemonic_undecided
;
263 Lisp_Object Qcoding_system_vector
, Qcoding_system_p
, Qcoding_system_error
;
265 /* Coding-systems are handed between Emacs Lisp programs and C internal
266 routines by the following three variables. */
267 /* Coding-system for reading files and receiving data from process. */
268 Lisp_Object Vcoding_system_for_read
;
269 /* Coding-system for writing files and sending data to process. */
270 Lisp_Object Vcoding_system_for_write
;
271 /* Coding-system actually used in the latest I/O. */
272 Lisp_Object Vlast_coding_system_used
;
274 /* Coding-system of what terminal accept for displaying. */
275 struct coding_system terminal_coding
;
277 /* Coding-system of what is sent from terminal keyboard. */
278 struct coding_system keyboard_coding
;
280 Lisp_Object Vcoding_system_alist
;
284 Lisp_Object Qcoding_category_index
;
286 /* List of symbols `coding-category-xxx' ordered by priority. */
287 Lisp_Object Vcoding_category_list
;
289 /* Table of coding-systems currently assigned to each coding-category. */
290 Lisp_Object coding_category_table
[CODING_CATEGORY_IDX_MAX
];
292 /* Table of names of symbol for each coding-category. */
293 char *coding_category_name
[CODING_CATEGORY_IDX_MAX
] = {
294 "coding-category-internal",
295 "coding-category-sjis",
296 "coding-category-iso-7",
297 "coding-category-iso-8-1",
298 "coding-category-iso-8-2",
299 "coding-category-iso-else",
300 "coding-category-big5",
301 "coding-category-binary"
304 /* Alist of charsets vs the alternate charsets. */
305 Lisp_Object Valternate_charset_table
;
307 /* Alist of charsets vs revision number. */
308 Lisp_Object Vcharset_revision_alist
;
311 /*** 2. Emacs internal format handlers ***/
313 /* Emacs' internal format for encoding multiple character sets is a
314 kind of multi-byte encoding, i.e. encoding a character by a sequence
315 of one-byte codes of variable length. ASCII characters and control
316 characters (e.g. `tab', `newline') are represented by one-byte as
317 is. It takes the range 0x00 through 0x7F. The other characters
318 are represented by a sequence of `base leading-code', optional
319 `extended leading-code', and one or two `position-code's. Length
320 of the sequence is decided by the base leading-code. Leading-code
321 takes the range 0x80 through 0x9F, whereas extended leading-code
322 and position-code take the range 0xA0 through 0xFF. See the
323 document of `charset.h' for more detail about leading-code and
326 There's one exception in this rule. Special leading-code
327 `leading-code-composition' denotes that the following several
328 characters should be composed into one character. Leading-codes of
329 components (except for ASCII) are added 0x20. An ASCII character
330 component is represented by a 2-byte sequence of `0xA0' and
331 `ASCII-code + 0x80'. See also the document in `charset.h' for the
332 detail of composite character. Hence, we can summarize the code
335 --- CODE RANGE of Emacs' internal format ---
336 (character set) (range)
338 ELSE (1st byte) 0x80 .. 0x9F
339 (rest bytes) 0xA0 .. 0xFF
340 ---------------------------------------------
344 enum emacs_code_class_type emacs_code_class
[256];
346 /* Go to the next statement only if *SRC is accessible and the code is
347 greater than 0xA0. */
348 #define CHECK_CODE_RANGE_A0_FF \
350 if (src >= src_end) \
351 goto label_end_of_switch; \
352 else if (*src++ < 0xA0) \
356 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
357 Check if a text is encoded in Emacs' internal format. If it is,
358 return CODING_CATEGORY_MASK_INTERNAL, else return 0. */
361 detect_coding_internal (src
, src_end
)
362 unsigned char *src
, *src_end
;
367 while (src
< src_end
)
379 switch (emacs_code_class
[c
])
381 case EMACS_ascii_code
:
382 case EMACS_linefeed_code
:
385 case EMACS_control_code
:
386 if (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
390 case EMACS_invalid_code
:
393 case EMACS_leading_code_composition
: /* c == 0x80 */
395 CHECK_CODE_RANGE_A0_FF
;
400 case EMACS_leading_code_4
:
401 CHECK_CODE_RANGE_A0_FF
;
402 /* fall down to check it two more times ... */
404 case EMACS_leading_code_3
:
405 CHECK_CODE_RANGE_A0_FF
;
406 /* fall down to check it one more time ... */
408 case EMACS_leading_code_2
:
409 CHECK_CODE_RANGE_A0_FF
;
417 return CODING_CATEGORY_MASK_INTERNAL
;
421 /*** 3. ISO2022 handlers ***/
423 /* The following note describes the coding system ISO2022 briefly.
424 Since the intension of this note is to help understanding of the
425 programs in this file, some parts are NOT ACCURATE or OVERLY
426 SIMPLIFIED. For the thorough understanding, please refer to the
427 original document of ISO2022.
429 ISO2022 provides many mechanisms to encode several character sets
430 in 7-bit and 8-bit environment. If one choose 7-bite environment,
431 all text is encoded by codes of less than 128. This may make the
432 encoded text a little bit longer, but the text get more stability
433 to pass through several gateways (some of them split MSB off).
435 There are two kind of character set: control character set and
436 graphic character set. The former contains control characters such
437 as `newline' and `escape' to provide control functions (control
438 functions are provided also by escape sequence). The latter
439 contains graphic characters such as ' A' and '-'. Emacs recognizes
440 two control character sets and many graphic character sets.
442 Graphic character sets are classified into one of the following
443 four classes, DIMENSION1_CHARS94, DIMENSION1_CHARS96,
444 DIMENSION2_CHARS94, DIMENSION2_CHARS96 according to the number of
445 bytes (DIMENSION) and the number of characters in one dimension
446 (CHARS) of the set. In addition, each character set is assigned an
447 identification tag (called "final character" and denoted as <F>
448 here after) which is unique in each class. <F> of each character
449 set is decided by ECMA(*) when it is registered in ISO. Code range
450 of <F> is 0x30..0x7F (0x30..0x3F are for private use only).
452 Note (*): ECMA = European Computer Manufacturers Association
454 Here are examples of graphic character set [NAME(<F>)]:
455 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
456 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
457 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
458 o DIMENSION2_CHARS96 -- none for the moment
460 A code area (1byte=8bits) is divided into 4 areas, C0, GL, C1, and GR.
461 C0 [0x00..0x1F] -- control character plane 0
462 GL [0x20..0x7F] -- graphic character plane 0
463 C1 [0x80..0x9F] -- control character plane 1
464 GR [0xA0..0xFF] -- graphic character plane 1
466 A control character set is directly designated and invoked to C0 or
467 C1 by an escape sequence. The most common case is that ISO646's
468 control character set is designated/invoked to C0 and ISO6429's
469 control character set is designated/invoked to C1, and usually
470 these designations/invocations are omitted in a coded text. With
471 7-bit environment, only C0 can be used, and a control character for
472 C1 is encoded by an appropriate escape sequence to fit in the
473 environment. All control characters for C1 are defined the
474 corresponding escape sequences.
476 A graphic character set is at first designated to one of four
477 graphic registers (G0 through G3), then these graphic registers are
478 invoked to GL or GR. These designations and invocations can be
479 done independently. The most common case is that G0 is invoked to
480 GL, G1 is invoked to GR, and ASCII is designated to G0, and usually
481 these invocations and designations are omitted in a coded text.
482 With 7-bit environment, only GL can be used.
484 When a graphic character set of CHARS94 is invoked to GL, code 0x20
485 and 0x7F of GL area work as control characters SPACE and DEL
486 respectively, and code 0xA0 and 0xFF of GR area should not be used.
488 There are two ways of invocation: locking-shift and single-shift.
489 With locking-shift, the invocation lasts until the next different
490 invocation, whereas with single-shift, the invocation works only
491 for the following character and doesn't affect locking-shift.
492 Invocations are done by the following control characters or escape
495 ----------------------------------------------------------------------
496 function control char escape sequence description
497 ----------------------------------------------------------------------
498 SI (shift-in) 0x0F none invoke G0 to GL
499 SI (shift-out) 0x0E none invoke G1 to GL
500 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
501 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
502 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 into GL
503 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 into GL
504 ----------------------------------------------------------------------
505 The first four are for locking-shift. Control characters for these
506 functions are defined by macros ISO_CODE_XXX in `coding.h'.
508 Designations are done by the following escape sequences.
509 ----------------------------------------------------------------------
510 escape sequence description
511 ----------------------------------------------------------------------
512 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
513 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
514 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
515 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
516 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
517 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
518 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
519 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
520 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
521 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
522 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
523 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
524 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
525 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
526 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
527 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
528 ----------------------------------------------------------------------
530 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
531 of dimension 1, chars 94, and final character <F>, and etc.
533 Note (*): Although these designations are not allowed in ISO2022,
534 Emacs accepts them on decoding, and produces them on encoding
535 CHARS96 character set in a coding system which is characterized as
536 7-bit environment, non-locking-shift, and non-single-shift.
538 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
539 '(' can be omitted. We call this as "short-form" here after.
541 Now you may notice that there are a lot of ways for encoding the
542 same multilingual text in ISO2022. Actually, there exist many
543 coding systems such as Compound Text (used in X's inter client
544 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
545 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
546 localized platforms), and all of these are variants of ISO2022.
548 In addition to the above, Emacs handles two more kinds of escape
549 sequences: ISO6429's direction specification and Emacs' private
550 sequence for specifying character composition.
552 ISO6429's direction specification takes the following format:
553 o CSI ']' -- end of the current direction
554 o CSI '0' ']' -- end of the current direction
555 o CSI '1' ']' -- start of left-to-right text
556 o CSI '2' ']' -- start of right-to-left text
557 The control character CSI (0x9B: control sequence introducer) is
558 abbreviated to the escape sequence ESC '[' in 7-bit environment.
560 Character composition specification takes the following format:
561 o ESC '0' -- start character composition
562 o ESC '1' -- end character composition
563 Since these are not standard escape sequences of any ISO, the use
564 of them for these meaning is restricted to Emacs only. */
566 enum iso_code_class_type iso_code_class
[256];
568 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
569 Check if a text is encoded in ISO2022. If it is, returns an
570 integer in which appropriate flag bits any of:
571 CODING_CATEGORY_MASK_ISO_7
572 CODING_CATEGORY_MASK_ISO_8_1
573 CODING_CATEGORY_MASK_ISO_8_2
574 CODING_CATEGORY_MASK_ISO_ELSE
575 are set. If a code which should never appear in ISO2022 is found,
579 detect_coding_iso2022 (src
, src_end
)
580 unsigned char *src
, *src_end
;
582 unsigned char graphic_register
[4];
583 unsigned char c
, esc_cntl
;
584 int mask
= (CODING_CATEGORY_MASK_ISO_7
585 | CODING_CATEGORY_MASK_ISO_8_1
586 | CODING_CATEGORY_MASK_ISO_8_2
);
587 /* We may look ahead maximum 3 bytes. */
588 unsigned char *adjusted_src_end
= src_end
- 3;
591 for (i
= 0; i
< 4; i
++)
592 graphic_register
[i
] = CHARSET_ASCII
;
594 while (src
< adjusted_src_end
)
600 if (src
>= adjusted_src_end
)
605 /* Designation of 2-byte character set. */
606 if (src
>= adjusted_src_end
)
610 if ((c
>= ')' && c
<= '+') || (c
>= '-' && c
<= '/'))
611 /* Designation to graphic register 1, 2, or 3. */
612 mask
&= ~CODING_CATEGORY_MASK_ISO_7
;
613 else if (c
== 'N' || c
== 'O' || c
== 'n' || c
== 'o')
614 return CODING_CATEGORY_MASK_ISO_ELSE
;
619 return CODING_CATEGORY_MASK_ISO_ELSE
;
624 mask
&= ~CODING_CATEGORY_MASK_ISO_7
;
636 mask
&= ~CODING_CATEGORY_MASK_ISO_7
;
637 while (src
< adjusted_src_end
&& *src
>= 0xA0)
639 if (count
& 1 && src
< adjusted_src_end
)
640 mask
&= ~CODING_CATEGORY_MASK_ISO_8_2
;
649 /* Decode a character of which charset is CHARSET and the 1st position
650 code is C1. If dimension of CHARSET 2, the 2nd position code is
651 fetched from SRC and set to C2. If CHARSET is negative, it means
652 that we are decoding ill formed text, and what we can do is just to
655 #define DECODE_ISO_CHARACTER(charset, c1) \
657 if ((charset) >= 0 && CHARSET_DIMENSION (charset) == 2) \
658 ONE_MORE_BYTE (c2); \
659 if (COMPOSING_HEAD_P (coding->composing)) \
661 *dst++ = LEADING_CODE_COMPOSITION; \
662 if (COMPOSING_WITH_RULE_P (coding->composing)) \
663 /* To tell composition rules are embeded. */ \
665 coding->composing += 2; \
669 else if ((charset) == CHARSET_ASCII) \
670 DECODE_CHARACTER_ASCII (c1); \
671 else if (CHARSET_DIMENSION (charset) == 1) \
672 DECODE_CHARACTER_DIMENSION1 (charset, c1); \
674 DECODE_CHARACTER_DIMENSION2 (charset, c1, c2); \
675 if (COMPOSING_WITH_RULE_P (coding->composing)) \
676 /* To tell a composition rule follows. */ \
677 coding->composing = COMPOSING_WITH_RULE_RULE; \
680 /* Set designation state into CODING. */
681 #define DECODE_DESIGNATION(reg, dimension, chars, final_char) \
683 int charset = ISO_CHARSET_TABLE (dimension, chars, final_char); \
685 = Fassq (CHARSET_SYMBOL (charset), Valternate_charset_table); \
687 charset = get_charset_id (XCONS (temp)->cdr); \
690 if (coding->direction == 1 \
691 && CHARSET_REVERSE_CHARSET (charset) >= 0) \
692 charset = CHARSET_REVERSE_CHARSET (charset); \
693 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
697 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
700 decode_coding_iso2022 (coding
, source
, destination
,
701 src_bytes
, dst_bytes
, consumed
)
702 struct coding_system
*coding
;
703 unsigned char *source
, *destination
;
704 int src_bytes
, dst_bytes
;
707 unsigned char *src
= source
;
708 unsigned char *src_end
= source
+ src_bytes
;
709 unsigned char *dst
= destination
;
710 unsigned char *dst_end
= destination
+ dst_bytes
;
711 /* Since the maximum bytes produced by each loop is 7, we subtract 6
712 from DST_END to assure that overflow checking is necessary only
713 at the head of loop. */
714 unsigned char *adjusted_dst_end
= dst_end
- 6;
716 /* Charsets invoked to graphic plane 0 and 1 respectively. */
717 int charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
718 int charset1
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 1);
720 while (src
< src_end
&& dst
< adjusted_dst_end
)
722 /* SRC_BASE remembers the start position in source in each loop.
723 The loop will be exited when there's not enough source text
724 to analyze long escape sequence or 2-byte code (within macros
725 ONE_MORE_BYTE or TWO_MORE_BYTES). In that case, SRC is reset
726 to SRC_BASE before exiting. */
727 unsigned char *src_base
= src
;
728 unsigned char c1
= *src
++, c2
, cmprule
;
730 switch (iso_code_class
[c1
])
732 case ISO_0x20_or_0x7F
:
733 if (!coding
->composing
734 && (charset0
< 0 || CHARSET_CHARS (charset0
) == 94))
736 /* This is SPACE or DEL. */
740 /* This is a graphic character, we fall down ... */
742 case ISO_graphic_plane_0
:
743 if (coding
->composing
== COMPOSING_WITH_RULE_RULE
)
745 /* This is a composition rule. */
747 coding
->composing
= COMPOSING_WITH_RULE_TAIL
;
750 DECODE_ISO_CHARACTER (charset0
, c1
);
753 case ISO_0xA0_or_0xFF
:
754 if (charset1
< 0 || CHARSET_CHARS (charset1
) == 94)
760 /* This is a graphic character, we fall down ... */
762 case ISO_graphic_plane_1
:
763 DECODE_ISO_CHARACTER (charset1
, c1
);
766 case ISO_control_code
:
767 /* All ISO2022 control characters in this class have the
768 same representation in Emacs internal format. */
772 case ISO_carriage_return
:
773 if (coding
->eol_type
== CODING_EOL_CR
)
777 else if (coding
->eol_type
== CODING_EOL_CRLF
)
780 if (c1
== ISO_CODE_LF
)
795 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 1;
796 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
800 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 0;
801 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
804 case ISO_single_shift_2_7
:
805 case ISO_single_shift_2
:
806 /* SS2 is handled as an escape sequence of ESC 'N' */
808 goto label_escape_sequence
;
810 case ISO_single_shift_3
:
811 /* SS2 is handled as an escape sequence of ESC 'O' */
813 goto label_escape_sequence
;
815 case ISO_control_sequence_introducer
:
816 /* CSI is handled as an escape sequence of ESC '[' ... */
818 goto label_escape_sequence
;
822 label_escape_sequence
:
823 /* Escape sequences handled by Emacs are invocation,
824 designation, direction specification, and character
825 composition specification. */
828 case '&': /* revision of following character set */
830 if (!(c1
>= '@' && c1
<= '~'))
832 goto label_invalid_escape_sequence
;
835 if (c1
!= ISO_CODE_ESC
)
837 goto label_invalid_escape_sequence
;
840 goto label_escape_sequence
;
842 case '$': /* designation of 2-byte character set */
844 if (c1
>= '@' && c1
<= 'B')
845 { /* designation of JISX0208.1978, GB2312.1980,
847 DECODE_DESIGNATION (0, 2, 94, c1
);
849 else if (c1
>= 0x28 && c1
<= 0x2B)
850 { /* designation of DIMENSION2_CHARS94 character set */
852 DECODE_DESIGNATION (c1
- 0x28, 2, 94, c2
);
854 else if (c1
>= 0x2C && c1
<= 0x2F)
855 { /* designation of DIMENSION2_CHARS96 character set */
857 DECODE_DESIGNATION (c1
- 0x2C, 2, 96, c2
);
861 goto label_invalid_escape_sequence
;
865 case 'n': /* invocation of locking-shift-2 */
866 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 2;
869 case 'o': /* invocation of locking-shift-3 */
870 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 3;
873 case 'N': /* invocation of single-shift-2 */
875 charset
= CODING_SPEC_ISO_DESIGNATION (coding
, 2);
876 DECODE_ISO_CHARACTER (charset
, c1
);
879 case 'O': /* invocation of single-shift-3 */
881 charset
= CODING_SPEC_ISO_DESIGNATION (coding
, 3);
882 DECODE_ISO_CHARACTER (charset
, c1
);
885 case '0': /* start composing without embeded rules */
886 coding
->composing
= COMPOSING_NO_RULE_HEAD
;
889 case '1': /* end composing */
890 coding
->composing
= COMPOSING_NO
;
893 case '2': /* start composing with embeded rules */
894 coding
->composing
= COMPOSING_WITH_RULE_HEAD
;
897 case '[': /* specification of direction */
898 /* For the moment, nested direction is not supported.
899 So, the value of `coding->direction' is 0 or 1: 0
900 means left-to-right, 1 means right-to-left. */
904 case ']': /* end of the current direction */
905 coding
->direction
= 0;
907 case '0': /* end of the current direction */
908 case '1': /* start of left-to-right direction */
911 coding
->direction
= 0;
913 goto label_invalid_escape_sequence
;
916 case '2': /* start of right-to-left direction */
919 coding
->direction
= 1;
921 goto label_invalid_escape_sequence
;
925 goto label_invalid_escape_sequence
;
930 if (c1
>= 0x28 && c1
<= 0x2B)
931 { /* designation of DIMENSION1_CHARS94 character set */
933 DECODE_DESIGNATION (c1
- 0x28, 1, 94, c2
);
935 else if (c1
>= 0x2C && c1
<= 0x2F)
936 { /* designation of DIMENSION1_CHARS96 character set */
938 DECODE_DESIGNATION (c1
- 0x2C, 1, 96, c2
);
942 goto label_invalid_escape_sequence
;
945 /* We must update these variables now. */
946 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
947 charset1
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 1);
950 label_invalid_escape_sequence
:
952 int length
= src
- src_base
;
954 bcopy (src_base
, dst
, length
);
961 coding
->carryover_size
= src
- src_base
;
962 bcopy (src_base
, coding
->carryover
, coding
->carryover_size
);
967 /* If this is the last block of the text to be decoded, we had
968 better just flush out all remaining codes in the text although
969 they are not valid characters. */
970 if (coding
->last_block
)
972 bcopy (src
, dst
, src_end
- src
);
973 dst
+= (src_end
- src
);
976 *consumed
= src
- source
;
977 return dst
- destination
;
980 /* ISO2022 encoding staffs. */
983 It is not enough to say just "ISO2022" on encoding, but we have to
984 specify more details. In Emacs, each coding-system of ISO2022
985 variant has the following specifications:
986 1. Initial designation to G0 thru G3.
987 2. Allows short-form designation?
988 3. ASCII should be designated to G0 before control characters?
989 4. ASCII should be designated to G0 at end of line?
990 5. 7-bit environment or 8-bit environment?
991 6. Use locking-shift?
993 And the following two are only for Japanese:
994 8. Use ASCII in place of JIS0201-1976-Roman?
995 9. Use JISX0208-1983 in place of JISX0208-1978?
996 These specifications are encoded in `coding->flags' as flag bits
997 defined by macros CODING_FLAG_ISO_XXX. See `coding.h' for more
1001 /* Produce codes (escape sequence) for designating CHARSET to graphic
1002 register REG. If <final-char> of CHARSET is '@', 'A', or 'B' and
1003 the coding system CODING allows, produce designation sequence of
1006 #define ENCODE_DESIGNATION(charset, reg, coding) \
1008 unsigned char final_char = CHARSET_ISO_FINAL_CHAR (charset); \
1009 char *intermediate_char_94 = "()*+"; \
1010 char *intermediate_char_96 = ",-./"; \
1012 = Fassq (make_number (charset), Vcharset_revision_alist); \
1013 if (! NILP (temp)) \
1015 *dst++ = ISO_CODE_ESC; \
1017 *dst++ = XINT (XCONS (temp)->cdr) + '@'; \
1019 *dst++ = ISO_CODE_ESC; \
1020 if (CHARSET_DIMENSION (charset) == 1) \
1022 if (CHARSET_CHARS (charset) == 94) \
1023 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
1025 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
1030 if (CHARSET_CHARS (charset) == 94) \
1032 if (! (coding->flags & CODING_FLAG_ISO_SHORT_FORM) \
1034 || final_char < '@' || final_char > 'B') \
1035 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
1038 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
1040 *dst++ = final_char; \
1041 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
1044 /* The following two macros produce codes (control character or escape
1045 sequence) for ISO2022 single-shift functions (single-shift-2 and
1048 #define ENCODE_SINGLE_SHIFT_2 \
1050 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1051 *dst++ = ISO_CODE_ESC, *dst++ = 'N'; \
1053 *dst++ = ISO_CODE_SS2; \
1054 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
1057 #define ENCODE_SINGLE_SHIFT_3 \
1059 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1060 *dst++ = ISO_CODE_ESC, *dst++ = 'O'; \
1062 *dst++ = ISO_CODE_SS3; \
1063 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
1066 /* The following four macros produce codes (control character or
1067 escape sequence) for ISO2022 locking-shift functions (shift-in,
1068 shift-out, locking-shift-2, and locking-shift-3). */
1070 #define ENCODE_SHIFT_IN \
1072 *dst++ = ISO_CODE_SI; \
1073 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0; \
1076 #define ENCODE_SHIFT_OUT \
1078 *dst++ = ISO_CODE_SO; \
1079 CODING_SPEC_ISO_INVOCATION (coding, 0) = 1; \
1082 #define ENCODE_LOCKING_SHIFT_2 \
1084 *dst++ = ISO_CODE_ESC, *dst++ = 'n'; \
1085 CODING_SPEC_ISO_INVOCATION (coding, 0) = 2; \
1088 #define ENCODE_LOCKING_SHIFT_3 \
1090 *dst++ = ISO_CODE_ESC, *dst++ = 'o'; \
1091 CODING_SPEC_ISO_INVOCATION (coding, 0) = 3; \
1094 /* Produce codes for a DIMENSION1 character of which character set is
1095 CHARSET and position-code is C1. Designation and invocation
1096 sequences are also produced in advance if necessary. */
1099 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
1101 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
1103 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1104 *dst++ = c1 & 0x7F; \
1106 *dst++ = c1 | 0x80; \
1107 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
1110 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
1112 *dst++ = c1 & 0x7F; \
1115 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
1117 *dst++ = c1 | 0x80; \
1121 /* Since CHARSET is not yet invoked to any graphic planes, we \
1122 must invoke it, or, at first, designate it to some graphic \
1123 register. Then repeat the loop to actually produce the \
1125 dst = encode_invocation_designation (charset, coding, dst); \
1128 /* Produce codes for a DIMENSION2 character of which character set is
1129 CHARSET and position-codes are C1 and C2. Designation and
1130 invocation codes are also produced in advance if necessary. */
1132 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
1134 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
1136 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1137 *dst++ = c1 & 0x7F, *dst++ = c2 & 0x7F; \
1139 *dst++ = c1 | 0x80, *dst++ = c2 | 0x80; \
1140 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
1143 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
1145 *dst++ = c1 & 0x7F, *dst++= c2 & 0x7F; \
1148 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
1150 *dst++ = c1 | 0x80, *dst++= c2 | 0x80; \
1154 /* Since CHARSET is not yet invoked to any graphic planes, we \
1155 must invoke it, or, at first, designate it to some graphic \
1156 register. Then repeat the loop to actually produce the \
1158 dst = encode_invocation_designation (charset, coding, dst); \
1161 /* Produce designation and invocation codes at a place pointed by DST
1162 to use CHARSET. The element `spec.iso2022' of *CODING is updated.
1166 encode_invocation_designation (charset
, coding
, dst
)
1168 struct coding_system
*coding
;
1171 int reg
; /* graphic register number */
1173 /* At first, check designations. */
1174 for (reg
= 0; reg
< 4; reg
++)
1175 if (charset
== CODING_SPEC_ISO_DESIGNATION (coding
, reg
))
1180 /* CHARSET is not yet designated to any graphic registers. */
1181 /* At first check the requested designation. */
1182 reg
= CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
);
1184 /* Since CHARSET requests no special designation, designate to
1185 graphic register 0. */
1188 ENCODE_DESIGNATION (charset
, reg
, coding
);
1191 if (CODING_SPEC_ISO_INVOCATION (coding
, 0) != reg
1192 && CODING_SPEC_ISO_INVOCATION (coding
, 1) != reg
)
1194 /* Since the graphic register REG is not invoked to any graphic
1195 planes, invoke it to graphic plane 0. */
1198 case 0: /* graphic register 0 */
1202 case 1: /* graphic register 1 */
1206 case 2: /* graphic register 2 */
1207 if (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
1208 ENCODE_SINGLE_SHIFT_2
;
1210 ENCODE_LOCKING_SHIFT_2
;
1213 case 3: /* graphic register 3 */
1214 if (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
1215 ENCODE_SINGLE_SHIFT_3
;
1217 ENCODE_LOCKING_SHIFT_3
;
1224 /* The following two macros produce codes for indicating composition. */
1225 #define ENCODE_COMPOSITION_NO_RULE_START *dst++ = ISO_CODE_ESC, *dst++ = '0'
1226 #define ENCODE_COMPOSITION_WITH_RULE_START *dst++ = ISO_CODE_ESC, *dst++ = '2'
1227 #define ENCODE_COMPOSITION_END *dst++ = ISO_CODE_ESC, *dst++ = '1'
1229 /* The following three macros produce codes for indicating direction
1231 #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
1233 if (coding->flags == CODING_FLAG_ISO_SEVEN_BITS) \
1234 *dst++ = ISO_CODE_ESC, *dst++ = '['; \
1236 *dst++ = ISO_CODE_CSI; \
1239 #define ENCODE_DIRECTION_R2L \
1240 ENCODE_CONTROL_SEQUENCE_INTRODUCER, *dst++ = '2', *dst++ = ']'
1242 #define ENCODE_DIRECTION_L2R \
1243 ENCODE_CONTROL_SEQUENCE_INTRODUCER, *dst++ = '0', *dst++ = ']'
1245 /* Produce codes for designation and invocation to reset the graphic
1246 planes and registers to initial state. */
1247 #define ENCODE_RESET_PLANE_AND_REGISTER(eol) \
1250 if (CODING_SPEC_ISO_INVOCATION (coding, 0) != 0) \
1252 for (reg = 0; reg < 4; reg++) \
1254 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg) < 0) \
1256 if (eol) CODING_SPEC_ISO_DESIGNATION (coding, reg) = -1; \
1258 else if (CODING_SPEC_ISO_DESIGNATION (coding, reg) \
1259 != CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg)) \
1260 ENCODE_DESIGNATION \
1261 (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg), reg, coding); \
1265 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
1268 encode_coding_iso2022 (coding
, source
, destination
,
1269 src_bytes
, dst_bytes
, consumed
)
1270 struct coding_system
*coding
;
1271 unsigned char *source
, *destination
;
1272 int src_bytes
, dst_bytes
;
1275 unsigned char *src
= source
;
1276 unsigned char *src_end
= source
+ src_bytes
;
1277 unsigned char *dst
= destination
;
1278 unsigned char *dst_end
= destination
+ dst_bytes
;
1279 /* Since the maximum bytes produced by each loop is 6, we subtract 5
1280 from DST_END to assure overflow checking is necessary only at the
1282 unsigned char *adjusted_dst_end
= dst_end
- 5;
1284 while (src
< src_end
&& dst
< adjusted_dst_end
)
1286 /* SRC_BASE remembers the start position in source in each loop.
1287 The loop will be exited when there's not enough source text
1288 to analyze multi-byte codes (within macros ONE_MORE_BYTE,
1289 TWO_MORE_BYTES, and THREE_MORE_BYTES). In that case, SRC is
1290 reset to SRC_BASE before exiting. */
1291 unsigned char *src_base
= src
;
1292 unsigned char c1
= *src
++, c2
, c3
, c4
;
1295 /* If we are seeing a component of a composite character, we are
1296 seeing a leading-code specially encoded for composition, or a
1297 composition rule if composing with rule. We must set C1
1298 to a normal leading-code or an ASCII code. If we are not at
1299 a composed character, we must reset the composition state. */
1300 if (COMPOSING_P (coding
->composing
))
1304 /* We are not in a composite character any longer. */
1305 coding
->composing
= COMPOSING_NO
;
1306 ENCODE_COMPOSITION_END
;
1310 if (coding
->composing
== COMPOSING_WITH_RULE_RULE
)
1313 coding
->composing
= COMPOSING_WITH_RULE_HEAD
;
1316 else if (coding
->composing
== COMPOSING_WITH_RULE_HEAD
)
1317 coding
->composing
= COMPOSING_WITH_RULE_RULE
;
1320 /* This is an ASCII component. */
1325 /* This is a leading-code of non ASCII component. */
1330 /* Now encode one character. C1 is a control character, an
1331 ASCII character, or a leading-code of multi-byte character. */
1332 switch (emacs_code_class
[c1
])
1334 case EMACS_ascii_code
:
1335 ENCODE_ISO_CHARACTER_DIMENSION1 (CHARSET_ASCII
, c1
);
1338 case EMACS_control_code
:
1339 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_CNTL
)
1340 ENCODE_RESET_PLANE_AND_REGISTER (0);
1344 case EMACS_carriage_return_code
:
1345 if (!coding
->selective
)
1347 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_CNTL
)
1348 ENCODE_RESET_PLANE_AND_REGISTER (0);
1352 /* fall down to treat '\r' as '\n' ... */
1354 case EMACS_linefeed_code
:
1355 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_EOL
)
1356 ENCODE_RESET_PLANE_AND_REGISTER (1);
1357 if (coding
->eol_type
== CODING_EOL_LF
1358 || coding
->eol_type
== CODING_EOL_AUTOMATIC
)
1359 *dst
++ = ISO_CODE_LF
;
1360 else if (coding
->eol_type
== CODING_EOL_CRLF
)
1361 *dst
++ = ISO_CODE_CR
, *dst
++ = ISO_CODE_LF
;
1363 *dst
++ = ISO_CODE_CR
;
1366 case EMACS_leading_code_2
:
1368 ENCODE_ISO_CHARACTER_DIMENSION1 (c1
, c2
);
1371 case EMACS_leading_code_3
:
1372 TWO_MORE_BYTES (c2
, c3
);
1373 if (c1
< LEADING_CODE_PRIVATE_11
)
1374 ENCODE_ISO_CHARACTER_DIMENSION2 (c1
, c2
, c3
);
1376 ENCODE_ISO_CHARACTER_DIMENSION1 (c2
, c3
);
1379 case EMACS_leading_code_4
:
1380 THREE_MORE_BYTES (c2
, c3
, c4
);
1381 ENCODE_ISO_CHARACTER_DIMENSION2 (c2
, c3
, c4
);
1384 case EMACS_leading_code_composition
:
1388 coding
->composing
= COMPOSING_WITH_RULE_HEAD
;
1389 ENCODE_COMPOSITION_WITH_RULE_START
;
1393 /* Rewind one byte because it is a character code of
1394 composition elements. */
1396 coding
->composing
= COMPOSING_NO_RULE_HEAD
;
1397 ENCODE_COMPOSITION_NO_RULE_START
;
1401 case EMACS_invalid_code
:
1407 coding
->carryover_size
= src
- src_base
;
1408 bcopy (src_base
, coding
->carryover
, coding
->carryover_size
);
1413 /* If this is the last block of the text to be encoded, we must
1414 reset the state of graphic planes and registers to initial one.
1415 In addition, we had better just flush out all remaining codes in
1416 the text although they are not valid characters. */
1417 if (coding
->last_block
)
1419 ENCODE_RESET_PLANE_AND_REGISTER (1);
1420 bcopy(src
, dst
, src_end
- src
);
1421 dst
+= (src_end
- src
);
1424 *consumed
= src
- source
;
1425 return dst
- destination
;
1429 /*** 4. SJIS and BIG5 handlers ***/
1431 /* Although SJIS and BIG5 are not ISO's coding system, They are used
1432 quite widely. So, for the moment, Emacs supports them in the bare
1433 C code. But, in the future, they may be supported only by CCL. */
1435 /* SJIS is a coding system encoding three character sets: ASCII, right
1436 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
1437 as is. A character of charset katakana-jisx0201 is encoded by
1438 "position-code + 0x80". A character of charset japanese-jisx0208
1439 is encoded in 2-byte but two position-codes are divided and shifted
1440 so that it fit in the range below.
1442 --- CODE RANGE of SJIS ---
1443 (character set) (range)
1445 KATAKANA-JISX0201 0xA0 .. 0xDF
1446 JISX0208 (1st byte) 0x80 .. 0x9F and 0xE0 .. 0xFF
1447 (2nd byte) 0x40 .. 0xFF
1448 -------------------------------
1452 /* BIG5 is a coding system encoding two character sets: ASCII and
1453 Big5. An ASCII character is encoded as is. Big5 is a two-byte
1454 character set and is encoded in two-byte.
1456 --- CODE RANGE of BIG5 ---
1457 (character set) (range)
1459 Big5 (1st byte) 0xA1 .. 0xFE
1460 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
1461 --------------------------
1463 Since the number of characters in Big5 is larger than maximum
1464 characters in Emacs' charset (96x96), it can't be handled as one
1465 charset. So, in Emacs, Big5 is divided into two: `charset-big5-1'
1466 and `charset-big5-2'. Both are DIMENSION2 and CHARS94. The former
1467 contains frequently used characters and the latter contains less
1468 frequently used characters. */
1470 /* Macros to decode or encode a character of Big5 in BIG5. B1 and B2
1471 are the 1st and 2nd position-codes of Big5 in BIG5 coding system.
1472 C1 and C2 are the 1st and 2nd position-codes of of Emacs' internal
1473 format. CHARSET is `charset_big5_1' or `charset_big5_2'. */
1475 /* Number of Big5 characters which have the same code in 1st byte. */
1476 #define BIG5_SAME_ROW (0xFF - 0xA1 + 0x7F - 0x40)
1478 #define DECODE_BIG5(b1, b2, charset, c1, c2) \
1481 = (b1 - 0xA1) * BIG5_SAME_ROW + b2 - (b2 < 0x7F ? 0x40 : 0x62); \
1483 charset = charset_big5_1; \
1486 charset = charset_big5_2; \
1487 temp -= (0xC9 - 0xA1) * BIG5_SAME_ROW; \
1489 c1 = temp / (0xFF - 0xA1) + 0x21; \
1490 c2 = temp % (0xFF - 0xA1) + 0x21; \
1493 #define ENCODE_BIG5(charset, c1, c2, b1, b2) \
1495 unsigned int temp = (c1 - 0x21) * (0xFF - 0xA1) + (c2 - 0x21); \
1496 if (charset == charset_big5_2) \
1497 temp += BIG5_SAME_ROW * (0xC9 - 0xA1); \
1498 b1 = temp / BIG5_SAME_ROW + 0xA1; \
1499 b2 = temp % BIG5_SAME_ROW; \
1500 b2 += b2 < 0x3F ? 0x40 : 0x62; \
1503 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1504 Check if a text is encoded in SJIS. If it is, return
1505 CODING_CATEGORY_MASK_SJIS, else return 0. */
1508 detect_coding_sjis (src
, src_end
)
1509 unsigned char *src
, *src_end
;
1513 while (src
< src_end
)
1516 if (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
1518 if ((c
>= 0x80 && c
< 0xA0) || c
>= 0xE0)
1520 if (src
< src_end
&& *src
++ < 0x40)
1524 return CODING_CATEGORY_MASK_SJIS
;
1527 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1528 Check if a text is encoded in BIG5. If it is, return
1529 CODING_CATEGORY_MASK_BIG5, else return 0. */
1532 detect_coding_big5 (src
, src_end
)
1533 unsigned char *src
, *src_end
;
1537 while (src
< src_end
)
1540 if (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
1547 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
1551 return CODING_CATEGORY_MASK_BIG5
;
1554 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
1555 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
1558 decode_coding_sjis_big5 (coding
, source
, destination
,
1559 src_bytes
, dst_bytes
, consumed
, sjis_p
)
1560 struct coding_system
*coding
;
1561 unsigned char *source
, *destination
;
1562 int src_bytes
, dst_bytes
;
1566 unsigned char *src
= source
;
1567 unsigned char *src_end
= source
+ src_bytes
;
1568 unsigned char *dst
= destination
;
1569 unsigned char *dst_end
= destination
+ dst_bytes
;
1570 /* Since the maximum bytes produced by each loop is 4, we subtract 3
1571 from DST_END to assure overflow checking is necessary only at the
1573 unsigned char *adjusted_dst_end
= dst_end
- 3;
1575 while (src
< src_end
&& dst
< adjusted_dst_end
)
1577 /* SRC_BASE remembers the start position in source in each loop.
1578 The loop will be exited when there's not enough source text
1579 to analyze two-byte character (within macro ONE_MORE_BYTE).
1580 In that case, SRC is reset to SRC_BASE before exiting. */
1581 unsigned char *src_base
= src
;
1582 unsigned char c1
= *src
++, c2
, c3
, c4
;
1586 if (coding
->eol_type
== CODING_EOL_CRLF
)
1592 /* To process C2 again, SRC is subtracted by 1. */
1600 else if (c1
< 0xA0 || c1
>= 0xE0)
1602 /* SJIS -> JISX0208, BIG5 -> Big5 (only if 0xE0 <= c1 < 0xFF) */
1606 DECODE_SJIS (c1
, c2
, c3
, c4
);
1607 DECODE_CHARACTER_DIMENSION2 (charset_jisx0208
, c3
, c4
);
1609 else if (c1
>= 0xE0 && c1
< 0xFF)
1614 DECODE_BIG5 (c1
, c2
, charset
, c3
, c4
);
1615 DECODE_CHARACTER_DIMENSION2 (charset
, c3
, c4
);
1617 else /* Invalid code */
1622 /* SJIS -> JISX0201-Kana, BIG5 -> Big5 */
1624 DECODE_CHARACTER_DIMENSION1 (charset_katakana_jisx0201
, c1
);
1630 DECODE_BIG5 (c1
, c2
, charset
, c3
, c4
);
1631 DECODE_CHARACTER_DIMENSION2 (charset
, c3
, c4
);
1637 coding
->carryover_size
= src
- src_base
;
1638 bcopy (src_base
, coding
->carryover
, coding
->carryover_size
);
1643 *consumed
= src
- source
;
1644 return dst
- destination
;
1647 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
1648 This function can encode `charset_ascii', `charset_katakana_jisx0201',
1649 `charset_jisx0208', `charset_big5_1', and `charset_big5-2'. We are
1650 sure that all these charsets are registered as official charset
1651 (i.e. do not have extended leading-codes). Characters of other
1652 charsets are produced without any encoding. If SJIS_P is 1, encode
1653 SJIS text, else encode BIG5 text. */
1656 encode_coding_sjis_big5 (coding
, source
, destination
,
1657 src_bytes
, dst_bytes
, consumed
, sjis_p
)
1658 struct coding_system
*coding
;
1659 unsigned char *source
, *destination
;
1660 int src_bytes
, dst_bytes
;
1664 unsigned char *src
= source
;
1665 unsigned char *src_end
= source
+ src_bytes
;
1666 unsigned char *dst
= destination
;
1667 unsigned char *dst_end
= destination
+ dst_bytes
;
1668 /* Since the maximum bytes produced by each loop is 2, we subtract 1
1669 from DST_END to assure overflow checking is necessary only at the
1671 unsigned char *adjusted_dst_end
= dst_end
- 1;
1673 while (src
< src_end
&& dst
< adjusted_dst_end
)
1675 /* SRC_BASE remembers the start position in source in each loop.
1676 The loop will be exited when there's not enough source text
1677 to analyze multi-byte codes (within macros ONE_MORE_BYTE and
1678 TWO_MORE_BYTES). In that case, SRC is reset to SRC_BASE
1680 unsigned char *src_base
= src
;
1681 unsigned char c1
= *src
++, c2
, c3
, c4
;
1683 if (coding
->composing
)
1690 else if (c1
>= 0xA0)
1693 coding
->composing
= 0;
1696 switch (emacs_code_class
[c1
])
1698 case EMACS_ascii_code
:
1699 case EMACS_control_code
:
1703 case EMACS_carriage_return_code
:
1704 if (!coding
->selective
)
1709 /* fall down to treat '\r' as '\n' ... */
1711 case EMACS_linefeed_code
:
1712 if (coding
->eol_type
== CODING_EOL_LF
1713 || coding
->eol_type
== CODING_EOL_AUTOMATIC
)
1715 else if (coding
->eol_type
== CODING_EOL_CRLF
)
1716 *dst
++ = '\r', *dst
++ = '\n';
1721 case EMACS_leading_code_2
:
1723 if (sjis_p
&& c1
== charset_katakana_jisx0201
)
1726 *dst
++ = c1
, *dst
++ = c2
;
1729 case EMACS_leading_code_3
:
1730 TWO_MORE_BYTES (c2
, c3
);
1731 c2
&= 0x7F, c3
&= 0x7F;
1732 if (sjis_p
&& c1
== charset_jisx0208
)
1734 unsigned char s1
, s2
;
1736 ENCODE_SJIS (c2
, c3
, s1
, s2
);
1737 *dst
++ = s1
, *dst
++ = s2
;
1739 else if (!sjis_p
&& (c1
== charset_big5_1
|| c1
== charset_big5_2
))
1741 unsigned char b1
, b2
;
1743 ENCODE_BIG5 (c1
, c2
, c3
, b1
, b2
);
1744 *dst
++ = b1
, *dst
++ = b2
;
1747 *dst
++ = c1
, *dst
++ = c2
, *dst
++ = c3
;
1750 case EMACS_leading_code_4
:
1751 THREE_MORE_BYTES (c2
, c3
, c4
);
1752 *dst
++ = c1
, *dst
++ = c2
, *dst
++ = c3
, *dst
++ = c4
;
1755 case EMACS_leading_code_composition
:
1756 coding
->composing
= 1;
1759 default: /* i.e. case EMACS_invalid_code: */
1765 coding
->carryover_size
= src
- src_base
;
1766 bcopy (src_base
, coding
->carryover
, coding
->carryover_size
);
1771 *consumed
= src
- source
;
1772 return dst
- destination
;
1776 /*** 5. End-of-line handlers ***/
1778 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
1779 This function is called only when `coding->eol_type' is
1780 CODING_EOL_CRLF or CODING_EOL_CR. */
1782 decode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
1783 struct coding_system
*coding
;
1784 unsigned char *source
, *destination
;
1785 int src_bytes
, dst_bytes
;
1788 unsigned char *src
= source
;
1789 unsigned char *src_end
= source
+ src_bytes
;
1790 unsigned char *dst
= destination
;
1791 unsigned char *dst_end
= destination
+ dst_bytes
;
1794 switch (coding
->eol_type
)
1796 case CODING_EOL_CRLF
:
1798 /* Since the maximum bytes produced by each loop is 2, we
1799 subtract 1 from DST_END to assure overflow checking is
1800 necessary only at the head of loop. */
1801 unsigned char *adjusted_dst_end
= dst_end
- 1;
1803 while (src
< src_end
&& dst
< adjusted_dst_end
)
1805 unsigned char *src_base
= src
;
1806 unsigned char c
= *src
++;
1819 coding
->carryover_size
= src
- src_base
;
1820 bcopy (src_base
, coding
->carryover
, coding
->carryover_size
);
1824 *consumed
= src
- source
;
1825 produced
= dst
- destination
;
1830 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
1831 bcopy (source
, destination
, produced
);
1832 dst_end
= destination
+ produced
;
1833 while (dst
< dst_end
)
1834 if (*dst
++ == '\r') dst
[-1] = '\n';
1835 *consumed
= produced
;
1838 default: /* i.e. case: CODING_EOL_LF */
1839 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
1840 bcopy (source
, destination
, produced
);
1841 *consumed
= produced
;
1848 /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". Encode
1849 format of end-of-line according to `coding->eol_type'. If
1850 `coding->selective' is 1, code '\r' in source text also means
1853 encode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
1854 struct coding_system
*coding
;
1855 unsigned char *source
, *destination
;
1856 int src_bytes
, dst_bytes
;
1859 unsigned char *src
= source
;
1860 unsigned char *dst
= destination
;
1866 switch (coding
->eol_type
)
1869 case CODING_EOL_AUTOMATIC
:
1870 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
1871 bcopy (source
, destination
, produced
);
1872 if (coding
->selective
)
1876 if (*dst
++ == '\r') dst
[-1] = '\n';
1878 *consumed
= produced
;
1880 case CODING_EOL_CRLF
:
1883 unsigned char *src_end
= source
+ src_bytes
;
1884 unsigned char *dst_end
= destination
+ dst_bytes
;
1885 /* Since the maximum bytes produced by each loop is 2, we
1886 subtract 1 from DST_END to assure overflow checking is
1887 necessary only at the head of loop. */
1888 unsigned char *adjusted_dst_end
= dst_end
- 1;
1890 while (src
< src_end
&& dst
< adjusted_dst_end
)
1893 if (c
== '\n' || (c
== '\r' && coding
->selective
))
1894 *dst
++ = '\r', *dst
++ = '\n';
1898 produced
= dst
- destination
;
1899 *consumed
= src
- source
;
1903 default: /* i.e. case CODING_EOL_CR: */
1904 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
1905 bcopy (source
, destination
, produced
);
1909 if (*dst
++ == '\n') dst
[-1] = '\r';
1911 *consumed
= produced
;
1918 /*** 6. C library functions ***/
1920 /* In Emacs Lisp, coding system is represented by a Lisp symbol which
1921 has a property `coding-system'. The value of this property is a
1922 vector of length 5 (called as coding-vector). Among elements of
1923 this vector, the first (element[0]) and the fifth (element[4])
1924 carry important information for decoding/encoding. Before
1925 decoding/encoding, this information should be set in fields of a
1926 structure of type `coding_system'.
1928 A value of property `coding-system' can be a symbol of another
1929 subsidiary coding-system. In that case, Emacs gets coding-vector
1932 `element[0]' contains information to be set in `coding->type'. The
1933 value and its meaning is as follows:
1935 0 -- coding_system_internal
1936 1 -- coding_system_sjis
1937 2 -- coding_system_iso2022
1938 3 -- coding_system_big5
1939 4 -- coding_system_ccl
1940 nil -- coding_system_no_conversion
1941 t -- coding_system_automatic
1943 `element[4]' contains information to be set in `coding->flags' and
1944 `coding->spec'. The meaning varies by `coding->type'.
1946 If `coding->type' is `coding_type_iso2022', element[4] is a vector
1947 of length 32 (of which the first 13 sub-elements are used now).
1948 Meanings of these sub-elements are:
1950 sub-element[N] where N is 0 through 3: to be set in `coding->spec.iso2022'
1951 If the value is an integer of valid charset, the charset is
1952 assumed to be designated to graphic register N initially.
1954 If the value is minus, it is a minus value of charset which
1955 reserves graphic register N, which means that the charset is
1956 not designated initially but should be designated to graphic
1957 register N just before encoding a character in that charset.
1959 If the value is nil, graphic register N is never used on
1962 sub-element[N] where N is 4 through 11: to be set in `coding->flags'
1963 Each value takes t or nil. See the section ISO2022 of
1964 `coding.h' for more information.
1966 If `coding->type' is `coding_type_big5', element[4] is t to denote
1967 BIG5-ETen or nil to denote BIG5-HKU.
1969 If `coding->type' takes the other value, element[4] is ignored.
1971 Emacs Lisp's coding system also carries information about format of
1972 end-of-line in a value of property `eol-type'. If the value is
1973 integer, 0 means CODING_EOL_LF, 1 means CODING_EOL_CRLF, and 2
1974 means CODING_EOL_CR. If it is not integer, it should be a vector
1975 of subsidiary coding systems of which property `eol-type' has one
1980 /* Extract information for decoding/encoding from CODING_SYSTEM_SYMBOL
1981 and set it in CODING. If CODING_SYSTEM_SYMBOL is invalid, CODING
1982 is setup so that no conversion is necessary and return -1, else
1986 setup_coding_system (coding_system_symbol
, coding
)
1987 Lisp_Object coding_system_symbol
;
1988 struct coding_system
*coding
;
1990 Lisp_Object coding_system_vector
= Qnil
;
1991 Lisp_Object type
, eol_type
;
1993 /* At first, set several fields default values. */
1994 coding
->require_flushing
= 0;
1995 coding
->last_block
= 0;
1996 coding
->selective
= 0;
1997 coding
->composing
= 0;
1998 coding
->direction
= 0;
1999 coding
->carryover_size
= 0;
2000 coding
->symbol
= Qnil
;
2001 coding
->post_read_conversion
= coding
->pre_write_conversion
= Qnil
;
2003 /* Get value of property `coding-system'. If it is a Lisp symbol
2004 pointing another coding system, fetch its property until we get a
2006 while (!NILP (coding_system_symbol
))
2008 coding
->symbol
= coding_system_symbol
;
2009 if (NILP (coding
->post_read_conversion
))
2010 coding
->post_read_conversion
= Fget (coding_system_symbol
,
2011 Qpost_read_conversion
);
2012 if (NILP (coding
->pre_write_conversion
))
2013 coding
->pre_write_conversion
= Fget (coding_system_symbol
,
2014 Qpre_write_conversion
);
2016 coding_system_vector
= Fget (coding_system_symbol
, Qcoding_system
);
2017 if (VECTORP (coding_system_vector
))
2019 coding_system_symbol
= coding_system_vector
;
2021 Vlast_coding_system_used
= coding
->symbol
;
2023 if (!VECTORP (coding_system_vector
)
2024 || XVECTOR (coding_system_vector
)->size
!= 5)
2025 goto label_invalid_coding_system
;
2027 /* Get value of property `eol-type' by searching from the root
2029 coding_system_symbol
= coding
->symbol
;
2031 while (SYMBOLP (coding_system_symbol
) && !NILP (coding_system_symbol
))
2033 eol_type
= Fget (coding_system_symbol
, Qeol_type
);
2034 if (!NILP (eol_type
))
2036 coding_system_symbol
= Fget (coding_system_symbol
, Qcoding_system
);
2039 if (VECTORP (eol_type
))
2040 coding
->eol_type
= CODING_EOL_AUTOMATIC
;
2041 else if (XFASTINT (eol_type
) == 1)
2042 coding
->eol_type
= CODING_EOL_CRLF
;
2043 else if (XFASTINT (eol_type
) == 2)
2044 coding
->eol_type
= CODING_EOL_CR
;
2046 coding
->eol_type
= CODING_EOL_LF
;
2048 type
= XVECTOR (coding_system_vector
)->contents
[0];
2049 switch (XFASTINT (type
))
2052 coding
->type
= coding_type_internal
;
2056 coding
->type
= coding_type_sjis
;
2060 coding
->type
= coding_type_iso2022
;
2062 Lisp_Object val
= XVECTOR (coding_system_vector
)->contents
[4];
2064 int i
, charset
, default_reg_bits
= 0;
2066 if (!VECTORP (val
) || XVECTOR (val
)->size
!= 32)
2067 goto label_invalid_coding_system
;
2069 flags
= XVECTOR (val
)->contents
;
2071 = ((NILP (flags
[4]) ? 0 : CODING_FLAG_ISO_SHORT_FORM
)
2072 | (NILP (flags
[5]) ? 0 : CODING_FLAG_ISO_RESET_AT_EOL
)
2073 | (NILP (flags
[6]) ? 0 : CODING_FLAG_ISO_RESET_AT_CNTL
)
2074 | (NILP (flags
[7]) ? 0 : CODING_FLAG_ISO_SEVEN_BITS
)
2075 | (NILP (flags
[8]) ? 0 : CODING_FLAG_ISO_LOCKING_SHIFT
)
2076 | (NILP (flags
[9]) ? 0 : CODING_FLAG_ISO_SINGLE_SHIFT
)
2077 | (NILP (flags
[10]) ? 0 : CODING_FLAG_ISO_USE_ROMAN
)
2078 | (NILP (flags
[11]) ? 0 : CODING_FLAG_ISO_USE_OLDJIS
)
2079 | (NILP (flags
[12]) ? 0 : CODING_FLAG_ISO_NO_DIRECTION
));
2081 /* Invoke graphic register 0 to plane 0. */
2082 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 0;
2083 /* Invoke graphic register 1 to plane 1 if we can use full 8-bit. */
2084 CODING_SPEC_ISO_INVOCATION (coding
, 1)
2085 = (coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
? -1 : 1);
2086 /* Not single shifting at first. */
2087 CODING_SPEC_ISO_SINGLE_SHIFTING(coding
) = 0;
2089 /* Checks FLAGS[REG] (REG = 0, 1, 2 3) and decide designations.
2090 FLAGS[REG] can be one of below:
2091 integer CHARSET: CHARSET occupies register I,
2092 t: designate nothing to REG initially, but can be used
2094 list of integer, nil, or t: designate the first
2095 element (if integer) to REG initially, the remaining
2096 elements (if integer) is designated to REG on request,
2097 if an element is t, REG can be used by any charset,
2098 nil: REG is never used. */
2099 for (charset
= 0; charset
< MAX_CHARSET
; charset
++)
2100 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
) = -1;
2101 for (i
= 0; i
< 4; i
++)
2103 if (INTEGERP (flags
[i
])
2104 && (charset
= XINT (flags
[i
]), CHARSET_VALID_P (charset
)))
2106 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = charset
;
2107 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
) = i
;
2109 else if (EQ (flags
[i
], Qt
))
2111 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
2112 default_reg_bits
|= 1 << i
;
2114 else if (CONSP (flags
[i
]))
2116 Lisp_Object tail
= flags
[i
];
2118 if (INTEGERP (XCONS (tail
)->car
)
2119 && (charset
= XINT (XCONS (tail
)->car
),
2120 CHARSET_VALID_P (charset
)))
2122 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = charset
;
2123 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
) =i
;
2126 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
2127 tail
= XCONS (tail
)->cdr
;
2128 while (CONSP (tail
))
2130 if (INTEGERP (XCONS (tail
)->car
)
2131 && (charset
= XINT (XCONS (tail
)->car
),
2132 CHARSET_VALID_P (charset
)))
2133 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2135 else if (EQ (XCONS (tail
)->car
, Qt
))
2136 default_reg_bits
|= 1 << i
;
2137 tail
= XCONS (tail
)->cdr
;
2141 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
2143 CODING_SPEC_ISO_DESIGNATION (coding
, i
)
2144 = CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
);
2147 if (! (coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
))
2149 /* REG 1 can be used only by locking shift in 7-bit env. */
2150 if (coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
)
2151 default_reg_bits
&= ~2;
2152 if (! (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
))
2153 /* Without any shifting, only REG 0 and 1 can be used. */
2154 default_reg_bits
&= 3;
2157 for (charset
= 0; charset
< MAX_CHARSET
; charset
++)
2158 if (CHARSET_VALID_P (charset
)
2159 && CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
) < 0)
2161 /* We have not yet decided where to designate CHARSET. */
2162 int reg_bits
= default_reg_bits
;
2164 if (CHARSET_CHARS (charset
) == 96)
2165 /* A charset of CHARS96 can't be designated to REG 0. */
2169 /* There exist some default graphic register. */
2170 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2172 ? 0 : (reg_bits
& 2 ? 1 : (reg_bits
& 4 ? 2 : 3)));
2174 /* We anyway have to designate CHARSET to somewhere. */
2175 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2176 = (CHARSET_CHARS (charset
) == 94
2178 : ((coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
2179 || ! coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
)
2181 : (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
2185 coding
->require_flushing
= 1;
2189 coding
->type
= coding_type_big5
;
2191 = (NILP (XVECTOR (coding_system_vector
)->contents
[4])
2192 ? CODING_FLAG_BIG5_HKU
2193 : CODING_FLAG_BIG5_ETEN
);
2197 coding
->type
= coding_type_ccl
;
2199 Lisp_Object val
= XVECTOR (coding_system_vector
)->contents
[4];
2201 && VECTORP (XCONS (val
)->car
)
2202 && VECTORP (XCONS (val
)->cdr
))
2204 setup_ccl_program (&(coding
->spec
.ccl
.decoder
), XCONS (val
)->car
);
2205 setup_ccl_program (&(coding
->spec
.ccl
.encoder
), XCONS (val
)->cdr
);
2208 goto label_invalid_coding_system
;
2210 coding
->require_flushing
= 1;
2215 coding
->type
= coding_type_automatic
;
2217 coding
->type
= coding_type_no_conversion
;
2222 label_invalid_coding_system
:
2223 coding
->type
= coding_type_no_conversion
;
2227 /* Emacs has a mechanism to automatically detect a coding system if it
2228 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
2229 it's impossible to distinguish some coding systems accurately
2230 because they use the same range of codes. So, at first, coding
2231 systems are categorized into 7, those are:
2233 o coding-category-internal
2235 The category for a coding system which has the same code range
2236 as Emacs' internal format. Assigned the coding-system (Lisp
2237 symbol) `coding-system-internal' by default.
2239 o coding-category-sjis
2241 The category for a coding system which has the same code range
2242 as SJIS. Assigned the coding-system (Lisp
2243 symbol) `coding-system-sjis' by default.
2245 o coding-category-iso-7
2247 The category for a coding system which has the same code range
2248 as ISO2022 of 7-bit environment. Assigned the coding-system
2249 (Lisp symbol) `coding-system-junet' by default.
2251 o coding-category-iso-8-1
2253 The category for a coding system which has the same code range
2254 as ISO2022 of 8-bit environment and graphic plane 1 used only
2255 for DIMENSION1 charset. Assigned the coding-system (Lisp
2256 symbol) `coding-system-ctext' by default.
2258 o coding-category-iso-8-2
2260 The category for a coding system which has the same code range
2261 as ISO2022 of 8-bit environment and graphic plane 1 used only
2262 for DIMENSION2 charset. Assigned the coding-system (Lisp
2263 symbol) `coding-system-euc-japan' by default.
2265 o coding-category-iso-else
2267 The category for a coding system which has the same code range
2268 as ISO2022 but not belongs to any of the above three
2269 categories. Assigned the coding-system (Lisp symbol)
2270 `coding-system-iso-2022-ss2-7' by default.
2272 o coding-category-big5
2274 The category for a coding system which has the same code range
2275 as BIG5. Assigned the coding-system (Lisp symbol)
2276 `coding-system-big5' by default.
2278 o coding-category-binary
2280 The category for a coding system not categorized in any of the
2281 above. Assigned the coding-system (Lisp symbol)
2282 `coding-system-noconv' by default.
2284 Each of them is a Lisp symbol and the value is an actual
2285 `coding-system's (this is also a Lisp symbol) assigned by a user.
2286 What Emacs does actually is to detect a category of coding system.
2287 Then, it uses a `coding-system' assigned to it. If Emacs can't
2288 decide only one possible category, it selects a category of the
2289 highest priority. Priorities of categories are also specified by a
2290 user in a Lisp variable `coding-category-list'.
2294 /* Detect how a text of length SRC_BYTES pointed by SRC is encoded.
2295 If it detects possible coding systems, return an integer in which
2296 appropriate flag bits are set. Flag bits are defined by macros
2297 CODING_CATEGORY_MASK_XXX in `coding.h'. */
2300 detect_coding_mask (src
, src_bytes
)
2304 register unsigned char c
;
2305 unsigned char *src_end
= src
+ src_bytes
;
2308 /* At first, skip all ASCII characters and control characters except
2309 for three ISO2022 specific control characters. */
2310 while (src
< src_end
)
2314 || (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
))
2320 /* We found nothing other than ASCII. There's nothing to do. */
2321 return CODING_CATEGORY_MASK_ANY
;
2323 /* The text seems to be encoded in some multilingual coding system.
2324 Now, try to find in which coding system the text is encoded. */
2326 /* i.e. (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) */
2327 /* C is an ISO2022 specific control code of C0. */
2328 mask
= detect_coding_iso2022 (src
, src_end
);
2330 else if (c
== ISO_CODE_SS2
|| c
== ISO_CODE_SS3
|| c
== ISO_CODE_CSI
)
2331 /* C is an ISO2022 specific control code of C1,
2332 or the first byte of SJIS's 2-byte character code,
2333 or a leading code of Emacs. */
2334 mask
= (detect_coding_iso2022 (src
, src_end
)
2335 | detect_coding_sjis (src
, src_end
)
2336 | detect_coding_internal (src
, src_end
));
2339 /* C is the first byte of SJIS character code,
2340 or a leading-code of Emacs. */
2341 mask
= (detect_coding_sjis (src
, src_end
)
2342 | detect_coding_internal (src
, src_end
));
2345 /* C is a character of ISO2022 in graphic plane right,
2346 or a SJIS's 1-byte character code (i.e. JISX0201),
2347 or the first byte of BIG5's 2-byte code. */
2348 mask
= (detect_coding_iso2022 (src
, src_end
)
2349 | detect_coding_sjis (src
, src_end
)
2350 | detect_coding_big5 (src
, src_end
));
2355 /* Detect how a text of length SRC_BYTES pointed by SRC is encoded.
2356 The information of the detected coding system is set in CODING. */
2359 detect_coding (coding
, src
, src_bytes
)
2360 struct coding_system
*coding
;
2364 int mask
= detect_coding_mask (src
, src_bytes
);
2367 if (mask
== CODING_CATEGORY_MASK_ANY
)
2368 /* We found nothing other than ASCII. There's nothing to do. */
2372 /* The source text seems to be encoded in unknown coding system.
2373 Emacs regards the category of such a kind of coding system as
2374 `coding-category-binary'. We assume that a user has assigned
2375 an appropriate coding system for a `coding-category-binary'. */
2376 idx
= CODING_CATEGORY_IDX_BINARY
;
2379 /* We found some plausible coding systems. Let's use a coding
2380 system of the highest priority. */
2381 Lisp_Object val
= Vcoding_category_list
;
2386 idx
= XFASTINT (Fget (XCONS (val
)->car
, Qcoding_category_index
));
2387 if ((idx
< CODING_CATEGORY_IDX_MAX
) && (mask
& (1 << idx
)))
2389 val
= XCONS (val
)->cdr
;
2396 /* For unknown reason, `Vcoding_category_list' contains none
2397 of found categories. Let's use any of them. */
2398 for (idx
= 0; idx
< CODING_CATEGORY_IDX_MAX
; idx
++)
2399 if (mask
& (1 << idx
))
2403 setup_coding_system (XSYMBOL (coding_category_table
[idx
])->value
, coding
);
2406 /* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC
2407 is encoded. Return one of CODING_EOL_LF, CODING_EOL_CRLF,
2408 CODING_EOL_CR, and CODING_EOL_AUTOMATIC. */
2411 detect_eol_type (src
, src_bytes
)
2415 unsigned char *src_end
= src
+ src_bytes
;
2418 while (src
< src_end
)
2422 return CODING_EOL_LF
;
2425 if (src
< src_end
&& *src
== '\n')
2426 return CODING_EOL_CRLF
;
2428 return CODING_EOL_CR
;
2431 return CODING_EOL_AUTOMATIC
;
2434 /* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC
2435 is encoded. If it detects an appropriate format of end-of-line, it
2436 sets the information in *CODING. */
2439 detect_eol (coding
, src
, src_bytes
)
2440 struct coding_system
*coding
;
2445 int eol_type
= detect_eol_type (src
, src_bytes
);
2447 if (eol_type
== CODING_EOL_AUTOMATIC
)
2448 /* We found no end-of-line in the source text. */
2451 val
= Fget (coding
->symbol
, Qeol_type
);
2452 if (VECTORP (val
) && XVECTOR (val
)->size
== 3)
2453 setup_coding_system (XVECTOR (val
)->contents
[eol_type
], coding
);
2456 /* See "GENERAL NOTES about `decode_coding_XXX ()' functions". Before
2457 decoding, it may detect coding system and format of end-of-line if
2458 those are not yet decided. */
2461 decode_coding (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
2462 struct coding_system
*coding
;
2463 unsigned char *source
, *destination
;
2464 int src_bytes
, dst_bytes
;
2475 if (coding
->type
== coding_type_automatic
)
2476 detect_coding (coding
, source
, src_bytes
);
2478 if (coding
->eol_type
== CODING_EOL_AUTOMATIC
)
2479 detect_eol (coding
, source
, src_bytes
);
2481 coding
->carryover_size
= 0;
2482 switch (coding
->type
)
2484 case coding_type_no_conversion
:
2485 label_no_conversion
:
2486 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
2487 bcopy (source
, destination
, produced
);
2488 *consumed
= produced
;
2491 case coding_type_internal
:
2492 case coding_type_automatic
:
2493 if (coding
->eol_type
== CODING_EOL_LF
2494 || coding
->eol_type
== CODING_EOL_AUTOMATIC
)
2495 goto label_no_conversion
;
2496 produced
= decode_eol (coding
, source
, destination
,
2497 src_bytes
, dst_bytes
, consumed
);
2500 case coding_type_sjis
:
2501 produced
= decode_coding_sjis_big5 (coding
, source
, destination
,
2502 src_bytes
, dst_bytes
, consumed
,
2506 case coding_type_iso2022
:
2507 produced
= decode_coding_iso2022 (coding
, source
, destination
,
2508 src_bytes
, dst_bytes
, consumed
);
2511 case coding_type_big5
:
2512 produced
= decode_coding_sjis_big5 (coding
, source
, destination
,
2513 src_bytes
, dst_bytes
, consumed
,
2517 case coding_type_ccl
:
2518 produced
= ccl_driver (&coding
->spec
.ccl
.decoder
, source
, destination
,
2519 src_bytes
, dst_bytes
, consumed
);
2526 /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". */
2529 encode_coding (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
2530 struct coding_system
*coding
;
2531 unsigned char *source
, *destination
;
2532 int src_bytes
, dst_bytes
;
2537 coding
->carryover_size
= 0;
2538 switch (coding
->type
)
2540 case coding_type_no_conversion
:
2541 label_no_conversion
:
2542 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
2545 bcopy (source
, destination
, produced
);
2546 if (coding
->selective
)
2548 unsigned char *p
= destination
, *pend
= destination
+ produced
;
2550 if (*p
++ = '\015') p
[-1] = '\n';
2553 *consumed
= produced
;
2556 case coding_type_internal
:
2557 case coding_type_automatic
:
2558 if (coding
->eol_type
== CODING_EOL_LF
2559 || coding
->eol_type
== CODING_EOL_AUTOMATIC
)
2560 goto label_no_conversion
;
2561 produced
= encode_eol (coding
, source
, destination
,
2562 src_bytes
, dst_bytes
, consumed
);
2565 case coding_type_sjis
:
2566 produced
= encode_coding_sjis_big5 (coding
, source
, destination
,
2567 src_bytes
, dst_bytes
, consumed
,
2571 case coding_type_iso2022
:
2572 produced
= encode_coding_iso2022 (coding
, source
, destination
,
2573 src_bytes
, dst_bytes
, consumed
);
2576 case coding_type_big5
:
2577 produced
= encode_coding_sjis_big5 (coding
, source
, destination
,
2578 src_bytes
, dst_bytes
, consumed
,
2582 case coding_type_ccl
:
2583 produced
= ccl_driver (&coding
->spec
.ccl
.encoder
, source
, destination
,
2584 src_bytes
, dst_bytes
, consumed
);
2591 #define CONVERSION_BUFFER_EXTRA_ROOM 256
2593 /* Return maximum size (bytes) of a buffer enough for decoding
2594 SRC_BYTES of text encoded in CODING. */
2597 decoding_buffer_size (coding
, src_bytes
)
2598 struct coding_system
*coding
;
2603 if (coding
->type
== coding_type_iso2022
)
2605 else if (coding
->type
== coding_type_ccl
)
2606 magnification
= coding
->spec
.ccl
.decoder
.buf_magnification
;
2610 return (src_bytes
* magnification
+ CONVERSION_BUFFER_EXTRA_ROOM
);
2613 /* Return maximum size (bytes) of a buffer enough for encoding
2614 SRC_BYTES of text to CODING. */
2617 encoding_buffer_size (coding
, src_bytes
)
2618 struct coding_system
*coding
;
2623 if (coding
->type
== coding_type_ccl
)
2624 magnification
= coding
->spec
.ccl
.encoder
.buf_magnification
;
2628 return (src_bytes
* magnification
+ CONVERSION_BUFFER_EXTRA_ROOM
);
2631 #ifndef MINIMUM_CONVERSION_BUFFER_SIZE
2632 #define MINIMUM_CONVERSION_BUFFER_SIZE 1024
2635 char *conversion_buffer
;
2636 int conversion_buffer_size
;
2638 /* Return a pointer to a SIZE bytes of buffer to be used for encoding
2639 or decoding. Sufficient memory is allocated automatically. If we
2640 run out of memory, return NULL. */
2643 get_conversion_buffer (size
)
2646 if (size
> conversion_buffer_size
)
2649 int real_size
= conversion_buffer_size
* 2;
2651 while (real_size
< size
) real_size
*= 2;
2652 buf
= (char *) xmalloc (real_size
);
2653 xfree (conversion_buffer
);
2654 conversion_buffer
= buf
;
2655 conversion_buffer_size
= real_size
;
2657 return conversion_buffer
;
2662 /*** 7. Emacs Lisp library functions ***/
2664 DEFUN ("coding-system-vector", Fcoding_system_vector
, Scoding_system_vector
,
2666 "Return coding-vector of CODING-SYSTEM.\n\
2667 If CODING-SYSTEM is not a valid coding-system, return nil.")
2671 while (SYMBOLP (obj
) && !NILP (obj
))
2672 obj
= Fget (obj
, Qcoding_system
);
2673 return ((NILP (obj
) || !VECTORP (obj
) || XVECTOR (obj
)->size
!= 5)
2677 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
2678 "Return t if OBJECT is nil or a coding-system.\n\
2679 See document of make-coding-system for coding-system object.")
2683 return ((NILP (obj
) || !NILP (Fcoding_system_vector (obj
))) ? Qt
: Qnil
);
2686 DEFUN ("read-non-nil-coding-system",
2687 Fread_non_nil_coding_system
, Sread_non_nil_coding_system
, 1, 1, 0,
2688 "Read a coding-system from the minibuffer, prompting with string PROMPT.")
2692 return Fintern (Fcompleting_read (prompt
, Vobarray
, Qcoding_system_vector
,
2697 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 1, 0,
2698 "Read a coding-system or nil from the minibuffer, prompting with string PROMPT.")
2702 return Fintern (Fcompleting_read (prompt
, Vobarray
, Qcoding_system_p
,
2707 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
2709 "Check validity of CODING-SYSTEM.\n\
2710 If valid, return CODING-SYSTEM, else `coding-system-error' is signaled.\n\
2711 CODING-SYSTEM is valid if it is a symbol and has \"coding-system\" property.\n\
2712 The value of property should be a vector of length 5.")
2714 Lisp_Object coding_system
;
2716 CHECK_SYMBOL (coding_system
, 0);
2717 if (!NILP (Fcoding_system_p (coding_system
)))
2718 return coding_system
;
2720 Fsignal (Qcoding_system_error
, coding_system
);
2723 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
2725 "Detect coding-system of the text in the region between START and END.\n\
2726 Return a list of possible coding-systems ordered by priority.\n\
2727 If only ASCII characters are found, it returns `coding-system-automatic'\n\
2728 or its subsidiary coding-system according to a detected end-of-line format.")
2732 int coding_mask
, eol_type
;
2736 validate_region (&b
, &e
);
2737 beg
= XINT (b
), end
= XINT (e
);
2738 if (beg
< GPT
&& end
>= GPT
) move_gap (end
);
2740 coding_mask
= detect_coding_mask (POS_ADDR (beg
), end
- beg
);
2741 eol_type
= detect_eol_type (POS_ADDR (beg
), end
- beg
);
2743 if (coding_mask
== CODING_CATEGORY_MASK_ANY
)
2745 val
= intern ("coding-system-automatic");
2746 if (eol_type
!= CODING_EOL_AUTOMATIC
)
2748 Lisp_Object val2
= Fget (val
, Qeol_type
);
2750 val
= XVECTOR (val2
)->contents
[eol_type
];
2757 /* At first, gather possible coding-systems in VAL in a reverse
2760 for (val2
= Vcoding_category_list
;
2762 val2
= XCONS (val2
)->cdr
)
2765 = XFASTINT (Fget (XCONS (val2
)->car
, Qcoding_category_index
));
2766 if (coding_mask
& (1 << idx
))
2767 val
= Fcons (Fsymbol_value (XCONS (val2
)->car
), val
);
2770 /* Then, change the order of the list, while getting subsidiary
2774 for (; !NILP (val2
); val2
= XCONS (val2
)->cdr
)
2776 if (eol_type
== CODING_EOL_AUTOMATIC
)
2777 val
= Fcons (XCONS (val2
)->car
, val
);
2780 Lisp_Object val3
= Fget (XCONS (val2
)->car
, Qeol_type
);
2782 val
= Fcons (XVECTOR (val3
)->contents
[eol_type
], val
);
2784 val
= Fcons (XCONS (val2
)->car
, val
);
2792 /* Scan text in the region between *BEGP and *ENDP, skip characters
2793 which we never have to encode to (iff ENCODEP is 1) or decode from
2794 coding system CODING at the head and tail, then set BEGP and ENDP
2795 to the addresses of start and end of the text we actually convert. */
2798 shrink_conversion_area (begp
, endp
, coding
, encodep
)
2799 unsigned char **begp
, **endp
;
2800 struct coding_system
*coding
;
2803 register unsigned char *beg_addr
= *begp
, *end_addr
= *endp
;
2805 if (coding
->eol_type
!= CODING_EOL_LF
2806 && coding
->eol_type
!= CODING_EOL_AUTOMATIC
)
2807 /* Since we anyway have to convert end-of-line format, it is not
2808 worth skipping at most 100 bytes or so. */
2811 if (encodep
) /* for encoding */
2813 switch (coding
->type
)
2815 case coding_type_no_conversion
:
2816 case coding_type_internal
:
2817 case coding_type_automatic
:
2818 /* We need no conversion. */
2821 case coding_type_ccl
:
2822 /* We can't skip any data. */
2825 /* We can skip all ASCII characters at the head and tail. */
2826 while (beg_addr
< end_addr
&& *beg_addr
< 0x80) beg_addr
++;
2827 while (beg_addr
< end_addr
&& *(end_addr
- 1) < 0x80) end_addr
--;
2831 else /* for decoding */
2833 switch (coding
->type
)
2835 case coding_type_no_conversion
:
2836 /* We need no conversion. */
2839 case coding_type_internal
:
2840 if (coding
->eol_type
== CODING_EOL_LF
)
2842 /* We need no conversion. */
2846 /* We can skip all but carriage-return. */
2847 while (beg_addr
< end_addr
&& *beg_addr
!= '\r') beg_addr
++;
2848 while (beg_addr
< end_addr
&& *(end_addr
- 1) != '\r') end_addr
--;
2850 case coding_type_sjis
:
2851 case coding_type_big5
:
2852 /* We can skip all ASCII characters at the head. */
2853 while (beg_addr
< end_addr
&& *beg_addr
< 0x80) beg_addr
++;
2854 /* We can skip all ASCII characters at the tail except for
2855 the second byte of SJIS or BIG5 code. */
2856 while (beg_addr
< end_addr
&& *(end_addr
- 1) < 0x80) end_addr
--;
2857 if (end_addr
!= *endp
)
2860 case coding_type_ccl
:
2861 /* We can't skip any data. */
2863 default: /* i.e. case coding_type_iso2022: */
2867 /* We can skip all ASCII characters except for a few
2868 control codes at the head. */
2869 while (beg_addr
< end_addr
&& (c
= *beg_addr
) < 0x80
2870 && c
!= ISO_CODE_CR
&& c
!= ISO_CODE_SO
2871 && c
!= ISO_CODE_SI
&& c
!= ISO_CODE_ESC
)
2882 /* Encode to (iff ENCODEP is 1) or decode form coding system CODING a
2883 text between B and E. B and E are buffer position. */
2886 code_convert_region (b
, e
, coding
, encodep
)
2888 struct coding_system
*coding
;
2891 int beg
, end
, len
, consumed
, produced
;
2893 unsigned char *begp
, *endp
;
2896 validate_region (&b
, &e
);
2897 beg
= XINT (b
), end
= XINT (e
);
2898 if (beg
< GPT
&& end
>= GPT
)
2901 if (encodep
&& !NILP (coding
->pre_write_conversion
))
2903 /* We must call a pre-conversion function which may put a new
2904 text to be converted in a new buffer. */
2905 struct buffer
*old
= current_buffer
, *new;
2908 call2 (coding
->pre_write_conversion
, b
, e
);
2909 if (old
!= current_buffer
)
2911 /* Replace the original text by the text just generated. */
2913 new = current_buffer
;
2914 set_buffer_internal (old
);
2915 del_range (beg
, end
);
2916 insert_from_buffer (new, 1, len
, 0);
2921 /* We may be able to shrink the conversion region. */
2922 begp
= POS_ADDR (beg
); endp
= begp
+ (end
- beg
);
2923 shrink_conversion_area (&begp
, &endp
, coding
, encodep
);
2926 /* We need no conversion. */
2930 beg
+= begp
- POS_ADDR (beg
);
2931 end
= beg
+ (endp
- begp
);
2934 len
= encoding_buffer_size (coding
, end
- beg
);
2936 len
= decoding_buffer_size (coding
, end
- beg
);
2937 buf
= get_conversion_buffer (len
);
2939 coding
->last_block
= 1;
2941 ? encode_coding (coding
, POS_ADDR (beg
), buf
, end
- beg
, len
,
2943 : decode_coding (coding
, POS_ADDR (beg
), buf
, end
- beg
, len
,
2946 len
= produced
+ (beg
- XINT (b
)) + (XINT (e
) - end
);
2949 insert (buf
, produced
);
2950 del_range (PT
, PT
+ end
- beg
);
2952 pos
= PT
+ (pos
- end
);
2958 if (!encodep
&& !NILP (coding
->post_read_conversion
))
2960 /* We must call a post-conversion function which may alter
2961 the text just converted. */
2966 insval
= call1 (coding
->post_read_conversion
, make_number (len
));
2967 CHECK_NUMBER (insval
, 0);
2968 len
= XINT (insval
);
2971 return make_number (len
);
2975 code_convert_string (str
, coding
, encodep
)
2977 struct coding_system
*coding
;
2980 int len
, consumed
, produced
;
2982 unsigned char *begp
, *endp
;
2983 int head_skip
, tail_skip
;
2984 struct gcpro gcpro1
;
2986 if (encodep
&& !NILP (coding
->pre_write_conversion
)
2987 || !encodep
&& !NILP (coding
->post_read_conversion
))
2989 /* Since we have to call Lisp functions which assume target text
2990 is in a buffer, after setting a temporary buffer, call
2991 code_convert_region. */
2992 int count
= specpdl_ptr
- specpdl
;
2993 int len
= XSTRING (str
)->size
;
2995 struct buffer
*old
= current_buffer
;
2997 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
2998 temp_output_buffer_setup (" *code-converting-work*");
2999 set_buffer_internal (XBUFFER (Vstandard_output
));
3000 insert_from_string (str
, 0, len
, 0);
3001 code_convert_region (make_number (BEGV
), make_number (ZV
),
3003 result
= make_buffer_string (BEGV
, ZV
, 0);
3004 set_buffer_internal (old
);
3005 return unbind_to (count
, result
);
3008 /* We may be able to shrink the conversion region. */
3009 begp
= XSTRING (str
)->data
;
3010 endp
= begp
+ XSTRING (str
)->size
;
3011 shrink_conversion_area (&begp
, &endp
, coding
, encodep
);
3014 /* We need no conversion. */
3017 head_skip
= begp
- XSTRING (str
)->data
;
3018 tail_skip
= XSTRING (str
)->size
- head_skip
- (endp
- begp
);
3023 len
= encoding_buffer_size (coding
, endp
- begp
);
3025 len
= decoding_buffer_size (coding
, endp
- begp
);
3026 buf
= get_conversion_buffer (len
+ head_skip
+ tail_skip
);
3028 bcopy (XSTRING (str
)->data
, buf
, head_skip
);
3029 coding
->last_block
= 1;
3031 ? encode_coding (coding
, XSTRING (str
)->data
+ head_skip
,
3032 buf
+ head_skip
, endp
- begp
, len
, &consumed
)
3033 : decode_coding (coding
, XSTRING (str
)->data
+ head_skip
,
3034 buf
+ head_skip
, endp
- begp
, len
, &consumed
));
3035 bcopy (XSTRING (str
)->data
+ head_skip
+ (endp
- begp
),
3036 buf
+ head_skip
+ produced
,
3041 return make_string (buf
, head_skip
+ produced
+ tail_skip
);
3044 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
3046 "Decode the text between START and END which is encoded in CODING-SYSTEM.\n\
3047 Return length of decoded text.")
3048 (b
, e
, coding_system
)
3049 Lisp_Object b
, e
, coding_system
;
3051 struct coding_system coding
;
3053 CHECK_NUMBER_COERCE_MARKER (b
, 0);
3054 CHECK_NUMBER_COERCE_MARKER (e
, 1);
3055 CHECK_SYMBOL (coding_system
, 2);
3057 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
3058 error ("Invalid coding-system: %s", XSYMBOL (coding_system
)->name
->data
);
3060 return code_convert_region (b
, e
, &coding
, 0);
3063 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
3065 "Encode the text between START and END to CODING-SYSTEM.\n\
3066 Return length of encoded text.")
3067 (b
, e
, coding_system
)
3068 Lisp_Object b
, e
, coding_system
;
3070 struct coding_system coding
;
3072 CHECK_NUMBER_COERCE_MARKER (b
, 0);
3073 CHECK_NUMBER_COERCE_MARKER (e
, 1);
3074 CHECK_SYMBOL (coding_system
, 2);
3076 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
3077 error ("Invalid coding-system: %s", XSYMBOL (coding_system
)->name
->data
);
3079 return code_convert_region (b
, e
, &coding
, 1);
3082 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
3084 "Decode STRING which is encoded in CODING-SYSTEM, and return the result.")
3085 (string
, coding_system
)
3086 Lisp_Object string
, coding_system
;
3088 struct coding_system coding
;
3090 CHECK_STRING (string
, 0);
3091 CHECK_SYMBOL (coding_system
, 1);
3093 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
3094 error ("Invalid coding-system: %s", XSYMBOL (coding_system
)->name
->data
);
3096 return code_convert_string (string
, &coding
, 0);
3099 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
3101 "Encode STRING to CODING-SYSTEM, and return the result.")
3102 (string
, coding_system
)
3103 Lisp_Object string
, coding_system
;
3105 struct coding_system coding
;
3107 CHECK_STRING (string
, 0);
3108 CHECK_SYMBOL (coding_system
, 1);
3110 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
3111 error ("Invalid coding-system: %s", XSYMBOL (coding_system
)->name
->data
);
3113 return code_convert_string (string
, &coding
, 1);
3116 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
3117 "Decode a JISX0208 character of SJIS coding-system-sjis.\n\
3118 CODE is the character code in SJIS.\n\
3119 Return the corresponding character.")
3123 unsigned char c1
, c2
, s1
, s2
;
3126 CHECK_NUMBER (code
, 0);
3127 s1
= (XFASTINT (code
)) >> 8, s2
= (XFASTINT (code
)) & 0xFF;
3128 DECODE_SJIS (s1
, s2
, c1
, c2
);
3129 XSETFASTINT (val
, MAKE_NON_ASCII_CHAR (charset_jisx0208
, c1
, c2
));
3133 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
3134 "Encode a JISX0208 character CHAR to SJIS coding-system.\n\
3135 Return the corresponding character code in SJIS.")
3140 unsigned char c1
, c2
, s1
, s2
;
3143 CHECK_NUMBER (ch
, 0);
3144 SPLIT_CHAR (XFASTINT (ch
), charset
, c1
, c2
);
3145 if (charset
== charset_jisx0208
)
3147 ENCODE_SJIS (c1
, c2
, s1
, s2
);
3148 XSETFASTINT (val
, ((int)s1
<< 8) | s2
);
3151 XSETFASTINT (val
, 0);
3155 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
3156 "Decode a Big5 character CODE of BIG5 coding-system.\n\
3157 CODE is the character code in BIG5.\n\
3158 Return the corresponding character.")
3163 unsigned char b1
, b2
, c1
, c2
;
3166 CHECK_NUMBER (code
, 0);
3167 b1
= (XFASTINT (code
)) >> 8, b2
= (XFASTINT (code
)) & 0xFF;
3168 DECODE_BIG5 (b1
, b2
, charset
, c1
, c2
);
3169 XSETFASTINT (val
, MAKE_NON_ASCII_CHAR (charset
, c1
, c2
));
3173 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
3174 "Encode the Big5 character CHAR to BIG5 coding-system.\n\
3175 Return the corresponding character code in Big5.")
3180 unsigned char c1
, c2
, b1
, b2
;
3183 CHECK_NUMBER (ch
, 0);
3184 SPLIT_CHAR (XFASTINT (ch
), charset
, c1
, c2
);
3185 if (charset
== charset_big5_1
|| charset
== charset_big5_2
)
3187 ENCODE_BIG5 (charset
, c1
, c2
, b1
, b2
);
3188 XSETFASTINT (val
, ((int)b1
<< 8) | b2
);
3191 XSETFASTINT (val
, 0);
3195 DEFUN ("set-terminal-coding-system",
3196 Fset_terminal_coding_system
, Sset_terminal_coding_system
, 1, 1,
3197 "zCoding-system for terminal display: ",
3198 "Set coding-system of your terminal to CODING-SYSTEM.\n\
3199 All outputs to terminal are encoded to this coding-system.")
3201 Lisp_Object coding_system
;
3203 CHECK_SYMBOL (coding_system
, 0);
3204 setup_coding_system (Fcheck_coding_system (coding_system
), &terminal_coding
);
3205 update_mode_lines
++;
3206 if (!NILP (Finteractive_p ()))
3211 DEFUN ("terminal-coding-system",
3212 Fterminal_coding_system
, Sterminal_coding_system
, 0, 0, 0,
3213 "Return coding-system of your terminal.")
3216 return terminal_coding
.symbol
;
3219 DEFUN ("set-keyboard-coding-system",
3220 Fset_keyboard_coding_system
, Sset_keyboard_coding_system
, 1, 1,
3221 "zCoding-system for keyboard input: ",
3222 "Set coding-system of what is sent from terminal keyboard to CODING-SYSTEM.\n\
3223 All inputs from terminal are decoded from this coding-system.")
3225 Lisp_Object coding_system
;
3227 CHECK_SYMBOL (coding_system
, 0);
3228 setup_coding_system (Fcheck_coding_system (coding_system
), &keyboard_coding
);
3232 DEFUN ("keyboard-coding-system",
3233 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 0, 0,
3234 "Return coding-system of what is sent from terminal keyboard.")
3237 return keyboard_coding
.symbol
;
3241 DEFUN ("find-coding-system", Ffind_coding_system
, Sfind_coding_system
,
3243 "Return a cons of coding systems for I/O primitive OPERATION.\n\
3244 Remaining arguments are for OPERATION.\n\
3245 OPERATION is one of the following Emacs I/O primitives:\n\
3246 For file I/O, insert-file-contents or write-region.\n\
3247 For process I/O, call-process, call-process-region, or start-process.\n\
3248 For network I/O, open-network-stream.\n\
3249 For each OPERATION, TARGET is selected from the arguments as below:\n\
3250 For file I/O, TARGET is a file name.\n\
3251 For process I/O, TARGET is a process name.\n\
3252 For network I/O, TARGET is a service name or a port number\n\
3254 The return value is a cons of coding systems for decoding and encoding\n\
3255 registered in nested alist `coding-system-alist' (which see) at a slot\n\
3256 corresponding to OPERATION and TARGET.
3257 If a function symbol is at the slot, return a result of the function call.\n\
3258 The function is called with one argument, a list of all the arguments.")
3263 Lisp_Object operation
, target_idx
, target
, val
;
3264 register Lisp_Object chain
;
3267 error ("Too few arguments");
3268 operation
= args
[0];
3269 if (!SYMBOLP (operation
)
3270 || !INTEGERP (target_idx
= Fget (operation
, Qtarget_idx
)))
3271 error ("Invalid first arguement");
3272 if (nargs
< 1 + XINT (target_idx
))
3273 error ("Too few arguments for operation: %s",
3274 XSYMBOL (operation
)->name
->data
);
3275 target
= args
[XINT (target_idx
) + 1];
3276 if (!(STRINGP (target
)
3277 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
3278 error ("Invalid %dth argument", XINT (target_idx
) + 1);
3280 chain
= Fassq (operation
, Vcoding_system_alist
);
3284 for (chain
= XCONS (chain
)->cdr
; CONSP (chain
); chain
= XCONS (chain
)->cdr
)
3286 Lisp_Object elt
= XCONS (chain
)->car
;
3289 && ((STRINGP (target
)
3290 && STRINGP (XCONS (elt
)->car
)
3291 && fast_string_match (XCONS (elt
)->car
, target
) >= 0)
3292 || (INTEGERP (target
) && EQ (target
, XCONS (elt
)->car
))))
3293 return (CONSP (val
= XCONS (elt
)->cdr
)
3295 : ((SYMBOLP (val
) && Fboundp (val
)
3296 ? call2 (val
, Flist (nargs
, args
))
3305 /*** 8. Post-amble ***/
3311 /* Emacs internal format specific initialize routine. */
3312 for (i
= 0; i
<= 0x20; i
++)
3313 emacs_code_class
[i
] = EMACS_control_code
;
3314 emacs_code_class
[0x0A] = EMACS_linefeed_code
;
3315 emacs_code_class
[0x0D] = EMACS_carriage_return_code
;
3316 for (i
= 0x21 ; i
< 0x7F; i
++)
3317 emacs_code_class
[i
] = EMACS_ascii_code
;
3318 emacs_code_class
[0x7F] = EMACS_control_code
;
3319 emacs_code_class
[0x80] = EMACS_leading_code_composition
;
3320 for (i
= 0x81; i
< 0xFF; i
++)
3321 emacs_code_class
[i
] = EMACS_invalid_code
;
3322 emacs_code_class
[LEADING_CODE_PRIVATE_11
] = EMACS_leading_code_3
;
3323 emacs_code_class
[LEADING_CODE_PRIVATE_12
] = EMACS_leading_code_3
;
3324 emacs_code_class
[LEADING_CODE_PRIVATE_21
] = EMACS_leading_code_4
;
3325 emacs_code_class
[LEADING_CODE_PRIVATE_22
] = EMACS_leading_code_4
;
3327 /* ISO2022 specific initialize routine. */
3328 for (i
= 0; i
< 0x20; i
++)
3329 iso_code_class
[i
] = ISO_control_code
;
3330 for (i
= 0x21; i
< 0x7F; i
++)
3331 iso_code_class
[i
] = ISO_graphic_plane_0
;
3332 for (i
= 0x80; i
< 0xA0; i
++)
3333 iso_code_class
[i
] = ISO_control_code
;
3334 for (i
= 0xA1; i
< 0xFF; i
++)
3335 iso_code_class
[i
] = ISO_graphic_plane_1
;
3336 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
3337 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
3338 iso_code_class
[ISO_CODE_CR
] = ISO_carriage_return
;
3339 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
3340 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
3341 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
3342 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
3343 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
3344 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
3345 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
3347 Qcoding_system
= intern ("coding-system");
3348 staticpro (&Qcoding_system
);
3350 Qeol_type
= intern ("eol-type");
3351 staticpro (&Qeol_type
);
3353 Qbuffer_file_coding_system
= intern ("buffer-file-coding-system");
3354 staticpro (&Qbuffer_file_coding_system
);
3356 Qpost_read_conversion
= intern ("post-read-conversion");
3357 staticpro (&Qpost_read_conversion
);
3359 Qpre_write_conversion
= intern ("pre-write-conversion");
3360 staticpro (&Qpre_write_conversion
);
3362 Qcoding_system_vector
= intern ("coding-system-vector");
3363 staticpro (&Qcoding_system_vector
);
3365 Qcoding_system_p
= intern ("coding-system-p");
3366 staticpro (&Qcoding_system_p
);
3368 Qcoding_system_error
= intern ("coding-system-error");
3369 staticpro (&Qcoding_system_error
);
3371 Fput (Qcoding_system_error
, Qerror_conditions
,
3372 Fcons (Qcoding_system_error
, Fcons (Qerror
, Qnil
)));
3373 Fput (Qcoding_system_error
, Qerror_message
,
3374 build_string ("Coding-system error"));
3376 Qcoding_category_index
= intern ("coding-category-index");
3377 staticpro (&Qcoding_category_index
);
3381 for (i
= 0; i
< CODING_CATEGORY_IDX_MAX
; i
++)
3383 coding_category_table
[i
] = intern (coding_category_name
[i
]);
3384 staticpro (&coding_category_table
[i
]);
3385 Fput (coding_category_table
[i
], Qcoding_category_index
,
3390 conversion_buffer_size
= MINIMUM_CONVERSION_BUFFER_SIZE
;
3391 conversion_buffer
= (char *) xmalloc (MINIMUM_CONVERSION_BUFFER_SIZE
);
3393 setup_coding_system (Qnil
, &keyboard_coding
);
3394 setup_coding_system (Qnil
, &terminal_coding
);
3401 Qtarget_idx
= intern ("target-idx");
3402 staticpro (&Qtarget_idx
);
3404 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
3405 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
3407 Qcall_process
= intern ("call-process");
3408 staticpro (&Qcall_process
);
3409 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
3411 Qcall_process_region
= intern ("call-process-region");
3412 staticpro (&Qcall_process_region
);
3413 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
3415 Qstart_process
= intern ("start-process");
3416 staticpro (&Qstart_process
);
3417 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
3419 Qopen_network_stream
= intern ("open-network-stream");
3420 staticpro (&Qopen_network_stream
);
3421 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
3423 defsubr (&Scoding_system_vector
);
3424 defsubr (&Scoding_system_p
);
3425 defsubr (&Sread_coding_system
);
3426 defsubr (&Sread_non_nil_coding_system
);
3427 defsubr (&Scheck_coding_system
);
3428 defsubr (&Sdetect_coding_region
);
3429 defsubr (&Sdecode_coding_region
);
3430 defsubr (&Sencode_coding_region
);
3431 defsubr (&Sdecode_coding_string
);
3432 defsubr (&Sencode_coding_string
);
3433 defsubr (&Sdecode_sjis_char
);
3434 defsubr (&Sencode_sjis_char
);
3435 defsubr (&Sdecode_big5_char
);
3436 defsubr (&Sencode_big5_char
);
3437 defsubr (&Sset_terminal_coding_system
);
3438 defsubr (&Sterminal_coding_system
);
3439 defsubr (&Sset_keyboard_coding_system
);
3440 defsubr (&Skeyboard_coding_system
);
3441 defsubr (&Sfind_coding_system
);
3443 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list
,
3444 "List of coding-categories (symbols) ordered by priority.");
3448 Vcoding_category_list
= Qnil
;
3449 for (i
= CODING_CATEGORY_IDX_MAX
- 1; i
>= 0; i
--)
3450 Vcoding_category_list
3451 = Fcons (coding_category_table
[i
], Vcoding_category_list
);
3454 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read
,
3455 "A variable of internal use only.\n\
3456 If the value is a coding system, it is used for decoding on read operation.\n\
3457 If not, an appropriate element in `coding-system-alist' (which see) is used.");
3458 Vcoding_system_for_read
= Qnil
;
3460 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write
,
3461 "A variable of internal use only.\n\
3462 If the value is a coding system, it is used for encoding on write operation.\n\
3463 If not, an appropriate element in `coding-system-alist' (which see) is used.");
3464 Vcoding_system_for_write
= Qnil
;
3466 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used
,
3467 "Coding-system used in the latest file or process I/O.");
3468 Vlast_coding_system_used
= Qnil
;
3470 DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist
,
3471 "Nested alist to decide a coding system for a specific I/O operation.\n\
3472 The format is ((OPERATION . ((REGEXP . CODING-SYSTEMS) ...)) ...).\n\
3474 OPERATION is one of the following Emacs I/O primitives:\n\
3475 For file I/O, insert-file-contents and write-region.\n\
3476 For process I/O, call-process, call-process-region, and start-process.\n\
3477 For network I/O, open-network-stream.\n\
3478 In addition, for process I/O, `process-argument' can be specified for\n\
3479 encoding arguments of the process.\n\
3481 REGEXP is a regular expression matching a target of OPERATION, where\n\
3482 target is a file name for file I/O operations, a process name for\n\
3483 process I/O operations, or a service name for network I/O\n\
3484 operations. REGEXP might be a port number for network I/O operation.\n\
3486 CODING-SYSTEMS is a cons of coding systems to encode and decode\n\
3487 character code on OPERATION, or a function symbol returning the cons.\n\
3488 See the documentation of `find-coding-system' for more detail.");
3489 Vcoding_system_alist
= Qnil
;
3491 DEFVAR_INT ("eol-mnemonic-unix", &eol_mnemonic_unix
,
3492 "Mnemonic character indicating UNIX-like end-of-line format (i.e. LF) .");
3493 eol_mnemonic_unix
= '.';
3495 DEFVAR_INT ("eol-mnemonic-dos", &eol_mnemonic_dos
,
3496 "Mnemonic character indicating DOS-like end-of-line format (i.e. CRLF).");
3497 eol_mnemonic_dos
= ':';
3499 DEFVAR_INT ("eol-mnemonic-mac", &eol_mnemonic_mac
,
3500 "Mnemonic character indicating MAC-like end-of-line format (i.e. CR).");
3501 eol_mnemonic_mac
= '\'';
3503 DEFVAR_INT ("eol-mnemonic-undecided", &eol_mnemonic_undecided
,
3504 "Mnemonic character indicating end-of-line format is not yet decided.");
3505 eol_mnemonic_undecided
= '-';
3507 DEFVAR_LISP ("alternate-charset-table", &Valternate_charset_table
,
3508 "Alist of charsets vs the alternate charsets.\n\
3509 While decoding, if a charset (car part of an element) is found,\n\
3510 decode it as the alternate charset (cdr part of the element).");
3511 Valternate_charset_table
= Qnil
;
3513 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_alist
,
3514 "Alist of charsets vs revision numbers.\n\
3515 While encoding, if a charset (car part of an element) is found,\n\
3516 designate it with the escape sequence identifing revision (cdr part of the element).");
3517 Vcharset_revision_alist
= Qnil
;