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