comparison src/coding.c @ 88365:d29ddd7d50f9

Completely re-written.
author Kenichi Handa <handa@m17n.org>
date Fri, 01 Mar 2002 01:17:24 +0000
parents 983767b5ccc0
children 6418a272b97e
comparison
equal deleted inserted replaced
88364:f6557aebe110 88365:d29ddd7d50f9
1 /* Coding system handler (conversion, detection, and etc). 1 /* Coding system handler (conversion, detection, and etc).
2 Copyright (C) 1995, 1997, 1998 Electrotechnical Laboratory, JAPAN. 2 Copyright (C) 1995, 1997, 1998 Electrotechnical Laboratory, JAPAN.
3 Licensed to the Free Software Foundation. 3 Licensed to the Free Software Foundation.
4 Copyright (C) 2001 Free Software Foundation, Inc. 4 Copyright (C) 2001 Free Software Foundation, Inc.
5 Copyright (C) 2001, 2002
6 National Institute of Advanced Industrial Science and Technology (AIST)
7 Registration Number H13PRO009
5 8
6 This file is part of GNU Emacs. 9 This file is part of GNU Emacs.
7 10
8 GNU Emacs is free software; you can redistribute it and/or modify 11 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by 12 it under the terms of the GNU General Public License as published by
22 25
23 /*** TABLE OF CONTENTS *** 26 /*** TABLE OF CONTENTS ***
24 27
25 0. General comments 28 0. General comments
26 1. Preamble 29 1. Preamble
27 2. Emacs' internal format (emacs-mule) handlers 30 2. Emacs' internal format (emacs-utf-8) handlers
28 3. ISO2022 handlers 31 3. UTF-8 handlers
29 4. Shift-JIS and BIG5 handlers 32 4. UTF-16 handlers
30 5. CCL handlers 33 5. Charset-base coding systems handlers
31 6. End-of-line handlers 34 6. emacs-mule (old Emacs' internal format) handlers
32 7. C library functions 35 7. ISO2022 handlers
33 8. Emacs Lisp library functions 36 8. Shift-JIS and BIG5 handlers
34 9. Post-amble 37 9. CCL handlers
38 10. C library functions
39 11. Emacs Lisp library functions
40 12. Postamble
35 41
36 */ 42 */
37 43
38 /*** 0. General comments ***/ 44 /*** 0. General comments ***
39 45
40 46
41 /*** GENERAL NOTE on CODING SYSTEMS *** 47 CODING SYSTEM
42 48
43 A coding system is an encoding mechanism for one or more character 49 Coding system is an encoding mechanism of one or more character
44 sets. Here's a list of coding systems which Emacs can handle. When 50 sets. Here's a list of coding system types supported by Emacs.
45 we say "decode", it means converting some other coding system to 51 When we say "decode", it means converting a text encoded by some
46 Emacs' internal format (emacs-mule), and when we say "encode", 52 coding system into Emacs' internal format (emacs-utf-8), and when we
47 it means converting the coding system emacs-mule to some other 53 say "encode", it means converting a text of emacs-utf-8 to some
48 coding system. 54 other coding system.
49 55
50 0. Emacs' internal format (emacs-mule) 56 Emacs represents a coding system by a Lisp symbol. Each symbol is a
51 57 key to the hash table Vcharset_hash_table. This hash table
52 Emacs itself holds a multi-lingual character in buffers and strings 58 associates the symbol to the corresponding detailed specifications.
53 in a special format. Details are described in section 2. 59
54 60 Before using a coding system for decoding and encoding, we setup a
55 1. ISO2022 61 structure of type `struct coding_system'. This structure keeps
62 various information about a specific code conversion (e.g. the
63 location of source and destination data).
64
65 Coding systems are classified into the following types by how to
66 represent a character in a byte sequence. Here's a brief descrition
67 about type.
68
69 o Emacs' internal format (emacs-utf-8)
70
71 The extended UTF-8 which allows eight-bit raw bytes mixed with
72 character codes. Emacs holds characters in buffers and strings by
73 this format.
74
75 o UTF-8
76
77 o UTF-16
78
79 o Charset-base coding system
80
81 A coding system defined by one or more (coded) character sets.
82 Decoding and encoding are done by code converter defined for each
83 character set.
84
85 o Old Emacs' internal format (emacs-mule)
86
87 The coding system adopted by an old versions of Emacs (20 and 21).
88
89 o ISO2022-base coding system
56 90
57 The most famous coding system for multiple character sets. X's 91 The most famous coding system for multiple character sets. X's
58 Compound Text, various EUCs (Extended Unix Code), and coding 92 Compound Text, various EUCs (Extended Unix Code), and coding systems
59 systems used in Internet communication such as ISO-2022-JP are 93 used in the Internet communication such as ISO-2022-JP are all
60 all variants of ISO2022. Details are described in section 3. 94 variants of ISO2022.
61 95
62 2. SJIS (or Shift-JIS or MS-Kanji-Code) 96 o SJIS (or Shift-JIS or MS-Kanji-Code)
63 97
64 A coding system to encode character sets: ASCII, JISX0201, and 98 A coding system to encode character sets: ASCII, JISX0201, and
65 JISX0208. Widely used for PC's in Japan. Details are described in 99 JISX0208. Widely used for PC's in Japan. Details are described in
66 section 4. 100 section 8.
67 101
68 3. BIG5 102 o BIG5
69 103
70 A coding system to encode the character sets ASCII and Big5. Widely 104 A coding system to encode character sets: ASCII and Big5. Widely
71 used for Chinese (mainly in Taiwan and Hong Kong). Details are 105 used by Chinese (mainly in Taiwan and Hong Kong). Details are
72 described in section 4. In this file, when we write "BIG5" 106 described in section 8. In this file, when we write "big5" (all
73 (all uppercase), we mean the coding system, and when we write 107 lowercase), we mean the coding system, and when we write "Big5"
74 "Big5" (capitalized), we mean the character set. 108 (capitalized), we mean the character set.
75 109
76 4. Raw text 110 o CCL
77 111
78 A coding system for text containing random 8-bit code. Emacs does 112 If a user wants to decode/encode a text encoded in a coding system
79 no code conversion on such text except for end-of-line format. 113 not listed above, he can supply a decoder and an encoder for it in
80 114 CCL (Code Conversion Language) programs. Emacs executes the CCL
81 5. Other 115 program while decoding/encoding.
82 116
83 If a user wants to read/write text encoded in a coding system not 117 o Raw-text
84 listed above, he can supply a decoder and an encoder for it as CCL 118
85 (Code Conversion Language) programs. Emacs executes the CCL program 119 A coding system for a text containing raw eight-bit data. Emacs
86 while reading/writing. 120 treat each byte of source text as a character (except for
87 121 end-of-line conversion).
88 Emacs represents a coding system by a Lisp symbol that has a property 122
89 `coding-system'. But, before actually using the coding system, the 123 o No-conversion
90 information about it is set in a structure of type `struct 124
91 coding_system' for rapid processing. See section 6 for more details. 125 Like raw text, but don't do end-of-line conversion.
92 126
93 */ 127
94 128 END-OF-LINE FORMAT
95 /*** GENERAL NOTES on END-OF-LINE FORMAT *** 129
96 130 How end-of-line of a text is encoded depends on a system. For
97 How end-of-line of text is encoded depends on the operating system. 131 instance, Unix's format is just one byte of LF (line-feed) code,
98 For instance, Unix's format is just one byte of `line-feed' code,
99 whereas DOS's format is two-byte sequence of `carriage-return' and 132 whereas DOS's format is two-byte sequence of `carriage-return' and
100 `line-feed' codes. MacOS's format is usually one byte of 133 `line-feed' codes. MacOS's format is usually one byte of
101 `carriage-return'. 134 `carriage-return'.
102 135
103 Since text character encoding and end-of-line encoding are 136 Since text characters encoding and end-of-line encoding are
104 independent, any coding system described above can have any 137 independent, any coding system described above can take any format
105 end-of-line format. So Emacs has information about end-of-line 138 of end-of-line (except for no-conversion).
106 format in each coding-system. See section 6 for more details.
107 139
108 */ 140 */
109 141
142 /* COMMON MACROS */
143
144
110 /*** GENERAL NOTES on `detect_coding_XXX ()' functions *** 145 /*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
111 146
112 These functions check if a text between SRC and SRC_END is encoded 147 These functions check if a byte sequence specified as a source in
113 in the coding system category XXX. Each returns an integer value in 148 CODING conforms to the format of XXX. Return 1 if the data contains
114 which appropriate flag bits for the category XXX are set. The flag 149 a byte sequence which can be decoded into non-ASCII characters by
115 bits are defined in macros CODING_CATEGORY_MASK_XXX. Below is the 150 the coding system. Otherwize (i.e. the data contains only ASCII
116 template for these functions. If MULTIBYTEP is nonzero, 8-bit codes 151 characters or invalid sequence) return 0.
117 of the range 0x80..0x9F are in multibyte form. */ 152
153 It also resets some bits of an integer pointed by MASK. The macros
154 CATEGORY_MASK_XXX specifies each bit of this integer.
155
156 Below is the template of these functions. */
157
118 #if 0 158 #if 0
119 int 159 static int
120 detect_coding_emacs_mule (src, src_end, multibytep) 160 detect_coding_XXX (coding, mask)
121 unsigned char *src, *src_end; 161 struct coding_system *coding;
122 int multibytep; 162 int *mask;
123 { 163 {
124 ... 164 unsigned char *src = coding->source;
165 unsigned char *src_end = coding->source + coding->src_bytes;
166 int multibytep = coding->src_multibyte;
167 int c;
168 int found = 0;
169 ...;
170
171 while (1)
172 {
173 /* Get one byte from the source. If the souce is exausted, jump
174 to no_more_source:. */
175 ONE_MORE_BYTE (c);
176 /* Check if it conforms to XXX. If not, break the loop. */
177 }
178 /* As the data is invalid for XXX, reset a proper bits. */
179 *mask &= ~CODING_CATEGORY_XXX;
180 return 0;
181 no_more_source:
182 /* The source exausted. */
183 if (!found)
184 /* ASCII characters only. */
185 return 0;
186 /* Some data should be decoded into non-ASCII characters. */
187 *mask &= CODING_CATEGORY_XXX;
188 return 1;
125 } 189 }
126 #endif 190 #endif
127 191
128 /*** GENERAL NOTES on `decode_coding_XXX ()' functions *** 192 /*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
129 193
130 These functions decode SRC_BYTES length of unibyte text at SOURCE 194 These functions decode a byte sequence specified as a source by
131 encoded in CODING to Emacs' internal format. The resulting 195 CODING. The resulting multibyte text goes to a place pointed to by
132 multibyte text goes to a place pointed to by DESTINATION, the length 196 CODING->charbuf, the length of which should not exceed
133 of which should not exceed DST_BYTES. 197 CODING->charbuf_size;
134 198
135 These functions set the information about original and decoded texts 199 These functions set the information of original and decoded texts in
136 in the members `produced', `produced_char', `consumed', and 200 CODING->consumed, CODING->consumed_char, and CODING->charbuf_used.
137 `consumed_char' of the structure *CODING. They also set the member 201 They also set CODING->result to one of CODING_RESULT_XXX indicating
138 `result' to one of CODING_FINISH_XXX indicating how the decoding 202 how the decoding is finished.
139 finished. 203
140 204 Below is the template of these functions. */
141 DST_BYTES zero means that the source area and destination area are 205
142 overlapped, which means that we can produce a decoded text until it
143 reaches the head of the not-yet-decoded source text.
144
145 Below is a template for these functions. */
146 #if 0 206 #if 0
147 static void 207 static void
148 decode_coding_XXX (coding, source, destination, src_bytes, dst_bytes) 208 decode_coding_XXXX (coding)
149 struct coding_system *coding; 209 struct coding_system *coding;
150 unsigned char *source, *destination; 210 {
151 int src_bytes, dst_bytes; 211 unsigned char *src = coding->source + coding->consumed;
152 { 212 unsigned char *src_end = coding->source + coding->src_bytes;
153 ... 213 /* SRC_BASE remembers the start position in source in each loop.
214 The loop will be exited when there's not enough source code, or
215 when there's no room in CHARBUF for a decoded character. */
216 unsigned char *src_base;
217 /* A buffer to produce decoded characters. */
218 int *charbuf = coding->charbuf;
219 int *charbuf_end = charbuf + coding->charbuf_size;
220 int multibytep = coding->src_multibyte;
221
222 while (1)
223 {
224 src_base = src;
225 if (charbuf < charbuf_end)
226 /* No more room to produce a decoded character. */
227 break;
228 ONE_MORE_BYTE (c);
229 /* Decode it. */
230 }
231
232 no_more_source:
233 if (src_base < src_end
234 && coding->mode & CODING_MODE_LAST_BLOCK)
235 /* If the source ends by partial bytes to construct a character,
236 treat them as eight-bit raw data. */
237 while (src_base < src_end && charbuf < charbuf_end)
238 *charbuf++ = *src_base++;
239 /* Remember how many bytes and characters we consumed. If the
240 source is multibyte, the bytes and chars are not identical. */
241 coding->consumed = coding->consumed_char = src_base - coding->source;
242 /* Remember how many characters we produced. */
243 coding->charbuf_used = charbuf - coding->charbuf;
154 } 244 }
155 #endif 245 #endif
156 246
157 /*** GENERAL NOTES on `encode_coding_XXX ()' functions *** 247 /*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
158 248
159 These functions encode SRC_BYTES length text at SOURCE from Emacs' 249 These functions encode SRC_BYTES length text at SOURCE of Emacs'
160 internal multibyte format to CODING. The resulting unibyte text 250 internal multibyte format by CODING. The resulting byte sequence
161 goes to a place pointed to by DESTINATION, the length of which 251 goes to a place pointed to by DESTINATION, the length of which
162 should not exceed DST_BYTES. 252 should not exceed DST_BYTES.
163 253
164 These functions set the information about original and encoded texts 254 These functions set the information of original and encoded texts in
165 in the members `produced', `produced_char', `consumed', and 255 the members produced, produced_char, consumed, and consumed_char of
166 `consumed_char' of the structure *CODING. They also set the member 256 the structure *CODING. They also set the member result to one of
167 `result' to one of CODING_FINISH_XXX indicating how the encoding 257 CODING_RESULT_XXX indicating how the encoding finished.
168 finished. 258
169 259 DST_BYTES zero means that source area and destination area are
170 DST_BYTES zero means that the source area and destination area are 260 overlapped, which means that we can produce a encoded text until it
171 overlapped, which means that we can produce encoded text until it 261 reaches at the head of not-yet-encoded source text.
172 reaches at the head of the not-yet-encoded source text. 262
173 263 Below is a template of these functions. */
174 Below is a template for these functions. */
175 #if 0 264 #if 0
176 static void 265 static void
177 encode_coding_XXX (coding, source, destination, src_bytes, dst_bytes) 266 encode_coding_XXX (coding)
178 struct coding_system *coding; 267 struct coding_system *coding;
179 unsigned char *source, *destination; 268 {
180 int src_bytes, dst_bytes; 269 int multibytep = coding->dst_multibyte;
181 { 270 int *charbuf = coding->charbuf;
182 ... 271 int *charbuf_end = charbuf->charbuf + coding->charbuf_used;
272 unsigned char *dst = coding->destination + coding->produced;
273 unsigned char *dst_end = coding->destination + coding->dst_bytes;
274 unsigned char *adjusted_dst_end = dst_end - _MAX_BYTES_PRODUCED_IN_LOOP_;
275 int produced_chars = 0;
276
277 for (; charbuf < charbuf_end && dst < adjusted_dst_end; charbuf++)
278 {
279 int c = *charbuf;
280 /* Encode C into DST, and increment DST. */
281 }
282 label_no_more_destination:
283 /* How many chars and bytes we produced. */
284 coding->produced_char += produced_chars;
285 coding->produced = dst - coding->destination;
183 } 286 }
184 #endif 287 #endif
185
186 /*** COMMONLY USED MACROS ***/
187
188 /* The following two macros ONE_MORE_BYTE and TWO_MORE_BYTES safely
189 get one, two, and three bytes from the source text respectively.
190 If there are not enough bytes in the source, they jump to
191 `label_end_of_loop'. The caller should set variables `coding',
192 `src' and `src_end' to appropriate pointer in advance. These
193 macros are called from decoding routines `decode_coding_XXX', thus
194 it is assumed that the source text is unibyte. */
195
196 #define ONE_MORE_BYTE(c1) \
197 do { \
198 if (src >= src_end) \
199 { \
200 coding->result = CODING_FINISH_INSUFFICIENT_SRC; \
201 goto label_end_of_loop; \
202 } \
203 c1 = *src++; \
204 } while (0)
205
206 #define TWO_MORE_BYTES(c1, c2) \
207 do { \
208 if (src + 1 >= src_end) \
209 { \
210 coding->result = CODING_FINISH_INSUFFICIENT_SRC; \
211 goto label_end_of_loop; \
212 } \
213 c1 = *src++; \
214 c2 = *src++; \
215 } while (0)
216
217
218 /* Like ONE_MORE_BYTE, but 8-bit bytes of data at SRC are in multibyte
219 form if MULTIBYTEP is nonzero. */
220
221 #define ONE_MORE_BYTE_CHECK_MULTIBYTE(c1, multibytep) \
222 do { \
223 if (src >= src_end) \
224 { \
225 coding->result = CODING_FINISH_INSUFFICIENT_SRC; \
226 goto label_end_of_loop; \
227 } \
228 c1 = *src++; \
229 if (multibytep && c1 == LEADING_CODE_8_BIT_CONTROL) \
230 c1 = *src++ - 0x20; \
231 } while (0)
232
233 /* Set C to the next character at the source text pointed by `src'.
234 If there are not enough characters in the source, jump to
235 `label_end_of_loop'. The caller should set variables `coding'
236 `src', `src_end', and `translation_table' to appropriate pointers
237 in advance. This macro is used in encoding routines
238 `encode_coding_XXX', thus it assumes that the source text is in
239 multibyte form except for 8-bit characters. 8-bit characters are
240 in multibyte form if coding->src_multibyte is nonzero, else they
241 are represented by a single byte. */
242
243 #define ONE_MORE_CHAR(c) \
244 do { \
245 int len = src_end - src; \
246 int bytes; \
247 if (len <= 0) \
248 { \
249 coding->result = CODING_FINISH_INSUFFICIENT_SRC; \
250 goto label_end_of_loop; \
251 } \
252 if (coding->src_multibyte \
253 || UNIBYTE_STR_AS_MULTIBYTE_P (src, len, bytes)) \
254 c = STRING_CHAR_AND_LENGTH (src, len, bytes); \
255 else \
256 c = *src, bytes = 1; \
257 if (!NILP (translation_table)) \
258 c = translate_char (translation_table, c, -1, 0, 0); \
259 src += bytes; \
260 } while (0)
261
262
263 /* Produce a multibyte form of character C to `dst'. Jump to
264 `label_end_of_loop' if there's not enough space at `dst'.
265
266 If we are now in the middle of a composition sequence, the decoded
267 character may be ALTCHAR (for the current composition). In that
268 case, the character goes to coding->cmp_data->data instead of
269 `dst'.
270
271 This macro is used in decoding routines. */
272
273 #define EMIT_CHAR(c) \
274 do { \
275 if (! COMPOSING_P (coding) \
276 || coding->composing == COMPOSITION_RELATIVE \
277 || coding->composing == COMPOSITION_WITH_RULE) \
278 { \
279 int bytes = CHAR_BYTES (c); \
280 if ((dst + bytes) > (dst_bytes ? dst_end : src)) \
281 { \
282 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
283 goto label_end_of_loop; \
284 } \
285 dst += CHAR_STRING (c, dst); \
286 coding->produced_char++; \
287 } \
288 \
289 if (COMPOSING_P (coding) \
290 && coding->composing != COMPOSITION_RELATIVE) \
291 { \
292 CODING_ADD_COMPOSITION_COMPONENT (coding, c); \
293 coding->composition_rule_follows \
294 = coding->composing != COMPOSITION_WITH_ALTCHARS; \
295 } \
296 } while (0)
297
298
299 #define EMIT_ONE_BYTE(c) \
300 do { \
301 if (dst >= (dst_bytes ? dst_end : src)) \
302 { \
303 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
304 goto label_end_of_loop; \
305 } \
306 *dst++ = c; \
307 } while (0)
308
309 #define EMIT_TWO_BYTES(c1, c2) \
310 do { \
311 if (dst + 2 > (dst_bytes ? dst_end : src)) \
312 { \
313 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
314 goto label_end_of_loop; \
315 } \
316 *dst++ = c1, *dst++ = c2; \
317 } while (0)
318
319 #define EMIT_BYTES(from, to) \
320 do { \
321 if (dst + (to - from) > (dst_bytes ? dst_end : src)) \
322 { \
323 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
324 goto label_end_of_loop; \
325 } \
326 while (from < to) \
327 *dst++ = *from++; \
328 } while (0)
329 288
330 289
331 /*** 1. Preamble ***/ 290 /*** 1. Preamble ***/
332 291
333 #ifdef emacs
334 #include <config.h> 292 #include <config.h>
335 #endif
336
337 #include <stdio.h> 293 #include <stdio.h>
338
339 #ifdef emacs
340 294
341 #include "lisp.h" 295 #include "lisp.h"
342 #include "buffer.h" 296 #include "buffer.h"
297 #include "character.h"
343 #include "charset.h" 298 #include "charset.h"
299 #include "ccl.h"
344 #include "composite.h" 300 #include "composite.h"
345 #include "ccl.h"
346 #include "coding.h" 301 #include "coding.h"
347 #include "window.h" 302 #include "window.h"
348 303
349 #else /* not emacs */ 304 Lisp_Object Vcoding_system_hash_table;
350 305
351 #include "mulelib.h" 306 Lisp_Object Qcoding_system, Qcoding_aliases, Qeol_type;
352 307 Lisp_Object Qunix, Qdos, Qmac;
353 #endif /* not emacs */
354
355 Lisp_Object Qcoding_system, Qeol_type;
356 Lisp_Object Qbuffer_file_coding_system; 308 Lisp_Object Qbuffer_file_coding_system;
357 Lisp_Object Qpost_read_conversion, Qpre_write_conversion; 309 Lisp_Object Qpost_read_conversion, Qpre_write_conversion;
310 Lisp_Object Qdefault_char;
358 Lisp_Object Qno_conversion, Qundecided; 311 Lisp_Object Qno_conversion, Qundecided;
312 Lisp_Object Qcharset, Qiso_2022, Qutf_8, Qutf_16, Qshift_jis, Qbig5;
313 Lisp_Object Qutf_16_be_nosig, Qutf_16_be, Qutf_16_le_nosig, Qutf_16_le;
314 Lisp_Object Qsignature, Qendian, Qbig, Qlittle;
359 Lisp_Object Qcoding_system_history; 315 Lisp_Object Qcoding_system_history;
360 Lisp_Object Qsafe_chars;
361 Lisp_Object Qvalid_codes; 316 Lisp_Object Qvalid_codes;
362 317
363 extern Lisp_Object Qinsert_file_contents, Qwrite_region; 318 extern Lisp_Object Qinsert_file_contents, Qwrite_region;
364 Lisp_Object Qcall_process, Qcall_process_region, Qprocess_argument; 319 Lisp_Object Qcall_process, Qcall_process_region, Qprocess_argument;
365 Lisp_Object Qstart_process, Qopen_network_stream; 320 Lisp_Object Qstart_process, Qopen_network_stream;
370 /* Mnemonic string for each format of end-of-line. */ 325 /* Mnemonic string for each format of end-of-line. */
371 Lisp_Object eol_mnemonic_unix, eol_mnemonic_dos, eol_mnemonic_mac; 326 Lisp_Object eol_mnemonic_unix, eol_mnemonic_dos, eol_mnemonic_mac;
372 /* Mnemonic string to indicate format of end-of-line is not yet 327 /* Mnemonic string to indicate format of end-of-line is not yet
373 decided. */ 328 decided. */
374 Lisp_Object eol_mnemonic_undecided; 329 Lisp_Object eol_mnemonic_undecided;
375
376 /* Format of end-of-line decided by system. This is CODING_EOL_LF on
377 Unix, CODING_EOL_CRLF on DOS/Windows, and CODING_EOL_CR on Mac. */
378 int system_eol_type;
379 330
380 #ifdef emacs 331 #ifdef emacs
381 332
382 Lisp_Object Vcoding_system_list, Vcoding_system_alist; 333 Lisp_Object Vcoding_system_list, Vcoding_system_alist;
383 334
417 struct coding_system safe_terminal_coding; 368 struct coding_system safe_terminal_coding;
418 369
419 /* Coding system of what is sent from terminal keyboard. */ 370 /* Coding system of what is sent from terminal keyboard. */
420 struct coding_system keyboard_coding; 371 struct coding_system keyboard_coding;
421 372
422 /* Default coding system to be used to write a file. */
423 struct coding_system default_buffer_file_coding;
424
425 Lisp_Object Vfile_coding_system_alist; 373 Lisp_Object Vfile_coding_system_alist;
426 Lisp_Object Vprocess_coding_system_alist; 374 Lisp_Object Vprocess_coding_system_alist;
427 Lisp_Object Vnetwork_coding_system_alist; 375 Lisp_Object Vnetwork_coding_system_alist;
428 376
429 Lisp_Object Vlocale_coding_system; 377 Lisp_Object Vlocale_coding_system;
430 378
431 #endif /* emacs */ 379 #endif /* emacs */
432
433 Lisp_Object Qcoding_category, Qcoding_category_index;
434
435 /* List of symbols `coding-category-xxx' ordered by priority. */
436 Lisp_Object Vcoding_category_list;
437
438 /* Table of coding categories (Lisp symbols). */
439 Lisp_Object Vcoding_category_table;
440
441 /* Table of names of symbol for each coding-category. */
442 char *coding_category_name[CODING_CATEGORY_IDX_MAX] = {
443 "coding-category-emacs-mule",
444 "coding-category-sjis",
445 "coding-category-iso-7",
446 "coding-category-iso-7-tight",
447 "coding-category-iso-8-1",
448 "coding-category-iso-8-2",
449 "coding-category-iso-7-else",
450 "coding-category-iso-8-else",
451 "coding-category-ccl",
452 "coding-category-big5",
453 "coding-category-utf-8",
454 "coding-category-utf-16-be",
455 "coding-category-utf-16-le",
456 "coding-category-raw-text",
457 "coding-category-binary"
458 };
459
460 /* Table of pointers to coding systems corresponding to each coding
461 categories. */
462 struct coding_system *coding_system_table[CODING_CATEGORY_IDX_MAX];
463
464 /* Table of coding category masks. Nth element is a mask for a coding
465 category of which priority is Nth. */
466 static
467 int coding_priorities[CODING_CATEGORY_IDX_MAX];
468 380
469 /* Flag to tell if we look up translation table on character code 381 /* Flag to tell if we look up translation table on character code
470 conversion. */ 382 conversion. */
471 Lisp_Object Venable_character_translation; 383 Lisp_Object Venable_character_translation;
472 /* Standard translation table to look up on decoding (reading). */ 384 /* Standard translation table to look up on decoding (reading). */
478 Lisp_Object Qtranslation_table_id; 390 Lisp_Object Qtranslation_table_id;
479 Lisp_Object Qtranslation_table_for_decode; 391 Lisp_Object Qtranslation_table_for_decode;
480 Lisp_Object Qtranslation_table_for_encode; 392 Lisp_Object Qtranslation_table_for_encode;
481 393
482 /* Alist of charsets vs revision number. */ 394 /* Alist of charsets vs revision number. */
483 Lisp_Object Vcharset_revision_alist; 395 static Lisp_Object Vcharset_revision_table;
484 396
485 /* Default coding systems used for process I/O. */ 397 /* Default coding systems used for process I/O. */
486 Lisp_Object Vdefault_process_coding_system; 398 Lisp_Object Vdefault_process_coding_system;
487 399
488 /* Global flag to tell that we can't call post-read-conversion and 400 /* Global flag to tell that we can't call post-read-conversion and
493 405
494 /* Char-table containing safe coding systems of each character. */ 406 /* Char-table containing safe coding systems of each character. */
495 Lisp_Object Vchar_coding_system_table; 407 Lisp_Object Vchar_coding_system_table;
496 Lisp_Object Qchar_coding_system; 408 Lisp_Object Qchar_coding_system;
497 409
498 /* Return `safe-chars' property of coding system CODING. Don't check 410 /* Two special coding systems. */
499 validity of CODING. */ 411 Lisp_Object Vsjis_coding_system;
500 412 Lisp_Object Vbig5_coding_system;
501 Lisp_Object 413
502 coding_safe_chars (coding) 414
415 static int detect_coding_utf_8 P_ ((struct coding_system *, int *));
416 static void decode_coding_utf_8 P_ ((struct coding_system *));
417 static int encode_coding_utf_8 P_ ((struct coding_system *));
418
419 static int detect_coding_utf_16 P_ ((struct coding_system *, int *));
420 static void decode_coding_utf_16 P_ ((struct coding_system *));
421 static int encode_coding_utf_16 P_ ((struct coding_system *));
422
423 static int detect_coding_iso_2022 P_ ((struct coding_system *, int *));
424 static void decode_coding_iso_2022 P_ ((struct coding_system *));
425 static int encode_coding_iso_2022 P_ ((struct coding_system *));
426
427 static int detect_coding_emacs_mule P_ ((struct coding_system *, int *));
428 static void decode_coding_emacs_mule P_ ((struct coding_system *));
429 static int encode_coding_emacs_mule P_ ((struct coding_system *));
430
431 static int detect_coding_sjis P_ ((struct coding_system *, int *));
432 static void decode_coding_sjis P_ ((struct coding_system *));
433 static int encode_coding_sjis P_ ((struct coding_system *));
434
435 static int detect_coding_big5 P_ ((struct coding_system *, int *));
436 static void decode_coding_big5 P_ ((struct coding_system *));
437 static int encode_coding_big5 P_ ((struct coding_system *));
438
439 static int detect_coding_ccl P_ ((struct coding_system *, int *));
440 static void decode_coding_ccl P_ ((struct coding_system *));
441 static int encode_coding_ccl P_ ((struct coding_system *));
442
443 static void decode_coding_raw_text P_ ((struct coding_system *));
444 static int encode_coding_raw_text P_ ((struct coding_system *));
445
446
447 /* ISO2022 section */
448
449 #define CODING_ISO_INITIAL(coding, reg) \
450 (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \
451 coding_attr_iso_initial), \
452 reg)))
453
454
455 #define CODING_ISO_REQUEST(coding, charset_id) \
456 ((charset_id <= (coding)->max_charset_id \
457 ? (coding)->safe_charsets[charset_id] \
458 : -1))
459
460
461 #define CODING_ISO_FLAGS(coding) \
462 ((coding)->spec.iso_2022.flags)
463 #define CODING_ISO_DESIGNATION(coding, reg) \
464 ((coding)->spec.iso_2022.current_designation[reg])
465 #define CODING_ISO_INVOCATION(coding, plane) \
466 ((coding)->spec.iso_2022.current_invocation[plane])
467 #define CODING_ISO_SINGLE_SHIFTING(coding) \
468 ((coding)->spec.iso_2022.single_shifting)
469 #define CODING_ISO_BOL(coding) \
470 ((coding)->spec.iso_2022.bol)
471 #define CODING_ISO_INVOKED_CHARSET(coding, plane) \
472 CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane)))
473
474 /* Control characters of ISO2022. */
475 /* code */ /* function */
476 #define ISO_CODE_LF 0x0A /* line-feed */
477 #define ISO_CODE_CR 0x0D /* carriage-return */
478 #define ISO_CODE_SO 0x0E /* shift-out */
479 #define ISO_CODE_SI 0x0F /* shift-in */
480 #define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */
481 #define ISO_CODE_ESC 0x1B /* escape */
482 #define ISO_CODE_SS2 0x8E /* single-shift-2 */
483 #define ISO_CODE_SS3 0x8F /* single-shift-3 */
484 #define ISO_CODE_CSI 0x9B /* control-sequence-introducer */
485
486 /* All code (1-byte) of ISO2022 is classified into one of the
487 followings. */
488 enum iso_code_class_type
489 {
490 ISO_control_0, /* Control codes in the range
491 0x00..0x1F and 0x7F, except for the
492 following 5 codes. */
493 ISO_carriage_return, /* ISO_CODE_CR (0x0D) */
494 ISO_shift_out, /* ISO_CODE_SO (0x0E) */
495 ISO_shift_in, /* ISO_CODE_SI (0x0F) */
496 ISO_single_shift_2_7, /* ISO_CODE_SS2_7 (0x19) */
497 ISO_escape, /* ISO_CODE_SO (0x1B) */
498 ISO_control_1, /* Control codes in the range
499 0x80..0x9F, except for the
500 following 3 codes. */
501 ISO_single_shift_2, /* ISO_CODE_SS2 (0x8E) */
502 ISO_single_shift_3, /* ISO_CODE_SS3 (0x8F) */
503 ISO_control_sequence_introducer, /* ISO_CODE_CSI (0x9B) */
504 ISO_0x20_or_0x7F, /* Codes of the values 0x20 or 0x7F. */
505 ISO_graphic_plane_0, /* Graphic codes in the range 0x21..0x7E. */
506 ISO_0xA0_or_0xFF, /* Codes of the values 0xA0 or 0xFF. */
507 ISO_graphic_plane_1 /* Graphic codes in the range 0xA1..0xFE. */
508 };
509
510 /** The macros CODING_ISO_FLAG_XXX defines a flag bit of the
511 `iso-flags' attribute of an iso2022 coding system. */
512
513 /* If set, produce long-form designation sequence (e.g. ESC $ ( A)
514 instead of the correct short-form sequence (e.g. ESC $ A). */
515 #define CODING_ISO_FLAG_LONG_FORM 0x0001
516
517 /* If set, reset graphic planes and registers at end-of-line to the
518 initial state. */
519 #define CODING_ISO_FLAG_RESET_AT_EOL 0x0002
520
521 /* If set, reset graphic planes and registers before any control
522 characters to the initial state. */
523 #define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004
524
525 /* If set, encode by 7-bit environment. */
526 #define CODING_ISO_FLAG_SEVEN_BITS 0x0008
527
528 /* If set, use locking-shift function. */
529 #define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010
530
531 /* If set, use single-shift function. Overwrite
532 CODING_ISO_FLAG_LOCKING_SHIFT. */
533 #define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020
534
535 /* If set, use designation escape sequence. */
536 #define CODING_ISO_FLAG_DESIGNATION 0x0040
537
538 /* If set, produce revision number sequence. */
539 #define CODING_ISO_FLAG_REVISION 0x0080
540
541 /* If set, produce ISO6429's direction specifying sequence. */
542 #define CODING_ISO_FLAG_DIRECTION 0x0100
543
544 /* If set, assume designation states are reset at beginning of line on
545 output. */
546 #define CODING_ISO_FLAG_INIT_AT_BOL 0x0200
547
548 /* If set, designation sequence should be placed at beginning of line
549 on output. */
550 #define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400
551
552 /* If set, do not encode unsafe charactes on output. */
553 #define CODING_ISO_FLAG_SAFE 0x0800
554
555 /* If set, extra latin codes (128..159) are accepted as a valid code
556 on input. */
557 #define CODING_ISO_FLAG_LATIN_EXTRA 0x1000
558
559 #define CODING_ISO_FLAG_COMPOSITION 0x2000
560
561 #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000
562
563 #define CODING_ISO_FLAG_FULL_SUPPORT 0x8000
564
565 /* A character to be produced on output if encoding of the original
566 character is prohibited by CODING_ISO_FLAG_SAFE. */
567 #define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?'
568
569
570 /* UTF-16 section */
571 #define CODING_UTF_16_BOM(coding) \
572 ((coding)->spec.utf_16.bom)
573
574 #define CODING_UTF_16_ENDIAN(coding) \
575 ((coding)->spec.utf_16.endian)
576
577 #define CODING_UTF_16_SURROGATE(coding) \
578 ((coding)->spec.utf_16.surrogate)
579
580
581 /* CCL section */
582 #define CODING_CCL_DECODER(coding) \
583 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder)
584 #define CODING_CCL_ENCODER(coding) \
585 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder)
586 #define CODING_CCL_VALIDS(coding) \
587 (XSTRING (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids)) \
588 ->data)
589
590 /* Index for each coding category in `coding_category_table' */
591
592 enum coding_category
593 {
594 coding_category_iso_7,
595 coding_category_iso_7_tight,
596 coding_category_iso_8_1,
597 coding_category_iso_8_2,
598 coding_category_iso_7_else,
599 coding_category_iso_8_else,
600 coding_category_utf_8,
601 coding_category_utf_16_auto,
602 coding_category_utf_16_be,
603 coding_category_utf_16_le,
604 coding_category_utf_16_be_nosig,
605 coding_category_utf_16_le_nosig,
606 coding_category_charset,
607 coding_category_sjis,
608 coding_category_big5,
609 coding_category_ccl,
610 coding_category_emacs_mule,
611 /* All above are targets of code detection. */
612 coding_category_raw_text,
613 coding_category_undecided,
614 coding_category_max
615 };
616
617 /* Definitions of flag bits used in detect_coding_XXXX. */
618 #define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7)
619 #define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight)
620 #define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1)
621 #define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2)
622 #define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else)
623 #define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else)
624 #define CATEGORY_MASK_UTF_8 (1 << coding_category_utf_8)
625 #define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be)
626 #define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le)
627 #define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig)
628 #define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig)
629 #define CATEGORY_MASK_CHARSET (1 << coding_category_charset)
630 #define CATEGORY_MASK_SJIS (1 << coding_category_sjis)
631 #define CATEGORY_MASK_BIG5 (1 << coding_category_big5)
632 #define CATEGORY_MASK_CCL (1 << coding_category_ccl)
633 #define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule)
634
635 /* This value is returned if detect_coding_mask () find nothing other
636 than ASCII characters. */
637 #define CATEGORY_MASK_ANY \
638 (CATEGORY_MASK_ISO_7 \
639 | CATEGORY_MASK_ISO_7_TIGHT \
640 | CATEGORY_MASK_ISO_8_1 \
641 | CATEGORY_MASK_ISO_8_2 \
642 | CATEGORY_MASK_ISO_7_ELSE \
643 | CATEGORY_MASK_ISO_8_ELSE \
644 | CATEGORY_MASK_UTF_8 \
645 | CATEGORY_MASK_UTF_16_BE \
646 | CATEGORY_MASK_UTF_16_LE \
647 | CATEGORY_MASK_UTF_16_BE_NOSIG \
648 | CATEGORY_MASK_UTF_16_LE_NOSIG \
649 | CATEGORY_MASK_CHARSET \
650 | CATEGORY_MASK_SJIS \
651 | CATEGORY_MASK_BIG5 \
652 | CATEGORY_MASK_CCL \
653 | CATEGORY_MASK_EMACS_MULE)
654
655
656 #define CATEGORY_MASK_ISO_7BIT \
657 (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT)
658
659 #define CATEGORY_MASK_ISO_8BIT \
660 (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2)
661
662 #define CATEGORY_MASK_ISO_ELSE \
663 (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE)
664
665 #define CATEGORY_MASK_ISO_ESCAPE \
666 (CATEGORY_MASK_ISO_7 \
667 | CATEGORY_MASK_ISO_7_TIGHT \
668 | CATEGORY_MASK_ISO_7_ELSE \
669 | CATEGORY_MASK_ISO_8_ELSE)
670
671 #define CATEGORY_MASK_ISO \
672 ( CATEGORY_MASK_ISO_7BIT \
673 | CATEGORY_MASK_ISO_8BIT \
674 | CATEGORY_MASK_ISO_ELSE)
675
676 #define CATEGORY_MASK_UTF_16 \
677 (CATEGORY_MASK_UTF_16_BE \
678 | CATEGORY_MASK_UTF_16_LE \
679 | CATEGORY_MASK_UTF_16_BE_NOSIG \
680 | CATEGORY_MASK_UTF_16_LE_NOSIG)
681
682
683 /* List of symbols `coding-category-xxx' ordered by priority. This
684 variable is exposed to Emacs Lisp. */
685 static Lisp_Object Vcoding_category_list;
686
687 /* Table of coding categories (Lisp symbols). This variable is for
688 internal use oly. */
689 static Lisp_Object Vcoding_category_table;
690
691 /* Table of coding-categories ordered by priority. */
692 static enum coding_category coding_priorities[coding_category_max];
693
694 /* Nth element is a coding context for the coding system bound to the
695 Nth coding category. */
696 static struct coding_system coding_categories[coding_category_max];
697
698 static int detected_mask[coding_category_raw_text] =
699 { CATEGORY_MASK_ISO,
700 CATEGORY_MASK_ISO,
701 CATEGORY_MASK_ISO,
702 CATEGORY_MASK_ISO,
703 CATEGORY_MASK_ISO,
704 CATEGORY_MASK_ISO,
705 CATEGORY_MASK_UTF_8,
706 CATEGORY_MASK_UTF_16,
707 CATEGORY_MASK_UTF_16,
708 CATEGORY_MASK_UTF_16,
709 CATEGORY_MASK_UTF_16,
710 CATEGORY_MASK_UTF_16,
711 CATEGORY_MASK_CHARSET,
712 CATEGORY_MASK_SJIS,
713 CATEGORY_MASK_BIG5,
714 CATEGORY_MASK_CCL,
715 CATEGORY_MASK_EMACS_MULE
716 };
717
718 /*** Commonly used macros and functions ***/
719
720 #ifndef min
721 #define min(a, b) ((a) < (b) ? (a) : (b))
722 #endif
723 #ifndef max
724 #define max(a, b) ((a) > (b) ? (a) : (b))
725 #endif
726
727 #define CODING_GET_INFO(coding, attrs, eol_type, charset_list) \
728 do { \
729 attrs = CODING_ID_ATTRS (coding->id); \
730 eol_type = CODING_ID_EOL_TYPE (coding->id); \
731 if (VECTORP (eol_type)) \
732 eol_type = Qunix; \
733 charset_list = CODING_ATTR_CHARSET_LIST (attrs); \
734 } while (0)
735
736
737 /* Safely get one byte from the source text pointed by SRC which ends
738 at SRC_END, and set C to that byte. If there are not enough bytes
739 in the source, it jumps to `no_more_source'. The caller
740 should declare and set these variables appropriately in advance:
741 src, src_end, multibytep
742 */
743
744 #define ONE_MORE_BYTE(c) \
745 do { \
746 if (src == src_end) \
747 { \
748 if (src_base < src) \
749 coding->result = CODING_RESULT_INSUFFICIENT_SRC; \
750 goto no_more_source; \
751 } \
752 c = *src++; \
753 if (multibytep && (c & 0x80)) \
754 { \
755 if ((c & 0xFE) != 0xC0) \
756 error ("Undecodable char found"); \
757 c = ((c & 1) << 6) | *src++; \
758 } \
759 consumed_chars++; \
760 } while (0)
761
762
763 #define ONE_MORE_BYTE_NO_CHECK(c) \
764 do { \
765 c = *src++; \
766 if (multibytep && (c & 0x80)) \
767 { \
768 if ((c & 0xFE) != 0xC0) \
769 error ("Undecodable char found"); \
770 c = ((c & 1) << 6) | *src++; \
771 } \
772 } while (0)
773
774
775 /* Store a byte C in the place pointed by DST and increment DST to the
776 next free point, and increment PRODUCED_CHARS. The caller should
777 assure that C is 0..127, and declare and set the variable `dst'
778 appropriately in advance.
779 */
780
781
782 #define EMIT_ONE_ASCII_BYTE(c) \
783 do { \
784 produced_chars++; \
785 *dst++ = (c); \
786 } while (0)
787
788
789 /* Like EMIT_ONE_ASCII_BYTE byt store two bytes; C1 and C2. */
790
791 #define EMIT_TWO_ASCII_BYTES(c1, c2) \
792 do { \
793 produced_chars += 2; \
794 *dst++ = (c1), *dst++ = (c2); \
795 } while (0)
796
797
798 /* Store a byte C in the place pointed by DST and increment DST to the
799 next free point, and increment PRODUCED_CHARS. If MULTIBYTEP is
800 nonzero, store in an appropriate multibyte from. The caller should
801 declare and set the variables `dst' and `multibytep' appropriately
802 in advance. */
803
804 #define EMIT_ONE_BYTE(c) \
805 do { \
806 produced_chars++; \
807 if (multibytep) \
808 { \
809 int ch = (c); \
810 if (ch >= 0x80) \
811 ch = BYTE8_TO_CHAR (ch); \
812 CHAR_STRING_ADVANCE (ch, dst); \
813 } \
814 else \
815 *dst++ = (c); \
816 } while (0)
817
818
819 /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */
820
821 #define EMIT_TWO_BYTES(c1, c2) \
822 do { \
823 produced_chars += 2; \
824 if (multibytep) \
825 { \
826 CHAR_STRING_ADVANCE ((int) (c1), dst); \
827 CHAR_STRING_ADVANCE ((int) (c2), dst); \
828 } \
829 else \
830 { \
831 *dst++ = (c1); \
832 *dst++ = (c2); \
833 } \
834 } while (0)
835
836
837 #define EMIT_THREE_BYTES(c1, c2, c3) \
838 do { \
839 EMIT_ONE_BYTE (c1); \
840 EMIT_TWO_BYTES (c2, c3); \
841 } while (0)
842
843
844 #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \
845 do { \
846 EMIT_TWO_BYTES (c1, c2); \
847 EMIT_TWO_BYTES (c3, c4); \
848 } while (0)
849
850
851 #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \
852 do { \
853 charset_map_loaded = 0; \
854 c = DECODE_CHAR (charset, code); \
855 if (charset_map_loaded) \
856 { \
857 unsigned char *orig = coding->source; \
858 EMACS_INT offset; \
859 \
860 coding_set_source (coding); \
861 offset = coding->source - orig; \
862 src += offset; \
863 src_base += offset; \
864 src_end += offset; \
865 } \
866 } while (0)
867
868
869 #define ASSURE_DESTINATION(bytes) \
870 do { \
871 if (dst + (bytes) >= dst_end) \
872 { \
873 int more_bytes = charbuf_end - charbuf + (bytes); \
874 \
875 dst = alloc_destination (coding, more_bytes, dst); \
876 dst_end = coding->destination + coding->dst_bytes; \
877 } \
878 } while (0)
879
880
881
882 static void
883 coding_set_source (coding)
503 struct coding_system *coding; 884 struct coding_system *coding;
504 { 885 {
505 Lisp_Object coding_spec, plist, safe_chars; 886 if (BUFFERP (coding->src_object))
506 887 {
507 coding_spec = Fget (coding->symbol, Qcoding_system); 888 if (coding->src_pos < 0)
508 plist = XVECTOR (coding_spec)->contents[3]; 889 coding->source = GAP_END_ADDR + coding->src_pos_byte;
509 safe_chars = Fplist_get (XVECTOR (coding_spec)->contents[3], Qsafe_chars); 890 else
510 return (CHAR_TABLE_P (safe_chars) ? safe_chars : Qt); 891 {
511 } 892 if (coding->src_pos < GPT
512 893 && coding->src_pos + coding->src_chars >= GPT)
513 #define CODING_SAFE_CHAR_P(safe_chars, c) \ 894 move_gap_both (coding->src_pos, coding->src_pos_byte);
514 (EQ (safe_chars, Qt) || !NILP (CHAR_TABLE_REF (safe_chars, c))) 895 coding->source = BYTE_POS_ADDR (coding->src_pos_byte);
896 }
897 }
898 else if (STRINGP (coding->src_object))
899 {
900 coding->source = (XSTRING (coding->src_object)->data
901 + coding->src_pos_byte);
902 }
903 else
904 /* Otherwise, the source is C string and is never relocated
905 automatically. Thus we don't have to update anything. */
906 ;
907 }
908
909 static void
910 coding_set_destination (coding)
911 struct coding_system *coding;
912 {
913 if (BUFFERP (coding->dst_object))
914 {
915 /* We are sure that coding->dst_pos_byte is before the gap of the
916 buffer. */
917 coding->destination = (BUF_BEG_ADDR (XBUFFER (coding->dst_object))
918 + coding->dst_pos_byte - 1);
919 if (coding->src_pos < 0)
920 /* The source and destination is in the same buffer. */
921 coding->dst_bytes = (GAP_END_ADDR
922 - (coding->src_bytes - coding->consumed)
923 - coding->destination);
924 else
925 coding->dst_bytes = (BUF_GAP_END_ADDR (XBUFFER (coding->dst_object))
926 - coding->destination);
927 }
928 else
929 /* Otherwise, the destination is C string and is never relocated
930 automatically. Thus we don't have to update anything. */
931 ;
932 }
933
934
935 static void
936 coding_alloc_by_realloc (coding, bytes)
937 struct coding_system *coding;
938 EMACS_INT bytes;
939 {
940 coding->destination = (unsigned char *) xrealloc (coding->destination,
941 coding->dst_bytes + bytes);
942 coding->dst_bytes += bytes;
943 }
944
945 static void
946 coding_alloc_by_making_gap (coding, bytes)
947 struct coding_system *coding;
948 EMACS_INT bytes;
949 {
950 Lisp_Object this_buffer;
951
952 this_buffer = Fcurrent_buffer ();
953 if (EQ (this_buffer, coding->dst_object))
954 {
955 EMACS_INT add = coding->src_bytes - coding->consumed;
956
957 GAP_SIZE -= add; ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add;
958 make_gap (bytes);
959 GAP_SIZE += add; ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add;
960 }
961 else
962 {
963 set_buffer_internal (XBUFFER (coding->dst_object));
964 make_gap (bytes);
965 set_buffer_internal (XBUFFER (this_buffer));
966 }
967 }
968
969
970 static unsigned char *
971 alloc_destination (coding, nbytes, dst)
972 struct coding_system *coding;
973 int nbytes;
974 unsigned char *dst;
975 {
976 EMACS_INT offset = dst - coding->destination;
977
978 if (BUFFERP (coding->dst_object))
979 coding_alloc_by_making_gap (coding, nbytes);
980 else
981 coding_alloc_by_realloc (coding, nbytes);
982 coding->result = CODING_RESULT_SUCCESS;
983 coding_set_destination (coding);
984 dst = coding->destination + offset;
985 return dst;
986 }
515 987
516 988
517 /*** 2. Emacs internal format (emacs-mule) handlers ***/ 989 /*** 2. Emacs' internal format (emacs-utf-8) ***/
990
991
992
993
994 /*** 3. UTF-8 ***/
995
996 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
997 Check if a text is encoded in UTF-8. If it is, return
998 CATEGORY_MASK_UTF_8, else return 0. */
999
1000 #define UTF_8_1_OCTET_P(c) ((c) < 0x80)
1001 #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
1002 #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
1003 #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
1004 #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
1005 #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
1006
1007 static int
1008 detect_coding_utf_8 (coding, mask)
1009 struct coding_system *coding;
1010 int *mask;
1011 {
1012 unsigned char *src = coding->source, *src_base = src;
1013 unsigned char *src_end = coding->source + coding->src_bytes;
1014 int multibytep = coding->src_multibyte;
1015 int consumed_chars = 0;
1016 int found = 0;
1017
1018 /* A coding system of this category is always ASCII compatible. */
1019 src += coding->head_ascii;
1020
1021 while (1)
1022 {
1023 int c, c1, c2, c3, c4;
1024
1025 ONE_MORE_BYTE (c);
1026 if (UTF_8_1_OCTET_P (c))
1027 continue;
1028 ONE_MORE_BYTE (c1);
1029 if (! UTF_8_EXTRA_OCTET_P (c1))
1030 break;
1031 if (UTF_8_2_OCTET_LEADING_P (c))
1032 {
1033 found++;
1034 continue;
1035 }
1036 ONE_MORE_BYTE (c2);
1037 if (! UTF_8_EXTRA_OCTET_P (c2))
1038 break;
1039 if (UTF_8_3_OCTET_LEADING_P (c))
1040 {
1041 found++;
1042 continue;
1043 }
1044 ONE_MORE_BYTE (c3);
1045 if (! UTF_8_EXTRA_OCTET_P (c3))
1046 break;
1047 if (UTF_8_4_OCTET_LEADING_P (c))
1048 {
1049 found++;
1050 continue;
1051 }
1052 ONE_MORE_BYTE (c4);
1053 if (! UTF_8_EXTRA_OCTET_P (c4))
1054 break;
1055 if (UTF_8_5_OCTET_LEADING_P (c))
1056 {
1057 found++;
1058 continue;
1059 }
1060 break;
1061 }
1062 *mask &= ~CATEGORY_MASK_UTF_8;
1063 return 0;
1064
1065 no_more_source:
1066 if (! found)
1067 return 0;
1068 *mask &= CATEGORY_MASK_UTF_8;
1069 return 1;
1070 }
1071
1072
1073 static void
1074 decode_coding_utf_8 (coding)
1075 struct coding_system *coding;
1076 {
1077 unsigned char *src = coding->source + coding->consumed;
1078 unsigned char *src_end = coding->source + coding->src_bytes;
1079 unsigned char *src_base;
1080 int *charbuf = coding->charbuf;
1081 int *charbuf_end = charbuf + coding->charbuf_size;
1082 int consumed_chars = 0, consumed_chars_base;
1083 int multibytep = coding->src_multibyte;
1084 Lisp_Object attr, eol_type, charset_list;
1085
1086 CODING_GET_INFO (coding, attr, eol_type, charset_list);
1087
1088 while (1)
1089 {
1090 int c, c1, c2, c3, c4, c5;
1091
1092 src_base = src;
1093 consumed_chars_base = consumed_chars;
1094
1095 if (charbuf >= charbuf_end)
1096 break;
1097
1098 ONE_MORE_BYTE (c1);
1099 if (UTF_8_1_OCTET_P(c1))
1100 {
1101 c = c1;
1102 if (c == '\r')
1103 {
1104 if (EQ (eol_type, Qdos))
1105 {
1106 if (src == src_end)
1107 goto no_more_source;
1108 if (*src == '\n')
1109 ONE_MORE_BYTE (c);
1110 }
1111 else if (EQ (eol_type, Qmac))
1112 c = '\n';
1113 }
1114 }
1115 else
1116 {
1117 ONE_MORE_BYTE (c2);
1118 if (! UTF_8_EXTRA_OCTET_P (c2))
1119 goto invalid_code;
1120 if (UTF_8_2_OCTET_LEADING_P (c1))
1121 c = ((c1 & 0x1F) << 6) | (c2 & 0x3F);
1122 else
1123 {
1124 ONE_MORE_BYTE (c3);
1125 if (! UTF_8_EXTRA_OCTET_P (c3))
1126 goto invalid_code;
1127 if (UTF_8_3_OCTET_LEADING_P (c1))
1128 c = (((c1 & 0xF) << 12)
1129 | ((c2 & 0x3F) << 6) | (c3 & 0x3F));
1130 else
1131 {
1132 ONE_MORE_BYTE (c4);
1133 if (! UTF_8_EXTRA_OCTET_P (c4))
1134 goto invalid_code;
1135 if (UTF_8_4_OCTET_LEADING_P (c1))
1136 c = (((c1 & 0x7) << 18) | ((c2 & 0x3F) << 12)
1137 | ((c3 & 0x3F) << 6) | (c4 & 0x3F));
1138 else
1139 {
1140 ONE_MORE_BYTE (c5);
1141 if (! UTF_8_EXTRA_OCTET_P (c5))
1142 goto invalid_code;
1143 if (UTF_8_5_OCTET_LEADING_P (c1))
1144 {
1145 c = (((c1 & 0x3) << 24) | ((c2 & 0x3F) << 18)
1146 | ((c3 & 0x3F) << 12) | ((c4 & 0x3F) << 6)
1147 | (c5 & 0x3F));
1148 if (c > MAX_CHAR)
1149 goto invalid_code;
1150 }
1151 else
1152 goto invalid_code;
1153 }
1154 }
1155 }
1156 }
1157
1158 *charbuf++ = c;
1159 continue;
1160
1161 invalid_code:
1162 src = src_base;
1163 consumed_chars = consumed_chars_base;
1164 ONE_MORE_BYTE (c);
1165 *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
1166 coding->errors++;
1167 }
1168
1169 no_more_source:
1170 coding->consumed_char += consumed_chars_base;
1171 coding->consumed = src_base - coding->source;
1172 coding->charbuf_used = charbuf - coding->charbuf;
1173 }
1174
1175
1176 static int
1177 encode_coding_utf_8 (coding)
1178 struct coding_system *coding;
1179 {
1180 int multibytep = coding->dst_multibyte;
1181 int *charbuf = coding->charbuf;
1182 int *charbuf_end = charbuf + coding->charbuf_used;
1183 unsigned char *dst = coding->destination + coding->produced;
1184 unsigned char *dst_end = coding->destination + coding->dst_bytes;
1185 int produced_chars;
1186 int c;
1187
1188 if (multibytep)
1189 {
1190 int safe_room = MAX_MULTIBYTE_LENGTH * 2;
1191
1192 while (charbuf < charbuf_end)
1193 {
1194 unsigned char str[MAX_MULTIBYTE_LENGTH], *p, *pend = str;
1195
1196 ASSURE_DESTINATION (safe_room);
1197 c = *charbuf++;
1198 CHAR_STRING_ADVANCE (c, pend);
1199 for (p = str; p < pend; p++)
1200 EMIT_ONE_BYTE (*p);
1201 }
1202 }
1203 else
1204 {
1205 int safe_room = MAX_MULTIBYTE_LENGTH;
1206
1207 while (charbuf < charbuf_end)
1208 {
1209 ASSURE_DESTINATION (safe_room);
1210 c = *charbuf++;
1211 dst += CHAR_STRING (c, dst);
1212 produced_chars++;
1213 }
1214 }
1215 coding->result = CODING_RESULT_SUCCESS;
1216 coding->produced_char += produced_chars;
1217 coding->produced = dst - coding->destination;
1218 return 0;
1219 }
1220
1221
1222 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1223 Check if a text is encoded in UTF-16 Big Endian (endian == 1) or
1224 Little Endian (otherwise). If it is, return
1225 CATEGORY_MASK_UTF_16_BE or CATEGORY_MASK_UTF_16_LE,
1226 else return 0. */
1227
1228 #define UTF_16_HIGH_SURROGATE_P(val) \
1229 (((val) & 0xFC00) == 0xD800)
1230
1231 #define UTF_16_LOW_SURROGATE_P(val) \
1232 (((val) & 0xFC00) == 0xDC00)
1233
1234 #define UTF_16_INVALID_P(val) \
1235 (((val) == 0xFFFE) \
1236 || ((val) == 0xFFFF) \
1237 || UTF_16_LOW_SURROGATE_P (val))
1238
1239
1240 static int
1241 detect_coding_utf_16 (coding, mask)
1242 struct coding_system *coding;
1243 int *mask;
1244 {
1245 unsigned char *src = coding->source, *src_base = src;
1246 unsigned char *src_end = coding->source + coding->src_bytes;
1247 int multibytep = coding->src_multibyte;
1248 int consumed_chars = 0;
1249 int c1, c2;
1250
1251 ONE_MORE_BYTE (c1);
1252 ONE_MORE_BYTE (c2);
1253
1254 if ((c1 == 0xFF) && (c2 == 0xFE))
1255 {
1256 *mask &= CATEGORY_MASK_UTF_16_LE;
1257 return 1;
1258 }
1259 else if ((c1 == 0xFE) && (c2 == 0xFF))
1260 {
1261 *mask &= CATEGORY_MASK_UTF_16_BE;
1262 return 1;
1263 }
1264 no_more_source:
1265 return 0;
1266 }
1267
1268 static void
1269 decode_coding_utf_16 (coding)
1270 struct coding_system *coding;
1271 {
1272 unsigned char *src = coding->source + coding->consumed;
1273 unsigned char *src_end = coding->source + coding->src_bytes;
1274 unsigned char *src_base, *surrogate_high_base;
1275 int *charbuf = coding->charbuf;
1276 int *charbuf_end = charbuf + coding->charbuf_size;
1277 int consumed_chars = 0, consumed_chars_base;
1278 int multibytep = coding->src_multibyte;
1279 enum utf_16_bom_type bom = CODING_UTF_16_BOM (coding);
1280 enum utf_16_endian_type endian = CODING_UTF_16_ENDIAN (coding);
1281 int surrogate = CODING_UTF_16_SURROGATE (coding);
1282 Lisp_Object attr, eol_type, charset_list;
1283
1284 CODING_GET_INFO (coding, attr, eol_type, charset_list);
1285
1286 if (bom != utf_16_without_bom)
1287 {
1288 int c, c1, c2;
1289
1290 src_base = src;
1291 ONE_MORE_BYTE (c1);
1292 ONE_MORE_BYTE (c2);
1293 c = (c1 << 16) | c2;
1294 if (bom == utf_16_with_bom)
1295 {
1296 if (endian == utf_16_big_endian
1297 ? c != 0xFFFE : c != 0xFEFF)
1298 {
1299 /* We are sure that there's enouph room at CHARBUF. */
1300 *charbuf++ = c1;
1301 *charbuf++ = c2;
1302 coding->errors++;
1303 }
1304 }
1305 else
1306 {
1307 if (c == 0xFFFE)
1308 CODING_UTF_16_ENDIAN (coding)
1309 = endian = utf_16_big_endian;
1310 else if (c == 0xFEFF)
1311 CODING_UTF_16_ENDIAN (coding)
1312 = endian = utf_16_little_endian;
1313 else
1314 {
1315 CODING_UTF_16_ENDIAN (coding)
1316 = endian = utf_16_big_endian;
1317 src = src_base;
1318 }
1319 }
1320 CODING_UTF_16_BOM (coding) = utf_16_with_bom;
1321 }
1322
1323 while (1)
1324 {
1325 int c, c1, c2;
1326
1327 src_base = src;
1328 consumed_chars_base = consumed_chars;
1329
1330 if (charbuf + 2 >= charbuf_end)
1331 break;
1332
1333 ONE_MORE_BYTE (c1);
1334 ONE_MORE_BYTE (c2);
1335 c = (endian == utf_16_big_endian
1336 ? ((c1 << 16) | c2) : ((c2 << 16) | c1));
1337 if (surrogate)
1338 {
1339 if (! UTF_16_LOW_SURROGATE_P (c))
1340 {
1341 if (endian == utf_16_big_endian)
1342 c1 = surrogate >> 8, c2 = surrogate & 0xFF;
1343 else
1344 c1 = surrogate & 0xFF, c2 = surrogate >> 8;
1345 *charbuf++ = c1;
1346 *charbuf++ = c2;
1347 coding->errors++;
1348 if (UTF_16_HIGH_SURROGATE_P (c))
1349 CODING_UTF_16_SURROGATE (coding) = surrogate = c;
1350 else
1351 *charbuf++ = c;
1352 }
1353 else
1354 {
1355 c = ((surrogate - 0xD800) << 10) | (c - 0xDC00);
1356 CODING_UTF_16_SURROGATE (coding) = surrogate = 0;
1357 *charbuf++ = c;
1358 }
1359 }
1360 else
1361 {
1362 if (UTF_16_HIGH_SURROGATE_P (c))
1363 CODING_UTF_16_SURROGATE (coding) = surrogate = c;
1364 else
1365 *charbuf++ = c;
1366 }
1367 }
1368
1369 no_more_source:
1370 coding->consumed_char += consumed_chars_base;
1371 coding->consumed = src_base - coding->source;
1372 coding->charbuf_used = charbuf - coding->charbuf;
1373 }
1374
1375 static int
1376 encode_coding_utf_16 (coding)
1377 struct coding_system *coding;
1378 {
1379 int multibytep = coding->dst_multibyte;
1380 int *charbuf = coding->charbuf;
1381 int *charbuf_end = charbuf + coding->charbuf_used;
1382 unsigned char *dst = coding->destination + coding->produced;
1383 unsigned char *dst_end = coding->destination + coding->dst_bytes;
1384 int safe_room = 8;
1385 enum utf_16_bom_type bom = CODING_UTF_16_BOM (coding);
1386 int big_endian = CODING_UTF_16_ENDIAN (coding) == utf_16_big_endian;
1387 int produced_chars = 0;
1388 Lisp_Object attrs, eol_type, charset_list;
1389 int c;
1390
1391 CODING_GET_INFO (coding, attrs, eol_type, charset_list);
1392
1393 if (bom == utf_16_with_bom)
1394 {
1395 ASSURE_DESTINATION (safe_room);
1396 if (big_endian)
1397 EMIT_TWO_BYTES (0xFF, 0xFE);
1398 else
1399 EMIT_TWO_BYTES (0xFE, 0xFF);
1400 CODING_UTF_16_BOM (coding) = utf_16_without_bom;
1401 }
1402
1403 while (charbuf < charbuf_end)
1404 {
1405 ASSURE_DESTINATION (safe_room);
1406 c = *charbuf++;
1407 if (c >= 0x110000)
1408 c = 0xFFFF;
1409
1410 if (c < 0x10000)
1411 {
1412 if (big_endian)
1413 EMIT_TWO_BYTES (c >> 8, c & 0xFF);
1414 else
1415 EMIT_TWO_BYTES (c & 0xFF, c >> 8);
1416 }
1417 else
1418 {
1419 int c1, c2;
1420
1421 c -= 0x10000;
1422 c1 = (c >> 10) + 0xD800;
1423 c2 = (c & 0x3FF) + 0xDC00;
1424 if (big_endian)
1425 EMIT_FOUR_BYTES (c1 >> 8, c1 & 0xFF, c2 >> 8, c2 & 0xFF);
1426 else
1427 EMIT_FOUR_BYTES (c1 & 0xFF, c1 >> 8, c2 & 0xFF, c2 >> 8);
1428 }
1429 }
1430 coding->result = CODING_RESULT_SUCCESS;
1431 coding->produced = dst - coding->destination;
1432 coding->produced_char += produced_chars;
1433 return 0;
1434 }
1435
1436
1437 /*** 6. Old Emacs' internal format (emacs-mule) ***/
518 1438
519 /* Emacs' internal format for representation of multiple character 1439 /* Emacs' internal format for representation of multiple character
520 sets is a kind of multi-byte encoding, i.e. characters are 1440 sets is a kind of multi-byte encoding, i.e. characters are
521 represented by variable-length sequences of one-byte codes. 1441 represented by variable-length sequences of one-byte codes.
522 1442
554 format (i.e. by encoding by the coding system `emacs-mule'). 1474 format (i.e. by encoding by the coding system `emacs-mule').
555 1475
556 In that case, a sequence of one-byte codes has a slightly different 1476 In that case, a sequence of one-byte codes has a slightly different
557 form. 1477 form.
558 1478
559 Firstly, all characters in eight-bit-control are represented by 1479 At first, all characters in eight-bit-control are represented by
560 one-byte sequences which are their 8-bit code. 1480 one-byte sequences which are their 8-bit code.
561 1481
562 Next, character composition data are represented by the byte 1482 Next, character composition data are represented by the byte
563 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ..., 1483 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
564 where, 1484 where,
565 METHOD is 0xF0 plus one of composition method (enum 1485 METHOD is 0xF0 plus one of composition method (enum
566 composition_method), 1486 composition_method),
567 1487
568 BYTES is 0xA0 plus the byte length of these composition data, 1488 BYTES is 0xA0 plus a byte length of this composition data,
569 1489
570 CHARS is 0xA0 plus the number of characters composed by these 1490 CHARS is 0x20 plus a number of characters composed by this
571 data, 1491 data,
572 1492
573 COMPONENTs are characters of multibyte form or composition 1493 COMPONENTs are characters of multibye form or composition
574 rules encoded by two-byte of ASCII codes. 1494 rules encoded by two-byte of ASCII codes.
575 1495
576 In addition, for backward compatibility, the following formats are 1496 In addition, for backward compatibility, the following formats are
577 also recognized as composition data on decoding. 1497 also recognized as composition data on decoding.
578 1498
585 other: LEADING_CODE+0x20 FOLLOWING-BYTE ..., 1505 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
586 RULE is a one byte code of the range 0xA0..0xF0 that 1506 RULE is a one byte code of the range 0xA0..0xF0 that
587 represents a composition rule. 1507 represents a composition rule.
588 */ 1508 */
589 1509
590 enum emacs_code_class_type emacs_code_class[256]; 1510 char emacs_mule_bytes[256];
1511
1512 /* Leading-code followed by extended leading-code. */
1513 #define LEADING_CODE_PRIVATE_11 0x9A /* for private DIMENSION1 of 1-column */
1514 #define LEADING_CODE_PRIVATE_12 0x9B /* for private DIMENSION1 of 2-column */
1515 #define LEADING_CODE_PRIVATE_21 0x9C /* for private DIMENSION2 of 1-column */
1516 #define LEADING_CODE_PRIVATE_22 0x9D /* for private DIMENSION2 of 2-column */
1517
1518
1519 int
1520 emacs_mule_char (coding, composition, nbytes, nchars)
1521 struct coding_system *coding;
1522 int composition;
1523 int *nbytes, *nchars;
1524 {
1525 unsigned char *src = coding->source + coding->consumed;
1526 unsigned char *src_end = coding->source + coding->src_bytes;
1527 int multibytep = coding->src_multibyte;
1528 unsigned char *src_base = src;
1529 struct charset *charset;
1530 unsigned code;
1531 int c;
1532 int consumed_chars = 0;
1533
1534 ONE_MORE_BYTE (c);
1535 if (composition)
1536 {
1537 c -= 0x20;
1538 if (c == 0x80)
1539 {
1540 ONE_MORE_BYTE (c);
1541 if (c < 0xA0)
1542 goto invalid_code;
1543 *nbytes = src - src_base;
1544 *nchars = consumed_chars;
1545 return (c - 0x80);
1546 }
1547 }
1548
1549 switch (emacs_mule_bytes[c])
1550 {
1551 case 2:
1552 if (! (charset = emacs_mule_charset[c]))
1553 goto invalid_code;
1554 ONE_MORE_BYTE (c);
1555 code = c & 0x7F;
1556 break;
1557
1558 case 3:
1559 if (c == LEADING_CODE_PRIVATE_11
1560 || c == LEADING_CODE_PRIVATE_12)
1561 {
1562 ONE_MORE_BYTE (c);
1563 if (! (charset = emacs_mule_charset[c]))
1564 goto invalid_code;
1565 ONE_MORE_BYTE (c);
1566 code = c & 0x7F;
1567 }
1568 else
1569 {
1570 if (! (charset = emacs_mule_charset[c]))
1571 goto invalid_code;
1572 ONE_MORE_BYTE (c);
1573 code = (c & 0x7F) << 7;
1574 ONE_MORE_BYTE (c);
1575 code |= c & 0x7F;
1576 }
1577 break;
1578
1579 case 4:
1580 if (! (charset = emacs_mule_charset[c]))
1581 goto invalid_code;
1582 ONE_MORE_BYTE (c);
1583 code = (c & 0x7F) << 7;
1584 ONE_MORE_BYTE (c);
1585 code |= c & 0x7F;
1586 break;
1587
1588 case 1:
1589 code = c;
1590 charset = CHARSET_FROM_ID (ASCII_BYTE_P (code) ? charset_ascii
1591 : code < 0xA0 ? charset_8_bit_control
1592 : charset_8_bit_graphic);
1593 break;
1594
1595 default:
1596 abort ();
1597 }
1598 c = DECODE_CHAR (charset, code);
1599 if (c < 0)
1600 goto invalid_code;
1601 *nbytes = src - src_base;
1602 *nchars = consumed_chars;
1603 return c;
1604
1605 no_more_source:
1606 return -2;
1607
1608 invalid_code:
1609 return -1;
1610 }
1611
591 1612
592 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". 1613 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
593 Check if a text is encoded in Emacs' internal format. If it is, 1614 Check if a text is encoded in `emacs-mule'. */
594 return CODING_CATEGORY_MASK_EMACS_MULE, else return 0. */
595 1615
596 static int 1616 static int
597 detect_coding_emacs_mule (src, src_end, multibytep) 1617 detect_coding_emacs_mule (coding, mask)
598 unsigned char *src, *src_end; 1618 struct coding_system *coding;
599 int multibytep; 1619 int *mask;
600 { 1620 {
601 unsigned char c; 1621 unsigned char *src = coding->source, *src_base = src;
602 int composing = 0; 1622 unsigned char *src_end = coding->source + coding->src_bytes;
603 /* Dummy for ONE_MORE_BYTE. */ 1623 int multibytep = coding->src_multibyte;
604 struct coding_system dummy_coding; 1624 int consumed_chars = 0;
605 struct coding_system *coding = &dummy_coding; 1625 int c;
1626 int found = 0;
1627
1628 /* A coding system of this category is always ASCII compatible. */
1629 src += coding->head_ascii;
606 1630
607 while (1) 1631 while (1)
608 { 1632 {
609 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep); 1633 ONE_MORE_BYTE (c);
610 1634
611 if (composing) 1635 if (c == 0x80)
612 { 1636 {
613 if (c < 0xA0) 1637 /* Perhaps the start of composite character. We simple skip
614 composing = 0; 1638 it because analyzing it is too heavy for detecting. But,
615 else if (c == 0xA0) 1639 at least, we check that the composite character
1640 constitues of more than 4 bytes. */
1641 unsigned char *src_base;
1642
1643 repeat:
1644 src_base = src;
1645 do
616 { 1646 {
617 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep); 1647 ONE_MORE_BYTE (c);
618 c &= 0x7F;
619 } 1648 }
1649 while (c >= 0xA0);
1650
1651 if (src - src_base <= 4)
1652 break;
1653 found = 1;
1654 if (c == 0x80)
1655 goto repeat;
1656 }
1657
1658 if (c < 0x80)
1659 {
1660 if (c < 0x20
1661 && (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO))
1662 break;
1663 }
1664 else
1665 {
1666 unsigned char *src_base = src - 1;
1667
1668 do
1669 {
1670 ONE_MORE_BYTE (c);
1671 }
1672 while (c >= 0xA0);
1673 if (src - src_base != emacs_mule_bytes[*src_base])
1674 break;
1675 found = 1;
1676 }
1677 }
1678 *mask &= ~CATEGORY_MASK_EMACS_MULE;
1679 return 0;
1680
1681 no_more_source:
1682 if (!found)
1683 return 0;
1684 *mask &= CATEGORY_MASK_EMACS_MULE;
1685 return 1;
1686 }
1687
1688
1689 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
1690
1691 /* Decode a character represented as a component of composition
1692 sequence of Emacs 20/21 style at SRC. Set C to that character and
1693 update SRC to the head of next character (or an encoded composition
1694 rule). If SRC doesn't points a composition component, set C to -1.
1695 If SRC points an invalid byte sequence, global exit by a return
1696 value 0. */
1697
1698 #define DECODE_EMACS_MULE_COMPOSITION_CHAR(buf) \
1699 if (1) \
1700 { \
1701 int c; \
1702 int nbytes, nchars; \
1703 \
1704 if (src == src_end) \
1705 break; \
1706 c = emacs_mule_char (coding, 1, &nbytes, &nchars); \
1707 if (c < 0) \
1708 { \
1709 if (c == -2) \
1710 break; \
1711 goto invalid_code; \
1712 } \
1713 *buf++ = c; \
1714 src += nbytes; \
1715 consumed_chars += nchars; \
1716 } \
1717 else
1718
1719
1720 /* Decode a composition rule represented as a component of composition
1721 sequence of Emacs 20 style at SRC. Set C to the rule. If SRC
1722 points an invalid byte sequence, set C to -1. */
1723
1724 #define DECODE_EMACS_MULE_COMPOSITION_RULE(buf) \
1725 do { \
1726 int c, gref, nref; \
1727 \
1728 if (src < src_end) \
1729 goto invalid_code; \
1730 ONE_MORE_BYTE_NO_CHECK (c); \
1731 c -= 0xA0; \
1732 if (c < 0 || c >= 81) \
1733 goto invalid_code; \
1734 \
1735 gref = c / 9, nref = c % 9; \
1736 *buf++ = COMPOSITION_ENCODE_RULE (gref, nref); \
1737 } while (0)
1738
1739
1740 #define ADD_COMPOSITION_DATA(buf, method, nchars) \
1741 do { \
1742 *buf++ = -5; \
1743 *buf++ = coding->produced_char + char_offset; \
1744 *buf++ = CODING_ANNOTATE_COMPOSITION_MASK; \
1745 *buf++ = method; \
1746 *buf++ = nchars; \
1747 } while (0)
1748
1749
1750 #define DECODE_EMACS_MULE_21_COMPOSITION(c) \
1751 do { \
1752 /* Emacs 21 style format. The first three bytes at SRC are \
1753 (METHOD - 0xF0), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is \
1754 the byte length of this composition information, CHARS is the \
1755 number of characters composed by this composition. */ \
1756 enum composition_method method = c - 0xF0; \
1757 int consumed_chars_limit; \
1758 int nbytes, nchars; \
1759 \
1760 ONE_MORE_BYTE (c); \
1761 nbytes = c - 0xA0; \
1762 if (nbytes < 3) \
1763 goto invalid_code; \
1764 ONE_MORE_BYTE (c); \
1765 nchars = c - 0xA0; \
1766 ADD_COMPOSITION_DATA (charbuf, method, nchars); \
1767 consumed_chars_limit = consumed_chars_base + nbytes; \
1768 if (method != COMPOSITION_RELATIVE) \
1769 { \
1770 int i = 0; \
1771 while (consumed_chars < consumed_chars_limit) \
1772 { \
1773 if (i % 2 && method != COMPOSITION_WITH_ALTCHARS) \
1774 DECODE_EMACS_MULE_COMPOSITION_RULE (charbuf); \
1775 else \
1776 DECODE_EMACS_MULE_COMPOSITION_CHAR (charbuf); \
1777 } \
1778 if (consumed_chars < consumed_chars_limit) \
1779 goto invalid_code; \
1780 } \
1781 } while (0)
1782
1783
1784 #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION(c) \
1785 do { \
1786 /* Emacs 20 style format for relative composition. */ \
1787 /* Store multibyte form of characters to be composed. */ \
1788 int components[MAX_COMPOSITION_COMPONENTS * 2 - 1]; \
1789 int *buf = components; \
1790 int i, j; \
1791 \
1792 src = src_base; \
1793 ONE_MORE_BYTE (c); /* skip 0x80 */ \
1794 for (i = 0; i < MAX_COMPOSITION_COMPONENTS; i++) \
1795 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
1796 if (i < 2) \
1797 goto invalid_code; \
1798 ADD_COMPOSITION_DATA (charbuf, COMPOSITION_RELATIVE, i); \
1799 for (j = 0; j < i; j++) \
1800 *charbuf++ = components[j]; \
1801 } while (0)
1802
1803
1804 #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION(c) \
1805 do { \
1806 /* Emacs 20 style format for rule-base composition. */ \
1807 /* Store multibyte form of characters to be composed. */ \
1808 int components[MAX_COMPOSITION_COMPONENTS * 2 - 1]; \
1809 int *buf = components; \
1810 int i, j; \
1811 \
1812 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
1813 for (i = 0; i < MAX_COMPOSITION_COMPONENTS; i++) \
1814 { \
1815 DECODE_EMACS_MULE_COMPOSITION_RULE (buf); \
1816 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
1817 } \
1818 if (i < 1 || (buf - components) % 2 == 0) \
1819 goto invalid_code; \
1820 if (charbuf + i + (i / 2) + 1 < charbuf_end) \
1821 goto no_more_source; \
1822 ADD_COMPOSITION_DATA (buf, COMPOSITION_WITH_RULE, i); \
1823 for (j = 0; j < i; j++) \
1824 *charbuf++ = components[j]; \
1825 for (j = 0; j < i; j += 2) \
1826 *charbuf++ = components[j]; \
1827 } while (0)
1828
1829
1830 static void
1831 decode_coding_emacs_mule (coding)
1832 struct coding_system *coding;
1833 {
1834 unsigned char *src = coding->source + coding->consumed;
1835 unsigned char *src_end = coding->source + coding->src_bytes;
1836 unsigned char *src_base;
1837 int *charbuf = coding->charbuf;
1838 int *charbuf_end = charbuf + coding->charbuf_size;
1839 int consumed_chars = 0, consumed_chars_base;
1840 int char_offset = 0;
1841 int multibytep = coding->src_multibyte;
1842 Lisp_Object attrs, eol_type, charset_list;
1843
1844 CODING_GET_INFO (coding, attrs, eol_type, charset_list);
1845
1846 while (1)
1847 {
1848 int c;
1849
1850 src_base = src;
1851 consumed_chars_base = consumed_chars;
1852
1853 if (charbuf >= charbuf_end)
1854 break;
1855
1856 ONE_MORE_BYTE (c);
1857
1858 if (c < 0x80)
1859 {
1860 if (c == '\r')
1861 {
1862 if (EQ (eol_type, Qdos))
1863 {
1864 if (src == src_end)
1865 goto no_more_source;
1866 if (*src == '\n')
1867 ONE_MORE_BYTE (c);
1868 }
1869 else if (EQ (eol_type, Qmac))
1870 c = '\n';
1871 }
1872 *charbuf++ = c;
1873 char_offset++;
1874 }
1875 else if (c == 0x80)
1876 {
1877 if (charbuf + 5 + (MAX_COMPOSITION_COMPONENTS * 2) - 1 > charbuf_end)
1878 break;
1879 ONE_MORE_BYTE (c);
1880 if (c - 0xF0 >= COMPOSITION_RELATIVE
1881 && c - 0xF0 <= COMPOSITION_WITH_RULE_ALTCHARS)
1882 DECODE_EMACS_MULE_21_COMPOSITION (c);
1883 else if (c < 0xC0)
1884 DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (c);
1885 else if (c == 0xFF)
1886 DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (c);
620 else 1887 else
621 c -= 0x20; 1888 goto invalid_code;
622 } 1889 }
623 1890 else if (c < 0xA0 && emacs_mule_bytes[c] > 1)
624 if (c < 0x20) 1891 {
625 { 1892 int nbytes, nchars;
626 if (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) 1893 src--;
627 return 0; 1894 c = emacs_mule_char (coding, 0, &nbytes, &nchars);
628 } 1895 if (c < 0)
629 else if (c >= 0x80 && c < 0xA0) 1896 {
630 { 1897 if (c == -2)
631 if (c == 0x80) 1898 break;
632 /* Old leading code for a composite character. */ 1899 goto invalid_code;
633 composing = 1; 1900 }
1901 *charbuf++ = c;
1902 char_offset++;
1903 }
1904 continue;
1905
1906 invalid_code:
1907 src = src_base;
1908 consumed_chars = consumed_chars_base;
1909 ONE_MORE_BYTE (c);
1910 *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
1911 coding->errors++;
1912 }
1913
1914 no_more_source:
1915 coding->consumed_char += consumed_chars_base;
1916 coding->consumed = src_base - coding->source;
1917 coding->charbuf_used = charbuf - coding->charbuf;
1918 }
1919
1920
1921 #define EMACS_MULE_LEADING_CODES(id, codes) \
1922 do { \
1923 if (id < 0xA0) \
1924 codes[0] = id, codes[1] = 0; \
1925 else if (id < 0xE0) \
1926 codes[0] = 0x9A, codes[1] = id; \
1927 else if (id < 0xF0) \
1928 codes[0] = 0x9B, codes[1] = id; \
1929 else if (id < 0xF5) \
1930 codes[0] = 0x9C, codes[1] = id; \
1931 else \
1932 codes[0] = 0x9D, codes[1] = id; \
1933 } while (0);
1934
1935
1936 static int
1937 encode_coding_emacs_mule (coding)
1938 struct coding_system *coding;
1939 {
1940 int multibytep = coding->dst_multibyte;
1941 int *charbuf = coding->charbuf;
1942 int *charbuf_end = charbuf + coding->charbuf_used;
1943 unsigned char *dst = coding->destination + coding->produced;
1944 unsigned char *dst_end = coding->destination + coding->dst_bytes;
1945 int safe_room = 8;
1946 unsigned char *adjusted_dst_end =dst_end - 8;
1947 int produced_chars = 0;
1948 Lisp_Object attrs, eol_type, charset_list;
1949 int c;
1950
1951 CODING_GET_INFO (coding, attrs, eol_type, charset_list);
1952
1953 while (charbuf < charbuf_end)
1954 {
1955 ASSURE_DESTINATION (safe_room);
1956 c = *charbuf++;
1957 if (ASCII_CHAR_P (c))
1958 EMIT_ONE_ASCII_BYTE (c);
1959 else
1960 {
1961 struct charset *charset;
1962 unsigned code;
1963 int dimension;
1964 int emacs_mule_id;
1965 unsigned char leading_codes[2];
1966
1967 charset = char_charset (c, charset_list, &code);
1968 if (! charset)
1969 {
1970 c = coding->default_char;
1971 if (ASCII_CHAR_P (c))
1972 {
1973 EMIT_ONE_ASCII_BYTE (c);
1974 continue;
1975 }
1976 charset = char_charset (c, charset_list, &code);
1977 }
1978 dimension = CHARSET_DIMENSION (charset);
1979 emacs_mule_id = CHARSET_EMACS_MULE_ID (charset);
1980 EMACS_MULE_LEADING_CODES (emacs_mule_id, leading_codes);
1981 EMIT_ONE_BYTE (leading_codes[0]);
1982 if (leading_codes[1])
1983 EMIT_ONE_BYTE (leading_codes[1]);
1984 if (dimension == 1)
1985 EMIT_ONE_BYTE (code);
634 else 1986 else
635 { 1987 {
636 unsigned char *src_base = src - 1; 1988 EMIT_ONE_BYTE (code >> 8);
637 int bytes; 1989 EMIT_ONE_BYTE (code & 0xFF);
638
639 if (!UNIBYTE_STR_AS_MULTIBYTE_P (src_base, src_end - src_base,
640 bytes))
641 return 0;
642 src = src_base + bytes;
643 } 1990 }
644 } 1991 }
645 } 1992 }
646 label_end_of_loop: 1993 coding->result = CODING_RESULT_SUCCESS;
647 return CODING_CATEGORY_MASK_EMACS_MULE; 1994 coding->produced_char += produced_chars;
648 } 1995 coding->produced = dst - coding->destination;
649 1996 return 0;
650
651 /* Record the starting position START and METHOD of one composition. */
652
653 #define CODING_ADD_COMPOSITION_START(coding, start, method) \
654 do { \
655 struct composition_data *cmp_data = coding->cmp_data; \
656 int *data = cmp_data->data + cmp_data->used; \
657 coding->cmp_data_start = cmp_data->used; \
658 data[0] = -1; \
659 data[1] = cmp_data->char_offset + start; \
660 data[3] = (int) method; \
661 cmp_data->used += 4; \
662 } while (0)
663
664 /* Record the ending position END of the current composition. */
665
666 #define CODING_ADD_COMPOSITION_END(coding, end) \
667 do { \
668 struct composition_data *cmp_data = coding->cmp_data; \
669 int *data = cmp_data->data + coding->cmp_data_start; \
670 data[0] = cmp_data->used - coding->cmp_data_start; \
671 data[2] = cmp_data->char_offset + end; \
672 } while (0)
673
674 /* Record one COMPONENT (alternate character or composition rule). */
675
676 #define CODING_ADD_COMPOSITION_COMPONENT(coding, component) \
677 (coding->cmp_data->data[coding->cmp_data->used++] = component)
678
679
680 /* Get one byte from a data pointed by SRC and increment SRC. If SRC
681 is not less than SRC_END, return -1 without incrementing Src. */
682
683 #define SAFE_ONE_MORE_BYTE() (src >= src_end ? -1 : *src++)
684
685
686 /* Decode a character represented as a component of composition
687 sequence of Emacs 20 style at SRC. Set C to that character, store
688 its multibyte form sequence at P, and set P to the end of that
689 sequence. If no valid character is found, set C to -1. */
690
691 #define DECODE_EMACS_MULE_COMPOSITION_CHAR(c, p) \
692 do { \
693 int bytes; \
694 \
695 c = SAFE_ONE_MORE_BYTE (); \
696 if (c < 0) \
697 break; \
698 if (CHAR_HEAD_P (c)) \
699 c = -1; \
700 else if (c == 0xA0) \
701 { \
702 c = SAFE_ONE_MORE_BYTE (); \
703 if (c < 0xA0) \
704 c = -1; \
705 else \
706 { \
707 c -= 0xA0; \
708 *p++ = c; \
709 } \
710 } \
711 else if (BASE_LEADING_CODE_P (c - 0x20)) \
712 { \
713 unsigned char *p0 = p; \
714 \
715 c -= 0x20; \
716 *p++ = c; \
717 bytes = BYTES_BY_CHAR_HEAD (c); \
718 while (--bytes) \
719 { \
720 c = SAFE_ONE_MORE_BYTE (); \
721 if (c < 0) \
722 break; \
723 *p++ = c; \
724 } \
725 if (UNIBYTE_STR_AS_MULTIBYTE_P (p0, p - p0, bytes)) \
726 c = STRING_CHAR (p0, bytes); \
727 else \
728 c = -1; \
729 } \
730 else \
731 c = -1; \
732 } while (0)
733
734
735 /* Decode a composition rule represented as a component of composition
736 sequence of Emacs 20 style at SRC. Set C to the rule. If not
737 valid rule is found, set C to -1. */
738
739 #define DECODE_EMACS_MULE_COMPOSITION_RULE(c) \
740 do { \
741 c = SAFE_ONE_MORE_BYTE (); \
742 c -= 0xA0; \
743 if (c < 0 || c >= 81) \
744 c = -1; \
745 else \
746 { \
747 gref = c / 9, nref = c % 9; \
748 c = COMPOSITION_ENCODE_RULE (gref, nref); \
749 } \
750 } while (0)
751
752
753 /* Decode composition sequence encoded by `emacs-mule' at the source
754 pointed by SRC. SRC_END is the end of source. Store information
755 of the composition in CODING->cmp_data.
756
757 For backward compatibility, decode also a composition sequence of
758 Emacs 20 style. In that case, the composition sequence contains
759 characters that should be extracted into a buffer or string. Store
760 those characters at *DESTINATION in multibyte form.
761
762 If we encounter an invalid byte sequence, return 0.
763 If we encounter an insufficient source or destination, or
764 insufficient space in CODING->cmp_data, return 1.
765 Otherwise, return consumed bytes in the source.
766
767 */
768 static INLINE int
769 decode_composition_emacs_mule (coding, src, src_end,
770 destination, dst_end, dst_bytes)
771 struct coding_system *coding;
772 unsigned char *src, *src_end, **destination, *dst_end;
773 int dst_bytes;
774 {
775 unsigned char *dst = *destination;
776 int method, data_len, nchars;
777 unsigned char *src_base = src++;
778 /* Store components of composition. */
779 int component[COMPOSITION_DATA_MAX_BUNCH_LENGTH];
780 int ncomponent;
781 /* Store multibyte form of characters to be composed. This is for
782 Emacs 20 style composition sequence. */
783 unsigned char buf[MAX_COMPOSITION_COMPONENTS * MAX_MULTIBYTE_LENGTH];
784 unsigned char *bufp = buf;
785 int c, i, gref, nref;
786
787 if (coding->cmp_data->used + COMPOSITION_DATA_MAX_BUNCH_LENGTH
788 >= COMPOSITION_DATA_SIZE)
789 {
790 coding->result = CODING_FINISH_INSUFFICIENT_CMP;
791 return -1;
792 }
793
794 ONE_MORE_BYTE (c);
795 if (c - 0xF0 >= COMPOSITION_RELATIVE
796 && c - 0xF0 <= COMPOSITION_WITH_RULE_ALTCHARS)
797 {
798 int with_rule;
799
800 method = c - 0xF0;
801 with_rule = (method == COMPOSITION_WITH_RULE
802 || method == COMPOSITION_WITH_RULE_ALTCHARS);
803 ONE_MORE_BYTE (c);
804 data_len = c - 0xA0;
805 if (data_len < 4
806 || src_base + data_len > src_end)
807 return 0;
808 ONE_MORE_BYTE (c);
809 nchars = c - 0xA0;
810 if (c < 1)
811 return 0;
812 for (ncomponent = 0; src < src_base + data_len; ncomponent++)
813 {
814 /* If it is longer than this, it can't be valid. */
815 if (ncomponent >= COMPOSITION_DATA_MAX_BUNCH_LENGTH)
816 return 0;
817
818 if (ncomponent % 2 && with_rule)
819 {
820 ONE_MORE_BYTE (gref);
821 gref -= 32;
822 ONE_MORE_BYTE (nref);
823 nref -= 32;
824 c = COMPOSITION_ENCODE_RULE (gref, nref);
825 }
826 else
827 {
828 int bytes;
829 if (UNIBYTE_STR_AS_MULTIBYTE_P (src, src_end - src, bytes))
830 c = STRING_CHAR (src, bytes);
831 else
832 c = *src, bytes = 1;
833 src += bytes;
834 }
835 component[ncomponent] = c;
836 }
837 }
838 else
839 {
840 /* This may be an old Emacs 20 style format. See the comment at
841 the section 2 of this file. */
842 while (src < src_end && !CHAR_HEAD_P (*src)) src++;
843 if (src == src_end
844 && !(coding->mode & CODING_MODE_LAST_BLOCK))
845 goto label_end_of_loop;
846
847 src_end = src;
848 src = src_base + 1;
849 if (c < 0xC0)
850 {
851 method = COMPOSITION_RELATIVE;
852 for (ncomponent = 0; ncomponent < MAX_COMPOSITION_COMPONENTS;)
853 {
854 DECODE_EMACS_MULE_COMPOSITION_CHAR (c, bufp);
855 if (c < 0)
856 break;
857 component[ncomponent++] = c;
858 }
859 if (ncomponent < 2)
860 return 0;
861 nchars = ncomponent;
862 }
863 else if (c == 0xFF)
864 {
865 method = COMPOSITION_WITH_RULE;
866 src++;
867 DECODE_EMACS_MULE_COMPOSITION_CHAR (c, bufp);
868 if (c < 0)
869 return 0;
870 component[0] = c;
871 for (ncomponent = 1;
872 ncomponent < MAX_COMPOSITION_COMPONENTS * 2 - 1;)
873 {
874 DECODE_EMACS_MULE_COMPOSITION_RULE (c);
875 if (c < 0)
876 break;
877 component[ncomponent++] = c;
878 DECODE_EMACS_MULE_COMPOSITION_CHAR (c, bufp);
879 if (c < 0)
880 break;
881 component[ncomponent++] = c;
882 }
883 if (ncomponent < 3)
884 return 0;
885 nchars = (ncomponent + 1) / 2;
886 }
887 else
888 return 0;
889 }
890
891 if (buf == bufp || dst + (bufp - buf) <= (dst_bytes ? dst_end : src))
892 {
893 CODING_ADD_COMPOSITION_START (coding, coding->produced_char, method);
894 for (i = 0; i < ncomponent; i++)
895 CODING_ADD_COMPOSITION_COMPONENT (coding, component[i]);
896 CODING_ADD_COMPOSITION_END (coding, coding->produced_char + nchars);
897 if (buf < bufp)
898 {
899 unsigned char *p = buf;
900 EMIT_BYTES (p, bufp);
901 *destination += bufp - buf;
902 coding->produced_char += nchars;
903 }
904 return (src - src_base);
905 }
906 label_end_of_loop:
907 return -1;
908 }
909
910 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
911
912 static void
913 decode_coding_emacs_mule (coding, source, destination, src_bytes, dst_bytes)
914 struct coding_system *coding;
915 unsigned char *source, *destination;
916 int src_bytes, dst_bytes;
917 {
918 unsigned char *src = source;
919 unsigned char *src_end = source + src_bytes;
920 unsigned char *dst = destination;
921 unsigned char *dst_end = destination + dst_bytes;
922 /* SRC_BASE remembers the start position in source in each loop.
923 The loop will be exited when there's not enough source code, or
924 when there's not enough destination area to produce a
925 character. */
926 unsigned char *src_base;
927
928 coding->produced_char = 0;
929 while ((src_base = src) < src_end)
930 {
931 unsigned char tmp[MAX_MULTIBYTE_LENGTH], *p;
932 int bytes;
933
934 if (*src == '\r')
935 {
936 int c = *src++;
937
938 if (coding->eol_type == CODING_EOL_CR)
939 c = '\n';
940 else if (coding->eol_type == CODING_EOL_CRLF)
941 {
942 ONE_MORE_BYTE (c);
943 if (c != '\n')
944 {
945 if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
946 {
947 coding->result = CODING_FINISH_INCONSISTENT_EOL;
948 goto label_end_of_loop;
949 }
950 src--;
951 c = '\r';
952 }
953 }
954 *dst++ = c;
955 coding->produced_char++;
956 continue;
957 }
958 else if (*src == '\n')
959 {
960 if ((coding->eol_type == CODING_EOL_CR
961 || coding->eol_type == CODING_EOL_CRLF)
962 && coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
963 {
964 coding->result = CODING_FINISH_INCONSISTENT_EOL;
965 goto label_end_of_loop;
966 }
967 *dst++ = *src++;
968 coding->produced_char++;
969 continue;
970 }
971 else if (*src == 0x80)
972 {
973 /* Start of composition data. */
974 int consumed = decode_composition_emacs_mule (coding, src, src_end,
975 &dst, dst_end,
976 dst_bytes);
977 if (consumed < 0)
978 goto label_end_of_loop;
979 else if (consumed > 0)
980 {
981 src += consumed;
982 continue;
983 }
984 bytes = CHAR_STRING (*src, tmp);
985 p = tmp;
986 src++;
987 }
988 else if (UNIBYTE_STR_AS_MULTIBYTE_P (src, src_end - src, bytes))
989 {
990 p = src;
991 src += bytes;
992 }
993 else
994 {
995 bytes = CHAR_STRING (*src, tmp);
996 p = tmp;
997 src++;
998 }
999 if (dst + bytes >= (dst_bytes ? dst_end : src))
1000 {
1001 coding->result = CODING_FINISH_INSUFFICIENT_DST;
1002 break;
1003 }
1004 while (bytes--) *dst++ = *p++;
1005 coding->produced_char++;
1006 }
1007 label_end_of_loop:
1008 coding->consumed = coding->consumed_char = src_base - source;
1009 coding->produced = dst - destination;
1010 }
1011
1012
1013 /* Encode composition data stored at DATA into a special byte sequence
1014 starting by 0x80. Update CODING->cmp_data_start and maybe
1015 CODING->cmp_data for the next call. */
1016
1017 #define ENCODE_COMPOSITION_EMACS_MULE(coding, data) \
1018 do { \
1019 unsigned char buf[1024], *p0 = buf, *p; \
1020 int len = data[0]; \
1021 int i; \
1022 \
1023 buf[0] = 0x80; \
1024 buf[1] = 0xF0 + data[3]; /* METHOD */ \
1025 buf[3] = 0xA0 + (data[2] - data[1]); /* COMPOSED-CHARS */ \
1026 p = buf + 4; \
1027 if (data[3] == COMPOSITION_WITH_RULE \
1028 || data[3] == COMPOSITION_WITH_RULE_ALTCHARS) \
1029 { \
1030 p += CHAR_STRING (data[4], p); \
1031 for (i = 5; i < len; i += 2) \
1032 { \
1033 int gref, nref; \
1034 COMPOSITION_DECODE_RULE (data[i], gref, nref); \
1035 *p++ = 0x20 + gref; \
1036 *p++ = 0x20 + nref; \
1037 p += CHAR_STRING (data[i + 1], p); \
1038 } \
1039 } \
1040 else \
1041 { \
1042 for (i = 4; i < len; i++) \
1043 p += CHAR_STRING (data[i], p); \
1044 } \
1045 buf[2] = 0xA0 + (p - buf); /* COMPONENTS-BYTES */ \
1046 \
1047 if (dst + (p - buf) + 4 > (dst_bytes ? dst_end : src)) \
1048 { \
1049 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
1050 goto label_end_of_loop; \
1051 } \
1052 while (p0 < p) \
1053 *dst++ = *p0++; \
1054 coding->cmp_data_start += data[0]; \
1055 if (coding->cmp_data_start == coding->cmp_data->used \
1056 && coding->cmp_data->next) \
1057 { \
1058 coding->cmp_data = coding->cmp_data->next; \
1059 coding->cmp_data_start = 0; \
1060 } \
1061 } while (0)
1062
1063
1064 static void encode_eol P_ ((struct coding_system *, unsigned char *,
1065 unsigned char *, int, int));
1066
1067 static void
1068 encode_coding_emacs_mule (coding, source, destination, src_bytes, dst_bytes)
1069 struct coding_system *coding;
1070 unsigned char *source, *destination;
1071 int src_bytes, dst_bytes;
1072 {
1073 unsigned char *src = source;
1074 unsigned char *src_end = source + src_bytes;
1075 unsigned char *dst = destination;
1076 unsigned char *dst_end = destination + dst_bytes;
1077 unsigned char *src_base;
1078 int c;
1079 int char_offset;
1080 int *data;
1081
1082 Lisp_Object translation_table;
1083
1084 translation_table = Qnil;
1085
1086 /* Optimization for the case that there's no composition. */
1087 if (!coding->cmp_data || coding->cmp_data->used == 0)
1088 {
1089 encode_eol (coding, source, destination, src_bytes, dst_bytes);
1090 return;
1091 }
1092
1093 char_offset = coding->cmp_data->char_offset;
1094 data = coding->cmp_data->data + coding->cmp_data_start;
1095 while (1)
1096 {
1097 src_base = src;
1098
1099 /* If SRC starts a composition, encode the information about the
1100 composition in advance. */
1101 if (coding->cmp_data_start < coding->cmp_data->used
1102 && char_offset + coding->consumed_char == data[1])
1103 {
1104 ENCODE_COMPOSITION_EMACS_MULE (coding, data);
1105 char_offset = coding->cmp_data->char_offset;
1106 data = coding->cmp_data->data + coding->cmp_data_start;
1107 }
1108
1109 ONE_MORE_CHAR (c);
1110 if (c == '\n' && (coding->eol_type == CODING_EOL_CRLF
1111 || coding->eol_type == CODING_EOL_CR))
1112 {
1113 if (coding->eol_type == CODING_EOL_CRLF)
1114 EMIT_TWO_BYTES ('\r', c);
1115 else
1116 EMIT_ONE_BYTE ('\r');
1117 }
1118 else if (SINGLE_BYTE_CHAR_P (c))
1119 EMIT_ONE_BYTE (c);
1120 else
1121 EMIT_BYTES (src_base, src);
1122 coding->consumed_char++;
1123 }
1124 label_end_of_loop:
1125 coding->consumed = src_base - source;
1126 coding->produced = coding->produced_char = dst - destination;
1127 return;
1128 } 1997 }
1129 1998
1130 1999
1131 /*** 3. ISO2022 handlers ***/ 2000 /*** 7. ISO2022 handlers ***/
1132 2001
1133 /* The following note describes the coding system ISO2022 briefly. 2002 /* The following note describes the coding system ISO2022 briefly.
1134 Since the intention of this note is to help understand the 2003 Since the intention of this note is to help understand the
1135 functions in this file, some parts are NOT ACCURATE or are OVERLY 2004 functions in this file, some parts are NOT ACCURATE or OVERLY
1136 SIMPLIFIED. For thorough understanding, please refer to the 2005 SIMPLIFIED. For thorough understanding, please refer to the
1137 original document of ISO2022. This is equivalent to the standard 2006 original document of ISO2022.
1138 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
1139 2007
1140 ISO2022 provides many mechanisms to encode several character sets 2008 ISO2022 provides many mechanisms to encode several character sets
1141 in 7-bit and 8-bit environments. For 7-bit environments, all text 2009 in 7-bit and 8-bit environments. For 7-bite environments, all text
1142 is encoded using bytes less than 128. This may make the encoded 2010 is encoded using bytes less than 128. This may make the encoded
1143 text a little bit longer, but the text passes more easily through 2011 text a little bit longer, but the text passes more easily through
1144 several types of gateway, some of which strip off the MSB (Most 2012 several gateways, some of which strip off MSB (Most Signigant Bit).
1145 Significant Bit). 2013
1146 2014 There are two kinds of character sets: control character set and
1147 There are two kinds of character sets: control character sets and 2015 graphic character set. The former contains control characters such
1148 graphic character sets. The former contain control characters such
1149 as `newline' and `escape' to provide control functions (control 2016 as `newline' and `escape' to provide control functions (control
1150 functions are also provided by escape sequences). The latter 2017 functions are also provided by escape sequences). The latter
1151 contain graphic characters such as 'A' and '-'. Emacs recognizes 2018 contains graphic characters such as 'A' and '-'. Emacs recognizes
1152 two control character sets and many graphic character sets. 2019 two control character sets and many graphic character sets.
1153 2020
1154 Graphic character sets are classified into one of the following 2021 Graphic character sets are classified into one of the following
1155 four classes, according to the number of bytes (DIMENSION) and 2022 four classes, according to the number of bytes (DIMENSION) and
1156 number of characters in one dimension (CHARS) of the set: 2023 number of characters in one dimension (CHARS) of the set:
1158 - DIMENSION1_CHARS96 2025 - DIMENSION1_CHARS96
1159 - DIMENSION2_CHARS94 2026 - DIMENSION2_CHARS94
1160 - DIMENSION2_CHARS96 2027 - DIMENSION2_CHARS96
1161 2028
1162 In addition, each character set is assigned an identification tag, 2029 In addition, each character set is assigned an identification tag,
1163 unique for each set, called the "final character" (denoted as <F> 2030 unique for each set, called "final character" (denoted as <F>
1164 hereafter). The <F> of each character set is decided by ECMA(*) 2031 hereafter). The <F> of each character set is decided by ECMA(*)
1165 when it is registered in ISO. The code range of <F> is 0x30..0x7F 2032 when it is registered in ISO. The code range of <F> is 0x30..0x7F
1166 (0x30..0x3F are for private use only). 2033 (0x30..0x3F are for private use only).
1167 2034
1168 Note (*): ECMA = European Computer Manufacturers Association 2035 Note (*): ECMA = European Computer Manufacturers Association
1169 2036
1170 Here are examples of graphic character sets [NAME(<F>)]: 2037 Here are examples of graphic character set [NAME(<F>)]:
1171 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ... 2038 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
1172 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ... 2039 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
1173 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ... 2040 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
1174 o DIMENSION2_CHARS96 -- none for the moment 2041 o DIMENSION2_CHARS96 -- none for the moment
1175 2042
1256 Emacs accepts them on decoding, and produces them on encoding 2123 Emacs accepts them on decoding, and produces them on encoding
1257 CHARS96 character sets in a coding system which is characterized as 2124 CHARS96 character sets in a coding system which is characterized as
1258 7-bit environment, non-locking-shift, and non-single-shift. 2125 7-bit environment, non-locking-shift, and non-single-shift.
1259 2126
1260 Note (**): If <F> is '@', 'A', or 'B', the intermediate character 2127 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
1261 '(' can be omitted. We refer to this as "short-form" hereafter. 2128 '(' must be omitted. We refer to this as "short-form" hereafter.
1262 2129
1263 Now you may notice that there are a lot of ways of encoding the 2130 Now you may notice that there are a lot of ways for encoding the
1264 same multilingual text in ISO2022. Actually, there exist many 2131 same multilingual text in ISO2022. Actually, there exist many
1265 coding systems such as Compound Text (used in X11's inter client 2132 coding systems such as Compound Text (used in X11's inter client
1266 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR 2133 communication, ISO-2022-JP (used in Japanese internet), ISO-2022-KR
1267 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian 2134 (used in Korean internet), EUC (Extended UNIX Code, used in Asian
1268 localized platforms), and all of these are variants of ISO2022. 2135 localized platforms), and all of these are variants of ISO2022.
1269 2136
1270 In addition to the above, Emacs handles two more kinds of escape 2137 In addition to the above, Emacs handles two more kinds of escape
1271 sequences: ISO6429's direction specification and Emacs' private 2138 sequences: ISO6429's direction specification and Emacs' private
1272 sequence for specifying character composition. 2139 sequence for specifying character composition.
1284 o ESC '1' -- end composition 2151 o ESC '1' -- end composition
1285 o ESC '2' -- start rule-base composition (*) 2152 o ESC '2' -- start rule-base composition (*)
1286 o ESC '3' -- start relative composition with alternate chars (**) 2153 o ESC '3' -- start relative composition with alternate chars (**)
1287 o ESC '4' -- start rule-base composition with alternate chars (**) 2154 o ESC '4' -- start rule-base composition with alternate chars (**)
1288 Since these are not standard escape sequences of any ISO standard, 2155 Since these are not standard escape sequences of any ISO standard,
1289 the use of them with these meanings is restricted to Emacs only. 2156 the use of them for these meaning is restricted to Emacs only.
1290 2157
1291 (*) This form is used only in Emacs 20.5 and older versions, 2158 (*) This form is used only in Emacs 20.5 and the older versions,
1292 but the newer versions can safely decode it. 2159 but the newer versions can safely decode it.
1293 (**) This form is used only in Emacs 21.1 and newer versions, 2160 (**) This form is used only in Emacs 21.1 and the newer versions,
1294 and the older versions can't decode it. 2161 and the older versions can't decode it.
1295 2162
1296 Here's a list of example usages of these composition escape 2163 Here's a list of examples usages of these composition escape
1297 sequences (categorized by `enum composition_method'). 2164 sequences (categorized by `enum composition_method').
1298 2165
1299 COMPOSITION_RELATIVE: 2166 COMPOSITION_RELATIVE:
1300 ESC 0 CHAR [ CHAR ] ESC 1 2167 ESC 0 CHAR [ CHAR ] ESC 1
1301 COMPOSITION_WITH_RULE: 2168 COMPOSITOIN_WITH_RULE:
1302 ESC 2 CHAR [ RULE CHAR ] ESC 1 2169 ESC 2 CHAR [ RULE CHAR ] ESC 1
1303 COMPOSITION_WITH_ALTCHARS: 2170 COMPOSITION_WITH_ALTCHARS:
1304 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 2171 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
1305 COMPOSITION_WITH_RULE_ALTCHARS: 2172 COMPOSITION_WITH_RULE_ALTCHARS:
1306 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */ 2173 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
1307 2174
1308 enum iso_code_class_type iso_code_class[256]; 2175 enum iso_code_class_type iso_code_class[256];
1309 2176
1310 #define CHARSET_OK(idx, charset, c) \ 2177 #define SAFE_CHARSET_P(coding, id) \
1311 (coding_system_table[idx] \ 2178 ((id) <= (coding)->max_charset_id \
1312 && (charset == CHARSET_ASCII \ 2179 && (coding)->safe_charsets[id] >= 0)
1313 || (safe_chars = coding_safe_chars (coding_system_table[idx]), \ 2180
1314 CODING_SAFE_CHAR_P (safe_chars, c))) \ 2181
1315 && (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding_system_table[idx], \ 2182 #define SHIFT_OUT_OK(category) \
1316 charset) \ 2183 (CODING_ISO_INITIAL (&coding_categories[category], 1) >= 0)
1317 != CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION)) 2184
1318 2185 static void
1319 #define SHIFT_OUT_OK(idx) \ 2186 setup_iso_safe_charsets (Lisp_Object attrs)
1320 (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding_system_table[idx], 1) >= 0) 2187 {
2188 Lisp_Object charset_list, safe_charsets;
2189 Lisp_Object request;
2190 Lisp_Object reg_usage;
2191 Lisp_Object tail;
2192 int reg94, reg96;
2193 int flags = XINT (AREF (attrs, coding_attr_iso_flags));
2194 int max_charset_id;
2195
2196 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
2197 if ((flags & CODING_ISO_FLAG_FULL_SUPPORT)
2198 && ! EQ (charset_list, Viso_2022_charset_list))
2199 {
2200 CODING_ATTR_CHARSET_LIST (attrs)
2201 = charset_list = Viso_2022_charset_list;
2202 ASET (attrs, coding_attr_safe_charsets, Qnil);
2203 }
2204
2205 if (STRINGP (AREF (attrs, coding_attr_safe_charsets)))
2206 return;
2207
2208 max_charset_id = 0;
2209 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
2210 {
2211 int id = XINT (XCAR (tail));
2212 if (max_charset_id < id)
2213 max_charset_id = id;
2214 }
2215
2216 safe_charsets = Fmake_string (make_number (max_charset_id + 1),
2217 make_number (255));
2218 request = AREF (attrs, coding_attr_iso_request);
2219 reg_usage = AREF (attrs, coding_attr_iso_usage);
2220 reg94 = XINT (XCAR (reg_usage));
2221 reg96 = XINT (XCDR (reg_usage));
2222
2223 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
2224 {
2225 Lisp_Object id;
2226 Lisp_Object reg;
2227 struct charset *charset;
2228
2229 id = XCAR (tail);
2230 charset = CHARSET_FROM_ID (XINT (id));
2231 reg = Fcdr (Fassq (request, id));
2232 if (! NILP (reg))
2233 XSTRING (safe_charsets)->data[XINT (id)] = XINT (reg);
2234 else if (charset->iso_chars_96)
2235 {
2236 if (reg96 < 4)
2237 XSTRING (safe_charsets)->data[XINT (id)] = reg96;
2238 }
2239 else
2240 {
2241 if (reg94 < 4)
2242 XSTRING (safe_charsets)->data[XINT (id)] = reg94;
2243 }
2244 }
2245 ASET (attrs, coding_attr_safe_charsets, safe_charsets);
2246 }
2247
1321 2248
1322 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". 2249 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1323 Check if a text is encoded in ISO2022. If it is, return an 2250 Check if a text is encoded in ISO2022. If it is, returns an
1324 integer in which appropriate flag bits any of: 2251 integer in which appropriate flag bits any of:
1325 CODING_CATEGORY_MASK_ISO_7 2252 CATEGORY_MASK_ISO_7
1326 CODING_CATEGORY_MASK_ISO_7_TIGHT 2253 CATEGORY_MASK_ISO_7_TIGHT
1327 CODING_CATEGORY_MASK_ISO_8_1 2254 CATEGORY_MASK_ISO_8_1
1328 CODING_CATEGORY_MASK_ISO_8_2 2255 CATEGORY_MASK_ISO_8_2
1329 CODING_CATEGORY_MASK_ISO_7_ELSE 2256 CATEGORY_MASK_ISO_7_ELSE
1330 CODING_CATEGORY_MASK_ISO_8_ELSE 2257 CATEGORY_MASK_ISO_8_ELSE
1331 are set. If a code which should never appear in ISO2022 is found, 2258 are set. If a code which should never appear in ISO2022 is found,
1332 returns 0. */ 2259 returns 0. */
1333 2260
1334 static int 2261 static int
1335 detect_coding_iso2022 (src, src_end, multibytep) 2262 detect_coding_iso_2022 (coding, mask)
1336 unsigned char *src, *src_end; 2263 struct coding_system *coding;
1337 int multibytep; 2264 int *mask;
1338 { 2265 {
1339 int mask = CODING_CATEGORY_MASK_ISO; 2266 unsigned char *src = coding->source, *src_base = src;
1340 int mask_found = 0; 2267 unsigned char *src_end = coding->source + coding->src_bytes;
2268 int multibytep = coding->src_multibyte;
2269 int mask_iso = CATEGORY_MASK_ISO;
2270 int mask_found = 0, mask_8bit_found = 0;
1341 int reg[4], shift_out = 0, single_shifting = 0; 2271 int reg[4], shift_out = 0, single_shifting = 0;
1342 int c, c1, charset; 2272 int id;
1343 /* Dummy for ONE_MORE_BYTE. */ 2273 int c, c1;
1344 struct coding_system dummy_coding; 2274 int consumed_chars = 0;
1345 struct coding_system *coding = &dummy_coding; 2275 int i;
1346 Lisp_Object safe_chars; 2276
1347 2277 for (i = coding_category_iso_7; i <= coding_category_iso_8_else; i++)
1348 reg[0] = CHARSET_ASCII, reg[1] = reg[2] = reg[3] = -1; 2278 {
1349 while (mask && src < src_end) 2279 struct coding_system *this = &(coding_categories[i]);
1350 { 2280 Lisp_Object attrs, val;
1351 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep); 2281
2282 attrs = CODING_ID_ATTRS (this->id);
2283 if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT
2284 && ! EQ (CODING_ATTR_SAFE_CHARSETS (attrs), Viso_2022_charset_list))
2285 setup_iso_safe_charsets (attrs);
2286 val = CODING_ATTR_SAFE_CHARSETS (attrs);
2287 this->max_charset_id = XSTRING (val)->size - 1;
2288 this->safe_charsets = (char *) XSTRING (val)->data;
2289 }
2290
2291 /* A coding system of this category is always ASCII compatible. */
2292 src += coding->head_ascii;
2293
2294 reg[0] = charset_ascii, reg[1] = reg[2] = reg[3] = -1;
2295 while (mask_iso && src < src_end)
2296 {
2297 ONE_MORE_BYTE (c);
1352 switch (c) 2298 switch (c)
1353 { 2299 {
1354 case ISO_CODE_ESC: 2300 case ISO_CODE_ESC:
1355 if (inhibit_iso_escape_detection) 2301 if (inhibit_iso_escape_detection)
1356 break; 2302 break;
1357 single_shifting = 0; 2303 single_shifting = 0;
1358 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep); 2304 ONE_MORE_BYTE (c);
1359 if (c >= '(' && c <= '/') 2305 if (c >= '(' && c <= '/')
1360 { 2306 {
1361 /* Designation sequence for a charset of dimension 1. */ 2307 /* Designation sequence for a charset of dimension 1. */
1362 ONE_MORE_BYTE_CHECK_MULTIBYTE (c1, multibytep); 2308 ONE_MORE_BYTE (c1);
1363 if (c1 < ' ' || c1 >= 0x80 2309 if (c1 < ' ' || c1 >= 0x80
1364 || (charset = iso_charset_table[0][c >= ','][c1]) < 0) 2310 || (id = iso_charset_table[0][c >= ','][c1]) < 0)
1365 /* Invalid designation sequence. Just ignore. */ 2311 /* Invalid designation sequence. Just ignore. */
1366 break; 2312 break;
1367 reg[(c - '(') % 4] = charset; 2313 reg[(c - '(') % 4] = id;
1368 } 2314 }
1369 else if (c == '$') 2315 else if (c == '$')
1370 { 2316 {
1371 /* Designation sequence for a charset of dimension 2. */ 2317 /* Designation sequence for a charset of dimension 2. */
1372 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep); 2318 ONE_MORE_BYTE (c);
1373 if (c >= '@' && c <= 'B') 2319 if (c >= '@' && c <= 'B')
1374 /* Designation for JISX0208.1978, GB2312, or JISX0208. */ 2320 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
1375 reg[0] = charset = iso_charset_table[1][0][c]; 2321 reg[0] = id = iso_charset_table[1][0][c];
1376 else if (c >= '(' && c <= '/') 2322 else if (c >= '(' && c <= '/')
1377 { 2323 {
1378 ONE_MORE_BYTE_CHECK_MULTIBYTE (c1, multibytep); 2324 ONE_MORE_BYTE (c1);
1379 if (c1 < ' ' || c1 >= 0x80 2325 if (c1 < ' ' || c1 >= 0x80
1380 || (charset = iso_charset_table[1][c >= ','][c1]) < 0) 2326 || (id = iso_charset_table[1][c >= ','][c1]) < 0)
1381 /* Invalid designation sequence. Just ignore. */ 2327 /* Invalid designation sequence. Just ignore. */
1382 break; 2328 break;
1383 reg[(c - '(') % 4] = charset; 2329 reg[(c - '(') % 4] = id;
1384 } 2330 }
1385 else 2331 else
1386 /* Invalid designation sequence. Just ignore. */ 2332 /* Invalid designation sequence. Just ignore. */
1387 break; 2333 break;
1388 } 2334 }
1389 else if (c == 'N' || c == 'O') 2335 else if (c == 'N' || c == 'O')
1390 { 2336 {
1391 /* ESC <Fe> for SS2 or SS3. */ 2337 /* ESC <Fe> for SS2 or SS3. */
1392 mask &= CODING_CATEGORY_MASK_ISO_7_ELSE; 2338 mask_iso &= CATEGORY_MASK_ISO_7_ELSE;
1393 break; 2339 break;
1394 } 2340 }
1395 else if (c >= '0' && c <= '4') 2341 else if (c >= '0' && c <= '4')
1396 { 2342 {
1397 /* ESC <Fp> for start/end composition. */ 2343 /* ESC <Fp> for start/end composition. */
1398 mask_found |= CODING_CATEGORY_MASK_ISO; 2344 mask_found |= CATEGORY_MASK_ISO;
1399 break; 2345 break;
1400 } 2346 }
1401 else 2347 else
1402 /* Invalid escape sequence. Just ignore. */ 2348 {
1403 break; 2349 /* Invalid escape sequence. */
2350 mask_iso &= ~CATEGORY_MASK_ISO_ESCAPE;
2351 break;
2352 }
1404 2353
1405 /* We found a valid designation sequence for CHARSET. */ 2354 /* We found a valid designation sequence for CHARSET. */
1406 mask &= ~CODING_CATEGORY_MASK_ISO_8BIT; 2355 mask_iso &= ~CATEGORY_MASK_ISO_8BIT;
1407 c = MAKE_CHAR (charset, 0, 0); 2356 if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7],
1408 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7, charset, c)) 2357 id))
1409 mask_found |= CODING_CATEGORY_MASK_ISO_7; 2358 mask_found |= CATEGORY_MASK_ISO_7;
1410 else 2359 else
1411 mask &= ~CODING_CATEGORY_MASK_ISO_7; 2360 mask_iso &= ~CATEGORY_MASK_ISO_7;
1412 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7_TIGHT, charset, c)) 2361 if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7_tight],
1413 mask_found |= CODING_CATEGORY_MASK_ISO_7_TIGHT; 2362 id))
2363 mask_found |= CATEGORY_MASK_ISO_7_TIGHT;
1414 else 2364 else
1415 mask &= ~CODING_CATEGORY_MASK_ISO_7_TIGHT; 2365 mask_iso &= ~CATEGORY_MASK_ISO_7_TIGHT;
1416 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7_ELSE, charset, c)) 2366 if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7_else],
1417 mask_found |= CODING_CATEGORY_MASK_ISO_7_ELSE; 2367 id))
2368 mask_found |= CATEGORY_MASK_ISO_7_ELSE;
1418 else 2369 else
1419 mask &= ~CODING_CATEGORY_MASK_ISO_7_ELSE; 2370 mask_iso &= ~CATEGORY_MASK_ISO_7_ELSE;
1420 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_8_ELSE, charset, c)) 2371 if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_8_else],
1421 mask_found |= CODING_CATEGORY_MASK_ISO_8_ELSE; 2372 id))
2373 mask_found |= CATEGORY_MASK_ISO_8_ELSE;
1422 else 2374 else
1423 mask &= ~CODING_CATEGORY_MASK_ISO_8_ELSE; 2375 mask_iso &= ~CATEGORY_MASK_ISO_8_ELSE;
1424 break; 2376 break;
1425 2377
1426 case ISO_CODE_SO: 2378 case ISO_CODE_SO:
1427 if (inhibit_iso_escape_detection) 2379 if (inhibit_iso_escape_detection)
1428 break; 2380 break;
1429 single_shifting = 0; 2381 single_shifting = 0;
1430 if (shift_out == 0 2382 if (shift_out == 0
1431 && (reg[1] >= 0 2383 && (reg[1] >= 0
1432 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_7_ELSE) 2384 || SHIFT_OUT_OK (coding_category_iso_7_else)
1433 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_8_ELSE))) 2385 || SHIFT_OUT_OK (coding_category_iso_8_else)))
1434 { 2386 {
1435 /* Locking shift out. */ 2387 /* Locking shift out. */
1436 mask &= ~CODING_CATEGORY_MASK_ISO_7BIT; 2388 mask_iso &= ~CATEGORY_MASK_ISO_7BIT;
1437 mask_found |= CODING_CATEGORY_MASK_ISO_SHIFT; 2389 mask_found |= CATEGORY_MASK_ISO_ELSE;
1438 } 2390 }
1439 break; 2391 break;
1440 2392
1441 case ISO_CODE_SI: 2393 case ISO_CODE_SI:
1442 if (inhibit_iso_escape_detection) 2394 if (inhibit_iso_escape_detection)
1443 break; 2395 break;
1444 single_shifting = 0; 2396 single_shifting = 0;
1445 if (shift_out == 1) 2397 if (shift_out == 1)
1446 { 2398 {
1447 /* Locking shift in. */ 2399 /* Locking shift in. */
1448 mask &= ~CODING_CATEGORY_MASK_ISO_7BIT; 2400 mask_iso &= ~CATEGORY_MASK_ISO_7BIT;
1449 mask_found |= CODING_CATEGORY_MASK_ISO_SHIFT; 2401 mask_found |= CATEGORY_MASK_ISO_ELSE;
1450 } 2402 }
1451 break; 2403 break;
1452 2404
1453 case ISO_CODE_CSI: 2405 case ISO_CODE_CSI:
1454 single_shifting = 0; 2406 single_shifting = 0;
1455 case ISO_CODE_SS2: 2407 case ISO_CODE_SS2:
1456 case ISO_CODE_SS3: 2408 case ISO_CODE_SS3:
1457 { 2409 {
1458 int newmask = CODING_CATEGORY_MASK_ISO_8_ELSE; 2410 int newmask = CATEGORY_MASK_ISO_8_ELSE;
1459 2411
1460 if (inhibit_iso_escape_detection) 2412 if (inhibit_iso_escape_detection)
1461 break; 2413 break;
1462 if (c != ISO_CODE_CSI) 2414 if (c != ISO_CODE_CSI)
1463 { 2415 {
1464 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_1]->flags 2416 if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1])
1465 & CODING_FLAG_ISO_SINGLE_SHIFT) 2417 & CODING_ISO_FLAG_SINGLE_SHIFT)
1466 newmask |= CODING_CATEGORY_MASK_ISO_8_1; 2418 newmask |= CATEGORY_MASK_ISO_8_1;
1467 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_2]->flags 2419 if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_2])
1468 & CODING_FLAG_ISO_SINGLE_SHIFT) 2420 & CODING_ISO_FLAG_SINGLE_SHIFT)
1469 newmask |= CODING_CATEGORY_MASK_ISO_8_2; 2421 newmask |= CATEGORY_MASK_ISO_8_2;
1470 single_shifting = 1; 2422 single_shifting = 1;
1471 } 2423 }
1472 if (VECTORP (Vlatin_extra_code_table) 2424 if (VECTORP (Vlatin_extra_code_table)
1473 && !NILP (XVECTOR (Vlatin_extra_code_table)->contents[c])) 2425 && !NILP (XVECTOR (Vlatin_extra_code_table)->contents[c]))
1474 { 2426 {
1475 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_1]->flags 2427 if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1])
1476 & CODING_FLAG_ISO_LATIN_EXTRA) 2428 & CODING_ISO_FLAG_LATIN_EXTRA)
1477 newmask |= CODING_CATEGORY_MASK_ISO_8_1; 2429 newmask |= CATEGORY_MASK_ISO_8_1;
1478 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_2]->flags 2430 if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_2])
1479 & CODING_FLAG_ISO_LATIN_EXTRA) 2431 & CODING_ISO_FLAG_LATIN_EXTRA)
1480 newmask |= CODING_CATEGORY_MASK_ISO_8_2; 2432 newmask |= CATEGORY_MASK_ISO_8_2;
1481 } 2433 }
1482 mask &= newmask; 2434 mask_iso &= newmask;
1483 mask_found |= newmask; 2435 mask_found |= newmask;
1484 } 2436 }
1485 break; 2437 break;
1486 2438
1487 default: 2439 default:
1491 break; 2443 break;
1492 } 2444 }
1493 else if (c < 0xA0) 2445 else if (c < 0xA0)
1494 { 2446 {
1495 single_shifting = 0; 2447 single_shifting = 0;
2448 mask_8bit_found = 1;
1496 if (VECTORP (Vlatin_extra_code_table) 2449 if (VECTORP (Vlatin_extra_code_table)
1497 && !NILP (XVECTOR (Vlatin_extra_code_table)->contents[c])) 2450 && !NILP (XVECTOR (Vlatin_extra_code_table)->contents[c]))
1498 { 2451 {
1499 int newmask = 0; 2452 int newmask = 0;
1500 2453
1501 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_1]->flags 2454 if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1])
1502 & CODING_FLAG_ISO_LATIN_EXTRA) 2455 & CODING_ISO_FLAG_LATIN_EXTRA)
1503 newmask |= CODING_CATEGORY_MASK_ISO_8_1; 2456 newmask |= CATEGORY_MASK_ISO_8_1;
1504 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_2]->flags 2457 if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_2])
1505 & CODING_FLAG_ISO_LATIN_EXTRA) 2458 & CODING_ISO_FLAG_LATIN_EXTRA)
1506 newmask |= CODING_CATEGORY_MASK_ISO_8_2; 2459 newmask |= CATEGORY_MASK_ISO_8_2;
1507 mask &= newmask; 2460 mask_iso &= newmask;
1508 mask_found |= newmask; 2461 mask_found |= newmask;
1509 } 2462 }
1510 else 2463 else
1511 return 0; 2464 return 0;
1512 } 2465 }
1513 else 2466 else
1514 { 2467 {
1515 mask &= ~(CODING_CATEGORY_MASK_ISO_7BIT 2468 mask_iso &= ~(CATEGORY_MASK_ISO_7BIT
1516 | CODING_CATEGORY_MASK_ISO_7_ELSE); 2469 | CATEGORY_MASK_ISO_7_ELSE);
1517 mask_found |= CODING_CATEGORY_MASK_ISO_8_1; 2470 mask_found |= CATEGORY_MASK_ISO_8_1;
2471 mask_8bit_found = 1;
1518 /* Check the length of succeeding codes of the range 2472 /* Check the length of succeeding codes of the range
1519 0xA0..0FF. If the byte length is odd, we exclude 2473 0xA0..0FF. If the byte length is odd, we exclude
1520 CODING_CATEGORY_MASK_ISO_8_2. We can check this only 2474 CATEGORY_MASK_ISO_8_2. We can check this only
1521 when we are not single shifting. */ 2475 when we are not single shifting. */
1522 if (!single_shifting 2476 if (!single_shifting
1523 && mask & CODING_CATEGORY_MASK_ISO_8_2) 2477 && mask_iso & CATEGORY_MASK_ISO_8_2)
1524 { 2478 {
1525 int i = 1; 2479 int i = 1;
1526 while (src < src_end) 2480 while (src < src_end)
1527 { 2481 {
1528 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep); 2482 ONE_MORE_BYTE (c);
1529 if (c < 0xA0) 2483 if (c < 0xA0)
1530 break; 2484 break;
1531 i++; 2485 i++;
1532 } 2486 }
1533 2487
1534 if (i & 1 && src < src_end) 2488 if (i & 1 && src < src_end)
1535 mask &= ~CODING_CATEGORY_MASK_ISO_8_2; 2489 mask_iso &= ~CATEGORY_MASK_ISO_8_2;
1536 else 2490 else
1537 mask_found |= CODING_CATEGORY_MASK_ISO_8_2; 2491 mask_found |= CATEGORY_MASK_ISO_8_2;
1538 } 2492 }
1539 } 2493 }
1540 break; 2494 break;
1541 } 2495 }
1542 } 2496 }
1543 label_end_of_loop: 2497 no_more_source:
1544 return (mask & mask_found); 2498 if (!mask_iso)
1545 } 2499 {
1546 2500 *mask &= ~CATEGORY_MASK_ISO;
1547 /* Decode a character of which charset is CHARSET, the 1st position 2501 return 0;
1548 code is C1, the 2nd position code is C2, and return the decoded 2502 }
1549 character code. If the variable `translation_table' is non-nil, 2503 if (!mask_found)
1550 returned the translated code. */ 2504 return 0;
1551 2505 *mask &= mask_iso & mask_found;
1552 #define DECODE_ISO_CHARACTER(charset, c1, c2) \ 2506 if (! mask_8bit_found)
1553 (NILP (translation_table) \ 2507 *mask &= ~(CATEGORY_MASK_ISO_8BIT | CATEGORY_MASK_ISO_8_ELSE);
1554 ? MAKE_CHAR (charset, c1, c2) \ 2508 return 1;
1555 : translate_char (translation_table, -1, charset, c1, c2)) 2509 }
2510
1556 2511
1557 /* Set designation state into CODING. */ 2512 /* Set designation state into CODING. */
1558 #define DECODE_DESIGNATION(reg, dimension, chars, final_char) \ 2513 #define DECODE_DESIGNATION(reg, dim, chars_96, final) \
1559 do { \ 2514 do { \
1560 int charset, c; \ 2515 int id, prev; \
1561 \ 2516 \
1562 if (final_char < '0' || final_char >= 128) \ 2517 if (final < '0' || final >= 128 \
1563 goto label_invalid_code; \ 2518 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
1564 charset = ISO_CHARSET_TABLE (make_number (dimension), \ 2519 || !SAFE_CHARSET_P (coding, id)) \
1565 make_number (chars), \ 2520 { \
1566 make_number (final_char)); \ 2521 CODING_ISO_DESIGNATION (coding, reg) = -2; \
1567 c = MAKE_CHAR (charset, 0, 0); \ 2522 goto invalid_code; \
1568 if (charset >= 0 \ 2523 } \
1569 && (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) == reg \ 2524 prev = CODING_ISO_DESIGNATION (coding, reg); \
1570 || CODING_SAFE_CHAR_P (safe_chars, c))) \ 2525 CODING_ISO_DESIGNATION (coding, reg) = id; \
1571 { \ 2526 /* If there was an invalid designation to REG previously, and this \
1572 if (coding->spec.iso2022.last_invalid_designation_register == 0 \ 2527 designation is ASCII to REG, we should keep this designation \
1573 && reg == 0 \ 2528 sequence. */ \
1574 && charset == CHARSET_ASCII) \ 2529 if (prev == -2 && id == charset_ascii) \
1575 { \ 2530 goto invalid_code; \
1576 /* We should insert this designation sequence as is so \
1577 that it is surely written back to a file. */ \
1578 coding->spec.iso2022.last_invalid_designation_register = -1; \
1579 goto label_invalid_code; \
1580 } \
1581 coding->spec.iso2022.last_invalid_designation_register = -1; \
1582 if ((coding->mode & CODING_MODE_DIRECTION) \
1583 && CHARSET_REVERSE_CHARSET (charset) >= 0) \
1584 charset = CHARSET_REVERSE_CHARSET (charset); \
1585 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
1586 } \
1587 else \
1588 { \
1589 coding->spec.iso2022.last_invalid_designation_register = reg; \
1590 goto label_invalid_code; \
1591 } \
1592 } while (0) 2531 } while (0)
1593 2532
1594 /* Allocate a memory block for storing information about compositions. 2533
1595 The block is chained to the already allocated blocks. */ 2534 #define MAYBE_FINISH_COMPOSITION() \
1596 2535 do { \
1597 void 2536 int i; \
1598 coding_allocate_composition_data (coding, char_offset) 2537 if (composition_state == COMPOSING_NO) \
1599 struct coding_system *coding; 2538 break; \
1600 int char_offset; 2539 /* It is assured that we have enough room for producing \
1601 { 2540 characters stored in the table `components'. */ \
1602 struct composition_data *cmp_data 2541 if (charbuf + component_idx > charbuf_end) \
1603 = (struct composition_data *) xmalloc (sizeof *cmp_data); 2542 goto no_more_source; \
1604 2543 composition_state = COMPOSING_NO; \
1605 cmp_data->char_offset = char_offset; 2544 if (method == COMPOSITION_RELATIVE \
1606 cmp_data->used = 0; 2545 || method == COMPOSITION_WITH_ALTCHARS) \
1607 cmp_data->prev = coding->cmp_data; 2546 { \
1608 cmp_data->next = NULL; 2547 for (i = 0; i < component_idx; i++) \
1609 if (coding->cmp_data) 2548 *charbuf++ = components[i]; \
1610 coding->cmp_data->next = cmp_data; 2549 char_offset += component_idx; \
1611 coding->cmp_data = cmp_data; 2550 } \
1612 coding->cmp_data_start = 0; 2551 else \
1613 } 2552 { \
2553 for (i = 0; i < component_idx; i += 2) \
2554 *charbuf++ = components[i]; \
2555 char_offset += (component_idx / 2) + 1; \
2556 } \
2557 } while (0)
2558
1614 2559
1615 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4. 2560 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
1616 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1 2561 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
1617 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1 2562 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
1618 ESC 3 : altchar composition : ESC 3 ALT ... ESC 0 CHAR ... ESC 1 2563 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
1619 ESC 4 : alt&rule composition : ESC 4 ALT RULE .. ALT ESC 0 CHAR ... ESC 1 2564 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
1620 */ 2565 */
1621 2566
1622 #define DECODE_COMPOSITION_START(c1) \ 2567 #define DECODE_COMPOSITION_START(c1) \
1623 do { \
1624 if (coding->composing == COMPOSITION_DISABLED) \
1625 { \
1626 *dst++ = ISO_CODE_ESC; \
1627 *dst++ = c1 & 0x7f; \
1628 coding->produced_char += 2; \
1629 } \
1630 else if (!COMPOSING_P (coding)) \
1631 { \
1632 /* This is surely the start of a composition. We must be sure \
1633 that coding->cmp_data has enough space to store the \
1634 information about the composition. If not, terminate the \
1635 current decoding loop, allocate one more memory block for \
1636 coding->cmp_data in the caller, then start the decoding \
1637 loop again. We can't allocate memory here directly because \
1638 it may cause buffer/string relocation. */ \
1639 if (!coding->cmp_data \
1640 || (coding->cmp_data->used + COMPOSITION_DATA_MAX_BUNCH_LENGTH \
1641 >= COMPOSITION_DATA_SIZE)) \
1642 { \
1643 coding->result = CODING_FINISH_INSUFFICIENT_CMP; \
1644 goto label_end_of_loop; \
1645 } \
1646 coding->composing = (c1 == '0' ? COMPOSITION_RELATIVE \
1647 : c1 == '2' ? COMPOSITION_WITH_RULE \
1648 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
1649 : COMPOSITION_WITH_RULE_ALTCHARS); \
1650 CODING_ADD_COMPOSITION_START (coding, coding->produced_char, \
1651 coding->composing); \
1652 coding->composition_rule_follows = 0; \
1653 } \
1654 else \
1655 { \
1656 /* We are already handling a composition. If the method is \
1657 the following two, the codes following the current escape \
1658 sequence are actual characters stored in a buffer. */ \
1659 if (coding->composing == COMPOSITION_WITH_ALTCHARS \
1660 || coding->composing == COMPOSITION_WITH_RULE_ALTCHARS) \
1661 { \
1662 coding->composing = COMPOSITION_RELATIVE; \
1663 coding->composition_rule_follows = 0; \
1664 } \
1665 } \
1666 } while (0)
1667
1668 /* Handle composition end sequence ESC 1. */
1669
1670 #define DECODE_COMPOSITION_END(c1) \
1671 do { \ 2568 do { \
1672 if (! COMPOSING_P (coding)) \ 2569 if (c1 == '0' \
2570 && composition_state == COMPOSING_COMPONENT_CHAR) \
1673 { \ 2571 { \
1674 *dst++ = ISO_CODE_ESC; \ 2572 component_len = component_idx; \
1675 *dst++ = c1; \ 2573 composition_state = COMPOSING_CHAR; \
1676 coding->produced_char += 2; \
1677 } \ 2574 } \
1678 else \ 2575 else \
1679 { \ 2576 { \
1680 CODING_ADD_COMPOSITION_END (coding, coding->produced_char); \ 2577 unsigned char *p; \
1681 coding->composing = COMPOSITION_NO; \ 2578 \
2579 MAYBE_FINISH_COMPOSITION (); \
2580 if (charbuf + MAX_COMPOSITION_COMPONENTS > charbuf_end) \
2581 goto no_more_source; \
2582 for (p = src; p < src_end - 1; p++) \
2583 if (*p == ISO_CODE_ESC && p[1] == '1') \
2584 break; \
2585 if (p == src_end - 1) \
2586 { \
2587 if (coding->mode & CODING_MODE_LAST_BLOCK) \
2588 goto invalid_code; \
2589 goto no_more_source; \
2590 } \
2591 \
2592 /* This is surely the start of a composition. */ \
2593 method = (c1 == '0' ? COMPOSITION_RELATIVE \
2594 : c1 == '2' ? COMPOSITION_WITH_RULE \
2595 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
2596 : COMPOSITION_WITH_RULE_ALTCHARS); \
2597 composition_state = (c1 <= '2' ? COMPOSING_CHAR \
2598 : COMPOSING_COMPONENT_CHAR); \
2599 component_idx = component_len = 0; \
1682 } \ 2600 } \
1683 } while (0) 2601 } while (0)
2602
2603
2604 /* Handle compositoin end sequence ESC 1. */
2605
2606 #define DECODE_COMPOSITION_END() \
2607 do { \
2608 int nchars = (component_len > 0 ? component_idx - component_len \
2609 : method == COMPOSITION_RELATIVE ? component_idx \
2610 : (component_idx + 1) / 2); \
2611 int i; \
2612 int *saved_charbuf = charbuf; \
2613 \
2614 ADD_COMPOSITION_DATA (charbuf, method, nchars); \
2615 if (method != COMPOSITION_RELATIVE) \
2616 { \
2617 if (component_len == 0) \
2618 for (i = 0; i < component_idx; i++) \
2619 *charbuf++ = components[i]; \
2620 else \
2621 for (i = 0; i < component_len; i++) \
2622 *charbuf++ = components[i]; \
2623 *saved_charbuf = saved_charbuf - charbuf; \
2624 } \
2625 if (method == COMPOSITION_WITH_RULE) \
2626 for (i = 0; i < component_idx; i += 2, char_offset++) \
2627 *charbuf++ = components[i]; \
2628 else \
2629 for (i = component_len; i < component_idx; i++, char_offset++) \
2630 *charbuf++ = components[i]; \
2631 coding->annotated = 1; \
2632 composition_state = COMPOSING_NO; \
2633 } while (0)
2634
1684 2635
1685 /* Decode a composition rule from the byte C1 (and maybe one more byte 2636 /* Decode a composition rule from the byte C1 (and maybe one more byte
1686 from SRC) and store one encoded composition rule in 2637 from SRC) and store one encoded composition rule in
1687 coding->cmp_data. */ 2638 coding->cmp_data. */
1688 2639
1689 #define DECODE_COMPOSITION_RULE(c1) \ 2640 #define DECODE_COMPOSITION_RULE(c1) \
1690 do { \ 2641 do { \
1691 int rule = 0; \
1692 (c1) -= 32; \ 2642 (c1) -= 32; \
1693 if (c1 < 81) /* old format (before ver.21) */ \ 2643 if (c1 < 81) /* old format (before ver.21) */ \
1694 { \ 2644 { \
1695 int gref = (c1) / 9; \ 2645 int gref = (c1) / 9; \
1696 int nref = (c1) % 9; \ 2646 int nref = (c1) % 9; \
1697 if (gref == 4) gref = 10; \ 2647 if (gref == 4) gref = 10; \
1698 if (nref == 4) nref = 10; \ 2648 if (nref == 4) nref = 10; \
1699 rule = COMPOSITION_ENCODE_RULE (gref, nref); \ 2649 c1 = COMPOSITION_ENCODE_RULE (gref, nref); \
1700 } \ 2650 } \
1701 else if (c1 < 93) /* new format (after ver.21) */ \ 2651 else if (c1 < 93) /* new format (after ver.21) */ \
1702 { \ 2652 { \
1703 ONE_MORE_BYTE (c2); \ 2653 ONE_MORE_BYTE (c2); \
1704 rule = COMPOSITION_ENCODE_RULE (c1 - 81, c2 - 32); \ 2654 c1 = COMPOSITION_ENCODE_RULE (c1 - 81, c2 - 32); \
1705 } \ 2655 } \
1706 CODING_ADD_COMPOSITION_COMPONENT (coding, rule); \ 2656 else \
1707 coding->composition_rule_follows = 0; \ 2657 c1 = 0; \
1708 } while (0) 2658 } while (0)
1709 2659
1710 2660
1711 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ 2661 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
1712 2662
1713 static void 2663 static void
1714 decode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes) 2664 decode_coding_iso_2022 (coding)
1715 struct coding_system *coding; 2665 struct coding_system *coding;
1716 unsigned char *source, *destination; 2666 {
1717 int src_bytes, dst_bytes; 2667 unsigned char *src = coding->source + coding->consumed;
1718 { 2668 unsigned char *src_end = coding->source + coding->src_bytes;
1719 unsigned char *src = source; 2669 unsigned char *src_base;
1720 unsigned char *src_end = source + src_bytes; 2670 int *charbuf = coding->charbuf;
1721 unsigned char *dst = destination; 2671 int *charbuf_end = charbuf + coding->charbuf_size - 4;
1722 unsigned char *dst_end = destination + dst_bytes; 2672 int consumed_chars = 0, consumed_chars_base;
2673 int char_offset = 0;
2674 int multibytep = coding->src_multibyte;
1723 /* Charsets invoked to graphic plane 0 and 1 respectively. */ 2675 /* Charsets invoked to graphic plane 0 and 1 respectively. */
1724 int charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0); 2676 int charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
1725 int charset1 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 1); 2677 int charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1);
1726 /* SRC_BASE remembers the start position in source in each loop. 2678 struct charset *charset;
1727 The loop will be exited when there's not enough source code 2679 int c;
1728 (within macro ONE_MORE_BYTE), or when there's not enough 2680 /* For handling composition sequence. */
1729 destination area to produce a character (within macro 2681 #define COMPOSING_NO 0
1730 EMIT_CHAR). */ 2682 #define COMPOSING_CHAR 1
1731 unsigned char *src_base; 2683 #define COMPOSING_RULE 2
1732 int c, charset; 2684 #define COMPOSING_COMPONENT_CHAR 3
1733 Lisp_Object translation_table; 2685 #define COMPOSING_COMPONENT_RULE 4
1734 Lisp_Object safe_chars; 2686
1735 2687 int composition_state = COMPOSING_NO;
1736 safe_chars = coding_safe_chars (coding); 2688 enum composition_method method;
1737 2689 int components[MAX_COMPOSITION_COMPONENTS * 2 + 1];
1738 if (NILP (Venable_character_translation)) 2690 int component_idx;
1739 translation_table = Qnil; 2691 int component_len;
1740 else 2692 Lisp_Object attrs, eol_type, charset_list;
1741 { 2693
1742 translation_table = coding->translation_table_for_decode; 2694 CODING_GET_INFO (coding, attrs, eol_type, charset_list);
1743 if (NILP (translation_table)) 2695 setup_iso_safe_charsets (attrs);
1744 translation_table = Vstandard_translation_table_for_decode;
1745 }
1746
1747 coding->result = CODING_FINISH_NORMAL;
1748 2696
1749 while (1) 2697 while (1)
1750 { 2698 {
1751 int c1, c2; 2699 int c1, c2;
1752 2700
1753 src_base = src; 2701 src_base = src;
2702 consumed_chars_base = consumed_chars;
2703
2704 if (charbuf >= charbuf_end)
2705 break;
2706
1754 ONE_MORE_BYTE (c1); 2707 ONE_MORE_BYTE (c1);
1755 2708
1756 /* We produce no character or one character. */ 2709 /* We produce no character or one character. */
1757 switch (iso_code_class [c1]) 2710 switch (iso_code_class [c1])
1758 { 2711 {
1759 case ISO_0x20_or_0x7F: 2712 case ISO_0x20_or_0x7F:
1760 if (COMPOSING_P (coding) && coding->composition_rule_follows) 2713 if (composition_state != COMPOSING_NO)
1761 { 2714 {
1762 DECODE_COMPOSITION_RULE (c1); 2715 if (composition_state == COMPOSING_RULE
1763 continue; 2716 || composition_state == COMPOSING_COMPONENT_RULE)
2717 {
2718 DECODE_COMPOSITION_RULE (c1);
2719 components[component_idx++] = c1;
2720 composition_state--;
2721 continue;
2722 }
2723 else if (method == COMPOSITION_WITH_RULE)
2724 composition_state = COMPOSING_RULE;
2725 else if (method == COMPOSITION_WITH_RULE_ALTCHARS
2726 && composition_state == COMPOSING_COMPONENT_CHAR)
2727 composition_state = COMPOSING_COMPONENT_CHAR;
1764 } 2728 }
1765 if (charset0 < 0 || CHARSET_CHARS (charset0) == 94) 2729 if (charset_id_0 < 0
2730 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0)))
1766 { 2731 {
1767 /* This is SPACE or DEL. */ 2732 /* This is SPACE or DEL. */
1768 charset = CHARSET_ASCII; 2733 charset = CHARSET_FROM_ID (charset_ascii);
1769 break; 2734 break;
1770 } 2735 }
1771 /* This is a graphic character, we fall down ... */ 2736 /* This is a graphic character, we fall down ... */
1772 2737
1773 case ISO_graphic_plane_0: 2738 case ISO_graphic_plane_0:
1774 if (COMPOSING_P (coding) && coding->composition_rule_follows) 2739 if (composition_state == COMPOSING_RULE)
1775 { 2740 {
1776 DECODE_COMPOSITION_RULE (c1); 2741 DECODE_COMPOSITION_RULE (c1);
1777 continue; 2742 components[component_idx++] = c1;
2743 composition_state = COMPOSING_CHAR;
1778 } 2744 }
1779 charset = charset0; 2745 charset = CHARSET_FROM_ID (charset_id_0);
1780 break; 2746 break;
1781 2747
1782 case ISO_0xA0_or_0xFF: 2748 case ISO_0xA0_or_0xFF:
1783 if (charset1 < 0 || CHARSET_CHARS (charset1) == 94 2749 if (charset_id_1 < 0
1784 || coding->flags & CODING_FLAG_ISO_SEVEN_BITS) 2750 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1))
1785 goto label_invalid_code; 2751 || CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS)
2752 goto invalid_code;
1786 /* This is a graphic character, we fall down ... */ 2753 /* This is a graphic character, we fall down ... */
1787 2754
1788 case ISO_graphic_plane_1: 2755 case ISO_graphic_plane_1:
1789 if (charset1 < 0) 2756 if (charset_id_1 < 0)
1790 goto label_invalid_code; 2757 goto invalid_code;
1791 charset = charset1; 2758 charset = CHARSET_FROM_ID (charset_id_1);
1792 break; 2759 break;
1793 2760
2761 case ISO_carriage_return:
2762 if (c1 == '\r')
2763 {
2764 if (EQ (eol_type, Qdos))
2765 {
2766 if (src == src_end)
2767 goto no_more_source;
2768 if (*src == '\n')
2769 ONE_MORE_BYTE (c1);
2770 }
2771 else if (EQ (eol_type, Qmac))
2772 c1 = '\n';
2773 }
2774 /* fall through */
2775
1794 case ISO_control_0: 2776 case ISO_control_0:
1795 if (COMPOSING_P (coding)) 2777 MAYBE_FINISH_COMPOSITION ();
1796 DECODE_COMPOSITION_END ('1'); 2778 charset = CHARSET_FROM_ID (charset_ascii);
1797
1798 /* All ISO2022 control characters in this class have the
1799 same representation in Emacs internal format. */
1800 if (c1 == '\n'
1801 && (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
1802 && (coding->eol_type == CODING_EOL_CR
1803 || coding->eol_type == CODING_EOL_CRLF))
1804 {
1805 coding->result = CODING_FINISH_INCONSISTENT_EOL;
1806 goto label_end_of_loop;
1807 }
1808 charset = CHARSET_ASCII;
1809 break; 2779 break;
1810 2780
1811 case ISO_control_1: 2781 case ISO_control_1:
1812 if (COMPOSING_P (coding)) 2782 MAYBE_FINISH_COMPOSITION ();
1813 DECODE_COMPOSITION_END ('1'); 2783 goto invalid_code;
1814 goto label_invalid_code;
1815
1816 case ISO_carriage_return:
1817 if (COMPOSING_P (coding))
1818 DECODE_COMPOSITION_END ('1');
1819
1820 if (coding->eol_type == CODING_EOL_CR)
1821 c1 = '\n';
1822 else if (coding->eol_type == CODING_EOL_CRLF)
1823 {
1824 ONE_MORE_BYTE (c1);
1825 if (c1 != ISO_CODE_LF)
1826 {
1827 if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
1828 {
1829 coding->result = CODING_FINISH_INCONSISTENT_EOL;
1830 goto label_end_of_loop;
1831 }
1832 src--;
1833 c1 = '\r';
1834 }
1835 }
1836 charset = CHARSET_ASCII;
1837 break;
1838 2784
1839 case ISO_shift_out: 2785 case ISO_shift_out:
1840 if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT) 2786 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT)
1841 || CODING_SPEC_ISO_DESIGNATION (coding, 1) < 0) 2787 || CODING_ISO_DESIGNATION (coding, 1) < 0)
1842 goto label_invalid_code; 2788 goto invalid_code;
1843 CODING_SPEC_ISO_INVOCATION (coding, 0) = 1; 2789 CODING_ISO_INVOCATION (coding, 0) = 1;
1844 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0); 2790 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
1845 continue; 2791 continue;
1846 2792
1847 case ISO_shift_in: 2793 case ISO_shift_in:
1848 if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT)) 2794 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT))
1849 goto label_invalid_code; 2795 goto invalid_code;
1850 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0; 2796 CODING_ISO_INVOCATION (coding, 0) = 0;
1851 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0); 2797 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
1852 continue; 2798 continue;
1853 2799
1854 case ISO_single_shift_2_7: 2800 case ISO_single_shift_2_7:
1855 case ISO_single_shift_2: 2801 case ISO_single_shift_2:
1856 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT)) 2802 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT))
1857 goto label_invalid_code; 2803 goto invalid_code;
1858 /* SS2 is handled as an escape sequence of ESC 'N' */ 2804 /* SS2 is handled as an escape sequence of ESC 'N' */
1859 c1 = 'N'; 2805 c1 = 'N';
1860 goto label_escape_sequence; 2806 goto label_escape_sequence;
1861 2807
1862 case ISO_single_shift_3: 2808 case ISO_single_shift_3:
1863 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT)) 2809 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT))
1864 goto label_invalid_code; 2810 goto invalid_code;
1865 /* SS2 is handled as an escape sequence of ESC 'O' */ 2811 /* SS2 is handled as an escape sequence of ESC 'O' */
1866 c1 = 'O'; 2812 c1 = 'O';
1867 goto label_escape_sequence; 2813 goto label_escape_sequence;
1868 2814
1869 case ISO_control_sequence_introducer: 2815 case ISO_control_sequence_introducer:
1872 goto label_escape_sequence; 2818 goto label_escape_sequence;
1873 2819
1874 case ISO_escape: 2820 case ISO_escape:
1875 ONE_MORE_BYTE (c1); 2821 ONE_MORE_BYTE (c1);
1876 label_escape_sequence: 2822 label_escape_sequence:
1877 /* Escape sequences handled by Emacs are invocation, 2823 /* Escape sequences handled here are invocation,
1878 designation, direction specification, and character 2824 designation, direction specification, and character
1879 composition specification. */ 2825 composition specification. */
1880 switch (c1) 2826 switch (c1)
1881 { 2827 {
1882 case '&': /* revision of following character set */ 2828 case '&': /* revision of following character set */
1883 ONE_MORE_BYTE (c1); 2829 ONE_MORE_BYTE (c1);
1884 if (!(c1 >= '@' && c1 <= '~')) 2830 if (!(c1 >= '@' && c1 <= '~'))
1885 goto label_invalid_code; 2831 goto invalid_code;
1886 ONE_MORE_BYTE (c1); 2832 ONE_MORE_BYTE (c1);
1887 if (c1 != ISO_CODE_ESC) 2833 if (c1 != ISO_CODE_ESC)
1888 goto label_invalid_code; 2834 goto invalid_code;
1889 ONE_MORE_BYTE (c1); 2835 ONE_MORE_BYTE (c1);
1890 goto label_escape_sequence; 2836 goto label_escape_sequence;
1891 2837
1892 case '$': /* designation of 2-byte character set */ 2838 case '$': /* designation of 2-byte character set */
1893 if (! (coding->flags & CODING_FLAG_ISO_DESIGNATION)) 2839 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION))
1894 goto label_invalid_code; 2840 goto invalid_code;
1895 ONE_MORE_BYTE (c1); 2841 ONE_MORE_BYTE (c1);
1896 if (c1 >= '@' && c1 <= 'B') 2842 if (c1 >= '@' && c1 <= 'B')
1897 { /* designation of JISX0208.1978, GB2312.1980, 2843 { /* designation of JISX0208.1978, GB2312.1980,
1898 or JISX0208.1980 */ 2844 or JISX0208.1980 */
1899 DECODE_DESIGNATION (0, 2, 94, c1); 2845 DECODE_DESIGNATION (0, 2, 0, c1);
1900 } 2846 }
1901 else if (c1 >= 0x28 && c1 <= 0x2B) 2847 else if (c1 >= 0x28 && c1 <= 0x2B)
1902 { /* designation of DIMENSION2_CHARS94 character set */ 2848 { /* designation of DIMENSION2_CHARS94 character set */
1903 ONE_MORE_BYTE (c2); 2849 ONE_MORE_BYTE (c2);
1904 DECODE_DESIGNATION (c1 - 0x28, 2, 94, c2); 2850 DECODE_DESIGNATION (c1 - 0x28, 2, 0, c2);
1905 } 2851 }
1906 else if (c1 >= 0x2C && c1 <= 0x2F) 2852 else if (c1 >= 0x2C && c1 <= 0x2F)
1907 { /* designation of DIMENSION2_CHARS96 character set */ 2853 { /* designation of DIMENSION2_CHARS96 character set */
1908 ONE_MORE_BYTE (c2); 2854 ONE_MORE_BYTE (c2);
1909 DECODE_DESIGNATION (c1 - 0x2C, 2, 96, c2); 2855 DECODE_DESIGNATION (c1 - 0x2C, 2, 1, c2);
1910 } 2856 }
1911 else 2857 else
1912 goto label_invalid_code; 2858 goto invalid_code;
1913 /* We must update these variables now. */ 2859 /* We must update these variables now. */
1914 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0); 2860 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
1915 charset1 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 1); 2861 charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1);
1916 continue; 2862 continue;
1917 2863
1918 case 'n': /* invocation of locking-shift-2 */ 2864 case 'n': /* invocation of locking-shift-2 */
1919 if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT) 2865 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT)
1920 || CODING_SPEC_ISO_DESIGNATION (coding, 2) < 0) 2866 || CODING_ISO_DESIGNATION (coding, 2) < 0)
1921 goto label_invalid_code; 2867 goto invalid_code;
1922 CODING_SPEC_ISO_INVOCATION (coding, 0) = 2; 2868 CODING_ISO_INVOCATION (coding, 0) = 2;
1923 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0); 2869 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
1924 continue; 2870 continue;
1925 2871
1926 case 'o': /* invocation of locking-shift-3 */ 2872 case 'o': /* invocation of locking-shift-3 */
1927 if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT) 2873 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT)
1928 || CODING_SPEC_ISO_DESIGNATION (coding, 3) < 0) 2874 || CODING_ISO_DESIGNATION (coding, 3) < 0)
1929 goto label_invalid_code; 2875 goto invalid_code;
1930 CODING_SPEC_ISO_INVOCATION (coding, 0) = 3; 2876 CODING_ISO_INVOCATION (coding, 0) = 3;
1931 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0); 2877 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
1932 continue; 2878 continue;
1933 2879
1934 case 'N': /* invocation of single-shift-2 */ 2880 case 'N': /* invocation of single-shift-2 */
1935 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT) 2881 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)
1936 || CODING_SPEC_ISO_DESIGNATION (coding, 2) < 0) 2882 || CODING_ISO_DESIGNATION (coding, 2) < 0)
1937 goto label_invalid_code; 2883 goto invalid_code;
1938 charset = CODING_SPEC_ISO_DESIGNATION (coding, 2); 2884 charset = CHARSET_FROM_ID (CODING_ISO_DESIGNATION (coding, 2));
1939 ONE_MORE_BYTE (c1); 2885 ONE_MORE_BYTE (c1);
1940 if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0)) 2886 if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0))
1941 goto label_invalid_code; 2887 goto invalid_code;
1942 break; 2888 break;
1943 2889
1944 case 'O': /* invocation of single-shift-3 */ 2890 case 'O': /* invocation of single-shift-3 */
1945 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT) 2891 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)
1946 || CODING_SPEC_ISO_DESIGNATION (coding, 3) < 0) 2892 || CODING_ISO_DESIGNATION (coding, 3) < 0)
1947 goto label_invalid_code; 2893 goto invalid_code;
1948 charset = CODING_SPEC_ISO_DESIGNATION (coding, 3); 2894 charset = CHARSET_FROM_ID (CODING_ISO_DESIGNATION (coding, 3));
1949 ONE_MORE_BYTE (c1); 2895 ONE_MORE_BYTE (c1);
1950 if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0)) 2896 if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0))
1951 goto label_invalid_code; 2897 goto invalid_code;
1952 break; 2898 break;
1953 2899
1954 case '0': case '2': case '3': case '4': /* start composition */ 2900 case '0': case '2': case '3': case '4': /* start composition */
2901 if (! (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK))
2902 goto invalid_code;
1955 DECODE_COMPOSITION_START (c1); 2903 DECODE_COMPOSITION_START (c1);
1956 continue; 2904 continue;
1957 2905
1958 case '1': /* end composition */ 2906 case '1': /* end composition */
1959 DECODE_COMPOSITION_END (c1); 2907 if (composition_state == COMPOSING_NO)
2908 goto invalid_code;
2909 DECODE_COMPOSITION_END ();
1960 continue; 2910 continue;
1961 2911
1962 case '[': /* specification of direction */ 2912 case '[': /* specification of direction */
1963 if (coding->flags & CODING_FLAG_ISO_NO_DIRECTION) 2913 if (! CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DIRECTION)
1964 goto label_invalid_code; 2914 goto invalid_code;
1965 /* For the moment, nested direction is not supported. 2915 /* For the moment, nested direction is not supported.
1966 So, `coding->mode & CODING_MODE_DIRECTION' zero means 2916 So, `coding->mode & CODING_MODE_DIRECTION' zero means
1967 left-to-right, and nonzero means right-to-left. */ 2917 left-to-right, and nozero means right-to-left. */
1968 ONE_MORE_BYTE (c1); 2918 ONE_MORE_BYTE (c1);
1969 switch (c1) 2919 switch (c1)
1970 { 2920 {
1971 case ']': /* end of the current direction */ 2921 case ']': /* end of the current direction */
1972 coding->mode &= ~CODING_MODE_DIRECTION; 2922 coding->mode &= ~CODING_MODE_DIRECTION;
1975 case '1': /* start of left-to-right direction */ 2925 case '1': /* start of left-to-right direction */
1976 ONE_MORE_BYTE (c1); 2926 ONE_MORE_BYTE (c1);
1977 if (c1 == ']') 2927 if (c1 == ']')
1978 coding->mode &= ~CODING_MODE_DIRECTION; 2928 coding->mode &= ~CODING_MODE_DIRECTION;
1979 else 2929 else
1980 goto label_invalid_code; 2930 goto invalid_code;
1981 break; 2931 break;
1982 2932
1983 case '2': /* start of right-to-left direction */ 2933 case '2': /* start of right-to-left direction */
1984 ONE_MORE_BYTE (c1); 2934 ONE_MORE_BYTE (c1);
1985 if (c1 == ']') 2935 if (c1 == ']')
1986 coding->mode |= CODING_MODE_DIRECTION; 2936 coding->mode |= CODING_MODE_DIRECTION;
1987 else 2937 else
1988 goto label_invalid_code; 2938 goto invalid_code;
1989 break; 2939 break;
1990 2940
1991 default: 2941 default:
1992 goto label_invalid_code; 2942 goto invalid_code;
1993 } 2943 }
1994 continue; 2944 continue;
1995 2945
1996 default: 2946 default:
1997 if (! (coding->flags & CODING_FLAG_ISO_DESIGNATION)) 2947 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION))
1998 goto label_invalid_code; 2948 goto invalid_code;
1999 if (c1 >= 0x28 && c1 <= 0x2B) 2949 if (c1 >= 0x28 && c1 <= 0x2B)
2000 { /* designation of DIMENSION1_CHARS94 character set */ 2950 { /* designation of DIMENSION1_CHARS94 character set */
2001 ONE_MORE_BYTE (c2); 2951 ONE_MORE_BYTE (c2);
2002 DECODE_DESIGNATION (c1 - 0x28, 1, 94, c2); 2952 DECODE_DESIGNATION (c1 - 0x28, 1, 0, c2);
2003 } 2953 }
2004 else if (c1 >= 0x2C && c1 <= 0x2F) 2954 else if (c1 >= 0x2C && c1 <= 0x2F)
2005 { /* designation of DIMENSION1_CHARS96 character set */ 2955 { /* designation of DIMENSION1_CHARS96 character set */
2006 ONE_MORE_BYTE (c2); 2956 ONE_MORE_BYTE (c2);
2007 DECODE_DESIGNATION (c1 - 0x2C, 1, 96, c2); 2957 DECODE_DESIGNATION (c1 - 0x2C, 1, 1, c2);
2008 } 2958 }
2009 else 2959 else
2010 goto label_invalid_code; 2960 goto invalid_code;
2011 /* We must update these variables now. */ 2961 /* We must update these variables now. */
2012 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0); 2962 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
2013 charset1 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 1); 2963 charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1);
2014 continue; 2964 continue;
2015 } 2965 }
2016 } 2966 }
2017 2967
2018 /* Now we know CHARSET and 1st position code C1 of a character. 2968 /* Now we know CHARSET and 1st position code C1 of a character.
2019 Produce a multibyte sequence for that character while getting 2969 Produce a decoded character while getting 2nd position code
2020 2nd position code C2 if necessary. */ 2970 C2 if necessary. */
2021 if (CHARSET_DIMENSION (charset) == 2) 2971 c1 &= 0x7F;
2972 if (CHARSET_DIMENSION (charset) > 1)
2022 { 2973 {
2023 ONE_MORE_BYTE (c2); 2974 ONE_MORE_BYTE (c2);
2024 if (c1 < 0x80 ? c2 < 0x20 || c2 >= 0x80 : c2 < 0xA0) 2975 if (c2 < 0x20 || (c2 >= 0x80 && c2 < 0xA0))
2025 /* C2 is not in a valid range. */ 2976 /* C2 is not in a valid range. */
2026 goto label_invalid_code; 2977 goto invalid_code;
2027 } 2978 c1 = (c1 << 8) | (c2 & 0x7F);
2028 c = DECODE_ISO_CHARACTER (charset, c1, c2); 2979 if (CHARSET_DIMENSION (charset) > 2)
2029 EMIT_CHAR (c); 2980 {
2981 ONE_MORE_BYTE (c2);
2982 if (c2 < 0x20 || (c2 >= 0x80 && c2 < 0xA0))
2983 /* C2 is not in a valid range. */
2984 goto invalid_code;
2985 c1 = (c1 << 8) | (c2 & 0x7F);
2986 }
2987 }
2988
2989 CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c1, c);
2990 if (c < 0)
2991 {
2992 MAYBE_FINISH_COMPOSITION ();
2993 for (; src_base < src; src_base++, char_offset++)
2994 {
2995 if (ASCII_BYTE_P (*src_base))
2996 *charbuf++ = *src_base;
2997 else
2998 *charbuf++ = BYTE8_TO_CHAR (*src_base);
2999 }
3000 }
3001 else if (composition_state == COMPOSING_NO)
3002 {
3003 *charbuf++ = c;
3004 char_offset++;
3005 }
3006 else
3007 components[component_idx++] = c;
2030 continue; 3008 continue;
2031 3009
2032 label_invalid_code: 3010 invalid_code:
3011 MAYBE_FINISH_COMPOSITION ();
3012 src = src_base;
3013 consumed_chars = consumed_chars_base;
3014 ONE_MORE_BYTE (c);
3015 *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
2033 coding->errors++; 3016 coding->errors++;
2034 if (COMPOSING_P (coding)) 3017 }
2035 DECODE_COMPOSITION_END ('1'); 3018
2036 src = src_base; 3019 no_more_source:
2037 c = *src++; 3020 coding->consumed_char += consumed_chars_base;
2038 EMIT_CHAR (c); 3021 coding->consumed = src_base - coding->source;
2039 } 3022 coding->charbuf_used = charbuf - coding->charbuf;
2040
2041 label_end_of_loop:
2042 coding->consumed = coding->consumed_char = src_base - source;
2043 coding->produced = dst - destination;
2044 return;
2045 } 3023 }
2046 3024
2047 3025
2048 /* ISO2022 encoding stuff. */ 3026 /* ISO2022 encoding stuff. */
2049 3027
2050 /* 3028 /*
2051 It is not enough to say just "ISO2022" on encoding, we have to 3029 It is not enough to say just "ISO2022" on encoding, we have to
2052 specify more details. In Emacs, each ISO2022 coding system 3030 specify more details. In Emacs, each coding system of ISO2022
2053 variant has the following specifications: 3031 variant has the following specifications:
2054 1. Initial designation to G0 through G3. 3032 1. Initial designation to G0 thru G3.
2055 2. Allows short-form designation? 3033 2. Allows short-form designation?
2056 3. ASCII should be designated to G0 before control characters? 3034 3. ASCII should be designated to G0 before control characters?
2057 4. ASCII should be designated to G0 at end of line? 3035 4. ASCII should be designated to G0 at end of line?
2058 5. 7-bit environment or 8-bit environment? 3036 5. 7-bit environment or 8-bit environment?
2059 6. Use locking-shift? 3037 6. Use locking-shift?
2060 7. Use Single-shift? 3038 7. Use Single-shift?
2061 And the following two are only for Japanese: 3039 And the following two are only for Japanese:
2062 8. Use ASCII in place of JIS0201-1976-Roman? 3040 8. Use ASCII in place of JIS0201-1976-Roman?
2063 9. Use JISX0208-1983 in place of JISX0208-1978? 3041 9. Use JISX0208-1983 in place of JISX0208-1978?
2064 These specifications are encoded in `coding->flags' as flag bits 3042 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
2065 defined by macros CODING_FLAG_ISO_XXX. See `coding.h' for more 3043 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
2066 details. 3044 details.
2067 */ 3045 */
2068 3046
2069 /* Produce codes (escape sequence) for designating CHARSET to graphic 3047 /* Produce codes (escape sequence) for designating CHARSET to graphic
2070 register REG at DST, and increment DST. If <final-char> of CHARSET is 3048 register REG at DST, and increment DST. If <final-char> of CHARSET is
2071 '@', 'A', or 'B' and the coding system CODING allows, produce 3049 '@', 'A', or 'B' and the coding system CODING allows, produce
2072 designation sequence of short-form. */ 3050 designation sequence of short-form. */
2073 3051
2074 #define ENCODE_DESIGNATION(charset, reg, coding) \ 3052 #define ENCODE_DESIGNATION(charset, reg, coding) \
2075 do { \ 3053 do { \
2076 unsigned char final_char = CHARSET_ISO_FINAL_CHAR (charset); \ 3054 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
2077 char *intermediate_char_94 = "()*+"; \ 3055 char *intermediate_char_94 = "()*+"; \
2078 char *intermediate_char_96 = ",-./"; \ 3056 char *intermediate_char_96 = ",-./"; \
2079 int revision = CODING_SPEC_ISO_REVISION_NUMBER(coding, charset); \ 3057 int revision = -1; \
2080 \ 3058 int c; \
2081 if (revision < 255) \ 3059 \
3060 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
3061 revision = XINT (CHARSET_ISO_REVISION (charset)); \
3062 \
3063 if (revision >= 0) \
2082 { \ 3064 { \
2083 *dst++ = ISO_CODE_ESC; \ 3065 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
2084 *dst++ = '&'; \ 3066 EMIT_ONE_BYTE ('@' + revision); \
2085 *dst++ = '@' + revision; \
2086 } \ 3067 } \
2087 *dst++ = ISO_CODE_ESC; \ 3068 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
2088 if (CHARSET_DIMENSION (charset) == 1) \ 3069 if (CHARSET_DIMENSION (charset) == 1) \
2089 { \ 3070 { \
2090 if (CHARSET_CHARS (charset) == 94) \ 3071 if (! CHARSET_ISO_CHARS_96 (charset)) \
2091 *dst++ = (unsigned char) (intermediate_char_94[reg]); \ 3072 c = intermediate_char_94[reg]; \
2092 else \ 3073 else \
2093 *dst++ = (unsigned char) (intermediate_char_96[reg]); \ 3074 c = intermediate_char_96[reg]; \
3075 EMIT_ONE_ASCII_BYTE (c); \
2094 } \ 3076 } \
2095 else \ 3077 else \
2096 { \ 3078 { \
2097 *dst++ = '$'; \ 3079 EMIT_ONE_ASCII_BYTE ('$'); \
2098 if (CHARSET_CHARS (charset) == 94) \ 3080 if (! CHARSET_ISO_CHARS_96 (charset)) \
2099 { \ 3081 { \
2100 if (! (coding->flags & CODING_FLAG_ISO_SHORT_FORM) \ 3082 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
2101 || reg != 0 \ 3083 || reg != 0 \
2102 || final_char < '@' || final_char > 'B') \ 3084 || final_char < '@' || final_char > 'B') \
2103 *dst++ = (unsigned char) (intermediate_char_94[reg]); \ 3085 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
2104 } \ 3086 } \
2105 else \ 3087 else \
2106 *dst++ = (unsigned char) (intermediate_char_96[reg]); \ 3088 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
2107 } \ 3089 } \
2108 *dst++ = final_char; \ 3090 EMIT_ONE_ASCII_BYTE (final_char); \
2109 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \ 3091 \
3092 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
2110 } while (0) 3093 } while (0)
3094
2111 3095
2112 /* The following two macros produce codes (control character or escape 3096 /* The following two macros produce codes (control character or escape
2113 sequence) for ISO2022 single-shift functions (single-shift-2 and 3097 sequence) for ISO2022 single-shift functions (single-shift-2 and
2114 single-shift-3). */ 3098 single-shift-3). */
2115 3099
2116 #define ENCODE_SINGLE_SHIFT_2 \ 3100 #define ENCODE_SINGLE_SHIFT_2 \
2117 do { \ 3101 do { \
2118 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \ 3102 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
2119 *dst++ = ISO_CODE_ESC, *dst++ = 'N'; \ 3103 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
2120 else \ 3104 else \
2121 *dst++ = ISO_CODE_SS2; \ 3105 EMIT_ONE_BYTE (ISO_CODE_SS2); \
2122 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \ 3106 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
2123 } while (0) 3107 } while (0)
2124 3108
2125 #define ENCODE_SINGLE_SHIFT_3 \ 3109
2126 do { \ 3110 #define ENCODE_SINGLE_SHIFT_3 \
2127 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \ 3111 do { \
2128 *dst++ = ISO_CODE_ESC, *dst++ = 'O'; \ 3112 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
2129 else \ 3113 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
2130 *dst++ = ISO_CODE_SS3; \ 3114 else \
2131 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \ 3115 EMIT_ONE_BYTE (ISO_CODE_SS3); \
3116 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
2132 } while (0) 3117 } while (0)
3118
2133 3119
2134 /* The following four macros produce codes (control character or 3120 /* The following four macros produce codes (control character or
2135 escape sequence) for ISO2022 locking-shift functions (shift-in, 3121 escape sequence) for ISO2022 locking-shift functions (shift-in,
2136 shift-out, locking-shift-2, and locking-shift-3). */ 3122 shift-out, locking-shift-2, and locking-shift-3). */
2137 3123
2138 #define ENCODE_SHIFT_IN \ 3124 #define ENCODE_SHIFT_IN \
2139 do { \ 3125 do { \
2140 *dst++ = ISO_CODE_SI; \ 3126 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
2141 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0; \ 3127 CODING_ISO_INVOCATION (coding, 0) = 0; \
2142 } while (0) 3128 } while (0)
2143 3129
2144 #define ENCODE_SHIFT_OUT \ 3130
2145 do { \ 3131 #define ENCODE_SHIFT_OUT \
2146 *dst++ = ISO_CODE_SO; \ 3132 do { \
2147 CODING_SPEC_ISO_INVOCATION (coding, 0) = 1; \ 3133 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
3134 CODING_ISO_INVOCATION (coding, 0) = 1; \
2148 } while (0) 3135 } while (0)
2149 3136
2150 #define ENCODE_LOCKING_SHIFT_2 \ 3137
2151 do { \ 3138 #define ENCODE_LOCKING_SHIFT_2 \
2152 *dst++ = ISO_CODE_ESC, *dst++ = 'n'; \ 3139 do { \
2153 CODING_SPEC_ISO_INVOCATION (coding, 0) = 2; \ 3140 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
3141 CODING_ISO_INVOCATION (coding, 0) = 2; \
2154 } while (0) 3142 } while (0)
2155 3143
2156 #define ENCODE_LOCKING_SHIFT_3 \ 3144
2157 do { \ 3145 #define ENCODE_LOCKING_SHIFT_3 \
2158 *dst++ = ISO_CODE_ESC, *dst++ = 'o'; \ 3146 do { \
2159 CODING_SPEC_ISO_INVOCATION (coding, 0) = 3; \ 3147 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
3148 CODING_ISO_INVOCATION (coding, 0) = 3; \
2160 } while (0) 3149 } while (0)
3150
2161 3151
2162 /* Produce codes for a DIMENSION1 character whose character set is 3152 /* Produce codes for a DIMENSION1 character whose character set is
2163 CHARSET and whose position-code is C1. Designation and invocation 3153 CHARSET and whose position-code is C1. Designation and invocation
2164 sequences are also produced in advance if necessary. */ 3154 sequences are also produced in advance if necessary. */
2165 3155
2166 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \ 3156 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
2167 do { \ 3157 do { \
2168 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \ 3158 int id = CHARSET_ID (charset); \
3159 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
2169 { \ 3160 { \
2170 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \ 3161 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
2171 *dst++ = c1 & 0x7F; \ 3162 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
2172 else \ 3163 else \
2173 *dst++ = c1 | 0x80; \ 3164 EMIT_ONE_BYTE (c1 | 0x80); \
2174 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \ 3165 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
2175 break; \ 3166 break; \
2176 } \ 3167 } \
2177 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \ 3168 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
2178 { \ 3169 { \
2179 *dst++ = c1 & 0x7F; \ 3170 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
2180 break; \ 3171 break; \
2181 } \ 3172 } \
2182 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \ 3173 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
2183 { \ 3174 { \
2184 *dst++ = c1 | 0x80; \ 3175 EMIT_ONE_BYTE (c1 | 0x80); \
2185 break; \ 3176 break; \
2186 } \ 3177 } \
2187 else \ 3178 else \
2188 /* Since CHARSET is not yet invoked to any graphic planes, we \ 3179 /* Since CHARSET is not yet invoked to any graphic planes, we \
2189 must invoke it, or, at first, designate it to some graphic \ 3180 must invoke it, or, at first, designate it to some graphic \
2190 register. Then repeat the loop to actually produce the \ 3181 register. Then repeat the loop to actually produce the \
2191 character. */ \ 3182 character. */ \
2192 dst = encode_invocation_designation (charset, coding, dst); \ 3183 dst = encode_invocation_designation (charset, coding, dst, \
3184 &produced_chars); \
2193 } while (1) 3185 } while (1)
3186
2194 3187
2195 /* Produce codes for a DIMENSION2 character whose character set is 3188 /* Produce codes for a DIMENSION2 character whose character set is
2196 CHARSET and whose position-codes are C1 and C2. Designation and 3189 CHARSET and whose position-codes are C1 and C2. Designation and
2197 invocation codes are also produced in advance if necessary. */ 3190 invocation codes are also produced in advance if necessary. */
2198 3191
2199 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \ 3192 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
2200 do { \ 3193 do { \
2201 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \ 3194 int id = CHARSET_ID (charset); \
3195 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
2202 { \ 3196 { \
2203 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \ 3197 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
2204 *dst++ = c1 & 0x7F, *dst++ = c2 & 0x7F; \ 3198 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
2205 else \ 3199 else \
2206 *dst++ = c1 | 0x80, *dst++ = c2 | 0x80; \ 3200 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
2207 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \ 3201 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
2208 break; \ 3202 break; \
2209 } \ 3203 } \
2210 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \ 3204 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
2211 { \ 3205 { \
2212 *dst++ = c1 & 0x7F, *dst++= c2 & 0x7F; \ 3206 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
2213 break; \ 3207 break; \
2214 } \ 3208 } \
2215 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \ 3209 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
2216 { \ 3210 { \
2217 *dst++ = c1 | 0x80, *dst++= c2 | 0x80; \ 3211 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
2218 break; \ 3212 break; \
2219 } \ 3213 } \
2220 else \ 3214 else \
2221 /* Since CHARSET is not yet invoked to any graphic planes, we \ 3215 /* Since CHARSET is not yet invoked to any graphic planes, we \
2222 must invoke it, or, at first, designate it to some graphic \ 3216 must invoke it, or, at first, designate it to some graphic \
2223 register. Then repeat the loop to actually produce the \ 3217 register. Then repeat the loop to actually produce the \
2224 character. */ \ 3218 character. */ \
2225 dst = encode_invocation_designation (charset, coding, dst); \ 3219 dst = encode_invocation_designation (charset, coding, dst, \
3220 &produced_chars); \
2226 } while (1) 3221 } while (1)
2227 3222
2228 #define ENCODE_ISO_CHARACTER(c) \ 3223
2229 do { \ 3224 #define ENCODE_ISO_CHARACTER(charset, c) \
2230 int charset, c1, c2; \ 3225 do { \
2231 \ 3226 int code = ENCODE_CHAR ((charset),(c)); \
2232 SPLIT_CHAR (c, charset, c1, c2); \ 3227 \
2233 if (CHARSET_DEFINED_P (charset)) \ 3228 if (CHARSET_DIMENSION (charset) == 1) \
2234 { \ 3229 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
2235 if (CHARSET_DIMENSION (charset) == 1) \ 3230 else \
2236 { \ 3231 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
2237 if (charset == CHARSET_ASCII \
2238 && coding->flags & CODING_FLAG_ISO_USE_ROMAN) \
2239 charset = charset_latin_jisx0201; \
2240 ENCODE_ISO_CHARACTER_DIMENSION1 (charset, c1); \
2241 } \
2242 else \
2243 { \
2244 if (charset == charset_jisx0208 \
2245 && coding->flags & CODING_FLAG_ISO_USE_OLDJIS) \
2246 charset = charset_jisx0208_1978; \
2247 ENCODE_ISO_CHARACTER_DIMENSION2 (charset, c1, c2); \
2248 } \
2249 } \
2250 else \
2251 { \
2252 *dst++ = c1; \
2253 if (c2 >= 0) \
2254 *dst++ = c2; \
2255 } \
2256 } while (0) 3232 } while (0)
2257 3233
2258 3234
2259 /* Instead of encoding character C, produce one or two `?'s. */
2260
2261 #define ENCODE_UNSAFE_CHARACTER(c) \
2262 do { \
2263 ENCODE_ISO_CHARACTER (CODING_INHIBIT_CHARACTER_SUBSTITUTION); \
2264 if (CHARSET_WIDTH (CHAR_CHARSET (c)) > 1) \
2265 ENCODE_ISO_CHARACTER (CODING_INHIBIT_CHARACTER_SUBSTITUTION); \
2266 } while (0)
2267
2268
2269 /* Produce designation and invocation codes at a place pointed by DST 3235 /* Produce designation and invocation codes at a place pointed by DST
2270 to use CHARSET. The element `spec.iso2022' of *CODING is updated. 3236 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
2271 Return new DST. */ 3237 Return new DST. */
2272 3238
2273 unsigned char * 3239 unsigned char *
2274 encode_invocation_designation (charset, coding, dst) 3240 encode_invocation_designation (charset, coding, dst, p_nchars)
2275 int charset; 3241 struct charset *charset;
2276 struct coding_system *coding; 3242 struct coding_system *coding;
2277 unsigned char *dst; 3243 unsigned char *dst;
2278 { 3244 int *p_nchars;
3245 {
3246 int multibytep = coding->dst_multibyte;
3247 int produced_chars = *p_nchars;
2279 int reg; /* graphic register number */ 3248 int reg; /* graphic register number */
3249 int id = CHARSET_ID (charset);
2280 3250
2281 /* At first, check designations. */ 3251 /* At first, check designations. */
2282 for (reg = 0; reg < 4; reg++) 3252 for (reg = 0; reg < 4; reg++)
2283 if (charset == CODING_SPEC_ISO_DESIGNATION (coding, reg)) 3253 if (id == CODING_ISO_DESIGNATION (coding, reg))
2284 break; 3254 break;
2285 3255
2286 if (reg >= 4) 3256 if (reg >= 4)
2287 { 3257 {
2288 /* CHARSET is not yet designated to any graphic registers. */ 3258 /* CHARSET is not yet designated to any graphic registers. */
2289 /* At first check the requested designation. */ 3259 /* At first check the requested designation. */
2290 reg = CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset); 3260 reg = CODING_ISO_REQUEST (coding, id);
2291 if (reg == CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION) 3261 if (reg < 0)
2292 /* Since CHARSET requests no special designation, designate it 3262 /* Since CHARSET requests no special designation, designate it
2293 to graphic register 0. */ 3263 to graphic register 0. */
2294 reg = 0; 3264 reg = 0;
2295 3265
2296 ENCODE_DESIGNATION (charset, reg, coding); 3266 ENCODE_DESIGNATION (charset, reg, coding);
2297 } 3267 }
2298 3268
2299 if (CODING_SPEC_ISO_INVOCATION (coding, 0) != reg 3269 if (CODING_ISO_INVOCATION (coding, 0) != reg
2300 && CODING_SPEC_ISO_INVOCATION (coding, 1) != reg) 3270 && CODING_ISO_INVOCATION (coding, 1) != reg)
2301 { 3271 {
2302 /* Since the graphic register REG is not invoked to any graphic 3272 /* Since the graphic register REG is not invoked to any graphic
2303 planes, invoke it to graphic plane 0. */ 3273 planes, invoke it to graphic plane 0. */
2304 switch (reg) 3274 switch (reg)
2305 { 3275 {
2310 case 1: /* graphic register 1 */ 3280 case 1: /* graphic register 1 */
2311 ENCODE_SHIFT_OUT; 3281 ENCODE_SHIFT_OUT;
2312 break; 3282 break;
2313 3283
2314 case 2: /* graphic register 2 */ 3284 case 2: /* graphic register 2 */
2315 if (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT) 3285 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)
2316 ENCODE_SINGLE_SHIFT_2; 3286 ENCODE_SINGLE_SHIFT_2;
2317 else 3287 else
2318 ENCODE_LOCKING_SHIFT_2; 3288 ENCODE_LOCKING_SHIFT_2;
2319 break; 3289 break;
2320 3290
2321 case 3: /* graphic register 3 */ 3291 case 3: /* graphic register 3 */
2322 if (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT) 3292 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)
2323 ENCODE_SINGLE_SHIFT_3; 3293 ENCODE_SINGLE_SHIFT_3;
2324 else 3294 else
2325 ENCODE_LOCKING_SHIFT_3; 3295 ENCODE_LOCKING_SHIFT_3;
2326 break; 3296 break;
2327 } 3297 }
2328 } 3298 }
2329 3299
3300 *p_nchars = produced_chars;
2330 return dst; 3301 return dst;
2331 } 3302 }
2332
2333 /* Produce 2-byte codes for encoded composition rule RULE. */
2334
2335 #define ENCODE_COMPOSITION_RULE(rule) \
2336 do { \
2337 int gref, nref; \
2338 COMPOSITION_DECODE_RULE (rule, gref, nref); \
2339 *dst++ = 32 + 81 + gref; \
2340 *dst++ = 32 + nref; \
2341 } while (0)
2342
2343 /* Produce codes for indicating the start of a composition sequence
2344 (ESC 0, ESC 3, or ESC 4). DATA points to an array of integers
2345 which specify information about the composition. See the comment
2346 in coding.h for the format of DATA. */
2347
2348 #define ENCODE_COMPOSITION_START(coding, data) \
2349 do { \
2350 coding->composing = data[3]; \
2351 *dst++ = ISO_CODE_ESC; \
2352 if (coding->composing == COMPOSITION_RELATIVE) \
2353 *dst++ = '0'; \
2354 else \
2355 { \
2356 *dst++ = (coding->composing == COMPOSITION_WITH_ALTCHARS \
2357 ? '3' : '4'); \
2358 coding->cmp_data_index = coding->cmp_data_start + 4; \
2359 coding->composition_rule_follows = 0; \
2360 } \
2361 } while (0)
2362
2363 /* Produce codes for indicating the end of the current composition. */
2364
2365 #define ENCODE_COMPOSITION_END(coding, data) \
2366 do { \
2367 *dst++ = ISO_CODE_ESC; \
2368 *dst++ = '1'; \
2369 coding->cmp_data_start += data[0]; \
2370 coding->composing = COMPOSITION_NO; \
2371 if (coding->cmp_data_start == coding->cmp_data->used \
2372 && coding->cmp_data->next) \
2373 { \
2374 coding->cmp_data = coding->cmp_data->next; \
2375 coding->cmp_data_start = 0; \
2376 } \
2377 } while (0)
2378
2379 /* Produce composition start sequence ESC 0. Here, this sequence
2380 doesn't mean the start of a new composition but means that we have
2381 just produced components (alternate chars and composition rules) of
2382 the composition and the actual text follows in SRC. */
2383
2384 #define ENCODE_COMPOSITION_FAKE_START(coding) \
2385 do { \
2386 *dst++ = ISO_CODE_ESC; \
2387 *dst++ = '0'; \
2388 coding->composing = COMPOSITION_RELATIVE; \
2389 } while (0)
2390 3303
2391 /* The following three macros produce codes for indicating direction 3304 /* The following three macros produce codes for indicating direction
2392 of text. */ 3305 of text. */
2393 #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \ 3306 #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
2394 do { \ 3307 do { \
2395 if (coding->flags == CODING_FLAG_ISO_SEVEN_BITS) \ 3308 if (CODING_ISO_FLAGS (coding) == CODING_ISO_FLAG_SEVEN_BITS) \
2396 *dst++ = ISO_CODE_ESC, *dst++ = '['; \ 3309 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '['); \
2397 else \ 3310 else \
2398 *dst++ = ISO_CODE_CSI; \ 3311 EMIT_ONE_BYTE (ISO_CODE_CSI); \
2399 } while (0) 3312 } while (0)
2400 3313
2401 #define ENCODE_DIRECTION_R2L \ 3314
2402 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst), *dst++ = '2', *dst++ = ']' 3315 #define ENCODE_DIRECTION_R2L() \
2403 3316 do { \
2404 #define ENCODE_DIRECTION_L2R \ 3317 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
2405 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst), *dst++ = '0', *dst++ = ']' 3318 EMIT_TWO_ASCII_BYTES ('2', ']'); \
3319 } while (0)
3320
3321
3322 #define ENCODE_DIRECTION_L2R() \
3323 do { \
3324 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
3325 EMIT_TWO_ASCII_BYTES ('0', ']'); \
3326 } while (0)
3327
2406 3328
2407 /* Produce codes for designation and invocation to reset the graphic 3329 /* Produce codes for designation and invocation to reset the graphic
2408 planes and registers to initial state. */ 3330 planes and registers to initial state. */
2409 #define ENCODE_RESET_PLANE_AND_REGISTER \ 3331 #define ENCODE_RESET_PLANE_AND_REGISTER() \
2410 do { \ 3332 do { \
2411 int reg; \ 3333 int reg; \
2412 if (CODING_SPEC_ISO_INVOCATION (coding, 0) != 0) \ 3334 struct charset *charset; \
2413 ENCODE_SHIFT_IN; \ 3335 \
2414 for (reg = 0; reg < 4; reg++) \ 3336 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
2415 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg) >= 0 \ 3337 ENCODE_SHIFT_IN; \
2416 && (CODING_SPEC_ISO_DESIGNATION (coding, reg) \ 3338 for (reg = 0; reg < 4; reg++) \
2417 != CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg))) \ 3339 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
2418 ENCODE_DESIGNATION \ 3340 && (CODING_ISO_DESIGNATION (coding, reg) \
2419 (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg), reg, coding); \ 3341 != CODING_ISO_INITIAL (coding, reg))) \
3342 { \
3343 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
3344 ENCODE_DESIGNATION (charset, reg, coding); \
3345 } \
2420 } while (0) 3346 } while (0)
3347
2421 3348
2422 /* Produce designation sequences of charsets in the line started from 3349 /* Produce designation sequences of charsets in the line started from
2423 SRC to a place pointed by DST, and return updated DST. 3350 SRC to a place pointed by DST, and return updated DST.
2424 3351
2425 If the current block ends before any end-of-line, we may fail to 3352 If the current block ends before any end-of-line, we may fail to
2426 find all the necessary designations. */ 3353 find all the necessary designations. */
2427 3354
2428 static unsigned char * 3355 static unsigned char *
2429 encode_designation_at_bol (coding, translation_table, src, src_end, dst) 3356 encode_designation_at_bol (coding, charbuf, charbuf_end, dst)
2430 struct coding_system *coding; 3357 struct coding_system *coding;
2431 Lisp_Object translation_table; 3358 int *charbuf, *charbuf_end;
2432 unsigned char *src, *src_end, *dst; 3359 unsigned char *dst;
2433 { 3360 {
2434 int charset, c, found = 0, reg; 3361 struct charset *charset;
2435 /* Table of charsets to be designated to each graphic register. */ 3362 /* Table of charsets to be designated to each graphic register. */
2436 int r[4]; 3363 int r[4];
3364 int c, found = 0, reg;
3365 int produced_chars = 0;
3366 int multibytep = coding->dst_multibyte;
3367 Lisp_Object attrs;
3368 Lisp_Object charset_list;
3369
3370 attrs = CODING_ID_ATTRS (coding->id);
3371 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
3372 if (EQ (charset_list, Qiso_2022))
3373 charset_list = Viso_2022_charset_list;
2437 3374
2438 for (reg = 0; reg < 4; reg++) 3375 for (reg = 0; reg < 4; reg++)
2439 r[reg] = -1; 3376 r[reg] = -1;
2440 3377
2441 while (found < 4) 3378 while (found < 4)
2442 { 3379 {
2443 ONE_MORE_CHAR (c); 3380 int id;
3381
3382 c = *charbuf++;
2444 if (c == '\n') 3383 if (c == '\n')
2445 break; 3384 break;
2446 3385 charset = char_charset (c, charset_list, NULL);
2447 charset = CHAR_CHARSET (c); 3386 id = CHARSET_ID (charset);
2448 reg = CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset); 3387 reg = CODING_ISO_REQUEST (coding, id);
2449 if (reg != CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION && r[reg] < 0) 3388 if (reg >= 0 && r[reg] < 0)
2450 { 3389 {
2451 found++; 3390 found++;
2452 r[reg] = charset; 3391 r[reg] = id;
2453 } 3392 }
2454 } 3393 }
2455 3394
2456 label_end_of_loop:
2457 if (found) 3395 if (found)
2458 { 3396 {
2459 for (reg = 0; reg < 4; reg++) 3397 for (reg = 0; reg < 4; reg++)
2460 if (r[reg] >= 0 3398 if (r[reg] >= 0
2461 && CODING_SPEC_ISO_DESIGNATION (coding, reg) != r[reg]) 3399 && CODING_ISO_DESIGNATION (coding, reg) != r[reg])
2462 ENCODE_DESIGNATION (r[reg], reg, coding); 3400 ENCODE_DESIGNATION (CHARSET_FROM_ID (r[reg]), reg, coding);
2463 } 3401 }
2464 3402
2465 return dst; 3403 return dst;
2466 } 3404 }
2467 3405
2468 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */ 3406 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
2469 3407
2470 static void 3408 static int
2471 encode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes) 3409 encode_coding_iso_2022 (coding)
2472 struct coding_system *coding; 3410 struct coding_system *coding;
2473 unsigned char *source, *destination; 3411 {
2474 int src_bytes, dst_bytes; 3412 int multibytep = coding->dst_multibyte;
2475 { 3413 int *charbuf = coding->charbuf;
2476 unsigned char *src = source; 3414 int *charbuf_end = charbuf + coding->charbuf_used;
2477 unsigned char *src_end = source + src_bytes; 3415 unsigned char *dst = coding->destination + coding->produced;
2478 unsigned char *dst = destination; 3416 unsigned char *dst_end = coding->destination + coding->dst_bytes;
2479 unsigned char *dst_end = destination + dst_bytes; 3417 int safe_room = 16;
2480 /* Since the maximum bytes produced by each loop is 20, we subtract 19 3418 int bol_designation
2481 from DST_END to assure overflow checking is necessary only at the 3419 = (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
2482 head of loop. */ 3420 && CODING_ISO_BOL (coding));
2483 unsigned char *adjusted_dst_end = dst_end - 19; 3421 int produced_chars = 0;
2484 /* SRC_BASE remembers the start position in source in each loop. 3422 Lisp_Object attrs, eol_type, charset_list;
2485 The loop will be exited when there's not enough source text to 3423 int ascii_compatible;
2486 analyze multi-byte codes (within macro ONE_MORE_CHAR), or when
2487 there's not enough destination area to produce encoded codes
2488 (within macro EMIT_BYTES). */
2489 unsigned char *src_base;
2490 int c; 3424 int c;
2491 Lisp_Object translation_table; 3425
2492 Lisp_Object safe_chars; 3426 CODING_GET_INFO (coding, attrs, eol_type, charset_list);
2493 3427
2494 safe_chars = coding_safe_chars (coding); 3428 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
2495 3429
2496 if (NILP (Venable_character_translation)) 3430 while (charbuf < charbuf_end)
2497 translation_table = Qnil; 3431 {
2498 else 3432 ASSURE_DESTINATION (safe_room);
2499 { 3433
2500 translation_table = coding->translation_table_for_encode; 3434 if (bol_designation)
2501 if (NILP (translation_table)) 3435 {
2502 translation_table = Vstandard_translation_table_for_encode; 3436 unsigned char *dst_prev = dst;
2503 } 3437
2504
2505 coding->consumed_char = 0;
2506 coding->errors = 0;
2507 while (1)
2508 {
2509 src_base = src;
2510
2511 if (dst >= (dst_bytes ? adjusted_dst_end : (src - 19)))
2512 {
2513 coding->result = CODING_FINISH_INSUFFICIENT_DST;
2514 break;
2515 }
2516
2517 if (coding->flags & CODING_FLAG_ISO_DESIGNATE_AT_BOL
2518 && CODING_SPEC_ISO_BOL (coding))
2519 {
2520 /* We have to produce designation sequences if any now. */ 3438 /* We have to produce designation sequences if any now. */
2521 dst = encode_designation_at_bol (coding, translation_table, 3439 dst = encode_designation_at_bol (coding, charbuf, charbuf_end, dst);
2522 src, src_end, dst); 3440 bol_designation = 0;
2523 CODING_SPEC_ISO_BOL (coding) = 0; 3441 /* We are sure that designation sequences are all ASCII bytes. */
2524 } 3442 produced_chars += dst - dst_prev;
2525 3443 }
2526 /* Check composition start and end. */ 3444
2527 if (coding->composing != COMPOSITION_DISABLED 3445 c = *charbuf++;
2528 && coding->cmp_data_start < coding->cmp_data->used)
2529 {
2530 struct composition_data *cmp_data = coding->cmp_data;
2531 int *data = cmp_data->data + coding->cmp_data_start;
2532 int this_pos = cmp_data->char_offset + coding->consumed_char;
2533
2534 if (coding->composing == COMPOSITION_RELATIVE)
2535 {
2536 if (this_pos == data[2])
2537 {
2538 ENCODE_COMPOSITION_END (coding, data);
2539 cmp_data = coding->cmp_data;
2540 data = cmp_data->data + coding->cmp_data_start;
2541 }
2542 }
2543 else if (COMPOSING_P (coding))
2544 {
2545 /* COMPOSITION_WITH_ALTCHARS or COMPOSITION_WITH_RULE_ALTCHAR */
2546 if (coding->cmp_data_index == coding->cmp_data_start + data[0])
2547 /* We have consumed components of the composition.
2548 What follows in SRC is the composition's base
2549 text. */
2550 ENCODE_COMPOSITION_FAKE_START (coding);
2551 else
2552 {
2553 int c = cmp_data->data[coding->cmp_data_index++];
2554 if (coding->composition_rule_follows)
2555 {
2556 ENCODE_COMPOSITION_RULE (c);
2557 coding->composition_rule_follows = 0;
2558 }
2559 else
2560 {
2561 if (coding->flags & CODING_FLAG_ISO_SAFE
2562 && ! CODING_SAFE_CHAR_P (safe_chars, c))
2563 ENCODE_UNSAFE_CHARACTER (c);
2564 else
2565 ENCODE_ISO_CHARACTER (c);
2566 if (coding->composing == COMPOSITION_WITH_RULE_ALTCHARS)
2567 coding->composition_rule_follows = 1;
2568 }
2569 continue;
2570 }
2571 }
2572 if (!COMPOSING_P (coding))
2573 {
2574 if (this_pos == data[1])
2575 {
2576 ENCODE_COMPOSITION_START (coding, data);
2577 continue;
2578 }
2579 }
2580 }
2581
2582 ONE_MORE_CHAR (c);
2583 3446
2584 /* Now encode the character C. */ 3447 /* Now encode the character C. */
2585 if (c < 0x20 || c == 0x7F) 3448 if (c < 0x20 || c == 0x7F)
2586 { 3449 {
2587 if (c == '\r') 3450 if (c == '\n'
3451 || (c == '\r' && EQ (eol_type, Qmac)))
2588 { 3452 {
2589 if (! (coding->mode & CODING_MODE_SELECTIVE_DISPLAY)) 3453 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL)
3454 ENCODE_RESET_PLANE_AND_REGISTER ();
3455 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_INIT_AT_BOL)
2590 { 3456 {
2591 if (coding->flags & CODING_FLAG_ISO_RESET_AT_CNTL) 3457 int i;
2592 ENCODE_RESET_PLANE_AND_REGISTER; 3458
2593 *dst++ = c; 3459 for (i = 0; i < 4; i++)
2594 continue; 3460 CODING_ISO_DESIGNATION (coding, i)
3461 = CODING_ISO_INITIAL (coding, i);
2595 } 3462 }
2596 /* fall down to treat '\r' as '\n' ... */ 3463 bol_designation
2597 c = '\n'; 3464 = CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATE_AT_BOL;
2598 } 3465 }
2599 if (c == '\n') 3466 else if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_CNTL)
3467 ENCODE_RESET_PLANE_AND_REGISTER ();
3468 EMIT_ONE_ASCII_BYTE (c);
3469 }
3470 else if (ASCII_CHAR_P (c))
3471 {
3472 if (ascii_compatible)
3473 EMIT_ONE_ASCII_BYTE (c);
3474 else
3475 ENCODE_ISO_CHARACTER (CHARSET_FROM_ID (charset_ascii), c);
3476 }
3477 else
3478 {
3479 struct charset *charset = char_charset (c, charset_list, NULL);
3480
3481 if (!charset)
2600 { 3482 {
2601 if (coding->flags & CODING_FLAG_ISO_RESET_AT_EOL) 3483 c = coding->default_char;
2602 ENCODE_RESET_PLANE_AND_REGISTER; 3484 charset = char_charset (c, charset_list, NULL);
2603 if (coding->flags & CODING_FLAG_ISO_INIT_AT_BOL)
2604 bcopy (coding->spec.iso2022.initial_designation,
2605 coding->spec.iso2022.current_designation,
2606 sizeof coding->spec.iso2022.initial_designation);
2607 if (coding->eol_type == CODING_EOL_LF
2608 || coding->eol_type == CODING_EOL_UNDECIDED)
2609 *dst++ = ISO_CODE_LF;
2610 else if (coding->eol_type == CODING_EOL_CRLF)
2611 *dst++ = ISO_CODE_CR, *dst++ = ISO_CODE_LF;
2612 else
2613 *dst++ = ISO_CODE_CR;
2614 CODING_SPEC_ISO_BOL (coding) = 1;
2615 } 3485 }
2616 else 3486 ENCODE_ISO_CHARACTER (charset, c);
2617 { 3487 }
2618 if (coding->flags & CODING_FLAG_ISO_RESET_AT_CNTL) 3488 }
2619 ENCODE_RESET_PLANE_AND_REGISTER; 3489
2620 *dst++ = c; 3490 if (coding->mode & CODING_MODE_LAST_BLOCK
2621 } 3491 && CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL)
2622 } 3492 {
2623 else if (ASCII_BYTE_P (c)) 3493 ASSURE_DESTINATION (safe_room);
2624 ENCODE_ISO_CHARACTER (c); 3494 ENCODE_RESET_PLANE_AND_REGISTER ();
2625 else if (SINGLE_BYTE_CHAR_P (c)) 3495 }
2626 { 3496 coding->result = CODING_RESULT_SUCCESS;
2627 *dst++ = c; 3497 CODING_ISO_BOL (coding) = bol_designation;
2628 coding->errors++; 3498 coding->produced_char += produced_chars;
2629 } 3499 coding->produced = dst - coding->destination;
2630 else if (coding->flags & CODING_FLAG_ISO_SAFE 3500 return 0;
2631 && ! CODING_SAFE_CHAR_P (safe_chars, c))
2632 ENCODE_UNSAFE_CHARACTER (c);
2633 else
2634 ENCODE_ISO_CHARACTER (c);
2635
2636 coding->consumed_char++;
2637 }
2638
2639 label_end_of_loop:
2640 coding->consumed = src_base - source;
2641 coding->produced = coding->produced_char = dst - destination;
2642 } 3501 }
2643 3502
2644 3503
2645 /*** 4. SJIS and BIG5 handlers ***/ 3504 /*** 8,9. SJIS and BIG5 handlers ***/
2646 3505
2647 /* Although SJIS and BIG5 are not ISO coding systems, they are used 3506 /* Although SJIS and BIG5 are not ISO's coding system, they are used
2648 quite widely. So, for the moment, Emacs supports them in the bare 3507 quite widely. So, for the moment, Emacs supports them in the bare
2649 C code. But, in the future, they may be supported only by CCL. */ 3508 C code. But, in the future, they may be supported only by CCL. */
2650 3509
2651 /* SJIS is a coding system encoding three character sets: ASCII, right 3510 /* SJIS is a coding system encoding three character sets: ASCII, right
2652 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded 3511 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
2653 as is. A character of charset katakana-jisx0201 is encoded by 3512 as is. A character of charset katakana-jisx0201 is encoded by
2654 "position-code + 0x80". A character of charset japanese-jisx0208 3513 "position-code + 0x80". A character of charset japanese-jisx0208
2655 is encoded in 2-byte but two position-codes are divided and shifted 3514 is encoded in 2-byte but two position-codes are divided and shifted
2656 so that it fits in the range below. 3515 so that it fit in the range below.
2657 3516
2658 --- CODE RANGE of SJIS --- 3517 --- CODE RANGE of SJIS ---
2659 (character set) (range) 3518 (character set) (range)
2660 ASCII 0x00 .. 0x7F 3519 ASCII 0x00 .. 0x7F
2661 KATAKANA-JISX0201 0xA1 .. 0xDF 3520 KATAKANA-JISX0201 0xA0 .. 0xDF
2662 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF 3521 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
2663 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC 3522 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
2664 ------------------------------- 3523 -------------------------------
2665 3524
2666 */ 3525 */
2667 3526
2668 /* BIG5 is a coding system encoding two character sets: ASCII and 3527 /* BIG5 is a coding system encoding two character sets: ASCII and
2669 Big5. An ASCII character is encoded as is. Big5 is a two-byte 3528 Big5. An ASCII character is encoded as is. Big5 is a two-byte
2670 character set and is encoded in two bytes. 3529 character set and is encoded in two-byte.
2671 3530
2672 --- CODE RANGE of BIG5 --- 3531 --- CODE RANGE of BIG5 ---
2673 (character set) (range) 3532 (character set) (range)
2674 ASCII 0x00 .. 0x7F 3533 ASCII 0x00 .. 0x7F
2675 Big5 (1st byte) 0xA1 .. 0xFE 3534 Big5 (1st byte) 0xA1 .. 0xFE
2676 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE 3535 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
2677 -------------------------- 3536 --------------------------
2678 3537
2679 Since the number of characters in Big5 is larger than maximum 3538 */
2680 characters in Emacs' charset (96x96), it can't be handled as one
2681 charset. So, in Emacs, Big5 is divided into two: `charset-big5-1'
2682 and `charset-big5-2'. Both are DIMENSION2 and CHARS94. The former
2683 contains frequently used characters and the latter contains less
2684 frequently used characters. */
2685
2686 /* Macros to decode or encode a character of Big5 in BIG5. B1 and B2
2687 are the 1st and 2nd position-codes of Big5 in BIG5 coding system.
2688 C1 and C2 are the 1st and 2nd position-codes of of Emacs' internal
2689 format. CHARSET is `charset_big5_1' or `charset_big5_2'. */
2690
2691 /* Number of Big5 characters which have the same code in 1st byte. */
2692 #define BIG5_SAME_ROW (0xFF - 0xA1 + 0x7F - 0x40)
2693
2694 #define DECODE_BIG5(b1, b2, charset, c1, c2) \
2695 do { \
2696 unsigned int temp \
2697 = (b1 - 0xA1) * BIG5_SAME_ROW + b2 - (b2 < 0x7F ? 0x40 : 0x62); \
2698 if (b1 < 0xC9) \
2699 charset = charset_big5_1; \
2700 else \
2701 { \
2702 charset = charset_big5_2; \
2703 temp -= (0xC9 - 0xA1) * BIG5_SAME_ROW; \
2704 } \
2705 c1 = temp / (0xFF - 0xA1) + 0x21; \
2706 c2 = temp % (0xFF - 0xA1) + 0x21; \
2707 } while (0)
2708
2709 #define ENCODE_BIG5(charset, c1, c2, b1, b2) \
2710 do { \
2711 unsigned int temp = (c1 - 0x21) * (0xFF - 0xA1) + (c2 - 0x21); \
2712 if (charset == charset_big5_2) \
2713 temp += BIG5_SAME_ROW * (0xC9 - 0xA1); \
2714 b1 = temp / BIG5_SAME_ROW + 0xA1; \
2715 b2 = temp % BIG5_SAME_ROW; \
2716 b2 += b2 < 0x3F ? 0x40 : 0x62; \
2717 } while (0)
2718 3539
2719 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". 3540 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2720 Check if a text is encoded in SJIS. If it is, return 3541 Check if a text is encoded in SJIS. If it is, return
2721 CODING_CATEGORY_MASK_SJIS, else return 0. */ 3542 CATEGORY_MASK_SJIS, else return 0. */
2722 3543
2723 static int 3544 static int
2724 detect_coding_sjis (src, src_end, multibytep) 3545 detect_coding_sjis (coding, mask)
2725 unsigned char *src, *src_end; 3546 struct coding_system *coding;
2726 int multibytep; 3547 int *mask;
2727 { 3548 {
3549 unsigned char *src = coding->source, *src_base = src;
3550 unsigned char *src_end = coding->source + coding->src_bytes;
3551 int multibytep = coding->src_multibyte;
3552 int consumed_chars = 0;
3553 int found = 0;
2728 int c; 3554 int c;
2729 /* Dummy for ONE_MORE_BYTE. */ 3555
2730 struct coding_system dummy_coding; 3556 /* A coding system of this category is always ASCII compatible. */
2731 struct coding_system *coding = &dummy_coding; 3557 src += coding->head_ascii;
2732 3558
2733 while (1) 3559 while (1)
2734 { 3560 {
2735 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep); 3561 ONE_MORE_BYTE (c);
2736 if (c < 0x80) 3562 if (c < 0x80)
2737 continue; 3563 continue;
2738 if (c == 0x80 || c == 0xA0 || c > 0xEF) 3564 if ((c >= 0x81 && c <= 0x9F) || (c >= 0xE0 && c <= 0xEF))
2739 return 0; 3565 {
2740 if (c <= 0x9F || c >= 0xE0) 3566 ONE_MORE_BYTE (c);
2741 {
2742 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
2743 if (c < 0x40 || c == 0x7F || c > 0xFC) 3567 if (c < 0x40 || c == 0x7F || c > 0xFC)
2744 return 0; 3568 break;
2745 } 3569 found = 1;
2746 } 3570 }
2747 label_end_of_loop: 3571 else if (c >= 0xA0 && c < 0xE0)
2748 return CODING_CATEGORY_MASK_SJIS; 3572 found = 1;
3573 else
3574 break;
3575 }
3576 *mask &= ~CATEGORY_MASK_SJIS;
3577 return 0;
3578
3579 no_more_source:
3580 if (!found)
3581 return 0;
3582 *mask &= CATEGORY_MASK_SJIS;
3583 return 1;
2749 } 3584 }
2750 3585
2751 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". 3586 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2752 Check if a text is encoded in BIG5. If it is, return 3587 Check if a text is encoded in BIG5. If it is, return
2753 CODING_CATEGORY_MASK_BIG5, else return 0. */ 3588 CATEGORY_MASK_BIG5, else return 0. */
2754 3589
2755 static int 3590 static int
2756 detect_coding_big5 (src, src_end, multibytep) 3591 detect_coding_big5 (coding, mask)
2757 unsigned char *src, *src_end; 3592 struct coding_system *coding;
2758 int multibytep; 3593 int *mask;
2759 { 3594 {
3595 unsigned char *src = coding->source, *src_base = src;
3596 unsigned char *src_end = coding->source + coding->src_bytes;
3597 int multibytep = coding->src_multibyte;
3598 int consumed_chars = 0;
3599 int found = 0;
2760 int c; 3600 int c;
2761 /* Dummy for ONE_MORE_BYTE. */ 3601
2762 struct coding_system dummy_coding; 3602 /* A coding system of this category is always ASCII compatible. */
2763 struct coding_system *coding = &dummy_coding; 3603 src += coding->head_ascii;
2764 3604
2765 while (1) 3605 while (1)
2766 { 3606 {
2767 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep); 3607 ONE_MORE_BYTE (c);
2768 if (c < 0x80) 3608 if (c < 0x80)
2769 continue; 3609 continue;
2770 if (c < 0xA1 || c > 0xFE) 3610 if (c >= 0xA1)
2771 return 0; 3611 {
2772 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep); 3612 ONE_MORE_BYTE (c);
2773 if (c < 0x40 || (c > 0x7F && c < 0xA1) || c > 0xFE) 3613 if (c < 0x40 || (c >= 0x7F && c <= 0xA0))
2774 return 0; 3614 return 0;
2775 } 3615 found = 1;
2776 label_end_of_loop: 3616 }
2777 return CODING_CATEGORY_MASK_BIG5;
2778 }
2779
2780 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2781 Check if a text is encoded in UTF-8. If it is, return
2782 CODING_CATEGORY_MASK_UTF_8, else return 0. */
2783
2784 #define UTF_8_1_OCTET_P(c) ((c) < 0x80)
2785 #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
2786 #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
2787 #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
2788 #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
2789 #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
2790 #define UTF_8_6_OCTET_LEADING_P(c) (((c) & 0xFE) == 0xFC)
2791
2792 static int
2793 detect_coding_utf_8 (src, src_end, multibytep)
2794 unsigned char *src, *src_end;
2795 int multibytep;
2796 {
2797 unsigned char c;
2798 int seq_maybe_bytes;
2799 /* Dummy for ONE_MORE_BYTE. */
2800 struct coding_system dummy_coding;
2801 struct coding_system *coding = &dummy_coding;
2802
2803 while (1)
2804 {
2805 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
2806 if (UTF_8_1_OCTET_P (c))
2807 continue;
2808 else if (UTF_8_2_OCTET_LEADING_P (c))
2809 seq_maybe_bytes = 1;
2810 else if (UTF_8_3_OCTET_LEADING_P (c))
2811 seq_maybe_bytes = 2;
2812 else if (UTF_8_4_OCTET_LEADING_P (c))
2813 seq_maybe_bytes = 3;
2814 else if (UTF_8_5_OCTET_LEADING_P (c))
2815 seq_maybe_bytes = 4;
2816 else if (UTF_8_6_OCTET_LEADING_P (c))
2817 seq_maybe_bytes = 5;
2818 else 3617 else
2819 return 0; 3618 break;
2820 3619 }
2821 do 3620 *mask &= ~CATEGORY_MASK_BIG5;
2822 {
2823 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
2824 if (!UTF_8_EXTRA_OCTET_P (c))
2825 return 0;
2826 seq_maybe_bytes--;
2827 }
2828 while (seq_maybe_bytes > 0);
2829 }
2830
2831 label_end_of_loop:
2832 return CODING_CATEGORY_MASK_UTF_8;
2833 }
2834
2835 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2836 Check if a text is encoded in UTF-16 Big Endian (endian == 1) or
2837 Little Endian (otherwise). If it is, return
2838 CODING_CATEGORY_MASK_UTF_16_BE or CODING_CATEGORY_MASK_UTF_16_LE,
2839 else return 0. */
2840
2841 #define UTF_16_INVALID_P(val) \
2842 (((val) == 0xFFFE) \
2843 || ((val) == 0xFFFF))
2844
2845 #define UTF_16_HIGH_SURROGATE_P(val) \
2846 (((val) & 0xD800) == 0xD800)
2847
2848 #define UTF_16_LOW_SURROGATE_P(val) \
2849 (((val) & 0xDC00) == 0xDC00)
2850
2851 static int
2852 detect_coding_utf_16 (src, src_end, multibytep)
2853 unsigned char *src, *src_end;
2854 int multibytep;
2855 {
2856 unsigned char c1, c2;
2857 /* Dummy for TWO_MORE_BYTES. */
2858 struct coding_system dummy_coding;
2859 struct coding_system *coding = &dummy_coding;
2860
2861 ONE_MORE_BYTE_CHECK_MULTIBYTE (c1, multibytep);
2862 ONE_MORE_BYTE_CHECK_MULTIBYTE (c2, multibytep);
2863
2864 if ((c1 == 0xFF) && (c2 == 0xFE))
2865 return CODING_CATEGORY_MASK_UTF_16_LE;
2866 else if ((c1 == 0xFE) && (c2 == 0xFF))
2867 return CODING_CATEGORY_MASK_UTF_16_BE;
2868
2869 label_end_of_loop:
2870 return 0; 3621 return 0;
3622
3623 no_more_source:
3624 if (!found)
3625 return 0;
3626 *mask &= CATEGORY_MASK_BIG5;
3627 return 1;
2871 } 3628 }
2872 3629
2873 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". 3630 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
2874 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */ 3631 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
2875 3632
2876 static void 3633 static void
2877 decode_coding_sjis_big5 (coding, source, destination, 3634 decode_coding_sjis (coding)
2878 src_bytes, dst_bytes, sjis_p)
2879 struct coding_system *coding; 3635 struct coding_system *coding;
2880 unsigned char *source, *destination; 3636 {
2881 int src_bytes, dst_bytes; 3637 unsigned char *src = coding->source + coding->consumed;
2882 int sjis_p; 3638 unsigned char *src_end = coding->source + coding->src_bytes;
2883 {
2884 unsigned char *src = source;
2885 unsigned char *src_end = source + src_bytes;
2886 unsigned char *dst = destination;
2887 unsigned char *dst_end = destination + dst_bytes;
2888 /* SRC_BASE remembers the start position in source in each loop.
2889 The loop will be exited when there's not enough source code
2890 (within macro ONE_MORE_BYTE), or when there's not enough
2891 destination area to produce a character (within macro
2892 EMIT_CHAR). */
2893 unsigned char *src_base; 3639 unsigned char *src_base;
2894 Lisp_Object translation_table; 3640 int *charbuf = coding->charbuf;
2895 3641 int *charbuf_end = charbuf + coding->charbuf_size;
2896 if (NILP (Venable_character_translation)) 3642 int consumed_chars = 0, consumed_chars_base;
2897 translation_table = Qnil; 3643 int multibytep = coding->src_multibyte;
2898 else 3644 struct charset *charset_roman, *charset_kanji, *charset_kana;
2899 { 3645 Lisp_Object attrs, eol_type, charset_list, val;
2900 translation_table = coding->translation_table_for_decode; 3646
2901 if (NILP (translation_table)) 3647 CODING_GET_INFO (coding, attrs, eol_type, charset_list);
2902 translation_table = Vstandard_translation_table_for_decode; 3648
2903 } 3649 val = charset_list;
2904 3650 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
2905 coding->produced_char = 0; 3651 charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
3652 charset_kana = CHARSET_FROM_ID (XINT (XCAR (val)));
3653
2906 while (1) 3654 while (1)
2907 { 3655 {
2908 int c, charset, c1, c2; 3656 int c, c1;
2909 3657
2910 src_base = src; 3658 src_base = src;
2911 ONE_MORE_BYTE (c1); 3659 consumed_chars_base = consumed_chars;
2912 3660
2913 if (c1 < 0x80) 3661 if (charbuf >= charbuf_end)
2914 { 3662 break;
2915 charset = CHARSET_ASCII; 3663
2916 if (c1 < 0x20) 3664 ONE_MORE_BYTE (c);
3665
3666 if (c == '\r')
3667 {
3668 if (EQ (eol_type, Qdos))
2917 { 3669 {
2918 if (c1 == '\r') 3670 if (src == src_end)
2919 { 3671 goto no_more_source;
2920 if (coding->eol_type == CODING_EOL_CRLF) 3672 if (*src == '\n')
2921 { 3673 ONE_MORE_BYTE (c);
2922 ONE_MORE_BYTE (c2);
2923 if (c2 == '\n')
2924 c1 = c2;
2925 else if (coding->mode
2926 & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
2927 {
2928 coding->result = CODING_FINISH_INCONSISTENT_EOL;
2929 goto label_end_of_loop;
2930 }
2931 else
2932 /* To process C2 again, SRC is subtracted by 1. */
2933 src--;
2934 }
2935 else if (coding->eol_type == CODING_EOL_CR)
2936 c1 = '\n';
2937 }
2938 else if (c1 == '\n'
2939 && (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
2940 && (coding->eol_type == CODING_EOL_CR
2941 || coding->eol_type == CODING_EOL_CRLF))
2942 {
2943 coding->result = CODING_FINISH_INCONSISTENT_EOL;
2944 goto label_end_of_loop;
2945 }
2946 } 3674 }
3675 else if (EQ (eol_type, Qmac))
3676 c = '\n';
2947 } 3677 }
2948 else 3678 else
2949 { 3679 {
2950 if (sjis_p) 3680 struct charset *charset;
3681
3682 if (c < 0x80)
3683 charset = charset_roman;
3684 else
2951 { 3685 {
2952 if (c1 == 0x80 || c1 == 0xA0 || c1 > 0xEF) 3686 if (c >= 0xF0)
2953 goto label_invalid_code; 3687 goto invalid_code;
2954 if (c1 <= 0x9F || c1 >= 0xE0) 3688 if (c < 0xA0 || c >= 0xE0)
2955 { 3689 {
2956 /* SJIS -> JISX0208 */ 3690 /* SJIS -> JISX0208 */
2957 ONE_MORE_BYTE (c2); 3691 ONE_MORE_BYTE (c1);
2958 if (c2 < 0x40 || c2 == 0x7F || c2 > 0xFC) 3692 if (c1 < 0x40 || c1 == 0x7F || c1 > 0xFC)
2959 goto label_invalid_code; 3693 goto invalid_code;
2960 DECODE_SJIS (c1, c2, c1, c2); 3694 c = (c << 8) | c1;
2961 charset = charset_jisx0208; 3695 SJIS_TO_JIS (c);
3696 charset = charset_kanji;
2962 } 3697 }
2963 else 3698 else
2964 /* SJIS -> JISX0201-Kana */ 3699 /* SJIS -> JISX0201-Kana */
2965 charset = charset_katakana_jisx0201; 3700 charset = charset_kana;
2966 } 3701 }
3702 CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c);
3703 }
3704 *charbuf++ = c;
3705 continue;
3706
3707 invalid_code:
3708 src = src_base;
3709 consumed_chars = consumed_chars_base;
3710 ONE_MORE_BYTE (c);
3711 *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
3712 coding->errors++;
3713 }
3714
3715 no_more_source:
3716 coding->consumed_char += consumed_chars_base;
3717 coding->consumed = src_base - coding->source;
3718 coding->charbuf_used = charbuf - coding->charbuf;
3719 }
3720
3721 static void
3722 decode_coding_big5 (coding)
3723 struct coding_system *coding;
3724 {
3725 unsigned char *src = coding->source + coding->consumed;
3726 unsigned char *src_end = coding->source + coding->src_bytes;
3727 unsigned char *src_base;
3728 int *charbuf = coding->charbuf;
3729 int *charbuf_end = charbuf + coding->charbuf_size;
3730 int consumed_chars = 0, consumed_chars_base;
3731 int multibytep = coding->src_multibyte;
3732 struct charset *charset_roman, *charset_big5;
3733 Lisp_Object attrs, eol_type, charset_list, val;
3734
3735 CODING_GET_INFO (coding, attrs, eol_type, charset_list);
3736 val = charset_list;
3737 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
3738 charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val)));
3739
3740 while (1)
3741 {
3742 int c, c1;
3743
3744 src_base = src;
3745 consumed_chars_base = consumed_chars;
3746
3747 if (charbuf >= charbuf_end)
3748 break;
3749
3750 ONE_MORE_BYTE (c);
3751
3752 if (c == '\r')
3753 {
3754 if (EQ (eol_type, Qdos))
3755 {
3756 if (src == src_end)
3757 goto no_more_source;
3758 if (*src == '\n')
3759 ONE_MORE_BYTE (c);
3760 }
3761 else if (EQ (eol_type, Qmac))
3762 c = '\n';
3763 }
3764 else
3765 {
3766 struct charset *charset;
3767 if (c < 0x80)
3768 charset = charset_roman;
2967 else 3769 else
2968 { 3770 {
2969 /* BIG5 -> Big5 */ 3771 /* BIG5 -> Big5 */
2970 if (c1 < 0xA0 || c1 > 0xFE) 3772 if (c < 0xA1 || c > 0xFE)
2971 goto label_invalid_code; 3773 goto invalid_code;
2972 ONE_MORE_BYTE (c2); 3774 ONE_MORE_BYTE (c1);
2973 if (c2 < 0x40 || (c2 > 0x7E && c2 < 0xA1) || c2 > 0xFE) 3775 if (c1 < 0x40 || (c1 > 0x7E && c1 < 0xA1) || c1 > 0xFE)
2974 goto label_invalid_code; 3776 goto invalid_code;
2975 DECODE_BIG5 (c1, c2, charset, c1, c2); 3777 c = c << 8 | c1;
3778 charset = charset_big5;
2976 } 3779 }
2977 } 3780 CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c);
2978 3781 }
2979 c = DECODE_ISO_CHARACTER (charset, c1, c2); 3782
2980 EMIT_CHAR (c); 3783 *charbuf++ = c;
2981 continue; 3784 continue;
2982 3785
2983 label_invalid_code: 3786 invalid_code:
3787 src = src_base;
3788 consumed_chars = consumed_chars_base;
3789 ONE_MORE_BYTE (c);
3790 *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
2984 coding->errors++; 3791 coding->errors++;
2985 src = src_base; 3792 }
2986 c = *src++; 3793
2987 EMIT_CHAR (c); 3794 no_more_source:
2988 } 3795 coding->consumed_char += consumed_chars_base;
2989 3796 coding->consumed = src_base - coding->source;
2990 label_end_of_loop: 3797 coding->charbuf_used = charbuf - coding->charbuf;
2991 coding->consumed = coding->consumed_char = src_base - source;
2992 coding->produced = dst - destination;
2993 return;
2994 } 3798 }
2995 3799
2996 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". 3800 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
2997 This function can encode charsets `ascii', `katakana-jisx0201', 3801 This function can encode charsets `ascii', `katakana-jisx0201',
2998 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We 3802 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
2999 are sure that all these charsets are registered as official charset 3803 are sure that all these charsets are registered as official charset
3000 (i.e. do not have extended leading-codes). Characters of other 3804 (i.e. do not have extended leading-codes). Characters of other
3001 charsets are produced without any encoding. If SJIS_P is 1, encode 3805 charsets are produced without any encoding. If SJIS_P is 1, encode
3002 SJIS text, else encode BIG5 text. */ 3806 SJIS text, else encode BIG5 text. */
3003 3807
3004 static void 3808 static int
3005 encode_coding_sjis_big5 (coding, source, destination, 3809 encode_coding_sjis (coding)
3006 src_bytes, dst_bytes, sjis_p)
3007 struct coding_system *coding; 3810 struct coding_system *coding;
3008 unsigned char *source, *destination; 3811 {
3009 int src_bytes, dst_bytes; 3812 int multibytep = coding->dst_multibyte;
3010 int sjis_p; 3813 int *charbuf = coding->charbuf;
3011 { 3814 int *charbuf_end = charbuf + coding->charbuf_used;
3012 unsigned char *src = source; 3815 unsigned char *dst = coding->destination + coding->produced;
3013 unsigned char *src_end = source + src_bytes; 3816 unsigned char *dst_end = coding->destination + coding->dst_bytes;
3014 unsigned char *dst = destination; 3817 int safe_room = 4;
3015 unsigned char *dst_end = destination + dst_bytes; 3818 int produced_chars = 0;
3016 /* SRC_BASE remembers the start position in source in each loop. 3819 Lisp_Object attrs, eol_type, charset_list, val;
3017 The loop will be exited when there's not enough source text to 3820 int ascii_compatible;
3018 analyze multi-byte codes (within macro ONE_MORE_CHAR), or when 3821 struct charset *charset_roman, *charset_kanji, *charset_kana;
3019 there's not enough destination area to produce encoded codes 3822 int c;
3020 (within macro EMIT_BYTES). */ 3823
3021 unsigned char *src_base; 3824 CODING_GET_INFO (coding, attrs, eol_type, charset_list);
3022 Lisp_Object translation_table; 3825 val = charset_list;
3023 3826 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
3024 if (NILP (Venable_character_translation)) 3827 charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
3025 translation_table = Qnil; 3828 charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val)));
3026 else 3829
3027 { 3830 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
3028 translation_table = coding->translation_table_for_encode; 3831
3029 if (NILP (translation_table)) 3832 while (charbuf < charbuf_end)
3030 translation_table = Vstandard_translation_table_for_encode; 3833 {
3031 } 3834 ASSURE_DESTINATION (safe_room);
3032 3835 c = *charbuf++;
3033 while (1)
3034 {
3035 int c, charset, c1, c2;
3036
3037 src_base = src;
3038 ONE_MORE_CHAR (c);
3039
3040 /* Now encode the character C. */ 3836 /* Now encode the character C. */
3041 if (SINGLE_BYTE_CHAR_P (c)) 3837 if (ASCII_CHAR_P (c) && ascii_compatible)
3042 { 3838 EMIT_ONE_ASCII_BYTE (c);
3043 switch (c) 3839 else
3840 {
3841 unsigned code;
3842 struct charset *charset = char_charset (c, charset_list, &code);
3843
3844 if (!charset)
3044 { 3845 {
3045 case '\r': 3846 c = coding->default_char;
3046 if (!coding->mode & CODING_MODE_SELECTIVE_DISPLAY) 3847 charset = char_charset (c, charset_list, &code);
3047 {
3048 EMIT_ONE_BYTE (c);
3049 break;
3050 }
3051 c = '\n';
3052 case '\n':
3053 if (coding->eol_type == CODING_EOL_CRLF)
3054 {
3055 EMIT_TWO_BYTES ('\r', c);
3056 break;
3057 }
3058 else if (coding->eol_type == CODING_EOL_CR)
3059 c = '\r';
3060 default:
3061 EMIT_ONE_BYTE (c);
3062 } 3848 }
3063 } 3849 if (code == CHARSET_INVALID_CODE (charset))
3850 abort ();
3851 if (charset == charset_kanji)
3852 {
3853 int c1, c2;
3854 JIS_TO_SJIS (code);
3855 c1 = code >> 8, c2 = code & 0xFF;
3856 EMIT_TWO_BYTES (c1, c2);
3857 }
3858 else if (charset == charset_kana)
3859 EMIT_ONE_BYTE (code | 0x80);
3860 else
3861 EMIT_ONE_ASCII_BYTE (code & 0x7F);
3862 }
3863 }
3864 coding->result = CODING_RESULT_SUCCESS;
3865 coding->produced_char += produced_chars;
3866 coding->produced = dst - coding->destination;
3867 return 0;
3868 }
3869
3870 static int
3871 encode_coding_big5 (coding)
3872 struct coding_system *coding;
3873 {
3874 int multibytep = coding->dst_multibyte;
3875 int *charbuf = coding->charbuf;
3876 int *charbuf_end = charbuf + coding->charbuf_used;
3877 unsigned char *dst = coding->destination + coding->produced;
3878 unsigned char *dst_end = coding->destination + coding->dst_bytes;
3879 int safe_room = 4;
3880 int produced_chars = 0;
3881 Lisp_Object attrs, eol_type, charset_list, val;
3882 int ascii_compatible;
3883 struct charset *charset_roman, *charset_big5;
3884 int c;
3885
3886 CODING_GET_INFO (coding, attrs, eol_type, charset_list);
3887 val = charset_list;
3888 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
3889 charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val)));
3890 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
3891
3892 while (charbuf < charbuf_end)
3893 {
3894 ASSURE_DESTINATION (safe_room);
3895 c = *charbuf++;
3896 /* Now encode the character C. */
3897 if (ASCII_CHAR_P (c) && ascii_compatible)
3898 EMIT_ONE_ASCII_BYTE (c);
3064 else 3899 else
3065 { 3900 {
3066 SPLIT_CHAR (c, charset, c1, c2); 3901 unsigned code;
3067 if (sjis_p) 3902 struct charset *charset = char_charset (c, charset_list, &code);
3903
3904 if (! charset)
3068 { 3905 {
3069 if (charset == charset_jisx0208 3906 c = coding->default_char;
3070 || charset == charset_jisx0208_1978) 3907 charset = char_charset (c, charset_list, &code);
3071 { 3908 }
3072 ENCODE_SJIS (c1, c2, c1, c2); 3909 if (code == CHARSET_INVALID_CODE (charset))
3073 EMIT_TWO_BYTES (c1, c2); 3910 abort ();
3074 } 3911 if (charset == charset_big5)
3075 else if (charset == charset_katakana_jisx0201) 3912 {
3076 EMIT_ONE_BYTE (c1 | 0x80); 3913 int c1, c2;
3077 else if (charset == charset_latin_jisx0201) 3914
3078 EMIT_ONE_BYTE (c1); 3915 c1 = code >> 8, c2 = code & 0xFF;
3079 else 3916 EMIT_TWO_BYTES (c1, c2);
3080 /* There's no way other than producing the internal
3081 codes as is. */
3082 EMIT_BYTES (src_base, src);
3083 } 3917 }
3084 else 3918 else
3085 { 3919 EMIT_ONE_ASCII_BYTE (code & 0x7F);
3086 if (charset == charset_big5_1 || charset == charset_big5_2) 3920 }
3087 { 3921 }
3088 ENCODE_BIG5 (charset, c1, c2, c1, c2); 3922 coding->result = CODING_RESULT_SUCCESS;
3089 EMIT_TWO_BYTES (c1, c2); 3923 coding->produced_char += produced_chars;
3090 } 3924 coding->produced = dst - coding->destination;
3091 else 3925 return 0;
3092 /* There's no way other than producing the internal
3093 codes as is. */
3094 EMIT_BYTES (src_base, src);
3095 }
3096 }
3097 coding->consumed_char++;
3098 }
3099
3100 label_end_of_loop:
3101 coding->consumed = src_base - source;
3102 coding->produced = coding->produced_char = dst - destination;
3103 } 3926 }
3104 3927
3105 3928
3106 /*** 5. CCL handlers ***/ 3929 /*** 10. CCL handlers ***/
3107 3930
3108 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". 3931 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
3109 Check if a text is encoded in a coding system of which 3932 Check if a text is encoded in a coding system of which
3110 encoder/decoder are written in CCL program. If it is, return 3933 encoder/decoder are written in CCL program. If it is, return
3111 CODING_CATEGORY_MASK_CCL, else return 0. */ 3934 CATEGORY_MASK_CCL, else return 0. */
3112 3935
3113 static int 3936 static int
3114 detect_coding_ccl (src, src_end, multibytep) 3937 detect_coding_ccl (coding, mask)
3115 unsigned char *src, *src_end; 3938 struct coding_system *coding;
3116 int multibytep; 3939 int *mask;
3117 { 3940 {
3118 unsigned char *valid; 3941 unsigned char *src = coding->source, *src_base = src;
3942 unsigned char *src_end = coding->source + coding->src_bytes;
3943 int multibytep = coding->src_multibyte;
3944 int consumed_chars = 0;
3945 int found = 0;
3946 unsigned char *valids = CODING_CCL_VALIDS (coding);
3947 int head_ascii = coding->head_ascii;
3948 Lisp_Object attrs;
3949
3950 coding = &coding_categories[coding_category_ccl];
3951 attrs = CODING_ID_ATTRS (coding->id);
3952 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
3953 src += head_ascii;
3954
3955 while (1)
3956 {
3957 int c;
3958 ONE_MORE_BYTE (c);
3959 if (! valids[c])
3960 break;
3961 if (!found && valids[c] > 1)
3962 found = 1;
3963 }
3964 *mask &= ~CATEGORY_MASK_CCL;
3965 return 0;
3966
3967 no_more_source:
3968 if (!found)
3969 return 0;
3970 *mask &= CATEGORY_MASK_CCL;
3971 return 1;
3972 }
3973
3974 static void
3975 decode_coding_ccl (coding)
3976 struct coding_system *coding;
3977 {
3978 unsigned char *src = coding->source + coding->consumed;
3979 unsigned char *src_end = coding->source + coding->src_bytes;
3980 int *charbuf = coding->charbuf;
3981 int *charbuf_end = charbuf + coding->charbuf_size;
3982 int consumed_chars = 0;
3983 int multibytep = coding->src_multibyte;
3984 struct ccl_program ccl;
3985 int source_charbuf[1024];
3986 int source_byteidx[1024];
3987
3988 setup_ccl_program (&ccl, CODING_CCL_DECODER (coding));
3989
3990 while (src < src_end)
3991 {
3992 unsigned char *p = src;
3993 int *source, *source_end;
3994 int i = 0;
3995
3996 if (multibytep)
3997 while (i < 1024 && p < src_end)
3998 {
3999 source_byteidx[i] = p - src;
4000 source_charbuf[i++] = STRING_CHAR_ADVANCE (p);
4001 }
4002 else
4003 while (i < 1024 && p < src_end)
4004 source_charbuf[i++] = *p++;
4005
4006 if (p == src_end && coding->mode & CODING_MODE_LAST_BLOCK)
4007 ccl.last_block = 1;
4008
4009 source = source_charbuf;
4010 source_end = source + i;
4011 while (source < source_end)
4012 {
4013 ccl_driver (&ccl, source, charbuf,
4014 source_end - source, charbuf_end - charbuf);
4015 source += ccl.consumed;
4016 charbuf += ccl.produced;
4017 if (ccl.status != CCL_STAT_SUSPEND_BY_DST)
4018 break;
4019 }
4020 if (source < source_end)
4021 src += source_byteidx[source - source_charbuf];
4022 else
4023 src = p;
4024 consumed_chars += source - source_charbuf;
4025
4026 if (ccl.status != CCL_STAT_SUSPEND_BY_SRC
4027 && ccl.status != CODING_RESULT_INSUFFICIENT_SRC)
4028 break;
4029 }
4030
4031 switch (ccl.status)
4032 {
4033 case CCL_STAT_SUSPEND_BY_SRC:
4034 coding->result = CODING_RESULT_INSUFFICIENT_SRC;
4035 break;
4036 case CCL_STAT_SUSPEND_BY_DST:
4037 break;
4038 case CCL_STAT_QUIT:
4039 case CCL_STAT_INVALID_CMD:
4040 coding->result = CODING_RESULT_INTERRUPT;
4041 break;
4042 default:
4043 coding->result = CODING_RESULT_SUCCESS;
4044 break;
4045 }
4046 coding->consumed_char += consumed_chars;
4047 coding->consumed = src - coding->source;
4048 coding->charbuf_used = charbuf - coding->charbuf;
4049 }
4050
4051 static int
4052 encode_coding_ccl (coding)
4053 struct coding_system *coding;
4054 {
4055 struct ccl_program ccl;
4056 int multibytep = coding->dst_multibyte;
4057 int *charbuf = coding->charbuf;
4058 int *charbuf_end = charbuf + coding->charbuf_used;
4059 unsigned char *dst = coding->destination + coding->produced;
4060 unsigned char *dst_end = coding->destination + coding->dst_bytes;
4061 unsigned char *adjusted_dst_end = dst_end - 1;
4062 int destination_charbuf[1024];
4063 int i, produced_chars = 0;
4064
4065 setup_ccl_program (&ccl, CODING_CCL_ENCODER (coding));
4066
4067 ccl.last_block = coding->mode & CODING_MODE_LAST_BLOCK;
4068 ccl.dst_multibyte = coding->dst_multibyte;
4069
4070 while (charbuf < charbuf_end && dst < adjusted_dst_end)
4071 {
4072 int dst_bytes = dst_end - dst;
4073 if (dst_bytes > 1024)
4074 dst_bytes = 1024;
4075
4076 ccl_driver (&ccl, charbuf, destination_charbuf,
4077 charbuf_end - charbuf, dst_bytes);
4078 charbuf += ccl.consumed;
4079 if (multibytep)
4080 for (i = 0; i < ccl.produced; i++)
4081 EMIT_ONE_BYTE (destination_charbuf[i] & 0xFF);
4082 else
4083 {
4084 for (i = 0; i < ccl.produced; i++)
4085 *dst++ = destination_charbuf[i] & 0xFF;
4086 produced_chars += ccl.produced;
4087 }
4088 }
4089
4090 switch (ccl.status)
4091 {
4092 case CCL_STAT_SUSPEND_BY_SRC:
4093 coding->result = CODING_RESULT_INSUFFICIENT_SRC;
4094 break;
4095 case CCL_STAT_SUSPEND_BY_DST:
4096 coding->result = CODING_RESULT_INSUFFICIENT_DST;
4097 break;
4098 case CCL_STAT_QUIT:
4099 case CCL_STAT_INVALID_CMD:
4100 coding->result = CODING_RESULT_INTERRUPT;
4101 break;
4102 default:
4103 coding->result = CODING_RESULT_SUCCESS;
4104 break;
4105 }
4106
4107 coding->produced_char += produced_chars;
4108 coding->produced = dst - coding->destination;
4109 return 0;
4110 }
4111
4112
4113
4114 /*** 10, 11. no-conversion handlers ***/
4115
4116 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
4117
4118 static void
4119 decode_coding_raw_text (coding)
4120 struct coding_system *coding;
4121 {
4122 coding->chars_at_source = 1;
4123 coding->consumed_char = coding->src_chars;
4124 coding->consumed = coding->src_bytes;
4125 coding->result = CODING_RESULT_SUCCESS;
4126 }
4127
4128 static int
4129 encode_coding_raw_text (coding)
4130 struct coding_system *coding;
4131 {
4132 int multibytep = coding->dst_multibyte;
4133 int *charbuf = coding->charbuf;
4134 int *charbuf_end = coding->charbuf + coding->charbuf_used;
4135 unsigned char *dst = coding->destination + coding->produced;
4136 unsigned char *dst_end = coding->destination + coding->dst_bytes;
4137 int produced_chars = 0;
3119 int c; 4138 int c;
3120 /* Dummy for ONE_MORE_BYTE. */ 4139
3121 struct coding_system dummy_coding; 4140 if (multibytep)
3122 struct coding_system *coding = &dummy_coding; 4141 {
3123 4142 int safe_room = MAX_MULTIBYTE_LENGTH * 2;
3124 /* No coding system is assigned to coding-category-ccl. */ 4143
3125 if (!coding_system_table[CODING_CATEGORY_IDX_CCL]) 4144 if (coding->src_multibyte)
3126 return 0; 4145 while (charbuf < charbuf_end)
3127 4146 {
3128 valid = coding_system_table[CODING_CATEGORY_IDX_CCL]->spec.ccl.valid_codes; 4147 ASSURE_DESTINATION (safe_room);
4148 c = *charbuf++;
4149 if (ASCII_CHAR_P (c))
4150 EMIT_ONE_ASCII_BYTE (c);
4151 else if (CHAR_BYTE8_P (c))
4152 {
4153 c = CHAR_TO_BYTE8 (c);
4154 EMIT_ONE_BYTE (c);
4155 }
4156 else
4157 {
4158 unsigned char str[MAX_MULTIBYTE_LENGTH], *p0 = str, *p1 = str;
4159
4160 CHAR_STRING_ADVANCE (c, p1);
4161 while (p0 < p1)
4162 EMIT_ONE_BYTE (*p0);
4163 }
4164 }
4165 else
4166 while (charbuf < charbuf_end)
4167 {
4168 ASSURE_DESTINATION (safe_room);
4169 c = *charbuf++;
4170 EMIT_ONE_BYTE (c);
4171 }
4172 }
4173 else
4174 {
4175 if (coding->src_multibyte)
4176 {
4177 int safe_room = MAX_MULTIBYTE_LENGTH;
4178
4179 while (charbuf < charbuf_end)
4180 {
4181 ASSURE_DESTINATION (safe_room);
4182 c = *charbuf++;
4183 if (ASCII_CHAR_P (c))
4184 *dst++ = c;
4185 else if (CHAR_BYTE8_P (c))
4186 *dst++ = CHAR_TO_BYTE8 (c);
4187 else
4188 CHAR_STRING_ADVANCE (c, dst);
4189 produced_chars++;
4190 }
4191 }
4192 else
4193 {
4194 ASSURE_DESTINATION (charbuf_end - charbuf);
4195 while (charbuf < charbuf_end && dst < dst_end)
4196 *dst++ = *charbuf++;
4197 produced_chars = dst - (coding->destination + coding->dst_bytes);
4198 }
4199 }
4200 coding->result = CODING_RESULT_SUCCESS;
4201 coding->produced_char += produced_chars;
4202 coding->produced = dst - coding->destination;
4203 return 0;
4204 }
4205
4206 static int
4207 detect_coding_charset (coding, mask)
4208 struct coding_system *coding;
4209 int *mask;
4210 {
4211 unsigned char *src = coding->source, *src_base = src;
4212 unsigned char *src_end = coding->source + coding->src_bytes;
4213 int multibytep = coding->src_multibyte;
4214 int consumed_chars = 0;
4215 Lisp_Object attrs, valids;
4216
4217 coding = &coding_categories[coding_category_charset];
4218 attrs = CODING_ID_ATTRS (coding->id);
4219 valids = AREF (attrs, coding_attr_charset_valids);
4220
4221 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
4222 src += coding->head_ascii;
4223
3129 while (1) 4224 while (1)
3130 { 4225 {
3131 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep); 4226 int c;
3132 if (! valid[c]) 4227
3133 return 0; 4228 ONE_MORE_BYTE (c);
3134 } 4229 if (NILP (AREF (valids, c)))
3135 label_end_of_loop: 4230 break;
3136 return CODING_CATEGORY_MASK_CCL; 4231 }
3137 } 4232 *mask &= ~CATEGORY_MASK_CHARSET;
3138 4233 return 0;
3139 4234
3140 /*** 6. End-of-line handlers ***/ 4235 no_more_source:
3141 4236 *mask &= CATEGORY_MASK_CHARSET;
3142 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ 4237 return 1;
4238 }
3143 4239
3144 static void 4240 static void
3145 decode_eol (coding, source, destination, src_bytes, dst_bytes) 4241 decode_coding_charset (coding)
3146 struct coding_system *coding; 4242 struct coding_system *coding;
3147 unsigned char *source, *destination; 4243 {
3148 int src_bytes, dst_bytes; 4244 unsigned char *src = coding->source + coding->consumed;
3149 { 4245 unsigned char *src_end = coding->source + coding->src_bytes;
3150 unsigned char *src = source;
3151 unsigned char *dst = destination;
3152 unsigned char *src_end = src + src_bytes;
3153 unsigned char *dst_end = dst + dst_bytes;
3154 Lisp_Object translation_table;
3155 /* SRC_BASE remembers the start position in source in each loop.
3156 The loop will be exited when there's not enough source code
3157 (within macro ONE_MORE_BYTE), or when there's not enough
3158 destination area to produce a character (within macro
3159 EMIT_CHAR). */
3160 unsigned char *src_base; 4246 unsigned char *src_base;
4247 int *charbuf = coding->charbuf;
4248 int *charbuf_end = charbuf + coding->charbuf_size;
4249 int consumed_chars = 0, consumed_chars_base;
4250 int multibytep = coding->src_multibyte;
4251 struct charset *charset;
4252 Lisp_Object attrs, eol_type, charset_list;
4253
4254 CODING_GET_INFO (coding, attrs, eol_type, charset_list);
4255 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
4256
4257 while (1)
4258 {
4259 int c, c1;
4260
4261 src_base = src;
4262 consumed_chars_base = consumed_chars;
4263
4264 if (charbuf >= charbuf_end)
4265 break;
4266
4267 ONE_MORE_BYTE (c1);
4268 if (c == '\r')
4269 {
4270 if (EQ (eol_type, Qdos))
4271 {
4272 if (src == src_end)
4273 goto no_more_source;
4274 if (*src == '\n')
4275 ONE_MORE_BYTE (c);
4276 }
4277 else if (EQ (eol_type, Qmac))
4278 c = '\n';
4279 }
4280 else
4281 {
4282 CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c1, c);
4283 if (c < 0)
4284 goto invalid_code;
4285 }
4286 *charbuf++ = c;
4287 continue;
4288
4289 invalid_code:
4290 src = src_base;
4291 consumed_chars = consumed_chars_base;
4292 ONE_MORE_BYTE (c);
4293 *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
4294 coding->errors++;
4295 }
4296
4297 no_more_source:
4298 coding->consumed_char += consumed_chars_base;
4299 coding->consumed = src_base - coding->source;
4300 coding->charbuf_used = charbuf - coding->charbuf;
4301 }
4302
4303 static int
4304 encode_coding_charset (coding)
4305 struct coding_system *coding;
4306 {
4307 int multibytep = coding->dst_multibyte;
4308 int *charbuf = coding->charbuf;
4309 int *charbuf_end = charbuf + coding->charbuf_used;
4310 unsigned char *dst = coding->destination + coding->produced;
4311 unsigned char *dst_end = coding->destination + coding->dst_bytes;
4312 int safe_room = MAX_MULTIBYTE_LENGTH;
4313 int produced_chars = 0;
4314 struct charset *charset;
4315 Lisp_Object attrs, eol_type, charset_list;
4316 int ascii_compatible;
3161 int c; 4317 int c;
3162 4318
3163 translation_table = Qnil; 4319 CODING_GET_INFO (coding, attrs, eol_type, charset_list);
3164 switch (coding->eol_type) 4320 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
3165 { 4321 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
3166 case CODING_EOL_CRLF: 4322
3167 while (1) 4323 while (charbuf < charbuf_end)
3168 { 4324 {
3169 src_base = src; 4325 unsigned code;
3170 ONE_MORE_BYTE (c); 4326
3171 if (c == '\r') 4327 ASSURE_DESTINATION (safe_room);
3172 { 4328 c = *charbuf++;
3173 ONE_MORE_BYTE (c); 4329 if (ascii_compatible && ASCII_CHAR_P (c))
3174 if (c != '\n') 4330 EMIT_ONE_ASCII_BYTE (c);
3175 { 4331 else if ((code = ENCODE_CHAR (charset, c))
3176 if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL) 4332 != CHARSET_INVALID_CODE (charset))
3177 { 4333 EMIT_ONE_BYTE (code);
3178 coding->result = CODING_FINISH_INCONSISTENT_EOL;
3179 goto label_end_of_loop;
3180 }
3181 src--;
3182 c = '\r';
3183 }
3184 }
3185 else if (c == '\n'
3186 && (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL))
3187 {
3188 coding->result = CODING_FINISH_INCONSISTENT_EOL;
3189 goto label_end_of_loop;
3190 }
3191 EMIT_CHAR (c);
3192 }
3193 break;
3194
3195 case CODING_EOL_CR:
3196 while (1)
3197 {
3198 src_base = src;
3199 ONE_MORE_BYTE (c);
3200 if (c == '\n')
3201 {
3202 if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
3203 {
3204 coding->result = CODING_FINISH_INCONSISTENT_EOL;
3205 goto label_end_of_loop;
3206 }
3207 }
3208 else if (c == '\r')
3209 c = '\n';
3210 EMIT_CHAR (c);
3211 }
3212 break;
3213
3214 default: /* no need for EOL handling */
3215 while (1)
3216 {
3217 src_base = src;
3218 ONE_MORE_BYTE (c);
3219 EMIT_CHAR (c);
3220 }
3221 }
3222
3223 label_end_of_loop:
3224 coding->consumed = coding->consumed_char = src_base - source;
3225 coding->produced = dst - destination;
3226 return;
3227 }
3228
3229 /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". Encode
3230 format of end-of-line according to `coding->eol_type'. It also
3231 convert multibyte form 8-bit characters to unibyte if
3232 CODING->src_multibyte is nonzero. If `coding->mode &
3233 CODING_MODE_SELECTIVE_DISPLAY' is nonzero, code '\r' in source text
3234 also means end-of-line. */
3235
3236 static void
3237 encode_eol (coding, source, destination, src_bytes, dst_bytes)
3238 struct coding_system *coding;
3239 unsigned char *source, *destination;
3240 int src_bytes, dst_bytes;
3241 {
3242 unsigned char *src = source;
3243 unsigned char *dst = destination;
3244 unsigned char *src_end = src + src_bytes;
3245 unsigned char *dst_end = dst + dst_bytes;
3246 Lisp_Object translation_table;
3247 /* SRC_BASE remembers the start position in source in each loop.
3248 The loop will be exited when there's not enough source text to
3249 analyze multi-byte codes (within macro ONE_MORE_CHAR), or when
3250 there's not enough destination area to produce encoded codes
3251 (within macro EMIT_BYTES). */
3252 unsigned char *src_base;
3253 int c;
3254 int selective_display = coding->mode & CODING_MODE_SELECTIVE_DISPLAY;
3255
3256 translation_table = Qnil;
3257 if (coding->src_multibyte
3258 && *(src_end - 1) == LEADING_CODE_8_BIT_CONTROL)
3259 {
3260 src_end--;
3261 src_bytes--;
3262 coding->result = CODING_FINISH_INSUFFICIENT_SRC;
3263 }
3264
3265 if (coding->eol_type == CODING_EOL_CRLF)
3266 {
3267 while (src < src_end)
3268 {
3269 src_base = src;
3270 c = *src++;
3271 if (c >= 0x20)
3272 EMIT_ONE_BYTE (c);
3273 else if (c == '\n' || (c == '\r' && selective_display))
3274 EMIT_TWO_BYTES ('\r', '\n');
3275 else
3276 EMIT_ONE_BYTE (c);
3277 }
3278 src_base = src;
3279 label_end_of_loop:
3280 ;
3281 }
3282 else
3283 {
3284 if (!dst_bytes || src_bytes <= dst_bytes)
3285 {
3286 safe_bcopy (src, dst, src_bytes);
3287 src_base = src_end;
3288 dst += src_bytes;
3289 }
3290 else 4334 else
3291 { 4335 EMIT_ONE_BYTE (coding->default_char);
3292 if (coding->src_multibyte 4336 }
3293 && *(src + dst_bytes - 1) == LEADING_CODE_8_BIT_CONTROL) 4337
3294 dst_bytes--; 4338 coding->result = CODING_RESULT_SUCCESS;
3295 safe_bcopy (src, dst, dst_bytes); 4339 coding->produced_char += produced_chars;
3296 src_base = src + dst_bytes; 4340 coding->produced = dst - coding->destination;
3297 dst = destination + dst_bytes; 4341 return 0;
3298 coding->result = CODING_FINISH_INSUFFICIENT_DST;
3299 }
3300 if (coding->eol_type == CODING_EOL_CR)
3301 {
3302 for (src = destination; src < dst; src++)
3303 if (*src == '\n') *src = '\r';
3304 }
3305 else if (selective_display)
3306 {
3307 for (src = destination; src < dst; src++)
3308 if (*src == '\r') *src = '\n';
3309 }
3310 }
3311 if (coding->src_multibyte)
3312 dst = destination + str_as_unibyte (destination, dst - destination);
3313
3314 coding->consumed = src_base - source;
3315 coding->produced = dst - destination;
3316 coding->produced_char = coding->produced;
3317 } 4342 }
3318 4343
3319 4344
3320 /*** 7. C library functions ***/ 4345 /*** 7. C library functions ***/
3321 4346
3322 /* In Emacs Lisp, a coding system is represented by a Lisp symbol which 4347 /* In Emacs Lisp, coding system is represented by a Lisp symbol which
3323 has a property `coding-system'. The value of this property is a 4348 has a property `coding-system'. The value of this property is a
3324 vector of length 5 (called the coding-vector). Among elements of 4349 vector of length 5 (called as coding-vector). Among elements of
3325 this vector, the first (element[0]) and the fifth (element[4]) 4350 this vector, the first (element[0]) and the fifth (element[4])
3326 carry important information for decoding/encoding. Before 4351 carry important information for decoding/encoding. Before
3327 decoding/encoding, this information should be set in fields of a 4352 decoding/encoding, this information should be set in fields of a
3328 structure of type `coding_system'. 4353 structure of type `coding_system'.
3329 4354
3330 The value of the property `coding-system' can be a symbol of another 4355 A value of property `coding-system' can be a symbol of another
3331 subsidiary coding-system. In that case, Emacs gets coding-vector 4356 subsidiary coding-system. In that case, Emacs gets coding-vector
3332 from that symbol. 4357 from that symbol.
3333 4358
3334 `element[0]' contains information to be set in `coding->type'. The 4359 `element[0]' contains information to be set in `coding->type'. The
3335 value and its meaning is as follows: 4360 value and its meaning is as follows:
3336 4361
3337 0 -- coding_type_emacs_mule 4362 0 -- coding_type_emacs_mule
3338 1 -- coding_type_sjis 4363 1 -- coding_type_sjis
3339 2 -- coding_type_iso2022 4364 2 -- coding_type_iso_2022
3340 3 -- coding_type_big5 4365 3 -- coding_type_big5
3341 4 -- coding_type_ccl encoder/decoder written in CCL 4366 4 -- coding_type_ccl encoder/decoder written in CCL
3342 nil -- coding_type_no_conversion 4367 nil -- coding_type_no_conversion
3343 t -- coding_type_undecided (automatic conversion on decoding, 4368 t -- coding_type_undecided (automatic conversion on decoding,
3344 no-conversion on encoding) 4369 no-conversion on encoding)
3345 4370
3346 `element[4]' contains information to be set in `coding->flags' and 4371 `element[4]' contains information to be set in `coding->flags' and
3347 `coding->spec'. The meaning varies by `coding->type'. 4372 `coding->spec'. The meaning varies by `coding->type'.
3348 4373
3349 If `coding->type' is `coding_type_iso2022', element[4] is a vector 4374 If `coding->type' is `coding_type_iso_2022', element[4] is a vector
3350 of length 32 (of which the first 13 sub-elements are used now). 4375 of length 32 (of which the first 13 sub-elements are used now).
3351 Meanings of these sub-elements are: 4376 Meanings of these sub-elements are:
3352 4377
3353 sub-element[N] where N is 0 through 3: to be set in `coding->spec.iso2022' 4378 sub-element[N] where N is 0 through 3: to be set in `coding->spec.iso_2022'
3354 If the value is an integer of valid charset, the charset is 4379 If the value is an integer of valid charset, the charset is
3355 assumed to be designated to graphic register N initially. 4380 assumed to be designated to graphic register N initially.
3356 4381
3357 If the value is minus, it is a minus value of charset which 4382 If the value is minus, it is a minus value of charset which
3358 reserves graphic register N, which means that the charset is 4383 reserves graphic register N, which means that the charset is
3359 not designated initially but should be designated to graphic 4384 not designated initially but should be designated to graphic
3360 register N just before encoding a character in that charset. 4385 register N just before encoding a character in that charset.
3361 4386
3362 If the value is nil, graphic register N is never used on 4387 If the value is nil, graphic register N is never used on
3363 encoding. 4388 encoding.
3364 4389
3365 sub-element[N] where N is 4 through 11: to be set in `coding->flags' 4390 sub-element[N] where N is 4 through 11: to be set in `coding->flags'
3366 Each value takes t or nil. See the section ISO2022 of 4391 Each value takes t or nil. See the section ISO2022 of
3367 `coding.h' for more information. 4392 `coding.h' for more information.
3368 4393
3369 If `coding->type' is `coding_type_big5', element[4] is t to denote 4394 If `coding->type' is `coding_type_big5', element[4] is t to denote
3370 BIG5-ETen or nil to denote BIG5-HKU. 4395 BIG5-ETen or nil to denote BIG5-HKU.
3371 4396
3372 If `coding->type' takes the other value, element[4] is ignored. 4397 If `coding->type' takes the other value, element[4] is ignored.
3373 4398
3374 Emacs Lisp's coding systems also carry information about format of 4399 Emacs Lisp's coding system also carries information about format of
3375 end-of-line in a value of property `eol-type'. If the value is 4400 end-of-line in a value of property `eol-type'. If the value is
3376 integer, 0 means CODING_EOL_LF, 1 means CODING_EOL_CRLF, and 2 4401 integer, 0 means eol_lf, 1 means eol_crlf, and 2 means eol_cr. If
3377 means CODING_EOL_CR. If it is not integer, it should be a vector 4402 it is not integer, it should be a vector of subsidiary coding
3378 of subsidiary coding systems of which property `eol-type' has one 4403 systems of which property `eol-type' has one of above values.
3379 of the above values.
3380 4404
3381 */ 4405 */
3382 4406
3383 /* Extract information for decoding/encoding from CODING_SYSTEM_SYMBOL 4407 /* Setup coding context CODING from information about CODING_SYSTEM.
3384 and set it in CODING. If CODING_SYSTEM_SYMBOL is invalid, CODING 4408 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
3385 is setup so that no conversion is necessary and return -1, else 4409 CODING_SYSTEM is invalid, signal an error. */
3386 return 0. */ 4410
3387 4411 void
3388 int
3389 setup_coding_system (coding_system, coding) 4412 setup_coding_system (coding_system, coding)
3390 Lisp_Object coding_system; 4413 Lisp_Object coding_system;
3391 struct coding_system *coding; 4414 struct coding_system *coding;
3392 { 4415 {
3393 Lisp_Object coding_spec, coding_type, eol_type, plist; 4416 int id;
4417 Lisp_Object attrs;
4418 Lisp_Object eol_type;
4419 Lisp_Object coding_type;
3394 Lisp_Object val; 4420 Lisp_Object val;
3395 4421
3396 /* At first, zero clear all members. */
3397 bzero (coding, sizeof (struct coding_system));
3398
3399 /* Initialize some fields required for all kinds of coding systems. */
3400 coding->symbol = coding_system;
3401 coding->heading_ascii = -1;
3402 coding->post_read_conversion = coding->pre_write_conversion = Qnil;
3403 coding->composing = COMPOSITION_DISABLED;
3404 coding->cmp_data = NULL;
3405
3406 if (NILP (coding_system)) 4422 if (NILP (coding_system))
3407 goto label_invalid_coding_system; 4423 coding_system = Qno_conversion;
3408 4424
3409 coding_spec = Fget (coding_system, Qcoding_system); 4425 CHECK_CODING_SYSTEM_GET_ID (coding_system, coding->id);
3410 4426
3411 if (!VECTORP (coding_spec) 4427 attrs = CODING_ID_ATTRS (coding->id);
3412 || XVECTOR (coding_spec)->size != 5 4428 eol_type = CODING_ID_EOL_TYPE (coding->id);
3413 || !CONSP (XVECTOR (coding_spec)->contents[3])) 4429
3414 goto label_invalid_coding_system; 4430 coding->mode = 0;
3415 4431 coding->head_ascii = -1;
3416 eol_type = inhibit_eol_conversion ? Qnil : Fget (coding_system, Qeol_type); 4432 coding->common_flags
4433 = (VECTORP (eol_type) ? CODING_REQUIRE_DETECTION_MASK : 0);
4434
4435 val = CODING_ATTR_SAFE_CHARSETS (attrs);
4436 coding->max_charset_id = XSTRING (val)->size - 1;
4437 coding->safe_charsets = (char *) XSTRING (val)->data;
4438 coding->default_char = XINT (CODING_ATTR_DEFAULT_CHAR (attrs));
4439
4440 coding_type = CODING_ATTR_TYPE (attrs);
4441 if (EQ (coding_type, Qundecided))
4442 {
4443 coding->detector = NULL;
4444 coding->decoder = decode_coding_raw_text;
4445 coding->encoder = encode_coding_raw_text;
4446 coding->common_flags |= CODING_REQUIRE_DETECTION_MASK;
4447 }
4448 else if (EQ (coding_type, Qiso_2022))
4449 {
4450 int i;
4451 int flags = XINT (AREF (attrs, coding_attr_iso_flags));
4452
4453 /* Invoke graphic register 0 to plane 0. */
4454 CODING_ISO_INVOCATION (coding, 0) = 0;
4455 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
4456 CODING_ISO_INVOCATION (coding, 1)
4457 = (flags & CODING_ISO_FLAG_SEVEN_BITS ? -1 : 1);
4458 /* Setup the initial status of designation. */
4459 for (i = 0; i < 4; i++)
4460 CODING_ISO_DESIGNATION (coding, i) = CODING_ISO_INITIAL (coding, i);
4461 /* Not single shifting initially. */
4462 CODING_ISO_SINGLE_SHIFTING (coding) = 0;
4463 /* Beginning of buffer should also be regarded as bol. */
4464 CODING_ISO_BOL (coding) = 1;
4465 coding->detector = detect_coding_iso_2022;
4466 coding->decoder = decode_coding_iso_2022;
4467 coding->encoder = encode_coding_iso_2022;
4468 if (flags & CODING_ISO_FLAG_SAFE)
4469 coding->mode |= CODING_MODE_SAFE_ENCODING;
4470 coding->common_flags
4471 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK
4472 | CODING_REQUIRE_FLUSHING_MASK);
4473 if (flags & CODING_ISO_FLAG_COMPOSITION)
4474 coding->common_flags |= CODING_ANNOTATE_COMPOSITION_MASK;
4475 if (flags & CODING_ISO_FLAG_FULL_SUPPORT)
4476 {
4477 setup_iso_safe_charsets (attrs);
4478 val = CODING_ATTR_SAFE_CHARSETS (attrs);
4479 coding->max_charset_id = XSTRING (val)->size - 1;
4480 coding->safe_charsets = (char *) XSTRING (val)->data;
4481 }
4482 CODING_ISO_FLAGS (coding) = flags;
4483 }
4484 else if (EQ (coding_type, Qcharset))
4485 {
4486 coding->detector = detect_coding_charset;
4487 coding->decoder = decode_coding_charset;
4488 coding->encoder = encode_coding_charset;
4489 coding->common_flags
4490 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
4491 }
4492 else if (EQ (coding_type, Qutf_8))
4493 {
4494 coding->detector = detect_coding_utf_8;
4495 coding->decoder = decode_coding_utf_8;
4496 coding->encoder = encode_coding_utf_8;
4497 coding->common_flags
4498 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
4499 }
4500 else if (EQ (coding_type, Qutf_16))
4501 {
4502 val = AREF (attrs, coding_attr_utf_16_bom);
4503 CODING_UTF_16_BOM (coding) = (CONSP (val) ? utf_16_detect_bom
4504 : EQ (val, Qt) ? utf_16_with_bom
4505 : utf_16_without_bom);
4506 val = AREF (attrs, coding_attr_utf_16_endian);
4507 CODING_UTF_16_ENDIAN (coding) = (NILP (val) ? utf_16_big_endian
4508 : utf_16_little_endian);
4509 coding->detector = detect_coding_utf_16;
4510 coding->decoder = decode_coding_utf_16;
4511 coding->encoder = encode_coding_utf_16;
4512 coding->common_flags
4513 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
4514 }
4515 else if (EQ (coding_type, Qccl))
4516 {
4517 coding->detector = detect_coding_ccl;
4518 coding->decoder = decode_coding_ccl;
4519 coding->encoder = encode_coding_ccl;
4520 coding->common_flags
4521 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK
4522 | CODING_REQUIRE_FLUSHING_MASK);
4523 }
4524 else if (EQ (coding_type, Qemacs_mule))
4525 {
4526 coding->detector = detect_coding_emacs_mule;
4527 coding->decoder = decode_coding_emacs_mule;
4528 coding->encoder = encode_coding_emacs_mule;
4529 coding->common_flags
4530 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
4531 if (! NILP (AREF (attrs, coding_attr_emacs_mule_full))
4532 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs), Vemacs_mule_charset_list))
4533 {
4534 Lisp_Object tail, safe_charsets;
4535 int max_charset_id = 0;
4536
4537 for (tail = Vemacs_mule_charset_list; CONSP (tail);
4538 tail = XCDR (tail))
4539 if (max_charset_id < XFASTINT (XCAR (tail)))
4540 max_charset_id = XFASTINT (XCAR (tail));
4541 safe_charsets = Fmake_string (make_number (max_charset_id + 1),
4542 make_number (255));
4543 for (tail = Vemacs_mule_charset_list; CONSP (tail);
4544 tail = XCDR (tail))
4545 XSTRING (safe_charsets)->data[XFASTINT (XCAR (tail))] = 0;
4546 coding->max_charset_id = max_charset_id;
4547 coding->safe_charsets = (char *) XSTRING (safe_charsets)->data;
4548 }
4549 }
4550 else if (EQ (coding_type, Qshift_jis))
4551 {
4552 coding->detector = detect_coding_sjis;
4553 coding->decoder = decode_coding_sjis;
4554 coding->encoder = encode_coding_sjis;
4555 coding->common_flags
4556 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
4557 }
4558 else if (EQ (coding_type, Qbig5))
4559 {
4560 coding->detector = detect_coding_big5;
4561 coding->decoder = decode_coding_big5;
4562 coding->encoder = encode_coding_big5;
4563 coding->common_flags
4564 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
4565 }
4566 else /* EQ (coding_type, Qraw_text) */
4567 {
4568 coding->detector = NULL;
4569 coding->decoder = decode_coding_raw_text;
4570 coding->encoder = encode_coding_raw_text;
4571 coding->common_flags |= CODING_FOR_UNIBYTE_MASK;
4572 }
4573
4574 return;
4575 }
4576
4577 /* Return raw-text or one of its subsidiaries that has the same
4578 eol_type as CODING-SYSTEM. */
4579
4580 Lisp_Object
4581 raw_text_coding_system (coding_system)
4582 Lisp_Object coding_system;
4583 {
4584 Lisp_Object spec, attrs, coding_type;
4585 Lisp_Object eol_type, raw_text_eol_type;
4586
4587 spec = CODING_SYSTEM_SPEC (coding_system);
4588 attrs = AREF (spec, 0);
4589
4590 if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text))
4591 return coding_system;
4592
4593 eol_type = AREF (spec, 2);
3417 if (VECTORP (eol_type)) 4594 if (VECTORP (eol_type))
3418 { 4595 return Qraw_text;
3419 coding->eol_type = CODING_EOL_UNDECIDED; 4596 spec = CODING_SYSTEM_SPEC (Qraw_text);
3420 coding->common_flags = CODING_REQUIRE_DETECTION_MASK; 4597 raw_text_eol_type = AREF (spec, 2);
3421 } 4598 return (EQ (eol_type, Qunix) ? AREF (raw_text_eol_type, 0)
3422 else if (XFASTINT (eol_type) == 1) 4599 : EQ (eol_type, Qdos) ? AREF (raw_text_eol_type, 1)
3423 { 4600 : AREF (raw_text_eol_type, 2));
3424 coding->eol_type = CODING_EOL_CRLF; 4601 }
3425 coding->common_flags 4602
3426 = CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK; 4603
3427 } 4604 /* If CODING_SYSTEM doesn't specify end-of-line format but PARENT
3428 else if (XFASTINT (eol_type) == 2) 4605 does, return one of the subsidiary that has the same eol-spec as
3429 { 4606 PARENT. Otherwise, return CODING_SYSTEM. */
3430 coding->eol_type = CODING_EOL_CR; 4607
3431 coding->common_flags 4608 Lisp_Object
3432 = CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK; 4609 coding_inherit_eol_type (coding_system, parent)
3433 } 4610 {
3434 else 4611 Lisp_Object spec, attrs, eol_type;
3435 coding->eol_type = CODING_EOL_LF; 4612
3436 4613 spec = CODING_SYSTEM_SPEC (coding_system);
3437 coding_type = XVECTOR (coding_spec)->contents[0]; 4614 attrs = AREF (spec, 0);
3438 /* Try short cut. */ 4615 eol_type = AREF (spec, 2);
3439 if (SYMBOLP (coding_type)) 4616 if (VECTORP (eol_type))
3440 { 4617 {
3441 if (EQ (coding_type, Qt)) 4618 Lisp_Object parent_spec;
3442 { 4619 Lisp_Object parent_attrs;
3443 coding->type = coding_type_undecided; 4620 Lisp_Object parent_eol_type;
3444 coding->common_flags |= CODING_REQUIRE_DETECTION_MASK; 4621
3445 } 4622 parent_spec
3446 else 4623 = CODING_SYSTEM_SPEC (buffer_defaults.buffer_file_coding_system);
3447 coding->type = coding_type_no_conversion; 4624 parent_eol_type = AREF (parent_spec, 2);
3448 /* Initialize this member. Any thing other than 4625 if (EQ (parent_eol_type, Qunix))
3449 CODING_CATEGORY_IDX_UTF_16_BE and 4626 coding_system = AREF (eol_type, 0);
3450 CODING_CATEGORY_IDX_UTF_16_LE are ok because they have 4627 else if (EQ (parent_eol_type, Qdos))
3451 special treatment in detect_eol. */ 4628 coding_system = AREF (eol_type, 1);
3452 coding->category_idx = CODING_CATEGORY_IDX_EMACS_MULE; 4629 else if (EQ (parent_eol_type, Qmac))
3453 4630 coding_system = AREF (eol_type, 2);
3454 return 0; 4631 }
3455 } 4632 return coding_system;
3456
3457 /* Get values of coding system properties:
3458 `post-read-conversion', `pre-write-conversion',
3459 `translation-table-for-decode', `translation-table-for-encode'. */
3460 plist = XVECTOR (coding_spec)->contents[3];
3461 /* Pre & post conversion functions should be disabled if
3462 inhibit_eol_conversion is nonzero. This is the case that a code
3463 conversion function is called while those functions are running. */
3464 if (! inhibit_pre_post_conversion)
3465 {
3466 coding->post_read_conversion = Fplist_get (plist, Qpost_read_conversion);
3467 coding->pre_write_conversion = Fplist_get (plist, Qpre_write_conversion);
3468 }
3469 val = Fplist_get (plist, Qtranslation_table_for_decode);
3470 if (SYMBOLP (val))
3471 val = Fget (val, Qtranslation_table_for_decode);
3472 coding->translation_table_for_decode = CHAR_TABLE_P (val) ? val : Qnil;
3473 val = Fplist_get (plist, Qtranslation_table_for_encode);
3474 if (SYMBOLP (val))
3475 val = Fget (val, Qtranslation_table_for_encode);
3476 coding->translation_table_for_encode = CHAR_TABLE_P (val) ? val : Qnil;
3477 val = Fplist_get (plist, Qcoding_category);
3478 if (!NILP (val))
3479 {
3480 val = Fget (val, Qcoding_category_index);
3481 if (INTEGERP (val))
3482 coding->category_idx = XINT (val);
3483 else
3484 goto label_invalid_coding_system;
3485 }
3486 else
3487 goto label_invalid_coding_system;
3488
3489 /* If the coding system has non-nil `composition' property, enable
3490 composition handling. */
3491 val = Fplist_get (plist, Qcomposition);
3492 if (!NILP (val))
3493 coding->composing = COMPOSITION_NO;
3494
3495 switch (XFASTINT (coding_type))
3496 {
3497 case 0:
3498 coding->type = coding_type_emacs_mule;
3499 coding->common_flags
3500 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
3501 coding->composing = COMPOSITION_NO;
3502 if (!NILP (coding->post_read_conversion))
3503 coding->common_flags |= CODING_REQUIRE_DECODING_MASK;
3504 if (!NILP (coding->pre_write_conversion))
3505 coding->common_flags |= CODING_REQUIRE_ENCODING_MASK;
3506 break;
3507
3508 case 1:
3509 coding->type = coding_type_sjis;
3510 coding->common_flags
3511 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
3512 break;
3513
3514 case 2:
3515 coding->type = coding_type_iso2022;
3516 coding->common_flags
3517 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
3518 {
3519 Lisp_Object val, temp;
3520 Lisp_Object *flags;
3521 int i, charset, reg_bits = 0;
3522
3523 val = XVECTOR (coding_spec)->contents[4];
3524
3525 if (!VECTORP (val) || XVECTOR (val)->size != 32)
3526 goto label_invalid_coding_system;
3527
3528 flags = XVECTOR (val)->contents;
3529 coding->flags
3530 = ((NILP (flags[4]) ? 0 : CODING_FLAG_ISO_SHORT_FORM)
3531 | (NILP (flags[5]) ? 0 : CODING_FLAG_ISO_RESET_AT_EOL)
3532 | (NILP (flags[6]) ? 0 : CODING_FLAG_ISO_RESET_AT_CNTL)
3533 | (NILP (flags[7]) ? 0 : CODING_FLAG_ISO_SEVEN_BITS)
3534 | (NILP (flags[8]) ? 0 : CODING_FLAG_ISO_LOCKING_SHIFT)
3535 | (NILP (flags[9]) ? 0 : CODING_FLAG_ISO_SINGLE_SHIFT)
3536 | (NILP (flags[10]) ? 0 : CODING_FLAG_ISO_USE_ROMAN)
3537 | (NILP (flags[11]) ? 0 : CODING_FLAG_ISO_USE_OLDJIS)
3538 | (NILP (flags[12]) ? 0 : CODING_FLAG_ISO_NO_DIRECTION)
3539 | (NILP (flags[13]) ? 0 : CODING_FLAG_ISO_INIT_AT_BOL)
3540 | (NILP (flags[14]) ? 0 : CODING_FLAG_ISO_DESIGNATE_AT_BOL)
3541 | (NILP (flags[15]) ? 0 : CODING_FLAG_ISO_SAFE)
3542 | (NILP (flags[16]) ? 0 : CODING_FLAG_ISO_LATIN_EXTRA)
3543 );
3544
3545 /* Invoke graphic register 0 to plane 0. */
3546 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0;
3547 /* Invoke graphic register 1 to plane 1 if we can use full 8-bit. */
3548 CODING_SPEC_ISO_INVOCATION (coding, 1)
3549 = (coding->flags & CODING_FLAG_ISO_SEVEN_BITS ? -1 : 1);
3550 /* Not single shifting at first. */
3551 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0;
3552 /* Beginning of buffer should also be regarded as bol. */
3553 CODING_SPEC_ISO_BOL (coding) = 1;
3554
3555 for (charset = 0; charset <= MAX_CHARSET; charset++)
3556 CODING_SPEC_ISO_REVISION_NUMBER (coding, charset) = 255;
3557 val = Vcharset_revision_alist;
3558 while (CONSP (val))
3559 {
3560 charset = get_charset_id (Fcar_safe (XCAR (val)));
3561 if (charset >= 0
3562 && (temp = Fcdr_safe (XCAR (val)), INTEGERP (temp))
3563 && (i = XINT (temp), (i >= 0 && (i + '@') < 128)))
3564 CODING_SPEC_ISO_REVISION_NUMBER (coding, charset) = i;
3565 val = XCDR (val);
3566 }
3567
3568 /* Checks FLAGS[REG] (REG = 0, 1, 2 3) and decide designations.
3569 FLAGS[REG] can be one of below:
3570 integer CHARSET: CHARSET occupies register I,
3571 t: designate nothing to REG initially, but can be used
3572 by any charsets,
3573 list of integer, nil, or t: designate the first
3574 element (if integer) to REG initially, the remaining
3575 elements (if integer) is designated to REG on request,
3576 if an element is t, REG can be used by any charsets,
3577 nil: REG is never used. */
3578 for (charset = 0; charset <= MAX_CHARSET; charset++)
3579 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3580 = CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION;
3581 for (i = 0; i < 4; i++)
3582 {
3583 if ((INTEGERP (flags[i])
3584 && (charset = XINT (flags[i]), CHARSET_VALID_P (charset)))
3585 || (charset = get_charset_id (flags[i])) >= 0)
3586 {
3587 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = charset;
3588 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) = i;
3589 }
3590 else if (EQ (flags[i], Qt))
3591 {
3592 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = -1;
3593 reg_bits |= 1 << i;
3594 coding->flags |= CODING_FLAG_ISO_DESIGNATION;
3595 }
3596 else if (CONSP (flags[i]))
3597 {
3598 Lisp_Object tail;
3599 tail = flags[i];
3600
3601 coding->flags |= CODING_FLAG_ISO_DESIGNATION;
3602 if ((INTEGERP (XCAR (tail))
3603 && (charset = XINT (XCAR (tail)),
3604 CHARSET_VALID_P (charset)))
3605 || (charset = get_charset_id (XCAR (tail))) >= 0)
3606 {
3607 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = charset;
3608 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) =i;
3609 }
3610 else
3611 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = -1;
3612 tail = XCDR (tail);
3613 while (CONSP (tail))
3614 {
3615 if ((INTEGERP (XCAR (tail))
3616 && (charset = XINT (XCAR (tail)),
3617 CHARSET_VALID_P (charset)))
3618 || (charset = get_charset_id (XCAR (tail))) >= 0)
3619 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3620 = i;
3621 else if (EQ (XCAR (tail), Qt))
3622 reg_bits |= 1 << i;
3623 tail = XCDR (tail);
3624 }
3625 }
3626 else
3627 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = -1;
3628
3629 CODING_SPEC_ISO_DESIGNATION (coding, i)
3630 = CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i);
3631 }
3632
3633 if (reg_bits && ! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT))
3634 {
3635 /* REG 1 can be used only by locking shift in 7-bit env. */
3636 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS)
3637 reg_bits &= ~2;
3638 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT))
3639 /* Without any shifting, only REG 0 and 1 can be used. */
3640 reg_bits &= 3;
3641 }
3642
3643 if (reg_bits)
3644 for (charset = 0; charset <= MAX_CHARSET; charset++)
3645 {
3646 if (CHARSET_DEFINED_P (charset)
3647 && (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3648 == CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION))
3649 {
3650 /* There exist some default graphic registers to be
3651 used by CHARSET. */
3652
3653 /* We had better avoid designating a charset of
3654 CHARS96 to REG 0 as far as possible. */
3655 if (CHARSET_CHARS (charset) == 96)
3656 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3657 = (reg_bits & 2
3658 ? 1 : (reg_bits & 4 ? 2 : (reg_bits & 8 ? 3 : 0)));
3659 else
3660 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3661 = (reg_bits & 1
3662 ? 0 : (reg_bits & 2 ? 1 : (reg_bits & 4 ? 2 : 3)));
3663 }
3664 }
3665 }
3666 coding->common_flags |= CODING_REQUIRE_FLUSHING_MASK;
3667 coding->spec.iso2022.last_invalid_designation_register = -1;
3668 break;
3669
3670 case 3:
3671 coding->type = coding_type_big5;
3672 coding->common_flags
3673 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
3674 coding->flags
3675 = (NILP (XVECTOR (coding_spec)->contents[4])
3676 ? CODING_FLAG_BIG5_HKU
3677 : CODING_FLAG_BIG5_ETEN);
3678 break;
3679
3680 case 4:
3681 coding->type = coding_type_ccl;
3682 coding->common_flags
3683 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
3684 {
3685 val = XVECTOR (coding_spec)->contents[4];
3686 if (! CONSP (val)
3687 || setup_ccl_program (&(coding->spec.ccl.decoder),
3688 XCAR (val)) < 0
3689 || setup_ccl_program (&(coding->spec.ccl.encoder),
3690 XCDR (val)) < 0)
3691 goto label_invalid_coding_system;
3692
3693 bzero (coding->spec.ccl.valid_codes, 256);
3694 val = Fplist_get (plist, Qvalid_codes);
3695 if (CONSP (val))
3696 {
3697 Lisp_Object this;
3698
3699 for (; CONSP (val); val = XCDR (val))
3700 {
3701 this = XCAR (val);
3702 if (INTEGERP (this)
3703 && XINT (this) >= 0 && XINT (this) < 256)
3704 coding->spec.ccl.valid_codes[XINT (this)] = 1;
3705 else if (CONSP (this)
3706 && INTEGERP (XCAR (this))
3707 && INTEGERP (XCDR (this)))
3708 {
3709 int start = XINT (XCAR (this));
3710 int end = XINT (XCDR (this));
3711
3712 if (start >= 0 && start <= end && end < 256)
3713 while (start <= end)
3714 coding->spec.ccl.valid_codes[start++] = 1;
3715 }
3716 }
3717 }
3718 }
3719 coding->common_flags |= CODING_REQUIRE_FLUSHING_MASK;
3720 coding->spec.ccl.cr_carryover = 0;
3721 coding->spec.ccl.eight_bit_carryover[0] = 0;
3722 break;
3723
3724 case 5:
3725 coding->type = coding_type_raw_text;
3726 break;
3727
3728 default:
3729 goto label_invalid_coding_system;
3730 }
3731 return 0;
3732
3733 label_invalid_coding_system:
3734 coding->type = coding_type_no_conversion;
3735 coding->category_idx = CODING_CATEGORY_IDX_BINARY;
3736 coding->common_flags = 0;
3737 coding->eol_type = CODING_EOL_LF;
3738 coding->pre_write_conversion = coding->post_read_conversion = Qnil;
3739 return -1;
3740 }
3741
3742 /* Free memory blocks allocated for storing composition information. */
3743
3744 void
3745 coding_free_composition_data (coding)
3746 struct coding_system *coding;
3747 {
3748 struct composition_data *cmp_data = coding->cmp_data, *next;
3749
3750 if (!cmp_data)
3751 return;
3752 /* Memory blocks are chained. At first, rewind to the first, then,
3753 free blocks one by one. */
3754 while (cmp_data->prev)
3755 cmp_data = cmp_data->prev;
3756 while (cmp_data)
3757 {
3758 next = cmp_data->next;
3759 xfree (cmp_data);
3760 cmp_data = next;
3761 }
3762 coding->cmp_data = NULL;
3763 }
3764
3765 /* Set `char_offset' member of all memory blocks pointed by
3766 coding->cmp_data to POS. */
3767
3768 void
3769 coding_adjust_composition_offset (coding, pos)
3770 struct coding_system *coding;
3771 int pos;
3772 {
3773 struct composition_data *cmp_data;
3774
3775 for (cmp_data = coding->cmp_data; cmp_data; cmp_data = cmp_data->next)
3776 cmp_data->char_offset = pos;
3777 }
3778
3779 /* Setup raw-text or one of its subsidiaries in the structure
3780 coding_system CODING according to the already setup value eol_type
3781 in CODING. CODING should be setup for some coding system in
3782 advance. */
3783
3784 void
3785 setup_raw_text_coding_system (coding)
3786 struct coding_system *coding;
3787 {
3788 if (coding->type != coding_type_raw_text)
3789 {
3790 coding->symbol = Qraw_text;
3791 coding->type = coding_type_raw_text;
3792 if (coding->eol_type != CODING_EOL_UNDECIDED)
3793 {
3794 Lisp_Object subsidiaries;
3795 subsidiaries = Fget (Qraw_text, Qeol_type);
3796
3797 if (VECTORP (subsidiaries)
3798 && XVECTOR (subsidiaries)->size == 3)
3799 coding->symbol
3800 = XVECTOR (subsidiaries)->contents[coding->eol_type];
3801 }
3802 setup_coding_system (coding->symbol, coding);
3803 }
3804 return;
3805 } 4633 }
3806 4634
3807 /* Emacs has a mechanism to automatically detect a coding system if it 4635 /* Emacs has a mechanism to automatically detect a coding system if it
3808 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But, 4636 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
3809 it's impossible to distinguish some coding systems accurately 4637 it's impossible to distinguish some coding systems accurately
3852 symbol) `japanese-iso-8bit' by default. 4680 symbol) `japanese-iso-8bit' by default.
3853 4681
3854 o coding-category-iso-7-else 4682 o coding-category-iso-7-else
3855 4683
3856 The category for a coding system which has the same code range 4684 The category for a coding system which has the same code range
3857 as ISO2022 of 7-bit environment but uses locking shift or 4685 as ISO2022 of 7-bit environemnt but uses locking shift or
3858 single shift functions. Assigned the coding-system (Lisp 4686 single shift functions. Assigned the coding-system (Lisp
3859 symbol) `iso-2022-7bit-lock' by default. 4687 symbol) `iso-2022-7bit-lock' by default.
3860 4688
3861 o coding-category-iso-8-else 4689 o coding-category-iso-8-else
3862 4690
3863 The category for a coding system which has the same code range 4691 The category for a coding system which has the same code range
3864 as ISO2022 of 8-bit environment but uses locking shift or 4692 as ISO2022 of 8-bit environemnt but uses locking shift or
3865 single shift functions. Assigned the coding-system (Lisp 4693 single shift functions. Assigned the coding-system (Lisp
3866 symbol) `iso-2022-8bit-ss2' by default. 4694 symbol) `iso-2022-8bit-ss2' by default.
3867 4695
3868 o coding-category-big5 4696 o coding-category-big5
3869 4697
3902 The category for a coding system not categorized in any of the 4730 The category for a coding system not categorized in any of the
3903 above. Assigned the coding-system (Lisp symbol) 4731 above. Assigned the coding-system (Lisp symbol)
3904 `no-conversion' by default. 4732 `no-conversion' by default.
3905 4733
3906 Each of them is a Lisp symbol and the value is an actual 4734 Each of them is a Lisp symbol and the value is an actual
3907 `coding-system' (this is also a Lisp symbol) assigned by a user. 4735 `coding-system's (this is also a Lisp symbol) assigned by a user.
3908 What Emacs does actually is to detect a category of coding system. 4736 What Emacs does actually is to detect a category of coding system.
3909 Then, it uses a `coding-system' assigned to it. If Emacs can't 4737 Then, it uses a `coding-system' assigned to it. If Emacs can't
3910 decide a single possible category, it selects a category of the 4738 decide only one possible category, it selects a category of the
3911 highest priority. Priorities of categories are also specified by a 4739 highest priority. Priorities of categories are also specified by a
3912 user in a Lisp variable `coding-category-list'. 4740 user in a Lisp variable `coding-category-list'.
3913 4741
3914 */ 4742 */
3915 4743
3916 static 4744 #define EOL_SEEN_NONE 0
3917 int ascii_skip_code[256]; 4745 #define EOL_SEEN_LF 1
3918 4746 #define EOL_SEEN_CR 2
3919 /* Detect how a text of length SRC_BYTES pointed by SOURCE is encoded. 4747 #define EOL_SEEN_CRLF 4
3920 If it detects possible coding systems, return an integer in which 4748
3921 appropriate flag bits are set. Flag bits are defined by macros 4749 /* Detect how end-of-line of a text of length CODING->src_bytes
3922 CODING_CATEGORY_MASK_XXX in `coding.h'. If PRIORITIES is non-NULL, 4750 pointed by CODING->source is encoded. Return one of
3923 it should point the table `coding_priorities'. In that case, only 4751 EOL_SEEN_XXX. */
3924 the flag bit for a coding system of the highest priority is set in 4752
3925 the returned value. If MULTIBYTEP is nonzero, 8-bit codes of the 4753 #define MAX_EOL_CHECK_COUNT 3
3926 range 0x80..0x9F are in multibyte form.
3927
3928 How many ASCII characters are at the head is returned as *SKIP. */
3929 4754
3930 static int 4755 static int
3931 detect_coding_mask (source, src_bytes, priorities, skip, multibytep) 4756 detect_eol (coding, source, src_bytes)
4757 struct coding_system *coding;
3932 unsigned char *source; 4758 unsigned char *source;
3933 int src_bytes, *priorities, *skip; 4759 EMACS_INT src_bytes;
3934 int multibytep; 4760 {
3935 { 4761 Lisp_Object attrs, coding_type;
3936 register unsigned char c;
3937 unsigned char *src = source, *src_end = source + src_bytes;
3938 unsigned int mask, utf16_examined_p, iso2022_examined_p;
3939 int i;
3940
3941 /* At first, skip all ASCII characters and control characters except
3942 for three ISO2022 specific control characters. */
3943 ascii_skip_code[ISO_CODE_SO] = 0;
3944 ascii_skip_code[ISO_CODE_SI] = 0;
3945 ascii_skip_code[ISO_CODE_ESC] = 0;
3946
3947 label_loop_detect_coding:
3948 while (src < src_end && ascii_skip_code[*src]) src++;
3949 *skip = src - source;
3950
3951 if (src >= src_end)
3952 /* We found nothing other than ASCII. There's nothing to do. */
3953 return 0;
3954
3955 c = *src;
3956 /* The text seems to be encoded in some multilingual coding system.
3957 Now, try to find in which coding system the text is encoded. */
3958 if (c < 0x80)
3959 {
3960 /* i.e. (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) */
3961 /* C is an ISO2022 specific control code of C0. */
3962 mask = detect_coding_iso2022 (src, src_end, multibytep);
3963 if (mask == 0)
3964 {
3965 /* No valid ISO2022 code follows C. Try again. */
3966 src++;
3967 if (c == ISO_CODE_ESC)
3968 ascii_skip_code[ISO_CODE_ESC] = 1;
3969 else
3970 ascii_skip_code[ISO_CODE_SO] = ascii_skip_code[ISO_CODE_SI] = 1;
3971 goto label_loop_detect_coding;
3972 }
3973 if (priorities)
3974 {
3975 for (i = 0; i < CODING_CATEGORY_IDX_MAX; i++)
3976 {
3977 if (mask & priorities[i])
3978 return priorities[i];
3979 }
3980 return CODING_CATEGORY_MASK_RAW_TEXT;
3981 }
3982 }
3983 else
3984 {
3985 int try;
3986
3987 if (multibytep && c == LEADING_CODE_8_BIT_CONTROL)
3988 c = src[1] - 0x20;
3989
3990 if (c < 0xA0)
3991 {
3992 /* C is the first byte of SJIS character code,
3993 or a leading-code of Emacs' internal format (emacs-mule),
3994 or the first byte of UTF-16. */
3995 try = (CODING_CATEGORY_MASK_SJIS
3996 | CODING_CATEGORY_MASK_EMACS_MULE
3997 | CODING_CATEGORY_MASK_UTF_16_BE
3998 | CODING_CATEGORY_MASK_UTF_16_LE);
3999
4000 /* Or, if C is a special latin extra code,
4001 or is an ISO2022 specific control code of C1 (SS2 or SS3),
4002 or is an ISO2022 control-sequence-introducer (CSI),
4003 we should also consider the possibility of ISO2022 codings. */
4004 if ((VECTORP (Vlatin_extra_code_table)
4005 && !NILP (XVECTOR (Vlatin_extra_code_table)->contents[c]))
4006 || (c == ISO_CODE_SS2 || c == ISO_CODE_SS3)
4007 || (c == ISO_CODE_CSI
4008 && (src < src_end
4009 && (*src == ']'
4010 || ((*src == '0' || *src == '1' || *src == '2')
4011 && src + 1 < src_end
4012 && src[1] == ']')))))
4013 try |= (CODING_CATEGORY_MASK_ISO_8_ELSE
4014 | CODING_CATEGORY_MASK_ISO_8BIT);
4015 }
4016 else
4017 /* C is a character of ISO2022 in graphic plane right,
4018 or a SJIS's 1-byte character code (i.e. JISX0201),
4019 or the first byte of BIG5's 2-byte code,
4020 or the first byte of UTF-8/16. */
4021 try = (CODING_CATEGORY_MASK_ISO_8_ELSE
4022 | CODING_CATEGORY_MASK_ISO_8BIT
4023 | CODING_CATEGORY_MASK_SJIS
4024 | CODING_CATEGORY_MASK_BIG5
4025 | CODING_CATEGORY_MASK_UTF_8
4026 | CODING_CATEGORY_MASK_UTF_16_BE
4027 | CODING_CATEGORY_MASK_UTF_16_LE);
4028
4029 /* Or, we may have to consider the possibility of CCL. */
4030 if (coding_system_table[CODING_CATEGORY_IDX_CCL]
4031 && (coding_system_table[CODING_CATEGORY_IDX_CCL]
4032 ->spec.ccl.valid_codes)[c])
4033 try |= CODING_CATEGORY_MASK_CCL;
4034
4035 mask = 0;
4036 utf16_examined_p = iso2022_examined_p = 0;
4037 if (priorities)
4038 {
4039 for (i = 0; i < CODING_CATEGORY_IDX_MAX; i++)
4040 {
4041 if (!iso2022_examined_p
4042 && (priorities[i] & try & CODING_CATEGORY_MASK_ISO))
4043 {
4044 mask |= detect_coding_iso2022 (src, src_end, multibytep);
4045 iso2022_examined_p = 1;
4046 }
4047 else if (priorities[i] & try & CODING_CATEGORY_MASK_SJIS)
4048 mask |= detect_coding_sjis (src, src_end, multibytep);
4049 else if (priorities[i] & try & CODING_CATEGORY_MASK_UTF_8)
4050 mask |= detect_coding_utf_8 (src, src_end, multibytep);
4051 else if (!utf16_examined_p
4052 && (priorities[i] & try &
4053 CODING_CATEGORY_MASK_UTF_16_BE_LE))
4054 {
4055 mask |= detect_coding_utf_16 (src, src_end, multibytep);
4056 utf16_examined_p = 1;
4057 }
4058 else if (priorities[i] & try & CODING_CATEGORY_MASK_BIG5)
4059 mask |= detect_coding_big5 (src, src_end, multibytep);
4060 else if (priorities[i] & try & CODING_CATEGORY_MASK_EMACS_MULE)
4061 mask |= detect_coding_emacs_mule (src, src_end, multibytep);
4062 else if (priorities[i] & try & CODING_CATEGORY_MASK_CCL)
4063 mask |= detect_coding_ccl (src, src_end, multibytep);
4064 else if (priorities[i] & CODING_CATEGORY_MASK_RAW_TEXT)
4065 mask |= CODING_CATEGORY_MASK_RAW_TEXT;
4066 else if (priorities[i] & CODING_CATEGORY_MASK_BINARY)
4067 mask |= CODING_CATEGORY_MASK_BINARY;
4068 if (mask & priorities[i])
4069 return priorities[i];
4070 }
4071 return CODING_CATEGORY_MASK_RAW_TEXT;
4072 }
4073 if (try & CODING_CATEGORY_MASK_ISO)
4074 mask |= detect_coding_iso2022 (src, src_end, multibytep);
4075 if (try & CODING_CATEGORY_MASK_SJIS)
4076 mask |= detect_coding_sjis (src, src_end, multibytep);
4077 if (try & CODING_CATEGORY_MASK_BIG5)
4078 mask |= detect_coding_big5 (src, src_end, multibytep);
4079 if (try & CODING_CATEGORY_MASK_UTF_8)
4080 mask |= detect_coding_utf_8 (src, src_end, multibytep);
4081 if (try & CODING_CATEGORY_MASK_UTF_16_BE_LE)
4082 mask |= detect_coding_utf_16 (src, src_end, multibytep);
4083 if (try & CODING_CATEGORY_MASK_EMACS_MULE)
4084 mask |= detect_coding_emacs_mule (src, src_end, multibytep);
4085 if (try & CODING_CATEGORY_MASK_CCL)
4086 mask |= detect_coding_ccl (src, src_end, multibytep);
4087 }
4088 return (mask | CODING_CATEGORY_MASK_RAW_TEXT | CODING_CATEGORY_MASK_BINARY);
4089 }
4090
4091 /* Detect how a text of length SRC_BYTES pointed by SRC is encoded.
4092 The information of the detected coding system is set in CODING. */
4093
4094 void
4095 detect_coding (coding, src, src_bytes)
4096 struct coding_system *coding;
4097 unsigned char *src;
4098 int src_bytes;
4099 {
4100 unsigned int idx;
4101 int skip, mask;
4102 Lisp_Object val;
4103
4104 val = Vcoding_category_list;
4105 mask = detect_coding_mask (src, src_bytes, coding_priorities, &skip,
4106 coding->src_multibyte);
4107 coding->heading_ascii = skip;
4108
4109 if (!mask) return;
4110
4111 /* We found a single coding system of the highest priority in MASK. */
4112 idx = 0;
4113 while (mask && ! (mask & 1)) mask >>= 1, idx++;
4114 if (! mask)
4115 idx = CODING_CATEGORY_IDX_RAW_TEXT;
4116
4117 val = SYMBOL_VALUE (XVECTOR (Vcoding_category_table)->contents[idx]);
4118
4119 if (coding->eol_type != CODING_EOL_UNDECIDED)
4120 {
4121 Lisp_Object tmp;
4122
4123 tmp = Fget (val, Qeol_type);
4124 if (VECTORP (tmp))
4125 val = XVECTOR (tmp)->contents[coding->eol_type];
4126 }
4127
4128 /* Setup this new coding system while preserving some slots. */
4129 {
4130 int src_multibyte = coding->src_multibyte;
4131 int dst_multibyte = coding->dst_multibyte;
4132
4133 setup_coding_system (val, coding);
4134 coding->src_multibyte = src_multibyte;
4135 coding->dst_multibyte = dst_multibyte;
4136 coding->heading_ascii = skip;
4137 }
4138 }
4139
4140 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
4141 SOURCE is encoded. Return one of CODING_EOL_LF, CODING_EOL_CRLF,
4142 CODING_EOL_CR, and CODING_EOL_UNDECIDED.
4143
4144 How many non-eol characters are at the head is returned as *SKIP. */
4145
4146 #define MAX_EOL_CHECK_COUNT 3
4147
4148 static int
4149 detect_eol_type (source, src_bytes, skip)
4150 unsigned char *source;
4151 int src_bytes, *skip;
4152 {
4153 unsigned char *src = source, *src_end = src + src_bytes; 4762 unsigned char *src = source, *src_end = src + src_bytes;
4154 unsigned char c; 4763 unsigned char c;
4155 int total = 0; /* How many end-of-lines are found so far. */ 4764 int total = 0;
4156 int eol_type = CODING_EOL_UNDECIDED; 4765 int eol_seen = EOL_SEEN_NONE;
4157 int this_eol_type; 4766 int first_eol_seen;
4158 4767
4159 *skip = 0; 4768 attrs = CODING_ID_ATTRS (coding->id);
4160 4769 coding_type = CODING_ATTR_TYPE (attrs);
4161 while (src < src_end && total < MAX_EOL_CHECK_COUNT) 4770
4162 { 4771 if (EQ (coding_type, Qccl))
4163 c = *src++; 4772 {
4164 if (c == '\n' || c == '\r') 4773 int msb, lsb;
4165 { 4774
4166 if (*skip == 0) 4775 msb = coding->spec.utf_16.endian == utf_16_little_endian;
4167 *skip = src - 1 - source; 4776 lsb = 1 - msb;
4168 total++; 4777
4169 if (c == '\n') 4778 while (src + 1 < src_end)
4170 this_eol_type = CODING_EOL_LF; 4779 {
4171 else if (src >= src_end || *src != '\n') 4780 c = src[lsb];
4172 this_eol_type = CODING_EOL_CR; 4781 if (src[msb] == 0 && (c == '\n' || c == '\r'))
4173 else
4174 this_eol_type = CODING_EOL_CRLF, src++;
4175
4176 if (eol_type == CODING_EOL_UNDECIDED)
4177 /* This is the first end-of-line. */
4178 eol_type = this_eol_type;
4179 else if (eol_type != this_eol_type)
4180 { 4782 {
4181 /* The found type is different from what found before. */ 4783 int this_eol;
4182 eol_type = CODING_EOL_INCONSISTENT; 4784
4183 break; 4785 if (c == '\n')
4786 this_eol = EOL_SEEN_LF;
4787 else if (src + 3 >= src_end
4788 || src[msb + 2] != 0
4789 || src[lsb + 2] != '\n')
4790 this_eol = EOL_SEEN_CR;
4791 else
4792 this_eol = EOL_SEEN_CRLF;
4793
4794 if (eol_seen == EOL_SEEN_NONE)
4795 /* This is the first end-of-line. */
4796 eol_seen = this_eol;
4797 else if (eol_seen != this_eol)
4798 {
4799 /* The found type is different from what found before. */
4800 eol_seen = EOL_SEEN_LF;
4801 break;
4802 }
4803 if (++total == MAX_EOL_CHECK_COUNT)
4804 break;
4184 } 4805 }
4185 } 4806 src += 2;
4186 } 4807 }
4187 4808 }
4188 if (*skip == 0)
4189 *skip = src_end - source;
4190 return eol_type;
4191 }
4192
4193 /* Like detect_eol_type, but detect EOL type in 2-octet
4194 big-endian/little-endian format for coding systems utf-16-be and
4195 utf-16-le. */
4196
4197 static int
4198 detect_eol_type_in_2_octet_form (source, src_bytes, skip, big_endian_p)
4199 unsigned char *source;
4200 int src_bytes, *skip, big_endian_p;
4201 {
4202 unsigned char *src = source, *src_end = src + src_bytes;
4203 unsigned int c1, c2;
4204 int total = 0; /* How many end-of-lines are found so far. */
4205 int eol_type = CODING_EOL_UNDECIDED;
4206 int this_eol_type;
4207 int msb, lsb;
4208
4209 if (big_endian_p)
4210 msb = 0, lsb = 1;
4211 else 4809 else
4212 msb = 1, lsb = 0; 4810 {
4213 4811 while (src < src_end)
4214 *skip = 0; 4812 {
4215 4813 c = *src++;
4216 while ((src + 1) < src_end && total < MAX_EOL_CHECK_COUNT) 4814 if (c == '\n' || c == '\r')
4217 {
4218 c1 = (src[msb] << 8) | (src[lsb]);
4219 src += 2;
4220
4221 if (c1 == '\n' || c1 == '\r')
4222 {
4223 if (*skip == 0)
4224 *skip = src - 2 - source;
4225 total++;
4226 if (c1 == '\n')
4227 { 4815 {
4228 this_eol_type = CODING_EOL_LF; 4816 int this_eol;
4817
4818 if (c == '\n')
4819 this_eol = EOL_SEEN_LF;
4820 else if (src >= src_end || *src != '\n')
4821 this_eol = EOL_SEEN_CR;
4822 else
4823 this_eol = EOL_SEEN_CRLF, src++;
4824
4825 if (eol_seen == EOL_SEEN_NONE)
4826 /* This is the first end-of-line. */
4827 eol_seen = this_eol;
4828 else if (eol_seen != this_eol)
4829 {
4830 /* The found type is different from what found before. */
4831 eol_seen = EOL_SEEN_LF;
4832 break;
4833 }
4834 if (++total == MAX_EOL_CHECK_COUNT)
4835 break;
4229 } 4836 }
4230 else 4837 }
4838 }
4839 return eol_seen;
4840 }
4841
4842
4843 static void
4844 adjust_coding_eol_type (coding, eol_seen)
4845 struct coding_system *coding;
4846 int eol_seen;
4847 {
4848 Lisp_Object eol_type, coding_system;
4849
4850 eol_type = CODING_ID_EOL_TYPE (coding->id);
4851 if (eol_seen & EOL_SEEN_LF)
4852 coding->id = CODING_SYSTEM_ID (AREF (eol_type, 0));
4853 else if (eol_type & EOL_SEEN_CRLF)
4854 coding->id = CODING_SYSTEM_ID (AREF (eol_type, 1));
4855 else if (eol_type & EOL_SEEN_CR)
4856 coding->id = CODING_SYSTEM_ID (AREF (eol_type, 2));
4857 }
4858
4859 /* Detect how a text specified in CODING is encoded. If a coding
4860 system is detected, update fields of CODING by the detected coding
4861 system. */
4862
4863 void
4864 detect_coding (coding)
4865 struct coding_system *coding;
4866 {
4867 unsigned char *src, *src_end;
4868 Lisp_Object attrs, coding_type;
4869
4870 coding->consumed = coding->consumed_char = 0;
4871 coding->produced = coding->produced_char = 0;
4872 coding_set_source (coding);
4873
4874 src_end = coding->source + coding->src_bytes;
4875
4876 /* If we have not yet decided the text encoding type, detect it
4877 now. */
4878 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding->id)), Qundecided))
4879 {
4880 int mask = CATEGORY_MASK_ANY;
4881 int c, i;
4882
4883 for (src = coding->source; src < src_end; src++)
4884 {
4885 c = *src;
4886 if (c & 0x80 || (c < 0x20 && (c == ISO_CODE_ESC
4887 || c == ISO_CODE_SI
4888 || c == ISO_CODE_SO)))
4889 break;
4890 }
4891 coding->head_ascii = src - (coding->source + coding->consumed);
4892
4893 if (coding->head_ascii < coding->src_bytes)
4894 {
4895 int detected = 0;
4896
4897 for (i = 0; i < coding_category_raw_text; i++)
4231 { 4898 {
4232 if ((src + 1) >= src_end) 4899 enum coding_category category = coding_priorities[i];
4900 struct coding_system *this = coding_categories + category;
4901
4902 if (category >= coding_category_raw_text
4903 || detected & (1 << category))
4904 continue;
4905
4906 if (this->id < 0)
4233 { 4907 {
4234 this_eol_type = CODING_EOL_CR; 4908 /* No coding system of this category is defined. */
4909 mask &= ~(1 << category);
4235 } 4910 }
4236 else 4911 else
4237 { 4912 {
4238 c2 = (src[msb] << 8) | (src[lsb]); 4913 detected |= detected_mask[category];
4239 if (c2 == '\n') 4914 if ((*(this->detector)) (coding, &mask))
4240 this_eol_type = CODING_EOL_CRLF, src += 2; 4915 break;
4241 else
4242 this_eol_type = CODING_EOL_CR;
4243 } 4916 }
4244 } 4917 }
4245 4918 if (! mask)
4246 if (eol_type == CODING_EOL_UNDECIDED) 4919 setup_coding_system (Qraw_text, coding);
4247 /* This is the first end-of-line. */ 4920 else if (mask != CATEGORY_MASK_ANY)
4248 eol_type = this_eol_type; 4921 for (i = 0; i < coding_category_raw_text; i++)
4249 else if (eol_type != this_eol_type) 4922 {
4923 enum coding_category category = coding_priorities[i];
4924 struct coding_system *this = coding_categories + category;
4925
4926 if (mask & (1 << category))
4927 {
4928 setup_coding_system (CODING_ID_NAME (this->id), coding);
4929 break;
4930 }
4931 }
4932 }
4933 }
4934
4935 attrs = CODING_ID_ATTRS (coding->id);
4936 coding_type = CODING_ATTR_TYPE (attrs);
4937
4938 /* If we have not yet decided the EOL type, detect it now. But, the
4939 detection is impossible for a CCL based coding system, in which
4940 case, we detct the EOL type after decoding. */
4941 if (VECTORP (CODING_ID_EOL_TYPE (coding->id))
4942 && ! EQ (coding_type, Qccl))
4943 {
4944 int eol_seen = detect_eol (coding, coding->source, coding->src_bytes);
4945
4946 if (eol_seen != EOL_SEEN_NONE)
4947 adjust_coding_eol_type (coding, eol_seen);
4948 }
4949 }
4950
4951
4952 static void
4953 decode_eol (coding)
4954 struct coding_system *coding;
4955 {
4956 if (VECTORP (CODING_ID_EOL_TYPE (coding->id)))
4957 {
4958 unsigned char *p = CHAR_POS_ADDR (coding->dst_pos);
4959 unsigned char *pend = p + coding->produced;
4960 int eol_seen = EOL_SEEN_NONE;
4961
4962 for (; p < pend; p++)
4963 {
4964 if (*p == '\n')
4965 eol_seen |= EOL_SEEN_LF;
4966 else if (*p == '\r')
4250 { 4967 {
4251 /* The found type is different from what found before. */ 4968 if (p + 1 < pend && *(p + 1) == '\n')
4252 eol_type = CODING_EOL_INCONSISTENT; 4969 {
4253 break; 4970 eol_seen |= EOL_SEEN_CRLF;
4971 p++;
4972 }
4973 else
4974 eol_seen |= EOL_SEEN_CR;
4254 } 4975 }
4255 } 4976 }
4256 } 4977 if (eol_seen != EOL_SEEN_NONE)
4257 4978 adjust_coding_eol_type (coding, eol_seen);
4258 if (*skip == 0) 4979 }
4259 *skip = src_end - source; 4980
4260 return eol_type; 4981 if (EQ (CODING_ID_EOL_TYPE (coding->id), Qmac))
4261 } 4982 {
4262 4983 unsigned char *p = CHAR_POS_ADDR (coding->dst_pos);
4263 /* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC 4984 unsigned char *pend = p + coding->produced;
4264 is encoded. If it detects an appropriate format of end-of-line, it 4985
4265 sets the information in *CODING. */ 4986 for (; p < pend; p++)
4266 4987 if (*p == '\r')
4267 void 4988 *p = '\n';
4268 detect_eol (coding, src, src_bytes) 4989 }
4990 else if (EQ (CODING_ID_EOL_TYPE (coding->id), Qdos))
4991 {
4992 unsigned char *p, *pbeg, *pend;
4993 Lisp_Object undo_list;
4994
4995 move_gap_both (coding->dst_pos + coding->produced_char,
4996 coding->dst_pos_byte + coding->produced);
4997 undo_list = current_buffer->undo_list;
4998 current_buffer->undo_list = Qt;
4999 del_range_2 (coding->dst_pos, coding->dst_pos_byte, GPT, GPT_BYTE, Qnil);
5000 current_buffer->undo_list = undo_list;
5001 pbeg = GPT_ADDR;
5002 pend = pbeg + coding->produced;
5003
5004 for (p = pend - 1; p >= pbeg; p--)
5005 if (*p == '\r')
5006 {
5007 safe_bcopy ((char *) (p + 1), (char *) p, pend - p - 1);
5008 pend--;
5009 }
5010 coding->produced_char -= coding->produced - (pend - pbeg);
5011 coding->produced = pend - pbeg;
5012 insert_from_gap (coding->produced_char, coding->produced);
5013 }
5014 }
5015
5016 static void
5017 translate_chars (coding, table)
4269 struct coding_system *coding; 5018 struct coding_system *coding;
4270 unsigned char *src; 5019 Lisp_Object table;
4271 int src_bytes; 5020 {
4272 { 5021 int *charbuf = coding->charbuf;
4273 Lisp_Object val; 5022 int *charbuf_end = charbuf + coding->charbuf_used;
4274 int skip; 5023 int c;
4275 int eol_type; 5024
4276 5025 if (coding->chars_at_source)
4277 switch (coding->category_idx) 5026 return;
4278 { 5027
4279 case CODING_CATEGORY_IDX_UTF_16_BE: 5028 while (charbuf < charbuf_end)
4280 eol_type = detect_eol_type_in_2_octet_form (src, src_bytes, &skip, 1); 5029 {
4281 break; 5030 c = *charbuf;
4282 case CODING_CATEGORY_IDX_UTF_16_LE: 5031 if (c < 0)
4283 eol_type = detect_eol_type_in_2_octet_form (src, src_bytes, &skip, 0); 5032 charbuf += c;
4284 break; 5033 else
4285 default: 5034 *charbuf++ = translate_char (table, c);
4286 eol_type = detect_eol_type (src, src_bytes, &skip); 5035 }
4287 break; 5036 }
4288 } 5037
4289 5038 static int
4290 if (coding->heading_ascii > skip) 5039 produce_chars (coding)
4291 coding->heading_ascii = skip; 5040 struct coding_system *coding;
5041 {
5042 unsigned char *dst = coding->destination + coding->produced;
5043 unsigned char *dst_end = coding->destination + coding->dst_bytes;
5044 int produced;
5045 int produced_chars = 0;
5046
5047 if (! coding->chars_at_source)
5048 {
5049 /* Characters are in coding->charbuf. */
5050 int *buf = coding->charbuf;
5051 int *buf_end = buf + coding->charbuf_used;
5052 unsigned char *adjusted_dst_end;
5053
5054 if (BUFFERP (coding->src_object)
5055 && EQ (coding->src_object, coding->dst_object))
5056 dst_end = coding->source + coding->consumed;
5057 adjusted_dst_end = dst_end - MAX_MULTIBYTE_LENGTH;
5058
5059 while (buf < buf_end)
5060 {
5061 int c = *buf++;
5062
5063 if (dst >= adjusted_dst_end)
5064 {
5065 dst = alloc_destination (coding,
5066 buf_end - buf + MAX_MULTIBYTE_LENGTH,
5067 dst);
5068 dst_end = coding->destination + coding->dst_bytes;
5069 adjusted_dst_end = dst_end - MAX_MULTIBYTE_LENGTH;
5070 }
5071 if (c >= 0)
5072 {
5073 if (coding->dst_multibyte
5074 || ! CHAR_BYTE8_P (c))
5075 CHAR_STRING_ADVANCE (c, dst);
5076 else
5077 *dst++ = CHAR_TO_BYTE8 (c);
5078 produced_chars++;
5079 }
5080 else
5081 /* This is an annotation data. */
5082 buf -= c + 1;
5083 }
5084 }
4292 else 5085 else
4293 skip = coding->heading_ascii; 5086 {
4294 5087 int multibytep = coding->src_multibyte;
4295 if (eol_type == CODING_EOL_UNDECIDED) 5088 unsigned char *src = coding->source;
4296 return; 5089 unsigned char *src_end = src + coding->src_bytes;
4297 if (eol_type == CODING_EOL_INCONSISTENT) 5090 Lisp_Object eol_type;
4298 { 5091
4299 #if 0 5092 eol_type = CODING_ID_EOL_TYPE (coding->id);
4300 /* This code is suppressed until we find a better way to 5093
4301 distinguish raw text file and binary file. */ 5094 if (coding->src_multibyte != coding->dst_multibyte)
4302 5095 {
4303 /* If we have already detected that the coding is raw-text, the 5096 if (coding->src_multibyte)
4304 coding should actually be no-conversion. */
4305 if (coding->type == coding_type_raw_text)
4306 {
4307 setup_coding_system (Qno_conversion, coding);
4308 return;
4309 }
4310 /* Else, let's decode only text code anyway. */
4311 #endif /* 0 */
4312 eol_type = CODING_EOL_LF;
4313 }
4314
4315 val = Fget (coding->symbol, Qeol_type);
4316 if (VECTORP (val) && XVECTOR (val)->size == 3)
4317 {
4318 int src_multibyte = coding->src_multibyte;
4319 int dst_multibyte = coding->dst_multibyte;
4320
4321 setup_coding_system (XVECTOR (val)->contents[eol_type], coding);
4322 coding->src_multibyte = src_multibyte;
4323 coding->dst_multibyte = dst_multibyte;
4324 coding->heading_ascii = skip;
4325 }
4326 }
4327
4328 #define CONVERSION_BUFFER_EXTRA_ROOM 256
4329
4330 #define DECODING_BUFFER_MAG(coding) \
4331 (coding->type == coding_type_iso2022 \
4332 ? 3 \
4333 : (coding->type == coding_type_ccl \
4334 ? coding->spec.ccl.decoder.buf_magnification \
4335 : 2))
4336
4337 /* Return maximum size (bytes) of a buffer enough for decoding
4338 SRC_BYTES of text encoded in CODING. */
4339
4340 int
4341 decoding_buffer_size (coding, src_bytes)
4342 struct coding_system *coding;
4343 int src_bytes;
4344 {
4345 return (src_bytes * DECODING_BUFFER_MAG (coding)
4346 + CONVERSION_BUFFER_EXTRA_ROOM);
4347 }
4348
4349 /* Return maximum size (bytes) of a buffer enough for encoding
4350 SRC_BYTES of text to CODING. */
4351
4352 int
4353 encoding_buffer_size (coding, src_bytes)
4354 struct coding_system *coding;
4355 int src_bytes;
4356 {
4357 int magnification;
4358
4359 if (coding->type == coding_type_ccl)
4360 magnification = coding->spec.ccl.encoder.buf_magnification;
4361 else if (CODING_REQUIRE_ENCODING (coding))
4362 magnification = 3;
4363 else
4364 magnification = 1;
4365
4366 return (src_bytes * magnification + CONVERSION_BUFFER_EXTRA_ROOM);
4367 }
4368
4369 /* Working buffer for code conversion. */
4370 struct conversion_buffer
4371 {
4372 int size; /* size of data. */
4373 int on_stack; /* 1 if allocated by alloca. */
4374 unsigned char *data;
4375 };
4376
4377 /* Don't use alloca for allocating memory space larger than this, lest
4378 we overflow their stack. */
4379 #define MAX_ALLOCA 16*1024
4380
4381 /* Allocate LEN bytes of memory for BUF (struct conversion_buffer). */
4382 #define allocate_conversion_buffer(buf, len) \
4383 do { \
4384 if (len < MAX_ALLOCA) \
4385 { \
4386 buf.data = (unsigned char *) alloca (len); \
4387 buf.on_stack = 1; \
4388 } \
4389 else \
4390 { \
4391 buf.data = (unsigned char *) xmalloc (len); \
4392 buf.on_stack = 0; \
4393 } \
4394 buf.size = len; \
4395 } while (0)
4396
4397 /* Double the allocated memory for *BUF. */
4398 static void
4399 extend_conversion_buffer (buf)
4400 struct conversion_buffer *buf;
4401 {
4402 if (buf->on_stack)
4403 {
4404 unsigned char *save = buf->data;
4405 buf->data = (unsigned char *) xmalloc (buf->size * 2);
4406 bcopy (save, buf->data, buf->size);
4407 buf->on_stack = 0;
4408 }
4409 else
4410 {
4411 buf->data = (unsigned char *) xrealloc (buf->data, buf->size * 2);
4412 }
4413 buf->size *= 2;
4414 }
4415
4416 /* Free the allocated memory for BUF if it is not on stack. */
4417 static void
4418 free_conversion_buffer (buf)
4419 struct conversion_buffer *buf;
4420 {
4421 if (!buf->on_stack)
4422 xfree (buf->data);
4423 }
4424
4425 int
4426 ccl_coding_driver (coding, source, destination, src_bytes, dst_bytes, encodep)
4427 struct coding_system *coding;
4428 unsigned char *source, *destination;
4429 int src_bytes, dst_bytes, encodep;
4430 {
4431 struct ccl_program *ccl
4432 = encodep ? &coding->spec.ccl.encoder : &coding->spec.ccl.decoder;
4433 unsigned char *dst = destination;
4434
4435 ccl->suppress_error = coding->suppress_error;
4436 ccl->last_block = coding->mode & CODING_MODE_LAST_BLOCK;
4437 if (encodep)
4438 {
4439 /* On encoding, EOL format is converted within ccl_driver. For
4440 that, setup proper information in the structure CCL. */
4441 ccl->eol_type = coding->eol_type;
4442 if (ccl->eol_type ==CODING_EOL_UNDECIDED)
4443 ccl->eol_type = CODING_EOL_LF;
4444 ccl->cr_consumed = coding->spec.ccl.cr_carryover;
4445 }
4446 ccl->multibyte = coding->src_multibyte;
4447 if (coding->spec.ccl.eight_bit_carryover[0] != 0)
4448 {
4449 /* Move carryover bytes to DESTINATION. */
4450 unsigned char *p = coding->spec.ccl.eight_bit_carryover;
4451 while (*p)
4452 *dst++ = *p++;
4453 coding->spec.ccl.eight_bit_carryover[0] = 0;
4454 if (dst_bytes)
4455 dst_bytes -= dst - destination;
4456 }
4457
4458 coding->produced = (ccl_driver (ccl, source, dst, src_bytes, dst_bytes,
4459 &(coding->consumed))
4460 + dst - destination);
4461
4462 if (encodep)
4463 {
4464 coding->produced_char = coding->produced;
4465 coding->spec.ccl.cr_carryover = ccl->cr_consumed;
4466 }
4467 else if (!ccl->eight_bit_control)
4468 {
4469 /* The produced bytes forms a valid multibyte sequence. */
4470 coding->produced_char
4471 = multibyte_chars_in_text (destination, coding->produced);
4472 coding->spec.ccl.eight_bit_carryover[0] = 0;
4473 }
4474 else
4475 {
4476 /* On decoding, the destination should always multibyte. But,
4477 CCL program might have been generated an invalid multibyte
4478 sequence. Here we make such a sequence valid as
4479 multibyte. */
4480 int bytes
4481 = dst_bytes ? dst_bytes : source + coding->consumed - destination;
4482
4483 if ((coding->consumed < src_bytes
4484 || !ccl->last_block)
4485 && coding->produced >= 1
4486 && destination[coding->produced - 1] >= 0x80)
4487 {
4488 /* We should not convert the tailing 8-bit codes to
4489 multibyte form even if they doesn't form a valid
4490 multibyte sequence. They may form a valid sequence in
4491 the next call. */
4492 int carryover = 0;
4493
4494 if (destination[coding->produced - 1] < 0xA0)
4495 carryover = 1;
4496 else if (coding->produced >= 2)
4497 { 5097 {
4498 if (destination[coding->produced - 2] >= 0x80) 5098 int consumed_chars;
5099
5100 while (1)
4499 { 5101 {
4500 if (destination[coding->produced - 2] < 0xA0) 5102 unsigned char *src_base = src;
4501 carryover = 2; 5103 int c;
4502 else if (coding->produced >= 3 5104
4503 && destination[coding->produced - 3] >= 0x80 5105 ONE_MORE_BYTE (c);
4504 && destination[coding->produced - 3] < 0xA0) 5106 if (c == '\r')
4505 carryover = 3; 5107 {
5108 if (EQ (eol_type, Qdos))
5109 {
5110 if (src < src_end
5111 && *src == '\n')
5112 c = *src++;
5113 }
5114 else if (EQ (eol_type, Qmac))
5115 c = '\n';
5116 }
5117 if (dst == dst_end)
5118 {
5119 EMACS_INT offset = src - coding->source;
5120
5121 dst = alloc_destination (coding, src_end - src + 1, dst);
5122 dst_end = coding->destination + coding->dst_bytes;
5123 coding_set_source (coding);
5124 src = coding->source + offset;
5125 src_end = coding->source + coding->src_bytes;
5126 }
5127 *dst++ = c;
5128 produced_chars++;
5129 }
5130 no_more_source:
5131 ;
5132 }
5133 else
5134 while (src < src_end)
5135 {
5136 int c = *src++;
5137
5138 if (c == '\r')
5139 {
5140 if (EQ (eol_type, Qdos))
5141 {
5142 if (src < src_end
5143 && *src == '\n')
5144 c = *src++;
5145 }
5146 else if (EQ (eol_type, Qmac))
5147 c = '\n';
5148 }
5149 if (dst >= dst_end - 1)
5150 {
5151 EMACS_INT offset = src - coding->source;
5152
5153 dst = alloc_destination (coding, src_end - src + 2, dst);
5154 dst_end = coding->destination + coding->dst_bytes;
5155 coding_set_source (coding);
5156 src = coding->source + offset;
5157 src_end = coding->source + coding->src_bytes;
5158 }
5159 EMIT_ONE_BYTE (c);
5160 }
5161 }
5162 else
5163 {
5164 if (!EQ (coding->src_object, coding->dst_object))
5165 {
5166 int require = coding->src_bytes - coding->dst_bytes;
5167
5168 if (require > 0)
5169 {
5170 EMACS_INT offset = src - coding->source;
5171
5172 dst = alloc_destination (coding, require, dst);
5173 coding_set_source (coding);
5174 src = coding->source + offset;
5175 src_end = coding->source + coding->src_bytes;
4506 } 5176 }
4507 } 5177 }
4508 if (carryover > 0) 5178 produced_chars = coding->src_chars;
5179 while (src < src_end)
4509 { 5180 {
4510 BCOPY_SHORT (destination + coding->produced - carryover, 5181 int c = *src++;
4511 coding->spec.ccl.eight_bit_carryover, 5182
4512 carryover); 5183 if (c == '\r')
4513 coding->spec.ccl.eight_bit_carryover[carryover] = 0; 5184 {
4514 coding->produced -= carryover; 5185 if (EQ (eol_type, Qdos))
5186 {
5187 if (src < src_end
5188 && *src == '\n')
5189 c = *src++;
5190 produced_chars--;
5191 }
5192 else if (EQ (eol_type, Qmac))
5193 c = '\n';
5194 }
5195 *dst++ = c;
4515 } 5196 }
4516 } 5197 }
4517 coding->produced = str_as_multibyte (destination, bytes, 5198 }
4518 coding->produced, 5199
4519 &(coding->produced_char)); 5200 produced = dst - (coding->destination + coding->produced);
4520 } 5201 if (BUFFERP (coding->dst_object))
4521 5202 insert_from_gap (produced_chars, produced);
4522 switch (ccl->status) 5203 coding->produced += produced;
4523 { 5204 coding->produced_char += produced_chars;
4524 case CCL_STAT_SUSPEND_BY_SRC: 5205 return produced_chars;
4525 coding->result = CODING_FINISH_INSUFFICIENT_SRC; 5206 }
4526 break; 5207
4527 case CCL_STAT_SUSPEND_BY_DST: 5208 /* [ -LENGTH CHAR_POS_OFFSET MASK METHOD COMP_LEN ]
4528 coding->result = CODING_FINISH_INSUFFICIENT_DST; 5209 or
4529 break; 5210 [ -LENGTH CHAR_POS_OFFSET MASK METHOD COMP_LEN COMPONENTS... ]
4530 case CCL_STAT_QUIT: 5211 */
4531 case CCL_STAT_INVALID_CMD: 5212
4532 coding->result = CODING_FINISH_INTERRUPT; 5213 static INLINE void
4533 break; 5214 produce_composition (coding, charbuf)
4534 default: 5215 struct coding_system *coding;
4535 coding->result = CODING_FINISH_NORMAL; 5216 int *charbuf;
4536 break; 5217 {
4537 } 5218 Lisp_Object buffer;
5219 int len;
5220 EMACS_INT pos;
5221 enum composition_method method;
5222 int cmp_len;
5223 Lisp_Object components;
5224
5225 buffer = coding->dst_object;
5226 len = -charbuf[0];
5227 pos = coding->dst_pos + charbuf[1];
5228 method = (enum composition_method) (charbuf[3]);
5229 cmp_len = charbuf[4];
5230
5231 if (method == COMPOSITION_RELATIVE)
5232 components = Qnil;
5233 else
5234 {
5235 Lisp_Object args[MAX_COMPOSITION_COMPONENTS * 2 - 1];
5236 int i;
5237
5238 len -= 5;
5239 charbuf += 5;
5240 for (i = 0; i < len; i++)
5241 args[i] = make_number (charbuf[i]);
5242 components = (method == COMPOSITION_WITH_ALTCHARS
5243 ? Fstring (len, args) : Fvector (len, args));
5244 }
5245 compose_text (pos, pos + cmp_len, components, Qnil, Qnil);
5246 }
5247
5248 static int *
5249 save_composition_data (buf, buf_end, prop)
5250 int *buf, *buf_end;
5251 Lisp_Object prop;
5252 {
5253 enum composition_method method = COMPOSITION_METHOD (prop);
5254 int cmp_len = COMPOSITION_LENGTH (prop);
5255
5256 if (buf + 4 + (MAX_COMPOSITION_COMPONENTS * 2 - 1) > buf_end)
5257 return NULL;
5258
5259 buf[1] = CODING_ANNOTATE_COMPOSITION_MASK;
5260 buf[2] = method;
5261 buf[3] = cmp_len;
5262
5263 if (method == COMPOSITION_RELATIVE)
5264 buf[0] = 4;
5265 else
5266 {
5267 Lisp_Object components;
5268 int len, i;
5269
5270 components = COMPOSITION_COMPONENTS (prop);
5271 if (VECTORP (components))
5272 {
5273 len = XVECTOR (components)->size;
5274 for (i = 0; i < len; i++)
5275 buf[4 + i] = XINT (AREF (components, i));
5276 }
5277 else if (STRINGP (components))
5278 {
5279 int i_byte;
5280
5281 len = XSTRING (components)->size;
5282 i = i_byte = 0;
5283 while (i < len)
5284 FETCH_STRING_CHAR_ADVANCE (buf[4 + i], components, i, i_byte);
5285 }
5286 else if (INTEGERP (components))
5287 {
5288 len = 1;
5289 buf[4] = XINT (components);
5290 }
5291 else if (CONSP (components))
5292 {
5293 for (len = 0; CONSP (components);
5294 len++, components = XCDR (components))
5295 buf[4 + len] = XINT (XCAR (components));
5296 }
5297 else
5298 abort ();
5299 buf[0] = 4 + len;
5300 }
5301 return (buf + buf[0]);
5302 }
5303
5304 #define CHARBUF_SIZE 0x4000
5305
5306 #define ALLOC_CONVERSION_WORK_AREA(coding) \
5307 do { \
5308 int size = CHARBUF_SIZE;; \
5309 \
5310 coding->charbuf = NULL; \
5311 while (size > 1024) \
5312 { \
5313 coding->charbuf = (int *) alloca (sizeof (int) * size); \
5314 if (coding->charbuf) \
5315 break; \
5316 size >>= 1; \
5317 } \
5318 if (! coding->charbuf) \
5319 { \
5320 coding->result = CODING_RESULT_INSUFFICIENT_MEM; \
5321 return coding->result; \
5322 } \
5323 coding->charbuf_size = size; \
5324 } while (0)
5325
5326
5327 static void
5328 produce_annotation (coding)
5329 struct coding_system *coding;
5330 {
5331 int *charbuf = coding->charbuf;
5332 int *charbuf_end = charbuf + coding->charbuf_used;
5333
5334 while (charbuf < charbuf_end)
5335 {
5336 if (*charbuf >= 0)
5337 charbuf++;
5338 else
5339 {
5340 int len = -*charbuf;
5341 switch (charbuf[2])
5342 {
5343 case CODING_ANNOTATE_COMPOSITION_MASK:
5344 produce_composition (coding, charbuf);
5345 break;
5346 default:
5347 abort ();
5348 }
5349 charbuf += len;
5350 }
5351 }
5352 }
5353
5354 /* Decode the data at CODING->src_object into CODING->dst_object.
5355 CODING->src_object is a buffer, a string, or nil.
5356 CODING->dst_object is a buffer.
5357
5358 If CODING->src_object is a buffer, it must be the current buffer.
5359 In this case, if CODING->src_pos is positive, it is a position of
5360 the source text in the buffer, otherwise, the source text is in the
5361 gap area of the buffer, and CODING->src_pos specifies the offset of
5362 the text from GPT (which must be the same as PT). If this is the
5363 same buffer as CODING->dst_object, CODING->src_pos must be
5364 negative.
5365
5366 If CODING->src_object is a string, CODING->src_pos in an index to
5367 that string.
5368
5369 If CODING->src_object is nil, CODING->source must already point to
5370 the non-relocatable memory area. In this case, CODING->src_pos is
5371 an offset from CODING->source.
5372
5373 The decoded data is inserted at the current point of the buffer
5374 CODING->dst_object.
5375 */
5376
5377 static int
5378 decode_coding (coding)
5379 struct coding_system *coding;
5380 {
5381 Lisp_Object attrs;
5382
5383 if (BUFFERP (coding->src_object)
5384 && coding->src_pos > 0
5385 && coding->src_pos < GPT
5386 && coding->src_pos + coding->src_chars > GPT)
5387 move_gap_both (coding->src_pos, coding->src_pos_byte);
5388
5389 if (BUFFERP (coding->dst_object))
5390 {
5391 if (current_buffer != XBUFFER (coding->dst_object))
5392 set_buffer_internal (XBUFFER (coding->dst_object));
5393 if (GPT != PT)
5394 move_gap_both (PT, PT_BYTE);
5395 }
5396
5397 coding->consumed = coding->consumed_char = 0;
5398 coding->produced = coding->produced_char = 0;
5399 coding->chars_at_source = 0;
5400 coding->result = CODING_RESULT_SUCCESS;
5401 coding->errors = 0;
5402
5403 ALLOC_CONVERSION_WORK_AREA (coding);
5404
5405 attrs = CODING_ID_ATTRS (coding->id);
5406
5407 do
5408 {
5409 coding_set_source (coding);
5410 coding->annotated = 0;
5411 (*(coding->decoder)) (coding);
5412 if (!NILP (CODING_ATTR_DECODE_TBL (attrs)))
5413 translate_chars (CODING_ATTR_DECODE_TBL (attrs), coding);
5414 coding_set_destination (coding);
5415 produce_chars (coding);
5416 if (coding->annotated)
5417 produce_annotation (coding);
5418 }
5419 while (coding->consumed < coding->src_bytes
5420 && ! coding->result);
5421
5422 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding->id)), Qccl)
5423 && SYMBOLP (CODING_ID_EOL_TYPE (coding->id))
5424 && ! EQ (CODING_ID_EOL_TYPE (coding->id), Qunix))
5425 decode_eol (coding);
5426
5427 coding->carryover_bytes = 0;
5428 if (coding->consumed < coding->src_bytes)
5429 {
5430 int nbytes = coding->src_bytes - coding->consumed;
5431 unsigned char *src;
5432
5433 coding_set_source (coding);
5434 coding_set_destination (coding);
5435 src = coding->source + coding->consumed;
5436
5437 if (coding->mode & CODING_MODE_LAST_BLOCK)
5438 {
5439 /* Flush out unprocessed data as binary chars. We are sure
5440 that the number of data is less than the size of
5441 coding->charbuf. */
5442 int *charbuf = coding->charbuf;
5443
5444 while (nbytes-- > 0)
5445 {
5446 int c = *src++;
5447 *charbuf++ = (c & 0x80 ? - c : c);
5448 }
5449 produce_chars (coding);
5450 }
5451 else
5452 {
5453 /* Record unprocessed bytes in coding->carryover. We are
5454 sure that the number of data is less than the size of
5455 coding->carryover. */
5456 unsigned char *p = coding->carryover;
5457
5458 coding->carryover_bytes = nbytes;
5459 while (nbytes-- > 0)
5460 *p++ = *src++;
5461 }
5462 coding->consumed = coding->src_bytes;
5463 }
5464
5465 if (BUFFERP (coding->dst_object))
5466 {
5467 record_insert (coding->dst_pos, coding->produced_char);
5468 }
5469
4538 return coding->result; 5470 return coding->result;
4539 } 5471 }
4540 5472
4541 /* Decode EOL format of the text at PTR of BYTES length destructively
4542 according to CODING->eol_type. This is called after the CCL
4543 program produced a decoded text at PTR. If we do CRLF->LF
4544 conversion, update CODING->produced and CODING->produced_char. */
4545
4546 static void 5473 static void
4547 decode_eol_post_ccl (coding, ptr, bytes) 5474 consume_chars (coding)
4548 struct coding_system *coding; 5475 struct coding_system *coding;
4549 unsigned char *ptr; 5476 {
4550 int bytes; 5477 int *buf = coding->charbuf;
4551 { 5478 /* -1 is to compensate for CRLF. */
4552 Lisp_Object val, saved_coding_symbol; 5479 int *buf_end = coding->charbuf + coding->charbuf_size - 1;
4553 unsigned char *pend = ptr + bytes; 5480 unsigned char *src = coding->source + coding->consumed;
4554 int dummy; 5481 int pos = coding->src_pos + coding->consumed_char;
4555 5482 int end_pos = coding->src_pos + coding->src_chars;
4556 /* Remember the current coding system symbol. We set it back when 5483 int multibytep = coding->src_multibyte;
4557 an inconsistent EOL is found so that `last-coding-system-used' is 5484 Lisp_Object eol_type;
4558 set to the coding system that doesn't specify EOL conversion. */ 5485 int c;
4559 saved_coding_symbol = coding->symbol; 5486 int start, end, stop;
4560 5487 Lisp_Object object, prop;
4561 coding->spec.ccl.cr_carryover = 0; 5488
4562 if (coding->eol_type == CODING_EOL_UNDECIDED) 5489 eol_type = CODING_ID_EOL_TYPE (coding->id);
4563 { 5490 if (VECTORP (eol_type))
4564 /* Here, to avoid the call of setup_coding_system, we directly 5491 eol_type = Qunix;
4565 call detect_eol_type. */ 5492
4566 coding->eol_type = detect_eol_type (ptr, bytes, &dummy); 5493 object = coding->src_object;
4567 if (coding->eol_type == CODING_EOL_INCONSISTENT) 5494
4568 coding->eol_type = CODING_EOL_LF; 5495 /* Note: composition handling is not yet implemented. */
4569 if (coding->eol_type != CODING_EOL_UNDECIDED) 5496 coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
4570 { 5497
4571 val = Fget (coding->symbol, Qeol_type); 5498 if (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK
4572 if (VECTORP (val) && XVECTOR (val)->size == 3) 5499 && find_composition (pos, end_pos, &start, &end, &prop, object)
4573 coding->symbol = XVECTOR (val)->contents[coding->eol_type]; 5500 && end <= end_pos
4574 } 5501 && (start >= pos
4575 coding->mode |= CODING_MODE_INHIBIT_INCONSISTENT_EOL; 5502 || (find_composition (end, end_pos, &start, &end, &prop, object)
4576 } 5503 && end <= end_pos)))
4577 5504 stop = start;
4578 if (coding->eol_type == CODING_EOL_LF 5505 else
4579 || coding->eol_type == CODING_EOL_UNDECIDED) 5506 stop = end_pos;
4580 { 5507
4581 /* We have nothing to do. */ 5508 while (buf < buf_end)
4582 ptr = pend; 5509 {
4583 } 5510 if (pos == stop)
4584 else if (coding->eol_type == CODING_EOL_CRLF) 5511 {
4585 { 5512 int *p;
4586 unsigned char *pstart = ptr, *p = ptr; 5513
4587 5514 if (pos == end_pos)
4588 if (! (coding->mode & CODING_MODE_LAST_BLOCK) 5515 break;
4589 && *(pend - 1) == '\r') 5516 p = save_composition_data (buf, buf_end, prop);
4590 { 5517 if (p == NULL)
4591 /* If the last character is CR, we can't handle it here 5518 break;
4592 because LF will be in the not-yet-decoded source text. 5519 buf = p;
4593 Recorded that the CR is not yet processed. */ 5520 if (find_composition (end, end_pos, &start, &end, &prop, object)
4594 coding->spec.ccl.cr_carryover = 1; 5521 && end <= end_pos)
4595 coding->produced--; 5522 stop = start;
4596 coding->produced_char--; 5523 else
4597 pend--; 5524 stop = end_pos;
4598 } 5525 }
4599 while (ptr < pend) 5526
4600 { 5527 if (! multibytep)
4601 if (*ptr == '\r') 5528 c = *src++;
5529 else
5530 c = STRING_CHAR_ADVANCE (src);
5531 if ((c == '\r') && (coding->mode & CODING_MODE_SELECTIVE_DISPLAY))
5532 c = '\n';
5533 if (! EQ (eol_type, Qunix))
5534 {
5535 if (c == '\n')
4602 { 5536 {
4603 if (ptr + 1 < pend && *(ptr + 1) == '\n') 5537 if (EQ (eol_type, Qdos))
4604 { 5538 *buf++ = '\r';
4605 *p++ = '\n';
4606 ptr += 2;
4607 }
4608 else 5539 else
4609 { 5540 c = '\r';
4610 if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
4611 goto undo_eol_conversion;
4612 *p++ = *ptr++;
4613 }
4614 } 5541 }
4615 else if (*ptr == '\n' 5542 }
4616 && coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL) 5543 *buf++ = c;
4617 goto undo_eol_conversion; 5544 pos++;
4618 else 5545 }
4619 *p++ = *ptr++; 5546
4620 continue; 5547 coding->consumed = src - coding->source;
4621 5548 coding->consumed_char = pos - coding->src_pos;
4622 undo_eol_conversion: 5549 coding->charbuf_used = buf - coding->charbuf;
4623 /* We have faced with inconsistent EOL format at PTR. 5550 coding->chars_at_source = 0;
4624 Convert all LFs before PTR back to CRLFs. */ 5551 }
4625 for (p--, ptr--; p >= pstart; p--) 5552
5553
5554 /* Encode the text at CODING->src_object into CODING->dst_object.
5555 CODING->src_object is a buffer or a string.
5556 CODING->dst_object is a buffer or nil.
5557
5558 If CODING->src_object is a buffer, it must be the current buffer.
5559 In this case, if CODING->src_pos is positive, it is a position of
5560 the source text in the buffer, otherwise. the source text is in the
5561 gap area of the buffer, and coding->src_pos specifies the offset of
5562 the text from GPT (which must be the same as PT). If this is the
5563 same buffer as CODING->dst_object, CODING->src_pos must be
5564 negative and CODING should not have `pre-write-conversion'.
5565
5566 If CODING->src_object is a string, CODING should not have
5567 `pre-write-conversion'.
5568
5569 If CODING->dst_object is a buffer, the encoded data is inserted at
5570 the current point of that buffer.
5571
5572 If CODING->dst_object is nil, the encoded data is placed at the
5573 memory area specified by CODING->destination. */
5574
5575 static int
5576 encode_coding (coding)
5577 struct coding_system *coding;
5578 {
5579 int error = 0;
5580 Lisp_Object attrs;
5581
5582 attrs = CODING_ID_ATTRS (coding->id);
5583
5584 if (BUFFERP (coding->dst_object))
5585 {
5586 set_buffer_internal (XBUFFER (coding->dst_object));
5587 coding->dst_multibyte
5588 = ! NILP (current_buffer->enable_multibyte_characters);
5589 }
5590
5591 coding->consumed = coding->consumed_char = 0;
5592 coding->produced = coding->produced_char = 0;
5593 coding->result = CODING_RESULT_SUCCESS;
5594 coding->errors = 0;
5595
5596 ALLOC_CONVERSION_WORK_AREA (coding);
5597
5598 do {
5599 coding_set_source (coding);
5600 consume_chars (coding);
5601
5602 if (!NILP (CODING_ATTR_ENCODE_TBL (attrs)))
5603 translate_chars (CODING_ATTR_ENCODE_TBL (attrs), coding);
5604
5605 coding_set_destination (coding);
5606 (*(coding->encoder)) (coding);
5607 } while (coding->consumed_char < coding->src_chars);
5608
5609 if (BUFFERP (coding->dst_object))
5610 insert_from_gap (coding->produced_char, coding->produced);
5611
5612 return (coding->result);
5613 }
5614
5615 /* Work buffer */
5616
5617 /* List of currently used working buffer. */
5618 Lisp_Object Vcode_conversion_work_buf_list;
5619
5620 /* A working buffer used by the top level conversion. */
5621 Lisp_Object Vcode_conversion_reused_work_buf;
5622
5623
5624 /* Return a working buffer that can be freely used by the following
5625 code conversion. MULTIBYTEP specifies the multibyteness of the
5626 buffer. */
5627
5628 Lisp_Object
5629 make_conversion_work_buffer (multibytep)
5630 int multibytep;
5631 {
5632 struct buffer *current = current_buffer;
5633 Lisp_Object buf;
5634
5635 if (NILP (Vcode_conversion_work_buf_list))
5636 {
5637 if (NILP (Vcode_conversion_reused_work_buf))
5638 Vcode_conversion_reused_work_buf
5639 = Fget_buffer_create (build_string (" *code-conversion-work*"));
5640 Vcode_conversion_work_buf_list
5641 = Fcons (Vcode_conversion_reused_work_buf, Qnil);
5642 }
5643 else
5644 {
5645 int depth = Flength (Vcode_conversion_work_buf_list);
5646 char str[128];
5647
5648 sprintf (str, " *code-conversion-work*<%d>", depth);
5649 Vcode_conversion_work_buf_list
5650 = Fcons (Fget_buffer_create (build_string (str)),
5651 Vcode_conversion_work_buf_list);
5652 }
5653
5654 buf = XCAR (Vcode_conversion_work_buf_list);
5655 set_buffer_internal (XBUFFER (buf));
5656 current_buffer->undo_list = Qt;
5657 Ferase_buffer ();
5658 Fset_buffer_multibyte (multibytep ? Qt : Qnil);
5659 set_buffer_internal (current);
5660 return buf;
5661 }
5662
5663 static struct coding_system *saved_coding;
5664
5665 Lisp_Object
5666 code_conversion_restore (info)
5667 Lisp_Object info;
5668 {
5669 int depth = Flength (Vcode_conversion_work_buf_list);
5670 Lisp_Object buf;
5671
5672 if (depth > 0)
5673 {
5674 buf = XCAR (Vcode_conversion_work_buf_list);
5675 Vcode_conversion_work_buf_list = XCDR (Vcode_conversion_work_buf_list);
5676 if (depth > 1 && !NILP (Fbuffer_live_p (buf)))
5677 Fkill_buffer (buf);
5678 }
5679
5680 if (saved_coding->dst_object == Qt
5681 && saved_coding->destination)
5682 xfree (saved_coding->destination);
5683
5684 return save_excursion_restore (info);
5685 }
5686
5687
5688 int
5689 decode_coding_gap (coding, chars, bytes)
5690 struct coding_system *coding;
5691 EMACS_INT chars, bytes;
5692 {
5693 int count = specpdl_ptr - specpdl;
5694
5695 saved_coding = coding;
5696 record_unwind_protect (code_conversion_restore, save_excursion_save ());
5697
5698 coding->src_object = Fcurrent_buffer ();
5699 coding->src_chars = chars;
5700 coding->src_bytes = bytes;
5701 coding->src_pos = -chars;
5702 coding->src_pos_byte = -bytes;
5703 coding->src_multibyte = chars < bytes;
5704 coding->dst_object = coding->src_object;
5705 coding->dst_pos = PT;
5706 coding->dst_pos_byte = PT_BYTE;
5707
5708 if (CODING_REQUIRE_DETECTION (coding))
5709 detect_coding (coding);
5710
5711 decode_coding (coding);
5712
5713 unbind_to (count, Qnil);
5714 return coding->result;
5715 }
5716
5717 int
5718 encode_coding_gap (coding, chars, bytes)
5719 struct coding_system *coding;
5720 EMACS_INT chars, bytes;
5721 {
5722 int count = specpdl_ptr - specpdl;
5723 Lisp_Object buffer;
5724
5725 saved_coding = coding;
5726 record_unwind_protect (code_conversion_restore, save_excursion_save ());
5727
5728 buffer = Fcurrent_buffer ();
5729 coding->src_object = buffer;
5730 coding->src_chars = chars;
5731 coding->src_bytes = bytes;
5732 coding->src_pos = -chars;
5733 coding->src_pos_byte = -bytes;
5734 coding->src_multibyte = chars < bytes;
5735 coding->dst_object = coding->src_object;
5736 coding->dst_pos = PT;
5737 coding->dst_pos_byte = PT_BYTE;
5738
5739 encode_coding (coding);
5740
5741 unbind_to (count, Qnil);
5742 return coding->result;
5743 }
5744
5745
5746 /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
5747 SRC_OBJECT into DST_OBJECT by coding context CODING.
5748
5749 SRC_OBJECT is a buffer, a string, or Qnil.
5750
5751 If it is a buffer, the text is at point of the buffer. FROM and TO
5752 are positions in the buffer.
5753
5754 If it is a string, the text is at the beginning of the string.
5755 FROM and TO are indices to the string.
5756
5757 If it is nil, the text is at coding->source. FROM and TO are
5758 indices to coding->source.
5759
5760 DST_OBJECT is a buffer, Qt, or Qnil.
5761
5762 If it is a buffer, the decoded text is inserted at point of the
5763 buffer. If the buffer is the same as SRC_OBJECT, the source text
5764 is deleted.
5765
5766 If it is Qt, a string is made from the decoded text, and
5767 set in CODING->dst_object.
5768
5769 If it is Qnil, the decoded text is stored at CODING->destination.
5770 The called must allocate CODING->dst_bytes bytes at
5771 CODING->destination by xmalloc. If the decoded text is longer than
5772 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
5773 */
5774
5775 void
5776 decode_coding_object (coding, src_object, from, from_byte, to, to_byte,
5777 dst_object)
5778 struct coding_system *coding;
5779 Lisp_Object src_object;
5780 EMACS_INT from, from_byte, to, to_byte;
5781 Lisp_Object dst_object;
5782 {
5783 int count = specpdl_ptr - specpdl;
5784 unsigned char *destination;
5785 EMACS_INT dst_bytes;
5786 EMACS_INT chars = to - from;
5787 EMACS_INT bytes = to_byte - from_byte;
5788 Lisp_Object attrs;
5789
5790 saved_coding = coding;
5791 record_unwind_protect (code_conversion_restore, save_excursion_save ());
5792
5793 if (NILP (dst_object))
5794 {
5795 destination = coding->destination;
5796 dst_bytes = coding->dst_bytes;
5797 }
5798
5799 coding->src_object = src_object;
5800 coding->src_chars = chars;
5801 coding->src_bytes = bytes;
5802 coding->src_multibyte = chars < bytes;
5803
5804 if (STRINGP (src_object))
5805 {
5806 coding->src_pos = from;
5807 coding->src_pos_byte = from_byte;
5808 }
5809 else if (BUFFERP (src_object))
5810 {
5811 set_buffer_internal (XBUFFER (src_object));
5812 if (from != GPT)
5813 move_gap_both (from, from_byte);
5814 if (EQ (src_object, dst_object))
5815 {
5816 TEMP_SET_PT_BOTH (from, from_byte);
5817 del_range_both (from, from_byte, to, to_byte, 1);
5818 coding->src_pos = -chars;
5819 coding->src_pos_byte = -bytes;
5820 }
5821 else
5822 {
5823 coding->src_pos = from;
5824 coding->src_pos_byte = from_byte;
5825 }
5826 }
5827
5828 if (CODING_REQUIRE_DETECTION (coding))
5829 detect_coding (coding);
5830 attrs = CODING_ID_ATTRS (coding->id);
5831
5832 if (! NILP (CODING_ATTR_POST_READ (attrs))
5833 || EQ (dst_object, Qt))
5834 {
5835 coding->dst_object = make_conversion_work_buffer (1);
5836 coding->dst_pos = BEG;
5837 coding->dst_pos_byte = BEG_BYTE;
5838 coding->dst_multibyte = 1;
5839 }
5840 else if (BUFFERP (dst_object))
5841 {
5842 coding->dst_object = dst_object;
5843 coding->dst_pos = BUF_PT (XBUFFER (dst_object));
5844 coding->dst_pos_byte = BUF_PT_BYTE (XBUFFER (dst_object));
5845 coding->dst_multibyte
5846 = ! NILP (XBUFFER (dst_object)->enable_multibyte_characters);
5847 }
5848 else
5849 {
5850 coding->dst_object = Qnil;
5851 coding->dst_multibyte = 1;
5852 }
5853
5854 decode_coding (coding);
5855
5856 if (BUFFERP (coding->dst_object))
5857 set_buffer_internal (XBUFFER (coding->dst_object));
5858
5859 if (! NILP (CODING_ATTR_POST_READ (attrs)))
5860 {
5861 struct gcpro gcpro1, gcpro2;
5862 EMACS_INT prev_Z = Z, prev_Z_BYTE = Z_BYTE;
5863 Lisp_Object val;
5864
5865 GCPRO2 (coding->src_object, coding->dst_object);
5866 val = call1 (CODING_ATTR_POST_READ (attrs),
5867 make_number (coding->produced_char));
5868 UNGCPRO;
5869 CHECK_NATNUM (val);
5870 coding->produced_char += Z - prev_Z;
5871 coding->produced += Z_BYTE - prev_Z_BYTE;
5872 }
5873
5874 if (EQ (dst_object, Qt))
5875 {
5876 coding->dst_object = Fbuffer_string ();
5877 }
5878 else if (NILP (dst_object) && BUFFERP (coding->dst_object))
5879 {
5880 set_buffer_internal (XBUFFER (coding->dst_object));
5881 if (dst_bytes < coding->produced)
5882 {
5883 destination
5884 = (unsigned char *) xrealloc (destination, coding->produced);
5885 if (! destination)
4626 { 5886 {
4627 if (*p == '\n') 5887 coding->result = CODING_RESULT_INSUFFICIENT_DST;
4628 *ptr-- = '\n', *ptr-- = '\r'; 5888 unbind_to (count, Qnil);
4629 else 5889 return;
4630 *ptr-- = *p;
4631 } 5890 }
4632 /* If carryover is recorded, cancel it because we don't 5891 if (BEGV < GPT && GPT < BEGV + coding->produced_char)
4633 convert CRLF anymore. */ 5892 move_gap_both (BEGV, BEGV_BYTE);
4634 if (coding->spec.ccl.cr_carryover) 5893 bcopy (BEGV_ADDR, destination, coding->produced);
4635 { 5894 coding->destination = destination;
4636 coding->spec.ccl.cr_carryover = 0; 5895 }
4637 coding->produced++; 5896 }
4638 coding->produced_char++; 5897
4639 pend++; 5898 unbind_to (count, Qnil);
4640 } 5899 }
4641 p = ptr = pend; 5900
4642 coding->eol_type = CODING_EOL_LF; 5901
4643 coding->symbol = saved_coding_symbol; 5902 void
4644 } 5903 encode_coding_object (coding, src_object, from, from_byte, to, to_byte,
4645 if (p < pend) 5904 dst_object)
4646 {
4647 /* As each two-byte sequence CRLF was converted to LF, (PEND
4648 - P) is the number of deleted characters. */
4649 coding->produced -= pend - p;
4650 coding->produced_char -= pend - p;
4651 }
4652 }
4653 else /* i.e. coding->eol_type == CODING_EOL_CR */
4654 {
4655 unsigned char *p = ptr;
4656
4657 for (; ptr < pend; ptr++)
4658 {
4659 if (*ptr == '\r')
4660 *ptr = '\n';
4661 else if (*ptr == '\n'
4662 && coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
4663 {
4664 for (; p < ptr; p++)
4665 {
4666 if (*p == '\n')
4667 *p = '\r';
4668 }
4669 ptr = pend;
4670 coding->eol_type = CODING_EOL_LF;
4671 coding->symbol = saved_coding_symbol;
4672 }
4673 }
4674 }
4675 }
4676
4677 /* See "GENERAL NOTES about `decode_coding_XXX ()' functions". Before
4678 decoding, it may detect coding system and format of end-of-line if
4679 those are not yet decided. The source should be unibyte, the
4680 result is multibyte if CODING->dst_multibyte is nonzero, else
4681 unibyte. */
4682
4683 int
4684 decode_coding (coding, source, destination, src_bytes, dst_bytes)
4685 struct coding_system *coding; 5905 struct coding_system *coding;
4686 unsigned char *source, *destination; 5906 Lisp_Object src_object;
4687 int src_bytes, dst_bytes; 5907 EMACS_INT from, from_byte, to, to_byte;
4688 { 5908 Lisp_Object dst_object;
4689 if (coding->type == coding_type_undecided) 5909 {
4690 detect_coding (coding, source, src_bytes); 5910 int count = specpdl_ptr - specpdl;
4691 5911 EMACS_INT chars = to - from;
4692 if (coding->eol_type == CODING_EOL_UNDECIDED 5912 EMACS_INT bytes = to_byte - from_byte;
4693 && coding->type != coding_type_ccl) 5913 Lisp_Object attrs;
4694 { 5914
4695 detect_eol (coding, source, src_bytes); 5915 saved_coding = coding;
4696 /* We had better recover the original eol format if we 5916 record_unwind_protect (code_conversion_restore, save_excursion_save ());
4697 encounter an inconsistent eol format while decoding. */ 5917
4698 coding->mode |= CODING_MODE_INHIBIT_INCONSISTENT_EOL; 5918 coding->src_object = src_object;
4699 } 5919 coding->src_chars = chars;
4700 5920 coding->src_bytes = bytes;
4701 coding->produced = coding->produced_char = 0; 5921 coding->src_multibyte = chars < bytes;
4702 coding->consumed = coding->consumed_char = 0; 5922
4703 coding->errors = 0; 5923 attrs = CODING_ID_ATTRS (coding->id);
4704 coding->result = CODING_FINISH_NORMAL; 5924
4705 5925 if (! NILP (CODING_ATTR_PRE_WRITE (attrs)))
4706 switch (coding->type) 5926 {
4707 { 5927 Lisp_Object val;
4708 case coding_type_sjis: 5928
4709 decode_coding_sjis_big5 (coding, source, destination, 5929 coding->src_object = make_conversion_work_buffer (coding->src_multibyte);
4710 src_bytes, dst_bytes, 1); 5930 set_buffer_internal (XBUFFER (coding->src_object));
4711 break; 5931 if (STRINGP (src_object))
4712 5932 insert_from_string (src_object, from, from_byte, chars, bytes, 0);
4713 case coding_type_iso2022: 5933 else if (BUFFERP (src_object))
4714 decode_coding_iso2022 (coding, source, destination, 5934 insert_from_buffer (XBUFFER (src_object), from, chars, 0);
4715 src_bytes, dst_bytes); 5935 else
4716 break; 5936 insert_1_both (coding->source + from, chars, bytes, 0, 0, 0);
4717 5937
4718 case coding_type_big5: 5938 if (EQ (src_object, dst_object))
4719 decode_coding_sjis_big5 (coding, source, destination, 5939 {
4720 src_bytes, dst_bytes, 0); 5940 set_buffer_internal (XBUFFER (src_object));
4721 break; 5941 del_range_both (from, from_byte, to, to_byte, 1);
4722 5942 set_buffer_internal (XBUFFER (coding->src_object));
4723 case coding_type_emacs_mule: 5943 }
4724 decode_coding_emacs_mule (coding, source, destination, 5944
4725 src_bytes, dst_bytes); 5945 val = call2 (CODING_ATTR_PRE_WRITE (attrs),
4726 break; 5946 make_number (1), make_number (chars));
4727 5947 CHECK_NATNUM (val);
4728 case coding_type_ccl: 5948 if (BEG != GPT)
4729 if (coding->spec.ccl.cr_carryover) 5949 move_gap_both (BEG, BEG_BYTE);
4730 { 5950 coding->src_chars = Z - BEG;
4731 /* Set the CR which is not processed by the previous call of 5951 coding->src_bytes = Z_BYTE - BEG_BYTE;
4732 decode_eol_post_ccl in DESTINATION. */ 5952 coding->src_pos = BEG;
4733 *destination = '\r'; 5953 coding->src_pos_byte = BEG_BYTE;
4734 coding->produced++; 5954 coding->src_multibyte = Z < Z_BYTE;
4735 coding->produced_char++; 5955 }
4736 dst_bytes--; 5956 else if (STRINGP (src_object))
4737 } 5957 {
4738 ccl_coding_driver (coding, source, 5958 coding->src_pos = from;
4739 destination + coding->spec.ccl.cr_carryover, 5959 coding->src_pos_byte = from_byte;
4740 src_bytes, dst_bytes, 0); 5960 }
4741 if (coding->eol_type != CODING_EOL_LF) 5961 else if (BUFFERP (src_object))
4742 decode_eol_post_ccl (coding, destination, coding->produced); 5962 {
4743 break; 5963 set_buffer_internal (XBUFFER (src_object));
4744 5964 if (from != GPT)
4745 default: 5965 move_gap_both (from, from_byte);
4746 decode_eol (coding, source, destination, src_bytes, dst_bytes); 5966 if (EQ (src_object, dst_object))
4747 } 5967 {
4748 5968 del_range_both (from, from_byte, to, to_byte, 1);
4749 if (coding->result == CODING_FINISH_INSUFFICIENT_SRC 5969 coding->src_pos = -chars;
4750 && coding->mode & CODING_MODE_LAST_BLOCK 5970 coding->src_pos_byte = -bytes;
4751 && coding->consumed == src_bytes) 5971 }
4752 coding->result = CODING_FINISH_NORMAL; 5972 else
4753 5973 {
4754 if (coding->mode & CODING_MODE_LAST_BLOCK 5974 coding->src_pos = from;
4755 && coding->result == CODING_FINISH_INSUFFICIENT_SRC) 5975 coding->src_pos_byte = from_byte;
4756 { 5976 }
4757 unsigned char *src = source + coding->consumed; 5977 }
4758 unsigned char *dst = destination + coding->produced; 5978
4759 5979 if (BUFFERP (dst_object))
4760 src_bytes -= coding->consumed; 5980 {
4761 coding->errors++; 5981 coding->dst_object = dst_object;
4762 if (COMPOSING_P (coding)) 5982 coding->dst_pos = BUF_PT (XBUFFER (dst_object));
4763 DECODE_COMPOSITION_END ('1'); 5983 coding->dst_pos_byte = BUF_PT_BYTE (XBUFFER (dst_object));
4764 while (src_bytes--) 5984 coding->dst_multibyte
4765 { 5985 = ! NILP (XBUFFER (dst_object)->enable_multibyte_characters);
4766 int c = *src++; 5986 }
4767 dst += CHAR_STRING (c, dst); 5987 else if (EQ (dst_object, Qt))
4768 coding->produced_char++; 5988 {
4769 } 5989 coding->dst_object = Qnil;
4770 coding->consumed = coding->consumed_char = src - source; 5990 coding->destination = (unsigned char *) xmalloc (coding->src_chars);
4771 coding->produced = dst - destination; 5991 coding->dst_bytes = coding->src_chars;
4772 coding->result = CODING_FINISH_NORMAL; 5992 coding->dst_multibyte = 0;
4773 }
4774
4775 if (!coding->dst_multibyte)
4776 {
4777 coding->produced = str_as_unibyte (destination, coding->produced);
4778 coding->produced_char = coding->produced;
4779 }
4780
4781 return coding->result;
4782 }
4783
4784 /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". The
4785 multibyteness of the source is CODING->src_multibyte, the
4786 multibyteness of the result is always unibyte. */
4787
4788 int
4789 encode_coding (coding, source, destination, src_bytes, dst_bytes)
4790 struct coding_system *coding;
4791 unsigned char *source, *destination;
4792 int src_bytes, dst_bytes;
4793 {
4794 coding->produced = coding->produced_char = 0;
4795 coding->consumed = coding->consumed_char = 0;
4796 coding->errors = 0;
4797 coding->result = CODING_FINISH_NORMAL;
4798
4799 switch (coding->type)
4800 {
4801 case coding_type_sjis:
4802 encode_coding_sjis_big5 (coding, source, destination,
4803 src_bytes, dst_bytes, 1);
4804 break;
4805
4806 case coding_type_iso2022:
4807 encode_coding_iso2022 (coding, source, destination,
4808 src_bytes, dst_bytes);
4809 break;
4810
4811 case coding_type_big5:
4812 encode_coding_sjis_big5 (coding, source, destination,
4813 src_bytes, dst_bytes, 0);
4814 break;
4815
4816 case coding_type_emacs_mule:
4817 encode_coding_emacs_mule (coding, source, destination,
4818 src_bytes, dst_bytes);
4819 break;
4820
4821 case coding_type_ccl:
4822 ccl_coding_driver (coding, source, destination,
4823 src_bytes, dst_bytes, 1);
4824 break;
4825
4826 default:
4827 encode_eol (coding, source, destination, src_bytes, dst_bytes);
4828 }
4829
4830 if (coding->mode & CODING_MODE_LAST_BLOCK
4831 && coding->result == CODING_FINISH_INSUFFICIENT_SRC)
4832 {
4833 unsigned char *src = source + coding->consumed;
4834 unsigned char *dst = destination + coding->produced;
4835
4836 if (coding->type == coding_type_iso2022)
4837 ENCODE_RESET_PLANE_AND_REGISTER;
4838 if (COMPOSING_P (coding))
4839 *dst++ = ISO_CODE_ESC, *dst++ = '1';
4840 if (coding->consumed < src_bytes)
4841 {
4842 int len = src_bytes - coding->consumed;
4843
4844 BCOPY_SHORT (src, dst, len);
4845 if (coding->src_multibyte)
4846 len = str_as_unibyte (dst, len);
4847 dst += len;
4848 coding->consumed = src_bytes;
4849 }
4850 coding->produced = coding->produced_char = dst - destination;
4851 coding->result = CODING_FINISH_NORMAL;
4852 }
4853
4854 if (coding->result == CODING_FINISH_INSUFFICIENT_SRC
4855 && coding->consumed == src_bytes)
4856 coding->result = CODING_FINISH_NORMAL;
4857
4858 return coding->result;
4859 }
4860
4861 /* Scan text in the region between *BEG and *END (byte positions),
4862 skip characters which we don't have to decode by coding system
4863 CODING at the head and tail, then set *BEG and *END to the region
4864 of the text we actually have to convert. The caller should move
4865 the gap out of the region in advance if the region is from a
4866 buffer.
4867
4868 If STR is not NULL, *BEG and *END are indices into STR. */
4869
4870 static void
4871 shrink_decoding_region (beg, end, coding, str)
4872 int *beg, *end;
4873 struct coding_system *coding;
4874 unsigned char *str;
4875 {
4876 unsigned char *begp_orig, *begp, *endp_orig, *endp, c;
4877 int eol_conversion;
4878 Lisp_Object translation_table;
4879
4880 if (coding->type == coding_type_ccl
4881 || coding->type == coding_type_undecided
4882 || coding->eol_type != CODING_EOL_LF
4883 || !NILP (coding->post_read_conversion)
4884 || coding->composing != COMPOSITION_DISABLED)
4885 {
4886 /* We can't skip any data. */
4887 return;
4888 }
4889 if (coding->type == coding_type_no_conversion
4890 || coding->type == coding_type_raw_text
4891 || coding->type == coding_type_emacs_mule)
4892 {
4893 /* We need no conversion, but don't have to skip any data here.
4894 Decoding routine handles them effectively anyway. */
4895 return;
4896 }
4897
4898 translation_table = coding->translation_table_for_decode;
4899 if (NILP (translation_table) && !NILP (Venable_character_translation))
4900 translation_table = Vstandard_translation_table_for_decode;
4901 if (CHAR_TABLE_P (translation_table))
4902 {
4903 int i;
4904 for (i = 0; i < 128; i++)
4905 if (!NILP (CHAR_TABLE_REF (translation_table, i)))
4906 break;
4907 if (i < 128)
4908 /* Some ASCII character should be translated. We give up
4909 shrinking. */
4910 return;
4911 }
4912
4913 if (coding->heading_ascii >= 0)
4914 /* Detection routine has already found how much we can skip at the
4915 head. */
4916 *beg += coding->heading_ascii;
4917
4918 if (str)
4919 {
4920 begp_orig = begp = str + *beg;
4921 endp_orig = endp = str + *end;
4922 } 5993 }
4923 else 5994 else
4924 { 5995 {
4925 begp_orig = begp = BYTE_POS_ADDR (*beg); 5996 coding->dst_object = Qnil;
4926 endp_orig = endp = begp + *end - *beg; 5997 coding->dst_multibyte = 0;
4927 } 5998 }
4928 5999
4929 eol_conversion = (coding->eol_type == CODING_EOL_CR 6000 encode_coding (coding);
4930 || coding->eol_type == CODING_EOL_CRLF); 6001
4931 6002 if (EQ (dst_object, Qt))
4932 switch (coding->type) 6003 {
4933 { 6004 if (BUFFERP (coding->dst_object))
4934 case coding_type_sjis: 6005 coding->dst_object = Fbuffer_string ();
4935 case coding_type_big5:
4936 /* We can skip all ASCII characters at the head. */
4937 if (coding->heading_ascii < 0)
4938 {
4939 if (eol_conversion)
4940 while (begp < endp && *begp < 0x80 && *begp != '\r') begp++;
4941 else
4942 while (begp < endp && *begp < 0x80) begp++;
4943 }
4944 /* We can skip all ASCII characters at the tail except for the
4945 second byte of SJIS or BIG5 code. */
4946 if (eol_conversion)
4947 while (begp < endp && endp[-1] < 0x80 && endp[-1] != '\r') endp--;
4948 else 6006 else
4949 while (begp < endp && endp[-1] < 0x80) endp--; 6007 {
4950 /* Do not consider LF as ascii if preceded by CR, since that 6008 coding->dst_object
4951 confuses eol decoding. */ 6009 = make_unibyte_string ((char *) coding->destination,
4952 if (begp < endp && endp < endp_orig && endp[-1] == '\r' && endp[0] == '\n') 6010 coding->produced);
4953 endp++; 6011 xfree (coding->destination);
4954 if (begp < endp && endp < endp_orig && endp[-1] >= 0x80) 6012 }
4955 endp++; 6013 }
4956 break; 6014
4957 6015 unbind_to (count, Qnil);
4958 case coding_type_iso2022: 6016 }
4959 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, 0) != CHARSET_ASCII) 6017
4960 /* We can't skip any data. */
4961 break;
4962 if (coding->heading_ascii < 0)
4963 {
4964 /* We can skip all ASCII characters at the head except for a
4965 few control codes. */
4966 while (begp < endp && (c = *begp) < 0x80
4967 && c != ISO_CODE_CR && c != ISO_CODE_SO
4968 && c != ISO_CODE_SI && c != ISO_CODE_ESC
4969 && (!eol_conversion || c != ISO_CODE_LF))
4970 begp++;
4971 }
4972 switch (coding->category_idx)
4973 {
4974 case CODING_CATEGORY_IDX_ISO_8_1:
4975 case CODING_CATEGORY_IDX_ISO_8_2:
4976 /* We can skip all ASCII characters at the tail. */
4977 if (eol_conversion)
4978 while (begp < endp && (c = endp[-1]) < 0x80 && c != '\r') endp--;
4979 else
4980 while (begp < endp && endp[-1] < 0x80) endp--;
4981 /* Do not consider LF as ascii if preceded by CR, since that
4982 confuses eol decoding. */
4983 if (begp < endp && endp < endp_orig && endp[-1] == '\r' && endp[0] == '\n')
4984 endp++;
4985 break;
4986
4987 case CODING_CATEGORY_IDX_ISO_7:
4988 case CODING_CATEGORY_IDX_ISO_7_TIGHT:
4989 {
4990 /* We can skip all characters at the tail except for 8-bit
4991 codes and ESC and the following 2-byte at the tail. */
4992 unsigned char *eight_bit = NULL;
4993
4994 if (eol_conversion)
4995 while (begp < endp
4996 && (c = endp[-1]) != ISO_CODE_ESC && c != '\r')
4997 {
4998 if (!eight_bit && c & 0x80) eight_bit = endp;
4999 endp--;
5000 }
5001 else
5002 while (begp < endp
5003 && (c = endp[-1]) != ISO_CODE_ESC)
5004 {
5005 if (!eight_bit && c & 0x80) eight_bit = endp;
5006 endp--;
5007 }
5008 /* Do not consider LF as ascii if preceded by CR, since that
5009 confuses eol decoding. */
5010 if (begp < endp && endp < endp_orig
5011 && endp[-1] == '\r' && endp[0] == '\n')
5012 endp++;
5013 if (begp < endp && endp[-1] == ISO_CODE_ESC)
5014 {
5015 if (endp + 1 < endp_orig && end[0] == '(' && end[1] == 'B')
5016 /* This is an ASCII designation sequence. We can
5017 surely skip the tail. But, if we have
5018 encountered an 8-bit code, skip only the codes
5019 after that. */
5020 endp = eight_bit ? eight_bit : endp + 2;
5021 else
5022 /* Hmmm, we can't skip the tail. */
5023 endp = endp_orig;
5024 }
5025 else if (eight_bit)
5026 endp = eight_bit;
5027 }
5028 }
5029 break;
5030
5031 default:
5032 abort ();
5033 }
5034 *beg += begp - begp_orig;
5035 *end += endp - endp_orig;
5036 return;
5037 }
5038
5039 /* Like shrink_decoding_region but for encoding. */
5040
5041 static void
5042 shrink_encoding_region (beg, end, coding, str)
5043 int *beg, *end;
5044 struct coding_system *coding;
5045 unsigned char *str;
5046 {
5047 unsigned char *begp_orig, *begp, *endp_orig, *endp;
5048 int eol_conversion;
5049 Lisp_Object translation_table;
5050
5051 if (coding->type == coding_type_ccl
5052 || coding->eol_type == CODING_EOL_CRLF
5053 || coding->eol_type == CODING_EOL_CR
5054 || (coding->cmp_data && coding->cmp_data->used > 0))
5055 {
5056 /* We can't skip any data. */
5057 return;
5058 }
5059 if (coding->type == coding_type_no_conversion
5060 || coding->type == coding_type_raw_text
5061 || coding->type == coding_type_emacs_mule
5062 || coding->type == coding_type_undecided)
5063 {
5064 /* We need no conversion, but don't have to skip any data here.
5065 Encoding routine handles them effectively anyway. */
5066 return;
5067 }
5068
5069 translation_table = coding->translation_table_for_encode;
5070 if (NILP (translation_table) && !NILP (Venable_character_translation))
5071 translation_table = Vstandard_translation_table_for_encode;
5072 if (CHAR_TABLE_P (translation_table))
5073 {
5074 int i;
5075 for (i = 0; i < 128; i++)
5076 if (!NILP (CHAR_TABLE_REF (translation_table, i)))
5077 break;
5078 if (i < 128)
5079 /* Some ASCII character should be translated. We give up
5080 shrinking. */
5081 return;
5082 }
5083
5084 if (str)
5085 {
5086 begp_orig = begp = str + *beg;
5087 endp_orig = endp = str + *end;
5088 }
5089 else
5090 {
5091 begp_orig = begp = BYTE_POS_ADDR (*beg);
5092 endp_orig = endp = begp + *end - *beg;
5093 }
5094
5095 eol_conversion = (coding->eol_type == CODING_EOL_CR
5096 || coding->eol_type == CODING_EOL_CRLF);
5097
5098 /* Here, we don't have to check coding->pre_write_conversion because
5099 the caller is expected to have handled it already. */
5100 switch (coding->type)
5101 {
5102 case coding_type_iso2022:
5103 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, 0) != CHARSET_ASCII)
5104 /* We can't skip any data. */
5105 break;
5106 if (coding->flags & CODING_FLAG_ISO_DESIGNATE_AT_BOL)
5107 {
5108 unsigned char *bol = begp;
5109 while (begp < endp && *begp < 0x80)
5110 {
5111 begp++;
5112 if (begp[-1] == '\n')
5113 bol = begp;
5114 }
5115 begp = bol;
5116 goto label_skip_tail;
5117 }
5118 /* fall down ... */
5119
5120 case coding_type_sjis:
5121 case coding_type_big5:
5122 /* We can skip all ASCII characters at the head and tail. */
5123 if (eol_conversion)
5124 while (begp < endp && *begp < 0x80 && *begp != '\n') begp++;
5125 else
5126 while (begp < endp && *begp < 0x80) begp++;
5127 label_skip_tail:
5128 if (eol_conversion)
5129 while (begp < endp && endp[-1] < 0x80 && endp[-1] != '\n') endp--;
5130 else
5131 while (begp < endp && *(endp - 1) < 0x80) endp--;
5132 break;
5133
5134 default:
5135 abort ();
5136 }
5137
5138 *beg += begp - begp_orig;
5139 *end += endp - endp_orig;
5140 return;
5141 }
5142
5143 /* As shrinking conversion region requires some overhead, we don't try
5144 shrinking if the length of conversion region is less than this
5145 value. */
5146 static int shrink_conversion_region_threshhold = 1024;
5147
5148 #define SHRINK_CONVERSION_REGION(beg, end, coding, str, encodep) \
5149 do { \
5150 if (*(end) - *(beg) > shrink_conversion_region_threshhold) \
5151 { \
5152 if (encodep) shrink_encoding_region (beg, end, coding, str); \
5153 else shrink_decoding_region (beg, end, coding, str); \
5154 } \
5155 } while (0)
5156
5157 static Lisp_Object
5158 code_convert_region_unwind (dummy)
5159 Lisp_Object dummy;
5160 {
5161 inhibit_pre_post_conversion = 0;
5162 return Qnil;
5163 }
5164
5165 /* Store information about all compositions in the range FROM and TO
5166 of OBJ in memory blocks pointed by CODING->cmp_data. OBJ is a
5167 buffer or a string, defaults to the current buffer. */
5168
5169 void
5170 coding_save_composition (coding, from, to, obj)
5171 struct coding_system *coding;
5172 int from, to;
5173 Lisp_Object obj;
5174 {
5175 Lisp_Object prop;
5176 int start, end;
5177
5178 if (coding->composing == COMPOSITION_DISABLED)
5179 return;
5180 if (!coding->cmp_data)
5181 coding_allocate_composition_data (coding, from);
5182 if (!find_composition (from, to, &start, &end, &prop, obj)
5183 || end > to)
5184 return;
5185 if (start < from
5186 && (!find_composition (end, to, &start, &end, &prop, obj)
5187 || end > to))
5188 return;
5189 coding->composing = COMPOSITION_NO;
5190 do
5191 {
5192 if (COMPOSITION_VALID_P (start, end, prop))
5193 {
5194 enum composition_method method = COMPOSITION_METHOD (prop);
5195 if (coding->cmp_data->used + COMPOSITION_DATA_MAX_BUNCH_LENGTH
5196 >= COMPOSITION_DATA_SIZE)
5197 coding_allocate_composition_data (coding, from);
5198 /* For relative composition, we remember start and end
5199 positions, for the other compositions, we also remember
5200 components. */
5201 CODING_ADD_COMPOSITION_START (coding, start - from, method);
5202 if (method != COMPOSITION_RELATIVE)
5203 {
5204 /* We must store a*/
5205 Lisp_Object val, ch;
5206
5207 val = COMPOSITION_COMPONENTS (prop);
5208 if (CONSP (val))
5209 while (CONSP (val))
5210 {
5211 ch = XCAR (val), val = XCDR (val);
5212 CODING_ADD_COMPOSITION_COMPONENT (coding, XINT (ch));
5213 }
5214 else if (VECTORP (val) || STRINGP (val))
5215 {
5216 int len = (VECTORP (val)
5217 ? XVECTOR (val)->size : XSTRING (val)->size);
5218 int i;
5219 for (i = 0; i < len; i++)
5220 {
5221 ch = (STRINGP (val)
5222 ? Faref (val, make_number (i))
5223 : XVECTOR (val)->contents[i]);
5224 CODING_ADD_COMPOSITION_COMPONENT (coding, XINT (ch));
5225 }
5226 }
5227 else /* INTEGERP (val) */
5228 CODING_ADD_COMPOSITION_COMPONENT (coding, XINT (val));
5229 }
5230 CODING_ADD_COMPOSITION_END (coding, end - from);
5231 }
5232 start = end;
5233 }
5234 while (start < to
5235 && find_composition (start, to, &start, &end, &prop, obj)
5236 && end <= to);
5237
5238 /* Make coding->cmp_data point to the first memory block. */
5239 while (coding->cmp_data->prev)
5240 coding->cmp_data = coding->cmp_data->prev;
5241 coding->cmp_data_start = 0;
5242 }
5243
5244 /* Reflect the saved information about compositions to OBJ.
5245 CODING->cmp_data points to a memory block for the information. OBJ
5246 is a buffer or a string, defaults to the current buffer. */
5247
5248 void
5249 coding_restore_composition (coding, obj)
5250 struct coding_system *coding;
5251 Lisp_Object obj;
5252 {
5253 struct composition_data *cmp_data = coding->cmp_data;
5254
5255 if (!cmp_data)
5256 return;
5257
5258 while (cmp_data->prev)
5259 cmp_data = cmp_data->prev;
5260
5261 while (cmp_data)
5262 {
5263 int i;
5264
5265 for (i = 0; i < cmp_data->used && cmp_data->data[i] > 0;
5266 i += cmp_data->data[i])
5267 {
5268 int *data = cmp_data->data + i;
5269 enum composition_method method = (enum composition_method) data[3];
5270 Lisp_Object components;
5271
5272 if (method == COMPOSITION_RELATIVE)
5273 components = Qnil;
5274 else
5275 {
5276 int len = data[0] - 4, j;
5277 Lisp_Object args[MAX_COMPOSITION_COMPONENTS * 2 - 1];
5278
5279 for (j = 0; j < len; j++)
5280 args[j] = make_number (data[4 + j]);
5281 components = (method == COMPOSITION_WITH_ALTCHARS
5282 ? Fstring (len, args) : Fvector (len, args));
5283 }
5284 compose_text (data[1], data[2], components, Qnil, obj);
5285 }
5286 cmp_data = cmp_data->next;
5287 }
5288 }
5289
5290 /* Decode (if ENCODEP is zero) or encode (if ENCODEP is nonzero) the
5291 text from FROM to TO (byte positions are FROM_BYTE and TO_BYTE) by
5292 coding system CODING, and return the status code of code conversion
5293 (currently, this value has no meaning).
5294
5295 How many characters (and bytes) are converted to how many
5296 characters (and bytes) are recorded in members of the structure
5297 CODING.
5298
5299 If REPLACE is nonzero, we do various things as if the original text
5300 is deleted and a new text is inserted. See the comments in
5301 replace_range (insdel.c) to know what we are doing.
5302
5303 If REPLACE is zero, it is assumed that the source text is unibyte.
5304 Otherwise, it is assumed that the source text is multibyte. */
5305
5306 int
5307 code_convert_region (from, from_byte, to, to_byte, coding, encodep, replace)
5308 int from, from_byte, to, to_byte, encodep, replace;
5309 struct coding_system *coding;
5310 {
5311 int len = to - from, len_byte = to_byte - from_byte;
5312 int nchars_del = 0, nbytes_del = 0;
5313 int require, inserted, inserted_byte;
5314 int head_skip, tail_skip, total_skip = 0;
5315 Lisp_Object saved_coding_symbol;
5316 int first = 1;
5317 unsigned char *src, *dst;
5318 Lisp_Object deletion;
5319 int orig_point = PT, orig_len = len;
5320 int prev_Z;
5321 int multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5322
5323 deletion = Qnil;
5324 saved_coding_symbol = coding->symbol;
5325
5326 if (from < PT && PT < to)
5327 {
5328 TEMP_SET_PT_BOTH (from, from_byte);
5329 orig_point = from;
5330 }
5331
5332 if (replace)
5333 {
5334 int saved_from = from;
5335 int saved_inhibit_modification_hooks;
5336
5337 prepare_to_modify_buffer (from, to, &from);
5338 if (saved_from != from)
5339 {
5340 to = from + len;
5341 from_byte = CHAR_TO_BYTE (from), to_byte = CHAR_TO_BYTE (to);
5342 len_byte = to_byte - from_byte;
5343 }
5344
5345 /* The code conversion routine can not preserve text properties
5346 for now. So, we must remove all text properties in the
5347 region. Here, we must suppress all modification hooks. */
5348 saved_inhibit_modification_hooks = inhibit_modification_hooks;
5349 inhibit_modification_hooks = 1;
5350 Fset_text_properties (make_number (from), make_number (to), Qnil, Qnil);
5351 inhibit_modification_hooks = saved_inhibit_modification_hooks;
5352 }
5353
5354 if (! encodep && CODING_REQUIRE_DETECTION (coding))
5355 {
5356 /* We must detect encoding of text and eol format. */
5357
5358 if (from < GPT && to > GPT)
5359 move_gap_both (from, from_byte);
5360 if (coding->type == coding_type_undecided)
5361 {
5362 detect_coding (coding, BYTE_POS_ADDR (from_byte), len_byte);
5363 if (coding->type == coding_type_undecided)
5364 {
5365 /* It seems that the text contains only ASCII, but we
5366 should not leave it undecided because the deeper
5367 decoding routine (decode_coding) tries to detect the
5368 encodings again in vain. */
5369 coding->type = coding_type_emacs_mule;
5370 coding->category_idx = CODING_CATEGORY_IDX_EMACS_MULE;
5371 /* As emacs-mule decoder will handle composition, we
5372 need this setting to allocate coding->cmp_data
5373 later. */
5374 coding->composing = COMPOSITION_NO;
5375 }
5376 }
5377 if (coding->eol_type == CODING_EOL_UNDECIDED
5378 && coding->type != coding_type_ccl)
5379 {
5380 detect_eol (coding, BYTE_POS_ADDR (from_byte), len_byte);
5381 if (coding->eol_type == CODING_EOL_UNDECIDED)
5382 coding->eol_type = CODING_EOL_LF;
5383 /* We had better recover the original eol format if we
5384 encounter an inconsistent eol format while decoding. */
5385 coding->mode |= CODING_MODE_INHIBIT_INCONSISTENT_EOL;
5386 }
5387 }
5388
5389 /* Now we convert the text. */
5390
5391 /* For encoding, we must process pre-write-conversion in advance. */
5392 if (! inhibit_pre_post_conversion
5393 && encodep
5394 && SYMBOLP (coding->pre_write_conversion)
5395 && ! NILP (Ffboundp (coding->pre_write_conversion)))
5396 {
5397 /* The function in pre-write-conversion may put a new text in a
5398 new buffer. */
5399 struct buffer *prev = current_buffer;
5400 Lisp_Object new;
5401
5402 record_unwind_protect (code_convert_region_unwind, Qnil);
5403 /* We should not call any more pre-write/post-read-conversion
5404 functions while this pre-write-conversion is running. */
5405 inhibit_pre_post_conversion = 1;
5406 call2 (coding->pre_write_conversion,
5407 make_number (from), make_number (to));
5408 inhibit_pre_post_conversion = 0;
5409 /* Discard the unwind protect. */
5410 specpdl_ptr--;
5411
5412 if (current_buffer != prev)
5413 {
5414 len = ZV - BEGV;
5415 new = Fcurrent_buffer ();
5416 set_buffer_internal_1 (prev);
5417 del_range_2 (from, from_byte, to, to_byte, 0);
5418 TEMP_SET_PT_BOTH (from, from_byte);
5419 insert_from_buffer (XBUFFER (new), 1, len, 0);
5420 Fkill_buffer (new);
5421 if (orig_point >= to)
5422 orig_point += len - orig_len;
5423 else if (orig_point > from)
5424 orig_point = from;
5425 orig_len = len;
5426 to = from + len;
5427 from_byte = CHAR_TO_BYTE (from);
5428 to_byte = CHAR_TO_BYTE (to);
5429 len_byte = to_byte - from_byte;
5430 TEMP_SET_PT_BOTH (from, from_byte);
5431 }
5432 }
5433
5434 if (replace)
5435 {
5436 if (! EQ (current_buffer->undo_list, Qt))
5437 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
5438 else
5439 {
5440 nchars_del = to - from;
5441 nbytes_del = to_byte - from_byte;
5442 }
5443 }
5444
5445 if (coding->composing != COMPOSITION_DISABLED)
5446 {
5447 if (encodep)
5448 coding_save_composition (coding, from, to, Fcurrent_buffer ());
5449 else
5450 coding_allocate_composition_data (coding, from);
5451 }
5452
5453 /* Try to skip the heading and tailing ASCIIs. */
5454 if (coding->type != coding_type_ccl)
5455 {
5456 int from_byte_orig = from_byte, to_byte_orig = to_byte;
5457
5458 if (from < GPT && GPT < to)
5459 move_gap_both (from, from_byte);
5460 SHRINK_CONVERSION_REGION (&from_byte, &to_byte, coding, NULL, encodep);
5461 if (from_byte == to_byte
5462 && (encodep || NILP (coding->post_read_conversion))
5463 && ! CODING_REQUIRE_FLUSHING (coding))
5464 {
5465 coding->produced = len_byte;
5466 coding->produced_char = len;
5467 if (!replace)
5468 /* We must record and adjust for this new text now. */
5469 adjust_after_insert (from, from_byte_orig, to, to_byte_orig, len);
5470 return 0;
5471 }
5472
5473 head_skip = from_byte - from_byte_orig;
5474 tail_skip = to_byte_orig - to_byte;
5475 total_skip = head_skip + tail_skip;
5476 from += head_skip;
5477 to -= tail_skip;
5478 len -= total_skip; len_byte -= total_skip;
5479 }
5480
5481 /* For conversion, we must put the gap before the text in addition to
5482 making the gap larger for efficient decoding. The required gap
5483 size starts from 2000 which is the magic number used in make_gap.
5484 But, after one batch of conversion, it will be incremented if we
5485 find that it is not enough . */
5486 require = 2000;
5487
5488 if (GAP_SIZE < require)
5489 make_gap (require - GAP_SIZE);
5490 move_gap_both (from, from_byte);
5491
5492 inserted = inserted_byte = 0;
5493
5494 GAP_SIZE += len_byte;
5495 ZV -= len;
5496 Z -= len;
5497 ZV_BYTE -= len_byte;
5498 Z_BYTE -= len_byte;
5499
5500 if (GPT - BEG < BEG_UNCHANGED)
5501 BEG_UNCHANGED = GPT - BEG;
5502 if (Z - GPT < END_UNCHANGED)
5503 END_UNCHANGED = Z - GPT;
5504
5505 if (!encodep && coding->src_multibyte)
5506 {
5507 /* Decoding routines expects that the source text is unibyte.
5508 We must convert 8-bit characters of multibyte form to
5509 unibyte. */
5510 int len_byte_orig = len_byte;
5511 len_byte = str_as_unibyte (GAP_END_ADDR - len_byte, len_byte);
5512 if (len_byte < len_byte_orig)
5513 safe_bcopy (GAP_END_ADDR - len_byte_orig, GAP_END_ADDR - len_byte,
5514 len_byte);
5515 coding->src_multibyte = 0;
5516 }
5517
5518 for (;;)
5519 {
5520 int result;
5521
5522 /* The buffer memory is now:
5523 +--------+converted-text+---------+-------original-text-------+---+
5524 |<-from->|<--inserted-->|---------|<--------len_byte--------->|---|
5525 |<---------------------- GAP ----------------------->| */
5526 src = GAP_END_ADDR - len_byte;
5527 dst = GPT_ADDR + inserted_byte;
5528
5529 if (encodep)
5530 result = encode_coding (coding, src, dst, len_byte, 0);
5531 else
5532 {
5533 if (coding->composing != COMPOSITION_DISABLED)
5534 coding->cmp_data->char_offset = from + inserted;
5535 result = decode_coding (coding, src, dst, len_byte, 0);
5536 }
5537
5538 /* The buffer memory is now:
5539 +--------+-------converted-text----+--+------original-text----+---+
5540 |<-from->|<-inserted->|<-produced->|--|<-(len_byte-consumed)->|---|
5541 |<---------------------- GAP ----------------------->| */
5542
5543 inserted += coding->produced_char;
5544 inserted_byte += coding->produced;
5545 len_byte -= coding->consumed;
5546
5547 if (result == CODING_FINISH_INSUFFICIENT_CMP)
5548 {
5549 coding_allocate_composition_data (coding, from + inserted);
5550 continue;
5551 }
5552
5553 src += coding->consumed;
5554 dst += coding->produced;
5555
5556 if (result == CODING_FINISH_NORMAL)
5557 {
5558 src += len_byte;
5559 break;
5560 }
5561 if (! encodep && result == CODING_FINISH_INCONSISTENT_EOL)
5562 {
5563 unsigned char *pend = dst, *p = pend - inserted_byte;
5564 Lisp_Object eol_type;
5565
5566 /* Encode LFs back to the original eol format (CR or CRLF). */
5567 if (coding->eol_type == CODING_EOL_CR)
5568 {
5569 while (p < pend) if (*p++ == '\n') p[-1] = '\r';
5570 }
5571 else
5572 {
5573 int count = 0;
5574
5575 while (p < pend) if (*p++ == '\n') count++;
5576 if (src - dst < count)
5577 {
5578 /* We don't have sufficient room for encoding LFs
5579 back to CRLF. We must record converted and
5580 not-yet-converted text back to the buffer
5581 content, enlarge the gap, then record them out of
5582 the buffer contents again. */
5583 int add = len_byte + inserted_byte;
5584
5585 GAP_SIZE -= add;
5586 ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add;
5587 GPT += inserted_byte; GPT_BYTE += inserted_byte;
5588 make_gap (count - GAP_SIZE);
5589 GAP_SIZE += add;
5590 ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add;
5591 GPT -= inserted_byte; GPT_BYTE -= inserted_byte;
5592 /* Don't forget to update SRC, DST, and PEND. */
5593 src = GAP_END_ADDR - len_byte;
5594 dst = GPT_ADDR + inserted_byte;
5595 pend = dst;
5596 }
5597 inserted += count;
5598 inserted_byte += count;
5599 coding->produced += count;
5600 p = dst = pend + count;
5601 while (count)
5602 {
5603 *--p = *--pend;
5604 if (*p == '\n') count--, *--p = '\r';
5605 }
5606 }
5607
5608 /* Suppress eol-format conversion in the further conversion. */
5609 coding->eol_type = CODING_EOL_LF;
5610
5611 /* Set the coding system symbol to that for Unix-like EOL. */
5612 eol_type = Fget (saved_coding_symbol, Qeol_type);
5613 if (VECTORP (eol_type)
5614 && XVECTOR (eol_type)->size == 3
5615 && SYMBOLP (XVECTOR (eol_type)->contents[CODING_EOL_LF]))
5616 coding->symbol = XVECTOR (eol_type)->contents[CODING_EOL_LF];
5617 else
5618 coding->symbol = saved_coding_symbol;
5619
5620 continue;
5621 }
5622 if (len_byte <= 0)
5623 {
5624 if (coding->type != coding_type_ccl
5625 || coding->mode & CODING_MODE_LAST_BLOCK)
5626 break;
5627 coding->mode |= CODING_MODE_LAST_BLOCK;
5628 continue;
5629 }
5630 if (result == CODING_FINISH_INSUFFICIENT_SRC)
5631 {
5632 /* The source text ends in invalid codes. Let's just
5633 make them valid buffer contents, and finish conversion. */
5634 if (multibyte_p)
5635 {
5636 unsigned char *start = dst;
5637
5638 inserted += len_byte;
5639 while (len_byte--)
5640 {
5641 int c = *src++;
5642 dst += CHAR_STRING (c, dst);
5643 }
5644
5645 inserted_byte += dst - start;
5646 }
5647 else
5648 {
5649 inserted += len_byte;
5650 inserted_byte += len_byte;
5651 while (len_byte--)
5652 *dst++ = *src++;
5653 }
5654 break;
5655 }
5656 if (result == CODING_FINISH_INTERRUPT)
5657 {
5658 /* The conversion procedure was interrupted by a user. */
5659 break;
5660 }
5661 /* Now RESULT == CODING_FINISH_INSUFFICIENT_DST */
5662 if (coding->consumed < 1)
5663 {
5664 /* It's quite strange to require more memory without
5665 consuming any bytes. Perhaps CCL program bug. */
5666 break;
5667 }
5668 if (first)
5669 {
5670 /* We have just done the first batch of conversion which was
5671 stopped because of insufficient gap. Let's reconsider the
5672 required gap size (i.e. SRT - DST) now.
5673
5674 We have converted ORIG bytes (== coding->consumed) into
5675 NEW bytes (coding->produced). To convert the remaining
5676 LEN bytes, we may need REQUIRE bytes of gap, where:
5677 REQUIRE + LEN_BYTE = LEN_BYTE * (NEW / ORIG)
5678 REQUIRE = LEN_BYTE * (NEW - ORIG) / ORIG
5679 Here, we are sure that NEW >= ORIG. */
5680 float ratio = coding->produced - coding->consumed;
5681 ratio /= coding->consumed;
5682 require = len_byte * ratio;
5683 first = 0;
5684 }
5685 if ((src - dst) < (require + 2000))
5686 {
5687 /* See the comment above the previous call of make_gap. */
5688 int add = len_byte + inserted_byte;
5689
5690 GAP_SIZE -= add;
5691 ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add;
5692 GPT += inserted_byte; GPT_BYTE += inserted_byte;
5693 make_gap (require + 2000);
5694 GAP_SIZE += add;
5695 ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add;
5696 GPT -= inserted_byte; GPT_BYTE -= inserted_byte;
5697 }
5698 }
5699 if (src - dst > 0) *dst = 0; /* Put an anchor. */
5700
5701 if (encodep && coding->dst_multibyte)
5702 {
5703 /* The output is unibyte. We must convert 8-bit characters to
5704 multibyte form. */
5705 if (inserted_byte * 2 > GAP_SIZE)
5706 {
5707 GAP_SIZE -= inserted_byte;
5708 ZV += inserted_byte; Z += inserted_byte;
5709 ZV_BYTE += inserted_byte; Z_BYTE += inserted_byte;
5710 GPT += inserted_byte; GPT_BYTE += inserted_byte;
5711 make_gap (inserted_byte - GAP_SIZE);
5712 GAP_SIZE += inserted_byte;
5713 ZV -= inserted_byte; Z -= inserted_byte;
5714 ZV_BYTE -= inserted_byte; Z_BYTE -= inserted_byte;
5715 GPT -= inserted_byte; GPT_BYTE -= inserted_byte;
5716 }
5717 inserted_byte = str_to_multibyte (GPT_ADDR, GAP_SIZE, inserted_byte);
5718 }
5719
5720 /* If we shrank the conversion area, adjust it now. */
5721 if (total_skip > 0)
5722 {
5723 if (tail_skip > 0)
5724 safe_bcopy (GAP_END_ADDR, GPT_ADDR + inserted_byte, tail_skip);
5725 inserted += total_skip; inserted_byte += total_skip;
5726 GAP_SIZE += total_skip;
5727 GPT -= head_skip; GPT_BYTE -= head_skip;
5728 ZV -= total_skip; ZV_BYTE -= total_skip;
5729 Z -= total_skip; Z_BYTE -= total_skip;
5730 from -= head_skip; from_byte -= head_skip;
5731 to += tail_skip; to_byte += tail_skip;
5732 }
5733
5734 prev_Z = Z;
5735 if (! EQ (current_buffer->undo_list, Qt))
5736 adjust_after_replace (from, from_byte, deletion, inserted, inserted_byte);
5737 else
5738 adjust_after_replace_noundo (from, from_byte, nchars_del, nbytes_del,
5739 inserted, inserted_byte);
5740 inserted = Z - prev_Z;
5741
5742 if (!encodep && coding->cmp_data && coding->cmp_data->used)
5743 coding_restore_composition (coding, Fcurrent_buffer ());
5744 coding_free_composition_data (coding);
5745
5746 if (! inhibit_pre_post_conversion
5747 && ! encodep && ! NILP (coding->post_read_conversion))
5748 {
5749 Lisp_Object val;
5750
5751 if (from != PT)
5752 TEMP_SET_PT_BOTH (from, from_byte);
5753 prev_Z = Z;
5754 record_unwind_protect (code_convert_region_unwind, Qnil);
5755 /* We should not call any more pre-write/post-read-conversion
5756 functions while this post-read-conversion is running. */
5757 inhibit_pre_post_conversion = 1;
5758 val = call1 (coding->post_read_conversion, make_number (inserted));
5759 inhibit_pre_post_conversion = 0;
5760 /* Discard the unwind protect. */
5761 specpdl_ptr--;
5762 CHECK_NUMBER (val);
5763 inserted += Z - prev_Z;
5764 }
5765
5766 if (orig_point >= from)
5767 {
5768 if (orig_point >= from + orig_len)
5769 orig_point += inserted - orig_len;
5770 else
5771 orig_point = from;
5772 TEMP_SET_PT (orig_point);
5773 }
5774
5775 if (replace)
5776 {
5777 signal_after_change (from, to - from, inserted);
5778 update_compositions (from, from + inserted, CHECK_BORDER);
5779 }
5780
5781 {
5782 coding->consumed = to_byte - from_byte;
5783 coding->consumed_char = to - from;
5784 coding->produced = inserted_byte;
5785 coding->produced_char = inserted;
5786 }
5787
5788 return 0;
5789 }
5790 6018
5791 Lisp_Object 6019 Lisp_Object
5792 run_pre_post_conversion_on_str (str, coding, encodep) 6020 preferred_coding_system ()
5793 Lisp_Object str; 6021 {
5794 struct coding_system *coding; 6022 int id = coding_categories[coding_priorities[0]].id;
5795 int encodep; 6023
5796 { 6024 return CODING_ID_NAME (id);
5797 int count = specpdl_ptr - specpdl;
5798 struct gcpro gcpro1;
5799 int multibyte = STRING_MULTIBYTE (str);
5800
5801 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
5802 record_unwind_protect (code_convert_region_unwind, Qnil);
5803 GCPRO1 (str);
5804 temp_output_buffer_setup (" *code-converting-work*");
5805 set_buffer_internal (XBUFFER (Vstandard_output));
5806 /* We must insert the contents of STR as is without
5807 unibyte<->multibyte conversion. For that, we adjust the
5808 multibyteness of the working buffer to that of STR. */
5809 Ferase_buffer ();
5810 current_buffer->enable_multibyte_characters = multibyte ? Qt : Qnil;
5811 insert_from_string (str, 0, 0,
5812 XSTRING (str)->size, STRING_BYTES (XSTRING (str)), 0);
5813 UNGCPRO;
5814 inhibit_pre_post_conversion = 1;
5815 if (encodep)
5816 call2 (coding->pre_write_conversion, make_number (BEG), make_number (Z));
5817 else
5818 {
5819 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5820 call1 (coding->post_read_conversion, make_number (Z - BEG));
5821 }
5822 inhibit_pre_post_conversion = 0;
5823 str = make_buffer_string (BEG, Z, 1);
5824 return unbind_to (count, str);
5825 }
5826
5827 Lisp_Object
5828 decode_coding_string (str, coding, nocopy)
5829 Lisp_Object str;
5830 struct coding_system *coding;
5831 int nocopy;
5832 {
5833 int len;
5834 struct conversion_buffer buf;
5835 int from, to_byte;
5836 Lisp_Object saved_coding_symbol;
5837 int result;
5838 int require_decoding;
5839 int shrinked_bytes = 0;
5840 Lisp_Object newstr;
5841 int consumed, consumed_char, produced, produced_char;
5842
5843 from = 0;
5844 to_byte = STRING_BYTES (XSTRING (str));
5845
5846 saved_coding_symbol = coding->symbol;
5847 coding->src_multibyte = STRING_MULTIBYTE (str);
5848 coding->dst_multibyte = 1;
5849 if (CODING_REQUIRE_DETECTION (coding))
5850 {
5851 /* See the comments in code_convert_region. */
5852 if (coding->type == coding_type_undecided)
5853 {
5854 detect_coding (coding, XSTRING (str)->data, to_byte);
5855 if (coding->type == coding_type_undecided)
5856 {
5857 coding->type = coding_type_emacs_mule;
5858 coding->category_idx = CODING_CATEGORY_IDX_EMACS_MULE;
5859 /* As emacs-mule decoder will handle composition, we
5860 need this setting to allocate coding->cmp_data
5861 later. */
5862 coding->composing = COMPOSITION_NO;
5863 }
5864 }
5865 if (coding->eol_type == CODING_EOL_UNDECIDED
5866 && coding->type != coding_type_ccl)
5867 {
5868 saved_coding_symbol = coding->symbol;
5869 detect_eol (coding, XSTRING (str)->data, to_byte);
5870 if (coding->eol_type == CODING_EOL_UNDECIDED)
5871 coding->eol_type = CODING_EOL_LF;
5872 /* We had better recover the original eol format if we
5873 encounter an inconsistent eol format while decoding. */
5874 coding->mode |= CODING_MODE_INHIBIT_INCONSISTENT_EOL;
5875 }
5876 }
5877
5878 if (coding->type == coding_type_no_conversion
5879 || coding->type == coding_type_raw_text)
5880 coding->dst_multibyte = 0;
5881
5882 require_decoding = CODING_REQUIRE_DECODING (coding);
5883
5884 if (STRING_MULTIBYTE (str))
5885 {
5886 /* Decoding routines expect the source text to be unibyte. */
5887 str = Fstring_as_unibyte (str);
5888 to_byte = STRING_BYTES (XSTRING (str));
5889 nocopy = 1;
5890 coding->src_multibyte = 0;
5891 }
5892
5893 /* Try to skip the heading and tailing ASCIIs. */
5894 if (require_decoding && coding->type != coding_type_ccl)
5895 {
5896 SHRINK_CONVERSION_REGION (&from, &to_byte, coding, XSTRING (str)->data,
5897 0);
5898 if (from == to_byte)
5899 require_decoding = 0;
5900 shrinked_bytes = from + (STRING_BYTES (XSTRING (str)) - to_byte);
5901 }
5902
5903 if (!require_decoding)
5904 {
5905 coding->consumed = STRING_BYTES (XSTRING (str));
5906 coding->consumed_char = XSTRING (str)->size;
5907 if (coding->dst_multibyte)
5908 {
5909 str = Fstring_as_multibyte (str);
5910 nocopy = 1;
5911 }
5912 coding->produced = STRING_BYTES (XSTRING (str));
5913 coding->produced_char = XSTRING (str)->size;
5914 return (nocopy ? str : Fcopy_sequence (str));
5915 }
5916
5917 if (coding->composing != COMPOSITION_DISABLED)
5918 coding_allocate_composition_data (coding, from);
5919 len = decoding_buffer_size (coding, to_byte - from);
5920 allocate_conversion_buffer (buf, len);
5921
5922 consumed = consumed_char = produced = produced_char = 0;
5923 while (1)
5924 {
5925 result = decode_coding (coding, XSTRING (str)->data + from + consumed,
5926 buf.data + produced, to_byte - from - consumed,
5927 buf.size - produced);
5928 consumed += coding->consumed;
5929 consumed_char += coding->consumed_char;
5930 produced += coding->produced;
5931 produced_char += coding->produced_char;
5932 if (result == CODING_FINISH_NORMAL
5933 || (result == CODING_FINISH_INSUFFICIENT_SRC
5934 && coding->consumed == 0))
5935 break;
5936 if (result == CODING_FINISH_INSUFFICIENT_CMP)
5937 coding_allocate_composition_data (coding, from + produced_char);
5938 else if (result == CODING_FINISH_INSUFFICIENT_DST)
5939 extend_conversion_buffer (&buf);
5940 else if (result == CODING_FINISH_INCONSISTENT_EOL)
5941 {
5942 Lisp_Object eol_type;
5943
5944 /* Recover the original EOL format. */
5945 if (coding->eol_type == CODING_EOL_CR)
5946 {
5947 unsigned char *p;
5948 for (p = buf.data; p < buf.data + produced; p++)
5949 if (*p == '\n') *p = '\r';
5950 }
5951 else if (coding->eol_type == CODING_EOL_CRLF)
5952 {
5953 int num_eol = 0;
5954 unsigned char *p0, *p1;
5955 for (p0 = buf.data, p1 = p0 + produced; p0 < p1; p0++)
5956 if (*p0 == '\n') num_eol++;
5957 if (produced + num_eol >= buf.size)
5958 extend_conversion_buffer (&buf);
5959 for (p0 = buf.data + produced, p1 = p0 + num_eol; p0 > buf.data;)
5960 {
5961 *--p1 = *--p0;
5962 if (*p0 == '\n') *--p1 = '\r';
5963 }
5964 produced += num_eol;
5965 produced_char += num_eol;
5966 }
5967 /* Suppress eol-format conversion in the further conversion. */
5968 coding->eol_type = CODING_EOL_LF;
5969
5970 /* Set the coding system symbol to that for Unix-like EOL. */
5971 eol_type = Fget (saved_coding_symbol, Qeol_type);
5972 if (VECTORP (eol_type)
5973 && XVECTOR (eol_type)->size == 3
5974 && SYMBOLP (XVECTOR (eol_type)->contents[CODING_EOL_LF]))
5975 coding->symbol = XVECTOR (eol_type)->contents[CODING_EOL_LF];
5976 else
5977 coding->symbol = saved_coding_symbol;
5978
5979
5980 }
5981 }
5982
5983 coding->consumed = consumed;
5984 coding->consumed_char = consumed_char;
5985 coding->produced = produced;
5986 coding->produced_char = produced_char;
5987
5988 if (coding->dst_multibyte)
5989 newstr = make_uninit_multibyte_string (produced_char + shrinked_bytes,
5990 produced + shrinked_bytes);
5991 else
5992 newstr = make_uninit_string (produced + shrinked_bytes);
5993 if (from > 0)
5994 bcopy (XSTRING (str)->data, XSTRING (newstr)->data, from);
5995 bcopy (buf.data, XSTRING (newstr)->data + from, produced);
5996 if (shrinked_bytes > from)
5997 bcopy (XSTRING (str)->data + to_byte,
5998 XSTRING (newstr)->data + from + produced,
5999 shrinked_bytes - from);
6000 free_conversion_buffer (&buf);
6001
6002 if (coding->cmp_data && coding->cmp_data->used)
6003 coding_restore_composition (coding, newstr);
6004 coding_free_composition_data (coding);
6005
6006 if (SYMBOLP (coding->post_read_conversion)
6007 && !NILP (Ffboundp (coding->post_read_conversion)))
6008 newstr = run_pre_post_conversion_on_str (newstr, coding, 0);
6009
6010 return newstr;
6011 }
6012
6013 Lisp_Object
6014 encode_coding_string (str, coding, nocopy)
6015 Lisp_Object str;
6016 struct coding_system *coding;
6017 int nocopy;
6018 {
6019 int len;
6020 struct conversion_buffer buf;
6021 int from, to, to_byte;
6022 int result;
6023 int shrinked_bytes = 0;
6024 Lisp_Object newstr;
6025 int consumed, consumed_char, produced, produced_char;
6026
6027 if (SYMBOLP (coding->pre_write_conversion)
6028 && !NILP (Ffboundp (coding->pre_write_conversion)))
6029 str = run_pre_post_conversion_on_str (str, coding, 1);
6030
6031 from = 0;
6032 to = XSTRING (str)->size;
6033 to_byte = STRING_BYTES (XSTRING (str));
6034
6035 /* Encoding routines determine the multibyteness of the source text
6036 by coding->src_multibyte. */
6037 coding->src_multibyte = STRING_MULTIBYTE (str);
6038 coding->dst_multibyte = 0;
6039 if (! CODING_REQUIRE_ENCODING (coding))
6040 {
6041 coding->consumed = STRING_BYTES (XSTRING (str));
6042 coding->consumed_char = XSTRING (str)->size;
6043 if (STRING_MULTIBYTE (str))
6044 {
6045 str = Fstring_as_unibyte (str);
6046 nocopy = 1;
6047 }
6048 coding->produced = STRING_BYTES (XSTRING (str));
6049 coding->produced_char = XSTRING (str)->size;
6050 return (nocopy ? str : Fcopy_sequence (str));
6051 }
6052
6053 if (coding->composing != COMPOSITION_DISABLED)
6054 coding_save_composition (coding, from, to, str);
6055
6056 /* Try to skip the heading and tailing ASCIIs. */
6057 if (coding->type != coding_type_ccl)
6058 {
6059 SHRINK_CONVERSION_REGION (&from, &to_byte, coding, XSTRING (str)->data,
6060 1);
6061 if (from == to_byte)
6062 return (nocopy ? str : Fcopy_sequence (str));
6063 shrinked_bytes = from + (STRING_BYTES (XSTRING (str)) - to_byte);
6064 }
6065
6066 len = encoding_buffer_size (coding, to_byte - from);
6067 allocate_conversion_buffer (buf, len);
6068
6069 consumed = consumed_char = produced = produced_char = 0;
6070 while (1)
6071 {
6072 result = encode_coding (coding, XSTRING (str)->data + from + consumed,
6073 buf.data + produced, to_byte - from - consumed,
6074 buf.size - produced);
6075 consumed += coding->consumed;
6076 consumed_char += coding->consumed_char;
6077 produced += coding->produced;
6078 produced_char += coding->produced_char;
6079 if (result == CODING_FINISH_NORMAL
6080 || (result == CODING_FINISH_INSUFFICIENT_SRC
6081 && coding->consumed == 0))
6082 break;
6083 /* Now result should be CODING_FINISH_INSUFFICIENT_DST. */
6084 extend_conversion_buffer (&buf);
6085 }
6086
6087 coding->consumed = consumed;
6088 coding->consumed_char = consumed_char;
6089 coding->produced = produced;
6090 coding->produced_char = produced_char;
6091
6092 newstr = make_uninit_string (produced + shrinked_bytes);
6093 if (from > 0)
6094 bcopy (XSTRING (str)->data, XSTRING (newstr)->data, from);
6095 bcopy (buf.data, XSTRING (newstr)->data + from, produced);
6096 if (shrinked_bytes > from)
6097 bcopy (XSTRING (str)->data + to_byte,
6098 XSTRING (newstr)->data + from + produced,
6099 shrinked_bytes - from);
6100
6101 free_conversion_buffer (&buf);
6102 coding_free_composition_data (coding);
6103
6104 return newstr;
6105 } 6025 }
6106 6026
6107 6027
6108 #ifdef emacs 6028 #ifdef emacs
6109 /*** 8. Emacs Lisp library functions ***/ 6029 /*** 8. Emacs Lisp library functions ***/
6110 6030
6111 DEFUN ("coding-system-p", Fcoding_system_p, Scoding_system_p, 1, 1, 0, 6031 DEFUN ("coding-system-p", Fcoding_system_p, Scoding_system_p, 1, 1, 0,
6112 doc: /* Return t if OBJECT is nil or a coding-system. 6032 doc: /* Return t if OBJECT is nil or a coding-system.
6113 See the documentation of `make-coding-system' for information 6033 See the documentation of `define-coding-system' for information
6114 about coding-system objects. */) 6034 about coding-system objects. */)
6115 (obj) 6035 (obj)
6116 Lisp_Object obj; 6036 Lisp_Object obj;
6117 { 6037 {
6118 if (NILP (obj)) 6038 return ((NILP (obj) || CODING_SYSTEM_P (obj)) ? Qt : Qnil);
6119 return Qt;
6120 if (!SYMBOLP (obj))
6121 return Qnil;
6122 /* Get coding-spec vector for OBJ. */
6123 obj = Fget (obj, Qcoding_system);
6124 return ((VECTORP (obj) && XVECTOR (obj)->size == 5)
6125 ? Qt : Qnil);
6126 } 6039 }
6127 6040
6128 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system, 6041 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system,
6129 Sread_non_nil_coding_system, 1, 1, 0, 6042 Sread_non_nil_coding_system, 1, 1, 0,
6130 doc: /* Read a coding system from the minibuffer, prompting with string PROMPT. */) 6043 doc: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
6160 1, 1, 0, 6073 1, 1, 0,
6161 doc: /* Check validity of CODING-SYSTEM. 6074 doc: /* Check validity of CODING-SYSTEM.
6162 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error. 6075 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
6163 It is valid if it is a symbol with a non-nil `coding-system' property. 6076 It is valid if it is a symbol with a non-nil `coding-system' property.
6164 The value of property should be a vector of length 5. */) 6077 The value of property should be a vector of length 5. */)
6165 (coding_system) 6078 (coding_system)
6166 Lisp_Object coding_system; 6079 Lisp_Object coding_system;
6167 { 6080 {
6168 CHECK_SYMBOL (coding_system); 6081 CHECK_SYMBOL (coding_system);
6169 if (!NILP (Fcoding_system_p (coding_system))) 6082 if (!NILP (Fcoding_system_p (coding_system)))
6170 return coding_system; 6083 return coding_system;
6171 while (1) 6084 while (1)
6172 Fsignal (Qcoding_system_error, Fcons (coding_system, Qnil)); 6085 Fsignal (Qcoding_system_error, Fcons (coding_system, Qnil));
6173 } 6086 }
6087
6174 6088
6175 Lisp_Object 6089 Lisp_Object
6176 detect_coding_system (src, src_bytes, highest, multibytep) 6090 detect_coding_system (src, src_bytes, highest, multibytep, coding_system)
6177 unsigned char *src; 6091 unsigned char *src;
6178 int src_bytes, highest; 6092 int src_bytes, highest;
6179 int multibytep; 6093 int multibytep;
6180 { 6094 Lisp_Object coding_system;
6181 int coding_mask, eol_type; 6095 {
6182 Lisp_Object val, tmp; 6096 unsigned char *src_end = src + src_bytes;
6183 int dummy; 6097 int mask = CATEGORY_MASK_ANY;
6184 6098 int detected = 0;
6185 coding_mask = detect_coding_mask (src, src_bytes, NULL, &dummy, multibytep); 6099 int c, i;
6186 eol_type = detect_eol_type (src, src_bytes, &dummy); 6100 Lisp_Object attrs, eol_type;
6187 if (eol_type == CODING_EOL_INCONSISTENT) 6101 Lisp_Object val;
6188 eol_type = CODING_EOL_UNDECIDED; 6102 struct coding_system coding;
6189 6103
6190 if (!coding_mask) 6104 if (NILP (coding_system))
6191 { 6105 coding_system = Qundecided;
6192 val = Qundecided; 6106 setup_coding_system (coding_system, &coding);
6193 if (eol_type != CODING_EOL_UNDECIDED) 6107 attrs = CODING_ID_ATTRS (coding.id);
6194 { 6108 eol_type = CODING_ID_EOL_TYPE (coding.id);
6195 Lisp_Object val2; 6109
6196 val2 = Fget (Qundecided, Qeol_type); 6110 coding.source = src;
6197 if (VECTORP (val2)) 6111 coding.src_bytes = src_bytes;
6198 val = XVECTOR (val2)->contents[eol_type]; 6112 coding.src_multibyte = multibytep;
6199 } 6113 coding.consumed = 0;
6200 return (highest ? val : Fcons (val, Qnil)); 6114
6201 } 6115 if (XINT (CODING_ATTR_CATEGORY (attrs)) != coding_category_undecided)
6202 6116 {
6203 /* At first, gather possible coding systems in VAL. */ 6117 mask = 1 << XINT (CODING_ATTR_CATEGORY (attrs));
6204 val = Qnil; 6118 }
6205 for (tmp = Vcoding_category_list; CONSP (tmp); tmp = XCDR (tmp)) 6119 else
6206 { 6120 {
6207 Lisp_Object category_val, category_index; 6121 coding_system = Qnil;
6208 6122 for (; src < src_end; src++)
6209 category_index = Fget (XCAR (tmp), Qcoding_category_index); 6123 {
6210 category_val = Fsymbol_value (XCAR (tmp)); 6124 c = *src;
6211 if (!NILP (category_val) 6125 if (c & 0x80 || (c < 0x20 && (c == ISO_CODE_ESC
6212 && NATNUMP (category_index) 6126 || c == ISO_CODE_SI
6213 && (coding_mask & (1 << XFASTINT (category_index)))) 6127 || c == ISO_CODE_SO)))
6214 {
6215 val = Fcons (category_val, val);
6216 if (highest)
6217 break; 6128 break;
6218 } 6129 }
6219 } 6130 coding.head_ascii = src - coding.source;
6220 if (!highest) 6131
6221 val = Fnreverse (val); 6132 if (src < src_end)
6222 6133 for (i = 0; i < coding_category_raw_text; i++)
6223 /* Then, replace the elements with subsidiary coding systems. */ 6134 {
6224 for (tmp = val; CONSP (tmp); tmp = XCDR (tmp)) 6135 enum coding_category category = coding_priorities[i];
6225 { 6136 struct coding_system *this = coding_categories + category;
6226 if (eol_type != CODING_EOL_UNDECIDED 6137
6227 && eol_type != CODING_EOL_INCONSISTENT) 6138 if (category >= coding_category_raw_text
6228 { 6139 || detected & (1 << category))
6229 Lisp_Object eol; 6140 continue;
6230 eol = Fget (XCAR (tmp), Qeol_type); 6141
6231 if (VECTORP (eol)) 6142 if (this->id < 0)
6232 XSETCAR (tmp, XVECTOR (eol)->contents[eol_type]); 6143 {
6233 } 6144 /* No coding system of this category is defined. */
6234 } 6145 mask &= ~(1 << category);
6146 }
6147 else
6148 {
6149 detected |= detected_mask[category];
6150 if ((*(coding_categories[category].detector)) (&coding, &mask)
6151 && highest)
6152 {
6153 mask &= detected_mask[category];
6154 break;
6155 }
6156 }
6157 }
6158 }
6159
6160 if (!mask)
6161 val = Fcons (make_number (coding_category_raw_text), Qnil);
6162 else if (mask == CATEGORY_MASK_ANY)
6163 val = Fcons (make_number (coding_category_undecided), Qnil);
6164 else if (highest)
6165 {
6166 for (i = 0; i < coding_category_raw_text; i++)
6167 if (mask & (1 << coding_priorities[i]))
6168 {
6169 val = Fcons (make_number (coding_priorities[i]), Qnil);
6170 break;
6171 }
6172 }
6173 else
6174 {
6175 val = Qnil;
6176 for (i = coding_category_raw_text - 1; i >= 0; i--)
6177 if (mask & (1 << coding_priorities[i]))
6178 val = Fcons (make_number (coding_priorities[i]), val);
6179 }
6180
6181 {
6182 int one_byte_eol = -1, two_byte_eol = -1;
6183 Lisp_Object tail;
6184
6185 for (tail = val; CONSP (tail); tail = XCDR (tail))
6186 {
6187 struct coding_system *this
6188 = (NILP (coding_system) ? coding_categories + XINT (XCAR (tail))
6189 : &coding);
6190 int this_eol;
6191
6192 attrs = CODING_ID_ATTRS (this->id);
6193 eol_type = CODING_ID_EOL_TYPE (this->id);
6194 XSETCAR (tail, CODING_ID_NAME (this->id));
6195 if (VECTORP (eol_type))
6196 {
6197 if (EQ (CODING_ATTR_TYPE (attrs), Qutf_16))
6198 {
6199 if (two_byte_eol < 0)
6200 two_byte_eol = detect_eol (this, coding.source, src_bytes);
6201 this_eol = two_byte_eol;
6202 }
6203 else
6204 {
6205 if (one_byte_eol < 0)
6206 one_byte_eol =detect_eol (this, coding.source, src_bytes);
6207 this_eol = one_byte_eol;
6208 }
6209 if (this_eol == EOL_SEEN_LF)
6210 XSETCAR (tail, AREF (eol_type, 0));
6211 else if (this_eol == EOL_SEEN_CRLF)
6212 XSETCAR (tail, AREF (eol_type, 1));
6213 else if (this_eol == EOL_SEEN_CR)
6214 XSETCAR (tail, AREF (eol_type, 2));
6215 }
6216 }
6217 }
6218
6235 return (highest ? XCAR (val) : val); 6219 return (highest ? XCAR (val) : val);
6236 } 6220 }
6221
6237 6222
6238 DEFUN ("detect-coding-region", Fdetect_coding_region, Sdetect_coding_region, 6223 DEFUN ("detect-coding-region", Fdetect_coding_region, Sdetect_coding_region,
6239 2, 3, 0, 6224 2, 3, 0,
6240 doc: /* Detect coding system of the text in the region between START and END. 6225 doc: /* Detect coding system of the text in the region between START and END.
6241 Return a list of possible coding systems ordered by priority. 6226 Return a list of possible coding systems ordered by priority.
6249 (start, end, highest) 6234 (start, end, highest)
6250 Lisp_Object start, end, highest; 6235 Lisp_Object start, end, highest;
6251 { 6236 {
6252 int from, to; 6237 int from, to;
6253 int from_byte, to_byte; 6238 int from_byte, to_byte;
6254 int include_anchor_byte = 0;
6255 6239
6256 CHECK_NUMBER_COERCE_MARKER (start); 6240 CHECK_NUMBER_COERCE_MARKER (start);
6257 CHECK_NUMBER_COERCE_MARKER (end); 6241 CHECK_NUMBER_COERCE_MARKER (end);
6258 6242
6259 validate_region (&start, &end); 6243 validate_region (&start, &end);
6261 from_byte = CHAR_TO_BYTE (from); 6245 from_byte = CHAR_TO_BYTE (from);
6262 to_byte = CHAR_TO_BYTE (to); 6246 to_byte = CHAR_TO_BYTE (to);
6263 6247
6264 if (from < GPT && to >= GPT) 6248 if (from < GPT && to >= GPT)
6265 move_gap_both (to, to_byte); 6249 move_gap_both (to, to_byte);
6266 /* If we an anchor byte `\0' follows the region, we include it in 6250
6267 the detecting source. Then code detectors can handle the tailing
6268 byte sequence more accurately.
6269
6270 Fix me: This is not an perfect solution. It is better that we
6271 add one more argument, say LAST_BLOCK, to all detect_coding_XXX.
6272 */
6273 if (to == Z || (to == GPT && GAP_SIZE > 0))
6274 include_anchor_byte = 1;
6275 return detect_coding_system (BYTE_POS_ADDR (from_byte), 6251 return detect_coding_system (BYTE_POS_ADDR (from_byte),
6276 to_byte - from_byte + include_anchor_byte, 6252 to_byte - from_byte,
6277 !NILP (highest), 6253 !NILP (highest),
6278 !NILP (current_buffer 6254 !NILP (current_buffer
6279 ->enable_multibyte_characters)); 6255 ->enable_multibyte_characters),
6256 Qnil);
6280 } 6257 }
6281 6258
6282 DEFUN ("detect-coding-string", Fdetect_coding_string, Sdetect_coding_string, 6259 DEFUN ("detect-coding-string", Fdetect_coding_string, Sdetect_coding_string,
6283 1, 2, 0, 6260 1, 2, 0,
6284 doc: /* Detect coding system of the text in STRING. 6261 doc: /* Detect coding system of the text in STRING.
6294 Lisp_Object string, highest; 6271 Lisp_Object string, highest;
6295 { 6272 {
6296 CHECK_STRING (string); 6273 CHECK_STRING (string);
6297 6274
6298 return detect_coding_system (XSTRING (string)->data, 6275 return detect_coding_system (XSTRING (string)->data,
6299 /* "+ 1" is to include the anchor byte 6276 STRING_BYTES (XSTRING (string)),
6300 `\0'. With this, code detectors can
6301 handle the tailing bytes more
6302 accurately. */
6303 STRING_BYTES (XSTRING (string)) + 1,
6304 !NILP (highest), 6277 !NILP (highest),
6305 STRING_MULTIBYTE (string)); 6278 STRING_MULTIBYTE (string),
6306 } 6279 Qnil);
6307 6280 }
6308 /* Return an intersection of lists L1 and L2. */ 6281
6309 6282
6310 static Lisp_Object 6283 static INLINE int
6311 intersection (l1, l2) 6284 char_encodable_p (c, attrs)
6312 Lisp_Object l1, l2; 6285 int c;
6313 { 6286 Lisp_Object attrs;
6314 Lisp_Object val; 6287 {
6315 6288 Lisp_Object tail;
6316 for (val = Qnil; CONSP (l1); l1 = XCDR (l1)) 6289 int id;
6317 { 6290 struct charset *charset;
6318 if (!NILP (Fmemq (XCAR (l1), l2))) 6291
6319 val = Fcons (XCAR (l1), val); 6292 for (tail = CODING_ATTR_CHARSET_LIST (attrs);
6320 } 6293 CONSP (tail); tail = XCDR (tail))
6321 return val; 6294 {
6322 } 6295 charset = CHARSET_FROM_ID (XINT (XCAR (tail)));
6323 6296 if (CHAR_CHARSET_P (c, charset))
6324 6297 break;
6325 /* Subroutine for Fsafe_coding_systems_region_internal. 6298 }
6326 6299 return (! NILP (tail));
6327 Return a list of coding systems that safely encode the multibyte
6328 text between P and PEND. SAFE_CODINGS, if non-nil, is a list of
6329 possible coding systems. If it is nil, it means that we have not
6330 yet found any coding systems.
6331
6332 WORK_TABLE is a copy of the char-table Vchar_coding_system_table. An
6333 element of WORK_TABLE is set to t once the element is looked up.
6334
6335 If a non-ASCII single byte char is found, set
6336 *single_byte_char_found to 1. */
6337
6338 static Lisp_Object
6339 find_safe_codings (p, pend, safe_codings, work_table, single_byte_char_found)
6340 unsigned char *p, *pend;
6341 Lisp_Object safe_codings, work_table;
6342 int *single_byte_char_found;
6343 {
6344 int c, len, idx;
6345 Lisp_Object val;
6346
6347 while (p < pend)
6348 {
6349 c = STRING_CHAR_AND_LENGTH (p, pend - p, len);
6350 p += len;
6351 if (ASCII_BYTE_P (c))
6352 /* We can ignore ASCII characters here. */
6353 continue;
6354 if (SINGLE_BYTE_CHAR_P (c))
6355 *single_byte_char_found = 1;
6356 if (NILP (safe_codings))
6357 continue;
6358 /* Check the safe coding systems for C. */
6359 val = char_table_ref_and_index (work_table, c, &idx);
6360 if (EQ (val, Qt))
6361 /* This element was already checked. Ignore it. */
6362 continue;
6363 /* Remember that we checked this element. */
6364 CHAR_TABLE_SET (work_table, make_number (idx), Qt);
6365
6366 /* If there are some safe coding systems for C and we have
6367 already found the other set of coding systems for the
6368 different characters, get the intersection of them. */
6369 if (!EQ (safe_codings, Qt) && !NILP (val))
6370 val = intersection (safe_codings, val);
6371 safe_codings = val;
6372 }
6373 return safe_codings;
6374 } 6300 }
6375 6301
6376 6302
6377 /* Return a list of coding systems that safely encode the text between 6303 /* Return a list of coding systems that safely encode the text between
6378 START and END. If the text contains only ASCII or is unibyte, 6304 START and END. If EXCLUDE is non-nil, it is a list of coding
6379 return t. */ 6305 systems not to check. The returned list doesn't contain any such
6306 coding systems. In any case, If the text contains only ASCII or is
6307 unibyte, return t. */
6380 6308
6381 DEFUN ("find-coding-systems-region-internal", 6309 DEFUN ("find-coding-systems-region-internal",
6382 Ffind_coding_systems_region_internal, 6310 Ffind_coding_systems_region_internal,
6383 Sfind_coding_systems_region_internal, 2, 2, 0, 6311 Sfind_coding_systems_region_internal, 2, 3, 0,
6384 doc: /* Internal use only. */) 6312 doc: /* Internal use only. */)
6385 (start, end) 6313 (start, end, exclude)
6386 Lisp_Object start, end; 6314 Lisp_Object start, end, exclude;
6387 { 6315 {
6388 Lisp_Object work_table, safe_codings; 6316 Lisp_Object coding_attrs_list, safe_codings;
6389 int non_ascii_p = 0; 6317 EMACS_INT start_byte, end_byte;
6390 int single_byte_char_found = 0; 6318 unsigned char *p, *pbeg, *pend;
6391 unsigned char *p1, *p1end, *p2, *p2end, *p; 6319 int c;
6320 Lisp_Object tail, elt;
6392 6321
6393 if (STRINGP (start)) 6322 if (STRINGP (start))
6394 { 6323 {
6395 if (!STRING_MULTIBYTE (start)) 6324 if (!STRING_MULTIBYTE (start)
6325 && XSTRING (start)->size != STRING_BYTES (XSTRING (start)))
6396 return Qt; 6326 return Qt;
6397 p1 = XSTRING (start)->data, p1end = p1 + STRING_BYTES (XSTRING (start)); 6327 start_byte = 0;
6398 p2 = p2end = p1end; 6328 end_byte = STRING_BYTES (XSTRING (start));
6399 if (XSTRING (start)->size != STRING_BYTES (XSTRING (start)))
6400 non_ascii_p = 1;
6401 } 6329 }
6402 else 6330 else
6403 { 6331 {
6404 int from, to, stop;
6405
6406 CHECK_NUMBER_COERCE_MARKER (start); 6332 CHECK_NUMBER_COERCE_MARKER (start);
6407 CHECK_NUMBER_COERCE_MARKER (end); 6333 CHECK_NUMBER_COERCE_MARKER (end);
6408 if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end)) 6334 if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end))
6409 args_out_of_range (start, end); 6335 args_out_of_range (start, end);
6410 if (NILP (current_buffer->enable_multibyte_characters)) 6336 if (NILP (current_buffer->enable_multibyte_characters))
6411 return Qt; 6337 return Qt;
6412 from = CHAR_TO_BYTE (XINT (start)); 6338 start_byte = CHAR_TO_BYTE (XINT (start));
6413 to = CHAR_TO_BYTE (XINT (end)); 6339 end_byte = CHAR_TO_BYTE (XINT (end));
6414 stop = from < GPT_BYTE && GPT_BYTE < to ? GPT_BYTE : to; 6340 if (XINT (end) - XINT (start) == end_byte - start_byte)
6415 p1 = BYTE_POS_ADDR (from), p1end = p1 + (stop - from); 6341 return Qt;
6416 if (stop == to) 6342
6417 p2 = p2end = p1end; 6343 if (start < GPT && end > GPT)
6344 {
6345 if ((GPT - start) < (end - GPT))
6346 move_gap_both (start, start_byte);
6347 else
6348 move_gap_both (end, end_byte);
6349 }
6350 }
6351
6352 coding_attrs_list = Qnil;
6353 for (tail = Vcoding_system_list; CONSP (tail); tail = XCDR (tail))
6354 if (NILP (exclude)
6355 || NILP (Fmemq (XCAR (tail), exclude)))
6356 {
6357 Lisp_Object attrs;
6358
6359 attrs = AREF (CODING_SYSTEM_SPEC (XCAR (tail)), 0);
6360 if (EQ (XCAR (tail), CODING_ATTR_BASE_NAME (attrs))
6361 && ! EQ (CODING_ATTR_TYPE (attrs), Qundecided))
6362 coding_attrs_list = Fcons (attrs, coding_attrs_list);
6363 }
6364
6365 if (STRINGP (start))
6366 p = pbeg = XSTRING (start)->data;
6367 else
6368 p = pbeg = BYTE_POS_ADDR (start_byte);
6369 pend = p + (end_byte - start_byte);
6370
6371 while (p < pend && ASCII_BYTE_P (*p)) p++;
6372 while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--;
6373
6374 while (p < pend)
6375 {
6376 if (ASCII_BYTE_P (*p))
6377 p++;
6418 else 6378 else
6419 p2 = BYTE_POS_ADDR (stop), p2end = p2 + (to - stop); 6379 {
6420 if (XINT (end) - XINT (start) != to - from) 6380 c = STRING_CHAR_ADVANCE (p);
6421 non_ascii_p = 1; 6381
6422 } 6382 charset_map_loaded = 0;
6423 6383 for (tail = coding_attrs_list; CONSP (tail);)
6424 if (!non_ascii_p) 6384 {
6425 { 6385 elt = XCAR (tail);
6426 /* We are sure that the text contains no multibyte character. 6386 if (NILP (elt))
6427 Check if it contains eight-bit-graphic. */ 6387 tail = XCDR (tail);
6428 p = p1; 6388 else if (char_encodable_p (c, elt))
6429 for (p = p1; p < p1end && ASCII_BYTE_P (*p); p++); 6389 tail = XCDR (tail);
6430 if (p == p1end) 6390 else if (CONSP (XCDR (tail)))
6431 { 6391 {
6432 for (p = p2; p < p2end && ASCII_BYTE_P (*p); p++); 6392 XSETCAR (tail, XCAR (XCDR (tail)));
6433 if (p == p2end) 6393 XSETCDR (tail, XCDR (XCDR (tail)));
6434 return Qt; 6394 }
6435 } 6395 else
6436 } 6396 {
6437 6397 XSETCAR (tail, Qnil);
6438 /* The text contains non-ASCII characters. */ 6398 tail = XCDR (tail);
6439 work_table = Fcopy_sequence (Vchar_coding_system_table); 6399 }
6440 safe_codings = find_safe_codings (p1, p1end, Qt, work_table, 6400 }
6441 &single_byte_char_found); 6401 if (charset_map_loaded)
6442 if (p2 < p2end) 6402 {
6443 safe_codings = find_safe_codings (p2, p2end, safe_codings, work_table, 6403 EMACS_INT p_offset = p - pbeg, pend_offset = pend - pbeg;
6444 &single_byte_char_found); 6404
6445 6405 if (STRINGP (start))
6446 if (EQ (safe_codings, Qt)) 6406 pbeg = XSTRING (start)->data;
6447 ; /* Nothing to be done. */ 6407 else
6448 else if (!single_byte_char_found) 6408 pbeg = BYTE_POS_ADDR (start_byte);
6449 { 6409 p = pbeg + p_offset;
6450 /* Append generic coding systems. */ 6410 pend = pbeg + pend_offset;
6451 Lisp_Object args[2]; 6411 }
6452 args[0] = safe_codings; 6412 }
6453 args[1] = Fchar_table_extra_slot (Vchar_coding_system_table, 6413 }
6454 make_number (0)); 6414
6455 safe_codings = Fappend (2, args); 6415 safe_codings = Qnil;
6416 for (tail = coding_attrs_list; CONSP (tail); tail = XCDR (tail))
6417 if (! NILP (XCAR (tail)))
6418 safe_codings = Fcons (CODING_ATTR_BASE_NAME (XCAR (tail)), safe_codings);
6419
6420 return safe_codings;
6421 }
6422
6423
6424 DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region,
6425 Scheck_coding_systems_region, 3, 3, 0,
6426 doc: /* Check if the region is encodable by coding systems.
6427
6428 START and END are buffer positions specifying the region.
6429 CODING-SYSTEM-LIST is a list of coding systems to check.
6430
6431 The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
6432 CODING-SYSTEM is a member of CODING-SYSTEM-LIst and can't encode the
6433 whole region, POS0, POS1, ... are buffer positions where non-encodable
6434 characters are found.
6435
6436 If all coding systems in CODING-SYSTEM-LIST can encode the region, the
6437 value is nil.
6438
6439 START may be a string. In that case, check if the string is
6440 encodable, and the value contains indices to the string instead of
6441 buffer positions. END is ignored. */)
6442 (start, end, coding_system_list)
6443 Lisp_Object start, end, coding_system_list;
6444 {
6445 Lisp_Object list;
6446 EMACS_INT start_byte, end_byte;
6447 int pos;
6448 unsigned char *p, *pbeg, *pend;
6449 int c;
6450 Lisp_Object tail, elt;
6451
6452 if (STRINGP (start))
6453 {
6454 if (!STRING_MULTIBYTE (start)
6455 && XSTRING (start)->size != STRING_BYTES (XSTRING (start)))
6456 return Qnil;
6457 start_byte = 0;
6458 end_byte = STRING_BYTES (XSTRING (start));
6459 pos = 0;
6456 } 6460 }
6457 else 6461 else
6458 safe_codings = Fcons (Qraw_text, 6462 {
6459 Fcons (Qemacs_mule, 6463 CHECK_NUMBER_COERCE_MARKER (start);
6460 Fcons (Qno_conversion, safe_codings))); 6464 CHECK_NUMBER_COERCE_MARKER (end);
6461 return safe_codings; 6465 if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end))
6462 } 6466 args_out_of_range (start, end);
6467 if (NILP (current_buffer->enable_multibyte_characters))
6468 return Qnil;
6469 start_byte = CHAR_TO_BYTE (XINT (start));
6470 end_byte = CHAR_TO_BYTE (XINT (end));
6471 if (XINT (end) - XINT (start) == end_byte - start_byte)
6472 return Qt;
6473
6474 if (start < GPT && end > GPT)
6475 {
6476 if ((GPT - start) < (end - GPT))
6477 move_gap_both (start, start_byte);
6478 else
6479 move_gap_both (end, end_byte);
6480 }
6481 pos = start;
6482 }
6483
6484 list = Qnil;
6485 for (tail = coding_system_list; CONSP (tail); tail = XCDR (tail))
6486 {
6487 elt = XCAR (tail);
6488 list = Fcons (Fcons (elt, Fcons (AREF (CODING_SYSTEM_SPEC (elt), 0),
6489 Qnil)),
6490 list);
6491 }
6492
6493 if (STRINGP (start))
6494 p = pbeg = XSTRING (start)->data;
6495 else
6496 p = pbeg = BYTE_POS_ADDR (start_byte);
6497 pend = p + (end_byte - start_byte);
6498
6499 while (p < pend && ASCII_BYTE_P (*p)) p++, pos++;
6500 while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--;
6501
6502 while (p < pend)
6503 {
6504 if (ASCII_BYTE_P (*p))
6505 p++;
6506 else
6507 {
6508 c = STRING_CHAR_ADVANCE (p);
6509
6510 charset_map_loaded = 0;
6511 for (tail = list; CONSP (tail); tail = XCDR (tail))
6512 {
6513 elt = XCDR (XCAR (tail));
6514 if (! char_encodable_p (c, XCAR (elt)))
6515 XSETCDR (elt, Fcons (make_number (pos), XCDR (elt)));
6516 }
6517 if (charset_map_loaded)
6518 {
6519 EMACS_INT p_offset = p - pbeg, pend_offset = pend - pbeg;
6520
6521 if (STRINGP (start))
6522 pbeg = XSTRING (start)->data;
6523 else
6524 pbeg = BYTE_POS_ADDR (start_byte);
6525 p = pbeg + p_offset;
6526 pend = pbeg + pend_offset;
6527 }
6528 }
6529 pos++;
6530 }
6531
6532 tail = list;
6533 list = Qnil;
6534 for (; CONSP (tail); tail = XCDR (tail))
6535 {
6536 elt = XCAR (tail);
6537 if (CONSP (XCDR (XCDR (elt))))
6538 list = Fcons (Fcons (XCAR (elt), Fnreverse (XCDR (XCDR (elt)))),
6539 list);
6540 }
6541
6542 return list;
6543 }
6544
6463 6545
6464 6546
6465 Lisp_Object 6547 Lisp_Object
6466 code_convert_region1 (start, end, coding_system, encodep) 6548 code_convert_region (start, end, coding_system, dst_object, encodep, norecord)
6467 Lisp_Object start, end, coding_system; 6549 Lisp_Object start, end, coding_system, dst_object;
6468 int encodep; 6550 int encodep, norecord;
6469 { 6551 {
6470 struct coding_system coding; 6552 struct coding_system coding;
6471 int from, to; 6553 EMACS_INT from, from_byte, to, to_byte;
6554 Lisp_Object src_object;
6472 6555
6473 CHECK_NUMBER_COERCE_MARKER (start); 6556 CHECK_NUMBER_COERCE_MARKER (start);
6474 CHECK_NUMBER_COERCE_MARKER (end); 6557 CHECK_NUMBER_COERCE_MARKER (end);
6475 CHECK_SYMBOL (coding_system); 6558 if (NILP (coding_system))
6559 coding_system = Qno_conversion;
6560 else
6561 CHECK_CODING_SYSTEM (coding_system);
6562 src_object = Fcurrent_buffer ();
6563 if (NILP (dst_object))
6564 dst_object = src_object;
6565 else if (! EQ (dst_object, Qt))
6566 CHECK_BUFFER (dst_object);
6476 6567
6477 validate_region (&start, &end); 6568 validate_region (&start, &end);
6478 from = XFASTINT (start); 6569 from = XFASTINT (start);
6570 from_byte = CHAR_TO_BYTE (from);
6479 to = XFASTINT (end); 6571 to = XFASTINT (end);
6480 6572 to_byte = CHAR_TO_BYTE (to);
6481 if (NILP (coding_system)) 6573
6482 return make_number (to - from); 6574 setup_coding_system (coding_system, &coding);
6483
6484 if (setup_coding_system (Fcheck_coding_system (coding_system), &coding) < 0)
6485 error ("Invalid coding system: %s", XSYMBOL (coding_system)->name->data);
6486
6487 coding.mode |= CODING_MODE_LAST_BLOCK; 6575 coding.mode |= CODING_MODE_LAST_BLOCK;
6488 coding.src_multibyte = coding.dst_multibyte 6576
6489 = !NILP (current_buffer->enable_multibyte_characters); 6577 if (encodep)
6490 code_convert_region (from, CHAR_TO_BYTE (from), to, CHAR_TO_BYTE (to), 6578 encode_coding_object (&coding, src_object, from, from_byte, to, to_byte,
6491 &coding, encodep, 1); 6579 dst_object);
6492 Vlast_coding_system_used = coding.symbol; 6580 else
6493 return make_number (coding.produced_char); 6581 decode_coding_object (&coding, src_object, from, from_byte, to, to_byte,
6494 } 6582 dst_object);
6583 if (! norecord)
6584 Vlast_coding_system_used = CODING_ID_NAME (coding.id);
6585
6586 if (coding.result != CODING_RESULT_SUCCESS)
6587 error ("Code conversion error: %d", coding.result);
6588
6589 return (BUFFERP (dst_object)
6590 ? make_number (coding.produced_char)
6591 : coding.dst_object);
6592 }
6593
6495 6594
6496 DEFUN ("decode-coding-region", Fdecode_coding_region, Sdecode_coding_region, 6595 DEFUN ("decode-coding-region", Fdecode_coding_region, Sdecode_coding_region,
6497 3, 3, "r\nzCoding system: ", 6596 3, 4, "r\nzCoding system: ",
6498 doc: /* Decode the current region from the specified coding system. 6597 doc: /* Decode the current region from the specified coding system.
6499 When called from a program, takes three arguments: 6598 When called from a program, takes four arguments:
6500 START, END, and CODING-SYSTEM. START and END are buffer positions. 6599 START, END, CODING-SYSTEM, and DESTINATION.
6600 START and END are buffer positions.
6601
6602 Optional 4th arguments DESTINATION specifies where the decoded text goes.
6603 If nil, the region between START and END is replace by the decoded text.
6604 If buffer, the decoded text is inserted in the buffer.
6605 If t, the decoded text is returned.
6606
6501 This function sets `last-coding-system-used' to the precise coding system 6607 This function sets `last-coding-system-used' to the precise coding system
6502 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is 6608 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
6503 not fully specified.) 6609 not fully specified.)
6504 It returns the length of the decoded text. */) 6610 It returns the length of the decoded text. */)
6505 (start, end, coding_system) 6611 (start, end, coding_system, destination)
6506 Lisp_Object start, end, coding_system; 6612 Lisp_Object start, end, coding_system, destination;
6507 { 6613 {
6508 return code_convert_region1 (start, end, coding_system, 0); 6614 return code_convert_region (start, end, coding_system, destination, 0, 0);
6509 } 6615 }
6510 6616
6511 DEFUN ("encode-coding-region", Fencode_coding_region, Sencode_coding_region, 6617 DEFUN ("encode-coding-region", Fencode_coding_region, Sencode_coding_region,
6512 3, 3, "r\nzCoding system: ", 6618 3, 4, "r\nzCoding system: ",
6513 doc: /* Encode the current region into the specified coding system. 6619 doc: /* Encode the current region by specified coding system.
6514 When called from a program, takes three arguments: 6620 When called from a program, takes three arguments:
6515 START, END, and CODING-SYSTEM. START and END are buffer positions. 6621 START, END, and CODING-SYSTEM. START and END are buffer positions.
6622
6623 Optional 4th arguments DESTINATION specifies where the encoded text goes.
6624 If nil, the region between START and END is replace by the encoded text.
6625 If buffer, the encoded text is inserted in the buffer.
6626 If t, the encoded text is returned.
6627
6516 This function sets `last-coding-system-used' to the precise coding system 6628 This function sets `last-coding-system-used' to the precise coding system
6517 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is 6629 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
6518 not fully specified.) 6630 not fully specified.)
6519 It returns the length of the encoded text. */) 6631 It returns the length of the encoded text. */)
6520 (start, end, coding_system) 6632 (start, end, coding_system, destination)
6521 Lisp_Object start, end, coding_system; 6633 Lisp_Object start, end, coding_system, destination;
6522 { 6634 {
6523 return code_convert_region1 (start, end, coding_system, 1); 6635 return code_convert_region (start, end, coding_system, destination, 1, 0);
6524 } 6636 }
6525 6637
6526 Lisp_Object 6638 Lisp_Object
6527 code_convert_string1 (string, coding_system, nocopy, encodep) 6639 code_convert_string (string, coding_system, dst_object,
6528 Lisp_Object string, coding_system, nocopy; 6640 encodep, nocopy, norecord)
6529 int encodep; 6641 Lisp_Object string, coding_system, dst_object;
6642 int encodep, nocopy, norecord;
6530 { 6643 {
6531 struct coding_system coding; 6644 struct coding_system coding;
6645 EMACS_INT chars, bytes;
6532 6646
6533 CHECK_STRING (string); 6647 CHECK_STRING (string);
6534 CHECK_SYMBOL (coding_system);
6535
6536 if (NILP (coding_system)) 6648 if (NILP (coding_system))
6537 return (NILP (nocopy) ? Fcopy_sequence (string) : string); 6649 {
6538 6650 if (! norecord)
6539 if (setup_coding_system (Fcheck_coding_system (coding_system), &coding) < 0) 6651 Vlast_coding_system_used = Qno_conversion;
6540 error ("Invalid coding system: %s", XSYMBOL (coding_system)->name->data); 6652 if (NILP (dst_object))
6541 6653 return (nocopy ? Fcopy_sequence (string) : string);
6654 }
6655
6656 if (NILP (coding_system))
6657 coding_system = Qno_conversion;
6658 else
6659 CHECK_CODING_SYSTEM (coding_system);
6660 if (NILP (dst_object))
6661 dst_object = Qt;
6662 else if (! EQ (dst_object, Qt))
6663 CHECK_BUFFER (dst_object);
6664
6665 setup_coding_system (coding_system, &coding);
6542 coding.mode |= CODING_MODE_LAST_BLOCK; 6666 coding.mode |= CODING_MODE_LAST_BLOCK;
6543 string = (encodep 6667 chars = XSTRING (string)->size;
6544 ? encode_coding_string (string, &coding, !NILP (nocopy)) 6668 bytes = STRING_BYTES (XSTRING (string));
6545 : decode_coding_string (string, &coding, !NILP (nocopy))); 6669 if (encodep)
6546 Vlast_coding_system_used = coding.symbol; 6670 encode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object);
6547 6671 else
6548 return string; 6672 decode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object);
6549 } 6673 if (! norecord)
6550 6674 Vlast_coding_system_used = CODING_ID_NAME (coding.id);
6551 DEFUN ("decode-coding-string", Fdecode_coding_string, Sdecode_coding_string, 6675
6552 2, 3, 0, 6676 if (coding.result != CODING_RESULT_SUCCESS)
6553 doc: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result. 6677 error ("Code conversion error: %d", coding.result);
6554 Optional arg NOCOPY non-nil means it is OK to return STRING itself 6678
6555 if the decoding operation is trivial. 6679 return (BUFFERP (dst_object)
6556 This function sets `last-coding-system-used' to the precise coding system 6680 ? make_number (coding.produced_char)
6557 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is 6681 : coding.dst_object);
6558 not fully specified.) */) 6682 }
6559 (string, coding_system, nocopy) 6683
6560 Lisp_Object string, coding_system, nocopy;
6561 {
6562 return code_convert_string1 (string, coding_system, nocopy, 0);
6563 }
6564
6565 DEFUN ("encode-coding-string", Fencode_coding_string, Sencode_coding_string,
6566 2, 3, 0,
6567 doc: /* Encode STRING to CODING-SYSTEM, and return the result.
6568 Optional arg NOCOPY non-nil means it is OK to return STRING itself
6569 if the encoding operation is trivial.
6570 This function sets `last-coding-system-used' to the precise coding system
6571 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
6572 not fully specified.) */)
6573 (string, coding_system, nocopy)
6574 Lisp_Object string, coding_system, nocopy;
6575 {
6576 return code_convert_string1 (string, coding_system, nocopy, 1);
6577 }
6578 6684
6579 /* Encode or decode STRING according to CODING_SYSTEM. 6685 /* Encode or decode STRING according to CODING_SYSTEM.
6580 Do not set Vlast_coding_system_used. 6686 Do not set Vlast_coding_system_used.
6581 6687
6582 This function is called only from macros DECODE_FILE and 6688 This function is called only from macros DECODE_FILE and
6585 Lisp_Object 6691 Lisp_Object
6586 code_convert_string_norecord (string, coding_system, encodep) 6692 code_convert_string_norecord (string, coding_system, encodep)
6587 Lisp_Object string, coding_system; 6693 Lisp_Object string, coding_system;
6588 int encodep; 6694 int encodep;
6589 { 6695 {
6590 struct coding_system coding; 6696 code_convert_string (string, coding_system, Qt, encodep, 0, 1);
6591 6697 }
6592 CHECK_STRING (string); 6698
6593 CHECK_SYMBOL (coding_system); 6699
6594 6700 DEFUN ("decode-coding-string", Fdecode_coding_string, Sdecode_coding_string,
6595 if (NILP (coding_system)) 6701 2, 4, 0,
6596 return string; 6702 doc: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
6597 6703
6598 if (setup_coding_system (Fcheck_coding_system (coding_system), &coding) < 0) 6704 Optional third arg NOCOPY non-nil means it is OK to return STRING itself
6599 error ("Invalid coding system: %s", XSYMBOL (coding_system)->name->data); 6705 if the decoding operation is trivial.
6600 6706
6601 coding.composing = COMPOSITION_DISABLED; 6707 Optional fourth arg BUFFER non-nil meant that the decoded text is
6602 coding.mode |= CODING_MODE_LAST_BLOCK; 6708 inserted in BUFFER instead of returned as a astring. In this case,
6603 return (encodep 6709 the return value is BUFFER.
6604 ? encode_coding_string (string, &coding, 1) 6710
6605 : decode_coding_string (string, &coding, 1)); 6711 This function sets `last-coding-system-used' to the precise coding system
6606 } 6712 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
6713 not fully specified. */)
6714 (string, coding_system, nocopy, buffer)
6715 Lisp_Object string, coding_system, nocopy, buffer;
6716 {
6717 return code_convert_string (string, coding_system, buffer,
6718 0, ! NILP (nocopy), 0);
6719 }
6720
6721 DEFUN ("encode-coding-string", Fencode_coding_string, Sencode_coding_string,
6722 2, 4, 0,
6723 doc: /* Encode STRING to CODING-SYSTEM, and return the result.
6724
6725 Optional third arg NOCOPY non-nil means it is OK to return STRING
6726 itself if the encoding operation is trivial.
6727
6728 Optional fourth arg BUFFER non-nil meant that the encoded text is
6729 inserted in BUFFER instead of returned as a astring. In this case,
6730 the return value is BUFFER.
6731
6732 This function sets `last-coding-system-used' to the precise coding system
6733 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
6734 not fully specified.) */)
6735 (string, coding_system, nocopy, buffer)
6736 Lisp_Object string, coding_system, nocopy, buffer;
6737 {
6738 return code_convert_string (string, coding_system, buffer,
6739 nocopy, ! NILP (nocopy), 1);
6740 }
6741
6607 6742
6608 DEFUN ("decode-sjis-char", Fdecode_sjis_char, Sdecode_sjis_char, 1, 1, 0, 6743 DEFUN ("decode-sjis-char", Fdecode_sjis_char, Sdecode_sjis_char, 1, 1, 0,
6609 doc: /* Decode a Japanese character which has CODE in shift_jis encoding. 6744 doc: /* Decode a Japanese character which has CODE in shift_jis encoding.
6610 Return the corresponding character. */) 6745 Return the corresponding character. */)
6611 (code) 6746 (code)
6612 Lisp_Object code; 6747 Lisp_Object code;
6613 { 6748 {
6614 unsigned char c1, c2, s1, s2; 6749 Lisp_Object spec, attrs, val;
6615 Lisp_Object val; 6750 struct charset *charset_roman, *charset_kanji, *charset_kana, *charset;
6616 6751 int c;
6617 CHECK_NUMBER (code); 6752
6618 s1 = (XFASTINT (code)) >> 8, s2 = (XFASTINT (code)) & 0xFF; 6753 CHECK_NATNUM (code);
6619 if (s1 == 0) 6754 c = XFASTINT (code);
6620 { 6755 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec);
6621 if (s2 < 0x80) 6756 attrs = AREF (spec, 0);
6622 XSETFASTINT (val, s2); 6757
6623 else if (s2 >= 0xA0 || s2 <= 0xDF) 6758 if (ASCII_BYTE_P (c)
6624 XSETFASTINT (val, MAKE_CHAR (charset_katakana_jisx0201, s2, 0)); 6759 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
6625 else 6760 return code;
6626 error ("Invalid Shift JIS code: %x", XFASTINT (code)); 6761
6762 val = CODING_ATTR_CHARSET_LIST (attrs);
6763 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
6764 charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
6765 charset_kana = CHARSET_FROM_ID (XINT (XCAR (val)));
6766
6767 if (c <= 0x7F)
6768 charset = charset_roman;
6769 else if (c >= 0xA0 && c < 0xDF)
6770 {
6771 charset = charset_kana;
6772 c -= 0x80;
6627 } 6773 }
6628 else 6774 else
6629 { 6775 {
6630 if ((s1 < 0x80 || (s1 > 0x9F && s1 < 0xE0) || s1 > 0xEF) 6776 int s1 = c >> 8, s2 = c & 0x7F;
6631 || (s2 < 0x40 || s2 == 0x7F || s2 > 0xFC)) 6777
6632 error ("Invalid Shift JIS code: %x", XFASTINT (code)); 6778 if (s1 < 0x81 || (s1 > 0x9F && s1 < 0xE0) || s1 > 0xEF
6633 DECODE_SJIS (s1, s2, c1, c2); 6779 || s2 < 0x40 || s2 == 0x7F || s2 > 0xFC)
6634 XSETFASTINT (val, MAKE_CHAR (charset_jisx0208, c1, c2)); 6780 error ("Invalid code: %d", code);
6635 } 6781 SJIS_TO_JIS (c);
6636 return val; 6782 charset = charset_kanji;
6637 } 6783 }
6784 c = DECODE_CHAR (charset, c);
6785 if (c < 0)
6786 error ("Invalid code: %d", code);
6787 return make_number (c);
6788 }
6789
6638 6790
6639 DEFUN ("encode-sjis-char", Fencode_sjis_char, Sencode_sjis_char, 1, 1, 0, 6791 DEFUN ("encode-sjis-char", Fencode_sjis_char, Sencode_sjis_char, 1, 1, 0,
6640 doc: /* Encode a Japanese character CHAR to shift_jis encoding. 6792 doc: /* Encode a Japanese character CHAR to shift_jis encoding.
6641 Return the corresponding code in SJIS. */) 6793 Return the corresponding code in SJIS. */)
6642 (ch) 6794 (ch)
6643 Lisp_Object ch; 6795 Lisp_Object ch;
6644 { 6796 {
6645 int charset, c1, c2, s1, s2; 6797 Lisp_Object spec, attrs, charset_list;
6646 Lisp_Object val; 6798 int c;
6647 6799 struct charset *charset;
6648 CHECK_NUMBER (ch); 6800 unsigned code;
6649 SPLIT_CHAR (XFASTINT (ch), charset, c1, c2); 6801
6650 if (charset == CHARSET_ASCII) 6802 CHECK_CHARACTER (ch);
6651 { 6803 c = XFASTINT (ch);
6652 val = ch; 6804 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec);
6653 } 6805 attrs = AREF (spec, 0);
6654 else if (charset == charset_jisx0208 6806
6655 && c1 > 0x20 && c1 < 0x7F && c2 > 0x20 && c2 < 0x7F) 6807 if (ASCII_CHAR_P (c)
6656 { 6808 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
6657 ENCODE_SJIS (c1, c2, s1, s2); 6809 return ch;
6658 XSETFASTINT (val, (s1 << 8) | s2); 6810
6659 } 6811 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
6660 else if (charset == charset_katakana_jisx0201 6812 charset = char_charset (c, charset_list, &code);
6661 && c1 > 0x20 && c2 < 0xE0) 6813 if (code == CHARSET_INVALID_CODE (charset))
6662 { 6814 error ("Can't encode by shift_jis encoding: %d", c);
6663 XSETFASTINT (val, c1 | 0x80); 6815 JIS_TO_SJIS (code);
6664 } 6816
6665 else 6817 return make_number (code);
6666 error ("Can't encode to shift_jis: %d", XFASTINT (ch));
6667 return val;
6668 } 6818 }
6669 6819
6670 DEFUN ("decode-big5-char", Fdecode_big5_char, Sdecode_big5_char, 1, 1, 0, 6820 DEFUN ("decode-big5-char", Fdecode_big5_char, Sdecode_big5_char, 1, 1, 0,
6671 doc: /* Decode a Big5 character which has CODE in BIG5 coding system. 6821 doc: /* Decode a Big5 character which has CODE in BIG5 coding system.
6672 Return the corresponding character. */) 6822 Return the corresponding character. */)
6673 (code) 6823 (code)
6674 Lisp_Object code; 6824 Lisp_Object code;
6675 { 6825 {
6676 int charset; 6826 Lisp_Object spec, attrs, val;
6677 unsigned char b1, b2, c1, c2; 6827 struct charset *charset_roman, *charset_big5, *charset;
6678 Lisp_Object val; 6828 int c;
6679 6829
6680 CHECK_NUMBER (code); 6830 CHECK_NATNUM (code);
6681 b1 = (XFASTINT (code)) >> 8, b2 = (XFASTINT (code)) & 0xFF; 6831 c = XFASTINT (code);
6682 if (b1 == 0) 6832 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec);
6683 { 6833 attrs = AREF (spec, 0);
6684 if (b2 >= 0x80) 6834
6685 error ("Invalid BIG5 code: %x", XFASTINT (code)); 6835 if (ASCII_BYTE_P (c)
6686 val = code; 6836 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
6687 } 6837 return code;
6838
6839 val = CODING_ATTR_CHARSET_LIST (attrs);
6840 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
6841 charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val)));
6842
6843 if (c <= 0x7F)
6844 charset = charset_roman;
6688 else 6845 else
6689 { 6846 {
6690 if ((b1 < 0xA1 || b1 > 0xFE) 6847 int b1 = c >> 8, b2 = c & 0x7F;
6691 || (b2 < 0x40 || (b2 > 0x7E && b2 < 0xA1) || b2 > 0xFE)) 6848 if (b1 < 0xA1 || b1 > 0xFE
6692 error ("Invalid BIG5 code: %x", XFASTINT (code)); 6849 || b2 < 0x40 || (b2 > 0x7E && b2 < 0xA1) || b2 > 0xFE)
6693 DECODE_BIG5 (b1, b2, charset, c1, c2); 6850 error ("Invalid code: %d", code);
6694 XSETFASTINT (val, MAKE_CHAR (charset, c1, c2)); 6851 charset = charset_big5;
6695 } 6852 }
6696 return val; 6853 c = DECODE_CHAR (charset, (unsigned )c);
6854 if (c < 0)
6855 error ("Invalid code: %d", code);
6856 return make_number (c);
6697 } 6857 }
6698 6858
6699 DEFUN ("encode-big5-char", Fencode_big5_char, Sencode_big5_char, 1, 1, 0, 6859 DEFUN ("encode-big5-char", Fencode_big5_char, Sencode_big5_char, 1, 1, 0,
6700 doc: /* Encode the Big5 character CHAR to BIG5 coding system. 6860 doc: /* Encode the Big5 character CHAR to BIG5 coding system.
6701 Return the corresponding character code in Big5. */) 6861 Return the corresponding character code in Big5. */)
6702 (ch) 6862 (ch)
6703 Lisp_Object ch; 6863 Lisp_Object ch;
6704 { 6864 {
6705 int charset, c1, c2, b1, b2; 6865 Lisp_Object spec, attrs, charset_list;
6706 Lisp_Object val; 6866 struct charset *charset;
6707 6867 int c;
6708 CHECK_NUMBER (ch); 6868 unsigned code;
6709 SPLIT_CHAR (XFASTINT (ch), charset, c1, c2); 6869
6710 if (charset == CHARSET_ASCII) 6870 CHECK_CHARACTER (ch);
6711 { 6871 c = XFASTINT (ch);
6712 val = ch; 6872 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec);
6713 } 6873 attrs = AREF (spec, 0);
6714 else if ((charset == charset_big5_1 6874 if (ASCII_CHAR_P (c)
6715 && (XFASTINT (ch) >= 0x250a1 && XFASTINT (ch) <= 0x271ec)) 6875 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
6716 || (charset == charset_big5_2 6876 return ch;
6717 && XFASTINT (ch) >= 0x290a1 && XFASTINT (ch) <= 0x2bdb2)) 6877
6718 { 6878 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
6719 ENCODE_BIG5 (charset, c1, c2, b1, b2); 6879 charset = char_charset (c, charset_list, &code);
6720 XSETFASTINT (val, (b1 << 8) | b2); 6880 if (code == CHARSET_INVALID_CODE (charset))
6721 } 6881 error ("Can't encode by Big5 encoding: %d", c);
6722 else 6882
6723 error ("Can't encode to Big5: %d", XFASTINT (ch)); 6883 return make_number (code);
6724 return val; 6884 }
6725 } 6885
6726 6886
6727 DEFUN ("set-terminal-coding-system-internal", 6887 DEFUN ("set-terminal-coding-system-internal",
6728 Fset_terminal_coding_system_internal, 6888 Fset_terminal_coding_system_internal,
6729 Sset_terminal_coding_system_internal, 1, 1, 0, 6889 Sset_terminal_coding_system_internal, 1, 1, 0,
6730 doc: /* Internal use only. */) 6890 doc: /* Internal use only. */)
6731 (coding_system) 6891 (coding_system)
6732 Lisp_Object coding_system;
6733 { 6892 {
6734 CHECK_SYMBOL (coding_system); 6893 CHECK_SYMBOL (coding_system);
6735 setup_coding_system (Fcheck_coding_system (coding_system), &terminal_coding); 6894 setup_coding_system (Fcheck_coding_system (coding_system),
6895 &terminal_coding);
6896
6736 /* We had better not send unsafe characters to terminal. */ 6897 /* We had better not send unsafe characters to terminal. */
6737 terminal_coding.flags |= CODING_FLAG_ISO_SAFE; 6898 terminal_coding.mode |= CODING_MODE_SAFE_ENCODING;
6738 /* Character composition should be disabled. */ 6899 /* Characer composition should be disabled. */
6739 terminal_coding.composing = COMPOSITION_DISABLED; 6900 terminal_coding.common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
6740 /* Error notification should be suppressed. */
6741 terminal_coding.suppress_error = 1;
6742 terminal_coding.src_multibyte = 1; 6901 terminal_coding.src_multibyte = 1;
6743 terminal_coding.dst_multibyte = 0; 6902 terminal_coding.dst_multibyte = 0;
6744 return Qnil; 6903 return Qnil;
6745 } 6904 }
6746 6905
6747 DEFUN ("set-safe-terminal-coding-system-internal", 6906 DEFUN ("set-safe-terminal-coding-system-internal",
6748 Fset_safe_terminal_coding_system_internal, 6907 Fset_safe_terminal_coding_system_internal,
6749 Sset_safe_terminal_coding_system_internal, 1, 1, 0, 6908 Sset_safe_terminal_coding_system_internal, 1, 1, 0,
6750 doc: /* Internal use only. */) 6909 doc: /* Internal use only. */)
6751 (coding_system) 6910 (coding_system)
6752 Lisp_Object coding_system;
6753 { 6911 {
6754 CHECK_SYMBOL (coding_system); 6912 CHECK_SYMBOL (coding_system);
6755 setup_coding_system (Fcheck_coding_system (coding_system), 6913 setup_coding_system (Fcheck_coding_system (coding_system),
6756 &safe_terminal_coding); 6914 &safe_terminal_coding);
6757 /* Character composition should be disabled. */ 6915 /* Characer composition should be disabled. */
6758 safe_terminal_coding.composing = COMPOSITION_DISABLED; 6916 safe_terminal_coding.common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
6759 /* Error notification should be suppressed. */
6760 terminal_coding.suppress_error = 1;
6761 safe_terminal_coding.src_multibyte = 1; 6917 safe_terminal_coding.src_multibyte = 1;
6762 safe_terminal_coding.dst_multibyte = 0; 6918 safe_terminal_coding.dst_multibyte = 0;
6763 return Qnil; 6919 return Qnil;
6764 } 6920 }
6765 6921
6766 DEFUN ("terminal-coding-system", 6922 DEFUN ("terminal-coding-system",
6767 Fterminal_coding_system, Sterminal_coding_system, 0, 0, 0, 6923 Fterminal_coding_system, Sterminal_coding_system, 0, 0, 0,
6768 doc: /* Return coding system specified for terminal output. */) 6924 doc: /* Return coding system specified for terminal output. */)
6769 () 6925 ()
6770 { 6926 {
6771 return terminal_coding.symbol; 6927 return CODING_ID_NAME (terminal_coding.id);
6772 } 6928 }
6773 6929
6774 DEFUN ("set-keyboard-coding-system-internal", 6930 DEFUN ("set-keyboard-coding-system-internal",
6775 Fset_keyboard_coding_system_internal, 6931 Fset_keyboard_coding_system_internal,
6776 Sset_keyboard_coding_system_internal, 1, 1, 0, 6932 Sset_keyboard_coding_system_internal, 1, 1, 0,
6777 doc: /* Internal use only. */) 6933 doc: /* Internal use only. */)
6778 (coding_system) 6934 (coding_system)
6779 Lisp_Object coding_system; 6935 Lisp_Object coding_system;
6780 { 6936 {
6781 CHECK_SYMBOL (coding_system); 6937 CHECK_SYMBOL (coding_system);
6782 setup_coding_system (Fcheck_coding_system (coding_system), &keyboard_coding); 6938 setup_coding_system (Fcheck_coding_system (coding_system),
6783 /* Character composition should be disabled. */ 6939 &keyboard_coding);
6784 keyboard_coding.composing = COMPOSITION_DISABLED; 6940 /* Characer composition should be disabled. */
6941 keyboard_coding.common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
6785 return Qnil; 6942 return Qnil;
6786 } 6943 }
6787 6944
6788 DEFUN ("keyboard-coding-system", 6945 DEFUN ("keyboard-coding-system",
6789 Fkeyboard_coding_system, Skeyboard_coding_system, 0, 0, 0, 6946 Fkeyboard_coding_system, Skeyboard_coding_system, 0, 0, 0,
6790 doc: /* Return coding system specified for decoding keyboard input. */) 6947 doc: /* Return coding system specified for decoding keyboard input. */)
6791 () 6948 ()
6792 { 6949 {
6793 return keyboard_coding.symbol; 6950 return CODING_ID_NAME (keyboard_coding.id);
6794 } 6951 }
6795 6952
6796 6953
6797 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system, 6954 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system,
6798 Sfind_operation_coding_system, 1, MANY, 0, 6955 Sfind_operation_coding_system, 1, MANY, 0,
6836 if (nargs < 2) 6993 if (nargs < 2)
6837 error ("Too few arguments"); 6994 error ("Too few arguments");
6838 operation = args[0]; 6995 operation = args[0];
6839 if (!SYMBOLP (operation) 6996 if (!SYMBOLP (operation)
6840 || !INTEGERP (target_idx = Fget (operation, Qtarget_idx))) 6997 || !INTEGERP (target_idx = Fget (operation, Qtarget_idx)))
6841 error ("Invalid first argument"); 6998 error ("Invalid first arguement");
6842 if (nargs < 1 + XINT (target_idx)) 6999 if (nargs < 1 + XINT (target_idx))
6843 error ("Too few arguments for operation: %s", 7000 error ("Too few arguments for operation: %s",
6844 XSYMBOL (operation)->name->data); 7001 XSYMBOL (operation)->name->data);
6845 target = args[XINT (target_idx) + 1]; 7002 target = args[XINT (target_idx) + 1];
6846 if (!(STRINGP (target) 7003 if (!(STRINGP (target)
6847 || (EQ (operation, Qopen_network_stream) && INTEGERP (target)))) 7004 || (EQ (operation, Qopen_network_stream) && INTEGERP (target))))
6848 error ("Invalid argument %d", XINT (target_idx) + 1); 7005 error ("Invalid %dth argument", XINT (target_idx) + 1);
6849 7006
6850 chain = ((EQ (operation, Qinsert_file_contents) 7007 chain = ((EQ (operation, Qinsert_file_contents)
6851 || EQ (operation, Qwrite_region)) 7008 || EQ (operation, Qwrite_region))
6852 ? Vfile_coding_system_alist 7009 ? Vfile_coding_system_alist
6853 : (EQ (operation, Qopen_network_stream) 7010 : (EQ (operation, Qopen_network_stream)
6857 return Qnil; 7014 return Qnil;
6858 7015
6859 for (; CONSP (chain); chain = XCDR (chain)) 7016 for (; CONSP (chain); chain = XCDR (chain))
6860 { 7017 {
6861 Lisp_Object elt; 7018 Lisp_Object elt;
7019
6862 elt = XCAR (chain); 7020 elt = XCAR (chain);
6863
6864 if (CONSP (elt) 7021 if (CONSP (elt)
6865 && ((STRINGP (target) 7022 && ((STRINGP (target)
6866 && STRINGP (XCAR (elt)) 7023 && STRINGP (XCAR (elt))
6867 && fast_string_match (XCAR (elt), target) >= 0) 7024 && fast_string_match (XCAR (elt), target) >= 0)
6868 || (INTEGERP (target) && EQ (target, XCAR (elt))))) 7025 || (INTEGERP (target) && EQ (target, XCAR (elt)))))
6888 } 7045 }
6889 } 7046 }
6890 return Qnil; 7047 return Qnil;
6891 } 7048 }
6892 7049
6893 DEFUN ("update-coding-systems-internal", Fupdate_coding_systems_internal, 7050 DEFUN ("set-coding-system-priority", Fset_coding_system_priority,
6894 Supdate_coding_systems_internal, 0, 0, 0, 7051 Sset_coding_system_priority, 1, MANY, 0,
6895 doc: /* Update internal database for ISO2022 and CCL based coding systems. 7052 doc: /* Put higher priority to coding systems of the arguments. */)
6896 When values of any coding categories are changed, you must 7053 (nargs, args)
6897 call this function. */) 7054 int nargs;
6898 () 7055 Lisp_Object *args;
7056 {
7057 int i, j;
7058 int changed[coding_category_max];
7059 enum coding_category priorities[coding_category_max];
7060
7061 bzero (changed, sizeof changed);
7062
7063 for (i = j = 0; i < nargs; i++)
7064 {
7065 enum coding_category category;
7066 Lisp_Object spec, attrs;
7067
7068 CHECK_CODING_SYSTEM_GET_SPEC (args[i], spec);
7069 attrs = AREF (spec, 0);
7070 category = XINT (CODING_ATTR_CATEGORY (attrs));
7071 if (changed[category])
7072 /* Ignore this coding system because a coding system of the
7073 same category already had a higher priority. */
7074 continue;
7075 changed[category] = 1;
7076 priorities[j++] = category;
7077 if (coding_categories[category].id >= 0
7078 && ! EQ (args[i], CODING_ID_NAME (coding_categories[category].id)))
7079 setup_coding_system (args[i], &coding_categories[category]);
7080 }
7081
7082 /* Now we have decided top J priorities. Reflect the order of the
7083 original priorities to the remaining priorities. */
7084
7085 for (i = j, j = 0; i < coding_category_max; i++, j++)
7086 {
7087 while (j < coding_category_max
7088 && changed[coding_priorities[j]])
7089 j++;
7090 if (j == coding_category_max)
7091 abort ();
7092 priorities[i] = coding_priorities[j];
7093 }
7094
7095 bcopy (priorities, coding_priorities, sizeof priorities);
7096 return Qnil;
7097 }
7098
7099 DEFUN ("coding-system-priority-list", Fcoding_system_priority_list,
7100 Scoding_system_priority_list, 0, 1, 0,
7101 doc: /* Return a list of coding systems ordered by their priorities. */)
7102 (highestp)
7103 Lisp_Object highestp;
6899 { 7104 {
6900 int i; 7105 int i;
6901 7106 Lisp_Object val;
6902 for (i = CODING_CATEGORY_IDX_EMACS_MULE; i < CODING_CATEGORY_IDX_MAX; i++) 7107
6903 { 7108 for (i = 0, val = Qnil; i < coding_category_max; i++)
6904 Lisp_Object val; 7109 {
6905 7110 enum coding_category category = coding_priorities[i];
6906 val = SYMBOL_VALUE (XVECTOR (Vcoding_category_table)->contents[i]); 7111 int id = coding_categories[category].id;
6907 if (!NILP (val)) 7112 Lisp_Object attrs;
6908 { 7113
6909 if (! coding_system_table[i]) 7114 if (id < 0)
6910 coding_system_table[i] = ((struct coding_system *) 7115 continue;
6911 xmalloc (sizeof (struct coding_system))); 7116 attrs = CODING_ID_ATTRS (id);
6912 setup_coding_system (val, coding_system_table[i]); 7117 if (! NILP (highestp))
6913 } 7118 return CODING_ATTR_BASE_NAME (attrs);
6914 else if (coding_system_table[i]) 7119 val = Fcons (CODING_ATTR_BASE_NAME (attrs), val);
6915 { 7120 }
6916 xfree (coding_system_table[i]); 7121 return Fnreverse (val);
6917 coding_system_table[i] = NULL; 7122 }
6918 } 7123
6919 } 7124 static Lisp_Object
7125 make_subsidiaries (base)
7126 Lisp_Object base;
7127 {
7128 Lisp_Object subsidiaries;
7129 char *suffixes[] = { "-unix", "-dos", "-mac" };
7130 int base_name_len = STRING_BYTES (XSYMBOL (base)->name);
7131 char *buf = (char *) alloca (base_name_len + 6);
7132 int i;
7133
7134 bcopy (XSYMBOL (base)->name->data, buf, base_name_len);
7135 subsidiaries = Fmake_vector (make_number (3), Qnil);
7136 for (i = 0; i < 3; i++)
7137 {
7138 bcopy (suffixes[i], buf + base_name_len, strlen (suffixes[i]) + 1);
7139 ASET (subsidiaries, i, intern (buf));
7140 }
7141 return subsidiaries;
7142 }
7143
7144
7145 DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal,
7146 Sdefine_coding_system_internal, coding_arg_max, MANY, 0,
7147 doc: /* For internal use only. */)
7148 (nargs, args)
7149 int nargs;
7150 Lisp_Object *args;
7151 {
7152 Lisp_Object name;
7153 Lisp_Object spec_vec; /* [ ATTRS ALIASE EOL_TYPE ] */
7154 Lisp_Object attrs; /* Vector of attributes. */
7155 Lisp_Object eol_type;
7156 Lisp_Object aliases;
7157 Lisp_Object coding_type, charset_list, safe_charsets;
7158 enum coding_category category;
7159 Lisp_Object tail, val;
7160 int max_charset_id = 0;
7161 int i;
7162
7163 if (nargs < coding_arg_max)
7164 goto short_args;
7165
7166 attrs = Fmake_vector (make_number (coding_attr_last_index), Qnil);
7167
7168 name = args[coding_arg_name];
7169 CHECK_SYMBOL (name);
7170 CODING_ATTR_BASE_NAME (attrs) = name;
7171
7172 val = args[coding_arg_mnemonic];
7173 if (! STRINGP (val))
7174 CHECK_CHARACTER (val);
7175 CODING_ATTR_MNEMONIC (attrs) = val;
7176
7177 coding_type = args[coding_arg_coding_type];
7178 CHECK_SYMBOL (coding_type);
7179 CODING_ATTR_TYPE (attrs) = coding_type;
7180
7181 charset_list = args[coding_arg_charset_list];
7182 if (SYMBOLP (charset_list))
7183 {
7184 if (EQ (charset_list, Qiso_2022))
7185 {
7186 if (! EQ (coding_type, Qiso_2022))
7187 error ("Invalid charset-list");
7188 charset_list = Viso_2022_charset_list;
7189 }
7190 else if (EQ (charset_list, Qemacs_mule))
7191 {
7192 if (! EQ (coding_type, Qemacs_mule))
7193 error ("Invalid charset-list");
7194 charset_list = Vemacs_mule_charset_list;
7195 }
7196 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
7197 if (max_charset_id < XFASTINT (XCAR (tail)))
7198 max_charset_id = XFASTINT (XCAR (tail));
7199 }
7200 else
7201 {
7202 charset_list = Fcopy_sequence (charset_list);
7203 for (tail = charset_list; !NILP (tail); tail = Fcdr (tail))
7204 {
7205 struct charset *charset;
7206
7207 val = Fcar (tail);
7208 CHECK_CHARSET_GET_CHARSET (val, charset);
7209 if (EQ (coding_type, Qiso_2022)
7210 ? CHARSET_ISO_FINAL (charset) < 0
7211 : EQ (coding_type, Qemacs_mule)
7212 ? CHARSET_EMACS_MULE_ID (charset) < 0
7213 : 0)
7214 error ("Can't handle charset `%s'",
7215 XSYMBOL (CHARSET_NAME (charset))->name->data);
7216
7217 XCAR (tail) = make_number (charset->id);
7218 if (max_charset_id < charset->id)
7219 max_charset_id = charset->id;
7220 }
7221 }
7222 CODING_ATTR_CHARSET_LIST (attrs) = charset_list;
7223
7224 safe_charsets = Fmake_string (make_number (max_charset_id + 1),
7225 make_number (255));
7226 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
7227 XSTRING (safe_charsets)->data[XFASTINT (XCAR (tail))] = 0;
7228 CODING_ATTR_SAFE_CHARSETS (attrs) = safe_charsets;
7229
7230 val = args[coding_arg_decode_translation_table];
7231 if (! NILP (val))
7232 CHECK_CHAR_TABLE (val);
7233 CODING_ATTR_DECODE_TBL (attrs) = val;
7234
7235 val = args[coding_arg_encode_translation_table];
7236 if (! NILP (val))
7237 CHECK_CHAR_TABLE (val);
7238 CODING_ATTR_ENCODE_TBL (attrs) = val;
7239
7240 val = args[coding_arg_post_read_conversion];
7241 CHECK_SYMBOL (val);
7242 CODING_ATTR_POST_READ (attrs) = val;
7243
7244 val = args[coding_arg_pre_write_conversion];
7245 CHECK_SYMBOL (val);
7246 CODING_ATTR_PRE_WRITE (attrs) = val;
7247
7248 val = args[coding_arg_default_char];
7249 if (NILP (val))
7250 CODING_ATTR_DEFAULT_CHAR (attrs) = make_number (' ');
7251 else
7252 {
7253 CHECK_CHARACTER (val);
7254 CODING_ATTR_DEFAULT_CHAR (attrs) = val;
7255 }
7256
7257 val = args[coding_arg_plist];
7258 CHECK_LIST (val);
7259 CODING_ATTR_PLIST (attrs) = val;
7260
7261 if (EQ (coding_type, Qcharset))
7262 {
7263 val = Fmake_vector (make_number (256), Qnil);
7264
7265 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
7266 {
7267 struct charset *charset = CHARSET_FROM_ID (XINT (XCAR (tail)));
7268
7269 for (i = charset->code_space[0]; i <= charset->code_space[1]; i++)
7270 if (NILP (AREF (val, i)))
7271 ASET (val, i, XCAR (tail));
7272 }
7273 ASET (attrs, coding_attr_charset_valids, val);
7274 category = coding_category_charset;
7275 }
7276 else if (EQ (coding_type, Qccl))
7277 {
7278 Lisp_Object valids;
7279
7280 if (nargs < coding_arg_ccl_max)
7281 goto short_args;
7282
7283 val = args[coding_arg_ccl_decoder];
7284 CHECK_CCL_PROGRAM (val);
7285 if (VECTORP (val))
7286 val = Fcopy_sequence (val);
7287 ASET (attrs, coding_attr_ccl_decoder, val);
7288
7289 val = args[coding_arg_ccl_encoder];
7290 CHECK_CCL_PROGRAM (val);
7291 if (VECTORP (val))
7292 val = Fcopy_sequence (val);
7293 ASET (attrs, coding_attr_ccl_encoder, val);
7294
7295 val = args[coding_arg_ccl_valids];
7296 valids = Fmake_string (make_number (256), make_number (0));
7297 for (tail = val; !NILP (tail); tail = Fcdr (tail))
7298 {
7299 val = Fcar (tail);
7300 if (INTEGERP (val))
7301 ASET (valids, XINT (val), 1);
7302 else
7303 {
7304 int from, to;
7305
7306 CHECK_CONS (val);
7307 CHECK_NUMBER (XCAR (val));
7308 CHECK_NUMBER (XCDR (val));
7309 from = XINT (XCAR (val));
7310 to = XINT (XCDR (val));
7311 for (i = from; i <= to; i++)
7312 ASET (valids, i, 1);
7313 }
7314 }
7315 ASET (attrs, coding_attr_ccl_valids, valids);
7316
7317 category = coding_category_ccl;
7318 }
7319 else if (EQ (coding_type, Qutf_16))
7320 {
7321 Lisp_Object bom, endian;
7322
7323 if (nargs < coding_arg_utf16_max)
7324 goto short_args;
7325
7326 bom = args[coding_arg_utf16_bom];
7327 if (! NILP (bom) && ! EQ (bom, Qt))
7328 {
7329 CHECK_CONS (bom);
7330 CHECK_CODING_SYSTEM (XCAR (bom));
7331 CHECK_CODING_SYSTEM (XCDR (bom));
7332 }
7333 ASET (attrs, coding_attr_utf_16_bom, bom);
7334
7335 endian = args[coding_arg_utf16_endian];
7336 ASET (attrs, coding_attr_utf_16_endian, endian);
7337
7338 category = (CONSP (bom)
7339 ? coding_category_utf_16_auto
7340 : NILP (bom)
7341 ? (NILP (endian)
7342 ? coding_category_utf_16_be_nosig
7343 : coding_category_utf_16_le_nosig)
7344 : (NILP (endian)
7345 ? coding_category_utf_16_be
7346 : coding_category_utf_16_le));
7347 }
7348 else if (EQ (coding_type, Qiso_2022))
7349 {
7350 Lisp_Object initial, reg_usage, request, flags;
7351 struct charset *charset;
7352 int i, id, max_id = -1;
7353
7354 if (nargs < coding_arg_iso2022_max)
7355 goto short_args;
7356
7357 initial = Fcopy_sequence (args[coding_arg_iso2022_initial]);
7358 CHECK_VECTOR (initial);
7359 for (i = 0; i < 4; i++)
7360 {
7361 val = Faref (initial, make_number (i));
7362 if (! NILP (val))
7363 {
7364 CHECK_CHARSET_GET_ID (val, id);
7365 ASET (initial, i, make_number (id));
7366 }
7367 else
7368 ASET (initial, i, make_number (-1));
7369 }
7370
7371 reg_usage = args[coding_arg_iso2022_reg_usage];
7372 CHECK_CONS (reg_usage);
7373 CHECK_NATNUM (XCAR (reg_usage));
7374 CHECK_NATNUM (XCDR (reg_usage));
7375
7376 request = Fcopy_sequence (args[coding_arg_iso2022_request]);
7377 for (tail = request; ! NILP (tail); tail = Fcdr (tail))
7378 {
7379 int id;
7380
7381 val = Fcar (tail);
7382 CHECK_CONS (val);
7383 CHECK_CHARSET_GET_ID (XCAR (val), id);
7384 CHECK_NATNUM (XCDR (val));
7385 if (XINT (XCDR (val)) >= 4)
7386 error ("Invalid graphic register number: %d", XINT (XCDR (val)));
7387 XCAR (val) = make_number (id);
7388 }
7389
7390 flags = args[coding_arg_iso2022_flags];
7391 CHECK_NATNUM (flags);
7392 i = XINT (flags);
7393 if (EQ (args[coding_arg_charset_list], Qiso_2022))
7394 flags = make_number (i | CODING_ISO_FLAG_FULL_SUPPORT);
7395
7396 ASET (attrs, coding_attr_iso_initial, initial);
7397 ASET (attrs, coding_attr_iso_usage, reg_usage);
7398 ASET (attrs, coding_attr_iso_request, request);
7399 ASET (attrs, coding_attr_iso_flags, flags);
7400 setup_iso_safe_charsets (attrs);
7401
7402 if (i & CODING_ISO_FLAG_SEVEN_BITS)
7403 category = ((i & (CODING_ISO_FLAG_LOCKING_SHIFT
7404 | CODING_ISO_FLAG_SINGLE_SHIFT))
7405 ? coding_category_iso_7_else
7406 : EQ (args[coding_arg_charset_list], Qiso_2022)
7407 ? coding_category_iso_7
7408 : coding_category_iso_7_tight);
7409 else
7410 {
7411 int id = XINT (AREF (initial, 1));
7412
7413 category = (((i & (CODING_ISO_FLAG_LOCKING_SHIFT
7414 | CODING_ISO_FLAG_SINGLE_SHIFT))
7415 || EQ (args[coding_arg_charset_list], Qiso_2022)
7416 || id < 0)
7417 ? coding_category_iso_8_else
7418 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id)) == 1)
7419 ? coding_category_iso_8_1
7420 : coding_category_iso_8_2);
7421 }
7422 }
7423 else if (EQ (coding_type, Qemacs_mule))
7424 {
7425 if (EQ (args[coding_arg_charset_list], Qemacs_mule))
7426 ASET (attrs, coding_attr_emacs_mule_full, Qt);
7427
7428 category = coding_category_emacs_mule;
7429 }
7430 else if (EQ (coding_type, Qshift_jis))
7431 {
7432
7433 struct charset *charset;
7434
7435 if (XINT (Flength (charset_list)) != 3)
7436 error ("There should be just three charsets");
7437
7438 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
7439 if (CHARSET_DIMENSION (charset) != 1)
7440 error ("Dimension of charset %s is not one",
7441 XSYMBOL (CHARSET_NAME (charset))->name->data);
7442
7443 charset_list = XCDR (charset_list);
7444 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
7445 if (CHARSET_DIMENSION (charset) != 1)
7446 error ("Dimension of charset %s is not one",
7447 XSYMBOL (CHARSET_NAME (charset))->name->data);
7448
7449 charset_list = XCDR (charset_list);
7450 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
7451 if (CHARSET_DIMENSION (charset) != 2)
7452 error ("Dimension of charset %s is not two",
7453 XSYMBOL (CHARSET_NAME (charset))->name->data);
7454
7455 category = coding_category_sjis;
7456 Vsjis_coding_system = name;
7457 }
7458 else if (EQ (coding_type, Qbig5))
7459 {
7460 struct charset *charset;
7461
7462 if (XINT (Flength (charset_list)) != 2)
7463 error ("There should be just two charsets");
7464
7465 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
7466 if (CHARSET_DIMENSION (charset) != 1)
7467 error ("Dimension of charset %s is not one",
7468 XSYMBOL (CHARSET_NAME (charset))->name->data);
7469
7470 charset_list = XCDR (charset_list);
7471 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
7472 if (CHARSET_DIMENSION (charset) != 2)
7473 error ("Dimension of charset %s is not two",
7474 XSYMBOL (CHARSET_NAME (charset))->name->data);
7475
7476 category = coding_category_big5;
7477 Vbig5_coding_system = name;
7478 }
7479 else if (EQ (coding_type, Qraw_text))
7480 category = coding_category_raw_text;
7481 else if (EQ (coding_type, Qutf_8))
7482 category = coding_category_utf_8;
7483 else if (EQ (coding_type, Qundecided))
7484 category = coding_category_undecided;
7485 else
7486 error ("Invalid coding system type: %s",
7487 XSYMBOL (coding_type)->name->data);
7488
7489 CODING_ATTR_CATEGORY (attrs) = make_number (category);
7490
7491 eol_type = args[coding_arg_eol_type];
7492 if (! NILP (eol_type)
7493 && ! EQ (eol_type, Qunix)
7494 && ! EQ (eol_type, Qdos)
7495 && ! EQ (eol_type, Qmac))
7496 error ("Invalid eol-type");
7497
7498 aliases = Fcons (name, Qnil);
7499
7500 if (NILP (eol_type))
7501 {
7502 eol_type = make_subsidiaries (name);
7503 for (i = 0; i < 3; i++)
7504 {
7505 Lisp_Object this_spec, this_name, this_aliases, this_eol_type;
7506
7507 this_name = AREF (eol_type, i);
7508 this_aliases = Fcons (this_name, Qnil);
7509 this_eol_type = (i == 0 ? Qunix : i == 1 ? Qdos : Qmac);
7510 this_spec = Fmake_vector (make_number (3), attrs);
7511 ASET (this_spec, 1, this_aliases);
7512 ASET (this_spec, 2, this_eol_type);
7513 Fputhash (this_name, this_spec, Vcoding_system_hash_table);
7514 Vcoding_system_list = Fcons (this_name, Vcoding_system_list);
7515 Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (this_name), Qnil),
7516 Vcoding_system_alist);
7517 }
7518 }
7519
7520 spec_vec = Fmake_vector (make_number (3), attrs);
7521 ASET (spec_vec, 1, aliases);
7522 ASET (spec_vec, 2, eol_type);
7523
7524 Fputhash (name, spec_vec, Vcoding_system_hash_table);
7525 Vcoding_system_list = Fcons (name, Vcoding_system_list);
7526 Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (name), Qnil),
7527 Vcoding_system_alist);
7528
7529 {
7530 int id = coding_categories[category].id;
7531
7532 if (id < 0 || EQ (name, CODING_ID_NAME (id)))
7533 setup_coding_system (name, &coding_categories[category]);
7534 }
6920 7535
6921 return Qnil; 7536 return Qnil;
6922 } 7537
6923 7538 short_args:
6924 DEFUN ("set-coding-priority-internal", Fset_coding_priority_internal, 7539 return Fsignal (Qwrong_number_of_arguments,
6925 Sset_coding_priority_internal, 0, 0, 0, 7540 Fcons (intern ("define-coding-system-internal"),
6926 doc: /* Update internal database for the current value of `coding-category-list'. 7541 make_number (nargs)));
6927 This function is internal use only. */) 7542 }
6928 () 7543
6929 { 7544 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias,
6930 int i = 0, idx; 7545 Sdefine_coding_system_alias, 2, 2, 0,
6931 Lisp_Object val; 7546 doc: /* Define ALIAS as an alias for CODING-SYSTEM. */)
6932 7547 (alias, coding_system)
6933 val = Vcoding_category_list; 7548 Lisp_Object alias, coding_system;
6934 7549 {
6935 while (CONSP (val) && i < CODING_CATEGORY_IDX_MAX) 7550 Lisp_Object spec, aliases, eol_type;
6936 { 7551
6937 if (! SYMBOLP (XCAR (val))) 7552 CHECK_SYMBOL (alias);
6938 break; 7553 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
6939 idx = XFASTINT (Fget (XCAR (val), Qcoding_category_index)); 7554 aliases = AREF (spec, 1);
6940 if (idx >= CODING_CATEGORY_IDX_MAX) 7555 while (!NILP (XCDR (aliases)))
6941 break; 7556 aliases = XCDR (aliases);
6942 coding_priorities[i++] = (1 << idx); 7557 XCDR (aliases) = Fcons (alias, Qnil);
6943 val = XCDR (val); 7558
6944 } 7559 eol_type = AREF (spec, 2);
6945 /* If coding-category-list is valid and contains all coding 7560 if (VECTORP (eol_type))
6946 categories, `i' should be CODING_CATEGORY_IDX_MAX now. If not, 7561 {
6947 the following code saves Emacs from crashing. */ 7562 Lisp_Object subsidiaries;
6948 while (i < CODING_CATEGORY_IDX_MAX) 7563 int i;
6949 coding_priorities[i++] = CODING_CATEGORY_MASK_RAW_TEXT; 7564
7565 subsidiaries = make_subsidiaries (alias);
7566 for (i = 0; i < 3; i++)
7567 Fdefine_coding_system_alias (AREF (subsidiaries, i),
7568 AREF (eol_type, i));
7569
7570 ASET (spec, 2, subsidiaries);
7571 }
7572
7573 Fputhash (alias, spec, Vcoding_system_hash_table);
6950 7574
6951 return Qnil; 7575 return Qnil;
7576 }
7577
7578 DEFUN ("coding-system-base", Fcoding_system_base, Scoding_system_base,
7579 1, 1, 0,
7580 doc: /* Return the base of CODING-SYSTEM.
7581 Any alias or subsidiary coding systems are not base coding system. */)
7582 (coding_system)
7583 Lisp_Object coding_system;
7584 {
7585 Lisp_Object spec, attrs;
7586
7587 if (NILP (coding_system))
7588 return (Qno_conversion);
7589 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
7590 attrs = AREF (spec, 0);
7591 return CODING_ATTR_BASE_NAME (attrs);
7592 }
7593
7594 DEFUN ("coding-system-plist", Fcoding_system_plist, Scoding_system_plist,
7595 1, 1, 0,
7596 doc: "Return the property list of CODING-SYSTEM.")
7597 (coding_system)
7598 Lisp_Object coding_system;
7599 {
7600 Lisp_Object spec, attrs;
7601
7602 if (NILP (coding_system))
7603 coding_system = Qno_conversion;
7604 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
7605 attrs = AREF (spec, 0);
7606 return CODING_ATTR_PLIST (attrs);
7607 }
7608
7609
7610 DEFUN ("coding-system-aliases", Fcoding_system_aliases, Scoding_system_aliases,
7611 1, 1, 0,
7612 doc: /* Return the list of aliases of CODING-SYSTEM.
7613 A base coding system is what made by `define-coding-system'.
7614 Any alias nor subsidiary coding systems are not base coding system. */)
7615 (coding_system)
7616 Lisp_Object coding_system;
7617 {
7618 Lisp_Object spec;
7619
7620 if (NILP (coding_system))
7621 coding_system = Qno_conversion;
7622 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
7623 return AREF (spec, 2);
7624 }
7625
7626 DEFUN ("coding-system-eol-type", Fcoding_system_eol_type,
7627 Scoding_system_eol_type, 1, 1, 0,
7628 doc: /* Return eol-type of CODING-SYSTEM.
7629 An eol-type is integer 0, 1, 2, or a vector of coding systems.
7630
7631 Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
7632 and CR respectively.
7633
7634 A vector value indicates that a format of end-of-line should be
7635 detected automatically. Nth element of the vector is the subsidiary
7636 coding system whose eol-type is N. */)
7637 (coding_system)
7638 Lisp_Object coding_system;
7639 {
7640 Lisp_Object spec, eol_type;
7641 int n;
7642
7643 if (NILP (coding_system))
7644 coding_system = Qno_conversion;
7645 if (! CODING_SYSTEM_P (coding_system))
7646 return Qnil;
7647 spec = CODING_SYSTEM_SPEC (coding_system);
7648 eol_type = AREF (spec, 2);
7649 if (VECTORP (eol_type))
7650 return Fcopy_sequence (eol_type);
7651 n = EQ (eol_type, Qunix) ? 0 : EQ (eol_type, Qdos) ? 1 : 2;
7652 return make_number (n);
6952 } 7653 }
6953 7654
6954 #endif /* emacs */ 7655 #endif /* emacs */
6955 7656
6956 7657
6959 void 7660 void
6960 init_coding_once () 7661 init_coding_once ()
6961 { 7662 {
6962 int i; 7663 int i;
6963 7664
6964 /* Emacs' internal format specific initialize routine. */ 7665 for (i = 0; i < coding_category_max; i++)
6965 for (i = 0; i <= 0x20; i++) 7666 {
6966 emacs_code_class[i] = EMACS_control_code; 7667 coding_categories[i].id = -1;
6967 emacs_code_class[0x0A] = EMACS_linefeed_code; 7668 coding_priorities[i] = i;
6968 emacs_code_class[0x0D] = EMACS_carriage_return_code; 7669 }
6969 for (i = 0x21 ; i < 0x7F; i++)
6970 emacs_code_class[i] = EMACS_ascii_code;
6971 emacs_code_class[0x7F] = EMACS_control_code;
6972 for (i = 0x80; i < 0xFF; i++)
6973 emacs_code_class[i] = EMACS_invalid_code;
6974 emacs_code_class[LEADING_CODE_PRIVATE_11] = EMACS_leading_code_3;
6975 emacs_code_class[LEADING_CODE_PRIVATE_12] = EMACS_leading_code_3;
6976 emacs_code_class[LEADING_CODE_PRIVATE_21] = EMACS_leading_code_4;
6977 emacs_code_class[LEADING_CODE_PRIVATE_22] = EMACS_leading_code_4;
6978 7670
6979 /* ISO2022 specific initialize routine. */ 7671 /* ISO2022 specific initialize routine. */
6980 for (i = 0; i < 0x20; i++) 7672 for (i = 0; i < 0x20; i++)
6981 iso_code_class[i] = ISO_control_0; 7673 iso_code_class[i] = ISO_control_0;
6982 for (i = 0x21; i < 0x7F; i++) 7674 for (i = 0x21; i < 0x7F; i++)
6994 iso_code_class[ISO_CODE_ESC] = ISO_escape; 7686 iso_code_class[ISO_CODE_ESC] = ISO_escape;
6995 iso_code_class[ISO_CODE_SS2] = ISO_single_shift_2; 7687 iso_code_class[ISO_CODE_SS2] = ISO_single_shift_2;
6996 iso_code_class[ISO_CODE_SS3] = ISO_single_shift_3; 7688 iso_code_class[ISO_CODE_SS3] = ISO_single_shift_3;
6997 iso_code_class[ISO_CODE_CSI] = ISO_control_sequence_introducer; 7689 iso_code_class[ISO_CODE_CSI] = ISO_control_sequence_introducer;
6998 7690
6999 setup_coding_system (Qnil, &keyboard_coding);
7000 setup_coding_system (Qnil, &terminal_coding);
7001 setup_coding_system (Qnil, &safe_terminal_coding);
7002 setup_coding_system (Qnil, &default_buffer_file_coding);
7003
7004 bzero (coding_system_table, sizeof coding_system_table);
7005
7006 bzero (ascii_skip_code, sizeof ascii_skip_code);
7007 for (i = 0; i < 128; i++)
7008 ascii_skip_code[i] = 1;
7009
7010 #if defined (MSDOS) || defined (WINDOWSNT)
7011 system_eol_type = CODING_EOL_CRLF;
7012 #else
7013 system_eol_type = CODING_EOL_LF;
7014 #endif
7015
7016 inhibit_pre_post_conversion = 0; 7691 inhibit_pre_post_conversion = 0;
7692
7693 for (i = 0; i < 256; i++)
7694 {
7695 emacs_mule_bytes[i] = 1;
7696 }
7017 } 7697 }
7018 7698
7019 #ifdef emacs 7699 #ifdef emacs
7020 7700
7021 void 7701 void
7022 syms_of_coding () 7702 syms_of_coding ()
7023 { 7703 {
7024 Qtarget_idx = intern ("target-idx"); 7704 staticpro (&Vcoding_system_hash_table);
7025 staticpro (&Qtarget_idx); 7705 Vcoding_system_hash_table = Fmakehash (Qeq);
7026 7706
7027 Qcoding_system_history = intern ("coding-system-history"); 7707 staticpro (&Vsjis_coding_system);
7028 staticpro (&Qcoding_system_history); 7708 Vsjis_coding_system = Qnil;
7709
7710 staticpro (&Vbig5_coding_system);
7711 Vbig5_coding_system = Qnil;
7712
7713 staticpro (&Vcode_conversion_work_buf_list);
7714 Vcode_conversion_work_buf_list = Qnil;
7715
7716 staticpro (&Vcode_conversion_reused_work_buf);
7717 Vcode_conversion_reused_work_buf = Qnil;
7718
7719 DEFSYM (Qcharset, "charset");
7720 DEFSYM (Qtarget_idx, "target-idx");
7721 DEFSYM (Qcoding_system_history, "coding-system-history");
7029 Fset (Qcoding_system_history, Qnil); 7722 Fset (Qcoding_system_history, Qnil);
7030 7723
7031 /* Target FILENAME is the first argument. */ 7724 /* Target FILENAME is the first argument. */
7032 Fput (Qinsert_file_contents, Qtarget_idx, make_number (0)); 7725 Fput (Qinsert_file_contents, Qtarget_idx, make_number (0));
7033 /* Target FILENAME is the third argument. */ 7726 /* Target FILENAME is the third argument. */
7034 Fput (Qwrite_region, Qtarget_idx, make_number (2)); 7727 Fput (Qwrite_region, Qtarget_idx, make_number (2));
7035 7728
7036 Qcall_process = intern ("call-process"); 7729 DEFSYM (Qcall_process, "call-process");
7037 staticpro (&Qcall_process);
7038 /* Target PROGRAM is the first argument. */ 7730 /* Target PROGRAM is the first argument. */
7039 Fput (Qcall_process, Qtarget_idx, make_number (0)); 7731 Fput (Qcall_process, Qtarget_idx, make_number (0));
7040 7732
7041 Qcall_process_region = intern ("call-process-region"); 7733 DEFSYM (Qcall_process_region, "call-process-region");
7042 staticpro (&Qcall_process_region);
7043 /* Target PROGRAM is the third argument. */ 7734 /* Target PROGRAM is the third argument. */
7044 Fput (Qcall_process_region, Qtarget_idx, make_number (2)); 7735 Fput (Qcall_process_region, Qtarget_idx, make_number (2));
7045 7736
7046 Qstart_process = intern ("start-process"); 7737 DEFSYM (Qstart_process, "start-process");
7047 staticpro (&Qstart_process);
7048 /* Target PROGRAM is the third argument. */ 7738 /* Target PROGRAM is the third argument. */
7049 Fput (Qstart_process, Qtarget_idx, make_number (2)); 7739 Fput (Qstart_process, Qtarget_idx, make_number (2));
7050 7740
7051 Qopen_network_stream = intern ("open-network-stream"); 7741 DEFSYM (Qopen_network_stream, "open-network-stream");
7052 staticpro (&Qopen_network_stream);
7053 /* Target SERVICE is the fourth argument. */ 7742 /* Target SERVICE is the fourth argument. */
7054 Fput (Qopen_network_stream, Qtarget_idx, make_number (3)); 7743 Fput (Qopen_network_stream, Qtarget_idx, make_number (3));
7055 7744
7056 Qcoding_system = intern ("coding-system"); 7745 DEFSYM (Qcoding_system, "coding-system");
7057 staticpro (&Qcoding_system); 7746 DEFSYM (Qcoding_aliases, "coding-aliases");
7058 7747
7059 Qeol_type = intern ("eol-type"); 7748 DEFSYM (Qeol_type, "eol-type");
7060 staticpro (&Qeol_type); 7749 DEFSYM (Qunix, "unix");
7061 7750 DEFSYM (Qdos, "dos");
7062 Qbuffer_file_coding_system = intern ("buffer-file-coding-system"); 7751 DEFSYM (Qmac, "mac");
7063 staticpro (&Qbuffer_file_coding_system); 7752
7064 7753 DEFSYM (Qbuffer_file_coding_system, "buffer-file-coding-system");
7065 Qpost_read_conversion = intern ("post-read-conversion"); 7754 DEFSYM (Qpost_read_conversion, "post-read-conversion");
7066 staticpro (&Qpost_read_conversion); 7755 DEFSYM (Qpre_write_conversion, "pre-write-conversion");
7067 7756 DEFSYM (Qdefault_char, "default-char");
7068 Qpre_write_conversion = intern ("pre-write-conversion"); 7757 DEFSYM (Qundecided, "undecided");
7069 staticpro (&Qpre_write_conversion); 7758 DEFSYM (Qno_conversion, "no-conversion");
7070 7759 DEFSYM (Qraw_text, "raw-text");
7071 Qno_conversion = intern ("no-conversion"); 7760
7072 staticpro (&Qno_conversion); 7761 DEFSYM (Qiso_2022, "iso-2022");
7073 7762
7074 Qundecided = intern ("undecided"); 7763 DEFSYM (Qutf_8, "utf-8");
7075 staticpro (&Qundecided); 7764
7076 7765 DEFSYM (Qutf_16, "utf-16");
7077 Qcoding_system_p = intern ("coding-system-p"); 7766 DEFSYM (Qutf_16_be, "utf-16-be");
7078 staticpro (&Qcoding_system_p); 7767 DEFSYM (Qutf_16_be_nosig, "utf-16-be-nosig");
7079 7768 DEFSYM (Qutf_16_le, "utf-16-l3");
7080 Qcoding_system_error = intern ("coding-system-error"); 7769 DEFSYM (Qutf_16_le_nosig, "utf-16-le-nosig");
7081 staticpro (&Qcoding_system_error); 7770 DEFSYM (Qsignature, "signature");
7082 7771 DEFSYM (Qendian, "endian");
7772 DEFSYM (Qbig, "big");
7773 DEFSYM (Qlittle, "little");
7774
7775 DEFSYM (Qshift_jis, "shift-jis");
7776 DEFSYM (Qbig5, "big5");
7777
7778 DEFSYM (Qcoding_system_p, "coding-system-p");
7779
7780 DEFSYM (Qcoding_system_error, "coding-system-error");
7083 Fput (Qcoding_system_error, Qerror_conditions, 7781 Fput (Qcoding_system_error, Qerror_conditions,
7084 Fcons (Qcoding_system_error, Fcons (Qerror, Qnil))); 7782 Fcons (Qcoding_system_error, Fcons (Qerror, Qnil)));
7085 Fput (Qcoding_system_error, Qerror_message, 7783 Fput (Qcoding_system_error, Qerror_message,
7086 build_string ("Invalid coding system")); 7784 build_string ("Invalid coding system"));
7087 7785
7088 Qcoding_category = intern ("coding-category");
7089 staticpro (&Qcoding_category);
7090 Qcoding_category_index = intern ("coding-category-index");
7091 staticpro (&Qcoding_category_index);
7092
7093 Vcoding_category_table
7094 = Fmake_vector (make_number (CODING_CATEGORY_IDX_MAX), Qnil);
7095 staticpro (&Vcoding_category_table);
7096 {
7097 int i;
7098 for (i = 0; i < CODING_CATEGORY_IDX_MAX; i++)
7099 {
7100 XVECTOR (Vcoding_category_table)->contents[i]
7101 = intern (coding_category_name[i]);
7102 Fput (XVECTOR (Vcoding_category_table)->contents[i],
7103 Qcoding_category_index, make_number (i));
7104 }
7105 }
7106
7107 Qtranslation_table = intern ("translation-table");
7108 staticpro (&Qtranslation_table);
7109 Fput (Qtranslation_table, Qchar_table_extra_slots, make_number (1));
7110
7111 Qtranslation_table_id = intern ("translation-table-id");
7112 staticpro (&Qtranslation_table_id);
7113
7114 Qtranslation_table_for_decode = intern ("translation-table-for-decode");
7115 staticpro (&Qtranslation_table_for_decode);
7116
7117 Qtranslation_table_for_encode = intern ("translation-table-for-encode");
7118 staticpro (&Qtranslation_table_for_encode);
7119
7120 Qsafe_chars = intern ("safe-chars");
7121 staticpro (&Qsafe_chars);
7122
7123 Qchar_coding_system = intern ("char-coding-system");
7124 staticpro (&Qchar_coding_system);
7125
7126 /* Intern this now in case it isn't already done. 7786 /* Intern this now in case it isn't already done.
7127 Setting this variable twice is harmless. 7787 Setting this variable twice is harmless.
7128 But don't staticpro it here--that is done in alloc.c. */ 7788 But don't staticpro it here--that is done in alloc.c. */
7129 Qchar_table_extra_slots = intern ("char-table-extra-slots"); 7789 Qchar_table_extra_slots = intern ("char-table-extra-slots");
7130 Fput (Qsafe_chars, Qchar_table_extra_slots, make_number (0)); 7790
7791 DEFSYM (Qtranslation_table, "translation-table");
7792 Fput (Qtranslation_table, Qchar_table_extra_slots, make_number (1));
7793 DEFSYM (Qtranslation_table_id, "translation-table-id");
7794 DEFSYM (Qtranslation_table_for_decode, "translation-table-for-decode");
7795 DEFSYM (Qtranslation_table_for_encode, "translation-table-for-encode");
7796
7797 DEFSYM (Qchar_coding_system, "char-coding-system");
7798
7131 Fput (Qchar_coding_system, Qchar_table_extra_slots, make_number (2)); 7799 Fput (Qchar_coding_system, Qchar_table_extra_slots, make_number (2));
7132 7800
7133 Qvalid_codes = intern ("valid-codes"); 7801 DEFSYM (Qvalid_codes, "valid-codes");
7134 staticpro (&Qvalid_codes); 7802
7135 7803 DEFSYM (Qemacs_mule, "emacs-mule");
7136 Qemacs_mule = intern ("emacs-mule"); 7804
7137 staticpro (&Qemacs_mule); 7805 Vcoding_category_table
7138 7806 = Fmake_vector (make_number (coding_category_max), Qnil);
7139 Qraw_text = intern ("raw-text"); 7807 staticpro (&Vcoding_category_table);
7140 staticpro (&Qraw_text); 7808 /* Followings are target of code detection. */
7809 ASET (Vcoding_category_table, coding_category_iso_7,
7810 intern ("coding-category-iso-7"));
7811 ASET (Vcoding_category_table, coding_category_iso_7_tight,
7812 intern ("coding-category-iso-7-tight"));
7813 ASET (Vcoding_category_table, coding_category_iso_8_1,
7814 intern ("coding-category-iso-8-1"));
7815 ASET (Vcoding_category_table, coding_category_iso_8_2,
7816 intern ("coding-category-iso-8-2"));
7817 ASET (Vcoding_category_table, coding_category_iso_7_else,
7818 intern ("coding-category-iso-7-else"));
7819 ASET (Vcoding_category_table, coding_category_iso_8_else,
7820 intern ("coding-category-iso-8-else"));
7821 ASET (Vcoding_category_table, coding_category_utf_8,
7822 intern ("coding-category-utf-8"));
7823 ASET (Vcoding_category_table, coding_category_utf_16_be,
7824 intern ("coding-category-utf-16-be"));
7825 ASET (Vcoding_category_table, coding_category_utf_16_le,
7826 intern ("coding-category-utf-16-le"));
7827 ASET (Vcoding_category_table, coding_category_utf_16_be_nosig,
7828 intern ("coding-category-utf-16-be-nosig"));
7829 ASET (Vcoding_category_table, coding_category_utf_16_le_nosig,
7830 intern ("coding-category-utf-16-le-nosig"));
7831 ASET (Vcoding_category_table, coding_category_charset,
7832 intern ("coding-category-charset"));
7833 ASET (Vcoding_category_table, coding_category_sjis,
7834 intern ("coding-category-sjis"));
7835 ASET (Vcoding_category_table, coding_category_big5,
7836 intern ("coding-category-big5"));
7837 ASET (Vcoding_category_table, coding_category_ccl,
7838 intern ("coding-category-ccl"));
7839 ASET (Vcoding_category_table, coding_category_emacs_mule,
7840 intern ("coding-category-emacs-mule"));
7841 /* Followings are NOT target of code detection. */
7842 ASET (Vcoding_category_table, coding_category_raw_text,
7843 intern ("coding-category-raw-text"));
7844 ASET (Vcoding_category_table, coding_category_undecided,
7845 intern ("coding-category-undecided"));
7846
7847 {
7848 Lisp_Object args[coding_arg_max];
7849 Lisp_Object plist[14];
7850 int i;
7851
7852 for (i = 0; i < coding_arg_max; i++)
7853 args[i] = Qnil;
7854
7855 plist[0] = intern (":name");
7856 plist[1] = args[coding_arg_name] = Qno_conversion;
7857 plist[2] = intern (":mnemonic");
7858 plist[3] = args[coding_arg_mnemonic] = make_number ('=');
7859 plist[4] = intern (":coding-type");
7860 plist[5] = args[coding_arg_coding_type] = Qraw_text;
7861 plist[6] = intern (":ascii-compatible-p");
7862 plist[7] = args[coding_arg_ascii_compatible_p] = Qt;
7863 plist[8] = intern (":default-char");
7864 plist[9] = args[coding_arg_default_char] = make_number (0);
7865 plist[10] = intern (":docstring");
7866 plist[11] = build_string ("Do no conversion.\n\
7867 \n\
7868 When you visit a file with this coding, the file is read into a\n\
7869 unibyte buffer as is, thus each byte of a file is treated as a\n\
7870 character.");
7871 plist[12] = intern (":eol-type");
7872 plist[13] = args[coding_arg_eol_type] = Qunix;
7873 args[coding_arg_plist] = Flist (14, plist);
7874 Fdefine_coding_system_internal (coding_arg_max, args);
7875 }
7876
7877 setup_coding_system (Qno_conversion, &keyboard_coding);
7878 setup_coding_system (Qno_conversion, &terminal_coding);
7879 setup_coding_system (Qno_conversion, &safe_terminal_coding);
7141 7880
7142 defsubr (&Scoding_system_p); 7881 defsubr (&Scoding_system_p);
7143 defsubr (&Sread_coding_system); 7882 defsubr (&Sread_coding_system);
7144 defsubr (&Sread_non_nil_coding_system); 7883 defsubr (&Sread_non_nil_coding_system);
7145 defsubr (&Scheck_coding_system); 7884 defsubr (&Scheck_coding_system);
7146 defsubr (&Sdetect_coding_region); 7885 defsubr (&Sdetect_coding_region);
7147 defsubr (&Sdetect_coding_string); 7886 defsubr (&Sdetect_coding_string);
7148 defsubr (&Sfind_coding_systems_region_internal); 7887 defsubr (&Sfind_coding_systems_region_internal);
7888 defsubr (&Scheck_coding_systems_region);
7149 defsubr (&Sdecode_coding_region); 7889 defsubr (&Sdecode_coding_region);
7150 defsubr (&Sencode_coding_region); 7890 defsubr (&Sencode_coding_region);
7151 defsubr (&Sdecode_coding_string); 7891 defsubr (&Sdecode_coding_string);
7152 defsubr (&Sencode_coding_string); 7892 defsubr (&Sencode_coding_string);
7153 defsubr (&Sdecode_sjis_char); 7893 defsubr (&Sdecode_sjis_char);
7158 defsubr (&Sset_safe_terminal_coding_system_internal); 7898 defsubr (&Sset_safe_terminal_coding_system_internal);
7159 defsubr (&Sterminal_coding_system); 7899 defsubr (&Sterminal_coding_system);
7160 defsubr (&Sset_keyboard_coding_system_internal); 7900 defsubr (&Sset_keyboard_coding_system_internal);
7161 defsubr (&Skeyboard_coding_system); 7901 defsubr (&Skeyboard_coding_system);
7162 defsubr (&Sfind_operation_coding_system); 7902 defsubr (&Sfind_operation_coding_system);
7163 defsubr (&Supdate_coding_systems_internal); 7903 defsubr (&Sset_coding_system_priority);
7164 defsubr (&Sset_coding_priority_internal); 7904 defsubr (&Sdefine_coding_system_internal);
7905 defsubr (&Sdefine_coding_system_alias);
7906 defsubr (&Scoding_system_base);
7907 defsubr (&Scoding_system_plist);
7908 defsubr (&Scoding_system_aliases);
7909 defsubr (&Scoding_system_eol_type);
7910 defsubr (&Scoding_system_priority_list);
7165 7911
7166 DEFVAR_LISP ("coding-system-list", &Vcoding_system_list, 7912 DEFVAR_LISP ("coding-system-list", &Vcoding_system_list,
7167 doc: /* List of coding systems. 7913 doc: /* List of coding systems.
7168 7914
7169 Do not alter the value of this variable manually. This variable should be 7915 Do not alter the value of this variable manually. This variable should be
7170 updated by the functions `make-coding-system' and 7916 updated by the functions `define-coding-system' and
7171 `define-coding-system-alias'. */); 7917 `define-coding-system-alias'. */);
7172 Vcoding_system_list = Qnil; 7918 Vcoding_system_list = Qnil;
7173 7919
7174 DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist, 7920 DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist,
7175 doc: /* Alist of coding system names. 7921 doc: /* Alist of coding system names.
7190 system bound to the corresponding coding-category is selected. */); 7936 system bound to the corresponding coding-category is selected. */);
7191 { 7937 {
7192 int i; 7938 int i;
7193 7939
7194 Vcoding_category_list = Qnil; 7940 Vcoding_category_list = Qnil;
7195 for (i = CODING_CATEGORY_IDX_MAX - 1; i >= 0; i--) 7941 for (i = coding_category_max - 1; i >= 0; i--)
7196 Vcoding_category_list 7942 Vcoding_category_list
7197 = Fcons (XVECTOR (Vcoding_category_table)->contents[i], 7943 = Fcons (XVECTOR (Vcoding_category_table)->contents[i],
7198 Vcoding_category_list); 7944 Vcoding_category_list);
7199 } 7945 }
7200 7946
7220 For output to files, if the above procedure does not specify a coding system, 7966 For output to files, if the above procedure does not specify a coding system,
7221 the value of `buffer-file-coding-system' is used. */); 7967 the value of `buffer-file-coding-system' is used. */);
7222 Vcoding_system_for_write = Qnil; 7968 Vcoding_system_for_write = Qnil;
7223 7969
7224 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used, 7970 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used,
7225 doc: /* Coding system used in the latest file or process I/O. */); 7971 doc: /*
7972 Coding system used in the latest file or process I/O. */);
7226 Vlast_coding_system_used = Qnil; 7973 Vlast_coding_system_used = Qnil;
7227 7974
7228 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion, 7975 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion,
7229 doc: /* *Non-nil means always inhibit code conversion of end-of-line format. 7976 doc: /*
7977 *Non-nil means always inhibit code conversion of end-of-line format.
7230 See info node `Coding Systems' and info node `Text and Binary' concerning 7978 See info node `Coding Systems' and info node `Text and Binary' concerning
7231 such conversion. */); 7979 such conversion. */);
7232 inhibit_eol_conversion = 0; 7980 inhibit_eol_conversion = 0;
7233 7981
7234 DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system, 7982 DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system,
7235 doc: /* Non-nil means process buffer inherits coding system of process output. 7983 doc: /*
7984 Non-nil means process buffer inherits coding system of process output.
7236 Bind it to t if the process output is to be treated as if it were a file 7985 Bind it to t if the process output is to be treated as if it were a file
7237 read from some filesystem. */); 7986 read from some filesystem. */);
7238 inherit_process_coding_system = 0; 7987 inherit_process_coding_system = 0;
7239 7988
7240 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist, 7989 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist,
7241 doc: /* Alist to decide a coding system to use for a file I/O operation. 7990 doc: /*
7991 Alist to decide a coding system to use for a file I/O operation.
7242 The format is ((PATTERN . VAL) ...), 7992 The format is ((PATTERN . VAL) ...),
7243 where PATTERN is a regular expression matching a file name, 7993 where PATTERN is a regular expression matching a file name,
7244 VAL is a coding system, a cons of coding systems, or a function symbol. 7994 VAL is a coding system, a cons of coding systems, or a function symbol.
7245 If VAL is a coding system, it is used for both decoding and encoding 7995 If VAL is a coding system, it is used for both decoding and encoding
7246 the file contents. 7996 the file contents.
7253 See also the function `find-operation-coding-system' 8003 See also the function `find-operation-coding-system'
7254 and the variable `auto-coding-alist'. */); 8004 and the variable `auto-coding-alist'. */);
7255 Vfile_coding_system_alist = Qnil; 8005 Vfile_coding_system_alist = Qnil;
7256 8006
7257 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist, 8007 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist,
7258 doc: /* Alist to decide a coding system to use for a process I/O operation. 8008 doc: /*
8009 Alist to decide a coding system to use for a process I/O operation.
7259 The format is ((PATTERN . VAL) ...), 8010 The format is ((PATTERN . VAL) ...),
7260 where PATTERN is a regular expression matching a program name, 8011 where PATTERN is a regular expression matching a program name,
7261 VAL is a coding system, a cons of coding systems, or a function symbol. 8012 VAL is a coding system, a cons of coding systems, or a function symbol.
7262 If VAL is a coding system, it is used for both decoding what received 8013 If VAL is a coding system, it is used for both decoding what received
7263 from the program and encoding what sent to the program. 8014 from the program and encoding what sent to the program.
7268 8019
7269 See also the function `find-operation-coding-system'. */); 8020 See also the function `find-operation-coding-system'. */);
7270 Vprocess_coding_system_alist = Qnil; 8021 Vprocess_coding_system_alist = Qnil;
7271 8022
7272 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist, 8023 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist,
7273 doc: /* Alist to decide a coding system to use for a network I/O operation. 8024 doc: /*
8025 Alist to decide a coding system to use for a network I/O operation.
7274 The format is ((PATTERN . VAL) ...), 8026 The format is ((PATTERN . VAL) ...),
7275 where PATTERN is a regular expression matching a network service name 8027 where PATTERN is a regular expression matching a network service name
7276 or is a port number to connect to, 8028 or is a port number to connect to,
7277 VAL is a coding system, a cons of coding systems, or a function symbol. 8029 VAL is a coding system, a cons of coding systems, or a function symbol.
7278 If VAL is a coding system, it is used for both decoding what received 8030 If VAL is a coding system, it is used for both decoding what received
7290 Also used for decoding keyboard input on X Window system. */); 8042 Also used for decoding keyboard input on X Window system. */);
7291 Vlocale_coding_system = Qnil; 8043 Vlocale_coding_system = Qnil;
7292 8044
7293 /* The eol mnemonics are reset in startup.el system-dependently. */ 8045 /* The eol mnemonics are reset in startup.el system-dependently. */
7294 DEFVAR_LISP ("eol-mnemonic-unix", &eol_mnemonic_unix, 8046 DEFVAR_LISP ("eol-mnemonic-unix", &eol_mnemonic_unix,
7295 doc: /* *String displayed in mode line for UNIX-like (LF) end-of-line format. */); 8047 doc: /*
8048 *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
7296 eol_mnemonic_unix = build_string (":"); 8049 eol_mnemonic_unix = build_string (":");
7297 8050
7298 DEFVAR_LISP ("eol-mnemonic-dos", &eol_mnemonic_dos, 8051 DEFVAR_LISP ("eol-mnemonic-dos", &eol_mnemonic_dos,
7299 doc: /* *String displayed in mode line for DOS-like (CRLF) end-of-line format. */); 8052 doc: /*
8053 *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
7300 eol_mnemonic_dos = build_string ("\\"); 8054 eol_mnemonic_dos = build_string ("\\");
7301 8055
7302 DEFVAR_LISP ("eol-mnemonic-mac", &eol_mnemonic_mac, 8056 DEFVAR_LISP ("eol-mnemonic-mac", &eol_mnemonic_mac,
7303 doc: /* *String displayed in mode line for MAC-like (CR) end-of-line format. */); 8057 doc: /*
8058 *String displayed in mode line for MAC-like (CR) end-of-line format. */);
7304 eol_mnemonic_mac = build_string ("/"); 8059 eol_mnemonic_mac = build_string ("/");
7305 8060
7306 DEFVAR_LISP ("eol-mnemonic-undecided", &eol_mnemonic_undecided, 8061 DEFVAR_LISP ("eol-mnemonic-undecided", &eol_mnemonic_undecided,
7307 doc: /* *String displayed in mode line when end-of-line format is not yet determined. */); 8062 doc: /*
8063 *String displayed in mode line when end-of-line format is not yet determined. */);
7308 eol_mnemonic_undecided = build_string (":"); 8064 eol_mnemonic_undecided = build_string (":");
7309 8065
7310 DEFVAR_LISP ("enable-character-translation", &Venable_character_translation, 8066 DEFVAR_LISP ("enable-character-translation", &Venable_character_translation,
7311 doc: /* *Non-nil enables character translation while encoding and decoding. */); 8067 doc: /*
8068 *Non-nil enables character translation while encoding and decoding. */);
7312 Venable_character_translation = Qt; 8069 Venable_character_translation = Qt;
7313 8070
7314 DEFVAR_LISP ("standard-translation-table-for-decode", 8071 DEFVAR_LISP ("standard-translation-table-for-decode",
7315 &Vstandard_translation_table_for_decode, 8072 &Vstandard_translation_table_for_decode,
7316 doc: /* Table for translating characters while decoding. */); 8073 doc: /* Table for translating characters while decoding. */);
7319 DEFVAR_LISP ("standard-translation-table-for-encode", 8076 DEFVAR_LISP ("standard-translation-table-for-encode",
7320 &Vstandard_translation_table_for_encode, 8077 &Vstandard_translation_table_for_encode,
7321 doc: /* Table for translating characters while encoding. */); 8078 doc: /* Table for translating characters while encoding. */);
7322 Vstandard_translation_table_for_encode = Qnil; 8079 Vstandard_translation_table_for_encode = Qnil;
7323 8080
7324 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_alist, 8081 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_table,
7325 doc: /* Alist of charsets vs revision numbers. 8082 doc: /* Alist of charsets vs revision numbers.
7326 While encoding, if a charset (car part of an element) is found, 8083 While encoding, if a charset (car part of an element) is found,
7327 designate it with the escape sequence identifying revision (cdr part of the element). */); 8084 designate it with the escape sequence identifying revision (cdr part
7328 Vcharset_revision_alist = Qnil; 8085 of the element). */);
8086 Vcharset_revision_table = Qnil;
7329 8087
7330 DEFVAR_LISP ("default-process-coding-system", 8088 DEFVAR_LISP ("default-process-coding-system",
7331 &Vdefault_process_coding_system, 8089 &Vdefault_process_coding_system,
7332 doc: /* Cons of coding systems used for process I/O by default. 8090 doc: /* Cons of coding systems used for process I/O by default.
7333 The car part is used for decoding a process output, 8091 The car part is used for decoding a process output,
7334 the cdr part is used for encoding a text to be sent to a process. */); 8092 the cdr part is used for encoding a text to be sent to a process. */);
7335 Vdefault_process_coding_system = Qnil; 8093 Vdefault_process_coding_system = Qnil;
7336 8094
7337 DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table, 8095 DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table,
7338 doc: /* Table of extra Latin codes in the range 128..159 (inclusive). 8096 doc: /*
8097 Table of extra Latin codes in the range 128..159 (inclusive).
7339 This is a vector of length 256. 8098 This is a vector of length 256.
7340 If Nth element is non-nil, the existence of code N in a file 8099 If Nth element is non-nil, the existence of code N in a file
7341 \(or output of subprocess) doesn't prevent it to be detected as 8100 \(or output of subprocess) doesn't prevent it to be detected as
7342 a coding system of ISO 2022 variant which has a flag 8101 a coding system of ISO 2022 variant which has a flag
7343 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file 8102 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
7345 Only 128th through 159th elements has a meaning. */); 8104 Only 128th through 159th elements has a meaning. */);
7346 Vlatin_extra_code_table = Fmake_vector (make_number (256), Qnil); 8105 Vlatin_extra_code_table = Fmake_vector (make_number (256), Qnil);
7347 8106
7348 DEFVAR_LISP ("select-safe-coding-system-function", 8107 DEFVAR_LISP ("select-safe-coding-system-function",
7349 &Vselect_safe_coding_system_function, 8108 &Vselect_safe_coding_system_function,
7350 doc: /* Function to call to select safe coding system for encoding a text. 8109 doc: /*
8110 Function to call to select safe coding system for encoding a text.
7351 8111
7352 If set, this function is called to force a user to select a proper 8112 If set, this function is called to force a user to select a proper
7353 coding system which can encode the text in the case that a default 8113 coding system which can encode the text in the case that a default
7354 coding system used in each operation can't encode the text. 8114 coding system used in each operation can't encode the text.
7355 8115
7356 The default value is `select-safe-coding-system' (which see). */); 8116 The default value is `select-safe-coding-system' (which see). */);
7357 Vselect_safe_coding_system_function = Qnil; 8117 Vselect_safe_coding_system_function = Qnil;
7358 8118
7359 DEFVAR_LISP ("char-coding-system-table", &Vchar_coding_system_table, 8119 DEFVAR_LISP ("char-coding-system-table", &Vchar_coding_system_table,
7360 doc: /* Char-table containing safe coding systems of each characters. 8120 doc: /*
8121 Char-table containing safe coding systems of each characters.
7361 Each element doesn't include such generic coding systems that can 8122 Each element doesn't include such generic coding systems that can
7362 encode any characters. They are in the first extra slot. */); 8123 encode any characters. They are in the first extra slot. */);
7363 Vchar_coding_system_table = Fmake_char_table (Qchar_coding_system, Qnil); 8124 Vchar_coding_system_table = Fmake_char_table (Qchar_coding_system, Qnil);
7364 8125
7365 DEFVAR_BOOL ("inhibit-iso-escape-detection", 8126 DEFVAR_BOOL ("inhibit-iso-escape-detection",
7366 &inhibit_iso_escape_detection, 8127 &inhibit_iso_escape_detection,
7367 doc: /* If non-nil, Emacs ignores ISO2022's escape sequence on code detection. 8128 doc: /*
8129 If non-nil, Emacs ignores ISO2022's escape sequence on code detection.
7368 8130
7369 By default, on reading a file, Emacs tries to detect how the text is 8131 By default, on reading a file, Emacs tries to detect how the text is
7370 encoded. This code detection is sensitive to escape sequences. If 8132 encoded. This code detection is sensitive to escape sequences. If
7371 the sequence is valid as ISO2022, the code is determined as one of 8133 the sequence is valid as ISO2022, the code is determined as one of
7372 the ISO2022 encodings, and the file is decoded by the corresponding 8134 the ISO2022 encodings, and the file is decoded by the corresponding
7409 8171
7410 return str; 8172 return str;
7411 } 8173 }
7412 8174
7413 #endif /* emacs */ 8175 #endif /* emacs */
7414