1 /* Coding system handler (conversion, detection, and etc).
2 Copyright (C) 1995, 1997 Electrotechnical Laboratory, JAPAN.
3 Licensed to the Free Software Foundation.
5 This file is part of GNU Emacs.
7 GNU Emacs 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 GNU Emacs 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 GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /*** TABLE OF CONTENTS ***
25 2. Emacs' internal format (emacs-mule) handlers
27 4. Shift-JIS and BIG5 handlers
28 5. End-of-line handlers
29 6. C library functions
30 7. Emacs Lisp library functions
35 /*** GENERAL NOTE on CODING SYSTEM ***
37 Coding system is an encoding mechanism of one or more character
38 sets. Here's a list of coding systems which Emacs can handle. When
39 we say "decode", it means converting some other coding system to
40 Emacs' internal format (emacs-internal), and when we say "encode",
41 it means converting the coding system emacs-mule to some other
44 0. Emacs' internal format (emacs-mule)
46 Emacs itself holds a multi-lingual character in a buffer and a string
47 in a special format. Details are described in the section 2.
51 The most famous coding system for multiple character sets. X's
52 Compound Text, various EUCs (Extended Unix Code), and such coding
53 systems used in Internet communication as ISO-2022-JP are all
54 variants of ISO2022. Details are described in the section 3.
56 2. SJIS (or Shift-JIS or MS-Kanji-Code)
58 A coding system to encode character sets: ASCII, JISX0201, and
59 JISX0208. Widely used for PC's in Japan. Details are described in
64 A coding system to encode character sets: ASCII and Big5. Widely
65 used by Chinese (mainly in Taiwan and Hong Kong). Details are
66 described in the section 4. In this file, when written as "BIG5"
67 (all uppercase), it means the coding system, and when written as
68 "Big5" (capitalized), it means the character set.
72 If a user want to read/write a text encoded in a coding system not
73 listed above, he can supply a decoder and an encoder for it in CCL
74 (Code Conversion Language) programs. Emacs executes the CCL program
75 while reading/writing.
77 Emacs represent a coding-system by a Lisp symbol that has a property
78 `coding-system'. But, before actually using the coding-system, the
79 information about it is set in a structure of type `struct
80 coding_system' for rapid processing. See the section 6 for more
85 /*** GENERAL NOTES on END-OF-LINE FORMAT ***
87 How end-of-line of a text is encoded depends on a system. For
88 instance, Unix's format is just one byte of `line-feed' code,
89 whereas DOS's format is two bytes sequence of `carriage-return' and
90 `line-feed' codes. MacOS's format is one byte of `carriage-return'.
92 Since how characters in a text is encoded and how end-of-line is
93 encoded is independent, any coding system described above can take
94 any format of end-of-line. So, Emacs has information of format of
95 end-of-line in each coding-system. See the section 6 for more
100 /*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
102 These functions check if a text between SRC and SRC_END is encoded
103 in the coding system category XXX. Each returns an integer value in
104 which appropriate flag bits for the category XXX is set. The flag
105 bits are defined in macros CODING_CATEGORY_MASK_XXX. Below is the
106 template of these functions. */
109 detect_coding_emacs_mule (src
, src_end
)
110 unsigned char *src
, *src_end
;
116 /*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
118 These functions decode SRC_BYTES length text at SOURCE encoded in
119 CODING to Emacs' internal format (emacs-mule). The resulting text
120 goes to a place pointed by DESTINATION, the length of which should
121 not exceed DST_BYTES. The bytes actually processed is returned as
122 *CONSUMED. The return value is the length of the decoded text.
123 Below is a template of these functions. */
125 decode_coding_XXX (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
126 struct coding_system
*coding
;
127 unsigned char *source
, *destination
;
128 int src_bytes
, dst_bytes
;
135 /*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
137 These functions encode SRC_BYTES length text at SOURCE of Emacs'
138 internal format (emacs-mule) to CODING. The resulting text goes to
139 a place pointed by DESTINATION, the length of which should not
140 exceed DST_BYTES. The bytes actually processed is returned as
141 *CONSUMED. The return value is the length of the encoded text.
142 Below is a template of these functions. */
144 encode_coding_XXX (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
145 struct coding_system
*coding
;
146 unsigned char *source
, *destination
;
147 int src_bytes
, dst_bytes
;
154 /*** COMMONLY USED MACROS ***/
156 /* The following three macros ONE_MORE_BYTE, TWO_MORE_BYTES, and
157 THREE_MORE_BYTES safely get one, two, and three bytes from the
158 source text respectively. If there are not enough bytes in the
159 source, they jump to `label_end_of_loop'. The caller should set
160 variables `src' and `src_end' to appropriate areas in advance. */
162 #define ONE_MORE_BYTE(c1) \
167 goto label_end_of_loop; \
170 #define TWO_MORE_BYTES(c1, c2) \
172 if (src + 1 < src_end) \
173 c1 = *src++, c2 = *src++; \
175 goto label_end_of_loop; \
178 #define THREE_MORE_BYTES(c1, c2, c3) \
180 if (src + 2 < src_end) \
181 c1 = *src++, c2 = *src++, c3 = *src++; \
183 goto label_end_of_loop; \
186 /* The following three macros DECODE_CHARACTER_ASCII,
187 DECODE_CHARACTER_DIMENSION1, and DECODE_CHARACTER_DIMENSION2 put
188 the multi-byte form of a character of each class at the place
189 pointed by `dst'. The caller should set the variable `dst' to
190 point to an appropriate area and the variable `coding' to point to
191 the coding-system of the currently decoding text in advance. */
193 /* Decode one ASCII character C. */
195 #define DECODE_CHARACTER_ASCII(c) \
197 if (COMPOSING_P (coding->composing)) \
198 *dst++ = 0xA0, *dst++ = (c) | 0x80; \
203 /* Decode one DIMENSION1 character of which charset is CHARSET and
204 position-code is C. */
206 #define DECODE_CHARACTER_DIMENSION1(charset, c) \
208 unsigned char leading_code = CHARSET_LEADING_CODE_BASE (charset); \
209 if (COMPOSING_P (coding->composing)) \
210 *dst++ = leading_code + 0x20; \
212 *dst++ = leading_code; \
213 if (leading_code = CHARSET_LEADING_CODE_EXT (charset)) \
214 *dst++ = leading_code; \
215 *dst++ = (c) | 0x80; \
218 /* Decode one DIMENSION2 character of which charset is CHARSET and
219 position-codes are C1 and C2. */
221 #define DECODE_CHARACTER_DIMENSION2(charset, c1, c2) \
223 DECODE_CHARACTER_DIMENSION1 (charset, c1); \
224 *dst++ = (c2) | 0x80; \
228 /*** 1. Preamble ***/
242 #else /* not emacs */
246 #endif /* not emacs */
248 Lisp_Object Qcoding_system
, Qeol_type
;
249 Lisp_Object Qbuffer_file_coding_system
;
250 Lisp_Object Qpost_read_conversion
, Qpre_write_conversion
;
252 extern Lisp_Object Qinsert_file_contents
, Qwrite_region
;
253 Lisp_Object Qcall_process
, Qcall_process_region
, Qprocess_argument
;
254 Lisp_Object Qstart_process
, Qopen_network_stream
;
255 Lisp_Object Qtarget_idx
;
257 /* Mnemonic character of each format of end-of-line. */
258 int eol_mnemonic_unix
, eol_mnemonic_dos
, eol_mnemonic_mac
;
259 /* Mnemonic character to indicate format of end-of-line is not yet
261 int eol_mnemonic_undecided
;
265 Lisp_Object Qcoding_system_spec
, Qcoding_system_p
, Qcoding_system_error
;
267 /* Coding-systems are handed between Emacs Lisp programs and C internal
268 routines by the following three variables. */
269 /* Coding-system for reading files and receiving data from process. */
270 Lisp_Object Vcoding_system_for_read
;
271 /* Coding-system for writing files and sending data to process. */
272 Lisp_Object Vcoding_system_for_write
;
273 /* Coding-system actually used in the latest I/O. */
274 Lisp_Object Vlast_coding_system_used
;
276 /* Coding-system of what terminal accept for displaying. */
277 struct coding_system terminal_coding
;
279 /* Coding-system of what is sent from terminal keyboard. */
280 struct coding_system keyboard_coding
;
282 Lisp_Object Vfile_coding_system_alist
;
283 Lisp_Object Vprocess_coding_system_alist
;
284 Lisp_Object Vnetwork_coding_system_alist
;
288 Lisp_Object Qcoding_category_index
;
290 /* List of symbols `coding-category-xxx' ordered by priority. */
291 Lisp_Object Vcoding_category_list
;
293 /* Table of coding-systems currently assigned to each coding-category. */
294 Lisp_Object coding_category_table
[CODING_CATEGORY_IDX_MAX
];
296 /* Table of names of symbol for each coding-category. */
297 char *coding_category_name
[CODING_CATEGORY_IDX_MAX
] = {
298 "coding-category-emacs-mule",
299 "coding-category-sjis",
300 "coding-category-iso-7",
301 "coding-category-iso-8-1",
302 "coding-category-iso-8-2",
303 "coding-category-iso-else",
304 "coding-category-big5",
305 "coding-category-binary"
308 /* Flag to tell if we look up unification table on character code
310 Lisp_Object Venable_character_unification
;
311 /* Standard unification table to look up on decoding (reading). */
312 Lisp_Object Vstandard_character_unification_table_for_decode
;
313 /* Standard unification table to look up on encoding (writing). */
314 Lisp_Object Vstandard_character_unification_table_for_encode
;
316 Lisp_Object Qcharacter_unification_table
;
317 Lisp_Object Qcharacter_unification_table_for_decode
;
318 Lisp_Object Qcharacter_unification_table_for_encode
;
320 /* Alist of charsets vs revision number. */
321 Lisp_Object Vcharset_revision_alist
;
323 /* Default coding systems used for process I/O. */
324 Lisp_Object Vdefault_process_coding_system
;
327 /*** 2. Emacs internal format (emacs-mule) handlers ***/
329 /* Emacs' internal format for encoding multiple character sets is a
330 kind of multi-byte encoding, i.e. encoding a character by a sequence
331 of one-byte codes of variable length. ASCII characters and control
332 characters (e.g. `tab', `newline') are represented by one-byte as
333 is. It takes the range 0x00 through 0x7F. The other characters
334 are represented by a sequence of `base leading-code', optional
335 `extended leading-code', and one or two `position-code's. Length
336 of the sequence is decided by the base leading-code. Leading-code
337 takes the range 0x80 through 0x9F, whereas extended leading-code
338 and position-code take the range 0xA0 through 0xFF. See the
339 document of `charset.h' for more detail about leading-code and
342 There's one exception in this rule. Special leading-code
343 `leading-code-composition' denotes that the following several
344 characters should be composed into one character. Leading-codes of
345 components (except for ASCII) are added 0x20. An ASCII character
346 component is represented by a 2-byte sequence of `0xA0' and
347 `ASCII-code + 0x80'. See also the document in `charset.h' for the
348 detail of composite character. Hence, we can summarize the code
351 --- CODE RANGE of Emacs' internal format ---
352 (character set) (range)
354 ELSE (1st byte) 0x80 .. 0x9F
355 (rest bytes) 0xA0 .. 0xFF
356 ---------------------------------------------
360 enum emacs_code_class_type emacs_code_class
[256];
362 /* Go to the next statement only if *SRC is accessible and the code is
363 greater than 0xA0. */
364 #define CHECK_CODE_RANGE_A0_FF \
366 if (src >= src_end) \
367 goto label_end_of_switch; \
368 else if (*src++ < 0xA0) \
372 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
373 Check if a text is encoded in Emacs' internal format. If it is,
374 return CODING_CATEGORY_MASK_EMASC_MULE, else return 0. */
377 detect_coding_emacs_mule (src
, src_end
)
378 unsigned char *src
, *src_end
;
383 while (src
< src_end
)
395 switch (emacs_code_class
[c
])
397 case EMACS_ascii_code
:
398 case EMACS_linefeed_code
:
401 case EMACS_control_code
:
402 if (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
406 case EMACS_invalid_code
:
409 case EMACS_leading_code_composition
: /* c == 0x80 */
411 CHECK_CODE_RANGE_A0_FF
;
416 case EMACS_leading_code_4
:
417 CHECK_CODE_RANGE_A0_FF
;
418 /* fall down to check it two more times ... */
420 case EMACS_leading_code_3
:
421 CHECK_CODE_RANGE_A0_FF
;
422 /* fall down to check it one more time ... */
424 case EMACS_leading_code_2
:
425 CHECK_CODE_RANGE_A0_FF
;
433 return CODING_CATEGORY_MASK_EMACS_MULE
;
437 /*** 3. ISO2022 handlers ***/
439 /* The following note describes the coding system ISO2022 briefly.
440 Since the intension of this note is to help understanding of the
441 programs in this file, some parts are NOT ACCURATE or OVERLY
442 SIMPLIFIED. For the thorough understanding, please refer to the
443 original document of ISO2022.
445 ISO2022 provides many mechanisms to encode several character sets
446 in 7-bit and 8-bit environment. If one choose 7-bite environment,
447 all text is encoded by codes of less than 128. This may make the
448 encoded text a little bit longer, but the text get more stability
449 to pass through several gateways (some of them split MSB off).
451 There are two kind of character set: control character set and
452 graphic character set. The former contains control characters such
453 as `newline' and `escape' to provide control functions (control
454 functions are provided also by escape sequence). The latter
455 contains graphic characters such as ' A' and '-'. Emacs recognizes
456 two control character sets and many graphic character sets.
458 Graphic character sets are classified into one of the following
459 four classes, DIMENSION1_CHARS94, DIMENSION1_CHARS96,
460 DIMENSION2_CHARS94, DIMENSION2_CHARS96 according to the number of
461 bytes (DIMENSION) and the number of characters in one dimension
462 (CHARS) of the set. In addition, each character set is assigned an
463 identification tag (called "final character" and denoted as <F>
464 here after) which is unique in each class. <F> of each character
465 set is decided by ECMA(*) when it is registered in ISO. Code range
466 of <F> is 0x30..0x7F (0x30..0x3F are for private use only).
468 Note (*): ECMA = European Computer Manufacturers Association
470 Here are examples of graphic character set [NAME(<F>)]:
471 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
472 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
473 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
474 o DIMENSION2_CHARS96 -- none for the moment
476 A code area (1byte=8bits) is divided into 4 areas, C0, GL, C1, and GR.
477 C0 [0x00..0x1F] -- control character plane 0
478 GL [0x20..0x7F] -- graphic character plane 0
479 C1 [0x80..0x9F] -- control character plane 1
480 GR [0xA0..0xFF] -- graphic character plane 1
482 A control character set is directly designated and invoked to C0 or
483 C1 by an escape sequence. The most common case is that ISO646's
484 control character set is designated/invoked to C0 and ISO6429's
485 control character set is designated/invoked to C1, and usually
486 these designations/invocations are omitted in a coded text. With
487 7-bit environment, only C0 can be used, and a control character for
488 C1 is encoded by an appropriate escape sequence to fit in the
489 environment. All control characters for C1 are defined the
490 corresponding escape sequences.
492 A graphic character set is at first designated to one of four
493 graphic registers (G0 through G3), then these graphic registers are
494 invoked to GL or GR. These designations and invocations can be
495 done independently. The most common case is that G0 is invoked to
496 GL, G1 is invoked to GR, and ASCII is designated to G0, and usually
497 these invocations and designations are omitted in a coded text.
498 With 7-bit environment, only GL can be used.
500 When a graphic character set of CHARS94 is invoked to GL, code 0x20
501 and 0x7F of GL area work as control characters SPACE and DEL
502 respectively, and code 0xA0 and 0xFF of GR area should not be used.
504 There are two ways of invocation: locking-shift and single-shift.
505 With locking-shift, the invocation lasts until the next different
506 invocation, whereas with single-shift, the invocation works only
507 for the following character and doesn't affect locking-shift.
508 Invocations are done by the following control characters or escape
511 ----------------------------------------------------------------------
512 function control char escape sequence description
513 ----------------------------------------------------------------------
514 SI (shift-in) 0x0F none invoke G0 to GL
515 SI (shift-out) 0x0E none invoke G1 to GL
516 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
517 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
518 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 into GL
519 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 into GL
520 ----------------------------------------------------------------------
521 The first four are for locking-shift. Control characters for these
522 functions are defined by macros ISO_CODE_XXX in `coding.h'.
524 Designations are done by the following escape sequences.
525 ----------------------------------------------------------------------
526 escape sequence description
527 ----------------------------------------------------------------------
528 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
529 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
530 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
531 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
532 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
533 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
534 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
535 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
536 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
537 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
538 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
539 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
540 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
541 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
542 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
543 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
544 ----------------------------------------------------------------------
546 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
547 of dimension 1, chars 94, and final character <F>, and etc.
549 Note (*): Although these designations are not allowed in ISO2022,
550 Emacs accepts them on decoding, and produces them on encoding
551 CHARS96 character set in a coding system which is characterized as
552 7-bit environment, non-locking-shift, and non-single-shift.
554 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
555 '(' can be omitted. We call this as "short-form" here after.
557 Now you may notice that there are a lot of ways for encoding the
558 same multilingual text in ISO2022. Actually, there exist many
559 coding systems such as Compound Text (used in X's inter client
560 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
561 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
562 localized platforms), and all of these are variants of ISO2022.
564 In addition to the above, Emacs handles two more kinds of escape
565 sequences: ISO6429's direction specification and Emacs' private
566 sequence for specifying character composition.
568 ISO6429's direction specification takes the following format:
569 o CSI ']' -- end of the current direction
570 o CSI '0' ']' -- end of the current direction
571 o CSI '1' ']' -- start of left-to-right text
572 o CSI '2' ']' -- start of right-to-left text
573 The control character CSI (0x9B: control sequence introducer) is
574 abbreviated to the escape sequence ESC '[' in 7-bit environment.
576 Character composition specification takes the following format:
577 o ESC '0' -- start character composition
578 o ESC '1' -- end character composition
579 Since these are not standard escape sequences of any ISO, the use
580 of them for these meaning is restricted to Emacs only. */
582 enum iso_code_class_type iso_code_class
[256];
584 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
585 Check if a text is encoded in ISO2022. If it is, returns an
586 integer in which appropriate flag bits any of:
587 CODING_CATEGORY_MASK_ISO_7
588 CODING_CATEGORY_MASK_ISO_8_1
589 CODING_CATEGORY_MASK_ISO_8_2
590 CODING_CATEGORY_MASK_ISO_ELSE
591 are set. If a code which should never appear in ISO2022 is found,
595 detect_coding_iso2022 (src
, src_end
)
596 unsigned char *src
, *src_end
;
598 int mask
= (CODING_CATEGORY_MASK_ISO_7
599 | CODING_CATEGORY_MASK_ISO_8_1
600 | CODING_CATEGORY_MASK_ISO_8_2
601 | CODING_CATEGORY_MASK_ISO_ELSE
);
602 int g1
= 0; /* 1 iff designating to G1. */
605 while (src
< src_end
)
615 && ((c
>= '(' && c
<= '/')
616 || c
== '$' && ((*src
>= '(' && *src
<= '/')
617 || (*src
>= '@' && *src
<= 'B'))))
619 /* Valid designation sequence. */
620 if (c
== ')' || (c
== '$' && *src
== ')'))
623 mask
&= ~CODING_CATEGORY_MASK_ISO_7
;
628 else if (c
== 'N' || c
== 'O' || c
== 'n' || c
== 'o')
629 return CODING_CATEGORY_MASK_ISO_ELSE
;
634 return CODING_CATEGORY_MASK_ISO_ELSE
;
640 mask
&= ~CODING_CATEGORY_MASK_ISO_7
;
652 mask
&= ~CODING_CATEGORY_MASK_ISO_7
;
653 while (src
< src_end
&& *src
>= 0xA0)
655 if (count
& 1 && src
< src_end
)
656 mask
&= ~CODING_CATEGORY_MASK_ISO_8_2
;
665 /* Decode a character of which charset is CHARSET and the 1st position
666 code is C1. If dimension of CHARSET is 2, the 2nd position code is
667 fetched from SRC and set to C2. If CHARSET is negative, it means
668 that we are decoding ill formed text, and what we can do is just to
671 #define DECODE_ISO_CHARACTER(charset, c1) \
673 int c_alt, charset_alt = (charset); \
674 if (COMPOSING_HEAD_P (coding->composing)) \
676 *dst++ = LEADING_CODE_COMPOSITION; \
677 if (COMPOSING_WITH_RULE_P (coding->composing)) \
678 /* To tell composition rules are embeded. */ \
680 coding->composing += 2; \
682 if ((charset) >= 0) \
684 if (CHARSET_DIMENSION (charset) == 2) \
685 ONE_MORE_BYTE (c2); \
686 if (!NILP (unification_table) \
687 && ((c_alt = unify_char (unification_table, \
688 -1, (charset), c1, c2)) >= 0)) \
689 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
691 if (charset_alt == CHARSET_ASCII || charset_alt < 0) \
692 DECODE_CHARACTER_ASCII (c1); \
693 else if (CHARSET_DIMENSION (charset_alt) == 1) \
694 DECODE_CHARACTER_DIMENSION1 (charset_alt, c1); \
696 DECODE_CHARACTER_DIMENSION2 (charset_alt, c1, c2); \
697 if (COMPOSING_WITH_RULE_P (coding->composing)) \
698 /* To tell a composition rule follows. */ \
699 coding->composing = COMPOSING_WITH_RULE_RULE; \
702 /* Set designation state into CODING. */
703 #define DECODE_DESIGNATION(reg, dimension, chars, final_char) \
705 int charset = ISO_CHARSET_TABLE (make_number (dimension), \
706 make_number (chars), \
707 make_number (final_char)); \
710 if (coding->direction == 1 \
711 && CHARSET_REVERSE_CHARSET (charset) >= 0) \
712 charset = CHARSET_REVERSE_CHARSET (charset); \
713 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
717 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
720 decode_coding_iso2022 (coding
, source
, destination
,
721 src_bytes
, dst_bytes
, consumed
)
722 struct coding_system
*coding
;
723 unsigned char *source
, *destination
;
724 int src_bytes
, dst_bytes
;
727 unsigned char *src
= source
;
728 unsigned char *src_end
= source
+ src_bytes
;
729 unsigned char *dst
= destination
;
730 unsigned char *dst_end
= destination
+ dst_bytes
;
731 /* Since the maximum bytes produced by each loop is 7, we subtract 6
732 from DST_END to assure that overflow checking is necessary only
733 at the head of loop. */
734 unsigned char *adjusted_dst_end
= dst_end
- 6;
736 /* Charsets invoked to graphic plane 0 and 1 respectively. */
737 int charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
738 int charset1
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 1);
739 Lisp_Object unification_table
740 = coding
->character_unification_table_for_decode
;
742 if (!NILP (Venable_character_unification
) && NILP (unification_table
))
743 unification_table
= Vstandard_character_unification_table_for_decode
;
745 while (src
< src_end
&& dst
< adjusted_dst_end
)
747 /* SRC_BASE remembers the start position in source in each loop.
748 The loop will be exited when there's not enough source text
749 to analyze long escape sequence or 2-byte code (within macros
750 ONE_MORE_BYTE or TWO_MORE_BYTES). In that case, SRC is reset
751 to SRC_BASE before exiting. */
752 unsigned char *src_base
= src
;
755 switch (iso_code_class
[c1
])
757 case ISO_0x20_or_0x7F
:
758 if (!coding
->composing
759 && (charset0
< 0 || CHARSET_CHARS (charset0
) == 94))
761 /* This is SPACE or DEL. */
765 /* This is a graphic character, we fall down ... */
767 case ISO_graphic_plane_0
:
768 if (coding
->composing
== COMPOSING_WITH_RULE_RULE
)
770 /* This is a composition rule. */
772 coding
->composing
= COMPOSING_WITH_RULE_TAIL
;
775 DECODE_ISO_CHARACTER (charset0
, c1
);
778 case ISO_0xA0_or_0xFF
:
779 if (charset1
< 0 || CHARSET_CHARS (charset1
) == 94)
785 /* This is a graphic character, we fall down ... */
787 case ISO_graphic_plane_1
:
788 DECODE_ISO_CHARACTER (charset1
, c1
);
791 case ISO_control_code
:
792 /* All ISO2022 control characters in this class have the
793 same representation in Emacs internal format. */
797 case ISO_carriage_return
:
798 if (coding
->eol_type
== CODING_EOL_CR
)
802 else if (coding
->eol_type
== CODING_EOL_CRLF
)
805 if (c1
== ISO_CODE_LF
)
820 if (CODING_SPEC_ISO_DESIGNATION (coding
, 1) < 0)
821 goto label_invalid_escape_sequence
;
822 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 1;
823 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
827 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 0;
828 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
831 case ISO_single_shift_2_7
:
832 case ISO_single_shift_2
:
833 /* SS2 is handled as an escape sequence of ESC 'N' */
835 goto label_escape_sequence
;
837 case ISO_single_shift_3
:
838 /* SS2 is handled as an escape sequence of ESC 'O' */
840 goto label_escape_sequence
;
842 case ISO_control_sequence_introducer
:
843 /* CSI is handled as an escape sequence of ESC '[' ... */
845 goto label_escape_sequence
;
849 label_escape_sequence
:
850 /* Escape sequences handled by Emacs are invocation,
851 designation, direction specification, and character
852 composition specification. */
855 case '&': /* revision of following character set */
857 if (!(c1
>= '@' && c1
<= '~'))
858 goto label_invalid_escape_sequence
;
860 if (c1
!= ISO_CODE_ESC
)
861 goto label_invalid_escape_sequence
;
863 goto label_escape_sequence
;
865 case '$': /* designation of 2-byte character set */
867 if (c1
>= '@' && c1
<= 'B')
868 { /* designation of JISX0208.1978, GB2312.1980,
870 DECODE_DESIGNATION (0, 2, 94, c1
);
872 else if (c1
>= 0x28 && c1
<= 0x2B)
873 { /* designation of DIMENSION2_CHARS94 character set */
875 DECODE_DESIGNATION (c1
- 0x28, 2, 94, c2
);
877 else if (c1
>= 0x2C && c1
<= 0x2F)
878 { /* designation of DIMENSION2_CHARS96 character set */
880 DECODE_DESIGNATION (c1
- 0x2C, 2, 96, c2
);
883 goto label_invalid_escape_sequence
;
886 case 'n': /* invocation of locking-shift-2 */
887 if (CODING_SPEC_ISO_DESIGNATION (coding
, 2) < 0)
888 goto label_invalid_escape_sequence
;
889 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 2;
890 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
893 case 'o': /* invocation of locking-shift-3 */
894 if (CODING_SPEC_ISO_DESIGNATION (coding
, 3) < 0)
895 goto label_invalid_escape_sequence
;
896 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 3;
897 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
900 case 'N': /* invocation of single-shift-2 */
901 if (CODING_SPEC_ISO_DESIGNATION (coding
, 2) < 0)
902 goto label_invalid_escape_sequence
;
904 charset
= CODING_SPEC_ISO_DESIGNATION (coding
, 2);
905 DECODE_ISO_CHARACTER (charset
, c1
);
908 case 'O': /* invocation of single-shift-3 */
909 if (CODING_SPEC_ISO_DESIGNATION (coding
, 3) < 0)
910 goto label_invalid_escape_sequence
;
912 charset
= CODING_SPEC_ISO_DESIGNATION (coding
, 3);
913 DECODE_ISO_CHARACTER (charset
, c1
);
916 case '0': /* start composing without embeded rules */
917 coding
->composing
= COMPOSING_NO_RULE_HEAD
;
920 case '1': /* end composing */
921 coding
->composing
= COMPOSING_NO
;
924 case '2': /* start composing with embeded rules */
925 coding
->composing
= COMPOSING_WITH_RULE_HEAD
;
928 case '[': /* specification of direction */
929 /* For the moment, nested direction is not supported.
930 So, the value of `coding->direction' is 0 or 1: 0
931 means left-to-right, 1 means right-to-left. */
935 case ']': /* end of the current direction */
936 coding
->direction
= 0;
938 case '0': /* end of the current direction */
939 case '1': /* start of left-to-right direction */
942 coding
->direction
= 0;
944 goto label_invalid_escape_sequence
;
947 case '2': /* start of right-to-left direction */
950 coding
->direction
= 1;
952 goto label_invalid_escape_sequence
;
956 goto label_invalid_escape_sequence
;
961 if (c1
>= 0x28 && c1
<= 0x2B)
962 { /* designation of DIMENSION1_CHARS94 character set */
964 DECODE_DESIGNATION (c1
- 0x28, 1, 94, c2
);
966 else if (c1
>= 0x2C && c1
<= 0x2F)
967 { /* designation of DIMENSION1_CHARS96 character set */
969 DECODE_DESIGNATION (c1
- 0x2C, 1, 96, c2
);
973 goto label_invalid_escape_sequence
;
976 /* We must update these variables now. */
977 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
978 charset1
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 1);
981 label_invalid_escape_sequence
:
983 int length
= src
- src_base
;
985 bcopy (src_base
, dst
, length
);
992 coding
->carryover_size
= src
- src_base
;
993 bcopy (src_base
, coding
->carryover
, coding
->carryover_size
);
998 /* If this is the last block of the text to be decoded, we had
999 better just flush out all remaining codes in the text although
1000 they are not valid characters. */
1001 if (coding
->last_block
)
1003 bcopy (src
, dst
, src_end
- src
);
1004 dst
+= (src_end
- src
);
1007 *consumed
= src
- source
;
1008 return dst
- destination
;
1011 /* ISO2022 encoding staffs. */
1014 It is not enough to say just "ISO2022" on encoding, but we have to
1015 specify more details. In Emacs, each coding-system of ISO2022
1016 variant has the following specifications:
1017 1. Initial designation to G0 thru G3.
1018 2. Allows short-form designation?
1019 3. ASCII should be designated to G0 before control characters?
1020 4. ASCII should be designated to G0 at end of line?
1021 5. 7-bit environment or 8-bit environment?
1022 6. Use locking-shift?
1023 7. Use Single-shift?
1024 And the following two are only for Japanese:
1025 8. Use ASCII in place of JIS0201-1976-Roman?
1026 9. Use JISX0208-1983 in place of JISX0208-1978?
1027 These specifications are encoded in `coding->flags' as flag bits
1028 defined by macros CODING_FLAG_ISO_XXX. See `coding.h' for more
1032 /* Produce codes (escape sequence) for designating CHARSET to graphic
1033 register REG. If <final-char> of CHARSET is '@', 'A', or 'B' and
1034 the coding system CODING allows, produce designation sequence of
1037 #define ENCODE_DESIGNATION(charset, reg, coding) \
1039 unsigned char final_char = CHARSET_ISO_FINAL_CHAR (charset); \
1040 char *intermediate_char_94 = "()*+"; \
1041 char *intermediate_char_96 = ",-./"; \
1043 = Fassq (make_number (charset), Vcharset_revision_alist); \
1044 if (! NILP (temp)) \
1046 *dst++ = ISO_CODE_ESC; \
1048 *dst++ = XINT (XCONS (temp)->cdr) + '@'; \
1050 *dst++ = ISO_CODE_ESC; \
1051 if (CHARSET_DIMENSION (charset) == 1) \
1053 if (CHARSET_CHARS (charset) == 94) \
1054 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
1056 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
1061 if (CHARSET_CHARS (charset) == 94) \
1063 if (! (coding->flags & CODING_FLAG_ISO_SHORT_FORM) \
1065 || final_char < '@' || final_char > 'B') \
1066 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
1069 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
1071 *dst++ = final_char; \
1072 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
1075 /* The following two macros produce codes (control character or escape
1076 sequence) for ISO2022 single-shift functions (single-shift-2 and
1079 #define ENCODE_SINGLE_SHIFT_2 \
1081 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1082 *dst++ = ISO_CODE_ESC, *dst++ = 'N'; \
1084 *dst++ = ISO_CODE_SS2; \
1085 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
1088 #define ENCODE_SINGLE_SHIFT_3 \
1090 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1091 *dst++ = ISO_CODE_ESC, *dst++ = 'O'; \
1093 *dst++ = ISO_CODE_SS3; \
1094 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
1097 /* The following four macros produce codes (control character or
1098 escape sequence) for ISO2022 locking-shift functions (shift-in,
1099 shift-out, locking-shift-2, and locking-shift-3). */
1101 #define ENCODE_SHIFT_IN \
1103 *dst++ = ISO_CODE_SI; \
1104 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0; \
1107 #define ENCODE_SHIFT_OUT \
1109 *dst++ = ISO_CODE_SO; \
1110 CODING_SPEC_ISO_INVOCATION (coding, 0) = 1; \
1113 #define ENCODE_LOCKING_SHIFT_2 \
1115 *dst++ = ISO_CODE_ESC, *dst++ = 'n'; \
1116 CODING_SPEC_ISO_INVOCATION (coding, 0) = 2; \
1119 #define ENCODE_LOCKING_SHIFT_3 \
1121 *dst++ = ISO_CODE_ESC, *dst++ = 'o'; \
1122 CODING_SPEC_ISO_INVOCATION (coding, 0) = 3; \
1125 /* Produce codes for a DIMENSION1 character of which character set is
1126 CHARSET and position-code is C1. Designation and invocation
1127 sequences are also produced in advance if necessary. */
1130 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
1132 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
1134 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1135 *dst++ = c1 & 0x7F; \
1137 *dst++ = c1 | 0x80; \
1138 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
1141 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
1143 *dst++ = c1 & 0x7F; \
1146 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
1148 *dst++ = c1 | 0x80; \
1152 /* Since CHARSET is not yet invoked to any graphic planes, we \
1153 must invoke it, or, at first, designate it to some graphic \
1154 register. Then repeat the loop to actually produce the \
1156 dst = encode_invocation_designation (charset, coding, dst); \
1159 /* Produce codes for a DIMENSION2 character of which character set is
1160 CHARSET and position-codes are C1 and C2. Designation and
1161 invocation codes are also produced in advance if necessary. */
1163 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
1165 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
1167 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1168 *dst++ = c1 & 0x7F, *dst++ = c2 & 0x7F; \
1170 *dst++ = c1 | 0x80, *dst++ = c2 | 0x80; \
1171 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
1174 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
1176 *dst++ = c1 & 0x7F, *dst++= c2 & 0x7F; \
1179 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
1181 *dst++ = c1 | 0x80, *dst++= c2 | 0x80; \
1185 /* Since CHARSET is not yet invoked to any graphic planes, we \
1186 must invoke it, or, at first, designate it to some graphic \
1187 register. Then repeat the loop to actually produce the \
1189 dst = encode_invocation_designation (charset, coding, dst); \
1192 #define ENCODE_ISO_CHARACTER(charset, c1, c2) \
1194 int c_alt, charset_alt; \
1195 if (!NILP (unification_table) \
1196 && ((c_alt = unify_char (unification_table, -1, charset, c1, c2)) \
1198 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
1200 charset_alt = charset; \
1201 if (CHARSET_DIMENSION (charset_alt) == 1) \
1202 ENCODE_ISO_CHARACTER_DIMENSION1 (charset_alt, c1); \
1204 ENCODE_ISO_CHARACTER_DIMENSION2 (charset_alt, c1, c2); \
1207 /* Produce designation and invocation codes at a place pointed by DST
1208 to use CHARSET. The element `spec.iso2022' of *CODING is updated.
1212 encode_invocation_designation (charset
, coding
, dst
)
1214 struct coding_system
*coding
;
1217 int reg
; /* graphic register number */
1219 /* At first, check designations. */
1220 for (reg
= 0; reg
< 4; reg
++)
1221 if (charset
== CODING_SPEC_ISO_DESIGNATION (coding
, reg
))
1226 /* CHARSET is not yet designated to any graphic registers. */
1227 /* At first check the requested designation. */
1228 reg
= CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
);
1229 if (reg
== CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
)
1230 /* Since CHARSET requests no special designation, designate it
1231 to graphic register 0. */
1234 ENCODE_DESIGNATION (charset
, reg
, coding
);
1237 if (CODING_SPEC_ISO_INVOCATION (coding
, 0) != reg
1238 && CODING_SPEC_ISO_INVOCATION (coding
, 1) != reg
)
1240 /* Since the graphic register REG is not invoked to any graphic
1241 planes, invoke it to graphic plane 0. */
1244 case 0: /* graphic register 0 */
1248 case 1: /* graphic register 1 */
1252 case 2: /* graphic register 2 */
1253 if (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
1254 ENCODE_SINGLE_SHIFT_2
;
1256 ENCODE_LOCKING_SHIFT_2
;
1259 case 3: /* graphic register 3 */
1260 if (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
1261 ENCODE_SINGLE_SHIFT_3
;
1263 ENCODE_LOCKING_SHIFT_3
;
1270 /* The following two macros produce codes for indicating composition. */
1271 #define ENCODE_COMPOSITION_NO_RULE_START *dst++ = ISO_CODE_ESC, *dst++ = '0'
1272 #define ENCODE_COMPOSITION_WITH_RULE_START *dst++ = ISO_CODE_ESC, *dst++ = '2'
1273 #define ENCODE_COMPOSITION_END *dst++ = ISO_CODE_ESC, *dst++ = '1'
1275 /* The following three macros produce codes for indicating direction
1277 #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
1279 if (coding->flags == CODING_FLAG_ISO_SEVEN_BITS) \
1280 *dst++ = ISO_CODE_ESC, *dst++ = '['; \
1282 *dst++ = ISO_CODE_CSI; \
1285 #define ENCODE_DIRECTION_R2L \
1286 ENCODE_CONTROL_SEQUENCE_INTRODUCER, *dst++ = '2', *dst++ = ']'
1288 #define ENCODE_DIRECTION_L2R \
1289 ENCODE_CONTROL_SEQUENCE_INTRODUCER, *dst++ = '0', *dst++ = ']'
1291 /* Produce codes for designation and invocation to reset the graphic
1292 planes and registers to initial state. */
1293 #define ENCODE_RESET_PLANE_AND_REGISTER \
1296 if (CODING_SPEC_ISO_INVOCATION (coding, 0) != 0) \
1298 for (reg = 0; reg < 4; reg++) \
1299 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg) >= 0 \
1300 && (CODING_SPEC_ISO_DESIGNATION (coding, reg) \
1301 != CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg))) \
1302 ENCODE_DESIGNATION \
1303 (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg), reg, coding); \
1306 /* Produce designation sequences of charsets in the line started from
1307 *SRC to a place pointed by DSTP.
1309 If the current block ends before any end-of-line, we may fail to
1310 find all the necessary *designations. */
1311 encode_designation_at_bol (coding
, table
, src
, src_end
, dstp
)
1312 struct coding_system
*coding
;
1314 unsigned char *src
, *src_end
, **dstp
;
1316 int charset
, c
, found
= 0, reg
;
1317 /* Table of charsets to be designated to each graphic register. */
1319 unsigned char *dst
= *dstp
;
1321 for (reg
= 0; reg
< 4; reg
++)
1324 while (src
< src_end
&& *src
!= '\n' && found
< 4)
1326 int bytes
= BYTES_BY_CHAR_HEAD (*src
);
1329 charset
= CHARSET_AT (src
);
1334 SPLIT_STRING(src
, bytes
, charset
, c1
, c2
);
1335 if ((c_alt
= unify_char (table
, -1, charset
, c1
, c2
)) >= 0)
1336 charset
= CHAR_CHARSET (c_alt
);
1339 reg
= CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
);
1340 if (r
[reg
] == CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
)
1351 for (reg
= 0; reg
< 4; reg
++)
1353 && CODING_SPEC_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
1354 ENCODE_DESIGNATION (r
[reg
], reg
, coding
);
1359 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
1362 encode_coding_iso2022 (coding
, source
, destination
,
1363 src_bytes
, dst_bytes
, consumed
)
1364 struct coding_system
*coding
;
1365 unsigned char *source
, *destination
;
1366 int src_bytes
, dst_bytes
;
1369 unsigned char *src
= source
;
1370 unsigned char *src_end
= source
+ src_bytes
;
1371 unsigned char *dst
= destination
;
1372 unsigned char *dst_end
= destination
+ dst_bytes
;
1373 /* Since the maximum bytes produced by each loop is 20, we subtract 19
1374 from DST_END to assure overflow checking is necessary only at the
1376 unsigned char *adjusted_dst_end
= dst_end
- 19;
1377 Lisp_Object unification_table
1378 = coding
->character_unification_table_for_encode
;
1380 if (!NILP (Venable_character_unification
) && NILP (unification_table
))
1381 unification_table
= Vstandard_character_unification_table_for_encode
;
1383 while (src
< src_end
&& dst
< adjusted_dst_end
)
1385 /* SRC_BASE remembers the start position in source in each loop.
1386 The loop will be exited when there's not enough source text
1387 to analyze multi-byte codes (within macros ONE_MORE_BYTE,
1388 TWO_MORE_BYTES, and THREE_MORE_BYTES). In that case, SRC is
1389 reset to SRC_BASE before exiting. */
1390 unsigned char *src_base
= src
;
1391 int charset
, c1
, c2
, c3
, c4
;
1393 if (coding
->flags
& CODING_FLAG_ISO_DESIGNATE_AT_BOL
1394 && CODING_SPEC_ISO_BOL (coding
))
1396 /* We have to produce designation sequences if any now. */
1397 encode_designation_at_bol (coding
, unification_table
,
1398 src
, src_end
, &dst
);
1399 CODING_SPEC_ISO_BOL (coding
) = 0;
1403 /* If we are seeing a component of a composite character, we are
1404 seeing a leading-code specially encoded for composition, or a
1405 composition rule if composing with rule. We must set C1
1406 to a normal leading-code or an ASCII code. If we are not at
1407 a composed character, we must reset the composition state. */
1408 if (COMPOSING_P (coding
->composing
))
1412 /* We are not in a composite character any longer. */
1413 coding
->composing
= COMPOSING_NO
;
1414 ENCODE_COMPOSITION_END
;
1418 if (coding
->composing
== COMPOSING_WITH_RULE_RULE
)
1421 coding
->composing
= COMPOSING_WITH_RULE_HEAD
;
1424 else if (coding
->composing
== COMPOSING_WITH_RULE_HEAD
)
1425 coding
->composing
= COMPOSING_WITH_RULE_RULE
;
1428 /* This is an ASCII component. */
1433 /* This is a leading-code of non ASCII component. */
1438 /* Now encode one character. C1 is a control character, an
1439 ASCII character, or a leading-code of multi-byte character. */
1440 switch (emacs_code_class
[c1
])
1442 case EMACS_ascii_code
:
1443 ENCODE_ISO_CHARACTER (CHARSET_ASCII
, c1
, /* dummy */ c2
);
1446 case EMACS_control_code
:
1447 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_CNTL
)
1448 ENCODE_RESET_PLANE_AND_REGISTER
;
1452 case EMACS_carriage_return_code
:
1453 if (!coding
->selective
)
1455 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_CNTL
)
1456 ENCODE_RESET_PLANE_AND_REGISTER
;
1460 /* fall down to treat '\r' as '\n' ... */
1462 case EMACS_linefeed_code
:
1463 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_EOL
)
1464 ENCODE_RESET_PLANE_AND_REGISTER
;
1465 if (coding
->flags
& CODING_FLAG_ISO_INIT_AT_BOL
)
1466 bcopy (coding
->spec
.iso2022
.initial_designation
,
1467 coding
->spec
.iso2022
.current_designation
,
1468 sizeof coding
->spec
.iso2022
.initial_designation
);
1469 if (coding
->eol_type
== CODING_EOL_LF
1470 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
1471 *dst
++ = ISO_CODE_LF
;
1472 else if (coding
->eol_type
== CODING_EOL_CRLF
)
1473 *dst
++ = ISO_CODE_CR
, *dst
++ = ISO_CODE_LF
;
1475 *dst
++ = ISO_CODE_CR
;
1476 CODING_SPEC_ISO_BOL (coding
) = 1;
1479 case EMACS_leading_code_2
:
1481 ENCODE_ISO_CHARACTER (c1
, c2
, /* dummy */ c3
);
1484 case EMACS_leading_code_3
:
1485 TWO_MORE_BYTES (c2
, c3
);
1486 if (c1
< LEADING_CODE_PRIVATE_11
)
1487 ENCODE_ISO_CHARACTER (c1
, c2
, c3
);
1489 ENCODE_ISO_CHARACTER (c2
, c3
, /* dummy */ c4
);
1492 case EMACS_leading_code_4
:
1493 THREE_MORE_BYTES (c2
, c3
, c4
);
1494 ENCODE_ISO_CHARACTER (c2
, c3
, c4
);
1497 case EMACS_leading_code_composition
:
1501 coding
->composing
= COMPOSING_WITH_RULE_HEAD
;
1502 ENCODE_COMPOSITION_WITH_RULE_START
;
1506 /* Rewind one byte because it is a character code of
1507 composition elements. */
1509 coding
->composing
= COMPOSING_NO_RULE_HEAD
;
1510 ENCODE_COMPOSITION_NO_RULE_START
;
1514 case EMACS_invalid_code
:
1520 coding
->carryover_size
= src
- src_base
;
1521 bcopy (src_base
, coding
->carryover
, coding
->carryover_size
);
1525 /* If this is the last block of the text to be encoded, we must
1526 reset graphic planes and registers to the initial state. */
1527 if (src
>= src_end
&& coding
->last_block
)
1529 ENCODE_RESET_PLANE_AND_REGISTER
;
1530 if (coding
->carryover_size
> 0
1531 && coding
->carryover_size
< (dst_end
- dst
))
1533 bcopy (coding
->carryover
, dst
, coding
->carryover_size
);
1534 dst
+= coding
->carryover_size
;
1535 coding
->carryover_size
= 0;
1538 *consumed
= src
- source
;
1539 return dst
- destination
;
1543 /*** 4. SJIS and BIG5 handlers ***/
1545 /* Although SJIS and BIG5 are not ISO's coding system, They are used
1546 quite widely. So, for the moment, Emacs supports them in the bare
1547 C code. But, in the future, they may be supported only by CCL. */
1549 /* SJIS is a coding system encoding three character sets: ASCII, right
1550 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
1551 as is. A character of charset katakana-jisx0201 is encoded by
1552 "position-code + 0x80". A character of charset japanese-jisx0208
1553 is encoded in 2-byte but two position-codes are divided and shifted
1554 so that it fit in the range below.
1556 --- CODE RANGE of SJIS ---
1557 (character set) (range)
1559 KATAKANA-JISX0201 0xA0 .. 0xDF
1560 JISX0208 (1st byte) 0x80 .. 0x9F and 0xE0 .. 0xFF
1561 (2nd byte) 0x40 .. 0xFF
1562 -------------------------------
1566 /* BIG5 is a coding system encoding two character sets: ASCII and
1567 Big5. An ASCII character is encoded as is. Big5 is a two-byte
1568 character set and is encoded in two-byte.
1570 --- CODE RANGE of BIG5 ---
1571 (character set) (range)
1573 Big5 (1st byte) 0xA1 .. 0xFE
1574 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
1575 --------------------------
1577 Since the number of characters in Big5 is larger than maximum
1578 characters in Emacs' charset (96x96), it can't be handled as one
1579 charset. So, in Emacs, Big5 is divided into two: `charset-big5-1'
1580 and `charset-big5-2'. Both are DIMENSION2 and CHARS94. The former
1581 contains frequently used characters and the latter contains less
1582 frequently used characters. */
1584 /* Macros to decode or encode a character of Big5 in BIG5. B1 and B2
1585 are the 1st and 2nd position-codes of Big5 in BIG5 coding system.
1586 C1 and C2 are the 1st and 2nd position-codes of of Emacs' internal
1587 format. CHARSET is `charset_big5_1' or `charset_big5_2'. */
1589 /* Number of Big5 characters which have the same code in 1st byte. */
1590 #define BIG5_SAME_ROW (0xFF - 0xA1 + 0x7F - 0x40)
1592 #define DECODE_BIG5(b1, b2, charset, c1, c2) \
1595 = (b1 - 0xA1) * BIG5_SAME_ROW + b2 - (b2 < 0x7F ? 0x40 : 0x62); \
1597 charset = charset_big5_1; \
1600 charset = charset_big5_2; \
1601 temp -= (0xC9 - 0xA1) * BIG5_SAME_ROW; \
1603 c1 = temp / (0xFF - 0xA1) + 0x21; \
1604 c2 = temp % (0xFF - 0xA1) + 0x21; \
1607 #define ENCODE_BIG5(charset, c1, c2, b1, b2) \
1609 unsigned int temp = (c1 - 0x21) * (0xFF - 0xA1) + (c2 - 0x21); \
1610 if (charset == charset_big5_2) \
1611 temp += BIG5_SAME_ROW * (0xC9 - 0xA1); \
1612 b1 = temp / BIG5_SAME_ROW + 0xA1; \
1613 b2 = temp % BIG5_SAME_ROW; \
1614 b2 += b2 < 0x3F ? 0x40 : 0x62; \
1617 #define DECODE_SJIS_BIG5_CHARACTER(charset, c1, c2) \
1619 int c_alt, charset_alt = (charset); \
1620 if (!NILP (unification_table) \
1621 && ((c_alt = unify_char (unification_table, \
1622 -1, (charset), c1, c2)) >= 0)) \
1623 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
1624 if (charset_alt == CHARSET_ASCII || charset_alt < 0) \
1625 DECODE_CHARACTER_ASCII (c1); \
1626 else if (CHARSET_DIMENSION (charset_alt) == 1) \
1627 DECODE_CHARACTER_DIMENSION1 (charset_alt, c1); \
1629 DECODE_CHARACTER_DIMENSION2 (charset_alt, c1, c2); \
1632 #define ENCODE_SJIS_BIG5_CHARACTER(charset, c1, c2) \
1634 int c_alt, charset_alt; \
1635 if (!NILP (unification_table) \
1636 && ((c_alt = unify_char (unification_table, -1, charset, c1, c2)) \
1638 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
1640 charset_alt = charset; \
1641 if (charset_alt == charset_ascii) \
1643 else if (CHARSET_DIMENSION (charset_alt) == 1) \
1645 if (sjis_p && charset_alt == charset_katakana_jisx0201) \
1648 *dst++ = charset_alt, *dst++ = c1; \
1652 c1 &= 0x7F, c2 &= 0x7F; \
1653 if (sjis_p && charset_alt == charset_jisx0208) \
1655 unsigned char s1, s2; \
1657 ENCODE_SJIS (c1, c2, s1, s2); \
1658 *dst++ = s1, *dst++ = s2; \
1661 && (charset_alt == charset_big5_1 \
1662 || charset_alt == charset_big5_2)) \
1664 unsigned char b1, b2; \
1666 ENCODE_BIG5 (c1, c2, c3, b1, b2); \
1667 *dst++ = b1, *dst++ = b2; \
1670 *dst++ = charset_alt, *dst++ = c1, *dst++ = c2; \
1674 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1675 Check if a text is encoded in SJIS. If it is, return
1676 CODING_CATEGORY_MASK_SJIS, else return 0. */
1679 detect_coding_sjis (src
, src_end
)
1680 unsigned char *src
, *src_end
;
1684 while (src
< src_end
)
1687 if (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
1689 if ((c
>= 0x80 && c
< 0xA0) || c
>= 0xE0)
1691 if (src
< src_end
&& *src
++ < 0x40)
1695 return CODING_CATEGORY_MASK_SJIS
;
1698 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1699 Check if a text is encoded in BIG5. If it is, return
1700 CODING_CATEGORY_MASK_BIG5, else return 0. */
1703 detect_coding_big5 (src
, src_end
)
1704 unsigned char *src
, *src_end
;
1708 while (src
< src_end
)
1711 if (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
1718 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
1722 return CODING_CATEGORY_MASK_BIG5
;
1725 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
1726 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
1729 decode_coding_sjis_big5 (coding
, source
, destination
,
1730 src_bytes
, dst_bytes
, consumed
, sjis_p
)
1731 struct coding_system
*coding
;
1732 unsigned char *source
, *destination
;
1733 int src_bytes
, dst_bytes
;
1737 unsigned char *src
= source
;
1738 unsigned char *src_end
= source
+ src_bytes
;
1739 unsigned char *dst
= destination
;
1740 unsigned char *dst_end
= destination
+ dst_bytes
;
1741 /* Since the maximum bytes produced by each loop is 4, we subtract 3
1742 from DST_END to assure overflow checking is necessary only at the
1744 unsigned char *adjusted_dst_end
= dst_end
- 3;
1745 Lisp_Object unification_table
1746 = coding
->character_unification_table_for_decode
;
1748 if (!NILP (Venable_character_unification
) && NILP (unification_table
))
1749 unification_table
= Vstandard_character_unification_table_for_decode
;
1751 while (src
< src_end
&& dst
< adjusted_dst_end
)
1753 /* SRC_BASE remembers the start position in source in each loop.
1754 The loop will be exited when there's not enough source text
1755 to analyze two-byte character (within macro ONE_MORE_BYTE).
1756 In that case, SRC is reset to SRC_BASE before exiting. */
1757 unsigned char *src_base
= src
;
1758 unsigned char c1
= *src
++, c2
, c3
, c4
;
1762 if (coding
->eol_type
== CODING_EOL_CRLF
)
1768 /* To process C2 again, SRC is subtracted by 1. */
1777 DECODE_SJIS_BIG5_CHARACTER (charset_ascii
, c1
, /* dummy */ c2
);
1778 else if (c1
< 0xA0 || c1
>= 0xE0)
1780 /* SJIS -> JISX0208, BIG5 -> Big5 (only if 0xE0 <= c1 < 0xFF) */
1784 DECODE_SJIS (c1
, c2
, c3
, c4
);
1785 DECODE_SJIS_BIG5_CHARACTER (charset_jisx0208
, c3
, c4
);
1787 else if (c1
>= 0xE0 && c1
< 0xFF)
1792 DECODE_BIG5 (c1
, c2
, charset
, c3
, c4
);
1793 DECODE_SJIS_BIG5_CHARACTER (charset
, c3
, c4
);
1795 else /* Invalid code */
1800 /* SJIS -> JISX0201-Kana, BIG5 -> Big5 */
1802 DECODE_SJIS_BIG5_CHARACTER (charset_katakana_jisx0201
, c1
, /* dummy */ c2
);
1808 DECODE_BIG5 (c1
, c2
, charset
, c3
, c4
);
1809 DECODE_SJIS_BIG5_CHARACTER (charset
, c3
, c4
);
1815 coding
->carryover_size
= src
- src_base
;
1816 bcopy (src_base
, coding
->carryover
, coding
->carryover_size
);
1821 *consumed
= src
- source
;
1822 return dst
- destination
;
1825 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
1826 This function can encode `charset_ascii', `charset_katakana_jisx0201',
1827 `charset_jisx0208', `charset_big5_1', and `charset_big5-2'. We are
1828 sure that all these charsets are registered as official charset
1829 (i.e. do not have extended leading-codes). Characters of other
1830 charsets are produced without any encoding. If SJIS_P is 1, encode
1831 SJIS text, else encode BIG5 text. */
1834 encode_coding_sjis_big5 (coding
, source
, destination
,
1835 src_bytes
, dst_bytes
, consumed
, sjis_p
)
1836 struct coding_system
*coding
;
1837 unsigned char *source
, *destination
;
1838 int src_bytes
, dst_bytes
;
1842 unsigned char *src
= source
;
1843 unsigned char *src_end
= source
+ src_bytes
;
1844 unsigned char *dst
= destination
;
1845 unsigned char *dst_end
= destination
+ dst_bytes
;
1846 /* Since the maximum bytes produced by each loop is 2, we subtract 1
1847 from DST_END to assure overflow checking is necessary only at the
1849 unsigned char *adjusted_dst_end
= dst_end
- 1;
1850 Lisp_Object unification_table
1851 = coding
->character_unification_table_for_encode
;
1853 if (!NILP (Venable_character_unification
) && NILP (unification_table
))
1854 unification_table
= Vstandard_character_unification_table_for_encode
;
1856 while (src
< src_end
&& dst
< adjusted_dst_end
)
1858 /* SRC_BASE remembers the start position in source in each loop.
1859 The loop will be exited when there's not enough source text
1860 to analyze multi-byte codes (within macros ONE_MORE_BYTE and
1861 TWO_MORE_BYTES). In that case, SRC is reset to SRC_BASE
1863 unsigned char *src_base
= src
;
1864 unsigned char c1
= *src
++, c2
, c3
, c4
;
1866 if (coding
->composing
)
1873 else if (c1
>= 0xA0)
1876 coding
->composing
= 0;
1879 switch (emacs_code_class
[c1
])
1881 case EMACS_ascii_code
:
1882 ENCODE_SJIS_BIG5_CHARACTER (charset_ascii
, c1
, /* dummy */ c2
);
1885 case EMACS_control_code
:
1889 case EMACS_carriage_return_code
:
1890 if (!coding
->selective
)
1895 /* fall down to treat '\r' as '\n' ... */
1897 case EMACS_linefeed_code
:
1898 if (coding
->eol_type
== CODING_EOL_LF
1899 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
1901 else if (coding
->eol_type
== CODING_EOL_CRLF
)
1902 *dst
++ = '\r', *dst
++ = '\n';
1907 case EMACS_leading_code_2
:
1909 ENCODE_SJIS_BIG5_CHARACTER (c1
, c2
, /* dummy */ c3
);
1912 case EMACS_leading_code_3
:
1913 TWO_MORE_BYTES (c2
, c3
);
1914 ENCODE_SJIS_BIG5_CHARACTER (c1
, c2
, c3
);
1917 case EMACS_leading_code_4
:
1918 THREE_MORE_BYTES (c2
, c3
, c4
);
1919 ENCODE_SJIS_BIG5_CHARACTER (c2
, c3
, c4
);
1922 case EMACS_leading_code_composition
:
1923 coding
->composing
= 1;
1926 default: /* i.e. case EMACS_invalid_code: */
1932 coding
->carryover_size
= src
- src_base
;
1933 bcopy (src_base
, coding
->carryover
, coding
->carryover_size
);
1938 *consumed
= src
- source
;
1939 return dst
- destination
;
1943 /*** 5. End-of-line handlers ***/
1945 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
1946 This function is called only when `coding->eol_type' is
1947 CODING_EOL_CRLF or CODING_EOL_CR. */
1949 decode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
1950 struct coding_system
*coding
;
1951 unsigned char *source
, *destination
;
1952 int src_bytes
, dst_bytes
;
1955 unsigned char *src
= source
;
1956 unsigned char *src_end
= source
+ src_bytes
;
1957 unsigned char *dst
= destination
;
1958 unsigned char *dst_end
= destination
+ dst_bytes
;
1961 switch (coding
->eol_type
)
1963 case CODING_EOL_CRLF
:
1965 /* Since the maximum bytes produced by each loop is 2, we
1966 subtract 1 from DST_END to assure overflow checking is
1967 necessary only at the head of loop. */
1968 unsigned char *adjusted_dst_end
= dst_end
- 1;
1970 while (src
< src_end
&& dst
< adjusted_dst_end
)
1972 unsigned char *src_base
= src
;
1973 unsigned char c
= *src
++;
1986 coding
->carryover_size
= src
- src_base
;
1987 bcopy (src_base
, coding
->carryover
, coding
->carryover_size
);
1991 *consumed
= src
- source
;
1992 produced
= dst
- destination
;
1997 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
1998 bcopy (source
, destination
, produced
);
1999 dst_end
= destination
+ produced
;
2000 while (dst
< dst_end
)
2001 if (*dst
++ == '\r') dst
[-1] = '\n';
2002 *consumed
= produced
;
2005 default: /* i.e. case: CODING_EOL_LF */
2006 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
2007 bcopy (source
, destination
, produced
);
2008 *consumed
= produced
;
2015 /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". Encode
2016 format of end-of-line according to `coding->eol_type'. If
2017 `coding->selective' is 1, code '\r' in source text also means
2020 encode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
2021 struct coding_system
*coding
;
2022 unsigned char *source
, *destination
;
2023 int src_bytes
, dst_bytes
;
2026 unsigned char *src
= source
;
2027 unsigned char *dst
= destination
;
2033 switch (coding
->eol_type
)
2036 case CODING_EOL_UNDECIDED
:
2037 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
2038 bcopy (source
, destination
, produced
);
2039 if (coding
->selective
)
2043 if (*dst
++ == '\r') dst
[-1] = '\n';
2045 *consumed
= produced
;
2047 case CODING_EOL_CRLF
:
2050 unsigned char *src_end
= source
+ src_bytes
;
2051 unsigned char *dst_end
= destination
+ dst_bytes
;
2052 /* Since the maximum bytes produced by each loop is 2, we
2053 subtract 1 from DST_END to assure overflow checking is
2054 necessary only at the head of loop. */
2055 unsigned char *adjusted_dst_end
= dst_end
- 1;
2057 while (src
< src_end
&& dst
< adjusted_dst_end
)
2060 if (c
== '\n' || (c
== '\r' && coding
->selective
))
2061 *dst
++ = '\r', *dst
++ = '\n';
2065 produced
= dst
- destination
;
2066 *consumed
= src
- source
;
2070 default: /* i.e. case CODING_EOL_CR: */
2071 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
2072 bcopy (source
, destination
, produced
);
2076 if (*dst
++ == '\n') dst
[-1] = '\r';
2078 *consumed
= produced
;
2085 /*** 6. C library functions ***/
2087 /* In Emacs Lisp, coding system is represented by a Lisp symbol which
2088 has a property `coding-system'. The value of this property is a
2089 vector of length 5 (called as coding-vector). Among elements of
2090 this vector, the first (element[0]) and the fifth (element[4])
2091 carry important information for decoding/encoding. Before
2092 decoding/encoding, this information should be set in fields of a
2093 structure of type `coding_system'.
2095 A value of property `coding-system' can be a symbol of another
2096 subsidiary coding-system. In that case, Emacs gets coding-vector
2099 `element[0]' contains information to be set in `coding->type'. The
2100 value and its meaning is as follows:
2102 0 -- coding_type_emacs_mule
2103 1 -- coding_type_sjis
2104 2 -- coding_type_iso2022
2105 3 -- coding_type_big5
2106 4 -- coding_type_ccl encoder/decoder written in CCL
2107 nil -- coding_type_no_conversion
2108 t -- coding_type_undecided (automatic conversion on decoding,
2109 no-conversion on encoding)
2111 `element[4]' contains information to be set in `coding->flags' and
2112 `coding->spec'. The meaning varies by `coding->type'.
2114 If `coding->type' is `coding_type_iso2022', element[4] is a vector
2115 of length 32 (of which the first 13 sub-elements are used now).
2116 Meanings of these sub-elements are:
2118 sub-element[N] where N is 0 through 3: to be set in `coding->spec.iso2022'
2119 If the value is an integer of valid charset, the charset is
2120 assumed to be designated to graphic register N initially.
2122 If the value is minus, it is a minus value of charset which
2123 reserves graphic register N, which means that the charset is
2124 not designated initially but should be designated to graphic
2125 register N just before encoding a character in that charset.
2127 If the value is nil, graphic register N is never used on
2130 sub-element[N] where N is 4 through 11: to be set in `coding->flags'
2131 Each value takes t or nil. See the section ISO2022 of
2132 `coding.h' for more information.
2134 If `coding->type' is `coding_type_big5', element[4] is t to denote
2135 BIG5-ETen or nil to denote BIG5-HKU.
2137 If `coding->type' takes the other value, element[4] is ignored.
2139 Emacs Lisp's coding system also carries information about format of
2140 end-of-line in a value of property `eol-type'. If the value is
2141 integer, 0 means CODING_EOL_LF, 1 means CODING_EOL_CRLF, and 2
2142 means CODING_EOL_CR. If it is not integer, it should be a vector
2143 of subsidiary coding systems of which property `eol-type' has one
2148 /* Extract information for decoding/encoding from CODING_SYSTEM_SYMBOL
2149 and set it in CODING. If CODING_SYSTEM_SYMBOL is invalid, CODING
2150 is setup so that no conversion is necessary and return -1, else
2154 setup_coding_system (coding_system
, coding
)
2155 Lisp_Object coding_system
;
2156 struct coding_system
*coding
;
2158 Lisp_Object type
, eol_type
;
2160 /* At first, set several fields default values. */
2161 coding
->require_flushing
= 0;
2162 coding
->last_block
= 0;
2163 coding
->selective
= 0;
2164 coding
->composing
= 0;
2165 coding
->direction
= 0;
2166 coding
->carryover_size
= 0;
2167 coding
->post_read_conversion
= coding
->pre_write_conversion
= Qnil
;
2168 coding
->character_unification_table_for_decode
= Qnil
;
2169 coding
->character_unification_table_for_encode
= Qnil
;
2171 Vlast_coding_system_used
= coding
->symbol
= coding_system
;
2173 /* Get value of property `coding-system' until we get a vector.
2174 While doing that, also get values of properties
2175 `post-read-conversion', `pre-write-conversion',
2176 `character-unification-table-for-decode',
2177 `character-unification-table-for-encode' and `eol-type'. */
2178 while (!NILP (coding_system
) && SYMBOLP (coding_system
))
2180 if (NILP (coding
->post_read_conversion
))
2181 coding
->post_read_conversion
= Fget (coding_system
,
2182 Qpost_read_conversion
);
2183 if (NILP (coding
->pre_write_conversion
))
2184 coding
->pre_write_conversion
= Fget (coding_system
,
2185 Qpre_write_conversion
);
2186 if (NILP (eol_type
))
2187 eol_type
= Fget (coding_system
, Qeol_type
);
2189 if (NILP (coding
->character_unification_table_for_decode
))
2190 coding
->character_unification_table_for_decode
2191 = Fget (coding_system
, Qcharacter_unification_table_for_decode
);
2193 if (NILP (coding
->character_unification_table_for_encode
))
2194 coding
->character_unification_table_for_encode
2195 = Fget (coding_system
, Qcharacter_unification_table_for_encode
);
2197 coding_system
= Fget (coding_system
, Qcoding_system
);
2200 while (!NILP (coding
->character_unification_table_for_decode
)
2201 && SYMBOLP (coding
->character_unification_table_for_decode
))
2202 coding
->character_unification_table_for_decode
2203 = Fget (coding
->character_unification_table_for_decode
,
2204 Qcharacter_unification_table_for_decode
);
2205 if (!NILP (coding
->character_unification_table_for_decode
)
2206 && !CHAR_TABLE_P (coding
->character_unification_table_for_decode
))
2207 coding
->character_unification_table_for_decode
= Qnil
;
2209 while (!NILP (coding
->character_unification_table_for_encode
)
2210 && SYMBOLP (coding
->character_unification_table_for_encode
))
2211 coding
->character_unification_table_for_encode
2212 = Fget (coding
->character_unification_table_for_encode
,
2213 Qcharacter_unification_table_for_encode
);
2214 if (!NILP (coding
->character_unification_table_for_encode
)
2215 && !CHAR_TABLE_P (coding
->character_unification_table_for_encode
))
2216 coding
->character_unification_table_for_encode
= Qnil
;
2218 if (!VECTORP (coding_system
)
2219 || XVECTOR (coding_system
)->size
!= 5)
2220 goto label_invalid_coding_system
;
2222 if (VECTORP (eol_type
))
2223 coding
->eol_type
= CODING_EOL_UNDECIDED
;
2224 else if (XFASTINT (eol_type
) == 1)
2225 coding
->eol_type
= CODING_EOL_CRLF
;
2226 else if (XFASTINT (eol_type
) == 2)
2227 coding
->eol_type
= CODING_EOL_CR
;
2229 coding
->eol_type
= CODING_EOL_LF
;
2231 type
= XVECTOR (coding_system
)->contents
[0];
2232 switch (XFASTINT (type
))
2235 coding
->type
= coding_type_emacs_mule
;
2239 coding
->type
= coding_type_sjis
;
2243 coding
->type
= coding_type_iso2022
;
2245 Lisp_Object val
= XVECTOR (coding_system
)->contents
[4];
2247 int i
, charset
, default_reg_bits
= 0;
2249 if (!VECTORP (val
) || XVECTOR (val
)->size
!= 32)
2250 goto label_invalid_coding_system
;
2252 flags
= XVECTOR (val
)->contents
;
2254 = ((NILP (flags
[4]) ? 0 : CODING_FLAG_ISO_SHORT_FORM
)
2255 | (NILP (flags
[5]) ? 0 : CODING_FLAG_ISO_RESET_AT_EOL
)
2256 | (NILP (flags
[6]) ? 0 : CODING_FLAG_ISO_RESET_AT_CNTL
)
2257 | (NILP (flags
[7]) ? 0 : CODING_FLAG_ISO_SEVEN_BITS
)
2258 | (NILP (flags
[8]) ? 0 : CODING_FLAG_ISO_LOCKING_SHIFT
)
2259 | (NILP (flags
[9]) ? 0 : CODING_FLAG_ISO_SINGLE_SHIFT
)
2260 | (NILP (flags
[10]) ? 0 : CODING_FLAG_ISO_USE_ROMAN
)
2261 | (NILP (flags
[11]) ? 0 : CODING_FLAG_ISO_USE_OLDJIS
)
2262 | (NILP (flags
[12]) ? 0 : CODING_FLAG_ISO_NO_DIRECTION
)
2263 | (NILP (flags
[13]) ? 0 : CODING_FLAG_ISO_INIT_AT_BOL
)
2264 | (NILP (flags
[14]) ? 0 : CODING_FLAG_ISO_DESIGNATE_AT_BOL
));
2266 /* Invoke graphic register 0 to plane 0. */
2267 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 0;
2268 /* Invoke graphic register 1 to plane 1 if we can use full 8-bit. */
2269 CODING_SPEC_ISO_INVOCATION (coding
, 1)
2270 = (coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
? -1 : 1);
2271 /* Not single shifting at first. */
2272 CODING_SPEC_ISO_SINGLE_SHIFTING(coding
) = 0;
2273 /* Beginning of buffer should also be regarded as bol. */
2274 CODING_SPEC_ISO_BOL(coding
) = 1;
2276 /* Checks FLAGS[REG] (REG = 0, 1, 2 3) and decide designations.
2277 FLAGS[REG] can be one of below:
2278 integer CHARSET: CHARSET occupies register I,
2279 t: designate nothing to REG initially, but can be used
2281 list of integer, nil, or t: designate the first
2282 element (if integer) to REG initially, the remaining
2283 elements (if integer) is designated to REG on request,
2284 if an element is t, REG can be used by any charset,
2285 nil: REG is never used. */
2286 for (charset
= 0; charset
<= MAX_CHARSET
; charset
++)
2287 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2288 = CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
;
2289 for (i
= 0; i
< 4; i
++)
2291 if (INTEGERP (flags
[i
])
2292 && (charset
= XINT (flags
[i
]), CHARSET_VALID_P (charset
))
2293 || (charset
= get_charset_id (flags
[i
])) >= 0)
2295 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = charset
;
2296 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
) = i
;
2298 else if (EQ (flags
[i
], Qt
))
2300 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
2301 default_reg_bits
|= 1 << i
;
2303 else if (CONSP (flags
[i
]))
2305 Lisp_Object tail
= flags
[i
];
2307 if (INTEGERP (XCONS (tail
)->car
)
2308 && (charset
= XINT (XCONS (tail
)->car
),
2309 CHARSET_VALID_P (charset
))
2310 || (charset
= get_charset_id (XCONS (tail
)->car
)) >= 0)
2312 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = charset
;
2313 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
) =i
;
2316 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
2317 tail
= XCONS (tail
)->cdr
;
2318 while (CONSP (tail
))
2320 if (INTEGERP (XCONS (tail
)->car
)
2321 && (charset
= XINT (XCONS (tail
)->car
),
2322 CHARSET_VALID_P (charset
))
2323 || (charset
= get_charset_id (XCONS (tail
)->car
)) >= 0)
2324 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2326 else if (EQ (XCONS (tail
)->car
, Qt
))
2327 default_reg_bits
|= 1 << i
;
2328 tail
= XCONS (tail
)->cdr
;
2332 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
2334 CODING_SPEC_ISO_DESIGNATION (coding
, i
)
2335 = CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
);
2338 if (! (coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
))
2340 /* REG 1 can be used only by locking shift in 7-bit env. */
2341 if (coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
)
2342 default_reg_bits
&= ~2;
2343 if (! (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
))
2344 /* Without any shifting, only REG 0 and 1 can be used. */
2345 default_reg_bits
&= 3;
2348 for (charset
= 0; charset
<= MAX_CHARSET
; charset
++)
2349 if (CHARSET_VALID_P (charset
)
2350 && (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2351 == CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
))
2353 /* We have not yet decided where to designate CHARSET. */
2354 int reg_bits
= default_reg_bits
;
2356 if (CHARSET_CHARS (charset
) == 96)
2357 /* A charset of CHARS96 can't be designated to REG 0. */
2361 /* There exist some default graphic register. */
2362 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2364 ? 0 : (reg_bits
& 2 ? 1 : (reg_bits
& 4 ? 2 : 3)));
2366 /* We anyway have to designate CHARSET to somewhere. */
2367 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2368 = (CHARSET_CHARS (charset
) == 94
2370 : ((coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
2371 || ! coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
)
2373 : (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
2377 coding
->require_flushing
= 1;
2381 coding
->type
= coding_type_big5
;
2383 = (NILP (XVECTOR (coding_system
)->contents
[4])
2384 ? CODING_FLAG_BIG5_HKU
2385 : CODING_FLAG_BIG5_ETEN
);
2389 coding
->type
= coding_type_ccl
;
2391 Lisp_Object val
= XVECTOR (coding_system
)->contents
[4];
2393 && VECTORP (XCONS (val
)->car
)
2394 && VECTORP (XCONS (val
)->cdr
))
2396 setup_ccl_program (&(coding
->spec
.ccl
.decoder
), XCONS (val
)->car
);
2397 setup_ccl_program (&(coding
->spec
.ccl
.encoder
), XCONS (val
)->cdr
);
2400 goto label_invalid_coding_system
;
2402 coding
->require_flushing
= 1;
2407 coding
->type
= coding_type_undecided
;
2409 coding
->type
= coding_type_no_conversion
;
2414 label_invalid_coding_system
:
2415 coding
->type
= coding_type_no_conversion
;
2416 coding
->eol_type
= CODING_EOL_LF
;
2417 coding
->symbol
= coding
->pre_write_conversion
= coding
->post_read_conversion
2422 /* Emacs has a mechanism to automatically detect a coding system if it
2423 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
2424 it's impossible to distinguish some coding systems accurately
2425 because they use the same range of codes. So, at first, coding
2426 systems are categorized into 7, those are:
2428 o coding-category-emacs-mule
2430 The category for a coding system which has the same code range
2431 as Emacs' internal format. Assigned the coding-system (Lisp
2432 symbol) `emacs-mule' by default.
2434 o coding-category-sjis
2436 The category for a coding system which has the same code range
2437 as SJIS. Assigned the coding-system (Lisp
2438 symbol) `shift-jis' by default.
2440 o coding-category-iso-7
2442 The category for a coding system which has the same code range
2443 as ISO2022 of 7-bit environment. Assigned the coding-system
2444 (Lisp symbol) `iso-2022-7' by default.
2446 o coding-category-iso-8-1
2448 The category for a coding system which has the same code range
2449 as ISO2022 of 8-bit environment and graphic plane 1 used only
2450 for DIMENSION1 charset. Assigned the coding-system (Lisp
2451 symbol) `iso-8859-1' by default.
2453 o coding-category-iso-8-2
2455 The category for a coding system which has the same code range
2456 as ISO2022 of 8-bit environment and graphic plane 1 used only
2457 for DIMENSION2 charset. Assigned the coding-system (Lisp
2458 symbol) `euc-japan' by default.
2460 o coding-category-iso-else
2462 The category for a coding system which has the same code range
2463 as ISO2022 but not belongs to any of the above three
2464 categories. Assigned the coding-system (Lisp symbol)
2465 `iso-2022-ss2-7' by default.
2467 o coding-category-big5
2469 The category for a coding system which has the same code range
2470 as BIG5. Assigned the coding-system (Lisp symbol)
2471 `cn-big5' by default.
2473 o coding-category-binary
2475 The category for a coding system not categorized in any of the
2476 above. Assigned the coding-system (Lisp symbol)
2477 `no-conversion' by default.
2479 Each of them is a Lisp symbol and the value is an actual
2480 `coding-system's (this is also a Lisp symbol) assigned by a user.
2481 What Emacs does actually is to detect a category of coding system.
2482 Then, it uses a `coding-system' assigned to it. If Emacs can't
2483 decide only one possible category, it selects a category of the
2484 highest priority. Priorities of categories are also specified by a
2485 user in a Lisp variable `coding-category-list'.
2489 /* Detect how a text of length SRC_BYTES pointed by SRC is encoded.
2490 If it detects possible coding systems, return an integer in which
2491 appropriate flag bits are set. Flag bits are defined by macros
2492 CODING_CATEGORY_MASK_XXX in `coding.h'. */
2495 detect_coding_mask (src
, src_bytes
)
2499 register unsigned char c
;
2500 unsigned char *src_end
= src
+ src_bytes
;
2503 /* At first, skip all ASCII characters and control characters except
2504 for three ISO2022 specific control characters. */
2505 label_loop_detect_coding
:
2506 while (src
< src_end
)
2510 || (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
))
2516 /* We found nothing other than ASCII. There's nothing to do. */
2517 return CODING_CATEGORY_MASK_ANY
;
2519 /* The text seems to be encoded in some multilingual coding system.
2520 Now, try to find in which coding system the text is encoded. */
2523 /* i.e. (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) */
2524 /* C is an ISO2022 specific control code of C0. */
2525 mask
= detect_coding_iso2022 (src
, src_end
);
2527 if (mask
== CODING_CATEGORY_MASK_ANY
)
2528 /* No valid ISO2022 code follows C. Try again. */
2529 goto label_loop_detect_coding
;
2531 else if (c
== ISO_CODE_SS2
|| c
== ISO_CODE_SS3
|| c
== ISO_CODE_CSI
)
2532 /* C is an ISO2022 specific control code of C1,
2533 or the first byte of SJIS's 2-byte character code,
2534 or a leading code of Emacs. */
2535 mask
= (detect_coding_iso2022 (src
, src_end
)
2536 | detect_coding_sjis (src
, src_end
)
2537 | detect_coding_emacs_mule (src
, src_end
));
2540 /* C is the first byte of SJIS character code,
2541 or a leading-code of Emacs. */
2542 mask
= (detect_coding_sjis (src
, src_end
)
2543 | detect_coding_emacs_mule (src
, src_end
));
2546 /* C is a character of ISO2022 in graphic plane right,
2547 or a SJIS's 1-byte character code (i.e. JISX0201),
2548 or the first byte of BIG5's 2-byte code. */
2549 mask
= (detect_coding_iso2022 (src
, src_end
)
2550 | detect_coding_sjis (src
, src_end
)
2551 | detect_coding_big5 (src
, src_end
));
2556 /* Detect how a text of length SRC_BYTES pointed by SRC is encoded.
2557 The information of the detected coding system is set in CODING. */
2560 detect_coding (coding
, src
, src_bytes
)
2561 struct coding_system
*coding
;
2565 int mask
= detect_coding_mask (src
, src_bytes
);
2568 if (mask
== CODING_CATEGORY_MASK_ANY
)
2569 /* We found nothing other than ASCII. There's nothing to do. */
2573 /* The source text seems to be encoded in unknown coding system.
2574 Emacs regards the category of such a kind of coding system as
2575 `coding-category-binary'. We assume that a user has assigned
2576 an appropriate coding system for a `coding-category-binary'. */
2577 idx
= CODING_CATEGORY_IDX_BINARY
;
2580 /* We found some plausible coding systems. Let's use a coding
2581 system of the highest priority. */
2582 Lisp_Object val
= Vcoding_category_list
;
2587 idx
= XFASTINT (Fget (XCONS (val
)->car
, Qcoding_category_index
));
2588 if ((idx
< CODING_CATEGORY_IDX_MAX
) && (mask
& (1 << idx
)))
2590 val
= XCONS (val
)->cdr
;
2597 /* For unknown reason, `Vcoding_category_list' contains none
2598 of found categories. Let's use any of them. */
2599 for (idx
= 0; idx
< CODING_CATEGORY_IDX_MAX
; idx
++)
2600 if (mask
& (1 << idx
))
2604 setup_coding_system (XSYMBOL (coding_category_table
[idx
])->value
, coding
);
2607 /* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC
2608 is encoded. Return one of CODING_EOL_LF, CODING_EOL_CRLF,
2609 CODING_EOL_CR, and CODING_EOL_UNDECIDED. */
2612 detect_eol_type (src
, src_bytes
)
2616 unsigned char *src_end
= src
+ src_bytes
;
2619 while (src
< src_end
)
2623 return CODING_EOL_LF
;
2626 if (src
< src_end
&& *src
== '\n')
2627 return CODING_EOL_CRLF
;
2629 return CODING_EOL_CR
;
2632 return CODING_EOL_UNDECIDED
;
2635 /* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC
2636 is encoded. If it detects an appropriate format of end-of-line, it
2637 sets the information in *CODING. */
2640 detect_eol (coding
, src
, src_bytes
)
2641 struct coding_system
*coding
;
2646 int eol_type
= detect_eol_type (src
, src_bytes
);
2648 if (eol_type
== CODING_EOL_UNDECIDED
)
2649 /* We found no end-of-line in the source text. */
2652 val
= Fget (coding
->symbol
, Qeol_type
);
2653 if (VECTORP (val
) && XVECTOR (val
)->size
== 3)
2654 setup_coding_system (XVECTOR (val
)->contents
[eol_type
], coding
);
2657 /* See "GENERAL NOTES about `decode_coding_XXX ()' functions". Before
2658 decoding, it may detect coding system and format of end-of-line if
2659 those are not yet decided. */
2662 decode_coding (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
2663 struct coding_system
*coding
;
2664 unsigned char *source
, *destination
;
2665 int src_bytes
, dst_bytes
;
2676 if (coding
->type
== coding_type_undecided
)
2677 detect_coding (coding
, source
, src_bytes
);
2679 if (coding
->eol_type
== CODING_EOL_UNDECIDED
)
2680 detect_eol (coding
, source
, src_bytes
);
2682 coding
->carryover_size
= 0;
2683 switch (coding
->type
)
2685 case coding_type_no_conversion
:
2686 label_no_conversion
:
2687 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
2688 bcopy (source
, destination
, produced
);
2689 *consumed
= produced
;
2692 case coding_type_emacs_mule
:
2693 case coding_type_undecided
:
2694 if (coding
->eol_type
== CODING_EOL_LF
2695 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
2696 goto label_no_conversion
;
2697 produced
= decode_eol (coding
, source
, destination
,
2698 src_bytes
, dst_bytes
, consumed
);
2701 case coding_type_sjis
:
2702 produced
= decode_coding_sjis_big5 (coding
, source
, destination
,
2703 src_bytes
, dst_bytes
, consumed
,
2707 case coding_type_iso2022
:
2708 produced
= decode_coding_iso2022 (coding
, source
, destination
,
2709 src_bytes
, dst_bytes
, consumed
);
2712 case coding_type_big5
:
2713 produced
= decode_coding_sjis_big5 (coding
, source
, destination
,
2714 src_bytes
, dst_bytes
, consumed
,
2718 case coding_type_ccl
:
2719 produced
= ccl_driver (&coding
->spec
.ccl
.decoder
, source
, destination
,
2720 src_bytes
, dst_bytes
, consumed
);
2727 /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". */
2730 encode_coding (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
2731 struct coding_system
*coding
;
2732 unsigned char *source
, *destination
;
2733 int src_bytes
, dst_bytes
;
2738 coding
->carryover_size
= 0;
2739 switch (coding
->type
)
2741 case coding_type_no_conversion
:
2742 label_no_conversion
:
2743 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
2746 bcopy (source
, destination
, produced
);
2747 if (coding
->selective
)
2749 unsigned char *p
= destination
, *pend
= destination
+ produced
;
2751 if (*p
++ == '\015') p
[-1] = '\n';
2754 *consumed
= produced
;
2757 case coding_type_emacs_mule
:
2758 case coding_type_undecided
:
2759 if (coding
->eol_type
== CODING_EOL_LF
2760 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
2761 goto label_no_conversion
;
2762 produced
= encode_eol (coding
, source
, destination
,
2763 src_bytes
, dst_bytes
, consumed
);
2766 case coding_type_sjis
:
2767 produced
= encode_coding_sjis_big5 (coding
, source
, destination
,
2768 src_bytes
, dst_bytes
, consumed
,
2772 case coding_type_iso2022
:
2773 produced
= encode_coding_iso2022 (coding
, source
, destination
,
2774 src_bytes
, dst_bytes
, consumed
);
2777 case coding_type_big5
:
2778 produced
= encode_coding_sjis_big5 (coding
, source
, destination
,
2779 src_bytes
, dst_bytes
, consumed
,
2783 case coding_type_ccl
:
2784 produced
= ccl_driver (&coding
->spec
.ccl
.encoder
, source
, destination
,
2785 src_bytes
, dst_bytes
, consumed
);
2792 #define CONVERSION_BUFFER_EXTRA_ROOM 256
2794 /* Return maximum size (bytes) of a buffer enough for decoding
2795 SRC_BYTES of text encoded in CODING. */
2798 decoding_buffer_size (coding
, src_bytes
)
2799 struct coding_system
*coding
;
2804 if (coding
->type
== coding_type_iso2022
)
2806 else if (coding
->type
== coding_type_ccl
)
2807 magnification
= coding
->spec
.ccl
.decoder
.buf_magnification
;
2811 return (src_bytes
* magnification
+ CONVERSION_BUFFER_EXTRA_ROOM
);
2814 /* Return maximum size (bytes) of a buffer enough for encoding
2815 SRC_BYTES of text to CODING. */
2818 encoding_buffer_size (coding
, src_bytes
)
2819 struct coding_system
*coding
;
2824 if (coding
->type
== coding_type_ccl
)
2825 magnification
= coding
->spec
.ccl
.encoder
.buf_magnification
;
2829 return (src_bytes
* magnification
+ CONVERSION_BUFFER_EXTRA_ROOM
);
2832 #ifndef MINIMUM_CONVERSION_BUFFER_SIZE
2833 #define MINIMUM_CONVERSION_BUFFER_SIZE 1024
2836 char *conversion_buffer
;
2837 int conversion_buffer_size
;
2839 /* Return a pointer to a SIZE bytes of buffer to be used for encoding
2840 or decoding. Sufficient memory is allocated automatically. If we
2841 run out of memory, return NULL. */
2844 get_conversion_buffer (size
)
2847 if (size
> conversion_buffer_size
)
2850 int real_size
= conversion_buffer_size
* 2;
2852 while (real_size
< size
) real_size
*= 2;
2853 buf
= (char *) xmalloc (real_size
);
2854 xfree (conversion_buffer
);
2855 conversion_buffer
= buf
;
2856 conversion_buffer_size
= real_size
;
2858 return conversion_buffer
;
2863 /*** 7. Emacs Lisp library functions ***/
2865 DEFUN ("coding-system-spec", Fcoding_system_spec
, Scoding_system_spec
,
2867 "Return coding-spec of CODING-SYSTEM.\n\
2868 If CODING-SYSTEM is not a valid coding-system, return nil.")
2872 while (SYMBOLP (obj
) && !NILP (obj
))
2873 obj
= Fget (obj
, Qcoding_system
);
2874 return ((NILP (obj
) || !VECTORP (obj
) || XVECTOR (obj
)->size
!= 5)
2878 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
2879 "Return t if OBJECT is nil or a coding-system.\n\
2880 See document of make-coding-system for coding-system object.")
2884 return ((NILP (obj
) || !NILP (Fcoding_system_spec (obj
))) ? Qt
: Qnil
);
2887 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
2888 Sread_non_nil_coding_system
, 1, 1, 0,
2889 "Read a coding system from the minibuffer, prompting with string PROMPT.")
2896 val
= Fcompleting_read (prompt
, Vobarray
, Qcoding_system_spec
,
2897 Qt
, Qnil
, Qnil
, Qnil
);
2899 while (XSTRING (val
)->size
== 0);
2900 return (Fintern (val
, Qnil
));
2903 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 1, 0,
2904 "Read a coding system or nil from the minibuffer, prompting with string PROMPT.")
2908 Lisp_Object val
= Fcompleting_read (prompt
, Vobarray
, Qcoding_system_p
,
2909 Qt
, Qnil
, Qnil
, Qnil
);
2910 return (XSTRING (val
)->size
== 0 ? Qnil
: Fintern (val
, Qnil
));
2913 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
2915 "Check validity of CODING-SYSTEM.\n\
2916 If valid, return CODING-SYSTEM, else `coding-system-error' is signaled.\n\
2917 CODING-SYSTEM is valid if it is a symbol and has \"coding-system\" property.\n\
2918 The value of property should be a vector of length 5.")
2920 Lisp_Object coding_system
;
2922 CHECK_SYMBOL (coding_system
, 0);
2923 if (!NILP (Fcoding_system_p (coding_system
)))
2924 return coding_system
;
2926 Fsignal (Qcoding_system_error
, Fcons (coding_system
, Qnil
));
2929 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
2931 "Detect coding-system of the text in the region between START and END.\n\
2932 Return a list of possible coding-systems ordered by priority.\n\
2933 If only ASCII characters are found, it returns `undecided'\n\
2934 or its subsidiary coding-system according to a detected end-of-line format.")
2938 int coding_mask
, eol_type
;
2942 validate_region (&b
, &e
);
2943 beg
= XINT (b
), end
= XINT (e
);
2944 if (beg
< GPT
&& end
>= GPT
) move_gap (end
);
2946 coding_mask
= detect_coding_mask (POS_ADDR (beg
), end
- beg
);
2947 eol_type
= detect_eol_type (POS_ADDR (beg
), end
- beg
);
2949 if (coding_mask
== CODING_CATEGORY_MASK_ANY
)
2951 val
= intern ("undecided");
2952 if (eol_type
!= CODING_EOL_UNDECIDED
)
2954 Lisp_Object val2
= Fget (val
, Qeol_type
);
2956 val
= XVECTOR (val2
)->contents
[eol_type
];
2963 /* At first, gather possible coding-systems in VAL in a reverse
2966 for (val2
= Vcoding_category_list
;
2968 val2
= XCONS (val2
)->cdr
)
2971 = XFASTINT (Fget (XCONS (val2
)->car
, Qcoding_category_index
));
2972 if (coding_mask
& (1 << idx
))
2973 val
= Fcons (Fsymbol_value (XCONS (val2
)->car
), val
);
2976 /* Then, change the order of the list, while getting subsidiary
2980 for (; !NILP (val2
); val2
= XCONS (val2
)->cdr
)
2982 if (eol_type
== CODING_EOL_UNDECIDED
)
2983 val
= Fcons (XCONS (val2
)->car
, val
);
2986 Lisp_Object val3
= Fget (XCONS (val2
)->car
, Qeol_type
);
2988 val
= Fcons (XVECTOR (val3
)->contents
[eol_type
], val
);
2990 val
= Fcons (XCONS (val2
)->car
, val
);
2998 /* Scan text in the region between *BEGP and *ENDP, skip characters
2999 which we never have to encode to (iff ENCODEP is 1) or decode from
3000 coding system CODING at the head and tail, then set BEGP and ENDP
3001 to the addresses of start and end of the text we actually convert. */
3004 shrink_conversion_area (begp
, endp
, coding
, encodep
)
3005 unsigned char **begp
, **endp
;
3006 struct coding_system
*coding
;
3009 register unsigned char *beg_addr
= *begp
, *end_addr
= *endp
;
3011 if (coding
->eol_type
!= CODING_EOL_LF
3012 && coding
->eol_type
!= CODING_EOL_UNDECIDED
)
3013 /* Since we anyway have to convert end-of-line format, it is not
3014 worth skipping at most 100 bytes or so. */
3017 if (encodep
) /* for encoding */
3019 switch (coding
->type
)
3021 case coding_type_no_conversion
:
3022 case coding_type_emacs_mule
:
3023 case coding_type_undecided
:
3024 /* We need no conversion. */
3027 case coding_type_ccl
:
3028 /* We can't skip any data. */
3030 case coding_type_iso2022
:
3031 if (coding
->flags
& CODING_FLAG_ISO_DESIGNATE_AT_BOL
)
3033 unsigned char *bol
= beg_addr
;
3034 while (beg_addr
< end_addr
&& *beg_addr
< 0x80)
3037 if (*(beg_addr
- 1) == '\n')
3041 goto label_skip_tail
;
3045 /* We can skip all ASCII characters at the head and tail. */
3046 while (beg_addr
< end_addr
&& *beg_addr
< 0x80) beg_addr
++;
3048 while (beg_addr
< end_addr
&& *(end_addr
- 1) < 0x80) end_addr
--;
3052 else /* for decoding */
3054 switch (coding
->type
)
3056 case coding_type_no_conversion
:
3057 /* We need no conversion. */
3060 case coding_type_emacs_mule
:
3061 if (coding
->eol_type
== CODING_EOL_LF
)
3063 /* We need no conversion. */
3067 /* We can skip all but carriage-return. */
3068 while (beg_addr
< end_addr
&& *beg_addr
!= '\r') beg_addr
++;
3069 while (beg_addr
< end_addr
&& *(end_addr
- 1) != '\r') end_addr
--;
3071 case coding_type_sjis
:
3072 case coding_type_big5
:
3073 /* We can skip all ASCII characters at the head. */
3074 while (beg_addr
< end_addr
&& *beg_addr
< 0x80) beg_addr
++;
3075 /* We can skip all ASCII characters at the tail except for
3076 the second byte of SJIS or BIG5 code. */
3077 while (beg_addr
< end_addr
&& *(end_addr
- 1) < 0x80) end_addr
--;
3078 if (end_addr
!= *endp
)
3081 case coding_type_ccl
:
3082 /* We can't skip any data. */
3084 default: /* i.e. case coding_type_iso2022: */
3088 /* We can skip all ASCII characters except for a few
3089 control codes at the head. */
3090 while (beg_addr
< end_addr
&& (c
= *beg_addr
) < 0x80
3091 && c
!= ISO_CODE_CR
&& c
!= ISO_CODE_SO
3092 && c
!= ISO_CODE_SI
&& c
!= ISO_CODE_ESC
)
3103 /* Encode to (iff ENCODEP is 1) or decode form coding system CODING a
3104 text between B and E. B and E are buffer position. */
3107 code_convert_region (b
, e
, coding
, encodep
)
3109 struct coding_system
*coding
;
3112 int beg
, end
, len
, consumed
, produced
;
3114 unsigned char *begp
, *endp
;
3117 validate_region (&b
, &e
);
3118 beg
= XINT (b
), end
= XINT (e
);
3119 if (beg
< GPT
&& end
>= GPT
)
3122 if (encodep
&& !NILP (coding
->pre_write_conversion
))
3124 /* We must call a pre-conversion function which may put a new
3125 text to be converted in a new buffer. */
3126 struct buffer
*old
= current_buffer
, *new;
3129 call2 (coding
->pre_write_conversion
, b
, e
);
3130 if (old
!= current_buffer
)
3132 /* Replace the original text by the text just generated. */
3134 new = current_buffer
;
3135 set_buffer_internal (old
);
3136 del_range (beg
, end
);
3137 insert_from_buffer (new, 1, len
, 0);
3142 /* We may be able to shrink the conversion region. */
3143 begp
= POS_ADDR (beg
); endp
= begp
+ (end
- beg
);
3144 shrink_conversion_area (&begp
, &endp
, coding
, encodep
);
3147 /* We need no conversion. */
3151 beg
+= begp
- POS_ADDR (beg
);
3152 end
= beg
+ (endp
- begp
);
3155 len
= encoding_buffer_size (coding
, end
- beg
);
3157 len
= decoding_buffer_size (coding
, end
- beg
);
3158 buf
= get_conversion_buffer (len
);
3160 coding
->last_block
= 1;
3162 ? encode_coding (coding
, POS_ADDR (beg
), buf
, end
- beg
, len
,
3164 : decode_coding (coding
, POS_ADDR (beg
), buf
, end
- beg
, len
,
3167 len
= produced
+ (beg
- XINT (b
)) + (XINT (e
) - end
);
3170 insert (buf
, produced
);
3171 del_range (PT
, PT
+ end
- beg
);
3173 pos
= PT
+ (pos
- end
);
3179 if (!encodep
&& !NILP (coding
->post_read_conversion
))
3181 /* We must call a post-conversion function which may alter
3182 the text just converted. */
3187 insval
= call1 (coding
->post_read_conversion
, make_number (len
));
3188 CHECK_NUMBER (insval
, 0);
3189 len
= XINT (insval
);
3192 return make_number (len
);
3196 code_convert_string (str
, coding
, encodep
, nocopy
)
3197 Lisp_Object str
, nocopy
;
3198 struct coding_system
*coding
;
3201 int len
, consumed
, produced
;
3203 unsigned char *begp
, *endp
;
3204 int head_skip
, tail_skip
;
3205 struct gcpro gcpro1
;
3207 if (encodep
&& !NILP (coding
->pre_write_conversion
)
3208 || !encodep
&& !NILP (coding
->post_read_conversion
))
3210 /* Since we have to call Lisp functions which assume target text
3211 is in a buffer, after setting a temporary buffer, call
3212 code_convert_region. */
3213 int count
= specpdl_ptr
- specpdl
;
3214 int len
= XSTRING (str
)->size
;
3216 struct buffer
*old
= current_buffer
;
3218 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
3219 temp_output_buffer_setup (" *code-converting-work*");
3220 set_buffer_internal (XBUFFER (Vstandard_output
));
3221 insert_from_string (str
, 0, len
, 0);
3222 code_convert_region (make_number (BEGV
), make_number (ZV
),
3224 result
= make_buffer_string (BEGV
, ZV
, 0);
3225 set_buffer_internal (old
);
3226 return unbind_to (count
, result
);
3229 /* We may be able to shrink the conversion region. */
3230 begp
= XSTRING (str
)->data
;
3231 endp
= begp
+ XSTRING (str
)->size
;
3232 shrink_conversion_area (&begp
, &endp
, coding
, encodep
);
3235 /* We need no conversion. */
3236 return (NILP (nocopy
) ? Fcopy_sequence (str
) : str
);
3238 head_skip
= begp
- XSTRING (str
)->data
;
3239 tail_skip
= XSTRING (str
)->size
- head_skip
- (endp
- begp
);
3244 len
= encoding_buffer_size (coding
, endp
- begp
);
3246 len
= decoding_buffer_size (coding
, endp
- begp
);
3247 buf
= get_conversion_buffer (len
+ head_skip
+ tail_skip
);
3249 bcopy (XSTRING (str
)->data
, buf
, head_skip
);
3250 coding
->last_block
= 1;
3252 ? encode_coding (coding
, XSTRING (str
)->data
+ head_skip
,
3253 buf
+ head_skip
, endp
- begp
, len
, &consumed
)
3254 : decode_coding (coding
, XSTRING (str
)->data
+ head_skip
,
3255 buf
+ head_skip
, endp
- begp
, len
, &consumed
));
3256 bcopy (XSTRING (str
)->data
+ head_skip
+ (endp
- begp
),
3257 buf
+ head_skip
+ produced
,
3262 return make_string (buf
, head_skip
+ produced
+ tail_skip
);
3265 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
3266 3, 3, "r\nzCoding system: ",
3267 "Decode current region by specified coding system.\n\
3268 When called from a program, takes three arguments:\n\
3269 START, END, and CODING-SYSTEM. START END are buffer positions.\n\
3270 Return length of decoded text.")
3271 (b
, e
, coding_system
)
3272 Lisp_Object b
, e
, coding_system
;
3274 struct coding_system coding
;
3276 CHECK_NUMBER_COERCE_MARKER (b
, 0);
3277 CHECK_NUMBER_COERCE_MARKER (e
, 1);
3278 CHECK_SYMBOL (coding_system
, 2);
3280 if (NILP (coding_system
))
3281 return make_number (XFASTINT (e
) - XFASTINT (b
));
3282 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
3283 error ("Invalid coding-system: %s", XSYMBOL (coding_system
)->name
->data
);
3285 return code_convert_region (b
, e
, &coding
, 0);
3288 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
3289 3, 3, "r\nzCoding system: ",
3290 "Encode current region by specified coding system.\n\
3291 When called from a program, takes three arguments:\n\
3292 START, END, and CODING-SYSTEM. START END are buffer positions.\n\
3293 Return length of encoded text.")
3294 (b
, e
, coding_system
)
3295 Lisp_Object b
, e
, coding_system
;
3297 struct coding_system coding
;
3299 CHECK_NUMBER_COERCE_MARKER (b
, 0);
3300 CHECK_NUMBER_COERCE_MARKER (e
, 1);
3301 CHECK_SYMBOL (coding_system
, 2);
3303 if (NILP (coding_system
))
3304 return make_number (XFASTINT (e
) - XFASTINT (b
));
3305 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
3306 error ("Invalid coding-system: %s", XSYMBOL (coding_system
)->name
->data
);
3308 return code_convert_region (b
, e
, &coding
, 1);
3311 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
3313 "Decode STRING which is encoded in CODING-SYSTEM, and return the result.\n\
3314 Optional arg NOCOPY non-nil means return STRING itself if there's no need\n\
3316 (string
, coding_system
, nocopy
)
3317 Lisp_Object string
, coding_system
, nocopy
;
3319 struct coding_system coding
;
3321 CHECK_STRING (string
, 0);
3322 CHECK_SYMBOL (coding_system
, 1);
3324 if (NILP (coding_system
))
3325 return (NILP (nocopy
) ? Fcopy_sequence (string
) : string
);
3326 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
3327 error ("Invalid coding-system: %s", XSYMBOL (coding_system
)->name
->data
);
3329 return code_convert_string (string
, &coding
, 0, nocopy
);
3332 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
3334 "Encode STRING to CODING-SYSTEM, and return the result.\n\
3335 Optional arg NOCOPY non-nil means return STRING itself if there's no need\n\
3337 (string
, coding_system
, nocopy
)
3338 Lisp_Object string
, coding_system
, nocopy
;
3340 struct coding_system coding
;
3342 CHECK_STRING (string
, 0);
3343 CHECK_SYMBOL (coding_system
, 1);
3345 if (NILP (coding_system
))
3346 return (NILP (nocopy
) ? Fcopy_sequence (string
) : string
);
3347 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
3348 error ("Invalid coding-system: %s", XSYMBOL (coding_system
)->name
->data
);
3350 return code_convert_string (string
, &coding
, 1, nocopy
);
3353 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
3354 "Decode a JISX0208 character of shift-jis encoding.\n\
3355 CODE is the character code in SJIS.\n\
3356 Return the corresponding character.")
3360 unsigned char c1
, c2
, s1
, s2
;
3363 CHECK_NUMBER (code
, 0);
3364 s1
= (XFASTINT (code
)) >> 8, s2
= (XFASTINT (code
)) & 0xFF;
3365 DECODE_SJIS (s1
, s2
, c1
, c2
);
3366 XSETFASTINT (val
, MAKE_NON_ASCII_CHAR (charset_jisx0208
, c1
, c2
));
3370 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
3371 "Encode a JISX0208 character CHAR to SJIS coding-system.\n\
3372 Return the corresponding character code in SJIS.")
3376 int charset
, c1
, c2
, s1
, s2
;
3379 CHECK_NUMBER (ch
, 0);
3380 SPLIT_CHAR (XFASTINT (ch
), charset
, c1
, c2
);
3381 if (charset
== charset_jisx0208
)
3383 ENCODE_SJIS (c1
, c2
, s1
, s2
);
3384 XSETFASTINT (val
, (s1
<< 8) | s2
);
3387 XSETFASTINT (val
, 0);
3391 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
3392 "Decode a Big5 character CODE of BIG5 coding-system.\n\
3393 CODE is the character code in BIG5.\n\
3394 Return the corresponding character.")
3399 unsigned char b1
, b2
, c1
, c2
;
3402 CHECK_NUMBER (code
, 0);
3403 b1
= (XFASTINT (code
)) >> 8, b2
= (XFASTINT (code
)) & 0xFF;
3404 DECODE_BIG5 (b1
, b2
, charset
, c1
, c2
);
3405 XSETFASTINT (val
, MAKE_NON_ASCII_CHAR (charset
, c1
, c2
));
3409 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
3410 "Encode the Big5 character CHAR to BIG5 coding-system.\n\
3411 Return the corresponding character code in Big5.")
3415 int charset
, c1
, c2
, b1
, b2
;
3418 CHECK_NUMBER (ch
, 0);
3419 SPLIT_CHAR (XFASTINT (ch
), charset
, c1
, c2
);
3420 if (charset
== charset_big5_1
|| charset
== charset_big5_2
)
3422 ENCODE_BIG5 (charset
, c1
, c2
, b1
, b2
);
3423 XSETFASTINT (val
, (b1
<< 8) | b2
);
3426 XSETFASTINT (val
, 0);
3430 DEFUN ("set-terminal-coding-system-internal",
3431 Fset_terminal_coding_system_internal
,
3432 Sset_terminal_coding_system_internal
, 1, 1, 0, "")
3434 Lisp_Object coding_system
;
3436 CHECK_SYMBOL (coding_system
, 0);
3437 setup_coding_system (Fcheck_coding_system (coding_system
), &terminal_coding
);
3441 DEFUN ("terminal-coding-system",
3442 Fterminal_coding_system
, Sterminal_coding_system
, 0, 0, 0,
3443 "Return coding-system of your terminal.")
3446 return terminal_coding
.symbol
;
3449 DEFUN ("set-keyboard-coding-system-internal",
3450 Fset_keyboard_coding_system_internal
,
3451 Sset_keyboard_coding_system_internal
, 1, 1, 0, "")
3453 Lisp_Object coding_system
;
3455 CHECK_SYMBOL (coding_system
, 0);
3456 setup_coding_system (Fcheck_coding_system (coding_system
), &keyboard_coding
);
3460 DEFUN ("keyboard-coding-system",
3461 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 0, 0,
3462 "Return coding-system of what is sent from terminal keyboard.")
3465 return keyboard_coding
.symbol
;
3469 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
3470 Sfind_operation_coding_system
, 1, MANY
, 0,
3471 "Choose a coding system for an operation based on the target name.\n\
3472 The value names a pair of coding systems: (ENCODING-SYSTEM DECODING-SYSTEM).\n\
3473 ENCODING-SYSTEM is the coding system to use for encoding\n\
3474 \(in case OPERATION does encoding), and DECODING-SYSTEM is the coding system\n\
3475 for decoding (in case OPERATION does decoding).\n\
3477 The first argument OPERATION specifies an I/O primitive:\n\
3478 For file I/O, `insert-file-contents' or `write-region'.\n\
3479 For process I/O, `call-process', `call-process-region', or `start-process'.\n\
3480 For network I/O, `open-network-stream'.\n\
3482 The remaining arguments should be the same arguments that were passed\n\
3483 to the primitive. Depending on which primitive, one of those arguments\n\
3484 is selected as the TARGET. For example, if OPERATION does file I/O,\n\
3485 whichever argument specifies the file name is TARGET.\n\
3487 TARGET has a meaning which depends on OPERATION:\n\
3488 For file I/O, TARGET is a file name.\n\
3489 For process I/O, TARGET is a process name.\n\
3490 For network I/O, TARGET is a service name or a port number\n\
3492 This function looks up what specified for TARGET in,\n\
3493 `file-coding-system-alist', `process-coding-system-alist',\n\
3494 or `network-coding-system-alist' depending on OPERATION.\n\
3495 They may specify a coding system, a cons of coding systems,\n\
3496 or a function symbol to call.\n\
3497 In the last case, we call the function with one argument,\n\
3498 which is a list of all the arguments given to `find-coding-system'.")
3503 Lisp_Object operation
, target_idx
, target
, val
;
3504 register Lisp_Object chain
;
3507 error ("Too few arguments");
3508 operation
= args
[0];
3509 if (!SYMBOLP (operation
)
3510 || !INTEGERP (target_idx
= Fget (operation
, Qtarget_idx
)))
3511 error ("Invalid first arguement");
3512 if (nargs
< 1 + XINT (target_idx
))
3513 error ("Too few arguments for operation: %s",
3514 XSYMBOL (operation
)->name
->data
);
3515 target
= args
[XINT (target_idx
) + 1];
3516 if (!(STRINGP (target
)
3517 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
3518 error ("Invalid %dth argument", XINT (target_idx
) + 1);
3520 chain
= ((EQ (operation
, Qinsert_file_contents
)
3521 || EQ (operation
, Qwrite_region
))
3522 ? Vfile_coding_system_alist
3523 : (EQ (operation
, Qopen_network_stream
)
3524 ? Vnetwork_coding_system_alist
3525 : Vprocess_coding_system_alist
));
3529 for (; CONSP (chain
); chain
= XCONS (chain
)->cdr
)
3531 Lisp_Object elt
= XCONS (chain
)->car
;
3534 && ((STRINGP (target
)
3535 && STRINGP (XCONS (elt
)->car
)
3536 && fast_string_match (XCONS (elt
)->car
, target
) >= 0)
3537 || (INTEGERP (target
) && EQ (target
, XCONS (elt
)->car
))))
3539 val
= XCONS (elt
)->cdr
;
3542 if (! SYMBOLP (val
))
3544 if (! NILP (Fcoding_system_p (val
)))
3545 return Fcons (val
, val
);
3546 if (!NILP (Fboundp (val
)))
3547 return call2 (val
, Flist (nargs
, args
));
3557 /*** 8. Post-amble ***/
3563 /* Emacs' internal format specific initialize routine. */
3564 for (i
= 0; i
<= 0x20; i
++)
3565 emacs_code_class
[i
] = EMACS_control_code
;
3566 emacs_code_class
[0x0A] = EMACS_linefeed_code
;
3567 emacs_code_class
[0x0D] = EMACS_carriage_return_code
;
3568 for (i
= 0x21 ; i
< 0x7F; i
++)
3569 emacs_code_class
[i
] = EMACS_ascii_code
;
3570 emacs_code_class
[0x7F] = EMACS_control_code
;
3571 emacs_code_class
[0x80] = EMACS_leading_code_composition
;
3572 for (i
= 0x81; i
< 0xFF; i
++)
3573 emacs_code_class
[i
] = EMACS_invalid_code
;
3574 emacs_code_class
[LEADING_CODE_PRIVATE_11
] = EMACS_leading_code_3
;
3575 emacs_code_class
[LEADING_CODE_PRIVATE_12
] = EMACS_leading_code_3
;
3576 emacs_code_class
[LEADING_CODE_PRIVATE_21
] = EMACS_leading_code_4
;
3577 emacs_code_class
[LEADING_CODE_PRIVATE_22
] = EMACS_leading_code_4
;
3579 /* ISO2022 specific initialize routine. */
3580 for (i
= 0; i
< 0x20; i
++)
3581 iso_code_class
[i
] = ISO_control_code
;
3582 for (i
= 0x21; i
< 0x7F; i
++)
3583 iso_code_class
[i
] = ISO_graphic_plane_0
;
3584 for (i
= 0x80; i
< 0xA0; i
++)
3585 iso_code_class
[i
] = ISO_control_code
;
3586 for (i
= 0xA1; i
< 0xFF; i
++)
3587 iso_code_class
[i
] = ISO_graphic_plane_1
;
3588 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
3589 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
3590 iso_code_class
[ISO_CODE_CR
] = ISO_carriage_return
;
3591 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
3592 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
3593 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
3594 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
3595 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
3596 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
3597 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
3599 conversion_buffer_size
= MINIMUM_CONVERSION_BUFFER_SIZE
;
3600 conversion_buffer
= (char *) xmalloc (MINIMUM_CONVERSION_BUFFER_SIZE
);
3602 setup_coding_system (Qnil
, &keyboard_coding
);
3603 setup_coding_system (Qnil
, &terminal_coding
);
3610 Qtarget_idx
= intern ("target-idx");
3611 staticpro (&Qtarget_idx
);
3613 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
3614 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
3616 Qcall_process
= intern ("call-process");
3617 staticpro (&Qcall_process
);
3618 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
3620 Qcall_process_region
= intern ("call-process-region");
3621 staticpro (&Qcall_process_region
);
3622 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
3624 Qstart_process
= intern ("start-process");
3625 staticpro (&Qstart_process
);
3626 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
3628 Qopen_network_stream
= intern ("open-network-stream");
3629 staticpro (&Qopen_network_stream
);
3630 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
3632 Qcoding_system
= intern ("coding-system");
3633 staticpro (&Qcoding_system
);
3635 Qeol_type
= intern ("eol-type");
3636 staticpro (&Qeol_type
);
3638 Qbuffer_file_coding_system
= intern ("buffer-file-coding-system");
3639 staticpro (&Qbuffer_file_coding_system
);
3641 Qpost_read_conversion
= intern ("post-read-conversion");
3642 staticpro (&Qpost_read_conversion
);
3644 Qpre_write_conversion
= intern ("pre-write-conversion");
3645 staticpro (&Qpre_write_conversion
);
3647 Qcoding_system_spec
= intern ("coding-system-spec");
3648 staticpro (&Qcoding_system_spec
);
3650 Qcoding_system_p
= intern ("coding-system-p");
3651 staticpro (&Qcoding_system_p
);
3653 Qcoding_system_error
= intern ("coding-system-error");
3654 staticpro (&Qcoding_system_error
);
3656 Fput (Qcoding_system_error
, Qerror_conditions
,
3657 Fcons (Qcoding_system_error
, Fcons (Qerror
, Qnil
)));
3658 Fput (Qcoding_system_error
, Qerror_message
,
3659 build_string ("Coding-system error"));
3661 Qcoding_category_index
= intern ("coding-category-index");
3662 staticpro (&Qcoding_category_index
);
3666 for (i
= 0; i
< CODING_CATEGORY_IDX_MAX
; i
++)
3668 coding_category_table
[i
] = intern (coding_category_name
[i
]);
3669 staticpro (&coding_category_table
[i
]);
3670 Fput (coding_category_table
[i
], Qcoding_category_index
,
3675 Qcharacter_unification_table
= intern ("character-unification-table");
3676 staticpro (&Qcharacter_unification_table
);
3677 Fput (Qcharacter_unification_table
, Qchar_table_extra_slots
,
3680 Qcharacter_unification_table_for_decode
3681 = intern ("character-unification-table-for-decode");
3682 staticpro (&Qcharacter_unification_table_for_decode
);
3684 Qcharacter_unification_table_for_encode
3685 = intern ("character-unification-table-for-encode");
3686 staticpro (&Qcharacter_unification_table_for_encode
);
3688 defsubr (&Scoding_system_spec
);
3689 defsubr (&Scoding_system_p
);
3690 defsubr (&Sread_coding_system
);
3691 defsubr (&Sread_non_nil_coding_system
);
3692 defsubr (&Scheck_coding_system
);
3693 defsubr (&Sdetect_coding_region
);
3694 defsubr (&Sdecode_coding_region
);
3695 defsubr (&Sencode_coding_region
);
3696 defsubr (&Sdecode_coding_string
);
3697 defsubr (&Sencode_coding_string
);
3698 defsubr (&Sdecode_sjis_char
);
3699 defsubr (&Sencode_sjis_char
);
3700 defsubr (&Sdecode_big5_char
);
3701 defsubr (&Sencode_big5_char
);
3702 defsubr (&Sset_terminal_coding_system_internal
);
3703 defsubr (&Sterminal_coding_system
);
3704 defsubr (&Sset_keyboard_coding_system_internal
);
3705 defsubr (&Skeyboard_coding_system
);
3706 defsubr (&Sfind_operation_coding_system
);
3708 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list
,
3709 "List of coding-categories (symbols) ordered by priority.");
3713 Vcoding_category_list
= Qnil
;
3714 for (i
= CODING_CATEGORY_IDX_MAX
- 1; i
>= 0; i
--)
3715 Vcoding_category_list
3716 = Fcons (coding_category_table
[i
], Vcoding_category_list
);
3719 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read
,
3720 "A variable of internal use only.\n\
3721 If the value is a coding system, it is used for decoding on read operation.\n\
3722 If not, an appropriate element in `coding-system-alist' (which see) is used.");
3723 Vcoding_system_for_read
= Qnil
;
3725 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write
,
3726 "A variable of internal use only.\n\
3727 If the value is a coding system, it is used for encoding on write operation.\n\
3728 If not, an appropriate element in `coding-system-alist' (which see) is used.");
3729 Vcoding_system_for_write
= Qnil
;
3731 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used
,
3732 "Coding-system used in the latest file or process I/O.");
3733 Vlast_coding_system_used
= Qnil
;
3735 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist
,
3736 "Alist to decide a coding system to use for a file I/O operation.\n\
3737 The format is ((PATTERN . VAL) ...),\n\
3738 where PATTERN is a regular expression matching a file name,\n\
3739 VAL is a coding system, a cons of coding systems, or a function symbol.\n\
3740 If VAL is a coding system, it is used for both decoding and encoding\n\
3741 the file contents.\n\
3742 If VAL is a cons of coding systems, the car part is used for decoding,\n\
3743 and the cdr part is used for encoding.\n\
3744 If VAL is a function symbol, the function must return a coding system\n\
3745 or a cons of coding systems which are used as above.\n\
3747 See also the function `find-coding-system'.");
3748 Vfile_coding_system_alist
= Qnil
;
3750 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist
,
3751 "Alist to decide a coding system to use for a process I/O operation.\n\
3752 The format is ((PATTERN . VAL) ...),\n\
3753 where PATTERN is a regular expression matching a program name,\n\
3754 VAL is a coding system, a cons of coding systems, or a function symbol.\n\
3755 If VAL is a coding system, it is used for both decoding what received\n\
3756 from the program and encoding what sent to the program.\n\
3757 If VAL is a cons of coding systems, the car part is used for decoding,\n\
3758 and the cdr part is used for encoding.\n\
3759 If VAL is a function symbol, the function must return a coding system\n\
3760 or a cons of coding systems which are used as above.\n\
3762 See also the function `find-coding-system'.");
3763 Vprocess_coding_system_alist
= Qnil
;
3765 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist
,
3766 "Alist to decide a coding system to use for a network I/O operation.\n\
3767 The format is ((PATTERN . VAL) ...),\n\
3768 where PATTERN is a regular expression matching a network service name\n\
3769 or is a port number to connect to,\n\
3770 VAL is a coding system, a cons of coding systems, or a function symbol.\n\
3771 If VAL is a coding system, it is used for both decoding what received\n\
3772 from the network stream and encoding what sent to the network stream.\n\
3773 If VAL is a cons of coding systems, the car part is used for decoding,\n\
3774 and the cdr part is used for encoding.\n\
3775 If VAL is a function symbol, the function must return a coding system\n\
3776 or a cons of coding systems which are used as above.\n\
3778 See also the function `find-coding-system'.");
3779 Vnetwork_coding_system_alist
= Qnil
;
3781 DEFVAR_INT ("eol-mnemonic-unix", &eol_mnemonic_unix
,
3782 "Mnemonic character indicating UNIX-like end-of-line format (i.e. LF) .");
3783 eol_mnemonic_unix
= ':';
3785 DEFVAR_INT ("eol-mnemonic-dos", &eol_mnemonic_dos
,
3786 "Mnemonic character indicating DOS-like end-of-line format (i.e. CRLF).");
3787 eol_mnemonic_dos
= '\\';
3789 DEFVAR_INT ("eol-mnemonic-mac", &eol_mnemonic_mac
,
3790 "Mnemonic character indicating MAC-like end-of-line format (i.e. CR).");
3791 eol_mnemonic_mac
= '/';
3793 DEFVAR_INT ("eol-mnemonic-undecided", &eol_mnemonic_undecided
,
3794 "Mnemonic character indicating end-of-line format is not yet decided.");
3795 eol_mnemonic_undecided
= ':';
3797 DEFVAR_LISP ("enable-character-unification", &Venable_character_unification
,
3798 "Non-nil means ISO 2022 encoder/decoder do character unification.");
3799 Venable_character_unification
= Qt
;
3801 DEFVAR_LISP ("standard-character-unification-table-for-decode",
3802 &Vstandard_character_unification_table_for_decode
,
3803 "Table for unifying characters when reading.");
3804 Vstandard_character_unification_table_for_decode
= Qnil
;
3806 DEFVAR_LISP ("standard-character-unification-table-for-encode",
3807 &Vstandard_character_unification_table_for_encode
,
3808 "Table for unifying characters when writing.");
3809 Vstandard_character_unification_table_for_encode
= Qnil
;
3811 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_alist
,
3812 "Alist of charsets vs revision numbers.\n\
3813 While encoding, if a charset (car part of an element) is found,\n\
3814 designate it with the escape sequence identifing revision (cdr part of the element).");
3815 Vcharset_revision_alist
= Qnil
;
3817 DEFVAR_LISP ("default-process-coding-system",
3818 &Vdefault_process_coding_system
,
3819 "Cons of coding systems used for process I/O by default.\n\
3820 The car part is used for decoding a process output,\n\
3821 the cdr part is used for encoding a text to be sent to a process.");
3822 Vdefault_process_coding_system
= Qnil
;