1 /* Coding system handler (conversion, detection, and etc).
2 Copyright (C) 1995, 1997, 1998 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 section 2.
51 The most famous coding system for multiple character sets. X's
52 Compound Text, various EUCs (Extended Unix Code), and coding
53 systems used in Internet communication such as ISO-2022-JP are
54 all variants of ISO2022. Details are described in 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 section 4. In this file, when we write "BIG5"
67 (all uppercase), we mean the coding system, and when we write
68 "Big5" (capitalized), we mean the character set.
72 A coding system for a text containing random 8-bit code. Emacs does
73 no code conversion on such a text except for end-of-line format.
77 If a user wants to read/write a text encoded in a coding system not
78 listed above, he can supply a decoder and an encoder for it in CCL
79 (Code Conversion Language) programs. Emacs executes the CCL program
80 while reading/writing.
82 Emacs represents a coding system by a Lisp symbol that has a property
83 `coding-system'. But, before actually using the coding system, the
84 information about it is set in a structure of type `struct
85 coding_system' for rapid processing. See section 6 for more details.
89 /*** GENERAL NOTES on END-OF-LINE FORMAT ***
91 How end-of-line of a text is encoded depends on a system. For
92 instance, Unix's format is just one byte of `line-feed' code,
93 whereas DOS's format is two-byte sequence of `carriage-return' and
94 `line-feed' codes. MacOS's format is usually one byte of
97 Since text characters encoding and end-of-line encoding are
98 independent, any coding system described above can take
99 any format of end-of-line. So, Emacs has information of format of
100 end-of-line in each coding-system. See section 6 for more details.
104 /*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
106 These functions check if a text between SRC and SRC_END is encoded
107 in the coding system category XXX. Each returns an integer value in
108 which appropriate flag bits for the category XXX is set. The flag
109 bits are defined in macros CODING_CATEGORY_MASK_XXX. Below is the
110 template of these functions. */
113 detect_coding_emacs_mule (src
, src_end
)
114 unsigned char *src
, *src_end
;
120 /*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
122 These functions decode SRC_BYTES length text at SOURCE encoded in
123 CODING to Emacs' internal format (emacs-mule). The resulting text
124 goes to a place pointed to by DESTINATION, the length of which
125 should not exceed DST_BYTES. These functions set the information of
126 original and decoded texts in the members produced, produced_char,
127 consumed, and consumed_char of the structure *CODING.
129 The return value is an integer (CODING_FINISH_XXX) indicating how
130 the decoding finished.
132 DST_BYTES zero means that source area and destination area are
133 overlapped, which means that we can produce a decoded text until it
134 reaches at the head of not-yet-decoded source text.
136 Below is a template of these functions. */
138 decode_coding_XXX (coding
, source
, destination
, src_bytes
, dst_bytes
)
139 struct coding_system
*coding
;
140 unsigned char *source
, *destination
;
141 int src_bytes
, dst_bytes
;
147 /*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
149 These functions encode SRC_BYTES length text at SOURCE of Emacs'
150 internal format (emacs-mule) to CODING. The resulting text goes to
151 a place pointed to by DESTINATION, the length of which should not
152 exceed DST_BYTES. These functions set the information of
153 original and encoded texts in the members produced, produced_char,
154 consumed, and consumed_char of the structure *CODING.
156 The return value is an integer (CODING_FINISH_XXX) indicating how
157 the encoding finished.
159 DST_BYTES zero means that source area and destination area are
160 overlapped, which means that we can produce a decoded text until it
161 reaches at the head of not-yet-decoded source text.
163 Below is a template of these functions. */
165 encode_coding_XXX (coding
, source
, destination
, src_bytes
, dst_bytes
)
166 struct coding_system
*coding
;
167 unsigned char *source
, *destination
;
168 int src_bytes
, dst_bytes
;
174 /*** COMMONLY USED MACROS ***/
176 /* The following three macros ONE_MORE_BYTE, TWO_MORE_BYTES, and
177 THREE_MORE_BYTES safely get one, two, and three bytes from the
178 source text respectively. If there are not enough bytes in the
179 source, they jump to `label_end_of_loop'. The caller should set
180 variables `src' and `src_end' to appropriate areas in advance. */
182 #define ONE_MORE_BYTE(c1) \
187 goto label_end_of_loop; \
190 #define TWO_MORE_BYTES(c1, c2) \
192 if (src + 1 < src_end) \
193 c1 = *src++, c2 = *src++; \
195 goto label_end_of_loop; \
198 #define THREE_MORE_BYTES(c1, c2, c3) \
200 if (src + 2 < src_end) \
201 c1 = *src++, c2 = *src++, c3 = *src++; \
203 goto label_end_of_loop; \
206 /* The following three macros DECODE_CHARACTER_ASCII,
207 DECODE_CHARACTER_DIMENSION1, and DECODE_CHARACTER_DIMENSION2 put
208 the multi-byte form of a character of each class at the place
209 pointed by `dst'. The caller should set the variable `dst' to
210 point to an appropriate area and the variable `coding' to point to
211 the coding-system of the currently decoding text in advance. */
213 /* Decode one ASCII character C. */
215 #define DECODE_CHARACTER_ASCII(c) \
217 if (COMPOSING_P (coding->composing)) \
218 *dst++ = 0xA0, *dst++ = (c) | 0x80; \
222 coding->produced_char++; \
226 /* Decode one DIMENSION1 character whose charset is CHARSET and whose
227 position-code is C. */
229 #define DECODE_CHARACTER_DIMENSION1(charset, c) \
231 unsigned char leading_code = CHARSET_LEADING_CODE_BASE (charset); \
232 if (COMPOSING_P (coding->composing)) \
233 *dst++ = leading_code + 0x20; \
236 *dst++ = leading_code; \
237 coding->produced_char++; \
239 if (leading_code = CHARSET_LEADING_CODE_EXT (charset)) \
240 *dst++ = leading_code; \
241 *dst++ = (c) | 0x80; \
244 /* Decode one DIMENSION2 character whose charset is CHARSET and whose
245 position-codes are C1 and C2. */
247 #define DECODE_CHARACTER_DIMENSION2(charset, c1, c2) \
249 DECODE_CHARACTER_DIMENSION1 (charset, c1); \
250 *dst++ = (c2) | 0x80; \
254 /*** 1. Preamble ***/
268 #else /* not emacs */
272 #endif /* not emacs */
274 Lisp_Object Qcoding_system
, Qeol_type
;
275 Lisp_Object Qbuffer_file_coding_system
;
276 Lisp_Object Qpost_read_conversion
, Qpre_write_conversion
;
277 Lisp_Object Qno_conversion
, Qundecided
;
278 Lisp_Object Qcoding_system_history
;
279 Lisp_Object Qsafe_charsets
;
281 extern Lisp_Object Qinsert_file_contents
, Qwrite_region
;
282 Lisp_Object Qcall_process
, Qcall_process_region
, Qprocess_argument
;
283 Lisp_Object Qstart_process
, Qopen_network_stream
;
284 Lisp_Object Qtarget_idx
;
286 Lisp_Object Vselect_safe_coding_system_function
;
288 /* Mnemonic character of each format of end-of-line. */
289 int eol_mnemonic_unix
, eol_mnemonic_dos
, eol_mnemonic_mac
;
290 /* Mnemonic character to indicate format of end-of-line is not yet
292 int eol_mnemonic_undecided
;
294 /* Format of end-of-line decided by system. This is CODING_EOL_LF on
295 Unix, CODING_EOL_CRLF on DOS/Windows, and CODING_EOL_CR on Mac. */
300 Lisp_Object Vcoding_system_list
, Vcoding_system_alist
;
302 Lisp_Object Qcoding_system_p
, Qcoding_system_error
;
304 /* Coding system emacs-mule and raw-text are for converting only
305 end-of-line format. */
306 Lisp_Object Qemacs_mule
, Qraw_text
;
308 /* Coding-systems are handed between Emacs Lisp programs and C internal
309 routines by the following three variables. */
310 /* Coding-system for reading files and receiving data from process. */
311 Lisp_Object Vcoding_system_for_read
;
312 /* Coding-system for writing files and sending data to process. */
313 Lisp_Object Vcoding_system_for_write
;
314 /* Coding-system actually used in the latest I/O. */
315 Lisp_Object Vlast_coding_system_used
;
317 /* A vector of length 256 which contains information about special
318 Latin codes (espepcially for dealing with Microsoft code). */
319 Lisp_Object Vlatin_extra_code_table
;
321 /* Flag to inhibit code conversion of end-of-line format. */
322 int inhibit_eol_conversion
;
324 /* Flag to make buffer-file-coding-system inherit from process-coding. */
325 int inherit_process_coding_system
;
327 /* Coding system to be used to encode text for terminal display. */
328 struct coding_system terminal_coding
;
330 /* Coding system to be used to encode text for terminal display when
331 terminal coding system is nil. */
332 struct coding_system safe_terminal_coding
;
334 /* Coding system of what is sent from terminal keyboard. */
335 struct coding_system keyboard_coding
;
337 Lisp_Object Vfile_coding_system_alist
;
338 Lisp_Object Vprocess_coding_system_alist
;
339 Lisp_Object Vnetwork_coding_system_alist
;
343 Lisp_Object Qcoding_category
, Qcoding_category_index
;
345 /* List of symbols `coding-category-xxx' ordered by priority. */
346 Lisp_Object Vcoding_category_list
;
348 /* Table of coding categories (Lisp symbols). */
349 Lisp_Object Vcoding_category_table
;
351 /* Table of names of symbol for each coding-category. */
352 char *coding_category_name
[CODING_CATEGORY_IDX_MAX
] = {
353 "coding-category-emacs-mule",
354 "coding-category-sjis",
355 "coding-category-iso-7",
356 "coding-category-iso-7-tight",
357 "coding-category-iso-8-1",
358 "coding-category-iso-8-2",
359 "coding-category-iso-7-else",
360 "coding-category-iso-8-else",
361 "coding-category-big5",
362 "coding-category-raw-text",
363 "coding-category-binary"
366 /* Table of pointers to coding systems corresponding to each coding
368 struct coding_system
*coding_system_table
[CODING_CATEGORY_IDX_MAX
];
370 /* Table of coding category masks. Nth element is a mask for a coding
371 cateogry of which priority is Nth. */
373 int coding_priorities
[CODING_CATEGORY_IDX_MAX
];
375 /* Flag to tell if we look up translation table on character code
377 Lisp_Object Venable_character_translation
;
378 /* Standard translation table to look up on decoding (reading). */
379 Lisp_Object Vstandard_translation_table_for_decode
;
380 /* Standard translation table to look up on encoding (writing). */
381 Lisp_Object Vstandard_translation_table_for_encode
;
383 Lisp_Object Qtranslation_table
;
384 Lisp_Object Qtranslation_table_id
;
385 Lisp_Object Qtranslation_table_for_decode
;
386 Lisp_Object Qtranslation_table_for_encode
;
388 /* Alist of charsets vs revision number. */
389 Lisp_Object Vcharset_revision_alist
;
391 /* Default coding systems used for process I/O. */
392 Lisp_Object Vdefault_process_coding_system
;
395 /*** 2. Emacs internal format (emacs-mule) handlers ***/
397 /* Emacs' internal format for encoding multiple character sets is a
398 kind of multi-byte encoding, i.e. characters are encoded by
399 variable-length sequences of one-byte codes. ASCII characters
400 and control characters (e.g. `tab', `newline') are represented by
401 one-byte sequences which are their ASCII codes, in the range 0x00
402 through 0x7F. The other characters are represented by a sequence
403 of `base leading-code', optional `extended leading-code', and one
404 or two `position-code's. The length of the sequence is determined
405 by the base leading-code. Leading-code takes the range 0x80
406 through 0x9F, whereas extended leading-code and position-code take
407 the range 0xA0 through 0xFF. See `charset.h' for more details
408 about leading-code and position-code.
410 There's one exception to this rule. Special leading-code
411 `leading-code-composition' denotes that the following several
412 characters should be composed into one character. Leading-codes of
413 components (except for ASCII) are added 0x20. An ASCII character
414 component is represented by a 2-byte sequence of `0xA0' and
415 `ASCII-code + 0x80'. See also the comments in `charset.h' for the
416 details of composite character. Hence, we can summarize the code
419 --- CODE RANGE of Emacs' internal format ---
420 (character set) (range)
422 ELSE (1st byte) 0x80 .. 0x9F
423 (rest bytes) 0xA0 .. 0xFF
424 ---------------------------------------------
428 enum emacs_code_class_type emacs_code_class
[256];
430 /* Go to the next statement only if *SRC is accessible and the code is
431 greater than 0xA0. */
432 #define CHECK_CODE_RANGE_A0_FF \
434 if (src >= src_end) \
435 goto label_end_of_switch; \
436 else if (*src++ < 0xA0) \
440 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
441 Check if a text is encoded in Emacs' internal format. If it is,
442 return CODING_CATEGORY_MASK_EMACS_MULE, else return 0. */
445 detect_coding_emacs_mule (src
, src_end
)
446 unsigned char *src
, *src_end
;
451 while (src
< src_end
)
463 switch (emacs_code_class
[c
])
465 case EMACS_ascii_code
:
466 case EMACS_linefeed_code
:
469 case EMACS_control_code
:
470 if (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
474 case EMACS_invalid_code
:
477 case EMACS_leading_code_composition
: /* c == 0x80 */
479 CHECK_CODE_RANGE_A0_FF
;
484 case EMACS_leading_code_4
:
485 CHECK_CODE_RANGE_A0_FF
;
486 /* fall down to check it two more times ... */
488 case EMACS_leading_code_3
:
489 CHECK_CODE_RANGE_A0_FF
;
490 /* fall down to check it one more time ... */
492 case EMACS_leading_code_2
:
493 CHECK_CODE_RANGE_A0_FF
;
501 return CODING_CATEGORY_MASK_EMACS_MULE
;
505 /*** 3. ISO2022 handlers ***/
507 /* The following note describes the coding system ISO2022 briefly.
508 Since the intention of this note is to help in understanding of
509 the programs in this file, some parts are NOT ACCURATE or OVERLY
510 SIMPLIFIED. For the thorough understanding, please refer to the
511 original document of ISO2022.
513 ISO2022 provides many mechanisms to encode several character sets
514 in 7-bit and 8-bit environment. If one chooses 7-bite environment,
515 all text is encoded by codes of less than 128. This may make the
516 encoded text a little bit longer, but the text gets more stability
517 to pass through several gateways (some of them strip off the MSB).
519 There are two kinds of character set: control character set and
520 graphic character set. The former contains control characters such
521 as `newline' and `escape' to provide control functions (control
522 functions are provided also by escape sequences). The latter
523 contains graphic characters such as ' A' and '-'. Emacs recognizes
524 two control character sets and many graphic character sets.
526 Graphic character sets are classified into one of the following
527 four classes, DIMENSION1_CHARS94, DIMENSION1_CHARS96,
528 DIMENSION2_CHARS94, DIMENSION2_CHARS96 according to the number of
529 bytes (DIMENSION) and the number of characters in one dimension
530 (CHARS) of the set. In addition, each character set is assigned an
531 identification tag (called "final character" and denoted as <F>
532 here after) which is unique in each class. <F> of each character
533 set is decided by ECMA(*) when it is registered in ISO. Code range
534 of <F> is 0x30..0x7F (0x30..0x3F are for private use only).
536 Note (*): ECMA = European Computer Manufacturers Association
538 Here are examples of graphic character set [NAME(<F>)]:
539 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
540 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
541 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
542 o DIMENSION2_CHARS96 -- none for the moment
544 A code area (1byte=8bits) is divided into 4 areas, C0, GL, C1, and GR.
545 C0 [0x00..0x1F] -- control character plane 0
546 GL [0x20..0x7F] -- graphic character plane 0
547 C1 [0x80..0x9F] -- control character plane 1
548 GR [0xA0..0xFF] -- graphic character plane 1
550 A control character set is directly designated and invoked to C0 or
551 C1 by an escape sequence. The most common case is that ISO646's
552 control character set is designated/invoked to C0 and ISO6429's
553 control character set is designated/invoked to C1, and usually
554 these designations/invocations are omitted in a coded text. With
555 7-bit environment, only C0 can be used, and a control character for
556 C1 is encoded by an appropriate escape sequence to fit in the
557 environment. All control characters for C1 are defined the
558 corresponding escape sequences.
560 A graphic character set is at first designated to one of four
561 graphic registers (G0 through G3), then these graphic registers are
562 invoked to GL or GR. These designations and invocations can be
563 done independently. The most common case is that G0 is invoked to
564 GL, G1 is invoked to GR, and ASCII is designated to G0, and usually
565 these invocations and designations are omitted in a coded text.
566 With 7-bit environment, only GL can be used.
568 When a graphic character set of CHARS94 is invoked to GL, code 0x20
569 and 0x7F of GL area work as control characters SPACE and DEL
570 respectively, and code 0xA0 and 0xFF of GR area should not be used.
572 There are two ways of invocation: locking-shift and single-shift.
573 With locking-shift, the invocation lasts until the next different
574 invocation, whereas with single-shift, the invocation works only
575 for the following character and doesn't affect locking-shift.
576 Invocations are done by the following control characters or escape
579 ----------------------------------------------------------------------
580 function control char escape sequence description
581 ----------------------------------------------------------------------
582 SI (shift-in) 0x0F none invoke G0 to GL
583 SO (shift-out) 0x0E none invoke G1 to GL
584 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
585 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
586 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 into GL
587 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 into GL
588 ----------------------------------------------------------------------
589 The first four are for locking-shift. Control characters for these
590 functions are defined by macros ISO_CODE_XXX in `coding.h'.
592 Designations are done by the following escape sequences.
593 ----------------------------------------------------------------------
594 escape sequence description
595 ----------------------------------------------------------------------
596 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
597 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
598 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
599 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
600 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
601 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
602 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
603 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
604 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
605 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
606 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
607 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
608 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
609 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
610 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
611 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
612 ----------------------------------------------------------------------
614 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
615 of dimension 1, chars 94, and final character <F>, and etc.
617 Note (*): Although these designations are not allowed in ISO2022,
618 Emacs accepts them on decoding, and produces them on encoding
619 CHARS96 character set in a coding system which is characterized as
620 7-bit environment, non-locking-shift, and non-single-shift.
622 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
623 '(' can be omitted. We call this as "short-form" here after.
625 Now you may notice that there are a lot of ways for encoding the
626 same multilingual text in ISO2022. Actually, there exists many
627 coding systems such as Compound Text (used in X's inter client
628 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
629 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
630 localized platforms), and all of these are variants of ISO2022.
632 In addition to the above, Emacs handles two more kinds of escape
633 sequences: ISO6429's direction specification and Emacs' private
634 sequence for specifying character composition.
636 ISO6429's direction specification takes the following format:
637 o CSI ']' -- end of the current direction
638 o CSI '0' ']' -- end of the current direction
639 o CSI '1' ']' -- start of left-to-right text
640 o CSI '2' ']' -- start of right-to-left text
641 The control character CSI (0x9B: control sequence introducer) is
642 abbreviated to the escape sequence ESC '[' in 7-bit environment.
644 Character composition specification takes the following format:
645 o ESC '0' -- start character composition
646 o ESC '1' -- end character composition
647 Since these are not standard escape sequences of any ISO, the use
648 of them for these meaning is restricted to Emacs only. */
650 enum iso_code_class_type iso_code_class
[256];
652 #define CHARSET_OK(idx, charset) \
653 (coding_system_table[idx]->safe_charsets[charset] \
654 || (CODING_SPEC_ISO_REQUESTED_DESIGNATION \
655 (coding_system_table[idx], charset) \
656 != CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION))
658 #define SHIFT_OUT_OK(idx) \
659 (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding_system_table[idx], 1) >= 0)
661 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
662 Check if a text is encoded in ISO2022. If it is, returns an
663 integer in which appropriate flag bits any of:
664 CODING_CATEGORY_MASK_ISO_7
665 CODING_CATEGORY_MASK_ISO_7_TIGHT
666 CODING_CATEGORY_MASK_ISO_8_1
667 CODING_CATEGORY_MASK_ISO_8_2
668 CODING_CATEGORY_MASK_ISO_7_ELSE
669 CODING_CATEGORY_MASK_ISO_8_ELSE
670 are set. If a code which should never appear in ISO2022 is found,
674 detect_coding_iso2022 (src
, src_end
)
675 unsigned char *src
, *src_end
;
677 int mask
= CODING_CATEGORY_MASK_ISO
;
679 int reg
[4], shift_out
= 0;
680 int c
, c1
, i
, charset
;
682 reg
[0] = CHARSET_ASCII
, reg
[1] = reg
[2] = reg
[3] = -1;
683 while (mask
&& src
< src_end
)
692 if (c
>= '(' && c
<= '/')
694 /* Designation sequence for a charset of dimension 1. */
698 if (c1
< ' ' || c1
>= 0x80
699 || (charset
= iso_charset_table
[0][c
>= ','][c1
]) < 0)
700 /* Invalid designation sequence. Just ignore. */
702 reg
[(c
- '(') % 4] = charset
;
706 /* Designation sequence for a charset of dimension 2. */
710 if (c
>= '@' && c
<= 'B')
711 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
712 reg
[0] = charset
= iso_charset_table
[1][0][c
];
713 else if (c
>= '(' && c
<= '/')
718 if (c1
< ' ' || c1
>= 0x80
719 || (charset
= iso_charset_table
[1][c
>= ','][c1
]) < 0)
720 /* Invalid designation sequence. Just ignore. */
722 reg
[(c
- '(') % 4] = charset
;
725 /* Invalid designation sequence. Just ignore. */
728 else if (c
== 'N' || c
== 'n')
732 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_7_ELSE
)
733 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_8_ELSE
)))
735 /* Locking shift out. */
736 mask
&= ~CODING_CATEGORY_MASK_ISO_7BIT
;
737 mask_found
|= CODING_CATEGORY_MASK_ISO_SHIFT
;
742 else if (c
== 'O' || c
== 'o')
746 /* Locking shift in. */
747 mask
&= ~CODING_CATEGORY_MASK_ISO_7BIT
;
748 mask_found
|= CODING_CATEGORY_MASK_ISO_SHIFT
;
753 else if (c
== '0' || c
== '1' || c
== '2')
754 /* Start/end composition. Just ignore. */
757 /* Invalid escape sequence. Just ignore. */
760 /* We found a valid designation sequence for CHARSET. */
761 mask
&= ~CODING_CATEGORY_MASK_ISO_8BIT
;
762 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7
, charset
))
763 mask_found
|= CODING_CATEGORY_MASK_ISO_7
;
765 mask
&= ~CODING_CATEGORY_MASK_ISO_7
;
766 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7_TIGHT
, charset
))
767 mask_found
|= CODING_CATEGORY_MASK_ISO_7_TIGHT
;
769 mask
&= ~CODING_CATEGORY_MASK_ISO_7_TIGHT
;
770 if (! CHARSET_OK (CODING_CATEGORY_IDX_ISO_7_ELSE
, charset
))
771 mask
&= ~CODING_CATEGORY_MASK_ISO_7_ELSE
;
772 if (! CHARSET_OK (CODING_CATEGORY_IDX_ISO_8_ELSE
, charset
))
773 mask
&= ~CODING_CATEGORY_MASK_ISO_8_ELSE
;
779 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_7_ELSE
)
780 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_8_ELSE
)))
782 /* Locking shift out. */
783 mask
&= ~CODING_CATEGORY_MASK_ISO_7BIT
;
784 mask_found
|= CODING_CATEGORY_MASK_ISO_SHIFT
;
791 /* Locking shift in. */
792 mask
&= ~CODING_CATEGORY_MASK_ISO_7BIT
;
793 mask_found
|= CODING_CATEGORY_MASK_ISO_SHIFT
;
801 int newmask
= CODING_CATEGORY_MASK_ISO_8_ELSE
;
803 if (c
!= ISO_CODE_CSI
)
805 if (coding_system_table
[CODING_CATEGORY_IDX_ISO_8_1
]->flags
806 & CODING_FLAG_ISO_SINGLE_SHIFT
)
807 newmask
|= CODING_CATEGORY_MASK_ISO_8_1
;
808 if (coding_system_table
[CODING_CATEGORY_IDX_ISO_8_2
]->flags
809 & CODING_FLAG_ISO_SINGLE_SHIFT
)
810 newmask
|= CODING_CATEGORY_MASK_ISO_8_2
;
812 if (VECTORP (Vlatin_extra_code_table
)
813 && !NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
815 if (coding_system_table
[CODING_CATEGORY_IDX_ISO_8_1
]->flags
816 & CODING_FLAG_ISO_LATIN_EXTRA
)
817 newmask
|= CODING_CATEGORY_MASK_ISO_8_1
;
818 if (coding_system_table
[CODING_CATEGORY_IDX_ISO_8_2
]->flags
819 & CODING_FLAG_ISO_LATIN_EXTRA
)
820 newmask
|= CODING_CATEGORY_MASK_ISO_8_2
;
823 mask_found
|= newmask
;
832 if (VECTORP (Vlatin_extra_code_table
)
833 && !NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
837 if (coding_system_table
[CODING_CATEGORY_IDX_ISO_8_1
]->flags
838 & CODING_FLAG_ISO_LATIN_EXTRA
)
839 newmask
|= CODING_CATEGORY_MASK_ISO_8_1
;
840 if (coding_system_table
[CODING_CATEGORY_IDX_ISO_8_2
]->flags
841 & CODING_FLAG_ISO_LATIN_EXTRA
)
842 newmask
|= CODING_CATEGORY_MASK_ISO_8_2
;
844 mask_found
|= newmask
;
851 unsigned char *src_begin
= src
;
853 mask
&= ~(CODING_CATEGORY_MASK_ISO_7BIT
854 | CODING_CATEGORY_MASK_ISO_7_ELSE
);
855 mask_found
|= CODING_CATEGORY_MASK_ISO_8_1
;
856 while (src
< src_end
&& *src
>= 0xA0)
858 if ((src
- src_begin
- 1) & 1 && src
< src_end
)
859 mask
&= ~CODING_CATEGORY_MASK_ISO_8_2
;
861 mask_found
|= CODING_CATEGORY_MASK_ISO_8_2
;
867 return (mask
& mask_found
);
870 /* Decode a character of which charset is CHARSET and the 1st position
871 code is C1. If dimension of CHARSET is 2, the 2nd position code is
872 fetched from SRC and set to C2. If CHARSET is negative, it means
873 that we are decoding ill formed text, and what we can do is just to
876 #define DECODE_ISO_CHARACTER(charset, c1) \
878 int c_alt, charset_alt = (charset); \
879 if (COMPOSING_HEAD_P (coding->composing)) \
881 *dst++ = LEADING_CODE_COMPOSITION; \
882 if (COMPOSING_WITH_RULE_P (coding->composing)) \
883 /* To tell composition rules are embeded. */ \
885 coding->composing += 2; \
887 if ((charset) >= 0) \
889 if (CHARSET_DIMENSION (charset) == 2) \
891 ONE_MORE_BYTE (c2); \
892 if (iso_code_class[(c2) & 0x7F] != ISO_0x20_or_0x7F \
893 && iso_code_class[(c2) & 0x7F] != ISO_graphic_plane_0) \
899 if (!NILP (translation_table) \
900 && ((c_alt = translate_char (translation_table, \
901 -1, (charset), c1, c2)) >= 0)) \
902 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
904 if (charset_alt == CHARSET_ASCII || charset_alt < 0) \
905 DECODE_CHARACTER_ASCII (c1); \
906 else if (CHARSET_DIMENSION (charset_alt) == 1) \
907 DECODE_CHARACTER_DIMENSION1 (charset_alt, c1); \
909 DECODE_CHARACTER_DIMENSION2 (charset_alt, c1, c2); \
910 if (COMPOSING_WITH_RULE_P (coding->composing)) \
911 /* To tell a composition rule follows. */ \
912 coding->composing = COMPOSING_WITH_RULE_RULE; \
915 /* Set designation state into CODING. */
916 #define DECODE_DESIGNATION(reg, dimension, chars, final_char) \
918 int charset = ISO_CHARSET_TABLE (make_number (dimension), \
919 make_number (chars), \
920 make_number (final_char)); \
922 && (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) == reg \
923 || coding->safe_charsets[charset])) \
925 if (coding->spec.iso2022.last_invalid_designation_register == 0 \
927 && charset == CHARSET_ASCII) \
929 /* We should insert this designation sequence as is so \
930 that it is surely written back to a file. */ \
931 coding->spec.iso2022.last_invalid_designation_register = -1; \
932 goto label_invalid_code; \
934 coding->spec.iso2022.last_invalid_designation_register = -1; \
935 if ((coding->mode & CODING_MODE_DIRECTION) \
936 && CHARSET_REVERSE_CHARSET (charset) >= 0) \
937 charset = CHARSET_REVERSE_CHARSET (charset); \
938 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
942 coding->spec.iso2022.last_invalid_designation_register = reg; \
943 goto label_invalid_code; \
947 /* Check if the current composing sequence contains only valid codes.
948 If the composing sequence doesn't end before SRC_END, return -1.
949 Else, if it contains only valid codes, return 0.
950 Else return the length of the composing sequence. */
953 check_composing_code (coding
, src
, src_end
)
954 struct coding_system
*coding
;
955 unsigned char *src
, *src_end
;
957 unsigned char *src_start
= src
;
958 int invalid_code_found
= 0;
959 int charset
, c
, c1
, dim
;
961 while (src
< src_end
)
963 if (*src
++ != ISO_CODE_ESC
) continue;
964 if (src
>= src_end
) break;
965 if ((c
= *src
++) == '1') /* end of compsition */
966 return (invalid_code_found
? src
- src_start
: 0);
967 if (src
+ 2 >= src_end
) break;
968 if (!coding
->flags
& CODING_FLAG_ISO_DESIGNATION
)
969 invalid_code_found
= 1;
976 c
= (*src
>= '@' && *src
<= 'B') ? '(' : *src
++;
978 if (c
>= '(' && c
<= '/')
981 if ((c1
< ' ' || c1
>= 0x80)
982 || (charset
= iso_charset_table
[dim
][c
>= ','][c1
]) < 0
983 || ! coding
->safe_charsets
[charset
]
984 || (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
985 == CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
))
986 invalid_code_found
= 1;
989 invalid_code_found
= 1;
992 return (invalid_code_found
994 : (coding
->mode
& CODING_MODE_LAST_BLOCK
? 0 : -1));
997 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
1000 decode_coding_iso2022 (coding
, source
, destination
, src_bytes
, dst_bytes
)
1001 struct coding_system
*coding
;
1002 unsigned char *source
, *destination
;
1003 int src_bytes
, dst_bytes
;
1005 unsigned char *src
= source
;
1006 unsigned char *src_end
= source
+ src_bytes
;
1007 unsigned char *dst
= destination
;
1008 unsigned char *dst_end
= destination
+ dst_bytes
;
1009 /* Since the maximum bytes produced by each loop is 7, we subtract 6
1010 from DST_END to assure that overflow checking is necessary only
1011 at the head of loop. */
1012 unsigned char *adjusted_dst_end
= dst_end
- 6;
1014 /* Charsets invoked to graphic plane 0 and 1 respectively. */
1015 int charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
1016 int charset1
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 1);
1017 Lisp_Object translation_table
1018 = coding
->translation_table_for_decode
;
1019 int result
= CODING_FINISH_NORMAL
;
1021 if (!NILP (Venable_character_translation
) && NILP (translation_table
))
1022 translation_table
= Vstandard_translation_table_for_decode
;
1024 coding
->produced_char
= 0;
1025 coding
->fake_multibyte
= 0;
1026 while (src
< src_end
&& (dst_bytes
1027 ? (dst
< adjusted_dst_end
)
1030 /* SRC_BASE remembers the start position in source in each loop.
1031 The loop will be exited when there's not enough source text
1032 to analyze long escape sequence or 2-byte code (within macros
1033 ONE_MORE_BYTE or TWO_MORE_BYTES). In that case, SRC is reset
1034 to SRC_BASE before exiting. */
1035 unsigned char *src_base
= src
;
1036 int c1
= *src
++, c2
;
1038 switch (iso_code_class
[c1
])
1040 case ISO_0x20_or_0x7F
:
1041 if (!coding
->composing
1042 && (charset0
< 0 || CHARSET_CHARS (charset0
) == 94))
1044 /* This is SPACE or DEL. */
1046 coding
->produced_char
++;
1049 /* This is a graphic character, we fall down ... */
1051 case ISO_graphic_plane_0
:
1052 if (coding
->composing
== COMPOSING_WITH_RULE_RULE
)
1054 /* This is a composition rule. */
1056 coding
->composing
= COMPOSING_WITH_RULE_TAIL
;
1059 DECODE_ISO_CHARACTER (charset0
, c1
);
1062 case ISO_0xA0_or_0xFF
:
1063 if (charset1
< 0 || CHARSET_CHARS (charset1
) == 94
1064 || coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
)
1065 goto label_invalid_code
;
1066 /* This is a graphic character, we fall down ... */
1068 case ISO_graphic_plane_1
:
1069 if (coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
)
1070 goto label_invalid_code
;
1072 DECODE_ISO_CHARACTER (charset1
, c1
);
1075 case ISO_control_code
:
1076 /* All ISO2022 control characters in this class have the
1077 same representation in Emacs internal format. */
1079 && (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
)
1080 && (coding
->eol_type
== CODING_EOL_CR
1081 || coding
->eol_type
== CODING_EOL_CRLF
))
1083 result
= CODING_FINISH_INCONSISTENT_EOL
;
1084 goto label_end_of_loop_2
;
1087 coding
->produced_char
++;
1090 case ISO_carriage_return
:
1091 if (coding
->eol_type
== CODING_EOL_CR
)
1093 else if (coding
->eol_type
== CODING_EOL_CRLF
)
1096 if (c1
== ISO_CODE_LF
)
1100 if (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
)
1102 result
= CODING_FINISH_INCONSISTENT_EOL
;
1103 goto label_end_of_loop_2
;
1111 coding
->produced_char
++;
1115 if (! (coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
)
1116 || CODING_SPEC_ISO_DESIGNATION (coding
, 1) < 0)
1117 goto label_invalid_code
;
1118 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 1;
1119 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
1123 if (! (coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
))
1124 goto label_invalid_code
;
1125 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 0;
1126 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
1129 case ISO_single_shift_2_7
:
1130 case ISO_single_shift_2
:
1131 if (! (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
))
1132 goto label_invalid_code
;
1133 /* SS2 is handled as an escape sequence of ESC 'N' */
1135 goto label_escape_sequence
;
1137 case ISO_single_shift_3
:
1138 if (! (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
))
1139 goto label_invalid_code
;
1140 /* SS2 is handled as an escape sequence of ESC 'O' */
1142 goto label_escape_sequence
;
1144 case ISO_control_sequence_introducer
:
1145 /* CSI is handled as an escape sequence of ESC '[' ... */
1147 goto label_escape_sequence
;
1151 label_escape_sequence
:
1152 /* Escape sequences handled by Emacs are invocation,
1153 designation, direction specification, and character
1154 composition specification. */
1157 case '&': /* revision of following character set */
1159 if (!(c1
>= '@' && c1
<= '~'))
1160 goto label_invalid_code
;
1162 if (c1
!= ISO_CODE_ESC
)
1163 goto label_invalid_code
;
1165 goto label_escape_sequence
;
1167 case '$': /* designation of 2-byte character set */
1168 if (! (coding
->flags
& CODING_FLAG_ISO_DESIGNATION
))
1169 goto label_invalid_code
;
1171 if (c1
>= '@' && c1
<= 'B')
1172 { /* designation of JISX0208.1978, GB2312.1980,
1174 DECODE_DESIGNATION (0, 2, 94, c1
);
1176 else if (c1
>= 0x28 && c1
<= 0x2B)
1177 { /* designation of DIMENSION2_CHARS94 character set */
1179 DECODE_DESIGNATION (c1
- 0x28, 2, 94, c2
);
1181 else if (c1
>= 0x2C && c1
<= 0x2F)
1182 { /* designation of DIMENSION2_CHARS96 character set */
1184 DECODE_DESIGNATION (c1
- 0x2C, 2, 96, c2
);
1187 goto label_invalid_code
;
1190 case 'n': /* invocation of locking-shift-2 */
1191 if (! (coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
)
1192 || CODING_SPEC_ISO_DESIGNATION (coding
, 2) < 0)
1193 goto label_invalid_code
;
1194 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 2;
1195 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
1198 case 'o': /* invocation of locking-shift-3 */
1199 if (! (coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
)
1200 || CODING_SPEC_ISO_DESIGNATION (coding
, 3) < 0)
1201 goto label_invalid_code
;
1202 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 3;
1203 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
1206 case 'N': /* invocation of single-shift-2 */
1207 if (! (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
1208 || CODING_SPEC_ISO_DESIGNATION (coding
, 2) < 0)
1209 goto label_invalid_code
;
1211 charset
= CODING_SPEC_ISO_DESIGNATION (coding
, 2);
1212 DECODE_ISO_CHARACTER (charset
, c1
);
1215 case 'O': /* invocation of single-shift-3 */
1216 if (! (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
1217 || CODING_SPEC_ISO_DESIGNATION (coding
, 3) < 0)
1218 goto label_invalid_code
;
1220 charset
= CODING_SPEC_ISO_DESIGNATION (coding
, 3);
1221 DECODE_ISO_CHARACTER (charset
, c1
);
1224 case '0': case '2': /* start composing */
1225 /* Before processing composing, we must be sure that all
1226 characters being composed are supported by CODING.
1227 If not, we must give up composing and insert the
1228 bunch of codes for composing as is without decoding. */
1232 result1
= check_composing_code (coding
, src
, src_end
);
1235 coding
->composing
= (c1
== '0'
1236 ? COMPOSING_NO_RULE_HEAD
1237 : COMPOSING_WITH_RULE_HEAD
);
1238 coding
->produced_char
++;
1240 else if (result1
> 0)
1242 if (result1
+ 2 < (dst_bytes
? dst_end
: src_base
) - dst
)
1244 bcopy (src_base
, dst
, result1
+ 2);
1247 coding
->produced_char
+= result1
+ 2;
1251 result
= CODING_FINISH_INSUFFICIENT_DST
;
1252 goto label_end_of_loop_2
;
1256 goto label_end_of_loop
;
1260 case '1': /* end composing */
1261 coding
->composing
= COMPOSING_NO
;
1264 case '[': /* specification of direction */
1265 if (coding
->flags
& CODING_FLAG_ISO_NO_DIRECTION
)
1266 goto label_invalid_code
;
1267 /* For the moment, nested direction is not supported.
1268 So, `coding->mode & CODING_MODE_DIRECTION' zero means
1269 left-to-right, and nozero means right-to-left. */
1273 case ']': /* end of the current direction */
1274 coding
->mode
&= ~CODING_MODE_DIRECTION
;
1276 case '0': /* end of the current direction */
1277 case '1': /* start of left-to-right direction */
1280 coding
->mode
&= ~CODING_MODE_DIRECTION
;
1282 goto label_invalid_code
;
1285 case '2': /* start of right-to-left direction */
1288 coding
->mode
|= CODING_MODE_DIRECTION
;
1290 goto label_invalid_code
;
1294 goto label_invalid_code
;
1299 if (! (coding
->flags
& CODING_FLAG_ISO_DESIGNATION
))
1300 goto label_invalid_code
;
1301 if (c1
>= 0x28 && c1
<= 0x2B)
1302 { /* designation of DIMENSION1_CHARS94 character set */
1304 DECODE_DESIGNATION (c1
- 0x28, 1, 94, c2
);
1306 else if (c1
>= 0x2C && c1
<= 0x2F)
1307 { /* designation of DIMENSION1_CHARS96 character set */
1309 DECODE_DESIGNATION (c1
- 0x2C, 1, 96, c2
);
1313 goto label_invalid_code
;
1316 /* We must update these variables now. */
1317 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
1318 charset1
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 1);
1322 while (src_base
< src
)
1323 *dst
++ = *src_base
++;
1324 coding
->fake_multibyte
= 1;
1329 result
= CODING_FINISH_INSUFFICIENT_SRC
;
1330 label_end_of_loop_2
:
1337 if (result
== CODING_FINISH_NORMAL
)
1338 result
= CODING_FINISH_INSUFFICIENT_DST
;
1339 else if (result
!= CODING_FINISH_INCONSISTENT_EOL
1340 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
1342 /* This is the last block of the text to be decoded. We had
1343 better just flush out all remaining codes in the text
1344 although they are not valid characters. */
1345 src_bytes
= src_end
- src
;
1346 if (dst_bytes
&& (dst_end
- dst
< src_bytes
))
1347 src_bytes
= dst_end
- dst
;
1348 bcopy (src
, dst
, src_bytes
);
1351 coding
->fake_multibyte
= 1;
1355 coding
->consumed
= coding
->consumed_char
= src
- source
;
1356 coding
->produced
= dst
- destination
;
1360 /* ISO2022 encoding stuff. */
1363 It is not enough to say just "ISO2022" on encoding, we have to
1364 specify more details. In Emacs, each coding system of ISO2022
1365 variant has the following specifications:
1366 1. Initial designation to G0 thru G3.
1367 2. Allows short-form designation?
1368 3. ASCII should be designated to G0 before control characters?
1369 4. ASCII should be designated to G0 at end of line?
1370 5. 7-bit environment or 8-bit environment?
1371 6. Use locking-shift?
1372 7. Use Single-shift?
1373 And the following two are only for Japanese:
1374 8. Use ASCII in place of JIS0201-1976-Roman?
1375 9. Use JISX0208-1983 in place of JISX0208-1978?
1376 These specifications are encoded in `coding->flags' as flag bits
1377 defined by macros CODING_FLAG_ISO_XXX. See `coding.h' for more
1381 /* Produce codes (escape sequence) for designating CHARSET to graphic
1382 register REG. If <final-char> of CHARSET is '@', 'A', or 'B' and
1383 the coding system CODING allows, produce designation sequence of
1386 #define ENCODE_DESIGNATION(charset, reg, coding) \
1388 unsigned char final_char = CHARSET_ISO_FINAL_CHAR (charset); \
1389 char *intermediate_char_94 = "()*+"; \
1390 char *intermediate_char_96 = ",-./"; \
1391 int revision = CODING_SPEC_ISO_REVISION_NUMBER(coding, charset); \
1392 if (revision < 255) \
1394 *dst++ = ISO_CODE_ESC; \
1396 *dst++ = '@' + revision; \
1398 *dst++ = ISO_CODE_ESC; \
1399 if (CHARSET_DIMENSION (charset) == 1) \
1401 if (CHARSET_CHARS (charset) == 94) \
1402 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
1404 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
1409 if (CHARSET_CHARS (charset) == 94) \
1411 if (! (coding->flags & CODING_FLAG_ISO_SHORT_FORM) \
1413 || final_char < '@' || final_char > 'B') \
1414 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
1417 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
1419 *dst++ = final_char; \
1420 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
1423 /* The following two macros produce codes (control character or escape
1424 sequence) for ISO2022 single-shift functions (single-shift-2 and
1427 #define ENCODE_SINGLE_SHIFT_2 \
1429 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1430 *dst++ = ISO_CODE_ESC, *dst++ = 'N'; \
1433 *dst++ = ISO_CODE_SS2; \
1434 coding->fake_multibyte = 1; \
1436 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
1439 #define ENCODE_SINGLE_SHIFT_3 \
1441 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1442 *dst++ = ISO_CODE_ESC, *dst++ = 'O'; \
1445 *dst++ = ISO_CODE_SS3; \
1446 coding->fake_multibyte = 1; \
1448 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
1451 /* The following four macros produce codes (control character or
1452 escape sequence) for ISO2022 locking-shift functions (shift-in,
1453 shift-out, locking-shift-2, and locking-shift-3). */
1455 #define ENCODE_SHIFT_IN \
1457 *dst++ = ISO_CODE_SI; \
1458 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0; \
1461 #define ENCODE_SHIFT_OUT \
1463 *dst++ = ISO_CODE_SO; \
1464 CODING_SPEC_ISO_INVOCATION (coding, 0) = 1; \
1467 #define ENCODE_LOCKING_SHIFT_2 \
1469 *dst++ = ISO_CODE_ESC, *dst++ = 'n'; \
1470 CODING_SPEC_ISO_INVOCATION (coding, 0) = 2; \
1473 #define ENCODE_LOCKING_SHIFT_3 \
1475 *dst++ = ISO_CODE_ESC, *dst++ = 'o'; \
1476 CODING_SPEC_ISO_INVOCATION (coding, 0) = 3; \
1479 /* Produce codes for a DIMENSION1 character whose character set is
1480 CHARSET and whose position-code is C1. Designation and invocation
1481 sequences are also produced in advance if necessary. */
1484 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
1486 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
1488 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1489 *dst++ = c1 & 0x7F; \
1491 *dst++ = c1 | 0x80; \
1492 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
1495 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
1497 *dst++ = c1 & 0x7F; \
1500 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
1502 *dst++ = c1 | 0x80; \
1505 else if (coding->flags & CODING_FLAG_ISO_SAFE \
1506 && !coding->safe_charsets[charset]) \
1508 /* We should not encode this character, instead produce one or \
1510 *dst++ = CODING_INHIBIT_CHARACTER_SUBSTITUTION; \
1511 if (CHARSET_WIDTH (charset) == 2) \
1512 *dst++ = CODING_INHIBIT_CHARACTER_SUBSTITUTION; \
1516 /* Since CHARSET is not yet invoked to any graphic planes, we \
1517 must invoke it, or, at first, designate it to some graphic \
1518 register. Then repeat the loop to actually produce the \
1520 dst = encode_invocation_designation (charset, coding, dst); \
1523 /* Produce codes for a DIMENSION2 character whose character set is
1524 CHARSET and whose position-codes are C1 and C2. Designation and
1525 invocation codes are also produced in advance if necessary. */
1527 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
1529 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
1531 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1532 *dst++ = c1 & 0x7F, *dst++ = c2 & 0x7F; \
1534 *dst++ = c1 | 0x80, *dst++ = c2 | 0x80; \
1535 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
1538 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
1540 *dst++ = c1 & 0x7F, *dst++= c2 & 0x7F; \
1543 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
1545 *dst++ = c1 | 0x80, *dst++= c2 | 0x80; \
1548 else if (coding->flags & CODING_FLAG_ISO_SAFE \
1549 && !coding->safe_charsets[charset]) \
1551 /* We should not encode this character, instead produce one or \
1553 *dst++ = CODING_INHIBIT_CHARACTER_SUBSTITUTION; \
1554 if (CHARSET_WIDTH (charset) == 2) \
1555 *dst++ = CODING_INHIBIT_CHARACTER_SUBSTITUTION; \
1559 /* Since CHARSET is not yet invoked to any graphic planes, we \
1560 must invoke it, or, at first, designate it to some graphic \
1561 register. Then repeat the loop to actually produce the \
1563 dst = encode_invocation_designation (charset, coding, dst); \
1566 #define ENCODE_ISO_CHARACTER(charset, c1, c2) \
1568 int c_alt, charset_alt; \
1569 if (!NILP (translation_table) \
1570 && ((c_alt = translate_char (translation_table, -1, \
1573 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
1575 charset_alt = charset; \
1576 if (CHARSET_DIMENSION (charset_alt) == 1) \
1578 if (charset == CHARSET_ASCII \
1579 && coding->flags & CODING_FLAG_ISO_USE_ROMAN) \
1580 charset_alt = charset_latin_jisx0201; \
1581 ENCODE_ISO_CHARACTER_DIMENSION1 (charset_alt, c1); \
1585 if (charset == charset_jisx0208 \
1586 && coding->flags & CODING_FLAG_ISO_USE_OLDJIS) \
1587 charset_alt = charset_jisx0208_1978; \
1588 ENCODE_ISO_CHARACTER_DIMENSION2 (charset_alt, c1, c2); \
1590 if (! COMPOSING_P (coding->composing)) \
1591 coding->consumed_char++; \
1594 /* Produce designation and invocation codes at a place pointed by DST
1595 to use CHARSET. The element `spec.iso2022' of *CODING is updated.
1599 encode_invocation_designation (charset
, coding
, dst
)
1601 struct coding_system
*coding
;
1604 int reg
; /* graphic register number */
1606 /* At first, check designations. */
1607 for (reg
= 0; reg
< 4; reg
++)
1608 if (charset
== CODING_SPEC_ISO_DESIGNATION (coding
, reg
))
1613 /* CHARSET is not yet designated to any graphic registers. */
1614 /* At first check the requested designation. */
1615 reg
= CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
);
1616 if (reg
== CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
)
1617 /* Since CHARSET requests no special designation, designate it
1618 to graphic register 0. */
1621 ENCODE_DESIGNATION (charset
, reg
, coding
);
1624 if (CODING_SPEC_ISO_INVOCATION (coding
, 0) != reg
1625 && CODING_SPEC_ISO_INVOCATION (coding
, 1) != reg
)
1627 /* Since the graphic register REG is not invoked to any graphic
1628 planes, invoke it to graphic plane 0. */
1631 case 0: /* graphic register 0 */
1635 case 1: /* graphic register 1 */
1639 case 2: /* graphic register 2 */
1640 if (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
1641 ENCODE_SINGLE_SHIFT_2
;
1643 ENCODE_LOCKING_SHIFT_2
;
1646 case 3: /* graphic register 3 */
1647 if (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
1648 ENCODE_SINGLE_SHIFT_3
;
1650 ENCODE_LOCKING_SHIFT_3
;
1657 /* The following two macros produce codes for indicating composition. */
1658 #define ENCODE_COMPOSITION_NO_RULE_START *dst++ = ISO_CODE_ESC, *dst++ = '0'
1659 #define ENCODE_COMPOSITION_WITH_RULE_START *dst++ = ISO_CODE_ESC, *dst++ = '2'
1660 #define ENCODE_COMPOSITION_END *dst++ = ISO_CODE_ESC, *dst++ = '1'
1662 /* The following three macros produce codes for indicating direction
1664 #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
1666 if (coding->flags == CODING_FLAG_ISO_SEVEN_BITS) \
1667 *dst++ = ISO_CODE_ESC, *dst++ = '['; \
1669 *dst++ = ISO_CODE_CSI; \
1672 #define ENCODE_DIRECTION_R2L \
1673 ENCODE_CONTROL_SEQUENCE_INTRODUCER, *dst++ = '2', *dst++ = ']'
1675 #define ENCODE_DIRECTION_L2R \
1676 ENCODE_CONTROL_SEQUENCE_INTRODUCER, *dst++ = '0', *dst++ = ']'
1678 /* Produce codes for designation and invocation to reset the graphic
1679 planes and registers to initial state. */
1680 #define ENCODE_RESET_PLANE_AND_REGISTER \
1683 if (CODING_SPEC_ISO_INVOCATION (coding, 0) != 0) \
1685 for (reg = 0; reg < 4; reg++) \
1686 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg) >= 0 \
1687 && (CODING_SPEC_ISO_DESIGNATION (coding, reg) \
1688 != CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg))) \
1689 ENCODE_DESIGNATION \
1690 (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg), reg, coding); \
1693 /* Produce designation sequences of charsets in the line started from
1694 SRC to a place pointed by *DSTP, and update DSTP.
1696 If the current block ends before any end-of-line, we may fail to
1697 find all the necessary designations. */
1700 encode_designation_at_bol (coding
, table
, src
, src_end
, dstp
)
1701 struct coding_system
*coding
;
1703 unsigned char *src
, *src_end
, **dstp
;
1705 int charset
, c
, found
= 0, reg
;
1706 /* Table of charsets to be designated to each graphic register. */
1708 unsigned char *dst
= *dstp
;
1710 for (reg
= 0; reg
< 4; reg
++)
1713 while (src
< src_end
&& *src
!= '\n' && found
< 4)
1715 int bytes
= BYTES_BY_CHAR_HEAD (*src
);
1718 charset
= CHARSET_AT (src
);
1722 unsigned char c1
, c2
;
1724 SPLIT_STRING(src
, bytes
, charset
, c1
, c2
);
1725 if ((c_alt
= translate_char (table
, -1, charset
, c1
, c2
)) >= 0)
1726 charset
= CHAR_CHARSET (c_alt
);
1729 reg
= CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
);
1730 if (reg
!= CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
&& r
[reg
] < 0)
1741 for (reg
= 0; reg
< 4; reg
++)
1743 && CODING_SPEC_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
1744 ENCODE_DESIGNATION (r
[reg
], reg
, coding
);
1749 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
1752 encode_coding_iso2022 (coding
, source
, destination
, src_bytes
, dst_bytes
)
1753 struct coding_system
*coding
;
1754 unsigned char *source
, *destination
;
1755 int src_bytes
, dst_bytes
;
1757 unsigned char *src
= source
;
1758 unsigned char *src_end
= source
+ src_bytes
;
1759 unsigned char *dst
= destination
;
1760 unsigned char *dst_end
= destination
+ dst_bytes
;
1761 /* Since the maximum bytes produced by each loop is 20, we subtract 19
1762 from DST_END to assure overflow checking is necessary only at the
1764 unsigned char *adjusted_dst_end
= dst_end
- 19;
1765 Lisp_Object translation_table
1766 = coding
->translation_table_for_encode
;
1767 int result
= CODING_FINISH_NORMAL
;
1769 if (!NILP (Venable_character_translation
) && NILP (translation_table
))
1770 translation_table
= Vstandard_translation_table_for_encode
;
1772 coding
->consumed_char
= 0;
1773 coding
->fake_multibyte
= 0;
1774 while (src
< src_end
&& (dst_bytes
1775 ? (dst
< adjusted_dst_end
)
1776 : (dst
< src
- 19)))
1778 /* SRC_BASE remembers the start position in source in each loop.
1779 The loop will be exited when there's not enough source text
1780 to analyze multi-byte codes (within macros ONE_MORE_BYTE,
1781 TWO_MORE_BYTES, and THREE_MORE_BYTES). In that case, SRC is
1782 reset to SRC_BASE before exiting. */
1783 unsigned char *src_base
= src
;
1784 int charset
, c1
, c2
, c3
, c4
;
1786 if (coding
->flags
& CODING_FLAG_ISO_DESIGNATE_AT_BOL
1787 && CODING_SPEC_ISO_BOL (coding
))
1789 /* We have to produce designation sequences if any now. */
1790 encode_designation_at_bol (coding
, translation_table
,
1791 src
, src_end
, &dst
);
1792 CODING_SPEC_ISO_BOL (coding
) = 0;
1796 /* If we are seeing a component of a composite character, we are
1797 seeing a leading-code encoded irregularly for composition, or
1798 a composition rule if composing with rule. We must set C1 to
1799 a normal leading-code or an ASCII code. If we are not seeing
1800 a composite character, we must reset composition,
1801 designation, and invocation states. */
1802 if (COMPOSING_P (coding
->composing
))
1806 /* We are not in a composite character any longer. */
1807 coding
->composing
= COMPOSING_NO
;
1808 ENCODE_RESET_PLANE_AND_REGISTER
;
1809 ENCODE_COMPOSITION_END
;
1813 if (coding
->composing
== COMPOSING_WITH_RULE_RULE
)
1816 coding
->composing
= COMPOSING_WITH_RULE_HEAD
;
1819 else if (coding
->composing
== COMPOSING_WITH_RULE_HEAD
)
1820 coding
->composing
= COMPOSING_WITH_RULE_RULE
;
1823 /* This is an ASCII component. */
1828 /* This is a leading-code of non ASCII component. */
1833 /* Now encode one character. C1 is a control character, an
1834 ASCII character, or a leading-code of multi-byte character. */
1835 switch (emacs_code_class
[c1
])
1837 case EMACS_ascii_code
:
1838 ENCODE_ISO_CHARACTER (CHARSET_ASCII
, c1
, /* dummy */ c2
);
1841 case EMACS_control_code
:
1842 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_CNTL
)
1843 ENCODE_RESET_PLANE_AND_REGISTER
;
1845 coding
->consumed_char
++;
1848 case EMACS_carriage_return_code
:
1849 if (! (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
1851 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_CNTL
)
1852 ENCODE_RESET_PLANE_AND_REGISTER
;
1854 coding
->consumed_char
++;
1857 /* fall down to treat '\r' as '\n' ... */
1859 case EMACS_linefeed_code
:
1860 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_EOL
)
1861 ENCODE_RESET_PLANE_AND_REGISTER
;
1862 if (coding
->flags
& CODING_FLAG_ISO_INIT_AT_BOL
)
1863 bcopy (coding
->spec
.iso2022
.initial_designation
,
1864 coding
->spec
.iso2022
.current_designation
,
1865 sizeof coding
->spec
.iso2022
.initial_designation
);
1866 if (coding
->eol_type
== CODING_EOL_LF
1867 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
1868 *dst
++ = ISO_CODE_LF
;
1869 else if (coding
->eol_type
== CODING_EOL_CRLF
)
1870 *dst
++ = ISO_CODE_CR
, *dst
++ = ISO_CODE_LF
;
1872 *dst
++ = ISO_CODE_CR
;
1873 CODING_SPEC_ISO_BOL (coding
) = 1;
1874 coding
->consumed_char
++;
1877 case EMACS_leading_code_2
:
1881 /* invalid sequence */
1884 coding
->consumed_char
++;
1887 ENCODE_ISO_CHARACTER (c1
, c2
, /* dummy */ c3
);
1890 case EMACS_leading_code_3
:
1891 TWO_MORE_BYTES (c2
, c3
);
1892 if (c2
< 0xA0 || c3
< 0xA0)
1894 /* invalid sequence */
1897 coding
->consumed_char
++;
1899 else if (c1
< LEADING_CODE_PRIVATE_11
)
1900 ENCODE_ISO_CHARACTER (c1
, c2
, c3
);
1902 ENCODE_ISO_CHARACTER (c2
, c3
, /* dummy */ c4
);
1905 case EMACS_leading_code_4
:
1906 THREE_MORE_BYTES (c2
, c3
, c4
);
1907 if (c2
< 0xA0 || c3
< 0xA0 || c4
< 0xA0)
1909 /* invalid sequence */
1912 coding
->consumed_char
++;
1915 ENCODE_ISO_CHARACTER (c2
, c3
, c4
);
1918 case EMACS_leading_code_composition
:
1922 /* invalid sequence */
1925 coding
->consumed_char
++;
1927 else if (c2
== 0xFF)
1929 ENCODE_RESET_PLANE_AND_REGISTER
;
1930 coding
->composing
= COMPOSING_WITH_RULE_HEAD
;
1931 ENCODE_COMPOSITION_WITH_RULE_START
;
1932 coding
->consumed_char
++;
1936 ENCODE_RESET_PLANE_AND_REGISTER
;
1937 /* Rewind one byte because it is a character code of
1938 composition elements. */
1940 coding
->composing
= COMPOSING_NO_RULE_HEAD
;
1941 ENCODE_COMPOSITION_NO_RULE_START
;
1942 coding
->consumed_char
++;
1946 case EMACS_invalid_code
:
1948 coding
->consumed_char
++;
1953 result
= CODING_FINISH_INSUFFICIENT_SRC
;
1958 if (src
< src_end
&& result
== CODING_FINISH_NORMAL
)
1959 result
= CODING_FINISH_INSUFFICIENT_DST
;
1961 /* If this is the last block of the text to be encoded, we must
1962 reset graphic planes and registers to the initial state, and
1963 flush out the carryover if any. */
1964 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
1966 ENCODE_RESET_PLANE_AND_REGISTER
;
1967 if (COMPOSING_P (coding
->composing
))
1968 ENCODE_COMPOSITION_END
;
1970 coding
->consumed
= src
- source
;
1971 coding
->produced
= coding
->produced_char
= dst
- destination
;
1976 /*** 4. SJIS and BIG5 handlers ***/
1978 /* Although SJIS and BIG5 are not ISO's coding system, they are used
1979 quite widely. So, for the moment, Emacs supports them in the bare
1980 C code. But, in the future, they may be supported only by CCL. */
1982 /* SJIS is a coding system encoding three character sets: ASCII, right
1983 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
1984 as is. A character of charset katakana-jisx0201 is encoded by
1985 "position-code + 0x80". A character of charset japanese-jisx0208
1986 is encoded in 2-byte but two position-codes are divided and shifted
1987 so that it fit in the range below.
1989 --- CODE RANGE of SJIS ---
1990 (character set) (range)
1992 KATAKANA-JISX0201 0xA0 .. 0xDF
1993 JISX0208 (1st byte) 0x80 .. 0x9F and 0xE0 .. 0xFF
1994 (2nd byte) 0x40 .. 0xFF
1995 -------------------------------
1999 /* BIG5 is a coding system encoding two character sets: ASCII and
2000 Big5. An ASCII character is encoded as is. Big5 is a two-byte
2001 character set and is encoded in two-byte.
2003 --- CODE RANGE of BIG5 ---
2004 (character set) (range)
2006 Big5 (1st byte) 0xA1 .. 0xFE
2007 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
2008 --------------------------
2010 Since the number of characters in Big5 is larger than maximum
2011 characters in Emacs' charset (96x96), it can't be handled as one
2012 charset. So, in Emacs, Big5 is divided into two: `charset-big5-1'
2013 and `charset-big5-2'. Both are DIMENSION2 and CHARS94. The former
2014 contains frequently used characters and the latter contains less
2015 frequently used characters. */
2017 /* Macros to decode or encode a character of Big5 in BIG5. B1 and B2
2018 are the 1st and 2nd position-codes of Big5 in BIG5 coding system.
2019 C1 and C2 are the 1st and 2nd position-codes of of Emacs' internal
2020 format. CHARSET is `charset_big5_1' or `charset_big5_2'. */
2022 /* Number of Big5 characters which have the same code in 1st byte. */
2023 #define BIG5_SAME_ROW (0xFF - 0xA1 + 0x7F - 0x40)
2025 #define DECODE_BIG5(b1, b2, charset, c1, c2) \
2028 = (b1 - 0xA1) * BIG5_SAME_ROW + b2 - (b2 < 0x7F ? 0x40 : 0x62); \
2030 charset = charset_big5_1; \
2033 charset = charset_big5_2; \
2034 temp -= (0xC9 - 0xA1) * BIG5_SAME_ROW; \
2036 c1 = temp / (0xFF - 0xA1) + 0x21; \
2037 c2 = temp % (0xFF - 0xA1) + 0x21; \
2040 #define ENCODE_BIG5(charset, c1, c2, b1, b2) \
2042 unsigned int temp = (c1 - 0x21) * (0xFF - 0xA1) + (c2 - 0x21); \
2043 if (charset == charset_big5_2) \
2044 temp += BIG5_SAME_ROW * (0xC9 - 0xA1); \
2045 b1 = temp / BIG5_SAME_ROW + 0xA1; \
2046 b2 = temp % BIG5_SAME_ROW; \
2047 b2 += b2 < 0x3F ? 0x40 : 0x62; \
2050 #define DECODE_SJIS_BIG5_CHARACTER(charset, c1, c2) \
2052 int c_alt, charset_alt = (charset); \
2053 if (!NILP (translation_table) \
2054 && ((c_alt = translate_char (translation_table, \
2055 -1, (charset), c1, c2)) >= 0)) \
2056 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
2057 if (charset_alt == CHARSET_ASCII || charset_alt < 0) \
2058 DECODE_CHARACTER_ASCII (c1); \
2059 else if (CHARSET_DIMENSION (charset_alt) == 1) \
2060 DECODE_CHARACTER_DIMENSION1 (charset_alt, c1); \
2062 DECODE_CHARACTER_DIMENSION2 (charset_alt, c1, c2); \
2065 #define ENCODE_SJIS_BIG5_CHARACTER(charset, c1, c2) \
2067 int c_alt, charset_alt; \
2068 if (!NILP (translation_table) \
2069 && ((c_alt = translate_char (translation_table, -1, \
2072 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
2074 charset_alt = charset; \
2075 if (charset_alt == charset_ascii) \
2077 else if (CHARSET_DIMENSION (charset_alt) == 1) \
2079 if (sjis_p && charset_alt == charset_katakana_jisx0201) \
2083 *dst++ = charset_alt, *dst++ = c1; \
2084 coding->fake_multibyte = 1; \
2089 c1 &= 0x7F, c2 &= 0x7F; \
2090 if (sjis_p && charset_alt == charset_jisx0208) \
2092 unsigned char s1, s2; \
2094 ENCODE_SJIS (c1, c2, s1, s2); \
2095 *dst++ = s1, *dst++ = s2; \
2096 coding->fake_multibyte = 1; \
2099 && (charset_alt == charset_big5_1 \
2100 || charset_alt == charset_big5_2)) \
2102 unsigned char b1, b2; \
2104 ENCODE_BIG5 (charset_alt, c1, c2, b1, b2); \
2105 *dst++ = b1, *dst++ = b2; \
2109 *dst++ = charset_alt, *dst++ = c1, *dst++ = c2; \
2110 coding->fake_multibyte = 1; \
2113 coding->consumed_char++; \
2116 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2117 Check if a text is encoded in SJIS. If it is, return
2118 CODING_CATEGORY_MASK_SJIS, else return 0. */
2121 detect_coding_sjis (src
, src_end
)
2122 unsigned char *src
, *src_end
;
2126 while (src
< src_end
)
2129 if ((c
>= 0x80 && c
< 0xA0) || c
>= 0xE0)
2131 if (src
< src_end
&& *src
++ < 0x40)
2135 return CODING_CATEGORY_MASK_SJIS
;
2138 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2139 Check if a text is encoded in BIG5. If it is, return
2140 CODING_CATEGORY_MASK_BIG5, else return 0. */
2143 detect_coding_big5 (src
, src_end
)
2144 unsigned char *src
, *src_end
;
2148 while (src
< src_end
)
2156 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
2160 return CODING_CATEGORY_MASK_BIG5
;
2163 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
2164 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
2167 decode_coding_sjis_big5 (coding
, source
, destination
,
2168 src_bytes
, dst_bytes
, sjis_p
)
2169 struct coding_system
*coding
;
2170 unsigned char *source
, *destination
;
2171 int src_bytes
, dst_bytes
;
2174 unsigned char *src
= source
;
2175 unsigned char *src_end
= source
+ src_bytes
;
2176 unsigned char *dst
= destination
;
2177 unsigned char *dst_end
= destination
+ dst_bytes
;
2178 /* Since the maximum bytes produced by each loop is 4, we subtract 3
2179 from DST_END to assure overflow checking is necessary only at the
2181 unsigned char *adjusted_dst_end
= dst_end
- 3;
2182 Lisp_Object translation_table
2183 = coding
->translation_table_for_decode
;
2184 int result
= CODING_FINISH_NORMAL
;
2186 if (!NILP (Venable_character_translation
) && NILP (translation_table
))
2187 translation_table
= Vstandard_translation_table_for_decode
;
2189 coding
->produced_char
= 0;
2190 coding
->fake_multibyte
= 0;
2191 while (src
< src_end
&& (dst_bytes
2192 ? (dst
< adjusted_dst_end
)
2195 /* SRC_BASE remembers the start position in source in each loop.
2196 The loop will be exited when there's not enough source text
2197 to analyze two-byte character (within macro ONE_MORE_BYTE).
2198 In that case, SRC is reset to SRC_BASE before exiting. */
2199 unsigned char *src_base
= src
;
2200 unsigned char c1
= *src
++, c2
, c3
, c4
;
2206 if (coding
->eol_type
== CODING_EOL_CRLF
)
2211 else if (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
)
2213 result
= CODING_FINISH_INCONSISTENT_EOL
;
2214 goto label_end_of_loop_2
;
2217 /* To process C2 again, SRC is subtracted by 1. */
2220 else if (coding
->eol_type
== CODING_EOL_CR
)
2226 && (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
)
2227 && (coding
->eol_type
== CODING_EOL_CR
2228 || coding
->eol_type
== CODING_EOL_CRLF
))
2230 result
= CODING_FINISH_INCONSISTENT_EOL
;
2231 goto label_end_of_loop_2
;
2235 coding
->produced_char
++;
2238 DECODE_SJIS_BIG5_CHARACTER (charset_ascii
, c1
, /* dummy */ c2
);
2241 /* SJIS -> JISX0208 */
2247 DECODE_SJIS (c1
, c2
, c3
, c4
);
2248 DECODE_SJIS_BIG5_CHARACTER (charset_jisx0208
, c3
, c4
);
2251 goto label_invalid_code_2
;
2254 goto label_invalid_code_1
;
2258 /* SJIS -> JISX0201-Kana, BIG5 -> Big5 */
2260 DECODE_SJIS_BIG5_CHARACTER (charset_katakana_jisx0201
, c1
,
2267 if ((c2
>= 0x40 && c2
<= 0x7E) || (c2
>= 0xA1 && c2
<= 0xFE))
2269 DECODE_BIG5 (c1
, c2
, charset
, c3
, c4
);
2270 DECODE_SJIS_BIG5_CHARACTER (charset
, c3
, c4
);
2273 goto label_invalid_code_2
;
2276 else /* C1 >= 0xE0 */
2278 /* SJIS -> JISX0208, BIG5 -> Big5 */
2284 DECODE_SJIS (c1
, c2
, c3
, c4
);
2285 DECODE_SJIS_BIG5_CHARACTER (charset_jisx0208
, c3
, c4
);
2288 goto label_invalid_code_2
;
2295 if ((c2
>= 0x40 && c2
<= 0x7E) || (c2
>= 0xA1 && c2
<= 0xFE))
2297 DECODE_BIG5 (c1
, c2
, charset
, c3
, c4
);
2298 DECODE_SJIS_BIG5_CHARACTER (charset
, c3
, c4
);
2301 goto label_invalid_code_2
;
2306 label_invalid_code_1
:
2308 coding
->produced_char
++;
2309 coding
->fake_multibyte
= 1;
2312 label_invalid_code_2
:
2313 *dst
++ = c1
; *dst
++= c2
;
2314 coding
->produced_char
+= 2;
2315 coding
->fake_multibyte
= 1;
2319 result
= CODING_FINISH_INSUFFICIENT_SRC
;
2320 label_end_of_loop_2
:
2327 if (result
== CODING_FINISH_NORMAL
)
2328 result
= CODING_FINISH_INSUFFICIENT_DST
;
2329 else if (result
!= CODING_FINISH_INCONSISTENT_EOL
2330 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
2332 src_bytes
= src_end
- src
;
2333 if (dst_bytes
&& (dst_end
- dst
< src_bytes
))
2334 src_bytes
= dst_end
- dst
;
2335 bcopy (dst
, src
, src_bytes
);
2338 coding
->fake_multibyte
= 1;
2342 coding
->consumed
= coding
->consumed_char
= src
- source
;
2343 coding
->produced
= dst
- destination
;
2347 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
2348 This function can encode `charset_ascii', `charset_katakana_jisx0201',
2349 `charset_jisx0208', `charset_big5_1', and `charset_big5-2'. We are
2350 sure that all these charsets are registered as official charset
2351 (i.e. do not have extended leading-codes). Characters of other
2352 charsets are produced without any encoding. If SJIS_P is 1, encode
2353 SJIS text, else encode BIG5 text. */
2356 encode_coding_sjis_big5 (coding
, source
, destination
,
2357 src_bytes
, dst_bytes
, sjis_p
)
2358 struct coding_system
*coding
;
2359 unsigned char *source
, *destination
;
2360 int src_bytes
, dst_bytes
;
2363 unsigned char *src
= source
;
2364 unsigned char *src_end
= source
+ src_bytes
;
2365 unsigned char *dst
= destination
;
2366 unsigned char *dst_end
= destination
+ dst_bytes
;
2367 /* Since the maximum bytes produced by each loop is 2, we subtract 1
2368 from DST_END to assure overflow checking is necessary only at the
2370 unsigned char *adjusted_dst_end
= dst_end
- 1;
2371 Lisp_Object translation_table
2372 = coding
->translation_table_for_encode
;
2373 int result
= CODING_FINISH_NORMAL
;
2375 if (!NILP (Venable_character_translation
) && NILP (translation_table
))
2376 translation_table
= Vstandard_translation_table_for_encode
;
2378 coding
->consumed_char
= 0;
2379 coding
->fake_multibyte
= 0;
2380 while (src
< src_end
&& (dst_bytes
2381 ? (dst
< adjusted_dst_end
)
2384 /* SRC_BASE remembers the start position in source in each loop.
2385 The loop will be exited when there's not enough source text
2386 to analyze multi-byte codes (within macros ONE_MORE_BYTE and
2387 TWO_MORE_BYTES). In that case, SRC is reset to SRC_BASE
2389 unsigned char *src_base
= src
;
2390 unsigned char c1
= *src
++, c2
, c3
, c4
;
2392 if (coding
->composing
)
2399 else if (c1
>= 0xA0)
2402 coding
->composing
= 0;
2405 switch (emacs_code_class
[c1
])
2407 case EMACS_ascii_code
:
2408 ENCODE_SJIS_BIG5_CHARACTER (charset_ascii
, c1
, /* dummy */ c2
);
2411 case EMACS_control_code
:
2413 coding
->consumed_char
++;
2416 case EMACS_carriage_return_code
:
2417 if (! (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
2420 coding
->consumed_char
++;
2423 /* fall down to treat '\r' as '\n' ... */
2425 case EMACS_linefeed_code
:
2426 if (coding
->eol_type
== CODING_EOL_LF
2427 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
2429 else if (coding
->eol_type
== CODING_EOL_CRLF
)
2430 *dst
++ = '\r', *dst
++ = '\n';
2433 coding
->consumed_char
++;
2436 case EMACS_leading_code_2
:
2438 ENCODE_SJIS_BIG5_CHARACTER (c1
, c2
, /* dummy */ c3
);
2441 case EMACS_leading_code_3
:
2442 TWO_MORE_BYTES (c2
, c3
);
2443 ENCODE_SJIS_BIG5_CHARACTER (c1
, c2
, c3
);
2446 case EMACS_leading_code_4
:
2447 THREE_MORE_BYTES (c2
, c3
, c4
);
2448 ENCODE_SJIS_BIG5_CHARACTER (c2
, c3
, c4
);
2451 case EMACS_leading_code_composition
:
2452 coding
->composing
= 1;
2455 default: /* i.e. case EMACS_invalid_code: */
2457 coding
->consumed_char
++;
2462 result
= CODING_FINISH_INSUFFICIENT_SRC
;
2467 if (result
== CODING_FINISH_NORMAL
2469 result
= CODING_FINISH_INSUFFICIENT_DST
;
2470 coding
->consumed
= src
- source
;
2471 coding
->produced
= coding
->produced_char
= dst
- destination
;
2476 /*** 5. End-of-line handlers ***/
2478 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
2479 This function is called only when `coding->eol_type' is
2480 CODING_EOL_CRLF or CODING_EOL_CR. */
2483 decode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
)
2484 struct coding_system
*coding
;
2485 unsigned char *source
, *destination
;
2486 int src_bytes
, dst_bytes
;
2488 unsigned char *src
= source
;
2489 unsigned char *src_end
= source
+ src_bytes
;
2490 unsigned char *dst
= destination
;
2491 unsigned char *dst_end
= destination
+ dst_bytes
;
2493 int result
= CODING_FINISH_NORMAL
;
2495 coding
->fake_multibyte
= 0;
2500 switch (coding
->eol_type
)
2502 case CODING_EOL_CRLF
:
2504 /* Since the maximum bytes produced by each loop is 2, we
2505 subtract 1 from DST_END to assure overflow checking is
2506 necessary only at the head of loop. */
2507 unsigned char *adjusted_dst_end
= dst_end
- 1;
2509 while (src
< src_end
&& (dst_bytes
2510 ? (dst
< adjusted_dst_end
)
2513 unsigned char *src_base
= src
;
2521 if (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
)
2523 result
= CODING_FINISH_INCONSISTENT_EOL
;
2524 goto label_end_of_loop_2
;
2527 if (BASE_LEADING_CODE_P (c
))
2528 coding
->fake_multibyte
= 1;
2533 && (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
))
2535 result
= CODING_FINISH_INCONSISTENT_EOL
;
2536 goto label_end_of_loop_2
;
2541 if (BASE_LEADING_CODE_P (c
))
2542 coding
->fake_multibyte
= 1;
2547 result
= CODING_FINISH_INSUFFICIENT_SRC
;
2548 label_end_of_loop_2
:
2552 if (result
== CODING_FINISH_NORMAL
2554 result
= CODING_FINISH_INSUFFICIENT_DST
;
2559 if (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
)
2561 while (src
< src_end
)
2563 if ((c
= *src
++) == '\n')
2565 if (BASE_LEADING_CODE_P (c
))
2566 coding
->fake_multibyte
= 1;
2570 src_bytes
= src
- source
;
2571 result
= CODING_FINISH_INCONSISTENT_EOL
;
2574 if (dst_bytes
&& src_bytes
> dst_bytes
)
2576 result
= CODING_FINISH_INSUFFICIENT_DST
;
2577 src_bytes
= dst_bytes
;
2580 bcopy (source
, destination
, src_bytes
);
2582 safe_bcopy (source
, destination
, src_bytes
);
2583 src
= source
+ src_bytes
;
2584 while (src_bytes
--) if (*dst
++ == '\r') dst
[-1] = '\n';
2587 default: /* i.e. case: CODING_EOL_LF */
2588 if (dst_bytes
&& src_bytes
> dst_bytes
)
2590 result
= CODING_FINISH_INSUFFICIENT_DST
;
2591 src_bytes
= dst_bytes
;
2594 bcopy (source
, destination
, src_bytes
);
2596 safe_bcopy (source
, destination
, src_bytes
);
2599 coding
->fake_multibyte
= 1;
2603 coding
->consumed
= coding
->consumed_char
= src
- source
;
2604 coding
->produced
= coding
->produced_char
= dst
- destination
;
2608 /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". Encode
2609 format of end-of-line according to `coding->eol_type'. If
2610 `coding->mode & CODING_MODE_SELECTIVE_DISPLAY' is nonzero, code
2611 '\r' in source text also means end-of-line. */
2614 encode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
)
2615 struct coding_system
*coding
;
2616 unsigned char *source
, *destination
;
2617 int src_bytes
, dst_bytes
;
2619 unsigned char *src
= source
;
2620 unsigned char *dst
= destination
;
2621 int result
= CODING_FINISH_NORMAL
;
2623 coding
->fake_multibyte
= 0;
2625 if (coding
->eol_type
== CODING_EOL_CRLF
)
2628 unsigned char *src_end
= source
+ src_bytes
;
2629 unsigned char *dst_end
= destination
+ dst_bytes
;
2630 /* Since the maximum bytes produced by each loop is 2, we
2631 subtract 1 from DST_END to assure overflow checking is
2632 necessary only at the head of loop. */
2633 unsigned char *adjusted_dst_end
= dst_end
- 1;
2635 while (src
< src_end
&& (dst_bytes
2636 ? (dst
< adjusted_dst_end
)
2641 || (c
== '\r' && (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
)))
2642 *dst
++ = '\r', *dst
++ = '\n';
2646 if (BASE_LEADING_CODE_P (c
))
2647 coding
->fake_multibyte
= 1;
2651 result
= CODING_FINISH_INSUFFICIENT_DST
;
2657 if (dst_bytes
&& src_bytes
> dst_bytes
)
2659 src_bytes
= dst_bytes
;
2660 result
= CODING_FINISH_INSUFFICIENT_DST
;
2663 bcopy (source
, destination
, src_bytes
);
2666 safe_bcopy (source
, destination
, src_bytes
);
2667 dst_bytes
= src_bytes
;
2669 if (coding
->eol_type
== CODING_EOL_CRLF
)
2673 if ((c
= *dst
++) == '\n')
2675 else if (BASE_LEADING_CODE_P (c
))
2676 coding
->fake_multibyte
= 1;
2681 if (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
)
2684 if (*dst
++ == '\r') dst
[-1] = '\n';
2686 coding
->fake_multibyte
= 1;
2688 src
= source
+ dst_bytes
;
2689 dst
= destination
+ dst_bytes
;
2692 coding
->consumed
= coding
->consumed_char
= src
- source
;
2693 coding
->produced
= coding
->produced_char
= dst
- destination
;
2698 /*** 6. C library functions ***/
2700 /* In Emacs Lisp, coding system is represented by a Lisp symbol which
2701 has a property `coding-system'. The value of this property is a
2702 vector of length 5 (called as coding-vector). Among elements of
2703 this vector, the first (element[0]) and the fifth (element[4])
2704 carry important information for decoding/encoding. Before
2705 decoding/encoding, this information should be set in fields of a
2706 structure of type `coding_system'.
2708 A value of property `coding-system' can be a symbol of another
2709 subsidiary coding-system. In that case, Emacs gets coding-vector
2712 `element[0]' contains information to be set in `coding->type'. The
2713 value and its meaning is as follows:
2715 0 -- coding_type_emacs_mule
2716 1 -- coding_type_sjis
2717 2 -- coding_type_iso2022
2718 3 -- coding_type_big5
2719 4 -- coding_type_ccl encoder/decoder written in CCL
2720 nil -- coding_type_no_conversion
2721 t -- coding_type_undecided (automatic conversion on decoding,
2722 no-conversion on encoding)
2724 `element[4]' contains information to be set in `coding->flags' and
2725 `coding->spec'. The meaning varies by `coding->type'.
2727 If `coding->type' is `coding_type_iso2022', element[4] is a vector
2728 of length 32 (of which the first 13 sub-elements are used now).
2729 Meanings of these sub-elements are:
2731 sub-element[N] where N is 0 through 3: to be set in `coding->spec.iso2022'
2732 If the value is an integer of valid charset, the charset is
2733 assumed to be designated to graphic register N initially.
2735 If the value is minus, it is a minus value of charset which
2736 reserves graphic register N, which means that the charset is
2737 not designated initially but should be designated to graphic
2738 register N just before encoding a character in that charset.
2740 If the value is nil, graphic register N is never used on
2743 sub-element[N] where N is 4 through 11: to be set in `coding->flags'
2744 Each value takes t or nil. See the section ISO2022 of
2745 `coding.h' for more information.
2747 If `coding->type' is `coding_type_big5', element[4] is t to denote
2748 BIG5-ETen or nil to denote BIG5-HKU.
2750 If `coding->type' takes the other value, element[4] is ignored.
2752 Emacs Lisp's coding system also carries information about format of
2753 end-of-line in a value of property `eol-type'. If the value is
2754 integer, 0 means CODING_EOL_LF, 1 means CODING_EOL_CRLF, and 2
2755 means CODING_EOL_CR. If it is not integer, it should be a vector
2756 of subsidiary coding systems of which property `eol-type' has one
2761 /* Extract information for decoding/encoding from CODING_SYSTEM_SYMBOL
2762 and set it in CODING. If CODING_SYSTEM_SYMBOL is invalid, CODING
2763 is setup so that no conversion is necessary and return -1, else
2767 setup_coding_system (coding_system
, coding
)
2768 Lisp_Object coding_system
;
2769 struct coding_system
*coding
;
2771 Lisp_Object coding_spec
, coding_type
, eol_type
, plist
;
2775 /* Initialize some fields required for all kinds of coding systems. */
2776 coding
->symbol
= coding_system
;
2777 coding
->common_flags
= 0;
2779 coding
->heading_ascii
= -1;
2780 coding
->post_read_conversion
= coding
->pre_write_conversion
= Qnil
;
2781 coding_spec
= Fget (coding_system
, Qcoding_system
);
2782 if (!VECTORP (coding_spec
)
2783 || XVECTOR (coding_spec
)->size
!= 5
2784 || !CONSP (XVECTOR (coding_spec
)->contents
[3]))
2785 goto label_invalid_coding_system
;
2787 eol_type
= inhibit_eol_conversion
? Qnil
: Fget (coding_system
, Qeol_type
);
2788 if (VECTORP (eol_type
))
2790 coding
->eol_type
= CODING_EOL_UNDECIDED
;
2791 coding
->common_flags
= CODING_REQUIRE_DETECTION_MASK
;
2793 else if (XFASTINT (eol_type
) == 1)
2795 coding
->eol_type
= CODING_EOL_CRLF
;
2796 coding
->common_flags
2797 = CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
;
2799 else if (XFASTINT (eol_type
) == 2)
2801 coding
->eol_type
= CODING_EOL_CR
;
2802 coding
->common_flags
2803 = CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
;
2806 coding
->eol_type
= CODING_EOL_LF
;
2808 coding_type
= XVECTOR (coding_spec
)->contents
[0];
2809 /* Try short cut. */
2810 if (SYMBOLP (coding_type
))
2812 if (EQ (coding_type
, Qt
))
2814 coding
->type
= coding_type_undecided
;
2815 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
2818 coding
->type
= coding_type_no_conversion
;
2822 /* Initialize remaining fields. */
2823 coding
->composing
= 0;
2824 coding
->translation_table_for_decode
= Qnil
;
2825 coding
->translation_table_for_encode
= Qnil
;
2827 /* Get values of coding system properties:
2828 `post-read-conversion', `pre-write-conversion',
2829 `translation-table-for-decode', `translation-table-for-encode'. */
2830 plist
= XVECTOR (coding_spec
)->contents
[3];
2831 coding
->post_read_conversion
= Fplist_get (plist
, Qpost_read_conversion
);
2832 coding
->pre_write_conversion
= Fplist_get (plist
, Qpre_write_conversion
);
2833 val
= Fplist_get (plist
, Qtranslation_table_for_decode
);
2835 val
= Fget (val
, Qtranslation_table_for_decode
);
2836 coding
->translation_table_for_decode
= CHAR_TABLE_P (val
) ? val
: Qnil
;
2837 val
= Fplist_get (plist
, Qtranslation_table_for_encode
);
2839 val
= Fget (val
, Qtranslation_table_for_encode
);
2840 coding
->translation_table_for_encode
= CHAR_TABLE_P (val
) ? val
: Qnil
;
2841 val
= Fplist_get (plist
, Qcoding_category
);
2844 val
= Fget (val
, Qcoding_category_index
);
2846 coding
->category_idx
= XINT (val
);
2848 goto label_invalid_coding_system
;
2851 goto label_invalid_coding_system
;
2853 val
= Fplist_get (plist
, Qsafe_charsets
);
2856 for (i
= 0; i
<= MAX_CHARSET
; i
++)
2857 coding
->safe_charsets
[i
] = 1;
2861 bzero (coding
->safe_charsets
, MAX_CHARSET
+ 1);
2864 if ((i
= get_charset_id (XCONS (val
)->car
)) >= 0)
2865 coding
->safe_charsets
[i
] = 1;
2866 val
= XCONS (val
)->cdr
;
2870 switch (XFASTINT (coding_type
))
2873 coding
->type
= coding_type_emacs_mule
;
2874 if (!NILP (coding
->post_read_conversion
))
2875 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
2876 if (!NILP (coding
->pre_write_conversion
))
2877 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
2881 coding
->type
= coding_type_sjis
;
2882 coding
->common_flags
2883 |= CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
;
2887 coding
->type
= coding_type_iso2022
;
2888 coding
->common_flags
2889 |= CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
;
2891 Lisp_Object val
, temp
;
2893 int i
, charset
, reg_bits
= 0;
2895 val
= XVECTOR (coding_spec
)->contents
[4];
2897 if (!VECTORP (val
) || XVECTOR (val
)->size
!= 32)
2898 goto label_invalid_coding_system
;
2900 flags
= XVECTOR (val
)->contents
;
2902 = ((NILP (flags
[4]) ? 0 : CODING_FLAG_ISO_SHORT_FORM
)
2903 | (NILP (flags
[5]) ? 0 : CODING_FLAG_ISO_RESET_AT_EOL
)
2904 | (NILP (flags
[6]) ? 0 : CODING_FLAG_ISO_RESET_AT_CNTL
)
2905 | (NILP (flags
[7]) ? 0 : CODING_FLAG_ISO_SEVEN_BITS
)
2906 | (NILP (flags
[8]) ? 0 : CODING_FLAG_ISO_LOCKING_SHIFT
)
2907 | (NILP (flags
[9]) ? 0 : CODING_FLAG_ISO_SINGLE_SHIFT
)
2908 | (NILP (flags
[10]) ? 0 : CODING_FLAG_ISO_USE_ROMAN
)
2909 | (NILP (flags
[11]) ? 0 : CODING_FLAG_ISO_USE_OLDJIS
)
2910 | (NILP (flags
[12]) ? 0 : CODING_FLAG_ISO_NO_DIRECTION
)
2911 | (NILP (flags
[13]) ? 0 : CODING_FLAG_ISO_INIT_AT_BOL
)
2912 | (NILP (flags
[14]) ? 0 : CODING_FLAG_ISO_DESIGNATE_AT_BOL
)
2913 | (NILP (flags
[15]) ? 0 : CODING_FLAG_ISO_SAFE
)
2914 | (NILP (flags
[16]) ? 0 : CODING_FLAG_ISO_LATIN_EXTRA
)
2917 /* Invoke graphic register 0 to plane 0. */
2918 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 0;
2919 /* Invoke graphic register 1 to plane 1 if we can use full 8-bit. */
2920 CODING_SPEC_ISO_INVOCATION (coding
, 1)
2921 = (coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
? -1 : 1);
2922 /* Not single shifting at first. */
2923 CODING_SPEC_ISO_SINGLE_SHIFTING (coding
) = 0;
2924 /* Beginning of buffer should also be regarded as bol. */
2925 CODING_SPEC_ISO_BOL (coding
) = 1;
2927 for (charset
= 0; charset
<= MAX_CHARSET
; charset
++)
2928 CODING_SPEC_ISO_REVISION_NUMBER (coding
, charset
) = 255;
2929 val
= Vcharset_revision_alist
;
2932 charset
= get_charset_id (Fcar_safe (XCONS (val
)->car
));
2934 && (temp
= Fcdr_safe (XCONS (val
)->car
), INTEGERP (temp
))
2935 && (i
= XINT (temp
), (i
>= 0 && (i
+ '@') < 128)))
2936 CODING_SPEC_ISO_REVISION_NUMBER (coding
, charset
) = i
;
2937 val
= XCONS (val
)->cdr
;
2940 /* Checks FLAGS[REG] (REG = 0, 1, 2 3) and decide designations.
2941 FLAGS[REG] can be one of below:
2942 integer CHARSET: CHARSET occupies register I,
2943 t: designate nothing to REG initially, but can be used
2945 list of integer, nil, or t: designate the first
2946 element (if integer) to REG initially, the remaining
2947 elements (if integer) is designated to REG on request,
2948 if an element is t, REG can be used by any charsets,
2949 nil: REG is never used. */
2950 for (charset
= 0; charset
<= MAX_CHARSET
; charset
++)
2951 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2952 = CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
;
2953 for (i
= 0; i
< 4; i
++)
2955 if (INTEGERP (flags
[i
])
2956 && (charset
= XINT (flags
[i
]), CHARSET_VALID_P (charset
))
2957 || (charset
= get_charset_id (flags
[i
])) >= 0)
2959 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = charset
;
2960 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
) = i
;
2962 else if (EQ (flags
[i
], Qt
))
2964 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
2966 coding
->flags
|= CODING_FLAG_ISO_DESIGNATION
;
2968 else if (CONSP (flags
[i
]))
2970 Lisp_Object tail
= flags
[i
];
2972 coding
->flags
|= CODING_FLAG_ISO_DESIGNATION
;
2973 if (INTEGERP (XCONS (tail
)->car
)
2974 && (charset
= XINT (XCONS (tail
)->car
),
2975 CHARSET_VALID_P (charset
))
2976 || (charset
= get_charset_id (XCONS (tail
)->car
)) >= 0)
2978 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = charset
;
2979 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
) =i
;
2982 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
2983 tail
= XCONS (tail
)->cdr
;
2984 while (CONSP (tail
))
2986 if (INTEGERP (XCONS (tail
)->car
)
2987 && (charset
= XINT (XCONS (tail
)->car
),
2988 CHARSET_VALID_P (charset
))
2989 || (charset
= get_charset_id (XCONS (tail
)->car
)) >= 0)
2990 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2992 else if (EQ (XCONS (tail
)->car
, Qt
))
2994 tail
= XCONS (tail
)->cdr
;
2998 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
3000 CODING_SPEC_ISO_DESIGNATION (coding
, i
)
3001 = CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
);
3004 if (reg_bits
&& ! (coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
))
3006 /* REG 1 can be used only by locking shift in 7-bit env. */
3007 if (coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
)
3009 if (! (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
))
3010 /* Without any shifting, only REG 0 and 1 can be used. */
3015 for (charset
= 0; charset
<= MAX_CHARSET
; charset
++)
3017 if (CHARSET_VALID_P (charset
))
3019 /* There exist some default graphic registers to be
3022 /* We had better avoid designating a charset of
3023 CHARS96 to REG 0 as far as possible. */
3024 if (CHARSET_CHARS (charset
) == 96)
3025 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
3027 ? 1 : (reg_bits
& 4 ? 2 : (reg_bits
& 8 ? 3 : 0)));
3029 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
3031 ? 0 : (reg_bits
& 2 ? 1 : (reg_bits
& 4 ? 2 : 3)));
3035 coding
->common_flags
|= CODING_REQUIRE_FLUSHING_MASK
;
3036 coding
->spec
.iso2022
.last_invalid_designation_register
= -1;
3040 coding
->type
= coding_type_big5
;
3041 coding
->common_flags
3042 |= CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
;
3044 = (NILP (XVECTOR (coding_spec
)->contents
[4])
3045 ? CODING_FLAG_BIG5_HKU
3046 : CODING_FLAG_BIG5_ETEN
);
3050 coding
->type
= coding_type_ccl
;
3051 coding
->common_flags
3052 |= CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
;
3054 Lisp_Object val
= XVECTOR (coding_spec
)->contents
[4];
3055 Lisp_Object decoder
, encoder
;
3058 && SYMBOLP (XCONS (val
)->car
)
3059 && !NILP (decoder
= Fget (XCONS (val
)->car
, Qccl_program_idx
))
3060 && !NILP (decoder
= Fcdr (Faref (Vccl_program_table
, decoder
)))
3061 && SYMBOLP (XCONS (val
)->cdr
)
3062 && !NILP (encoder
= Fget (XCONS (val
)->cdr
, Qccl_program_idx
))
3063 && !NILP (encoder
= Fcdr (Faref (Vccl_program_table
, encoder
))))
3065 setup_ccl_program (&(coding
->spec
.ccl
.decoder
), decoder
);
3066 setup_ccl_program (&(coding
->spec
.ccl
.encoder
), encoder
);
3069 goto label_invalid_coding_system
;
3071 coding
->common_flags
|= CODING_REQUIRE_FLUSHING_MASK
;
3075 coding
->type
= coding_type_raw_text
;
3079 goto label_invalid_coding_system
;
3083 label_invalid_coding_system
:
3084 coding
->type
= coding_type_no_conversion
;
3085 coding
->category_idx
= CODING_CATEGORY_IDX_BINARY
;
3086 coding
->common_flags
= 0;
3087 coding
->eol_type
= CODING_EOL_LF
;
3088 coding
->pre_write_conversion
= coding
->post_read_conversion
= Qnil
;
3092 /* Emacs has a mechanism to automatically detect a coding system if it
3093 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
3094 it's impossible to distinguish some coding systems accurately
3095 because they use the same range of codes. So, at first, coding
3096 systems are categorized into 7, those are:
3098 o coding-category-emacs-mule
3100 The category for a coding system which has the same code range
3101 as Emacs' internal format. Assigned the coding-system (Lisp
3102 symbol) `emacs-mule' by default.
3104 o coding-category-sjis
3106 The category for a coding system which has the same code range
3107 as SJIS. Assigned the coding-system (Lisp
3108 symbol) `japanese-shift-jis' by default.
3110 o coding-category-iso-7
3112 The category for a coding system which has the same code range
3113 as ISO2022 of 7-bit environment. This doesn't use any locking
3114 shift and single shift functions. This can encode/decode all
3115 charsets. Assigned the coding-system (Lisp symbol)
3116 `iso-2022-7bit' by default.
3118 o coding-category-iso-7-tight
3120 Same as coding-category-iso-7 except that this can
3121 encode/decode only the specified charsets.
3123 o coding-category-iso-8-1
3125 The category for a coding system which has the same code range
3126 as ISO2022 of 8-bit environment and graphic plane 1 used only
3127 for DIMENSION1 charset. This doesn't use any locking shift
3128 and single shift functions. Assigned the coding-system (Lisp
3129 symbol) `iso-latin-1' by default.
3131 o coding-category-iso-8-2
3133 The category for a coding system which has the same code range
3134 as ISO2022 of 8-bit environment and graphic plane 1 used only
3135 for DIMENSION2 charset. This doesn't use any locking shift
3136 and single shift functions. Assigned the coding-system (Lisp
3137 symbol) `japanese-iso-8bit' by default.
3139 o coding-category-iso-7-else
3141 The category for a coding system which has the same code range
3142 as ISO2022 of 7-bit environemnt but uses locking shift or
3143 single shift functions. Assigned the coding-system (Lisp
3144 symbol) `iso-2022-7bit-lock' by default.
3146 o coding-category-iso-8-else
3148 The category for a coding system which has the same code range
3149 as ISO2022 of 8-bit environemnt but uses locking shift or
3150 single shift functions. Assigned the coding-system (Lisp
3151 symbol) `iso-2022-8bit-ss2' by default.
3153 o coding-category-big5
3155 The category for a coding system which has the same code range
3156 as BIG5. Assigned the coding-system (Lisp symbol)
3157 `cn-big5' by default.
3159 o coding-category-binary
3161 The category for a coding system not categorized in any of the
3162 above. Assigned the coding-system (Lisp symbol)
3163 `no-conversion' by default.
3165 Each of them is a Lisp symbol and the value is an actual
3166 `coding-system's (this is also a Lisp symbol) assigned by a user.
3167 What Emacs does actually is to detect a category of coding system.
3168 Then, it uses a `coding-system' assigned to it. If Emacs can't
3169 decide only one possible category, it selects a category of the
3170 highest priority. Priorities of categories are also specified by a
3171 user in a Lisp variable `coding-category-list'.
3176 int ascii_skip_code
[256];
3178 /* Detect how a text of length SRC_BYTES pointed by SOURCE is encoded.
3179 If it detects possible coding systems, return an integer in which
3180 appropriate flag bits are set. Flag bits are defined by macros
3181 CODING_CATEGORY_MASK_XXX in `coding.h'.
3183 How many ASCII characters are at the head is returned as *SKIP. */
3186 detect_coding_mask (source
, src_bytes
, priorities
, skip
)
3187 unsigned char *source
;
3188 int src_bytes
, *priorities
, *skip
;
3190 register unsigned char c
;
3191 unsigned char *src
= source
, *src_end
= source
+ src_bytes
;
3195 /* At first, skip all ASCII characters and control characters except
3196 for three ISO2022 specific control characters. */
3197 ascii_skip_code
[ISO_CODE_SO
] = 0;
3198 ascii_skip_code
[ISO_CODE_SI
] = 0;
3199 ascii_skip_code
[ISO_CODE_ESC
] = 0;
3201 label_loop_detect_coding
:
3202 while (src
< src_end
&& ascii_skip_code
[*src
]) src
++;
3203 *skip
= src
- source
;
3206 /* We found nothing other than ASCII. There's nothing to do. */
3209 /* The text seems to be encoded in some multilingual coding system.
3210 Now, try to find in which coding system the text is encoded. */
3213 /* i.e. (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) */
3214 /* C is an ISO2022 specific control code of C0. */
3215 mask
= detect_coding_iso2022 (src
, src_end
);
3218 /* No valid ISO2022 code follows C. Try again. */
3220 if (c
== ISO_CODE_ESC
)
3221 ascii_skip_code
[ISO_CODE_ESC
] = 1;
3223 ascii_skip_code
[ISO_CODE_SO
] = ascii_skip_code
[ISO_CODE_SI
] = 1;
3224 goto label_loop_detect_coding
;
3227 goto label_return_highest_only
;
3235 /* C is the first byte of SJIS character code,
3236 or a leading-code of Emacs' internal format (emacs-mule). */
3237 try = CODING_CATEGORY_MASK_SJIS
| CODING_CATEGORY_MASK_EMACS_MULE
;
3239 /* Or, if C is a special latin extra code,
3240 or is an ISO2022 specific control code of C1 (SS2 or SS3),
3241 or is an ISO2022 control-sequence-introducer (CSI),
3242 we should also consider the possibility of ISO2022 codings. */
3243 if ((VECTORP (Vlatin_extra_code_table
)
3244 && !NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
3245 || (c
== ISO_CODE_SS2
|| c
== ISO_CODE_SS3
)
3246 || (c
== ISO_CODE_CSI
3249 || ((*src
== '0' || *src
== '1' || *src
== '2')
3250 && src
+ 1 < src_end
3251 && src
[1] == ']')))))
3252 try |= (CODING_CATEGORY_MASK_ISO_8_ELSE
3253 | CODING_CATEGORY_MASK_ISO_8BIT
);
3256 /* C is a character of ISO2022 in graphic plane right,
3257 or a SJIS's 1-byte character code (i.e. JISX0201),
3258 or the first byte of BIG5's 2-byte code. */
3259 try = (CODING_CATEGORY_MASK_ISO_8_ELSE
3260 | CODING_CATEGORY_MASK_ISO_8BIT
3261 | CODING_CATEGORY_MASK_SJIS
3262 | CODING_CATEGORY_MASK_BIG5
);
3267 for (i
= 0; i
< CODING_CATEGORY_IDX_MAX
; i
++)
3269 if (priorities
[i
] & try & CODING_CATEGORY_MASK_ISO
)
3270 mask
= detect_coding_iso2022 (src
, src_end
);
3271 else if (priorities
[i
] & try & CODING_CATEGORY_MASK_SJIS
)
3272 mask
= detect_coding_sjis (src
, src_end
);
3273 else if (priorities
[i
] & try & CODING_CATEGORY_MASK_BIG5
)
3274 mask
= detect_coding_big5 (src
, src_end
);
3275 else if (priorities
[i
] & try & CODING_CATEGORY_MASK_EMACS_MULE
)
3276 mask
= detect_coding_emacs_mule (src
, src_end
);
3277 else if (priorities
[i
] & CODING_CATEGORY_MASK_RAW_TEXT
)
3278 mask
= CODING_CATEGORY_MASK_RAW_TEXT
;
3279 else if (priorities
[i
] & CODING_CATEGORY_MASK_BINARY
)
3280 mask
= CODING_CATEGORY_MASK_BINARY
;
3282 goto label_return_highest_only
;
3284 return CODING_CATEGORY_MASK_RAW_TEXT
;
3286 if (try & CODING_CATEGORY_MASK_ISO
)
3287 mask
|= detect_coding_iso2022 (src
, src_end
);
3288 if (try & CODING_CATEGORY_MASK_SJIS
)
3289 mask
|= detect_coding_sjis (src
, src_end
);
3290 if (try & CODING_CATEGORY_MASK_BIG5
)
3291 mask
|= detect_coding_big5 (src
, src_end
);
3292 if (try & CODING_CATEGORY_MASK_EMACS_MULE
)
3293 mask
|= detect_coding_emacs_mule (src
, src_end
);
3295 return (mask
| CODING_CATEGORY_MASK_RAW_TEXT
| CODING_CATEGORY_MASK_BINARY
);
3297 label_return_highest_only
:
3298 for (i
= 0; i
< CODING_CATEGORY_IDX_MAX
; i
++)
3300 if (mask
& priorities
[i
])
3301 return priorities
[i
];
3303 return CODING_CATEGORY_MASK_RAW_TEXT
;
3306 /* Detect how a text of length SRC_BYTES pointed by SRC is encoded.
3307 The information of the detected coding system is set in CODING. */
3310 detect_coding (coding
, src
, src_bytes
)
3311 struct coding_system
*coding
;
3317 Lisp_Object val
= Vcoding_category_list
;
3319 mask
= detect_coding_mask (src
, src_bytes
, coding_priorities
, &skip
);
3320 coding
->heading_ascii
= skip
;
3324 /* We found a single coding system of the highest priority in MASK. */
3326 while (mask
&& ! (mask
& 1)) mask
>>= 1, idx
++;
3328 idx
= CODING_CATEGORY_IDX_RAW_TEXT
;
3330 val
= XSYMBOL (XVECTOR (Vcoding_category_table
)->contents
[idx
])->value
;
3332 if (coding
->eol_type
!= CODING_EOL_UNDECIDED
)
3334 Lisp_Object tmp
= Fget (val
, Qeol_type
);
3337 val
= XVECTOR (tmp
)->contents
[coding
->eol_type
];
3339 setup_coding_system (val
, coding
);
3340 /* Set this again because setup_coding_system reset this member. */
3341 coding
->heading_ascii
= skip
;
3344 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
3345 SOURCE is encoded. Return one of CODING_EOL_LF, CODING_EOL_CRLF,
3346 CODING_EOL_CR, and CODING_EOL_UNDECIDED.
3348 How many non-eol characters are at the head is returned as *SKIP. */
3350 #define MAX_EOL_CHECK_COUNT 3
3353 detect_eol_type (source
, src_bytes
, skip
)
3354 unsigned char *source
;
3355 int src_bytes
, *skip
;
3357 unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
3359 int total
= 0; /* How many end-of-lines are found so far. */
3360 int eol_type
= CODING_EOL_UNDECIDED
;
3365 while (src
< src_end
&& total
< MAX_EOL_CHECK_COUNT
)
3368 if (c
== '\n' || c
== '\r')
3371 *skip
= src
- 1 - source
;
3374 this_eol_type
= CODING_EOL_LF
;
3375 else if (src
>= src_end
|| *src
!= '\n')
3376 this_eol_type
= CODING_EOL_CR
;
3378 this_eol_type
= CODING_EOL_CRLF
, src
++;
3380 if (eol_type
== CODING_EOL_UNDECIDED
)
3381 /* This is the first end-of-line. */
3382 eol_type
= this_eol_type
;
3383 else if (eol_type
!= this_eol_type
)
3385 /* The found type is different from what found before. */
3386 eol_type
= CODING_EOL_INCONSISTENT
;
3393 *skip
= src_end
- source
;
3397 /* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC
3398 is encoded. If it detects an appropriate format of end-of-line, it
3399 sets the information in *CODING. */
3402 detect_eol (coding
, src
, src_bytes
)
3403 struct coding_system
*coding
;
3409 int eol_type
= detect_eol_type (src
, src_bytes
, &skip
);
3411 if (coding
->heading_ascii
> skip
)
3412 coding
->heading_ascii
= skip
;
3414 skip
= coding
->heading_ascii
;
3416 if (eol_type
== CODING_EOL_UNDECIDED
)
3418 if (eol_type
== CODING_EOL_INCONSISTENT
)
3421 /* This code is suppressed until we find a better way to
3422 distinguish raw text file and binary file. */
3424 /* If we have already detected that the coding is raw-text, the
3425 coding should actually be no-conversion. */
3426 if (coding
->type
== coding_type_raw_text
)
3428 setup_coding_system (Qno_conversion
, coding
);
3431 /* Else, let's decode only text code anyway. */
3433 eol_type
= CODING_EOL_LF
;
3436 val
= Fget (coding
->symbol
, Qeol_type
);
3437 if (VECTORP (val
) && XVECTOR (val
)->size
== 3)
3439 setup_coding_system (XVECTOR (val
)->contents
[eol_type
], coding
);
3440 coding
->heading_ascii
= skip
;
3444 #define CONVERSION_BUFFER_EXTRA_ROOM 256
3446 #define DECODING_BUFFER_MAG(coding) \
3447 (coding->type == coding_type_iso2022 \
3449 : ((coding->type == coding_type_sjis || coding->type == coding_type_big5) \
3451 : (coding->type == coding_type_raw_text \
3453 : (coding->type == coding_type_ccl \
3454 ? coding->spec.ccl.decoder.buf_magnification \
3457 /* Return maximum size (bytes) of a buffer enough for decoding
3458 SRC_BYTES of text encoded in CODING. */
3461 decoding_buffer_size (coding
, src_bytes
)
3462 struct coding_system
*coding
;
3465 return (src_bytes
* DECODING_BUFFER_MAG (coding
)
3466 + CONVERSION_BUFFER_EXTRA_ROOM
);
3469 /* Return maximum size (bytes) of a buffer enough for encoding
3470 SRC_BYTES of text to CODING. */
3473 encoding_buffer_size (coding
, src_bytes
)
3474 struct coding_system
*coding
;
3479 if (coding
->type
== coding_type_ccl
)
3480 magnification
= coding
->spec
.ccl
.encoder
.buf_magnification
;
3484 return (src_bytes
* magnification
+ CONVERSION_BUFFER_EXTRA_ROOM
);
3487 #ifndef MINIMUM_CONVERSION_BUFFER_SIZE
3488 #define MINIMUM_CONVERSION_BUFFER_SIZE 1024
3491 char *conversion_buffer
;
3492 int conversion_buffer_size
;
3494 /* Return a pointer to a SIZE bytes of buffer to be used for encoding
3495 or decoding. Sufficient memory is allocated automatically. If we
3496 run out of memory, return NULL. */
3499 get_conversion_buffer (size
)
3502 if (size
> conversion_buffer_size
)
3505 int real_size
= conversion_buffer_size
* 2;
3507 while (real_size
< size
) real_size
*= 2;
3508 buf
= (char *) xmalloc (real_size
);
3509 xfree (conversion_buffer
);
3510 conversion_buffer
= buf
;
3511 conversion_buffer_size
= real_size
;
3513 return conversion_buffer
;
3517 ccl_coding_driver (coding
, source
, destination
, src_bytes
, dst_bytes
, encodep
)
3518 struct coding_system
*coding
;
3519 unsigned char *source
, *destination
;
3520 int src_bytes
, dst_bytes
, encodep
;
3522 struct ccl_program
*ccl
3523 = encodep
? &coding
->spec
.ccl
.encoder
: &coding
->spec
.ccl
.decoder
;
3526 coding
->produced
= ccl_driver (ccl
, source
, destination
,
3527 src_bytes
, dst_bytes
, &(coding
->consumed
));
3530 coding
->produced_char
= coding
->produced
;
3531 coding
->consumed_char
3532 = multibyte_chars_in_text (source
, coding
->consumed
);
3536 coding
->produced_char
3537 = multibyte_chars_in_text (destination
, coding
->produced
);
3538 coding
->consumed_char
= coding
->consumed
;
3540 switch (ccl
->status
)
3542 case CCL_STAT_SUSPEND_BY_SRC
:
3543 result
= CODING_FINISH_INSUFFICIENT_SRC
;
3545 case CCL_STAT_SUSPEND_BY_DST
:
3546 result
= CODING_FINISH_INSUFFICIENT_DST
;
3549 result
= CODING_FINISH_NORMAL
;
3555 /* See "GENERAL NOTES about `decode_coding_XXX ()' functions". Before
3556 decoding, it may detect coding system and format of end-of-line if
3557 those are not yet decided. */
3560 decode_coding (coding
, source
, destination
, src_bytes
, dst_bytes
)
3561 struct coding_system
*coding
;
3562 unsigned char *source
, *destination
;
3563 int src_bytes
, dst_bytes
;
3569 coding
->produced
= coding
->produced_char
= 0;
3570 coding
->consumed
= coding
->consumed_char
= 0;
3571 coding
->fake_multibyte
= 0;
3572 return CODING_FINISH_NORMAL
;
3575 if (coding
->type
== coding_type_undecided
)
3576 detect_coding (coding
, source
, src_bytes
);
3578 if (coding
->eol_type
== CODING_EOL_UNDECIDED
)
3579 detect_eol (coding
, source
, src_bytes
);
3581 switch (coding
->type
)
3583 case coding_type_emacs_mule
:
3584 case coding_type_undecided
:
3585 case coding_type_raw_text
:
3586 if (coding
->eol_type
== CODING_EOL_LF
3587 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
3588 goto label_no_conversion
;
3589 result
= decode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
);
3592 case coding_type_sjis
:
3593 result
= decode_coding_sjis_big5 (coding
, source
, destination
,
3594 src_bytes
, dst_bytes
, 1);
3597 case coding_type_iso2022
:
3598 result
= decode_coding_iso2022 (coding
, source
, destination
,
3599 src_bytes
, dst_bytes
);
3602 case coding_type_big5
:
3603 result
= decode_coding_sjis_big5 (coding
, source
, destination
,
3604 src_bytes
, dst_bytes
, 0);
3607 case coding_type_ccl
:
3608 result
= ccl_coding_driver (coding
, source
, destination
,
3609 src_bytes
, dst_bytes
, 0);
3612 default: /* i.e. case coding_type_no_conversion: */
3613 label_no_conversion
:
3614 if (dst_bytes
&& src_bytes
> dst_bytes
)
3616 coding
->produced
= dst_bytes
;
3617 result
= CODING_FINISH_INSUFFICIENT_DST
;
3621 coding
->produced
= src_bytes
;
3622 result
= CODING_FINISH_NORMAL
;
3625 bcopy (source
, destination
, coding
->produced
);
3627 safe_bcopy (source
, destination
, coding
->produced
);
3628 coding
->fake_multibyte
= 1;
3630 = coding
->consumed_char
= coding
->produced_char
= coding
->produced
;
3637 /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". */
3640 encode_coding (coding
, source
, destination
, src_bytes
, dst_bytes
)
3641 struct coding_system
*coding
;
3642 unsigned char *source
, *destination
;
3643 int src_bytes
, dst_bytes
;
3649 coding
->produced
= coding
->produced_char
= 0;
3650 coding
->consumed
= coding
->consumed_char
= 0;
3651 coding
->fake_multibyte
= 0;
3652 return CODING_FINISH_NORMAL
;
3655 switch (coding
->type
)
3657 case coding_type_emacs_mule
:
3658 case coding_type_undecided
:
3659 case coding_type_raw_text
:
3660 if (coding
->eol_type
== CODING_EOL_LF
3661 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
3662 goto label_no_conversion
;
3663 result
= encode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
);
3666 case coding_type_sjis
:
3667 result
= encode_coding_sjis_big5 (coding
, source
, destination
,
3668 src_bytes
, dst_bytes
, 1);
3671 case coding_type_iso2022
:
3672 result
= encode_coding_iso2022 (coding
, source
, destination
,
3673 src_bytes
, dst_bytes
);
3676 case coding_type_big5
:
3677 result
= encode_coding_sjis_big5 (coding
, source
, destination
,
3678 src_bytes
, dst_bytes
, 0);
3681 case coding_type_ccl
:
3682 result
= ccl_coding_driver (coding
, source
, destination
,
3683 src_bytes
, dst_bytes
, 1);
3686 default: /* i.e. case coding_type_no_conversion: */
3687 label_no_conversion
:
3688 if (dst_bytes
&& src_bytes
> dst_bytes
)
3690 coding
->produced
= dst_bytes
;
3691 result
= CODING_FINISH_INSUFFICIENT_DST
;
3695 coding
->produced
= src_bytes
;
3696 result
= CODING_FINISH_NORMAL
;
3699 bcopy (source
, destination
, coding
->produced
);
3701 safe_bcopy (source
, destination
, coding
->produced
);
3702 if (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
)
3704 unsigned char *p
= destination
, *pend
= p
+ coding
->produced
;
3706 if (*p
++ == '\015') p
[-1] = '\n';
3708 coding
->fake_multibyte
= 1;
3710 = coding
->consumed_char
= coding
->produced_char
= coding
->produced
;
3717 /* Scan text in the region between *BEG and *END (byte positions),
3718 skip characters which we don't have to decode by coding system
3719 CODING at the head and tail, then set *BEG and *END to the region
3720 of the text we actually have to convert. The caller should move
3721 the gap out of the region in advance.
3723 If STR is not NULL, *BEG and *END are indices into STR. */
3726 shrink_decoding_region (beg
, end
, coding
, str
)
3728 struct coding_system
*coding
;
3731 unsigned char *begp_orig
, *begp
, *endp_orig
, *endp
, c
;
3734 if (coding
->type
== coding_type_ccl
3735 || coding
->type
== coding_type_undecided
3736 || !NILP (coding
->post_read_conversion
))
3738 /* We can't skip any data. */
3741 else if (coding
->type
== coding_type_no_conversion
)
3743 /* We need no conversion, but don't have to skip any data here.
3744 Decoding routine handles them effectively anyway. */
3748 eol_conversion
= (coding
->eol_type
!= CODING_EOL_LF
);
3750 if ((! eol_conversion
) && (coding
->heading_ascii
>= 0))
3751 /* Detection routine has already found how much we can skip at the
3753 *beg
+= coding
->heading_ascii
;
3757 begp_orig
= begp
= str
+ *beg
;
3758 endp_orig
= endp
= str
+ *end
;
3762 begp_orig
= begp
= BYTE_POS_ADDR (*beg
);
3763 endp_orig
= endp
= begp
+ *end
- *beg
;
3766 switch (coding
->type
)
3768 case coding_type_emacs_mule
:
3769 case coding_type_raw_text
:
3772 if (coding
->heading_ascii
< 0)
3773 while (begp
< endp
&& *begp
!= '\r' && *begp
< 0x80) begp
++;
3774 while (begp
< endp
&& endp
[-1] != '\r' && endp
[-1] < 0x80)
3776 /* Do not consider LF as ascii if preceded by CR, since that
3777 confuses eol decoding. */
3778 if (begp
< endp
&& endp
< endp_orig
&& endp
[-1] == '\r' && endp
[0] == '\n')
3785 case coding_type_sjis
:
3786 case coding_type_big5
:
3787 /* We can skip all ASCII characters at the head. */
3788 if (coding
->heading_ascii
< 0)
3791 while (begp
< endp
&& *begp
< 0x80 && *begp
!= '\r') begp
++;
3793 while (begp
< endp
&& *begp
< 0x80) begp
++;
3795 /* We can skip all ASCII characters at the tail except for the
3796 second byte of SJIS or BIG5 code. */
3798 while (begp
< endp
&& endp
[-1] < 0x80 && endp
[-1] != '\r') endp
--;
3800 while (begp
< endp
&& endp
[-1] < 0x80) endp
--;
3801 /* Do not consider LF as ascii if preceded by CR, since that
3802 confuses eol decoding. */
3803 if (begp
< endp
&& endp
< endp_orig
&& endp
[-1] == '\r' && endp
[0] == '\n')
3805 if (begp
< endp
&& endp
< endp_orig
&& endp
[-1] >= 0x80)
3809 default: /* i.e. case coding_type_iso2022: */
3810 if (coding
->heading_ascii
< 0)
3812 /* We can skip all ASCII characters at the head except for a
3813 few control codes. */
3814 while (begp
< endp
&& (c
= *begp
) < 0x80
3815 && c
!= ISO_CODE_CR
&& c
!= ISO_CODE_SO
3816 && c
!= ISO_CODE_SI
&& c
!= ISO_CODE_ESC
3817 && (!eol_conversion
|| c
!= ISO_CODE_LF
))
3820 switch (coding
->category_idx
)
3822 case CODING_CATEGORY_IDX_ISO_8_1
:
3823 case CODING_CATEGORY_IDX_ISO_8_2
:
3824 /* We can skip all ASCII characters at the tail. */
3826 while (begp
< endp
&& (c
= endp
[-1]) < 0x80 && c
!= '\r') endp
--;
3828 while (begp
< endp
&& endp
[-1] < 0x80) endp
--;
3829 /* Do not consider LF as ascii if preceded by CR, since that
3830 confuses eol decoding. */
3831 if (begp
< endp
&& endp
< endp_orig
&& endp
[-1] == '\r' && endp
[0] == '\n')
3835 case CODING_CATEGORY_IDX_ISO_7
:
3836 case CODING_CATEGORY_IDX_ISO_7_TIGHT
:
3837 /* We can skip all charactes at the tail except for ESC and
3838 the following 2-byte at the tail. */
3841 && (c
= endp
[-1]) < 0x80 && c
!= ISO_CODE_ESC
&& c
!= '\r')
3845 && (c
= endp
[-1]) < 0x80 && c
!= ISO_CODE_ESC
)
3847 /* Do not consider LF as ascii if preceded by CR, since that
3848 confuses eol decoding. */
3849 if (begp
< endp
&& endp
< endp_orig
&& endp
[-1] == '\r' && endp
[0] == '\n')
3851 if (begp
< endp
&& endp
[-1] == ISO_CODE_ESC
)
3853 if (endp
+ 1 < endp_orig
&& end
[0] == '(' && end
[1] == 'B')
3854 /* This is an ASCII designation sequence. We can
3855 surely skip the tail. */
3858 /* Hmmm, we can't skip the tail. */
3863 *beg
+= begp
- begp_orig
;
3864 *end
+= endp
- endp_orig
;
3868 /* Like shrink_decoding_region but for encoding. */
3871 shrink_encoding_region (beg
, end
, coding
, str
)
3873 struct coding_system
*coding
;
3876 unsigned char *begp_orig
, *begp
, *endp_orig
, *endp
;
3879 if (coding
->type
== coding_type_ccl
)
3880 /* We can't skip any data. */
3882 else if (coding
->type
== coding_type_no_conversion
)
3884 /* We need no conversion. */
3891 begp_orig
= begp
= str
+ *beg
;
3892 endp_orig
= endp
= str
+ *end
;
3896 begp_orig
= begp
= BYTE_POS_ADDR (*beg
);
3897 endp_orig
= endp
= begp
+ *end
- *beg
;
3900 eol_conversion
= (coding
->eol_type
== CODING_EOL_CR
3901 || coding
->eol_type
== CODING_EOL_CRLF
);
3903 /* Here, we don't have to check coding->pre_write_conversion because
3904 the caller is expected to have handled it already. */
3905 switch (coding
->type
)
3907 case coding_type_undecided
:
3908 case coding_type_emacs_mule
:
3909 case coding_type_raw_text
:
3912 while (begp
< endp
&& *begp
!= '\n') begp
++;
3913 while (begp
< endp
&& endp
[-1] != '\n') endp
--;
3919 case coding_type_iso2022
:
3920 if (coding
->flags
& CODING_FLAG_ISO_DESIGNATE_AT_BOL
)
3922 unsigned char *bol
= begp
;
3923 while (begp
< endp
&& *begp
< 0x80)
3926 if (begp
[-1] == '\n')
3930 goto label_skip_tail
;
3935 /* We can skip all ASCII characters at the head and tail. */
3937 while (begp
< endp
&& *begp
< 0x80 && *begp
!= '\n') begp
++;
3939 while (begp
< endp
&& *begp
< 0x80) begp
++;
3942 while (begp
< endp
&& endp
[-1] < 0x80 && endp
[-1] != '\n') endp
--;
3944 while (begp
< endp
&& *(endp
- 1) < 0x80) endp
--;
3948 *beg
+= begp
- begp_orig
;
3949 *end
+= endp
- endp_orig
;
3953 /* Decode (if ENCODEP is zero) or encode (if ENCODEP is nonzero) the
3954 text from FROM to TO (byte positions are FROM_BYTE and TO_BYTE) by
3955 coding system CODING, and return the status code of code conversion
3956 (currently, this value has no meaning).
3958 How many characters (and bytes) are converted to how many
3959 characters (and bytes) are recorded in members of the structure
3962 If REPLACE is nonzero, we do various things as if the original text
3963 is deleted and a new text is inserted. See the comments in
3964 replace_range (insdel.c) to know what we are doing. */
3967 code_convert_region (from
, from_byte
, to
, to_byte
, coding
, encodep
, replace
)
3968 int from
, from_byte
, to
, to_byte
, encodep
, replace
;
3969 struct coding_system
*coding
;
3971 int len
= to
- from
, len_byte
= to_byte
- from_byte
;
3972 int require
, inserted
, inserted_byte
;
3973 int head_skip
, tail_skip
, total_skip
;
3974 Lisp_Object saved_coding_symbol
= Qnil
;
3975 int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
3977 int fake_multibyte
= 0;
3978 unsigned char *src
, *dst
;
3979 Lisp_Object deletion
= Qnil
;
3981 if (from
< PT
&& PT
< to
)
3982 SET_PT_BOTH (from
, from_byte
);
3986 int saved_from
= from
;
3988 prepare_to_modify_buffer (from
, to
, &from
);
3989 if (saved_from
!= from
)
3993 from_byte
= CHAR_TO_BYTE (from
), to_byte
= CHAR_TO_BYTE (to
);
3995 from_byte
= from
, to_byte
= to
;
3996 len_byte
= to_byte
- from_byte
;
4000 if (! encodep
&& CODING_REQUIRE_DETECTION (coding
))
4002 /* We must detect encoding of text and eol format. */
4004 if (from
< GPT
&& to
> GPT
)
4005 move_gap_both (from
, from_byte
);
4006 if (coding
->type
== coding_type_undecided
)
4008 detect_coding (coding
, BYTE_POS_ADDR (from_byte
), len_byte
);
4009 if (coding
->type
== coding_type_undecided
)
4010 /* It seems that the text contains only ASCII, but we
4011 should not left it undecided because the deeper
4012 decoding routine (decode_coding) tries to detect the
4013 encodings again in vain. */
4014 coding
->type
= coding_type_emacs_mule
;
4016 if (coding
->eol_type
== CODING_EOL_UNDECIDED
)
4018 saved_coding_symbol
= coding
->symbol
;
4019 detect_eol (coding
, BYTE_POS_ADDR (from_byte
), len_byte
);
4020 if (coding
->eol_type
== CODING_EOL_UNDECIDED
)
4021 coding
->eol_type
= CODING_EOL_LF
;
4022 /* We had better recover the original eol format if we
4023 encounter an inconsitent eol format while decoding. */
4024 coding
->mode
|= CODING_MODE_INHIBIT_INCONSISTENT_EOL
;
4028 coding
->consumed_char
= len
, coding
->consumed
= len_byte
;
4031 ? ! CODING_REQUIRE_ENCODING (coding
)
4032 : ! CODING_REQUIRE_DECODING (coding
))
4034 coding
->produced
= len_byte
;
4037 /* See the comment of the member heading_ascii in coding.h. */
4038 && coding
->heading_ascii
< len_byte
)
4040 /* We still may have to combine byte at the head and the
4041 tail of the text in the region. */
4042 if (from
< GPT
&& GPT
< to
)
4043 move_gap_both (to
, to_byte
);
4044 len
= multibyte_chars_in_text (BYTE_POS_ADDR (from_byte
), len_byte
);
4045 adjust_after_insert (from
, from_byte
, to
, to_byte
, len
);
4046 coding
->produced_char
= len
;
4051 adjust_after_insert (from
, from_byte
, to
, to_byte
, len_byte
);
4052 coding
->produced_char
= len_byte
;
4057 /* Now we convert the text. */
4059 /* For encoding, we must process pre-write-conversion in advance. */
4061 && ! NILP (coding
->pre_write_conversion
)
4062 && SYMBOLP (coding
->pre_write_conversion
)
4063 && ! NILP (Ffboundp (coding
->pre_write_conversion
)))
4065 /* The function in pre-write-conversion may put a new text in a
4067 struct buffer
*prev
= current_buffer
, *new;
4069 call2 (coding
->pre_write_conversion
,
4070 make_number (from
), make_number (to
));
4071 if (current_buffer
!= prev
)
4074 new = current_buffer
;
4075 set_buffer_internal_1 (prev
);
4076 del_range_2 (from
, from_byte
, to
, to_byte
);
4077 insert_from_buffer (new, BEG
, len
, 0);
4079 to_byte
= multibyte
? CHAR_TO_BYTE (to
) : to
;
4080 len_byte
= to_byte
- from_byte
;
4085 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
4087 /* Try to skip the heading and tailing ASCIIs. */
4089 int from_byte_orig
= from_byte
, to_byte_orig
= to_byte
;
4091 if (from
< GPT
&& GPT
< to
)
4092 move_gap_both (from
, from_byte
);
4094 shrink_encoding_region (&from_byte
, &to_byte
, coding
, NULL
);
4096 shrink_decoding_region (&from_byte
, &to_byte
, coding
, NULL
);
4097 if (from_byte
== to_byte
)
4099 coding
->produced
= len_byte
;
4100 coding
->produced_char
= multibyte
? len
: len_byte
;
4102 /* We must record and adjust for this new text now. */
4103 adjust_after_insert (from
, from_byte_orig
, to
, to_byte_orig
, len
);
4107 head_skip
= from_byte
- from_byte_orig
;
4108 tail_skip
= to_byte_orig
- to_byte
;
4109 total_skip
= head_skip
+ tail_skip
;
4112 len
-= total_skip
; len_byte
-= total_skip
;
4115 /* For converion, we must put the gap before the text in addition to
4116 making the gap larger for efficient decoding. The required gap
4117 size starts from 2000 which is the magic number used in make_gap.
4118 But, after one batch of conversion, it will be incremented if we
4119 find that it is not enough . */
4122 if (GAP_SIZE
< require
)
4123 make_gap (require
- GAP_SIZE
);
4124 move_gap_both (from
, from_byte
);
4126 if (GPT
- BEG
< beg_unchanged
)
4127 beg_unchanged
= GPT
- BEG
;
4128 if (Z
- GPT
< end_unchanged
)
4129 end_unchanged
= Z
- GPT
;
4131 inserted
= inserted_byte
= 0;
4132 src
= GAP_END_ADDR
, dst
= GPT_ADDR
;
4134 GAP_SIZE
+= len_byte
;
4137 ZV_BYTE
-= len_byte
;
4144 /* The buffer memory is changed from:
4145 +--------+converted-text+---------+-------original-text------+---+
4146 |<-from->|<--inserted-->|---------|<-----------len---------->|---|
4147 |<------------------- GAP_SIZE -------------------->| */
4149 result
= encode_coding (coding
, src
, dst
, len_byte
, 0);
4151 result
= decode_coding (coding
, src
, dst
, len_byte
, 0);
4153 +--------+-------converted-text--------+--+---original-text--+---+
4154 |<-from->|<--inserted-->|<--produced-->|--|<-(len-consumed)->|---|
4155 |<------------------- GAP_SIZE -------------------->| */
4156 if (coding
->fake_multibyte
)
4159 if (!encodep
&& !multibyte
)
4160 coding
->produced_char
= coding
->produced
;
4161 inserted
+= coding
->produced_char
;
4162 inserted_byte
+= coding
->produced
;
4163 len_byte
-= coding
->consumed
;
4164 src
+= coding
->consumed
;
4165 dst
+= inserted_byte
;
4167 if (! encodep
&& result
== CODING_FINISH_INCONSISTENT_EOL
)
4169 unsigned char *pend
= dst
, *p
= pend
- inserted_byte
;
4171 /* Encode LFs back to the original eol format (CR or CRLF). */
4172 if (coding
->eol_type
== CODING_EOL_CR
)
4174 while (p
< pend
) if (*p
++ == '\n') p
[-1] = '\r';
4180 while (p
< pend
) if (*p
++ == '\n') count
++;
4181 if (src
- dst
< count
)
4183 /* We don't have sufficient room for putting LFs
4184 back to CRLF. We must record converted and
4185 not-yet-converted text back to the buffer
4186 content, enlarge the gap, then record them out of
4187 the buffer contents again. */
4188 int add
= len_byte
+ inserted_byte
;
4191 ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
4192 GPT
+= inserted_byte
; GPT_BYTE
+= inserted_byte
;
4193 make_gap (count
- GAP_SIZE
);
4195 ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
4196 GPT
-= inserted_byte
; GPT_BYTE
-= inserted_byte
;
4197 /* Don't forget to update SRC, DST, and PEND. */
4198 src
= GAP_END_ADDR
- len_byte
;
4199 dst
= GPT_ADDR
+ inserted_byte
;
4203 inserted_byte
+= count
;
4204 coding
->produced
+= count
;
4205 p
= dst
= pend
+ count
;
4209 if (*p
== '\n') count
--, *--p
= '\r';
4213 /* Suppress eol-format conversion in the further conversion. */
4214 coding
->eol_type
= CODING_EOL_LF
;
4216 /* Restore the original symbol. */
4217 coding
->symbol
= saved_coding_symbol
;
4223 if (result
== CODING_FINISH_INSUFFICIENT_SRC
)
4225 /* The source text ends in invalid codes. Let's just
4226 make them valid buffer contents, and finish conversion. */
4227 inserted
+= len_byte
;
4228 inserted_byte
+= len_byte
;
4236 /* We have just done the first batch of conversion which was
4237 stoped because of insufficient gap. Let's reconsider the
4238 required gap size (i.e. SRT - DST) now.
4240 We have converted ORIG bytes (== coding->consumed) into
4241 NEW bytes (coding->produced). To convert the remaining
4242 LEN bytes, we may need REQUIRE bytes of gap, where:
4243 REQUIRE + LEN_BYTE = LEN_BYTE * (NEW / ORIG)
4244 REQUIRE = LEN_BYTE * (NEW - ORIG) / ORIG
4245 Here, we are sure that NEW >= ORIG. */
4246 float ratio
= coding
->produced
- coding
->consumed
;
4247 ratio
/= coding
->consumed
;
4248 require
= len_byte
* ratio
;
4251 if ((src
- dst
) < (require
+ 2000))
4253 /* See the comment above the previous call of make_gap. */
4254 int add
= len_byte
+ inserted_byte
;
4257 ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
4258 GPT
+= inserted_byte
; GPT_BYTE
+= inserted_byte
;
4259 make_gap (require
+ 2000);
4261 ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
4262 GPT
-= inserted_byte
; GPT_BYTE
-= inserted_byte
;
4263 /* Don't forget to update SRC, DST. */
4264 src
= GAP_END_ADDR
- len_byte
;
4265 dst
= GPT_ADDR
+ inserted_byte
;
4268 if (src
- dst
> 0) *dst
= 0; /* Put an anchor. */
4272 || !encodep
&& (to
- from
) != (to_byte
- from_byte
)))
4273 inserted
= multibyte_chars_in_text (GPT_ADDR
, inserted_byte
);
4275 /* If we have shrinked the conversion area, adjust it now. */
4279 safe_bcopy (GAP_END_ADDR
, GPT_ADDR
+ inserted_byte
, tail_skip
);
4280 inserted
+= total_skip
; inserted_byte
+= total_skip
;
4281 GAP_SIZE
+= total_skip
;
4282 GPT
-= head_skip
; GPT_BYTE
-= head_skip
;
4283 ZV
-= total_skip
; ZV_BYTE
-= total_skip
;
4284 Z
-= total_skip
; Z_BYTE
-= total_skip
;
4285 from
-= head_skip
; from_byte
-= head_skip
;
4286 to
+= tail_skip
; to_byte
+= tail_skip
;
4289 adjust_after_replace (from
, from_byte
, deletion
, inserted
, inserted_byte
);
4291 if (! encodep
&& ! NILP (coding
->post_read_conversion
))
4294 int orig_inserted
= inserted
, pos
= PT
;
4297 temp_set_point_both (current_buffer
, from
, from_byte
);
4298 val
= call1 (coding
->post_read_conversion
, make_number (inserted
));
4301 CHECK_NUMBER (val
, 0);
4302 inserted
= XFASTINT (val
);
4304 if (pos
>= from
+ orig_inserted
)
4305 temp_set_point (current_buffer
, pos
+ (inserted
- orig_inserted
));
4308 signal_after_change (from
, to
- from
, inserted
);
4311 coding
->consumed
= to_byte
- from_byte
;
4312 coding
->consumed_char
= to
- from
;
4313 coding
->produced
= inserted_byte
;
4314 coding
->produced_char
= inserted
;
4321 code_convert_string (str
, coding
, encodep
, nocopy
)
4323 struct coding_system
*coding
;
4324 int encodep
, nocopy
;
4328 int from
= 0, to
= XSTRING (str
)->size
;
4329 int to_byte
= STRING_BYTES (XSTRING (str
));
4330 struct gcpro gcpro1
;
4331 Lisp_Object saved_coding_symbol
= Qnil
;
4334 if (encodep
&& !NILP (coding
->pre_write_conversion
)
4335 || !encodep
&& !NILP (coding
->post_read_conversion
))
4337 /* Since we have to call Lisp functions which assume target text
4338 is in a buffer, after setting a temporary buffer, call
4339 code_convert_region. */
4340 int count
= specpdl_ptr
- specpdl
;
4341 struct buffer
*prev
= current_buffer
;
4343 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
4344 temp_output_buffer_setup (" *code-converting-work*");
4345 set_buffer_internal (XBUFFER (Vstandard_output
));
4347 insert_from_string (str
, 0, 0, to
, to_byte
, 0);
4350 /* We must insert the contents of STR as is without
4351 unibyte<->multibyte conversion. */
4352 current_buffer
->enable_multibyte_characters
= Qnil
;
4353 insert_from_string (str
, 0, 0, to_byte
, to_byte
, 0);
4354 current_buffer
->enable_multibyte_characters
= Qt
;
4356 code_convert_region (BEGV
, BEGV_BYTE
, ZV
, ZV_BYTE
, coding
, encodep
, 1);
4358 /* We must return the buffer contents as unibyte string. */
4359 current_buffer
->enable_multibyte_characters
= Qnil
;
4360 str
= make_buffer_string (BEGV
, ZV
, 0);
4361 set_buffer_internal (prev
);
4362 return unbind_to (count
, str
);
4365 if (! encodep
&& CODING_REQUIRE_DETECTION (coding
))
4367 /* See the comments in code_convert_region. */
4368 if (coding
->type
== coding_type_undecided
)
4370 detect_coding (coding
, XSTRING (str
)->data
, to_byte
);
4371 if (coding
->type
== coding_type_undecided
)
4372 coding
->type
= coding_type_emacs_mule
;
4374 if (coding
->eol_type
== CODING_EOL_UNDECIDED
)
4376 saved_coding_symbol
= coding
->symbol
;
4377 detect_eol (coding
, XSTRING (str
)->data
, to_byte
);
4378 if (coding
->eol_type
== CODING_EOL_UNDECIDED
)
4379 coding
->eol_type
= CODING_EOL_LF
;
4380 /* We had better recover the original eol format if we
4381 encounter an inconsitent eol format while decoding. */
4382 coding
->mode
|= CODING_MODE_INHIBIT_INCONSISTENT_EOL
;
4387 ? ! CODING_REQUIRE_ENCODING (coding
)
4388 : ! CODING_REQUIRE_DECODING (coding
))
4392 /* Try to skip the heading and tailing ASCIIs. */
4394 shrink_encoding_region (&from
, &to_byte
, coding
, XSTRING (str
)->data
);
4396 shrink_decoding_region (&from
, &to_byte
, coding
, XSTRING (str
)->data
);
4398 if (from
== to_byte
)
4399 return (nocopy
? str
: Fcopy_sequence (str
));
4402 len
= encoding_buffer_size (coding
, to_byte
- from
);
4404 len
= decoding_buffer_size (coding
, to_byte
- from
);
4405 len
+= from
+ STRING_BYTES (XSTRING (str
)) - to_byte
;
4407 buf
= get_conversion_buffer (len
);
4411 bcopy (XSTRING (str
)->data
, buf
, from
);
4413 ? encode_coding (coding
, XSTRING (str
)->data
+ from
,
4414 buf
+ from
, to_byte
- from
, len
)
4415 : decode_coding (coding
, XSTRING (str
)->data
+ from
,
4416 buf
+ from
, to_byte
- from
, len
));
4417 if (! encodep
&& result
== CODING_FINISH_INCONSISTENT_EOL
)
4419 /* We simple try to decode the whole string again but without
4420 eol-conversion this time. */
4421 coding
->eol_type
= CODING_EOL_LF
;
4422 coding
->symbol
= saved_coding_symbol
;
4423 return code_convert_string (str
, coding
, encodep
, nocopy
);
4426 bcopy (XSTRING (str
)->data
+ to_byte
, buf
+ from
+ coding
->produced
,
4427 STRING_BYTES (XSTRING (str
)) - to_byte
);
4429 len
= from
+ STRING_BYTES (XSTRING (str
)) - to_byte
;
4431 str
= make_unibyte_string (buf
, len
+ coding
->produced
);
4433 str
= make_string_from_bytes (buf
, len
+ coding
->produced_char
,
4434 len
+ coding
->produced
);
4440 /*** 7. Emacs Lisp library functions ***/
4442 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
4443 "Return t if OBJECT is nil or a coding-system.\n\
4444 See the documentation of `make-coding-system' for information\n\
4445 about coding-system objects.")
4453 /* Get coding-spec vector for OBJ. */
4454 obj
= Fget (obj
, Qcoding_system
);
4455 return ((VECTORP (obj
) && XVECTOR (obj
)->size
== 5)
4459 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
4460 Sread_non_nil_coding_system
, 1, 1, 0,
4461 "Read a coding system from the minibuffer, prompting with string PROMPT.")
4468 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
4469 Qt
, Qnil
, Qcoding_system_history
, Qnil
, Qnil
);
4471 while (XSTRING (val
)->size
== 0);
4472 return (Fintern (val
, Qnil
));
4475 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 2, 0,
4476 "Read a coding system from the minibuffer, prompting with string PROMPT.\n\
4477 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.")
4478 (prompt
, default_coding_system
)
4479 Lisp_Object prompt
, default_coding_system
;
4482 if (SYMBOLP (default_coding_system
))
4483 XSETSTRING (default_coding_system
, XSYMBOL (default_coding_system
)->name
);
4484 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
4485 Qt
, Qnil
, Qcoding_system_history
,
4486 default_coding_system
, Qnil
);
4487 return (XSTRING (val
)->size
== 0 ? Qnil
: Fintern (val
, Qnil
));
4490 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
4492 "Check validity of CODING-SYSTEM.\n\
4493 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.\n\
4494 It is valid if it is a symbol with a non-nil `coding-system' property.\n\
4495 The value of property should be a vector of length 5.")
4497 Lisp_Object coding_system
;
4499 CHECK_SYMBOL (coding_system
, 0);
4500 if (!NILP (Fcoding_system_p (coding_system
)))
4501 return coding_system
;
4503 Fsignal (Qcoding_system_error
, Fcons (coding_system
, Qnil
));
4507 detect_coding_system (src
, src_bytes
, highest
)
4509 int src_bytes
, highest
;
4511 int coding_mask
, eol_type
;
4512 Lisp_Object val
, tmp
;
4515 coding_mask
= detect_coding_mask (src
, src_bytes
, NULL
, &dummy
);
4516 eol_type
= detect_eol_type (src
, src_bytes
, &dummy
);
4517 if (eol_type
== CODING_EOL_INCONSISTENT
)
4518 eol_type
== CODING_EOL_UNDECIDED
;
4523 if (eol_type
!= CODING_EOL_UNDECIDED
)
4526 val2
= Fget (Qundecided
, Qeol_type
);
4528 val
= XVECTOR (val2
)->contents
[eol_type
];
4533 /* At first, gather possible coding systems in VAL. */
4535 for (tmp
= Vcoding_category_list
; !NILP (tmp
); tmp
= XCONS (tmp
)->cdr
)
4538 = XFASTINT (Fget (XCONS (tmp
)->car
, Qcoding_category_index
));
4539 if (coding_mask
& (1 << idx
))
4541 val
= Fcons (Fsymbol_value (XCONS (tmp
)->car
), val
);
4547 val
= Fnreverse (val
);
4549 /* Then, substitute the elements by subsidiary coding systems. */
4550 for (tmp
= val
; !NILP (tmp
); tmp
= XCONS (tmp
)->cdr
)
4552 if (eol_type
!= CODING_EOL_UNDECIDED
)
4555 eol
= Fget (XCONS (tmp
)->car
, Qeol_type
);
4557 XCONS (tmp
)->car
= XVECTOR (eol
)->contents
[eol_type
];
4560 return (highest
? XCONS (val
)->car
: val
);
4563 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
4565 "Detect coding system of the text in the region between START and END.\n\
4566 Return a list of possible coding systems ordered by priority.\n\
4568 If only ASCII characters are found, it returns `undecided'\n\
4569 or its subsidiary coding system according to a detected end-of-line format.\n\
4571 If optional argument HIGHEST is non-nil, return the coding system of\n\
4573 (start
, end
, highest
)
4574 Lisp_Object start
, end
, highest
;
4577 int from_byte
, to_byte
;
4579 CHECK_NUMBER_COERCE_MARKER (start
, 0);
4580 CHECK_NUMBER_COERCE_MARKER (end
, 1);
4582 validate_region (&start
, &end
);
4583 from
= XINT (start
), to
= XINT (end
);
4584 from_byte
= CHAR_TO_BYTE (from
);
4585 to_byte
= CHAR_TO_BYTE (to
);
4587 if (from
< GPT
&& to
>= GPT
)
4588 move_gap_both (to
, to_byte
);
4590 return detect_coding_system (BYTE_POS_ADDR (from_byte
),
4591 to_byte
- from_byte
,
4595 DEFUN ("detect-coding-string", Fdetect_coding_string
, Sdetect_coding_string
,
4597 "Detect coding system of the text in STRING.\n\
4598 Return a list of possible coding systems ordered by priority.\n\
4600 If only ASCII characters are found, it returns `undecided'\n\
4601 or its subsidiary coding system according to a detected end-of-line format.\n\
4603 If optional argument HIGHEST is non-nil, return the coding system of\n\
4606 Lisp_Object string
, highest
;
4608 CHECK_STRING (string
, 0);
4610 return detect_coding_system (XSTRING (string
)->data
,
4611 STRING_BYTES (XSTRING (string
)),
4616 code_convert_region1 (start
, end
, coding_system
, encodep
)
4617 Lisp_Object start
, end
, coding_system
;
4620 struct coding_system coding
;
4623 CHECK_NUMBER_COERCE_MARKER (start
, 0);
4624 CHECK_NUMBER_COERCE_MARKER (end
, 1);
4625 CHECK_SYMBOL (coding_system
, 2);
4627 validate_region (&start
, &end
);
4628 from
= XFASTINT (start
);
4629 to
= XFASTINT (end
);
4631 if (NILP (coding_system
))
4632 return make_number (to
- from
);
4634 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
4635 error ("Invalid coding system: %s", XSYMBOL (coding_system
)->name
->data
);
4637 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
4638 code_convert_region (from
, CHAR_TO_BYTE (from
), to
, CHAR_TO_BYTE (to
),
4639 &coding
, encodep
, 1);
4640 Vlast_coding_system_used
= coding
.symbol
;
4641 return make_number (coding
.produced_char
);
4644 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
4645 3, 3, "r\nzCoding system: ",
4646 "Decode the current region by specified coding system.\n\
4647 When called from a program, takes three arguments:\n\
4648 START, END, and CODING-SYSTEM. START and END are buffer positions.\n\
4649 This function sets `last-coding-system-used' to the precise coding system\n\
4650 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is\n\
4651 not fully specified.)\n\
4652 It returns the length of the decoded text.")
4653 (start
, end
, coding_system
)
4654 Lisp_Object start
, end
, coding_system
;
4656 return code_convert_region1 (start
, end
, coding_system
, 0);
4659 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
4660 3, 3, "r\nzCoding system: ",
4661 "Encode the current region by specified coding system.\n\
4662 When called from a program, takes three arguments:\n\
4663 START, END, and CODING-SYSTEM. START and END are buffer positions.\n\
4664 This function sets `last-coding-system-used' to the precise coding system\n\
4665 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is\n\
4666 not fully specified.)\n\
4667 It returns the length of the encoded text.")
4668 (start
, end
, coding_system
)
4669 Lisp_Object start
, end
, coding_system
;
4671 return code_convert_region1 (start
, end
, coding_system
, 1);
4675 code_convert_string1 (string
, coding_system
, nocopy
, encodep
)
4676 Lisp_Object string
, coding_system
, nocopy
;
4679 struct coding_system coding
;
4681 CHECK_STRING (string
, 0);
4682 CHECK_SYMBOL (coding_system
, 1);
4684 if (NILP (coding_system
))
4685 return (NILP (nocopy
) ? Fcopy_sequence (string
) : string
);
4687 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
4688 error ("Invalid coding system: %s", XSYMBOL (coding_system
)->name
->data
);
4690 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
4691 Vlast_coding_system_used
= coding
.symbol
;
4692 return code_convert_string (string
, &coding
, encodep
, !NILP (nocopy
));
4695 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
4697 "Decode STRING which is encoded in CODING-SYSTEM, and return the result.\n\
4698 Optional arg NOCOPY non-nil means it is ok to return STRING itself\n\
4699 if the decoding operation is trivial.\n\
4700 This function sets `last-coding-system-used' to the precise coding system\n\
4701 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is\n\
4702 not fully specified.)")
4703 (string
, coding_system
, nocopy
)
4704 Lisp_Object string
, coding_system
, nocopy
;
4706 return code_convert_string1 (string
, coding_system
, nocopy
, 0);
4709 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
4711 "Encode STRING to CODING-SYSTEM, and return the result.\n\
4712 Optional arg NOCOPY non-nil means it is ok to return STRING itself\n\
4713 if the encoding operation is trivial.\n\
4714 This function sets `last-coding-system-used' to the precise coding system\n\
4715 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is\n\
4716 not fully specified.)")
4717 (string
, coding_system
, nocopy
)
4718 Lisp_Object string
, coding_system
, nocopy
;
4720 return code_convert_string1 (string
, coding_system
, nocopy
, 1);
4724 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
4725 "Decode a JISX0208 character of shift-jis encoding.\n\
4726 CODE is the character code in SJIS.\n\
4727 Return the corresponding character.")
4731 unsigned char c1
, c2
, s1
, s2
;
4734 CHECK_NUMBER (code
, 0);
4735 s1
= (XFASTINT (code
)) >> 8, s2
= (XFASTINT (code
)) & 0xFF;
4736 DECODE_SJIS (s1
, s2
, c1
, c2
);
4737 XSETFASTINT (val
, MAKE_NON_ASCII_CHAR (charset_jisx0208
, c1
, c2
));
4741 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
4742 "Encode a JISX0208 character CHAR to SJIS coding system.\n\
4743 Return the corresponding character code in SJIS.")
4747 int charset
, c1
, c2
, s1
, s2
;
4750 CHECK_NUMBER (ch
, 0);
4751 SPLIT_CHAR (XFASTINT (ch
), charset
, c1
, c2
);
4752 if (charset
== charset_jisx0208
)
4754 ENCODE_SJIS (c1
, c2
, s1
, s2
);
4755 XSETFASTINT (val
, (s1
<< 8) | s2
);
4758 XSETFASTINT (val
, 0);
4762 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
4763 "Decode a Big5 character CODE of BIG5 coding system.\n\
4764 CODE is the character code in BIG5.\n\
4765 Return the corresponding character.")
4770 unsigned char b1
, b2
, c1
, c2
;
4773 CHECK_NUMBER (code
, 0);
4774 b1
= (XFASTINT (code
)) >> 8, b2
= (XFASTINT (code
)) & 0xFF;
4775 DECODE_BIG5 (b1
, b2
, charset
, c1
, c2
);
4776 XSETFASTINT (val
, MAKE_NON_ASCII_CHAR (charset
, c1
, c2
));
4780 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
4781 "Encode the Big5 character CHAR to BIG5 coding system.\n\
4782 Return the corresponding character code in Big5.")
4786 int charset
, c1
, c2
, b1
, b2
;
4789 CHECK_NUMBER (ch
, 0);
4790 SPLIT_CHAR (XFASTINT (ch
), charset
, c1
, c2
);
4791 if (charset
== charset_big5_1
|| charset
== charset_big5_2
)
4793 ENCODE_BIG5 (charset
, c1
, c2
, b1
, b2
);
4794 XSETFASTINT (val
, (b1
<< 8) | b2
);
4797 XSETFASTINT (val
, 0);
4801 DEFUN ("set-terminal-coding-system-internal",
4802 Fset_terminal_coding_system_internal
,
4803 Sset_terminal_coding_system_internal
, 1, 1, 0, "")
4805 Lisp_Object coding_system
;
4807 CHECK_SYMBOL (coding_system
, 0);
4808 setup_coding_system (Fcheck_coding_system (coding_system
), &terminal_coding
);
4809 /* We had better not send unsafe characters to terminal. */
4810 terminal_coding
.flags
|= CODING_FLAG_ISO_SAFE
;
4815 DEFUN ("set-safe-terminal-coding-system-internal",
4816 Fset_safe_terminal_coding_system_internal
,
4817 Sset_safe_terminal_coding_system_internal
, 1, 1, 0, "")
4819 Lisp_Object coding_system
;
4821 CHECK_SYMBOL (coding_system
, 0);
4822 setup_coding_system (Fcheck_coding_system (coding_system
),
4823 &safe_terminal_coding
);
4827 DEFUN ("terminal-coding-system",
4828 Fterminal_coding_system
, Sterminal_coding_system
, 0, 0, 0,
4829 "Return coding system specified for terminal output.")
4832 return terminal_coding
.symbol
;
4835 DEFUN ("set-keyboard-coding-system-internal",
4836 Fset_keyboard_coding_system_internal
,
4837 Sset_keyboard_coding_system_internal
, 1, 1, 0, "")
4839 Lisp_Object coding_system
;
4841 CHECK_SYMBOL (coding_system
, 0);
4842 setup_coding_system (Fcheck_coding_system (coding_system
), &keyboard_coding
);
4846 DEFUN ("keyboard-coding-system",
4847 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 0, 0,
4848 "Return coding system specified for decoding keyboard input.")
4851 return keyboard_coding
.symbol
;
4855 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
4856 Sfind_operation_coding_system
, 1, MANY
, 0,
4857 "Choose a coding system for an operation based on the target name.\n\
4858 The value names a pair of coding systems: (DECODING-SYSTEM ENCODING-SYSTEM).\n\
4859 DECODING-SYSTEM is the coding system to use for decoding\n\
4860 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system\n\
4861 for encoding (in case OPERATION does encoding).\n\
4863 The first argument OPERATION specifies an I/O primitive:\n\
4864 For file I/O, `insert-file-contents' or `write-region'.\n\
4865 For process I/O, `call-process', `call-process-region', or `start-process'.\n\
4866 For network I/O, `open-network-stream'.\n\
4868 The remaining arguments should be the same arguments that were passed\n\
4869 to the primitive. Depending on which primitive, one of those arguments\n\
4870 is selected as the TARGET. For example, if OPERATION does file I/O,\n\
4871 whichever argument specifies the file name is TARGET.\n\
4873 TARGET has a meaning which depends on OPERATION:\n\
4874 For file I/O, TARGET is a file name.\n\
4875 For process I/O, TARGET is a process name.\n\
4876 For network I/O, TARGET is a service name or a port number\n\
4878 This function looks up what specified for TARGET in,\n\
4879 `file-coding-system-alist', `process-coding-system-alist',\n\
4880 or `network-coding-system-alist' depending on OPERATION.\n\
4881 They may specify a coding system, a cons of coding systems,\n\
4882 or a function symbol to call.\n\
4883 In the last case, we call the function with one argument,\n\
4884 which is a list of all the arguments given to this function.")
4889 Lisp_Object operation
, target_idx
, target
, val
;
4890 register Lisp_Object chain
;
4893 error ("Too few arguments");
4894 operation
= args
[0];
4895 if (!SYMBOLP (operation
)
4896 || !INTEGERP (target_idx
= Fget (operation
, Qtarget_idx
)))
4897 error ("Invalid first arguement");
4898 if (nargs
< 1 + XINT (target_idx
))
4899 error ("Too few arguments for operation: %s",
4900 XSYMBOL (operation
)->name
->data
);
4901 target
= args
[XINT (target_idx
) + 1];
4902 if (!(STRINGP (target
)
4903 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
4904 error ("Invalid %dth argument", XINT (target_idx
) + 1);
4906 chain
= ((EQ (operation
, Qinsert_file_contents
)
4907 || EQ (operation
, Qwrite_region
))
4908 ? Vfile_coding_system_alist
4909 : (EQ (operation
, Qopen_network_stream
)
4910 ? Vnetwork_coding_system_alist
4911 : Vprocess_coding_system_alist
));
4915 for (; CONSP (chain
); chain
= XCONS (chain
)->cdr
)
4918 elt
= XCONS (chain
)->car
;
4921 && ((STRINGP (target
)
4922 && STRINGP (XCONS (elt
)->car
)
4923 && fast_string_match (XCONS (elt
)->car
, target
) >= 0)
4924 || (INTEGERP (target
) && EQ (target
, XCONS (elt
)->car
))))
4926 val
= XCONS (elt
)->cdr
;
4927 /* Here, if VAL is both a valid coding system and a valid
4928 function symbol, we return VAL as a coding system. */
4931 if (! SYMBOLP (val
))
4933 if (! NILP (Fcoding_system_p (val
)))
4934 return Fcons (val
, val
);
4935 if (! NILP (Ffboundp (val
)))
4937 val
= call1 (val
, Flist (nargs
, args
));
4940 if (SYMBOLP (val
) && ! NILP (Fcoding_system_p (val
)))
4941 return Fcons (val
, val
);
4949 DEFUN ("update-iso-coding-systems", Fupdate_iso_coding_systems
,
4950 Supdate_iso_coding_systems
, 0, 0, 0,
4951 "Update internal database for ISO2022 based coding systems.\n\
4952 When values of the following coding categories are changed, you must\n\
4953 call this function:\n\
4954 coding-category-iso-7, coding-category-iso-7-tight,\n\
4955 coding-category-iso-8-1, coding-category-iso-8-2,\n\
4956 coding-category-iso-7-else, coding-category-iso-8-else")
4961 for (i
= CODING_CATEGORY_IDX_ISO_7
; i
<= CODING_CATEGORY_IDX_ISO_8_ELSE
;
4964 if (! coding_system_table
[i
])
4965 coding_system_table
[i
]
4966 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
4968 (XSYMBOL (XVECTOR (Vcoding_category_table
)->contents
[i
])->value
,
4969 coding_system_table
[i
]);
4974 DEFUN ("set-coding-priority-internal", Fset_coding_priority_internal
,
4975 Sset_coding_priority_internal
, 0, 0, 0,
4976 "Update internal database for the current value of `coding-category-list'.\n\
4977 This function is internal use only.")
4981 Lisp_Object val
= Vcoding_category_list
;
4983 while (CONSP (val
) && i
< CODING_CATEGORY_IDX_MAX
)
4985 if (! SYMBOLP (XCONS (val
)->car
))
4987 idx
= XFASTINT (Fget (XCONS (val
)->car
, Qcoding_category_index
));
4988 if (idx
>= CODING_CATEGORY_IDX_MAX
)
4990 coding_priorities
[i
++] = (1 << idx
);
4991 val
= XCONS (val
)->cdr
;
4993 /* If coding-category-list is valid and contains all coding
4994 categories, `i' should be CODING_CATEGORY_IDX_MAX now. If not,
4995 the following code saves Emacs from craching. */
4996 while (i
< CODING_CATEGORY_IDX_MAX
)
4997 coding_priorities
[i
++] = CODING_CATEGORY_MASK_RAW_TEXT
;
5005 /*** 8. Post-amble ***/
5012 /* Emacs' internal format specific initialize routine. */
5013 for (i
= 0; i
<= 0x20; i
++)
5014 emacs_code_class
[i
] = EMACS_control_code
;
5015 emacs_code_class
[0x0A] = EMACS_linefeed_code
;
5016 emacs_code_class
[0x0D] = EMACS_carriage_return_code
;
5017 for (i
= 0x21 ; i
< 0x7F; i
++)
5018 emacs_code_class
[i
] = EMACS_ascii_code
;
5019 emacs_code_class
[0x7F] = EMACS_control_code
;
5020 emacs_code_class
[0x80] = EMACS_leading_code_composition
;
5021 for (i
= 0x81; i
< 0xFF; i
++)
5022 emacs_code_class
[i
] = EMACS_invalid_code
;
5023 emacs_code_class
[LEADING_CODE_PRIVATE_11
] = EMACS_leading_code_3
;
5024 emacs_code_class
[LEADING_CODE_PRIVATE_12
] = EMACS_leading_code_3
;
5025 emacs_code_class
[LEADING_CODE_PRIVATE_21
] = EMACS_leading_code_4
;
5026 emacs_code_class
[LEADING_CODE_PRIVATE_22
] = EMACS_leading_code_4
;
5028 /* ISO2022 specific initialize routine. */
5029 for (i
= 0; i
< 0x20; i
++)
5030 iso_code_class
[i
] = ISO_control_code
;
5031 for (i
= 0x21; i
< 0x7F; i
++)
5032 iso_code_class
[i
] = ISO_graphic_plane_0
;
5033 for (i
= 0x80; i
< 0xA0; i
++)
5034 iso_code_class
[i
] = ISO_control_code
;
5035 for (i
= 0xA1; i
< 0xFF; i
++)
5036 iso_code_class
[i
] = ISO_graphic_plane_1
;
5037 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
5038 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
5039 iso_code_class
[ISO_CODE_CR
] = ISO_carriage_return
;
5040 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
5041 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
5042 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
5043 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
5044 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
5045 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
5046 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
5048 conversion_buffer_size
= MINIMUM_CONVERSION_BUFFER_SIZE
;
5049 conversion_buffer
= (char *) xmalloc (MINIMUM_CONVERSION_BUFFER_SIZE
);
5051 setup_coding_system (Qnil
, &keyboard_coding
);
5052 setup_coding_system (Qnil
, &terminal_coding
);
5053 setup_coding_system (Qnil
, &safe_terminal_coding
);
5055 bzero (coding_system_table
, sizeof coding_system_table
);
5057 bzero (ascii_skip_code
, sizeof ascii_skip_code
);
5058 for (i
= 0; i
< 128; i
++)
5059 ascii_skip_code
[i
] = 1;
5061 #if defined (MSDOS) || defined (WINDOWSNT)
5062 system_eol_type
= CODING_EOL_CRLF
;
5064 system_eol_type
= CODING_EOL_LF
;
5073 Qtarget_idx
= intern ("target-idx");
5074 staticpro (&Qtarget_idx
);
5076 Qcoding_system_history
= intern ("coding-system-history");
5077 staticpro (&Qcoding_system_history
);
5078 Fset (Qcoding_system_history
, Qnil
);
5080 /* Target FILENAME is the first argument. */
5081 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
5082 /* Target FILENAME is the third argument. */
5083 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
5085 Qcall_process
= intern ("call-process");
5086 staticpro (&Qcall_process
);
5087 /* Target PROGRAM is the first argument. */
5088 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
5090 Qcall_process_region
= intern ("call-process-region");
5091 staticpro (&Qcall_process_region
);
5092 /* Target PROGRAM is the third argument. */
5093 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
5095 Qstart_process
= intern ("start-process");
5096 staticpro (&Qstart_process
);
5097 /* Target PROGRAM is the third argument. */
5098 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
5100 Qopen_network_stream
= intern ("open-network-stream");
5101 staticpro (&Qopen_network_stream
);
5102 /* Target SERVICE is the fourth argument. */
5103 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
5105 Qcoding_system
= intern ("coding-system");
5106 staticpro (&Qcoding_system
);
5108 Qeol_type
= intern ("eol-type");
5109 staticpro (&Qeol_type
);
5111 Qbuffer_file_coding_system
= intern ("buffer-file-coding-system");
5112 staticpro (&Qbuffer_file_coding_system
);
5114 Qpost_read_conversion
= intern ("post-read-conversion");
5115 staticpro (&Qpost_read_conversion
);
5117 Qpre_write_conversion
= intern ("pre-write-conversion");
5118 staticpro (&Qpre_write_conversion
);
5120 Qno_conversion
= intern ("no-conversion");
5121 staticpro (&Qno_conversion
);
5123 Qundecided
= intern ("undecided");
5124 staticpro (&Qundecided
);
5126 Qcoding_system_p
= intern ("coding-system-p");
5127 staticpro (&Qcoding_system_p
);
5129 Qcoding_system_error
= intern ("coding-system-error");
5130 staticpro (&Qcoding_system_error
);
5132 Fput (Qcoding_system_error
, Qerror_conditions
,
5133 Fcons (Qcoding_system_error
, Fcons (Qerror
, Qnil
)));
5134 Fput (Qcoding_system_error
, Qerror_message
,
5135 build_string ("Invalid coding system"));
5137 Qcoding_category
= intern ("coding-category");
5138 staticpro (&Qcoding_category
);
5139 Qcoding_category_index
= intern ("coding-category-index");
5140 staticpro (&Qcoding_category_index
);
5142 Vcoding_category_table
5143 = Fmake_vector (make_number (CODING_CATEGORY_IDX_MAX
), Qnil
);
5144 staticpro (&Vcoding_category_table
);
5147 for (i
= 0; i
< CODING_CATEGORY_IDX_MAX
; i
++)
5149 XVECTOR (Vcoding_category_table
)->contents
[i
]
5150 = intern (coding_category_name
[i
]);
5151 Fput (XVECTOR (Vcoding_category_table
)->contents
[i
],
5152 Qcoding_category_index
, make_number (i
));
5156 Qtranslation_table
= intern ("translation-table");
5157 staticpro (&Qtranslation_table
);
5158 Fput (Qtranslation_table
, Qchar_table_extra_slots
, make_number (0));
5160 Qtranslation_table_id
= intern ("translation-table-id");
5161 staticpro (&Qtranslation_table_id
);
5163 Qtranslation_table_for_decode
= intern ("translation-table-for-decode");
5164 staticpro (&Qtranslation_table_for_decode
);
5166 Qtranslation_table_for_encode
= intern ("translation-table-for-encode");
5167 staticpro (&Qtranslation_table_for_encode
);
5169 Qsafe_charsets
= intern ("safe-charsets");
5170 staticpro (&Qsafe_charsets
);
5172 Qemacs_mule
= intern ("emacs-mule");
5173 staticpro (&Qemacs_mule
);
5175 Qraw_text
= intern ("raw-text");
5176 staticpro (&Qraw_text
);
5178 defsubr (&Scoding_system_p
);
5179 defsubr (&Sread_coding_system
);
5180 defsubr (&Sread_non_nil_coding_system
);
5181 defsubr (&Scheck_coding_system
);
5182 defsubr (&Sdetect_coding_region
);
5183 defsubr (&Sdetect_coding_string
);
5184 defsubr (&Sdecode_coding_region
);
5185 defsubr (&Sencode_coding_region
);
5186 defsubr (&Sdecode_coding_string
);
5187 defsubr (&Sencode_coding_string
);
5188 defsubr (&Sdecode_sjis_char
);
5189 defsubr (&Sencode_sjis_char
);
5190 defsubr (&Sdecode_big5_char
);
5191 defsubr (&Sencode_big5_char
);
5192 defsubr (&Sset_terminal_coding_system_internal
);
5193 defsubr (&Sset_safe_terminal_coding_system_internal
);
5194 defsubr (&Sterminal_coding_system
);
5195 defsubr (&Sset_keyboard_coding_system_internal
);
5196 defsubr (&Skeyboard_coding_system
);
5197 defsubr (&Sfind_operation_coding_system
);
5198 defsubr (&Supdate_iso_coding_systems
);
5199 defsubr (&Sset_coding_priority_internal
);
5201 DEFVAR_LISP ("coding-system-list", &Vcoding_system_list
,
5202 "List of coding systems.\n\
5204 Do not alter the value of this variable manually. This variable should be\n\
5205 updated by the functions `make-coding-system' and\n\
5206 `define-coding-system-alias'.");
5207 Vcoding_system_list
= Qnil
;
5209 DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist
,
5210 "Alist of coding system names.\n\
5211 Each element is one element list of coding system name.\n\
5212 This variable is given to `completing-read' as TABLE argument.\n\
5214 Do not alter the value of this variable manually. This variable should be\n\
5215 updated by the functions `make-coding-system' and\n\
5216 `define-coding-system-alias'.");
5217 Vcoding_system_alist
= Qnil
;
5219 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list
,
5220 "List of coding-categories (symbols) ordered by priority.");
5224 Vcoding_category_list
= Qnil
;
5225 for (i
= CODING_CATEGORY_IDX_MAX
- 1; i
>= 0; i
--)
5226 Vcoding_category_list
5227 = Fcons (XVECTOR (Vcoding_category_table
)->contents
[i
],
5228 Vcoding_category_list
);
5231 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read
,
5232 "Specify the coding system for read operations.\n\
5233 It is useful to bind this variable with `let', but do not set it globally.\n\
5234 If the value is a coding system, it is used for decoding on read operation.\n\
5235 If not, an appropriate element is used from one of the coding system alists:\n\
5236 There are three such tables, `file-coding-system-alist',\n\
5237 `process-coding-system-alist', and `network-coding-system-alist'.");
5238 Vcoding_system_for_read
= Qnil
;
5240 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write
,
5241 "Specify the coding system for write operations.\n\
5242 It is useful to bind this variable with `let', but do not set it globally.\n\
5243 If the value is a coding system, it is used for encoding on write operation.\n\
5244 If not, an appropriate element is used from one of the coding system alists:\n\
5245 There are three such tables, `file-coding-system-alist',\n\
5246 `process-coding-system-alist', and `network-coding-system-alist'.");
5247 Vcoding_system_for_write
= Qnil
;
5249 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used
,
5250 "Coding system used in the latest file or process I/O.");
5251 Vlast_coding_system_used
= Qnil
;
5253 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion
,
5254 "*Non-nil inhibit code conversion of end-of-line format in any cases.");
5255 inhibit_eol_conversion
= 0;
5257 DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system
,
5258 "Non-nil means process buffer inherits coding system of process output.\n\
5259 Bind it to t if the process output is to be treated as if it were a file\n\
5260 read from some filesystem.");
5261 inherit_process_coding_system
= 0;
5263 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist
,
5264 "Alist to decide a coding system to use for a file I/O operation.\n\
5265 The format is ((PATTERN . VAL) ...),\n\
5266 where PATTERN is a regular expression matching a file name,\n\
5267 VAL is a coding system, a cons of coding systems, or a function symbol.\n\
5268 If VAL is a coding system, it is used for both decoding and encoding\n\
5269 the file contents.\n\
5270 If VAL is a cons of coding systems, the car part is used for decoding,\n\
5271 and the cdr part is used for encoding.\n\
5272 If VAL is a function symbol, the function must return a coding system\n\
5273 or a cons of coding systems which are used as above.\n\
5275 See also the function `find-operation-coding-system'.");
5276 Vfile_coding_system_alist
= Qnil
;
5278 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist
,
5279 "Alist to decide a coding system to use for a process I/O operation.\n\
5280 The format is ((PATTERN . VAL) ...),\n\
5281 where PATTERN is a regular expression matching a program name,\n\
5282 VAL is a coding system, a cons of coding systems, or a function symbol.\n\
5283 If VAL is a coding system, it is used for both decoding what received\n\
5284 from the program and encoding what sent to the program.\n\
5285 If VAL is a cons of coding systems, the car part is used for decoding,\n\
5286 and the cdr part is used for encoding.\n\
5287 If VAL is a function symbol, the function must return a coding system\n\
5288 or a cons of coding systems which are used as above.\n\
5290 See also the function `find-operation-coding-system'.");
5291 Vprocess_coding_system_alist
= Qnil
;
5293 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist
,
5294 "Alist to decide a coding system to use for a network I/O operation.\n\
5295 The format is ((PATTERN . VAL) ...),\n\
5296 where PATTERN is a regular expression matching a network service name\n\
5297 or is a port number to connect to,\n\
5298 VAL is a coding system, a cons of coding systems, or a function symbol.\n\
5299 If VAL is a coding system, it is used for both decoding what received\n\
5300 from the network stream and encoding what sent to the network stream.\n\
5301 If VAL is a cons of coding systems, the car part is used for decoding,\n\
5302 and the cdr part is used for encoding.\n\
5303 If VAL is a function symbol, the function must return a coding system\n\
5304 or a cons of coding systems which are used as above.\n\
5306 See also the function `find-operation-coding-system'.");
5307 Vnetwork_coding_system_alist
= Qnil
;
5309 DEFVAR_INT ("eol-mnemonic-unix", &eol_mnemonic_unix
,
5310 "Mnemonic character indicating UNIX-like end-of-line format (i.e. LF) .");
5311 eol_mnemonic_unix
= ':';
5313 DEFVAR_INT ("eol-mnemonic-dos", &eol_mnemonic_dos
,
5314 "Mnemonic character indicating DOS-like end-of-line format (i.e. CRLF).");
5315 eol_mnemonic_dos
= '\\';
5317 DEFVAR_INT ("eol-mnemonic-mac", &eol_mnemonic_mac
,
5318 "Mnemonic character indicating MAC-like end-of-line format (i.e. CR).");
5319 eol_mnemonic_mac
= '/';
5321 DEFVAR_INT ("eol-mnemonic-undecided", &eol_mnemonic_undecided
,
5322 "Mnemonic character indicating end-of-line format is not yet decided.");
5323 eol_mnemonic_undecided
= ':';
5325 DEFVAR_LISP ("enable-character-translation", &Venable_character_translation
,
5326 "*Non-nil enables character translation while encoding and decoding.");
5327 Venable_character_translation
= Qt
;
5329 DEFVAR_LISP ("standard-translation-table-for-decode",
5330 &Vstandard_translation_table_for_decode
,
5331 "Table for translating characters while decoding.");
5332 Vstandard_translation_table_for_decode
= Qnil
;
5334 DEFVAR_LISP ("standard-translation-table-for-encode",
5335 &Vstandard_translation_table_for_encode
,
5336 "Table for translationg characters while encoding.");
5337 Vstandard_translation_table_for_encode
= Qnil
;
5339 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_alist
,
5340 "Alist of charsets vs revision numbers.\n\
5341 While encoding, if a charset (car part of an element) is found,\n\
5342 designate it with the escape sequence identifing revision (cdr part of the element).");
5343 Vcharset_revision_alist
= Qnil
;
5345 DEFVAR_LISP ("default-process-coding-system",
5346 &Vdefault_process_coding_system
,
5347 "Cons of coding systems used for process I/O by default.\n\
5348 The car part is used for decoding a process output,\n\
5349 the cdr part is used for encoding a text to be sent to a process.");
5350 Vdefault_process_coding_system
= Qnil
;
5352 DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table
,
5353 "Table of extra Latin codes in the range 128..159 (inclusive).\n\
5354 This is a vector of length 256.\n\
5355 If Nth element is non-nil, the existence of code N in a file\n\
5356 \(or output of subprocess) doesn't prevent it to be detected as\n\
5357 a coding system of ISO 2022 variant which has a flag\n\
5358 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file\n\
5359 or reading output of a subprocess.\n\
5360 Only 128th through 159th elements has a meaning.");
5361 Vlatin_extra_code_table
= Fmake_vector (make_number (256), Qnil
);
5363 DEFVAR_LISP ("select-safe-coding-system-function",
5364 &Vselect_safe_coding_system_function
,
5365 "Function to call to select safe coding system for encoding a text.\n\
5367 If set, this function is called to force a user to select a proper\n\
5368 coding system which can encode the text in the case that a default\n\
5369 coding system used in each operation can't encode the text.\n\
5371 The default value is `select-safe-codign-system' (which see).");
5372 Vselect_safe_coding_system_function
= Qnil
;