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