(re_search_2): Fix indentation.
[bpt/emacs.git] / src / coding.c
CommitLineData
4ed46869 1/* Coding system handler (conversion, detection, and etc).
4a2f9c6a 2 Copyright (C) 1995, 1997, 1998 Electrotechnical Laboratory, JAPAN.
203cb916 3 Licensed to the Free Software Foundation.
4ed46869 4
369314dc
KH
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
4ed46869 11
369314dc
KH
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
4ed46869 16
369314dc
KH
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
4ed46869
KH
21
22/*** TABLE OF CONTENTS ***
23
24 1. Preamble
0ef69138 25 2. Emacs' internal format (emacs-mule) handlers
4ed46869
KH
26 3. ISO2022 handlers
27 4. Shift-JIS and BIG5 handlers
28 5. End-of-line handlers
29 6. C library functions
30 7. Emacs Lisp library functions
31 8. Post-amble
32
33*/
34
35/*** GENERAL NOTE on CODING SYSTEM ***
36
37 Coding system is an encoding mechanism of one or more character
38 sets. Here's a list of coding systems which Emacs can handle. When
39 we say "decode", it means converting some other coding system to
0ef69138
KH
40 Emacs' internal format (emacs-internal), and when we say "encode",
41 it means converting the coding system emacs-mule to some other
42 coding system.
4ed46869 43
0ef69138 44 0. Emacs' internal format (emacs-mule)
4ed46869
KH
45
46 Emacs itself holds a multi-lingual character in a buffer and a string
f4dee582 47 in a special format. Details are described in section 2.
4ed46869
KH
48
49 1. ISO2022
50
51 The most famous coding system for multiple character sets. X's
f4dee582
RS
52 Compound Text, various EUCs (Extended Unix Code), and coding
53 systems used in Internet communication such as ISO-2022-JP are
54 all variants of ISO2022. Details are described in section 3.
4ed46869
KH
55
56 2. SJIS (or Shift-JIS or MS-Kanji-Code)
57
58 A coding system to encode character sets: ASCII, JISX0201, and
59 JISX0208. Widely used for PC's in Japan. Details are described in
f4dee582 60 section 4.
4ed46869
KH
61
62 3. BIG5
63
64 A coding system to encode character sets: ASCII and Big5. Widely
65 used by Chinese (mainly in Taiwan and Hong Kong). Details are
f4dee582
RS
66 described in section 4. In this file, when we write "BIG5"
67 (all uppercase), we mean the coding system, and when we write
68 "Big5" (capitalized), we mean the character set.
4ed46869 69
27901516
KH
70 4. Raw text
71
4608c386
KH
72 A coding system for a text containing random 8-bit code. Emacs does
73 no code conversion on such a text except for end-of-line format.
27901516
KH
74
75 5. Other
4ed46869 76
f4dee582 77 If a user wants to read/write a text encoded in a coding system not
4ed46869
KH
78 listed above, he can supply a decoder and an encoder for it in CCL
79 (Code Conversion Language) programs. Emacs executes the CCL program
80 while reading/writing.
81
d46c5b12
KH
82 Emacs represents a coding system by a Lisp symbol that has a property
83 `coding-system'. But, before actually using the coding system, the
4ed46869 84 information about it is set in a structure of type `struct
f4dee582 85 coding_system' for rapid processing. See section 6 for more details.
4ed46869
KH
86
87*/
88
89/*** GENERAL NOTES on END-OF-LINE FORMAT ***
90
91 How end-of-line of a text is encoded depends on a system. For
92 instance, Unix's format is just one byte of `line-feed' code,
f4dee582 93 whereas DOS's format is two-byte sequence of `carriage-return' and
d46c5b12
KH
94 `line-feed' codes. MacOS's format is usually one byte of
95 `carriage-return'.
4ed46869 96
f4dee582
RS
97 Since text characters encoding and end-of-line encoding are
98 independent, any coding system described above can take
4ed46869 99 any format of end-of-line. So, Emacs has information of format of
f4dee582 100 end-of-line in each coding-system. See section 6 for more details.
4ed46869
KH
101
102*/
103
104/*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
105
106 These functions check if a text between SRC and SRC_END is encoded
107 in the coding system category XXX. Each returns an integer value in
108 which appropriate flag bits for the category XXX is set. The flag
109 bits are defined in macros CODING_CATEGORY_MASK_XXX. Below is the
110 template of these functions. */
111#if 0
112int
0ef69138 113detect_coding_emacs_mule (src, src_end)
4ed46869
KH
114 unsigned char *src, *src_end;
115{
116 ...
117}
118#endif
119
120/*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
121
122 These functions decode SRC_BYTES length text at SOURCE encoded in
0ef69138 123 CODING to Emacs' internal format (emacs-mule). The resulting text
d46c5b12
KH
124 goes to a place pointed to by DESTINATION, the length of which
125 should not exceed DST_BYTES. These functions set the information of
126 original and decoded texts in the members produced, produced_char,
127 consumed, and consumed_char of the structure *CODING.
128
129 The return value is an integer (CODING_FINISH_XXX) indicating how
130 the decoding finished.
131
132 DST_BYTES zero means that source area and destination area are
133 overlapped, which means that we can produce a decoded text until it
134 reaches at the head of not-yet-decoded source text.
135
136 Below is a template of these functions. */
4ed46869 137#if 0
d46c5b12 138decode_coding_XXX (coding, source, destination, src_bytes, dst_bytes)
4ed46869
KH
139 struct coding_system *coding;
140 unsigned char *source, *destination;
141 int src_bytes, dst_bytes;
4ed46869
KH
142{
143 ...
144}
145#endif
146
147/*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
148
0ef69138
KH
149 These functions encode SRC_BYTES length text at SOURCE of Emacs'
150 internal format (emacs-mule) to CODING. The resulting text goes to
f4dee582 151 a place pointed to by DESTINATION, the length of which should not
d46c5b12
KH
152 exceed DST_BYTES. These functions set the information of
153 original and encoded texts in the members produced, produced_char,
154 consumed, and consumed_char of the structure *CODING.
155
156 The return value is an integer (CODING_FINISH_XXX) indicating how
157 the encoding finished.
158
159 DST_BYTES zero means that source area and destination area are
160 overlapped, which means that we can produce a decoded text until it
161 reaches at the head of not-yet-decoded source text.
162
163 Below is a template of these functions. */
4ed46869 164#if 0
d46c5b12 165encode_coding_XXX (coding, source, destination, src_bytes, dst_bytes)
4ed46869
KH
166 struct coding_system *coding;
167 unsigned char *source, *destination;
168 int src_bytes, dst_bytes;
4ed46869
KH
169{
170 ...
171}
172#endif
173
174/*** COMMONLY USED MACROS ***/
175
176/* The following three macros ONE_MORE_BYTE, TWO_MORE_BYTES, and
177 THREE_MORE_BYTES safely get one, two, and three bytes from the
178 source text respectively. If there are not enough bytes in the
179 source, they jump to `label_end_of_loop'. The caller should set
180 variables `src' and `src_end' to appropriate areas in advance. */
181
182#define ONE_MORE_BYTE(c1) \
183 do { \
184 if (src < src_end) \
185 c1 = *src++; \
186 else \
187 goto label_end_of_loop; \
188 } while (0)
189
190#define TWO_MORE_BYTES(c1, c2) \
191 do { \
192 if (src + 1 < src_end) \
193 c1 = *src++, c2 = *src++; \
194 else \
195 goto label_end_of_loop; \
196 } while (0)
197
198#define THREE_MORE_BYTES(c1, c2, c3) \
199 do { \
200 if (src + 2 < src_end) \
201 c1 = *src++, c2 = *src++, c3 = *src++; \
202 else \
203 goto label_end_of_loop; \
204 } while (0)
205
206/* The following three macros DECODE_CHARACTER_ASCII,
207 DECODE_CHARACTER_DIMENSION1, and DECODE_CHARACTER_DIMENSION2 put
208 the multi-byte form of a character of each class at the place
209 pointed by `dst'. The caller should set the variable `dst' to
210 point to an appropriate area and the variable `coding' to point to
211 the coding-system of the currently decoding text in advance. */
212
213/* Decode one ASCII character C. */
214
215#define DECODE_CHARACTER_ASCII(c) \
216 do { \
217 if (COMPOSING_P (coding->composing)) \
218 *dst++ = 0xA0, *dst++ = (c) | 0x80; \
219 else \
d46c5b12
KH
220 { \
221 *dst++ = (c); \
222 coding->produced_char++; \
223 } \
4ed46869
KH
224 } while (0)
225
f4dee582 226/* Decode one DIMENSION1 character whose charset is CHARSET and whose
4ed46869
KH
227 position-code is C. */
228
229#define DECODE_CHARACTER_DIMENSION1(charset, c) \
230 do { \
231 unsigned char leading_code = CHARSET_LEADING_CODE_BASE (charset); \
232 if (COMPOSING_P (coding->composing)) \
233 *dst++ = leading_code + 0x20; \
234 else \
d46c5b12
KH
235 { \
236 *dst++ = leading_code; \
237 coding->produced_char++; \
238 } \
4ed46869
KH
239 if (leading_code = CHARSET_LEADING_CODE_EXT (charset)) \
240 *dst++ = leading_code; \
241 *dst++ = (c) | 0x80; \
242 } while (0)
243
f4dee582 244/* Decode one DIMENSION2 character whose charset is CHARSET and whose
4ed46869
KH
245 position-codes are C1 and C2. */
246
247#define DECODE_CHARACTER_DIMENSION2(charset, c1, c2) \
248 do { \
249 DECODE_CHARACTER_DIMENSION1 (charset, c1); \
250 *dst++ = (c2) | 0x80; \
251 } while (0)
252
253\f
254/*** 1. Preamble ***/
255
256#include <stdio.h>
257
258#ifdef emacs
259
260#include <config.h>
261#include "lisp.h"
262#include "buffer.h"
263#include "charset.h"
264#include "ccl.h"
265#include "coding.h"
266#include "window.h"
267
268#else /* not emacs */
269
270#include "mulelib.h"
271
272#endif /* not emacs */
273
274Lisp_Object Qcoding_system, Qeol_type;
275Lisp_Object Qbuffer_file_coding_system;
276Lisp_Object Qpost_read_conversion, Qpre_write_conversion;
27901516 277Lisp_Object Qno_conversion, Qundecided;
bb0115a2 278Lisp_Object Qcoding_system_history;
70c22245 279Lisp_Object Qsafe_charsets;
4ed46869
KH
280
281extern Lisp_Object Qinsert_file_contents, Qwrite_region;
282Lisp_Object Qcall_process, Qcall_process_region, Qprocess_argument;
283Lisp_Object Qstart_process, Qopen_network_stream;
284Lisp_Object Qtarget_idx;
285
d46c5b12
KH
286Lisp_Object Vselect_safe_coding_system_function;
287
4ed46869
KH
288/* Mnemonic character of each format of end-of-line. */
289int eol_mnemonic_unix, eol_mnemonic_dos, eol_mnemonic_mac;
290/* Mnemonic character to indicate format of end-of-line is not yet
291 decided. */
292int eol_mnemonic_undecided;
293
9ce27fde
KH
294/* Format of end-of-line decided by system. This is CODING_EOL_LF on
295 Unix, CODING_EOL_CRLF on DOS/Windows, and CODING_EOL_CR on Mac. */
296int system_eol_type;
297
4ed46869
KH
298#ifdef emacs
299
4608c386
KH
300Lisp_Object Vcoding_system_list, Vcoding_system_alist;
301
302Lisp_Object Qcoding_system_p, Qcoding_system_error;
4ed46869 303
d46c5b12
KH
304/* Coding system emacs-mule and raw-text are for converting only
305 end-of-line format. */
306Lisp_Object Qemacs_mule, Qraw_text;
9ce27fde 307
4ed46869
KH
308/* Coding-systems are handed between Emacs Lisp programs and C internal
309 routines by the following three variables. */
310/* Coding-system for reading files and receiving data from process. */
311Lisp_Object Vcoding_system_for_read;
312/* Coding-system for writing files and sending data to process. */
313Lisp_Object Vcoding_system_for_write;
314/* Coding-system actually used in the latest I/O. */
315Lisp_Object Vlast_coding_system_used;
316
c4825358 317/* A vector of length 256 which contains information about special
3f003981
KH
318 Latin codes (espepcially for dealing with Microsoft code). */
319Lisp_Object Vlatin_extra_code_table;
c4825358 320
9ce27fde
KH
321/* Flag to inhibit code conversion of end-of-line format. */
322int inhibit_eol_conversion;
323
ed29121d
EZ
324/* Flag to make buffer-file-coding-system inherit from process-coding. */
325int inherit_process_coding_system;
326
c4825358 327/* Coding system to be used to encode text for terminal display. */
4ed46869
KH
328struct coding_system terminal_coding;
329
c4825358
KH
330/* Coding system to be used to encode text for terminal display when
331 terminal coding system is nil. */
332struct coding_system safe_terminal_coding;
333
334/* Coding system of what is sent from terminal keyboard. */
4ed46869
KH
335struct coding_system keyboard_coding;
336
02ba4723
KH
337Lisp_Object Vfile_coding_system_alist;
338Lisp_Object Vprocess_coding_system_alist;
339Lisp_Object Vnetwork_coding_system_alist;
4ed46869
KH
340
341#endif /* emacs */
342
d46c5b12 343Lisp_Object Qcoding_category, Qcoding_category_index;
4ed46869
KH
344
345/* List of symbols `coding-category-xxx' ordered by priority. */
346Lisp_Object Vcoding_category_list;
347
d46c5b12
KH
348/* Table of coding categories (Lisp symbols). */
349Lisp_Object Vcoding_category_table;
4ed46869
KH
350
351/* Table of names of symbol for each coding-category. */
352char *coding_category_name[CODING_CATEGORY_IDX_MAX] = {
0ef69138 353 "coding-category-emacs-mule",
4ed46869
KH
354 "coding-category-sjis",
355 "coding-category-iso-7",
d46c5b12 356 "coding-category-iso-7-tight",
4ed46869
KH
357 "coding-category-iso-8-1",
358 "coding-category-iso-8-2",
7717c392
KH
359 "coding-category-iso-7-else",
360 "coding-category-iso-8-else",
4ed46869 361 "coding-category-big5",
27901516 362 "coding-category-raw-text",
4ed46869
KH
363 "coding-category-binary"
364};
365
d46c5b12
KH
366/* Table pointers to coding systems corresponding to each coding
367 categories. */
368struct coding_system *coding_system_table[CODING_CATEGORY_IDX_MAX];
369
bdd9fb48
KH
370/* Flag to tell if we look up unification table on character code
371 conversion. */
372Lisp_Object Venable_character_unification;
a5d301df
KH
373/* Standard unification table to look up on decoding (reading). */
374Lisp_Object Vstandard_character_unification_table_for_decode;
375/* Standard unification table to look up on encoding (writing). */
376Lisp_Object Vstandard_character_unification_table_for_encode;
bdd9fb48
KH
377
378Lisp_Object Qcharacter_unification_table;
a5d301df
KH
379Lisp_Object Qcharacter_unification_table_for_decode;
380Lisp_Object Qcharacter_unification_table_for_encode;
4ed46869
KH
381
382/* Alist of charsets vs revision number. */
383Lisp_Object Vcharset_revision_alist;
384
02ba4723
KH
385/* Default coding systems used for process I/O. */
386Lisp_Object Vdefault_process_coding_system;
387
4ed46869 388\f
0ef69138 389/*** 2. Emacs internal format (emacs-mule) handlers ***/
4ed46869
KH
390
391/* Emacs' internal format for encoding multiple character sets is a
f4dee582
RS
392 kind of multi-byte encoding, i.e. characters are encoded by
393 variable-length sequences of one-byte codes. ASCII characters
394 and control characters (e.g. `tab', `newline') are represented by
395 one-byte sequences which are their ASCII codes, in the range 0x00
396 through 0x7F. The other characters are represented by a sequence
397 of `base leading-code', optional `extended leading-code', and one
398 or two `position-code's. The length of the sequence is determined
399 by the base leading-code. Leading-code takes the range 0x80
400 through 0x9F, whereas extended leading-code and position-code take
401 the range 0xA0 through 0xFF. See `charset.h' for more details
402 about leading-code and position-code.
403
404 There's one exception to this rule. Special leading-code
4ed46869
KH
405 `leading-code-composition' denotes that the following several
406 characters should be composed into one character. Leading-codes of
407 components (except for ASCII) are added 0x20. An ASCII character
408 component is represented by a 2-byte sequence of `0xA0' and
f4dee582
RS
409 `ASCII-code + 0x80'. See also the comments in `charset.h' for the
410 details of composite character. Hence, we can summarize the code
4ed46869
KH
411 range as follows:
412
413 --- CODE RANGE of Emacs' internal format ---
414 (character set) (range)
415 ASCII 0x00 .. 0x7F
416 ELSE (1st byte) 0x80 .. 0x9F
417 (rest bytes) 0xA0 .. 0xFF
418 ---------------------------------------------
419
420 */
421
422enum emacs_code_class_type emacs_code_class[256];
423
424/* Go to the next statement only if *SRC is accessible and the code is
425 greater than 0xA0. */
426#define CHECK_CODE_RANGE_A0_FF \
427 do { \
428 if (src >= src_end) \
429 goto label_end_of_switch; \
430 else if (*src++ < 0xA0) \
431 return 0; \
432 } while (0)
433
434/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
435 Check if a text is encoded in Emacs' internal format. If it is,
d46c5b12 436 return CODING_CATEGORY_MASK_EMACS_MULE, else return 0. */
4ed46869
KH
437
438int
0ef69138 439detect_coding_emacs_mule (src, src_end)
4ed46869
KH
440 unsigned char *src, *src_end;
441{
442 unsigned char c;
443 int composing = 0;
444
445 while (src < src_end)
446 {
447 c = *src++;
448
449 if (composing)
450 {
451 if (c < 0xA0)
452 composing = 0;
453 else
454 c -= 0x20;
455 }
456
457 switch (emacs_code_class[c])
458 {
459 case EMACS_ascii_code:
460 case EMACS_linefeed_code:
461 break;
462
463 case EMACS_control_code:
464 if (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)
465 return 0;
466 break;
467
468 case EMACS_invalid_code:
469 return 0;
470
471 case EMACS_leading_code_composition: /* c == 0x80 */
472 if (composing)
473 CHECK_CODE_RANGE_A0_FF;
474 else
475 composing = 1;
476 break;
477
478 case EMACS_leading_code_4:
479 CHECK_CODE_RANGE_A0_FF;
480 /* fall down to check it two more times ... */
481
482 case EMACS_leading_code_3:
483 CHECK_CODE_RANGE_A0_FF;
484 /* fall down to check it one more time ... */
485
486 case EMACS_leading_code_2:
487 CHECK_CODE_RANGE_A0_FF;
488 break;
489
490 default:
491 label_end_of_switch:
492 break;
493 }
494 }
0ef69138 495 return CODING_CATEGORY_MASK_EMACS_MULE;
4ed46869
KH
496}
497
498\f
499/*** 3. ISO2022 handlers ***/
500
501/* The following note describes the coding system ISO2022 briefly.
f4dee582
RS
502 Since the intention of this note is to help in understanding of
503 the programs in this file, some parts are NOT ACCURATE or OVERLY
4ed46869
KH
504 SIMPLIFIED. For the thorough understanding, please refer to the
505 original document of ISO2022.
506
507 ISO2022 provides many mechanisms to encode several character sets
f4dee582 508 in 7-bit and 8-bit environment. If one chooses 7-bite environment,
4ed46869 509 all text is encoded by codes of less than 128. This may make the
f4dee582
RS
510 encoded text a little bit longer, but the text gets more stability
511 to pass through several gateways (some of them strip off the MSB).
4ed46869 512
f4dee582 513 There are two kinds of character set: control character set and
4ed46869
KH
514 graphic character set. The former contains control characters such
515 as `newline' and `escape' to provide control functions (control
f4dee582 516 functions are provided also by escape sequences). The latter
4ed46869
KH
517 contains graphic characters such as ' A' and '-'. Emacs recognizes
518 two control character sets and many graphic character sets.
519
520 Graphic character sets are classified into one of the following
521 four classes, DIMENSION1_CHARS94, DIMENSION1_CHARS96,
522 DIMENSION2_CHARS94, DIMENSION2_CHARS96 according to the number of
523 bytes (DIMENSION) and the number of characters in one dimension
524 (CHARS) of the set. In addition, each character set is assigned an
525 identification tag (called "final character" and denoted as <F>
526 here after) which is unique in each class. <F> of each character
527 set is decided by ECMA(*) when it is registered in ISO. Code range
528 of <F> is 0x30..0x7F (0x30..0x3F are for private use only).
529
530 Note (*): ECMA = European Computer Manufacturers Association
531
532 Here are examples of graphic character set [NAME(<F>)]:
533 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
534 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
535 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
536 o DIMENSION2_CHARS96 -- none for the moment
537
538 A code area (1byte=8bits) is divided into 4 areas, C0, GL, C1, and GR.
539 C0 [0x00..0x1F] -- control character plane 0
540 GL [0x20..0x7F] -- graphic character plane 0
541 C1 [0x80..0x9F] -- control character plane 1
542 GR [0xA0..0xFF] -- graphic character plane 1
543
544 A control character set is directly designated and invoked to C0 or
545 C1 by an escape sequence. The most common case is that ISO646's
546 control character set is designated/invoked to C0 and ISO6429's
547 control character set is designated/invoked to C1, and usually
548 these designations/invocations are omitted in a coded text. With
549 7-bit environment, only C0 can be used, and a control character for
550 C1 is encoded by an appropriate escape sequence to fit in the
551 environment. All control characters for C1 are defined the
552 corresponding escape sequences.
553
554 A graphic character set is at first designated to one of four
555 graphic registers (G0 through G3), then these graphic registers are
556 invoked to GL or GR. These designations and invocations can be
557 done independently. The most common case is that G0 is invoked to
558 GL, G1 is invoked to GR, and ASCII is designated to G0, and usually
559 these invocations and designations are omitted in a coded text.
560 With 7-bit environment, only GL can be used.
561
562 When a graphic character set of CHARS94 is invoked to GL, code 0x20
563 and 0x7F of GL area work as control characters SPACE and DEL
564 respectively, and code 0xA0 and 0xFF of GR area should not be used.
565
566 There are two ways of invocation: locking-shift and single-shift.
567 With locking-shift, the invocation lasts until the next different
568 invocation, whereas with single-shift, the invocation works only
569 for the following character and doesn't affect locking-shift.
570 Invocations are done by the following control characters or escape
571 sequences.
572
573 ----------------------------------------------------------------------
574 function control char escape sequence description
575 ----------------------------------------------------------------------
576 SI (shift-in) 0x0F none invoke G0 to GL
10bff6f1 577 SO (shift-out) 0x0E none invoke G1 to GL
4ed46869
KH
578 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
579 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
580 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 into GL
581 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 into GL
582 ----------------------------------------------------------------------
583 The first four are for locking-shift. Control characters for these
584 functions are defined by macros ISO_CODE_XXX in `coding.h'.
585
586 Designations are done by the following escape sequences.
587 ----------------------------------------------------------------------
588 escape sequence description
589 ----------------------------------------------------------------------
590 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
591 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
592 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
593 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
594 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
595 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
596 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
597 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
598 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
599 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
600 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
601 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
602 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
603 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
604 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
605 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
606 ----------------------------------------------------------------------
607
608 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
609 of dimension 1, chars 94, and final character <F>, and etc.
610
611 Note (*): Although these designations are not allowed in ISO2022,
612 Emacs accepts them on decoding, and produces them on encoding
613 CHARS96 character set in a coding system which is characterized as
614 7-bit environment, non-locking-shift, and non-single-shift.
615
616 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
617 '(' can be omitted. We call this as "short-form" here after.
618
619 Now you may notice that there are a lot of ways for encoding the
f4dee582 620 same multilingual text in ISO2022. Actually, there exists many
4ed46869
KH
621 coding systems such as Compound Text (used in X's inter client
622 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
623 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
624 localized platforms), and all of these are variants of ISO2022.
625
626 In addition to the above, Emacs handles two more kinds of escape
627 sequences: ISO6429's direction specification and Emacs' private
628 sequence for specifying character composition.
629
630 ISO6429's direction specification takes the following format:
631 o CSI ']' -- end of the current direction
632 o CSI '0' ']' -- end of the current direction
633 o CSI '1' ']' -- start of left-to-right text
634 o CSI '2' ']' -- start of right-to-left text
635 The control character CSI (0x9B: control sequence introducer) is
636 abbreviated to the escape sequence ESC '[' in 7-bit environment.
637
638 Character composition specification takes the following format:
639 o ESC '0' -- start character composition
640 o ESC '1' -- end character composition
641 Since these are not standard escape sequences of any ISO, the use
642 of them for these meaning is restricted to Emacs only. */
643
644enum iso_code_class_type iso_code_class[256];
645
704c5781
KH
646#define CHARSET_OK(idx, charset) \
647 (coding_system_table[idx]->safe_charsets[charset] \
648 || (CODING_SPEC_ISO_REQUESTED_DESIGNATION \
649 (coding_system_table[idx], charset) \
650 != CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION))
d46c5b12
KH
651
652#define SHIFT_OUT_OK(idx) \
653 (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding_system_table[idx], 1) >= 0)
654
4ed46869
KH
655/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
656 Check if a text is encoded in ISO2022. If it is, returns an
657 integer in which appropriate flag bits any of:
658 CODING_CATEGORY_MASK_ISO_7
d46c5b12 659 CODING_CATEGORY_MASK_ISO_7_TIGHT
4ed46869
KH
660 CODING_CATEGORY_MASK_ISO_8_1
661 CODING_CATEGORY_MASK_ISO_8_2
7717c392
KH
662 CODING_CATEGORY_MASK_ISO_7_ELSE
663 CODING_CATEGORY_MASK_ISO_8_ELSE
4ed46869
KH
664 are set. If a code which should never appear in ISO2022 is found,
665 returns 0. */
666
667int
668detect_coding_iso2022 (src, src_end)
669 unsigned char *src, *src_end;
670{
d46c5b12
KH
671 int mask = CODING_CATEGORY_MASK_ISO;
672 int mask_found = 0;
673 int reg[4], shift_out = 0;
674 int c, c1, i, charset;
3f003981 675
d46c5b12 676 reg[0] = CHARSET_ASCII, reg[1] = reg[2] = reg[3] = -1;
3f003981 677 while (mask && src < src_end)
4ed46869
KH
678 {
679 c = *src++;
680 switch (c)
681 {
682 case ISO_CODE_ESC:
e0e989f6 683 if (src >= src_end)
4ed46869
KH
684 break;
685 c = *src++;
d46c5b12 686 if (c >= '(' && c <= '/')
4ed46869 687 {
bf9cdd4e
KH
688 /* Designation sequence for a charset of dimension 1. */
689 if (src >= src_end)
690 break;
d46c5b12
KH
691 c1 = *src++;
692 if (c1 < ' ' || c1 >= 0x80
693 || (charset = iso_charset_table[0][c >= ','][c1]) < 0)
694 /* Invalid designation sequence. Just ignore. */
695 break;
696 reg[(c - '(') % 4] = charset;
bf9cdd4e
KH
697 }
698 else if (c == '$')
699 {
700 /* Designation sequence for a charset of dimension 2. */
701 if (src >= src_end)
702 break;
703 c = *src++;
704 if (c >= '@' && c <= 'B')
705 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
d46c5b12 706 reg[0] = charset = iso_charset_table[1][0][c];
bf9cdd4e 707 else if (c >= '(' && c <= '/')
bcf26d6a 708 {
bf9cdd4e
KH
709 if (src >= src_end)
710 break;
d46c5b12
KH
711 c1 = *src++;
712 if (c1 < ' ' || c1 >= 0x80
713 || (charset = iso_charset_table[1][c >= ','][c1]) < 0)
714 /* Invalid designation sequence. Just ignore. */
715 break;
716 reg[(c - '(') % 4] = charset;
bcf26d6a 717 }
bf9cdd4e 718 else
d46c5b12
KH
719 /* Invalid designation sequence. Just ignore. */
720 break;
721 }
722 else if (c == 'N' || c == 'n')
723 {
724 if (shift_out == 0
725 && (reg[1] >= 0
726 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_7_ELSE)
727 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_8_ELSE)))
728 {
729 /* Locking shift out. */
730 mask &= ~CODING_CATEGORY_MASK_ISO_7BIT;
731 mask_found |= CODING_CATEGORY_MASK_ISO_SHIFT;
732 shift_out = 1;
733 }
734 break;
735 }
736 else if (c == 'O' || c == 'o')
737 {
738 if (shift_out == 1)
739 {
740 /* Locking shift in. */
741 mask &= ~CODING_CATEGORY_MASK_ISO_7BIT;
742 mask_found |= CODING_CATEGORY_MASK_ISO_SHIFT;
743 shift_out = 0;
744 }
745 break;
4ed46869 746 }
bf9cdd4e 747 else if (c == '0' || c == '1' || c == '2')
d46c5b12
KH
748 /* Start/end composition. Just ignore. */
749 break;
bf9cdd4e 750 else
d46c5b12
KH
751 /* Invalid escape sequence. Just ignore. */
752 break;
753
754 /* We found a valid designation sequence for CHARSET. */
755 mask &= ~CODING_CATEGORY_MASK_ISO_8BIT;
756 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7, charset))
757 mask_found |= CODING_CATEGORY_MASK_ISO_7;
758 else
759 mask &= ~CODING_CATEGORY_MASK_ISO_7;
760 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7_TIGHT, charset))
761 mask_found |= CODING_CATEGORY_MASK_ISO_7_TIGHT;
762 else
763 mask &= ~CODING_CATEGORY_MASK_ISO_7_TIGHT;
764 if (! CHARSET_OK (CODING_CATEGORY_IDX_ISO_7_ELSE, charset))
765 mask &= ~CODING_CATEGORY_MASK_ISO_7_ELSE;
766 if (! CHARSET_OK (CODING_CATEGORY_IDX_ISO_8_ELSE, charset))
767 mask &= ~CODING_CATEGORY_MASK_ISO_8_ELSE;
4ed46869
KH
768 break;
769
4ed46869 770 case ISO_CODE_SO:
d46c5b12
KH
771 if (shift_out == 0
772 && (reg[1] >= 0
773 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_7_ELSE)
774 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_8_ELSE)))
775 {
776 /* Locking shift out. */
777 mask &= ~CODING_CATEGORY_MASK_ISO_7BIT;
778 mask_found |= CODING_CATEGORY_MASK_ISO_SHIFT;
779 }
e0e989f6
KH
780 break;
781
d46c5b12
KH
782 case ISO_CODE_SI:
783 if (shift_out == 1)
784 {
785 /* Locking shift in. */
786 mask &= ~CODING_CATEGORY_MASK_ISO_7BIT;
787 mask_found |= CODING_CATEGORY_MASK_ISO_SHIFT;
788 }
789 break;
790
4ed46869
KH
791 case ISO_CODE_CSI:
792 case ISO_CODE_SS2:
793 case ISO_CODE_SS3:
3f003981
KH
794 {
795 int newmask = CODING_CATEGORY_MASK_ISO_8_ELSE;
796
70c22245
KH
797 if (c != ISO_CODE_CSI)
798 {
d46c5b12
KH
799 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_1]->flags
800 & CODING_FLAG_ISO_SINGLE_SHIFT)
70c22245 801 newmask |= CODING_CATEGORY_MASK_ISO_8_1;
d46c5b12
KH
802 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_2]->flags
803 & CODING_FLAG_ISO_SINGLE_SHIFT)
70c22245
KH
804 newmask |= CODING_CATEGORY_MASK_ISO_8_2;
805 }
3f003981
KH
806 if (VECTORP (Vlatin_extra_code_table)
807 && !NILP (XVECTOR (Vlatin_extra_code_table)->contents[c]))
808 {
d46c5b12
KH
809 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_1]->flags
810 & CODING_FLAG_ISO_LATIN_EXTRA)
3f003981 811 newmask |= CODING_CATEGORY_MASK_ISO_8_1;
d46c5b12
KH
812 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_2]->flags
813 & CODING_FLAG_ISO_LATIN_EXTRA)
3f003981
KH
814 newmask |= CODING_CATEGORY_MASK_ISO_8_2;
815 }
816 mask &= newmask;
d46c5b12 817 mask_found |= newmask;
3f003981
KH
818 }
819 break;
4ed46869
KH
820
821 default:
822 if (c < 0x80)
823 break;
824 else if (c < 0xA0)
c4825358 825 {
3f003981
KH
826 if (VECTORP (Vlatin_extra_code_table)
827 && !NILP (XVECTOR (Vlatin_extra_code_table)->contents[c]))
c4825358 828 {
3f003981
KH
829 int newmask = 0;
830
d46c5b12
KH
831 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_1]->flags
832 & CODING_FLAG_ISO_LATIN_EXTRA)
3f003981 833 newmask |= CODING_CATEGORY_MASK_ISO_8_1;
d46c5b12
KH
834 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_2]->flags
835 & CODING_FLAG_ISO_LATIN_EXTRA)
3f003981
KH
836 newmask |= CODING_CATEGORY_MASK_ISO_8_2;
837 mask &= newmask;
d46c5b12 838 mask_found |= newmask;
c4825358 839 }
3f003981
KH
840 else
841 return 0;
c4825358 842 }
4ed46869
KH
843 else
844 {
7717c392 845 unsigned char *src_begin = src;
4ed46869 846
d46c5b12 847 mask &= ~(CODING_CATEGORY_MASK_ISO_7BIT
7717c392 848 | CODING_CATEGORY_MASK_ISO_7_ELSE);
d46c5b12 849 mask_found |= CODING_CATEGORY_MASK_ISO_8_1;
e0e989f6 850 while (src < src_end && *src >= 0xA0)
7717c392
KH
851 src++;
852 if ((src - src_begin - 1) & 1 && src < src_end)
4ed46869 853 mask &= ~CODING_CATEGORY_MASK_ISO_8_2;
d46c5b12
KH
854 else
855 mask_found |= CODING_CATEGORY_MASK_ISO_8_2;
4ed46869
KH
856 }
857 break;
858 }
859 }
860
d46c5b12 861 return (mask & mask_found);
4ed46869
KH
862}
863
864/* Decode a character of which charset is CHARSET and the 1st position
bdd9fb48 865 code is C1. If dimension of CHARSET is 2, the 2nd position code is
4ed46869
KH
866 fetched from SRC and set to C2. If CHARSET is negative, it means
867 that we are decoding ill formed text, and what we can do is just to
868 read C1 as is. */
869
bdd9fb48
KH
870#define DECODE_ISO_CHARACTER(charset, c1) \
871 do { \
872 int c_alt, charset_alt = (charset); \
873 if (COMPOSING_HEAD_P (coding->composing)) \
874 { \
875 *dst++ = LEADING_CODE_COMPOSITION; \
876 if (COMPOSING_WITH_RULE_P (coding->composing)) \
877 /* To tell composition rules are embeded. */ \
878 *dst++ = 0xFF; \
879 coding->composing += 2; \
880 } \
881 if ((charset) >= 0) \
882 { \
883 if (CHARSET_DIMENSION (charset) == 2) \
70c22245
KH
884 { \
885 ONE_MORE_BYTE (c2); \
886 if (iso_code_class[(c2) & 0x7F] != ISO_0x20_or_0x7F \
887 && iso_code_class[(c2) & 0x7F] != ISO_graphic_plane_0) \
888 { \
889 src--; \
890 c2 = ' '; \
891 } \
892 } \
bdd9fb48
KH
893 if (!NILP (unification_table) \
894 && ((c_alt = unify_char (unification_table, \
895 -1, (charset), c1, c2)) >= 0)) \
896 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
897 } \
898 if (charset_alt == CHARSET_ASCII || charset_alt < 0) \
899 DECODE_CHARACTER_ASCII (c1); \
900 else if (CHARSET_DIMENSION (charset_alt) == 1) \
901 DECODE_CHARACTER_DIMENSION1 (charset_alt, c1); \
902 else \
903 DECODE_CHARACTER_DIMENSION2 (charset_alt, c1, c2); \
904 if (COMPOSING_WITH_RULE_P (coding->composing)) \
905 /* To tell a composition rule follows. */ \
906 coding->composing = COMPOSING_WITH_RULE_RULE; \
4ed46869
KH
907 } while (0)
908
909/* Set designation state into CODING. */
d46c5b12
KH
910#define DECODE_DESIGNATION(reg, dimension, chars, final_char) \
911 do { \
912 int charset = ISO_CHARSET_TABLE (make_number (dimension), \
913 make_number (chars), \
914 make_number (final_char)); \
915 if (charset >= 0 \
704c5781
KH
916 && (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) == reg \
917 || coding->safe_charsets[charset])) \
d46c5b12
KH
918 { \
919 if (coding->spec.iso2022.last_invalid_designation_register == 0 \
920 && reg == 0 \
921 && charset == CHARSET_ASCII) \
922 { \
923 /* We should insert this designation sequence as is so \
924 that it is surely written back to a file. */ \
925 coding->spec.iso2022.last_invalid_designation_register = -1; \
926 goto label_invalid_code; \
927 } \
928 coding->spec.iso2022.last_invalid_designation_register = -1; \
929 if ((coding->mode & CODING_MODE_DIRECTION) \
930 && CHARSET_REVERSE_CHARSET (charset) >= 0) \
931 charset = CHARSET_REVERSE_CHARSET (charset); \
932 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
933 } \
934 else \
935 { \
936 coding->spec.iso2022.last_invalid_designation_register = reg; \
937 goto label_invalid_code; \
938 } \
4ed46869
KH
939 } while (0)
940
d46c5b12
KH
941/* Check if the current composing sequence contains only valid codes.
942 If the composing sequence doesn't end before SRC_END, return -1.
943 Else, if it contains only valid codes, return 0.
944 Else return the length of the composing sequence. */
945
946int check_composing_code (coding, src, src_end)
947 struct coding_system *coding;
948 unsigned char *src, *src_end;
949{
950 unsigned char *src_start = src;
951 int invalid_code_found = 0;
952 int charset, c, c1, dim;
953
954 while (src < src_end)
955 {
956 if (*src++ != ISO_CODE_ESC) continue;
957 if (src >= src_end) break;
958 if ((c = *src++) == '1') /* end of compsition */
959 return (invalid_code_found ? src - src_start : 0);
960 if (src + 2 >= src_end) break;
961 if (!coding->flags & CODING_FLAG_ISO_DESIGNATION)
962 invalid_code_found = 1;
963 else
964 {
965 dim = 0;
966 if (c == '$')
967 {
968 dim = 1;
969 c = (*src >= '@' && *src <= 'B') ? '(' : *src++;
970 }
971 if (c >= '(' && c <= '/')
972 {
973 c1 = *src++;
974 if ((c1 < ' ' || c1 >= 0x80)
975 || (charset = iso_charset_table[dim][c >= ','][c1]) < 0
704c5781 976 || ! coding->safe_charsets[charset]
d46c5b12
KH
977 || (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
978 == CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION))
979 invalid_code_found = 1;
980 }
981 else
982 invalid_code_found = 1;
983 }
984 }
985 return ((coding->mode & CODING_MODE_LAST_BLOCK) ? src_end - src_start : -1);
986}
987
4ed46869
KH
988/* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
989
990int
d46c5b12 991decode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
4ed46869
KH
992 struct coding_system *coding;
993 unsigned char *source, *destination;
994 int src_bytes, dst_bytes;
4ed46869
KH
995{
996 unsigned char *src = source;
997 unsigned char *src_end = source + src_bytes;
998 unsigned char *dst = destination;
999 unsigned char *dst_end = destination + dst_bytes;
1000 /* Since the maximum bytes produced by each loop is 7, we subtract 6
1001 from DST_END to assure that overflow checking is necessary only
1002 at the head of loop. */
1003 unsigned char *adjusted_dst_end = dst_end - 6;
1004 int charset;
1005 /* Charsets invoked to graphic plane 0 and 1 respectively. */
1006 int charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
1007 int charset1 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 1);
a5d301df 1008 Lisp_Object unification_table
d46c5b12
KH
1009 = coding->character_unification_table_for_decode;
1010 int result = CODING_FINISH_NORMAL;
bdd9fb48
KH
1011
1012 if (!NILP (Venable_character_unification) && NILP (unification_table))
a5d301df 1013 unification_table = Vstandard_character_unification_table_for_decode;
4ed46869 1014
d46c5b12 1015 coding->produced_char = 0;
fb88bf2d 1016 coding->fake_multibyte = 0;
d46c5b12
KH
1017 while (src < src_end && (dst_bytes
1018 ? (dst < adjusted_dst_end)
1019 : (dst < src - 6)))
4ed46869
KH
1020 {
1021 /* SRC_BASE remembers the start position in source in each loop.
1022 The loop will be exited when there's not enough source text
1023 to analyze long escape sequence or 2-byte code (within macros
1024 ONE_MORE_BYTE or TWO_MORE_BYTES). In that case, SRC is reset
1025 to SRC_BASE before exiting. */
1026 unsigned char *src_base = src;
bdd9fb48 1027 int c1 = *src++, c2;
4ed46869
KH
1028
1029 switch (iso_code_class [c1])
1030 {
1031 case ISO_0x20_or_0x7F:
1032 if (!coding->composing
1033 && (charset0 < 0 || CHARSET_CHARS (charset0) == 94))
1034 {
1035 /* This is SPACE or DEL. */
1036 *dst++ = c1;
d46c5b12 1037 coding->produced_char++;
4ed46869
KH
1038 break;
1039 }
1040 /* This is a graphic character, we fall down ... */
1041
1042 case ISO_graphic_plane_0:
1043 if (coding->composing == COMPOSING_WITH_RULE_RULE)
1044 {
1045 /* This is a composition rule. */
1046 *dst++ = c1 | 0x80;
1047 coding->composing = COMPOSING_WITH_RULE_TAIL;
1048 }
1049 else
1050 DECODE_ISO_CHARACTER (charset0, c1);
1051 break;
1052
1053 case ISO_0xA0_or_0xFF:
d46c5b12
KH
1054 if (charset1 < 0 || CHARSET_CHARS (charset1) == 94
1055 || coding->flags & CODING_FLAG_ISO_SEVEN_BITS)
fb88bf2d 1056 goto label_invalid_code;
4ed46869
KH
1057 /* This is a graphic character, we fall down ... */
1058
1059 case ISO_graphic_plane_1:
d46c5b12 1060 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS)
fb88bf2d 1061 goto label_invalid_code;
d46c5b12
KH
1062 else
1063 DECODE_ISO_CHARACTER (charset1, c1);
4ed46869
KH
1064 break;
1065
1066 case ISO_control_code:
1067 /* All ISO2022 control characters in this class have the
1068 same representation in Emacs internal format. */
d46c5b12
KH
1069 if (c1 == '\n'
1070 && (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
1071 && (coding->eol_type == CODING_EOL_CR
1072 || coding->eol_type == CODING_EOL_CRLF))
1073 {
1074 result = CODING_FINISH_INCONSISTENT_EOL;
1075 goto label_end_of_loop_2;
1076 }
4ed46869 1077 *dst++ = c1;
d46c5b12 1078 coding->produced_char++;
4ed46869
KH
1079 break;
1080
1081 case ISO_carriage_return:
1082 if (coding->eol_type == CODING_EOL_CR)
d46c5b12 1083 *dst++ = '\n';
4ed46869
KH
1084 else if (coding->eol_type == CODING_EOL_CRLF)
1085 {
1086 ONE_MORE_BYTE (c1);
1087 if (c1 == ISO_CODE_LF)
1088 *dst++ = '\n';
1089 else
1090 {
d46c5b12
KH
1091 if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
1092 {
1093 result = CODING_FINISH_INCONSISTENT_EOL;
1094 goto label_end_of_loop_2;
1095 }
4ed46869 1096 src--;
d46c5b12 1097 *dst++ = '\r';
4ed46869
KH
1098 }
1099 }
1100 else
d46c5b12
KH
1101 *dst++ = c1;
1102 coding->produced_char++;
4ed46869
KH
1103 break;
1104
1105 case ISO_shift_out:
d46c5b12
KH
1106 if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT)
1107 || CODING_SPEC_ISO_DESIGNATION (coding, 1) < 0)
1108 goto label_invalid_code;
4ed46869
KH
1109 CODING_SPEC_ISO_INVOCATION (coding, 0) = 1;
1110 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
1111 break;
1112
1113 case ISO_shift_in:
d46c5b12
KH
1114 if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT))
1115 goto label_invalid_code;
4ed46869
KH
1116 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0;
1117 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
1118 break;
1119
1120 case ISO_single_shift_2_7:
1121 case ISO_single_shift_2:
d46c5b12
KH
1122 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT))
1123 goto label_invalid_code;
4ed46869
KH
1124 /* SS2 is handled as an escape sequence of ESC 'N' */
1125 c1 = 'N';
1126 goto label_escape_sequence;
1127
1128 case ISO_single_shift_3:
d46c5b12
KH
1129 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT))
1130 goto label_invalid_code;
4ed46869
KH
1131 /* SS2 is handled as an escape sequence of ESC 'O' */
1132 c1 = 'O';
1133 goto label_escape_sequence;
1134
1135 case ISO_control_sequence_introducer:
1136 /* CSI is handled as an escape sequence of ESC '[' ... */
1137 c1 = '[';
1138 goto label_escape_sequence;
1139
1140 case ISO_escape:
1141 ONE_MORE_BYTE (c1);
1142 label_escape_sequence:
1143 /* Escape sequences handled by Emacs are invocation,
1144 designation, direction specification, and character
1145 composition specification. */
1146 switch (c1)
1147 {
1148 case '&': /* revision of following character set */
1149 ONE_MORE_BYTE (c1);
1150 if (!(c1 >= '@' && c1 <= '~'))
d46c5b12 1151 goto label_invalid_code;
4ed46869
KH
1152 ONE_MORE_BYTE (c1);
1153 if (c1 != ISO_CODE_ESC)
d46c5b12 1154 goto label_invalid_code;
4ed46869
KH
1155 ONE_MORE_BYTE (c1);
1156 goto label_escape_sequence;
1157
1158 case '$': /* designation of 2-byte character set */
d46c5b12
KH
1159 if (! (coding->flags & CODING_FLAG_ISO_DESIGNATION))
1160 goto label_invalid_code;
4ed46869
KH
1161 ONE_MORE_BYTE (c1);
1162 if (c1 >= '@' && c1 <= 'B')
1163 { /* designation of JISX0208.1978, GB2312.1980,
1164 or JISX0208.1980 */
1165 DECODE_DESIGNATION (0, 2, 94, c1);
1166 }
1167 else if (c1 >= 0x28 && c1 <= 0x2B)
1168 { /* designation of DIMENSION2_CHARS94 character set */
1169 ONE_MORE_BYTE (c2);
1170 DECODE_DESIGNATION (c1 - 0x28, 2, 94, c2);
1171 }
1172 else if (c1 >= 0x2C && c1 <= 0x2F)
1173 { /* designation of DIMENSION2_CHARS96 character set */
1174 ONE_MORE_BYTE (c2);
1175 DECODE_DESIGNATION (c1 - 0x2C, 2, 96, c2);
1176 }
1177 else
d46c5b12 1178 goto label_invalid_code;
4ed46869
KH
1179 break;
1180
1181 case 'n': /* invocation of locking-shift-2 */
d46c5b12
KH
1182 if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT)
1183 || CODING_SPEC_ISO_DESIGNATION (coding, 2) < 0)
1184 goto label_invalid_code;
4ed46869 1185 CODING_SPEC_ISO_INVOCATION (coding, 0) = 2;
e0e989f6 1186 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
4ed46869
KH
1187 break;
1188
1189 case 'o': /* invocation of locking-shift-3 */
d46c5b12
KH
1190 if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT)
1191 || CODING_SPEC_ISO_DESIGNATION (coding, 3) < 0)
1192 goto label_invalid_code;
4ed46869 1193 CODING_SPEC_ISO_INVOCATION (coding, 0) = 3;
e0e989f6 1194 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
4ed46869
KH
1195 break;
1196
1197 case 'N': /* invocation of single-shift-2 */
d46c5b12
KH
1198 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT)
1199 || CODING_SPEC_ISO_DESIGNATION (coding, 2) < 0)
1200 goto label_invalid_code;
4ed46869
KH
1201 ONE_MORE_BYTE (c1);
1202 charset = CODING_SPEC_ISO_DESIGNATION (coding, 2);
1203 DECODE_ISO_CHARACTER (charset, c1);
1204 break;
1205
1206 case 'O': /* invocation of single-shift-3 */
d46c5b12
KH
1207 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT)
1208 || CODING_SPEC_ISO_DESIGNATION (coding, 3) < 0)
1209 goto label_invalid_code;
4ed46869
KH
1210 ONE_MORE_BYTE (c1);
1211 charset = CODING_SPEC_ISO_DESIGNATION (coding, 3);
1212 DECODE_ISO_CHARACTER (charset, c1);
1213 break;
1214
d46c5b12
KH
1215 case '0': case '2': /* start composing */
1216 /* Before processing composing, we must be sure that all
1217 characters being composed are supported by CODING.
1218 If not, we must give up composing and insert the
1219 bunch of codes for composing as is without decoding. */
1220 {
1221 int result1;
1222
1223 result1 = check_composing_code (coding, src, src_end);
1224 if (result1 == 0)
1225 coding->composing = (c1 == '0'
1226 ? COMPOSING_NO_RULE_HEAD
1227 : COMPOSING_WITH_RULE_HEAD);
1228 else if (result1 > 0)
1229 {
1230 if (result1 + 2 < (dst_bytes ? dst_end : src_base) - dst)
1231 {
1232 bcopy (src_base, dst, result1 + 2);
1233 src += result1;
1234 dst += result1 + 2;
1235 coding->produced_char += result1 + 2;
1236 }
1237 else
1238 {
1239 result = CODING_FINISH_INSUFFICIENT_DST;
1240 goto label_end_of_loop_2;
1241 }
1242 }
1243 else
1244 goto label_end_of_loop;
1245 }
4ed46869
KH
1246 break;
1247
1248 case '1': /* end composing */
1249 coding->composing = COMPOSING_NO;
d46c5b12 1250 coding->produced_char++;
4ed46869
KH
1251 break;
1252
1253 case '[': /* specification of direction */
d46c5b12
KH
1254 if (coding->flags & CODING_FLAG_ISO_NO_DIRECTION)
1255 goto label_invalid_code;
4ed46869 1256 /* For the moment, nested direction is not supported.
d46c5b12
KH
1257 So, `coding->mode & CODING_MODE_DIRECTION' zero means
1258 left-to-right, and nozero means right-to-left. */
4ed46869
KH
1259 ONE_MORE_BYTE (c1);
1260 switch (c1)
1261 {
1262 case ']': /* end of the current direction */
d46c5b12 1263 coding->mode &= ~CODING_MODE_DIRECTION;
4ed46869
KH
1264
1265 case '0': /* end of the current direction */
1266 case '1': /* start of left-to-right direction */
1267 ONE_MORE_BYTE (c1);
1268 if (c1 == ']')
d46c5b12 1269 coding->mode &= ~CODING_MODE_DIRECTION;
4ed46869 1270 else
d46c5b12 1271 goto label_invalid_code;
4ed46869
KH
1272 break;
1273
1274 case '2': /* start of right-to-left direction */
1275 ONE_MORE_BYTE (c1);
1276 if (c1 == ']')
d46c5b12 1277 coding->mode |= CODING_MODE_DIRECTION;
4ed46869 1278 else
d46c5b12 1279 goto label_invalid_code;
4ed46869
KH
1280 break;
1281
1282 default:
d46c5b12 1283 goto label_invalid_code;
4ed46869
KH
1284 }
1285 break;
1286
1287 default:
d46c5b12
KH
1288 if (! (coding->flags & CODING_FLAG_ISO_DESIGNATION))
1289 goto label_invalid_code;
4ed46869
KH
1290 if (c1 >= 0x28 && c1 <= 0x2B)
1291 { /* designation of DIMENSION1_CHARS94 character set */
1292 ONE_MORE_BYTE (c2);
1293 DECODE_DESIGNATION (c1 - 0x28, 1, 94, c2);
1294 }
1295 else if (c1 >= 0x2C && c1 <= 0x2F)
1296 { /* designation of DIMENSION1_CHARS96 character set */
1297 ONE_MORE_BYTE (c2);
1298 DECODE_DESIGNATION (c1 - 0x2C, 1, 96, c2);
1299 }
1300 else
1301 {
d46c5b12 1302 goto label_invalid_code;
4ed46869
KH
1303 }
1304 }
1305 /* We must update these variables now. */
1306 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
1307 charset1 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 1);
1308 break;
1309
d46c5b12 1310 label_invalid_code:
d46c5b12
KH
1311 while (src_base < src)
1312 *dst++ = *src_base++;
fb88bf2d 1313 coding->fake_multibyte = 1;
4ed46869
KH
1314 }
1315 continue;
1316
1317 label_end_of_loop:
d46c5b12
KH
1318 result = CODING_FINISH_INSUFFICIENT_SRC;
1319 label_end_of_loop_2:
4ed46869
KH
1320 src = src_base;
1321 break;
1322 }
1323
fb88bf2d 1324 if (src < src_end)
4ed46869 1325 {
fb88bf2d
KH
1326 if (result == CODING_FINISH_NORMAL)
1327 result = CODING_FINISH_INSUFFICIENT_DST;
1328 else if (result != CODING_FINISH_INCONSISTENT_EOL
1329 && coding->mode & CODING_MODE_LAST_BLOCK)
1330 {
1331 /* This is the last block of the text to be decoded. We had
1332 better just flush out all remaining codes in the text
1333 although they are not valid characters. */
1334 src_bytes = src_end - src;
1335 if (dst_bytes && (dst_end - dst < src_bytes))
1336 src_bytes = dst_end - dst;
1337 bcopy (src, dst, src_bytes);
1338 dst += src_bytes;
1339 src += src_bytes;
1340 coding->fake_multibyte = 1;
1341 }
4ed46869 1342 }
fb88bf2d 1343
d46c5b12
KH
1344 coding->consumed = coding->consumed_char = src - source;
1345 coding->produced = dst - destination;
1346 return result;
4ed46869
KH
1347}
1348
f4dee582 1349/* ISO2022 encoding stuff. */
4ed46869
KH
1350
1351/*
f4dee582 1352 It is not enough to say just "ISO2022" on encoding, we have to
d46c5b12 1353 specify more details. In Emacs, each coding system of ISO2022
4ed46869
KH
1354 variant has the following specifications:
1355 1. Initial designation to G0 thru G3.
1356 2. Allows short-form designation?
1357 3. ASCII should be designated to G0 before control characters?
1358 4. ASCII should be designated to G0 at end of line?
1359 5. 7-bit environment or 8-bit environment?
1360 6. Use locking-shift?
1361 7. Use Single-shift?
1362 And the following two are only for Japanese:
1363 8. Use ASCII in place of JIS0201-1976-Roman?
1364 9. Use JISX0208-1983 in place of JISX0208-1978?
1365 These specifications are encoded in `coding->flags' as flag bits
1366 defined by macros CODING_FLAG_ISO_XXX. See `coding.h' for more
f4dee582 1367 details.
4ed46869
KH
1368*/
1369
1370/* Produce codes (escape sequence) for designating CHARSET to graphic
1371 register REG. If <final-char> of CHARSET is '@', 'A', or 'B' and
1372 the coding system CODING allows, produce designation sequence of
1373 short-form. */
1374
1375#define ENCODE_DESIGNATION(charset, reg, coding) \
1376 do { \
1377 unsigned char final_char = CHARSET_ISO_FINAL_CHAR (charset); \
1378 char *intermediate_char_94 = "()*+"; \
1379 char *intermediate_char_96 = ",-./"; \
70c22245
KH
1380 int revision = CODING_SPEC_ISO_REVISION_NUMBER(coding, charset); \
1381 if (revision < 255) \
1382 { \
4ed46869
KH
1383 *dst++ = ISO_CODE_ESC; \
1384 *dst++ = '&'; \
70c22245 1385 *dst++ = '@' + revision; \
4ed46869
KH
1386 } \
1387 *dst++ = ISO_CODE_ESC; \
1388 if (CHARSET_DIMENSION (charset) == 1) \
1389 { \
1390 if (CHARSET_CHARS (charset) == 94) \
1391 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
1392 else \
1393 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
1394 } \
1395 else \
1396 { \
1397 *dst++ = '$'; \
1398 if (CHARSET_CHARS (charset) == 94) \
1399 { \
1400 if (! (coding->flags & CODING_FLAG_ISO_SHORT_FORM) \
1401 || reg != 0 \
1402 || final_char < '@' || final_char > 'B') \
1403 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
1404 } \
1405 else \
1406 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
1407 } \
1408 *dst++ = final_char; \
1409 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
1410 } while (0)
1411
1412/* The following two macros produce codes (control character or escape
1413 sequence) for ISO2022 single-shift functions (single-shift-2 and
1414 single-shift-3). */
1415
1416#define ENCODE_SINGLE_SHIFT_2 \
1417 do { \
1418 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1419 *dst++ = ISO_CODE_ESC, *dst++ = 'N'; \
1420 else \
fb88bf2d
KH
1421 { \
1422 *dst++ = ISO_CODE_SS2; \
1423 coding->fake_multibyte = 1; \
1424 } \
4ed46869
KH
1425 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
1426 } while (0)
1427
fb88bf2d
KH
1428#define ENCODE_SINGLE_SHIFT_3 \
1429 do { \
4ed46869 1430 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
fb88bf2d
KH
1431 *dst++ = ISO_CODE_ESC, *dst++ = 'O'; \
1432 else \
1433 { \
1434 *dst++ = ISO_CODE_SS3; \
1435 coding->fake_multibyte = 1; \
1436 } \
4ed46869
KH
1437 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
1438 } while (0)
1439
1440/* The following four macros produce codes (control character or
1441 escape sequence) for ISO2022 locking-shift functions (shift-in,
1442 shift-out, locking-shift-2, and locking-shift-3). */
1443
1444#define ENCODE_SHIFT_IN \
1445 do { \
1446 *dst++ = ISO_CODE_SI; \
1447 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0; \
1448 } while (0)
1449
1450#define ENCODE_SHIFT_OUT \
1451 do { \
1452 *dst++ = ISO_CODE_SO; \
1453 CODING_SPEC_ISO_INVOCATION (coding, 0) = 1; \
1454 } while (0)
1455
1456#define ENCODE_LOCKING_SHIFT_2 \
1457 do { \
1458 *dst++ = ISO_CODE_ESC, *dst++ = 'n'; \
1459 CODING_SPEC_ISO_INVOCATION (coding, 0) = 2; \
1460 } while (0)
1461
1462#define ENCODE_LOCKING_SHIFT_3 \
1463 do { \
1464 *dst++ = ISO_CODE_ESC, *dst++ = 'o'; \
1465 CODING_SPEC_ISO_INVOCATION (coding, 0) = 3; \
1466 } while (0)
1467
f4dee582
RS
1468/* Produce codes for a DIMENSION1 character whose character set is
1469 CHARSET and whose position-code is C1. Designation and invocation
4ed46869
KH
1470 sequences are also produced in advance if necessary. */
1471
1472
6e85d753
KH
1473#define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
1474 do { \
1475 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
1476 { \
1477 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1478 *dst++ = c1 & 0x7F; \
1479 else \
1480 *dst++ = c1 | 0x80; \
1481 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
1482 break; \
1483 } \
1484 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
1485 { \
1486 *dst++ = c1 & 0x7F; \
1487 break; \
1488 } \
1489 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
1490 { \
1491 *dst++ = c1 | 0x80; \
1492 break; \
1493 } \
1494 else if (coding->flags & CODING_FLAG_ISO_SAFE \
70c22245 1495 && !coding->safe_charsets[charset]) \
6e85d753
KH
1496 { \
1497 /* We should not encode this character, instead produce one or \
1498 two `?'s. */ \
1499 *dst++ = CODING_INHIBIT_CHARACTER_SUBSTITUTION; \
1500 if (CHARSET_WIDTH (charset) == 2) \
1501 *dst++ = CODING_INHIBIT_CHARACTER_SUBSTITUTION; \
1502 break; \
1503 } \
1504 else \
1505 /* Since CHARSET is not yet invoked to any graphic planes, we \
1506 must invoke it, or, at first, designate it to some graphic \
1507 register. Then repeat the loop to actually produce the \
1508 character. */ \
1509 dst = encode_invocation_designation (charset, coding, dst); \
4ed46869
KH
1510 } while (1)
1511
f4dee582
RS
1512/* Produce codes for a DIMENSION2 character whose character set is
1513 CHARSET and whose position-codes are C1 and C2. Designation and
4ed46869
KH
1514 invocation codes are also produced in advance if necessary. */
1515
6e85d753
KH
1516#define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
1517 do { \
1518 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
1519 { \
1520 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
1521 *dst++ = c1 & 0x7F, *dst++ = c2 & 0x7F; \
1522 else \
1523 *dst++ = c1 | 0x80, *dst++ = c2 | 0x80; \
1524 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
1525 break; \
1526 } \
1527 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
1528 { \
1529 *dst++ = c1 & 0x7F, *dst++= c2 & 0x7F; \
1530 break; \
1531 } \
1532 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
1533 { \
1534 *dst++ = c1 | 0x80, *dst++= c2 | 0x80; \
1535 break; \
1536 } \
1537 else if (coding->flags & CODING_FLAG_ISO_SAFE \
70c22245 1538 && !coding->safe_charsets[charset]) \
6e85d753
KH
1539 { \
1540 /* We should not encode this character, instead produce one or \
1541 two `?'s. */ \
1542 *dst++ = CODING_INHIBIT_CHARACTER_SUBSTITUTION; \
1543 if (CHARSET_WIDTH (charset) == 2) \
1544 *dst++ = CODING_INHIBIT_CHARACTER_SUBSTITUTION; \
1545 break; \
1546 } \
1547 else \
1548 /* Since CHARSET is not yet invoked to any graphic planes, we \
1549 must invoke it, or, at first, designate it to some graphic \
1550 register. Then repeat the loop to actually produce the \
1551 character. */ \
1552 dst = encode_invocation_designation (charset, coding, dst); \
4ed46869
KH
1553 } while (1)
1554
bdd9fb48
KH
1555#define ENCODE_ISO_CHARACTER(charset, c1, c2) \
1556 do { \
1557 int c_alt, charset_alt; \
1558 if (!NILP (unification_table) \
1559 && ((c_alt = unify_char (unification_table, -1, charset, c1, c2)) \
a5d301df 1560 >= 0)) \
bdd9fb48
KH
1561 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
1562 else \
1563 charset_alt = charset; \
1564 if (CHARSET_DIMENSION (charset_alt) == 1) \
4031e2bf
KH
1565 { \
1566 if (charset == CHARSET_ASCII \
1567 && coding->flags & CODING_FLAG_ISO_USE_ROMAN) \
1568 charset_alt = charset_latin_jisx0201; \
1569 ENCODE_ISO_CHARACTER_DIMENSION1 (charset_alt, c1); \
1570 } \
bdd9fb48 1571 else \
4031e2bf
KH
1572 { \
1573 if (charset == charset_jisx0208 \
1574 && coding->flags & CODING_FLAG_ISO_USE_OLDJIS) \
1575 charset_alt = charset_jisx0208_1978; \
1576 ENCODE_ISO_CHARACTER_DIMENSION2 (charset_alt, c1, c2); \
1577 } \
d46c5b12
KH
1578 if (! COMPOSING_P (coding->composing)) \
1579 coding->consumed_char++; \
4031e2bf 1580 } while (0)
bdd9fb48 1581
4ed46869
KH
1582/* Produce designation and invocation codes at a place pointed by DST
1583 to use CHARSET. The element `spec.iso2022' of *CODING is updated.
1584 Return new DST. */
1585
1586unsigned char *
1587encode_invocation_designation (charset, coding, dst)
1588 int charset;
1589 struct coding_system *coding;
1590 unsigned char *dst;
1591{
1592 int reg; /* graphic register number */
1593
1594 /* At first, check designations. */
1595 for (reg = 0; reg < 4; reg++)
1596 if (charset == CODING_SPEC_ISO_DESIGNATION (coding, reg))
1597 break;
1598
1599 if (reg >= 4)
1600 {
1601 /* CHARSET is not yet designated to any graphic registers. */
1602 /* At first check the requested designation. */
1603 reg = CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset);
1ba9e4ab
KH
1604 if (reg == CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION)
1605 /* Since CHARSET requests no special designation, designate it
1606 to graphic register 0. */
4ed46869
KH
1607 reg = 0;
1608
1609 ENCODE_DESIGNATION (charset, reg, coding);
1610 }
1611
1612 if (CODING_SPEC_ISO_INVOCATION (coding, 0) != reg
1613 && CODING_SPEC_ISO_INVOCATION (coding, 1) != reg)
1614 {
1615 /* Since the graphic register REG is not invoked to any graphic
1616 planes, invoke it to graphic plane 0. */
1617 switch (reg)
1618 {
1619 case 0: /* graphic register 0 */
1620 ENCODE_SHIFT_IN;
1621 break;
1622
1623 case 1: /* graphic register 1 */
1624 ENCODE_SHIFT_OUT;
1625 break;
1626
1627 case 2: /* graphic register 2 */
1628 if (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT)
1629 ENCODE_SINGLE_SHIFT_2;
1630 else
1631 ENCODE_LOCKING_SHIFT_2;
1632 break;
1633
1634 case 3: /* graphic register 3 */
1635 if (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT)
1636 ENCODE_SINGLE_SHIFT_3;
1637 else
1638 ENCODE_LOCKING_SHIFT_3;
1639 break;
1640 }
1641 }
1642 return dst;
1643}
1644
1645/* The following two macros produce codes for indicating composition. */
1646#define ENCODE_COMPOSITION_NO_RULE_START *dst++ = ISO_CODE_ESC, *dst++ = '0'
1647#define ENCODE_COMPOSITION_WITH_RULE_START *dst++ = ISO_CODE_ESC, *dst++ = '2'
1648#define ENCODE_COMPOSITION_END *dst++ = ISO_CODE_ESC, *dst++ = '1'
1649
1650/* The following three macros produce codes for indicating direction
1651 of text. */
1652#define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
1653 do { \
1654 if (coding->flags == CODING_FLAG_ISO_SEVEN_BITS) \
1655 *dst++ = ISO_CODE_ESC, *dst++ = '['; \
1656 else \
1657 *dst++ = ISO_CODE_CSI; \
1658 } while (0)
1659
1660#define ENCODE_DIRECTION_R2L \
1661 ENCODE_CONTROL_SEQUENCE_INTRODUCER, *dst++ = '2', *dst++ = ']'
1662
1663#define ENCODE_DIRECTION_L2R \
1664 ENCODE_CONTROL_SEQUENCE_INTRODUCER, *dst++ = '0', *dst++ = ']'
1665
1666/* Produce codes for designation and invocation to reset the graphic
1667 planes and registers to initial state. */
e0e989f6
KH
1668#define ENCODE_RESET_PLANE_AND_REGISTER \
1669 do { \
1670 int reg; \
1671 if (CODING_SPEC_ISO_INVOCATION (coding, 0) != 0) \
1672 ENCODE_SHIFT_IN; \
1673 for (reg = 0; reg < 4; reg++) \
1674 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg) >= 0 \
1675 && (CODING_SPEC_ISO_DESIGNATION (coding, reg) \
1676 != CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg))) \
1677 ENCODE_DESIGNATION \
1678 (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg), reg, coding); \
4ed46869
KH
1679 } while (0)
1680
bdd9fb48 1681/* Produce designation sequences of charsets in the line started from
d46c5b12 1682 SRC to a place pointed by *DSTP, and update DSTP.
bdd9fb48
KH
1683
1684 If the current block ends before any end-of-line, we may fail to
d46c5b12
KH
1685 find all the necessary designations. */
1686
dfcf069d 1687void
bdd9fb48 1688encode_designation_at_bol (coding, table, src, src_end, dstp)
e0e989f6 1689 struct coding_system *coding;
bdd9fb48 1690 Lisp_Object table;
e0e989f6
KH
1691 unsigned char *src, *src_end, **dstp;
1692{
bdd9fb48
KH
1693 int charset, c, found = 0, reg;
1694 /* Table of charsets to be designated to each graphic register. */
1695 int r[4];
1696 unsigned char *dst = *dstp;
1697
1698 for (reg = 0; reg < 4; reg++)
1699 r[reg] = -1;
1700
1701 while (src < src_end && *src != '\n' && found < 4)
e0e989f6 1702 {
bdd9fb48
KH
1703 int bytes = BYTES_BY_CHAR_HEAD (*src);
1704
1705 if (NILP (table))
1706 charset = CHARSET_AT (src);
1707 else
e0e989f6 1708 {
35cb8686
RS
1709 int c_alt;
1710 unsigned char c1, c2;
bdd9fb48
KH
1711
1712 SPLIT_STRING(src, bytes, charset, c1, c2);
1713 if ((c_alt = unify_char (table, -1, charset, c1, c2)) >= 0)
1714 charset = CHAR_CHARSET (c_alt);
e0e989f6 1715 }
bdd9fb48 1716
e0e989f6 1717 reg = CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset);
d46c5b12 1718 if (reg != CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION && r[reg] < 0)
bdd9fb48
KH
1719 {
1720 found++;
1721 r[reg] = charset;
1722 }
1723
1724 src += bytes;
1725 }
1726
1727 if (found)
1728 {
1729 for (reg = 0; reg < 4; reg++)
1730 if (r[reg] >= 0
1731 && CODING_SPEC_ISO_DESIGNATION (coding, reg) != r[reg])
1732 ENCODE_DESIGNATION (r[reg], reg, coding);
1733 *dstp = dst;
e0e989f6 1734 }
e0e989f6
KH
1735}
1736
4ed46869
KH
1737/* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
1738
1739int
d46c5b12 1740encode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
4ed46869
KH
1741 struct coding_system *coding;
1742 unsigned char *source, *destination;
1743 int src_bytes, dst_bytes;
4ed46869
KH
1744{
1745 unsigned char *src = source;
1746 unsigned char *src_end = source + src_bytes;
1747 unsigned char *dst = destination;
1748 unsigned char *dst_end = destination + dst_bytes;
e0e989f6 1749 /* Since the maximum bytes produced by each loop is 20, we subtract 19
4ed46869
KH
1750 from DST_END to assure overflow checking is necessary only at the
1751 head of loop. */
e0e989f6 1752 unsigned char *adjusted_dst_end = dst_end - 19;
a5d301df
KH
1753 Lisp_Object unification_table
1754 = coding->character_unification_table_for_encode;
d46c5b12 1755 int result = CODING_FINISH_NORMAL;
bdd9fb48
KH
1756
1757 if (!NILP (Venable_character_unification) && NILP (unification_table))
a5d301df 1758 unification_table = Vstandard_character_unification_table_for_encode;
4ed46869 1759
d46c5b12 1760 coding->consumed_char = 0;
fb88bf2d 1761 coding->fake_multibyte = 0;
d46c5b12
KH
1762 while (src < src_end && (dst_bytes
1763 ? (dst < adjusted_dst_end)
1764 : (dst < src - 19)))
4ed46869
KH
1765 {
1766 /* SRC_BASE remembers the start position in source in each loop.
1767 The loop will be exited when there's not enough source text
1768 to analyze multi-byte codes (within macros ONE_MORE_BYTE,
1769 TWO_MORE_BYTES, and THREE_MORE_BYTES). In that case, SRC is
1770 reset to SRC_BASE before exiting. */
1771 unsigned char *src_base = src;
bdd9fb48 1772 int charset, c1, c2, c3, c4;
4ed46869 1773
e0e989f6
KH
1774 if (coding->flags & CODING_FLAG_ISO_DESIGNATE_AT_BOL
1775 && CODING_SPEC_ISO_BOL (coding))
1776 {
bdd9fb48
KH
1777 /* We have to produce designation sequences if any now. */
1778 encode_designation_at_bol (coding, unification_table,
1779 src, src_end, &dst);
e0e989f6
KH
1780 CODING_SPEC_ISO_BOL (coding) = 0;
1781 }
1782
1783 c1 = *src++;
4ed46869 1784 /* If we are seeing a component of a composite character, we are
d46c5b12
KH
1785 seeing a leading-code encoded irregularly for composition, or
1786 a composition rule if composing with rule. We must set C1 to
1787 a normal leading-code or an ASCII code. If we are not seeing
1788 a composite character, we must reset composition,
1789 designation, and invocation states. */
4ed46869
KH
1790 if (COMPOSING_P (coding->composing))
1791 {
1792 if (c1 < 0xA0)
1793 {
1794 /* We are not in a composite character any longer. */
1795 coding->composing = COMPOSING_NO;
d46c5b12 1796 ENCODE_RESET_PLANE_AND_REGISTER;
4ed46869
KH
1797 ENCODE_COMPOSITION_END;
1798 }
1799 else
1800 {
1801 if (coding->composing == COMPOSING_WITH_RULE_RULE)
1802 {
1803 *dst++ = c1 & 0x7F;
1804 coding->composing = COMPOSING_WITH_RULE_HEAD;
1805 continue;
1806 }
1807 else if (coding->composing == COMPOSING_WITH_RULE_HEAD)
1808 coding->composing = COMPOSING_WITH_RULE_RULE;
1809 if (c1 == 0xA0)
1810 {
1811 /* This is an ASCII component. */
1812 ONE_MORE_BYTE (c1);
1813 c1 &= 0x7F;
1814 }
1815 else
1816 /* This is a leading-code of non ASCII component. */
1817 c1 -= 0x20;
1818 }
1819 }
1820
1821 /* Now encode one character. C1 is a control character, an
1822 ASCII character, or a leading-code of multi-byte character. */
1823 switch (emacs_code_class[c1])
1824 {
1825 case EMACS_ascii_code:
bdd9fb48 1826 ENCODE_ISO_CHARACTER (CHARSET_ASCII, c1, /* dummy */ c2);
4ed46869
KH
1827 break;
1828
1829 case EMACS_control_code:
1830 if (coding->flags & CODING_FLAG_ISO_RESET_AT_CNTL)
e0e989f6 1831 ENCODE_RESET_PLANE_AND_REGISTER;
4ed46869 1832 *dst++ = c1;
d46c5b12 1833 coding->consumed_char++;
4ed46869
KH
1834 break;
1835
1836 case EMACS_carriage_return_code:
d46c5b12 1837 if (! (coding->mode & CODING_MODE_SELECTIVE_DISPLAY))
4ed46869
KH
1838 {
1839 if (coding->flags & CODING_FLAG_ISO_RESET_AT_CNTL)
e0e989f6 1840 ENCODE_RESET_PLANE_AND_REGISTER;
4ed46869 1841 *dst++ = c1;
d46c5b12 1842 coding->consumed_char++;
4ed46869
KH
1843 break;
1844 }
1845 /* fall down to treat '\r' as '\n' ... */
1846
1847 case EMACS_linefeed_code:
1848 if (coding->flags & CODING_FLAG_ISO_RESET_AT_EOL)
e0e989f6
KH
1849 ENCODE_RESET_PLANE_AND_REGISTER;
1850 if (coding->flags & CODING_FLAG_ISO_INIT_AT_BOL)
1851 bcopy (coding->spec.iso2022.initial_designation,
1852 coding->spec.iso2022.current_designation,
1853 sizeof coding->spec.iso2022.initial_designation);
4ed46869 1854 if (coding->eol_type == CODING_EOL_LF
0ef69138 1855 || coding->eol_type == CODING_EOL_UNDECIDED)
4ed46869
KH
1856 *dst++ = ISO_CODE_LF;
1857 else if (coding->eol_type == CODING_EOL_CRLF)
1858 *dst++ = ISO_CODE_CR, *dst++ = ISO_CODE_LF;
1859 else
1860 *dst++ = ISO_CODE_CR;
e0e989f6 1861 CODING_SPEC_ISO_BOL (coding) = 1;
d46c5b12 1862 coding->consumed_char++;
4ed46869
KH
1863 break;
1864
1865 case EMACS_leading_code_2:
1866 ONE_MORE_BYTE (c2);
19a8d9e0
KH
1867 if (c2 < 0xA0)
1868 {
1869 /* invalid sequence */
1870 *dst++ = c1;
1871 *dst++ = c2;
d46c5b12 1872 coding->consumed_char += 2;
19a8d9e0
KH
1873 }
1874 else
1875 ENCODE_ISO_CHARACTER (c1, c2, /* dummy */ c3);
4ed46869
KH
1876 break;
1877
1878 case EMACS_leading_code_3:
1879 TWO_MORE_BYTES (c2, c3);
19a8d9e0
KH
1880 if (c2 < 0xA0 || c3 < 0xA0)
1881 {
1882 /* invalid sequence */
1883 *dst++ = c1;
1884 *dst++ = c2;
1885 *dst++ = c3;
d46c5b12 1886 coding->consumed_char += 3;
19a8d9e0
KH
1887 }
1888 else if (c1 < LEADING_CODE_PRIVATE_11)
bdd9fb48 1889 ENCODE_ISO_CHARACTER (c1, c2, c3);
4ed46869 1890 else
bdd9fb48 1891 ENCODE_ISO_CHARACTER (c2, c3, /* dummy */ c4);
4ed46869
KH
1892 break;
1893
1894 case EMACS_leading_code_4:
1895 THREE_MORE_BYTES (c2, c3, c4);
19a8d9e0
KH
1896 if (c2 < 0xA0 || c3 < 0xA0 || c4 < 0xA0)
1897 {
1898 /* invalid sequence */
1899 *dst++ = c1;
1900 *dst++ = c2;
1901 *dst++ = c3;
1902 *dst++ = c4;
d46c5b12 1903 coding->consumed_char += 4;
19a8d9e0
KH
1904 }
1905 else
1906 ENCODE_ISO_CHARACTER (c2, c3, c4);
4ed46869
KH
1907 break;
1908
1909 case EMACS_leading_code_composition:
19a8d9e0
KH
1910 ONE_MORE_BYTE (c2);
1911 if (c2 < 0xA0)
1912 {
1913 /* invalid sequence */
1914 *dst++ = c1;
1915 *dst++ = c2;
d46c5b12 1916 coding->consumed_char += 2;
19a8d9e0
KH
1917 }
1918 else if (c2 == 0xFF)
4ed46869 1919 {
d46c5b12 1920 ENCODE_RESET_PLANE_AND_REGISTER;
4ed46869
KH
1921 coding->composing = COMPOSING_WITH_RULE_HEAD;
1922 ENCODE_COMPOSITION_WITH_RULE_START;
d46c5b12 1923 coding->consumed_char++;
4ed46869
KH
1924 }
1925 else
1926 {
d46c5b12 1927 ENCODE_RESET_PLANE_AND_REGISTER;
4ed46869
KH
1928 /* Rewind one byte because it is a character code of
1929 composition elements. */
1930 src--;
1931 coding->composing = COMPOSING_NO_RULE_HEAD;
1932 ENCODE_COMPOSITION_NO_RULE_START;
d46c5b12 1933 coding->consumed_char++;
4ed46869
KH
1934 }
1935 break;
1936
1937 case EMACS_invalid_code:
1938 *dst++ = c1;
d46c5b12 1939 coding->consumed_char++;
4ed46869
KH
1940 break;
1941 }
1942 continue;
1943 label_end_of_loop:
d46c5b12
KH
1944 result = CODING_FINISH_INSUFFICIENT_SRC;
1945 src = src_base;
4ed46869
KH
1946 break;
1947 }
1948
fb88bf2d
KH
1949 if (src < src_end)
1950 {
1951 if (result == CODING_FINISH_NORMAL)
1952 result = CODING_FINISH_INSUFFICIENT_DST;
1953 else
1954 /* If this is the last block of the text to be encoded, we
1955 must reset graphic planes and registers to the initial
1956 state, and flush out the carryover if any. */
1957 if (coding->mode & CODING_MODE_LAST_BLOCK)
1958 ENCODE_RESET_PLANE_AND_REGISTER;
1959 }
d46c5b12
KH
1960
1961 coding->consumed = src - source;
1962 coding->produced = coding->produced_char = dst - destination;
1963 return result;
4ed46869
KH
1964}
1965
1966\f
1967/*** 4. SJIS and BIG5 handlers ***/
1968
f4dee582 1969/* Although SJIS and BIG5 are not ISO's coding system, they are used
4ed46869
KH
1970 quite widely. So, for the moment, Emacs supports them in the bare
1971 C code. But, in the future, they may be supported only by CCL. */
1972
1973/* SJIS is a coding system encoding three character sets: ASCII, right
1974 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
1975 as is. A character of charset katakana-jisx0201 is encoded by
1976 "position-code + 0x80". A character of charset japanese-jisx0208
1977 is encoded in 2-byte but two position-codes are divided and shifted
1978 so that it fit in the range below.
1979
1980 --- CODE RANGE of SJIS ---
1981 (character set) (range)
1982 ASCII 0x00 .. 0x7F
1983 KATAKANA-JISX0201 0xA0 .. 0xDF
1984 JISX0208 (1st byte) 0x80 .. 0x9F and 0xE0 .. 0xFF
1985 (2nd byte) 0x40 .. 0xFF
1986 -------------------------------
1987
1988*/
1989
1990/* BIG5 is a coding system encoding two character sets: ASCII and
1991 Big5. An ASCII character is encoded as is. Big5 is a two-byte
1992 character set and is encoded in two-byte.
1993
1994 --- CODE RANGE of BIG5 ---
1995 (character set) (range)
1996 ASCII 0x00 .. 0x7F
1997 Big5 (1st byte) 0xA1 .. 0xFE
1998 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
1999 --------------------------
2000
2001 Since the number of characters in Big5 is larger than maximum
2002 characters in Emacs' charset (96x96), it can't be handled as one
2003 charset. So, in Emacs, Big5 is divided into two: `charset-big5-1'
2004 and `charset-big5-2'. Both are DIMENSION2 and CHARS94. The former
2005 contains frequently used characters and the latter contains less
2006 frequently used characters. */
2007
2008/* Macros to decode or encode a character of Big5 in BIG5. B1 and B2
2009 are the 1st and 2nd position-codes of Big5 in BIG5 coding system.
2010 C1 and C2 are the 1st and 2nd position-codes of of Emacs' internal
2011 format. CHARSET is `charset_big5_1' or `charset_big5_2'. */
2012
2013/* Number of Big5 characters which have the same code in 1st byte. */
2014#define BIG5_SAME_ROW (0xFF - 0xA1 + 0x7F - 0x40)
2015
2016#define DECODE_BIG5(b1, b2, charset, c1, c2) \
2017 do { \
2018 unsigned int temp \
2019 = (b1 - 0xA1) * BIG5_SAME_ROW + b2 - (b2 < 0x7F ? 0x40 : 0x62); \
2020 if (b1 < 0xC9) \
2021 charset = charset_big5_1; \
2022 else \
2023 { \
2024 charset = charset_big5_2; \
2025 temp -= (0xC9 - 0xA1) * BIG5_SAME_ROW; \
2026 } \
2027 c1 = temp / (0xFF - 0xA1) + 0x21; \
2028 c2 = temp % (0xFF - 0xA1) + 0x21; \
2029 } while (0)
2030
2031#define ENCODE_BIG5(charset, c1, c2, b1, b2) \
2032 do { \
2033 unsigned int temp = (c1 - 0x21) * (0xFF - 0xA1) + (c2 - 0x21); \
2034 if (charset == charset_big5_2) \
2035 temp += BIG5_SAME_ROW * (0xC9 - 0xA1); \
2036 b1 = temp / BIG5_SAME_ROW + 0xA1; \
2037 b2 = temp % BIG5_SAME_ROW; \
2038 b2 += b2 < 0x3F ? 0x40 : 0x62; \
2039 } while (0)
2040
a5d301df
KH
2041#define DECODE_SJIS_BIG5_CHARACTER(charset, c1, c2) \
2042 do { \
2043 int c_alt, charset_alt = (charset); \
2044 if (!NILP (unification_table) \
2045 && ((c_alt = unify_char (unification_table, \
2046 -1, (charset), c1, c2)) >= 0)) \
2047 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
2048 if (charset_alt == CHARSET_ASCII || charset_alt < 0) \
2049 DECODE_CHARACTER_ASCII (c1); \
2050 else if (CHARSET_DIMENSION (charset_alt) == 1) \
2051 DECODE_CHARACTER_DIMENSION1 (charset_alt, c1); \
2052 else \
2053 DECODE_CHARACTER_DIMENSION2 (charset_alt, c1, c2); \
2054 } while (0)
2055
2056#define ENCODE_SJIS_BIG5_CHARACTER(charset, c1, c2) \
2057 do { \
2058 int c_alt, charset_alt; \
2059 if (!NILP (unification_table) \
2060 && ((c_alt = unify_char (unification_table, -1, charset, c1, c2)) \
2061 >= 0)) \
2062 SPLIT_CHAR (c_alt, charset_alt, c1, c2); \
2063 else \
2064 charset_alt = charset; \
2065 if (charset_alt == charset_ascii) \
2066 *dst++ = c1; \
2067 else if (CHARSET_DIMENSION (charset_alt) == 1) \
2068 { \
2069 if (sjis_p && charset_alt == charset_katakana_jisx0201) \
2070 *dst++ = c1; \
2071 else \
fb88bf2d
KH
2072 { \
2073 *dst++ = charset_alt, *dst++ = c1; \
2074 coding->fake_multibyte = 1; \
2075 } \
a5d301df
KH
2076 } \
2077 else \
2078 { \
2079 c1 &= 0x7F, c2 &= 0x7F; \
2080 if (sjis_p && charset_alt == charset_jisx0208) \
2081 { \
2082 unsigned char s1, s2; \
fb88bf2d 2083 \
a5d301df
KH
2084 ENCODE_SJIS (c1, c2, s1, s2); \
2085 *dst++ = s1, *dst++ = s2; \
fb88bf2d 2086 coding->fake_multibyte = 1; \
a5d301df
KH
2087 } \
2088 else if (!sjis_p \
2089 && (charset_alt == charset_big5_1 \
2090 || charset_alt == charset_big5_2)) \
2091 { \
2092 unsigned char b1, b2; \
fb88bf2d 2093 \
9ce27fde 2094 ENCODE_BIG5 (charset_alt, c1, c2, b1, b2); \
a5d301df
KH
2095 *dst++ = b1, *dst++ = b2; \
2096 } \
2097 else \
fb88bf2d
KH
2098 { \
2099 *dst++ = charset_alt, *dst++ = c1, *dst++ = c2; \
2100 coding->fake_multibyte = 1; \
2101 } \
a5d301df 2102 } \
d46c5b12 2103 coding->consumed_char++; \
a5d301df
KH
2104 } while (0);
2105
4ed46869
KH
2106/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2107 Check if a text is encoded in SJIS. If it is, return
2108 CODING_CATEGORY_MASK_SJIS, else return 0. */
2109
2110int
2111detect_coding_sjis (src, src_end)
2112 unsigned char *src, *src_end;
2113{
2114 unsigned char c;
2115
2116 while (src < src_end)
2117 {
2118 c = *src++;
4ed46869
KH
2119 if ((c >= 0x80 && c < 0xA0) || c >= 0xE0)
2120 {
2121 if (src < src_end && *src++ < 0x40)
2122 return 0;
2123 }
2124 }
2125 return CODING_CATEGORY_MASK_SJIS;
2126}
2127
2128/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2129 Check if a text is encoded in BIG5. If it is, return
2130 CODING_CATEGORY_MASK_BIG5, else return 0. */
2131
2132int
2133detect_coding_big5 (src, src_end)
2134 unsigned char *src, *src_end;
2135{
2136 unsigned char c;
2137
2138 while (src < src_end)
2139 {
2140 c = *src++;
4ed46869
KH
2141 if (c >= 0xA1)
2142 {
2143 if (src >= src_end)
2144 break;
2145 c = *src++;
2146 if (c < 0x40 || (c >= 0x7F && c <= 0xA0))
2147 return 0;
2148 }
2149 }
2150 return CODING_CATEGORY_MASK_BIG5;
2151}
2152
2153/* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
2154 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
2155
2156int
2157decode_coding_sjis_big5 (coding, source, destination,
d46c5b12 2158 src_bytes, dst_bytes, sjis_p)
4ed46869
KH
2159 struct coding_system *coding;
2160 unsigned char *source, *destination;
2161 int src_bytes, dst_bytes;
4ed46869
KH
2162 int sjis_p;
2163{
2164 unsigned char *src = source;
2165 unsigned char *src_end = source + src_bytes;
2166 unsigned char *dst = destination;
2167 unsigned char *dst_end = destination + dst_bytes;
2168 /* Since the maximum bytes produced by each loop is 4, we subtract 3
2169 from DST_END to assure overflow checking is necessary only at the
2170 head of loop. */
2171 unsigned char *adjusted_dst_end = dst_end - 3;
a5d301df
KH
2172 Lisp_Object unification_table
2173 = coding->character_unification_table_for_decode;
d46c5b12 2174 int result = CODING_FINISH_NORMAL;
a5d301df
KH
2175
2176 if (!NILP (Venable_character_unification) && NILP (unification_table))
2177 unification_table = Vstandard_character_unification_table_for_decode;
4ed46869 2178
d46c5b12 2179 coding->produced_char = 0;
fb88bf2d 2180 coding->fake_multibyte = 0;
d46c5b12
KH
2181 while (src < src_end && (dst_bytes
2182 ? (dst < adjusted_dst_end)
2183 : (dst < src - 3)))
4ed46869
KH
2184 {
2185 /* SRC_BASE remembers the start position in source in each loop.
2186 The loop will be exited when there's not enough source text
2187 to analyze two-byte character (within macro ONE_MORE_BYTE).
2188 In that case, SRC is reset to SRC_BASE before exiting. */
2189 unsigned char *src_base = src;
2190 unsigned char c1 = *src++, c2, c3, c4;
2191
d46c5b12 2192 if (c1 < 0x20)
4ed46869 2193 {
d46c5b12 2194 if (c1 == '\r')
4ed46869 2195 {
d46c5b12
KH
2196 if (coding->eol_type == CODING_EOL_CRLF)
2197 {
2198 ONE_MORE_BYTE (c2);
2199 if (c2 == '\n')
2200 *dst++ = c2;
2201 else if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
2202 {
2203 result = CODING_FINISH_INCONSISTENT_EOL;
2204 goto label_end_of_loop_2;
2205 }
2206 else
2207 /* To process C2 again, SRC is subtracted by 1. */
2208 *dst++ = c1, src--;
2209 }
2210 else if (coding->eol_type == CODING_EOL_CR)
2211 *dst++ = '\n';
4ed46869 2212 else
d46c5b12
KH
2213 *dst++ = c1;
2214 }
2215 else if (c1 == '\n'
2216 && (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
2217 && (coding->eol_type == CODING_EOL_CR
2218 || coding->eol_type == CODING_EOL_CRLF))
2219 {
2220 result = CODING_FINISH_INCONSISTENT_EOL;
2221 goto label_end_of_loop_2;
4ed46869
KH
2222 }
2223 else
2224 *dst++ = c1;
d46c5b12 2225 coding->produced_char++;
4ed46869 2226 }
a5d301df
KH
2227 else if (c1 < 0x80)
2228 DECODE_SJIS_BIG5_CHARACTER (charset_ascii, c1, /* dummy */ c2);
fb88bf2d 2229 else if (c1 < 0xA0)
4ed46869 2230 {
fb88bf2d 2231 /* SJIS -> JISX0208 */
4ed46869
KH
2232 if (sjis_p)
2233 {
2234 ONE_MORE_BYTE (c2);
fb88bf2d
KH
2235 if (c2 >= 0x40)
2236 {
2237 DECODE_SJIS (c1, c2, c3, c4);
2238 DECODE_SJIS_BIG5_CHARACTER (charset_jisx0208, c3, c4);
2239 }
2240 else
2241 goto label_invalid_code_2;
4ed46869 2242 }
fb88bf2d
KH
2243 else
2244 goto label_invalid_code_1;
2245 }
2246 else if (c1 < 0xE0)
2247 {
2248 /* SJIS -> JISX0201-Kana, BIG5 -> Big5 */
2249 if (sjis_p)
2250 DECODE_SJIS_BIG5_CHARACTER (charset_katakana_jisx0201, c1,
2251 /* dummy */ c2);
2252 else
4ed46869
KH
2253 {
2254 int charset;
2255
2256 ONE_MORE_BYTE (c2);
fb88bf2d
KH
2257 if ((c2 >= 0x40 && c2 <= 0x7E) || (c2 >= 0xA1 && c2 <= 0xFE))
2258 {
2259 DECODE_BIG5 (c1, c2, charset, c3, c4);
2260 DECODE_SJIS_BIG5_CHARACTER (charset, c3, c4);
2261 }
2262 else
2263 goto label_invalid_code_2;
d46c5b12 2264 }
4ed46869 2265 }
fb88bf2d 2266 else /* C1 >= 0xE0 */
4ed46869 2267 {
fb88bf2d 2268 /* SJIS -> JISX0208, BIG5 -> Big5 */
4ed46869 2269 if (sjis_p)
fb88bf2d
KH
2270 {
2271 ONE_MORE_BYTE (c2);
2272 if (c2 >= 0x40)
2273 {
2274 DECODE_SJIS (c1, c2, c3, c4);
2275 DECODE_SJIS_BIG5_CHARACTER (charset_jisx0208, c3, c4);
2276 }
2277 else
2278 goto label_invalid_code_2;
2279 }
4ed46869
KH
2280 else
2281 {
2282 int charset;
2283
2284 ONE_MORE_BYTE (c2);
fb88bf2d
KH
2285 if ((c2 >= 0x40 && c2 <= 0x7E) || (c2 >= 0xA1 && c2 <= 0xFE))
2286 {
2287 DECODE_BIG5 (c1, c2, charset, c3, c4);
2288 DECODE_SJIS_BIG5_CHARACTER (charset, c3, c4);
2289 }
2290 else
2291 goto label_invalid_code_2;
4ed46869
KH
2292 }
2293 }
2294 continue;
2295
fb88bf2d
KH
2296 label_invalid_code_1:
2297 *dst++ = c1;
2298 coding->produced_char++;
2299 coding->fake_multibyte = 1;
2300 continue;
2301
2302 label_invalid_code_2:
2303 *dst++ = c1; *dst++= c2;
2304 coding->produced_char += 2;
2305 coding->fake_multibyte = 1;
2306 continue;
2307
4ed46869 2308 label_end_of_loop:
d46c5b12
KH
2309 result = CODING_FINISH_INSUFFICIENT_SRC;
2310 label_end_of_loop_2:
4ed46869
KH
2311 src = src_base;
2312 break;
2313 }
2314
fb88bf2d
KH
2315 if (src < src_end)
2316 {
2317 if (result == CODING_FINISH_NORMAL)
2318 result = CODING_FINISH_INSUFFICIENT_DST;
2319 else if (result != CODING_FINISH_INCONSISTENT_EOL
2320 && coding->mode & CODING_MODE_LAST_BLOCK)
2321 {
2322 src_bytes = src_end - src;
2323 if (dst_bytes && (dst_end - dst < src_bytes))
2324 src_bytes = dst_end - dst;
2325 bcopy (dst, src, src_bytes);
2326 src += src_bytes;
2327 dst += src_bytes;
2328 coding->fake_multibyte = 1;
2329 }
2330 }
d46c5b12
KH
2331
2332 coding->consumed = coding->consumed_char = src - source;
2333 coding->produced = dst - destination;
2334 return result;
4ed46869
KH
2335}
2336
2337/* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
2338 This function can encode `charset_ascii', `charset_katakana_jisx0201',
2339 `charset_jisx0208', `charset_big5_1', and `charset_big5-2'. We are
2340 sure that all these charsets are registered as official charset
2341 (i.e. do not have extended leading-codes). Characters of other
2342 charsets are produced without any encoding. If SJIS_P is 1, encode
2343 SJIS text, else encode BIG5 text. */
2344
2345int
2346encode_coding_sjis_big5 (coding, source, destination,
d46c5b12 2347 src_bytes, dst_bytes, sjis_p)
4ed46869
KH
2348 struct coding_system *coding;
2349 unsigned char *source, *destination;
2350 int src_bytes, dst_bytes;
4ed46869
KH
2351 int sjis_p;
2352{
2353 unsigned char *src = source;
2354 unsigned char *src_end = source + src_bytes;
2355 unsigned char *dst = destination;
2356 unsigned char *dst_end = destination + dst_bytes;
2357 /* Since the maximum bytes produced by each loop is 2, we subtract 1
2358 from DST_END to assure overflow checking is necessary only at the
2359 head of loop. */
2360 unsigned char *adjusted_dst_end = dst_end - 1;
a5d301df
KH
2361 Lisp_Object unification_table
2362 = coding->character_unification_table_for_encode;
d46c5b12 2363 int result = CODING_FINISH_NORMAL;
a5d301df
KH
2364
2365 if (!NILP (Venable_character_unification) && NILP (unification_table))
2366 unification_table = Vstandard_character_unification_table_for_encode;
4ed46869 2367
d46c5b12 2368 coding->consumed_char = 0;
fb88bf2d 2369 coding->fake_multibyte = 0;
d46c5b12
KH
2370 while (src < src_end && (dst_bytes
2371 ? (dst < adjusted_dst_end)
2372 : (dst < src - 1)))
4ed46869
KH
2373 {
2374 /* SRC_BASE remembers the start position in source in each loop.
2375 The loop will be exited when there's not enough source text
2376 to analyze multi-byte codes (within macros ONE_MORE_BYTE and
2377 TWO_MORE_BYTES). In that case, SRC is reset to SRC_BASE
2378 before exiting. */
2379 unsigned char *src_base = src;
2380 unsigned char c1 = *src++, c2, c3, c4;
2381
2382 if (coding->composing)
2383 {
2384 if (c1 == 0xA0)
2385 {
2386 ONE_MORE_BYTE (c1);
2387 c1 &= 0x7F;
2388 }
2389 else if (c1 >= 0xA0)
2390 c1 -= 0x20;
2391 else
2392 coding->composing = 0;
2393 }
2394
2395 switch (emacs_code_class[c1])
2396 {
2397 case EMACS_ascii_code:
a5d301df
KH
2398 ENCODE_SJIS_BIG5_CHARACTER (charset_ascii, c1, /* dummy */ c2);
2399 break;
2400
4ed46869
KH
2401 case EMACS_control_code:
2402 *dst++ = c1;
d46c5b12 2403 coding->consumed_char++;
4ed46869
KH
2404 break;
2405
2406 case EMACS_carriage_return_code:
d46c5b12 2407 if (! (coding->mode & CODING_MODE_SELECTIVE_DISPLAY))
4ed46869
KH
2408 {
2409 *dst++ = c1;
d46c5b12 2410 coding->consumed_char++;
4ed46869
KH
2411 break;
2412 }
2413 /* fall down to treat '\r' as '\n' ... */
2414
2415 case EMACS_linefeed_code:
2416 if (coding->eol_type == CODING_EOL_LF
0ef69138 2417 || coding->eol_type == CODING_EOL_UNDECIDED)
4ed46869
KH
2418 *dst++ = '\n';
2419 else if (coding->eol_type == CODING_EOL_CRLF)
2420 *dst++ = '\r', *dst++ = '\n';
2421 else
2422 *dst++ = '\r';
d46c5b12 2423 coding->consumed_char++;
4ed46869
KH
2424 break;
2425
2426 case EMACS_leading_code_2:
2427 ONE_MORE_BYTE (c2);
a5d301df 2428 ENCODE_SJIS_BIG5_CHARACTER (c1, c2, /* dummy */ c3);
4ed46869
KH
2429 break;
2430
2431 case EMACS_leading_code_3:
2432 TWO_MORE_BYTES (c2, c3);
a5d301df 2433 ENCODE_SJIS_BIG5_CHARACTER (c1, c2, c3);
4ed46869
KH
2434 break;
2435
2436 case EMACS_leading_code_4:
2437 THREE_MORE_BYTES (c2, c3, c4);
a5d301df 2438 ENCODE_SJIS_BIG5_CHARACTER (c2, c3, c4);
4ed46869
KH
2439 break;
2440
2441 case EMACS_leading_code_composition:
2442 coding->composing = 1;
2443 break;
2444
2445 default: /* i.e. case EMACS_invalid_code: */
2446 *dst++ = c1;
d46c5b12 2447 coding->consumed_char++;
4ed46869
KH
2448 }
2449 continue;
2450
2451 label_end_of_loop:
d46c5b12
KH
2452 result = CODING_FINISH_INSUFFICIENT_SRC;
2453 src = src_base;
4ed46869
KH
2454 break;
2455 }
2456
d46c5b12
KH
2457 if (result == CODING_FINISH_NORMAL
2458 && src < src_end)
2459 result = CODING_FINISH_INSUFFICIENT_DST;
2460 coding->consumed = src - source;
2461 coding->produced = coding->produced_char = dst - destination;
2462 return result;
4ed46869
KH
2463}
2464
2465\f
2466/*** 5. End-of-line handlers ***/
2467
2468/* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
2469 This function is called only when `coding->eol_type' is
2470 CODING_EOL_CRLF or CODING_EOL_CR. */
2471
dfcf069d 2472int
d46c5b12 2473decode_eol (coding, source, destination, src_bytes, dst_bytes)
4ed46869
KH
2474 struct coding_system *coding;
2475 unsigned char *source, *destination;
2476 int src_bytes, dst_bytes;
4ed46869
KH
2477{
2478 unsigned char *src = source;
2479 unsigned char *src_end = source + src_bytes;
2480 unsigned char *dst = destination;
2481 unsigned char *dst_end = destination + dst_bytes;
fb88bf2d 2482 unsigned char c;
d46c5b12
KH
2483 int result = CODING_FINISH_NORMAL;
2484
fb88bf2d
KH
2485 coding->fake_multibyte = 0;
2486
d46c5b12
KH
2487 if (src_bytes <= 0)
2488 return result;
4ed46869
KH
2489
2490 switch (coding->eol_type)
2491 {
2492 case CODING_EOL_CRLF:
2493 {
2494 /* Since the maximum bytes produced by each loop is 2, we
2495 subtract 1 from DST_END to assure overflow checking is
2496 necessary only at the head of loop. */
2497 unsigned char *adjusted_dst_end = dst_end - 1;
2498
d46c5b12
KH
2499 while (src < src_end && (dst_bytes
2500 ? (dst < adjusted_dst_end)
2501 : (dst < src - 1)))
4ed46869
KH
2502 {
2503 unsigned char *src_base = src;
fb88bf2d
KH
2504
2505 c = *src++;
4ed46869
KH
2506 if (c == '\r')
2507 {
2508 ONE_MORE_BYTE (c);
2509 if (c != '\n')
d46c5b12
KH
2510 {
2511 if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
2512 {
2513 result = CODING_FINISH_INCONSISTENT_EOL;
2514 goto label_end_of_loop_2;
2515 }
2516 *dst++ = '\r';
fb88bf2d
KH
2517 if (BASE_LEADING_CODE_P (c))
2518 coding->fake_multibyte = 1;
d46c5b12 2519 }
bfd99048 2520 *dst++ = c;
4ed46869 2521 }
d46c5b12
KH
2522 else if (c == '\n'
2523 && (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL))
2524 {
2525 result = CODING_FINISH_INCONSISTENT_EOL;
2526 goto label_end_of_loop_2;
2527 }
4ed46869 2528 else
fb88bf2d
KH
2529 {
2530 *dst++ = c;
2531 if (BASE_LEADING_CODE_P (c))
2532 coding->fake_multibyte = 1;
2533 }
4ed46869
KH
2534 continue;
2535
2536 label_end_of_loop:
d46c5b12
KH
2537 result = CODING_FINISH_INSUFFICIENT_SRC;
2538 label_end_of_loop_2:
4ed46869
KH
2539 src = src_base;
2540 break;
2541 }
d46c5b12
KH
2542 if (result == CODING_FINISH_NORMAL
2543 && src < src_end)
2544 result = CODING_FINISH_INSUFFICIENT_DST;
4ed46869 2545 }
d46c5b12 2546 break;
4ed46869
KH
2547
2548 case CODING_EOL_CR:
d46c5b12
KH
2549 if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
2550 {
fb88bf2d
KH
2551 while (src < src_end)
2552 {
2553 if ((c = *src++) == '\n')
2554 break;
2555 if (BASE_LEADING_CODE_P (c))
2556 coding->fake_multibyte = 1;
2557 }
d46c5b12
KH
2558 if (*--src == '\n')
2559 {
2560 src_bytes = src - source;
2561 result = CODING_FINISH_INCONSISTENT_EOL;
2562 }
2563 }
2564 if (dst_bytes && src_bytes > dst_bytes)
2565 {
2566 result = CODING_FINISH_INSUFFICIENT_DST;
2567 src_bytes = dst_bytes;
2568 }
2569 if (dst_bytes)
2570 bcopy (source, destination, src_bytes);
2571 else
2572 safe_bcopy (source, destination, src_bytes);
2573 src = source + src_bytes;
2574 while (src_bytes--) if (*dst++ == '\r') dst[-1] = '\n';
4ed46869
KH
2575 break;
2576
2577 default: /* i.e. case: CODING_EOL_LF */
d46c5b12
KH
2578 if (dst_bytes && src_bytes > dst_bytes)
2579 {
2580 result = CODING_FINISH_INSUFFICIENT_DST;
2581 src_bytes = dst_bytes;
2582 }
2583 if (dst_bytes)
2584 bcopy (source, destination, src_bytes);
2585 else
2586 safe_bcopy (source, destination, src_bytes);
2587 src += src_bytes;
2588 dst += dst_bytes;
fb88bf2d 2589 coding->fake_multibyte = 1;
4ed46869
KH
2590 break;
2591 }
2592
d46c5b12
KH
2593 coding->consumed = coding->consumed_char = src - source;
2594 coding->produced = coding->produced_char = dst - destination;
2595 return result;
4ed46869
KH
2596}
2597
2598/* See "GENERAL NOTES about `encode_coding_XXX ()' functions". Encode
2599 format of end-of-line according to `coding->eol_type'. If
d46c5b12
KH
2600 `coding->mode & CODING_MODE_SELECTIVE_DISPLAY' is nonzero, code
2601 '\r' in source text also means end-of-line. */
4ed46869 2602
dfcf069d 2603int
d46c5b12 2604encode_eol (coding, source, destination, src_bytes, dst_bytes)
4ed46869
KH
2605 struct coding_system *coding;
2606 unsigned char *source, *destination;
2607 int src_bytes, dst_bytes;
4ed46869
KH
2608{
2609 unsigned char *src = source;
2610 unsigned char *dst = destination;
d46c5b12 2611 int result = CODING_FINISH_NORMAL;
4ed46869 2612
fb88bf2d
KH
2613 coding->fake_multibyte = 0;
2614
d46c5b12
KH
2615 if (coding->eol_type == CODING_EOL_CRLF)
2616 {
2617 unsigned char c;
2618 unsigned char *src_end = source + src_bytes;
2619 unsigned char *dst_end = destination + dst_bytes;
2620 /* Since the maximum bytes produced by each loop is 2, we
2621 subtract 1 from DST_END to assure overflow checking is
2622 necessary only at the head of loop. */
2623 unsigned char *adjusted_dst_end = dst_end - 1;
2624
2625 while (src < src_end && (dst_bytes
2626 ? (dst < adjusted_dst_end)
2627 : (dst < src - 1)))
2628 {
2629 c = *src++;
2630 if (c == '\n'
2631 || (c == '\r' && (coding->mode & CODING_MODE_SELECTIVE_DISPLAY)))
2632 *dst++ = '\r', *dst++ = '\n';
2633 else
fb88bf2d
KH
2634 {
2635 *dst++ = c;
2636 if (BASE_LEADING_CODE_P (c))
2637 coding->fake_multibyte = 1;
2638 }
d46c5b12
KH
2639 }
2640 if (src < src_end)
2641 result = CODING_FINISH_INSUFFICIENT_DST;
2642 }
2643 else
4ed46869 2644 {
fb88bf2d
KH
2645 unsigned char c;
2646
d46c5b12 2647 if (dst_bytes && src_bytes > dst_bytes)
4ed46869 2648 {
d46c5b12
KH
2649 src_bytes = dst_bytes;
2650 result = CODING_FINISH_INSUFFICIENT_DST;
2651 }
2652 if (dst_bytes)
2653 bcopy (source, destination, src_bytes);
2654 else
fb88bf2d
KH
2655 {
2656 safe_bcopy (source, destination, src_bytes);
2657 dst_bytes = src_bytes;
2658 }
d46c5b12
KH
2659 if (coding->eol_type == CODING_EOL_CRLF)
2660 {
2661 while (src_bytes--)
fb88bf2d
KH
2662 {
2663 if ((c = *dst++) == '\n')
2664 dst[-1] = '\r';
2665 else if (BASE_LEADING_CODE_P (c))
2666 coding->fake_multibyte = 1;
2667 }
d46c5b12 2668 }
fb88bf2d 2669 else
d46c5b12 2670 {
fb88bf2d
KH
2671 if (coding->mode & CODING_MODE_SELECTIVE_DISPLAY)
2672 {
2673 while (src_bytes--)
2674 if (*dst++ == '\r') dst[-1] = '\n';
2675 }
2676 coding->fake_multibyte = 1;
4ed46869 2677 }
fb88bf2d
KH
2678 src = source + dst_bytes;
2679 dst = destination + dst_bytes;
4ed46869
KH
2680 }
2681
d46c5b12
KH
2682 coding->consumed = coding->consumed_char = src - source;
2683 coding->produced = coding->produced_char = dst - destination;
2684 return result;
4ed46869
KH
2685}
2686
2687\f
2688/*** 6. C library functions ***/
2689
2690/* In Emacs Lisp, coding system is represented by a Lisp symbol which
2691 has a property `coding-system'. The value of this property is a
2692 vector of length 5 (called as coding-vector). Among elements of
2693 this vector, the first (element[0]) and the fifth (element[4])
2694 carry important information for decoding/encoding. Before
2695 decoding/encoding, this information should be set in fields of a
2696 structure of type `coding_system'.
2697
2698 A value of property `coding-system' can be a symbol of another
2699 subsidiary coding-system. In that case, Emacs gets coding-vector
2700 from that symbol.
2701
2702 `element[0]' contains information to be set in `coding->type'. The
2703 value and its meaning is as follows:
2704
0ef69138
KH
2705 0 -- coding_type_emacs_mule
2706 1 -- coding_type_sjis
2707 2 -- coding_type_iso2022
2708 3 -- coding_type_big5
2709 4 -- coding_type_ccl encoder/decoder written in CCL
2710 nil -- coding_type_no_conversion
2711 t -- coding_type_undecided (automatic conversion on decoding,
2712 no-conversion on encoding)
4ed46869
KH
2713
2714 `element[4]' contains information to be set in `coding->flags' and
2715 `coding->spec'. The meaning varies by `coding->type'.
2716
2717 If `coding->type' is `coding_type_iso2022', element[4] is a vector
2718 of length 32 (of which the first 13 sub-elements are used now).
2719 Meanings of these sub-elements are:
2720
2721 sub-element[N] where N is 0 through 3: to be set in `coding->spec.iso2022'
2722 If the value is an integer of valid charset, the charset is
2723 assumed to be designated to graphic register N initially.
2724
2725 If the value is minus, it is a minus value of charset which
2726 reserves graphic register N, which means that the charset is
2727 not designated initially but should be designated to graphic
2728 register N just before encoding a character in that charset.
2729
2730 If the value is nil, graphic register N is never used on
2731 encoding.
2732
2733 sub-element[N] where N is 4 through 11: to be set in `coding->flags'
2734 Each value takes t or nil. See the section ISO2022 of
2735 `coding.h' for more information.
2736
2737 If `coding->type' is `coding_type_big5', element[4] is t to denote
2738 BIG5-ETen or nil to denote BIG5-HKU.
2739
2740 If `coding->type' takes the other value, element[4] is ignored.
2741
2742 Emacs Lisp's coding system also carries information about format of
2743 end-of-line in a value of property `eol-type'. If the value is
2744 integer, 0 means CODING_EOL_LF, 1 means CODING_EOL_CRLF, and 2
2745 means CODING_EOL_CR. If it is not integer, it should be a vector
2746 of subsidiary coding systems of which property `eol-type' has one
2747 of above values.
2748
2749*/
2750
2751/* Extract information for decoding/encoding from CODING_SYSTEM_SYMBOL
2752 and set it in CODING. If CODING_SYSTEM_SYMBOL is invalid, CODING
2753 is setup so that no conversion is necessary and return -1, else
2754 return 0. */
2755
2756int
e0e989f6
KH
2757setup_coding_system (coding_system, coding)
2758 Lisp_Object coding_system;
4ed46869
KH
2759 struct coding_system *coding;
2760{
d46c5b12 2761 Lisp_Object coding_spec, coding_type, eol_type, plist;
4608c386 2762 Lisp_Object val;
70c22245 2763 int i;
4ed46869 2764
d46c5b12 2765 /* Initialize some fields required for all kinds of coding systems. */
774324d6 2766 coding->symbol = coding_system;
d46c5b12
KH
2767 coding->common_flags = 0;
2768 coding->mode = 0;
2769 coding->heading_ascii = -1;
2770 coding->post_read_conversion = coding->pre_write_conversion = Qnil;
4608c386
KH
2771 coding_spec = Fget (coding_system, Qcoding_system);
2772 if (!VECTORP (coding_spec)
2773 || XVECTOR (coding_spec)->size != 5
2774 || !CONSP (XVECTOR (coding_spec)->contents[3]))
4ed46869 2775 goto label_invalid_coding_system;
4608c386 2776
d46c5b12
KH
2777 eol_type = inhibit_eol_conversion ? Qnil : Fget (coding_system, Qeol_type);
2778 if (VECTORP (eol_type))
2779 {
2780 coding->eol_type = CODING_EOL_UNDECIDED;
2781 coding->common_flags = CODING_REQUIRE_DETECTION_MASK;
2782 }
2783 else if (XFASTINT (eol_type) == 1)
2784 {
2785 coding->eol_type = CODING_EOL_CRLF;
2786 coding->common_flags
2787 = CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
2788 }
2789 else if (XFASTINT (eol_type) == 2)
2790 {
2791 coding->eol_type = CODING_EOL_CR;
2792 coding->common_flags
2793 = CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
2794 }
2795 else
2796 coding->eol_type = CODING_EOL_LF;
2797
2798 coding_type = XVECTOR (coding_spec)->contents[0];
2799 /* Try short cut. */
2800 if (SYMBOLP (coding_type))
2801 {
2802 if (EQ (coding_type, Qt))
2803 {
2804 coding->type = coding_type_undecided;
2805 coding->common_flags |= CODING_REQUIRE_DETECTION_MASK;
2806 }
2807 else
2808 coding->type = coding_type_no_conversion;
2809 return 0;
2810 }
2811
2812 /* Initialize remaining fields. */
2813 coding->composing = 0;
2814 coding->character_unification_table_for_decode = Qnil;
2815 coding->character_unification_table_for_encode = Qnil;
2816
2817 /* Get values of coding system properties:
2818 `post-read-conversion', `pre-write-conversion',
2819 `character-unification-table-for-decode',
2820 `character-unification-table-for-encode'. */
4608c386
KH
2821 plist = XVECTOR (coding_spec)->contents[3];
2822 coding->post_read_conversion = Fplist_get (plist, Qpost_read_conversion);
2823 coding->pre_write_conversion = Fplist_get (plist, Qpre_write_conversion);
2824 val = Fplist_get (plist, Qcharacter_unification_table_for_decode);
2825 if (SYMBOLP (val))
2826 val = Fget (val, Qcharacter_unification_table_for_decode);
2827 coding->character_unification_table_for_decode
2828 = CHAR_TABLE_P (val) ? val : Qnil;
2829 val = Fplist_get (plist, Qcharacter_unification_table_for_encode);
2830 if (SYMBOLP (val))
2831 val = Fget (val, Qcharacter_unification_table_for_encode);
2832 coding->character_unification_table_for_encode
2833 = CHAR_TABLE_P (val) ? val : Qnil;
d46c5b12
KH
2834 val = Fplist_get (plist, Qcoding_category);
2835 if (!NILP (val))
2836 {
2837 val = Fget (val, Qcoding_category_index);
2838 if (INTEGERP (val))
2839 coding->category_idx = XINT (val);
2840 else
2841 goto label_invalid_coding_system;
2842 }
2843 else
2844 goto label_invalid_coding_system;
4608c386 2845
70c22245
KH
2846 val = Fplist_get (plist, Qsafe_charsets);
2847 if (EQ (val, Qt))
2848 {
2849 for (i = 0; i <= MAX_CHARSET; i++)
2850 coding->safe_charsets[i] = 1;
2851 }
2852 else
2853 {
2854 bzero (coding->safe_charsets, MAX_CHARSET + 1);
2855 while (CONSP (val))
2856 {
2857 if ((i = get_charset_id (XCONS (val)->car)) >= 0)
2858 coding->safe_charsets[i] = 1;
2859 val = XCONS (val)->cdr;
2860 }
2861 }
2862
d46c5b12 2863 switch (XFASTINT (coding_type))
4ed46869
KH
2864 {
2865 case 0:
0ef69138 2866 coding->type = coding_type_emacs_mule;
c952af22
KH
2867 if (!NILP (coding->post_read_conversion))
2868 coding->common_flags |= CODING_REQUIRE_DECODING_MASK;
2869 if (!NILP (coding->pre_write_conversion))
2870 coding->common_flags |= CODING_REQUIRE_ENCODING_MASK;
4ed46869
KH
2871 break;
2872
2873 case 1:
2874 coding->type = coding_type_sjis;
c952af22
KH
2875 coding->common_flags
2876 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
4ed46869
KH
2877 break;
2878
2879 case 2:
2880 coding->type = coding_type_iso2022;
c952af22
KH
2881 coding->common_flags
2882 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
4ed46869 2883 {
70c22245 2884 Lisp_Object val, temp;
4ed46869 2885 Lisp_Object *flags;
d46c5b12 2886 int i, charset, reg_bits = 0;
4ed46869 2887
4608c386 2888 val = XVECTOR (coding_spec)->contents[4];
f44d27ce 2889
4ed46869
KH
2890 if (!VECTORP (val) || XVECTOR (val)->size != 32)
2891 goto label_invalid_coding_system;
2892
2893 flags = XVECTOR (val)->contents;
2894 coding->flags
2895 = ((NILP (flags[4]) ? 0 : CODING_FLAG_ISO_SHORT_FORM)
2896 | (NILP (flags[5]) ? 0 : CODING_FLAG_ISO_RESET_AT_EOL)
2897 | (NILP (flags[6]) ? 0 : CODING_FLAG_ISO_RESET_AT_CNTL)
2898 | (NILP (flags[7]) ? 0 : CODING_FLAG_ISO_SEVEN_BITS)
2899 | (NILP (flags[8]) ? 0 : CODING_FLAG_ISO_LOCKING_SHIFT)
2900 | (NILP (flags[9]) ? 0 : CODING_FLAG_ISO_SINGLE_SHIFT)
2901 | (NILP (flags[10]) ? 0 : CODING_FLAG_ISO_USE_ROMAN)
2902 | (NILP (flags[11]) ? 0 : CODING_FLAG_ISO_USE_OLDJIS)
e0e989f6
KH
2903 | (NILP (flags[12]) ? 0 : CODING_FLAG_ISO_NO_DIRECTION)
2904 | (NILP (flags[13]) ? 0 : CODING_FLAG_ISO_INIT_AT_BOL)
c4825358
KH
2905 | (NILP (flags[14]) ? 0 : CODING_FLAG_ISO_DESIGNATE_AT_BOL)
2906 | (NILP (flags[15]) ? 0 : CODING_FLAG_ISO_SAFE)
3f003981 2907 | (NILP (flags[16]) ? 0 : CODING_FLAG_ISO_LATIN_EXTRA)
c4825358 2908 );
4ed46869
KH
2909
2910 /* Invoke graphic register 0 to plane 0. */
2911 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0;
2912 /* Invoke graphic register 1 to plane 1 if we can use full 8-bit. */
2913 CODING_SPEC_ISO_INVOCATION (coding, 1)
2914 = (coding->flags & CODING_FLAG_ISO_SEVEN_BITS ? -1 : 1);
2915 /* Not single shifting at first. */
6e85d753 2916 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0;
e0e989f6 2917 /* Beginning of buffer should also be regarded as bol. */
6e85d753 2918 CODING_SPEC_ISO_BOL (coding) = 1;
4ed46869 2919
70c22245
KH
2920 for (charset = 0; charset <= MAX_CHARSET; charset++)
2921 CODING_SPEC_ISO_REVISION_NUMBER (coding, charset) = 255;
2922 val = Vcharset_revision_alist;
2923 while (CONSP (val))
2924 {
2925 charset = get_charset_id (Fcar_safe (XCONS (val)->car));
2926 if (charset >= 0
2927 && (temp = Fcdr_safe (XCONS (val)->car), INTEGERP (temp))
2928 && (i = XINT (temp), (i >= 0 && (i + '@') < 128)))
2929 CODING_SPEC_ISO_REVISION_NUMBER (coding, charset) = i;
2930 val = XCONS (val)->cdr;
2931 }
2932
4ed46869
KH
2933 /* Checks FLAGS[REG] (REG = 0, 1, 2 3) and decide designations.
2934 FLAGS[REG] can be one of below:
2935 integer CHARSET: CHARSET occupies register I,
2936 t: designate nothing to REG initially, but can be used
2937 by any charsets,
2938 list of integer, nil, or t: designate the first
2939 element (if integer) to REG initially, the remaining
2940 elements (if integer) is designated to REG on request,
d46c5b12 2941 if an element is t, REG can be used by any charsets,
4ed46869 2942 nil: REG is never used. */
467e7675 2943 for (charset = 0; charset <= MAX_CHARSET; charset++)
1ba9e4ab
KH
2944 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
2945 = CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION;
4ed46869
KH
2946 for (i = 0; i < 4; i++)
2947 {
2948 if (INTEGERP (flags[i])
e0e989f6
KH
2949 && (charset = XINT (flags[i]), CHARSET_VALID_P (charset))
2950 || (charset = get_charset_id (flags[i])) >= 0)
4ed46869
KH
2951 {
2952 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = charset;
2953 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) = i;
2954 }
2955 else if (EQ (flags[i], Qt))
2956 {
2957 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = -1;
d46c5b12
KH
2958 reg_bits |= 1 << i;
2959 coding->flags |= CODING_FLAG_ISO_DESIGNATION;
4ed46869
KH
2960 }
2961 else if (CONSP (flags[i]))
2962 {
2963 Lisp_Object tail = flags[i];
2964
d46c5b12 2965 coding->flags |= CODING_FLAG_ISO_DESIGNATION;
4ed46869
KH
2966 if (INTEGERP (XCONS (tail)->car)
2967 && (charset = XINT (XCONS (tail)->car),
e0e989f6
KH
2968 CHARSET_VALID_P (charset))
2969 || (charset = get_charset_id (XCONS (tail)->car)) >= 0)
4ed46869
KH
2970 {
2971 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = charset;
2972 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) =i;
2973 }
2974 else
2975 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = -1;
2976 tail = XCONS (tail)->cdr;
2977 while (CONSP (tail))
2978 {
2979 if (INTEGERP (XCONS (tail)->car)
2980 && (charset = XINT (XCONS (tail)->car),
e0e989f6
KH
2981 CHARSET_VALID_P (charset))
2982 || (charset = get_charset_id (XCONS (tail)->car)) >= 0)
70c22245
KH
2983 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
2984 = i;
4ed46869 2985 else if (EQ (XCONS (tail)->car, Qt))
d46c5b12 2986 reg_bits |= 1 << i;
4ed46869
KH
2987 tail = XCONS (tail)->cdr;
2988 }
2989 }
2990 else
2991 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = -1;
2992
2993 CODING_SPEC_ISO_DESIGNATION (coding, i)
2994 = CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i);
2995 }
2996
d46c5b12 2997 if (reg_bits && ! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT))
4ed46869
KH
2998 {
2999 /* REG 1 can be used only by locking shift in 7-bit env. */
3000 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS)
d46c5b12 3001 reg_bits &= ~2;
4ed46869
KH
3002 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT))
3003 /* Without any shifting, only REG 0 and 1 can be used. */
d46c5b12 3004 reg_bits &= 3;
4ed46869
KH
3005 }
3006
d46c5b12
KH
3007 if (reg_bits)
3008 for (charset = 0; charset <= MAX_CHARSET; charset++)
6e85d753 3009 {
d46c5b12
KH
3010 if (CHARSET_VALID_P (charset))
3011 {
3012 /* There exist some default graphic registers to be
3013 used CHARSET. */
3014
3015 /* We had better avoid designating a charset of
3016 CHARS96 to REG 0 as far as possible. */
3017 if (CHARSET_CHARS (charset) == 96)
3018 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3019 = (reg_bits & 2
3020 ? 1 : (reg_bits & 4 ? 2 : (reg_bits & 8 ? 3 : 0)));
3021 else
3022 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3023 = (reg_bits & 1
3024 ? 0 : (reg_bits & 2 ? 1 : (reg_bits & 4 ? 2 : 3)));
3025 }
6e85d753 3026 }
4ed46869 3027 }
c952af22 3028 coding->common_flags |= CODING_REQUIRE_FLUSHING_MASK;
d46c5b12 3029 coding->spec.iso2022.last_invalid_designation_register = -1;
4ed46869
KH
3030 break;
3031
3032 case 3:
3033 coding->type = coding_type_big5;
c952af22
KH
3034 coding->common_flags
3035 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
4ed46869 3036 coding->flags
4608c386 3037 = (NILP (XVECTOR (coding_spec)->contents[4])
4ed46869
KH
3038 ? CODING_FLAG_BIG5_HKU
3039 : CODING_FLAG_BIG5_ETEN);
3040 break;
3041
3042 case 4:
3043 coding->type = coding_type_ccl;
c952af22
KH
3044 coding->common_flags
3045 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
4ed46869 3046 {
4608c386 3047 Lisp_Object val = XVECTOR (coding_spec)->contents[4];
d21ca14d
KH
3048 Lisp_Object decoder, encoder;
3049
4ed46869 3050 if (CONSP (val)
d21ca14d
KH
3051 && SYMBOLP (XCONS (val)->car)
3052 && !NILP (decoder = Fget (XCONS (val)->car, Qccl_program_idx))
3053 && (decoder = Fcdr (Faref (Vccl_program_table, decoder)))
3054 && SYMBOLP (XCONS (val)->cdr)
3055 && !NILP (encoder = Fget (XCONS (val)->cdr, Qccl_program_idx))
3056 && (encoder = Fcdr (Faref (Vccl_program_table, encoder))))
4ed46869 3057 {
d21ca14d
KH
3058 setup_ccl_program (&(coding->spec.ccl.decoder), decoder);
3059 setup_ccl_program (&(coding->spec.ccl.encoder), encoder);
4ed46869
KH
3060 }
3061 else
3062 goto label_invalid_coding_system;
3063 }
c952af22 3064 coding->common_flags |= CODING_REQUIRE_FLUSHING_MASK;
4ed46869
KH
3065 break;
3066
27901516
KH
3067 case 5:
3068 coding->type = coding_type_raw_text;
3069 break;
3070
4ed46869 3071 default:
d46c5b12 3072 goto label_invalid_coding_system;
4ed46869
KH
3073 }
3074 return 0;
3075
3076 label_invalid_coding_system:
3077 coding->type = coding_type_no_conversion;
d46c5b12 3078 coding->category_idx = CODING_CATEGORY_IDX_BINARY;
c952af22 3079 coding->common_flags = 0;
dec137e5 3080 coding->eol_type = CODING_EOL_LF;
d46c5b12 3081 coding->pre_write_conversion = coding->post_read_conversion = Qnil;
4ed46869
KH
3082 return -1;
3083}
3084
3085/* Emacs has a mechanism to automatically detect a coding system if it
3086 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
3087 it's impossible to distinguish some coding systems accurately
3088 because they use the same range of codes. So, at first, coding
3089 systems are categorized into 7, those are:
3090
0ef69138 3091 o coding-category-emacs-mule
4ed46869
KH
3092
3093 The category for a coding system which has the same code range
3094 as Emacs' internal format. Assigned the coding-system (Lisp
0ef69138 3095 symbol) `emacs-mule' by default.
4ed46869
KH
3096
3097 o coding-category-sjis
3098
3099 The category for a coding system which has the same code range
3100 as SJIS. Assigned the coding-system (Lisp
7717c392 3101 symbol) `japanese-shift-jis' by default.
4ed46869
KH
3102
3103 o coding-category-iso-7
3104
3105 The category for a coding system which has the same code range
7717c392 3106 as ISO2022 of 7-bit environment. This doesn't use any locking
d46c5b12
KH
3107 shift and single shift functions. This can encode/decode all
3108 charsets. Assigned the coding-system (Lisp symbol)
3109 `iso-2022-7bit' by default.
3110
3111 o coding-category-iso-7-tight
3112
3113 Same as coding-category-iso-7 except that this can
3114 encode/decode only the specified charsets.
4ed46869
KH
3115
3116 o coding-category-iso-8-1
3117
3118 The category for a coding system which has the same code range
3119 as ISO2022 of 8-bit environment and graphic plane 1 used only
7717c392
KH
3120 for DIMENSION1 charset. This doesn't use any locking shift
3121 and single shift functions. Assigned the coding-system (Lisp
3122 symbol) `iso-latin-1' by default.
4ed46869
KH
3123
3124 o coding-category-iso-8-2
3125
3126 The category for a coding system which has the same code range
3127 as ISO2022 of 8-bit environment and graphic plane 1 used only
7717c392
KH
3128 for DIMENSION2 charset. This doesn't use any locking shift
3129 and single shift functions. Assigned the coding-system (Lisp
3130 symbol) `japanese-iso-8bit' by default.
4ed46869 3131
7717c392 3132 o coding-category-iso-7-else
4ed46869
KH
3133
3134 The category for a coding system which has the same code range
7717c392
KH
3135 as ISO2022 of 7-bit environemnt but uses locking shift or
3136 single shift functions. Assigned the coding-system (Lisp
3137 symbol) `iso-2022-7bit-lock' by default.
3138
3139 o coding-category-iso-8-else
3140
3141 The category for a coding system which has the same code range
3142 as ISO2022 of 8-bit environemnt but uses locking shift or
3143 single shift functions. Assigned the coding-system (Lisp
3144 symbol) `iso-2022-8bit-ss2' by default.
4ed46869
KH
3145
3146 o coding-category-big5
3147
3148 The category for a coding system which has the same code range
3149 as BIG5. Assigned the coding-system (Lisp symbol)
e0e989f6 3150 `cn-big5' by default.
4ed46869
KH
3151
3152 o coding-category-binary
3153
3154 The category for a coding system not categorized in any of the
3155 above. Assigned the coding-system (Lisp symbol)
e0e989f6 3156 `no-conversion' by default.
4ed46869
KH
3157
3158 Each of them is a Lisp symbol and the value is an actual
3159 `coding-system's (this is also a Lisp symbol) assigned by a user.
3160 What Emacs does actually is to detect a category of coding system.
3161 Then, it uses a `coding-system' assigned to it. If Emacs can't
3162 decide only one possible category, it selects a category of the
3163 highest priority. Priorities of categories are also specified by a
3164 user in a Lisp variable `coding-category-list'.
3165
3166*/
3167
d46c5b12 3168/* Detect how a text of length SRC_BYTES pointed by SOURCE is encoded.
4ed46869
KH
3169 If it detects possible coding systems, return an integer in which
3170 appropriate flag bits are set. Flag bits are defined by macros
d46c5b12 3171 CODING_CATEGORY_MASK_XXX in `coding.h'.
4ed46869 3172
d46c5b12
KH
3173 How many ASCII characters are at the head is returned as *SKIP. */
3174
3175static int
3176detect_coding_mask (source, src_bytes, priorities, skip)
3177 unsigned char *source;
3178 int src_bytes, *priorities, *skip;
4ed46869
KH
3179{
3180 register unsigned char c;
d46c5b12
KH
3181 unsigned char *src = source, *src_end = source + src_bytes;
3182 unsigned int mask = (CODING_CATEGORY_MASK_ISO_7BIT
3183 | CODING_CATEGORY_MASK_ISO_SHIFT);
3184 int i;
4ed46869
KH
3185
3186 /* At first, skip all ASCII characters and control characters except
3187 for three ISO2022 specific control characters. */
bcf26d6a 3188 label_loop_detect_coding:
4ed46869
KH
3189 while (src < src_end)
3190 {
3191 c = *src;
3192 if (c >= 0x80
d46c5b12
KH
3193 || ((mask & CODING_CATEGORY_MASK_ISO_7BIT)
3194 && c == ISO_CODE_ESC)
3195 || ((mask & CODING_CATEGORY_MASK_ISO_SHIFT)
3196 && (c == ISO_CODE_SI || c == ISO_CODE_SO)))
4ed46869
KH
3197 break;
3198 src++;
3199 }
d46c5b12 3200 *skip = src - source;
4ed46869
KH
3201
3202 if (src >= src_end)
3203 /* We found nothing other than ASCII. There's nothing to do. */
d46c5b12 3204 return 0;
4ed46869
KH
3205
3206 /* The text seems to be encoded in some multilingual coding system.
3207 Now, try to find in which coding system the text is encoded. */
3208 if (c < 0x80)
bcf26d6a
KH
3209 {
3210 /* i.e. (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) */
3211 /* C is an ISO2022 specific control code of C0. */
3212 mask = detect_coding_iso2022 (src, src_end);
1b2af4b0 3213 if (mask == 0)
d46c5b12
KH
3214 {
3215 /* No valid ISO2022 code follows C. Try again. */
3216 src++;
3217 mask = (c != ISO_CODE_ESC
3218 ? CODING_CATEGORY_MASK_ISO_7BIT
3219 : CODING_CATEGORY_MASK_ISO_SHIFT);
3220 goto label_loop_detect_coding;
3221 }
3222 if (priorities)
3223 goto label_return_highest_only;
bcf26d6a 3224 }
d46c5b12 3225 else
c4825358 3226 {
d46c5b12 3227 int try;
4ed46869 3228
d46c5b12
KH
3229 if (c < 0xA0)
3230 {
3231 /* C is the first byte of SJIS character code,
3232 or a leading-code of Emacs' internal format (emacs-mule). */
3233 try = CODING_CATEGORY_MASK_SJIS | CODING_CATEGORY_MASK_EMACS_MULE;
3234
3235 /* Or, if C is a special latin extra code,
3236 or is an ISO2022 specific control code of C1 (SS2 or SS3),
3237 or is an ISO2022 control-sequence-introducer (CSI),
3238 we should also consider the possibility of ISO2022 codings. */
3239 if ((VECTORP (Vlatin_extra_code_table)
3240 && !NILP (XVECTOR (Vlatin_extra_code_table)->contents[c]))
3241 || (c == ISO_CODE_SS2 || c == ISO_CODE_SS3)
3242 || (c == ISO_CODE_CSI
3243 && (src < src_end
3244 && (*src == ']'
3245 || ((*src == '0' || *src == '1' || *src == '2')
3246 && src + 1 < src_end
3247 && src[1] == ']')))))
3248 try |= (CODING_CATEGORY_MASK_ISO_8_ELSE
3249 | CODING_CATEGORY_MASK_ISO_8BIT);
3250 }
c4825358 3251 else
d46c5b12
KH
3252 /* C is a character of ISO2022 in graphic plane right,
3253 or a SJIS's 1-byte character code (i.e. JISX0201),
3254 or the first byte of BIG5's 2-byte code. */
3255 try = (CODING_CATEGORY_MASK_ISO_8_ELSE
3256 | CODING_CATEGORY_MASK_ISO_8BIT
3257 | CODING_CATEGORY_MASK_SJIS
3258 | CODING_CATEGORY_MASK_BIG5);
3259
3260 mask = 0;
3261 if (priorities)
3262 {
3263 for (i = 0; i < CODING_CATEGORY_IDX_MAX; i++)
3264 {
3265 priorities[i] &= try;
3266 if (priorities[i] & CODING_CATEGORY_MASK_ISO)
3267 mask = detect_coding_iso2022 (src, src_end);
3268 else if (priorities[i] & CODING_CATEGORY_MASK_SJIS)
3269 mask = detect_coding_sjis (src, src_end);
3270 else if (priorities[i] & CODING_CATEGORY_MASK_BIG5)
3271 mask = detect_coding_big5 (src, src_end);
3272 else if (priorities[i] & CODING_CATEGORY_MASK_EMACS_MULE)
3273 mask = detect_coding_emacs_mule (src, src_end);
3274 if (mask)
3275 goto label_return_highest_only;
3276 }
3277 return CODING_CATEGORY_MASK_RAW_TEXT;
3278 }
3279 if (try & CODING_CATEGORY_MASK_ISO)
3280 mask |= detect_coding_iso2022 (src, src_end);
3281 if (try & CODING_CATEGORY_MASK_SJIS)
3282 mask |= detect_coding_sjis (src, src_end);
3283 if (try & CODING_CATEGORY_MASK_BIG5)
3284 mask |= detect_coding_big5 (src, src_end);
3285 if (try & CODING_CATEGORY_MASK_EMACS_MULE)
3286 mask |= detect_coding_emacs_mule (src, src_end);
c4825358 3287 }
d46c5b12
KH
3288 return (mask | CODING_CATEGORY_MASK_RAW_TEXT);
3289
3290 label_return_highest_only:
3291 for (i = 0; i < CODING_CATEGORY_IDX_MAX; i++)
3292 {
3293 if (mask & priorities[i])
3294 return priorities[i];
3295 }
3296 return CODING_CATEGORY_MASK_RAW_TEXT;
4ed46869
KH
3297}
3298
3299/* Detect how a text of length SRC_BYTES pointed by SRC is encoded.
3300 The information of the detected coding system is set in CODING. */
3301
3302void
3303detect_coding (coding, src, src_bytes)
3304 struct coding_system *coding;
3305 unsigned char *src;
3306 int src_bytes;
3307{
d46c5b12
KH
3308 unsigned int idx;
3309 int skip, mask, i;
3310 int priorities[CODING_CATEGORY_IDX_MAX];
27901516 3311 Lisp_Object val = Vcoding_category_list;
4ed46869 3312
d46c5b12
KH
3313 i = 0;
3314 while (CONSP (val) && i < CODING_CATEGORY_IDX_MAX)
3315 {
3316 if (! SYMBOLP (XCONS (val)->car))
3317 break;
3318 idx = XFASTINT (Fget (XCONS (val)->car, Qcoding_category_index));
3319 if (idx >= CODING_CATEGORY_IDX_MAX)
3320 break;
3321 priorities[i++] = (1 << idx);
3322 val = XCONS (val)->cdr;
3323 }
3324 /* If coding-category-list is valid and contains all coding
3325 categories, `i' should be CODING_CATEGORY_IDX_MAX now. If not,
3326 the following code saves Emacs from craching. */
3327 while (i < CODING_CATEGORY_IDX_MAX)
3328 priorities[i++] = CODING_CATEGORY_MASK_RAW_TEXT;
4ed46869 3329
d46c5b12
KH
3330 mask = detect_coding_mask (src, src_bytes, priorities, &skip);
3331 coding->heading_ascii = skip;
4ed46869 3332
d46c5b12
KH
3333 if (!mask) return;
3334
3335 /* We found a single coding system of the highest priority in MASK. */
3336 idx = 0;
3337 while (mask && ! (mask & 1)) mask >>= 1, idx++;
3338 if (! mask)
3339 idx = CODING_CATEGORY_IDX_RAW_TEXT;
4ed46869 3340
d46c5b12
KH
3341 val = XSYMBOL (XVECTOR (Vcoding_category_table)->contents[idx])->value;
3342
3343 if (coding->eol_type != CODING_EOL_UNDECIDED)
27901516 3344 {
d46c5b12
KH
3345 Lisp_Object tmp = Fget (val, Qeol_type);
3346
3347 if (VECTORP (tmp))
3348 val = XVECTOR (tmp)->contents[coding->eol_type];
4ed46869 3349 }
d46c5b12
KH
3350 setup_coding_system (val, coding);
3351 /* Set this again because setup_coding_system reset this member. */
3352 coding->heading_ascii = skip;
4ed46869
KH
3353}
3354
d46c5b12
KH
3355/* Detect how end-of-line of a text of length SRC_BYTES pointed by
3356 SOURCE is encoded. Return one of CODING_EOL_LF, CODING_EOL_CRLF,
3357 CODING_EOL_CR, and CODING_EOL_UNDECIDED.
3358
3359 How many non-eol characters are at the head is returned as *SKIP. */
4ed46869 3360
bc4bc72a
RS
3361#define MAX_EOL_CHECK_COUNT 3
3362
d46c5b12
KH
3363static int
3364detect_eol_type (source, src_bytes, skip)
3365 unsigned char *source;
3366 int src_bytes, *skip;
4ed46869 3367{
d46c5b12 3368 unsigned char *src = source, *src_end = src + src_bytes;
4ed46869 3369 unsigned char c;
bc4bc72a
RS
3370 int total = 0; /* How many end-of-lines are found so far. */
3371 int eol_type = CODING_EOL_UNDECIDED;
3372 int this_eol_type;
4ed46869 3373
d46c5b12
KH
3374 *skip = 0;
3375
bc4bc72a 3376 while (src < src_end && total < MAX_EOL_CHECK_COUNT)
4ed46869
KH
3377 {
3378 c = *src++;
bc4bc72a 3379 if (c == '\n' || c == '\r')
4ed46869 3380 {
d46c5b12
KH
3381 if (*skip == 0)
3382 *skip = src - 1 - source;
bc4bc72a
RS
3383 total++;
3384 if (c == '\n')
3385 this_eol_type = CODING_EOL_LF;
3386 else if (src >= src_end || *src != '\n')
3387 this_eol_type = CODING_EOL_CR;
4ed46869 3388 else
bc4bc72a
RS
3389 this_eol_type = CODING_EOL_CRLF, src++;
3390
3391 if (eol_type == CODING_EOL_UNDECIDED)
3392 /* This is the first end-of-line. */
3393 eol_type = this_eol_type;
3394 else if (eol_type != this_eol_type)
d46c5b12
KH
3395 {
3396 /* The found type is different from what found before. */
3397 eol_type = CODING_EOL_INCONSISTENT;
3398 break;
3399 }
4ed46869
KH
3400 }
3401 }
bc4bc72a 3402
d46c5b12
KH
3403 if (*skip == 0)
3404 *skip = src_end - source;
85a02ca4 3405 return eol_type;
4ed46869
KH
3406}
3407
3408/* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC
3409 is encoded. If it detects an appropriate format of end-of-line, it
3410 sets the information in *CODING. */
3411
3412void
3413detect_eol (coding, src, src_bytes)
3414 struct coding_system *coding;
3415 unsigned char *src;
3416 int src_bytes;
3417{
4608c386 3418 Lisp_Object val;
d46c5b12
KH
3419 int skip;
3420 int eol_type = detect_eol_type (src, src_bytes, &skip);
3421
3422 if (coding->heading_ascii > skip)
3423 coding->heading_ascii = skip;
3424 else
3425 skip = coding->heading_ascii;
4ed46869 3426
0ef69138 3427 if (eol_type == CODING_EOL_UNDECIDED)
4ed46869 3428 return;
27901516
KH
3429 if (eol_type == CODING_EOL_INCONSISTENT)
3430 {
3431#if 0
3432 /* This code is suppressed until we find a better way to
992f23f2 3433 distinguish raw text file and binary file. */
27901516
KH
3434
3435 /* If we have already detected that the coding is raw-text, the
3436 coding should actually be no-conversion. */
3437 if (coding->type == coding_type_raw_text)
3438 {
3439 setup_coding_system (Qno_conversion, coding);
3440 return;
3441 }
3442 /* Else, let's decode only text code anyway. */
3443#endif /* 0 */
1b2af4b0 3444 eol_type = CODING_EOL_LF;
27901516
KH
3445 }
3446
4608c386 3447 val = Fget (coding->symbol, Qeol_type);
4ed46869 3448 if (VECTORP (val) && XVECTOR (val)->size == 3)
d46c5b12
KH
3449 {
3450 setup_coding_system (XVECTOR (val)->contents[eol_type], coding);
3451 coding->heading_ascii = skip;
3452 }
3453}
3454
3455#define CONVERSION_BUFFER_EXTRA_ROOM 256
3456
3457#define DECODING_BUFFER_MAG(coding) \
3458 (coding->type == coding_type_iso2022 \
3459 ? 3 \
3460 : ((coding->type == coding_type_sjis || coding->type == coding_type_big5) \
3461 ? 2 \
3462 : (coding->type == coding_type_raw_text \
3463 ? 1 \
3464 : (coding->type == coding_type_ccl \
3465 ? coding->spec.ccl.decoder.buf_magnification \
3466 : 2))))
3467
3468/* Return maximum size (bytes) of a buffer enough for decoding
3469 SRC_BYTES of text encoded in CODING. */
3470
3471int
3472decoding_buffer_size (coding, src_bytes)
3473 struct coding_system *coding;
3474 int src_bytes;
3475{
3476 return (src_bytes * DECODING_BUFFER_MAG (coding)
3477 + CONVERSION_BUFFER_EXTRA_ROOM);
3478}
3479
3480/* Return maximum size (bytes) of a buffer enough for encoding
3481 SRC_BYTES of text to CODING. */
3482
3483int
3484encoding_buffer_size (coding, src_bytes)
3485 struct coding_system *coding;
3486 int src_bytes;
3487{
3488 int magnification;
3489
3490 if (coding->type == coding_type_ccl)
3491 magnification = coding->spec.ccl.encoder.buf_magnification;
3492 else
3493 magnification = 3;
3494
3495 return (src_bytes * magnification + CONVERSION_BUFFER_EXTRA_ROOM);
3496}
3497
3498#ifndef MINIMUM_CONVERSION_BUFFER_SIZE
3499#define MINIMUM_CONVERSION_BUFFER_SIZE 1024
3500#endif
3501
3502char *conversion_buffer;
3503int conversion_buffer_size;
3504
3505/* Return a pointer to a SIZE bytes of buffer to be used for encoding
3506 or decoding. Sufficient memory is allocated automatically. If we
3507 run out of memory, return NULL. */
3508
3509char *
3510get_conversion_buffer (size)
3511 int size;
3512{
3513 if (size > conversion_buffer_size)
3514 {
3515 char *buf;
3516 int real_size = conversion_buffer_size * 2;
3517
3518 while (real_size < size) real_size *= 2;
3519 buf = (char *) xmalloc (real_size);
3520 xfree (conversion_buffer);
3521 conversion_buffer = buf;
3522 conversion_buffer_size = real_size;
3523 }
3524 return conversion_buffer;
3525}
3526
3527int
3528ccl_coding_driver (coding, source, destination, src_bytes, dst_bytes, encodep)
3529 struct coding_system *coding;
3530 unsigned char *source, *destination;
3531 int src_bytes, dst_bytes, encodep;
3532{
3533 struct ccl_program *ccl
3534 = encodep ? &coding->spec.ccl.encoder : &coding->spec.ccl.decoder;
3535 int result;
3536
3537 coding->produced = ccl_driver (ccl, source, destination,
3538 src_bytes, dst_bytes, &(coding->consumed));
3539 if (encodep)
3540 {
3541 coding->produced_char = coding->produced;
3542 coding->consumed_char
3543 = multibyte_chars_in_text (source, coding->consumed);
3544 }
3545 else
3546 {
3547 coding->produced_char
3548 = multibyte_chars_in_text (destination, coding->produced);
3549 coding->consumed_char = coding->consumed;
3550 }
3551 switch (ccl->status)
3552 {
3553 case CCL_STAT_SUSPEND_BY_SRC:
3554 result = CODING_FINISH_INSUFFICIENT_SRC;
3555 break;
3556 case CCL_STAT_SUSPEND_BY_DST:
3557 result = CODING_FINISH_INSUFFICIENT_DST;
3558 break;
3559 default:
3560 result = CODING_FINISH_NORMAL;
3561 break;
3562 }
3563 return result;
4ed46869
KH
3564}
3565
3566/* See "GENERAL NOTES about `decode_coding_XXX ()' functions". Before
3567 decoding, it may detect coding system and format of end-of-line if
3568 those are not yet decided. */
3569
3570int
d46c5b12 3571decode_coding (coding, source, destination, src_bytes, dst_bytes)
4ed46869
KH
3572 struct coding_system *coding;
3573 unsigned char *source, *destination;
3574 int src_bytes, dst_bytes;
4ed46869 3575{
d46c5b12 3576 int result;
4ed46869
KH
3577
3578 if (src_bytes <= 0)
3579 {
d46c5b12
KH
3580 coding->produced = coding->produced_char = 0;
3581 coding->consumed = coding->consumed_char = 0;
fb88bf2d 3582 coding->fake_multibyte = 0;
d46c5b12 3583 return CODING_FINISH_NORMAL;
4ed46869
KH
3584 }
3585
0ef69138 3586 if (coding->type == coding_type_undecided)
4ed46869
KH
3587 detect_coding (coding, source, src_bytes);
3588
0ef69138 3589 if (coding->eol_type == CODING_EOL_UNDECIDED)
4ed46869
KH
3590 detect_eol (coding, source, src_bytes);
3591
4ed46869
KH
3592 switch (coding->type)
3593 {
0ef69138
KH
3594 case coding_type_emacs_mule:
3595 case coding_type_undecided:
27901516 3596 case coding_type_raw_text:
4ed46869 3597 if (coding->eol_type == CODING_EOL_LF
0ef69138 3598 || coding->eol_type == CODING_EOL_UNDECIDED)
4ed46869 3599 goto label_no_conversion;
d46c5b12 3600 result = decode_eol (coding, source, destination, src_bytes, dst_bytes);
4ed46869
KH
3601 break;
3602
3603 case coding_type_sjis:
d46c5b12
KH
3604 result = decode_coding_sjis_big5 (coding, source, destination,
3605 src_bytes, dst_bytes, 1);
4ed46869
KH
3606 break;
3607
3608 case coding_type_iso2022:
d46c5b12
KH
3609 result = decode_coding_iso2022 (coding, source, destination,
3610 src_bytes, dst_bytes);
4ed46869
KH
3611 break;
3612
3613 case coding_type_big5:
d46c5b12
KH
3614 result = decode_coding_sjis_big5 (coding, source, destination,
3615 src_bytes, dst_bytes, 0);
4ed46869
KH
3616 break;
3617
3618 case coding_type_ccl:
d46c5b12
KH
3619 result = ccl_coding_driver (coding, source, destination,
3620 src_bytes, dst_bytes, 0);
3621 break;
3622
3623 default: /* i.e. case coding_type_no_conversion: */
3624 label_no_conversion:
3625 if (dst_bytes && src_bytes > dst_bytes)
3626 {
3627 coding->produced = dst_bytes;
3628 result = CODING_FINISH_INSUFFICIENT_DST;
3629 }
3630 else
3631 {
3632 coding->produced = src_bytes;
3633 result = CODING_FINISH_NORMAL;
3634 }
3635 if (dst_bytes)
3636 bcopy (source, destination, coding->produced);
3637 else
3638 safe_bcopy (source, destination, coding->produced);
fb88bf2d 3639 coding->fake_multibyte = 1;
d46c5b12
KH
3640 coding->consumed
3641 = coding->consumed_char = coding->produced_char = coding->produced;
4ed46869
KH
3642 break;
3643 }
3644
d46c5b12 3645 return result;
4ed46869
KH
3646}
3647
3648/* See "GENERAL NOTES about `encode_coding_XXX ()' functions". */
3649
3650int
d46c5b12 3651encode_coding (coding, source, destination, src_bytes, dst_bytes)
4ed46869
KH
3652 struct coding_system *coding;
3653 unsigned char *source, *destination;
3654 int src_bytes, dst_bytes;
4ed46869 3655{
d46c5b12 3656 int result;
4ed46869 3657
d46c5b12 3658 if (src_bytes <= 0)
4ed46869 3659 {
d46c5b12
KH
3660 coding->produced = coding->produced_char = 0;
3661 coding->consumed = coding->consumed_char = 0;
fb88bf2d 3662 coding->fake_multibyte = 0;
d46c5b12
KH
3663 return CODING_FINISH_NORMAL;
3664 }
4ed46869 3665
d46c5b12
KH
3666 switch (coding->type)
3667 {
0ef69138
KH
3668 case coding_type_emacs_mule:
3669 case coding_type_undecided:
27901516 3670 case coding_type_raw_text:
4ed46869 3671 if (coding->eol_type == CODING_EOL_LF
0ef69138 3672 || coding->eol_type == CODING_EOL_UNDECIDED)
4ed46869 3673 goto label_no_conversion;
d46c5b12 3674 result = encode_eol (coding, source, destination, src_bytes, dst_bytes);
4ed46869
KH
3675 break;
3676
3677 case coding_type_sjis:
d46c5b12
KH
3678 result = encode_coding_sjis_big5 (coding, source, destination,
3679 src_bytes, dst_bytes, 1);
4ed46869
KH
3680 break;
3681
3682 case coding_type_iso2022:
d46c5b12
KH
3683 result = encode_coding_iso2022 (coding, source, destination,
3684 src_bytes, dst_bytes);
4ed46869
KH
3685 break;
3686
3687 case coding_type_big5:
d46c5b12
KH
3688 result = encode_coding_sjis_big5 (coding, source, destination,
3689 src_bytes, dst_bytes, 0);
4ed46869
KH
3690 break;
3691
3692 case coding_type_ccl:
d46c5b12
KH
3693 result = ccl_coding_driver (coding, source, destination,
3694 src_bytes, dst_bytes, 1);
3695 break;
3696
3697 default: /* i.e. case coding_type_no_conversion: */
3698 label_no_conversion:
3699 if (dst_bytes && src_bytes > dst_bytes)
3700 {
3701 coding->produced = dst_bytes;
3702 result = CODING_FINISH_INSUFFICIENT_DST;
3703 }
3704 else
3705 {
3706 coding->produced = src_bytes;
3707 result = CODING_FINISH_NORMAL;
3708 }
3709 if (dst_bytes)
3710 bcopy (source, destination, coding->produced);
3711 else
3712 safe_bcopy (source, destination, coding->produced);
3713 if (coding->mode & CODING_MODE_SELECTIVE_DISPLAY)
3714 {
3715 unsigned char *p = destination, *pend = p + coding->produced;
3716 while (p < pend)
3717 if (*p++ == '\015') p[-1] = '\n';
3718 }
fb88bf2d 3719 coding->fake_multibyte = 1;
d46c5b12
KH
3720 coding->consumed
3721 = coding->consumed_char = coding->produced_char = coding->produced;
4ed46869
KH
3722 break;
3723 }
3724
d46c5b12 3725 return result;
4ed46869
KH
3726}
3727
fb88bf2d
KH
3728/* Scan text in the region between *BEG and *END (byte positions),
3729 skip characters which we don't have to decode by coding system
3730 CODING at the head and tail, then set *BEG and *END to the region
3731 of the text we actually have to convert. The caller should move
3732 the gap out of the region in advance.
4ed46869 3733
d46c5b12
KH
3734 If STR is not NULL, *BEG and *END are indices into STR. */
3735
3736static void
3737shrink_decoding_region (beg, end, coding, str)
3738 int *beg, *end;
3739 struct coding_system *coding;
3740 unsigned char *str;
3741{
fb88bf2d 3742 unsigned char *begp_orig, *begp, *endp_orig, *endp, c;
d46c5b12
KH
3743 int eol_conversion;
3744
3745 if (coding->type == coding_type_ccl
3746 || coding->type == coding_type_undecided
3747 || !NILP (coding->post_read_conversion))
3748 {
3749 /* We can't skip any data. */
3750 return;
3751 }
3752 else if (coding->type == coding_type_no_conversion)
3753 {
fb88bf2d
KH
3754 /* We need no conversion, but don't have to skip any data here.
3755 Decoding routine handles them effectively anyway. */
d46c5b12
KH
3756 return;
3757 }
3758
3759 if (coding->heading_ascii >= 0)
3760 /* Detection routine has already found how much we can skip at the
3761 head. */
3762 *beg += coding->heading_ascii;
3763
3764 if (str)
3765 {
3766 begp_orig = begp = str + *beg;
3767 endp_orig = endp = str + *end;
3768 }
3769 else
3770 {
fb88bf2d 3771 begp_orig = begp = BYTE_POS_ADDR (*beg);
d46c5b12
KH
3772 endp_orig = endp = begp + *end - *beg;
3773 }
3774
3775 eol_conversion = (coding->eol_type != CODING_EOL_LF);
3776
3777 switch (coding->type)
3778 {
3779 case coding_type_emacs_mule:
3780 case coding_type_raw_text:
3781 if (eol_conversion)
3782 {
3783 if (coding->heading_ascii < 0)
fb88bf2d 3784 while (begp < endp && *begp != '\r' && *begp < 0x80) begp++;
ee59c65f 3785 while (begp < endp && endp[-1] != '\r' && endp[-1] < 0x80)
fb88bf2d 3786 endp--;
ee59c65f
RS
3787 /* Do not consider LF as ascii if preceded by CR, since that
3788 confuses eol decoding. */
3789 if (begp < endp && endp < endp_orig && endp[-1] == '\r' && endp[0] == '\n')
3790 endp++;
d46c5b12
KH
3791 }
3792 else
3793 begp = endp;
3794 break;
3795
3796 case coding_type_sjis:
3797 case coding_type_big5:
3798 /* We can skip all ASCII characters at the head. */
3799 if (coding->heading_ascii < 0)
3800 {
3801 if (eol_conversion)
de9d083c 3802 while (begp < endp && *begp < 0x80 && *begp != '\r') begp++;
d46c5b12
KH
3803 else
3804 while (begp < endp && *begp < 0x80) begp++;
3805 }
3806 /* We can skip all ASCII characters at the tail except for the
3807 second byte of SJIS or BIG5 code. */
3808 if (eol_conversion)
de9d083c 3809 while (begp < endp && endp[-1] < 0x80 && endp[-1] != '\r') endp--;
d46c5b12
KH
3810 else
3811 while (begp < endp && endp[-1] < 0x80) endp--;
ee59c65f
RS
3812 /* Do not consider LF as ascii if preceded by CR, since that
3813 confuses eol decoding. */
3814 if (begp < endp && endp < endp_orig && endp[-1] == '\r' && endp[0] == '\n')
3815 endp++;
d46c5b12
KH
3816 if (begp < endp && endp < endp_orig && endp[-1] >= 0x80)
3817 endp++;
3818 break;
3819
3820 default: /* i.e. case coding_type_iso2022: */
3821 if (coding->heading_ascii < 0)
3822 {
d46c5b12
KH
3823 /* We can skip all ASCII characters at the head except for a
3824 few control codes. */
3825 while (begp < endp && (c = *begp) < 0x80
3826 && c != ISO_CODE_CR && c != ISO_CODE_SO
3827 && c != ISO_CODE_SI && c != ISO_CODE_ESC
3828 && (!eol_conversion || c != ISO_CODE_LF))
3829 begp++;
3830 }
3831 switch (coding->category_idx)
3832 {
3833 case CODING_CATEGORY_IDX_ISO_8_1:
3834 case CODING_CATEGORY_IDX_ISO_8_2:
3835 /* We can skip all ASCII characters at the tail. */
3836 if (eol_conversion)
de9d083c 3837 while (begp < endp && (c = endp[-1]) < 0x80 && c != '\r') endp--;
d46c5b12
KH
3838 else
3839 while (begp < endp && endp[-1] < 0x80) endp--;
ee59c65f
RS
3840 /* Do not consider LF as ascii if preceded by CR, since that
3841 confuses eol decoding. */
3842 if (begp < endp && endp < endp_orig && endp[-1] == '\r' && endp[0] == '\n')
3843 endp++;
d46c5b12
KH
3844 break;
3845
3846 case CODING_CATEGORY_IDX_ISO_7:
3847 case CODING_CATEGORY_IDX_ISO_7_TIGHT:
3848 /* We can skip all charactes at the tail except for ESC and
3849 the following 2-byte at the tail. */
3850 if (eol_conversion)
fb88bf2d 3851 while (begp < endp
de9d083c 3852 && (c = endp[-1]) < 0x80 && c != ISO_CODE_ESC && c != '\r')
d46c5b12
KH
3853 endp--;
3854 else
fb88bf2d
KH
3855 while (begp < endp
3856 && (c = endp[-1]) < 0x80 && c != ISO_CODE_ESC)
d46c5b12 3857 endp--;
ee59c65f
RS
3858 /* Do not consider LF as ascii if preceded by CR, since that
3859 confuses eol decoding. */
3860 if (begp < endp && endp < endp_orig && endp[-1] == '\r' && endp[0] == '\n')
3861 endp++;
d46c5b12
KH
3862 if (begp < endp && endp[-1] == ISO_CODE_ESC)
3863 {
3864 if (endp + 1 < endp_orig && end[0] == '(' && end[1] == 'B')
3865 /* This is an ASCII designation sequence. We can
3866 surely skip the tail. */
3867 endp += 2;
3868 else
3869 /* Hmmm, we can't skip the tail. */
3870 endp = endp_orig;
3871 }
3872 }
3873 }
3874 *beg += begp - begp_orig;
3875 *end += endp - endp_orig;
3876 return;
3877}
3878
3879/* Like shrink_decoding_region but for encoding. */
3880
3881static void
3882shrink_encoding_region (beg, end, coding, str)
3883 int *beg, *end;
3884 struct coding_system *coding;
3885 unsigned char *str;
3886{
3887 unsigned char *begp_orig, *begp, *endp_orig, *endp;
3888 int eol_conversion;
3889
3890 if (coding->type == coding_type_ccl)
3891 /* We can't skip any data. */
3892 return;
3893 else if (coding->type == coding_type_no_conversion)
3894 {
3895 /* We need no conversion. */
3896 *beg = *end;
3897 return;
3898 }
3899
3900 if (str)
3901 {
3902 begp_orig = begp = str + *beg;
3903 endp_orig = endp = str + *end;
3904 }
3905 else
3906 {
fb88bf2d 3907 begp_orig = begp = BYTE_POS_ADDR (*beg);
d46c5b12
KH
3908 endp_orig = endp = begp + *end - *beg;
3909 }
3910
3911 eol_conversion = (coding->eol_type == CODING_EOL_CR
3912 || coding->eol_type == CODING_EOL_CRLF);
3913
3914 /* Here, we don't have to check coding->pre_write_conversion because
3915 the caller is expected to have handled it already. */
3916 switch (coding->type)
3917 {
3918 case coding_type_undecided:
3919 case coding_type_emacs_mule:
3920 case coding_type_raw_text:
3921 if (eol_conversion)
3922 {
3923 while (begp < endp && *begp != '\n') begp++;
3924 while (begp < endp && endp[-1] != '\n') endp--;
3925 }
3926 else
3927 begp = endp;
3928 break;
3929
3930 case coding_type_iso2022:
3931 if (coding->flags & CODING_FLAG_ISO_DESIGNATE_AT_BOL)
3932 {
3933 unsigned char *bol = begp;
3934 while (begp < endp && *begp < 0x80)
3935 {
3936 begp++;
3937 if (begp[-1] == '\n')
3938 bol = begp;
3939 }
3940 begp = bol;
3941 goto label_skip_tail;
3942 }
3943 /* fall down ... */
3944
3945 default:
3946 /* We can skip all ASCII characters at the head and tail. */
3947 if (eol_conversion)
3948 while (begp < endp && *begp < 0x80 && *begp != '\n') begp++;
3949 else
3950 while (begp < endp && *begp < 0x80) begp++;
3951 label_skip_tail:
3952 if (eol_conversion)
3953 while (begp < endp && endp[-1] < 0x80 && endp[-1] != '\n') endp--;
3954 else
3955 while (begp < endp && *(endp - 1) < 0x80) endp--;
3956 break;
3957 }
3958
3959 *beg += begp - begp_orig;
3960 *end += endp - endp_orig;
3961 return;
3962}
3963
3964/* Decode (if ENCODEP is zero) or encode (if ENCODEP is nonzero) the
fb88bf2d
KH
3965 text from FROM to TO (byte positions are FROM_BYTE and TO_BYTE) by
3966 coding system CODING, and return the status code of code conversion
3967 (currently, this value has no meaning).
3968
3969 How many characters (and bytes) are converted to how many
3970 characters (and bytes) are recorded in members of the structure
3971 CODING.
d46c5b12 3972
6e44253b 3973 If REPLACE is nonzero, we do various things as if the original text
d46c5b12 3974 is deleted and a new text is inserted. See the comments in
6e44253b 3975 replace_range (insdel.c) to know what we are doing. */
4ed46869
KH
3976
3977int
6e44253b
KH
3978code_convert_region (from, from_byte, to, to_byte, coding, encodep, replace)
3979 int from, from_byte, to, to_byte, encodep, replace;
4ed46869 3980 struct coding_system *coding;
4ed46869 3981{
fb88bf2d
KH
3982 int len = to - from, len_byte = to_byte - from_byte;
3983 int require, inserted, inserted_byte;
12410ef1 3984 int head_skip, tail_skip, total_skip;
d46c5b12 3985 Lisp_Object saved_coding_symbol = Qnil;
fb88bf2d
KH
3986 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
3987 int first = 1;
3988 int fake_multibyte = 0;
3989 unsigned char *src, *dst;
12410ef1 3990 Lisp_Object deletion = Qnil;
d46c5b12 3991
83fa074f
KH
3992 if (from < PT && PT < to)
3993 SET_PT_BOTH (from, from_byte);
3994
6e44253b 3995 if (replace)
d46c5b12 3996 {
fb88bf2d
KH
3997 int saved_from = from;
3998
d46c5b12 3999 prepare_to_modify_buffer (from, to, &from);
fb88bf2d
KH
4000 if (saved_from != from)
4001 {
4002 to = from + len;
4003 if (multibyte)
4004 from_byte = CHAR_TO_BYTE (from), to_byte = CHAR_TO_BYTE (to);
4005 else
4006 from_byte = from, to_byte = to;
4007 len_byte = to_byte - from_byte;
4008 }
d46c5b12 4009 }
d46c5b12
KH
4010
4011 if (! encodep && CODING_REQUIRE_DETECTION (coding))
4012 {
12410ef1 4013 /* We must detect encoding of text and eol format. */
d46c5b12
KH
4014
4015 if (from < GPT && to > GPT)
4016 move_gap_both (from, from_byte);
4017 if (coding->type == coding_type_undecided)
4018 {
fb88bf2d 4019 detect_coding (coding, BYTE_POS_ADDR (from_byte), len_byte);
d46c5b12 4020 if (coding->type == coding_type_undecided)
12410ef1
KH
4021 /* It seems that the text contains only ASCII, but we
4022 should not left it undecided because the deeper
4023 decoding routine (decode_coding) tries to detect the
4024 encodings again in vain. */
d46c5b12
KH
4025 coding->type = coding_type_emacs_mule;
4026 }
4027 if (coding->eol_type == CODING_EOL_UNDECIDED)
4028 {
4029 saved_coding_symbol = coding->symbol;
4030 detect_eol (coding, BYTE_POS_ADDR (from_byte), len_byte);
4031 if (coding->eol_type == CODING_EOL_UNDECIDED)
4032 coding->eol_type = CODING_EOL_LF;
4033 /* We had better recover the original eol format if we
4034 encounter an inconsitent eol format while decoding. */
4035 coding->mode |= CODING_MODE_INHIBIT_INCONSISTENT_EOL;
4036 }
4037 }
4038
fb88bf2d
KH
4039 coding->consumed_char = len, coding->consumed = len_byte;
4040
d46c5b12
KH
4041 if (encodep
4042 ? ! CODING_REQUIRE_ENCODING (coding)
4043 : ! CODING_REQUIRE_DECODING (coding))
fb88bf2d
KH
4044 {
4045 coding->produced = len_byte;
12410ef1
KH
4046 if (multibyte
4047 && ! replace
4048 /* See the comment of the member heading_ascii in coding.h. */
4049 && coding->heading_ascii < len_byte)
fb88bf2d 4050 {
6e44253b
KH
4051 /* We still may have to combine byte at the head and the
4052 tail of the text in the region. */
12410ef1 4053 if (from < GPT && GPT < to)
6e44253b 4054 move_gap_both (to, to_byte);
12410ef1
KH
4055 len = multibyte_chars_in_text (BYTE_POS_ADDR (from_byte), len_byte);
4056 adjust_after_insert (from, from_byte, to, to_byte, len);
4057 coding->produced_char = len;
fb88bf2d
KH
4058 }
4059 else
68e3a8f1
AS
4060 {
4061 if (!replace)
4062 adjust_after_insert (from, from_byte, to, to_byte, len_byte);
4063 coding->produced_char = len_byte;
4064 }
fb88bf2d
KH
4065 return 0;
4066 }
d46c5b12
KH
4067
4068 /* Now we convert the text. */
4069
4070 /* For encoding, we must process pre-write-conversion in advance. */
4071 if (encodep
d46c5b12
KH
4072 && ! NILP (coding->pre_write_conversion)
4073 && SYMBOLP (coding->pre_write_conversion)
4074 && ! NILP (Ffboundp (coding->pre_write_conversion)))
4075 {
2b4f9037
KH
4076 /* The function in pre-write-conversion may put a new text in a
4077 new buffer. */
d46c5b12
KH
4078 struct buffer *prev = current_buffer, *new;
4079
b39f748c
AS
4080 call2 (coding->pre_write_conversion,
4081 make_number (from), make_number (to));
d46c5b12
KH
4082 if (current_buffer != prev)
4083 {
4084 len = ZV - BEGV;
4085 new = current_buffer;
4086 set_buffer_internal_1 (prev);
ddbc19ff 4087 del_range_2 (from, from_byte, to, to_byte);
d46c5b12
KH
4088 insert_from_buffer (new, BEG, len, 0);
4089 to = from + len;
fb88bf2d 4090 to_byte = multibyte ? CHAR_TO_BYTE (to) : to;
d46c5b12
KH
4091 len_byte = to_byte - from_byte;
4092 }
4093 }
4094
12410ef1
KH
4095 if (replace)
4096 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
4097
d46c5b12 4098 /* Try to skip the heading and tailing ASCIIs. */
12410ef1
KH
4099 {
4100 int from_byte_orig = from_byte, to_byte_orig = to_byte;
4101
4102 if (from < GPT && GPT < to)
4103 move_gap_both (from, from_byte);
4104 if (encodep)
4105 shrink_encoding_region (&from_byte, &to_byte, coding, NULL);
4106 else
4107 shrink_decoding_region (&from_byte, &to_byte, coding, NULL);
4108 if (from_byte == to_byte)
4109 {
4110 coding->produced = len_byte;
4111 coding->produced_char = multibyte ? len : len_byte;
4112 if (!replace)
4113 /* We must record and adjust for this new text now. */
4114 adjust_after_insert (from, from_byte_orig, to, to_byte_orig, len);
4115 return 0;
4116 }
fb88bf2d 4117
12410ef1
KH
4118 head_skip = from_byte - from_byte_orig;
4119 tail_skip = to_byte_orig - to_byte;
4120 total_skip = head_skip + tail_skip;
4121 from += head_skip;
4122 to -= tail_skip;
4123 len -= total_skip; len_byte -= total_skip;
4124 }
d46c5b12 4125
fb88bf2d
KH
4126 /* For converion, we must put the gap before the text in addition to
4127 making the gap larger for efficient decoding. The required gap
4128 size starts from 2000 which is the magic number used in make_gap.
4129 But, after one batch of conversion, it will be incremented if we
4130 find that it is not enough . */
d46c5b12
KH
4131 require = 2000;
4132
4133 if (GAP_SIZE < require)
4134 make_gap (require - GAP_SIZE);
4135 move_gap_both (from, from_byte);
4136
d46c5b12
KH
4137 if (GPT - BEG < beg_unchanged)
4138 beg_unchanged = GPT - BEG;
4139 if (Z - GPT < end_unchanged)
4140 end_unchanged = Z - GPT;
4141
4142 inserted = inserted_byte = 0;
fb88bf2d
KH
4143 src = GAP_END_ADDR, dst = GPT_ADDR;
4144
4145 GAP_SIZE += len_byte;
4146 ZV -= len;
4147 Z -= len;
4148 ZV_BYTE -= len_byte;
4149 Z_BYTE -= len_byte;
4150
d46c5b12
KH
4151 for (;;)
4152 {
fb88bf2d 4153 int result;
d46c5b12
KH
4154
4155 /* The buffer memory is changed from:
fb88bf2d
KH
4156 +--------+converted-text+---------+-------original-text------+---+
4157 |<-from->|<--inserted-->|---------|<-----------len---------->|---|
4158 |<------------------- GAP_SIZE -------------------->| */
d46c5b12 4159 if (encodep)
fb88bf2d 4160 result = encode_coding (coding, src, dst, len_byte, 0);
d46c5b12 4161 else
fb88bf2d 4162 result = decode_coding (coding, src, dst, len_byte, 0);
d46c5b12
KH
4163 /* to:
4164 +--------+-------converted-text--------+--+---original-text--+---+
fb88bf2d
KH
4165 |<-from->|<--inserted-->|<--produced-->|--|<-(len-consumed)->|---|
4166 |<------------------- GAP_SIZE -------------------->| */
4167 if (coding->fake_multibyte)
4168 fake_multibyte = 1;
d46c5b12 4169
fb88bf2d
KH
4170 if (!encodep && !multibyte)
4171 coding->produced_char = coding->produced;
d46c5b12
KH
4172 inserted += coding->produced_char;
4173 inserted_byte += coding->produced;
d46c5b12 4174 len_byte -= coding->consumed;
fb88bf2d
KH
4175 src += coding->consumed;
4176 dst += inserted_byte;
d46c5b12
KH
4177
4178 if (! encodep && result == CODING_FINISH_INCONSISTENT_EOL)
4179 {
fb88bf2d 4180 unsigned char *pend = dst, *p = pend - inserted_byte;
d46c5b12
KH
4181
4182 /* Encode LFs back to the original eol format (CR or CRLF). */
4183 if (coding->eol_type == CODING_EOL_CR)
4184 {
4185 while (p < pend) if (*p++ == '\n') p[-1] = '\r';
4186 }
4187 else
4188 {
d46c5b12
KH
4189 int count = 0;
4190
fb88bf2d
KH
4191 while (p < pend) if (*p++ == '\n') count++;
4192 if (src - dst < count)
d46c5b12 4193 {
fb88bf2d
KH
4194 /* We don't have sufficient room for putting LFs
4195 back to CRLF. We must record converted and
4196 not-yet-converted text back to the buffer
4197 content, enlarge the gap, then record them out of
4198 the buffer contents again. */
4199 int add = len_byte + inserted_byte;
4200
4201 GAP_SIZE -= add;
4202 ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add;
4203 GPT += inserted_byte; GPT_BYTE += inserted_byte;
4204 make_gap (count - GAP_SIZE);
4205 GAP_SIZE += add;
4206 ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add;
4207 GPT -= inserted_byte; GPT_BYTE -= inserted_byte;
4208 /* Don't forget to update SRC, DST, and PEND. */
4209 src = GAP_END_ADDR - len_byte;
4210 dst = GPT_ADDR + inserted_byte;
4211 pend = dst;
d46c5b12 4212 }
d46c5b12
KH
4213 inserted += count;
4214 inserted_byte += count;
fb88bf2d
KH
4215 coding->produced += count;
4216 p = dst = pend + count;
4217 while (count)
4218 {
4219 *--p = *--pend;
4220 if (*p == '\n') count--, *--p = '\r';
4221 }
d46c5b12
KH
4222 }
4223
4224 /* Suppress eol-format conversion in the further conversion. */
4225 coding->eol_type = CODING_EOL_LF;
4226
4227 /* Restore the original symbol. */
4228 coding->symbol = saved_coding_symbol;
fb88bf2d
KH
4229
4230 continue;
d46c5b12
KH
4231 }
4232 if (len_byte <= 0)
4233 break;
4234 if (result == CODING_FINISH_INSUFFICIENT_SRC)
4235 {
4236 /* The source text ends in invalid codes. Let's just
4237 make them valid buffer contents, and finish conversion. */
fb88bf2d 4238 inserted += len_byte;
d46c5b12 4239 inserted_byte += len_byte;
fb88bf2d 4240 while (len_byte--)
ee59c65f 4241 *dst++ = *src++;
fb88bf2d 4242 fake_multibyte = 1;
d46c5b12
KH
4243 break;
4244 }
fb88bf2d
KH
4245 if (first)
4246 {
4247 /* We have just done the first batch of conversion which was
4248 stoped because of insufficient gap. Let's reconsider the
4249 required gap size (i.e. SRT - DST) now.
4250
4251 We have converted ORIG bytes (== coding->consumed) into
4252 NEW bytes (coding->produced). To convert the remaining
4253 LEN bytes, we may need REQUIRE bytes of gap, where:
4254 REQUIRE + LEN_BYTE = LEN_BYTE * (NEW / ORIG)
4255 REQUIRE = LEN_BYTE * (NEW - ORIG) / ORIG
4256 Here, we are sure that NEW >= ORIG. */
6e44253b
KH
4257 float ratio = coding->produced - coding->consumed;
4258 ratio /= coding->consumed;
4259 require = len_byte * ratio;
fb88bf2d
KH
4260 first = 0;
4261 }
4262 if ((src - dst) < (require + 2000))
4263 {
4264 /* See the comment above the previous call of make_gap. */
4265 int add = len_byte + inserted_byte;
4266
4267 GAP_SIZE -= add;
4268 ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add;
4269 GPT += inserted_byte; GPT_BYTE += inserted_byte;
4270 make_gap (require + 2000);
4271 GAP_SIZE += add;
4272 ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add;
4273 GPT -= inserted_byte; GPT_BYTE -= inserted_byte;
4274 /* Don't forget to update SRC, DST. */
4275 src = GAP_END_ADDR - len_byte;
4276 dst = GPT_ADDR + inserted_byte;
4277 }
d46c5b12 4278 }
fb88bf2d
KH
4279 if (src - dst > 0) *dst = 0; /* Put an anchor. */
4280
2b4f9037 4281 if (multibyte
12410ef1
KH
4282 && (fake_multibyte
4283 || !encodep && (to - from) != (to_byte - from_byte)))
2b4f9037 4284 inserted = multibyte_chars_in_text (GPT_ADDR, inserted_byte);
7553d0e1 4285
12410ef1
KH
4286 /* If we have shrinked the conversion area, adjust it now. */
4287 if (total_skip > 0)
4288 {
4289 if (tail_skip > 0)
4290 safe_bcopy (GAP_END_ADDR, GPT_ADDR + inserted_byte, tail_skip);
4291 inserted += total_skip; inserted_byte += total_skip;
4292 GAP_SIZE += total_skip;
4293 GPT -= head_skip; GPT_BYTE -= head_skip;
4294 ZV -= total_skip; ZV_BYTE -= total_skip;
4295 Z -= total_skip; Z_BYTE -= total_skip;
4296 from -= head_skip; from_byte -= head_skip;
4297 to += tail_skip; to_byte += tail_skip;
4298 }
4299
4300 adjust_after_replace (from, from_byte, deletion, inserted, inserted_byte);
4ed46869 4301
2b4f9037 4302 if (! encodep && ! NILP (coding->post_read_conversion))
d46c5b12 4303 {
2b4f9037
KH
4304 Lisp_Object val;
4305 int orig_inserted = inserted, pos = PT;
4ed46869 4306
2b4f9037
KH
4307 if (from != pos)
4308 temp_set_point_both (current_buffer, from, from_byte);
4309 val = call1 (coding->post_read_conversion, make_number (inserted));
4310 if (! NILP (val))
d46c5b12 4311 {
2b4f9037
KH
4312 CHECK_NUMBER (val, 0);
4313 inserted = XFASTINT (val);
d46c5b12 4314 }
2b4f9037
KH
4315 if (pos >= from + orig_inserted)
4316 temp_set_point (current_buffer, pos + (inserted - orig_inserted));
d46c5b12 4317 }
4ed46869 4318
2b4f9037
KH
4319 signal_after_change (from, to - from, inserted);
4320
fb88bf2d 4321 {
12410ef1
KH
4322 coding->consumed = to_byte - from_byte;
4323 coding->consumed_char = to - from;
4324 coding->produced = inserted_byte;
4325 coding->produced_char = inserted;
fb88bf2d 4326 }
7553d0e1 4327
fb88bf2d 4328 return 0;
d46c5b12
KH
4329}
4330
4331Lisp_Object
4332code_convert_string (str, coding, encodep, nocopy)
4333 Lisp_Object str;
4ed46869 4334 struct coding_system *coding;
d46c5b12 4335 int encodep, nocopy;
4ed46869 4336{
d46c5b12
KH
4337 int len;
4338 char *buf;
fc932ac6
RS
4339 int from = 0, to = XSTRING (str)->size;
4340 int to_byte = STRING_BYTES (XSTRING (str));
d46c5b12
KH
4341 struct gcpro gcpro1;
4342 Lisp_Object saved_coding_symbol = Qnil;
4343 int result;
4ed46869 4344
d46c5b12
KH
4345 if (encodep && !NILP (coding->pre_write_conversion)
4346 || !encodep && !NILP (coding->post_read_conversion))
4347 {
4348 /* Since we have to call Lisp functions which assume target text
4349 is in a buffer, after setting a temporary buffer, call
4350 code_convert_region. */
4351 int count = specpdl_ptr - specpdl;
4352 struct buffer *prev = current_buffer;
4353
4354 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
4355 temp_output_buffer_setup (" *code-converting-work*");
4356 set_buffer_internal (XBUFFER (Vstandard_output));
4357 if (encodep)
4358 insert_from_string (str, 0, 0, to, to_byte, 0);
4359 else
4360 {
4361 /* We must insert the contents of STR as is without
4362 unibyte<->multibyte conversion. */
4363 current_buffer->enable_multibyte_characters = Qnil;
4364 insert_from_string (str, 0, 0, to_byte, to_byte, 0);
4365 current_buffer->enable_multibyte_characters = Qt;
4366 }
fb88bf2d 4367 code_convert_region (BEGV, BEGV_BYTE, ZV, ZV_BYTE, coding, encodep, 1);
d46c5b12
KH
4368 if (encodep)
4369 /* We must return the buffer contents as unibyte string. */
4370 current_buffer->enable_multibyte_characters = Qnil;
4371 str = make_buffer_string (BEGV, ZV, 0);
4372 set_buffer_internal (prev);
4373 return unbind_to (count, str);
4374 }
4ed46869 4375
d46c5b12
KH
4376 if (! encodep && CODING_REQUIRE_DETECTION (coding))
4377 {
4378 /* See the comments in code_convert_region. */
4379 if (coding->type == coding_type_undecided)
4380 {
4381 detect_coding (coding, XSTRING (str)->data, to_byte);
4382 if (coding->type == coding_type_undecided)
4383 coding->type = coding_type_emacs_mule;
4384 }
4385 if (coding->eol_type == CODING_EOL_UNDECIDED)
4386 {
4387 saved_coding_symbol = coding->symbol;
4388 detect_eol (coding, XSTRING (str)->data, to_byte);
4389 if (coding->eol_type == CODING_EOL_UNDECIDED)
4390 coding->eol_type = CODING_EOL_LF;
4391 /* We had better recover the original eol format if we
4392 encounter an inconsitent eol format while decoding. */
4393 coding->mode |= CODING_MODE_INHIBIT_INCONSISTENT_EOL;
4394 }
4395 }
4ed46869 4396
d46c5b12
KH
4397 if (encodep
4398 ? ! CODING_REQUIRE_ENCODING (coding)
4399 : ! CODING_REQUIRE_DECODING (coding))
4400 from = to_byte;
4401 else
4402 {
4403 /* Try to skip the heading and tailing ASCIIs. */
4404 if (encodep)
4405 shrink_encoding_region (&from, &to_byte, coding, XSTRING (str)->data);
4406 else
4407 shrink_decoding_region (&from, &to_byte, coding, XSTRING (str)->data);
4408 }
4409 if (from == to_byte)
4410 return (nocopy ? str : Fcopy_sequence (str));
4ed46869 4411
d46c5b12
KH
4412 if (encodep)
4413 len = encoding_buffer_size (coding, to_byte - from);
4414 else
4415 len = decoding_buffer_size (coding, to_byte - from);
fc932ac6 4416 len += from + STRING_BYTES (XSTRING (str)) - to_byte;
d46c5b12
KH
4417 GCPRO1 (str);
4418 buf = get_conversion_buffer (len);
4419 UNGCPRO;
4ed46869 4420
d46c5b12
KH
4421 if (from > 0)
4422 bcopy (XSTRING (str)->data, buf, from);
4423 result = (encodep
4424 ? encode_coding (coding, XSTRING (str)->data + from,
4425 buf + from, to_byte - from, len)
4426 : decode_coding (coding, XSTRING (str)->data + from,
f30cc612 4427 buf + from, to_byte - from, len));
d46c5b12 4428 if (! encodep && result == CODING_FINISH_INCONSISTENT_EOL)
4ed46869 4429 {
d46c5b12
KH
4430 /* We simple try to decode the whole string again but without
4431 eol-conversion this time. */
4432 coding->eol_type = CODING_EOL_LF;
4433 coding->symbol = saved_coding_symbol;
4434 return code_convert_string (str, coding, encodep, nocopy);
4ed46869 4435 }
d46c5b12
KH
4436
4437 bcopy (XSTRING (str)->data + to_byte, buf + from + coding->produced,
fc932ac6 4438 STRING_BYTES (XSTRING (str)) - to_byte);
d46c5b12 4439
fc932ac6 4440 len = from + STRING_BYTES (XSTRING (str)) - to_byte;
d46c5b12
KH
4441 if (encodep)
4442 str = make_unibyte_string (buf, len + coding->produced);
4443 else
bbdf84bd
RS
4444 str = make_string_from_bytes (buf, len + coding->produced_char,
4445 len + coding->produced);
d46c5b12 4446 return str;
4ed46869
KH
4447}
4448
4449\f
4450#ifdef emacs
4451/*** 7. Emacs Lisp library functions ***/
4452
4ed46869
KH
4453DEFUN ("coding-system-p", Fcoding_system_p, Scoding_system_p, 1, 1, 0,
4454 "Return t if OBJECT is nil or a coding-system.\n\
3a73fa5d
RS
4455See the documentation of `make-coding-system' for information\n\
4456about coding-system objects.")
4ed46869
KH
4457 (obj)
4458 Lisp_Object obj;
4459{
4608c386
KH
4460 if (NILP (obj))
4461 return Qt;
4462 if (!SYMBOLP (obj))
4463 return Qnil;
4464 /* Get coding-spec vector for OBJ. */
4465 obj = Fget (obj, Qcoding_system);
4466 return ((VECTORP (obj) && XVECTOR (obj)->size == 5)
4467 ? Qt : Qnil);
4ed46869
KH
4468}
4469
9d991de8
RS
4470DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system,
4471 Sread_non_nil_coding_system, 1, 1, 0,
e0e989f6 4472 "Read a coding system from the minibuffer, prompting with string PROMPT.")
4ed46869
KH
4473 (prompt)
4474 Lisp_Object prompt;
4475{
e0e989f6 4476 Lisp_Object val;
9d991de8
RS
4477 do
4478 {
4608c386
KH
4479 val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil,
4480 Qt, Qnil, Qcoding_system_history, Qnil, Qnil);
9d991de8
RS
4481 }
4482 while (XSTRING (val)->size == 0);
e0e989f6 4483 return (Fintern (val, Qnil));
4ed46869
KH
4484}
4485
9b787f3e
RS
4486DEFUN ("read-coding-system", Fread_coding_system, Sread_coding_system, 1, 2, 0,
4487 "Read a coding system from the minibuffer, prompting with string PROMPT.\n\
4488If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.")
4489 (prompt, default_coding_system)
4490 Lisp_Object prompt, default_coding_system;
4ed46869 4491{
f44d27ce 4492 Lisp_Object val;
9b787f3e
RS
4493 if (SYMBOLP (default_coding_system))
4494 XSETSTRING (default_coding_system, XSYMBOL (default_coding_system)->name);
4608c386 4495 val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil,
9b787f3e
RS
4496 Qt, Qnil, Qcoding_system_history,
4497 default_coding_system, Qnil);
e0e989f6 4498 return (XSTRING (val)->size == 0 ? Qnil : Fintern (val, Qnil));
4ed46869
KH
4499}
4500
4501DEFUN ("check-coding-system", Fcheck_coding_system, Scheck_coding_system,
4502 1, 1, 0,
4503 "Check validity of CODING-SYSTEM.\n\
3a73fa5d
RS
4504If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.\n\
4505It is valid if it is a symbol with a non-nil `coding-system' property.\n\
4ed46869
KH
4506The value of property should be a vector of length 5.")
4507 (coding_system)
4508 Lisp_Object coding_system;
4509{
4510 CHECK_SYMBOL (coding_system, 0);
4511 if (!NILP (Fcoding_system_p (coding_system)))
4512 return coding_system;
4513 while (1)
02ba4723 4514 Fsignal (Qcoding_system_error, Fcons (coding_system, Qnil));
4ed46869 4515}
3a73fa5d 4516\f
d46c5b12
KH
4517Lisp_Object
4518detect_coding_system (src, src_bytes, highest)
4519 unsigned char *src;
4520 int src_bytes, highest;
4ed46869
KH
4521{
4522 int coding_mask, eol_type;
d46c5b12
KH
4523 Lisp_Object val, tmp;
4524 int dummy;
4ed46869 4525
d46c5b12
KH
4526 coding_mask = detect_coding_mask (src, src_bytes, NULL, &dummy);
4527 eol_type = detect_eol_type (src, src_bytes, &dummy);
4528 if (eol_type == CODING_EOL_INCONSISTENT)
4529 eol_type == CODING_EOL_UNDECIDED;
4ed46869 4530
d46c5b12 4531 if (!coding_mask)
4ed46869 4532 {
27901516 4533 val = Qundecided;
d46c5b12 4534 if (eol_type != CODING_EOL_UNDECIDED)
4ed46869 4535 {
f44d27ce
RS
4536 Lisp_Object val2;
4537 val2 = Fget (Qundecided, Qeol_type);
4ed46869
KH
4538 if (VECTORP (val2))
4539 val = XVECTOR (val2)->contents[eol_type];
4540 }
d46c5b12 4541 return val;
4ed46869 4542 }
4ed46869 4543
d46c5b12
KH
4544 /* At first, gather possible coding systems in VAL. */
4545 val = Qnil;
4546 for (tmp = Vcoding_category_list; !NILP (tmp); tmp = XCONS (tmp)->cdr)
4ed46869 4547 {
d46c5b12
KH
4548 int idx
4549 = XFASTINT (Fget (XCONS (tmp)->car, Qcoding_category_index));
4550 if (coding_mask & (1 << idx))
4ed46869 4551 {
d46c5b12
KH
4552 val = Fcons (Fsymbol_value (XCONS (tmp)->car), val);
4553 if (highest)
4554 break;
4ed46869
KH
4555 }
4556 }
d46c5b12
KH
4557 if (!highest)
4558 val = Fnreverse (val);
4ed46869 4559
d46c5b12
KH
4560 /* Then, substitute the elements by subsidiary coding systems. */
4561 for (tmp = val; !NILP (tmp); tmp = XCONS (tmp)->cdr)
4ed46869 4562 {
d46c5b12 4563 if (eol_type != CODING_EOL_UNDECIDED)
4ed46869 4564 {
d46c5b12
KH
4565 Lisp_Object eol;
4566 eol = Fget (XCONS (tmp)->car, Qeol_type);
4567 if (VECTORP (eol))
4568 XCONS (tmp)->car = XVECTOR (eol)->contents[eol_type];
4ed46869
KH
4569 }
4570 }
d46c5b12
KH
4571 return (highest ? XCONS (val)->car : val);
4572}
4ed46869 4573
d46c5b12
KH
4574DEFUN ("detect-coding-region", Fdetect_coding_region, Sdetect_coding_region,
4575 2, 3, 0,
4576 "Detect coding system of the text in the region between START and END.\n\
4577Return a list of possible coding systems ordered by priority.\n\
4578\n\
4579If only ASCII characters are found, it returns `undecided'\n\
4580or its subsidiary coding system according to a detected end-of-line format.\n\
4581\n\
4582If optional argument HIGHEST is non-nil, return the coding system of\n\
4583highest priority.")
4584 (start, end, highest)
4585 Lisp_Object start, end, highest;
4586{
4587 int from, to;
4588 int from_byte, to_byte;
6289dd10 4589
d46c5b12
KH
4590 CHECK_NUMBER_COERCE_MARKER (start, 0);
4591 CHECK_NUMBER_COERCE_MARKER (end, 1);
4ed46869 4592
d46c5b12
KH
4593 validate_region (&start, &end);
4594 from = XINT (start), to = XINT (end);
4595 from_byte = CHAR_TO_BYTE (from);
4596 to_byte = CHAR_TO_BYTE (to);
6289dd10 4597
d46c5b12
KH
4598 if (from < GPT && to >= GPT)
4599 move_gap_both (to, to_byte);
4ed46869 4600
d46c5b12
KH
4601 return detect_coding_system (BYTE_POS_ADDR (from_byte),
4602 to_byte - from_byte,
4603 !NILP (highest));
4604}
6289dd10 4605
d46c5b12
KH
4606DEFUN ("detect-coding-string", Fdetect_coding_string, Sdetect_coding_string,
4607 1, 2, 0,
4608 "Detect coding system of the text in STRING.\n\
4609Return a list of possible coding systems ordered by priority.\n\
4610\n\
4611If only ASCII characters are found, it returns `undecided'\n\
4612or its subsidiary coding system according to a detected end-of-line format.\n\
4613\n\
4614If optional argument HIGHEST is non-nil, return the coding system of\n\
4615highest priority.")
4616 (string, highest)
4617 Lisp_Object string, highest;
4618{
4619 CHECK_STRING (string, 0);
4ed46869 4620
d46c5b12 4621 return detect_coding_system (XSTRING (string)->data,
fc932ac6 4622 STRING_BYTES (XSTRING (string)),
d46c5b12 4623 !NILP (highest));
4ed46869
KH
4624}
4625
4031e2bf
KH
4626Lisp_Object
4627code_convert_region1 (start, end, coding_system, encodep)
d46c5b12 4628 Lisp_Object start, end, coding_system;
4031e2bf 4629 int encodep;
3a73fa5d
RS
4630{
4631 struct coding_system coding;
4031e2bf 4632 int from, to, len;
3a73fa5d 4633
d46c5b12
KH
4634 CHECK_NUMBER_COERCE_MARKER (start, 0);
4635 CHECK_NUMBER_COERCE_MARKER (end, 1);
3a73fa5d
RS
4636 CHECK_SYMBOL (coding_system, 2);
4637
d46c5b12
KH
4638 validate_region (&start, &end);
4639 from = XFASTINT (start);
4640 to = XFASTINT (end);
4641
3a73fa5d 4642 if (NILP (coding_system))
d46c5b12
KH
4643 return make_number (to - from);
4644
3a73fa5d 4645 if (setup_coding_system (Fcheck_coding_system (coding_system), &coding) < 0)
d46c5b12 4646 error ("Invalid coding system: %s", XSYMBOL (coding_system)->name->data);
3a73fa5d 4647
d46c5b12 4648 coding.mode |= CODING_MODE_LAST_BLOCK;
fb88bf2d
KH
4649 code_convert_region (from, CHAR_TO_BYTE (from), to, CHAR_TO_BYTE (to),
4650 &coding, encodep, 1);
4651 return make_number (coding.produced_char);
4031e2bf
KH
4652}
4653
4654DEFUN ("decode-coding-region", Fdecode_coding_region, Sdecode_coding_region,
4655 3, 3, "r\nzCoding system: ",
4656 "Decode the current region by specified coding system.\n\
4657When called from a program, takes three arguments:\n\
4658START, END, and CODING-SYSTEM. START and END are buffer positions.\n\
4659Return length of decoded text.")
4660 (start, end, coding_system)
4661 Lisp_Object start, end, coding_system;
4662{
4663 return code_convert_region1 (start, end, coding_system, 0);
3a73fa5d
RS
4664}
4665
4666DEFUN ("encode-coding-region", Fencode_coding_region, Sencode_coding_region,
4667 3, 3, "r\nzCoding system: ",
d46c5b12 4668 "Encode the current region by specified coding system.\n\
3a73fa5d 4669When called from a program, takes three arguments:\n\
d46c5b12 4670START, END, and CODING-SYSTEM. START and END are buffer positions.\n\
3a73fa5d 4671Return length of encoded text.")
d46c5b12
KH
4672 (start, end, coding_system)
4673 Lisp_Object start, end, coding_system;
3a73fa5d 4674{
4031e2bf
KH
4675 return code_convert_region1 (start, end, coding_system, 1);
4676}
3a73fa5d 4677
4031e2bf
KH
4678Lisp_Object
4679code_convert_string1 (string, coding_system, nocopy, encodep)
4680 Lisp_Object string, coding_system, nocopy;
4681 int encodep;
4682{
4683 struct coding_system coding;
3a73fa5d 4684
4031e2bf
KH
4685 CHECK_STRING (string, 0);
4686 CHECK_SYMBOL (coding_system, 1);
4ed46869 4687
d46c5b12 4688 if (NILP (coding_system))
4031e2bf 4689 return (NILP (nocopy) ? Fcopy_sequence (string) : string);
4ed46869 4690
d46c5b12
KH
4691 if (setup_coding_system (Fcheck_coding_system (coding_system), &coding) < 0)
4692 error ("Invalid coding system: %s", XSYMBOL (coding_system)->name->data);
5f1cd180 4693
d46c5b12 4694 coding.mode |= CODING_MODE_LAST_BLOCK;
4031e2bf 4695 return code_convert_string (string, &coding, encodep, !NILP (nocopy));
4ed46869
KH
4696}
4697
4ed46869 4698DEFUN ("decode-coding-string", Fdecode_coding_string, Sdecode_coding_string,
e0e989f6
KH
4699 2, 3, 0,
4700 "Decode STRING which is encoded in CODING-SYSTEM, and return the result.\n\
fe487a71
RS
4701Optional arg NOCOPY non-nil means it is ok to return STRING itself\n\
4702if the decoding operation is trivial.")
e0e989f6
KH
4703 (string, coding_system, nocopy)
4704 Lisp_Object string, coding_system, nocopy;
4ed46869 4705{
4031e2bf 4706 return code_convert_string1(string, coding_system, nocopy, 0);
4ed46869
KH
4707}
4708
4709DEFUN ("encode-coding-string", Fencode_coding_string, Sencode_coding_string,
e0e989f6
KH
4710 2, 3, 0,
4711 "Encode STRING to CODING-SYSTEM, and return the result.\n\
fe487a71
RS
4712Optional arg NOCOPY non-nil means it is ok to return STRING itself\n\
4713if the encoding operation is trivial.")
e0e989f6
KH
4714 (string, coding_system, nocopy)
4715 Lisp_Object string, coding_system, nocopy;
4ed46869 4716{
4031e2bf 4717 return code_convert_string1(string, coding_system, nocopy, 1);
4ed46869 4718}
4031e2bf 4719
3a73fa5d 4720\f
4ed46869 4721DEFUN ("decode-sjis-char", Fdecode_sjis_char, Sdecode_sjis_char, 1, 1, 0,
e0e989f6 4722 "Decode a JISX0208 character of shift-jis encoding.\n\
4ed46869
KH
4723CODE is the character code in SJIS.\n\
4724Return the corresponding character.")
4725 (code)
4726 Lisp_Object code;
4727{
4728 unsigned char c1, c2, s1, s2;
4729 Lisp_Object val;
4730
4731 CHECK_NUMBER (code, 0);
4732 s1 = (XFASTINT (code)) >> 8, s2 = (XFASTINT (code)) & 0xFF;
4733 DECODE_SJIS (s1, s2, c1, c2);
4734 XSETFASTINT (val, MAKE_NON_ASCII_CHAR (charset_jisx0208, c1, c2));
4735 return val;
4736}
4737
4738DEFUN ("encode-sjis-char", Fencode_sjis_char, Sencode_sjis_char, 1, 1, 0,
d46c5b12 4739 "Encode a JISX0208 character CHAR to SJIS coding system.\n\
4ed46869
KH
4740Return the corresponding character code in SJIS.")
4741 (ch)
4742 Lisp_Object ch;
4743{
bcf26d6a 4744 int charset, c1, c2, s1, s2;
4ed46869
KH
4745 Lisp_Object val;
4746
4747 CHECK_NUMBER (ch, 0);
4748 SPLIT_CHAR (XFASTINT (ch), charset, c1, c2);
4749 if (charset == charset_jisx0208)
4750 {
4751 ENCODE_SJIS (c1, c2, s1, s2);
bcf26d6a 4752 XSETFASTINT (val, (s1 << 8) | s2);
4ed46869
KH
4753 }
4754 else
4755 XSETFASTINT (val, 0);
4756 return val;
4757}
4758
4759DEFUN ("decode-big5-char", Fdecode_big5_char, Sdecode_big5_char, 1, 1, 0,
d46c5b12 4760 "Decode a Big5 character CODE of BIG5 coding system.\n\
4ed46869
KH
4761CODE is the character code in BIG5.\n\
4762Return the corresponding character.")
4763 (code)
4764 Lisp_Object code;
4765{
4766 int charset;
4767 unsigned char b1, b2, c1, c2;
4768 Lisp_Object val;
4769
4770 CHECK_NUMBER (code, 0);
4771 b1 = (XFASTINT (code)) >> 8, b2 = (XFASTINT (code)) & 0xFF;
4772 DECODE_BIG5 (b1, b2, charset, c1, c2);
4773 XSETFASTINT (val, MAKE_NON_ASCII_CHAR (charset, c1, c2));
4774 return val;
4775}
4776
4777DEFUN ("encode-big5-char", Fencode_big5_char, Sencode_big5_char, 1, 1, 0,
d46c5b12 4778 "Encode the Big5 character CHAR to BIG5 coding system.\n\
4ed46869
KH
4779Return the corresponding character code in Big5.")
4780 (ch)
4781 Lisp_Object ch;
4782{
bcf26d6a 4783 int charset, c1, c2, b1, b2;
4ed46869
KH
4784 Lisp_Object val;
4785
4786 CHECK_NUMBER (ch, 0);
4787 SPLIT_CHAR (XFASTINT (ch), charset, c1, c2);
4788 if (charset == charset_big5_1 || charset == charset_big5_2)
4789 {
4790 ENCODE_BIG5 (charset, c1, c2, b1, b2);
bcf26d6a 4791 XSETFASTINT (val, (b1 << 8) | b2);
4ed46869
KH
4792 }
4793 else
4794 XSETFASTINT (val, 0);
4795 return val;
4796}
3a73fa5d 4797\f
1ba9e4ab
KH
4798DEFUN ("set-terminal-coding-system-internal",
4799 Fset_terminal_coding_system_internal,
4800 Sset_terminal_coding_system_internal, 1, 1, 0, "")
4ed46869
KH
4801 (coding_system)
4802 Lisp_Object coding_system;
4803{
4804 CHECK_SYMBOL (coding_system, 0);
4805 setup_coding_system (Fcheck_coding_system (coding_system), &terminal_coding);
70c22245 4806 /* We had better not send unsafe characters to terminal. */
6e85d753
KH
4807 terminal_coding.flags |= CODING_FLAG_ISO_SAFE;
4808
4ed46869
KH
4809 return Qnil;
4810}
4811
c4825358
KH
4812DEFUN ("set-safe-terminal-coding-system-internal",
4813 Fset_safe_terminal_coding_system_internal,
4814 Sset_safe_terminal_coding_system_internal, 1, 1, 0, "")
4815 (coding_system)
4816 Lisp_Object coding_system;
4817{
4818 CHECK_SYMBOL (coding_system, 0);
4819 setup_coding_system (Fcheck_coding_system (coding_system),
4820 &safe_terminal_coding);
4821 return Qnil;
4822}
4823
4ed46869
KH
4824DEFUN ("terminal-coding-system",
4825 Fterminal_coding_system, Sterminal_coding_system, 0, 0, 0,
3a73fa5d 4826 "Return coding system specified for terminal output.")
4ed46869
KH
4827 ()
4828{
4829 return terminal_coding.symbol;
4830}
4831
1ba9e4ab
KH
4832DEFUN ("set-keyboard-coding-system-internal",
4833 Fset_keyboard_coding_system_internal,
4834 Sset_keyboard_coding_system_internal, 1, 1, 0, "")
4ed46869
KH
4835 (coding_system)
4836 Lisp_Object coding_system;
4837{
4838 CHECK_SYMBOL (coding_system, 0);
4839 setup_coding_system (Fcheck_coding_system (coding_system), &keyboard_coding);
4840 return Qnil;
4841}
4842
4843DEFUN ("keyboard-coding-system",
4844 Fkeyboard_coding_system, Skeyboard_coding_system, 0, 0, 0,
3a73fa5d 4845 "Return coding system specified for decoding keyboard input.")
4ed46869
KH
4846 ()
4847{
4848 return keyboard_coding.symbol;
4849}
4850
4851\f
a5d301df
KH
4852DEFUN ("find-operation-coding-system", Ffind_operation_coding_system,
4853 Sfind_operation_coding_system, 1, MANY, 0,
4854 "Choose a coding system for an operation based on the target name.\n\
9ce27fde
KH
4855The value names a pair of coding systems: (DECODING-SYSTEM ENCODING-SYSTEM).\n\
4856DECODING-SYSTEM is the coding system to use for decoding\n\
4857\(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system\n\
4858for encoding (in case OPERATION does encoding).\n\
ccdb79f5
RS
4859\n\
4860The first argument OPERATION specifies an I/O primitive:\n\
4861 For file I/O, `insert-file-contents' or `write-region'.\n\
4862 For process I/O, `call-process', `call-process-region', or `start-process'.\n\
4863 For network I/O, `open-network-stream'.\n\
4864\n\
4865The remaining arguments should be the same arguments that were passed\n\
4866to the primitive. Depending on which primitive, one of those arguments\n\
4867is selected as the TARGET. For example, if OPERATION does file I/O,\n\
4868whichever argument specifies the file name is TARGET.\n\
4869\n\
4870TARGET has a meaning which depends on OPERATION:\n\
4ed46869
KH
4871 For file I/O, TARGET is a file name.\n\
4872 For process I/O, TARGET is a process name.\n\
4873 For network I/O, TARGET is a service name or a port number\n\
4874\n\
02ba4723
KH
4875This function looks up what specified for TARGET in,\n\
4876`file-coding-system-alist', `process-coding-system-alist',\n\
4877or `network-coding-system-alist' depending on OPERATION.\n\
4878They may specify a coding system, a cons of coding systems,\n\
4879or a function symbol to call.\n\
4880In the last case, we call the function with one argument,\n\
9ce27fde 4881which is a list of all the arguments given to this function.")
4ed46869
KH
4882 (nargs, args)
4883 int nargs;
4884 Lisp_Object *args;
4885{
4886 Lisp_Object operation, target_idx, target, val;
4887 register Lisp_Object chain;
4888
4889 if (nargs < 2)
4890 error ("Too few arguments");
4891 operation = args[0];
4892 if (!SYMBOLP (operation)
4893 || !INTEGERP (target_idx = Fget (operation, Qtarget_idx)))
4894 error ("Invalid first arguement");
4895 if (nargs < 1 + XINT (target_idx))
4896 error ("Too few arguments for operation: %s",
4897 XSYMBOL (operation)->name->data);
4898 target = args[XINT (target_idx) + 1];
4899 if (!(STRINGP (target)
4900 || (EQ (operation, Qopen_network_stream) && INTEGERP (target))))
4901 error ("Invalid %dth argument", XINT (target_idx) + 1);
4902
2e34157c
RS
4903 chain = ((EQ (operation, Qinsert_file_contents)
4904 || EQ (operation, Qwrite_region))
02ba4723 4905 ? Vfile_coding_system_alist
2e34157c 4906 : (EQ (operation, Qopen_network_stream)
02ba4723
KH
4907 ? Vnetwork_coding_system_alist
4908 : Vprocess_coding_system_alist));
4ed46869
KH
4909 if (NILP (chain))
4910 return Qnil;
4911
02ba4723 4912 for (; CONSP (chain); chain = XCONS (chain)->cdr)
4ed46869 4913 {
f44d27ce
RS
4914 Lisp_Object elt;
4915 elt = XCONS (chain)->car;
4ed46869
KH
4916
4917 if (CONSP (elt)
4918 && ((STRINGP (target)
4919 && STRINGP (XCONS (elt)->car)
4920 && fast_string_match (XCONS (elt)->car, target) >= 0)
4921 || (INTEGERP (target) && EQ (target, XCONS (elt)->car))))
02ba4723
KH
4922 {
4923 val = XCONS (elt)->cdr;
b19fd4c5
KH
4924 /* Here, if VAL is both a valid coding system and a valid
4925 function symbol, we return VAL as a coding system. */
02ba4723
KH
4926 if (CONSP (val))
4927 return val;
4928 if (! SYMBOLP (val))
4929 return Qnil;
4930 if (! NILP (Fcoding_system_p (val)))
4931 return Fcons (val, val);
b19fd4c5
KH
4932 if (! NILP (Ffboundp (val)))
4933 {
4934 val = call1 (val, Flist (nargs, args));
4935 if (CONSP (val))
4936 return val;
4937 if (SYMBOLP (val) && ! NILP (Fcoding_system_p (val)))
4938 return Fcons (val, val);
4939 }
02ba4723
KH
4940 return Qnil;
4941 }
4ed46869
KH
4942 }
4943 return Qnil;
4944}
4945
d46c5b12
KH
4946DEFUN ("update-iso-coding-systems", Fupdate_iso_coding_systems,
4947 Supdate_iso_coding_systems, 0, 0, 0,
4948 "Update internal database for ISO2022 based coding systems.\n\
4949When values of the following coding categories are changed, you must\n\
4950call this function:\n\
4951 coding-category-iso-7, coding-category-iso-7-tight,\n\
4952 coding-category-iso-8-1, coding-category-iso-8-2,\n\
4953 coding-category-iso-7-else, coding-category-iso-8-else")
4954 ()
4955{
4956 int i;
4957
4958 for (i = CODING_CATEGORY_IDX_ISO_7; i <= CODING_CATEGORY_IDX_ISO_8_ELSE;
4959 i++)
4960 {
4961 if (! coding_system_table[i])
4962 coding_system_table[i]
4963 = (struct coding_system *) xmalloc (sizeof (struct coding_system));
4964 setup_coding_system
4965 (XSYMBOL (XVECTOR (Vcoding_category_table)->contents[i])->value,
4966 coding_system_table[i]);
4967 }
4968 return Qnil;
4969}
4970
4ed46869
KH
4971#endif /* emacs */
4972
4973\f
4974/*** 8. Post-amble ***/
4975
dfcf069d 4976void
4ed46869
KH
4977init_coding_once ()
4978{
4979 int i;
4980
0ef69138 4981 /* Emacs' internal format specific initialize routine. */
4ed46869
KH
4982 for (i = 0; i <= 0x20; i++)
4983 emacs_code_class[i] = EMACS_control_code;
4984 emacs_code_class[0x0A] = EMACS_linefeed_code;
4985 emacs_code_class[0x0D] = EMACS_carriage_return_code;
4986 for (i = 0x21 ; i < 0x7F; i++)
4987 emacs_code_class[i] = EMACS_ascii_code;
4988 emacs_code_class[0x7F] = EMACS_control_code;
4989 emacs_code_class[0x80] = EMACS_leading_code_composition;
4990 for (i = 0x81; i < 0xFF; i++)
4991 emacs_code_class[i] = EMACS_invalid_code;
4992 emacs_code_class[LEADING_CODE_PRIVATE_11] = EMACS_leading_code_3;
4993 emacs_code_class[LEADING_CODE_PRIVATE_12] = EMACS_leading_code_3;
4994 emacs_code_class[LEADING_CODE_PRIVATE_21] = EMACS_leading_code_4;
4995 emacs_code_class[LEADING_CODE_PRIVATE_22] = EMACS_leading_code_4;
4996
4997 /* ISO2022 specific initialize routine. */
4998 for (i = 0; i < 0x20; i++)
4999 iso_code_class[i] = ISO_control_code;
5000 for (i = 0x21; i < 0x7F; i++)
5001 iso_code_class[i] = ISO_graphic_plane_0;
5002 for (i = 0x80; i < 0xA0; i++)
5003 iso_code_class[i] = ISO_control_code;
5004 for (i = 0xA1; i < 0xFF; i++)
5005 iso_code_class[i] = ISO_graphic_plane_1;
5006 iso_code_class[0x20] = iso_code_class[0x7F] = ISO_0x20_or_0x7F;
5007 iso_code_class[0xA0] = iso_code_class[0xFF] = ISO_0xA0_or_0xFF;
5008 iso_code_class[ISO_CODE_CR] = ISO_carriage_return;
5009 iso_code_class[ISO_CODE_SO] = ISO_shift_out;
5010 iso_code_class[ISO_CODE_SI] = ISO_shift_in;
5011 iso_code_class[ISO_CODE_SS2_7] = ISO_single_shift_2_7;
5012 iso_code_class[ISO_CODE_ESC] = ISO_escape;
5013 iso_code_class[ISO_CODE_SS2] = ISO_single_shift_2;
5014 iso_code_class[ISO_CODE_SS3] = ISO_single_shift_3;
5015 iso_code_class[ISO_CODE_CSI] = ISO_control_sequence_introducer;
5016
e0e989f6
KH
5017 conversion_buffer_size = MINIMUM_CONVERSION_BUFFER_SIZE;
5018 conversion_buffer = (char *) xmalloc (MINIMUM_CONVERSION_BUFFER_SIZE);
5019
5020 setup_coding_system (Qnil, &keyboard_coding);
5021 setup_coding_system (Qnil, &terminal_coding);
c4825358 5022 setup_coding_system (Qnil, &safe_terminal_coding);
9ce27fde 5023
d46c5b12
KH
5024 bzero (coding_system_table, sizeof coding_system_table);
5025
9ce27fde
KH
5026#if defined (MSDOS) || defined (WINDOWSNT)
5027 system_eol_type = CODING_EOL_CRLF;
5028#else
5029 system_eol_type = CODING_EOL_LF;
5030#endif
e0e989f6
KH
5031}
5032
5033#ifdef emacs
5034
dfcf069d 5035void
e0e989f6
KH
5036syms_of_coding ()
5037{
5038 Qtarget_idx = intern ("target-idx");
5039 staticpro (&Qtarget_idx);
5040
bb0115a2
RS
5041 Qcoding_system_history = intern ("coding-system-history");
5042 staticpro (&Qcoding_system_history);
5043 Fset (Qcoding_system_history, Qnil);
5044
9ce27fde 5045 /* Target FILENAME is the first argument. */
e0e989f6 5046 Fput (Qinsert_file_contents, Qtarget_idx, make_number (0));
9ce27fde 5047 /* Target FILENAME is the third argument. */
e0e989f6
KH
5048 Fput (Qwrite_region, Qtarget_idx, make_number (2));
5049
5050 Qcall_process = intern ("call-process");
5051 staticpro (&Qcall_process);
9ce27fde 5052 /* Target PROGRAM is the first argument. */
e0e989f6
KH
5053 Fput (Qcall_process, Qtarget_idx, make_number (0));
5054
5055 Qcall_process_region = intern ("call-process-region");
5056 staticpro (&Qcall_process_region);
9ce27fde 5057 /* Target PROGRAM is the third argument. */
e0e989f6
KH
5058 Fput (Qcall_process_region, Qtarget_idx, make_number (2));
5059
5060 Qstart_process = intern ("start-process");
5061 staticpro (&Qstart_process);
9ce27fde 5062 /* Target PROGRAM is the third argument. */
e0e989f6
KH
5063 Fput (Qstart_process, Qtarget_idx, make_number (2));
5064
5065 Qopen_network_stream = intern ("open-network-stream");
5066 staticpro (&Qopen_network_stream);
9ce27fde 5067 /* Target SERVICE is the fourth argument. */
e0e989f6
KH
5068 Fput (Qopen_network_stream, Qtarget_idx, make_number (3));
5069
4ed46869
KH
5070 Qcoding_system = intern ("coding-system");
5071 staticpro (&Qcoding_system);
5072
5073 Qeol_type = intern ("eol-type");
5074 staticpro (&Qeol_type);
5075
5076 Qbuffer_file_coding_system = intern ("buffer-file-coding-system");
5077 staticpro (&Qbuffer_file_coding_system);
5078
5079 Qpost_read_conversion = intern ("post-read-conversion");
5080 staticpro (&Qpost_read_conversion);
5081
5082 Qpre_write_conversion = intern ("pre-write-conversion");
5083 staticpro (&Qpre_write_conversion);
5084
27901516
KH
5085 Qno_conversion = intern ("no-conversion");
5086 staticpro (&Qno_conversion);
5087
5088 Qundecided = intern ("undecided");
5089 staticpro (&Qundecided);
5090
4ed46869
KH
5091 Qcoding_system_p = intern ("coding-system-p");
5092 staticpro (&Qcoding_system_p);
5093
5094 Qcoding_system_error = intern ("coding-system-error");
5095 staticpro (&Qcoding_system_error);
5096
5097 Fput (Qcoding_system_error, Qerror_conditions,
5098 Fcons (Qcoding_system_error, Fcons (Qerror, Qnil)));
5099 Fput (Qcoding_system_error, Qerror_message,
9ce27fde 5100 build_string ("Invalid coding system"));
4ed46869 5101
d46c5b12
KH
5102 Qcoding_category = intern ("coding-category");
5103 staticpro (&Qcoding_category);
4ed46869
KH
5104 Qcoding_category_index = intern ("coding-category-index");
5105 staticpro (&Qcoding_category_index);
5106
d46c5b12
KH
5107 Vcoding_category_table
5108 = Fmake_vector (make_number (CODING_CATEGORY_IDX_MAX), Qnil);
5109 staticpro (&Vcoding_category_table);
4ed46869
KH
5110 {
5111 int i;
5112 for (i = 0; i < CODING_CATEGORY_IDX_MAX; i++)
5113 {
d46c5b12
KH
5114 XVECTOR (Vcoding_category_table)->contents[i]
5115 = intern (coding_category_name[i]);
5116 Fput (XVECTOR (Vcoding_category_table)->contents[i],
5117 Qcoding_category_index, make_number (i));
4ed46869
KH
5118 }
5119 }
5120
bdd9fb48
KH
5121 Qcharacter_unification_table = intern ("character-unification-table");
5122 staticpro (&Qcharacter_unification_table);
5123 Fput (Qcharacter_unification_table, Qchar_table_extra_slots,
5124 make_number (0));
5125
a5d301df
KH
5126 Qcharacter_unification_table_for_decode
5127 = intern ("character-unification-table-for-decode");
5128 staticpro (&Qcharacter_unification_table_for_decode);
5129
5130 Qcharacter_unification_table_for_encode
5131 = intern ("character-unification-table-for-encode");
5132 staticpro (&Qcharacter_unification_table_for_encode);
5133
70c22245
KH
5134 Qsafe_charsets = intern ("safe-charsets");
5135 staticpro (&Qsafe_charsets);
5136
9ce27fde
KH
5137 Qemacs_mule = intern ("emacs-mule");
5138 staticpro (&Qemacs_mule);
5139
d46c5b12
KH
5140 Qraw_text = intern ("raw-text");
5141 staticpro (&Qraw_text);
5142
4ed46869
KH
5143 defsubr (&Scoding_system_p);
5144 defsubr (&Sread_coding_system);
5145 defsubr (&Sread_non_nil_coding_system);
5146 defsubr (&Scheck_coding_system);
5147 defsubr (&Sdetect_coding_region);
d46c5b12 5148 defsubr (&Sdetect_coding_string);
4ed46869
KH
5149 defsubr (&Sdecode_coding_region);
5150 defsubr (&Sencode_coding_region);
5151 defsubr (&Sdecode_coding_string);
5152 defsubr (&Sencode_coding_string);
5153 defsubr (&Sdecode_sjis_char);
5154 defsubr (&Sencode_sjis_char);
5155 defsubr (&Sdecode_big5_char);
5156 defsubr (&Sencode_big5_char);
1ba9e4ab 5157 defsubr (&Sset_terminal_coding_system_internal);
c4825358 5158 defsubr (&Sset_safe_terminal_coding_system_internal);
4ed46869 5159 defsubr (&Sterminal_coding_system);
1ba9e4ab 5160 defsubr (&Sset_keyboard_coding_system_internal);
4ed46869 5161 defsubr (&Skeyboard_coding_system);
a5d301df 5162 defsubr (&Sfind_operation_coding_system);
d46c5b12 5163 defsubr (&Supdate_iso_coding_systems);
4ed46869 5164
4608c386
KH
5165 DEFVAR_LISP ("coding-system-list", &Vcoding_system_list,
5166 "List of coding systems.\n\
5167\n\
5168Do not alter the value of this variable manually. This variable should be\n\
5169updated by the functions `make-coding-system' and\n\
5170`define-coding-system-alias'.");
5171 Vcoding_system_list = Qnil;
5172
5173 DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist,
5174 "Alist of coding system names.\n\
5175Each element is one element list of coding system name.\n\
5176This variable is given to `completing-read' as TABLE argument.\n\
5177\n\
5178Do not alter the value of this variable manually. This variable should be\n\
5179updated by the functions `make-coding-system' and\n\
5180`define-coding-system-alias'.");
5181 Vcoding_system_alist = Qnil;
5182
4ed46869
KH
5183 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list,
5184 "List of coding-categories (symbols) ordered by priority.");
5185 {
5186 int i;
5187
5188 Vcoding_category_list = Qnil;
5189 for (i = CODING_CATEGORY_IDX_MAX - 1; i >= 0; i--)
5190 Vcoding_category_list
d46c5b12
KH
5191 = Fcons (XVECTOR (Vcoding_category_table)->contents[i],
5192 Vcoding_category_list);
4ed46869
KH
5193 }
5194
5195 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read,
10bff6f1 5196 "Specify the coding system for read operations.\n\
2ebb362d 5197It is useful to bind this variable with `let', but do not set it globally.\n\
4ed46869 5198If the value is a coding system, it is used for decoding on read operation.\n\
a67a9c66 5199If not, an appropriate element is used from one of the coding system alists:\n\
10bff6f1 5200There are three such tables, `file-coding-system-alist',\n\
a67a9c66 5201`process-coding-system-alist', and `network-coding-system-alist'.");
4ed46869
KH
5202 Vcoding_system_for_read = Qnil;
5203
5204 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write,
10bff6f1 5205 "Specify the coding system for write operations.\n\
2ebb362d 5206It is useful to bind this variable with `let', but do not set it globally.\n\
4ed46869 5207If the value is a coding system, it is used for encoding on write operation.\n\
a67a9c66 5208If not, an appropriate element is used from one of the coding system alists:\n\
10bff6f1 5209There are three such tables, `file-coding-system-alist',\n\
a67a9c66 5210`process-coding-system-alist', and `network-coding-system-alist'.");
4ed46869
KH
5211 Vcoding_system_for_write = Qnil;
5212
5213 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used,
a67a9c66 5214 "Coding system used in the latest file or process I/O.");
4ed46869
KH
5215 Vlast_coding_system_used = Qnil;
5216
9ce27fde
KH
5217 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion,
5218 "*Non-nil inhibit code conversion of end-of-line format in any cases.");
5219 inhibit_eol_conversion = 0;
5220
ed29121d
EZ
5221 DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system,
5222 "Non-nil means process buffer inherits coding system of process output.\n\
5223Bind it to t if the process output is to be treated as if it were a file\n\
5224read from some filesystem.");
5225 inherit_process_coding_system = 0;
5226
02ba4723
KH
5227 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist,
5228 "Alist to decide a coding system to use for a file I/O operation.\n\
5229The format is ((PATTERN . VAL) ...),\n\
5230where PATTERN is a regular expression matching a file name,\n\
5231VAL is a coding system, a cons of coding systems, or a function symbol.\n\
5232If VAL is a coding system, it is used for both decoding and encoding\n\
5233the file contents.\n\
5234If VAL is a cons of coding systems, the car part is used for decoding,\n\
5235and the cdr part is used for encoding.\n\
5236If VAL is a function symbol, the function must return a coding system\n\
5237or a cons of coding systems which are used as above.\n\
e0e989f6 5238\n\
9ce27fde 5239See also the function `find-operation-coding-system'.");
02ba4723
KH
5240 Vfile_coding_system_alist = Qnil;
5241
5242 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist,
5243 "Alist to decide a coding system to use for a process I/O operation.\n\
5244The format is ((PATTERN . VAL) ...),\n\
5245where PATTERN is a regular expression matching a program name,\n\
5246VAL is a coding system, a cons of coding systems, or a function symbol.\n\
5247If VAL is a coding system, it is used for both decoding what received\n\
5248from the program and encoding what sent to the program.\n\
5249If VAL is a cons of coding systems, the car part is used for decoding,\n\
5250and the cdr part is used for encoding.\n\
5251If VAL is a function symbol, the function must return a coding system\n\
5252or a cons of coding systems which are used as above.\n\
4ed46869 5253\n\
9ce27fde 5254See also the function `find-operation-coding-system'.");
02ba4723
KH
5255 Vprocess_coding_system_alist = Qnil;
5256
5257 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist,
5258 "Alist to decide a coding system to use for a network I/O operation.\n\
5259The format is ((PATTERN . VAL) ...),\n\
5260where PATTERN is a regular expression matching a network service name\n\
5261or is a port number to connect to,\n\
5262VAL is a coding system, a cons of coding systems, or a function symbol.\n\
5263If VAL is a coding system, it is used for both decoding what received\n\
5264from the network stream and encoding what sent to the network stream.\n\
5265If VAL is a cons of coding systems, the car part is used for decoding,\n\
5266and the cdr part is used for encoding.\n\
5267If VAL is a function symbol, the function must return a coding system\n\
5268or a cons of coding systems which are used as above.\n\
4ed46869 5269\n\
9ce27fde 5270See also the function `find-operation-coding-system'.");
02ba4723 5271 Vnetwork_coding_system_alist = Qnil;
4ed46869
KH
5272
5273 DEFVAR_INT ("eol-mnemonic-unix", &eol_mnemonic_unix,
5274 "Mnemonic character indicating UNIX-like end-of-line format (i.e. LF) .");
458822a0 5275 eol_mnemonic_unix = ':';
4ed46869
KH
5276
5277 DEFVAR_INT ("eol-mnemonic-dos", &eol_mnemonic_dos,
5278 "Mnemonic character indicating DOS-like end-of-line format (i.e. CRLF).");
458822a0 5279 eol_mnemonic_dos = '\\';
4ed46869
KH
5280
5281 DEFVAR_INT ("eol-mnemonic-mac", &eol_mnemonic_mac,
5282 "Mnemonic character indicating MAC-like end-of-line format (i.e. CR).");
458822a0 5283 eol_mnemonic_mac = '/';
4ed46869
KH
5284
5285 DEFVAR_INT ("eol-mnemonic-undecided", &eol_mnemonic_undecided,
5286 "Mnemonic character indicating end-of-line format is not yet decided.");
458822a0 5287 eol_mnemonic_undecided = ':';
4ed46869 5288
bdd9fb48
KH
5289 DEFVAR_LISP ("enable-character-unification", &Venable_character_unification,
5290 "Non-nil means ISO 2022 encoder/decoder do character unification.");
5291 Venable_character_unification = Qt;
5292
a5d301df
KH
5293 DEFVAR_LISP ("standard-character-unification-table-for-decode",
5294 &Vstandard_character_unification_table_for_decode,
bdd9fb48 5295 "Table for unifying characters when reading.");
a5d301df 5296 Vstandard_character_unification_table_for_decode = Qnil;
bdd9fb48 5297
a5d301df
KH
5298 DEFVAR_LISP ("standard-character-unification-table-for-encode",
5299 &Vstandard_character_unification_table_for_encode,
bdd9fb48 5300 "Table for unifying characters when writing.");
a5d301df 5301 Vstandard_character_unification_table_for_encode = Qnil;
4ed46869
KH
5302
5303 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_alist,
5304 "Alist of charsets vs revision numbers.\n\
5305While encoding, if a charset (car part of an element) is found,\n\
5306designate it with the escape sequence identifing revision (cdr part of the element).");
5307 Vcharset_revision_alist = Qnil;
02ba4723
KH
5308
5309 DEFVAR_LISP ("default-process-coding-system",
5310 &Vdefault_process_coding_system,
5311 "Cons of coding systems used for process I/O by default.\n\
5312The car part is used for decoding a process output,\n\
5313the cdr part is used for encoding a text to be sent to a process.");
5314 Vdefault_process_coding_system = Qnil;
c4825358 5315
3f003981
KH
5316 DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table,
5317 "Table of extra Latin codes in the range 128..159 (inclusive).\n\
c4825358
KH
5318This is a vector of length 256.\n\
5319If Nth element is non-nil, the existence of code N in a file\n\
bb0115a2 5320\(or output of subprocess) doesn't prevent it to be detected as\n\
3f003981
KH
5321a coding system of ISO 2022 variant which has a flag\n\
5322`accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file\n\
c4825358
KH
5323or reading output of a subprocess.\n\
5324Only 128th through 159th elements has a meaning.");
3f003981 5325 Vlatin_extra_code_table = Fmake_vector (make_number (256), Qnil);
d46c5b12
KH
5326
5327 DEFVAR_LISP ("select-safe-coding-system-function",
5328 &Vselect_safe_coding_system_function,
5329 "Function to call to select safe coding system for encoding a text.\n\
5330\n\
5331If set, this function is called to force a user to select a proper\n\
5332coding system which can encode the text in the case that a default\n\
5333coding system used in each operation can't encode the text.\n\
5334\n\
5335The default value is `select-safe-codign-system' (which see).");
5336 Vselect_safe_coding_system_function = Qnil;
5337
4ed46869
KH
5338}
5339
5340#endif /* emacs */