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