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 /* Coding system to be used to encode text for terminal display. */
325 struct coding_system terminal_coding
;
327 /* Coding system to be used to encode text for terminal display when
328 terminal coding system is nil. */
329 struct coding_system safe_terminal_coding
;
331 /* Coding system of what is sent from terminal keyboard. */
332 struct coding_system keyboard_coding
;
334 Lisp_Object Vfile_coding_system_alist
;
335 Lisp_Object Vprocess_coding_system_alist
;
336 Lisp_Object Vnetwork_coding_system_alist
;
340 Lisp_Object Qcoding_category
, Qcoding_category_index
;
342 /* List of symbols `coding-category-xxx' ordered by priority. */
343 Lisp_Object Vcoding_category_list
;
345 /* Table of coding categories (Lisp symbols). */
346 Lisp_Object Vcoding_category_table
;
348 /* Table of names of symbol for each coding-category. */
349 char *coding_category_name
[CODING_CATEGORY_IDX_MAX
] = {
350 "coding-category-emacs-mule",
351 "coding-category-sjis",
352 "coding-category-iso-7",
353 "coding-category-iso-7-tight",
354 "coding-category-iso-8-1",
355 "coding-category-iso-8-2",
356 "coding-category-iso-7-else",
357 "coding-category-iso-8-else",
358 "coding-category-big5",
359 "coding-category-raw-text",
360 "coding-category-binary"
363 /* Table pointers to coding systems corresponding to each coding
365 struct coding_system
*coding_system_table
[CODING_CATEGORY_IDX_MAX
];
367 /* Flag to tell if we look up unification table on character code
369 Lisp_Object Venable_character_unification
;
370 /* Standard unification table to look up on decoding (reading). */
371 Lisp_Object Vstandard_character_unification_table_for_decode
;
372 /* Standard unification table to look up on encoding (writing). */
373 Lisp_Object Vstandard_character_unification_table_for_encode
;
375 Lisp_Object Qcharacter_unification_table
;
376 Lisp_Object Qcharacter_unification_table_for_decode
;
377 Lisp_Object Qcharacter_unification_table_for_encode
;
379 /* Alist of charsets vs revision number. */
380 Lisp_Object Vcharset_revision_alist
;
382 /* Default coding systems used for process I/O. */
383 Lisp_Object Vdefault_process_coding_system
;
386 /*** 2. Emacs internal format (emacs-mule) handlers ***/
388 /* Emacs' internal format for encoding multiple character sets is a
389 kind of multi-byte encoding, i.e. characters are encoded by
390 variable-length sequences of one-byte codes. ASCII characters
391 and control characters (e.g. `tab', `newline') are represented by
392 one-byte sequences which are their ASCII codes, in the range 0x00
393 through 0x7F. The other characters are represented by a sequence
394 of `base leading-code', optional `extended leading-code', and one
395 or two `position-code's. The length of the sequence is determined
396 by the base leading-code. Leading-code takes the range 0x80
397 through 0x9F, whereas extended leading-code and position-code take
398 the range 0xA0 through 0xFF. See `charset.h' for more details
399 about leading-code and position-code.
401 There's one exception to this rule. Special leading-code
402 `leading-code-composition' denotes that the following several
403 characters should be composed into one character. Leading-codes of
404 components (except for ASCII) are added 0x20. An ASCII character
405 component is represented by a 2-byte sequence of `0xA0' and
406 `ASCII-code + 0x80'. See also the comments in `charset.h' for the
407 details of composite character. Hence, we can summarize the code
410 --- CODE RANGE of Emacs' internal format ---
411 (character set) (range)
413 ELSE (1st byte) 0x80 .. 0x9F
414 (rest bytes) 0xA0 .. 0xFF
415 ---------------------------------------------
419 enum emacs_code_class_type emacs_code_class
[256];
421 /* Go to the next statement only if *SRC is accessible and the code is
422 greater than 0xA0. */
423 #define CHECK_CODE_RANGE_A0_FF \
425 if (src >= src_end) \
426 goto label_end_of_switch; \
427 else if (*src++ < 0xA0) \
431 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
432 Check if a text is encoded in Emacs' internal format. If it is,
433 return CODING_CATEGORY_MASK_EMACS_MULE, else return 0. */
436 detect_coding_emacs_mule (src
, src_end
)
437 unsigned char *src
, *src_end
;
442 while (src
< src_end
)
454 switch (emacs_code_class
[c
])
456 case EMACS_ascii_code
:
457 case EMACS_linefeed_code
:
460 case EMACS_control_code
:
461 if (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
465 case EMACS_invalid_code
:
468 case EMACS_leading_code_composition
: /* c == 0x80 */
470 CHECK_CODE_RANGE_A0_FF
;
475 case EMACS_leading_code_4
:
476 CHECK_CODE_RANGE_A0_FF
;
477 /* fall down to check it two more times ... */
479 case EMACS_leading_code_3
:
480 CHECK_CODE_RANGE_A0_FF
;
481 /* fall down to check it one more time ... */
483 case EMACS_leading_code_2
:
484 CHECK_CODE_RANGE_A0_FF
;
492 return CODING_CATEGORY_MASK_EMACS_MULE
;
496 /*** 3. ISO2022 handlers ***/
498 /* The following note describes the coding system ISO2022 briefly.
499 Since the intention of this note is to help in understanding of
500 the programs in this file, some parts are NOT ACCURATE or OVERLY
501 SIMPLIFIED. For the thorough understanding, please refer to the
502 original document of ISO2022.
504 ISO2022 provides many mechanisms to encode several character sets
505 in 7-bit and 8-bit environment. If one chooses 7-bite environment,
506 all text is encoded by codes of less than 128. This may make the
507 encoded text a little bit longer, but the text gets more stability
508 to pass through several gateways (some of them strip off the MSB).
510 There are two kinds of character set: control character set and
511 graphic character set. The former contains control characters such
512 as `newline' and `escape' to provide control functions (control
513 functions are provided also by escape sequences). The latter
514 contains graphic characters such as ' A' and '-'. Emacs recognizes
515 two control character sets and many graphic character sets.
517 Graphic character sets are classified into one of the following
518 four classes, DIMENSION1_CHARS94, DIMENSION1_CHARS96,
519 DIMENSION2_CHARS94, DIMENSION2_CHARS96 according to the number of
520 bytes (DIMENSION) and the number of characters in one dimension
521 (CHARS) of the set. In addition, each character set is assigned an
522 identification tag (called "final character" and denoted as <F>
523 here after) which is unique in each class. <F> of each character
524 set is decided by ECMA(*) when it is registered in ISO. Code range
525 of <F> is 0x30..0x7F (0x30..0x3F are for private use only).
527 Note (*): ECMA = European Computer Manufacturers Association
529 Here are examples of graphic character set [NAME(<F>)]:
530 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
531 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
532 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
533 o DIMENSION2_CHARS96 -- none for the moment
535 A code area (1byte=8bits) is divided into 4 areas, C0, GL, C1, and GR.
536 C0 [0x00..0x1F] -- control character plane 0
537 GL [0x20..0x7F] -- graphic character plane 0
538 C1 [0x80..0x9F] -- control character plane 1
539 GR [0xA0..0xFF] -- graphic character plane 1
541 A control character set is directly designated and invoked to C0 or
542 C1 by an escape sequence. The most common case is that ISO646's
543 control character set is designated/invoked to C0 and ISO6429's
544 control character set is designated/invoked to C1, and usually
545 these designations/invocations are omitted in a coded text. With
546 7-bit environment, only C0 can be used, and a control character for
547 C1 is encoded by an appropriate escape sequence to fit in the
548 environment. All control characters for C1 are defined the
549 corresponding escape sequences.
551 A graphic character set is at first designated to one of four
552 graphic registers (G0 through G3), then these graphic registers are
553 invoked to GL or GR. These designations and invocations can be
554 done independently. The most common case is that G0 is invoked to
555 GL, G1 is invoked to GR, and ASCII is designated to G0, and usually
556 these invocations and designations are omitted in a coded text.
557 With 7-bit environment, only GL can be used.
559 When a graphic character set of CHARS94 is invoked to GL, code 0x20
560 and 0x7F of GL area work as control characters SPACE and DEL
561 respectively, and code 0xA0 and 0xFF of GR area should not be used.
563 There are two ways of invocation: locking-shift and single-shift.
564 With locking-shift, the invocation lasts until the next different
565 invocation, whereas with single-shift, the invocation works only
566 for the following character and doesn't affect locking-shift.
567 Invocations are done by the following control characters or escape
570 ----------------------------------------------------------------------
571 function control char escape sequence description
572 ----------------------------------------------------------------------
573 SI (shift-in) 0x0F none invoke G0 to GL
574 SO (shift-out) 0x0E none invoke G1 to GL
575 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
576 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
577 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 into GL
578 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 into GL
579 ----------------------------------------------------------------------
580 The first four are for locking-shift. Control characters for these
581 functions are defined by macros ISO_CODE_XXX in `coding.h'.
583 Designations are done by the following escape sequences.
584 ----------------------------------------------------------------------
585 escape sequence description
586 ----------------------------------------------------------------------
587 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
588 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
589 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
590 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
591 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
592 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
593 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
594 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
595 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
596 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
597 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
598 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
599 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
600 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
601 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
602 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
603 ----------------------------------------------------------------------
605 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
606 of dimension 1, chars 94, and final character <F>, and etc.
608 Note (*): Although these designations are not allowed in ISO2022,
609 Emacs accepts them on decoding, and produces them on encoding
610 CHARS96 character set in a coding system which is characterized as
611 7-bit environment, non-locking-shift, and non-single-shift.
613 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
614 '(' can be omitted. We call this as "short-form" here after.
616 Now you may notice that there are a lot of ways for encoding the
617 same multilingual text in ISO2022. Actually, there exists many
618 coding systems such as Compound Text (used in X's inter client
619 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
620 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
621 localized platforms), and all of these are variants of ISO2022.
623 In addition to the above, Emacs handles two more kinds of escape
624 sequences: ISO6429's direction specification and Emacs' private
625 sequence for specifying character composition.
627 ISO6429's direction specification takes the following format:
628 o CSI ']' -- end of the current direction
629 o CSI '0' ']' -- end of the current direction
630 o CSI '1' ']' -- start of left-to-right text
631 o CSI '2' ']' -- start of right-to-left text
632 The control character CSI (0x9B: control sequence introducer) is
633 abbreviated to the escape sequence ESC '[' in 7-bit environment.
635 Character composition specification takes the following format:
636 o ESC '0' -- start character composition
637 o ESC '1' -- end character composition
638 Since these are not standard escape sequences of any ISO, the use
639 of them for these meaning is restricted to Emacs only. */
641 enum iso_code_class_type iso_code_class
[256];
643 #define CHARSET_OK(idx, charset) \
644 (CODING_SPEC_ISO_REQUESTED_DESIGNATION \
645 (coding_system_table[idx], charset) \
646 != CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION)
648 #define SHIFT_OUT_OK(idx) \
649 (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding_system_table[idx], 1) >= 0)
651 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
652 Check if a text is encoded in ISO2022. If it is, returns an
653 integer in which appropriate flag bits any of:
654 CODING_CATEGORY_MASK_ISO_7
655 CODING_CATEGORY_MASK_ISO_7_TIGHT
656 CODING_CATEGORY_MASK_ISO_8_1
657 CODING_CATEGORY_MASK_ISO_8_2
658 CODING_CATEGORY_MASK_ISO_7_ELSE
659 CODING_CATEGORY_MASK_ISO_8_ELSE
660 are set. If a code which should never appear in ISO2022 is found,
664 detect_coding_iso2022 (src
, src_end
)
665 unsigned char *src
, *src_end
;
667 int mask
= CODING_CATEGORY_MASK_ISO
;
669 int reg
[4], shift_out
= 0;
670 int c
, c1
, i
, charset
;
672 reg
[0] = CHARSET_ASCII
, reg
[1] = reg
[2] = reg
[3] = -1;
673 while (mask
&& src
< src_end
)
682 if (c
>= '(' && c
<= '/')
684 /* Designation sequence for a charset of dimension 1. */
688 if (c1
< ' ' || c1
>= 0x80
689 || (charset
= iso_charset_table
[0][c
>= ','][c1
]) < 0)
690 /* Invalid designation sequence. Just ignore. */
692 reg
[(c
- '(') % 4] = charset
;
696 /* Designation sequence for a charset of dimension 2. */
700 if (c
>= '@' && c
<= 'B')
701 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
702 reg
[0] = charset
= iso_charset_table
[1][0][c
];
703 else if (c
>= '(' && c
<= '/')
708 if (c1
< ' ' || c1
>= 0x80
709 || (charset
= iso_charset_table
[1][c
>= ','][c1
]) < 0)
710 /* Invalid designation sequence. Just ignore. */
712 reg
[(c
- '(') % 4] = charset
;
715 /* Invalid designation sequence. Just ignore. */
718 else if (c
== 'N' || c
== 'n')
722 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_7_ELSE
)
723 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_8_ELSE
)))
725 /* Locking shift out. */
726 mask
&= ~CODING_CATEGORY_MASK_ISO_7BIT
;
727 mask_found
|= CODING_CATEGORY_MASK_ISO_SHIFT
;
732 else if (c
== 'O' || c
== 'o')
736 /* Locking shift in. */
737 mask
&= ~CODING_CATEGORY_MASK_ISO_7BIT
;
738 mask_found
|= CODING_CATEGORY_MASK_ISO_SHIFT
;
743 else if (c
== '0' || c
== '1' || c
== '2')
744 /* Start/end composition. Just ignore. */
747 /* Invalid escape sequence. Just ignore. */
750 /* We found a valid designation sequence for CHARSET. */
751 mask
&= ~CODING_CATEGORY_MASK_ISO_8BIT
;
752 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7
, charset
))
753 mask_found
|= CODING_CATEGORY_MASK_ISO_7
;
755 mask
&= ~CODING_CATEGORY_MASK_ISO_7
;
756 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7_TIGHT
, charset
))
757 mask_found
|= CODING_CATEGORY_MASK_ISO_7_TIGHT
;
759 mask
&= ~CODING_CATEGORY_MASK_ISO_7_TIGHT
;
760 if (! CHARSET_OK (CODING_CATEGORY_IDX_ISO_7_ELSE
, charset
))
761 mask
&= ~CODING_CATEGORY_MASK_ISO_7_ELSE
;
762 if (! CHARSET_OK (CODING_CATEGORY_IDX_ISO_8_ELSE
, charset
))
763 mask
&= ~CODING_CATEGORY_MASK_ISO_8_ELSE
;
769 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_7_ELSE
)
770 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_8_ELSE
)))
772 /* Locking shift out. */
773 mask
&= ~CODING_CATEGORY_MASK_ISO_7BIT
;
774 mask_found
|= CODING_CATEGORY_MASK_ISO_SHIFT
;
781 /* Locking shift in. */
782 mask
&= ~CODING_CATEGORY_MASK_ISO_7BIT
;
783 mask_found
|= CODING_CATEGORY_MASK_ISO_SHIFT
;
791 int newmask
= CODING_CATEGORY_MASK_ISO_8_ELSE
;
793 if (c
!= ISO_CODE_CSI
)
795 if (coding_system_table
[CODING_CATEGORY_IDX_ISO_8_1
]->flags
796 & CODING_FLAG_ISO_SINGLE_SHIFT
)
797 newmask
|= CODING_CATEGORY_MASK_ISO_8_1
;
798 if (coding_system_table
[CODING_CATEGORY_IDX_ISO_8_2
]->flags
799 & CODING_FLAG_ISO_SINGLE_SHIFT
)
800 newmask
|= CODING_CATEGORY_MASK_ISO_8_2
;
802 if (VECTORP (Vlatin_extra_code_table
)
803 && !NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
805 if (coding_system_table
[CODING_CATEGORY_IDX_ISO_8_1
]->flags
806 & CODING_FLAG_ISO_LATIN_EXTRA
)
807 newmask
|= CODING_CATEGORY_MASK_ISO_8_1
;
808 if (coding_system_table
[CODING_CATEGORY_IDX_ISO_8_2
]->flags
809 & CODING_FLAG_ISO_LATIN_EXTRA
)
810 newmask
|= CODING_CATEGORY_MASK_ISO_8_2
;
813 mask_found
|= newmask
;
822 if (VECTORP (Vlatin_extra_code_table
)
823 && !NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
827 if (coding_system_table
[CODING_CATEGORY_IDX_ISO_8_1
]->flags
828 & CODING_FLAG_ISO_LATIN_EXTRA
)
829 newmask
|= CODING_CATEGORY_MASK_ISO_8_1
;
830 if (coding_system_table
[CODING_CATEGORY_IDX_ISO_8_2
]->flags
831 & CODING_FLAG_ISO_LATIN_EXTRA
)
832 newmask
|= CODING_CATEGORY_MASK_ISO_8_2
;
834 mask_found
|= newmask
;
841 unsigned char *src_begin
= src
;
843 mask
&= ~(CODING_CATEGORY_MASK_ISO_7BIT
844 | CODING_CATEGORY_MASK_ISO_7_ELSE
);
845 mask_found
|= CODING_CATEGORY_MASK_ISO_8_1
;
846 while (src
< src_end
&& *src
>= 0xA0)
848 if ((src
- src_begin
- 1) & 1 && src
< src_end
)
849 mask
&= ~CODING_CATEGORY_MASK_ISO_8_2
;
851 mask_found
|= CODING_CATEGORY_MASK_ISO_8_2
;
857 return (mask
& mask_found
);
860 /* Decode a character of which charset is CHARSET and the 1st position
861 code is C1. If dimension of CHARSET is 2, the 2nd position code is
862 fetched from SRC and set to C2. If CHARSET is negative, it means
863 that we are decoding ill formed text, and what we can do is just to
866 #define DECODE_ISO_CHARACTER(charset, c1) \
868 int c_alt, charset_alt = (charset); \
869 if (COMPOSING_HEAD_P (coding->composing)) \
871 *dst++ = LEADING_CODE_COMPOSITION; \
872 if (COMPOSING_WITH_RULE_P (coding->composing)) \
873 /* To tell composition rules are embeded. */ \
875 coding->composing += 2; \
877 if ((charset) >= 0) \
879 if (CHARSET_DIMENSION (charset) == 2) \
881 ONE_MORE_BYTE (c2); \
882 if (iso_code_class[(c2) & 0x7F] != ISO_0x20_or_0x7F \
883 && iso_code_class[(c2) & 0x7F] != ISO_graphic_plane_0) \
889 if (!NILP (unification_table) \
890 && ((c_alt = unify_char (unification_table, \
891 -1, (charset), c1, c2)) >= 0)) \
892 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
894 if (charset_alt == CHARSET_ASCII || charset_alt < 0) \
895 DECODE_CHARACTER_ASCII (c1); \
896 else if (CHARSET_DIMENSION (charset_alt) == 1) \
897 DECODE_CHARACTER_DIMENSION1 (charset_alt, c1); \
899 DECODE_CHARACTER_DIMENSION2 (charset_alt, c1, c2); \
900 if (COMPOSING_WITH_RULE_P (coding->composing)) \
901 /* To tell a composition rule follows. */ \
902 coding->composing = COMPOSING_WITH_RULE_RULE; \
905 /* Set designation state into CODING. */
906 #define DECODE_DESIGNATION(reg, dimension, chars, final_char) \
908 int charset = ISO_CHARSET_TABLE (make_number (dimension), \
909 make_number (chars), \
910 make_number (final_char)); \
912 && CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) == reg) \
914 if (coding->spec.iso2022.last_invalid_designation_register == 0 \
916 && charset == CHARSET_ASCII) \
918 /* We should insert this designation sequence as is so \
919 that it is surely written back to a file. */ \
920 coding->spec.iso2022.last_invalid_designation_register = -1; \
921 goto label_invalid_code; \
923 coding->spec.iso2022.last_invalid_designation_register = -1; \
924 if ((coding->mode & CODING_MODE_DIRECTION) \
925 && CHARSET_REVERSE_CHARSET (charset) >= 0) \
926 charset = CHARSET_REVERSE_CHARSET (charset); \
927 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
931 coding->spec.iso2022.last_invalid_designation_register = reg; \
932 goto label_invalid_code; \
936 /* Check if the current composing sequence contains only valid codes.
937 If the composing sequence doesn't end before SRC_END, return -1.
938 Else, if it contains only valid codes, return 0.
939 Else return the length of the composing sequence. */
941 int check_composing_code (coding
, src
, src_end
)
942 struct coding_system
*coding
;
943 unsigned char *src
, *src_end
;
945 unsigned char *src_start
= src
;
946 int invalid_code_found
= 0;
947 int charset
, c
, c1
, dim
;
949 while (src
< src_end
)
951 if (*src
++ != ISO_CODE_ESC
) continue;
952 if (src
>= src_end
) break;
953 if ((c
= *src
++) == '1') /* end of compsition */
954 return (invalid_code_found
? src
- src_start
: 0);
955 if (src
+ 2 >= src_end
) break;
956 if (!coding
->flags
& CODING_FLAG_ISO_DESIGNATION
)
957 invalid_code_found
= 1;
964 c
= (*src
>= '@' && *src
<= 'B') ? '(' : *src
++;
966 if (c
>= '(' && c
<= '/')
969 if ((c1
< ' ' || c1
>= 0x80)
970 || (charset
= iso_charset_table
[dim
][c
>= ','][c1
]) < 0
971 || (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
972 == CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
))
973 invalid_code_found
= 1;
976 invalid_code_found
= 1;
979 return ((coding
->mode
& CODING_MODE_LAST_BLOCK
) ? src_end
- src_start
: -1);
982 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
985 decode_coding_iso2022 (coding
, source
, destination
, src_bytes
, dst_bytes
)
986 struct coding_system
*coding
;
987 unsigned char *source
, *destination
;
988 int src_bytes
, dst_bytes
;
990 unsigned char *src
= source
;
991 unsigned char *src_end
= source
+ src_bytes
;
992 unsigned char *dst
= destination
;
993 unsigned char *dst_end
= destination
+ dst_bytes
;
994 /* Since the maximum bytes produced by each loop is 7, we subtract 6
995 from DST_END to assure that overflow checking is necessary only
996 at the head of loop. */
997 unsigned char *adjusted_dst_end
= dst_end
- 6;
999 /* Charsets invoked to graphic plane 0 and 1 respectively. */
1000 int charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
1001 int charset1
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 1);
1002 Lisp_Object unification_table
1003 = coding
->character_unification_table_for_decode
;
1004 int result
= CODING_FINISH_NORMAL
;
1006 if (!NILP (Venable_character_unification
) && NILP (unification_table
))
1007 unification_table
= Vstandard_character_unification_table_for_decode
;
1009 coding
->produced_char
= 0;
1010 coding
->fake_multibyte
= 0;
1011 while (src
< src_end
&& (dst_bytes
1012 ? (dst
< adjusted_dst_end
)
1015 /* SRC_BASE remembers the start position in source in each loop.
1016 The loop will be exited when there's not enough source text
1017 to analyze long escape sequence or 2-byte code (within macros
1018 ONE_MORE_BYTE or TWO_MORE_BYTES). In that case, SRC is reset
1019 to SRC_BASE before exiting. */
1020 unsigned char *src_base
= src
;
1021 int c1
= *src
++, c2
;
1023 switch (iso_code_class
[c1
])
1025 case ISO_0x20_or_0x7F
:
1026 if (!coding
->composing
1027 && (charset0
< 0 || CHARSET_CHARS (charset0
) == 94))
1029 /* This is SPACE or DEL. */
1031 coding
->produced_char
++;
1034 /* This is a graphic character, we fall down ... */
1036 case ISO_graphic_plane_0
:
1037 if (coding
->composing
== COMPOSING_WITH_RULE_RULE
)
1039 /* This is a composition rule. */
1041 coding
->composing
= COMPOSING_WITH_RULE_TAIL
;
1044 DECODE_ISO_CHARACTER (charset0
, c1
);
1047 case ISO_0xA0_or_0xFF
:
1048 if (charset1
< 0 || CHARSET_CHARS (charset1
) == 94
1049 || coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
)
1050 goto label_invalid_code
;
1051 /* This is a graphic character, we fall down ... */
1053 case ISO_graphic_plane_1
:
1054 if (coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
)
1055 goto label_invalid_code
;
1057 DECODE_ISO_CHARACTER (charset1
, c1
);
1060 case ISO_control_code
:
1061 /* All ISO2022 control characters in this class have the
1062 same representation in Emacs internal format. */
1064 && (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
)
1065 && (coding
->eol_type
== CODING_EOL_CR
1066 || coding
->eol_type
== CODING_EOL_CRLF
))
1068 result
= CODING_FINISH_INCONSISTENT_EOL
;
1069 goto label_end_of_loop_2
;
1072 coding
->produced_char
++;
1075 case ISO_carriage_return
:
1076 if (coding
->eol_type
== CODING_EOL_CR
)
1078 else if (coding
->eol_type
== CODING_EOL_CRLF
)
1081 if (c1
== ISO_CODE_LF
)
1085 if (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
)
1087 result
= CODING_FINISH_INCONSISTENT_EOL
;
1088 goto label_end_of_loop_2
;
1096 coding
->produced_char
++;
1100 if (! (coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
)
1101 || CODING_SPEC_ISO_DESIGNATION (coding
, 1) < 0)
1102 goto label_invalid_code
;
1103 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 1;
1104 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
1108 if (! (coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
))
1109 goto label_invalid_code
;
1110 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 0;
1111 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
1114 case ISO_single_shift_2_7
:
1115 case ISO_single_shift_2
:
1116 if (! (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
))
1117 goto label_invalid_code
;
1118 /* SS2 is handled as an escape sequence of ESC 'N' */
1120 goto label_escape_sequence
;
1122 case ISO_single_shift_3
:
1123 if (! (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
))
1124 goto label_invalid_code
;
1125 /* SS2 is handled as an escape sequence of ESC 'O' */
1127 goto label_escape_sequence
;
1129 case ISO_control_sequence_introducer
:
1130 /* CSI is handled as an escape sequence of ESC '[' ... */
1132 goto label_escape_sequence
;
1136 label_escape_sequence
:
1137 /* Escape sequences handled by Emacs are invocation,
1138 designation, direction specification, and character
1139 composition specification. */
1142 case '&': /* revision of following character set */
1144 if (!(c1
>= '@' && c1
<= '~'))
1145 goto label_invalid_code
;
1147 if (c1
!= ISO_CODE_ESC
)
1148 goto label_invalid_code
;
1150 goto label_escape_sequence
;
1152 case '$': /* designation of 2-byte character set */
1153 if (! (coding
->flags
& CODING_FLAG_ISO_DESIGNATION
))
1154 goto label_invalid_code
;
1156 if (c1
>= '@' && c1
<= 'B')
1157 { /* designation of JISX0208.1978, GB2312.1980,
1159 DECODE_DESIGNATION (0, 2, 94, c1
);
1161 else if (c1
>= 0x28 && c1
<= 0x2B)
1162 { /* designation of DIMENSION2_CHARS94 character set */
1164 DECODE_DESIGNATION (c1
- 0x28, 2, 94, c2
);
1166 else if (c1
>= 0x2C && c1
<= 0x2F)
1167 { /* designation of DIMENSION2_CHARS96 character set */
1169 DECODE_DESIGNATION (c1
- 0x2C, 2, 96, c2
);
1172 goto label_invalid_code
;
1175 case 'n': /* invocation of locking-shift-2 */
1176 if (! (coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
)
1177 || CODING_SPEC_ISO_DESIGNATION (coding
, 2) < 0)
1178 goto label_invalid_code
;
1179 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 2;
1180 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
1183 case 'o': /* invocation of locking-shift-3 */
1184 if (! (coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
)
1185 || CODING_SPEC_ISO_DESIGNATION (coding
, 3) < 0)
1186 goto label_invalid_code
;
1187 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 3;
1188 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
1191 case 'N': /* invocation of single-shift-2 */
1192 if (! (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
1193 || CODING_SPEC_ISO_DESIGNATION (coding
, 2) < 0)
1194 goto label_invalid_code
;
1196 charset
= CODING_SPEC_ISO_DESIGNATION (coding
, 2);
1197 DECODE_ISO_CHARACTER (charset
, c1
);
1200 case 'O': /* invocation of single-shift-3 */
1201 if (! (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
1202 || CODING_SPEC_ISO_DESIGNATION (coding
, 3) < 0)
1203 goto label_invalid_code
;
1205 charset
= CODING_SPEC_ISO_DESIGNATION (coding
, 3);
1206 DECODE_ISO_CHARACTER (charset
, c1
);
1209 case '0': case '2': /* start composing */
1210 /* Before processing composing, we must be sure that all
1211 characters being composed are supported by CODING.
1212 If not, we must give up composing and insert the
1213 bunch of codes for composing as is without decoding. */
1217 result1
= check_composing_code (coding
, src
, src_end
);
1219 coding
->composing
= (c1
== '0'
1220 ? COMPOSING_NO_RULE_HEAD
1221 : COMPOSING_WITH_RULE_HEAD
);
1222 else if (result1
> 0)
1224 if (result1
+ 2 < (dst_bytes
? dst_end
: src_base
) - dst
)
1226 bcopy (src_base
, dst
, result1
+ 2);
1229 coding
->produced_char
+= result1
+ 2;
1233 result
= CODING_FINISH_INSUFFICIENT_DST
;
1234 goto label_end_of_loop_2
;
1238 goto label_end_of_loop
;
1242 case '1': /* end composing */
1243 coding
->composing
= COMPOSING_NO
;
1244 coding
->produced_char
++;
1247 case '[': /* specification of direction */
1248 if (coding
->flags
& CODING_FLAG_ISO_NO_DIRECTION
)
1249 goto label_invalid_code
;
1250 /* For the moment, nested direction is not supported.
1251 So, `coding->mode & CODING_MODE_DIRECTION' zero means
1252 left-to-right, and nozero means right-to-left. */
1256 case ']': /* end of the current direction */
1257 coding
->mode
&= ~CODING_MODE_DIRECTION
;
1259 case '0': /* end of the current direction */
1260 case '1': /* start of left-to-right direction */
1263 coding
->mode
&= ~CODING_MODE_DIRECTION
;
1265 goto label_invalid_code
;
1268 case '2': /* start of right-to-left direction */
1271 coding
->mode
|= CODING_MODE_DIRECTION
;
1273 goto label_invalid_code
;
1277 goto label_invalid_code
;
1282 if (! (coding
->flags
& CODING_FLAG_ISO_DESIGNATION
))
1283 goto label_invalid_code
;
1284 if (c1
>= 0x28 && c1
<= 0x2B)
1285 { /* designation of DIMENSION1_CHARS94 character set */
1287 DECODE_DESIGNATION (c1
- 0x28, 1, 94, c2
);
1289 else if (c1
>= 0x2C && c1
<= 0x2F)
1290 { /* designation of DIMENSION1_CHARS96 character set */
1292 DECODE_DESIGNATION (c1
- 0x2C, 1, 96, c2
);
1296 goto label_invalid_code
;
1299 /* We must update these variables now. */
1300 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
1301 charset1
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 1);
1305 while (src_base
< src
)
1306 *dst
++ = *src_base
++;
1307 coding
->fake_multibyte
= 1;
1312 result
= CODING_FINISH_INSUFFICIENT_SRC
;
1313 label_end_of_loop_2
:
1320 if (result
== CODING_FINISH_NORMAL
)
1321 result
= CODING_FINISH_INSUFFICIENT_DST
;
1322 else if (result
!= CODING_FINISH_INCONSISTENT_EOL
1323 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
1325 /* This is the last block of the text to be decoded. We had
1326 better just flush out all remaining codes in the text
1327 although they are not valid characters. */
1328 src_bytes
= src_end
- src
;
1329 if (dst_bytes
&& (dst_end
- dst
< src_bytes
))
1330 src_bytes
= dst_end
- dst
;
1331 bcopy (src
, dst
, src_bytes
);
1334 coding
->fake_multibyte
= 1;
1338 coding
->consumed
= coding
->consumed_char
= src
- source
;
1339 coding
->produced
= dst
- destination
;
1343 /* ISO2022 encoding stuff. */
1346 It is not enough to say just "ISO2022" on encoding, we have to
1347 specify more details. In Emacs, each coding system of ISO2022
1348 variant has the following specifications:
1349 1. Initial designation to G0 thru G3.
1350 2. Allows short-form designation?
1351 3. ASCII should be designated to G0 before control characters?
1352 4. ASCII should be designated to G0 at end of line?
1353 5. 7-bit environment or 8-bit environment?
1354 6. Use locking-shift?
1355 7. Use Single-shift?
1356 And the following two are only for Japanese:
1357 8. Use ASCII in place of JIS0201-1976-Roman?
1358 9. Use JISX0208-1983 in place of JISX0208-1978?
1359 These specifications are encoded in `coding->flags' as flag bits
1360 defined by macros CODING_FLAG_ISO_XXX. See `coding.h' for more
1364 /* Produce codes (escape sequence) for designating CHARSET to graphic
1365 register REG. If <final-char> of CHARSET is '@', 'A', or 'B' and
1366 the coding system CODING allows, produce designation sequence of
1369 #define ENCODE_DESIGNATION(charset, reg, coding) \
1371 unsigned char final_char = CHARSET_ISO_FINAL_CHAR (charset); \
1372 char *intermediate_char_94 = "()*+"; \
1373 char *intermediate_char_96 = ",-./"; \
1374 int revision = CODING_SPEC_ISO_REVISION_NUMBER(coding, charset); \
1375 if (revision < 255) \
1377 *dst++ = ISO_CODE_ESC; \
1379 *dst++ = '@' + revision; \
1381 *dst++ = ISO_CODE_ESC; \
1382 if (CHARSET_DIMENSION (charset) == 1) \
1384 if (CHARSET_CHARS (charset) == 94) \
1385 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
1387 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
1392 if (CHARSET_CHARS (charset) == 94) \
1394 if (! (coding->flags & CODING_FLAG_ISO_SHORT_FORM) \
1396 || final_char < '@' || final_char > 'B') \
1397 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
1400 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
1402 *dst++ = final_char; \
1403 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
1406 /* The following two macros produce codes (control character or escape
1407 sequence) for ISO2022 single-shift functions (single-shift-2 and
1410 #define ENCODE_SINGLE_SHIFT_2 \
1412 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1413 *dst++ = ISO_CODE_ESC, *dst++ = 'N'; \
1416 *dst++ = ISO_CODE_SS2; \
1417 coding->fake_multibyte = 1; \
1419 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
1422 #define ENCODE_SINGLE_SHIFT_3 \
1424 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1425 *dst++ = ISO_CODE_ESC, *dst++ = 'O'; \
1428 *dst++ = ISO_CODE_SS3; \
1429 coding->fake_multibyte = 1; \
1431 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
1434 /* The following four macros produce codes (control character or
1435 escape sequence) for ISO2022 locking-shift functions (shift-in,
1436 shift-out, locking-shift-2, and locking-shift-3). */
1438 #define ENCODE_SHIFT_IN \
1440 *dst++ = ISO_CODE_SI; \
1441 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0; \
1444 #define ENCODE_SHIFT_OUT \
1446 *dst++ = ISO_CODE_SO; \
1447 CODING_SPEC_ISO_INVOCATION (coding, 0) = 1; \
1450 #define ENCODE_LOCKING_SHIFT_2 \
1452 *dst++ = ISO_CODE_ESC, *dst++ = 'n'; \
1453 CODING_SPEC_ISO_INVOCATION (coding, 0) = 2; \
1456 #define ENCODE_LOCKING_SHIFT_3 \
1458 *dst++ = ISO_CODE_ESC, *dst++ = 'o'; \
1459 CODING_SPEC_ISO_INVOCATION (coding, 0) = 3; \
1462 /* Produce codes for a DIMENSION1 character whose character set is
1463 CHARSET and whose position-code is C1. Designation and invocation
1464 sequences are also produced in advance if necessary. */
1467 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
1469 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
1471 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1472 *dst++ = c1 & 0x7F; \
1474 *dst++ = c1 | 0x80; \
1475 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
1478 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
1480 *dst++ = c1 & 0x7F; \
1483 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
1485 *dst++ = c1 | 0x80; \
1488 else if (coding->flags & CODING_FLAG_ISO_SAFE \
1489 && !coding->safe_charsets[charset]) \
1491 /* We should not encode this character, instead produce one or \
1493 *dst++ = CODING_INHIBIT_CHARACTER_SUBSTITUTION; \
1494 if (CHARSET_WIDTH (charset) == 2) \
1495 *dst++ = CODING_INHIBIT_CHARACTER_SUBSTITUTION; \
1499 /* Since CHARSET is not yet invoked to any graphic planes, we \
1500 must invoke it, or, at first, designate it to some graphic \
1501 register. Then repeat the loop to actually produce the \
1503 dst = encode_invocation_designation (charset, coding, dst); \
1506 /* Produce codes for a DIMENSION2 character whose character set is
1507 CHARSET and whose position-codes are C1 and C2. Designation and
1508 invocation codes are also produced in advance if necessary. */
1510 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
1512 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
1514 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1515 *dst++ = c1 & 0x7F, *dst++ = c2 & 0x7F; \
1517 *dst++ = c1 | 0x80, *dst++ = c2 | 0x80; \
1518 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
1521 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
1523 *dst++ = c1 & 0x7F, *dst++= c2 & 0x7F; \
1526 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
1528 *dst++ = c1 | 0x80, *dst++= c2 | 0x80; \
1531 else if (coding->flags & CODING_FLAG_ISO_SAFE \
1532 && !coding->safe_charsets[charset]) \
1534 /* We should not encode this character, instead produce one or \
1536 *dst++ = CODING_INHIBIT_CHARACTER_SUBSTITUTION; \
1537 if (CHARSET_WIDTH (charset) == 2) \
1538 *dst++ = CODING_INHIBIT_CHARACTER_SUBSTITUTION; \
1542 /* Since CHARSET is not yet invoked to any graphic planes, we \
1543 must invoke it, or, at first, designate it to some graphic \
1544 register. Then repeat the loop to actually produce the \
1546 dst = encode_invocation_designation (charset, coding, dst); \
1549 #define ENCODE_ISO_CHARACTER(charset, c1, c2) \
1551 int c_alt, charset_alt; \
1552 if (!NILP (unification_table) \
1553 && ((c_alt = unify_char (unification_table, -1, charset, c1, c2)) \
1555 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
1557 charset_alt = charset; \
1558 if (CHARSET_DIMENSION (charset_alt) == 1) \
1560 if (charset == CHARSET_ASCII \
1561 && coding->flags & CODING_FLAG_ISO_USE_ROMAN) \
1562 charset_alt = charset_latin_jisx0201; \
1563 ENCODE_ISO_CHARACTER_DIMENSION1 (charset_alt, c1); \
1567 if (charset == charset_jisx0208 \
1568 && coding->flags & CODING_FLAG_ISO_USE_OLDJIS) \
1569 charset_alt = charset_jisx0208_1978; \
1570 ENCODE_ISO_CHARACTER_DIMENSION2 (charset_alt, c1, c2); \
1572 if (! COMPOSING_P (coding->composing)) \
1573 coding->consumed_char++; \
1576 /* Produce designation and invocation codes at a place pointed by DST
1577 to use CHARSET. The element `spec.iso2022' of *CODING is updated.
1581 encode_invocation_designation (charset
, coding
, dst
)
1583 struct coding_system
*coding
;
1586 int reg
; /* graphic register number */
1588 /* At first, check designations. */
1589 for (reg
= 0; reg
< 4; reg
++)
1590 if (charset
== CODING_SPEC_ISO_DESIGNATION (coding
, reg
))
1595 /* CHARSET is not yet designated to any graphic registers. */
1596 /* At first check the requested designation. */
1597 reg
= CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
);
1598 if (reg
== CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
)
1599 /* Since CHARSET requests no special designation, designate it
1600 to graphic register 0. */
1603 ENCODE_DESIGNATION (charset
, reg
, coding
);
1606 if (CODING_SPEC_ISO_INVOCATION (coding
, 0) != reg
1607 && CODING_SPEC_ISO_INVOCATION (coding
, 1) != reg
)
1609 /* Since the graphic register REG is not invoked to any graphic
1610 planes, invoke it to graphic plane 0. */
1613 case 0: /* graphic register 0 */
1617 case 1: /* graphic register 1 */
1621 case 2: /* graphic register 2 */
1622 if (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
1623 ENCODE_SINGLE_SHIFT_2
;
1625 ENCODE_LOCKING_SHIFT_2
;
1628 case 3: /* graphic register 3 */
1629 if (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
1630 ENCODE_SINGLE_SHIFT_3
;
1632 ENCODE_LOCKING_SHIFT_3
;
1639 /* The following two macros produce codes for indicating composition. */
1640 #define ENCODE_COMPOSITION_NO_RULE_START *dst++ = ISO_CODE_ESC, *dst++ = '0'
1641 #define ENCODE_COMPOSITION_WITH_RULE_START *dst++ = ISO_CODE_ESC, *dst++ = '2'
1642 #define ENCODE_COMPOSITION_END *dst++ = ISO_CODE_ESC, *dst++ = '1'
1644 /* The following three macros produce codes for indicating direction
1646 #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
1648 if (coding->flags == CODING_FLAG_ISO_SEVEN_BITS) \
1649 *dst++ = ISO_CODE_ESC, *dst++ = '['; \
1651 *dst++ = ISO_CODE_CSI; \
1654 #define ENCODE_DIRECTION_R2L \
1655 ENCODE_CONTROL_SEQUENCE_INTRODUCER, *dst++ = '2', *dst++ = ']'
1657 #define ENCODE_DIRECTION_L2R \
1658 ENCODE_CONTROL_SEQUENCE_INTRODUCER, *dst++ = '0', *dst++ = ']'
1660 /* Produce codes for designation and invocation to reset the graphic
1661 planes and registers to initial state. */
1662 #define ENCODE_RESET_PLANE_AND_REGISTER \
1665 if (CODING_SPEC_ISO_INVOCATION (coding, 0) != 0) \
1667 for (reg = 0; reg < 4; reg++) \
1668 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg) >= 0 \
1669 && (CODING_SPEC_ISO_DESIGNATION (coding, reg) \
1670 != CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg))) \
1671 ENCODE_DESIGNATION \
1672 (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg), reg, coding); \
1675 /* Produce designation sequences of charsets in the line started from
1676 SRC to a place pointed by *DSTP, and update DSTP.
1678 If the current block ends before any end-of-line, we may fail to
1679 find all the necessary designations. */
1681 encode_designation_at_bol (coding
, table
, src
, src_end
, dstp
)
1682 struct coding_system
*coding
;
1684 unsigned char *src
, *src_end
, **dstp
;
1686 int charset
, c
, found
= 0, reg
;
1687 /* Table of charsets to be designated to each graphic register. */
1689 unsigned char *dst
= *dstp
;
1691 for (reg
= 0; reg
< 4; reg
++)
1694 while (src
< src_end
&& *src
!= '\n' && found
< 4)
1696 int bytes
= BYTES_BY_CHAR_HEAD (*src
);
1699 charset
= CHARSET_AT (src
);
1703 unsigned char c1
, c2
;
1705 SPLIT_STRING(src
, bytes
, charset
, c1
, c2
);
1706 if ((c_alt
= unify_char (table
, -1, charset
, c1
, c2
)) >= 0)
1707 charset
= CHAR_CHARSET (c_alt
);
1710 reg
= CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
);
1711 if (reg
!= CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
&& r
[reg
] < 0)
1722 for (reg
= 0; reg
< 4; reg
++)
1724 && CODING_SPEC_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
1725 ENCODE_DESIGNATION (r
[reg
], reg
, coding
);
1730 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
1733 encode_coding_iso2022 (coding
, source
, destination
, src_bytes
, dst_bytes
)
1734 struct coding_system
*coding
;
1735 unsigned char *source
, *destination
;
1736 int src_bytes
, dst_bytes
;
1738 unsigned char *src
= source
;
1739 unsigned char *src_end
= source
+ src_bytes
;
1740 unsigned char *dst
= destination
;
1741 unsigned char *dst_end
= destination
+ dst_bytes
;
1742 /* Since the maximum bytes produced by each loop is 20, we subtract 19
1743 from DST_END to assure overflow checking is necessary only at the
1745 unsigned char *adjusted_dst_end
= dst_end
- 19;
1746 Lisp_Object unification_table
1747 = coding
->character_unification_table_for_encode
;
1748 int result
= CODING_FINISH_NORMAL
;
1750 if (!NILP (Venable_character_unification
) && NILP (unification_table
))
1751 unification_table
= Vstandard_character_unification_table_for_encode
;
1753 coding
->consumed_char
= 0;
1754 coding
->fake_multibyte
= 0;
1755 while (src
< src_end
&& (dst_bytes
1756 ? (dst
< adjusted_dst_end
)
1757 : (dst
< src
- 19)))
1759 /* SRC_BASE remembers the start position in source in each loop.
1760 The loop will be exited when there's not enough source text
1761 to analyze multi-byte codes (within macros ONE_MORE_BYTE,
1762 TWO_MORE_BYTES, and THREE_MORE_BYTES). In that case, SRC is
1763 reset to SRC_BASE before exiting. */
1764 unsigned char *src_base
= src
;
1765 int charset
, c1
, c2
, c3
, c4
;
1767 if (coding
->flags
& CODING_FLAG_ISO_DESIGNATE_AT_BOL
1768 && CODING_SPEC_ISO_BOL (coding
))
1770 /* We have to produce designation sequences if any now. */
1771 encode_designation_at_bol (coding
, unification_table
,
1772 src
, src_end
, &dst
);
1773 CODING_SPEC_ISO_BOL (coding
) = 0;
1777 /* If we are seeing a component of a composite character, we are
1778 seeing a leading-code encoded irregularly for composition, or
1779 a composition rule if composing with rule. We must set C1 to
1780 a normal leading-code or an ASCII code. If we are not seeing
1781 a composite character, we must reset composition,
1782 designation, and invocation states. */
1783 if (COMPOSING_P (coding
->composing
))
1787 /* We are not in a composite character any longer. */
1788 coding
->composing
= COMPOSING_NO
;
1789 ENCODE_RESET_PLANE_AND_REGISTER
;
1790 ENCODE_COMPOSITION_END
;
1794 if (coding
->composing
== COMPOSING_WITH_RULE_RULE
)
1797 coding
->composing
= COMPOSING_WITH_RULE_HEAD
;
1800 else if (coding
->composing
== COMPOSING_WITH_RULE_HEAD
)
1801 coding
->composing
= COMPOSING_WITH_RULE_RULE
;
1804 /* This is an ASCII component. */
1809 /* This is a leading-code of non ASCII component. */
1814 /* Now encode one character. C1 is a control character, an
1815 ASCII character, or a leading-code of multi-byte character. */
1816 switch (emacs_code_class
[c1
])
1818 case EMACS_ascii_code
:
1819 ENCODE_ISO_CHARACTER (CHARSET_ASCII
, c1
, /* dummy */ c2
);
1822 case EMACS_control_code
:
1823 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_CNTL
)
1824 ENCODE_RESET_PLANE_AND_REGISTER
;
1826 coding
->consumed_char
++;
1829 case EMACS_carriage_return_code
:
1830 if (! (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
1832 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_CNTL
)
1833 ENCODE_RESET_PLANE_AND_REGISTER
;
1835 coding
->consumed_char
++;
1838 /* fall down to treat '\r' as '\n' ... */
1840 case EMACS_linefeed_code
:
1841 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_EOL
)
1842 ENCODE_RESET_PLANE_AND_REGISTER
;
1843 if (coding
->flags
& CODING_FLAG_ISO_INIT_AT_BOL
)
1844 bcopy (coding
->spec
.iso2022
.initial_designation
,
1845 coding
->spec
.iso2022
.current_designation
,
1846 sizeof coding
->spec
.iso2022
.initial_designation
);
1847 if (coding
->eol_type
== CODING_EOL_LF
1848 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
1849 *dst
++ = ISO_CODE_LF
;
1850 else if (coding
->eol_type
== CODING_EOL_CRLF
)
1851 *dst
++ = ISO_CODE_CR
, *dst
++ = ISO_CODE_LF
;
1853 *dst
++ = ISO_CODE_CR
;
1854 CODING_SPEC_ISO_BOL (coding
) = 1;
1855 coding
->consumed_char
++;
1858 case EMACS_leading_code_2
:
1862 /* invalid sequence */
1865 coding
->consumed_char
+= 2;
1868 ENCODE_ISO_CHARACTER (c1
, c2
, /* dummy */ c3
);
1871 case EMACS_leading_code_3
:
1872 TWO_MORE_BYTES (c2
, c3
);
1873 if (c2
< 0xA0 || c3
< 0xA0)
1875 /* invalid sequence */
1879 coding
->consumed_char
+= 3;
1881 else if (c1
< LEADING_CODE_PRIVATE_11
)
1882 ENCODE_ISO_CHARACTER (c1
, c2
, c3
);
1884 ENCODE_ISO_CHARACTER (c2
, c3
, /* dummy */ c4
);
1887 case EMACS_leading_code_4
:
1888 THREE_MORE_BYTES (c2
, c3
, c4
);
1889 if (c2
< 0xA0 || c3
< 0xA0 || c4
< 0xA0)
1891 /* invalid sequence */
1896 coding
->consumed_char
+= 4;
1899 ENCODE_ISO_CHARACTER (c2
, c3
, c4
);
1902 case EMACS_leading_code_composition
:
1906 /* invalid sequence */
1909 coding
->consumed_char
+= 2;
1911 else if (c2
== 0xFF)
1913 ENCODE_RESET_PLANE_AND_REGISTER
;
1914 coding
->composing
= COMPOSING_WITH_RULE_HEAD
;
1915 ENCODE_COMPOSITION_WITH_RULE_START
;
1916 coding
->consumed_char
++;
1920 ENCODE_RESET_PLANE_AND_REGISTER
;
1921 /* Rewind one byte because it is a character code of
1922 composition elements. */
1924 coding
->composing
= COMPOSING_NO_RULE_HEAD
;
1925 ENCODE_COMPOSITION_NO_RULE_START
;
1926 coding
->consumed_char
++;
1930 case EMACS_invalid_code
:
1932 coding
->consumed_char
++;
1937 result
= CODING_FINISH_INSUFFICIENT_SRC
;
1944 if (result
== CODING_FINISH_NORMAL
)
1945 result
= CODING_FINISH_INSUFFICIENT_DST
;
1947 /* If this is the last block of the text to be encoded, we
1948 must reset graphic planes and registers to the initial
1949 state, and flush out the carryover if any. */
1950 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
1951 ENCODE_RESET_PLANE_AND_REGISTER
;
1954 coding
->consumed
= src
- source
;
1955 coding
->produced
= coding
->produced_char
= dst
- destination
;
1960 /*** 4. SJIS and BIG5 handlers ***/
1962 /* Although SJIS and BIG5 are not ISO's coding system, they are used
1963 quite widely. So, for the moment, Emacs supports them in the bare
1964 C code. But, in the future, they may be supported only by CCL. */
1966 /* SJIS is a coding system encoding three character sets: ASCII, right
1967 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
1968 as is. A character of charset katakana-jisx0201 is encoded by
1969 "position-code + 0x80". A character of charset japanese-jisx0208
1970 is encoded in 2-byte but two position-codes are divided and shifted
1971 so that it fit in the range below.
1973 --- CODE RANGE of SJIS ---
1974 (character set) (range)
1976 KATAKANA-JISX0201 0xA0 .. 0xDF
1977 JISX0208 (1st byte) 0x80 .. 0x9F and 0xE0 .. 0xFF
1978 (2nd byte) 0x40 .. 0xFF
1979 -------------------------------
1983 /* BIG5 is a coding system encoding two character sets: ASCII and
1984 Big5. An ASCII character is encoded as is. Big5 is a two-byte
1985 character set and is encoded in two-byte.
1987 --- CODE RANGE of BIG5 ---
1988 (character set) (range)
1990 Big5 (1st byte) 0xA1 .. 0xFE
1991 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
1992 --------------------------
1994 Since the number of characters in Big5 is larger than maximum
1995 characters in Emacs' charset (96x96), it can't be handled as one
1996 charset. So, in Emacs, Big5 is divided into two: `charset-big5-1'
1997 and `charset-big5-2'. Both are DIMENSION2 and CHARS94. The former
1998 contains frequently used characters and the latter contains less
1999 frequently used characters. */
2001 /* Macros to decode or encode a character of Big5 in BIG5. B1 and B2
2002 are the 1st and 2nd position-codes of Big5 in BIG5 coding system.
2003 C1 and C2 are the 1st and 2nd position-codes of of Emacs' internal
2004 format. CHARSET is `charset_big5_1' or `charset_big5_2'. */
2006 /* Number of Big5 characters which have the same code in 1st byte. */
2007 #define BIG5_SAME_ROW (0xFF - 0xA1 + 0x7F - 0x40)
2009 #define DECODE_BIG5(b1, b2, charset, c1, c2) \
2012 = (b1 - 0xA1) * BIG5_SAME_ROW + b2 - (b2 < 0x7F ? 0x40 : 0x62); \
2014 charset = charset_big5_1; \
2017 charset = charset_big5_2; \
2018 temp -= (0xC9 - 0xA1) * BIG5_SAME_ROW; \
2020 c1 = temp / (0xFF - 0xA1) + 0x21; \
2021 c2 = temp % (0xFF - 0xA1) + 0x21; \
2024 #define ENCODE_BIG5(charset, c1, c2, b1, b2) \
2026 unsigned int temp = (c1 - 0x21) * (0xFF - 0xA1) + (c2 - 0x21); \
2027 if (charset == charset_big5_2) \
2028 temp += BIG5_SAME_ROW * (0xC9 - 0xA1); \
2029 b1 = temp / BIG5_SAME_ROW + 0xA1; \
2030 b2 = temp % BIG5_SAME_ROW; \
2031 b2 += b2 < 0x3F ? 0x40 : 0x62; \
2034 #define DECODE_SJIS_BIG5_CHARACTER(charset, c1, c2) \
2036 int c_alt, charset_alt = (charset); \
2037 if (!NILP (unification_table) \
2038 && ((c_alt = unify_char (unification_table, \
2039 -1, (charset), c1, c2)) >= 0)) \
2040 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
2041 if (charset_alt == CHARSET_ASCII || charset_alt < 0) \
2042 DECODE_CHARACTER_ASCII (c1); \
2043 else if (CHARSET_DIMENSION (charset_alt) == 1) \
2044 DECODE_CHARACTER_DIMENSION1 (charset_alt, c1); \
2046 DECODE_CHARACTER_DIMENSION2 (charset_alt, c1, c2); \
2049 #define ENCODE_SJIS_BIG5_CHARACTER(charset, c1, c2) \
2051 int c_alt, charset_alt; \
2052 if (!NILP (unification_table) \
2053 && ((c_alt = unify_char (unification_table, -1, charset, c1, c2)) \
2055 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
2057 charset_alt = charset; \
2058 if (charset_alt == charset_ascii) \
2060 else if (CHARSET_DIMENSION (charset_alt) == 1) \
2062 if (sjis_p && charset_alt == charset_katakana_jisx0201) \
2066 *dst++ = charset_alt, *dst++ = c1; \
2067 coding->fake_multibyte = 1; \
2072 c1 &= 0x7F, c2 &= 0x7F; \
2073 if (sjis_p && charset_alt == charset_jisx0208) \
2075 unsigned char s1, s2; \
2077 ENCODE_SJIS (c1, c2, s1, s2); \
2078 *dst++ = s1, *dst++ = s2; \
2079 coding->fake_multibyte = 1; \
2082 && (charset_alt == charset_big5_1 \
2083 || charset_alt == charset_big5_2)) \
2085 unsigned char b1, b2; \
2087 ENCODE_BIG5 (charset_alt, c1, c2, b1, b2); \
2088 *dst++ = b1, *dst++ = b2; \
2092 *dst++ = charset_alt, *dst++ = c1, *dst++ = c2; \
2093 coding->fake_multibyte = 1; \
2096 coding->consumed_char++; \
2099 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2100 Check if a text is encoded in SJIS. If it is, return
2101 CODING_CATEGORY_MASK_SJIS, else return 0. */
2104 detect_coding_sjis (src
, src_end
)
2105 unsigned char *src
, *src_end
;
2109 while (src
< src_end
)
2112 if ((c
>= 0x80 && c
< 0xA0) || c
>= 0xE0)
2114 if (src
< src_end
&& *src
++ < 0x40)
2118 return CODING_CATEGORY_MASK_SJIS
;
2121 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2122 Check if a text is encoded in BIG5. If it is, return
2123 CODING_CATEGORY_MASK_BIG5, else return 0. */
2126 detect_coding_big5 (src
, src_end
)
2127 unsigned char *src
, *src_end
;
2131 while (src
< src_end
)
2139 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
2143 return CODING_CATEGORY_MASK_BIG5
;
2146 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
2147 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
2150 decode_coding_sjis_big5 (coding
, source
, destination
,
2151 src_bytes
, dst_bytes
, sjis_p
)
2152 struct coding_system
*coding
;
2153 unsigned char *source
, *destination
;
2154 int src_bytes
, dst_bytes
;
2157 unsigned char *src
= source
;
2158 unsigned char *src_end
= source
+ src_bytes
;
2159 unsigned char *dst
= destination
;
2160 unsigned char *dst_end
= destination
+ dst_bytes
;
2161 /* Since the maximum bytes produced by each loop is 4, we subtract 3
2162 from DST_END to assure overflow checking is necessary only at the
2164 unsigned char *adjusted_dst_end
= dst_end
- 3;
2165 Lisp_Object unification_table
2166 = coding
->character_unification_table_for_decode
;
2167 int result
= CODING_FINISH_NORMAL
;
2169 if (!NILP (Venable_character_unification
) && NILP (unification_table
))
2170 unification_table
= Vstandard_character_unification_table_for_decode
;
2172 coding
->produced_char
= 0;
2173 coding
->fake_multibyte
= 0;
2174 while (src
< src_end
&& (dst_bytes
2175 ? (dst
< adjusted_dst_end
)
2178 /* SRC_BASE remembers the start position in source in each loop.
2179 The loop will be exited when there's not enough source text
2180 to analyze two-byte character (within macro ONE_MORE_BYTE).
2181 In that case, SRC is reset to SRC_BASE before exiting. */
2182 unsigned char *src_base
= src
;
2183 unsigned char c1
= *src
++, c2
, c3
, c4
;
2189 if (coding
->eol_type
== CODING_EOL_CRLF
)
2194 else if (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
)
2196 result
= CODING_FINISH_INCONSISTENT_EOL
;
2197 goto label_end_of_loop_2
;
2200 /* To process C2 again, SRC is subtracted by 1. */
2203 else if (coding
->eol_type
== CODING_EOL_CR
)
2209 && (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
)
2210 && (coding
->eol_type
== CODING_EOL_CR
2211 || coding
->eol_type
== CODING_EOL_CRLF
))
2213 result
= CODING_FINISH_INCONSISTENT_EOL
;
2214 goto label_end_of_loop_2
;
2218 coding
->produced_char
++;
2221 DECODE_SJIS_BIG5_CHARACTER (charset_ascii
, c1
, /* dummy */ c2
);
2224 /* SJIS -> JISX0208 */
2230 DECODE_SJIS (c1
, c2
, c3
, c4
);
2231 DECODE_SJIS_BIG5_CHARACTER (charset_jisx0208
, c3
, c4
);
2234 goto label_invalid_code_2
;
2237 goto label_invalid_code_1
;
2241 /* SJIS -> JISX0201-Kana, BIG5 -> Big5 */
2243 DECODE_SJIS_BIG5_CHARACTER (charset_katakana_jisx0201
, c1
,
2250 if ((c2
>= 0x40 && c2
<= 0x7E) || (c2
>= 0xA1 && c2
<= 0xFE))
2252 DECODE_BIG5 (c1
, c2
, charset
, c3
, c4
);
2253 DECODE_SJIS_BIG5_CHARACTER (charset
, c3
, c4
);
2256 goto label_invalid_code_2
;
2259 else /* C1 >= 0xE0 */
2261 /* SJIS -> JISX0208, BIG5 -> Big5 */
2267 DECODE_SJIS (c1
, c2
, c3
, c4
);
2268 DECODE_SJIS_BIG5_CHARACTER (charset_jisx0208
, c3
, c4
);
2271 goto label_invalid_code_2
;
2278 if ((c2
>= 0x40 && c2
<= 0x7E) || (c2
>= 0xA1 && c2
<= 0xFE))
2280 DECODE_BIG5 (c1
, c2
, charset
, c3
, c4
);
2281 DECODE_SJIS_BIG5_CHARACTER (charset
, c3
, c4
);
2284 goto label_invalid_code_2
;
2289 label_invalid_code_1
:
2291 coding
->produced_char
++;
2292 coding
->fake_multibyte
= 1;
2295 label_invalid_code_2
:
2296 *dst
++ = c1
; *dst
++= c2
;
2297 coding
->produced_char
+= 2;
2298 coding
->fake_multibyte
= 1;
2302 result
= CODING_FINISH_INSUFFICIENT_SRC
;
2303 label_end_of_loop_2
:
2310 if (result
== CODING_FINISH_NORMAL
)
2311 result
= CODING_FINISH_INSUFFICIENT_DST
;
2312 else if (result
!= CODING_FINISH_INCONSISTENT_EOL
2313 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
2315 src_bytes
= src_end
- src
;
2316 if (dst_bytes
&& (dst_end
- dst
< src_bytes
))
2317 src_bytes
= dst_end
- dst
;
2318 bcopy (dst
, src
, src_bytes
);
2321 coding
->fake_multibyte
= 1;
2325 coding
->consumed
= coding
->consumed_char
= src
- source
;
2326 coding
->produced
= dst
- destination
;
2330 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
2331 This function can encode `charset_ascii', `charset_katakana_jisx0201',
2332 `charset_jisx0208', `charset_big5_1', and `charset_big5-2'. We are
2333 sure that all these charsets are registered as official charset
2334 (i.e. do not have extended leading-codes). Characters of other
2335 charsets are produced without any encoding. If SJIS_P is 1, encode
2336 SJIS text, else encode BIG5 text. */
2339 encode_coding_sjis_big5 (coding
, source
, destination
,
2340 src_bytes
, dst_bytes
, sjis_p
)
2341 struct coding_system
*coding
;
2342 unsigned char *source
, *destination
;
2343 int src_bytes
, dst_bytes
;
2346 unsigned char *src
= source
;
2347 unsigned char *src_end
= source
+ src_bytes
;
2348 unsigned char *dst
= destination
;
2349 unsigned char *dst_end
= destination
+ dst_bytes
;
2350 /* Since the maximum bytes produced by each loop is 2, we subtract 1
2351 from DST_END to assure overflow checking is necessary only at the
2353 unsigned char *adjusted_dst_end
= dst_end
- 1;
2354 Lisp_Object unification_table
2355 = coding
->character_unification_table_for_encode
;
2356 int result
= CODING_FINISH_NORMAL
;
2358 if (!NILP (Venable_character_unification
) && NILP (unification_table
))
2359 unification_table
= Vstandard_character_unification_table_for_encode
;
2361 coding
->consumed_char
= 0;
2362 coding
->fake_multibyte
= 0;
2363 while (src
< src_end
&& (dst_bytes
2364 ? (dst
< adjusted_dst_end
)
2367 /* SRC_BASE remembers the start position in source in each loop.
2368 The loop will be exited when there's not enough source text
2369 to analyze multi-byte codes (within macros ONE_MORE_BYTE and
2370 TWO_MORE_BYTES). In that case, SRC is reset to SRC_BASE
2372 unsigned char *src_base
= src
;
2373 unsigned char c1
= *src
++, c2
, c3
, c4
;
2375 if (coding
->composing
)
2382 else if (c1
>= 0xA0)
2385 coding
->composing
= 0;
2388 switch (emacs_code_class
[c1
])
2390 case EMACS_ascii_code
:
2391 ENCODE_SJIS_BIG5_CHARACTER (charset_ascii
, c1
, /* dummy */ c2
);
2394 case EMACS_control_code
:
2396 coding
->consumed_char
++;
2399 case EMACS_carriage_return_code
:
2400 if (! (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
2403 coding
->consumed_char
++;
2406 /* fall down to treat '\r' as '\n' ... */
2408 case EMACS_linefeed_code
:
2409 if (coding
->eol_type
== CODING_EOL_LF
2410 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
2412 else if (coding
->eol_type
== CODING_EOL_CRLF
)
2413 *dst
++ = '\r', *dst
++ = '\n';
2416 coding
->consumed_char
++;
2419 case EMACS_leading_code_2
:
2421 ENCODE_SJIS_BIG5_CHARACTER (c1
, c2
, /* dummy */ c3
);
2424 case EMACS_leading_code_3
:
2425 TWO_MORE_BYTES (c2
, c3
);
2426 ENCODE_SJIS_BIG5_CHARACTER (c1
, c2
, c3
);
2429 case EMACS_leading_code_4
:
2430 THREE_MORE_BYTES (c2
, c3
, c4
);
2431 ENCODE_SJIS_BIG5_CHARACTER (c2
, c3
, c4
);
2434 case EMACS_leading_code_composition
:
2435 coding
->composing
= 1;
2438 default: /* i.e. case EMACS_invalid_code: */
2440 coding
->consumed_char
++;
2445 result
= CODING_FINISH_INSUFFICIENT_SRC
;
2450 if (result
== CODING_FINISH_NORMAL
2452 result
= CODING_FINISH_INSUFFICIENT_DST
;
2453 coding
->consumed
= src
- source
;
2454 coding
->produced
= coding
->produced_char
= dst
- destination
;
2459 /*** 5. End-of-line handlers ***/
2461 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
2462 This function is called only when `coding->eol_type' is
2463 CODING_EOL_CRLF or CODING_EOL_CR. */
2465 decode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
)
2466 struct coding_system
*coding
;
2467 unsigned char *source
, *destination
;
2468 int src_bytes
, dst_bytes
;
2470 unsigned char *src
= source
;
2471 unsigned char *src_end
= source
+ src_bytes
;
2472 unsigned char *dst
= destination
;
2473 unsigned char *dst_end
= destination
+ dst_bytes
;
2475 int result
= CODING_FINISH_NORMAL
;
2477 coding
->fake_multibyte
= 0;
2482 switch (coding
->eol_type
)
2484 case CODING_EOL_CRLF
:
2486 /* Since the maximum bytes produced by each loop is 2, we
2487 subtract 1 from DST_END to assure overflow checking is
2488 necessary only at the head of loop. */
2489 unsigned char *adjusted_dst_end
= dst_end
- 1;
2491 while (src
< src_end
&& (dst_bytes
2492 ? (dst
< adjusted_dst_end
)
2495 unsigned char *src_base
= src
;
2503 if (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
)
2505 result
= CODING_FINISH_INCONSISTENT_EOL
;
2506 goto label_end_of_loop_2
;
2509 if (BASE_LEADING_CODE_P (c
))
2510 coding
->fake_multibyte
= 1;
2515 && (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
))
2517 result
= CODING_FINISH_INCONSISTENT_EOL
;
2518 goto label_end_of_loop_2
;
2523 if (BASE_LEADING_CODE_P (c
))
2524 coding
->fake_multibyte
= 1;
2529 result
= CODING_FINISH_INSUFFICIENT_SRC
;
2530 label_end_of_loop_2
:
2534 if (result
== CODING_FINISH_NORMAL
2536 result
= CODING_FINISH_INSUFFICIENT_DST
;
2541 if (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
)
2543 while (src
< src_end
)
2545 if ((c
= *src
++) == '\n')
2547 if (BASE_LEADING_CODE_P (c
))
2548 coding
->fake_multibyte
= 1;
2552 src_bytes
= src
- source
;
2553 result
= CODING_FINISH_INCONSISTENT_EOL
;
2556 if (dst_bytes
&& src_bytes
> dst_bytes
)
2558 result
= CODING_FINISH_INSUFFICIENT_DST
;
2559 src_bytes
= dst_bytes
;
2562 bcopy (source
, destination
, src_bytes
);
2564 safe_bcopy (source
, destination
, src_bytes
);
2565 src
= source
+ src_bytes
;
2566 while (src_bytes
--) if (*dst
++ == '\r') dst
[-1] = '\n';
2569 default: /* i.e. case: CODING_EOL_LF */
2570 if (dst_bytes
&& src_bytes
> dst_bytes
)
2572 result
= CODING_FINISH_INSUFFICIENT_DST
;
2573 src_bytes
= dst_bytes
;
2576 bcopy (source
, destination
, src_bytes
);
2578 safe_bcopy (source
, destination
, src_bytes
);
2581 coding
->fake_multibyte
= 1;
2585 coding
->consumed
= coding
->consumed_char
= src
- source
;
2586 coding
->produced
= coding
->produced_char
= dst
- destination
;
2590 /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". Encode
2591 format of end-of-line according to `coding->eol_type'. If
2592 `coding->mode & CODING_MODE_SELECTIVE_DISPLAY' is nonzero, code
2593 '\r' in source text also means end-of-line. */
2595 encode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
)
2596 struct coding_system
*coding
;
2597 unsigned char *source
, *destination
;
2598 int src_bytes
, dst_bytes
;
2600 unsigned char *src
= source
;
2601 unsigned char *dst
= destination
;
2602 int result
= CODING_FINISH_NORMAL
;
2604 coding
->fake_multibyte
= 0;
2606 if (coding
->eol_type
== CODING_EOL_CRLF
)
2609 unsigned char *src_end
= source
+ src_bytes
;
2610 unsigned char *dst_end
= destination
+ dst_bytes
;
2611 /* Since the maximum bytes produced by each loop is 2, we
2612 subtract 1 from DST_END to assure overflow checking is
2613 necessary only at the head of loop. */
2614 unsigned char *adjusted_dst_end
= dst_end
- 1;
2616 while (src
< src_end
&& (dst_bytes
2617 ? (dst
< adjusted_dst_end
)
2622 || (c
== '\r' && (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
)))
2623 *dst
++ = '\r', *dst
++ = '\n';
2627 if (BASE_LEADING_CODE_P (c
))
2628 coding
->fake_multibyte
= 1;
2632 result
= CODING_FINISH_INSUFFICIENT_DST
;
2638 if (dst_bytes
&& src_bytes
> dst_bytes
)
2640 src_bytes
= dst_bytes
;
2641 result
= CODING_FINISH_INSUFFICIENT_DST
;
2644 bcopy (source
, destination
, src_bytes
);
2647 safe_bcopy (source
, destination
, src_bytes
);
2648 dst_bytes
= src_bytes
;
2650 if (coding
->eol_type
== CODING_EOL_CRLF
)
2654 if ((c
= *dst
++) == '\n')
2656 else if (BASE_LEADING_CODE_P (c
))
2657 coding
->fake_multibyte
= 1;
2662 if (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
)
2665 if (*dst
++ == '\r') dst
[-1] = '\n';
2667 coding
->fake_multibyte
= 1;
2669 src
= source
+ dst_bytes
;
2670 dst
= destination
+ dst_bytes
;
2673 coding
->consumed
= coding
->consumed_char
= src
- source
;
2674 coding
->produced
= coding
->produced_char
= dst
- destination
;
2679 /*** 6. C library functions ***/
2681 /* In Emacs Lisp, coding system is represented by a Lisp symbol which
2682 has a property `coding-system'. The value of this property is a
2683 vector of length 5 (called as coding-vector). Among elements of
2684 this vector, the first (element[0]) and the fifth (element[4])
2685 carry important information for decoding/encoding. Before
2686 decoding/encoding, this information should be set in fields of a
2687 structure of type `coding_system'.
2689 A value of property `coding-system' can be a symbol of another
2690 subsidiary coding-system. In that case, Emacs gets coding-vector
2693 `element[0]' contains information to be set in `coding->type'. The
2694 value and its meaning is as follows:
2696 0 -- coding_type_emacs_mule
2697 1 -- coding_type_sjis
2698 2 -- coding_type_iso2022
2699 3 -- coding_type_big5
2700 4 -- coding_type_ccl encoder/decoder written in CCL
2701 nil -- coding_type_no_conversion
2702 t -- coding_type_undecided (automatic conversion on decoding,
2703 no-conversion on encoding)
2705 `element[4]' contains information to be set in `coding->flags' and
2706 `coding->spec'. The meaning varies by `coding->type'.
2708 If `coding->type' is `coding_type_iso2022', element[4] is a vector
2709 of length 32 (of which the first 13 sub-elements are used now).
2710 Meanings of these sub-elements are:
2712 sub-element[N] where N is 0 through 3: to be set in `coding->spec.iso2022'
2713 If the value is an integer of valid charset, the charset is
2714 assumed to be designated to graphic register N initially.
2716 If the value is minus, it is a minus value of charset which
2717 reserves graphic register N, which means that the charset is
2718 not designated initially but should be designated to graphic
2719 register N just before encoding a character in that charset.
2721 If the value is nil, graphic register N is never used on
2724 sub-element[N] where N is 4 through 11: to be set in `coding->flags'
2725 Each value takes t or nil. See the section ISO2022 of
2726 `coding.h' for more information.
2728 If `coding->type' is `coding_type_big5', element[4] is t to denote
2729 BIG5-ETen or nil to denote BIG5-HKU.
2731 If `coding->type' takes the other value, element[4] is ignored.
2733 Emacs Lisp's coding system also carries information about format of
2734 end-of-line in a value of property `eol-type'. If the value is
2735 integer, 0 means CODING_EOL_LF, 1 means CODING_EOL_CRLF, and 2
2736 means CODING_EOL_CR. If it is not integer, it should be a vector
2737 of subsidiary coding systems of which property `eol-type' has one
2742 /* Extract information for decoding/encoding from CODING_SYSTEM_SYMBOL
2743 and set it in CODING. If CODING_SYSTEM_SYMBOL is invalid, CODING
2744 is setup so that no conversion is necessary and return -1, else
2748 setup_coding_system (coding_system
, coding
)
2749 Lisp_Object coding_system
;
2750 struct coding_system
*coding
;
2752 Lisp_Object coding_spec
, coding_type
, eol_type
, plist
;
2756 /* Initialize some fields required for all kinds of coding systems. */
2757 coding
->symbol
= coding_system
;
2758 coding
->common_flags
= 0;
2760 coding
->heading_ascii
= -1;
2761 coding
->post_read_conversion
= coding
->pre_write_conversion
= Qnil
;
2762 coding_spec
= Fget (coding_system
, Qcoding_system
);
2763 if (!VECTORP (coding_spec
)
2764 || XVECTOR (coding_spec
)->size
!= 5
2765 || !CONSP (XVECTOR (coding_spec
)->contents
[3]))
2766 goto label_invalid_coding_system
;
2768 eol_type
= inhibit_eol_conversion
? Qnil
: Fget (coding_system
, Qeol_type
);
2769 if (VECTORP (eol_type
))
2771 coding
->eol_type
= CODING_EOL_UNDECIDED
;
2772 coding
->common_flags
= CODING_REQUIRE_DETECTION_MASK
;
2774 else if (XFASTINT (eol_type
) == 1)
2776 coding
->eol_type
= CODING_EOL_CRLF
;
2777 coding
->common_flags
2778 = CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
;
2780 else if (XFASTINT (eol_type
) == 2)
2782 coding
->eol_type
= CODING_EOL_CR
;
2783 coding
->common_flags
2784 = CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
;
2787 coding
->eol_type
= CODING_EOL_LF
;
2789 coding_type
= XVECTOR (coding_spec
)->contents
[0];
2790 /* Try short cut. */
2791 if (SYMBOLP (coding_type
))
2793 if (EQ (coding_type
, Qt
))
2795 coding
->type
= coding_type_undecided
;
2796 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
2799 coding
->type
= coding_type_no_conversion
;
2803 /* Initialize remaining fields. */
2804 coding
->composing
= 0;
2805 coding
->character_unification_table_for_decode
= Qnil
;
2806 coding
->character_unification_table_for_encode
= Qnil
;
2808 /* Get values of coding system properties:
2809 `post-read-conversion', `pre-write-conversion',
2810 `character-unification-table-for-decode',
2811 `character-unification-table-for-encode'. */
2812 plist
= XVECTOR (coding_spec
)->contents
[3];
2813 coding
->post_read_conversion
= Fplist_get (plist
, Qpost_read_conversion
);
2814 coding
->pre_write_conversion
= Fplist_get (plist
, Qpre_write_conversion
);
2815 val
= Fplist_get (plist
, Qcharacter_unification_table_for_decode
);
2817 val
= Fget (val
, Qcharacter_unification_table_for_decode
);
2818 coding
->character_unification_table_for_decode
2819 = CHAR_TABLE_P (val
) ? val
: Qnil
;
2820 val
= Fplist_get (plist
, Qcharacter_unification_table_for_encode
);
2822 val
= Fget (val
, Qcharacter_unification_table_for_encode
);
2823 coding
->character_unification_table_for_encode
2824 = CHAR_TABLE_P (val
) ? val
: Qnil
;
2825 val
= Fplist_get (plist
, Qcoding_category
);
2828 val
= Fget (val
, Qcoding_category_index
);
2830 coding
->category_idx
= XINT (val
);
2832 goto label_invalid_coding_system
;
2835 goto label_invalid_coding_system
;
2837 val
= Fplist_get (plist
, Qsafe_charsets
);
2840 for (i
= 0; i
<= MAX_CHARSET
; i
++)
2841 coding
->safe_charsets
[i
] = 1;
2845 bzero (coding
->safe_charsets
, MAX_CHARSET
+ 1);
2848 if ((i
= get_charset_id (XCONS (val
)->car
)) >= 0)
2849 coding
->safe_charsets
[i
] = 1;
2850 val
= XCONS (val
)->cdr
;
2854 switch (XFASTINT (coding_type
))
2857 coding
->type
= coding_type_emacs_mule
;
2858 if (!NILP (coding
->post_read_conversion
))
2859 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
2860 if (!NILP (coding
->pre_write_conversion
))
2861 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
2865 coding
->type
= coding_type_sjis
;
2866 coding
->common_flags
2867 |= CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
;
2871 coding
->type
= coding_type_iso2022
;
2872 coding
->common_flags
2873 |= CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
;
2875 Lisp_Object val
, temp
;
2877 int i
, charset
, reg_bits
= 0;
2879 val
= XVECTOR (coding_spec
)->contents
[4];
2881 if (!VECTORP (val
) || XVECTOR (val
)->size
!= 32)
2882 goto label_invalid_coding_system
;
2884 flags
= XVECTOR (val
)->contents
;
2886 = ((NILP (flags
[4]) ? 0 : CODING_FLAG_ISO_SHORT_FORM
)
2887 | (NILP (flags
[5]) ? 0 : CODING_FLAG_ISO_RESET_AT_EOL
)
2888 | (NILP (flags
[6]) ? 0 : CODING_FLAG_ISO_RESET_AT_CNTL
)
2889 | (NILP (flags
[7]) ? 0 : CODING_FLAG_ISO_SEVEN_BITS
)
2890 | (NILP (flags
[8]) ? 0 : CODING_FLAG_ISO_LOCKING_SHIFT
)
2891 | (NILP (flags
[9]) ? 0 : CODING_FLAG_ISO_SINGLE_SHIFT
)
2892 | (NILP (flags
[10]) ? 0 : CODING_FLAG_ISO_USE_ROMAN
)
2893 | (NILP (flags
[11]) ? 0 : CODING_FLAG_ISO_USE_OLDJIS
)
2894 | (NILP (flags
[12]) ? 0 : CODING_FLAG_ISO_NO_DIRECTION
)
2895 | (NILP (flags
[13]) ? 0 : CODING_FLAG_ISO_INIT_AT_BOL
)
2896 | (NILP (flags
[14]) ? 0 : CODING_FLAG_ISO_DESIGNATE_AT_BOL
)
2897 | (NILP (flags
[15]) ? 0 : CODING_FLAG_ISO_SAFE
)
2898 | (NILP (flags
[16]) ? 0 : CODING_FLAG_ISO_LATIN_EXTRA
)
2901 /* Invoke graphic register 0 to plane 0. */
2902 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 0;
2903 /* Invoke graphic register 1 to plane 1 if we can use full 8-bit. */
2904 CODING_SPEC_ISO_INVOCATION (coding
, 1)
2905 = (coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
? -1 : 1);
2906 /* Not single shifting at first. */
2907 CODING_SPEC_ISO_SINGLE_SHIFTING (coding
) = 0;
2908 /* Beginning of buffer should also be regarded as bol. */
2909 CODING_SPEC_ISO_BOL (coding
) = 1;
2911 for (charset
= 0; charset
<= MAX_CHARSET
; charset
++)
2912 CODING_SPEC_ISO_REVISION_NUMBER (coding
, charset
) = 255;
2913 val
= Vcharset_revision_alist
;
2916 charset
= get_charset_id (Fcar_safe (XCONS (val
)->car
));
2918 && (temp
= Fcdr_safe (XCONS (val
)->car
), INTEGERP (temp
))
2919 && (i
= XINT (temp
), (i
>= 0 && (i
+ '@') < 128)))
2920 CODING_SPEC_ISO_REVISION_NUMBER (coding
, charset
) = i
;
2921 val
= XCONS (val
)->cdr
;
2924 /* Checks FLAGS[REG] (REG = 0, 1, 2 3) and decide designations.
2925 FLAGS[REG] can be one of below:
2926 integer CHARSET: CHARSET occupies register I,
2927 t: designate nothing to REG initially, but can be used
2929 list of integer, nil, or t: designate the first
2930 element (if integer) to REG initially, the remaining
2931 elements (if integer) is designated to REG on request,
2932 if an element is t, REG can be used by any charsets,
2933 nil: REG is never used. */
2934 for (charset
= 0; charset
<= MAX_CHARSET
; charset
++)
2935 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2936 = CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
;
2937 for (i
= 0; i
< 4; i
++)
2939 if (INTEGERP (flags
[i
])
2940 && (charset
= XINT (flags
[i
]), CHARSET_VALID_P (charset
))
2941 || (charset
= get_charset_id (flags
[i
])) >= 0)
2943 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = charset
;
2944 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
) = i
;
2946 else if (EQ (flags
[i
], Qt
))
2948 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
2950 coding
->flags
|= CODING_FLAG_ISO_DESIGNATION
;
2952 else if (CONSP (flags
[i
]))
2954 Lisp_Object tail
= flags
[i
];
2956 coding
->flags
|= CODING_FLAG_ISO_DESIGNATION
;
2957 if (INTEGERP (XCONS (tail
)->car
)
2958 && (charset
= XINT (XCONS (tail
)->car
),
2959 CHARSET_VALID_P (charset
))
2960 || (charset
= get_charset_id (XCONS (tail
)->car
)) >= 0)
2962 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = charset
;
2963 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
) =i
;
2966 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
2967 tail
= XCONS (tail
)->cdr
;
2968 while (CONSP (tail
))
2970 if (INTEGERP (XCONS (tail
)->car
)
2971 && (charset
= XINT (XCONS (tail
)->car
),
2972 CHARSET_VALID_P (charset
))
2973 || (charset
= get_charset_id (XCONS (tail
)->car
)) >= 0)
2974 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2976 else if (EQ (XCONS (tail
)->car
, Qt
))
2978 tail
= XCONS (tail
)->cdr
;
2982 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
2984 CODING_SPEC_ISO_DESIGNATION (coding
, i
)
2985 = CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
);
2988 if (reg_bits
&& ! (coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
))
2990 /* REG 1 can be used only by locking shift in 7-bit env. */
2991 if (coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
)
2993 if (! (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
))
2994 /* Without any shifting, only REG 0 and 1 can be used. */
2999 for (charset
= 0; charset
<= MAX_CHARSET
; charset
++)
3001 if (CHARSET_VALID_P (charset
))
3003 /* There exist some default graphic registers to be
3006 /* We had better avoid designating a charset of
3007 CHARS96 to REG 0 as far as possible. */
3008 if (CHARSET_CHARS (charset
) == 96)
3009 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
3011 ? 1 : (reg_bits
& 4 ? 2 : (reg_bits
& 8 ? 3 : 0)));
3013 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
3015 ? 0 : (reg_bits
& 2 ? 1 : (reg_bits
& 4 ? 2 : 3)));
3019 coding
->common_flags
|= CODING_REQUIRE_FLUSHING_MASK
;
3020 coding
->spec
.iso2022
.last_invalid_designation_register
= -1;
3024 coding
->type
= coding_type_big5
;
3025 coding
->common_flags
3026 |= CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
;
3028 = (NILP (XVECTOR (coding_spec
)->contents
[4])
3029 ? CODING_FLAG_BIG5_HKU
3030 : CODING_FLAG_BIG5_ETEN
);
3034 coding
->type
= coding_type_ccl
;
3035 coding
->common_flags
3036 |= CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
;
3038 Lisp_Object val
= XVECTOR (coding_spec
)->contents
[4];
3040 && VECTORP (XCONS (val
)->car
)
3041 && VECTORP (XCONS (val
)->cdr
))
3043 setup_ccl_program (&(coding
->spec
.ccl
.decoder
), XCONS (val
)->car
);
3044 setup_ccl_program (&(coding
->spec
.ccl
.encoder
), XCONS (val
)->cdr
);
3047 goto label_invalid_coding_system
;
3049 coding
->common_flags
|= CODING_REQUIRE_FLUSHING_MASK
;
3053 coding
->type
= coding_type_raw_text
;
3057 goto label_invalid_coding_system
;
3061 label_invalid_coding_system
:
3062 coding
->type
= coding_type_no_conversion
;
3063 coding
->category_idx
= CODING_CATEGORY_IDX_BINARY
;
3064 coding
->common_flags
= 0;
3065 coding
->eol_type
= CODING_EOL_LF
;
3066 coding
->pre_write_conversion
= coding
->post_read_conversion
= Qnil
;
3070 /* Emacs has a mechanism to automatically detect a coding system if it
3071 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
3072 it's impossible to distinguish some coding systems accurately
3073 because they use the same range of codes. So, at first, coding
3074 systems are categorized into 7, those are:
3076 o coding-category-emacs-mule
3078 The category for a coding system which has the same code range
3079 as Emacs' internal format. Assigned the coding-system (Lisp
3080 symbol) `emacs-mule' by default.
3082 o coding-category-sjis
3084 The category for a coding system which has the same code range
3085 as SJIS. Assigned the coding-system (Lisp
3086 symbol) `japanese-shift-jis' by default.
3088 o coding-category-iso-7
3090 The category for a coding system which has the same code range
3091 as ISO2022 of 7-bit environment. This doesn't use any locking
3092 shift and single shift functions. This can encode/decode all
3093 charsets. Assigned the coding-system (Lisp symbol)
3094 `iso-2022-7bit' by default.
3096 o coding-category-iso-7-tight
3098 Same as coding-category-iso-7 except that this can
3099 encode/decode only the specified charsets.
3101 o coding-category-iso-8-1
3103 The category for a coding system which has the same code range
3104 as ISO2022 of 8-bit environment and graphic plane 1 used only
3105 for DIMENSION1 charset. This doesn't use any locking shift
3106 and single shift functions. Assigned the coding-system (Lisp
3107 symbol) `iso-latin-1' by default.
3109 o coding-category-iso-8-2
3111 The category for a coding system which has the same code range
3112 as ISO2022 of 8-bit environment and graphic plane 1 used only
3113 for DIMENSION2 charset. This doesn't use any locking shift
3114 and single shift functions. Assigned the coding-system (Lisp
3115 symbol) `japanese-iso-8bit' by default.
3117 o coding-category-iso-7-else
3119 The category for a coding system which has the same code range
3120 as ISO2022 of 7-bit environemnt but uses locking shift or
3121 single shift functions. Assigned the coding-system (Lisp
3122 symbol) `iso-2022-7bit-lock' by default.
3124 o coding-category-iso-8-else
3126 The category for a coding system which has the same code range
3127 as ISO2022 of 8-bit environemnt but uses locking shift or
3128 single shift functions. Assigned the coding-system (Lisp
3129 symbol) `iso-2022-8bit-ss2' by default.
3131 o coding-category-big5
3133 The category for a coding system which has the same code range
3134 as BIG5. Assigned the coding-system (Lisp symbol)
3135 `cn-big5' by default.
3137 o coding-category-binary
3139 The category for a coding system not categorized in any of the
3140 above. Assigned the coding-system (Lisp symbol)
3141 `no-conversion' by default.
3143 Each of them is a Lisp symbol and the value is an actual
3144 `coding-system's (this is also a Lisp symbol) assigned by a user.
3145 What Emacs does actually is to detect a category of coding system.
3146 Then, it uses a `coding-system' assigned to it. If Emacs can't
3147 decide only one possible category, it selects a category of the
3148 highest priority. Priorities of categories are also specified by a
3149 user in a Lisp variable `coding-category-list'.
3153 /* Detect how a text of length SRC_BYTES pointed by SOURCE is encoded.
3154 If it detects possible coding systems, return an integer in which
3155 appropriate flag bits are set. Flag bits are defined by macros
3156 CODING_CATEGORY_MASK_XXX in `coding.h'.
3158 How many ASCII characters are at the head is returned as *SKIP. */
3161 detect_coding_mask (source
, src_bytes
, priorities
, skip
)
3162 unsigned char *source
;
3163 int src_bytes
, *priorities
, *skip
;
3165 register unsigned char c
;
3166 unsigned char *src
= source
, *src_end
= source
+ src_bytes
;
3167 unsigned int mask
= (CODING_CATEGORY_MASK_ISO_7BIT
3168 | CODING_CATEGORY_MASK_ISO_SHIFT
);
3171 /* At first, skip all ASCII characters and control characters except
3172 for three ISO2022 specific control characters. */
3173 label_loop_detect_coding
:
3174 while (src
< src_end
)
3178 || ((mask
& CODING_CATEGORY_MASK_ISO_7BIT
)
3179 && c
== ISO_CODE_ESC
)
3180 || ((mask
& CODING_CATEGORY_MASK_ISO_SHIFT
)
3181 && (c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)))
3185 *skip
= src
- source
;
3188 /* We found nothing other than ASCII. There's nothing to do. */
3191 /* The text seems to be encoded in some multilingual coding system.
3192 Now, try to find in which coding system the text is encoded. */
3195 /* i.e. (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) */
3196 /* C is an ISO2022 specific control code of C0. */
3197 mask
= detect_coding_iso2022 (src
, src_end
);
3200 /* No valid ISO2022 code follows C. Try again. */
3202 mask
= (c
!= ISO_CODE_ESC
3203 ? CODING_CATEGORY_MASK_ISO_7BIT
3204 : CODING_CATEGORY_MASK_ISO_SHIFT
);
3205 goto label_loop_detect_coding
;
3208 goto label_return_highest_only
;
3216 /* C is the first byte of SJIS character code,
3217 or a leading-code of Emacs' internal format (emacs-mule). */
3218 try = CODING_CATEGORY_MASK_SJIS
| CODING_CATEGORY_MASK_EMACS_MULE
;
3220 /* Or, if C is a special latin extra code,
3221 or is an ISO2022 specific control code of C1 (SS2 or SS3),
3222 or is an ISO2022 control-sequence-introducer (CSI),
3223 we should also consider the possibility of ISO2022 codings. */
3224 if ((VECTORP (Vlatin_extra_code_table
)
3225 && !NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
3226 || (c
== ISO_CODE_SS2
|| c
== ISO_CODE_SS3
)
3227 || (c
== ISO_CODE_CSI
3230 || ((*src
== '0' || *src
== '1' || *src
== '2')
3231 && src
+ 1 < src_end
3232 && src
[1] == ']')))))
3233 try |= (CODING_CATEGORY_MASK_ISO_8_ELSE
3234 | CODING_CATEGORY_MASK_ISO_8BIT
);
3237 /* C is a character of ISO2022 in graphic plane right,
3238 or a SJIS's 1-byte character code (i.e. JISX0201),
3239 or the first byte of BIG5's 2-byte code. */
3240 try = (CODING_CATEGORY_MASK_ISO_8_ELSE
3241 | CODING_CATEGORY_MASK_ISO_8BIT
3242 | CODING_CATEGORY_MASK_SJIS
3243 | CODING_CATEGORY_MASK_BIG5
);
3248 for (i
= 0; i
< CODING_CATEGORY_IDX_MAX
; i
++)
3250 priorities
[i
] &= try;
3251 if (priorities
[i
] & CODING_CATEGORY_MASK_ISO
)
3252 mask
= detect_coding_iso2022 (src
, src_end
);
3253 else if (priorities
[i
] & CODING_CATEGORY_MASK_SJIS
)
3254 mask
= detect_coding_sjis (src
, src_end
);
3255 else if (priorities
[i
] & CODING_CATEGORY_MASK_BIG5
)
3256 mask
= detect_coding_big5 (src
, src_end
);
3257 else if (priorities
[i
] & CODING_CATEGORY_MASK_EMACS_MULE
)
3258 mask
= detect_coding_emacs_mule (src
, src_end
);
3260 goto label_return_highest_only
;
3262 return CODING_CATEGORY_MASK_RAW_TEXT
;
3264 if (try & CODING_CATEGORY_MASK_ISO
)
3265 mask
|= detect_coding_iso2022 (src
, src_end
);
3266 if (try & CODING_CATEGORY_MASK_SJIS
)
3267 mask
|= detect_coding_sjis (src
, src_end
);
3268 if (try & CODING_CATEGORY_MASK_BIG5
)
3269 mask
|= detect_coding_big5 (src
, src_end
);
3270 if (try & CODING_CATEGORY_MASK_EMACS_MULE
)
3271 mask
|= detect_coding_emacs_mule (src
, src_end
);
3273 return (mask
| CODING_CATEGORY_MASK_RAW_TEXT
);
3275 label_return_highest_only
:
3276 for (i
= 0; i
< CODING_CATEGORY_IDX_MAX
; i
++)
3278 if (mask
& priorities
[i
])
3279 return priorities
[i
];
3281 return CODING_CATEGORY_MASK_RAW_TEXT
;
3284 /* Detect how a text of length SRC_BYTES pointed by SRC is encoded.
3285 The information of the detected coding system is set in CODING. */
3288 detect_coding (coding
, src
, src_bytes
)
3289 struct coding_system
*coding
;
3295 int priorities
[CODING_CATEGORY_IDX_MAX
];
3296 Lisp_Object val
= Vcoding_category_list
;
3299 while (CONSP (val
) && i
< CODING_CATEGORY_IDX_MAX
)
3301 if (! SYMBOLP (XCONS (val
)->car
))
3303 idx
= XFASTINT (Fget (XCONS (val
)->car
, Qcoding_category_index
));
3304 if (idx
>= CODING_CATEGORY_IDX_MAX
)
3306 priorities
[i
++] = (1 << idx
);
3307 val
= XCONS (val
)->cdr
;
3309 /* If coding-category-list is valid and contains all coding
3310 categories, `i' should be CODING_CATEGORY_IDX_MAX now. If not,
3311 the following code saves Emacs from craching. */
3312 while (i
< CODING_CATEGORY_IDX_MAX
)
3313 priorities
[i
++] = CODING_CATEGORY_MASK_RAW_TEXT
;
3315 mask
= detect_coding_mask (src
, src_bytes
, priorities
, &skip
);
3316 coding
->heading_ascii
= skip
;
3320 /* We found a single coding system of the highest priority in MASK. */
3322 while (mask
&& ! (mask
& 1)) mask
>>= 1, idx
++;
3324 idx
= CODING_CATEGORY_IDX_RAW_TEXT
;
3326 val
= XSYMBOL (XVECTOR (Vcoding_category_table
)->contents
[idx
])->value
;
3328 if (coding
->eol_type
!= CODING_EOL_UNDECIDED
)
3330 Lisp_Object tmp
= Fget (val
, Qeol_type
);
3333 val
= XVECTOR (tmp
)->contents
[coding
->eol_type
];
3335 setup_coding_system (val
, coding
);
3336 /* Set this again because setup_coding_system reset this member. */
3337 coding
->heading_ascii
= skip
;
3340 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
3341 SOURCE is encoded. Return one of CODING_EOL_LF, CODING_EOL_CRLF,
3342 CODING_EOL_CR, and CODING_EOL_UNDECIDED.
3344 How many non-eol characters are at the head is returned as *SKIP. */
3346 #define MAX_EOL_CHECK_COUNT 3
3349 detect_eol_type (source
, src_bytes
, skip
)
3350 unsigned char *source
;
3351 int src_bytes
, *skip
;
3353 unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
3355 int total
= 0; /* How many end-of-lines are found so far. */
3356 int eol_type
= CODING_EOL_UNDECIDED
;
3361 while (src
< src_end
&& total
< MAX_EOL_CHECK_COUNT
)
3364 if (c
== '\n' || c
== '\r')
3367 *skip
= src
- 1 - source
;
3370 this_eol_type
= CODING_EOL_LF
;
3371 else if (src
>= src_end
|| *src
!= '\n')
3372 this_eol_type
= CODING_EOL_CR
;
3374 this_eol_type
= CODING_EOL_CRLF
, src
++;
3376 if (eol_type
== CODING_EOL_UNDECIDED
)
3377 /* This is the first end-of-line. */
3378 eol_type
= this_eol_type
;
3379 else if (eol_type
!= this_eol_type
)
3381 /* The found type is different from what found before. */
3382 eol_type
= CODING_EOL_INCONSISTENT
;
3389 *skip
= src_end
- source
;
3393 /* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC
3394 is encoded. If it detects an appropriate format of end-of-line, it
3395 sets the information in *CODING. */
3398 detect_eol (coding
, src
, src_bytes
)
3399 struct coding_system
*coding
;
3405 int eol_type
= detect_eol_type (src
, src_bytes
, &skip
);
3407 if (coding
->heading_ascii
> skip
)
3408 coding
->heading_ascii
= skip
;
3410 skip
= coding
->heading_ascii
;
3412 if (eol_type
== CODING_EOL_UNDECIDED
)
3414 if (eol_type
== CODING_EOL_INCONSISTENT
)
3417 /* This code is suppressed until we find a better way to
3418 distinguish raw text file and binary file. */
3420 /* If we have already detected that the coding is raw-text, the
3421 coding should actually be no-conversion. */
3422 if (coding
->type
== coding_type_raw_text
)
3424 setup_coding_system (Qno_conversion
, coding
);
3427 /* Else, let's decode only text code anyway. */
3429 eol_type
= CODING_EOL_LF
;
3432 val
= Fget (coding
->symbol
, Qeol_type
);
3433 if (VECTORP (val
) && XVECTOR (val
)->size
== 3)
3435 setup_coding_system (XVECTOR (val
)->contents
[eol_type
], coding
);
3436 coding
->heading_ascii
= skip
;
3440 #define CONVERSION_BUFFER_EXTRA_ROOM 256
3442 #define DECODING_BUFFER_MAG(coding) \
3443 (coding->type == coding_type_iso2022 \
3445 : ((coding->type == coding_type_sjis || coding->type == coding_type_big5) \
3447 : (coding->type == coding_type_raw_text \
3449 : (coding->type == coding_type_ccl \
3450 ? coding->spec.ccl.decoder.buf_magnification \
3453 /* Return maximum size (bytes) of a buffer enough for decoding
3454 SRC_BYTES of text encoded in CODING. */
3457 decoding_buffer_size (coding
, src_bytes
)
3458 struct coding_system
*coding
;
3461 return (src_bytes
* DECODING_BUFFER_MAG (coding
)
3462 + CONVERSION_BUFFER_EXTRA_ROOM
);
3465 /* Return maximum size (bytes) of a buffer enough for encoding
3466 SRC_BYTES of text to CODING. */
3469 encoding_buffer_size (coding
, src_bytes
)
3470 struct coding_system
*coding
;
3475 if (coding
->type
== coding_type_ccl
)
3476 magnification
= coding
->spec
.ccl
.encoder
.buf_magnification
;
3480 return (src_bytes
* magnification
+ CONVERSION_BUFFER_EXTRA_ROOM
);
3483 #ifndef MINIMUM_CONVERSION_BUFFER_SIZE
3484 #define MINIMUM_CONVERSION_BUFFER_SIZE 1024
3487 char *conversion_buffer
;
3488 int conversion_buffer_size
;
3490 /* Return a pointer to a SIZE bytes of buffer to be used for encoding
3491 or decoding. Sufficient memory is allocated automatically. If we
3492 run out of memory, return NULL. */
3495 get_conversion_buffer (size
)
3498 if (size
> conversion_buffer_size
)
3501 int real_size
= conversion_buffer_size
* 2;
3503 while (real_size
< size
) real_size
*= 2;
3504 buf
= (char *) xmalloc (real_size
);
3505 xfree (conversion_buffer
);
3506 conversion_buffer
= buf
;
3507 conversion_buffer_size
= real_size
;
3509 return conversion_buffer
;
3513 ccl_coding_driver (coding
, source
, destination
, src_bytes
, dst_bytes
, encodep
)
3514 struct coding_system
*coding
;
3515 unsigned char *source
, *destination
;
3516 int src_bytes
, dst_bytes
, encodep
;
3518 struct ccl_program
*ccl
3519 = encodep
? &coding
->spec
.ccl
.encoder
: &coding
->spec
.ccl
.decoder
;
3522 coding
->produced
= ccl_driver (ccl
, source
, destination
,
3523 src_bytes
, dst_bytes
, &(coding
->consumed
));
3526 coding
->produced_char
= coding
->produced
;
3527 coding
->consumed_char
3528 = multibyte_chars_in_text (source
, coding
->consumed
);
3532 coding
->produced_char
3533 = multibyte_chars_in_text (destination
, coding
->produced
);
3534 coding
->consumed_char
= coding
->consumed
;
3536 switch (ccl
->status
)
3538 case CCL_STAT_SUSPEND_BY_SRC
:
3539 result
= CODING_FINISH_INSUFFICIENT_SRC
;
3541 case CCL_STAT_SUSPEND_BY_DST
:
3542 result
= CODING_FINISH_INSUFFICIENT_DST
;
3545 result
= CODING_FINISH_NORMAL
;
3551 /* See "GENERAL NOTES about `decode_coding_XXX ()' functions". Before
3552 decoding, it may detect coding system and format of end-of-line if
3553 those are not yet decided. */
3556 decode_coding (coding
, source
, destination
, src_bytes
, dst_bytes
)
3557 struct coding_system
*coding
;
3558 unsigned char *source
, *destination
;
3559 int src_bytes
, dst_bytes
;
3565 coding
->produced
= coding
->produced_char
= 0;
3566 coding
->consumed
= coding
->consumed_char
= 0;
3567 coding
->fake_multibyte
= 0;
3568 return CODING_FINISH_NORMAL
;
3571 if (coding
->type
== coding_type_undecided
)
3572 detect_coding (coding
, source
, src_bytes
);
3574 if (coding
->eol_type
== CODING_EOL_UNDECIDED
)
3575 detect_eol (coding
, source
, src_bytes
);
3577 switch (coding
->type
)
3579 case coding_type_emacs_mule
:
3580 case coding_type_undecided
:
3581 case coding_type_raw_text
:
3582 if (coding
->eol_type
== CODING_EOL_LF
3583 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
3584 goto label_no_conversion
;
3585 result
= decode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
);
3588 case coding_type_sjis
:
3589 result
= decode_coding_sjis_big5 (coding
, source
, destination
,
3590 src_bytes
, dst_bytes
, 1);
3593 case coding_type_iso2022
:
3594 result
= decode_coding_iso2022 (coding
, source
, destination
,
3595 src_bytes
, dst_bytes
);
3598 case coding_type_big5
:
3599 result
= decode_coding_sjis_big5 (coding
, source
, destination
,
3600 src_bytes
, dst_bytes
, 0);
3603 case coding_type_ccl
:
3604 result
= ccl_coding_driver (coding
, source
, destination
,
3605 src_bytes
, dst_bytes
, 0);
3608 default: /* i.e. case coding_type_no_conversion: */
3609 label_no_conversion
:
3610 if (dst_bytes
&& src_bytes
> dst_bytes
)
3612 coding
->produced
= dst_bytes
;
3613 result
= CODING_FINISH_INSUFFICIENT_DST
;
3617 coding
->produced
= src_bytes
;
3618 result
= CODING_FINISH_NORMAL
;
3621 bcopy (source
, destination
, coding
->produced
);
3623 safe_bcopy (source
, destination
, coding
->produced
);
3624 coding
->fake_multibyte
= 1;
3626 = coding
->consumed_char
= coding
->produced_char
= coding
->produced
;
3633 /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". */
3636 encode_coding (coding
, source
, destination
, src_bytes
, dst_bytes
)
3637 struct coding_system
*coding
;
3638 unsigned char *source
, *destination
;
3639 int src_bytes
, dst_bytes
;
3645 coding
->produced
= coding
->produced_char
= 0;
3646 coding
->consumed
= coding
->consumed_char
= 0;
3647 coding
->fake_multibyte
= 0;
3648 return CODING_FINISH_NORMAL
;
3651 switch (coding
->type
)
3653 case coding_type_emacs_mule
:
3654 case coding_type_undecided
:
3655 case coding_type_raw_text
:
3656 if (coding
->eol_type
== CODING_EOL_LF
3657 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
3658 goto label_no_conversion
;
3659 result
= encode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
);
3662 case coding_type_sjis
:
3663 result
= encode_coding_sjis_big5 (coding
, source
, destination
,
3664 src_bytes
, dst_bytes
, 1);
3667 case coding_type_iso2022
:
3668 result
= encode_coding_iso2022 (coding
, source
, destination
,
3669 src_bytes
, dst_bytes
);
3672 case coding_type_big5
:
3673 result
= encode_coding_sjis_big5 (coding
, source
, destination
,
3674 src_bytes
, dst_bytes
, 0);
3677 case coding_type_ccl
:
3678 result
= ccl_coding_driver (coding
, source
, destination
,
3679 src_bytes
, dst_bytes
, 1);
3682 default: /* i.e. case coding_type_no_conversion: */
3683 label_no_conversion
:
3684 if (dst_bytes
&& src_bytes
> dst_bytes
)
3686 coding
->produced
= dst_bytes
;
3687 result
= CODING_FINISH_INSUFFICIENT_DST
;
3691 coding
->produced
= src_bytes
;
3692 result
= CODING_FINISH_NORMAL
;
3695 bcopy (source
, destination
, coding
->produced
);
3697 safe_bcopy (source
, destination
, coding
->produced
);
3698 if (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
)
3700 unsigned char *p
= destination
, *pend
= p
+ coding
->produced
;
3702 if (*p
++ == '\015') p
[-1] = '\n';
3704 coding
->fake_multibyte
= 1;
3706 = coding
->consumed_char
= coding
->produced_char
= coding
->produced
;
3713 /* Scan text in the region between *BEG and *END (byte positions),
3714 skip characters which we don't have to decode by coding system
3715 CODING at the head and tail, then set *BEG and *END to the region
3716 of the text we actually have to convert. The caller should move
3717 the gap out of the region in advance.
3719 If STR is not NULL, *BEG and *END are indices into STR. */
3722 shrink_decoding_region (beg
, end
, coding
, str
)
3724 struct coding_system
*coding
;
3727 unsigned char *begp_orig
, *begp
, *endp_orig
, *endp
, c
;
3730 if (coding
->type
== coding_type_ccl
3731 || coding
->type
== coding_type_undecided
3732 || !NILP (coding
->post_read_conversion
))
3734 /* We can't skip any data. */
3737 else if (coding
->type
== coding_type_no_conversion
)
3739 /* We need no conversion, but don't have to skip any data here.
3740 Decoding routine handles them effectively anyway. */
3744 if (coding
->heading_ascii
>= 0)
3745 /* Detection routine has already found how much we can skip at the
3747 *beg
+= coding
->heading_ascii
;
3751 begp_orig
= begp
= str
+ *beg
;
3752 endp_orig
= endp
= str
+ *end
;
3756 begp_orig
= begp
= BYTE_POS_ADDR (*beg
);
3757 endp_orig
= endp
= begp
+ *end
- *beg
;
3760 eol_conversion
= (coding
->eol_type
!= CODING_EOL_LF
);
3762 switch (coding
->type
)
3764 case coding_type_emacs_mule
:
3765 case coding_type_raw_text
:
3768 if (coding
->heading_ascii
< 0)
3769 while (begp
< endp
&& *begp
!= '\r' && *begp
< 0x80) begp
++;
3770 while (begp
< endp
&& *(endp
- 1) != '\r' && *(endp
- 1) < 0x80)
3777 case coding_type_sjis
:
3778 case coding_type_big5
:
3779 /* We can skip all ASCII characters at the head. */
3780 if (coding
->heading_ascii
< 0)
3783 while (begp
< endp
&& *begp
< 0x80 && *begp
!= '\n') begp
++;
3785 while (begp
< endp
&& *begp
< 0x80) begp
++;
3787 /* We can skip all ASCII characters at the tail except for the
3788 second byte of SJIS or BIG5 code. */
3790 while (begp
< endp
&& endp
[-1] < 0x80 && endp
[-1] != '\n') endp
--;
3792 while (begp
< endp
&& endp
[-1] < 0x80) endp
--;
3793 if (begp
< endp
&& endp
< endp_orig
&& endp
[-1] >= 0x80)
3797 default: /* i.e. case coding_type_iso2022: */
3798 if (coding
->heading_ascii
< 0)
3800 /* We can skip all ASCII characters at the head except for a
3801 few control codes. */
3802 while (begp
< endp
&& (c
= *begp
) < 0x80
3803 && c
!= ISO_CODE_CR
&& c
!= ISO_CODE_SO
3804 && c
!= ISO_CODE_SI
&& c
!= ISO_CODE_ESC
3805 && (!eol_conversion
|| c
!= ISO_CODE_LF
))
3808 switch (coding
->category_idx
)
3810 case CODING_CATEGORY_IDX_ISO_8_1
:
3811 case CODING_CATEGORY_IDX_ISO_8_2
:
3812 /* We can skip all ASCII characters at the tail. */
3814 while (begp
< endp
&& (c
= endp
[-1]) < 0x80 && c
!= '\n') endp
--;
3816 while (begp
< endp
&& endp
[-1] < 0x80) endp
--;
3819 case CODING_CATEGORY_IDX_ISO_7
:
3820 case CODING_CATEGORY_IDX_ISO_7_TIGHT
:
3821 /* We can skip all charactes at the tail except for ESC and
3822 the following 2-byte at the tail. */
3825 && (c
= endp
[-1]) < 0x80 && c
!= ISO_CODE_ESC
&& c
!= '\n')
3829 && (c
= endp
[-1]) < 0x80 && c
!= ISO_CODE_ESC
)
3831 if (begp
< endp
&& endp
[-1] == ISO_CODE_ESC
)
3833 if (endp
+ 1 < endp_orig
&& end
[0] == '(' && end
[1] == 'B')
3834 /* This is an ASCII designation sequence. We can
3835 surely skip the tail. */
3838 /* Hmmm, we can't skip the tail. */
3843 *beg
+= begp
- begp_orig
;
3844 *end
+= endp
- endp_orig
;
3848 /* Like shrink_decoding_region but for encoding. */
3851 shrink_encoding_region (beg
, end
, coding
, str
)
3853 struct coding_system
*coding
;
3856 unsigned char *begp_orig
, *begp
, *endp_orig
, *endp
;
3859 if (coding
->type
== coding_type_ccl
)
3860 /* We can't skip any data. */
3862 else if (coding
->type
== coding_type_no_conversion
)
3864 /* We need no conversion. */
3871 begp_orig
= begp
= str
+ *beg
;
3872 endp_orig
= endp
= str
+ *end
;
3876 begp_orig
= begp
= BYTE_POS_ADDR (*beg
);
3877 endp_orig
= endp
= begp
+ *end
- *beg
;
3880 eol_conversion
= (coding
->eol_type
== CODING_EOL_CR
3881 || coding
->eol_type
== CODING_EOL_CRLF
);
3883 /* Here, we don't have to check coding->pre_write_conversion because
3884 the caller is expected to have handled it already. */
3885 switch (coding
->type
)
3887 case coding_type_undecided
:
3888 case coding_type_emacs_mule
:
3889 case coding_type_raw_text
:
3892 while (begp
< endp
&& *begp
!= '\n') begp
++;
3893 while (begp
< endp
&& endp
[-1] != '\n') endp
--;
3899 case coding_type_iso2022
:
3900 if (coding
->flags
& CODING_FLAG_ISO_DESIGNATE_AT_BOL
)
3902 unsigned char *bol
= begp
;
3903 while (begp
< endp
&& *begp
< 0x80)
3906 if (begp
[-1] == '\n')
3910 goto label_skip_tail
;
3915 /* We can skip all ASCII characters at the head and tail. */
3917 while (begp
< endp
&& *begp
< 0x80 && *begp
!= '\n') begp
++;
3919 while (begp
< endp
&& *begp
< 0x80) begp
++;
3922 while (begp
< endp
&& endp
[-1] < 0x80 && endp
[-1] != '\n') endp
--;
3924 while (begp
< endp
&& *(endp
- 1) < 0x80) endp
--;
3928 *beg
+= begp
- begp_orig
;
3929 *end
+= endp
- endp_orig
;
3933 /* Decode (if ENCODEP is zero) or encode (if ENCODEP is nonzero) the
3934 text from FROM to TO (byte positions are FROM_BYTE and TO_BYTE) by
3935 coding system CODING, and return the status code of code conversion
3936 (currently, this value has no meaning).
3938 How many characters (and bytes) are converted to how many
3939 characters (and bytes) are recorded in members of the structure
3942 If REPLACE is nonzero, we do various things as if the original text
3943 is deleted and a new text is inserted. See the comments in
3944 replace_range (insdel.c) to know what we are doing. */
3947 code_convert_region (from
, from_byte
, to
, to_byte
, coding
, encodep
, replace
)
3948 int from
, from_byte
, to
, to_byte
, encodep
, replace
;
3949 struct coding_system
*coding
;
3951 int len
= to
- from
, len_byte
= to_byte
- from_byte
;
3952 int require
, inserted
, inserted_byte
;
3953 int from_byte_orig
, to_byte_orig
;
3954 Lisp_Object saved_coding_symbol
= Qnil
;
3955 int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
3957 int fake_multibyte
= 0;
3958 unsigned char *src
, *dst
;
3962 int saved_from
= from
;
3964 prepare_to_modify_buffer (from
, to
, &from
);
3965 if (saved_from
!= from
)
3969 from_byte
= CHAR_TO_BYTE (from
), to_byte
= CHAR_TO_BYTE (to
);
3971 from_byte
= from
, to_byte
= to
;
3972 len_byte
= to_byte
- from_byte
;
3976 if (! encodep
&& CODING_REQUIRE_DETECTION (coding
))
3978 /* We must detect encoding of text and eol. Even if detection
3979 routines can't decide the encoding, we should not let them
3980 undecided because the deeper decoding routine (decode_coding)
3981 tries to detect the encodings in vain in that case. */
3983 if (from
< GPT
&& to
> GPT
)
3984 move_gap_both (from
, from_byte
);
3985 if (coding
->type
== coding_type_undecided
)
3987 detect_coding (coding
, BYTE_POS_ADDR (from_byte
), len_byte
);
3988 if (coding
->type
== coding_type_undecided
)
3989 coding
->type
= coding_type_emacs_mule
;
3991 if (coding
->eol_type
== CODING_EOL_UNDECIDED
)
3993 saved_coding_symbol
= coding
->symbol
;
3994 detect_eol (coding
, BYTE_POS_ADDR (from_byte
), len_byte
);
3995 if (coding
->eol_type
== CODING_EOL_UNDECIDED
)
3996 coding
->eol_type
= CODING_EOL_LF
;
3997 /* We had better recover the original eol format if we
3998 encounter an inconsitent eol format while decoding. */
3999 coding
->mode
|= CODING_MODE_INHIBIT_INCONSISTENT_EOL
;
4003 coding
->consumed_char
= len
, coding
->consumed
= len_byte
;
4006 ? ! CODING_REQUIRE_ENCODING (coding
)
4007 : ! CODING_REQUIRE_DECODING (coding
))
4009 coding
->produced
= len_byte
;
4012 /* We still may have to combine byte at the head and the
4013 tail of the text in the region. */
4015 move_gap_both (to
, to_byte
);
4016 coding
->produced_char
4017 = multibyte_chars_in_text (BYTE_POS_ADDR (from_byte
), len_byte
);
4018 GAP_SIZE
+= len_byte
;
4019 GPT_BYTE
-= len_byte
;
4020 ZV_BYTE
-= len_byte
;
4025 adjust_after_replace (from
, from_byte
, to
, to_byte
,
4026 coding
->produced_char
, len_byte
, replace
);
4029 coding
->produced_char
= len_byte
;
4033 /* Now we convert the text. */
4035 /* For encoding, we must process pre-write-conversion in advance. */
4037 && ! NILP (coding
->pre_write_conversion
)
4038 && SYMBOLP (coding
->pre_write_conversion
)
4039 && ! NILP (Ffboundp (coding
->pre_write_conversion
)))
4041 /* The function in pre-write-conversion may put a new text in a
4043 struct buffer
*prev
= current_buffer
, *new;
4045 call2 (coding
->pre_write_conversion
, from
, to
);
4046 if (current_buffer
!= prev
)
4049 new = current_buffer
;
4050 set_buffer_internal_1 (prev
);
4051 del_range_2 (from
, from_byte
, to
, to_byte
);
4052 insert_from_buffer (new, BEG
, len
, 0);
4054 to_byte
= multibyte
? CHAR_TO_BYTE (to
) : to
;
4055 len_byte
= to_byte
- from_byte
;
4059 /* Try to skip the heading and tailing ASCIIs. */
4060 from_byte_orig
= from_byte
; to_byte_orig
= to_byte
;
4061 if (from
< GPT
&& GPT
< to
)
4064 shrink_encoding_region (&from_byte
, &to_byte
, coding
, NULL
);
4066 shrink_decoding_region (&from_byte
, &to_byte
, coding
, NULL
);
4067 if (from_byte
== to_byte
)
4069 coding
->produced
= len_byte
;
4070 coding
->produced_char
= multibyte
? len
: len_byte
;
4074 /* Here, the excluded region by shrinking contains only ASCIIs. */
4075 from
+= (from_byte
- from_byte_orig
);
4076 to
+= (to_byte
- to_byte_orig
);
4078 len_byte
= to_byte
- from_byte
;
4080 /* For converion, we must put the gap before the text in addition to
4081 making the gap larger for efficient decoding. The required gap
4082 size starts from 2000 which is the magic number used in make_gap.
4083 But, after one batch of conversion, it will be incremented if we
4084 find that it is not enough . */
4087 if (GAP_SIZE
< require
)
4088 make_gap (require
- GAP_SIZE
);
4089 move_gap_both (from
, from_byte
);
4092 adjust_before_replace (from
, from_byte
, to
, to_byte
);
4094 if (GPT
- BEG
< beg_unchanged
)
4095 beg_unchanged
= GPT
- BEG
;
4096 if (Z
- GPT
< end_unchanged
)
4097 end_unchanged
= Z
- GPT
;
4099 inserted
= inserted_byte
= 0;
4100 src
= GAP_END_ADDR
, dst
= GPT_ADDR
;
4102 GAP_SIZE
+= len_byte
;
4105 ZV_BYTE
-= len_byte
;
4112 /* The buffer memory is changed from:
4113 +--------+converted-text+---------+-------original-text------+---+
4114 |<-from->|<--inserted-->|---------|<-----------len---------->|---|
4115 |<------------------- GAP_SIZE -------------------->| */
4117 result
= encode_coding (coding
, src
, dst
, len_byte
, 0);
4119 result
= decode_coding (coding
, src
, dst
, len_byte
, 0);
4121 +--------+-------converted-text--------+--+---original-text--+---+
4122 |<-from->|<--inserted-->|<--produced-->|--|<-(len-consumed)->|---|
4123 |<------------------- GAP_SIZE -------------------->| */
4124 if (coding
->fake_multibyte
)
4127 if (!encodep
&& !multibyte
)
4128 coding
->produced_char
= coding
->produced
;
4129 inserted
+= coding
->produced_char
;
4130 inserted_byte
+= coding
->produced
;
4131 len_byte
-= coding
->consumed
;
4132 src
+= coding
->consumed
;
4133 dst
+= inserted_byte
;
4135 if (! encodep
&& result
== CODING_FINISH_INCONSISTENT_EOL
)
4137 unsigned char *pend
= dst
, *p
= pend
- inserted_byte
;
4139 /* Encode LFs back to the original eol format (CR or CRLF). */
4140 if (coding
->eol_type
== CODING_EOL_CR
)
4142 while (p
< pend
) if (*p
++ == '\n') p
[-1] = '\r';
4148 while (p
< pend
) if (*p
++ == '\n') count
++;
4149 if (src
- dst
< count
)
4151 /* We don't have sufficient room for putting LFs
4152 back to CRLF. We must record converted and
4153 not-yet-converted text back to the buffer
4154 content, enlarge the gap, then record them out of
4155 the buffer contents again. */
4156 int add
= len_byte
+ inserted_byte
;
4159 ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
4160 GPT
+= inserted_byte
; GPT_BYTE
+= inserted_byte
;
4161 make_gap (count
- GAP_SIZE
);
4163 ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
4164 GPT
-= inserted_byte
; GPT_BYTE
-= inserted_byte
;
4165 /* Don't forget to update SRC, DST, and PEND. */
4166 src
= GAP_END_ADDR
- len_byte
;
4167 dst
= GPT_ADDR
+ inserted_byte
;
4171 inserted_byte
+= count
;
4172 coding
->produced
+= count
;
4173 p
= dst
= pend
+ count
;
4177 if (*p
== '\n') count
--, *--p
= '\r';
4181 /* Suppress eol-format conversion in the further conversion. */
4182 coding
->eol_type
= CODING_EOL_LF
;
4184 /* Restore the original symbol. */
4185 coding
->symbol
= saved_coding_symbol
;
4191 if (result
== CODING_FINISH_INSUFFICIENT_SRC
)
4193 /* The source text ends in invalid codes. Let's just
4194 make them valid buffer contents, and finish conversion. */
4195 inserted
+= len_byte
;
4196 inserted_byte
+= len_byte
;
4204 /* We have just done the first batch of conversion which was
4205 stoped because of insufficient gap. Let's reconsider the
4206 required gap size (i.e. SRT - DST) now.
4208 We have converted ORIG bytes (== coding->consumed) into
4209 NEW bytes (coding->produced). To convert the remaining
4210 LEN bytes, we may need REQUIRE bytes of gap, where:
4211 REQUIRE + LEN_BYTE = LEN_BYTE * (NEW / ORIG)
4212 REQUIRE = LEN_BYTE * (NEW - ORIG) / ORIG
4213 Here, we are sure that NEW >= ORIG. */
4214 float ratio
= coding
->produced
- coding
->consumed
;
4215 ratio
/= coding
->consumed
;
4216 require
= len_byte
* ratio
;
4219 if ((src
- dst
) < (require
+ 2000))
4221 /* See the comment above the previous call of make_gap. */
4222 int add
= len_byte
+ inserted_byte
;
4225 ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
4226 GPT
+= inserted_byte
; GPT_BYTE
+= inserted_byte
;
4227 make_gap (require
+ 2000);
4229 ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
4230 GPT
-= inserted_byte
; GPT_BYTE
-= inserted_byte
;
4231 /* Don't forget to update SRC, DST. */
4232 src
= GAP_END_ADDR
- len_byte
;
4233 dst
= GPT_ADDR
+ inserted_byte
;
4236 if (src
- dst
> 0) *dst
= 0; /* Put an anchor. */
4239 && (fake_multibyte
|| !encodep
&& (to
- from
) != (to_byte
- from_byte
)))
4240 inserted
= multibyte_chars_in_text (GPT_ADDR
, inserted_byte
);
4242 adjust_after_replace (from
, from_byte
, to
, to_byte
,
4243 inserted
, inserted_byte
, replace
);
4244 if (from_byte_orig
== from_byte
)
4245 from_byte_orig
= from_byte
= PT_BYTE
;
4247 if (! encodep
&& ! NILP (coding
->post_read_conversion
))
4250 int orig_inserted
= inserted
, pos
= PT
;
4253 temp_set_point_both (current_buffer
, from
, from_byte
);
4254 val
= call1 (coding
->post_read_conversion
, make_number (inserted
));
4257 CHECK_NUMBER (val
, 0);
4258 inserted
= XFASTINT (val
);
4260 if (pos
>= from
+ orig_inserted
)
4261 temp_set_point (current_buffer
, pos
+ (inserted
- orig_inserted
));
4264 signal_after_change (from
, to
- from
, inserted
);
4267 int skip
= (to_byte_orig
- to_byte
) + (from_byte
- from_byte_orig
);
4269 coding
->consumed
= to_byte_orig
- from_byte_orig
;
4270 coding
->consumed_char
= skip
+ (to
- from
);
4271 coding
->produced
= skip
+ inserted_byte
;
4272 coding
->produced_char
= skip
+ inserted
;
4279 code_convert_string (str
, coding
, encodep
, nocopy
)
4281 struct coding_system
*coding
;
4282 int encodep
, nocopy
;
4286 int from
= 0, to
= XSTRING (str
)->size
, to_byte
= XSTRING (str
)->size_byte
;
4287 struct gcpro gcpro1
;
4288 Lisp_Object saved_coding_symbol
= Qnil
;
4291 if (encodep
&& !NILP (coding
->pre_write_conversion
)
4292 || !encodep
&& !NILP (coding
->post_read_conversion
))
4294 /* Since we have to call Lisp functions which assume target text
4295 is in a buffer, after setting a temporary buffer, call
4296 code_convert_region. */
4297 int count
= specpdl_ptr
- specpdl
;
4298 struct buffer
*prev
= current_buffer
;
4300 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
4301 temp_output_buffer_setup (" *code-converting-work*");
4302 set_buffer_internal (XBUFFER (Vstandard_output
));
4304 insert_from_string (str
, 0, 0, to
, to_byte
, 0);
4307 /* We must insert the contents of STR as is without
4308 unibyte<->multibyte conversion. */
4309 current_buffer
->enable_multibyte_characters
= Qnil
;
4310 insert_from_string (str
, 0, 0, to_byte
, to_byte
, 0);
4311 current_buffer
->enable_multibyte_characters
= Qt
;
4313 code_convert_region (BEGV
, BEGV_BYTE
, ZV
, ZV_BYTE
, coding
, encodep
, 1);
4315 /* We must return the buffer contents as unibyte string. */
4316 current_buffer
->enable_multibyte_characters
= Qnil
;
4317 str
= make_buffer_string (BEGV
, ZV
, 0);
4318 set_buffer_internal (prev
);
4319 return unbind_to (count
, str
);
4322 if (! encodep
&& CODING_REQUIRE_DETECTION (coding
))
4324 /* See the comments in code_convert_region. */
4325 if (coding
->type
== coding_type_undecided
)
4327 detect_coding (coding
, XSTRING (str
)->data
, to_byte
);
4328 if (coding
->type
== coding_type_undecided
)
4329 coding
->type
= coding_type_emacs_mule
;
4331 if (coding
->eol_type
== CODING_EOL_UNDECIDED
)
4333 saved_coding_symbol
= coding
->symbol
;
4334 detect_eol (coding
, XSTRING (str
)->data
, to_byte
);
4335 if (coding
->eol_type
== CODING_EOL_UNDECIDED
)
4336 coding
->eol_type
= CODING_EOL_LF
;
4337 /* We had better recover the original eol format if we
4338 encounter an inconsitent eol format while decoding. */
4339 coding
->mode
|= CODING_MODE_INHIBIT_INCONSISTENT_EOL
;
4344 ? ! CODING_REQUIRE_ENCODING (coding
)
4345 : ! CODING_REQUIRE_DECODING (coding
))
4349 /* Try to skip the heading and tailing ASCIIs. */
4351 shrink_encoding_region (&from
, &to_byte
, coding
, XSTRING (str
)->data
);
4353 shrink_decoding_region (&from
, &to_byte
, coding
, XSTRING (str
)->data
);
4355 if (from
== to_byte
)
4356 return (nocopy
? str
: Fcopy_sequence (str
));
4359 len
= encoding_buffer_size (coding
, to_byte
- from
);
4361 len
= decoding_buffer_size (coding
, to_byte
- from
);
4362 len
+= from
+ XSTRING (str
)->size_byte
- to_byte
;
4364 buf
= get_conversion_buffer (len
);
4368 bcopy (XSTRING (str
)->data
, buf
, from
);
4370 ? encode_coding (coding
, XSTRING (str
)->data
+ from
,
4371 buf
+ from
, to_byte
- from
, len
)
4372 : decode_coding (coding
, XSTRING (str
)->data
+ from
,
4373 buf
+ from
, to
- from
, len
));
4374 if (! encodep
&& result
== CODING_FINISH_INCONSISTENT_EOL
)
4376 /* We simple try to decode the whole string again but without
4377 eol-conversion this time. */
4378 coding
->eol_type
= CODING_EOL_LF
;
4379 coding
->symbol
= saved_coding_symbol
;
4380 return code_convert_string (str
, coding
, encodep
, nocopy
);
4383 bcopy (XSTRING (str
)->data
+ to_byte
, buf
+ from
+ coding
->produced
,
4384 XSTRING (str
)->size_byte
- to_byte
);
4386 len
= from
+ XSTRING (str
)->size_byte
- to_byte
;
4388 str
= make_unibyte_string (buf
, len
+ coding
->produced
);
4390 str
= make_multibyte_string (buf
, len
+ coding
->produced_char
,
4391 len
+ coding
->produced
);
4397 /*** 7. Emacs Lisp library functions ***/
4399 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
4400 "Return t if OBJECT is nil or a coding-system.\n\
4401 See the documentation of `make-coding-system' for information\n\
4402 about coding-system objects.")
4410 /* Get coding-spec vector for OBJ. */
4411 obj
= Fget (obj
, Qcoding_system
);
4412 return ((VECTORP (obj
) && XVECTOR (obj
)->size
== 5)
4416 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
4417 Sread_non_nil_coding_system
, 1, 1, 0,
4418 "Read a coding system from the minibuffer, prompting with string PROMPT.")
4425 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
4426 Qt
, Qnil
, Qcoding_system_history
, Qnil
, Qnil
);
4428 while (XSTRING (val
)->size
== 0);
4429 return (Fintern (val
, Qnil
));
4432 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 2, 0,
4433 "Read a coding system from the minibuffer, prompting with string PROMPT.\n\
4434 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.")
4435 (prompt
, default_coding_system
)
4436 Lisp_Object prompt
, default_coding_system
;
4439 if (SYMBOLP (default_coding_system
))
4440 XSETSTRING (default_coding_system
, XSYMBOL (default_coding_system
)->name
);
4441 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
4442 Qt
, Qnil
, Qcoding_system_history
,
4443 default_coding_system
, Qnil
);
4444 return (XSTRING (val
)->size
== 0 ? Qnil
: Fintern (val
, Qnil
));
4447 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
4449 "Check validity of CODING-SYSTEM.\n\
4450 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.\n\
4451 It is valid if it is a symbol with a non-nil `coding-system' property.\n\
4452 The value of property should be a vector of length 5.")
4454 Lisp_Object coding_system
;
4456 CHECK_SYMBOL (coding_system
, 0);
4457 if (!NILP (Fcoding_system_p (coding_system
)))
4458 return coding_system
;
4460 Fsignal (Qcoding_system_error
, Fcons (coding_system
, Qnil
));
4464 detect_coding_system (src
, src_bytes
, highest
)
4466 int src_bytes
, highest
;
4468 int coding_mask
, eol_type
;
4469 Lisp_Object val
, tmp
;
4472 coding_mask
= detect_coding_mask (src
, src_bytes
, NULL
, &dummy
);
4473 eol_type
= detect_eol_type (src
, src_bytes
, &dummy
);
4474 if (eol_type
== CODING_EOL_INCONSISTENT
)
4475 eol_type
== CODING_EOL_UNDECIDED
;
4480 if (eol_type
!= CODING_EOL_UNDECIDED
)
4483 val2
= Fget (Qundecided
, Qeol_type
);
4485 val
= XVECTOR (val2
)->contents
[eol_type
];
4490 /* At first, gather possible coding systems in VAL. */
4492 for (tmp
= Vcoding_category_list
; !NILP (tmp
); tmp
= XCONS (tmp
)->cdr
)
4495 = XFASTINT (Fget (XCONS (tmp
)->car
, Qcoding_category_index
));
4496 if (coding_mask
& (1 << idx
))
4498 val
= Fcons (Fsymbol_value (XCONS (tmp
)->car
), val
);
4504 val
= Fnreverse (val
);
4506 /* Then, substitute the elements by subsidiary coding systems. */
4507 for (tmp
= val
; !NILP (tmp
); tmp
= XCONS (tmp
)->cdr
)
4509 if (eol_type
!= CODING_EOL_UNDECIDED
)
4512 eol
= Fget (XCONS (tmp
)->car
, Qeol_type
);
4514 XCONS (tmp
)->car
= XVECTOR (eol
)->contents
[eol_type
];
4517 return (highest
? XCONS (val
)->car
: val
);
4520 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
4522 "Detect coding system of the text in the region between START and END.\n\
4523 Return a list of possible coding systems ordered by priority.\n\
4525 If only ASCII characters are found, it returns `undecided'\n\
4526 or its subsidiary coding system according to a detected end-of-line format.\n\
4528 If optional argument HIGHEST is non-nil, return the coding system of\n\
4530 (start
, end
, highest
)
4531 Lisp_Object start
, end
, highest
;
4534 int from_byte
, to_byte
;
4536 CHECK_NUMBER_COERCE_MARKER (start
, 0);
4537 CHECK_NUMBER_COERCE_MARKER (end
, 1);
4539 validate_region (&start
, &end
);
4540 from
= XINT (start
), to
= XINT (end
);
4541 from_byte
= CHAR_TO_BYTE (from
);
4542 to_byte
= CHAR_TO_BYTE (to
);
4544 if (from
< GPT
&& to
>= GPT
)
4545 move_gap_both (to
, to_byte
);
4547 return detect_coding_system (BYTE_POS_ADDR (from_byte
),
4548 to_byte
- from_byte
,
4552 DEFUN ("detect-coding-string", Fdetect_coding_string
, Sdetect_coding_string
,
4554 "Detect coding system of the text in STRING.\n\
4555 Return a list of possible coding systems ordered by priority.\n\
4557 If only ASCII characters are found, it returns `undecided'\n\
4558 or its subsidiary coding system according to a detected end-of-line format.\n\
4560 If optional argument HIGHEST is non-nil, return the coding system of\n\
4563 Lisp_Object string
, highest
;
4565 CHECK_STRING (string
, 0);
4567 return detect_coding_system (XSTRING (string
)->data
,
4568 XSTRING (string
)->size_byte
,
4573 code_convert_region1 (start
, end
, coding_system
, encodep
)
4574 Lisp_Object start
, end
, coding_system
;
4577 struct coding_system coding
;
4580 CHECK_NUMBER_COERCE_MARKER (start
, 0);
4581 CHECK_NUMBER_COERCE_MARKER (end
, 1);
4582 CHECK_SYMBOL (coding_system
, 2);
4584 validate_region (&start
, &end
);
4585 from
= XFASTINT (start
);
4586 to
= XFASTINT (end
);
4588 if (NILP (coding_system
))
4589 return make_number (to
- from
);
4591 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
4592 error ("Invalid coding system: %s", XSYMBOL (coding_system
)->name
->data
);
4594 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
4595 code_convert_region (from
, CHAR_TO_BYTE (from
), to
, CHAR_TO_BYTE (to
),
4596 &coding
, encodep
, 1);
4597 return make_number (coding
.produced_char
);
4600 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
4601 3, 3, "r\nzCoding system: ",
4602 "Decode the current region by specified coding system.\n\
4603 When called from a program, takes three arguments:\n\
4604 START, END, and CODING-SYSTEM. START and END are buffer positions.\n\
4605 Return length of decoded text.")
4606 (start
, end
, coding_system
)
4607 Lisp_Object start
, end
, coding_system
;
4609 return code_convert_region1 (start
, end
, coding_system
, 0);
4612 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
4613 3, 3, "r\nzCoding system: ",
4614 "Encode the current region by specified coding system.\n\
4615 When called from a program, takes three arguments:\n\
4616 START, END, and CODING-SYSTEM. START and END are buffer positions.\n\
4617 Return length of encoded text.")
4618 (start
, end
, coding_system
)
4619 Lisp_Object start
, end
, coding_system
;
4621 return code_convert_region1 (start
, end
, coding_system
, 1);
4625 code_convert_string1 (string
, coding_system
, nocopy
, encodep
)
4626 Lisp_Object string
, coding_system
, nocopy
;
4629 struct coding_system coding
;
4631 CHECK_STRING (string
, 0);
4632 CHECK_SYMBOL (coding_system
, 1);
4634 if (NILP (coding_system
))
4635 return (NILP (nocopy
) ? Fcopy_sequence (string
) : string
);
4637 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
4638 error ("Invalid coding system: %s", XSYMBOL (coding_system
)->name
->data
);
4640 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
4641 return code_convert_string (string
, &coding
, encodep
, !NILP (nocopy
));
4644 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
4646 "Decode STRING which is encoded in CODING-SYSTEM, and return the result.\n\
4647 Optional arg NOCOPY non-nil means it is ok to return STRING itself\n\
4648 if the decoding operation is trivial.")
4649 (string
, coding_system
, nocopy
)
4650 Lisp_Object string
, coding_system
, nocopy
;
4652 return code_convert_string1(string
, coding_system
, nocopy
, 0);
4655 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
4657 "Encode STRING to CODING-SYSTEM, and return the result.\n\
4658 Optional arg NOCOPY non-nil means it is ok to return STRING itself\n\
4659 if the encoding operation is trivial.")
4660 (string
, coding_system
, nocopy
)
4661 Lisp_Object string
, coding_system
, nocopy
;
4663 return code_convert_string1(string
, coding_system
, nocopy
, 1);
4667 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
4668 "Decode a JISX0208 character of shift-jis encoding.\n\
4669 CODE is the character code in SJIS.\n\
4670 Return the corresponding character.")
4674 unsigned char c1
, c2
, s1
, s2
;
4677 CHECK_NUMBER (code
, 0);
4678 s1
= (XFASTINT (code
)) >> 8, s2
= (XFASTINT (code
)) & 0xFF;
4679 DECODE_SJIS (s1
, s2
, c1
, c2
);
4680 XSETFASTINT (val
, MAKE_NON_ASCII_CHAR (charset_jisx0208
, c1
, c2
));
4684 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
4685 "Encode a JISX0208 character CHAR to SJIS coding system.\n\
4686 Return the corresponding character code in SJIS.")
4690 int charset
, c1
, c2
, s1
, s2
;
4693 CHECK_NUMBER (ch
, 0);
4694 SPLIT_CHAR (XFASTINT (ch
), charset
, c1
, c2
);
4695 if (charset
== charset_jisx0208
)
4697 ENCODE_SJIS (c1
, c2
, s1
, s2
);
4698 XSETFASTINT (val
, (s1
<< 8) | s2
);
4701 XSETFASTINT (val
, 0);
4705 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
4706 "Decode a Big5 character CODE of BIG5 coding system.\n\
4707 CODE is the character code in BIG5.\n\
4708 Return the corresponding character.")
4713 unsigned char b1
, b2
, c1
, c2
;
4716 CHECK_NUMBER (code
, 0);
4717 b1
= (XFASTINT (code
)) >> 8, b2
= (XFASTINT (code
)) & 0xFF;
4718 DECODE_BIG5 (b1
, b2
, charset
, c1
, c2
);
4719 XSETFASTINT (val
, MAKE_NON_ASCII_CHAR (charset
, c1
, c2
));
4723 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
4724 "Encode the Big5 character CHAR to BIG5 coding system.\n\
4725 Return the corresponding character code in Big5.")
4729 int charset
, c1
, c2
, b1
, b2
;
4732 CHECK_NUMBER (ch
, 0);
4733 SPLIT_CHAR (XFASTINT (ch
), charset
, c1
, c2
);
4734 if (charset
== charset_big5_1
|| charset
== charset_big5_2
)
4736 ENCODE_BIG5 (charset
, c1
, c2
, b1
, b2
);
4737 XSETFASTINT (val
, (b1
<< 8) | b2
);
4740 XSETFASTINT (val
, 0);
4744 DEFUN ("set-terminal-coding-system-internal",
4745 Fset_terminal_coding_system_internal
,
4746 Sset_terminal_coding_system_internal
, 1, 1, 0, "")
4748 Lisp_Object coding_system
;
4750 CHECK_SYMBOL (coding_system
, 0);
4751 setup_coding_system (Fcheck_coding_system (coding_system
), &terminal_coding
);
4752 /* We had better not send unsafe characters to terminal. */
4753 terminal_coding
.flags
|= CODING_FLAG_ISO_SAFE
;
4758 DEFUN ("set-safe-terminal-coding-system-internal",
4759 Fset_safe_terminal_coding_system_internal
,
4760 Sset_safe_terminal_coding_system_internal
, 1, 1, 0, "")
4762 Lisp_Object coding_system
;
4764 CHECK_SYMBOL (coding_system
, 0);
4765 setup_coding_system (Fcheck_coding_system (coding_system
),
4766 &safe_terminal_coding
);
4770 DEFUN ("terminal-coding-system",
4771 Fterminal_coding_system
, Sterminal_coding_system
, 0, 0, 0,
4772 "Return coding system specified for terminal output.")
4775 return terminal_coding
.symbol
;
4778 DEFUN ("set-keyboard-coding-system-internal",
4779 Fset_keyboard_coding_system_internal
,
4780 Sset_keyboard_coding_system_internal
, 1, 1, 0, "")
4782 Lisp_Object coding_system
;
4784 CHECK_SYMBOL (coding_system
, 0);
4785 setup_coding_system (Fcheck_coding_system (coding_system
), &keyboard_coding
);
4789 DEFUN ("keyboard-coding-system",
4790 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 0, 0,
4791 "Return coding system specified for decoding keyboard input.")
4794 return keyboard_coding
.symbol
;
4798 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
4799 Sfind_operation_coding_system
, 1, MANY
, 0,
4800 "Choose a coding system for an operation based on the target name.\n\
4801 The value names a pair of coding systems: (DECODING-SYSTEM ENCODING-SYSTEM).\n\
4802 DECODING-SYSTEM is the coding system to use for decoding\n\
4803 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system\n\
4804 for encoding (in case OPERATION does encoding).\n\
4806 The first argument OPERATION specifies an I/O primitive:\n\
4807 For file I/O, `insert-file-contents' or `write-region'.\n\
4808 For process I/O, `call-process', `call-process-region', or `start-process'.\n\
4809 For network I/O, `open-network-stream'.\n\
4811 The remaining arguments should be the same arguments that were passed\n\
4812 to the primitive. Depending on which primitive, one of those arguments\n\
4813 is selected as the TARGET. For example, if OPERATION does file I/O,\n\
4814 whichever argument specifies the file name is TARGET.\n\
4816 TARGET has a meaning which depends on OPERATION:\n\
4817 For file I/O, TARGET is a file name.\n\
4818 For process I/O, TARGET is a process name.\n\
4819 For network I/O, TARGET is a service name or a port number\n\
4821 This function looks up what specified for TARGET in,\n\
4822 `file-coding-system-alist', `process-coding-system-alist',\n\
4823 or `network-coding-system-alist' depending on OPERATION.\n\
4824 They may specify a coding system, a cons of coding systems,\n\
4825 or a function symbol to call.\n\
4826 In the last case, we call the function with one argument,\n\
4827 which is a list of all the arguments given to this function.")
4832 Lisp_Object operation
, target_idx
, target
, val
;
4833 register Lisp_Object chain
;
4836 error ("Too few arguments");
4837 operation
= args
[0];
4838 if (!SYMBOLP (operation
)
4839 || !INTEGERP (target_idx
= Fget (operation
, Qtarget_idx
)))
4840 error ("Invalid first arguement");
4841 if (nargs
< 1 + XINT (target_idx
))
4842 error ("Too few arguments for operation: %s",
4843 XSYMBOL (operation
)->name
->data
);
4844 target
= args
[XINT (target_idx
) + 1];
4845 if (!(STRINGP (target
)
4846 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
4847 error ("Invalid %dth argument", XINT (target_idx
) + 1);
4849 chain
= ((EQ (operation
, Qinsert_file_contents
)
4850 || EQ (operation
, Qwrite_region
))
4851 ? Vfile_coding_system_alist
4852 : (EQ (operation
, Qopen_network_stream
)
4853 ? Vnetwork_coding_system_alist
4854 : Vprocess_coding_system_alist
));
4858 for (; CONSP (chain
); chain
= XCONS (chain
)->cdr
)
4861 elt
= XCONS (chain
)->car
;
4864 && ((STRINGP (target
)
4865 && STRINGP (XCONS (elt
)->car
)
4866 && fast_string_match (XCONS (elt
)->car
, target
) >= 0)
4867 || (INTEGERP (target
) && EQ (target
, XCONS (elt
)->car
))))
4869 val
= XCONS (elt
)->cdr
;
4870 /* Here, if VAL is both a valid coding system and a valid
4871 function symbol, we return VAL as a coding system. */
4874 if (! SYMBOLP (val
))
4876 if (! NILP (Fcoding_system_p (val
)))
4877 return Fcons (val
, val
);
4878 if (! NILP (Ffboundp (val
)))
4880 val
= call1 (val
, Flist (nargs
, args
));
4883 if (SYMBOLP (val
) && ! NILP (Fcoding_system_p (val
)))
4884 return Fcons (val
, val
);
4892 DEFUN ("update-iso-coding-systems", Fupdate_iso_coding_systems
,
4893 Supdate_iso_coding_systems
, 0, 0, 0,
4894 "Update internal database for ISO2022 based coding systems.\n\
4895 When values of the following coding categories are changed, you must\n\
4896 call this function:\n\
4897 coding-category-iso-7, coding-category-iso-7-tight,\n\
4898 coding-category-iso-8-1, coding-category-iso-8-2,\n\
4899 coding-category-iso-7-else, coding-category-iso-8-else")
4904 for (i
= CODING_CATEGORY_IDX_ISO_7
; i
<= CODING_CATEGORY_IDX_ISO_8_ELSE
;
4907 if (! coding_system_table
[i
])
4908 coding_system_table
[i
]
4909 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
4911 (XSYMBOL (XVECTOR (Vcoding_category_table
)->contents
[i
])->value
,
4912 coding_system_table
[i
]);
4920 /*** 8. Post-amble ***/
4926 /* Emacs' internal format specific initialize routine. */
4927 for (i
= 0; i
<= 0x20; i
++)
4928 emacs_code_class
[i
] = EMACS_control_code
;
4929 emacs_code_class
[0x0A] = EMACS_linefeed_code
;
4930 emacs_code_class
[0x0D] = EMACS_carriage_return_code
;
4931 for (i
= 0x21 ; i
< 0x7F; i
++)
4932 emacs_code_class
[i
] = EMACS_ascii_code
;
4933 emacs_code_class
[0x7F] = EMACS_control_code
;
4934 emacs_code_class
[0x80] = EMACS_leading_code_composition
;
4935 for (i
= 0x81; i
< 0xFF; i
++)
4936 emacs_code_class
[i
] = EMACS_invalid_code
;
4937 emacs_code_class
[LEADING_CODE_PRIVATE_11
] = EMACS_leading_code_3
;
4938 emacs_code_class
[LEADING_CODE_PRIVATE_12
] = EMACS_leading_code_3
;
4939 emacs_code_class
[LEADING_CODE_PRIVATE_21
] = EMACS_leading_code_4
;
4940 emacs_code_class
[LEADING_CODE_PRIVATE_22
] = EMACS_leading_code_4
;
4942 /* ISO2022 specific initialize routine. */
4943 for (i
= 0; i
< 0x20; i
++)
4944 iso_code_class
[i
] = ISO_control_code
;
4945 for (i
= 0x21; i
< 0x7F; i
++)
4946 iso_code_class
[i
] = ISO_graphic_plane_0
;
4947 for (i
= 0x80; i
< 0xA0; i
++)
4948 iso_code_class
[i
] = ISO_control_code
;
4949 for (i
= 0xA1; i
< 0xFF; i
++)
4950 iso_code_class
[i
] = ISO_graphic_plane_1
;
4951 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
4952 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
4953 iso_code_class
[ISO_CODE_CR
] = ISO_carriage_return
;
4954 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
4955 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
4956 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
4957 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
4958 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
4959 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
4960 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
4962 conversion_buffer_size
= MINIMUM_CONVERSION_BUFFER_SIZE
;
4963 conversion_buffer
= (char *) xmalloc (MINIMUM_CONVERSION_BUFFER_SIZE
);
4965 setup_coding_system (Qnil
, &keyboard_coding
);
4966 setup_coding_system (Qnil
, &terminal_coding
);
4967 setup_coding_system (Qnil
, &safe_terminal_coding
);
4969 bzero (coding_system_table
, sizeof coding_system_table
);
4971 #if defined (MSDOS) || defined (WINDOWSNT)
4972 system_eol_type
= CODING_EOL_CRLF
;
4974 system_eol_type
= CODING_EOL_LF
;
4982 Qtarget_idx
= intern ("target-idx");
4983 staticpro (&Qtarget_idx
);
4985 Qcoding_system_history
= intern ("coding-system-history");
4986 staticpro (&Qcoding_system_history
);
4987 Fset (Qcoding_system_history
, Qnil
);
4989 /* Target FILENAME is the first argument. */
4990 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
4991 /* Target FILENAME is the third argument. */
4992 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
4994 Qcall_process
= intern ("call-process");
4995 staticpro (&Qcall_process
);
4996 /* Target PROGRAM is the first argument. */
4997 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
4999 Qcall_process_region
= intern ("call-process-region");
5000 staticpro (&Qcall_process_region
);
5001 /* Target PROGRAM is the third argument. */
5002 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
5004 Qstart_process
= intern ("start-process");
5005 staticpro (&Qstart_process
);
5006 /* Target PROGRAM is the third argument. */
5007 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
5009 Qopen_network_stream
= intern ("open-network-stream");
5010 staticpro (&Qopen_network_stream
);
5011 /* Target SERVICE is the fourth argument. */
5012 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
5014 Qcoding_system
= intern ("coding-system");
5015 staticpro (&Qcoding_system
);
5017 Qeol_type
= intern ("eol-type");
5018 staticpro (&Qeol_type
);
5020 Qbuffer_file_coding_system
= intern ("buffer-file-coding-system");
5021 staticpro (&Qbuffer_file_coding_system
);
5023 Qpost_read_conversion
= intern ("post-read-conversion");
5024 staticpro (&Qpost_read_conversion
);
5026 Qpre_write_conversion
= intern ("pre-write-conversion");
5027 staticpro (&Qpre_write_conversion
);
5029 Qno_conversion
= intern ("no-conversion");
5030 staticpro (&Qno_conversion
);
5032 Qundecided
= intern ("undecided");
5033 staticpro (&Qundecided
);
5035 Qcoding_system_p
= intern ("coding-system-p");
5036 staticpro (&Qcoding_system_p
);
5038 Qcoding_system_error
= intern ("coding-system-error");
5039 staticpro (&Qcoding_system_error
);
5041 Fput (Qcoding_system_error
, Qerror_conditions
,
5042 Fcons (Qcoding_system_error
, Fcons (Qerror
, Qnil
)));
5043 Fput (Qcoding_system_error
, Qerror_message
,
5044 build_string ("Invalid coding system"));
5046 Qcoding_category
= intern ("coding-category");
5047 staticpro (&Qcoding_category
);
5048 Qcoding_category_index
= intern ("coding-category-index");
5049 staticpro (&Qcoding_category_index
);
5051 Vcoding_category_table
5052 = Fmake_vector (make_number (CODING_CATEGORY_IDX_MAX
), Qnil
);
5053 staticpro (&Vcoding_category_table
);
5056 for (i
= 0; i
< CODING_CATEGORY_IDX_MAX
; i
++)
5058 XVECTOR (Vcoding_category_table
)->contents
[i
]
5059 = intern (coding_category_name
[i
]);
5060 Fput (XVECTOR (Vcoding_category_table
)->contents
[i
],
5061 Qcoding_category_index
, make_number (i
));
5065 Qcharacter_unification_table
= intern ("character-unification-table");
5066 staticpro (&Qcharacter_unification_table
);
5067 Fput (Qcharacter_unification_table
, Qchar_table_extra_slots
,
5070 Qcharacter_unification_table_for_decode
5071 = intern ("character-unification-table-for-decode");
5072 staticpro (&Qcharacter_unification_table_for_decode
);
5074 Qcharacter_unification_table_for_encode
5075 = intern ("character-unification-table-for-encode");
5076 staticpro (&Qcharacter_unification_table_for_encode
);
5078 Qsafe_charsets
= intern ("safe-charsets");
5079 staticpro (&Qsafe_charsets
);
5081 Qemacs_mule
= intern ("emacs-mule");
5082 staticpro (&Qemacs_mule
);
5084 Qraw_text
= intern ("raw-text");
5085 staticpro (&Qraw_text
);
5087 defsubr (&Scoding_system_p
);
5088 defsubr (&Sread_coding_system
);
5089 defsubr (&Sread_non_nil_coding_system
);
5090 defsubr (&Scheck_coding_system
);
5091 defsubr (&Sdetect_coding_region
);
5092 defsubr (&Sdetect_coding_string
);
5093 defsubr (&Sdecode_coding_region
);
5094 defsubr (&Sencode_coding_region
);
5095 defsubr (&Sdecode_coding_string
);
5096 defsubr (&Sencode_coding_string
);
5097 defsubr (&Sdecode_sjis_char
);
5098 defsubr (&Sencode_sjis_char
);
5099 defsubr (&Sdecode_big5_char
);
5100 defsubr (&Sencode_big5_char
);
5101 defsubr (&Sset_terminal_coding_system_internal
);
5102 defsubr (&Sset_safe_terminal_coding_system_internal
);
5103 defsubr (&Sterminal_coding_system
);
5104 defsubr (&Sset_keyboard_coding_system_internal
);
5105 defsubr (&Skeyboard_coding_system
);
5106 defsubr (&Sfind_operation_coding_system
);
5107 defsubr (&Supdate_iso_coding_systems
);
5109 DEFVAR_LISP ("coding-system-list", &Vcoding_system_list
,
5110 "List of coding systems.\n\
5112 Do not alter the value of this variable manually. This variable should be\n\
5113 updated by the functions `make-coding-system' and\n\
5114 `define-coding-system-alias'.");
5115 Vcoding_system_list
= Qnil
;
5117 DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist
,
5118 "Alist of coding system names.\n\
5119 Each element is one element list of coding system name.\n\
5120 This variable is given to `completing-read' as TABLE argument.\n\
5122 Do not alter the value of this variable manually. This variable should be\n\
5123 updated by the functions `make-coding-system' and\n\
5124 `define-coding-system-alias'.");
5125 Vcoding_system_alist
= Qnil
;
5127 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list
,
5128 "List of coding-categories (symbols) ordered by priority.");
5132 Vcoding_category_list
= Qnil
;
5133 for (i
= CODING_CATEGORY_IDX_MAX
- 1; i
>= 0; i
--)
5134 Vcoding_category_list
5135 = Fcons (XVECTOR (Vcoding_category_table
)->contents
[i
],
5136 Vcoding_category_list
);
5139 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read
,
5140 "Specify the coding system for read operations.\n\
5141 It is useful to bind this variable with `let', but do not set it globally.\n\
5142 If the value is a coding system, it is used for decoding on read operation.\n\
5143 If not, an appropriate element is used from one of the coding system alists:\n\
5144 There are three such tables, `file-coding-system-alist',\n\
5145 `process-coding-system-alist', and `network-coding-system-alist'.");
5146 Vcoding_system_for_read
= Qnil
;
5148 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write
,
5149 "Specify the coding system for write operations.\n\
5150 It is useful to bind this variable with `let', but do not set it globally.\n\
5151 If the value is a coding system, it is used for encoding on write operation.\n\
5152 If not, an appropriate element is used from one of the coding system alists:\n\
5153 There are three such tables, `file-coding-system-alist',\n\
5154 `process-coding-system-alist', and `network-coding-system-alist'.");
5155 Vcoding_system_for_write
= Qnil
;
5157 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used
,
5158 "Coding system used in the latest file or process I/O.");
5159 Vlast_coding_system_used
= Qnil
;
5161 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion
,
5162 "*Non-nil inhibit code conversion of end-of-line format in any cases.");
5163 inhibit_eol_conversion
= 0;
5165 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist
,
5166 "Alist to decide a coding system to use for a file I/O operation.\n\
5167 The format is ((PATTERN . VAL) ...),\n\
5168 where PATTERN is a regular expression matching a file name,\n\
5169 VAL is a coding system, a cons of coding systems, or a function symbol.\n\
5170 If VAL is a coding system, it is used for both decoding and encoding\n\
5171 the file contents.\n\
5172 If VAL is a cons of coding systems, the car part is used for decoding,\n\
5173 and the cdr part is used for encoding.\n\
5174 If VAL is a function symbol, the function must return a coding system\n\
5175 or a cons of coding systems which are used as above.\n\
5177 See also the function `find-operation-coding-system'.");
5178 Vfile_coding_system_alist
= Qnil
;
5180 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist
,
5181 "Alist to decide a coding system to use for a process I/O operation.\n\
5182 The format is ((PATTERN . VAL) ...),\n\
5183 where PATTERN is a regular expression matching a program name,\n\
5184 VAL is a coding system, a cons of coding systems, or a function symbol.\n\
5185 If VAL is a coding system, it is used for both decoding what received\n\
5186 from the program and encoding what sent to the program.\n\
5187 If VAL is a cons of coding systems, the car part is used for decoding,\n\
5188 and the cdr part is used for encoding.\n\
5189 If VAL is a function symbol, the function must return a coding system\n\
5190 or a cons of coding systems which are used as above.\n\
5192 See also the function `find-operation-coding-system'.");
5193 Vprocess_coding_system_alist
= Qnil
;
5195 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist
,
5196 "Alist to decide a coding system to use for a network I/O operation.\n\
5197 The format is ((PATTERN . VAL) ...),\n\
5198 where PATTERN is a regular expression matching a network service name\n\
5199 or is a port number to connect to,\n\
5200 VAL is a coding system, a cons of coding systems, or a function symbol.\n\
5201 If VAL is a coding system, it is used for both decoding what received\n\
5202 from the network stream and encoding what sent to the network stream.\n\
5203 If VAL is a cons of coding systems, the car part is used for decoding,\n\
5204 and the cdr part is used for encoding.\n\
5205 If VAL is a function symbol, the function must return a coding system\n\
5206 or a cons of coding systems which are used as above.\n\
5208 See also the function `find-operation-coding-system'.");
5209 Vnetwork_coding_system_alist
= Qnil
;
5211 DEFVAR_INT ("eol-mnemonic-unix", &eol_mnemonic_unix
,
5212 "Mnemonic character indicating UNIX-like end-of-line format (i.e. LF) .");
5213 eol_mnemonic_unix
= ':';
5215 DEFVAR_INT ("eol-mnemonic-dos", &eol_mnemonic_dos
,
5216 "Mnemonic character indicating DOS-like end-of-line format (i.e. CRLF).");
5217 eol_mnemonic_dos
= '\\';
5219 DEFVAR_INT ("eol-mnemonic-mac", &eol_mnemonic_mac
,
5220 "Mnemonic character indicating MAC-like end-of-line format (i.e. CR).");
5221 eol_mnemonic_mac
= '/';
5223 DEFVAR_INT ("eol-mnemonic-undecided", &eol_mnemonic_undecided
,
5224 "Mnemonic character indicating end-of-line format is not yet decided.");
5225 eol_mnemonic_undecided
= ':';
5227 DEFVAR_LISP ("enable-character-unification", &Venable_character_unification
,
5228 "Non-nil means ISO 2022 encoder/decoder do character unification.");
5229 Venable_character_unification
= Qt
;
5231 DEFVAR_LISP ("standard-character-unification-table-for-decode",
5232 &Vstandard_character_unification_table_for_decode
,
5233 "Table for unifying characters when reading.");
5234 Vstandard_character_unification_table_for_decode
= Qnil
;
5236 DEFVAR_LISP ("standard-character-unification-table-for-encode",
5237 &Vstandard_character_unification_table_for_encode
,
5238 "Table for unifying characters when writing.");
5239 Vstandard_character_unification_table_for_encode
= Qnil
;
5241 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_alist
,
5242 "Alist of charsets vs revision numbers.\n\
5243 While encoding, if a charset (car part of an element) is found,\n\
5244 designate it with the escape sequence identifing revision (cdr part of the element).");
5245 Vcharset_revision_alist
= Qnil
;
5247 DEFVAR_LISP ("default-process-coding-system",
5248 &Vdefault_process_coding_system
,
5249 "Cons of coding systems used for process I/O by default.\n\
5250 The car part is used for decoding a process output,\n\
5251 the cdr part is used for encoding a text to be sent to a process.");
5252 Vdefault_process_coding_system
= Qnil
;
5254 DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table
,
5255 "Table of extra Latin codes in the range 128..159 (inclusive).\n\
5256 This is a vector of length 256.\n\
5257 If Nth element is non-nil, the existence of code N in a file\n\
5258 \(or output of subprocess) doesn't prevent it to be detected as\n\
5259 a coding system of ISO 2022 variant which has a flag\n\
5260 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file\n\
5261 or reading output of a subprocess.\n\
5262 Only 128th through 159th elements has a meaning.");
5263 Vlatin_extra_code_table
= Fmake_vector (make_number (256), Qnil
);
5265 DEFVAR_LISP ("select-safe-coding-system-function",
5266 &Vselect_safe_coding_system_function
,
5267 "Function to call to select safe coding system for encoding a text.\n\
5269 If set, this function is called to force a user to select a proper\n\
5270 coding system which can encode the text in the case that a default\n\
5271 coding system used in each operation can't encode the text.\n\
5273 The default value is `select-safe-codign-system' (which see).");
5274 Vselect_safe_coding_system_function
= Qnil
;