(Fcall_process): If code detection is necessary, call detect_coding
[bpt/emacs.git] / src / coding.c
CommitLineData
4ed46869 1/* Coding system handler (conversion, detection, and etc).
ff955d90 2 Copyright (C) 1995, 1997, 1998, 2002 Electrotechnical Laboratory, JAPAN.
203cb916 3 Licensed to the Free Software Foundation.
70ad9fc4 4 Copyright (C) 2001 Free Software Foundation, Inc.
4ed46869 5
369314dc
KH
6This file is part of GNU Emacs.
7
8GNU Emacs is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
4ed46869 12
369314dc
KH
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
4ed46869 17
369314dc
KH
18You should have received a copy of the GNU General Public License
19along with GNU Emacs; see the file COPYING. If not, write to
20the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA. */
4ed46869
KH
22
23/*** TABLE OF CONTENTS ***
24
b73bfc1c 25 0. General comments
4ed46869 26 1. Preamble
0ef69138 27 2. Emacs' internal format (emacs-mule) handlers
4ed46869
KH
28 3. ISO2022 handlers
29 4. Shift-JIS and BIG5 handlers
1397dc18
KH
30 5. CCL handlers
31 6. End-of-line handlers
32 7. C library functions
33 8. Emacs Lisp library functions
34 9. Post-amble
4ed46869
KH
35
36*/
37
b73bfc1c
KH
38/*** 0. General comments ***/
39
40
cfb43547 41/*** GENERAL NOTE on CODING SYSTEMS ***
4ed46869 42
cfb43547 43 A coding system is an encoding mechanism for one or more character
4ed46869
KH
44 sets. Here's a list of coding systems which Emacs can handle. When
45 we say "decode", it means converting some other coding system to
cfb43547 46 Emacs' internal format (emacs-mule), and when we say "encode",
0ef69138
KH
47 it means converting the coding system emacs-mule to some other
48 coding system.
4ed46869 49
0ef69138 50 0. Emacs' internal format (emacs-mule)
4ed46869 51
cfb43547 52 Emacs itself holds a multi-lingual character in buffers and strings
f4dee582 53 in a special format. Details are described in section 2.
4ed46869
KH
54
55 1. ISO2022
56
57 The most famous coding system for multiple character sets. X's
f4dee582
RS
58 Compound Text, various EUCs (Extended Unix Code), and coding
59 systems used in Internet communication such as ISO-2022-JP are
60 all variants of ISO2022. Details are described in section 3.
4ed46869
KH
61
62 2. SJIS (or Shift-JIS or MS-Kanji-Code)
93dec019 63
4ed46869
KH
64 A coding system to encode character sets: ASCII, JISX0201, and
65 JISX0208. Widely used for PC's in Japan. Details are described in
f4dee582 66 section 4.
4ed46869
KH
67
68 3. BIG5
69
cfb43547
DL
70 A coding system to encode the character sets ASCII and Big5. Widely
71 used for Chinese (mainly in Taiwan and Hong Kong). Details are
f4dee582
RS
72 described in section 4. In this file, when we write "BIG5"
73 (all uppercase), we mean the coding system, and when we write
74 "Big5" (capitalized), we mean the character set.
4ed46869 75
27901516
KH
76 4. Raw text
77
cfb43547
DL
78 A coding system for text containing random 8-bit code. Emacs does
79 no code conversion on such text except for end-of-line format.
27901516
KH
80
81 5. Other
4ed46869 82
cfb43547
DL
83 If a user wants to read/write text encoded in a coding system not
84 listed above, he can supply a decoder and an encoder for it as CCL
4ed46869
KH
85 (Code Conversion Language) programs. Emacs executes the CCL program
86 while reading/writing.
87
d46c5b12
KH
88 Emacs represents a coding system by a Lisp symbol that has a property
89 `coding-system'. But, before actually using the coding system, the
4ed46869 90 information about it is set in a structure of type `struct
f4dee582 91 coding_system' for rapid processing. See section 6 for more details.
4ed46869
KH
92
93*/
94
95/*** GENERAL NOTES on END-OF-LINE FORMAT ***
96
cfb43547
DL
97 How end-of-line of text is encoded depends on the operating system.
98 For instance, Unix's format is just one byte of `line-feed' code,
f4dee582 99 whereas DOS's format is two-byte sequence of `carriage-return' and
d46c5b12
KH
100 `line-feed' codes. MacOS's format is usually one byte of
101 `carriage-return'.
4ed46869 102
cfb43547
DL
103 Since text character encoding and end-of-line encoding are
104 independent, any coding system described above can have any
105 end-of-line format. So Emacs has information about end-of-line
106 format in each coding-system. See section 6 for more details.
4ed46869
KH
107
108*/
109
110/*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
111
112 These functions check if a text between SRC and SRC_END is encoded
113 in the coding system category XXX. Each returns an integer value in
cfb43547 114 which appropriate flag bits for the category XXX are set. The flag
4ed46869 115 bits are defined in macros CODING_CATEGORY_MASK_XXX. Below is the
cfb43547 116 template for these functions. If MULTIBYTEP is nonzero, 8-bit codes
0a28aafb 117 of the range 0x80..0x9F are in multibyte form. */
4ed46869
KH
118#if 0
119int
0a28aafb 120detect_coding_emacs_mule (src, src_end, multibytep)
4ed46869 121 unsigned char *src, *src_end;
0a28aafb 122 int multibytep;
4ed46869
KH
123{
124 ...
125}
126#endif
127
128/*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
129
b73bfc1c
KH
130 These functions decode SRC_BYTES length of unibyte text at SOURCE
131 encoded in CODING to Emacs' internal format. The resulting
132 multibyte text goes to a place pointed to by DESTINATION, the length
133 of which should not exceed DST_BYTES.
d46c5b12 134
cfb43547
DL
135 These functions set the information about original and decoded texts
136 in the members `produced', `produced_char', `consumed', and
137 `consumed_char' of the structure *CODING. They also set the member
138 `result' to one of CODING_FINISH_XXX indicating how the decoding
139 finished.
d46c5b12 140
cfb43547 141 DST_BYTES zero means that the source area and destination area are
d46c5b12 142 overlapped, which means that we can produce a decoded text until it
cfb43547 143 reaches the head of the not-yet-decoded source text.
d46c5b12 144
cfb43547 145 Below is a template for these functions. */
4ed46869 146#if 0
b73bfc1c 147static void
d46c5b12 148decode_coding_XXX (coding, source, destination, src_bytes, dst_bytes)
4ed46869
KH
149 struct coding_system *coding;
150 unsigned char *source, *destination;
151 int src_bytes, dst_bytes;
4ed46869
KH
152{
153 ...
154}
155#endif
156
157/*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
158
cfb43547 159 These functions encode SRC_BYTES length text at SOURCE from Emacs'
b73bfc1c
KH
160 internal multibyte format to CODING. The resulting unibyte text
161 goes to a place pointed to by DESTINATION, the length of which
162 should not exceed DST_BYTES.
d46c5b12 163
cfb43547
DL
164 These functions set the information about original and encoded texts
165 in the members `produced', `produced_char', `consumed', and
166 `consumed_char' of the structure *CODING. They also set the member
167 `result' to one of CODING_FINISH_XXX indicating how the encoding
168 finished.
d46c5b12 169
cfb43547
DL
170 DST_BYTES zero means that the source area and destination area are
171 overlapped, which means that we can produce encoded text until it
172 reaches at the head of the not-yet-encoded source text.
d46c5b12 173
cfb43547 174 Below is a template for these functions. */
4ed46869 175#if 0
b73bfc1c 176static void
d46c5b12 177encode_coding_XXX (coding, source, destination, src_bytes, dst_bytes)
4ed46869
KH
178 struct coding_system *coding;
179 unsigned char *source, *destination;
180 int src_bytes, dst_bytes;
4ed46869
KH
181{
182 ...
183}
184#endif
185
186/*** COMMONLY USED MACROS ***/
187
b73bfc1c
KH
188/* The following two macros ONE_MORE_BYTE and TWO_MORE_BYTES safely
189 get one, two, and three bytes from the source text respectively.
190 If there are not enough bytes in the source, they jump to
191 `label_end_of_loop'. The caller should set variables `coding',
192 `src' and `src_end' to appropriate pointer in advance. These
193 macros are called from decoding routines `decode_coding_XXX', thus
194 it is assumed that the source text is unibyte. */
4ed46869 195
b73bfc1c
KH
196#define ONE_MORE_BYTE(c1) \
197 do { \
198 if (src >= src_end) \
199 { \
200 coding->result = CODING_FINISH_INSUFFICIENT_SRC; \
201 goto label_end_of_loop; \
202 } \
203 c1 = *src++; \
4ed46869
KH
204 } while (0)
205
b73bfc1c
KH
206#define TWO_MORE_BYTES(c1, c2) \
207 do { \
208 if (src + 1 >= src_end) \
209 { \
210 coding->result = CODING_FINISH_INSUFFICIENT_SRC; \
211 goto label_end_of_loop; \
212 } \
213 c1 = *src++; \
214 c2 = *src++; \
4ed46869
KH
215 } while (0)
216
4ed46869 217
0a28aafb
KH
218/* Like ONE_MORE_BYTE, but 8-bit bytes of data at SRC are in multibyte
219 form if MULTIBYTEP is nonzero. */
220
221#define ONE_MORE_BYTE_CHECK_MULTIBYTE(c1, multibytep) \
222 do { \
223 if (src >= src_end) \
224 { \
225 coding->result = CODING_FINISH_INSUFFICIENT_SRC; \
226 goto label_end_of_loop; \
227 } \
228 c1 = *src++; \
229 if (multibytep && c1 == LEADING_CODE_8_BIT_CONTROL) \
230 c1 = *src++ - 0x20; \
231 } while (0)
232
b73bfc1c
KH
233/* Set C to the next character at the source text pointed by `src'.
234 If there are not enough characters in the source, jump to
235 `label_end_of_loop'. The caller should set variables `coding'
236 `src', `src_end', and `translation_table' to appropriate pointers
237 in advance. This macro is used in encoding routines
238 `encode_coding_XXX', thus it assumes that the source text is in
239 multibyte form except for 8-bit characters. 8-bit characters are
240 in multibyte form if coding->src_multibyte is nonzero, else they
241 are represented by a single byte. */
4ed46869 242
b73bfc1c
KH
243#define ONE_MORE_CHAR(c) \
244 do { \
245 int len = src_end - src; \
246 int bytes; \
247 if (len <= 0) \
248 { \
249 coding->result = CODING_FINISH_INSUFFICIENT_SRC; \
250 goto label_end_of_loop; \
251 } \
252 if (coding->src_multibyte \
253 || UNIBYTE_STR_AS_MULTIBYTE_P (src, len, bytes)) \
254 c = STRING_CHAR_AND_LENGTH (src, len, bytes); \
255 else \
256 c = *src, bytes = 1; \
257 if (!NILP (translation_table)) \
39658efc 258 c = translate_char (translation_table, c, -1, 0, 0); \
b73bfc1c 259 src += bytes; \
4ed46869
KH
260 } while (0)
261
4ed46869 262
8ca3766a 263/* Produce a multibyte form of character C to `dst'. Jump to
b73bfc1c
KH
264 `label_end_of_loop' if there's not enough space at `dst'.
265
cfb43547 266 If we are now in the middle of a composition sequence, the decoded
b73bfc1c
KH
267 character may be ALTCHAR (for the current composition). In that
268 case, the character goes to coding->cmp_data->data instead of
269 `dst'.
270
271 This macro is used in decoding routines. */
272
273#define EMIT_CHAR(c) \
4ed46869 274 do { \
b73bfc1c
KH
275 if (! COMPOSING_P (coding) \
276 || coding->composing == COMPOSITION_RELATIVE \
277 || coding->composing == COMPOSITION_WITH_RULE) \
278 { \
279 int bytes = CHAR_BYTES (c); \
280 if ((dst + bytes) > (dst_bytes ? dst_end : src)) \
281 { \
282 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
283 goto label_end_of_loop; \
284 } \
285 dst += CHAR_STRING (c, dst); \
286 coding->produced_char++; \
287 } \
ec6d2bb8 288 \
b73bfc1c
KH
289 if (COMPOSING_P (coding) \
290 && coding->composing != COMPOSITION_RELATIVE) \
291 { \
292 CODING_ADD_COMPOSITION_COMPONENT (coding, c); \
293 coding->composition_rule_follows \
294 = coding->composing != COMPOSITION_WITH_ALTCHARS; \
295 } \
4ed46869
KH
296 } while (0)
297
4ed46869 298
b73bfc1c
KH
299#define EMIT_ONE_BYTE(c) \
300 do { \
301 if (dst >= (dst_bytes ? dst_end : src)) \
302 { \
303 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
304 goto label_end_of_loop; \
305 } \
306 *dst++ = c; \
307 } while (0)
308
309#define EMIT_TWO_BYTES(c1, c2) \
310 do { \
311 if (dst + 2 > (dst_bytes ? dst_end : src)) \
312 { \
313 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
314 goto label_end_of_loop; \
315 } \
316 *dst++ = c1, *dst++ = c2; \
317 } while (0)
318
319#define EMIT_BYTES(from, to) \
320 do { \
321 if (dst + (to - from) > (dst_bytes ? dst_end : src)) \
322 { \
323 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
324 goto label_end_of_loop; \
325 } \
326 while (from < to) \
327 *dst++ = *from++; \
4ed46869
KH
328 } while (0)
329
330\f
331/*** 1. Preamble ***/
332
68c45bf0
PE
333#ifdef emacs
334#include <config.h>
335#endif
336
4ed46869
KH
337#include <stdio.h>
338
339#ifdef emacs
340
4ed46869
KH
341#include "lisp.h"
342#include "buffer.h"
343#include "charset.h"
ec6d2bb8 344#include "composite.h"
4ed46869
KH
345#include "ccl.h"
346#include "coding.h"
347#include "window.h"
348
349#else /* not emacs */
350
351#include "mulelib.h"
352
353#endif /* not emacs */
354
355Lisp_Object Qcoding_system, Qeol_type;
356Lisp_Object Qbuffer_file_coding_system;
357Lisp_Object Qpost_read_conversion, Qpre_write_conversion;
27901516 358Lisp_Object Qno_conversion, Qundecided;
bb0115a2 359Lisp_Object Qcoding_system_history;
05e6f5dc 360Lisp_Object Qsafe_chars;
1397dc18 361Lisp_Object Qvalid_codes;
4ed46869
KH
362
363extern Lisp_Object Qinsert_file_contents, Qwrite_region;
364Lisp_Object Qcall_process, Qcall_process_region, Qprocess_argument;
365Lisp_Object Qstart_process, Qopen_network_stream;
366Lisp_Object Qtarget_idx;
367
d46c5b12
KH
368Lisp_Object Vselect_safe_coding_system_function;
369
7722baf9
EZ
370/* Mnemonic string for each format of end-of-line. */
371Lisp_Object eol_mnemonic_unix, eol_mnemonic_dos, eol_mnemonic_mac;
372/* Mnemonic string to indicate format of end-of-line is not yet
4ed46869 373 decided. */
7722baf9 374Lisp_Object eol_mnemonic_undecided;
4ed46869 375
9ce27fde
KH
376/* Format of end-of-line decided by system. This is CODING_EOL_LF on
377 Unix, CODING_EOL_CRLF on DOS/Windows, and CODING_EOL_CR on Mac. */
378int system_eol_type;
379
4ed46869
KH
380#ifdef emacs
381
4608c386
KH
382Lisp_Object Vcoding_system_list, Vcoding_system_alist;
383
384Lisp_Object Qcoding_system_p, Qcoding_system_error;
4ed46869 385
d46c5b12
KH
386/* Coding system emacs-mule and raw-text are for converting only
387 end-of-line format. */
388Lisp_Object Qemacs_mule, Qraw_text;
9ce27fde 389
4ed46869
KH
390/* Coding-systems are handed between Emacs Lisp programs and C internal
391 routines by the following three variables. */
392/* Coding-system for reading files and receiving data from process. */
393Lisp_Object Vcoding_system_for_read;
394/* Coding-system for writing files and sending data to process. */
395Lisp_Object Vcoding_system_for_write;
396/* Coding-system actually used in the latest I/O. */
397Lisp_Object Vlast_coding_system_used;
398
c4825358 399/* A vector of length 256 which contains information about special
94487c4e 400 Latin codes (especially for dealing with Microsoft codes). */
3f003981 401Lisp_Object Vlatin_extra_code_table;
c4825358 402
9ce27fde
KH
403/* Flag to inhibit code conversion of end-of-line format. */
404int inhibit_eol_conversion;
405
74383408
KH
406/* Flag to inhibit ISO2022 escape sequence detection. */
407int inhibit_iso_escape_detection;
408
ed29121d
EZ
409/* Flag to make buffer-file-coding-system inherit from process-coding. */
410int inherit_process_coding_system;
411
c4825358 412/* Coding system to be used to encode text for terminal display. */
4ed46869
KH
413struct coding_system terminal_coding;
414
c4825358
KH
415/* Coding system to be used to encode text for terminal display when
416 terminal coding system is nil. */
417struct coding_system safe_terminal_coding;
418
419/* Coding system of what is sent from terminal keyboard. */
4ed46869
KH
420struct coding_system keyboard_coding;
421
6bc51348
KH
422/* Default coding system to be used to write a file. */
423struct coding_system default_buffer_file_coding;
424
02ba4723
KH
425Lisp_Object Vfile_coding_system_alist;
426Lisp_Object Vprocess_coding_system_alist;
427Lisp_Object Vnetwork_coding_system_alist;
4ed46869 428
68c45bf0
PE
429Lisp_Object Vlocale_coding_system;
430
4ed46869
KH
431#endif /* emacs */
432
d46c5b12 433Lisp_Object Qcoding_category, Qcoding_category_index;
4ed46869
KH
434
435/* List of symbols `coding-category-xxx' ordered by priority. */
436Lisp_Object Vcoding_category_list;
437
d46c5b12
KH
438/* Table of coding categories (Lisp symbols). */
439Lisp_Object Vcoding_category_table;
4ed46869
KH
440
441/* Table of names of symbol for each coding-category. */
442char *coding_category_name[CODING_CATEGORY_IDX_MAX] = {
0ef69138 443 "coding-category-emacs-mule",
4ed46869
KH
444 "coding-category-sjis",
445 "coding-category-iso-7",
d46c5b12 446 "coding-category-iso-7-tight",
4ed46869
KH
447 "coding-category-iso-8-1",
448 "coding-category-iso-8-2",
7717c392
KH
449 "coding-category-iso-7-else",
450 "coding-category-iso-8-else",
89fa8b36 451 "coding-category-ccl",
4ed46869 452 "coding-category-big5",
fa42c37f
KH
453 "coding-category-utf-8",
454 "coding-category-utf-16-be",
455 "coding-category-utf-16-le",
27901516 456 "coding-category-raw-text",
89fa8b36 457 "coding-category-binary"
4ed46869
KH
458};
459
66cfb530 460/* Table of pointers to coding systems corresponding to each coding
d46c5b12
KH
461 categories. */
462struct coding_system *coding_system_table[CODING_CATEGORY_IDX_MAX];
463
66cfb530 464/* Table of coding category masks. Nth element is a mask for a coding
8ca3766a 465 category of which priority is Nth. */
66cfb530
KH
466static
467int coding_priorities[CODING_CATEGORY_IDX_MAX];
468
f967223b
KH
469/* Flag to tell if we look up translation table on character code
470 conversion. */
84fbb8a0 471Lisp_Object Venable_character_translation;
f967223b
KH
472/* Standard translation table to look up on decoding (reading). */
473Lisp_Object Vstandard_translation_table_for_decode;
474/* Standard translation table to look up on encoding (writing). */
475Lisp_Object Vstandard_translation_table_for_encode;
84fbb8a0 476
f967223b
KH
477Lisp_Object Qtranslation_table;
478Lisp_Object Qtranslation_table_id;
479Lisp_Object Qtranslation_table_for_decode;
480Lisp_Object Qtranslation_table_for_encode;
4ed46869
KH
481
482/* Alist of charsets vs revision number. */
483Lisp_Object Vcharset_revision_alist;
484
02ba4723
KH
485/* Default coding systems used for process I/O. */
486Lisp_Object Vdefault_process_coding_system;
487
b843d1ae
KH
488/* Global flag to tell that we can't call post-read-conversion and
489 pre-write-conversion functions. Usually the value is zero, but it
490 is set to 1 temporarily while such functions are running. This is
491 to avoid infinite recursive call. */
492static int inhibit_pre_post_conversion;
493
05e6f5dc
KH
494/* Char-table containing safe coding systems of each character. */
495Lisp_Object Vchar_coding_system_table;
496Lisp_Object Qchar_coding_system;
497
498/* Return `safe-chars' property of coding system CODING. Don't check
499 validity of CODING. */
500
501Lisp_Object
502coding_safe_chars (coding)
503 struct coding_system *coding;
504{
505 Lisp_Object coding_spec, plist, safe_chars;
93dec019 506
05e6f5dc
KH
507 coding_spec = Fget (coding->symbol, Qcoding_system);
508 plist = XVECTOR (coding_spec)->contents[3];
509 safe_chars = Fplist_get (XVECTOR (coding_spec)->contents[3], Qsafe_chars);
510 return (CHAR_TABLE_P (safe_chars) ? safe_chars : Qt);
511}
512
513#define CODING_SAFE_CHAR_P(safe_chars, c) \
514 (EQ (safe_chars, Qt) || !NILP (CHAR_TABLE_REF (safe_chars, c)))
515
4ed46869 516\f
0ef69138 517/*** 2. Emacs internal format (emacs-mule) handlers ***/
4ed46869 518
aa72b389
KH
519/* Emacs' internal format for representation of multiple character
520 sets is a kind of multi-byte encoding, i.e. characters are
521 represented by variable-length sequences of one-byte codes.
b73bfc1c
KH
522
523 ASCII characters and control characters (e.g. `tab', `newline') are
524 represented by one-byte sequences which are their ASCII codes, in
525 the range 0x00 through 0x7F.
526
527 8-bit characters of the range 0x80..0x9F are represented by
528 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
529 code + 0x20).
530
531 8-bit characters of the range 0xA0..0xFF are represented by
532 one-byte sequences which are their 8-bit code.
533
534 The other characters are represented by a sequence of `base
535 leading-code', optional `extended leading-code', and one or two
536 `position-code's. The length of the sequence is determined by the
aa72b389 537 base leading-code. Leading-code takes the range 0x81 through 0x9D,
b73bfc1c
KH
538 whereas extended leading-code and position-code take the range 0xA0
539 through 0xFF. See `charset.h' for more details about leading-code
540 and position-code.
f4dee582 541
4ed46869 542 --- CODE RANGE of Emacs' internal format ---
b73bfc1c
KH
543 character set range
544 ------------- -----
545 ascii 0x00..0x7F
546 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
547 eight-bit-graphic 0xA0..0xBF
aa72b389 548 ELSE 0x81..0x9D + [0xA0..0xFF]+
4ed46869
KH
549 ---------------------------------------------
550
aa72b389
KH
551 As this is the internal character representation, the format is
552 usually not used externally (i.e. in a file or in a data sent to a
553 process). But, it is possible to have a text externally in this
554 format (i.e. by encoding by the coding system `emacs-mule').
555
556 In that case, a sequence of one-byte codes has a slightly different
557 form.
558
ae5145c2 559 Firstly, all characters in eight-bit-control are represented by
aa72b389
KH
560 one-byte sequences which are their 8-bit code.
561
562 Next, character composition data are represented by the byte
563 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
564 where,
565 METHOD is 0xF0 plus one of composition method (enum
566 composition_method),
567
ae5145c2 568 BYTES is 0xA0 plus the byte length of these composition data,
aa72b389 569
ae5145c2 570 CHARS is 0xA0 plus the number of characters composed by these
aa72b389
KH
571 data,
572
8ca3766a 573 COMPONENTs are characters of multibyte form or composition
aa72b389
KH
574 rules encoded by two-byte of ASCII codes.
575
576 In addition, for backward compatibility, the following formats are
577 also recognized as composition data on decoding.
578
579 0x80 MSEQ ...
580 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
581
582 Here,
583 MSEQ is a multibyte form but in these special format:
584 ASCII: 0xA0 ASCII_CODE+0x80,
585 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
586 RULE is a one byte code of the range 0xA0..0xF0 that
587 represents a composition rule.
4ed46869
KH
588 */
589
590enum emacs_code_class_type emacs_code_class[256];
591
4ed46869
KH
592/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
593 Check if a text is encoded in Emacs' internal format. If it is,
d46c5b12 594 return CODING_CATEGORY_MASK_EMACS_MULE, else return 0. */
4ed46869 595
0a28aafb
KH
596static int
597detect_coding_emacs_mule (src, src_end, multibytep)
b73bfc1c 598 unsigned char *src, *src_end;
0a28aafb 599 int multibytep;
4ed46869
KH
600{
601 unsigned char c;
602 int composing = 0;
b73bfc1c
KH
603 /* Dummy for ONE_MORE_BYTE. */
604 struct coding_system dummy_coding;
605 struct coding_system *coding = &dummy_coding;
4ed46869 606
b73bfc1c 607 while (1)
4ed46869 608 {
0a28aafb 609 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
4ed46869
KH
610
611 if (composing)
612 {
613 if (c < 0xA0)
614 composing = 0;
b73bfc1c
KH
615 else if (c == 0xA0)
616 {
0a28aafb 617 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
b73bfc1c
KH
618 c &= 0x7F;
619 }
4ed46869
KH
620 else
621 c -= 0x20;
622 }
623
b73bfc1c 624 if (c < 0x20)
4ed46869 625 {
4ed46869
KH
626 if (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)
627 return 0;
b73bfc1c
KH
628 }
629 else if (c >= 0x80 && c < 0xA0)
630 {
631 if (c == 0x80)
632 /* Old leading code for a composite character. */
633 composing = 1;
634 else
635 {
636 unsigned char *src_base = src - 1;
637 int bytes;
4ed46869 638
b73bfc1c
KH
639 if (!UNIBYTE_STR_AS_MULTIBYTE_P (src_base, src_end - src_base,
640 bytes))
641 return 0;
642 src = src_base + bytes;
643 }
644 }
645 }
646 label_end_of_loop:
647 return CODING_CATEGORY_MASK_EMACS_MULE;
648}
4ed46869 649
4ed46869 650
aa72b389
KH
651/* Record the starting position START and METHOD of one composition. */
652
653#define CODING_ADD_COMPOSITION_START(coding, start, method) \
654 do { \
655 struct composition_data *cmp_data = coding->cmp_data; \
656 int *data = cmp_data->data + cmp_data->used; \
657 coding->cmp_data_start = cmp_data->used; \
658 data[0] = -1; \
659 data[1] = cmp_data->char_offset + start; \
660 data[3] = (int) method; \
661 cmp_data->used += 4; \
662 } while (0)
663
664/* Record the ending position END of the current composition. */
665
666#define CODING_ADD_COMPOSITION_END(coding, end) \
667 do { \
668 struct composition_data *cmp_data = coding->cmp_data; \
669 int *data = cmp_data->data + coding->cmp_data_start; \
670 data[0] = cmp_data->used - coding->cmp_data_start; \
671 data[2] = cmp_data->char_offset + end; \
672 } while (0)
673
674/* Record one COMPONENT (alternate character or composition rule). */
675
676#define CODING_ADD_COMPOSITION_COMPONENT(coding, component) \
677 (coding->cmp_data->data[coding->cmp_data->used++] = component)
678
679
680/* Get one byte from a data pointed by SRC and increment SRC. If SRC
8ca3766a 681 is not less than SRC_END, return -1 without incrementing Src. */
aa72b389
KH
682
683#define SAFE_ONE_MORE_BYTE() (src >= src_end ? -1 : *src++)
684
685
686/* Decode a character represented as a component of composition
687 sequence of Emacs 20 style at SRC. Set C to that character, store
688 its multibyte form sequence at P, and set P to the end of that
689 sequence. If no valid character is found, set C to -1. */
690
691#define DECODE_EMACS_MULE_COMPOSITION_CHAR(c, p) \
692 do { \
693 int bytes; \
694 \
695 c = SAFE_ONE_MORE_BYTE (); \
696 if (c < 0) \
697 break; \
698 if (CHAR_HEAD_P (c)) \
699 c = -1; \
700 else if (c == 0xA0) \
701 { \
702 c = SAFE_ONE_MORE_BYTE (); \
703 if (c < 0xA0) \
704 c = -1; \
705 else \
706 { \
707 c -= 0xA0; \
708 *p++ = c; \
709 } \
710 } \
711 else if (BASE_LEADING_CODE_P (c - 0x20)) \
712 { \
713 unsigned char *p0 = p; \
714 \
715 c -= 0x20; \
716 *p++ = c; \
717 bytes = BYTES_BY_CHAR_HEAD (c); \
718 while (--bytes) \
719 { \
720 c = SAFE_ONE_MORE_BYTE (); \
721 if (c < 0) \
722 break; \
723 *p++ = c; \
724 } \
725 if (UNIBYTE_STR_AS_MULTIBYTE_P (p0, p - p0, bytes)) \
726 c = STRING_CHAR (p0, bytes); \
727 else \
728 c = -1; \
729 } \
730 else \
731 c = -1; \
732 } while (0)
733
734
735/* Decode a composition rule represented as a component of composition
736 sequence of Emacs 20 style at SRC. Set C to the rule. If not
737 valid rule is found, set C to -1. */
738
739#define DECODE_EMACS_MULE_COMPOSITION_RULE(c) \
740 do { \
741 c = SAFE_ONE_MORE_BYTE (); \
742 c -= 0xA0; \
743 if (c < 0 || c >= 81) \
744 c = -1; \
745 else \
746 { \
747 gref = c / 9, nref = c % 9; \
748 c = COMPOSITION_ENCODE_RULE (gref, nref); \
749 } \
750 } while (0)
751
752
753/* Decode composition sequence encoded by `emacs-mule' at the source
754 pointed by SRC. SRC_END is the end of source. Store information
755 of the composition in CODING->cmp_data.
756
757 For backward compatibility, decode also a composition sequence of
758 Emacs 20 style. In that case, the composition sequence contains
759 characters that should be extracted into a buffer or string. Store
760 those characters at *DESTINATION in multibyte form.
761
762 If we encounter an invalid byte sequence, return 0.
763 If we encounter an insufficient source or destination, or
764 insufficient space in CODING->cmp_data, return 1.
765 Otherwise, return consumed bytes in the source.
766
767*/
768static INLINE int
769decode_composition_emacs_mule (coding, src, src_end,
770 destination, dst_end, dst_bytes)
771 struct coding_system *coding;
772 unsigned char *src, *src_end, **destination, *dst_end;
773 int dst_bytes;
774{
775 unsigned char *dst = *destination;
776 int method, data_len, nchars;
777 unsigned char *src_base = src++;
8ca3766a 778 /* Store components of composition. */
aa72b389
KH
779 int component[COMPOSITION_DATA_MAX_BUNCH_LENGTH];
780 int ncomponent;
781 /* Store multibyte form of characters to be composed. This is for
782 Emacs 20 style composition sequence. */
783 unsigned char buf[MAX_COMPOSITION_COMPONENTS * MAX_MULTIBYTE_LENGTH];
784 unsigned char *bufp = buf;
785 int c, i, gref, nref;
786
787 if (coding->cmp_data->used + COMPOSITION_DATA_MAX_BUNCH_LENGTH
788 >= COMPOSITION_DATA_SIZE)
789 {
790 coding->result = CODING_FINISH_INSUFFICIENT_CMP;
791 return -1;
792 }
793
794 ONE_MORE_BYTE (c);
795 if (c - 0xF0 >= COMPOSITION_RELATIVE
796 && c - 0xF0 <= COMPOSITION_WITH_RULE_ALTCHARS)
797 {
798 int with_rule;
799
800 method = c - 0xF0;
801 with_rule = (method == COMPOSITION_WITH_RULE
802 || method == COMPOSITION_WITH_RULE_ALTCHARS);
803 ONE_MORE_BYTE (c);
804 data_len = c - 0xA0;
805 if (data_len < 4
806 || src_base + data_len > src_end)
807 return 0;
808 ONE_MORE_BYTE (c);
809 nchars = c - 0xA0;
810 if (c < 1)
811 return 0;
812 for (ncomponent = 0; src < src_base + data_len; ncomponent++)
813 {
b1887814
RS
814 /* If it is longer than this, it can't be valid. */
815 if (ncomponent >= COMPOSITION_DATA_MAX_BUNCH_LENGTH)
816 return 0;
817
aa72b389
KH
818 if (ncomponent % 2 && with_rule)
819 {
820 ONE_MORE_BYTE (gref);
821 gref -= 32;
822 ONE_MORE_BYTE (nref);
823 nref -= 32;
824 c = COMPOSITION_ENCODE_RULE (gref, nref);
825 }
826 else
827 {
828 int bytes;
829 if (UNIBYTE_STR_AS_MULTIBYTE_P (src, src_end - src, bytes))
830 c = STRING_CHAR (src, bytes);
831 else
832 c = *src, bytes = 1;
833 src += bytes;
834 }
835 component[ncomponent] = c;
836 }
837 }
838 else
839 {
840 /* This may be an old Emacs 20 style format. See the comment at
841 the section 2 of this file. */
842 while (src < src_end && !CHAR_HEAD_P (*src)) src++;
843 if (src == src_end
844 && !(coding->mode & CODING_MODE_LAST_BLOCK))
845 goto label_end_of_loop;
846
847 src_end = src;
848 src = src_base + 1;
849 if (c < 0xC0)
850 {
851 method = COMPOSITION_RELATIVE;
852 for (ncomponent = 0; ncomponent < MAX_COMPOSITION_COMPONENTS;)
853 {
854 DECODE_EMACS_MULE_COMPOSITION_CHAR (c, bufp);
855 if (c < 0)
856 break;
857 component[ncomponent++] = c;
858 }
859 if (ncomponent < 2)
860 return 0;
861 nchars = ncomponent;
862 }
863 else if (c == 0xFF)
864 {
865 method = COMPOSITION_WITH_RULE;
866 src++;
867 DECODE_EMACS_MULE_COMPOSITION_CHAR (c, bufp);
868 if (c < 0)
869 return 0;
870 component[0] = c;
871 for (ncomponent = 1;
872 ncomponent < MAX_COMPOSITION_COMPONENTS * 2 - 1;)
873 {
874 DECODE_EMACS_MULE_COMPOSITION_RULE (c);
875 if (c < 0)
876 break;
877 component[ncomponent++] = c;
878 DECODE_EMACS_MULE_COMPOSITION_CHAR (c, bufp);
879 if (c < 0)
880 break;
881 component[ncomponent++] = c;
882 }
883 if (ncomponent < 3)
884 return 0;
885 nchars = (ncomponent + 1) / 2;
886 }
887 else
888 return 0;
889 }
890
891 if (buf == bufp || dst + (bufp - buf) <= (dst_bytes ? dst_end : src))
892 {
893 CODING_ADD_COMPOSITION_START (coding, coding->produced_char, method);
894 for (i = 0; i < ncomponent; i++)
895 CODING_ADD_COMPOSITION_COMPONENT (coding, component[i]);
93dec019 896 CODING_ADD_COMPOSITION_END (coding, coding->produced_char + nchars);
aa72b389
KH
897 if (buf < bufp)
898 {
899 unsigned char *p = buf;
900 EMIT_BYTES (p, bufp);
901 *destination += bufp - buf;
902 coding->produced_char += nchars;
903 }
904 return (src - src_base);
905 }
906 label_end_of_loop:
907 return -1;
908}
909
b73bfc1c 910/* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
4ed46869 911
b73bfc1c
KH
912static void
913decode_coding_emacs_mule (coding, source, destination, src_bytes, dst_bytes)
914 struct coding_system *coding;
915 unsigned char *source, *destination;
916 int src_bytes, dst_bytes;
917{
918 unsigned char *src = source;
919 unsigned char *src_end = source + src_bytes;
920 unsigned char *dst = destination;
921 unsigned char *dst_end = destination + dst_bytes;
922 /* SRC_BASE remembers the start position in source in each loop.
923 The loop will be exited when there's not enough source code, or
924 when there's not enough destination area to produce a
925 character. */
926 unsigned char *src_base;
4ed46869 927
b73bfc1c 928 coding->produced_char = 0;
8a33cf7b 929 while ((src_base = src) < src_end)
b73bfc1c
KH
930 {
931 unsigned char tmp[MAX_MULTIBYTE_LENGTH], *p;
932 int bytes;
ec6d2bb8 933
4af310db
EZ
934 if (*src == '\r')
935 {
2bcdf662 936 int c = *src++;
4af310db 937
4af310db
EZ
938 if (coding->eol_type == CODING_EOL_CR)
939 c = '\n';
940 else if (coding->eol_type == CODING_EOL_CRLF)
941 {
942 ONE_MORE_BYTE (c);
943 if (c != '\n')
944 {
945 if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
946 {
947 coding->result = CODING_FINISH_INCONSISTENT_EOL;
948 goto label_end_of_loop;
949 }
950 src--;
951 c = '\r';
952 }
953 }
954 *dst++ = c;
955 coding->produced_char++;
956 continue;
957 }
958 else if (*src == '\n')
959 {
960 if ((coding->eol_type == CODING_EOL_CR
961 || coding->eol_type == CODING_EOL_CRLF)
962 && coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
963 {
964 coding->result = CODING_FINISH_INCONSISTENT_EOL;
965 goto label_end_of_loop;
966 }
967 *dst++ = *src++;
968 coding->produced_char++;
969 continue;
970 }
aa72b389
KH
971 else if (*src == 0x80)
972 {
973 /* Start of composition data. */
974 int consumed = decode_composition_emacs_mule (coding, src, src_end,
975 &dst, dst_end,
976 dst_bytes);
977 if (consumed < 0)
978 goto label_end_of_loop;
979 else if (consumed > 0)
980 {
981 src += consumed;
982 continue;
983 }
984 bytes = CHAR_STRING (*src, tmp);
985 p = tmp;
986 src++;
987 }
4af310db 988 else if (UNIBYTE_STR_AS_MULTIBYTE_P (src, src_end - src, bytes))
b73bfc1c
KH
989 {
990 p = src;
991 src += bytes;
992 }
993 else
994 {
995 bytes = CHAR_STRING (*src, tmp);
996 p = tmp;
997 src++;
998 }
999 if (dst + bytes >= (dst_bytes ? dst_end : src))
1000 {
1001 coding->result = CODING_FINISH_INSUFFICIENT_DST;
4ed46869
KH
1002 break;
1003 }
b73bfc1c
KH
1004 while (bytes--) *dst++ = *p++;
1005 coding->produced_char++;
4ed46869 1006 }
4af310db 1007 label_end_of_loop:
b73bfc1c
KH
1008 coding->consumed = coding->consumed_char = src_base - source;
1009 coding->produced = dst - destination;
4ed46869
KH
1010}
1011
b73bfc1c 1012
aa72b389
KH
1013/* Encode composition data stored at DATA into a special byte sequence
1014 starting by 0x80. Update CODING->cmp_data_start and maybe
1015 CODING->cmp_data for the next call. */
1016
1017#define ENCODE_COMPOSITION_EMACS_MULE(coding, data) \
1018 do { \
1019 unsigned char buf[1024], *p0 = buf, *p; \
1020 int len = data[0]; \
1021 int i; \
1022 \
1023 buf[0] = 0x80; \
1024 buf[1] = 0xF0 + data[3]; /* METHOD */ \
1025 buf[3] = 0xA0 + (data[2] - data[1]); /* COMPOSED-CHARS */ \
1026 p = buf + 4; \
1027 if (data[3] == COMPOSITION_WITH_RULE \
1028 || data[3] == COMPOSITION_WITH_RULE_ALTCHARS) \
1029 { \
1030 p += CHAR_STRING (data[4], p); \
1031 for (i = 5; i < len; i += 2) \
1032 { \
1033 int gref, nref; \
1034 COMPOSITION_DECODE_RULE (data[i], gref, nref); \
1035 *p++ = 0x20 + gref; \
1036 *p++ = 0x20 + nref; \
1037 p += CHAR_STRING (data[i + 1], p); \
1038 } \
1039 } \
1040 else \
1041 { \
1042 for (i = 4; i < len; i++) \
1043 p += CHAR_STRING (data[i], p); \
1044 } \
1045 buf[2] = 0xA0 + (p - buf); /* COMPONENTS-BYTES */ \
1046 \
1047 if (dst + (p - buf) + 4 > (dst_bytes ? dst_end : src)) \
1048 { \
1049 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
1050 goto label_end_of_loop; \
1051 } \
1052 while (p0 < p) \
1053 *dst++ = *p0++; \
1054 coding->cmp_data_start += data[0]; \
1055 if (coding->cmp_data_start == coding->cmp_data->used \
1056 && coding->cmp_data->next) \
1057 { \
1058 coding->cmp_data = coding->cmp_data->next; \
1059 coding->cmp_data_start = 0; \
1060 } \
1061 } while (0)
93dec019 1062
aa72b389
KH
1063
1064static void encode_eol P_ ((struct coding_system *, unsigned char *,
1065 unsigned char *, int, int));
1066
1067static void
1068encode_coding_emacs_mule (coding, source, destination, src_bytes, dst_bytes)
1069 struct coding_system *coding;
1070 unsigned char *source, *destination;
1071 int src_bytes, dst_bytes;
1072{
1073 unsigned char *src = source;
1074 unsigned char *src_end = source + src_bytes;
1075 unsigned char *dst = destination;
1076 unsigned char *dst_end = destination + dst_bytes;
1077 unsigned char *src_base;
1078 int c;
1079 int char_offset;
1080 int *data;
1081
1082 Lisp_Object translation_table;
1083
1084 translation_table = Qnil;
1085
1086 /* Optimization for the case that there's no composition. */
1087 if (!coding->cmp_data || coding->cmp_data->used == 0)
1088 {
1089 encode_eol (coding, source, destination, src_bytes, dst_bytes);
1090 return;
1091 }
1092
1093 char_offset = coding->cmp_data->char_offset;
1094 data = coding->cmp_data->data + coding->cmp_data_start;
1095 while (1)
1096 {
1097 src_base = src;
1098
1099 /* If SRC starts a composition, encode the information about the
1100 composition in advance. */
1101 if (coding->cmp_data_start < coding->cmp_data->used
1102 && char_offset + coding->consumed_char == data[1])
1103 {
1104 ENCODE_COMPOSITION_EMACS_MULE (coding, data);
1105 char_offset = coding->cmp_data->char_offset;
1106 data = coding->cmp_data->data + coding->cmp_data_start;
1107 }
1108
1109 ONE_MORE_CHAR (c);
1110 if (c == '\n' && (coding->eol_type == CODING_EOL_CRLF
1111 || coding->eol_type == CODING_EOL_CR))
1112 {
1113 if (coding->eol_type == CODING_EOL_CRLF)
1114 EMIT_TWO_BYTES ('\r', c);
1115 else
1116 EMIT_ONE_BYTE ('\r');
1117 }
1118 else if (SINGLE_BYTE_CHAR_P (c))
1119 EMIT_ONE_BYTE (c);
1120 else
1121 EMIT_BYTES (src_base, src);
1122 coding->consumed_char++;
1123 }
1124 label_end_of_loop:
1125 coding->consumed = src_base - source;
1126 coding->produced = coding->produced_char = dst - destination;
1127 return;
1128}
b73bfc1c 1129
4ed46869
KH
1130\f
1131/*** 3. ISO2022 handlers ***/
1132
1133/* The following note describes the coding system ISO2022 briefly.
39787efd 1134 Since the intention of this note is to help understand the
cfb43547 1135 functions in this file, some parts are NOT ACCURATE or are OVERLY
39787efd 1136 SIMPLIFIED. For thorough understanding, please refer to the
cfb43547
DL
1137 original document of ISO2022. This is equivalent to the standard
1138 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
4ed46869
KH
1139
1140 ISO2022 provides many mechanisms to encode several character sets
cfb43547 1141 in 7-bit and 8-bit environments. For 7-bit environments, all text
39787efd
KH
1142 is encoded using bytes less than 128. This may make the encoded
1143 text a little bit longer, but the text passes more easily through
cfb43547 1144 several types of gateway, some of which strip off the MSB (Most
8ca3766a 1145 Significant Bit).
b73bfc1c 1146
cfb43547
DL
1147 There are two kinds of character sets: control character sets and
1148 graphic character sets. The former contain control characters such
4ed46869 1149 as `newline' and `escape' to provide control functions (control
39787efd 1150 functions are also provided by escape sequences). The latter
cfb43547 1151 contain graphic characters such as 'A' and '-'. Emacs recognizes
4ed46869
KH
1152 two control character sets and many graphic character sets.
1153
1154 Graphic character sets are classified into one of the following
39787efd
KH
1155 four classes, according to the number of bytes (DIMENSION) and
1156 number of characters in one dimension (CHARS) of the set:
1157 - DIMENSION1_CHARS94
1158 - DIMENSION1_CHARS96
1159 - DIMENSION2_CHARS94
1160 - DIMENSION2_CHARS96
1161
1162 In addition, each character set is assigned an identification tag,
cfb43547 1163 unique for each set, called the "final character" (denoted as <F>
39787efd
KH
1164 hereafter). The <F> of each character set is decided by ECMA(*)
1165 when it is registered in ISO. The code range of <F> is 0x30..0x7F
1166 (0x30..0x3F are for private use only).
4ed46869
KH
1167
1168 Note (*): ECMA = European Computer Manufacturers Association
1169
cfb43547 1170 Here are examples of graphic character sets [NAME(<F>)]:
4ed46869
KH
1171 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
1172 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
1173 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
1174 o DIMENSION2_CHARS96 -- none for the moment
1175
39787efd 1176 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
4ed46869
KH
1177 C0 [0x00..0x1F] -- control character plane 0
1178 GL [0x20..0x7F] -- graphic character plane 0
1179 C1 [0x80..0x9F] -- control character plane 1
1180 GR [0xA0..0xFF] -- graphic character plane 1
1181
1182 A control character set is directly designated and invoked to C0 or
39787efd
KH
1183 C1 by an escape sequence. The most common case is that:
1184 - ISO646's control character set is designated/invoked to C0, and
1185 - ISO6429's control character set is designated/invoked to C1,
1186 and usually these designations/invocations are omitted in encoded
1187 text. In a 7-bit environment, only C0 can be used, and a control
1188 character for C1 is encoded by an appropriate escape sequence to
1189 fit into the environment. All control characters for C1 are
1190 defined to have corresponding escape sequences.
4ed46869
KH
1191
1192 A graphic character set is at first designated to one of four
1193 graphic registers (G0 through G3), then these graphic registers are
1194 invoked to GL or GR. These designations and invocations can be
1195 done independently. The most common case is that G0 is invoked to
39787efd
KH
1196 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
1197 these invocations and designations are omitted in encoded text.
1198 In a 7-bit environment, only GL can be used.
4ed46869 1199
39787efd
KH
1200 When a graphic character set of CHARS94 is invoked to GL, codes
1201 0x20 and 0x7F of the GL area work as control characters SPACE and
1202 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
1203 be used.
4ed46869
KH
1204
1205 There are two ways of invocation: locking-shift and single-shift.
1206 With locking-shift, the invocation lasts until the next different
39787efd
KH
1207 invocation, whereas with single-shift, the invocation affects the
1208 following character only and doesn't affect the locking-shift
1209 state. Invocations are done by the following control characters or
1210 escape sequences:
4ed46869
KH
1211
1212 ----------------------------------------------------------------------
39787efd 1213 abbrev function cntrl escape seq description
4ed46869 1214 ----------------------------------------------------------------------
39787efd
KH
1215 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
1216 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
1217 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
1218 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
1219 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
1220 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
1221 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
1222 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
1223 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
4ed46869 1224 ----------------------------------------------------------------------
39787efd
KH
1225 (*) These are not used by any known coding system.
1226
1227 Control characters for these functions are defined by macros
1228 ISO_CODE_XXX in `coding.h'.
4ed46869 1229
39787efd 1230 Designations are done by the following escape sequences:
4ed46869
KH
1231 ----------------------------------------------------------------------
1232 escape sequence description
1233 ----------------------------------------------------------------------
1234 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
1235 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
1236 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
1237 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
1238 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
1239 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
1240 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
1241 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
1242 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
1243 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
1244 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
1245 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
1246 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
1247 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
1248 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
1249 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
1250 ----------------------------------------------------------------------
1251
1252 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
39787efd 1253 of dimension 1, chars 94, and final character <F>, etc...
4ed46869
KH
1254
1255 Note (*): Although these designations are not allowed in ISO2022,
1256 Emacs accepts them on decoding, and produces them on encoding
39787efd 1257 CHARS96 character sets in a coding system which is characterized as
4ed46869
KH
1258 7-bit environment, non-locking-shift, and non-single-shift.
1259
1260 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
39787efd 1261 '(' can be omitted. We refer to this as "short-form" hereafter.
4ed46869 1262
cfb43547 1263 Now you may notice that there are a lot of ways of encoding the
39787efd
KH
1264 same multilingual text in ISO2022. Actually, there exist many
1265 coding systems such as Compound Text (used in X11's inter client
8ca3766a
DL
1266 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
1267 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
4ed46869
KH
1268 localized platforms), and all of these are variants of ISO2022.
1269
1270 In addition to the above, Emacs handles two more kinds of escape
1271 sequences: ISO6429's direction specification and Emacs' private
1272 sequence for specifying character composition.
1273
39787efd 1274 ISO6429's direction specification takes the following form:
4ed46869
KH
1275 o CSI ']' -- end of the current direction
1276 o CSI '0' ']' -- end of the current direction
1277 o CSI '1' ']' -- start of left-to-right text
1278 o CSI '2' ']' -- start of right-to-left text
1279 The control character CSI (0x9B: control sequence introducer) is
39787efd
KH
1280 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
1281
1282 Character composition specification takes the following form:
ec6d2bb8
KH
1283 o ESC '0' -- start relative composition
1284 o ESC '1' -- end composition
1285 o ESC '2' -- start rule-base composition (*)
1286 o ESC '3' -- start relative composition with alternate chars (**)
1287 o ESC '4' -- start rule-base composition with alternate chars (**)
b73bfc1c 1288 Since these are not standard escape sequences of any ISO standard,
cfb43547 1289 the use of them with these meanings is restricted to Emacs only.
ec6d2bb8 1290
cfb43547 1291 (*) This form is used only in Emacs 20.5 and older versions,
b73bfc1c 1292 but the newer versions can safely decode it.
cfb43547 1293 (**) This form is used only in Emacs 21.1 and newer versions,
b73bfc1c 1294 and the older versions can't decode it.
ec6d2bb8 1295
cfb43547 1296 Here's a list of example usages of these composition escape
b73bfc1c 1297 sequences (categorized by `enum composition_method').
ec6d2bb8 1298
b73bfc1c 1299 COMPOSITION_RELATIVE:
ec6d2bb8 1300 ESC 0 CHAR [ CHAR ] ESC 1
8ca3766a 1301 COMPOSITION_WITH_RULE:
ec6d2bb8 1302 ESC 2 CHAR [ RULE CHAR ] ESC 1
b73bfc1c 1303 COMPOSITION_WITH_ALTCHARS:
ec6d2bb8 1304 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
b73bfc1c 1305 COMPOSITION_WITH_RULE_ALTCHARS:
ec6d2bb8 1306 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
4ed46869
KH
1307
1308enum iso_code_class_type iso_code_class[256];
1309
05e6f5dc
KH
1310#define CHARSET_OK(idx, charset, c) \
1311 (coding_system_table[idx] \
1312 && (charset == CHARSET_ASCII \
1313 || (safe_chars = coding_safe_chars (coding_system_table[idx]), \
1314 CODING_SAFE_CHAR_P (safe_chars, c))) \
1315 && (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding_system_table[idx], \
1316 charset) \
1317 != CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION))
d46c5b12
KH
1318
1319#define SHIFT_OUT_OK(idx) \
1320 (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding_system_table[idx], 1) >= 0)
1321
4ed46869 1322/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
cfb43547 1323 Check if a text is encoded in ISO2022. If it is, return an
4ed46869
KH
1324 integer in which appropriate flag bits any of:
1325 CODING_CATEGORY_MASK_ISO_7
d46c5b12 1326 CODING_CATEGORY_MASK_ISO_7_TIGHT
4ed46869
KH
1327 CODING_CATEGORY_MASK_ISO_8_1
1328 CODING_CATEGORY_MASK_ISO_8_2
7717c392
KH
1329 CODING_CATEGORY_MASK_ISO_7_ELSE
1330 CODING_CATEGORY_MASK_ISO_8_ELSE
4ed46869
KH
1331 are set. If a code which should never appear in ISO2022 is found,
1332 returns 0. */
1333
0a28aafb
KH
1334static int
1335detect_coding_iso2022 (src, src_end, multibytep)
4ed46869 1336 unsigned char *src, *src_end;
0a28aafb 1337 int multibytep;
4ed46869 1338{
d46c5b12
KH
1339 int mask = CODING_CATEGORY_MASK_ISO;
1340 int mask_found = 0;
f46869e4 1341 int reg[4], shift_out = 0, single_shifting = 0;
da55a2b7 1342 int c, c1, charset;
b73bfc1c
KH
1343 /* Dummy for ONE_MORE_BYTE. */
1344 struct coding_system dummy_coding;
1345 struct coding_system *coding = &dummy_coding;
05e6f5dc 1346 Lisp_Object safe_chars;
3f003981 1347
d46c5b12 1348 reg[0] = CHARSET_ASCII, reg[1] = reg[2] = reg[3] = -1;
3f003981 1349 while (mask && src < src_end)
4ed46869 1350 {
0a28aafb 1351 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
4ed46869
KH
1352 switch (c)
1353 {
1354 case ISO_CODE_ESC:
74383408
KH
1355 if (inhibit_iso_escape_detection)
1356 break;
f46869e4 1357 single_shifting = 0;
0a28aafb 1358 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
d46c5b12 1359 if (c >= '(' && c <= '/')
4ed46869 1360 {
bf9cdd4e 1361 /* Designation sequence for a charset of dimension 1. */
0a28aafb 1362 ONE_MORE_BYTE_CHECK_MULTIBYTE (c1, multibytep);
d46c5b12
KH
1363 if (c1 < ' ' || c1 >= 0x80
1364 || (charset = iso_charset_table[0][c >= ','][c1]) < 0)
1365 /* Invalid designation sequence. Just ignore. */
1366 break;
1367 reg[(c - '(') % 4] = charset;
bf9cdd4e
KH
1368 }
1369 else if (c == '$')
1370 {
1371 /* Designation sequence for a charset of dimension 2. */
0a28aafb 1372 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
bf9cdd4e
KH
1373 if (c >= '@' && c <= 'B')
1374 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
d46c5b12 1375 reg[0] = charset = iso_charset_table[1][0][c];
bf9cdd4e 1376 else if (c >= '(' && c <= '/')
bcf26d6a 1377 {
0a28aafb 1378 ONE_MORE_BYTE_CHECK_MULTIBYTE (c1, multibytep);
d46c5b12
KH
1379 if (c1 < ' ' || c1 >= 0x80
1380 || (charset = iso_charset_table[1][c >= ','][c1]) < 0)
1381 /* Invalid designation sequence. Just ignore. */
1382 break;
1383 reg[(c - '(') % 4] = charset;
bcf26d6a 1384 }
bf9cdd4e 1385 else
d46c5b12
KH
1386 /* Invalid designation sequence. Just ignore. */
1387 break;
1388 }
ae9ff118 1389 else if (c == 'N' || c == 'O')
d46c5b12 1390 {
ae9ff118
KH
1391 /* ESC <Fe> for SS2 or SS3. */
1392 mask &= CODING_CATEGORY_MASK_ISO_7_ELSE;
d46c5b12 1393 break;
4ed46869 1394 }
ec6d2bb8
KH
1395 else if (c >= '0' && c <= '4')
1396 {
1397 /* ESC <Fp> for start/end composition. */
1398 mask_found |= CODING_CATEGORY_MASK_ISO;
1399 break;
1400 }
bf9cdd4e 1401 else
d46c5b12
KH
1402 /* Invalid escape sequence. Just ignore. */
1403 break;
1404
1405 /* We found a valid designation sequence for CHARSET. */
1406 mask &= ~CODING_CATEGORY_MASK_ISO_8BIT;
05e6f5dc
KH
1407 c = MAKE_CHAR (charset, 0, 0);
1408 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7, charset, c))
d46c5b12
KH
1409 mask_found |= CODING_CATEGORY_MASK_ISO_7;
1410 else
1411 mask &= ~CODING_CATEGORY_MASK_ISO_7;
05e6f5dc 1412 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7_TIGHT, charset, c))
d46c5b12
KH
1413 mask_found |= CODING_CATEGORY_MASK_ISO_7_TIGHT;
1414 else
1415 mask &= ~CODING_CATEGORY_MASK_ISO_7_TIGHT;
05e6f5dc 1416 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7_ELSE, charset, c))
ae9ff118
KH
1417 mask_found |= CODING_CATEGORY_MASK_ISO_7_ELSE;
1418 else
d46c5b12 1419 mask &= ~CODING_CATEGORY_MASK_ISO_7_ELSE;
05e6f5dc 1420 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_8_ELSE, charset, c))
ae9ff118
KH
1421 mask_found |= CODING_CATEGORY_MASK_ISO_8_ELSE;
1422 else
d46c5b12 1423 mask &= ~CODING_CATEGORY_MASK_ISO_8_ELSE;
4ed46869
KH
1424 break;
1425
4ed46869 1426 case ISO_CODE_SO:
74383408
KH
1427 if (inhibit_iso_escape_detection)
1428 break;
f46869e4 1429 single_shifting = 0;
d46c5b12
KH
1430 if (shift_out == 0
1431 && (reg[1] >= 0
1432 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_7_ELSE)
1433 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_8_ELSE)))
1434 {
1435 /* Locking shift out. */
1436 mask &= ~CODING_CATEGORY_MASK_ISO_7BIT;
1437 mask_found |= CODING_CATEGORY_MASK_ISO_SHIFT;
1438 }
e0e989f6 1439 break;
93dec019 1440
d46c5b12 1441 case ISO_CODE_SI:
74383408
KH
1442 if (inhibit_iso_escape_detection)
1443 break;
f46869e4 1444 single_shifting = 0;
d46c5b12
KH
1445 if (shift_out == 1)
1446 {
1447 /* Locking shift in. */
1448 mask &= ~CODING_CATEGORY_MASK_ISO_7BIT;
1449 mask_found |= CODING_CATEGORY_MASK_ISO_SHIFT;
1450 }
1451 break;
1452
4ed46869 1453 case ISO_CODE_CSI:
f46869e4 1454 single_shifting = 0;
4ed46869
KH
1455 case ISO_CODE_SS2:
1456 case ISO_CODE_SS3:
3f003981
KH
1457 {
1458 int newmask = CODING_CATEGORY_MASK_ISO_8_ELSE;
1459
74383408
KH
1460 if (inhibit_iso_escape_detection)
1461 break;
70c22245
KH
1462 if (c != ISO_CODE_CSI)
1463 {
d46c5b12
KH
1464 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_1]->flags
1465 & CODING_FLAG_ISO_SINGLE_SHIFT)
70c22245 1466 newmask |= CODING_CATEGORY_MASK_ISO_8_1;
d46c5b12
KH
1467 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_2]->flags
1468 & CODING_FLAG_ISO_SINGLE_SHIFT)
70c22245 1469 newmask |= CODING_CATEGORY_MASK_ISO_8_2;
f46869e4 1470 single_shifting = 1;
70c22245 1471 }
3f003981
KH
1472 if (VECTORP (Vlatin_extra_code_table)
1473 && !NILP (XVECTOR (Vlatin_extra_code_table)->contents[c]))
1474 {
d46c5b12
KH
1475 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_1]->flags
1476 & CODING_FLAG_ISO_LATIN_EXTRA)
3f003981 1477 newmask |= CODING_CATEGORY_MASK_ISO_8_1;
d46c5b12
KH
1478 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_2]->flags
1479 & CODING_FLAG_ISO_LATIN_EXTRA)
3f003981
KH
1480 newmask |= CODING_CATEGORY_MASK_ISO_8_2;
1481 }
1482 mask &= newmask;
d46c5b12 1483 mask_found |= newmask;
3f003981
KH
1484 }
1485 break;
4ed46869
KH
1486
1487 default:
1488 if (c < 0x80)
f46869e4
KH
1489 {
1490 single_shifting = 0;
1491 break;
1492 }
4ed46869 1493 else if (c < 0xA0)
c4825358 1494 {
f46869e4 1495 single_shifting = 0;
3f003981
KH
1496 if (VECTORP (Vlatin_extra_code_table)
1497 && !NILP (XVECTOR (Vlatin_extra_code_table)->contents[c]))
c4825358 1498 {
3f003981
KH
1499 int newmask = 0;
1500
d46c5b12
KH
1501 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_1]->flags
1502 & CODING_FLAG_ISO_LATIN_EXTRA)
3f003981 1503 newmask |= CODING_CATEGORY_MASK_ISO_8_1;
d46c5b12
KH
1504 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_2]->flags
1505 & CODING_FLAG_ISO_LATIN_EXTRA)
3f003981
KH
1506 newmask |= CODING_CATEGORY_MASK_ISO_8_2;
1507 mask &= newmask;
d46c5b12 1508 mask_found |= newmask;
c4825358 1509 }
3f003981
KH
1510 else
1511 return 0;
c4825358 1512 }
4ed46869
KH
1513 else
1514 {
d46c5b12 1515 mask &= ~(CODING_CATEGORY_MASK_ISO_7BIT
7717c392 1516 | CODING_CATEGORY_MASK_ISO_7_ELSE);
d46c5b12 1517 mask_found |= CODING_CATEGORY_MASK_ISO_8_1;
f46869e4
KH
1518 /* Check the length of succeeding codes of the range
1519 0xA0..0FF. If the byte length is odd, we exclude
1520 CODING_CATEGORY_MASK_ISO_8_2. We can check this only
1521 when we are not single shifting. */
b73bfc1c
KH
1522 if (!single_shifting
1523 && mask & CODING_CATEGORY_MASK_ISO_8_2)
f46869e4 1524 {
e17de821 1525 int i = 1;
b73bfc1c
KH
1526 while (src < src_end)
1527 {
0a28aafb 1528 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
b73bfc1c
KH
1529 if (c < 0xA0)
1530 break;
1531 i++;
1532 }
1533
1534 if (i & 1 && src < src_end)
f46869e4
KH
1535 mask &= ~CODING_CATEGORY_MASK_ISO_8_2;
1536 else
1537 mask_found |= CODING_CATEGORY_MASK_ISO_8_2;
1538 }
4ed46869
KH
1539 }
1540 break;
1541 }
1542 }
b73bfc1c 1543 label_end_of_loop:
d46c5b12 1544 return (mask & mask_found);
4ed46869
KH
1545}
1546
b73bfc1c
KH
1547/* Decode a character of which charset is CHARSET, the 1st position
1548 code is C1, the 2nd position code is C2, and return the decoded
1549 character code. If the variable `translation_table' is non-nil,
1550 returned the translated code. */
ec6d2bb8 1551
b73bfc1c
KH
1552#define DECODE_ISO_CHARACTER(charset, c1, c2) \
1553 (NILP (translation_table) \
1554 ? MAKE_CHAR (charset, c1, c2) \
1555 : translate_char (translation_table, -1, charset, c1, c2))
4ed46869
KH
1556
1557/* Set designation state into CODING. */
d46c5b12
KH
1558#define DECODE_DESIGNATION(reg, dimension, chars, final_char) \
1559 do { \
05e6f5dc 1560 int charset, c; \
944bd420
KH
1561 \
1562 if (final_char < '0' || final_char >= 128) \
1563 goto label_invalid_code; \
1564 charset = ISO_CHARSET_TABLE (make_number (dimension), \
1565 make_number (chars), \
1566 make_number (final_char)); \
05e6f5dc 1567 c = MAKE_CHAR (charset, 0, 0); \
d46c5b12 1568 if (charset >= 0 \
704c5781 1569 && (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) == reg \
05e6f5dc 1570 || CODING_SAFE_CHAR_P (safe_chars, c))) \
d46c5b12
KH
1571 { \
1572 if (coding->spec.iso2022.last_invalid_designation_register == 0 \
1573 && reg == 0 \
1574 && charset == CHARSET_ASCII) \
1575 { \
1576 /* We should insert this designation sequence as is so \
1577 that it is surely written back to a file. */ \
1578 coding->spec.iso2022.last_invalid_designation_register = -1; \
1579 goto label_invalid_code; \
1580 } \
1581 coding->spec.iso2022.last_invalid_designation_register = -1; \
1582 if ((coding->mode & CODING_MODE_DIRECTION) \
1583 && CHARSET_REVERSE_CHARSET (charset) >= 0) \
1584 charset = CHARSET_REVERSE_CHARSET (charset); \
1585 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
1586 } \
1587 else \
1588 { \
1589 coding->spec.iso2022.last_invalid_designation_register = reg; \
1590 goto label_invalid_code; \
1591 } \
4ed46869
KH
1592 } while (0)
1593
ec6d2bb8
KH
1594/* Allocate a memory block for storing information about compositions.
1595 The block is chained to the already allocated blocks. */
d46c5b12 1596
33fb63eb 1597void
ec6d2bb8 1598coding_allocate_composition_data (coding, char_offset)
d46c5b12 1599 struct coding_system *coding;
ec6d2bb8 1600 int char_offset;
d46c5b12 1601{
ec6d2bb8
KH
1602 struct composition_data *cmp_data
1603 = (struct composition_data *) xmalloc (sizeof *cmp_data);
1604
1605 cmp_data->char_offset = char_offset;
1606 cmp_data->used = 0;
1607 cmp_data->prev = coding->cmp_data;
1608 cmp_data->next = NULL;
1609 if (coding->cmp_data)
1610 coding->cmp_data->next = cmp_data;
1611 coding->cmp_data = cmp_data;
1612 coding->cmp_data_start = 0;
1613}
d46c5b12 1614
aa72b389
KH
1615/* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
1616 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
1617 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
1618 ESC 3 : altchar composition : ESC 3 ALT ... ESC 0 CHAR ... ESC 1
1619 ESC 4 : alt&rule composition : ESC 4 ALT RULE .. ALT ESC 0 CHAR ... ESC 1
1620 */
ec6d2bb8 1621
33fb63eb
KH
1622#define DECODE_COMPOSITION_START(c1) \
1623 do { \
1624 if (coding->composing == COMPOSITION_DISABLED) \
1625 { \
1626 *dst++ = ISO_CODE_ESC; \
1627 *dst++ = c1 & 0x7f; \
1628 coding->produced_char += 2; \
1629 } \
1630 else if (!COMPOSING_P (coding)) \
1631 { \
1632 /* This is surely the start of a composition. We must be sure \
1633 that coding->cmp_data has enough space to store the \
1634 information about the composition. If not, terminate the \
1635 current decoding loop, allocate one more memory block for \
8ca3766a 1636 coding->cmp_data in the caller, then start the decoding \
33fb63eb
KH
1637 loop again. We can't allocate memory here directly because \
1638 it may cause buffer/string relocation. */ \
1639 if (!coding->cmp_data \
1640 || (coding->cmp_data->used + COMPOSITION_DATA_MAX_BUNCH_LENGTH \
1641 >= COMPOSITION_DATA_SIZE)) \
1642 { \
1643 coding->result = CODING_FINISH_INSUFFICIENT_CMP; \
1644 goto label_end_of_loop; \
1645 } \
1646 coding->composing = (c1 == '0' ? COMPOSITION_RELATIVE \
1647 : c1 == '2' ? COMPOSITION_WITH_RULE \
1648 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
1649 : COMPOSITION_WITH_RULE_ALTCHARS); \
1650 CODING_ADD_COMPOSITION_START (coding, coding->produced_char, \
1651 coding->composing); \
1652 coding->composition_rule_follows = 0; \
1653 } \
1654 else \
1655 { \
1656 /* We are already handling a composition. If the method is \
1657 the following two, the codes following the current escape \
1658 sequence are actual characters stored in a buffer. */ \
1659 if (coding->composing == COMPOSITION_WITH_ALTCHARS \
1660 || coding->composing == COMPOSITION_WITH_RULE_ALTCHARS) \
1661 { \
1662 coding->composing = COMPOSITION_RELATIVE; \
1663 coding->composition_rule_follows = 0; \
1664 } \
1665 } \
ec6d2bb8
KH
1666 } while (0)
1667
8ca3766a 1668/* Handle composition end sequence ESC 1. */
ec6d2bb8
KH
1669
1670#define DECODE_COMPOSITION_END(c1) \
1671 do { \
93dec019 1672 if (! COMPOSING_P (coding)) \
ec6d2bb8
KH
1673 { \
1674 *dst++ = ISO_CODE_ESC; \
1675 *dst++ = c1; \
1676 coding->produced_char += 2; \
1677 } \
1678 else \
1679 { \
1680 CODING_ADD_COMPOSITION_END (coding, coding->produced_char); \
1681 coding->composing = COMPOSITION_NO; \
1682 } \
1683 } while (0)
1684
1685/* Decode a composition rule from the byte C1 (and maybe one more byte
1686 from SRC) and store one encoded composition rule in
1687 coding->cmp_data. */
1688
1689#define DECODE_COMPOSITION_RULE(c1) \
1690 do { \
1691 int rule = 0; \
1692 (c1) -= 32; \
1693 if (c1 < 81) /* old format (before ver.21) */ \
1694 { \
1695 int gref = (c1) / 9; \
1696 int nref = (c1) % 9; \
1697 if (gref == 4) gref = 10; \
1698 if (nref == 4) nref = 10; \
1699 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
1700 } \
b73bfc1c 1701 else if (c1 < 93) /* new format (after ver.21) */ \
ec6d2bb8
KH
1702 { \
1703 ONE_MORE_BYTE (c2); \
1704 rule = COMPOSITION_ENCODE_RULE (c1 - 81, c2 - 32); \
1705 } \
1706 CODING_ADD_COMPOSITION_COMPONENT (coding, rule); \
1707 coding->composition_rule_follows = 0; \
1708 } while (0)
88993dfd 1709
d46c5b12 1710
4ed46869
KH
1711/* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
1712
b73bfc1c 1713static void
d46c5b12 1714decode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
4ed46869
KH
1715 struct coding_system *coding;
1716 unsigned char *source, *destination;
1717 int src_bytes, dst_bytes;
4ed46869
KH
1718{
1719 unsigned char *src = source;
1720 unsigned char *src_end = source + src_bytes;
1721 unsigned char *dst = destination;
1722 unsigned char *dst_end = destination + dst_bytes;
4ed46869
KH
1723 /* Charsets invoked to graphic plane 0 and 1 respectively. */
1724 int charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
1725 int charset1 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 1);
b73bfc1c
KH
1726 /* SRC_BASE remembers the start position in source in each loop.
1727 The loop will be exited when there's not enough source code
1728 (within macro ONE_MORE_BYTE), or when there's not enough
1729 destination area to produce a character (within macro
1730 EMIT_CHAR). */
1731 unsigned char *src_base;
1732 int c, charset;
1733 Lisp_Object translation_table;
05e6f5dc
KH
1734 Lisp_Object safe_chars;
1735
1736 safe_chars = coding_safe_chars (coding);
bdd9fb48 1737
b73bfc1c
KH
1738 if (NILP (Venable_character_translation))
1739 translation_table = Qnil;
1740 else
1741 {
1742 translation_table = coding->translation_table_for_decode;
1743 if (NILP (translation_table))
1744 translation_table = Vstandard_translation_table_for_decode;
1745 }
4ed46869 1746
b73bfc1c
KH
1747 coding->result = CODING_FINISH_NORMAL;
1748
1749 while (1)
4ed46869 1750 {
b73bfc1c
KH
1751 int c1, c2;
1752
1753 src_base = src;
1754 ONE_MORE_BYTE (c1);
4ed46869 1755
ec6d2bb8 1756 /* We produce no character or one character. */
4ed46869
KH
1757 switch (iso_code_class [c1])
1758 {
1759 case ISO_0x20_or_0x7F:
ec6d2bb8
KH
1760 if (COMPOSING_P (coding) && coding->composition_rule_follows)
1761 {
1762 DECODE_COMPOSITION_RULE (c1);
b73bfc1c 1763 continue;
ec6d2bb8
KH
1764 }
1765 if (charset0 < 0 || CHARSET_CHARS (charset0) == 94)
4ed46869
KH
1766 {
1767 /* This is SPACE or DEL. */
b73bfc1c 1768 charset = CHARSET_ASCII;
4ed46869
KH
1769 break;
1770 }
1771 /* This is a graphic character, we fall down ... */
1772
1773 case ISO_graphic_plane_0:
ec6d2bb8 1774 if (COMPOSING_P (coding) && coding->composition_rule_follows)
b73bfc1c
KH
1775 {
1776 DECODE_COMPOSITION_RULE (c1);
1777 continue;
1778 }
1779 charset = charset0;
4ed46869
KH
1780 break;
1781
1782 case ISO_0xA0_or_0xFF:
d46c5b12
KH
1783 if (charset1 < 0 || CHARSET_CHARS (charset1) == 94
1784 || coding->flags & CODING_FLAG_ISO_SEVEN_BITS)
fb88bf2d 1785 goto label_invalid_code;
4ed46869
KH
1786 /* This is a graphic character, we fall down ... */
1787
1788 case ISO_graphic_plane_1:
b73bfc1c 1789 if (charset1 < 0)
fb88bf2d 1790 goto label_invalid_code;
b73bfc1c 1791 charset = charset1;
4ed46869
KH
1792 break;
1793
b73bfc1c 1794 case ISO_control_0:
ec6d2bb8
KH
1795 if (COMPOSING_P (coding))
1796 DECODE_COMPOSITION_END ('1');
1797
4ed46869
KH
1798 /* All ISO2022 control characters in this class have the
1799 same representation in Emacs internal format. */
d46c5b12
KH
1800 if (c1 == '\n'
1801 && (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
1802 && (coding->eol_type == CODING_EOL_CR
1803 || coding->eol_type == CODING_EOL_CRLF))
1804 {
b73bfc1c
KH
1805 coding->result = CODING_FINISH_INCONSISTENT_EOL;
1806 goto label_end_of_loop;
d46c5b12 1807 }
b73bfc1c 1808 charset = CHARSET_ASCII;
4ed46869
KH
1809 break;
1810
b73bfc1c
KH
1811 case ISO_control_1:
1812 if (COMPOSING_P (coding))
1813 DECODE_COMPOSITION_END ('1');
1814 goto label_invalid_code;
1815
4ed46869 1816 case ISO_carriage_return:
ec6d2bb8
KH
1817 if (COMPOSING_P (coding))
1818 DECODE_COMPOSITION_END ('1');
1819
4ed46869 1820 if (coding->eol_type == CODING_EOL_CR)
b73bfc1c 1821 c1 = '\n';
4ed46869
KH
1822 else if (coding->eol_type == CODING_EOL_CRLF)
1823 {
1824 ONE_MORE_BYTE (c1);
b73bfc1c 1825 if (c1 != ISO_CODE_LF)
4ed46869 1826 {
d46c5b12
KH
1827 if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
1828 {
b73bfc1c
KH
1829 coding->result = CODING_FINISH_INCONSISTENT_EOL;
1830 goto label_end_of_loop;
d46c5b12 1831 }
4ed46869 1832 src--;
b73bfc1c 1833 c1 = '\r';
4ed46869
KH
1834 }
1835 }
b73bfc1c 1836 charset = CHARSET_ASCII;
4ed46869
KH
1837 break;
1838
1839 case ISO_shift_out:
d46c5b12
KH
1840 if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT)
1841 || CODING_SPEC_ISO_DESIGNATION (coding, 1) < 0)
1842 goto label_invalid_code;
4ed46869
KH
1843 CODING_SPEC_ISO_INVOCATION (coding, 0) = 1;
1844 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
b73bfc1c 1845 continue;
4ed46869
KH
1846
1847 case ISO_shift_in:
d46c5b12
KH
1848 if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT))
1849 goto label_invalid_code;
4ed46869
KH
1850 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0;
1851 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
b73bfc1c 1852 continue;
4ed46869
KH
1853
1854 case ISO_single_shift_2_7:
1855 case ISO_single_shift_2:
d46c5b12
KH
1856 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT))
1857 goto label_invalid_code;
4ed46869
KH
1858 /* SS2 is handled as an escape sequence of ESC 'N' */
1859 c1 = 'N';
1860 goto label_escape_sequence;
1861
1862 case ISO_single_shift_3:
d46c5b12
KH
1863 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT))
1864 goto label_invalid_code;
4ed46869
KH
1865 /* SS2 is handled as an escape sequence of ESC 'O' */
1866 c1 = 'O';
1867 goto label_escape_sequence;
1868
1869 case ISO_control_sequence_introducer:
1870 /* CSI is handled as an escape sequence of ESC '[' ... */
1871 c1 = '[';
1872 goto label_escape_sequence;
1873
1874 case ISO_escape:
1875 ONE_MORE_BYTE (c1);
1876 label_escape_sequence:
1877 /* Escape sequences handled by Emacs are invocation,
1878 designation, direction specification, and character
1879 composition specification. */
1880 switch (c1)
1881 {
1882 case '&': /* revision of following character set */
1883 ONE_MORE_BYTE (c1);
1884 if (!(c1 >= '@' && c1 <= '~'))
d46c5b12 1885 goto label_invalid_code;
4ed46869
KH
1886 ONE_MORE_BYTE (c1);
1887 if (c1 != ISO_CODE_ESC)
d46c5b12 1888 goto label_invalid_code;
4ed46869
KH
1889 ONE_MORE_BYTE (c1);
1890 goto label_escape_sequence;
1891
1892 case '$': /* designation of 2-byte character set */
d46c5b12
KH
1893 if (! (coding->flags & CODING_FLAG_ISO_DESIGNATION))
1894 goto label_invalid_code;
4ed46869
KH
1895 ONE_MORE_BYTE (c1);
1896 if (c1 >= '@' && c1 <= 'B')
1897 { /* designation of JISX0208.1978, GB2312.1980,
88993dfd 1898 or JISX0208.1980 */
4ed46869
KH
1899 DECODE_DESIGNATION (0, 2, 94, c1);
1900 }
1901 else if (c1 >= 0x28 && c1 <= 0x2B)
1902 { /* designation of DIMENSION2_CHARS94 character set */
1903 ONE_MORE_BYTE (c2);
1904 DECODE_DESIGNATION (c1 - 0x28, 2, 94, c2);
1905 }
1906 else if (c1 >= 0x2C && c1 <= 0x2F)
1907 { /* designation of DIMENSION2_CHARS96 character set */
1908 ONE_MORE_BYTE (c2);
1909 DECODE_DESIGNATION (c1 - 0x2C, 2, 96, c2);
1910 }
1911 else
d46c5b12 1912 goto label_invalid_code;
b73bfc1c
KH
1913 /* We must update these variables now. */
1914 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
1915 charset1 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 1);
1916 continue;
4ed46869
KH
1917
1918 case 'n': /* invocation of locking-shift-2 */
d46c5b12
KH
1919 if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT)
1920 || CODING_SPEC_ISO_DESIGNATION (coding, 2) < 0)
1921 goto label_invalid_code;
4ed46869 1922 CODING_SPEC_ISO_INVOCATION (coding, 0) = 2;
e0e989f6 1923 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
b73bfc1c 1924 continue;
4ed46869
KH
1925
1926 case 'o': /* invocation of locking-shift-3 */
d46c5b12
KH
1927 if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT)
1928 || CODING_SPEC_ISO_DESIGNATION (coding, 3) < 0)
1929 goto label_invalid_code;
4ed46869 1930 CODING_SPEC_ISO_INVOCATION (coding, 0) = 3;
e0e989f6 1931 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
b73bfc1c 1932 continue;
4ed46869
KH
1933
1934 case 'N': /* invocation of single-shift-2 */
d46c5b12
KH
1935 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT)
1936 || CODING_SPEC_ISO_DESIGNATION (coding, 2) < 0)
1937 goto label_invalid_code;
4ed46869 1938 charset = CODING_SPEC_ISO_DESIGNATION (coding, 2);
b73bfc1c 1939 ONE_MORE_BYTE (c1);
e7046a18
KH
1940 if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0))
1941 goto label_invalid_code;
4ed46869
KH
1942 break;
1943
1944 case 'O': /* invocation of single-shift-3 */
d46c5b12
KH
1945 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT)
1946 || CODING_SPEC_ISO_DESIGNATION (coding, 3) < 0)
1947 goto label_invalid_code;
4ed46869 1948 charset = CODING_SPEC_ISO_DESIGNATION (coding, 3);
b73bfc1c 1949 ONE_MORE_BYTE (c1);
e7046a18
KH
1950 if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0))
1951 goto label_invalid_code;
4ed46869
KH
1952 break;
1953
ec6d2bb8
KH
1954 case '0': case '2': case '3': case '4': /* start composition */
1955 DECODE_COMPOSITION_START (c1);
b73bfc1c 1956 continue;
4ed46869 1957
ec6d2bb8
KH
1958 case '1': /* end composition */
1959 DECODE_COMPOSITION_END (c1);
b73bfc1c 1960 continue;
4ed46869
KH
1961
1962 case '[': /* specification of direction */
d46c5b12
KH
1963 if (coding->flags & CODING_FLAG_ISO_NO_DIRECTION)
1964 goto label_invalid_code;
4ed46869 1965 /* For the moment, nested direction is not supported.
d46c5b12 1966 So, `coding->mode & CODING_MODE_DIRECTION' zero means
8ca3766a 1967 left-to-right, and nonzero means right-to-left. */
4ed46869
KH
1968 ONE_MORE_BYTE (c1);
1969 switch (c1)
1970 {
1971 case ']': /* end of the current direction */
d46c5b12 1972 coding->mode &= ~CODING_MODE_DIRECTION;
4ed46869
KH
1973
1974 case '0': /* end of the current direction */
1975 case '1': /* start of left-to-right direction */
1976 ONE_MORE_BYTE (c1);
1977 if (c1 == ']')
d46c5b12 1978 coding->mode &= ~CODING_MODE_DIRECTION;
4ed46869 1979 else
d46c5b12 1980 goto label_invalid_code;
4ed46869
KH
1981 break;
1982
1983 case '2': /* start of right-to-left direction */
1984 ONE_MORE_BYTE (c1);
1985 if (c1 == ']')
d46c5b12 1986 coding->mode |= CODING_MODE_DIRECTION;
4ed46869 1987 else
d46c5b12 1988 goto label_invalid_code;
4ed46869
KH
1989 break;
1990
1991 default:
d46c5b12 1992 goto label_invalid_code;
4ed46869 1993 }
b73bfc1c 1994 continue;
4ed46869
KH
1995
1996 default:
d46c5b12
KH
1997 if (! (coding->flags & CODING_FLAG_ISO_DESIGNATION))
1998 goto label_invalid_code;
4ed46869
KH
1999 if (c1 >= 0x28 && c1 <= 0x2B)
2000 { /* designation of DIMENSION1_CHARS94 character set */
2001 ONE_MORE_BYTE (c2);
2002 DECODE_DESIGNATION (c1 - 0x28, 1, 94, c2);
2003 }
2004 else if (c1 >= 0x2C && c1 <= 0x2F)
2005 { /* designation of DIMENSION1_CHARS96 character set */
2006 ONE_MORE_BYTE (c2);
2007 DECODE_DESIGNATION (c1 - 0x2C, 1, 96, c2);
2008 }
2009 else
b73bfc1c
KH
2010 goto label_invalid_code;
2011 /* We must update these variables now. */
2012 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
2013 charset1 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 1);
2014 continue;
4ed46869 2015 }
b73bfc1c 2016 }
4ed46869 2017
b73bfc1c
KH
2018 /* Now we know CHARSET and 1st position code C1 of a character.
2019 Produce a multibyte sequence for that character while getting
2020 2nd position code C2 if necessary. */
2021 if (CHARSET_DIMENSION (charset) == 2)
2022 {
2023 ONE_MORE_BYTE (c2);
2024 if (c1 < 0x80 ? c2 < 0x20 || c2 >= 0x80 : c2 < 0xA0)
2025 /* C2 is not in a valid range. */
2026 goto label_invalid_code;
4ed46869 2027 }
b73bfc1c
KH
2028 c = DECODE_ISO_CHARACTER (charset, c1, c2);
2029 EMIT_CHAR (c);
4ed46869
KH
2030 continue;
2031
b73bfc1c
KH
2032 label_invalid_code:
2033 coding->errors++;
2034 if (COMPOSING_P (coding))
2035 DECODE_COMPOSITION_END ('1');
4ed46869 2036 src = src_base;
b73bfc1c
KH
2037 c = *src++;
2038 EMIT_CHAR (c);
4ed46869 2039 }
fb88bf2d 2040
b73bfc1c
KH
2041 label_end_of_loop:
2042 coding->consumed = coding->consumed_char = src_base - source;
d46c5b12 2043 coding->produced = dst - destination;
b73bfc1c 2044 return;
4ed46869
KH
2045}
2046
b73bfc1c 2047
f4dee582 2048/* ISO2022 encoding stuff. */
4ed46869
KH
2049
2050/*
f4dee582 2051 It is not enough to say just "ISO2022" on encoding, we have to
cfb43547 2052 specify more details. In Emacs, each ISO2022 coding system
4ed46869 2053 variant has the following specifications:
8ca3766a 2054 1. Initial designation to G0 through G3.
4ed46869
KH
2055 2. Allows short-form designation?
2056 3. ASCII should be designated to G0 before control characters?
2057 4. ASCII should be designated to G0 at end of line?
2058 5. 7-bit environment or 8-bit environment?
2059 6. Use locking-shift?
2060 7. Use Single-shift?
2061 And the following two are only for Japanese:
2062 8. Use ASCII in place of JIS0201-1976-Roman?
2063 9. Use JISX0208-1983 in place of JISX0208-1978?
2064 These specifications are encoded in `coding->flags' as flag bits
2065 defined by macros CODING_FLAG_ISO_XXX. See `coding.h' for more
f4dee582 2066 details.
4ed46869
KH
2067*/
2068
2069/* Produce codes (escape sequence) for designating CHARSET to graphic
b73bfc1c
KH
2070 register REG at DST, and increment DST. If <final-char> of CHARSET is
2071 '@', 'A', or 'B' and the coding system CODING allows, produce
2072 designation sequence of short-form. */
4ed46869
KH
2073
2074#define ENCODE_DESIGNATION(charset, reg, coding) \
2075 do { \
2076 unsigned char final_char = CHARSET_ISO_FINAL_CHAR (charset); \
2077 char *intermediate_char_94 = "()*+"; \
2078 char *intermediate_char_96 = ",-./"; \
70c22245 2079 int revision = CODING_SPEC_ISO_REVISION_NUMBER(coding, charset); \
b73bfc1c 2080 \
70c22245
KH
2081 if (revision < 255) \
2082 { \
4ed46869
KH
2083 *dst++ = ISO_CODE_ESC; \
2084 *dst++ = '&'; \
70c22245 2085 *dst++ = '@' + revision; \
4ed46869 2086 } \
b73bfc1c 2087 *dst++ = ISO_CODE_ESC; \
4ed46869
KH
2088 if (CHARSET_DIMENSION (charset) == 1) \
2089 { \
2090 if (CHARSET_CHARS (charset) == 94) \
2091 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
2092 else \
2093 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
2094 } \
2095 else \
2096 { \
2097 *dst++ = '$'; \
2098 if (CHARSET_CHARS (charset) == 94) \
2099 { \
b73bfc1c
KH
2100 if (! (coding->flags & CODING_FLAG_ISO_SHORT_FORM) \
2101 || reg != 0 \
2102 || final_char < '@' || final_char > 'B') \
4ed46869
KH
2103 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
2104 } \
2105 else \
b73bfc1c 2106 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
4ed46869 2107 } \
b73bfc1c 2108 *dst++ = final_char; \
4ed46869
KH
2109 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
2110 } while (0)
2111
2112/* The following two macros produce codes (control character or escape
2113 sequence) for ISO2022 single-shift functions (single-shift-2 and
2114 single-shift-3). */
2115
2116#define ENCODE_SINGLE_SHIFT_2 \
2117 do { \
2118 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
2119 *dst++ = ISO_CODE_ESC, *dst++ = 'N'; \
2120 else \
b73bfc1c 2121 *dst++ = ISO_CODE_SS2; \
4ed46869
KH
2122 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
2123 } while (0)
2124
fb88bf2d
KH
2125#define ENCODE_SINGLE_SHIFT_3 \
2126 do { \
4ed46869 2127 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
fb88bf2d
KH
2128 *dst++ = ISO_CODE_ESC, *dst++ = 'O'; \
2129 else \
b73bfc1c 2130 *dst++ = ISO_CODE_SS3; \
4ed46869
KH
2131 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
2132 } while (0)
2133
2134/* The following four macros produce codes (control character or
2135 escape sequence) for ISO2022 locking-shift functions (shift-in,
2136 shift-out, locking-shift-2, and locking-shift-3). */
2137
b73bfc1c
KH
2138#define ENCODE_SHIFT_IN \
2139 do { \
2140 *dst++ = ISO_CODE_SI; \
4ed46869
KH
2141 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0; \
2142 } while (0)
2143
b73bfc1c
KH
2144#define ENCODE_SHIFT_OUT \
2145 do { \
2146 *dst++ = ISO_CODE_SO; \
4ed46869
KH
2147 CODING_SPEC_ISO_INVOCATION (coding, 0) = 1; \
2148 } while (0)
2149
2150#define ENCODE_LOCKING_SHIFT_2 \
2151 do { \
2152 *dst++ = ISO_CODE_ESC, *dst++ = 'n'; \
2153 CODING_SPEC_ISO_INVOCATION (coding, 0) = 2; \
2154 } while (0)
2155
b73bfc1c
KH
2156#define ENCODE_LOCKING_SHIFT_3 \
2157 do { \
2158 *dst++ = ISO_CODE_ESC, *dst++ = 'o'; \
4ed46869
KH
2159 CODING_SPEC_ISO_INVOCATION (coding, 0) = 3; \
2160 } while (0)
2161
f4dee582
RS
2162/* Produce codes for a DIMENSION1 character whose character set is
2163 CHARSET and whose position-code is C1. Designation and invocation
4ed46869
KH
2164 sequences are also produced in advance if necessary. */
2165
6e85d753
KH
2166#define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
2167 do { \
2168 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
2169 { \
2170 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
2171 *dst++ = c1 & 0x7F; \
2172 else \
2173 *dst++ = c1 | 0x80; \
2174 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
2175 break; \
2176 } \
2177 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
2178 { \
2179 *dst++ = c1 & 0x7F; \
2180 break; \
2181 } \
2182 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
2183 { \
2184 *dst++ = c1 | 0x80; \
2185 break; \
2186 } \
6e85d753
KH
2187 else \
2188 /* Since CHARSET is not yet invoked to any graphic planes, we \
2189 must invoke it, or, at first, designate it to some graphic \
2190 register. Then repeat the loop to actually produce the \
2191 character. */ \
2192 dst = encode_invocation_designation (charset, coding, dst); \
4ed46869
KH
2193 } while (1)
2194
f4dee582
RS
2195/* Produce codes for a DIMENSION2 character whose character set is
2196 CHARSET and whose position-codes are C1 and C2. Designation and
4ed46869
KH
2197 invocation codes are also produced in advance if necessary. */
2198
6e85d753
KH
2199#define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
2200 do { \
2201 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
2202 { \
2203 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
2204 *dst++ = c1 & 0x7F, *dst++ = c2 & 0x7F; \
2205 else \
2206 *dst++ = c1 | 0x80, *dst++ = c2 | 0x80; \
2207 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
2208 break; \
2209 } \
2210 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
2211 { \
2212 *dst++ = c1 & 0x7F, *dst++= c2 & 0x7F; \
2213 break; \
2214 } \
2215 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
2216 { \
2217 *dst++ = c1 | 0x80, *dst++= c2 | 0x80; \
2218 break; \
2219 } \
6e85d753
KH
2220 else \
2221 /* Since CHARSET is not yet invoked to any graphic planes, we \
2222 must invoke it, or, at first, designate it to some graphic \
2223 register. Then repeat the loop to actually produce the \
2224 character. */ \
2225 dst = encode_invocation_designation (charset, coding, dst); \
4ed46869
KH
2226 } while (1)
2227
05e6f5dc
KH
2228#define ENCODE_ISO_CHARACTER(c) \
2229 do { \
2230 int charset, c1, c2; \
2231 \
2232 SPLIT_CHAR (c, charset, c1, c2); \
2233 if (CHARSET_DEFINED_P (charset)) \
2234 { \
2235 if (CHARSET_DIMENSION (charset) == 1) \
2236 { \
2237 if (charset == CHARSET_ASCII \
2238 && coding->flags & CODING_FLAG_ISO_USE_ROMAN) \
2239 charset = charset_latin_jisx0201; \
2240 ENCODE_ISO_CHARACTER_DIMENSION1 (charset, c1); \
2241 } \
2242 else \
2243 { \
2244 if (charset == charset_jisx0208 \
2245 && coding->flags & CODING_FLAG_ISO_USE_OLDJIS) \
2246 charset = charset_jisx0208_1978; \
2247 ENCODE_ISO_CHARACTER_DIMENSION2 (charset, c1, c2); \
2248 } \
2249 } \
2250 else \
2251 { \
2252 *dst++ = c1; \
2253 if (c2 >= 0) \
2254 *dst++ = c2; \
2255 } \
2256 } while (0)
2257
2258
2259/* Instead of encoding character C, produce one or two `?'s. */
2260
2261#define ENCODE_UNSAFE_CHARACTER(c) \
6f551029 2262 do { \
05e6f5dc
KH
2263 ENCODE_ISO_CHARACTER (CODING_INHIBIT_CHARACTER_SUBSTITUTION); \
2264 if (CHARSET_WIDTH (CHAR_CHARSET (c)) > 1) \
2265 ENCODE_ISO_CHARACTER (CODING_INHIBIT_CHARACTER_SUBSTITUTION); \
84fbb8a0 2266 } while (0)
bdd9fb48 2267
05e6f5dc 2268
4ed46869
KH
2269/* Produce designation and invocation codes at a place pointed by DST
2270 to use CHARSET. The element `spec.iso2022' of *CODING is updated.
2271 Return new DST. */
2272
2273unsigned char *
2274encode_invocation_designation (charset, coding, dst)
2275 int charset;
2276 struct coding_system *coding;
2277 unsigned char *dst;
2278{
2279 int reg; /* graphic register number */
2280
2281 /* At first, check designations. */
2282 for (reg = 0; reg < 4; reg++)
2283 if (charset == CODING_SPEC_ISO_DESIGNATION (coding, reg))
2284 break;
2285
2286 if (reg >= 4)
2287 {
2288 /* CHARSET is not yet designated to any graphic registers. */
2289 /* At first check the requested designation. */
2290 reg = CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset);
1ba9e4ab
KH
2291 if (reg == CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION)
2292 /* Since CHARSET requests no special designation, designate it
2293 to graphic register 0. */
4ed46869
KH
2294 reg = 0;
2295
2296 ENCODE_DESIGNATION (charset, reg, coding);
2297 }
2298
2299 if (CODING_SPEC_ISO_INVOCATION (coding, 0) != reg
2300 && CODING_SPEC_ISO_INVOCATION (coding, 1) != reg)
2301 {
2302 /* Since the graphic register REG is not invoked to any graphic
2303 planes, invoke it to graphic plane 0. */
2304 switch (reg)
2305 {
2306 case 0: /* graphic register 0 */
2307 ENCODE_SHIFT_IN;
2308 break;
2309
2310 case 1: /* graphic register 1 */
2311 ENCODE_SHIFT_OUT;
2312 break;
2313
2314 case 2: /* graphic register 2 */
2315 if (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT)
2316 ENCODE_SINGLE_SHIFT_2;
2317 else
2318 ENCODE_LOCKING_SHIFT_2;
2319 break;
2320
2321 case 3: /* graphic register 3 */
2322 if (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT)
2323 ENCODE_SINGLE_SHIFT_3;
2324 else
2325 ENCODE_LOCKING_SHIFT_3;
2326 break;
2327 }
2328 }
b73bfc1c 2329
4ed46869
KH
2330 return dst;
2331}
2332
ec6d2bb8
KH
2333/* Produce 2-byte codes for encoded composition rule RULE. */
2334
2335#define ENCODE_COMPOSITION_RULE(rule) \
2336 do { \
2337 int gref, nref; \
2338 COMPOSITION_DECODE_RULE (rule, gref, nref); \
2339 *dst++ = 32 + 81 + gref; \
2340 *dst++ = 32 + nref; \
2341 } while (0)
2342
2343/* Produce codes for indicating the start of a composition sequence
2344 (ESC 0, ESC 3, or ESC 4). DATA points to an array of integers
2345 which specify information about the composition. See the comment
2346 in coding.h for the format of DATA. */
2347
2348#define ENCODE_COMPOSITION_START(coding, data) \
2349 do { \
2350 coding->composing = data[3]; \
2351 *dst++ = ISO_CODE_ESC; \
2352 if (coding->composing == COMPOSITION_RELATIVE) \
2353 *dst++ = '0'; \
2354 else \
2355 { \
2356 *dst++ = (coding->composing == COMPOSITION_WITH_ALTCHARS \
2357 ? '3' : '4'); \
2358 coding->cmp_data_index = coding->cmp_data_start + 4; \
2359 coding->composition_rule_follows = 0; \
2360 } \
2361 } while (0)
2362
2363/* Produce codes for indicating the end of the current composition. */
2364
2365#define ENCODE_COMPOSITION_END(coding, data) \
2366 do { \
2367 *dst++ = ISO_CODE_ESC; \
2368 *dst++ = '1'; \
2369 coding->cmp_data_start += data[0]; \
2370 coding->composing = COMPOSITION_NO; \
2371 if (coding->cmp_data_start == coding->cmp_data->used \
2372 && coding->cmp_data->next) \
2373 { \
2374 coding->cmp_data = coding->cmp_data->next; \
2375 coding->cmp_data_start = 0; \
2376 } \
2377 } while (0)
2378
2379/* Produce composition start sequence ESC 0. Here, this sequence
2380 doesn't mean the start of a new composition but means that we have
2381 just produced components (alternate chars and composition rules) of
2382 the composition and the actual text follows in SRC. */
2383
2384#define ENCODE_COMPOSITION_FAKE_START(coding) \
2385 do { \
2386 *dst++ = ISO_CODE_ESC; \
2387 *dst++ = '0'; \
2388 coding->composing = COMPOSITION_RELATIVE; \
2389 } while (0)
4ed46869
KH
2390
2391/* The following three macros produce codes for indicating direction
2392 of text. */
b73bfc1c
KH
2393#define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
2394 do { \
4ed46869 2395 if (coding->flags == CODING_FLAG_ISO_SEVEN_BITS) \
b73bfc1c
KH
2396 *dst++ = ISO_CODE_ESC, *dst++ = '['; \
2397 else \
2398 *dst++ = ISO_CODE_CSI; \
4ed46869
KH
2399 } while (0)
2400
2401#define ENCODE_DIRECTION_R2L \
b73bfc1c 2402 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst), *dst++ = '2', *dst++ = ']'
4ed46869
KH
2403
2404#define ENCODE_DIRECTION_L2R \
b73bfc1c 2405 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst), *dst++ = '0', *dst++ = ']'
4ed46869
KH
2406
2407/* Produce codes for designation and invocation to reset the graphic
2408 planes and registers to initial state. */
e0e989f6
KH
2409#define ENCODE_RESET_PLANE_AND_REGISTER \
2410 do { \
2411 int reg; \
2412 if (CODING_SPEC_ISO_INVOCATION (coding, 0) != 0) \
2413 ENCODE_SHIFT_IN; \
2414 for (reg = 0; reg < 4; reg++) \
2415 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg) >= 0 \
2416 && (CODING_SPEC_ISO_DESIGNATION (coding, reg) \
2417 != CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg))) \
2418 ENCODE_DESIGNATION \
2419 (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg), reg, coding); \
4ed46869
KH
2420 } while (0)
2421
bdd9fb48 2422/* Produce designation sequences of charsets in the line started from
b73bfc1c 2423 SRC to a place pointed by DST, and return updated DST.
bdd9fb48
KH
2424
2425 If the current block ends before any end-of-line, we may fail to
d46c5b12
KH
2426 find all the necessary designations. */
2427
b73bfc1c
KH
2428static unsigned char *
2429encode_designation_at_bol (coding, translation_table, src, src_end, dst)
e0e989f6 2430 struct coding_system *coding;
b73bfc1c
KH
2431 Lisp_Object translation_table;
2432 unsigned char *src, *src_end, *dst;
e0e989f6 2433{
bdd9fb48
KH
2434 int charset, c, found = 0, reg;
2435 /* Table of charsets to be designated to each graphic register. */
2436 int r[4];
bdd9fb48
KH
2437
2438 for (reg = 0; reg < 4; reg++)
2439 r[reg] = -1;
2440
b73bfc1c 2441 while (found < 4)
e0e989f6 2442 {
b73bfc1c
KH
2443 ONE_MORE_CHAR (c);
2444 if (c == '\n')
2445 break;
93dec019 2446
b73bfc1c 2447 charset = CHAR_CHARSET (c);
e0e989f6 2448 reg = CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset);
d46c5b12 2449 if (reg != CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION && r[reg] < 0)
bdd9fb48
KH
2450 {
2451 found++;
2452 r[reg] = charset;
2453 }
bdd9fb48
KH
2454 }
2455
b73bfc1c 2456 label_end_of_loop:
bdd9fb48
KH
2457 if (found)
2458 {
2459 for (reg = 0; reg < 4; reg++)
2460 if (r[reg] >= 0
2461 && CODING_SPEC_ISO_DESIGNATION (coding, reg) != r[reg])
2462 ENCODE_DESIGNATION (r[reg], reg, coding);
e0e989f6 2463 }
b73bfc1c
KH
2464
2465 return dst;
e0e989f6
KH
2466}
2467
4ed46869
KH
2468/* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
2469
b73bfc1c 2470static void
d46c5b12 2471encode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
4ed46869
KH
2472 struct coding_system *coding;
2473 unsigned char *source, *destination;
2474 int src_bytes, dst_bytes;
4ed46869
KH
2475{
2476 unsigned char *src = source;
2477 unsigned char *src_end = source + src_bytes;
2478 unsigned char *dst = destination;
2479 unsigned char *dst_end = destination + dst_bytes;
b73bfc1c 2480 /* Since the maximum bytes produced by each loop is 20, we subtract 19
4ed46869
KH
2481 from DST_END to assure overflow checking is necessary only at the
2482 head of loop. */
b73bfc1c
KH
2483 unsigned char *adjusted_dst_end = dst_end - 19;
2484 /* SRC_BASE remembers the start position in source in each loop.
2485 The loop will be exited when there's not enough source text to
2486 analyze multi-byte codes (within macro ONE_MORE_CHAR), or when
2487 there's not enough destination area to produce encoded codes
2488 (within macro EMIT_BYTES). */
2489 unsigned char *src_base;
2490 int c;
2491 Lisp_Object translation_table;
05e6f5dc
KH
2492 Lisp_Object safe_chars;
2493
2494 safe_chars = coding_safe_chars (coding);
bdd9fb48 2495
b73bfc1c
KH
2496 if (NILP (Venable_character_translation))
2497 translation_table = Qnil;
2498 else
2499 {
2500 translation_table = coding->translation_table_for_encode;
2501 if (NILP (translation_table))
2502 translation_table = Vstandard_translation_table_for_encode;
2503 }
4ed46869 2504
d46c5b12 2505 coding->consumed_char = 0;
b73bfc1c
KH
2506 coding->errors = 0;
2507 while (1)
4ed46869 2508 {
b73bfc1c
KH
2509 src_base = src;
2510
2511 if (dst >= (dst_bytes ? adjusted_dst_end : (src - 19)))
2512 {
2513 coding->result = CODING_FINISH_INSUFFICIENT_DST;
2514 break;
2515 }
4ed46869 2516
e0e989f6
KH
2517 if (coding->flags & CODING_FLAG_ISO_DESIGNATE_AT_BOL
2518 && CODING_SPEC_ISO_BOL (coding))
2519 {
bdd9fb48 2520 /* We have to produce designation sequences if any now. */
b73bfc1c
KH
2521 dst = encode_designation_at_bol (coding, translation_table,
2522 src, src_end, dst);
e0e989f6
KH
2523 CODING_SPEC_ISO_BOL (coding) = 0;
2524 }
2525
ec6d2bb8
KH
2526 /* Check composition start and end. */
2527 if (coding->composing != COMPOSITION_DISABLED
2528 && coding->cmp_data_start < coding->cmp_data->used)
4ed46869 2529 {
ec6d2bb8
KH
2530 struct composition_data *cmp_data = coding->cmp_data;
2531 int *data = cmp_data->data + coding->cmp_data_start;
2532 int this_pos = cmp_data->char_offset + coding->consumed_char;
2533
2534 if (coding->composing == COMPOSITION_RELATIVE)
4ed46869 2535 {
ec6d2bb8
KH
2536 if (this_pos == data[2])
2537 {
2538 ENCODE_COMPOSITION_END (coding, data);
2539 cmp_data = coding->cmp_data;
2540 data = cmp_data->data + coding->cmp_data_start;
2541 }
4ed46869 2542 }
ec6d2bb8 2543 else if (COMPOSING_P (coding))
4ed46869 2544 {
ec6d2bb8
KH
2545 /* COMPOSITION_WITH_ALTCHARS or COMPOSITION_WITH_RULE_ALTCHAR */
2546 if (coding->cmp_data_index == coding->cmp_data_start + data[0])
2547 /* We have consumed components of the composition.
8ca3766a 2548 What follows in SRC is the composition's base
ec6d2bb8
KH
2549 text. */
2550 ENCODE_COMPOSITION_FAKE_START (coding);
2551 else
4ed46869 2552 {
ec6d2bb8
KH
2553 int c = cmp_data->data[coding->cmp_data_index++];
2554 if (coding->composition_rule_follows)
2555 {
2556 ENCODE_COMPOSITION_RULE (c);
2557 coding->composition_rule_follows = 0;
2558 }
2559 else
2560 {
05e6f5dc
KH
2561 if (coding->flags & CODING_FLAG_ISO_SAFE
2562 && ! CODING_SAFE_CHAR_P (safe_chars, c))
2563 ENCODE_UNSAFE_CHARACTER (c);
2564 else
2565 ENCODE_ISO_CHARACTER (c);
ec6d2bb8
KH
2566 if (coding->composing == COMPOSITION_WITH_RULE_ALTCHARS)
2567 coding->composition_rule_follows = 1;
2568 }
4ed46869
KH
2569 continue;
2570 }
ec6d2bb8
KH
2571 }
2572 if (!COMPOSING_P (coding))
2573 {
2574 if (this_pos == data[1])
4ed46869 2575 {
ec6d2bb8
KH
2576 ENCODE_COMPOSITION_START (coding, data);
2577 continue;
4ed46869 2578 }
4ed46869
KH
2579 }
2580 }
ec6d2bb8 2581
b73bfc1c 2582 ONE_MORE_CHAR (c);
4ed46869 2583
b73bfc1c
KH
2584 /* Now encode the character C. */
2585 if (c < 0x20 || c == 0x7F)
2586 {
2587 if (c == '\r')
19a8d9e0 2588 {
b73bfc1c
KH
2589 if (! (coding->mode & CODING_MODE_SELECTIVE_DISPLAY))
2590 {
2591 if (coding->flags & CODING_FLAG_ISO_RESET_AT_CNTL)
2592 ENCODE_RESET_PLANE_AND_REGISTER;
2593 *dst++ = c;
2594 continue;
2595 }
2596 /* fall down to treat '\r' as '\n' ... */
2597 c = '\n';
19a8d9e0 2598 }
b73bfc1c 2599 if (c == '\n')
19a8d9e0 2600 {
b73bfc1c
KH
2601 if (coding->flags & CODING_FLAG_ISO_RESET_AT_EOL)
2602 ENCODE_RESET_PLANE_AND_REGISTER;
2603 if (coding->flags & CODING_FLAG_ISO_INIT_AT_BOL)
2604 bcopy (coding->spec.iso2022.initial_designation,
2605 coding->spec.iso2022.current_designation,
2606 sizeof coding->spec.iso2022.initial_designation);
2607 if (coding->eol_type == CODING_EOL_LF
2608 || coding->eol_type == CODING_EOL_UNDECIDED)
2609 *dst++ = ISO_CODE_LF;
2610 else if (coding->eol_type == CODING_EOL_CRLF)
2611 *dst++ = ISO_CODE_CR, *dst++ = ISO_CODE_LF;
2612 else
2613 *dst++ = ISO_CODE_CR;
2614 CODING_SPEC_ISO_BOL (coding) = 1;
19a8d9e0 2615 }
93dec019 2616 else
19a8d9e0 2617 {
b73bfc1c
KH
2618 if (coding->flags & CODING_FLAG_ISO_RESET_AT_CNTL)
2619 ENCODE_RESET_PLANE_AND_REGISTER;
2620 *dst++ = c;
19a8d9e0 2621 }
4ed46869 2622 }
b73bfc1c 2623 else if (ASCII_BYTE_P (c))
05e6f5dc 2624 ENCODE_ISO_CHARACTER (c);
b73bfc1c 2625 else if (SINGLE_BYTE_CHAR_P (c))
88993dfd 2626 {
b73bfc1c
KH
2627 *dst++ = c;
2628 coding->errors++;
88993dfd 2629 }
05e6f5dc
KH
2630 else if (coding->flags & CODING_FLAG_ISO_SAFE
2631 && ! CODING_SAFE_CHAR_P (safe_chars, c))
2632 ENCODE_UNSAFE_CHARACTER (c);
b73bfc1c 2633 else
05e6f5dc 2634 ENCODE_ISO_CHARACTER (c);
b73bfc1c
KH
2635
2636 coding->consumed_char++;
84fbb8a0 2637 }
b73bfc1c
KH
2638
2639 label_end_of_loop:
2640 coding->consumed = src_base - source;
d46c5b12 2641 coding->produced = coding->produced_char = dst - destination;
4ed46869
KH
2642}
2643
2644\f
2645/*** 4. SJIS and BIG5 handlers ***/
2646
cfb43547 2647/* Although SJIS and BIG5 are not ISO coding systems, they are used
4ed46869
KH
2648 quite widely. So, for the moment, Emacs supports them in the bare
2649 C code. But, in the future, they may be supported only by CCL. */
2650
2651/* SJIS is a coding system encoding three character sets: ASCII, right
2652 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
2653 as is. A character of charset katakana-jisx0201 is encoded by
2654 "position-code + 0x80". A character of charset japanese-jisx0208
2655 is encoded in 2-byte but two position-codes are divided and shifted
cfb43547 2656 so that it fits in the range below.
4ed46869
KH
2657
2658 --- CODE RANGE of SJIS ---
2659 (character set) (range)
2660 ASCII 0x00 .. 0x7F
682169fe 2661 KATAKANA-JISX0201 0xA1 .. 0xDF
c28a9453 2662 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
d14d03ac 2663 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
4ed46869
KH
2664 -------------------------------
2665
2666*/
2667
2668/* BIG5 is a coding system encoding two character sets: ASCII and
2669 Big5. An ASCII character is encoded as is. Big5 is a two-byte
cfb43547 2670 character set and is encoded in two bytes.
4ed46869
KH
2671
2672 --- CODE RANGE of BIG5 ---
2673 (character set) (range)
2674 ASCII 0x00 .. 0x7F
2675 Big5 (1st byte) 0xA1 .. 0xFE
2676 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
2677 --------------------------
2678
2679 Since the number of characters in Big5 is larger than maximum
2680 characters in Emacs' charset (96x96), it can't be handled as one
2681 charset. So, in Emacs, Big5 is divided into two: `charset-big5-1'
2682 and `charset-big5-2'. Both are DIMENSION2 and CHARS94. The former
2683 contains frequently used characters and the latter contains less
2684 frequently used characters. */
2685
2686/* Macros to decode or encode a character of Big5 in BIG5. B1 and B2
2687 are the 1st and 2nd position-codes of Big5 in BIG5 coding system.
2688 C1 and C2 are the 1st and 2nd position-codes of of Emacs' internal
2689 format. CHARSET is `charset_big5_1' or `charset_big5_2'. */
2690
2691/* Number of Big5 characters which have the same code in 1st byte. */
2692#define BIG5_SAME_ROW (0xFF - 0xA1 + 0x7F - 0x40)
2693
2694#define DECODE_BIG5(b1, b2, charset, c1, c2) \
2695 do { \
2696 unsigned int temp \
2697 = (b1 - 0xA1) * BIG5_SAME_ROW + b2 - (b2 < 0x7F ? 0x40 : 0x62); \
2698 if (b1 < 0xC9) \
2699 charset = charset_big5_1; \
2700 else \
2701 { \
2702 charset = charset_big5_2; \
2703 temp -= (0xC9 - 0xA1) * BIG5_SAME_ROW; \
2704 } \
2705 c1 = temp / (0xFF - 0xA1) + 0x21; \
2706 c2 = temp % (0xFF - 0xA1) + 0x21; \
2707 } while (0)
2708
2709#define ENCODE_BIG5(charset, c1, c2, b1, b2) \
2710 do { \
2711 unsigned int temp = (c1 - 0x21) * (0xFF - 0xA1) + (c2 - 0x21); \
2712 if (charset == charset_big5_2) \
2713 temp += BIG5_SAME_ROW * (0xC9 - 0xA1); \
2714 b1 = temp / BIG5_SAME_ROW + 0xA1; \
2715 b2 = temp % BIG5_SAME_ROW; \
2716 b2 += b2 < 0x3F ? 0x40 : 0x62; \
2717 } while (0)
2718
2719/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2720 Check if a text is encoded in SJIS. If it is, return
2721 CODING_CATEGORY_MASK_SJIS, else return 0. */
2722
0a28aafb
KH
2723static int
2724detect_coding_sjis (src, src_end, multibytep)
4ed46869 2725 unsigned char *src, *src_end;
0a28aafb 2726 int multibytep;
4ed46869 2727{
b73bfc1c
KH
2728 int c;
2729 /* Dummy for ONE_MORE_BYTE. */
2730 struct coding_system dummy_coding;
2731 struct coding_system *coding = &dummy_coding;
4ed46869 2732
b73bfc1c 2733 while (1)
4ed46869 2734 {
0a28aafb 2735 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
682169fe
KH
2736 if (c < 0x80)
2737 continue;
2738 if (c == 0x80 || c == 0xA0 || c > 0xEF)
2739 return 0;
2740 if (c <= 0x9F || c >= 0xE0)
4ed46869 2741 {
682169fe
KH
2742 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
2743 if (c < 0x40 || c == 0x7F || c > 0xFC)
4ed46869
KH
2744 return 0;
2745 }
2746 }
b73bfc1c 2747 label_end_of_loop:
4ed46869
KH
2748 return CODING_CATEGORY_MASK_SJIS;
2749}
2750
2751/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2752 Check if a text is encoded in BIG5. If it is, return
2753 CODING_CATEGORY_MASK_BIG5, else return 0. */
2754
0a28aafb
KH
2755static int
2756detect_coding_big5 (src, src_end, multibytep)
4ed46869 2757 unsigned char *src, *src_end;
0a28aafb 2758 int multibytep;
4ed46869 2759{
b73bfc1c
KH
2760 int c;
2761 /* Dummy for ONE_MORE_BYTE. */
2762 struct coding_system dummy_coding;
2763 struct coding_system *coding = &dummy_coding;
4ed46869 2764
b73bfc1c 2765 while (1)
4ed46869 2766 {
0a28aafb 2767 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
682169fe
KH
2768 if (c < 0x80)
2769 continue;
2770 if (c < 0xA1 || c > 0xFE)
2771 return 0;
2772 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
2773 if (c < 0x40 || (c > 0x7F && c < 0xA1) || c > 0xFE)
2774 return 0;
4ed46869 2775 }
b73bfc1c 2776 label_end_of_loop:
4ed46869
KH
2777 return CODING_CATEGORY_MASK_BIG5;
2778}
2779
fa42c37f
KH
2780/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2781 Check if a text is encoded in UTF-8. If it is, return
2782 CODING_CATEGORY_MASK_UTF_8, else return 0. */
2783
2784#define UTF_8_1_OCTET_P(c) ((c) < 0x80)
2785#define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
2786#define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
2787#define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
2788#define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
2789#define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
2790#define UTF_8_6_OCTET_LEADING_P(c) (((c) & 0xFE) == 0xFC)
2791
0a28aafb
KH
2792static int
2793detect_coding_utf_8 (src, src_end, multibytep)
fa42c37f 2794 unsigned char *src, *src_end;
0a28aafb 2795 int multibytep;
fa42c37f
KH
2796{
2797 unsigned char c;
2798 int seq_maybe_bytes;
b73bfc1c
KH
2799 /* Dummy for ONE_MORE_BYTE. */
2800 struct coding_system dummy_coding;
2801 struct coding_system *coding = &dummy_coding;
fa42c37f 2802
b73bfc1c 2803 while (1)
fa42c37f 2804 {
0a28aafb 2805 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
fa42c37f
KH
2806 if (UTF_8_1_OCTET_P (c))
2807 continue;
2808 else if (UTF_8_2_OCTET_LEADING_P (c))
2809 seq_maybe_bytes = 1;
2810 else if (UTF_8_3_OCTET_LEADING_P (c))
2811 seq_maybe_bytes = 2;
2812 else if (UTF_8_4_OCTET_LEADING_P (c))
2813 seq_maybe_bytes = 3;
2814 else if (UTF_8_5_OCTET_LEADING_P (c))
2815 seq_maybe_bytes = 4;
2816 else if (UTF_8_6_OCTET_LEADING_P (c))
2817 seq_maybe_bytes = 5;
2818 else
2819 return 0;
2820
2821 do
2822 {
0a28aafb 2823 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
fa42c37f
KH
2824 if (!UTF_8_EXTRA_OCTET_P (c))
2825 return 0;
2826 seq_maybe_bytes--;
2827 }
2828 while (seq_maybe_bytes > 0);
2829 }
2830
b73bfc1c 2831 label_end_of_loop:
fa42c37f
KH
2832 return CODING_CATEGORY_MASK_UTF_8;
2833}
2834
2835/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2836 Check if a text is encoded in UTF-16 Big Endian (endian == 1) or
2837 Little Endian (otherwise). If it is, return
2838 CODING_CATEGORY_MASK_UTF_16_BE or CODING_CATEGORY_MASK_UTF_16_LE,
2839 else return 0. */
2840
2841#define UTF_16_INVALID_P(val) \
2842 (((val) == 0xFFFE) \
2843 || ((val) == 0xFFFF))
2844
2845#define UTF_16_HIGH_SURROGATE_P(val) \
2846 (((val) & 0xD800) == 0xD800)
2847
2848#define UTF_16_LOW_SURROGATE_P(val) \
2849 (((val) & 0xDC00) == 0xDC00)
2850
0a28aafb
KH
2851static int
2852detect_coding_utf_16 (src, src_end, multibytep)
fa42c37f 2853 unsigned char *src, *src_end;
0a28aafb 2854 int multibytep;
fa42c37f 2855{
b73bfc1c
KH
2856 unsigned char c1, c2;
2857 /* Dummy for TWO_MORE_BYTES. */
2858 struct coding_system dummy_coding;
2859 struct coding_system *coding = &dummy_coding;
fa42c37f 2860
0a28aafb
KH
2861 ONE_MORE_BYTE_CHECK_MULTIBYTE (c1, multibytep);
2862 ONE_MORE_BYTE_CHECK_MULTIBYTE (c2, multibytep);
b73bfc1c
KH
2863
2864 if ((c1 == 0xFF) && (c2 == 0xFE))
fa42c37f 2865 return CODING_CATEGORY_MASK_UTF_16_LE;
b73bfc1c 2866 else if ((c1 == 0xFE) && (c2 == 0xFF))
fa42c37f
KH
2867 return CODING_CATEGORY_MASK_UTF_16_BE;
2868
b73bfc1c 2869 label_end_of_loop:
fa42c37f
KH
2870 return 0;
2871}
2872
4ed46869
KH
2873/* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
2874 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
2875
b73bfc1c 2876static void
4ed46869 2877decode_coding_sjis_big5 (coding, source, destination,
d46c5b12 2878 src_bytes, dst_bytes, sjis_p)
4ed46869
KH
2879 struct coding_system *coding;
2880 unsigned char *source, *destination;
2881 int src_bytes, dst_bytes;
4ed46869
KH
2882 int sjis_p;
2883{
2884 unsigned char *src = source;
2885 unsigned char *src_end = source + src_bytes;
2886 unsigned char *dst = destination;
2887 unsigned char *dst_end = destination + dst_bytes;
b73bfc1c
KH
2888 /* SRC_BASE remembers the start position in source in each loop.
2889 The loop will be exited when there's not enough source code
2890 (within macro ONE_MORE_BYTE), or when there's not enough
2891 destination area to produce a character (within macro
2892 EMIT_CHAR). */
2893 unsigned char *src_base;
2894 Lisp_Object translation_table;
a5d301df 2895
b73bfc1c
KH
2896 if (NILP (Venable_character_translation))
2897 translation_table = Qnil;
2898 else
2899 {
2900 translation_table = coding->translation_table_for_decode;
2901 if (NILP (translation_table))
2902 translation_table = Vstandard_translation_table_for_decode;
2903 }
4ed46869 2904
d46c5b12 2905 coding->produced_char = 0;
b73bfc1c 2906 while (1)
4ed46869 2907 {
b73bfc1c
KH
2908 int c, charset, c1, c2;
2909
2910 src_base = src;
2911 ONE_MORE_BYTE (c1);
2912
2913 if (c1 < 0x80)
4ed46869 2914 {
b73bfc1c
KH
2915 charset = CHARSET_ASCII;
2916 if (c1 < 0x20)
4ed46869 2917 {
b73bfc1c 2918 if (c1 == '\r')
d46c5b12 2919 {
b73bfc1c 2920 if (coding->eol_type == CODING_EOL_CRLF)
d46c5b12 2921 {
b73bfc1c
KH
2922 ONE_MORE_BYTE (c2);
2923 if (c2 == '\n')
2924 c1 = c2;
2925 else if (coding->mode
2926 & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
2927 {
2928 coding->result = CODING_FINISH_INCONSISTENT_EOL;
2929 goto label_end_of_loop;
2930 }
2931 else
2932 /* To process C2 again, SRC is subtracted by 1. */
2933 src--;
d46c5b12 2934 }
b73bfc1c
KH
2935 else if (coding->eol_type == CODING_EOL_CR)
2936 c1 = '\n';
2937 }
2938 else if (c1 == '\n'
2939 && (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
2940 && (coding->eol_type == CODING_EOL_CR
2941 || coding->eol_type == CODING_EOL_CRLF))
2942 {
2943 coding->result = CODING_FINISH_INCONSISTENT_EOL;
2944 goto label_end_of_loop;
d46c5b12 2945 }
4ed46869 2946 }
4ed46869 2947 }
54f78171 2948 else
b73bfc1c 2949 {
4ed46869
KH
2950 if (sjis_p)
2951 {
682169fe 2952 if (c1 == 0x80 || c1 == 0xA0 || c1 > 0xEF)
b73bfc1c 2953 goto label_invalid_code;
682169fe 2954 if (c1 <= 0x9F || c1 >= 0xE0)
fb88bf2d 2955 {
54f78171
KH
2956 /* SJIS -> JISX0208 */
2957 ONE_MORE_BYTE (c2);
b73bfc1c
KH
2958 if (c2 < 0x40 || c2 == 0x7F || c2 > 0xFC)
2959 goto label_invalid_code;
2960 DECODE_SJIS (c1, c2, c1, c2);
2961 charset = charset_jisx0208;
5e34de15 2962 }
fb88bf2d 2963 else
b73bfc1c
KH
2964 /* SJIS -> JISX0201-Kana */
2965 charset = charset_katakana_jisx0201;
4ed46869 2966 }
fb88bf2d 2967 else
fb88bf2d 2968 {
54f78171 2969 /* BIG5 -> Big5 */
682169fe 2970 if (c1 < 0xA0 || c1 > 0xFE)
b73bfc1c
KH
2971 goto label_invalid_code;
2972 ONE_MORE_BYTE (c2);
2973 if (c2 < 0x40 || (c2 > 0x7E && c2 < 0xA1) || c2 > 0xFE)
2974 goto label_invalid_code;
2975 DECODE_BIG5 (c1, c2, charset, c1, c2);
4ed46869
KH
2976 }
2977 }
4ed46869 2978
b73bfc1c
KH
2979 c = DECODE_ISO_CHARACTER (charset, c1, c2);
2980 EMIT_CHAR (c);
fb88bf2d
KH
2981 continue;
2982
b73bfc1c
KH
2983 label_invalid_code:
2984 coding->errors++;
4ed46869 2985 src = src_base;
b73bfc1c
KH
2986 c = *src++;
2987 EMIT_CHAR (c);
fb88bf2d 2988 }
d46c5b12 2989
b73bfc1c
KH
2990 label_end_of_loop:
2991 coding->consumed = coding->consumed_char = src_base - source;
d46c5b12 2992 coding->produced = dst - destination;
b73bfc1c 2993 return;
4ed46869
KH
2994}
2995
2996/* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
b73bfc1c
KH
2997 This function can encode charsets `ascii', `katakana-jisx0201',
2998 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
2999 are sure that all these charsets are registered as official charset
4ed46869
KH
3000 (i.e. do not have extended leading-codes). Characters of other
3001 charsets are produced without any encoding. If SJIS_P is 1, encode
3002 SJIS text, else encode BIG5 text. */
3003
b73bfc1c 3004static void
4ed46869 3005encode_coding_sjis_big5 (coding, source, destination,
d46c5b12 3006 src_bytes, dst_bytes, sjis_p)
4ed46869
KH
3007 struct coding_system *coding;
3008 unsigned char *source, *destination;
3009 int src_bytes, dst_bytes;
4ed46869
KH
3010 int sjis_p;
3011{
3012 unsigned char *src = source;
3013 unsigned char *src_end = source + src_bytes;
3014 unsigned char *dst = destination;
3015 unsigned char *dst_end = destination + dst_bytes;
b73bfc1c
KH
3016 /* SRC_BASE remembers the start position in source in each loop.
3017 The loop will be exited when there's not enough source text to
3018 analyze multi-byte codes (within macro ONE_MORE_CHAR), or when
3019 there's not enough destination area to produce encoded codes
3020 (within macro EMIT_BYTES). */
3021 unsigned char *src_base;
3022 Lisp_Object translation_table;
4ed46869 3023
b73bfc1c
KH
3024 if (NILP (Venable_character_translation))
3025 translation_table = Qnil;
3026 else
4ed46869 3027 {
39658efc 3028 translation_table = coding->translation_table_for_encode;
b73bfc1c 3029 if (NILP (translation_table))
39658efc 3030 translation_table = Vstandard_translation_table_for_encode;
b73bfc1c 3031 }
a5d301df 3032
b73bfc1c
KH
3033 while (1)
3034 {
3035 int c, charset, c1, c2;
4ed46869 3036
b73bfc1c
KH
3037 src_base = src;
3038 ONE_MORE_CHAR (c);
93dec019 3039
b73bfc1c
KH
3040 /* Now encode the character C. */
3041 if (SINGLE_BYTE_CHAR_P (c))
3042 {
3043 switch (c)
4ed46869 3044 {
b73bfc1c 3045 case '\r':
7371fe0a 3046 if (!(coding->mode & CODING_MODE_SELECTIVE_DISPLAY))
b73bfc1c
KH
3047 {
3048 EMIT_ONE_BYTE (c);
3049 break;
3050 }
3051 c = '\n';
3052 case '\n':
3053 if (coding->eol_type == CODING_EOL_CRLF)
3054 {
3055 EMIT_TWO_BYTES ('\r', c);
3056 break;
3057 }
3058 else if (coding->eol_type == CODING_EOL_CR)
3059 c = '\r';
3060 default:
3061 EMIT_ONE_BYTE (c);
3062 }
3063 }
3064 else
3065 {
3066 SPLIT_CHAR (c, charset, c1, c2);
3067 if (sjis_p)
3068 {
3069 if (charset == charset_jisx0208
3070 || charset == charset_jisx0208_1978)
3071 {
3072 ENCODE_SJIS (c1, c2, c1, c2);
3073 EMIT_TWO_BYTES (c1, c2);
3074 }
39658efc
KH
3075 else if (charset == charset_katakana_jisx0201)
3076 EMIT_ONE_BYTE (c1 | 0x80);
fc53a214
KH
3077 else if (charset == charset_latin_jisx0201)
3078 EMIT_ONE_BYTE (c1);
b73bfc1c
KH
3079 else
3080 /* There's no way other than producing the internal
3081 codes as is. */
3082 EMIT_BYTES (src_base, src);
4ed46869 3083 }
4ed46869 3084 else
b73bfc1c
KH
3085 {
3086 if (charset == charset_big5_1 || charset == charset_big5_2)
3087 {
3088 ENCODE_BIG5 (charset, c1, c2, c1, c2);
3089 EMIT_TWO_BYTES (c1, c2);
3090 }
3091 else
3092 /* There's no way other than producing the internal
3093 codes as is. */
3094 EMIT_BYTES (src_base, src);
3095 }
4ed46869 3096 }
b73bfc1c 3097 coding->consumed_char++;
4ed46869
KH
3098 }
3099
b73bfc1c
KH
3100 label_end_of_loop:
3101 coding->consumed = src_base - source;
d46c5b12 3102 coding->produced = coding->produced_char = dst - destination;
4ed46869
KH
3103}
3104
3105\f
1397dc18
KH
3106/*** 5. CCL handlers ***/
3107
3108/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
3109 Check if a text is encoded in a coding system of which
3110 encoder/decoder are written in CCL program. If it is, return
3111 CODING_CATEGORY_MASK_CCL, else return 0. */
3112
0a28aafb
KH
3113static int
3114detect_coding_ccl (src, src_end, multibytep)
1397dc18 3115 unsigned char *src, *src_end;
0a28aafb 3116 int multibytep;
1397dc18
KH
3117{
3118 unsigned char *valid;
b73bfc1c
KH
3119 int c;
3120 /* Dummy for ONE_MORE_BYTE. */
3121 struct coding_system dummy_coding;
3122 struct coding_system *coding = &dummy_coding;
1397dc18
KH
3123
3124 /* No coding system is assigned to coding-category-ccl. */
3125 if (!coding_system_table[CODING_CATEGORY_IDX_CCL])
3126 return 0;
3127
3128 valid = coding_system_table[CODING_CATEGORY_IDX_CCL]->spec.ccl.valid_codes;
b73bfc1c 3129 while (1)
1397dc18 3130 {
0a28aafb 3131 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
b73bfc1c
KH
3132 if (! valid[c])
3133 return 0;
1397dc18 3134 }
b73bfc1c 3135 label_end_of_loop:
1397dc18
KH
3136 return CODING_CATEGORY_MASK_CCL;
3137}
3138
3139\f
3140/*** 6. End-of-line handlers ***/
4ed46869 3141
b73bfc1c 3142/* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
4ed46869 3143
b73bfc1c 3144static void
d46c5b12 3145decode_eol (coding, source, destination, src_bytes, dst_bytes)
4ed46869
KH
3146 struct coding_system *coding;
3147 unsigned char *source, *destination;
3148 int src_bytes, dst_bytes;
4ed46869
KH
3149{
3150 unsigned char *src = source;
4ed46869 3151 unsigned char *dst = destination;
b73bfc1c
KH
3152 unsigned char *src_end = src + src_bytes;
3153 unsigned char *dst_end = dst + dst_bytes;
3154 Lisp_Object translation_table;
3155 /* SRC_BASE remembers the start position in source in each loop.
3156 The loop will be exited when there's not enough source code
3157 (within macro ONE_MORE_BYTE), or when there's not enough
3158 destination area to produce a character (within macro
3159 EMIT_CHAR). */
3160 unsigned char *src_base;
3161 int c;
3162
3163 translation_table = Qnil;
4ed46869
KH
3164 switch (coding->eol_type)
3165 {
3166 case CODING_EOL_CRLF:
b73bfc1c 3167 while (1)
d46c5b12 3168 {
b73bfc1c
KH
3169 src_base = src;
3170 ONE_MORE_BYTE (c);
3171 if (c == '\r')
fb88bf2d 3172 {
b73bfc1c
KH
3173 ONE_MORE_BYTE (c);
3174 if (c != '\n')
3175 {
3176 if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
3177 {
3178 coding->result = CODING_FINISH_INCONSISTENT_EOL;
3179 goto label_end_of_loop;
3180 }
3181 src--;
3182 c = '\r';
3183 }
fb88bf2d 3184 }
b73bfc1c
KH
3185 else if (c == '\n'
3186 && (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL))
d46c5b12 3187 {
b73bfc1c
KH
3188 coding->result = CODING_FINISH_INCONSISTENT_EOL;
3189 goto label_end_of_loop;
d46c5b12 3190 }
b73bfc1c 3191 EMIT_CHAR (c);
d46c5b12 3192 }
b73bfc1c
KH
3193 break;
3194
3195 case CODING_EOL_CR:
3196 while (1)
d46c5b12 3197 {
b73bfc1c
KH
3198 src_base = src;
3199 ONE_MORE_BYTE (c);
3200 if (c == '\n')
3201 {
3202 if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
3203 {
3204 coding->result = CODING_FINISH_INCONSISTENT_EOL;
3205 goto label_end_of_loop;
3206 }
3207 }
3208 else if (c == '\r')
3209 c = '\n';
3210 EMIT_CHAR (c);
d46c5b12 3211 }
4ed46869
KH
3212 break;
3213
b73bfc1c
KH
3214 default: /* no need for EOL handling */
3215 while (1)
d46c5b12 3216 {
b73bfc1c
KH
3217 src_base = src;
3218 ONE_MORE_BYTE (c);
3219 EMIT_CHAR (c);
d46c5b12 3220 }
4ed46869
KH
3221 }
3222
b73bfc1c
KH
3223 label_end_of_loop:
3224 coding->consumed = coding->consumed_char = src_base - source;
3225 coding->produced = dst - destination;
3226 return;
4ed46869
KH
3227}
3228
3229/* See "GENERAL NOTES about `encode_coding_XXX ()' functions". Encode
b73bfc1c 3230 format of end-of-line according to `coding->eol_type'. It also
8ca3766a 3231 convert multibyte form 8-bit characters to unibyte if
b73bfc1c
KH
3232 CODING->src_multibyte is nonzero. If `coding->mode &
3233 CODING_MODE_SELECTIVE_DISPLAY' is nonzero, code '\r' in source text
3234 also means end-of-line. */
4ed46869 3235
b73bfc1c 3236static void
d46c5b12 3237encode_eol (coding, source, destination, src_bytes, dst_bytes)
4ed46869
KH
3238 struct coding_system *coding;
3239 unsigned char *source, *destination;
3240 int src_bytes, dst_bytes;
4ed46869
KH
3241{
3242 unsigned char *src = source;
3243 unsigned char *dst = destination;
b73bfc1c
KH
3244 unsigned char *src_end = src + src_bytes;
3245 unsigned char *dst_end = dst + dst_bytes;
3246 Lisp_Object translation_table;
3247 /* SRC_BASE remembers the start position in source in each loop.
3248 The loop will be exited when there's not enough source text to
3249 analyze multi-byte codes (within macro ONE_MORE_CHAR), or when
3250 there's not enough destination area to produce encoded codes
3251 (within macro EMIT_BYTES). */
3252 unsigned char *src_base;
3253 int c;
3254 int selective_display = coding->mode & CODING_MODE_SELECTIVE_DISPLAY;
3255
3256 translation_table = Qnil;
3257 if (coding->src_multibyte
3258 && *(src_end - 1) == LEADING_CODE_8_BIT_CONTROL)
3259 {
3260 src_end--;
3261 src_bytes--;
3262 coding->result = CODING_FINISH_INSUFFICIENT_SRC;
3263 }
fb88bf2d 3264
d46c5b12
KH
3265 if (coding->eol_type == CODING_EOL_CRLF)
3266 {
b73bfc1c 3267 while (src < src_end)
d46c5b12 3268 {
b73bfc1c 3269 src_base = src;
d46c5b12 3270 c = *src++;
b73bfc1c
KH
3271 if (c >= 0x20)
3272 EMIT_ONE_BYTE (c);
3273 else if (c == '\n' || (c == '\r' && selective_display))
3274 EMIT_TWO_BYTES ('\r', '\n');
d46c5b12 3275 else
b73bfc1c 3276 EMIT_ONE_BYTE (c);
d46c5b12 3277 }
ff2b1ea9 3278 src_base = src;
b73bfc1c 3279 label_end_of_loop:
005f0d35 3280 ;
d46c5b12
KH
3281 }
3282 else
4ed46869 3283 {
78a629d2 3284 if (!dst_bytes || src_bytes <= dst_bytes)
4ed46869 3285 {
b73bfc1c
KH
3286 safe_bcopy (src, dst, src_bytes);
3287 src_base = src_end;
3288 dst += src_bytes;
d46c5b12 3289 }
d46c5b12 3290 else
b73bfc1c
KH
3291 {
3292 if (coding->src_multibyte
3293 && *(src + dst_bytes - 1) == LEADING_CODE_8_BIT_CONTROL)
3294 dst_bytes--;
3295 safe_bcopy (src, dst, dst_bytes);
3296 src_base = src + dst_bytes;
3297 dst = destination + dst_bytes;
3298 coding->result = CODING_FINISH_INSUFFICIENT_DST;
3299 }
993824c9 3300 if (coding->eol_type == CODING_EOL_CR)
d46c5b12 3301 {
b73bfc1c
KH
3302 for (src = destination; src < dst; src++)
3303 if (*src == '\n') *src = '\r';
d46c5b12 3304 }
b73bfc1c 3305 else if (selective_display)
d46c5b12 3306 {
b73bfc1c
KH
3307 for (src = destination; src < dst; src++)
3308 if (*src == '\r') *src = '\n';
4ed46869 3309 }
4ed46869 3310 }
b73bfc1c
KH
3311 if (coding->src_multibyte)
3312 dst = destination + str_as_unibyte (destination, dst - destination);
4ed46869 3313
b73bfc1c
KH
3314 coding->consumed = src_base - source;
3315 coding->produced = dst - destination;
78a629d2 3316 coding->produced_char = coding->produced;
4ed46869
KH
3317}
3318
3319\f
1397dc18 3320/*** 7. C library functions ***/
4ed46869 3321
cfb43547 3322/* In Emacs Lisp, a coding system is represented by a Lisp symbol which
4ed46869 3323 has a property `coding-system'. The value of this property is a
cfb43547 3324 vector of length 5 (called the coding-vector). Among elements of
4ed46869
KH
3325 this vector, the first (element[0]) and the fifth (element[4])
3326 carry important information for decoding/encoding. Before
3327 decoding/encoding, this information should be set in fields of a
3328 structure of type `coding_system'.
3329
cfb43547 3330 The value of the property `coding-system' can be a symbol of another
4ed46869
KH
3331 subsidiary coding-system. In that case, Emacs gets coding-vector
3332 from that symbol.
3333
3334 `element[0]' contains information to be set in `coding->type'. The
3335 value and its meaning is as follows:
3336
0ef69138
KH
3337 0 -- coding_type_emacs_mule
3338 1 -- coding_type_sjis
3339 2 -- coding_type_iso2022
3340 3 -- coding_type_big5
3341 4 -- coding_type_ccl encoder/decoder written in CCL
3342 nil -- coding_type_no_conversion
3343 t -- coding_type_undecided (automatic conversion on decoding,
3344 no-conversion on encoding)
4ed46869
KH
3345
3346 `element[4]' contains information to be set in `coding->flags' and
3347 `coding->spec'. The meaning varies by `coding->type'.
3348
3349 If `coding->type' is `coding_type_iso2022', element[4] is a vector
3350 of length 32 (of which the first 13 sub-elements are used now).
3351 Meanings of these sub-elements are:
3352
3353 sub-element[N] where N is 0 through 3: to be set in `coding->spec.iso2022'
3354 If the value is an integer of valid charset, the charset is
3355 assumed to be designated to graphic register N initially.
3356
3357 If the value is minus, it is a minus value of charset which
3358 reserves graphic register N, which means that the charset is
3359 not designated initially but should be designated to graphic
3360 register N just before encoding a character in that charset.
3361
3362 If the value is nil, graphic register N is never used on
3363 encoding.
93dec019 3364
4ed46869
KH
3365 sub-element[N] where N is 4 through 11: to be set in `coding->flags'
3366 Each value takes t or nil. See the section ISO2022 of
3367 `coding.h' for more information.
3368
3369 If `coding->type' is `coding_type_big5', element[4] is t to denote
3370 BIG5-ETen or nil to denote BIG5-HKU.
3371
3372 If `coding->type' takes the other value, element[4] is ignored.
3373
cfb43547 3374 Emacs Lisp's coding systems also carry information about format of
4ed46869
KH
3375 end-of-line in a value of property `eol-type'. If the value is
3376 integer, 0 means CODING_EOL_LF, 1 means CODING_EOL_CRLF, and 2
3377 means CODING_EOL_CR. If it is not integer, it should be a vector
3378 of subsidiary coding systems of which property `eol-type' has one
cfb43547 3379 of the above values.
4ed46869
KH
3380
3381*/
3382
3383/* Extract information for decoding/encoding from CODING_SYSTEM_SYMBOL
3384 and set it in CODING. If CODING_SYSTEM_SYMBOL is invalid, CODING
3385 is setup so that no conversion is necessary and return -1, else
3386 return 0. */
3387
3388int
e0e989f6
KH
3389setup_coding_system (coding_system, coding)
3390 Lisp_Object coding_system;
4ed46869
KH
3391 struct coding_system *coding;
3392{
d46c5b12 3393 Lisp_Object coding_spec, coding_type, eol_type, plist;
4608c386 3394 Lisp_Object val;
4ed46869 3395
c07c8e12
KH
3396 /* At first, zero clear all members. */
3397 bzero (coding, sizeof (struct coding_system));
3398
d46c5b12 3399 /* Initialize some fields required for all kinds of coding systems. */
774324d6 3400 coding->symbol = coding_system;
d46c5b12
KH
3401 coding->heading_ascii = -1;
3402 coding->post_read_conversion = coding->pre_write_conversion = Qnil;
ec6d2bb8
KH
3403 coding->composing = COMPOSITION_DISABLED;
3404 coding->cmp_data = NULL;
1f5dbf34
KH
3405
3406 if (NILP (coding_system))
3407 goto label_invalid_coding_system;
3408
4608c386 3409 coding_spec = Fget (coding_system, Qcoding_system);
1f5dbf34 3410
4608c386
KH
3411 if (!VECTORP (coding_spec)
3412 || XVECTOR (coding_spec)->size != 5
3413 || !CONSP (XVECTOR (coding_spec)->contents[3]))
4ed46869 3414 goto label_invalid_coding_system;
4608c386 3415
d46c5b12
KH
3416 eol_type = inhibit_eol_conversion ? Qnil : Fget (coding_system, Qeol_type);
3417 if (VECTORP (eol_type))
3418 {
3419 coding->eol_type = CODING_EOL_UNDECIDED;
3420 coding->common_flags = CODING_REQUIRE_DETECTION_MASK;
3421 }
3422 else if (XFASTINT (eol_type) == 1)
3423 {
3424 coding->eol_type = CODING_EOL_CRLF;
3425 coding->common_flags
3426 = CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
3427 }
3428 else if (XFASTINT (eol_type) == 2)
3429 {
3430 coding->eol_type = CODING_EOL_CR;
3431 coding->common_flags
3432 = CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
3433 }
3434 else
3435 coding->eol_type = CODING_EOL_LF;
3436
3437 coding_type = XVECTOR (coding_spec)->contents[0];
3438 /* Try short cut. */
3439 if (SYMBOLP (coding_type))
3440 {
3441 if (EQ (coding_type, Qt))
3442 {
3443 coding->type = coding_type_undecided;
3444 coding->common_flags |= CODING_REQUIRE_DETECTION_MASK;
3445 }
3446 else
3447 coding->type = coding_type_no_conversion;
9b96232f
KH
3448 /* Initialize this member. Any thing other than
3449 CODING_CATEGORY_IDX_UTF_16_BE and
3450 CODING_CATEGORY_IDX_UTF_16_LE are ok because they have
3451 special treatment in detect_eol. */
3452 coding->category_idx = CODING_CATEGORY_IDX_EMACS_MULE;
3453
d46c5b12
KH
3454 return 0;
3455 }
3456
d46c5b12
KH
3457 /* Get values of coding system properties:
3458 `post-read-conversion', `pre-write-conversion',
f967223b 3459 `translation-table-for-decode', `translation-table-for-encode'. */
4608c386 3460 plist = XVECTOR (coding_spec)->contents[3];
b843d1ae 3461 /* Pre & post conversion functions should be disabled if
8ca3766a 3462 inhibit_eol_conversion is nonzero. This is the case that a code
b843d1ae
KH
3463 conversion function is called while those functions are running. */
3464 if (! inhibit_pre_post_conversion)
3465 {
3466 coding->post_read_conversion = Fplist_get (plist, Qpost_read_conversion);
3467 coding->pre_write_conversion = Fplist_get (plist, Qpre_write_conversion);
3468 }
f967223b 3469 val = Fplist_get (plist, Qtranslation_table_for_decode);
4608c386 3470 if (SYMBOLP (val))
f967223b
KH
3471 val = Fget (val, Qtranslation_table_for_decode);
3472 coding->translation_table_for_decode = CHAR_TABLE_P (val) ? val : Qnil;
3473 val = Fplist_get (plist, Qtranslation_table_for_encode);
4608c386 3474 if (SYMBOLP (val))
f967223b
KH
3475 val = Fget (val, Qtranslation_table_for_encode);
3476 coding->translation_table_for_encode = CHAR_TABLE_P (val) ? val : Qnil;
d46c5b12
KH
3477 val = Fplist_get (plist, Qcoding_category);
3478 if (!NILP (val))
3479 {
3480 val = Fget (val, Qcoding_category_index);
3481 if (INTEGERP (val))
3482 coding->category_idx = XINT (val);
3483 else
3484 goto label_invalid_coding_system;
3485 }
3486 else
3487 goto label_invalid_coding_system;
93dec019 3488
ec6d2bb8
KH
3489 /* If the coding system has non-nil `composition' property, enable
3490 composition handling. */
3491 val = Fplist_get (plist, Qcomposition);
3492 if (!NILP (val))
3493 coding->composing = COMPOSITION_NO;
3494
d46c5b12 3495 switch (XFASTINT (coding_type))
4ed46869
KH
3496 {
3497 case 0:
0ef69138 3498 coding->type = coding_type_emacs_mule;
aa72b389
KH
3499 coding->common_flags
3500 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
3501 coding->composing = COMPOSITION_NO;
c952af22
KH
3502 if (!NILP (coding->post_read_conversion))
3503 coding->common_flags |= CODING_REQUIRE_DECODING_MASK;
3504 if (!NILP (coding->pre_write_conversion))
3505 coding->common_flags |= CODING_REQUIRE_ENCODING_MASK;
4ed46869
KH
3506 break;
3507
3508 case 1:
3509 coding->type = coding_type_sjis;
c952af22
KH
3510 coding->common_flags
3511 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
4ed46869
KH
3512 break;
3513
3514 case 2:
3515 coding->type = coding_type_iso2022;
c952af22
KH
3516 coding->common_flags
3517 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
4ed46869 3518 {
70c22245 3519 Lisp_Object val, temp;
4ed46869 3520 Lisp_Object *flags;
d46c5b12 3521 int i, charset, reg_bits = 0;
4ed46869 3522
4608c386 3523 val = XVECTOR (coding_spec)->contents[4];
f44d27ce 3524
4ed46869
KH
3525 if (!VECTORP (val) || XVECTOR (val)->size != 32)
3526 goto label_invalid_coding_system;
3527
3528 flags = XVECTOR (val)->contents;
3529 coding->flags
3530 = ((NILP (flags[4]) ? 0 : CODING_FLAG_ISO_SHORT_FORM)
3531 | (NILP (flags[5]) ? 0 : CODING_FLAG_ISO_RESET_AT_EOL)
3532 | (NILP (flags[6]) ? 0 : CODING_FLAG_ISO_RESET_AT_CNTL)
3533 | (NILP (flags[7]) ? 0 : CODING_FLAG_ISO_SEVEN_BITS)
3534 | (NILP (flags[8]) ? 0 : CODING_FLAG_ISO_LOCKING_SHIFT)
3535 | (NILP (flags[9]) ? 0 : CODING_FLAG_ISO_SINGLE_SHIFT)
3536 | (NILP (flags[10]) ? 0 : CODING_FLAG_ISO_USE_ROMAN)
3537 | (NILP (flags[11]) ? 0 : CODING_FLAG_ISO_USE_OLDJIS)
e0e989f6
KH
3538 | (NILP (flags[12]) ? 0 : CODING_FLAG_ISO_NO_DIRECTION)
3539 | (NILP (flags[13]) ? 0 : CODING_FLAG_ISO_INIT_AT_BOL)
c4825358
KH
3540 | (NILP (flags[14]) ? 0 : CODING_FLAG_ISO_DESIGNATE_AT_BOL)
3541 | (NILP (flags[15]) ? 0 : CODING_FLAG_ISO_SAFE)
3f003981 3542 | (NILP (flags[16]) ? 0 : CODING_FLAG_ISO_LATIN_EXTRA)
c4825358 3543 );
4ed46869
KH
3544
3545 /* Invoke graphic register 0 to plane 0. */
3546 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0;
3547 /* Invoke graphic register 1 to plane 1 if we can use full 8-bit. */
3548 CODING_SPEC_ISO_INVOCATION (coding, 1)
3549 = (coding->flags & CODING_FLAG_ISO_SEVEN_BITS ? -1 : 1);
3550 /* Not single shifting at first. */
6e85d753 3551 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0;
e0e989f6 3552 /* Beginning of buffer should also be regarded as bol. */
6e85d753 3553 CODING_SPEC_ISO_BOL (coding) = 1;
4ed46869 3554
70c22245
KH
3555 for (charset = 0; charset <= MAX_CHARSET; charset++)
3556 CODING_SPEC_ISO_REVISION_NUMBER (coding, charset) = 255;
3557 val = Vcharset_revision_alist;
3558 while (CONSP (val))
3559 {
03699b14 3560 charset = get_charset_id (Fcar_safe (XCAR (val)));
70c22245 3561 if (charset >= 0
03699b14 3562 && (temp = Fcdr_safe (XCAR (val)), INTEGERP (temp))
70c22245
KH
3563 && (i = XINT (temp), (i >= 0 && (i + '@') < 128)))
3564 CODING_SPEC_ISO_REVISION_NUMBER (coding, charset) = i;
03699b14 3565 val = XCDR (val);
70c22245
KH
3566 }
3567
4ed46869
KH
3568 /* Checks FLAGS[REG] (REG = 0, 1, 2 3) and decide designations.
3569 FLAGS[REG] can be one of below:
3570 integer CHARSET: CHARSET occupies register I,
3571 t: designate nothing to REG initially, but can be used
3572 by any charsets,
3573 list of integer, nil, or t: designate the first
3574 element (if integer) to REG initially, the remaining
3575 elements (if integer) is designated to REG on request,
d46c5b12 3576 if an element is t, REG can be used by any charsets,
4ed46869 3577 nil: REG is never used. */
467e7675 3578 for (charset = 0; charset <= MAX_CHARSET; charset++)
1ba9e4ab
KH
3579 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3580 = CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION;
4ed46869
KH
3581 for (i = 0; i < 4; i++)
3582 {
87323294
PJ
3583 if ((INTEGERP (flags[i])
3584 && (charset = XINT (flags[i]), CHARSET_VALID_P (charset)))
e0e989f6 3585 || (charset = get_charset_id (flags[i])) >= 0)
4ed46869
KH
3586 {
3587 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = charset;
3588 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) = i;
3589 }
3590 else if (EQ (flags[i], Qt))
3591 {
3592 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = -1;
d46c5b12
KH
3593 reg_bits |= 1 << i;
3594 coding->flags |= CODING_FLAG_ISO_DESIGNATION;
4ed46869
KH
3595 }
3596 else if (CONSP (flags[i]))
3597 {
84d60297
RS
3598 Lisp_Object tail;
3599 tail = flags[i];
4ed46869 3600
d46c5b12 3601 coding->flags |= CODING_FLAG_ISO_DESIGNATION;
87323294
PJ
3602 if ((INTEGERP (XCAR (tail))
3603 && (charset = XINT (XCAR (tail)),
3604 CHARSET_VALID_P (charset)))
03699b14 3605 || (charset = get_charset_id (XCAR (tail))) >= 0)
4ed46869
KH
3606 {
3607 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = charset;
3608 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) =i;
3609 }
3610 else
3611 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = -1;
03699b14 3612 tail = XCDR (tail);
4ed46869
KH
3613 while (CONSP (tail))
3614 {
87323294
PJ
3615 if ((INTEGERP (XCAR (tail))
3616 && (charset = XINT (XCAR (tail)),
3617 CHARSET_VALID_P (charset)))
03699b14 3618 || (charset = get_charset_id (XCAR (tail))) >= 0)
70c22245
KH
3619 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3620 = i;
03699b14 3621 else if (EQ (XCAR (tail), Qt))
d46c5b12 3622 reg_bits |= 1 << i;
03699b14 3623 tail = XCDR (tail);
4ed46869
KH
3624 }
3625 }
3626 else
3627 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = -1;
93dec019 3628
4ed46869
KH
3629 CODING_SPEC_ISO_DESIGNATION (coding, i)
3630 = CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i);
3631 }
3632
d46c5b12 3633 if (reg_bits && ! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT))
4ed46869
KH
3634 {
3635 /* REG 1 can be used only by locking shift in 7-bit env. */
3636 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS)
d46c5b12 3637 reg_bits &= ~2;
4ed46869
KH
3638 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT))
3639 /* Without any shifting, only REG 0 and 1 can be used. */
d46c5b12 3640 reg_bits &= 3;
4ed46869
KH
3641 }
3642
d46c5b12
KH
3643 if (reg_bits)
3644 for (charset = 0; charset <= MAX_CHARSET; charset++)
6e85d753 3645 {
928a85c1 3646 if (CHARSET_DEFINED_P (charset)
96148065
KH
3647 && (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3648 == CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION))
d46c5b12
KH
3649 {
3650 /* There exist some default graphic registers to be
96148065 3651 used by CHARSET. */
d46c5b12
KH
3652
3653 /* We had better avoid designating a charset of
3654 CHARS96 to REG 0 as far as possible. */
3655 if (CHARSET_CHARS (charset) == 96)
3656 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3657 = (reg_bits & 2
3658 ? 1 : (reg_bits & 4 ? 2 : (reg_bits & 8 ? 3 : 0)));
3659 else
3660 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3661 = (reg_bits & 1
3662 ? 0 : (reg_bits & 2 ? 1 : (reg_bits & 4 ? 2 : 3)));
3663 }
6e85d753 3664 }
4ed46869 3665 }
c952af22 3666 coding->common_flags |= CODING_REQUIRE_FLUSHING_MASK;
d46c5b12 3667 coding->spec.iso2022.last_invalid_designation_register = -1;
4ed46869
KH
3668 break;
3669
3670 case 3:
3671 coding->type = coding_type_big5;
c952af22
KH
3672 coding->common_flags
3673 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
4ed46869 3674 coding->flags
4608c386 3675 = (NILP (XVECTOR (coding_spec)->contents[4])
4ed46869
KH
3676 ? CODING_FLAG_BIG5_HKU
3677 : CODING_FLAG_BIG5_ETEN);
3678 break;
3679
3680 case 4:
3681 coding->type = coding_type_ccl;
c952af22
KH
3682 coding->common_flags
3683 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
4ed46869 3684 {
84d60297 3685 val = XVECTOR (coding_spec)->contents[4];
ef4ced28
KH
3686 if (! CONSP (val)
3687 || setup_ccl_program (&(coding->spec.ccl.decoder),
03699b14 3688 XCAR (val)) < 0
ef4ced28 3689 || setup_ccl_program (&(coding->spec.ccl.encoder),
03699b14 3690 XCDR (val)) < 0)
4ed46869 3691 goto label_invalid_coding_system;
1397dc18
KH
3692
3693 bzero (coding->spec.ccl.valid_codes, 256);
3694 val = Fplist_get (plist, Qvalid_codes);
3695 if (CONSP (val))
3696 {
3697 Lisp_Object this;
3698
03699b14 3699 for (; CONSP (val); val = XCDR (val))
1397dc18 3700 {
03699b14 3701 this = XCAR (val);
1397dc18
KH
3702 if (INTEGERP (this)
3703 && XINT (this) >= 0 && XINT (this) < 256)
3704 coding->spec.ccl.valid_codes[XINT (this)] = 1;
3705 else if (CONSP (this)
03699b14
KR
3706 && INTEGERP (XCAR (this))
3707 && INTEGERP (XCDR (this)))
1397dc18 3708 {
03699b14
KR
3709 int start = XINT (XCAR (this));
3710 int end = XINT (XCDR (this));
1397dc18
KH
3711
3712 if (start >= 0 && start <= end && end < 256)
e133c8fa 3713 while (start <= end)
1397dc18
KH
3714 coding->spec.ccl.valid_codes[start++] = 1;
3715 }
3716 }
3717 }
4ed46869 3718 }
c952af22 3719 coding->common_flags |= CODING_REQUIRE_FLUSHING_MASK;
aaaf0b1e 3720 coding->spec.ccl.cr_carryover = 0;
1c3478b0 3721 coding->spec.ccl.eight_bit_carryover[0] = 0;
4ed46869
KH
3722 break;
3723
27901516
KH
3724 case 5:
3725 coding->type = coding_type_raw_text;
3726 break;
3727
4ed46869 3728 default:
d46c5b12 3729 goto label_invalid_coding_system;
4ed46869
KH
3730 }
3731 return 0;
3732
3733 label_invalid_coding_system:
3734 coding->type = coding_type_no_conversion;
d46c5b12 3735 coding->category_idx = CODING_CATEGORY_IDX_BINARY;
c952af22 3736 coding->common_flags = 0;
dec137e5 3737 coding->eol_type = CODING_EOL_LF;
d46c5b12 3738 coding->pre_write_conversion = coding->post_read_conversion = Qnil;
4ed46869
KH
3739 return -1;
3740}
3741
ec6d2bb8
KH
3742/* Free memory blocks allocated for storing composition information. */
3743
3744void
3745coding_free_composition_data (coding)
3746 struct coding_system *coding;
3747{
3748 struct composition_data *cmp_data = coding->cmp_data, *next;
3749
3750 if (!cmp_data)
3751 return;
3752 /* Memory blocks are chained. At first, rewind to the first, then,
3753 free blocks one by one. */
3754 while (cmp_data->prev)
3755 cmp_data = cmp_data->prev;
3756 while (cmp_data)
3757 {
3758 next = cmp_data->next;
3759 xfree (cmp_data);
3760 cmp_data = next;
3761 }
3762 coding->cmp_data = NULL;
3763}
3764
3765/* Set `char_offset' member of all memory blocks pointed by
3766 coding->cmp_data to POS. */
3767
3768void
3769coding_adjust_composition_offset (coding, pos)
3770 struct coding_system *coding;
3771 int pos;
3772{
3773 struct composition_data *cmp_data;
3774
3775 for (cmp_data = coding->cmp_data; cmp_data; cmp_data = cmp_data->next)
3776 cmp_data->char_offset = pos;
3777}
3778
54f78171
KH
3779/* Setup raw-text or one of its subsidiaries in the structure
3780 coding_system CODING according to the already setup value eol_type
3781 in CODING. CODING should be setup for some coding system in
3782 advance. */
3783
3784void
3785setup_raw_text_coding_system (coding)
3786 struct coding_system *coding;
3787{
3788 if (coding->type != coding_type_raw_text)
3789 {
3790 coding->symbol = Qraw_text;
3791 coding->type = coding_type_raw_text;
3792 if (coding->eol_type != CODING_EOL_UNDECIDED)
3793 {
84d60297
RS
3794 Lisp_Object subsidiaries;
3795 subsidiaries = Fget (Qraw_text, Qeol_type);
54f78171
KH
3796
3797 if (VECTORP (subsidiaries)
3798 && XVECTOR (subsidiaries)->size == 3)
3799 coding->symbol
3800 = XVECTOR (subsidiaries)->contents[coding->eol_type];
3801 }
716e0b0a 3802 setup_coding_system (coding->symbol, coding);
54f78171
KH
3803 }
3804 return;
3805}
3806
4ed46869
KH
3807/* Emacs has a mechanism to automatically detect a coding system if it
3808 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
3809 it's impossible to distinguish some coding systems accurately
3810 because they use the same range of codes. So, at first, coding
3811 systems are categorized into 7, those are:
3812
0ef69138 3813 o coding-category-emacs-mule
4ed46869
KH
3814
3815 The category for a coding system which has the same code range
3816 as Emacs' internal format. Assigned the coding-system (Lisp
0ef69138 3817 symbol) `emacs-mule' by default.
4ed46869
KH
3818
3819 o coding-category-sjis
3820
3821 The category for a coding system which has the same code range
3822 as SJIS. Assigned the coding-system (Lisp
7717c392 3823 symbol) `japanese-shift-jis' by default.
4ed46869
KH
3824
3825 o coding-category-iso-7
3826
3827 The category for a coding system which has the same code range
7717c392 3828 as ISO2022 of 7-bit environment. This doesn't use any locking
d46c5b12
KH
3829 shift and single shift functions. This can encode/decode all
3830 charsets. Assigned the coding-system (Lisp symbol)
3831 `iso-2022-7bit' by default.
3832
3833 o coding-category-iso-7-tight
3834
3835 Same as coding-category-iso-7 except that this can
3836 encode/decode only the specified charsets.
4ed46869
KH
3837
3838 o coding-category-iso-8-1
3839
3840 The category for a coding system which has the same code range
3841 as ISO2022 of 8-bit environment and graphic plane 1 used only
7717c392
KH
3842 for DIMENSION1 charset. This doesn't use any locking shift
3843 and single shift functions. Assigned the coding-system (Lisp
3844 symbol) `iso-latin-1' by default.
4ed46869
KH
3845
3846 o coding-category-iso-8-2
3847
3848 The category for a coding system which has the same code range
3849 as ISO2022 of 8-bit environment and graphic plane 1 used only
7717c392
KH
3850 for DIMENSION2 charset. This doesn't use any locking shift
3851 and single shift functions. Assigned the coding-system (Lisp
3852 symbol) `japanese-iso-8bit' by default.
4ed46869 3853
7717c392 3854 o coding-category-iso-7-else
4ed46869
KH
3855
3856 The category for a coding system which has the same code range
8ca3766a 3857 as ISO2022 of 7-bit environment but uses locking shift or
7717c392
KH
3858 single shift functions. Assigned the coding-system (Lisp
3859 symbol) `iso-2022-7bit-lock' by default.
3860
3861 o coding-category-iso-8-else
3862
3863 The category for a coding system which has the same code range
8ca3766a 3864 as ISO2022 of 8-bit environment but uses locking shift or
7717c392
KH
3865 single shift functions. Assigned the coding-system (Lisp
3866 symbol) `iso-2022-8bit-ss2' by default.
4ed46869
KH
3867
3868 o coding-category-big5
3869
3870 The category for a coding system which has the same code range
3871 as BIG5. Assigned the coding-system (Lisp symbol)
e0e989f6 3872 `cn-big5' by default.
4ed46869 3873
fa42c37f
KH
3874 o coding-category-utf-8
3875
3876 The category for a coding system which has the same code range
3877 as UTF-8 (cf. RFC2279). Assigned the coding-system (Lisp
3878 symbol) `utf-8' by default.
3879
3880 o coding-category-utf-16-be
3881
3882 The category for a coding system in which a text has an
3883 Unicode signature (cf. Unicode Standard) in the order of BIG
3884 endian at the head. Assigned the coding-system (Lisp symbol)
3885 `utf-16-be' by default.
3886
3887 o coding-category-utf-16-le
3888
3889 The category for a coding system in which a text has an
3890 Unicode signature (cf. Unicode Standard) in the order of
3891 LITTLE endian at the head. Assigned the coding-system (Lisp
3892 symbol) `utf-16-le' by default.
3893
1397dc18
KH
3894 o coding-category-ccl
3895
3896 The category for a coding system of which encoder/decoder is
3897 written in CCL programs. The default value is nil, i.e., no
3898 coding system is assigned.
3899
4ed46869
KH
3900 o coding-category-binary
3901
3902 The category for a coding system not categorized in any of the
3903 above. Assigned the coding-system (Lisp symbol)
e0e989f6 3904 `no-conversion' by default.
4ed46869
KH
3905
3906 Each of them is a Lisp symbol and the value is an actual
cfb43547 3907 `coding-system' (this is also a Lisp symbol) assigned by a user.
4ed46869
KH
3908 What Emacs does actually is to detect a category of coding system.
3909 Then, it uses a `coding-system' assigned to it. If Emacs can't
cfb43547 3910 decide a single possible category, it selects a category of the
4ed46869
KH
3911 highest priority. Priorities of categories are also specified by a
3912 user in a Lisp variable `coding-category-list'.
3913
3914*/
3915
66cfb530
KH
3916static
3917int ascii_skip_code[256];
3918
d46c5b12 3919/* Detect how a text of length SRC_BYTES pointed by SOURCE is encoded.
4ed46869
KH
3920 If it detects possible coding systems, return an integer in which
3921 appropriate flag bits are set. Flag bits are defined by macros
fa42c37f
KH
3922 CODING_CATEGORY_MASK_XXX in `coding.h'. If PRIORITIES is non-NULL,
3923 it should point the table `coding_priorities'. In that case, only
3924 the flag bit for a coding system of the highest priority is set in
0a28aafb
KH
3925 the returned value. If MULTIBYTEP is nonzero, 8-bit codes of the
3926 range 0x80..0x9F are in multibyte form.
4ed46869 3927
d46c5b12
KH
3928 How many ASCII characters are at the head is returned as *SKIP. */
3929
3930static int
0a28aafb 3931detect_coding_mask (source, src_bytes, priorities, skip, multibytep)
d46c5b12
KH
3932 unsigned char *source;
3933 int src_bytes, *priorities, *skip;
0a28aafb 3934 int multibytep;
4ed46869
KH
3935{
3936 register unsigned char c;
d46c5b12 3937 unsigned char *src = source, *src_end = source + src_bytes;
fa42c37f 3938 unsigned int mask, utf16_examined_p, iso2022_examined_p;
da55a2b7 3939 int i;
4ed46869
KH
3940
3941 /* At first, skip all ASCII characters and control characters except
3942 for three ISO2022 specific control characters. */
66cfb530
KH
3943 ascii_skip_code[ISO_CODE_SO] = 0;
3944 ascii_skip_code[ISO_CODE_SI] = 0;
3945 ascii_skip_code[ISO_CODE_ESC] = 0;
3946
bcf26d6a 3947 label_loop_detect_coding:
66cfb530 3948 while (src < src_end && ascii_skip_code[*src]) src++;
d46c5b12 3949 *skip = src - source;
4ed46869
KH
3950
3951 if (src >= src_end)
3952 /* We found nothing other than ASCII. There's nothing to do. */
d46c5b12 3953 return 0;
4ed46869 3954
8a8147d6 3955 c = *src;
4ed46869
KH
3956 /* The text seems to be encoded in some multilingual coding system.
3957 Now, try to find in which coding system the text is encoded. */
3958 if (c < 0x80)
bcf26d6a
KH
3959 {
3960 /* i.e. (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) */
3961 /* C is an ISO2022 specific control code of C0. */
0a28aafb 3962 mask = detect_coding_iso2022 (src, src_end, multibytep);
1b2af4b0 3963 if (mask == 0)
d46c5b12
KH
3964 {
3965 /* No valid ISO2022 code follows C. Try again. */
3966 src++;
66cfb530
KH
3967 if (c == ISO_CODE_ESC)
3968 ascii_skip_code[ISO_CODE_ESC] = 1;
3969 else
3970 ascii_skip_code[ISO_CODE_SO] = ascii_skip_code[ISO_CODE_SI] = 1;
d46c5b12
KH
3971 goto label_loop_detect_coding;
3972 }
3973 if (priorities)
fa42c37f
KH
3974 {
3975 for (i = 0; i < CODING_CATEGORY_IDX_MAX; i++)
3976 {
3977 if (mask & priorities[i])
3978 return priorities[i];
3979 }
3980 return CODING_CATEGORY_MASK_RAW_TEXT;
3981 }
bcf26d6a 3982 }
d46c5b12 3983 else
c4825358 3984 {
d46c5b12 3985 int try;
4ed46869 3986
0a28aafb 3987 if (multibytep && c == LEADING_CODE_8_BIT_CONTROL)
67091e59 3988 c = src[1] - 0x20;
0a28aafb 3989
d46c5b12
KH
3990 if (c < 0xA0)
3991 {
3992 /* C is the first byte of SJIS character code,
fa42c37f
KH
3993 or a leading-code of Emacs' internal format (emacs-mule),
3994 or the first byte of UTF-16. */
3995 try = (CODING_CATEGORY_MASK_SJIS
3996 | CODING_CATEGORY_MASK_EMACS_MULE
3997 | CODING_CATEGORY_MASK_UTF_16_BE
3998 | CODING_CATEGORY_MASK_UTF_16_LE);
d46c5b12
KH
3999
4000 /* Or, if C is a special latin extra code,
93dec019 4001 or is an ISO2022 specific control code of C1 (SS2 or SS3),
d46c5b12
KH
4002 or is an ISO2022 control-sequence-introducer (CSI),
4003 we should also consider the possibility of ISO2022 codings. */
4004 if ((VECTORP (Vlatin_extra_code_table)
4005 && !NILP (XVECTOR (Vlatin_extra_code_table)->contents[c]))
4006 || (c == ISO_CODE_SS2 || c == ISO_CODE_SS3)
4007 || (c == ISO_CODE_CSI
4008 && (src < src_end
4009 && (*src == ']'
4010 || ((*src == '0' || *src == '1' || *src == '2')
4011 && src + 1 < src_end
4012 && src[1] == ']')))))
4013 try |= (CODING_CATEGORY_MASK_ISO_8_ELSE
4014 | CODING_CATEGORY_MASK_ISO_8BIT);
4015 }
c4825358 4016 else
d46c5b12
KH
4017 /* C is a character of ISO2022 in graphic plane right,
4018 or a SJIS's 1-byte character code (i.e. JISX0201),
fa42c37f
KH
4019 or the first byte of BIG5's 2-byte code,
4020 or the first byte of UTF-8/16. */
d46c5b12
KH
4021 try = (CODING_CATEGORY_MASK_ISO_8_ELSE
4022 | CODING_CATEGORY_MASK_ISO_8BIT
4023 | CODING_CATEGORY_MASK_SJIS
fa42c37f
KH
4024 | CODING_CATEGORY_MASK_BIG5
4025 | CODING_CATEGORY_MASK_UTF_8
4026 | CODING_CATEGORY_MASK_UTF_16_BE
4027 | CODING_CATEGORY_MASK_UTF_16_LE);
d46c5b12 4028
1397dc18
KH
4029 /* Or, we may have to consider the possibility of CCL. */
4030 if (coding_system_table[CODING_CATEGORY_IDX_CCL]
4031 && (coding_system_table[CODING_CATEGORY_IDX_CCL]
4032 ->spec.ccl.valid_codes)[c])
4033 try |= CODING_CATEGORY_MASK_CCL;
4034
d46c5b12 4035 mask = 0;
fa42c37f 4036 utf16_examined_p = iso2022_examined_p = 0;
d46c5b12
KH
4037 if (priorities)
4038 {
4039 for (i = 0; i < CODING_CATEGORY_IDX_MAX; i++)
4040 {
fa42c37f
KH
4041 if (!iso2022_examined_p
4042 && (priorities[i] & try & CODING_CATEGORY_MASK_ISO))
4043 {
0192762c 4044 mask |= detect_coding_iso2022 (src, src_end, multibytep);
fa42c37f
KH
4045 iso2022_examined_p = 1;
4046 }
5ab13dd0 4047 else if (priorities[i] & try & CODING_CATEGORY_MASK_SJIS)
0a28aafb 4048 mask |= detect_coding_sjis (src, src_end, multibytep);
fa42c37f 4049 else if (priorities[i] & try & CODING_CATEGORY_MASK_UTF_8)
0a28aafb 4050 mask |= detect_coding_utf_8 (src, src_end, multibytep);
fa42c37f
KH
4051 else if (!utf16_examined_p
4052 && (priorities[i] & try &
4053 CODING_CATEGORY_MASK_UTF_16_BE_LE))
4054 {
0a28aafb 4055 mask |= detect_coding_utf_16 (src, src_end, multibytep);
fa42c37f
KH
4056 utf16_examined_p = 1;
4057 }
5ab13dd0 4058 else if (priorities[i] & try & CODING_CATEGORY_MASK_BIG5)
0a28aafb 4059 mask |= detect_coding_big5 (src, src_end, multibytep);
5ab13dd0 4060 else if (priorities[i] & try & CODING_CATEGORY_MASK_EMACS_MULE)
0a28aafb 4061 mask |= detect_coding_emacs_mule (src, src_end, multibytep);
89fa8b36 4062 else if (priorities[i] & try & CODING_CATEGORY_MASK_CCL)
0a28aafb 4063 mask |= detect_coding_ccl (src, src_end, multibytep);
5ab13dd0 4064 else if (priorities[i] & CODING_CATEGORY_MASK_RAW_TEXT)
fa42c37f 4065 mask |= CODING_CATEGORY_MASK_RAW_TEXT;
5ab13dd0 4066 else if (priorities[i] & CODING_CATEGORY_MASK_BINARY)
fa42c37f
KH
4067 mask |= CODING_CATEGORY_MASK_BINARY;
4068 if (mask & priorities[i])
4069 return priorities[i];
d46c5b12
KH
4070 }
4071 return CODING_CATEGORY_MASK_RAW_TEXT;
4072 }
4073 if (try & CODING_CATEGORY_MASK_ISO)
0a28aafb 4074 mask |= detect_coding_iso2022 (src, src_end, multibytep);
d46c5b12 4075 if (try & CODING_CATEGORY_MASK_SJIS)
0a28aafb 4076 mask |= detect_coding_sjis (src, src_end, multibytep);
d46c5b12 4077 if (try & CODING_CATEGORY_MASK_BIG5)
0a28aafb 4078 mask |= detect_coding_big5 (src, src_end, multibytep);
fa42c37f 4079 if (try & CODING_CATEGORY_MASK_UTF_8)
0a28aafb 4080 mask |= detect_coding_utf_8 (src, src_end, multibytep);
fa42c37f 4081 if (try & CODING_CATEGORY_MASK_UTF_16_BE_LE)
0a28aafb 4082 mask |= detect_coding_utf_16 (src, src_end, multibytep);
d46c5b12 4083 if (try & CODING_CATEGORY_MASK_EMACS_MULE)
0a28aafb 4084 mask |= detect_coding_emacs_mule (src, src_end, multibytep);
1397dc18 4085 if (try & CODING_CATEGORY_MASK_CCL)
0a28aafb 4086 mask |= detect_coding_ccl (src, src_end, multibytep);
c4825358 4087 }
5ab13dd0 4088 return (mask | CODING_CATEGORY_MASK_RAW_TEXT | CODING_CATEGORY_MASK_BINARY);
4ed46869
KH
4089}
4090
4091/* Detect how a text of length SRC_BYTES pointed by SRC is encoded.
4092 The information of the detected coding system is set in CODING. */
4093
4094void
4095detect_coding (coding, src, src_bytes)
4096 struct coding_system *coding;
4097 unsigned char *src;
4098 int src_bytes;
4099{
d46c5b12 4100 unsigned int idx;
da55a2b7 4101 int skip, mask;
84d60297 4102 Lisp_Object val;
4ed46869 4103
84d60297 4104 val = Vcoding_category_list;
64c1e55f
KH
4105 mask = detect_coding_mask (src, src_bytes, coding_priorities, &skip,
4106 coding->src_multibyte);
d46c5b12 4107 coding->heading_ascii = skip;
4ed46869 4108
d46c5b12
KH
4109 if (!mask) return;
4110
4111 /* We found a single coding system of the highest priority in MASK. */
4112 idx = 0;
4113 while (mask && ! (mask & 1)) mask >>= 1, idx++;
4114 if (! mask)
4115 idx = CODING_CATEGORY_IDX_RAW_TEXT;
4ed46869 4116
f5c1dd0d 4117 val = SYMBOL_VALUE (XVECTOR (Vcoding_category_table)->contents[idx]);
d46c5b12
KH
4118
4119 if (coding->eol_type != CODING_EOL_UNDECIDED)
27901516 4120 {
84d60297 4121 Lisp_Object tmp;
d46c5b12 4122
84d60297 4123 tmp = Fget (val, Qeol_type);
d46c5b12
KH
4124 if (VECTORP (tmp))
4125 val = XVECTOR (tmp)->contents[coding->eol_type];
4ed46869 4126 }
b73bfc1c
KH
4127
4128 /* Setup this new coding system while preserving some slots. */
4129 {
4130 int src_multibyte = coding->src_multibyte;
4131 int dst_multibyte = coding->dst_multibyte;
4132
4133 setup_coding_system (val, coding);
4134 coding->src_multibyte = src_multibyte;
4135 coding->dst_multibyte = dst_multibyte;
4136 coding->heading_ascii = skip;
4137 }
4ed46869
KH
4138}
4139
d46c5b12
KH
4140/* Detect how end-of-line of a text of length SRC_BYTES pointed by
4141 SOURCE is encoded. Return one of CODING_EOL_LF, CODING_EOL_CRLF,
4142 CODING_EOL_CR, and CODING_EOL_UNDECIDED.
4143
4144 How many non-eol characters are at the head is returned as *SKIP. */
4ed46869 4145
bc4bc72a
RS
4146#define MAX_EOL_CHECK_COUNT 3
4147
d46c5b12
KH
4148static int
4149detect_eol_type (source, src_bytes, skip)
4150 unsigned char *source;
4151 int src_bytes, *skip;
4ed46869 4152{
d46c5b12 4153 unsigned char *src = source, *src_end = src + src_bytes;
4ed46869 4154 unsigned char c;
bc4bc72a
RS
4155 int total = 0; /* How many end-of-lines are found so far. */
4156 int eol_type = CODING_EOL_UNDECIDED;
4157 int this_eol_type;
4ed46869 4158
d46c5b12
KH
4159 *skip = 0;
4160
bc4bc72a 4161 while (src < src_end && total < MAX_EOL_CHECK_COUNT)
4ed46869
KH
4162 {
4163 c = *src++;
bc4bc72a 4164 if (c == '\n' || c == '\r')
4ed46869 4165 {
d46c5b12
KH
4166 if (*skip == 0)
4167 *skip = src - 1 - source;
bc4bc72a
RS
4168 total++;
4169 if (c == '\n')
4170 this_eol_type = CODING_EOL_LF;
4171 else if (src >= src_end || *src != '\n')
4172 this_eol_type = CODING_EOL_CR;
4ed46869 4173 else
bc4bc72a
RS
4174 this_eol_type = CODING_EOL_CRLF, src++;
4175
4176 if (eol_type == CODING_EOL_UNDECIDED)
4177 /* This is the first end-of-line. */
4178 eol_type = this_eol_type;
4179 else if (eol_type != this_eol_type)
d46c5b12
KH
4180 {
4181 /* The found type is different from what found before. */
4182 eol_type = CODING_EOL_INCONSISTENT;
4183 break;
4184 }
4ed46869
KH
4185 }
4186 }
bc4bc72a 4187
d46c5b12
KH
4188 if (*skip == 0)
4189 *skip = src_end - source;
85a02ca4 4190 return eol_type;
4ed46869
KH
4191}
4192
fa42c37f
KH
4193/* Like detect_eol_type, but detect EOL type in 2-octet
4194 big-endian/little-endian format for coding systems utf-16-be and
4195 utf-16-le. */
4196
4197static int
4198detect_eol_type_in_2_octet_form (source, src_bytes, skip, big_endian_p)
4199 unsigned char *source;
cfb43547 4200 int src_bytes, *skip, big_endian_p;
fa42c37f
KH
4201{
4202 unsigned char *src = source, *src_end = src + src_bytes;
4203 unsigned int c1, c2;
4204 int total = 0; /* How many end-of-lines are found so far. */
4205 int eol_type = CODING_EOL_UNDECIDED;
4206 int this_eol_type;
4207 int msb, lsb;
4208
4209 if (big_endian_p)
4210 msb = 0, lsb = 1;
4211 else
4212 msb = 1, lsb = 0;
4213
4214 *skip = 0;
4215
4216 while ((src + 1) < src_end && total < MAX_EOL_CHECK_COUNT)
4217 {
4218 c1 = (src[msb] << 8) | (src[lsb]);
4219 src += 2;
4220
4221 if (c1 == '\n' || c1 == '\r')
4222 {
4223 if (*skip == 0)
4224 *skip = src - 2 - source;
4225 total++;
4226 if (c1 == '\n')
4227 {
4228 this_eol_type = CODING_EOL_LF;
4229 }
4230 else
4231 {
4232 if ((src + 1) >= src_end)
4233 {
4234 this_eol_type = CODING_EOL_CR;
4235 }
4236 else
4237 {
4238 c2 = (src[msb] << 8) | (src[lsb]);
4239 if (c2 == '\n')
4240 this_eol_type = CODING_EOL_CRLF, src += 2;
4241 else
4242 this_eol_type = CODING_EOL_CR;
4243 }
4244 }
4245
4246 if (eol_type == CODING_EOL_UNDECIDED)
4247 /* This is the first end-of-line. */
4248 eol_type = this_eol_type;
4249 else if (eol_type != this_eol_type)
4250 {
4251 /* The found type is different from what found before. */
4252 eol_type = CODING_EOL_INCONSISTENT;
4253 break;
4254 }
4255 }
4256 }
4257
4258 if (*skip == 0)
4259 *skip = src_end - source;
4260 return eol_type;
4261}
4262
4ed46869
KH
4263/* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC
4264 is encoded. If it detects an appropriate format of end-of-line, it
4265 sets the information in *CODING. */
4266
4267void
4268detect_eol (coding, src, src_bytes)
4269 struct coding_system *coding;
4270 unsigned char *src;
4271 int src_bytes;
4272{
4608c386 4273 Lisp_Object val;
d46c5b12 4274 int skip;
fa42c37f
KH
4275 int eol_type;
4276
4277 switch (coding->category_idx)
4278 {
4279 case CODING_CATEGORY_IDX_UTF_16_BE:
4280 eol_type = detect_eol_type_in_2_octet_form (src, src_bytes, &skip, 1);
4281 break;
4282 case CODING_CATEGORY_IDX_UTF_16_LE:
4283 eol_type = detect_eol_type_in_2_octet_form (src, src_bytes, &skip, 0);
4284 break;
4285 default:
4286 eol_type = detect_eol_type (src, src_bytes, &skip);
4287 break;
4288 }
d46c5b12
KH
4289
4290 if (coding->heading_ascii > skip)
4291 coding->heading_ascii = skip;
4292 else
4293 skip = coding->heading_ascii;
4ed46869 4294
0ef69138 4295 if (eol_type == CODING_EOL_UNDECIDED)
4ed46869 4296 return;
27901516
KH
4297 if (eol_type == CODING_EOL_INCONSISTENT)
4298 {
4299#if 0
4300 /* This code is suppressed until we find a better way to
992f23f2 4301 distinguish raw text file and binary file. */
27901516
KH
4302
4303 /* If we have already detected that the coding is raw-text, the
4304 coding should actually be no-conversion. */
4305 if (coding->type == coding_type_raw_text)
4306 {
4307 setup_coding_system (Qno_conversion, coding);
4308 return;
4309 }
4310 /* Else, let's decode only text code anyway. */
4311#endif /* 0 */
1b2af4b0 4312 eol_type = CODING_EOL_LF;
27901516
KH
4313 }
4314
4608c386 4315 val = Fget (coding->symbol, Qeol_type);
4ed46869 4316 if (VECTORP (val) && XVECTOR (val)->size == 3)
d46c5b12 4317 {
b73bfc1c
KH
4318 int src_multibyte = coding->src_multibyte;
4319 int dst_multibyte = coding->dst_multibyte;
4320
d46c5b12 4321 setup_coding_system (XVECTOR (val)->contents[eol_type], coding);
b73bfc1c
KH
4322 coding->src_multibyte = src_multibyte;
4323 coding->dst_multibyte = dst_multibyte;
d46c5b12
KH
4324 coding->heading_ascii = skip;
4325 }
4326}
4327
4328#define CONVERSION_BUFFER_EXTRA_ROOM 256
4329
b73bfc1c
KH
4330#define DECODING_BUFFER_MAG(coding) \
4331 (coding->type == coding_type_iso2022 \
4332 ? 3 \
4333 : (coding->type == coding_type_ccl \
4334 ? coding->spec.ccl.decoder.buf_magnification \
4335 : 2))
d46c5b12
KH
4336
4337/* Return maximum size (bytes) of a buffer enough for decoding
4338 SRC_BYTES of text encoded in CODING. */
4339
4340int
4341decoding_buffer_size (coding, src_bytes)
4342 struct coding_system *coding;
4343 int src_bytes;
4344{
4345 return (src_bytes * DECODING_BUFFER_MAG (coding)
4346 + CONVERSION_BUFFER_EXTRA_ROOM);
4347}
4348
4349/* Return maximum size (bytes) of a buffer enough for encoding
4350 SRC_BYTES of text to CODING. */
4351
4352int
4353encoding_buffer_size (coding, src_bytes)
4354 struct coding_system *coding;
4355 int src_bytes;
4356{
4357 int magnification;
4358
4359 if (coding->type == coding_type_ccl)
4360 magnification = coding->spec.ccl.encoder.buf_magnification;
b73bfc1c 4361 else if (CODING_REQUIRE_ENCODING (coding))
d46c5b12 4362 magnification = 3;
b73bfc1c
KH
4363 else
4364 magnification = 1;
d46c5b12
KH
4365
4366 return (src_bytes * magnification + CONVERSION_BUFFER_EXTRA_ROOM);
4367}
4368
73be902c
KH
4369/* Working buffer for code conversion. */
4370struct conversion_buffer
4371{
4372 int size; /* size of data. */
4373 int on_stack; /* 1 if allocated by alloca. */
4374 unsigned char *data;
4375};
d46c5b12 4376
73be902c
KH
4377/* Don't use alloca for allocating memory space larger than this, lest
4378 we overflow their stack. */
4379#define MAX_ALLOCA 16*1024
d46c5b12 4380
73be902c
KH
4381/* Allocate LEN bytes of memory for BUF (struct conversion_buffer). */
4382#define allocate_conversion_buffer(buf, len) \
4383 do { \
4384 if (len < MAX_ALLOCA) \
4385 { \
4386 buf.data = (unsigned char *) alloca (len); \
4387 buf.on_stack = 1; \
4388 } \
4389 else \
4390 { \
4391 buf.data = (unsigned char *) xmalloc (len); \
4392 buf.on_stack = 0; \
4393 } \
4394 buf.size = len; \
4395 } while (0)
d46c5b12 4396
73be902c
KH
4397/* Double the allocated memory for *BUF. */
4398static void
4399extend_conversion_buffer (buf)
4400 struct conversion_buffer *buf;
d46c5b12 4401{
73be902c 4402 if (buf->on_stack)
d46c5b12 4403 {
73be902c
KH
4404 unsigned char *save = buf->data;
4405 buf->data = (unsigned char *) xmalloc (buf->size * 2);
4406 bcopy (save, buf->data, buf->size);
4407 buf->on_stack = 0;
d46c5b12 4408 }
73be902c
KH
4409 else
4410 {
4411 buf->data = (unsigned char *) xrealloc (buf->data, buf->size * 2);
4412 }
4413 buf->size *= 2;
4414}
4415
4416/* Free the allocated memory for BUF if it is not on stack. */
4417static void
4418free_conversion_buffer (buf)
4419 struct conversion_buffer *buf;
4420{
4421 if (!buf->on_stack)
4422 xfree (buf->data);
d46c5b12
KH
4423}
4424
4425int
4426ccl_coding_driver (coding, source, destination, src_bytes, dst_bytes, encodep)
4427 struct coding_system *coding;
4428 unsigned char *source, *destination;
4429 int src_bytes, dst_bytes, encodep;
4430{
4431 struct ccl_program *ccl
4432 = encodep ? &coding->spec.ccl.encoder : &coding->spec.ccl.decoder;
1c3478b0 4433 unsigned char *dst = destination;
d46c5b12 4434
bd64290d 4435 ccl->suppress_error = coding->suppress_error;
ae9ff118 4436 ccl->last_block = coding->mode & CODING_MODE_LAST_BLOCK;
aaaf0b1e 4437 if (encodep)
80e0ca99
KH
4438 {
4439 /* On encoding, EOL format is converted within ccl_driver. For
4440 that, setup proper information in the structure CCL. */
4441 ccl->eol_type = coding->eol_type;
4442 if (ccl->eol_type ==CODING_EOL_UNDECIDED)
4443 ccl->eol_type = CODING_EOL_LF;
4444 ccl->cr_consumed = coding->spec.ccl.cr_carryover;
4445 }
7272d75c 4446 ccl->multibyte = coding->src_multibyte;
1c3478b0
KH
4447 if (coding->spec.ccl.eight_bit_carryover[0] != 0)
4448 {
4449 /* Move carryover bytes to DESTINATION. */
4450 unsigned char *p = coding->spec.ccl.eight_bit_carryover;
4451 while (*p)
4452 *dst++ = *p++;
4453 coding->spec.ccl.eight_bit_carryover[0] = 0;
4454 if (dst_bytes)
4455 dst_bytes -= dst - destination;
4456 }
4457
4458 coding->produced = (ccl_driver (ccl, source, dst, src_bytes, dst_bytes,
4459 &(coding->consumed))
4460 + dst - destination);
4461
b73bfc1c 4462 if (encodep)
80e0ca99
KH
4463 {
4464 coding->produced_char = coding->produced;
4465 coding->spec.ccl.cr_carryover = ccl->cr_consumed;
4466 }
ade8d05e
KH
4467 else if (!ccl->eight_bit_control)
4468 {
4469 /* The produced bytes forms a valid multibyte sequence. */
4470 coding->produced_char
4471 = multibyte_chars_in_text (destination, coding->produced);
4472 coding->spec.ccl.eight_bit_carryover[0] = 0;
4473 }
b73bfc1c
KH
4474 else
4475 {
1c3478b0
KH
4476 /* On decoding, the destination should always multibyte. But,
4477 CCL program might have been generated an invalid multibyte
4478 sequence. Here we make such a sequence valid as
4479 multibyte. */
b73bfc1c
KH
4480 int bytes
4481 = dst_bytes ? dst_bytes : source + coding->consumed - destination;
1c3478b0
KH
4482
4483 if ((coding->consumed < src_bytes
4484 || !ccl->last_block)
4485 && coding->produced >= 1
4486 && destination[coding->produced - 1] >= 0x80)
4487 {
4488 /* We should not convert the tailing 8-bit codes to
4489 multibyte form even if they doesn't form a valid
4490 multibyte sequence. They may form a valid sequence in
4491 the next call. */
4492 int carryover = 0;
4493
4494 if (destination[coding->produced - 1] < 0xA0)
4495 carryover = 1;
4496 else if (coding->produced >= 2)
4497 {
4498 if (destination[coding->produced - 2] >= 0x80)
4499 {
4500 if (destination[coding->produced - 2] < 0xA0)
4501 carryover = 2;
4502 else if (coding->produced >= 3
4503 && destination[coding->produced - 3] >= 0x80
4504 && destination[coding->produced - 3] < 0xA0)
4505 carryover = 3;
4506 }
4507 }
4508 if (carryover > 0)
4509 {
4510 BCOPY_SHORT (destination + coding->produced - carryover,
4511 coding->spec.ccl.eight_bit_carryover,
4512 carryover);
4513 coding->spec.ccl.eight_bit_carryover[carryover] = 0;
4514 coding->produced -= carryover;
4515 }
4516 }
b73bfc1c
KH
4517 coding->produced = str_as_multibyte (destination, bytes,
4518 coding->produced,
4519 &(coding->produced_char));
4520 }
69f76525 4521
d46c5b12
KH
4522 switch (ccl->status)
4523 {
4524 case CCL_STAT_SUSPEND_BY_SRC:
73be902c 4525 coding->result = CODING_FINISH_INSUFFICIENT_SRC;
d46c5b12
KH
4526 break;
4527 case CCL_STAT_SUSPEND_BY_DST:
73be902c 4528 coding->result = CODING_FINISH_INSUFFICIENT_DST;
d46c5b12 4529 break;
9864ebce
KH
4530 case CCL_STAT_QUIT:
4531 case CCL_STAT_INVALID_CMD:
73be902c 4532 coding->result = CODING_FINISH_INTERRUPT;
9864ebce 4533 break;
d46c5b12 4534 default:
73be902c 4535 coding->result = CODING_FINISH_NORMAL;
d46c5b12
KH
4536 break;
4537 }
73be902c 4538 return coding->result;
4ed46869
KH
4539}
4540
aaaf0b1e
KH
4541/* Decode EOL format of the text at PTR of BYTES length destructively
4542 according to CODING->eol_type. This is called after the CCL
4543 program produced a decoded text at PTR. If we do CRLF->LF
4544 conversion, update CODING->produced and CODING->produced_char. */
4545
4546static void
4547decode_eol_post_ccl (coding, ptr, bytes)
4548 struct coding_system *coding;
4549 unsigned char *ptr;
4550 int bytes;
4551{
4552 Lisp_Object val, saved_coding_symbol;
4553 unsigned char *pend = ptr + bytes;
4554 int dummy;
4555
4556 /* Remember the current coding system symbol. We set it back when
4557 an inconsistent EOL is found so that `last-coding-system-used' is
4558 set to the coding system that doesn't specify EOL conversion. */
4559 saved_coding_symbol = coding->symbol;
4560
4561 coding->spec.ccl.cr_carryover = 0;
4562 if (coding->eol_type == CODING_EOL_UNDECIDED)
4563 {
4564 /* Here, to avoid the call of setup_coding_system, we directly
4565 call detect_eol_type. */
4566 coding->eol_type = detect_eol_type (ptr, bytes, &dummy);
74b01b80
EZ
4567 if (coding->eol_type == CODING_EOL_INCONSISTENT)
4568 coding->eol_type = CODING_EOL_LF;
4569 if (coding->eol_type != CODING_EOL_UNDECIDED)
4570 {
4571 val = Fget (coding->symbol, Qeol_type);
4572 if (VECTORP (val) && XVECTOR (val)->size == 3)
4573 coding->symbol = XVECTOR (val)->contents[coding->eol_type];
4574 }
aaaf0b1e
KH
4575 coding->mode |= CODING_MODE_INHIBIT_INCONSISTENT_EOL;
4576 }
4577
74b01b80
EZ
4578 if (coding->eol_type == CODING_EOL_LF
4579 || coding->eol_type == CODING_EOL_UNDECIDED)
aaaf0b1e
KH
4580 {
4581 /* We have nothing to do. */
4582 ptr = pend;
4583 }
4584 else if (coding->eol_type == CODING_EOL_CRLF)
4585 {
4586 unsigned char *pstart = ptr, *p = ptr;
4587
4588 if (! (coding->mode & CODING_MODE_LAST_BLOCK)
4589 && *(pend - 1) == '\r')
4590 {
4591 /* If the last character is CR, we can't handle it here
4592 because LF will be in the not-yet-decoded source text.
9861e777 4593 Record that the CR is not yet processed. */
aaaf0b1e
KH
4594 coding->spec.ccl.cr_carryover = 1;
4595 coding->produced--;
4596 coding->produced_char--;
4597 pend--;
4598 }
4599 while (ptr < pend)
4600 {
4601 if (*ptr == '\r')
4602 {
4603 if (ptr + 1 < pend && *(ptr + 1) == '\n')
4604 {
4605 *p++ = '\n';
4606 ptr += 2;
4607 }
4608 else
4609 {
4610 if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
4611 goto undo_eol_conversion;
4612 *p++ = *ptr++;
4613 }
4614 }
4615 else if (*ptr == '\n'
4616 && coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
4617 goto undo_eol_conversion;
4618 else
4619 *p++ = *ptr++;
4620 continue;
4621
4622 undo_eol_conversion:
4623 /* We have faced with inconsistent EOL format at PTR.
4624 Convert all LFs before PTR back to CRLFs. */
4625 for (p--, ptr--; p >= pstart; p--)
4626 {
4627 if (*p == '\n')
4628 *ptr-- = '\n', *ptr-- = '\r';
4629 else
4630 *ptr-- = *p;
4631 }
4632 /* If carryover is recorded, cancel it because we don't
4633 convert CRLF anymore. */
4634 if (coding->spec.ccl.cr_carryover)
4635 {
4636 coding->spec.ccl.cr_carryover = 0;
4637 coding->produced++;
4638 coding->produced_char++;
4639 pend++;
4640 }
4641 p = ptr = pend;
4642 coding->eol_type = CODING_EOL_LF;
4643 coding->symbol = saved_coding_symbol;
4644 }
4645 if (p < pend)
4646 {
4647 /* As each two-byte sequence CRLF was converted to LF, (PEND
4648 - P) is the number of deleted characters. */
4649 coding->produced -= pend - p;
4650 coding->produced_char -= pend - p;
4651 }
4652 }
4653 else /* i.e. coding->eol_type == CODING_EOL_CR */
4654 {
4655 unsigned char *p = ptr;
4656
4657 for (; ptr < pend; ptr++)
4658 {
4659 if (*ptr == '\r')
4660 *ptr = '\n';
4661 else if (*ptr == '\n'
4662 && coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
4663 {
4664 for (; p < ptr; p++)
4665 {
4666 if (*p == '\n')
4667 *p = '\r';
4668 }
4669 ptr = pend;
4670 coding->eol_type = CODING_EOL_LF;
4671 coding->symbol = saved_coding_symbol;
4672 }
4673 }
4674 }
4675}
4676
4ed46869
KH
4677/* See "GENERAL NOTES about `decode_coding_XXX ()' functions". Before
4678 decoding, it may detect coding system and format of end-of-line if
b73bfc1c
KH
4679 those are not yet decided. The source should be unibyte, the
4680 result is multibyte if CODING->dst_multibyte is nonzero, else
4681 unibyte. */
4ed46869
KH
4682
4683int
d46c5b12 4684decode_coding (coding, source, destination, src_bytes, dst_bytes)
4ed46869
KH
4685 struct coding_system *coding;
4686 unsigned char *source, *destination;
4687 int src_bytes, dst_bytes;
4ed46869 4688{
9861e777
EZ
4689 int extra = 0;
4690
0ef69138 4691 if (coding->type == coding_type_undecided)
4ed46869
KH
4692 detect_coding (coding, source, src_bytes);
4693
aaaf0b1e
KH
4694 if (coding->eol_type == CODING_EOL_UNDECIDED
4695 && coding->type != coding_type_ccl)
8844fa83
KH
4696 {
4697 detect_eol (coding, source, src_bytes);
4698 /* We had better recover the original eol format if we
8ca3766a 4699 encounter an inconsistent eol format while decoding. */
8844fa83
KH
4700 coding->mode |= CODING_MODE_INHIBIT_INCONSISTENT_EOL;
4701 }
4ed46869 4702
b73bfc1c
KH
4703 coding->produced = coding->produced_char = 0;
4704 coding->consumed = coding->consumed_char = 0;
4705 coding->errors = 0;
4706 coding->result = CODING_FINISH_NORMAL;
4707
4ed46869
KH
4708 switch (coding->type)
4709 {
4ed46869 4710 case coding_type_sjis:
b73bfc1c
KH
4711 decode_coding_sjis_big5 (coding, source, destination,
4712 src_bytes, dst_bytes, 1);
4ed46869
KH
4713 break;
4714
4715 case coding_type_iso2022:
b73bfc1c
KH
4716 decode_coding_iso2022 (coding, source, destination,
4717 src_bytes, dst_bytes);
4ed46869
KH
4718 break;
4719
4720 case coding_type_big5:
b73bfc1c
KH
4721 decode_coding_sjis_big5 (coding, source, destination,
4722 src_bytes, dst_bytes, 0);
4723 break;
4724
4725 case coding_type_emacs_mule:
4726 decode_coding_emacs_mule (coding, source, destination,
4727 src_bytes, dst_bytes);
4ed46869
KH
4728 break;
4729
4730 case coding_type_ccl:
aaaf0b1e
KH
4731 if (coding->spec.ccl.cr_carryover)
4732 {
9861e777
EZ
4733 /* Put the CR which was not processed by the previous call
4734 of decode_eol_post_ccl in DESTINATION. It will be
4735 decoded together with the following LF by the call to
4736 decode_eol_post_ccl below. */
aaaf0b1e
KH
4737 *destination = '\r';
4738 coding->produced++;
4739 coding->produced_char++;
4740 dst_bytes--;
9861e777 4741 extra = coding->spec.ccl.cr_carryover;
aaaf0b1e 4742 }
9861e777 4743 ccl_coding_driver (coding, source, destination + extra,
b73bfc1c 4744 src_bytes, dst_bytes, 0);
aaaf0b1e 4745 if (coding->eol_type != CODING_EOL_LF)
9861e777
EZ
4746 {
4747 coding->produced += extra;
4748 coding->produced_char += extra;
4749 decode_eol_post_ccl (coding, destination, coding->produced);
4750 }
d46c5b12
KH
4751 break;
4752
b73bfc1c
KH
4753 default:
4754 decode_eol (coding, source, destination, src_bytes, dst_bytes);
4755 }
4756
4757 if (coding->result == CODING_FINISH_INSUFFICIENT_SRC
e7c9eef9 4758 && coding->mode & CODING_MODE_LAST_BLOCK
b73bfc1c
KH
4759 && coding->consumed == src_bytes)
4760 coding->result = CODING_FINISH_NORMAL;
4761
4762 if (coding->mode & CODING_MODE_LAST_BLOCK
4763 && coding->result == CODING_FINISH_INSUFFICIENT_SRC)
4764 {
4765 unsigned char *src = source + coding->consumed;
4766 unsigned char *dst = destination + coding->produced;
4767
4768 src_bytes -= coding->consumed;
bb10be8b 4769 coding->errors++;
b73bfc1c
KH
4770 if (COMPOSING_P (coding))
4771 DECODE_COMPOSITION_END ('1');
4772 while (src_bytes--)
d46c5b12 4773 {
b73bfc1c
KH
4774 int c = *src++;
4775 dst += CHAR_STRING (c, dst);
4776 coding->produced_char++;
d46c5b12 4777 }
b73bfc1c
KH
4778 coding->consumed = coding->consumed_char = src - source;
4779 coding->produced = dst - destination;
73be902c 4780 coding->result = CODING_FINISH_NORMAL;
4ed46869
KH
4781 }
4782
b73bfc1c
KH
4783 if (!coding->dst_multibyte)
4784 {
4785 coding->produced = str_as_unibyte (destination, coding->produced);
4786 coding->produced_char = coding->produced;
4787 }
4ed46869 4788
b73bfc1c
KH
4789 return coding->result;
4790}
52d41803 4791
b73bfc1c
KH
4792/* See "GENERAL NOTES about `encode_coding_XXX ()' functions". The
4793 multibyteness of the source is CODING->src_multibyte, the
4794 multibyteness of the result is always unibyte. */
4ed46869
KH
4795
4796int
d46c5b12 4797encode_coding (coding, source, destination, src_bytes, dst_bytes)
4ed46869
KH
4798 struct coding_system *coding;
4799 unsigned char *source, *destination;
4800 int src_bytes, dst_bytes;
4ed46869 4801{
b73bfc1c
KH
4802 coding->produced = coding->produced_char = 0;
4803 coding->consumed = coding->consumed_char = 0;
4804 coding->errors = 0;
4805 coding->result = CODING_FINISH_NORMAL;
4ed46869 4806
d46c5b12
KH
4807 switch (coding->type)
4808 {
4ed46869 4809 case coding_type_sjis:
b73bfc1c
KH
4810 encode_coding_sjis_big5 (coding, source, destination,
4811 src_bytes, dst_bytes, 1);
4ed46869
KH
4812 break;
4813
4814 case coding_type_iso2022:
b73bfc1c
KH
4815 encode_coding_iso2022 (coding, source, destination,
4816 src_bytes, dst_bytes);
4ed46869
KH
4817 break;
4818
4819 case coding_type_big5:
b73bfc1c
KH
4820 encode_coding_sjis_big5 (coding, source, destination,
4821 src_bytes, dst_bytes, 0);
4822 break;
4823
4824 case coding_type_emacs_mule:
4825 encode_coding_emacs_mule (coding, source, destination,
4826 src_bytes, dst_bytes);
4ed46869
KH
4827 break;
4828
4829 case coding_type_ccl:
b73bfc1c
KH
4830 ccl_coding_driver (coding, source, destination,
4831 src_bytes, dst_bytes, 1);
d46c5b12
KH
4832 break;
4833
b73bfc1c
KH
4834 default:
4835 encode_eol (coding, source, destination, src_bytes, dst_bytes);
4836 }
4837
73be902c
KH
4838 if (coding->mode & CODING_MODE_LAST_BLOCK
4839 && coding->result == CODING_FINISH_INSUFFICIENT_SRC)
b73bfc1c
KH
4840 {
4841 unsigned char *src = source + coding->consumed;
b73bfc1c
KH
4842 unsigned char *dst = destination + coding->produced;
4843
4844 if (coding->type == coding_type_iso2022)
4845 ENCODE_RESET_PLANE_AND_REGISTER;
4846 if (COMPOSING_P (coding))
4847 *dst++ = ISO_CODE_ESC, *dst++ = '1';
4848 if (coding->consumed < src_bytes)
d46c5b12 4849 {
b73bfc1c
KH
4850 int len = src_bytes - coding->consumed;
4851
fabf4a91 4852 BCOPY_SHORT (src, dst, len);
b73bfc1c
KH
4853 if (coding->src_multibyte)
4854 len = str_as_unibyte (dst, len);
4855 dst += len;
4856 coding->consumed = src_bytes;
d46c5b12 4857 }
b73bfc1c 4858 coding->produced = coding->produced_char = dst - destination;
73be902c 4859 coding->result = CODING_FINISH_NORMAL;
4ed46869
KH
4860 }
4861
bb10be8b
KH
4862 if (coding->result == CODING_FINISH_INSUFFICIENT_SRC
4863 && coding->consumed == src_bytes)
4864 coding->result = CODING_FINISH_NORMAL;
4865
b73bfc1c 4866 return coding->result;
4ed46869
KH
4867}
4868
fb88bf2d
KH
4869/* Scan text in the region between *BEG and *END (byte positions),
4870 skip characters which we don't have to decode by coding system
4871 CODING at the head and tail, then set *BEG and *END to the region
4872 of the text we actually have to convert. The caller should move
b73bfc1c
KH
4873 the gap out of the region in advance if the region is from a
4874 buffer.
4ed46869 4875
d46c5b12
KH
4876 If STR is not NULL, *BEG and *END are indices into STR. */
4877
4878static void
4879shrink_decoding_region (beg, end, coding, str)
4880 int *beg, *end;
4881 struct coding_system *coding;
4882 unsigned char *str;
4883{
fb88bf2d 4884 unsigned char *begp_orig, *begp, *endp_orig, *endp, c;
d46c5b12 4885 int eol_conversion;
88993dfd 4886 Lisp_Object translation_table;
d46c5b12
KH
4887
4888 if (coding->type == coding_type_ccl
4889 || coding->type == coding_type_undecided
b73bfc1c
KH
4890 || coding->eol_type != CODING_EOL_LF
4891 || !NILP (coding->post_read_conversion)
4892 || coding->composing != COMPOSITION_DISABLED)
d46c5b12
KH
4893 {
4894 /* We can't skip any data. */
4895 return;
4896 }
b73bfc1c
KH
4897 if (coding->type == coding_type_no_conversion
4898 || coding->type == coding_type_raw_text
4899 || coding->type == coding_type_emacs_mule)
d46c5b12 4900 {
fb88bf2d
KH
4901 /* We need no conversion, but don't have to skip any data here.
4902 Decoding routine handles them effectively anyway. */
d46c5b12
KH
4903 return;
4904 }
4905
88993dfd
KH
4906 translation_table = coding->translation_table_for_decode;
4907 if (NILP (translation_table) && !NILP (Venable_character_translation))
4908 translation_table = Vstandard_translation_table_for_decode;
4909 if (CHAR_TABLE_P (translation_table))
4910 {
4911 int i;
4912 for (i = 0; i < 128; i++)
4913 if (!NILP (CHAR_TABLE_REF (translation_table, i)))
4914 break;
4915 if (i < 128)
fa46990e 4916 /* Some ASCII character should be translated. We give up
88993dfd
KH
4917 shrinking. */
4918 return;
4919 }
4920
b73bfc1c 4921 if (coding->heading_ascii >= 0)
d46c5b12
KH
4922 /* Detection routine has already found how much we can skip at the
4923 head. */
4924 *beg += coding->heading_ascii;
4925
4926 if (str)
4927 {
4928 begp_orig = begp = str + *beg;
4929 endp_orig = endp = str + *end;
4930 }
4931 else
4932 {
fb88bf2d 4933 begp_orig = begp = BYTE_POS_ADDR (*beg);
d46c5b12
KH
4934 endp_orig = endp = begp + *end - *beg;
4935 }
4936
fa46990e
DL
4937 eol_conversion = (coding->eol_type == CODING_EOL_CR
4938 || coding->eol_type == CODING_EOL_CRLF);
4939
d46c5b12
KH
4940 switch (coding->type)
4941 {
d46c5b12
KH
4942 case coding_type_sjis:
4943 case coding_type_big5:
4944 /* We can skip all ASCII characters at the head. */
4945 if (coding->heading_ascii < 0)
4946 {
4947 if (eol_conversion)
de9d083c 4948 while (begp < endp && *begp < 0x80 && *begp != '\r') begp++;
d46c5b12
KH
4949 else
4950 while (begp < endp && *begp < 0x80) begp++;
4951 }
4952 /* We can skip all ASCII characters at the tail except for the
4953 second byte of SJIS or BIG5 code. */
4954 if (eol_conversion)
de9d083c 4955 while (begp < endp && endp[-1] < 0x80 && endp[-1] != '\r') endp--;
d46c5b12
KH
4956 else
4957 while (begp < endp && endp[-1] < 0x80) endp--;
ee59c65f
RS
4958 /* Do not consider LF as ascii if preceded by CR, since that
4959 confuses eol decoding. */
4960 if (begp < endp && endp < endp_orig && endp[-1] == '\r' && endp[0] == '\n')
4961 endp++;
d46c5b12
KH
4962 if (begp < endp && endp < endp_orig && endp[-1] >= 0x80)
4963 endp++;
4964 break;
4965
b73bfc1c 4966 case coding_type_iso2022:
622fece5
KH
4967 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, 0) != CHARSET_ASCII)
4968 /* We can't skip any data. */
4969 break;
d46c5b12
KH
4970 if (coding->heading_ascii < 0)
4971 {
d46c5b12
KH
4972 /* We can skip all ASCII characters at the head except for a
4973 few control codes. */
4974 while (begp < endp && (c = *begp) < 0x80
4975 && c != ISO_CODE_CR && c != ISO_CODE_SO
4976 && c != ISO_CODE_SI && c != ISO_CODE_ESC
4977 && (!eol_conversion || c != ISO_CODE_LF))
4978 begp++;
4979 }
4980 switch (coding->category_idx)
4981 {
4982 case CODING_CATEGORY_IDX_ISO_8_1:
4983 case CODING_CATEGORY_IDX_ISO_8_2:
4984 /* We can skip all ASCII characters at the tail. */
4985 if (eol_conversion)
de9d083c 4986 while (begp < endp && (c = endp[-1]) < 0x80 && c != '\r') endp--;
d46c5b12
KH
4987 else
4988 while (begp < endp && endp[-1] < 0x80) endp--;
ee59c65f
RS
4989 /* Do not consider LF as ascii if preceded by CR, since that
4990 confuses eol decoding. */
4991 if (begp < endp && endp < endp_orig && endp[-1] == '\r' && endp[0] == '\n')
4992 endp++;
d46c5b12
KH
4993 break;
4994
4995 case CODING_CATEGORY_IDX_ISO_7:
4996 case CODING_CATEGORY_IDX_ISO_7_TIGHT:
de79a6a5 4997 {
8ca3766a 4998 /* We can skip all characters at the tail except for 8-bit
de79a6a5
KH
4999 codes and ESC and the following 2-byte at the tail. */
5000 unsigned char *eight_bit = NULL;
5001
5002 if (eol_conversion)
5003 while (begp < endp
5004 && (c = endp[-1]) != ISO_CODE_ESC && c != '\r')
5005 {
5006 if (!eight_bit && c & 0x80) eight_bit = endp;
5007 endp--;
5008 }
5009 else
5010 while (begp < endp
5011 && (c = endp[-1]) != ISO_CODE_ESC)
5012 {
5013 if (!eight_bit && c & 0x80) eight_bit = endp;
5014 endp--;
5015 }
5016 /* Do not consider LF as ascii if preceded by CR, since that
5017 confuses eol decoding. */
5018 if (begp < endp && endp < endp_orig
5019 && endp[-1] == '\r' && endp[0] == '\n')
5020 endp++;
5021 if (begp < endp && endp[-1] == ISO_CODE_ESC)
5022 {
5023 if (endp + 1 < endp_orig && end[0] == '(' && end[1] == 'B')
5024 /* This is an ASCII designation sequence. We can
5025 surely skip the tail. But, if we have
5026 encountered an 8-bit code, skip only the codes
5027 after that. */
5028 endp = eight_bit ? eight_bit : endp + 2;
5029 else
5030 /* Hmmm, we can't skip the tail. */
5031 endp = endp_orig;
5032 }
5033 else if (eight_bit)
5034 endp = eight_bit;
5035 }
d46c5b12 5036 }
b73bfc1c
KH
5037 break;
5038
5039 default:
5040 abort ();
d46c5b12
KH
5041 }
5042 *beg += begp - begp_orig;
5043 *end += endp - endp_orig;
5044 return;
5045}
5046
5047/* Like shrink_decoding_region but for encoding. */
5048
5049static void
5050shrink_encoding_region (beg, end, coding, str)
5051 int *beg, *end;
5052 struct coding_system *coding;
5053 unsigned char *str;
5054{
5055 unsigned char *begp_orig, *begp, *endp_orig, *endp;
5056 int eol_conversion;
88993dfd 5057 Lisp_Object translation_table;
d46c5b12 5058
b73bfc1c
KH
5059 if (coding->type == coding_type_ccl
5060 || coding->eol_type == CODING_EOL_CRLF
5061 || coding->eol_type == CODING_EOL_CR
87323294 5062 || (coding->cmp_data && coding->cmp_data->used > 0))
d46c5b12 5063 {
b73bfc1c
KH
5064 /* We can't skip any data. */
5065 return;
5066 }
5067 if (coding->type == coding_type_no_conversion
5068 || coding->type == coding_type_raw_text
5069 || coding->type == coding_type_emacs_mule
5070 || coding->type == coding_type_undecided)
5071 {
5072 /* We need no conversion, but don't have to skip any data here.
5073 Encoding routine handles them effectively anyway. */
d46c5b12
KH
5074 return;
5075 }
5076
88993dfd
KH
5077 translation_table = coding->translation_table_for_encode;
5078 if (NILP (translation_table) && !NILP (Venable_character_translation))
5079 translation_table = Vstandard_translation_table_for_encode;
5080 if (CHAR_TABLE_P (translation_table))
5081 {
5082 int i;
5083 for (i = 0; i < 128; i++)
5084 if (!NILP (CHAR_TABLE_REF (translation_table, i)))
5085 break;
5086 if (i < 128)
8ca3766a 5087 /* Some ASCII character should be translated. We give up
88993dfd
KH
5088 shrinking. */
5089 return;
5090 }
5091
d46c5b12
KH
5092 if (str)
5093 {
5094 begp_orig = begp = str + *beg;
5095 endp_orig = endp = str + *end;
5096 }
5097 else
5098 {
fb88bf2d 5099 begp_orig = begp = BYTE_POS_ADDR (*beg);
d46c5b12
KH
5100 endp_orig = endp = begp + *end - *beg;
5101 }
5102
5103 eol_conversion = (coding->eol_type == CODING_EOL_CR
5104 || coding->eol_type == CODING_EOL_CRLF);
5105
5106 /* Here, we don't have to check coding->pre_write_conversion because
5107 the caller is expected to have handled it already. */
5108 switch (coding->type)
5109 {
d46c5b12 5110 case coding_type_iso2022:
622fece5
KH
5111 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, 0) != CHARSET_ASCII)
5112 /* We can't skip any data. */
5113 break;
d46c5b12
KH
5114 if (coding->flags & CODING_FLAG_ISO_DESIGNATE_AT_BOL)
5115 {
93dec019 5116 unsigned char *bol = begp;
d46c5b12
KH
5117 while (begp < endp && *begp < 0x80)
5118 {
5119 begp++;
5120 if (begp[-1] == '\n')
5121 bol = begp;
5122 }
5123 begp = bol;
5124 goto label_skip_tail;
5125 }
5126 /* fall down ... */
5127
b73bfc1c
KH
5128 case coding_type_sjis:
5129 case coding_type_big5:
d46c5b12
KH
5130 /* We can skip all ASCII characters at the head and tail. */
5131 if (eol_conversion)
5132 while (begp < endp && *begp < 0x80 && *begp != '\n') begp++;
5133 else
5134 while (begp < endp && *begp < 0x80) begp++;
5135 label_skip_tail:
5136 if (eol_conversion)
5137 while (begp < endp && endp[-1] < 0x80 && endp[-1] != '\n') endp--;
5138 else
5139 while (begp < endp && *(endp - 1) < 0x80) endp--;
5140 break;
b73bfc1c
KH
5141
5142 default:
5143 abort ();
d46c5b12
KH
5144 }
5145
5146 *beg += begp - begp_orig;
5147 *end += endp - endp_orig;
5148 return;
5149}
5150
88993dfd
KH
5151/* As shrinking conversion region requires some overhead, we don't try
5152 shrinking if the length of conversion region is less than this
5153 value. */
5154static int shrink_conversion_region_threshhold = 1024;
5155
5156#define SHRINK_CONVERSION_REGION(beg, end, coding, str, encodep) \
5157 do { \
5158 if (*(end) - *(beg) > shrink_conversion_region_threshhold) \
5159 { \
5160 if (encodep) shrink_encoding_region (beg, end, coding, str); \
5161 else shrink_decoding_region (beg, end, coding, str); \
5162 } \
5163 } while (0)
5164
b843d1ae
KH
5165static Lisp_Object
5166code_convert_region_unwind (dummy)
5167 Lisp_Object dummy;
5168{
5169 inhibit_pre_post_conversion = 0;
5170 return Qnil;
5171}
5172
ec6d2bb8
KH
5173/* Store information about all compositions in the range FROM and TO
5174 of OBJ in memory blocks pointed by CODING->cmp_data. OBJ is a
5175 buffer or a string, defaults to the current buffer. */
5176
5177void
5178coding_save_composition (coding, from, to, obj)
5179 struct coding_system *coding;
5180 int from, to;
5181 Lisp_Object obj;
5182{
5183 Lisp_Object prop;
5184 int start, end;
5185
91bee881
KH
5186 if (coding->composing == COMPOSITION_DISABLED)
5187 return;
5188 if (!coding->cmp_data)
5189 coding_allocate_composition_data (coding, from);
ec6d2bb8
KH
5190 if (!find_composition (from, to, &start, &end, &prop, obj)
5191 || end > to)
5192 return;
5193 if (start < from
5194 && (!find_composition (end, to, &start, &end, &prop, obj)
5195 || end > to))
5196 return;
5197 coding->composing = COMPOSITION_NO;
ec6d2bb8
KH
5198 do
5199 {
5200 if (COMPOSITION_VALID_P (start, end, prop))
5201 {
5202 enum composition_method method = COMPOSITION_METHOD (prop);
5203 if (coding->cmp_data->used + COMPOSITION_DATA_MAX_BUNCH_LENGTH
5204 >= COMPOSITION_DATA_SIZE)
5205 coding_allocate_composition_data (coding, from);
5206 /* For relative composition, we remember start and end
5207 positions, for the other compositions, we also remember
5208 components. */
5209 CODING_ADD_COMPOSITION_START (coding, start - from, method);
5210 if (method != COMPOSITION_RELATIVE)
5211 {
5212 /* We must store a*/
5213 Lisp_Object val, ch;
5214
5215 val = COMPOSITION_COMPONENTS (prop);
5216 if (CONSP (val))
5217 while (CONSP (val))
5218 {
5219 ch = XCAR (val), val = XCDR (val);
5220 CODING_ADD_COMPOSITION_COMPONENT (coding, XINT (ch));
5221 }
5222 else if (VECTORP (val) || STRINGP (val))
5223 {
5224 int len = (VECTORP (val)
5225 ? XVECTOR (val)->size : XSTRING (val)->size);
5226 int i;
5227 for (i = 0; i < len; i++)
5228 {
5229 ch = (STRINGP (val)
5230 ? Faref (val, make_number (i))
5231 : XVECTOR (val)->contents[i]);
5232 CODING_ADD_COMPOSITION_COMPONENT (coding, XINT (ch));
5233 }
5234 }
5235 else /* INTEGERP (val) */
5236 CODING_ADD_COMPOSITION_COMPONENT (coding, XINT (val));
5237 }
5238 CODING_ADD_COMPOSITION_END (coding, end - from);
5239 }
5240 start = end;
5241 }
5242 while (start < to
5243 && find_composition (start, to, &start, &end, &prop, obj)
5244 && end <= to);
5245
5246 /* Make coding->cmp_data point to the first memory block. */
5247 while (coding->cmp_data->prev)
5248 coding->cmp_data = coding->cmp_data->prev;
5249 coding->cmp_data_start = 0;
5250}
5251
5252/* Reflect the saved information about compositions to OBJ.
8ca3766a 5253 CODING->cmp_data points to a memory block for the information. OBJ
ec6d2bb8
KH
5254 is a buffer or a string, defaults to the current buffer. */
5255
33fb63eb 5256void
ec6d2bb8
KH
5257coding_restore_composition (coding, obj)
5258 struct coding_system *coding;
5259 Lisp_Object obj;
5260{
5261 struct composition_data *cmp_data = coding->cmp_data;
5262
5263 if (!cmp_data)
5264 return;
5265
5266 while (cmp_data->prev)
5267 cmp_data = cmp_data->prev;
5268
5269 while (cmp_data)
5270 {
5271 int i;
5272
78108bcd
KH
5273 for (i = 0; i < cmp_data->used && cmp_data->data[i] > 0;
5274 i += cmp_data->data[i])
ec6d2bb8
KH
5275 {
5276 int *data = cmp_data->data + i;
5277 enum composition_method method = (enum composition_method) data[3];
5278 Lisp_Object components;
5279
5280 if (method == COMPOSITION_RELATIVE)
5281 components = Qnil;
5282 else
5283 {
5284 int len = data[0] - 4, j;
5285 Lisp_Object args[MAX_COMPOSITION_COMPONENTS * 2 - 1];
5286
5287 for (j = 0; j < len; j++)
5288 args[j] = make_number (data[4 + j]);
5289 components = (method == COMPOSITION_WITH_ALTCHARS
5290 ? Fstring (len, args) : Fvector (len, args));
5291 }
5292 compose_text (data[1], data[2], components, Qnil, obj);
5293 }
5294 cmp_data = cmp_data->next;
5295 }
5296}
5297
d46c5b12 5298/* Decode (if ENCODEP is zero) or encode (if ENCODEP is nonzero) the
fb88bf2d
KH
5299 text from FROM to TO (byte positions are FROM_BYTE and TO_BYTE) by
5300 coding system CODING, and return the status code of code conversion
5301 (currently, this value has no meaning).
5302
5303 How many characters (and bytes) are converted to how many
5304 characters (and bytes) are recorded in members of the structure
5305 CODING.
d46c5b12 5306
6e44253b 5307 If REPLACE is nonzero, we do various things as if the original text
d46c5b12 5308 is deleted and a new text is inserted. See the comments in
b73bfc1c
KH
5309 replace_range (insdel.c) to know what we are doing.
5310
5311 If REPLACE is zero, it is assumed that the source text is unibyte.
8ca3766a 5312 Otherwise, it is assumed that the source text is multibyte. */
4ed46869
KH
5313
5314int
6e44253b
KH
5315code_convert_region (from, from_byte, to, to_byte, coding, encodep, replace)
5316 int from, from_byte, to, to_byte, encodep, replace;
4ed46869 5317 struct coding_system *coding;
4ed46869 5318{
fb88bf2d 5319 int len = to - from, len_byte = to_byte - from_byte;
72d1a715 5320 int nchars_del = 0, nbytes_del = 0;
fb88bf2d 5321 int require, inserted, inserted_byte;
4b39528c 5322 int head_skip, tail_skip, total_skip = 0;
84d60297 5323 Lisp_Object saved_coding_symbol;
fb88bf2d 5324 int first = 1;
fb88bf2d 5325 unsigned char *src, *dst;
84d60297 5326 Lisp_Object deletion;
e133c8fa 5327 int orig_point = PT, orig_len = len;
6abb9bd9 5328 int prev_Z;
b73bfc1c
KH
5329 int multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5330
84d60297 5331 deletion = Qnil;
8844fa83 5332 saved_coding_symbol = coding->symbol;
d46c5b12 5333
83fa074f 5334 if (from < PT && PT < to)
e133c8fa
KH
5335 {
5336 TEMP_SET_PT_BOTH (from, from_byte);
5337 orig_point = from;
5338 }
83fa074f 5339
6e44253b 5340 if (replace)
d46c5b12 5341 {
fb88bf2d 5342 int saved_from = from;
e077cc80 5343 int saved_inhibit_modification_hooks;
fb88bf2d 5344
d46c5b12 5345 prepare_to_modify_buffer (from, to, &from);
fb88bf2d
KH
5346 if (saved_from != from)
5347 {
5348 to = from + len;
b73bfc1c 5349 from_byte = CHAR_TO_BYTE (from), to_byte = CHAR_TO_BYTE (to);
fb88bf2d
KH
5350 len_byte = to_byte - from_byte;
5351 }
e077cc80
KH
5352
5353 /* The code conversion routine can not preserve text properties
5354 for now. So, we must remove all text properties in the
5355 region. Here, we must suppress all modification hooks. */
5356 saved_inhibit_modification_hooks = inhibit_modification_hooks;
5357 inhibit_modification_hooks = 1;
5358 Fset_text_properties (make_number (from), make_number (to), Qnil, Qnil);
5359 inhibit_modification_hooks = saved_inhibit_modification_hooks;
d46c5b12 5360 }
d46c5b12
KH
5361
5362 if (! encodep && CODING_REQUIRE_DETECTION (coding))
5363 {
12410ef1 5364 /* We must detect encoding of text and eol format. */
d46c5b12
KH
5365
5366 if (from < GPT && to > GPT)
5367 move_gap_both (from, from_byte);
5368 if (coding->type == coding_type_undecided)
5369 {
fb88bf2d 5370 detect_coding (coding, BYTE_POS_ADDR (from_byte), len_byte);
d46c5b12 5371 if (coding->type == coding_type_undecided)
62b3ef1d
KH
5372 {
5373 /* It seems that the text contains only ASCII, but we
d9aef30f 5374 should not leave it undecided because the deeper
62b3ef1d
KH
5375 decoding routine (decode_coding) tries to detect the
5376 encodings again in vain. */
5377 coding->type = coding_type_emacs_mule;
5378 coding->category_idx = CODING_CATEGORY_IDX_EMACS_MULE;
d280ccb6
KH
5379 /* As emacs-mule decoder will handle composition, we
5380 need this setting to allocate coding->cmp_data
5381 later. */
5382 coding->composing = COMPOSITION_NO;
62b3ef1d 5383 }
d46c5b12 5384 }
aaaf0b1e
KH
5385 if (coding->eol_type == CODING_EOL_UNDECIDED
5386 && coding->type != coding_type_ccl)
d46c5b12 5387 {
d46c5b12
KH
5388 detect_eol (coding, BYTE_POS_ADDR (from_byte), len_byte);
5389 if (coding->eol_type == CODING_EOL_UNDECIDED)
5390 coding->eol_type = CODING_EOL_LF;
5391 /* We had better recover the original eol format if we
8ca3766a 5392 encounter an inconsistent eol format while decoding. */
d46c5b12
KH
5393 coding->mode |= CODING_MODE_INHIBIT_INCONSISTENT_EOL;
5394 }
5395 }
5396
d46c5b12
KH
5397 /* Now we convert the text. */
5398
5399 /* For encoding, we must process pre-write-conversion in advance. */
b73bfc1c
KH
5400 if (! inhibit_pre_post_conversion
5401 && encodep
d46c5b12
KH
5402 && SYMBOLP (coding->pre_write_conversion)
5403 && ! NILP (Ffboundp (coding->pre_write_conversion)))
5404 {
2b4f9037
KH
5405 /* The function in pre-write-conversion may put a new text in a
5406 new buffer. */
0007bdd0
KH
5407 struct buffer *prev = current_buffer;
5408 Lisp_Object new;
d46c5b12 5409
b843d1ae
KH
5410 record_unwind_protect (code_convert_region_unwind, Qnil);
5411 /* We should not call any more pre-write/post-read-conversion
5412 functions while this pre-write-conversion is running. */
5413 inhibit_pre_post_conversion = 1;
b39f748c
AS
5414 call2 (coding->pre_write_conversion,
5415 make_number (from), make_number (to));
b843d1ae
KH
5416 inhibit_pre_post_conversion = 0;
5417 /* Discard the unwind protect. */
5418 specpdl_ptr--;
5419
d46c5b12
KH
5420 if (current_buffer != prev)
5421 {
5422 len = ZV - BEGV;
0007bdd0 5423 new = Fcurrent_buffer ();
d46c5b12 5424 set_buffer_internal_1 (prev);
7dae4502 5425 del_range_2 (from, from_byte, to, to_byte, 0);
e133c8fa 5426 TEMP_SET_PT_BOTH (from, from_byte);
0007bdd0
KH
5427 insert_from_buffer (XBUFFER (new), 1, len, 0);
5428 Fkill_buffer (new);
e133c8fa
KH
5429 if (orig_point >= to)
5430 orig_point += len - orig_len;
5431 else if (orig_point > from)
5432 orig_point = from;
5433 orig_len = len;
d46c5b12 5434 to = from + len;
b73bfc1c
KH
5435 from_byte = CHAR_TO_BYTE (from);
5436 to_byte = CHAR_TO_BYTE (to);
d46c5b12 5437 len_byte = to_byte - from_byte;
e133c8fa 5438 TEMP_SET_PT_BOTH (from, from_byte);
d46c5b12
KH
5439 }
5440 }
5441
12410ef1 5442 if (replace)
72d1a715
RS
5443 {
5444 if (! EQ (current_buffer->undo_list, Qt))
5445 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
5446 else
5447 {
5448 nchars_del = to - from;
5449 nbytes_del = to_byte - from_byte;
5450 }
5451 }
12410ef1 5452
ec6d2bb8
KH
5453 if (coding->composing != COMPOSITION_DISABLED)
5454 {
5455 if (encodep)
5456 coding_save_composition (coding, from, to, Fcurrent_buffer ());
5457 else
5458 coding_allocate_composition_data (coding, from);
5459 }
fb88bf2d 5460
b73bfc1c 5461 /* Try to skip the heading and tailing ASCIIs. */
4956c225
KH
5462 if (coding->type != coding_type_ccl)
5463 {
5464 int from_byte_orig = from_byte, to_byte_orig = to_byte;
ec6d2bb8 5465
4956c225
KH
5466 if (from < GPT && GPT < to)
5467 move_gap_both (from, from_byte);
5468 SHRINK_CONVERSION_REGION (&from_byte, &to_byte, coding, NULL, encodep);
5469 if (from_byte == to_byte
5470 && (encodep || NILP (coding->post_read_conversion))
5471 && ! CODING_REQUIRE_FLUSHING (coding))
5472 {
5473 coding->produced = len_byte;
5474 coding->produced_char = len;
5475 if (!replace)
5476 /* We must record and adjust for this new text now. */
5477 adjust_after_insert (from, from_byte_orig, to, to_byte_orig, len);
5478 return 0;
5479 }
5480
5481 head_skip = from_byte - from_byte_orig;
5482 tail_skip = to_byte_orig - to_byte;
5483 total_skip = head_skip + tail_skip;
5484 from += head_skip;
5485 to -= tail_skip;
5486 len -= total_skip; len_byte -= total_skip;
5487 }
d46c5b12 5488
8ca3766a 5489 /* For conversion, we must put the gap before the text in addition to
fb88bf2d
KH
5490 making the gap larger for efficient decoding. The required gap
5491 size starts from 2000 which is the magic number used in make_gap.
5492 But, after one batch of conversion, it will be incremented if we
5493 find that it is not enough . */
d46c5b12
KH
5494 require = 2000;
5495
5496 if (GAP_SIZE < require)
5497 make_gap (require - GAP_SIZE);
5498 move_gap_both (from, from_byte);
5499
d46c5b12 5500 inserted = inserted_byte = 0;
fb88bf2d
KH
5501
5502 GAP_SIZE += len_byte;
5503 ZV -= len;
5504 Z -= len;
5505 ZV_BYTE -= len_byte;
5506 Z_BYTE -= len_byte;
5507
d9f9a1bc
GM
5508 if (GPT - BEG < BEG_UNCHANGED)
5509 BEG_UNCHANGED = GPT - BEG;
5510 if (Z - GPT < END_UNCHANGED)
5511 END_UNCHANGED = Z - GPT;
f2558efd 5512
b73bfc1c
KH
5513 if (!encodep && coding->src_multibyte)
5514 {
5515 /* Decoding routines expects that the source text is unibyte.
5516 We must convert 8-bit characters of multibyte form to
5517 unibyte. */
5518 int len_byte_orig = len_byte;
5519 len_byte = str_as_unibyte (GAP_END_ADDR - len_byte, len_byte);
5520 if (len_byte < len_byte_orig)
5521 safe_bcopy (GAP_END_ADDR - len_byte_orig, GAP_END_ADDR - len_byte,
5522 len_byte);
5523 coding->src_multibyte = 0;
5524 }
5525
d46c5b12
KH
5526 for (;;)
5527 {
fb88bf2d 5528 int result;
d46c5b12 5529
ec6d2bb8 5530 /* The buffer memory is now:
b73bfc1c
KH
5531 +--------+converted-text+---------+-------original-text-------+---+
5532 |<-from->|<--inserted-->|---------|<--------len_byte--------->|---|
5533 |<---------------------- GAP ----------------------->| */
ec6d2bb8
KH
5534 src = GAP_END_ADDR - len_byte;
5535 dst = GPT_ADDR + inserted_byte;
5536
d46c5b12 5537 if (encodep)
fb88bf2d 5538 result = encode_coding (coding, src, dst, len_byte, 0);
d46c5b12 5539 else
0e79d667
RS
5540 {
5541 if (coding->composing != COMPOSITION_DISABLED)
5542 coding->cmp_data->char_offset = from + inserted;
5543 result = decode_coding (coding, src, dst, len_byte, 0);
5544 }
ec6d2bb8
KH
5545
5546 /* The buffer memory is now:
b73bfc1c
KH
5547 +--------+-------converted-text----+--+------original-text----+---+
5548 |<-from->|<-inserted->|<-produced->|--|<-(len_byte-consumed)->|---|
5549 |<---------------------- GAP ----------------------->| */
ec6d2bb8 5550
d46c5b12
KH
5551 inserted += coding->produced_char;
5552 inserted_byte += coding->produced;
d46c5b12 5553 len_byte -= coding->consumed;
ec6d2bb8
KH
5554
5555 if (result == CODING_FINISH_INSUFFICIENT_CMP)
5556 {
5557 coding_allocate_composition_data (coding, from + inserted);
5558 continue;
5559 }
5560
fb88bf2d 5561 src += coding->consumed;
3636f7a3 5562 dst += coding->produced;
d46c5b12 5563
9864ebce
KH
5564 if (result == CODING_FINISH_NORMAL)
5565 {
5566 src += len_byte;
5567 break;
5568 }
d46c5b12
KH
5569 if (! encodep && result == CODING_FINISH_INCONSISTENT_EOL)
5570 {
fb88bf2d 5571 unsigned char *pend = dst, *p = pend - inserted_byte;
38edf7d4 5572 Lisp_Object eol_type;
d46c5b12
KH
5573
5574 /* Encode LFs back to the original eol format (CR or CRLF). */
5575 if (coding->eol_type == CODING_EOL_CR)
5576 {
5577 while (p < pend) if (*p++ == '\n') p[-1] = '\r';
5578 }
5579 else
5580 {
d46c5b12
KH
5581 int count = 0;
5582
fb88bf2d
KH
5583 while (p < pend) if (*p++ == '\n') count++;
5584 if (src - dst < count)
d46c5b12 5585 {
38edf7d4 5586 /* We don't have sufficient room for encoding LFs
fb88bf2d
KH
5587 back to CRLF. We must record converted and
5588 not-yet-converted text back to the buffer
5589 content, enlarge the gap, then record them out of
5590 the buffer contents again. */
5591 int add = len_byte + inserted_byte;
5592
5593 GAP_SIZE -= add;
5594 ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add;
5595 GPT += inserted_byte; GPT_BYTE += inserted_byte;
5596 make_gap (count - GAP_SIZE);
5597 GAP_SIZE += add;
5598 ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add;
5599 GPT -= inserted_byte; GPT_BYTE -= inserted_byte;
5600 /* Don't forget to update SRC, DST, and PEND. */
5601 src = GAP_END_ADDR - len_byte;
5602 dst = GPT_ADDR + inserted_byte;
5603 pend = dst;
d46c5b12 5604 }
d46c5b12
KH
5605 inserted += count;
5606 inserted_byte += count;
fb88bf2d
KH
5607 coding->produced += count;
5608 p = dst = pend + count;
5609 while (count)
5610 {
5611 *--p = *--pend;
5612 if (*p == '\n') count--, *--p = '\r';
5613 }
d46c5b12
KH
5614 }
5615
5616 /* Suppress eol-format conversion in the further conversion. */
5617 coding->eol_type = CODING_EOL_LF;
5618
38edf7d4
KH
5619 /* Set the coding system symbol to that for Unix-like EOL. */
5620 eol_type = Fget (saved_coding_symbol, Qeol_type);
5621 if (VECTORP (eol_type)
5622 && XVECTOR (eol_type)->size == 3
5623 && SYMBOLP (XVECTOR (eol_type)->contents[CODING_EOL_LF]))
5624 coding->symbol = XVECTOR (eol_type)->contents[CODING_EOL_LF];
5625 else
5626 coding->symbol = saved_coding_symbol;
93dec019 5627
fb88bf2d 5628 continue;
d46c5b12
KH
5629 }
5630 if (len_byte <= 0)
944bd420
KH
5631 {
5632 if (coding->type != coding_type_ccl
5633 || coding->mode & CODING_MODE_LAST_BLOCK)
5634 break;
5635 coding->mode |= CODING_MODE_LAST_BLOCK;
5636 continue;
5637 }
d46c5b12
KH
5638 if (result == CODING_FINISH_INSUFFICIENT_SRC)
5639 {
5640 /* The source text ends in invalid codes. Let's just
5641 make them valid buffer contents, and finish conversion. */
70ad9fc4
GM
5642 if (multibyte_p)
5643 {
5644 unsigned char *start = dst;
93dec019 5645
70ad9fc4
GM
5646 inserted += len_byte;
5647 while (len_byte--)
5648 {
5649 int c = *src++;
5650 dst += CHAR_STRING (c, dst);
5651 }
5652
5653 inserted_byte += dst - start;
5654 }
5655 else
5656 {
5657 inserted += len_byte;
5658 inserted_byte += len_byte;
5659 while (len_byte--)
5660 *dst++ = *src++;
5661 }
d46c5b12
KH
5662 break;
5663 }
9864ebce
KH
5664 if (result == CODING_FINISH_INTERRUPT)
5665 {
5666 /* The conversion procedure was interrupted by a user. */
9864ebce
KH
5667 break;
5668 }
5669 /* Now RESULT == CODING_FINISH_INSUFFICIENT_DST */
5670 if (coding->consumed < 1)
5671 {
5672 /* It's quite strange to require more memory without
5673 consuming any bytes. Perhaps CCL program bug. */
9864ebce
KH
5674 break;
5675 }
fb88bf2d
KH
5676 if (first)
5677 {
5678 /* We have just done the first batch of conversion which was
8ca3766a 5679 stopped because of insufficient gap. Let's reconsider the
fb88bf2d
KH
5680 required gap size (i.e. SRT - DST) now.
5681
5682 We have converted ORIG bytes (== coding->consumed) into
5683 NEW bytes (coding->produced). To convert the remaining
5684 LEN bytes, we may need REQUIRE bytes of gap, where:
5685 REQUIRE + LEN_BYTE = LEN_BYTE * (NEW / ORIG)
5686 REQUIRE = LEN_BYTE * (NEW - ORIG) / ORIG
5687 Here, we are sure that NEW >= ORIG. */
6e44253b
KH
5688 float ratio = coding->produced - coding->consumed;
5689 ratio /= coding->consumed;
5690 require = len_byte * ratio;
fb88bf2d
KH
5691 first = 0;
5692 }
5693 if ((src - dst) < (require + 2000))
5694 {
5695 /* See the comment above the previous call of make_gap. */
5696 int add = len_byte + inserted_byte;
5697
5698 GAP_SIZE -= add;
5699 ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add;
5700 GPT += inserted_byte; GPT_BYTE += inserted_byte;
5701 make_gap (require + 2000);
5702 GAP_SIZE += add;
5703 ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add;
5704 GPT -= inserted_byte; GPT_BYTE -= inserted_byte;
fb88bf2d 5705 }
d46c5b12 5706 }
fb88bf2d
KH
5707 if (src - dst > 0) *dst = 0; /* Put an anchor. */
5708
b73bfc1c
KH
5709 if (encodep && coding->dst_multibyte)
5710 {
5711 /* The output is unibyte. We must convert 8-bit characters to
5712 multibyte form. */
5713 if (inserted_byte * 2 > GAP_SIZE)
5714 {
5715 GAP_SIZE -= inserted_byte;
5716 ZV += inserted_byte; Z += inserted_byte;
5717 ZV_BYTE += inserted_byte; Z_BYTE += inserted_byte;
5718 GPT += inserted_byte; GPT_BYTE += inserted_byte;
5719 make_gap (inserted_byte - GAP_SIZE);
5720 GAP_SIZE += inserted_byte;
5721 ZV -= inserted_byte; Z -= inserted_byte;
5722 ZV_BYTE -= inserted_byte; Z_BYTE -= inserted_byte;
5723 GPT -= inserted_byte; GPT_BYTE -= inserted_byte;
5724 }
5725 inserted_byte = str_to_multibyte (GPT_ADDR, GAP_SIZE, inserted_byte);
5726 }
7553d0e1 5727
93dec019 5728 /* If we shrank the conversion area, adjust it now. */
12410ef1
KH
5729 if (total_skip > 0)
5730 {
5731 if (tail_skip > 0)
5732 safe_bcopy (GAP_END_ADDR, GPT_ADDR + inserted_byte, tail_skip);
5733 inserted += total_skip; inserted_byte += total_skip;
5734 GAP_SIZE += total_skip;
5735 GPT -= head_skip; GPT_BYTE -= head_skip;
5736 ZV -= total_skip; ZV_BYTE -= total_skip;
5737 Z -= total_skip; Z_BYTE -= total_skip;
5738 from -= head_skip; from_byte -= head_skip;
5739 to += tail_skip; to_byte += tail_skip;
5740 }
5741
6abb9bd9 5742 prev_Z = Z;
72d1a715
RS
5743 if (! EQ (current_buffer->undo_list, Qt))
5744 adjust_after_replace (from, from_byte, deletion, inserted, inserted_byte);
5745 else
5746 adjust_after_replace_noundo (from, from_byte, nchars_del, nbytes_del,
5747 inserted, inserted_byte);
6abb9bd9 5748 inserted = Z - prev_Z;
4ed46869 5749
ec6d2bb8
KH
5750 if (!encodep && coding->cmp_data && coding->cmp_data->used)
5751 coding_restore_composition (coding, Fcurrent_buffer ());
5752 coding_free_composition_data (coding);
5753
b73bfc1c
KH
5754 if (! inhibit_pre_post_conversion
5755 && ! encodep && ! NILP (coding->post_read_conversion))
d46c5b12 5756 {
2b4f9037 5757 Lisp_Object val;
4ed46869 5758
e133c8fa
KH
5759 if (from != PT)
5760 TEMP_SET_PT_BOTH (from, from_byte);
6abb9bd9 5761 prev_Z = Z;
b843d1ae
KH
5762 record_unwind_protect (code_convert_region_unwind, Qnil);
5763 /* We should not call any more pre-write/post-read-conversion
5764 functions while this post-read-conversion is running. */
5765 inhibit_pre_post_conversion = 1;
2b4f9037 5766 val = call1 (coding->post_read_conversion, make_number (inserted));
b843d1ae
KH
5767 inhibit_pre_post_conversion = 0;
5768 /* Discard the unwind protect. */
5769 specpdl_ptr--;
b7826503 5770 CHECK_NUMBER (val);
944bd420 5771 inserted += Z - prev_Z;
e133c8fa
KH
5772 }
5773
5774 if (orig_point >= from)
5775 {
5776 if (orig_point >= from + orig_len)
5777 orig_point += inserted - orig_len;
5778 else
5779 orig_point = from;
5780 TEMP_SET_PT (orig_point);
d46c5b12 5781 }
4ed46869 5782
ec6d2bb8
KH
5783 if (replace)
5784 {
5785 signal_after_change (from, to - from, inserted);
e19539f1 5786 update_compositions (from, from + inserted, CHECK_BORDER);
ec6d2bb8 5787 }
2b4f9037 5788
fb88bf2d 5789 {
12410ef1
KH
5790 coding->consumed = to_byte - from_byte;
5791 coding->consumed_char = to - from;
5792 coding->produced = inserted_byte;
5793 coding->produced_char = inserted;
fb88bf2d 5794 }
7553d0e1 5795
fb88bf2d 5796 return 0;
d46c5b12
KH
5797}
5798
5799Lisp_Object
b73bfc1c
KH
5800run_pre_post_conversion_on_str (str, coding, encodep)
5801 Lisp_Object str;
5802 struct coding_system *coding;
5803 int encodep;
5804{
5805 int count = specpdl_ptr - specpdl;
5806 struct gcpro gcpro1;
b73bfc1c 5807 int multibyte = STRING_MULTIBYTE (str);
3fd9494b
RS
5808 Lisp_Object buffer;
5809 struct buffer *buf;
b73bfc1c
KH
5810
5811 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
5812 record_unwind_protect (code_convert_region_unwind, Qnil);
5813 GCPRO1 (str);
3fd9494b
RS
5814
5815 buffer = Fget_buffer_create (build_string (" *code-converting-work*"));
5816 buf = XBUFFER (buffer);
5817
5818 buf->directory = current_buffer->directory;
5819 buf->read_only = Qnil;
5820 buf->filename = Qnil;
5821 buf->undo_list = Qt;
5822 buf->overlays_before = Qnil;
5823 buf->overlays_after = Qnil;
5824
5825 set_buffer_internal (buf);
b73bfc1c
KH
5826 /* We must insert the contents of STR as is without
5827 unibyte<->multibyte conversion. For that, we adjust the
5828 multibyteness of the working buffer to that of STR. */
5829 Ferase_buffer ();
3fd9494b
RS
5830 buf->enable_multibyte_characters = multibyte ? Qt : Qnil;
5831
b73bfc1c
KH
5832 insert_from_string (str, 0, 0,
5833 XSTRING (str)->size, STRING_BYTES (XSTRING (str)), 0);
5834 UNGCPRO;
5835 inhibit_pre_post_conversion = 1;
5836 if (encodep)
5837 call2 (coding->pre_write_conversion, make_number (BEG), make_number (Z));
5838 else
6bac5b12
KH
5839 {
5840 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5841 call1 (coding->post_read_conversion, make_number (Z - BEG));
5842 }
b73bfc1c 5843 inhibit_pre_post_conversion = 0;
78108bcd 5844 str = make_buffer_string (BEG, Z, 1);
b73bfc1c
KH
5845 return unbind_to (count, str);
5846}
5847
5848Lisp_Object
5849decode_coding_string (str, coding, nocopy)
d46c5b12 5850 Lisp_Object str;
4ed46869 5851 struct coding_system *coding;
b73bfc1c 5852 int nocopy;
4ed46869 5853{
d46c5b12 5854 int len;
73be902c 5855 struct conversion_buffer buf;
da55a2b7 5856 int from, to_byte;
84d60297 5857 Lisp_Object saved_coding_symbol;
d46c5b12 5858 int result;
78108bcd 5859 int require_decoding;
73be902c
KH
5860 int shrinked_bytes = 0;
5861 Lisp_Object newstr;
2391eaa4 5862 int consumed, consumed_char, produced, produced_char;
4ed46869 5863
b73bfc1c 5864 from = 0;
b73bfc1c 5865 to_byte = STRING_BYTES (XSTRING (str));
4ed46869 5866
8844fa83 5867 saved_coding_symbol = coding->symbol;
764ca8da
KH
5868 coding->src_multibyte = STRING_MULTIBYTE (str);
5869 coding->dst_multibyte = 1;
b73bfc1c 5870 if (CODING_REQUIRE_DETECTION (coding))
d46c5b12
KH
5871 {
5872 /* See the comments in code_convert_region. */
5873 if (coding->type == coding_type_undecided)
5874 {
5875 detect_coding (coding, XSTRING (str)->data, to_byte);
5876 if (coding->type == coding_type_undecided)
d280ccb6
KH
5877 {
5878 coding->type = coding_type_emacs_mule;
5879 coding->category_idx = CODING_CATEGORY_IDX_EMACS_MULE;
5880 /* As emacs-mule decoder will handle composition, we
5881 need this setting to allocate coding->cmp_data
5882 later. */
5883 coding->composing = COMPOSITION_NO;
5884 }
d46c5b12 5885 }
aaaf0b1e
KH
5886 if (coding->eol_type == CODING_EOL_UNDECIDED
5887 && coding->type != coding_type_ccl)
d46c5b12
KH
5888 {
5889 saved_coding_symbol = coding->symbol;
5890 detect_eol (coding, XSTRING (str)->data, to_byte);
5891 if (coding->eol_type == CODING_EOL_UNDECIDED)
5892 coding->eol_type = CODING_EOL_LF;
5893 /* We had better recover the original eol format if we
8ca3766a 5894 encounter an inconsistent eol format while decoding. */
d46c5b12
KH
5895 coding->mode |= CODING_MODE_INHIBIT_INCONSISTENT_EOL;
5896 }
5897 }
4ed46869 5898
764ca8da
KH
5899 if (coding->type == coding_type_no_conversion
5900 || coding->type == coding_type_raw_text)
5901 coding->dst_multibyte = 0;
5902
78108bcd 5903 require_decoding = CODING_REQUIRE_DECODING (coding);
ec6d2bb8 5904
b73bfc1c 5905 if (STRING_MULTIBYTE (str))
d46c5b12 5906 {
b73bfc1c
KH
5907 /* Decoding routines expect the source text to be unibyte. */
5908 str = Fstring_as_unibyte (str);
86af83a9 5909 to_byte = STRING_BYTES (XSTRING (str));
b73bfc1c 5910 nocopy = 1;
764ca8da 5911 coding->src_multibyte = 0;
b73bfc1c 5912 }
ec6d2bb8 5913
b73bfc1c 5914 /* Try to skip the heading and tailing ASCIIs. */
78108bcd 5915 if (require_decoding && coding->type != coding_type_ccl)
4956c225 5916 {
4956c225
KH
5917 SHRINK_CONVERSION_REGION (&from, &to_byte, coding, XSTRING (str)->data,
5918 0);
5919 if (from == to_byte)
78108bcd 5920 require_decoding = 0;
73be902c 5921 shrinked_bytes = from + (STRING_BYTES (XSTRING (str)) - to_byte);
4956c225 5922 }
b73bfc1c 5923
78108bcd
KH
5924 if (!require_decoding)
5925 {
5926 coding->consumed = STRING_BYTES (XSTRING (str));
5927 coding->consumed_char = XSTRING (str)->size;
5928 if (coding->dst_multibyte)
5929 {
5930 str = Fstring_as_multibyte (str);
5931 nocopy = 1;
5932 }
5933 coding->produced = STRING_BYTES (XSTRING (str));
5934 coding->produced_char = XSTRING (str)->size;
5935 return (nocopy ? str : Fcopy_sequence (str));
5936 }
5937
5938 if (coding->composing != COMPOSITION_DISABLED)
5939 coding_allocate_composition_data (coding, from);
b73bfc1c 5940 len = decoding_buffer_size (coding, to_byte - from);
73be902c 5941 allocate_conversion_buffer (buf, len);
4ed46869 5942
2391eaa4 5943 consumed = consumed_char = produced = produced_char = 0;
73be902c 5944 while (1)
4ed46869 5945 {
73be902c
KH
5946 result = decode_coding (coding, XSTRING (str)->data + from + consumed,
5947 buf.data + produced, to_byte - from - consumed,
5948 buf.size - produced);
5949 consumed += coding->consumed;
2391eaa4 5950 consumed_char += coding->consumed_char;
73be902c
KH
5951 produced += coding->produced;
5952 produced_char += coding->produced_char;
2391eaa4
KH
5953 if (result == CODING_FINISH_NORMAL
5954 || (result == CODING_FINISH_INSUFFICIENT_SRC
5955 && coding->consumed == 0))
73be902c
KH
5956 break;
5957 if (result == CODING_FINISH_INSUFFICIENT_CMP)
5958 coding_allocate_composition_data (coding, from + produced_char);
5959 else if (result == CODING_FINISH_INSUFFICIENT_DST)
5960 extend_conversion_buffer (&buf);
5961 else if (result == CODING_FINISH_INCONSISTENT_EOL)
5962 {
8844fa83
KH
5963 Lisp_Object eol_type;
5964
73be902c
KH
5965 /* Recover the original EOL format. */
5966 if (coding->eol_type == CODING_EOL_CR)
5967 {
5968 unsigned char *p;
5969 for (p = buf.data; p < buf.data + produced; p++)
5970 if (*p == '\n') *p = '\r';
5971 }
5972 else if (coding->eol_type == CODING_EOL_CRLF)
5973 {
5974 int num_eol = 0;
5975 unsigned char *p0, *p1;
5976 for (p0 = buf.data, p1 = p0 + produced; p0 < p1; p0++)
5977 if (*p0 == '\n') num_eol++;
5978 if (produced + num_eol >= buf.size)
5979 extend_conversion_buffer (&buf);
5980 for (p0 = buf.data + produced, p1 = p0 + num_eol; p0 > buf.data;)
5981 {
5982 *--p1 = *--p0;
5983 if (*p0 == '\n') *--p1 = '\r';
5984 }
5985 produced += num_eol;
5986 produced_char += num_eol;
93dec019 5987 }
8844fa83 5988 /* Suppress eol-format conversion in the further conversion. */
73be902c 5989 coding->eol_type = CODING_EOL_LF;
8844fa83
KH
5990
5991 /* Set the coding system symbol to that for Unix-like EOL. */
5992 eol_type = Fget (saved_coding_symbol, Qeol_type);
5993 if (VECTORP (eol_type)
5994 && XVECTOR (eol_type)->size == 3
5995 && SYMBOLP (XVECTOR (eol_type)->contents[CODING_EOL_LF]))
5996 coding->symbol = XVECTOR (eol_type)->contents[CODING_EOL_LF];
5997 else
5998 coding->symbol = saved_coding_symbol;
5999
6000
73be902c 6001 }
4ed46869 6002 }
d46c5b12 6003
2391eaa4
KH
6004 coding->consumed = consumed;
6005 coding->consumed_char = consumed_char;
6006 coding->produced = produced;
6007 coding->produced_char = produced_char;
6008
78108bcd 6009 if (coding->dst_multibyte)
73be902c
KH
6010 newstr = make_uninit_multibyte_string (produced_char + shrinked_bytes,
6011 produced + shrinked_bytes);
78108bcd 6012 else
73be902c
KH
6013 newstr = make_uninit_string (produced + shrinked_bytes);
6014 if (from > 0)
6015 bcopy (XSTRING (str)->data, XSTRING (newstr)->data, from);
6016 bcopy (buf.data, XSTRING (newstr)->data + from, produced);
6017 if (shrinked_bytes > from)
6018 bcopy (XSTRING (str)->data + to_byte,
6019 XSTRING (newstr)->data + from + produced,
6020 shrinked_bytes - from);
6021 free_conversion_buffer (&buf);
b73bfc1c
KH
6022
6023 if (coding->cmp_data && coding->cmp_data->used)
73be902c 6024 coding_restore_composition (coding, newstr);
b73bfc1c
KH
6025 coding_free_composition_data (coding);
6026
6027 if (SYMBOLP (coding->post_read_conversion)
6028 && !NILP (Ffboundp (coding->post_read_conversion)))
73be902c 6029 newstr = run_pre_post_conversion_on_str (newstr, coding, 0);
b73bfc1c 6030
73be902c 6031 return newstr;
b73bfc1c
KH
6032}
6033
6034Lisp_Object
6035encode_coding_string (str, coding, nocopy)
6036 Lisp_Object str;
6037 struct coding_system *coding;
6038 int nocopy;
6039{
6040 int len;
73be902c 6041 struct conversion_buffer buf;
b73bfc1c 6042 int from, to, to_byte;
b73bfc1c 6043 int result;
73be902c
KH
6044 int shrinked_bytes = 0;
6045 Lisp_Object newstr;
2391eaa4 6046 int consumed, consumed_char, produced, produced_char;
b73bfc1c
KH
6047
6048 if (SYMBOLP (coding->pre_write_conversion)
6049 && !NILP (Ffboundp (coding->pre_write_conversion)))
6bac5b12 6050 str = run_pre_post_conversion_on_str (str, coding, 1);
b73bfc1c
KH
6051
6052 from = 0;
6053 to = XSTRING (str)->size;
6054 to_byte = STRING_BYTES (XSTRING (str));
6055
e2c06b17
KH
6056 /* Encoding routines determine the multibyteness of the source text
6057 by coding->src_multibyte. */
6058 coding->src_multibyte = STRING_MULTIBYTE (str);
6059 coding->dst_multibyte = 0;
b73bfc1c 6060 if (! CODING_REQUIRE_ENCODING (coding))
826bfb8b 6061 {
2391eaa4
KH
6062 coding->consumed = STRING_BYTES (XSTRING (str));
6063 coding->consumed_char = XSTRING (str)->size;
b73bfc1c
KH
6064 if (STRING_MULTIBYTE (str))
6065 {
6066 str = Fstring_as_unibyte (str);
6067 nocopy = 1;
6068 }
2391eaa4
KH
6069 coding->produced = STRING_BYTES (XSTRING (str));
6070 coding->produced_char = XSTRING (str)->size;
b73bfc1c 6071 return (nocopy ? str : Fcopy_sequence (str));
826bfb8b
KH
6072 }
6073
b73bfc1c
KH
6074 if (coding->composing != COMPOSITION_DISABLED)
6075 coding_save_composition (coding, from, to, str);
ec6d2bb8 6076
b73bfc1c 6077 /* Try to skip the heading and tailing ASCIIs. */
4956c225
KH
6078 if (coding->type != coding_type_ccl)
6079 {
4956c225
KH
6080 SHRINK_CONVERSION_REGION (&from, &to_byte, coding, XSTRING (str)->data,
6081 1);
6082 if (from == to_byte)
6083 return (nocopy ? str : Fcopy_sequence (str));
73be902c 6084 shrinked_bytes = from + (STRING_BYTES (XSTRING (str)) - to_byte);
4956c225 6085 }
b73bfc1c
KH
6086
6087 len = encoding_buffer_size (coding, to_byte - from);
73be902c
KH
6088 allocate_conversion_buffer (buf, len);
6089
2391eaa4 6090 consumed = consumed_char = produced = produced_char = 0;
73be902c
KH
6091 while (1)
6092 {
6093 result = encode_coding (coding, XSTRING (str)->data + from + consumed,
6094 buf.data + produced, to_byte - from - consumed,
6095 buf.size - produced);
6096 consumed += coding->consumed;
2391eaa4 6097 consumed_char += coding->consumed_char;
13004bef 6098 produced += coding->produced;
2391eaa4
KH
6099 produced_char += coding->produced_char;
6100 if (result == CODING_FINISH_NORMAL
6101 || (result == CODING_FINISH_INSUFFICIENT_SRC
6102 && coding->consumed == 0))
73be902c
KH
6103 break;
6104 /* Now result should be CODING_FINISH_INSUFFICIENT_DST. */
6105 extend_conversion_buffer (&buf);
6106 }
6107
2391eaa4
KH
6108 coding->consumed = consumed;
6109 coding->consumed_char = consumed_char;
6110 coding->produced = produced;
6111 coding->produced_char = produced_char;
6112
73be902c 6113 newstr = make_uninit_string (produced + shrinked_bytes);
b73bfc1c 6114 if (from > 0)
73be902c
KH
6115 bcopy (XSTRING (str)->data, XSTRING (newstr)->data, from);
6116 bcopy (buf.data, XSTRING (newstr)->data + from, produced);
6117 if (shrinked_bytes > from)
6118 bcopy (XSTRING (str)->data + to_byte,
6119 XSTRING (newstr)->data + from + produced,
6120 shrinked_bytes - from);
6121
6122 free_conversion_buffer (&buf);
ec6d2bb8 6123 coding_free_composition_data (coding);
b73bfc1c 6124
73be902c 6125 return newstr;
4ed46869
KH
6126}
6127
6128\f
6129#ifdef emacs
1397dc18 6130/*** 8. Emacs Lisp library functions ***/
4ed46869 6131
4ed46869 6132DEFUN ("coding-system-p", Fcoding_system_p, Scoding_system_p, 1, 1, 0,
48b0f3ae
PJ
6133 doc: /* Return t if OBJECT is nil or a coding-system.
6134See the documentation of `make-coding-system' for information
6135about coding-system objects. */)
6136 (obj)
4ed46869
KH
6137 Lisp_Object obj;
6138{
4608c386
KH
6139 if (NILP (obj))
6140 return Qt;
6141 if (!SYMBOLP (obj))
6142 return Qnil;
6143 /* Get coding-spec vector for OBJ. */
6144 obj = Fget (obj, Qcoding_system);
6145 return ((VECTORP (obj) && XVECTOR (obj)->size == 5)
6146 ? Qt : Qnil);
4ed46869
KH
6147}
6148
9d991de8
RS
6149DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system,
6150 Sread_non_nil_coding_system, 1, 1, 0,
48b0f3ae
PJ
6151 doc: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
6152 (prompt)
4ed46869
KH
6153 Lisp_Object prompt;
6154{
e0e989f6 6155 Lisp_Object val;
9d991de8
RS
6156 do
6157 {
4608c386
KH
6158 val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil,
6159 Qt, Qnil, Qcoding_system_history, Qnil, Qnil);
9d991de8
RS
6160 }
6161 while (XSTRING (val)->size == 0);
e0e989f6 6162 return (Fintern (val, Qnil));
4ed46869
KH
6163}
6164
9b787f3e 6165DEFUN ("read-coding-system", Fread_coding_system, Sread_coding_system, 1, 2, 0,
48b0f3ae
PJ
6166 doc: /* Read a coding system from the minibuffer, prompting with string PROMPT.
6167If the user enters null input, return second argument DEFAULT-CODING-SYSTEM. */)
6168 (prompt, default_coding_system)
9b787f3e 6169 Lisp_Object prompt, default_coding_system;
4ed46869 6170{
f44d27ce 6171 Lisp_Object val;
9b787f3e 6172 if (SYMBOLP (default_coding_system))
57d25e6f 6173 default_coding_system = SYMBOL_NAME (default_coding_system);
4608c386 6174 val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil,
9b787f3e
RS
6175 Qt, Qnil, Qcoding_system_history,
6176 default_coding_system, Qnil);
e0e989f6 6177 return (XSTRING (val)->size == 0 ? Qnil : Fintern (val, Qnil));
4ed46869
KH
6178}
6179
6180DEFUN ("check-coding-system", Fcheck_coding_system, Scheck_coding_system,
6181 1, 1, 0,
48b0f3ae
PJ
6182 doc: /* Check validity of CODING-SYSTEM.
6183If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
6184It is valid if it is a symbol with a non-nil `coding-system' property.
6185The value of property should be a vector of length 5. */)
6186 (coding_system)
4ed46869
KH
6187 Lisp_Object coding_system;
6188{
b7826503 6189 CHECK_SYMBOL (coding_system);
4ed46869
KH
6190 if (!NILP (Fcoding_system_p (coding_system)))
6191 return coding_system;
6192 while (1)
02ba4723 6193 Fsignal (Qcoding_system_error, Fcons (coding_system, Qnil));
4ed46869 6194}
3a73fa5d 6195\f
d46c5b12 6196Lisp_Object
0a28aafb 6197detect_coding_system (src, src_bytes, highest, multibytep)
d46c5b12
KH
6198 unsigned char *src;
6199 int src_bytes, highest;
0a28aafb 6200 int multibytep;
4ed46869
KH
6201{
6202 int coding_mask, eol_type;
d46c5b12
KH
6203 Lisp_Object val, tmp;
6204 int dummy;
4ed46869 6205
0a28aafb 6206 coding_mask = detect_coding_mask (src, src_bytes, NULL, &dummy, multibytep);
d46c5b12
KH
6207 eol_type = detect_eol_type (src, src_bytes, &dummy);
6208 if (eol_type == CODING_EOL_INCONSISTENT)
25b02698 6209 eol_type = CODING_EOL_UNDECIDED;
4ed46869 6210
d46c5b12 6211 if (!coding_mask)
4ed46869 6212 {
27901516 6213 val = Qundecided;
d46c5b12 6214 if (eol_type != CODING_EOL_UNDECIDED)
4ed46869 6215 {
f44d27ce
RS
6216 Lisp_Object val2;
6217 val2 = Fget (Qundecided, Qeol_type);
4ed46869
KH
6218 if (VECTORP (val2))
6219 val = XVECTOR (val2)->contents[eol_type];
6220 }
80e803b4 6221 return (highest ? val : Fcons (val, Qnil));
4ed46869 6222 }
4ed46869 6223
d46c5b12
KH
6224 /* At first, gather possible coding systems in VAL. */
6225 val = Qnil;
fa42c37f 6226 for (tmp = Vcoding_category_list; CONSP (tmp); tmp = XCDR (tmp))
4ed46869 6227 {
fa42c37f
KH
6228 Lisp_Object category_val, category_index;
6229
6230 category_index = Fget (XCAR (tmp), Qcoding_category_index);
6231 category_val = Fsymbol_value (XCAR (tmp));
6232 if (!NILP (category_val)
6233 && NATNUMP (category_index)
6234 && (coding_mask & (1 << XFASTINT (category_index))))
4ed46869 6235 {
fa42c37f 6236 val = Fcons (category_val, val);
d46c5b12
KH
6237 if (highest)
6238 break;
4ed46869
KH
6239 }
6240 }
d46c5b12
KH
6241 if (!highest)
6242 val = Fnreverse (val);
4ed46869 6243
65059037 6244 /* Then, replace the elements with subsidiary coding systems. */
fa42c37f 6245 for (tmp = val; CONSP (tmp); tmp = XCDR (tmp))
4ed46869 6246 {
65059037
RS
6247 if (eol_type != CODING_EOL_UNDECIDED
6248 && eol_type != CODING_EOL_INCONSISTENT)
4ed46869 6249 {
d46c5b12 6250 Lisp_Object eol;
03699b14 6251 eol = Fget (XCAR (tmp), Qeol_type);
d46c5b12 6252 if (VECTORP (eol))
f3fbd155 6253 XSETCAR (tmp, XVECTOR (eol)->contents[eol_type]);
4ed46869
KH
6254 }
6255 }
03699b14 6256 return (highest ? XCAR (val) : val);
93dec019 6257}
4ed46869 6258
d46c5b12
KH
6259DEFUN ("detect-coding-region", Fdetect_coding_region, Sdetect_coding_region,
6260 2, 3, 0,
48b0f3ae
PJ
6261 doc: /* Detect coding system of the text in the region between START and END.
6262Return a list of possible coding systems ordered by priority.
6263
6264If only ASCII characters are found, it returns a list of single element
6265`undecided' or its subsidiary coding system according to a detected
6266end-of-line format.
6267
6268If optional argument HIGHEST is non-nil, return the coding system of
6269highest priority. */)
6270 (start, end, highest)
d46c5b12
KH
6271 Lisp_Object start, end, highest;
6272{
6273 int from, to;
6274 int from_byte, to_byte;
682169fe 6275 int include_anchor_byte = 0;
6289dd10 6276
b7826503
PJ
6277 CHECK_NUMBER_COERCE_MARKER (start);
6278 CHECK_NUMBER_COERCE_MARKER (end);
4ed46869 6279
d46c5b12
KH
6280 validate_region (&start, &end);
6281 from = XINT (start), to = XINT (end);
6282 from_byte = CHAR_TO_BYTE (from);
6283 to_byte = CHAR_TO_BYTE (to);
6289dd10 6284
d46c5b12
KH
6285 if (from < GPT && to >= GPT)
6286 move_gap_both (to, to_byte);
c210f766
KH
6287 /* If we an anchor byte `\0' follows the region, we include it in
6288 the detecting source. Then code detectors can handle the tailing
6289 byte sequence more accurately.
6290
6291 Fix me: This is not an perfect solution. It is better that we
6292 add one more argument, say LAST_BLOCK, to all detect_coding_XXX.
6293 */
682169fe
KH
6294 if (to == Z || (to == GPT && GAP_SIZE > 0))
6295 include_anchor_byte = 1;
d46c5b12 6296 return detect_coding_system (BYTE_POS_ADDR (from_byte),
682169fe 6297 to_byte - from_byte + include_anchor_byte,
0a28aafb
KH
6298 !NILP (highest),
6299 !NILP (current_buffer
6300 ->enable_multibyte_characters));
d46c5b12 6301}
6289dd10 6302
d46c5b12
KH
6303DEFUN ("detect-coding-string", Fdetect_coding_string, Sdetect_coding_string,
6304 1, 2, 0,
48b0f3ae
PJ
6305 doc: /* Detect coding system of the text in STRING.
6306Return a list of possible coding systems ordered by priority.
6307
6308If only ASCII characters are found, it returns a list of single element
6309`undecided' or its subsidiary coding system according to a detected
6310end-of-line format.
6311
6312If optional argument HIGHEST is non-nil, return the coding system of
6313highest priority. */)
6314 (string, highest)
d46c5b12
KH
6315 Lisp_Object string, highest;
6316{
b7826503 6317 CHECK_STRING (string);
4ed46869 6318
d46c5b12 6319 return detect_coding_system (XSTRING (string)->data,
682169fe
KH
6320 /* "+ 1" is to include the anchor byte
6321 `\0'. With this, code detectors can
c210f766
KH
6322 handle the tailing bytes more
6323 accurately. */
682169fe 6324 STRING_BYTES (XSTRING (string)) + 1,
0a28aafb
KH
6325 !NILP (highest),
6326 STRING_MULTIBYTE (string));
4ed46869
KH
6327}
6328
05e6f5dc
KH
6329/* Return an intersection of lists L1 and L2. */
6330
6331static Lisp_Object
6332intersection (l1, l2)
6333 Lisp_Object l1, l2;
6334{
eef762fc 6335 Lisp_Object val = Fcons (Qnil, Qnil), tail;
05e6f5dc 6336
eef762fc 6337 for (tail = val; CONSP (l1); l1 = XCDR (l1))
05e6f5dc
KH
6338 {
6339 if (!NILP (Fmemq (XCAR (l1), l2)))
eef762fc
AS
6340 {
6341 XSETCDR (tail, Fcons (XCAR (l1), Qnil));
6342 tail = XCDR (tail);
6343 }
05e6f5dc 6344 }
eef762fc 6345 return XCDR (val);
05e6f5dc
KH
6346}
6347
6348
6349/* Subroutine for Fsafe_coding_systems_region_internal.
6350
6351 Return a list of coding systems that safely encode the multibyte
6352 text between P and PEND. SAFE_CODINGS, if non-nil, is a list of
6353 possible coding systems. If it is nil, it means that we have not
6354 yet found any coding systems.
6355
6356 WORK_TABLE is a copy of the char-table Vchar_coding_system_table. An
6357 element of WORK_TABLE is set to t once the element is looked up.
6358
6359 If a non-ASCII single byte char is found, set
6360 *single_byte_char_found to 1. */
6361
6362static Lisp_Object
6363find_safe_codings (p, pend, safe_codings, work_table, single_byte_char_found)
6364 unsigned char *p, *pend;
6365 Lisp_Object safe_codings, work_table;
6366 int *single_byte_char_found;
6367{
6368 int c, len, idx;
6369 Lisp_Object val;
6370
6371 while (p < pend)
6372 {
6373 c = STRING_CHAR_AND_LENGTH (p, pend - p, len);
6374 p += len;
6375 if (ASCII_BYTE_P (c))
6376 /* We can ignore ASCII characters here. */
6377 continue;
6378 if (SINGLE_BYTE_CHAR_P (c))
6379 *single_byte_char_found = 1;
6380 if (NILP (safe_codings))
6381 continue;
6382 /* Check the safe coding systems for C. */
6383 val = char_table_ref_and_index (work_table, c, &idx);
6384 if (EQ (val, Qt))
6385 /* This element was already checked. Ignore it. */
6386 continue;
6387 /* Remember that we checked this element. */
975f250a 6388 CHAR_TABLE_SET (work_table, make_number (idx), Qt);
05e6f5dc
KH
6389
6390 /* If there are some safe coding systems for C and we have
6391 already found the other set of coding systems for the
6392 different characters, get the intersection of them. */
6393 if (!EQ (safe_codings, Qt) && !NILP (val))
6394 val = intersection (safe_codings, val);
6395 safe_codings = val;
6396 }
6397 return safe_codings;
6398}
6399
6400
6401/* Return a list of coding systems that safely encode the text between
6402 START and END. If the text contains only ASCII or is unibyte,
6403 return t. */
6404
6405DEFUN ("find-coding-systems-region-internal",
6406 Ffind_coding_systems_region_internal,
6407 Sfind_coding_systems_region_internal, 2, 2, 0,
48b0f3ae
PJ
6408 doc: /* Internal use only. */)
6409 (start, end)
05e6f5dc
KH
6410 Lisp_Object start, end;
6411{
6412 Lisp_Object work_table, safe_codings;
6413 int non_ascii_p = 0;
6414 int single_byte_char_found = 0;
6415 unsigned char *p1, *p1end, *p2, *p2end, *p;
05e6f5dc
KH
6416
6417 if (STRINGP (start))
6418 {
6419 if (!STRING_MULTIBYTE (start))
6420 return Qt;
6421 p1 = XSTRING (start)->data, p1end = p1 + STRING_BYTES (XSTRING (start));
6422 p2 = p2end = p1end;
6423 if (XSTRING (start)->size != STRING_BYTES (XSTRING (start)))
6424 non_ascii_p = 1;
6425 }
6426 else
6427 {
6428 int from, to, stop;
6429
b7826503
PJ
6430 CHECK_NUMBER_COERCE_MARKER (start);
6431 CHECK_NUMBER_COERCE_MARKER (end);
05e6f5dc
KH
6432 if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end))
6433 args_out_of_range (start, end);
6434 if (NILP (current_buffer->enable_multibyte_characters))
6435 return Qt;
6436 from = CHAR_TO_BYTE (XINT (start));
6437 to = CHAR_TO_BYTE (XINT (end));
6438 stop = from < GPT_BYTE && GPT_BYTE < to ? GPT_BYTE : to;
6439 p1 = BYTE_POS_ADDR (from), p1end = p1 + (stop - from);
6440 if (stop == to)
6441 p2 = p2end = p1end;
6442 else
6443 p2 = BYTE_POS_ADDR (stop), p2end = p2 + (to - stop);
6444 if (XINT (end) - XINT (start) != to - from)
6445 non_ascii_p = 1;
6446 }
6447
6448 if (!non_ascii_p)
6449 {
6450 /* We are sure that the text contains no multibyte character.
6451 Check if it contains eight-bit-graphic. */
6452 p = p1;
6453 for (p = p1; p < p1end && ASCII_BYTE_P (*p); p++);
6454 if (p == p1end)
6455 {
93dec019 6456 for (p = p2; p < p2end && ASCII_BYTE_P (*p); p++);
05e6f5dc
KH
6457 if (p == p2end)
6458 return Qt;
6459 }
6460 }
6461
6462 /* The text contains non-ASCII characters. */
6463 work_table = Fcopy_sequence (Vchar_coding_system_table);
6464 safe_codings = find_safe_codings (p1, p1end, Qt, work_table,
6465 &single_byte_char_found);
6466 if (p2 < p2end)
6467 safe_codings = find_safe_codings (p2, p2end, safe_codings, work_table,
6468 &single_byte_char_found);
6469
176c92e6
SM
6470 if (EQ (safe_codings, Qt))
6471 ; /* Nothing to be done. */
6472 else if (!single_byte_char_found)
05e6f5dc
KH
6473 {
6474 /* Append generic coding systems. */
6475 Lisp_Object args[2];
6476 args[0] = safe_codings;
6477 args[1] = Fchar_table_extra_slot (Vchar_coding_system_table,
6478 make_number (0));
975f250a 6479 safe_codings = Fappend (2, args);
05e6f5dc
KH
6480 }
6481 else
109a5acb
KH
6482 safe_codings = Fcons (Qraw_text,
6483 Fcons (Qemacs_mule,
6484 Fcons (Qno_conversion, safe_codings)));
05e6f5dc
KH
6485 return safe_codings;
6486}
6487
6488
4031e2bf
KH
6489Lisp_Object
6490code_convert_region1 (start, end, coding_system, encodep)
d46c5b12 6491 Lisp_Object start, end, coding_system;
4031e2bf 6492 int encodep;
3a73fa5d
RS
6493{
6494 struct coding_system coding;
da55a2b7 6495 int from, to;
3a73fa5d 6496
b7826503
PJ
6497 CHECK_NUMBER_COERCE_MARKER (start);
6498 CHECK_NUMBER_COERCE_MARKER (end);
6499 CHECK_SYMBOL (coding_system);
3a73fa5d 6500
d46c5b12
KH
6501 validate_region (&start, &end);
6502 from = XFASTINT (start);
6503 to = XFASTINT (end);
6504
3a73fa5d 6505 if (NILP (coding_system))
d46c5b12
KH
6506 return make_number (to - from);
6507
3a73fa5d 6508 if (setup_coding_system (Fcheck_coding_system (coding_system), &coding) < 0)
57d25e6f 6509 error ("Invalid coding system: %s", XSTRING (SYMBOL_NAME (coding_system))->data);
3a73fa5d 6510
d46c5b12 6511 coding.mode |= CODING_MODE_LAST_BLOCK;
b73bfc1c
KH
6512 coding.src_multibyte = coding.dst_multibyte
6513 = !NILP (current_buffer->enable_multibyte_characters);
fb88bf2d
KH
6514 code_convert_region (from, CHAR_TO_BYTE (from), to, CHAR_TO_BYTE (to),
6515 &coding, encodep, 1);
f072a3e8 6516 Vlast_coding_system_used = coding.symbol;
fb88bf2d 6517 return make_number (coding.produced_char);
4031e2bf
KH
6518}
6519
6520DEFUN ("decode-coding-region", Fdecode_coding_region, Sdecode_coding_region,
6521 3, 3, "r\nzCoding system: ",
48b0f3ae
PJ
6522 doc: /* Decode the current region from the specified coding system.
6523When called from a program, takes three arguments:
6524START, END, and CODING-SYSTEM. START and END are buffer positions.
6525This function sets `last-coding-system-used' to the precise coding system
6526used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
6527not fully specified.)
6528It returns the length of the decoded text. */)
6529 (start, end, coding_system)
4031e2bf
KH
6530 Lisp_Object start, end, coding_system;
6531{
6532 return code_convert_region1 (start, end, coding_system, 0);
3a73fa5d
RS
6533}
6534
6535DEFUN ("encode-coding-region", Fencode_coding_region, Sencode_coding_region,
6536 3, 3, "r\nzCoding system: ",
48b0f3ae
PJ
6537 doc: /* Encode the current region into the specified coding system.
6538When called from a program, takes three arguments:
6539START, END, and CODING-SYSTEM. START and END are buffer positions.
6540This function sets `last-coding-system-used' to the precise coding system
6541used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
6542not fully specified.)
6543It returns the length of the encoded text. */)
6544 (start, end, coding_system)
d46c5b12 6545 Lisp_Object start, end, coding_system;
3a73fa5d 6546{
4031e2bf
KH
6547 return code_convert_region1 (start, end, coding_system, 1);
6548}
3a73fa5d 6549
4031e2bf
KH
6550Lisp_Object
6551code_convert_string1 (string, coding_system, nocopy, encodep)
6552 Lisp_Object string, coding_system, nocopy;
6553 int encodep;
6554{
6555 struct coding_system coding;
3a73fa5d 6556
b7826503
PJ
6557 CHECK_STRING (string);
6558 CHECK_SYMBOL (coding_system);
4ed46869 6559
d46c5b12 6560 if (NILP (coding_system))
4031e2bf 6561 return (NILP (nocopy) ? Fcopy_sequence (string) : string);
4ed46869 6562
d46c5b12 6563 if (setup_coding_system (Fcheck_coding_system (coding_system), &coding) < 0)
57d25e6f 6564 error ("Invalid coding system: %s", XSTRING (SYMBOL_NAME (coding_system))->data);
5f1cd180 6565
d46c5b12 6566 coding.mode |= CODING_MODE_LAST_BLOCK;
b73bfc1c
KH
6567 string = (encodep
6568 ? encode_coding_string (string, &coding, !NILP (nocopy))
6569 : decode_coding_string (string, &coding, !NILP (nocopy)));
f072a3e8 6570 Vlast_coding_system_used = coding.symbol;
ec6d2bb8
KH
6571
6572 return string;
4ed46869
KH
6573}
6574
4ed46869 6575DEFUN ("decode-coding-string", Fdecode_coding_string, Sdecode_coding_string,
e0e989f6 6576 2, 3, 0,
48b0f3ae
PJ
6577 doc: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
6578Optional arg NOCOPY non-nil means it is OK to return STRING itself
6579if the decoding operation is trivial.
6580This function sets `last-coding-system-used' to the precise coding system
6581used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
6582not fully specified.) */)
6583 (string, coding_system, nocopy)
e0e989f6 6584 Lisp_Object string, coding_system, nocopy;
4ed46869 6585{
f072a3e8 6586 return code_convert_string1 (string, coding_system, nocopy, 0);
4ed46869
KH
6587}
6588
6589DEFUN ("encode-coding-string", Fencode_coding_string, Sencode_coding_string,
e0e989f6 6590 2, 3, 0,
48b0f3ae
PJ
6591 doc: /* Encode STRING to CODING-SYSTEM, and return the result.
6592Optional arg NOCOPY non-nil means it is OK to return STRING itself
6593if the encoding operation is trivial.
6594This function sets `last-coding-system-used' to the precise coding system
6595used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
6596not fully specified.) */)
6597 (string, coding_system, nocopy)
e0e989f6 6598 Lisp_Object string, coding_system, nocopy;
4ed46869 6599{
f072a3e8 6600 return code_convert_string1 (string, coding_system, nocopy, 1);
4ed46869 6601}
4031e2bf 6602
ecec61c1 6603/* Encode or decode STRING according to CODING_SYSTEM.
ec6d2bb8
KH
6604 Do not set Vlast_coding_system_used.
6605
6606 This function is called only from macros DECODE_FILE and
6607 ENCODE_FILE, thus we ignore character composition. */
ecec61c1
KH
6608
6609Lisp_Object
6610code_convert_string_norecord (string, coding_system, encodep)
6611 Lisp_Object string, coding_system;
6612 int encodep;
6613{
6614 struct coding_system coding;
6615
b7826503
PJ
6616 CHECK_STRING (string);
6617 CHECK_SYMBOL (coding_system);
ecec61c1
KH
6618
6619 if (NILP (coding_system))
6620 return string;
6621
6622 if (setup_coding_system (Fcheck_coding_system (coding_system), &coding) < 0)
57d25e6f 6623 error ("Invalid coding system: %s", XSTRING (SYMBOL_NAME (coding_system))->data);
ecec61c1 6624
ec6d2bb8 6625 coding.composing = COMPOSITION_DISABLED;
ecec61c1 6626 coding.mode |= CODING_MODE_LAST_BLOCK;
b73bfc1c
KH
6627 return (encodep
6628 ? encode_coding_string (string, &coding, 1)
6629 : decode_coding_string (string, &coding, 1));
ecec61c1 6630}
3a73fa5d 6631\f
4ed46869 6632DEFUN ("decode-sjis-char", Fdecode_sjis_char, Sdecode_sjis_char, 1, 1, 0,
48b0f3ae
PJ
6633 doc: /* Decode a Japanese character which has CODE in shift_jis encoding.
6634Return the corresponding character. */)
6635 (code)
4ed46869
KH
6636 Lisp_Object code;
6637{
6638 unsigned char c1, c2, s1, s2;
6639 Lisp_Object val;
6640
b7826503 6641 CHECK_NUMBER (code);
4ed46869 6642 s1 = (XFASTINT (code)) >> 8, s2 = (XFASTINT (code)) & 0xFF;
55ab7be3
KH
6643 if (s1 == 0)
6644 {
c28a9453
KH
6645 if (s2 < 0x80)
6646 XSETFASTINT (val, s2);
6647 else if (s2 >= 0xA0 || s2 <= 0xDF)
b73bfc1c 6648 XSETFASTINT (val, MAKE_CHAR (charset_katakana_jisx0201, s2, 0));
c28a9453 6649 else
9da8350f 6650 error ("Invalid Shift JIS code: %x", XFASTINT (code));
55ab7be3
KH
6651 }
6652 else
6653 {
87323294 6654 if ((s1 < 0x80 || (s1 > 0x9F && s1 < 0xE0) || s1 > 0xEF)
55ab7be3 6655 || (s2 < 0x40 || s2 == 0x7F || s2 > 0xFC))
9da8350f 6656 error ("Invalid Shift JIS code: %x", XFASTINT (code));
55ab7be3 6657 DECODE_SJIS (s1, s2, c1, c2);
b73bfc1c 6658 XSETFASTINT (val, MAKE_CHAR (charset_jisx0208, c1, c2));
55ab7be3 6659 }
4ed46869
KH
6660 return val;
6661}
6662
6663DEFUN ("encode-sjis-char", Fencode_sjis_char, Sencode_sjis_char, 1, 1, 0,
48b0f3ae
PJ
6664 doc: /* Encode a Japanese character CHAR to shift_jis encoding.
6665Return the corresponding code in SJIS. */)
6666 (ch)
4ed46869
KH
6667 Lisp_Object ch;
6668{
bcf26d6a 6669 int charset, c1, c2, s1, s2;
4ed46869
KH
6670 Lisp_Object val;
6671
b7826503 6672 CHECK_NUMBER (ch);
4ed46869 6673 SPLIT_CHAR (XFASTINT (ch), charset, c1, c2);
c28a9453
KH
6674 if (charset == CHARSET_ASCII)
6675 {
6676 val = ch;
6677 }
6678 else if (charset == charset_jisx0208
6679 && c1 > 0x20 && c1 < 0x7F && c2 > 0x20 && c2 < 0x7F)
4ed46869
KH
6680 {
6681 ENCODE_SJIS (c1, c2, s1, s2);
bcf26d6a 6682 XSETFASTINT (val, (s1 << 8) | s2);
4ed46869 6683 }
55ab7be3
KH
6684 else if (charset == charset_katakana_jisx0201
6685 && c1 > 0x20 && c2 < 0xE0)
6686 {
6687 XSETFASTINT (val, c1 | 0x80);
6688 }
4ed46869 6689 else
55ab7be3 6690 error ("Can't encode to shift_jis: %d", XFASTINT (ch));
4ed46869
KH
6691 return val;
6692}
6693
6694DEFUN ("decode-big5-char", Fdecode_big5_char, Sdecode_big5_char, 1, 1, 0,
48b0f3ae
PJ
6695 doc: /* Decode a Big5 character which has CODE in BIG5 coding system.
6696Return the corresponding character. */)
6697 (code)
4ed46869
KH
6698 Lisp_Object code;
6699{
6700 int charset;
6701 unsigned char b1, b2, c1, c2;
6702 Lisp_Object val;
6703
b7826503 6704 CHECK_NUMBER (code);
4ed46869 6705 b1 = (XFASTINT (code)) >> 8, b2 = (XFASTINT (code)) & 0xFF;
c28a9453
KH
6706 if (b1 == 0)
6707 {
6708 if (b2 >= 0x80)
9da8350f 6709 error ("Invalid BIG5 code: %x", XFASTINT (code));
c28a9453
KH
6710 val = code;
6711 }
6712 else
6713 {
6714 if ((b1 < 0xA1 || b1 > 0xFE)
6715 || (b2 < 0x40 || (b2 > 0x7E && b2 < 0xA1) || b2 > 0xFE))
9da8350f 6716 error ("Invalid BIG5 code: %x", XFASTINT (code));
c28a9453 6717 DECODE_BIG5 (b1, b2, charset, c1, c2);
b73bfc1c 6718 XSETFASTINT (val, MAKE_CHAR (charset, c1, c2));
c28a9453 6719 }
4ed46869
KH
6720 return val;
6721}
6722
6723DEFUN ("encode-big5-char", Fencode_big5_char, Sencode_big5_char, 1, 1, 0,
48b0f3ae
PJ
6724 doc: /* Encode the Big5 character CHAR to BIG5 coding system.
6725Return the corresponding character code in Big5. */)
6726 (ch)
4ed46869
KH
6727 Lisp_Object ch;
6728{
bcf26d6a 6729 int charset, c1, c2, b1, b2;
4ed46869
KH
6730 Lisp_Object val;
6731
b7826503 6732 CHECK_NUMBER (ch);
4ed46869 6733 SPLIT_CHAR (XFASTINT (ch), charset, c1, c2);
c28a9453
KH
6734 if (charset == CHARSET_ASCII)
6735 {
6736 val = ch;
6737 }
6738 else if ((charset == charset_big5_1
6739 && (XFASTINT (ch) >= 0x250a1 && XFASTINT (ch) <= 0x271ec))
6740 || (charset == charset_big5_2
6741 && XFASTINT (ch) >= 0x290a1 && XFASTINT (ch) <= 0x2bdb2))
4ed46869
KH
6742 {
6743 ENCODE_BIG5 (charset, c1, c2, b1, b2);
bcf26d6a 6744 XSETFASTINT (val, (b1 << 8) | b2);
4ed46869
KH
6745 }
6746 else
c28a9453 6747 error ("Can't encode to Big5: %d", XFASTINT (ch));
4ed46869
KH
6748 return val;
6749}
3a73fa5d 6750\f
1ba9e4ab
KH
6751DEFUN ("set-terminal-coding-system-internal",
6752 Fset_terminal_coding_system_internal,
48b0f3ae
PJ
6753 Sset_terminal_coding_system_internal, 1, 1, 0,
6754 doc: /* Internal use only. */)
6755 (coding_system)
4ed46869
KH
6756 Lisp_Object coding_system;
6757{
b7826503 6758 CHECK_SYMBOL (coding_system);
4ed46869 6759 setup_coding_system (Fcheck_coding_system (coding_system), &terminal_coding);
70c22245 6760 /* We had better not send unsafe characters to terminal. */
6e85d753 6761 terminal_coding.flags |= CODING_FLAG_ISO_SAFE;
8ca3766a 6762 /* Character composition should be disabled. */
ec6d2bb8 6763 terminal_coding.composing = COMPOSITION_DISABLED;
bd64290d
KH
6764 /* Error notification should be suppressed. */
6765 terminal_coding.suppress_error = 1;
b73bfc1c
KH
6766 terminal_coding.src_multibyte = 1;
6767 terminal_coding.dst_multibyte = 0;
4ed46869
KH
6768 return Qnil;
6769}
6770
c4825358
KH
6771DEFUN ("set-safe-terminal-coding-system-internal",
6772 Fset_safe_terminal_coding_system_internal,
48b0f3ae 6773 Sset_safe_terminal_coding_system_internal, 1, 1, 0,
ddb67bdc 6774 doc: /* Internal use only. */)
48b0f3ae 6775 (coding_system)
c4825358
KH
6776 Lisp_Object coding_system;
6777{
b7826503 6778 CHECK_SYMBOL (coding_system);
c4825358
KH
6779 setup_coding_system (Fcheck_coding_system (coding_system),
6780 &safe_terminal_coding);
8ca3766a 6781 /* Character composition should be disabled. */
ec6d2bb8 6782 safe_terminal_coding.composing = COMPOSITION_DISABLED;
bd64290d
KH
6783 /* Error notification should be suppressed. */
6784 terminal_coding.suppress_error = 1;
b73bfc1c
KH
6785 safe_terminal_coding.src_multibyte = 1;
6786 safe_terminal_coding.dst_multibyte = 0;
c4825358
KH
6787 return Qnil;
6788}
6789
4ed46869
KH
6790DEFUN ("terminal-coding-system",
6791 Fterminal_coding_system, Sterminal_coding_system, 0, 0, 0,
48b0f3ae
PJ
6792 doc: /* Return coding system specified for terminal output. */)
6793 ()
4ed46869
KH
6794{
6795 return terminal_coding.symbol;
6796}
6797
1ba9e4ab
KH
6798DEFUN ("set-keyboard-coding-system-internal",
6799 Fset_keyboard_coding_system_internal,
48b0f3ae
PJ
6800 Sset_keyboard_coding_system_internal, 1, 1, 0,
6801 doc: /* Internal use only. */)
6802 (coding_system)
4ed46869
KH
6803 Lisp_Object coding_system;
6804{
b7826503 6805 CHECK_SYMBOL (coding_system);
4ed46869 6806 setup_coding_system (Fcheck_coding_system (coding_system), &keyboard_coding);
8ca3766a 6807 /* Character composition should be disabled. */
ec6d2bb8 6808 keyboard_coding.composing = COMPOSITION_DISABLED;
4ed46869
KH
6809 return Qnil;
6810}
6811
6812DEFUN ("keyboard-coding-system",
6813 Fkeyboard_coding_system, Skeyboard_coding_system, 0, 0, 0,
48b0f3ae
PJ
6814 doc: /* Return coding system specified for decoding keyboard input. */)
6815 ()
4ed46869
KH
6816{
6817 return keyboard_coding.symbol;
6818}
6819
6820\f
a5d301df
KH
6821DEFUN ("find-operation-coding-system", Ffind_operation_coding_system,
6822 Sfind_operation_coding_system, 1, MANY, 0,
48b0f3ae
PJ
6823 doc: /* Choose a coding system for an operation based on the target name.
6824The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
6825DECODING-SYSTEM is the coding system to use for decoding
6826\(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
6827for encoding (in case OPERATION does encoding).
6828
6829The first argument OPERATION specifies an I/O primitive:
6830 For file I/O, `insert-file-contents' or `write-region'.
6831 For process I/O, `call-process', `call-process-region', or `start-process'.
6832 For network I/O, `open-network-stream'.
6833
6834The remaining arguments should be the same arguments that were passed
6835to the primitive. Depending on which primitive, one of those arguments
6836is selected as the TARGET. For example, if OPERATION does file I/O,
6837whichever argument specifies the file name is TARGET.
6838
6839TARGET has a meaning which depends on OPERATION:
6840 For file I/O, TARGET is a file name.
6841 For process I/O, TARGET is a process name.
6842 For network I/O, TARGET is a service name or a port number
6843
6844This function looks up what specified for TARGET in,
6845`file-coding-system-alist', `process-coding-system-alist',
6846or `network-coding-system-alist' depending on OPERATION.
6847They may specify a coding system, a cons of coding systems,
6848or a function symbol to call.
6849In the last case, we call the function with one argument,
6850which is a list of all the arguments given to this function.
6851
6852usage: (find-operation-coding-system OPERATION ARGUMENTS ...) */)
6853 (nargs, args)
4ed46869
KH
6854 int nargs;
6855 Lisp_Object *args;
6856{
6857 Lisp_Object operation, target_idx, target, val;
6858 register Lisp_Object chain;
6859
6860 if (nargs < 2)
6861 error ("Too few arguments");
6862 operation = args[0];
6863 if (!SYMBOLP (operation)
6864 || !INTEGERP (target_idx = Fget (operation, Qtarget_idx)))
8ca3766a 6865 error ("Invalid first argument");
4ed46869
KH
6866 if (nargs < 1 + XINT (target_idx))
6867 error ("Too few arguments for operation: %s",
57d25e6f 6868 XSTRING (SYMBOL_NAME (operation))->data);
4ed46869
KH
6869 target = args[XINT (target_idx) + 1];
6870 if (!(STRINGP (target)
6871 || (EQ (operation, Qopen_network_stream) && INTEGERP (target))))
8ca3766a 6872 error ("Invalid argument %d", XINT (target_idx) + 1);
4ed46869 6873
2e34157c
RS
6874 chain = ((EQ (operation, Qinsert_file_contents)
6875 || EQ (operation, Qwrite_region))
02ba4723 6876 ? Vfile_coding_system_alist
2e34157c 6877 : (EQ (operation, Qopen_network_stream)
02ba4723
KH
6878 ? Vnetwork_coding_system_alist
6879 : Vprocess_coding_system_alist));
4ed46869
KH
6880 if (NILP (chain))
6881 return Qnil;
6882
03699b14 6883 for (; CONSP (chain); chain = XCDR (chain))
4ed46869 6884 {
f44d27ce 6885 Lisp_Object elt;
03699b14 6886 elt = XCAR (chain);
4ed46869
KH
6887
6888 if (CONSP (elt)
6889 && ((STRINGP (target)
03699b14
KR
6890 && STRINGP (XCAR (elt))
6891 && fast_string_match (XCAR (elt), target) >= 0)
6892 || (INTEGERP (target) && EQ (target, XCAR (elt)))))
02ba4723 6893 {
03699b14 6894 val = XCDR (elt);
b19fd4c5
KH
6895 /* Here, if VAL is both a valid coding system and a valid
6896 function symbol, we return VAL as a coding system. */
02ba4723
KH
6897 if (CONSP (val))
6898 return val;
6899 if (! SYMBOLP (val))
6900 return Qnil;
6901 if (! NILP (Fcoding_system_p (val)))
6902 return Fcons (val, val);
b19fd4c5
KH
6903 if (! NILP (Ffboundp (val)))
6904 {
6905 val = call1 (val, Flist (nargs, args));
6906 if (CONSP (val))
6907 return val;
6908 if (SYMBOLP (val) && ! NILP (Fcoding_system_p (val)))
6909 return Fcons (val, val);
6910 }
02ba4723
KH
6911 return Qnil;
6912 }
4ed46869
KH
6913 }
6914 return Qnil;
6915}
6916
1397dc18
KH
6917DEFUN ("update-coding-systems-internal", Fupdate_coding_systems_internal,
6918 Supdate_coding_systems_internal, 0, 0, 0,
48b0f3ae
PJ
6919 doc: /* Update internal database for ISO2022 and CCL based coding systems.
6920When values of any coding categories are changed, you must
6921call this function. */)
6922 ()
d46c5b12
KH
6923{
6924 int i;
6925
fa42c37f 6926 for (i = CODING_CATEGORY_IDX_EMACS_MULE; i < CODING_CATEGORY_IDX_MAX; i++)
d46c5b12 6927 {
1397dc18
KH
6928 Lisp_Object val;
6929
f5c1dd0d 6930 val = SYMBOL_VALUE (XVECTOR (Vcoding_category_table)->contents[i]);
1397dc18
KH
6931 if (!NILP (val))
6932 {
6933 if (! coding_system_table[i])
6934 coding_system_table[i] = ((struct coding_system *)
6935 xmalloc (sizeof (struct coding_system)));
6936 setup_coding_system (val, coding_system_table[i]);
6937 }
6938 else if (coding_system_table[i])
6939 {
6940 xfree (coding_system_table[i]);
6941 coding_system_table[i] = NULL;
6942 }
d46c5b12 6943 }
1397dc18 6944
d46c5b12
KH
6945 return Qnil;
6946}
6947
66cfb530
KH
6948DEFUN ("set-coding-priority-internal", Fset_coding_priority_internal,
6949 Sset_coding_priority_internal, 0, 0, 0,
48b0f3ae
PJ
6950 doc: /* Update internal database for the current value of `coding-category-list'.
6951This function is internal use only. */)
6952 ()
66cfb530
KH
6953{
6954 int i = 0, idx;
84d60297
RS
6955 Lisp_Object val;
6956
6957 val = Vcoding_category_list;
66cfb530
KH
6958
6959 while (CONSP (val) && i < CODING_CATEGORY_IDX_MAX)
6960 {
03699b14 6961 if (! SYMBOLP (XCAR (val)))
66cfb530 6962 break;
03699b14 6963 idx = XFASTINT (Fget (XCAR (val), Qcoding_category_index));
66cfb530
KH
6964 if (idx >= CODING_CATEGORY_IDX_MAX)
6965 break;
6966 coding_priorities[i++] = (1 << idx);
03699b14 6967 val = XCDR (val);
66cfb530
KH
6968 }
6969 /* If coding-category-list is valid and contains all coding
6970 categories, `i' should be CODING_CATEGORY_IDX_MAX now. If not,
fa42c37f 6971 the following code saves Emacs from crashing. */
66cfb530
KH
6972 while (i < CODING_CATEGORY_IDX_MAX)
6973 coding_priorities[i++] = CODING_CATEGORY_MASK_RAW_TEXT;
6974
6975 return Qnil;
6976}
6977
4ed46869
KH
6978#endif /* emacs */
6979
6980\f
1397dc18 6981/*** 9. Post-amble ***/
4ed46869 6982
dfcf069d 6983void
4ed46869
KH
6984init_coding_once ()
6985{
6986 int i;
6987
93dec019 6988 /* Emacs' internal format specific initialize routine. */
4ed46869
KH
6989 for (i = 0; i <= 0x20; i++)
6990 emacs_code_class[i] = EMACS_control_code;
6991 emacs_code_class[0x0A] = EMACS_linefeed_code;
6992 emacs_code_class[0x0D] = EMACS_carriage_return_code;
6993 for (i = 0x21 ; i < 0x7F; i++)
6994 emacs_code_class[i] = EMACS_ascii_code;
6995 emacs_code_class[0x7F] = EMACS_control_code;
ec6d2bb8 6996 for (i = 0x80; i < 0xFF; i++)
4ed46869
KH
6997 emacs_code_class[i] = EMACS_invalid_code;
6998 emacs_code_class[LEADING_CODE_PRIVATE_11] = EMACS_leading_code_3;
6999 emacs_code_class[LEADING_CODE_PRIVATE_12] = EMACS_leading_code_3;
7000 emacs_code_class[LEADING_CODE_PRIVATE_21] = EMACS_leading_code_4;
7001 emacs_code_class[LEADING_CODE_PRIVATE_22] = EMACS_leading_code_4;
7002
7003 /* ISO2022 specific initialize routine. */
7004 for (i = 0; i < 0x20; i++)
b73bfc1c 7005 iso_code_class[i] = ISO_control_0;
4ed46869
KH
7006 for (i = 0x21; i < 0x7F; i++)
7007 iso_code_class[i] = ISO_graphic_plane_0;
7008 for (i = 0x80; i < 0xA0; i++)
b73bfc1c 7009 iso_code_class[i] = ISO_control_1;
4ed46869
KH
7010 for (i = 0xA1; i < 0xFF; i++)
7011 iso_code_class[i] = ISO_graphic_plane_1;
7012 iso_code_class[0x20] = iso_code_class[0x7F] = ISO_0x20_or_0x7F;
7013 iso_code_class[0xA0] = iso_code_class[0xFF] = ISO_0xA0_or_0xFF;
7014 iso_code_class[ISO_CODE_CR] = ISO_carriage_return;
7015 iso_code_class[ISO_CODE_SO] = ISO_shift_out;
7016 iso_code_class[ISO_CODE_SI] = ISO_shift_in;
7017 iso_code_class[ISO_CODE_SS2_7] = ISO_single_shift_2_7;
7018 iso_code_class[ISO_CODE_ESC] = ISO_escape;
7019 iso_code_class[ISO_CODE_SS2] = ISO_single_shift_2;
7020 iso_code_class[ISO_CODE_SS3] = ISO_single_shift_3;
7021 iso_code_class[ISO_CODE_CSI] = ISO_control_sequence_introducer;
7022
e0e989f6
KH
7023 setup_coding_system (Qnil, &keyboard_coding);
7024 setup_coding_system (Qnil, &terminal_coding);
c4825358 7025 setup_coding_system (Qnil, &safe_terminal_coding);
6bc51348 7026 setup_coding_system (Qnil, &default_buffer_file_coding);
9ce27fde 7027
d46c5b12
KH
7028 bzero (coding_system_table, sizeof coding_system_table);
7029
66cfb530
KH
7030 bzero (ascii_skip_code, sizeof ascii_skip_code);
7031 for (i = 0; i < 128; i++)
7032 ascii_skip_code[i] = 1;
7033
9ce27fde
KH
7034#if defined (MSDOS) || defined (WINDOWSNT)
7035 system_eol_type = CODING_EOL_CRLF;
7036#else
7037 system_eol_type = CODING_EOL_LF;
7038#endif
b843d1ae
KH
7039
7040 inhibit_pre_post_conversion = 0;
e0e989f6
KH
7041}
7042
7043#ifdef emacs
7044
dfcf069d 7045void
e0e989f6
KH
7046syms_of_coding ()
7047{
7048 Qtarget_idx = intern ("target-idx");
7049 staticpro (&Qtarget_idx);
7050
bb0115a2
RS
7051 Qcoding_system_history = intern ("coding-system-history");
7052 staticpro (&Qcoding_system_history);
7053 Fset (Qcoding_system_history, Qnil);
7054
9ce27fde 7055 /* Target FILENAME is the first argument. */
e0e989f6 7056 Fput (Qinsert_file_contents, Qtarget_idx, make_number (0));
9ce27fde 7057 /* Target FILENAME is the third argument. */
e0e989f6
KH
7058 Fput (Qwrite_region, Qtarget_idx, make_number (2));
7059
7060 Qcall_process = intern ("call-process");
7061 staticpro (&Qcall_process);
9ce27fde 7062 /* Target PROGRAM is the first argument. */
e0e989f6
KH
7063 Fput (Qcall_process, Qtarget_idx, make_number (0));
7064
7065 Qcall_process_region = intern ("call-process-region");
7066 staticpro (&Qcall_process_region);
9ce27fde 7067 /* Target PROGRAM is the third argument. */
e0e989f6
KH
7068 Fput (Qcall_process_region, Qtarget_idx, make_number (2));
7069
7070 Qstart_process = intern ("start-process");
7071 staticpro (&Qstart_process);
9ce27fde 7072 /* Target PROGRAM is the third argument. */
e0e989f6
KH
7073 Fput (Qstart_process, Qtarget_idx, make_number (2));
7074
7075 Qopen_network_stream = intern ("open-network-stream");
7076 staticpro (&Qopen_network_stream);
9ce27fde 7077 /* Target SERVICE is the fourth argument. */
e0e989f6
KH
7078 Fput (Qopen_network_stream, Qtarget_idx, make_number (3));
7079
4ed46869
KH
7080 Qcoding_system = intern ("coding-system");
7081 staticpro (&Qcoding_system);
7082
7083 Qeol_type = intern ("eol-type");
7084 staticpro (&Qeol_type);
7085
7086 Qbuffer_file_coding_system = intern ("buffer-file-coding-system");
7087 staticpro (&Qbuffer_file_coding_system);
7088
7089 Qpost_read_conversion = intern ("post-read-conversion");
7090 staticpro (&Qpost_read_conversion);
7091
7092 Qpre_write_conversion = intern ("pre-write-conversion");
7093 staticpro (&Qpre_write_conversion);
7094
27901516
KH
7095 Qno_conversion = intern ("no-conversion");
7096 staticpro (&Qno_conversion);
7097
7098 Qundecided = intern ("undecided");
7099 staticpro (&Qundecided);
7100
4ed46869
KH
7101 Qcoding_system_p = intern ("coding-system-p");
7102 staticpro (&Qcoding_system_p);
7103
7104 Qcoding_system_error = intern ("coding-system-error");
7105 staticpro (&Qcoding_system_error);
7106
7107 Fput (Qcoding_system_error, Qerror_conditions,
7108 Fcons (Qcoding_system_error, Fcons (Qerror, Qnil)));
7109 Fput (Qcoding_system_error, Qerror_message,
9ce27fde 7110 build_string ("Invalid coding system"));
4ed46869 7111
d46c5b12
KH
7112 Qcoding_category = intern ("coding-category");
7113 staticpro (&Qcoding_category);
4ed46869
KH
7114 Qcoding_category_index = intern ("coding-category-index");
7115 staticpro (&Qcoding_category_index);
7116
d46c5b12
KH
7117 Vcoding_category_table
7118 = Fmake_vector (make_number (CODING_CATEGORY_IDX_MAX), Qnil);
7119 staticpro (&Vcoding_category_table);
4ed46869
KH
7120 {
7121 int i;
7122 for (i = 0; i < CODING_CATEGORY_IDX_MAX; i++)
7123 {
d46c5b12
KH
7124 XVECTOR (Vcoding_category_table)->contents[i]
7125 = intern (coding_category_name[i]);
7126 Fput (XVECTOR (Vcoding_category_table)->contents[i],
7127 Qcoding_category_index, make_number (i));
4ed46869
KH
7128 }
7129 }
7130
f967223b
KH
7131 Qtranslation_table = intern ("translation-table");
7132 staticpro (&Qtranslation_table);
1397dc18 7133 Fput (Qtranslation_table, Qchar_table_extra_slots, make_number (1));
bdd9fb48 7134
f967223b
KH
7135 Qtranslation_table_id = intern ("translation-table-id");
7136 staticpro (&Qtranslation_table_id);
84fbb8a0 7137
f967223b
KH
7138 Qtranslation_table_for_decode = intern ("translation-table-for-decode");
7139 staticpro (&Qtranslation_table_for_decode);
a5d301df 7140
f967223b
KH
7141 Qtranslation_table_for_encode = intern ("translation-table-for-encode");
7142 staticpro (&Qtranslation_table_for_encode);
a5d301df 7143
05e6f5dc
KH
7144 Qsafe_chars = intern ("safe-chars");
7145 staticpro (&Qsafe_chars);
7146
7147 Qchar_coding_system = intern ("char-coding-system");
7148 staticpro (&Qchar_coding_system);
7149
7150 /* Intern this now in case it isn't already done.
7151 Setting this variable twice is harmless.
7152 But don't staticpro it here--that is done in alloc.c. */
7153 Qchar_table_extra_slots = intern ("char-table-extra-slots");
7154 Fput (Qsafe_chars, Qchar_table_extra_slots, make_number (0));
0192762c 7155 Fput (Qchar_coding_system, Qchar_table_extra_slots, make_number (2));
70c22245 7156
1397dc18
KH
7157 Qvalid_codes = intern ("valid-codes");
7158 staticpro (&Qvalid_codes);
7159
9ce27fde
KH
7160 Qemacs_mule = intern ("emacs-mule");
7161 staticpro (&Qemacs_mule);
7162
d46c5b12
KH
7163 Qraw_text = intern ("raw-text");
7164 staticpro (&Qraw_text);
7165
4ed46869
KH
7166 defsubr (&Scoding_system_p);
7167 defsubr (&Sread_coding_system);
7168 defsubr (&Sread_non_nil_coding_system);
7169 defsubr (&Scheck_coding_system);
7170 defsubr (&Sdetect_coding_region);
d46c5b12 7171 defsubr (&Sdetect_coding_string);
05e6f5dc 7172 defsubr (&Sfind_coding_systems_region_internal);
4ed46869
KH
7173 defsubr (&Sdecode_coding_region);
7174 defsubr (&Sencode_coding_region);
7175 defsubr (&Sdecode_coding_string);
7176 defsubr (&Sencode_coding_string);
7177 defsubr (&Sdecode_sjis_char);
7178 defsubr (&Sencode_sjis_char);
7179 defsubr (&Sdecode_big5_char);
7180 defsubr (&Sencode_big5_char);
1ba9e4ab 7181 defsubr (&Sset_terminal_coding_system_internal);
c4825358 7182 defsubr (&Sset_safe_terminal_coding_system_internal);
4ed46869 7183 defsubr (&Sterminal_coding_system);
1ba9e4ab 7184 defsubr (&Sset_keyboard_coding_system_internal);
4ed46869 7185 defsubr (&Skeyboard_coding_system);
a5d301df 7186 defsubr (&Sfind_operation_coding_system);
1397dc18 7187 defsubr (&Supdate_coding_systems_internal);
66cfb530 7188 defsubr (&Sset_coding_priority_internal);
4ed46869 7189
4608c386 7190 DEFVAR_LISP ("coding-system-list", &Vcoding_system_list,
48b0f3ae
PJ
7191 doc: /* List of coding systems.
7192
7193Do not alter the value of this variable manually. This variable should be
7194updated by the functions `make-coding-system' and
7195`define-coding-system-alias'. */);
4608c386
KH
7196 Vcoding_system_list = Qnil;
7197
7198 DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist,
48b0f3ae
PJ
7199 doc: /* Alist of coding system names.
7200Each element is one element list of coding system name.
7201This variable is given to `completing-read' as TABLE argument.
7202
7203Do not alter the value of this variable manually. This variable should be
7204updated by the functions `make-coding-system' and
7205`define-coding-system-alias'. */);
4608c386
KH
7206 Vcoding_system_alist = Qnil;
7207
4ed46869 7208 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list,
48b0f3ae
PJ
7209 doc: /* List of coding-categories (symbols) ordered by priority.
7210
7211On detecting a coding system, Emacs tries code detection algorithms
7212associated with each coding-category one by one in this order. When
7213one algorithm agrees with a byte sequence of source text, the coding
7214system bound to the corresponding coding-category is selected. */);
4ed46869
KH
7215 {
7216 int i;
7217
7218 Vcoding_category_list = Qnil;
7219 for (i = CODING_CATEGORY_IDX_MAX - 1; i >= 0; i--)
7220 Vcoding_category_list
d46c5b12
KH
7221 = Fcons (XVECTOR (Vcoding_category_table)->contents[i],
7222 Vcoding_category_list);
4ed46869
KH
7223 }
7224
7225 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read,
48b0f3ae
PJ
7226 doc: /* Specify the coding system for read operations.
7227It is useful to bind this variable with `let', but do not set it globally.
7228If the value is a coding system, it is used for decoding on read operation.
7229If not, an appropriate element is used from one of the coding system alists:
7230There are three such tables, `file-coding-system-alist',
7231`process-coding-system-alist', and `network-coding-system-alist'. */);
4ed46869
KH
7232 Vcoding_system_for_read = Qnil;
7233
7234 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write,
48b0f3ae
PJ
7235 doc: /* Specify the coding system for write operations.
7236Programs bind this variable with `let', but you should not set it globally.
7237If the value is a coding system, it is used for encoding of output,
7238when writing it to a file and when sending it to a file or subprocess.
7239
7240If this does not specify a coding system, an appropriate element
7241is used from one of the coding system alists:
7242There are three such tables, `file-coding-system-alist',
7243`process-coding-system-alist', and `network-coding-system-alist'.
7244For output to files, if the above procedure does not specify a coding system,
7245the value of `buffer-file-coding-system' is used. */);
4ed46869
KH
7246 Vcoding_system_for_write = Qnil;
7247
7248 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used,
48b0f3ae 7249 doc: /* Coding system used in the latest file or process I/O. */);
4ed46869
KH
7250 Vlast_coding_system_used = Qnil;
7251
9ce27fde 7252 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion,
48b0f3ae
PJ
7253 doc: /* *Non-nil means always inhibit code conversion of end-of-line format.
7254See info node `Coding Systems' and info node `Text and Binary' concerning
7255such conversion. */);
9ce27fde
KH
7256 inhibit_eol_conversion = 0;
7257
ed29121d 7258 DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system,
48b0f3ae
PJ
7259 doc: /* Non-nil means process buffer inherits coding system of process output.
7260Bind it to t if the process output is to be treated as if it were a file
7261read from some filesystem. */);
ed29121d
EZ
7262 inherit_process_coding_system = 0;
7263
02ba4723 7264 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist,
48b0f3ae
PJ
7265 doc: /* Alist to decide a coding system to use for a file I/O operation.
7266The format is ((PATTERN . VAL) ...),
7267where PATTERN is a regular expression matching a file name,
7268VAL is a coding system, a cons of coding systems, or a function symbol.
7269If VAL is a coding system, it is used for both decoding and encoding
7270the file contents.
7271If VAL is a cons of coding systems, the car part is used for decoding,
7272and the cdr part is used for encoding.
7273If VAL is a function symbol, the function must return a coding system
0192762c 7274or a cons of coding systems which are used as above. The function gets
ff955d90 7275the arguments with which `find-operation-coding-system' was called.
48b0f3ae
PJ
7276
7277See also the function `find-operation-coding-system'
7278and the variable `auto-coding-alist'. */);
02ba4723
KH
7279 Vfile_coding_system_alist = Qnil;
7280
7281 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist,
48b0f3ae
PJ
7282 doc: /* Alist to decide a coding system to use for a process I/O operation.
7283The format is ((PATTERN . VAL) ...),
7284where PATTERN is a regular expression matching a program name,
7285VAL is a coding system, a cons of coding systems, or a function symbol.
7286If VAL is a coding system, it is used for both decoding what received
7287from the program and encoding what sent to the program.
7288If VAL is a cons of coding systems, the car part is used for decoding,
7289and the cdr part is used for encoding.
7290If VAL is a function symbol, the function must return a coding system
7291or a cons of coding systems which are used as above.
7292
7293See also the function `find-operation-coding-system'. */);
02ba4723
KH
7294 Vprocess_coding_system_alist = Qnil;
7295
7296 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist,
48b0f3ae
PJ
7297 doc: /* Alist to decide a coding system to use for a network I/O operation.
7298The format is ((PATTERN . VAL) ...),
7299where PATTERN is a regular expression matching a network service name
7300or is a port number to connect to,
7301VAL is a coding system, a cons of coding systems, or a function symbol.
7302If VAL is a coding system, it is used for both decoding what received
7303from the network stream and encoding what sent to the network stream.
7304If VAL is a cons of coding systems, the car part is used for decoding,
7305and the cdr part is used for encoding.
7306If VAL is a function symbol, the function must return a coding system
7307or a cons of coding systems which are used as above.
7308
7309See also the function `find-operation-coding-system'. */);
02ba4723 7310 Vnetwork_coding_system_alist = Qnil;
4ed46869 7311
68c45bf0 7312 DEFVAR_LISP ("locale-coding-system", &Vlocale_coding_system,
75205970
RS
7313 doc: /* Coding system to use with system messages.
7314Also used for decoding keyboard input on X Window system. */);
68c45bf0
PE
7315 Vlocale_coding_system = Qnil;
7316
005f0d35 7317 /* The eol mnemonics are reset in startup.el system-dependently. */
7722baf9 7318 DEFVAR_LISP ("eol-mnemonic-unix", &eol_mnemonic_unix,
48b0f3ae 7319 doc: /* *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
7722baf9 7320 eol_mnemonic_unix = build_string (":");
4ed46869 7321
7722baf9 7322 DEFVAR_LISP ("eol-mnemonic-dos", &eol_mnemonic_dos,
48b0f3ae 7323 doc: /* *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
7722baf9 7324 eol_mnemonic_dos = build_string ("\\");
4ed46869 7325
7722baf9 7326 DEFVAR_LISP ("eol-mnemonic-mac", &eol_mnemonic_mac,
48b0f3ae 7327 doc: /* *String displayed in mode line for MAC-like (CR) end-of-line format. */);
7722baf9 7328 eol_mnemonic_mac = build_string ("/");
4ed46869 7329
7722baf9 7330 DEFVAR_LISP ("eol-mnemonic-undecided", &eol_mnemonic_undecided,
48b0f3ae 7331 doc: /* *String displayed in mode line when end-of-line format is not yet determined. */);
7722baf9 7332 eol_mnemonic_undecided = build_string (":");
4ed46869 7333
84fbb8a0 7334 DEFVAR_LISP ("enable-character-translation", &Venable_character_translation,
48b0f3ae 7335 doc: /* *Non-nil enables character translation while encoding and decoding. */);
84fbb8a0 7336 Venable_character_translation = Qt;
bdd9fb48 7337
f967223b 7338 DEFVAR_LISP ("standard-translation-table-for-decode",
48b0f3ae
PJ
7339 &Vstandard_translation_table_for_decode,
7340 doc: /* Table for translating characters while decoding. */);
f967223b 7341 Vstandard_translation_table_for_decode = Qnil;
bdd9fb48 7342
f967223b 7343 DEFVAR_LISP ("standard-translation-table-for-encode",
48b0f3ae
PJ
7344 &Vstandard_translation_table_for_encode,
7345 doc: /* Table for translating characters while encoding. */);
f967223b 7346 Vstandard_translation_table_for_encode = Qnil;
4ed46869
KH
7347
7348 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_alist,
48b0f3ae
PJ
7349 doc: /* Alist of charsets vs revision numbers.
7350While encoding, if a charset (car part of an element) is found,
7351designate it with the escape sequence identifying revision (cdr part of the element). */);
4ed46869 7352 Vcharset_revision_alist = Qnil;
02ba4723
KH
7353
7354 DEFVAR_LISP ("default-process-coding-system",
7355 &Vdefault_process_coding_system,
48b0f3ae
PJ
7356 doc: /* Cons of coding systems used for process I/O by default.
7357The car part is used for decoding a process output,
7358the cdr part is used for encoding a text to be sent to a process. */);
02ba4723 7359 Vdefault_process_coding_system = Qnil;
c4825358 7360
3f003981 7361 DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table,
48b0f3ae
PJ
7362 doc: /* Table of extra Latin codes in the range 128..159 (inclusive).
7363This is a vector of length 256.
7364If Nth element is non-nil, the existence of code N in a file
7365\(or output of subprocess) doesn't prevent it to be detected as
7366a coding system of ISO 2022 variant which has a flag
7367`accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
7368or reading output of a subprocess.
7369Only 128th through 159th elements has a meaning. */);
3f003981 7370 Vlatin_extra_code_table = Fmake_vector (make_number (256), Qnil);
d46c5b12
KH
7371
7372 DEFVAR_LISP ("select-safe-coding-system-function",
7373 &Vselect_safe_coding_system_function,
48b0f3ae
PJ
7374 doc: /* Function to call to select safe coding system for encoding a text.
7375
7376If set, this function is called to force a user to select a proper
7377coding system which can encode the text in the case that a default
7378coding system used in each operation can't encode the text.
7379
7380The default value is `select-safe-coding-system' (which see). */);
d46c5b12
KH
7381 Vselect_safe_coding_system_function = Qnil;
7382
05e6f5dc 7383 DEFVAR_LISP ("char-coding-system-table", &Vchar_coding_system_table,
48b0f3ae
PJ
7384 doc: /* Char-table containing safe coding systems of each characters.
7385Each element doesn't include such generic coding systems that can
7386encode any characters. They are in the first extra slot. */);
05e6f5dc
KH
7387 Vchar_coding_system_table = Fmake_char_table (Qchar_coding_system, Qnil);
7388
22ab2303 7389 DEFVAR_BOOL ("inhibit-iso-escape-detection",
74383408 7390 &inhibit_iso_escape_detection,
48b0f3ae
PJ
7391 doc: /* If non-nil, Emacs ignores ISO2022's escape sequence on code detection.
7392
7393By default, on reading a file, Emacs tries to detect how the text is
7394encoded. This code detection is sensitive to escape sequences. If
7395the sequence is valid as ISO2022, the code is determined as one of
7396the ISO2022 encodings, and the file is decoded by the corresponding
7397coding system (e.g. `iso-2022-7bit').
7398
7399However, there may be a case that you want to read escape sequences in
7400a file as is. In such a case, you can set this variable to non-nil.
7401Then, as the code detection ignores any escape sequences, no file is
7402detected as encoded in some ISO2022 encoding. The result is that all
7403escape sequences become visible in a buffer.
7404
7405The default value is nil, and it is strongly recommended not to change
7406it. That is because many Emacs Lisp source files that contain
7407non-ASCII characters are encoded by the coding system `iso-2022-7bit'
7408in Emacs's distribution, and they won't be decoded correctly on
7409reading if you suppress escape sequence detection.
7410
7411The other way to read escape sequences in a file without decoding is
7412to explicitly specify some coding system that doesn't use ISO2022's
7413escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
74383408 7414 inhibit_iso_escape_detection = 0;
4ed46869
KH
7415}
7416
68c45bf0
PE
7417char *
7418emacs_strerror (error_number)
7419 int error_number;
7420{
7421 char *str;
7422
ca9c0567 7423 synchronize_system_messages_locale ();
68c45bf0
PE
7424 str = strerror (error_number);
7425
7426 if (! NILP (Vlocale_coding_system))
7427 {
7428 Lisp_Object dec = code_convert_string_norecord (build_string (str),
7429 Vlocale_coding_system,
7430 0);
7431 str = (char *) XSTRING (dec)->data;
7432 }
7433
7434 return str;
7435}
7436
4ed46869 7437#endif /* emacs */
c2f94ebc 7438