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