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