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