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
;
263 /* Format of end-of-line decided by system. This is CODING_EOL_LF on
264 Unix, CODING_EOL_CRLF on DOS/Windows, and CODING_EOL_CR on Mac. */
269 Lisp_Object Qcoding_system_spec
, Qcoding_system_p
, Qcoding_system_error
;
271 /* Coding system emacs-mule is for converting only end-of-line format. */
272 Lisp_Object Qemacs_mule
;
274 /* Coding-systems are handed between Emacs Lisp programs and C internal
275 routines by the following three variables. */
276 /* Coding-system for reading files and receiving data from process. */
277 Lisp_Object Vcoding_system_for_read
;
278 /* Coding-system for writing files and sending data to process. */
279 Lisp_Object Vcoding_system_for_write
;
280 /* Coding-system actually used in the latest I/O. */
281 Lisp_Object Vlast_coding_system_used
;
283 /* Flag to inhibit code conversion of end-of-line format. */
284 int inhibit_eol_conversion
;
286 /* Coding-system of what terminal accept for displaying. */
287 struct coding_system terminal_coding
;
289 /* Coding-system of what is sent from terminal keyboard. */
290 struct coding_system keyboard_coding
;
292 Lisp_Object Vfile_coding_system_alist
;
293 Lisp_Object Vprocess_coding_system_alist
;
294 Lisp_Object Vnetwork_coding_system_alist
;
298 Lisp_Object Qcoding_category_index
;
300 /* List of symbols `coding-category-xxx' ordered by priority. */
301 Lisp_Object Vcoding_category_list
;
303 /* Table of coding-systems currently assigned to each coding-category. */
304 Lisp_Object coding_category_table
[CODING_CATEGORY_IDX_MAX
];
306 /* Table of names of symbol for each coding-category. */
307 char *coding_category_name
[CODING_CATEGORY_IDX_MAX
] = {
308 "coding-category-emacs-mule",
309 "coding-category-sjis",
310 "coding-category-iso-7",
311 "coding-category-iso-8-1",
312 "coding-category-iso-8-2",
313 "coding-category-iso-else",
314 "coding-category-big5",
315 "coding-category-binary"
318 /* Flag to tell if we look up unification table on character code
320 Lisp_Object Venable_character_unification
;
321 /* Standard unification table to look up on decoding (reading). */
322 Lisp_Object Vstandard_character_unification_table_for_decode
;
323 /* Standard unification table to look up on encoding (writing). */
324 Lisp_Object Vstandard_character_unification_table_for_encode
;
326 Lisp_Object Qcharacter_unification_table
;
327 Lisp_Object Qcharacter_unification_table_for_decode
;
328 Lisp_Object Qcharacter_unification_table_for_encode
;
330 /* Alist of charsets vs revision number. */
331 Lisp_Object Vcharset_revision_alist
;
333 /* Default coding systems used for process I/O. */
334 Lisp_Object Vdefault_process_coding_system
;
337 /*** 2. Emacs internal format (emacs-mule) handlers ***/
339 /* Emacs' internal format for encoding multiple character sets is a
340 kind of multi-byte encoding, i.e. encoding a character by a sequence
341 of one-byte codes of variable length. ASCII characters and control
342 characters (e.g. `tab', `newline') are represented by one-byte as
343 is. It takes the range 0x00 through 0x7F. The other characters
344 are represented by a sequence of `base leading-code', optional
345 `extended leading-code', and one or two `position-code's. Length
346 of the sequence is decided by the base leading-code. Leading-code
347 takes the range 0x80 through 0x9F, whereas extended leading-code
348 and position-code take the range 0xA0 through 0xFF. See the
349 document of `charset.h' for more detail about leading-code and
352 There's one exception in this rule. Special leading-code
353 `leading-code-composition' denotes that the following several
354 characters should be composed into one character. Leading-codes of
355 components (except for ASCII) are added 0x20. An ASCII character
356 component is represented by a 2-byte sequence of `0xA0' and
357 `ASCII-code + 0x80'. See also the document in `charset.h' for the
358 detail of composite character. Hence, we can summarize the code
361 --- CODE RANGE of Emacs' internal format ---
362 (character set) (range)
364 ELSE (1st byte) 0x80 .. 0x9F
365 (rest bytes) 0xA0 .. 0xFF
366 ---------------------------------------------
370 enum emacs_code_class_type emacs_code_class
[256];
372 /* Go to the next statement only if *SRC is accessible and the code is
373 greater than 0xA0. */
374 #define CHECK_CODE_RANGE_A0_FF \
376 if (src >= src_end) \
377 goto label_end_of_switch; \
378 else if (*src++ < 0xA0) \
382 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
383 Check if a text is encoded in Emacs' internal format. If it is,
384 return CODING_CATEGORY_MASK_EMASC_MULE, else return 0. */
387 detect_coding_emacs_mule (src
, src_end
)
388 unsigned char *src
, *src_end
;
393 while (src
< src_end
)
405 switch (emacs_code_class
[c
])
407 case EMACS_ascii_code
:
408 case EMACS_linefeed_code
:
411 case EMACS_control_code
:
412 if (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
416 case EMACS_invalid_code
:
419 case EMACS_leading_code_composition
: /* c == 0x80 */
421 CHECK_CODE_RANGE_A0_FF
;
426 case EMACS_leading_code_4
:
427 CHECK_CODE_RANGE_A0_FF
;
428 /* fall down to check it two more times ... */
430 case EMACS_leading_code_3
:
431 CHECK_CODE_RANGE_A0_FF
;
432 /* fall down to check it one more time ... */
434 case EMACS_leading_code_2
:
435 CHECK_CODE_RANGE_A0_FF
;
443 return CODING_CATEGORY_MASK_EMACS_MULE
;
447 /*** 3. ISO2022 handlers ***/
449 /* The following note describes the coding system ISO2022 briefly.
450 Since the intension of this note is to help understanding of the
451 programs in this file, some parts are NOT ACCURATE or OVERLY
452 SIMPLIFIED. For the thorough understanding, please refer to the
453 original document of ISO2022.
455 ISO2022 provides many mechanisms to encode several character sets
456 in 7-bit and 8-bit environment. If one choose 7-bite environment,
457 all text is encoded by codes of less than 128. This may make the
458 encoded text a little bit longer, but the text get more stability
459 to pass through several gateways (some of them split MSB off).
461 There are two kind of character set: control character set and
462 graphic character set. The former contains control characters such
463 as `newline' and `escape' to provide control functions (control
464 functions are provided also by escape sequence). The latter
465 contains graphic characters such as ' A' and '-'. Emacs recognizes
466 two control character sets and many graphic character sets.
468 Graphic character sets are classified into one of the following
469 four classes, DIMENSION1_CHARS94, DIMENSION1_CHARS96,
470 DIMENSION2_CHARS94, DIMENSION2_CHARS96 according to the number of
471 bytes (DIMENSION) and the number of characters in one dimension
472 (CHARS) of the set. In addition, each character set is assigned an
473 identification tag (called "final character" and denoted as <F>
474 here after) which is unique in each class. <F> of each character
475 set is decided by ECMA(*) when it is registered in ISO. Code range
476 of <F> is 0x30..0x7F (0x30..0x3F are for private use only).
478 Note (*): ECMA = European Computer Manufacturers Association
480 Here are examples of graphic character set [NAME(<F>)]:
481 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
482 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
483 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
484 o DIMENSION2_CHARS96 -- none for the moment
486 A code area (1byte=8bits) is divided into 4 areas, C0, GL, C1, and GR.
487 C0 [0x00..0x1F] -- control character plane 0
488 GL [0x20..0x7F] -- graphic character plane 0
489 C1 [0x80..0x9F] -- control character plane 1
490 GR [0xA0..0xFF] -- graphic character plane 1
492 A control character set is directly designated and invoked to C0 or
493 C1 by an escape sequence. The most common case is that ISO646's
494 control character set is designated/invoked to C0 and ISO6429's
495 control character set is designated/invoked to C1, and usually
496 these designations/invocations are omitted in a coded text. With
497 7-bit environment, only C0 can be used, and a control character for
498 C1 is encoded by an appropriate escape sequence to fit in the
499 environment. All control characters for C1 are defined the
500 corresponding escape sequences.
502 A graphic character set is at first designated to one of four
503 graphic registers (G0 through G3), then these graphic registers are
504 invoked to GL or GR. These designations and invocations can be
505 done independently. The most common case is that G0 is invoked to
506 GL, G1 is invoked to GR, and ASCII is designated to G0, and usually
507 these invocations and designations are omitted in a coded text.
508 With 7-bit environment, only GL can be used.
510 When a graphic character set of CHARS94 is invoked to GL, code 0x20
511 and 0x7F of GL area work as control characters SPACE and DEL
512 respectively, and code 0xA0 and 0xFF of GR area should not be used.
514 There are two ways of invocation: locking-shift and single-shift.
515 With locking-shift, the invocation lasts until the next different
516 invocation, whereas with single-shift, the invocation works only
517 for the following character and doesn't affect locking-shift.
518 Invocations are done by the following control characters or escape
521 ----------------------------------------------------------------------
522 function control char escape sequence description
523 ----------------------------------------------------------------------
524 SI (shift-in) 0x0F none invoke G0 to GL
525 SI (shift-out) 0x0E none invoke G1 to GL
526 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
527 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
528 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 into GL
529 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 into GL
530 ----------------------------------------------------------------------
531 The first four are for locking-shift. Control characters for these
532 functions are defined by macros ISO_CODE_XXX in `coding.h'.
534 Designations are done by the following escape sequences.
535 ----------------------------------------------------------------------
536 escape sequence description
537 ----------------------------------------------------------------------
538 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
539 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
540 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
541 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
542 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
543 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
544 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
545 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
546 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
547 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
548 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
549 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
550 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
551 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
552 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
553 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
554 ----------------------------------------------------------------------
556 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
557 of dimension 1, chars 94, and final character <F>, and etc.
559 Note (*): Although these designations are not allowed in ISO2022,
560 Emacs accepts them on decoding, and produces them on encoding
561 CHARS96 character set in a coding system which is characterized as
562 7-bit environment, non-locking-shift, and non-single-shift.
564 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
565 '(' can be omitted. We call this as "short-form" here after.
567 Now you may notice that there are a lot of ways for encoding the
568 same multilingual text in ISO2022. Actually, there exist many
569 coding systems such as Compound Text (used in X's inter client
570 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
571 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
572 localized platforms), and all of these are variants of ISO2022.
574 In addition to the above, Emacs handles two more kinds of escape
575 sequences: ISO6429's direction specification and Emacs' private
576 sequence for specifying character composition.
578 ISO6429's direction specification takes the following format:
579 o CSI ']' -- end of the current direction
580 o CSI '0' ']' -- end of the current direction
581 o CSI '1' ']' -- start of left-to-right text
582 o CSI '2' ']' -- start of right-to-left text
583 The control character CSI (0x9B: control sequence introducer) is
584 abbreviated to the escape sequence ESC '[' in 7-bit environment.
586 Character composition specification takes the following format:
587 o ESC '0' -- start character composition
588 o ESC '1' -- end character composition
589 Since these are not standard escape sequences of any ISO, the use
590 of them for these meaning is restricted to Emacs only. */
592 enum iso_code_class_type iso_code_class
[256];
594 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
595 Check if a text is encoded in ISO2022. If it is, returns an
596 integer in which appropriate flag bits any of:
597 CODING_CATEGORY_MASK_ISO_7
598 CODING_CATEGORY_MASK_ISO_8_1
599 CODING_CATEGORY_MASK_ISO_8_2
600 CODING_CATEGORY_MASK_ISO_ELSE
601 are set. If a code which should never appear in ISO2022 is found,
605 detect_coding_iso2022 (src
, src_end
)
606 unsigned char *src
, *src_end
;
608 int mask
= (CODING_CATEGORY_MASK_ISO_7
609 | CODING_CATEGORY_MASK_ISO_8_1
610 | CODING_CATEGORY_MASK_ISO_8_2
611 | CODING_CATEGORY_MASK_ISO_ELSE
);
612 int g1
= 0; /* 1 iff designating to G1. */
615 while (src
< src_end
)
625 && ((c
>= '(' && c
<= '/')
626 || c
== '$' && ((*src
>= '(' && *src
<= '/')
627 || (*src
>= '@' && *src
<= 'B'))))
629 /* Valid designation sequence. */
630 if (c
== ')' || (c
== '$' && *src
== ')'))
633 mask
&= ~CODING_CATEGORY_MASK_ISO_7
;
638 else if (c
== 'N' || c
== 'O' || c
== 'n' || c
== 'o')
639 return CODING_CATEGORY_MASK_ISO_ELSE
;
644 return CODING_CATEGORY_MASK_ISO_ELSE
;
650 mask
&= ~CODING_CATEGORY_MASK_ISO_7
;
662 mask
&= ~CODING_CATEGORY_MASK_ISO_7
;
663 while (src
< src_end
&& *src
>= 0xA0)
665 if (count
& 1 && src
< src_end
)
666 mask
&= ~CODING_CATEGORY_MASK_ISO_8_2
;
675 /* Decode a character of which charset is CHARSET and the 1st position
676 code is C1. If dimension of CHARSET is 2, the 2nd position code is
677 fetched from SRC and set to C2. If CHARSET is negative, it means
678 that we are decoding ill formed text, and what we can do is just to
681 #define DECODE_ISO_CHARACTER(charset, c1) \
683 int c_alt, charset_alt = (charset); \
684 if (COMPOSING_HEAD_P (coding->composing)) \
686 *dst++ = LEADING_CODE_COMPOSITION; \
687 if (COMPOSING_WITH_RULE_P (coding->composing)) \
688 /* To tell composition rules are embeded. */ \
690 coding->composing += 2; \
692 if ((charset) >= 0) \
694 if (CHARSET_DIMENSION (charset) == 2) \
695 ONE_MORE_BYTE (c2); \
696 if (!NILP (unification_table) \
697 && ((c_alt = unify_char (unification_table, \
698 -1, (charset), c1, c2)) >= 0)) \
699 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
701 if (charset_alt == CHARSET_ASCII || charset_alt < 0) \
702 DECODE_CHARACTER_ASCII (c1); \
703 else if (CHARSET_DIMENSION (charset_alt) == 1) \
704 DECODE_CHARACTER_DIMENSION1 (charset_alt, c1); \
706 DECODE_CHARACTER_DIMENSION2 (charset_alt, c1, c2); \
707 if (COMPOSING_WITH_RULE_P (coding->composing)) \
708 /* To tell a composition rule follows. */ \
709 coding->composing = COMPOSING_WITH_RULE_RULE; \
712 /* Set designation state into CODING. */
713 #define DECODE_DESIGNATION(reg, dimension, chars, final_char) \
715 int charset = ISO_CHARSET_TABLE (make_number (dimension), \
716 make_number (chars), \
717 make_number (final_char)); \
720 if (coding->direction == 1 \
721 && CHARSET_REVERSE_CHARSET (charset) >= 0) \
722 charset = CHARSET_REVERSE_CHARSET (charset); \
723 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
727 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
730 decode_coding_iso2022 (coding
, source
, destination
,
731 src_bytes
, dst_bytes
, consumed
)
732 struct coding_system
*coding
;
733 unsigned char *source
, *destination
;
734 int src_bytes
, dst_bytes
;
737 unsigned char *src
= source
;
738 unsigned char *src_end
= source
+ src_bytes
;
739 unsigned char *dst
= destination
;
740 unsigned char *dst_end
= destination
+ dst_bytes
;
741 /* Since the maximum bytes produced by each loop is 7, we subtract 6
742 from DST_END to assure that overflow checking is necessary only
743 at the head of loop. */
744 unsigned char *adjusted_dst_end
= dst_end
- 6;
746 /* Charsets invoked to graphic plane 0 and 1 respectively. */
747 int charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
748 int charset1
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 1);
749 Lisp_Object unification_table
750 = coding
->character_unification_table_for_decode
;
752 if (!NILP (Venable_character_unification
) && NILP (unification_table
))
753 unification_table
= Vstandard_character_unification_table_for_decode
;
755 while (src
< src_end
&& dst
< adjusted_dst_end
)
757 /* SRC_BASE remembers the start position in source in each loop.
758 The loop will be exited when there's not enough source text
759 to analyze long escape sequence or 2-byte code (within macros
760 ONE_MORE_BYTE or TWO_MORE_BYTES). In that case, SRC is reset
761 to SRC_BASE before exiting. */
762 unsigned char *src_base
= src
;
765 switch (iso_code_class
[c1
])
767 case ISO_0x20_or_0x7F
:
768 if (!coding
->composing
769 && (charset0
< 0 || CHARSET_CHARS (charset0
) == 94))
771 /* This is SPACE or DEL. */
775 /* This is a graphic character, we fall down ... */
777 case ISO_graphic_plane_0
:
778 if (coding
->composing
== COMPOSING_WITH_RULE_RULE
)
780 /* This is a composition rule. */
782 coding
->composing
= COMPOSING_WITH_RULE_TAIL
;
785 DECODE_ISO_CHARACTER (charset0
, c1
);
788 case ISO_0xA0_or_0xFF
:
789 if (charset1
< 0 || CHARSET_CHARS (charset1
) == 94)
795 /* This is a graphic character, we fall down ... */
797 case ISO_graphic_plane_1
:
798 DECODE_ISO_CHARACTER (charset1
, c1
);
801 case ISO_control_code
:
802 /* All ISO2022 control characters in this class have the
803 same representation in Emacs internal format. */
807 case ISO_carriage_return
:
808 if (coding
->eol_type
== CODING_EOL_CR
)
812 else if (coding
->eol_type
== CODING_EOL_CRLF
)
815 if (c1
== ISO_CODE_LF
)
830 if (CODING_SPEC_ISO_DESIGNATION (coding
, 1) < 0)
831 goto label_invalid_escape_sequence
;
832 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 1;
833 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
837 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 0;
838 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
841 case ISO_single_shift_2_7
:
842 case ISO_single_shift_2
:
843 /* SS2 is handled as an escape sequence of ESC 'N' */
845 goto label_escape_sequence
;
847 case ISO_single_shift_3
:
848 /* SS2 is handled as an escape sequence of ESC 'O' */
850 goto label_escape_sequence
;
852 case ISO_control_sequence_introducer
:
853 /* CSI is handled as an escape sequence of ESC '[' ... */
855 goto label_escape_sequence
;
859 label_escape_sequence
:
860 /* Escape sequences handled by Emacs are invocation,
861 designation, direction specification, and character
862 composition specification. */
865 case '&': /* revision of following character set */
867 if (!(c1
>= '@' && c1
<= '~'))
868 goto label_invalid_escape_sequence
;
870 if (c1
!= ISO_CODE_ESC
)
871 goto label_invalid_escape_sequence
;
873 goto label_escape_sequence
;
875 case '$': /* designation of 2-byte character set */
877 if (c1
>= '@' && c1
<= 'B')
878 { /* designation of JISX0208.1978, GB2312.1980,
880 DECODE_DESIGNATION (0, 2, 94, c1
);
882 else if (c1
>= 0x28 && c1
<= 0x2B)
883 { /* designation of DIMENSION2_CHARS94 character set */
885 DECODE_DESIGNATION (c1
- 0x28, 2, 94, c2
);
887 else if (c1
>= 0x2C && c1
<= 0x2F)
888 { /* designation of DIMENSION2_CHARS96 character set */
890 DECODE_DESIGNATION (c1
- 0x2C, 2, 96, c2
);
893 goto label_invalid_escape_sequence
;
896 case 'n': /* invocation of locking-shift-2 */
897 if (CODING_SPEC_ISO_DESIGNATION (coding
, 2) < 0)
898 goto label_invalid_escape_sequence
;
899 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 2;
900 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
903 case 'o': /* invocation of locking-shift-3 */
904 if (CODING_SPEC_ISO_DESIGNATION (coding
, 3) < 0)
905 goto label_invalid_escape_sequence
;
906 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 3;
907 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
910 case 'N': /* invocation of single-shift-2 */
911 if (CODING_SPEC_ISO_DESIGNATION (coding
, 2) < 0)
912 goto label_invalid_escape_sequence
;
914 charset
= CODING_SPEC_ISO_DESIGNATION (coding
, 2);
915 DECODE_ISO_CHARACTER (charset
, c1
);
918 case 'O': /* invocation of single-shift-3 */
919 if (CODING_SPEC_ISO_DESIGNATION (coding
, 3) < 0)
920 goto label_invalid_escape_sequence
;
922 charset
= CODING_SPEC_ISO_DESIGNATION (coding
, 3);
923 DECODE_ISO_CHARACTER (charset
, c1
);
926 case '0': /* start composing without embeded rules */
927 coding
->composing
= COMPOSING_NO_RULE_HEAD
;
930 case '1': /* end composing */
931 coding
->composing
= COMPOSING_NO
;
934 case '2': /* start composing with embeded rules */
935 coding
->composing
= COMPOSING_WITH_RULE_HEAD
;
938 case '[': /* specification of direction */
939 /* For the moment, nested direction is not supported.
940 So, the value of `coding->direction' is 0 or 1: 0
941 means left-to-right, 1 means right-to-left. */
945 case ']': /* end of the current direction */
946 coding
->direction
= 0;
948 case '0': /* end of the current direction */
949 case '1': /* start of left-to-right direction */
952 coding
->direction
= 0;
954 goto label_invalid_escape_sequence
;
957 case '2': /* start of right-to-left direction */
960 coding
->direction
= 1;
962 goto label_invalid_escape_sequence
;
966 goto label_invalid_escape_sequence
;
971 if (c1
>= 0x28 && c1
<= 0x2B)
972 { /* designation of DIMENSION1_CHARS94 character set */
974 DECODE_DESIGNATION (c1
- 0x28, 1, 94, c2
);
976 else if (c1
>= 0x2C && c1
<= 0x2F)
977 { /* designation of DIMENSION1_CHARS96 character set */
979 DECODE_DESIGNATION (c1
- 0x2C, 1, 96, c2
);
983 goto label_invalid_escape_sequence
;
986 /* We must update these variables now. */
987 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
988 charset1
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 1);
991 label_invalid_escape_sequence
:
993 int length
= src
- src_base
;
995 bcopy (src_base
, dst
, length
);
1002 coding
->carryover_size
= src
- src_base
;
1003 bcopy (src_base
, coding
->carryover
, coding
->carryover_size
);
1008 /* If this is the last block of the text to be decoded, we had
1009 better just flush out all remaining codes in the text although
1010 they are not valid characters. */
1011 if (coding
->last_block
)
1013 bcopy (src
, dst
, src_end
- src
);
1014 dst
+= (src_end
- src
);
1017 *consumed
= src
- source
;
1018 return dst
- destination
;
1021 /* ISO2022 encoding staffs. */
1024 It is not enough to say just "ISO2022" on encoding, but we have to
1025 specify more details. In Emacs, each coding-system of ISO2022
1026 variant has the following specifications:
1027 1. Initial designation to G0 thru G3.
1028 2. Allows short-form designation?
1029 3. ASCII should be designated to G0 before control characters?
1030 4. ASCII should be designated to G0 at end of line?
1031 5. 7-bit environment or 8-bit environment?
1032 6. Use locking-shift?
1033 7. Use Single-shift?
1034 And the following two are only for Japanese:
1035 8. Use ASCII in place of JIS0201-1976-Roman?
1036 9. Use JISX0208-1983 in place of JISX0208-1978?
1037 These specifications are encoded in `coding->flags' as flag bits
1038 defined by macros CODING_FLAG_ISO_XXX. See `coding.h' for more
1042 /* Produce codes (escape sequence) for designating CHARSET to graphic
1043 register REG. If <final-char> of CHARSET is '@', 'A', or 'B' and
1044 the coding system CODING allows, produce designation sequence of
1047 #define ENCODE_DESIGNATION(charset, reg, coding) \
1049 unsigned char final_char = CHARSET_ISO_FINAL_CHAR (charset); \
1050 char *intermediate_char_94 = "()*+"; \
1051 char *intermediate_char_96 = ",-./"; \
1053 = Fassq (make_number (charset), Vcharset_revision_alist); \
1054 if (! NILP (temp)) \
1056 *dst++ = ISO_CODE_ESC; \
1058 *dst++ = XINT (XCONS (temp)->cdr) + '@'; \
1060 *dst++ = ISO_CODE_ESC; \
1061 if (CHARSET_DIMENSION (charset) == 1) \
1063 if (CHARSET_CHARS (charset) == 94) \
1064 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
1066 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
1071 if (CHARSET_CHARS (charset) == 94) \
1073 if (! (coding->flags & CODING_FLAG_ISO_SHORT_FORM) \
1075 || final_char < '@' || final_char > 'B') \
1076 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
1079 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
1081 *dst++ = final_char; \
1082 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
1085 /* The following two macros produce codes (control character or escape
1086 sequence) for ISO2022 single-shift functions (single-shift-2 and
1089 #define ENCODE_SINGLE_SHIFT_2 \
1091 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1092 *dst++ = ISO_CODE_ESC, *dst++ = 'N'; \
1094 *dst++ = ISO_CODE_SS2; \
1095 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
1098 #define ENCODE_SINGLE_SHIFT_3 \
1100 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1101 *dst++ = ISO_CODE_ESC, *dst++ = 'O'; \
1103 *dst++ = ISO_CODE_SS3; \
1104 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
1107 /* The following four macros produce codes (control character or
1108 escape sequence) for ISO2022 locking-shift functions (shift-in,
1109 shift-out, locking-shift-2, and locking-shift-3). */
1111 #define ENCODE_SHIFT_IN \
1113 *dst++ = ISO_CODE_SI; \
1114 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0; \
1117 #define ENCODE_SHIFT_OUT \
1119 *dst++ = ISO_CODE_SO; \
1120 CODING_SPEC_ISO_INVOCATION (coding, 0) = 1; \
1123 #define ENCODE_LOCKING_SHIFT_2 \
1125 *dst++ = ISO_CODE_ESC, *dst++ = 'n'; \
1126 CODING_SPEC_ISO_INVOCATION (coding, 0) = 2; \
1129 #define ENCODE_LOCKING_SHIFT_3 \
1131 *dst++ = ISO_CODE_ESC, *dst++ = 'o'; \
1132 CODING_SPEC_ISO_INVOCATION (coding, 0) = 3; \
1135 /* Produce codes for a DIMENSION1 character of which character set is
1136 CHARSET and position-code is C1. Designation and invocation
1137 sequences are also produced in advance if necessary. */
1140 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
1142 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
1144 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1145 *dst++ = c1 & 0x7F; \
1147 *dst++ = c1 | 0x80; \
1148 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
1151 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
1153 *dst++ = c1 & 0x7F; \
1156 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
1158 *dst++ = c1 | 0x80; \
1162 /* Since CHARSET is not yet invoked to any graphic planes, we \
1163 must invoke it, or, at first, designate it to some graphic \
1164 register. Then repeat the loop to actually produce the \
1166 dst = encode_invocation_designation (charset, coding, dst); \
1169 /* Produce codes for a DIMENSION2 character of which character set is
1170 CHARSET and position-codes are C1 and C2. Designation and
1171 invocation codes are also produced in advance if necessary. */
1173 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
1175 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
1177 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1178 *dst++ = c1 & 0x7F, *dst++ = c2 & 0x7F; \
1180 *dst++ = c1 | 0x80, *dst++ = c2 | 0x80; \
1181 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
1184 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
1186 *dst++ = c1 & 0x7F, *dst++= c2 & 0x7F; \
1189 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
1191 *dst++ = c1 | 0x80, *dst++= c2 | 0x80; \
1195 /* Since CHARSET is not yet invoked to any graphic planes, we \
1196 must invoke it, or, at first, designate it to some graphic \
1197 register. Then repeat the loop to actually produce the \
1199 dst = encode_invocation_designation (charset, coding, dst); \
1202 #define ENCODE_ISO_CHARACTER(charset, c1, c2) \
1204 int c_alt, charset_alt; \
1205 if (!NILP (unification_table) \
1206 && ((c_alt = unify_char (unification_table, -1, charset, c1, c2)) \
1208 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
1210 charset_alt = charset; \
1211 if (CHARSET_DIMENSION (charset_alt) == 1) \
1212 ENCODE_ISO_CHARACTER_DIMENSION1 (charset_alt, c1); \
1214 ENCODE_ISO_CHARACTER_DIMENSION2 (charset_alt, c1, c2); \
1217 /* Produce designation and invocation codes at a place pointed by DST
1218 to use CHARSET. The element `spec.iso2022' of *CODING is updated.
1222 encode_invocation_designation (charset
, coding
, dst
)
1224 struct coding_system
*coding
;
1227 int reg
; /* graphic register number */
1229 /* At first, check designations. */
1230 for (reg
= 0; reg
< 4; reg
++)
1231 if (charset
== CODING_SPEC_ISO_DESIGNATION (coding
, reg
))
1236 /* CHARSET is not yet designated to any graphic registers. */
1237 /* At first check the requested designation. */
1238 reg
= CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
);
1239 if (reg
== CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
)
1240 /* Since CHARSET requests no special designation, designate it
1241 to graphic register 0. */
1244 ENCODE_DESIGNATION (charset
, reg
, coding
);
1247 if (CODING_SPEC_ISO_INVOCATION (coding
, 0) != reg
1248 && CODING_SPEC_ISO_INVOCATION (coding
, 1) != reg
)
1250 /* Since the graphic register REG is not invoked to any graphic
1251 planes, invoke it to graphic plane 0. */
1254 case 0: /* graphic register 0 */
1258 case 1: /* graphic register 1 */
1262 case 2: /* graphic register 2 */
1263 if (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
1264 ENCODE_SINGLE_SHIFT_2
;
1266 ENCODE_LOCKING_SHIFT_2
;
1269 case 3: /* graphic register 3 */
1270 if (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
1271 ENCODE_SINGLE_SHIFT_3
;
1273 ENCODE_LOCKING_SHIFT_3
;
1280 /* The following two macros produce codes for indicating composition. */
1281 #define ENCODE_COMPOSITION_NO_RULE_START *dst++ = ISO_CODE_ESC, *dst++ = '0'
1282 #define ENCODE_COMPOSITION_WITH_RULE_START *dst++ = ISO_CODE_ESC, *dst++ = '2'
1283 #define ENCODE_COMPOSITION_END *dst++ = ISO_CODE_ESC, *dst++ = '1'
1285 /* The following three macros produce codes for indicating direction
1287 #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
1289 if (coding->flags == CODING_FLAG_ISO_SEVEN_BITS) \
1290 *dst++ = ISO_CODE_ESC, *dst++ = '['; \
1292 *dst++ = ISO_CODE_CSI; \
1295 #define ENCODE_DIRECTION_R2L \
1296 ENCODE_CONTROL_SEQUENCE_INTRODUCER, *dst++ = '2', *dst++ = ']'
1298 #define ENCODE_DIRECTION_L2R \
1299 ENCODE_CONTROL_SEQUENCE_INTRODUCER, *dst++ = '0', *dst++ = ']'
1301 /* Produce codes for designation and invocation to reset the graphic
1302 planes and registers to initial state. */
1303 #define ENCODE_RESET_PLANE_AND_REGISTER \
1306 if (CODING_SPEC_ISO_INVOCATION (coding, 0) != 0) \
1308 for (reg = 0; reg < 4; reg++) \
1309 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg) >= 0 \
1310 && (CODING_SPEC_ISO_DESIGNATION (coding, reg) \
1311 != CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg))) \
1312 ENCODE_DESIGNATION \
1313 (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg), reg, coding); \
1316 /* Produce designation sequences of charsets in the line started from
1317 *SRC to a place pointed by DSTP.
1319 If the current block ends before any end-of-line, we may fail to
1320 find all the necessary *designations. */
1321 encode_designation_at_bol (coding
, table
, src
, src_end
, dstp
)
1322 struct coding_system
*coding
;
1324 unsigned char *src
, *src_end
, **dstp
;
1326 int charset
, c
, found
= 0, reg
;
1327 /* Table of charsets to be designated to each graphic register. */
1329 unsigned char *dst
= *dstp
;
1331 for (reg
= 0; reg
< 4; reg
++)
1334 while (src
< src_end
&& *src
!= '\n' && found
< 4)
1336 int bytes
= BYTES_BY_CHAR_HEAD (*src
);
1339 charset
= CHARSET_AT (src
);
1344 SPLIT_STRING(src
, bytes
, charset
, c1
, c2
);
1345 if ((c_alt
= unify_char (table
, -1, charset
, c1
, c2
)) >= 0)
1346 charset
= CHAR_CHARSET (c_alt
);
1349 reg
= CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
);
1350 if (r
[reg
] == CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
)
1361 for (reg
= 0; reg
< 4; reg
++)
1363 && CODING_SPEC_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
1364 ENCODE_DESIGNATION (r
[reg
], reg
, coding
);
1369 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
1372 encode_coding_iso2022 (coding
, source
, destination
,
1373 src_bytes
, dst_bytes
, consumed
)
1374 struct coding_system
*coding
;
1375 unsigned char *source
, *destination
;
1376 int src_bytes
, dst_bytes
;
1379 unsigned char *src
= source
;
1380 unsigned char *src_end
= source
+ src_bytes
;
1381 unsigned char *dst
= destination
;
1382 unsigned char *dst_end
= destination
+ dst_bytes
;
1383 /* Since the maximum bytes produced by each loop is 20, we subtract 19
1384 from DST_END to assure overflow checking is necessary only at the
1386 unsigned char *adjusted_dst_end
= dst_end
- 19;
1387 Lisp_Object unification_table
1388 = coding
->character_unification_table_for_encode
;
1390 if (!NILP (Venable_character_unification
) && NILP (unification_table
))
1391 unification_table
= Vstandard_character_unification_table_for_encode
;
1393 while (src
< src_end
&& dst
< adjusted_dst_end
)
1395 /* SRC_BASE remembers the start position in source in each loop.
1396 The loop will be exited when there's not enough source text
1397 to analyze multi-byte codes (within macros ONE_MORE_BYTE,
1398 TWO_MORE_BYTES, and THREE_MORE_BYTES). In that case, SRC is
1399 reset to SRC_BASE before exiting. */
1400 unsigned char *src_base
= src
;
1401 int charset
, c1
, c2
, c3
, c4
;
1403 if (coding
->flags
& CODING_FLAG_ISO_DESIGNATE_AT_BOL
1404 && CODING_SPEC_ISO_BOL (coding
))
1406 /* We have to produce designation sequences if any now. */
1407 encode_designation_at_bol (coding
, unification_table
,
1408 src
, src_end
, &dst
);
1409 CODING_SPEC_ISO_BOL (coding
) = 0;
1413 /* If we are seeing a component of a composite character, we are
1414 seeing a leading-code specially encoded for composition, or a
1415 composition rule if composing with rule. We must set C1
1416 to a normal leading-code or an ASCII code. If we are not at
1417 a composed character, we must reset the composition state. */
1418 if (COMPOSING_P (coding
->composing
))
1422 /* We are not in a composite character any longer. */
1423 coding
->composing
= COMPOSING_NO
;
1424 ENCODE_COMPOSITION_END
;
1428 if (coding
->composing
== COMPOSING_WITH_RULE_RULE
)
1431 coding
->composing
= COMPOSING_WITH_RULE_HEAD
;
1434 else if (coding
->composing
== COMPOSING_WITH_RULE_HEAD
)
1435 coding
->composing
= COMPOSING_WITH_RULE_RULE
;
1438 /* This is an ASCII component. */
1443 /* This is a leading-code of non ASCII component. */
1448 /* Now encode one character. C1 is a control character, an
1449 ASCII character, or a leading-code of multi-byte character. */
1450 switch (emacs_code_class
[c1
])
1452 case EMACS_ascii_code
:
1453 ENCODE_ISO_CHARACTER (CHARSET_ASCII
, c1
, /* dummy */ c2
);
1456 case EMACS_control_code
:
1457 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_CNTL
)
1458 ENCODE_RESET_PLANE_AND_REGISTER
;
1462 case EMACS_carriage_return_code
:
1463 if (!coding
->selective
)
1465 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_CNTL
)
1466 ENCODE_RESET_PLANE_AND_REGISTER
;
1470 /* fall down to treat '\r' as '\n' ... */
1472 case EMACS_linefeed_code
:
1473 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_EOL
)
1474 ENCODE_RESET_PLANE_AND_REGISTER
;
1475 if (coding
->flags
& CODING_FLAG_ISO_INIT_AT_BOL
)
1476 bcopy (coding
->spec
.iso2022
.initial_designation
,
1477 coding
->spec
.iso2022
.current_designation
,
1478 sizeof coding
->spec
.iso2022
.initial_designation
);
1479 if (coding
->eol_type
== CODING_EOL_LF
1480 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
1481 *dst
++ = ISO_CODE_LF
;
1482 else if (coding
->eol_type
== CODING_EOL_CRLF
)
1483 *dst
++ = ISO_CODE_CR
, *dst
++ = ISO_CODE_LF
;
1485 *dst
++ = ISO_CODE_CR
;
1486 CODING_SPEC_ISO_BOL (coding
) = 1;
1489 case EMACS_leading_code_2
:
1491 ENCODE_ISO_CHARACTER (c1
, c2
, /* dummy */ c3
);
1494 case EMACS_leading_code_3
:
1495 TWO_MORE_BYTES (c2
, c3
);
1496 if (c1
< LEADING_CODE_PRIVATE_11
)
1497 ENCODE_ISO_CHARACTER (c1
, c2
, c3
);
1499 ENCODE_ISO_CHARACTER (c2
, c3
, /* dummy */ c4
);
1502 case EMACS_leading_code_4
:
1503 THREE_MORE_BYTES (c2
, c3
, c4
);
1504 ENCODE_ISO_CHARACTER (c2
, c3
, c4
);
1507 case EMACS_leading_code_composition
:
1511 coding
->composing
= COMPOSING_WITH_RULE_HEAD
;
1512 ENCODE_COMPOSITION_WITH_RULE_START
;
1516 /* Rewind one byte because it is a character code of
1517 composition elements. */
1519 coding
->composing
= COMPOSING_NO_RULE_HEAD
;
1520 ENCODE_COMPOSITION_NO_RULE_START
;
1524 case EMACS_invalid_code
:
1530 coding
->carryover_size
= src
- src_base
;
1531 bcopy (src_base
, coding
->carryover
, coding
->carryover_size
);
1535 /* If this is the last block of the text to be encoded, we must
1536 reset graphic planes and registers to the initial state. */
1537 if (src
>= src_end
&& coding
->last_block
)
1539 ENCODE_RESET_PLANE_AND_REGISTER
;
1540 if (coding
->carryover_size
> 0
1541 && coding
->carryover_size
< (dst_end
- dst
))
1543 bcopy (coding
->carryover
, dst
, coding
->carryover_size
);
1544 dst
+= coding
->carryover_size
;
1545 coding
->carryover_size
= 0;
1548 *consumed
= src
- source
;
1549 return dst
- destination
;
1553 /*** 4. SJIS and BIG5 handlers ***/
1555 /* Although SJIS and BIG5 are not ISO's coding system, They are used
1556 quite widely. So, for the moment, Emacs supports them in the bare
1557 C code. But, in the future, they may be supported only by CCL. */
1559 /* SJIS is a coding system encoding three character sets: ASCII, right
1560 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
1561 as is. A character of charset katakana-jisx0201 is encoded by
1562 "position-code + 0x80". A character of charset japanese-jisx0208
1563 is encoded in 2-byte but two position-codes are divided and shifted
1564 so that it fit in the range below.
1566 --- CODE RANGE of SJIS ---
1567 (character set) (range)
1569 KATAKANA-JISX0201 0xA0 .. 0xDF
1570 JISX0208 (1st byte) 0x80 .. 0x9F and 0xE0 .. 0xFF
1571 (2nd byte) 0x40 .. 0xFF
1572 -------------------------------
1576 /* BIG5 is a coding system encoding two character sets: ASCII and
1577 Big5. An ASCII character is encoded as is. Big5 is a two-byte
1578 character set and is encoded in two-byte.
1580 --- CODE RANGE of BIG5 ---
1581 (character set) (range)
1583 Big5 (1st byte) 0xA1 .. 0xFE
1584 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
1585 --------------------------
1587 Since the number of characters in Big5 is larger than maximum
1588 characters in Emacs' charset (96x96), it can't be handled as one
1589 charset. So, in Emacs, Big5 is divided into two: `charset-big5-1'
1590 and `charset-big5-2'. Both are DIMENSION2 and CHARS94. The former
1591 contains frequently used characters and the latter contains less
1592 frequently used characters. */
1594 /* Macros to decode or encode a character of Big5 in BIG5. B1 and B2
1595 are the 1st and 2nd position-codes of Big5 in BIG5 coding system.
1596 C1 and C2 are the 1st and 2nd position-codes of of Emacs' internal
1597 format. CHARSET is `charset_big5_1' or `charset_big5_2'. */
1599 /* Number of Big5 characters which have the same code in 1st byte. */
1600 #define BIG5_SAME_ROW (0xFF - 0xA1 + 0x7F - 0x40)
1602 #define DECODE_BIG5(b1, b2, charset, c1, c2) \
1605 = (b1 - 0xA1) * BIG5_SAME_ROW + b2 - (b2 < 0x7F ? 0x40 : 0x62); \
1607 charset = charset_big5_1; \
1610 charset = charset_big5_2; \
1611 temp -= (0xC9 - 0xA1) * BIG5_SAME_ROW; \
1613 c1 = temp / (0xFF - 0xA1) + 0x21; \
1614 c2 = temp % (0xFF - 0xA1) + 0x21; \
1617 #define ENCODE_BIG5(charset, c1, c2, b1, b2) \
1619 unsigned int temp = (c1 - 0x21) * (0xFF - 0xA1) + (c2 - 0x21); \
1620 if (charset == charset_big5_2) \
1621 temp += BIG5_SAME_ROW * (0xC9 - 0xA1); \
1622 b1 = temp / BIG5_SAME_ROW + 0xA1; \
1623 b2 = temp % BIG5_SAME_ROW; \
1624 b2 += b2 < 0x3F ? 0x40 : 0x62; \
1627 #define DECODE_SJIS_BIG5_CHARACTER(charset, c1, c2) \
1629 int c_alt, charset_alt = (charset); \
1630 if (!NILP (unification_table) \
1631 && ((c_alt = unify_char (unification_table, \
1632 -1, (charset), c1, c2)) >= 0)) \
1633 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
1634 if (charset_alt == CHARSET_ASCII || charset_alt < 0) \
1635 DECODE_CHARACTER_ASCII (c1); \
1636 else if (CHARSET_DIMENSION (charset_alt) == 1) \
1637 DECODE_CHARACTER_DIMENSION1 (charset_alt, c1); \
1639 DECODE_CHARACTER_DIMENSION2 (charset_alt, c1, c2); \
1642 #define ENCODE_SJIS_BIG5_CHARACTER(charset, c1, c2) \
1644 int c_alt, charset_alt; \
1645 if (!NILP (unification_table) \
1646 && ((c_alt = unify_char (unification_table, -1, charset, c1, c2)) \
1648 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
1650 charset_alt = charset; \
1651 if (charset_alt == charset_ascii) \
1653 else if (CHARSET_DIMENSION (charset_alt) == 1) \
1655 if (sjis_p && charset_alt == charset_katakana_jisx0201) \
1658 *dst++ = charset_alt, *dst++ = c1; \
1662 c1 &= 0x7F, c2 &= 0x7F; \
1663 if (sjis_p && charset_alt == charset_jisx0208) \
1665 unsigned char s1, s2; \
1667 ENCODE_SJIS (c1, c2, s1, s2); \
1668 *dst++ = s1, *dst++ = s2; \
1671 && (charset_alt == charset_big5_1 \
1672 || charset_alt == charset_big5_2)) \
1674 unsigned char b1, b2; \
1676 ENCODE_BIG5 (charset_alt, c1, c2, b1, b2); \
1677 *dst++ = b1, *dst++ = b2; \
1680 *dst++ = charset_alt, *dst++ = c1, *dst++ = c2; \
1684 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1685 Check if a text is encoded in SJIS. If it is, return
1686 CODING_CATEGORY_MASK_SJIS, else return 0. */
1689 detect_coding_sjis (src
, src_end
)
1690 unsigned char *src
, *src_end
;
1694 while (src
< src_end
)
1697 if (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
1699 if ((c
>= 0x80 && c
< 0xA0) || c
>= 0xE0)
1701 if (src
< src_end
&& *src
++ < 0x40)
1705 return CODING_CATEGORY_MASK_SJIS
;
1708 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1709 Check if a text is encoded in BIG5. If it is, return
1710 CODING_CATEGORY_MASK_BIG5, else return 0. */
1713 detect_coding_big5 (src
, src_end
)
1714 unsigned char *src
, *src_end
;
1718 while (src
< src_end
)
1721 if (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
1728 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
1732 return CODING_CATEGORY_MASK_BIG5
;
1735 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
1736 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
1739 decode_coding_sjis_big5 (coding
, source
, destination
,
1740 src_bytes
, dst_bytes
, consumed
, sjis_p
)
1741 struct coding_system
*coding
;
1742 unsigned char *source
, *destination
;
1743 int src_bytes
, dst_bytes
;
1747 unsigned char *src
= source
;
1748 unsigned char *src_end
= source
+ src_bytes
;
1749 unsigned char *dst
= destination
;
1750 unsigned char *dst_end
= destination
+ dst_bytes
;
1751 /* Since the maximum bytes produced by each loop is 4, we subtract 3
1752 from DST_END to assure overflow checking is necessary only at the
1754 unsigned char *adjusted_dst_end
= dst_end
- 3;
1755 Lisp_Object unification_table
1756 = coding
->character_unification_table_for_decode
;
1758 if (!NILP (Venable_character_unification
) && NILP (unification_table
))
1759 unification_table
= Vstandard_character_unification_table_for_decode
;
1761 while (src
< src_end
&& dst
< adjusted_dst_end
)
1763 /* SRC_BASE remembers the start position in source in each loop.
1764 The loop will be exited when there's not enough source text
1765 to analyze two-byte character (within macro ONE_MORE_BYTE).
1766 In that case, SRC is reset to SRC_BASE before exiting. */
1767 unsigned char *src_base
= src
;
1768 unsigned char c1
= *src
++, c2
, c3
, c4
;
1772 if (coding
->eol_type
== CODING_EOL_CRLF
)
1778 /* To process C2 again, SRC is subtracted by 1. */
1787 DECODE_SJIS_BIG5_CHARACTER (charset_ascii
, c1
, /* dummy */ c2
);
1788 else if (c1
< 0xA0 || c1
>= 0xE0)
1790 /* SJIS -> JISX0208, BIG5 -> Big5 (only if 0xE0 <= c1 < 0xFF) */
1794 DECODE_SJIS (c1
, c2
, c3
, c4
);
1795 DECODE_SJIS_BIG5_CHARACTER (charset_jisx0208
, c3
, c4
);
1797 else if (c1
>= 0xE0 && c1
< 0xFF)
1802 DECODE_BIG5 (c1
, c2
, charset
, c3
, c4
);
1803 DECODE_SJIS_BIG5_CHARACTER (charset
, c3
, c4
);
1805 else /* Invalid code */
1810 /* SJIS -> JISX0201-Kana, BIG5 -> Big5 */
1812 DECODE_SJIS_BIG5_CHARACTER (charset_katakana_jisx0201
, c1
, /* dummy */ c2
);
1818 DECODE_BIG5 (c1
, c2
, charset
, c3
, c4
);
1819 DECODE_SJIS_BIG5_CHARACTER (charset
, c3
, c4
);
1825 coding
->carryover_size
= src
- src_base
;
1826 bcopy (src_base
, coding
->carryover
, coding
->carryover_size
);
1831 *consumed
= src
- source
;
1832 return dst
- destination
;
1835 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
1836 This function can encode `charset_ascii', `charset_katakana_jisx0201',
1837 `charset_jisx0208', `charset_big5_1', and `charset_big5-2'. We are
1838 sure that all these charsets are registered as official charset
1839 (i.e. do not have extended leading-codes). Characters of other
1840 charsets are produced without any encoding. If SJIS_P is 1, encode
1841 SJIS text, else encode BIG5 text. */
1844 encode_coding_sjis_big5 (coding
, source
, destination
,
1845 src_bytes
, dst_bytes
, consumed
, sjis_p
)
1846 struct coding_system
*coding
;
1847 unsigned char *source
, *destination
;
1848 int src_bytes
, dst_bytes
;
1852 unsigned char *src
= source
;
1853 unsigned char *src_end
= source
+ src_bytes
;
1854 unsigned char *dst
= destination
;
1855 unsigned char *dst_end
= destination
+ dst_bytes
;
1856 /* Since the maximum bytes produced by each loop is 2, we subtract 1
1857 from DST_END to assure overflow checking is necessary only at the
1859 unsigned char *adjusted_dst_end
= dst_end
- 1;
1860 Lisp_Object unification_table
1861 = coding
->character_unification_table_for_encode
;
1863 if (!NILP (Venable_character_unification
) && NILP (unification_table
))
1864 unification_table
= Vstandard_character_unification_table_for_encode
;
1866 while (src
< src_end
&& dst
< adjusted_dst_end
)
1868 /* SRC_BASE remembers the start position in source in each loop.
1869 The loop will be exited when there's not enough source text
1870 to analyze multi-byte codes (within macros ONE_MORE_BYTE and
1871 TWO_MORE_BYTES). In that case, SRC is reset to SRC_BASE
1873 unsigned char *src_base
= src
;
1874 unsigned char c1
= *src
++, c2
, c3
, c4
;
1876 if (coding
->composing
)
1883 else if (c1
>= 0xA0)
1886 coding
->composing
= 0;
1889 switch (emacs_code_class
[c1
])
1891 case EMACS_ascii_code
:
1892 ENCODE_SJIS_BIG5_CHARACTER (charset_ascii
, c1
, /* dummy */ c2
);
1895 case EMACS_control_code
:
1899 case EMACS_carriage_return_code
:
1900 if (!coding
->selective
)
1905 /* fall down to treat '\r' as '\n' ... */
1907 case EMACS_linefeed_code
:
1908 if (coding
->eol_type
== CODING_EOL_LF
1909 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
1911 else if (coding
->eol_type
== CODING_EOL_CRLF
)
1912 *dst
++ = '\r', *dst
++ = '\n';
1917 case EMACS_leading_code_2
:
1919 ENCODE_SJIS_BIG5_CHARACTER (c1
, c2
, /* dummy */ c3
);
1922 case EMACS_leading_code_3
:
1923 TWO_MORE_BYTES (c2
, c3
);
1924 ENCODE_SJIS_BIG5_CHARACTER (c1
, c2
, c3
);
1927 case EMACS_leading_code_4
:
1928 THREE_MORE_BYTES (c2
, c3
, c4
);
1929 ENCODE_SJIS_BIG5_CHARACTER (c2
, c3
, c4
);
1932 case EMACS_leading_code_composition
:
1933 coding
->composing
= 1;
1936 default: /* i.e. case EMACS_invalid_code: */
1942 coding
->carryover_size
= src
- src_base
;
1943 bcopy (src_base
, coding
->carryover
, coding
->carryover_size
);
1948 *consumed
= src
- source
;
1949 return dst
- destination
;
1953 /*** 5. End-of-line handlers ***/
1955 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
1956 This function is called only when `coding->eol_type' is
1957 CODING_EOL_CRLF or CODING_EOL_CR. */
1959 decode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
1960 struct coding_system
*coding
;
1961 unsigned char *source
, *destination
;
1962 int src_bytes
, dst_bytes
;
1965 unsigned char *src
= source
;
1966 unsigned char *src_end
= source
+ src_bytes
;
1967 unsigned char *dst
= destination
;
1968 unsigned char *dst_end
= destination
+ dst_bytes
;
1971 switch (coding
->eol_type
)
1973 case CODING_EOL_CRLF
:
1975 /* Since the maximum bytes produced by each loop is 2, we
1976 subtract 1 from DST_END to assure overflow checking is
1977 necessary only at the head of loop. */
1978 unsigned char *adjusted_dst_end
= dst_end
- 1;
1980 while (src
< src_end
&& dst
< adjusted_dst_end
)
1982 unsigned char *src_base
= src
;
1983 unsigned char c
= *src
++;
1996 coding
->carryover_size
= src
- src_base
;
1997 bcopy (src_base
, coding
->carryover
, coding
->carryover_size
);
2001 *consumed
= src
- source
;
2002 produced
= dst
- destination
;
2007 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
2008 bcopy (source
, destination
, produced
);
2009 dst_end
= destination
+ produced
;
2010 while (dst
< dst_end
)
2011 if (*dst
++ == '\r') dst
[-1] = '\n';
2012 *consumed
= produced
;
2015 default: /* i.e. case: CODING_EOL_LF */
2016 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
2017 bcopy (source
, destination
, produced
);
2018 *consumed
= produced
;
2025 /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". Encode
2026 format of end-of-line according to `coding->eol_type'. If
2027 `coding->selective' is 1, code '\r' in source text also means
2030 encode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
2031 struct coding_system
*coding
;
2032 unsigned char *source
, *destination
;
2033 int src_bytes
, dst_bytes
;
2036 unsigned char *src
= source
;
2037 unsigned char *dst
= destination
;
2043 switch (coding
->eol_type
)
2046 case CODING_EOL_UNDECIDED
:
2047 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
2048 bcopy (source
, destination
, produced
);
2049 if (coding
->selective
)
2053 if (*dst
++ == '\r') dst
[-1] = '\n';
2055 *consumed
= produced
;
2057 case CODING_EOL_CRLF
:
2060 unsigned char *src_end
= source
+ src_bytes
;
2061 unsigned char *dst_end
= destination
+ dst_bytes
;
2062 /* Since the maximum bytes produced by each loop is 2, we
2063 subtract 1 from DST_END to assure overflow checking is
2064 necessary only at the head of loop. */
2065 unsigned char *adjusted_dst_end
= dst_end
- 1;
2067 while (src
< src_end
&& dst
< adjusted_dst_end
)
2070 if (c
== '\n' || (c
== '\r' && coding
->selective
))
2071 *dst
++ = '\r', *dst
++ = '\n';
2075 produced
= dst
- destination
;
2076 *consumed
= src
- source
;
2080 default: /* i.e. case CODING_EOL_CR: */
2081 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
2082 bcopy (source
, destination
, produced
);
2086 if (*dst
++ == '\n') dst
[-1] = '\r';
2088 *consumed
= produced
;
2095 /*** 6. C library functions ***/
2097 /* In Emacs Lisp, coding system is represented by a Lisp symbol which
2098 has a property `coding-system'. The value of this property is a
2099 vector of length 5 (called as coding-vector). Among elements of
2100 this vector, the first (element[0]) and the fifth (element[4])
2101 carry important information for decoding/encoding. Before
2102 decoding/encoding, this information should be set in fields of a
2103 structure of type `coding_system'.
2105 A value of property `coding-system' can be a symbol of another
2106 subsidiary coding-system. In that case, Emacs gets coding-vector
2109 `element[0]' contains information to be set in `coding->type'. The
2110 value and its meaning is as follows:
2112 0 -- coding_type_emacs_mule
2113 1 -- coding_type_sjis
2114 2 -- coding_type_iso2022
2115 3 -- coding_type_big5
2116 4 -- coding_type_ccl encoder/decoder written in CCL
2117 nil -- coding_type_no_conversion
2118 t -- coding_type_undecided (automatic conversion on decoding,
2119 no-conversion on encoding)
2121 `element[4]' contains information to be set in `coding->flags' and
2122 `coding->spec'. The meaning varies by `coding->type'.
2124 If `coding->type' is `coding_type_iso2022', element[4] is a vector
2125 of length 32 (of which the first 13 sub-elements are used now).
2126 Meanings of these sub-elements are:
2128 sub-element[N] where N is 0 through 3: to be set in `coding->spec.iso2022'
2129 If the value is an integer of valid charset, the charset is
2130 assumed to be designated to graphic register N initially.
2132 If the value is minus, it is a minus value of charset which
2133 reserves graphic register N, which means that the charset is
2134 not designated initially but should be designated to graphic
2135 register N just before encoding a character in that charset.
2137 If the value is nil, graphic register N is never used on
2140 sub-element[N] where N is 4 through 11: to be set in `coding->flags'
2141 Each value takes t or nil. See the section ISO2022 of
2142 `coding.h' for more information.
2144 If `coding->type' is `coding_type_big5', element[4] is t to denote
2145 BIG5-ETen or nil to denote BIG5-HKU.
2147 If `coding->type' takes the other value, element[4] is ignored.
2149 Emacs Lisp's coding system also carries information about format of
2150 end-of-line in a value of property `eol-type'. If the value is
2151 integer, 0 means CODING_EOL_LF, 1 means CODING_EOL_CRLF, and 2
2152 means CODING_EOL_CR. If it is not integer, it should be a vector
2153 of subsidiary coding systems of which property `eol-type' has one
2158 /* Extract information for decoding/encoding from CODING_SYSTEM_SYMBOL
2159 and set it in CODING. If CODING_SYSTEM_SYMBOL is invalid, CODING
2160 is setup so that no conversion is necessary and return -1, else
2164 setup_coding_system (coding_system
, coding
)
2165 Lisp_Object coding_system
;
2166 struct coding_system
*coding
;
2168 Lisp_Object type
, eol_type
;
2170 /* At first, set several fields default values. */
2171 coding
->require_flushing
= 0;
2172 coding
->last_block
= 0;
2173 coding
->selective
= 0;
2174 coding
->composing
= 0;
2175 coding
->direction
= 0;
2176 coding
->carryover_size
= 0;
2177 coding
->post_read_conversion
= coding
->pre_write_conversion
= Qnil
;
2178 coding
->character_unification_table_for_decode
= Qnil
;
2179 coding
->character_unification_table_for_encode
= Qnil
;
2181 Vlast_coding_system_used
= coding
->symbol
= coding_system
;
2183 /* Get value of property `coding-system' until we get a vector.
2184 While doing that, also get values of properties
2185 `post-read-conversion', `pre-write-conversion',
2186 `character-unification-table-for-decode',
2187 `character-unification-table-for-encode' and `eol-type'. */
2188 while (!NILP (coding_system
) && SYMBOLP (coding_system
))
2190 if (NILP (coding
->post_read_conversion
))
2191 coding
->post_read_conversion
= Fget (coding_system
,
2192 Qpost_read_conversion
);
2193 if (NILP (coding
->pre_write_conversion
))
2194 coding
->pre_write_conversion
= Fget (coding_system
,
2195 Qpre_write_conversion
);
2196 if (!inhibit_eol_conversion
&& NILP (eol_type
))
2197 eol_type
= Fget (coding_system
, Qeol_type
);
2199 if (NILP (coding
->character_unification_table_for_decode
))
2200 coding
->character_unification_table_for_decode
2201 = Fget (coding_system
, Qcharacter_unification_table_for_decode
);
2203 if (NILP (coding
->character_unification_table_for_encode
))
2204 coding
->character_unification_table_for_encode
2205 = Fget (coding_system
, Qcharacter_unification_table_for_encode
);
2207 coding_system
= Fget (coding_system
, Qcoding_system
);
2210 while (!NILP (coding
->character_unification_table_for_decode
)
2211 && SYMBOLP (coding
->character_unification_table_for_decode
))
2212 coding
->character_unification_table_for_decode
2213 = Fget (coding
->character_unification_table_for_decode
,
2214 Qcharacter_unification_table_for_decode
);
2215 if (!NILP (coding
->character_unification_table_for_decode
)
2216 && !CHAR_TABLE_P (coding
->character_unification_table_for_decode
))
2217 coding
->character_unification_table_for_decode
= Qnil
;
2219 while (!NILP (coding
->character_unification_table_for_encode
)
2220 && SYMBOLP (coding
->character_unification_table_for_encode
))
2221 coding
->character_unification_table_for_encode
2222 = Fget (coding
->character_unification_table_for_encode
,
2223 Qcharacter_unification_table_for_encode
);
2224 if (!NILP (coding
->character_unification_table_for_encode
)
2225 && !CHAR_TABLE_P (coding
->character_unification_table_for_encode
))
2226 coding
->character_unification_table_for_encode
= Qnil
;
2228 if (!VECTORP (coding_system
)
2229 || XVECTOR (coding_system
)->size
!= 5)
2230 goto label_invalid_coding_system
;
2232 if (VECTORP (eol_type
))
2233 coding
->eol_type
= CODING_EOL_UNDECIDED
;
2234 else if (XFASTINT (eol_type
) == 1)
2235 coding
->eol_type
= CODING_EOL_CRLF
;
2236 else if (XFASTINT (eol_type
) == 2)
2237 coding
->eol_type
= CODING_EOL_CR
;
2239 coding
->eol_type
= CODING_EOL_LF
;
2241 type
= XVECTOR (coding_system
)->contents
[0];
2242 switch (XFASTINT (type
))
2245 coding
->type
= coding_type_emacs_mule
;
2249 coding
->type
= coding_type_sjis
;
2253 coding
->type
= coding_type_iso2022
;
2255 Lisp_Object val
= XVECTOR (coding_system
)->contents
[4];
2257 int i
, charset
, default_reg_bits
= 0;
2259 if (!VECTORP (val
) || XVECTOR (val
)->size
!= 32)
2260 goto label_invalid_coding_system
;
2262 flags
= XVECTOR (val
)->contents
;
2264 = ((NILP (flags
[4]) ? 0 : CODING_FLAG_ISO_SHORT_FORM
)
2265 | (NILP (flags
[5]) ? 0 : CODING_FLAG_ISO_RESET_AT_EOL
)
2266 | (NILP (flags
[6]) ? 0 : CODING_FLAG_ISO_RESET_AT_CNTL
)
2267 | (NILP (flags
[7]) ? 0 : CODING_FLAG_ISO_SEVEN_BITS
)
2268 | (NILP (flags
[8]) ? 0 : CODING_FLAG_ISO_LOCKING_SHIFT
)
2269 | (NILP (flags
[9]) ? 0 : CODING_FLAG_ISO_SINGLE_SHIFT
)
2270 | (NILP (flags
[10]) ? 0 : CODING_FLAG_ISO_USE_ROMAN
)
2271 | (NILP (flags
[11]) ? 0 : CODING_FLAG_ISO_USE_OLDJIS
)
2272 | (NILP (flags
[12]) ? 0 : CODING_FLAG_ISO_NO_DIRECTION
)
2273 | (NILP (flags
[13]) ? 0 : CODING_FLAG_ISO_INIT_AT_BOL
)
2274 | (NILP (flags
[14]) ? 0 : CODING_FLAG_ISO_DESIGNATE_AT_BOL
));
2276 /* Invoke graphic register 0 to plane 0. */
2277 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 0;
2278 /* Invoke graphic register 1 to plane 1 if we can use full 8-bit. */
2279 CODING_SPEC_ISO_INVOCATION (coding
, 1)
2280 = (coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
? -1 : 1);
2281 /* Not single shifting at first. */
2282 CODING_SPEC_ISO_SINGLE_SHIFTING(coding
) = 0;
2283 /* Beginning of buffer should also be regarded as bol. */
2284 CODING_SPEC_ISO_BOL(coding
) = 1;
2286 /* Checks FLAGS[REG] (REG = 0, 1, 2 3) and decide designations.
2287 FLAGS[REG] can be one of below:
2288 integer CHARSET: CHARSET occupies register I,
2289 t: designate nothing to REG initially, but can be used
2291 list of integer, nil, or t: designate the first
2292 element (if integer) to REG initially, the remaining
2293 elements (if integer) is designated to REG on request,
2294 if an element is t, REG can be used by any charset,
2295 nil: REG is never used. */
2296 for (charset
= 0; charset
<= MAX_CHARSET
; charset
++)
2297 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2298 = CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
;
2299 for (i
= 0; i
< 4; i
++)
2301 if (INTEGERP (flags
[i
])
2302 && (charset
= XINT (flags
[i
]), CHARSET_VALID_P (charset
))
2303 || (charset
= get_charset_id (flags
[i
])) >= 0)
2305 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = charset
;
2306 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
) = i
;
2308 else if (EQ (flags
[i
], Qt
))
2310 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
2311 default_reg_bits
|= 1 << i
;
2313 else if (CONSP (flags
[i
]))
2315 Lisp_Object tail
= flags
[i
];
2317 if (INTEGERP (XCONS (tail
)->car
)
2318 && (charset
= XINT (XCONS (tail
)->car
),
2319 CHARSET_VALID_P (charset
))
2320 || (charset
= get_charset_id (XCONS (tail
)->car
)) >= 0)
2322 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = charset
;
2323 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
) =i
;
2326 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
2327 tail
= XCONS (tail
)->cdr
;
2328 while (CONSP (tail
))
2330 if (INTEGERP (XCONS (tail
)->car
)
2331 && (charset
= XINT (XCONS (tail
)->car
),
2332 CHARSET_VALID_P (charset
))
2333 || (charset
= get_charset_id (XCONS (tail
)->car
)) >= 0)
2334 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2336 else if (EQ (XCONS (tail
)->car
, Qt
))
2337 default_reg_bits
|= 1 << i
;
2338 tail
= XCONS (tail
)->cdr
;
2342 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
2344 CODING_SPEC_ISO_DESIGNATION (coding
, i
)
2345 = CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
);
2348 if (! (coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
))
2350 /* REG 1 can be used only by locking shift in 7-bit env. */
2351 if (coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
)
2352 default_reg_bits
&= ~2;
2353 if (! (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
))
2354 /* Without any shifting, only REG 0 and 1 can be used. */
2355 default_reg_bits
&= 3;
2358 for (charset
= 0; charset
<= MAX_CHARSET
; charset
++)
2359 if (CHARSET_VALID_P (charset
)
2360 && (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2361 == CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
))
2363 /* We have not yet decided where to designate CHARSET. */
2364 int reg_bits
= default_reg_bits
;
2366 if (CHARSET_CHARS (charset
) == 96)
2367 /* A charset of CHARS96 can't be designated to REG 0. */
2371 /* There exist some default graphic register. */
2372 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2374 ? 0 : (reg_bits
& 2 ? 1 : (reg_bits
& 4 ? 2 : 3)));
2376 /* We anyway have to designate CHARSET to somewhere. */
2377 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2378 = (CHARSET_CHARS (charset
) == 94
2380 : ((coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
2381 || ! coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
)
2383 : (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
2387 coding
->require_flushing
= 1;
2391 coding
->type
= coding_type_big5
;
2393 = (NILP (XVECTOR (coding_system
)->contents
[4])
2394 ? CODING_FLAG_BIG5_HKU
2395 : CODING_FLAG_BIG5_ETEN
);
2399 coding
->type
= coding_type_ccl
;
2401 Lisp_Object val
= XVECTOR (coding_system
)->contents
[4];
2403 && VECTORP (XCONS (val
)->car
)
2404 && VECTORP (XCONS (val
)->cdr
))
2406 setup_ccl_program (&(coding
->spec
.ccl
.decoder
), XCONS (val
)->car
);
2407 setup_ccl_program (&(coding
->spec
.ccl
.encoder
), XCONS (val
)->cdr
);
2410 goto label_invalid_coding_system
;
2412 coding
->require_flushing
= 1;
2417 coding
->type
= coding_type_undecided
;
2419 coding
->type
= coding_type_no_conversion
;
2424 label_invalid_coding_system
:
2425 coding
->type
= coding_type_no_conversion
;
2426 coding
->eol_type
= CODING_EOL_LF
;
2427 coding
->symbol
= coding
->pre_write_conversion
= coding
->post_read_conversion
2432 /* Emacs has a mechanism to automatically detect a coding system if it
2433 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
2434 it's impossible to distinguish some coding systems accurately
2435 because they use the same range of codes. So, at first, coding
2436 systems are categorized into 7, those are:
2438 o coding-category-emacs-mule
2440 The category for a coding system which has the same code range
2441 as Emacs' internal format. Assigned the coding-system (Lisp
2442 symbol) `emacs-mule' by default.
2444 o coding-category-sjis
2446 The category for a coding system which has the same code range
2447 as SJIS. Assigned the coding-system (Lisp
2448 symbol) `shift-jis' by default.
2450 o coding-category-iso-7
2452 The category for a coding system which has the same code range
2453 as ISO2022 of 7-bit environment. Assigned the coding-system
2454 (Lisp symbol) `iso-2022-7' by default.
2456 o coding-category-iso-8-1
2458 The category for a coding system which has the same code range
2459 as ISO2022 of 8-bit environment and graphic plane 1 used only
2460 for DIMENSION1 charset. Assigned the coding-system (Lisp
2461 symbol) `iso-8859-1' by default.
2463 o coding-category-iso-8-2
2465 The category for a coding system which has the same code range
2466 as ISO2022 of 8-bit environment and graphic plane 1 used only
2467 for DIMENSION2 charset. Assigned the coding-system (Lisp
2468 symbol) `euc-japan' by default.
2470 o coding-category-iso-else
2472 The category for a coding system which has the same code range
2473 as ISO2022 but not belongs to any of the above three
2474 categories. Assigned the coding-system (Lisp symbol)
2475 `iso-2022-ss2-7' by default.
2477 o coding-category-big5
2479 The category for a coding system which has the same code range
2480 as BIG5. Assigned the coding-system (Lisp symbol)
2481 `cn-big5' by default.
2483 o coding-category-binary
2485 The category for a coding system not categorized in any of the
2486 above. Assigned the coding-system (Lisp symbol)
2487 `no-conversion' by default.
2489 Each of them is a Lisp symbol and the value is an actual
2490 `coding-system's (this is also a Lisp symbol) assigned by a user.
2491 What Emacs does actually is to detect a category of coding system.
2492 Then, it uses a `coding-system' assigned to it. If Emacs can't
2493 decide only one possible category, it selects a category of the
2494 highest priority. Priorities of categories are also specified by a
2495 user in a Lisp variable `coding-category-list'.
2499 /* Detect how a text of length SRC_BYTES pointed by SRC is encoded.
2500 If it detects possible coding systems, return an integer in which
2501 appropriate flag bits are set. Flag bits are defined by macros
2502 CODING_CATEGORY_MASK_XXX in `coding.h'. */
2505 detect_coding_mask (src
, src_bytes
)
2509 register unsigned char c
;
2510 unsigned char *src_end
= src
+ src_bytes
;
2513 /* At first, skip all ASCII characters and control characters except
2514 for three ISO2022 specific control characters. */
2515 label_loop_detect_coding
:
2516 while (src
< src_end
)
2520 || (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
))
2526 /* We found nothing other than ASCII. There's nothing to do. */
2527 return CODING_CATEGORY_MASK_ANY
;
2529 /* The text seems to be encoded in some multilingual coding system.
2530 Now, try to find in which coding system the text is encoded. */
2533 /* i.e. (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) */
2534 /* C is an ISO2022 specific control code of C0. */
2535 mask
= detect_coding_iso2022 (src
, src_end
);
2537 if (mask
== CODING_CATEGORY_MASK_ANY
)
2538 /* No valid ISO2022 code follows C. Try again. */
2539 goto label_loop_detect_coding
;
2541 else if (c
== ISO_CODE_SS2
|| c
== ISO_CODE_SS3
|| c
== ISO_CODE_CSI
)
2542 /* C is an ISO2022 specific control code of C1,
2543 or the first byte of SJIS's 2-byte character code,
2544 or a leading code of Emacs. */
2545 mask
= (detect_coding_iso2022 (src
, src_end
)
2546 | detect_coding_sjis (src
, src_end
)
2547 | detect_coding_emacs_mule (src
, src_end
));
2550 /* C is the first byte of SJIS character code,
2551 or a leading-code of Emacs. */
2552 mask
= (detect_coding_sjis (src
, src_end
)
2553 | detect_coding_emacs_mule (src
, src_end
));
2556 /* C is a character of ISO2022 in graphic plane right,
2557 or a SJIS's 1-byte character code (i.e. JISX0201),
2558 or the first byte of BIG5's 2-byte code. */
2559 mask
= (detect_coding_iso2022 (src
, src_end
)
2560 | detect_coding_sjis (src
, src_end
)
2561 | detect_coding_big5 (src
, src_end
));
2566 /* Detect how a text of length SRC_BYTES pointed by SRC is encoded.
2567 The information of the detected coding system is set in CODING. */
2570 detect_coding (coding
, src
, src_bytes
)
2571 struct coding_system
*coding
;
2575 int mask
= detect_coding_mask (src
, src_bytes
);
2578 if (mask
== CODING_CATEGORY_MASK_ANY
)
2579 /* We found nothing other than ASCII. There's nothing to do. */
2583 /* The source text seems to be encoded in unknown coding system.
2584 Emacs regards the category of such a kind of coding system as
2585 `coding-category-binary'. We assume that a user has assigned
2586 an appropriate coding system for a `coding-category-binary'. */
2587 idx
= CODING_CATEGORY_IDX_BINARY
;
2590 /* We found some plausible coding systems. Let's use a coding
2591 system of the highest priority. */
2592 Lisp_Object val
= Vcoding_category_list
;
2597 idx
= XFASTINT (Fget (XCONS (val
)->car
, Qcoding_category_index
));
2598 if ((idx
< CODING_CATEGORY_IDX_MAX
) && (mask
& (1 << idx
)))
2600 val
= XCONS (val
)->cdr
;
2607 /* For unknown reason, `Vcoding_category_list' contains none
2608 of found categories. Let's use any of them. */
2609 for (idx
= 0; idx
< CODING_CATEGORY_IDX_MAX
; idx
++)
2610 if (mask
& (1 << idx
))
2614 setup_coding_system (XSYMBOL (coding_category_table
[idx
])->value
, coding
);
2617 /* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC
2618 is encoded. Return one of CODING_EOL_LF, CODING_EOL_CRLF,
2619 CODING_EOL_CR, and CODING_EOL_UNDECIDED. */
2622 detect_eol_type (src
, src_bytes
)
2626 unsigned char *src_end
= src
+ src_bytes
;
2629 while (src
< src_end
)
2633 return CODING_EOL_LF
;
2636 if (src
< src_end
&& *src
== '\n')
2637 return CODING_EOL_CRLF
;
2639 return CODING_EOL_CR
;
2642 return CODING_EOL_UNDECIDED
;
2645 /* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC
2646 is encoded. If it detects an appropriate format of end-of-line, it
2647 sets the information in *CODING. */
2650 detect_eol (coding
, src
, src_bytes
)
2651 struct coding_system
*coding
;
2656 int eol_type
= detect_eol_type (src
, src_bytes
);
2658 if (eol_type
== CODING_EOL_UNDECIDED
)
2659 /* We found no end-of-line in the source text. */
2662 val
= Fget (coding
->symbol
, Qeol_type
);
2663 if (VECTORP (val
) && XVECTOR (val
)->size
== 3)
2664 setup_coding_system (XVECTOR (val
)->contents
[eol_type
], coding
);
2667 /* See "GENERAL NOTES about `decode_coding_XXX ()' functions". Before
2668 decoding, it may detect coding system and format of end-of-line if
2669 those are not yet decided. */
2672 decode_coding (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
2673 struct coding_system
*coding
;
2674 unsigned char *source
, *destination
;
2675 int src_bytes
, dst_bytes
;
2686 if (coding
->type
== coding_type_undecided
)
2687 detect_coding (coding
, source
, src_bytes
);
2689 if (coding
->eol_type
== CODING_EOL_UNDECIDED
)
2690 detect_eol (coding
, source
, src_bytes
);
2692 coding
->carryover_size
= 0;
2693 switch (coding
->type
)
2695 case coding_type_no_conversion
:
2696 label_no_conversion
:
2697 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
2698 bcopy (source
, destination
, produced
);
2699 *consumed
= produced
;
2702 case coding_type_emacs_mule
:
2703 case coding_type_undecided
:
2704 if (coding
->eol_type
== CODING_EOL_LF
2705 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
2706 goto label_no_conversion
;
2707 produced
= decode_eol (coding
, source
, destination
,
2708 src_bytes
, dst_bytes
, consumed
);
2711 case coding_type_sjis
:
2712 produced
= decode_coding_sjis_big5 (coding
, source
, destination
,
2713 src_bytes
, dst_bytes
, consumed
,
2717 case coding_type_iso2022
:
2718 produced
= decode_coding_iso2022 (coding
, source
, destination
,
2719 src_bytes
, dst_bytes
, consumed
);
2722 case coding_type_big5
:
2723 produced
= decode_coding_sjis_big5 (coding
, source
, destination
,
2724 src_bytes
, dst_bytes
, consumed
,
2728 case coding_type_ccl
:
2729 produced
= ccl_driver (&coding
->spec
.ccl
.decoder
, source
, destination
,
2730 src_bytes
, dst_bytes
, consumed
);
2737 /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". */
2740 encode_coding (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
2741 struct coding_system
*coding
;
2742 unsigned char *source
, *destination
;
2743 int src_bytes
, dst_bytes
;
2748 coding
->carryover_size
= 0;
2749 switch (coding
->type
)
2751 case coding_type_no_conversion
:
2752 label_no_conversion
:
2753 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
2756 bcopy (source
, destination
, produced
);
2757 if (coding
->selective
)
2759 unsigned char *p
= destination
, *pend
= destination
+ produced
;
2761 if (*p
++ == '\015') p
[-1] = '\n';
2764 *consumed
= produced
;
2767 case coding_type_emacs_mule
:
2768 case coding_type_undecided
:
2769 if (coding
->eol_type
== CODING_EOL_LF
2770 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
2771 goto label_no_conversion
;
2772 produced
= encode_eol (coding
, source
, destination
,
2773 src_bytes
, dst_bytes
, consumed
);
2776 case coding_type_sjis
:
2777 produced
= encode_coding_sjis_big5 (coding
, source
, destination
,
2778 src_bytes
, dst_bytes
, consumed
,
2782 case coding_type_iso2022
:
2783 produced
= encode_coding_iso2022 (coding
, source
, destination
,
2784 src_bytes
, dst_bytes
, consumed
);
2787 case coding_type_big5
:
2788 produced
= encode_coding_sjis_big5 (coding
, source
, destination
,
2789 src_bytes
, dst_bytes
, consumed
,
2793 case coding_type_ccl
:
2794 produced
= ccl_driver (&coding
->spec
.ccl
.encoder
, source
, destination
,
2795 src_bytes
, dst_bytes
, consumed
);
2802 #define CONVERSION_BUFFER_EXTRA_ROOM 256
2804 /* Return maximum size (bytes) of a buffer enough for decoding
2805 SRC_BYTES of text encoded in CODING. */
2808 decoding_buffer_size (coding
, src_bytes
)
2809 struct coding_system
*coding
;
2814 if (coding
->type
== coding_type_iso2022
)
2816 else if (coding
->type
== coding_type_ccl
)
2817 magnification
= coding
->spec
.ccl
.decoder
.buf_magnification
;
2821 return (src_bytes
* magnification
+ CONVERSION_BUFFER_EXTRA_ROOM
);
2824 /* Return maximum size (bytes) of a buffer enough for encoding
2825 SRC_BYTES of text to CODING. */
2828 encoding_buffer_size (coding
, src_bytes
)
2829 struct coding_system
*coding
;
2834 if (coding
->type
== coding_type_ccl
)
2835 magnification
= coding
->spec
.ccl
.encoder
.buf_magnification
;
2839 return (src_bytes
* magnification
+ CONVERSION_BUFFER_EXTRA_ROOM
);
2842 #ifndef MINIMUM_CONVERSION_BUFFER_SIZE
2843 #define MINIMUM_CONVERSION_BUFFER_SIZE 1024
2846 char *conversion_buffer
;
2847 int conversion_buffer_size
;
2849 /* Return a pointer to a SIZE bytes of buffer to be used for encoding
2850 or decoding. Sufficient memory is allocated automatically. If we
2851 run out of memory, return NULL. */
2854 get_conversion_buffer (size
)
2857 if (size
> conversion_buffer_size
)
2860 int real_size
= conversion_buffer_size
* 2;
2862 while (real_size
< size
) real_size
*= 2;
2863 buf
= (char *) xmalloc (real_size
);
2864 xfree (conversion_buffer
);
2865 conversion_buffer
= buf
;
2866 conversion_buffer_size
= real_size
;
2868 return conversion_buffer
;
2873 /*** 7. Emacs Lisp library functions ***/
2875 DEFUN ("coding-system-spec", Fcoding_system_spec
, Scoding_system_spec
,
2877 "Return coding-spec of CODING-SYSTEM.\n\
2878 If CODING-SYSTEM is not a valid coding-system, return nil.")
2882 while (SYMBOLP (obj
) && !NILP (obj
))
2883 obj
= Fget (obj
, Qcoding_system
);
2884 return ((NILP (obj
) || !VECTORP (obj
) || XVECTOR (obj
)->size
!= 5)
2888 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
2889 "Return t if OBJECT is nil or a coding-system.\n\
2890 See document of make-coding-system for coding-system object.")
2894 return ((NILP (obj
) || !NILP (Fcoding_system_spec (obj
))) ? Qt
: Qnil
);
2897 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
2898 Sread_non_nil_coding_system
, 1, 1, 0,
2899 "Read a coding system from the minibuffer, prompting with string PROMPT.")
2906 val
= Fcompleting_read (prompt
, Vobarray
, Qcoding_system_spec
,
2907 Qt
, Qnil
, Qnil
, Qnil
);
2909 while (XSTRING (val
)->size
== 0);
2910 return (Fintern (val
, Qnil
));
2913 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 1, 0,
2914 "Read a coding system or nil from the minibuffer, prompting with string PROMPT.")
2918 Lisp_Object val
= Fcompleting_read (prompt
, Vobarray
, Qcoding_system_p
,
2919 Qt
, Qnil
, Qnil
, Qnil
);
2920 return (XSTRING (val
)->size
== 0 ? Qnil
: Fintern (val
, Qnil
));
2923 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
2925 "Check validity of CODING-SYSTEM.\n\
2926 If valid, return CODING-SYSTEM, else `coding-system-error' is signaled.\n\
2927 CODING-SYSTEM is valid if it is a symbol and has \"coding-system\" property.\n\
2928 The value of property should be a vector of length 5.")
2930 Lisp_Object coding_system
;
2932 CHECK_SYMBOL (coding_system
, 0);
2933 if (!NILP (Fcoding_system_p (coding_system
)))
2934 return coding_system
;
2936 Fsignal (Qcoding_system_error
, Fcons (coding_system
, Qnil
));
2939 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
2941 "Detect coding-system of the text in the region between START and END.\n\
2942 Return a list of possible coding-systems ordered by priority.\n\
2943 If only ASCII characters are found, it returns `undecided'\n\
2944 or its subsidiary coding-system according to a detected end-of-line format.")
2948 int coding_mask
, eol_type
;
2952 validate_region (&b
, &e
);
2953 beg
= XINT (b
), end
= XINT (e
);
2954 if (beg
< GPT
&& end
>= GPT
) move_gap (end
);
2956 coding_mask
= detect_coding_mask (POS_ADDR (beg
), end
- beg
);
2957 eol_type
= detect_eol_type (POS_ADDR (beg
), end
- beg
);
2959 if (coding_mask
== CODING_CATEGORY_MASK_ANY
)
2961 val
= intern ("undecided");
2962 if (eol_type
!= CODING_EOL_UNDECIDED
)
2964 Lisp_Object val2
= Fget (val
, Qeol_type
);
2966 val
= XVECTOR (val2
)->contents
[eol_type
];
2973 /* At first, gather possible coding-systems in VAL in a reverse
2976 for (val2
= Vcoding_category_list
;
2978 val2
= XCONS (val2
)->cdr
)
2981 = XFASTINT (Fget (XCONS (val2
)->car
, Qcoding_category_index
));
2982 if (coding_mask
& (1 << idx
))
2983 val
= Fcons (Fsymbol_value (XCONS (val2
)->car
), val
);
2986 /* Then, change the order of the list, while getting subsidiary
2990 for (; !NILP (val2
); val2
= XCONS (val2
)->cdr
)
2992 if (eol_type
== CODING_EOL_UNDECIDED
)
2993 val
= Fcons (XCONS (val2
)->car
, val
);
2996 Lisp_Object val3
= Fget (XCONS (val2
)->car
, Qeol_type
);
2998 val
= Fcons (XVECTOR (val3
)->contents
[eol_type
], val
);
3000 val
= Fcons (XCONS (val2
)->car
, val
);
3008 /* Scan text in the region between *BEGP and *ENDP, skip characters
3009 which we never have to encode to (iff ENCODEP is 1) or decode from
3010 coding system CODING at the head and tail, then set BEGP and ENDP
3011 to the addresses of start and end of the text we actually convert. */
3014 shrink_conversion_area (begp
, endp
, coding
, encodep
)
3015 unsigned char **begp
, **endp
;
3016 struct coding_system
*coding
;
3019 register unsigned char *beg_addr
= *begp
, *end_addr
= *endp
;
3021 if (coding
->eol_type
!= CODING_EOL_LF
3022 && coding
->eol_type
!= CODING_EOL_UNDECIDED
)
3023 /* Since we anyway have to convert end-of-line format, it is not
3024 worth skipping at most 100 bytes or so. */
3027 if (encodep
) /* for encoding */
3029 switch (coding
->type
)
3031 case coding_type_no_conversion
:
3032 case coding_type_emacs_mule
:
3033 case coding_type_undecided
:
3034 /* We need no conversion. */
3037 case coding_type_ccl
:
3038 /* We can't skip any data. */
3040 case coding_type_iso2022
:
3041 if (coding
->flags
& CODING_FLAG_ISO_DESIGNATE_AT_BOL
)
3043 unsigned char *bol
= beg_addr
;
3044 while (beg_addr
< end_addr
&& *beg_addr
< 0x80)
3047 if (*(beg_addr
- 1) == '\n')
3051 goto label_skip_tail
;
3055 /* We can skip all ASCII characters at the head and tail. */
3056 while (beg_addr
< end_addr
&& *beg_addr
< 0x80) beg_addr
++;
3058 while (beg_addr
< end_addr
&& *(end_addr
- 1) < 0x80) end_addr
--;
3062 else /* for decoding */
3064 switch (coding
->type
)
3066 case coding_type_no_conversion
:
3067 /* We need no conversion. */
3070 case coding_type_emacs_mule
:
3071 if (coding
->eol_type
== CODING_EOL_LF
)
3073 /* We need no conversion. */
3077 /* We can skip all but carriage-return. */
3078 while (beg_addr
< end_addr
&& *beg_addr
!= '\r') beg_addr
++;
3079 while (beg_addr
< end_addr
&& *(end_addr
- 1) != '\r') end_addr
--;
3081 case coding_type_sjis
:
3082 case coding_type_big5
:
3083 /* We can skip all ASCII characters at the head. */
3084 while (beg_addr
< end_addr
&& *beg_addr
< 0x80) beg_addr
++;
3085 /* We can skip all ASCII characters at the tail except for
3086 the second byte of SJIS or BIG5 code. */
3087 while (beg_addr
< end_addr
&& *(end_addr
- 1) < 0x80) end_addr
--;
3088 if (end_addr
!= *endp
)
3091 case coding_type_ccl
:
3092 /* We can't skip any data. */
3094 default: /* i.e. case coding_type_iso2022: */
3098 /* We can skip all ASCII characters except for a few
3099 control codes at the head. */
3100 while (beg_addr
< end_addr
&& (c
= *beg_addr
) < 0x80
3101 && c
!= ISO_CODE_CR
&& c
!= ISO_CODE_SO
3102 && c
!= ISO_CODE_SI
&& c
!= ISO_CODE_ESC
)
3113 /* Encode to (iff ENCODEP is 1) or decode form coding system CODING a
3114 text between B and E. B and E are buffer position. */
3117 code_convert_region (b
, e
, coding
, encodep
)
3119 struct coding_system
*coding
;
3122 int beg
, end
, len
, consumed
, produced
;
3124 unsigned char *begp
, *endp
;
3127 validate_region (&b
, &e
);
3128 beg
= XINT (b
), end
= XINT (e
);
3129 if (beg
< GPT
&& end
>= GPT
)
3132 if (encodep
&& !NILP (coding
->pre_write_conversion
))
3134 /* We must call a pre-conversion function which may put a new
3135 text to be converted in a new buffer. */
3136 struct buffer
*old
= current_buffer
, *new;
3139 call2 (coding
->pre_write_conversion
, b
, e
);
3140 if (old
!= current_buffer
)
3142 /* Replace the original text by the text just generated. */
3144 new = current_buffer
;
3145 set_buffer_internal (old
);
3146 del_range (beg
, end
);
3147 insert_from_buffer (new, 1, len
, 0);
3152 /* We may be able to shrink the conversion region. */
3153 begp
= POS_ADDR (beg
); endp
= begp
+ (end
- beg
);
3154 shrink_conversion_area (&begp
, &endp
, coding
, encodep
);
3157 /* We need no conversion. */
3161 beg
+= begp
- POS_ADDR (beg
);
3162 end
= beg
+ (endp
- begp
);
3165 len
= encoding_buffer_size (coding
, end
- beg
);
3167 len
= decoding_buffer_size (coding
, end
- beg
);
3168 buf
= get_conversion_buffer (len
);
3170 coding
->last_block
= 1;
3172 ? encode_coding (coding
, POS_ADDR (beg
), buf
, end
- beg
, len
,
3174 : decode_coding (coding
, POS_ADDR (beg
), buf
, end
- beg
, len
,
3177 len
= produced
+ (beg
- XINT (b
)) + (XINT (e
) - end
);
3180 insert (buf
, produced
);
3181 del_range (PT
, PT
+ end
- beg
);
3183 pos
= PT
+ (pos
- end
);
3189 if (!encodep
&& !NILP (coding
->post_read_conversion
))
3191 /* We must call a post-conversion function which may alter
3192 the text just converted. */
3197 insval
= call1 (coding
->post_read_conversion
, make_number (len
));
3198 CHECK_NUMBER (insval
, 0);
3199 len
= XINT (insval
);
3202 return make_number (len
);
3206 code_convert_string (str
, coding
, encodep
, nocopy
)
3207 Lisp_Object str
, nocopy
;
3208 struct coding_system
*coding
;
3211 int len
, consumed
, produced
;
3213 unsigned char *begp
, *endp
;
3214 int head_skip
, tail_skip
;
3215 struct gcpro gcpro1
;
3217 if (encodep
&& !NILP (coding
->pre_write_conversion
)
3218 || !encodep
&& !NILP (coding
->post_read_conversion
))
3220 /* Since we have to call Lisp functions which assume target text
3221 is in a buffer, after setting a temporary buffer, call
3222 code_convert_region. */
3223 int count
= specpdl_ptr
- specpdl
;
3224 int len
= XSTRING (str
)->size
;
3226 struct buffer
*old
= current_buffer
;
3228 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
3229 temp_output_buffer_setup (" *code-converting-work*");
3230 set_buffer_internal (XBUFFER (Vstandard_output
));
3231 insert_from_string (str
, 0, len
, 0);
3232 code_convert_region (make_number (BEGV
), make_number (ZV
),
3234 result
= make_buffer_string (BEGV
, ZV
, 0);
3235 set_buffer_internal (old
);
3236 return unbind_to (count
, result
);
3239 /* We may be able to shrink the conversion region. */
3240 begp
= XSTRING (str
)->data
;
3241 endp
= begp
+ XSTRING (str
)->size
;
3242 shrink_conversion_area (&begp
, &endp
, coding
, encodep
);
3245 /* We need no conversion. */
3246 return (NILP (nocopy
) ? Fcopy_sequence (str
) : str
);
3248 head_skip
= begp
- XSTRING (str
)->data
;
3249 tail_skip
= XSTRING (str
)->size
- head_skip
- (endp
- begp
);
3254 len
= encoding_buffer_size (coding
, endp
- begp
);
3256 len
= decoding_buffer_size (coding
, endp
- begp
);
3257 buf
= get_conversion_buffer (len
+ head_skip
+ tail_skip
);
3259 bcopy (XSTRING (str
)->data
, buf
, head_skip
);
3260 coding
->last_block
= 1;
3262 ? encode_coding (coding
, XSTRING (str
)->data
+ head_skip
,
3263 buf
+ head_skip
, endp
- begp
, len
, &consumed
)
3264 : decode_coding (coding
, XSTRING (str
)->data
+ head_skip
,
3265 buf
+ head_skip
, endp
- begp
, len
, &consumed
));
3266 bcopy (XSTRING (str
)->data
+ head_skip
+ (endp
- begp
),
3267 buf
+ head_skip
+ produced
,
3272 return make_string (buf
, head_skip
+ produced
+ tail_skip
);
3275 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
3276 3, 3, "r\nzCoding system: ",
3277 "Decode current region by specified coding system.\n\
3278 When called from a program, takes three arguments:\n\
3279 START, END, and CODING-SYSTEM. START END are buffer positions.\n\
3280 Return length of decoded text.")
3281 (b
, e
, coding_system
)
3282 Lisp_Object b
, e
, coding_system
;
3284 struct coding_system coding
;
3286 CHECK_NUMBER_COERCE_MARKER (b
, 0);
3287 CHECK_NUMBER_COERCE_MARKER (e
, 1);
3288 CHECK_SYMBOL (coding_system
, 2);
3290 if (NILP (coding_system
))
3291 return make_number (XFASTINT (e
) - XFASTINT (b
));
3292 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
3293 error ("Invalid coding-system: %s", XSYMBOL (coding_system
)->name
->data
);
3295 return code_convert_region (b
, e
, &coding
, 0);
3298 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
3299 3, 3, "r\nzCoding system: ",
3300 "Encode current region by specified coding system.\n\
3301 When called from a program, takes three arguments:\n\
3302 START, END, and CODING-SYSTEM. START END are buffer positions.\n\
3303 Return length of encoded text.")
3304 (b
, e
, coding_system
)
3305 Lisp_Object b
, e
, coding_system
;
3307 struct coding_system coding
;
3309 CHECK_NUMBER_COERCE_MARKER (b
, 0);
3310 CHECK_NUMBER_COERCE_MARKER (e
, 1);
3311 CHECK_SYMBOL (coding_system
, 2);
3313 if (NILP (coding_system
))
3314 return make_number (XFASTINT (e
) - XFASTINT (b
));
3315 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
3316 error ("Invalid coding-system: %s", XSYMBOL (coding_system
)->name
->data
);
3318 return code_convert_region (b
, e
, &coding
, 1);
3321 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
3323 "Decode STRING which is encoded in CODING-SYSTEM, and return the result.\n\
3324 Optional arg NOCOPY non-nil means return STRING itself if there's no need\n\
3326 (string
, coding_system
, nocopy
)
3327 Lisp_Object string
, coding_system
, nocopy
;
3329 struct coding_system coding
;
3331 CHECK_STRING (string
, 0);
3332 CHECK_SYMBOL (coding_system
, 1);
3334 if (NILP (coding_system
))
3335 return (NILP (nocopy
) ? Fcopy_sequence (string
) : string
);
3336 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
3337 error ("Invalid coding-system: %s", XSYMBOL (coding_system
)->name
->data
);
3339 return code_convert_string (string
, &coding
, 0, nocopy
);
3342 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
3344 "Encode STRING to CODING-SYSTEM, and return the result.\n\
3345 Optional arg NOCOPY non-nil means return STRING itself if there's no need\n\
3347 (string
, coding_system
, nocopy
)
3348 Lisp_Object string
, coding_system
, nocopy
;
3350 struct coding_system coding
;
3352 CHECK_STRING (string
, 0);
3353 CHECK_SYMBOL (coding_system
, 1);
3355 if (NILP (coding_system
))
3356 return (NILP (nocopy
) ? Fcopy_sequence (string
) : string
);
3357 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
3358 error ("Invalid coding-system: %s", XSYMBOL (coding_system
)->name
->data
);
3360 return code_convert_string (string
, &coding
, 1, nocopy
);
3363 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
3364 "Decode a JISX0208 character of shift-jis encoding.\n\
3365 CODE is the character code in SJIS.\n\
3366 Return the corresponding character.")
3370 unsigned char c1
, c2
, s1
, s2
;
3373 CHECK_NUMBER (code
, 0);
3374 s1
= (XFASTINT (code
)) >> 8, s2
= (XFASTINT (code
)) & 0xFF;
3375 DECODE_SJIS (s1
, s2
, c1
, c2
);
3376 XSETFASTINT (val
, MAKE_NON_ASCII_CHAR (charset_jisx0208
, c1
, c2
));
3380 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
3381 "Encode a JISX0208 character CHAR to SJIS coding-system.\n\
3382 Return the corresponding character code in SJIS.")
3386 int charset
, c1
, c2
, s1
, s2
;
3389 CHECK_NUMBER (ch
, 0);
3390 SPLIT_CHAR (XFASTINT (ch
), charset
, c1
, c2
);
3391 if (charset
== charset_jisx0208
)
3393 ENCODE_SJIS (c1
, c2
, s1
, s2
);
3394 XSETFASTINT (val
, (s1
<< 8) | s2
);
3397 XSETFASTINT (val
, 0);
3401 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
3402 "Decode a Big5 character CODE of BIG5 coding-system.\n\
3403 CODE is the character code in BIG5.\n\
3404 Return the corresponding character.")
3409 unsigned char b1
, b2
, c1
, c2
;
3412 CHECK_NUMBER (code
, 0);
3413 b1
= (XFASTINT (code
)) >> 8, b2
= (XFASTINT (code
)) & 0xFF;
3414 DECODE_BIG5 (b1
, b2
, charset
, c1
, c2
);
3415 XSETFASTINT (val
, MAKE_NON_ASCII_CHAR (charset
, c1
, c2
));
3419 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
3420 "Encode the Big5 character CHAR to BIG5 coding-system.\n\
3421 Return the corresponding character code in Big5.")
3425 int charset
, c1
, c2
, b1
, b2
;
3428 CHECK_NUMBER (ch
, 0);
3429 SPLIT_CHAR (XFASTINT (ch
), charset
, c1
, c2
);
3430 if (charset
== charset_big5_1
|| charset
== charset_big5_2
)
3432 ENCODE_BIG5 (charset
, c1
, c2
, b1
, b2
);
3433 XSETFASTINT (val
, (b1
<< 8) | b2
);
3436 XSETFASTINT (val
, 0);
3440 DEFUN ("set-terminal-coding-system-internal",
3441 Fset_terminal_coding_system_internal
,
3442 Sset_terminal_coding_system_internal
, 1, 1, 0, "")
3444 Lisp_Object coding_system
;
3446 CHECK_SYMBOL (coding_system
, 0);
3447 setup_coding_system (Fcheck_coding_system (coding_system
), &terminal_coding
);
3451 DEFUN ("terminal-coding-system",
3452 Fterminal_coding_system
, Sterminal_coding_system
, 0, 0, 0,
3453 "Return coding-system of your terminal.")
3456 return terminal_coding
.symbol
;
3459 DEFUN ("set-keyboard-coding-system-internal",
3460 Fset_keyboard_coding_system_internal
,
3461 Sset_keyboard_coding_system_internal
, 1, 1, 0, "")
3463 Lisp_Object coding_system
;
3465 CHECK_SYMBOL (coding_system
, 0);
3466 setup_coding_system (Fcheck_coding_system (coding_system
), &keyboard_coding
);
3470 DEFUN ("keyboard-coding-system",
3471 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 0, 0,
3472 "Return coding-system of what is sent from terminal keyboard.")
3475 return keyboard_coding
.symbol
;
3479 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
3480 Sfind_operation_coding_system
, 1, MANY
, 0,
3481 "Choose a coding system for an operation based on the target name.\n\
3482 The value names a pair of coding systems: (DECODING-SYSTEM ENCODING-SYSTEM).\n\
3483 DECODING-SYSTEM is the coding system to use for decoding\n\
3484 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system\n\
3485 for encoding (in case OPERATION does encoding).\n\
3487 The first argument OPERATION specifies an I/O primitive:\n\
3488 For file I/O, `insert-file-contents' or `write-region'.\n\
3489 For process I/O, `call-process', `call-process-region', or `start-process'.\n\
3490 For network I/O, `open-network-stream'.\n\
3492 The remaining arguments should be the same arguments that were passed\n\
3493 to the primitive. Depending on which primitive, one of those arguments\n\
3494 is selected as the TARGET. For example, if OPERATION does file I/O,\n\
3495 whichever argument specifies the file name is TARGET.\n\
3497 TARGET has a meaning which depends on OPERATION:\n\
3498 For file I/O, TARGET is a file name.\n\
3499 For process I/O, TARGET is a process name.\n\
3500 For network I/O, TARGET is a service name or a port number\n\
3502 This function looks up what specified for TARGET in,\n\
3503 `file-coding-system-alist', `process-coding-system-alist',\n\
3504 or `network-coding-system-alist' depending on OPERATION.\n\
3505 They may specify a coding system, a cons of coding systems,\n\
3506 or a function symbol to call.\n\
3507 In the last case, we call the function with one argument,\n\
3508 which is a list of all the arguments given to this function.")
3513 Lisp_Object operation
, target_idx
, target
, val
;
3514 register Lisp_Object chain
;
3517 error ("Too few arguments");
3518 operation
= args
[0];
3519 if (!SYMBOLP (operation
)
3520 || !INTEGERP (target_idx
= Fget (operation
, Qtarget_idx
)))
3521 error ("Invalid first arguement");
3522 if (nargs
< 1 + XINT (target_idx
))
3523 error ("Too few arguments for operation: %s",
3524 XSYMBOL (operation
)->name
->data
);
3525 target
= args
[XINT (target_idx
) + 1];
3526 if (!(STRINGP (target
)
3527 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
3528 error ("Invalid %dth argument", XINT (target_idx
) + 1);
3530 chain
= ((EQ (operation
, Qinsert_file_contents
)
3531 || EQ (operation
, Qwrite_region
))
3532 ? Vfile_coding_system_alist
3533 : (EQ (operation
, Qopen_network_stream
)
3534 ? Vnetwork_coding_system_alist
3535 : Vprocess_coding_system_alist
));
3539 for (; CONSP (chain
); chain
= XCONS (chain
)->cdr
)
3541 Lisp_Object elt
= XCONS (chain
)->car
;
3544 && ((STRINGP (target
)
3545 && STRINGP (XCONS (elt
)->car
)
3546 && fast_string_match (XCONS (elt
)->car
, target
) >= 0)
3547 || (INTEGERP (target
) && EQ (target
, XCONS (elt
)->car
))))
3549 val
= XCONS (elt
)->cdr
;
3552 if (! SYMBOLP (val
))
3554 if (! NILP (Fcoding_system_p (val
)))
3555 return Fcons (val
, val
);
3556 if (!NILP (Fboundp (val
)))
3557 return call2 (val
, Flist (nargs
, args
));
3567 /*** 8. Post-amble ***/
3573 /* Emacs' internal format specific initialize routine. */
3574 for (i
= 0; i
<= 0x20; i
++)
3575 emacs_code_class
[i
] = EMACS_control_code
;
3576 emacs_code_class
[0x0A] = EMACS_linefeed_code
;
3577 emacs_code_class
[0x0D] = EMACS_carriage_return_code
;
3578 for (i
= 0x21 ; i
< 0x7F; i
++)
3579 emacs_code_class
[i
] = EMACS_ascii_code
;
3580 emacs_code_class
[0x7F] = EMACS_control_code
;
3581 emacs_code_class
[0x80] = EMACS_leading_code_composition
;
3582 for (i
= 0x81; i
< 0xFF; i
++)
3583 emacs_code_class
[i
] = EMACS_invalid_code
;
3584 emacs_code_class
[LEADING_CODE_PRIVATE_11
] = EMACS_leading_code_3
;
3585 emacs_code_class
[LEADING_CODE_PRIVATE_12
] = EMACS_leading_code_3
;
3586 emacs_code_class
[LEADING_CODE_PRIVATE_21
] = EMACS_leading_code_4
;
3587 emacs_code_class
[LEADING_CODE_PRIVATE_22
] = EMACS_leading_code_4
;
3589 /* ISO2022 specific initialize routine. */
3590 for (i
= 0; i
< 0x20; i
++)
3591 iso_code_class
[i
] = ISO_control_code
;
3592 for (i
= 0x21; i
< 0x7F; i
++)
3593 iso_code_class
[i
] = ISO_graphic_plane_0
;
3594 for (i
= 0x80; i
< 0xA0; i
++)
3595 iso_code_class
[i
] = ISO_control_code
;
3596 for (i
= 0xA1; i
< 0xFF; i
++)
3597 iso_code_class
[i
] = ISO_graphic_plane_1
;
3598 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
3599 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
3600 iso_code_class
[ISO_CODE_CR
] = ISO_carriage_return
;
3601 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
3602 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
3603 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
3604 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
3605 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
3606 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
3607 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
3609 conversion_buffer_size
= MINIMUM_CONVERSION_BUFFER_SIZE
;
3610 conversion_buffer
= (char *) xmalloc (MINIMUM_CONVERSION_BUFFER_SIZE
);
3612 setup_coding_system (Qnil
, &keyboard_coding
);
3613 setup_coding_system (Qnil
, &terminal_coding
);
3615 #if defined (MSDOS) || defined (WINDOWSNT)
3616 system_eol_type
= CODING_EOL_CRLF
;
3618 system_eol_type
= CODING_EOL_LF
;
3626 Qtarget_idx
= intern ("target-idx");
3627 staticpro (&Qtarget_idx
);
3629 /* Target FILENAME is the first argument. */
3630 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
3631 /* Target FILENAME is the third argument. */
3632 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
3634 Qcall_process
= intern ("call-process");
3635 staticpro (&Qcall_process
);
3636 /* Target PROGRAM is the first argument. */
3637 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
3639 Qcall_process_region
= intern ("call-process-region");
3640 staticpro (&Qcall_process_region
);
3641 /* Target PROGRAM is the third argument. */
3642 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
3644 Qstart_process
= intern ("start-process");
3645 staticpro (&Qstart_process
);
3646 /* Target PROGRAM is the third argument. */
3647 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
3649 Qopen_network_stream
= intern ("open-network-stream");
3650 staticpro (&Qopen_network_stream
);
3651 /* Target SERVICE is the fourth argument. */
3652 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
3654 Qcoding_system
= intern ("coding-system");
3655 staticpro (&Qcoding_system
);
3657 Qeol_type
= intern ("eol-type");
3658 staticpro (&Qeol_type
);
3660 Qbuffer_file_coding_system
= intern ("buffer-file-coding-system");
3661 staticpro (&Qbuffer_file_coding_system
);
3663 Qpost_read_conversion
= intern ("post-read-conversion");
3664 staticpro (&Qpost_read_conversion
);
3666 Qpre_write_conversion
= intern ("pre-write-conversion");
3667 staticpro (&Qpre_write_conversion
);
3669 Qcoding_system_spec
= intern ("coding-system-spec");
3670 staticpro (&Qcoding_system_spec
);
3672 Qcoding_system_p
= intern ("coding-system-p");
3673 staticpro (&Qcoding_system_p
);
3675 Qcoding_system_error
= intern ("coding-system-error");
3676 staticpro (&Qcoding_system_error
);
3678 Fput (Qcoding_system_error
, Qerror_conditions
,
3679 Fcons (Qcoding_system_error
, Fcons (Qerror
, Qnil
)));
3680 Fput (Qcoding_system_error
, Qerror_message
,
3681 build_string ("Invalid coding system"));
3683 Qcoding_category_index
= intern ("coding-category-index");
3684 staticpro (&Qcoding_category_index
);
3688 for (i
= 0; i
< CODING_CATEGORY_IDX_MAX
; i
++)
3690 coding_category_table
[i
] = intern (coding_category_name
[i
]);
3691 staticpro (&coding_category_table
[i
]);
3692 Fput (coding_category_table
[i
], Qcoding_category_index
,
3697 Qcharacter_unification_table
= intern ("character-unification-table");
3698 staticpro (&Qcharacter_unification_table
);
3699 Fput (Qcharacter_unification_table
, Qchar_table_extra_slots
,
3702 Qcharacter_unification_table_for_decode
3703 = intern ("character-unification-table-for-decode");
3704 staticpro (&Qcharacter_unification_table_for_decode
);
3706 Qcharacter_unification_table_for_encode
3707 = intern ("character-unification-table-for-encode");
3708 staticpro (&Qcharacter_unification_table_for_encode
);
3710 Qemacs_mule
= intern ("emacs-mule");
3711 staticpro (&Qemacs_mule
);
3713 defsubr (&Scoding_system_spec
);
3714 defsubr (&Scoding_system_p
);
3715 defsubr (&Sread_coding_system
);
3716 defsubr (&Sread_non_nil_coding_system
);
3717 defsubr (&Scheck_coding_system
);
3718 defsubr (&Sdetect_coding_region
);
3719 defsubr (&Sdecode_coding_region
);
3720 defsubr (&Sencode_coding_region
);
3721 defsubr (&Sdecode_coding_string
);
3722 defsubr (&Sencode_coding_string
);
3723 defsubr (&Sdecode_sjis_char
);
3724 defsubr (&Sencode_sjis_char
);
3725 defsubr (&Sdecode_big5_char
);
3726 defsubr (&Sencode_big5_char
);
3727 defsubr (&Sset_terminal_coding_system_internal
);
3728 defsubr (&Sterminal_coding_system
);
3729 defsubr (&Sset_keyboard_coding_system_internal
);
3730 defsubr (&Skeyboard_coding_system
);
3731 defsubr (&Sfind_operation_coding_system
);
3733 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list
,
3734 "List of coding-categories (symbols) ordered by priority.");
3738 Vcoding_category_list
= Qnil
;
3739 for (i
= CODING_CATEGORY_IDX_MAX
- 1; i
>= 0; i
--)
3740 Vcoding_category_list
3741 = Fcons (coding_category_table
[i
], Vcoding_category_list
);
3744 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read
,
3745 "A variable of internal use only.\n\
3746 If the value is a coding system, it is used for decoding on read operation.\n\
3747 If not, an appropriate element in `coding-system-alist' (which see) is used.");
3748 Vcoding_system_for_read
= Qnil
;
3750 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write
,
3751 "A variable of internal use only.\n\
3752 If the value is a coding system, it is used for encoding on write operation.\n\
3753 If not, an appropriate element in `coding-system-alist' (which see) is used.");
3754 Vcoding_system_for_write
= Qnil
;
3756 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used
,
3757 "Coding-system used in the latest file or process I/O.");
3758 Vlast_coding_system_used
= Qnil
;
3760 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion
,
3761 "*Non-nil inhibit code conversion of end-of-line format in any cases.");
3762 inhibit_eol_conversion
= 0;
3764 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist
,
3765 "Alist to decide a coding system to use for a file I/O operation.\n\
3766 The format is ((PATTERN . VAL) ...),\n\
3767 where PATTERN is a regular expression matching a file name,\n\
3768 VAL is a coding system, a cons of coding systems, or a function symbol.\n\
3769 If VAL is a coding system, it is used for both decoding and encoding\n\
3770 the file contents.\n\
3771 If VAL is a cons of coding systems, the car part is used for decoding,\n\
3772 and the cdr part is used for encoding.\n\
3773 If VAL is a function symbol, the function must return a coding system\n\
3774 or a cons of coding systems which are used as above.\n\
3776 See also the function `find-operation-coding-system'.");
3777 Vfile_coding_system_alist
= Qnil
;
3779 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist
,
3780 "Alist to decide a coding system to use for a process I/O operation.\n\
3781 The format is ((PATTERN . VAL) ...),\n\
3782 where PATTERN is a regular expression matching a program name,\n\
3783 VAL is a coding system, a cons of coding systems, or a function symbol.\n\
3784 If VAL is a coding system, it is used for both decoding what received\n\
3785 from the program and encoding what sent to the program.\n\
3786 If VAL is a cons of coding systems, the car part is used for decoding,\n\
3787 and the cdr part is used for encoding.\n\
3788 If VAL is a function symbol, the function must return a coding system\n\
3789 or a cons of coding systems which are used as above.\n\
3791 See also the function `find-operation-coding-system'.");
3792 Vprocess_coding_system_alist
= Qnil
;
3794 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist
,
3795 "Alist to decide a coding system to use for a network I/O operation.\n\
3796 The format is ((PATTERN . VAL) ...),\n\
3797 where PATTERN is a regular expression matching a network service name\n\
3798 or is a port number to connect to,\n\
3799 VAL is a coding system, a cons of coding systems, or a function symbol.\n\
3800 If VAL is a coding system, it is used for both decoding what received\n\
3801 from the network stream and encoding what sent to the network stream.\n\
3802 If VAL is a cons of coding systems, the car part is used for decoding,\n\
3803 and the cdr part is used for encoding.\n\
3804 If VAL is a function symbol, the function must return a coding system\n\
3805 or a cons of coding systems which are used as above.\n\
3807 See also the function `find-operation-coding-system'.");
3808 Vnetwork_coding_system_alist
= Qnil
;
3810 DEFVAR_INT ("eol-mnemonic-unix", &eol_mnemonic_unix
,
3811 "Mnemonic character indicating UNIX-like end-of-line format (i.e. LF) .");
3812 eol_mnemonic_unix
= ':';
3814 DEFVAR_INT ("eol-mnemonic-dos", &eol_mnemonic_dos
,
3815 "Mnemonic character indicating DOS-like end-of-line format (i.e. CRLF).");
3816 eol_mnemonic_dos
= '\\';
3818 DEFVAR_INT ("eol-mnemonic-mac", &eol_mnemonic_mac
,
3819 "Mnemonic character indicating MAC-like end-of-line format (i.e. CR).");
3820 eol_mnemonic_mac
= '/';
3822 DEFVAR_INT ("eol-mnemonic-undecided", &eol_mnemonic_undecided
,
3823 "Mnemonic character indicating end-of-line format is not yet decided.");
3824 eol_mnemonic_undecided
= ':';
3826 DEFVAR_LISP ("enable-character-unification", &Venable_character_unification
,
3827 "Non-nil means ISO 2022 encoder/decoder do character unification.");
3828 Venable_character_unification
= Qt
;
3830 DEFVAR_LISP ("standard-character-unification-table-for-decode",
3831 &Vstandard_character_unification_table_for_decode
,
3832 "Table for unifying characters when reading.");
3833 Vstandard_character_unification_table_for_decode
= Qnil
;
3835 DEFVAR_LISP ("standard-character-unification-table-for-encode",
3836 &Vstandard_character_unification_table_for_encode
,
3837 "Table for unifying characters when writing.");
3838 Vstandard_character_unification_table_for_encode
= Qnil
;
3840 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_alist
,
3841 "Alist of charsets vs revision numbers.\n\
3842 While encoding, if a charset (car part of an element) is found,\n\
3843 designate it with the escape sequence identifing revision (cdr part of the element).");
3844 Vcharset_revision_alist
= Qnil
;
3846 DEFVAR_LISP ("default-process-coding-system",
3847 &Vdefault_process_coding_system
,
3848 "Cons of coding systems used for process I/O by default.\n\
3849 The car part is used for decoding a process output,\n\
3850 the cdr part is used for encoding a text to be sent to a process.");
3851 Vdefault_process_coding_system
= Qnil
;