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
29 6. End-of-line handlers
30 7. C library functions
31 8. Emacs Lisp library functions
36 /*** GENERAL NOTE on CODING SYSTEM ***
38 Coding system is an encoding mechanism of one or more character
39 sets. Here's a list of coding systems which Emacs can handle. When
40 we say "decode", it means converting some other coding system to
41 Emacs' internal format (emacs-internal), and when we say "encode",
42 it means converting the coding system emacs-mule to some other
45 0. Emacs' internal format (emacs-mule)
47 Emacs itself holds a multi-lingual character in a buffer and a string
48 in a special format. Details are described in section 2.
52 The most famous coding system for multiple character sets. X's
53 Compound Text, various EUCs (Extended Unix Code), and coding
54 systems used in Internet communication such as ISO-2022-JP are
55 all variants of ISO2022. Details are described in section 3.
57 2. SJIS (or Shift-JIS or MS-Kanji-Code)
59 A coding system to encode character sets: ASCII, JISX0201, and
60 JISX0208. Widely used for PC's in Japan. Details are described in
65 A coding system to encode character sets: ASCII and Big5. Widely
66 used by Chinese (mainly in Taiwan and Hong Kong). Details are
67 described in section 4. In this file, when we write "BIG5"
68 (all uppercase), we mean the coding system, and when we write
69 "Big5" (capitalized), we mean the character set.
73 A coding system for a text containing random 8-bit code. Emacs does
74 no code conversion on such a text except for end-of-line format.
78 If a user wants to read/write a text encoded in a coding system not
79 listed above, he can supply a decoder and an encoder for it in CCL
80 (Code Conversion Language) programs. Emacs executes the CCL program
81 while reading/writing.
83 Emacs represents a coding system by a Lisp symbol that has a property
84 `coding-system'. But, before actually using the coding system, the
85 information about it is set in a structure of type `struct
86 coding_system' for rapid processing. See section 6 for more details.
90 /*** GENERAL NOTES on END-OF-LINE FORMAT ***
92 How end-of-line of a text is encoded depends on a system. For
93 instance, Unix's format is just one byte of `line-feed' code,
94 whereas DOS's format is two-byte sequence of `carriage-return' and
95 `line-feed' codes. MacOS's format is usually one byte of
98 Since text characters encoding and end-of-line encoding are
99 independent, any coding system described above can take
100 any format of end-of-line. So, Emacs has information of format of
101 end-of-line in each coding-system. See section 6 for more details.
105 /*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
107 These functions check if a text between SRC and SRC_END is encoded
108 in the coding system category XXX. Each returns an integer value in
109 which appropriate flag bits for the category XXX is set. The flag
110 bits are defined in macros CODING_CATEGORY_MASK_XXX. Below is the
111 template of these functions. */
114 detect_coding_emacs_mule (src
, src_end
)
115 unsigned char *src
, *src_end
;
121 /*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
123 These functions decode SRC_BYTES length text at SOURCE encoded in
124 CODING to Emacs' internal format (emacs-mule). The resulting text
125 goes to a place pointed to by DESTINATION, the length of which
126 should not exceed DST_BYTES. These functions set the information of
127 original and decoded texts in the members produced, produced_char,
128 consumed, and consumed_char of the structure *CODING.
130 The return value is an integer (CODING_FINISH_XXX) indicating how
131 the decoding finished.
133 DST_BYTES zero means that source area and destination area are
134 overlapped, which means that we can produce a decoded text until it
135 reaches at the head of not-yet-decoded source text.
137 Below is a template of these functions. */
139 decode_coding_XXX (coding
, source
, destination
, src_bytes
, dst_bytes
)
140 struct coding_system
*coding
;
141 unsigned char *source
, *destination
;
142 int src_bytes
, dst_bytes
;
148 /*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
150 These functions encode SRC_BYTES length text at SOURCE of Emacs'
151 internal format (emacs-mule) to CODING. The resulting text goes to
152 a place pointed to by DESTINATION, the length of which should not
153 exceed DST_BYTES. These functions set the information of
154 original and encoded texts in the members produced, produced_char,
155 consumed, and consumed_char of the structure *CODING.
157 The return value is an integer (CODING_FINISH_XXX) indicating how
158 the encoding finished.
160 DST_BYTES zero means that source area and destination area are
161 overlapped, which means that we can produce a decoded text until it
162 reaches at the head of not-yet-decoded source text.
164 Below is a template of these functions. */
166 encode_coding_XXX (coding
, source
, destination
, src_bytes
, dst_bytes
)
167 struct coding_system
*coding
;
168 unsigned char *source
, *destination
;
169 int src_bytes
, dst_bytes
;
175 /*** COMMONLY USED MACROS ***/
177 /* The following three macros ONE_MORE_BYTE, TWO_MORE_BYTES, and
178 THREE_MORE_BYTES safely get one, two, and three bytes from the
179 source text respectively. If there are not enough bytes in the
180 source, they jump to `label_end_of_loop'. The caller should set
181 variables `src' and `src_end' to appropriate areas in advance. */
183 #define ONE_MORE_BYTE(c1) \
188 goto label_end_of_loop; \
191 #define TWO_MORE_BYTES(c1, c2) \
193 if (src + 1 < src_end) \
194 c1 = *src++, c2 = *src++; \
196 goto label_end_of_loop; \
199 #define THREE_MORE_BYTES(c1, c2, c3) \
201 if (src + 2 < src_end) \
202 c1 = *src++, c2 = *src++, c3 = *src++; \
204 goto label_end_of_loop; \
207 /* The following three macros DECODE_CHARACTER_ASCII,
208 DECODE_CHARACTER_DIMENSION1, and DECODE_CHARACTER_DIMENSION2 put
209 the multi-byte form of a character of each class at the place
210 pointed by `dst'. The caller should set the variable `dst' to
211 point to an appropriate area and the variable `coding' to point to
212 the coding-system of the currently decoding text in advance. */
214 /* Decode one ASCII character C. */
216 #define DECODE_CHARACTER_ASCII(c) \
218 if (COMPOSING_P (coding->composing)) \
219 *dst++ = 0xA0, *dst++ = (c) | 0x80; \
223 coding->produced_char++; \
227 /* Decode one DIMENSION1 character whose charset is CHARSET and whose
228 position-code is C. */
230 #define DECODE_CHARACTER_DIMENSION1(charset, c) \
232 unsigned char leading_code = CHARSET_LEADING_CODE_BASE (charset); \
233 if (COMPOSING_P (coding->composing)) \
234 *dst++ = leading_code + 0x20; \
237 *dst++ = leading_code; \
238 coding->produced_char++; \
240 if (leading_code = CHARSET_LEADING_CODE_EXT (charset)) \
241 *dst++ = leading_code; \
242 *dst++ = (c) | 0x80; \
245 /* Decode one DIMENSION2 character whose charset is CHARSET and whose
246 position-codes are C1 and C2. */
248 #define DECODE_CHARACTER_DIMENSION2(charset, c1, c2) \
250 DECODE_CHARACTER_DIMENSION1 (charset, c1); \
251 *dst++ = (c2) | 0x80; \
255 /*** 1. Preamble ***/
269 #else /* not emacs */
273 #endif /* not emacs */
275 Lisp_Object Qcoding_system
, Qeol_type
;
276 Lisp_Object Qbuffer_file_coding_system
;
277 Lisp_Object Qpost_read_conversion
, Qpre_write_conversion
;
278 Lisp_Object Qno_conversion
, Qundecided
;
279 Lisp_Object Qcoding_system_history
;
280 Lisp_Object Qsafe_charsets
;
281 Lisp_Object Qvalid_codes
;
283 extern Lisp_Object Qinsert_file_contents
, Qwrite_region
;
284 Lisp_Object Qcall_process
, Qcall_process_region
, Qprocess_argument
;
285 Lisp_Object Qstart_process
, Qopen_network_stream
;
286 Lisp_Object Qtarget_idx
;
288 Lisp_Object Vselect_safe_coding_system_function
;
290 /* Mnemonic character of each format of end-of-line. */
291 int eol_mnemonic_unix
, eol_mnemonic_dos
, eol_mnemonic_mac
;
292 /* Mnemonic character to indicate format of end-of-line is not yet
294 int eol_mnemonic_undecided
;
296 /* Format of end-of-line decided by system. This is CODING_EOL_LF on
297 Unix, CODING_EOL_CRLF on DOS/Windows, and CODING_EOL_CR on Mac. */
302 Lisp_Object Vcoding_system_list
, Vcoding_system_alist
;
304 Lisp_Object Qcoding_system_p
, Qcoding_system_error
;
306 /* Coding system emacs-mule and raw-text are for converting only
307 end-of-line format. */
308 Lisp_Object Qemacs_mule
, Qraw_text
;
310 /* Coding-systems are handed between Emacs Lisp programs and C internal
311 routines by the following three variables. */
312 /* Coding-system for reading files and receiving data from process. */
313 Lisp_Object Vcoding_system_for_read
;
314 /* Coding-system for writing files and sending data to process. */
315 Lisp_Object Vcoding_system_for_write
;
316 /* Coding-system actually used in the latest I/O. */
317 Lisp_Object Vlast_coding_system_used
;
319 /* A vector of length 256 which contains information about special
320 Latin codes (especially for dealing with Microsoft codes). */
321 Lisp_Object Vlatin_extra_code_table
;
323 /* Flag to inhibit code conversion of end-of-line format. */
324 int inhibit_eol_conversion
;
326 /* Flag to make buffer-file-coding-system inherit from process-coding. */
327 int inherit_process_coding_system
;
329 /* Coding system to be used to encode text for terminal display. */
330 struct coding_system terminal_coding
;
332 /* Coding system to be used to encode text for terminal display when
333 terminal coding system is nil. */
334 struct coding_system safe_terminal_coding
;
336 /* Coding system of what is sent from terminal keyboard. */
337 struct coding_system keyboard_coding
;
339 /* Default coding system to be used to write a file. */
340 struct coding_system default_buffer_file_coding
;
342 Lisp_Object Vfile_coding_system_alist
;
343 Lisp_Object Vprocess_coding_system_alist
;
344 Lisp_Object Vnetwork_coding_system_alist
;
348 Lisp_Object Qcoding_category
, Qcoding_category_index
;
350 /* List of symbols `coding-category-xxx' ordered by priority. */
351 Lisp_Object Vcoding_category_list
;
353 /* Table of coding categories (Lisp symbols). */
354 Lisp_Object Vcoding_category_table
;
356 /* Table of names of symbol for each coding-category. */
357 char *coding_category_name
[CODING_CATEGORY_IDX_MAX
] = {
358 "coding-category-emacs-mule",
359 "coding-category-sjis",
360 "coding-category-iso-7",
361 "coding-category-iso-7-tight",
362 "coding-category-iso-8-1",
363 "coding-category-iso-8-2",
364 "coding-category-iso-7-else",
365 "coding-category-iso-8-else",
366 "coding-category-ccl",
367 "coding-category-big5",
368 "coding-category-raw-text",
369 "coding-category-binary"
372 /* Table of pointers to coding systems corresponding to each coding
374 struct coding_system
*coding_system_table
[CODING_CATEGORY_IDX_MAX
];
376 /* Table of coding category masks. Nth element is a mask for a coding
377 cateogry of which priority is Nth. */
379 int coding_priorities
[CODING_CATEGORY_IDX_MAX
];
381 /* Flag to tell if we look up translation table on character code
383 Lisp_Object Venable_character_translation
;
384 /* Standard translation table to look up on decoding (reading). */
385 Lisp_Object Vstandard_translation_table_for_decode
;
386 /* Standard translation table to look up on encoding (writing). */
387 Lisp_Object Vstandard_translation_table_for_encode
;
389 Lisp_Object Qtranslation_table
;
390 Lisp_Object Qtranslation_table_id
;
391 Lisp_Object Qtranslation_table_for_decode
;
392 Lisp_Object Qtranslation_table_for_encode
;
394 /* Alist of charsets vs revision number. */
395 Lisp_Object Vcharset_revision_alist
;
397 /* Default coding systems used for process I/O. */
398 Lisp_Object Vdefault_process_coding_system
;
401 /*** 2. Emacs internal format (emacs-mule) handlers ***/
403 /* Emacs' internal format for encoding multiple character sets is a
404 kind of multi-byte encoding, i.e. characters are encoded by
405 variable-length sequences of one-byte codes. ASCII characters
406 and control characters (e.g. `tab', `newline') are represented by
407 one-byte sequences which are their ASCII codes, in the range 0x00
408 through 0x7F. The other characters are represented by a sequence
409 of `base leading-code', optional `extended leading-code', and one
410 or two `position-code's. The length of the sequence is determined
411 by the base leading-code. Leading-code takes the range 0x80
412 through 0x9F, whereas extended leading-code and position-code take
413 the range 0xA0 through 0xFF. See `charset.h' for more details
414 about leading-code and position-code.
416 There's one exception to this rule. Special leading-code
417 `leading-code-composition' denotes that the following several
418 characters should be composed into one character. Leading-codes of
419 components (except for ASCII) are added 0x20. An ASCII character
420 component is represented by a 2-byte sequence of `0xA0' and
421 `ASCII-code + 0x80'. See also the comments in `charset.h' for the
422 details of composite character. Hence, we can summarize the code
425 --- CODE RANGE of Emacs' internal format ---
426 (character set) (range)
428 ELSE (1st byte) 0x80 .. 0x9F
429 (rest bytes) 0xA0 .. 0xFF
430 ---------------------------------------------
434 enum emacs_code_class_type emacs_code_class
[256];
436 /* Go to the next statement only if *SRC is accessible and the code is
437 greater than 0xA0. */
438 #define CHECK_CODE_RANGE_A0_FF \
440 if (src >= src_end) \
441 goto label_end_of_switch; \
442 else if (*src++ < 0xA0) \
446 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
447 Check if a text is encoded in Emacs' internal format. If it is,
448 return CODING_CATEGORY_MASK_EMACS_MULE, else return 0. */
451 detect_coding_emacs_mule (src
, src_end
)
452 unsigned char *src
, *src_end
;
457 while (src
< src_end
)
469 switch (emacs_code_class
[c
])
471 case EMACS_ascii_code
:
472 case EMACS_linefeed_code
:
475 case EMACS_control_code
:
476 if (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
480 case EMACS_invalid_code
:
483 case EMACS_leading_code_composition
: /* c == 0x80 */
485 CHECK_CODE_RANGE_A0_FF
;
490 case EMACS_leading_code_4
:
491 CHECK_CODE_RANGE_A0_FF
;
492 /* fall down to check it two more times ... */
494 case EMACS_leading_code_3
:
495 CHECK_CODE_RANGE_A0_FF
;
496 /* fall down to check it one more time ... */
498 case EMACS_leading_code_2
:
499 CHECK_CODE_RANGE_A0_FF
;
507 return CODING_CATEGORY_MASK_EMACS_MULE
;
511 /*** 3. ISO2022 handlers ***/
513 /* The following note describes the coding system ISO2022 briefly.
514 Since the intention of this note is to help in understanding of
515 the programs in this file, some parts are NOT ACCURATE or OVERLY
516 SIMPLIFIED. For the thorough understanding, please refer to the
517 original document of ISO2022.
519 ISO2022 provides many mechanisms to encode several character sets
520 in 7-bit and 8-bit environment. If one chooses 7-bite environment,
521 all text is encoded by codes of less than 128. This may make the
522 encoded text a little bit longer, but the text gets more stability
523 to pass through several gateways (some of them strip off the MSB).
525 There are two kinds of character set: control character set and
526 graphic character set. The former contains control characters such
527 as `newline' and `escape' to provide control functions (control
528 functions are provided also by escape sequences). The latter
529 contains graphic characters such as ' A' and '-'. Emacs recognizes
530 two control character sets and many graphic character sets.
532 Graphic character sets are classified into one of the following
533 four classes, DIMENSION1_CHARS94, DIMENSION1_CHARS96,
534 DIMENSION2_CHARS94, DIMENSION2_CHARS96 according to the number of
535 bytes (DIMENSION) and the number of characters in one dimension
536 (CHARS) of the set. In addition, each character set is assigned an
537 identification tag (called "final character" and denoted as <F>
538 here after) which is unique in each class. <F> of each character
539 set is decided by ECMA(*) when it is registered in ISO. Code range
540 of <F> is 0x30..0x7F (0x30..0x3F are for private use only).
542 Note (*): ECMA = European Computer Manufacturers Association
544 Here are examples of graphic character set [NAME(<F>)]:
545 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
546 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
547 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
548 o DIMENSION2_CHARS96 -- none for the moment
550 A code area (1byte=8bits) is divided into 4 areas, C0, GL, C1, and GR.
551 C0 [0x00..0x1F] -- control character plane 0
552 GL [0x20..0x7F] -- graphic character plane 0
553 C1 [0x80..0x9F] -- control character plane 1
554 GR [0xA0..0xFF] -- graphic character plane 1
556 A control character set is directly designated and invoked to C0 or
557 C1 by an escape sequence. The most common case is that ISO646's
558 control character set is designated/invoked to C0 and ISO6429's
559 control character set is designated/invoked to C1, and usually
560 these designations/invocations are omitted in a coded text. With
561 7-bit environment, only C0 can be used, and a control character for
562 C1 is encoded by an appropriate escape sequence to fit in the
563 environment. All control characters for C1 are defined the
564 corresponding escape sequences.
566 A graphic character set is at first designated to one of four
567 graphic registers (G0 through G3), then these graphic registers are
568 invoked to GL or GR. These designations and invocations can be
569 done independently. The most common case is that G0 is invoked to
570 GL, G1 is invoked to GR, and ASCII is designated to G0, and usually
571 these invocations and designations are omitted in a coded text.
572 With 7-bit environment, only GL can be used.
574 When a graphic character set of CHARS94 is invoked to GL, code 0x20
575 and 0x7F of GL area work as control characters SPACE and DEL
576 respectively, and code 0xA0 and 0xFF of GR area should not be used.
578 There are two ways of invocation: locking-shift and single-shift.
579 With locking-shift, the invocation lasts until the next different
580 invocation, whereas with single-shift, the invocation works only
581 for the following character and doesn't affect locking-shift.
582 Invocations are done by the following control characters or escape
585 ----------------------------------------------------------------------
586 function control char escape sequence description
587 ----------------------------------------------------------------------
588 SI (shift-in) 0x0F none invoke G0 to GL
589 SO (shift-out) 0x0E none invoke G1 to GL
590 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
591 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
592 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 into GL
593 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 into GL
594 ----------------------------------------------------------------------
595 The first four are for locking-shift. Control characters for these
596 functions are defined by macros ISO_CODE_XXX in `coding.h'.
598 Designations are done by the following escape sequences.
599 ----------------------------------------------------------------------
600 escape sequence description
601 ----------------------------------------------------------------------
602 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
603 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
604 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
605 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
606 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
607 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
608 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
609 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
610 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
611 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
612 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
613 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
614 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
615 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
616 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
617 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
618 ----------------------------------------------------------------------
620 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
621 of dimension 1, chars 94, and final character <F>, and etc.
623 Note (*): Although these designations are not allowed in ISO2022,
624 Emacs accepts them on decoding, and produces them on encoding
625 CHARS96 character set in a coding system which is characterized as
626 7-bit environment, non-locking-shift, and non-single-shift.
628 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
629 '(' can be omitted. We call this as "short-form" here after.
631 Now you may notice that there are a lot of ways for encoding the
632 same multilingual text in ISO2022. Actually, there exists many
633 coding systems such as Compound Text (used in X's inter client
634 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
635 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
636 localized platforms), and all of these are variants of ISO2022.
638 In addition to the above, Emacs handles two more kinds of escape
639 sequences: ISO6429's direction specification and Emacs' private
640 sequence for specifying character composition.
642 ISO6429's direction specification takes the following format:
643 o CSI ']' -- end of the current direction
644 o CSI '0' ']' -- end of the current direction
645 o CSI '1' ']' -- start of left-to-right text
646 o CSI '2' ']' -- start of right-to-left text
647 The control character CSI (0x9B: control sequence introducer) is
648 abbreviated to the escape sequence ESC '[' in 7-bit environment.
650 Character composition specification takes the following format:
651 o ESC '0' -- start character composition
652 o ESC '1' -- end character composition
653 Since these are not standard escape sequences of any ISO, the use
654 of them for these meaning is restricted to Emacs only. */
656 enum iso_code_class_type iso_code_class
[256];
658 #define CHARSET_OK(idx, charset) \
659 (coding_system_table[idx] \
660 && (coding_system_table[idx]->safe_charsets[charset] \
661 || (CODING_SPEC_ISO_REQUESTED_DESIGNATION \
662 (coding_system_table[idx], charset) \
663 != CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION)))
665 #define SHIFT_OUT_OK(idx) \
666 (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding_system_table[idx], 1) >= 0)
668 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
669 Check if a text is encoded in ISO2022. If it is, returns an
670 integer in which appropriate flag bits any of:
671 CODING_CATEGORY_MASK_ISO_7
672 CODING_CATEGORY_MASK_ISO_7_TIGHT
673 CODING_CATEGORY_MASK_ISO_8_1
674 CODING_CATEGORY_MASK_ISO_8_2
675 CODING_CATEGORY_MASK_ISO_7_ELSE
676 CODING_CATEGORY_MASK_ISO_8_ELSE
677 are set. If a code which should never appear in ISO2022 is found,
681 detect_coding_iso2022 (src
, src_end
)
682 unsigned char *src
, *src_end
;
684 int mask
= CODING_CATEGORY_MASK_ISO
;
686 int reg
[4], shift_out
= 0, single_shifting
= 0;
687 int c
, c1
, i
, charset
;
689 reg
[0] = CHARSET_ASCII
, reg
[1] = reg
[2] = reg
[3] = -1;
690 while (mask
&& src
< src_end
)
700 if (c
>= '(' && c
<= '/')
702 /* Designation sequence for a charset of dimension 1. */
706 if (c1
< ' ' || c1
>= 0x80
707 || (charset
= iso_charset_table
[0][c
>= ','][c1
]) < 0)
708 /* Invalid designation sequence. Just ignore. */
710 reg
[(c
- '(') % 4] = charset
;
714 /* Designation sequence for a charset of dimension 2. */
718 if (c
>= '@' && c
<= 'B')
719 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
720 reg
[0] = charset
= iso_charset_table
[1][0][c
];
721 else if (c
>= '(' && c
<= '/')
726 if (c1
< ' ' || c1
>= 0x80
727 || (charset
= iso_charset_table
[1][c
>= ','][c1
]) < 0)
728 /* Invalid designation sequence. Just ignore. */
730 reg
[(c
- '(') % 4] = charset
;
733 /* Invalid designation sequence. Just ignore. */
736 else if (c
== 'N' || c
== 'n')
740 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_7_ELSE
)
741 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_8_ELSE
)))
743 /* Locking shift out. */
744 mask
&= ~CODING_CATEGORY_MASK_ISO_7BIT
;
745 mask_found
|= CODING_CATEGORY_MASK_ISO_SHIFT
;
750 else if (c
== 'O' || c
== 'o')
754 /* Locking shift in. */
755 mask
&= ~CODING_CATEGORY_MASK_ISO_7BIT
;
756 mask_found
|= CODING_CATEGORY_MASK_ISO_SHIFT
;
761 else if (c
== '0' || c
== '1' || c
== '2')
762 /* Start/end composition. Just ignore. */
765 /* Invalid escape sequence. Just ignore. */
768 /* We found a valid designation sequence for CHARSET. */
769 mask
&= ~CODING_CATEGORY_MASK_ISO_8BIT
;
770 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7
, charset
))
771 mask_found
|= CODING_CATEGORY_MASK_ISO_7
;
773 mask
&= ~CODING_CATEGORY_MASK_ISO_7
;
774 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7_TIGHT
, charset
))
775 mask_found
|= CODING_CATEGORY_MASK_ISO_7_TIGHT
;
777 mask
&= ~CODING_CATEGORY_MASK_ISO_7_TIGHT
;
778 if (! CHARSET_OK (CODING_CATEGORY_IDX_ISO_7_ELSE
, charset
))
779 mask
&= ~CODING_CATEGORY_MASK_ISO_7_ELSE
;
780 if (! CHARSET_OK (CODING_CATEGORY_IDX_ISO_8_ELSE
, charset
))
781 mask
&= ~CODING_CATEGORY_MASK_ISO_8_ELSE
;
788 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_7_ELSE
)
789 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_8_ELSE
)))
791 /* Locking shift out. */
792 mask
&= ~CODING_CATEGORY_MASK_ISO_7BIT
;
793 mask_found
|= CODING_CATEGORY_MASK_ISO_SHIFT
;
801 /* Locking shift in. */
802 mask
&= ~CODING_CATEGORY_MASK_ISO_7BIT
;
803 mask_found
|= CODING_CATEGORY_MASK_ISO_SHIFT
;
812 int newmask
= CODING_CATEGORY_MASK_ISO_8_ELSE
;
814 if (c
!= ISO_CODE_CSI
)
816 if (coding_system_table
[CODING_CATEGORY_IDX_ISO_8_1
]->flags
817 & CODING_FLAG_ISO_SINGLE_SHIFT
)
818 newmask
|= CODING_CATEGORY_MASK_ISO_8_1
;
819 if (coding_system_table
[CODING_CATEGORY_IDX_ISO_8_2
]->flags
820 & CODING_FLAG_ISO_SINGLE_SHIFT
)
821 newmask
|= CODING_CATEGORY_MASK_ISO_8_2
;
824 if (VECTORP (Vlatin_extra_code_table
)
825 && !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
;
835 mask_found
|= newmask
;
848 if (VECTORP (Vlatin_extra_code_table
)
849 && !NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
853 if (coding_system_table
[CODING_CATEGORY_IDX_ISO_8_1
]->flags
854 & CODING_FLAG_ISO_LATIN_EXTRA
)
855 newmask
|= CODING_CATEGORY_MASK_ISO_8_1
;
856 if (coding_system_table
[CODING_CATEGORY_IDX_ISO_8_2
]->flags
857 & CODING_FLAG_ISO_LATIN_EXTRA
)
858 newmask
|= CODING_CATEGORY_MASK_ISO_8_2
;
860 mask_found
|= newmask
;
867 unsigned char *src_begin
= src
;
869 mask
&= ~(CODING_CATEGORY_MASK_ISO_7BIT
870 | CODING_CATEGORY_MASK_ISO_7_ELSE
);
871 mask_found
|= CODING_CATEGORY_MASK_ISO_8_1
;
872 /* Check the length of succeeding codes of the range
873 0xA0..0FF. If the byte length is odd, we exclude
874 CODING_CATEGORY_MASK_ISO_8_2. We can check this only
875 when we are not single shifting. */
876 if (!single_shifting
)
878 while (src
< src_end
&& *src
>= 0xA0)
880 if ((src
- src_begin
- 1) & 1 && src
< src_end
)
881 mask
&= ~CODING_CATEGORY_MASK_ISO_8_2
;
883 mask_found
|= CODING_CATEGORY_MASK_ISO_8_2
;
890 return (mask
& mask_found
);
893 /* Decode a character of which charset is CHARSET and the 1st position
894 code is C1. If dimension of CHARSET is 2, the 2nd position code is
895 fetched from SRC and set to C2. If CHARSET is negative, it means
896 that we are decoding ill formed text, and what we can do is just to
899 #define DECODE_ISO_CHARACTER(charset, c1) \
901 int c_alt, charset_alt = (charset); \
902 if (COMPOSING_HEAD_P (coding->composing)) \
904 *dst++ = LEADING_CODE_COMPOSITION; \
905 if (COMPOSING_WITH_RULE_P (coding->composing)) \
906 /* To tell composition rules are embeded. */ \
908 coding->composing += 2; \
910 if (charset_alt >= 0) \
912 if (CHARSET_DIMENSION (charset_alt) == 2) \
914 ONE_MORE_BYTE (c2); \
915 if (iso_code_class[(c2) & 0x7F] != ISO_0x20_or_0x7F \
916 && iso_code_class[(c2) & 0x7F] != ISO_graphic_plane_0) \
919 charset_alt = CHARSET_ASCII; \
922 if (!NILP (translation_table) \
923 && ((c_alt = translate_char (translation_table, \
924 -1, charset_alt, c1, c2)) >= 0)) \
925 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
927 if (charset_alt == CHARSET_ASCII || charset_alt < 0) \
928 DECODE_CHARACTER_ASCII (c1); \
929 else if (CHARSET_DIMENSION (charset_alt) == 1) \
930 DECODE_CHARACTER_DIMENSION1 (charset_alt, c1); \
932 DECODE_CHARACTER_DIMENSION2 (charset_alt, c1, c2); \
933 if (COMPOSING_WITH_RULE_P (coding->composing)) \
934 /* To tell a composition rule follows. */ \
935 coding->composing = COMPOSING_WITH_RULE_RULE; \
938 /* Set designation state into CODING. */
939 #define DECODE_DESIGNATION(reg, dimension, chars, final_char) \
941 int charset = ISO_CHARSET_TABLE (make_number (dimension), \
942 make_number (chars), \
943 make_number (final_char)); \
945 && (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) == reg \
946 || coding->safe_charsets[charset])) \
948 if (coding->spec.iso2022.last_invalid_designation_register == 0 \
950 && charset == CHARSET_ASCII) \
952 /* We should insert this designation sequence as is so \
953 that it is surely written back to a file. */ \
954 coding->spec.iso2022.last_invalid_designation_register = -1; \
955 goto label_invalid_code; \
957 coding->spec.iso2022.last_invalid_designation_register = -1; \
958 if ((coding->mode & CODING_MODE_DIRECTION) \
959 && CHARSET_REVERSE_CHARSET (charset) >= 0) \
960 charset = CHARSET_REVERSE_CHARSET (charset); \
961 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
965 coding->spec.iso2022.last_invalid_designation_register = reg; \
966 goto label_invalid_code; \
970 /* Check if the current composing sequence contains only valid codes.
971 If the composing sequence doesn't end before SRC_END, return -1.
972 Else, if it contains only valid codes, return 0.
973 Else return the length of the composing sequence. */
976 check_composing_code (coding
, src
, src_end
)
977 struct coding_system
*coding
;
978 unsigned char *src
, *src_end
;
980 unsigned char *src_start
= src
;
981 int invalid_code_found
= 0;
982 int charset
, c
, c1
, dim
;
984 while (src
< src_end
)
986 if (*src
++ != ISO_CODE_ESC
) continue;
987 if (src
>= src_end
) break;
988 if ((c
= *src
++) == '1') /* end of compsition */
989 return (invalid_code_found
? src
- src_start
: 0);
990 if (src
+ 2 >= src_end
) break;
991 if (!coding
->flags
& CODING_FLAG_ISO_DESIGNATION
)
992 invalid_code_found
= 1;
999 c
= (*src
>= '@' && *src
<= 'B') ? '(' : *src
++;
1001 if (c
>= '(' && c
<= '/')
1004 if ((c1
< ' ' || c1
>= 0x80)
1005 || (charset
= iso_charset_table
[dim
][c
>= ','][c1
]) < 0
1006 || ! coding
->safe_charsets
[charset
]
1007 || (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
1008 == CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
))
1009 invalid_code_found
= 1;
1012 invalid_code_found
= 1;
1015 return (invalid_code_found
1017 : (coding
->mode
& CODING_MODE_LAST_BLOCK
? 0 : -1));
1020 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
1023 decode_coding_iso2022 (coding
, source
, destination
, src_bytes
, dst_bytes
)
1024 struct coding_system
*coding
;
1025 unsigned char *source
, *destination
;
1026 int src_bytes
, dst_bytes
;
1028 unsigned char *src
= source
;
1029 unsigned char *src_end
= source
+ src_bytes
;
1030 unsigned char *dst
= destination
;
1031 unsigned char *dst_end
= destination
+ dst_bytes
;
1032 /* Since the maximum bytes produced by each loop is 7, we subtract 6
1033 from DST_END to assure that overflow checking is necessary only
1034 at the head of loop. */
1035 unsigned char *adjusted_dst_end
= dst_end
- 6;
1037 /* Charsets invoked to graphic plane 0 and 1 respectively. */
1038 int charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
1039 int charset1
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 1);
1040 Lisp_Object translation_table
1041 = coding
->translation_table_for_decode
;
1042 int result
= CODING_FINISH_NORMAL
;
1044 if (!NILP (Venable_character_translation
) && NILP (translation_table
))
1045 translation_table
= Vstandard_translation_table_for_decode
;
1047 coding
->produced_char
= 0;
1048 coding
->fake_multibyte
= 0;
1049 while (src
< src_end
&& (dst_bytes
1050 ? (dst
< adjusted_dst_end
)
1053 /* SRC_BASE remembers the start position in source in each loop.
1054 The loop will be exited when there's not enough source text
1055 to analyze long escape sequence or 2-byte code (within macros
1056 ONE_MORE_BYTE or TWO_MORE_BYTES). In that case, SRC is reset
1057 to SRC_BASE before exiting. */
1058 unsigned char *src_base
= src
;
1059 int c1
= *src
++, c2
;
1061 switch (iso_code_class
[c1
])
1063 case ISO_0x20_or_0x7F
:
1064 if (!coding
->composing
1065 && (charset0
< 0 || CHARSET_CHARS (charset0
) == 94))
1067 /* This is SPACE or DEL. */
1069 coding
->produced_char
++;
1072 /* This is a graphic character, we fall down ... */
1074 case ISO_graphic_plane_0
:
1075 if (coding
->composing
== COMPOSING_WITH_RULE_RULE
)
1077 /* This is a composition rule. */
1079 coding
->composing
= COMPOSING_WITH_RULE_TAIL
;
1082 DECODE_ISO_CHARACTER (charset0
, c1
);
1085 case ISO_0xA0_or_0xFF
:
1086 if (charset1
< 0 || CHARSET_CHARS (charset1
) == 94
1087 || coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
)
1088 goto label_invalid_code
;
1089 /* This is a graphic character, we fall down ... */
1091 case ISO_graphic_plane_1
:
1092 if (coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
)
1093 goto label_invalid_code
;
1095 DECODE_ISO_CHARACTER (charset1
, c1
);
1098 case ISO_control_code
:
1099 /* All ISO2022 control characters in this class have the
1100 same representation in Emacs internal format. */
1102 && (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
)
1103 && (coding
->eol_type
== CODING_EOL_CR
1104 || coding
->eol_type
== CODING_EOL_CRLF
))
1106 result
= CODING_FINISH_INCONSISTENT_EOL
;
1107 goto label_end_of_loop_2
;
1110 coding
->produced_char
++;
1113 case ISO_carriage_return
:
1114 if (coding
->eol_type
== CODING_EOL_CR
)
1116 else if (coding
->eol_type
== CODING_EOL_CRLF
)
1119 if (c1
== ISO_CODE_LF
)
1123 if (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
)
1125 result
= CODING_FINISH_INCONSISTENT_EOL
;
1126 goto label_end_of_loop_2
;
1134 coding
->produced_char
++;
1138 if (! (coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
)
1139 || CODING_SPEC_ISO_DESIGNATION (coding
, 1) < 0)
1140 goto label_invalid_code
;
1141 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 1;
1142 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
1146 if (! (coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
))
1147 goto label_invalid_code
;
1148 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 0;
1149 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
1152 case ISO_single_shift_2_7
:
1153 case ISO_single_shift_2
:
1154 if (! (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
))
1155 goto label_invalid_code
;
1156 /* SS2 is handled as an escape sequence of ESC 'N' */
1158 goto label_escape_sequence
;
1160 case ISO_single_shift_3
:
1161 if (! (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
))
1162 goto label_invalid_code
;
1163 /* SS2 is handled as an escape sequence of ESC 'O' */
1165 goto label_escape_sequence
;
1167 case ISO_control_sequence_introducer
:
1168 /* CSI is handled as an escape sequence of ESC '[' ... */
1170 goto label_escape_sequence
;
1174 label_escape_sequence
:
1175 /* Escape sequences handled by Emacs are invocation,
1176 designation, direction specification, and character
1177 composition specification. */
1180 case '&': /* revision of following character set */
1182 if (!(c1
>= '@' && c1
<= '~'))
1183 goto label_invalid_code
;
1185 if (c1
!= ISO_CODE_ESC
)
1186 goto label_invalid_code
;
1188 goto label_escape_sequence
;
1190 case '$': /* designation of 2-byte character set */
1191 if (! (coding
->flags
& CODING_FLAG_ISO_DESIGNATION
))
1192 goto label_invalid_code
;
1194 if (c1
>= '@' && c1
<= 'B')
1195 { /* designation of JISX0208.1978, GB2312.1980,
1197 DECODE_DESIGNATION (0, 2, 94, c1
);
1199 else if (c1
>= 0x28 && c1
<= 0x2B)
1200 { /* designation of DIMENSION2_CHARS94 character set */
1202 DECODE_DESIGNATION (c1
- 0x28, 2, 94, c2
);
1204 else if (c1
>= 0x2C && c1
<= 0x2F)
1205 { /* designation of DIMENSION2_CHARS96 character set */
1207 DECODE_DESIGNATION (c1
- 0x2C, 2, 96, c2
);
1210 goto label_invalid_code
;
1213 case 'n': /* invocation of locking-shift-2 */
1214 if (! (coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
)
1215 || CODING_SPEC_ISO_DESIGNATION (coding
, 2) < 0)
1216 goto label_invalid_code
;
1217 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 2;
1218 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
1221 case 'o': /* invocation of locking-shift-3 */
1222 if (! (coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
)
1223 || CODING_SPEC_ISO_DESIGNATION (coding
, 3) < 0)
1224 goto label_invalid_code
;
1225 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 3;
1226 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
1229 case 'N': /* invocation of single-shift-2 */
1230 if (! (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
1231 || CODING_SPEC_ISO_DESIGNATION (coding
, 2) < 0)
1232 goto label_invalid_code
;
1234 charset
= CODING_SPEC_ISO_DESIGNATION (coding
, 2);
1235 DECODE_ISO_CHARACTER (charset
, c1
);
1238 case 'O': /* invocation of single-shift-3 */
1239 if (! (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
1240 || CODING_SPEC_ISO_DESIGNATION (coding
, 3) < 0)
1241 goto label_invalid_code
;
1243 charset
= CODING_SPEC_ISO_DESIGNATION (coding
, 3);
1244 DECODE_ISO_CHARACTER (charset
, c1
);
1247 case '0': case '2': /* start composing */
1248 /* Before processing composing, we must be sure that all
1249 characters being composed are supported by CODING.
1250 If not, we must give up composing and insert the
1251 bunch of codes for composing as is without decoding. */
1255 result1
= check_composing_code (coding
, src
, src_end
);
1258 coding
->composing
= (c1
== '0'
1259 ? COMPOSING_NO_RULE_HEAD
1260 : COMPOSING_WITH_RULE_HEAD
);
1261 coding
->produced_char
++;
1263 else if (result1
> 0)
1265 if (result1
+ 2 < (dst_bytes
? dst_end
: src_base
) - dst
)
1267 bcopy (src_base
, dst
, result1
+ 2);
1270 coding
->produced_char
+= result1
+ 2;
1274 result
= CODING_FINISH_INSUFFICIENT_DST
;
1275 goto label_end_of_loop_2
;
1279 goto label_end_of_loop
;
1283 case '1': /* end composing */
1284 coding
->composing
= COMPOSING_NO
;
1287 case '[': /* specification of direction */
1288 if (coding
->flags
& CODING_FLAG_ISO_NO_DIRECTION
)
1289 goto label_invalid_code
;
1290 /* For the moment, nested direction is not supported.
1291 So, `coding->mode & CODING_MODE_DIRECTION' zero means
1292 left-to-right, and nozero means right-to-left. */
1296 case ']': /* end of the current direction */
1297 coding
->mode
&= ~CODING_MODE_DIRECTION
;
1299 case '0': /* end of the current direction */
1300 case '1': /* start of left-to-right direction */
1303 coding
->mode
&= ~CODING_MODE_DIRECTION
;
1305 goto label_invalid_code
;
1308 case '2': /* start of right-to-left direction */
1311 coding
->mode
|= CODING_MODE_DIRECTION
;
1313 goto label_invalid_code
;
1317 goto label_invalid_code
;
1322 if (! (coding
->flags
& CODING_FLAG_ISO_DESIGNATION
))
1323 goto label_invalid_code
;
1324 if (c1
>= 0x28 && c1
<= 0x2B)
1325 { /* designation of DIMENSION1_CHARS94 character set */
1327 DECODE_DESIGNATION (c1
- 0x28, 1, 94, c2
);
1329 else if (c1
>= 0x2C && c1
<= 0x2F)
1330 { /* designation of DIMENSION1_CHARS96 character set */
1332 DECODE_DESIGNATION (c1
- 0x2C, 1, 96, c2
);
1336 goto label_invalid_code
;
1339 /* We must update these variables now. */
1340 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
1341 charset1
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 1);
1345 while (src_base
< src
)
1346 *dst
++ = *src_base
++;
1347 coding
->fake_multibyte
= 1;
1352 result
= CODING_FINISH_INSUFFICIENT_SRC
;
1353 label_end_of_loop_2
:
1360 if (result
== CODING_FINISH_NORMAL
)
1361 result
= CODING_FINISH_INSUFFICIENT_DST
;
1362 else if (result
!= CODING_FINISH_INCONSISTENT_EOL
1363 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
1365 /* This is the last block of the text to be decoded. We had
1366 better just flush out all remaining codes in the text
1367 although they are not valid characters. */
1368 src_bytes
= src_end
- src
;
1369 if (dst_bytes
&& (dst_end
- dst
< src_bytes
))
1370 src_bytes
= dst_end
- dst
;
1371 bcopy (src
, dst
, src_bytes
);
1374 coding
->fake_multibyte
= 1;
1378 coding
->consumed
= coding
->consumed_char
= src
- source
;
1379 coding
->produced
= dst
- destination
;
1383 /* ISO2022 encoding stuff. */
1386 It is not enough to say just "ISO2022" on encoding, we have to
1387 specify more details. In Emacs, each coding system of ISO2022
1388 variant has the following specifications:
1389 1. Initial designation to G0 thru G3.
1390 2. Allows short-form designation?
1391 3. ASCII should be designated to G0 before control characters?
1392 4. ASCII should be designated to G0 at end of line?
1393 5. 7-bit environment or 8-bit environment?
1394 6. Use locking-shift?
1395 7. Use Single-shift?
1396 And the following two are only for Japanese:
1397 8. Use ASCII in place of JIS0201-1976-Roman?
1398 9. Use JISX0208-1983 in place of JISX0208-1978?
1399 These specifications are encoded in `coding->flags' as flag bits
1400 defined by macros CODING_FLAG_ISO_XXX. See `coding.h' for more
1404 /* Produce codes (escape sequence) for designating CHARSET to graphic
1405 register REG. If <final-char> of CHARSET is '@', 'A', or 'B' and
1406 the coding system CODING allows, produce designation sequence of
1409 #define ENCODE_DESIGNATION(charset, reg, coding) \
1411 unsigned char final_char = CHARSET_ISO_FINAL_CHAR (charset); \
1412 char *intermediate_char_94 = "()*+"; \
1413 char *intermediate_char_96 = ",-./"; \
1414 int revision = CODING_SPEC_ISO_REVISION_NUMBER(coding, charset); \
1415 if (revision < 255) \
1417 *dst++ = ISO_CODE_ESC; \
1419 *dst++ = '@' + revision; \
1421 *dst++ = ISO_CODE_ESC; \
1422 if (CHARSET_DIMENSION (charset) == 1) \
1424 if (CHARSET_CHARS (charset) == 94) \
1425 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
1427 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
1432 if (CHARSET_CHARS (charset) == 94) \
1434 if (! (coding->flags & CODING_FLAG_ISO_SHORT_FORM) \
1436 || final_char < '@' || final_char > 'B') \
1437 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
1440 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
1442 *dst++ = final_char; \
1443 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
1446 /* The following two macros produce codes (control character or escape
1447 sequence) for ISO2022 single-shift functions (single-shift-2 and
1450 #define ENCODE_SINGLE_SHIFT_2 \
1452 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1453 *dst++ = ISO_CODE_ESC, *dst++ = 'N'; \
1456 *dst++ = ISO_CODE_SS2; \
1457 coding->fake_multibyte = 1; \
1459 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
1462 #define ENCODE_SINGLE_SHIFT_3 \
1464 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1465 *dst++ = ISO_CODE_ESC, *dst++ = 'O'; \
1468 *dst++ = ISO_CODE_SS3; \
1469 coding->fake_multibyte = 1; \
1471 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
1474 /* The following four macros produce codes (control character or
1475 escape sequence) for ISO2022 locking-shift functions (shift-in,
1476 shift-out, locking-shift-2, and locking-shift-3). */
1478 #define ENCODE_SHIFT_IN \
1480 *dst++ = ISO_CODE_SI; \
1481 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0; \
1484 #define ENCODE_SHIFT_OUT \
1486 *dst++ = ISO_CODE_SO; \
1487 CODING_SPEC_ISO_INVOCATION (coding, 0) = 1; \
1490 #define ENCODE_LOCKING_SHIFT_2 \
1492 *dst++ = ISO_CODE_ESC, *dst++ = 'n'; \
1493 CODING_SPEC_ISO_INVOCATION (coding, 0) = 2; \
1496 #define ENCODE_LOCKING_SHIFT_3 \
1498 *dst++ = ISO_CODE_ESC, *dst++ = 'o'; \
1499 CODING_SPEC_ISO_INVOCATION (coding, 0) = 3; \
1502 /* Produce codes for a DIMENSION1 character whose character set is
1503 CHARSET and whose position-code is C1. Designation and invocation
1504 sequences are also produced in advance if necessary. */
1507 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
1509 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
1511 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1512 *dst++ = c1 & 0x7F; \
1514 *dst++ = c1 | 0x80; \
1515 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
1518 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
1520 *dst++ = c1 & 0x7F; \
1523 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
1525 *dst++ = c1 | 0x80; \
1528 else if (coding->flags & CODING_FLAG_ISO_SAFE \
1529 && !coding->safe_charsets[charset]) \
1531 /* We should not encode this character, instead produce one or \
1533 *dst++ = CODING_INHIBIT_CHARACTER_SUBSTITUTION; \
1534 if (CHARSET_WIDTH (charset) == 2) \
1535 *dst++ = CODING_INHIBIT_CHARACTER_SUBSTITUTION; \
1539 /* Since CHARSET is not yet invoked to any graphic planes, we \
1540 must invoke it, or, at first, designate it to some graphic \
1541 register. Then repeat the loop to actually produce the \
1543 dst = encode_invocation_designation (charset, coding, dst); \
1546 /* Produce codes for a DIMENSION2 character whose character set is
1547 CHARSET and whose position-codes are C1 and C2. Designation and
1548 invocation codes are also produced in advance if necessary. */
1550 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
1552 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
1554 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1555 *dst++ = c1 & 0x7F, *dst++ = c2 & 0x7F; \
1557 *dst++ = c1 | 0x80, *dst++ = c2 | 0x80; \
1558 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
1561 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
1563 *dst++ = c1 & 0x7F, *dst++= c2 & 0x7F; \
1566 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
1568 *dst++ = c1 | 0x80, *dst++= c2 | 0x80; \
1571 else if (coding->flags & CODING_FLAG_ISO_SAFE \
1572 && !coding->safe_charsets[charset]) \
1574 /* We should not encode this character, instead produce one or \
1576 *dst++ = CODING_INHIBIT_CHARACTER_SUBSTITUTION; \
1577 if (CHARSET_WIDTH (charset) == 2) \
1578 *dst++ = CODING_INHIBIT_CHARACTER_SUBSTITUTION; \
1582 /* Since CHARSET is not yet invoked to any graphic planes, we \
1583 must invoke it, or, at first, designate it to some graphic \
1584 register. Then repeat the loop to actually produce the \
1586 dst = encode_invocation_designation (charset, coding, dst); \
1589 #define ENCODE_ISO_CHARACTER(charset, c1, c2) \
1591 int c_alt, charset_alt; \
1592 if (!NILP (translation_table) \
1593 && ((c_alt = translate_char (translation_table, -1, \
1596 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
1598 charset_alt = charset; \
1599 if (CHARSET_DIMENSION (charset_alt) == 1) \
1601 if (charset == CHARSET_ASCII \
1602 && coding->flags & CODING_FLAG_ISO_USE_ROMAN) \
1603 charset_alt = charset_latin_jisx0201; \
1604 ENCODE_ISO_CHARACTER_DIMENSION1 (charset_alt, c1); \
1608 if (charset == charset_jisx0208 \
1609 && coding->flags & CODING_FLAG_ISO_USE_OLDJIS) \
1610 charset_alt = charset_jisx0208_1978; \
1611 ENCODE_ISO_CHARACTER_DIMENSION2 (charset_alt, c1, c2); \
1613 if (! COMPOSING_P (coding->composing)) \
1614 coding->consumed_char++; \
1617 /* Produce designation and invocation codes at a place pointed by DST
1618 to use CHARSET. The element `spec.iso2022' of *CODING is updated.
1622 encode_invocation_designation (charset
, coding
, dst
)
1624 struct coding_system
*coding
;
1627 int reg
; /* graphic register number */
1629 /* At first, check designations. */
1630 for (reg
= 0; reg
< 4; reg
++)
1631 if (charset
== CODING_SPEC_ISO_DESIGNATION (coding
, reg
))
1636 /* CHARSET is not yet designated to any graphic registers. */
1637 /* At first check the requested designation. */
1638 reg
= CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
);
1639 if (reg
== CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
)
1640 /* Since CHARSET requests no special designation, designate it
1641 to graphic register 0. */
1644 ENCODE_DESIGNATION (charset
, reg
, coding
);
1647 if (CODING_SPEC_ISO_INVOCATION (coding
, 0) != reg
1648 && CODING_SPEC_ISO_INVOCATION (coding
, 1) != reg
)
1650 /* Since the graphic register REG is not invoked to any graphic
1651 planes, invoke it to graphic plane 0. */
1654 case 0: /* graphic register 0 */
1658 case 1: /* graphic register 1 */
1662 case 2: /* graphic register 2 */
1663 if (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
1664 ENCODE_SINGLE_SHIFT_2
;
1666 ENCODE_LOCKING_SHIFT_2
;
1669 case 3: /* graphic register 3 */
1670 if (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
1671 ENCODE_SINGLE_SHIFT_3
;
1673 ENCODE_LOCKING_SHIFT_3
;
1680 /* The following two macros produce codes for indicating composition. */
1681 #define ENCODE_COMPOSITION_NO_RULE_START *dst++ = ISO_CODE_ESC, *dst++ = '0'
1682 #define ENCODE_COMPOSITION_WITH_RULE_START *dst++ = ISO_CODE_ESC, *dst++ = '2'
1683 #define ENCODE_COMPOSITION_END *dst++ = ISO_CODE_ESC, *dst++ = '1'
1685 /* The following three macros produce codes for indicating direction
1687 #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
1689 if (coding->flags == CODING_FLAG_ISO_SEVEN_BITS) \
1690 *dst++ = ISO_CODE_ESC, *dst++ = '['; \
1692 *dst++ = ISO_CODE_CSI; \
1695 #define ENCODE_DIRECTION_R2L \
1696 ENCODE_CONTROL_SEQUENCE_INTRODUCER, *dst++ = '2', *dst++ = ']'
1698 #define ENCODE_DIRECTION_L2R \
1699 ENCODE_CONTROL_SEQUENCE_INTRODUCER, *dst++ = '0', *dst++ = ']'
1701 /* Produce codes for designation and invocation to reset the graphic
1702 planes and registers to initial state. */
1703 #define ENCODE_RESET_PLANE_AND_REGISTER \
1706 if (CODING_SPEC_ISO_INVOCATION (coding, 0) != 0) \
1708 for (reg = 0; reg < 4; reg++) \
1709 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg) >= 0 \
1710 && (CODING_SPEC_ISO_DESIGNATION (coding, reg) \
1711 != CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg))) \
1712 ENCODE_DESIGNATION \
1713 (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg), reg, coding); \
1716 /* Produce designation sequences of charsets in the line started from
1717 SRC to a place pointed by *DSTP, and update DSTP.
1719 If the current block ends before any end-of-line, we may fail to
1720 find all the necessary designations. */
1723 encode_designation_at_bol (coding
, table
, src
, src_end
, dstp
)
1724 struct coding_system
*coding
;
1726 unsigned char *src
, *src_end
, **dstp
;
1728 int charset
, c
, found
= 0, reg
;
1729 /* Table of charsets to be designated to each graphic register. */
1731 unsigned char *dst
= *dstp
;
1733 for (reg
= 0; reg
< 4; reg
++)
1736 while (src
< src_end
&& *src
!= '\n' && found
< 4)
1738 int bytes
= BYTES_BY_CHAR_HEAD (*src
);
1741 charset
= CHARSET_AT (src
);
1745 unsigned char c1
, c2
;
1747 SPLIT_STRING(src
, bytes
, charset
, c1
, c2
);
1748 if ((c_alt
= translate_char (table
, -1, charset
, c1
, c2
)) >= 0)
1749 charset
= CHAR_CHARSET (c_alt
);
1752 reg
= CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
);
1753 if (reg
!= CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
&& r
[reg
] < 0)
1764 for (reg
= 0; reg
< 4; reg
++)
1766 && CODING_SPEC_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
1767 ENCODE_DESIGNATION (r
[reg
], reg
, coding
);
1772 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
1775 encode_coding_iso2022 (coding
, source
, destination
, src_bytes
, dst_bytes
)
1776 struct coding_system
*coding
;
1777 unsigned char *source
, *destination
;
1778 int src_bytes
, dst_bytes
;
1780 unsigned char *src
= source
;
1781 unsigned char *src_end
= source
+ src_bytes
;
1782 unsigned char *dst
= destination
;
1783 unsigned char *dst_end
= destination
+ dst_bytes
;
1784 /* Since the maximum bytes produced by each loop is 20, we subtract 19
1785 from DST_END to assure overflow checking is necessary only at the
1787 unsigned char *adjusted_dst_end
= dst_end
- 19;
1788 Lisp_Object translation_table
1789 = coding
->translation_table_for_encode
;
1790 int result
= CODING_FINISH_NORMAL
;
1792 if (!NILP (Venable_character_translation
) && NILP (translation_table
))
1793 translation_table
= Vstandard_translation_table_for_encode
;
1795 coding
->consumed_char
= 0;
1796 coding
->fake_multibyte
= 0;
1797 while (src
< src_end
&& (dst_bytes
1798 ? (dst
< adjusted_dst_end
)
1799 : (dst
< src
- 19)))
1801 /* SRC_BASE remembers the start position in source in each loop.
1802 The loop will be exited when there's not enough source text
1803 to analyze multi-byte codes (within macros ONE_MORE_BYTE,
1804 TWO_MORE_BYTES, and THREE_MORE_BYTES). In that case, SRC is
1805 reset to SRC_BASE before exiting. */
1806 unsigned char *src_base
= src
;
1807 int charset
, c1
, c2
, c3
, c4
;
1809 if (coding
->flags
& CODING_FLAG_ISO_DESIGNATE_AT_BOL
1810 && CODING_SPEC_ISO_BOL (coding
))
1812 /* We have to produce designation sequences if any now. */
1813 encode_designation_at_bol (coding
, translation_table
,
1814 src
, src_end
, &dst
);
1815 CODING_SPEC_ISO_BOL (coding
) = 0;
1819 /* If we are seeing a component of a composite character, we are
1820 seeing a leading-code encoded irregularly for composition, or
1821 a composition rule if composing with rule. We must set C1 to
1822 a normal leading-code or an ASCII code. If we are not seeing
1823 a composite character, we must reset composition,
1824 designation, and invocation states. */
1825 if (COMPOSING_P (coding
->composing
))
1829 /* We are not in a composite character any longer. */
1830 coding
->composing
= COMPOSING_NO
;
1831 ENCODE_RESET_PLANE_AND_REGISTER
;
1832 ENCODE_COMPOSITION_END
;
1836 if (coding
->composing
== COMPOSING_WITH_RULE_RULE
)
1839 coding
->composing
= COMPOSING_WITH_RULE_HEAD
;
1842 else if (coding
->composing
== COMPOSING_WITH_RULE_HEAD
)
1843 coding
->composing
= COMPOSING_WITH_RULE_RULE
;
1846 /* This is an ASCII component. */
1851 /* This is a leading-code of non ASCII component. */
1856 /* Now encode one character. C1 is a control character, an
1857 ASCII character, or a leading-code of multi-byte character. */
1858 switch (emacs_code_class
[c1
])
1860 case EMACS_ascii_code
:
1861 ENCODE_ISO_CHARACTER (CHARSET_ASCII
, c1
, /* dummy */ c2
);
1864 case EMACS_control_code
:
1865 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_CNTL
)
1866 ENCODE_RESET_PLANE_AND_REGISTER
;
1868 coding
->consumed_char
++;
1871 case EMACS_carriage_return_code
:
1872 if (! (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
1874 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_CNTL
)
1875 ENCODE_RESET_PLANE_AND_REGISTER
;
1877 coding
->consumed_char
++;
1880 /* fall down to treat '\r' as '\n' ... */
1882 case EMACS_linefeed_code
:
1883 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_EOL
)
1884 ENCODE_RESET_PLANE_AND_REGISTER
;
1885 if (coding
->flags
& CODING_FLAG_ISO_INIT_AT_BOL
)
1886 bcopy (coding
->spec
.iso2022
.initial_designation
,
1887 coding
->spec
.iso2022
.current_designation
,
1888 sizeof coding
->spec
.iso2022
.initial_designation
);
1889 if (coding
->eol_type
== CODING_EOL_LF
1890 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
1891 *dst
++ = ISO_CODE_LF
;
1892 else if (coding
->eol_type
== CODING_EOL_CRLF
)
1893 *dst
++ = ISO_CODE_CR
, *dst
++ = ISO_CODE_LF
;
1895 *dst
++ = ISO_CODE_CR
;
1896 CODING_SPEC_ISO_BOL (coding
) = 1;
1897 coding
->consumed_char
++;
1900 case EMACS_leading_code_2
:
1904 /* invalid sequence */
1907 coding
->consumed_char
++;
1910 ENCODE_ISO_CHARACTER (c1
, c2
, /* dummy */ c3
);
1913 case EMACS_leading_code_3
:
1914 TWO_MORE_BYTES (c2
, c3
);
1915 if (c2
< 0xA0 || c3
< 0xA0)
1917 /* invalid sequence */
1920 coding
->consumed_char
++;
1922 else if (c1
< LEADING_CODE_PRIVATE_11
)
1923 ENCODE_ISO_CHARACTER (c1
, c2
, c3
);
1925 ENCODE_ISO_CHARACTER (c2
, c3
, /* dummy */ c4
);
1928 case EMACS_leading_code_4
:
1929 THREE_MORE_BYTES (c2
, c3
, c4
);
1930 if (c2
< 0xA0 || c3
< 0xA0 || c4
< 0xA0)
1932 /* invalid sequence */
1935 coding
->consumed_char
++;
1938 ENCODE_ISO_CHARACTER (c2
, c3
, c4
);
1941 case EMACS_leading_code_composition
:
1945 /* invalid sequence */
1948 coding
->consumed_char
++;
1950 else if (c2
== 0xFF)
1952 ENCODE_RESET_PLANE_AND_REGISTER
;
1953 coding
->composing
= COMPOSING_WITH_RULE_HEAD
;
1954 ENCODE_COMPOSITION_WITH_RULE_START
;
1955 coding
->consumed_char
++;
1959 ENCODE_RESET_PLANE_AND_REGISTER
;
1960 /* Rewind one byte because it is a character code of
1961 composition elements. */
1963 coding
->composing
= COMPOSING_NO_RULE_HEAD
;
1964 ENCODE_COMPOSITION_NO_RULE_START
;
1965 coding
->consumed_char
++;
1969 case EMACS_invalid_code
:
1971 coding
->consumed_char
++;
1976 result
= CODING_FINISH_INSUFFICIENT_SRC
;
1981 if (src
< src_end
&& result
== CODING_FINISH_NORMAL
)
1982 result
= CODING_FINISH_INSUFFICIENT_DST
;
1984 /* If this is the last block of the text to be encoded, we must
1985 reset graphic planes and registers to the initial state, and
1986 flush out the carryover if any. */
1987 if (coding
->mode
& CODING_MODE_LAST_BLOCK
)
1989 ENCODE_RESET_PLANE_AND_REGISTER
;
1990 if (COMPOSING_P (coding
->composing
))
1991 ENCODE_COMPOSITION_END
;
1993 coding
->consumed
= src
- source
;
1994 coding
->produced
= coding
->produced_char
= dst
- destination
;
1999 /*** 4. SJIS and BIG5 handlers ***/
2001 /* Although SJIS and BIG5 are not ISO's coding system, they are used
2002 quite widely. So, for the moment, Emacs supports them in the bare
2003 C code. But, in the future, they may be supported only by CCL. */
2005 /* SJIS is a coding system encoding three character sets: ASCII, right
2006 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
2007 as is. A character of charset katakana-jisx0201 is encoded by
2008 "position-code + 0x80". A character of charset japanese-jisx0208
2009 is encoded in 2-byte but two position-codes are divided and shifted
2010 so that it fit in the range below.
2012 --- CODE RANGE of SJIS ---
2013 (character set) (range)
2015 KATAKANA-JISX0201 0xA0 .. 0xDF
2016 JISX0208 (1st byte) 0x80 .. 0x9F and 0xE0 .. 0xEF
2017 (2nd byte) 0x40 .. 0xFF
2018 -------------------------------
2022 /* BIG5 is a coding system encoding two character sets: ASCII and
2023 Big5. An ASCII character is encoded as is. Big5 is a two-byte
2024 character set and is encoded in two-byte.
2026 --- CODE RANGE of BIG5 ---
2027 (character set) (range)
2029 Big5 (1st byte) 0xA1 .. 0xFE
2030 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
2031 --------------------------
2033 Since the number of characters in Big5 is larger than maximum
2034 characters in Emacs' charset (96x96), it can't be handled as one
2035 charset. So, in Emacs, Big5 is divided into two: `charset-big5-1'
2036 and `charset-big5-2'. Both are DIMENSION2 and CHARS94. The former
2037 contains frequently used characters and the latter contains less
2038 frequently used characters. */
2040 /* Macros to decode or encode a character of Big5 in BIG5. B1 and B2
2041 are the 1st and 2nd position-codes of Big5 in BIG5 coding system.
2042 C1 and C2 are the 1st and 2nd position-codes of of Emacs' internal
2043 format. CHARSET is `charset_big5_1' or `charset_big5_2'. */
2045 /* Number of Big5 characters which have the same code in 1st byte. */
2046 #define BIG5_SAME_ROW (0xFF - 0xA1 + 0x7F - 0x40)
2048 #define DECODE_BIG5(b1, b2, charset, c1, c2) \
2051 = (b1 - 0xA1) * BIG5_SAME_ROW + b2 - (b2 < 0x7F ? 0x40 : 0x62); \
2053 charset = charset_big5_1; \
2056 charset = charset_big5_2; \
2057 temp -= (0xC9 - 0xA1) * BIG5_SAME_ROW; \
2059 c1 = temp / (0xFF - 0xA1) + 0x21; \
2060 c2 = temp % (0xFF - 0xA1) + 0x21; \
2063 #define ENCODE_BIG5(charset, c1, c2, b1, b2) \
2065 unsigned int temp = (c1 - 0x21) * (0xFF - 0xA1) + (c2 - 0x21); \
2066 if (charset == charset_big5_2) \
2067 temp += BIG5_SAME_ROW * (0xC9 - 0xA1); \
2068 b1 = temp / BIG5_SAME_ROW + 0xA1; \
2069 b2 = temp % BIG5_SAME_ROW; \
2070 b2 += b2 < 0x3F ? 0x40 : 0x62; \
2073 #define DECODE_SJIS_BIG5_CHARACTER(charset, c1, c2) \
2075 int c_alt, charset_alt = (charset); \
2076 if (!NILP (translation_table) \
2077 && ((c_alt = translate_char (translation_table, \
2078 -1, (charset), c1, c2)) >= 0)) \
2079 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
2080 if (charset_alt == CHARSET_ASCII || charset_alt < 0) \
2081 DECODE_CHARACTER_ASCII (c1); \
2082 else if (CHARSET_DIMENSION (charset_alt) == 1) \
2083 DECODE_CHARACTER_DIMENSION1 (charset_alt, c1); \
2085 DECODE_CHARACTER_DIMENSION2 (charset_alt, c1, c2); \
2088 #define ENCODE_SJIS_BIG5_CHARACTER(charset, c1, c2) \
2090 int c_alt, charset_alt; \
2091 if (!NILP (translation_table) \
2092 && ((c_alt = translate_char (translation_table, -1, \
2095 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
2097 charset_alt = charset; \
2098 if (charset_alt == charset_ascii) \
2100 else if (CHARSET_DIMENSION (charset_alt) == 1) \
2102 if (sjis_p && charset_alt == charset_katakana_jisx0201) \
2106 *dst++ = charset_alt, *dst++ = c1; \
2107 coding->fake_multibyte = 1; \
2112 c1 &= 0x7F, c2 &= 0x7F; \
2113 if (sjis_p && charset_alt == charset_jisx0208) \
2115 unsigned char s1, s2; \
2117 ENCODE_SJIS (c1, c2, s1, s2); \
2118 *dst++ = s1, *dst++ = s2; \
2119 coding->fake_multibyte = 1; \
2122 && (charset_alt == charset_big5_1 \
2123 || charset_alt == charset_big5_2)) \
2125 unsigned char b1, b2; \
2127 ENCODE_BIG5 (charset_alt, c1, c2, b1, b2); \
2128 *dst++ = b1, *dst++ = b2; \
2132 *dst++ = charset_alt, *dst++ = c1, *dst++ = c2; \
2133 coding->fake_multibyte = 1; \
2136 coding->consumed_char++; \
2139 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2140 Check if a text is encoded in SJIS. If it is, return
2141 CODING_CATEGORY_MASK_SJIS, else return 0. */
2144 detect_coding_sjis (src
, src_end
)
2145 unsigned char *src
, *src_end
;
2149 while (src
< src_end
)
2152 if ((c
>= 0x80 && c
< 0xA0) || c
>= 0xE0)
2154 if (src
< src_end
&& *src
++ < 0x40)
2158 return CODING_CATEGORY_MASK_SJIS
;
2161 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2162 Check if a text is encoded in BIG5. If it is, return
2163 CODING_CATEGORY_MASK_BIG5, else return 0. */
2166 detect_coding_big5 (src
, src_end
)
2167 unsigned char *src
, *src_end
;
2171 while (src
< src_end
)
2179 if (c
< 0x40 || (c
>= 0x7F && c
<= 0xA0))
2183 return CODING_CATEGORY_MASK_BIG5
;
2186 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
2187 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
2190 decode_coding_sjis_big5 (coding
, source
, destination
,
2191 src_bytes
, dst_bytes
, sjis_p
)
2192 struct coding_system
*coding
;
2193 unsigned char *source
, *destination
;
2194 int src_bytes
, dst_bytes
;
2197 unsigned char *src
= source
;
2198 unsigned char *src_end
= source
+ src_bytes
;
2199 unsigned char *dst
= destination
;
2200 unsigned char *dst_end
= destination
+ dst_bytes
;
2201 /* Since the maximum bytes produced by each loop is 4, we subtract 3
2202 from DST_END to assure overflow checking is necessary only at the
2204 unsigned char *adjusted_dst_end
= dst_end
- 3;
2205 Lisp_Object translation_table
2206 = coding
->translation_table_for_decode
;
2207 int result
= CODING_FINISH_NORMAL
;
2209 if (!NILP (Venable_character_translation
) && NILP (translation_table
))
2210 translation_table
= Vstandard_translation_table_for_decode
;
2212 coding
->produced_char
= 0;
2213 coding
->fake_multibyte
= 0;
2214 while (src
< src_end
&& (dst_bytes
2215 ? (dst
< adjusted_dst_end
)
2218 /* SRC_BASE remembers the start position in source in each loop.
2219 The loop will be exited when there's not enough source text
2220 to analyze two-byte character (within macro ONE_MORE_BYTE).
2221 In that case, SRC is reset to SRC_BASE before exiting. */
2222 unsigned char *src_base
= src
;
2223 unsigned char c1
= *src
++, c2
, c3
, c4
;
2229 if (coding
->eol_type
== CODING_EOL_CRLF
)
2234 else if (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
)
2236 result
= CODING_FINISH_INCONSISTENT_EOL
;
2237 goto label_end_of_loop_2
;
2240 /* To process C2 again, SRC is subtracted by 1. */
2243 else if (coding
->eol_type
== CODING_EOL_CR
)
2249 && (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
)
2250 && (coding
->eol_type
== CODING_EOL_CR
2251 || coding
->eol_type
== CODING_EOL_CRLF
))
2253 result
= CODING_FINISH_INCONSISTENT_EOL
;
2254 goto label_end_of_loop_2
;
2258 coding
->produced_char
++;
2261 DECODE_SJIS_BIG5_CHARACTER (charset_ascii
, c1
, /* dummy */ c2
);
2266 if (c1
< 0xA0 || (c1
>= 0xE0 && c1
< 0xF0))
2268 /* SJIS -> JISX0208 */
2272 DECODE_SJIS (c1
, c2
, c3
, c4
);
2273 DECODE_SJIS_BIG5_CHARACTER (charset_jisx0208
, c3
, c4
);
2276 goto label_invalid_code_2
;
2279 /* SJIS -> JISX0201-Kana */
2280 DECODE_SJIS_BIG5_CHARACTER (charset_katakana_jisx0201
, c1
,
2283 goto label_invalid_code_1
;
2288 if (c1
>= 0xA1 && c1
<= 0xFE)
2291 if ((c2
>= 0x40 && c2
<= 0x7E) || (c2
>= 0xA1 && c2
<= 0xFE))
2295 DECODE_BIG5 (c1
, c2
, charset
, c3
, c4
);
2296 DECODE_SJIS_BIG5_CHARACTER (charset
, c3
, c4
);
2299 goto label_invalid_code_2
;
2302 goto label_invalid_code_1
;
2307 label_invalid_code_1
:
2309 coding
->produced_char
++;
2310 coding
->fake_multibyte
= 1;
2313 label_invalid_code_2
:
2314 *dst
++ = c1
; *dst
++= c2
;
2315 coding
->produced_char
+= 2;
2316 coding
->fake_multibyte
= 1;
2320 result
= CODING_FINISH_INSUFFICIENT_SRC
;
2321 label_end_of_loop_2
:
2328 if (result
== CODING_FINISH_NORMAL
)
2329 result
= CODING_FINISH_INSUFFICIENT_DST
;
2330 else if (result
!= CODING_FINISH_INCONSISTENT_EOL
2331 && coding
->mode
& CODING_MODE_LAST_BLOCK
)
2333 src_bytes
= src_end
- src
;
2334 if (dst_bytes
&& (dst_end
- dst
< src_bytes
))
2335 src_bytes
= dst_end
- dst
;
2336 bcopy (dst
, src
, src_bytes
);
2339 coding
->fake_multibyte
= 1;
2343 coding
->consumed
= coding
->consumed_char
= src
- source
;
2344 coding
->produced
= dst
- destination
;
2348 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
2349 This function can encode `charset_ascii', `charset_katakana_jisx0201',
2350 `charset_jisx0208', `charset_big5_1', and `charset_big5-2'. We are
2351 sure that all these charsets are registered as official charset
2352 (i.e. do not have extended leading-codes). Characters of other
2353 charsets are produced without any encoding. If SJIS_P is 1, encode
2354 SJIS text, else encode BIG5 text. */
2357 encode_coding_sjis_big5 (coding
, source
, destination
,
2358 src_bytes
, dst_bytes
, sjis_p
)
2359 struct coding_system
*coding
;
2360 unsigned char *source
, *destination
;
2361 int src_bytes
, dst_bytes
;
2364 unsigned char *src
= source
;
2365 unsigned char *src_end
= source
+ src_bytes
;
2366 unsigned char *dst
= destination
;
2367 unsigned char *dst_end
= destination
+ dst_bytes
;
2368 /* Since the maximum bytes produced by each loop is 2, we subtract 1
2369 from DST_END to assure overflow checking is necessary only at the
2371 unsigned char *adjusted_dst_end
= dst_end
- 1;
2372 Lisp_Object translation_table
2373 = coding
->translation_table_for_encode
;
2374 int result
= CODING_FINISH_NORMAL
;
2376 if (!NILP (Venable_character_translation
) && NILP (translation_table
))
2377 translation_table
= Vstandard_translation_table_for_encode
;
2379 coding
->consumed_char
= 0;
2380 coding
->fake_multibyte
= 0;
2381 while (src
< src_end
&& (dst_bytes
2382 ? (dst
< adjusted_dst_end
)
2385 /* SRC_BASE remembers the start position in source in each loop.
2386 The loop will be exited when there's not enough source text
2387 to analyze multi-byte codes (within macros ONE_MORE_BYTE and
2388 TWO_MORE_BYTES). In that case, SRC is reset to SRC_BASE
2390 unsigned char *src_base
= src
;
2391 unsigned char c1
= *src
++, c2
, c3
, c4
;
2393 if (coding
->composing
)
2400 else if (c1
>= 0xA0)
2403 coding
->composing
= 0;
2406 switch (emacs_code_class
[c1
])
2408 case EMACS_ascii_code
:
2409 ENCODE_SJIS_BIG5_CHARACTER (charset_ascii
, c1
, /* dummy */ c2
);
2412 case EMACS_control_code
:
2414 coding
->consumed_char
++;
2417 case EMACS_carriage_return_code
:
2418 if (! (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
2421 coding
->consumed_char
++;
2424 /* fall down to treat '\r' as '\n' ... */
2426 case EMACS_linefeed_code
:
2427 if (coding
->eol_type
== CODING_EOL_LF
2428 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
2430 else if (coding
->eol_type
== CODING_EOL_CRLF
)
2431 *dst
++ = '\r', *dst
++ = '\n';
2434 coding
->consumed_char
++;
2437 case EMACS_leading_code_2
:
2439 ENCODE_SJIS_BIG5_CHARACTER (c1
, c2
, /* dummy */ c3
);
2442 case EMACS_leading_code_3
:
2443 TWO_MORE_BYTES (c2
, c3
);
2444 ENCODE_SJIS_BIG5_CHARACTER (c1
, c2
, c3
);
2447 case EMACS_leading_code_4
:
2448 THREE_MORE_BYTES (c2
, c3
, c4
);
2449 ENCODE_SJIS_BIG5_CHARACTER (c2
, c3
, c4
);
2452 case EMACS_leading_code_composition
:
2453 coding
->composing
= 1;
2456 default: /* i.e. case EMACS_invalid_code: */
2458 coding
->consumed_char
++;
2463 result
= CODING_FINISH_INSUFFICIENT_SRC
;
2468 if (result
== CODING_FINISH_NORMAL
2470 result
= CODING_FINISH_INSUFFICIENT_DST
;
2471 coding
->consumed
= src
- source
;
2472 coding
->produced
= coding
->produced_char
= dst
- destination
;
2477 /*** 5. CCL handlers ***/
2479 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2480 Check if a text is encoded in a coding system of which
2481 encoder/decoder are written in CCL program. If it is, return
2482 CODING_CATEGORY_MASK_CCL, else return 0. */
2485 detect_coding_ccl (src
, src_end
)
2486 unsigned char *src
, *src_end
;
2488 unsigned char *valid
;
2490 /* No coding system is assigned to coding-category-ccl. */
2491 if (!coding_system_table
[CODING_CATEGORY_IDX_CCL
])
2494 valid
= coding_system_table
[CODING_CATEGORY_IDX_CCL
]->spec
.ccl
.valid_codes
;
2495 while (src
< src_end
)
2497 if (! valid
[*src
]) return 0;
2500 return CODING_CATEGORY_MASK_CCL
;
2504 /*** 6. End-of-line handlers ***/
2506 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
2507 This function is called only when `coding->eol_type' is
2508 CODING_EOL_CRLF or CODING_EOL_CR. */
2511 decode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
)
2512 struct coding_system
*coding
;
2513 unsigned char *source
, *destination
;
2514 int src_bytes
, dst_bytes
;
2516 unsigned char *src
= source
;
2517 unsigned char *src_end
= source
+ src_bytes
;
2518 unsigned char *dst
= destination
;
2519 unsigned char *dst_end
= destination
+ dst_bytes
;
2521 int result
= CODING_FINISH_NORMAL
;
2523 coding
->fake_multibyte
= 0;
2528 switch (coding
->eol_type
)
2530 case CODING_EOL_CRLF
:
2532 /* Since the maximum bytes produced by each loop is 2, we
2533 subtract 1 from DST_END to assure overflow checking is
2534 necessary only at the head of loop. */
2535 unsigned char *adjusted_dst_end
= dst_end
- 1;
2537 while (src
< src_end
&& (dst_bytes
2538 ? (dst
< adjusted_dst_end
)
2541 unsigned char *src_base
= src
;
2549 if (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
)
2551 result
= CODING_FINISH_INCONSISTENT_EOL
;
2552 goto label_end_of_loop_2
;
2555 if (BASE_LEADING_CODE_P (c
))
2556 coding
->fake_multibyte
= 1;
2561 && (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
))
2563 result
= CODING_FINISH_INCONSISTENT_EOL
;
2564 goto label_end_of_loop_2
;
2569 if (BASE_LEADING_CODE_P (c
))
2570 coding
->fake_multibyte
= 1;
2575 result
= CODING_FINISH_INSUFFICIENT_SRC
;
2576 label_end_of_loop_2
:
2580 if (result
== CODING_FINISH_NORMAL
2582 result
= CODING_FINISH_INSUFFICIENT_DST
;
2587 if (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
)
2589 while (src
< src_end
)
2591 if ((c
= *src
++) == '\n')
2593 if (BASE_LEADING_CODE_P (c
))
2594 coding
->fake_multibyte
= 1;
2598 src_bytes
= src
- source
;
2599 result
= CODING_FINISH_INCONSISTENT_EOL
;
2602 if (dst_bytes
&& src_bytes
> dst_bytes
)
2604 result
= CODING_FINISH_INSUFFICIENT_DST
;
2605 src_bytes
= dst_bytes
;
2608 bcopy (source
, destination
, src_bytes
);
2610 safe_bcopy (source
, destination
, src_bytes
);
2611 src
= source
+ src_bytes
;
2612 while (src_bytes
--) if (*dst
++ == '\r') dst
[-1] = '\n';
2615 default: /* i.e. case: CODING_EOL_LF */
2616 if (dst_bytes
&& src_bytes
> dst_bytes
)
2618 result
= CODING_FINISH_INSUFFICIENT_DST
;
2619 src_bytes
= dst_bytes
;
2622 bcopy (source
, destination
, src_bytes
);
2624 safe_bcopy (source
, destination
, src_bytes
);
2627 coding
->fake_multibyte
= 1;
2631 coding
->consumed
= coding
->consumed_char
= src
- source
;
2632 coding
->produced
= coding
->produced_char
= dst
- destination
;
2636 /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". Encode
2637 format of end-of-line according to `coding->eol_type'. If
2638 `coding->mode & CODING_MODE_SELECTIVE_DISPLAY' is nonzero, code
2639 '\r' in source text also means end-of-line. */
2642 encode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
)
2643 struct coding_system
*coding
;
2644 unsigned char *source
, *destination
;
2645 int src_bytes
, dst_bytes
;
2647 unsigned char *src
= source
;
2648 unsigned char *dst
= destination
;
2649 int result
= CODING_FINISH_NORMAL
;
2651 coding
->fake_multibyte
= 0;
2653 if (coding
->eol_type
== CODING_EOL_CRLF
)
2656 unsigned char *src_end
= source
+ src_bytes
;
2657 unsigned char *dst_end
= destination
+ dst_bytes
;
2658 /* Since the maximum bytes produced by each loop is 2, we
2659 subtract 1 from DST_END to assure overflow checking is
2660 necessary only at the head of loop. */
2661 unsigned char *adjusted_dst_end
= dst_end
- 1;
2663 while (src
< src_end
&& (dst_bytes
2664 ? (dst
< adjusted_dst_end
)
2669 || (c
== '\r' && (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
)))
2670 *dst
++ = '\r', *dst
++ = '\n';
2674 if (BASE_LEADING_CODE_P (c
))
2675 coding
->fake_multibyte
= 1;
2679 result
= CODING_FINISH_INSUFFICIENT_DST
;
2685 if (dst_bytes
&& src_bytes
> dst_bytes
)
2687 src_bytes
= dst_bytes
;
2688 result
= CODING_FINISH_INSUFFICIENT_DST
;
2691 bcopy (source
, destination
, src_bytes
);
2693 safe_bcopy (source
, destination
, src_bytes
);
2694 dst_bytes
= src_bytes
;
2695 if (coding
->eol_type
== CODING_EOL_CR
)
2699 if ((c
= *dst
++) == '\n')
2701 else if (BASE_LEADING_CODE_P (c
))
2702 coding
->fake_multibyte
= 1;
2707 if (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
)
2710 if (*dst
++ == '\r') dst
[-1] = '\n';
2712 coding
->fake_multibyte
= 1;
2714 src
= source
+ dst_bytes
;
2715 dst
= destination
+ dst_bytes
;
2718 coding
->consumed
= coding
->consumed_char
= src
- source
;
2719 coding
->produced
= coding
->produced_char
= dst
- destination
;
2724 /*** 7. C library functions ***/
2726 /* In Emacs Lisp, coding system is represented by a Lisp symbol which
2727 has a property `coding-system'. The value of this property is a
2728 vector of length 5 (called as coding-vector). Among elements of
2729 this vector, the first (element[0]) and the fifth (element[4])
2730 carry important information for decoding/encoding. Before
2731 decoding/encoding, this information should be set in fields of a
2732 structure of type `coding_system'.
2734 A value of property `coding-system' can be a symbol of another
2735 subsidiary coding-system. In that case, Emacs gets coding-vector
2738 `element[0]' contains information to be set in `coding->type'. The
2739 value and its meaning is as follows:
2741 0 -- coding_type_emacs_mule
2742 1 -- coding_type_sjis
2743 2 -- coding_type_iso2022
2744 3 -- coding_type_big5
2745 4 -- coding_type_ccl encoder/decoder written in CCL
2746 nil -- coding_type_no_conversion
2747 t -- coding_type_undecided (automatic conversion on decoding,
2748 no-conversion on encoding)
2750 `element[4]' contains information to be set in `coding->flags' and
2751 `coding->spec'. The meaning varies by `coding->type'.
2753 If `coding->type' is `coding_type_iso2022', element[4] is a vector
2754 of length 32 (of which the first 13 sub-elements are used now).
2755 Meanings of these sub-elements are:
2757 sub-element[N] where N is 0 through 3: to be set in `coding->spec.iso2022'
2758 If the value is an integer of valid charset, the charset is
2759 assumed to be designated to graphic register N initially.
2761 If the value is minus, it is a minus value of charset which
2762 reserves graphic register N, which means that the charset is
2763 not designated initially but should be designated to graphic
2764 register N just before encoding a character in that charset.
2766 If the value is nil, graphic register N is never used on
2769 sub-element[N] where N is 4 through 11: to be set in `coding->flags'
2770 Each value takes t or nil. See the section ISO2022 of
2771 `coding.h' for more information.
2773 If `coding->type' is `coding_type_big5', element[4] is t to denote
2774 BIG5-ETen or nil to denote BIG5-HKU.
2776 If `coding->type' takes the other value, element[4] is ignored.
2778 Emacs Lisp's coding system also carries information about format of
2779 end-of-line in a value of property `eol-type'. If the value is
2780 integer, 0 means CODING_EOL_LF, 1 means CODING_EOL_CRLF, and 2
2781 means CODING_EOL_CR. If it is not integer, it should be a vector
2782 of subsidiary coding systems of which property `eol-type' has one
2787 /* Extract information for decoding/encoding from CODING_SYSTEM_SYMBOL
2788 and set it in CODING. If CODING_SYSTEM_SYMBOL is invalid, CODING
2789 is setup so that no conversion is necessary and return -1, else
2793 setup_coding_system (coding_system
, coding
)
2794 Lisp_Object coding_system
;
2795 struct coding_system
*coding
;
2797 Lisp_Object coding_spec
, coding_type
, eol_type
, plist
;
2801 /* Initialize some fields required for all kinds of coding systems. */
2802 coding
->symbol
= coding_system
;
2803 coding
->common_flags
= 0;
2805 coding
->heading_ascii
= -1;
2806 coding
->post_read_conversion
= coding
->pre_write_conversion
= Qnil
;
2807 coding_spec
= Fget (coding_system
, Qcoding_system
);
2808 if (!VECTORP (coding_spec
)
2809 || XVECTOR (coding_spec
)->size
!= 5
2810 || !CONSP (XVECTOR (coding_spec
)->contents
[3]))
2811 goto label_invalid_coding_system
;
2813 eol_type
= inhibit_eol_conversion
? Qnil
: Fget (coding_system
, Qeol_type
);
2814 if (VECTORP (eol_type
))
2816 coding
->eol_type
= CODING_EOL_UNDECIDED
;
2817 coding
->common_flags
= CODING_REQUIRE_DETECTION_MASK
;
2819 else if (XFASTINT (eol_type
) == 1)
2821 coding
->eol_type
= CODING_EOL_CRLF
;
2822 coding
->common_flags
2823 = CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
;
2825 else if (XFASTINT (eol_type
) == 2)
2827 coding
->eol_type
= CODING_EOL_CR
;
2828 coding
->common_flags
2829 = CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
;
2832 coding
->eol_type
= CODING_EOL_LF
;
2834 coding_type
= XVECTOR (coding_spec
)->contents
[0];
2835 /* Try short cut. */
2836 if (SYMBOLP (coding_type
))
2838 if (EQ (coding_type
, Qt
))
2840 coding
->type
= coding_type_undecided
;
2841 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
2844 coding
->type
= coding_type_no_conversion
;
2848 /* Initialize remaining fields. */
2849 coding
->composing
= 0;
2850 coding
->translation_table_for_decode
= Qnil
;
2851 coding
->translation_table_for_encode
= Qnil
;
2853 /* Get values of coding system properties:
2854 `post-read-conversion', `pre-write-conversion',
2855 `translation-table-for-decode', `translation-table-for-encode'. */
2856 plist
= XVECTOR (coding_spec
)->contents
[3];
2857 coding
->post_read_conversion
= Fplist_get (plist
, Qpost_read_conversion
);
2858 coding
->pre_write_conversion
= Fplist_get (plist
, Qpre_write_conversion
);
2859 val
= Fplist_get (plist
, Qtranslation_table_for_decode
);
2861 val
= Fget (val
, Qtranslation_table_for_decode
);
2862 coding
->translation_table_for_decode
= CHAR_TABLE_P (val
) ? val
: Qnil
;
2863 val
= Fplist_get (plist
, Qtranslation_table_for_encode
);
2865 val
= Fget (val
, Qtranslation_table_for_encode
);
2866 coding
->translation_table_for_encode
= CHAR_TABLE_P (val
) ? val
: Qnil
;
2867 val
= Fplist_get (plist
, Qcoding_category
);
2870 val
= Fget (val
, Qcoding_category_index
);
2872 coding
->category_idx
= XINT (val
);
2874 goto label_invalid_coding_system
;
2877 goto label_invalid_coding_system
;
2879 val
= Fplist_get (plist
, Qsafe_charsets
);
2882 for (i
= 0; i
<= MAX_CHARSET
; i
++)
2883 coding
->safe_charsets
[i
] = 1;
2887 bzero (coding
->safe_charsets
, MAX_CHARSET
+ 1);
2890 if ((i
= get_charset_id (XCONS (val
)->car
)) >= 0)
2891 coding
->safe_charsets
[i
] = 1;
2892 val
= XCONS (val
)->cdr
;
2896 switch (XFASTINT (coding_type
))
2899 coding
->type
= coding_type_emacs_mule
;
2900 if (!NILP (coding
->post_read_conversion
))
2901 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
2902 if (!NILP (coding
->pre_write_conversion
))
2903 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
2907 coding
->type
= coding_type_sjis
;
2908 coding
->common_flags
2909 |= CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
;
2913 coding
->type
= coding_type_iso2022
;
2914 coding
->common_flags
2915 |= CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
;
2917 Lisp_Object val
, temp
;
2919 int i
, charset
, reg_bits
= 0;
2921 val
= XVECTOR (coding_spec
)->contents
[4];
2923 if (!VECTORP (val
) || XVECTOR (val
)->size
!= 32)
2924 goto label_invalid_coding_system
;
2926 flags
= XVECTOR (val
)->contents
;
2928 = ((NILP (flags
[4]) ? 0 : CODING_FLAG_ISO_SHORT_FORM
)
2929 | (NILP (flags
[5]) ? 0 : CODING_FLAG_ISO_RESET_AT_EOL
)
2930 | (NILP (flags
[6]) ? 0 : CODING_FLAG_ISO_RESET_AT_CNTL
)
2931 | (NILP (flags
[7]) ? 0 : CODING_FLAG_ISO_SEVEN_BITS
)
2932 | (NILP (flags
[8]) ? 0 : CODING_FLAG_ISO_LOCKING_SHIFT
)
2933 | (NILP (flags
[9]) ? 0 : CODING_FLAG_ISO_SINGLE_SHIFT
)
2934 | (NILP (flags
[10]) ? 0 : CODING_FLAG_ISO_USE_ROMAN
)
2935 | (NILP (flags
[11]) ? 0 : CODING_FLAG_ISO_USE_OLDJIS
)
2936 | (NILP (flags
[12]) ? 0 : CODING_FLAG_ISO_NO_DIRECTION
)
2937 | (NILP (flags
[13]) ? 0 : CODING_FLAG_ISO_INIT_AT_BOL
)
2938 | (NILP (flags
[14]) ? 0 : CODING_FLAG_ISO_DESIGNATE_AT_BOL
)
2939 | (NILP (flags
[15]) ? 0 : CODING_FLAG_ISO_SAFE
)
2940 | (NILP (flags
[16]) ? 0 : CODING_FLAG_ISO_LATIN_EXTRA
)
2943 /* Invoke graphic register 0 to plane 0. */
2944 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 0;
2945 /* Invoke graphic register 1 to plane 1 if we can use full 8-bit. */
2946 CODING_SPEC_ISO_INVOCATION (coding
, 1)
2947 = (coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
? -1 : 1);
2948 /* Not single shifting at first. */
2949 CODING_SPEC_ISO_SINGLE_SHIFTING (coding
) = 0;
2950 /* Beginning of buffer should also be regarded as bol. */
2951 CODING_SPEC_ISO_BOL (coding
) = 1;
2953 for (charset
= 0; charset
<= MAX_CHARSET
; charset
++)
2954 CODING_SPEC_ISO_REVISION_NUMBER (coding
, charset
) = 255;
2955 val
= Vcharset_revision_alist
;
2958 charset
= get_charset_id (Fcar_safe (XCONS (val
)->car
));
2960 && (temp
= Fcdr_safe (XCONS (val
)->car
), INTEGERP (temp
))
2961 && (i
= XINT (temp
), (i
>= 0 && (i
+ '@') < 128)))
2962 CODING_SPEC_ISO_REVISION_NUMBER (coding
, charset
) = i
;
2963 val
= XCONS (val
)->cdr
;
2966 /* Checks FLAGS[REG] (REG = 0, 1, 2 3) and decide designations.
2967 FLAGS[REG] can be one of below:
2968 integer CHARSET: CHARSET occupies register I,
2969 t: designate nothing to REG initially, but can be used
2971 list of integer, nil, or t: designate the first
2972 element (if integer) to REG initially, the remaining
2973 elements (if integer) is designated to REG on request,
2974 if an element is t, REG can be used by any charsets,
2975 nil: REG is never used. */
2976 for (charset
= 0; charset
<= MAX_CHARSET
; charset
++)
2977 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
2978 = CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
;
2979 for (i
= 0; i
< 4; i
++)
2981 if (INTEGERP (flags
[i
])
2982 && (charset
= XINT (flags
[i
]), CHARSET_VALID_P (charset
))
2983 || (charset
= get_charset_id (flags
[i
])) >= 0)
2985 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = charset
;
2986 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
) = i
;
2988 else if (EQ (flags
[i
], Qt
))
2990 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
2992 coding
->flags
|= CODING_FLAG_ISO_DESIGNATION
;
2994 else if (CONSP (flags
[i
]))
2999 coding
->flags
|= CODING_FLAG_ISO_DESIGNATION
;
3000 if (INTEGERP (XCONS (tail
)->car
)
3001 && (charset
= XINT (XCONS (tail
)->car
),
3002 CHARSET_VALID_P (charset
))
3003 || (charset
= get_charset_id (XCONS (tail
)->car
)) >= 0)
3005 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = charset
;
3006 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
) =i
;
3009 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
3010 tail
= XCONS (tail
)->cdr
;
3011 while (CONSP (tail
))
3013 if (INTEGERP (XCONS (tail
)->car
)
3014 && (charset
= XINT (XCONS (tail
)->car
),
3015 CHARSET_VALID_P (charset
))
3016 || (charset
= get_charset_id (XCONS (tail
)->car
)) >= 0)
3017 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
3019 else if (EQ (XCONS (tail
)->car
, Qt
))
3021 tail
= XCONS (tail
)->cdr
;
3025 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
3027 CODING_SPEC_ISO_DESIGNATION (coding
, i
)
3028 = CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
);
3031 if (reg_bits
&& ! (coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
))
3033 /* REG 1 can be used only by locking shift in 7-bit env. */
3034 if (coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
)
3036 if (! (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
))
3037 /* Without any shifting, only REG 0 and 1 can be used. */
3042 for (charset
= 0; charset
<= MAX_CHARSET
; charset
++)
3044 if (CHARSET_VALID_P (charset
))
3046 /* There exist some default graphic registers to be
3049 /* We had better avoid designating a charset of
3050 CHARS96 to REG 0 as far as possible. */
3051 if (CHARSET_CHARS (charset
) == 96)
3052 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
3054 ? 1 : (reg_bits
& 4 ? 2 : (reg_bits
& 8 ? 3 : 0)));
3056 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
3058 ? 0 : (reg_bits
& 2 ? 1 : (reg_bits
& 4 ? 2 : 3)));
3062 coding
->common_flags
|= CODING_REQUIRE_FLUSHING_MASK
;
3063 coding
->spec
.iso2022
.last_invalid_designation_register
= -1;
3067 coding
->type
= coding_type_big5
;
3068 coding
->common_flags
3069 |= CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
;
3071 = (NILP (XVECTOR (coding_spec
)->contents
[4])
3072 ? CODING_FLAG_BIG5_HKU
3073 : CODING_FLAG_BIG5_ETEN
);
3077 coding
->type
= coding_type_ccl
;
3078 coding
->common_flags
3079 |= CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
;
3082 Lisp_Object decoder
, encoder
;
3084 val
= XVECTOR (coding_spec
)->contents
[4];
3086 && SYMBOLP (XCONS (val
)->car
)
3087 && !NILP (decoder
= Fget (XCONS (val
)->car
, Qccl_program_idx
))
3088 && !NILP (decoder
= Fcdr (Faref (Vccl_program_table
, decoder
)))
3089 && SYMBOLP (XCONS (val
)->cdr
)
3090 && !NILP (encoder
= Fget (XCONS (val
)->cdr
, Qccl_program_idx
))
3091 && !NILP (encoder
= Fcdr (Faref (Vccl_program_table
, encoder
))))
3093 setup_ccl_program (&(coding
->spec
.ccl
.decoder
), decoder
);
3094 setup_ccl_program (&(coding
->spec
.ccl
.encoder
), encoder
);
3097 goto label_invalid_coding_system
;
3099 bzero (coding
->spec
.ccl
.valid_codes
, 256);
3100 val
= Fplist_get (plist
, Qvalid_codes
);
3105 for (; CONSP (val
); val
= XCONS (val
)->cdr
)
3107 this = XCONS (val
)->car
;
3109 && XINT (this) >= 0 && XINT (this) < 256)
3110 coding
->spec
.ccl
.valid_codes
[XINT (this)] = 1;
3111 else if (CONSP (this)
3112 && INTEGERP (XCONS (this)->car
)
3113 && INTEGERP (XCONS (this)->cdr
))
3115 int start
= XINT (XCONS (this)->car
);
3116 int end
= XINT (XCONS (this)->cdr
);
3118 if (start
>= 0 && start
<= end
&& end
< 256)
3120 coding
->spec
.ccl
.valid_codes
[start
++] = 1;
3125 coding
->common_flags
|= CODING_REQUIRE_FLUSHING_MASK
;
3129 coding
->type
= coding_type_raw_text
;
3133 goto label_invalid_coding_system
;
3137 label_invalid_coding_system
:
3138 coding
->type
= coding_type_no_conversion
;
3139 coding
->category_idx
= CODING_CATEGORY_IDX_BINARY
;
3140 coding
->common_flags
= 0;
3141 coding
->eol_type
= CODING_EOL_LF
;
3142 coding
->pre_write_conversion
= coding
->post_read_conversion
= Qnil
;
3146 /* Setup raw-text or one of its subsidiaries in the structure
3147 coding_system CODING according to the already setup value eol_type
3148 in CODING. CODING should be setup for some coding system in
3152 setup_raw_text_coding_system (coding
)
3153 struct coding_system
*coding
;
3155 if (coding
->type
!= coding_type_raw_text
)
3157 coding
->symbol
= Qraw_text
;
3158 coding
->type
= coding_type_raw_text
;
3159 if (coding
->eol_type
!= CODING_EOL_UNDECIDED
)
3161 Lisp_Object subsidiaries
;
3162 subsidiaries
= Fget (Qraw_text
, Qeol_type
);
3164 if (VECTORP (subsidiaries
)
3165 && XVECTOR (subsidiaries
)->size
== 3)
3167 = XVECTOR (subsidiaries
)->contents
[coding
->eol_type
];
3173 /* Emacs has a mechanism to automatically detect a coding system if it
3174 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
3175 it's impossible to distinguish some coding systems accurately
3176 because they use the same range of codes. So, at first, coding
3177 systems are categorized into 7, those are:
3179 o coding-category-emacs-mule
3181 The category for a coding system which has the same code range
3182 as Emacs' internal format. Assigned the coding-system (Lisp
3183 symbol) `emacs-mule' by default.
3185 o coding-category-sjis
3187 The category for a coding system which has the same code range
3188 as SJIS. Assigned the coding-system (Lisp
3189 symbol) `japanese-shift-jis' by default.
3191 o coding-category-iso-7
3193 The category for a coding system which has the same code range
3194 as ISO2022 of 7-bit environment. This doesn't use any locking
3195 shift and single shift functions. This can encode/decode all
3196 charsets. Assigned the coding-system (Lisp symbol)
3197 `iso-2022-7bit' by default.
3199 o coding-category-iso-7-tight
3201 Same as coding-category-iso-7 except that this can
3202 encode/decode only the specified charsets.
3204 o coding-category-iso-8-1
3206 The category for a coding system which has the same code range
3207 as ISO2022 of 8-bit environment and graphic plane 1 used only
3208 for DIMENSION1 charset. This doesn't use any locking shift
3209 and single shift functions. Assigned the coding-system (Lisp
3210 symbol) `iso-latin-1' by default.
3212 o coding-category-iso-8-2
3214 The category for a coding system which has the same code range
3215 as ISO2022 of 8-bit environment and graphic plane 1 used only
3216 for DIMENSION2 charset. This doesn't use any locking shift
3217 and single shift functions. Assigned the coding-system (Lisp
3218 symbol) `japanese-iso-8bit' by default.
3220 o coding-category-iso-7-else
3222 The category for a coding system which has the same code range
3223 as ISO2022 of 7-bit environemnt but uses locking shift or
3224 single shift functions. Assigned the coding-system (Lisp
3225 symbol) `iso-2022-7bit-lock' by default.
3227 o coding-category-iso-8-else
3229 The category for a coding system which has the same code range
3230 as ISO2022 of 8-bit environemnt but uses locking shift or
3231 single shift functions. Assigned the coding-system (Lisp
3232 symbol) `iso-2022-8bit-ss2' by default.
3234 o coding-category-big5
3236 The category for a coding system which has the same code range
3237 as BIG5. Assigned the coding-system (Lisp symbol)
3238 `cn-big5' by default.
3240 o coding-category-ccl
3242 The category for a coding system of which encoder/decoder is
3243 written in CCL programs. The default value is nil, i.e., no
3244 coding system is assigned.
3246 o coding-category-binary
3248 The category for a coding system not categorized in any of the
3249 above. Assigned the coding-system (Lisp symbol)
3250 `no-conversion' by default.
3252 Each of them is a Lisp symbol and the value is an actual
3253 `coding-system's (this is also a Lisp symbol) assigned by a user.
3254 What Emacs does actually is to detect a category of coding system.
3255 Then, it uses a `coding-system' assigned to it. If Emacs can't
3256 decide only one possible category, it selects a category of the
3257 highest priority. Priorities of categories are also specified by a
3258 user in a Lisp variable `coding-category-list'.
3263 int ascii_skip_code
[256];
3265 /* Detect how a text of length SRC_BYTES pointed by SOURCE is encoded.
3266 If it detects possible coding systems, return an integer in which
3267 appropriate flag bits are set. Flag bits are defined by macros
3268 CODING_CATEGORY_MASK_XXX in `coding.h'.
3270 How many ASCII characters are at the head is returned as *SKIP. */
3273 detect_coding_mask (source
, src_bytes
, priorities
, skip
)
3274 unsigned char *source
;
3275 int src_bytes
, *priorities
, *skip
;
3277 register unsigned char c
;
3278 unsigned char *src
= source
, *src_end
= source
+ src_bytes
;
3282 /* At first, skip all ASCII characters and control characters except
3283 for three ISO2022 specific control characters. */
3284 ascii_skip_code
[ISO_CODE_SO
] = 0;
3285 ascii_skip_code
[ISO_CODE_SI
] = 0;
3286 ascii_skip_code
[ISO_CODE_ESC
] = 0;
3288 label_loop_detect_coding
:
3289 while (src
< src_end
&& ascii_skip_code
[*src
]) src
++;
3290 *skip
= src
- source
;
3293 /* We found nothing other than ASCII. There's nothing to do. */
3297 /* The text seems to be encoded in some multilingual coding system.
3298 Now, try to find in which coding system the text is encoded. */
3301 /* i.e. (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) */
3302 /* C is an ISO2022 specific control code of C0. */
3303 mask
= detect_coding_iso2022 (src
, src_end
);
3306 /* No valid ISO2022 code follows C. Try again. */
3308 if (c
== ISO_CODE_ESC
)
3309 ascii_skip_code
[ISO_CODE_ESC
] = 1;
3311 ascii_skip_code
[ISO_CODE_SO
] = ascii_skip_code
[ISO_CODE_SI
] = 1;
3312 goto label_loop_detect_coding
;
3315 goto label_return_highest_only
;
3323 /* C is the first byte of SJIS character code,
3324 or a leading-code of Emacs' internal format (emacs-mule). */
3325 try = CODING_CATEGORY_MASK_SJIS
| CODING_CATEGORY_MASK_EMACS_MULE
;
3327 /* Or, if C is a special latin extra code,
3328 or is an ISO2022 specific control code of C1 (SS2 or SS3),
3329 or is an ISO2022 control-sequence-introducer (CSI),
3330 we should also consider the possibility of ISO2022 codings. */
3331 if ((VECTORP (Vlatin_extra_code_table
)
3332 && !NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
3333 || (c
== ISO_CODE_SS2
|| c
== ISO_CODE_SS3
)
3334 || (c
== ISO_CODE_CSI
3337 || ((*src
== '0' || *src
== '1' || *src
== '2')
3338 && src
+ 1 < src_end
3339 && src
[1] == ']')))))
3340 try |= (CODING_CATEGORY_MASK_ISO_8_ELSE
3341 | CODING_CATEGORY_MASK_ISO_8BIT
);
3344 /* C is a character of ISO2022 in graphic plane right,
3345 or a SJIS's 1-byte character code (i.e. JISX0201),
3346 or the first byte of BIG5's 2-byte code. */
3347 try = (CODING_CATEGORY_MASK_ISO_8_ELSE
3348 | CODING_CATEGORY_MASK_ISO_8BIT
3349 | CODING_CATEGORY_MASK_SJIS
3350 | CODING_CATEGORY_MASK_BIG5
);
3352 /* Or, we may have to consider the possibility of CCL. */
3353 if (coding_system_table
[CODING_CATEGORY_IDX_CCL
]
3354 && (coding_system_table
[CODING_CATEGORY_IDX_CCL
]
3355 ->spec
.ccl
.valid_codes
)[c
])
3356 try |= CODING_CATEGORY_MASK_CCL
;
3361 for (i
= 0; i
< CODING_CATEGORY_IDX_MAX
; i
++)
3363 if (priorities
[i
] & try & CODING_CATEGORY_MASK_ISO
)
3364 mask
= detect_coding_iso2022 (src
, src_end
);
3365 else if (priorities
[i
] & try & CODING_CATEGORY_MASK_SJIS
)
3366 mask
= detect_coding_sjis (src
, src_end
);
3367 else if (priorities
[i
] & try & CODING_CATEGORY_MASK_BIG5
)
3368 mask
= detect_coding_big5 (src
, src_end
);
3369 else if (priorities
[i
] & try & CODING_CATEGORY_MASK_EMACS_MULE
)
3370 mask
= detect_coding_emacs_mule (src
, src_end
);
3371 else if (priorities
[i
] & try & CODING_CATEGORY_MASK_CCL
)
3372 mask
= detect_coding_ccl (src
, src_end
);
3373 else if (priorities
[i
] & CODING_CATEGORY_MASK_RAW_TEXT
)
3374 mask
= CODING_CATEGORY_MASK_RAW_TEXT
;
3375 else if (priorities
[i
] & CODING_CATEGORY_MASK_BINARY
)
3376 mask
= CODING_CATEGORY_MASK_BINARY
;
3378 goto label_return_highest_only
;
3380 return CODING_CATEGORY_MASK_RAW_TEXT
;
3382 if (try & CODING_CATEGORY_MASK_ISO
)
3383 mask
|= detect_coding_iso2022 (src
, src_end
);
3384 if (try & CODING_CATEGORY_MASK_SJIS
)
3385 mask
|= detect_coding_sjis (src
, src_end
);
3386 if (try & CODING_CATEGORY_MASK_BIG5
)
3387 mask
|= detect_coding_big5 (src
, src_end
);
3388 if (try & CODING_CATEGORY_MASK_EMACS_MULE
)
3389 mask
|= detect_coding_emacs_mule (src
, src_end
);
3390 if (try & CODING_CATEGORY_MASK_CCL
)
3391 mask
|= detect_coding_ccl (src
, src_end
);
3393 return (mask
| CODING_CATEGORY_MASK_RAW_TEXT
| CODING_CATEGORY_MASK_BINARY
);
3395 label_return_highest_only
:
3396 for (i
= 0; i
< CODING_CATEGORY_IDX_MAX
; i
++)
3398 if (mask
& priorities
[i
])
3399 return priorities
[i
];
3401 return CODING_CATEGORY_MASK_RAW_TEXT
;
3404 /* Detect how a text of length SRC_BYTES pointed by SRC is encoded.
3405 The information of the detected coding system is set in CODING. */
3408 detect_coding (coding
, src
, src_bytes
)
3409 struct coding_system
*coding
;
3417 val
= Vcoding_category_list
;
3418 mask
= detect_coding_mask (src
, src_bytes
, coding_priorities
, &skip
);
3419 coding
->heading_ascii
= skip
;
3423 /* We found a single coding system of the highest priority in MASK. */
3425 while (mask
&& ! (mask
& 1)) mask
>>= 1, idx
++;
3427 idx
= CODING_CATEGORY_IDX_RAW_TEXT
;
3429 val
= XSYMBOL (XVECTOR (Vcoding_category_table
)->contents
[idx
])->value
;
3431 if (coding
->eol_type
!= CODING_EOL_UNDECIDED
)
3435 tmp
= Fget (val
, Qeol_type
);
3437 val
= XVECTOR (tmp
)->contents
[coding
->eol_type
];
3439 setup_coding_system (val
, coding
);
3440 /* Set this again because setup_coding_system reset this member. */
3441 coding
->heading_ascii
= skip
;
3444 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
3445 SOURCE is encoded. Return one of CODING_EOL_LF, CODING_EOL_CRLF,
3446 CODING_EOL_CR, and CODING_EOL_UNDECIDED.
3448 How many non-eol characters are at the head is returned as *SKIP. */
3450 #define MAX_EOL_CHECK_COUNT 3
3453 detect_eol_type (source
, src_bytes
, skip
)
3454 unsigned char *source
;
3455 int src_bytes
, *skip
;
3457 unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
3459 int total
= 0; /* How many end-of-lines are found so far. */
3460 int eol_type
= CODING_EOL_UNDECIDED
;
3465 while (src
< src_end
&& total
< MAX_EOL_CHECK_COUNT
)
3468 if (c
== '\n' || c
== '\r')
3471 *skip
= src
- 1 - source
;
3474 this_eol_type
= CODING_EOL_LF
;
3475 else if (src
>= src_end
|| *src
!= '\n')
3476 this_eol_type
= CODING_EOL_CR
;
3478 this_eol_type
= CODING_EOL_CRLF
, src
++;
3480 if (eol_type
== CODING_EOL_UNDECIDED
)
3481 /* This is the first end-of-line. */
3482 eol_type
= this_eol_type
;
3483 else if (eol_type
!= this_eol_type
)
3485 /* The found type is different from what found before. */
3486 eol_type
= CODING_EOL_INCONSISTENT
;
3493 *skip
= src_end
- source
;
3497 /* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC
3498 is encoded. If it detects an appropriate format of end-of-line, it
3499 sets the information in *CODING. */
3502 detect_eol (coding
, src
, src_bytes
)
3503 struct coding_system
*coding
;
3509 int eol_type
= detect_eol_type (src
, src_bytes
, &skip
);
3511 if (coding
->heading_ascii
> skip
)
3512 coding
->heading_ascii
= skip
;
3514 skip
= coding
->heading_ascii
;
3516 if (eol_type
== CODING_EOL_UNDECIDED
)
3518 if (eol_type
== CODING_EOL_INCONSISTENT
)
3521 /* This code is suppressed until we find a better way to
3522 distinguish raw text file and binary file. */
3524 /* If we have already detected that the coding is raw-text, the
3525 coding should actually be no-conversion. */
3526 if (coding
->type
== coding_type_raw_text
)
3528 setup_coding_system (Qno_conversion
, coding
);
3531 /* Else, let's decode only text code anyway. */
3533 eol_type
= CODING_EOL_LF
;
3536 val
= Fget (coding
->symbol
, Qeol_type
);
3537 if (VECTORP (val
) && XVECTOR (val
)->size
== 3)
3539 setup_coding_system (XVECTOR (val
)->contents
[eol_type
], coding
);
3540 coding
->heading_ascii
= skip
;
3544 #define CONVERSION_BUFFER_EXTRA_ROOM 256
3546 #define DECODING_BUFFER_MAG(coding) \
3547 (coding->type == coding_type_iso2022 \
3549 : ((coding->type == coding_type_sjis || coding->type == coding_type_big5) \
3551 : (coding->type == coding_type_raw_text \
3553 : (coding->type == coding_type_ccl \
3554 ? coding->spec.ccl.decoder.buf_magnification \
3557 /* Return maximum size (bytes) of a buffer enough for decoding
3558 SRC_BYTES of text encoded in CODING. */
3561 decoding_buffer_size (coding
, src_bytes
)
3562 struct coding_system
*coding
;
3565 return (src_bytes
* DECODING_BUFFER_MAG (coding
)
3566 + CONVERSION_BUFFER_EXTRA_ROOM
);
3569 /* Return maximum size (bytes) of a buffer enough for encoding
3570 SRC_BYTES of text to CODING. */
3573 encoding_buffer_size (coding
, src_bytes
)
3574 struct coding_system
*coding
;
3579 if (coding
->type
== coding_type_ccl
)
3580 magnification
= coding
->spec
.ccl
.encoder
.buf_magnification
;
3584 return (src_bytes
* magnification
+ CONVERSION_BUFFER_EXTRA_ROOM
);
3587 #ifndef MINIMUM_CONVERSION_BUFFER_SIZE
3588 #define MINIMUM_CONVERSION_BUFFER_SIZE 1024
3591 char *conversion_buffer
;
3592 int conversion_buffer_size
;
3594 /* Return a pointer to a SIZE bytes of buffer to be used for encoding
3595 or decoding. Sufficient memory is allocated automatically. If we
3596 run out of memory, return NULL. */
3599 get_conversion_buffer (size
)
3602 if (size
> conversion_buffer_size
)
3605 int real_size
= conversion_buffer_size
* 2;
3607 while (real_size
< size
) real_size
*= 2;
3608 buf
= (char *) xmalloc (real_size
);
3609 xfree (conversion_buffer
);
3610 conversion_buffer
= buf
;
3611 conversion_buffer_size
= real_size
;
3613 return conversion_buffer
;
3617 ccl_coding_driver (coding
, source
, destination
, src_bytes
, dst_bytes
, encodep
)
3618 struct coding_system
*coding
;
3619 unsigned char *source
, *destination
;
3620 int src_bytes
, dst_bytes
, encodep
;
3622 struct ccl_program
*ccl
3623 = encodep
? &coding
->spec
.ccl
.encoder
: &coding
->spec
.ccl
.decoder
;
3627 ccl
->last_block
= coding
->mode
& CODING_MODE_LAST_BLOCK
;
3629 coding
->produced
= ccl_driver (ccl
, source
, destination
,
3630 src_bytes
, dst_bytes
, &(coding
->consumed
));
3633 coding
->produced_char
= coding
->produced
;
3634 coding
->consumed_char
3635 = multibyte_chars_in_text (source
, coding
->consumed
);
3639 coding
->produced_char
3640 = multibyte_chars_in_text (destination
, coding
->produced
);
3641 coding
->consumed_char
= coding
->consumed
;
3643 switch (ccl
->status
)
3645 case CCL_STAT_SUSPEND_BY_SRC
:
3646 result
= CODING_FINISH_INSUFFICIENT_SRC
;
3648 case CCL_STAT_SUSPEND_BY_DST
:
3649 result
= CODING_FINISH_INSUFFICIENT_DST
;
3652 result
= CODING_FINISH_NORMAL
;
3658 /* See "GENERAL NOTES about `decode_coding_XXX ()' functions". Before
3659 decoding, it may detect coding system and format of end-of-line if
3660 those are not yet decided. */
3663 decode_coding (coding
, source
, destination
, src_bytes
, dst_bytes
)
3664 struct coding_system
*coding
;
3665 unsigned char *source
, *destination
;
3666 int src_bytes
, dst_bytes
;
3672 coding
->produced
= coding
->produced_char
= 0;
3673 coding
->consumed
= coding
->consumed_char
= 0;
3674 coding
->fake_multibyte
= 0;
3675 return CODING_FINISH_NORMAL
;
3678 if (coding
->type
== coding_type_undecided
)
3679 detect_coding (coding
, source
, src_bytes
);
3681 if (coding
->eol_type
== CODING_EOL_UNDECIDED
)
3682 detect_eol (coding
, source
, src_bytes
);
3684 switch (coding
->type
)
3686 case coding_type_emacs_mule
:
3687 case coding_type_undecided
:
3688 case coding_type_raw_text
:
3689 if (coding
->eol_type
== CODING_EOL_LF
3690 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
3691 goto label_no_conversion
;
3692 result
= decode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
);
3695 case coding_type_sjis
:
3696 result
= decode_coding_sjis_big5 (coding
, source
, destination
,
3697 src_bytes
, dst_bytes
, 1);
3700 case coding_type_iso2022
:
3701 result
= decode_coding_iso2022 (coding
, source
, destination
,
3702 src_bytes
, dst_bytes
);
3705 case coding_type_big5
:
3706 result
= decode_coding_sjis_big5 (coding
, source
, destination
,
3707 src_bytes
, dst_bytes
, 0);
3710 case coding_type_ccl
:
3711 result
= ccl_coding_driver (coding
, source
, destination
,
3712 src_bytes
, dst_bytes
, 0);
3715 default: /* i.e. case coding_type_no_conversion: */
3716 label_no_conversion
:
3717 if (dst_bytes
&& src_bytes
> dst_bytes
)
3719 coding
->produced
= dst_bytes
;
3720 result
= CODING_FINISH_INSUFFICIENT_DST
;
3724 coding
->produced
= src_bytes
;
3725 result
= CODING_FINISH_NORMAL
;
3728 bcopy (source
, destination
, coding
->produced
);
3730 safe_bcopy (source
, destination
, coding
->produced
);
3731 coding
->fake_multibyte
= 1;
3733 = coding
->consumed_char
= coding
->produced_char
= coding
->produced
;
3740 /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". */
3743 encode_coding (coding
, source
, destination
, src_bytes
, dst_bytes
)
3744 struct coding_system
*coding
;
3745 unsigned char *source
, *destination
;
3746 int src_bytes
, dst_bytes
;
3752 coding
->produced
= coding
->produced_char
= 0;
3753 coding
->consumed
= coding
->consumed_char
= 0;
3754 coding
->fake_multibyte
= 0;
3755 return CODING_FINISH_NORMAL
;
3758 switch (coding
->type
)
3760 case coding_type_emacs_mule
:
3761 case coding_type_undecided
:
3762 case coding_type_raw_text
:
3763 if (coding
->eol_type
== CODING_EOL_LF
3764 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
3765 goto label_no_conversion
;
3766 result
= encode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
);
3769 case coding_type_sjis
:
3770 result
= encode_coding_sjis_big5 (coding
, source
, destination
,
3771 src_bytes
, dst_bytes
, 1);
3774 case coding_type_iso2022
:
3775 result
= encode_coding_iso2022 (coding
, source
, destination
,
3776 src_bytes
, dst_bytes
);
3779 case coding_type_big5
:
3780 result
= encode_coding_sjis_big5 (coding
, source
, destination
,
3781 src_bytes
, dst_bytes
, 0);
3784 case coding_type_ccl
:
3785 result
= ccl_coding_driver (coding
, source
, destination
,
3786 src_bytes
, dst_bytes
, 1);
3789 default: /* i.e. case coding_type_no_conversion: */
3790 label_no_conversion
:
3791 if (dst_bytes
&& src_bytes
> dst_bytes
)
3793 coding
->produced
= dst_bytes
;
3794 result
= CODING_FINISH_INSUFFICIENT_DST
;
3798 coding
->produced
= src_bytes
;
3799 result
= CODING_FINISH_NORMAL
;
3802 bcopy (source
, destination
, coding
->produced
);
3804 safe_bcopy (source
, destination
, coding
->produced
);
3805 if (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
)
3807 unsigned char *p
= destination
, *pend
= p
+ coding
->produced
;
3809 if (*p
++ == '\015') p
[-1] = '\n';
3811 coding
->fake_multibyte
= 1;
3813 = coding
->consumed_char
= coding
->produced_char
= coding
->produced
;
3820 /* Scan text in the region between *BEG and *END (byte positions),
3821 skip characters which we don't have to decode by coding system
3822 CODING at the head and tail, then set *BEG and *END to the region
3823 of the text we actually have to convert. The caller should move
3824 the gap out of the region in advance.
3826 If STR is not NULL, *BEG and *END are indices into STR. */
3829 shrink_decoding_region (beg
, end
, coding
, str
)
3831 struct coding_system
*coding
;
3834 unsigned char *begp_orig
, *begp
, *endp_orig
, *endp
, c
;
3837 if (coding
->type
== coding_type_ccl
3838 || coding
->type
== coding_type_undecided
3839 || !NILP (coding
->post_read_conversion
))
3841 /* We can't skip any data. */
3844 else if (coding
->type
== coding_type_no_conversion
)
3846 /* We need no conversion, but don't have to skip any data here.
3847 Decoding routine handles them effectively anyway. */
3851 eol_conversion
= (coding
->eol_type
!= CODING_EOL_LF
);
3853 if ((! eol_conversion
) && (coding
->heading_ascii
>= 0))
3854 /* Detection routine has already found how much we can skip at the
3856 *beg
+= coding
->heading_ascii
;
3860 begp_orig
= begp
= str
+ *beg
;
3861 endp_orig
= endp
= str
+ *end
;
3865 begp_orig
= begp
= BYTE_POS_ADDR (*beg
);
3866 endp_orig
= endp
= begp
+ *end
- *beg
;
3869 switch (coding
->type
)
3871 case coding_type_emacs_mule
:
3872 case coding_type_raw_text
:
3875 if (coding
->heading_ascii
< 0)
3876 while (begp
< endp
&& *begp
!= '\r' && *begp
< 0x80) begp
++;
3877 while (begp
< endp
&& endp
[-1] != '\r' && endp
[-1] < 0x80)
3879 /* Do not consider LF as ascii if preceded by CR, since that
3880 confuses eol decoding. */
3881 if (begp
< endp
&& endp
< endp_orig
&& endp
[-1] == '\r' && endp
[0] == '\n')
3888 case coding_type_sjis
:
3889 case coding_type_big5
:
3890 /* We can skip all ASCII characters at the head. */
3891 if (coding
->heading_ascii
< 0)
3894 while (begp
< endp
&& *begp
< 0x80 && *begp
!= '\r') begp
++;
3896 while (begp
< endp
&& *begp
< 0x80) begp
++;
3898 /* We can skip all ASCII characters at the tail except for the
3899 second byte of SJIS or BIG5 code. */
3901 while (begp
< endp
&& endp
[-1] < 0x80 && endp
[-1] != '\r') endp
--;
3903 while (begp
< endp
&& endp
[-1] < 0x80) endp
--;
3904 /* Do not consider LF as ascii if preceded by CR, since that
3905 confuses eol decoding. */
3906 if (begp
< endp
&& endp
< endp_orig
&& endp
[-1] == '\r' && endp
[0] == '\n')
3908 if (begp
< endp
&& endp
< endp_orig
&& endp
[-1] >= 0x80)
3912 default: /* i.e. case coding_type_iso2022: */
3913 if (coding
->heading_ascii
< 0)
3915 /* We can skip all ASCII characters at the head except for a
3916 few control codes. */
3917 while (begp
< endp
&& (c
= *begp
) < 0x80
3918 && c
!= ISO_CODE_CR
&& c
!= ISO_CODE_SO
3919 && c
!= ISO_CODE_SI
&& c
!= ISO_CODE_ESC
3920 && (!eol_conversion
|| c
!= ISO_CODE_LF
))
3923 switch (coding
->category_idx
)
3925 case CODING_CATEGORY_IDX_ISO_8_1
:
3926 case CODING_CATEGORY_IDX_ISO_8_2
:
3927 /* We can skip all ASCII characters at the tail. */
3929 while (begp
< endp
&& (c
= endp
[-1]) < 0x80 && c
!= '\r') endp
--;
3931 while (begp
< endp
&& endp
[-1] < 0x80) endp
--;
3932 /* Do not consider LF as ascii if preceded by CR, since that
3933 confuses eol decoding. */
3934 if (begp
< endp
&& endp
< endp_orig
&& endp
[-1] == '\r' && endp
[0] == '\n')
3938 case CODING_CATEGORY_IDX_ISO_7
:
3939 case CODING_CATEGORY_IDX_ISO_7_TIGHT
:
3940 /* We can skip all charactes at the tail except for ESC and
3941 the following 2-byte at the tail. */
3944 && (c
= endp
[-1]) < 0x80 && c
!= ISO_CODE_ESC
&& c
!= '\r')
3948 && (c
= endp
[-1]) < 0x80 && c
!= ISO_CODE_ESC
)
3950 /* Do not consider LF as ascii if preceded by CR, since that
3951 confuses eol decoding. */
3952 if (begp
< endp
&& endp
< endp_orig
&& endp
[-1] == '\r' && endp
[0] == '\n')
3954 if (begp
< endp
&& endp
[-1] == ISO_CODE_ESC
)
3956 if (endp
+ 1 < endp_orig
&& end
[0] == '(' && end
[1] == 'B')
3957 /* This is an ASCII designation sequence. We can
3958 surely skip the tail. */
3961 /* Hmmm, we can't skip the tail. */
3966 *beg
+= begp
- begp_orig
;
3967 *end
+= endp
- endp_orig
;
3971 /* Like shrink_decoding_region but for encoding. */
3974 shrink_encoding_region (beg
, end
, coding
, str
)
3976 struct coding_system
*coding
;
3979 unsigned char *begp_orig
, *begp
, *endp_orig
, *endp
;
3982 if (coding
->type
== coding_type_ccl
)
3983 /* We can't skip any data. */
3985 else if (coding
->type
== coding_type_no_conversion
)
3987 /* We need no conversion. */
3994 begp_orig
= begp
= str
+ *beg
;
3995 endp_orig
= endp
= str
+ *end
;
3999 begp_orig
= begp
= BYTE_POS_ADDR (*beg
);
4000 endp_orig
= endp
= begp
+ *end
- *beg
;
4003 eol_conversion
= (coding
->eol_type
== CODING_EOL_CR
4004 || coding
->eol_type
== CODING_EOL_CRLF
);
4006 /* Here, we don't have to check coding->pre_write_conversion because
4007 the caller is expected to have handled it already. */
4008 switch (coding
->type
)
4010 case coding_type_undecided
:
4011 case coding_type_emacs_mule
:
4012 case coding_type_raw_text
:
4015 while (begp
< endp
&& *begp
!= '\n') begp
++;
4016 while (begp
< endp
&& endp
[-1] != '\n') endp
--;
4022 case coding_type_iso2022
:
4023 if (coding
->flags
& CODING_FLAG_ISO_DESIGNATE_AT_BOL
)
4025 unsigned char *bol
= begp
;
4026 while (begp
< endp
&& *begp
< 0x80)
4029 if (begp
[-1] == '\n')
4033 goto label_skip_tail
;
4038 /* We can skip all ASCII characters at the head and tail. */
4040 while (begp
< endp
&& *begp
< 0x80 && *begp
!= '\n') begp
++;
4042 while (begp
< endp
&& *begp
< 0x80) begp
++;
4045 while (begp
< endp
&& endp
[-1] < 0x80 && endp
[-1] != '\n') endp
--;
4047 while (begp
< endp
&& *(endp
- 1) < 0x80) endp
--;
4051 *beg
+= begp
- begp_orig
;
4052 *end
+= endp
- endp_orig
;
4056 /* Decode (if ENCODEP is zero) or encode (if ENCODEP is nonzero) the
4057 text from FROM to TO (byte positions are FROM_BYTE and TO_BYTE) by
4058 coding system CODING, and return the status code of code conversion
4059 (currently, this value has no meaning).
4061 How many characters (and bytes) are converted to how many
4062 characters (and bytes) are recorded in members of the structure
4065 If REPLACE is nonzero, we do various things as if the original text
4066 is deleted and a new text is inserted. See the comments in
4067 replace_range (insdel.c) to know what we are doing. */
4070 code_convert_region (from
, from_byte
, to
, to_byte
, coding
, encodep
, replace
)
4071 int from
, from_byte
, to
, to_byte
, encodep
, replace
;
4072 struct coding_system
*coding
;
4074 int len
= to
- from
, len_byte
= to_byte
- from_byte
;
4075 int require
, inserted
, inserted_byte
;
4076 int head_skip
, tail_skip
, total_skip
;
4077 Lisp_Object saved_coding_symbol
;
4078 int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
4080 int fake_multibyte
= 0;
4081 unsigned char *src
, *dst
;
4082 Lisp_Object deletion
;
4085 saved_coding_symbol
= Qnil
;
4087 if (from
< PT
&& PT
< to
)
4088 SET_PT_BOTH (from
, from_byte
);
4092 int saved_from
= from
;
4094 prepare_to_modify_buffer (from
, to
, &from
);
4095 if (saved_from
!= from
)
4099 from_byte
= CHAR_TO_BYTE (from
), to_byte
= CHAR_TO_BYTE (to
);
4101 from_byte
= from
, to_byte
= to
;
4102 len_byte
= to_byte
- from_byte
;
4106 if (! encodep
&& CODING_REQUIRE_DETECTION (coding
))
4108 /* We must detect encoding of text and eol format. */
4110 if (from
< GPT
&& to
> GPT
)
4111 move_gap_both (from
, from_byte
);
4112 if (coding
->type
== coding_type_undecided
)
4114 detect_coding (coding
, BYTE_POS_ADDR (from_byte
), len_byte
);
4115 if (coding
->type
== coding_type_undecided
)
4116 /* It seems that the text contains only ASCII, but we
4117 should not left it undecided because the deeper
4118 decoding routine (decode_coding) tries to detect the
4119 encodings again in vain. */
4120 coding
->type
= coding_type_emacs_mule
;
4122 if (coding
->eol_type
== CODING_EOL_UNDECIDED
)
4124 saved_coding_symbol
= coding
->symbol
;
4125 detect_eol (coding
, BYTE_POS_ADDR (from_byte
), len_byte
);
4126 if (coding
->eol_type
== CODING_EOL_UNDECIDED
)
4127 coding
->eol_type
= CODING_EOL_LF
;
4128 /* We had better recover the original eol format if we
4129 encounter an inconsitent eol format while decoding. */
4130 coding
->mode
|= CODING_MODE_INHIBIT_INCONSISTENT_EOL
;
4134 coding
->consumed_char
= len
, coding
->consumed
= len_byte
;
4137 ? ! CODING_REQUIRE_ENCODING (coding
)
4138 : ! CODING_REQUIRE_DECODING (coding
))
4140 coding
->produced
= len_byte
;
4143 /* See the comment of the member heading_ascii in coding.h. */
4144 && coding
->heading_ascii
< len_byte
)
4146 /* We still may have to combine byte at the head and the
4147 tail of the text in the region. */
4148 if (from
< GPT
&& GPT
< to
)
4149 move_gap_both (to
, to_byte
);
4150 len
= multibyte_chars_in_text (BYTE_POS_ADDR (from_byte
), len_byte
);
4151 adjust_after_insert (from
, from_byte
, to
, to_byte
, len
);
4152 coding
->produced_char
= len
;
4157 adjust_after_insert (from
, from_byte
, to
, to_byte
, len_byte
);
4158 coding
->produced_char
= len_byte
;
4163 /* Now we convert the text. */
4165 /* For encoding, we must process pre-write-conversion in advance. */
4167 && ! NILP (coding
->pre_write_conversion
)
4168 && SYMBOLP (coding
->pre_write_conversion
)
4169 && ! NILP (Ffboundp (coding
->pre_write_conversion
)))
4171 /* The function in pre-write-conversion may put a new text in a
4173 struct buffer
*prev
= current_buffer
, *new;
4175 call2 (coding
->pre_write_conversion
,
4176 make_number (from
), make_number (to
));
4177 if (current_buffer
!= prev
)
4180 new = current_buffer
;
4181 set_buffer_internal_1 (prev
);
4182 del_range_2 (from
, from_byte
, to
, to_byte
);
4183 insert_from_buffer (new, BEG
, len
, 0);
4185 to_byte
= multibyte
? CHAR_TO_BYTE (to
) : to
;
4186 len_byte
= to_byte
- from_byte
;
4191 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
4193 /* Try to skip the heading and tailing ASCIIs. */
4195 int from_byte_orig
= from_byte
, to_byte_orig
= to_byte
;
4197 if (from
< GPT
&& GPT
< to
)
4198 move_gap_both (from
, from_byte
);
4200 shrink_encoding_region (&from_byte
, &to_byte
, coding
, NULL
);
4202 shrink_decoding_region (&from_byte
, &to_byte
, coding
, NULL
);
4203 if (from_byte
== to_byte
)
4205 coding
->produced
= len_byte
;
4206 coding
->produced_char
= multibyte
? len
: len_byte
;
4208 /* We must record and adjust for this new text now. */
4209 adjust_after_insert (from
, from_byte_orig
, to
, to_byte_orig
, len
);
4213 head_skip
= from_byte
- from_byte_orig
;
4214 tail_skip
= to_byte_orig
- to_byte
;
4215 total_skip
= head_skip
+ tail_skip
;
4218 len
-= total_skip
; len_byte
-= total_skip
;
4221 /* For converion, we must put the gap before the text in addition to
4222 making the gap larger for efficient decoding. The required gap
4223 size starts from 2000 which is the magic number used in make_gap.
4224 But, after one batch of conversion, it will be incremented if we
4225 find that it is not enough . */
4228 if (GAP_SIZE
< require
)
4229 make_gap (require
- GAP_SIZE
);
4230 move_gap_both (from
, from_byte
);
4232 if (GPT
- BEG
< beg_unchanged
)
4233 beg_unchanged
= GPT
- BEG
;
4234 if (Z
- GPT
< end_unchanged
)
4235 end_unchanged
= Z
- GPT
;
4237 inserted
= inserted_byte
= 0;
4238 src
= GAP_END_ADDR
, dst
= GPT_ADDR
;
4240 GAP_SIZE
+= len_byte
;
4243 ZV_BYTE
-= len_byte
;
4250 /* The buffer memory is changed from:
4251 +--------+converted-text+---------+-------original-text------+---+
4252 |<-from->|<--inserted-->|---------|<-----------len---------->|---|
4253 |<------------------- GAP_SIZE -------------------->| */
4255 result
= encode_coding (coding
, src
, dst
, len_byte
, 0);
4257 result
= decode_coding (coding
, src
, dst
, len_byte
, 0);
4259 +--------+-------converted-text--------+--+---original-text--+---+
4260 |<-from->|<--inserted-->|<--produced-->|--|<-(len-consumed)->|---|
4261 |<------------------- GAP_SIZE -------------------->| */
4262 if (coding
->fake_multibyte
)
4265 if (!encodep
&& !multibyte
)
4266 coding
->produced_char
= coding
->produced
;
4267 inserted
+= coding
->produced_char
;
4268 inserted_byte
+= coding
->produced
;
4269 len_byte
-= coding
->consumed
;
4270 src
+= coding
->consumed
;
4271 dst
+= inserted_byte
;
4273 if (! encodep
&& result
== CODING_FINISH_INCONSISTENT_EOL
)
4275 unsigned char *pend
= dst
, *p
= pend
- inserted_byte
;
4277 /* Encode LFs back to the original eol format (CR or CRLF). */
4278 if (coding
->eol_type
== CODING_EOL_CR
)
4280 while (p
< pend
) if (*p
++ == '\n') p
[-1] = '\r';
4286 while (p
< pend
) if (*p
++ == '\n') count
++;
4287 if (src
- dst
< count
)
4289 /* We don't have sufficient room for putting LFs
4290 back to CRLF. We must record converted and
4291 not-yet-converted text back to the buffer
4292 content, enlarge the gap, then record them out of
4293 the buffer contents again. */
4294 int add
= len_byte
+ inserted_byte
;
4297 ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
4298 GPT
+= inserted_byte
; GPT_BYTE
+= inserted_byte
;
4299 make_gap (count
- GAP_SIZE
);
4301 ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
4302 GPT
-= inserted_byte
; GPT_BYTE
-= inserted_byte
;
4303 /* Don't forget to update SRC, DST, and PEND. */
4304 src
= GAP_END_ADDR
- len_byte
;
4305 dst
= GPT_ADDR
+ inserted_byte
;
4309 inserted_byte
+= count
;
4310 coding
->produced
+= count
;
4311 p
= dst
= pend
+ count
;
4315 if (*p
== '\n') count
--, *--p
= '\r';
4319 /* Suppress eol-format conversion in the further conversion. */
4320 coding
->eol_type
= CODING_EOL_LF
;
4322 /* Restore the original symbol. */
4323 coding
->symbol
= saved_coding_symbol
;
4329 if (result
== CODING_FINISH_INSUFFICIENT_SRC
)
4331 /* The source text ends in invalid codes. Let's just
4332 make them valid buffer contents, and finish conversion. */
4333 inserted
+= len_byte
;
4334 inserted_byte
+= len_byte
;
4342 /* We have just done the first batch of conversion which was
4343 stoped because of insufficient gap. Let's reconsider the
4344 required gap size (i.e. SRT - DST) now.
4346 We have converted ORIG bytes (== coding->consumed) into
4347 NEW bytes (coding->produced). To convert the remaining
4348 LEN bytes, we may need REQUIRE bytes of gap, where:
4349 REQUIRE + LEN_BYTE = LEN_BYTE * (NEW / ORIG)
4350 REQUIRE = LEN_BYTE * (NEW - ORIG) / ORIG
4351 Here, we are sure that NEW >= ORIG. */
4352 float ratio
= coding
->produced
- coding
->consumed
;
4353 ratio
/= coding
->consumed
;
4354 require
= len_byte
* ratio
;
4357 if ((src
- dst
) < (require
+ 2000))
4359 /* See the comment above the previous call of make_gap. */
4360 int add
= len_byte
+ inserted_byte
;
4363 ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
4364 GPT
+= inserted_byte
; GPT_BYTE
+= inserted_byte
;
4365 make_gap (require
+ 2000);
4367 ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
4368 GPT
-= inserted_byte
; GPT_BYTE
-= inserted_byte
;
4369 /* Don't forget to update SRC, DST. */
4370 src
= GAP_END_ADDR
- len_byte
;
4371 dst
= GPT_ADDR
+ inserted_byte
;
4374 if (src
- dst
> 0) *dst
= 0; /* Put an anchor. */
4378 || !encodep
&& (to
- from
) != (to_byte
- from_byte
)))
4379 inserted
= multibyte_chars_in_text (GPT_ADDR
, inserted_byte
);
4381 /* If we have shrinked the conversion area, adjust it now. */
4385 safe_bcopy (GAP_END_ADDR
, GPT_ADDR
+ inserted_byte
, tail_skip
);
4386 inserted
+= total_skip
; inserted_byte
+= total_skip
;
4387 GAP_SIZE
+= total_skip
;
4388 GPT
-= head_skip
; GPT_BYTE
-= head_skip
;
4389 ZV
-= total_skip
; ZV_BYTE
-= total_skip
;
4390 Z
-= total_skip
; Z_BYTE
-= total_skip
;
4391 from
-= head_skip
; from_byte
-= head_skip
;
4392 to
+= tail_skip
; to_byte
+= tail_skip
;
4395 adjust_after_replace (from
, from_byte
, deletion
, inserted
, inserted_byte
);
4397 if (! encodep
&& ! NILP (coding
->post_read_conversion
))
4400 int orig_inserted
= inserted
, pos
= PT
;
4403 temp_set_point_both (current_buffer
, from
, from_byte
);
4404 val
= call1 (coding
->post_read_conversion
, make_number (inserted
));
4407 CHECK_NUMBER (val
, 0);
4408 inserted
= XFASTINT (val
);
4410 if (pos
>= from
+ orig_inserted
)
4411 temp_set_point (current_buffer
, pos
+ (inserted
- orig_inserted
));
4414 signal_after_change (from
, to
- from
, inserted
);
4417 coding
->consumed
= to_byte
- from_byte
;
4418 coding
->consumed_char
= to
- from
;
4419 coding
->produced
= inserted_byte
;
4420 coding
->produced_char
= inserted
;
4427 code_convert_string (str
, coding
, encodep
, nocopy
)
4429 struct coding_system
*coding
;
4430 int encodep
, nocopy
;
4434 int from
= 0, to
= XSTRING (str
)->size
;
4435 int to_byte
= STRING_BYTES (XSTRING (str
));
4436 struct gcpro gcpro1
;
4437 Lisp_Object saved_coding_symbol
;
4440 saved_coding_symbol
= Qnil
;
4441 if (encodep
&& !NILP (coding
->pre_write_conversion
)
4442 || !encodep
&& !NILP (coding
->post_read_conversion
))
4444 /* Since we have to call Lisp functions which assume target text
4445 is in a buffer, after setting a temporary buffer, call
4446 code_convert_region. */
4447 int count
= specpdl_ptr
- specpdl
;
4448 struct buffer
*prev
= current_buffer
;
4450 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
4451 temp_output_buffer_setup (" *code-converting-work*");
4452 set_buffer_internal (XBUFFER (Vstandard_output
));
4454 insert_from_string (str
, 0, 0, to
, to_byte
, 0);
4457 /* We must insert the contents of STR as is without
4458 unibyte<->multibyte conversion. */
4459 current_buffer
->enable_multibyte_characters
= Qnil
;
4460 insert_from_string (str
, 0, 0, to_byte
, to_byte
, 0);
4461 current_buffer
->enable_multibyte_characters
= Qt
;
4463 code_convert_region (BEGV
, BEGV_BYTE
, ZV
, ZV_BYTE
, coding
, encodep
, 1);
4465 /* We must return the buffer contents as unibyte string. */
4466 current_buffer
->enable_multibyte_characters
= Qnil
;
4467 str
= make_buffer_string (BEGV
, ZV
, 0);
4468 set_buffer_internal (prev
);
4469 return unbind_to (count
, str
);
4472 if (! encodep
&& CODING_REQUIRE_DETECTION (coding
))
4474 /* See the comments in code_convert_region. */
4475 if (coding
->type
== coding_type_undecided
)
4477 detect_coding (coding
, XSTRING (str
)->data
, to_byte
);
4478 if (coding
->type
== coding_type_undecided
)
4479 coding
->type
= coding_type_emacs_mule
;
4481 if (coding
->eol_type
== CODING_EOL_UNDECIDED
)
4483 saved_coding_symbol
= coding
->symbol
;
4484 detect_eol (coding
, XSTRING (str
)->data
, to_byte
);
4485 if (coding
->eol_type
== CODING_EOL_UNDECIDED
)
4486 coding
->eol_type
= CODING_EOL_LF
;
4487 /* We had better recover the original eol format if we
4488 encounter an inconsitent eol format while decoding. */
4489 coding
->mode
|= CODING_MODE_INHIBIT_INCONSISTENT_EOL
;
4494 ? ! CODING_REQUIRE_ENCODING (coding
)
4495 : ! CODING_REQUIRE_DECODING (coding
))
4499 /* Try to skip the heading and tailing ASCIIs. */
4501 shrink_encoding_region (&from
, &to_byte
, coding
, XSTRING (str
)->data
);
4503 shrink_decoding_region (&from
, &to_byte
, coding
, XSTRING (str
)->data
);
4505 if (from
== to_byte
)
4506 return (nocopy
? str
: Fcopy_sequence (str
));
4509 len
= encoding_buffer_size (coding
, to_byte
- from
);
4511 len
= decoding_buffer_size (coding
, to_byte
- from
);
4512 len
+= from
+ STRING_BYTES (XSTRING (str
)) - to_byte
;
4514 buf
= get_conversion_buffer (len
);
4518 bcopy (XSTRING (str
)->data
, buf
, from
);
4520 ? encode_coding (coding
, XSTRING (str
)->data
+ from
,
4521 buf
+ from
, to_byte
- from
, len
)
4522 : decode_coding (coding
, XSTRING (str
)->data
+ from
,
4523 buf
+ from
, to_byte
- from
, len
));
4524 if (! encodep
&& result
== CODING_FINISH_INCONSISTENT_EOL
)
4526 /* We simple try to decode the whole string again but without
4527 eol-conversion this time. */
4528 coding
->eol_type
= CODING_EOL_LF
;
4529 coding
->symbol
= saved_coding_symbol
;
4530 return code_convert_string (str
, coding
, encodep
, nocopy
);
4533 bcopy (XSTRING (str
)->data
+ to_byte
, buf
+ from
+ coding
->produced
,
4534 STRING_BYTES (XSTRING (str
)) - to_byte
);
4536 len
= from
+ STRING_BYTES (XSTRING (str
)) - to_byte
;
4538 str
= make_unibyte_string (buf
, len
+ coding
->produced
);
4541 int chars
= (coding
->fake_multibyte
4542 ? multibyte_chars_in_text (buf
+ from
, coding
->produced
)
4543 : coding
->produced_char
);
4544 str
= make_multibyte_string (buf
, len
+ chars
, len
+ coding
->produced
);
4552 /*** 8. Emacs Lisp library functions ***/
4554 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
4555 "Return t if OBJECT is nil or a coding-system.\n\
4556 See the documentation of `make-coding-system' for information\n\
4557 about coding-system objects.")
4565 /* Get coding-spec vector for OBJ. */
4566 obj
= Fget (obj
, Qcoding_system
);
4567 return ((VECTORP (obj
) && XVECTOR (obj
)->size
== 5)
4571 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
4572 Sread_non_nil_coding_system
, 1, 1, 0,
4573 "Read a coding system from the minibuffer, prompting with string PROMPT.")
4580 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
4581 Qt
, Qnil
, Qcoding_system_history
, Qnil
, Qnil
);
4583 while (XSTRING (val
)->size
== 0);
4584 return (Fintern (val
, Qnil
));
4587 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 2, 0,
4588 "Read a coding system from the minibuffer, prompting with string PROMPT.\n\
4589 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.")
4590 (prompt
, default_coding_system
)
4591 Lisp_Object prompt
, default_coding_system
;
4594 if (SYMBOLP (default_coding_system
))
4595 XSETSTRING (default_coding_system
, XSYMBOL (default_coding_system
)->name
);
4596 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
4597 Qt
, Qnil
, Qcoding_system_history
,
4598 default_coding_system
, Qnil
);
4599 return (XSTRING (val
)->size
== 0 ? Qnil
: Fintern (val
, Qnil
));
4602 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
4604 "Check validity of CODING-SYSTEM.\n\
4605 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.\n\
4606 It is valid if it is a symbol with a non-nil `coding-system' property.\n\
4607 The value of property should be a vector of length 5.")
4609 Lisp_Object coding_system
;
4611 CHECK_SYMBOL (coding_system
, 0);
4612 if (!NILP (Fcoding_system_p (coding_system
)))
4613 return coding_system
;
4615 Fsignal (Qcoding_system_error
, Fcons (coding_system
, Qnil
));
4619 detect_coding_system (src
, src_bytes
, highest
)
4621 int src_bytes
, highest
;
4623 int coding_mask
, eol_type
;
4624 Lisp_Object val
, tmp
;
4627 coding_mask
= detect_coding_mask (src
, src_bytes
, NULL
, &dummy
);
4628 eol_type
= detect_eol_type (src
, src_bytes
, &dummy
);
4629 if (eol_type
== CODING_EOL_INCONSISTENT
)
4630 eol_type
= CODING_EOL_UNDECIDED
;
4635 if (eol_type
!= CODING_EOL_UNDECIDED
)
4638 val2
= Fget (Qundecided
, Qeol_type
);
4640 val
= XVECTOR (val2
)->contents
[eol_type
];
4642 return (highest
? val
: Fcons (val
, Qnil
));
4645 /* At first, gather possible coding systems in VAL. */
4647 for (tmp
= Vcoding_category_list
; !NILP (tmp
); tmp
= XCONS (tmp
)->cdr
)
4650 = XFASTINT (Fget (XCONS (tmp
)->car
, Qcoding_category_index
));
4651 if (coding_mask
& (1 << idx
))
4653 val
= Fcons (Fsymbol_value (XCONS (tmp
)->car
), val
);
4659 val
= Fnreverse (val
);
4661 /* Then, replace the elements with subsidiary coding systems. */
4662 for (tmp
= val
; !NILP (tmp
); tmp
= XCONS (tmp
)->cdr
)
4664 if (eol_type
!= CODING_EOL_UNDECIDED
4665 && eol_type
!= CODING_EOL_INCONSISTENT
)
4668 eol
= Fget (XCONS (tmp
)->car
, Qeol_type
);
4670 XCONS (tmp
)->car
= XVECTOR (eol
)->contents
[eol_type
];
4673 return (highest
? XCONS (val
)->car
: val
);
4676 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
4678 "Detect coding system of the text in the region between START and END.\n\
4679 Return a list of possible coding systems ordered by priority.\n\
4681 If only ASCII characters are found, it returns a list of single element\n\
4682 `undecided' or its subsidiary coding system according to a detected\n\
4683 end-of-line format.\n\
4685 If optional argument HIGHEST is non-nil, return the coding system of\n\
4687 (start
, end
, highest
)
4688 Lisp_Object start
, end
, highest
;
4691 int from_byte
, to_byte
;
4693 CHECK_NUMBER_COERCE_MARKER (start
, 0);
4694 CHECK_NUMBER_COERCE_MARKER (end
, 1);
4696 validate_region (&start
, &end
);
4697 from
= XINT (start
), to
= XINT (end
);
4698 from_byte
= CHAR_TO_BYTE (from
);
4699 to_byte
= CHAR_TO_BYTE (to
);
4701 if (from
< GPT
&& to
>= GPT
)
4702 move_gap_both (to
, to_byte
);
4704 return detect_coding_system (BYTE_POS_ADDR (from_byte
),
4705 to_byte
- from_byte
,
4709 DEFUN ("detect-coding-string", Fdetect_coding_string
, Sdetect_coding_string
,
4711 "Detect coding system of the text in STRING.\n\
4712 Return a list of possible coding systems ordered by priority.\n\
4714 If only ASCII characters are found, it returns a list of single element\n\
4715 `undecided' or its subsidiary coding system according to a detected\n\
4716 end-of-line format.\n\
4718 If optional argument HIGHEST is non-nil, return the coding system of\n\
4721 Lisp_Object string
, highest
;
4723 CHECK_STRING (string
, 0);
4725 return detect_coding_system (XSTRING (string
)->data
,
4726 STRING_BYTES (XSTRING (string
)),
4731 code_convert_region1 (start
, end
, coding_system
, encodep
)
4732 Lisp_Object start
, end
, coding_system
;
4735 struct coding_system coding
;
4738 CHECK_NUMBER_COERCE_MARKER (start
, 0);
4739 CHECK_NUMBER_COERCE_MARKER (end
, 1);
4740 CHECK_SYMBOL (coding_system
, 2);
4742 validate_region (&start
, &end
);
4743 from
= XFASTINT (start
);
4744 to
= XFASTINT (end
);
4746 if (NILP (coding_system
))
4747 return make_number (to
- from
);
4749 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
4750 error ("Invalid coding system: %s", XSYMBOL (coding_system
)->name
->data
);
4752 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
4753 code_convert_region (from
, CHAR_TO_BYTE (from
), to
, CHAR_TO_BYTE (to
),
4754 &coding
, encodep
, 1);
4755 Vlast_coding_system_used
= coding
.symbol
;
4756 return make_number (coding
.produced_char
);
4759 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
4760 3, 3, "r\nzCoding system: ",
4761 "Decode the current region by specified coding system.\n\
4762 When called from a program, takes three arguments:\n\
4763 START, END, and CODING-SYSTEM. START and END are buffer positions.\n\
4764 This function sets `last-coding-system-used' to the precise coding system\n\
4765 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is\n\
4766 not fully specified.)\n\
4767 It returns the length of the decoded text.")
4768 (start
, end
, coding_system
)
4769 Lisp_Object start
, end
, coding_system
;
4771 return code_convert_region1 (start
, end
, coding_system
, 0);
4774 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
4775 3, 3, "r\nzCoding system: ",
4776 "Encode the current region by specified coding system.\n\
4777 When called from a program, takes three arguments:\n\
4778 START, END, and CODING-SYSTEM. START and END are buffer positions.\n\
4779 This function sets `last-coding-system-used' to the precise coding system\n\
4780 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is\n\
4781 not fully specified.)\n\
4782 It returns the length of the encoded text.")
4783 (start
, end
, coding_system
)
4784 Lisp_Object start
, end
, coding_system
;
4786 return code_convert_region1 (start
, end
, coding_system
, 1);
4790 code_convert_string1 (string
, coding_system
, nocopy
, encodep
)
4791 Lisp_Object string
, coding_system
, nocopy
;
4794 struct coding_system coding
;
4796 CHECK_STRING (string
, 0);
4797 CHECK_SYMBOL (coding_system
, 1);
4799 if (NILP (coding_system
))
4800 return (NILP (nocopy
) ? Fcopy_sequence (string
) : string
);
4802 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
4803 error ("Invalid coding system: %s", XSYMBOL (coding_system
)->name
->data
);
4805 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
4806 Vlast_coding_system_used
= coding
.symbol
;
4807 return code_convert_string (string
, &coding
, encodep
, !NILP (nocopy
));
4810 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
4812 "Decode STRING which is encoded in CODING-SYSTEM, and return the result.\n\
4813 Optional arg NOCOPY non-nil means it is ok to return STRING itself\n\
4814 if the decoding operation is trivial.\n\
4815 This function sets `last-coding-system-used' to the precise coding system\n\
4816 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is\n\
4817 not fully specified.)")
4818 (string
, coding_system
, nocopy
)
4819 Lisp_Object string
, coding_system
, nocopy
;
4821 return code_convert_string1 (string
, coding_system
, nocopy
, 0);
4824 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
4826 "Encode STRING to CODING-SYSTEM, and return the result.\n\
4827 Optional arg NOCOPY non-nil means it is ok to return STRING itself\n\
4828 if the encoding operation is trivial.\n\
4829 This function sets `last-coding-system-used' to the precise coding system\n\
4830 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is\n\
4831 not fully specified.)")
4832 (string
, coding_system
, nocopy
)
4833 Lisp_Object string
, coding_system
, nocopy
;
4835 return code_convert_string1 (string
, coding_system
, nocopy
, 1);
4838 /* Encode or decode STRING according to CODING_SYSTEM.
4839 Do not set Vlast_coding_system_used. */
4842 code_convert_string_norecord (string
, coding_system
, encodep
)
4843 Lisp_Object string
, coding_system
;
4846 struct coding_system coding
;
4848 CHECK_STRING (string
, 0);
4849 CHECK_SYMBOL (coding_system
, 1);
4851 if (NILP (coding_system
))
4854 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
4855 error ("Invalid coding system: %s", XSYMBOL (coding_system
)->name
->data
);
4857 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
4858 return code_convert_string (string
, &coding
, encodep
, Qt
);
4861 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
4862 "Decode a JISX0208 character of shift-jis encoding.\n\
4863 CODE is the character code in SJIS.\n\
4864 Return the corresponding character.")
4868 unsigned char c1
, c2
, s1
, s2
;
4871 CHECK_NUMBER (code
, 0);
4872 s1
= (XFASTINT (code
)) >> 8, s2
= (XFASTINT (code
)) & 0xFF;
4873 DECODE_SJIS (s1
, s2
, c1
, c2
);
4874 XSETFASTINT (val
, MAKE_NON_ASCII_CHAR (charset_jisx0208
, c1
, c2
));
4878 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
4879 "Encode a JISX0208 character CHAR to SJIS coding system.\n\
4880 Return the corresponding character code in SJIS.")
4884 int charset
, c1
, c2
, s1
, s2
;
4887 CHECK_NUMBER (ch
, 0);
4888 SPLIT_CHAR (XFASTINT (ch
), charset
, c1
, c2
);
4889 if (charset
== charset_jisx0208
)
4891 ENCODE_SJIS (c1
, c2
, s1
, s2
);
4892 XSETFASTINT (val
, (s1
<< 8) | s2
);
4895 XSETFASTINT (val
, 0);
4899 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
4900 "Decode a Big5 character CODE of BIG5 coding system.\n\
4901 CODE is the character code in BIG5.\n\
4902 Return the corresponding character.")
4907 unsigned char b1
, b2
, c1
, c2
;
4910 CHECK_NUMBER (code
, 0);
4911 b1
= (XFASTINT (code
)) >> 8, b2
= (XFASTINT (code
)) & 0xFF;
4912 DECODE_BIG5 (b1
, b2
, charset
, c1
, c2
);
4913 XSETFASTINT (val
, MAKE_NON_ASCII_CHAR (charset
, c1
, c2
));
4917 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
4918 "Encode the Big5 character CHAR to BIG5 coding system.\n\
4919 Return the corresponding character code in Big5.")
4923 int charset
, c1
, c2
, b1
, b2
;
4926 CHECK_NUMBER (ch
, 0);
4927 SPLIT_CHAR (XFASTINT (ch
), charset
, c1
, c2
);
4928 if (charset
== charset_big5_1
|| charset
== charset_big5_2
)
4930 ENCODE_BIG5 (charset
, c1
, c2
, b1
, b2
);
4931 XSETFASTINT (val
, (b1
<< 8) | b2
);
4934 XSETFASTINT (val
, 0);
4938 DEFUN ("set-terminal-coding-system-internal",
4939 Fset_terminal_coding_system_internal
,
4940 Sset_terminal_coding_system_internal
, 1, 1, 0, "")
4942 Lisp_Object coding_system
;
4944 CHECK_SYMBOL (coding_system
, 0);
4945 setup_coding_system (Fcheck_coding_system (coding_system
), &terminal_coding
);
4946 /* We had better not send unsafe characters to terminal. */
4947 terminal_coding
.flags
|= CODING_FLAG_ISO_SAFE
;
4952 DEFUN ("set-safe-terminal-coding-system-internal",
4953 Fset_safe_terminal_coding_system_internal
,
4954 Sset_safe_terminal_coding_system_internal
, 1, 1, 0, "")
4956 Lisp_Object coding_system
;
4958 CHECK_SYMBOL (coding_system
, 0);
4959 setup_coding_system (Fcheck_coding_system (coding_system
),
4960 &safe_terminal_coding
);
4964 DEFUN ("terminal-coding-system",
4965 Fterminal_coding_system
, Sterminal_coding_system
, 0, 0, 0,
4966 "Return coding system specified for terminal output.")
4969 return terminal_coding
.symbol
;
4972 DEFUN ("set-keyboard-coding-system-internal",
4973 Fset_keyboard_coding_system_internal
,
4974 Sset_keyboard_coding_system_internal
, 1, 1, 0, "")
4976 Lisp_Object coding_system
;
4978 CHECK_SYMBOL (coding_system
, 0);
4979 setup_coding_system (Fcheck_coding_system (coding_system
), &keyboard_coding
);
4983 DEFUN ("keyboard-coding-system",
4984 Fkeyboard_coding_system
, Skeyboard_coding_system
, 0, 0, 0,
4985 "Return coding system specified for decoding keyboard input.")
4988 return keyboard_coding
.symbol
;
4992 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
4993 Sfind_operation_coding_system
, 1, MANY
, 0,
4994 "Choose a coding system for an operation based on the target name.\n\
4995 The value names a pair of coding systems: (DECODING-SYSTEM ENCODING-SYSTEM).\n\
4996 DECODING-SYSTEM is the coding system to use for decoding\n\
4997 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system\n\
4998 for encoding (in case OPERATION does encoding).\n\
5000 The first argument OPERATION specifies an I/O primitive:\n\
5001 For file I/O, `insert-file-contents' or `write-region'.\n\
5002 For process I/O, `call-process', `call-process-region', or `start-process'.\n\
5003 For network I/O, `open-network-stream'.\n\
5005 The remaining arguments should be the same arguments that were passed\n\
5006 to the primitive. Depending on which primitive, one of those arguments\n\
5007 is selected as the TARGET. For example, if OPERATION does file I/O,\n\
5008 whichever argument specifies the file name is TARGET.\n\
5010 TARGET has a meaning which depends on OPERATION:\n\
5011 For file I/O, TARGET is a file name.\n\
5012 For process I/O, TARGET is a process name.\n\
5013 For network I/O, TARGET is a service name or a port number\n\
5015 This function looks up what specified for TARGET in,\n\
5016 `file-coding-system-alist', `process-coding-system-alist',\n\
5017 or `network-coding-system-alist' depending on OPERATION.\n\
5018 They may specify a coding system, a cons of coding systems,\n\
5019 or a function symbol to call.\n\
5020 In the last case, we call the function with one argument,\n\
5021 which is a list of all the arguments given to this function.")
5026 Lisp_Object operation
, target_idx
, target
, val
;
5027 register Lisp_Object chain
;
5030 error ("Too few arguments");
5031 operation
= args
[0];
5032 if (!SYMBOLP (operation
)
5033 || !INTEGERP (target_idx
= Fget (operation
, Qtarget_idx
)))
5034 error ("Invalid first arguement");
5035 if (nargs
< 1 + XINT (target_idx
))
5036 error ("Too few arguments for operation: %s",
5037 XSYMBOL (operation
)->name
->data
);
5038 target
= args
[XINT (target_idx
) + 1];
5039 if (!(STRINGP (target
)
5040 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
5041 error ("Invalid %dth argument", XINT (target_idx
) + 1);
5043 chain
= ((EQ (operation
, Qinsert_file_contents
)
5044 || EQ (operation
, Qwrite_region
))
5045 ? Vfile_coding_system_alist
5046 : (EQ (operation
, Qopen_network_stream
)
5047 ? Vnetwork_coding_system_alist
5048 : Vprocess_coding_system_alist
));
5052 for (; CONSP (chain
); chain
= XCONS (chain
)->cdr
)
5055 elt
= XCONS (chain
)->car
;
5058 && ((STRINGP (target
)
5059 && STRINGP (XCONS (elt
)->car
)
5060 && fast_string_match (XCONS (elt
)->car
, target
) >= 0)
5061 || (INTEGERP (target
) && EQ (target
, XCONS (elt
)->car
))))
5063 val
= XCONS (elt
)->cdr
;
5064 /* Here, if VAL is both a valid coding system and a valid
5065 function symbol, we return VAL as a coding system. */
5068 if (! SYMBOLP (val
))
5070 if (! NILP (Fcoding_system_p (val
)))
5071 return Fcons (val
, val
);
5072 if (! NILP (Ffboundp (val
)))
5074 val
= call1 (val
, Flist (nargs
, args
));
5077 if (SYMBOLP (val
) && ! NILP (Fcoding_system_p (val
)))
5078 return Fcons (val
, val
);
5086 DEFUN ("update-coding-systems-internal", Fupdate_coding_systems_internal
,
5087 Supdate_coding_systems_internal
, 0, 0, 0,
5088 "Update internal database for ISO2022 and CCL based coding systems.\n\
5089 When values of the following coding categories are changed, you must\n\
5090 call this function:\n\
5091 coding-category-iso-7, coding-category-iso-7-tight,\n\
5092 coding-category-iso-8-1, coding-category-iso-8-2,\n\
5093 coding-category-iso-7-else, coding-category-iso-8-else,\n\
5094 coding-category-ccl")
5099 for (i
= CODING_CATEGORY_IDX_ISO_7
; i
<= CODING_CATEGORY_IDX_CCL
; i
++)
5103 val
= XSYMBOL (XVECTOR (Vcoding_category_table
)->contents
[i
])->value
;
5106 if (! coding_system_table
[i
])
5107 coding_system_table
[i
] = ((struct coding_system
*)
5108 xmalloc (sizeof (struct coding_system
)));
5109 setup_coding_system (val
, coding_system_table
[i
]);
5111 else if (coding_system_table
[i
])
5113 xfree (coding_system_table
[i
]);
5114 coding_system_table
[i
] = NULL
;
5121 DEFUN ("set-coding-priority-internal", Fset_coding_priority_internal
,
5122 Sset_coding_priority_internal
, 0, 0, 0,
5123 "Update internal database for the current value of `coding-category-list'.\n\
5124 This function is internal use only.")
5130 val
= Vcoding_category_list
;
5132 while (CONSP (val
) && i
< CODING_CATEGORY_IDX_MAX
)
5134 if (! SYMBOLP (XCONS (val
)->car
))
5136 idx
= XFASTINT (Fget (XCONS (val
)->car
, Qcoding_category_index
));
5137 if (idx
>= CODING_CATEGORY_IDX_MAX
)
5139 coding_priorities
[i
++] = (1 << idx
);
5140 val
= XCONS (val
)->cdr
;
5142 /* If coding-category-list is valid and contains all coding
5143 categories, `i' should be CODING_CATEGORY_IDX_MAX now. If not,
5144 the following code saves Emacs from craching. */
5145 while (i
< CODING_CATEGORY_IDX_MAX
)
5146 coding_priorities
[i
++] = CODING_CATEGORY_MASK_RAW_TEXT
;
5154 /*** 9. Post-amble ***/
5159 conversion_buffer
= (char *) xmalloc (MINIMUM_CONVERSION_BUFFER_SIZE
);
5167 /* Emacs' internal format specific initialize routine. */
5168 for (i
= 0; i
<= 0x20; i
++)
5169 emacs_code_class
[i
] = EMACS_control_code
;
5170 emacs_code_class
[0x0A] = EMACS_linefeed_code
;
5171 emacs_code_class
[0x0D] = EMACS_carriage_return_code
;
5172 for (i
= 0x21 ; i
< 0x7F; i
++)
5173 emacs_code_class
[i
] = EMACS_ascii_code
;
5174 emacs_code_class
[0x7F] = EMACS_control_code
;
5175 emacs_code_class
[0x80] = EMACS_leading_code_composition
;
5176 for (i
= 0x81; i
< 0xFF; i
++)
5177 emacs_code_class
[i
] = EMACS_invalid_code
;
5178 emacs_code_class
[LEADING_CODE_PRIVATE_11
] = EMACS_leading_code_3
;
5179 emacs_code_class
[LEADING_CODE_PRIVATE_12
] = EMACS_leading_code_3
;
5180 emacs_code_class
[LEADING_CODE_PRIVATE_21
] = EMACS_leading_code_4
;
5181 emacs_code_class
[LEADING_CODE_PRIVATE_22
] = EMACS_leading_code_4
;
5183 /* ISO2022 specific initialize routine. */
5184 for (i
= 0; i
< 0x20; i
++)
5185 iso_code_class
[i
] = ISO_control_code
;
5186 for (i
= 0x21; i
< 0x7F; i
++)
5187 iso_code_class
[i
] = ISO_graphic_plane_0
;
5188 for (i
= 0x80; i
< 0xA0; i
++)
5189 iso_code_class
[i
] = ISO_control_code
;
5190 for (i
= 0xA1; i
< 0xFF; i
++)
5191 iso_code_class
[i
] = ISO_graphic_plane_1
;
5192 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
5193 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
5194 iso_code_class
[ISO_CODE_CR
] = ISO_carriage_return
;
5195 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
5196 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
5197 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
5198 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
5199 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
5200 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
5201 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
5203 conversion_buffer_size
= MINIMUM_CONVERSION_BUFFER_SIZE
;
5205 setup_coding_system (Qnil
, &keyboard_coding
);
5206 setup_coding_system (Qnil
, &terminal_coding
);
5207 setup_coding_system (Qnil
, &safe_terminal_coding
);
5208 setup_coding_system (Qnil
, &default_buffer_file_coding
);
5210 bzero (coding_system_table
, sizeof coding_system_table
);
5212 bzero (ascii_skip_code
, sizeof ascii_skip_code
);
5213 for (i
= 0; i
< 128; i
++)
5214 ascii_skip_code
[i
] = 1;
5216 #if defined (MSDOS) || defined (WINDOWSNT)
5217 system_eol_type
= CODING_EOL_CRLF
;
5219 system_eol_type
= CODING_EOL_LF
;
5228 Qtarget_idx
= intern ("target-idx");
5229 staticpro (&Qtarget_idx
);
5231 Qcoding_system_history
= intern ("coding-system-history");
5232 staticpro (&Qcoding_system_history
);
5233 Fset (Qcoding_system_history
, Qnil
);
5235 /* Target FILENAME is the first argument. */
5236 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
5237 /* Target FILENAME is the third argument. */
5238 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
5240 Qcall_process
= intern ("call-process");
5241 staticpro (&Qcall_process
);
5242 /* Target PROGRAM is the first argument. */
5243 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
5245 Qcall_process_region
= intern ("call-process-region");
5246 staticpro (&Qcall_process_region
);
5247 /* Target PROGRAM is the third argument. */
5248 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
5250 Qstart_process
= intern ("start-process");
5251 staticpro (&Qstart_process
);
5252 /* Target PROGRAM is the third argument. */
5253 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
5255 Qopen_network_stream
= intern ("open-network-stream");
5256 staticpro (&Qopen_network_stream
);
5257 /* Target SERVICE is the fourth argument. */
5258 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
5260 Qcoding_system
= intern ("coding-system");
5261 staticpro (&Qcoding_system
);
5263 Qeol_type
= intern ("eol-type");
5264 staticpro (&Qeol_type
);
5266 Qbuffer_file_coding_system
= intern ("buffer-file-coding-system");
5267 staticpro (&Qbuffer_file_coding_system
);
5269 Qpost_read_conversion
= intern ("post-read-conversion");
5270 staticpro (&Qpost_read_conversion
);
5272 Qpre_write_conversion
= intern ("pre-write-conversion");
5273 staticpro (&Qpre_write_conversion
);
5275 Qno_conversion
= intern ("no-conversion");
5276 staticpro (&Qno_conversion
);
5278 Qundecided
= intern ("undecided");
5279 staticpro (&Qundecided
);
5281 Qcoding_system_p
= intern ("coding-system-p");
5282 staticpro (&Qcoding_system_p
);
5284 Qcoding_system_error
= intern ("coding-system-error");
5285 staticpro (&Qcoding_system_error
);
5287 Fput (Qcoding_system_error
, Qerror_conditions
,
5288 Fcons (Qcoding_system_error
, Fcons (Qerror
, Qnil
)));
5289 Fput (Qcoding_system_error
, Qerror_message
,
5290 build_string ("Invalid coding system"));
5292 Qcoding_category
= intern ("coding-category");
5293 staticpro (&Qcoding_category
);
5294 Qcoding_category_index
= intern ("coding-category-index");
5295 staticpro (&Qcoding_category_index
);
5297 Vcoding_category_table
5298 = Fmake_vector (make_number (CODING_CATEGORY_IDX_MAX
), Qnil
);
5299 staticpro (&Vcoding_category_table
);
5302 for (i
= 0; i
< CODING_CATEGORY_IDX_MAX
; i
++)
5304 XVECTOR (Vcoding_category_table
)->contents
[i
]
5305 = intern (coding_category_name
[i
]);
5306 Fput (XVECTOR (Vcoding_category_table
)->contents
[i
],
5307 Qcoding_category_index
, make_number (i
));
5311 Qtranslation_table
= intern ("translation-table");
5312 staticpro (&Qtranslation_table
);
5313 Fput (Qtranslation_table
, Qchar_table_extra_slots
, make_number (1));
5315 Qtranslation_table_id
= intern ("translation-table-id");
5316 staticpro (&Qtranslation_table_id
);
5318 Qtranslation_table_for_decode
= intern ("translation-table-for-decode");
5319 staticpro (&Qtranslation_table_for_decode
);
5321 Qtranslation_table_for_encode
= intern ("translation-table-for-encode");
5322 staticpro (&Qtranslation_table_for_encode
);
5324 Qsafe_charsets
= intern ("safe-charsets");
5325 staticpro (&Qsafe_charsets
);
5327 Qvalid_codes
= intern ("valid-codes");
5328 staticpro (&Qvalid_codes
);
5330 Qemacs_mule
= intern ("emacs-mule");
5331 staticpro (&Qemacs_mule
);
5333 Qraw_text
= intern ("raw-text");
5334 staticpro (&Qraw_text
);
5336 defsubr (&Scoding_system_p
);
5337 defsubr (&Sread_coding_system
);
5338 defsubr (&Sread_non_nil_coding_system
);
5339 defsubr (&Scheck_coding_system
);
5340 defsubr (&Sdetect_coding_region
);
5341 defsubr (&Sdetect_coding_string
);
5342 defsubr (&Sdecode_coding_region
);
5343 defsubr (&Sencode_coding_region
);
5344 defsubr (&Sdecode_coding_string
);
5345 defsubr (&Sencode_coding_string
);
5346 defsubr (&Sdecode_sjis_char
);
5347 defsubr (&Sencode_sjis_char
);
5348 defsubr (&Sdecode_big5_char
);
5349 defsubr (&Sencode_big5_char
);
5350 defsubr (&Sset_terminal_coding_system_internal
);
5351 defsubr (&Sset_safe_terminal_coding_system_internal
);
5352 defsubr (&Sterminal_coding_system
);
5353 defsubr (&Sset_keyboard_coding_system_internal
);
5354 defsubr (&Skeyboard_coding_system
);
5355 defsubr (&Sfind_operation_coding_system
);
5356 defsubr (&Supdate_coding_systems_internal
);
5357 defsubr (&Sset_coding_priority_internal
);
5359 DEFVAR_LISP ("coding-system-list", &Vcoding_system_list
,
5360 "List of coding systems.\n\
5362 Do not alter the value of this variable manually. This variable should be\n\
5363 updated by the functions `make-coding-system' and\n\
5364 `define-coding-system-alias'.");
5365 Vcoding_system_list
= Qnil
;
5367 DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist
,
5368 "Alist of coding system names.\n\
5369 Each element is one element list of coding system name.\n\
5370 This variable is given to `completing-read' as TABLE argument.\n\
5372 Do not alter the value of this variable manually. This variable should be\n\
5373 updated by the functions `make-coding-system' and\n\
5374 `define-coding-system-alias'.");
5375 Vcoding_system_alist
= Qnil
;
5377 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list
,
5378 "List of coding-categories (symbols) ordered by priority.");
5382 Vcoding_category_list
= Qnil
;
5383 for (i
= CODING_CATEGORY_IDX_MAX
- 1; i
>= 0; i
--)
5384 Vcoding_category_list
5385 = Fcons (XVECTOR (Vcoding_category_table
)->contents
[i
],
5386 Vcoding_category_list
);
5389 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read
,
5390 "Specify the coding system for read operations.\n\
5391 It is useful to bind this variable with `let', but do not set it globally.\n\
5392 If the value is a coding system, it is used for decoding on read operation.\n\
5393 If not, an appropriate element is used from one of the coding system alists:\n\
5394 There are three such tables, `file-coding-system-alist',\n\
5395 `process-coding-system-alist', and `network-coding-system-alist'.");
5396 Vcoding_system_for_read
= Qnil
;
5398 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write
,
5399 "Specify the coding system for write operations.\n\
5400 It is useful to bind this variable with `let', but do not set it globally.\n\
5401 If the value is a coding system, it is used for encoding on write operation.\n\
5402 If not, an appropriate element is used from one of the coding system alists:\n\
5403 There are three such tables, `file-coding-system-alist',\n\
5404 `process-coding-system-alist', and `network-coding-system-alist'.");
5405 Vcoding_system_for_write
= Qnil
;
5407 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used
,
5408 "Coding system used in the latest file or process I/O.");
5409 Vlast_coding_system_used
= Qnil
;
5411 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion
,
5412 "*Non-nil inhibit code conversion of end-of-line format in any cases.");
5413 inhibit_eol_conversion
= 0;
5415 DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system
,
5416 "Non-nil means process buffer inherits coding system of process output.\n\
5417 Bind it to t if the process output is to be treated as if it were a file\n\
5418 read from some filesystem.");
5419 inherit_process_coding_system
= 0;
5421 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist
,
5422 "Alist to decide a coding system to use for a file I/O operation.\n\
5423 The format is ((PATTERN . VAL) ...),\n\
5424 where PATTERN is a regular expression matching a file name,\n\
5425 VAL is a coding system, a cons of coding systems, or a function symbol.\n\
5426 If VAL is a coding system, it is used for both decoding and encoding\n\
5427 the file contents.\n\
5428 If VAL is a cons of coding systems, the car part is used for decoding,\n\
5429 and the cdr part is used for encoding.\n\
5430 If VAL is a function symbol, the function must return a coding system\n\
5431 or a cons of coding systems which are used as above.\n\
5433 See also the function `find-operation-coding-system'.\n\
5434 and the variable `auto-coding-alist'.");
5435 Vfile_coding_system_alist
= Qnil
;
5437 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist
,
5438 "Alist to decide a coding system to use for a process I/O operation.\n\
5439 The format is ((PATTERN . VAL) ...),\n\
5440 where PATTERN is a regular expression matching a program name,\n\
5441 VAL is a coding system, a cons of coding systems, or a function symbol.\n\
5442 If VAL is a coding system, it is used for both decoding what received\n\
5443 from the program and encoding what sent to the program.\n\
5444 If VAL is a cons of coding systems, the car part is used for decoding,\n\
5445 and the cdr part is used for encoding.\n\
5446 If VAL is a function symbol, the function must return a coding system\n\
5447 or a cons of coding systems which are used as above.\n\
5449 See also the function `find-operation-coding-system'.");
5450 Vprocess_coding_system_alist
= Qnil
;
5452 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist
,
5453 "Alist to decide a coding system to use for a network I/O operation.\n\
5454 The format is ((PATTERN . VAL) ...),\n\
5455 where PATTERN is a regular expression matching a network service name\n\
5456 or is a port number to connect to,\n\
5457 VAL is a coding system, a cons of coding systems, or a function symbol.\n\
5458 If VAL is a coding system, it is used for both decoding what received\n\
5459 from the network stream and encoding what sent to the network stream.\n\
5460 If VAL is a cons of coding systems, the car part is used for decoding,\n\
5461 and the cdr part is used for encoding.\n\
5462 If VAL is a function symbol, the function must return a coding system\n\
5463 or a cons of coding systems which are used as above.\n\
5465 See also the function `find-operation-coding-system'.");
5466 Vnetwork_coding_system_alist
= Qnil
;
5468 DEFVAR_INT ("eol-mnemonic-unix", &eol_mnemonic_unix
,
5469 "Mnemonic character indicating UNIX-like end-of-line format (i.e. LF) .");
5470 eol_mnemonic_unix
= ':';
5472 DEFVAR_INT ("eol-mnemonic-dos", &eol_mnemonic_dos
,
5473 "Mnemonic character indicating DOS-like end-of-line format (i.e. CRLF).");
5474 eol_mnemonic_dos
= '\\';
5476 DEFVAR_INT ("eol-mnemonic-mac", &eol_mnemonic_mac
,
5477 "Mnemonic character indicating MAC-like end-of-line format (i.e. CR).");
5478 eol_mnemonic_mac
= '/';
5480 DEFVAR_INT ("eol-mnemonic-undecided", &eol_mnemonic_undecided
,
5481 "Mnemonic character indicating end-of-line format is not yet decided.");
5482 eol_mnemonic_undecided
= ':';
5484 DEFVAR_LISP ("enable-character-translation", &Venable_character_translation
,
5485 "*Non-nil enables character translation while encoding and decoding.");
5486 Venable_character_translation
= Qt
;
5488 DEFVAR_LISP ("standard-translation-table-for-decode",
5489 &Vstandard_translation_table_for_decode
,
5490 "Table for translating characters while decoding.");
5491 Vstandard_translation_table_for_decode
= Qnil
;
5493 DEFVAR_LISP ("standard-translation-table-for-encode",
5494 &Vstandard_translation_table_for_encode
,
5495 "Table for translationg characters while encoding.");
5496 Vstandard_translation_table_for_encode
= Qnil
;
5498 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_alist
,
5499 "Alist of charsets vs revision numbers.\n\
5500 While encoding, if a charset (car part of an element) is found,\n\
5501 designate it with the escape sequence identifing revision (cdr part of the element).");
5502 Vcharset_revision_alist
= Qnil
;
5504 DEFVAR_LISP ("default-process-coding-system",
5505 &Vdefault_process_coding_system
,
5506 "Cons of coding systems used for process I/O by default.\n\
5507 The car part is used for decoding a process output,\n\
5508 the cdr part is used for encoding a text to be sent to a process.");
5509 Vdefault_process_coding_system
= Qnil
;
5511 DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table
,
5512 "Table of extra Latin codes in the range 128..159 (inclusive).\n\
5513 This is a vector of length 256.\n\
5514 If Nth element is non-nil, the existence of code N in a file\n\
5515 \(or output of subprocess) doesn't prevent it to be detected as\n\
5516 a coding system of ISO 2022 variant which has a flag\n\
5517 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file\n\
5518 or reading output of a subprocess.\n\
5519 Only 128th through 159th elements has a meaning.");
5520 Vlatin_extra_code_table
= Fmake_vector (make_number (256), Qnil
);
5522 DEFVAR_LISP ("select-safe-coding-system-function",
5523 &Vselect_safe_coding_system_function
,
5524 "Function to call to select safe coding system for encoding a text.\n\
5526 If set, this function is called to force a user to select a proper\n\
5527 coding system which can encode the text in the case that a default\n\
5528 coding system used in each operation can't encode the text.\n\
5530 The default value is `select-safe-codign-system' (which see).");
5531 Vselect_safe_coding_system_function
= Qnil
;