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