1 /* Coding system handler (conversion, detection, and etc).
2 Copyright (C) 1995, 1997 Electrotechnical Laboratory, JAPAN.
3 Licensed to the Free Software Foundation.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /*** TABLE OF CONTENTS ***
25 2. Emacs' internal format (emacs-mule) handlers
27 4. Shift-JIS and BIG5 handlers
28 5. End-of-line handlers
29 6. C library functions
30 7. Emacs Lisp library functions
35 /*** GENERAL NOTE on CODING SYSTEM ***
37 Coding system is an encoding mechanism of one or more character
38 sets. Here's a list of coding systems which Emacs can handle. When
39 we say "decode", it means converting some other coding system to
40 Emacs' internal format (emacs-internal), and when we say "encode",
41 it means converting the coding system emacs-mule to some other
44 0. Emacs' internal format (emacs-mule)
46 Emacs itself holds a multi-lingual character in a buffer and a string
47 in a special format. Details are described in the section 2.
51 The most famous coding system for multiple character sets. X's
52 Compound Text, various EUCs (Extended Unix Code), and such coding
53 systems used in Internet communication as ISO-2022-JP are all
54 variants of ISO2022. Details are described in the section 3.
56 2. SJIS (or Shift-JIS or MS-Kanji-Code)
58 A coding system to encode character sets: ASCII, JISX0201, and
59 JISX0208. Widely used for PC's in Japan. Details are described in
64 A coding system to encode character sets: ASCII and Big5. Widely
65 used by Chinese (mainly in Taiwan and Hong Kong). Details are
66 described in the section 4. In this file, when written as "BIG5"
67 (all uppercase), it means the coding system, and when written as
68 "Big5" (capitalized), it means the character set.
72 If a user want to read/write a text encoded in a coding system not
73 listed above, he can supply a decoder and an encoder for it in CCL
74 (Code Conversion Language) programs. Emacs executes the CCL program
75 while reading/writing.
77 Emacs represent a coding-system by a Lisp symbol that has a property
78 `coding-system'. But, before actually using the coding-system, the
79 information about it is set in a structure of type `struct
80 coding_system' for rapid processing. See the section 6 for more
85 /*** GENERAL NOTES on END-OF-LINE FORMAT ***
87 How end-of-line of a text is encoded depends on a system. For
88 instance, Unix's format is just one byte of `line-feed' code,
89 whereas DOS's format is two bytes sequence of `carriage-return' and
90 `line-feed' codes. MacOS's format is one byte of `carriage-return'.
92 Since how characters in a text is encoded and how end-of-line is
93 encoded is independent, any coding system described above can take
94 any format of end-of-line. So, Emacs has information of format of
95 end-of-line in each coding-system. See the section 6 for more
100 /*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
102 These functions check if a text between SRC and SRC_END is encoded
103 in the coding system category XXX. Each returns an integer value in
104 which appropriate flag bits for the category XXX is set. The flag
105 bits are defined in macros CODING_CATEGORY_MASK_XXX. Below is the
106 template of these functions. */
109 detect_coding_emacs_mule (src
, src_end
)
110 unsigned char *src
, *src_end
;
116 /*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
118 These functions decode SRC_BYTES length text at SOURCE encoded in
119 CODING to Emacs' internal format (emacs-mule). The resulting text
120 goes to a place pointed by DESTINATION, the length of which should
121 not exceed DST_BYTES. The bytes actually processed is returned as
122 *CONSUMED. The return value is the length of the decoded text.
123 Below is a template of these functions. */
125 decode_coding_XXX (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
126 struct coding_system
*coding
;
127 unsigned char *source
, *destination
;
128 int src_bytes
, dst_bytes
;
135 /*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
137 These functions encode SRC_BYTES length text at SOURCE of Emacs'
138 internal format (emacs-mule) to CODING. The resulting text goes to
139 a place pointed by DESTINATION, the length of which should not
140 exceed DST_BYTES. The bytes actually processed is returned as
141 *CONSUMED. The return value is the length of the encoded text.
142 Below is a template of these functions. */
144 encode_coding_XXX (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
145 struct coding_system
*coding
;
146 unsigned char *source
, *destination
;
147 int src_bytes
, dst_bytes
;
154 /*** COMMONLY USED MACROS ***/
156 /* The following three macros ONE_MORE_BYTE, TWO_MORE_BYTES, and
157 THREE_MORE_BYTES safely get one, two, and three bytes from the
158 source text respectively. If there are not enough bytes in the
159 source, they jump to `label_end_of_loop'. The caller should set
160 variables `src' and `src_end' to appropriate areas in advance. */
162 #define ONE_MORE_BYTE(c1) \
167 goto label_end_of_loop; \
170 #define TWO_MORE_BYTES(c1, c2) \
172 if (src + 1 < src_end) \
173 c1 = *src++, c2 = *src++; \
175 goto label_end_of_loop; \
178 #define THREE_MORE_BYTES(c1, c2, c3) \
180 if (src + 2 < src_end) \
181 c1 = *src++, c2 = *src++, c3 = *src++; \
183 goto label_end_of_loop; \
186 /* The following three macros DECODE_CHARACTER_ASCII,
187 DECODE_CHARACTER_DIMENSION1, and DECODE_CHARACTER_DIMENSION2 put
188 the multi-byte form of a character of each class at the place
189 pointed by `dst'. The caller should set the variable `dst' to
190 point to an appropriate area and the variable `coding' to point to
191 the coding-system of the currently decoding text in advance. */
193 /* Decode one ASCII character C. */
195 #define DECODE_CHARACTER_ASCII(c) \
197 if (COMPOSING_P (coding->composing)) \
198 *dst++ = 0xA0, *dst++ = (c) | 0x80; \
203 /* Decode one DIMENSION1 character of which charset is CHARSET and
204 position-code is C. */
206 #define DECODE_CHARACTER_DIMENSION1(charset, c) \
208 unsigned char leading_code = CHARSET_LEADING_CODE_BASE (charset); \
209 if (COMPOSING_P (coding->composing)) \
210 *dst++ = leading_code + 0x20; \
212 *dst++ = leading_code; \
213 if (leading_code = CHARSET_LEADING_CODE_EXT (charset)) \
214 *dst++ = leading_code; \
215 *dst++ = (c) | 0x80; \
218 /* Decode one DIMENSION2 character of which charset is CHARSET and
219 position-codes are C1 and C2. */
221 #define DECODE_CHARACTER_DIMENSION2(charset, c1, c2) \
223 DECODE_CHARACTER_DIMENSION1 (charset, c1); \
224 *dst++ = (c2) | 0x80; \
228 /*** 1. Preamble ***/
242 #else /* not emacs */
246 #endif /* not emacs */
248 Lisp_Object Qcoding_system
, Qeol_type
;
249 Lisp_Object Qbuffer_file_coding_system
;
250 Lisp_Object Qpost_read_conversion
, Qpre_write_conversion
;
252 extern Lisp_Object Qinsert_file_contents
, Qwrite_region
;
253 Lisp_Object Qcall_process
, Qcall_process_region
, Qprocess_argument
;
254 Lisp_Object Qstart_process
, Qopen_network_stream
;
255 Lisp_Object Qtarget_idx
;
257 /* Mnemonic character of each format of end-of-line. */
258 int eol_mnemonic_unix
, eol_mnemonic_dos
, eol_mnemonic_mac
;
259 /* Mnemonic character to indicate format of end-of-line is not yet
261 int eol_mnemonic_undecided
;
265 Lisp_Object Qcoding_system_spec
, Qcoding_system_p
, Qcoding_system_error
;
267 /* Coding-systems are handed between Emacs Lisp programs and C internal
268 routines by the following three variables. */
269 /* Coding-system for reading files and receiving data from process. */
270 Lisp_Object Vcoding_system_for_read
;
271 /* Coding-system for writing files and sending data to process. */
272 Lisp_Object Vcoding_system_for_write
;
273 /* Coding-system actually used in the latest I/O. */
274 Lisp_Object Vlast_coding_system_used
;
276 /* Coding-system of what terminal accept for displaying. */
277 struct coding_system terminal_coding
;
279 /* Coding-system of what is sent from terminal keyboard. */
280 struct coding_system keyboard_coding
;
282 Lisp_Object Vfile_coding_system_alist
;
283 Lisp_Object Vprocess_coding_system_alist
;
284 Lisp_Object Vnetwork_coding_system_alist
;
288 Lisp_Object Qcoding_category_index
;
290 /* List of symbols `coding-category-xxx' ordered by priority. */
291 Lisp_Object Vcoding_category_list
;
293 /* Table of coding-systems currently assigned to each coding-category. */
294 Lisp_Object coding_category_table
[CODING_CATEGORY_IDX_MAX
];
296 /* Table of names of symbol for each coding-category. */
297 char *coding_category_name
[CODING_CATEGORY_IDX_MAX
] = {
298 "coding-category-emacs-mule",
299 "coding-category-sjis",
300 "coding-category-iso-7",
301 "coding-category-iso-8-1",
302 "coding-category-iso-8-2",
303 "coding-category-iso-else",
304 "coding-category-big5",
305 "coding-category-binary"
308 /* Flag to tell if we look up unification table on character code
310 Lisp_Object Venable_character_unification
;
311 /* Standard unification table to look up on reading (decoding). */
312 Lisp_Object Vstandard_character_unification_table_for_read
;
313 /* Standard unification table to look up on writing (encoding). */
314 Lisp_Object Vstandard_character_unification_table_for_write
;
316 Lisp_Object Qcharacter_unification_table
;
318 /* Alist of charsets vs revision number. */
319 Lisp_Object Vcharset_revision_alist
;
321 /* Default coding systems used for process I/O. */
322 Lisp_Object Vdefault_process_coding_system
;
325 /*** 2. Emacs internal format (emacs-mule) handlers ***/
327 /* Emacs' internal format for encoding multiple character sets is a
328 kind of multi-byte encoding, i.e. encoding a character by a sequence
329 of one-byte codes of variable length. ASCII characters and control
330 characters (e.g. `tab', `newline') are represented by one-byte as
331 is. It takes the range 0x00 through 0x7F. The other characters
332 are represented by a sequence of `base leading-code', optional
333 `extended leading-code', and one or two `position-code's. Length
334 of the sequence is decided by the base leading-code. Leading-code
335 takes the range 0x80 through 0x9F, whereas extended leading-code
336 and position-code take the range 0xA0 through 0xFF. See the
337 document of `charset.h' for more detail about leading-code and
340 There's one exception in this rule. Special leading-code
341 `leading-code-composition' denotes that the following several
342 characters should be composed into one character. Leading-codes of
343 components (except for ASCII) are added 0x20. An ASCII character
344 component is represented by a 2-byte sequence of `0xA0' and
345 `ASCII-code + 0x80'. See also the document in `charset.h' for the
346 detail of composite character. Hence, we can summarize the code
349 --- CODE RANGE of Emacs' internal format ---
350 (character set) (range)
352 ELSE (1st byte) 0x80 .. 0x9F
353 (rest bytes) 0xA0 .. 0xFF
354 ---------------------------------------------
358 enum emacs_code_class_type emacs_code_class
[256];
360 /* Go to the next statement only if *SRC is accessible and the code is
361 greater than 0xA0. */
362 #define CHECK_CODE_RANGE_A0_FF \
364 if (src >= src_end) \
365 goto label_end_of_switch; \
366 else if (*src++ < 0xA0) \
370 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
371 Check if a text is encoded in Emacs' internal format. If it is,
372 return CODING_CATEGORY_MASK_EMASC_MULE, else return 0. */
375 detect_coding_emacs_mule (src
, src_end
)
376 unsigned char *src
, *src_end
;
381 while (src
< src_end
)
393 switch (emacs_code_class
[c
])
395 case EMACS_ascii_code
:
396 case EMACS_linefeed_code
:
399 case EMACS_control_code
:
400 if (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
404 case EMACS_invalid_code
:
407 case EMACS_leading_code_composition
: /* c == 0x80 */
409 CHECK_CODE_RANGE_A0_FF
;
414 case EMACS_leading_code_4
:
415 CHECK_CODE_RANGE_A0_FF
;
416 /* fall down to check it two more times ... */
418 case EMACS_leading_code_3
:
419 CHECK_CODE_RANGE_A0_FF
;
420 /* fall down to check it one more time ... */
422 case EMACS_leading_code_2
:
423 CHECK_CODE_RANGE_A0_FF
;
431 return CODING_CATEGORY_MASK_EMACS_MULE
;
435 /*** 3. ISO2022 handlers ***/
437 /* The following note describes the coding system ISO2022 briefly.
438 Since the intension of this note is to help understanding of the
439 programs in this file, some parts are NOT ACCURATE or OVERLY
440 SIMPLIFIED. For the thorough understanding, please refer to the
441 original document of ISO2022.
443 ISO2022 provides many mechanisms to encode several character sets
444 in 7-bit and 8-bit environment. If one choose 7-bite environment,
445 all text is encoded by codes of less than 128. This may make the
446 encoded text a little bit longer, but the text get more stability
447 to pass through several gateways (some of them split MSB off).
449 There are two kind of character set: control character set and
450 graphic character set. The former contains control characters such
451 as `newline' and `escape' to provide control functions (control
452 functions are provided also by escape sequence). The latter
453 contains graphic characters such as ' A' and '-'. Emacs recognizes
454 two control character sets and many graphic character sets.
456 Graphic character sets are classified into one of the following
457 four classes, DIMENSION1_CHARS94, DIMENSION1_CHARS96,
458 DIMENSION2_CHARS94, DIMENSION2_CHARS96 according to the number of
459 bytes (DIMENSION) and the number of characters in one dimension
460 (CHARS) of the set. In addition, each character set is assigned an
461 identification tag (called "final character" and denoted as <F>
462 here after) which is unique in each class. <F> of each character
463 set is decided by ECMA(*) when it is registered in ISO. Code range
464 of <F> is 0x30..0x7F (0x30..0x3F are for private use only).
466 Note (*): ECMA = European Computer Manufacturers Association
468 Here are examples of graphic character set [NAME(<F>)]:
469 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
470 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
471 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
472 o DIMENSION2_CHARS96 -- none for the moment
474 A code area (1byte=8bits) is divided into 4 areas, C0, GL, C1, and GR.
475 C0 [0x00..0x1F] -- control character plane 0
476 GL [0x20..0x7F] -- graphic character plane 0
477 C1 [0x80..0x9F] -- control character plane 1
478 GR [0xA0..0xFF] -- graphic character plane 1
480 A control character set is directly designated and invoked to C0 or
481 C1 by an escape sequence. The most common case is that ISO646's
482 control character set is designated/invoked to C0 and ISO6429's
483 control character set is designated/invoked to C1, and usually
484 these designations/invocations are omitted in a coded text. With
485 7-bit environment, only C0 can be used, and a control character for
486 C1 is encoded by an appropriate escape sequence to fit in the
487 environment. All control characters for C1 are defined the
488 corresponding escape sequences.
490 A graphic character set is at first designated to one of four
491 graphic registers (G0 through G3), then these graphic registers are
492 invoked to GL or GR. These designations and invocations can be
493 done independently. The most common case is that G0 is invoked to
494 GL, G1 is invoked to GR, and ASCII is designated to G0, and usually
495 these invocations and designations are omitted in a coded text.
496 With 7-bit environment, only GL can be used.
498 When a graphic character set of CHARS94 is invoked to GL, code 0x20
499 and 0x7F of GL area work as control characters SPACE and DEL
500 respectively, and code 0xA0 and 0xFF of GR area should not be used.
502 There are two ways of invocation: locking-shift and single-shift.
503 With locking-shift, the invocation lasts until the next different
504 invocation, whereas with single-shift, the invocation works only
505 for the following character and doesn't affect locking-shift.
506 Invocations are done by the following control characters or escape
509 ----------------------------------------------------------------------
510 function control char escape sequence description
511 ----------------------------------------------------------------------
512 SI (shift-in) 0x0F none invoke G0 to GL
513 SI (shift-out) 0x0E none invoke G1 to GL
514 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
515 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
516 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 into GL
517 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 into GL
518 ----------------------------------------------------------------------
519 The first four are for locking-shift. Control characters for these
520 functions are defined by macros ISO_CODE_XXX in `coding.h'.
522 Designations are done by the following escape sequences.
523 ----------------------------------------------------------------------
524 escape sequence description
525 ----------------------------------------------------------------------
526 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
527 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
528 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
529 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
530 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
531 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
532 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
533 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
534 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
535 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
536 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
537 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
538 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
539 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
540 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
541 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
542 ----------------------------------------------------------------------
544 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
545 of dimension 1, chars 94, and final character <F>, and etc.
547 Note (*): Although these designations are not allowed in ISO2022,
548 Emacs accepts them on decoding, and produces them on encoding
549 CHARS96 character set in a coding system which is characterized as
550 7-bit environment, non-locking-shift, and non-single-shift.
552 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
553 '(' can be omitted. We call this as "short-form" here after.
555 Now you may notice that there are a lot of ways for encoding the
556 same multilingual text in ISO2022. Actually, there exist many
557 coding systems such as Compound Text (used in X's inter client
558 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
559 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
560 localized platforms), and all of these are variants of ISO2022.
562 In addition to the above, Emacs handles two more kinds of escape
563 sequences: ISO6429's direction specification and Emacs' private
564 sequence for specifying character composition.
566 ISO6429's direction specification takes the following format:
567 o CSI ']' -- end of the current direction
568 o CSI '0' ']' -- end of the current direction
569 o CSI '1' ']' -- start of left-to-right text
570 o CSI '2' ']' -- start of right-to-left text
571 The control character CSI (0x9B: control sequence introducer) is
572 abbreviated to the escape sequence ESC '[' in 7-bit environment.
574 Character composition specification takes the following format:
575 o ESC '0' -- start character composition
576 o ESC '1' -- end character composition
577 Since these are not standard escape sequences of any ISO, the use
578 of them for these meaning is restricted to Emacs only. */
580 enum iso_code_class_type iso_code_class
[256];
582 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
583 Check if a text is encoded in ISO2022. If it is, returns an
584 integer in which appropriate flag bits any of:
585 CODING_CATEGORY_MASK_ISO_7
586 CODING_CATEGORY_MASK_ISO_8_1
587 CODING_CATEGORY_MASK_ISO_8_2
588 CODING_CATEGORY_MASK_ISO_ELSE
589 are set. If a code which should never appear in ISO2022 is found,
593 detect_coding_iso2022 (src
, src_end
)
594 unsigned char *src
, *src_end
;
596 int mask
= CODING_CATEGORY_MASK_ANY
;
597 int g1
= 0; /* 1 iff designating to G1. */
600 while (src
< src_end
)
610 && ((c
>= '(' && c
<= '/')
611 || c
== '$' && ((*src
>= '(' && *src
<= '/')
612 || (*src
>= '@' && *src
<= 'B'))))
614 /* Valid designation sequence. */
615 mask
&= (CODING_CATEGORY_MASK_ISO_7
616 | CODING_CATEGORY_MASK_ISO_8_1
617 | CODING_CATEGORY_MASK_ISO_8_2
618 | CODING_CATEGORY_MASK_ISO_ELSE
);
619 if (c
== ')' || (c
== '$' && *src
== ')'))
622 mask
&= ~CODING_CATEGORY_MASK_ISO_7
;
627 else if (c
== 'N' || c
== 'O' || c
== 'n' || c
== 'o')
628 return CODING_CATEGORY_MASK_ISO_ELSE
;
633 return CODING_CATEGORY_MASK_ISO_ELSE
;
639 mask
&= ~CODING_CATEGORY_MASK_ISO_7
;
651 mask
&= ~CODING_CATEGORY_MASK_ISO_7
;
652 while (src
< src_end
&& *src
>= 0xA0)
654 if (count
& 1 && src
< src_end
)
655 mask
&= ~CODING_CATEGORY_MASK_ISO_8_2
;
664 /* Decode a character of which charset is CHARSET and the 1st position
665 code is C1. If dimension of CHARSET is 2, the 2nd position code is
666 fetched from SRC and set to C2. If CHARSET is negative, it means
667 that we are decoding ill formed text, and what we can do is just to
670 #define DECODE_ISO_CHARACTER(charset, c1) \
672 int c_alt, charset_alt = (charset); \
673 if (COMPOSING_HEAD_P (coding->composing)) \
675 *dst++ = LEADING_CODE_COMPOSITION; \
676 if (COMPOSING_WITH_RULE_P (coding->composing)) \
677 /* To tell composition rules are embeded. */ \
679 coding->composing += 2; \
681 if ((charset) >= 0) \
683 if (CHARSET_DIMENSION (charset) == 2) \
684 ONE_MORE_BYTE (c2); \
685 if (!NILP (unification_table) \
686 && ((c_alt = unify_char (unification_table, \
687 -1, (charset), c1, c2)) >= 0)) \
688 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
690 if (charset_alt == CHARSET_ASCII || charset_alt < 0) \
691 DECODE_CHARACTER_ASCII (c1); \
692 else if (CHARSET_DIMENSION (charset_alt) == 1) \
693 DECODE_CHARACTER_DIMENSION1 (charset_alt, c1); \
695 DECODE_CHARACTER_DIMENSION2 (charset_alt, c1, c2); \
696 if (COMPOSING_WITH_RULE_P (coding->composing)) \
697 /* To tell a composition rule follows. */ \
698 coding->composing = COMPOSING_WITH_RULE_RULE; \
701 /* Set designation state into CODING. */
702 #define DECODE_DESIGNATION(reg, dimension, chars, final_char) \
704 int charset = ISO_CHARSET_TABLE (dimension, chars, final_char); \
707 if (coding->direction == 1 \
708 && CHARSET_REVERSE_CHARSET (charset) >= 0) \
709 charset = CHARSET_REVERSE_CHARSET (charset); \
710 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
714 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
717 decode_coding_iso2022 (coding
, source
, destination
,
718 src_bytes
, dst_bytes
, consumed
)
719 struct coding_system
*coding
;
720 unsigned char *source
, *destination
;
721 int src_bytes
, dst_bytes
;
724 unsigned char *src
= source
;
725 unsigned char *src_end
= source
+ src_bytes
;
726 unsigned char *dst
= destination
;
727 unsigned char *dst_end
= destination
+ dst_bytes
;
728 /* Since the maximum bytes produced by each loop is 7, we subtract 6
729 from DST_END to assure that overflow checking is necessary only
730 at the head of loop. */
731 unsigned char *adjusted_dst_end
= dst_end
- 6;
733 /* Charsets invoked to graphic plane 0 and 1 respectively. */
734 int charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
735 int charset1
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 1);
736 Lisp_Object unification_table
= coding
->character_unification_table
;
738 if (!NILP (Venable_character_unification
) && NILP (unification_table
))
739 unification_table
= Vstandard_character_unification_table_for_read
;
741 while (src
< src_end
&& dst
< adjusted_dst_end
)
743 /* SRC_BASE remembers the start position in source in each loop.
744 The loop will be exited when there's not enough source text
745 to analyze long escape sequence or 2-byte code (within macros
746 ONE_MORE_BYTE or TWO_MORE_BYTES). In that case, SRC is reset
747 to SRC_BASE before exiting. */
748 unsigned char *src_base
= src
;
751 switch (iso_code_class
[c1
])
753 case ISO_0x20_or_0x7F
:
754 if (!coding
->composing
755 && (charset0
< 0 || CHARSET_CHARS (charset0
) == 94))
757 /* This is SPACE or DEL. */
761 /* This is a graphic character, we fall down ... */
763 case ISO_graphic_plane_0
:
764 if (coding
->composing
== COMPOSING_WITH_RULE_RULE
)
766 /* This is a composition rule. */
768 coding
->composing
= COMPOSING_WITH_RULE_TAIL
;
771 DECODE_ISO_CHARACTER (charset0
, c1
);
774 case ISO_0xA0_or_0xFF
:
775 if (charset1
< 0 || CHARSET_CHARS (charset1
) == 94)
781 /* This is a graphic character, we fall down ... */
783 case ISO_graphic_plane_1
:
784 DECODE_ISO_CHARACTER (charset1
, c1
);
787 case ISO_control_code
:
788 /* All ISO2022 control characters in this class have the
789 same representation in Emacs internal format. */
793 case ISO_carriage_return
:
794 if (coding
->eol_type
== CODING_EOL_CR
)
798 else if (coding
->eol_type
== CODING_EOL_CRLF
)
801 if (c1
== ISO_CODE_LF
)
816 if (CODING_SPEC_ISO_DESIGNATION (coding
, 1) < 0)
817 goto label_invalid_escape_sequence
;
818 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 1;
819 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
823 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 0;
824 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
827 case ISO_single_shift_2_7
:
828 case ISO_single_shift_2
:
829 /* SS2 is handled as an escape sequence of ESC 'N' */
831 goto label_escape_sequence
;
833 case ISO_single_shift_3
:
834 /* SS2 is handled as an escape sequence of ESC 'O' */
836 goto label_escape_sequence
;
838 case ISO_control_sequence_introducer
:
839 /* CSI is handled as an escape sequence of ESC '[' ... */
841 goto label_escape_sequence
;
845 label_escape_sequence
:
846 /* Escape sequences handled by Emacs are invocation,
847 designation, direction specification, and character
848 composition specification. */
851 case '&': /* revision of following character set */
853 if (!(c1
>= '@' && c1
<= '~'))
854 goto label_invalid_escape_sequence
;
856 if (c1
!= ISO_CODE_ESC
)
857 goto label_invalid_escape_sequence
;
859 goto label_escape_sequence
;
861 case '$': /* designation of 2-byte character set */
863 if (c1
>= '@' && c1
<= 'B')
864 { /* designation of JISX0208.1978, GB2312.1980,
866 DECODE_DESIGNATION (0, 2, 94, c1
);
868 else if (c1
>= 0x28 && c1
<= 0x2B)
869 { /* designation of DIMENSION2_CHARS94 character set */
871 DECODE_DESIGNATION (c1
- 0x28, 2, 94, c2
);
873 else if (c1
>= 0x2C && c1
<= 0x2F)
874 { /* designation of DIMENSION2_CHARS96 character set */
876 DECODE_DESIGNATION (c1
- 0x2C, 2, 96, c2
);
879 goto label_invalid_escape_sequence
;
882 case 'n': /* invocation of locking-shift-2 */
883 if (CODING_SPEC_ISO_DESIGNATION (coding
, 2) < 0)
884 goto label_invalid_escape_sequence
;
885 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 2;
886 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
889 case 'o': /* invocation of locking-shift-3 */
890 if (CODING_SPEC_ISO_DESIGNATION (coding
, 3) < 0)
891 goto label_invalid_escape_sequence
;
892 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 3;
893 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
896 case 'N': /* invocation of single-shift-2 */
897 if (CODING_SPEC_ISO_DESIGNATION (coding
, 2) < 0)
898 goto label_invalid_escape_sequence
;
900 charset
= CODING_SPEC_ISO_DESIGNATION (coding
, 2);
901 DECODE_ISO_CHARACTER (charset
, c1
);
904 case 'O': /* invocation of single-shift-3 */
905 if (CODING_SPEC_ISO_DESIGNATION (coding
, 3) < 0)
906 goto label_invalid_escape_sequence
;
908 charset
= CODING_SPEC_ISO_DESIGNATION (coding
, 3);
909 DECODE_ISO_CHARACTER (charset
, c1
);
912 case '0': /* start composing without embeded rules */
913 coding
->composing
= COMPOSING_NO_RULE_HEAD
;
916 case '1': /* end composing */
917 coding
->composing
= COMPOSING_NO
;
920 case '2': /* start composing with embeded rules */
921 coding
->composing
= COMPOSING_WITH_RULE_HEAD
;
924 case '[': /* specification of direction */
925 /* For the moment, nested direction is not supported.
926 So, the value of `coding->direction' is 0 or 1: 0
927 means left-to-right, 1 means right-to-left. */
931 case ']': /* end of the current direction */
932 coding
->direction
= 0;
934 case '0': /* end of the current direction */
935 case '1': /* start of left-to-right direction */
938 coding
->direction
= 0;
940 goto label_invalid_escape_sequence
;
943 case '2': /* start of right-to-left direction */
946 coding
->direction
= 1;
948 goto label_invalid_escape_sequence
;
952 goto label_invalid_escape_sequence
;
957 if (c1
>= 0x28 && c1
<= 0x2B)
958 { /* designation of DIMENSION1_CHARS94 character set */
960 DECODE_DESIGNATION (c1
- 0x28, 1, 94, c2
);
962 else if (c1
>= 0x2C && c1
<= 0x2F)
963 { /* designation of DIMENSION1_CHARS96 character set */
965 DECODE_DESIGNATION (c1
- 0x2C, 1, 96, c2
);
969 goto label_invalid_escape_sequence
;
972 /* We must update these variables now. */
973 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
974 charset1
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 1);
977 label_invalid_escape_sequence
:
979 int length
= src
- src_base
;
981 bcopy (src_base
, dst
, length
);
988 coding
->carryover_size
= src
- src_base
;
989 bcopy (src_base
, coding
->carryover
, coding
->carryover_size
);
994 /* If this is the last block of the text to be decoded, we had
995 better just flush out all remaining codes in the text although
996 they are not valid characters. */
997 if (coding
->last_block
)
999 bcopy (src
, dst
, src_end
- src
);
1000 dst
+= (src_end
- src
);
1003 *consumed
= src
- source
;
1004 return dst
- destination
;
1007 /* ISO2022 encoding staffs. */
1010 It is not enough to say just "ISO2022" on encoding, but we have to
1011 specify more details. In Emacs, each coding-system of ISO2022
1012 variant has the following specifications:
1013 1. Initial designation to G0 thru G3.
1014 2. Allows short-form designation?
1015 3. ASCII should be designated to G0 before control characters?
1016 4. ASCII should be designated to G0 at end of line?
1017 5. 7-bit environment or 8-bit environment?
1018 6. Use locking-shift?
1019 7. Use Single-shift?
1020 And the following two are only for Japanese:
1021 8. Use ASCII in place of JIS0201-1976-Roman?
1022 9. Use JISX0208-1983 in place of JISX0208-1978?
1023 These specifications are encoded in `coding->flags' as flag bits
1024 defined by macros CODING_FLAG_ISO_XXX. See `coding.h' for more
1028 /* Produce codes (escape sequence) for designating CHARSET to graphic
1029 register REG. If <final-char> of CHARSET is '@', 'A', or 'B' and
1030 the coding system CODING allows, produce designation sequence of
1033 #define ENCODE_DESIGNATION(charset, reg, coding) \
1035 unsigned char final_char = CHARSET_ISO_FINAL_CHAR (charset); \
1036 char *intermediate_char_94 = "()*+"; \
1037 char *intermediate_char_96 = ",-./"; \
1039 = Fassq (make_number (charset), Vcharset_revision_alist); \
1040 if (! NILP (temp)) \
1042 *dst++ = ISO_CODE_ESC; \
1044 *dst++ = XINT (XCONS (temp)->cdr) + '@'; \
1046 *dst++ = ISO_CODE_ESC; \
1047 if (CHARSET_DIMENSION (charset) == 1) \
1049 if (CHARSET_CHARS (charset) == 94) \
1050 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
1052 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
1057 if (CHARSET_CHARS (charset) == 94) \
1059 if (! (coding->flags & CODING_FLAG_ISO_SHORT_FORM) \
1061 || final_char < '@' || final_char > 'B') \
1062 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
1065 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
1067 *dst++ = final_char; \
1068 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
1071 /* The following two macros produce codes (control character or escape
1072 sequence) for ISO2022 single-shift functions (single-shift-2 and
1075 #define ENCODE_SINGLE_SHIFT_2 \
1077 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1078 *dst++ = ISO_CODE_ESC, *dst++ = 'N'; \
1080 *dst++ = ISO_CODE_SS2; \
1081 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
1084 #define ENCODE_SINGLE_SHIFT_3 \
1086 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1087 *dst++ = ISO_CODE_ESC, *dst++ = 'O'; \
1089 *dst++ = ISO_CODE_SS3; \
1090 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
1093 /* The following four macros produce codes (control character or
1094 escape sequence) for ISO2022 locking-shift functions (shift-in,
1095 shift-out, locking-shift-2, and locking-shift-3). */
1097 #define ENCODE_SHIFT_IN \
1099 *dst++ = ISO_CODE_SI; \
1100 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0; \
1103 #define ENCODE_SHIFT_OUT \
1105 *dst++ = ISO_CODE_SO; \
1106 CODING_SPEC_ISO_INVOCATION (coding, 0) = 1; \
1109 #define ENCODE_LOCKING_SHIFT_2 \
1111 *dst++ = ISO_CODE_ESC, *dst++ = 'n'; \
1112 CODING_SPEC_ISO_INVOCATION (coding, 0) = 2; \
1115 #define ENCODE_LOCKING_SHIFT_3 \
1117 *dst++ = ISO_CODE_ESC, *dst++ = 'o'; \
1118 CODING_SPEC_ISO_INVOCATION (coding, 0) = 3; \
1121 /* Produce codes for a DIMENSION1 character of which character set is
1122 CHARSET and position-code is C1. Designation and invocation
1123 sequences are also produced in advance if necessary. */
1126 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
1128 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
1130 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1131 *dst++ = c1 & 0x7F; \
1133 *dst++ = c1 | 0x80; \
1134 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
1137 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
1139 *dst++ = c1 & 0x7F; \
1142 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
1144 *dst++ = c1 | 0x80; \
1148 /* Since CHARSET is not yet invoked to any graphic planes, we \
1149 must invoke it, or, at first, designate it to some graphic \
1150 register. Then repeat the loop to actually produce the \
1152 dst = encode_invocation_designation (charset, coding, dst); \
1155 /* Produce codes for a DIMENSION2 character of which character set is
1156 CHARSET and position-codes are C1 and C2. Designation and
1157 invocation codes are also produced in advance if necessary. */
1159 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
1161 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
1163 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1164 *dst++ = c1 & 0x7F, *dst++ = c2 & 0x7F; \
1166 *dst++ = c1 | 0x80, *dst++ = c2 | 0x80; \
1167 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
1170 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
1172 *dst++ = c1 & 0x7F, *dst++= c2 & 0x7F; \
1175 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
1177 *dst++ = c1 | 0x80, *dst++= c2 | 0x80; \
1181 /* Since CHARSET is not yet invoked to any graphic planes, we \
1182 must invoke it, or, at first, designate it to some graphic \
1183 register. Then repeat the loop to actually produce the \
1185 dst = encode_invocation_designation (charset, coding, dst); \
1188 #define ENCODE_ISO_CHARACTER(charset, c1, c2) \
1190 int c_alt, charset_alt; \
1191 if (!NILP (unification_table) \
1192 && ((c_alt = unify_char (unification_table, -1, charset, c1, c2)) \
1194 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
1196 charset_alt = charset; \
1197 if (CHARSET_DIMENSION (charset_alt) == 1) \
1198 ENCODE_ISO_CHARACTER_DIMENSION1 (charset_alt, c1); \
1200 ENCODE_ISO_CHARACTER_DIMENSION2 (charset_alt, c1, c2); \
1203 /* Produce designation and invocation codes at a place pointed by DST
1204 to use CHARSET. The element `spec.iso2022' of *CODING is updated.
1208 encode_invocation_designation (charset
, coding
, dst
)
1210 struct coding_system
*coding
;
1213 int reg
; /* graphic register number */
1215 /* At first, check designations. */
1216 for (reg
= 0; reg
< 4; reg
++)
1217 if (charset
== CODING_SPEC_ISO_DESIGNATION (coding
, reg
))
1222 /* CHARSET is not yet designated to any graphic registers. */
1223 /* At first check the requested designation. */
1224 reg
= CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
);
1225 if (reg
== CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
)
1226 /* Since CHARSET requests no special designation, designate it
1227 to graphic register 0. */
1230 ENCODE_DESIGNATION (charset
, reg
, coding
);
1233 if (CODING_SPEC_ISO_INVOCATION (coding
, 0) != reg
1234 && CODING_SPEC_ISO_INVOCATION (coding
, 1) != reg
)
1236 /* Since the graphic register REG is not invoked to any graphic
1237 planes, invoke it to graphic plane 0. */
1240 case 0: /* graphic register 0 */
1244 case 1: /* graphic register 1 */
1248 case 2: /* graphic register 2 */
1249 if (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
1250 ENCODE_SINGLE_SHIFT_2
;
1252 ENCODE_LOCKING_SHIFT_2
;
1255 case 3: /* graphic register 3 */
1256 if (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
1257 ENCODE_SINGLE_SHIFT_3
;
1259 ENCODE_LOCKING_SHIFT_3
;
1266 /* The following two macros produce codes for indicating composition. */
1267 #define ENCODE_COMPOSITION_NO_RULE_START *dst++ = ISO_CODE_ESC, *dst++ = '0'
1268 #define ENCODE_COMPOSITION_WITH_RULE_START *dst++ = ISO_CODE_ESC, *dst++ = '2'
1269 #define ENCODE_COMPOSITION_END *dst++ = ISO_CODE_ESC, *dst++ = '1'
1271 /* The following three macros produce codes for indicating direction
1273 #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
1275 if (coding->flags == CODING_FLAG_ISO_SEVEN_BITS) \
1276 *dst++ = ISO_CODE_ESC, *dst++ = '['; \
1278 *dst++ = ISO_CODE_CSI; \
1281 #define ENCODE_DIRECTION_R2L \
1282 ENCODE_CONTROL_SEQUENCE_INTRODUCER, *dst++ = '2', *dst++ = ']'
1284 #define ENCODE_DIRECTION_L2R \
1285 ENCODE_CONTROL_SEQUENCE_INTRODUCER, *dst++ = '0', *dst++ = ']'
1287 /* Produce codes for designation and invocation to reset the graphic
1288 planes and registers to initial state. */
1289 #define ENCODE_RESET_PLANE_AND_REGISTER \
1292 if (CODING_SPEC_ISO_INVOCATION (coding, 0) != 0) \
1294 for (reg = 0; reg < 4; reg++) \
1295 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg) >= 0 \
1296 && (CODING_SPEC_ISO_DESIGNATION (coding, reg) \
1297 != CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg))) \
1298 ENCODE_DESIGNATION \
1299 (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg), reg, coding); \
1302 /* Produce designation sequences of charsets in the line started from
1303 *SRC to a place pointed by DSTP.
1305 If the current block ends before any end-of-line, we may fail to
1306 find all the necessary *designations. */
1307 encode_designation_at_bol (coding
, table
, src
, src_end
, dstp
)
1308 struct coding_system
*coding
;
1310 unsigned char *src
, *src_end
, **dstp
;
1312 int charset
, c
, found
= 0, reg
;
1313 /* Table of charsets to be designated to each graphic register. */
1315 unsigned char *dst
= *dstp
;
1317 for (reg
= 0; reg
< 4; reg
++)
1320 while (src
< src_end
&& *src
!= '\n' && found
< 4)
1322 int bytes
= BYTES_BY_CHAR_HEAD (*src
);
1325 charset
= CHARSET_AT (src
);
1330 SPLIT_STRING(src
, bytes
, charset
, c1
, c2
);
1331 if ((c_alt
= unify_char (table
, -1, charset
, c1
, c2
)) >= 0)
1332 charset
= CHAR_CHARSET (c_alt
);
1335 reg
= CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
);
1336 if (r
[reg
] == CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
)
1347 for (reg
= 0; reg
< 4; reg
++)
1349 && CODING_SPEC_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
1350 ENCODE_DESIGNATION (r
[reg
], reg
, coding
);
1355 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
1358 encode_coding_iso2022 (coding
, source
, destination
,
1359 src_bytes
, dst_bytes
, consumed
)
1360 struct coding_system
*coding
;
1361 unsigned char *source
, *destination
;
1362 int src_bytes
, dst_bytes
;
1365 unsigned char *src
= source
;
1366 unsigned char *src_end
= source
+ src_bytes
;
1367 unsigned char *dst
= destination
;
1368 unsigned char *dst_end
= destination
+ dst_bytes
;
1369 /* Since the maximum bytes produced by each loop is 20, we subtract 19
1370 from DST_END to assure overflow checking is necessary only at the
1372 unsigned char *adjusted_dst_end
= dst_end
- 19;
1373 Lisp_Object unification_table
= coding
->character_unification_table
;
1375 if (!NILP (Venable_character_unification
) && NILP (unification_table
))
1376 unification_table
= Vstandard_character_unification_table_for_write
;
1378 while (src
< src_end
&& dst
< adjusted_dst_end
)
1380 /* SRC_BASE remembers the start position in source in each loop.
1381 The loop will be exited when there's not enough source text
1382 to analyze multi-byte codes (within macros ONE_MORE_BYTE,
1383 TWO_MORE_BYTES, and THREE_MORE_BYTES). In that case, SRC is
1384 reset to SRC_BASE before exiting. */
1385 unsigned char *src_base
= src
;
1386 int charset
, c1
, c2
, c3
, c4
;
1388 if (coding
->flags
& CODING_FLAG_ISO_DESIGNATE_AT_BOL
1389 && CODING_SPEC_ISO_BOL (coding
))
1391 /* We have to produce designation sequences if any now. */
1392 encode_designation_at_bol (coding
, unification_table
,
1393 src
, src_end
, &dst
);
1394 CODING_SPEC_ISO_BOL (coding
) = 0;
1398 /* If we are seeing a component of a composite character, we are
1399 seeing a leading-code specially encoded for composition, or a
1400 composition rule if composing with rule. We must set C1
1401 to a normal leading-code or an ASCII code. If we are not at
1402 a composed character, we must reset the composition state. */
1403 if (COMPOSING_P (coding
->composing
))
1407 /* We are not in a composite character any longer. */
1408 coding
->composing
= COMPOSING_NO
;
1409 ENCODE_COMPOSITION_END
;
1413 if (coding
->composing
== COMPOSING_WITH_RULE_RULE
)
1416 coding
->composing
= COMPOSING_WITH_RULE_HEAD
;
1419 else if (coding
->composing
== COMPOSING_WITH_RULE_HEAD
)
1420 coding
->composing
= COMPOSING_WITH_RULE_RULE
;
1423 /* This is an ASCII component. */
1428 /* This is a leading-code of non ASCII component. */
1433 /* Now encode one character. C1 is a control character, an
1434 ASCII character, or a leading-code of multi-byte character. */
1435 switch (emacs_code_class
[c1
])
1437 case EMACS_ascii_code
:
1438 ENCODE_ISO_CHARACTER (CHARSET_ASCII
, c1
, /* dummy */ c2
);
1441 case EMACS_control_code
:
1442 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_CNTL
)
1443 ENCODE_RESET_PLANE_AND_REGISTER
;
1447 case EMACS_carriage_return_code
:
1448 if (!coding
->selective
)
1450 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_CNTL
)
1451 ENCODE_RESET_PLANE_AND_REGISTER
;
1455 /* fall down to treat '\r' as '\n' ... */
1457 case EMACS_linefeed_code
:
1458 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_EOL
)
1459 ENCODE_RESET_PLANE_AND_REGISTER
;
1460 if (coding
->flags
& CODING_FLAG_ISO_INIT_AT_BOL
)
1461 bcopy (coding
->spec
.iso2022
.initial_designation
,
1462 coding
->spec
.iso2022
.current_designation
,
1463 sizeof coding
->spec
.iso2022
.initial_designation
);
1464 if (coding
->eol_type
== CODING_EOL_LF
1465 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
1466 *dst
++ = ISO_CODE_LF
;
1467 else if (coding
->eol_type
== CODING_EOL_CRLF
)
1468 *dst
++ = ISO_CODE_CR
, *dst
++ = ISO_CODE_LF
;
1470 *dst
++ = ISO_CODE_CR
;
1471 CODING_SPEC_ISO_BOL (coding
) = 1;
1474 case EMACS_leading_code_2
:
1476 ENCODE_ISO_CHARACTER (c1
, c2
, /* dummy */ c3
);
1479 case EMACS_leading_code_3
:
1480 TWO_MORE_BYTES (c2
, c3
);
1481 if (c1
< LEADING_CODE_PRIVATE_11
)
1482 ENCODE_ISO_CHARACTER (c1
, c2
, c3
);
1484 ENCODE_ISO_CHARACTER (c2
, c3
, /* dummy */ c4
);
1487 case EMACS_leading_code_4
:
1488 THREE_MORE_BYTES (c2
, c3
, c4
);
1489 ENCODE_ISO_CHARACTER (c2
, c3
, c4
);
1492 case EMACS_leading_code_composition
:
1496 coding
->composing
= COMPOSING_WITH_RULE_HEAD
;
1497 ENCODE_COMPOSITION_WITH_RULE_START
;
1501 /* Rewind one byte because it is a character code of
1502 composition elements. */
1504 coding
->composing
= COMPOSING_NO_RULE_HEAD
;
1505 ENCODE_COMPOSITION_NO_RULE_START
;
1509 case EMACS_invalid_code
:
1515 coding
->carryover_size
= src
- src_base
;
1516 bcopy (src_base
, coding
->carryover
, coding
->carryover_size
);
1520 /* If this is the last block of the text to be encoded, we must
1521 reset graphic planes and registers to the initial state. */
1522 if (src
>= src_end
&& coding
->last_block
)
1524 ENCODE_RESET_PLANE_AND_REGISTER
;
1525 if (coding
->carryover_size
> 0
1526 && coding
->carryover_size
< (dst_end
- dst
))
1528 bcopy (coding
->carryover
, dst
, coding
->carryover_size
);
1529 dst
+= coding
->carryover_size
;
1530 coding
->carryover_size
= 0;
1533 *consumed
= src
- source
;
1534 return dst
- destination
;
1538 /*** 4. SJIS and BIG5 handlers ***/
1540 /* Although SJIS and BIG5 are not ISO's coding system, They are used
1541 quite widely. So, for the moment, Emacs supports them in the bare
1542 C code. But, in the future, they may be supported only by CCL. */
1544 /* SJIS is a coding system encoding three character sets: ASCII, right
1545 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
1546 as is. A character of charset katakana-jisx0201 is encoded by
1547 "position-code + 0x80". A character of charset japanese-jisx0208
1548 is encoded in 2-byte but two position-codes are divided and shifted
1549 so that it fit in the range below.
1551 --- CODE RANGE of SJIS ---
1552 (character set) (range)
1554 KATAKANA-JISX0201 0xA0 .. 0xDF
1555 JISX0208 (1st byte) 0x80 .. 0x9F and 0xE0 .. 0xFF
1556 (2nd byte) 0x40 .. 0xFF
1557 -------------------------------
1561 /* BIG5 is a coding system encoding two character sets: ASCII and
1562 Big5. An ASCII character is encoded as is. Big5 is a two-byte
1563 character set and is encoded in two-byte.
1565 --- CODE RANGE of BIG5 ---
1566 (character set) (range)
1568 Big5 (1st byte) 0xA1 .. 0xFE
1569 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
1570 --------------------------
1572 Since the number of characters in Big5 is larger than maximum
1573 characters in Emacs' charset (96x96), it can't be handled as one
1574 charset. So, in Emacs, Big5 is divided into two: `charset-big5-1'
1575 and `charset-big5-2'. Both are DIMENSION2 and CHARS94. The former
1576 contains frequently used characters and the latter contains less
1577 frequently used characters. */
1579 /* Macros to decode or encode a character of Big5 in BIG5. B1 and B2
1580 are the 1st and 2nd position-codes of Big5 in BIG5 coding system.
1581 C1 and C2 are the 1st and 2nd position-codes of of Emacs' internal
1582 format. CHARSET is `charset_big5_1' or `charset_big5_2'. */
1584 /* Number of Big5 characters which have the same code in 1st byte. */
1585 #define BIG5_SAME_ROW (0xFF - 0xA1 + 0x7F - 0x40)
1587 #define DECODE_BIG5(b1, b2, charset, c1, c2) \
1590 = (b1 - 0xA1) * BIG5_SAME_ROW + b2 - (b2 < 0x7F ? 0x40 : 0x62); \
1592 charset = charset_big5_1; \
1595 charset = charset_big5_2; \
1596 temp -= (0xC9 - 0xA1) * BIG5_SAME_ROW; \
1598 c1 = temp / (0xFF - 0xA1) + 0x21; \
1599 c2 = temp % (0xFF - 0xA1) + 0x21; \
1602 #define ENCODE_BIG5(charset, c1, c2, b1, b2) \
1604 unsigned int temp = (c1 - 0x21) * (0xFF - 0xA1) + (c2 - 0x21); \
1605 if (charset == charset_big5_2) \
1606 temp += BIG5_SAME_ROW * (0xC9 - 0xA1); \
1607 b1 = temp / BIG5_SAME_ROW + 0xA1; \
1608 b2 = temp % BIG5_SAME_ROW; \
1609 b2 += b2 < 0x3F ? 0x40 : 0x62; \
1612 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1613 Check if a text is encoded in SJIS. If it is, return
1614 CODING_CATEGORY_MASK_SJIS, else return 0. */
1617 detect_coding_sjis (src
, src_end
)
1618 unsigned char *src
, *src_end
;
1622 while (src
< src_end
)
1625 if (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
1627 if ((c
>= 0x80 && c
< 0xA0) || c
>= 0xE0)
1629 if (src
< src_end
&& *src
++ < 0x40)
1633 return CODING_CATEGORY_MASK_SJIS
;
1636 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1637 Check if a text is encoded in BIG5. If it is, return
1638 CODING_CATEGORY_MASK_BIG5, else return 0. */
1641 detect_coding_big5 (src
, src_end
)
1642 unsigned char *src
, *src_end
;
1646 while (src
< src_end
)
1649 if (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
1656 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
1660 return CODING_CATEGORY_MASK_BIG5
;
1663 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
1664 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
1667 decode_coding_sjis_big5 (coding
, source
, destination
,
1668 src_bytes
, dst_bytes
, consumed
, sjis_p
)
1669 struct coding_system
*coding
;
1670 unsigned char *source
, *destination
;
1671 int src_bytes
, dst_bytes
;
1675 unsigned char *src
= source
;
1676 unsigned char *src_end
= source
+ src_bytes
;
1677 unsigned char *dst
= destination
;
1678 unsigned char *dst_end
= destination
+ dst_bytes
;
1679 /* Since the maximum bytes produced by each loop is 4, we subtract 3
1680 from DST_END to assure overflow checking is necessary only at the
1682 unsigned char *adjusted_dst_end
= dst_end
- 3;
1684 while (src
< src_end
&& dst
< adjusted_dst_end
)
1686 /* SRC_BASE remembers the start position in source in each loop.
1687 The loop will be exited when there's not enough source text
1688 to analyze two-byte character (within macro ONE_MORE_BYTE).
1689 In that case, SRC is reset to SRC_BASE before exiting. */
1690 unsigned char *src_base
= src
;
1691 unsigned char c1
= *src
++, c2
, c3
, c4
;
1695 if (coding
->eol_type
== CODING_EOL_CRLF
)
1701 /* To process C2 again, SRC is subtracted by 1. */
1709 else if (c1
< 0xA0 || c1
>= 0xE0)
1711 /* SJIS -> JISX0208, BIG5 -> Big5 (only if 0xE0 <= c1 < 0xFF) */
1715 DECODE_SJIS (c1
, c2
, c3
, c4
);
1716 DECODE_CHARACTER_DIMENSION2 (charset_jisx0208
, c3
, c4
);
1718 else if (c1
>= 0xE0 && c1
< 0xFF)
1723 DECODE_BIG5 (c1
, c2
, charset
, c3
, c4
);
1724 DECODE_CHARACTER_DIMENSION2 (charset
, c3
, c4
);
1726 else /* Invalid code */
1731 /* SJIS -> JISX0201-Kana, BIG5 -> Big5 */
1733 DECODE_CHARACTER_DIMENSION1 (charset_katakana_jisx0201
, c1
);
1739 DECODE_BIG5 (c1
, c2
, charset
, c3
, c4
);
1740 DECODE_CHARACTER_DIMENSION2 (charset
, c3
, c4
);
1746 coding
->carryover_size
= src
- src_base
;
1747 bcopy (src_base
, coding
->carryover
, coding
->carryover_size
);
1752 *consumed
= src
- source
;
1753 return dst
- destination
;
1756 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
1757 This function can encode `charset_ascii', `charset_katakana_jisx0201',
1758 `charset_jisx0208', `charset_big5_1', and `charset_big5-2'. We are
1759 sure that all these charsets are registered as official charset
1760 (i.e. do not have extended leading-codes). Characters of other
1761 charsets are produced without any encoding. If SJIS_P is 1, encode
1762 SJIS text, else encode BIG5 text. */
1765 encode_coding_sjis_big5 (coding
, source
, destination
,
1766 src_bytes
, dst_bytes
, consumed
, sjis_p
)
1767 struct coding_system
*coding
;
1768 unsigned char *source
, *destination
;
1769 int src_bytes
, dst_bytes
;
1773 unsigned char *src
= source
;
1774 unsigned char *src_end
= source
+ src_bytes
;
1775 unsigned char *dst
= destination
;
1776 unsigned char *dst_end
= destination
+ dst_bytes
;
1777 /* Since the maximum bytes produced by each loop is 2, we subtract 1
1778 from DST_END to assure overflow checking is necessary only at the
1780 unsigned char *adjusted_dst_end
= dst_end
- 1;
1782 while (src
< src_end
&& dst
< adjusted_dst_end
)
1784 /* SRC_BASE remembers the start position in source in each loop.
1785 The loop will be exited when there's not enough source text
1786 to analyze multi-byte codes (within macros ONE_MORE_BYTE and
1787 TWO_MORE_BYTES). In that case, SRC is reset to SRC_BASE
1789 unsigned char *src_base
= src
;
1790 unsigned char c1
= *src
++, c2
, c3
, c4
;
1792 if (coding
->composing
)
1799 else if (c1
>= 0xA0)
1802 coding
->composing
= 0;
1805 switch (emacs_code_class
[c1
])
1807 case EMACS_ascii_code
:
1808 case EMACS_control_code
:
1812 case EMACS_carriage_return_code
:
1813 if (!coding
->selective
)
1818 /* fall down to treat '\r' as '\n' ... */
1820 case EMACS_linefeed_code
:
1821 if (coding
->eol_type
== CODING_EOL_LF
1822 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
1824 else if (coding
->eol_type
== CODING_EOL_CRLF
)
1825 *dst
++ = '\r', *dst
++ = '\n';
1830 case EMACS_leading_code_2
:
1832 if (sjis_p
&& c1
== charset_katakana_jisx0201
)
1835 *dst
++ = c1
, *dst
++ = c2
;
1838 case EMACS_leading_code_3
:
1839 TWO_MORE_BYTES (c2
, c3
);
1840 c2
&= 0x7F, c3
&= 0x7F;
1841 if (sjis_p
&& c1
== charset_jisx0208
)
1843 unsigned char s1
, s2
;
1845 ENCODE_SJIS (c2
, c3
, s1
, s2
);
1846 *dst
++ = s1
, *dst
++ = s2
;
1848 else if (!sjis_p
&& (c1
== charset_big5_1
|| c1
== charset_big5_2
))
1850 unsigned char b1
, b2
;
1852 ENCODE_BIG5 (c1
, c2
, c3
, b1
, b2
);
1853 *dst
++ = b1
, *dst
++ = b2
;
1856 *dst
++ = c1
, *dst
++ = c2
, *dst
++ = c3
;
1859 case EMACS_leading_code_4
:
1860 THREE_MORE_BYTES (c2
, c3
, c4
);
1861 *dst
++ = c1
, *dst
++ = c2
, *dst
++ = c3
, *dst
++ = c4
;
1864 case EMACS_leading_code_composition
:
1865 coding
->composing
= 1;
1868 default: /* i.e. case EMACS_invalid_code: */
1874 coding
->carryover_size
= src
- src_base
;
1875 bcopy (src_base
, coding
->carryover
, coding
->carryover_size
);
1880 *consumed
= src
- source
;
1881 return dst
- destination
;
1885 /*** 5. End-of-line handlers ***/
1887 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
1888 This function is called only when `coding->eol_type' is
1889 CODING_EOL_CRLF or CODING_EOL_CR. */
1891 decode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
1892 struct coding_system
*coding
;
1893 unsigned char *source
, *destination
;
1894 int src_bytes
, dst_bytes
;
1897 unsigned char *src
= source
;
1898 unsigned char *src_end
= source
+ src_bytes
;
1899 unsigned char *dst
= destination
;
1900 unsigned char *dst_end
= destination
+ dst_bytes
;
1903 switch (coding
->eol_type
)
1905 case CODING_EOL_CRLF
:
1907 /* Since the maximum bytes produced by each loop is 2, we
1908 subtract 1 from DST_END to assure overflow checking is
1909 necessary only at the head of loop. */
1910 unsigned char *adjusted_dst_end
= dst_end
- 1;
1912 while (src
< src_end
&& dst
< adjusted_dst_end
)
1914 unsigned char *src_base
= src
;
1915 unsigned char c
= *src
++;
1928 coding
->carryover_size
= src
- src_base
;
1929 bcopy (src_base
, coding
->carryover
, coding
->carryover_size
);
1933 *consumed
= src
- source
;
1934 produced
= dst
- destination
;
1939 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
1940 bcopy (source
, destination
, produced
);
1941 dst_end
= destination
+ produced
;
1942 while (dst
< dst_end
)
1943 if (*dst
++ == '\r') dst
[-1] = '\n';
1944 *consumed
= produced
;
1947 default: /* i.e. case: CODING_EOL_LF */
1948 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
1949 bcopy (source
, destination
, produced
);
1950 *consumed
= produced
;
1957 /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". Encode
1958 format of end-of-line according to `coding->eol_type'. If
1959 `coding->selective' is 1, code '\r' in source text also means
1962 encode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
1963 struct coding_system
*coding
;
1964 unsigned char *source
, *destination
;
1965 int src_bytes
, dst_bytes
;
1968 unsigned char *src
= source
;
1969 unsigned char *dst
= destination
;
1975 switch (coding
->eol_type
)
1978 case CODING_EOL_UNDECIDED
:
1979 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
1980 bcopy (source
, destination
, produced
);
1981 if (coding
->selective
)
1985 if (*dst
++ == '\r') dst
[-1] = '\n';
1987 *consumed
= produced
;
1989 case CODING_EOL_CRLF
:
1992 unsigned char *src_end
= source
+ src_bytes
;
1993 unsigned char *dst_end
= destination
+ dst_bytes
;
1994 /* Since the maximum bytes produced by each loop is 2, we
1995 subtract 1 from DST_END to assure overflow checking is
1996 necessary only at the head of loop. */
1997 unsigned char *adjusted_dst_end
= dst_end
- 1;
1999 while (src
< src_end
&& dst
< adjusted_dst_end
)
2002 if (c
== '\n' || (c
== '\r' && coding
->selective
))
2003 *dst
++ = '\r', *dst
++ = '\n';
2007 produced
= dst
- destination
;
2008 *consumed
= src
- source
;
2012 default: /* i.e. case CODING_EOL_CR: */
2013 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
2014 bcopy (source
, destination
, produced
);
2018 if (*dst
++ == '\n') dst
[-1] = '\r';
2020 *consumed
= produced
;
2027 /*** 6. C library functions ***/
2029 /* In Emacs Lisp, coding system is represented by a Lisp symbol which
2030 has a property `coding-system'. The value of this property is a
2031 vector of length 5 (called as coding-vector). Among elements of
2032 this vector, the first (element[0]) and the fifth (element[4])
2033 carry important information for decoding/encoding. Before
2034 decoding/encoding, this information should be set in fields of a
2035 structure of type `coding_system'.
2037 A value of property `coding-system' can be a symbol of another
2038 subsidiary coding-system. In that case, Emacs gets coding-vector
2041 `element[0]' contains information to be set in `coding->type'. The
2042 value and its meaning is as follows:
2044 0 -- coding_type_emacs_mule
2045 1 -- coding_type_sjis
2046 2 -- coding_type_iso2022
2047 3 -- coding_type_big5
2048 4 -- coding_type_ccl encoder/decoder written in CCL
2049 nil -- coding_type_no_conversion
2050 t -- coding_type_undecided (automatic conversion on decoding,
2051 no-conversion on encoding)
2053 `element[4]' contains information to be set in `coding->flags' and
2054 `coding->spec'. The meaning varies by `coding->type'.
2056 If `coding->type' is `coding_type_iso2022', element[4] is a vector
2057 of length 32 (of which the first 13 sub-elements are used now).
2058 Meanings of these sub-elements are:
2060 sub-element[N] where N is 0 through 3: to be set in `coding->spec.iso2022'
2061 If the value is an integer of valid charset, the charset is
2062 assumed to be designated to graphic register N initially.
2064 If the value is minus, it is a minus value of charset which
2065 reserves graphic register N, which means that the charset is
2066 not designated initially but should be designated to graphic
2067 register N just before encoding a character in that charset.
2069 If the value is nil, graphic register N is never used on
2072 sub-element[N] where N is 4 through 11: to be set in `coding->flags'
2073 Each value takes t or nil. See the section ISO2022 of
2074 `coding.h' for more information.
2076 If `coding->type' is `coding_type_big5', element[4] is t to denote
2077 BIG5-ETen or nil to denote BIG5-HKU.
2079 If `coding->type' takes the other value, element[4] is ignored.
2081 Emacs Lisp's coding system also carries information about format of
2082 end-of-line in a value of property `eol-type'. If the value is
2083 integer, 0 means CODING_EOL_LF, 1 means CODING_EOL_CRLF, and 2
2084 means CODING_EOL_CR. If it is not integer, it should be a vector
2085 of subsidiary coding systems of which property `eol-type' has one
2090 /* Extract information for decoding/encoding from CODING_SYSTEM_SYMBOL
2091 and set it in CODING. If CODING_SYSTEM_SYMBOL is invalid, CODING
2092 is setup so that no conversion is necessary and return -1, else
2096 setup_coding_system (coding_system
, coding
)
2097 Lisp_Object coding_system
;
2098 struct coding_system
*coding
;
2100 Lisp_Object type
, eol_type
;
2102 /* At first, set several fields default values. */
2103 coding
->require_flushing
= 0;
2104 coding
->last_block
= 0;
2105 coding
->selective
= 0;
2106 coding
->composing
= 0;
2107 coding
->direction
= 0;
2108 coding
->carryover_size
= 0;
2109 coding
->post_read_conversion
= coding
->pre_write_conversion
= Qnil
;
2110 /* We have not yet implemented a way to specify unification table in
2112 coding
->character_unification_table
= Qnil
;
2114 Vlast_coding_system_used
= coding
->symbol
= coding_system
;
2116 /* Get value of property `coding-system' until we get a vector.
2117 While doing that, also get values of properties
2118 `post-read-conversion', `pre-write-conversion', and `eol-type'. */
2119 while (!NILP (coding_system
) && SYMBOLP (coding_system
))
2121 if (NILP (coding
->post_read_conversion
))
2122 coding
->post_read_conversion
= Fget (coding_system
,
2123 Qpost_read_conversion
);
2124 if (NILP (coding
->pre_write_conversion
))
2125 coding
->pre_write_conversion
= Fget (coding_system
,
2126 Qpre_write_conversion
);
2127 if (NILP (eol_type
))
2128 eol_type
= Fget (coding_system
, Qeol_type
);
2129 coding_system
= Fget (coding_system
, Qcoding_system
);
2131 if (!VECTORP (coding_system
)
2132 || XVECTOR (coding_system
)->size
!= 5)
2133 goto label_invalid_coding_system
;
2135 if (VECTORP (eol_type
))
2136 coding
->eol_type
= CODING_EOL_UNDECIDED
;
2137 else if (XFASTINT (eol_type
) == 1)
2138 coding
->eol_type
= CODING_EOL_CRLF
;
2139 else if (XFASTINT (eol_type
) == 2)
2140 coding
->eol_type
= CODING_EOL_CR
;
2142 coding
->eol_type
= CODING_EOL_LF
;
2144 type
= XVECTOR (coding_system
)->contents
[0];
2145 switch (XFASTINT (type
))
2148 coding
->type
= coding_type_emacs_mule
;
2152 coding
->type
= coding_type_sjis
;
2156 coding
->type
= coding_type_iso2022
;
2158 Lisp_Object val
= XVECTOR (coding_system
)->contents
[4];
2160 int i
, charset
, default_reg_bits
= 0;
2162 if (!VECTORP (val
) || XVECTOR (val
)->size
!= 32)
2163 goto label_invalid_coding_system
;
2165 flags
= XVECTOR (val
)->contents
;
2167 = ((NILP (flags
[4]) ? 0 : CODING_FLAG_ISO_SHORT_FORM
)
2168 | (NILP (flags
[5]) ? 0 : CODING_FLAG_ISO_RESET_AT_EOL
)
2169 | (NILP (flags
[6]) ? 0 : CODING_FLAG_ISO_RESET_AT_CNTL
)
2170 | (NILP (flags
[7]) ? 0 : CODING_FLAG_ISO_SEVEN_BITS
)
2171 | (NILP (flags
[8]) ? 0 : CODING_FLAG_ISO_LOCKING_SHIFT
)
2172 | (NILP (flags
[9]) ? 0 : CODING_FLAG_ISO_SINGLE_SHIFT
)
2173 | (NILP (flags
[10]) ? 0 : CODING_FLAG_ISO_USE_ROMAN
)
2174 | (NILP (flags
[11]) ? 0 : CODING_FLAG_ISO_USE_OLDJIS
)
2175 | (NILP (flags
[12]) ? 0 : CODING_FLAG_ISO_NO_DIRECTION
)
2176 | (NILP (flags
[13]) ? 0 : CODING_FLAG_ISO_INIT_AT_BOL
)
2177 | (NILP (flags
[14]) ? 0 : CODING_FLAG_ISO_DESIGNATE_AT_BOL
));
2179 /* Invoke graphic register 0 to plane 0. */
2180 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 0;
2181 /* Invoke graphic register 1 to plane 1 if we can use full 8-bit. */
2182 CODING_SPEC_ISO_INVOCATION (coding
, 1)
2183 = (coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
? -1 : 1);
2184 /* Not single shifting at first. */
2185 CODING_SPEC_ISO_SINGLE_SHIFTING(coding
) = 0;
2186 /* Beginning of buffer should also be regarded as bol. */
2187 CODING_SPEC_ISO_BOL(coding
) = 1;
2189 /* Checks FLAGS[REG] (REG = 0, 1, 2 3) and decide designations.
2190 FLAGS[REG] can be one of below:
2191 integer CHARSET: CHARSET occupies register I,
2192 t: designate nothing to REG initially, but can be used
2194 list of integer, nil, or t: designate the first
2195 element (if integer) to REG initially, the remaining
2196 elements (if integer) is designated to REG on request,
2197 if an element is t, REG can be used by any charset,
2198 nil: REG is never used. */
2199 for (charset
= 0; charset
<= MAX_CHARSET
; charset
++)
2200 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2201 = CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
;
2202 for (i
= 0; i
< 4; i
++)
2204 if (INTEGERP (flags
[i
])
2205 && (charset
= XINT (flags
[i
]), CHARSET_VALID_P (charset
))
2206 || (charset
= get_charset_id (flags
[i
])) >= 0)
2208 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = charset
;
2209 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
) = i
;
2211 else if (EQ (flags
[i
], Qt
))
2213 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
2214 default_reg_bits
|= 1 << i
;
2216 else if (CONSP (flags
[i
]))
2218 Lisp_Object tail
= flags
[i
];
2220 if (INTEGERP (XCONS (tail
)->car
)
2221 && (charset
= XINT (XCONS (tail
)->car
),
2222 CHARSET_VALID_P (charset
))
2223 || (charset
= get_charset_id (XCONS (tail
)->car
)) >= 0)
2225 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = charset
;
2226 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
) =i
;
2229 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
2230 tail
= XCONS (tail
)->cdr
;
2231 while (CONSP (tail
))
2233 if (INTEGERP (XCONS (tail
)->car
)
2234 && (charset
= XINT (XCONS (tail
)->car
),
2235 CHARSET_VALID_P (charset
))
2236 || (charset
= get_charset_id (XCONS (tail
)->car
)) >= 0)
2237 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2239 else if (EQ (XCONS (tail
)->car
, Qt
))
2240 default_reg_bits
|= 1 << i
;
2241 tail
= XCONS (tail
)->cdr
;
2245 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
2247 CODING_SPEC_ISO_DESIGNATION (coding
, i
)
2248 = CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
);
2251 if (! (coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
))
2253 /* REG 1 can be used only by locking shift in 7-bit env. */
2254 if (coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
)
2255 default_reg_bits
&= ~2;
2256 if (! (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
))
2257 /* Without any shifting, only REG 0 and 1 can be used. */
2258 default_reg_bits
&= 3;
2261 for (charset
= 0; charset
<= MAX_CHARSET
; charset
++)
2262 if (CHARSET_VALID_P (charset
)
2263 && (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2264 == CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
))
2266 /* We have not yet decided where to designate CHARSET. */
2267 int reg_bits
= default_reg_bits
;
2269 if (CHARSET_CHARS (charset
) == 96)
2270 /* A charset of CHARS96 can't be designated to REG 0. */
2274 /* There exist some default graphic register. */
2275 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2277 ? 0 : (reg_bits
& 2 ? 1 : (reg_bits
& 4 ? 2 : 3)));
2279 /* We anyway have to designate CHARSET to somewhere. */
2280 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2281 = (CHARSET_CHARS (charset
) == 94
2283 : ((coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
2284 || ! coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
)
2286 : (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
2290 coding
->require_flushing
= 1;
2294 coding
->type
= coding_type_big5
;
2296 = (NILP (XVECTOR (coding_system
)->contents
[4])
2297 ? CODING_FLAG_BIG5_HKU
2298 : CODING_FLAG_BIG5_ETEN
);
2302 coding
->type
= coding_type_ccl
;
2304 Lisp_Object val
= XVECTOR (coding_system
)->contents
[4];
2306 && VECTORP (XCONS (val
)->car
)
2307 && VECTORP (XCONS (val
)->cdr
))
2309 setup_ccl_program (&(coding
->spec
.ccl
.decoder
), XCONS (val
)->car
);
2310 setup_ccl_program (&(coding
->spec
.ccl
.encoder
), XCONS (val
)->cdr
);
2313 goto label_invalid_coding_system
;
2315 coding
->require_flushing
= 1;
2320 coding
->type
= coding_type_undecided
;
2322 coding
->type
= coding_type_no_conversion
;
2327 label_invalid_coding_system
:
2328 coding
->type
= coding_type_no_conversion
;
2329 coding
->eol_type
= CODING_EOL_LF
;
2330 coding
->symbol
= coding
->pre_write_conversion
= coding
->post_read_conversion
2335 /* Emacs has a mechanism to automatically detect a coding system if it
2336 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
2337 it's impossible to distinguish some coding systems accurately
2338 because they use the same range of codes. So, at first, coding
2339 systems are categorized into 7, those are:
2341 o coding-category-emacs-mule
2343 The category for a coding system which has the same code range
2344 as Emacs' internal format. Assigned the coding-system (Lisp
2345 symbol) `emacs-mule' by default.
2347 o coding-category-sjis
2349 The category for a coding system which has the same code range
2350 as SJIS. Assigned the coding-system (Lisp
2351 symbol) `shift-jis' by default.
2353 o coding-category-iso-7
2355 The category for a coding system which has the same code range
2356 as ISO2022 of 7-bit environment. Assigned the coding-system
2357 (Lisp symbol) `iso-2022-7' by default.
2359 o coding-category-iso-8-1
2361 The category for a coding system which has the same code range
2362 as ISO2022 of 8-bit environment and graphic plane 1 used only
2363 for DIMENSION1 charset. Assigned the coding-system (Lisp
2364 symbol) `iso-8859-1' by default.
2366 o coding-category-iso-8-2
2368 The category for a coding system which has the same code range
2369 as ISO2022 of 8-bit environment and graphic plane 1 used only
2370 for DIMENSION2 charset. Assigned the coding-system (Lisp
2371 symbol) `euc-japan' by default.
2373 o coding-category-iso-else
2375 The category for a coding system which has the same code range
2376 as ISO2022 but not belongs to any of the above three
2377 categories. Assigned the coding-system (Lisp symbol)
2378 `iso-2022-ss2-7' by default.
2380 o coding-category-big5
2382 The category for a coding system which has the same code range
2383 as BIG5. Assigned the coding-system (Lisp symbol)
2384 `cn-big5' by default.
2386 o coding-category-binary
2388 The category for a coding system not categorized in any of the
2389 above. Assigned the coding-system (Lisp symbol)
2390 `no-conversion' by default.
2392 Each of them is a Lisp symbol and the value is an actual
2393 `coding-system's (this is also a Lisp symbol) assigned by a user.
2394 What Emacs does actually is to detect a category of coding system.
2395 Then, it uses a `coding-system' assigned to it. If Emacs can't
2396 decide only one possible category, it selects a category of the
2397 highest priority. Priorities of categories are also specified by a
2398 user in a Lisp variable `coding-category-list'.
2402 /* Detect how a text of length SRC_BYTES pointed by SRC is encoded.
2403 If it detects possible coding systems, return an integer in which
2404 appropriate flag bits are set. Flag bits are defined by macros
2405 CODING_CATEGORY_MASK_XXX in `coding.h'. */
2408 detect_coding_mask (src
, src_bytes
)
2412 register unsigned char c
;
2413 unsigned char *src_end
= src
+ src_bytes
;
2416 /* At first, skip all ASCII characters and control characters except
2417 for three ISO2022 specific control characters. */
2418 label_loop_detect_coding
:
2419 while (src
< src_end
)
2423 || (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
))
2429 /* We found nothing other than ASCII. There's nothing to do. */
2430 return CODING_CATEGORY_MASK_ANY
;
2432 /* The text seems to be encoded in some multilingual coding system.
2433 Now, try to find in which coding system the text is encoded. */
2436 /* i.e. (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) */
2437 /* C is an ISO2022 specific control code of C0. */
2438 mask
= detect_coding_iso2022 (src
, src_end
);
2440 if (mask
== CODING_CATEGORY_MASK_ANY
)
2441 /* No valid ISO2022 code follows C. Try again. */
2442 goto label_loop_detect_coding
;
2444 else if (c
== ISO_CODE_SS2
|| c
== ISO_CODE_SS3
|| c
== ISO_CODE_CSI
)
2445 /* C is an ISO2022 specific control code of C1,
2446 or the first byte of SJIS's 2-byte character code,
2447 or a leading code of Emacs. */
2448 mask
= (detect_coding_iso2022 (src
, src_end
)
2449 | detect_coding_sjis (src
, src_end
)
2450 | detect_coding_emacs_mule (src
, src_end
));
2453 /* C is the first byte of SJIS character code,
2454 or a leading-code of Emacs. */
2455 mask
= (detect_coding_sjis (src
, src_end
)
2456 | detect_coding_emacs_mule (src
, src_end
));
2459 /* C is a character of ISO2022 in graphic plane right,
2460 or a SJIS's 1-byte character code (i.e. JISX0201),
2461 or the first byte of BIG5's 2-byte code. */
2462 mask
= (detect_coding_iso2022 (src
, src_end
)
2463 | detect_coding_sjis (src
, src_end
)
2464 | detect_coding_big5 (src
, src_end
));
2469 /* Detect how a text of length SRC_BYTES pointed by SRC is encoded.
2470 The information of the detected coding system is set in CODING. */
2473 detect_coding (coding
, src
, src_bytes
)
2474 struct coding_system
*coding
;
2478 int mask
= detect_coding_mask (src
, src_bytes
);
2481 if (mask
== CODING_CATEGORY_MASK_ANY
)
2482 /* We found nothing other than ASCII. There's nothing to do. */
2486 /* The source text seems to be encoded in unknown coding system.
2487 Emacs regards the category of such a kind of coding system as
2488 `coding-category-binary'. We assume that a user has assigned
2489 an appropriate coding system for a `coding-category-binary'. */
2490 idx
= CODING_CATEGORY_IDX_BINARY
;
2493 /* We found some plausible coding systems. Let's use a coding
2494 system of the highest priority. */
2495 Lisp_Object val
= Vcoding_category_list
;
2500 idx
= XFASTINT (Fget (XCONS (val
)->car
, Qcoding_category_index
));
2501 if ((idx
< CODING_CATEGORY_IDX_MAX
) && (mask
& (1 << idx
)))
2503 val
= XCONS (val
)->cdr
;
2510 /* For unknown reason, `Vcoding_category_list' contains none
2511 of found categories. Let's use any of them. */
2512 for (idx
= 0; idx
< CODING_CATEGORY_IDX_MAX
; idx
++)
2513 if (mask
& (1 << idx
))
2517 setup_coding_system (XSYMBOL (coding_category_table
[idx
])->value
, coding
);
2520 /* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC
2521 is encoded. Return one of CODING_EOL_LF, CODING_EOL_CRLF,
2522 CODING_EOL_CR, and CODING_EOL_UNDECIDED. */
2525 detect_eol_type (src
, src_bytes
)
2529 unsigned char *src_end
= src
+ src_bytes
;
2532 while (src
< src_end
)
2536 return CODING_EOL_LF
;
2539 if (src
< src_end
&& *src
== '\n')
2540 return CODING_EOL_CRLF
;
2542 return CODING_EOL_CR
;
2545 return CODING_EOL_UNDECIDED
;
2548 /* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC
2549 is encoded. If it detects an appropriate format of end-of-line, it
2550 sets the information in *CODING. */
2553 detect_eol (coding
, src
, src_bytes
)
2554 struct coding_system
*coding
;
2559 int eol_type
= detect_eol_type (src
, src_bytes
);
2561 if (eol_type
== CODING_EOL_UNDECIDED
)
2562 /* We found no end-of-line in the source text. */
2565 val
= Fget (coding
->symbol
, Qeol_type
);
2566 if (VECTORP (val
) && XVECTOR (val
)->size
== 3)
2567 setup_coding_system (XVECTOR (val
)->contents
[eol_type
], coding
);
2570 /* See "GENERAL NOTES about `decode_coding_XXX ()' functions". Before
2571 decoding, it may detect coding system and format of end-of-line if
2572 those are not yet decided. */
2575 decode_coding (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
2576 struct coding_system
*coding
;
2577 unsigned char *source
, *destination
;
2578 int src_bytes
, dst_bytes
;
2589 if (coding
->type
== coding_type_undecided
)
2590 detect_coding (coding
, source
, src_bytes
);
2592 if (coding
->eol_type
== CODING_EOL_UNDECIDED
)
2593 detect_eol (coding
, source
, src_bytes
);
2595 coding
->carryover_size
= 0;
2596 switch (coding
->type
)
2598 case coding_type_no_conversion
:
2599 label_no_conversion
:
2600 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
2601 bcopy (source
, destination
, produced
);
2602 *consumed
= produced
;
2605 case coding_type_emacs_mule
:
2606 case coding_type_undecided
:
2607 if (coding
->eol_type
== CODING_EOL_LF
2608 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
2609 goto label_no_conversion
;
2610 produced
= decode_eol (coding
, source
, destination
,
2611 src_bytes
, dst_bytes
, consumed
);
2614 case coding_type_sjis
:
2615 produced
= decode_coding_sjis_big5 (coding
, source
, destination
,
2616 src_bytes
, dst_bytes
, consumed
,
2620 case coding_type_iso2022
:
2621 produced
= decode_coding_iso2022 (coding
, source
, destination
,
2622 src_bytes
, dst_bytes
, consumed
);
2625 case coding_type_big5
:
2626 produced
= decode_coding_sjis_big5 (coding
, source
, destination
,
2627 src_bytes
, dst_bytes
, consumed
,
2631 case coding_type_ccl
:
2632 produced
= ccl_driver (&coding
->spec
.ccl
.decoder
, source
, destination
,
2633 src_bytes
, dst_bytes
, consumed
);
2640 /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". */
2643 encode_coding (coding
, source
, destination
, src_bytes
, dst_bytes
, consumed
)
2644 struct coding_system
*coding
;
2645 unsigned char *source
, *destination
;
2646 int src_bytes
, dst_bytes
;
2651 coding
->carryover_size
= 0;
2652 switch (coding
->type
)
2654 case coding_type_no_conversion
:
2655 label_no_conversion
:
2656 produced
= (src_bytes
> dst_bytes
) ? dst_bytes
: src_bytes
;
2659 bcopy (source
, destination
, produced
);
2660 if (coding
->selective
)
2662 unsigned char *p
= destination
, *pend
= destination
+ produced
;
2664 if (*p
++ == '\015') p
[-1] = '\n';
2667 *consumed
= produced
;
2670 case coding_type_emacs_mule
:
2671 case coding_type_undecided
:
2672 if (coding
->eol_type
== CODING_EOL_LF
2673 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
2674 goto label_no_conversion
;
2675 produced
= encode_eol (coding
, source
, destination
,
2676 src_bytes
, dst_bytes
, consumed
);
2679 case coding_type_sjis
:
2680 produced
= encode_coding_sjis_big5 (coding
, source
, destination
,
2681 src_bytes
, dst_bytes
, consumed
,
2685 case coding_type_iso2022
:
2686 produced
= encode_coding_iso2022 (coding
, source
, destination
,
2687 src_bytes
, dst_bytes
, consumed
);
2690 case coding_type_big5
:
2691 produced
= encode_coding_sjis_big5 (coding
, source
, destination
,
2692 src_bytes
, dst_bytes
, consumed
,
2696 case coding_type_ccl
:
2697 produced
= ccl_driver (&coding
->spec
.ccl
.encoder
, source
, destination
,
2698 src_bytes
, dst_bytes
, consumed
);
2705 #define CONVERSION_BUFFER_EXTRA_ROOM 256
2707 /* Return maximum size (bytes) of a buffer enough for decoding
2708 SRC_BYTES of text encoded in CODING. */
2711 decoding_buffer_size (coding
, src_bytes
)
2712 struct coding_system
*coding
;
2717 if (coding
->type
== coding_type_iso2022
)
2719 else if (coding
->type
== coding_type_ccl
)
2720 magnification
= coding
->spec
.ccl
.decoder
.buf_magnification
;
2724 return (src_bytes
* magnification
+ CONVERSION_BUFFER_EXTRA_ROOM
);
2727 /* Return maximum size (bytes) of a buffer enough for encoding
2728 SRC_BYTES of text to CODING. */
2731 encoding_buffer_size (coding
, src_bytes
)
2732 struct coding_system
*coding
;
2737 if (coding
->type
== coding_type_ccl
)
2738 magnification
= coding
->spec
.ccl
.encoder
.buf_magnification
;
2742 return (src_bytes
* magnification
+ CONVERSION_BUFFER_EXTRA_ROOM
);
2745 #ifndef MINIMUM_CONVERSION_BUFFER_SIZE
2746 #define MINIMUM_CONVERSION_BUFFER_SIZE 1024
2749 char *conversion_buffer
;
2750 int conversion_buffer_size
;
2752 /* Return a pointer to a SIZE bytes of buffer to be used for encoding
2753 or decoding. Sufficient memory is allocated automatically. If we
2754 run out of memory, return NULL. */
2757 get_conversion_buffer (size
)
2760 if (size
> conversion_buffer_size
)
2763 int real_size
= conversion_buffer_size
* 2;
2765 while (real_size
< size
) real_size
*= 2;
2766 buf
= (char *) xmalloc (real_size
);
2767 xfree (conversion_buffer
);
2768 conversion_buffer
= buf
;
2769 conversion_buffer_size
= real_size
;
2771 return conversion_buffer
;
2776 /*** 7. Emacs Lisp library functions ***/
2778 DEFUN ("coding-system-spec", Fcoding_system_spec
, Scoding_system_spec
,
2780 "Return coding-spec of CODING-SYSTEM.\n\
2781 If CODING-SYSTEM is not a valid coding-system, return nil.")
2785 while (SYMBOLP (obj
) && !NILP (obj
))
2786 obj
= Fget (obj
, Qcoding_system
);
2787 return ((NILP (obj
) || !VECTORP (obj
) || XVECTOR (obj
)->size
!= 5)
2791 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
2792 "Return t if OBJECT is nil or a coding-system.\n\
2793 See document of make-coding-system for coding-system object.")
2797 return ((NILP (obj
) || !NILP (Fcoding_system_spec (obj
))) ? Qt
: Qnil
);
2800 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
2801 Sread_non_nil_coding_system
, 1, 1, 0,
2802 "Read a coding system from the minibuffer, prompting with string PROMPT.")
2809 val
= Fcompleting_read (prompt
, Vobarray
, Qcoding_system_spec
,
2810 Qt
, Qnil
, Qnil
, Qnil
);
2812 while (XSTRING (val
)->size
== 0);
2813 return (Fintern (val
, Qnil
));
2816 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 1, 0,
2817 "Read a coding system or nil from the minibuffer, prompting with string PROMPT.")
2821 Lisp_Object val
= Fcompleting_read (prompt
, Vobarray
, Qcoding_system_p
,
2822 Qt
, Qnil
, Qnil
, Qnil
);
2823 return (XSTRING (val
)->size
== 0 ? Qnil
: Fintern (val
, Qnil
));
2826 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
2828 "Check validity of CODING-SYSTEM.\n\
2829 If valid, return CODING-SYSTEM, else `coding-system-error' is signaled.\n\
2830 CODING-SYSTEM is valid if it is a symbol and has \"coding-system\" property.\n\
2831 The value of property should be a vector of length 5.")
2833 Lisp_Object coding_system
;
2835 CHECK_SYMBOL (coding_system
, 0);
2836 if (!NILP (Fcoding_system_p (coding_system
)))
2837 return coding_system
;
2839 Fsignal (Qcoding_system_error
, Fcons (coding_system
, Qnil
));
2842 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
2844 "Detect coding-system of the text in the region between START and END.\n\
2845 Return a list of possible coding-systems ordered by priority.\n\
2846 If only ASCII characters are found, it returns `undecided'\n\
2847 or its subsidiary coding-system according to a detected end-of-line format.")
2851 int coding_mask
, eol_type
;
2855 validate_region (&b
, &e
);
2856 beg
= XINT (b
), end
= XINT (e
);
2857 if (beg
< GPT
&& end
>= GPT
) move_gap (end
);
2859 coding_mask
= detect_coding_mask (POS_ADDR (beg
), end
- beg
);
2860 eol_type
= detect_eol_type (POS_ADDR (beg
), end
- beg
);
2862 if (coding_mask
== CODING_CATEGORY_MASK_ANY
)
2864 val
= intern ("undecided");
2865 if (eol_type
!= CODING_EOL_UNDECIDED
)
2867 Lisp_Object val2
= Fget (val
, Qeol_type
);
2869 val
= XVECTOR (val2
)->contents
[eol_type
];
2876 /* At first, gather possible coding-systems in VAL in a reverse
2879 for (val2
= Vcoding_category_list
;
2881 val2
= XCONS (val2
)->cdr
)
2884 = XFASTINT (Fget (XCONS (val2
)->car
, Qcoding_category_index
));
2885 if (coding_mask
& (1 << idx
))
2886 val
= Fcons (Fsymbol_value (XCONS (val2
)->car
), val
);
2889 /* Then, change the order of the list, while getting subsidiary
2893 for (; !NILP (val2
); val2
= XCONS (val2
)->cdr
)
2895 if (eol_type
== CODING_EOL_UNDECIDED
)
2896 val
= Fcons (XCONS (val2
)->car
, val
);
2899 Lisp_Object val3
= Fget (XCONS (val2
)->car
, Qeol_type
);
2901 val
= Fcons (XVECTOR (val3
)->contents
[eol_type
], val
);
2903 val
= Fcons (XCONS (val2
)->car
, val
);
2911 /* Scan text in the region between *BEGP and *ENDP, skip characters
2912 which we never have to encode to (iff ENCODEP is 1) or decode from
2913 coding system CODING at the head and tail, then set BEGP and ENDP
2914 to the addresses of start and end of the text we actually convert. */
2917 shrink_conversion_area (begp
, endp
, coding
, encodep
)
2918 unsigned char **begp
, **endp
;
2919 struct coding_system
*coding
;
2922 register unsigned char *beg_addr
= *begp
, *end_addr
= *endp
;
2924 if (coding
->eol_type
!= CODING_EOL_LF
2925 && coding
->eol_type
!= CODING_EOL_UNDECIDED
)
2926 /* Since we anyway have to convert end-of-line format, it is not
2927 worth skipping at most 100 bytes or so. */
2930 if (encodep
) /* for encoding */
2932 switch (coding
->type
)
2934 case coding_type_no_conversion
:
2935 case coding_type_emacs_mule
:
2936 case coding_type_undecided
:
2937 /* We need no conversion. */
2940 case coding_type_ccl
:
2941 /* We can't skip any data. */
2943 case coding_type_iso2022
:
2944 if (coding
->flags
& CODING_FLAG_ISO_DESIGNATE_AT_BOL
)
2946 unsigned char *bol
= beg_addr
;
2947 while (beg_addr
< end_addr
&& *beg_addr
< 0x80)
2950 if (*(beg_addr
- 1) == '\n')
2954 goto label_skip_tail
;
2958 /* We can skip all ASCII characters at the head and tail. */
2959 while (beg_addr
< end_addr
&& *beg_addr
< 0x80) beg_addr
++;
2961 while (beg_addr
< end_addr
&& *(end_addr
- 1) < 0x80) end_addr
--;
2965 else /* for decoding */
2967 switch (coding
->type
)
2969 case coding_type_no_conversion
:
2970 /* We need no conversion. */
2973 case coding_type_emacs_mule
:
2974 if (coding
->eol_type
== CODING_EOL_LF
)
2976 /* We need no conversion. */
2980 /* We can skip all but carriage-return. */
2981 while (beg_addr
< end_addr
&& *beg_addr
!= '\r') beg_addr
++;
2982 while (beg_addr
< end_addr
&& *(end_addr
- 1) != '\r') end_addr
--;
2984 case coding_type_sjis
:
2985 case coding_type_big5
:
2986 /* We can skip all ASCII characters at the head. */
2987 while (beg_addr
< end_addr
&& *beg_addr
< 0x80) beg_addr
++;
2988 /* We can skip all ASCII characters at the tail except for
2989 the second byte of SJIS or BIG5 code. */
2990 while (beg_addr
< end_addr
&& *(end_addr
- 1) < 0x80) end_addr
--;
2991 if (end_addr
!= *endp
)
2994 case coding_type_ccl
:
2995 /* We can't skip any data. */
2997 default: /* i.e. case coding_type_iso2022: */
3001 /* We can skip all ASCII characters except for a few
3002 control codes at the head. */
3003 while (beg_addr
< end_addr
&& (c
= *beg_addr
) < 0x80
3004 && c
!= ISO_CODE_CR
&& c
!= ISO_CODE_SO
3005 && c
!= ISO_CODE_SI
&& c
!= ISO_CODE_ESC
)
3016 /* Encode to (iff ENCODEP is 1) or decode form coding system CODING a
3017 text between B and E. B and E are buffer position. */
3020 code_convert_region (b
, e
, coding
, encodep
)
3022 struct coding_system
*coding
;
3025 int beg
, end
, len
, consumed
, produced
;
3027 unsigned char *begp
, *endp
;
3030 validate_region (&b
, &e
);
3031 beg
= XINT (b
), end
= XINT (e
);
3032 if (beg
< GPT
&& end
>= GPT
)
3035 if (encodep
&& !NILP (coding
->pre_write_conversion
))
3037 /* We must call a pre-conversion function which may put a new
3038 text to be converted in a new buffer. */
3039 struct buffer
*old
= current_buffer
, *new;
3042 call2 (coding
->pre_write_conversion
, b
, e
);
3043 if (old
!= current_buffer
)
3045 /* Replace the original text by the text just generated. */
3047 new = current_buffer
;
3048 set_buffer_internal (old
);
3049 del_range (beg
, end
);
3050 insert_from_buffer (new, 1, len
, 0);
3055 /* We may be able to shrink the conversion region. */
3056 begp
= POS_ADDR (beg
); endp
= begp
+ (end
- beg
);
3057 shrink_conversion_area (&begp
, &endp
, coding
, encodep
);
3060 /* We need no conversion. */
3064 beg
+= begp
- POS_ADDR (beg
);
3065 end
= beg
+ (endp
- begp
);
3068 len
= encoding_buffer_size (coding
, end
- beg
);
3070 len
= decoding_buffer_size (coding
, end
- beg
);
3071 buf
= get_conversion_buffer (len
);
3073 coding
->last_block
= 1;
3075 ? encode_coding (coding
, POS_ADDR (beg
), buf
, end
- beg
, len
,
3077 : decode_coding (coding
, POS_ADDR (beg
), buf
, end
- beg
, len
,
3080 len
= produced
+ (beg
- XINT (b
)) + (XINT (e
) - end
);
3083 insert (buf
, produced
);
3084 del_range (PT
, PT
+ end
- beg
);
3086 pos
= PT
+ (pos
- end
);
3092 if (!encodep
&& !NILP (coding
->post_read_conversion
))
3094 /* We must call a post-conversion function which may alter
3095 the text just converted. */
3100 insval
= call1 (coding
->post_read_conversion
, make_number (len
));
3101 CHECK_NUMBER (insval
, 0);
3102 len
= XINT (insval
);
3105 return make_number (len
);
3109 code_convert_string (str
, coding
, encodep
, nocopy
)
3110 Lisp_Object str
, nocopy
;
3111 struct coding_system
*coding
;
3114 int len
, consumed
, produced
;
3116 unsigned char *begp
, *endp
;
3117 int head_skip
, tail_skip
;
3118 struct gcpro gcpro1
;
3120 if (encodep
&& !NILP (coding
->pre_write_conversion
)
3121 || !encodep
&& !NILP (coding
->post_read_conversion
))
3123 /* Since we have to call Lisp functions which assume target text
3124 is in a buffer, after setting a temporary buffer, call
3125 code_convert_region. */
3126 int count
= specpdl_ptr
- specpdl
;
3127 int len
= XSTRING (str
)->size
;
3129 struct buffer
*old
= current_buffer
;
3131 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
3132 temp_output_buffer_setup (" *code-converting-work*");
3133 set_buffer_internal (XBUFFER (Vstandard_output
));
3134 insert_from_string (str
, 0, len
, 0);
3135 code_convert_region (make_number (BEGV
), make_number (ZV
),
3137 result
= make_buffer_string (BEGV
, ZV
, 0);
3138 set_buffer_internal (old
);
3139 return unbind_to (count
, result
);
3142 /* We may be able to shrink the conversion region. */
3143 begp
= XSTRING (str
)->data
;
3144 endp
= begp
+ XSTRING (str
)->size
;
3145 shrink_conversion_area (&begp
, &endp
, coding
, encodep
);
3148 /* We need no conversion. */
3149 return (NILP (nocopy
) ? Fcopy_sequence (str
) : str
);
3151 head_skip
= begp
- XSTRING (str
)->data
;
3152 tail_skip
= XSTRING (str
)->size
- head_skip
- (endp
- begp
);
3157 len
= encoding_buffer_size (coding
, endp
- begp
);
3159 len
= decoding_buffer_size (coding
, endp
- begp
);
3160 buf
= get_conversion_buffer (len
+ head_skip
+ tail_skip
);
3162 bcopy (XSTRING (str
)->data
, buf
, head_skip
);
3163 coding
->last_block
= 1;
3165 ? encode_coding (coding
, XSTRING (str
)->data
+ head_skip
,
3166 buf
+ head_skip
, endp
- begp
, len
, &consumed
)
3167 : decode_coding (coding
, XSTRING (str
)->data
+ head_skip
,
3168 buf
+ head_skip
, endp
- begp
, len
, &consumed
));
3169 bcopy (XSTRING (str
)->data
+ head_skip
+ (endp
- begp
),
3170 buf
+ head_skip
+ produced
,
3175 return make_string (buf
, head_skip
+ produced
+ tail_skip
);
3178 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
3179 3, 3, "r\nzCoding system: ",
3180 "Decode current region by specified coding system.\n\
3181 When called from a program, takes three arguments:\n\
3182 START, END, and CODING-SYSTEM. START END are buffer positions.\n\
3183 Return length of decoded text.")
3184 (b
, e
, coding_system
)
3185 Lisp_Object b
, e
, coding_system
;
3187 struct coding_system coding
;
3189 CHECK_NUMBER_COERCE_MARKER (b
, 0);
3190 CHECK_NUMBER_COERCE_MARKER (e
, 1);
3191 CHECK_SYMBOL (coding_system
, 2);
3193 if (NILP (coding_system
))
3194 return make_number (XFASTINT (e
) - XFASTINT (b
));
3195 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
3196 error ("Invalid coding-system: %s", XSYMBOL (coding_system
)->name
->data
);
3198 return code_convert_region (b
, e
, &coding
, 0);
3201 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
3202 3, 3, "r\nzCoding system: ",
3203 "Encode current region by specified coding system.\n\
3204 When called from a program, takes three arguments:\n\
3205 START, END, and CODING-SYSTEM. START END are buffer positions.\n\
3206 Return length of encoded text.")
3207 (b
, e
, coding_system
)
3208 Lisp_Object b
, e
, coding_system
;
3210 struct coding_system coding
;
3212 CHECK_NUMBER_COERCE_MARKER (b
, 0);
3213 CHECK_NUMBER_COERCE_MARKER (e
, 1);
3214 CHECK_SYMBOL (coding_system
, 2);
3216 if (NILP (coding_system
))
3217 return make_number (XFASTINT (e
) - XFASTINT (b
));
3218 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
3219 error ("Invalid coding-system: %s", XSYMBOL (coding_system
)->name
->data
);
3221 return code_convert_region (b
, e
, &coding
, 1);
3224 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
3226 "Decode STRING which is encoded in CODING-SYSTEM, and return the result.\n\
3227 Optional arg NOCOPY non-nil means return STRING itself if there's no need\n\
3229 (string
, coding_system
, nocopy
)
3230 Lisp_Object string
, coding_system
, nocopy
;
3232 struct coding_system coding
;
3234 CHECK_STRING (string
, 0);
3235 CHECK_SYMBOL (coding_system
, 1);
3237 if (NILP (coding_system
))
3238 return (NILP (nocopy
) ? Fcopy_sequence (string
) : string
);
3239 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
3240 error ("Invalid coding-system: %s", XSYMBOL (coding_system
)->name
->data
);
3242 return code_convert_string (string
, &coding
, 0, nocopy
);
3245 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
3247 "Encode STRING to CODING-SYSTEM, and return the result.\n\
3248 Optional arg NOCOPY non-nil means return STRING itself if there's no need\n\
3250 (string
, coding_system
, nocopy
)
3251 Lisp_Object string
, coding_system
, nocopy
;
3253 struct coding_system coding
;
3255 CHECK_STRING (string
, 0);
3256 CHECK_SYMBOL (coding_system
, 1);
3258 if (NILP (coding_system
))
3259 return (NILP (nocopy
) ? Fcopy_sequence (string
) : string
);
3260 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
3261 error ("Invalid coding-system: %s", XSYMBOL (coding_system
)->name
->data
);
3263 return code_convert_string (string
, &coding
, 1, nocopy
);
3266 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
3267 "Decode a JISX0208 character of shift-jis encoding.\n\
3268 CODE is the character code in SJIS.\n\
3269 Return the corresponding character.")
3273 unsigned char c1
, c2
, s1
, s2
;
3276 CHECK_NUMBER (code
, 0);
3277 s1
= (XFASTINT (code
)) >> 8, s2
= (XFASTINT (code
)) & 0xFF;
3278 DECODE_SJIS (s1
, s2
, c1
, c2
);
3279 XSETFASTINT (val
, MAKE_NON_ASCII_CHAR (charset_jisx0208
, c1
, c2
));
3283 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
3284 "Encode a JISX0208 character CHAR to SJIS coding-system.\n\
3285 Return the corresponding character code in SJIS.")
3289 int charset
, c1
, c2
, s1
, s2
;
3292 CHECK_NUMBER (ch
, 0);
3293 SPLIT_CHAR (XFASTINT (ch
), charset
, c1
, c2
);
3294 if (charset
== charset_jisx0208
)
3296 ENCODE_SJIS (c1
, c2
, s1
, s2
);
3297 XSETFASTINT (val
, (s1
<< 8) | s2
);
3300 XSETFASTINT (val
, 0);
3304 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
3305 "Decode a Big5 character CODE of BIG5 coding-system.\n\
3306 CODE is the character code in BIG5.\n\
3307 Return the corresponding character.")
3312 unsigned char b1
, b2
, c1
, c2
;
3315 CHECK_NUMBER (code
, 0);
3316 b1
= (XFASTINT (code
)) >> 8, b2
= (XFASTINT (code
)) & 0xFF;
3317 DECODE_BIG5 (b1
, b2
, charset
, c1
, c2
);
3318 XSETFASTINT (val
, MAKE_NON_ASCII_CHAR (charset
, c1
, c2
));
3322 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
3323 "Encode the Big5 character CHAR to BIG5 coding-system.\n\
3324 Return the corresponding character code in Big5.")
3328 int charset
, c1
, c2
, b1
, b2
;
3331 CHECK_NUMBER (ch
, 0);
3332 SPLIT_CHAR (XFASTINT (ch
), charset
, c1
, c2
);
3333 if (charset
== charset_big5_1
|| charset
== charset_big5_2
)
3335 ENCODE_BIG5 (charset
, c1
, c2
, b1
, b2
);
3336 XSETFASTINT (val
, (b1
<< 8) | b2
);
3339 XSETFASTINT (val
, 0);
3343 DEFUN ("set-terminal-coding-system-internal",
3344 Fset_terminal_coding_system_internal
,
3345 Sset_terminal_coding_system_internal
, 1, 1, 0, "")
3347 Lisp_Object coding_system
;
3349 CHECK_SYMBOL (coding_system
, 0);
3350 setup_coding_system (Fcheck_coding_system (coding_system
), &terminal_coding
);
3354 DEFUN ("terminal-coding-system",
3355 Fterminal_coding_system
, Sterminal_coding_system
, 0, 0, 0,
3356 "Return coding-system of your terminal.")
3359 return terminal_coding
.symbol
;
3362 DEFUN ("set-keyboard-coding-system-internal",
3363 Fset_keyboard_coding_system_internal
,
3364 Sset_keyboard_coding_system_internal
, 1, 1, 0, "")
3366 Lisp_Object coding_system
;
3368 CHECK_SYMBOL (coding_system
, 0);
3369 setup_coding_system (Fcheck_coding_system (coding_system
), &keyboard_coding
);
3373 DEFUN ("keyboard-coding-system",
3374 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 0, 0,
3375 "Return coding-system of what is sent from terminal keyboard.")
3378 return keyboard_coding
.symbol
;
3382 DEFUN ("find-coding-system", Ffind_coding_system
, Sfind_coding_system
,
3384 "Choose a coding system for a file operation based on file name.\n\
3385 The value names a pair of coding systems: (ENCODING-SYSTEM DECODING-SYSTEM).\n\
3386 ENCODING-SYSTEM is the coding system to use for encoding\n\
3387 \(in case OPERATION does encoding), and DECODING-SYSTEM is the coding system\n\
3388 for decoding (in case OPERATION does decoding).\n\
3390 The first argument OPERATION specifies an I/O primitive:\n\
3391 For file I/O, `insert-file-contents' or `write-region'.\n\
3392 For process I/O, `call-process', `call-process-region', or `start-process'.\n\
3393 For network I/O, `open-network-stream'.\n\
3395 The remaining arguments should be the same arguments that were passed\n\
3396 to the primitive. Depending on which primitive, one of those arguments\n\
3397 is selected as the TARGET. For example, if OPERATION does file I/O,\n\
3398 whichever argument specifies the file name is TARGET.\n\
3400 TARGET has a meaning which depends on OPERATION:\n\
3401 For file I/O, TARGET is a file name.\n\
3402 For process I/O, TARGET is a process name.\n\
3403 For network I/O, TARGET is a service name or a port number\n\
3405 This function looks up what specified for TARGET in,\n\
3406 `file-coding-system-alist', `process-coding-system-alist',\n\
3407 or `network-coding-system-alist' depending on OPERATION.\n\
3408 They may specify a coding system, a cons of coding systems,\n\
3409 or a function symbol to call.\n\
3410 In the last case, we call the function with one argument,\n\
3411 which is a list of all the arguments given to `find-coding-system'.")
3416 Lisp_Object operation
, target_idx
, target
, val
;
3417 register Lisp_Object chain
;
3420 error ("Too few arguments");
3421 operation
= args
[0];
3422 if (!SYMBOLP (operation
)
3423 || !INTEGERP (target_idx
= Fget (operation
, Qtarget_idx
)))
3424 error ("Invalid first arguement");
3425 if (nargs
< 1 + XINT (target_idx
))
3426 error ("Too few arguments for operation: %s",
3427 XSYMBOL (operation
)->name
->data
);
3428 target
= args
[XINT (target_idx
) + 1];
3429 if (!(STRINGP (target
)
3430 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
3431 error ("Invalid %dth argument", XINT (target_idx
) + 1);
3433 chain
= (operation
== Qinsert_file_contents
|| operation
== Qwrite_region
3434 ? Vfile_coding_system_alist
3435 : (operation
== Qopen_network_stream
3436 ? Vnetwork_coding_system_alist
3437 : Vprocess_coding_system_alist
));
3441 for (; CONSP (chain
); chain
= XCONS (chain
)->cdr
)
3443 Lisp_Object elt
= XCONS (chain
)->car
;
3446 && ((STRINGP (target
)
3447 && STRINGP (XCONS (elt
)->car
)
3448 && fast_string_match (XCONS (elt
)->car
, target
) >= 0)
3449 || (INTEGERP (target
) && EQ (target
, XCONS (elt
)->car
))))
3451 val
= XCONS (elt
)->cdr
;
3454 if (! SYMBOLP (val
))
3456 if (! NILP (Fcoding_system_p (val
)))
3457 return Fcons (val
, val
);
3458 if (!NILP (Fboundp (val
)))
3459 return call2 (val
, Flist (nargs
, args
));
3469 /*** 8. Post-amble ***/
3475 /* Emacs' internal format specific initialize routine. */
3476 for (i
= 0; i
<= 0x20; i
++)
3477 emacs_code_class
[i
] = EMACS_control_code
;
3478 emacs_code_class
[0x0A] = EMACS_linefeed_code
;
3479 emacs_code_class
[0x0D] = EMACS_carriage_return_code
;
3480 for (i
= 0x21 ; i
< 0x7F; i
++)
3481 emacs_code_class
[i
] = EMACS_ascii_code
;
3482 emacs_code_class
[0x7F] = EMACS_control_code
;
3483 emacs_code_class
[0x80] = EMACS_leading_code_composition
;
3484 for (i
= 0x81; i
< 0xFF; i
++)
3485 emacs_code_class
[i
] = EMACS_invalid_code
;
3486 emacs_code_class
[LEADING_CODE_PRIVATE_11
] = EMACS_leading_code_3
;
3487 emacs_code_class
[LEADING_CODE_PRIVATE_12
] = EMACS_leading_code_3
;
3488 emacs_code_class
[LEADING_CODE_PRIVATE_21
] = EMACS_leading_code_4
;
3489 emacs_code_class
[LEADING_CODE_PRIVATE_22
] = EMACS_leading_code_4
;
3491 /* ISO2022 specific initialize routine. */
3492 for (i
= 0; i
< 0x20; i
++)
3493 iso_code_class
[i
] = ISO_control_code
;
3494 for (i
= 0x21; i
< 0x7F; i
++)
3495 iso_code_class
[i
] = ISO_graphic_plane_0
;
3496 for (i
= 0x80; i
< 0xA0; i
++)
3497 iso_code_class
[i
] = ISO_control_code
;
3498 for (i
= 0xA1; i
< 0xFF; i
++)
3499 iso_code_class
[i
] = ISO_graphic_plane_1
;
3500 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
3501 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
3502 iso_code_class
[ISO_CODE_CR
] = ISO_carriage_return
;
3503 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
3504 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
3505 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
3506 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
3507 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
3508 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
3509 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
3511 conversion_buffer_size
= MINIMUM_CONVERSION_BUFFER_SIZE
;
3512 conversion_buffer
= (char *) xmalloc (MINIMUM_CONVERSION_BUFFER_SIZE
);
3514 setup_coding_system (Qnil
, &keyboard_coding
);
3515 setup_coding_system (Qnil
, &terminal_coding
);
3522 Qtarget_idx
= intern ("target-idx");
3523 staticpro (&Qtarget_idx
);
3525 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
3526 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
3528 Qcall_process
= intern ("call-process");
3529 staticpro (&Qcall_process
);
3530 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
3532 Qcall_process_region
= intern ("call-process-region");
3533 staticpro (&Qcall_process_region
);
3534 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
3536 Qstart_process
= intern ("start-process");
3537 staticpro (&Qstart_process
);
3538 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
3540 Qopen_network_stream
= intern ("open-network-stream");
3541 staticpro (&Qopen_network_stream
);
3542 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
3544 Qcoding_system
= intern ("coding-system");
3545 staticpro (&Qcoding_system
);
3547 Qeol_type
= intern ("eol-type");
3548 staticpro (&Qeol_type
);
3550 Qbuffer_file_coding_system
= intern ("buffer-file-coding-system");
3551 staticpro (&Qbuffer_file_coding_system
);
3553 Qpost_read_conversion
= intern ("post-read-conversion");
3554 staticpro (&Qpost_read_conversion
);
3556 Qpre_write_conversion
= intern ("pre-write-conversion");
3557 staticpro (&Qpre_write_conversion
);
3559 Qcoding_system_spec
= intern ("coding-system-spec");
3560 staticpro (&Qcoding_system_spec
);
3562 Qcoding_system_p
= intern ("coding-system-p");
3563 staticpro (&Qcoding_system_p
);
3565 Qcoding_system_error
= intern ("coding-system-error");
3566 staticpro (&Qcoding_system_error
);
3568 Fput (Qcoding_system_error
, Qerror_conditions
,
3569 Fcons (Qcoding_system_error
, Fcons (Qerror
, Qnil
)));
3570 Fput (Qcoding_system_error
, Qerror_message
,
3571 build_string ("Coding-system error"));
3573 Qcoding_category_index
= intern ("coding-category-index");
3574 staticpro (&Qcoding_category_index
);
3578 for (i
= 0; i
< CODING_CATEGORY_IDX_MAX
; i
++)
3580 coding_category_table
[i
] = intern (coding_category_name
[i
]);
3581 staticpro (&coding_category_table
[i
]);
3582 Fput (coding_category_table
[i
], Qcoding_category_index
,
3587 Qcharacter_unification_table
= intern ("character-unification-table");
3588 staticpro (&Qcharacter_unification_table
);
3589 Fput (Qcharacter_unification_table
, Qchar_table_extra_slots
,
3592 defsubr (&Scoding_system_spec
);
3593 defsubr (&Scoding_system_p
);
3594 defsubr (&Sread_coding_system
);
3595 defsubr (&Sread_non_nil_coding_system
);
3596 defsubr (&Scheck_coding_system
);
3597 defsubr (&Sdetect_coding_region
);
3598 defsubr (&Sdecode_coding_region
);
3599 defsubr (&Sencode_coding_region
);
3600 defsubr (&Sdecode_coding_string
);
3601 defsubr (&Sencode_coding_string
);
3602 defsubr (&Sdecode_sjis_char
);
3603 defsubr (&Sencode_sjis_char
);
3604 defsubr (&Sdecode_big5_char
);
3605 defsubr (&Sencode_big5_char
);
3606 defsubr (&Sset_terminal_coding_system_internal
);
3607 defsubr (&Sterminal_coding_system
);
3608 defsubr (&Sset_keyboard_coding_system_internal
);
3609 defsubr (&Skeyboard_coding_system
);
3610 defsubr (&Sfind_coding_system
);
3612 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list
,
3613 "List of coding-categories (symbols) ordered by priority.");
3617 Vcoding_category_list
= Qnil
;
3618 for (i
= CODING_CATEGORY_IDX_MAX
- 1; i
>= 0; i
--)
3619 Vcoding_category_list
3620 = Fcons (coding_category_table
[i
], Vcoding_category_list
);
3623 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read
,
3624 "A variable of internal use only.\n\
3625 If the value is a coding system, it is used for decoding on read operation.\n\
3626 If not, an appropriate element in `coding-system-alist' (which see) is used.");
3627 Vcoding_system_for_read
= Qnil
;
3629 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write
,
3630 "A variable of internal use only.\n\
3631 If the value is a coding system, it is used for encoding on write operation.\n\
3632 If not, an appropriate element in `coding-system-alist' (which see) is used.");
3633 Vcoding_system_for_write
= Qnil
;
3635 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used
,
3636 "Coding-system used in the latest file or process I/O.");
3637 Vlast_coding_system_used
= Qnil
;
3639 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist
,
3640 "Alist to decide a coding system to use for a file I/O operation.\n\
3641 The format is ((PATTERN . VAL) ...),\n\
3642 where PATTERN is a regular expression matching a file name,\n\
3643 VAL is a coding system, a cons of coding systems, or a function symbol.\n\
3644 If VAL is a coding system, it is used for both decoding and encoding\n\
3645 the file contents.\n\
3646 If VAL is a cons of coding systems, the car part is used for decoding,\n\
3647 and the cdr part is used for encoding.\n\
3648 If VAL is a function symbol, the function must return a coding system\n\
3649 or a cons of coding systems which are used as above.\n\
3651 See also the function `find-coding-system'.");
3652 Vfile_coding_system_alist
= Qnil
;
3654 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist
,
3655 "Alist to decide a coding system to use for a process I/O operation.\n\
3656 The format is ((PATTERN . VAL) ...),\n\
3657 where PATTERN is a regular expression matching a program name,\n\
3658 VAL is a coding system, a cons of coding systems, or a function symbol.\n\
3659 If VAL is a coding system, it is used for both decoding what received\n\
3660 from the program and encoding what sent to the program.\n\
3661 If VAL is a cons of coding systems, the car part is used for decoding,\n\
3662 and the cdr part is used for encoding.\n\
3663 If VAL is a function symbol, the function must return a coding system\n\
3664 or a cons of coding systems which are used as above.\n\
3666 See also the function `find-coding-system'.");
3667 Vprocess_coding_system_alist
= Qnil
;
3669 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist
,
3670 "Alist to decide a coding system to use for a network I/O operation.\n\
3671 The format is ((PATTERN . VAL) ...),\n\
3672 where PATTERN is a regular expression matching a network service name\n\
3673 or is a port number to connect to,\n\
3674 VAL is a coding system, a cons of coding systems, or a function symbol.\n\
3675 If VAL is a coding system, it is used for both decoding what received\n\
3676 from the network stream and encoding what sent to the network stream.\n\
3677 If VAL is a cons of coding systems, the car part is used for decoding,\n\
3678 and the cdr part is used for encoding.\n\
3679 If VAL is a function symbol, the function must return a coding system\n\
3680 or a cons of coding systems which are used as above.\n\
3682 See also the function `find-coding-system'.");
3683 Vnetwork_coding_system_alist
= Qnil
;
3685 DEFVAR_INT ("eol-mnemonic-unix", &eol_mnemonic_unix
,
3686 "Mnemonic character indicating UNIX-like end-of-line format (i.e. LF) .");
3687 eol_mnemonic_unix
= '.';
3689 DEFVAR_INT ("eol-mnemonic-dos", &eol_mnemonic_dos
,
3690 "Mnemonic character indicating DOS-like end-of-line format (i.e. CRLF).");
3691 eol_mnemonic_dos
= ':';
3693 DEFVAR_INT ("eol-mnemonic-mac", &eol_mnemonic_mac
,
3694 "Mnemonic character indicating MAC-like end-of-line format (i.e. CR).");
3695 eol_mnemonic_mac
= '\'';
3697 DEFVAR_INT ("eol-mnemonic-undecided", &eol_mnemonic_undecided
,
3698 "Mnemonic character indicating end-of-line format is not yet decided.");
3699 eol_mnemonic_undecided
= '-';
3701 DEFVAR_LISP ("enable-character-unification", &Venable_character_unification
,
3702 "Non-nil means ISO 2022 encoder/decoder do character unification.");
3703 Venable_character_unification
= Qt
;
3705 DEFVAR_LISP ("standard-character-unification-table-for-read",
3706 &Vstandard_character_unification_table_for_read
,
3707 "Table for unifying characters when reading.");
3708 Vstandard_character_unification_table_for_read
= Qnil
;
3710 DEFVAR_LISP ("standard-character-unification-table-for-write",
3711 &Vstandard_character_unification_table_for_write
,
3712 "Table for unifying characters when writing.");
3713 Vstandard_character_unification_table_for_write
= Qnil
;
3715 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_alist
,
3716 "Alist of charsets vs revision numbers.\n\
3717 While encoding, if a charset (car part of an element) is found,\n\
3718 designate it with the escape sequence identifing revision (cdr part of the element).");
3719 Vcharset_revision_alist
= Qnil
;
3721 DEFVAR_LISP ("default-process-coding-system",
3722 &Vdefault_process_coding_system
,
3723 "Cons of coding systems used for process I/O by default.\n\
3724 The car part is used for decoding a process output,\n\
3725 the cdr part is used for encoding a text to be sent to a process.");
3726 Vdefault_process_coding_system
= Qnil
;