1 /* Coding system handler (conversion, detection, and etc).
2 Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 Copyright (C) 1995, 1997, 1998, 2002, 2003, 2004, 2005
4 National Institute of Advanced Industrial Science and Technology (AIST)
5 Registration Number H14PRO021
7 This file is part of GNU Emacs.
9 GNU Emacs is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs; see the file COPYING. If not, write to
21 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
24 /*** TABLE OF CONTENTS ***
28 2. Emacs' internal format (emacs-mule) handlers
30 4. Shift-JIS and BIG5 handlers
32 6. End-of-line handlers
33 7. C library functions
34 8. Emacs Lisp library functions
39 /*** 0. General comments ***/
42 /*** GENERAL NOTE on CODING SYSTEMS ***
44 A coding system is an encoding mechanism for one or more character
45 sets. Here's a list of coding systems which Emacs can handle. When
46 we say "decode", it means converting some other coding system to
47 Emacs' internal format (emacs-mule), and when we say "encode",
48 it means converting the coding system emacs-mule to some other
51 0. Emacs' internal format (emacs-mule)
53 Emacs itself holds a multi-lingual character in buffers and strings
54 in a special format. Details are described in section 2.
58 The most famous coding system for multiple character sets. X's
59 Compound Text, various EUCs (Extended Unix Code), and coding
60 systems used in Internet communication such as ISO-2022-JP are
61 all variants of ISO2022. Details are described in section 3.
63 2. SJIS (or Shift-JIS or MS-Kanji-Code)
65 A coding system to encode character sets: ASCII, JISX0201, and
66 JISX0208. Widely used for PC's in Japan. Details are described in
71 A coding system to encode the character sets ASCII and Big5. Widely
72 used for Chinese (mainly in Taiwan and Hong Kong). Details are
73 described in section 4. In this file, when we write "BIG5"
74 (all uppercase), we mean the coding system, and when we write
75 "Big5" (capitalized), we mean the character set.
79 A coding system for text containing random 8-bit code. Emacs does
80 no code conversion on such text except for end-of-line format.
84 If a user wants to read/write text encoded in a coding system not
85 listed above, he can supply a decoder and an encoder for it as CCL
86 (Code Conversion Language) programs. Emacs executes the CCL program
87 while reading/writing.
89 Emacs represents a coding system by a Lisp symbol that has a property
90 `coding-system'. But, before actually using the coding system, the
91 information about it is set in a structure of type `struct
92 coding_system' for rapid processing. See section 6 for more details.
96 /*** GENERAL NOTES on END-OF-LINE FORMAT ***
98 How end-of-line of text is encoded depends on the operating system.
99 For instance, Unix's format is just one byte of `line-feed' code,
100 whereas DOS's format is two-byte sequence of `carriage-return' and
101 `line-feed' codes. MacOS's format is usually one byte of
104 Since text character encoding and end-of-line encoding are
105 independent, any coding system described above can have any
106 end-of-line format. So Emacs has information about end-of-line
107 format in each coding-system. See section 6 for more details.
111 /*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
113 These functions check if a text between SRC and SRC_END is encoded
114 in the coding system category XXX. Each returns an integer value in
115 which appropriate flag bits for the category XXX are set. The flag
116 bits are defined in macros CODING_CATEGORY_MASK_XXX. Below is the
117 template for these functions. If MULTIBYTEP is nonzero, 8-bit codes
118 of the range 0x80..0x9F are in multibyte form. */
121 detect_coding_emacs_mule (src
, src_end
, multibytep
)
122 unsigned char *src
, *src_end
;
129 /*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
131 These functions decode SRC_BYTES length of unibyte text at SOURCE
132 encoded in CODING to Emacs' internal format. The resulting
133 multibyte text goes to a place pointed to by DESTINATION, the length
134 of which should not exceed DST_BYTES.
136 These functions set the information about original and decoded texts
137 in the members `produced', `produced_char', `consumed', and
138 `consumed_char' of the structure *CODING. They also set the member
139 `result' to one of CODING_FINISH_XXX indicating how the decoding
142 DST_BYTES zero means that the source area and destination area are
143 overlapped, which means that we can produce a decoded text until it
144 reaches the head of the not-yet-decoded source text.
146 Below is a template for these functions. */
149 decode_coding_XXX (coding
, source
, destination
, src_bytes
, dst_bytes
)
150 struct coding_system
*coding
;
151 const unsigned char *source
;
152 unsigned char *destination
;
153 int src_bytes
, dst_bytes
;
159 /*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
161 These functions encode SRC_BYTES length text at SOURCE from Emacs'
162 internal multibyte format to CODING. The resulting unibyte text
163 goes to a place pointed to by DESTINATION, the length of which
164 should not exceed DST_BYTES.
166 These functions set the information about original and encoded texts
167 in the members `produced', `produced_char', `consumed', and
168 `consumed_char' of the structure *CODING. They also set the member
169 `result' to one of CODING_FINISH_XXX indicating how the encoding
172 DST_BYTES zero means that the source area and destination area are
173 overlapped, which means that we can produce encoded text until it
174 reaches at the head of the not-yet-encoded source text.
176 Below is a template for these functions. */
179 encode_coding_XXX (coding
, source
, destination
, src_bytes
, dst_bytes
)
180 struct coding_system
*coding
;
181 unsigned char *source
, *destination
;
182 int src_bytes
, dst_bytes
;
188 /*** COMMONLY USED MACROS ***/
190 /* The following two macros ONE_MORE_BYTE and TWO_MORE_BYTES safely
191 get one, two, and three bytes from the source text respectively.
192 If there are not enough bytes in the source, they jump to
193 `label_end_of_loop'. The caller should set variables `coding',
194 `src' and `src_end' to appropriate pointer in advance. These
195 macros are called from decoding routines `decode_coding_XXX', thus
196 it is assumed that the source text is unibyte. */
198 #define ONE_MORE_BYTE(c1) \
200 if (src >= src_end) \
202 coding->result = CODING_FINISH_INSUFFICIENT_SRC; \
203 goto label_end_of_loop; \
208 #define TWO_MORE_BYTES(c1, c2) \
210 if (src + 1 >= src_end) \
212 coding->result = CODING_FINISH_INSUFFICIENT_SRC; \
213 goto label_end_of_loop; \
220 /* Like ONE_MORE_BYTE, but 8-bit bytes of data at SRC are in multibyte
221 form if MULTIBYTEP is nonzero. */
223 #define ONE_MORE_BYTE_CHECK_MULTIBYTE(c1, multibytep) \
225 if (src >= src_end) \
227 coding->result = CODING_FINISH_INSUFFICIENT_SRC; \
228 goto label_end_of_loop; \
231 if (multibytep && c1 == LEADING_CODE_8_BIT_CONTROL) \
232 c1 = *src++ - 0x20; \
235 /* Set C to the next character at the source text pointed by `src'.
236 If there are not enough characters in the source, jump to
237 `label_end_of_loop'. The caller should set variables `coding'
238 `src', `src_end', and `translation_table' to appropriate pointers
239 in advance. This macro is used in encoding routines
240 `encode_coding_XXX', thus it assumes that the source text is in
241 multibyte form except for 8-bit characters. 8-bit characters are
242 in multibyte form if coding->src_multibyte is nonzero, else they
243 are represented by a single byte. */
245 #define ONE_MORE_CHAR(c) \
247 int len = src_end - src; \
251 coding->result = CODING_FINISH_INSUFFICIENT_SRC; \
252 goto label_end_of_loop; \
254 if (coding->src_multibyte \
255 || UNIBYTE_STR_AS_MULTIBYTE_P (src, len, bytes)) \
256 c = STRING_CHAR_AND_LENGTH (src, len, bytes); \
258 c = *src, bytes = 1; \
259 if (!NILP (translation_table)) \
260 c = translate_char (translation_table, c, -1, 0, 0); \
265 /* Produce a multibyte form of character C to `dst'. Jump to
266 `label_end_of_loop' if there's not enough space at `dst'.
268 If we are now in the middle of a composition sequence, the decoded
269 character may be ALTCHAR (for the current composition). In that
270 case, the character goes to coding->cmp_data->data instead of
273 This macro is used in decoding routines. */
275 #define EMIT_CHAR(c) \
277 if (! COMPOSING_P (coding) \
278 || coding->composing == COMPOSITION_RELATIVE \
279 || coding->composing == COMPOSITION_WITH_RULE) \
281 int bytes = CHAR_BYTES (c); \
282 if ((dst + bytes) > (dst_bytes ? dst_end : src)) \
284 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
285 goto label_end_of_loop; \
287 dst += CHAR_STRING (c, dst); \
288 coding->produced_char++; \
291 if (COMPOSING_P (coding) \
292 && coding->composing != COMPOSITION_RELATIVE) \
294 CODING_ADD_COMPOSITION_COMPONENT (coding, c); \
295 coding->composition_rule_follows \
296 = coding->composing != COMPOSITION_WITH_ALTCHARS; \
301 #define EMIT_ONE_BYTE(c) \
303 if (dst >= (dst_bytes ? dst_end : src)) \
305 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
306 goto label_end_of_loop; \
311 #define EMIT_TWO_BYTES(c1, c2) \
313 if (dst + 2 > (dst_bytes ? dst_end : src)) \
315 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
316 goto label_end_of_loop; \
318 *dst++ = c1, *dst++ = c2; \
321 #define EMIT_BYTES(from, to) \
323 if (dst + (to - from) > (dst_bytes ? dst_end : src)) \
325 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
326 goto label_end_of_loop; \
333 /*** 1. Preamble ***/
346 #include "composite.h"
350 #include "intervals.h"
352 #else /* not emacs */
356 #endif /* not emacs */
358 Lisp_Object Qcoding_system
, Qeol_type
;
359 Lisp_Object Qbuffer_file_coding_system
;
360 Lisp_Object Qpost_read_conversion
, Qpre_write_conversion
;
361 Lisp_Object Qno_conversion
, Qundecided
;
362 Lisp_Object Qcoding_system_history
;
363 Lisp_Object Qsafe_chars
;
364 Lisp_Object Qvalid_codes
;
366 extern Lisp_Object Qinsert_file_contents
, Qwrite_region
;
367 Lisp_Object Qcall_process
, Qcall_process_region
;
368 Lisp_Object Qstart_process
, Qopen_network_stream
;
369 Lisp_Object Qtarget_idx
;
371 /* If a symbol has this property, evaluate the value to define the
372 symbol as a coding system. */
373 Lisp_Object Qcoding_system_define_form
;
375 Lisp_Object Vselect_safe_coding_system_function
;
377 int coding_system_require_warning
;
379 /* Mnemonic string for each format of end-of-line. */
380 Lisp_Object eol_mnemonic_unix
, eol_mnemonic_dos
, eol_mnemonic_mac
;
381 /* Mnemonic string to indicate format of end-of-line is not yet
383 Lisp_Object eol_mnemonic_undecided
;
385 /* Format of end-of-line decided by system. This is CODING_EOL_LF on
386 Unix, CODING_EOL_CRLF on DOS/Windows, and CODING_EOL_CR on Mac. */
391 /* Information about which coding system is safe for which chars.
392 The value has the form (GENERIC-LIST . NON-GENERIC-ALIST).
394 GENERIC-LIST is a list of generic coding systems which can encode
397 NON-GENERIC-ALIST is an alist of non generic coding systems vs the
398 corresponding char table that contains safe chars. */
399 Lisp_Object Vcoding_system_safe_chars
;
401 Lisp_Object Vcoding_system_list
, Vcoding_system_alist
;
403 Lisp_Object Qcoding_system_p
, Qcoding_system_error
;
405 /* Coding system emacs-mule and raw-text are for converting only
406 end-of-line format. */
407 Lisp_Object Qemacs_mule
, Qraw_text
;
411 /* Coding-systems are handed between Emacs Lisp programs and C internal
412 routines by the following three variables. */
413 /* Coding-system for reading files and receiving data from process. */
414 Lisp_Object Vcoding_system_for_read
;
415 /* Coding-system for writing files and sending data to process. */
416 Lisp_Object Vcoding_system_for_write
;
417 /* Coding-system actually used in the latest I/O. */
418 Lisp_Object Vlast_coding_system_used
;
420 /* A vector of length 256 which contains information about special
421 Latin codes (especially for dealing with Microsoft codes). */
422 Lisp_Object Vlatin_extra_code_table
;
424 /* Flag to inhibit code conversion of end-of-line format. */
425 int inhibit_eol_conversion
;
427 /* Flag to inhibit ISO2022 escape sequence detection. */
428 int inhibit_iso_escape_detection
;
430 /* Flag to make buffer-file-coding-system inherit from process-coding. */
431 int inherit_process_coding_system
;
433 /* Coding system to be used to encode text for terminal display. */
434 struct coding_system terminal_coding
;
436 /* Coding system to be used to encode text for terminal display when
437 terminal coding system is nil. */
438 struct coding_system safe_terminal_coding
;
440 /* Coding system of what is sent from terminal keyboard. */
441 struct coding_system keyboard_coding
;
443 /* Default coding system to be used to write a file. */
444 struct coding_system default_buffer_file_coding
;
446 Lisp_Object Vfile_coding_system_alist
;
447 Lisp_Object Vprocess_coding_system_alist
;
448 Lisp_Object Vnetwork_coding_system_alist
;
450 Lisp_Object Vlocale_coding_system
;
454 Lisp_Object Qcoding_category
, Qcoding_category_index
;
456 /* List of symbols `coding-category-xxx' ordered by priority. */
457 Lisp_Object Vcoding_category_list
;
459 /* Table of coding categories (Lisp symbols). */
460 Lisp_Object Vcoding_category_table
;
462 /* Table of names of symbol for each coding-category. */
463 char *coding_category_name
[CODING_CATEGORY_IDX_MAX
] = {
464 "coding-category-emacs-mule",
465 "coding-category-sjis",
466 "coding-category-iso-7",
467 "coding-category-iso-7-tight",
468 "coding-category-iso-8-1",
469 "coding-category-iso-8-2",
470 "coding-category-iso-7-else",
471 "coding-category-iso-8-else",
472 "coding-category-ccl",
473 "coding-category-big5",
474 "coding-category-utf-8",
475 "coding-category-utf-16-be",
476 "coding-category-utf-16-le",
477 "coding-category-raw-text",
478 "coding-category-binary"
481 /* Table of pointers to coding systems corresponding to each coding
483 struct coding_system
*coding_system_table
[CODING_CATEGORY_IDX_MAX
];
485 /* Table of coding category masks. Nth element is a mask for a coding
486 category of which priority is Nth. */
488 int coding_priorities
[CODING_CATEGORY_IDX_MAX
];
490 /* Flag to tell if we look up translation table on character code
492 Lisp_Object Venable_character_translation
;
493 /* Standard translation table to look up on decoding (reading). */
494 Lisp_Object Vstandard_translation_table_for_decode
;
495 /* Standard translation table to look up on encoding (writing). */
496 Lisp_Object Vstandard_translation_table_for_encode
;
498 Lisp_Object Qtranslation_table
;
499 Lisp_Object Qtranslation_table_id
;
500 Lisp_Object Qtranslation_table_for_decode
;
501 Lisp_Object Qtranslation_table_for_encode
;
503 /* Alist of charsets vs revision number. */
504 Lisp_Object Vcharset_revision_alist
;
506 /* Default coding systems used for process I/O. */
507 Lisp_Object Vdefault_process_coding_system
;
509 /* Char table for translating Quail and self-inserting input. */
510 Lisp_Object Vtranslation_table_for_input
;
512 /* Global flag to tell that we can't call post-read-conversion and
513 pre-write-conversion functions. Usually the value is zero, but it
514 is set to 1 temporarily while such functions are running. This is
515 to avoid infinite recursive call. */
516 static int inhibit_pre_post_conversion
;
518 Lisp_Object Qchar_coding_system
;
520 /* Return `safe-chars' property of CODING_SYSTEM (symbol). Don't check
524 coding_safe_chars (coding_system
)
525 Lisp_Object coding_system
;
527 Lisp_Object coding_spec
, plist
, safe_chars
;
529 coding_spec
= Fget (coding_system
, Qcoding_system
);
530 plist
= XVECTOR (coding_spec
)->contents
[3];
531 safe_chars
= Fplist_get (XVECTOR (coding_spec
)->contents
[3], Qsafe_chars
);
532 return (CHAR_TABLE_P (safe_chars
) ? safe_chars
: Qt
);
535 #define CODING_SAFE_CHAR_P(safe_chars, c) \
536 (EQ (safe_chars, Qt) || !NILP (CHAR_TABLE_REF (safe_chars, c)))
539 /*** 2. Emacs internal format (emacs-mule) handlers ***/
541 /* Emacs' internal format for representation of multiple character
542 sets is a kind of multi-byte encoding, i.e. characters are
543 represented by variable-length sequences of one-byte codes.
545 ASCII characters and control characters (e.g. `tab', `newline') are
546 represented by one-byte sequences which are their ASCII codes, in
547 the range 0x00 through 0x7F.
549 8-bit characters of the range 0x80..0x9F are represented by
550 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
553 8-bit characters of the range 0xA0..0xFF are represented by
554 one-byte sequences which are their 8-bit code.
556 The other characters are represented by a sequence of `base
557 leading-code', optional `extended leading-code', and one or two
558 `position-code's. The length of the sequence is determined by the
559 base leading-code. Leading-code takes the range 0x81 through 0x9D,
560 whereas extended leading-code and position-code take the range 0xA0
561 through 0xFF. See `charset.h' for more details about leading-code
564 --- CODE RANGE of Emacs' internal format ---
568 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
569 eight-bit-graphic 0xA0..0xBF
570 ELSE 0x81..0x9D + [0xA0..0xFF]+
571 ---------------------------------------------
573 As this is the internal character representation, the format is
574 usually not used externally (i.e. in a file or in a data sent to a
575 process). But, it is possible to have a text externally in this
576 format (i.e. by encoding by the coding system `emacs-mule').
578 In that case, a sequence of one-byte codes has a slightly different
581 Firstly, all characters in eight-bit-control are represented by
582 one-byte sequences which are their 8-bit code.
584 Next, character composition data are represented by the byte
585 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
587 METHOD is 0xF0 plus one of composition method (enum
590 BYTES is 0xA0 plus the byte length of these composition data,
592 CHARS is 0xA0 plus the number of characters composed by these
595 COMPONENTs are characters of multibyte form or composition
596 rules encoded by two-byte of ASCII codes.
598 In addition, for backward compatibility, the following formats are
599 also recognized as composition data on decoding.
602 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
605 MSEQ is a multibyte form but in these special format:
606 ASCII: 0xA0 ASCII_CODE+0x80,
607 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
608 RULE is a one byte code of the range 0xA0..0xF0 that
609 represents a composition rule.
612 enum emacs_code_class_type emacs_code_class
[256];
614 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
615 Check if a text is encoded in Emacs' internal format. If it is,
616 return CODING_CATEGORY_MASK_EMACS_MULE, else return 0. */
619 detect_coding_emacs_mule (src
, src_end
, multibytep
)
620 unsigned char *src
, *src_end
;
625 /* Dummy for ONE_MORE_BYTE. */
626 struct coding_system dummy_coding
;
627 struct coding_system
*coding
= &dummy_coding
;
631 ONE_MORE_BYTE_CHECK_MULTIBYTE (c
, multibytep
);
639 ONE_MORE_BYTE_CHECK_MULTIBYTE (c
, multibytep
);
648 if (c
== ISO_CODE_ESC
|| c
== ISO_CODE_SI
|| c
== ISO_CODE_SO
)
651 else if (c
>= 0x80 && c
< 0xA0)
654 /* Old leading code for a composite character. */
658 unsigned char *src_base
= src
- 1;
661 if (!UNIBYTE_STR_AS_MULTIBYTE_P (src_base
, src_end
- src_base
,
664 src
= src_base
+ bytes
;
669 return CODING_CATEGORY_MASK_EMACS_MULE
;
673 /* Record the starting position START and METHOD of one composition. */
675 #define CODING_ADD_COMPOSITION_START(coding, start, method) \
677 struct composition_data *cmp_data = coding->cmp_data; \
678 int *data = cmp_data->data + cmp_data->used; \
679 coding->cmp_data_start = cmp_data->used; \
681 data[1] = cmp_data->char_offset + start; \
682 data[3] = (int) method; \
683 cmp_data->used += 4; \
686 /* Record the ending position END of the current composition. */
688 #define CODING_ADD_COMPOSITION_END(coding, end) \
690 struct composition_data *cmp_data = coding->cmp_data; \
691 int *data = cmp_data->data + coding->cmp_data_start; \
692 data[0] = cmp_data->used - coding->cmp_data_start; \
693 data[2] = cmp_data->char_offset + end; \
696 /* Record one COMPONENT (alternate character or composition rule). */
698 #define CODING_ADD_COMPOSITION_COMPONENT(coding, component) \
700 coding->cmp_data->data[coding->cmp_data->used++] = component; \
701 if (coding->cmp_data->used - coding->cmp_data_start \
702 == COMPOSITION_DATA_MAX_BUNCH_LENGTH) \
704 CODING_ADD_COMPOSITION_END (coding, coding->produced_char); \
705 coding->composing = COMPOSITION_NO; \
710 /* Get one byte from a data pointed by SRC and increment SRC. If SRC
711 is not less than SRC_END, return -1 without incrementing Src. */
713 #define SAFE_ONE_MORE_BYTE() (src >= src_end ? -1 : *src++)
716 /* Decode a character represented as a component of composition
717 sequence of Emacs 20 style at SRC. Set C to that character, store
718 its multibyte form sequence at P, and set P to the end of that
719 sequence. If no valid character is found, set C to -1. */
721 #define DECODE_EMACS_MULE_COMPOSITION_CHAR(c, p) \
725 c = SAFE_ONE_MORE_BYTE (); \
728 if (CHAR_HEAD_P (c)) \
730 else if (c == 0xA0) \
732 c = SAFE_ONE_MORE_BYTE (); \
741 else if (BASE_LEADING_CODE_P (c - 0x20)) \
743 unsigned char *p0 = p; \
747 bytes = BYTES_BY_CHAR_HEAD (c); \
750 c = SAFE_ONE_MORE_BYTE (); \
755 if (UNIBYTE_STR_AS_MULTIBYTE_P (p0, p - p0, bytes) \
756 || (coding->flags /* We are recovering a file. */ \
757 && p0[0] == LEADING_CODE_8_BIT_CONTROL \
758 && ! CHAR_HEAD_P (p0[1]))) \
759 c = STRING_CHAR (p0, bytes); \
768 /* Decode a composition rule represented as a component of composition
769 sequence of Emacs 20 style at SRC. Set C to the rule. If not
770 valid rule is found, set C to -1. */
772 #define DECODE_EMACS_MULE_COMPOSITION_RULE(c) \
774 c = SAFE_ONE_MORE_BYTE (); \
776 if (c < 0 || c >= 81) \
780 gref = c / 9, nref = c % 9; \
781 c = COMPOSITION_ENCODE_RULE (gref, nref); \
786 /* Decode composition sequence encoded by `emacs-mule' at the source
787 pointed by SRC. SRC_END is the end of source. Store information
788 of the composition in CODING->cmp_data.
790 For backward compatibility, decode also a composition sequence of
791 Emacs 20 style. In that case, the composition sequence contains
792 characters that should be extracted into a buffer or string. Store
793 those characters at *DESTINATION in multibyte form.
795 If we encounter an invalid byte sequence, return 0.
796 If we encounter an insufficient source or destination, or
797 insufficient space in CODING->cmp_data, return 1.
798 Otherwise, return consumed bytes in the source.
802 decode_composition_emacs_mule (coding
, src
, src_end
,
803 destination
, dst_end
, dst_bytes
)
804 struct coding_system
*coding
;
805 const unsigned char *src
, *src_end
;
806 unsigned char **destination
, *dst_end
;
809 unsigned char *dst
= *destination
;
810 int method
, data_len
, nchars
;
811 const unsigned char *src_base
= src
++;
812 /* Store components of composition. */
813 int component
[COMPOSITION_DATA_MAX_BUNCH_LENGTH
];
815 /* Store multibyte form of characters to be composed. This is for
816 Emacs 20 style composition sequence. */
817 unsigned char buf
[MAX_COMPOSITION_COMPONENTS
* MAX_MULTIBYTE_LENGTH
];
818 unsigned char *bufp
= buf
;
819 int c
, i
, gref
, nref
;
821 if (coding
->cmp_data
->used
+ COMPOSITION_DATA_MAX_BUNCH_LENGTH
822 >= COMPOSITION_DATA_SIZE
)
824 coding
->result
= CODING_FINISH_INSUFFICIENT_CMP
;
829 if (c
- 0xF0 >= COMPOSITION_RELATIVE
830 && c
- 0xF0 <= COMPOSITION_WITH_RULE_ALTCHARS
)
835 with_rule
= (method
== COMPOSITION_WITH_RULE
836 || method
== COMPOSITION_WITH_RULE_ALTCHARS
);
840 || src_base
+ data_len
> src_end
)
846 for (ncomponent
= 0; src
< src_base
+ data_len
; ncomponent
++)
848 /* If it is longer than this, it can't be valid. */
849 if (ncomponent
>= COMPOSITION_DATA_MAX_BUNCH_LENGTH
)
852 if (ncomponent
% 2 && with_rule
)
854 ONE_MORE_BYTE (gref
);
856 ONE_MORE_BYTE (nref
);
858 c
= COMPOSITION_ENCODE_RULE (gref
, nref
);
863 if (UNIBYTE_STR_AS_MULTIBYTE_P (src
, src_end
- src
, bytes
)
864 || (coding
->flags
/* We are recovering a file. */
865 && src
[0] == LEADING_CODE_8_BIT_CONTROL
866 && ! CHAR_HEAD_P (src
[1])))
867 c
= STRING_CHAR (src
, bytes
);
872 component
[ncomponent
] = c
;
877 /* This may be an old Emacs 20 style format. See the comment at
878 the section 2 of this file. */
879 while (src
< src_end
&& !CHAR_HEAD_P (*src
)) src
++;
881 && !(coding
->mode
& CODING_MODE_LAST_BLOCK
))
882 goto label_end_of_loop
;
888 method
= COMPOSITION_RELATIVE
;
889 for (ncomponent
= 0; ncomponent
< MAX_COMPOSITION_COMPONENTS
;)
891 DECODE_EMACS_MULE_COMPOSITION_CHAR (c
, bufp
);
894 component
[ncomponent
++] = c
;
902 method
= COMPOSITION_WITH_RULE
;
904 DECODE_EMACS_MULE_COMPOSITION_CHAR (c
, bufp
);
909 ncomponent
< MAX_COMPOSITION_COMPONENTS
* 2 - 1;)
911 DECODE_EMACS_MULE_COMPOSITION_RULE (c
);
914 component
[ncomponent
++] = c
;
915 DECODE_EMACS_MULE_COMPOSITION_CHAR (c
, bufp
);
918 component
[ncomponent
++] = c
;
922 nchars
= (ncomponent
+ 1) / 2;
928 if (buf
== bufp
|| dst
+ (bufp
- buf
) <= (dst_bytes
? dst_end
: src
))
930 CODING_ADD_COMPOSITION_START (coding
, coding
->produced_char
, method
);
931 for (i
= 0; i
< ncomponent
; i
++)
932 CODING_ADD_COMPOSITION_COMPONENT (coding
, component
[i
]);
933 CODING_ADD_COMPOSITION_END (coding
, coding
->produced_char
+ nchars
);
936 unsigned char *p
= buf
;
937 EMIT_BYTES (p
, bufp
);
938 *destination
+= bufp
- buf
;
939 coding
->produced_char
+= nchars
;
941 return (src
- src_base
);
947 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
950 decode_coding_emacs_mule (coding
, source
, destination
, src_bytes
, dst_bytes
)
951 struct coding_system
*coding
;
952 const unsigned char *source
;
953 unsigned char *destination
;
954 int src_bytes
, dst_bytes
;
956 const unsigned char *src
= source
;
957 const unsigned char *src_end
= source
+ src_bytes
;
958 unsigned char *dst
= destination
;
959 unsigned char *dst_end
= destination
+ dst_bytes
;
960 /* SRC_BASE remembers the start position in source in each loop.
961 The loop will be exited when there's not enough source code, or
962 when there's not enough destination area to produce a
964 const unsigned char *src_base
;
966 coding
->produced_char
= 0;
967 while ((src_base
= src
) < src_end
)
969 unsigned char tmp
[MAX_MULTIBYTE_LENGTH
];
970 const unsigned char *p
;
977 if (coding
->eol_type
== CODING_EOL_CR
)
979 else if (coding
->eol_type
== CODING_EOL_CRLF
)
989 coding
->produced_char
++;
992 else if (*src
== '\n')
994 if ((coding
->eol_type
== CODING_EOL_CR
995 || coding
->eol_type
== CODING_EOL_CRLF
)
996 && coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
)
998 coding
->result
= CODING_FINISH_INCONSISTENT_EOL
;
999 goto label_end_of_loop
;
1002 coding
->produced_char
++;
1005 else if (*src
== 0x80 && coding
->cmp_data
)
1007 /* Start of composition data. */
1008 int consumed
= decode_composition_emacs_mule (coding
, src
, src_end
,
1012 goto label_end_of_loop
;
1013 else if (consumed
> 0)
1018 bytes
= CHAR_STRING (*src
, tmp
);
1022 else if (UNIBYTE_STR_AS_MULTIBYTE_P (src
, src_end
- src
, bytes
)
1023 || (coding
->flags
/* We are recovering a file. */
1024 && src
[0] == LEADING_CODE_8_BIT_CONTROL
1025 && ! CHAR_HEAD_P (src
[1])))
1034 bytes
= BYTES_BY_CHAR_HEAD (*src
);
1036 for (i
= 1; i
< bytes
; i
++)
1039 if (CHAR_HEAD_P (c
))
1044 bytes
= CHAR_STRING (*src_base
, tmp
);
1053 if (dst
+ bytes
>= (dst_bytes
? dst_end
: src
))
1055 coding
->result
= CODING_FINISH_INSUFFICIENT_DST
;
1058 while (bytes
--) *dst
++ = *p
++;
1059 coding
->produced_char
++;
1062 coding
->consumed
= coding
->consumed_char
= src_base
- source
;
1063 coding
->produced
= dst
- destination
;
1067 /* Encode composition data stored at DATA into a special byte sequence
1068 starting by 0x80. Update CODING->cmp_data_start and maybe
1069 CODING->cmp_data for the next call. */
1071 #define ENCODE_COMPOSITION_EMACS_MULE(coding, data) \
1073 unsigned char buf[1024], *p0 = buf, *p; \
1074 int len = data[0]; \
1078 buf[1] = 0xF0 + data[3]; /* METHOD */ \
1079 buf[3] = 0xA0 + (data[2] - data[1]); /* COMPOSED-CHARS */ \
1081 if (data[3] == COMPOSITION_WITH_RULE \
1082 || data[3] == COMPOSITION_WITH_RULE_ALTCHARS) \
1084 p += CHAR_STRING (data[4], p); \
1085 for (i = 5; i < len; i += 2) \
1088 COMPOSITION_DECODE_RULE (data[i], gref, nref); \
1089 *p++ = 0x20 + gref; \
1090 *p++ = 0x20 + nref; \
1091 p += CHAR_STRING (data[i + 1], p); \
1096 for (i = 4; i < len; i++) \
1097 p += CHAR_STRING (data[i], p); \
1099 buf[2] = 0xA0 + (p - buf); /* COMPONENTS-BYTES */ \
1101 if (dst + (p - buf) + 4 > (dst_bytes ? dst_end : src)) \
1103 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
1104 goto label_end_of_loop; \
1108 coding->cmp_data_start += data[0]; \
1109 if (coding->cmp_data_start == coding->cmp_data->used \
1110 && coding->cmp_data->next) \
1112 coding->cmp_data = coding->cmp_data->next; \
1113 coding->cmp_data_start = 0; \
1118 static void encode_eol
P_ ((struct coding_system
*, const unsigned char *,
1119 unsigned char *, int, int));
1122 encode_coding_emacs_mule (coding
, source
, destination
, src_bytes
, dst_bytes
)
1123 struct coding_system
*coding
;
1124 const unsigned char *source
;
1125 unsigned char *destination
;
1126 int src_bytes
, dst_bytes
;
1128 const unsigned char *src
= source
;
1129 const unsigned char *src_end
= source
+ src_bytes
;
1130 unsigned char *dst
= destination
;
1131 unsigned char *dst_end
= destination
+ dst_bytes
;
1132 const unsigned char *src_base
;
1137 Lisp_Object translation_table
;
1139 translation_table
= Qnil
;
1141 /* Optimization for the case that there's no composition. */
1142 if (!coding
->cmp_data
|| coding
->cmp_data
->used
== 0)
1144 encode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
);
1148 char_offset
= coding
->cmp_data
->char_offset
;
1149 data
= coding
->cmp_data
->data
+ coding
->cmp_data_start
;
1154 /* If SRC starts a composition, encode the information about the
1155 composition in advance. */
1156 if (coding
->cmp_data_start
< coding
->cmp_data
->used
1157 && char_offset
+ coding
->consumed_char
== data
[1])
1159 ENCODE_COMPOSITION_EMACS_MULE (coding
, data
);
1160 char_offset
= coding
->cmp_data
->char_offset
;
1161 data
= coding
->cmp_data
->data
+ coding
->cmp_data_start
;
1165 if (c
== '\n' && (coding
->eol_type
== CODING_EOL_CRLF
1166 || coding
->eol_type
== CODING_EOL_CR
))
1168 if (coding
->eol_type
== CODING_EOL_CRLF
)
1169 EMIT_TWO_BYTES ('\r', c
);
1171 EMIT_ONE_BYTE ('\r');
1173 else if (SINGLE_BYTE_CHAR_P (c
))
1175 if (coding
->flags
&& ! ASCII_BYTE_P (c
))
1177 /* As we are auto saving, retain the multibyte form for
1179 unsigned char buf
[MAX_MULTIBYTE_LENGTH
];
1180 int bytes
= CHAR_STRING (c
, buf
);
1183 EMIT_ONE_BYTE (buf
[0]);
1185 EMIT_TWO_BYTES (buf
[0], buf
[1]);
1191 EMIT_BYTES (src_base
, src
);
1192 coding
->consumed_char
++;
1195 coding
->consumed
= src_base
- source
;
1196 coding
->produced
= coding
->produced_char
= dst
- destination
;
1201 /*** 3. ISO2022 handlers ***/
1203 /* The following note describes the coding system ISO2022 briefly.
1204 Since the intention of this note is to help understand the
1205 functions in this file, some parts are NOT ACCURATE or are OVERLY
1206 SIMPLIFIED. For thorough understanding, please refer to the
1207 original document of ISO2022. This is equivalent to the standard
1208 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
1210 ISO2022 provides many mechanisms to encode several character sets
1211 in 7-bit and 8-bit environments. For 7-bit environments, all text
1212 is encoded using bytes less than 128. This may make the encoded
1213 text a little bit longer, but the text passes more easily through
1214 several types of gateway, some of which strip off the MSB (Most
1217 There are two kinds of character sets: control character sets and
1218 graphic character sets. The former contain control characters such
1219 as `newline' and `escape' to provide control functions (control
1220 functions are also provided by escape sequences). The latter
1221 contain graphic characters such as 'A' and '-'. Emacs recognizes
1222 two control character sets and many graphic character sets.
1224 Graphic character sets are classified into one of the following
1225 four classes, according to the number of bytes (DIMENSION) and
1226 number of characters in one dimension (CHARS) of the set:
1227 - DIMENSION1_CHARS94
1228 - DIMENSION1_CHARS96
1229 - DIMENSION2_CHARS94
1230 - DIMENSION2_CHARS96
1232 In addition, each character set is assigned an identification tag,
1233 unique for each set, called the "final character" (denoted as <F>
1234 hereafter). The <F> of each character set is decided by ECMA(*)
1235 when it is registered in ISO. The code range of <F> is 0x30..0x7F
1236 (0x30..0x3F are for private use only).
1238 Note (*): ECMA = European Computer Manufacturers Association
1240 Here are examples of graphic character sets [NAME(<F>)]:
1241 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
1242 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
1243 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
1244 o DIMENSION2_CHARS96 -- none for the moment
1246 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
1247 C0 [0x00..0x1F] -- control character plane 0
1248 GL [0x20..0x7F] -- graphic character plane 0
1249 C1 [0x80..0x9F] -- control character plane 1
1250 GR [0xA0..0xFF] -- graphic character plane 1
1252 A control character set is directly designated and invoked to C0 or
1253 C1 by an escape sequence. The most common case is that:
1254 - ISO646's control character set is designated/invoked to C0, and
1255 - ISO6429's control character set is designated/invoked to C1,
1256 and usually these designations/invocations are omitted in encoded
1257 text. In a 7-bit environment, only C0 can be used, and a control
1258 character for C1 is encoded by an appropriate escape sequence to
1259 fit into the environment. All control characters for C1 are
1260 defined to have corresponding escape sequences.
1262 A graphic character set is at first designated to one of four
1263 graphic registers (G0 through G3), then these graphic registers are
1264 invoked to GL or GR. These designations and invocations can be
1265 done independently. The most common case is that G0 is invoked to
1266 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
1267 these invocations and designations are omitted in encoded text.
1268 In a 7-bit environment, only GL can be used.
1270 When a graphic character set of CHARS94 is invoked to GL, codes
1271 0x20 and 0x7F of the GL area work as control characters SPACE and
1272 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
1275 There are two ways of invocation: locking-shift and single-shift.
1276 With locking-shift, the invocation lasts until the next different
1277 invocation, whereas with single-shift, the invocation affects the
1278 following character only and doesn't affect the locking-shift
1279 state. Invocations are done by the following control characters or
1282 ----------------------------------------------------------------------
1283 abbrev function cntrl escape seq description
1284 ----------------------------------------------------------------------
1285 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
1286 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
1287 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
1288 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
1289 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
1290 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
1291 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
1292 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
1293 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
1294 ----------------------------------------------------------------------
1295 (*) These are not used by any known coding system.
1297 Control characters for these functions are defined by macros
1298 ISO_CODE_XXX in `coding.h'.
1300 Designations are done by the following escape sequences:
1301 ----------------------------------------------------------------------
1302 escape sequence description
1303 ----------------------------------------------------------------------
1304 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
1305 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
1306 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
1307 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
1308 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
1309 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
1310 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
1311 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
1312 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
1313 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
1314 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
1315 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
1316 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
1317 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
1318 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
1319 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
1320 ----------------------------------------------------------------------
1322 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
1323 of dimension 1, chars 94, and final character <F>, etc...
1325 Note (*): Although these designations are not allowed in ISO2022,
1326 Emacs accepts them on decoding, and produces them on encoding
1327 CHARS96 character sets in a coding system which is characterized as
1328 7-bit environment, non-locking-shift, and non-single-shift.
1330 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
1331 '(' can be omitted. We refer to this as "short-form" hereafter.
1333 Now you may notice that there are a lot of ways of encoding the
1334 same multilingual text in ISO2022. Actually, there exist many
1335 coding systems such as Compound Text (used in X11's inter client
1336 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
1337 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
1338 localized platforms), and all of these are variants of ISO2022.
1340 In addition to the above, Emacs handles two more kinds of escape
1341 sequences: ISO6429's direction specification and Emacs' private
1342 sequence for specifying character composition.
1344 ISO6429's direction specification takes the following form:
1345 o CSI ']' -- end of the current direction
1346 o CSI '0' ']' -- end of the current direction
1347 o CSI '1' ']' -- start of left-to-right text
1348 o CSI '2' ']' -- start of right-to-left text
1349 The control character CSI (0x9B: control sequence introducer) is
1350 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
1352 Character composition specification takes the following form:
1353 o ESC '0' -- start relative composition
1354 o ESC '1' -- end composition
1355 o ESC '2' -- start rule-base composition (*)
1356 o ESC '3' -- start relative composition with alternate chars (**)
1357 o ESC '4' -- start rule-base composition with alternate chars (**)
1358 Since these are not standard escape sequences of any ISO standard,
1359 the use of them with these meanings is restricted to Emacs only.
1361 (*) This form is used only in Emacs 20.5 and older versions,
1362 but the newer versions can safely decode it.
1363 (**) This form is used only in Emacs 21.1 and newer versions,
1364 and the older versions can't decode it.
1366 Here's a list of example usages of these composition escape
1367 sequences (categorized by `enum composition_method').
1369 COMPOSITION_RELATIVE:
1370 ESC 0 CHAR [ CHAR ] ESC 1
1371 COMPOSITION_WITH_RULE:
1372 ESC 2 CHAR [ RULE CHAR ] ESC 1
1373 COMPOSITION_WITH_ALTCHARS:
1374 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
1375 COMPOSITION_WITH_RULE_ALTCHARS:
1376 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
1378 enum iso_code_class_type iso_code_class
[256];
1380 #define CHARSET_OK(idx, charset, c) \
1381 (coding_system_table[idx] \
1382 && (charset == CHARSET_ASCII \
1383 || (safe_chars = coding_safe_chars (coding_system_table[idx]->symbol), \
1384 CODING_SAFE_CHAR_P (safe_chars, c))) \
1385 && (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding_system_table[idx], \
1387 != CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION))
1389 #define SHIFT_OUT_OK(idx) \
1390 (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding_system_table[idx], 1) >= 0)
1392 #define COMPOSITION_OK(idx) \
1393 (coding_system_table[idx]->composing != COMPOSITION_DISABLED)
1395 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1396 Check if a text is encoded in ISO2022. If it is, return an
1397 integer in which appropriate flag bits any of:
1398 CODING_CATEGORY_MASK_ISO_7
1399 CODING_CATEGORY_MASK_ISO_7_TIGHT
1400 CODING_CATEGORY_MASK_ISO_8_1
1401 CODING_CATEGORY_MASK_ISO_8_2
1402 CODING_CATEGORY_MASK_ISO_7_ELSE
1403 CODING_CATEGORY_MASK_ISO_8_ELSE
1404 are set. If a code which should never appear in ISO2022 is found,
1408 detect_coding_iso2022 (src
, src_end
, multibytep
)
1409 unsigned char *src
, *src_end
;
1412 int mask
= CODING_CATEGORY_MASK_ISO
;
1414 int reg
[4], shift_out
= 0, single_shifting
= 0;
1416 /* Dummy for ONE_MORE_BYTE. */
1417 struct coding_system dummy_coding
;
1418 struct coding_system
*coding
= &dummy_coding
;
1419 Lisp_Object safe_chars
;
1421 reg
[0] = CHARSET_ASCII
, reg
[1] = reg
[2] = reg
[3] = -1;
1422 while (mask
&& src
< src_end
)
1424 ONE_MORE_BYTE_CHECK_MULTIBYTE (c
, multibytep
);
1429 if (inhibit_iso_escape_detection
)
1431 single_shifting
= 0;
1432 ONE_MORE_BYTE_CHECK_MULTIBYTE (c
, multibytep
);
1433 if (c
>= '(' && c
<= '/')
1435 /* Designation sequence for a charset of dimension 1. */
1436 ONE_MORE_BYTE_CHECK_MULTIBYTE (c1
, multibytep
);
1437 if (c1
< ' ' || c1
>= 0x80
1438 || (charset
= iso_charset_table
[0][c
>= ','][c1
]) < 0)
1439 /* Invalid designation sequence. Just ignore. */
1441 reg
[(c
- '(') % 4] = charset
;
1445 /* Designation sequence for a charset of dimension 2. */
1446 ONE_MORE_BYTE_CHECK_MULTIBYTE (c
, multibytep
);
1447 if (c
>= '@' && c
<= 'B')
1448 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
1449 reg
[0] = charset
= iso_charset_table
[1][0][c
];
1450 else if (c
>= '(' && c
<= '/')
1452 ONE_MORE_BYTE_CHECK_MULTIBYTE (c1
, multibytep
);
1453 if (c1
< ' ' || c1
>= 0x80
1454 || (charset
= iso_charset_table
[1][c
>= ','][c1
]) < 0)
1455 /* Invalid designation sequence. Just ignore. */
1457 reg
[(c
- '(') % 4] = charset
;
1460 /* Invalid designation sequence. Just ignore. */
1463 else if (c
== 'N' || c
== 'O')
1465 /* ESC <Fe> for SS2 or SS3. */
1466 mask
&= CODING_CATEGORY_MASK_ISO_7_ELSE
;
1469 else if (c
>= '0' && c
<= '4')
1471 /* ESC <Fp> for start/end composition. */
1472 if (COMPOSITION_OK (CODING_CATEGORY_IDX_ISO_7
))
1473 mask_found
|= CODING_CATEGORY_MASK_ISO_7
;
1475 mask
&= ~CODING_CATEGORY_MASK_ISO_7
;
1476 if (COMPOSITION_OK (CODING_CATEGORY_IDX_ISO_7_TIGHT
))
1477 mask_found
|= CODING_CATEGORY_MASK_ISO_7_TIGHT
;
1479 mask
&= ~CODING_CATEGORY_MASK_ISO_7_TIGHT
;
1480 if (COMPOSITION_OK (CODING_CATEGORY_IDX_ISO_8_1
))
1481 mask_found
|= CODING_CATEGORY_MASK_ISO_8_1
;
1483 mask
&= ~CODING_CATEGORY_MASK_ISO_8_1
;
1484 if (COMPOSITION_OK (CODING_CATEGORY_IDX_ISO_8_2
))
1485 mask_found
|= CODING_CATEGORY_MASK_ISO_8_2
;
1487 mask
&= ~CODING_CATEGORY_MASK_ISO_8_2
;
1488 if (COMPOSITION_OK (CODING_CATEGORY_IDX_ISO_7_ELSE
))
1489 mask_found
|= CODING_CATEGORY_MASK_ISO_7_ELSE
;
1491 mask
&= ~CODING_CATEGORY_MASK_ISO_7_ELSE
;
1492 if (COMPOSITION_OK (CODING_CATEGORY_IDX_ISO_8_ELSE
))
1493 mask_found
|= CODING_CATEGORY_MASK_ISO_8_ELSE
;
1495 mask
&= ~CODING_CATEGORY_MASK_ISO_8_ELSE
;
1499 /* Invalid escape sequence. Just ignore. */
1502 /* We found a valid designation sequence for CHARSET. */
1503 mask
&= ~CODING_CATEGORY_MASK_ISO_8BIT
;
1504 c
= MAKE_CHAR (charset
, 0, 0);
1505 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7
, charset
, c
))
1506 mask_found
|= CODING_CATEGORY_MASK_ISO_7
;
1508 mask
&= ~CODING_CATEGORY_MASK_ISO_7
;
1509 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7_TIGHT
, charset
, c
))
1510 mask_found
|= CODING_CATEGORY_MASK_ISO_7_TIGHT
;
1512 mask
&= ~CODING_CATEGORY_MASK_ISO_7_TIGHT
;
1513 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7_ELSE
, charset
, c
))
1514 mask_found
|= CODING_CATEGORY_MASK_ISO_7_ELSE
;
1516 mask
&= ~CODING_CATEGORY_MASK_ISO_7_ELSE
;
1517 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_8_ELSE
, charset
, c
))
1518 mask_found
|= CODING_CATEGORY_MASK_ISO_8_ELSE
;
1520 mask
&= ~CODING_CATEGORY_MASK_ISO_8_ELSE
;
1524 if (inhibit_iso_escape_detection
)
1526 single_shifting
= 0;
1529 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_7_ELSE
)
1530 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_8_ELSE
)))
1532 /* Locking shift out. */
1533 mask
&= ~CODING_CATEGORY_MASK_ISO_7BIT
;
1534 mask_found
|= CODING_CATEGORY_MASK_ISO_SHIFT
;
1539 if (inhibit_iso_escape_detection
)
1541 single_shifting
= 0;
1544 /* Locking shift in. */
1545 mask
&= ~CODING_CATEGORY_MASK_ISO_7BIT
;
1546 mask_found
|= CODING_CATEGORY_MASK_ISO_SHIFT
;
1551 single_shifting
= 0;
1555 int newmask
= CODING_CATEGORY_MASK_ISO_8_ELSE
;
1557 if (inhibit_iso_escape_detection
)
1559 if (c
!= ISO_CODE_CSI
)
1561 if (coding_system_table
[CODING_CATEGORY_IDX_ISO_8_1
]->flags
1562 & CODING_FLAG_ISO_SINGLE_SHIFT
)
1563 newmask
|= CODING_CATEGORY_MASK_ISO_8_1
;
1564 if (coding_system_table
[CODING_CATEGORY_IDX_ISO_8_2
]->flags
1565 & CODING_FLAG_ISO_SINGLE_SHIFT
)
1566 newmask
|= CODING_CATEGORY_MASK_ISO_8_2
;
1567 single_shifting
= 1;
1569 if (VECTORP (Vlatin_extra_code_table
)
1570 && !NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
1572 if (coding_system_table
[CODING_CATEGORY_IDX_ISO_8_1
]->flags
1573 & CODING_FLAG_ISO_LATIN_EXTRA
)
1574 newmask
|= CODING_CATEGORY_MASK_ISO_8_1
;
1575 if (coding_system_table
[CODING_CATEGORY_IDX_ISO_8_2
]->flags
1576 & CODING_FLAG_ISO_LATIN_EXTRA
)
1577 newmask
|= CODING_CATEGORY_MASK_ISO_8_2
;
1580 mask_found
|= newmask
;
1587 single_shifting
= 0;
1592 single_shifting
= 0;
1593 if (VECTORP (Vlatin_extra_code_table
)
1594 && !NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
1598 if (coding_system_table
[CODING_CATEGORY_IDX_ISO_8_1
]->flags
1599 & CODING_FLAG_ISO_LATIN_EXTRA
)
1600 newmask
|= CODING_CATEGORY_MASK_ISO_8_1
;
1601 if (coding_system_table
[CODING_CATEGORY_IDX_ISO_8_2
]->flags
1602 & CODING_FLAG_ISO_LATIN_EXTRA
)
1603 newmask
|= CODING_CATEGORY_MASK_ISO_8_2
;
1605 mask_found
|= newmask
;
1612 mask
&= ~(CODING_CATEGORY_MASK_ISO_7BIT
1613 | CODING_CATEGORY_MASK_ISO_7_ELSE
);
1614 mask_found
|= CODING_CATEGORY_MASK_ISO_8_1
;
1615 /* Check the length of succeeding codes of the range
1616 0xA0..0FF. If the byte length is odd, we exclude
1617 CODING_CATEGORY_MASK_ISO_8_2. We can check this only
1618 when we are not single shifting. */
1619 if (!single_shifting
1620 && mask
& CODING_CATEGORY_MASK_ISO_8_2
)
1625 while (src
< src_end
)
1627 ONE_MORE_BYTE_CHECK_MULTIBYTE (c
, multibytep
);
1633 if (i
& 1 && src
< src_end
)
1634 mask
&= ~CODING_CATEGORY_MASK_ISO_8_2
;
1636 mask_found
|= CODING_CATEGORY_MASK_ISO_8_2
;
1638 /* This means that we have read one extra byte. */
1646 return (mask
& mask_found
);
1649 /* Decode a character of which charset is CHARSET, the 1st position
1650 code is C1, the 2nd position code is C2, and return the decoded
1651 character code. If the variable `translation_table' is non-nil,
1652 returned the translated code. */
1654 #define DECODE_ISO_CHARACTER(charset, c1, c2) \
1655 (NILP (translation_table) \
1656 ? MAKE_CHAR (charset, c1, c2) \
1657 : translate_char (translation_table, -1, charset, c1, c2))
1659 /* Set designation state into CODING. */
1660 #define DECODE_DESIGNATION(reg, dimension, chars, final_char) \
1664 if (final_char < '0' || final_char >= 128) \
1665 goto label_invalid_code; \
1666 charset = ISO_CHARSET_TABLE (make_number (dimension), \
1667 make_number (chars), \
1668 make_number (final_char)); \
1669 c = MAKE_CHAR (charset, 0, 0); \
1671 && (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) == reg \
1672 || CODING_SAFE_CHAR_P (safe_chars, c))) \
1674 if (coding->spec.iso2022.last_invalid_designation_register == 0 \
1676 && charset == CHARSET_ASCII) \
1678 /* We should insert this designation sequence as is so \
1679 that it is surely written back to a file. */ \
1680 coding->spec.iso2022.last_invalid_designation_register = -1; \
1681 goto label_invalid_code; \
1683 coding->spec.iso2022.last_invalid_designation_register = -1; \
1684 if ((coding->mode & CODING_MODE_DIRECTION) \
1685 && CHARSET_REVERSE_CHARSET (charset) >= 0) \
1686 charset = CHARSET_REVERSE_CHARSET (charset); \
1687 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
1691 coding->spec.iso2022.last_invalid_designation_register = reg; \
1692 goto label_invalid_code; \
1696 /* Allocate a memory block for storing information about compositions.
1697 The block is chained to the already allocated blocks. */
1700 coding_allocate_composition_data (coding
, char_offset
)
1701 struct coding_system
*coding
;
1704 struct composition_data
*cmp_data
1705 = (struct composition_data
*) xmalloc (sizeof *cmp_data
);
1707 cmp_data
->char_offset
= char_offset
;
1709 cmp_data
->prev
= coding
->cmp_data
;
1710 cmp_data
->next
= NULL
;
1711 if (coding
->cmp_data
)
1712 coding
->cmp_data
->next
= cmp_data
;
1713 coding
->cmp_data
= cmp_data
;
1714 coding
->cmp_data_start
= 0;
1715 coding
->composing
= COMPOSITION_NO
;
1718 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
1719 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
1720 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
1721 ESC 3 : altchar composition : ESC 3 ALT ... ESC 0 CHAR ... ESC 1
1722 ESC 4 : alt&rule composition : ESC 4 ALT RULE .. ALT ESC 0 CHAR ... ESC 1
1725 #define DECODE_COMPOSITION_START(c1) \
1727 if (coding->composing == COMPOSITION_DISABLED) \
1729 *dst++ = ISO_CODE_ESC; \
1730 *dst++ = c1 & 0x7f; \
1731 coding->produced_char += 2; \
1733 else if (!COMPOSING_P (coding)) \
1735 /* This is surely the start of a composition. We must be sure \
1736 that coding->cmp_data has enough space to store the \
1737 information about the composition. If not, terminate the \
1738 current decoding loop, allocate one more memory block for \
1739 coding->cmp_data in the caller, then start the decoding \
1740 loop again. We can't allocate memory here directly because \
1741 it may cause buffer/string relocation. */ \
1742 if (!coding->cmp_data \
1743 || (coding->cmp_data->used + COMPOSITION_DATA_MAX_BUNCH_LENGTH \
1744 >= COMPOSITION_DATA_SIZE)) \
1746 coding->result = CODING_FINISH_INSUFFICIENT_CMP; \
1747 goto label_end_of_loop; \
1749 coding->composing = (c1 == '0' ? COMPOSITION_RELATIVE \
1750 : c1 == '2' ? COMPOSITION_WITH_RULE \
1751 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
1752 : COMPOSITION_WITH_RULE_ALTCHARS); \
1753 CODING_ADD_COMPOSITION_START (coding, coding->produced_char, \
1754 coding->composing); \
1755 coding->composition_rule_follows = 0; \
1759 /* We are already handling a composition. If the method is \
1760 the following two, the codes following the current escape \
1761 sequence are actual characters stored in a buffer. */ \
1762 if (coding->composing == COMPOSITION_WITH_ALTCHARS \
1763 || coding->composing == COMPOSITION_WITH_RULE_ALTCHARS) \
1765 coding->composing = COMPOSITION_RELATIVE; \
1766 coding->composition_rule_follows = 0; \
1771 /* Handle composition end sequence ESC 1. */
1773 #define DECODE_COMPOSITION_END(c1) \
1775 if (! COMPOSING_P (coding)) \
1777 *dst++ = ISO_CODE_ESC; \
1779 coding->produced_char += 2; \
1783 CODING_ADD_COMPOSITION_END (coding, coding->produced_char); \
1784 coding->composing = COMPOSITION_NO; \
1788 /* Decode a composition rule from the byte C1 (and maybe one more byte
1789 from SRC) and store one encoded composition rule in
1790 coding->cmp_data. */
1792 #define DECODE_COMPOSITION_RULE(c1) \
1796 if (c1 < 81) /* old format (before ver.21) */ \
1798 int gref = (c1) / 9; \
1799 int nref = (c1) % 9; \
1800 if (gref == 4) gref = 10; \
1801 if (nref == 4) nref = 10; \
1802 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
1804 else if (c1 < 93) /* new format (after ver.21) */ \
1806 ONE_MORE_BYTE (c2); \
1807 rule = COMPOSITION_ENCODE_RULE (c1 - 81, c2 - 32); \
1809 CODING_ADD_COMPOSITION_COMPONENT (coding, rule); \
1810 coding->composition_rule_follows = 0; \
1814 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
1817 decode_coding_iso2022 (coding
, source
, destination
, src_bytes
, dst_bytes
)
1818 struct coding_system
*coding
;
1819 const unsigned char *source
;
1820 unsigned char *destination
;
1821 int src_bytes
, dst_bytes
;
1823 const unsigned char *src
= source
;
1824 const unsigned char *src_end
= source
+ src_bytes
;
1825 unsigned char *dst
= destination
;
1826 unsigned char *dst_end
= destination
+ dst_bytes
;
1827 /* Charsets invoked to graphic plane 0 and 1 respectively. */
1828 int charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
1829 int charset1
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 1);
1830 /* SRC_BASE remembers the start position in source in each loop.
1831 The loop will be exited when there's not enough source code
1832 (within macro ONE_MORE_BYTE), or when there's not enough
1833 destination area to produce a character (within macro
1835 const unsigned char *src_base
;
1837 Lisp_Object translation_table
;
1838 Lisp_Object safe_chars
;
1840 safe_chars
= coding_safe_chars (coding
->symbol
);
1842 if (NILP (Venable_character_translation
))
1843 translation_table
= Qnil
;
1846 translation_table
= coding
->translation_table_for_decode
;
1847 if (NILP (translation_table
))
1848 translation_table
= Vstandard_translation_table_for_decode
;
1851 coding
->result
= CODING_FINISH_NORMAL
;
1860 /* We produce no character or one character. */
1861 switch (iso_code_class
[c1
])
1863 case ISO_0x20_or_0x7F
:
1864 if (COMPOSING_P (coding
) && coding
->composition_rule_follows
)
1866 DECODE_COMPOSITION_RULE (c1
);
1869 if (charset0
< 0 || CHARSET_CHARS (charset0
) == 94)
1871 /* This is SPACE or DEL. */
1872 charset
= CHARSET_ASCII
;
1875 /* This is a graphic character, we fall down ... */
1877 case ISO_graphic_plane_0
:
1878 if (COMPOSING_P (coding
) && coding
->composition_rule_follows
)
1880 DECODE_COMPOSITION_RULE (c1
);
1886 case ISO_0xA0_or_0xFF
:
1887 if (charset1
< 0 || CHARSET_CHARS (charset1
) == 94
1888 || coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
)
1889 goto label_invalid_code
;
1890 /* This is a graphic character, we fall down ... */
1892 case ISO_graphic_plane_1
:
1894 goto label_invalid_code
;
1899 if (COMPOSING_P (coding
))
1900 DECODE_COMPOSITION_END ('1');
1902 /* All ISO2022 control characters in this class have the
1903 same representation in Emacs internal format. */
1905 && (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
)
1906 && (coding
->eol_type
== CODING_EOL_CR
1907 || coding
->eol_type
== CODING_EOL_CRLF
))
1909 coding
->result
= CODING_FINISH_INCONSISTENT_EOL
;
1910 goto label_end_of_loop
;
1912 charset
= CHARSET_ASCII
;
1916 if (COMPOSING_P (coding
))
1917 DECODE_COMPOSITION_END ('1');
1918 goto label_invalid_code
;
1920 case ISO_carriage_return
:
1921 if (COMPOSING_P (coding
))
1922 DECODE_COMPOSITION_END ('1');
1924 if (coding
->eol_type
== CODING_EOL_CR
)
1926 else if (coding
->eol_type
== CODING_EOL_CRLF
)
1929 if (c1
!= ISO_CODE_LF
)
1935 charset
= CHARSET_ASCII
;
1939 if (! (coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
)
1940 || CODING_SPEC_ISO_DESIGNATION (coding
, 1) < 0)
1941 goto label_invalid_code
;
1942 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 1;
1943 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
1947 if (! (coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
))
1948 goto label_invalid_code
;
1949 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 0;
1950 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
1953 case ISO_single_shift_2_7
:
1954 case ISO_single_shift_2
:
1955 if (! (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
))
1956 goto label_invalid_code
;
1957 /* SS2 is handled as an escape sequence of ESC 'N' */
1959 goto label_escape_sequence
;
1961 case ISO_single_shift_3
:
1962 if (! (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
))
1963 goto label_invalid_code
;
1964 /* SS2 is handled as an escape sequence of ESC 'O' */
1966 goto label_escape_sequence
;
1968 case ISO_control_sequence_introducer
:
1969 /* CSI is handled as an escape sequence of ESC '[' ... */
1971 goto label_escape_sequence
;
1975 label_escape_sequence
:
1976 /* Escape sequences handled by Emacs are invocation,
1977 designation, direction specification, and character
1978 composition specification. */
1981 case '&': /* revision of following character set */
1983 if (!(c1
>= '@' && c1
<= '~'))
1984 goto label_invalid_code
;
1986 if (c1
!= ISO_CODE_ESC
)
1987 goto label_invalid_code
;
1989 goto label_escape_sequence
;
1991 case '$': /* designation of 2-byte character set */
1992 if (! (coding
->flags
& CODING_FLAG_ISO_DESIGNATION
))
1993 goto label_invalid_code
;
1995 if (c1
>= '@' && c1
<= 'B')
1996 { /* designation of JISX0208.1978, GB2312.1980,
1998 DECODE_DESIGNATION (0, 2, 94, c1
);
2000 else if (c1
>= 0x28 && c1
<= 0x2B)
2001 { /* designation of DIMENSION2_CHARS94 character set */
2003 DECODE_DESIGNATION (c1
- 0x28, 2, 94, c2
);
2005 else if (c1
>= 0x2C && c1
<= 0x2F)
2006 { /* designation of DIMENSION2_CHARS96 character set */
2008 DECODE_DESIGNATION (c1
- 0x2C, 2, 96, c2
);
2011 goto label_invalid_code
;
2012 /* We must update these variables now. */
2013 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
2014 charset1
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 1);
2017 case 'n': /* invocation of locking-shift-2 */
2018 if (! (coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
)
2019 || CODING_SPEC_ISO_DESIGNATION (coding
, 2) < 0)
2020 goto label_invalid_code
;
2021 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 2;
2022 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
2025 case 'o': /* invocation of locking-shift-3 */
2026 if (! (coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
)
2027 || CODING_SPEC_ISO_DESIGNATION (coding
, 3) < 0)
2028 goto label_invalid_code
;
2029 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 3;
2030 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
2033 case 'N': /* invocation of single-shift-2 */
2034 if (! (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
2035 || CODING_SPEC_ISO_DESIGNATION (coding
, 2) < 0)
2036 goto label_invalid_code
;
2037 charset
= CODING_SPEC_ISO_DESIGNATION (coding
, 2);
2039 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
2040 goto label_invalid_code
;
2043 case 'O': /* invocation of single-shift-3 */
2044 if (! (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
2045 || CODING_SPEC_ISO_DESIGNATION (coding
, 3) < 0)
2046 goto label_invalid_code
;
2047 charset
= CODING_SPEC_ISO_DESIGNATION (coding
, 3);
2049 if (c1
< 0x20 || (c1
>= 0x80 && c1
< 0xA0))
2050 goto label_invalid_code
;
2053 case '0': case '2': case '3': case '4': /* start composition */
2054 DECODE_COMPOSITION_START (c1
);
2057 case '1': /* end composition */
2058 DECODE_COMPOSITION_END (c1
);
2061 case '[': /* specification of direction */
2062 if (coding
->flags
& CODING_FLAG_ISO_NO_DIRECTION
)
2063 goto label_invalid_code
;
2064 /* For the moment, nested direction is not supported.
2065 So, `coding->mode & CODING_MODE_DIRECTION' zero means
2066 left-to-right, and nonzero means right-to-left. */
2070 case ']': /* end of the current direction */
2071 coding
->mode
&= ~CODING_MODE_DIRECTION
;
2073 case '0': /* end of the current direction */
2074 case '1': /* start of left-to-right direction */
2077 coding
->mode
&= ~CODING_MODE_DIRECTION
;
2079 goto label_invalid_code
;
2082 case '2': /* start of right-to-left direction */
2085 coding
->mode
|= CODING_MODE_DIRECTION
;
2087 goto label_invalid_code
;
2091 goto label_invalid_code
;
2096 if (COMPOSING_P (coding
))
2097 DECODE_COMPOSITION_END ('1');
2101 /* CTEXT extended segment:
2102 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
2103 We keep these bytes as is for the moment.
2104 They may be decoded by post-read-conversion. */
2109 ONE_MORE_BYTE (dim
);
2112 size
= ((M
- 128) * 128) + (L
- 128);
2113 required
= 8 + size
* 2;
2114 if (dst
+ required
> (dst_bytes
? dst_end
: src
))
2115 goto label_end_of_loop
;
2116 *dst
++ = ISO_CODE_ESC
;
2121 dst
+= CHAR_STRING (M
, dst
), produced_chars
++;
2122 dst
+= CHAR_STRING (L
, dst
), produced_chars
++;
2126 dst
+= CHAR_STRING (c1
, dst
), produced_chars
++;
2128 coding
->produced_char
+= produced_chars
;
2132 unsigned char *d
= dst
;
2135 /* XFree86 extension for embedding UTF-8 in CTEXT:
2136 ESC % G --UTF-8-BYTES-- ESC % @
2137 We keep these bytes as is for the moment.
2138 They may be decoded by post-read-conversion. */
2139 if (d
+ 6 > (dst_bytes
? dst_end
: src
))
2140 goto label_end_of_loop
;
2141 *d
++ = ISO_CODE_ESC
;
2145 while (d
+ 1 < (dst_bytes
? dst_end
: src
))
2148 if (c1
== ISO_CODE_ESC
2149 && src
+ 1 < src_end
2156 d
+= CHAR_STRING (c1
, d
), produced_chars
++;
2158 if (d
+ 3 > (dst_bytes
? dst_end
: src
))
2159 goto label_end_of_loop
;
2160 *d
++ = ISO_CODE_ESC
;
2164 coding
->produced_char
+= produced_chars
+ 3;
2167 goto label_invalid_code
;
2171 if (! (coding
->flags
& CODING_FLAG_ISO_DESIGNATION
))
2172 goto label_invalid_code
;
2173 if (c1
>= 0x28 && c1
<= 0x2B)
2174 { /* designation of DIMENSION1_CHARS94 character set */
2176 DECODE_DESIGNATION (c1
- 0x28, 1, 94, c2
);
2178 else if (c1
>= 0x2C && c1
<= 0x2F)
2179 { /* designation of DIMENSION1_CHARS96 character set */
2181 DECODE_DESIGNATION (c1
- 0x2C, 1, 96, c2
);
2184 goto label_invalid_code
;
2185 /* We must update these variables now. */
2186 charset0
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 0);
2187 charset1
= CODING_SPEC_ISO_PLANE_CHARSET (coding
, 1);
2192 /* Now we know CHARSET and 1st position code C1 of a character.
2193 Produce a multibyte sequence for that character while getting
2194 2nd position code C2 if necessary. */
2195 if (CHARSET_DIMENSION (charset
) == 2)
2198 if (c1
< 0x80 ? c2
< 0x20 || c2
>= 0x80 : c2
< 0xA0)
2199 /* C2 is not in a valid range. */
2200 goto label_invalid_code
;
2202 c
= DECODE_ISO_CHARACTER (charset
, c1
, c2
);
2208 if (COMPOSING_P (coding
))
2209 DECODE_COMPOSITION_END ('1');
2212 if (! NILP (translation_table
))
2213 c
= translate_char (translation_table
, c
, 0, 0, 0);
2218 coding
->consumed
= coding
->consumed_char
= src_base
- source
;
2219 coding
->produced
= dst
- destination
;
2224 /* ISO2022 encoding stuff. */
2227 It is not enough to say just "ISO2022" on encoding, we have to
2228 specify more details. In Emacs, each ISO2022 coding system
2229 variant has the following specifications:
2230 1. Initial designation to G0 through G3.
2231 2. Allows short-form designation?
2232 3. ASCII should be designated to G0 before control characters?
2233 4. ASCII should be designated to G0 at end of line?
2234 5. 7-bit environment or 8-bit environment?
2235 6. Use locking-shift?
2236 7. Use Single-shift?
2237 And the following two are only for Japanese:
2238 8. Use ASCII in place of JIS0201-1976-Roman?
2239 9. Use JISX0208-1983 in place of JISX0208-1978?
2240 These specifications are encoded in `coding->flags' as flag bits
2241 defined by macros CODING_FLAG_ISO_XXX. See `coding.h' for more
2245 /* Produce codes (escape sequence) for designating CHARSET to graphic
2246 register REG at DST, and increment DST. If <final-char> of CHARSET is
2247 '@', 'A', or 'B' and the coding system CODING allows, produce
2248 designation sequence of short-form. */
2250 #define ENCODE_DESIGNATION(charset, reg, coding) \
2252 unsigned char final_char = CHARSET_ISO_FINAL_CHAR (charset); \
2253 char *intermediate_char_94 = "()*+"; \
2254 char *intermediate_char_96 = ",-./"; \
2255 int revision = CODING_SPEC_ISO_REVISION_NUMBER(coding, charset); \
2257 if (revision < 255) \
2259 *dst++ = ISO_CODE_ESC; \
2261 *dst++ = '@' + revision; \
2263 *dst++ = ISO_CODE_ESC; \
2264 if (CHARSET_DIMENSION (charset) == 1) \
2266 if (CHARSET_CHARS (charset) == 94) \
2267 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
2269 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
2274 if (CHARSET_CHARS (charset) == 94) \
2276 if (! (coding->flags & CODING_FLAG_ISO_SHORT_FORM) \
2278 || final_char < '@' || final_char > 'B') \
2279 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
2282 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
2284 *dst++ = final_char; \
2285 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
2288 /* The following two macros produce codes (control character or escape
2289 sequence) for ISO2022 single-shift functions (single-shift-2 and
2292 #define ENCODE_SINGLE_SHIFT_2 \
2294 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
2295 *dst++ = ISO_CODE_ESC, *dst++ = 'N'; \
2297 *dst++ = ISO_CODE_SS2; \
2298 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
2301 #define ENCODE_SINGLE_SHIFT_3 \
2303 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
2304 *dst++ = ISO_CODE_ESC, *dst++ = 'O'; \
2306 *dst++ = ISO_CODE_SS3; \
2307 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
2310 /* The following four macros produce codes (control character or
2311 escape sequence) for ISO2022 locking-shift functions (shift-in,
2312 shift-out, locking-shift-2, and locking-shift-3). */
2314 #define ENCODE_SHIFT_IN \
2316 *dst++ = ISO_CODE_SI; \
2317 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0; \
2320 #define ENCODE_SHIFT_OUT \
2322 *dst++ = ISO_CODE_SO; \
2323 CODING_SPEC_ISO_INVOCATION (coding, 0) = 1; \
2326 #define ENCODE_LOCKING_SHIFT_2 \
2328 *dst++ = ISO_CODE_ESC, *dst++ = 'n'; \
2329 CODING_SPEC_ISO_INVOCATION (coding, 0) = 2; \
2332 #define ENCODE_LOCKING_SHIFT_3 \
2334 *dst++ = ISO_CODE_ESC, *dst++ = 'o'; \
2335 CODING_SPEC_ISO_INVOCATION (coding, 0) = 3; \
2338 /* Produce codes for a DIMENSION1 character whose character set is
2339 CHARSET and whose position-code is C1. Designation and invocation
2340 sequences are also produced in advance if necessary. */
2342 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
2344 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
2346 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
2347 *dst++ = c1 & 0x7F; \
2349 *dst++ = c1 | 0x80; \
2350 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
2353 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
2355 *dst++ = c1 & 0x7F; \
2358 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
2360 *dst++ = c1 | 0x80; \
2364 /* Since CHARSET is not yet invoked to any graphic planes, we \
2365 must invoke it, or, at first, designate it to some graphic \
2366 register. Then repeat the loop to actually produce the \
2368 dst = encode_invocation_designation (charset, coding, dst); \
2371 /* Produce codes for a DIMENSION2 character whose character set is
2372 CHARSET and whose position-codes are C1 and C2. Designation and
2373 invocation codes are also produced in advance if necessary. */
2375 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
2377 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
2379 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
2380 *dst++ = c1 & 0x7F, *dst++ = c2 & 0x7F; \
2382 *dst++ = c1 | 0x80, *dst++ = c2 | 0x80; \
2383 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
2386 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
2388 *dst++ = c1 & 0x7F, *dst++= c2 & 0x7F; \
2391 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
2393 *dst++ = c1 | 0x80, *dst++= c2 | 0x80; \
2397 /* Since CHARSET is not yet invoked to any graphic planes, we \
2398 must invoke it, or, at first, designate it to some graphic \
2399 register. Then repeat the loop to actually produce the \
2401 dst = encode_invocation_designation (charset, coding, dst); \
2404 #define ENCODE_ISO_CHARACTER(c) \
2406 int charset, c1, c2; \
2408 SPLIT_CHAR (c, charset, c1, c2); \
2409 if (CHARSET_DEFINED_P (charset)) \
2411 if (CHARSET_DIMENSION (charset) == 1) \
2413 if (charset == CHARSET_ASCII \
2414 && coding->flags & CODING_FLAG_ISO_USE_ROMAN) \
2415 charset = charset_latin_jisx0201; \
2416 ENCODE_ISO_CHARACTER_DIMENSION1 (charset, c1); \
2420 if (charset == charset_jisx0208 \
2421 && coding->flags & CODING_FLAG_ISO_USE_OLDJIS) \
2422 charset = charset_jisx0208_1978; \
2423 ENCODE_ISO_CHARACTER_DIMENSION2 (charset, c1, c2); \
2435 /* Instead of encoding character C, produce one or two `?'s. */
2437 #define ENCODE_UNSAFE_CHARACTER(c) \
2439 ENCODE_ISO_CHARACTER (CODING_REPLACEMENT_CHARACTER); \
2440 if (CHARSET_WIDTH (CHAR_CHARSET (c)) > 1) \
2441 ENCODE_ISO_CHARACTER (CODING_REPLACEMENT_CHARACTER); \
2445 /* Produce designation and invocation codes at a place pointed by DST
2446 to use CHARSET. The element `spec.iso2022' of *CODING is updated.
2450 encode_invocation_designation (charset
, coding
, dst
)
2452 struct coding_system
*coding
;
2455 int reg
; /* graphic register number */
2457 /* At first, check designations. */
2458 for (reg
= 0; reg
< 4; reg
++)
2459 if (charset
== CODING_SPEC_ISO_DESIGNATION (coding
, reg
))
2464 /* CHARSET is not yet designated to any graphic registers. */
2465 /* At first check the requested designation. */
2466 reg
= CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
);
2467 if (reg
== CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
)
2468 /* Since CHARSET requests no special designation, designate it
2469 to graphic register 0. */
2472 ENCODE_DESIGNATION (charset
, reg
, coding
);
2475 if (CODING_SPEC_ISO_INVOCATION (coding
, 0) != reg
2476 && CODING_SPEC_ISO_INVOCATION (coding
, 1) != reg
)
2478 /* Since the graphic register REG is not invoked to any graphic
2479 planes, invoke it to graphic plane 0. */
2482 case 0: /* graphic register 0 */
2486 case 1: /* graphic register 1 */
2490 case 2: /* graphic register 2 */
2491 if (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
2492 ENCODE_SINGLE_SHIFT_2
;
2494 ENCODE_LOCKING_SHIFT_2
;
2497 case 3: /* graphic register 3 */
2498 if (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
)
2499 ENCODE_SINGLE_SHIFT_3
;
2501 ENCODE_LOCKING_SHIFT_3
;
2509 /* Produce 2-byte codes for encoded composition rule RULE. */
2511 #define ENCODE_COMPOSITION_RULE(rule) \
2514 COMPOSITION_DECODE_RULE (rule, gref, nref); \
2515 *dst++ = 32 + 81 + gref; \
2516 *dst++ = 32 + nref; \
2519 /* Produce codes for indicating the start of a composition sequence
2520 (ESC 0, ESC 3, or ESC 4). DATA points to an array of integers
2521 which specify information about the composition. See the comment
2522 in coding.h for the format of DATA. */
2524 #define ENCODE_COMPOSITION_START(coding, data) \
2526 coding->composing = data[3]; \
2527 *dst++ = ISO_CODE_ESC; \
2528 if (coding->composing == COMPOSITION_RELATIVE) \
2532 *dst++ = (coding->composing == COMPOSITION_WITH_ALTCHARS \
2534 coding->cmp_data_index = coding->cmp_data_start + 4; \
2535 coding->composition_rule_follows = 0; \
2539 /* Produce codes for indicating the end of the current composition. */
2541 #define ENCODE_COMPOSITION_END(coding, data) \
2543 *dst++ = ISO_CODE_ESC; \
2545 coding->cmp_data_start += data[0]; \
2546 coding->composing = COMPOSITION_NO; \
2547 if (coding->cmp_data_start == coding->cmp_data->used \
2548 && coding->cmp_data->next) \
2550 coding->cmp_data = coding->cmp_data->next; \
2551 coding->cmp_data_start = 0; \
2555 /* Produce composition start sequence ESC 0. Here, this sequence
2556 doesn't mean the start of a new composition but means that we have
2557 just produced components (alternate chars and composition rules) of
2558 the composition and the actual text follows in SRC. */
2560 #define ENCODE_COMPOSITION_FAKE_START(coding) \
2562 *dst++ = ISO_CODE_ESC; \
2564 coding->composing = COMPOSITION_RELATIVE; \
2567 /* The following three macros produce codes for indicating direction
2569 #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
2571 if (coding->flags == CODING_FLAG_ISO_SEVEN_BITS) \
2572 *dst++ = ISO_CODE_ESC, *dst++ = '['; \
2574 *dst++ = ISO_CODE_CSI; \
2577 #define ENCODE_DIRECTION_R2L \
2578 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst), *dst++ = '2', *dst++ = ']'
2580 #define ENCODE_DIRECTION_L2R \
2581 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst), *dst++ = '0', *dst++ = ']'
2583 /* Produce codes for designation and invocation to reset the graphic
2584 planes and registers to initial state. */
2585 #define ENCODE_RESET_PLANE_AND_REGISTER \
2588 if (CODING_SPEC_ISO_INVOCATION (coding, 0) != 0) \
2590 for (reg = 0; reg < 4; reg++) \
2591 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg) >= 0 \
2592 && (CODING_SPEC_ISO_DESIGNATION (coding, reg) \
2593 != CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg))) \
2594 ENCODE_DESIGNATION \
2595 (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg), reg, coding); \
2598 /* Produce designation sequences of charsets in the line started from
2599 SRC to a place pointed by DST, and return updated DST.
2601 If the current block ends before any end-of-line, we may fail to
2602 find all the necessary designations. */
2604 static unsigned char *
2605 encode_designation_at_bol (coding
, translation_table
, src
, src_end
, dst
)
2606 struct coding_system
*coding
;
2607 Lisp_Object translation_table
;
2608 const unsigned char *src
, *src_end
;
2611 int charset
, c
, found
= 0, reg
;
2612 /* Table of charsets to be designated to each graphic register. */
2615 for (reg
= 0; reg
< 4; reg
++)
2624 charset
= CHAR_CHARSET (c
);
2625 reg
= CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
);
2626 if (reg
!= CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
&& r
[reg
] < 0)
2636 for (reg
= 0; reg
< 4; reg
++)
2638 && CODING_SPEC_ISO_DESIGNATION (coding
, reg
) != r
[reg
])
2639 ENCODE_DESIGNATION (r
[reg
], reg
, coding
);
2645 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
2648 encode_coding_iso2022 (coding
, source
, destination
, src_bytes
, dst_bytes
)
2649 struct coding_system
*coding
;
2650 const unsigned char *source
;
2651 unsigned char *destination
;
2652 int src_bytes
, dst_bytes
;
2654 const unsigned char *src
= source
;
2655 const unsigned char *src_end
= source
+ src_bytes
;
2656 unsigned char *dst
= destination
;
2657 unsigned char *dst_end
= destination
+ dst_bytes
;
2658 /* Since the maximum bytes produced by each loop is 20, we subtract 19
2659 from DST_END to assure overflow checking is necessary only at the
2661 unsigned char *adjusted_dst_end
= dst_end
- 19;
2662 /* SRC_BASE remembers the start position in source in each loop.
2663 The loop will be exited when there's not enough source text to
2664 analyze multi-byte codes (within macro ONE_MORE_CHAR), or when
2665 there's not enough destination area to produce encoded codes
2666 (within macro EMIT_BYTES). */
2667 const unsigned char *src_base
;
2669 Lisp_Object translation_table
;
2670 Lisp_Object safe_chars
;
2672 if (coding
->flags
& CODING_FLAG_ISO_SAFE
)
2673 coding
->mode
|= CODING_MODE_INHIBIT_UNENCODABLE_CHAR
;
2675 safe_chars
= coding_safe_chars (coding
->symbol
);
2677 if (NILP (Venable_character_translation
))
2678 translation_table
= Qnil
;
2681 translation_table
= coding
->translation_table_for_encode
;
2682 if (NILP (translation_table
))
2683 translation_table
= Vstandard_translation_table_for_encode
;
2686 coding
->consumed_char
= 0;
2692 if (dst
>= (dst_bytes
? adjusted_dst_end
: (src
- 19)))
2694 coding
->result
= CODING_FINISH_INSUFFICIENT_DST
;
2698 if (coding
->flags
& CODING_FLAG_ISO_DESIGNATE_AT_BOL
2699 && CODING_SPEC_ISO_BOL (coding
))
2701 /* We have to produce designation sequences if any now. */
2702 dst
= encode_designation_at_bol (coding
, translation_table
,
2704 CODING_SPEC_ISO_BOL (coding
) = 0;
2707 /* Check composition start and end. */
2708 if (coding
->composing
!= COMPOSITION_DISABLED
2709 && coding
->cmp_data_start
< coding
->cmp_data
->used
)
2711 struct composition_data
*cmp_data
= coding
->cmp_data
;
2712 int *data
= cmp_data
->data
+ coding
->cmp_data_start
;
2713 int this_pos
= cmp_data
->char_offset
+ coding
->consumed_char
;
2715 if (coding
->composing
== COMPOSITION_RELATIVE
)
2717 if (this_pos
== data
[2])
2719 ENCODE_COMPOSITION_END (coding
, data
);
2720 cmp_data
= coding
->cmp_data
;
2721 data
= cmp_data
->data
+ coding
->cmp_data_start
;
2724 else if (COMPOSING_P (coding
))
2726 /* COMPOSITION_WITH_ALTCHARS or COMPOSITION_WITH_RULE_ALTCHAR */
2727 if (coding
->cmp_data_index
== coding
->cmp_data_start
+ data
[0])
2728 /* We have consumed components of the composition.
2729 What follows in SRC is the composition's base
2731 ENCODE_COMPOSITION_FAKE_START (coding
);
2734 int c
= cmp_data
->data
[coding
->cmp_data_index
++];
2735 if (coding
->composition_rule_follows
)
2737 ENCODE_COMPOSITION_RULE (c
);
2738 coding
->composition_rule_follows
= 0;
2742 if (coding
->mode
& CODING_MODE_INHIBIT_UNENCODABLE_CHAR
2743 && ! CODING_SAFE_CHAR_P (safe_chars
, c
))
2744 ENCODE_UNSAFE_CHARACTER (c
);
2746 ENCODE_ISO_CHARACTER (c
);
2747 if (coding
->composing
== COMPOSITION_WITH_RULE_ALTCHARS
)
2748 coding
->composition_rule_follows
= 1;
2753 if (!COMPOSING_P (coding
))
2755 if (this_pos
== data
[1])
2757 ENCODE_COMPOSITION_START (coding
, data
);
2765 /* Now encode the character C. */
2766 if (c
< 0x20 || c
== 0x7F)
2770 if (! (coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
2772 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_CNTL
)
2773 ENCODE_RESET_PLANE_AND_REGISTER
;
2777 /* fall down to treat '\r' as '\n' ... */
2782 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_EOL
)
2783 ENCODE_RESET_PLANE_AND_REGISTER
;
2784 if (coding
->flags
& CODING_FLAG_ISO_INIT_AT_BOL
)
2785 bcopy (coding
->spec
.iso2022
.initial_designation
,
2786 coding
->spec
.iso2022
.current_designation
,
2787 sizeof coding
->spec
.iso2022
.initial_designation
);
2788 if (coding
->eol_type
== CODING_EOL_LF
2789 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
2790 *dst
++ = ISO_CODE_LF
;
2791 else if (coding
->eol_type
== CODING_EOL_CRLF
)
2792 *dst
++ = ISO_CODE_CR
, *dst
++ = ISO_CODE_LF
;
2794 *dst
++ = ISO_CODE_CR
;
2795 CODING_SPEC_ISO_BOL (coding
) = 1;
2799 if (coding
->flags
& CODING_FLAG_ISO_RESET_AT_CNTL
)
2800 ENCODE_RESET_PLANE_AND_REGISTER
;
2804 else if (ASCII_BYTE_P (c
))
2805 ENCODE_ISO_CHARACTER (c
);
2806 else if (SINGLE_BYTE_CHAR_P (c
))
2811 else if (coding
->mode
& CODING_MODE_INHIBIT_UNENCODABLE_CHAR
2812 && ! CODING_SAFE_CHAR_P (safe_chars
, c
))
2813 ENCODE_UNSAFE_CHARACTER (c
);
2815 ENCODE_ISO_CHARACTER (c
);
2817 coding
->consumed_char
++;
2821 coding
->consumed
= src_base
- source
;
2822 coding
->produced
= coding
->produced_char
= dst
- destination
;
2826 /*** 4. SJIS and BIG5 handlers ***/
2828 /* Although SJIS and BIG5 are not ISO coding systems, they are used
2829 quite widely. So, for the moment, Emacs supports them in the bare
2830 C code. But, in the future, they may be supported only by CCL. */
2832 /* SJIS is a coding system encoding three character sets: ASCII, right
2833 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
2834 as is. A character of charset katakana-jisx0201 is encoded by
2835 "position-code + 0x80". A character of charset japanese-jisx0208
2836 is encoded in 2-byte but two position-codes are divided and shifted
2837 so that it fits in the range below.
2839 --- CODE RANGE of SJIS ---
2840 (character set) (range)
2842 KATAKANA-JISX0201 0xA1 .. 0xDF
2843 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
2844 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
2845 -------------------------------
2849 /* BIG5 is a coding system encoding two character sets: ASCII and
2850 Big5. An ASCII character is encoded as is. Big5 is a two-byte
2851 character set and is encoded in two bytes.
2853 --- CODE RANGE of BIG5 ---
2854 (character set) (range)
2856 Big5 (1st byte) 0xA1 .. 0xFE
2857 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
2858 --------------------------
2860 Since the number of characters in Big5 is larger than maximum
2861 characters in Emacs' charset (96x96), it can't be handled as one
2862 charset. So, in Emacs, Big5 is divided into two: `charset-big5-1'
2863 and `charset-big5-2'. Both are DIMENSION2 and CHARS94. The former
2864 contains frequently used characters and the latter contains less
2865 frequently used characters. */
2867 /* Macros to decode or encode a character of Big5 in BIG5. B1 and B2
2868 are the 1st and 2nd position-codes of Big5 in BIG5 coding system.
2869 C1 and C2 are the 1st and 2nd position-codes of Emacs' internal
2870 format. CHARSET is `charset_big5_1' or `charset_big5_2'. */
2872 /* Number of Big5 characters which have the same code in 1st byte. */
2873 #define BIG5_SAME_ROW (0xFF - 0xA1 + 0x7F - 0x40)
2875 #define DECODE_BIG5(b1, b2, charset, c1, c2) \
2878 = (b1 - 0xA1) * BIG5_SAME_ROW + b2 - (b2 < 0x7F ? 0x40 : 0x62); \
2880 charset = charset_big5_1; \
2883 charset = charset_big5_2; \
2884 temp -= (0xC9 - 0xA1) * BIG5_SAME_ROW; \
2886 c1 = temp / (0xFF - 0xA1) + 0x21; \
2887 c2 = temp % (0xFF - 0xA1) + 0x21; \
2890 #define ENCODE_BIG5(charset, c1, c2, b1, b2) \
2892 unsigned int temp = (c1 - 0x21) * (0xFF - 0xA1) + (c2 - 0x21); \
2893 if (charset == charset_big5_2) \
2894 temp += BIG5_SAME_ROW * (0xC9 - 0xA1); \
2895 b1 = temp / BIG5_SAME_ROW + 0xA1; \
2896 b2 = temp % BIG5_SAME_ROW; \
2897 b2 += b2 < 0x3F ? 0x40 : 0x62; \
2900 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2901 Check if a text is encoded in SJIS. If it is, return
2902 CODING_CATEGORY_MASK_SJIS, else return 0. */
2905 detect_coding_sjis (src
, src_end
, multibytep
)
2906 unsigned char *src
, *src_end
;
2910 /* Dummy for ONE_MORE_BYTE. */
2911 struct coding_system dummy_coding
;
2912 struct coding_system
*coding
= &dummy_coding
;
2916 ONE_MORE_BYTE_CHECK_MULTIBYTE (c
, multibytep
);
2919 if (c
== 0x80 || c
== 0xA0 || c
> 0xEF)
2921 if (c
<= 0x9F || c
>= 0xE0)
2923 ONE_MORE_BYTE_CHECK_MULTIBYTE (c
, multibytep
);
2924 if (c
< 0x40 || c
== 0x7F || c
> 0xFC)
2929 return CODING_CATEGORY_MASK_SJIS
;
2932 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2933 Check if a text is encoded in BIG5. If it is, return
2934 CODING_CATEGORY_MASK_BIG5, else return 0. */
2937 detect_coding_big5 (src
, src_end
, multibytep
)
2938 unsigned char *src
, *src_end
;
2942 /* Dummy for ONE_MORE_BYTE. */
2943 struct coding_system dummy_coding
;
2944 struct coding_system
*coding
= &dummy_coding
;
2948 ONE_MORE_BYTE_CHECK_MULTIBYTE (c
, multibytep
);
2951 if (c
< 0xA1 || c
> 0xFE)
2953 ONE_MORE_BYTE_CHECK_MULTIBYTE (c
, multibytep
);
2954 if (c
< 0x40 || (c
> 0x7F && c
< 0xA1) || c
> 0xFE)
2958 return CODING_CATEGORY_MASK_BIG5
;
2961 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2962 Check if a text is encoded in UTF-8. If it is, return
2963 CODING_CATEGORY_MASK_UTF_8, else return 0. */
2965 #define UTF_8_1_OCTET_P(c) ((c) < 0x80)
2966 #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
2967 #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
2968 #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
2969 #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
2970 #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
2971 #define UTF_8_6_OCTET_LEADING_P(c) (((c) & 0xFE) == 0xFC)
2974 detect_coding_utf_8 (src
, src_end
, multibytep
)
2975 unsigned char *src
, *src_end
;
2979 int seq_maybe_bytes
;
2980 /* Dummy for ONE_MORE_BYTE. */
2981 struct coding_system dummy_coding
;
2982 struct coding_system
*coding
= &dummy_coding
;
2986 ONE_MORE_BYTE_CHECK_MULTIBYTE (c
, multibytep
);
2987 if (UTF_8_1_OCTET_P (c
))
2989 else if (UTF_8_2_OCTET_LEADING_P (c
))
2990 seq_maybe_bytes
= 1;
2991 else if (UTF_8_3_OCTET_LEADING_P (c
))
2992 seq_maybe_bytes
= 2;
2993 else if (UTF_8_4_OCTET_LEADING_P (c
))
2994 seq_maybe_bytes
= 3;
2995 else if (UTF_8_5_OCTET_LEADING_P (c
))
2996 seq_maybe_bytes
= 4;
2997 else if (UTF_8_6_OCTET_LEADING_P (c
))
2998 seq_maybe_bytes
= 5;
3004 ONE_MORE_BYTE_CHECK_MULTIBYTE (c
, multibytep
);
3005 if (!UTF_8_EXTRA_OCTET_P (c
))
3009 while (seq_maybe_bytes
> 0);
3013 return CODING_CATEGORY_MASK_UTF_8
;
3016 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
3017 Check if a text is encoded in UTF-16 Big Endian (endian == 1) or
3018 Little Endian (otherwise). If it is, return
3019 CODING_CATEGORY_MASK_UTF_16_BE or CODING_CATEGORY_MASK_UTF_16_LE,
3022 #define UTF_16_INVALID_P(val) \
3023 (((val) == 0xFFFE) \
3024 || ((val) == 0xFFFF))
3026 #define UTF_16_HIGH_SURROGATE_P(val) \
3027 (((val) & 0xD800) == 0xD800)
3029 #define UTF_16_LOW_SURROGATE_P(val) \
3030 (((val) & 0xDC00) == 0xDC00)
3033 detect_coding_utf_16 (src
, src_end
, multibytep
)
3034 unsigned char *src
, *src_end
;
3037 unsigned char c1
, c2
;
3038 /* Dummy for ONE_MORE_BYTE_CHECK_MULTIBYTE. */
3039 struct coding_system dummy_coding
;
3040 struct coding_system
*coding
= &dummy_coding
;
3042 ONE_MORE_BYTE_CHECK_MULTIBYTE (c1
, multibytep
);
3043 ONE_MORE_BYTE_CHECK_MULTIBYTE (c2
, multibytep
);
3045 if ((c1
== 0xFF) && (c2
== 0xFE))
3046 return CODING_CATEGORY_MASK_UTF_16_LE
;
3047 else if ((c1
== 0xFE) && (c2
== 0xFF))
3048 return CODING_CATEGORY_MASK_UTF_16_BE
;
3054 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
3055 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
3058 decode_coding_sjis_big5 (coding
, source
, destination
,
3059 src_bytes
, dst_bytes
, sjis_p
)
3060 struct coding_system
*coding
;
3061 const unsigned char *source
;
3062 unsigned char *destination
;
3063 int src_bytes
, dst_bytes
;
3066 const unsigned char *src
= source
;
3067 const unsigned char *src_end
= source
+ src_bytes
;
3068 unsigned char *dst
= destination
;
3069 unsigned char *dst_end
= destination
+ dst_bytes
;
3070 /* SRC_BASE remembers the start position in source in each loop.
3071 The loop will be exited when there's not enough source code
3072 (within macro ONE_MORE_BYTE), or when there's not enough
3073 destination area to produce a character (within macro
3075 const unsigned char *src_base
;
3076 Lisp_Object translation_table
;
3078 if (NILP (Venable_character_translation
))
3079 translation_table
= Qnil
;
3082 translation_table
= coding
->translation_table_for_decode
;
3083 if (NILP (translation_table
))
3084 translation_table
= Vstandard_translation_table_for_decode
;
3087 coding
->produced_char
= 0;
3090 int c
, charset
, c1
, c2
= 0;
3097 charset
= CHARSET_ASCII
;
3102 if (coding
->eol_type
== CODING_EOL_CRLF
)
3108 /* To process C2 again, SRC is subtracted by 1. */
3111 else if (coding
->eol_type
== CODING_EOL_CR
)
3115 && (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
)
3116 && (coding
->eol_type
== CODING_EOL_CR
3117 || coding
->eol_type
== CODING_EOL_CRLF
))
3119 coding
->result
= CODING_FINISH_INCONSISTENT_EOL
;
3120 goto label_end_of_loop
;
3128 if (c1
== 0x80 || c1
== 0xA0 || c1
> 0xEF)
3129 goto label_invalid_code
;
3130 if (c1
<= 0x9F || c1
>= 0xE0)
3132 /* SJIS -> JISX0208 */
3134 if (c2
< 0x40 || c2
== 0x7F || c2
> 0xFC)
3135 goto label_invalid_code
;
3136 DECODE_SJIS (c1
, c2
, c1
, c2
);
3137 charset
= charset_jisx0208
;
3140 /* SJIS -> JISX0201-Kana */
3141 charset
= charset_katakana_jisx0201
;
3146 if (c1
< 0xA0 || c1
> 0xFE)
3147 goto label_invalid_code
;
3149 if (c2
< 0x40 || (c2
> 0x7E && c2
< 0xA1) || c2
> 0xFE)
3150 goto label_invalid_code
;
3151 DECODE_BIG5 (c1
, c2
, charset
, c1
, c2
);
3155 c
= DECODE_ISO_CHARACTER (charset
, c1
, c2
);
3167 coding
->consumed
= coding
->consumed_char
= src_base
- source
;
3168 coding
->produced
= dst
- destination
;
3172 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
3173 This function can encode charsets `ascii', `katakana-jisx0201',
3174 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
3175 are sure that all these charsets are registered as official charset
3176 (i.e. do not have extended leading-codes). Characters of other
3177 charsets are produced without any encoding. If SJIS_P is 1, encode
3178 SJIS text, else encode BIG5 text. */
3181 encode_coding_sjis_big5 (coding
, source
, destination
,
3182 src_bytes
, dst_bytes
, sjis_p
)
3183 struct coding_system
*coding
;
3184 unsigned char *source
, *destination
;
3185 int src_bytes
, dst_bytes
;
3188 unsigned char *src
= source
;
3189 unsigned char *src_end
= source
+ src_bytes
;
3190 unsigned char *dst
= destination
;
3191 unsigned char *dst_end
= destination
+ dst_bytes
;
3192 /* SRC_BASE remembers the start position in source in each loop.
3193 The loop will be exited when there's not enough source text to
3194 analyze multi-byte codes (within macro ONE_MORE_CHAR), or when
3195 there's not enough destination area to produce encoded codes
3196 (within macro EMIT_BYTES). */
3197 unsigned char *src_base
;
3198 Lisp_Object translation_table
;
3200 if (NILP (Venable_character_translation
))
3201 translation_table
= Qnil
;
3204 translation_table
= coding
->translation_table_for_encode
;
3205 if (NILP (translation_table
))
3206 translation_table
= Vstandard_translation_table_for_encode
;
3211 int c
, charset
, c1
, c2
;
3216 /* Now encode the character C. */
3217 if (SINGLE_BYTE_CHAR_P (c
))
3222 if (!(coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
))
3229 if (coding
->eol_type
== CODING_EOL_CRLF
)
3231 EMIT_TWO_BYTES ('\r', c
);
3234 else if (coding
->eol_type
== CODING_EOL_CR
)
3242 SPLIT_CHAR (c
, charset
, c1
, c2
);
3245 if (charset
== charset_jisx0208
3246 || charset
== charset_jisx0208_1978
)
3248 ENCODE_SJIS (c1
, c2
, c1
, c2
);
3249 EMIT_TWO_BYTES (c1
, c2
);
3251 else if (charset
== charset_katakana_jisx0201
)
3252 EMIT_ONE_BYTE (c1
| 0x80);
3253 else if (charset
== charset_latin_jisx0201
)
3255 else if (coding
->mode
& CODING_MODE_INHIBIT_UNENCODABLE_CHAR
)
3257 EMIT_ONE_BYTE (CODING_REPLACEMENT_CHARACTER
);
3258 if (CHARSET_WIDTH (charset
) > 1)
3259 EMIT_ONE_BYTE (CODING_REPLACEMENT_CHARACTER
);
3262 /* There's no way other than producing the internal
3264 EMIT_BYTES (src_base
, src
);
3268 if (charset
== charset_big5_1
|| charset
== charset_big5_2
)
3270 ENCODE_BIG5 (charset
, c1
, c2
, c1
, c2
);
3271 EMIT_TWO_BYTES (c1
, c2
);
3273 else if (coding
->mode
& CODING_MODE_INHIBIT_UNENCODABLE_CHAR
)
3275 EMIT_ONE_BYTE (CODING_REPLACEMENT_CHARACTER
);
3276 if (CHARSET_WIDTH (charset
) > 1)
3277 EMIT_ONE_BYTE (CODING_REPLACEMENT_CHARACTER
);
3280 /* There's no way other than producing the internal
3282 EMIT_BYTES (src_base
, src
);
3285 coding
->consumed_char
++;
3289 coding
->consumed
= src_base
- source
;
3290 coding
->produced
= coding
->produced_char
= dst
- destination
;
3294 /*** 5. CCL handlers ***/
3296 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
3297 Check if a text is encoded in a coding system of which
3298 encoder/decoder are written in CCL program. If it is, return
3299 CODING_CATEGORY_MASK_CCL, else return 0. */
3302 detect_coding_ccl (src
, src_end
, multibytep
)
3303 unsigned char *src
, *src_end
;
3306 unsigned char *valid
;
3308 /* Dummy for ONE_MORE_BYTE. */
3309 struct coding_system dummy_coding
;
3310 struct coding_system
*coding
= &dummy_coding
;
3312 /* No coding system is assigned to coding-category-ccl. */
3313 if (!coding_system_table
[CODING_CATEGORY_IDX_CCL
])
3316 valid
= coding_system_table
[CODING_CATEGORY_IDX_CCL
]->spec
.ccl
.valid_codes
;
3319 ONE_MORE_BYTE_CHECK_MULTIBYTE (c
, multibytep
);
3324 return CODING_CATEGORY_MASK_CCL
;
3328 /*** 6. End-of-line handlers ***/
3330 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
3333 decode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
)
3334 struct coding_system
*coding
;
3335 const unsigned char *source
;
3336 unsigned char *destination
;
3337 int src_bytes
, dst_bytes
;
3339 const unsigned char *src
= source
;
3340 unsigned char *dst
= destination
;
3341 const unsigned char *src_end
= src
+ src_bytes
;
3342 unsigned char *dst_end
= dst
+ dst_bytes
;
3343 Lisp_Object translation_table
;
3344 /* SRC_BASE remembers the start position in source in each loop.
3345 The loop will be exited when there's not enough source code
3346 (within macro ONE_MORE_BYTE), or when there's not enough
3347 destination area to produce a character (within macro
3349 const unsigned char *src_base
;
3352 translation_table
= Qnil
;
3353 switch (coding
->eol_type
)
3355 case CODING_EOL_CRLF
:
3370 && (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
))
3372 coding
->result
= CODING_FINISH_INCONSISTENT_EOL
;
3373 goto label_end_of_loop
;
3386 if (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
)
3388 coding
->result
= CODING_FINISH_INCONSISTENT_EOL
;
3389 goto label_end_of_loop
;
3398 default: /* no need for EOL handling */
3408 coding
->consumed
= coding
->consumed_char
= src_base
- source
;
3409 coding
->produced
= dst
- destination
;
3413 /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". Encode
3414 format of end-of-line according to `coding->eol_type'. It also
3415 convert multibyte form 8-bit characters to unibyte if
3416 CODING->src_multibyte is nonzero. If `coding->mode &
3417 CODING_MODE_SELECTIVE_DISPLAY' is nonzero, code '\r' in source text
3418 also means end-of-line. */
3421 encode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
)
3422 struct coding_system
*coding
;
3423 const unsigned char *source
;
3424 unsigned char *destination
;
3425 int src_bytes
, dst_bytes
;
3427 const unsigned char *src
= source
;
3428 unsigned char *dst
= destination
;
3429 const unsigned char *src_end
= src
+ src_bytes
;
3430 unsigned char *dst_end
= dst
+ dst_bytes
;
3431 Lisp_Object translation_table
;
3432 /* SRC_BASE remembers the start position in source in each loop.
3433 The loop will be exited when there's not enough source text to
3434 analyze multi-byte codes (within macro ONE_MORE_CHAR), or when
3435 there's not enough destination area to produce encoded codes
3436 (within macro EMIT_BYTES). */
3437 const unsigned char *src_base
;
3440 int selective_display
= coding
->mode
& CODING_MODE_SELECTIVE_DISPLAY
;
3442 translation_table
= Qnil
;
3443 if (coding
->src_multibyte
3444 && *(src_end
- 1) == LEADING_CODE_8_BIT_CONTROL
)
3448 coding
->result
= CODING_FINISH_INSUFFICIENT_SRC
;
3451 if (coding
->eol_type
== CODING_EOL_CRLF
)
3453 while (src
< src_end
)
3459 else if (c
== '\n' || (c
== '\r' && selective_display
))
3460 EMIT_TWO_BYTES ('\r', '\n');
3470 if (!dst_bytes
|| src_bytes
<= dst_bytes
)
3472 safe_bcopy (src
, dst
, src_bytes
);
3478 if (coding
->src_multibyte
3479 && *(src
+ dst_bytes
- 1) == LEADING_CODE_8_BIT_CONTROL
)
3481 safe_bcopy (src
, dst
, dst_bytes
);
3482 src_base
= src
+ dst_bytes
;
3483 dst
= destination
+ dst_bytes
;
3484 coding
->result
= CODING_FINISH_INSUFFICIENT_DST
;
3486 if (coding
->eol_type
== CODING_EOL_CR
)
3488 for (tmp
= destination
; tmp
< dst
; tmp
++)
3489 if (*tmp
== '\n') *tmp
= '\r';
3491 else if (selective_display
)
3493 for (tmp
= destination
; tmp
< dst
; tmp
++)
3494 if (*tmp
== '\r') *tmp
= '\n';
3497 if (coding
->src_multibyte
)
3498 dst
= destination
+ str_as_unibyte (destination
, dst
- destination
);
3500 coding
->consumed
= src_base
- source
;
3501 coding
->produced
= dst
- destination
;
3502 coding
->produced_char
= coding
->produced
;
3506 /*** 7. C library functions ***/
3508 /* In Emacs Lisp, a coding system is represented by a Lisp symbol which
3509 has a property `coding-system'. The value of this property is a
3510 vector of length 5 (called the coding-vector). Among elements of
3511 this vector, the first (element[0]) and the fifth (element[4])
3512 carry important information for decoding/encoding. Before
3513 decoding/encoding, this information should be set in fields of a
3514 structure of type `coding_system'.
3516 The value of the property `coding-system' can be a symbol of another
3517 subsidiary coding-system. In that case, Emacs gets coding-vector
3520 `element[0]' contains information to be set in `coding->type'. The
3521 value and its meaning is as follows:
3523 0 -- coding_type_emacs_mule
3524 1 -- coding_type_sjis
3525 2 -- coding_type_iso2022
3526 3 -- coding_type_big5
3527 4 -- coding_type_ccl encoder/decoder written in CCL
3528 nil -- coding_type_no_conversion
3529 t -- coding_type_undecided (automatic conversion on decoding,
3530 no-conversion on encoding)
3532 `element[4]' contains information to be set in `coding->flags' and
3533 `coding->spec'. The meaning varies by `coding->type'.
3535 If `coding->type' is `coding_type_iso2022', element[4] is a vector
3536 of length 32 (of which the first 13 sub-elements are used now).
3537 Meanings of these sub-elements are:
3539 sub-element[N] where N is 0 through 3: to be set in `coding->spec.iso2022'
3540 If the value is an integer of valid charset, the charset is
3541 assumed to be designated to graphic register N initially.
3543 If the value is minus, it is a minus value of charset which
3544 reserves graphic register N, which means that the charset is
3545 not designated initially but should be designated to graphic
3546 register N just before encoding a character in that charset.
3548 If the value is nil, graphic register N is never used on
3551 sub-element[N] where N is 4 through 11: to be set in `coding->flags'
3552 Each value takes t or nil. See the section ISO2022 of
3553 `coding.h' for more information.
3555 If `coding->type' is `coding_type_big5', element[4] is t to denote
3556 BIG5-ETen or nil to denote BIG5-HKU.
3558 If `coding->type' takes the other value, element[4] is ignored.
3560 Emacs Lisp's coding systems also carry information about format of
3561 end-of-line in a value of property `eol-type'. If the value is
3562 integer, 0 means CODING_EOL_LF, 1 means CODING_EOL_CRLF, and 2
3563 means CODING_EOL_CR. If it is not integer, it should be a vector
3564 of subsidiary coding systems of which property `eol-type' has one
3565 of the above values.
3569 /* Extract information for decoding/encoding from CODING_SYSTEM_SYMBOL
3570 and set it in CODING. If CODING_SYSTEM_SYMBOL is invalid, CODING
3571 is setup so that no conversion is necessary and return -1, else
3575 setup_coding_system (coding_system
, coding
)
3576 Lisp_Object coding_system
;
3577 struct coding_system
*coding
;
3579 Lisp_Object coding_spec
, coding_type
, eol_type
, plist
;
3582 /* At first, zero clear all members. */
3583 bzero (coding
, sizeof (struct coding_system
));
3585 /* Initialize some fields required for all kinds of coding systems. */
3586 coding
->symbol
= coding_system
;
3587 coding
->heading_ascii
= -1;
3588 coding
->post_read_conversion
= coding
->pre_write_conversion
= Qnil
;
3589 coding
->composing
= COMPOSITION_DISABLED
;
3590 coding
->cmp_data
= NULL
;
3592 if (NILP (coding_system
))
3593 goto label_invalid_coding_system
;
3595 coding_spec
= Fget (coding_system
, Qcoding_system
);
3597 if (!VECTORP (coding_spec
)
3598 || XVECTOR (coding_spec
)->size
!= 5
3599 || !CONSP (XVECTOR (coding_spec
)->contents
[3]))
3600 goto label_invalid_coding_system
;
3602 eol_type
= inhibit_eol_conversion
? Qnil
: Fget (coding_system
, Qeol_type
);
3603 if (VECTORP (eol_type
))
3605 coding
->eol_type
= CODING_EOL_UNDECIDED
;
3606 coding
->common_flags
= CODING_REQUIRE_DETECTION_MASK
;
3608 else if (XFASTINT (eol_type
) == 1)
3610 coding
->eol_type
= CODING_EOL_CRLF
;
3611 coding
->common_flags
3612 = CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
;
3614 else if (XFASTINT (eol_type
) == 2)
3616 coding
->eol_type
= CODING_EOL_CR
;
3617 coding
->common_flags
3618 = CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
;
3621 coding
->eol_type
= CODING_EOL_LF
;
3623 coding_type
= XVECTOR (coding_spec
)->contents
[0];
3624 /* Try short cut. */
3625 if (SYMBOLP (coding_type
))
3627 if (EQ (coding_type
, Qt
))
3629 coding
->type
= coding_type_undecided
;
3630 coding
->common_flags
|= CODING_REQUIRE_DETECTION_MASK
;
3633 coding
->type
= coding_type_no_conversion
;
3634 /* Initialize this member. Any thing other than
3635 CODING_CATEGORY_IDX_UTF_16_BE and
3636 CODING_CATEGORY_IDX_UTF_16_LE are ok because they have
3637 special treatment in detect_eol. */
3638 coding
->category_idx
= CODING_CATEGORY_IDX_EMACS_MULE
;
3643 /* Get values of coding system properties:
3644 `post-read-conversion', `pre-write-conversion',
3645 `translation-table-for-decode', `translation-table-for-encode'. */
3646 plist
= XVECTOR (coding_spec
)->contents
[3];
3647 /* Pre & post conversion functions should be disabled if
3648 inhibit_eol_conversion is nonzero. This is the case that a code
3649 conversion function is called while those functions are running. */
3650 if (! inhibit_pre_post_conversion
)
3652 coding
->post_read_conversion
= Fplist_get (plist
, Qpost_read_conversion
);
3653 coding
->pre_write_conversion
= Fplist_get (plist
, Qpre_write_conversion
);
3655 val
= Fplist_get (plist
, Qtranslation_table_for_decode
);
3657 val
= Fget (val
, Qtranslation_table_for_decode
);
3658 coding
->translation_table_for_decode
= CHAR_TABLE_P (val
) ? val
: Qnil
;
3659 val
= Fplist_get (plist
, Qtranslation_table_for_encode
);
3661 val
= Fget (val
, Qtranslation_table_for_encode
);
3662 coding
->translation_table_for_encode
= CHAR_TABLE_P (val
) ? val
: Qnil
;
3663 val
= Fplist_get (plist
, Qcoding_category
);
3666 val
= Fget (val
, Qcoding_category_index
);
3668 coding
->category_idx
= XINT (val
);
3670 goto label_invalid_coding_system
;
3673 goto label_invalid_coding_system
;
3675 /* If the coding system has non-nil `composition' property, enable
3676 composition handling. */
3677 val
= Fplist_get (plist
, Qcomposition
);
3679 coding
->composing
= COMPOSITION_NO
;
3681 switch (XFASTINT (coding_type
))
3684 coding
->type
= coding_type_emacs_mule
;
3685 coding
->common_flags
3686 |= CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
;
3687 if (!NILP (coding
->post_read_conversion
))
3688 coding
->common_flags
|= CODING_REQUIRE_DECODING_MASK
;
3689 if (!NILP (coding
->pre_write_conversion
))
3690 coding
->common_flags
|= CODING_REQUIRE_ENCODING_MASK
;
3694 coding
->type
= coding_type_sjis
;
3695 coding
->common_flags
3696 |= CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
;
3700 coding
->type
= coding_type_iso2022
;
3701 coding
->common_flags
3702 |= CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
;
3704 Lisp_Object val
, temp
;
3706 int i
, charset
, reg_bits
= 0;
3708 val
= XVECTOR (coding_spec
)->contents
[4];
3710 if (!VECTORP (val
) || XVECTOR (val
)->size
!= 32)
3711 goto label_invalid_coding_system
;
3713 flags
= XVECTOR (val
)->contents
;
3715 = ((NILP (flags
[4]) ? 0 : CODING_FLAG_ISO_SHORT_FORM
)
3716 | (NILP (flags
[5]) ? 0 : CODING_FLAG_ISO_RESET_AT_EOL
)
3717 | (NILP (flags
[6]) ? 0 : CODING_FLAG_ISO_RESET_AT_CNTL
)
3718 | (NILP (flags
[7]) ? 0 : CODING_FLAG_ISO_SEVEN_BITS
)
3719 | (NILP (flags
[8]) ? 0 : CODING_FLAG_ISO_LOCKING_SHIFT
)
3720 | (NILP (flags
[9]) ? 0 : CODING_FLAG_ISO_SINGLE_SHIFT
)
3721 | (NILP (flags
[10]) ? 0 : CODING_FLAG_ISO_USE_ROMAN
)
3722 | (NILP (flags
[11]) ? 0 : CODING_FLAG_ISO_USE_OLDJIS
)
3723 | (NILP (flags
[12]) ? 0 : CODING_FLAG_ISO_NO_DIRECTION
)
3724 | (NILP (flags
[13]) ? 0 : CODING_FLAG_ISO_INIT_AT_BOL
)
3725 | (NILP (flags
[14]) ? 0 : CODING_FLAG_ISO_DESIGNATE_AT_BOL
)
3726 | (NILP (flags
[15]) ? 0 : CODING_FLAG_ISO_SAFE
)
3727 | (NILP (flags
[16]) ? 0 : CODING_FLAG_ISO_LATIN_EXTRA
)
3730 /* Invoke graphic register 0 to plane 0. */
3731 CODING_SPEC_ISO_INVOCATION (coding
, 0) = 0;
3732 /* Invoke graphic register 1 to plane 1 if we can use full 8-bit. */
3733 CODING_SPEC_ISO_INVOCATION (coding
, 1)
3734 = (coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
? -1 : 1);
3735 /* Not single shifting at first. */
3736 CODING_SPEC_ISO_SINGLE_SHIFTING (coding
) = 0;
3737 /* Beginning of buffer should also be regarded as bol. */
3738 CODING_SPEC_ISO_BOL (coding
) = 1;
3740 for (charset
= 0; charset
<= MAX_CHARSET
; charset
++)
3741 CODING_SPEC_ISO_REVISION_NUMBER (coding
, charset
) = 255;
3742 val
= Vcharset_revision_alist
;
3745 charset
= get_charset_id (Fcar_safe (XCAR (val
)));
3747 && (temp
= Fcdr_safe (XCAR (val
)), INTEGERP (temp
))
3748 && (i
= XINT (temp
), (i
>= 0 && (i
+ '@') < 128)))
3749 CODING_SPEC_ISO_REVISION_NUMBER (coding
, charset
) = i
;
3753 /* Checks FLAGS[REG] (REG = 0, 1, 2 3) and decide designations.
3754 FLAGS[REG] can be one of below:
3755 integer CHARSET: CHARSET occupies register I,
3756 t: designate nothing to REG initially, but can be used
3758 list of integer, nil, or t: designate the first
3759 element (if integer) to REG initially, the remaining
3760 elements (if integer) is designated to REG on request,
3761 if an element is t, REG can be used by any charsets,
3762 nil: REG is never used. */
3763 for (charset
= 0; charset
<= MAX_CHARSET
; charset
++)
3764 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
3765 = CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
;
3766 for (i
= 0; i
< 4; i
++)
3768 if ((INTEGERP (flags
[i
])
3769 && (charset
= XINT (flags
[i
]), CHARSET_VALID_P (charset
)))
3770 || (charset
= get_charset_id (flags
[i
])) >= 0)
3772 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = charset
;
3773 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
) = i
;
3775 else if (EQ (flags
[i
], Qt
))
3777 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
3779 coding
->flags
|= CODING_FLAG_ISO_DESIGNATION
;
3781 else if (CONSP (flags
[i
]))
3786 coding
->flags
|= CODING_FLAG_ISO_DESIGNATION
;
3787 if ((INTEGERP (XCAR (tail
))
3788 && (charset
= XINT (XCAR (tail
)),
3789 CHARSET_VALID_P (charset
)))
3790 || (charset
= get_charset_id (XCAR (tail
))) >= 0)
3792 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = charset
;
3793 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
) =i
;
3796 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
3798 while (CONSP (tail
))
3800 if ((INTEGERP (XCAR (tail
))
3801 && (charset
= XINT (XCAR (tail
)),
3802 CHARSET_VALID_P (charset
)))
3803 || (charset
= get_charset_id (XCAR (tail
))) >= 0)
3804 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
3806 else if (EQ (XCAR (tail
), Qt
))
3812 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
) = -1;
3814 CODING_SPEC_ISO_DESIGNATION (coding
, i
)
3815 = CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, i
);
3818 if (reg_bits
&& ! (coding
->flags
& CODING_FLAG_ISO_LOCKING_SHIFT
))
3820 /* REG 1 can be used only by locking shift in 7-bit env. */
3821 if (coding
->flags
& CODING_FLAG_ISO_SEVEN_BITS
)
3823 if (! (coding
->flags
& CODING_FLAG_ISO_SINGLE_SHIFT
))
3824 /* Without any shifting, only REG 0 and 1 can be used. */
3829 for (charset
= 0; charset
<= MAX_CHARSET
; charset
++)
3831 if (CHARSET_DEFINED_P (charset
)
3832 && (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
3833 == CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION
))
3835 /* There exist some default graphic registers to be
3838 /* We had better avoid designating a charset of
3839 CHARS96 to REG 0 as far as possible. */
3840 if (CHARSET_CHARS (charset
) == 96)
3841 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
3843 ? 1 : (reg_bits
& 4 ? 2 : (reg_bits
& 8 ? 3 : 0)));
3845 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding
, charset
)
3847 ? 0 : (reg_bits
& 2 ? 1 : (reg_bits
& 4 ? 2 : 3)));
3851 coding
->common_flags
|= CODING_REQUIRE_FLUSHING_MASK
;
3852 coding
->spec
.iso2022
.last_invalid_designation_register
= -1;
3856 coding
->type
= coding_type_big5
;
3857 coding
->common_flags
3858 |= CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
;
3860 = (NILP (XVECTOR (coding_spec
)->contents
[4])
3861 ? CODING_FLAG_BIG5_HKU
3862 : CODING_FLAG_BIG5_ETEN
);
3866 coding
->type
= coding_type_ccl
;
3867 coding
->common_flags
3868 |= CODING_REQUIRE_DECODING_MASK
| CODING_REQUIRE_ENCODING_MASK
;
3870 val
= XVECTOR (coding_spec
)->contents
[4];
3872 || setup_ccl_program (&(coding
->spec
.ccl
.decoder
),
3874 || setup_ccl_program (&(coding
->spec
.ccl
.encoder
),
3876 goto label_invalid_coding_system
;
3878 bzero (coding
->spec
.ccl
.valid_codes
, 256);
3879 val
= Fplist_get (plist
, Qvalid_codes
);
3884 for (; CONSP (val
); val
= XCDR (val
))
3888 && XINT (this) >= 0 && XINT (this) < 256)
3889 coding
->spec
.ccl
.valid_codes
[XINT (this)] = 1;
3890 else if (CONSP (this)
3891 && INTEGERP (XCAR (this))
3892 && INTEGERP (XCDR (this)))
3894 int start
= XINT (XCAR (this));
3895 int end
= XINT (XCDR (this));
3897 if (start
>= 0 && start
<= end
&& end
< 256)
3898 while (start
<= end
)
3899 coding
->spec
.ccl
.valid_codes
[start
++] = 1;
3904 coding
->common_flags
|= CODING_REQUIRE_FLUSHING_MASK
;
3905 coding
->spec
.ccl
.cr_carryover
= 0;
3906 coding
->spec
.ccl
.eight_bit_carryover
[0] = 0;
3910 coding
->type
= coding_type_raw_text
;
3914 goto label_invalid_coding_system
;
3918 label_invalid_coding_system
:
3919 coding
->type
= coding_type_no_conversion
;
3920 coding
->category_idx
= CODING_CATEGORY_IDX_BINARY
;
3921 coding
->common_flags
= 0;
3922 coding
->eol_type
= CODING_EOL_LF
;
3923 coding
->pre_write_conversion
= coding
->post_read_conversion
= Qnil
;
3927 /* Free memory blocks allocated for storing composition information. */
3930 coding_free_composition_data (coding
)
3931 struct coding_system
*coding
;
3933 struct composition_data
*cmp_data
= coding
->cmp_data
, *next
;
3937 /* Memory blocks are chained. At first, rewind to the first, then,
3938 free blocks one by one. */
3939 while (cmp_data
->prev
)
3940 cmp_data
= cmp_data
->prev
;
3943 next
= cmp_data
->next
;
3947 coding
->cmp_data
= NULL
;
3950 /* Set `char_offset' member of all memory blocks pointed by
3951 coding->cmp_data to POS. */
3954 coding_adjust_composition_offset (coding
, pos
)
3955 struct coding_system
*coding
;
3958 struct composition_data
*cmp_data
;
3960 for (cmp_data
= coding
->cmp_data
; cmp_data
; cmp_data
= cmp_data
->next
)
3961 cmp_data
->char_offset
= pos
;
3964 /* Setup raw-text or one of its subsidiaries in the structure
3965 coding_system CODING according to the already setup value eol_type
3966 in CODING. CODING should be setup for some coding system in
3970 setup_raw_text_coding_system (coding
)
3971 struct coding_system
*coding
;
3973 if (coding
->type
!= coding_type_raw_text
)
3975 coding
->symbol
= Qraw_text
;
3976 coding
->type
= coding_type_raw_text
;
3977 if (coding
->eol_type
!= CODING_EOL_UNDECIDED
)
3979 Lisp_Object subsidiaries
;
3980 subsidiaries
= Fget (Qraw_text
, Qeol_type
);
3982 if (VECTORP (subsidiaries
)
3983 && XVECTOR (subsidiaries
)->size
== 3)
3985 = XVECTOR (subsidiaries
)->contents
[coding
->eol_type
];
3987 setup_coding_system (coding
->symbol
, coding
);
3992 /* Emacs has a mechanism to automatically detect a coding system if it
3993 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
3994 it's impossible to distinguish some coding systems accurately
3995 because they use the same range of codes. So, at first, coding
3996 systems are categorized into 7, those are:
3998 o coding-category-emacs-mule
4000 The category for a coding system which has the same code range
4001 as Emacs' internal format. Assigned the coding-system (Lisp
4002 symbol) `emacs-mule' by default.
4004 o coding-category-sjis
4006 The category for a coding system which has the same code range
4007 as SJIS. Assigned the coding-system (Lisp
4008 symbol) `japanese-shift-jis' by default.
4010 o coding-category-iso-7
4012 The category for a coding system which has the same code range
4013 as ISO2022 of 7-bit environment. This doesn't use any locking
4014 shift and single shift functions. This can encode/decode all
4015 charsets. Assigned the coding-system (Lisp symbol)
4016 `iso-2022-7bit' by default.
4018 o coding-category-iso-7-tight
4020 Same as coding-category-iso-7 except that this can
4021 encode/decode only the specified charsets.
4023 o coding-category-iso-8-1
4025 The category for a coding system which has the same code range
4026 as ISO2022 of 8-bit environment and graphic plane 1 used only
4027 for DIMENSION1 charset. This doesn't use any locking shift
4028 and single shift functions. Assigned the coding-system (Lisp
4029 symbol) `iso-latin-1' by default.
4031 o coding-category-iso-8-2
4033 The category for a coding system which has the same code range
4034 as ISO2022 of 8-bit environment and graphic plane 1 used only
4035 for DIMENSION2 charset. This doesn't use any locking shift
4036 and single shift functions. Assigned the coding-system (Lisp
4037 symbol) `japanese-iso-8bit' by default.
4039 o coding-category-iso-7-else
4041 The category for a coding system which has the same code range
4042 as ISO2022 of 7-bit environment but uses locking shift or
4043 single shift functions. Assigned the coding-system (Lisp
4044 symbol) `iso-2022-7bit-lock' by default.
4046 o coding-category-iso-8-else
4048 The category for a coding system which has the same code range
4049 as ISO2022 of 8-bit environment but uses locking shift or
4050 single shift functions. Assigned the coding-system (Lisp
4051 symbol) `iso-2022-8bit-ss2' by default.
4053 o coding-category-big5
4055 The category for a coding system which has the same code range
4056 as BIG5. Assigned the coding-system (Lisp symbol)
4057 `cn-big5' by default.
4059 o coding-category-utf-8
4061 The category for a coding system which has the same code range
4062 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
4063 symbol) `utf-8' by default.
4065 o coding-category-utf-16-be
4067 The category for a coding system in which a text has an
4068 Unicode signature (cf. Unicode Standard) in the order of BIG
4069 endian at the head. Assigned the coding-system (Lisp symbol)
4070 `utf-16-be' by default.
4072 o coding-category-utf-16-le
4074 The category for a coding system in which a text has an
4075 Unicode signature (cf. Unicode Standard) in the order of
4076 LITTLE endian at the head. Assigned the coding-system (Lisp
4077 symbol) `utf-16-le' by default.
4079 o coding-category-ccl
4081 The category for a coding system of which encoder/decoder is
4082 written in CCL programs. The default value is nil, i.e., no
4083 coding system is assigned.
4085 o coding-category-binary
4087 The category for a coding system not categorized in any of the
4088 above. Assigned the coding-system (Lisp symbol)
4089 `no-conversion' by default.
4091 Each of them is a Lisp symbol and the value is an actual
4092 `coding-system' (this is also a Lisp symbol) assigned by a user.
4093 What Emacs does actually is to detect a category of coding system.
4094 Then, it uses a `coding-system' assigned to it. If Emacs can't
4095 decide a single possible category, it selects a category of the
4096 highest priority. Priorities of categories are also specified by a
4097 user in a Lisp variable `coding-category-list'.
4102 int ascii_skip_code
[256];
4104 /* Detect how a text of length SRC_BYTES pointed by SOURCE is encoded.
4105 If it detects possible coding systems, return an integer in which
4106 appropriate flag bits are set. Flag bits are defined by macros
4107 CODING_CATEGORY_MASK_XXX in `coding.h'. If PRIORITIES is non-NULL,
4108 it should point the table `coding_priorities'. In that case, only
4109 the flag bit for a coding system of the highest priority is set in
4110 the returned value. If MULTIBYTEP is nonzero, 8-bit codes of the
4111 range 0x80..0x9F are in multibyte form.
4113 How many ASCII characters are at the head is returned as *SKIP. */
4116 detect_coding_mask (source
, src_bytes
, priorities
, skip
, multibytep
)
4117 unsigned char *source
;
4118 int src_bytes
, *priorities
, *skip
;
4121 register unsigned char c
;
4122 unsigned char *src
= source
, *src_end
= source
+ src_bytes
;
4123 unsigned int mask
, utf16_examined_p
, iso2022_examined_p
;
4126 /* At first, skip all ASCII characters and control characters except
4127 for three ISO2022 specific control characters. */
4128 ascii_skip_code
[ISO_CODE_SO
] = 0;
4129 ascii_skip_code
[ISO_CODE_SI
] = 0;
4130 ascii_skip_code
[ISO_CODE_ESC
] = 0;
4132 label_loop_detect_coding
:
4133 while (src
< src_end
&& ascii_skip_code
[*src
]) src
++;
4134 *skip
= src
- source
;
4137 /* We found nothing other than ASCII. There's nothing to do. */
4141 /* The text seems to be encoded in some multilingual coding system.
4142 Now, try to find in which coding system the text is encoded. */
4145 /* i.e. (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) */
4146 /* C is an ISO2022 specific control code of C0. */
4147 mask
= detect_coding_iso2022 (src
, src_end
, multibytep
);
4150 /* No valid ISO2022 code follows C. Try again. */
4152 if (c
== ISO_CODE_ESC
)
4153 ascii_skip_code
[ISO_CODE_ESC
] = 1;
4155 ascii_skip_code
[ISO_CODE_SO
] = ascii_skip_code
[ISO_CODE_SI
] = 1;
4156 goto label_loop_detect_coding
;
4160 for (i
= 0; i
< CODING_CATEGORY_IDX_MAX
; i
++)
4162 if (mask
& priorities
[i
])
4163 return priorities
[i
];
4165 return CODING_CATEGORY_MASK_RAW_TEXT
;
4172 if (multibytep
&& c
== LEADING_CODE_8_BIT_CONTROL
)
4177 /* C is the first byte of SJIS character code,
4178 or a leading-code of Emacs' internal format (emacs-mule),
4179 or the first byte of UTF-16. */
4180 try = (CODING_CATEGORY_MASK_SJIS
4181 | CODING_CATEGORY_MASK_EMACS_MULE
4182 | CODING_CATEGORY_MASK_UTF_16_BE
4183 | CODING_CATEGORY_MASK_UTF_16_LE
);
4185 /* Or, if C is a special latin extra code,
4186 or is an ISO2022 specific control code of C1 (SS2 or SS3),
4187 or is an ISO2022 control-sequence-introducer (CSI),
4188 we should also consider the possibility of ISO2022 codings. */
4189 if ((VECTORP (Vlatin_extra_code_table
)
4190 && !NILP (XVECTOR (Vlatin_extra_code_table
)->contents
[c
]))
4191 || (c
== ISO_CODE_SS2
|| c
== ISO_CODE_SS3
)
4192 || (c
== ISO_CODE_CSI
4195 || ((*src
== '0' || *src
== '1' || *src
== '2')
4196 && src
+ 1 < src_end
4197 && src
[1] == ']')))))
4198 try |= (CODING_CATEGORY_MASK_ISO_8_ELSE
4199 | CODING_CATEGORY_MASK_ISO_8BIT
);
4202 /* C is a character of ISO2022 in graphic plane right,
4203 or a SJIS's 1-byte character code (i.e. JISX0201),
4204 or the first byte of BIG5's 2-byte code,
4205 or the first byte of UTF-8/16. */
4206 try = (CODING_CATEGORY_MASK_ISO_8_ELSE
4207 | CODING_CATEGORY_MASK_ISO_8BIT
4208 | CODING_CATEGORY_MASK_SJIS
4209 | CODING_CATEGORY_MASK_BIG5
4210 | CODING_CATEGORY_MASK_UTF_8
4211 | CODING_CATEGORY_MASK_UTF_16_BE
4212 | CODING_CATEGORY_MASK_UTF_16_LE
);
4214 /* Or, we may have to consider the possibility of CCL. */
4215 if (coding_system_table
[CODING_CATEGORY_IDX_CCL
]
4216 && (coding_system_table
[CODING_CATEGORY_IDX_CCL
]
4217 ->spec
.ccl
.valid_codes
)[c
])
4218 try |= CODING_CATEGORY_MASK_CCL
;
4221 utf16_examined_p
= iso2022_examined_p
= 0;
4224 for (i
= 0; i
< CODING_CATEGORY_IDX_MAX
; i
++)
4226 if (!iso2022_examined_p
4227 && (priorities
[i
] & try & CODING_CATEGORY_MASK_ISO
))
4229 mask
|= detect_coding_iso2022 (src
, src_end
, multibytep
);
4230 iso2022_examined_p
= 1;
4232 else if (priorities
[i
] & try & CODING_CATEGORY_MASK_SJIS
)
4233 mask
|= detect_coding_sjis (src
, src_end
, multibytep
);
4234 else if (priorities
[i
] & try & CODING_CATEGORY_MASK_UTF_8
)
4235 mask
|= detect_coding_utf_8 (src
, src_end
, multibytep
);
4236 else if (!utf16_examined_p
4237 && (priorities
[i
] & try &
4238 CODING_CATEGORY_MASK_UTF_16_BE_LE
))
4240 mask
|= detect_coding_utf_16 (src
, src_end
, multibytep
);
4241 utf16_examined_p
= 1;
4243 else if (priorities
[i
] & try & CODING_CATEGORY_MASK_BIG5
)
4244 mask
|= detect_coding_big5 (src
, src_end
, multibytep
);
4245 else if (priorities
[i
] & try & CODING_CATEGORY_MASK_EMACS_MULE
)
4246 mask
|= detect_coding_emacs_mule (src
, src_end
, multibytep
);
4247 else if (priorities
[i
] & try & CODING_CATEGORY_MASK_CCL
)
4248 mask
|= detect_coding_ccl (src
, src_end
, multibytep
);
4249 else if (priorities
[i
] & CODING_CATEGORY_MASK_RAW_TEXT
)
4250 mask
|= CODING_CATEGORY_MASK_RAW_TEXT
;
4251 else if (priorities
[i
] & CODING_CATEGORY_MASK_BINARY
)
4252 mask
|= CODING_CATEGORY_MASK_BINARY
;
4253 if (mask
& priorities
[i
])
4254 return priorities
[i
];
4256 return CODING_CATEGORY_MASK_RAW_TEXT
;
4258 if (try & CODING_CATEGORY_MASK_ISO
)
4259 mask
|= detect_coding_iso2022 (src
, src_end
, multibytep
);
4260 if (try & CODING_CATEGORY_MASK_SJIS
)
4261 mask
|= detect_coding_sjis (src
, src_end
, multibytep
);
4262 if (try & CODING_CATEGORY_MASK_BIG5
)
4263 mask
|= detect_coding_big5 (src
, src_end
, multibytep
);
4264 if (try & CODING_CATEGORY_MASK_UTF_8
)
4265 mask
|= detect_coding_utf_8 (src
, src_end
, multibytep
);
4266 if (try & CODING_CATEGORY_MASK_UTF_16_BE_LE
)
4267 mask
|= detect_coding_utf_16 (src
, src_end
, multibytep
);
4268 if (try & CODING_CATEGORY_MASK_EMACS_MULE
)
4269 mask
|= detect_coding_emacs_mule (src
, src_end
, multibytep
);
4270 if (try & CODING_CATEGORY_MASK_CCL
)
4271 mask
|= detect_coding_ccl (src
, src_end
, multibytep
);
4273 return (mask
| CODING_CATEGORY_MASK_RAW_TEXT
| CODING_CATEGORY_MASK_BINARY
);
4276 /* Detect how a text of length SRC_BYTES pointed by SRC is encoded.
4277 The information of the detected coding system is set in CODING. */
4280 detect_coding (coding
, src
, src_bytes
)
4281 struct coding_system
*coding
;
4282 const unsigned char *src
;
4289 val
= Vcoding_category_list
;
4290 mask
= detect_coding_mask (src
, src_bytes
, coding_priorities
, &skip
,
4291 coding
->src_multibyte
);
4292 coding
->heading_ascii
= skip
;
4296 /* We found a single coding system of the highest priority in MASK. */
4298 while (mask
&& ! (mask
& 1)) mask
>>= 1, idx
++;
4300 idx
= CODING_CATEGORY_IDX_RAW_TEXT
;
4302 val
= SYMBOL_VALUE (XVECTOR (Vcoding_category_table
)->contents
[idx
]);
4304 if (coding
->eol_type
!= CODING_EOL_UNDECIDED
)
4308 tmp
= Fget (val
, Qeol_type
);
4310 val
= XVECTOR (tmp
)->contents
[coding
->eol_type
];
4313 /* Setup this new coding system while preserving some slots. */
4315 int src_multibyte
= coding
->src_multibyte
;
4316 int dst_multibyte
= coding
->dst_multibyte
;
4318 setup_coding_system (val
, coding
);
4319 coding
->src_multibyte
= src_multibyte
;
4320 coding
->dst_multibyte
= dst_multibyte
;
4321 coding
->heading_ascii
= skip
;
4325 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
4326 SOURCE is encoded. Return one of CODING_EOL_LF, CODING_EOL_CRLF,
4327 CODING_EOL_CR, and CODING_EOL_UNDECIDED.
4329 How many non-eol characters are at the head is returned as *SKIP. */
4331 #define MAX_EOL_CHECK_COUNT 3
4334 detect_eol_type (source
, src_bytes
, skip
)
4335 unsigned char *source
;
4336 int src_bytes
, *skip
;
4338 unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
4340 int total
= 0; /* How many end-of-lines are found so far. */
4341 int eol_type
= CODING_EOL_UNDECIDED
;
4346 while (src
< src_end
&& total
< MAX_EOL_CHECK_COUNT
)
4349 if (c
== '\n' || c
== '\r')
4352 *skip
= src
- 1 - source
;
4355 this_eol_type
= CODING_EOL_LF
;
4356 else if (src
>= src_end
|| *src
!= '\n')
4357 this_eol_type
= CODING_EOL_CR
;
4359 this_eol_type
= CODING_EOL_CRLF
, src
++;
4361 if (eol_type
== CODING_EOL_UNDECIDED
)
4362 /* This is the first end-of-line. */
4363 eol_type
= this_eol_type
;
4364 else if (eol_type
!= this_eol_type
)
4366 /* The found type is different from what found before. */
4367 eol_type
= CODING_EOL_INCONSISTENT
;
4374 *skip
= src_end
- source
;
4378 /* Like detect_eol_type, but detect EOL type in 2-octet
4379 big-endian/little-endian format for coding systems utf-16-be and
4383 detect_eol_type_in_2_octet_form (source
, src_bytes
, skip
, big_endian_p
)
4384 unsigned char *source
;
4385 int src_bytes
, *skip
, big_endian_p
;
4387 unsigned char *src
= source
, *src_end
= src
+ src_bytes
;
4388 unsigned int c1
, c2
;
4389 int total
= 0; /* How many end-of-lines are found so far. */
4390 int eol_type
= CODING_EOL_UNDECIDED
;
4401 while ((src
+ 1) < src_end
&& total
< MAX_EOL_CHECK_COUNT
)
4403 c1
= (src
[msb
] << 8) | (src
[lsb
]);
4406 if (c1
== '\n' || c1
== '\r')
4409 *skip
= src
- 2 - source
;
4413 this_eol_type
= CODING_EOL_LF
;
4417 if ((src
+ 1) >= src_end
)
4419 this_eol_type
= CODING_EOL_CR
;
4423 c2
= (src
[msb
] << 8) | (src
[lsb
]);
4425 this_eol_type
= CODING_EOL_CRLF
, src
+= 2;
4427 this_eol_type
= CODING_EOL_CR
;
4431 if (eol_type
== CODING_EOL_UNDECIDED
)
4432 /* This is the first end-of-line. */
4433 eol_type
= this_eol_type
;
4434 else if (eol_type
!= this_eol_type
)
4436 /* The found type is different from what found before. */
4437 eol_type
= CODING_EOL_INCONSISTENT
;
4444 *skip
= src_end
- source
;
4448 /* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC
4449 is encoded. If it detects an appropriate format of end-of-line, it
4450 sets the information in *CODING. */
4453 detect_eol (coding
, src
, src_bytes
)
4454 struct coding_system
*coding
;
4455 const unsigned char *src
;
4462 switch (coding
->category_idx
)
4464 case CODING_CATEGORY_IDX_UTF_16_BE
:
4465 eol_type
= detect_eol_type_in_2_octet_form (src
, src_bytes
, &skip
, 1);
4467 case CODING_CATEGORY_IDX_UTF_16_LE
:
4468 eol_type
= detect_eol_type_in_2_octet_form (src
, src_bytes
, &skip
, 0);
4471 eol_type
= detect_eol_type (src
, src_bytes
, &skip
);
4475 if (coding
->heading_ascii
> skip
)
4476 coding
->heading_ascii
= skip
;
4478 skip
= coding
->heading_ascii
;
4480 if (eol_type
== CODING_EOL_UNDECIDED
)
4482 if (eol_type
== CODING_EOL_INCONSISTENT
)
4485 /* This code is suppressed until we find a better way to
4486 distinguish raw text file and binary file. */
4488 /* If we have already detected that the coding is raw-text, the
4489 coding should actually be no-conversion. */
4490 if (coding
->type
== coding_type_raw_text
)
4492 setup_coding_system (Qno_conversion
, coding
);
4495 /* Else, let's decode only text code anyway. */
4497 eol_type
= CODING_EOL_LF
;
4500 val
= Fget (coding
->symbol
, Qeol_type
);
4501 if (VECTORP (val
) && XVECTOR (val
)->size
== 3)
4503 int src_multibyte
= coding
->src_multibyte
;
4504 int dst_multibyte
= coding
->dst_multibyte
;
4505 struct composition_data
*cmp_data
= coding
->cmp_data
;
4507 setup_coding_system (XVECTOR (val
)->contents
[eol_type
], coding
);
4508 coding
->src_multibyte
= src_multibyte
;
4509 coding
->dst_multibyte
= dst_multibyte
;
4510 coding
->heading_ascii
= skip
;
4511 coding
->cmp_data
= cmp_data
;
4515 #define CONVERSION_BUFFER_EXTRA_ROOM 256
4517 #define DECODING_BUFFER_MAG(coding) \
4518 (coding->type == coding_type_iso2022 \
4520 : (coding->type == coding_type_ccl \
4521 ? coding->spec.ccl.decoder.buf_magnification \
4524 /* Return maximum size (bytes) of a buffer enough for decoding
4525 SRC_BYTES of text encoded in CODING. */
4528 decoding_buffer_size (coding
, src_bytes
)
4529 struct coding_system
*coding
;
4532 return (src_bytes
* DECODING_BUFFER_MAG (coding
)
4533 + CONVERSION_BUFFER_EXTRA_ROOM
);
4536 /* Return maximum size (bytes) of a buffer enough for encoding
4537 SRC_BYTES of text to CODING. */
4540 encoding_buffer_size (coding
, src_bytes
)
4541 struct coding_system
*coding
;
4546 if (coding
->type
== coding_type_ccl
)
4548 magnification
= coding
->spec
.ccl
.encoder
.buf_magnification
;
4549 if (coding
->eol_type
== CODING_EOL_CRLF
)
4552 else if (CODING_REQUIRE_ENCODING (coding
))
4557 return (src_bytes
* magnification
+ CONVERSION_BUFFER_EXTRA_ROOM
);
4560 /* Working buffer for code conversion. */
4561 struct conversion_buffer
4563 int size
; /* size of data. */
4564 int on_stack
; /* 1 if allocated by alloca. */
4565 unsigned char *data
;
4568 /* Allocate LEN bytes of memory for BUF (struct conversion_buffer). */
4569 #define allocate_conversion_buffer(buf, len) \
4571 if (len < MAX_ALLOCA) \
4573 buf.data = (unsigned char *) alloca (len); \
4578 buf.data = (unsigned char *) xmalloc (len); \
4584 /* Double the allocated memory for *BUF. */
4586 extend_conversion_buffer (buf
)
4587 struct conversion_buffer
*buf
;
4591 unsigned char *save
= buf
->data
;
4592 buf
->data
= (unsigned char *) xmalloc (buf
->size
* 2);
4593 bcopy (save
, buf
->data
, buf
->size
);
4598 buf
->data
= (unsigned char *) xrealloc (buf
->data
, buf
->size
* 2);
4603 /* Free the allocated memory for BUF if it is not on stack. */
4605 free_conversion_buffer (buf
)
4606 struct conversion_buffer
*buf
;
4613 ccl_coding_driver (coding
, source
, destination
, src_bytes
, dst_bytes
, encodep
)
4614 struct coding_system
*coding
;
4615 unsigned char *source
, *destination
;
4616 int src_bytes
, dst_bytes
, encodep
;
4618 struct ccl_program
*ccl
4619 = encodep
? &coding
->spec
.ccl
.encoder
: &coding
->spec
.ccl
.decoder
;
4620 unsigned char *dst
= destination
;
4622 ccl
->suppress_error
= coding
->suppress_error
;
4623 ccl
->last_block
= coding
->mode
& CODING_MODE_LAST_BLOCK
;
4626 /* On encoding, EOL format is converted within ccl_driver. For
4627 that, setup proper information in the structure CCL. */
4628 ccl
->eol_type
= coding
->eol_type
;
4629 if (ccl
->eol_type
==CODING_EOL_UNDECIDED
)
4630 ccl
->eol_type
= CODING_EOL_LF
;
4631 ccl
->cr_consumed
= coding
->spec
.ccl
.cr_carryover
;
4632 ccl
->eight_bit_control
= coding
->dst_multibyte
;
4635 ccl
->eight_bit_control
= 1;
4636 ccl
->multibyte
= coding
->src_multibyte
;
4637 if (coding
->spec
.ccl
.eight_bit_carryover
[0] != 0)
4639 /* Move carryover bytes to DESTINATION. */
4640 unsigned char *p
= coding
->spec
.ccl
.eight_bit_carryover
;
4643 coding
->spec
.ccl
.eight_bit_carryover
[0] = 0;
4645 dst_bytes
-= dst
- destination
;
4648 coding
->produced
= (ccl_driver (ccl
, source
, dst
, src_bytes
, dst_bytes
,
4649 &(coding
->consumed
))
4650 + dst
- destination
);
4654 coding
->produced_char
= coding
->produced
;
4655 coding
->spec
.ccl
.cr_carryover
= ccl
->cr_consumed
;
4657 else if (!ccl
->eight_bit_control
)
4659 /* The produced bytes forms a valid multibyte sequence. */
4660 coding
->produced_char
4661 = multibyte_chars_in_text (destination
, coding
->produced
);
4662 coding
->spec
.ccl
.eight_bit_carryover
[0] = 0;
4666 /* On decoding, the destination should always multibyte. But,
4667 CCL program might have been generated an invalid multibyte
4668 sequence. Here we make such a sequence valid as
4671 = dst_bytes
? dst_bytes
: source
+ coding
->consumed
- destination
;
4673 if ((coding
->consumed
< src_bytes
4674 || !ccl
->last_block
)
4675 && coding
->produced
>= 1
4676 && destination
[coding
->produced
- 1] >= 0x80)
4678 /* We should not convert the tailing 8-bit codes to
4679 multibyte form even if they doesn't form a valid
4680 multibyte sequence. They may form a valid sequence in
4684 if (destination
[coding
->produced
- 1] < 0xA0)
4686 else if (coding
->produced
>= 2)
4688 if (destination
[coding
->produced
- 2] >= 0x80)
4690 if (destination
[coding
->produced
- 2] < 0xA0)
4692 else if (coding
->produced
>= 3
4693 && destination
[coding
->produced
- 3] >= 0x80
4694 && destination
[coding
->produced
- 3] < 0xA0)
4700 BCOPY_SHORT (destination
+ coding
->produced
- carryover
,
4701 coding
->spec
.ccl
.eight_bit_carryover
,
4703 coding
->spec
.ccl
.eight_bit_carryover
[carryover
] = 0;
4704 coding
->produced
-= carryover
;
4707 coding
->produced
= str_as_multibyte (destination
, bytes
,
4709 &(coding
->produced_char
));
4712 switch (ccl
->status
)
4714 case CCL_STAT_SUSPEND_BY_SRC
:
4715 coding
->result
= CODING_FINISH_INSUFFICIENT_SRC
;
4717 case CCL_STAT_SUSPEND_BY_DST
:
4718 coding
->result
= CODING_FINISH_INSUFFICIENT_DST
;
4721 case CCL_STAT_INVALID_CMD
:
4722 coding
->result
= CODING_FINISH_INTERRUPT
;
4725 coding
->result
= CODING_FINISH_NORMAL
;
4728 return coding
->result
;
4731 /* Decode EOL format of the text at PTR of BYTES length destructively
4732 according to CODING->eol_type. This is called after the CCL
4733 program produced a decoded text at PTR. If we do CRLF->LF
4734 conversion, update CODING->produced and CODING->produced_char. */
4737 decode_eol_post_ccl (coding
, ptr
, bytes
)
4738 struct coding_system
*coding
;
4742 Lisp_Object val
, saved_coding_symbol
;
4743 unsigned char *pend
= ptr
+ bytes
;
4746 /* Remember the current coding system symbol. We set it back when
4747 an inconsistent EOL is found so that `last-coding-system-used' is
4748 set to the coding system that doesn't specify EOL conversion. */
4749 saved_coding_symbol
= coding
->symbol
;
4751 coding
->spec
.ccl
.cr_carryover
= 0;
4752 if (coding
->eol_type
== CODING_EOL_UNDECIDED
)
4754 /* Here, to avoid the call of setup_coding_system, we directly
4755 call detect_eol_type. */
4756 coding
->eol_type
= detect_eol_type (ptr
, bytes
, &dummy
);
4757 if (coding
->eol_type
== CODING_EOL_INCONSISTENT
)
4758 coding
->eol_type
= CODING_EOL_LF
;
4759 if (coding
->eol_type
!= CODING_EOL_UNDECIDED
)
4761 val
= Fget (coding
->symbol
, Qeol_type
);
4762 if (VECTORP (val
) && XVECTOR (val
)->size
== 3)
4763 coding
->symbol
= XVECTOR (val
)->contents
[coding
->eol_type
];
4765 coding
->mode
|= CODING_MODE_INHIBIT_INCONSISTENT_EOL
;
4768 if (coding
->eol_type
== CODING_EOL_LF
4769 || coding
->eol_type
== CODING_EOL_UNDECIDED
)
4771 /* We have nothing to do. */
4774 else if (coding
->eol_type
== CODING_EOL_CRLF
)
4776 unsigned char *pstart
= ptr
, *p
= ptr
;
4778 if (! (coding
->mode
& CODING_MODE_LAST_BLOCK
)
4779 && *(pend
- 1) == '\r')
4781 /* If the last character is CR, we can't handle it here
4782 because LF will be in the not-yet-decoded source text.
4783 Record that the CR is not yet processed. */
4784 coding
->spec
.ccl
.cr_carryover
= 1;
4786 coding
->produced_char
--;
4793 if (ptr
+ 1 < pend
&& *(ptr
+ 1) == '\n')
4800 if (coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
)
4801 goto undo_eol_conversion
;
4805 else if (*ptr
== '\n'
4806 && coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
)
4807 goto undo_eol_conversion
;
4812 undo_eol_conversion
:
4813 /* We have faced with inconsistent EOL format at PTR.
4814 Convert all LFs before PTR back to CRLFs. */
4815 for (p
--, ptr
--; p
>= pstart
; p
--)
4818 *ptr
-- = '\n', *ptr
-- = '\r';
4822 /* If carryover is recorded, cancel it because we don't
4823 convert CRLF anymore. */
4824 if (coding
->spec
.ccl
.cr_carryover
)
4826 coding
->spec
.ccl
.cr_carryover
= 0;
4828 coding
->produced_char
++;
4832 coding
->eol_type
= CODING_EOL_LF
;
4833 coding
->symbol
= saved_coding_symbol
;
4837 /* As each two-byte sequence CRLF was converted to LF, (PEND
4838 - P) is the number of deleted characters. */
4839 coding
->produced
-= pend
- p
;
4840 coding
->produced_char
-= pend
- p
;
4843 else /* i.e. coding->eol_type == CODING_EOL_CR */
4845 unsigned char *p
= ptr
;
4847 for (; ptr
< pend
; ptr
++)
4851 else if (*ptr
== '\n'
4852 && coding
->mode
& CODING_MODE_INHIBIT_INCONSISTENT_EOL
)
4854 for (; p
< ptr
; p
++)
4860 coding
->eol_type
= CODING_EOL_LF
;
4861 coding
->symbol
= saved_coding_symbol
;
4867 /* See "GENERAL NOTES about `decode_coding_XXX ()' functions". Before
4868 decoding, it may detect coding system and format of end-of-line if
4869 those are not yet decided. The source should be unibyte, the
4870 result is multibyte if CODING->dst_multibyte is nonzero, else
4874 decode_coding (coding
, source
, destination
, src_bytes
, dst_bytes
)
4875 struct coding_system
*coding
;
4876 const unsigned char *source
;
4877 unsigned char *destination
;
4878 int src_bytes
, dst_bytes
;
4882 if (coding
->type
== coding_type_undecided
)
4883 detect_coding (coding
, source
, src_bytes
);
4885 if (coding
->eol_type
== CODING_EOL_UNDECIDED
4886 && coding
->type
!= coding_type_ccl
)
4888 detect_eol (coding
, source
, src_bytes
);
4889 /* We had better recover the original eol format if we
4890 encounter an inconsistent eol format while decoding. */
4891 coding
->mode
|= CODING_MODE_INHIBIT_INCONSISTENT_EOL
;
4894 coding
->produced
= coding
->produced_char
= 0;
4895 coding
->consumed
= coding
->consumed_char
= 0;
4897 coding
->result
= CODING_FINISH_NORMAL
;
4899 switch (coding
->type
)
4901 case coding_type_sjis
:
4902 decode_coding_sjis_big5 (coding
, source
, destination
,
4903 src_bytes
, dst_bytes
, 1);
4906 case coding_type_iso2022
:
4907 decode_coding_iso2022 (coding
, source
, destination
,
4908 src_bytes
, dst_bytes
);
4911 case coding_type_big5
:
4912 decode_coding_sjis_big5 (coding
, source
, destination
,
4913 src_bytes
, dst_bytes
, 0);
4916 case coding_type_emacs_mule
:
4917 decode_coding_emacs_mule (coding
, source
, destination
,
4918 src_bytes
, dst_bytes
);
4921 case coding_type_ccl
:
4922 if (coding
->spec
.ccl
.cr_carryover
)
4924 /* Put the CR which was not processed by the previous call
4925 of decode_eol_post_ccl in DESTINATION. It will be
4926 decoded together with the following LF by the call to
4927 decode_eol_post_ccl below. */
4928 *destination
= '\r';
4930 coding
->produced_char
++;
4932 extra
= coding
->spec
.ccl
.cr_carryover
;
4934 ccl_coding_driver (coding
, source
, destination
+ extra
,
4935 src_bytes
, dst_bytes
, 0);
4936 if (coding
->eol_type
!= CODING_EOL_LF
)
4938 coding
->produced
+= extra
;
4939 coding
->produced_char
+= extra
;
4940 decode_eol_post_ccl (coding
, destination
, coding
->produced
);
4945 decode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
);
4948 if (coding
->result
== CODING_FINISH_INSUFFICIENT_SRC
4949 && coding
->mode
& CODING_MODE_LAST_BLOCK
4950 && coding
->consumed
== src_bytes
)
4951 coding
->result
= CODING_FINISH_NORMAL
;
4953 if (coding
->mode
& CODING_MODE_LAST_BLOCK
4954 && coding
->result
== CODING_FINISH_INSUFFICIENT_SRC
)
4956 const unsigned char *src
= source
+ coding
->consumed
;
4957 unsigned char *dst
= destination
+ coding
->produced
;
4959 src_bytes
-= coding
->consumed
;
4961 if (COMPOSING_P (coding
))
4962 DECODE_COMPOSITION_END ('1');
4966 dst
+= CHAR_STRING (c
, dst
);
4967 coding
->produced_char
++;
4969 coding
->consumed
= coding
->consumed_char
= src
- source
;
4970 coding
->produced
= dst
- destination
;
4971 coding
->result
= CODING_FINISH_NORMAL
;
4974 if (!coding
->dst_multibyte
)
4976 coding
->produced
= str_as_unibyte (destination
, coding
->produced
);
4977 coding
->produced_char
= coding
->produced
;
4980 return coding
->result
;
4983 /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". The
4984 multibyteness of the source is CODING->src_multibyte, the
4985 multibyteness of the result is always unibyte. */
4988 encode_coding (coding
, source
, destination
, src_bytes
, dst_bytes
)
4989 struct coding_system
*coding
;
4990 const unsigned char *source
;
4991 unsigned char *destination
;
4992 int src_bytes
, dst_bytes
;
4994 coding
->produced
= coding
->produced_char
= 0;
4995 coding
->consumed
= coding
->consumed_char
= 0;
4997 coding
->result
= CODING_FINISH_NORMAL
;
4999 switch (coding
->type
)
5001 case coding_type_sjis
:
5002 encode_coding_sjis_big5 (coding
, source
, destination
,
5003 src_bytes
, dst_bytes
, 1);
5006 case coding_type_iso2022
:
5007 encode_coding_iso2022 (coding
, source
, destination
,
5008 src_bytes
, dst_bytes
);
5011 case coding_type_big5
:
5012 encode_coding_sjis_big5 (coding
, source
, destination
,
5013 src_bytes
, dst_bytes
, 0);
5016 case coding_type_emacs_mule
:
5017 encode_coding_emacs_mule (coding
, source
, destination
,
5018 src_bytes
, dst_bytes
);
5021 case coding_type_ccl
:
5022 ccl_coding_driver (coding
, source
, destination
,
5023 src_bytes
, dst_bytes
, 1);
5027 encode_eol (coding
, source
, destination
, src_bytes
, dst_bytes
);
5030 if (coding
->mode
& CODING_MODE_LAST_BLOCK
5031 && coding
->result
== CODING_FINISH_INSUFFICIENT_SRC
)
5033 const unsigned char *src
= source
+ coding
->consumed
;
5034 unsigned char *dst
= destination
+ coding
->produced
;
5036 if (coding
->type
== coding_type_iso2022
)
5037 ENCODE_RESET_PLANE_AND_REGISTER
;
5038 if (COMPOSING_P (coding
))
5039 *dst
++ = ISO_CODE_ESC
, *dst
++ = '1';
5040 if (coding
->consumed
< src_bytes
)
5042 int len
= src_bytes
- coding
->consumed
;
5044 BCOPY_SHORT (src
, dst
, len
);
5045 if (coding
->src_multibyte
)
5046 len
= str_as_unibyte (dst
, len
);
5048 coding
->consumed
= src_bytes
;
5050 coding
->produced
= coding
->produced_char
= dst
- destination
;
5051 coding
->result
= CODING_FINISH_NORMAL
;
5054 if (coding
->result
== CODING_FINISH_INSUFFICIENT_SRC
5055 && coding
->consumed
== src_bytes
)
5056 coding
->result
= CODING_FINISH_NORMAL
;
5058 return coding
->result
;
5061 /* Scan text in the region between *BEG and *END (byte positions),
5062 skip characters which we don't have to decode by coding system
5063 CODING at the head and tail, then set *BEG and *END to the region
5064 of the text we actually have to convert. The caller should move
5065 the gap out of the region in advance if the region is from a
5068 If STR is not NULL, *BEG and *END are indices into STR. */
5071 shrink_decoding_region (beg
, end
, coding
, str
)
5073 struct coding_system
*coding
;
5076 unsigned char *begp_orig
, *begp
, *endp_orig
, *endp
, c
;
5078 Lisp_Object translation_table
;
5080 if (coding
->type
== coding_type_ccl
5081 || coding
->type
== coding_type_undecided
5082 || coding
->eol_type
!= CODING_EOL_LF
5083 || !NILP (coding
->post_read_conversion
)
5084 || coding
->composing
!= COMPOSITION_DISABLED
)
5086 /* We can't skip any data. */
5089 if (coding
->type
== coding_type_no_conversion
5090 || coding
->type
== coding_type_raw_text
5091 || coding
->type
== coding_type_emacs_mule
)
5093 /* We need no conversion, but don't have to skip any data here.
5094 Decoding routine handles them effectively anyway. */
5098 translation_table
= coding
->translation_table_for_decode
;
5099 if (NILP (translation_table
) && !NILP (Venable_character_translation
))
5100 translation_table
= Vstandard_translation_table_for_decode
;
5101 if (CHAR_TABLE_P (translation_table
))
5104 for (i
= 0; i
< 128; i
++)
5105 if (!NILP (CHAR_TABLE_REF (translation_table
, i
)))
5108 /* Some ASCII character should be translated. We give up
5113 if (coding
->heading_ascii
>= 0)
5114 /* Detection routine has already found how much we can skip at the
5116 *beg
+= coding
->heading_ascii
;
5120 begp_orig
= begp
= str
+ *beg
;
5121 endp_orig
= endp
= str
+ *end
;
5125 begp_orig
= begp
= BYTE_POS_ADDR (*beg
);
5126 endp_orig
= endp
= begp
+ *end
- *beg
;
5129 eol_conversion
= (coding
->eol_type
== CODING_EOL_CR
5130 || coding
->eol_type
== CODING_EOL_CRLF
);
5132 switch (coding
->type
)
5134 case coding_type_sjis
:
5135 case coding_type_big5
:
5136 /* We can skip all ASCII characters at the head. */
5137 if (coding
->heading_ascii
< 0)
5140 while (begp
< endp
&& *begp
< 0x80 && *begp
!= '\r') begp
++;
5142 while (begp
< endp
&& *begp
< 0x80) begp
++;
5144 /* We can skip all ASCII characters at the tail except for the
5145 second byte of SJIS or BIG5 code. */
5147 while (begp
< endp
&& endp
[-1] < 0x80 && endp
[-1] != '\r') endp
--;
5149 while (begp
< endp
&& endp
[-1] < 0x80) endp
--;
5150 /* Do not consider LF as ascii if preceded by CR, since that
5151 confuses eol decoding. */
5152 if (begp
< endp
&& endp
< endp_orig
&& endp
[-1] == '\r' && endp
[0] == '\n')
5154 if (begp
< endp
&& endp
< endp_orig
&& endp
[-1] >= 0x80)
5158 case coding_type_iso2022
:
5159 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, 0) != CHARSET_ASCII
)
5160 /* We can't skip any data. */
5162 if (coding
->heading_ascii
< 0)
5164 /* We can skip all ASCII characters at the head except for a
5165 few control codes. */
5166 while (begp
< endp
&& (c
= *begp
) < 0x80
5167 && c
!= ISO_CODE_CR
&& c
!= ISO_CODE_SO
5168 && c
!= ISO_CODE_SI
&& c
!= ISO_CODE_ESC
5169 && (!eol_conversion
|| c
!= ISO_CODE_LF
))
5172 switch (coding
->category_idx
)
5174 case CODING_CATEGORY_IDX_ISO_8_1
:
5175 case CODING_CATEGORY_IDX_ISO_8_2
:
5176 /* We can skip all ASCII characters at the tail. */
5178 while (begp
< endp
&& (c
= endp
[-1]) < 0x80 && c
!= '\r') endp
--;
5180 while (begp
< endp
&& endp
[-1] < 0x80) endp
--;
5181 /* Do not consider LF as ascii if preceded by CR, since that
5182 confuses eol decoding. */
5183 if (begp
< endp
&& endp
< endp_orig
&& endp
[-1] == '\r' && endp
[0] == '\n')
5187 case CODING_CATEGORY_IDX_ISO_7
:
5188 case CODING_CATEGORY_IDX_ISO_7_TIGHT
:
5190 /* We can skip all characters at the tail except for 8-bit
5191 codes and ESC and the following 2-byte at the tail. */
5192 unsigned char *eight_bit
= NULL
;
5196 && (c
= endp
[-1]) != ISO_CODE_ESC
&& c
!= '\r')
5198 if (!eight_bit
&& c
& 0x80) eight_bit
= endp
;
5203 && (c
= endp
[-1]) != ISO_CODE_ESC
)
5205 if (!eight_bit
&& c
& 0x80) eight_bit
= endp
;
5208 /* Do not consider LF as ascii if preceded by CR, since that
5209 confuses eol decoding. */
5210 if (begp
< endp
&& endp
< endp_orig
5211 && endp
[-1] == '\r' && endp
[0] == '\n')
5213 if (begp
< endp
&& endp
[-1] == ISO_CODE_ESC
)
5215 if (endp
+ 1 < endp_orig
&& end
[0] == '(' && end
[1] == 'B')
5216 /* This is an ASCII designation sequence. We can
5217 surely skip the tail. But, if we have
5218 encountered an 8-bit code, skip only the codes
5220 endp
= eight_bit
? eight_bit
: endp
+ 2;
5222 /* Hmmm, we can't skip the tail. */
5234 *beg
+= begp
- begp_orig
;
5235 *end
+= endp
- endp_orig
;
5239 /* Like shrink_decoding_region but for encoding. */
5242 shrink_encoding_region (beg
, end
, coding
, str
)
5244 struct coding_system
*coding
;
5247 unsigned char *begp_orig
, *begp
, *endp_orig
, *endp
;
5249 Lisp_Object translation_table
;
5251 if (coding
->type
== coding_type_ccl
5252 || coding
->eol_type
== CODING_EOL_CRLF
5253 || coding
->eol_type
== CODING_EOL_CR
5254 || (coding
->cmp_data
&& coding
->cmp_data
->used
> 0))
5256 /* We can't skip any data. */
5259 if (coding
->type
== coding_type_no_conversion
5260 || coding
->type
== coding_type_raw_text
5261 || coding
->type
== coding_type_emacs_mule
5262 || coding
->type
== coding_type_undecided
)
5264 /* We need no conversion, but don't have to skip any data here.
5265 Encoding routine handles them effectively anyway. */
5269 translation_table
= coding
->translation_table_for_encode
;
5270 if (NILP (translation_table
) && !NILP (Venable_character_translation
))
5271 translation_table
= Vstandard_translation_table_for_encode
;
5272 if (CHAR_TABLE_P (translation_table
))
5275 for (i
= 0; i
< 128; i
++)
5276 if (!NILP (CHAR_TABLE_REF (translation_table
, i
)))
5279 /* Some ASCII character should be translated. We give up
5286 begp_orig
= begp
= str
+ *beg
;
5287 endp_orig
= endp
= str
+ *end
;
5291 begp_orig
= begp
= BYTE_POS_ADDR (*beg
);
5292 endp_orig
= endp
= begp
+ *end
- *beg
;
5295 eol_conversion
= (coding
->eol_type
== CODING_EOL_CR
5296 || coding
->eol_type
== CODING_EOL_CRLF
);
5298 /* Here, we don't have to check coding->pre_write_conversion because
5299 the caller is expected to have handled it already. */
5300 switch (coding
->type
)
5302 case coding_type_iso2022
:
5303 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding
, 0) != CHARSET_ASCII
)
5304 /* We can't skip any data. */
5306 if (coding
->flags
& CODING_FLAG_ISO_DESIGNATE_AT_BOL
)
5308 unsigned char *bol
= begp
;
5309 while (begp
< endp
&& *begp
< 0x80)
5312 if (begp
[-1] == '\n')
5316 goto label_skip_tail
;
5320 case coding_type_sjis
:
5321 case coding_type_big5
:
5322 /* We can skip all ASCII characters at the head and tail. */
5324 while (begp
< endp
&& *begp
< 0x80 && *begp
!= '\n') begp
++;
5326 while (begp
< endp
&& *begp
< 0x80) begp
++;
5329 while (begp
< endp
&& endp
[-1] < 0x80 && endp
[-1] != '\n') endp
--;
5331 while (begp
< endp
&& *(endp
- 1) < 0x80) endp
--;
5338 *beg
+= begp
- begp_orig
;
5339 *end
+= endp
- endp_orig
;
5343 /* As shrinking conversion region requires some overhead, we don't try
5344 shrinking if the length of conversion region is less than this
5346 static int shrink_conversion_region_threshhold
= 1024;
5348 #define SHRINK_CONVERSION_REGION(beg, end, coding, str, encodep) \
5350 if (*(end) - *(beg) > shrink_conversion_region_threshhold) \
5352 if (encodep) shrink_encoding_region (beg, end, coding, str); \
5353 else shrink_decoding_region (beg, end, coding, str); \
5357 /* ARG is (CODING BUFFER ...) where CODING is what to be set in
5358 Vlast_coding_system_used and the remaining elements are buffers to
5361 code_convert_region_unwind (arg
)
5364 struct gcpro gcpro1
;
5367 inhibit_pre_post_conversion
= 0;
5368 Vlast_coding_system_used
= XCAR (arg
);
5369 for (arg
= XCDR (arg
); ! NILP (arg
); arg
= XCDR (arg
))
5370 Fkill_buffer (XCAR (arg
));
5376 /* Store information about all compositions in the range FROM and TO
5377 of OBJ in memory blocks pointed by CODING->cmp_data. OBJ is a
5378 buffer or a string, defaults to the current buffer. */
5381 coding_save_composition (coding
, from
, to
, obj
)
5382 struct coding_system
*coding
;
5389 if (coding
->composing
== COMPOSITION_DISABLED
)
5391 if (!coding
->cmp_data
)
5392 coding_allocate_composition_data (coding
, from
);
5393 if (!find_composition (from
, to
, &start
, &end
, &prop
, obj
)
5397 && (!find_composition (end
, to
, &start
, &end
, &prop
, obj
)
5400 coding
->composing
= COMPOSITION_NO
;
5403 if (COMPOSITION_VALID_P (start
, end
, prop
))
5405 enum composition_method method
= COMPOSITION_METHOD (prop
);
5406 if (coding
->cmp_data
->used
+ COMPOSITION_DATA_MAX_BUNCH_LENGTH
5407 >= COMPOSITION_DATA_SIZE
)
5408 coding_allocate_composition_data (coding
, from
);
5409 /* For relative composition, we remember start and end
5410 positions, for the other compositions, we also remember
5412 CODING_ADD_COMPOSITION_START (coding
, start
- from
, method
);
5413 if (method
!= COMPOSITION_RELATIVE
)
5415 /* We must store a*/
5416 Lisp_Object val
, ch
;
5418 val
= COMPOSITION_COMPONENTS (prop
);
5422 ch
= XCAR (val
), val
= XCDR (val
);
5423 CODING_ADD_COMPOSITION_COMPONENT (coding
, XINT (ch
));
5425 else if (VECTORP (val
) || STRINGP (val
))
5427 int len
= (VECTORP (val
)
5428 ? XVECTOR (val
)->size
: SCHARS (val
));
5430 for (i
= 0; i
< len
; i
++)
5433 ? Faref (val
, make_number (i
))
5434 : XVECTOR (val
)->contents
[i
]);
5435 CODING_ADD_COMPOSITION_COMPONENT (coding
, XINT (ch
));
5438 else /* INTEGERP (val) */
5439 CODING_ADD_COMPOSITION_COMPONENT (coding
, XINT (val
));
5441 CODING_ADD_COMPOSITION_END (coding
, end
- from
);
5446 && find_composition (start
, to
, &start
, &end
, &prop
, obj
)
5449 /* Make coding->cmp_data point to the first memory block. */
5450 while (coding
->cmp_data
->prev
)
5451 coding
->cmp_data
= coding
->cmp_data
->prev
;
5452 coding
->cmp_data_start
= 0;
5455 /* Reflect the saved information about compositions to OBJ.
5456 CODING->cmp_data points to a memory block for the information. OBJ
5457 is a buffer or a string, defaults to the current buffer. */
5460 coding_restore_composition (coding
, obj
)
5461 struct coding_system
*coding
;
5464 struct composition_data
*cmp_data
= coding
->cmp_data
;
5469 while (cmp_data
->prev
)
5470 cmp_data
= cmp_data
->prev
;
5476 for (i
= 0; i
< cmp_data
->used
&& cmp_data
->data
[i
] > 0;
5477 i
+= cmp_data
->data
[i
])
5479 int *data
= cmp_data
->data
+ i
;
5480 enum composition_method method
= (enum composition_method
) data
[3];
5481 Lisp_Object components
;
5483 if (data
[0] < 0 || i
+ data
[0] > cmp_data
->used
)
5484 /* Invalid composition data. */
5487 if (method
== COMPOSITION_RELATIVE
)
5491 int len
= data
[0] - 4, j
;
5492 Lisp_Object args
[MAX_COMPOSITION_COMPONENTS
* 2 - 1];
5494 if (method
== COMPOSITION_WITH_RULE_ALTCHARS
5498 /* Invalid composition data. */
5500 for (j
= 0; j
< len
; j
++)
5501 args
[j
] = make_number (data
[4 + j
]);
5502 components
= (method
== COMPOSITION_WITH_ALTCHARS
5503 ? Fstring (len
, args
)
5504 : Fvector (len
, args
));
5506 compose_text (data
[1], data
[2], components
, Qnil
, obj
);
5508 cmp_data
= cmp_data
->next
;
5512 /* Decode (if ENCODEP is zero) or encode (if ENCODEP is nonzero) the
5513 text from FROM to TO (byte positions are FROM_BYTE and TO_BYTE) by
5514 coding system CODING, and return the status code of code conversion
5515 (currently, this value has no meaning).
5517 How many characters (and bytes) are converted to how many
5518 characters (and bytes) are recorded in members of the structure
5521 If REPLACE is nonzero, we do various things as if the original text
5522 is deleted and a new text is inserted. See the comments in
5523 replace_range (insdel.c) to know what we are doing.
5525 If REPLACE is zero, it is assumed that the source text is unibyte.
5526 Otherwise, it is assumed that the source text is multibyte. */
5529 code_convert_region (from
, from_byte
, to
, to_byte
, coding
, encodep
, replace
)
5530 int from
, from_byte
, to
, to_byte
, encodep
, replace
;
5531 struct coding_system
*coding
;
5533 int len
= to
- from
, len_byte
= to_byte
- from_byte
;
5534 int nchars_del
= 0, nbytes_del
= 0;
5535 int require
, inserted
, inserted_byte
;
5536 int head_skip
, tail_skip
, total_skip
= 0;
5537 Lisp_Object saved_coding_symbol
;
5539 unsigned char *src
, *dst
;
5540 Lisp_Object deletion
;
5541 int orig_point
= PT
, orig_len
= len
;
5543 int multibyte_p
= !NILP (current_buffer
->enable_multibyte_characters
);
5546 saved_coding_symbol
= coding
->symbol
;
5548 if (from
< PT
&& PT
< to
)
5550 TEMP_SET_PT_BOTH (from
, from_byte
);
5556 int saved_from
= from
;
5557 int saved_inhibit_modification_hooks
;
5559 prepare_to_modify_buffer (from
, to
, &from
);
5560 if (saved_from
!= from
)
5563 from_byte
= CHAR_TO_BYTE (from
), to_byte
= CHAR_TO_BYTE (to
);
5564 len_byte
= to_byte
- from_byte
;
5567 /* The code conversion routine can not preserve text properties
5568 for now. So, we must remove all text properties in the
5569 region. Here, we must suppress all modification hooks. */
5570 saved_inhibit_modification_hooks
= inhibit_modification_hooks
;
5571 inhibit_modification_hooks
= 1;
5572 Fset_text_properties (make_number (from
), make_number (to
), Qnil
, Qnil
);
5573 inhibit_modification_hooks
= saved_inhibit_modification_hooks
;
5576 if (! encodep
&& CODING_REQUIRE_DETECTION (coding
))
5578 /* We must detect encoding of text and eol format. */
5580 if (from
< GPT
&& to
> GPT
)
5581 move_gap_both (from
, from_byte
);
5582 if (coding
->type
== coding_type_undecided
)
5584 detect_coding (coding
, BYTE_POS_ADDR (from_byte
), len_byte
);
5585 if (coding
->type
== coding_type_undecided
)
5587 /* It seems that the text contains only ASCII, but we
5588 should not leave it undecided because the deeper
5589 decoding routine (decode_coding) tries to detect the
5590 encodings again in vain. */
5591 coding
->type
= coding_type_emacs_mule
;
5592 coding
->category_idx
= CODING_CATEGORY_IDX_EMACS_MULE
;
5593 /* As emacs-mule decoder will handle composition, we
5594 need this setting to allocate coding->cmp_data
5596 coding
->composing
= COMPOSITION_NO
;
5599 if (coding
->eol_type
== CODING_EOL_UNDECIDED
5600 && coding
->type
!= coding_type_ccl
)
5602 detect_eol (coding
, BYTE_POS_ADDR (from_byte
), len_byte
);
5603 if (coding
->eol_type
== CODING_EOL_UNDECIDED
)
5604 coding
->eol_type
= CODING_EOL_LF
;
5605 /* We had better recover the original eol format if we
5606 encounter an inconsistent eol format while decoding. */
5607 coding
->mode
|= CODING_MODE_INHIBIT_INCONSISTENT_EOL
;
5611 /* Now we convert the text. */
5613 /* For encoding, we must process pre-write-conversion in advance. */
5614 if (! inhibit_pre_post_conversion
5616 && SYMBOLP (coding
->pre_write_conversion
)
5617 && ! NILP (Ffboundp (coding
->pre_write_conversion
)))
5619 /* The function in pre-write-conversion may put a new text in a
5621 struct buffer
*prev
= current_buffer
;
5624 record_unwind_protect (code_convert_region_unwind
,
5625 Fcons (Vlast_coding_system_used
, Qnil
));
5626 /* We should not call any more pre-write/post-read-conversion
5627 functions while this pre-write-conversion is running. */
5628 inhibit_pre_post_conversion
= 1;
5629 call2 (coding
->pre_write_conversion
,
5630 make_number (from
), make_number (to
));
5631 inhibit_pre_post_conversion
= 0;
5632 /* Discard the unwind protect. */
5635 if (current_buffer
!= prev
)
5638 new = Fcurrent_buffer ();
5639 set_buffer_internal_1 (prev
);
5640 del_range_2 (from
, from_byte
, to
, to_byte
, 0);
5641 TEMP_SET_PT_BOTH (from
, from_byte
);
5642 insert_from_buffer (XBUFFER (new), 1, len
, 0);
5644 if (orig_point
>= to
)
5645 orig_point
+= len
- orig_len
;
5646 else if (orig_point
> from
)
5650 from_byte
= CHAR_TO_BYTE (from
);
5651 to_byte
= CHAR_TO_BYTE (to
);
5652 len_byte
= to_byte
- from_byte
;
5653 TEMP_SET_PT_BOTH (from
, from_byte
);
5659 if (! EQ (current_buffer
->undo_list
, Qt
))
5660 deletion
= make_buffer_string_both (from
, from_byte
, to
, to_byte
, 1);
5663 nchars_del
= to
- from
;
5664 nbytes_del
= to_byte
- from_byte
;
5668 if (coding
->composing
!= COMPOSITION_DISABLED
)
5671 coding_save_composition (coding
, from
, to
, Fcurrent_buffer ());
5673 coding_allocate_composition_data (coding
, from
);
5676 /* Try to skip the heading and tailing ASCIIs. We can't skip them
5677 if we must run CCL program or there are compositions to
5679 if (coding
->type
!= coding_type_ccl
5680 && (! coding
->cmp_data
|| coding
->cmp_data
->used
== 0))
5682 int from_byte_orig
= from_byte
, to_byte_orig
= to_byte
;
5684 if (from
< GPT
&& GPT
< to
)
5685 move_gap_both (from
, from_byte
);
5686 SHRINK_CONVERSION_REGION (&from_byte
, &to_byte
, coding
, NULL
, encodep
);
5687 if (from_byte
== to_byte
5688 && (encodep
|| NILP (coding
->post_read_conversion
))
5689 && ! CODING_REQUIRE_FLUSHING (coding
))
5691 coding
->produced
= len_byte
;
5692 coding
->produced_char
= len
;
5694 /* We must record and adjust for this new text now. */
5695 adjust_after_insert (from
, from_byte_orig
, to
, to_byte_orig
, len
);
5696 coding_free_composition_data (coding
);
5700 head_skip
= from_byte
- from_byte_orig
;
5701 tail_skip
= to_byte_orig
- to_byte
;
5702 total_skip
= head_skip
+ tail_skip
;
5705 len
-= total_skip
; len_byte
-= total_skip
;
5708 /* For conversion, we must put the gap before the text in addition to
5709 making the gap larger for efficient decoding. The required gap
5710 size starts from 2000 which is the magic number used in make_gap.
5711 But, after one batch of conversion, it will be incremented if we
5712 find that it is not enough . */
5715 if (GAP_SIZE
< require
)
5716 make_gap (require
- GAP_SIZE
);
5717 move_gap_both (from
, from_byte
);
5719 inserted
= inserted_byte
= 0;
5721 GAP_SIZE
+= len_byte
;
5724 ZV_BYTE
-= len_byte
;
5727 if (GPT
- BEG
< BEG_UNCHANGED
)
5728 BEG_UNCHANGED
= GPT
- BEG
;
5729 if (Z
- GPT
< END_UNCHANGED
)
5730 END_UNCHANGED
= Z
- GPT
;
5732 if (!encodep
&& coding
->src_multibyte
)
5734 /* Decoding routines expects that the source text is unibyte.
5735 We must convert 8-bit characters of multibyte form to
5737 int len_byte_orig
= len_byte
;
5738 len_byte
= str_as_unibyte (GAP_END_ADDR
- len_byte
, len_byte
);
5739 if (len_byte
< len_byte_orig
)
5740 safe_bcopy (GAP_END_ADDR
- len_byte_orig
, GAP_END_ADDR
- len_byte
,
5742 coding
->src_multibyte
= 0;
5749 /* The buffer memory is now:
5750 +--------+converted-text+---------+-------original-text-------+---+
5751 |<-from->|<--inserted-->|---------|<--------len_byte--------->|---|
5752 |<---------------------- GAP ----------------------->| */
5753 src
= GAP_END_ADDR
- len_byte
;
5754 dst
= GPT_ADDR
+ inserted_byte
;
5757 result
= encode_coding (coding
, src
, dst
, len_byte
, 0);
5760 if (coding
->composing
!= COMPOSITION_DISABLED
)
5761 coding
->cmp_data
->char_offset
= from
+ inserted
;
5762 result
= decode_coding (coding
, src
, dst
, len_byte
, 0);
5765 /* The buffer memory is now:
5766 +--------+-------converted-text----+--+------original-text----+---+
5767 |<-from->|<-inserted->|<-produced->|--|<-(len_byte-consumed)->|---|
5768 |<---------------------- GAP ----------------------->| */
5770 inserted
+= coding
->produced_char
;
5771 inserted_byte
+= coding
->produced
;
5772 len_byte
-= coding
->consumed
;
5774 if (result
== CODING_FINISH_INSUFFICIENT_CMP
)
5776 coding_allocate_composition_data (coding
, from
+ inserted
);
5780 src
+= coding
->consumed
;
5781 dst
+= coding
->produced
;
5783 if (result
== CODING_FINISH_NORMAL
)
5788 if (! encodep
&& result
== CODING_FINISH_INCONSISTENT_EOL
)
5790 unsigned char *pend
= dst
, *p
= pend
- inserted_byte
;
5791 Lisp_Object eol_type
;
5793 /* Encode LFs back to the original eol format (CR or CRLF). */
5794 if (coding
->eol_type
== CODING_EOL_CR
)
5796 while (p
< pend
) if (*p
++ == '\n') p
[-1] = '\r';
5802 while (p
< pend
) if (*p
++ == '\n') count
++;
5803 if (src
- dst
< count
)
5805 /* We don't have sufficient room for encoding LFs
5806 back to CRLF. We must record converted and
5807 not-yet-converted text back to the buffer
5808 content, enlarge the gap, then record them out of
5809 the buffer contents again. */
5810 int add
= len_byte
+ inserted_byte
;
5813 ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
5814 GPT
+= inserted_byte
; GPT_BYTE
+= inserted_byte
;
5815 make_gap (count
- GAP_SIZE
);
5817 ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
5818 GPT
-= inserted_byte
; GPT_BYTE
-= inserted_byte
;
5819 /* Don't forget to update SRC, DST, and PEND. */
5820 src
= GAP_END_ADDR
- len_byte
;
5821 dst
= GPT_ADDR
+ inserted_byte
;
5825 inserted_byte
+= count
;
5826 coding
->produced
+= count
;
5827 p
= dst
= pend
+ count
;
5831 if (*p
== '\n') count
--, *--p
= '\r';
5835 /* Suppress eol-format conversion in the further conversion. */
5836 coding
->eol_type
= CODING_EOL_LF
;
5838 /* Set the coding system symbol to that for Unix-like EOL. */
5839 eol_type
= Fget (saved_coding_symbol
, Qeol_type
);
5840 if (VECTORP (eol_type
)
5841 && XVECTOR (eol_type
)->size
== 3
5842 && SYMBOLP (XVECTOR (eol_type
)->contents
[CODING_EOL_LF
]))
5843 coding
->symbol
= XVECTOR (eol_type
)->contents
[CODING_EOL_LF
];
5845 coding
->symbol
= saved_coding_symbol
;
5851 if (coding
->type
!= coding_type_ccl
5852 || coding
->mode
& CODING_MODE_LAST_BLOCK
)
5854 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
5857 if (result
== CODING_FINISH_INSUFFICIENT_SRC
)
5859 /* The source text ends in invalid codes. Let's just
5860 make them valid buffer contents, and finish conversion. */
5863 unsigned char *start
= dst
;
5865 inserted
+= len_byte
;
5869 dst
+= CHAR_STRING (c
, dst
);
5872 inserted_byte
+= dst
- start
;
5876 inserted
+= len_byte
;
5877 inserted_byte
+= len_byte
;
5883 if (result
== CODING_FINISH_INTERRUPT
)
5885 /* The conversion procedure was interrupted by a user. */
5888 /* Now RESULT == CODING_FINISH_INSUFFICIENT_DST */
5889 if (coding
->consumed
< 1)
5891 /* It's quite strange to require more memory without
5892 consuming any bytes. Perhaps CCL program bug. */
5897 /* We have just done the first batch of conversion which was
5898 stopped because of insufficient gap. Let's reconsider the
5899 required gap size (i.e. SRT - DST) now.
5901 We have converted ORIG bytes (== coding->consumed) into
5902 NEW bytes (coding->produced). To convert the remaining
5903 LEN bytes, we may need REQUIRE bytes of gap, where:
5904 REQUIRE + LEN_BYTE = LEN_BYTE * (NEW / ORIG)
5905 REQUIRE = LEN_BYTE * (NEW - ORIG) / ORIG
5906 Here, we are sure that NEW >= ORIG. */
5908 if (coding
->produced
<= coding
->consumed
)
5910 /* This happens because of CCL-based coding system with
5916 float ratio
= coding
->produced
- coding
->consumed
;
5917 ratio
/= coding
->consumed
;
5918 require
= len_byte
* ratio
;
5922 if ((src
- dst
) < (require
+ 2000))
5924 /* See the comment above the previous call of make_gap. */
5925 int add
= len_byte
+ inserted_byte
;
5928 ZV
+= add
; Z
+= add
; ZV_BYTE
+= add
; Z_BYTE
+= add
;
5929 GPT
+= inserted_byte
; GPT_BYTE
+= inserted_byte
;
5930 make_gap (require
+ 2000);
5932 ZV
-= add
; Z
-= add
; ZV_BYTE
-= add
; Z_BYTE
-= add
;
5933 GPT
-= inserted_byte
; GPT_BYTE
-= inserted_byte
;
5936 if (src
- dst
> 0) *dst
= 0; /* Put an anchor. */
5938 if (encodep
&& coding
->dst_multibyte
)
5940 /* The output is unibyte. We must convert 8-bit characters to
5942 if (inserted_byte
* 2 > GAP_SIZE
)
5944 GAP_SIZE
-= inserted_byte
;
5945 ZV
+= inserted_byte
; Z
+= inserted_byte
;
5946 ZV_BYTE
+= inserted_byte
; Z_BYTE
+= inserted_byte
;
5947 GPT
+= inserted_byte
; GPT_BYTE
+= inserted_byte
;
5948 make_gap (inserted_byte
- GAP_SIZE
);
5949 GAP_SIZE
+= inserted_byte
;
5950 ZV
-= inserted_byte
; Z
-= inserted_byte
;
5951 ZV_BYTE
-= inserted_byte
; Z_BYTE
-= inserted_byte
;
5952 GPT
-= inserted_byte
; GPT_BYTE
-= inserted_byte
;
5954 inserted_byte
= str_to_multibyte (GPT_ADDR
, GAP_SIZE
, inserted_byte
);
5957 /* If we shrank the conversion area, adjust it now. */
5961 safe_bcopy (GAP_END_ADDR
, GPT_ADDR
+ inserted_byte
, tail_skip
);
5962 inserted
+= total_skip
; inserted_byte
+= total_skip
;
5963 GAP_SIZE
+= total_skip
;
5964 GPT
-= head_skip
; GPT_BYTE
-= head_skip
;
5965 ZV
-= total_skip
; ZV_BYTE
-= total_skip
;
5966 Z
-= total_skip
; Z_BYTE
-= total_skip
;
5967 from
-= head_skip
; from_byte
-= head_skip
;
5968 to
+= tail_skip
; to_byte
+= tail_skip
;
5972 if (! EQ (current_buffer
->undo_list
, Qt
))
5973 adjust_after_replace (from
, from_byte
, deletion
, inserted
, inserted_byte
);
5975 adjust_after_replace_noundo (from
, from_byte
, nchars_del
, nbytes_del
,
5976 inserted
, inserted_byte
);
5977 inserted
= Z
- prev_Z
;
5979 if (!encodep
&& coding
->cmp_data
&& coding
->cmp_data
->used
)
5980 coding_restore_composition (coding
, Fcurrent_buffer ());
5981 coding_free_composition_data (coding
);
5983 if (! inhibit_pre_post_conversion
5984 && ! encodep
&& ! NILP (coding
->post_read_conversion
))
5987 Lisp_Object saved_coding_system
;
5990 TEMP_SET_PT_BOTH (from
, from_byte
);
5992 record_unwind_protect (code_convert_region_unwind
,
5993 Fcons (Vlast_coding_system_used
, Qnil
));
5994 saved_coding_system
= Vlast_coding_system_used
;
5995 Vlast_coding_system_used
= coding
->symbol
;
5996 /* We should not call any more pre-write/post-read-conversion
5997 functions while this post-read-conversion is running. */
5998 inhibit_pre_post_conversion
= 1;
5999 val
= call1 (coding
->post_read_conversion
, make_number (inserted
));
6000 inhibit_pre_post_conversion
= 0;
6001 coding
->symbol
= Vlast_coding_system_used
;
6002 Vlast_coding_system_used
= saved_coding_system
;
6003 /* Discard the unwind protect. */
6006 inserted
+= Z
- prev_Z
;
6009 if (orig_point
>= from
)
6011 if (orig_point
>= from
+ orig_len
)
6012 orig_point
+= inserted
- orig_len
;
6015 TEMP_SET_PT (orig_point
);
6020 signal_after_change (from
, to
- from
, inserted
);
6021 update_compositions (from
, from
+ inserted
, CHECK_BORDER
);
6025 coding
->consumed
= to_byte
- from_byte
;
6026 coding
->consumed_char
= to
- from
;
6027 coding
->produced
= inserted_byte
;
6028 coding
->produced_char
= inserted
;
6034 /* Name (or base name) of work buffer for code conversion. */
6035 static Lisp_Object Vcode_conversion_workbuf_name
;
6037 /* Set the current buffer to the working buffer prepared for
6038 code-conversion. MULTIBYTE specifies the multibyteness of the
6039 buffer. Return the buffer we set if it must be killed after use.
6040 Otherwise return Qnil. */
6043 set_conversion_work_buffer (multibyte
)
6046 Lisp_Object buffer
, buffer_to_kill
;
6049 buffer
= Fget_buffer_create (Vcode_conversion_workbuf_name
);
6050 buf
= XBUFFER (buffer
);
6051 if (buf
== current_buffer
)
6053 /* As we are already in the work buffer, we must generate a new
6054 buffer for the work. */
6057 name
= Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name
, Qnil
);
6058 buffer
= buffer_to_kill
= Fget_buffer_create (name
);
6059 buf
= XBUFFER (buffer
);
6062 buffer_to_kill
= Qnil
;
6064 delete_all_overlays (buf
);
6065 buf
->directory
= current_buffer
->directory
;
6066 buf
->read_only
= Qnil
;
6067 buf
->filename
= Qnil
;
6068 buf
->undo_list
= Qt
;
6069 eassert (buf
->overlays_before
== NULL
);
6070 eassert (buf
->overlays_after
== NULL
);
6071 set_buffer_internal (buf
);
6072 if (BEG
!= BEGV
|| Z
!= ZV
)
6074 del_range_2 (BEG
, BEG_BYTE
, Z
, Z_BYTE
, 0);
6075 buf
->enable_multibyte_characters
= multibyte
? Qt
: Qnil
;
6076 return buffer_to_kill
;
6080 run_pre_post_conversion_on_str (str
, coding
, encodep
)
6082 struct coding_system
*coding
;
6085 int count
= SPECPDL_INDEX ();
6086 struct gcpro gcpro1
, gcpro2
;
6087 int multibyte
= STRING_MULTIBYTE (str
);
6088 Lisp_Object old_deactivate_mark
;
6089 Lisp_Object buffer_to_kill
;
6090 Lisp_Object unwind_arg
;
6092 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
6093 /* It is not crucial to specbind this. */
6094 old_deactivate_mark
= Vdeactivate_mark
;
6095 GCPRO2 (str
, old_deactivate_mark
);
6097 /* We must insert the contents of STR as is without
6098 unibyte<->multibyte conversion. For that, we adjust the
6099 multibyteness of the working buffer to that of STR. */
6100 buffer_to_kill
= set_conversion_work_buffer (multibyte
);
6101 if (NILP (buffer_to_kill
))
6102 unwind_arg
= Fcons (Vlast_coding_system_used
, Qnil
);
6104 unwind_arg
= list2 (Vlast_coding_system_used
, buffer_to_kill
);
6105 record_unwind_protect (code_convert_region_unwind
, unwind_arg
);
6107 insert_from_string (str
, 0, 0,
6108 SCHARS (str
), SBYTES (str
), 0);
6110 inhibit_pre_post_conversion
= 1;
6113 struct buffer
*prev
= current_buffer
;
6115 call2 (coding
->pre_write_conversion
, make_number (BEG
), make_number (Z
));
6116 if (prev
!= current_buffer
)
6117 /* We must kill the current buffer too. */
6118 Fsetcdr (unwind_arg
, Fcons (Fcurrent_buffer (), XCDR (unwind_arg
)));
6122 Vlast_coding_system_used
= coding
->symbol
;
6123 TEMP_SET_PT_BOTH (BEG
, BEG_BYTE
);
6124 call1 (coding
->post_read_conversion
, make_number (Z
- BEG
));
6125 coding
->symbol
= Vlast_coding_system_used
;
6127 inhibit_pre_post_conversion
= 0;
6128 Vdeactivate_mark
= old_deactivate_mark
;
6129 str
= make_buffer_string (BEG
, Z
, 1);
6130 return unbind_to (count
, str
);
6134 /* Run pre-write-conversion function of CODING on NCHARS/NBYTES
6135 text in *STR. *SIZE is the allocated bytes for STR. As it
6136 is intended that this function is called from encode_terminal_code,
6137 the pre-write-conversion function is run by safe_call and thus
6138 "Error during redisplay: ..." is logged when an error occurs.
6140 Store the resulting text in *STR and set CODING->produced_char and
6141 CODING->produced to the number of characters and bytes
6142 respectively. If the size of *STR is too small, enlarge it by
6143 xrealloc and update *STR and *SIZE. */
6146 run_pre_write_conversin_on_c_str (str
, size
, nchars
, nbytes
, coding
)
6147 unsigned char **str
;
6148 int *size
, nchars
, nbytes
;
6149 struct coding_system
*coding
;
6151 struct gcpro gcpro1
, gcpro2
;
6152 struct buffer
*cur
= current_buffer
;
6153 struct buffer
*prev
;
6154 Lisp_Object old_deactivate_mark
, old_last_coding_system_used
;
6155 Lisp_Object args
[3];
6156 Lisp_Object buffer_to_kill
;
6158 /* It is not crucial to specbind this. */
6159 old_deactivate_mark
= Vdeactivate_mark
;
6160 old_last_coding_system_used
= Vlast_coding_system_used
;
6161 GCPRO2 (old_deactivate_mark
, old_last_coding_system_used
);
6163 /* We must insert the contents of STR as is without
6164 unibyte<->multibyte conversion. For that, we adjust the
6165 multibyteness of the working buffer to that of STR. */
6166 buffer_to_kill
= set_conversion_work_buffer (coding
->src_multibyte
);
6167 insert_1_both (*str
, nchars
, nbytes
, 0, 0, 0);
6169 inhibit_pre_post_conversion
= 1;
6170 prev
= current_buffer
;
6171 args
[0] = coding
->pre_write_conversion
;
6172 args
[1] = make_number (BEG
);
6173 args
[2] = make_number (Z
);
6174 safe_call (3, args
);
6175 inhibit_pre_post_conversion
= 0;
6176 Vdeactivate_mark
= old_deactivate_mark
;
6177 Vlast_coding_system_used
= old_last_coding_system_used
;
6178 coding
->produced_char
= Z
- BEG
;
6179 coding
->produced
= Z_BYTE
- BEG_BYTE
;
6180 if (coding
->produced
> *size
)
6182 *size
= coding
->produced
;
6183 *str
= xrealloc (*str
, *size
);
6185 if (BEG
< GPT
&& GPT
< Z
)
6187 bcopy (BEG_ADDR
, *str
, coding
->produced
);
6188 coding
->src_multibyte
6189 = ! NILP (current_buffer
->enable_multibyte_characters
);
6190 if (prev
!= current_buffer
)
6191 Fkill_buffer (Fcurrent_buffer ());
6192 set_buffer_internal (cur
);
6193 if (! NILP (buffer_to_kill
))
6194 Fkill_buffer (buffer_to_kill
);
6199 decode_coding_string (str
, coding
, nocopy
)
6201 struct coding_system
*coding
;
6205 struct conversion_buffer buf
;
6207 Lisp_Object saved_coding_symbol
;
6209 int require_decoding
;
6210 int shrinked_bytes
= 0;
6212 int consumed
, consumed_char
, produced
, produced_char
;
6215 to_byte
= SBYTES (str
);
6217 saved_coding_symbol
= coding
->symbol
;
6218 coding
->src_multibyte
= STRING_MULTIBYTE (str
);
6219 coding
->dst_multibyte
= 1;
6220 if (CODING_REQUIRE_DETECTION (coding
))
6222 /* See the comments in code_convert_region. */
6223 if (coding
->type
== coding_type_undecided
)
6225 detect_coding (coding
, SDATA (str
), to_byte
);
6226 if (coding
->type
== coding_type_undecided
)
6228 coding
->type
= coding_type_emacs_mule
;
6229 coding
->category_idx
= CODING_CATEGORY_IDX_EMACS_MULE
;
6230 /* As emacs-mule decoder will handle composition, we
6231 need this setting to allocate coding->cmp_data
6233 coding
->composing
= COMPOSITION_NO
;
6236 if (coding
->eol_type
== CODING_EOL_UNDECIDED
6237 && coding
->type
!= coding_type_ccl
)
6239 saved_coding_symbol
= coding
->symbol
;
6240 detect_eol (coding
, SDATA (str
), to_byte
);
6241 if (coding
->eol_type
== CODING_EOL_UNDECIDED
)
6242 coding
->eol_type
= CODING_EOL_LF
;
6243 /* We had better recover the original eol format if we
6244 encounter an inconsistent eol format while decoding. */
6245 coding
->mode
|= CODING_MODE_INHIBIT_INCONSISTENT_EOL
;
6249 if (coding
->type
== coding_type_no_conversion
6250 || coding
->type
== coding_type_raw_text
)
6251 coding
->dst_multibyte
= 0;
6253 require_decoding
= CODING_REQUIRE_DECODING (coding
);
6255 if (STRING_MULTIBYTE (str
))
6257 /* Decoding routines expect the source text to be unibyte. */
6258 str
= Fstring_as_unibyte (str
);
6259 to_byte
= SBYTES (str
);
6261 coding
->src_multibyte
= 0;
6264 /* Try to skip the heading and tailing ASCIIs. */
6265 if (require_decoding
&& coding
->type
!= coding_type_ccl
)
6267 SHRINK_CONVERSION_REGION (&from
, &to_byte
, coding
, SDATA (str
),
6269 if (from
== to_byte
)
6270 require_decoding
= 0;
6271 shrinked_bytes
= from
+ (SBYTES (str
) - to_byte
);
6274 if (!require_decoding
6275 && !(SYMBOLP (coding
->post_read_conversion
)
6276 && !NILP (Ffboundp (coding
->post_read_conversion
))))
6278 coding
->consumed
= SBYTES (str
);
6279 coding
->consumed_char
= SCHARS (str
);
6280 if (coding
->dst_multibyte
)
6282 str
= Fstring_as_multibyte (str
);
6285 coding
->produced
= SBYTES (str
);
6286 coding
->produced_char
= SCHARS (str
);
6287 return (nocopy
? str
: Fcopy_sequence (str
));
6290 if (coding
->composing
!= COMPOSITION_DISABLED
)
6291 coding_allocate_composition_data (coding
, from
);
6292 len
= decoding_buffer_size (coding
, to_byte
- from
);
6293 allocate_conversion_buffer (buf
, len
);
6295 consumed
= consumed_char
= produced
= produced_char
= 0;
6298 result
= decode_coding (coding
, SDATA (str
) + from
+ consumed
,
6299 buf
.data
+ produced
, to_byte
- from
- consumed
,
6300 buf
.size
- produced
);
6301 consumed
+= coding
->consumed
;
6302 consumed_char
+= coding
->consumed_char
;
6303 produced
+= coding
->produced
;
6304 produced_char
+= coding
->produced_char
;
6305 if (result
== CODING_FINISH_NORMAL
6306 || result
== CODING_FINISH_INTERRUPT
6307 || (result
== CODING_FINISH_INSUFFICIENT_SRC
6308 && coding
->consumed
== 0))
6310 if (result
== CODING_FINISH_INSUFFICIENT_CMP
)
6311 coding_allocate_composition_data (coding
, from
+ produced_char
);
6312 else if (result
== CODING_FINISH_INSUFFICIENT_DST
)
6313 extend_conversion_buffer (&buf
);
6314 else if (result
== CODING_FINISH_INCONSISTENT_EOL
)
6316 Lisp_Object eol_type
;
6318 /* Recover the original EOL format. */
6319 if (coding
->eol_type
== CODING_EOL_CR
)
6322 for (p
= buf
.data
; p
< buf
.data
+ produced
; p
++)
6323 if (*p
== '\n') *p
= '\r';
6325 else if (coding
->eol_type
== CODING_EOL_CRLF
)
6328 unsigned char *p0
, *p1
;
6329 for (p0
= buf
.data
, p1
= p0
+ produced
; p0
< p1
; p0
++)
6330 if (*p0
== '\n') num_eol
++;
6331 if (produced
+ num_eol
>= buf
.size
)
6332 extend_conversion_buffer (&buf
);
6333 for (p0
= buf
.data
+ produced
, p1
= p0
+ num_eol
; p0
> buf
.data
;)
6336 if (*p0
== '\n') *--p1
= '\r';
6338 produced
+= num_eol
;
6339 produced_char
+= num_eol
;
6341 /* Suppress eol-format conversion in the further conversion. */
6342 coding
->eol_type
= CODING_EOL_LF
;
6344 /* Set the coding system symbol to that for Unix-like EOL. */
6345 eol_type
= Fget (saved_coding_symbol
, Qeol_type
);
6346 if (VECTORP (eol_type
)
6347 && XVECTOR (eol_type
)->size
== 3
6348 && SYMBOLP (XVECTOR (eol_type
)->contents
[CODING_EOL_LF
]))
6349 coding
->symbol
= XVECTOR (eol_type
)->contents
[CODING_EOL_LF
];
6351 coding
->symbol
= saved_coding_symbol
;
6357 coding
->consumed
= consumed
;
6358 coding
->consumed_char
= consumed_char
;
6359 coding
->produced
= produced
;
6360 coding
->produced_char
= produced_char
;
6362 if (coding
->dst_multibyte
)
6363 newstr
= make_uninit_multibyte_string (produced_char
+ shrinked_bytes
,
6364 produced
+ shrinked_bytes
);
6366 newstr
= make_uninit_string (produced
+ shrinked_bytes
);
6368 STRING_COPYIN (newstr
, 0, SDATA (str
), from
);
6369 STRING_COPYIN (newstr
, from
, buf
.data
, produced
);
6370 if (shrinked_bytes
> from
)
6371 STRING_COPYIN (newstr
, from
+ produced
,
6372 SDATA (str
) + to_byte
,
6373 shrinked_bytes
- from
);
6374 free_conversion_buffer (&buf
);
6376 coding
->consumed
+= shrinked_bytes
;
6377 coding
->consumed_char
+= shrinked_bytes
;
6378 coding
->produced
+= shrinked_bytes
;
6379 coding
->produced_char
+= shrinked_bytes
;
6381 if (coding
->cmp_data
&& coding
->cmp_data
->used
)
6382 coding_restore_composition (coding
, newstr
);
6383 coding_free_composition_data (coding
);
6385 if (SYMBOLP (coding
->post_read_conversion
)
6386 && !NILP (Ffboundp (coding
->post_read_conversion
)))
6387 newstr
= run_pre_post_conversion_on_str (newstr
, coding
, 0);
6393 encode_coding_string (str
, coding
, nocopy
)
6395 struct coding_system
*coding
;
6399 struct conversion_buffer buf
;
6400 int from
, to
, to_byte
;
6402 int shrinked_bytes
= 0;
6404 int consumed
, consumed_char
, produced
, produced_char
;
6406 if (SYMBOLP (coding
->pre_write_conversion
)
6407 && !NILP (Ffboundp (coding
->pre_write_conversion
)))
6409 str
= run_pre_post_conversion_on_str (str
, coding
, 1);
6410 /* As STR is just newly generated, we don't have to copy it
6417 to_byte
= SBYTES (str
);
6419 /* Encoding routines determine the multibyteness of the source text
6420 by coding->src_multibyte. */
6421 coding
->src_multibyte
= SCHARS (str
) < SBYTES (str
);
6422 coding
->dst_multibyte
= 0;
6423 if (! CODING_REQUIRE_ENCODING (coding
))
6424 goto no_need_of_encoding
;
6426 if (coding
->composing
!= COMPOSITION_DISABLED
)
6427 coding_save_composition (coding
, from
, to
, str
);
6429 /* Try to skip the heading and tailing ASCIIs. We can't skip them
6430 if we must run CCL program or there are compositions to
6432 if (coding
->type
!= coding_type_ccl
6433 && (! coding
->cmp_data
|| coding
->cmp_data
->used
== 0))
6435 SHRINK_CONVERSION_REGION (&from
, &to_byte
, coding
, SDATA (str
),
6437 if (from
== to_byte
)
6439 coding_free_composition_data (coding
);
6440 goto no_need_of_encoding
;
6442 shrinked_bytes
= from
+ (SBYTES (str
) - to_byte
);
6445 len
= encoding_buffer_size (coding
, to_byte
- from
);
6446 allocate_conversion_buffer (buf
, len
);
6448 consumed
= consumed_char
= produced
= produced_char
= 0;
6451 result
= encode_coding (coding
, SDATA (str
) + from
+ consumed
,
6452 buf
.data
+ produced
, to_byte
- from
- consumed
,
6453 buf
.size
- produced
);
6454 consumed
+= coding
->consumed
;
6455 consumed_char
+= coding
->consumed_char
;
6456 produced
+= coding
->produced
;
6457 produced_char
+= coding
->produced_char
;
6458 if (result
== CODING_FINISH_NORMAL
6459 || result
== CODING_FINISH_INTERRUPT
6460 || (result
== CODING_FINISH_INSUFFICIENT_SRC
6461 && coding
->consumed
== 0))
6463 /* Now result should be CODING_FINISH_INSUFFICIENT_DST. */
6464 extend_conversion_buffer (&buf
);
6467 coding
->consumed
= consumed
;
6468 coding
->consumed_char
= consumed_char
;
6469 coding
->produced
= produced
;
6470 coding
->produced_char
= produced_char
;
6472 newstr
= make_uninit_string (produced
+ shrinked_bytes
);
6474 STRING_COPYIN (newstr
, 0, SDATA (str
), from
);
6475 STRING_COPYIN (newstr
, from
, buf
.data
, produced
);
6476 if (shrinked_bytes
> from
)
6477 STRING_COPYIN (newstr
, from
+ produced
,
6478 SDATA (str
) + to_byte
,
6479 shrinked_bytes
- from
);
6481 free_conversion_buffer (&buf
);
6482 coding_free_composition_data (coding
);
6486 no_need_of_encoding
:
6487 coding
->consumed
= SBYTES (str
);
6488 coding
->consumed_char
= SCHARS (str
);
6489 if (STRING_MULTIBYTE (str
))
6492 /* We are sure that STR doesn't contain a multibyte
6494 STRING_SET_UNIBYTE (str
);
6497 str
= Fstring_as_unibyte (str
);
6501 coding
->produced
= SBYTES (str
);
6502 coding
->produced_char
= SCHARS (str
);
6503 return (nocopy
? str
: Fcopy_sequence (str
));
6508 /*** 8. Emacs Lisp library functions ***/
6510 DEFUN ("coding-system-p", Fcoding_system_p
, Scoding_system_p
, 1, 1, 0,
6511 doc
: /* Return t if OBJECT is nil or a coding-system.
6512 See the documentation of `make-coding-system' for information
6513 about coding-system objects. */)
6521 if (! NILP (Fget (obj
, Qcoding_system_define_form
)))
6523 /* Get coding-spec vector for OBJ. */
6524 obj
= Fget (obj
, Qcoding_system
);
6525 return ((VECTORP (obj
) && XVECTOR (obj
)->size
== 5)
6529 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system
,
6530 Sread_non_nil_coding_system
, 1, 1, 0,
6531 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
6538 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
6539 Qt
, Qnil
, Qcoding_system_history
, Qnil
, Qnil
);
6541 while (SCHARS (val
) == 0);
6542 return (Fintern (val
, Qnil
));
6545 DEFUN ("read-coding-system", Fread_coding_system
, Sread_coding_system
, 1, 2, 0,
6546 doc
: /* Read a coding system from the minibuffer, prompting with string PROMPT.
6547 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM. */)
6548 (prompt
, default_coding_system
)
6549 Lisp_Object prompt
, default_coding_system
;
6552 if (SYMBOLP (default_coding_system
))
6553 default_coding_system
= SYMBOL_NAME (default_coding_system
);
6554 val
= Fcompleting_read (prompt
, Vcoding_system_alist
, Qnil
,
6555 Qt
, Qnil
, Qcoding_system_history
,
6556 default_coding_system
, Qnil
);
6557 return (SCHARS (val
) == 0 ? Qnil
: Fintern (val
, Qnil
));
6560 DEFUN ("check-coding-system", Fcheck_coding_system
, Scheck_coding_system
,
6562 doc
: /* Check validity of CODING-SYSTEM.
6563 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
6564 It is valid if it is nil or a symbol with a non-nil `coding-system' property.
6565 The value of this property should be a vector of length 5. */)
6567 Lisp_Object coding_system
;
6569 Lisp_Object define_form
;
6571 define_form
= Fget (coding_system
, Qcoding_system_define_form
);
6572 if (! NILP (define_form
))
6574 Fput (coding_system
, Qcoding_system_define_form
, Qnil
);
6575 safe_eval (define_form
);
6577 if (!NILP (Fcoding_system_p (coding_system
)))
6578 return coding_system
;
6580 Fsignal (Qcoding_system_error
, Fcons (coding_system
, Qnil
));
6584 detect_coding_system (src
, src_bytes
, highest
, multibytep
)
6585 const unsigned char *src
;
6586 int src_bytes
, highest
;
6589 int coding_mask
, eol_type
;
6590 Lisp_Object val
, tmp
;
6593 coding_mask
= detect_coding_mask (src
, src_bytes
, NULL
, &dummy
, multibytep
);
6594 eol_type
= detect_eol_type (src
, src_bytes
, &dummy
);
6595 if (eol_type
== CODING_EOL_INCONSISTENT
)
6596 eol_type
= CODING_EOL_UNDECIDED
;
6601 if (eol_type
!= CODING_EOL_UNDECIDED
)
6604 val2
= Fget (Qundecided
, Qeol_type
);
6606 val
= XVECTOR (val2
)->contents
[eol_type
];
6608 return (highest
? val
: Fcons (val
, Qnil
));
6611 /* At first, gather possible coding systems in VAL. */
6613 for (tmp
= Vcoding_category_list
; CONSP (tmp
); tmp
= XCDR (tmp
))
6615 Lisp_Object category_val
, category_index
;
6617 category_index
= Fget (XCAR (tmp
), Qcoding_category_index
);
6618 category_val
= Fsymbol_value (XCAR (tmp
));
6619 if (!NILP (category_val
)
6620 && NATNUMP (category_index
)
6621 && (coding_mask
& (1 << XFASTINT (category_index
))))
6623 val
= Fcons (category_val
, val
);
6629 val
= Fnreverse (val
);
6631 /* Then, replace the elements with subsidiary coding systems. */
6632 for (tmp
= val
; CONSP (tmp
); tmp
= XCDR (tmp
))
6634 if (eol_type
!= CODING_EOL_UNDECIDED
6635 && eol_type
!= CODING_EOL_INCONSISTENT
)
6638 eol
= Fget (XCAR (tmp
), Qeol_type
);
6640 XSETCAR (tmp
, XVECTOR (eol
)->contents
[eol_type
]);
6643 return (highest
? XCAR (val
) : val
);
6646 DEFUN ("detect-coding-region", Fdetect_coding_region
, Sdetect_coding_region
,
6648 doc
: /* Detect how the byte sequence in the region is encoded.
6649 Return a list of possible coding systems used on decoding a byte
6650 sequence containing the bytes in the region between START and END when
6651 the coding system `undecided' is specified. The list is ordered by
6652 priority decided in the current language environment.
6654 If only ASCII characters are found, it returns a list of single element
6655 `undecided' or its subsidiary coding system according to a detected
6658 If optional argument HIGHEST is non-nil, return the coding system of
6659 highest priority. */)
6660 (start
, end
, highest
)
6661 Lisp_Object start
, end
, highest
;
6664 int from_byte
, to_byte
;
6665 int include_anchor_byte
= 0;
6667 CHECK_NUMBER_COERCE_MARKER (start
);
6668 CHECK_NUMBER_COERCE_MARKER (end
);
6670 validate_region (&start
, &end
);
6671 from
= XINT (start
), to
= XINT (end
);
6672 from_byte
= CHAR_TO_BYTE (from
);
6673 to_byte
= CHAR_TO_BYTE (to
);
6675 if (from
< GPT
&& to
>= GPT
)
6676 move_gap_both (to
, to_byte
);
6677 /* If we an anchor byte `\0' follows the region, we include it in
6678 the detecting source. Then code detectors can handle the tailing
6679 byte sequence more accurately.
6681 Fix me: This is not a perfect solution. It is better that we
6682 add one more argument, say LAST_BLOCK, to all detect_coding_XXX.
6684 if (to
== Z
|| (to
== GPT
&& GAP_SIZE
> 0))
6685 include_anchor_byte
= 1;
6686 return detect_coding_system (BYTE_POS_ADDR (from_byte
),
6687 to_byte
- from_byte
+ include_anchor_byte
,
6689 !NILP (current_buffer
6690 ->enable_multibyte_characters
));
6693 DEFUN ("detect-coding-string", Fdetect_coding_string
, Sdetect_coding_string
,
6695 doc
: /* Detect how the byte sequence in STRING is encoded.
6696 Return a list of possible coding systems used on decoding a byte
6697 sequence containing the bytes in STRING when the coding system
6698 `undecided' is specified. The list is ordered by priority decided in
6699 the current language environment.
6701 If only ASCII characters are found, it returns a list of single element
6702 `undecided' or its subsidiary coding system according to a detected
6705 If optional argument HIGHEST is non-nil, return the coding system of
6706 highest priority. */)
6708 Lisp_Object string
, highest
;
6710 CHECK_STRING (string
);
6712 return detect_coding_system (SDATA (string
),
6713 /* "+ 1" is to include the anchor byte
6714 `\0'. With this, code detectors can
6715 handle the tailing bytes more
6717 SBYTES (string
) + 1,
6719 STRING_MULTIBYTE (string
));
6722 /* Subroutine for Ffind_coding_systems_region_internal.
6724 Return a list of coding systems that safely encode the multibyte
6725 text between P and PEND. SAFE_CODINGS, if non-nil, is an alist of
6726 possible coding systems. If it is nil, it means that we have not
6727 yet found any coding systems.
6729 WORK_TABLE a char-table of which element is set to t once the
6730 element is looked up.
6732 If a non-ASCII single byte char is found, set
6733 *single_byte_char_found to 1. */
6736 find_safe_codings (p
, pend
, safe_codings
, work_table
, single_byte_char_found
)
6737 unsigned char *p
, *pend
;
6738 Lisp_Object safe_codings
, work_table
;
6739 int *single_byte_char_found
;
6742 Lisp_Object val
, ch
;
6743 Lisp_Object prev
, tail
;
6745 if (NILP (safe_codings
))
6746 goto done_safe_codings
;
6749 c
= STRING_CHAR_AND_LENGTH (p
, pend
- p
, len
);
6751 if (ASCII_BYTE_P (c
))
6752 /* We can ignore ASCII characters here. */
6754 if (SINGLE_BYTE_CHAR_P (c
))
6755 *single_byte_char_found
= 1;
6756 /* Check the safe coding systems for C. */
6757 ch
= make_number (c
);
6758 val
= Faref (work_table
, ch
);
6760 /* This element was already checked. Ignore it. */
6762 /* Remember that we checked this element. */
6763 Faset (work_table
, ch
, Qt
);
6765 for (prev
= tail
= safe_codings
; CONSP (tail
); tail
= XCDR (tail
))
6767 Lisp_Object elt
, translation_table
, hash_table
, accept_latin_extra
;
6771 if (CONSP (XCDR (elt
)))
6773 /* This entry has this format now:
6774 ( CODING SAFE-CHARS TRANSLATION-TABLE HASH-TABLE
6775 ACCEPT-LATIN-EXTRA ) */
6777 encodable
= ! NILP (Faref (XCAR (val
), ch
));
6781 translation_table
= XCAR (val
);
6782 hash_table
= XCAR (XCDR (val
));
6783 accept_latin_extra
= XCAR (XCDR (XCDR (val
)));
6788 /* This entry has this format now: ( CODING . SAFE-CHARS) */
6789 encodable
= ! NILP (Faref (XCDR (elt
), ch
));
6792 /* Transform the format to:
6793 ( CODING SAFE-CHARS TRANSLATION-TABLE HASH-TABLE
6794 ACCEPT-LATIN-EXTRA ) */
6795 val
= Fget (XCAR (elt
), Qcoding_system
);
6797 = Fplist_get (AREF (val
, 3),
6798 Qtranslation_table_for_encode
);
6799 if (SYMBOLP (translation_table
))
6800 translation_table
= Fget (translation_table
,
6801 Qtranslation_table
);
6803 = (CHAR_TABLE_P (translation_table
)
6804 ? XCHAR_TABLE (translation_table
)->extras
[1]
6807 = ((EQ (AREF (val
, 0), make_number (2))
6808 && VECTORP (AREF (val
, 4)))
6809 ? AREF (AREF (val
, 4), 16)
6811 XSETCAR (tail
, list5 (XCAR (elt
), XCDR (elt
),
6812 translation_table
, hash_table
,
6813 accept_latin_extra
));
6818 && ((CHAR_TABLE_P (translation_table
)
6819 && ! NILP (Faref (translation_table
, ch
)))
6820 || (HASH_TABLE_P (hash_table
)
6821 && ! NILP (Fgethash (ch
, hash_table
, Qnil
)))
6822 || (SINGLE_BYTE_CHAR_P (c
)
6823 && ! NILP (accept_latin_extra
)
6824 && VECTORP (Vlatin_extra_code_table
)
6825 && ! NILP (AREF (Vlatin_extra_code_table
, c
)))))
6831 /* Exclude this coding system from SAFE_CODINGS. */
6832 if (EQ (tail
, safe_codings
))
6834 safe_codings
= XCDR (safe_codings
);
6835 if (NILP (safe_codings
))
6836 goto done_safe_codings
;
6839 XSETCDR (prev
, XCDR (tail
));
6845 /* If the above loop was terminated before P reaches PEND, it means
6846 SAFE_CODINGS was set to nil. If we have not yet found an
6847 non-ASCII single-byte char, check it now. */
6848 if (! *single_byte_char_found
)
6851 c
= STRING_CHAR_AND_LENGTH (p
, pend
- p
, len
);
6853 if (! ASCII_BYTE_P (c
)
6854 && SINGLE_BYTE_CHAR_P (c
))
6856 *single_byte_char_found
= 1;
6860 return safe_codings
;
6863 DEFUN ("find-coding-systems-region-internal",
6864 Ffind_coding_systems_region_internal
,
6865 Sfind_coding_systems_region_internal
, 2, 2, 0,
6866 doc
: /* Internal use only. */)
6868 Lisp_Object start
, end
;
6870 Lisp_Object work_table
, safe_codings
;
6871 int non_ascii_p
= 0;
6872 int single_byte_char_found
= 0;
6873 const unsigned char *p1
, *p1end
, *p2
, *p2end
, *p
;
6875 if (STRINGP (start
))
6877 if (!STRING_MULTIBYTE (start
))
6879 p1
= SDATA (start
), p1end
= p1
+ SBYTES (start
);
6881 if (SCHARS (start
) != SBYTES (start
))
6888 CHECK_NUMBER_COERCE_MARKER (start
);
6889 CHECK_NUMBER_COERCE_MARKER (end
);
6890 if (XINT (start
) < BEG
|| XINT (end
) > Z
|| XINT (start
) > XINT (end
))
6891 args_out_of_range (start
, end
);
6892 if (NILP (current_buffer
->enable_multibyte_characters
))
6894 from
= CHAR_TO_BYTE (XINT (start
));
6895 to
= CHAR_TO_BYTE (XINT (end
));
6896 stop
= from
< GPT_BYTE
&& GPT_BYTE
< to
? GPT_BYTE
: to
;
6897 p1
= BYTE_POS_ADDR (from
), p1end
= p1
+ (stop
- from
);
6901 p2
= BYTE_POS_ADDR (stop
), p2end
= p2
+ (to
- stop
);
6902 if (XINT (end
) - XINT (start
) != to
- from
)
6908 /* We are sure that the text contains no multibyte character.
6909 Check if it contains eight-bit-graphic. */
6911 for (p
= p1
; p
< p1end
&& ASCII_BYTE_P (*p
); p
++);
6914 for (p
= p2
; p
< p2end
&& ASCII_BYTE_P (*p
); p
++);
6920 /* The text contains non-ASCII characters. */
6922 work_table
= Fmake_char_table (Qchar_coding_system
, Qnil
);
6923 safe_codings
= Fcopy_sequence (XCDR (Vcoding_system_safe_chars
));
6925 safe_codings
= find_safe_codings (p1
, p1end
, safe_codings
, work_table
,
6926 &single_byte_char_found
);
6928 safe_codings
= find_safe_codings (p2
, p2end
, safe_codings
, work_table
,
6929 &single_byte_char_found
);
6930 if (EQ (safe_codings
, XCDR (Vcoding_system_safe_chars
)))
6934 /* Turn safe_codings to a list of coding systems... */
6937 if (single_byte_char_found
)
6938 /* ... and append these for eight-bit chars. */
6939 val
= Fcons (Qraw_text
,
6940 Fcons (Qemacs_mule
, Fcons (Qno_conversion
, Qnil
)));
6942 /* ... and append generic coding systems. */
6943 val
= Fcopy_sequence (XCAR (Vcoding_system_safe_chars
));
6945 for (; CONSP (safe_codings
); safe_codings
= XCDR (safe_codings
))
6946 val
= Fcons (XCAR (XCAR (safe_codings
)), val
);
6950 return safe_codings
;
6954 /* Search from position POS for such characters that are unencodable
6955 accoding to SAFE_CHARS, and return a list of their positions. P
6956 points where in the memory the character at POS exists. Limit the
6957 search at PEND or when Nth unencodable characters are found.
6959 If SAFE_CHARS is a char table, an element for an unencodable
6962 If SAFE_CHARS is nil, all non-ASCII characters are unencodable.
6964 Otherwise, SAFE_CHARS is t, and only eight-bit-contrl and
6965 eight-bit-graphic characters are unencodable. */
6968 unencodable_char_position (safe_chars
, pos
, p
, pend
, n
)
6969 Lisp_Object safe_chars
;
6971 unsigned char *p
, *pend
;
6974 Lisp_Object pos_list
;
6980 int c
= STRING_CHAR_AND_LENGTH (p
, MAX_MULTIBYTE_LENGTH
, len
);
6983 && (CHAR_TABLE_P (safe_chars
)
6984 ? NILP (CHAR_TABLE_REF (safe_chars
, c
))
6985 : (NILP (safe_chars
) || c
< 256)))
6987 pos_list
= Fcons (make_number (pos
), pos_list
);
6994 return Fnreverse (pos_list
);
6998 DEFUN ("unencodable-char-position", Funencodable_char_position
,
6999 Sunencodable_char_position
, 3, 5, 0,
7001 Return position of first un-encodable character in a region.
7002 START and END specfiy the region and CODING-SYSTEM specifies the
7003 encoding to check. Return nil if CODING-SYSTEM does encode the region.
7005 If optional 4th argument COUNT is non-nil, it specifies at most how
7006 many un-encodable characters to search. In this case, the value is a
7009 If optional 5th argument STRING is non-nil, it is a string to search
7010 for un-encodable characters. In that case, START and END are indexes
7012 (start
, end
, coding_system
, count
, string
)
7013 Lisp_Object start
, end
, coding_system
, count
, string
;
7016 Lisp_Object safe_chars
;
7017 struct coding_system coding
;
7018 Lisp_Object positions
;
7020 unsigned char *p
, *pend
;
7024 validate_region (&start
, &end
);
7025 from
= XINT (start
);
7027 if (NILP (current_buffer
->enable_multibyte_characters
))
7029 p
= CHAR_POS_ADDR (from
);
7033 pend
= CHAR_POS_ADDR (to
);
7037 CHECK_STRING (string
);
7038 CHECK_NATNUM (start
);
7040 from
= XINT (start
);
7043 || to
> SCHARS (string
))
7044 args_out_of_range_3 (string
, start
, end
);
7045 if (! STRING_MULTIBYTE (string
))
7047 p
= SDATA (string
) + string_char_to_byte (string
, from
);
7048 pend
= SDATA (string
) + string_char_to_byte (string
, to
);
7051 setup_coding_system (Fcheck_coding_system (coding_system
), &coding
);
7057 CHECK_NATNUM (count
);
7061 if (coding
.type
== coding_type_no_conversion
7062 || coding
.type
== coding_type_raw_text
)
7065 if (coding
.type
== coding_type_undecided
)
7068 safe_chars
= coding_safe_chars (coding_system
);
7070 if (STRINGP (string
)
7071 || from
>= GPT
|| to
<= GPT
)
7072 positions
= unencodable_char_position (safe_chars
, from
, p
, pend
, n
);
7075 Lisp_Object args
[2];
7077 args
[0] = unencodable_char_position (safe_chars
, from
, p
, GPT_ADDR
, n
);
7078 n
-= XINT (Flength (args
[0]));
7080 positions
= args
[0];
7083 args
[1] = unencodable_char_position (safe_chars
, GPT
, GAP_END_ADDR
,
7085 positions
= Fappend (2, args
);
7089 return (NILP (count
) ? Fcar (positions
) : positions
);
7094 code_convert_region1 (start
, end
, coding_system
, encodep
)
7095 Lisp_Object start
, end
, coding_system
;
7098 struct coding_system coding
;
7101 CHECK_NUMBER_COERCE_MARKER (start
);
7102 CHECK_NUMBER_COERCE_MARKER (end
);
7103 CHECK_SYMBOL (coding_system
);
7105 validate_region (&start
, &end
);
7106 from
= XFASTINT (start
);
7107 to
= XFASTINT (end
);
7109 if (NILP (coding_system
))
7110 return make_number (to
- from
);
7112 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
7113 error ("Invalid coding system: %s", SDATA (SYMBOL_NAME (coding_system
)));
7115 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
7116 coding
.src_multibyte
= coding
.dst_multibyte
7117 = !NILP (current_buffer
->enable_multibyte_characters
);
7118 code_convert_region (from
, CHAR_TO_BYTE (from
), to
, CHAR_TO_BYTE (to
),
7119 &coding
, encodep
, 1);
7120 Vlast_coding_system_used
= coding
.symbol
;
7121 return make_number (coding
.produced_char
);
7124 DEFUN ("decode-coding-region", Fdecode_coding_region
, Sdecode_coding_region
,
7125 3, 3, "r\nzCoding system: ",
7126 doc
: /* Decode the current region from the specified coding system.
7127 When called from a program, takes three arguments:
7128 START, END, and CODING-SYSTEM. START and END are buffer positions.
7129 This function sets `last-coding-system-used' to the precise coding system
7130 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
7131 not fully specified.)
7132 It returns the length of the decoded text. */)
7133 (start
, end
, coding_system
)
7134 Lisp_Object start
, end
, coding_system
;
7136 return code_convert_region1 (start
, end
, coding_system
, 0);
7139 DEFUN ("encode-coding-region", Fencode_coding_region
, Sencode_coding_region
,
7140 3, 3, "r\nzCoding system: ",
7141 doc
: /* Encode the current region into the specified coding system.
7142 When called from a program, takes three arguments:
7143 START, END, and CODING-SYSTEM. START and END are buffer positions.
7144 This function sets `last-coding-system-used' to the precise coding system
7145 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
7146 not fully specified.)
7147 It returns the length of the encoded text. */)
7148 (start
, end
, coding_system
)
7149 Lisp_Object start
, end
, coding_system
;
7151 return code_convert_region1 (start
, end
, coding_system
, 1);
7155 code_convert_string1 (string
, coding_system
, nocopy
, encodep
)
7156 Lisp_Object string
, coding_system
, nocopy
;
7159 struct coding_system coding
;
7161 CHECK_STRING (string
);
7162 CHECK_SYMBOL (coding_system
);
7164 if (NILP (coding_system
))
7165 return (NILP (nocopy
) ? Fcopy_sequence (string
) : string
);
7167 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
7168 error ("Invalid coding system: %s", SDATA (SYMBOL_NAME (coding_system
)));
7170 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
7172 ? encode_coding_string (string
, &coding
, !NILP (nocopy
))
7173 : decode_coding_string (string
, &coding
, !NILP (nocopy
)));
7174 Vlast_coding_system_used
= coding
.symbol
;
7179 DEFUN ("decode-coding-string", Fdecode_coding_string
, Sdecode_coding_string
,
7181 doc
: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
7182 Optional arg NOCOPY non-nil means it is OK to return STRING itself
7183 if the decoding operation is trivial.
7184 This function sets `last-coding-system-used' to the precise coding system
7185 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
7186 not fully specified.) */)
7187 (string
, coding_system
, nocopy
)
7188 Lisp_Object string
, coding_system
, nocopy
;
7190 return code_convert_string1 (string
, coding_system
, nocopy
, 0);
7193 DEFUN ("encode-coding-string", Fencode_coding_string
, Sencode_coding_string
,
7195 doc
: /* Encode STRING to CODING-SYSTEM, and return the result.
7196 Optional arg NOCOPY non-nil means it is OK to return STRING itself
7197 if the encoding operation is trivial.
7198 This function sets `last-coding-system-used' to the precise coding system
7199 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
7200 not fully specified.) */)
7201 (string
, coding_system
, nocopy
)
7202 Lisp_Object string
, coding_system
, nocopy
;
7204 return code_convert_string1 (string
, coding_system
, nocopy
, 1);
7207 /* Encode or decode STRING according to CODING_SYSTEM.
7208 Do not set Vlast_coding_system_used.
7210 This function is called only from macros DECODE_FILE and
7211 ENCODE_FILE, thus we ignore character composition. */
7214 code_convert_string_norecord (string
, coding_system
, encodep
)
7215 Lisp_Object string
, coding_system
;
7218 struct coding_system coding
;
7220 CHECK_STRING (string
);
7221 CHECK_SYMBOL (coding_system
);
7223 if (NILP (coding_system
))
7226 if (setup_coding_system (Fcheck_coding_system (coding_system
), &coding
) < 0)
7227 error ("Invalid coding system: %s", SDATA (SYMBOL_NAME (coding_system
)));
7229 coding
.composing
= COMPOSITION_DISABLED
;
7230 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
7232 ? encode_coding_string (string
, &coding
, 1)
7233 : decode_coding_string (string
, &coding
, 1));
7236 DEFUN ("decode-sjis-char", Fdecode_sjis_char
, Sdecode_sjis_char
, 1, 1, 0,
7237 doc
: /* Decode a Japanese character which has CODE in shift_jis encoding.
7238 Return the corresponding character. */)
7242 unsigned char c1
, c2
, s1
, s2
;
7245 CHECK_NUMBER (code
);
7246 s1
= (XFASTINT (code
)) >> 8, s2
= (XFASTINT (code
)) & 0xFF;
7250 XSETFASTINT (val
, s2
);
7251 else if (s2
>= 0xA0 || s2
<= 0xDF)
7252 XSETFASTINT (val
, MAKE_CHAR (charset_katakana_jisx0201
, s2
, 0));
7254 error ("Invalid Shift JIS code: %x", XFASTINT (code
));
7258 if ((s1
< 0x80 || (s1
> 0x9F && s1
< 0xE0) || s1
> 0xEF)
7259 || (s2
< 0x40 || s2
== 0x7F || s2
> 0xFC))
7260 error ("Invalid Shift JIS code: %x", XFASTINT (code
));
7261 DECODE_SJIS (s1
, s2
, c1
, c2
);
7262 XSETFASTINT (val
, MAKE_CHAR (charset_jisx0208
, c1
, c2
));
7267 DEFUN ("encode-sjis-char", Fencode_sjis_char
, Sencode_sjis_char
, 1, 1, 0,
7268 doc
: /* Encode a Japanese character CHAR to shift_jis encoding.
7269 Return the corresponding code in SJIS. */)
7273 int charset
, c1
, c2
, s1
, s2
;
7277 SPLIT_CHAR (XFASTINT (ch
), charset
, c1
, c2
);
7278 if (charset
== CHARSET_ASCII
)
7282 else if (charset
== charset_jisx0208
7283 && c1
> 0x20 && c1
< 0x7F && c2
> 0x20 && c2
< 0x7F)
7285 ENCODE_SJIS (c1
, c2
, s1
, s2
);
7286 XSETFASTINT (val
, (s1
<< 8) | s2
);
7288 else if (charset
== charset_katakana_jisx0201
7289 && c1
> 0x20 && c2
< 0xE0)
7291 XSETFASTINT (val
, c1
| 0x80);
7294 error ("Can't encode to shift_jis: %d", XFASTINT (ch
));
7298 DEFUN ("decode-big5-char", Fdecode_big5_char
, Sdecode_big5_char
, 1, 1, 0,
7299 doc
: /* Decode a Big5 character which has CODE in BIG5 coding system.
7300 Return the corresponding character. */)
7305 unsigned char b1
, b2
, c1
, c2
;
7308 CHECK_NUMBER (code
);
7309 b1
= (XFASTINT (code
)) >> 8, b2
= (XFASTINT (code
)) & 0xFF;
7313 error ("Invalid BIG5 code: %x", XFASTINT (code
));
7318 if ((b1
< 0xA1 || b1
> 0xFE)
7319 || (b2
< 0x40 || (b2
> 0x7E && b2
< 0xA1) || b2
> 0xFE))
7320 error ("Invalid BIG5 code: %x", XFASTINT (code
));
7321 DECODE_BIG5 (b1
, b2
, charset
, c1
, c2
);
7322 XSETFASTINT (val
, MAKE_CHAR (charset
, c1
, c2
));
7327 DEFUN ("encode-big5-char", Fencode_big5_char
, Sencode_big5_char
, 1, 1, 0,
7328 doc
: /* Encode the Big5 character CHAR to BIG5 coding system.
7329 Return the corresponding character code in Big5. */)
7333 int charset
, c1
, c2
, b1
, b2
;
7337 SPLIT_CHAR (XFASTINT (ch
), charset
, c1
, c2
);
7338 if (charset
== CHARSET_ASCII
)
7342 else if ((charset
== charset_big5_1
7343 && (XFASTINT (ch
) >= 0x250a1 && XFASTINT (ch
) <= 0x271ec))
7344 || (charset
== charset_big5_2
7345 && XFASTINT (ch
) >= 0x290a1 && XFASTINT (ch
) <= 0x2bdb2))
7347 ENCODE_BIG5 (charset
, c1
, c2
, b1
, b2
);
7348 XSETFASTINT (val
, (b1
<< 8) | b2
);
7351 error ("Can't encode to Big5: %d", XFASTINT (ch
));
7355 DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal
,
7356 Sset_terminal_coding_system_internal
, 1, 1, 0,
7357 doc
: /* Internal use only. */)
7359 Lisp_Object coding_system
;
7361 CHECK_SYMBOL (coding_system
);
7362 setup_coding_system (Fcheck_coding_system (coding_system
), &terminal_coding
);
7363 /* We had better not send unsafe characters to terminal. */
7364 terminal_coding
.mode
|= CODING_MODE_INHIBIT_UNENCODABLE_CHAR
;
7365 /* Character composition should be disabled. */
7366 terminal_coding
.composing
= COMPOSITION_DISABLED
;
7367 /* Error notification should be suppressed. */
7368 terminal_coding
.suppress_error
= 1;
7369 terminal_coding
.src_multibyte
= 1;
7370 terminal_coding
.dst_multibyte
= 0;
7374 DEFUN ("set-safe-terminal-coding-system-internal", Fset_safe_terminal_coding_system_internal
,
7375 Sset_safe_terminal_coding_system_internal
, 1, 1, 0,
7376 doc
: /* Internal use only. */)
7378 Lisp_Object coding_system
;
7380 CHECK_SYMBOL (coding_system
);
7381 setup_coding_system (Fcheck_coding_system (coding_system
),
7382 &safe_terminal_coding
);
7383 /* Character composition should be disabled. */
7384 safe_terminal_coding
.composing
= COMPOSITION_DISABLED
;
7385 /* Error notification should be suppressed. */
7386 safe_terminal_coding
.suppress_error
= 1;
7387 safe_terminal_coding
.src_multibyte
= 1;
7388 safe_terminal_coding
.dst_multibyte
= 0;
7392 DEFUN ("terminal-coding-system", Fterminal_coding_system
,
7393 Sterminal_coding_system
, 0, 0, 0,
7394 doc
: /* Return coding system specified for terminal output. */)
7397 return terminal_coding
.symbol
;
7400 DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal
,
7401 Sset_keyboard_coding_system_internal
, 1, 1, 0,
7402 doc
: /* Internal use only. */)
7404 Lisp_Object coding_system
;
7406 CHECK_SYMBOL (coding_system
);
7407 setup_coding_system (Fcheck_coding_system (coding_system
), &keyboard_coding
);
7408 /* Character composition should be disabled. */
7409 keyboard_coding
.composing
= COMPOSITION_DISABLED
;
7413 DEFUN ("keyboard-coding-system", Fkeyboard_coding_system
,
7414 Skeyboard_coding_system
, 0, 0, 0,
7415 doc
: /* Return coding system specified for decoding keyboard input. */)
7418 return keyboard_coding
.symbol
;
7422 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system
,
7423 Sfind_operation_coding_system
, 1, MANY
, 0,
7424 doc
: /* Choose a coding system for an operation based on the target name.
7425 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
7426 DECODING-SYSTEM is the coding system to use for decoding
7427 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
7428 for encoding (in case OPERATION does encoding).
7430 The first argument OPERATION specifies an I/O primitive:
7431 For file I/O, `insert-file-contents' or `write-region'.
7432 For process I/O, `call-process', `call-process-region', or `start-process'.
7433 For network I/O, `open-network-stream'.
7435 The remaining arguments should be the same arguments that were passed
7436 to the primitive. Depending on which primitive, one of those arguments
7437 is selected as the TARGET. For example, if OPERATION does file I/O,
7438 whichever argument specifies the file name is TARGET.
7440 TARGET has a meaning which depends on OPERATION:
7441 For file I/O, TARGET is a file name.
7442 For process I/O, TARGET is a process name.
7443 For network I/O, TARGET is a service name or a port number
7445 This function looks up what specified for TARGET in,
7446 `file-coding-system-alist', `process-coding-system-alist',
7447 or `network-coding-system-alist' depending on OPERATION.
7448 They may specify a coding system, a cons of coding systems,
7449 or a function symbol to call.
7450 In the last case, we call the function with one argument,
7451 which is a list of all the arguments given to this function.
7453 usage: (find-operation-coding-system OPERATION ARGUMENTS ...) */)
7458 Lisp_Object operation
, target_idx
, target
, val
;
7459 register Lisp_Object chain
;
7462 error ("Too few arguments");
7463 operation
= args
[0];
7464 if (!SYMBOLP (operation
)
7465 || !INTEGERP (target_idx
= Fget (operation
, Qtarget_idx
)))
7466 error ("Invalid first argument");
7467 if (nargs
< 1 + XINT (target_idx
))
7468 error ("Too few arguments for operation: %s",
7469 SDATA (SYMBOL_NAME (operation
)));
7470 /* For write-region, if the 6th argument (i.e. VISIT, the 5th
7471 argument to write-region) is string, it must be treated as a
7472 target file name. */
7473 if (EQ (operation
, Qwrite_region
)
7475 && STRINGP (args
[5]))
7476 target_idx
= make_number (4);
7477 target
= args
[XINT (target_idx
) + 1];
7478 if (!(STRINGP (target
)
7479 || (EQ (operation
, Qopen_network_stream
) && INTEGERP (target
))))
7480 error ("Invalid argument %d", XINT (target_idx
) + 1);
7482 chain
= ((EQ (operation
, Qinsert_file_contents
)
7483 || EQ (operation
, Qwrite_region
))
7484 ? Vfile_coding_system_alist
7485 : (EQ (operation
, Qopen_network_stream
)
7486 ? Vnetwork_coding_system_alist
7487 : Vprocess_coding_system_alist
));
7491 for (; CONSP (chain
); chain
= XCDR (chain
))
7497 && ((STRINGP (target
)
7498 && STRINGP (XCAR (elt
))
7499 && fast_string_match (XCAR (elt
), target
) >= 0)
7500 || (INTEGERP (target
) && EQ (target
, XCAR (elt
)))))
7503 /* Here, if VAL is both a valid coding system and a valid
7504 function symbol, we return VAL as a coding system. */
7507 if (! SYMBOLP (val
))
7509 if (! NILP (Fcoding_system_p (val
)))
7510 return Fcons (val
, val
);
7511 if (! NILP (Ffboundp (val
)))
7513 val
= call1 (val
, Flist (nargs
, args
));
7516 if (SYMBOLP (val
) && ! NILP (Fcoding_system_p (val
)))
7517 return Fcons (val
, val
);
7525 DEFUN ("update-coding-systems-internal", Fupdate_coding_systems_internal
,
7526 Supdate_coding_systems_internal
, 0, 0, 0,
7527 doc
: /* Update internal database for ISO2022 and CCL based coding systems.
7528 When values of any coding categories are changed, you must
7529 call this function. */)
7534 for (i
= CODING_CATEGORY_IDX_EMACS_MULE
; i
< CODING_CATEGORY_IDX_MAX
; i
++)
7538 val
= SYMBOL_VALUE (XVECTOR (Vcoding_category_table
)->contents
[i
]);
7541 if (! coding_system_table
[i
])
7542 coding_system_table
[i
] = ((struct coding_system
*)
7543 xmalloc (sizeof (struct coding_system
)));
7544 setup_coding_system (val
, coding_system_table
[i
]);
7546 else if (coding_system_table
[i
])
7548 xfree (coding_system_table
[i
]);
7549 coding_system_table
[i
] = NULL
;
7556 DEFUN ("set-coding-priority-internal", Fset_coding_priority_internal
,
7557 Sset_coding_priority_internal
, 0, 0, 0,
7558 doc
: /* Update internal database for the current value of `coding-category-list'.
7559 This function is internal use only. */)
7565 val
= Vcoding_category_list
;
7567 while (CONSP (val
) && i
< CODING_CATEGORY_IDX_MAX
)
7569 if (! SYMBOLP (XCAR (val
)))
7571 idx
= XFASTINT (Fget (XCAR (val
), Qcoding_category_index
));
7572 if (idx
>= CODING_CATEGORY_IDX_MAX
)
7574 coding_priorities
[i
++] = (1 << idx
);
7577 /* If coding-category-list is valid and contains all coding
7578 categories, `i' should be CODING_CATEGORY_IDX_MAX now. If not,
7579 the following code saves Emacs from crashing. */
7580 while (i
< CODING_CATEGORY_IDX_MAX
)
7581 coding_priorities
[i
++] = CODING_CATEGORY_MASK_RAW_TEXT
;
7586 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal
,
7587 Sdefine_coding_system_internal
, 1, 1, 0,
7588 doc
: /* Register CODING-SYSTEM as a base coding system.
7589 This function is internal use only. */)
7591 Lisp_Object coding_system
;
7593 Lisp_Object safe_chars
, slot
;
7595 if (NILP (Fcheck_coding_system (coding_system
)))
7596 Fsignal (Qcoding_system_error
, Fcons (coding_system
, Qnil
));
7597 safe_chars
= coding_safe_chars (coding_system
);
7598 if (! EQ (safe_chars
, Qt
) && ! CHAR_TABLE_P (safe_chars
))
7599 error ("No valid safe-chars property for %s",
7600 SDATA (SYMBOL_NAME (coding_system
)));
7601 if (EQ (safe_chars
, Qt
))
7603 if (NILP (Fmemq (coding_system
, XCAR (Vcoding_system_safe_chars
))))
7604 XSETCAR (Vcoding_system_safe_chars
,
7605 Fcons (coding_system
, XCAR (Vcoding_system_safe_chars
)));
7609 slot
= Fassq (coding_system
, XCDR (Vcoding_system_safe_chars
));
7611 XSETCDR (Vcoding_system_safe_chars
,
7612 nconc2 (XCDR (Vcoding_system_safe_chars
),
7613 Fcons (Fcons (coding_system
, safe_chars
), Qnil
)));
7615 XSETCDR (slot
, safe_chars
);
7623 /*** 9. Post-amble ***/
7630 /* Emacs' internal format specific initialize routine. */
7631 for (i
= 0; i
<= 0x20; i
++)
7632 emacs_code_class
[i
] = EMACS_control_code
;
7633 emacs_code_class
[0x0A] = EMACS_linefeed_code
;
7634 emacs_code_class
[0x0D] = EMACS_carriage_return_code
;
7635 for (i
= 0x21 ; i
< 0x7F; i
++)
7636 emacs_code_class
[i
] = EMACS_ascii_code
;
7637 emacs_code_class
[0x7F] = EMACS_control_code
;
7638 for (i
= 0x80; i
< 0xFF; i
++)
7639 emacs_code_class
[i
] = EMACS_invalid_code
;
7640 emacs_code_class
[LEADING_CODE_PRIVATE_11
] = EMACS_leading_code_3
;
7641 emacs_code_class
[LEADING_CODE_PRIVATE_12
] = EMACS_leading_code_3
;
7642 emacs_code_class
[LEADING_CODE_PRIVATE_21
] = EMACS_leading_code_4
;
7643 emacs_code_class
[LEADING_CODE_PRIVATE_22
] = EMACS_leading_code_4
;
7645 /* ISO2022 specific initialize routine. */
7646 for (i
= 0; i
< 0x20; i
++)
7647 iso_code_class
[i
] = ISO_control_0
;
7648 for (i
= 0x21; i
< 0x7F; i
++)
7649 iso_code_class
[i
] = ISO_graphic_plane_0
;
7650 for (i
= 0x80; i
< 0xA0; i
++)
7651 iso_code_class
[i
] = ISO_control_1
;
7652 for (i
= 0xA1; i
< 0xFF; i
++)
7653 iso_code_class
[i
] = ISO_graphic_plane_1
;
7654 iso_code_class
[0x20] = iso_code_class
[0x7F] = ISO_0x20_or_0x7F
;
7655 iso_code_class
[0xA0] = iso_code_class
[0xFF] = ISO_0xA0_or_0xFF
;
7656 iso_code_class
[ISO_CODE_CR
] = ISO_carriage_return
;
7657 iso_code_class
[ISO_CODE_SO
] = ISO_shift_out
;
7658 iso_code_class
[ISO_CODE_SI
] = ISO_shift_in
;
7659 iso_code_class
[ISO_CODE_SS2_7
] = ISO_single_shift_2_7
;
7660 iso_code_class
[ISO_CODE_ESC
] = ISO_escape
;
7661 iso_code_class
[ISO_CODE_SS2
] = ISO_single_shift_2
;
7662 iso_code_class
[ISO_CODE_SS3
] = ISO_single_shift_3
;
7663 iso_code_class
[ISO_CODE_CSI
] = ISO_control_sequence_introducer
;
7665 setup_coding_system (Qnil
, &keyboard_coding
);
7666 setup_coding_system (Qnil
, &terminal_coding
);
7667 setup_coding_system (Qnil
, &safe_terminal_coding
);
7668 setup_coding_system (Qnil
, &default_buffer_file_coding
);
7670 bzero (coding_system_table
, sizeof coding_system_table
);
7672 bzero (ascii_skip_code
, sizeof ascii_skip_code
);
7673 for (i
= 0; i
< 128; i
++)
7674 ascii_skip_code
[i
] = 1;
7676 #if defined (MSDOS) || defined (WINDOWSNT)
7677 system_eol_type
= CODING_EOL_CRLF
;
7679 system_eol_type
= CODING_EOL_LF
;
7682 inhibit_pre_post_conversion
= 0;
7690 staticpro (&Vcode_conversion_workbuf_name
);
7691 Vcode_conversion_workbuf_name
= build_string (" *code-conversion-work*");
7693 Qtarget_idx
= intern ("target-idx");
7694 staticpro (&Qtarget_idx
);
7696 Qcoding_system_history
= intern ("coding-system-history");
7697 staticpro (&Qcoding_system_history
);
7698 Fset (Qcoding_system_history
, Qnil
);
7700 /* Target FILENAME is the first argument. */
7701 Fput (Qinsert_file_contents
, Qtarget_idx
, make_number (0));
7702 /* Target FILENAME is the third argument. */
7703 Fput (Qwrite_region
, Qtarget_idx
, make_number (2));
7705 Qcall_process
= intern ("call-process");
7706 staticpro (&Qcall_process
);
7707 /* Target PROGRAM is the first argument. */
7708 Fput (Qcall_process
, Qtarget_idx
, make_number (0));
7710 Qcall_process_region
= intern ("call-process-region");
7711 staticpro (&Qcall_process_region
);
7712 /* Target PROGRAM is the third argument. */
7713 Fput (Qcall_process_region
, Qtarget_idx
, make_number (2));
7715 Qstart_process
= intern ("start-process");
7716 staticpro (&Qstart_process
);
7717 /* Target PROGRAM is the third argument. */
7718 Fput (Qstart_process
, Qtarget_idx
, make_number (2));
7720 Qopen_network_stream
= intern ("open-network-stream");
7721 staticpro (&Qopen_network_stream
);
7722 /* Target SERVICE is the fourth argument. */
7723 Fput (Qopen_network_stream
, Qtarget_idx
, make_number (3));
7725 Qcoding_system
= intern ("coding-system");
7726 staticpro (&Qcoding_system
);
7728 Qeol_type
= intern ("eol-type");
7729 staticpro (&Qeol_type
);
7731 Qbuffer_file_coding_system
= intern ("buffer-file-coding-system");
7732 staticpro (&Qbuffer_file_coding_system
);
7734 Qpost_read_conversion
= intern ("post-read-conversion");
7735 staticpro (&Qpost_read_conversion
);
7737 Qpre_write_conversion
= intern ("pre-write-conversion");
7738 staticpro (&Qpre_write_conversion
);
7740 Qno_conversion
= intern ("no-conversion");
7741 staticpro (&Qno_conversion
);
7743 Qundecided
= intern ("undecided");
7744 staticpro (&Qundecided
);
7746 Qcoding_system_p
= intern ("coding-system-p");
7747 staticpro (&Qcoding_system_p
);
7749 Qcoding_system_error
= intern ("coding-system-error");
7750 staticpro (&Qcoding_system_error
);
7752 Fput (Qcoding_system_error
, Qerror_conditions
,
7753 Fcons (Qcoding_system_error
, Fcons (Qerror
, Qnil
)));
7754 Fput (Qcoding_system_error
, Qerror_message
,
7755 build_string ("Invalid coding system"));
7757 Qcoding_category
= intern ("coding-category");
7758 staticpro (&Qcoding_category
);
7759 Qcoding_category_index
= intern ("coding-category-index");
7760 staticpro (&Qcoding_category_index
);
7762 Vcoding_category_table
7763 = Fmake_vector (make_number (CODING_CATEGORY_IDX_MAX
), Qnil
);
7764 staticpro (&Vcoding_category_table
);
7767 for (i
= 0; i
< CODING_CATEGORY_IDX_MAX
; i
++)
7769 XVECTOR (Vcoding_category_table
)->contents
[i
]
7770 = intern (coding_category_name
[i
]);
7771 Fput (XVECTOR (Vcoding_category_table
)->contents
[i
],
7772 Qcoding_category_index
, make_number (i
));
7776 Vcoding_system_safe_chars
= Fcons (Qnil
, Qnil
);
7777 staticpro (&Vcoding_system_safe_chars
);
7779 Qtranslation_table
= intern ("translation-table");
7780 staticpro (&Qtranslation_table
);
7781 Fput (Qtranslation_table
, Qchar_table_extra_slots
, make_number (2));
7783 Qtranslation_table_id
= intern ("translation-table-id");
7784 staticpro (&Qtranslation_table_id
);
7786 Qtranslation_table_for_decode
= intern ("translation-table-for-decode");
7787 staticpro (&Qtranslation_table_for_decode
);
7789 Qtranslation_table_for_encode
= intern ("translation-table-for-encode");
7790 staticpro (&Qtranslation_table_for_encode
);
7792 Qsafe_chars
= intern ("safe-chars");
7793 staticpro (&Qsafe_chars
);
7795 Qchar_coding_system
= intern ("char-coding-system");
7796 staticpro (&Qchar_coding_system
);
7798 /* Intern this now in case it isn't already done.
7799 Setting this variable twice is harmless.
7800 But don't staticpro it here--that is done in alloc.c. */
7801 Qchar_table_extra_slots
= intern ("char-table-extra-slots");
7802 Fput (Qsafe_chars
, Qchar_table_extra_slots
, make_number (0));
7803 Fput (Qchar_coding_system
, Qchar_table_extra_slots
, make_number (0));
7805 Qvalid_codes
= intern ("valid-codes");
7806 staticpro (&Qvalid_codes
);
7808 Qemacs_mule
= intern ("emacs-mule");
7809 staticpro (&Qemacs_mule
);
7811 Qraw_text
= intern ("raw-text");
7812 staticpro (&Qraw_text
);
7814 Qutf_8
= intern ("utf-8");
7815 staticpro (&Qutf_8
);
7817 Qcoding_system_define_form
= intern ("coding-system-define-form");
7818 staticpro (&Qcoding_system_define_form
);
7820 defsubr (&Scoding_system_p
);
7821 defsubr (&Sread_coding_system
);
7822 defsubr (&Sread_non_nil_coding_system
);
7823 defsubr (&Scheck_coding_system
);
7824 defsubr (&Sdetect_coding_region
);
7825 defsubr (&Sdetect_coding_string
);
7826 defsubr (&Sfind_coding_systems_region_internal
);
7827 defsubr (&Sunencodable_char_position
);
7828 defsubr (&Sdecode_coding_region
);
7829 defsubr (&Sencode_coding_region
);
7830 defsubr (&Sdecode_coding_string
);
7831 defsubr (&Sencode_coding_string
);
7832 defsubr (&Sdecode_sjis_char
);
7833 defsubr (&Sencode_sjis_char
);
7834 defsubr (&Sdecode_big5_char
);
7835 defsubr (&Sencode_big5_char
);
7836 defsubr (&Sset_terminal_coding_system_internal
);
7837 defsubr (&Sset_safe_terminal_coding_system_internal
);
7838 defsubr (&Sterminal_coding_system
);
7839 defsubr (&Sset_keyboard_coding_system_internal
);
7840 defsubr (&Skeyboard_coding_system
);
7841 defsubr (&Sfind_operation_coding_system
);
7842 defsubr (&Supdate_coding_systems_internal
);
7843 defsubr (&Sset_coding_priority_internal
);
7844 defsubr (&Sdefine_coding_system_internal
);
7846 DEFVAR_LISP ("coding-system-list", &Vcoding_system_list
,
7847 doc
: /* List of coding systems.
7849 Do not alter the value of this variable manually. This variable should be
7850 updated by the functions `make-coding-system' and
7851 `define-coding-system-alias'. */);
7852 Vcoding_system_list
= Qnil
;
7854 DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist
,
7855 doc
: /* Alist of coding system names.
7856 Each element is one element list of coding system name.
7857 This variable is given to `completing-read' as TABLE argument.
7859 Do not alter the value of this variable manually. This variable should be
7860 updated by the functions `make-coding-system' and
7861 `define-coding-system-alias'. */);
7862 Vcoding_system_alist
= Qnil
;
7864 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list
,
7865 doc
: /* List of coding-categories (symbols) ordered by priority.
7867 On detecting a coding system, Emacs tries code detection algorithms
7868 associated with each coding-category one by one in this order. When
7869 one algorithm agrees with a byte sequence of source text, the coding
7870 system bound to the corresponding coding-category is selected.
7872 Don't modify this variable directly, but use `set-coding-priority'. */);
7876 Vcoding_category_list
= Qnil
;
7877 for (i
= CODING_CATEGORY_IDX_MAX
- 1; i
>= 0; i
--)
7878 Vcoding_category_list
7879 = Fcons (XVECTOR (Vcoding_category_table
)->contents
[i
],
7880 Vcoding_category_list
);
7883 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read
,
7884 doc
: /* Specify the coding system for read operations.
7885 It is useful to bind this variable with `let', but do not set it globally.
7886 If the value is a coding system, it is used for decoding on read operation.
7887 If not, an appropriate element is used from one of the coding system alists:
7888 There are three such tables, `file-coding-system-alist',
7889 `process-coding-system-alist', and `network-coding-system-alist'. */);
7890 Vcoding_system_for_read
= Qnil
;
7892 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write
,
7893 doc
: /* Specify the coding system for write operations.
7894 Programs bind this variable with `let', but you should not set it globally.
7895 If the value is a coding system, it is used for encoding of output,
7896 when writing it to a file and when sending it to a file or subprocess.
7898 If this does not specify a coding system, an appropriate element
7899 is used from one of the coding system alists:
7900 There are three such tables, `file-coding-system-alist',
7901 `process-coding-system-alist', and `network-coding-system-alist'.
7902 For output to files, if the above procedure does not specify a coding system,
7903 the value of `buffer-file-coding-system' is used. */);
7904 Vcoding_system_for_write
= Qnil
;
7906 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used
,
7907 doc
: /* Coding system used in the latest file or process I/O.
7908 Also set by `encode-coding-region', `decode-coding-region',
7909 `encode-coding-string' and `decode-coding-string'. */);
7910 Vlast_coding_system_used
= Qnil
;
7912 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion
,
7913 doc
: /* *Non-nil means always inhibit code conversion of end-of-line format.
7914 See info node `Coding Systems' and info node `Text and Binary' concerning
7915 such conversion. */);
7916 inhibit_eol_conversion
= 0;
7918 DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system
,
7919 doc
: /* Non-nil means process buffer inherits coding system of process output.
7920 Bind it to t if the process output is to be treated as if it were a file
7921 read from some filesystem. */);
7922 inherit_process_coding_system
= 0;
7924 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist
,
7925 doc
: /* Alist to decide a coding system to use for a file I/O operation.
7926 The format is ((PATTERN . VAL) ...),
7927 where PATTERN is a regular expression matching a file name,
7928 VAL is a coding system, a cons of coding systems, or a function symbol.
7929 If VAL is a coding system, it is used for both decoding and encoding
7931 If VAL is a cons of coding systems, the car part is used for decoding,
7932 and the cdr part is used for encoding.
7933 If VAL is a function symbol, the function must return a coding system
7934 or a cons of coding systems which are used as above. The function gets
7935 the arguments with which `find-operation-coding-system' was called.
7937 See also the function `find-operation-coding-system'
7938 and the variable `auto-coding-alist'. */);
7939 Vfile_coding_system_alist
= Qnil
;
7941 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist
,
7942 doc
: /* Alist to decide a coding system to use for a process I/O operation.
7943 The format is ((PATTERN . VAL) ...),
7944 where PATTERN is a regular expression matching a program name,
7945 VAL is a coding system, a cons of coding systems, or a function symbol.
7946 If VAL is a coding system, it is used for both decoding what received
7947 from the program and encoding what sent to the program.
7948 If VAL is a cons of coding systems, the car part is used for decoding,
7949 and the cdr part is used for encoding.
7950 If VAL is a function symbol, the function must return a coding system
7951 or a cons of coding systems which are used as above.
7953 See also the function `find-operation-coding-system'. */);
7954 Vprocess_coding_system_alist
= Qnil
;
7956 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist
,
7957 doc
: /* Alist to decide a coding system to use for a network I/O operation.
7958 The format is ((PATTERN . VAL) ...),
7959 where PATTERN is a regular expression matching a network service name
7960 or is a port number to connect to,
7961 VAL is a coding system, a cons of coding systems, or a function symbol.
7962 If VAL is a coding system, it is used for both decoding what received
7963 from the network stream and encoding what sent to the network stream.
7964 If VAL is a cons of coding systems, the car part is used for decoding,
7965 and the cdr part is used for encoding.
7966 If VAL is a function symbol, the function must return a coding system
7967 or a cons of coding systems which are used as above.
7969 See also the function `find-operation-coding-system'. */);
7970 Vnetwork_coding_system_alist
= Qnil
;
7972 DEFVAR_LISP ("locale-coding-system", &Vlocale_coding_system
,
7973 doc
: /* Coding system to use with system messages.
7974 Also used for decoding keyboard input on X Window system. */);
7975 Vlocale_coding_system
= Qnil
;
7977 /* The eol mnemonics are reset in startup.el system-dependently. */
7978 DEFVAR_LISP ("eol-mnemonic-unix", &eol_mnemonic_unix
,
7979 doc
: /* *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
7980 eol_mnemonic_unix
= build_string (":");
7982 DEFVAR_LISP ("eol-mnemonic-dos", &eol_mnemonic_dos
,
7983 doc
: /* *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
7984 eol_mnemonic_dos
= build_string ("\\");
7986 DEFVAR_LISP ("eol-mnemonic-mac", &eol_mnemonic_mac
,
7987 doc
: /* *String displayed in mode line for MAC-like (CR) end-of-line format. */);
7988 eol_mnemonic_mac
= build_string ("/");
7990 DEFVAR_LISP ("eol-mnemonic-undecided", &eol_mnemonic_undecided
,
7991 doc
: /* *String displayed in mode line when end-of-line format is not yet determined. */);
7992 eol_mnemonic_undecided
= build_string (":");
7994 DEFVAR_LISP ("enable-character-translation", &Venable_character_translation
,
7995 doc
: /* *Non-nil enables character translation while encoding and decoding. */);
7996 Venable_character_translation
= Qt
;
7998 DEFVAR_LISP ("standard-translation-table-for-decode",
7999 &Vstandard_translation_table_for_decode
,
8000 doc
: /* Table for translating characters while decoding. */);
8001 Vstandard_translation_table_for_decode
= Qnil
;
8003 DEFVAR_LISP ("standard-translation-table-for-encode",
8004 &Vstandard_translation_table_for_encode
,
8005 doc
: /* Table for translating characters while encoding. */);
8006 Vstandard_translation_table_for_encode
= Qnil
;
8008 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_alist
,
8009 doc
: /* Alist of charsets vs revision numbers.
8010 While encoding, if a charset (car part of an element) is found,
8011 designate it with the escape sequence identifying revision (cdr part of the element). */);
8012 Vcharset_revision_alist
= Qnil
;
8014 DEFVAR_LISP ("default-process-coding-system",
8015 &Vdefault_process_coding_system
,
8016 doc
: /* Cons of coding systems used for process I/O by default.
8017 The car part is used for decoding a process output,
8018 the cdr part is used for encoding a text to be sent to a process. */);
8019 Vdefault_process_coding_system
= Qnil
;
8021 DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table
,
8022 doc
: /* Table of extra Latin codes in the range 128..159 (inclusive).
8023 This is a vector of length 256.
8024 If Nth element is non-nil, the existence of code N in a file
8025 \(or output of subprocess) doesn't prevent it to be detected as
8026 a coding system of ISO 2022 variant which has a flag
8027 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
8028 or reading output of a subprocess.
8029 Only 128th through 159th elements has a meaning. */);
8030 Vlatin_extra_code_table
= Fmake_vector (make_number (256), Qnil
);
8032 DEFVAR_LISP ("select-safe-coding-system-function",
8033 &Vselect_safe_coding_system_function
,
8034 doc
: /* Function to call to select safe coding system for encoding a text.
8036 If set, this function is called to force a user to select a proper
8037 coding system which can encode the text in the case that a default
8038 coding system used in each operation can't encode the text.
8040 The default value is `select-safe-coding-system' (which see). */);
8041 Vselect_safe_coding_system_function
= Qnil
;
8043 DEFVAR_BOOL ("coding-system-require-warning",
8044 &coding_system_require_warning
,
8045 doc
: /* Internal use only.
8046 If non-nil, on writing a file, `select-safe-coding-system-function' is
8047 called even if `coding-system-for-write' is non-nil. The command
8048 `universal-coding-system-argument' binds this variable to t temporarily. */);
8049 coding_system_require_warning
= 0;
8052 DEFVAR_BOOL ("inhibit-iso-escape-detection",
8053 &inhibit_iso_escape_detection
,
8054 doc
: /* If non-nil, Emacs ignores ISO2022's escape sequence on code detection.
8056 By default, on reading a file, Emacs tries to detect how the text is
8057 encoded. This code detection is sensitive to escape sequences. If
8058 the sequence is valid as ISO2022, the code is determined as one of
8059 the ISO2022 encodings, and the file is decoded by the corresponding
8060 coding system (e.g. `iso-2022-7bit').
8062 However, there may be a case that you want to read escape sequences in
8063 a file as is. In such a case, you can set this variable to non-nil.
8064 Then, as the code detection ignores any escape sequences, no file is
8065 detected as encoded in some ISO2022 encoding. The result is that all
8066 escape sequences become visible in a buffer.
8068 The default value is nil, and it is strongly recommended not to change
8069 it. That is because many Emacs Lisp source files that contain
8070 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
8071 in Emacs's distribution, and they won't be decoded correctly on
8072 reading if you suppress escape sequence detection.
8074 The other way to read escape sequences in a file without decoding is
8075 to explicitly specify some coding system that doesn't use ISO2022's
8076 escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
8077 inhibit_iso_escape_detection
= 0;
8079 DEFVAR_LISP ("translation-table-for-input", &Vtranslation_table_for_input
,
8080 doc
: /* Char table for translating self-inserting characters.
8081 This is applied to the result of input methods, not their input. See also
8082 `keyboard-translate-table'. */);
8083 Vtranslation_table_for_input
= Qnil
;
8087 emacs_strerror (error_number
)
8092 synchronize_system_messages_locale ();
8093 str
= strerror (error_number
);
8095 if (! NILP (Vlocale_coding_system
))
8097 Lisp_Object dec
= code_convert_string_norecord (build_string (str
),
8098 Vlocale_coding_system
,
8100 str
= (char *) SDATA (dec
);
8108 /* arch-tag: 3a3a2b01-5ff6-4071-9afe-f5b808d9229d
8109 (do not change this comment) */