Fix up multi-tty merge
[bpt/emacs.git] / src / coding.c
CommitLineData
9542cb1f 1/* Coding system handler (conversion, detection, etc).
aaef169d 2 Copyright (C) 2001, 2002, 2003, 2004, 2005,
4e6835db 3 2006, 2007 Free Software Foundation, Inc.
7976eda0 4 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4e6835db 5 2005, 2006, 2007
ce03bf76
KH
6 National Institute of Advanced Industrial Science and Technology (AIST)
7 Registration Number H14PRO021
8f924df7 8 Copyright (C) 2003
df7492f9
KH
9 National Institute of Advanced Industrial Science and Technology (AIST)
10 Registration Number H13PRO009
4ed46869 11
369314dc
KH
12This file is part of GNU Emacs.
13
14GNU Emacs is free software; you can redistribute it and/or modify
15it under the terms of the GNU General Public License as published by
1427aa65 16the Free Software Foundation; either version 3, or (at your option)
369314dc 17any later version.
4ed46869 18
369314dc
KH
19GNU Emacs is distributed in the hope that it will be useful,
20but WITHOUT ANY WARRANTY; without even the implied warranty of
21MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22GNU General Public License for more details.
4ed46869 23
369314dc
KH
24You should have received a copy of the GNU General Public License
25along with GNU Emacs; see the file COPYING. If not, write to
4fc5845f
LK
26the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27Boston, MA 02110-1301, USA. */
4ed46869
KH
28
29/*** TABLE OF CONTENTS ***
30
b73bfc1c 31 0. General comments
4ed46869 32 1. Preamble
df7492f9
KH
33 2. Emacs' internal format (emacs-utf-8) handlers
34 3. UTF-8 handlers
35 4. UTF-16 handlers
36 5. Charset-base coding systems handlers
37 6. emacs-mule (old Emacs' internal format) handlers
38 7. ISO2022 handlers
39 8. Shift-JIS and BIG5 handlers
40 9. CCL handlers
41 10. C library functions
42 11. Emacs Lisp library functions
43 12. Postamble
4ed46869
KH
44
45*/
46
df7492f9 47/*** 0. General comments ***
b73bfc1c
KH
48
49
df7492f9 50CODING SYSTEM
4ed46869 51
5bad0796
DL
52 A coding system is an object for an encoding mechanism that contains
53 information about how to convert byte sequences to character
e19c3639
KH
54 sequences and vice versa. When we say "decode", it means converting
55 a byte sequence of a specific coding system into a character
56 sequence that is represented by Emacs' internal coding system
57 `emacs-utf-8', and when we say "encode", it means converting a
58 character sequence of emacs-utf-8 to a byte sequence of a specific
0ef69138 59 coding system.
4ed46869 60
e19c3639
KH
61 In Emacs Lisp, a coding system is represented by a Lisp symbol. In
62 C level, a coding system is represented by a vector of attributes
5bad0796 63 stored in the hash table Vcharset_hash_table. The conversion from
e19c3639
KH
64 coding system symbol to attributes vector is done by looking up
65 Vcharset_hash_table by the symbol.
4ed46869 66
e19c3639 67 Coding systems are classified into the following types depending on
5bad0796 68 the encoding mechanism. Here's a brief description of the types.
4ed46869 69
df7492f9
KH
70 o UTF-8
71
72 o UTF-16
73
74 o Charset-base coding system
75
76 A coding system defined by one or more (coded) character sets.
5bad0796 77 Decoding and encoding are done by a code converter defined for each
df7492f9
KH
78 character set.
79
5bad0796 80 o Old Emacs internal format (emacs-mule)
df7492f9 81
5bad0796 82 The coding system adopted by old versions of Emacs (20 and 21).
4ed46869 83
df7492f9 84 o ISO2022-base coding system
4ed46869
KH
85
86 The most famous coding system for multiple character sets. X's
df7492f9
KH
87 Compound Text, various EUCs (Extended Unix Code), and coding systems
88 used in the Internet communication such as ISO-2022-JP are all
89 variants of ISO2022.
4ed46869 90
df7492f9 91 o SJIS (or Shift-JIS or MS-Kanji-Code)
93dec019 92
4ed46869
KH
93 A coding system to encode character sets: ASCII, JISX0201, and
94 JISX0208. Widely used for PC's in Japan. Details are described in
df7492f9 95 section 8.
4ed46869 96
df7492f9 97 o BIG5
4ed46869 98
df7492f9 99 A coding system to encode character sets: ASCII and Big5. Widely
cfb43547 100 used for Chinese (mainly in Taiwan and Hong Kong). Details are
df7492f9
KH
101 described in section 8. In this file, when we write "big5" (all
102 lowercase), we mean the coding system, and when we write "Big5"
103 (capitalized), we mean the character set.
4ed46869 104
df7492f9 105 o CCL
27901516 106
5bad0796 107 If a user wants to decode/encode text encoded in a coding system
df7492f9
KH
108 not listed above, he can supply a decoder and an encoder for it in
109 CCL (Code Conversion Language) programs. Emacs executes the CCL
110 program while decoding/encoding.
27901516 111
df7492f9 112 o Raw-text
4ed46869 113
5a936b46 114 A coding system for text containing raw eight-bit data. Emacs
5bad0796 115 treats each byte of source text as a character (except for
df7492f9 116 end-of-line conversion).
4ed46869 117
df7492f9
KH
118 o No-conversion
119
120 Like raw text, but don't do end-of-line conversion.
4ed46869 121
4ed46869 122
df7492f9 123END-OF-LINE FORMAT
4ed46869 124
5bad0796 125 How text end-of-line is encoded depends on operating system. For
df7492f9 126 instance, Unix's format is just one byte of LF (line-feed) code,
f4dee582 127 whereas DOS's format is two-byte sequence of `carriage-return' and
d46c5b12
KH
128 `line-feed' codes. MacOS's format is usually one byte of
129 `carriage-return'.
4ed46869 130
cfb43547 131 Since text character encoding and end-of-line encoding are
df7492f9
KH
132 independent, any coding system described above can take any format
133 of end-of-line (except for no-conversion).
4ed46869 134
e19c3639
KH
135STRUCT CODING_SYSTEM
136
137 Before using a coding system for code conversion (i.e. decoding and
138 encoding), we setup a structure of type `struct coding_system'.
139 This structure keeps various information about a specific code
5bad0796 140 conversion (e.g. the location of source and destination data).
4ed46869
KH
141
142*/
143
df7492f9
KH
144/* COMMON MACROS */
145
146
4ed46869
KH
147/*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
148
df7492f9 149 These functions check if a byte sequence specified as a source in
ff0dacd7
KH
150 CODING conforms to the format of XXX, and update the members of
151 DETECT_INFO.
df7492f9 152
ff0dacd7 153 Return 1 if the byte sequence conforms to XXX, otherwise return 0.
df7492f9
KH
154
155 Below is the template of these functions. */
156
4ed46869 157#if 0
df7492f9 158static int
ff0dacd7 159detect_coding_XXX (coding, detect_info)
df7492f9 160 struct coding_system *coding;
ff0dacd7 161 struct coding_detection_info *detect_info;
4ed46869 162{
f1d34bca
MB
163 const unsigned char *src = coding->source;
164 const unsigned char *src_end = coding->source + coding->src_bytes;
df7492f9 165 int multibytep = coding->src_multibyte;
ff0dacd7 166 int consumed_chars = 0;
df7492f9
KH
167 int found = 0;
168 ...;
169
170 while (1)
171 {
172 /* Get one byte from the source. If the souce is exausted, jump
173 to no_more_source:. */
174 ONE_MORE_BYTE (c);
ff0dacd7
KH
175
176 if (! __C_conforms_to_XXX___ (c))
177 break;
178 if (! __C_strongly_suggests_XXX__ (c))
179 found = CATEGORY_MASK_XXX;
df7492f9 180 }
ff0dacd7
KH
181 /* The byte sequence is invalid for XXX. */
182 detect_info->rejected |= CATEGORY_MASK_XXX;
df7492f9 183 return 0;
ff0dacd7 184
df7492f9 185 no_more_source:
ff0dacd7
KH
186 /* The source exausted successfully. */
187 detect_info->found |= found;
df7492f9 188 return 1;
4ed46869
KH
189}
190#endif
191
192/*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
193
df7492f9
KH
194 These functions decode a byte sequence specified as a source by
195 CODING. The resulting multibyte text goes to a place pointed to by
196 CODING->charbuf, the length of which should not exceed
197 CODING->charbuf_size;
d46c5b12 198
df7492f9
KH
199 These functions set the information of original and decoded texts in
200 CODING->consumed, CODING->consumed_char, and CODING->charbuf_used.
201 They also set CODING->result to one of CODING_RESULT_XXX indicating
202 how the decoding is finished.
d46c5b12 203
df7492f9 204 Below is the template of these functions. */
d46c5b12 205
4ed46869 206#if 0
b73bfc1c 207static void
df7492f9 208decode_coding_XXXX (coding)
4ed46869 209 struct coding_system *coding;
4ed46869 210{
f1d34bca
MB
211 const unsigned char *src = coding->source + coding->consumed;
212 const unsigned char *src_end = coding->source + coding->src_bytes;
df7492f9
KH
213 /* SRC_BASE remembers the start position in source in each loop.
214 The loop will be exited when there's not enough source code, or
215 when there's no room in CHARBUF for a decoded character. */
f1d34bca 216 const unsigned char *src_base;
df7492f9 217 /* A buffer to produce decoded characters. */
69a80ea3
KH
218 int *charbuf = coding->charbuf + coding->charbuf_used;
219 int *charbuf_end = coding->charbuf + coding->charbuf_size;
df7492f9
KH
220 int multibytep = coding->src_multibyte;
221
222 while (1)
223 {
224 src_base = src;
225 if (charbuf < charbuf_end)
226 /* No more room to produce a decoded character. */
227 break;
228 ONE_MORE_BYTE (c);
229 /* Decode it. */
230 }
231
232 no_more_source:
233 if (src_base < src_end
234 && coding->mode & CODING_MODE_LAST_BLOCK)
235 /* If the source ends by partial bytes to construct a character,
236 treat them as eight-bit raw data. */
237 while (src_base < src_end && charbuf < charbuf_end)
238 *charbuf++ = *src_base++;
239 /* Remember how many bytes and characters we consumed. If the
240 source is multibyte, the bytes and chars are not identical. */
241 coding->consumed = coding->consumed_char = src_base - coding->source;
242 /* Remember how many characters we produced. */
243 coding->charbuf_used = charbuf - coding->charbuf;
4ed46869
KH
244}
245#endif
246
247/*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
248
df7492f9
KH
249 These functions encode SRC_BYTES length text at SOURCE of Emacs'
250 internal multibyte format by CODING. The resulting byte sequence
b73bfc1c
KH
251 goes to a place pointed to by DESTINATION, the length of which
252 should not exceed DST_BYTES.
d46c5b12 253
df7492f9
KH
254 These functions set the information of original and encoded texts in
255 the members produced, produced_char, consumed, and consumed_char of
256 the structure *CODING. They also set the member result to one of
257 CODING_RESULT_XXX indicating how the encoding finished.
d46c5b12 258
df7492f9
KH
259 DST_BYTES zero means that source area and destination area are
260 overlapped, which means that we can produce a encoded text until it
261 reaches at the head of not-yet-encoded source text.
d46c5b12 262
df7492f9 263 Below is a template of these functions. */
4ed46869 264#if 0
b73bfc1c 265static void
df7492f9 266encode_coding_XXX (coding)
4ed46869 267 struct coding_system *coding;
4ed46869 268{
df7492f9
KH
269 int multibytep = coding->dst_multibyte;
270 int *charbuf = coding->charbuf;
271 int *charbuf_end = charbuf->charbuf + coding->charbuf_used;
272 unsigned char *dst = coding->destination + coding->produced;
273 unsigned char *dst_end = coding->destination + coding->dst_bytes;
274 unsigned char *adjusted_dst_end = dst_end - _MAX_BYTES_PRODUCED_IN_LOOP_;
275 int produced_chars = 0;
276
277 for (; charbuf < charbuf_end && dst < adjusted_dst_end; charbuf++)
278 {
279 int c = *charbuf;
280 /* Encode C into DST, and increment DST. */
281 }
282 label_no_more_destination:
283 /* How many chars and bytes we produced. */
284 coding->produced_char += produced_chars;
285 coding->produced = dst - coding->destination;
4ed46869
KH
286}
287#endif
288
4ed46869
KH
289\f
290/*** 1. Preamble ***/
291
68c45bf0 292#include <config.h>
4ed46869
KH
293#include <stdio.h>
294
4ed46869
KH
295#include "lisp.h"
296#include "buffer.h"
df7492f9 297#include "character.h"
4ed46869
KH
298#include "charset.h"
299#include "ccl.h"
df7492f9 300#include "composite.h"
4ed46869
KH
301#include "coding.h"
302#include "window.h"
b8299c66
KL
303#include "frame.h"
304#include "termhooks.h"
4ed46869 305
df7492f9 306Lisp_Object Vcoding_system_hash_table;
4ed46869 307
df7492f9 308Lisp_Object Qcoding_system, Qcoding_aliases, Qeol_type;
1965cb73
DL
309Lisp_Object Qunix, Qdos;
310extern Lisp_Object Qmac; /* frame.c */
4ed46869
KH
311Lisp_Object Qbuffer_file_coding_system;
312Lisp_Object Qpost_read_conversion, Qpre_write_conversion;
df7492f9 313Lisp_Object Qdefault_char;
27901516 314Lisp_Object Qno_conversion, Qundecided;
df7492f9 315Lisp_Object Qcharset, Qiso_2022, Qutf_8, Qutf_16, Qshift_jis, Qbig5;
b49a1807 316Lisp_Object Qbig, Qlittle;
bb0115a2 317Lisp_Object Qcoding_system_history;
1397dc18 318Lisp_Object Qvalid_codes;
a6f87d34
KH
319Lisp_Object QCcategory, QCmnemonic, QCdefalut_char;
320Lisp_Object QCdecode_translation_table, QCencode_translation_table;
321Lisp_Object QCpost_read_conversion, QCpre_write_conversion;
35befdaa 322Lisp_Object QCascii_compatible_p;
4ed46869
KH
323
324extern Lisp_Object Qinsert_file_contents, Qwrite_region;
387f6ba5 325Lisp_Object Qcall_process, Qcall_process_region;
4ed46869
KH
326Lisp_Object Qstart_process, Qopen_network_stream;
327Lisp_Object Qtarget_idx;
328
065e3595
KH
329Lisp_Object Qinsufficient_source, Qinconsistent_eol, Qinvalid_source;
330Lisp_Object Qinterrupted, Qinsufficient_memory;
331
44e8490d
KH
332/* If a symbol has this property, evaluate the value to define the
333 symbol as a coding system. */
334static Lisp_Object Qcoding_system_define_form;
335
5d5bf4d8
KH
336int coding_system_require_warning;
337
d46c5b12
KH
338Lisp_Object Vselect_safe_coding_system_function;
339
7722baf9
EZ
340/* Mnemonic string for each format of end-of-line. */
341Lisp_Object eol_mnemonic_unix, eol_mnemonic_dos, eol_mnemonic_mac;
342/* Mnemonic string to indicate format of end-of-line is not yet
4ed46869 343 decided. */
7722baf9 344Lisp_Object eol_mnemonic_undecided;
4ed46869 345
fcbcfb64
KH
346/* Format of end-of-line decided by system. This is Qunix on
347 Unix and Mac, Qdos on DOS/Windows.
348 This has an effect only for external encoding (i.e. for output to
349 file and process), not for in-buffer or Lisp string encoding. */
350static Lisp_Object system_eol_type;
351
4ed46869
KH
352#ifdef emacs
353
4608c386
KH
354Lisp_Object Vcoding_system_list, Vcoding_system_alist;
355
356Lisp_Object Qcoding_system_p, Qcoding_system_error;
4ed46869 357
d46c5b12
KH
358/* Coding system emacs-mule and raw-text are for converting only
359 end-of-line format. */
360Lisp_Object Qemacs_mule, Qraw_text;
8f924df7 361Lisp_Object Qutf_8_emacs;
ecf488bc 362
4ed46869
KH
363/* Coding-systems are handed between Emacs Lisp programs and C internal
364 routines by the following three variables. */
365/* Coding-system for reading files and receiving data from process. */
366Lisp_Object Vcoding_system_for_read;
367/* Coding-system for writing files and sending data to process. */
368Lisp_Object Vcoding_system_for_write;
369/* Coding-system actually used in the latest I/O. */
370Lisp_Object Vlast_coding_system_used;
065e3595
KH
371/* Set to non-nil when an error is detected while code conversion. */
372Lisp_Object Vlast_code_conversion_error;
c4825358 373/* A vector of length 256 which contains information about special
94487c4e 374 Latin codes (especially for dealing with Microsoft codes). */
3f003981 375Lisp_Object Vlatin_extra_code_table;
c4825358 376
9ce27fde
KH
377/* Flag to inhibit code conversion of end-of-line format. */
378int inhibit_eol_conversion;
379
74383408
KH
380/* Flag to inhibit ISO2022 escape sequence detection. */
381int inhibit_iso_escape_detection;
382
ed29121d
EZ
383/* Flag to make buffer-file-coding-system inherit from process-coding. */
384int inherit_process_coding_system;
385
c4825358
KH
386/* Coding system to be used to encode text for terminal display when
387 terminal coding system is nil. */
388struct coding_system safe_terminal_coding;
389
02ba4723
KH
390Lisp_Object Vfile_coding_system_alist;
391Lisp_Object Vprocess_coding_system_alist;
392Lisp_Object Vnetwork_coding_system_alist;
4ed46869 393
68c45bf0
PE
394Lisp_Object Vlocale_coding_system;
395
4ed46869
KH
396#endif /* emacs */
397
f967223b
KH
398/* Flag to tell if we look up translation table on character code
399 conversion. */
84fbb8a0 400Lisp_Object Venable_character_translation;
f967223b
KH
401/* Standard translation table to look up on decoding (reading). */
402Lisp_Object Vstandard_translation_table_for_decode;
403/* Standard translation table to look up on encoding (writing). */
404Lisp_Object Vstandard_translation_table_for_encode;
84fbb8a0 405
f967223b
KH
406Lisp_Object Qtranslation_table;
407Lisp_Object Qtranslation_table_id;
408Lisp_Object Qtranslation_table_for_decode;
409Lisp_Object Qtranslation_table_for_encode;
4ed46869
KH
410
411/* Alist of charsets vs revision number. */
df7492f9 412static Lisp_Object Vcharset_revision_table;
4ed46869 413
02ba4723
KH
414/* Default coding systems used for process I/O. */
415Lisp_Object Vdefault_process_coding_system;
416
002fdb44
DL
417/* Char table for translating Quail and self-inserting input. */
418Lisp_Object Vtranslation_table_for_input;
419
df7492f9
KH
420/* Two special coding systems. */
421Lisp_Object Vsjis_coding_system;
422Lisp_Object Vbig5_coding_system;
423
df7492f9
KH
424/* ISO2022 section */
425
426#define CODING_ISO_INITIAL(coding, reg) \
427 (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \
428 coding_attr_iso_initial), \
429 reg)))
430
431
432#define CODING_ISO_REQUEST(coding, charset_id) \
433 ((charset_id <= (coding)->max_charset_id \
434 ? (coding)->safe_charsets[charset_id] \
435 : -1))
436
437
438#define CODING_ISO_FLAGS(coding) \
439 ((coding)->spec.iso_2022.flags)
440#define CODING_ISO_DESIGNATION(coding, reg) \
441 ((coding)->spec.iso_2022.current_designation[reg])
442#define CODING_ISO_INVOCATION(coding, plane) \
443 ((coding)->spec.iso_2022.current_invocation[plane])
444#define CODING_ISO_SINGLE_SHIFTING(coding) \
445 ((coding)->spec.iso_2022.single_shifting)
446#define CODING_ISO_BOL(coding) \
447 ((coding)->spec.iso_2022.bol)
448#define CODING_ISO_INVOKED_CHARSET(coding, plane) \
449 CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane)))
450
451/* Control characters of ISO2022. */
452 /* code */ /* function */
453#define ISO_CODE_LF 0x0A /* line-feed */
454#define ISO_CODE_CR 0x0D /* carriage-return */
455#define ISO_CODE_SO 0x0E /* shift-out */
456#define ISO_CODE_SI 0x0F /* shift-in */
457#define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */
458#define ISO_CODE_ESC 0x1B /* escape */
459#define ISO_CODE_SS2 0x8E /* single-shift-2 */
460#define ISO_CODE_SS3 0x8F /* single-shift-3 */
461#define ISO_CODE_CSI 0x9B /* control-sequence-introducer */
462
463/* All code (1-byte) of ISO2022 is classified into one of the
464 followings. */
465enum iso_code_class_type
466 {
467 ISO_control_0, /* Control codes in the range
468 0x00..0x1F and 0x7F, except for the
469 following 5 codes. */
df7492f9
KH
470 ISO_shift_out, /* ISO_CODE_SO (0x0E) */
471 ISO_shift_in, /* ISO_CODE_SI (0x0F) */
472 ISO_single_shift_2_7, /* ISO_CODE_SS2_7 (0x19) */
473 ISO_escape, /* ISO_CODE_SO (0x1B) */
474 ISO_control_1, /* Control codes in the range
475 0x80..0x9F, except for the
476 following 3 codes. */
477 ISO_single_shift_2, /* ISO_CODE_SS2 (0x8E) */
478 ISO_single_shift_3, /* ISO_CODE_SS3 (0x8F) */
479 ISO_control_sequence_introducer, /* ISO_CODE_CSI (0x9B) */
480 ISO_0x20_or_0x7F, /* Codes of the values 0x20 or 0x7F. */
481 ISO_graphic_plane_0, /* Graphic codes in the range 0x21..0x7E. */
482 ISO_0xA0_or_0xFF, /* Codes of the values 0xA0 or 0xFF. */
483 ISO_graphic_plane_1 /* Graphic codes in the range 0xA1..0xFE. */
484 };
05e6f5dc 485
df7492f9
KH
486/** The macros CODING_ISO_FLAG_XXX defines a flag bit of the
487 `iso-flags' attribute of an iso2022 coding system. */
05e6f5dc 488
df7492f9
KH
489/* If set, produce long-form designation sequence (e.g. ESC $ ( A)
490 instead of the correct short-form sequence (e.g. ESC $ A). */
491#define CODING_ISO_FLAG_LONG_FORM 0x0001
93dec019 492
df7492f9
KH
493/* If set, reset graphic planes and registers at end-of-line to the
494 initial state. */
495#define CODING_ISO_FLAG_RESET_AT_EOL 0x0002
05e6f5dc 496
df7492f9
KH
497/* If set, reset graphic planes and registers before any control
498 characters to the initial state. */
499#define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004
05e6f5dc 500
df7492f9
KH
501/* If set, encode by 7-bit environment. */
502#define CODING_ISO_FLAG_SEVEN_BITS 0x0008
4ed46869 503
df7492f9
KH
504/* If set, use locking-shift function. */
505#define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010
b73bfc1c 506
df7492f9
KH
507/* If set, use single-shift function. Overwrite
508 CODING_ISO_FLAG_LOCKING_SHIFT. */
509#define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020
b73bfc1c 510
df7492f9
KH
511/* If set, use designation escape sequence. */
512#define CODING_ISO_FLAG_DESIGNATION 0x0040
b73bfc1c 513
df7492f9
KH
514/* If set, produce revision number sequence. */
515#define CODING_ISO_FLAG_REVISION 0x0080
b73bfc1c 516
df7492f9
KH
517/* If set, produce ISO6429's direction specifying sequence. */
518#define CODING_ISO_FLAG_DIRECTION 0x0100
f4dee582 519
df7492f9
KH
520/* If set, assume designation states are reset at beginning of line on
521 output. */
522#define CODING_ISO_FLAG_INIT_AT_BOL 0x0200
4ed46869 523
df7492f9
KH
524/* If set, designation sequence should be placed at beginning of line
525 on output. */
526#define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400
aa72b389 527
df7492f9
KH
528/* If set, do not encode unsafe charactes on output. */
529#define CODING_ISO_FLAG_SAFE 0x0800
aa72b389 530
df7492f9
KH
531/* If set, extra latin codes (128..159) are accepted as a valid code
532 on input. */
533#define CODING_ISO_FLAG_LATIN_EXTRA 0x1000
aa72b389 534
df7492f9 535#define CODING_ISO_FLAG_COMPOSITION 0x2000
aa72b389 536
df7492f9 537#define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000
aa72b389 538
bf16eb23 539#define CODING_ISO_FLAG_USE_ROMAN 0x8000
aa72b389 540
bf16eb23 541#define CODING_ISO_FLAG_USE_OLDJIS 0x10000
aa72b389 542
bf16eb23 543#define CODING_ISO_FLAG_FULL_SUPPORT 0x100000
aa72b389 544
df7492f9
KH
545/* A character to be produced on output if encoding of the original
546 character is prohibited by CODING_ISO_FLAG_SAFE. */
547#define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?'
aa72b389 548
4ed46869 549
df7492f9
KH
550/* UTF-16 section */
551#define CODING_UTF_16_BOM(coding) \
552 ((coding)->spec.utf_16.bom)
4ed46869 553
df7492f9
KH
554#define CODING_UTF_16_ENDIAN(coding) \
555 ((coding)->spec.utf_16.endian)
4ed46869 556
df7492f9
KH
557#define CODING_UTF_16_SURROGATE(coding) \
558 ((coding)->spec.utf_16.surrogate)
4ed46869 559
4ed46869 560
df7492f9
KH
561/* CCL section */
562#define CODING_CCL_DECODER(coding) \
563 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder)
564#define CODING_CCL_ENCODER(coding) \
565 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder)
566#define CODING_CCL_VALIDS(coding) \
8f924df7 567 (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids)))
4ed46869 568
5a936b46 569/* Index for each coding category in `coding_categories' */
4ed46869 570
df7492f9
KH
571enum coding_category
572 {
573 coding_category_iso_7,
574 coding_category_iso_7_tight,
575 coding_category_iso_8_1,
576 coding_category_iso_8_2,
577 coding_category_iso_7_else,
578 coding_category_iso_8_else,
579 coding_category_utf_8,
580 coding_category_utf_16_auto,
581 coding_category_utf_16_be,
582 coding_category_utf_16_le,
583 coding_category_utf_16_be_nosig,
584 coding_category_utf_16_le_nosig,
585 coding_category_charset,
586 coding_category_sjis,
587 coding_category_big5,
588 coding_category_ccl,
589 coding_category_emacs_mule,
590 /* All above are targets of code detection. */
591 coding_category_raw_text,
592 coding_category_undecided,
593 coding_category_max
594 };
595
596/* Definitions of flag bits used in detect_coding_XXXX. */
597#define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7)
598#define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight)
599#define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1)
600#define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2)
601#define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else)
602#define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else)
603#define CATEGORY_MASK_UTF_8 (1 << coding_category_utf_8)
b49a1807 604#define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto)
df7492f9
KH
605#define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be)
606#define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le)
607#define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig)
608#define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig)
609#define CATEGORY_MASK_CHARSET (1 << coding_category_charset)
610#define CATEGORY_MASK_SJIS (1 << coding_category_sjis)
611#define CATEGORY_MASK_BIG5 (1 << coding_category_big5)
612#define CATEGORY_MASK_CCL (1 << coding_category_ccl)
613#define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule)
ff0dacd7 614#define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text)
df7492f9
KH
615
616/* This value is returned if detect_coding_mask () find nothing other
617 than ASCII characters. */
618#define CATEGORY_MASK_ANY \
619 (CATEGORY_MASK_ISO_7 \
620 | CATEGORY_MASK_ISO_7_TIGHT \
621 | CATEGORY_MASK_ISO_8_1 \
622 | CATEGORY_MASK_ISO_8_2 \
623 | CATEGORY_MASK_ISO_7_ELSE \
624 | CATEGORY_MASK_ISO_8_ELSE \
625 | CATEGORY_MASK_UTF_8 \
626 | CATEGORY_MASK_UTF_16_BE \
627 | CATEGORY_MASK_UTF_16_LE \
628 | CATEGORY_MASK_UTF_16_BE_NOSIG \
629 | CATEGORY_MASK_UTF_16_LE_NOSIG \
630 | CATEGORY_MASK_CHARSET \
631 | CATEGORY_MASK_SJIS \
632 | CATEGORY_MASK_BIG5 \
633 | CATEGORY_MASK_CCL \
634 | CATEGORY_MASK_EMACS_MULE)
635
636
637#define CATEGORY_MASK_ISO_7BIT \
638 (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT)
639
640#define CATEGORY_MASK_ISO_8BIT \
641 (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2)
642
643#define CATEGORY_MASK_ISO_ELSE \
644 (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE)
645
646#define CATEGORY_MASK_ISO_ESCAPE \
647 (CATEGORY_MASK_ISO_7 \
648 | CATEGORY_MASK_ISO_7_TIGHT \
649 | CATEGORY_MASK_ISO_7_ELSE \
650 | CATEGORY_MASK_ISO_8_ELSE)
651
652#define CATEGORY_MASK_ISO \
653 ( CATEGORY_MASK_ISO_7BIT \
654 | CATEGORY_MASK_ISO_8BIT \
655 | CATEGORY_MASK_ISO_ELSE)
656
657#define CATEGORY_MASK_UTF_16 \
658 (CATEGORY_MASK_UTF_16_BE \
659 | CATEGORY_MASK_UTF_16_LE \
660 | CATEGORY_MASK_UTF_16_BE_NOSIG \
661 | CATEGORY_MASK_UTF_16_LE_NOSIG)
662
663
664/* List of symbols `coding-category-xxx' ordered by priority. This
665 variable is exposed to Emacs Lisp. */
666static Lisp_Object Vcoding_category_list;
667
668/* Table of coding categories (Lisp symbols). This variable is for
669 internal use oly. */
670static Lisp_Object Vcoding_category_table;
671
672/* Table of coding-categories ordered by priority. */
673static enum coding_category coding_priorities[coding_category_max];
674
675/* Nth element is a coding context for the coding system bound to the
676 Nth coding category. */
677static struct coding_system coding_categories[coding_category_max];
678
df7492f9
KH
679/*** Commonly used macros and functions ***/
680
681#ifndef min
682#define min(a, b) ((a) < (b) ? (a) : (b))
683#endif
684#ifndef max
685#define max(a, b) ((a) > (b) ? (a) : (b))
686#endif
4ed46869 687
24a73b0a
KH
688#define CODING_GET_INFO(coding, attrs, charset_list) \
689 do { \
690 (attrs) = CODING_ID_ATTRS ((coding)->id); \
691 (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \
df7492f9 692 } while (0)
4ed46869 693
4ed46869 694
df7492f9
KH
695/* Safely get one byte from the source text pointed by SRC which ends
696 at SRC_END, and set C to that byte. If there are not enough bytes
065e3595
KH
697 in the source, it jumps to `no_more_source'. If multibytep is
698 nonzero, and a multibyte character is found at SRC, set C to the
699 negative value of the character code. The caller should declare
700 and set these variables appropriately in advance:
701 src, src_end, multibytep */
aa72b389 702
065e3595
KH
703#define ONE_MORE_BYTE(c) \
704 do { \
705 if (src == src_end) \
706 { \
707 if (src_base < src) \
708 record_conversion_result \
709 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
710 goto no_more_source; \
711 } \
712 c = *src++; \
713 if (multibytep && (c & 0x80)) \
714 { \
715 if ((c & 0xFE) == 0xC0) \
716 c = ((c & 1) << 6) | *src++; \
717 else \
718 { \
35befdaa
KH
719 src--; \
720 c = - string_char (src, &src, NULL); \
065e3595
KH
721 record_conversion_result \
722 (coding, CODING_RESULT_INVALID_SRC); \
723 } \
724 } \
725 consumed_chars++; \
aa72b389
KH
726 } while (0)
727
aa72b389 728
065e3595
KH
729#define ONE_MORE_BYTE_NO_CHECK(c) \
730 do { \
731 c = *src++; \
732 if (multibytep && (c & 0x80)) \
733 { \
734 if ((c & 0xFE) == 0xC0) \
735 c = ((c & 1) << 6) | *src++; \
736 else \
737 { \
35befdaa
KH
738 src--; \
739 c = - string_char (src, &src, NULL); \
065e3595
KH
740 record_conversion_result \
741 (coding, CODING_RESULT_INVALID_SRC); \
742 } \
743 } \
744 consumed_chars++; \
aa72b389
KH
745 } while (0)
746
aa72b389 747
df7492f9
KH
748/* Store a byte C in the place pointed by DST and increment DST to the
749 next free point, and increment PRODUCED_CHARS. The caller should
750 assure that C is 0..127, and declare and set the variable `dst'
751 appropriately in advance.
752*/
aa72b389
KH
753
754
df7492f9
KH
755#define EMIT_ONE_ASCII_BYTE(c) \
756 do { \
757 produced_chars++; \
758 *dst++ = (c); \
b6871cc7 759 } while (0)
aa72b389
KH
760
761
df7492f9 762/* Like EMIT_ONE_ASCII_BYTE byt store two bytes; C1 and C2. */
aa72b389 763
df7492f9
KH
764#define EMIT_TWO_ASCII_BYTES(c1, c2) \
765 do { \
766 produced_chars += 2; \
767 *dst++ = (c1), *dst++ = (c2); \
768 } while (0)
aa72b389
KH
769
770
df7492f9
KH
771/* Store a byte C in the place pointed by DST and increment DST to the
772 next free point, and increment PRODUCED_CHARS. If MULTIBYTEP is
773 nonzero, store in an appropriate multibyte from. The caller should
774 declare and set the variables `dst' and `multibytep' appropriately
775 in advance. */
776
777#define EMIT_ONE_BYTE(c) \
778 do { \
779 produced_chars++; \
780 if (multibytep) \
781 { \
782 int ch = (c); \
783 if (ch >= 0x80) \
784 ch = BYTE8_TO_CHAR (ch); \
785 CHAR_STRING_ADVANCE (ch, dst); \
786 } \
787 else \
788 *dst++ = (c); \
aa72b389 789 } while (0)
aa72b389 790
aa72b389 791
df7492f9 792/* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */
aa72b389 793
e19c3639
KH
794#define EMIT_TWO_BYTES(c1, c2) \
795 do { \
796 produced_chars += 2; \
797 if (multibytep) \
798 { \
799 int ch; \
800 \
801 ch = (c1); \
802 if (ch >= 0x80) \
803 ch = BYTE8_TO_CHAR (ch); \
804 CHAR_STRING_ADVANCE (ch, dst); \
805 ch = (c2); \
806 if (ch >= 0x80) \
807 ch = BYTE8_TO_CHAR (ch); \
808 CHAR_STRING_ADVANCE (ch, dst); \
809 } \
810 else \
811 { \
812 *dst++ = (c1); \
813 *dst++ = (c2); \
814 } \
aa72b389
KH
815 } while (0)
816
817
df7492f9
KH
818#define EMIT_THREE_BYTES(c1, c2, c3) \
819 do { \
820 EMIT_ONE_BYTE (c1); \
821 EMIT_TWO_BYTES (c2, c3); \
822 } while (0)
aa72b389 823
aa72b389 824
df7492f9
KH
825#define EMIT_FOUR_BYTES(c1, c2, c3, c4) \
826 do { \
827 EMIT_TWO_BYTES (c1, c2); \
828 EMIT_TWO_BYTES (c3, c4); \
829 } while (0)
aa72b389 830
aa72b389 831
f6cbaf43
KH
832/* Prototypes for static functions. */
833static void record_conversion_result P_ ((struct coding_system *coding,
834 enum coding_result_code result));
835static int detect_coding_utf_8 P_ ((struct coding_system *,
836 struct coding_detection_info *info));
837static void decode_coding_utf_8 P_ ((struct coding_system *));
838static int encode_coding_utf_8 P_ ((struct coding_system *));
839
840static int detect_coding_utf_16 P_ ((struct coding_system *,
841 struct coding_detection_info *info));
842static void decode_coding_utf_16 P_ ((struct coding_system *));
843static int encode_coding_utf_16 P_ ((struct coding_system *));
844
845static int detect_coding_iso_2022 P_ ((struct coding_system *,
846 struct coding_detection_info *info));
847static void decode_coding_iso_2022 P_ ((struct coding_system *));
848static int encode_coding_iso_2022 P_ ((struct coding_system *));
849
850static int detect_coding_emacs_mule P_ ((struct coding_system *,
851 struct coding_detection_info *info));
852static void decode_coding_emacs_mule P_ ((struct coding_system *));
853static int encode_coding_emacs_mule P_ ((struct coding_system *));
854
855static int detect_coding_sjis P_ ((struct coding_system *,
856 struct coding_detection_info *info));
857static void decode_coding_sjis P_ ((struct coding_system *));
858static int encode_coding_sjis P_ ((struct coding_system *));
859
860static int detect_coding_big5 P_ ((struct coding_system *,
861 struct coding_detection_info *info));
862static void decode_coding_big5 P_ ((struct coding_system *));
863static int encode_coding_big5 P_ ((struct coding_system *));
864
865static int detect_coding_ccl P_ ((struct coding_system *,
866 struct coding_detection_info *info));
867static void decode_coding_ccl P_ ((struct coding_system *));
868static int encode_coding_ccl P_ ((struct coding_system *));
869
870static void decode_coding_raw_text P_ ((struct coding_system *));
871static int encode_coding_raw_text P_ ((struct coding_system *));
872
873static void coding_set_source P_ ((struct coding_system *));
874static void coding_set_destination P_ ((struct coding_system *));
875static void coding_alloc_by_realloc P_ ((struct coding_system *, EMACS_INT));
876static void coding_alloc_by_making_gap P_ ((struct coding_system *,
287c57d7 877 EMACS_INT, EMACS_INT));
f6cbaf43
KH
878static unsigned char *alloc_destination P_ ((struct coding_system *,
879 EMACS_INT, unsigned char *));
880static void setup_iso_safe_charsets P_ ((Lisp_Object));
881static unsigned char *encode_designation_at_bol P_ ((struct coding_system *,
882 int *, int *,
883 unsigned char *));
884static int detect_eol P_ ((const unsigned char *,
885 EMACS_INT, enum coding_category));
886static Lisp_Object adjust_coding_eol_type P_ ((struct coding_system *, int));
887static void decode_eol P_ ((struct coding_system *));
888static Lisp_Object get_translation_table P_ ((Lisp_Object, int, int *));
889static Lisp_Object get_translation P_ ((Lisp_Object, int *, int *,
890 int, int *, int *));
891static int produce_chars P_ ((struct coding_system *, Lisp_Object, int));
892static INLINE void produce_composition P_ ((struct coding_system *, int *,
893 EMACS_INT));
894static INLINE void produce_charset P_ ((struct coding_system *, int *,
895 EMACS_INT));
896static void produce_annotation P_ ((struct coding_system *, EMACS_INT));
897static int decode_coding P_ ((struct coding_system *));
898static INLINE int *handle_composition_annotation P_ ((EMACS_INT, EMACS_INT,
899 struct coding_system *,
900 int *, EMACS_INT *));
901static INLINE int *handle_charset_annotation P_ ((EMACS_INT, EMACS_INT,
902 struct coding_system *,
903 int *, EMACS_INT *));
904static void consume_chars P_ ((struct coding_system *, Lisp_Object, int));
905static int encode_coding P_ ((struct coding_system *));
906static Lisp_Object make_conversion_work_buffer P_ ((int));
907static Lisp_Object code_conversion_restore P_ ((Lisp_Object));
908static INLINE int char_encodable_p P_ ((int, Lisp_Object));
909static Lisp_Object make_subsidiaries P_ ((Lisp_Object));
910
065e3595
KH
911static void
912record_conversion_result (struct coding_system *coding,
913 enum coding_result_code result)
914{
915 coding->result = result;
916 switch (result)
917 {
918 case CODING_RESULT_INSUFFICIENT_SRC:
919 Vlast_code_conversion_error = Qinsufficient_source;
920 break;
921 case CODING_RESULT_INCONSISTENT_EOL:
922 Vlast_code_conversion_error = Qinconsistent_eol;
923 break;
924 case CODING_RESULT_INVALID_SRC:
925 Vlast_code_conversion_error = Qinvalid_source;
926 break;
927 case CODING_RESULT_INTERRUPT:
928 Vlast_code_conversion_error = Qinterrupted;
929 break;
930 case CODING_RESULT_INSUFFICIENT_MEM:
931 Vlast_code_conversion_error = Qinsufficient_memory;
932 break;
35befdaa
KH
933 default:
934 Vlast_code_conversion_error = intern ("Unknown error");
065e3595
KH
935 }
936}
937
df7492f9
KH
938#define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \
939 do { \
940 charset_map_loaded = 0; \
941 c = DECODE_CHAR (charset, code); \
942 if (charset_map_loaded) \
943 { \
8f924df7 944 const unsigned char *orig = coding->source; \
df7492f9
KH
945 EMACS_INT offset; \
946 \
947 coding_set_source (coding); \
948 offset = coding->source - orig; \
949 src += offset; \
950 src_base += offset; \
951 src_end += offset; \
952 } \
aa72b389
KH
953 } while (0)
954
955
df7492f9
KH
956#define ASSURE_DESTINATION(bytes) \
957 do { \
958 if (dst + (bytes) >= dst_end) \
959 { \
960 int more_bytes = charbuf_end - charbuf + (bytes); \
961 \
962 dst = alloc_destination (coding, more_bytes, dst); \
963 dst_end = coding->destination + coding->dst_bytes; \
964 } \
965 } while (0)
aa72b389 966
aa72b389 967
aa72b389 968
df7492f9
KH
969static void
970coding_set_source (coding)
aa72b389 971 struct coding_system *coding;
aa72b389 972{
df7492f9
KH
973 if (BUFFERP (coding->src_object))
974 {
2cb26057 975 struct buffer *buf = XBUFFER (coding->src_object);
aa72b389 976
df7492f9 977 if (coding->src_pos < 0)
2cb26057 978 coding->source = BUF_GAP_END_ADDR (buf) + coding->src_pos_byte;
df7492f9 979 else
2cb26057 980 coding->source = BUF_BYTE_ADDRESS (buf, coding->src_pos_byte);
aa72b389 981 }
df7492f9 982 else if (STRINGP (coding->src_object))
aa72b389 983 {
8f924df7 984 coding->source = SDATA (coding->src_object) + coding->src_pos_byte;
aa72b389 985 }
df7492f9
KH
986 else
987 /* Otherwise, the source is C string and is never relocated
988 automatically. Thus we don't have to update anything. */
989 ;
990}
aa72b389 991
df7492f9
KH
992static void
993coding_set_destination (coding)
994 struct coding_system *coding;
995{
996 if (BUFFERP (coding->dst_object))
aa72b389 997 {
df7492f9 998 if (coding->src_pos < 0)
aa72b389 999 {
28f67a95
KH
1000 coding->destination = BEG_ADDR + coding->dst_pos_byte - 1;
1001 coding->dst_bytes = (GAP_END_ADDR
1002 - (coding->src_bytes - coding->consumed)
1003 - coding->destination);
aa72b389 1004 }
df7492f9 1005 else
28f67a95
KH
1006 {
1007 /* We are sure that coding->dst_pos_byte is before the gap
1008 of the buffer. */
1009 coding->destination = (BUF_BEG_ADDR (XBUFFER (coding->dst_object))
1010 + coding->dst_pos_byte - 1);
1011 coding->dst_bytes = (BUF_GAP_END_ADDR (XBUFFER (coding->dst_object))
1012 - coding->destination);
1013 }
df7492f9
KH
1014 }
1015 else
1016 /* Otherwise, the destination is C string and is never relocated
1017 automatically. Thus we don't have to update anything. */
1018 ;
1019}
1020
1021
1022static void
1023coding_alloc_by_realloc (coding, bytes)
1024 struct coding_system *coding;
1025 EMACS_INT bytes;
1026{
1027 coding->destination = (unsigned char *) xrealloc (coding->destination,
1028 coding->dst_bytes + bytes);
1029 coding->dst_bytes += bytes;
1030}
1031
1032static void
287c57d7 1033coding_alloc_by_making_gap (coding, offset, bytes)
df7492f9 1034 struct coding_system *coding;
287c57d7 1035 EMACS_INT offset, bytes;
df7492f9 1036{
2c78b7e1
KH
1037 if (BUFFERP (coding->dst_object)
1038 && EQ (coding->src_object, coding->dst_object))
df7492f9 1039 {
287c57d7 1040 EMACS_INT add = offset + (coding->src_bytes - coding->consumed);
df7492f9 1041
287c57d7 1042 GPT += offset, GPT_BYTE += offset;
df7492f9
KH
1043 GAP_SIZE -= add; ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add;
1044 make_gap (bytes);
1045 GAP_SIZE += add; ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add;
287c57d7 1046 GPT -= offset, GPT_BYTE -= offset;
df7492f9 1047 }
730fff51 1048 else
df7492f9 1049 {
2c78b7e1
KH
1050 Lisp_Object this_buffer;
1051
1052 this_buffer = Fcurrent_buffer ();
df7492f9
KH
1053 set_buffer_internal (XBUFFER (coding->dst_object));
1054 make_gap (bytes);
1055 set_buffer_internal (XBUFFER (this_buffer));
aa72b389 1056 }
df7492f9 1057}
8f924df7 1058
df7492f9
KH
1059
1060static unsigned char *
1061alloc_destination (coding, nbytes, dst)
1062 struct coding_system *coding;
3e139625 1063 EMACS_INT nbytes;
df7492f9
KH
1064 unsigned char *dst;
1065{
1066 EMACS_INT offset = dst - coding->destination;
1067
1068 if (BUFFERP (coding->dst_object))
287c57d7 1069 coding_alloc_by_making_gap (coding, offset, nbytes);
aa72b389 1070 else
df7492f9 1071 coding_alloc_by_realloc (coding, nbytes);
065e3595 1072 record_conversion_result (coding, CODING_RESULT_SUCCESS);
df7492f9
KH
1073 coding_set_destination (coding);
1074 dst = coding->destination + offset;
1075 return dst;
1076}
aa72b389 1077
ff0dacd7
KH
1078/** Macros for annotations. */
1079
1080/* Maximum length of annotation data (sum of annotations for
1081 composition and charset). */
69a80ea3 1082#define MAX_ANNOTATION_LENGTH (4 + (MAX_COMPOSITION_COMPONENTS * 2) - 1 + 4)
ff0dacd7
KH
1083
1084/* An annotation data is stored in the array coding->charbuf in this
1085 format:
69a80ea3 1086 [ -LENGTH ANNOTATION_MASK NCHARS ... ]
ff0dacd7
KH
1087 LENGTH is the number of elements in the annotation.
1088 ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK.
69a80ea3 1089 NCHARS is the number of characters in the text annotated.
ff0dacd7
KH
1090
1091 The format of the following elements depend on ANNOTATION_MASK.
1092
1093 In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements
1094 follows:
1095 ... METHOD [ COMPOSITION-COMPONENTS ... ]
1096 METHOD is one of enum composition_method.
1097 Optionnal COMPOSITION-COMPONENTS are characters and composition
1098 rules.
1099
1100 In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID
1101 follows. */
1102
69a80ea3 1103#define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \
ff0dacd7
KH
1104 do { \
1105 *(buf)++ = -(len); \
1106 *(buf)++ = (mask); \
69a80ea3 1107 *(buf)++ = (nchars); \
ff0dacd7
KH
1108 coding->annotated = 1; \
1109 } while (0);
1110
69a80ea3
KH
1111#define ADD_COMPOSITION_DATA(buf, nchars, method) \
1112 do { \
1113 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \
1114 *buf++ = method; \
ff0dacd7
KH
1115 } while (0)
1116
1117
69a80ea3
KH
1118#define ADD_CHARSET_DATA(buf, nchars, id) \
1119 do { \
1120 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \
1121 *buf++ = id; \
ff0dacd7
KH
1122 } while (0)
1123
df7492f9
KH
1124\f
1125/*** 2. Emacs' internal format (emacs-utf-8) ***/
1126
1127
1128
1129\f
1130/*** 3. UTF-8 ***/
1131
1132/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
ff0dacd7
KH
1133 Check if a text is encoded in UTF-8. If it is, return 1, else
1134 return 0. */
df7492f9
KH
1135
1136#define UTF_8_1_OCTET_P(c) ((c) < 0x80)
1137#define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
1138#define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
1139#define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
1140#define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
1141#define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
1142
1143static int
ff0dacd7 1144detect_coding_utf_8 (coding, detect_info)
df7492f9 1145 struct coding_system *coding;
ff0dacd7 1146 struct coding_detection_info *detect_info;
df7492f9 1147{
065e3595 1148 const unsigned char *src = coding->source, *src_base;
8f924df7 1149 const unsigned char *src_end = coding->source + coding->src_bytes;
df7492f9
KH
1150 int multibytep = coding->src_multibyte;
1151 int consumed_chars = 0;
1152 int found = 0;
1153
ff0dacd7 1154 detect_info->checked |= CATEGORY_MASK_UTF_8;
df7492f9
KH
1155 /* A coding system of this category is always ASCII compatible. */
1156 src += coding->head_ascii;
1157
1158 while (1)
aa72b389 1159 {
df7492f9 1160 int c, c1, c2, c3, c4;
aa72b389 1161
065e3595 1162 src_base = src;
df7492f9 1163 ONE_MORE_BYTE (c);
065e3595 1164 if (c < 0 || UTF_8_1_OCTET_P (c))
df7492f9
KH
1165 continue;
1166 ONE_MORE_BYTE (c1);
065e3595 1167 if (c1 < 0 || ! UTF_8_EXTRA_OCTET_P (c1))
df7492f9
KH
1168 break;
1169 if (UTF_8_2_OCTET_LEADING_P (c))
aa72b389 1170 {
ff0dacd7 1171 found = CATEGORY_MASK_UTF_8;
df7492f9 1172 continue;
aa72b389 1173 }
df7492f9 1174 ONE_MORE_BYTE (c2);
065e3595 1175 if (c2 < 0 || ! UTF_8_EXTRA_OCTET_P (c2))
df7492f9
KH
1176 break;
1177 if (UTF_8_3_OCTET_LEADING_P (c))
aa72b389 1178 {
ff0dacd7 1179 found = CATEGORY_MASK_UTF_8;
df7492f9 1180 continue;
aa72b389 1181 }
df7492f9 1182 ONE_MORE_BYTE (c3);
065e3595 1183 if (c3 < 0 || ! UTF_8_EXTRA_OCTET_P (c3))
df7492f9
KH
1184 break;
1185 if (UTF_8_4_OCTET_LEADING_P (c))
aa72b389 1186 {
ff0dacd7 1187 found = CATEGORY_MASK_UTF_8;
df7492f9
KH
1188 continue;
1189 }
1190 ONE_MORE_BYTE (c4);
065e3595 1191 if (c4 < 0 || ! UTF_8_EXTRA_OCTET_P (c4))
df7492f9
KH
1192 break;
1193 if (UTF_8_5_OCTET_LEADING_P (c))
1194 {
ff0dacd7 1195 found = CATEGORY_MASK_UTF_8;
df7492f9
KH
1196 continue;
1197 }
1198 break;
aa72b389 1199 }
ff0dacd7 1200 detect_info->rejected |= CATEGORY_MASK_UTF_8;
df7492f9 1201 return 0;
aa72b389 1202
df7492f9 1203 no_more_source:
065e3595 1204 if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK)
aa72b389 1205 {
ff0dacd7 1206 detect_info->rejected |= CATEGORY_MASK_UTF_8;
89528eb3 1207 return 0;
aa72b389 1208 }
ff0dacd7
KH
1209 detect_info->found |= found;
1210 return 1;
aa72b389
KH
1211}
1212
4ed46869 1213
b73bfc1c 1214static void
df7492f9 1215decode_coding_utf_8 (coding)
b73bfc1c 1216 struct coding_system *coding;
b73bfc1c 1217{
8f924df7
KH
1218 const unsigned char *src = coding->source + coding->consumed;
1219 const unsigned char *src_end = coding->source + coding->src_bytes;
1220 const unsigned char *src_base;
69a80ea3
KH
1221 int *charbuf = coding->charbuf + coding->charbuf_used;
1222 int *charbuf_end = coding->charbuf + coding->charbuf_size;
df7492f9
KH
1223 int consumed_chars = 0, consumed_chars_base;
1224 int multibytep = coding->src_multibyte;
24a73b0a 1225 Lisp_Object attr, charset_list;
4ed46869 1226
24a73b0a 1227 CODING_GET_INFO (coding, attr, charset_list);
df7492f9
KH
1228
1229 while (1)
b73bfc1c 1230 {
df7492f9 1231 int c, c1, c2, c3, c4, c5;
ec6d2bb8 1232
df7492f9
KH
1233 src_base = src;
1234 consumed_chars_base = consumed_chars;
4af310db 1235
df7492f9
KH
1236 if (charbuf >= charbuf_end)
1237 break;
1238
1239 ONE_MORE_BYTE (c1);
065e3595
KH
1240 if (c1 < 0)
1241 {
1242 c = - c1;
1243 }
1244 else if (UTF_8_1_OCTET_P(c1))
df7492f9
KH
1245 {
1246 c = c1;
4af310db 1247 }
df7492f9 1248 else
4af310db 1249 {
df7492f9 1250 ONE_MORE_BYTE (c2);
065e3595 1251 if (c2 < 0 || ! UTF_8_EXTRA_OCTET_P (c2))
df7492f9
KH
1252 goto invalid_code;
1253 if (UTF_8_2_OCTET_LEADING_P (c1))
4af310db 1254 {
b0edb2c5
DL
1255 c = ((c1 & 0x1F) << 6) | (c2 & 0x3F);
1256 /* Reject overlong sequences here and below. Encoders
1257 producing them are incorrect, they can be misleading,
1258 and they mess up read/write invariance. */
1259 if (c < 128)
1260 goto invalid_code;
4af310db 1261 }
df7492f9 1262 else
aa72b389 1263 {
df7492f9 1264 ONE_MORE_BYTE (c3);
065e3595 1265 if (c3 < 0 || ! UTF_8_EXTRA_OCTET_P (c3))
df7492f9
KH
1266 goto invalid_code;
1267 if (UTF_8_3_OCTET_LEADING_P (c1))
b0edb2c5
DL
1268 {
1269 c = (((c1 & 0xF) << 12)
1270 | ((c2 & 0x3F) << 6) | (c3 & 0x3F));
72fe1301
DL
1271 if (c < 0x800
1272 || (c >= 0xd800 && c < 0xe000)) /* surrogates (invalid) */
b0edb2c5
DL
1273 goto invalid_code;
1274 }
df7492f9
KH
1275 else
1276 {
1277 ONE_MORE_BYTE (c4);
065e3595 1278 if (c4 < 0 || ! UTF_8_EXTRA_OCTET_P (c4))
df7492f9
KH
1279 goto invalid_code;
1280 if (UTF_8_4_OCTET_LEADING_P (c1))
b0edb2c5 1281 {
df7492f9
KH
1282 c = (((c1 & 0x7) << 18) | ((c2 & 0x3F) << 12)
1283 | ((c3 & 0x3F) << 6) | (c4 & 0x3F));
b0edb2c5
DL
1284 if (c < 0x10000)
1285 goto invalid_code;
1286 }
df7492f9
KH
1287 else
1288 {
1289 ONE_MORE_BYTE (c5);
065e3595 1290 if (c5 < 0 || ! UTF_8_EXTRA_OCTET_P (c5))
df7492f9
KH
1291 goto invalid_code;
1292 if (UTF_8_5_OCTET_LEADING_P (c1))
1293 {
1294 c = (((c1 & 0x3) << 24) | ((c2 & 0x3F) << 18)
1295 | ((c3 & 0x3F) << 12) | ((c4 & 0x3F) << 6)
1296 | (c5 & 0x3F));
b0edb2c5 1297 if ((c > MAX_CHAR) || (c < 0x200000))
df7492f9
KH
1298 goto invalid_code;
1299 }
1300 else
1301 goto invalid_code;
1302 }
1303 }
aa72b389 1304 }
b73bfc1c 1305 }
df7492f9
KH
1306
1307 *charbuf++ = c;
1308 continue;
1309
1310 invalid_code:
1311 src = src_base;
1312 consumed_chars = consumed_chars_base;
1313 ONE_MORE_BYTE (c);
1314 *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
1315 coding->errors++;
aa72b389
KH
1316 }
1317
df7492f9
KH
1318 no_more_source:
1319 coding->consumed_char += consumed_chars_base;
1320 coding->consumed = src_base - coding->source;
1321 coding->charbuf_used = charbuf - coding->charbuf;
1322}
1323
1324
1325static int
1326encode_coding_utf_8 (coding)
1327 struct coding_system *coding;
1328{
1329 int multibytep = coding->dst_multibyte;
1330 int *charbuf = coding->charbuf;
1331 int *charbuf_end = charbuf + coding->charbuf_used;
1332 unsigned char *dst = coding->destination + coding->produced;
1333 unsigned char *dst_end = coding->destination + coding->dst_bytes;
e19c3639 1334 int produced_chars = 0;
df7492f9
KH
1335 int c;
1336
1337 if (multibytep)
aa72b389 1338 {
df7492f9
KH
1339 int safe_room = MAX_MULTIBYTE_LENGTH * 2;
1340
1341 while (charbuf < charbuf_end)
b73bfc1c 1342 {
df7492f9 1343 unsigned char str[MAX_MULTIBYTE_LENGTH], *p, *pend = str;
8f924df7 1344
df7492f9
KH
1345 ASSURE_DESTINATION (safe_room);
1346 c = *charbuf++;
28f67a95
KH
1347 if (CHAR_BYTE8_P (c))
1348 {
1349 c = CHAR_TO_BYTE8 (c);
1350 EMIT_ONE_BYTE (c);
1351 }
1352 else
1353 {
1354 CHAR_STRING_ADVANCE (c, pend);
1355 for (p = str; p < pend; p++)
1356 EMIT_ONE_BYTE (*p);
1357 }
b73bfc1c 1358 }
aa72b389 1359 }
df7492f9
KH
1360 else
1361 {
1362 int safe_room = MAX_MULTIBYTE_LENGTH;
1363
1364 while (charbuf < charbuf_end)
b73bfc1c 1365 {
df7492f9
KH
1366 ASSURE_DESTINATION (safe_room);
1367 c = *charbuf++;
f03caae0
KH
1368 if (CHAR_BYTE8_P (c))
1369 *dst++ = CHAR_TO_BYTE8 (c);
1370 else
1371 dst += CHAR_STRING (c, dst);
df7492f9 1372 produced_chars++;
4ed46869
KH
1373 }
1374 }
065e3595 1375 record_conversion_result (coding, CODING_RESULT_SUCCESS);
df7492f9
KH
1376 coding->produced_char += produced_chars;
1377 coding->produced = dst - coding->destination;
1378 return 0;
4ed46869
KH
1379}
1380
b73bfc1c 1381
df7492f9 1382/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
ff0dacd7
KH
1383 Check if a text is encoded in one of UTF-16 based coding systems.
1384 If it is, return 1, else return 0. */
aa72b389 1385
df7492f9
KH
1386#define UTF_16_HIGH_SURROGATE_P(val) \
1387 (((val) & 0xFC00) == 0xD800)
1388
1389#define UTF_16_LOW_SURROGATE_P(val) \
1390 (((val) & 0xFC00) == 0xDC00)
93dec019 1391
df7492f9
KH
1392#define UTF_16_INVALID_P(val) \
1393 (((val) == 0xFFFE) \
1394 || ((val) == 0xFFFF) \
1395 || UTF_16_LOW_SURROGATE_P (val))
aa72b389 1396
aa72b389 1397
df7492f9 1398static int
ff0dacd7 1399detect_coding_utf_16 (coding, detect_info)
aa72b389 1400 struct coding_system *coding;
ff0dacd7 1401 struct coding_detection_info *detect_info;
aa72b389 1402{
8f924df7
KH
1403 const unsigned char *src = coding->source, *src_base = src;
1404 const unsigned char *src_end = coding->source + coding->src_bytes;
df7492f9
KH
1405 int multibytep = coding->src_multibyte;
1406 int consumed_chars = 0;
1407 int c1, c2;
aa72b389 1408
ff0dacd7 1409 detect_info->checked |= CATEGORY_MASK_UTF_16;
ff0dacd7 1410 if (coding->mode & CODING_MODE_LAST_BLOCK
24a73b0a 1411 && (coding->src_chars & 1))
ff0dacd7
KH
1412 {
1413 detect_info->rejected |= CATEGORY_MASK_UTF_16;
1414 return 0;
1415 }
24a73b0a 1416
df7492f9
KH
1417 ONE_MORE_BYTE (c1);
1418 ONE_MORE_BYTE (c2);
df7492f9 1419 if ((c1 == 0xFF) && (c2 == 0xFE))
aa72b389 1420 {
b49a1807
KH
1421 detect_info->found |= (CATEGORY_MASK_UTF_16_LE
1422 | CATEGORY_MASK_UTF_16_AUTO);
24a73b0a
KH
1423 detect_info->rejected |= (CATEGORY_MASK_UTF_16_BE
1424 | CATEGORY_MASK_UTF_16_BE_NOSIG
1425 | CATEGORY_MASK_UTF_16_LE_NOSIG);
aa72b389 1426 }
df7492f9 1427 else if ((c1 == 0xFE) && (c2 == 0xFF))
ff0dacd7 1428 {
b49a1807
KH
1429 detect_info->found |= (CATEGORY_MASK_UTF_16_BE
1430 | CATEGORY_MASK_UTF_16_AUTO);
24a73b0a
KH
1431 detect_info->rejected |= (CATEGORY_MASK_UTF_16_LE
1432 | CATEGORY_MASK_UTF_16_BE_NOSIG
1433 | CATEGORY_MASK_UTF_16_LE_NOSIG);
1434 }
065e3595 1435 else if (c1 >= 0 && c2 >= 0)
24a73b0a 1436 {
24a73b0a
KH
1437 detect_info->rejected
1438 |= (CATEGORY_MASK_UTF_16_BE | CATEGORY_MASK_UTF_16_LE);
ff0dacd7 1439 }
df7492f9 1440 no_more_source:
ff0dacd7 1441 return 1;
df7492f9 1442}
aa72b389 1443
df7492f9
KH
1444static void
1445decode_coding_utf_16 (coding)
1446 struct coding_system *coding;
1447{
8f924df7
KH
1448 const unsigned char *src = coding->source + coding->consumed;
1449 const unsigned char *src_end = coding->source + coding->src_bytes;
1450 const unsigned char *src_base;
69a80ea3
KH
1451 int *charbuf = coding->charbuf + coding->charbuf_used;
1452 int *charbuf_end = coding->charbuf + coding->charbuf_size;
df7492f9
KH
1453 int consumed_chars = 0, consumed_chars_base;
1454 int multibytep = coding->src_multibyte;
1455 enum utf_16_bom_type bom = CODING_UTF_16_BOM (coding);
1456 enum utf_16_endian_type endian = CODING_UTF_16_ENDIAN (coding);
1457 int surrogate = CODING_UTF_16_SURROGATE (coding);
24a73b0a 1458 Lisp_Object attr, charset_list;
df7492f9 1459
24a73b0a 1460 CODING_GET_INFO (coding, attr, charset_list);
df7492f9 1461
b49a1807 1462 if (bom == utf_16_with_bom)
aa72b389 1463 {
df7492f9 1464 int c, c1, c2;
4af310db 1465
aa72b389 1466 src_base = src;
df7492f9
KH
1467 ONE_MORE_BYTE (c1);
1468 ONE_MORE_BYTE (c2);
e19c3639 1469 c = (c1 << 8) | c2;
aa72b389 1470
b49a1807
KH
1471 if (endian == utf_16_big_endian
1472 ? c != 0xFEFF : c != 0xFFFE)
aa72b389 1473 {
b49a1807
KH
1474 /* The first two bytes are not BOM. Treat them as bytes
1475 for a normal character. */
1476 src = src_base;
1477 coding->errors++;
aa72b389 1478 }
b49a1807
KH
1479 CODING_UTF_16_BOM (coding) = utf_16_without_bom;
1480 }
1481 else if (bom == utf_16_detect_bom)
1482 {
1483 /* We have already tried to detect BOM and failed in
1484 detect_coding. */
1485 CODING_UTF_16_BOM (coding) = utf_16_without_bom;
df7492f9 1486 }
aa72b389 1487
df7492f9
KH
1488 while (1)
1489 {
1490 int c, c1, c2;
1491
1492 src_base = src;
1493 consumed_chars_base = consumed_chars;
1494
1495 if (charbuf + 2 >= charbuf_end)
1496 break;
1497
1498 ONE_MORE_BYTE (c1);
065e3595
KH
1499 if (c1 < 0)
1500 {
1501 *charbuf++ = -c1;
1502 continue;
1503 }
df7492f9 1504 ONE_MORE_BYTE (c2);
065e3595
KH
1505 if (c2 < 0)
1506 {
1507 *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1);
1508 *charbuf++ = -c2;
1509 continue;
1510 }
df7492f9 1511 c = (endian == utf_16_big_endian
e19c3639 1512 ? ((c1 << 8) | c2) : ((c2 << 8) | c1));
df7492f9 1513 if (surrogate)
fd3ae0b9 1514 {
df7492f9 1515 if (! UTF_16_LOW_SURROGATE_P (c))
fd3ae0b9 1516 {
df7492f9
KH
1517 if (endian == utf_16_big_endian)
1518 c1 = surrogate >> 8, c2 = surrogate & 0xFF;
1519 else
1520 c1 = surrogate & 0xFF, c2 = surrogate >> 8;
1521 *charbuf++ = c1;
1522 *charbuf++ = c2;
1523 coding->errors++;
1524 if (UTF_16_HIGH_SURROGATE_P (c))
1525 CODING_UTF_16_SURROGATE (coding) = surrogate = c;
fd3ae0b9 1526 else
df7492f9 1527 *charbuf++ = c;
fd3ae0b9
KH
1528 }
1529 else
df7492f9
KH
1530 {
1531 c = ((surrogate - 0xD800) << 10) | (c - 0xDC00);
1532 CODING_UTF_16_SURROGATE (coding) = surrogate = 0;
29f7ffd0 1533 *charbuf++ = 0x10000 + c;
df7492f9 1534 }
fd3ae0b9 1535 }
aa72b389 1536 else
df7492f9
KH
1537 {
1538 if (UTF_16_HIGH_SURROGATE_P (c))
1539 CODING_UTF_16_SURROGATE (coding) = surrogate = c;
1540 else
1541 *charbuf++ = c;
8f924df7 1542 }
aa72b389 1543 }
df7492f9
KH
1544
1545 no_more_source:
1546 coding->consumed_char += consumed_chars_base;
1547 coding->consumed = src_base - coding->source;
1548 coding->charbuf_used = charbuf - coding->charbuf;
aa72b389 1549}
b73bfc1c 1550
df7492f9
KH
1551static int
1552encode_coding_utf_16 (coding)
1553 struct coding_system *coding;
1554{
1555 int multibytep = coding->dst_multibyte;
1556 int *charbuf = coding->charbuf;
1557 int *charbuf_end = charbuf + coding->charbuf_used;
1558 unsigned char *dst = coding->destination + coding->produced;
1559 unsigned char *dst_end = coding->destination + coding->dst_bytes;
1560 int safe_room = 8;
1561 enum utf_16_bom_type bom = CODING_UTF_16_BOM (coding);
1562 int big_endian = CODING_UTF_16_ENDIAN (coding) == utf_16_big_endian;
1563 int produced_chars = 0;
24a73b0a 1564 Lisp_Object attrs, charset_list;
df7492f9 1565 int c;
4ed46869 1566
24a73b0a 1567 CODING_GET_INFO (coding, attrs, charset_list);
df7492f9 1568
b49a1807 1569 if (bom != utf_16_without_bom)
df7492f9
KH
1570 {
1571 ASSURE_DESTINATION (safe_room);
1572 if (big_endian)
df7492f9 1573 EMIT_TWO_BYTES (0xFE, 0xFF);
880cf180
KH
1574 else
1575 EMIT_TWO_BYTES (0xFF, 0xFE);
df7492f9
KH
1576 CODING_UTF_16_BOM (coding) = utf_16_without_bom;
1577 }
1578
1579 while (charbuf < charbuf_end)
1580 {
1581 ASSURE_DESTINATION (safe_room);
1582 c = *charbuf++;
e19c3639
KH
1583 if (c >= MAX_UNICODE_CHAR)
1584 c = coding->default_char;
df7492f9
KH
1585
1586 if (c < 0x10000)
1587 {
1588 if (big_endian)
1589 EMIT_TWO_BYTES (c >> 8, c & 0xFF);
1590 else
1591 EMIT_TWO_BYTES (c & 0xFF, c >> 8);
1592 }
1593 else
1594 {
1595 int c1, c2;
1596
1597 c -= 0x10000;
1598 c1 = (c >> 10) + 0xD800;
1599 c2 = (c & 0x3FF) + 0xDC00;
1600 if (big_endian)
1601 EMIT_FOUR_BYTES (c1 >> 8, c1 & 0xFF, c2 >> 8, c2 & 0xFF);
1602 else
1603 EMIT_FOUR_BYTES (c1 & 0xFF, c1 >> 8, c2 & 0xFF, c2 >> 8);
1604 }
1605 }
065e3595 1606 record_conversion_result (coding, CODING_RESULT_SUCCESS);
df7492f9
KH
1607 coding->produced = dst - coding->destination;
1608 coding->produced_char += produced_chars;
1609 return 0;
1610}
1611
1612\f
1613/*** 6. Old Emacs' internal format (emacs-mule) ***/
1614
1615/* Emacs' internal format for representation of multiple character
1616 sets is a kind of multi-byte encoding, i.e. characters are
1617 represented by variable-length sequences of one-byte codes.
1618
1619 ASCII characters and control characters (e.g. `tab', `newline') are
1620 represented by one-byte sequences which are their ASCII codes, in
1621 the range 0x00 through 0x7F.
1622
1623 8-bit characters of the range 0x80..0x9F are represented by
1624 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
1625 code + 0x20).
1626
1627 8-bit characters of the range 0xA0..0xFF are represented by
1628 one-byte sequences which are their 8-bit code.
1629
1630 The other characters are represented by a sequence of `base
1631 leading-code', optional `extended leading-code', and one or two
1632 `position-code's. The length of the sequence is determined by the
1633 base leading-code. Leading-code takes the range 0x81 through 0x9D,
1634 whereas extended leading-code and position-code take the range 0xA0
1635 through 0xFF. See `charset.h' for more details about leading-code
1636 and position-code.
1637
1638 --- CODE RANGE of Emacs' internal format ---
1639 character set range
1640 ------------- -----
1641 ascii 0x00..0x7F
1642 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
1643 eight-bit-graphic 0xA0..0xBF
1644 ELSE 0x81..0x9D + [0xA0..0xFF]+
1645 ---------------------------------------------
1646
1647 As this is the internal character representation, the format is
1648 usually not used externally (i.e. in a file or in a data sent to a
1649 process). But, it is possible to have a text externally in this
1650 format (i.e. by encoding by the coding system `emacs-mule').
1651
1652 In that case, a sequence of one-byte codes has a slightly different
1653 form.
1654
1655 At first, all characters in eight-bit-control are represented by
1656 one-byte sequences which are their 8-bit code.
1657
1658 Next, character composition data are represented by the byte
1659 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
1660 where,
1661 METHOD is 0xF0 plus one of composition method (enum
1662 composition_method),
1663
1664 BYTES is 0xA0 plus a byte length of this composition data,
1665
1666 CHARS is 0x20 plus a number of characters composed by this
1667 data,
1668
1669 COMPONENTs are characters of multibye form or composition
1670 rules encoded by two-byte of ASCII codes.
1671
1672 In addition, for backward compatibility, the following formats are
1673 also recognized as composition data on decoding.
1674
1675 0x80 MSEQ ...
1676 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
1677
1678 Here,
1679 MSEQ is a multibyte form but in these special format:
1680 ASCII: 0xA0 ASCII_CODE+0x80,
1681 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
1682 RULE is a one byte code of the range 0xA0..0xF0 that
1683 represents a composition rule.
1684 */
1685
1686char emacs_mule_bytes[256];
1687
df7492f9 1688int
ff0dacd7 1689emacs_mule_char (coding, src, nbytes, nchars, id)
df7492f9 1690 struct coding_system *coding;
065e3595 1691 const unsigned char *src;
ff0dacd7 1692 int *nbytes, *nchars, *id;
df7492f9 1693{
8f924df7
KH
1694 const unsigned char *src_end = coding->source + coding->src_bytes;
1695 const unsigned char *src_base = src;
df7492f9 1696 int multibytep = coding->src_multibyte;
df7492f9
KH
1697 struct charset *charset;
1698 unsigned code;
1699 int c;
1700 int consumed_chars = 0;
1701
1702 ONE_MORE_BYTE (c);
065e3595 1703 if (c < 0)
df7492f9 1704 {
065e3595
KH
1705 c = -c;
1706 charset = emacs_mule_charset[0];
1707 }
1708 else
1709 {
4d41e8b7
KH
1710 if (c >= 0xA0)
1711 {
1712 /* Old style component character of a compostion. */
1713 if (c == 0xA0)
1714 {
1715 ONE_MORE_BYTE (c);
1716 c -= 0x80;
1717 }
1718 else
1719 c -= 0x20;
1720 }
1721
065e3595 1722 switch (emacs_mule_bytes[c])
b73bfc1c 1723 {
065e3595 1724 case 2:
df7492f9
KH
1725 if (! (charset = emacs_mule_charset[c]))
1726 goto invalid_code;
1727 ONE_MORE_BYTE (c);
9ffd559c 1728 if (c < 0xA0)
065e3595 1729 goto invalid_code;
df7492f9 1730 code = c & 0x7F;
065e3595
KH
1731 break;
1732
1733 case 3:
1734 if (c == EMACS_MULE_LEADING_CODE_PRIVATE_11
1735 || c == EMACS_MULE_LEADING_CODE_PRIVATE_12)
1736 {
1737 ONE_MORE_BYTE (c);
9ffd559c 1738 if (c < 0xA0 || ! (charset = emacs_mule_charset[c]))
065e3595
KH
1739 goto invalid_code;
1740 ONE_MORE_BYTE (c);
9ffd559c 1741 if (c < 0xA0)
065e3595
KH
1742 goto invalid_code;
1743 code = c & 0x7F;
1744 }
1745 else
1746 {
1747 if (! (charset = emacs_mule_charset[c]))
1748 goto invalid_code;
1749 ONE_MORE_BYTE (c);
9ffd559c 1750 if (c < 0xA0)
065e3595
KH
1751 goto invalid_code;
1752 code = (c & 0x7F) << 8;
1753 ONE_MORE_BYTE (c);
9ffd559c 1754 if (c < 0xA0)
065e3595
KH
1755 goto invalid_code;
1756 code |= c & 0x7F;
1757 }
1758 break;
1759
1760 case 4:
1761 ONE_MORE_BYTE (c);
1762 if (c < 0 || ! (charset = emacs_mule_charset[c]))
df7492f9
KH
1763 goto invalid_code;
1764 ONE_MORE_BYTE (c);
9ffd559c 1765 if (c < 0xA0)
065e3595 1766 goto invalid_code;
781d7a48 1767 code = (c & 0x7F) << 8;
df7492f9 1768 ONE_MORE_BYTE (c);
9ffd559c 1769 if (c < 0xA0)
065e3595 1770 goto invalid_code;
df7492f9 1771 code |= c & 0x7F;
065e3595 1772 break;
df7492f9 1773
065e3595
KH
1774 case 1:
1775 code = c;
1776 charset = CHARSET_FROM_ID (ASCII_BYTE_P (code)
1777 ? charset_ascii : charset_eight_bit);
1778 break;
df7492f9 1779
065e3595
KH
1780 default:
1781 abort ();
1782 }
1783 c = DECODE_CHAR (charset, code);
1784 if (c < 0)
1785 goto invalid_code;
df7492f9 1786 }
df7492f9
KH
1787 *nbytes = src - src_base;
1788 *nchars = consumed_chars;
ff0dacd7
KH
1789 if (id)
1790 *id = charset->id;
df7492f9
KH
1791 return c;
1792
1793 no_more_source:
1794 return -2;
1795
1796 invalid_code:
1797 return -1;
1798}
1799
1800
1801/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
ff0dacd7
KH
1802 Check if a text is encoded in `emacs-mule'. If it is, return 1,
1803 else return 0. */
df7492f9
KH
1804
1805static int
ff0dacd7 1806detect_coding_emacs_mule (coding, detect_info)
df7492f9 1807 struct coding_system *coding;
ff0dacd7 1808 struct coding_detection_info *detect_info;
df7492f9 1809{
065e3595 1810 const unsigned char *src = coding->source, *src_base;
8f924df7 1811 const unsigned char *src_end = coding->source + coding->src_bytes;
df7492f9
KH
1812 int multibytep = coding->src_multibyte;
1813 int consumed_chars = 0;
1814 int c;
1815 int found = 0;
1816
ff0dacd7 1817 detect_info->checked |= CATEGORY_MASK_EMACS_MULE;
df7492f9
KH
1818 /* A coding system of this category is always ASCII compatible. */
1819 src += coding->head_ascii;
1820
1821 while (1)
1822 {
065e3595 1823 src_base = src;
df7492f9 1824 ONE_MORE_BYTE (c);
065e3595
KH
1825 if (c < 0)
1826 continue;
df7492f9
KH
1827 if (c == 0x80)
1828 {
1829 /* Perhaps the start of composite character. We simple skip
1830 it because analyzing it is too heavy for detecting. But,
1831 at least, we check that the composite character
1832 constitues of more than 4 bytes. */
8f924df7 1833 const unsigned char *src_base;
df7492f9
KH
1834
1835 repeat:
1836 src_base = src;
1837 do
1838 {
1839 ONE_MORE_BYTE (c);
1840 }
1841 while (c >= 0xA0);
1842
1843 if (src - src_base <= 4)
1844 break;
ff0dacd7 1845 found = CATEGORY_MASK_EMACS_MULE;
df7492f9
KH
1846 if (c == 0x80)
1847 goto repeat;
b73bfc1c 1848 }
df7492f9
KH
1849
1850 if (c < 0x80)
b73bfc1c 1851 {
df7492f9
KH
1852 if (c < 0x20
1853 && (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO))
1854 break;
1855 }
1856 else
1857 {
0e219d54 1858 int more_bytes = emacs_mule_bytes[*src_base] - 1;
df7492f9 1859
0e219d54 1860 while (more_bytes > 0)
df7492f9
KH
1861 {
1862 ONE_MORE_BYTE (c);
0e219d54
KH
1863 if (c < 0xA0)
1864 {
1865 src--; /* Unread the last byte. */
1866 break;
1867 }
1868 more_bytes--;
df7492f9 1869 }
0e219d54 1870 if (more_bytes != 0)
df7492f9 1871 break;
ff0dacd7 1872 found = CATEGORY_MASK_EMACS_MULE;
4ed46869
KH
1873 }
1874 }
ff0dacd7 1875 detect_info->rejected |= CATEGORY_MASK_EMACS_MULE;
df7492f9
KH
1876 return 0;
1877
1878 no_more_source:
065e3595 1879 if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK)
89528eb3 1880 {
ff0dacd7 1881 detect_info->rejected |= CATEGORY_MASK_EMACS_MULE;
89528eb3
KH
1882 return 0;
1883 }
ff0dacd7
KH
1884 detect_info->found |= found;
1885 return 1;
4ed46869
KH
1886}
1887
b73bfc1c 1888
df7492f9
KH
1889/* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
1890
1891/* Decode a character represented as a component of composition
1892 sequence of Emacs 20/21 style at SRC. Set C to that character and
1893 update SRC to the head of next character (or an encoded composition
1894 rule). If SRC doesn't points a composition component, set C to -1.
1895 If SRC points an invalid byte sequence, global exit by a return
1896 value 0. */
1897
1898#define DECODE_EMACS_MULE_COMPOSITION_CHAR(buf) \
1899 if (1) \
1900 { \
1901 int c; \
1902 int nbytes, nchars; \
1903 \
1904 if (src == src_end) \
1905 break; \
ff0dacd7 1906 c = emacs_mule_char (coding, src, &nbytes, &nchars, NULL);\
df7492f9
KH
1907 if (c < 0) \
1908 { \
1909 if (c == -2) \
1910 break; \
1911 goto invalid_code; \
1912 } \
1913 *buf++ = c; \
1914 src += nbytes; \
1915 consumed_chars += nchars; \
1916 } \
1917 else
1918
1919
1920/* Decode a composition rule represented as a component of composition
781d7a48
KH
1921 sequence of Emacs 20 style at SRC. Store the decoded rule in *BUF,
1922 and increment BUF. If SRC points an invalid byte sequence, set C
1923 to -1. */
df7492f9 1924
781d7a48 1925#define DECODE_EMACS_MULE_COMPOSITION_RULE_20(buf) \
df7492f9
KH
1926 do { \
1927 int c, gref, nref; \
1928 \
781d7a48 1929 if (src >= src_end) \
df7492f9
KH
1930 goto invalid_code; \
1931 ONE_MORE_BYTE_NO_CHECK (c); \
4d41e8b7 1932 c -= 0xA0; \
df7492f9
KH
1933 if (c < 0 || c >= 81) \
1934 goto invalid_code; \
1935 \
1936 gref = c / 9, nref = c % 9; \
1937 *buf++ = COMPOSITION_ENCODE_RULE (gref, nref); \
1938 } while (0)
1939
1940
781d7a48
KH
1941/* Decode a composition rule represented as a component of composition
1942 sequence of Emacs 21 style at SRC. Store the decoded rule in *BUF,
1943 and increment BUF. If SRC points an invalid byte sequence, set C
1944 to -1. */
1945
1946#define DECODE_EMACS_MULE_COMPOSITION_RULE_21(buf) \
1947 do { \
1948 int gref, nref; \
1949 \
1950 if (src + 1>= src_end) \
1951 goto invalid_code; \
1952 ONE_MORE_BYTE_NO_CHECK (gref); \
1953 gref -= 0x20; \
1954 ONE_MORE_BYTE_NO_CHECK (nref); \
1955 nref -= 0x20; \
1956 if (gref < 0 || gref >= 81 \
1957 || nref < 0 || nref >= 81) \
1958 goto invalid_code; \
1959 *buf++ = COMPOSITION_ENCODE_RULE (gref, nref); \
1960 } while (0)
1961
1962
df7492f9 1963#define DECODE_EMACS_MULE_21_COMPOSITION(c) \
aa72b389 1964 do { \
df7492f9 1965 /* Emacs 21 style format. The first three bytes at SRC are \
781d7a48 1966 (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is \
df7492f9
KH
1967 the byte length of this composition information, CHARS is the \
1968 number of characters composed by this composition. */ \
781d7a48
KH
1969 enum composition_method method = c - 0xF2; \
1970 int *charbuf_base = charbuf; \
df7492f9
KH
1971 int consumed_chars_limit; \
1972 int nbytes, nchars; \
1973 \
1974 ONE_MORE_BYTE (c); \
065e3595
KH
1975 if (c < 0) \
1976 goto invalid_code; \
df7492f9
KH
1977 nbytes = c - 0xA0; \
1978 if (nbytes < 3) \
1979 goto invalid_code; \
1980 ONE_MORE_BYTE (c); \
065e3595
KH
1981 if (c < 0) \
1982 goto invalid_code; \
df7492f9 1983 nchars = c - 0xA0; \
69a80ea3 1984 ADD_COMPOSITION_DATA (charbuf, nchars, method); \
df7492f9
KH
1985 consumed_chars_limit = consumed_chars_base + nbytes; \
1986 if (method != COMPOSITION_RELATIVE) \
aa72b389 1987 { \
df7492f9
KH
1988 int i = 0; \
1989 while (consumed_chars < consumed_chars_limit) \
aa72b389 1990 { \
df7492f9 1991 if (i % 2 && method != COMPOSITION_WITH_ALTCHARS) \
781d7a48 1992 DECODE_EMACS_MULE_COMPOSITION_RULE_21 (charbuf); \
df7492f9
KH
1993 else \
1994 DECODE_EMACS_MULE_COMPOSITION_CHAR (charbuf); \
781d7a48 1995 i++; \
aa72b389 1996 } \
df7492f9
KH
1997 if (consumed_chars < consumed_chars_limit) \
1998 goto invalid_code; \
781d7a48 1999 charbuf_base[0] -= i; \
aa72b389
KH
2000 } \
2001 } while (0)
93dec019 2002
aa72b389 2003
d959f512
KH
2004#define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION(c) \
2005 do { \
2006 /* Emacs 20 style format for relative composition. */ \
2007 /* Store multibyte form of characters to be composed. */ \
2008 enum composition_method method = COMPOSITION_RELATIVE; \
2009 int components[MAX_COMPOSITION_COMPONENTS * 2 - 1]; \
2010 int *buf = components; \
2011 int i, j; \
2012 \
2013 src = src_base; \
2014 ONE_MORE_BYTE (c); /* skip 0x80 */ \
2015 for (i = 0; *src >= 0xA0 && i < MAX_COMPOSITION_COMPONENTS; i++) \
2016 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
2017 if (i < 2) \
2018 goto invalid_code; \
2019 ADD_COMPOSITION_DATA (charbuf, i, method); \
2020 for (j = 0; j < i; j++) \
2021 *charbuf++ = components[j]; \
df7492f9
KH
2022 } while (0)
2023
2024
2025#define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION(c) \
2026 do { \
2027 /* Emacs 20 style format for rule-base composition. */ \
2028 /* Store multibyte form of characters to be composed. */ \
ff0dacd7 2029 enum composition_method method = COMPOSITION_WITH_RULE; \
4d41e8b7 2030 int *charbuf_base = charbuf; \
df7492f9
KH
2031 int components[MAX_COMPOSITION_COMPONENTS * 2 - 1]; \
2032 int *buf = components; \
2033 int i, j; \
4d41e8b7 2034 \
df7492f9 2035 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
4d41e8b7 2036 for (i = 1; i < MAX_COMPOSITION_COMPONENTS; i++) \
df7492f9 2037 { \
4d41e8b7
KH
2038 if (*src < 0xA0) \
2039 break; \
781d7a48 2040 DECODE_EMACS_MULE_COMPOSITION_RULE_20 (buf); \
df7492f9
KH
2041 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
2042 } \
4d41e8b7 2043 if (i <= 1 || (buf - components) % 2 == 0) \
df7492f9 2044 goto invalid_code; \
4d41e8b7 2045 if (charbuf + i + (i / 2) + 1 >= charbuf_end) \
df7492f9 2046 goto no_more_source; \
4d41e8b7
KH
2047 ADD_COMPOSITION_DATA (charbuf, i, method); \
2048 i = i * 2 - 1; \
df7492f9
KH
2049 for (j = 0; j < i; j++) \
2050 *charbuf++ = components[j]; \
4d41e8b7 2051 charbuf_base[0] -= i; \
df7492f9
KH
2052 for (j = 0; j < i; j += 2) \
2053 *charbuf++ = components[j]; \
2054 } while (0)
2055
aa72b389
KH
2056
2057static void
df7492f9 2058decode_coding_emacs_mule (coding)
aa72b389 2059 struct coding_system *coding;
aa72b389 2060{
8f924df7
KH
2061 const unsigned char *src = coding->source + coding->consumed;
2062 const unsigned char *src_end = coding->source + coding->src_bytes;
2063 const unsigned char *src_base;
69a80ea3
KH
2064 int *charbuf = coding->charbuf + coding->charbuf_used;
2065 int *charbuf_end
2066 = coding->charbuf + coding->charbuf_size - MAX_ANNOTATION_LENGTH;
df7492f9 2067 int consumed_chars = 0, consumed_chars_base;
df7492f9 2068 int multibytep = coding->src_multibyte;
24a73b0a 2069 Lisp_Object attrs, charset_list;
ff0dacd7
KH
2070 int char_offset = coding->produced_char;
2071 int last_offset = char_offset;
2072 int last_id = charset_ascii;
aa72b389 2073
24a73b0a 2074 CODING_GET_INFO (coding, attrs, charset_list);
aa72b389 2075
aa72b389
KH
2076 while (1)
2077 {
df7492f9
KH
2078 int c;
2079
aa72b389 2080 src_base = src;
df7492f9
KH
2081 consumed_chars_base = consumed_chars;
2082
2083 if (charbuf >= charbuf_end)
2084 break;
aa72b389 2085
df7492f9 2086 ONE_MORE_BYTE (c);
065e3595
KH
2087 if (c < 0)
2088 {
2089 *charbuf++ = -c;
2090 char_offset++;
2091 }
2092 else if (c < 0x80)
aa72b389 2093 {
df7492f9
KH
2094 *charbuf++ = c;
2095 char_offset++;
aa72b389 2096 }
df7492f9
KH
2097 else if (c == 0x80)
2098 {
df7492f9 2099 ONE_MORE_BYTE (c);
065e3595
KH
2100 if (c < 0)
2101 goto invalid_code;
781d7a48
KH
2102 if (c - 0xF2 >= COMPOSITION_RELATIVE
2103 && c - 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS)
df7492f9
KH
2104 DECODE_EMACS_MULE_21_COMPOSITION (c);
2105 else if (c < 0xC0)
2106 DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (c);
2107 else if (c == 0xFF)
2108 DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (c);
2109 else
2110 goto invalid_code;
2111 }
2112 else if (c < 0xA0 && emacs_mule_bytes[c] > 1)
2113 {
2114 int nbytes, nchars;
ff0dacd7
KH
2115 int id;
2116
781d7a48
KH
2117 src = src_base;
2118 consumed_chars = consumed_chars_base;
ff0dacd7 2119 c = emacs_mule_char (coding, src, &nbytes, &nchars, &id);
df7492f9
KH
2120 if (c < 0)
2121 {
2122 if (c == -2)
2123 break;
2124 goto invalid_code;
2125 }
ff0dacd7
KH
2126 if (last_id != id)
2127 {
2128 if (last_id != charset_ascii)
69a80ea3 2129 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
ff0dacd7
KH
2130 last_id = id;
2131 last_offset = char_offset;
2132 }
df7492f9 2133 *charbuf++ = c;
781d7a48
KH
2134 src += nbytes;
2135 consumed_chars += nchars;
df7492f9
KH
2136 char_offset++;
2137 }
4d41e8b7
KH
2138 else
2139 goto invalid_code;
df7492f9
KH
2140 continue;
2141
2142 invalid_code:
2143 src = src_base;
2144 consumed_chars = consumed_chars_base;
2145 ONE_MORE_BYTE (c);
2146 *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
ff0dacd7 2147 char_offset++;
df7492f9
KH
2148 coding->errors++;
2149 }
2150
2151 no_more_source:
ff0dacd7 2152 if (last_id != charset_ascii)
69a80ea3 2153 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
df7492f9
KH
2154 coding->consumed_char += consumed_chars_base;
2155 coding->consumed = src_base - coding->source;
2156 coding->charbuf_used = charbuf - coding->charbuf;
2157}
2158
2159
2160#define EMACS_MULE_LEADING_CODES(id, codes) \
2161 do { \
2162 if (id < 0xA0) \
2163 codes[0] = id, codes[1] = 0; \
2164 else if (id < 0xE0) \
2165 codes[0] = 0x9A, codes[1] = id; \
2166 else if (id < 0xF0) \
2167 codes[0] = 0x9B, codes[1] = id; \
2168 else if (id < 0xF5) \
2169 codes[0] = 0x9C, codes[1] = id; \
2170 else \
2171 codes[0] = 0x9D, codes[1] = id; \
2172 } while (0);
2173
aa72b389 2174
df7492f9
KH
2175static int
2176encode_coding_emacs_mule (coding)
2177 struct coding_system *coding;
2178{
2179 int multibytep = coding->dst_multibyte;
2180 int *charbuf = coding->charbuf;
2181 int *charbuf_end = charbuf + coding->charbuf_used;
2182 unsigned char *dst = coding->destination + coding->produced;
2183 unsigned char *dst_end = coding->destination + coding->dst_bytes;
2184 int safe_room = 8;
df7492f9 2185 int produced_chars = 0;
24a73b0a 2186 Lisp_Object attrs, charset_list;
df7492f9 2187 int c;
ff0dacd7 2188 int preferred_charset_id = -1;
df7492f9 2189
24a73b0a 2190 CODING_GET_INFO (coding, attrs, charset_list);
eccb6815
KH
2191 if (! EQ (charset_list, Vemacs_mule_charset_list))
2192 {
2193 CODING_ATTR_CHARSET_LIST (attrs)
2194 = charset_list = Vemacs_mule_charset_list;
2195 }
df7492f9
KH
2196
2197 while (charbuf < charbuf_end)
2198 {
2199 ASSURE_DESTINATION (safe_room);
2200 c = *charbuf++;
ff0dacd7
KH
2201
2202 if (c < 0)
2203 {
2204 /* Handle an annotation. */
2205 switch (*charbuf)
2206 {
2207 case CODING_ANNOTATE_COMPOSITION_MASK:
2208 /* Not yet implemented. */
2209 break;
2210 case CODING_ANNOTATE_CHARSET_MASK:
2211 preferred_charset_id = charbuf[3];
2212 if (preferred_charset_id >= 0
2213 && NILP (Fmemq (make_number (preferred_charset_id),
2214 charset_list)))
2215 preferred_charset_id = -1;
2216 break;
2217 default:
2218 abort ();
2219 }
2220 charbuf += -c - 1;
2221 continue;
2222 }
2223
df7492f9
KH
2224 if (ASCII_CHAR_P (c))
2225 EMIT_ONE_ASCII_BYTE (c);
16eafb5d
KH
2226 else if (CHAR_BYTE8_P (c))
2227 {
2228 c = CHAR_TO_BYTE8 (c);
2229 EMIT_ONE_BYTE (c);
2230 }
df7492f9 2231 else
aa72b389 2232 {
df7492f9
KH
2233 struct charset *charset;
2234 unsigned code;
2235 int dimension;
2236 int emacs_mule_id;
2237 unsigned char leading_codes[2];
2238
ff0dacd7
KH
2239 if (preferred_charset_id >= 0)
2240 {
2241 charset = CHARSET_FROM_ID (preferred_charset_id);
2242 if (! CHAR_CHARSET_P (c, charset))
2243 charset = char_charset (c, charset_list, NULL);
2244 }
2245 else
2246 charset = char_charset (c, charset_list, &code);
df7492f9
KH
2247 if (! charset)
2248 {
2249 c = coding->default_char;
2250 if (ASCII_CHAR_P (c))
2251 {
2252 EMIT_ONE_ASCII_BYTE (c);
2253 continue;
2254 }
2255 charset = char_charset (c, charset_list, &code);
2256 }
2257 dimension = CHARSET_DIMENSION (charset);
2258 emacs_mule_id = CHARSET_EMACS_MULE_ID (charset);
2259 EMACS_MULE_LEADING_CODES (emacs_mule_id, leading_codes);
2260 EMIT_ONE_BYTE (leading_codes[0]);
2261 if (leading_codes[1])
2262 EMIT_ONE_BYTE (leading_codes[1]);
2263 if (dimension == 1)
1fa663f9 2264 EMIT_ONE_BYTE (code | 0x80);
aa72b389 2265 else
df7492f9 2266 {
1fa663f9 2267 code |= 0x8080;
df7492f9
KH
2268 EMIT_ONE_BYTE (code >> 8);
2269 EMIT_ONE_BYTE (code & 0xFF);
2270 }
aa72b389 2271 }
aa72b389 2272 }
065e3595 2273 record_conversion_result (coding, CODING_RESULT_SUCCESS);
df7492f9
KH
2274 coding->produced_char += produced_chars;
2275 coding->produced = dst - coding->destination;
2276 return 0;
aa72b389 2277}
b73bfc1c 2278
4ed46869 2279\f
df7492f9 2280/*** 7. ISO2022 handlers ***/
4ed46869
KH
2281
2282/* The following note describes the coding system ISO2022 briefly.
39787efd 2283 Since the intention of this note is to help understand the
5a936b46 2284 functions in this file, some parts are NOT ACCURATE or are OVERLY
39787efd 2285 SIMPLIFIED. For thorough understanding, please refer to the
5a936b46 2286 original document of ISO2022. This is equivalent to the standard
cfb43547 2287 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
4ed46869
KH
2288
2289 ISO2022 provides many mechanisms to encode several character sets
cfb43547 2290 in 7-bit and 8-bit environments. For 7-bit environments, all text
39787efd
KH
2291 is encoded using bytes less than 128. This may make the encoded
2292 text a little bit longer, but the text passes more easily through
cfb43547 2293 several types of gateway, some of which strip off the MSB (Most
8ca3766a 2294 Significant Bit).
b73bfc1c 2295
cfb43547
DL
2296 There are two kinds of character sets: control character sets and
2297 graphic character sets. The former contain control characters such
4ed46869 2298 as `newline' and `escape' to provide control functions (control
39787efd 2299 functions are also provided by escape sequences). The latter
cfb43547 2300 contain graphic characters such as 'A' and '-'. Emacs recognizes
4ed46869
KH
2301 two control character sets and many graphic character sets.
2302
2303 Graphic character sets are classified into one of the following
39787efd
KH
2304 four classes, according to the number of bytes (DIMENSION) and
2305 number of characters in one dimension (CHARS) of the set:
2306 - DIMENSION1_CHARS94
2307 - DIMENSION1_CHARS96
2308 - DIMENSION2_CHARS94
2309 - DIMENSION2_CHARS96
2310
2311 In addition, each character set is assigned an identification tag,
cfb43547 2312 unique for each set, called the "final character" (denoted as <F>
39787efd
KH
2313 hereafter). The <F> of each character set is decided by ECMA(*)
2314 when it is registered in ISO. The code range of <F> is 0x30..0x7F
2315 (0x30..0x3F are for private use only).
4ed46869
KH
2316
2317 Note (*): ECMA = European Computer Manufacturers Association
2318
cfb43547 2319 Here are examples of graphic character sets [NAME(<F>)]:
4ed46869
KH
2320 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
2321 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
2322 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
2323 o DIMENSION2_CHARS96 -- none for the moment
2324
39787efd 2325 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
4ed46869
KH
2326 C0 [0x00..0x1F] -- control character plane 0
2327 GL [0x20..0x7F] -- graphic character plane 0
2328 C1 [0x80..0x9F] -- control character plane 1
2329 GR [0xA0..0xFF] -- graphic character plane 1
2330
2331 A control character set is directly designated and invoked to C0 or
39787efd
KH
2332 C1 by an escape sequence. The most common case is that:
2333 - ISO646's control character set is designated/invoked to C0, and
2334 - ISO6429's control character set is designated/invoked to C1,
2335 and usually these designations/invocations are omitted in encoded
2336 text. In a 7-bit environment, only C0 can be used, and a control
2337 character for C1 is encoded by an appropriate escape sequence to
2338 fit into the environment. All control characters for C1 are
2339 defined to have corresponding escape sequences.
4ed46869
KH
2340
2341 A graphic character set is at first designated to one of four
2342 graphic registers (G0 through G3), then these graphic registers are
2343 invoked to GL or GR. These designations and invocations can be
2344 done independently. The most common case is that G0 is invoked to
39787efd
KH
2345 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
2346 these invocations and designations are omitted in encoded text.
2347 In a 7-bit environment, only GL can be used.
4ed46869 2348
39787efd
KH
2349 When a graphic character set of CHARS94 is invoked to GL, codes
2350 0x20 and 0x7F of the GL area work as control characters SPACE and
2351 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
2352 be used.
4ed46869
KH
2353
2354 There are two ways of invocation: locking-shift and single-shift.
2355 With locking-shift, the invocation lasts until the next different
39787efd
KH
2356 invocation, whereas with single-shift, the invocation affects the
2357 following character only and doesn't affect the locking-shift
2358 state. Invocations are done by the following control characters or
2359 escape sequences:
4ed46869
KH
2360
2361 ----------------------------------------------------------------------
39787efd 2362 abbrev function cntrl escape seq description
4ed46869 2363 ----------------------------------------------------------------------
39787efd
KH
2364 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
2365 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
2366 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
2367 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
2368 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
2369 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
2370 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
2371 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
2372 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
4ed46869 2373 ----------------------------------------------------------------------
39787efd
KH
2374 (*) These are not used by any known coding system.
2375
2376 Control characters for these functions are defined by macros
2377 ISO_CODE_XXX in `coding.h'.
4ed46869 2378
39787efd 2379 Designations are done by the following escape sequences:
4ed46869
KH
2380 ----------------------------------------------------------------------
2381 escape sequence description
2382 ----------------------------------------------------------------------
2383 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
2384 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
2385 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
2386 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
2387 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
2388 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
2389 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
2390 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
2391 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
2392 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
2393 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
2394 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
2395 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
2396 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
2397 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
2398 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
2399 ----------------------------------------------------------------------
2400
2401 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
39787efd 2402 of dimension 1, chars 94, and final character <F>, etc...
4ed46869
KH
2403
2404 Note (*): Although these designations are not allowed in ISO2022,
2405 Emacs accepts them on decoding, and produces them on encoding
39787efd 2406 CHARS96 character sets in a coding system which is characterized as
4ed46869
KH
2407 7-bit environment, non-locking-shift, and non-single-shift.
2408
2409 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
df7492f9 2410 '(' must be omitted. We refer to this as "short-form" hereafter.
4ed46869 2411
cfb43547 2412 Now you may notice that there are a lot of ways of encoding the
39787efd
KH
2413 same multilingual text in ISO2022. Actually, there exist many
2414 coding systems such as Compound Text (used in X11's inter client
8ca3766a
DL
2415 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
2416 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
4ed46869
KH
2417 localized platforms), and all of these are variants of ISO2022.
2418
2419 In addition to the above, Emacs handles two more kinds of escape
2420 sequences: ISO6429's direction specification and Emacs' private
2421 sequence for specifying character composition.
2422
39787efd 2423 ISO6429's direction specification takes the following form:
4ed46869
KH
2424 o CSI ']' -- end of the current direction
2425 o CSI '0' ']' -- end of the current direction
2426 o CSI '1' ']' -- start of left-to-right text
2427 o CSI '2' ']' -- start of right-to-left text
2428 The control character CSI (0x9B: control sequence introducer) is
39787efd
KH
2429 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
2430
2431 Character composition specification takes the following form:
ec6d2bb8
KH
2432 o ESC '0' -- start relative composition
2433 o ESC '1' -- end composition
2434 o ESC '2' -- start rule-base composition (*)
2435 o ESC '3' -- start relative composition with alternate chars (**)
2436 o ESC '4' -- start rule-base composition with alternate chars (**)
b73bfc1c 2437 Since these are not standard escape sequences of any ISO standard,
cfb43547 2438 the use of them with these meanings is restricted to Emacs only.
ec6d2bb8 2439
5a936b46
DL
2440 (*) This form is used only in Emacs 20.7 and older versions,
2441 but newer versions can safely decode it.
cfb43547 2442 (**) This form is used only in Emacs 21.1 and newer versions,
5a936b46 2443 and older versions can't decode it.
ec6d2bb8 2444
cfb43547 2445 Here's a list of example usages of these composition escape
b73bfc1c 2446 sequences (categorized by `enum composition_method').
ec6d2bb8 2447
b73bfc1c 2448 COMPOSITION_RELATIVE:
ec6d2bb8 2449 ESC 0 CHAR [ CHAR ] ESC 1
8ca3766a 2450 COMPOSITION_WITH_RULE:
ec6d2bb8 2451 ESC 2 CHAR [ RULE CHAR ] ESC 1
b73bfc1c 2452 COMPOSITION_WITH_ALTCHARS:
ec6d2bb8 2453 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
b73bfc1c 2454 COMPOSITION_WITH_RULE_ALTCHARS:
ec6d2bb8 2455 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
4ed46869
KH
2456
2457enum iso_code_class_type iso_code_class[256];
2458
df7492f9
KH
2459#define SAFE_CHARSET_P(coding, id) \
2460 ((id) <= (coding)->max_charset_id \
2461 && (coding)->safe_charsets[id] >= 0)
2462
2463
2464#define SHIFT_OUT_OK(category) \
2465 (CODING_ISO_INITIAL (&coding_categories[category], 1) >= 0)
2466
2467static void
f0064e1f
DL
2468setup_iso_safe_charsets (attrs)
2469 Lisp_Object attrs;
df7492f9
KH
2470{
2471 Lisp_Object charset_list, safe_charsets;
2472 Lisp_Object request;
2473 Lisp_Object reg_usage;
2474 Lisp_Object tail;
2475 int reg94, reg96;
2476 int flags = XINT (AREF (attrs, coding_attr_iso_flags));
2477 int max_charset_id;
2478
2479 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
2480 if ((flags & CODING_ISO_FLAG_FULL_SUPPORT)
2481 && ! EQ (charset_list, Viso_2022_charset_list))
2482 {
2483 CODING_ATTR_CHARSET_LIST (attrs)
2484 = charset_list = Viso_2022_charset_list;
2485 ASET (attrs, coding_attr_safe_charsets, Qnil);
2486 }
2487
2488 if (STRINGP (AREF (attrs, coding_attr_safe_charsets)))
2489 return;
2490
2491 max_charset_id = 0;
2492 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
2493 {
2494 int id = XINT (XCAR (tail));
2495 if (max_charset_id < id)
2496 max_charset_id = id;
2497 }
d46c5b12 2498
df7492f9
KH
2499 safe_charsets = Fmake_string (make_number (max_charset_id + 1),
2500 make_number (255));
2501 request = AREF (attrs, coding_attr_iso_request);
2502 reg_usage = AREF (attrs, coding_attr_iso_usage);
2503 reg94 = XINT (XCAR (reg_usage));
2504 reg96 = XINT (XCDR (reg_usage));
2505
2506 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
2507 {
2508 Lisp_Object id;
2509 Lisp_Object reg;
2510 struct charset *charset;
2511
2512 id = XCAR (tail);
2513 charset = CHARSET_FROM_ID (XINT (id));
bf16eb23 2514 reg = Fcdr (Fassq (id, request));
df7492f9 2515 if (! NILP (reg))
8f924df7 2516 SSET (safe_charsets, XINT (id), XINT (reg));
df7492f9
KH
2517 else if (charset->iso_chars_96)
2518 {
2519 if (reg96 < 4)
8f924df7 2520 SSET (safe_charsets, XINT (id), reg96);
df7492f9
KH
2521 }
2522 else
2523 {
2524 if (reg94 < 4)
8f924df7 2525 SSET (safe_charsets, XINT (id), reg94);
df7492f9
KH
2526 }
2527 }
2528 ASET (attrs, coding_attr_safe_charsets, safe_charsets);
2529}
d46c5b12 2530
b6871cc7 2531
4ed46869 2532/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
ff0dacd7
KH
2533 Check if a text is encoded in one of ISO-2022 based codig systems.
2534 If it is, return 1, else return 0. */
4ed46869 2535
0a28aafb 2536static int
ff0dacd7 2537detect_coding_iso_2022 (coding, detect_info)
df7492f9 2538 struct coding_system *coding;
ff0dacd7 2539 struct coding_detection_info *detect_info;
4ed46869 2540{
8f924df7
KH
2541 const unsigned char *src = coding->source, *src_base = src;
2542 const unsigned char *src_end = coding->source + coding->src_bytes;
df7492f9 2543 int multibytep = coding->src_multibyte;
ff0dacd7 2544 int single_shifting = 0;
df7492f9
KH
2545 int id;
2546 int c, c1;
2547 int consumed_chars = 0;
2548 int i;
ff0dacd7
KH
2549 int rejected = 0;
2550 int found = 0;
2551
2552 detect_info->checked |= CATEGORY_MASK_ISO;
df7492f9
KH
2553
2554 for (i = coding_category_iso_7; i <= coding_category_iso_8_else; i++)
2555 {
2556 struct coding_system *this = &(coding_categories[i]);
2557 Lisp_Object attrs, val;
2558
2559 attrs = CODING_ID_ATTRS (this->id);
2560 if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT
2561 && ! EQ (CODING_ATTR_SAFE_CHARSETS (attrs), Viso_2022_charset_list))
2562 setup_iso_safe_charsets (attrs);
2563 val = CODING_ATTR_SAFE_CHARSETS (attrs);
8f924df7
KH
2564 this->max_charset_id = SCHARS (val) - 1;
2565 this->safe_charsets = (char *) SDATA (val);
df7492f9
KH
2566 }
2567
2568 /* A coding system of this category is always ASCII compatible. */
2569 src += coding->head_ascii;
3f003981 2570
ff0dacd7 2571 while (rejected != CATEGORY_MASK_ISO)
4ed46869 2572 {
065e3595 2573 src_base = src;
df7492f9 2574 ONE_MORE_BYTE (c);
4ed46869
KH
2575 switch (c)
2576 {
2577 case ISO_CODE_ESC:
74383408
KH
2578 if (inhibit_iso_escape_detection)
2579 break;
f46869e4 2580 single_shifting = 0;
df7492f9 2581 ONE_MORE_BYTE (c);
d46c5b12 2582 if (c >= '(' && c <= '/')
4ed46869 2583 {
bf9cdd4e 2584 /* Designation sequence for a charset of dimension 1. */
df7492f9 2585 ONE_MORE_BYTE (c1);
d46c5b12 2586 if (c1 < ' ' || c1 >= 0x80
df7492f9 2587 || (id = iso_charset_table[0][c >= ','][c1]) < 0)
d46c5b12
KH
2588 /* Invalid designation sequence. Just ignore. */
2589 break;
bf9cdd4e
KH
2590 }
2591 else if (c == '$')
2592 {
2593 /* Designation sequence for a charset of dimension 2. */
df7492f9 2594 ONE_MORE_BYTE (c);
bf9cdd4e
KH
2595 if (c >= '@' && c <= 'B')
2596 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
ff0dacd7 2597 id = iso_charset_table[1][0][c];
bf9cdd4e 2598 else if (c >= '(' && c <= '/')
bcf26d6a 2599 {
df7492f9 2600 ONE_MORE_BYTE (c1);
d46c5b12 2601 if (c1 < ' ' || c1 >= 0x80
df7492f9 2602 || (id = iso_charset_table[1][c >= ','][c1]) < 0)
d46c5b12
KH
2603 /* Invalid designation sequence. Just ignore. */
2604 break;
bcf26d6a 2605 }
bf9cdd4e 2606 else
ff0dacd7 2607 /* Invalid designation sequence. Just ignore it. */
d46c5b12
KH
2608 break;
2609 }
ae9ff118 2610 else if (c == 'N' || c == 'O')
d46c5b12 2611 {
ae9ff118 2612 /* ESC <Fe> for SS2 or SS3. */
ff0dacd7
KH
2613 single_shifting = 1;
2614 rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_8BIT;
d46c5b12 2615 break;
4ed46869 2616 }
ec6d2bb8
KH
2617 else if (c >= '0' && c <= '4')
2618 {
2619 /* ESC <Fp> for start/end composition. */
ff0dacd7 2620 found |= CATEGORY_MASK_ISO;
ec6d2bb8
KH
2621 break;
2622 }
bf9cdd4e 2623 else
df7492f9 2624 {
ff0dacd7 2625 /* Invalid escape sequence. Just ignore it. */
df7492f9
KH
2626 break;
2627 }
d46c5b12
KH
2628
2629 /* We found a valid designation sequence for CHARSET. */
ff0dacd7 2630 rejected |= CATEGORY_MASK_ISO_8BIT;
df7492f9
KH
2631 if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7],
2632 id))
ff0dacd7 2633 found |= CATEGORY_MASK_ISO_7;
d46c5b12 2634 else
ff0dacd7 2635 rejected |= CATEGORY_MASK_ISO_7;
df7492f9
KH
2636 if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7_tight],
2637 id))
ff0dacd7 2638 found |= CATEGORY_MASK_ISO_7_TIGHT;
d46c5b12 2639 else
ff0dacd7 2640 rejected |= CATEGORY_MASK_ISO_7_TIGHT;
df7492f9
KH
2641 if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7_else],
2642 id))
ff0dacd7 2643 found |= CATEGORY_MASK_ISO_7_ELSE;
ae9ff118 2644 else
ff0dacd7 2645 rejected |= CATEGORY_MASK_ISO_7_ELSE;
df7492f9
KH
2646 if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_8_else],
2647 id))
ff0dacd7 2648 found |= CATEGORY_MASK_ISO_8_ELSE;
ae9ff118 2649 else
ff0dacd7 2650 rejected |= CATEGORY_MASK_ISO_8_ELSE;
4ed46869
KH
2651 break;
2652
4ed46869 2653 case ISO_CODE_SO:
d46c5b12 2654 case ISO_CODE_SI:
ff0dacd7 2655 /* Locking shift out/in. */
74383408
KH
2656 if (inhibit_iso_escape_detection)
2657 break;
f46869e4 2658 single_shifting = 0;
ff0dacd7 2659 rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_8BIT;
d46c5b12
KH
2660 break;
2661
4ed46869 2662 case ISO_CODE_CSI:
ff0dacd7 2663 /* Control sequence introducer. */
f46869e4 2664 single_shifting = 0;
ff0dacd7
KH
2665 rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE;
2666 found |= CATEGORY_MASK_ISO_8_ELSE;
2667 goto check_extra_latin;
2668
4ed46869
KH
2669 case ISO_CODE_SS2:
2670 case ISO_CODE_SS3:
ff0dacd7
KH
2671 /* Single shift. */
2672 if (inhibit_iso_escape_detection)
2673 break;
75e2a253 2674 single_shifting = 0;
ff0dacd7
KH
2675 rejected |= CATEGORY_MASK_ISO_7BIT;
2676 if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1])
2677 & CODING_ISO_FLAG_SINGLE_SHIFT)
75e2a253 2678 found |= CATEGORY_MASK_ISO_8_1, single_shifting = 1;
ff0dacd7
KH
2679 if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_2])
2680 & CODING_ISO_FLAG_SINGLE_SHIFT)
75e2a253
KH
2681 found |= CATEGORY_MASK_ISO_8_2, single_shifting = 1;
2682 if (single_shifting)
2683 break;
ff0dacd7 2684 goto check_extra_latin;
4ed46869
KH
2685
2686 default:
065e3595
KH
2687 if (c < 0)
2688 continue;
4ed46869 2689 if (c < 0x80)
f46869e4
KH
2690 {
2691 single_shifting = 0;
2692 break;
2693 }
ff0dacd7 2694 if (c >= 0xA0)
c4825358 2695 {
ff0dacd7
KH
2696 rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE;
2697 found |= CATEGORY_MASK_ISO_8_1;
f46869e4 2698 /* Check the length of succeeding codes of the range
ff0dacd7
KH
2699 0xA0..0FF. If the byte length is even, we include
2700 CATEGORY_MASK_ISO_8_2 in `found'. We can check this
2701 only when we are not single shifting. */
2702 if (! single_shifting
2703 && ! (rejected & CATEGORY_MASK_ISO_8_2))
f46869e4 2704 {
e17de821 2705 int i = 1;
b73bfc1c
KH
2706 while (src < src_end)
2707 {
df7492f9 2708 ONE_MORE_BYTE (c);
b73bfc1c
KH
2709 if (c < 0xA0)
2710 break;
2711 i++;
2712 }
2713
2714 if (i & 1 && src < src_end)
ff0dacd7 2715 rejected |= CATEGORY_MASK_ISO_8_2;
f46869e4 2716 else
ff0dacd7 2717 found |= CATEGORY_MASK_ISO_8_2;
f46869e4 2718 }
ff0dacd7 2719 break;
4ed46869 2720 }
ff0dacd7
KH
2721 check_extra_latin:
2722 single_shifting = 0;
2723 if (! VECTORP (Vlatin_extra_code_table)
2724 || NILP (XVECTOR (Vlatin_extra_code_table)->contents[c]))
2725 {
2726 rejected = CATEGORY_MASK_ISO;
2727 break;
2728 }
2729 if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1])
2730 & CODING_ISO_FLAG_LATIN_EXTRA)
2731 found |= CATEGORY_MASK_ISO_8_1;
2732 else
2733 rejected |= CATEGORY_MASK_ISO_8_1;
75e2a253 2734 rejected |= CATEGORY_MASK_ISO_8_2;
4ed46869
KH
2735 }
2736 }
ff0dacd7
KH
2737 detect_info->rejected |= CATEGORY_MASK_ISO;
2738 return 0;
4ed46869 2739
df7492f9 2740 no_more_source:
ff0dacd7
KH
2741 detect_info->rejected |= rejected;
2742 detect_info->found |= (found & ~rejected);
df7492f9 2743 return 1;
4ed46869 2744}
ec6d2bb8 2745
4ed46869 2746
134b9549
KH
2747/* Set designation state into CODING. Set CHARS_96 to -1 if the
2748 escape sequence should be kept. */
df7492f9
KH
2749#define DECODE_DESIGNATION(reg, dim, chars_96, final) \
2750 do { \
2751 int id, prev; \
2752 \
2753 if (final < '0' || final >= 128 \
2754 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
2755 || !SAFE_CHARSET_P (coding, id)) \
2756 { \
2757 CODING_ISO_DESIGNATION (coding, reg) = -2; \
134b9549
KH
2758 chars_96 = -1; \
2759 break; \
df7492f9
KH
2760 } \
2761 prev = CODING_ISO_DESIGNATION (coding, reg); \
bf16eb23
KH
2762 if (id == charset_jisx0201_roman) \
2763 { \
2764 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
2765 id = charset_ascii; \
2766 } \
2767 else if (id == charset_jisx0208_1978) \
2768 { \
2769 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
2770 id = charset_jisx0208; \
2771 } \
df7492f9
KH
2772 CODING_ISO_DESIGNATION (coding, reg) = id; \
2773 /* If there was an invalid designation to REG previously, and this \
2774 designation is ASCII to REG, we should keep this designation \
2775 sequence. */ \
2776 if (prev == -2 && id == charset_ascii) \
134b9549 2777 chars_96 = -1; \
4ed46869
KH
2778 } while (0)
2779
d46c5b12 2780
df7492f9
KH
2781#define MAYBE_FINISH_COMPOSITION() \
2782 do { \
2783 int i; \
2784 if (composition_state == COMPOSING_NO) \
2785 break; \
2786 /* It is assured that we have enough room for producing \
2787 characters stored in the table `components'. */ \
2788 if (charbuf + component_idx > charbuf_end) \
2789 goto no_more_source; \
2790 composition_state = COMPOSING_NO; \
2791 if (method == COMPOSITION_RELATIVE \
2792 || method == COMPOSITION_WITH_ALTCHARS) \
2793 { \
2794 for (i = 0; i < component_idx; i++) \
2795 *charbuf++ = components[i]; \
2796 char_offset += component_idx; \
2797 } \
2798 else \
2799 { \
2800 for (i = 0; i < component_idx; i += 2) \
2801 *charbuf++ = components[i]; \
2802 char_offset += (component_idx / 2) + 1; \
2803 } \
2804 } while (0)
2805
d46c5b12 2806
aa72b389
KH
2807/* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
2808 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
2809 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
df7492f9
KH
2810 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
2811 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
aa72b389 2812 */
ec6d2bb8 2813
df7492f9
KH
2814#define DECODE_COMPOSITION_START(c1) \
2815 do { \
2816 if (c1 == '0' \
781d7a48 2817 && composition_state == COMPOSING_COMPONENT_RULE) \
df7492f9
KH
2818 { \
2819 component_len = component_idx; \
2820 composition_state = COMPOSING_CHAR; \
2821 } \
2822 else \
2823 { \
8f924df7 2824 const unsigned char *p; \
df7492f9
KH
2825 \
2826 MAYBE_FINISH_COMPOSITION (); \
2827 if (charbuf + MAX_COMPOSITION_COMPONENTS > charbuf_end) \
2828 goto no_more_source; \
2829 for (p = src; p < src_end - 1; p++) \
2830 if (*p == ISO_CODE_ESC && p[1] == '1') \
2831 break; \
2832 if (p == src_end - 1) \
2833 { \
9286b333
KH
2834 /* The current composition doesn't end in the current \
2835 source. */ \
2836 record_conversion_result \
2837 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
df7492f9
KH
2838 goto no_more_source; \
2839 } \
2840 \
2841 /* This is surely the start of a composition. */ \
2842 method = (c1 == '0' ? COMPOSITION_RELATIVE \
2843 : c1 == '2' ? COMPOSITION_WITH_RULE \
2844 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
2845 : COMPOSITION_WITH_RULE_ALTCHARS); \
2846 composition_state = (c1 <= '2' ? COMPOSING_CHAR \
2847 : COMPOSING_COMPONENT_CHAR); \
2848 component_idx = component_len = 0; \
2849 } \
ec6d2bb8
KH
2850 } while (0)
2851
ec6d2bb8 2852
df7492f9
KH
2853/* Handle compositoin end sequence ESC 1. */
2854
2855#define DECODE_COMPOSITION_END() \
ec6d2bb8 2856 do { \
df7492f9
KH
2857 int nchars = (component_len > 0 ? component_idx - component_len \
2858 : method == COMPOSITION_RELATIVE ? component_idx \
2859 : (component_idx + 1) / 2); \
2860 int i; \
2861 int *saved_charbuf = charbuf; \
2862 \
69a80ea3 2863 ADD_COMPOSITION_DATA (charbuf, nchars, method); \
df7492f9 2864 if (method != COMPOSITION_RELATIVE) \
ec6d2bb8 2865 { \
df7492f9
KH
2866 if (component_len == 0) \
2867 for (i = 0; i < component_idx; i++) \
2868 *charbuf++ = components[i]; \
2869 else \
2870 for (i = 0; i < component_len; i++) \
2871 *charbuf++ = components[i]; \
2872 *saved_charbuf = saved_charbuf - charbuf; \
ec6d2bb8 2873 } \
df7492f9
KH
2874 if (method == COMPOSITION_WITH_RULE) \
2875 for (i = 0; i < component_idx; i += 2, char_offset++) \
2876 *charbuf++ = components[i]; \
ec6d2bb8 2877 else \
df7492f9
KH
2878 for (i = component_len; i < component_idx; i++, char_offset++) \
2879 *charbuf++ = components[i]; \
2880 coding->annotated = 1; \
2881 composition_state = COMPOSING_NO; \
ec6d2bb8
KH
2882 } while (0)
2883
df7492f9 2884
ec6d2bb8
KH
2885/* Decode a composition rule from the byte C1 (and maybe one more byte
2886 from SRC) and store one encoded composition rule in
2887 coding->cmp_data. */
2888
2889#define DECODE_COMPOSITION_RULE(c1) \
2890 do { \
ec6d2bb8
KH
2891 (c1) -= 32; \
2892 if (c1 < 81) /* old format (before ver.21) */ \
2893 { \
2894 int gref = (c1) / 9; \
2895 int nref = (c1) % 9; \
2896 if (gref == 4) gref = 10; \
2897 if (nref == 4) nref = 10; \
df7492f9 2898 c1 = COMPOSITION_ENCODE_RULE (gref, nref); \
ec6d2bb8 2899 } \
b73bfc1c 2900 else if (c1 < 93) /* new format (after ver.21) */ \
ec6d2bb8
KH
2901 { \
2902 ONE_MORE_BYTE (c2); \
df7492f9 2903 c1 = COMPOSITION_ENCODE_RULE (c1 - 81, c2 - 32); \
ec6d2bb8 2904 } \
df7492f9
KH
2905 else \
2906 c1 = 0; \
ec6d2bb8 2907 } while (0)
88993dfd 2908
d46c5b12 2909
4ed46869
KH
2910/* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
2911
b73bfc1c 2912static void
df7492f9 2913decode_coding_iso_2022 (coding)
4ed46869 2914 struct coding_system *coding;
4ed46869 2915{
8f924df7
KH
2916 const unsigned char *src = coding->source + coding->consumed;
2917 const unsigned char *src_end = coding->source + coding->src_bytes;
2918 const unsigned char *src_base;
69a80ea3 2919 int *charbuf = coding->charbuf + coding->charbuf_used;
ff0dacd7 2920 int *charbuf_end
69a80ea3 2921 = coding->charbuf + coding->charbuf_size - 4 - MAX_ANNOTATION_LENGTH;
df7492f9 2922 int consumed_chars = 0, consumed_chars_base;
df7492f9 2923 int multibytep = coding->src_multibyte;
4ed46869 2924 /* Charsets invoked to graphic plane 0 and 1 respectively. */
df7492f9
KH
2925 int charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
2926 int charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1);
134b9549 2927 int charset_id_2, charset_id_3;
df7492f9
KH
2928 struct charset *charset;
2929 int c;
2930 /* For handling composition sequence. */
2931#define COMPOSING_NO 0
2932#define COMPOSING_CHAR 1
2933#define COMPOSING_RULE 2
2934#define COMPOSING_COMPONENT_CHAR 3
2935#define COMPOSING_COMPONENT_RULE 4
2936
2937 int composition_state = COMPOSING_NO;
2938 enum composition_method method;
2939 int components[MAX_COMPOSITION_COMPONENTS * 2 + 1];
2940 int component_idx;
2941 int component_len;
24a73b0a 2942 Lisp_Object attrs, charset_list;
ff0dacd7
KH
2943 int char_offset = coding->produced_char;
2944 int last_offset = char_offset;
2945 int last_id = charset_ascii;
df7492f9 2946
24a73b0a 2947 CODING_GET_INFO (coding, attrs, charset_list);
df7492f9 2948 setup_iso_safe_charsets (attrs);
287c57d7
KH
2949 /* Charset list may have been changed. */
2950 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
2951 coding->safe_charsets = (char *) SDATA (CODING_ATTR_SAFE_CHARSETS(attrs));
b73bfc1c
KH
2952
2953 while (1)
4ed46869 2954 {
463f5630 2955 int c1, c2;
b73bfc1c
KH
2956
2957 src_base = src;
df7492f9
KH
2958 consumed_chars_base = consumed_chars;
2959
2960 if (charbuf >= charbuf_end)
2961 break;
2962
b73bfc1c 2963 ONE_MORE_BYTE (c1);
065e3595
KH
2964 if (c1 < 0)
2965 goto invalid_code;
4ed46869 2966
98725083 2967 /* We produce at most one character. */
4ed46869
KH
2968 switch (iso_code_class [c1])
2969 {
2970 case ISO_0x20_or_0x7F:
df7492f9 2971 if (composition_state != COMPOSING_NO)
ec6d2bb8 2972 {
df7492f9
KH
2973 if (composition_state == COMPOSING_RULE
2974 || composition_state == COMPOSING_COMPONENT_RULE)
2975 {
2976 DECODE_COMPOSITION_RULE (c1);
2977 components[component_idx++] = c1;
2978 composition_state--;
2979 continue;
2980 }
4ed46869 2981 }
df7492f9
KH
2982 if (charset_id_0 < 0
2983 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0)))
781d7a48
KH
2984 /* This is SPACE or DEL. */
2985 charset = CHARSET_FROM_ID (charset_ascii);
2986 else
2987 charset = CHARSET_FROM_ID (charset_id_0);
2988 break;
4ed46869
KH
2989
2990 case ISO_graphic_plane_0:
781d7a48 2991 if (composition_state != COMPOSING_NO)
b73bfc1c 2992 {
781d7a48
KH
2993 if (composition_state == COMPOSING_RULE
2994 || composition_state == COMPOSING_COMPONENT_RULE)
2995 {
2996 DECODE_COMPOSITION_RULE (c1);
2997 components[component_idx++] = c1;
2998 composition_state--;
2999 continue;
3000 }
b73bfc1c 3001 }
134b9549
KH
3002 if (charset_id_0 < 0)
3003 charset = CHARSET_FROM_ID (charset_ascii);
3004 else
3005 charset = CHARSET_FROM_ID (charset_id_0);
4ed46869
KH
3006 break;
3007
3008 case ISO_0xA0_or_0xFF:
df7492f9
KH
3009 if (charset_id_1 < 0
3010 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1))
3011 || CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS)
3012 goto invalid_code;
4ed46869
KH
3013 /* This is a graphic character, we fall down ... */
3014
3015 case ISO_graphic_plane_1:
df7492f9
KH
3016 if (charset_id_1 < 0)
3017 goto invalid_code;
3018 charset = CHARSET_FROM_ID (charset_id_1);
4ed46869
KH
3019 break;
3020
df7492f9
KH
3021 case ISO_control_0:
3022 MAYBE_FINISH_COMPOSITION ();
3023 charset = CHARSET_FROM_ID (charset_ascii);
4ed46869
KH
3024 break;
3025
df7492f9
KH
3026 case ISO_control_1:
3027 MAYBE_FINISH_COMPOSITION ();
3028 goto invalid_code;
3029
4ed46869 3030 case ISO_shift_out:
df7492f9
KH
3031 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT)
3032 || CODING_ISO_DESIGNATION (coding, 1) < 0)
3033 goto invalid_code;
3034 CODING_ISO_INVOCATION (coding, 0) = 1;
3035 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
b73bfc1c 3036 continue;
4ed46869
KH
3037
3038 case ISO_shift_in:
df7492f9
KH
3039 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT))
3040 goto invalid_code;
3041 CODING_ISO_INVOCATION (coding, 0) = 0;
3042 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
b73bfc1c 3043 continue;
4ed46869
KH
3044
3045 case ISO_single_shift_2_7:
3046 case ISO_single_shift_2:
df7492f9
KH
3047 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT))
3048 goto invalid_code;
4ed46869
KH
3049 /* SS2 is handled as an escape sequence of ESC 'N' */
3050 c1 = 'N';
3051 goto label_escape_sequence;
3052
3053 case ISO_single_shift_3:
df7492f9
KH
3054 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT))
3055 goto invalid_code;
4ed46869
KH
3056 /* SS2 is handled as an escape sequence of ESC 'O' */
3057 c1 = 'O';
3058 goto label_escape_sequence;
3059
3060 case ISO_control_sequence_introducer:
3061 /* CSI is handled as an escape sequence of ESC '[' ... */
3062 c1 = '[';
3063 goto label_escape_sequence;
3064
3065 case ISO_escape:
3066 ONE_MORE_BYTE (c1);
3067 label_escape_sequence:
df7492f9 3068 /* Escape sequences handled here are invocation,
4ed46869
KH
3069 designation, direction specification, and character
3070 composition specification. */
3071 switch (c1)
3072 {
3073 case '&': /* revision of following character set */
3074 ONE_MORE_BYTE (c1);
3075 if (!(c1 >= '@' && c1 <= '~'))
df7492f9 3076 goto invalid_code;
4ed46869
KH
3077 ONE_MORE_BYTE (c1);
3078 if (c1 != ISO_CODE_ESC)
df7492f9 3079 goto invalid_code;
4ed46869
KH
3080 ONE_MORE_BYTE (c1);
3081 goto label_escape_sequence;
3082
3083 case '$': /* designation of 2-byte character set */
df7492f9
KH
3084 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION))
3085 goto invalid_code;
134b9549
KH
3086 {
3087 int reg, chars96;
3088
3089 ONE_MORE_BYTE (c1);
3090 if (c1 >= '@' && c1 <= 'B')
3091 { /* designation of JISX0208.1978, GB2312.1980,
88993dfd 3092 or JISX0208.1980 */
134b9549
KH
3093 reg = 0, chars96 = 0;
3094 }
3095 else if (c1 >= 0x28 && c1 <= 0x2B)
3096 { /* designation of DIMENSION2_CHARS94 character set */
3097 reg = c1 - 0x28, chars96 = 0;
3098 ONE_MORE_BYTE (c1);
3099 }
3100 else if (c1 >= 0x2C && c1 <= 0x2F)
3101 { /* designation of DIMENSION2_CHARS96 character set */
3102 reg = c1 - 0x2C, chars96 = 1;
3103 ONE_MORE_BYTE (c1);
3104 }
3105 else
3106 goto invalid_code;
3107 DECODE_DESIGNATION (reg, 2, chars96, c1);
3108 /* We must update these variables now. */
3109 if (reg == 0)
3110 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
3111 else if (reg == 1)
3112 charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1);
3113 if (chars96 < 0)
3114 goto invalid_code;
3115 }
b73bfc1c 3116 continue;
4ed46869
KH
3117
3118 case 'n': /* invocation of locking-shift-2 */
df7492f9
KH
3119 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT)
3120 || CODING_ISO_DESIGNATION (coding, 2) < 0)
3121 goto invalid_code;
3122 CODING_ISO_INVOCATION (coding, 0) = 2;
3123 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
b73bfc1c 3124 continue;
4ed46869
KH
3125
3126 case 'o': /* invocation of locking-shift-3 */
df7492f9
KH
3127 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT)
3128 || CODING_ISO_DESIGNATION (coding, 3) < 0)
3129 goto invalid_code;
3130 CODING_ISO_INVOCATION (coding, 0) = 3;
3131 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
b73bfc1c 3132 continue;
4ed46869
KH
3133
3134 case 'N': /* invocation of single-shift-2 */
df7492f9
KH
3135 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)
3136 || CODING_ISO_DESIGNATION (coding, 2) < 0)
3137 goto invalid_code;
134b9549
KH
3138 charset_id_2 = CODING_ISO_DESIGNATION (coding, 2);
3139 if (charset_id_2 < 0)
3140 charset = CHARSET_FROM_ID (charset_ascii);
3141 else
3142 charset = CHARSET_FROM_ID (charset_id_2);
b73bfc1c 3143 ONE_MORE_BYTE (c1);
e7046a18 3144 if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0))
df7492f9 3145 goto invalid_code;
4ed46869
KH
3146 break;
3147
3148 case 'O': /* invocation of single-shift-3 */
df7492f9
KH
3149 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)
3150 || CODING_ISO_DESIGNATION (coding, 3) < 0)
3151 goto invalid_code;
134b9549
KH
3152 charset_id_3 = CODING_ISO_DESIGNATION (coding, 3);
3153 if (charset_id_3 < 0)
3154 charset = CHARSET_FROM_ID (charset_ascii);
3155 else
3156 charset = CHARSET_FROM_ID (charset_id_3);
b73bfc1c 3157 ONE_MORE_BYTE (c1);
e7046a18 3158 if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0))
df7492f9 3159 goto invalid_code;
4ed46869
KH
3160 break;
3161
ec6d2bb8 3162 case '0': case '2': case '3': case '4': /* start composition */
df7492f9
KH
3163 if (! (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK))
3164 goto invalid_code;
ec6d2bb8 3165 DECODE_COMPOSITION_START (c1);
b73bfc1c 3166 continue;
4ed46869 3167
ec6d2bb8 3168 case '1': /* end composition */
df7492f9
KH
3169 if (composition_state == COMPOSING_NO)
3170 goto invalid_code;
3171 DECODE_COMPOSITION_END ();
b73bfc1c 3172 continue;
4ed46869
KH
3173
3174 case '[': /* specification of direction */
df7492f9
KH
3175 if (! CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DIRECTION)
3176 goto invalid_code;
4ed46869 3177 /* For the moment, nested direction is not supported.
d46c5b12 3178 So, `coding->mode & CODING_MODE_DIRECTION' zero means
df7492f9 3179 left-to-right, and nozero means right-to-left. */
4ed46869
KH
3180 ONE_MORE_BYTE (c1);
3181 switch (c1)
3182 {
3183 case ']': /* end of the current direction */
d46c5b12 3184 coding->mode &= ~CODING_MODE_DIRECTION;
4ed46869
KH
3185
3186 case '0': /* end of the current direction */
3187 case '1': /* start of left-to-right direction */
3188 ONE_MORE_BYTE (c1);
3189 if (c1 == ']')
d46c5b12 3190 coding->mode &= ~CODING_MODE_DIRECTION;
4ed46869 3191 else
df7492f9 3192 goto invalid_code;
4ed46869
KH
3193 break;
3194
3195 case '2': /* start of right-to-left direction */
3196 ONE_MORE_BYTE (c1);
3197 if (c1 == ']')
d46c5b12 3198 coding->mode |= CODING_MODE_DIRECTION;
4ed46869 3199 else
df7492f9 3200 goto invalid_code;
4ed46869
KH
3201 break;
3202
3203 default:
df7492f9 3204 goto invalid_code;
4ed46869 3205 }
b73bfc1c 3206 continue;
4ed46869 3207
103e0180 3208 case '%':
103e0180
KH
3209 ONE_MORE_BYTE (c1);
3210 if (c1 == '/')
3211 {
3212 /* CTEXT extended segment:
3213 ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES--
3214 We keep these bytes as is for the moment.
3215 They may be decoded by post-read-conversion. */
3216 int dim, M, L;
4776e638 3217 int size;
8f924df7 3218
103e0180
KH
3219 ONE_MORE_BYTE (dim);
3220 ONE_MORE_BYTE (M);
3221 ONE_MORE_BYTE (L);
3222 size = ((M - 128) * 128) + (L - 128);
4776e638
KH
3223 if (charbuf + 8 + size > charbuf_end)
3224 goto break_loop;
3225 *charbuf++ = ISO_CODE_ESC;
3226 *charbuf++ = '%';
3227 *charbuf++ = '/';
3228 *charbuf++ = dim;
3229 *charbuf++ = BYTE8_TO_CHAR (M);
3230 *charbuf++ = BYTE8_TO_CHAR (L);
103e0180
KH
3231 while (size-- > 0)
3232 {
3233 ONE_MORE_BYTE (c1);
4776e638 3234 *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1);
103e0180 3235 }
103e0180
KH
3236 }
3237 else if (c1 == 'G')
3238 {
103e0180
KH
3239 /* XFree86 extension for embedding UTF-8 in CTEXT:
3240 ESC % G --UTF-8-BYTES-- ESC % @
3241 We keep these bytes as is for the moment.
3242 They may be decoded by post-read-conversion. */
4776e638
KH
3243 int *p = charbuf;
3244
3245 if (p + 6 > charbuf_end)
3246 goto break_loop;
3247 *p++ = ISO_CODE_ESC;
3248 *p++ = '%';
3249 *p++ = 'G';
3250 while (p < charbuf_end)
103e0180
KH
3251 {
3252 ONE_MORE_BYTE (c1);
3253 if (c1 == ISO_CODE_ESC
3254 && src + 1 < src_end
3255 && src[0] == '%'
3256 && src[1] == '@')
9ffd559c
KH
3257 {
3258 src += 2;
3259 break;
3260 }
4776e638 3261 *p++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1);
103e0180 3262 }
4776e638
KH
3263 if (p + 3 > charbuf_end)
3264 goto break_loop;
3265 *p++ = ISO_CODE_ESC;
3266 *p++ = '%';
3267 *p++ = '@';
3268 charbuf = p;
103e0180
KH
3269 }
3270 else
4776e638 3271 goto invalid_code;
103e0180 3272 continue;
4776e638 3273 break;
103e0180 3274
4ed46869 3275 default:
df7492f9
KH
3276 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION))
3277 goto invalid_code;
134b9549
KH
3278 {
3279 int reg, chars96;
3280
3281 if (c1 >= 0x28 && c1 <= 0x2B)
3282 { /* designation of DIMENSION1_CHARS94 character set */
3283 reg = c1 - 0x28, chars96 = 0;
3284 ONE_MORE_BYTE (c1);
3285 }
3286 else if (c1 >= 0x2C && c1 <= 0x2F)
3287 { /* designation of DIMENSION1_CHARS96 character set */
3288 reg = c1 - 0x2C, chars96 = 1;
3289 ONE_MORE_BYTE (c1);
3290 }
3291 else
3292 goto invalid_code;
3293 DECODE_DESIGNATION (reg, 1, chars96, c1);
3294 /* We must update these variables now. */
3295 if (reg == 0)
3296 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
3297 else if (reg == 1)
3298 charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1);
3299 if (chars96 < 0)
3300 goto invalid_code;
3301 }
b73bfc1c 3302 continue;
4ed46869 3303 }
b73bfc1c 3304 }
4ed46869 3305
ff0dacd7
KH
3306 if (charset->id != charset_ascii
3307 && last_id != charset->id)
3308 {
3309 if (last_id != charset_ascii)
69a80ea3 3310 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
ff0dacd7
KH
3311 last_id = charset->id;
3312 last_offset = char_offset;
3313 }
3314
b73bfc1c 3315 /* Now we know CHARSET and 1st position code C1 of a character.
df7492f9
KH
3316 Produce a decoded character while getting 2nd position code
3317 C2 if necessary. */
3318 c1 &= 0x7F;
3319 if (CHARSET_DIMENSION (charset) > 1)
b73bfc1c
KH
3320 {
3321 ONE_MORE_BYTE (c2);
df7492f9 3322 if (c2 < 0x20 || (c2 >= 0x80 && c2 < 0xA0))
b73bfc1c 3323 /* C2 is not in a valid range. */
df7492f9
KH
3324 goto invalid_code;
3325 c1 = (c1 << 8) | (c2 & 0x7F);
3326 if (CHARSET_DIMENSION (charset) > 2)
3327 {
3328 ONE_MORE_BYTE (c2);
3329 if (c2 < 0x20 || (c2 >= 0x80 && c2 < 0xA0))
3330 /* C2 is not in a valid range. */
3331 goto invalid_code;
3332 c1 = (c1 << 8) | (c2 & 0x7F);
3333 }
3334 }
3335
3336 CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c1, c);
3337 if (c < 0)
3338 {
3339 MAYBE_FINISH_COMPOSITION ();
3340 for (; src_base < src; src_base++, char_offset++)
3341 {
3342 if (ASCII_BYTE_P (*src_base))
3343 *charbuf++ = *src_base;
3344 else
3345 *charbuf++ = BYTE8_TO_CHAR (*src_base);
3346 }
3347 }
3348 else if (composition_state == COMPOSING_NO)
3349 {
3350 *charbuf++ = c;
3351 char_offset++;
4ed46869 3352 }
df7492f9 3353 else
781d7a48
KH
3354 {
3355 components[component_idx++] = c;
3356 if (method == COMPOSITION_WITH_RULE
3357 || (method == COMPOSITION_WITH_RULE_ALTCHARS
3358 && composition_state == COMPOSING_COMPONENT_CHAR))
3359 composition_state++;
4ed46869
KH
3360 }
3361 continue;
3362
df7492f9
KH
3363 invalid_code:
3364 MAYBE_FINISH_COMPOSITION ();
4ed46869 3365 src = src_base;
df7492f9
KH
3366 consumed_chars = consumed_chars_base;
3367 ONE_MORE_BYTE (c);
065e3595 3368 *charbuf++ = c < 0 ? -c : ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
ff0dacd7 3369 char_offset++;
df7492f9 3370 coding->errors++;
4776e638
KH
3371 continue;
3372
3373 break_loop:
3374 break;
4ed46869 3375 }
fb88bf2d 3376
df7492f9 3377 no_more_source:
ff0dacd7 3378 if (last_id != charset_ascii)
69a80ea3 3379 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
df7492f9
KH
3380 coding->consumed_char += consumed_chars_base;
3381 coding->consumed = src_base - coding->source;
3382 coding->charbuf_used = charbuf - coding->charbuf;
4ed46869
KH
3383}
3384
b73bfc1c 3385
f4dee582 3386/* ISO2022 encoding stuff. */
4ed46869
KH
3387
3388/*
f4dee582 3389 It is not enough to say just "ISO2022" on encoding, we have to
df7492f9 3390 specify more details. In Emacs, each coding system of ISO2022
4ed46869 3391 variant has the following specifications:
df7492f9 3392 1. Initial designation to G0 thru G3.
4ed46869
KH
3393 2. Allows short-form designation?
3394 3. ASCII should be designated to G0 before control characters?
3395 4. ASCII should be designated to G0 at end of line?
3396 5. 7-bit environment or 8-bit environment?
3397 6. Use locking-shift?
3398 7. Use Single-shift?
3399 And the following two are only for Japanese:
3400 8. Use ASCII in place of JIS0201-1976-Roman?
3401 9. Use JISX0208-1983 in place of JISX0208-1978?
df7492f9
KH
3402 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
3403 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
f4dee582 3404 details.
4ed46869
KH
3405*/
3406
3407/* Produce codes (escape sequence) for designating CHARSET to graphic
b73bfc1c
KH
3408 register REG at DST, and increment DST. If <final-char> of CHARSET is
3409 '@', 'A', or 'B' and the coding system CODING allows, produce
3410 designation sequence of short-form. */
4ed46869
KH
3411
3412#define ENCODE_DESIGNATION(charset, reg, coding) \
3413 do { \
df7492f9 3414 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
4ed46869
KH
3415 char *intermediate_char_94 = "()*+"; \
3416 char *intermediate_char_96 = ",-./"; \
df7492f9
KH
3417 int revision = -1; \
3418 int c; \
3419 \
3420 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
c197f191 3421 revision = CHARSET_ISO_REVISION (charset); \
df7492f9
KH
3422 \
3423 if (revision >= 0) \
70c22245 3424 { \
df7492f9
KH
3425 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
3426 EMIT_ONE_BYTE ('@' + revision); \
4ed46869 3427 } \
df7492f9 3428 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
4ed46869
KH
3429 if (CHARSET_DIMENSION (charset) == 1) \
3430 { \
df7492f9
KH
3431 if (! CHARSET_ISO_CHARS_96 (charset)) \
3432 c = intermediate_char_94[reg]; \
4ed46869 3433 else \
df7492f9
KH
3434 c = intermediate_char_96[reg]; \
3435 EMIT_ONE_ASCII_BYTE (c); \
4ed46869
KH
3436 } \
3437 else \
3438 { \
df7492f9
KH
3439 EMIT_ONE_ASCII_BYTE ('$'); \
3440 if (! CHARSET_ISO_CHARS_96 (charset)) \
4ed46869 3441 { \
df7492f9 3442 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
b73bfc1c
KH
3443 || reg != 0 \
3444 || final_char < '@' || final_char > 'B') \
df7492f9 3445 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
4ed46869
KH
3446 } \
3447 else \
df7492f9 3448 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
4ed46869 3449 } \
df7492f9
KH
3450 EMIT_ONE_ASCII_BYTE (final_char); \
3451 \
3452 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
4ed46869
KH
3453 } while (0)
3454
df7492f9 3455
4ed46869
KH
3456/* The following two macros produce codes (control character or escape
3457 sequence) for ISO2022 single-shift functions (single-shift-2 and
3458 single-shift-3). */
3459
df7492f9
KH
3460#define ENCODE_SINGLE_SHIFT_2 \
3461 do { \
3462 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3463 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
3464 else \
3465 EMIT_ONE_BYTE (ISO_CODE_SS2); \
3466 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4ed46869
KH
3467 } while (0)
3468
df7492f9
KH
3469
3470#define ENCODE_SINGLE_SHIFT_3 \
3471 do { \
3472 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3473 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
3474 else \
3475 EMIT_ONE_BYTE (ISO_CODE_SS3); \
3476 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
4ed46869
KH
3477 } while (0)
3478
df7492f9 3479
4ed46869
KH
3480/* The following four macros produce codes (control character or
3481 escape sequence) for ISO2022 locking-shift functions (shift-in,
3482 shift-out, locking-shift-2, and locking-shift-3). */
3483
df7492f9
KH
3484#define ENCODE_SHIFT_IN \
3485 do { \
3486 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
3487 CODING_ISO_INVOCATION (coding, 0) = 0; \
4ed46869
KH
3488 } while (0)
3489
df7492f9
KH
3490
3491#define ENCODE_SHIFT_OUT \
3492 do { \
3493 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
3494 CODING_ISO_INVOCATION (coding, 0) = 1; \
4ed46869
KH
3495 } while (0)
3496
df7492f9
KH
3497
3498#define ENCODE_LOCKING_SHIFT_2 \
3499 do { \
3500 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
3501 CODING_ISO_INVOCATION (coding, 0) = 2; \
4ed46869
KH
3502 } while (0)
3503
df7492f9
KH
3504
3505#define ENCODE_LOCKING_SHIFT_3 \
3506 do { \
3507 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
3508 CODING_ISO_INVOCATION (coding, 0) = 3; \
4ed46869
KH
3509 } while (0)
3510
df7492f9 3511
f4dee582
RS
3512/* Produce codes for a DIMENSION1 character whose character set is
3513 CHARSET and whose position-code is C1. Designation and invocation
4ed46869
KH
3514 sequences are also produced in advance if necessary. */
3515
6e85d753
KH
3516#define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
3517 do { \
df7492f9 3518 int id = CHARSET_ID (charset); \
bf16eb23
KH
3519 \
3520 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3521 && id == charset_ascii) \
3522 { \
3523 id = charset_jisx0201_roman; \
3524 charset = CHARSET_FROM_ID (id); \
3525 } \
3526 \
df7492f9 3527 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
6e85d753 3528 { \
df7492f9
KH
3529 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3530 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
6e85d753 3531 else \
df7492f9
KH
3532 EMIT_ONE_BYTE (c1 | 0x80); \
3533 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
6e85d753
KH
3534 break; \
3535 } \
df7492f9 3536 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
6e85d753 3537 { \
df7492f9 3538 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
6e85d753
KH
3539 break; \
3540 } \
df7492f9 3541 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
6e85d753 3542 { \
df7492f9 3543 EMIT_ONE_BYTE (c1 | 0x80); \
6e85d753
KH
3544 break; \
3545 } \
6e85d753
KH
3546 else \
3547 /* Since CHARSET is not yet invoked to any graphic planes, we \
3548 must invoke it, or, at first, designate it to some graphic \
3549 register. Then repeat the loop to actually produce the \
3550 character. */ \
df7492f9
KH
3551 dst = encode_invocation_designation (charset, coding, dst, \
3552 &produced_chars); \
4ed46869
KH
3553 } while (1)
3554
df7492f9 3555
f4dee582
RS
3556/* Produce codes for a DIMENSION2 character whose character set is
3557 CHARSET and whose position-codes are C1 and C2. Designation and
4ed46869
KH
3558 invocation codes are also produced in advance if necessary. */
3559
6e85d753
KH
3560#define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
3561 do { \
df7492f9 3562 int id = CHARSET_ID (charset); \
bf16eb23
KH
3563 \
3564 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3565 && id == charset_jisx0208) \
3566 { \
3567 id = charset_jisx0208_1978; \
3568 charset = CHARSET_FROM_ID (id); \
3569 } \
3570 \
df7492f9 3571 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
6e85d753 3572 { \
df7492f9
KH
3573 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
3574 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
6e85d753 3575 else \
df7492f9
KH
3576 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
3577 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
6e85d753
KH
3578 break; \
3579 } \
df7492f9 3580 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
6e85d753 3581 { \
df7492f9 3582 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
6e85d753
KH
3583 break; \
3584 } \
df7492f9 3585 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
6e85d753 3586 { \
df7492f9 3587 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
6e85d753
KH
3588 break; \
3589 } \
6e85d753
KH
3590 else \
3591 /* Since CHARSET is not yet invoked to any graphic planes, we \
3592 must invoke it, or, at first, designate it to some graphic \
3593 register. Then repeat the loop to actually produce the \
3594 character. */ \
df7492f9
KH
3595 dst = encode_invocation_designation (charset, coding, dst, \
3596 &produced_chars); \
4ed46869
KH
3597 } while (1)
3598
05e6f5dc 3599
df7492f9
KH
3600#define ENCODE_ISO_CHARACTER(charset, c) \
3601 do { \
3602 int code = ENCODE_CHAR ((charset),(c)); \
3603 \
3604 if (CHARSET_DIMENSION (charset) == 1) \
3605 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
3606 else \
3607 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
84fbb8a0 3608 } while (0)
bdd9fb48 3609
05e6f5dc 3610
4ed46869 3611/* Produce designation and invocation codes at a place pointed by DST
df7492f9 3612 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
4ed46869
KH
3613 Return new DST. */
3614
3615unsigned char *
df7492f9
KH
3616encode_invocation_designation (charset, coding, dst, p_nchars)
3617 struct charset *charset;
4ed46869
KH
3618 struct coding_system *coding;
3619 unsigned char *dst;
df7492f9 3620 int *p_nchars;
4ed46869 3621{
df7492f9
KH
3622 int multibytep = coding->dst_multibyte;
3623 int produced_chars = *p_nchars;
4ed46869 3624 int reg; /* graphic register number */
df7492f9 3625 int id = CHARSET_ID (charset);
4ed46869
KH
3626
3627 /* At first, check designations. */
3628 for (reg = 0; reg < 4; reg++)
df7492f9 3629 if (id == CODING_ISO_DESIGNATION (coding, reg))
4ed46869
KH
3630 break;
3631
3632 if (reg >= 4)
3633 {
3634 /* CHARSET is not yet designated to any graphic registers. */
3635 /* At first check the requested designation. */
df7492f9
KH
3636 reg = CODING_ISO_REQUEST (coding, id);
3637 if (reg < 0)
1ba9e4ab
KH
3638 /* Since CHARSET requests no special designation, designate it
3639 to graphic register 0. */
4ed46869
KH
3640 reg = 0;
3641
3642 ENCODE_DESIGNATION (charset, reg, coding);
3643 }
3644
df7492f9
KH
3645 if (CODING_ISO_INVOCATION (coding, 0) != reg
3646 && CODING_ISO_INVOCATION (coding, 1) != reg)
4ed46869
KH
3647 {
3648 /* Since the graphic register REG is not invoked to any graphic
3649 planes, invoke it to graphic plane 0. */
3650 switch (reg)
3651 {
3652 case 0: /* graphic register 0 */
3653 ENCODE_SHIFT_IN;
3654 break;
3655
3656 case 1: /* graphic register 1 */
3657 ENCODE_SHIFT_OUT;
3658 break;
3659
3660 case 2: /* graphic register 2 */
df7492f9 3661 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)
4ed46869
KH
3662 ENCODE_SINGLE_SHIFT_2;
3663 else
3664 ENCODE_LOCKING_SHIFT_2;
3665 break;
3666
3667 case 3: /* graphic register 3 */
df7492f9 3668 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)
4ed46869
KH
3669 ENCODE_SINGLE_SHIFT_3;
3670 else
3671 ENCODE_LOCKING_SHIFT_3;
3672 break;
3673 }
3674 }
b73bfc1c 3675
df7492f9 3676 *p_nchars = produced_chars;
4ed46869
KH
3677 return dst;
3678}
3679
df7492f9
KH
3680/* The following three macros produce codes for indicating direction
3681 of text. */
3682#define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
ec6d2bb8 3683 do { \
df7492f9
KH
3684 if (CODING_ISO_FLAGS (coding) == CODING_ISO_FLAG_SEVEN_BITS) \
3685 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '['); \
ec6d2bb8 3686 else \
df7492f9 3687 EMIT_ONE_BYTE (ISO_CODE_CSI); \
ec6d2bb8
KH
3688 } while (0)
3689
ec6d2bb8 3690
df7492f9
KH
3691#define ENCODE_DIRECTION_R2L() \
3692 do { \
3693 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
3694 EMIT_TWO_ASCII_BYTES ('2', ']'); \
ec6d2bb8
KH
3695 } while (0)
3696
ec6d2bb8 3697
df7492f9 3698#define ENCODE_DIRECTION_L2R() \
ec6d2bb8 3699 do { \
df7492f9
KH
3700 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
3701 EMIT_TWO_ASCII_BYTES ('0', ']'); \
ec6d2bb8 3702 } while (0)
4ed46869 3703
4ed46869
KH
3704
3705/* Produce codes for designation and invocation to reset the graphic
3706 planes and registers to initial state. */
df7492f9
KH
3707#define ENCODE_RESET_PLANE_AND_REGISTER() \
3708 do { \
3709 int reg; \
3710 struct charset *charset; \
3711 \
3712 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
3713 ENCODE_SHIFT_IN; \
3714 for (reg = 0; reg < 4; reg++) \
3715 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
3716 && (CODING_ISO_DESIGNATION (coding, reg) \
3717 != CODING_ISO_INITIAL (coding, reg))) \
3718 { \
3719 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
3720 ENCODE_DESIGNATION (charset, reg, coding); \
3721 } \
4ed46869
KH
3722 } while (0)
3723
df7492f9 3724
bdd9fb48 3725/* Produce designation sequences of charsets in the line started from
b73bfc1c 3726 SRC to a place pointed by DST, and return updated DST.
bdd9fb48
KH
3727
3728 If the current block ends before any end-of-line, we may fail to
d46c5b12
KH
3729 find all the necessary designations. */
3730
b73bfc1c 3731static unsigned char *
df7492f9 3732encode_designation_at_bol (coding, charbuf, charbuf_end, dst)
e0e989f6 3733 struct coding_system *coding;
df7492f9
KH
3734 int *charbuf, *charbuf_end;
3735 unsigned char *dst;
e0e989f6 3736{
df7492f9 3737 struct charset *charset;
bdd9fb48
KH
3738 /* Table of charsets to be designated to each graphic register. */
3739 int r[4];
df7492f9
KH
3740 int c, found = 0, reg;
3741 int produced_chars = 0;
3742 int multibytep = coding->dst_multibyte;
3743 Lisp_Object attrs;
3744 Lisp_Object charset_list;
3745
3746 attrs = CODING_ID_ATTRS (coding->id);
3747 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
3748 if (EQ (charset_list, Qiso_2022))
3749 charset_list = Viso_2022_charset_list;
bdd9fb48
KH
3750
3751 for (reg = 0; reg < 4; reg++)
3752 r[reg] = -1;
3753
b73bfc1c 3754 while (found < 4)
e0e989f6 3755 {
df7492f9
KH
3756 int id;
3757
3758 c = *charbuf++;
b73bfc1c
KH
3759 if (c == '\n')
3760 break;
df7492f9
KH
3761 charset = char_charset (c, charset_list, NULL);
3762 id = CHARSET_ID (charset);
3763 reg = CODING_ISO_REQUEST (coding, id);
3764 if (reg >= 0 && r[reg] < 0)
bdd9fb48
KH
3765 {
3766 found++;
df7492f9 3767 r[reg] = id;
bdd9fb48 3768 }
bdd9fb48
KH
3769 }
3770
3771 if (found)
3772 {
3773 for (reg = 0; reg < 4; reg++)
3774 if (r[reg] >= 0
df7492f9
KH
3775 && CODING_ISO_DESIGNATION (coding, reg) != r[reg])
3776 ENCODE_DESIGNATION (CHARSET_FROM_ID (r[reg]), reg, coding);
e0e989f6 3777 }
b73bfc1c
KH
3778
3779 return dst;
e0e989f6
KH
3780}
3781
4ed46869
KH
3782/* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
3783
df7492f9
KH
3784static int
3785encode_coding_iso_2022 (coding)
4ed46869 3786 struct coding_system *coding;
4ed46869 3787{
df7492f9
KH
3788 int multibytep = coding->dst_multibyte;
3789 int *charbuf = coding->charbuf;
3790 int *charbuf_end = charbuf + coding->charbuf_used;
3791 unsigned char *dst = coding->destination + coding->produced;
3792 unsigned char *dst_end = coding->destination + coding->dst_bytes;
3793 int safe_room = 16;
3794 int bol_designation
3795 = (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
3796 && CODING_ISO_BOL (coding));
3797 int produced_chars = 0;
3798 Lisp_Object attrs, eol_type, charset_list;
3799 int ascii_compatible;
b73bfc1c 3800 int c;
ff0dacd7 3801 int preferred_charset_id = -1;
05e6f5dc 3802
24a73b0a
KH
3803 CODING_GET_INFO (coding, attrs, charset_list);
3804 eol_type = CODING_ID_EOL_TYPE (coding->id);
3805 if (VECTORP (eol_type))
3806 eol_type = Qunix;
3807
004068e4 3808 setup_iso_safe_charsets (attrs);
ff0dacd7 3809 /* Charset list may have been changed. */
287c57d7 3810 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
8f924df7 3811 coding->safe_charsets = (char *) SDATA (CODING_ATTR_SAFE_CHARSETS(attrs));
0eecad43 3812
df7492f9 3813 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
bdd9fb48 3814
df7492f9 3815 while (charbuf < charbuf_end)
4ed46869 3816 {
df7492f9 3817 ASSURE_DESTINATION (safe_room);
b73bfc1c 3818
df7492f9 3819 if (bol_designation)
b73bfc1c 3820 {
df7492f9 3821 unsigned char *dst_prev = dst;
4ed46869 3822
bdd9fb48 3823 /* We have to produce designation sequences if any now. */
df7492f9
KH
3824 dst = encode_designation_at_bol (coding, charbuf, charbuf_end, dst);
3825 bol_designation = 0;
3826 /* We are sure that designation sequences are all ASCII bytes. */
3827 produced_chars += dst - dst_prev;
e0e989f6
KH
3828 }
3829
df7492f9 3830 c = *charbuf++;
ec6d2bb8 3831
ff0dacd7
KH
3832 if (c < 0)
3833 {
3834 /* Handle an annotation. */
3835 switch (*charbuf)
ec6d2bb8 3836 {
ff0dacd7
KH
3837 case CODING_ANNOTATE_COMPOSITION_MASK:
3838 /* Not yet implemented. */
3839 break;
3840 case CODING_ANNOTATE_CHARSET_MASK:
cf7dfdf5 3841 preferred_charset_id = charbuf[2];
ff0dacd7
KH
3842 if (preferred_charset_id >= 0
3843 && NILP (Fmemq (make_number (preferred_charset_id),
3844 charset_list)))
3845 preferred_charset_id = -1;
3846 break;
3847 default:
3848 abort ();
4ed46869 3849 }
ff0dacd7
KH
3850 charbuf += -c - 1;
3851 continue;
4ed46869 3852 }
ec6d2bb8 3853
b73bfc1c
KH
3854 /* Now encode the character C. */
3855 if (c < 0x20 || c == 0x7F)
3856 {
df7492f9
KH
3857 if (c == '\n'
3858 || (c == '\r' && EQ (eol_type, Qmac)))
19a8d9e0 3859 {
df7492f9
KH
3860 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL)
3861 ENCODE_RESET_PLANE_AND_REGISTER ();
3862 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_INIT_AT_BOL)
b73bfc1c 3863 {
df7492f9
KH
3864 int i;
3865
3866 for (i = 0; i < 4; i++)
3867 CODING_ISO_DESIGNATION (coding, i)
3868 = CODING_ISO_INITIAL (coding, i);
b73bfc1c 3869 }
df7492f9
KH
3870 bol_designation
3871 = CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATE_AT_BOL;
19a8d9e0 3872 }
df7492f9
KH
3873 else if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_CNTL)
3874 ENCODE_RESET_PLANE_AND_REGISTER ();
3875 EMIT_ONE_ASCII_BYTE (c);
4ed46869 3876 }
df7492f9 3877 else if (ASCII_CHAR_P (c))
88993dfd 3878 {
df7492f9
KH
3879 if (ascii_compatible)
3880 EMIT_ONE_ASCII_BYTE (c);
93dec019 3881 else
19a8d9e0 3882 {
bf16eb23
KH
3883 struct charset *charset = CHARSET_FROM_ID (charset_ascii);
3884 ENCODE_ISO_CHARACTER (charset, c);
19a8d9e0 3885 }
4ed46869 3886 }
16eafb5d 3887 else if (CHAR_BYTE8_P (c))
88993dfd 3888 {
16eafb5d
KH
3889 c = CHAR_TO_BYTE8 (c);
3890 EMIT_ONE_BYTE (c);
88993dfd 3891 }
b73bfc1c 3892 else
df7492f9 3893 {
ff0dacd7 3894 struct charset *charset;
b73bfc1c 3895
ff0dacd7
KH
3896 if (preferred_charset_id >= 0)
3897 {
3898 charset = CHARSET_FROM_ID (preferred_charset_id);
3899 if (! CHAR_CHARSET_P (c, charset))
3900 charset = char_charset (c, charset_list, NULL);
3901 }
3902 else
3903 charset = char_charset (c, charset_list, NULL);
df7492f9
KH
3904 if (!charset)
3905 {
41cbe562
KH
3906 if (coding->mode & CODING_MODE_SAFE_ENCODING)
3907 {
3908 c = CODING_INHIBIT_CHARACTER_SUBSTITUTION;
3909 charset = CHARSET_FROM_ID (charset_ascii);
3910 }
3911 else
3912 {
3913 c = coding->default_char;
3914 charset = char_charset (c, charset_list, NULL);
3915 }
df7492f9
KH
3916 }
3917 ENCODE_ISO_CHARACTER (charset, c);
3918 }
84fbb8a0 3919 }
b73bfc1c 3920
df7492f9
KH
3921 if (coding->mode & CODING_MODE_LAST_BLOCK
3922 && CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL)
3923 {
3924 ASSURE_DESTINATION (safe_room);
3925 ENCODE_RESET_PLANE_AND_REGISTER ();
3926 }
065e3595 3927 record_conversion_result (coding, CODING_RESULT_SUCCESS);
df7492f9
KH
3928 CODING_ISO_BOL (coding) = bol_designation;
3929 coding->produced_char += produced_chars;
3930 coding->produced = dst - coding->destination;
3931 return 0;
4ed46869
KH
3932}
3933
3934\f
df7492f9 3935/*** 8,9. SJIS and BIG5 handlers ***/
4ed46869 3936
df7492f9 3937/* Although SJIS and BIG5 are not ISO's coding system, they are used
4ed46869
KH
3938 quite widely. So, for the moment, Emacs supports them in the bare
3939 C code. But, in the future, they may be supported only by CCL. */
3940
3941/* SJIS is a coding system encoding three character sets: ASCII, right
3942 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
3943 as is. A character of charset katakana-jisx0201 is encoded by
3944 "position-code + 0x80". A character of charset japanese-jisx0208
3945 is encoded in 2-byte but two position-codes are divided and shifted
df7492f9 3946 so that it fit in the range below.
4ed46869
KH
3947
3948 --- CODE RANGE of SJIS ---
3949 (character set) (range)
3950 ASCII 0x00 .. 0x7F
df7492f9 3951 KATAKANA-JISX0201 0xA0 .. 0xDF
c28a9453 3952 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
d14d03ac 3953 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
4ed46869
KH
3954 -------------------------------
3955
3956*/
3957
3958/* BIG5 is a coding system encoding two character sets: ASCII and
3959 Big5. An ASCII character is encoded as is. Big5 is a two-byte
df7492f9 3960 character set and is encoded in two-byte.
4ed46869
KH
3961
3962 --- CODE RANGE of BIG5 ---
3963 (character set) (range)
3964 ASCII 0x00 .. 0x7F
3965 Big5 (1st byte) 0xA1 .. 0xFE
3966 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
3967 --------------------------
3968
df7492f9 3969 */
4ed46869
KH
3970
3971/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
3972 Check if a text is encoded in SJIS. If it is, return
df7492f9 3973 CATEGORY_MASK_SJIS, else return 0. */
4ed46869 3974
0a28aafb 3975static int
ff0dacd7 3976detect_coding_sjis (coding, detect_info)
df7492f9 3977 struct coding_system *coding;
ff0dacd7 3978 struct coding_detection_info *detect_info;
4ed46869 3979{
065e3595 3980 const unsigned char *src = coding->source, *src_base;
8f924df7 3981 const unsigned char *src_end = coding->source + coding->src_bytes;
df7492f9
KH
3982 int multibytep = coding->src_multibyte;
3983 int consumed_chars = 0;
3984 int found = 0;
b73bfc1c 3985 int c;
df7492f9 3986
ff0dacd7 3987 detect_info->checked |= CATEGORY_MASK_SJIS;
df7492f9
KH
3988 /* A coding system of this category is always ASCII compatible. */
3989 src += coding->head_ascii;
4ed46869 3990
b73bfc1c 3991 while (1)
4ed46869 3992 {
065e3595 3993 src_base = src;
df7492f9 3994 ONE_MORE_BYTE (c);
682169fe
KH
3995 if (c < 0x80)
3996 continue;
df7492f9 3997 if ((c >= 0x81 && c <= 0x9F) || (c >= 0xE0 && c <= 0xEF))
4ed46869 3998 {
df7492f9 3999 ONE_MORE_BYTE (c);
682169fe 4000 if (c < 0x40 || c == 0x7F || c > 0xFC)
df7492f9 4001 break;
ff0dacd7 4002 found = CATEGORY_MASK_SJIS;
4ed46869 4003 }
df7492f9 4004 else if (c >= 0xA0 && c < 0xE0)
ff0dacd7 4005 found = CATEGORY_MASK_SJIS;
df7492f9
KH
4006 else
4007 break;
4ed46869 4008 }
ff0dacd7 4009 detect_info->rejected |= CATEGORY_MASK_SJIS;
df7492f9
KH
4010 return 0;
4011
4012 no_more_source:
065e3595 4013 if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK)
89528eb3 4014 {
ff0dacd7 4015 detect_info->rejected |= CATEGORY_MASK_SJIS;
89528eb3 4016 return 0;
4ed46869 4017 }
ff0dacd7
KH
4018 detect_info->found |= found;
4019 return 1;
4ed46869
KH
4020}
4021
4022/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4023 Check if a text is encoded in BIG5. If it is, return
df7492f9 4024 CATEGORY_MASK_BIG5, else return 0. */
4ed46869 4025
0a28aafb 4026static int
ff0dacd7 4027detect_coding_big5 (coding, detect_info)
df7492f9 4028 struct coding_system *coding;
ff0dacd7 4029 struct coding_detection_info *detect_info;
4ed46869 4030{
065e3595 4031 const unsigned char *src = coding->source, *src_base;
8f924df7 4032 const unsigned char *src_end = coding->source + coding->src_bytes;
df7492f9
KH
4033 int multibytep = coding->src_multibyte;
4034 int consumed_chars = 0;
4035 int found = 0;
b73bfc1c 4036 int c;
fa42c37f 4037
ff0dacd7 4038 detect_info->checked |= CATEGORY_MASK_BIG5;
df7492f9
KH
4039 /* A coding system of this category is always ASCII compatible. */
4040 src += coding->head_ascii;
fa42c37f 4041
b73bfc1c 4042 while (1)
fa42c37f 4043 {
065e3595 4044 src_base = src;
df7492f9
KH
4045 ONE_MORE_BYTE (c);
4046 if (c < 0x80)
fa42c37f 4047 continue;
df7492f9 4048 if (c >= 0xA1)
fa42c37f 4049 {
df7492f9
KH
4050 ONE_MORE_BYTE (c);
4051 if (c < 0x40 || (c >= 0x7F && c <= 0xA0))
fa42c37f 4052 return 0;
ff0dacd7 4053 found = CATEGORY_MASK_BIG5;
fa42c37f 4054 }
df7492f9
KH
4055 else
4056 break;
fa42c37f 4057 }
ff0dacd7 4058 detect_info->rejected |= CATEGORY_MASK_BIG5;
fa42c37f 4059 return 0;
fa42c37f 4060
df7492f9 4061 no_more_source:
065e3595 4062 if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK)
89528eb3 4063 {
ff0dacd7 4064 detect_info->rejected |= CATEGORY_MASK_BIG5;
89528eb3
KH
4065 return 0;
4066 }
ff0dacd7
KH
4067 detect_info->found |= found;
4068 return 1;
fa42c37f
KH
4069}
4070
4ed46869
KH
4071/* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
4072 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
fa42c37f 4073
b73bfc1c 4074static void
df7492f9 4075decode_coding_sjis (coding)
4ed46869 4076 struct coding_system *coding;
4ed46869 4077{
8f924df7
KH
4078 const unsigned char *src = coding->source + coding->consumed;
4079 const unsigned char *src_end = coding->source + coding->src_bytes;
4080 const unsigned char *src_base;
69a80ea3
KH
4081 int *charbuf = coding->charbuf + coding->charbuf_used;
4082 int *charbuf_end
4083 = coding->charbuf + coding->charbuf_size - MAX_ANNOTATION_LENGTH;
df7492f9
KH
4084 int consumed_chars = 0, consumed_chars_base;
4085 int multibytep = coding->src_multibyte;
4086 struct charset *charset_roman, *charset_kanji, *charset_kana;
57a47f8a 4087 struct charset *charset_kanji2;
24a73b0a 4088 Lisp_Object attrs, charset_list, val;
ff0dacd7
KH
4089 int char_offset = coding->produced_char;
4090 int last_offset = char_offset;
4091 int last_id = charset_ascii;
a5d301df 4092
24a73b0a 4093 CODING_GET_INFO (coding, attrs, charset_list);
df7492f9
KH
4094
4095 val = charset_list;
4096 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
89528eb3 4097 charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
57a47f8a
KH
4098 charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4099 charset_kanji2 = NILP (val) ? NULL : CHARSET_FROM_ID (XINT (XCAR (val)));
fa42c37f 4100
b73bfc1c 4101 while (1)
4ed46869 4102 {
df7492f9 4103 int c, c1;
24a73b0a 4104 struct charset *charset;
fa42c37f 4105
b73bfc1c 4106 src_base = src;
df7492f9 4107 consumed_chars_base = consumed_chars;
fa42c37f 4108
df7492f9
KH
4109 if (charbuf >= charbuf_end)
4110 break;
4111
4112 ONE_MORE_BYTE (c);
065e3595
KH
4113 if (c < 0)
4114 goto invalid_code;
24a73b0a
KH
4115 if (c < 0x80)
4116 charset = charset_roman;
57a47f8a 4117 else if (c == 0x80 || c == 0xA0)
8e921c4b 4118 goto invalid_code;
57a47f8a
KH
4119 else if (c >= 0xA1 && c <= 0xDF)
4120 {
4121 /* SJIS -> JISX0201-Kana */
4122 c &= 0x7F;
4123 charset = charset_kana;
4124 }
4125 else if (c <= 0xEF)
df7492f9 4126 {
57a47f8a
KH
4127 /* SJIS -> JISX0208 */
4128 ONE_MORE_BYTE (c1);
4129 if (c1 < 0x40 || c1 == 0x7F || c1 > 0xFC)
24a73b0a 4130 goto invalid_code;
57a47f8a
KH
4131 c = (c << 8) | c1;
4132 SJIS_TO_JIS (c);
4133 charset = charset_kanji;
4134 }
4135 else if (c <= 0xFC && charset_kanji2)
4136 {
c6876370 4137 /* SJIS -> JISX0213-2 */
57a47f8a
KH
4138 ONE_MORE_BYTE (c1);
4139 if (c1 < 0x40 || c1 == 0x7F || c1 > 0xFC)
24a73b0a 4140 goto invalid_code;
57a47f8a
KH
4141 c = (c << 8) | c1;
4142 SJIS_TO_JIS2 (c);
4143 charset = charset_kanji2;
df7492f9 4144 }
57a47f8a
KH
4145 else
4146 goto invalid_code;
24a73b0a
KH
4147 if (charset->id != charset_ascii
4148 && last_id != charset->id)
4149 {
4150 if (last_id != charset_ascii)
69a80ea3 4151 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
24a73b0a
KH
4152 last_id = charset->id;
4153 last_offset = char_offset;
4154 }
4155 CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c);
df7492f9 4156 *charbuf++ = c;
ff0dacd7 4157 char_offset++;
df7492f9 4158 continue;
b73bfc1c 4159
df7492f9
KH
4160 invalid_code:
4161 src = src_base;
4162 consumed_chars = consumed_chars_base;
4163 ONE_MORE_BYTE (c);
065e3595 4164 *charbuf++ = c < 0 ? -c : BYTE8_TO_CHAR (c);
ff0dacd7 4165 char_offset++;
df7492f9
KH
4166 coding->errors++;
4167 }
fa42c37f 4168
df7492f9 4169 no_more_source:
ff0dacd7 4170 if (last_id != charset_ascii)
69a80ea3 4171 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
df7492f9
KH
4172 coding->consumed_char += consumed_chars_base;
4173 coding->consumed = src_base - coding->source;
4174 coding->charbuf_used = charbuf - coding->charbuf;
fa42c37f
KH
4175}
4176
b73bfc1c 4177static void
df7492f9 4178decode_coding_big5 (coding)
4ed46869 4179 struct coding_system *coding;
4ed46869 4180{
8f924df7
KH
4181 const unsigned char *src = coding->source + coding->consumed;
4182 const unsigned char *src_end = coding->source + coding->src_bytes;
4183 const unsigned char *src_base;
69a80ea3
KH
4184 int *charbuf = coding->charbuf + coding->charbuf_used;
4185 int *charbuf_end
4186 = coding->charbuf + coding->charbuf_size - MAX_ANNOTATION_LENGTH;
df7492f9
KH
4187 int consumed_chars = 0, consumed_chars_base;
4188 int multibytep = coding->src_multibyte;
4189 struct charset *charset_roman, *charset_big5;
24a73b0a 4190 Lisp_Object attrs, charset_list, val;
ff0dacd7
KH
4191 int char_offset = coding->produced_char;
4192 int last_offset = char_offset;
4193 int last_id = charset_ascii;
df7492f9 4194
24a73b0a 4195 CODING_GET_INFO (coding, attrs, charset_list);
df7492f9
KH
4196 val = charset_list;
4197 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4198 charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val)));
4ed46869 4199
b73bfc1c 4200 while (1)
4ed46869 4201 {
df7492f9 4202 int c, c1;
24a73b0a 4203 struct charset *charset;
b73bfc1c
KH
4204
4205 src_base = src;
df7492f9
KH
4206 consumed_chars_base = consumed_chars;
4207
4208 if (charbuf >= charbuf_end)
4209 break;
4210
4211 ONE_MORE_BYTE (c);
b73bfc1c 4212
065e3595
KH
4213 if (c < 0)
4214 goto invalid_code;
24a73b0a
KH
4215 if (c < 0x80)
4216 charset = charset_roman;
4217 else
4ed46869 4218 {
24a73b0a
KH
4219 /* BIG5 -> Big5 */
4220 if (c < 0xA1 || c > 0xFE)
4221 goto invalid_code;
4222 ONE_MORE_BYTE (c1);
4223 if (c1 < 0x40 || (c1 > 0x7E && c1 < 0xA1) || c1 > 0xFE)
4224 goto invalid_code;
4225 c = c << 8 | c1;
4226 charset = charset_big5;
4ed46869 4227 }
24a73b0a
KH
4228 if (charset->id != charset_ascii
4229 && last_id != charset->id)
df7492f9 4230 {
24a73b0a 4231 if (last_id != charset_ascii)
69a80ea3 4232 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
24a73b0a
KH
4233 last_id = charset->id;
4234 last_offset = char_offset;
4ed46869 4235 }
24a73b0a 4236 CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c);
df7492f9 4237 *charbuf++ = c;
ff0dacd7 4238 char_offset++;
fb88bf2d
KH
4239 continue;
4240
df7492f9 4241 invalid_code:
4ed46869 4242 src = src_base;
df7492f9
KH
4243 consumed_chars = consumed_chars_base;
4244 ONE_MORE_BYTE (c);
065e3595 4245 *charbuf++ = c < 0 ? -c : BYTE8_TO_CHAR (c);
ff0dacd7 4246 char_offset++;
df7492f9 4247 coding->errors++;
fb88bf2d 4248 }
d46c5b12 4249
df7492f9 4250 no_more_source:
ff0dacd7 4251 if (last_id != charset_ascii)
69a80ea3 4252 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
df7492f9
KH
4253 coding->consumed_char += consumed_chars_base;
4254 coding->consumed = src_base - coding->source;
4255 coding->charbuf_used = charbuf - coding->charbuf;
4ed46869
KH
4256}
4257
4258/* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
b73bfc1c
KH
4259 This function can encode charsets `ascii', `katakana-jisx0201',
4260 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
4261 are sure that all these charsets are registered as official charset
4ed46869
KH
4262 (i.e. do not have extended leading-codes). Characters of other
4263 charsets are produced without any encoding. If SJIS_P is 1, encode
4264 SJIS text, else encode BIG5 text. */
4265
df7492f9
KH
4266static int
4267encode_coding_sjis (coding)
4ed46869 4268 struct coding_system *coding;
4ed46869 4269{
df7492f9
KH
4270 int multibytep = coding->dst_multibyte;
4271 int *charbuf = coding->charbuf;
4272 int *charbuf_end = charbuf + coding->charbuf_used;
4273 unsigned char *dst = coding->destination + coding->produced;
4274 unsigned char *dst_end = coding->destination + coding->dst_bytes;
4275 int safe_room = 4;
4276 int produced_chars = 0;
24a73b0a 4277 Lisp_Object attrs, charset_list, val;
df7492f9
KH
4278 int ascii_compatible;
4279 struct charset *charset_roman, *charset_kanji, *charset_kana;
57a47f8a 4280 struct charset *charset_kanji2;
df7492f9 4281 int c;
a5d301df 4282
24a73b0a 4283 CODING_GET_INFO (coding, attrs, charset_list);
df7492f9
KH
4284 val = charset_list;
4285 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4286 charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
57a47f8a
KH
4287 charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4288 charset_kanji2 = NILP (val) ? NULL : CHARSET_FROM_ID (XINT (XCAR (val)));
4ed46869 4289
df7492f9 4290 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
93dec019 4291
df7492f9
KH
4292 while (charbuf < charbuf_end)
4293 {
4294 ASSURE_DESTINATION (safe_room);
4295 c = *charbuf++;
b73bfc1c 4296 /* Now encode the character C. */
df7492f9
KH
4297 if (ASCII_CHAR_P (c) && ascii_compatible)
4298 EMIT_ONE_ASCII_BYTE (c);
16eafb5d
KH
4299 else if (CHAR_BYTE8_P (c))
4300 {
4301 c = CHAR_TO_BYTE8 (c);
4302 EMIT_ONE_BYTE (c);
4303 }
df7492f9 4304 else
b73bfc1c 4305 {
df7492f9
KH
4306 unsigned code;
4307 struct charset *charset = char_charset (c, charset_list, &code);
4308
4309 if (!charset)
4ed46869 4310 {
41cbe562 4311 if (coding->mode & CODING_MODE_SAFE_ENCODING)
b73bfc1c 4312 {
41cbe562
KH
4313 code = CODING_INHIBIT_CHARACTER_SUBSTITUTION;
4314 charset = CHARSET_FROM_ID (charset_ascii);
b73bfc1c 4315 }
41cbe562 4316 else
b73bfc1c 4317 {
41cbe562
KH
4318 c = coding->default_char;
4319 charset = char_charset (c, charset_list, &code);
b73bfc1c 4320 }
b73bfc1c 4321 }
df7492f9
KH
4322 if (code == CHARSET_INVALID_CODE (charset))
4323 abort ();
4324 if (charset == charset_kanji)
4325 {
4326 int c1, c2;
4327 JIS_TO_SJIS (code);
4328 c1 = code >> 8, c2 = code & 0xFF;
4329 EMIT_TWO_BYTES (c1, c2);
4330 }
4331 else if (charset == charset_kana)
4332 EMIT_ONE_BYTE (code | 0x80);
57a47f8a
KH
4333 else if (charset_kanji2 && charset == charset_kanji2)
4334 {
4335 int c1, c2;
4336
4337 c1 = code >> 8;
4338 if (c1 == 0x21 || (c1 >= 0x23 && c1 < 0x25)
4339 || (c1 >= 0x2C && c1 <= 0x2F) || c1 >= 0x6E)
4340 {
4341 JIS_TO_SJIS2 (code);
4342 c1 = code >> 8, c2 = code & 0xFF;
4343 EMIT_TWO_BYTES (c1, c2);
4344 }
4345 else
4346 EMIT_ONE_ASCII_BYTE (code & 0x7F);
4347 }
df7492f9
KH
4348 else
4349 EMIT_ONE_ASCII_BYTE (code & 0x7F);
4350 }
4351 }
065e3595 4352 record_conversion_result (coding, CODING_RESULT_SUCCESS);
df7492f9
KH
4353 coding->produced_char += produced_chars;
4354 coding->produced = dst - coding->destination;
4355 return 0;
4356}
4357
4358static int
4359encode_coding_big5 (coding)
4360 struct coding_system *coding;
4361{
4362 int multibytep = coding->dst_multibyte;
4363 int *charbuf = coding->charbuf;
4364 int *charbuf_end = charbuf + coding->charbuf_used;
4365 unsigned char *dst = coding->destination + coding->produced;
4366 unsigned char *dst_end = coding->destination + coding->dst_bytes;
4367 int safe_room = 4;
4368 int produced_chars = 0;
24a73b0a 4369 Lisp_Object attrs, charset_list, val;
df7492f9
KH
4370 int ascii_compatible;
4371 struct charset *charset_roman, *charset_big5;
4372 int c;
4373
24a73b0a 4374 CODING_GET_INFO (coding, attrs, charset_list);
df7492f9
KH
4375 val = charset_list;
4376 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4377 charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val)));
4378 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
4379
4380 while (charbuf < charbuf_end)
4381 {
4382 ASSURE_DESTINATION (safe_room);
4383 c = *charbuf++;
4384 /* Now encode the character C. */
4385 if (ASCII_CHAR_P (c) && ascii_compatible)
4386 EMIT_ONE_ASCII_BYTE (c);
16eafb5d
KH
4387 else if (CHAR_BYTE8_P (c))
4388 {
4389 c = CHAR_TO_BYTE8 (c);
4390 EMIT_ONE_BYTE (c);
b73bfc1c
KH
4391 }
4392 else
4393 {
df7492f9
KH
4394 unsigned code;
4395 struct charset *charset = char_charset (c, charset_list, &code);
4396
4397 if (! charset)
b73bfc1c 4398 {
41cbe562 4399 if (coding->mode & CODING_MODE_SAFE_ENCODING)
b73bfc1c 4400 {
41cbe562
KH
4401 code = CODING_INHIBIT_CHARACTER_SUBSTITUTION;
4402 charset = CHARSET_FROM_ID (charset_ascii);
b73bfc1c 4403 }
41cbe562 4404 else
0eecad43 4405 {
41cbe562
KH
4406 c = coding->default_char;
4407 charset = char_charset (c, charset_list, &code);
0eecad43 4408 }
4ed46869 4409 }
df7492f9
KH
4410 if (code == CHARSET_INVALID_CODE (charset))
4411 abort ();
4412 if (charset == charset_big5)
b73bfc1c 4413 {
df7492f9
KH
4414 int c1, c2;
4415
4416 c1 = code >> 8, c2 = code & 0xFF;
4417 EMIT_TWO_BYTES (c1, c2);
b73bfc1c 4418 }
df7492f9
KH
4419 else
4420 EMIT_ONE_ASCII_BYTE (code & 0x7F);
4ed46869 4421 }
4ed46869 4422 }
065e3595 4423 record_conversion_result (coding, CODING_RESULT_SUCCESS);
df7492f9
KH
4424 coding->produced_char += produced_chars;
4425 coding->produced = dst - coding->destination;
4426 return 0;
4ed46869
KH
4427}
4428
4429\f
df7492f9 4430/*** 10. CCL handlers ***/
1397dc18
KH
4431
4432/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4433 Check if a text is encoded in a coding system of which
4434 encoder/decoder are written in CCL program. If it is, return
df7492f9 4435 CATEGORY_MASK_CCL, else return 0. */
1397dc18 4436
0a28aafb 4437static int
ff0dacd7 4438detect_coding_ccl (coding, detect_info)
df7492f9 4439 struct coding_system *coding;
ff0dacd7 4440 struct coding_detection_info *detect_info;
1397dc18 4441{
065e3595 4442 const unsigned char *src = coding->source, *src_base;
8f924df7 4443 const unsigned char *src_end = coding->source + coding->src_bytes;
df7492f9
KH
4444 int multibytep = coding->src_multibyte;
4445 int consumed_chars = 0;
4446 int found = 0;
0e219d54 4447 unsigned char *valids;
df7492f9
KH
4448 int head_ascii = coding->head_ascii;
4449 Lisp_Object attrs;
4450
ff0dacd7
KH
4451 detect_info->checked |= CATEGORY_MASK_CCL;
4452
df7492f9 4453 coding = &coding_categories[coding_category_ccl];
0e219d54 4454 valids = CODING_CCL_VALIDS (coding);
df7492f9
KH
4455 attrs = CODING_ID_ATTRS (coding->id);
4456 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
4457 src += head_ascii;
1397dc18 4458
b73bfc1c 4459 while (1)
1397dc18 4460 {
df7492f9 4461 int c;
065e3595
KH
4462
4463 src_base = src;
df7492f9 4464 ONE_MORE_BYTE (c);
065e3595 4465 if (c < 0 || ! valids[c])
df7492f9 4466 break;
ff0dacd7
KH
4467 if ((valids[c] > 1))
4468 found = CATEGORY_MASK_CCL;
df7492f9 4469 }
ff0dacd7 4470 detect_info->rejected |= CATEGORY_MASK_CCL;
df7492f9
KH
4471 return 0;
4472
4473 no_more_source:
ff0dacd7
KH
4474 detect_info->found |= found;
4475 return 1;
df7492f9
KH
4476}
4477
4478static void
4479decode_coding_ccl (coding)
4480 struct coding_system *coding;
4481{
7c78e542 4482 const unsigned char *src = coding->source + coding->consumed;
8f924df7 4483 const unsigned char *src_end = coding->source + coding->src_bytes;
69a80ea3
KH
4484 int *charbuf = coding->charbuf + coding->charbuf_used;
4485 int *charbuf_end = coding->charbuf + coding->charbuf_size;
df7492f9
KH
4486 int consumed_chars = 0;
4487 int multibytep = coding->src_multibyte;
4488 struct ccl_program ccl;
4489 int source_charbuf[1024];
4490 int source_byteidx[1024];
24a73b0a 4491 Lisp_Object attrs, charset_list;
df7492f9 4492
24a73b0a 4493 CODING_GET_INFO (coding, attrs, charset_list);
df7492f9
KH
4494 setup_ccl_program (&ccl, CODING_CCL_DECODER (coding));
4495
4496 while (src < src_end)
4497 {
7c78e542 4498 const unsigned char *p = src;
df7492f9
KH
4499 int *source, *source_end;
4500 int i = 0;
4501
4502 if (multibytep)
4503 while (i < 1024 && p < src_end)
4504 {
4505 source_byteidx[i] = p - src;
4506 source_charbuf[i++] = STRING_CHAR_ADVANCE (p);
4507 }
4508 else
4509 while (i < 1024 && p < src_end)
4510 source_charbuf[i++] = *p++;
8f924df7 4511
df7492f9
KH
4512 if (p == src_end && coding->mode & CODING_MODE_LAST_BLOCK)
4513 ccl.last_block = 1;
4514
4515 source = source_charbuf;
4516 source_end = source + i;
4517 while (source < source_end)
4518 {
4519 ccl_driver (&ccl, source, charbuf,
8dcbea82
KH
4520 source_end - source, charbuf_end - charbuf,
4521 charset_list);
df7492f9
KH
4522 source += ccl.consumed;
4523 charbuf += ccl.produced;
4524 if (ccl.status != CCL_STAT_SUSPEND_BY_DST)
4525 break;
4526 }
4527 if (source < source_end)
4528 src += source_byteidx[source - source_charbuf];
4529 else
4530 src = p;
4531 consumed_chars += source - source_charbuf;
4532
4533 if (ccl.status != CCL_STAT_SUSPEND_BY_SRC
4534 && ccl.status != CODING_RESULT_INSUFFICIENT_SRC)
4535 break;
4536 }
4537
4538 switch (ccl.status)
4539 {
4540 case CCL_STAT_SUSPEND_BY_SRC:
065e3595 4541 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC);
df7492f9
KH
4542 break;
4543 case CCL_STAT_SUSPEND_BY_DST:
4544 break;
4545 case CCL_STAT_QUIT:
4546 case CCL_STAT_INVALID_CMD:
065e3595 4547 record_conversion_result (coding, CODING_RESULT_INTERRUPT);
df7492f9
KH
4548 break;
4549 default:
065e3595 4550 record_conversion_result (coding, CODING_RESULT_SUCCESS);
df7492f9
KH
4551 break;
4552 }
4553 coding->consumed_char += consumed_chars;
4554 coding->consumed = src - coding->source;
4555 coding->charbuf_used = charbuf - coding->charbuf;
4556}
4557
4558static int
4559encode_coding_ccl (coding)
4560 struct coding_system *coding;
4561{
4562 struct ccl_program ccl;
4563 int multibytep = coding->dst_multibyte;
4564 int *charbuf = coding->charbuf;
4565 int *charbuf_end = charbuf + coding->charbuf_used;
4566 unsigned char *dst = coding->destination + coding->produced;
4567 unsigned char *dst_end = coding->destination + coding->dst_bytes;
df7492f9
KH
4568 int destination_charbuf[1024];
4569 int i, produced_chars = 0;
24a73b0a 4570 Lisp_Object attrs, charset_list;
df7492f9 4571
24a73b0a 4572 CODING_GET_INFO (coding, attrs, charset_list);
df7492f9
KH
4573 setup_ccl_program (&ccl, CODING_CCL_ENCODER (coding));
4574
4575 ccl.last_block = coding->mode & CODING_MODE_LAST_BLOCK;
4576 ccl.dst_multibyte = coding->dst_multibyte;
4577
8cffd3e7 4578 while (charbuf < charbuf_end)
df7492f9 4579 {
df7492f9 4580 ccl_driver (&ccl, charbuf, destination_charbuf,
8cffd3e7 4581 charbuf_end - charbuf, 1024, charset_list);
df7492f9 4582 if (multibytep)
8cffd3e7
KH
4583 {
4584 ASSURE_DESTINATION (ccl.produced * 2);
4585 for (i = 0; i < ccl.produced; i++)
4586 EMIT_ONE_BYTE (destination_charbuf[i] & 0xFF);
4587 }
df7492f9
KH
4588 else
4589 {
8cffd3e7 4590 ASSURE_DESTINATION (ccl.produced);
df7492f9
KH
4591 for (i = 0; i < ccl.produced; i++)
4592 *dst++ = destination_charbuf[i] & 0xFF;
4593 produced_chars += ccl.produced;
4594 }
8cffd3e7
KH
4595 charbuf += ccl.consumed;
4596 if (ccl.status == CCL_STAT_QUIT
4597 || ccl.status == CCL_STAT_INVALID_CMD)
4598 break;
df7492f9
KH
4599 }
4600
4601 switch (ccl.status)
4602 {
4603 case CCL_STAT_SUSPEND_BY_SRC:
065e3595 4604 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC);
df7492f9
KH
4605 break;
4606 case CCL_STAT_SUSPEND_BY_DST:
065e3595 4607 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_DST);
df7492f9
KH
4608 break;
4609 case CCL_STAT_QUIT:
4610 case CCL_STAT_INVALID_CMD:
065e3595 4611 record_conversion_result (coding, CODING_RESULT_INTERRUPT);
df7492f9
KH
4612 break;
4613 default:
065e3595 4614 record_conversion_result (coding, CODING_RESULT_SUCCESS);
df7492f9 4615 break;
1397dc18 4616 }
df7492f9
KH
4617
4618 coding->produced_char += produced_chars;
4619 coding->produced = dst - coding->destination;
4620 return 0;
1397dc18
KH
4621}
4622
df7492f9 4623
1397dc18 4624\f
df7492f9 4625/*** 10, 11. no-conversion handlers ***/
4ed46869 4626
b73bfc1c 4627/* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
4ed46869 4628
b73bfc1c 4629static void
df7492f9 4630decode_coding_raw_text (coding)
4ed46869 4631 struct coding_system *coding;
4ed46869 4632{
df7492f9 4633 coding->chars_at_source = 1;
2c78b7e1
KH
4634 coding->consumed_char = 0;
4635 coding->consumed = 0;
065e3595 4636 record_conversion_result (coding, CODING_RESULT_SUCCESS);
df7492f9 4637}
4ed46869 4638
df7492f9
KH
4639static int
4640encode_coding_raw_text (coding)
4641 struct coding_system *coding;
4642{
4643 int multibytep = coding->dst_multibyte;
4644 int *charbuf = coding->charbuf;
4645 int *charbuf_end = coding->charbuf + coding->charbuf_used;
4646 unsigned char *dst = coding->destination + coding->produced;
4647 unsigned char *dst_end = coding->destination + coding->dst_bytes;
4648 int produced_chars = 0;
b73bfc1c
KH
4649 int c;
4650
df7492f9 4651 if (multibytep)
b73bfc1c 4652 {
df7492f9 4653 int safe_room = MAX_MULTIBYTE_LENGTH * 2;
4ed46869 4654
df7492f9
KH
4655 if (coding->src_multibyte)
4656 while (charbuf < charbuf_end)
4657 {
4658 ASSURE_DESTINATION (safe_room);
4659 c = *charbuf++;
4660 if (ASCII_CHAR_P (c))
4661 EMIT_ONE_ASCII_BYTE (c);
4662 else if (CHAR_BYTE8_P (c))
4663 {
4664 c = CHAR_TO_BYTE8 (c);
4665 EMIT_ONE_BYTE (c);
4666 }
4667 else
4668 {
4669 unsigned char str[MAX_MULTIBYTE_LENGTH], *p0 = str, *p1 = str;
93dec019 4670
df7492f9
KH
4671 CHAR_STRING_ADVANCE (c, p1);
4672 while (p0 < p1)
9d123124
KH
4673 {
4674 EMIT_ONE_BYTE (*p0);
4675 p0++;
4676 }
df7492f9
KH
4677 }
4678 }
b73bfc1c 4679 else
df7492f9
KH
4680 while (charbuf < charbuf_end)
4681 {
4682 ASSURE_DESTINATION (safe_room);
4683 c = *charbuf++;
4684 EMIT_ONE_BYTE (c);
4685 }
4686 }
4687 else
4ed46869 4688 {
df7492f9 4689 if (coding->src_multibyte)
d46c5b12 4690 {
df7492f9
KH
4691 int safe_room = MAX_MULTIBYTE_LENGTH;
4692
4693 while (charbuf < charbuf_end)
d46c5b12 4694 {
df7492f9
KH
4695 ASSURE_DESTINATION (safe_room);
4696 c = *charbuf++;
4697 if (ASCII_CHAR_P (c))
4698 *dst++ = c;
4699 else if (CHAR_BYTE8_P (c))
4700 *dst++ = CHAR_TO_BYTE8 (c);
b73bfc1c 4701 else
df7492f9
KH
4702 CHAR_STRING_ADVANCE (c, dst);
4703 produced_chars++;
d46c5b12
KH
4704 }
4705 }
df7492f9
KH
4706 else
4707 {
4708 ASSURE_DESTINATION (charbuf_end - charbuf);
4709 while (charbuf < charbuf_end && dst < dst_end)
4710 *dst++ = *charbuf++;
4711 produced_chars = dst - (coding->destination + coding->dst_bytes);
8f924df7 4712 }
4ed46869 4713 }
065e3595 4714 record_conversion_result (coding, CODING_RESULT_SUCCESS);
df7492f9
KH
4715 coding->produced_char += produced_chars;
4716 coding->produced = dst - coding->destination;
4717 return 0;
4ed46869
KH
4718}
4719
ff0dacd7
KH
4720/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4721 Check if a text is encoded in a charset-based coding system. If it
4722 is, return 1, else return 0. */
4723
0a28aafb 4724static int
ff0dacd7 4725detect_coding_charset (coding, detect_info)
df7492f9 4726 struct coding_system *coding;
ff0dacd7 4727 struct coding_detection_info *detect_info;
1397dc18 4728{
065e3595 4729 const unsigned char *src = coding->source, *src_base;
8f924df7 4730 const unsigned char *src_end = coding->source + coding->src_bytes;
df7492f9
KH
4731 int multibytep = coding->src_multibyte;
4732 int consumed_chars = 0;
4733 Lisp_Object attrs, valids;
584948ac 4734 int found = 0;
716b3fa0 4735 int head_ascii = coding->head_ascii;
1397dc18 4736
ff0dacd7
KH
4737 detect_info->checked |= CATEGORY_MASK_CHARSET;
4738
df7492f9
KH
4739 coding = &coding_categories[coding_category_charset];
4740 attrs = CODING_ID_ATTRS (coding->id);
4741 valids = AREF (attrs, coding_attr_charset_valids);
4742
4743 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
716b3fa0 4744 src += head_ascii;
1397dc18 4745
b73bfc1c 4746 while (1)
1397dc18 4747 {
df7492f9 4748 int c;
716b3fa0
KH
4749 Lisp_Object val;
4750 struct charset *charset;
4751 int dim, idx;
1397dc18 4752
065e3595 4753 src_base = src;
df7492f9 4754 ONE_MORE_BYTE (c);
065e3595
KH
4755 if (c < 0)
4756 continue;
716b3fa0
KH
4757 val = AREF (valids, c);
4758 if (NILP (val))
df7492f9 4759 break;
584948ac 4760 if (c >= 0x80)
ff0dacd7 4761 found = CATEGORY_MASK_CHARSET;
716b3fa0
KH
4762 if (INTEGERP (val))
4763 {
4764 charset = CHARSET_FROM_ID (XFASTINT (val));
4765 dim = CHARSET_DIMENSION (charset);
4766 for (idx = 1; idx < dim; idx++)
4767 {
4768 if (src == src_end)
4769 goto too_short;
4770 ONE_MORE_BYTE (c);
4771 if (c < charset->code_space[(dim - 1 - idx) * 2]
4772 || c > charset->code_space[(dim - 1 - idx) * 2 + 1])
4773 break;
4774 }
4775 if (idx < dim)
4776 break;
4777 }
4778 else
4779 {
4780 idx = 1;
4781 for (; CONSP (val); val = XCDR (val))
4782 {
4783 charset = CHARSET_FROM_ID (XFASTINT (XCAR (val)));
4784 dim = CHARSET_DIMENSION (charset);
4785 while (idx < dim)
4786 {
4787 if (src == src_end)
4788 goto too_short;
4789 ONE_MORE_BYTE (c);
4790 if (c < charset->code_space[(dim - 1 - idx) * 4]
4791 || c > charset->code_space[(dim - 1 - idx) * 4 + 1])
4792 break;
4793 idx++;
4794 }
4795 if (idx == dim)
4796 {
4797 val = Qnil;
4798 break;
4799 }
4800 }
4801 if (CONSP (val))
4802 break;
4803 }
df7492f9 4804 }
716b3fa0 4805 too_short:
ff0dacd7 4806 detect_info->rejected |= CATEGORY_MASK_CHARSET;
df7492f9 4807 return 0;
4ed46869 4808
df7492f9 4809 no_more_source:
ff0dacd7
KH
4810 detect_info->found |= found;
4811 return 1;
df7492f9 4812}
b73bfc1c 4813
b73bfc1c 4814static void
df7492f9 4815decode_coding_charset (coding)
4ed46869 4816 struct coding_system *coding;
4ed46869 4817{
8f924df7
KH
4818 const unsigned char *src = coding->source + coding->consumed;
4819 const unsigned char *src_end = coding->source + coding->src_bytes;
4820 const unsigned char *src_base;
69a80ea3
KH
4821 int *charbuf = coding->charbuf + coding->charbuf_used;
4822 int *charbuf_end
4823 = coding->charbuf + coding->charbuf_size - MAX_ANNOTATION_LENGTH;
df7492f9
KH
4824 int consumed_chars = 0, consumed_chars_base;
4825 int multibytep = coding->src_multibyte;
24a73b0a 4826 Lisp_Object attrs, charset_list, valids;
ff0dacd7
KH
4827 int char_offset = coding->produced_char;
4828 int last_offset = char_offset;
4829 int last_id = charset_ascii;
df7492f9 4830
24a73b0a 4831 CODING_GET_INFO (coding, attrs, charset_list);
4eb6d3f1 4832 valids = AREF (attrs, coding_attr_charset_valids);
b73bfc1c 4833
df7492f9 4834 while (1)
4ed46869 4835 {
4eb6d3f1 4836 int c;
24a73b0a
KH
4837 Lisp_Object val;
4838 struct charset *charset;
4839 int dim;
4840 int len = 1;
4841 unsigned code;
df7492f9
KH
4842
4843 src_base = src;
4844 consumed_chars_base = consumed_chars;
b73bfc1c 4845
df7492f9
KH
4846 if (charbuf >= charbuf_end)
4847 break;
4848
4eb6d3f1 4849 ONE_MORE_BYTE (c);
065e3595
KH
4850 if (c < 0)
4851 goto invalid_code;
24a73b0a
KH
4852 code = c;
4853
4854 val = AREF (valids, c);
4855 if (NILP (val))
4856 goto invalid_code;
4857 if (INTEGERP (val))
d46c5b12 4858 {
24a73b0a
KH
4859 charset = CHARSET_FROM_ID (XFASTINT (val));
4860 dim = CHARSET_DIMENSION (charset);
4861 while (len < dim)
b73bfc1c 4862 {
24a73b0a
KH
4863 ONE_MORE_BYTE (c);
4864 code = (code << 8) | c;
4865 len++;
b73bfc1c 4866 }
24a73b0a
KH
4867 CODING_DECODE_CHAR (coding, src, src_base, src_end,
4868 charset, code, c);
d46c5b12 4869 }
df7492f9 4870 else
d46c5b12 4871 {
24a73b0a
KH
4872 /* VAL is a list of charset IDs. It is assured that the
4873 list is sorted by charset dimensions (smaller one
4874 comes first). */
4875 while (CONSP (val))
4eb6d3f1 4876 {
24a73b0a 4877 charset = CHARSET_FROM_ID (XFASTINT (XCAR (val)));
c7c66a95 4878 dim = CHARSET_DIMENSION (charset);
f9d71dcd 4879 while (len < dim)
4eb6d3f1 4880 {
acb2a965
KH
4881 ONE_MORE_BYTE (c);
4882 code = (code << 8) | c;
f9d71dcd 4883 len++;
4eb6d3f1 4884 }
24a73b0a
KH
4885 CODING_DECODE_CHAR (coding, src, src_base,
4886 src_end, charset, code, c);
4887 if (c >= 0)
4888 break;
4889 val = XCDR (val);
ff0dacd7 4890 }
d46c5b12 4891 }
24a73b0a
KH
4892 if (c < 0)
4893 goto invalid_code;
4894 if (charset->id != charset_ascii
4895 && last_id != charset->id)
4896 {
4897 if (last_id != charset_ascii)
69a80ea3 4898 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
24a73b0a
KH
4899 last_id = charset->id;
4900 last_offset = char_offset;
4901 }
4902
df7492f9 4903 *charbuf++ = c;
ff0dacd7 4904 char_offset++;
df7492f9
KH
4905 continue;
4906
4907 invalid_code:
4908 src = src_base;
4909 consumed_chars = consumed_chars_base;
4910 ONE_MORE_BYTE (c);
065e3595 4911 *charbuf++ = c < 0 ? -c : ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
ff0dacd7 4912 char_offset++;
df7492f9 4913 coding->errors++;
4ed46869
KH
4914 }
4915
df7492f9 4916 no_more_source:
ff0dacd7 4917 if (last_id != charset_ascii)
69a80ea3 4918 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
df7492f9
KH
4919 coding->consumed_char += consumed_chars_base;
4920 coding->consumed = src_base - coding->source;
4921 coding->charbuf_used = charbuf - coding->charbuf;
4ed46869
KH
4922}
4923
df7492f9
KH
4924static int
4925encode_coding_charset (coding)
4ed46869 4926 struct coding_system *coding;
4ed46869 4927{
df7492f9
KH
4928 int multibytep = coding->dst_multibyte;
4929 int *charbuf = coding->charbuf;
4930 int *charbuf_end = charbuf + coding->charbuf_used;
4931 unsigned char *dst = coding->destination + coding->produced;
4932 unsigned char *dst_end = coding->destination + coding->dst_bytes;
4933 int safe_room = MAX_MULTIBYTE_LENGTH;
4934 int produced_chars = 0;
24a73b0a 4935 Lisp_Object attrs, charset_list;
df7492f9 4936 int ascii_compatible;
b73bfc1c 4937 int c;
b73bfc1c 4938
24a73b0a 4939 CODING_GET_INFO (coding, attrs, charset_list);
df7492f9 4940 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
fb88bf2d 4941
df7492f9 4942 while (charbuf < charbuf_end)
4ed46869 4943 {
4eb6d3f1 4944 struct charset *charset;
df7492f9 4945 unsigned code;
8f924df7 4946
df7492f9
KH
4947 ASSURE_DESTINATION (safe_room);
4948 c = *charbuf++;
4949 if (ascii_compatible && ASCII_CHAR_P (c))
4950 EMIT_ONE_ASCII_BYTE (c);
16eafb5d 4951 else if (CHAR_BYTE8_P (c))
4ed46869 4952 {
16eafb5d
KH
4953 c = CHAR_TO_BYTE8 (c);
4954 EMIT_ONE_BYTE (c);
d46c5b12 4955 }
d46c5b12 4956 else
b73bfc1c 4957 {
4eb6d3f1
KH
4958 charset = char_charset (c, charset_list, &code);
4959 if (charset)
4960 {
4961 if (CHARSET_DIMENSION (charset) == 1)
4962 EMIT_ONE_BYTE (code);
4963 else if (CHARSET_DIMENSION (charset) == 2)
4964 EMIT_TWO_BYTES (code >> 8, code & 0xFF);
4965 else if (CHARSET_DIMENSION (charset) == 3)
4966 EMIT_THREE_BYTES (code >> 16, (code >> 8) & 0xFF, code & 0xFF);
4967 else
4968 EMIT_FOUR_BYTES (code >> 24, (code >> 16) & 0xFF,
4969 (code >> 8) & 0xFF, code & 0xFF);
4970 }
4971 else
41cbe562
KH
4972 {
4973 if (coding->mode & CODING_MODE_SAFE_ENCODING)
4974 c = CODING_INHIBIT_CHARACTER_SUBSTITUTION;
4975 else
4976 c = coding->default_char;
4977 EMIT_ONE_BYTE (c);
4978 }
4ed46869 4979 }
4ed46869
KH
4980 }
4981
065e3595 4982 record_conversion_result (coding, CODING_RESULT_SUCCESS);
df7492f9
KH
4983 coding->produced_char += produced_chars;
4984 coding->produced = dst - coding->destination;
4985 return 0;
4ed46869
KH
4986}
4987
4988\f
1397dc18 4989/*** 7. C library functions ***/
4ed46869 4990
df7492f9
KH
4991/* Setup coding context CODING from information about CODING_SYSTEM.
4992 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
4993 CODING_SYSTEM is invalid, signal an error. */
4ed46869 4994
ec6d2bb8 4995void
e0e989f6
KH
4996setup_coding_system (coding_system, coding)
4997 Lisp_Object coding_system;
4ed46869
KH
4998 struct coding_system *coding;
4999{
df7492f9
KH
5000 Lisp_Object attrs;
5001 Lisp_Object eol_type;
5002 Lisp_Object coding_type;
4608c386 5003 Lisp_Object val;
4ed46869 5004
df7492f9 5005 if (NILP (coding_system))
ae6f73fa 5006 coding_system = Qundecided;
c07c8e12 5007
df7492f9 5008 CHECK_CODING_SYSTEM_GET_ID (coding_system, coding->id);
1f5dbf34 5009
df7492f9
KH
5010 attrs = CODING_ID_ATTRS (coding->id);
5011 eol_type = CODING_ID_EOL_TYPE (coding->id);
1f5dbf34 5012
df7492f9
KH
5013 coding->mode = 0;
5014 coding->head_ascii = -1;
4a015c45
KH
5015 if (VECTORP (eol_type))
5016 coding->common_flags = (CODING_REQUIRE_DECODING_MASK
5017 | CODING_REQUIRE_DETECTION_MASK);
5018 else if (! EQ (eol_type, Qunix))
5019 coding->common_flags = (CODING_REQUIRE_DECODING_MASK
5020 | CODING_REQUIRE_ENCODING_MASK);
5021 else
5022 coding->common_flags = 0;
5e5c78be
KH
5023 if (! NILP (CODING_ATTR_POST_READ (attrs)))
5024 coding->common_flags |= CODING_REQUIRE_DECODING_MASK;
5025 if (! NILP (CODING_ATTR_PRE_WRITE (attrs)))
5026 coding->common_flags |= CODING_REQUIRE_ENCODING_MASK;
8f924df7
KH
5027 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs)))
5028 coding->common_flags |= CODING_FOR_UNIBYTE_MASK;
1f5dbf34 5029
df7492f9 5030 val = CODING_ATTR_SAFE_CHARSETS (attrs);
8f924df7
KH
5031 coding->max_charset_id = SCHARS (val) - 1;
5032 coding->safe_charsets = (char *) SDATA (val);
df7492f9 5033 coding->default_char = XINT (CODING_ATTR_DEFAULT_CHAR (attrs));
4608c386 5034
df7492f9
KH
5035 coding_type = CODING_ATTR_TYPE (attrs);
5036 if (EQ (coding_type, Qundecided))
d46c5b12 5037 {
df7492f9
KH
5038 coding->detector = NULL;
5039 coding->decoder = decode_coding_raw_text;
5040 coding->encoder = encode_coding_raw_text;
5041 coding->common_flags |= CODING_REQUIRE_DETECTION_MASK;
d46c5b12 5042 }
df7492f9 5043 else if (EQ (coding_type, Qiso_2022))
d46c5b12 5044 {
df7492f9
KH
5045 int i;
5046 int flags = XINT (AREF (attrs, coding_attr_iso_flags));
5047
5048 /* Invoke graphic register 0 to plane 0. */
5049 CODING_ISO_INVOCATION (coding, 0) = 0;
5050 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
5051 CODING_ISO_INVOCATION (coding, 1)
5052 = (flags & CODING_ISO_FLAG_SEVEN_BITS ? -1 : 1);
5053 /* Setup the initial status of designation. */
5054 for (i = 0; i < 4; i++)
5055 CODING_ISO_DESIGNATION (coding, i) = CODING_ISO_INITIAL (coding, i);
5056 /* Not single shifting initially. */
5057 CODING_ISO_SINGLE_SHIFTING (coding) = 0;
5058 /* Beginning of buffer should also be regarded as bol. */
5059 CODING_ISO_BOL (coding) = 1;
5060 coding->detector = detect_coding_iso_2022;
5061 coding->decoder = decode_coding_iso_2022;
5062 coding->encoder = encode_coding_iso_2022;
5063 if (flags & CODING_ISO_FLAG_SAFE)
5064 coding->mode |= CODING_MODE_SAFE_ENCODING;
d46c5b12 5065 coding->common_flags
df7492f9
KH
5066 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK
5067 | CODING_REQUIRE_FLUSHING_MASK);
5068 if (flags & CODING_ISO_FLAG_COMPOSITION)
5069 coding->common_flags |= CODING_ANNOTATE_COMPOSITION_MASK;
ff0dacd7
KH
5070 if (flags & CODING_ISO_FLAG_DESIGNATION)
5071 coding->common_flags |= CODING_ANNOTATE_CHARSET_MASK;
df7492f9
KH
5072 if (flags & CODING_ISO_FLAG_FULL_SUPPORT)
5073 {
5074 setup_iso_safe_charsets (attrs);
5075 val = CODING_ATTR_SAFE_CHARSETS (attrs);
8f924df7
KH
5076 coding->max_charset_id = SCHARS (val) - 1;
5077 coding->safe_charsets = (char *) SDATA (val);
df7492f9
KH
5078 }
5079 CODING_ISO_FLAGS (coding) = flags;
d46c5b12 5080 }
df7492f9 5081 else if (EQ (coding_type, Qcharset))
d46c5b12 5082 {
df7492f9
KH
5083 coding->detector = detect_coding_charset;
5084 coding->decoder = decode_coding_charset;
5085 coding->encoder = encode_coding_charset;
d46c5b12 5086 coding->common_flags
df7492f9 5087 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
d46c5b12 5088 }
df7492f9 5089 else if (EQ (coding_type, Qutf_8))
d46c5b12 5090 {
df7492f9
KH
5091 coding->detector = detect_coding_utf_8;
5092 coding->decoder = decode_coding_utf_8;
5093 coding->encoder = encode_coding_utf_8;
5094 coding->common_flags
5095 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5096 }
5097 else if (EQ (coding_type, Qutf_16))
5098 {
5099 val = AREF (attrs, coding_attr_utf_16_bom);
5100 CODING_UTF_16_BOM (coding) = (CONSP (val) ? utf_16_detect_bom
5101 : EQ (val, Qt) ? utf_16_with_bom
5102 : utf_16_without_bom);
5103 val = AREF (attrs, coding_attr_utf_16_endian);
b49a1807 5104 CODING_UTF_16_ENDIAN (coding) = (EQ (val, Qbig) ? utf_16_big_endian
df7492f9 5105 : utf_16_little_endian);
e19c3639 5106 CODING_UTF_16_SURROGATE (coding) = 0;
df7492f9
KH
5107 coding->detector = detect_coding_utf_16;
5108 coding->decoder = decode_coding_utf_16;
5109 coding->encoder = encode_coding_utf_16;
5110 coding->common_flags
5111 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
b49a1807
KH
5112 if (CODING_UTF_16_BOM (coding) == utf_16_detect_bom)
5113 coding->common_flags |= CODING_REQUIRE_DETECTION_MASK;
d46c5b12 5114 }
df7492f9 5115 else if (EQ (coding_type, Qccl))
4ed46869 5116 {
df7492f9
KH
5117 coding->detector = detect_coding_ccl;
5118 coding->decoder = decode_coding_ccl;
5119 coding->encoder = encode_coding_ccl;
c952af22 5120 coding->common_flags
df7492f9
KH
5121 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK
5122 | CODING_REQUIRE_FLUSHING_MASK);
5123 }
5124 else if (EQ (coding_type, Qemacs_mule))
5125 {
5126 coding->detector = detect_coding_emacs_mule;
5127 coding->decoder = decode_coding_emacs_mule;
5128 coding->encoder = encode_coding_emacs_mule;
c952af22 5129 coding->common_flags
df7492f9
KH
5130 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5131 if (! NILP (AREF (attrs, coding_attr_emacs_mule_full))
5132 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs), Vemacs_mule_charset_list))
5133 {
5134 Lisp_Object tail, safe_charsets;
5135 int max_charset_id = 0;
5136
5137 for (tail = Vemacs_mule_charset_list; CONSP (tail);
5138 tail = XCDR (tail))
5139 if (max_charset_id < XFASTINT (XCAR (tail)))
5140 max_charset_id = XFASTINT (XCAR (tail));
5141 safe_charsets = Fmake_string (make_number (max_charset_id + 1),
5142 make_number (255));
5143 for (tail = Vemacs_mule_charset_list; CONSP (tail);
5144 tail = XCDR (tail))
8f924df7 5145 SSET (safe_charsets, XFASTINT (XCAR (tail)), 0);
df7492f9 5146 coding->max_charset_id = max_charset_id;
8f924df7 5147 coding->safe_charsets = (char *) SDATA (safe_charsets);
df7492f9
KH
5148 }
5149 }
5150 else if (EQ (coding_type, Qshift_jis))
5151 {
5152 coding->detector = detect_coding_sjis;
5153 coding->decoder = decode_coding_sjis;
5154 coding->encoder = encode_coding_sjis;
c952af22 5155 coding->common_flags
df7492f9
KH
5156 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5157 }
5158 else if (EQ (coding_type, Qbig5))
5159 {
5160 coding->detector = detect_coding_big5;
5161 coding->decoder = decode_coding_big5;
5162 coding->encoder = encode_coding_big5;
c952af22 5163 coding->common_flags
df7492f9
KH
5164 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5165 }
5166 else /* EQ (coding_type, Qraw_text) */
ec6d2bb8 5167 {
df7492f9
KH
5168 coding->detector = NULL;
5169 coding->decoder = decode_coding_raw_text;
5170 coding->encoder = encode_coding_raw_text;
ea29edf2
KH
5171 if (! EQ (eol_type, Qunix))
5172 {
5173 coding->common_flags |= CODING_REQUIRE_DECODING_MASK;
5174 if (! VECTORP (eol_type))
5175 coding->common_flags |= CODING_REQUIRE_ENCODING_MASK;
5176 }
5177
4ed46869 5178 }
4ed46869 5179
df7492f9 5180 return;
4ed46869
KH
5181}
5182
0ff61e78
KH
5183/* Return a list of charsets supported by CODING. */
5184
5185Lisp_Object
5186coding_charset_list (coding)
5187 struct coding_system *coding;
5188{
35befdaa 5189 Lisp_Object attrs, charset_list;
0ff61e78
KH
5190
5191 CODING_GET_INFO (coding, attrs, charset_list);
5192 if (EQ (CODING_ATTR_TYPE (attrs), Qiso_2022))
5193 {
5194 int flags = XINT (AREF (attrs, coding_attr_iso_flags));
5195
5196 if (flags & CODING_ISO_FLAG_FULL_SUPPORT)
5197 charset_list = Viso_2022_charset_list;
5198 }
5199 else if (EQ (CODING_ATTR_TYPE (attrs), Qemacs_mule))
5200 {
5201 charset_list = Vemacs_mule_charset_list;
5202 }
5203 return charset_list;
5204}
5205
5206
df7492f9
KH
5207/* Return raw-text or one of its subsidiaries that has the same
5208 eol_type as CODING-SYSTEM. */
ec6d2bb8 5209
df7492f9
KH
5210Lisp_Object
5211raw_text_coding_system (coding_system)
5212 Lisp_Object coding_system;
ec6d2bb8 5213{
0be8721c 5214 Lisp_Object spec, attrs;
df7492f9 5215 Lisp_Object eol_type, raw_text_eol_type;
ec6d2bb8 5216
d3e4cb56
KH
5217 if (NILP (coding_system))
5218 return Qraw_text;
df7492f9
KH
5219 spec = CODING_SYSTEM_SPEC (coding_system);
5220 attrs = AREF (spec, 0);
ec6d2bb8 5221
df7492f9
KH
5222 if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text))
5223 return coding_system;
ec6d2bb8 5224
df7492f9
KH
5225 eol_type = AREF (spec, 2);
5226 if (VECTORP (eol_type))
5227 return Qraw_text;
5228 spec = CODING_SYSTEM_SPEC (Qraw_text);
5229 raw_text_eol_type = AREF (spec, 2);
5230 return (EQ (eol_type, Qunix) ? AREF (raw_text_eol_type, 0)
5231 : EQ (eol_type, Qdos) ? AREF (raw_text_eol_type, 1)
5232 : AREF (raw_text_eol_type, 2));
ec6d2bb8
KH
5233}
5234
54f78171 5235
df7492f9
KH
5236/* If CODING_SYSTEM doesn't specify end-of-line format but PARENT
5237 does, return one of the subsidiary that has the same eol-spec as
fcbcfb64
KH
5238 PARENT. Otherwise, return CODING_SYSTEM. If PARENT is nil,
5239 inherit end-of-line format from the system's setting
5240 (system_eol_type). */
df7492f9
KH
5241
5242Lisp_Object
5243coding_inherit_eol_type (coding_system, parent)
b74e4686 5244 Lisp_Object coding_system, parent;
54f78171 5245{
3e139625 5246 Lisp_Object spec, eol_type;
54f78171 5247
d3e4cb56
KH
5248 if (NILP (coding_system))
5249 coding_system = Qraw_text;
df7492f9 5250 spec = CODING_SYSTEM_SPEC (coding_system);
df7492f9 5251 eol_type = AREF (spec, 2);
fcbcfb64 5252 if (VECTORP (eol_type))
df7492f9 5253 {
df7492f9
KH
5254 Lisp_Object parent_eol_type;
5255
fcbcfb64
KH
5256 if (! NILP (parent))
5257 {
5258 Lisp_Object parent_spec;
5259
4a015c45 5260 parent_spec = CODING_SYSTEM_SPEC (parent);
fcbcfb64
KH
5261 parent_eol_type = AREF (parent_spec, 2);
5262 }
5263 else
5264 parent_eol_type = system_eol_type;
df7492f9
KH
5265 if (EQ (parent_eol_type, Qunix))
5266 coding_system = AREF (eol_type, 0);
5267 else if (EQ (parent_eol_type, Qdos))
5268 coding_system = AREF (eol_type, 1);
5269 else if (EQ (parent_eol_type, Qmac))
5270 coding_system = AREF (eol_type, 2);
54f78171 5271 }
df7492f9 5272 return coding_system;
54f78171
KH
5273}
5274
4ed46869
KH
5275/* Emacs has a mechanism to automatically detect a coding system if it
5276 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
5277 it's impossible to distinguish some coding systems accurately
5278 because they use the same range of codes. So, at first, coding
5279 systems are categorized into 7, those are:
5280
0ef69138 5281 o coding-category-emacs-mule
4ed46869
KH
5282
5283 The category for a coding system which has the same code range
5284 as Emacs' internal format. Assigned the coding-system (Lisp
0ef69138 5285 symbol) `emacs-mule' by default.
4ed46869
KH
5286
5287 o coding-category-sjis
5288
5289 The category for a coding system which has the same code range
5290 as SJIS. Assigned the coding-system (Lisp
7717c392 5291 symbol) `japanese-shift-jis' by default.
4ed46869
KH
5292
5293 o coding-category-iso-7
5294
5295 The category for a coding system which has the same code range
7717c392 5296 as ISO2022 of 7-bit environment. This doesn't use any locking
d46c5b12
KH
5297 shift and single shift functions. This can encode/decode all
5298 charsets. Assigned the coding-system (Lisp symbol)
5299 `iso-2022-7bit' by default.
5300
5301 o coding-category-iso-7-tight
5302
5303 Same as coding-category-iso-7 except that this can
5304 encode/decode only the specified charsets.
4ed46869
KH
5305
5306 o coding-category-iso-8-1
5307
5308 The category for a coding system which has the same code range
5309 as ISO2022 of 8-bit environment and graphic plane 1 used only
7717c392
KH
5310 for DIMENSION1 charset. This doesn't use any locking shift
5311 and single shift functions. Assigned the coding-system (Lisp
5312 symbol) `iso-latin-1' by default.
4ed46869
KH
5313
5314 o coding-category-iso-8-2
5315
5316 The category for a coding system which has the same code range
5317 as ISO2022 of 8-bit environment and graphic plane 1 used only
7717c392
KH
5318 for DIMENSION2 charset. This doesn't use any locking shift
5319 and single shift functions. Assigned the coding-system (Lisp
5320 symbol) `japanese-iso-8bit' by default.
4ed46869 5321
7717c392 5322 o coding-category-iso-7-else
4ed46869
KH
5323
5324 The category for a coding system which has the same code range
df7492f9 5325 as ISO2022 of 7-bit environemnt but uses locking shift or
7717c392
KH
5326 single shift functions. Assigned the coding-system (Lisp
5327 symbol) `iso-2022-7bit-lock' by default.
5328
5329 o coding-category-iso-8-else
5330
5331 The category for a coding system which has the same code range
df7492f9 5332 as ISO2022 of 8-bit environemnt but uses locking shift or
7717c392
KH
5333 single shift functions. Assigned the coding-system (Lisp
5334 symbol) `iso-2022-8bit-ss2' by default.
4ed46869
KH
5335
5336 o coding-category-big5
5337
5338 The category for a coding system which has the same code range
5339 as BIG5. Assigned the coding-system (Lisp symbol)
e0e989f6 5340 `cn-big5' by default.
4ed46869 5341
fa42c37f
KH
5342 o coding-category-utf-8
5343
5344 The category for a coding system which has the same code range
6e76ae91 5345 as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp
fa42c37f
KH
5346 symbol) `utf-8' by default.
5347
5348 o coding-category-utf-16-be
5349
5350 The category for a coding system in which a text has an
5351 Unicode signature (cf. Unicode Standard) in the order of BIG
5352 endian at the head. Assigned the coding-system (Lisp symbol)
5353 `utf-16-be' by default.
5354
5355 o coding-category-utf-16-le
5356
5357 The category for a coding system in which a text has an
5358 Unicode signature (cf. Unicode Standard) in the order of
5359 LITTLE endian at the head. Assigned the coding-system (Lisp
5360 symbol) `utf-16-le' by default.
5361
1397dc18
KH
5362 o coding-category-ccl
5363
5364 The category for a coding system of which encoder/decoder is
5365 written in CCL programs. The default value is nil, i.e., no
5366 coding system is assigned.
5367
4ed46869
KH
5368 o coding-category-binary
5369
5370 The category for a coding system not categorized in any of the
5371 above. Assigned the coding-system (Lisp symbol)
e0e989f6 5372 `no-conversion' by default.
4ed46869
KH
5373
5374 Each of them is a Lisp symbol and the value is an actual
df7492f9 5375 `coding-system's (this is also a Lisp symbol) assigned by a user.
4ed46869
KH
5376 What Emacs does actually is to detect a category of coding system.
5377 Then, it uses a `coding-system' assigned to it. If Emacs can't
df7492f9 5378 decide only one possible category, it selects a category of the
4ed46869
KH
5379 highest priority. Priorities of categories are also specified by a
5380 user in a Lisp variable `coding-category-list'.
5381
5382*/
5383
df7492f9
KH
5384#define EOL_SEEN_NONE 0
5385#define EOL_SEEN_LF 1
5386#define EOL_SEEN_CR 2
5387#define EOL_SEEN_CRLF 4
66cfb530 5388
ff0dacd7
KH
5389/* Detect how end-of-line of a text of length SRC_BYTES pointed by
5390 SOURCE is encoded. If CATEGORY is one of
5391 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
5392 two-byte, else they are encoded by one-byte.
5393
5394 Return one of EOL_SEEN_XXX. */
4ed46869 5395
bc4bc72a 5396#define MAX_EOL_CHECK_COUNT 3
d46c5b12
KH
5397
5398static int
89528eb3 5399detect_eol (source, src_bytes, category)
f6cbaf43 5400 const unsigned char *source;
df7492f9 5401 EMACS_INT src_bytes;
89528eb3 5402 enum coding_category category;
4ed46869 5403{
f6cbaf43 5404 const unsigned char *src = source, *src_end = src + src_bytes;
4ed46869 5405 unsigned char c;
df7492f9
KH
5406 int total = 0;
5407 int eol_seen = EOL_SEEN_NONE;
4ed46869 5408
89528eb3 5409 if ((1 << category) & CATEGORY_MASK_UTF_16)
4ed46869 5410 {
df7492f9 5411 int msb, lsb;
fa42c37f 5412
89528eb3
KH
5413 msb = category == (coding_category_utf_16_le
5414 | coding_category_utf_16_le_nosig);
df7492f9 5415 lsb = 1 - msb;
fa42c37f 5416
df7492f9 5417 while (src + 1 < src_end)
fa42c37f 5418 {
df7492f9
KH
5419 c = src[lsb];
5420 if (src[msb] == 0 && (c == '\n' || c == '\r'))
fa42c37f 5421 {
df7492f9
KH
5422 int this_eol;
5423
5424 if (c == '\n')
5425 this_eol = EOL_SEEN_LF;
5426 else if (src + 3 >= src_end
5427 || src[msb + 2] != 0
5428 || src[lsb + 2] != '\n')
5429 this_eol = EOL_SEEN_CR;
fa42c37f 5430 else
8f924df7 5431 this_eol = EOL_SEEN_CRLF;
df7492f9
KH
5432
5433 if (eol_seen == EOL_SEEN_NONE)
5434 /* This is the first end-of-line. */
5435 eol_seen = this_eol;
5436 else if (eol_seen != this_eol)
fa42c37f 5437 {
df7492f9
KH
5438 /* The found type is different from what found before. */
5439 eol_seen = EOL_SEEN_LF;
5440 break;
fa42c37f 5441 }
df7492f9
KH
5442 if (++total == MAX_EOL_CHECK_COUNT)
5443 break;
fa42c37f 5444 }
df7492f9 5445 src += 2;
fa42c37f 5446 }
bcf26d6a 5447 }
d46c5b12 5448 else
c4825358 5449 {
df7492f9 5450 while (src < src_end)
27901516 5451 {
df7492f9
KH
5452 c = *src++;
5453 if (c == '\n' || c == '\r')
5454 {
5455 int this_eol;
d46c5b12 5456
df7492f9
KH
5457 if (c == '\n')
5458 this_eol = EOL_SEEN_LF;
5459 else if (src >= src_end || *src != '\n')
5460 this_eol = EOL_SEEN_CR;
5461 else
5462 this_eol = EOL_SEEN_CRLF, src++;
d46c5b12 5463
df7492f9
KH
5464 if (eol_seen == EOL_SEEN_NONE)
5465 /* This is the first end-of-line. */
5466 eol_seen = this_eol;
5467 else if (eol_seen != this_eol)
5468 {
5469 /* The found type is different from what found before. */
5470 eol_seen = EOL_SEEN_LF;
5471 break;
5472 }
5473 if (++total == MAX_EOL_CHECK_COUNT)
5474 break;
5475 }
5476 }
73be902c 5477 }
df7492f9 5478 return eol_seen;
73be902c
KH
5479}
5480
df7492f9 5481
24a73b0a 5482static Lisp_Object
df7492f9
KH
5483adjust_coding_eol_type (coding, eol_seen)
5484 struct coding_system *coding;
5485 int eol_seen;
73be902c 5486{
0be8721c 5487 Lisp_Object eol_type;
8f924df7 5488
df7492f9
KH
5489 eol_type = CODING_ID_EOL_TYPE (coding->id);
5490 if (eol_seen & EOL_SEEN_LF)
24a73b0a
KH
5491 {
5492 coding->id = CODING_SYSTEM_ID (AREF (eol_type, 0));
5493 eol_type = Qunix;
5494 }
6f197c07 5495 else if (eol_seen & EOL_SEEN_CRLF)
24a73b0a
KH
5496 {
5497 coding->id = CODING_SYSTEM_ID (AREF (eol_type, 1));
5498 eol_type = Qdos;
5499 }
6f197c07 5500 else if (eol_seen & EOL_SEEN_CR)
24a73b0a
KH
5501 {
5502 coding->id = CODING_SYSTEM_ID (AREF (eol_type, 2));
5503 eol_type = Qmac;
5504 }
5505 return eol_type;
d46c5b12 5506}
4ed46869 5507
df7492f9
KH
5508/* Detect how a text specified in CODING is encoded. If a coding
5509 system is detected, update fields of CODING by the detected coding
5510 system. */
0a28aafb 5511
df7492f9
KH
5512void
5513detect_coding (coding)
d46c5b12 5514 struct coding_system *coding;
d46c5b12 5515{
8f924df7 5516 const unsigned char *src, *src_end;
d46c5b12 5517
df7492f9
KH
5518 coding->consumed = coding->consumed_char = 0;
5519 coding->produced = coding->produced_char = 0;
5520 coding_set_source (coding);
1c3478b0 5521
df7492f9 5522 src_end = coding->source + coding->src_bytes;
1c3478b0 5523
df7492f9
KH
5524 /* If we have not yet decided the text encoding type, detect it
5525 now. */
5526 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding->id)), Qundecided))
b73bfc1c 5527 {
df7492f9 5528 int c, i;
6cb21a4f 5529 struct coding_detection_info detect_info;
df7492f9 5530
6cb21a4f 5531 detect_info.checked = detect_info.found = detect_info.rejected = 0;
24a73b0a 5532 for (i = 0, src = coding->source; src < src_end; i++, src++)
d46c5b12 5533 {
df7492f9 5534 c = *src;
6cb21a4f 5535 if (c & 0x80)
df7492f9 5536 break;
6cb21a4f
KH
5537 if (c < 0x20
5538 && (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)
5539 && ! inhibit_iso_escape_detection
5540 && ! detect_info.checked)
5541 {
5542 coding->head_ascii = src - (coding->source + coding->consumed);
5543 if (detect_coding_iso_2022 (coding, &detect_info))
5544 {
5545 /* We have scanned the whole data. */
5546 if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE))
5547 /* We didn't find an 8-bit code. */
5548 src = src_end;
5549 break;
5550 }
5551 }
d46c5b12 5552 }
df7492f9
KH
5553 coding->head_ascii = src - (coding->source + coding->consumed);
5554
3aef54f3 5555 if (coding->head_ascii < coding->src_bytes
6cb21a4f 5556 || detect_info.found)
d46c5b12 5557 {
ff0dacd7
KH
5558 enum coding_category category;
5559 struct coding_system *this;
df7492f9 5560
6cb21a4f
KH
5561 if (coding->head_ascii == coding->src_bytes)
5562 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
5563 for (i = 0; i < coding_category_raw_text; i++)
5564 {
5565 category = coding_priorities[i];
5566 this = coding_categories + category;
5567 if (detect_info.found & (1 << category))
24a73b0a 5568 break;
6cb21a4f
KH
5569 }
5570 else
5571 for (i = 0; i < coding_category_raw_text; i++)
5572 {
5573 category = coding_priorities[i];
5574 this = coding_categories + category;
5575 if (this->id < 0)
5576 {
5577 /* No coding system of this category is defined. */
5578 detect_info.rejected |= (1 << category);
5579 }
5580 else if (category >= coding_category_raw_text)
5581 continue;
5582 else if (detect_info.checked & (1 << category))
5583 {
5584 if (detect_info.found & (1 << category))
5585 break;
5586 }
5587 else if ((*(this->detector)) (coding, &detect_info)
5588 && detect_info.found & (1 << category))
5589 {
5590 if (category == coding_category_utf_16_auto)
5591 {
5592 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
5593 category = coding_category_utf_16_le;
5594 else
5595 category = coding_category_utf_16_be;
5596 }
5597 break;
5598 }
5599 }
5600
ff0dacd7
KH
5601 if (i < coding_category_raw_text)
5602 setup_coding_system (CODING_ID_NAME (this->id), coding);
5603 else if (detect_info.rejected == CATEGORY_MASK_ANY)
df7492f9 5604 setup_coding_system (Qraw_text, coding);
ff0dacd7 5605 else if (detect_info.rejected)
df7492f9 5606 for (i = 0; i < coding_category_raw_text; i++)
ff0dacd7
KH
5607 if (! (detect_info.rejected & (1 << coding_priorities[i])))
5608 {
5609 this = coding_categories + coding_priorities[i];
5610 setup_coding_system (CODING_ID_NAME (this->id), coding);
5611 break;
5612 }
d46c5b12 5613 }
b73bfc1c 5614 }
24a73b0a
KH
5615 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding->id)))
5616 == coding_category_utf_16_auto)
b49a1807
KH
5617 {
5618 Lisp_Object coding_systems;
5619 struct coding_detection_info detect_info;
5620
5621 coding_systems
5622 = AREF (CODING_ID_ATTRS (coding->id), coding_attr_utf_16_bom);
5623 detect_info.found = detect_info.rejected = 0;
5624 if (CONSP (coding_systems)
24a73b0a 5625 && detect_coding_utf_16 (coding, &detect_info))
b49a1807
KH
5626 {
5627 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
5628 setup_coding_system (XCAR (coding_systems), coding);
24a73b0a 5629 else if (detect_info.found & CATEGORY_MASK_UTF_16_BE)
b49a1807
KH
5630 setup_coding_system (XCDR (coding_systems), coding);
5631 }
5632 }
4ed46869 5633}
4ed46869 5634
d46c5b12 5635
aaaf0b1e 5636static void
df7492f9 5637decode_eol (coding)
aaaf0b1e 5638 struct coding_system *coding;
aaaf0b1e 5639{
24a73b0a
KH
5640 Lisp_Object eol_type;
5641 unsigned char *p, *pbeg, *pend;
5642
5643 eol_type = CODING_ID_EOL_TYPE (coding->id);
5644 if (EQ (eol_type, Qunix))
5645 return;
5646
5647 if (NILP (coding->dst_object))
5648 pbeg = coding->destination;
5649 else
5650 pbeg = BYTE_POS_ADDR (coding->dst_pos_byte);
5651 pend = pbeg + coding->produced;
5652
5653 if (VECTORP (eol_type))
aaaf0b1e 5654 {
df7492f9 5655 int eol_seen = EOL_SEEN_NONE;
4ed46869 5656
24a73b0a 5657 for (p = pbeg; p < pend; p++)
aaaf0b1e 5658 {
df7492f9
KH
5659 if (*p == '\n')
5660 eol_seen |= EOL_SEEN_LF;
5661 else if (*p == '\r')
aaaf0b1e 5662 {
df7492f9 5663 if (p + 1 < pend && *(p + 1) == '\n')
aaaf0b1e 5664 {
df7492f9
KH
5665 eol_seen |= EOL_SEEN_CRLF;
5666 p++;
aaaf0b1e 5667 }
aaaf0b1e 5668 else
df7492f9 5669 eol_seen |= EOL_SEEN_CR;
aaaf0b1e 5670 }
aaaf0b1e 5671 }
24a73b0a
KH
5672 if (eol_seen != EOL_SEEN_NONE
5673 && eol_seen != EOL_SEEN_LF
5674 && eol_seen != EOL_SEEN_CRLF
5675 && eol_seen != EOL_SEEN_CR)
5676 eol_seen = EOL_SEEN_LF;
df7492f9 5677 if (eol_seen != EOL_SEEN_NONE)
24a73b0a 5678 eol_type = adjust_coding_eol_type (coding, eol_seen);
aaaf0b1e 5679 }
d46c5b12 5680
24a73b0a 5681 if (EQ (eol_type, Qmac))
27901516 5682 {
24a73b0a 5683 for (p = pbeg; p < pend; p++)
df7492f9
KH
5684 if (*p == '\r')
5685 *p = '\n';
4ed46869 5686 }
24a73b0a 5687 else if (EQ (eol_type, Qdos))
df7492f9 5688 {
24a73b0a 5689 int n = 0;
b73bfc1c 5690
24a73b0a
KH
5691 if (NILP (coding->dst_object))
5692 {
4347441b
KH
5693 /* Start deleting '\r' from the tail to minimize the memory
5694 movement. */
24a73b0a
KH
5695 for (p = pend - 2; p >= pbeg; p--)
5696 if (*p == '\r')
5697 {
5698 safe_bcopy ((char *) (p + 1), (char *) p, pend-- - p - 1);
5699 n++;
5700 }
5701 }
5702 else
5703 {
4347441b
KH
5704 int pos_byte = coding->dst_pos_byte;
5705 int pos = coding->dst_pos;
5706 int pos_end = pos + coding->produced_char - 1;
5707
5708 while (pos < pos_end)
5709 {
5710 p = BYTE_POS_ADDR (pos_byte);
5711 if (*p == '\r' && p[1] == '\n')
5712 {
5713 del_range_2 (pos, pos_byte, pos + 1, pos_byte + 1, 0);
5714 n++;
5715 pos_end--;
5716 }
5717 pos++;
5718 pos_byte += BYTES_BY_CHAR_HEAD (*p);
5719 }
24a73b0a
KH
5720 }
5721 coding->produced -= n;
5722 coding->produced_char -= n;
aaaf0b1e 5723 }
4ed46869
KH
5724}
5725
7d64c6ad 5726
a6f87d34
KH
5727/* Return a translation table (or list of them) from coding system
5728 attribute vector ATTRS for encoding (ENCODEP is nonzero) or
5729 decoding (ENCODEP is zero). */
7d64c6ad 5730
e6a54062 5731static Lisp_Object
09ee6fdd
KH
5732get_translation_table (attrs, encodep, max_lookup)
5733 Lisp_Object attrs;
5734 int encodep, *max_lookup;
7d64c6ad
KH
5735{
5736 Lisp_Object standard, translation_table;
09ee6fdd 5737 Lisp_Object val;
7d64c6ad
KH
5738
5739 if (encodep)
5740 translation_table = CODING_ATTR_ENCODE_TBL (attrs),
5741 standard = Vstandard_translation_table_for_encode;
5742 else
5743 translation_table = CODING_ATTR_DECODE_TBL (attrs),
5744 standard = Vstandard_translation_table_for_decode;
7d64c6ad 5745 if (NILP (translation_table))
09ee6fdd
KH
5746 translation_table = standard;
5747 else
a6f87d34 5748 {
09ee6fdd
KH
5749 if (SYMBOLP (translation_table))
5750 translation_table = Fget (translation_table, Qtranslation_table);
5751 else if (CONSP (translation_table))
5752 {
5753 translation_table = Fcopy_sequence (translation_table);
5754 for (val = translation_table; CONSP (val); val = XCDR (val))
5755 if (SYMBOLP (XCAR (val)))
5756 XSETCAR (val, Fget (XCAR (val), Qtranslation_table));
5757 }
5758 if (CHAR_TABLE_P (standard))
5759 {
5760 if (CONSP (translation_table))
5761 translation_table = nconc2 (translation_table,
5762 Fcons (standard, Qnil));
5763 else
5764 translation_table = Fcons (translation_table,
5765 Fcons (standard, Qnil));
5766 }
a6f87d34 5767 }
2170c8f0
KH
5768
5769 if (max_lookup)
09ee6fdd 5770 {
2170c8f0
KH
5771 *max_lookup = 1;
5772 if (CHAR_TABLE_P (translation_table)
5773 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table)) > 1)
5774 {
5775 val = XCHAR_TABLE (translation_table)->extras[1];
5776 if (NATNUMP (val) && *max_lookup < XFASTINT (val))
5777 *max_lookup = XFASTINT (val);
5778 }
5779 else if (CONSP (translation_table))
5780 {
5781 Lisp_Object tail, val;
09ee6fdd 5782
2170c8f0
KH
5783 for (tail = translation_table; CONSP (tail); tail = XCDR (tail))
5784 if (CHAR_TABLE_P (XCAR (tail))
5785 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail))) > 1)
5786 {
5787 val = XCHAR_TABLE (XCAR (tail))->extras[1];
5788 if (NATNUMP (val) && *max_lookup < XFASTINT (val))
5789 *max_lookup = XFASTINT (val);
5790 }
5791 }
a6f87d34 5792 }
7d64c6ad
KH
5793 return translation_table;
5794}
5795
09ee6fdd
KH
5796#define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
5797 do { \
5798 trans = Qnil; \
5799 if (CHAR_TABLE_P (table)) \
5800 { \
5801 trans = CHAR_TABLE_REF (table, c); \
5802 if (CHARACTERP (trans)) \
5803 c = XFASTINT (trans), trans = Qnil; \
5804 } \
5805 else if (CONSP (table)) \
5806 { \
5807 Lisp_Object tail; \
5808 \
5809 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
5810 if (CHAR_TABLE_P (XCAR (tail))) \
5811 { \
5812 trans = CHAR_TABLE_REF (XCAR (tail), c); \
5813 if (CHARACTERP (trans)) \
5814 c = XFASTINT (trans), trans = Qnil; \
5815 else if (! NILP (trans)) \
5816 break; \
5817 } \
5818 } \
e6a54062
KH
5819 } while (0)
5820
7d64c6ad 5821
69a80ea3
KH
5822static Lisp_Object
5823get_translation (val, buf, buf_end, last_block, from_nchars, to_nchars)
5824 Lisp_Object val;
5825 int *buf, *buf_end;
5826 int last_block;
5827 int *from_nchars, *to_nchars;
5828{
433f7f87
KH
5829 /* VAL is TO or (([FROM-CHAR ...] . TO) ...) where TO is TO-CHAR or
5830 [TO-CHAR ...]. */
69a80ea3
KH
5831 if (CONSP (val))
5832 {
433f7f87 5833 Lisp_Object from, tail;
69a80ea3
KH
5834 int i, len;
5835
433f7f87 5836 for (tail = val; CONSP (tail); tail = XCDR (tail))
69a80ea3 5837 {
433f7f87
KH
5838 val = XCAR (tail);
5839 from = XCAR (val);
5840 len = ASIZE (from);
5841 for (i = 0; i < len; i++)
5842 {
5843 if (buf + i == buf_end)
5844 {
5845 if (! last_block)
5846 return Qt;
5847 break;
5848 }
5849 if (XINT (AREF (from, i)) != buf[i])
5850 break;
5851 }
5852 if (i == len)
5853 {
5854 val = XCDR (val);
5855 *from_nchars = len;
5856 break;
5857 }
69a80ea3 5858 }
433f7f87
KH
5859 if (! CONSP (tail))
5860 return Qnil;
69a80ea3
KH
5861 }
5862 if (VECTORP (val))
5863 *buf = XINT (AREF (val, 0)), *to_nchars = ASIZE (val);
5864 else
5865 *buf = XINT (val);
5866 return val;
5867}
5868
5869
d46c5b12 5870static int
69a80ea3 5871produce_chars (coding, translation_table, last_block)
df7492f9 5872 struct coding_system *coding;
69a80ea3
KH
5873 Lisp_Object translation_table;
5874 int last_block;
4ed46869 5875{
df7492f9
KH
5876 unsigned char *dst = coding->destination + coding->produced;
5877 unsigned char *dst_end = coding->destination + coding->dst_bytes;
5878 int produced;
5879 int produced_chars = 0;
69a80ea3 5880 int carryover = 0;
4ed46869 5881
df7492f9 5882 if (! coding->chars_at_source)
4ed46869 5883 {
df7492f9 5884 /* Characters are in coding->charbuf. */
fba4576f
AS
5885 int *buf = coding->charbuf;
5886 int *buf_end = buf + coding->charbuf_used;
4ed46869 5887
df7492f9
KH
5888 if (BUFFERP (coding->src_object)
5889 && EQ (coding->src_object, coding->dst_object))
8f924df7 5890 dst_end = ((unsigned char *) coding->source) + coding->consumed;
4ed46869 5891
df7492f9 5892 while (buf < buf_end)
4ed46869 5893 {
69a80ea3 5894 int c = *buf, i;
bc4bc72a 5895
df7492f9
KH
5896 if (c >= 0)
5897 {
69a80ea3
KH
5898 int from_nchars = 1, to_nchars = 1;
5899 Lisp_Object trans = Qnil;
5900
09ee6fdd 5901 LOOKUP_TRANSLATION_TABLE (translation_table, c, trans);
e6a54062 5902 if (! NILP (trans))
69a80ea3
KH
5903 {
5904 trans = get_translation (trans, buf, buf_end, last_block,
5905 &from_nchars, &to_nchars);
5906 if (EQ (trans, Qt))
5907 break;
5908 c = *buf;
5909 }
5910
5911 if (dst + MAX_MULTIBYTE_LENGTH * to_nchars > dst_end)
5912 {
5913 dst = alloc_destination (coding,
5914 buf_end - buf
5915 + MAX_MULTIBYTE_LENGTH * to_nchars,
5916 dst);
5917 dst_end = coding->destination + coding->dst_bytes;
5918 }
5919
433f7f87 5920 for (i = 0; i < to_nchars; i++)
69a80ea3 5921 {
433f7f87
KH
5922 if (i > 0)
5923 c = XINT (AREF (trans, i));
69a80ea3
KH
5924 if (coding->dst_multibyte
5925 || ! CHAR_BYTE8_P (c))
5926 CHAR_STRING_ADVANCE (c, dst);
5927 else
5928 *dst++ = CHAR_TO_BYTE8 (c);
5929 }
5930 produced_chars += to_nchars;
5931 *buf++ = to_nchars;
5932 while (--from_nchars > 0)
5933 *buf++ = 0;
d46c5b12 5934 }
df7492f9 5935 else
69a80ea3
KH
5936 /* This is an annotation datum. (-C) is the length. */
5937 buf += -c;
4ed46869 5938 }
69a80ea3 5939 carryover = buf_end - buf;
4ed46869 5940 }
fa42c37f 5941 else
fa42c37f 5942 {
8f924df7
KH
5943 const unsigned char *src = coding->source;
5944 const unsigned char *src_end = src + coding->src_bytes;
df7492f9 5945 Lisp_Object eol_type;
fa42c37f 5946
df7492f9 5947 eol_type = CODING_ID_EOL_TYPE (coding->id);
4ed46869 5948
df7492f9 5949 if (coding->src_multibyte != coding->dst_multibyte)
fa42c37f 5950 {
df7492f9 5951 if (coding->src_multibyte)
fa42c37f 5952 {
71c81426 5953 int multibytep = 1;
df7492f9 5954 int consumed_chars;
d46c5b12 5955
df7492f9
KH
5956 while (1)
5957 {
8f924df7 5958 const unsigned char *src_base = src;
df7492f9 5959 int c;
b73bfc1c 5960
df7492f9
KH
5961 ONE_MORE_BYTE (c);
5962 if (c == '\r')
5963 {
5964 if (EQ (eol_type, Qdos))
5965 {
98725083
KH
5966 if (src == src_end)
5967 {
065e3595
KH
5968 record_conversion_result
5969 (coding, CODING_RESULT_INSUFFICIENT_SRC);
98725083
KH
5970 goto no_more_source;
5971 }
5972 if (*src == '\n')
df7492f9
KH
5973 c = *src++;
5974 }
5975 else if (EQ (eol_type, Qmac))
5976 c = '\n';
5977 }
5978 if (dst == dst_end)
5979 {
2c78b7e1 5980 coding->consumed = src - coding->source;
b73bfc1c 5981
2c78b7e1 5982 if (EQ (coding->src_object, coding->dst_object))
8f924df7 5983 dst_end = (unsigned char *) src;
2c78b7e1
KH
5984 if (dst == dst_end)
5985 {
5986 dst = alloc_destination (coding, src_end - src + 1,
5987 dst);
5988 dst_end = coding->destination + coding->dst_bytes;
5989 coding_set_source (coding);
5990 src = coding->source + coding->consumed;
5991 src_end = coding->source + coding->src_bytes;
5992 }
df7492f9
KH
5993 }
5994 *dst++ = c;
5995 produced_chars++;
5996 }
5997 no_more_source:
5998 ;
fa42c37f
KH
5999 }
6000 else
df7492f9
KH
6001 while (src < src_end)
6002 {
71c81426 6003 int multibytep = 1;
df7492f9 6004 int c = *src++;
b73bfc1c 6005
df7492f9
KH
6006 if (c == '\r')
6007 {
6008 if (EQ (eol_type, Qdos))
6009 {
6010 if (src < src_end
6011 && *src == '\n')
6012 c = *src++;
6013 }
6014 else if (EQ (eol_type, Qmac))
6015 c = '\n';
6016 }
6017 if (dst >= dst_end - 1)
6018 {
2c78b7e1 6019 coding->consumed = src - coding->source;
df7492f9 6020
2c78b7e1 6021 if (EQ (coding->src_object, coding->dst_object))
8f924df7 6022 dst_end = (unsigned char *) src;
2c78b7e1
KH
6023 if (dst >= dst_end - 1)
6024 {
6025 dst = alloc_destination (coding, src_end - src + 2,
6026 dst);
6027 dst_end = coding->destination + coding->dst_bytes;
6028 coding_set_source (coding);
6029 src = coding->source + coding->consumed;
6030 src_end = coding->source + coding->src_bytes;
6031 }
df7492f9
KH
6032 }
6033 EMIT_ONE_BYTE (c);
6034 }
d46c5b12 6035 }
df7492f9
KH
6036 else
6037 {
6038 if (!EQ (coding->src_object, coding->dst_object))
fa42c37f 6039 {
df7492f9 6040 int require = coding->src_bytes - coding->dst_bytes;
4ed46869 6041
df7492f9 6042 if (require > 0)
fa42c37f 6043 {
df7492f9
KH
6044 EMACS_INT offset = src - coding->source;
6045
6046 dst = alloc_destination (coding, require, dst);
6047 coding_set_source (coding);
6048 src = coding->source + offset;
6049 src_end = coding->source + coding->src_bytes;
fa42c37f
KH
6050 }
6051 }
df7492f9
KH
6052 produced_chars = coding->src_chars;
6053 while (src < src_end)
fa42c37f 6054 {
df7492f9
KH
6055 int c = *src++;
6056
6057 if (c == '\r')
6058 {
6059 if (EQ (eol_type, Qdos))
6060 {
6061 if (src < src_end
6062 && *src == '\n')
6063 c = *src++;
6064 produced_chars--;
6065 }
6066 else if (EQ (eol_type, Qmac))
6067 c = '\n';
6068 }
6069 *dst++ = c;
fa42c37f
KH
6070 }
6071 }
2c78b7e1
KH
6072 coding->consumed = coding->src_bytes;
6073 coding->consumed_char = coding->src_chars;
fa42c37f
KH
6074 }
6075
df7492f9 6076 produced = dst - (coding->destination + coding->produced);
284201e4 6077 if (BUFFERP (coding->dst_object) && produced_chars > 0)
df7492f9
KH
6078 insert_from_gap (produced_chars, produced);
6079 coding->produced += produced;
6080 coding->produced_char += produced_chars;
69a80ea3 6081 return carryover;
fa42c37f
KH
6082}
6083
ff0dacd7
KH
6084/* Compose text in CODING->object according to the annotation data at
6085 CHARBUF. CHARBUF is an array:
6086 [ -LENGTH ANNOTATION_MASK FROM TO METHOD COMP_LEN [ COMPONENTS... ] ]
df7492f9 6087 */
4ed46869 6088
df7492f9 6089static INLINE void
69a80ea3 6090produce_composition (coding, charbuf, pos)
4ed46869 6091 struct coding_system *coding;
df7492f9 6092 int *charbuf;
69a80ea3 6093 EMACS_INT pos;
4ed46869 6094{
df7492f9 6095 int len;
69a80ea3 6096 EMACS_INT to;
df7492f9 6097 enum composition_method method;
df7492f9 6098 Lisp_Object components;
fa42c37f 6099
df7492f9 6100 len = -charbuf[0];
69a80ea3 6101 to = pos + charbuf[2];
9ffd559c
KH
6102 if (to <= pos)
6103 return;
69a80ea3 6104 method = (enum composition_method) (charbuf[3]);
d46c5b12 6105
df7492f9
KH
6106 if (method == COMPOSITION_RELATIVE)
6107 components = Qnil;
9ffd559c
KH
6108 else if (method >= COMPOSITION_WITH_RULE
6109 && method <= COMPOSITION_WITH_RULE_ALTCHARS)
d46c5b12 6110 {
df7492f9
KH
6111 Lisp_Object args[MAX_COMPOSITION_COMPONENTS * 2 - 1];
6112 int i;
b73bfc1c 6113
69a80ea3
KH
6114 len -= 4;
6115 charbuf += 4;
df7492f9 6116 for (i = 0; i < len; i++)
9ffd559c
KH
6117 {
6118 args[i] = make_number (charbuf[i]);
f75c90a9 6119 if (charbuf[i] < 0)
9ffd559c
KH
6120 return;
6121 }
df7492f9
KH
6122 components = (method == COMPOSITION_WITH_ALTCHARS
6123 ? Fstring (len, args) : Fvector (len, args));
d46c5b12 6124 }
9ffd559c
KH
6125 else
6126 return;
69a80ea3 6127 compose_text (pos, to, components, Qnil, coding->dst_object);
d46c5b12
KH
6128}
6129
d46c5b12 6130
ff0dacd7
KH
6131/* Put `charset' property on text in CODING->object according to
6132 the annotation data at CHARBUF. CHARBUF is an array:
69a80ea3 6133 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
ff0dacd7 6134 */
d46c5b12 6135
ff0dacd7 6136static INLINE void
69a80ea3 6137produce_charset (coding, charbuf, pos)
d46c5b12 6138 struct coding_system *coding;
ff0dacd7 6139 int *charbuf;
69a80ea3 6140 EMACS_INT pos;
d46c5b12 6141{
69a80ea3
KH
6142 EMACS_INT from = pos - charbuf[2];
6143 struct charset *charset = CHARSET_FROM_ID (charbuf[3]);
b73bfc1c 6144
69a80ea3 6145 Fput_text_property (make_number (from), make_number (pos),
ff0dacd7
KH
6146 Qcharset, CHARSET_NAME (charset),
6147 coding->dst_object);
d46c5b12
KH
6148}
6149
d46c5b12 6150
df7492f9
KH
6151#define CHARBUF_SIZE 0x4000
6152
6153#define ALLOC_CONVERSION_WORK_AREA(coding) \
6154 do { \
6155 int size = CHARBUF_SIZE;; \
6156 \
6157 coding->charbuf = NULL; \
6158 while (size > 1024) \
6159 { \
6160 coding->charbuf = (int *) alloca (sizeof (int) * size); \
6161 if (coding->charbuf) \
6162 break; \
6163 size >>= 1; \
6164 } \
6165 if (! coding->charbuf) \
6166 { \
065e3595 6167 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \
df7492f9
KH
6168 return coding->result; \
6169 } \
6170 coding->charbuf_size = size; \
6171 } while (0)
4ed46869 6172
d46c5b12
KH
6173
6174static void
69a80ea3 6175produce_annotation (coding, pos)
d46c5b12 6176 struct coding_system *coding;
69a80ea3 6177 EMACS_INT pos;
d46c5b12 6178{
df7492f9
KH
6179 int *charbuf = coding->charbuf;
6180 int *charbuf_end = charbuf + coding->charbuf_used;
d46c5b12 6181
ff0dacd7
KH
6182 if (NILP (coding->dst_object))
6183 return;
d46c5b12 6184
df7492f9 6185 while (charbuf < charbuf_end)
a84f1519 6186 {
df7492f9 6187 if (*charbuf >= 0)
69a80ea3 6188 pos += *charbuf++;
d46c5b12 6189 else
d46c5b12 6190 {
df7492f9 6191 int len = -*charbuf;
ff0dacd7 6192 switch (charbuf[1])
df7492f9
KH
6193 {
6194 case CODING_ANNOTATE_COMPOSITION_MASK:
69a80ea3 6195 produce_composition (coding, charbuf, pos);
df7492f9 6196 break;
ff0dacd7 6197 case CODING_ANNOTATE_CHARSET_MASK:
69a80ea3 6198 produce_charset (coding, charbuf, pos);
ff0dacd7 6199 break;
df7492f9
KH
6200 default:
6201 abort ();
6202 }
6203 charbuf += len;
d46c5b12 6204 }
a84f1519 6205 }
d46c5b12
KH
6206}
6207
df7492f9
KH
6208/* Decode the data at CODING->src_object into CODING->dst_object.
6209 CODING->src_object is a buffer, a string, or nil.
6210 CODING->dst_object is a buffer.
d46c5b12 6211
df7492f9
KH
6212 If CODING->src_object is a buffer, it must be the current buffer.
6213 In this case, if CODING->src_pos is positive, it is a position of
6214 the source text in the buffer, otherwise, the source text is in the
6215 gap area of the buffer, and CODING->src_pos specifies the offset of
6216 the text from GPT (which must be the same as PT). If this is the
6217 same buffer as CODING->dst_object, CODING->src_pos must be
6218 negative.
d46c5b12 6219
b6828792 6220 If CODING->src_object is a string, CODING->src_pos is an index to
df7492f9 6221 that string.
d46c5b12 6222
df7492f9
KH
6223 If CODING->src_object is nil, CODING->source must already point to
6224 the non-relocatable memory area. In this case, CODING->src_pos is
6225 an offset from CODING->source.
73be902c 6226
df7492f9
KH
6227 The decoded data is inserted at the current point of the buffer
6228 CODING->dst_object.
6229*/
d46c5b12 6230
df7492f9
KH
6231static int
6232decode_coding (coding)
d46c5b12 6233 struct coding_system *coding;
d46c5b12 6234{
df7492f9 6235 Lisp_Object attrs;
24a73b0a 6236 Lisp_Object undo_list;
7d64c6ad 6237 Lisp_Object translation_table;
69a80ea3
KH
6238 int carryover;
6239 int i;
d46c5b12 6240
df7492f9
KH
6241 if (BUFFERP (coding->src_object)
6242 && coding->src_pos > 0
6243 && coding->src_pos < GPT
6244 && coding->src_pos + coding->src_chars > GPT)
6245 move_gap_both (coding->src_pos, coding->src_pos_byte);
d46c5b12 6246
24a73b0a 6247 undo_list = Qt;
df7492f9 6248 if (BUFFERP (coding->dst_object))
1c3478b0 6249 {
df7492f9
KH
6250 if (current_buffer != XBUFFER (coding->dst_object))
6251 set_buffer_internal (XBUFFER (coding->dst_object));
6252 if (GPT != PT)
6253 move_gap_both (PT, PT_BYTE);
24a73b0a
KH
6254 undo_list = current_buffer->undo_list;
6255 current_buffer->undo_list = Qt;
1c3478b0
KH
6256 }
6257
df7492f9
KH
6258 coding->consumed = coding->consumed_char = 0;
6259 coding->produced = coding->produced_char = 0;
6260 coding->chars_at_source = 0;
065e3595 6261 record_conversion_result (coding, CODING_RESULT_SUCCESS);
df7492f9 6262 coding->errors = 0;
1c3478b0 6263
df7492f9
KH
6264 ALLOC_CONVERSION_WORK_AREA (coding);
6265
6266 attrs = CODING_ID_ATTRS (coding->id);
2170c8f0 6267 translation_table = get_translation_table (attrs, 0, NULL);
df7492f9 6268
69a80ea3 6269 carryover = 0;
df7492f9 6270 do
b73bfc1c 6271 {
69a80ea3
KH
6272 EMACS_INT pos = coding->dst_pos + coding->produced_char;
6273
df7492f9
KH
6274 coding_set_source (coding);
6275 coding->annotated = 0;
69a80ea3 6276 coding->charbuf_used = carryover;
df7492f9 6277 (*(coding->decoder)) (coding);
df7492f9 6278 coding_set_destination (coding);
69a80ea3 6279 carryover = produce_chars (coding, translation_table, 0);
df7492f9 6280 if (coding->annotated)
69a80ea3
KH
6281 produce_annotation (coding, pos);
6282 for (i = 0; i < carryover; i++)
6283 coding->charbuf[i]
6284 = coding->charbuf[coding->charbuf_used - carryover + i];
d46c5b12 6285 }
df7492f9 6286 while (coding->consumed < coding->src_bytes
54b367bb
KH
6287 && (coding->result == CODING_RESULT_SUCCESS
6288 || coding->result == CODING_RESULT_INVALID_SRC));
d46c5b12 6289
69a80ea3
KH
6290 if (carryover > 0)
6291 {
6292 coding_set_destination (coding);
6293 coding->charbuf_used = carryover;
6294 produce_chars (coding, translation_table, 1);
6295 }
6296
df7492f9
KH
6297 coding->carryover_bytes = 0;
6298 if (coding->consumed < coding->src_bytes)
d46c5b12 6299 {
df7492f9 6300 int nbytes = coding->src_bytes - coding->consumed;
8f924df7 6301 const unsigned char *src;
df7492f9
KH
6302
6303 coding_set_source (coding);
6304 coding_set_destination (coding);
6305 src = coding->source + coding->consumed;
6306
6307 if (coding->mode & CODING_MODE_LAST_BLOCK)
1c3478b0 6308 {
df7492f9
KH
6309 /* Flush out unprocessed data as binary chars. We are sure
6310 that the number of data is less than the size of
6311 coding->charbuf. */
065e3595 6312 coding->charbuf_used = 0;
df7492f9 6313 while (nbytes-- > 0)
1c3478b0 6314 {
df7492f9 6315 int c = *src++;
98725083 6316
1c91457d
KH
6317 if (c & 0x80)
6318 c = BYTE8_TO_CHAR (c);
6319 coding->charbuf[coding->charbuf_used++] = c;
1c3478b0 6320 }
f6cbaf43 6321 produce_chars (coding, Qnil, 1);
d46c5b12 6322 }
d46c5b12 6323 else
df7492f9
KH
6324 {
6325 /* Record unprocessed bytes in coding->carryover. We are
6326 sure that the number of data is less than the size of
6327 coding->carryover. */
6328 unsigned char *p = coding->carryover;
6329
6330 coding->carryover_bytes = nbytes;
6331 while (nbytes-- > 0)
6332 *p++ = *src++;
1c3478b0 6333 }
df7492f9 6334 coding->consumed = coding->src_bytes;
b73bfc1c 6335 }
69f76525 6336
4347441b
KH
6337 if (! EQ (CODING_ID_EOL_TYPE (coding->id), Qunix))
6338 decode_eol (coding);
24a73b0a
KH
6339 if (BUFFERP (coding->dst_object))
6340 {
6341 current_buffer->undo_list = undo_list;
6342 record_insert (coding->dst_pos, coding->produced_char);
6343 }
73be902c 6344 return coding->result;
4ed46869
KH
6345}
6346
aaaf0b1e 6347
e1c23804 6348/* Extract an annotation datum from a composition starting at POS and
ff0dacd7
KH
6349 ending before LIMIT of CODING->src_object (buffer or string), store
6350 the data in BUF, set *STOP to a starting position of the next
6351 composition (if any) or to LIMIT, and return the address of the
6352 next element of BUF.
6353
6354 If such an annotation is not found, set *STOP to a starting
6355 position of a composition after POS (if any) or to LIMIT, and
6356 return BUF. */
6357
6358static INLINE int *
6359handle_composition_annotation (pos, limit, coding, buf, stop)
6360 EMACS_INT pos, limit;
aaaf0b1e 6361 struct coding_system *coding;
ff0dacd7
KH
6362 int *buf;
6363 EMACS_INT *stop;
aaaf0b1e 6364{
ff0dacd7
KH
6365 EMACS_INT start, end;
6366 Lisp_Object prop;
aaaf0b1e 6367
ff0dacd7
KH
6368 if (! find_composition (pos, limit, &start, &end, &prop, coding->src_object)
6369 || end > limit)
6370 *stop = limit;
6371 else if (start > pos)
6372 *stop = start;
6373 else
aaaf0b1e 6374 {
ff0dacd7 6375 if (start == pos)
aaaf0b1e 6376 {
ff0dacd7
KH
6377 /* We found a composition. Store the corresponding
6378 annotation data in BUF. */
6379 int *head = buf;
6380 enum composition_method method = COMPOSITION_METHOD (prop);
6381 int nchars = COMPOSITION_LENGTH (prop);
6382
69a80ea3 6383 ADD_COMPOSITION_DATA (buf, nchars, method);
ff0dacd7 6384 if (method != COMPOSITION_RELATIVE)
aaaf0b1e 6385 {
ff0dacd7
KH
6386 Lisp_Object components;
6387 int len, i, i_byte;
6388
6389 components = COMPOSITION_COMPONENTS (prop);
6390 if (VECTORP (components))
aaaf0b1e 6391 {
ff0dacd7
KH
6392 len = XVECTOR (components)->size;
6393 for (i = 0; i < len; i++)
6394 *buf++ = XINT (AREF (components, i));
aaaf0b1e 6395 }
ff0dacd7 6396 else if (STRINGP (components))
aaaf0b1e 6397 {
8f924df7 6398 len = SCHARS (components);
ff0dacd7
KH
6399 i = i_byte = 0;
6400 while (i < len)
6401 {
6402 FETCH_STRING_CHAR_ADVANCE (*buf, components, i, i_byte);
6403 buf++;
6404 }
6405 }
6406 else if (INTEGERP (components))
6407 {
6408 len = 1;
6409 *buf++ = XINT (components);
6410 }
6411 else if (CONSP (components))
6412 {
6413 for (len = 0; CONSP (components);
6414 len++, components = XCDR (components))
6415 *buf++ = XINT (XCAR (components));
aaaf0b1e 6416 }
aaaf0b1e 6417 else
ff0dacd7
KH
6418 abort ();
6419 *head -= len;
aaaf0b1e 6420 }
aaaf0b1e 6421 }
ff0dacd7
KH
6422
6423 if (find_composition (end, limit, &start, &end, &prop,
6424 coding->src_object)
6425 && end <= limit)
6426 *stop = start;
6427 else
6428 *stop = limit;
aaaf0b1e 6429 }
ff0dacd7
KH
6430 return buf;
6431}
6432
6433
e1c23804 6434/* Extract an annotation datum from a text property `charset' at POS of
ff0dacd7
KH
6435 CODING->src_object (buffer of string), store the data in BUF, set
6436 *STOP to the position where the value of `charset' property changes
6437 (limiting by LIMIT), and return the address of the next element of
6438 BUF.
6439
6440 If the property value is nil, set *STOP to the position where the
6441 property value is non-nil (limiting by LIMIT), and return BUF. */
6442
6443static INLINE int *
6444handle_charset_annotation (pos, limit, coding, buf, stop)
6445 EMACS_INT pos, limit;
6446 struct coding_system *coding;
6447 int *buf;
6448 EMACS_INT *stop;
6449{
6450 Lisp_Object val, next;
6451 int id;
6452
6453 val = Fget_text_property (make_number (pos), Qcharset, coding->src_object);
6454 if (! NILP (val) && CHARSETP (val))
6455 id = XINT (CHARSET_SYMBOL_ID (val));
6456 else
6457 id = -1;
69a80ea3 6458 ADD_CHARSET_DATA (buf, 0, id);
ff0dacd7
KH
6459 next = Fnext_single_property_change (make_number (pos), Qcharset,
6460 coding->src_object,
6461 make_number (limit));
6462 *stop = XINT (next);
6463 return buf;
6464}
6465
6466
df7492f9 6467static void
09ee6fdd 6468consume_chars (coding, translation_table, max_lookup)
df7492f9 6469 struct coding_system *coding;
433f7f87 6470 Lisp_Object translation_table;
09ee6fdd 6471 int max_lookup;
df7492f9
KH
6472{
6473 int *buf = coding->charbuf;
ff0dacd7 6474 int *buf_end = coding->charbuf + coding->charbuf_size;
7c78e542 6475 const unsigned char *src = coding->source + coding->consumed;
4776e638 6476 const unsigned char *src_end = coding->source + coding->src_bytes;
ff0dacd7
KH
6477 EMACS_INT pos = coding->src_pos + coding->consumed_char;
6478 EMACS_INT end_pos = coding->src_pos + coding->src_chars;
df7492f9
KH
6479 int multibytep = coding->src_multibyte;
6480 Lisp_Object eol_type;
6481 int c;
ff0dacd7 6482 EMACS_INT stop, stop_composition, stop_charset;
09ee6fdd 6483 int *lookup_buf = NULL;
433f7f87
KH
6484
6485 if (! NILP (translation_table))
09ee6fdd 6486 lookup_buf = alloca (sizeof (int) * max_lookup);
88993dfd 6487
df7492f9
KH
6488 eol_type = CODING_ID_EOL_TYPE (coding->id);
6489 if (VECTORP (eol_type))
6490 eol_type = Qunix;
88993dfd 6491
df7492f9
KH
6492 /* Note: composition handling is not yet implemented. */
6493 coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
ec6d2bb8 6494
0b5670c9
KH
6495 if (NILP (coding->src_object))
6496 stop = stop_composition = stop_charset = end_pos;
ff0dacd7 6497 else
0b5670c9
KH
6498 {
6499 if (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK)
6500 stop = stop_composition = pos;
6501 else
6502 stop = stop_composition = end_pos;
6503 if (coding->common_flags & CODING_ANNOTATE_CHARSET_MASK)
6504 stop = stop_charset = pos;
6505 else
6506 stop_charset = end_pos;
6507 }
ec6d2bb8 6508
24a73b0a 6509 /* Compensate for CRLF and conversion. */
ff0dacd7 6510 buf_end -= 1 + MAX_ANNOTATION_LENGTH;
df7492f9 6511 while (buf < buf_end)
aaaf0b1e 6512 {
433f7f87
KH
6513 Lisp_Object trans;
6514
df7492f9 6515 if (pos == stop)
ec6d2bb8 6516 {
df7492f9
KH
6517 if (pos == end_pos)
6518 break;
ff0dacd7
KH
6519 if (pos == stop_composition)
6520 buf = handle_composition_annotation (pos, end_pos, coding,
6521 buf, &stop_composition);
6522 if (pos == stop_charset)
6523 buf = handle_charset_annotation (pos, end_pos, coding,
6524 buf, &stop_charset);
6525 stop = (stop_composition < stop_charset
6526 ? stop_composition : stop_charset);
df7492f9
KH
6527 }
6528
6529 if (! multibytep)
4776e638 6530 {
d3e4cb56 6531 EMACS_INT bytes;
aaaf0b1e 6532
ea29edf2
KH
6533 if (coding->encoder == encode_coding_raw_text)
6534 c = *src++, pos++;
6535 else if ((bytes = MULTIBYTE_LENGTH (src, src_end)) > 0)
4776e638
KH
6536 c = STRING_CHAR_ADVANCE (src), pos += bytes;
6537 else
f03caae0 6538 c = BYTE8_TO_CHAR (*src), src++, pos++;
4776e638 6539 }
df7492f9 6540 else
4776e638 6541 c = STRING_CHAR_ADVANCE (src), pos++;
df7492f9
KH
6542 if ((c == '\r') && (coding->mode & CODING_MODE_SELECTIVE_DISPLAY))
6543 c = '\n';
6544 if (! EQ (eol_type, Qunix))
aaaf0b1e 6545 {
df7492f9 6546 if (c == '\n')
aaaf0b1e 6547 {
df7492f9
KH
6548 if (EQ (eol_type, Qdos))
6549 *buf++ = '\r';
6550 else
6551 c = '\r';
aaaf0b1e
KH
6552 }
6553 }
433f7f87 6554
e6a54062 6555 trans = Qnil;
09ee6fdd 6556 LOOKUP_TRANSLATION_TABLE (translation_table, c, trans);
e6a54062 6557 if (NILP (trans))
433f7f87
KH
6558 *buf++ = c;
6559 else
6560 {
6561 int from_nchars = 1, to_nchars = 1;
6562 int *lookup_buf_end;
6563 const unsigned char *p = src;
6564 int i;
6565
6566 lookup_buf[0] = c;
6567 for (i = 1; i < max_lookup && p < src_end; i++)
6568 lookup_buf[i] = STRING_CHAR_ADVANCE (p);
6569 lookup_buf_end = lookup_buf + i;
6570 trans = get_translation (trans, lookup_buf, lookup_buf_end, 1,
6571 &from_nchars, &to_nchars);
6572 if (EQ (trans, Qt)
6573 || buf + to_nchars > buf_end)
6574 break;
6575 *buf++ = *lookup_buf;
6576 for (i = 1; i < to_nchars; i++)
6577 *buf++ = XINT (AREF (trans, i));
6578 for (i = 1; i < from_nchars; i++, pos++)
6579 src += MULTIBYTE_LENGTH_NO_CHECK (src);
6580 }
aaaf0b1e 6581 }
ec6d2bb8 6582
df7492f9
KH
6583 coding->consumed = src - coding->source;
6584 coding->consumed_char = pos - coding->src_pos;
6585 coding->charbuf_used = buf - coding->charbuf;
6586 coding->chars_at_source = 0;
aaaf0b1e
KH
6587}
6588
4ed46869 6589
df7492f9
KH
6590/* Encode the text at CODING->src_object into CODING->dst_object.
6591 CODING->src_object is a buffer or a string.
6592 CODING->dst_object is a buffer or nil.
6593
6594 If CODING->src_object is a buffer, it must be the current buffer.
6595 In this case, if CODING->src_pos is positive, it is a position of
6596 the source text in the buffer, otherwise. the source text is in the
6597 gap area of the buffer, and coding->src_pos specifies the offset of
6598 the text from GPT (which must be the same as PT). If this is the
6599 same buffer as CODING->dst_object, CODING->src_pos must be
6600 negative and CODING should not have `pre-write-conversion'.
6601
6602 If CODING->src_object is a string, CODING should not have
6603 `pre-write-conversion'.
6604
6605 If CODING->dst_object is a buffer, the encoded data is inserted at
6606 the current point of that buffer.
6607
6608 If CODING->dst_object is nil, the encoded data is placed at the
6609 memory area specified by CODING->destination. */
6610
6611static int
6612encode_coding (coding)
4ed46869 6613 struct coding_system *coding;
4ed46869 6614{
df7492f9 6615 Lisp_Object attrs;
7d64c6ad 6616 Lisp_Object translation_table;
09ee6fdd 6617 int max_lookup;
9861e777 6618
df7492f9 6619 attrs = CODING_ID_ATTRS (coding->id);
ea29edf2
KH
6620 if (coding->encoder == encode_coding_raw_text)
6621 translation_table = Qnil, max_lookup = 0;
6622 else
6623 translation_table = get_translation_table (attrs, 1, &max_lookup);
4ed46869 6624
df7492f9 6625 if (BUFFERP (coding->dst_object))
8844fa83 6626 {
df7492f9
KH
6627 set_buffer_internal (XBUFFER (coding->dst_object));
6628 coding->dst_multibyte
6629 = ! NILP (current_buffer->enable_multibyte_characters);
8844fa83 6630 }
4ed46869 6631
b73bfc1c 6632 coding->consumed = coding->consumed_char = 0;
df7492f9 6633 coding->produced = coding->produced_char = 0;
065e3595 6634 record_conversion_result (coding, CODING_RESULT_SUCCESS);
b73bfc1c 6635 coding->errors = 0;
b73bfc1c 6636
df7492f9 6637 ALLOC_CONVERSION_WORK_AREA (coding);
4ed46869 6638
df7492f9
KH
6639 do {
6640 coding_set_source (coding);
09ee6fdd 6641 consume_chars (coding, translation_table, max_lookup);
df7492f9
KH
6642 coding_set_destination (coding);
6643 (*(coding->encoder)) (coding);
6644 } while (coding->consumed_char < coding->src_chars);
6645
284201e4 6646 if (BUFFERP (coding->dst_object) && coding->produced_char > 0)
df7492f9
KH
6647 insert_from_gap (coding->produced_char, coding->produced);
6648
6649 return (coding->result);
ec6d2bb8
KH
6650}
6651
fb88bf2d 6652
24a73b0a
KH
6653/* Name (or base name) of work buffer for code conversion. */
6654static Lisp_Object Vcode_conversion_workbuf_name;
d46c5b12 6655
24a73b0a
KH
6656/* A working buffer used by the top level conversion. Once it is
6657 created, it is never destroyed. It has the name
6658 Vcode_conversion_workbuf_name. The other working buffers are
6659 destroyed after the use is finished, and their names are modified
6660 versions of Vcode_conversion_workbuf_name. */
6661static Lisp_Object Vcode_conversion_reused_workbuf;
b73bfc1c 6662
24a73b0a
KH
6663/* 1 iff Vcode_conversion_reused_workbuf is already in use. */
6664static int reused_workbuf_in_use;
4ed46869 6665
24a73b0a
KH
6666
6667/* Return a working buffer of code convesion. MULTIBYTE specifies the
6668 multibyteness of returning buffer. */
b73bfc1c 6669
f6cbaf43 6670static Lisp_Object
24a73b0a 6671make_conversion_work_buffer (multibyte)
f6cbaf43 6672 int multibyte;
df7492f9 6673{
24a73b0a
KH
6674 Lisp_Object name, workbuf;
6675 struct buffer *current;
4ed46869 6676
24a73b0a 6677 if (reused_workbuf_in_use++)
065e3595
KH
6678 {
6679 name = Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name, Qnil);
6680 workbuf = Fget_buffer_create (name);
6681 }
df7492f9 6682 else
065e3595
KH
6683 {
6684 name = Vcode_conversion_workbuf_name;
6685 workbuf = Fget_buffer_create (name);
6686 if (NILP (Vcode_conversion_reused_workbuf))
6687 Vcode_conversion_reused_workbuf = workbuf;
6688 }
24a73b0a
KH
6689 current = current_buffer;
6690 set_buffer_internal (XBUFFER (workbuf));
6691 Ferase_buffer ();
df7492f9 6692 current_buffer->undo_list = Qt;
24a73b0a 6693 current_buffer->enable_multibyte_characters = multibyte ? Qt : Qnil;
df7492f9 6694 set_buffer_internal (current);
24a73b0a 6695 return workbuf;
df7492f9 6696}
d46c5b12 6697
24a73b0a 6698
4776e638 6699static Lisp_Object
24a73b0a
KH
6700code_conversion_restore (arg)
6701 Lisp_Object arg;
4776e638 6702{
24a73b0a 6703 Lisp_Object current, workbuf;
948bdcf3 6704 struct gcpro gcpro1;
24a73b0a 6705
948bdcf3 6706 GCPRO1 (arg);
24a73b0a
KH
6707 current = XCAR (arg);
6708 workbuf = XCDR (arg);
6709 if (! NILP (workbuf))
6710 {
6711 if (EQ (workbuf, Vcode_conversion_reused_workbuf))
6712 reused_workbuf_in_use = 0;
6713 else if (! NILP (Fbuffer_live_p (workbuf)))
6714 Fkill_buffer (workbuf);
6715 }
6716 set_buffer_internal (XBUFFER (current));
948bdcf3 6717 UNGCPRO;
4776e638
KH
6718 return Qnil;
6719}
b73bfc1c 6720
24a73b0a
KH
6721Lisp_Object
6722code_conversion_save (with_work_buf, multibyte)
4776e638 6723 int with_work_buf, multibyte;
df7492f9 6724{
24a73b0a 6725 Lisp_Object workbuf = Qnil;
b73bfc1c 6726
4776e638 6727 if (with_work_buf)
24a73b0a
KH
6728 workbuf = make_conversion_work_buffer (multibyte);
6729 record_unwind_protect (code_conversion_restore,
6730 Fcons (Fcurrent_buffer (), workbuf));
4776e638 6731 return workbuf;
df7492f9 6732}
d46c5b12 6733
df7492f9
KH
6734int
6735decode_coding_gap (coding, chars, bytes)
6736 struct coding_system *coding;
6737 EMACS_INT chars, bytes;
6738{
6739 int count = specpdl_ptr - specpdl;
5e5c78be 6740 Lisp_Object attrs;
fb88bf2d 6741
24a73b0a 6742 code_conversion_save (0, 0);
ec6d2bb8 6743
24a73b0a 6744 coding->src_object = Fcurrent_buffer ();
df7492f9
KH
6745 coding->src_chars = chars;
6746 coding->src_bytes = bytes;
6747 coding->src_pos = -chars;
6748 coding->src_pos_byte = -bytes;
6749 coding->src_multibyte = chars < bytes;
24a73b0a 6750 coding->dst_object = coding->src_object;
df7492f9
KH
6751 coding->dst_pos = PT;
6752 coding->dst_pos_byte = PT_BYTE;
71c81426 6753 coding->dst_multibyte = ! NILP (current_buffer->enable_multibyte_characters);
4ed46869 6754
df7492f9
KH
6755 if (CODING_REQUIRE_DETECTION (coding))
6756 detect_coding (coding);
8f924df7 6757
9286b333 6758 coding->mode |= CODING_MODE_LAST_BLOCK;
287c57d7 6759 current_buffer->text->inhibit_shrinking = 1;
df7492f9 6760 decode_coding (coding);
287c57d7 6761 current_buffer->text->inhibit_shrinking = 0;
d46c5b12 6762
5e5c78be
KH
6763 attrs = CODING_ID_ATTRS (coding->id);
6764 if (! NILP (CODING_ATTR_POST_READ (attrs)))
b73bfc1c 6765 {
5e5c78be
KH
6766 EMACS_INT prev_Z = Z, prev_Z_BYTE = Z_BYTE;
6767 Lisp_Object val;
6768
6769 TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte);
5e5c78be
KH
6770 val = call1 (CODING_ATTR_POST_READ (attrs),
6771 make_number (coding->produced_char));
5e5c78be
KH
6772 CHECK_NATNUM (val);
6773 coding->produced_char += Z - prev_Z;
6774 coding->produced += Z_BYTE - prev_Z_BYTE;
b73bfc1c 6775 }
4ed46869 6776
df7492f9 6777 unbind_to (count, Qnil);
b73bfc1c
KH
6778 return coding->result;
6779}
52d41803 6780
4ed46869 6781int
df7492f9 6782encode_coding_gap (coding, chars, bytes)
4ed46869 6783 struct coding_system *coding;
df7492f9 6784 EMACS_INT chars, bytes;
4ed46869 6785{
df7492f9 6786 int count = specpdl_ptr - specpdl;
4ed46869 6787
24a73b0a 6788 code_conversion_save (0, 0);
4ed46869 6789
24a73b0a 6790 coding->src_object = Fcurrent_buffer ();
df7492f9
KH
6791 coding->src_chars = chars;
6792 coding->src_bytes = bytes;
6793 coding->src_pos = -chars;
6794 coding->src_pos_byte = -bytes;
6795 coding->src_multibyte = chars < bytes;
6796 coding->dst_object = coding->src_object;
6797 coding->dst_pos = PT;
6798 coding->dst_pos_byte = PT_BYTE;
4ed46869 6799
df7492f9 6800 encode_coding (coding);
b73bfc1c 6801
df7492f9
KH
6802 unbind_to (count, Qnil);
6803 return coding->result;
6804}
4ed46869 6805
d46c5b12 6806
df7492f9
KH
6807/* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
6808 SRC_OBJECT into DST_OBJECT by coding context CODING.
b73bfc1c 6809
df7492f9 6810 SRC_OBJECT is a buffer, a string, or Qnil.
b73bfc1c 6811
df7492f9
KH
6812 If it is a buffer, the text is at point of the buffer. FROM and TO
6813 are positions in the buffer.
b73bfc1c 6814
df7492f9
KH
6815 If it is a string, the text is at the beginning of the string.
6816 FROM and TO are indices to the string.
4ed46869 6817
df7492f9
KH
6818 If it is nil, the text is at coding->source. FROM and TO are
6819 indices to coding->source.
bb10be8b 6820
df7492f9 6821 DST_OBJECT is a buffer, Qt, or Qnil.
4ed46869 6822
df7492f9
KH
6823 If it is a buffer, the decoded text is inserted at point of the
6824 buffer. If the buffer is the same as SRC_OBJECT, the source text
6825 is deleted.
4ed46869 6826
df7492f9
KH
6827 If it is Qt, a string is made from the decoded text, and
6828 set in CODING->dst_object.
d46c5b12 6829
df7492f9 6830 If it is Qnil, the decoded text is stored at CODING->destination.
2cb26057 6831 The caller must allocate CODING->dst_bytes bytes at
df7492f9
KH
6832 CODING->destination by xmalloc. If the decoded text is longer than
6833 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
6834 */
d46c5b12 6835
df7492f9
KH
6836void
6837decode_coding_object (coding, src_object, from, from_byte, to, to_byte,
6838 dst_object)
d46c5b12 6839 struct coding_system *coding;
df7492f9
KH
6840 Lisp_Object src_object;
6841 EMACS_INT from, from_byte, to, to_byte;
6842 Lisp_Object dst_object;
d46c5b12 6843{
df7492f9
KH
6844 int count = specpdl_ptr - specpdl;
6845 unsigned char *destination;
6846 EMACS_INT dst_bytes;
6847 EMACS_INT chars = to - from;
6848 EMACS_INT bytes = to_byte - from_byte;
6849 Lisp_Object attrs;
4776e638
KH
6850 Lisp_Object buffer;
6851 int saved_pt = -1, saved_pt_byte;
d46c5b12 6852
4776e638 6853 buffer = Fcurrent_buffer ();
93dec019 6854
df7492f9 6855 if (NILP (dst_object))
d46c5b12 6856 {
df7492f9
KH
6857 destination = coding->destination;
6858 dst_bytes = coding->dst_bytes;
d46c5b12 6859 }
93dec019 6860
df7492f9
KH
6861 coding->src_object = src_object;
6862 coding->src_chars = chars;
6863 coding->src_bytes = bytes;
6864 coding->src_multibyte = chars < bytes;
70ad9fc4 6865
df7492f9 6866 if (STRINGP (src_object))
d46c5b12 6867 {
df7492f9
KH
6868 coding->src_pos = from;
6869 coding->src_pos_byte = from_byte;
d46c5b12 6870 }
df7492f9 6871 else if (BUFFERP (src_object))
88993dfd 6872 {
df7492f9
KH
6873 set_buffer_internal (XBUFFER (src_object));
6874 if (from != GPT)
6875 move_gap_both (from, from_byte);
6876 if (EQ (src_object, dst_object))
fb88bf2d 6877 {
4776e638 6878 saved_pt = PT, saved_pt_byte = PT_BYTE;
df7492f9
KH
6879 TEMP_SET_PT_BOTH (from, from_byte);
6880 del_range_both (from, from_byte, to, to_byte, 1);
6881 coding->src_pos = -chars;
6882 coding->src_pos_byte = -bytes;
fb88bf2d 6883 }
df7492f9 6884 else
fb88bf2d 6885 {
df7492f9
KH
6886 coding->src_pos = from;
6887 coding->src_pos_byte = from_byte;
fb88bf2d 6888 }
88993dfd
KH
6889 }
6890
df7492f9
KH
6891 if (CODING_REQUIRE_DETECTION (coding))
6892 detect_coding (coding);
6893 attrs = CODING_ID_ATTRS (coding->id);
d46c5b12 6894
2cb26057
KH
6895 if (EQ (dst_object, Qt)
6896 || (! NILP (CODING_ATTR_POST_READ (attrs))
6897 && NILP (dst_object)))
b73bfc1c 6898 {
24a73b0a 6899 coding->dst_object = code_conversion_save (1, 1);
df7492f9
KH
6900 coding->dst_pos = BEG;
6901 coding->dst_pos_byte = BEG_BYTE;
6902 coding->dst_multibyte = 1;
b73bfc1c 6903 }
df7492f9 6904 else if (BUFFERP (dst_object))
d46c5b12 6905 {
24a73b0a 6906 code_conversion_save (0, 0);
df7492f9
KH
6907 coding->dst_object = dst_object;
6908 coding->dst_pos = BUF_PT (XBUFFER (dst_object));
6909 coding->dst_pos_byte = BUF_PT_BYTE (XBUFFER (dst_object));
6910 coding->dst_multibyte
6911 = ! NILP (XBUFFER (dst_object)->enable_multibyte_characters);
d46c5b12
KH
6912 }
6913 else
6914 {
24a73b0a 6915 code_conversion_save (0, 0);
df7492f9
KH
6916 coding->dst_object = Qnil;
6917 coding->dst_multibyte = 1;
d46c5b12
KH
6918 }
6919
df7492f9 6920 decode_coding (coding);
fa46990e 6921
df7492f9
KH
6922 if (BUFFERP (coding->dst_object))
6923 set_buffer_internal (XBUFFER (coding->dst_object));
d46c5b12 6924
df7492f9 6925 if (! NILP (CODING_ATTR_POST_READ (attrs)))
d46c5b12 6926 {
df7492f9
KH
6927 struct gcpro gcpro1, gcpro2;
6928 EMACS_INT prev_Z = Z, prev_Z_BYTE = Z_BYTE;
2b4f9037 6929 Lisp_Object val;
d46c5b12 6930
c0cc7f7f 6931 TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte);
df7492f9 6932 GCPRO2 (coding->src_object, coding->dst_object);
d4850d67
KH
6933 val = safe_call1 (CODING_ATTR_POST_READ (attrs),
6934 make_number (coding->produced_char));
df7492f9
KH
6935 UNGCPRO;
6936 CHECK_NATNUM (val);
6937 coding->produced_char += Z - prev_Z;
6938 coding->produced += Z_BYTE - prev_Z_BYTE;
d46c5b12 6939 }
de79a6a5 6940
df7492f9 6941 if (EQ (dst_object, Qt))
ec6d2bb8 6942 {
df7492f9
KH
6943 coding->dst_object = Fbuffer_string ();
6944 }
6945 else if (NILP (dst_object) && BUFFERP (coding->dst_object))
6946 {
6947 set_buffer_internal (XBUFFER (coding->dst_object));
6948 if (dst_bytes < coding->produced)
6949 {
6950 destination
6951 = (unsigned char *) xrealloc (destination, coding->produced);
6952 if (! destination)
6953 {
065e3595
KH
6954 record_conversion_result (coding,
6955 CODING_RESULT_INSUFFICIENT_DST);
df7492f9
KH
6956 unbind_to (count, Qnil);
6957 return;
6958 }
6959 if (BEGV < GPT && GPT < BEGV + coding->produced_char)
6960 move_gap_both (BEGV, BEGV_BYTE);
6961 bcopy (BEGV_ADDR, destination, coding->produced);
6962 coding->destination = destination;
d46c5b12 6963 }
ec6d2bb8 6964 }
b73bfc1c 6965
4776e638
KH
6966 if (saved_pt >= 0)
6967 {
6968 /* This is the case of:
6969 (BUFFERP (src_object) && EQ (src_object, dst_object))
6970 As we have moved PT while replacing the original buffer
6971 contents, we must recover it now. */
6972 set_buffer_internal (XBUFFER (src_object));
6973 if (saved_pt < from)
6974 TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte);
6975 else if (saved_pt < from + chars)
6976 TEMP_SET_PT_BOTH (from, from_byte);
6977 else if (! NILP (current_buffer->enable_multibyte_characters))
6978 TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars),
6979 saved_pt_byte + (coding->produced - bytes));
6980 else
6981 TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes),
6982 saved_pt_byte + (coding->produced - bytes));
d46c5b12 6983 }
4776e638 6984
065e3595 6985 unbind_to (count, coding->dst_object);
d46c5b12
KH
6986}
6987
d46c5b12 6988
df7492f9
KH
6989void
6990encode_coding_object (coding, src_object, from, from_byte, to, to_byte,
6991 dst_object)
d46c5b12 6992 struct coding_system *coding;
df7492f9
KH
6993 Lisp_Object src_object;
6994 EMACS_INT from, from_byte, to, to_byte;
6995 Lisp_Object dst_object;
d46c5b12 6996{
b73bfc1c 6997 int count = specpdl_ptr - specpdl;
df7492f9
KH
6998 EMACS_INT chars = to - from;
6999 EMACS_INT bytes = to_byte - from_byte;
7000 Lisp_Object attrs;
4776e638
KH
7001 Lisp_Object buffer;
7002 int saved_pt = -1, saved_pt_byte;
c02d943b 7003 int kill_src_buffer = 0;
df7492f9 7004
4776e638 7005 buffer = Fcurrent_buffer ();
df7492f9
KH
7006
7007 coding->src_object = src_object;
7008 coding->src_chars = chars;
7009 coding->src_bytes = bytes;
7010 coding->src_multibyte = chars < bytes;
7011
7012 attrs = CODING_ID_ATTRS (coding->id);
7013
7014 if (! NILP (CODING_ATTR_PRE_WRITE (attrs)))
6bac5b12 7015 {
24a73b0a 7016 coding->src_object = code_conversion_save (1, coding->src_multibyte);
df7492f9
KH
7017 set_buffer_internal (XBUFFER (coding->src_object));
7018 if (STRINGP (src_object))
7019 insert_from_string (src_object, from, from_byte, chars, bytes, 0);
7020 else if (BUFFERP (src_object))
7021 insert_from_buffer (XBUFFER (src_object), from, chars, 0);
7022 else
7023 insert_1_both (coding->source + from, chars, bytes, 0, 0, 0);
d46c5b12 7024
df7492f9
KH
7025 if (EQ (src_object, dst_object))
7026 {
7027 set_buffer_internal (XBUFFER (src_object));
4776e638 7028 saved_pt = PT, saved_pt_byte = PT_BYTE;
df7492f9
KH
7029 del_range_both (from, from_byte, to, to_byte, 1);
7030 set_buffer_internal (XBUFFER (coding->src_object));
7031 }
7032
d4850d67
KH
7033 {
7034 Lisp_Object args[3];
7035
7036 args[0] = CODING_ATTR_PRE_WRITE (attrs);
7037 args[1] = make_number (BEG);
7038 args[2] = make_number (Z);
7039 safe_call (3, args);
7040 }
c02d943b
KH
7041 if (XBUFFER (coding->src_object) != current_buffer)
7042 kill_src_buffer = 1;
ac87bbef 7043 coding->src_object = Fcurrent_buffer ();
df7492f9
KH
7044 if (BEG != GPT)
7045 move_gap_both (BEG, BEG_BYTE);
7046 coding->src_chars = Z - BEG;
7047 coding->src_bytes = Z_BYTE - BEG_BYTE;
7048 coding->src_pos = BEG;
7049 coding->src_pos_byte = BEG_BYTE;
7050 coding->src_multibyte = Z < Z_BYTE;
7051 }
7052 else if (STRINGP (src_object))
d46c5b12 7053 {
24a73b0a 7054 code_conversion_save (0, 0);
df7492f9
KH
7055 coding->src_pos = from;
7056 coding->src_pos_byte = from_byte;
b73bfc1c 7057 }
df7492f9 7058 else if (BUFFERP (src_object))
b73bfc1c 7059 {
24a73b0a 7060 code_conversion_save (0, 0);
df7492f9 7061 set_buffer_internal (XBUFFER (src_object));
df7492f9 7062 if (EQ (src_object, dst_object))
d46c5b12 7063 {
4776e638 7064 saved_pt = PT, saved_pt_byte = PT_BYTE;
ff0dacd7
KH
7065 coding->src_object = del_range_1 (from, to, 1, 1);
7066 coding->src_pos = 0;
7067 coding->src_pos_byte = 0;
d46c5b12 7068 }
df7492f9 7069 else
d46c5b12 7070 {
ff0dacd7
KH
7071 if (from < GPT && to >= GPT)
7072 move_gap_both (from, from_byte);
df7492f9
KH
7073 coding->src_pos = from;
7074 coding->src_pos_byte = from_byte;
d46c5b12 7075 }
d46c5b12 7076 }
4776e638 7077 else
24a73b0a 7078 code_conversion_save (0, 0);
d46c5b12 7079
df7492f9 7080 if (BUFFERP (dst_object))
88993dfd 7081 {
df7492f9 7082 coding->dst_object = dst_object;
28f67a95
KH
7083 if (EQ (src_object, dst_object))
7084 {
7085 coding->dst_pos = from;
7086 coding->dst_pos_byte = from_byte;
7087 }
7088 else
7089 {
7090 coding->dst_pos = BUF_PT (XBUFFER (dst_object));
7091 coding->dst_pos_byte = BUF_PT_BYTE (XBUFFER (dst_object));
7092 }
df7492f9
KH
7093 coding->dst_multibyte
7094 = ! NILP (XBUFFER (dst_object)->enable_multibyte_characters);
88993dfd 7095 }
df7492f9 7096 else if (EQ (dst_object, Qt))
d46c5b12 7097 {
df7492f9 7098 coding->dst_object = Qnil;
df7492f9 7099 coding->dst_bytes = coding->src_chars;
ac87bbef
KH
7100 if (coding->dst_bytes == 0)
7101 coding->dst_bytes = 1;
7102 coding->destination = (unsigned char *) xmalloc (coding->dst_bytes);
df7492f9 7103 coding->dst_multibyte = 0;
d46c5b12
KH
7104 }
7105 else
7106 {
df7492f9
KH
7107 coding->dst_object = Qnil;
7108 coding->dst_multibyte = 0;
d46c5b12
KH
7109 }
7110
df7492f9 7111 encode_coding (coding);
d46c5b12 7112
df7492f9 7113 if (EQ (dst_object, Qt))
d46c5b12 7114 {
df7492f9
KH
7115 if (BUFFERP (coding->dst_object))
7116 coding->dst_object = Fbuffer_string ();
7117 else
d46c5b12 7118 {
df7492f9
KH
7119 coding->dst_object
7120 = make_unibyte_string ((char *) coding->destination,
7121 coding->produced);
7122 xfree (coding->destination);
d46c5b12 7123 }
4ed46869 7124 }
d46c5b12 7125
4776e638
KH
7126 if (saved_pt >= 0)
7127 {
7128 /* This is the case of:
7129 (BUFFERP (src_object) && EQ (src_object, dst_object))
7130 As we have moved PT while replacing the original buffer
7131 contents, we must recover it now. */
7132 set_buffer_internal (XBUFFER (src_object));
7133 if (saved_pt < from)
7134 TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte);
7135 else if (saved_pt < from + chars)
7136 TEMP_SET_PT_BOTH (from, from_byte);
7137 else if (! NILP (current_buffer->enable_multibyte_characters))
7138 TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars),
7139 saved_pt_byte + (coding->produced - bytes));
d46c5b12 7140 else
4776e638
KH
7141 TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes),
7142 saved_pt_byte + (coding->produced - bytes));
7143 }
7144
c02d943b
KH
7145 if (kill_src_buffer)
7146 Fkill_buffer (coding->src_object);
df7492f9 7147 unbind_to (count, Qnil);
b73bfc1c
KH
7148}
7149
df7492f9 7150
b73bfc1c 7151Lisp_Object
df7492f9 7152preferred_coding_system ()
b73bfc1c 7153{
df7492f9 7154 int id = coding_categories[coding_priorities[0]].id;
2391eaa4 7155
df7492f9 7156 return CODING_ID_NAME (id);
4ed46869
KH
7157}
7158
7159\f
7160#ifdef emacs
1397dc18 7161/*** 8. Emacs Lisp library functions ***/
4ed46869 7162
4ed46869 7163DEFUN ("coding-system-p", Fcoding_system_p, Scoding_system_p, 1, 1, 0,
48b0f3ae 7164 doc: /* Return t if OBJECT is nil or a coding-system.
df7492f9 7165See the documentation of `define-coding-system' for information
48b0f3ae
PJ
7166about coding-system objects. */)
7167 (obj)
4ed46869
KH
7168 Lisp_Object obj;
7169{
44e8490d
KH
7170 if (NILP (obj)
7171 || CODING_SYSTEM_ID (obj) >= 0)
7172 return Qt;
7173 if (! SYMBOLP (obj)
7174 || NILP (Fget (obj, Qcoding_system_define_form)))
7175 return Qnil;
7176 return Qt;
4ed46869
KH
7177}
7178
9d991de8
RS
7179DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system,
7180 Sread_non_nil_coding_system, 1, 1, 0,
48b0f3ae
PJ
7181 doc: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
7182 (prompt)
4ed46869
KH
7183 Lisp_Object prompt;
7184{
e0e989f6 7185 Lisp_Object val;
9d991de8
RS
7186 do
7187 {
4608c386
KH
7188 val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil,
7189 Qt, Qnil, Qcoding_system_history, Qnil, Qnil);
9d991de8 7190 }
8f924df7 7191 while (SCHARS (val) == 0);
e0e989f6 7192 return (Fintern (val, Qnil));
4ed46869
KH
7193}
7194
9b787f3e 7195DEFUN ("read-coding-system", Fread_coding_system, Sread_coding_system, 1, 2, 0,
48b0f3ae
PJ
7196 doc: /* Read a coding system from the minibuffer, prompting with string PROMPT.
7197If the user enters null input, return second argument DEFAULT-CODING-SYSTEM. */)
7198 (prompt, default_coding_system)
9b787f3e 7199 Lisp_Object prompt, default_coding_system;
4ed46869 7200{
f44d27ce 7201 Lisp_Object val;
9b787f3e 7202 if (SYMBOLP (default_coding_system))
a3181084 7203 XSETSTRING (default_coding_system, XPNTR (SYMBOL_NAME (default_coding_system)));
4608c386 7204 val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil,
9b787f3e
RS
7205 Qt, Qnil, Qcoding_system_history,
7206 default_coding_system, Qnil);
8f924df7 7207 return (SCHARS (val) == 0 ? Qnil : Fintern (val, Qnil));
4ed46869
KH
7208}
7209
7210DEFUN ("check-coding-system", Fcheck_coding_system, Scheck_coding_system,
7211 1, 1, 0,
48b0f3ae 7212 doc: /* Check validity of CODING-SYSTEM.
9ffd559c
KH
7213If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
7214It is valid if it is nil or a symbol defined as a coding system by the
7215function `define-coding-system'. */)
df7492f9 7216 (coding_system)
4ed46869
KH
7217 Lisp_Object coding_system;
7218{
44e8490d
KH
7219 Lisp_Object define_form;
7220
7221 define_form = Fget (coding_system, Qcoding_system_define_form);
7222 if (! NILP (define_form))
7223 {
7224 Fput (coding_system, Qcoding_system_define_form, Qnil);
7225 safe_eval (define_form);
7226 }
4ed46869
KH
7227 if (!NILP (Fcoding_system_p (coding_system)))
7228 return coding_system;
fcad4ec4 7229 xsignal1 (Qcoding_system_error, coding_system);
4ed46869 7230}
df7492f9 7231
3a73fa5d 7232\f
89528eb3
KH
7233/* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
7234 HIGHEST is nonzero, return the coding system of the highest
7235 priority among the detected coding systems. Otherwize return a
7236 list of detected coding systems sorted by their priorities. If
7237 MULTIBYTEP is nonzero, it is assumed that the bytes are in correct
7238 multibyte form but contains only ASCII and eight-bit chars.
7239 Otherwise, the bytes are raw bytes.
7240
7241 CODING-SYSTEM controls the detection as below:
7242
7243 If it is nil, detect both text-format and eol-format. If the
7244 text-format part of CODING-SYSTEM is already specified
7245 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
7246 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
7247 detect only text-format. */
7248
d46c5b12 7249Lisp_Object
24a73b0a
KH
7250detect_coding_system (src, src_chars, src_bytes, highest, multibytep,
7251 coding_system)
8f924df7 7252 const unsigned char *src;
24a73b0a 7253 int src_chars, src_bytes, highest;
0a28aafb 7254 int multibytep;
df7492f9 7255 Lisp_Object coding_system;
4ed46869 7256{
8f924df7 7257 const unsigned char *src_end = src + src_bytes;
df7492f9
KH
7258 Lisp_Object attrs, eol_type;
7259 Lisp_Object val;
7260 struct coding_system coding;
89528eb3 7261 int id;
ff0dacd7 7262 struct coding_detection_info detect_info;
24a73b0a 7263 enum coding_category base_category;
b73bfc1c 7264
df7492f9
KH
7265 if (NILP (coding_system))
7266 coding_system = Qundecided;
7267 setup_coding_system (coding_system, &coding);
7268 attrs = CODING_ID_ATTRS (coding.id);
7269 eol_type = CODING_ID_EOL_TYPE (coding.id);
89528eb3 7270 coding_system = CODING_ATTR_BASE_NAME (attrs);
4ed46869 7271
df7492f9 7272 coding.source = src;
24a73b0a 7273 coding.src_chars = src_chars;
df7492f9
KH
7274 coding.src_bytes = src_bytes;
7275 coding.src_multibyte = multibytep;
7276 coding.consumed = 0;
89528eb3 7277 coding.mode |= CODING_MODE_LAST_BLOCK;
d46c5b12 7278
ff0dacd7 7279 detect_info.checked = detect_info.found = detect_info.rejected = 0;
d46c5b12 7280
89528eb3 7281 /* At first, detect text-format if necessary. */
24a73b0a
KH
7282 base_category = XINT (CODING_ATTR_CATEGORY (attrs));
7283 if (base_category == coding_category_undecided)
4ed46869 7284 {
ff0dacd7
KH
7285 enum coding_category category;
7286 struct coding_system *this;
7287 int c, i;
88993dfd 7288
24a73b0a
KH
7289 /* Skip all ASCII bytes except for a few ISO2022 controls. */
7290 for (i = 0; src < src_end; i++, src++)
4ed46869 7291 {
df7492f9 7292 c = *src;
6cb21a4f 7293 if (c & 0x80)
d46c5b12 7294 break;
6cb21a4f
KH
7295 if (c < 0x20
7296 && (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)
c7266f4a 7297 && ! inhibit_iso_escape_detection)
6cb21a4f
KH
7298 {
7299 coding.head_ascii = src - coding.source;
7300 if (detect_coding_iso_2022 (&coding, &detect_info))
7301 {
7302 /* We have scanned the whole data. */
7303 if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE))
7304 /* We didn't find an 8-bit code. */
7305 src = src_end;
7306 break;
7307 }
7308 }
4ed46869 7309 }
df7492f9 7310 coding.head_ascii = src - coding.source;
88993dfd 7311
6cb21a4f
KH
7312 if (src < src_end
7313 || detect_info.found)
7314 {
7315 if (src == src_end)
7316 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
7317 for (i = 0; i < coding_category_raw_text; i++)
ff0dacd7 7318 {
6cb21a4f 7319 category = coding_priorities[i];
c7266f4a 7320 this = coding_categories + category;
6cb21a4f 7321 if (detect_info.found & (1 << category))
ff0dacd7
KH
7322 break;
7323 }
6cb21a4f
KH
7324 else
7325 for (i = 0; i < coding_category_raw_text; i++)
df7492f9 7326 {
6cb21a4f
KH
7327 category = coding_priorities[i];
7328 this = coding_categories + category;
7329
7330 if (this->id < 0)
24a73b0a 7331 {
6cb21a4f
KH
7332 /* No coding system of this category is defined. */
7333 detect_info.rejected |= (1 << category);
7334 }
7335 else if (category >= coding_category_raw_text)
7336 continue;
7337 else if (detect_info.checked & (1 << category))
7338 {
7339 if (highest
7340 && (detect_info.found & (1 << category)))
7341 break;
7342 }
7343 else
7344 {
7345 if ((*(this->detector)) (&coding, &detect_info)
7346 && highest
7347 && (detect_info.found & (1 << category)))
24a73b0a 7348 {
6cb21a4f
KH
7349 if (category == coding_category_utf_16_auto)
7350 {
7351 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
7352 category = coding_category_utf_16_le;
7353 else
7354 category = coding_category_utf_16_be;
7355 }
7356 break;
24a73b0a 7357 }
24a73b0a 7358 }
df7492f9 7359 }
6cb21a4f 7360 }
ec6d2bb8 7361
ff0dacd7 7362 if (detect_info.rejected == CATEGORY_MASK_ANY)
ec6d2bb8 7363 {
ff0dacd7 7364 detect_info.found = CATEGORY_MASK_RAW_TEXT;
89528eb3
KH
7365 id = coding_categories[coding_category_raw_text].id;
7366 val = Fcons (make_number (id), Qnil);
7367 }
ff0dacd7 7368 else if (! detect_info.rejected && ! detect_info.found)
89528eb3 7369 {
ff0dacd7 7370 detect_info.found = CATEGORY_MASK_ANY;
89528eb3
KH
7371 id = coding_categories[coding_category_undecided].id;
7372 val = Fcons (make_number (id), Qnil);
7373 }
7374 else if (highest)
7375 {
ff0dacd7 7376 if (detect_info.found)
ec6d2bb8 7377 {
ff0dacd7
KH
7378 detect_info.found = 1 << category;
7379 val = Fcons (make_number (this->id), Qnil);
7380 }
7381 else
7382 for (i = 0; i < coding_category_raw_text; i++)
7383 if (! (detect_info.rejected & (1 << coding_priorities[i])))
7384 {
7385 detect_info.found = 1 << coding_priorities[i];
7386 id = coding_categories[coding_priorities[i]].id;
7387 val = Fcons (make_number (id), Qnil);
7388 break;
7389 }
7390 }
89528eb3
KH
7391 else
7392 {
ff0dacd7
KH
7393 int mask = detect_info.rejected | detect_info.found;
7394 int found = 0;
89528eb3 7395 val = Qnil;
ec6d2bb8 7396
89528eb3 7397 for (i = coding_category_raw_text - 1; i >= 0; i--)
ff0dacd7
KH
7398 {
7399 category = coding_priorities[i];
7400 if (! (mask & (1 << category)))
ec6d2bb8 7401 {
ff0dacd7
KH
7402 found |= 1 << category;
7403 id = coding_categories[category].id;
c7266f4a
KH
7404 if (id >= 0)
7405 val = Fcons (make_number (id), val);
ff0dacd7
KH
7406 }
7407 }
7408 for (i = coding_category_raw_text - 1; i >= 0; i--)
7409 {
7410 category = coding_priorities[i];
7411 if (detect_info.found & (1 << category))
7412 {
7413 id = coding_categories[category].id;
7414 val = Fcons (make_number (id), val);
ec6d2bb8 7415 }
ec6d2bb8 7416 }
ff0dacd7 7417 detect_info.found |= found;
ec6d2bb8 7418 }
ec6d2bb8 7419 }
24a73b0a
KH
7420 else if (base_category == coding_category_utf_16_auto)
7421 {
7422 if (detect_coding_utf_16 (&coding, &detect_info))
7423 {
24a73b0a
KH
7424 struct coding_system *this;
7425
7426 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
7427 this = coding_categories + coding_category_utf_16_le;
7428 else if (detect_info.found & CATEGORY_MASK_UTF_16_BE)
7429 this = coding_categories + coding_category_utf_16_be;
7430 else if (detect_info.rejected & CATEGORY_MASK_UTF_16_LE_NOSIG)
7431 this = coding_categories + coding_category_utf_16_be_nosig;
7432 else
7433 this = coding_categories + coding_category_utf_16_le_nosig;
7434 val = Fcons (make_number (this->id), Qnil);
7435 }
7436 }
df7492f9
KH
7437 else
7438 {
ff0dacd7 7439 detect_info.found = 1 << XINT (CODING_ATTR_CATEGORY (attrs));
89528eb3 7440 val = Fcons (make_number (coding.id), Qnil);
4ed46869 7441 }
df7492f9 7442
89528eb3 7443 /* Then, detect eol-format if necessary. */
df7492f9 7444 {
89528eb3 7445 int normal_eol = -1, utf_16_be_eol = -1, utf_16_le_eol;
df7492f9
KH
7446 Lisp_Object tail;
7447
89528eb3
KH
7448 if (VECTORP (eol_type))
7449 {
ff0dacd7 7450 if (detect_info.found & ~CATEGORY_MASK_UTF_16)
89528eb3
KH
7451 normal_eol = detect_eol (coding.source, src_bytes,
7452 coding_category_raw_text);
ff0dacd7
KH
7453 if (detect_info.found & (CATEGORY_MASK_UTF_16_BE
7454 | CATEGORY_MASK_UTF_16_BE_NOSIG))
89528eb3
KH
7455 utf_16_be_eol = detect_eol (coding.source, src_bytes,
7456 coding_category_utf_16_be);
ff0dacd7
KH
7457 if (detect_info.found & (CATEGORY_MASK_UTF_16_LE
7458 | CATEGORY_MASK_UTF_16_LE_NOSIG))
89528eb3
KH
7459 utf_16_le_eol = detect_eol (coding.source, src_bytes,
7460 coding_category_utf_16_le);
7461 }
7462 else
7463 {
7464 if (EQ (eol_type, Qunix))
7465 normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_LF;
7466 else if (EQ (eol_type, Qdos))
7467 normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CRLF;
7468 else
7469 normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CR;
7470 }
7471
df7492f9
KH
7472 for (tail = val; CONSP (tail); tail = XCDR (tail))
7473 {
89528eb3 7474 enum coding_category category;
df7492f9 7475 int this_eol;
89528eb3
KH
7476
7477 id = XINT (XCAR (tail));
7478 attrs = CODING_ID_ATTRS (id);
7479 category = XINT (CODING_ATTR_CATEGORY (attrs));
7480 eol_type = CODING_ID_EOL_TYPE (id);
df7492f9
KH
7481 if (VECTORP (eol_type))
7482 {
89528eb3
KH
7483 if (category == coding_category_utf_16_be
7484 || category == coding_category_utf_16_be_nosig)
7485 this_eol = utf_16_be_eol;
7486 else if (category == coding_category_utf_16_le
7487 || category == coding_category_utf_16_le_nosig)
7488 this_eol = utf_16_le_eol;
df7492f9 7489 else
89528eb3
KH
7490 this_eol = normal_eol;
7491
df7492f9
KH
7492 if (this_eol == EOL_SEEN_LF)
7493 XSETCAR (tail, AREF (eol_type, 0));
7494 else if (this_eol == EOL_SEEN_CRLF)
7495 XSETCAR (tail, AREF (eol_type, 1));
7496 else if (this_eol == EOL_SEEN_CR)
7497 XSETCAR (tail, AREF (eol_type, 2));
89528eb3
KH
7498 else
7499 XSETCAR (tail, CODING_ID_NAME (id));
df7492f9 7500 }
89528eb3
KH
7501 else
7502 XSETCAR (tail, CODING_ID_NAME (id));
df7492f9
KH
7503 }
7504 }
ec6d2bb8 7505
03699b14 7506 return (highest ? XCAR (val) : val);
ec6d2bb8
KH
7507}
7508
ec6d2bb8 7509
d46c5b12
KH
7510DEFUN ("detect-coding-region", Fdetect_coding_region, Sdetect_coding_region,
7511 2, 3, 0,
48b0f3ae
PJ
7512 doc: /* Detect coding system of the text in the region between START and END.
7513Return a list of possible coding systems ordered by priority.
ec6d2bb8 7514
12e0131a
KH
7515If only ASCII characters are found (except for such ISO-2022 control
7516characters ISO-2022 as ESC), it returns a list of single element
48b0f3ae
PJ
7517`undecided' or its subsidiary coding system according to a detected
7518end-of-line format.
ec6d2bb8 7519
48b0f3ae
PJ
7520If optional argument HIGHEST is non-nil, return the coding system of
7521highest priority. */)
7522 (start, end, highest)
d46c5b12
KH
7523 Lisp_Object start, end, highest;
7524{
7525 int from, to;
7526 int from_byte, to_byte;
ec6d2bb8 7527
b7826503
PJ
7528 CHECK_NUMBER_COERCE_MARKER (start);
7529 CHECK_NUMBER_COERCE_MARKER (end);
ec6d2bb8 7530
d46c5b12
KH
7531 validate_region (&start, &end);
7532 from = XINT (start), to = XINT (end);
7533 from_byte = CHAR_TO_BYTE (from);
7534 to_byte = CHAR_TO_BYTE (to);
ec6d2bb8 7535
d46c5b12
KH
7536 if (from < GPT && to >= GPT)
7537 move_gap_both (to, to_byte);
c210f766 7538
d46c5b12 7539 return detect_coding_system (BYTE_POS_ADDR (from_byte),
24a73b0a 7540 to - from, to_byte - from_byte,
0a28aafb
KH
7541 !NILP (highest),
7542 !NILP (current_buffer
df7492f9
KH
7543 ->enable_multibyte_characters),
7544 Qnil);
ec6d2bb8
KH
7545}
7546
d46c5b12
KH
7547DEFUN ("detect-coding-string", Fdetect_coding_string, Sdetect_coding_string,
7548 1, 2, 0,
48b0f3ae
PJ
7549 doc: /* Detect coding system of the text in STRING.
7550Return a list of possible coding systems ordered by priority.
fb88bf2d 7551
12e0131a
KH
7552If only ASCII characters are found (except for such ISO-2022 control
7553characters ISO-2022 as ESC), it returns a list of single element
48b0f3ae
PJ
7554`undecided' or its subsidiary coding system according to a detected
7555end-of-line format.
d46c5b12 7556
48b0f3ae
PJ
7557If optional argument HIGHEST is non-nil, return the coding system of
7558highest priority. */)
7559 (string, highest)
d46c5b12
KH
7560 Lisp_Object string, highest;
7561{
b7826503 7562 CHECK_STRING (string);
b73bfc1c 7563
24a73b0a
KH
7564 return detect_coding_system (SDATA (string),
7565 SCHARS (string), SBYTES (string),
8f924df7 7566 !NILP (highest), STRING_MULTIBYTE (string),
df7492f9 7567 Qnil);
4ed46869 7568}
4ed46869 7569
b73bfc1c 7570
df7492f9
KH
7571static INLINE int
7572char_encodable_p (c, attrs)
7573 int c;
7574 Lisp_Object attrs;
05e6f5dc 7575{
df7492f9 7576 Lisp_Object tail;
df7492f9 7577 struct charset *charset;
7d64c6ad 7578 Lisp_Object translation_table;
d46c5b12 7579
7d64c6ad 7580 translation_table = CODING_ATTR_TRANS_TBL (attrs);
a6f87d34 7581 if (! NILP (translation_table))
7d64c6ad 7582 c = translate_char (translation_table, c);
df7492f9
KH
7583 for (tail = CODING_ATTR_CHARSET_LIST (attrs);
7584 CONSP (tail); tail = XCDR (tail))
e133c8fa 7585 {
df7492f9
KH
7586 charset = CHARSET_FROM_ID (XINT (XCAR (tail)));
7587 if (CHAR_CHARSET_P (c, charset))
7588 break;
e133c8fa 7589 }
df7492f9 7590 return (! NILP (tail));
05e6f5dc 7591}
83fa074f 7592
fb88bf2d 7593
df7492f9
KH
7594/* Return a list of coding systems that safely encode the text between
7595 START and END. If EXCLUDE is non-nil, it is a list of coding
7596 systems not to check. The returned list doesn't contain any such
48468dac 7597 coding systems. In any case, if the text contains only ASCII or is
df7492f9 7598 unibyte, return t. */
e077cc80 7599
df7492f9
KH
7600DEFUN ("find-coding-systems-region-internal",
7601 Ffind_coding_systems_region_internal,
7602 Sfind_coding_systems_region_internal, 2, 3, 0,
7603 doc: /* Internal use only. */)
7604 (start, end, exclude)
7605 Lisp_Object start, end, exclude;
7606{
7607 Lisp_Object coding_attrs_list, safe_codings;
7608 EMACS_INT start_byte, end_byte;
7c78e542 7609 const unsigned char *p, *pbeg, *pend;
df7492f9
KH
7610 int c;
7611 Lisp_Object tail, elt;
d46c5b12 7612
df7492f9
KH
7613 if (STRINGP (start))
7614 {
7615 if (!STRING_MULTIBYTE (start)
8f924df7 7616 || SCHARS (start) == SBYTES (start))
df7492f9
KH
7617 return Qt;
7618 start_byte = 0;
8f924df7 7619 end_byte = SBYTES (start);
df7492f9
KH
7620 }
7621 else
d46c5b12 7622 {
df7492f9
KH
7623 CHECK_NUMBER_COERCE_MARKER (start);
7624 CHECK_NUMBER_COERCE_MARKER (end);
7625 if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end))
7626 args_out_of_range (start, end);
7627 if (NILP (current_buffer->enable_multibyte_characters))
7628 return Qt;
7629 start_byte = CHAR_TO_BYTE (XINT (start));
7630 end_byte = CHAR_TO_BYTE (XINT (end));
7631 if (XINT (end) - XINT (start) == end_byte - start_byte)
7632 return Qt;
d46c5b12 7633
e1c23804 7634 if (XINT (start) < GPT && XINT (end) > GPT)
d46c5b12 7635 {
e1c23804
DL
7636 if ((GPT - XINT (start)) < (XINT (end) - GPT))
7637 move_gap_both (XINT (start), start_byte);
df7492f9 7638 else
e1c23804 7639 move_gap_both (XINT (end), end_byte);
d46c5b12
KH
7640 }
7641 }
7642
df7492f9
KH
7643 coding_attrs_list = Qnil;
7644 for (tail = Vcoding_system_list; CONSP (tail); tail = XCDR (tail))
7645 if (NILP (exclude)
7646 || NILP (Fmemq (XCAR (tail), exclude)))
7647 {
7648 Lisp_Object attrs;
d46c5b12 7649
df7492f9
KH
7650 attrs = AREF (CODING_SYSTEM_SPEC (XCAR (tail)), 0);
7651 if (EQ (XCAR (tail), CODING_ATTR_BASE_NAME (attrs))
7652 && ! EQ (CODING_ATTR_TYPE (attrs), Qundecided))
7d64c6ad
KH
7653 {
7654 ASET (attrs, coding_attr_trans_tbl,
2170c8f0 7655 get_translation_table (attrs, 1, NULL));
7d64c6ad
KH
7656 coding_attrs_list = Fcons (attrs, coding_attrs_list);
7657 }
df7492f9 7658 }
d46c5b12 7659
df7492f9 7660 if (STRINGP (start))
8f924df7 7661 p = pbeg = SDATA (start);
df7492f9
KH
7662 else
7663 p = pbeg = BYTE_POS_ADDR (start_byte);
7664 pend = p + (end_byte - start_byte);
b843d1ae 7665
df7492f9
KH
7666 while (p < pend && ASCII_BYTE_P (*p)) p++;
7667 while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--;
d46c5b12 7668
05e6f5dc 7669 while (p < pend)
72d1a715 7670 {
df7492f9
KH
7671 if (ASCII_BYTE_P (*p))
7672 p++;
72d1a715
RS
7673 else
7674 {
df7492f9 7675 c = STRING_CHAR_ADVANCE (p);
12410ef1 7676
df7492f9
KH
7677 charset_map_loaded = 0;
7678 for (tail = coding_attrs_list; CONSP (tail);)
7679 {
7680 elt = XCAR (tail);
7681 if (NILP (elt))
7682 tail = XCDR (tail);
7683 else if (char_encodable_p (c, elt))
7684 tail = XCDR (tail);
7685 else if (CONSP (XCDR (tail)))
7686 {
7687 XSETCAR (tail, XCAR (XCDR (tail)));
7688 XSETCDR (tail, XCDR (XCDR (tail)));
7689 }
7690 else
7691 {
7692 XSETCAR (tail, Qnil);
7693 tail = XCDR (tail);
7694 }
7695 }
7696 if (charset_map_loaded)
7697 {
7698 EMACS_INT p_offset = p - pbeg, pend_offset = pend - pbeg;
05e6f5dc 7699
df7492f9 7700 if (STRINGP (start))
8f924df7 7701 pbeg = SDATA (start);
df7492f9
KH
7702 else
7703 pbeg = BYTE_POS_ADDR (start_byte);
7704 p = pbeg + p_offset;
7705 pend = pbeg + pend_offset;
7706 }
7707 }
ec6d2bb8 7708 }
fb88bf2d 7709
988b3759 7710 safe_codings = list2 (Qraw_text, Qno_conversion);
df7492f9
KH
7711 for (tail = coding_attrs_list; CONSP (tail); tail = XCDR (tail))
7712 if (! NILP (XCAR (tail)))
7713 safe_codings = Fcons (CODING_ATTR_BASE_NAME (XCAR (tail)), safe_codings);
ec6d2bb8 7714
05e6f5dc
KH
7715 return safe_codings;
7716}
4956c225 7717
d46c5b12 7718
8f924df7
KH
7719DEFUN ("unencodable-char-position", Funencodable_char_position,
7720 Sunencodable_char_position, 3, 5, 0,
7721 doc: /*
7722Return position of first un-encodable character in a region.
7723START and END specfiy the region and CODING-SYSTEM specifies the
7724encoding to check. Return nil if CODING-SYSTEM does encode the region.
d46c5b12 7725
8f924df7
KH
7726If optional 4th argument COUNT is non-nil, it specifies at most how
7727many un-encodable characters to search. In this case, the value is a
7728list of positions.
d46c5b12 7729
8f924df7
KH
7730If optional 5th argument STRING is non-nil, it is a string to search
7731for un-encodable characters. In that case, START and END are indexes
7732to the string. */)
7733 (start, end, coding_system, count, string)
7734 Lisp_Object start, end, coding_system, count, string;
7735{
7736 int n;
7737 struct coding_system coding;
7d64c6ad 7738 Lisp_Object attrs, charset_list, translation_table;
8f924df7
KH
7739 Lisp_Object positions;
7740 int from, to;
7741 const unsigned char *p, *stop, *pend;
7742 int ascii_compatible;
fb88bf2d 7743
8f924df7
KH
7744 setup_coding_system (Fcheck_coding_system (coding_system), &coding);
7745 attrs = CODING_ID_ATTRS (coding.id);
7746 if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text))
7747 return Qnil;
7748 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
7749 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
2170c8f0 7750 translation_table = get_translation_table (attrs, 1, NULL);
fb88bf2d 7751
8f924df7
KH
7752 if (NILP (string))
7753 {
7754 validate_region (&start, &end);
7755 from = XINT (start);
7756 to = XINT (end);
7757 if (NILP (current_buffer->enable_multibyte_characters)
7758 || (ascii_compatible
7759 && (to - from) == (CHAR_TO_BYTE (to) - (CHAR_TO_BYTE (from)))))
7760 return Qnil;
7761 p = CHAR_POS_ADDR (from);
7762 pend = CHAR_POS_ADDR (to);
7763 if (from < GPT && to >= GPT)
7764 stop = GPT_ADDR;
7765 else
7766 stop = pend;
7767 }
7768 else
7769 {
7770 CHECK_STRING (string);
7771 CHECK_NATNUM (start);
7772 CHECK_NATNUM (end);
7773 from = XINT (start);
7774 to = XINT (end);
7775 if (from > to
7776 || to > SCHARS (string))
7777 args_out_of_range_3 (string, start, end);
7778 if (! STRING_MULTIBYTE (string))
7779 return Qnil;
7780 p = SDATA (string) + string_char_to_byte (string, from);
7781 stop = pend = SDATA (string) + string_char_to_byte (string, to);
7782 if (ascii_compatible && (to - from) == (pend - p))
7783 return Qnil;
7784 }
f2558efd 7785
8f924df7
KH
7786 if (NILP (count))
7787 n = 1;
7788 else
b73bfc1c 7789 {
8f924df7
KH
7790 CHECK_NATNUM (count);
7791 n = XINT (count);
b73bfc1c
KH
7792 }
7793
8f924df7
KH
7794 positions = Qnil;
7795 while (1)
d46c5b12 7796 {
8f924df7 7797 int c;
ec6d2bb8 7798
8f924df7
KH
7799 if (ascii_compatible)
7800 while (p < stop && ASCII_BYTE_P (*p))
7801 p++, from++;
7802 if (p >= stop)
0e79d667 7803 {
8f924df7
KH
7804 if (p >= pend)
7805 break;
7806 stop = pend;
7807 p = GAP_END_ADDR;
0e79d667 7808 }
ec6d2bb8 7809
8f924df7
KH
7810 c = STRING_CHAR_ADVANCE (p);
7811 if (! (ASCII_CHAR_P (c) && ascii_compatible)
7d64c6ad
KH
7812 && ! char_charset (translate_char (translation_table, c),
7813 charset_list, NULL))
ec6d2bb8 7814 {
8f924df7
KH
7815 positions = Fcons (make_number (from), positions);
7816 n--;
7817 if (n == 0)
7818 break;
ec6d2bb8
KH
7819 }
7820
8f924df7
KH
7821 from++;
7822 }
d46c5b12 7823
8f924df7
KH
7824 return (NILP (count) ? Fcar (positions) : Fnreverse (positions));
7825}
d46c5b12 7826
d46c5b12 7827
df7492f9
KH
7828DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region,
7829 Scheck_coding_systems_region, 3, 3, 0,
7830 doc: /* Check if the region is encodable by coding systems.
d46c5b12 7831
df7492f9
KH
7832START and END are buffer positions specifying the region.
7833CODING-SYSTEM-LIST is a list of coding systems to check.
d46c5b12 7834
df7492f9
KH
7835The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
7836CODING-SYSTEM is a member of CODING-SYSTEM-LIst and can't encode the
7837whole region, POS0, POS1, ... are buffer positions where non-encodable
7838characters are found.
93dec019 7839
df7492f9
KH
7840If all coding systems in CODING-SYSTEM-LIST can encode the region, the
7841value is nil.
93dec019 7842
df7492f9
KH
7843START may be a string. In that case, check if the string is
7844encodable, and the value contains indices to the string instead of
7845buffer positions. END is ignored. */)
7846 (start, end, coding_system_list)
7847 Lisp_Object start, end, coding_system_list;
05e6f5dc 7848{
df7492f9
KH
7849 Lisp_Object list;
7850 EMACS_INT start_byte, end_byte;
7851 int pos;
7c78e542 7852 const unsigned char *p, *pbeg, *pend;
df7492f9 7853 int c;
7d64c6ad 7854 Lisp_Object tail, elt, attrs;
70ad9fc4 7855
05e6f5dc
KH
7856 if (STRINGP (start))
7857 {
df7492f9 7858 if (!STRING_MULTIBYTE (start)
8f924df7 7859 && SCHARS (start) != SBYTES (start))
df7492f9
KH
7860 return Qnil;
7861 start_byte = 0;
8f924df7 7862 end_byte = SBYTES (start);
df7492f9 7863 pos = 0;
d46c5b12 7864 }
05e6f5dc 7865 else
b73bfc1c 7866 {
b7826503
PJ
7867 CHECK_NUMBER_COERCE_MARKER (start);
7868 CHECK_NUMBER_COERCE_MARKER (end);
05e6f5dc
KH
7869 if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end))
7870 args_out_of_range (start, end);
7871 if (NILP (current_buffer->enable_multibyte_characters))
df7492f9
KH
7872 return Qnil;
7873 start_byte = CHAR_TO_BYTE (XINT (start));
7874 end_byte = CHAR_TO_BYTE (XINT (end));
7875 if (XINT (end) - XINT (start) == end_byte - start_byte)
05e6f5dc 7876 return Qt;
df7492f9 7877
e1c23804 7878 if (XINT (start) < GPT && XINT (end) > GPT)
b73bfc1c 7879 {
e1c23804
DL
7880 if ((GPT - XINT (start)) < (XINT (end) - GPT))
7881 move_gap_both (XINT (start), start_byte);
df7492f9 7882 else
e1c23804 7883 move_gap_both (XINT (end), end_byte);
b73bfc1c 7884 }
e1c23804 7885 pos = XINT (start);
b73bfc1c 7886 }
7553d0e1 7887
df7492f9
KH
7888 list = Qnil;
7889 for (tail = coding_system_list; CONSP (tail); tail = XCDR (tail))
12410ef1 7890 {
df7492f9 7891 elt = XCAR (tail);
7d64c6ad 7892 attrs = AREF (CODING_SYSTEM_SPEC (elt), 0);
2170c8f0
KH
7893 ASET (attrs, coding_attr_trans_tbl,
7894 get_translation_table (attrs, 1, NULL));
7d64c6ad 7895 list = Fcons (Fcons (elt, Fcons (attrs, Qnil)), list);
12410ef1
KH
7896 }
7897
df7492f9 7898 if (STRINGP (start))
8f924df7 7899 p = pbeg = SDATA (start);
72d1a715 7900 else
df7492f9
KH
7901 p = pbeg = BYTE_POS_ADDR (start_byte);
7902 pend = p + (end_byte - start_byte);
4ed46869 7903
df7492f9
KH
7904 while (p < pend && ASCII_BYTE_P (*p)) p++, pos++;
7905 while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--;
ec6d2bb8 7906
df7492f9 7907 while (p < pend)
d46c5b12 7908 {
df7492f9
KH
7909 if (ASCII_BYTE_P (*p))
7910 p++;
e133c8fa 7911 else
05e6f5dc 7912 {
df7492f9
KH
7913 c = STRING_CHAR_ADVANCE (p);
7914
7915 charset_map_loaded = 0;
7916 for (tail = list; CONSP (tail); tail = XCDR (tail))
7917 {
7918 elt = XCDR (XCAR (tail));
7919 if (! char_encodable_p (c, XCAR (elt)))
7920 XSETCDR (elt, Fcons (make_number (pos), XCDR (elt)));
7921 }
7922 if (charset_map_loaded)
7923 {
7924 EMACS_INT p_offset = p - pbeg, pend_offset = pend - pbeg;
7925
7926 if (STRINGP (start))
8f924df7 7927 pbeg = SDATA (start);
df7492f9
KH
7928 else
7929 pbeg = BYTE_POS_ADDR (start_byte);
7930 p = pbeg + p_offset;
7931 pend = pbeg + pend_offset;
7932 }
05e6f5dc 7933 }
df7492f9 7934 pos++;
d46c5b12 7935 }
4ed46869 7936
df7492f9
KH
7937 tail = list;
7938 list = Qnil;
7939 for (; CONSP (tail); tail = XCDR (tail))
ec6d2bb8 7940 {
df7492f9
KH
7941 elt = XCAR (tail);
7942 if (CONSP (XCDR (XCDR (elt))))
7943 list = Fcons (Fcons (XCAR (elt), Fnreverse (XCDR (XCDR (elt)))),
7944 list);
ec6d2bb8 7945 }
2b4f9037 7946
df7492f9 7947 return list;
d46c5b12
KH
7948}
7949
3fd9494b 7950
b73bfc1c 7951Lisp_Object
df7492f9
KH
7952code_convert_region (start, end, coding_system, dst_object, encodep, norecord)
7953 Lisp_Object start, end, coding_system, dst_object;
7954 int encodep, norecord;
4ed46869 7955{
3a73fa5d 7956 struct coding_system coding;
df7492f9
KH
7957 EMACS_INT from, from_byte, to, to_byte;
7958 Lisp_Object src_object;
4ed46869 7959
b7826503
PJ
7960 CHECK_NUMBER_COERCE_MARKER (start);
7961 CHECK_NUMBER_COERCE_MARKER (end);
df7492f9
KH
7962 if (NILP (coding_system))
7963 coding_system = Qno_conversion;
7964 else
7965 CHECK_CODING_SYSTEM (coding_system);
7966 src_object = Fcurrent_buffer ();
7967 if (NILP (dst_object))
7968 dst_object = src_object;
7969 else if (! EQ (dst_object, Qt))
7970 CHECK_BUFFER (dst_object);
3a73fa5d 7971
d46c5b12
KH
7972 validate_region (&start, &end);
7973 from = XFASTINT (start);
df7492f9 7974 from_byte = CHAR_TO_BYTE (from);
d46c5b12 7975 to = XFASTINT (end);
df7492f9 7976 to_byte = CHAR_TO_BYTE (to);
764ca8da 7977
df7492f9
KH
7978 setup_coding_system (coding_system, &coding);
7979 coding.mode |= CODING_MODE_LAST_BLOCK;
ec6d2bb8 7980
df7492f9
KH
7981 if (encodep)
7982 encode_coding_object (&coding, src_object, from, from_byte, to, to_byte,
7983 dst_object);
7984 else
7985 decode_coding_object (&coding, src_object, from, from_byte, to, to_byte,
7986 dst_object);
7987 if (! norecord)
7988 Vlast_coding_system_used = CODING_ID_NAME (coding.id);
ec6d2bb8 7989
df7492f9
KH
7990 return (BUFFERP (dst_object)
7991 ? make_number (coding.produced_char)
7992 : coding.dst_object);
4031e2bf 7993}
78108bcd 7994
4ed46869 7995
4031e2bf 7996DEFUN ("decode-coding-region", Fdecode_coding_region, Sdecode_coding_region,
df7492f9 7997 3, 4, "r\nzCoding system: ",
48b0f3ae 7998 doc: /* Decode the current region from the specified coding system.
df7492f9
KH
7999When called from a program, takes four arguments:
8000 START, END, CODING-SYSTEM, and DESTINATION.
8001START and END are buffer positions.
8844fa83 8002
df7492f9
KH
8003Optional 4th arguments DESTINATION specifies where the decoded text goes.
8004If nil, the region between START and END is replace by the decoded text.
8005If buffer, the decoded text is inserted in the buffer.
8006If t, the decoded text is returned.
8844fa83 8007
48b0f3ae
PJ
8008This function sets `last-coding-system-used' to the precise coding system
8009used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8010not fully specified.)
8011It returns the length of the decoded text. */)
df7492f9
KH
8012 (start, end, coding_system, destination)
8013 Lisp_Object start, end, coding_system, destination;
4031e2bf 8014{
df7492f9 8015 return code_convert_region (start, end, coding_system, destination, 0, 0);
3a73fa5d 8016}
8844fa83 8017
3a73fa5d 8018DEFUN ("encode-coding-region", Fencode_coding_region, Sencode_coding_region,
df7492f9
KH
8019 3, 4, "r\nzCoding system: ",
8020 doc: /* Encode the current region by specified coding system.
48b0f3ae
PJ
8021When called from a program, takes three arguments:
8022START, END, and CODING-SYSTEM. START and END are buffer positions.
d46c5b12 8023
df7492f9
KH
8024Optional 4th arguments DESTINATION specifies where the encoded text goes.
8025If nil, the region between START and END is replace by the encoded text.
8026If buffer, the encoded text is inserted in the buffer.
8027If t, the encoded text is returned.
2391eaa4 8028
48b0f3ae
PJ
8029This function sets `last-coding-system-used' to the precise coding system
8030used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8031not fully specified.)
8032It returns the length of the encoded text. */)
df7492f9
KH
8033 (start, end, coding_system, destination)
8034 Lisp_Object start, end, coding_system, destination;
3a73fa5d 8035{
df7492f9 8036 return code_convert_region (start, end, coding_system, destination, 1, 0);
b73bfc1c
KH
8037}
8038
8039Lisp_Object
df7492f9
KH
8040code_convert_string (string, coding_system, dst_object,
8041 encodep, nocopy, norecord)
8042 Lisp_Object string, coding_system, dst_object;
8043 int encodep, nocopy, norecord;
b73bfc1c 8044{
4031e2bf 8045 struct coding_system coding;
df7492f9 8046 EMACS_INT chars, bytes;
ec6d2bb8 8047
b7826503 8048 CHECK_STRING (string);
d46c5b12 8049 if (NILP (coding_system))
4956c225 8050 {
df7492f9
KH
8051 if (! norecord)
8052 Vlast_coding_system_used = Qno_conversion;
8053 if (NILP (dst_object))
8054 return (nocopy ? Fcopy_sequence (string) : string);
4956c225 8055 }
b73bfc1c 8056
df7492f9
KH
8057 if (NILP (coding_system))
8058 coding_system = Qno_conversion;
8059 else
8060 CHECK_CODING_SYSTEM (coding_system);
8061 if (NILP (dst_object))
8062 dst_object = Qt;
8063 else if (! EQ (dst_object, Qt))
8064 CHECK_BUFFER (dst_object);
73be902c 8065
df7492f9 8066 setup_coding_system (coding_system, &coding);
d46c5b12 8067 coding.mode |= CODING_MODE_LAST_BLOCK;
8f924df7
KH
8068 chars = SCHARS (string);
8069 bytes = SBYTES (string);
df7492f9
KH
8070 if (encodep)
8071 encode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object);
8072 else
8073 decode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object);
8074 if (! norecord)
8075 Vlast_coding_system_used = CODING_ID_NAME (coding.id);
73be902c 8076
df7492f9
KH
8077 return (BUFFERP (dst_object)
8078 ? make_number (coding.produced_char)
8079 : coding.dst_object);
4ed46869 8080}
73be902c 8081
b73bfc1c 8082
ecec61c1 8083/* Encode or decode STRING according to CODING_SYSTEM.
ec6d2bb8 8084 Do not set Vlast_coding_system_used.
4ed46869 8085
ec6d2bb8
KH
8086 This function is called only from macros DECODE_FILE and
8087 ENCODE_FILE, thus we ignore character composition. */
4ed46869 8088
ecec61c1
KH
8089Lisp_Object
8090code_convert_string_norecord (string, coding_system, encodep)
8091 Lisp_Object string, coding_system;
8092 int encodep;
4ed46869 8093{
0be8721c 8094 return code_convert_string (string, coding_system, Qt, encodep, 0, 1);
4ed46869
KH
8095}
8096
4ed46869 8097
df7492f9
KH
8098DEFUN ("decode-coding-string", Fdecode_coding_string, Sdecode_coding_string,
8099 2, 4, 0,
8100 doc: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
8101
8102Optional third arg NOCOPY non-nil means it is OK to return STRING itself
8103if the decoding operation is trivial.
ecec61c1 8104
df7492f9 8105Optional fourth arg BUFFER non-nil meant that the decoded text is
a3f6ee6d 8106inserted in BUFFER instead of returned as a string. In this case,
df7492f9 8107the return value is BUFFER.
ecec61c1 8108
df7492f9
KH
8109This function sets `last-coding-system-used' to the precise coding system
8110used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8111not fully specified. */)
8112 (string, coding_system, nocopy, buffer)
8113 Lisp_Object string, coding_system, nocopy, buffer;
4ed46869 8114{
df7492f9
KH
8115 return code_convert_string (string, coding_system, buffer,
8116 0, ! NILP (nocopy), 0);
4ed46869
KH
8117}
8118
df7492f9
KH
8119DEFUN ("encode-coding-string", Fencode_coding_string, Sencode_coding_string,
8120 2, 4, 0,
8121 doc: /* Encode STRING to CODING-SYSTEM, and return the result.
8122
8123Optional third arg NOCOPY non-nil means it is OK to return STRING
8124itself if the encoding operation is trivial.
8125
8126Optional fourth arg BUFFER non-nil meant that the encoded text is
a3f6ee6d 8127inserted in BUFFER instead of returned as a string. In this case,
df7492f9
KH
8128the return value is BUFFER.
8129
8130This function sets `last-coding-system-used' to the precise coding system
8131used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8132not fully specified.) */)
8133 (string, coding_system, nocopy, buffer)
8134 Lisp_Object string, coding_system, nocopy, buffer;
4ed46869 8135{
df7492f9 8136 return code_convert_string (string, coding_system, buffer,
c197f191 8137 1, ! NILP (nocopy), 1);
4ed46869 8138}
df7492f9 8139
3a73fa5d 8140\f
4ed46869 8141DEFUN ("decode-sjis-char", Fdecode_sjis_char, Sdecode_sjis_char, 1, 1, 0,
48b0f3ae
PJ
8142 doc: /* Decode a Japanese character which has CODE in shift_jis encoding.
8143Return the corresponding character. */)
8144 (code)
4ed46869 8145 Lisp_Object code;
4ed46869 8146{
df7492f9
KH
8147 Lisp_Object spec, attrs, val;
8148 struct charset *charset_roman, *charset_kanji, *charset_kana, *charset;
8149 int c;
4ed46869 8150
df7492f9
KH
8151 CHECK_NATNUM (code);
8152 c = XFASTINT (code);
8153 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec);
8154 attrs = AREF (spec, 0);
4ed46869 8155
df7492f9
KH
8156 if (ASCII_BYTE_P (c)
8157 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
8158 return code;
4ed46869 8159
df7492f9
KH
8160 val = CODING_ATTR_CHARSET_LIST (attrs);
8161 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
004068e4
KH
8162 charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
8163 charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val)));
fa42c37f 8164
df7492f9
KH
8165 if (c <= 0x7F)
8166 charset = charset_roman;
8167 else if (c >= 0xA0 && c < 0xDF)
55ab7be3 8168 {
df7492f9
KH
8169 charset = charset_kana;
8170 c -= 0x80;
4ed46869 8171 }
55ab7be3 8172 else
4ed46869 8173 {
004068e4 8174 int s1 = c >> 8, s2 = c & 0xFF;
df7492f9
KH
8175
8176 if (s1 < 0x81 || (s1 > 0x9F && s1 < 0xE0) || s1 > 0xEF
8177 || s2 < 0x40 || s2 == 0x7F || s2 > 0xFC)
8178 error ("Invalid code: %d", code);
8179 SJIS_TO_JIS (c);
8180 charset = charset_kanji;
4ed46869 8181 }
df7492f9
KH
8182 c = DECODE_CHAR (charset, c);
8183 if (c < 0)
8184 error ("Invalid code: %d", code);
8185 return make_number (c);
93dec019 8186}
4ed46869 8187
48b0f3ae 8188
4ed46869 8189DEFUN ("encode-sjis-char", Fencode_sjis_char, Sencode_sjis_char, 1, 1, 0,
8acf0c0e 8190 doc: /* Encode a Japanese character CH to shift_jis encoding.
48b0f3ae
PJ
8191Return the corresponding code in SJIS. */)
8192 (ch)
df7492f9 8193 Lisp_Object ch;
4ed46869 8194{
df7492f9
KH
8195 Lisp_Object spec, attrs, charset_list;
8196 int c;
8197 struct charset *charset;
8198 unsigned code;
48b0f3ae 8199
df7492f9
KH
8200 CHECK_CHARACTER (ch);
8201 c = XFASTINT (ch);
8202 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec);
8203 attrs = AREF (spec, 0);
8204
8205 if (ASCII_CHAR_P (c)
8206 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
8207 return ch;
8208
8209 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
8210 charset = char_charset (c, charset_list, &code);
8211 if (code == CHARSET_INVALID_CODE (charset))
8212 error ("Can't encode by shift_jis encoding: %d", c);
8213 JIS_TO_SJIS (code);
8214
8215 return make_number (code);
4ed46869
KH
8216}
8217
8218DEFUN ("decode-big5-char", Fdecode_big5_char, Sdecode_big5_char, 1, 1, 0,
48b0f3ae
PJ
8219 doc: /* Decode a Big5 character which has CODE in BIG5 coding system.
8220Return the corresponding character. */)
8221 (code)
4ed46869 8222 Lisp_Object code;
d46c5b12 8223{
df7492f9
KH
8224 Lisp_Object spec, attrs, val;
8225 struct charset *charset_roman, *charset_big5, *charset;
8226 int c;
6289dd10 8227
df7492f9
KH
8228 CHECK_NATNUM (code);
8229 c = XFASTINT (code);
8230 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec);
8231 attrs = AREF (spec, 0);
4ed46869 8232
df7492f9
KH
8233 if (ASCII_BYTE_P (c)
8234 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
8235 return code;
6289dd10 8236
df7492f9
KH
8237 val = CODING_ATTR_CHARSET_LIST (attrs);
8238 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
8239 charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val)));
c210f766 8240
df7492f9
KH
8241 if (c <= 0x7F)
8242 charset = charset_roman;
c28a9453
KH
8243 else
8244 {
df7492f9
KH
8245 int b1 = c >> 8, b2 = c & 0x7F;
8246 if (b1 < 0xA1 || b1 > 0xFE
8247 || b2 < 0x40 || (b2 > 0x7E && b2 < 0xA1) || b2 > 0xFE)
8248 error ("Invalid code: %d", code);
8249 charset = charset_big5;
c28a9453 8250 }
df7492f9
KH
8251 c = DECODE_CHAR (charset, (unsigned )c);
8252 if (c < 0)
8253 error ("Invalid code: %d", code);
8254 return make_number (c);
d46c5b12 8255}
6289dd10 8256
4ed46869 8257DEFUN ("encode-big5-char", Fencode_big5_char, Sencode_big5_char, 1, 1, 0,
8acf0c0e 8258 doc: /* Encode the Big5 character CH to BIG5 coding system.
48b0f3ae
PJ
8259Return the corresponding character code in Big5. */)
8260 (ch)
4ed46869
KH
8261 Lisp_Object ch;
8262{
df7492f9
KH
8263 Lisp_Object spec, attrs, charset_list;
8264 struct charset *charset;
8265 int c;
8266 unsigned code;
8267
8268 CHECK_CHARACTER (ch);
8269 c = XFASTINT (ch);
8270 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec);
8271 attrs = AREF (spec, 0);
8272 if (ASCII_CHAR_P (c)
8273 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
8274 return ch;
8275
8276 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
8277 charset = char_charset (c, charset_list, &code);
8278 if (code == CHARSET_INVALID_CODE (charset))
8279 error ("Can't encode by Big5 encoding: %d", c);
8280
8281 return make_number (code);
4ed46869 8282}
48b0f3ae 8283
3a73fa5d 8284\f
002fdb44 8285DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal,
68bba4e4 8286 Sset_terminal_coding_system_internal, 1, 2, 0,
48b0f3ae 8287 doc: /* Internal use only. */)
6ed8eeff 8288 (coding_system, terminal)
b74e4686 8289 Lisp_Object coding_system;
6ed8eeff 8290 Lisp_Object terminal;
4ed46869 8291{
6ed8eeff 8292 struct coding_system *terminal_coding = TERMINAL_TERMINAL_CODING (get_terminal (terminal, 1));
b7826503 8293 CHECK_SYMBOL (coding_system);
b8299c66 8294 setup_coding_system (Fcheck_coding_system (coding_system), terminal_coding);
70c22245 8295 /* We had better not send unsafe characters to terminal. */
c73bd236 8296 terminal_coding->mode |= CODING_MODE_SAFE_ENCODING;
df7492f9 8297 /* Characer composition should be disabled. */
c73bd236 8298 terminal_coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
b8299c66
KL
8299 terminal_coding->src_multibyte = 1;
8300 terminal_coding->dst_multibyte = 0;
4ed46869
KH
8301 return Qnil;
8302}
8303
c4825358
KH
8304DEFUN ("set-safe-terminal-coding-system-internal",
8305 Fset_safe_terminal_coding_system_internal,
48b0f3ae 8306 Sset_safe_terminal_coding_system_internal, 1, 1, 0,
ddb67bdc 8307 doc: /* Internal use only. */)
48b0f3ae 8308 (coding_system)
b74e4686 8309 Lisp_Object coding_system;
d46c5b12 8310{
b7826503 8311 CHECK_SYMBOL (coding_system);
c4825358
KH
8312 setup_coding_system (Fcheck_coding_system (coding_system),
8313 &safe_terminal_coding);
df7492f9
KH
8314 /* Characer composition should be disabled. */
8315 safe_terminal_coding.common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
b73bfc1c
KH
8316 safe_terminal_coding.src_multibyte = 1;
8317 safe_terminal_coding.dst_multibyte = 0;
c4825358
KH
8318 return Qnil;
8319}
4ed46869 8320
002fdb44 8321DEFUN ("terminal-coding-system", Fterminal_coding_system,
68bba4e4 8322 Sterminal_coding_system, 0, 1, 0,
6ed8eeff
KL
8323 doc: /* Return coding system specified for terminal output on the given terminal.
8324TERMINAL may be a terminal id, a frame, or nil for the selected
8325frame's terminal device. */)
8326 (terminal)
8327 Lisp_Object terminal;
4ed46869 8328{
985773c9
MB
8329 struct coding_system *terminal_coding
8330 = TERMINAL_TERMINAL_CODING (get_terminal (terminal, 1));
8331 Lisp_Object coding_system = CODING_ID_NAME (terminal_coding->id);
ae6f73fa 8332
ae6f73fa 8333 /* For backward compatibility, return nil if it is `undecided'. */
f75c90a9 8334 return (! EQ (coding_system, Qundecided) ? coding_system : Qnil);
4ed46869
KH
8335}
8336
002fdb44 8337DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal,
68bba4e4 8338 Sset_keyboard_coding_system_internal, 1, 2, 0,
48b0f3ae 8339 doc: /* Internal use only. */)
6ed8eeff 8340 (coding_system, terminal)
4ed46869 8341 Lisp_Object coding_system;
6ed8eeff 8342 Lisp_Object terminal;
4ed46869 8343{
6ed8eeff 8344 struct terminal *t = get_terminal (terminal, 1);
b7826503 8345 CHECK_SYMBOL (coding_system);
df7492f9 8346 setup_coding_system (Fcheck_coding_system (coding_system),
c73bd236 8347 TERMINAL_KEYBOARD_CODING (t));
df7492f9 8348 /* Characer composition should be disabled. */
c73bd236
MB
8349 TERMINAL_KEYBOARD_CODING (t)->common_flags
8350 &= ~CODING_ANNOTATE_COMPOSITION_MASK;
4ed46869
KH
8351 return Qnil;
8352}
8353
8354DEFUN ("keyboard-coding-system",
985773c9 8355 Fkeyboard_coding_system, Skeyboard_coding_system, 0, 1, 0,
48b0f3ae 8356 doc: /* Return coding system specified for decoding keyboard input. */)
985773c9
MB
8357 (terminal)
8358 Lisp_Object terminal;
4ed46869 8359{
985773c9
MB
8360 return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING
8361 (get_terminal (terminal, 1))->id);
4ed46869
KH
8362}
8363
4ed46869 8364\f
a5d301df
KH
8365DEFUN ("find-operation-coding-system", Ffind_operation_coding_system,
8366 Sfind_operation_coding_system, 1, MANY, 0,
48b0f3ae
PJ
8367 doc: /* Choose a coding system for an operation based on the target name.
8368The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
8369DECODING-SYSTEM is the coding system to use for decoding
8370\(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
8371for encoding (in case OPERATION does encoding).
05e6f5dc 8372
48b0f3ae
PJ
8373The first argument OPERATION specifies an I/O primitive:
8374 For file I/O, `insert-file-contents' or `write-region'.
8375 For process I/O, `call-process', `call-process-region', or `start-process'.
8376 For network I/O, `open-network-stream'.
05e6f5dc 8377
48b0f3ae
PJ
8378The remaining arguments should be the same arguments that were passed
8379to the primitive. Depending on which primitive, one of those arguments
8380is selected as the TARGET. For example, if OPERATION does file I/O,
8381whichever argument specifies the file name is TARGET.
05e6f5dc 8382
48b0f3ae 8383TARGET has a meaning which depends on OPERATION:
b883cdb2 8384 For file I/O, TARGET is a file name (except for the special case below).
48b0f3ae
PJ
8385 For process I/O, TARGET is a process name.
8386 For network I/O, TARGET is a service name or a port number
05e6f5dc 8387
48b0f3ae
PJ
8388This function looks up what specified for TARGET in,
8389`file-coding-system-alist', `process-coding-system-alist',
8390or `network-coding-system-alist' depending on OPERATION.
8391They may specify a coding system, a cons of coding systems,
8392or a function symbol to call.
8393In the last case, we call the function with one argument,
8394which is a list of all the arguments given to this function.
1011c487
MB
8395If the function can't decide a coding system, it can return
8396`undecided' so that the normal code-detection is performed.
48b0f3ae 8397
b883cdb2
MB
8398If OPERATION is `insert-file-contents', the argument corresponding to
8399TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a
8400file name to look up, and BUFFER is a buffer that contains the file's
8401contents (not yet decoded). If `file-coding-system-alist' specifies a
8402function to call for FILENAME, that function should examine the
8403contents of BUFFER instead of reading the file.
8404
d918f936 8405usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
48b0f3ae 8406 (nargs, args)
4ed46869
KH
8407 int nargs;
8408 Lisp_Object *args;
6b89e3aa 8409{
4ed46869
KH
8410 Lisp_Object operation, target_idx, target, val;
8411 register Lisp_Object chain;
177c0ea7 8412
4ed46869
KH
8413 if (nargs < 2)
8414 error ("Too few arguments");
8415 operation = args[0];
8416 if (!SYMBOLP (operation)
8417 || !INTEGERP (target_idx = Fget (operation, Qtarget_idx)))
df7492f9 8418 error ("Invalid first arguement");
4ed46869
KH
8419 if (nargs < 1 + XINT (target_idx))
8420 error ("Too few arguments for operation: %s",
8f924df7 8421 SDATA (SYMBOL_NAME (operation)));
4ed46869
KH
8422 target = args[XINT (target_idx) + 1];
8423 if (!(STRINGP (target)
091a0ff0
KH
8424 || (EQ (operation, Qinsert_file_contents) && CONSP (target)
8425 && STRINGP (XCAR (target)) && BUFFERP (XCDR (target)))
4ed46869 8426 || (EQ (operation, Qopen_network_stream) && INTEGERP (target))))
df7492f9 8427 error ("Invalid %dth argument", XINT (target_idx) + 1);
091a0ff0
KH
8428 if (CONSP (target))
8429 target = XCAR (target);
4ed46869 8430
2e34157c
RS
8431 chain = ((EQ (operation, Qinsert_file_contents)
8432 || EQ (operation, Qwrite_region))
02ba4723 8433 ? Vfile_coding_system_alist
2e34157c 8434 : (EQ (operation, Qopen_network_stream)
02ba4723
KH
8435 ? Vnetwork_coding_system_alist
8436 : Vprocess_coding_system_alist));
4ed46869
KH
8437 if (NILP (chain))
8438 return Qnil;
8439
03699b14 8440 for (; CONSP (chain); chain = XCDR (chain))
6b89e3aa 8441 {
f44d27ce 8442 Lisp_Object elt;
6b89e3aa 8443
df7492f9 8444 elt = XCAR (chain);
4ed46869
KH
8445 if (CONSP (elt)
8446 && ((STRINGP (target)
03699b14
KR
8447 && STRINGP (XCAR (elt))
8448 && fast_string_match (XCAR (elt), target) >= 0)
8449 || (INTEGERP (target) && EQ (target, XCAR (elt)))))
6b89e3aa 8450 {
03699b14 8451 val = XCDR (elt);
b19fd4c5
KH
8452 /* Here, if VAL is both a valid coding system and a valid
8453 function symbol, we return VAL as a coding system. */
02ba4723
KH
8454 if (CONSP (val))
8455 return val;
8456 if (! SYMBOLP (val))
8457 return Qnil;
8458 if (! NILP (Fcoding_system_p (val)))
8459 return Fcons (val, val);
b19fd4c5 8460 if (! NILP (Ffboundp (val)))
6b89e3aa 8461 {
e2b97060
MB
8462 /* We use call1 rather than safe_call1
8463 so as to get bug reports about functions called here
8464 which don't handle the current interface. */
8465 val = call1 (val, Flist (nargs, args));
b19fd4c5
KH
8466 if (CONSP (val))
8467 return val;
8468 if (SYMBOLP (val) && ! NILP (Fcoding_system_p (val)))
8469 return Fcons (val, val);
6b89e3aa 8470 }
02ba4723 8471 return Qnil;
6b89e3aa
KH
8472 }
8473 }
4ed46869 8474 return Qnil;
6b89e3aa
KH
8475}
8476
df7492f9 8477DEFUN ("set-coding-system-priority", Fset_coding_system_priority,
a3f6ee6d 8478 Sset_coding_system_priority, 0, MANY, 0,
da7db224 8479 doc: /* Assign higher priority to the coding systems given as arguments.
ff563fce 8480If multiple coding systems belongs to the same category,
a3181084
DL
8481all but the first one are ignored.
8482
8483usage: (set-coding-system-priority ...) */)
df7492f9
KH
8484 (nargs, args)
8485 int nargs;
8486 Lisp_Object *args;
8487{
8488 int i, j;
8489 int changed[coding_category_max];
8490 enum coding_category priorities[coding_category_max];
8491
8492 bzero (changed, sizeof changed);
6b89e3aa 8493
df7492f9 8494 for (i = j = 0; i < nargs; i++)
6b89e3aa 8495 {
df7492f9
KH
8496 enum coding_category category;
8497 Lisp_Object spec, attrs;
6b89e3aa 8498
df7492f9
KH
8499 CHECK_CODING_SYSTEM_GET_SPEC (args[i], spec);
8500 attrs = AREF (spec, 0);
8501 category = XINT (CODING_ATTR_CATEGORY (attrs));
8502 if (changed[category])
8503 /* Ignore this coding system because a coding system of the
8504 same category already had a higher priority. */
8505 continue;
8506 changed[category] = 1;
8507 priorities[j++] = category;
8508 if (coding_categories[category].id >= 0
8509 && ! EQ (args[i], CODING_ID_NAME (coding_categories[category].id)))
8510 setup_coding_system (args[i], &coding_categories[category]);
ff563fce 8511 Fset (AREF (Vcoding_category_table, category), args[i]);
df7492f9 8512 }
6b89e3aa 8513
df7492f9
KH
8514 /* Now we have decided top J priorities. Reflect the order of the
8515 original priorities to the remaining priorities. */
6b89e3aa 8516
df7492f9 8517 for (i = j, j = 0; i < coding_category_max; i++, j++)
6b89e3aa 8518 {
df7492f9
KH
8519 while (j < coding_category_max
8520 && changed[coding_priorities[j]])
8521 j++;
8522 if (j == coding_category_max)
8523 abort ();
8524 priorities[i] = coding_priorities[j];
8525 }
6b89e3aa 8526
df7492f9 8527 bcopy (priorities, coding_priorities, sizeof priorities);
177c0ea7 8528
ff563fce
KH
8529 /* Update `coding-category-list'. */
8530 Vcoding_category_list = Qnil;
8531 for (i = coding_category_max - 1; i >= 0; i--)
8532 Vcoding_category_list
8533 = Fcons (AREF (Vcoding_category_table, priorities[i]),
8534 Vcoding_category_list);
6b89e3aa 8535
df7492f9 8536 return Qnil;
6b89e3aa
KH
8537}
8538
df7492f9
KH
8539DEFUN ("coding-system-priority-list", Fcoding_system_priority_list,
8540 Scoding_system_priority_list, 0, 1, 0,
da7db224
DL
8541 doc: /* Return a list of coding systems ordered by their priorities.
8542HIGHESTP non-nil means just return the highest priority one. */)
df7492f9
KH
8543 (highestp)
8544 Lisp_Object highestp;
d46c5b12
KH
8545{
8546 int i;
df7492f9 8547 Lisp_Object val;
6b89e3aa 8548
df7492f9 8549 for (i = 0, val = Qnil; i < coding_category_max; i++)
d46c5b12 8550 {
df7492f9
KH
8551 enum coding_category category = coding_priorities[i];
8552 int id = coding_categories[category].id;
8553 Lisp_Object attrs;
068a9dbd 8554
df7492f9
KH
8555 if (id < 0)
8556 continue;
8557 attrs = CODING_ID_ATTRS (id);
8558 if (! NILP (highestp))
8559 return CODING_ATTR_BASE_NAME (attrs);
8560 val = Fcons (CODING_ATTR_BASE_NAME (attrs), val);
8561 }
8562 return Fnreverse (val);
8563}
068a9dbd 8564
f0064e1f 8565static char *suffixes[] = { "-unix", "-dos", "-mac" };
068a9dbd
KH
8566
8567static Lisp_Object
df7492f9
KH
8568make_subsidiaries (base)
8569 Lisp_Object base;
068a9dbd 8570{
df7492f9 8571 Lisp_Object subsidiaries;
8f924df7 8572 int base_name_len = SBYTES (SYMBOL_NAME (base));
df7492f9
KH
8573 char *buf = (char *) alloca (base_name_len + 6);
8574 int i;
068a9dbd 8575
8f924df7 8576 bcopy (SDATA (SYMBOL_NAME (base)), buf, base_name_len);
df7492f9
KH
8577 subsidiaries = Fmake_vector (make_number (3), Qnil);
8578 for (i = 0; i < 3; i++)
068a9dbd 8579 {
df7492f9
KH
8580 bcopy (suffixes[i], buf + base_name_len, strlen (suffixes[i]) + 1);
8581 ASET (subsidiaries, i, intern (buf));
068a9dbd 8582 }
df7492f9 8583 return subsidiaries;
068a9dbd
KH
8584}
8585
8586
df7492f9
KH
8587DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal,
8588 Sdefine_coding_system_internal, coding_arg_max, MANY, 0,
1fcd6c8b
DL
8589 doc: /* For internal use only.
8590usage: (define-coding-system-internal ...) */)
df7492f9
KH
8591 (nargs, args)
8592 int nargs;
8593 Lisp_Object *args;
068a9dbd 8594{
df7492f9
KH
8595 Lisp_Object name;
8596 Lisp_Object spec_vec; /* [ ATTRS ALIASE EOL_TYPE ] */
8597 Lisp_Object attrs; /* Vector of attributes. */
8598 Lisp_Object eol_type;
8599 Lisp_Object aliases;
8600 Lisp_Object coding_type, charset_list, safe_charsets;
8601 enum coding_category category;
8602 Lisp_Object tail, val;
8603 int max_charset_id = 0;
8604 int i;
068a9dbd 8605
df7492f9
KH
8606 if (nargs < coding_arg_max)
8607 goto short_args;
068a9dbd 8608
df7492f9 8609 attrs = Fmake_vector (make_number (coding_attr_last_index), Qnil);
068a9dbd 8610
df7492f9
KH
8611 name = args[coding_arg_name];
8612 CHECK_SYMBOL (name);
8613 CODING_ATTR_BASE_NAME (attrs) = name;
068a9dbd 8614
df7492f9
KH
8615 val = args[coding_arg_mnemonic];
8616 if (! STRINGP (val))
8617 CHECK_CHARACTER (val);
8618 CODING_ATTR_MNEMONIC (attrs) = val;
068a9dbd 8619
df7492f9
KH
8620 coding_type = args[coding_arg_coding_type];
8621 CHECK_SYMBOL (coding_type);
8622 CODING_ATTR_TYPE (attrs) = coding_type;
068a9dbd 8623
df7492f9
KH
8624 charset_list = args[coding_arg_charset_list];
8625 if (SYMBOLP (charset_list))
8626 {
8627 if (EQ (charset_list, Qiso_2022))
8628 {
8629 if (! EQ (coding_type, Qiso_2022))
8630 error ("Invalid charset-list");
8631 charset_list = Viso_2022_charset_list;
8632 }
8633 else if (EQ (charset_list, Qemacs_mule))
8634 {
8635 if (! EQ (coding_type, Qemacs_mule))
8636 error ("Invalid charset-list");
8637 charset_list = Vemacs_mule_charset_list;
8638 }
8639 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
8640 if (max_charset_id < XFASTINT (XCAR (tail)))
8641 max_charset_id = XFASTINT (XCAR (tail));
8642 }
068a9dbd
KH
8643 else
8644 {
df7492f9 8645 charset_list = Fcopy_sequence (charset_list);
985773c9 8646 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
068a9dbd 8647 {
df7492f9
KH
8648 struct charset *charset;
8649
985773c9 8650 val = XCAR (tail);
df7492f9
KH
8651 CHECK_CHARSET_GET_CHARSET (val, charset);
8652 if (EQ (coding_type, Qiso_2022)
8653 ? CHARSET_ISO_FINAL (charset) < 0
8654 : EQ (coding_type, Qemacs_mule)
8655 ? CHARSET_EMACS_MULE_ID (charset) < 0
8656 : 0)
8657 error ("Can't handle charset `%s'",
8f924df7 8658 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
df7492f9 8659
8f924df7 8660 XSETCAR (tail, make_number (charset->id));
df7492f9
KH
8661 if (max_charset_id < charset->id)
8662 max_charset_id = charset->id;
068a9dbd
KH
8663 }
8664 }
df7492f9 8665 CODING_ATTR_CHARSET_LIST (attrs) = charset_list;
068a9dbd 8666
df7492f9
KH
8667 safe_charsets = Fmake_string (make_number (max_charset_id + 1),
8668 make_number (255));
8669 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
8f924df7 8670 SSET (safe_charsets, XFASTINT (XCAR (tail)), 0);
df7492f9 8671 CODING_ATTR_SAFE_CHARSETS (attrs) = safe_charsets;
068a9dbd 8672
584948ac 8673 CODING_ATTR_ASCII_COMPAT (attrs) = args[coding_arg_ascii_compatible_p];
3a73fa5d 8674
df7492f9 8675 val = args[coding_arg_decode_translation_table];
a6f87d34 8676 if (! CHAR_TABLE_P (val) && ! CONSP (val))
7d64c6ad 8677 CHECK_SYMBOL (val);
df7492f9 8678 CODING_ATTR_DECODE_TBL (attrs) = val;
3a73fa5d 8679
df7492f9 8680 val = args[coding_arg_encode_translation_table];
a6f87d34 8681 if (! CHAR_TABLE_P (val) && ! CONSP (val))
7d64c6ad 8682 CHECK_SYMBOL (val);
df7492f9 8683 CODING_ATTR_ENCODE_TBL (attrs) = val;
d46c5b12 8684
df7492f9
KH
8685 val = args[coding_arg_post_read_conversion];
8686 CHECK_SYMBOL (val);
8687 CODING_ATTR_POST_READ (attrs) = val;
d46c5b12 8688
df7492f9
KH
8689 val = args[coding_arg_pre_write_conversion];
8690 CHECK_SYMBOL (val);
8691 CODING_ATTR_PRE_WRITE (attrs) = val;
3a73fa5d 8692
df7492f9
KH
8693 val = args[coding_arg_default_char];
8694 if (NILP (val))
8695 CODING_ATTR_DEFAULT_CHAR (attrs) = make_number (' ');
8696 else
8697 {
8f924df7 8698 CHECK_CHARACTER (val);
df7492f9
KH
8699 CODING_ATTR_DEFAULT_CHAR (attrs) = val;
8700 }
4031e2bf 8701
8f924df7
KH
8702 val = args[coding_arg_for_unibyte];
8703 CODING_ATTR_FOR_UNIBYTE (attrs) = NILP (val) ? Qnil : Qt;
3a73fa5d 8704
df7492f9
KH
8705 val = args[coding_arg_plist];
8706 CHECK_LIST (val);
8707 CODING_ATTR_PLIST (attrs) = val;
3a73fa5d 8708
df7492f9
KH
8709 if (EQ (coding_type, Qcharset))
8710 {
c7c66a95
KH
8711 /* Generate a lisp vector of 256 elements. Each element is nil,
8712 integer, or a list of charset IDs.
3a73fa5d 8713
c7c66a95
KH
8714 If Nth element is nil, the byte code N is invalid in this
8715 coding system.
4ed46869 8716
c7c66a95
KH
8717 If Nth element is a number NUM, N is the first byte of a
8718 charset whose ID is NUM.
4ed46869 8719
c7c66a95
KH
8720 If Nth element is a list of charset IDs, N is the first byte
8721 of one of them. The list is sorted by dimensions of the
2bc515e4 8722 charsets. A charset of smaller dimension comes firtst. */
df7492f9 8723 val = Fmake_vector (make_number (256), Qnil);
4ed46869 8724
5c99c2e6 8725 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
df7492f9 8726 {
c7c66a95
KH
8727 struct charset *charset = CHARSET_FROM_ID (XFASTINT (XCAR (tail)));
8728 int dim = CHARSET_DIMENSION (charset);
8729 int idx = (dim - 1) * 4;
4ed46869 8730
5c99c2e6 8731 if (CHARSET_ASCII_COMPATIBLE_P (charset))
584948ac 8732 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
4031e2bf 8733
15d143f7
KH
8734 for (i = charset->code_space[idx];
8735 i <= charset->code_space[idx + 1]; i++)
8736 {
c7c66a95
KH
8737 Lisp_Object tmp, tmp2;
8738 int dim2;
ec6d2bb8 8739
c7c66a95
KH
8740 tmp = AREF (val, i);
8741 if (NILP (tmp))
8742 tmp = XCAR (tail);
8743 else if (NUMBERP (tmp))
8744 {
8745 dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp)));
8746 if (dim < dim2)
c7c66a95 8747 tmp = Fcons (XCAR (tail), Fcons (tmp, Qnil));
f9d71dcd
KH
8748 else
8749 tmp = Fcons (tmp, Fcons (XCAR (tail), Qnil));
c7c66a95 8750 }
15d143f7 8751 else
c7c66a95
KH
8752 {
8753 for (tmp2 = tmp; CONSP (tmp2); tmp2 = XCDR (tmp2))
8754 {
8755 dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2))));
8756 if (dim < dim2)
8757 break;
8758 }
8759 if (NILP (tmp2))
8760 tmp = nconc2 (tmp, Fcons (XCAR (tail), Qnil));
8761 else
8762 {
8763 XSETCDR (tmp2, Fcons (XCAR (tmp2), XCDR (tmp2)));
8764 XSETCAR (tmp2, XCAR (tail));
8765 }
8766 }
8767 ASET (val, i, tmp);
15d143f7 8768 }
df7492f9
KH
8769 }
8770 ASET (attrs, coding_attr_charset_valids, val);
8771 category = coding_category_charset;
8772 }
8773 else if (EQ (coding_type, Qccl))
8774 {
8775 Lisp_Object valids;
ecec61c1 8776
df7492f9
KH
8777 if (nargs < coding_arg_ccl_max)
8778 goto short_args;
ecec61c1 8779
df7492f9
KH
8780 val = args[coding_arg_ccl_decoder];
8781 CHECK_CCL_PROGRAM (val);
8782 if (VECTORP (val))
8783 val = Fcopy_sequence (val);
8784 ASET (attrs, coding_attr_ccl_decoder, val);
ecec61c1 8785
df7492f9
KH
8786 val = args[coding_arg_ccl_encoder];
8787 CHECK_CCL_PROGRAM (val);
8788 if (VECTORP (val))
8789 val = Fcopy_sequence (val);
8790 ASET (attrs, coding_attr_ccl_encoder, val);
ecec61c1 8791
df7492f9
KH
8792 val = args[coding_arg_ccl_valids];
8793 valids = Fmake_string (make_number (256), make_number (0));
8794 for (tail = val; !NILP (tail); tail = Fcdr (tail))
8795 {
8dcbea82 8796 int from, to;
ecec61c1 8797
df7492f9
KH
8798 val = Fcar (tail);
8799 if (INTEGERP (val))
8dcbea82
KH
8800 {
8801 from = to = XINT (val);
8802 if (from < 0 || from > 255)
8803 args_out_of_range_3 (val, make_number (0), make_number (255));
8804 }
df7492f9
KH
8805 else
8806 {
df7492f9 8807 CHECK_CONS (val);
8f924df7
KH
8808 CHECK_NATNUM_CAR (val);
8809 CHECK_NATNUM_CDR (val);
df7492f9 8810 from = XINT (XCAR (val));
8f924df7 8811 if (from > 255)
8dcbea82
KH
8812 args_out_of_range_3 (XCAR (val),
8813 make_number (0), make_number (255));
df7492f9 8814 to = XINT (XCDR (val));
8dcbea82
KH
8815 if (to < from || to > 255)
8816 args_out_of_range_3 (XCDR (val),
8817 XCAR (val), make_number (255));
df7492f9 8818 }
8dcbea82 8819 for (i = from; i <= to; i++)
8f924df7 8820 SSET (valids, i, 1);
df7492f9
KH
8821 }
8822 ASET (attrs, coding_attr_ccl_valids, valids);
4ed46869 8823
df7492f9 8824 category = coding_category_ccl;
55ab7be3 8825 }
df7492f9 8826 else if (EQ (coding_type, Qutf_16))
55ab7be3 8827 {
df7492f9 8828 Lisp_Object bom, endian;
4ed46869 8829
584948ac 8830 CODING_ATTR_ASCII_COMPAT (attrs) = Qnil;
4ed46869 8831
df7492f9
KH
8832 if (nargs < coding_arg_utf16_max)
8833 goto short_args;
4ed46869 8834
df7492f9
KH
8835 bom = args[coding_arg_utf16_bom];
8836 if (! NILP (bom) && ! EQ (bom, Qt))
8837 {
8838 CHECK_CONS (bom);
8f924df7
KH
8839 val = XCAR (bom);
8840 CHECK_CODING_SYSTEM (val);
8841 val = XCDR (bom);
8842 CHECK_CODING_SYSTEM (val);
df7492f9
KH
8843 }
8844 ASET (attrs, coding_attr_utf_16_bom, bom);
8845
8846 endian = args[coding_arg_utf16_endian];
b49a1807
KH
8847 CHECK_SYMBOL (endian);
8848 if (NILP (endian))
8849 endian = Qbig;
8850 else if (! EQ (endian, Qbig) && ! EQ (endian, Qlittle))
8f924df7 8851 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian)));
df7492f9
KH
8852 ASET (attrs, coding_attr_utf_16_endian, endian);
8853
8854 category = (CONSP (bom)
8855 ? coding_category_utf_16_auto
8856 : NILP (bom)
b49a1807 8857 ? (EQ (endian, Qbig)
df7492f9
KH
8858 ? coding_category_utf_16_be_nosig
8859 : coding_category_utf_16_le_nosig)
b49a1807 8860 : (EQ (endian, Qbig)
df7492f9
KH
8861 ? coding_category_utf_16_be
8862 : coding_category_utf_16_le));
8863 }
8864 else if (EQ (coding_type, Qiso_2022))
8865 {
8866 Lisp_Object initial, reg_usage, request, flags;
4776e638 8867 int i;
1397dc18 8868
df7492f9
KH
8869 if (nargs < coding_arg_iso2022_max)
8870 goto short_args;
8871
8872 initial = Fcopy_sequence (args[coding_arg_iso2022_initial]);
8873 CHECK_VECTOR (initial);
8874 for (i = 0; i < 4; i++)
8875 {
8876 val = Faref (initial, make_number (i));
8877 if (! NILP (val))
8878 {
584948ac
KH
8879 struct charset *charset;
8880
8881 CHECK_CHARSET_GET_CHARSET (val, charset);
8882 ASET (initial, i, make_number (CHARSET_ID (charset)));
8883 if (i == 0 && CHARSET_ASCII_COMPATIBLE_P (charset))
8884 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
df7492f9
KH
8885 }
8886 else
8887 ASET (initial, i, make_number (-1));
8888 }
8889
8890 reg_usage = args[coding_arg_iso2022_reg_usage];
8891 CHECK_CONS (reg_usage);
8f924df7
KH
8892 CHECK_NUMBER_CAR (reg_usage);
8893 CHECK_NUMBER_CDR (reg_usage);
df7492f9
KH
8894
8895 request = Fcopy_sequence (args[coding_arg_iso2022_request]);
8896 for (tail = request; ! NILP (tail); tail = Fcdr (tail))
1397dc18 8897 {
df7492f9 8898 int id;
8f924df7 8899 Lisp_Object tmp;
df7492f9
KH
8900
8901 val = Fcar (tail);
8902 CHECK_CONS (val);
8f924df7
KH
8903 tmp = XCAR (val);
8904 CHECK_CHARSET_GET_ID (tmp, id);
8905 CHECK_NATNUM_CDR (val);
df7492f9
KH
8906 if (XINT (XCDR (val)) >= 4)
8907 error ("Invalid graphic register number: %d", XINT (XCDR (val)));
8f924df7 8908 XSETCAR (val, make_number (id));
1397dc18 8909 }
4ed46869 8910
df7492f9
KH
8911 flags = args[coding_arg_iso2022_flags];
8912 CHECK_NATNUM (flags);
8913 i = XINT (flags);
8914 if (EQ (args[coding_arg_charset_list], Qiso_2022))
8915 flags = make_number (i | CODING_ISO_FLAG_FULL_SUPPORT);
8916
8917 ASET (attrs, coding_attr_iso_initial, initial);
8918 ASET (attrs, coding_attr_iso_usage, reg_usage);
8919 ASET (attrs, coding_attr_iso_request, request);
8920 ASET (attrs, coding_attr_iso_flags, flags);
8921 setup_iso_safe_charsets (attrs);
8922
8923 if (i & CODING_ISO_FLAG_SEVEN_BITS)
8924 category = ((i & (CODING_ISO_FLAG_LOCKING_SHIFT
8925 | CODING_ISO_FLAG_SINGLE_SHIFT))
8926 ? coding_category_iso_7_else
8927 : EQ (args[coding_arg_charset_list], Qiso_2022)
8928 ? coding_category_iso_7
8929 : coding_category_iso_7_tight);
8930 else
8931 {
8932 int id = XINT (AREF (initial, 1));
8933
c6fb6e98 8934 category = (((i & CODING_ISO_FLAG_LOCKING_SHIFT)
df7492f9
KH
8935 || EQ (args[coding_arg_charset_list], Qiso_2022)
8936 || id < 0)
8937 ? coding_category_iso_8_else
8938 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id)) == 1)
8939 ? coding_category_iso_8_1
8940 : coding_category_iso_8_2);
8941 }
0ce7886f
KH
8942 if (category != coding_category_iso_8_1
8943 && category != coding_category_iso_8_2)
8944 CODING_ATTR_ASCII_COMPAT (attrs) = Qnil;
df7492f9
KH
8945 }
8946 else if (EQ (coding_type, Qemacs_mule))
c28a9453 8947 {
df7492f9
KH
8948 if (EQ (args[coding_arg_charset_list], Qemacs_mule))
8949 ASET (attrs, coding_attr_emacs_mule_full, Qt);
584948ac 8950 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
df7492f9 8951 category = coding_category_emacs_mule;
c28a9453 8952 }
df7492f9 8953 else if (EQ (coding_type, Qshift_jis))
c28a9453 8954 {
df7492f9
KH
8955
8956 struct charset *charset;
8957
7d64c6ad 8958 if (XINT (Flength (charset_list)) != 3
6e07c25f 8959 && XINT (Flength (charset_list)) != 4)
7d64c6ad 8960 error ("There should be three or four charsets");
df7492f9
KH
8961
8962 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
8963 if (CHARSET_DIMENSION (charset) != 1)
8964 error ("Dimension of charset %s is not one",
8f924df7 8965 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
584948ac
KH
8966 if (CHARSET_ASCII_COMPATIBLE_P (charset))
8967 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
df7492f9
KH
8968
8969 charset_list = XCDR (charset_list);
8970 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
8971 if (CHARSET_DIMENSION (charset) != 1)
8972 error ("Dimension of charset %s is not one",
8f924df7 8973 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
df7492f9
KH
8974
8975 charset_list = XCDR (charset_list);
8976 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
8977 if (CHARSET_DIMENSION (charset) != 2)
7d64c6ad
KH
8978 error ("Dimension of charset %s is not two",
8979 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
8980
8981 charset_list = XCDR (charset_list);
2b917a06
KH
8982 if (! NILP (charset_list))
8983 {
8984 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
8985 if (CHARSET_DIMENSION (charset) != 2)
8986 error ("Dimension of charset %s is not two",
8987 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
8988 }
df7492f9
KH
8989
8990 category = coding_category_sjis;
8991 Vsjis_coding_system = name;
c28a9453 8992 }
df7492f9
KH
8993 else if (EQ (coding_type, Qbig5))
8994 {
8995 struct charset *charset;
4ed46869 8996
df7492f9
KH
8997 if (XINT (Flength (charset_list)) != 2)
8998 error ("There should be just two charsets");
8999
9000 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9001 if (CHARSET_DIMENSION (charset) != 1)
9002 error ("Dimension of charset %s is not one",
8f924df7 9003 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
584948ac
KH
9004 if (CHARSET_ASCII_COMPATIBLE_P (charset))
9005 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
df7492f9
KH
9006
9007 charset_list = XCDR (charset_list);
9008 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9009 if (CHARSET_DIMENSION (charset) != 2)
9010 error ("Dimension of charset %s is not two",
8f924df7 9011 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
4ed46869 9012
df7492f9
KH
9013 category = coding_category_big5;
9014 Vbig5_coding_system = name;
9015 }
9016 else if (EQ (coding_type, Qraw_text))
c28a9453 9017 {
584948ac
KH
9018 category = coding_category_raw_text;
9019 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
c28a9453 9020 }
df7492f9 9021 else if (EQ (coding_type, Qutf_8))
4ed46869 9022 {
584948ac
KH
9023 category = coding_category_utf_8;
9024 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
4ed46869 9025 }
df7492f9
KH
9026 else if (EQ (coding_type, Qundecided))
9027 category = coding_category_undecided;
4ed46869 9028 else
df7492f9 9029 error ("Invalid coding system type: %s",
8f924df7 9030 SDATA (SYMBOL_NAME (coding_type)));
4ed46869 9031
df7492f9 9032 CODING_ATTR_CATEGORY (attrs) = make_number (category);
01378f49
KH
9033 CODING_ATTR_PLIST (attrs)
9034 = Fcons (QCcategory, Fcons (AREF (Vcoding_category_table, category),
9035 CODING_ATTR_PLIST (attrs)));
35befdaa
KH
9036 CODING_ATTR_PLIST (attrs)
9037 = Fcons (QCascii_compatible_p,
9038 Fcons (CODING_ATTR_ASCII_COMPAT (attrs),
9039 CODING_ATTR_PLIST (attrs)));
c4825358 9040
df7492f9
KH
9041 eol_type = args[coding_arg_eol_type];
9042 if (! NILP (eol_type)
9043 && ! EQ (eol_type, Qunix)
9044 && ! EQ (eol_type, Qdos)
9045 && ! EQ (eol_type, Qmac))
9046 error ("Invalid eol-type");
4ed46869 9047
df7492f9 9048 aliases = Fcons (name, Qnil);
4ed46869 9049
df7492f9
KH
9050 if (NILP (eol_type))
9051 {
9052 eol_type = make_subsidiaries (name);
9053 for (i = 0; i < 3; i++)
1397dc18 9054 {
df7492f9
KH
9055 Lisp_Object this_spec, this_name, this_aliases, this_eol_type;
9056
9057 this_name = AREF (eol_type, i);
9058 this_aliases = Fcons (this_name, Qnil);
9059 this_eol_type = (i == 0 ? Qunix : i == 1 ? Qdos : Qmac);
9060 this_spec = Fmake_vector (make_number (3), attrs);
9061 ASET (this_spec, 1, this_aliases);
9062 ASET (this_spec, 2, this_eol_type);
9063 Fputhash (this_name, this_spec, Vcoding_system_hash_table);
9064 Vcoding_system_list = Fcons (this_name, Vcoding_system_list);
583f71ca
KH
9065 val = Fassoc (Fsymbol_name (this_name), Vcoding_system_alist);
9066 if (NILP (val))
9067 Vcoding_system_alist
9068 = Fcons (Fcons (Fsymbol_name (this_name), Qnil),
9069 Vcoding_system_alist);
1397dc18 9070 }
d46c5b12 9071 }
4ed46869 9072
df7492f9
KH
9073 spec_vec = Fmake_vector (make_number (3), attrs);
9074 ASET (spec_vec, 1, aliases);
9075 ASET (spec_vec, 2, eol_type);
48b0f3ae 9076
df7492f9
KH
9077 Fputhash (name, spec_vec, Vcoding_system_hash_table);
9078 Vcoding_system_list = Fcons (name, Vcoding_system_list);
583f71ca
KH
9079 val = Fassoc (Fsymbol_name (name), Vcoding_system_alist);
9080 if (NILP (val))
9081 Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (name), Qnil),
9082 Vcoding_system_alist);
48b0f3ae 9083
df7492f9
KH
9084 {
9085 int id = coding_categories[category].id;
48b0f3ae 9086
df7492f9
KH
9087 if (id < 0 || EQ (name, CODING_ID_NAME (id)))
9088 setup_coding_system (name, &coding_categories[category]);
9089 }
48b0f3ae 9090
d46c5b12 9091 return Qnil;
48b0f3ae 9092
df7492f9
KH
9093 short_args:
9094 return Fsignal (Qwrong_number_of_arguments,
9095 Fcons (intern ("define-coding-system-internal"),
9096 make_number (nargs)));
d46c5b12 9097}
4ed46869 9098
d6925f38 9099
a6f87d34
KH
9100DEFUN ("coding-system-put", Fcoding_system_put, Scoding_system_put,
9101 3, 3, 0,
9102 doc: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
9103 (coding_system, prop, val)
9104 Lisp_Object coding_system, prop, val;
9105{
3dbe7859 9106 Lisp_Object spec, attrs;
a6f87d34
KH
9107
9108 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
9109 attrs = AREF (spec, 0);
9110 if (EQ (prop, QCmnemonic))
9111 {
9112 if (! STRINGP (val))
9113 CHECK_CHARACTER (val);
9114 CODING_ATTR_MNEMONIC (attrs) = val;
9115 }
9116 else if (EQ (prop, QCdefalut_char))
9117 {
9118 if (NILP (val))
9119 val = make_number (' ');
9120 else
9121 CHECK_CHARACTER (val);
9122 CODING_ATTR_DEFAULT_CHAR (attrs) = val;
9123 }
9124 else if (EQ (prop, QCdecode_translation_table))
9125 {
9126 if (! CHAR_TABLE_P (val) && ! CONSP (val))
9127 CHECK_SYMBOL (val);
9128 CODING_ATTR_DECODE_TBL (attrs) = val;
9129 }
9130 else if (EQ (prop, QCencode_translation_table))
9131 {
9132 if (! CHAR_TABLE_P (val) && ! CONSP (val))
9133 CHECK_SYMBOL (val);
9134 CODING_ATTR_ENCODE_TBL (attrs) = val;
9135 }
9136 else if (EQ (prop, QCpost_read_conversion))
9137 {
9138 CHECK_SYMBOL (val);
9139 CODING_ATTR_POST_READ (attrs) = val;
9140 }
9141 else if (EQ (prop, QCpre_write_conversion))
9142 {
9143 CHECK_SYMBOL (val);
9144 CODING_ATTR_PRE_WRITE (attrs) = val;
9145 }
35befdaa
KH
9146 else if (EQ (prop, QCascii_compatible_p))
9147 {
9148 CODING_ATTR_ASCII_COMPAT (attrs) = val;
9149 }
a6f87d34
KH
9150
9151 CODING_ATTR_PLIST (attrs)
9152 = Fplist_put (CODING_ATTR_PLIST (attrs), prop, val);
9153 return val;
9154}
9155
9156
df7492f9
KH
9157DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias,
9158 Sdefine_coding_system_alias, 2, 2, 0,
9159 doc: /* Define ALIAS as an alias for CODING-SYSTEM. */)
9160 (alias, coding_system)
9161 Lisp_Object alias, coding_system;
66cfb530 9162{
583f71ca 9163 Lisp_Object spec, aliases, eol_type, val;
4ed46869 9164
df7492f9
KH
9165 CHECK_SYMBOL (alias);
9166 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
9167 aliases = AREF (spec, 1);
d6925f38
KH
9168 /* ALISES should be a list of length more than zero, and the first
9169 element is a base coding system. Append ALIAS at the tail of the
9170 list. */
df7492f9
KH
9171 while (!NILP (XCDR (aliases)))
9172 aliases = XCDR (aliases);
8f924df7 9173 XSETCDR (aliases, Fcons (alias, Qnil));
4ed46869 9174
df7492f9
KH
9175 eol_type = AREF (spec, 2);
9176 if (VECTORP (eol_type))
4ed46869 9177 {
df7492f9
KH
9178 Lisp_Object subsidiaries;
9179 int i;
4ed46869 9180
df7492f9
KH
9181 subsidiaries = make_subsidiaries (alias);
9182 for (i = 0; i < 3; i++)
9183 Fdefine_coding_system_alias (AREF (subsidiaries, i),
9184 AREF (eol_type, i));
4ed46869 9185 }
df7492f9
KH
9186
9187 Fputhash (alias, spec, Vcoding_system_hash_table);
d6925f38 9188 Vcoding_system_list = Fcons (alias, Vcoding_system_list);
583f71ca
KH
9189 val = Fassoc (Fsymbol_name (alias), Vcoding_system_alist);
9190 if (NILP (val))
9191 Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (alias), Qnil),
9192 Vcoding_system_alist);
66cfb530 9193
4ed46869
KH
9194 return Qnil;
9195}
9196
df7492f9
KH
9197DEFUN ("coding-system-base", Fcoding_system_base, Scoding_system_base,
9198 1, 1, 0,
9199 doc: /* Return the base of CODING-SYSTEM.
da7db224 9200Any alias or subsidiary coding system is not a base coding system. */)
df7492f9
KH
9201 (coding_system)
9202 Lisp_Object coding_system;
d46c5b12 9203{
df7492f9 9204 Lisp_Object spec, attrs;
d46c5b12 9205
df7492f9
KH
9206 if (NILP (coding_system))
9207 return (Qno_conversion);
9208 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
9209 attrs = AREF (spec, 0);
9210 return CODING_ATTR_BASE_NAME (attrs);
9211}
1397dc18 9212
df7492f9
KH
9213DEFUN ("coding-system-plist", Fcoding_system_plist, Scoding_system_plist,
9214 1, 1, 0,
9215 doc: "Return the property list of CODING-SYSTEM.")
9216 (coding_system)
9217 Lisp_Object coding_system;
9218{
9219 Lisp_Object spec, attrs;
1397dc18 9220
df7492f9
KH
9221 if (NILP (coding_system))
9222 coding_system = Qno_conversion;
9223 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
9224 attrs = AREF (spec, 0);
9225 return CODING_ATTR_PLIST (attrs);
d46c5b12
KH
9226}
9227
df7492f9
KH
9228
9229DEFUN ("coding-system-aliases", Fcoding_system_aliases, Scoding_system_aliases,
9230 1, 1, 0,
da7db224 9231 doc: /* Return the list of aliases of CODING-SYSTEM. */)
df7492f9
KH
9232 (coding_system)
9233 Lisp_Object coding_system;
66cfb530 9234{
df7492f9 9235 Lisp_Object spec;
84d60297 9236
df7492f9
KH
9237 if (NILP (coding_system))
9238 coding_system = Qno_conversion;
9239 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
da7db224 9240 return AREF (spec, 1);
df7492f9 9241}
66cfb530 9242
df7492f9
KH
9243DEFUN ("coding-system-eol-type", Fcoding_system_eol_type,
9244 Scoding_system_eol_type, 1, 1, 0,
9245 doc: /* Return eol-type of CODING-SYSTEM.
9246An eol-type is integer 0, 1, 2, or a vector of coding systems.
66cfb530 9247
df7492f9
KH
9248Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
9249and CR respectively.
66cfb530 9250
df7492f9
KH
9251A vector value indicates that a format of end-of-line should be
9252detected automatically. Nth element of the vector is the subsidiary
9253coding system whose eol-type is N. */)
6b89e3aa
KH
9254 (coding_system)
9255 Lisp_Object coding_system;
9256{
df7492f9
KH
9257 Lisp_Object spec, eol_type;
9258 int n;
6b89e3aa 9259
df7492f9
KH
9260 if (NILP (coding_system))
9261 coding_system = Qno_conversion;
9262 if (! CODING_SYSTEM_P (coding_system))
9263 return Qnil;
9264 spec = CODING_SYSTEM_SPEC (coding_system);
9265 eol_type = AREF (spec, 2);
9266 if (VECTORP (eol_type))
9267 return Fcopy_sequence (eol_type);
9268 n = EQ (eol_type, Qunix) ? 0 : EQ (eol_type, Qdos) ? 1 : 2;
9269 return make_number (n);
6b89e3aa
KH
9270}
9271
4ed46869
KH
9272#endif /* emacs */
9273
9274\f
1397dc18 9275/*** 9. Post-amble ***/
4ed46869 9276
dfcf069d 9277void
4ed46869
KH
9278init_coding_once ()
9279{
9280 int i;
9281
df7492f9
KH
9282 for (i = 0; i < coding_category_max; i++)
9283 {
9284 coding_categories[i].id = -1;
9285 coding_priorities[i] = i;
9286 }
4ed46869
KH
9287
9288 /* ISO2022 specific initialize routine. */
9289 for (i = 0; i < 0x20; i++)
b73bfc1c 9290 iso_code_class[i] = ISO_control_0;
4ed46869
KH
9291 for (i = 0x21; i < 0x7F; i++)
9292 iso_code_class[i] = ISO_graphic_plane_0;
9293 for (i = 0x80; i < 0xA0; i++)
b73bfc1c 9294 iso_code_class[i] = ISO_control_1;
4ed46869
KH
9295 for (i = 0xA1; i < 0xFF; i++)
9296 iso_code_class[i] = ISO_graphic_plane_1;
9297 iso_code_class[0x20] = iso_code_class[0x7F] = ISO_0x20_or_0x7F;
9298 iso_code_class[0xA0] = iso_code_class[0xFF] = ISO_0xA0_or_0xFF;
4ed46869
KH
9299 iso_code_class[ISO_CODE_SO] = ISO_shift_out;
9300 iso_code_class[ISO_CODE_SI] = ISO_shift_in;
9301 iso_code_class[ISO_CODE_SS2_7] = ISO_single_shift_2_7;
9302 iso_code_class[ISO_CODE_ESC] = ISO_escape;
9303 iso_code_class[ISO_CODE_SS2] = ISO_single_shift_2;
9304 iso_code_class[ISO_CODE_SS3] = ISO_single_shift_3;
9305 iso_code_class[ISO_CODE_CSI] = ISO_control_sequence_introducer;
9306
df7492f9
KH
9307 for (i = 0; i < 256; i++)
9308 {
9309 emacs_mule_bytes[i] = 1;
9310 }
7c78e542
KH
9311 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_11] = 3;
9312 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_12] = 3;
9313 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_21] = 4;
9314 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_22] = 4;
e0e989f6
KH
9315}
9316
9317#ifdef emacs
9318
dfcf069d 9319void
e0e989f6
KH
9320syms_of_coding ()
9321{
df7492f9 9322 staticpro (&Vcoding_system_hash_table);
8f924df7
KH
9323 {
9324 Lisp_Object args[2];
9325 args[0] = QCtest;
9326 args[1] = Qeq;
9327 Vcoding_system_hash_table = Fmake_hash_table (2, args);
9328 }
df7492f9
KH
9329
9330 staticpro (&Vsjis_coding_system);
9331 Vsjis_coding_system = Qnil;
e0e989f6 9332
df7492f9
KH
9333 staticpro (&Vbig5_coding_system);
9334 Vbig5_coding_system = Qnil;
9335
24a73b0a
KH
9336 staticpro (&Vcode_conversion_reused_workbuf);
9337 Vcode_conversion_reused_workbuf = Qnil;
9338
9339 staticpro (&Vcode_conversion_workbuf_name);
9340 Vcode_conversion_workbuf_name = build_string (" *code-conversion-work*");
e0e989f6 9341
24a73b0a 9342 reused_workbuf_in_use = 0;
df7492f9
KH
9343
9344 DEFSYM (Qcharset, "charset");
9345 DEFSYM (Qtarget_idx, "target-idx");
9346 DEFSYM (Qcoding_system_history, "coding-system-history");
bb0115a2
RS
9347 Fset (Qcoding_system_history, Qnil);
9348
9ce27fde 9349 /* Target FILENAME is the first argument. */
e0e989f6 9350 Fput (Qinsert_file_contents, Qtarget_idx, make_number (0));
9ce27fde 9351 /* Target FILENAME is the third argument. */
e0e989f6
KH
9352 Fput (Qwrite_region, Qtarget_idx, make_number (2));
9353
df7492f9 9354 DEFSYM (Qcall_process, "call-process");
9ce27fde 9355 /* Target PROGRAM is the first argument. */
e0e989f6
KH
9356 Fput (Qcall_process, Qtarget_idx, make_number (0));
9357
df7492f9 9358 DEFSYM (Qcall_process_region, "call-process-region");
9ce27fde 9359 /* Target PROGRAM is the third argument. */
e0e989f6
KH
9360 Fput (Qcall_process_region, Qtarget_idx, make_number (2));
9361
df7492f9 9362 DEFSYM (Qstart_process, "start-process");
9ce27fde 9363 /* Target PROGRAM is the third argument. */
e0e989f6
KH
9364 Fput (Qstart_process, Qtarget_idx, make_number (2));
9365
df7492f9 9366 DEFSYM (Qopen_network_stream, "open-network-stream");
9ce27fde 9367 /* Target SERVICE is the fourth argument. */
e0e989f6
KH
9368 Fput (Qopen_network_stream, Qtarget_idx, make_number (3));
9369
df7492f9
KH
9370 DEFSYM (Qcoding_system, "coding-system");
9371 DEFSYM (Qcoding_aliases, "coding-aliases");
4ed46869 9372
df7492f9
KH
9373 DEFSYM (Qeol_type, "eol-type");
9374 DEFSYM (Qunix, "unix");
9375 DEFSYM (Qdos, "dos");
4ed46869 9376
df7492f9
KH
9377 DEFSYM (Qbuffer_file_coding_system, "buffer-file-coding-system");
9378 DEFSYM (Qpost_read_conversion, "post-read-conversion");
9379 DEFSYM (Qpre_write_conversion, "pre-write-conversion");
9380 DEFSYM (Qdefault_char, "default-char");
9381 DEFSYM (Qundecided, "undecided");
9382 DEFSYM (Qno_conversion, "no-conversion");
9383 DEFSYM (Qraw_text, "raw-text");
4ed46869 9384
df7492f9 9385 DEFSYM (Qiso_2022, "iso-2022");
4ed46869 9386
df7492f9 9387 DEFSYM (Qutf_8, "utf-8");
8f924df7 9388 DEFSYM (Qutf_8_emacs, "utf-8-emacs");
27901516 9389
df7492f9 9390 DEFSYM (Qutf_16, "utf-16");
df7492f9
KH
9391 DEFSYM (Qbig, "big");
9392 DEFSYM (Qlittle, "little");
27901516 9393
df7492f9
KH
9394 DEFSYM (Qshift_jis, "shift-jis");
9395 DEFSYM (Qbig5, "big5");
4ed46869 9396
df7492f9 9397 DEFSYM (Qcoding_system_p, "coding-system-p");
4ed46869 9398
df7492f9 9399 DEFSYM (Qcoding_system_error, "coding-system-error");
4ed46869
KH
9400 Fput (Qcoding_system_error, Qerror_conditions,
9401 Fcons (Qcoding_system_error, Fcons (Qerror, Qnil)));
9402 Fput (Qcoding_system_error, Qerror_message,
9ce27fde 9403 build_string ("Invalid coding system"));
4ed46869 9404
05e6f5dc
KH
9405 /* Intern this now in case it isn't already done.
9406 Setting this variable twice is harmless.
9407 But don't staticpro it here--that is done in alloc.c. */
9408 Qchar_table_extra_slots = intern ("char-table-extra-slots");
70c22245 9409
df7492f9 9410 DEFSYM (Qtranslation_table, "translation-table");
433f7f87 9411 Fput (Qtranslation_table, Qchar_table_extra_slots, make_number (2));
df7492f9
KH
9412 DEFSYM (Qtranslation_table_id, "translation-table-id");
9413 DEFSYM (Qtranslation_table_for_decode, "translation-table-for-decode");
9414 DEFSYM (Qtranslation_table_for_encode, "translation-table-for-encode");
1397dc18 9415
df7492f9 9416 DEFSYM (Qvalid_codes, "valid-codes");
9ce27fde 9417
df7492f9 9418 DEFSYM (Qemacs_mule, "emacs-mule");
d46c5b12 9419
01378f49 9420 DEFSYM (QCcategory, ":category");
a6f87d34
KH
9421 DEFSYM (QCmnemonic, ":mnemonic");
9422 DEFSYM (QCdefalut_char, ":default-char");
9423 DEFSYM (QCdecode_translation_table, ":decode-translation-table");
9424 DEFSYM (QCencode_translation_table, ":encode-translation-table");
9425 DEFSYM (QCpost_read_conversion, ":post-read-conversion");
9426 DEFSYM (QCpre_write_conversion, ":pre-write-conversion");
35befdaa 9427 DEFSYM (QCascii_compatible_p, ":ascii-compatible-p");
01378f49 9428
df7492f9
KH
9429 Vcoding_category_table
9430 = Fmake_vector (make_number (coding_category_max), Qnil);
9431 staticpro (&Vcoding_category_table);
9432 /* Followings are target of code detection. */
9433 ASET (Vcoding_category_table, coding_category_iso_7,
9434 intern ("coding-category-iso-7"));
9435 ASET (Vcoding_category_table, coding_category_iso_7_tight,
9436 intern ("coding-category-iso-7-tight"));
9437 ASET (Vcoding_category_table, coding_category_iso_8_1,
9438 intern ("coding-category-iso-8-1"));
9439 ASET (Vcoding_category_table, coding_category_iso_8_2,
9440 intern ("coding-category-iso-8-2"));
9441 ASET (Vcoding_category_table, coding_category_iso_7_else,
9442 intern ("coding-category-iso-7-else"));
9443 ASET (Vcoding_category_table, coding_category_iso_8_else,
9444 intern ("coding-category-iso-8-else"));
9445 ASET (Vcoding_category_table, coding_category_utf_8,
9446 intern ("coding-category-utf-8"));
9447 ASET (Vcoding_category_table, coding_category_utf_16_be,
9448 intern ("coding-category-utf-16-be"));
ff563fce
KH
9449 ASET (Vcoding_category_table, coding_category_utf_16_auto,
9450 intern ("coding-category-utf-16-auto"));
df7492f9
KH
9451 ASET (Vcoding_category_table, coding_category_utf_16_le,
9452 intern ("coding-category-utf-16-le"));
9453 ASET (Vcoding_category_table, coding_category_utf_16_be_nosig,
9454 intern ("coding-category-utf-16-be-nosig"));
9455 ASET (Vcoding_category_table, coding_category_utf_16_le_nosig,
9456 intern ("coding-category-utf-16-le-nosig"));
9457 ASET (Vcoding_category_table, coding_category_charset,
9458 intern ("coding-category-charset"));
9459 ASET (Vcoding_category_table, coding_category_sjis,
9460 intern ("coding-category-sjis"));
9461 ASET (Vcoding_category_table, coding_category_big5,
9462 intern ("coding-category-big5"));
9463 ASET (Vcoding_category_table, coding_category_ccl,
9464 intern ("coding-category-ccl"));
9465 ASET (Vcoding_category_table, coding_category_emacs_mule,
9466 intern ("coding-category-emacs-mule"));
9467 /* Followings are NOT target of code detection. */
9468 ASET (Vcoding_category_table, coding_category_raw_text,
9469 intern ("coding-category-raw-text"));
9470 ASET (Vcoding_category_table, coding_category_undecided,
9471 intern ("coding-category-undecided"));
ecf488bc 9472
065e3595
KH
9473 DEFSYM (Qinsufficient_source, "insufficient-source");
9474 DEFSYM (Qinconsistent_eol, "inconsistent-eol");
9475 DEFSYM (Qinvalid_source, "invalid-source");
9476 DEFSYM (Qinterrupted, "interrupted");
9477 DEFSYM (Qinsufficient_memory, "insufficient-memory");
44e8490d 9478 DEFSYM (Qcoding_system_define_form, "coding-system-define-form");
065e3595 9479
4ed46869
KH
9480 defsubr (&Scoding_system_p);
9481 defsubr (&Sread_coding_system);
9482 defsubr (&Sread_non_nil_coding_system);
9483 defsubr (&Scheck_coding_system);
9484 defsubr (&Sdetect_coding_region);
d46c5b12 9485 defsubr (&Sdetect_coding_string);
05e6f5dc 9486 defsubr (&Sfind_coding_systems_region_internal);
068a9dbd 9487 defsubr (&Sunencodable_char_position);
df7492f9 9488 defsubr (&Scheck_coding_systems_region);
4ed46869
KH
9489 defsubr (&Sdecode_coding_region);
9490 defsubr (&Sencode_coding_region);
9491 defsubr (&Sdecode_coding_string);
9492 defsubr (&Sencode_coding_string);
9493 defsubr (&Sdecode_sjis_char);
9494 defsubr (&Sencode_sjis_char);
9495 defsubr (&Sdecode_big5_char);
9496 defsubr (&Sencode_big5_char);
1ba9e4ab 9497 defsubr (&Sset_terminal_coding_system_internal);
c4825358 9498 defsubr (&Sset_safe_terminal_coding_system_internal);
4ed46869 9499 defsubr (&Sterminal_coding_system);
1ba9e4ab 9500 defsubr (&Sset_keyboard_coding_system_internal);
4ed46869 9501 defsubr (&Skeyboard_coding_system);
a5d301df 9502 defsubr (&Sfind_operation_coding_system);
df7492f9 9503 defsubr (&Sset_coding_system_priority);
6b89e3aa 9504 defsubr (&Sdefine_coding_system_internal);
df7492f9 9505 defsubr (&Sdefine_coding_system_alias);
a6f87d34 9506 defsubr (&Scoding_system_put);
df7492f9
KH
9507 defsubr (&Scoding_system_base);
9508 defsubr (&Scoding_system_plist);
9509 defsubr (&Scoding_system_aliases);
9510 defsubr (&Scoding_system_eol_type);
9511 defsubr (&Scoding_system_priority_list);
4ed46869 9512
4608c386 9513 DEFVAR_LISP ("coding-system-list", &Vcoding_system_list,
48b0f3ae
PJ
9514 doc: /* List of coding systems.
9515
9516Do not alter the value of this variable manually. This variable should be
df7492f9 9517updated by the functions `define-coding-system' and
48b0f3ae 9518`define-coding-system-alias'. */);
4608c386
KH
9519 Vcoding_system_list = Qnil;
9520
9521 DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist,
48b0f3ae
PJ
9522 doc: /* Alist of coding system names.
9523Each element is one element list of coding system name.
9524This variable is given to `completing-read' as TABLE argument.
9525
9526Do not alter the value of this variable manually. This variable should be
9527updated by the functions `make-coding-system' and
9528`define-coding-system-alias'. */);
4608c386
KH
9529 Vcoding_system_alist = Qnil;
9530
4ed46869 9531 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list,
48b0f3ae
PJ
9532 doc: /* List of coding-categories (symbols) ordered by priority.
9533
9534On detecting a coding system, Emacs tries code detection algorithms
9535associated with each coding-category one by one in this order. When
9536one algorithm agrees with a byte sequence of source text, the coding
0ec31faf
KH
9537system bound to the corresponding coding-category is selected.
9538
42205607 9539Don't modify this variable directly, but use `set-coding-priority'. */);
4ed46869
KH
9540 {
9541 int i;
9542
9543 Vcoding_category_list = Qnil;
df7492f9 9544 for (i = coding_category_max - 1; i >= 0; i--)
4ed46869 9545 Vcoding_category_list
d46c5b12
KH
9546 = Fcons (XVECTOR (Vcoding_category_table)->contents[i],
9547 Vcoding_category_list);
4ed46869
KH
9548 }
9549
9550 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read,
48b0f3ae
PJ
9551 doc: /* Specify the coding system for read operations.
9552It is useful to bind this variable with `let', but do not set it globally.
9553If the value is a coding system, it is used for decoding on read operation.
9554If not, an appropriate element is used from one of the coding system alists:
9555There are three such tables, `file-coding-system-alist',
9556`process-coding-system-alist', and `network-coding-system-alist'. */);
4ed46869
KH
9557 Vcoding_system_for_read = Qnil;
9558
9559 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write,
48b0f3ae
PJ
9560 doc: /* Specify the coding system for write operations.
9561Programs bind this variable with `let', but you should not set it globally.
9562If the value is a coding system, it is used for encoding of output,
9563when writing it to a file and when sending it to a file or subprocess.
9564
9565If this does not specify a coding system, an appropriate element
9566is used from one of the coding system alists:
9567There are three such tables, `file-coding-system-alist',
9568`process-coding-system-alist', and `network-coding-system-alist'.
9569For output to files, if the above procedure does not specify a coding system,
9570the value of `buffer-file-coding-system' is used. */);
4ed46869
KH
9571 Vcoding_system_for_write = Qnil;
9572
9573 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used,
df7492f9
KH
9574 doc: /*
9575Coding system used in the latest file or process I/O. */);
4ed46869
KH
9576 Vlast_coding_system_used = Qnil;
9577
065e3595
KH
9578 DEFVAR_LISP ("last-code-conversion-error", &Vlast_code_conversion_error,
9579 doc: /*
9580Error status of the last code conversion.
9581
9582When an error was detected in the last code conversion, this variable
9583is set to one of the following symbols.
9584 `insufficient-source'
9585 `inconsistent-eol'
9586 `invalid-source'
9587 `interrupted'
9588 `insufficient-memory'
9589When no error was detected, the value doesn't change. So, to check
9590the error status of a code conversion by this variable, you must
9591explicitly set this variable to nil before performing code
9592conversion. */);
9593 Vlast_code_conversion_error = Qnil;
9594
9ce27fde 9595 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion,
df7492f9
KH
9596 doc: /*
9597*Non-nil means always inhibit code conversion of end-of-line format.
48b0f3ae
PJ
9598See info node `Coding Systems' and info node `Text and Binary' concerning
9599such conversion. */);
9ce27fde
KH
9600 inhibit_eol_conversion = 0;
9601
ed29121d 9602 DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system,
df7492f9
KH
9603 doc: /*
9604Non-nil means process buffer inherits coding system of process output.
48b0f3ae
PJ
9605Bind it to t if the process output is to be treated as if it were a file
9606read from some filesystem. */);
ed29121d
EZ
9607 inherit_process_coding_system = 0;
9608
02ba4723 9609 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist,
df7492f9
KH
9610 doc: /*
9611Alist to decide a coding system to use for a file I/O operation.
48b0f3ae
PJ
9612The format is ((PATTERN . VAL) ...),
9613where PATTERN is a regular expression matching a file name,
9614VAL is a coding system, a cons of coding systems, or a function symbol.
9615If VAL is a coding system, it is used for both decoding and encoding
9616the file contents.
9617If VAL is a cons of coding systems, the car part is used for decoding,
9618and the cdr part is used for encoding.
9619If VAL is a function symbol, the function must return a coding system
2c53e699
KH
9620or a cons of coding systems which are used as above. The function is
9621called with an argument that is a list of the arguments with which
5a0bbd9a
KH
9622`find-operation-coding-system' was called. If the function can't decide
9623a coding system, it can return `undecided' so that the normal
9624code-detection is performed.
48b0f3ae
PJ
9625
9626See also the function `find-operation-coding-system'
9627and the variable `auto-coding-alist'. */);
02ba4723
KH
9628 Vfile_coding_system_alist = Qnil;
9629
9630 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist,
df7492f9
KH
9631 doc: /*
9632Alist to decide a coding system to use for a process I/O operation.
48b0f3ae
PJ
9633The format is ((PATTERN . VAL) ...),
9634where PATTERN is a regular expression matching a program name,
9635VAL is a coding system, a cons of coding systems, or a function symbol.
9636If VAL is a coding system, it is used for both decoding what received
9637from the program and encoding what sent to the program.
9638If VAL is a cons of coding systems, the car part is used for decoding,
9639and the cdr part is used for encoding.
9640If VAL is a function symbol, the function must return a coding system
9641or a cons of coding systems which are used as above.
9642
9643See also the function `find-operation-coding-system'. */);
02ba4723
KH
9644 Vprocess_coding_system_alist = Qnil;
9645
9646 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist,
df7492f9
KH
9647 doc: /*
9648Alist to decide a coding system to use for a network I/O operation.
48b0f3ae
PJ
9649The format is ((PATTERN . VAL) ...),
9650where PATTERN is a regular expression matching a network service name
9651or is a port number to connect to,
9652VAL is a coding system, a cons of coding systems, or a function symbol.
9653If VAL is a coding system, it is used for both decoding what received
9654from the network stream and encoding what sent to the network stream.
9655If VAL is a cons of coding systems, the car part is used for decoding,
9656and the cdr part is used for encoding.
9657If VAL is a function symbol, the function must return a coding system
9658or a cons of coding systems which are used as above.
9659
9660See also the function `find-operation-coding-system'. */);
02ba4723 9661 Vnetwork_coding_system_alist = Qnil;
4ed46869 9662
68c45bf0 9663 DEFVAR_LISP ("locale-coding-system", &Vlocale_coding_system,
75205970
RS
9664 doc: /* Coding system to use with system messages.
9665Also used for decoding keyboard input on X Window system. */);
68c45bf0
PE
9666 Vlocale_coding_system = Qnil;
9667
005f0d35 9668 /* The eol mnemonics are reset in startup.el system-dependently. */
7722baf9 9669 DEFVAR_LISP ("eol-mnemonic-unix", &eol_mnemonic_unix,
df7492f9
KH
9670 doc: /*
9671*String displayed in mode line for UNIX-like (LF) end-of-line format. */);
7722baf9 9672 eol_mnemonic_unix = build_string (":");
4ed46869 9673
7722baf9 9674 DEFVAR_LISP ("eol-mnemonic-dos", &eol_mnemonic_dos,
df7492f9
KH
9675 doc: /*
9676*String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
7722baf9 9677 eol_mnemonic_dos = build_string ("\\");
4ed46869 9678
7722baf9 9679 DEFVAR_LISP ("eol-mnemonic-mac", &eol_mnemonic_mac,
df7492f9
KH
9680 doc: /*
9681*String displayed in mode line for MAC-like (CR) end-of-line format. */);
7722baf9 9682 eol_mnemonic_mac = build_string ("/");
4ed46869 9683
7722baf9 9684 DEFVAR_LISP ("eol-mnemonic-undecided", &eol_mnemonic_undecided,
df7492f9
KH
9685 doc: /*
9686*String displayed in mode line when end-of-line format is not yet determined. */);
7722baf9 9687 eol_mnemonic_undecided = build_string (":");
4ed46869 9688
84fbb8a0 9689 DEFVAR_LISP ("enable-character-translation", &Venable_character_translation,
df7492f9
KH
9690 doc: /*
9691*Non-nil enables character translation while encoding and decoding. */);
84fbb8a0 9692 Venable_character_translation = Qt;
bdd9fb48 9693
f967223b 9694 DEFVAR_LISP ("standard-translation-table-for-decode",
48b0f3ae
PJ
9695 &Vstandard_translation_table_for_decode,
9696 doc: /* Table for translating characters while decoding. */);
f967223b 9697 Vstandard_translation_table_for_decode = Qnil;
bdd9fb48 9698
f967223b 9699 DEFVAR_LISP ("standard-translation-table-for-encode",
48b0f3ae
PJ
9700 &Vstandard_translation_table_for_encode,
9701 doc: /* Table for translating characters while encoding. */);
f967223b 9702 Vstandard_translation_table_for_encode = Qnil;
4ed46869 9703
df7492f9 9704 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_table,
48b0f3ae
PJ
9705 doc: /* Alist of charsets vs revision numbers.
9706While encoding, if a charset (car part of an element) is found,
df7492f9
KH
9707designate it with the escape sequence identifying revision (cdr part
9708of the element). */);
9709 Vcharset_revision_table = Qnil;
02ba4723
KH
9710
9711 DEFVAR_LISP ("default-process-coding-system",
9712 &Vdefault_process_coding_system,
48b0f3ae
PJ
9713 doc: /* Cons of coding systems used for process I/O by default.
9714The car part is used for decoding a process output,
9715the cdr part is used for encoding a text to be sent to a process. */);
02ba4723 9716 Vdefault_process_coding_system = Qnil;
c4825358 9717
3f003981 9718 DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table,
df7492f9
KH
9719 doc: /*
9720Table of extra Latin codes in the range 128..159 (inclusive).
48b0f3ae
PJ
9721This is a vector of length 256.
9722If Nth element is non-nil, the existence of code N in a file
9723\(or output of subprocess) doesn't prevent it to be detected as
9724a coding system of ISO 2022 variant which has a flag
9725`accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
9726or reading output of a subprocess.
9727Only 128th through 159th elements has a meaning. */);
3f003981 9728 Vlatin_extra_code_table = Fmake_vector (make_number (256), Qnil);
d46c5b12
KH
9729
9730 DEFVAR_LISP ("select-safe-coding-system-function",
9731 &Vselect_safe_coding_system_function,
df7492f9
KH
9732 doc: /*
9733Function to call to select safe coding system for encoding a text.
48b0f3ae
PJ
9734
9735If set, this function is called to force a user to select a proper
9736coding system which can encode the text in the case that a default
9737coding system used in each operation can't encode the text.
9738
9739The default value is `select-safe-coding-system' (which see). */);
d46c5b12
KH
9740 Vselect_safe_coding_system_function = Qnil;
9741
5d5bf4d8
KH
9742 DEFVAR_BOOL ("coding-system-require-warning",
9743 &coding_system_require_warning,
9744 doc: /* Internal use only.
6b89e3aa
KH
9745If non-nil, on writing a file, `select-safe-coding-system-function' is
9746called even if `coding-system-for-write' is non-nil. The command
9747`universal-coding-system-argument' binds this variable to t temporarily. */);
5d5bf4d8
KH
9748 coding_system_require_warning = 0;
9749
9750
22ab2303 9751 DEFVAR_BOOL ("inhibit-iso-escape-detection",
74383408 9752 &inhibit_iso_escape_detection,
df7492f9
KH
9753 doc: /*
9754If non-nil, Emacs ignores ISO2022's escape sequence on code detection.
48b0f3ae
PJ
9755
9756By default, on reading a file, Emacs tries to detect how the text is
9757encoded. This code detection is sensitive to escape sequences. If
9758the sequence is valid as ISO2022, the code is determined as one of
9759the ISO2022 encodings, and the file is decoded by the corresponding
9760coding system (e.g. `iso-2022-7bit').
9761
9762However, there may be a case that you want to read escape sequences in
9763a file as is. In such a case, you can set this variable to non-nil.
9764Then, as the code detection ignores any escape sequences, no file is
9765detected as encoded in some ISO2022 encoding. The result is that all
9766escape sequences become visible in a buffer.
9767
9768The default value is nil, and it is strongly recommended not to change
9769it. That is because many Emacs Lisp source files that contain
9770non-ASCII characters are encoded by the coding system `iso-2022-7bit'
9771in Emacs's distribution, and they won't be decoded correctly on
9772reading if you suppress escape sequence detection.
9773
9774The other way to read escape sequences in a file without decoding is
9775to explicitly specify some coding system that doesn't use ISO2022's
9776escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
74383408 9777 inhibit_iso_escape_detection = 0;
002fdb44
DL
9778
9779 DEFVAR_LISP ("translation-table-for-input", &Vtranslation_table_for_input,
15c8f9d1
DL
9780 doc: /* Char table for translating self-inserting characters.
9781This is applied to the result of input methods, not their input. See also
9782`keyboard-translate-table'. */);
002fdb44 9783 Vtranslation_table_for_input = Qnil;
8f924df7 9784
2c78b7e1
KH
9785 {
9786 Lisp_Object args[coding_arg_max];
8f924df7 9787 Lisp_Object plist[16];
2c78b7e1
KH
9788 int i;
9789
9790 for (i = 0; i < coding_arg_max; i++)
9791 args[i] = Qnil;
9792
9793 plist[0] = intern (":name");
9794 plist[1] = args[coding_arg_name] = Qno_conversion;
9795 plist[2] = intern (":mnemonic");
9796 plist[3] = args[coding_arg_mnemonic] = make_number ('=');
9797 plist[4] = intern (":coding-type");
9798 plist[5] = args[coding_arg_coding_type] = Qraw_text;
9799 plist[6] = intern (":ascii-compatible-p");
9800 plist[7] = args[coding_arg_ascii_compatible_p] = Qt;
9801 plist[8] = intern (":default-char");
9802 plist[9] = args[coding_arg_default_char] = make_number (0);
8f924df7
KH
9803 plist[10] = intern (":for-unibyte");
9804 plist[11] = args[coding_arg_for_unibyte] = Qt;
9805 plist[12] = intern (":docstring");
9806 plist[13] = build_string ("Do no conversion.\n\
2c78b7e1
KH
9807\n\
9808When you visit a file with this coding, the file is read into a\n\
9809unibyte buffer as is, thus each byte of a file is treated as a\n\
9810character.");
8f924df7
KH
9811 plist[14] = intern (":eol-type");
9812 plist[15] = args[coding_arg_eol_type] = Qunix;
9813 args[coding_arg_plist] = Flist (16, plist);
2c78b7e1 9814 Fdefine_coding_system_internal (coding_arg_max, args);
ae6f73fa
KH
9815
9816 plist[1] = args[coding_arg_name] = Qundecided;
9817 plist[3] = args[coding_arg_mnemonic] = make_number ('-');
9818 plist[5] = args[coding_arg_coding_type] = Qundecided;
9819 /* This is already set.
35befdaa 9820 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
ae6f73fa
KH
9821 plist[8] = intern (":charset-list");
9822 plist[9] = args[coding_arg_charset_list] = Fcons (Qascii, Qnil);
9823 plist[11] = args[coding_arg_for_unibyte] = Qnil;
9824 plist[13] = build_string ("No conversion on encoding, automatic conversion on decoding.");
9825 plist[15] = args[coding_arg_eol_type] = Qnil;
9826 args[coding_arg_plist] = Flist (16, plist);
9827 Fdefine_coding_system_internal (coding_arg_max, args);
2c78b7e1
KH
9828 }
9829
2c78b7e1 9830 setup_coding_system (Qno_conversion, &safe_terminal_coding);
ff563fce
KH
9831
9832 {
9833 int i;
9834
9835 for (i = 0; i < coding_category_max; i++)
9836 Fset (AREF (Vcoding_category_table, i), Qno_conversion);
9837 }
fcbcfb64
KH
9838#if defined (MSDOS) || defined (WINDOWSNT)
9839 system_eol_type = Qdos;
9840#else
9841 system_eol_type = Qunix;
9842#endif
9843 staticpro (&system_eol_type);
4ed46869
KH
9844}
9845
68c45bf0
PE
9846char *
9847emacs_strerror (error_number)
9848 int error_number;
9849{
9850 char *str;
9851
ca9c0567 9852 synchronize_system_messages_locale ();
68c45bf0
PE
9853 str = strerror (error_number);
9854
9855 if (! NILP (Vlocale_coding_system))
9856 {
9857 Lisp_Object dec = code_convert_string_norecord (build_string (str),
9858 Vlocale_coding_system,
9859 0);
d5db4077 9860 str = (char *) SDATA (dec);
68c45bf0
PE
9861 }
9862
9863 return str;
9864}
9865
4ed46869 9866#endif /* emacs */
9ffd559c
KH
9867
9868/* arch-tag: 3a3a2b01-5ff6-4071-9afe-f5b808d9229d
9869 (do not change this comment) */