aboutsummaryrefslogtreecommitdiffstats
path: root/src/coding.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/coding.c')
-rw-r--r--src/coding.c12722
1 files changed, 7218 insertions, 5504 deletions
diff --git a/src/coding.c b/src/coding.c
index 7bfec68d5d1..89a3090fa56 100644
--- a/src/coding.c
+++ b/src/coding.c
@@ -1,10 +1,13 @@
1/* Coding system handler (conversion, detection, and etc). 1/* Coding system handler (conversion, detection, etc).
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2 Copyright (C) 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008 Free Software Foundation, Inc. 3 2006, 2007, 2008 Free Software Foundation, Inc.
4 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 4 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2008 5 2005, 2006, 2007, 2008
6 National Institute of Advanced Industrial Science and Technology (AIST) 6 National Institute of Advanced Industrial Science and Technology (AIST)
7 Registration Number H14PRO021 7 Registration Number H14PRO021
8 Copyright (C) 2003
9 National Institute of Advanced Industrial Science and Technology (AIST)
10 Registration Number H13PRO009
8 11
9This file is part of GNU Emacs. 12This file is part of GNU Emacs.
10 13
@@ -27,387 +30,329 @@ Boston, MA 02110-1301, USA. */
27 30
28 0. General comments 31 0. General comments
29 1. Preamble 32 1. Preamble
30 2. Emacs' internal format (emacs-mule) handlers 33 2. Emacs' internal format (emacs-utf-8) handlers
31 3. ISO2022 handlers 34 3. UTF-8 handlers
32 4. Shift-JIS and BIG5 handlers 35 4. UTF-16 handlers
33 5. CCL handlers 36 5. Charset-base coding systems handlers
34 6. End-of-line handlers 37 6. emacs-mule (old Emacs' internal format) handlers
35 7. C library functions 38 7. ISO2022 handlers
36 8. Emacs Lisp library functions 39 8. Shift-JIS and BIG5 handlers
37 9. Post-amble 40 9. CCL handlers
41 10. C library functions
42 11. Emacs Lisp library functions
43 12. Postamble
38 44
39*/ 45*/
40 46
41/*** 0. General comments ***/ 47/*** 0. General comments ***
42 48
43 49
44/*** GENERAL NOTE on CODING SYSTEMS *** 50CODING SYSTEM
45 51
46 A coding system is an encoding mechanism for one or more character 52 A coding system is an object for an encoding mechanism that contains
47 sets. Here's a list of coding systems which Emacs can handle. When 53 information about how to convert byte sequences to character
48 we say "decode", it means converting some other coding system to 54 sequences and vice versa. When we say "decode", it means converting
49 Emacs' internal format (emacs-mule), and when we say "encode", 55 a byte sequence of a specific coding system into a character
50 it means converting the coding system emacs-mule to some other 56 sequence that is represented by Emacs' internal coding system
57 `emacs-utf-8', and when we say "encode", it means converting a
58 character sequence of emacs-utf-8 to a byte sequence of a specific
51 coding system. 59 coding system.
52 60
53 0. Emacs' internal format (emacs-mule) 61 In Emacs Lisp, a coding system is represented by a Lisp symbol. In
62 C level, a coding system is represented by a vector of attributes
63 stored in the hash table Vcharset_hash_table. The conversion from
64 coding system symbol to attributes vector is done by looking up
65 Vcharset_hash_table by the symbol.
66
67 Coding systems are classified into the following types depending on
68 the encoding mechanism. Here's a brief description of the types.
69
70 o UTF-8
71
72 o UTF-16
73
74 o Charset-base coding system
75
76 A coding system defined by one or more (coded) character sets.
77 Decoding and encoding are done by a code converter defined for each
78 character set.
79
80 o Old Emacs internal format (emacs-mule)
54 81
55 Emacs itself holds a multi-lingual character in buffers and strings 82 The coding system adopted by old versions of Emacs (20 and 21).
56 in a special format. Details are described in section 2.
57 83
58 1. ISO2022 84 o ISO2022-base coding system
59 85
60 The most famous coding system for multiple character sets. X's 86 The most famous coding system for multiple character sets. X's
61 Compound Text, various EUCs (Extended Unix Code), and coding 87 Compound Text, various EUCs (Extended Unix Code), and coding systems
62 systems used in Internet communication such as ISO-2022-JP are 88 used in the Internet communication such as ISO-2022-JP are all
63 all variants of ISO2022. Details are described in section 3. 89 variants of ISO2022.
64 90
65 2. SJIS (or Shift-JIS or MS-Kanji-Code) 91 o SJIS (or Shift-JIS or MS-Kanji-Code)
66 92
67 A coding system to encode character sets: ASCII, JISX0201, and 93 A coding system to encode character sets: ASCII, JISX0201, and
68 JISX0208. Widely used for PC's in Japan. Details are described in 94 JISX0208. Widely used for PC's in Japan. Details are described in
69 section 4. 95 section 8.
70 96
71 3. BIG5 97 o BIG5
72 98
73 A coding system to encode the character sets ASCII and Big5. Widely 99 A coding system to encode character sets: ASCII and Big5. Widely
74 used for Chinese (mainly in Taiwan and Hong Kong). Details are 100 used for Chinese (mainly in Taiwan and Hong Kong). Details are
75 described in section 4. In this file, when we write "BIG5" 101 described in section 8. In this file, when we write "big5" (all
76 (all uppercase), we mean the coding system, and when we write 102 lowercase), we mean the coding system, and when we write "Big5"
77 "Big5" (capitalized), we mean the character set. 103 (capitalized), we mean the character set.
78 104
79 4. Raw text 105 o CCL
80 106
81 A coding system for text containing random 8-bit code. Emacs does 107 If a user wants to decode/encode text encoded in a coding system
82 no code conversion on such text except for end-of-line format. 108 not listed above, he can supply a decoder and an encoder for it in
109 CCL (Code Conversion Language) programs. Emacs executes the CCL
110 program while decoding/encoding.
83 111
84 5. Other 112 o Raw-text
85 113
86 If a user wants to read/write text encoded in a coding system not 114 A coding system for text containing raw eight-bit data. Emacs
87 listed above, he can supply a decoder and an encoder for it as CCL 115 treats each byte of source text as a character (except for
88 (Code Conversion Language) programs. Emacs executes the CCL program 116 end-of-line conversion).
89 while reading/writing.
90 117
91 Emacs represents a coding system by a Lisp symbol that has a property 118 o No-conversion
92 `coding-system'. But, before actually using the coding system, the 119
93 information about it is set in a structure of type `struct 120 Like raw text, but don't do end-of-line conversion.
94 coding_system' for rapid processing. See section 6 for more details.
95 121
96*/
97 122
98/*** GENERAL NOTES on END-OF-LINE FORMAT *** 123END-OF-LINE FORMAT
99 124
100 How end-of-line of text is encoded depends on the operating system. 125 How text end-of-line is encoded depends on operating system. For
101 For instance, Unix's format is just one byte of `line-feed' code, 126 instance, Unix's format is just one byte of LF (line-feed) code,
102 whereas DOS's format is two-byte sequence of `carriage-return' and 127 whereas DOS's format is two-byte sequence of `carriage-return' and
103 `line-feed' codes. MacOS's format is usually one byte of 128 `line-feed' codes. MacOS's format is usually one byte of
104 `carriage-return'. 129 `carriage-return'.
105 130
106 Since text character encoding and end-of-line encoding are 131 Since text character encoding and end-of-line encoding are
107 independent, any coding system described above can have any 132 independent, any coding system described above can take any format
108 end-of-line format. So Emacs has information about end-of-line 133 of end-of-line (except for no-conversion).
109 format in each coding-system. See section 6 for more details. 134
135STRUCT CODING_SYSTEM
136
137 Before using a coding system for code conversion (i.e. decoding and
138 encoding), we setup a structure of type `struct coding_system'.
139 This structure keeps various information about a specific code
140 conversion (e.g. the location of source and destination data).
110 141
111*/ 142*/
112 143
144/* COMMON MACROS */
145
146
113/*** GENERAL NOTES on `detect_coding_XXX ()' functions *** 147/*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
114 148
115 These functions check if a text between SRC and SRC_END is encoded 149 These functions check if a byte sequence specified as a source in
116 in the coding system category XXX. Each returns an integer value in 150 CODING conforms to the format of XXX, and update the members of
117 which appropriate flag bits for the category XXX are set. The flag 151 DETECT_INFO.
118 bits are defined in macros CODING_CATEGORY_MASK_XXX. Below is the 152
119 template for these functions. If MULTIBYTEP is nonzero, 8-bit codes 153 Return 1 if the byte sequence conforms to XXX, otherwise return 0.
120 of the range 0x80..0x9F are in multibyte form. */ 154
155 Below is the template of these functions. */
156
121#if 0 157#if 0
122int 158static int
123detect_coding_emacs_mule (src, src_end, multibytep) 159detect_coding_XXX (coding, detect_info)
124 unsigned char *src, *src_end; 160 struct coding_system *coding;
125 int multibytep; 161 struct coding_detection_info *detect_info;
126{ 162{
127 ... 163 const unsigned char *src = coding->source;
164 const unsigned char *src_end = coding->source + coding->src_bytes;
165 int multibytep = coding->src_multibyte;
166 int consumed_chars = 0;
167 int found = 0;
168 ...;
169
170 while (1)
171 {
172 /* Get one byte from the source. If the souce is exausted, jump
173 to no_more_source:. */
174 ONE_MORE_BYTE (c);
175
176 if (! __C_conforms_to_XXX___ (c))
177 break;
178 if (! __C_strongly_suggests_XXX__ (c))
179 found = CATEGORY_MASK_XXX;
180 }
181 /* The byte sequence is invalid for XXX. */
182 detect_info->rejected |= CATEGORY_MASK_XXX;
183 return 0;
184
185 no_more_source:
186 /* The source exausted successfully. */
187 detect_info->found |= found;
188 return 1;
128} 189}
129#endif 190#endif
130 191
131/*** GENERAL NOTES on `decode_coding_XXX ()' functions *** 192/*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
132 193
133 These functions decode SRC_BYTES length of unibyte text at SOURCE 194 These functions decode a byte sequence specified as a source by
134 encoded in CODING to Emacs' internal format. The resulting 195 CODING. The resulting multibyte text goes to a place pointed to by
135 multibyte text goes to a place pointed to by DESTINATION, the length 196 CODING->charbuf, the length of which should not exceed
136 of which should not exceed DST_BYTES. 197 CODING->charbuf_size;
137 198
138 These functions set the information about original and decoded texts 199 These functions set the information of original and decoded texts in
139 in the members `produced', `produced_char', `consumed', and 200 CODING->consumed, CODING->consumed_char, and CODING->charbuf_used.
140 `consumed_char' of the structure *CODING. They also set the member 201 They also set CODING->result to one of CODING_RESULT_XXX indicating
141 `result' to one of CODING_FINISH_XXX indicating how the decoding 202 how the decoding is finished.
142 finished.
143 203
144 DST_BYTES zero means that the source area and destination area are 204 Below is the template of these functions. */
145 overlapped, which means that we can produce a decoded text until it
146 reaches the head of the not-yet-decoded source text.
147 205
148 Below is a template for these functions. */
149#if 0 206#if 0
150static void 207static void
151decode_coding_XXX (coding, source, destination, src_bytes, dst_bytes) 208decode_coding_XXXX (coding)
152 struct coding_system *coding; 209 struct coding_system *coding;
153 const unsigned char *source;
154 unsigned char *destination;
155 int src_bytes, dst_bytes;
156{ 210{
157 ... 211 const unsigned char *src = coding->source + coding->consumed;
212 const unsigned char *src_end = coding->source + coding->src_bytes;
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 const unsigned char *src_base;
217 /* A buffer to produce decoded characters. */
218 int *charbuf = coding->charbuf + coding->charbuf_used;
219 int *charbuf_end = coding->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;
158} 244}
159#endif 245#endif
160 246
161/*** GENERAL NOTES on `encode_coding_XXX ()' functions *** 247/*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
162 248
163 These functions encode SRC_BYTES length text at SOURCE from Emacs' 249 These functions encode SRC_BYTES length text at SOURCE of Emacs'
164 internal multibyte format to CODING. The resulting unibyte text 250 internal multibyte format by CODING. The resulting byte sequence
165 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
166 should not exceed DST_BYTES. 252 should not exceed DST_BYTES.
167 253
168 These functions set the information about original and encoded texts 254 These functions set the information of original and encoded texts in
169 in the members `produced', `produced_char', `consumed', and 255 the members produced, produced_char, consumed, and consumed_char of
170 `consumed_char' of the structure *CODING. They also set the member 256 the structure *CODING. They also set the member result to one of
171 `result' to one of CODING_FINISH_XXX indicating how the encoding 257 CODING_RESULT_XXX indicating how the encoding finished.
172 finished.
173 258
174 DST_BYTES zero means that the source area and destination area are 259 DST_BYTES zero means that source area and destination area are
175 overlapped, which means that we can produce encoded text until it 260 overlapped, which means that we can produce a encoded text until it
176 reaches at the head of the not-yet-encoded source text. 261 reaches at the head of not-yet-encoded source text.
177 262
178 Below is a template for these functions. */ 263 Below is a template of these functions. */
179#if 0 264#if 0
180static void 265static void
181encode_coding_XXX (coding, source, destination, src_bytes, dst_bytes) 266encode_coding_XXX (coding)
182 struct coding_system *coding; 267 struct coding_system *coding;
183 unsigned char *source, *destination;
184 int src_bytes, dst_bytes;
185{ 268{
186 ... 269 int multibytep = coding->dst_multibyte;
270 int *charbuf = coding->charbuf;
271 int *charbuf_end = charbuf->charbuf + coding->charbuf_used;
272 unsigned char *dst = coding->destination + coding->produced;
273 unsigned char *dst_end = coding->destination + coding->dst_bytes;
274 unsigned char *adjusted_dst_end = dst_end - _MAX_BYTES_PRODUCED_IN_LOOP_;
275 int produced_chars = 0;
276
277 for (; charbuf < charbuf_end && dst < adjusted_dst_end; charbuf++)
278 {
279 int c = *charbuf;
280 /* Encode C into DST, and increment DST. */
281 }
282 label_no_more_destination:
283 /* How many chars and bytes we produced. */
284 coding->produced_char += produced_chars;
285 coding->produced = dst - coding->destination;
187} 286}
188#endif 287#endif
189 288
190/*** COMMONLY USED MACROS ***/
191
192/* The following two macros ONE_MORE_BYTE and TWO_MORE_BYTES safely
193 get one, two, and three bytes from the source text respectively.
194 If there are not enough bytes in the source, they jump to
195 `label_end_of_loop'. The caller should set variables `coding',
196 `src' and `src_end' to appropriate pointer in advance. These
197 macros are called from decoding routines `decode_coding_XXX', thus
198 it is assumed that the source text is unibyte. */
199
200#define ONE_MORE_BYTE(c1) \
201 do { \
202 if (src >= src_end) \
203 { \
204 coding->result = CODING_FINISH_INSUFFICIENT_SRC; \
205 goto label_end_of_loop; \
206 } \
207 c1 = *src++; \
208 } while (0)
209
210#define TWO_MORE_BYTES(c1, c2) \
211 do { \
212 if (src + 1 >= src_end) \
213 { \
214 coding->result = CODING_FINISH_INSUFFICIENT_SRC; \
215 goto label_end_of_loop; \
216 } \
217 c1 = *src++; \
218 c2 = *src++; \
219 } while (0)
220
221
222/* Like ONE_MORE_BYTE, but 8-bit bytes of data at SRC are in multibyte
223 form if MULTIBYTEP is nonzero. In addition, if SRC is not less
224 than SRC_END, return with RET. */
225
226#define ONE_MORE_BYTE_CHECK_MULTIBYTE(c1, multibytep, ret) \
227 do { \
228 if (src >= src_end) \
229 { \
230 coding->result = CODING_FINISH_INSUFFICIENT_SRC; \
231 return ret; \
232 } \
233 c1 = *src++; \
234 if (multibytep && c1 == LEADING_CODE_8_BIT_CONTROL) \
235 c1 = *src++ - 0x20; \
236 } while (0)
237
238/* Set C to the next character at the source text pointed by `src'.
239 If there are not enough characters in the source, jump to
240 `label_end_of_loop'. The caller should set variables `coding'
241 `src', `src_end', and `translation_table' to appropriate pointers
242 in advance. This macro is used in encoding routines
243 `encode_coding_XXX', thus it assumes that the source text is in
244 multibyte form except for 8-bit characters. 8-bit characters are
245 in multibyte form if coding->src_multibyte is nonzero, else they
246 are represented by a single byte. */
247
248#define ONE_MORE_CHAR(c) \
249 do { \
250 int len = src_end - src; \
251 int bytes; \
252 if (len <= 0) \
253 { \
254 coding->result = CODING_FINISH_INSUFFICIENT_SRC; \
255 goto label_end_of_loop; \
256 } \
257 if (coding->src_multibyte \
258 || UNIBYTE_STR_AS_MULTIBYTE_P (src, len, bytes)) \
259 c = STRING_CHAR_AND_LENGTH (src, len, bytes); \
260 else \
261 c = *src, bytes = 1; \
262 if (!NILP (translation_table)) \
263 c = translate_char (translation_table, c, -1, 0, 0); \
264 src += bytes; \
265 } while (0)
266
267
268/* Produce a multibyte form of character C to `dst'. Jump to
269 `label_end_of_loop' if there's not enough space at `dst'.
270
271 If we are now in the middle of a composition sequence, the decoded
272 character may be ALTCHAR (for the current composition). In that
273 case, the character goes to coding->cmp_data->data instead of
274 `dst'.
275
276 This macro is used in decoding routines. */
277
278#define EMIT_CHAR(c) \
279 do { \
280 if (! COMPOSING_P (coding) \
281 || coding->composing == COMPOSITION_RELATIVE \
282 || coding->composing == COMPOSITION_WITH_RULE) \
283 { \
284 int bytes = CHAR_BYTES (c); \
285 if ((dst + bytes) > (dst_bytes ? dst_end : src)) \
286 { \
287 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
288 goto label_end_of_loop; \
289 } \
290 dst += CHAR_STRING (c, dst); \
291 coding->produced_char++; \
292 } \
293 \
294 if (COMPOSING_P (coding) \
295 && coding->composing != COMPOSITION_RELATIVE) \
296 { \
297 CODING_ADD_COMPOSITION_COMPONENT (coding, c); \
298 coding->composition_rule_follows \
299 = coding->composing != COMPOSITION_WITH_ALTCHARS; \
300 } \
301 } while (0)
302
303
304#define EMIT_ONE_BYTE(c) \
305 do { \
306 if (dst >= (dst_bytes ? dst_end : src)) \
307 { \
308 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
309 goto label_end_of_loop; \
310 } \
311 *dst++ = c; \
312 } while (0)
313
314#define EMIT_TWO_BYTES(c1, c2) \
315 do { \
316 if (dst + 2 > (dst_bytes ? dst_end : src)) \
317 { \
318 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
319 goto label_end_of_loop; \
320 } \
321 *dst++ = c1, *dst++ = c2; \
322 } while (0)
323
324#define EMIT_BYTES(from, to) \
325 do { \
326 if (dst + (to - from) > (dst_bytes ? dst_end : src)) \
327 { \
328 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
329 goto label_end_of_loop; \
330 } \
331 while (from < to) \
332 *dst++ = *from++; \
333 } while (0)
334
335 289
336/*** 1. Preamble ***/ 290/*** 1. Preamble ***/
337 291
338#ifdef emacs
339#include <config.h> 292#include <config.h>
340#endif
341
342#include <stdio.h> 293#include <stdio.h>
343 294
344#ifdef emacs
345
346#include "lisp.h" 295#include "lisp.h"
347#include "buffer.h" 296#include "buffer.h"
297#include "character.h"
348#include "charset.h" 298#include "charset.h"
349#include "composite.h"
350#include "ccl.h" 299#include "ccl.h"
300#include "composite.h"
351#include "coding.h" 301#include "coding.h"
352#include "window.h" 302#include "window.h"
353#include "intervals.h"
354#include "frame.h" 303#include "frame.h"
355#include "termhooks.h" 304#include "termhooks.h"
356 305
357#else /* not emacs */ 306Lisp_Object Vcoding_system_hash_table;
358
359#include "mulelib.h"
360
361#endif /* not emacs */
362 307
363Lisp_Object Qcoding_system, Qeol_type; 308Lisp_Object Qcoding_system, Qcoding_aliases, Qeol_type;
309Lisp_Object Qunix, Qdos;
310extern Lisp_Object Qmac; /* frame.c */
364Lisp_Object Qbuffer_file_coding_system; 311Lisp_Object Qbuffer_file_coding_system;
365Lisp_Object Qpost_read_conversion, Qpre_write_conversion; 312Lisp_Object Qpost_read_conversion, Qpre_write_conversion;
313Lisp_Object Qdefault_char;
366Lisp_Object Qno_conversion, Qundecided; 314Lisp_Object Qno_conversion, Qundecided;
315Lisp_Object Qcharset, Qiso_2022, Qutf_8, Qutf_16, Qshift_jis, Qbig5;
316Lisp_Object Qbig, Qlittle;
367Lisp_Object Qcoding_system_history; 317Lisp_Object Qcoding_system_history;
368Lisp_Object Qsafe_chars;
369Lisp_Object Qvalid_codes; 318Lisp_Object Qvalid_codes;
370Lisp_Object Qascii_incompatible; 319Lisp_Object QCcategory, QCmnemonic, QCdefalut_char;
320Lisp_Object QCdecode_translation_table, QCencode_translation_table;
321Lisp_Object QCpost_read_conversion, QCpre_write_conversion;
322Lisp_Object QCascii_compatible_p;
371 323
372extern Lisp_Object Qinsert_file_contents, Qwrite_region; 324extern Lisp_Object Qinsert_file_contents, Qwrite_region;
373Lisp_Object Qcall_process, Qcall_process_region; 325Lisp_Object Qcall_process, Qcall_process_region;
374Lisp_Object Qstart_process, Qopen_network_stream; 326Lisp_Object Qstart_process, Qopen_network_stream;
375Lisp_Object Qtarget_idx; 327Lisp_Object Qtarget_idx;
376 328
329Lisp_Object Qinsufficient_source, Qinconsistent_eol, Qinvalid_source;
330Lisp_Object Qinterrupted, Qinsufficient_memory;
331
377extern Lisp_Object Qcompletion_ignore_case; 332extern Lisp_Object Qcompletion_ignore_case;
378 333
379/* If a symbol has this property, evaluate the value to define the 334/* If a symbol has this property, evaluate the value to define the
380 symbol as a coding system. */ 335 symbol as a coding system. */
381Lisp_Object Qcoding_system_define_form; 336static Lisp_Object Qcoding_system_define_form;
382
383Lisp_Object Vselect_safe_coding_system_function;
384 337
385int coding_system_require_warning; 338int coding_system_require_warning;
386 339
340Lisp_Object Vselect_safe_coding_system_function;
341
387/* Mnemonic string for each format of end-of-line. */ 342/* Mnemonic string for each format of end-of-line. */
388Lisp_Object eol_mnemonic_unix, eol_mnemonic_dos, eol_mnemonic_mac; 343Lisp_Object eol_mnemonic_unix, eol_mnemonic_dos, eol_mnemonic_mac;
389/* Mnemonic string to indicate format of end-of-line is not yet 344/* Mnemonic string to indicate format of end-of-line is not yet
390 decided. */ 345 decided. */
391Lisp_Object eol_mnemonic_undecided; 346Lisp_Object eol_mnemonic_undecided;
392 347
393/* Format of end-of-line decided by system. This is CODING_EOL_LF on 348/* Format of end-of-line decided by system. This is Qunix on
394 Unix, CODING_EOL_CRLF on DOS/Windows, and CODING_EOL_CR on Mac. 349 Unix and Mac, Qdos on DOS/Windows.
395 This has an effect only for external encoding (i.e. for output to 350 This has an effect only for external encoding (i.e. for output to
396 file and process), not for in-buffer or Lisp string encoding. */ 351 file and process), not for in-buffer or Lisp string encoding. */
397int system_eol_type; 352static Lisp_Object system_eol_type;
398 353
399#ifdef emacs 354#ifdef emacs
400 355
401/* Information about which coding system is safe for which chars.
402 The value has the form (GENERIC-LIST . NON-GENERIC-ALIST).
403
404 GENERIC-LIST is a list of generic coding systems which can encode
405 any characters.
406
407 NON-GENERIC-ALIST is an alist of non generic coding systems vs the
408 corresponding char table that contains safe chars. */
409Lisp_Object Vcoding_system_safe_chars;
410
411Lisp_Object Vcoding_system_list, Vcoding_system_alist; 356Lisp_Object Vcoding_system_list, Vcoding_system_alist;
412 357
413Lisp_Object Qcoding_system_p, Qcoding_system_error; 358Lisp_Object Qcoding_system_p, Qcoding_system_error;
@@ -415,8 +360,7 @@ Lisp_Object Qcoding_system_p, Qcoding_system_error;
415/* Coding system emacs-mule and raw-text are for converting only 360/* Coding system emacs-mule and raw-text are for converting only
416 end-of-line format. */ 361 end-of-line format. */
417Lisp_Object Qemacs_mule, Qraw_text; 362Lisp_Object Qemacs_mule, Qraw_text;
418 363Lisp_Object Qutf_8_emacs;
419Lisp_Object Qutf_8;
420 364
421/* Coding-systems are handed between Emacs Lisp programs and C internal 365/* Coding-systems are handed between Emacs Lisp programs and C internal
422 routines by the following three variables. */ 366 routines by the following three variables. */
@@ -426,7 +370,8 @@ Lisp_Object Vcoding_system_for_read;
426Lisp_Object Vcoding_system_for_write; 370Lisp_Object Vcoding_system_for_write;
427/* Coding-system actually used in the latest I/O. */ 371/* Coding-system actually used in the latest I/O. */
428Lisp_Object Vlast_coding_system_used; 372Lisp_Object Vlast_coding_system_used;
429 373/* Set to non-nil when an error is detected while code conversion. */
374Lisp_Object Vlast_code_conversion_error;
430/* A vector of length 256 which contains information about special 375/* A vector of length 256 which contains information about special
431 Latin codes (especially for dealing with Microsoft codes). */ 376 Latin codes (especially for dealing with Microsoft codes). */
432Lisp_Object Vlatin_extra_code_table; 377Lisp_Object Vlatin_extra_code_table;
@@ -444,9 +389,6 @@ int inherit_process_coding_system;
444 terminal coding system is nil. */ 389 terminal coding system is nil. */
445struct coding_system safe_terminal_coding; 390struct coding_system safe_terminal_coding;
446 391
447/* Default coding system to be used to write a file. */
448struct coding_system default_buffer_file_coding;
449
450Lisp_Object Vfile_coding_system_alist; 392Lisp_Object Vfile_coding_system_alist;
451Lisp_Object Vprocess_coding_system_alist; 393Lisp_Object Vprocess_coding_system_alist;
452Lisp_Object Vnetwork_coding_system_alist; 394Lisp_Object Vnetwork_coding_system_alist;
@@ -455,42 +397,6 @@ Lisp_Object Vlocale_coding_system;
455 397
456#endif /* emacs */ 398#endif /* emacs */
457 399
458Lisp_Object Qcoding_category, Qcoding_category_index;
459
460/* List of symbols `coding-category-xxx' ordered by priority. */
461Lisp_Object Vcoding_category_list;
462
463/* Table of coding categories (Lisp symbols). */
464Lisp_Object Vcoding_category_table;
465
466/* Table of names of symbol for each coding-category. */
467char *coding_category_name[CODING_CATEGORY_IDX_MAX] = {
468 "coding-category-emacs-mule",
469 "coding-category-sjis",
470 "coding-category-iso-7",
471 "coding-category-iso-7-tight",
472 "coding-category-iso-8-1",
473 "coding-category-iso-8-2",
474 "coding-category-iso-7-else",
475 "coding-category-iso-8-else",
476 "coding-category-ccl",
477 "coding-category-big5",
478 "coding-category-utf-8",
479 "coding-category-utf-16-be",
480 "coding-category-utf-16-le",
481 "coding-category-raw-text",
482 "coding-category-binary"
483};
484
485/* Table of pointers to coding systems corresponding to each coding
486 categories. */
487struct coding_system *coding_system_table[CODING_CATEGORY_IDX_MAX];
488
489/* Table of coding category masks. Nth element is a mask for a coding
490 category of which priority is Nth. */
491static
492int coding_priorities[CODING_CATEGORY_IDX_MAX];
493
494/* Flag to tell if we look up translation table on character code 400/* Flag to tell if we look up translation table on character code
495 conversion. */ 401 conversion. */
496Lisp_Object Venable_character_translation; 402Lisp_Object Venable_character_translation;
@@ -505,7 +411,7 @@ Lisp_Object Qtranslation_table_for_decode;
505Lisp_Object Qtranslation_table_for_encode; 411Lisp_Object Qtranslation_table_for_encode;
506 412
507/* Alist of charsets vs revision number. */ 413/* Alist of charsets vs revision number. */
508Lisp_Object Vcharset_revision_alist; 414static Lisp_Object Vcharset_revision_table;
509 415
510/* Default coding systems used for process I/O. */ 416/* Default coding systems used for process I/O. */
511Lisp_Object Vdefault_process_coding_system; 417Lisp_Object Vdefault_process_coding_system;
@@ -513,34 +419,1200 @@ Lisp_Object Vdefault_process_coding_system;
513/* Char table for translating Quail and self-inserting input. */ 419/* Char table for translating Quail and self-inserting input. */
514Lisp_Object Vtranslation_table_for_input; 420Lisp_Object Vtranslation_table_for_input;
515 421
516/* Global flag to tell that we can't call post-read-conversion and 422/* Two special coding systems. */
517 pre-write-conversion functions. Usually the value is zero, but it 423Lisp_Object Vsjis_coding_system;
518 is set to 1 temporarily while such functions are running. This is 424Lisp_Object Vbig5_coding_system;
519 to avoid infinite recursive call. */ 425
520static int inhibit_pre_post_conversion; 426/* ISO2022 section */
427
428#define CODING_ISO_INITIAL(coding, reg) \
429 (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \
430 coding_attr_iso_initial), \
431 reg)))
432
433
434#define CODING_ISO_REQUEST(coding, charset_id) \
435 ((charset_id <= (coding)->max_charset_id \
436 ? (coding)->safe_charsets[charset_id] \
437 : -1))
438
439
440#define CODING_ISO_FLAGS(coding) \
441 ((coding)->spec.iso_2022.flags)
442#define CODING_ISO_DESIGNATION(coding, reg) \
443 ((coding)->spec.iso_2022.current_designation[reg])
444#define CODING_ISO_INVOCATION(coding, plane) \
445 ((coding)->spec.iso_2022.current_invocation[plane])
446#define CODING_ISO_SINGLE_SHIFTING(coding) \
447 ((coding)->spec.iso_2022.single_shifting)
448#define CODING_ISO_BOL(coding) \
449 ((coding)->spec.iso_2022.bol)
450#define CODING_ISO_INVOKED_CHARSET(coding, plane) \
451 CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane)))
452
453/* Control characters of ISO2022. */
454 /* code */ /* function */
455#define ISO_CODE_LF 0x0A /* line-feed */
456#define ISO_CODE_CR 0x0D /* carriage-return */
457#define ISO_CODE_SO 0x0E /* shift-out */
458#define ISO_CODE_SI 0x0F /* shift-in */
459#define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */
460#define ISO_CODE_ESC 0x1B /* escape */
461#define ISO_CODE_SS2 0x8E /* single-shift-2 */
462#define ISO_CODE_SS3 0x8F /* single-shift-3 */
463#define ISO_CODE_CSI 0x9B /* control-sequence-introducer */
464
465/* All code (1-byte) of ISO2022 is classified into one of the
466 followings. */
467enum iso_code_class_type
468 {
469 ISO_control_0, /* Control codes in the range
470 0x00..0x1F and 0x7F, except for the
471 following 5 codes. */
472 ISO_shift_out, /* ISO_CODE_SO (0x0E) */
473 ISO_shift_in, /* ISO_CODE_SI (0x0F) */
474 ISO_single_shift_2_7, /* ISO_CODE_SS2_7 (0x19) */
475 ISO_escape, /* ISO_CODE_SO (0x1B) */
476 ISO_control_1, /* Control codes in the range
477 0x80..0x9F, except for the
478 following 3 codes. */
479 ISO_single_shift_2, /* ISO_CODE_SS2 (0x8E) */
480 ISO_single_shift_3, /* ISO_CODE_SS3 (0x8F) */
481 ISO_control_sequence_introducer, /* ISO_CODE_CSI (0x9B) */
482 ISO_0x20_or_0x7F, /* Codes of the values 0x20 or 0x7F. */
483 ISO_graphic_plane_0, /* Graphic codes in the range 0x21..0x7E. */
484 ISO_0xA0_or_0xFF, /* Codes of the values 0xA0 or 0xFF. */
485 ISO_graphic_plane_1 /* Graphic codes in the range 0xA1..0xFE. */
486 };
521 487
522Lisp_Object Qchar_coding_system; 488/** The macros CODING_ISO_FLAG_XXX defines a flag bit of the
489 `iso-flags' attribute of an iso2022 coding system. */
523 490
524/* Return `safe-chars' property of CODING_SYSTEM (symbol). Don't check 491/* If set, produce long-form designation sequence (e.g. ESC $ ( A)
525 its validity. */ 492 instead of the correct short-form sequence (e.g. ESC $ A). */
493#define CODING_ISO_FLAG_LONG_FORM 0x0001
526 494
527Lisp_Object 495/* If set, reset graphic planes and registers at end-of-line to the
528coding_safe_chars (coding_system) 496 initial state. */
529 Lisp_Object coding_system; 497#define CODING_ISO_FLAG_RESET_AT_EOL 0x0002
498
499/* If set, reset graphic planes and registers before any control
500 characters to the initial state. */
501#define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004
502
503/* If set, encode by 7-bit environment. */
504#define CODING_ISO_FLAG_SEVEN_BITS 0x0008
505
506/* If set, use locking-shift function. */
507#define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010
508
509/* If set, use single-shift function. Overwrite
510 CODING_ISO_FLAG_LOCKING_SHIFT. */
511#define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020
512
513/* If set, use designation escape sequence. */
514#define CODING_ISO_FLAG_DESIGNATION 0x0040
515
516/* If set, produce revision number sequence. */
517#define CODING_ISO_FLAG_REVISION 0x0080
518
519/* If set, produce ISO6429's direction specifying sequence. */
520#define CODING_ISO_FLAG_DIRECTION 0x0100
521
522/* If set, assume designation states are reset at beginning of line on
523 output. */
524#define CODING_ISO_FLAG_INIT_AT_BOL 0x0200
525
526/* If set, designation sequence should be placed at beginning of line
527 on output. */
528#define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400
529
530/* If set, do not encode unsafe charactes on output. */
531#define CODING_ISO_FLAG_SAFE 0x0800
532
533/* If set, extra latin codes (128..159) are accepted as a valid code
534 on input. */
535#define CODING_ISO_FLAG_LATIN_EXTRA 0x1000
536
537#define CODING_ISO_FLAG_COMPOSITION 0x2000
538
539#define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000
540
541#define CODING_ISO_FLAG_USE_ROMAN 0x8000
542
543#define CODING_ISO_FLAG_USE_OLDJIS 0x10000
544
545#define CODING_ISO_FLAG_FULL_SUPPORT 0x100000
546
547/* A character to be produced on output if encoding of the original
548 character is prohibited by CODING_ISO_FLAG_SAFE. */
549#define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?'
550
551
552/* UTF-16 section */
553#define CODING_UTF_16_BOM(coding) \
554 ((coding)->spec.utf_16.bom)
555
556#define CODING_UTF_16_ENDIAN(coding) \
557 ((coding)->spec.utf_16.endian)
558
559#define CODING_UTF_16_SURROGATE(coding) \
560 ((coding)->spec.utf_16.surrogate)
561
562
563/* CCL section */
564#define CODING_CCL_DECODER(coding) \
565 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder)
566#define CODING_CCL_ENCODER(coding) \
567 AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder)
568#define CODING_CCL_VALIDS(coding) \
569 (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids)))
570
571/* Index for each coding category in `coding_categories' */
572
573enum coding_category
574 {
575 coding_category_iso_7,
576 coding_category_iso_7_tight,
577 coding_category_iso_8_1,
578 coding_category_iso_8_2,
579 coding_category_iso_7_else,
580 coding_category_iso_8_else,
581 coding_category_utf_8,
582 coding_category_utf_16_auto,
583 coding_category_utf_16_be,
584 coding_category_utf_16_le,
585 coding_category_utf_16_be_nosig,
586 coding_category_utf_16_le_nosig,
587 coding_category_charset,
588 coding_category_sjis,
589 coding_category_big5,
590 coding_category_ccl,
591 coding_category_emacs_mule,
592 /* All above are targets of code detection. */
593 coding_category_raw_text,
594 coding_category_undecided,
595 coding_category_max
596 };
597
598/* Definitions of flag bits used in detect_coding_XXXX. */
599#define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7)
600#define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight)
601#define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1)
602#define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2)
603#define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else)
604#define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else)
605#define CATEGORY_MASK_UTF_8 (1 << coding_category_utf_8)
606#define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto)
607#define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be)
608#define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le)
609#define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig)
610#define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig)
611#define CATEGORY_MASK_CHARSET (1 << coding_category_charset)
612#define CATEGORY_MASK_SJIS (1 << coding_category_sjis)
613#define CATEGORY_MASK_BIG5 (1 << coding_category_big5)
614#define CATEGORY_MASK_CCL (1 << coding_category_ccl)
615#define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule)
616#define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text)
617
618/* This value is returned if detect_coding_mask () find nothing other
619 than ASCII characters. */
620#define CATEGORY_MASK_ANY \
621 (CATEGORY_MASK_ISO_7 \
622 | CATEGORY_MASK_ISO_7_TIGHT \
623 | CATEGORY_MASK_ISO_8_1 \
624 | CATEGORY_MASK_ISO_8_2 \
625 | CATEGORY_MASK_ISO_7_ELSE \
626 | CATEGORY_MASK_ISO_8_ELSE \
627 | CATEGORY_MASK_UTF_8 \
628 | CATEGORY_MASK_UTF_16_BE \
629 | CATEGORY_MASK_UTF_16_LE \
630 | CATEGORY_MASK_UTF_16_BE_NOSIG \
631 | CATEGORY_MASK_UTF_16_LE_NOSIG \
632 | CATEGORY_MASK_CHARSET \
633 | CATEGORY_MASK_SJIS \
634 | CATEGORY_MASK_BIG5 \
635 | CATEGORY_MASK_CCL \
636 | CATEGORY_MASK_EMACS_MULE)
637
638
639#define CATEGORY_MASK_ISO_7BIT \
640 (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT)
641
642#define CATEGORY_MASK_ISO_8BIT \
643 (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2)
644
645#define CATEGORY_MASK_ISO_ELSE \
646 (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE)
647
648#define CATEGORY_MASK_ISO_ESCAPE \
649 (CATEGORY_MASK_ISO_7 \
650 | CATEGORY_MASK_ISO_7_TIGHT \
651 | CATEGORY_MASK_ISO_7_ELSE \
652 | CATEGORY_MASK_ISO_8_ELSE)
653
654#define CATEGORY_MASK_ISO \
655 ( CATEGORY_MASK_ISO_7BIT \
656 | CATEGORY_MASK_ISO_8BIT \
657 | CATEGORY_MASK_ISO_ELSE)
658
659#define CATEGORY_MASK_UTF_16 \
660 (CATEGORY_MASK_UTF_16_BE \
661 | CATEGORY_MASK_UTF_16_LE \
662 | CATEGORY_MASK_UTF_16_BE_NOSIG \
663 | CATEGORY_MASK_UTF_16_LE_NOSIG)
664
665
666/* List of symbols `coding-category-xxx' ordered by priority. This
667 variable is exposed to Emacs Lisp. */
668static Lisp_Object Vcoding_category_list;
669
670/* Table of coding categories (Lisp symbols). This variable is for
671 internal use oly. */
672static Lisp_Object Vcoding_category_table;
673
674/* Table of coding-categories ordered by priority. */
675static enum coding_category coding_priorities[coding_category_max];
676
677/* Nth element is a coding context for the coding system bound to the
678 Nth coding category. */
679static struct coding_system coding_categories[coding_category_max];
680
681/*** Commonly used macros and functions ***/
682
683#ifndef min
684#define min(a, b) ((a) < (b) ? (a) : (b))
685#endif
686#ifndef max
687#define max(a, b) ((a) > (b) ? (a) : (b))
688#endif
689
690#define CODING_GET_INFO(coding, attrs, charset_list) \
691 do { \
692 (attrs) = CODING_ID_ATTRS ((coding)->id); \
693 (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \
694 } while (0)
695
696
697/* Safely get one byte from the source text pointed by SRC which ends
698 at SRC_END, and set C to that byte. If there are not enough bytes
699 in the source, it jumps to `no_more_source'. If multibytep is
700 nonzero, and a multibyte character is found at SRC, set C to the
701 negative value of the character code. The caller should declare
702 and set these variables appropriately in advance:
703 src, src_end, multibytep */
704
705#define ONE_MORE_BYTE(c) \
706 do { \
707 if (src == src_end) \
708 { \
709 if (src_base < src) \
710 record_conversion_result \
711 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
712 goto no_more_source; \
713 } \
714 c = *src++; \
715 if (multibytep && (c & 0x80)) \
716 { \
717 if ((c & 0xFE) == 0xC0) \
718 c = ((c & 1) << 6) | *src++; \
719 else \
720 { \
721 src--; \
722 c = - string_char (src, &src, NULL); \
723 record_conversion_result \
724 (coding, CODING_RESULT_INVALID_SRC); \
725 } \
726 } \
727 consumed_chars++; \
728 } while (0)
729
730
731#define ONE_MORE_BYTE_NO_CHECK(c) \
732 do { \
733 c = *src++; \
734 if (multibytep && (c & 0x80)) \
735 { \
736 if ((c & 0xFE) == 0xC0) \
737 c = ((c & 1) << 6) | *src++; \
738 else \
739 { \
740 src--; \
741 c = - string_char (src, &src, NULL); \
742 record_conversion_result \
743 (coding, CODING_RESULT_INVALID_SRC); \
744 } \
745 } \
746 consumed_chars++; \
747 } while (0)
748
749
750/* Store a byte C in the place pointed by DST and increment DST to the
751 next free point, and increment PRODUCED_CHARS. The caller should
752 assure that C is 0..127, and declare and set the variable `dst'
753 appropriately in advance.
754*/
755
756
757#define EMIT_ONE_ASCII_BYTE(c) \
758 do { \
759 produced_chars++; \
760 *dst++ = (c); \
761 } while (0)
762
763
764/* Like EMIT_ONE_ASCII_BYTE byt store two bytes; C1 and C2. */
765
766#define EMIT_TWO_ASCII_BYTES(c1, c2) \
767 do { \
768 produced_chars += 2; \
769 *dst++ = (c1), *dst++ = (c2); \
770 } while (0)
771
772
773/* Store a byte C in the place pointed by DST and increment DST to the
774 next free point, and increment PRODUCED_CHARS. If MULTIBYTEP is
775 nonzero, store in an appropriate multibyte from. The caller should
776 declare and set the variables `dst' and `multibytep' appropriately
777 in advance. */
778
779#define EMIT_ONE_BYTE(c) \
780 do { \
781 produced_chars++; \
782 if (multibytep) \
783 { \
784 int ch = (c); \
785 if (ch >= 0x80) \
786 ch = BYTE8_TO_CHAR (ch); \
787 CHAR_STRING_ADVANCE (ch, dst); \
788 } \
789 else \
790 *dst++ = (c); \
791 } while (0)
792
793
794/* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */
795
796#define EMIT_TWO_BYTES(c1, c2) \
797 do { \
798 produced_chars += 2; \
799 if (multibytep) \
800 { \
801 int ch; \
802 \
803 ch = (c1); \
804 if (ch >= 0x80) \
805 ch = BYTE8_TO_CHAR (ch); \
806 CHAR_STRING_ADVANCE (ch, dst); \
807 ch = (c2); \
808 if (ch >= 0x80) \
809 ch = BYTE8_TO_CHAR (ch); \
810 CHAR_STRING_ADVANCE (ch, dst); \
811 } \
812 else \
813 { \
814 *dst++ = (c1); \
815 *dst++ = (c2); \
816 } \
817 } while (0)
818
819
820#define EMIT_THREE_BYTES(c1, c2, c3) \
821 do { \
822 EMIT_ONE_BYTE (c1); \
823 EMIT_TWO_BYTES (c2, c3); \
824 } while (0)
825
826
827#define EMIT_FOUR_BYTES(c1, c2, c3, c4) \
828 do { \
829 EMIT_TWO_BYTES (c1, c2); \
830 EMIT_TWO_BYTES (c3, c4); \
831 } while (0)
832
833
834/* Prototypes for static functions. */
835static void record_conversion_result P_ ((struct coding_system *coding,
836 enum coding_result_code result));
837static int detect_coding_utf_8 P_ ((struct coding_system *,
838 struct coding_detection_info *info));
839static void decode_coding_utf_8 P_ ((struct coding_system *));
840static int encode_coding_utf_8 P_ ((struct coding_system *));
841
842static int detect_coding_utf_16 P_ ((struct coding_system *,
843 struct coding_detection_info *info));
844static void decode_coding_utf_16 P_ ((struct coding_system *));
845static int encode_coding_utf_16 P_ ((struct coding_system *));
846
847static int detect_coding_iso_2022 P_ ((struct coding_system *,
848 struct coding_detection_info *info));
849static void decode_coding_iso_2022 P_ ((struct coding_system *));
850static int encode_coding_iso_2022 P_ ((struct coding_system *));
851
852static int detect_coding_emacs_mule P_ ((struct coding_system *,
853 struct coding_detection_info *info));
854static void decode_coding_emacs_mule P_ ((struct coding_system *));
855static int encode_coding_emacs_mule P_ ((struct coding_system *));
856
857static int detect_coding_sjis P_ ((struct coding_system *,
858 struct coding_detection_info *info));
859static void decode_coding_sjis P_ ((struct coding_system *));
860static int encode_coding_sjis P_ ((struct coding_system *));
861
862static int detect_coding_big5 P_ ((struct coding_system *,
863 struct coding_detection_info *info));
864static void decode_coding_big5 P_ ((struct coding_system *));
865static int encode_coding_big5 P_ ((struct coding_system *));
866
867static int detect_coding_ccl P_ ((struct coding_system *,
868 struct coding_detection_info *info));
869static void decode_coding_ccl P_ ((struct coding_system *));
870static int encode_coding_ccl P_ ((struct coding_system *));
871
872static void decode_coding_raw_text P_ ((struct coding_system *));
873static int encode_coding_raw_text P_ ((struct coding_system *));
874
875static void coding_set_source P_ ((struct coding_system *));
876static void coding_set_destination P_ ((struct coding_system *));
877static void coding_alloc_by_realloc P_ ((struct coding_system *, EMACS_INT));
878static void coding_alloc_by_making_gap P_ ((struct coding_system *,
879 EMACS_INT, EMACS_INT));
880static unsigned char *alloc_destination P_ ((struct coding_system *,
881 EMACS_INT, unsigned char *));
882static void setup_iso_safe_charsets P_ ((Lisp_Object));
883static unsigned char *encode_designation_at_bol P_ ((struct coding_system *,
884 int *, int *,
885 unsigned char *));
886static int detect_eol P_ ((const unsigned char *,
887 EMACS_INT, enum coding_category));
888static Lisp_Object adjust_coding_eol_type P_ ((struct coding_system *, int));
889static void decode_eol P_ ((struct coding_system *));
890static Lisp_Object get_translation_table P_ ((Lisp_Object, int, int *));
891static Lisp_Object get_translation P_ ((Lisp_Object, int *, int *,
892 int, int *, int *));
893static int produce_chars P_ ((struct coding_system *, Lisp_Object, int));
894static INLINE void produce_composition P_ ((struct coding_system *, int *,
895 EMACS_INT));
896static INLINE void produce_charset P_ ((struct coding_system *, int *,
897 EMACS_INT));
898static void produce_annotation P_ ((struct coding_system *, EMACS_INT));
899static int decode_coding P_ ((struct coding_system *));
900static INLINE int *handle_composition_annotation P_ ((EMACS_INT, EMACS_INT,
901 struct coding_system *,
902 int *, EMACS_INT *));
903static INLINE int *handle_charset_annotation P_ ((EMACS_INT, EMACS_INT,
904 struct coding_system *,
905 int *, EMACS_INT *));
906static void consume_chars P_ ((struct coding_system *, Lisp_Object, int));
907static int encode_coding P_ ((struct coding_system *));
908static Lisp_Object make_conversion_work_buffer P_ ((int));
909static Lisp_Object code_conversion_restore P_ ((Lisp_Object));
910static INLINE int char_encodable_p P_ ((int, Lisp_Object));
911static Lisp_Object make_subsidiaries P_ ((Lisp_Object));
912
913static void
914record_conversion_result (struct coding_system *coding,
915 enum coding_result_code result)
530{ 916{
531 Lisp_Object coding_spec, plist, safe_chars; 917 coding->result = result;
918 switch (result)
919 {
920 case CODING_RESULT_INSUFFICIENT_SRC:
921 Vlast_code_conversion_error = Qinsufficient_source;
922 break;
923 case CODING_RESULT_INCONSISTENT_EOL:
924 Vlast_code_conversion_error = Qinconsistent_eol;
925 break;
926 case CODING_RESULT_INVALID_SRC:
927 Vlast_code_conversion_error = Qinvalid_source;
928 break;
929 case CODING_RESULT_INTERRUPT:
930 Vlast_code_conversion_error = Qinterrupted;
931 break;
932 case CODING_RESULT_INSUFFICIENT_MEM:
933 Vlast_code_conversion_error = Qinsufficient_memory;
934 break;
935 default:
936 Vlast_code_conversion_error = intern ("Unknown error");
937 }
938}
939
940#define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \
941 do { \
942 charset_map_loaded = 0; \
943 c = DECODE_CHAR (charset, code); \
944 if (charset_map_loaded) \
945 { \
946 const unsigned char *orig = coding->source; \
947 EMACS_INT offset; \
948 \
949 coding_set_source (coding); \
950 offset = coding->source - orig; \
951 src += offset; \
952 src_base += offset; \
953 src_end += offset; \
954 } \
955 } while (0)
956
957
958#define ASSURE_DESTINATION(bytes) \
959 do { \
960 if (dst + (bytes) >= dst_end) \
961 { \
962 int more_bytes = charbuf_end - charbuf + (bytes); \
963 \
964 dst = alloc_destination (coding, more_bytes, dst); \
965 dst_end = coding->destination + coding->dst_bytes; \
966 } \
967 } while (0)
968
969
970
971static void
972coding_set_source (coding)
973 struct coding_system *coding;
974{
975 if (BUFFERP (coding->src_object))
976 {
977 struct buffer *buf = XBUFFER (coding->src_object);
978
979 if (coding->src_pos < 0)
980 coding->source = BUF_GAP_END_ADDR (buf) + coding->src_pos_byte;
981 else
982 coding->source = BUF_BYTE_ADDRESS (buf, coding->src_pos_byte);
983 }
984 else if (STRINGP (coding->src_object))
985 {
986 coding->source = SDATA (coding->src_object) + coding->src_pos_byte;
987 }
988 else
989 /* Otherwise, the source is C string and is never relocated
990 automatically. Thus we don't have to update anything. */
991 ;
992}
993
994static void
995coding_set_destination (coding)
996 struct coding_system *coding;
997{
998 if (BUFFERP (coding->dst_object))
999 {
1000 if (coding->src_pos < 0)
1001 {
1002 coding->destination = BEG_ADDR + coding->dst_pos_byte - 1;
1003 coding->dst_bytes = (GAP_END_ADDR
1004 - (coding->src_bytes - coding->consumed)
1005 - coding->destination);
1006 }
1007 else
1008 {
1009 /* We are sure that coding->dst_pos_byte is before the gap
1010 of the buffer. */
1011 coding->destination = (BUF_BEG_ADDR (XBUFFER (coding->dst_object))
1012 + coding->dst_pos_byte - 1);
1013 coding->dst_bytes = (BUF_GAP_END_ADDR (XBUFFER (coding->dst_object))
1014 - coding->destination);
1015 }
1016 }
1017 else
1018 /* Otherwise, the destination is C string and is never relocated
1019 automatically. Thus we don't have to update anything. */
1020 ;
1021}
1022
1023
1024static void
1025coding_alloc_by_realloc (coding, bytes)
1026 struct coding_system *coding;
1027 EMACS_INT bytes;
1028{
1029 coding->destination = (unsigned char *) xrealloc (coding->destination,
1030 coding->dst_bytes + bytes);
1031 coding->dst_bytes += bytes;
1032}
1033
1034static void
1035coding_alloc_by_making_gap (coding, offset, bytes)
1036 struct coding_system *coding;
1037 EMACS_INT offset, bytes;
1038{
1039 if (BUFFERP (coding->dst_object)
1040 && EQ (coding->src_object, coding->dst_object))
1041 {
1042 EMACS_INT add = offset + (coding->src_bytes - coding->consumed);
532 1043
533 coding_spec = Fget (coding_system, Qcoding_system); 1044 GPT += offset, GPT_BYTE += offset;
534 plist = XVECTOR (coding_spec)->contents[3]; 1045 GAP_SIZE -= add; ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add;
535 safe_chars = Fplist_get (XVECTOR (coding_spec)->contents[3], Qsafe_chars); 1046 make_gap (bytes);
536 return (CHAR_TABLE_P (safe_chars) ? safe_chars : Qt); 1047 GAP_SIZE += add; ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add;
1048 GPT -= offset, GPT_BYTE -= offset;
1049 }
1050 else
1051 {
1052 Lisp_Object this_buffer;
1053
1054 this_buffer = Fcurrent_buffer ();
1055 set_buffer_internal (XBUFFER (coding->dst_object));
1056 make_gap (bytes);
1057 set_buffer_internal (XBUFFER (this_buffer));
1058 }
537} 1059}
538 1060
539#define CODING_SAFE_CHAR_P(safe_chars, c) \ 1061
540 (EQ (safe_chars, Qt) || !NILP (CHAR_TABLE_REF (safe_chars, c))) 1062static unsigned char *
1063alloc_destination (coding, nbytes, dst)
1064 struct coding_system *coding;
1065 EMACS_INT nbytes;
1066 unsigned char *dst;
1067{
1068 EMACS_INT offset = dst - coding->destination;
1069
1070 if (BUFFERP (coding->dst_object))
1071 coding_alloc_by_making_gap (coding, offset, nbytes);
1072 else
1073 coding_alloc_by_realloc (coding, nbytes);
1074 record_conversion_result (coding, CODING_RESULT_SUCCESS);
1075 coding_set_destination (coding);
1076 dst = coding->destination + offset;
1077 return dst;
1078}
1079
1080/** Macros for annotations. */
1081
1082/* Maximum length of annotation data (sum of annotations for
1083 composition and charset). */
1084#define MAX_ANNOTATION_LENGTH (4 + (MAX_COMPOSITION_COMPONENTS * 2) - 1 + 4)
1085
1086/* An annotation data is stored in the array coding->charbuf in this
1087 format:
1088 [ -LENGTH ANNOTATION_MASK NCHARS ... ]
1089 LENGTH is the number of elements in the annotation.
1090 ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK.
1091 NCHARS is the number of characters in the text annotated.
1092
1093 The format of the following elements depend on ANNOTATION_MASK.
1094
1095 In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements
1096 follows:
1097 ... METHOD [ COMPOSITION-COMPONENTS ... ]
1098 METHOD is one of enum composition_method.
1099 Optionnal COMPOSITION-COMPONENTS are characters and composition
1100 rules.
1101
1102 In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID
1103 follows. */
1104
1105#define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \
1106 do { \
1107 *(buf)++ = -(len); \
1108 *(buf)++ = (mask); \
1109 *(buf)++ = (nchars); \
1110 coding->annotated = 1; \
1111 } while (0);
1112
1113#define ADD_COMPOSITION_DATA(buf, nchars, method) \
1114 do { \
1115 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \
1116 *buf++ = method; \
1117 } while (0)
1118
1119
1120#define ADD_CHARSET_DATA(buf, nchars, id) \
1121 do { \
1122 ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \
1123 *buf++ = id; \
1124 } while (0)
1125
1126
1127/*** 2. Emacs' internal format (emacs-utf-8) ***/
1128
1129
541 1130
542 1131
543/*** 2. Emacs internal format (emacs-mule) handlers ***/ 1132/*** 3. UTF-8 ***/
1133
1134/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1135 Check if a text is encoded in UTF-8. If it is, return 1, else
1136 return 0. */
1137
1138#define UTF_8_1_OCTET_P(c) ((c) < 0x80)
1139#define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
1140#define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
1141#define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
1142#define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
1143#define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
1144
1145static int
1146detect_coding_utf_8 (coding, detect_info)
1147 struct coding_system *coding;
1148 struct coding_detection_info *detect_info;
1149{
1150 const unsigned char *src = coding->source, *src_base;
1151 const unsigned char *src_end = coding->source + coding->src_bytes;
1152 int multibytep = coding->src_multibyte;
1153 int consumed_chars = 0;
1154 int found = 0;
1155
1156 detect_info->checked |= CATEGORY_MASK_UTF_8;
1157 /* A coding system of this category is always ASCII compatible. */
1158 src += coding->head_ascii;
1159
1160 while (1)
1161 {
1162 int c, c1, c2, c3, c4;
1163
1164 src_base = src;
1165 ONE_MORE_BYTE (c);
1166 if (c < 0 || UTF_8_1_OCTET_P (c))
1167 continue;
1168 ONE_MORE_BYTE (c1);
1169 if (c1 < 0 || ! UTF_8_EXTRA_OCTET_P (c1))
1170 break;
1171 if (UTF_8_2_OCTET_LEADING_P (c))
1172 {
1173 found = CATEGORY_MASK_UTF_8;
1174 continue;
1175 }
1176 ONE_MORE_BYTE (c2);
1177 if (c2 < 0 || ! UTF_8_EXTRA_OCTET_P (c2))
1178 break;
1179 if (UTF_8_3_OCTET_LEADING_P (c))
1180 {
1181 found = CATEGORY_MASK_UTF_8;
1182 continue;
1183 }
1184 ONE_MORE_BYTE (c3);
1185 if (c3 < 0 || ! UTF_8_EXTRA_OCTET_P (c3))
1186 break;
1187 if (UTF_8_4_OCTET_LEADING_P (c))
1188 {
1189 found = CATEGORY_MASK_UTF_8;
1190 continue;
1191 }
1192 ONE_MORE_BYTE (c4);
1193 if (c4 < 0 || ! UTF_8_EXTRA_OCTET_P (c4))
1194 break;
1195 if (UTF_8_5_OCTET_LEADING_P (c))
1196 {
1197 found = CATEGORY_MASK_UTF_8;
1198 continue;
1199 }
1200 break;
1201 }
1202 detect_info->rejected |= CATEGORY_MASK_UTF_8;
1203 return 0;
1204
1205 no_more_source:
1206 if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK)
1207 {
1208 detect_info->rejected |= CATEGORY_MASK_UTF_8;
1209 return 0;
1210 }
1211 detect_info->found |= found;
1212 return 1;
1213}
1214
1215
1216static void
1217decode_coding_utf_8 (coding)
1218 struct coding_system *coding;
1219{
1220 const unsigned char *src = coding->source + coding->consumed;
1221 const unsigned char *src_end = coding->source + coding->src_bytes;
1222 const unsigned char *src_base;
1223 int *charbuf = coding->charbuf + coding->charbuf_used;
1224 int *charbuf_end = coding->charbuf + coding->charbuf_size;
1225 int consumed_chars = 0, consumed_chars_base;
1226 int multibytep = coding->src_multibyte;
1227 Lisp_Object attr, charset_list;
1228
1229 CODING_GET_INFO (coding, attr, charset_list);
1230
1231 while (1)
1232 {
1233 int c, c1, c2, c3, c4, c5;
1234
1235 src_base = src;
1236 consumed_chars_base = consumed_chars;
1237
1238 if (charbuf >= charbuf_end)
1239 break;
1240
1241 ONE_MORE_BYTE (c1);
1242 if (c1 < 0)
1243 {
1244 c = - c1;
1245 }
1246 else if (UTF_8_1_OCTET_P(c1))
1247 {
1248 c = c1;
1249 }
1250 else
1251 {
1252 ONE_MORE_BYTE (c2);
1253 if (c2 < 0 || ! UTF_8_EXTRA_OCTET_P (c2))
1254 goto invalid_code;
1255 if (UTF_8_2_OCTET_LEADING_P (c1))
1256 {
1257 c = ((c1 & 0x1F) << 6) | (c2 & 0x3F);
1258 /* Reject overlong sequences here and below. Encoders
1259 producing them are incorrect, they can be misleading,
1260 and they mess up read/write invariance. */
1261 if (c < 128)
1262 goto invalid_code;
1263 }
1264 else
1265 {
1266 ONE_MORE_BYTE (c3);
1267 if (c3 < 0 || ! UTF_8_EXTRA_OCTET_P (c3))
1268 goto invalid_code;
1269 if (UTF_8_3_OCTET_LEADING_P (c1))
1270 {
1271 c = (((c1 & 0xF) << 12)
1272 | ((c2 & 0x3F) << 6) | (c3 & 0x3F));
1273 if (c < 0x800
1274 || (c >= 0xd800 && c < 0xe000)) /* surrogates (invalid) */
1275 goto invalid_code;
1276 }
1277 else
1278 {
1279 ONE_MORE_BYTE (c4);
1280 if (c4 < 0 || ! UTF_8_EXTRA_OCTET_P (c4))
1281 goto invalid_code;
1282 if (UTF_8_4_OCTET_LEADING_P (c1))
1283 {
1284 c = (((c1 & 0x7) << 18) | ((c2 & 0x3F) << 12)
1285 | ((c3 & 0x3F) << 6) | (c4 & 0x3F));
1286 if (c < 0x10000)
1287 goto invalid_code;
1288 }
1289 else
1290 {
1291 ONE_MORE_BYTE (c5);
1292 if (c5 < 0 || ! UTF_8_EXTRA_OCTET_P (c5))
1293 goto invalid_code;
1294 if (UTF_8_5_OCTET_LEADING_P (c1))
1295 {
1296 c = (((c1 & 0x3) << 24) | ((c2 & 0x3F) << 18)
1297 | ((c3 & 0x3F) << 12) | ((c4 & 0x3F) << 6)
1298 | (c5 & 0x3F));
1299 if ((c > MAX_CHAR) || (c < 0x200000))
1300 goto invalid_code;
1301 }
1302 else
1303 goto invalid_code;
1304 }
1305 }
1306 }
1307 }
1308
1309 *charbuf++ = c;
1310 continue;
1311
1312 invalid_code:
1313 src = src_base;
1314 consumed_chars = consumed_chars_base;
1315 ONE_MORE_BYTE (c);
1316 *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
1317 coding->errors++;
1318 }
1319
1320 no_more_source:
1321 coding->consumed_char += consumed_chars_base;
1322 coding->consumed = src_base - coding->source;
1323 coding->charbuf_used = charbuf - coding->charbuf;
1324}
1325
1326
1327static int
1328encode_coding_utf_8 (coding)
1329 struct coding_system *coding;
1330{
1331 int multibytep = coding->dst_multibyte;
1332 int *charbuf = coding->charbuf;
1333 int *charbuf_end = charbuf + coding->charbuf_used;
1334 unsigned char *dst = coding->destination + coding->produced;
1335 unsigned char *dst_end = coding->destination + coding->dst_bytes;
1336 int produced_chars = 0;
1337 int c;
1338
1339 if (multibytep)
1340 {
1341 int safe_room = MAX_MULTIBYTE_LENGTH * 2;
1342
1343 while (charbuf < charbuf_end)
1344 {
1345 unsigned char str[MAX_MULTIBYTE_LENGTH], *p, *pend = str;
1346
1347 ASSURE_DESTINATION (safe_room);
1348 c = *charbuf++;
1349 if (CHAR_BYTE8_P (c))
1350 {
1351 c = CHAR_TO_BYTE8 (c);
1352 EMIT_ONE_BYTE (c);
1353 }
1354 else
1355 {
1356 CHAR_STRING_ADVANCE (c, pend);
1357 for (p = str; p < pend; p++)
1358 EMIT_ONE_BYTE (*p);
1359 }
1360 }
1361 }
1362 else
1363 {
1364 int safe_room = MAX_MULTIBYTE_LENGTH;
1365
1366 while (charbuf < charbuf_end)
1367 {
1368 ASSURE_DESTINATION (safe_room);
1369 c = *charbuf++;
1370 if (CHAR_BYTE8_P (c))
1371 *dst++ = CHAR_TO_BYTE8 (c);
1372 else
1373 dst += CHAR_STRING (c, dst);
1374 produced_chars++;
1375 }
1376 }
1377 record_conversion_result (coding, CODING_RESULT_SUCCESS);
1378 coding->produced_char += produced_chars;
1379 coding->produced = dst - coding->destination;
1380 return 0;
1381}
1382
1383
1384/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1385 Check if a text is encoded in one of UTF-16 based coding systems.
1386 If it is, return 1, else return 0. */
1387
1388#define UTF_16_HIGH_SURROGATE_P(val) \
1389 (((val) & 0xFC00) == 0xD800)
1390
1391#define UTF_16_LOW_SURROGATE_P(val) \
1392 (((val) & 0xFC00) == 0xDC00)
1393
1394#define UTF_16_INVALID_P(val) \
1395 (((val) == 0xFFFE) \
1396 || ((val) == 0xFFFF) \
1397 || UTF_16_LOW_SURROGATE_P (val))
1398
1399
1400static int
1401detect_coding_utf_16 (coding, detect_info)
1402 struct coding_system *coding;
1403 struct coding_detection_info *detect_info;
1404{
1405 const unsigned char *src = coding->source, *src_base = src;
1406 const unsigned char *src_end = coding->source + coding->src_bytes;
1407 int multibytep = coding->src_multibyte;
1408 int consumed_chars = 0;
1409 int c1, c2;
1410
1411 detect_info->checked |= CATEGORY_MASK_UTF_16;
1412 if (coding->mode & CODING_MODE_LAST_BLOCK
1413 && (coding->src_chars & 1))
1414 {
1415 detect_info->rejected |= CATEGORY_MASK_UTF_16;
1416 return 0;
1417 }
1418
1419 ONE_MORE_BYTE (c1);
1420 ONE_MORE_BYTE (c2);
1421 if ((c1 == 0xFF) && (c2 == 0xFE))
1422 {
1423 detect_info->found |= (CATEGORY_MASK_UTF_16_LE
1424 | CATEGORY_MASK_UTF_16_AUTO);
1425 detect_info->rejected |= (CATEGORY_MASK_UTF_16_BE
1426 | CATEGORY_MASK_UTF_16_BE_NOSIG
1427 | CATEGORY_MASK_UTF_16_LE_NOSIG);
1428 }
1429 else if ((c1 == 0xFE) && (c2 == 0xFF))
1430 {
1431 detect_info->found |= (CATEGORY_MASK_UTF_16_BE
1432 | CATEGORY_MASK_UTF_16_AUTO);
1433 detect_info->rejected |= (CATEGORY_MASK_UTF_16_LE
1434 | CATEGORY_MASK_UTF_16_BE_NOSIG
1435 | CATEGORY_MASK_UTF_16_LE_NOSIG);
1436 }
1437 else if (c1 >= 0 && c2 >= 0)
1438 {
1439 detect_info->rejected
1440 |= (CATEGORY_MASK_UTF_16_BE | CATEGORY_MASK_UTF_16_LE);
1441 }
1442 no_more_source:
1443 return 1;
1444}
1445
1446static void
1447decode_coding_utf_16 (coding)
1448 struct coding_system *coding;
1449{
1450 const unsigned char *src = coding->source + coding->consumed;
1451 const unsigned char *src_end = coding->source + coding->src_bytes;
1452 const unsigned char *src_base;
1453 int *charbuf = coding->charbuf + coding->charbuf_used;
1454 int *charbuf_end = coding->charbuf + coding->charbuf_size;
1455 int consumed_chars = 0, consumed_chars_base;
1456 int multibytep = coding->src_multibyte;
1457 enum utf_16_bom_type bom = CODING_UTF_16_BOM (coding);
1458 enum utf_16_endian_type endian = CODING_UTF_16_ENDIAN (coding);
1459 int surrogate = CODING_UTF_16_SURROGATE (coding);
1460 Lisp_Object attr, charset_list;
1461
1462 CODING_GET_INFO (coding, attr, charset_list);
1463
1464 if (bom == utf_16_with_bom)
1465 {
1466 int c, c1, c2;
1467
1468 src_base = src;
1469 ONE_MORE_BYTE (c1);
1470 ONE_MORE_BYTE (c2);
1471 c = (c1 << 8) | c2;
1472
1473 if (endian == utf_16_big_endian
1474 ? c != 0xFEFF : c != 0xFFFE)
1475 {
1476 /* The first two bytes are not BOM. Treat them as bytes
1477 for a normal character. */
1478 src = src_base;
1479 coding->errors++;
1480 }
1481 CODING_UTF_16_BOM (coding) = utf_16_without_bom;
1482 }
1483 else if (bom == utf_16_detect_bom)
1484 {
1485 /* We have already tried to detect BOM and failed in
1486 detect_coding. */
1487 CODING_UTF_16_BOM (coding) = utf_16_without_bom;
1488 }
1489
1490 while (1)
1491 {
1492 int c, c1, c2;
1493
1494 src_base = src;
1495 consumed_chars_base = consumed_chars;
1496
1497 if (charbuf + 2 >= charbuf_end)
1498 break;
1499
1500 ONE_MORE_BYTE (c1);
1501 if (c1 < 0)
1502 {
1503 *charbuf++ = -c1;
1504 continue;
1505 }
1506 ONE_MORE_BYTE (c2);
1507 if (c2 < 0)
1508 {
1509 *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1);
1510 *charbuf++ = -c2;
1511 continue;
1512 }
1513 c = (endian == utf_16_big_endian
1514 ? ((c1 << 8) | c2) : ((c2 << 8) | c1));
1515 if (surrogate)
1516 {
1517 if (! UTF_16_LOW_SURROGATE_P (c))
1518 {
1519 if (endian == utf_16_big_endian)
1520 c1 = surrogate >> 8, c2 = surrogate & 0xFF;
1521 else
1522 c1 = surrogate & 0xFF, c2 = surrogate >> 8;
1523 *charbuf++ = c1;
1524 *charbuf++ = c2;
1525 coding->errors++;
1526 if (UTF_16_HIGH_SURROGATE_P (c))
1527 CODING_UTF_16_SURROGATE (coding) = surrogate = c;
1528 else
1529 *charbuf++ = c;
1530 }
1531 else
1532 {
1533 c = ((surrogate - 0xD800) << 10) | (c - 0xDC00);
1534 CODING_UTF_16_SURROGATE (coding) = surrogate = 0;
1535 *charbuf++ = 0x10000 + c;
1536 }
1537 }
1538 else
1539 {
1540 if (UTF_16_HIGH_SURROGATE_P (c))
1541 CODING_UTF_16_SURROGATE (coding) = surrogate = c;
1542 else
1543 *charbuf++ = c;
1544 }
1545 }
1546
1547 no_more_source:
1548 coding->consumed_char += consumed_chars_base;
1549 coding->consumed = src_base - coding->source;
1550 coding->charbuf_used = charbuf - coding->charbuf;
1551}
1552
1553static int
1554encode_coding_utf_16 (coding)
1555 struct coding_system *coding;
1556{
1557 int multibytep = coding->dst_multibyte;
1558 int *charbuf = coding->charbuf;
1559 int *charbuf_end = charbuf + coding->charbuf_used;
1560 unsigned char *dst = coding->destination + coding->produced;
1561 unsigned char *dst_end = coding->destination + coding->dst_bytes;
1562 int safe_room = 8;
1563 enum utf_16_bom_type bom = CODING_UTF_16_BOM (coding);
1564 int big_endian = CODING_UTF_16_ENDIAN (coding) == utf_16_big_endian;
1565 int produced_chars = 0;
1566 Lisp_Object attrs, charset_list;
1567 int c;
1568
1569 CODING_GET_INFO (coding, attrs, charset_list);
1570
1571 if (bom != utf_16_without_bom)
1572 {
1573 ASSURE_DESTINATION (safe_room);
1574 if (big_endian)
1575 EMIT_TWO_BYTES (0xFE, 0xFF);
1576 else
1577 EMIT_TWO_BYTES (0xFF, 0xFE);
1578 CODING_UTF_16_BOM (coding) = utf_16_without_bom;
1579 }
1580
1581 while (charbuf < charbuf_end)
1582 {
1583 ASSURE_DESTINATION (safe_room);
1584 c = *charbuf++;
1585 if (c >= MAX_UNICODE_CHAR)
1586 c = coding->default_char;
1587
1588 if (c < 0x10000)
1589 {
1590 if (big_endian)
1591 EMIT_TWO_BYTES (c >> 8, c & 0xFF);
1592 else
1593 EMIT_TWO_BYTES (c & 0xFF, c >> 8);
1594 }
1595 else
1596 {
1597 int c1, c2;
1598
1599 c -= 0x10000;
1600 c1 = (c >> 10) + 0xD800;
1601 c2 = (c & 0x3FF) + 0xDC00;
1602 if (big_endian)
1603 EMIT_FOUR_BYTES (c1 >> 8, c1 & 0xFF, c2 >> 8, c2 & 0xFF);
1604 else
1605 EMIT_FOUR_BYTES (c1 & 0xFF, c1 >> 8, c2 & 0xFF, c2 >> 8);
1606 }
1607 }
1608 record_conversion_result (coding, CODING_RESULT_SUCCESS);
1609 coding->produced = dst - coding->destination;
1610 coding->produced_char += produced_chars;
1611 return 0;
1612}
1613
1614
1615/*** 6. Old Emacs' internal format (emacs-mule) ***/
544 1616
545/* Emacs' internal format for representation of multiple character 1617/* Emacs' internal format for representation of multiple character
546 sets is a kind of multi-byte encoding, i.e. characters are 1618 sets is a kind of multi-byte encoding, i.e. characters are
@@ -582,7 +1654,7 @@ coding_safe_chars (coding_system)
582 In that case, a sequence of one-byte codes has a slightly different 1654 In that case, a sequence of one-byte codes has a slightly different
583 form. 1655 form.
584 1656
585 Firstly, all characters in eight-bit-control are represented by 1657 At first, all characters in eight-bit-control are represented by
586 one-byte sequences which are their 8-bit code. 1658 one-byte sequences which are their 8-bit code.
587 1659
588 Next, character composition data are represented by the byte 1660 Next, character composition data are represented by the byte
@@ -591,12 +1663,12 @@ coding_safe_chars (coding_system)
591 METHOD is 0xF0 plus one of composition method (enum 1663 METHOD is 0xF0 plus one of composition method (enum
592 composition_method), 1664 composition_method),
593 1665
594 BYTES is 0xA0 plus the byte length of these composition data, 1666 BYTES is 0xA0 plus a byte length of this composition data,
595 1667
596 CHARS is 0xA0 plus the number of characters composed by these 1668 CHARS is 0x20 plus a number of characters composed by this
597 data, 1669 data,
598 1670
599 COMPONENTs are characters of multibyte form or composition 1671 COMPONENTs are characters of multibye form or composition
600 rules encoded by two-byte of ASCII codes. 1672 rules encoded by two-byte of ASCII codes.
601 1673
602 In addition, for backward compatibility, the following formats are 1674 In addition, for backward compatibility, the following formats are
@@ -613,596 +1685,601 @@ coding_safe_chars (coding_system)
613 represents a composition rule. 1685 represents a composition rule.
614 */ 1686 */
615 1687
616enum emacs_code_class_type emacs_code_class[256]; 1688char emacs_mule_bytes[256];
617
618/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
619 Check if a text is encoded in Emacs' internal format. If it is,
620 return CODING_CATEGORY_MASK_EMACS_MULE, else return 0. */
621 1689
622static int 1690int
623detect_coding_emacs_mule (src, src_end, multibytep) 1691emacs_mule_char (coding, src, nbytes, nchars, id)
624 unsigned char *src, *src_end; 1692 struct coding_system *coding;
625 int multibytep; 1693 const unsigned char *src;
1694 int *nbytes, *nchars, *id;
626{ 1695{
627 unsigned char c; 1696 const unsigned char *src_end = coding->source + coding->src_bytes;
628 int composing = 0; 1697 const unsigned char *src_base = src;
629 /* Dummy for ONE_MORE_BYTE. */ 1698 int multibytep = coding->src_multibyte;
630 struct coding_system dummy_coding; 1699 struct charset *charset;
631 struct coding_system *coding = &dummy_coding; 1700 unsigned code;
1701 int c;
1702 int consumed_chars = 0;
632 1703
633 while (1) 1704 ONE_MORE_BYTE (c);
1705 if (c < 0)
1706 {
1707 c = -c;
1708 charset = emacs_mule_charset[0];
1709 }
1710 else
634 { 1711 {
635 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep, 1712 if (c >= 0xA0)
636 CODING_CATEGORY_MASK_EMACS_MULE);
637 if (composing)
638 { 1713 {
639 if (c < 0xA0) 1714 /* Old style component character of a compostion. */
640 composing = 0; 1715 if (c == 0xA0)
641 else if (c == 0xA0)
642 { 1716 {
643 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep, 0); 1717 ONE_MORE_BYTE (c);
644 c &= 0x7F; 1718 c -= 0x80;
645 } 1719 }
646 else 1720 else
647 c -= 0x20; 1721 c -= 0x20;
648 } 1722 }
649 1723
650 if (c < 0x20) 1724 switch (emacs_mule_bytes[c])
651 {
652 if (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)
653 return 0;
654 }
655 else if (c >= 0x80 && c < 0xA0)
656 { 1725 {
657 if (c == 0x80) 1726 case 2:
658 /* Old leading code for a composite character. */ 1727 if (! (charset = emacs_mule_charset[c]))
659 composing = 1; 1728 goto invalid_code;
1729 ONE_MORE_BYTE (c);
1730 if (c < 0xA0)
1731 goto invalid_code;
1732 code = c & 0x7F;
1733 break;
1734
1735 case 3:
1736 if (c == EMACS_MULE_LEADING_CODE_PRIVATE_11
1737 || c == EMACS_MULE_LEADING_CODE_PRIVATE_12)
1738 {
1739 ONE_MORE_BYTE (c);
1740 if (c < 0xA0 || ! (charset = emacs_mule_charset[c]))
1741 goto invalid_code;
1742 ONE_MORE_BYTE (c);
1743 if (c < 0xA0)
1744 goto invalid_code;
1745 code = c & 0x7F;
1746 }
660 else 1747 else
661 { 1748 {
662 unsigned char *src_base = src - 1; 1749 if (! (charset = emacs_mule_charset[c]))
663 int bytes; 1750 goto invalid_code;
664 1751 ONE_MORE_BYTE (c);
665 if (!UNIBYTE_STR_AS_MULTIBYTE_P (src_base, src_end - src_base, 1752 if (c < 0xA0)
666 bytes)) 1753 goto invalid_code;
667 return 0; 1754 code = (c & 0x7F) << 8;
668 src = src_base + bytes; 1755 ONE_MORE_BYTE (c);
1756 if (c < 0xA0)
1757 goto invalid_code;
1758 code |= c & 0x7F;
669 } 1759 }
1760 break;
1761
1762 case 4:
1763 ONE_MORE_BYTE (c);
1764 if (c < 0 || ! (charset = emacs_mule_charset[c]))
1765 goto invalid_code;
1766 ONE_MORE_BYTE (c);
1767 if (c < 0xA0)
1768 goto invalid_code;
1769 code = (c & 0x7F) << 8;
1770 ONE_MORE_BYTE (c);
1771 if (c < 0xA0)
1772 goto invalid_code;
1773 code |= c & 0x7F;
1774 break;
1775
1776 case 1:
1777 code = c;
1778 charset = CHARSET_FROM_ID (ASCII_BYTE_P (code)
1779 ? charset_ascii : charset_eight_bit);
1780 break;
1781
1782 default:
1783 abort ();
670 } 1784 }
1785 c = DECODE_CHAR (charset, code);
1786 if (c < 0)
1787 goto invalid_code;
671 } 1788 }
1789 *nbytes = src - src_base;
1790 *nchars = consumed_chars;
1791 if (id)
1792 *id = charset->id;
1793 return c;
1794
1795 no_more_source:
1796 return -2;
1797
1798 invalid_code:
1799 return -1;
672} 1800}
673 1801
674 1802
675/* Record the starting position START and METHOD of one composition. */ 1803/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1804 Check if a text is encoded in `emacs-mule'. If it is, return 1,
1805 else return 0. */
676 1806
677#define CODING_ADD_COMPOSITION_START(coding, start, method) \ 1807static int
678 do { \ 1808detect_coding_emacs_mule (coding, detect_info)
679 struct composition_data *cmp_data = coding->cmp_data; \ 1809 struct coding_system *coding;
680 int *data = cmp_data->data + cmp_data->used; \ 1810 struct coding_detection_info *detect_info;
681 coding->cmp_data_start = cmp_data->used; \ 1811{
682 data[0] = -1; \ 1812 const unsigned char *src = coding->source, *src_base;
683 data[1] = cmp_data->char_offset + start; \ 1813 const unsigned char *src_end = coding->source + coding->src_bytes;
684 data[3] = (int) method; \ 1814 int multibytep = coding->src_multibyte;
685 cmp_data->used += 4; \ 1815 int consumed_chars = 0;
686 } while (0) 1816 int c;
1817 int found = 0;
687 1818
688/* Record the ending position END of the current composition. */ 1819 detect_info->checked |= CATEGORY_MASK_EMACS_MULE;
1820 /* A coding system of this category is always ASCII compatible. */
1821 src += coding->head_ascii;
689 1822
690#define CODING_ADD_COMPOSITION_END(coding, end) \ 1823 while (1)
691 do { \ 1824 {
692 struct composition_data *cmp_data = coding->cmp_data; \ 1825 src_base = src;
693 int *data = cmp_data->data + coding->cmp_data_start; \ 1826 ONE_MORE_BYTE (c);
694 data[0] = cmp_data->used - coding->cmp_data_start; \ 1827 if (c < 0)
695 data[2] = cmp_data->char_offset + end; \ 1828 continue;
696 } while (0) 1829 if (c == 0x80)
1830 {
1831 /* Perhaps the start of composite character. We simple skip
1832 it because analyzing it is too heavy for detecting. But,
1833 at least, we check that the composite character
1834 constitues of more than 4 bytes. */
1835 const unsigned char *src_base;
697 1836
698/* Record one COMPONENT (alternate character or composition rule). */ 1837 repeat:
1838 src_base = src;
1839 do
1840 {
1841 ONE_MORE_BYTE (c);
1842 }
1843 while (c >= 0xA0);
699 1844
700#define CODING_ADD_COMPOSITION_COMPONENT(coding, component) \ 1845 if (src - src_base <= 4)
701 do { \ 1846 break;
702 coding->cmp_data->data[coding->cmp_data->used++] = component; \ 1847 found = CATEGORY_MASK_EMACS_MULE;
703 if (coding->cmp_data->used - coding->cmp_data_start \ 1848 if (c == 0x80)
704 == COMPOSITION_DATA_MAX_BUNCH_LENGTH) \ 1849 goto repeat;
705 { \ 1850 }
706 CODING_ADD_COMPOSITION_END (coding, coding->produced_char); \ 1851
707 coding->composing = COMPOSITION_NO; \ 1852 if (c < 0x80)
708 } \ 1853 {
709 } while (0) 1854 if (c < 0x20
1855 && (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO))
1856 break;
1857 }
1858 else
1859 {
1860 int more_bytes = emacs_mule_bytes[*src_base] - 1;
710 1861
1862 while (more_bytes > 0)
1863 {
1864 ONE_MORE_BYTE (c);
1865 if (c < 0xA0)
1866 {
1867 src--; /* Unread the last byte. */
1868 break;
1869 }
1870 more_bytes--;
1871 }
1872 if (more_bytes != 0)
1873 break;
1874 found = CATEGORY_MASK_EMACS_MULE;
1875 }
1876 }
1877 detect_info->rejected |= CATEGORY_MASK_EMACS_MULE;
1878 return 0;
711 1879
712/* Get one byte from a data pointed by SRC and increment SRC. If SRC 1880 no_more_source:
713 is not less than SRC_END, return -1 without incrementing Src. */ 1881 if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK)
1882 {
1883 detect_info->rejected |= CATEGORY_MASK_EMACS_MULE;
1884 return 0;
1885 }
1886 detect_info->found |= found;
1887 return 1;
1888}
714 1889
715#define SAFE_ONE_MORE_BYTE() (src >= src_end ? -1 : *src++)
716 1890
1891/* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
717 1892
718/* Decode a character represented as a component of composition 1893/* Decode a character represented as a component of composition
719 sequence of Emacs 20 style at SRC. Set C to that character, store 1894 sequence of Emacs 20/21 style at SRC. Set C to that character and
720 its multibyte form sequence at P, and set P to the end of that 1895 update SRC to the head of next character (or an encoded composition
721 sequence. If no valid character is found, set C to -1. */ 1896 rule). If SRC doesn't points a composition component, set C to -1.
722 1897 If SRC points an invalid byte sequence, global exit by a return
723#define DECODE_EMACS_MULE_COMPOSITION_CHAR(c, p) \ 1898 value 0. */
724 do { \ 1899
725 int bytes; \ 1900#define DECODE_EMACS_MULE_COMPOSITION_CHAR(buf) \
726 \ 1901 if (1) \
727 c = SAFE_ONE_MORE_BYTE (); \ 1902 { \
728 if (c < 0) \ 1903 int c; \
729 break; \ 1904 int nbytes, nchars; \
730 if (CHAR_HEAD_P (c)) \
731 c = -1; \
732 else if (c == 0xA0) \
733 { \
734 c = SAFE_ONE_MORE_BYTE (); \
735 if (c < 0xA0) \
736 c = -1; \
737 else \
738 { \
739 c -= 0x80; \
740 *p++ = c; \
741 } \
742 } \
743 else if (BASE_LEADING_CODE_P (c - 0x20)) \
744 { \
745 unsigned char *p0 = p; \
746 \ 1905 \
747 c -= 0x20; \ 1906 if (src == src_end) \
748 *p++ = c; \ 1907 break; \
749 bytes = BYTES_BY_CHAR_HEAD (c); \ 1908 c = emacs_mule_char (coding, src, &nbytes, &nchars, NULL);\
750 while (--bytes) \ 1909 if (c < 0) \
751 { \ 1910 { \
752 c = SAFE_ONE_MORE_BYTE (); \ 1911 if (c == -2) \
753 if (c < 0) \ 1912 break; \
754 break; \ 1913 goto invalid_code; \
755 *p++ = c; \ 1914 } \
756 } \ 1915 *buf++ = c; \
757 if (UNIBYTE_STR_AS_MULTIBYTE_P (p0, p - p0, bytes) \ 1916 src += nbytes; \
758 || (coding->flags /* We are recovering a file. */ \ 1917 consumed_chars += nchars; \
759 && p0[0] == LEADING_CODE_8_BIT_CONTROL \ 1918 } \
760 && ! CHAR_HEAD_P (p0[1]))) \ 1919 else
761 c = STRING_CHAR (p0, bytes); \
762 else \
763 c = -1; \
764 } \
765 else \
766 c = -1; \
767 } while (0)
768 1920
769 1921
770/* Decode a composition rule represented as a component of composition 1922/* Decode a composition rule represented as a component of composition
771 sequence of Emacs 20 style at SRC. Set C to the rule. If not 1923 sequence of Emacs 20 style at SRC. Store the decoded rule in *BUF,
772 valid rule is found, set C to -1. */ 1924 and increment BUF. If SRC points an invalid byte sequence, set C
1925 to -1. */
773 1926
774#define DECODE_EMACS_MULE_COMPOSITION_RULE(c) \ 1927#define DECODE_EMACS_MULE_COMPOSITION_RULE_20(buf) \
775 do { \ 1928 do { \
776 c = SAFE_ONE_MORE_BYTE (); \ 1929 int c, gref, nref; \
1930 \
1931 if (src >= src_end) \
1932 goto invalid_code; \
1933 ONE_MORE_BYTE_NO_CHECK (c); \
777 c -= 0xA0; \ 1934 c -= 0xA0; \
778 if (c < 0 || c >= 81) \ 1935 if (c < 0 || c >= 81) \
779 c = -1; \ 1936 goto invalid_code; \
780 else \ 1937 \
781 { \ 1938 gref = c / 9, nref = c % 9; \
782 gref = c / 9, nref = c % 9; \ 1939 *buf++ = COMPOSITION_ENCODE_RULE (gref, nref); \
783 c = COMPOSITION_ENCODE_RULE (gref, nref); \
784 } \
785 } while (0) 1940 } while (0)
786 1941
787 1942
788/* Decode composition sequence encoded by `emacs-mule' at the source 1943/* Decode a composition rule represented as a component of composition
789 pointed by SRC. SRC_END is the end of source. Store information 1944 sequence of Emacs 21 style at SRC. Store the decoded rule in *BUF,
790 of the composition in CODING->cmp_data. 1945 and increment BUF. If SRC points an invalid byte sequence, set C
791 1946 to -1. */
792 For backward compatibility, decode also a composition sequence of
793 Emacs 20 style. In that case, the composition sequence contains
794 characters that should be extracted into a buffer or string. Store
795 those characters at *DESTINATION in multibyte form.
796
797 If we encounter an invalid byte sequence, return 0.
798 If we encounter an insufficient source or destination, or
799 insufficient space in CODING->cmp_data, return 1.
800 Otherwise, return consumed bytes in the source.
801 1947
802*/ 1948#define DECODE_EMACS_MULE_COMPOSITION_RULE_21(buf) \
803static INLINE int 1949 do { \
804decode_composition_emacs_mule (coding, src, src_end, 1950 int gref, nref; \
805 destination, dst_end, dst_bytes) 1951 \
806 struct coding_system *coding; 1952 if (src + 1>= src_end) \
807 const unsigned char *src, *src_end; 1953 goto invalid_code; \
808 unsigned char **destination, *dst_end; 1954 ONE_MORE_BYTE_NO_CHECK (gref); \
809 int dst_bytes; 1955 gref -= 0x20; \
810{ 1956 ONE_MORE_BYTE_NO_CHECK (nref); \
811 unsigned char *dst = *destination; 1957 nref -= 0x20; \
812 int method, data_len, nchars; 1958 if (gref < 0 || gref >= 81 \
813 const unsigned char *src_base = src++; 1959 || nref < 0 || nref >= 81) \
814 /* Store components of composition. */ 1960 goto invalid_code; \
815 int component[COMPOSITION_DATA_MAX_BUNCH_LENGTH]; 1961 *buf++ = COMPOSITION_ENCODE_RULE (gref, nref); \
816 int ncomponent; 1962 } while (0)
817 /* Store multibyte form of characters to be composed. This is for
818 Emacs 20 style composition sequence. */
819 unsigned char buf[MAX_COMPOSITION_COMPONENTS * MAX_MULTIBYTE_LENGTH];
820 unsigned char *bufp = buf;
821 int c, i, gref, nref;
822 1963
823 if (coding->cmp_data->used + COMPOSITION_DATA_MAX_BUNCH_LENGTH
824 >= COMPOSITION_DATA_SIZE)
825 {
826 coding->result = CODING_FINISH_INSUFFICIENT_CMP;
827 return -1;
828 }
829 1964
830 ONE_MORE_BYTE (c); 1965#define DECODE_EMACS_MULE_21_COMPOSITION(c) \
831 if (c - 0xF0 >= COMPOSITION_RELATIVE 1966 do { \
832 && c - 0xF0 <= COMPOSITION_WITH_RULE_ALTCHARS) 1967 /* Emacs 21 style format. The first three bytes at SRC are \
833 { 1968 (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is \
834 int with_rule; 1969 the byte length of this composition information, CHARS is the \
1970 number of characters composed by this composition. */ \
1971 enum composition_method method = c - 0xF2; \
1972 int *charbuf_base = charbuf; \
1973 int consumed_chars_limit; \
1974 int nbytes, nchars; \
1975 \
1976 ONE_MORE_BYTE (c); \
1977 if (c < 0) \
1978 goto invalid_code; \
1979 nbytes = c - 0xA0; \
1980 if (nbytes < 3) \
1981 goto invalid_code; \
1982 ONE_MORE_BYTE (c); \
1983 if (c < 0) \
1984 goto invalid_code; \
1985 nchars = c - 0xA0; \
1986 ADD_COMPOSITION_DATA (charbuf, nchars, method); \
1987 consumed_chars_limit = consumed_chars_base + nbytes; \
1988 if (method != COMPOSITION_RELATIVE) \
1989 { \
1990 int i = 0; \
1991 while (consumed_chars < consumed_chars_limit) \
1992 { \
1993 if (i % 2 && method != COMPOSITION_WITH_ALTCHARS) \
1994 DECODE_EMACS_MULE_COMPOSITION_RULE_21 (charbuf); \
1995 else \
1996 DECODE_EMACS_MULE_COMPOSITION_CHAR (charbuf); \
1997 i++; \
1998 } \
1999 if (consumed_chars < consumed_chars_limit) \
2000 goto invalid_code; \
2001 charbuf_base[0] -= i; \
2002 } \
2003 } while (0)
835 2004
836 method = c - 0xF0;
837 with_rule = (method == COMPOSITION_WITH_RULE
838 || method == COMPOSITION_WITH_RULE_ALTCHARS);
839 ONE_MORE_BYTE (c);
840 data_len = c - 0xA0;
841 if (data_len < 4
842 || src_base + data_len > src_end)
843 return 0;
844 ONE_MORE_BYTE (c);
845 nchars = c - 0xA0;
846 if (c < 1)
847 return 0;
848 for (ncomponent = 0; src < src_base + data_len; ncomponent++)
849 {
850 /* If it is longer than this, it can't be valid. */
851 if (ncomponent >= COMPOSITION_DATA_MAX_BUNCH_LENGTH)
852 return 0;
853 2005
854 if (ncomponent % 2 && with_rule) 2006#define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION(c) \
855 { 2007 do { \
856 ONE_MORE_BYTE (gref); 2008 /* Emacs 20 style format for relative composition. */ \
857 gref -= 32; 2009 /* Store multibyte form of characters to be composed. */ \
858 ONE_MORE_BYTE (nref); 2010 enum composition_method method = COMPOSITION_RELATIVE; \
859 nref -= 32; 2011 int components[MAX_COMPOSITION_COMPONENTS * 2 - 1]; \
860 c = COMPOSITION_ENCODE_RULE (gref, nref); 2012 int *buf = components; \
861 } 2013 int i, j; \
862 else 2014 \
863 { 2015 src = src_base; \
864 int bytes; 2016 ONE_MORE_BYTE (c); /* skip 0x80 */ \
865 if (UNIBYTE_STR_AS_MULTIBYTE_P (src, src_end - src, bytes) 2017 for (i = 0; *src >= 0xA0 && i < MAX_COMPOSITION_COMPONENTS; i++) \
866 || (coding->flags /* We are recovering a file. */ 2018 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
867 && src[0] == LEADING_CODE_8_BIT_CONTROL 2019 if (i < 2) \
868 && ! CHAR_HEAD_P (src[1]))) 2020 goto invalid_code; \
869 c = STRING_CHAR (src, bytes); 2021 ADD_COMPOSITION_DATA (charbuf, i, method); \
870 else 2022 for (j = 0; j < i; j++) \
871 c = *src, bytes = 1; 2023 *charbuf++ = components[j]; \
872 src += bytes; 2024 } while (0)
873 }
874 component[ncomponent] = c;
875 }
876 }
877 else if (c >= 0x80)
878 {
879 /* This may be an old Emacs 20 style format. See the comment at
880 the section 2 of this file. */
881 while (src < src_end && !CHAR_HEAD_P (*src)) src++;
882 if (src == src_end
883 && !(coding->mode & CODING_MODE_LAST_BLOCK))
884 goto label_end_of_loop;
885 2025
886 src_end = src;
887 src = src_base + 1;
888 if (c < 0xC0)
889 {
890 method = COMPOSITION_RELATIVE;
891 for (ncomponent = 0; ncomponent < MAX_COMPOSITION_COMPONENTS;)
892 {
893 DECODE_EMACS_MULE_COMPOSITION_CHAR (c, bufp);
894 if (c < 0)
895 break;
896 component[ncomponent++] = c;
897 }
898 if (ncomponent < 2)
899 return 0;
900 nchars = ncomponent;
901 }
902 else if (c == 0xFF)
903 {
904 method = COMPOSITION_WITH_RULE;
905 src++;
906 DECODE_EMACS_MULE_COMPOSITION_CHAR (c, bufp);
907 if (c < 0)
908 return 0;
909 component[0] = c;
910 for (ncomponent = 1;
911 ncomponent < MAX_COMPOSITION_COMPONENTS * 2 - 1;)
912 {
913 DECODE_EMACS_MULE_COMPOSITION_RULE (c);
914 if (c < 0)
915 break;
916 component[ncomponent++] = c;
917 DECODE_EMACS_MULE_COMPOSITION_CHAR (c, bufp);
918 if (c < 0)
919 break;
920 component[ncomponent++] = c;
921 }
922 if (ncomponent < 3)
923 return 0;
924 nchars = (ncomponent + 1) / 2;
925 }
926 else
927 return 0;
928 }
929 else
930 return 0;
931 2026
932 if (buf == bufp || dst + (bufp - buf) <= (dst_bytes ? dst_end : src)) 2027#define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION(c) \
933 { 2028 do { \
934 CODING_ADD_COMPOSITION_START (coding, coding->produced_char, method); 2029 /* Emacs 20 style format for rule-base composition. */ \
935 for (i = 0; i < ncomponent; i++) 2030 /* Store multibyte form of characters to be composed. */ \
936 CODING_ADD_COMPOSITION_COMPONENT (coding, component[i]); 2031 enum composition_method method = COMPOSITION_WITH_RULE; \
937 CODING_ADD_COMPOSITION_END (coding, coding->produced_char + nchars); 2032 int *charbuf_base = charbuf; \
938 if (buf < bufp) 2033 int components[MAX_COMPOSITION_COMPONENTS * 2 - 1]; \
939 { 2034 int *buf = components; \
940 unsigned char *p = buf; 2035 int i, j; \
941 EMIT_BYTES (p, bufp); 2036 \
942 *destination += bufp - buf; 2037 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
943 coding->produced_char += nchars; 2038 for (i = 1; i < MAX_COMPOSITION_COMPONENTS; i++) \
944 } 2039 { \
945 return (src - src_base); 2040 if (*src < 0xA0) \
946 } 2041 break; \
947 label_end_of_loop: 2042 DECODE_EMACS_MULE_COMPOSITION_RULE_20 (buf); \
948 return -1; 2043 DECODE_EMACS_MULE_COMPOSITION_CHAR (buf); \
949} 2044 } \
2045 if (i <= 1 || (buf - components) % 2 == 0) \
2046 goto invalid_code; \
2047 if (charbuf + i + (i / 2) + 1 >= charbuf_end) \
2048 goto no_more_source; \
2049 ADD_COMPOSITION_DATA (charbuf, i, method); \
2050 i = i * 2 - 1; \
2051 for (j = 0; j < i; j++) \
2052 *charbuf++ = components[j]; \
2053 charbuf_base[0] -= i; \
2054 for (j = 0; j < i; j += 2) \
2055 *charbuf++ = components[j]; \
2056 } while (0)
950 2057
951/* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
952 2058
953static void 2059static void
954decode_coding_emacs_mule (coding, source, destination, src_bytes, dst_bytes) 2060decode_coding_emacs_mule (coding)
955 struct coding_system *coding; 2061 struct coding_system *coding;
956 const unsigned char *source;
957 unsigned char *destination;
958 int src_bytes, dst_bytes;
959{ 2062{
960 const unsigned char *src = source; 2063 const unsigned char *src = coding->source + coding->consumed;
961 const unsigned char *src_end = source + src_bytes; 2064 const unsigned char *src_end = coding->source + coding->src_bytes;
962 unsigned char *dst = destination;
963 unsigned char *dst_end = destination + dst_bytes;
964 /* SRC_BASE remembers the start position in source in each loop.
965 The loop will be exited when there's not enough source code, or
966 when there's not enough destination area to produce a
967 character. */
968 const unsigned char *src_base; 2065 const unsigned char *src_base;
2066 int *charbuf = coding->charbuf + coding->charbuf_used;
2067 int *charbuf_end
2068 = coding->charbuf + coding->charbuf_size - MAX_ANNOTATION_LENGTH;
2069 int consumed_chars = 0, consumed_chars_base;
2070 int multibytep = coding->src_multibyte;
2071 Lisp_Object attrs, charset_list;
2072 int char_offset = coding->produced_char;
2073 int last_offset = char_offset;
2074 int last_id = charset_ascii;
2075
2076 CODING_GET_INFO (coding, attrs, charset_list);
969 2077
970 coding->produced_char = 0; 2078 while (1)
971 while ((src_base = src) < src_end)
972 { 2079 {
973 unsigned char tmp[MAX_MULTIBYTE_LENGTH]; 2080 int c;
974 const unsigned char *p;
975 int bytes;
976 2081
977 if (*src == '\r') 2082 src_base = src;
978 { 2083 consumed_chars_base = consumed_chars;
979 int c = *src++;
980 2084
981 if (coding->eol_type == CODING_EOL_CR) 2085 if (charbuf >= charbuf_end)
982 c = '\n'; 2086 break;
983 else if (coding->eol_type == CODING_EOL_CRLF) 2087
984 { 2088 ONE_MORE_BYTE (c);
985 ONE_MORE_BYTE (c); 2089 if (c < 0)
986 if (c != '\n')
987 {
988 src--;
989 c = '\r';
990 }
991 }
992 *dst++ = c;
993 coding->produced_char++;
994 continue;
995 }
996 else if (*src == '\n')
997 { 2090 {
998 if ((coding->eol_type == CODING_EOL_CR 2091 *charbuf++ = -c;
999 || coding->eol_type == CODING_EOL_CRLF) 2092 char_offset++;
1000 && coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
1001 {
1002 coding->result = CODING_FINISH_INCONSISTENT_EOL;
1003 goto label_end_of_loop;
1004 }
1005 *dst++ = *src++;
1006 coding->produced_char++;
1007 continue;
1008 } 2093 }
1009 else if (*src == 0x80 && coding->cmp_data) 2094 else if (c < 0x80)
1010 { 2095 {
1011 /* Start of composition data. */ 2096 *charbuf++ = c;
1012 int consumed = decode_composition_emacs_mule (coding, src, src_end, 2097 char_offset++;
1013 &dst, dst_end,
1014 dst_bytes);
1015 if (consumed < 0)
1016 goto label_end_of_loop;
1017 else if (consumed > 0)
1018 {
1019 src += consumed;
1020 continue;
1021 }
1022 bytes = CHAR_STRING (*src, tmp);
1023 p = tmp;
1024 src++;
1025 } 2098 }
1026 else if (UNIBYTE_STR_AS_MULTIBYTE_P (src, src_end - src, bytes) 2099 else if (c == 0x80)
1027 || (coding->flags /* We are recovering a file. */
1028 && src[0] == LEADING_CODE_8_BIT_CONTROL
1029 && ! CHAR_HEAD_P (src[1])))
1030 { 2100 {
1031 p = src; 2101 ONE_MORE_BYTE (c);
1032 src += bytes; 2102 if (c < 0)
2103 goto invalid_code;
2104 if (c - 0xF2 >= COMPOSITION_RELATIVE
2105 && c - 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS)
2106 DECODE_EMACS_MULE_21_COMPOSITION (c);
2107 else if (c < 0xC0)
2108 DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (c);
2109 else if (c == 0xFF)
2110 DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (c);
2111 else
2112 goto invalid_code;
1033 } 2113 }
1034 else 2114 else if (c < 0xA0 && emacs_mule_bytes[c] > 1)
1035 { 2115 {
1036 int i, c; 2116 int nbytes, nchars;
2117 int id;
1037 2118
1038 bytes = BYTES_BY_CHAR_HEAD (*src); 2119 src = src_base;
1039 src++; 2120 consumed_chars = consumed_chars_base;
1040 for (i = 1; i < bytes; i++) 2121 c = emacs_mule_char (coding, src, &nbytes, &nchars, &id);
2122 if (c < 0)
1041 { 2123 {
1042 ONE_MORE_BYTE (c); 2124 if (c == -2)
1043 if (CHAR_HEAD_P (c))
1044 break; 2125 break;
2126 goto invalid_code;
1045 } 2127 }
1046 if (i < bytes) 2128 if (last_id != id)
1047 { 2129 {
1048 bytes = CHAR_STRING (*src_base, tmp); 2130 if (last_id != charset_ascii)
1049 p = tmp; 2131 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
1050 src = src_base + 1; 2132 last_id = id;
2133 last_offset = char_offset;
1051 } 2134 }
1052 else 2135 *charbuf++ = c;
1053 { 2136 src += nbytes;
1054 p = src_base; 2137 consumed_chars += nchars;
1055 } 2138 char_offset++;
1056 }
1057 if (dst + bytes >= (dst_bytes ? dst_end : src))
1058 {
1059 coding->result = CODING_FINISH_INSUFFICIENT_DST;
1060 break;
1061 } 2139 }
1062 while (bytes--) *dst++ = *p++; 2140 else
1063 coding->produced_char++; 2141 goto invalid_code;
1064 } 2142 continue;
1065 label_end_of_loop:
1066 coding->consumed = coding->consumed_char = src_base - source;
1067 coding->produced = dst - destination;
1068}
1069 2143
2144 invalid_code:
2145 src = src_base;
2146 consumed_chars = consumed_chars_base;
2147 ONE_MORE_BYTE (c);
2148 *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
2149 char_offset++;
2150 coding->errors++;
2151 }
1070 2152
1071/* Encode composition data stored at DATA into a special byte sequence 2153 no_more_source:
1072 starting by 0x80. Update CODING->cmp_data_start and maybe 2154 if (last_id != charset_ascii)
1073 CODING->cmp_data for the next call. */ 2155 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
2156 coding->consumed_char += consumed_chars_base;
2157 coding->consumed = src_base - coding->source;
2158 coding->charbuf_used = charbuf - coding->charbuf;
2159}
1074 2160
1075#define ENCODE_COMPOSITION_EMACS_MULE(coding, data) \
1076 do { \
1077 unsigned char buf[1024], *p0 = buf, *p; \
1078 int len = data[0]; \
1079 int i; \
1080 \
1081 buf[0] = 0x80; \
1082 buf[1] = 0xF0 + data[3]; /* METHOD */ \
1083 buf[3] = 0xA0 + (data[2] - data[1]); /* COMPOSED-CHARS */ \
1084 p = buf + 4; \
1085 if (data[3] == COMPOSITION_WITH_RULE \
1086 || data[3] == COMPOSITION_WITH_RULE_ALTCHARS) \
1087 { \
1088 p += CHAR_STRING (data[4], p); \
1089 for (i = 5; i < len; i += 2) \
1090 { \
1091 int gref, nref; \
1092 COMPOSITION_DECODE_RULE (data[i], gref, nref); \
1093 *p++ = 0x20 + gref; \
1094 *p++ = 0x20 + nref; \
1095 p += CHAR_STRING (data[i + 1], p); \
1096 } \
1097 } \
1098 else \
1099 { \
1100 for (i = 4; i < len; i++) \
1101 p += CHAR_STRING (data[i], p); \
1102 } \
1103 buf[2] = 0xA0 + (p - buf); /* COMPONENTS-BYTES */ \
1104 \
1105 if (dst + (p - buf) + 4 > (dst_bytes ? dst_end : src)) \
1106 { \
1107 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
1108 goto label_end_of_loop; \
1109 } \
1110 while (p0 < p) \
1111 *dst++ = *p0++; \
1112 coding->cmp_data_start += data[0]; \
1113 if (coding->cmp_data_start == coding->cmp_data->used \
1114 && coding->cmp_data->next) \
1115 { \
1116 coding->cmp_data = coding->cmp_data->next; \
1117 coding->cmp_data_start = 0; \
1118 } \
1119 } while (0)
1120 2161
2162#define EMACS_MULE_LEADING_CODES(id, codes) \
2163 do { \
2164 if (id < 0xA0) \
2165 codes[0] = id, codes[1] = 0; \
2166 else if (id < 0xE0) \
2167 codes[0] = 0x9A, codes[1] = id; \
2168 else if (id < 0xF0) \
2169 codes[0] = 0x9B, codes[1] = id; \
2170 else if (id < 0xF5) \
2171 codes[0] = 0x9C, codes[1] = id; \
2172 else \
2173 codes[0] = 0x9D, codes[1] = id; \
2174 } while (0);
1121 2175
1122static void encode_eol P_ ((struct coding_system *, const unsigned char *,
1123 unsigned char *, int, int));
1124 2176
1125static void 2177static int
1126encode_coding_emacs_mule (coding, source, destination, src_bytes, dst_bytes) 2178encode_coding_emacs_mule (coding)
1127 struct coding_system *coding; 2179 struct coding_system *coding;
1128 const unsigned char *source;
1129 unsigned char *destination;
1130 int src_bytes, dst_bytes;
1131{ 2180{
1132 const unsigned char *src = source; 2181 int multibytep = coding->dst_multibyte;
1133 const unsigned char *src_end = source + src_bytes; 2182 int *charbuf = coding->charbuf;
1134 unsigned char *dst = destination; 2183 int *charbuf_end = charbuf + coding->charbuf_used;
1135 unsigned char *dst_end = destination + dst_bytes; 2184 unsigned char *dst = coding->destination + coding->produced;
1136 const unsigned char *src_base; 2185 unsigned char *dst_end = coding->destination + coding->dst_bytes;
2186 int safe_room = 8;
2187 int produced_chars = 0;
2188 Lisp_Object attrs, charset_list;
1137 int c; 2189 int c;
1138 int char_offset; 2190 int preferred_charset_id = -1;
1139 int *data;
1140
1141 Lisp_Object translation_table;
1142 2191
1143 translation_table = Qnil; 2192 CODING_GET_INFO (coding, attrs, charset_list);
1144 2193 if (! EQ (charset_list, Vemacs_mule_charset_list))
1145 /* Optimization for the case that there's no composition. */
1146 if (!coding->cmp_data || coding->cmp_data->used == 0)
1147 { 2194 {
1148 encode_eol (coding, source, destination, src_bytes, dst_bytes); 2195 CODING_ATTR_CHARSET_LIST (attrs)
1149 return; 2196 = charset_list = Vemacs_mule_charset_list;
1150 } 2197 }
1151 2198
1152 char_offset = coding->cmp_data->char_offset; 2199 while (charbuf < charbuf_end)
1153 data = coding->cmp_data->data + coding->cmp_data_start;
1154 while (1)
1155 { 2200 {
1156 src_base = src; 2201 ASSURE_DESTINATION (safe_room);
2202 c = *charbuf++;
1157 2203
1158 /* If SRC starts a composition, encode the information about the 2204 if (c < 0)
1159 composition in advance. */
1160 if (coding->cmp_data_start < coding->cmp_data->used
1161 && char_offset + coding->consumed_char == data[1])
1162 { 2205 {
1163 ENCODE_COMPOSITION_EMACS_MULE (coding, data); 2206 /* Handle an annotation. */
1164 char_offset = coding->cmp_data->char_offset; 2207 switch (*charbuf)
1165 data = coding->cmp_data->data + coding->cmp_data_start; 2208 {
2209 case CODING_ANNOTATE_COMPOSITION_MASK:
2210 /* Not yet implemented. */
2211 break;
2212 case CODING_ANNOTATE_CHARSET_MASK:
2213 preferred_charset_id = charbuf[3];
2214 if (preferred_charset_id >= 0
2215 && NILP (Fmemq (make_number (preferred_charset_id),
2216 charset_list)))
2217 preferred_charset_id = -1;
2218 break;
2219 default:
2220 abort ();
2221 }
2222 charbuf += -c - 1;
2223 continue;
1166 } 2224 }
1167 2225
1168 ONE_MORE_CHAR (c); 2226 if (ASCII_CHAR_P (c))
1169 if (c == '\n' && (coding->eol_type == CODING_EOL_CRLF 2227 EMIT_ONE_ASCII_BYTE (c);
1170 || coding->eol_type == CODING_EOL_CR)) 2228 else if (CHAR_BYTE8_P (c))
1171 { 2229 {
1172 if (coding->eol_type == CODING_EOL_CRLF) 2230 c = CHAR_TO_BYTE8 (c);
1173 EMIT_TWO_BYTES ('\r', c); 2231 EMIT_ONE_BYTE (c);
1174 else
1175 EMIT_ONE_BYTE ('\r');
1176 } 2232 }
1177 else if (SINGLE_BYTE_CHAR_P (c)) 2233 else
1178 { 2234 {
1179 if (coding->flags && ! ASCII_BYTE_P (c)) 2235 struct charset *charset;
1180 { 2236 unsigned code;
1181 /* As we are auto saving, retain the multibyte form for 2237 int dimension;
1182 8-bit chars. */ 2238 int emacs_mule_id;
1183 unsigned char buf[MAX_MULTIBYTE_LENGTH]; 2239 unsigned char leading_codes[2];
1184 int bytes = CHAR_STRING (c, buf);
1185 2240
1186 if (bytes == 1) 2241 if (preferred_charset_id >= 0)
1187 EMIT_ONE_BYTE (buf[0]); 2242 {
1188 else 2243 charset = CHARSET_FROM_ID (preferred_charset_id);
1189 EMIT_TWO_BYTES (buf[0], buf[1]); 2244 if (! CHAR_CHARSET_P (c, charset))
2245 charset = char_charset (c, charset_list, NULL);
1190 } 2246 }
1191 else 2247 else
1192 EMIT_ONE_BYTE (c); 2248 charset = char_charset (c, charset_list, &code);
2249 if (! charset)
2250 {
2251 c = coding->default_char;
2252 if (ASCII_CHAR_P (c))
2253 {
2254 EMIT_ONE_ASCII_BYTE (c);
2255 continue;
2256 }
2257 charset = char_charset (c, charset_list, &code);
2258 }
2259 dimension = CHARSET_DIMENSION (charset);
2260 emacs_mule_id = CHARSET_EMACS_MULE_ID (charset);
2261 EMACS_MULE_LEADING_CODES (emacs_mule_id, leading_codes);
2262 EMIT_ONE_BYTE (leading_codes[0]);
2263 if (leading_codes[1])
2264 EMIT_ONE_BYTE (leading_codes[1]);
2265 if (dimension == 1)
2266 EMIT_ONE_BYTE (code | 0x80);
2267 else
2268 {
2269 code |= 0x8080;
2270 EMIT_ONE_BYTE (code >> 8);
2271 EMIT_ONE_BYTE (code & 0xFF);
2272 }
1193 } 2273 }
1194 else
1195 EMIT_BYTES (src_base, src);
1196 coding->consumed_char++;
1197 } 2274 }
1198 label_end_of_loop: 2275 record_conversion_result (coding, CODING_RESULT_SUCCESS);
1199 coding->consumed = src_base - source; 2276 coding->produced_char += produced_chars;
1200 coding->produced = coding->produced_char = dst - destination; 2277 coding->produced = dst - coding->destination;
1201 return; 2278 return 0;
1202} 2279}
1203 2280
1204 2281
1205/*** 3. ISO2022 handlers ***/ 2282/*** 7. ISO2022 handlers ***/
1206 2283
1207/* The following note describes the coding system ISO2022 briefly. 2284/* The following note describes the coding system ISO2022 briefly.
1208 Since the intention of this note is to help understand the 2285 Since the intention of this note is to help understand the
@@ -1332,7 +2409,7 @@ encode_coding_emacs_mule (coding, source, destination, src_bytes, dst_bytes)
1332 7-bit environment, non-locking-shift, and non-single-shift. 2409 7-bit environment, non-locking-shift, and non-single-shift.
1333 2410
1334 Note (**): If <F> is '@', 'A', or 'B', the intermediate character 2411 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
1335 '(' can be omitted. We refer to this as "short-form" hereafter. 2412 '(' must be omitted. We refer to this as "short-form" hereafter.
1336 2413
1337 Now you may notice that there are a lot of ways of encoding the 2414 Now you may notice that there are a lot of ways of encoding the
1338 same multilingual text in ISO2022. Actually, there exist many 2415 same multilingual text in ISO2022. Actually, there exist many
@@ -1362,10 +2439,10 @@ encode_coding_emacs_mule (coding, source, destination, src_bytes, dst_bytes)
1362 Since these are not standard escape sequences of any ISO standard, 2439 Since these are not standard escape sequences of any ISO standard,
1363 the use of them with these meanings is restricted to Emacs only. 2440 the use of them with these meanings is restricted to Emacs only.
1364 2441
1365 (*) This form is used only in Emacs 20.5 and older versions, 2442 (*) This form is used only in Emacs 20.7 and older versions,
1366 but the newer versions can safely decode it. 2443 but newer versions can safely decode it.
1367 (**) This form is used only in Emacs 21.1 and newer versions, 2444 (**) This form is used only in Emacs 21.1 and newer versions,
1368 and the older versions can't decode it. 2445 and older versions can't decode it.
1369 2446
1370 Here's a list of example usages of these composition escape 2447 Here's a list of example usages of these composition escape
1371 sequences (categorized by `enum composition_method'). 2448 sequences (categorized by `enum composition_method').
@@ -1381,437 +2458,438 @@ encode_coding_emacs_mule (coding, source, destination, src_bytes, dst_bytes)
1381 2458
1382enum iso_code_class_type iso_code_class[256]; 2459enum iso_code_class_type iso_code_class[256];
1383 2460
1384#define CHARSET_OK(idx, charset, c) \ 2461#define SAFE_CHARSET_P(coding, id) \
1385 (coding_system_table[idx] \ 2462 ((id) <= (coding)->max_charset_id \
1386 && (charset == CHARSET_ASCII \ 2463 && (coding)->safe_charsets[id] >= 0)
1387 || (safe_chars = coding_safe_chars (coding_system_table[idx]->symbol), \ 2464
1388 CODING_SAFE_CHAR_P (safe_chars, c))) \ 2465
1389 && (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding_system_table[idx], \ 2466#define SHIFT_OUT_OK(category) \
1390 charset) \ 2467 (CODING_ISO_INITIAL (&coding_categories[category], 1) >= 0)
1391 != CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION)) 2468
2469static void
2470setup_iso_safe_charsets (attrs)
2471 Lisp_Object attrs;
2472{
2473 Lisp_Object charset_list, safe_charsets;
2474 Lisp_Object request;
2475 Lisp_Object reg_usage;
2476 Lisp_Object tail;
2477 int reg94, reg96;
2478 int flags = XINT (AREF (attrs, coding_attr_iso_flags));
2479 int max_charset_id;
2480
2481 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
2482 if ((flags & CODING_ISO_FLAG_FULL_SUPPORT)
2483 && ! EQ (charset_list, Viso_2022_charset_list))
2484 {
2485 CODING_ATTR_CHARSET_LIST (attrs)
2486 = charset_list = Viso_2022_charset_list;
2487 ASET (attrs, coding_attr_safe_charsets, Qnil);
2488 }
2489
2490 if (STRINGP (AREF (attrs, coding_attr_safe_charsets)))
2491 return;
2492
2493 max_charset_id = 0;
2494 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
2495 {
2496 int id = XINT (XCAR (tail));
2497 if (max_charset_id < id)
2498 max_charset_id = id;
2499 }
2500
2501 safe_charsets = Fmake_string (make_number (max_charset_id + 1),
2502 make_number (255));
2503 request = AREF (attrs, coding_attr_iso_request);
2504 reg_usage = AREF (attrs, coding_attr_iso_usage);
2505 reg94 = XINT (XCAR (reg_usage));
2506 reg96 = XINT (XCDR (reg_usage));
1392 2507
1393#define SHIFT_OUT_OK(idx) \ 2508 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
1394 (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding_system_table[idx], 1) >= 0) 2509 {
2510 Lisp_Object id;
2511 Lisp_Object reg;
2512 struct charset *charset;
2513
2514 id = XCAR (tail);
2515 charset = CHARSET_FROM_ID (XINT (id));
2516 reg = Fcdr (Fassq (id, request));
2517 if (! NILP (reg))
2518 SSET (safe_charsets, XINT (id), XINT (reg));
2519 else if (charset->iso_chars_96)
2520 {
2521 if (reg96 < 4)
2522 SSET (safe_charsets, XINT (id), reg96);
2523 }
2524 else
2525 {
2526 if (reg94 < 4)
2527 SSET (safe_charsets, XINT (id), reg94);
2528 }
2529 }
2530 ASET (attrs, coding_attr_safe_charsets, safe_charsets);
2531}
1395 2532
1396#define COMPOSITION_OK(idx) \
1397 (coding_system_table[idx]->composing != COMPOSITION_DISABLED)
1398 2533
1399/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". 2534/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1400 Check if a text is encoded in ISO2022. If it is, return an 2535 Check if a text is encoded in one of ISO-2022 based codig systems.
1401 integer in which appropriate flag bits any of: 2536 If it is, return 1, else return 0. */
1402 CODING_CATEGORY_MASK_ISO_7
1403 CODING_CATEGORY_MASK_ISO_7_TIGHT
1404 CODING_CATEGORY_MASK_ISO_8_1
1405 CODING_CATEGORY_MASK_ISO_8_2
1406 CODING_CATEGORY_MASK_ISO_7_ELSE
1407 CODING_CATEGORY_MASK_ISO_8_ELSE
1408 are set. If a code which should never appear in ISO2022 is found,
1409 returns 0.
1410
1411 If *latin_extra_code_state is zero and Latin extra codes are found,
1412 set *latin_extra_code_state to 1 and return 0. If it is nonzero,
1413 accept Latin extra codes. */
1414 2537
1415static int 2538static int
1416detect_coding_iso2022 (src, src_end, multibytep, latin_extra_code_state) 2539detect_coding_iso_2022 (coding, detect_info)
1417 unsigned char *src, *src_end; 2540 struct coding_system *coding;
1418 int multibytep; 2541 struct coding_detection_info *detect_info;
1419 int *latin_extra_code_state;
1420{ 2542{
1421 int mask = CODING_CATEGORY_MASK_ISO; 2543 const unsigned char *src = coding->source, *src_base = src;
1422 int mask_found = 0; 2544 const unsigned char *src_end = coding->source + coding->src_bytes;
1423 int reg[4], shift_out = 0, single_shifting = 0; 2545 int multibytep = coding->src_multibyte;
1424 int c, c1, charset; 2546 int single_shifting = 0;
1425 /* Dummy for ONE_MORE_BYTE. */ 2547 int id;
1426 struct coding_system dummy_coding; 2548 int c, c1;
1427 struct coding_system *coding = &dummy_coding; 2549 int consumed_chars = 0;
1428 Lisp_Object safe_chars; 2550 int i;
1429 2551 int rejected = 0;
1430 reg[0] = CHARSET_ASCII, reg[1] = reg[2] = reg[3] = -1; 2552 int found = 0;
1431 while (mask) 2553
1432 { 2554 detect_info->checked |= CATEGORY_MASK_ISO;
1433 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep, mask & mask_found); 2555
1434 retry: 2556 for (i = coding_category_iso_7; i <= coding_category_iso_8_else; i++)
2557 {
2558 struct coding_system *this = &(coding_categories[i]);
2559 Lisp_Object attrs, val;
2560
2561 attrs = CODING_ID_ATTRS (this->id);
2562 if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT
2563 && ! EQ (CODING_ATTR_SAFE_CHARSETS (attrs), Viso_2022_charset_list))
2564 setup_iso_safe_charsets (attrs);
2565 val = CODING_ATTR_SAFE_CHARSETS (attrs);
2566 this->max_charset_id = SCHARS (val) - 1;
2567 this->safe_charsets = (char *) SDATA (val);
2568 }
2569
2570 /* A coding system of this category is always ASCII compatible. */
2571 src += coding->head_ascii;
2572
2573 while (rejected != CATEGORY_MASK_ISO)
2574 {
2575 src_base = src;
2576 ONE_MORE_BYTE (c);
1435 switch (c) 2577 switch (c)
1436 { 2578 {
1437 case ISO_CODE_ESC: 2579 case ISO_CODE_ESC:
1438 if (inhibit_iso_escape_detection) 2580 if (inhibit_iso_escape_detection)
1439 break; 2581 break;
1440 single_shifting = 0; 2582 single_shifting = 0;
1441 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep, mask & mask_found); 2583 ONE_MORE_BYTE (c);
1442 if (c >= '(' && c <= '/') 2584 if (c >= '(' && c <= '/')
1443 { 2585 {
1444 /* Designation sequence for a charset of dimension 1. */ 2586 /* Designation sequence for a charset of dimension 1. */
1445 ONE_MORE_BYTE_CHECK_MULTIBYTE (c1, multibytep, mask & mask_found); 2587 ONE_MORE_BYTE (c1);
1446 if (c1 < ' ' || c1 >= 0x80 2588 if (c1 < ' ' || c1 >= 0x80
1447 || (charset = iso_charset_table[0][c >= ','][c1]) < 0) 2589 || (id = iso_charset_table[0][c >= ','][c1]) < 0)
1448 /* Invalid designation sequence. Just ignore. */ 2590 /* Invalid designation sequence. Just ignore. */
1449 break; 2591 break;
1450 reg[(c - '(') % 4] = charset;
1451 } 2592 }
1452 else if (c == '$') 2593 else if (c == '$')
1453 { 2594 {
1454 /* Designation sequence for a charset of dimension 2. */ 2595 /* Designation sequence for a charset of dimension 2. */
1455 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep, mask & mask_found); 2596 ONE_MORE_BYTE (c);
1456 if (c >= '@' && c <= 'B') 2597 if (c >= '@' && c <= 'B')
1457 /* Designation for JISX0208.1978, GB2312, or JISX0208. */ 2598 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
1458 reg[0] = charset = iso_charset_table[1][0][c]; 2599 id = iso_charset_table[1][0][c];
1459 else if (c >= '(' && c <= '/') 2600 else if (c >= '(' && c <= '/')
1460 { 2601 {
1461 ONE_MORE_BYTE_CHECK_MULTIBYTE (c1, multibytep, 2602 ONE_MORE_BYTE (c1);
1462 mask & mask_found);
1463 if (c1 < ' ' || c1 >= 0x80 2603 if (c1 < ' ' || c1 >= 0x80
1464 || (charset = iso_charset_table[1][c >= ','][c1]) < 0) 2604 || (id = iso_charset_table[1][c >= ','][c1]) < 0)
1465 /* Invalid designation sequence. Just ignore. */ 2605 /* Invalid designation sequence. Just ignore. */
1466 break; 2606 break;
1467 reg[(c - '(') % 4] = charset;
1468 } 2607 }
1469 else 2608 else
1470 /* Invalid designation sequence. Just ignore. */ 2609 /* Invalid designation sequence. Just ignore it. */
1471 break; 2610 break;
1472 } 2611 }
1473 else if (c == 'N' || c == 'O') 2612 else if (c == 'N' || c == 'O')
1474 { 2613 {
1475 /* ESC <Fe> for SS2 or SS3. */ 2614 /* ESC <Fe> for SS2 or SS3. */
1476 mask &= CODING_CATEGORY_MASK_ISO_7_ELSE; 2615 single_shifting = 1;
2616 rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_8BIT;
1477 break; 2617 break;
1478 } 2618 }
1479 else if (c >= '0' && c <= '4') 2619 else if (c >= '0' && c <= '4')
1480 { 2620 {
1481 /* ESC <Fp> for start/end composition. */ 2621 /* ESC <Fp> for start/end composition. */
1482 if (COMPOSITION_OK (CODING_CATEGORY_IDX_ISO_7)) 2622 found |= CATEGORY_MASK_ISO;
1483 mask_found |= CODING_CATEGORY_MASK_ISO_7;
1484 else
1485 mask &= ~CODING_CATEGORY_MASK_ISO_7;
1486 if (COMPOSITION_OK (CODING_CATEGORY_IDX_ISO_7_TIGHT))
1487 mask_found |= CODING_CATEGORY_MASK_ISO_7_TIGHT;
1488 else
1489 mask &= ~CODING_CATEGORY_MASK_ISO_7_TIGHT;
1490 if (COMPOSITION_OK (CODING_CATEGORY_IDX_ISO_8_1))
1491 mask_found |= CODING_CATEGORY_MASK_ISO_8_1;
1492 else
1493 mask &= ~CODING_CATEGORY_MASK_ISO_8_1;
1494 if (COMPOSITION_OK (CODING_CATEGORY_IDX_ISO_8_2))
1495 mask_found |= CODING_CATEGORY_MASK_ISO_8_2;
1496 else
1497 mask &= ~CODING_CATEGORY_MASK_ISO_8_2;
1498 if (COMPOSITION_OK (CODING_CATEGORY_IDX_ISO_7_ELSE))
1499 mask_found |= CODING_CATEGORY_MASK_ISO_7_ELSE;
1500 else
1501 mask &= ~CODING_CATEGORY_MASK_ISO_7_ELSE;
1502 if (COMPOSITION_OK (CODING_CATEGORY_IDX_ISO_8_ELSE))
1503 mask_found |= CODING_CATEGORY_MASK_ISO_8_ELSE;
1504 else
1505 mask &= ~CODING_CATEGORY_MASK_ISO_8_ELSE;
1506 break; 2623 break;
1507 } 2624 }
1508 else 2625 else
1509 /* Invalid escape sequence. Just ignore. */ 2626 {
1510 break; 2627 /* Invalid escape sequence. Just ignore it. */
2628 break;
2629 }
1511 2630
1512 /* We found a valid designation sequence for CHARSET. */ 2631 /* We found a valid designation sequence for CHARSET. */
1513 mask &= ~CODING_CATEGORY_MASK_ISO_8BIT; 2632 rejected |= CATEGORY_MASK_ISO_8BIT;
1514 c = MAKE_CHAR (charset, 0, 0); 2633 if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7],
1515 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7, charset, c)) 2634 id))
1516 mask_found |= CODING_CATEGORY_MASK_ISO_7; 2635 found |= CATEGORY_MASK_ISO_7;
1517 else 2636 else
1518 mask &= ~CODING_CATEGORY_MASK_ISO_7; 2637 rejected |= CATEGORY_MASK_ISO_7;
1519 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7_TIGHT, charset, c)) 2638 if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7_tight],
1520 mask_found |= CODING_CATEGORY_MASK_ISO_7_TIGHT; 2639 id))
2640 found |= CATEGORY_MASK_ISO_7_TIGHT;
1521 else 2641 else
1522 mask &= ~CODING_CATEGORY_MASK_ISO_7_TIGHT; 2642 rejected |= CATEGORY_MASK_ISO_7_TIGHT;
1523 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7_ELSE, charset, c)) 2643 if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7_else],
1524 mask_found |= CODING_CATEGORY_MASK_ISO_7_ELSE; 2644 id))
2645 found |= CATEGORY_MASK_ISO_7_ELSE;
1525 else 2646 else
1526 mask &= ~CODING_CATEGORY_MASK_ISO_7_ELSE; 2647 rejected |= CATEGORY_MASK_ISO_7_ELSE;
1527 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_8_ELSE, charset, c)) 2648 if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_8_else],
1528 mask_found |= CODING_CATEGORY_MASK_ISO_8_ELSE; 2649 id))
2650 found |= CATEGORY_MASK_ISO_8_ELSE;
1529 else 2651 else
1530 mask &= ~CODING_CATEGORY_MASK_ISO_8_ELSE; 2652 rejected |= CATEGORY_MASK_ISO_8_ELSE;
1531 break; 2653 break;
1532 2654
1533 case ISO_CODE_SO: 2655 case ISO_CODE_SO:
1534 if (inhibit_iso_escape_detection)
1535 break;
1536 single_shifting = 0;
1537 if (shift_out == 0
1538 && (reg[1] >= 0
1539 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_7_ELSE)
1540 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_8_ELSE)))
1541 {
1542 /* Locking shift out. */
1543 mask &= ~CODING_CATEGORY_MASK_ISO_7BIT;
1544 mask_found |= CODING_CATEGORY_MASK_ISO_SHIFT;
1545 }
1546 break;
1547
1548 case ISO_CODE_SI: 2656 case ISO_CODE_SI:
2657 /* Locking shift out/in. */
1549 if (inhibit_iso_escape_detection) 2658 if (inhibit_iso_escape_detection)
1550 break; 2659 break;
1551 single_shifting = 0; 2660 single_shifting = 0;
1552 if (shift_out == 1) 2661 rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_8BIT;
1553 {
1554 /* Locking shift in. */
1555 mask &= ~CODING_CATEGORY_MASK_ISO_7BIT;
1556 mask_found |= CODING_CATEGORY_MASK_ISO_SHIFT;
1557 }
1558 break; 2662 break;
1559 2663
1560 case ISO_CODE_CSI: 2664 case ISO_CODE_CSI:
2665 /* Control sequence introducer. */
1561 single_shifting = 0; 2666 single_shifting = 0;
2667 rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE;
2668 found |= CATEGORY_MASK_ISO_8_ELSE;
2669 goto check_extra_latin;
2670
1562 case ISO_CODE_SS2: 2671 case ISO_CODE_SS2:
1563 case ISO_CODE_SS3: 2672 case ISO_CODE_SS3:
1564 { 2673 /* Single shift. */
1565 int newmask = CODING_CATEGORY_MASK_ISO_8_ELSE; 2674 if (inhibit_iso_escape_detection)
1566 2675 break;
1567 if (inhibit_iso_escape_detection) 2676 single_shifting = 0;
1568 break; 2677 rejected |= CATEGORY_MASK_ISO_7BIT;
1569 if (c != ISO_CODE_CSI) 2678 if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1])
1570 { 2679 & CODING_ISO_FLAG_SINGLE_SHIFT)
1571 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_1]->flags 2680 found |= CATEGORY_MASK_ISO_8_1, single_shifting = 1;
1572 & CODING_FLAG_ISO_SINGLE_SHIFT) 2681 if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_2])
1573 newmask |= CODING_CATEGORY_MASK_ISO_8_1; 2682 & CODING_ISO_FLAG_SINGLE_SHIFT)
1574 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_2]->flags 2683 found |= CATEGORY_MASK_ISO_8_2, single_shifting = 1;
1575 & CODING_FLAG_ISO_SINGLE_SHIFT) 2684 if (single_shifting)
1576 newmask |= CODING_CATEGORY_MASK_ISO_8_2; 2685 break;
1577 single_shifting = 1; 2686 goto check_extra_latin;
1578 }
1579 if (VECTORP (Vlatin_extra_code_table)
1580 && !NILP (XVECTOR (Vlatin_extra_code_table)->contents[c]))
1581 {
1582 if (! *latin_extra_code_state)
1583 {
1584 *latin_extra_code_state = 1;
1585 return 0;
1586 }
1587 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_1]->flags
1588 & CODING_FLAG_ISO_LATIN_EXTRA)
1589 newmask |= CODING_CATEGORY_MASK_ISO_8_1;
1590 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_2]->flags
1591 & CODING_FLAG_ISO_LATIN_EXTRA)
1592 newmask |= CODING_CATEGORY_MASK_ISO_8_2;
1593 }
1594 mask &= newmask;
1595 mask_found |= newmask;
1596 }
1597 break;
1598 2687
1599 default: 2688 default:
2689 if (c < 0)
2690 continue;
1600 if (c < 0x80) 2691 if (c < 0x80)
1601 { 2692 {
1602 single_shifting = 0; 2693 single_shifting = 0;
1603 break; 2694 break;
1604 } 2695 }
1605 else if (c < 0xA0) 2696 if (c >= 0xA0)
1606 { 2697 {
1607 single_shifting = 0; 2698 rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE;
1608 if (VECTORP (Vlatin_extra_code_table) 2699 found |= CATEGORY_MASK_ISO_8_1;
1609 && !NILP (XVECTOR (Vlatin_extra_code_table)->contents[c]))
1610 {
1611 int newmask = 0;
1612
1613 if (! *latin_extra_code_state)
1614 {
1615 *latin_extra_code_state = 1;
1616 return 0;
1617 }
1618 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_1]->flags
1619 & CODING_FLAG_ISO_LATIN_EXTRA)
1620 newmask |= CODING_CATEGORY_MASK_ISO_8_1;
1621 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_2]->flags
1622 & CODING_FLAG_ISO_LATIN_EXTRA)
1623 newmask |= CODING_CATEGORY_MASK_ISO_8_2;
1624 mask &= newmask;
1625 mask_found |= newmask;
1626 }
1627 else
1628 return 0;
1629 }
1630 else
1631 {
1632 mask &= ~(CODING_CATEGORY_MASK_ISO_7BIT
1633 | CODING_CATEGORY_MASK_ISO_7_ELSE);
1634 mask_found |= CODING_CATEGORY_MASK_ISO_8_1;
1635 /* Check the length of succeeding codes of the range 2700 /* Check the length of succeeding codes of the range
1636 0xA0..0FF. If the byte length is odd, we exclude 2701 0xA0..0FF. If the byte length is even, we include
1637 CODING_CATEGORY_MASK_ISO_8_2. We can check this only 2702 CATEGORY_MASK_ISO_8_2 in `found'. We can check this
1638 when we are not single shifting. */ 2703 only when we are not single shifting. */
1639 if (!single_shifting 2704 if (! single_shifting
1640 && mask & CODING_CATEGORY_MASK_ISO_8_2) 2705 && ! (rejected & CATEGORY_MASK_ISO_8_2))
1641 { 2706 {
1642 int i = 1; 2707 int i = 1;
1643
1644 c = -1;
1645 while (src < src_end) 2708 while (src < src_end)
1646 { 2709 {
1647 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep, 2710 ONE_MORE_BYTE (c);
1648 mask & mask_found);
1649 if (c < 0xA0) 2711 if (c < 0xA0)
1650 break; 2712 break;
1651 i++; 2713 i++;
1652 } 2714 }
1653 2715
1654 if (i & 1 && src < src_end) 2716 if (i & 1 && src < src_end)
1655 mask &= ~CODING_CATEGORY_MASK_ISO_8_2; 2717 rejected |= CATEGORY_MASK_ISO_8_2;
1656 else 2718 else
1657 mask_found |= CODING_CATEGORY_MASK_ISO_8_2; 2719 found |= CATEGORY_MASK_ISO_8_2;
1658 if (c >= 0)
1659 /* This means that we have read one extra byte. */
1660 goto retry;
1661 } 2720 }
2721 break;
1662 } 2722 }
1663 break; 2723 check_extra_latin:
2724 single_shifting = 0;
2725 if (! VECTORP (Vlatin_extra_code_table)
2726 || NILP (XVECTOR (Vlatin_extra_code_table)->contents[c]))
2727 {
2728 rejected = CATEGORY_MASK_ISO;
2729 break;
2730 }
2731 if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1])
2732 & CODING_ISO_FLAG_LATIN_EXTRA)
2733 found |= CATEGORY_MASK_ISO_8_1;
2734 else
2735 rejected |= CATEGORY_MASK_ISO_8_1;
2736 rejected |= CATEGORY_MASK_ISO_8_2;
1664 } 2737 }
1665 } 2738 }
1666 return (mask & mask_found); 2739 detect_info->rejected |= CATEGORY_MASK_ISO;
1667} 2740 return 0;
1668 2741
1669/* Decode a character of which charset is CHARSET, the 1st position 2742 no_more_source:
1670 code is C1, the 2nd position code is C2, and return the decoded 2743 detect_info->rejected |= rejected;
1671 character code. If the variable `translation_table' is non-nil, 2744 detect_info->found |= (found & ~rejected);
1672 returned the translated code. */ 2745 return 1;
2746}
1673 2747
1674#define DECODE_ISO_CHARACTER(charset, c1, c2) \
1675 (NILP (translation_table) \
1676 ? MAKE_CHAR (charset, c1, c2) \
1677 : translate_char (translation_table, -1, charset, c1, c2))
1678 2748
1679/* Set designation state into CODING. */ 2749/* Set designation state into CODING. Set CHARS_96 to -1 if the
1680#define DECODE_DESIGNATION(reg, dimension, chars, final_char) \ 2750 escape sequence should be kept. */
1681 do { \ 2751#define DECODE_DESIGNATION(reg, dim, chars_96, final) \
1682 int charset, c; \ 2752 do { \
1683 \ 2753 int id, prev; \
1684 if (final_char < '0' || final_char >= 128) \ 2754 \
1685 goto label_invalid_code; \ 2755 if (final < '0' || final >= 128 \
1686 charset = ISO_CHARSET_TABLE (make_number (dimension), \ 2756 || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \
1687 make_number (chars), \ 2757 || !SAFE_CHARSET_P (coding, id)) \
1688 make_number (final_char)); \ 2758 { \
1689 c = MAKE_CHAR (charset, 0, 0); \ 2759 CODING_ISO_DESIGNATION (coding, reg) = -2; \
1690 if (charset >= 0 \ 2760 chars_96 = -1; \
1691 && (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) == reg \ 2761 break; \
1692 || CODING_SAFE_CHAR_P (safe_chars, c))) \ 2762 } \
1693 { \ 2763 prev = CODING_ISO_DESIGNATION (coding, reg); \
1694 if (coding->spec.iso2022.last_invalid_designation_register == 0 \ 2764 if (id == charset_jisx0201_roman) \
1695 && reg == 0 \ 2765 { \
1696 && charset == CHARSET_ASCII) \ 2766 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
1697 { \ 2767 id = charset_ascii; \
1698 /* We should insert this designation sequence as is so \ 2768 } \
1699 that it is surely written back to a file. */ \ 2769 else if (id == charset_jisx0208_1978) \
1700 coding->spec.iso2022.last_invalid_designation_register = -1; \ 2770 { \
1701 goto label_invalid_code; \ 2771 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
1702 } \ 2772 id = charset_jisx0208; \
1703 coding->spec.iso2022.last_invalid_designation_register = -1; \ 2773 } \
1704 if ((coding->mode & CODING_MODE_DIRECTION) \ 2774 CODING_ISO_DESIGNATION (coding, reg) = id; \
1705 && CHARSET_REVERSE_CHARSET (charset) >= 0) \ 2775 /* If there was an invalid designation to REG previously, and this \
1706 charset = CHARSET_REVERSE_CHARSET (charset); \ 2776 designation is ASCII to REG, we should keep this designation \
1707 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \ 2777 sequence. */ \
1708 } \ 2778 if (prev == -2 && id == charset_ascii) \
1709 else \ 2779 chars_96 = -1; \
1710 { \
1711 coding->spec.iso2022.last_invalid_designation_register = reg; \
1712 goto label_invalid_code; \
1713 } \
1714 } while (0) 2780 } while (0)
1715 2781
1716/* Allocate a memory block for storing information about compositions.
1717 The block is chained to the already allocated blocks. */
1718 2782
1719void 2783#define MAYBE_FINISH_COMPOSITION() \
1720coding_allocate_composition_data (coding, char_offset) 2784 do { \
1721 struct coding_system *coding; 2785 int i; \
1722 int char_offset; 2786 if (composition_state == COMPOSING_NO) \
1723{ 2787 break; \
1724 struct composition_data *cmp_data 2788 /* It is assured that we have enough room for producing \
1725 = (struct composition_data *) xmalloc (sizeof *cmp_data); 2789 characters stored in the table `components'. */ \
1726 2790 if (charbuf + component_idx > charbuf_end) \
1727 cmp_data->char_offset = char_offset; 2791 goto no_more_source; \
1728 cmp_data->used = 0; 2792 composition_state = COMPOSING_NO; \
1729 cmp_data->prev = coding->cmp_data; 2793 if (method == COMPOSITION_RELATIVE \
1730 cmp_data->next = NULL; 2794 || method == COMPOSITION_WITH_ALTCHARS) \
1731 if (coding->cmp_data) 2795 { \
1732 coding->cmp_data->next = cmp_data; 2796 for (i = 0; i < component_idx; i++) \
1733 coding->cmp_data = cmp_data; 2797 *charbuf++ = components[i]; \
1734 coding->cmp_data_start = 0; 2798 char_offset += component_idx; \
1735 coding->composing = COMPOSITION_NO; 2799 } \
1736} 2800 else \
2801 { \
2802 for (i = 0; i < component_idx; i += 2) \
2803 *charbuf++ = components[i]; \
2804 char_offset += (component_idx / 2) + 1; \
2805 } \
2806 } while (0)
2807
1737 2808
1738/* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4. 2809/* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
1739 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1 2810 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
1740 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1 2811 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
1741 ESC 3 : altchar composition : ESC 3 ALT ... ESC 0 CHAR ... ESC 1 2812 ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1
1742 ESC 4 : alt&rule composition : ESC 4 ALT RULE .. ALT ESC 0 CHAR ... ESC 1 2813 ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1
1743 */ 2814 */
1744 2815
1745#define DECODE_COMPOSITION_START(c1) \ 2816#define DECODE_COMPOSITION_START(c1) \
1746 do { \ 2817 do { \
1747 if (coding->composing == COMPOSITION_DISABLED) \ 2818 if (c1 == '0' \
1748 { \ 2819 && composition_state == COMPOSING_COMPONENT_RULE) \
1749 *dst++ = ISO_CODE_ESC; \ 2820 { \
1750 *dst++ = c1 & 0x7f; \ 2821 component_len = component_idx; \
1751 coding->produced_char += 2; \ 2822 composition_state = COMPOSING_CHAR; \
1752 } \ 2823 } \
1753 else if (!COMPOSING_P (coding)) \ 2824 else \
1754 { \ 2825 { \
1755 /* This is surely the start of a composition. We must be sure \ 2826 const unsigned char *p; \
1756 that coding->cmp_data has enough space to store the \ 2827 \
1757 information about the composition. If not, terminate the \ 2828 MAYBE_FINISH_COMPOSITION (); \
1758 current decoding loop, allocate one more memory block for \ 2829 if (charbuf + MAX_COMPOSITION_COMPONENTS > charbuf_end) \
1759 coding->cmp_data in the caller, then start the decoding \ 2830 goto no_more_source; \
1760 loop again. We can't allocate memory here directly because \ 2831 for (p = src; p < src_end - 1; p++) \
1761 it may cause buffer/string relocation. */ \ 2832 if (*p == ISO_CODE_ESC && p[1] == '1') \
1762 if (!coding->cmp_data \ 2833 break; \
1763 || (coding->cmp_data->used + COMPOSITION_DATA_MAX_BUNCH_LENGTH \ 2834 if (p == src_end - 1) \
1764 >= COMPOSITION_DATA_SIZE)) \ 2835 { \
1765 { \ 2836 /* The current composition doesn't end in the current \
1766 coding->result = CODING_FINISH_INSUFFICIENT_CMP; \ 2837 source. */ \
1767 goto label_end_of_loop; \ 2838 record_conversion_result \
1768 } \ 2839 (coding, CODING_RESULT_INSUFFICIENT_SRC); \
1769 coding->composing = (c1 == '0' ? COMPOSITION_RELATIVE \ 2840 goto no_more_source; \
1770 : c1 == '2' ? COMPOSITION_WITH_RULE \ 2841 } \
1771 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \ 2842 \
1772 : COMPOSITION_WITH_RULE_ALTCHARS); \ 2843 /* This is surely the start of a composition. */ \
1773 CODING_ADD_COMPOSITION_START (coding, coding->produced_char, \ 2844 method = (c1 == '0' ? COMPOSITION_RELATIVE \
1774 coding->composing); \ 2845 : c1 == '2' ? COMPOSITION_WITH_RULE \
1775 coding->composition_rule_follows = 0; \ 2846 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
1776 } \ 2847 : COMPOSITION_WITH_RULE_ALTCHARS); \
1777 else \ 2848 composition_state = (c1 <= '2' ? COMPOSING_CHAR \
1778 { \ 2849 : COMPOSING_COMPONENT_CHAR); \
1779 /* We are already handling a composition. If the method is \ 2850 component_idx = component_len = 0; \
1780 the following two, the codes following the current escape \ 2851 } \
1781 sequence are actual characters stored in a buffer. */ \
1782 if (coding->composing == COMPOSITION_WITH_ALTCHARS \
1783 || coding->composing == COMPOSITION_WITH_RULE_ALTCHARS) \
1784 { \
1785 coding->composing = COMPOSITION_RELATIVE; \
1786 coding->composition_rule_follows = 0; \
1787 } \
1788 } \
1789 } while (0) 2852 } while (0)
1790 2853
1791/* Handle composition end sequence ESC 1. */
1792 2854
1793#define DECODE_COMPOSITION_END(c1) \ 2855/* Handle compositoin end sequence ESC 1. */
2856
2857#define DECODE_COMPOSITION_END() \
1794 do { \ 2858 do { \
1795 if (! COMPOSING_P (coding)) \ 2859 int nchars = (component_len > 0 ? component_idx - component_len \
2860 : method == COMPOSITION_RELATIVE ? component_idx \
2861 : (component_idx + 1) / 2); \
2862 int i; \
2863 int *saved_charbuf = charbuf; \
2864 \
2865 ADD_COMPOSITION_DATA (charbuf, nchars, method); \
2866 if (method != COMPOSITION_RELATIVE) \
1796 { \ 2867 { \
1797 *dst++ = ISO_CODE_ESC; \ 2868 if (component_len == 0) \
1798 *dst++ = c1; \ 2869 for (i = 0; i < component_idx; i++) \
1799 coding->produced_char += 2; \ 2870 *charbuf++ = components[i]; \
2871 else \
2872 for (i = 0; i < component_len; i++) \
2873 *charbuf++ = components[i]; \
2874 *saved_charbuf = saved_charbuf - charbuf; \
1800 } \ 2875 } \
2876 if (method == COMPOSITION_WITH_RULE) \
2877 for (i = 0; i < component_idx; i += 2, char_offset++) \
2878 *charbuf++ = components[i]; \
1801 else \ 2879 else \
1802 { \ 2880 for (i = component_len; i < component_idx; i++, char_offset++) \
1803 CODING_ADD_COMPOSITION_END (coding, coding->produced_char); \ 2881 *charbuf++ = components[i]; \
1804 coding->composing = COMPOSITION_NO; \ 2882 coding->annotated = 1; \
1805 } \ 2883 composition_state = COMPOSING_NO; \
1806 } while (0) 2884 } while (0)
1807 2885
2886
1808/* Decode a composition rule from the byte C1 (and maybe one more byte 2887/* Decode a composition rule from the byte C1 (and maybe one more byte
1809 from SRC) and store one encoded composition rule in 2888 from SRC) and store one encoded composition rule in
1810 coding->cmp_data. */ 2889 coding->cmp_data. */
1811 2890
1812#define DECODE_COMPOSITION_RULE(c1) \ 2891#define DECODE_COMPOSITION_RULE(c1) \
1813 do { \ 2892 do { \
1814 int rule = 0; \
1815 (c1) -= 32; \ 2893 (c1) -= 32; \
1816 if (c1 < 81) /* old format (before ver.21) */ \ 2894 if (c1 < 81) /* old format (before ver.21) */ \
1817 { \ 2895 { \
@@ -1819,168 +2897,164 @@ coding_allocate_composition_data (coding, char_offset)
1819 int nref = (c1) % 9; \ 2897 int nref = (c1) % 9; \
1820 if (gref == 4) gref = 10; \ 2898 if (gref == 4) gref = 10; \
1821 if (nref == 4) nref = 10; \ 2899 if (nref == 4) nref = 10; \
1822 rule = COMPOSITION_ENCODE_RULE (gref, nref); \ 2900 c1 = COMPOSITION_ENCODE_RULE (gref, nref); \
1823 } \ 2901 } \
1824 else if (c1 < 93) /* new format (after ver.21) */ \ 2902 else if (c1 < 93) /* new format (after ver.21) */ \
1825 { \ 2903 { \
1826 ONE_MORE_BYTE (c2); \ 2904 ONE_MORE_BYTE (c2); \
1827 rule = COMPOSITION_ENCODE_RULE (c1 - 81, c2 - 32); \ 2905 c1 = COMPOSITION_ENCODE_RULE (c1 - 81, c2 - 32); \
1828 } \ 2906 } \
1829 CODING_ADD_COMPOSITION_COMPONENT (coding, rule); \ 2907 else \
1830 coding->composition_rule_follows = 0; \ 2908 c1 = 0; \
1831 } while (0) 2909 } while (0)
1832 2910
1833 2911
1834/* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ 2912/* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
1835 2913
1836static void 2914static void
1837decode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes) 2915decode_coding_iso_2022 (coding)
1838 struct coding_system *coding; 2916 struct coding_system *coding;
1839 const unsigned char *source;
1840 unsigned char *destination;
1841 int src_bytes, dst_bytes;
1842{ 2917{
1843 const unsigned char *src = source; 2918 const unsigned char *src = coding->source + coding->consumed;
1844 const unsigned char *src_end = source + src_bytes; 2919 const unsigned char *src_end = coding->source + coding->src_bytes;
1845 unsigned char *dst = destination;
1846 unsigned char *dst_end = destination + dst_bytes;
1847 /* Charsets invoked to graphic plane 0 and 1 respectively. */
1848 int charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
1849 int charset1 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 1);
1850 /* SRC_BASE remembers the start position in source in each loop.
1851 The loop will be exited when there's not enough source code
1852 (within macro ONE_MORE_BYTE), or when there's not enough
1853 destination area to produce a character (within macro
1854 EMIT_CHAR). */
1855 const unsigned char *src_base; 2920 const unsigned char *src_base;
1856 int c, charset; 2921 int *charbuf = coding->charbuf + coding->charbuf_used;
1857 Lisp_Object translation_table; 2922 int *charbuf_end
1858 Lisp_Object safe_chars; 2923 = coding->charbuf + coding->charbuf_size - 4 - MAX_ANNOTATION_LENGTH;
1859 2924 int consumed_chars = 0, consumed_chars_base;
1860 safe_chars = coding_safe_chars (coding->symbol); 2925 int multibytep = coding->src_multibyte;
1861 2926 /* Charsets invoked to graphic plane 0 and 1 respectively. */
1862 if (NILP (Venable_character_translation)) 2927 int charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
1863 translation_table = Qnil; 2928 int charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1);
1864 else 2929 int charset_id_2, charset_id_3;
1865 { 2930 struct charset *charset;
1866 translation_table = coding->translation_table_for_decode; 2931 int c;
1867 if (NILP (translation_table)) 2932 /* For handling composition sequence. */
1868 translation_table = Vstandard_translation_table_for_decode; 2933#define COMPOSING_NO 0
1869 } 2934#define COMPOSING_CHAR 1
1870 2935#define COMPOSING_RULE 2
1871 coding->result = CODING_FINISH_NORMAL; 2936#define COMPOSING_COMPONENT_CHAR 3
2937#define COMPOSING_COMPONENT_RULE 4
2938
2939 int composition_state = COMPOSING_NO;
2940 enum composition_method method;
2941 int components[MAX_COMPOSITION_COMPONENTS * 2 + 1];
2942 int component_idx;
2943 int component_len;
2944 Lisp_Object attrs, charset_list;
2945 int char_offset = coding->produced_char;
2946 int last_offset = char_offset;
2947 int last_id = charset_ascii;
2948
2949 CODING_GET_INFO (coding, attrs, charset_list);
2950 setup_iso_safe_charsets (attrs);
2951 /* Charset list may have been changed. */
2952 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
2953 coding->safe_charsets = (char *) SDATA (CODING_ATTR_SAFE_CHARSETS(attrs));
1872 2954
1873 while (1) 2955 while (1)
1874 { 2956 {
1875 int c1, c2 = 0; 2957 int c1, c2;
1876 2958
1877 src_base = src; 2959 src_base = src;
2960 consumed_chars_base = consumed_chars;
2961
2962 if (charbuf >= charbuf_end)
2963 break;
2964
1878 ONE_MORE_BYTE (c1); 2965 ONE_MORE_BYTE (c1);
2966 if (c1 < 0)
2967 goto invalid_code;
1879 2968
1880 /* We produce no character or one character. */ 2969 /* We produce at most one character. */
1881 switch (iso_code_class [c1]) 2970 switch (iso_code_class [c1])
1882 { 2971 {
1883 case ISO_0x20_or_0x7F: 2972 case ISO_0x20_or_0x7F:
1884 if (COMPOSING_P (coding) && coding->composition_rule_follows) 2973 if (composition_state != COMPOSING_NO)
1885 {
1886 DECODE_COMPOSITION_RULE (c1);
1887 continue;
1888 }
1889 if (charset0 < 0 || CHARSET_CHARS (charset0) == 94)
1890 { 2974 {
1891 /* This is SPACE or DEL. */ 2975 if (composition_state == COMPOSING_RULE
1892 charset = CHARSET_ASCII; 2976 || composition_state == COMPOSING_COMPONENT_RULE)
1893 break; 2977 {
2978 DECODE_COMPOSITION_RULE (c1);
2979 components[component_idx++] = c1;
2980 composition_state--;
2981 continue;
2982 }
1894 } 2983 }
1895 /* This is a graphic character, we fall down ... */ 2984 if (charset_id_0 < 0
2985 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0)))
2986 /* This is SPACE or DEL. */
2987 charset = CHARSET_FROM_ID (charset_ascii);
2988 else
2989 charset = CHARSET_FROM_ID (charset_id_0);
2990 break;
1896 2991
1897 case ISO_graphic_plane_0: 2992 case ISO_graphic_plane_0:
1898 if (COMPOSING_P (coding) && coding->composition_rule_follows) 2993 if (composition_state != COMPOSING_NO)
1899 { 2994 {
1900 DECODE_COMPOSITION_RULE (c1); 2995 if (composition_state == COMPOSING_RULE
1901 continue; 2996 || composition_state == COMPOSING_COMPONENT_RULE)
2997 {
2998 DECODE_COMPOSITION_RULE (c1);
2999 components[component_idx++] = c1;
3000 composition_state--;
3001 continue;
3002 }
1902 } 3003 }
1903 charset = charset0; 3004 if (charset_id_0 < 0)
3005 charset = CHARSET_FROM_ID (charset_ascii);
3006 else
3007 charset = CHARSET_FROM_ID (charset_id_0);
1904 break; 3008 break;
1905 3009
1906 case ISO_0xA0_or_0xFF: 3010 case ISO_0xA0_or_0xFF:
1907 if (charset1 < 0 || CHARSET_CHARS (charset1) == 94 3011 if (charset_id_1 < 0
1908 || coding->flags & CODING_FLAG_ISO_SEVEN_BITS) 3012 || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1))
1909 goto label_invalid_code; 3013 || CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS)
3014 goto invalid_code;
1910 /* This is a graphic character, we fall down ... */ 3015 /* This is a graphic character, we fall down ... */
1911 3016
1912 case ISO_graphic_plane_1: 3017 case ISO_graphic_plane_1:
1913 if (charset1 < 0) 3018 if (charset_id_1 < 0)
1914 goto label_invalid_code; 3019 goto invalid_code;
1915 charset = charset1; 3020 charset = CHARSET_FROM_ID (charset_id_1);
1916 break; 3021 break;
1917 3022
1918 case ISO_control_0: 3023 case ISO_control_0:
1919 if (COMPOSING_P (coding)) 3024 MAYBE_FINISH_COMPOSITION ();
1920 DECODE_COMPOSITION_END ('1'); 3025 charset = CHARSET_FROM_ID (charset_ascii);
1921
1922 /* All ISO2022 control characters in this class have the
1923 same representation in Emacs internal format. */
1924 if (c1 == '\n'
1925 && (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
1926 && (coding->eol_type == CODING_EOL_CR
1927 || coding->eol_type == CODING_EOL_CRLF))
1928 {
1929 coding->result = CODING_FINISH_INCONSISTENT_EOL;
1930 goto label_end_of_loop;
1931 }
1932 charset = CHARSET_ASCII;
1933 break; 3026 break;
1934 3027
1935 case ISO_control_1: 3028 case ISO_control_1:
1936 if (COMPOSING_P (coding)) 3029 MAYBE_FINISH_COMPOSITION ();
1937 DECODE_COMPOSITION_END ('1'); 3030 goto invalid_code;
1938 goto label_invalid_code;
1939
1940 case ISO_carriage_return:
1941 if (COMPOSING_P (coding))
1942 DECODE_COMPOSITION_END ('1');
1943
1944 if (coding->eol_type == CODING_EOL_CR)
1945 c1 = '\n';
1946 else if (coding->eol_type == CODING_EOL_CRLF)
1947 {
1948 ONE_MORE_BYTE (c1);
1949 if (c1 != ISO_CODE_LF)
1950 {
1951 src--;
1952 c1 = '\r';
1953 }
1954 }
1955 charset = CHARSET_ASCII;
1956 break;
1957 3031
1958 case ISO_shift_out: 3032 case ISO_shift_out:
1959 if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT) 3033 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT)
1960 || CODING_SPEC_ISO_DESIGNATION (coding, 1) < 0) 3034 || CODING_ISO_DESIGNATION (coding, 1) < 0)
1961 goto label_invalid_code; 3035 goto invalid_code;
1962 CODING_SPEC_ISO_INVOCATION (coding, 0) = 1; 3036 CODING_ISO_INVOCATION (coding, 0) = 1;
1963 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0); 3037 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
1964 continue; 3038 continue;
1965 3039
1966 case ISO_shift_in: 3040 case ISO_shift_in:
1967 if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT)) 3041 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT))
1968 goto label_invalid_code; 3042 goto invalid_code;
1969 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0; 3043 CODING_ISO_INVOCATION (coding, 0) = 0;
1970 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0); 3044 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
1971 continue; 3045 continue;
1972 3046
1973 case ISO_single_shift_2_7: 3047 case ISO_single_shift_2_7:
1974 case ISO_single_shift_2: 3048 case ISO_single_shift_2:
1975 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT)) 3049 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT))
1976 goto label_invalid_code; 3050 goto invalid_code;
1977 /* SS2 is handled as an escape sequence of ESC 'N' */ 3051 /* SS2 is handled as an escape sequence of ESC 'N' */
1978 c1 = 'N'; 3052 c1 = 'N';
1979 goto label_escape_sequence; 3053 goto label_escape_sequence;
1980 3054
1981 case ISO_single_shift_3: 3055 case ISO_single_shift_3:
1982 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT)) 3056 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT))
1983 goto label_invalid_code; 3057 goto invalid_code;
1984 /* SS2 is handled as an escape sequence of ESC 'O' */ 3058 /* SS2 is handled as an escape sequence of ESC 'O' */
1985 c1 = 'O'; 3059 c1 = 'O';
1986 goto label_escape_sequence; 3060 goto label_escape_sequence;
@@ -1993,7 +3067,7 @@ decode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
1993 case ISO_escape: 3067 case ISO_escape:
1994 ONE_MORE_BYTE (c1); 3068 ONE_MORE_BYTE (c1);
1995 label_escape_sequence: 3069 label_escape_sequence:
1996 /* Escape sequences handled by Emacs are invocation, 3070 /* Escape sequences handled here are invocation,
1997 designation, direction specification, and character 3071 designation, direction specification, and character
1998 composition specification. */ 3072 composition specification. */
1999 switch (c1) 3073 switch (c1)
@@ -2001,89 +3075,110 @@ decode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
2001 case '&': /* revision of following character set */ 3075 case '&': /* revision of following character set */
2002 ONE_MORE_BYTE (c1); 3076 ONE_MORE_BYTE (c1);
2003 if (!(c1 >= '@' && c1 <= '~')) 3077 if (!(c1 >= '@' && c1 <= '~'))
2004 goto label_invalid_code; 3078 goto invalid_code;
2005 ONE_MORE_BYTE (c1); 3079 ONE_MORE_BYTE (c1);
2006 if (c1 != ISO_CODE_ESC) 3080 if (c1 != ISO_CODE_ESC)
2007 goto label_invalid_code; 3081 goto invalid_code;
2008 ONE_MORE_BYTE (c1); 3082 ONE_MORE_BYTE (c1);
2009 goto label_escape_sequence; 3083 goto label_escape_sequence;
2010 3084
2011 case '$': /* designation of 2-byte character set */ 3085 case '$': /* designation of 2-byte character set */
2012 if (! (coding->flags & CODING_FLAG_ISO_DESIGNATION)) 3086 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION))
2013 goto label_invalid_code; 3087 goto invalid_code;
2014 ONE_MORE_BYTE (c1); 3088 {
2015 if (c1 >= '@' && c1 <= 'B') 3089 int reg, chars96;
2016 { /* designation of JISX0208.1978, GB2312.1980, 3090
3091 ONE_MORE_BYTE (c1);
3092 if (c1 >= '@' && c1 <= 'B')
3093 { /* designation of JISX0208.1978, GB2312.1980,
2017 or JISX0208.1980 */ 3094 or JISX0208.1980 */
2018 DECODE_DESIGNATION (0, 2, 94, c1); 3095 reg = 0, chars96 = 0;
2019 } 3096 }
2020 else if (c1 >= 0x28 && c1 <= 0x2B) 3097 else if (c1 >= 0x28 && c1 <= 0x2B)
2021 { /* designation of DIMENSION2_CHARS94 character set */ 3098 { /* designation of DIMENSION2_CHARS94 character set */
2022 ONE_MORE_BYTE (c2); 3099 reg = c1 - 0x28, chars96 = 0;
2023 DECODE_DESIGNATION (c1 - 0x28, 2, 94, c2); 3100 ONE_MORE_BYTE (c1);
2024 } 3101 }
2025 else if (c1 >= 0x2C && c1 <= 0x2F) 3102 else if (c1 >= 0x2C && c1 <= 0x2F)
2026 { /* designation of DIMENSION2_CHARS96 character set */ 3103 { /* designation of DIMENSION2_CHARS96 character set */
2027 ONE_MORE_BYTE (c2); 3104 reg = c1 - 0x2C, chars96 = 1;
2028 DECODE_DESIGNATION (c1 - 0x2C, 2, 96, c2); 3105 ONE_MORE_BYTE (c1);
2029 } 3106 }
2030 else 3107 else
2031 goto label_invalid_code; 3108 goto invalid_code;
2032 /* We must update these variables now. */ 3109 DECODE_DESIGNATION (reg, 2, chars96, c1);
2033 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0); 3110 /* We must update these variables now. */
2034 charset1 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 1); 3111 if (reg == 0)
3112 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
3113 else if (reg == 1)
3114 charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1);
3115 if (chars96 < 0)
3116 goto invalid_code;
3117 }
2035 continue; 3118 continue;
2036 3119
2037 case 'n': /* invocation of locking-shift-2 */ 3120 case 'n': /* invocation of locking-shift-2 */
2038 if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT) 3121 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT)
2039 || CODING_SPEC_ISO_DESIGNATION (coding, 2) < 0) 3122 || CODING_ISO_DESIGNATION (coding, 2) < 0)
2040 goto label_invalid_code; 3123 goto invalid_code;
2041 CODING_SPEC_ISO_INVOCATION (coding, 0) = 2; 3124 CODING_ISO_INVOCATION (coding, 0) = 2;
2042 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0); 3125 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
2043 continue; 3126 continue;
2044 3127
2045 case 'o': /* invocation of locking-shift-3 */ 3128 case 'o': /* invocation of locking-shift-3 */
2046 if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT) 3129 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT)
2047 || CODING_SPEC_ISO_DESIGNATION (coding, 3) < 0) 3130 || CODING_ISO_DESIGNATION (coding, 3) < 0)
2048 goto label_invalid_code; 3131 goto invalid_code;
2049 CODING_SPEC_ISO_INVOCATION (coding, 0) = 3; 3132 CODING_ISO_INVOCATION (coding, 0) = 3;
2050 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0); 3133 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
2051 continue; 3134 continue;
2052 3135
2053 case 'N': /* invocation of single-shift-2 */ 3136 case 'N': /* invocation of single-shift-2 */
2054 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT) 3137 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)
2055 || CODING_SPEC_ISO_DESIGNATION (coding, 2) < 0) 3138 || CODING_ISO_DESIGNATION (coding, 2) < 0)
2056 goto label_invalid_code; 3139 goto invalid_code;
2057 charset = CODING_SPEC_ISO_DESIGNATION (coding, 2); 3140 charset_id_2 = CODING_ISO_DESIGNATION (coding, 2);
3141 if (charset_id_2 < 0)
3142 charset = CHARSET_FROM_ID (charset_ascii);
3143 else
3144 charset = CHARSET_FROM_ID (charset_id_2);
2058 ONE_MORE_BYTE (c1); 3145 ONE_MORE_BYTE (c1);
2059 if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0)) 3146 if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0))
2060 goto label_invalid_code; 3147 goto invalid_code;
2061 break; 3148 break;
2062 3149
2063 case 'O': /* invocation of single-shift-3 */ 3150 case 'O': /* invocation of single-shift-3 */
2064 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT) 3151 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)
2065 || CODING_SPEC_ISO_DESIGNATION (coding, 3) < 0) 3152 || CODING_ISO_DESIGNATION (coding, 3) < 0)
2066 goto label_invalid_code; 3153 goto invalid_code;
2067 charset = CODING_SPEC_ISO_DESIGNATION (coding, 3); 3154 charset_id_3 = CODING_ISO_DESIGNATION (coding, 3);
3155 if (charset_id_3 < 0)
3156 charset = CHARSET_FROM_ID (charset_ascii);
3157 else
3158 charset = CHARSET_FROM_ID (charset_id_3);
2068 ONE_MORE_BYTE (c1); 3159 ONE_MORE_BYTE (c1);
2069 if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0)) 3160 if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0))
2070 goto label_invalid_code; 3161 goto invalid_code;
2071 break; 3162 break;
2072 3163
2073 case '0': case '2': case '3': case '4': /* start composition */ 3164 case '0': case '2': case '3': case '4': /* start composition */
3165 if (! (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK))
3166 goto invalid_code;
2074 DECODE_COMPOSITION_START (c1); 3167 DECODE_COMPOSITION_START (c1);
2075 continue; 3168 continue;
2076 3169
2077 case '1': /* end composition */ 3170 case '1': /* end composition */
2078 DECODE_COMPOSITION_END (c1); 3171 if (composition_state == COMPOSING_NO)
3172 goto invalid_code;
3173 DECODE_COMPOSITION_END ();
2079 continue; 3174 continue;
2080 3175
2081 case '[': /* specification of direction */ 3176 case '[': /* specification of direction */
2082 if (coding->flags & CODING_FLAG_ISO_NO_DIRECTION) 3177 if (! CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DIRECTION)
2083 goto label_invalid_code; 3178 goto invalid_code;
2084 /* For the moment, nested direction is not supported. 3179 /* For the moment, nested direction is not supported.
2085 So, `coding->mode & CODING_MODE_DIRECTION' zero means 3180 So, `coding->mode & CODING_MODE_DIRECTION' zero means
2086 left-to-right, and nonzero means right-to-left. */ 3181 left-to-right, and nozero means right-to-left. */
2087 ONE_MORE_BYTE (c1); 3182 ONE_MORE_BYTE (c1);
2088 switch (c1) 3183 switch (c1)
2089 { 3184 {
@@ -2096,7 +3191,7 @@ decode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
2096 if (c1 == ']') 3191 if (c1 == ']')
2097 coding->mode &= ~CODING_MODE_DIRECTION; 3192 coding->mode &= ~CODING_MODE_DIRECTION;
2098 else 3193 else
2099 goto label_invalid_code; 3194 goto invalid_code;
2100 break; 3195 break;
2101 3196
2102 case '2': /* start of right-to-left direction */ 3197 case '2': /* start of right-to-left direction */
@@ -2104,17 +3199,15 @@ decode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
2104 if (c1 == ']') 3199 if (c1 == ']')
2105 coding->mode |= CODING_MODE_DIRECTION; 3200 coding->mode |= CODING_MODE_DIRECTION;
2106 else 3201 else
2107 goto label_invalid_code; 3202 goto invalid_code;
2108 break; 3203 break;
2109 3204
2110 default: 3205 default:
2111 goto label_invalid_code; 3206 goto invalid_code;
2112 } 3207 }
2113 continue; 3208 continue;
2114 3209
2115 case '%': 3210 case '%':
2116 if (COMPOSING_P (coding))
2117 DECODE_COMPOSITION_END ('1');
2118 ONE_MORE_BYTE (c1); 3211 ONE_MORE_BYTE (c1);
2119 if (c1 == '/') 3212 if (c1 == '/')
2120 { 3213 {
@@ -2123,46 +3216,40 @@ decode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
2123 We keep these bytes as is for the moment. 3216 We keep these bytes as is for the moment.
2124 They may be decoded by post-read-conversion. */ 3217 They may be decoded by post-read-conversion. */
2125 int dim, M, L; 3218 int dim, M, L;
2126 int size, required; 3219 int size;
2127 int produced_chars;
2128 3220
2129 ONE_MORE_BYTE (dim); 3221 ONE_MORE_BYTE (dim);
2130 ONE_MORE_BYTE (M); 3222 ONE_MORE_BYTE (M);
2131 ONE_MORE_BYTE (L); 3223 ONE_MORE_BYTE (L);
2132 size = ((M - 128) * 128) + (L - 128); 3224 size = ((M - 128) * 128) + (L - 128);
2133 required = 8 + size * 2; 3225 if (charbuf + 8 + size > charbuf_end)
2134 if (dst + required > (dst_bytes ? dst_end : src)) 3226 goto break_loop;
2135 goto label_end_of_loop; 3227 *charbuf++ = ISO_CODE_ESC;
2136 *dst++ = ISO_CODE_ESC; 3228 *charbuf++ = '%';
2137 *dst++ = '%'; 3229 *charbuf++ = '/';
2138 *dst++ = '/'; 3230 *charbuf++ = dim;
2139 *dst++ = dim; 3231 *charbuf++ = BYTE8_TO_CHAR (M);
2140 produced_chars = 4; 3232 *charbuf++ = BYTE8_TO_CHAR (L);
2141 dst += CHAR_STRING (M, dst), produced_chars++;
2142 dst += CHAR_STRING (L, dst), produced_chars++;
2143 while (size-- > 0) 3233 while (size-- > 0)
2144 { 3234 {
2145 ONE_MORE_BYTE (c1); 3235 ONE_MORE_BYTE (c1);
2146 dst += CHAR_STRING (c1, dst), produced_chars++; 3236 *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1);
2147 } 3237 }
2148 coding->produced_char += produced_chars;
2149 } 3238 }
2150 else if (c1 == 'G') 3239 else if (c1 == 'G')
2151 { 3240 {
2152 unsigned char *d = dst;
2153 int produced_chars;
2154
2155 /* XFree86 extension for embedding UTF-8 in CTEXT: 3241 /* XFree86 extension for embedding UTF-8 in CTEXT:
2156 ESC % G --UTF-8-BYTES-- ESC % @ 3242 ESC % G --UTF-8-BYTES-- ESC % @
2157 We keep these bytes as is for the moment. 3243 We keep these bytes as is for the moment.
2158 They may be decoded by post-read-conversion. */ 3244 They may be decoded by post-read-conversion. */
2159 if (d + 6 > (dst_bytes ? dst_end : src)) 3245 int *p = charbuf;
2160 goto label_end_of_loop; 3246
2161 *d++ = ISO_CODE_ESC; 3247 if (p + 6 > charbuf_end)
2162 *d++ = '%'; 3248 goto break_loop;
2163 *d++ = 'G'; 3249 *p++ = ISO_CODE_ESC;
2164 produced_chars = 3; 3250 *p++ = '%';
2165 while (d + 1 < (dst_bytes ? dst_end : src)) 3251 *p++ = 'G';
3252 while (p < charbuf_end)
2166 { 3253 {
2167 ONE_MORE_BYTE (c1); 3254 ONE_MORE_BYTE (c1);
2168 if (c1 == ISO_CODE_ESC 3255 if (c1 == ISO_CODE_ESC
@@ -2173,71 +3260,128 @@ decode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
2173 src += 2; 3260 src += 2;
2174 break; 3261 break;
2175 } 3262 }
2176 d += CHAR_STRING (c1, d), produced_chars++; 3263 *p++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1);
2177 } 3264 }
2178 if (d + 3 > (dst_bytes ? dst_end : src)) 3265 if (p + 3 > charbuf_end)
2179 goto label_end_of_loop; 3266 goto break_loop;
2180 *d++ = ISO_CODE_ESC; 3267 *p++ = ISO_CODE_ESC;
2181 *d++ = '%'; 3268 *p++ = '%';
2182 *d++ = '@'; 3269 *p++ = '@';
2183 dst = d; 3270 charbuf = p;
2184 coding->produced_char += produced_chars + 3;
2185 } 3271 }
2186 else 3272 else
2187 goto label_invalid_code; 3273 goto invalid_code;
2188 continue; 3274 continue;
3275 break;
2189 3276
2190 default: 3277 default:
2191 if (! (coding->flags & CODING_FLAG_ISO_DESIGNATION)) 3278 if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION))
2192 goto label_invalid_code; 3279 goto invalid_code;
2193 if (c1 >= 0x28 && c1 <= 0x2B) 3280 {
2194 { /* designation of DIMENSION1_CHARS94 character set */ 3281 int reg, chars96;
2195 ONE_MORE_BYTE (c2); 3282
2196 DECODE_DESIGNATION (c1 - 0x28, 1, 94, c2); 3283 if (c1 >= 0x28 && c1 <= 0x2B)
2197 } 3284 { /* designation of DIMENSION1_CHARS94 character set */
2198 else if (c1 >= 0x2C && c1 <= 0x2F) 3285 reg = c1 - 0x28, chars96 = 0;
2199 { /* designation of DIMENSION1_CHARS96 character set */ 3286 ONE_MORE_BYTE (c1);
2200 ONE_MORE_BYTE (c2); 3287 }
2201 DECODE_DESIGNATION (c1 - 0x2C, 1, 96, c2); 3288 else if (c1 >= 0x2C && c1 <= 0x2F)
2202 } 3289 { /* designation of DIMENSION1_CHARS96 character set */
2203 else 3290 reg = c1 - 0x2C, chars96 = 1;
2204 goto label_invalid_code; 3291 ONE_MORE_BYTE (c1);
2205 /* We must update these variables now. */ 3292 }
2206 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0); 3293 else
2207 charset1 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 1); 3294 goto invalid_code;
3295 DECODE_DESIGNATION (reg, 1, chars96, c1);
3296 /* We must update these variables now. */
3297 if (reg == 0)
3298 charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0);
3299 else if (reg == 1)
3300 charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1);
3301 if (chars96 < 0)
3302 goto invalid_code;
3303 }
2208 continue; 3304 continue;
2209 } 3305 }
2210 } 3306 }
2211 3307
3308 if (charset->id != charset_ascii
3309 && last_id != charset->id)
3310 {
3311 if (last_id != charset_ascii)
3312 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
3313 last_id = charset->id;
3314 last_offset = char_offset;
3315 }
3316
2212 /* Now we know CHARSET and 1st position code C1 of a character. 3317 /* Now we know CHARSET and 1st position code C1 of a character.
2213 Produce a multibyte sequence for that character while getting 3318 Produce a decoded character while getting 2nd position code
2214 2nd position code C2 if necessary. */ 3319 C2 if necessary. */
2215 if (CHARSET_DIMENSION (charset) == 2) 3320 c1 &= 0x7F;
3321 if (CHARSET_DIMENSION (charset) > 1)
2216 { 3322 {
2217 ONE_MORE_BYTE (c2); 3323 ONE_MORE_BYTE (c2);
2218 if (c1 < 0x80 ? c2 < 0x20 || c2 >= 0x80 : c2 < 0xA0) 3324 if (c2 < 0x20 || (c2 >= 0x80 && c2 < 0xA0))
2219 /* C2 is not in a valid range. */ 3325 /* C2 is not in a valid range. */
2220 goto label_invalid_code; 3326 goto invalid_code;
3327 c1 = (c1 << 8) | (c2 & 0x7F);
3328 if (CHARSET_DIMENSION (charset) > 2)
3329 {
3330 ONE_MORE_BYTE (c2);
3331 if (c2 < 0x20 || (c2 >= 0x80 && c2 < 0xA0))
3332 /* C2 is not in a valid range. */
3333 goto invalid_code;
3334 c1 = (c1 << 8) | (c2 & 0x7F);
3335 }
3336 }
3337
3338 CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c1, c);
3339 if (c < 0)
3340 {
3341 MAYBE_FINISH_COMPOSITION ();
3342 for (; src_base < src; src_base++, char_offset++)
3343 {
3344 if (ASCII_BYTE_P (*src_base))
3345 *charbuf++ = *src_base;
3346 else
3347 *charbuf++ = BYTE8_TO_CHAR (*src_base);
3348 }
3349 }
3350 else if (composition_state == COMPOSING_NO)
3351 {
3352 *charbuf++ = c;
3353 char_offset++;
3354 }
3355 else
3356 {
3357 components[component_idx++] = c;
3358 if (method == COMPOSITION_WITH_RULE
3359 || (method == COMPOSITION_WITH_RULE_ALTCHARS
3360 && composition_state == COMPOSING_COMPONENT_CHAR))
3361 composition_state++;
2221 } 3362 }
2222 c = DECODE_ISO_CHARACTER (charset, c1, c2);
2223 EMIT_CHAR (c);
2224 continue; 3363 continue;
2225 3364
2226 label_invalid_code: 3365 invalid_code:
2227 coding->errors++; 3366 MAYBE_FINISH_COMPOSITION ();
2228 if (COMPOSING_P (coding))
2229 DECODE_COMPOSITION_END ('1');
2230 src = src_base; 3367 src = src_base;
2231 c = *src++; 3368 consumed_chars = consumed_chars_base;
2232 if (! NILP (translation_table)) 3369 ONE_MORE_BYTE (c);
2233 c = translate_char (translation_table, c, 0, 0, 0); 3370 *charbuf++ = c < 0 ? -c : ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
2234 EMIT_CHAR (c); 3371 char_offset++;
3372 coding->errors++;
3373 continue;
3374
3375 break_loop:
3376 break;
2235 } 3377 }
2236 3378
2237 label_end_of_loop: 3379 no_more_source:
2238 coding->consumed = coding->consumed_char = src_base - source; 3380 if (last_id != charset_ascii)
2239 coding->produced = dst - destination; 3381 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
2240 return; 3382 coding->consumed_char += consumed_chars_base;
3383 coding->consumed = src_base - coding->source;
3384 coding->charbuf_used = charbuf - coding->charbuf;
2241} 3385}
2242 3386
2243 3387
@@ -2245,9 +3389,9 @@ decode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
2245 3389
2246/* 3390/*
2247 It is not enough to say just "ISO2022" on encoding, we have to 3391 It is not enough to say just "ISO2022" on encoding, we have to
2248 specify more details. In Emacs, each ISO2022 coding system 3392 specify more details. In Emacs, each coding system of ISO2022
2249 variant has the following specifications: 3393 variant has the following specifications:
2250 1. Initial designation to G0 through G3. 3394 1. Initial designation to G0 thru G3.
2251 2. Allows short-form designation? 3395 2. Allows short-form designation?
2252 3. ASCII should be designated to G0 before control characters? 3396 3. ASCII should be designated to G0 before control characters?
2253 4. ASCII should be designated to G0 at end of line? 3397 4. ASCII should be designated to G0 at end of line?
@@ -2257,8 +3401,8 @@ decode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
2257 And the following two are only for Japanese: 3401 And the following two are only for Japanese:
2258 8. Use ASCII in place of JIS0201-1976-Roman? 3402 8. Use ASCII in place of JIS0201-1976-Roman?
2259 9. Use JISX0208-1983 in place of JISX0208-1978? 3403 9. Use JISX0208-1983 in place of JISX0208-1978?
2260 These specifications are encoded in `coding->flags' as flag bits 3404 These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits
2261 defined by macros CODING_FLAG_ISO_XXX. See `coding.h' for more 3405 defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more
2262 details. 3406 details.
2263*/ 3407*/
2264 3408
@@ -2269,115 +3413,136 @@ decode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
2269 3413
2270#define ENCODE_DESIGNATION(charset, reg, coding) \ 3414#define ENCODE_DESIGNATION(charset, reg, coding) \
2271 do { \ 3415 do { \
2272 unsigned char final_char = CHARSET_ISO_FINAL_CHAR (charset); \ 3416 unsigned char final_char = CHARSET_ISO_FINAL (charset); \
2273 char *intermediate_char_94 = "()*+"; \ 3417 char *intermediate_char_94 = "()*+"; \
2274 char *intermediate_char_96 = ",-./"; \ 3418 char *intermediate_char_96 = ",-./"; \
2275 int revision = CODING_SPEC_ISO_REVISION_NUMBER(coding, charset); \ 3419 int revision = -1; \
2276 \ 3420 int c; \
2277 if (revision < 255) \ 3421 \
3422 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \
3423 revision = CHARSET_ISO_REVISION (charset); \
3424 \
3425 if (revision >= 0) \
2278 { \ 3426 { \
2279 *dst++ = ISO_CODE_ESC; \ 3427 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \
2280 *dst++ = '&'; \ 3428 EMIT_ONE_BYTE ('@' + revision); \
2281 *dst++ = '@' + revision; \
2282 } \ 3429 } \
2283 *dst++ = ISO_CODE_ESC; \ 3430 EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \
2284 if (CHARSET_DIMENSION (charset) == 1) \ 3431 if (CHARSET_DIMENSION (charset) == 1) \
2285 { \ 3432 { \
2286 if (CHARSET_CHARS (charset) == 94) \ 3433 if (! CHARSET_ISO_CHARS_96 (charset)) \
2287 *dst++ = (unsigned char) (intermediate_char_94[reg]); \ 3434 c = intermediate_char_94[reg]; \
2288 else \ 3435 else \
2289 *dst++ = (unsigned char) (intermediate_char_96[reg]); \ 3436 c = intermediate_char_96[reg]; \
3437 EMIT_ONE_ASCII_BYTE (c); \
2290 } \ 3438 } \
2291 else \ 3439 else \
2292 { \ 3440 { \
2293 *dst++ = '$'; \ 3441 EMIT_ONE_ASCII_BYTE ('$'); \
2294 if (CHARSET_CHARS (charset) == 94) \ 3442 if (! CHARSET_ISO_CHARS_96 (charset)) \
2295 { \ 3443 { \
2296 if (! (coding->flags & CODING_FLAG_ISO_SHORT_FORM) \ 3444 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \
2297 || reg != 0 \ 3445 || reg != 0 \
2298 || final_char < '@' || final_char > 'B') \ 3446 || final_char < '@' || final_char > 'B') \
2299 *dst++ = (unsigned char) (intermediate_char_94[reg]); \ 3447 EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \
2300 } \ 3448 } \
2301 else \ 3449 else \
2302 *dst++ = (unsigned char) (intermediate_char_96[reg]); \ 3450 EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \
2303 } \ 3451 } \
2304 *dst++ = final_char; \ 3452 EMIT_ONE_ASCII_BYTE (final_char); \
2305 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \ 3453 \
3454 CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \
2306 } while (0) 3455 } while (0)
2307 3456
3457
2308/* The following two macros produce codes (control character or escape 3458/* The following two macros produce codes (control character or escape
2309 sequence) for ISO2022 single-shift functions (single-shift-2 and 3459 sequence) for ISO2022 single-shift functions (single-shift-2 and
2310 single-shift-3). */ 3460 single-shift-3). */
2311 3461
2312#define ENCODE_SINGLE_SHIFT_2 \ 3462#define ENCODE_SINGLE_SHIFT_2 \
2313 do { \ 3463 do { \
2314 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \ 3464 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
2315 *dst++ = ISO_CODE_ESC, *dst++ = 'N'; \ 3465 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \
2316 else \ 3466 else \
2317 *dst++ = ISO_CODE_SS2; \ 3467 EMIT_ONE_BYTE (ISO_CODE_SS2); \
2318 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \ 3468 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
2319 } while (0) 3469 } while (0)
2320 3470
2321#define ENCODE_SINGLE_SHIFT_3 \ 3471
2322 do { \ 3472#define ENCODE_SINGLE_SHIFT_3 \
2323 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \ 3473 do { \
2324 *dst++ = ISO_CODE_ESC, *dst++ = 'O'; \ 3474 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
2325 else \ 3475 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \
2326 *dst++ = ISO_CODE_SS3; \ 3476 else \
2327 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \ 3477 EMIT_ONE_BYTE (ISO_CODE_SS3); \
3478 CODING_ISO_SINGLE_SHIFTING (coding) = 1; \
2328 } while (0) 3479 } while (0)
2329 3480
3481
2330/* The following four macros produce codes (control character or 3482/* The following four macros produce codes (control character or
2331 escape sequence) for ISO2022 locking-shift functions (shift-in, 3483 escape sequence) for ISO2022 locking-shift functions (shift-in,
2332 shift-out, locking-shift-2, and locking-shift-3). */ 3484 shift-out, locking-shift-2, and locking-shift-3). */
2333 3485
2334#define ENCODE_SHIFT_IN \ 3486#define ENCODE_SHIFT_IN \
2335 do { \ 3487 do { \
2336 *dst++ = ISO_CODE_SI; \ 3488 EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \
2337 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0; \ 3489 CODING_ISO_INVOCATION (coding, 0) = 0; \
2338 } while (0) 3490 } while (0)
2339 3491
2340#define ENCODE_SHIFT_OUT \ 3492
2341 do { \ 3493#define ENCODE_SHIFT_OUT \
2342 *dst++ = ISO_CODE_SO; \ 3494 do { \
2343 CODING_SPEC_ISO_INVOCATION (coding, 0) = 1; \ 3495 EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \
3496 CODING_ISO_INVOCATION (coding, 0) = 1; \
2344 } while (0) 3497 } while (0)
2345 3498
2346#define ENCODE_LOCKING_SHIFT_2 \ 3499
2347 do { \ 3500#define ENCODE_LOCKING_SHIFT_2 \
2348 *dst++ = ISO_CODE_ESC, *dst++ = 'n'; \ 3501 do { \
2349 CODING_SPEC_ISO_INVOCATION (coding, 0) = 2; \ 3502 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
3503 CODING_ISO_INVOCATION (coding, 0) = 2; \
2350 } while (0) 3504 } while (0)
2351 3505
2352#define ENCODE_LOCKING_SHIFT_3 \ 3506
2353 do { \ 3507#define ENCODE_LOCKING_SHIFT_3 \
2354 *dst++ = ISO_CODE_ESC, *dst++ = 'o'; \ 3508 do { \
2355 CODING_SPEC_ISO_INVOCATION (coding, 0) = 3; \ 3509 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \
3510 CODING_ISO_INVOCATION (coding, 0) = 3; \
2356 } while (0) 3511 } while (0)
2357 3512
3513
2358/* Produce codes for a DIMENSION1 character whose character set is 3514/* Produce codes for a DIMENSION1 character whose character set is
2359 CHARSET and whose position-code is C1. Designation and invocation 3515 CHARSET and whose position-code is C1. Designation and invocation
2360 sequences are also produced in advance if necessary. */ 3516 sequences are also produced in advance if necessary. */
2361 3517
2362#define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \ 3518#define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
2363 do { \ 3519 do { \
2364 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \ 3520 int id = CHARSET_ID (charset); \
3521 \
3522 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \
3523 && id == charset_ascii) \
3524 { \
3525 id = charset_jisx0201_roman; \
3526 charset = CHARSET_FROM_ID (id); \
3527 } \
3528 \
3529 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
2365 { \ 3530 { \
2366 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \ 3531 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
2367 *dst++ = c1 & 0x7F; \ 3532 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
2368 else \ 3533 else \
2369 *dst++ = c1 | 0x80; \ 3534 EMIT_ONE_BYTE (c1 | 0x80); \
2370 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \ 3535 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
2371 break; \ 3536 break; \
2372 } \ 3537 } \
2373 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \ 3538 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
2374 { \ 3539 { \
2375 *dst++ = c1 & 0x7F; \ 3540 EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \
2376 break; \ 3541 break; \
2377 } \ 3542 } \
2378 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \ 3543 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
2379 { \ 3544 { \
2380 *dst++ = c1 | 0x80; \ 3545 EMIT_ONE_BYTE (c1 | 0x80); \
2381 break; \ 3546 break; \
2382 } \ 3547 } \
2383 else \ 3548 else \
@@ -2385,32 +3550,43 @@ decode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
2385 must invoke it, or, at first, designate it to some graphic \ 3550 must invoke it, or, at first, designate it to some graphic \
2386 register. Then repeat the loop to actually produce the \ 3551 register. Then repeat the loop to actually produce the \
2387 character. */ \ 3552 character. */ \
2388 dst = encode_invocation_designation (charset, coding, dst); \ 3553 dst = encode_invocation_designation (charset, coding, dst, \
3554 &produced_chars); \
2389 } while (1) 3555 } while (1)
2390 3556
3557
2391/* Produce codes for a DIMENSION2 character whose character set is 3558/* Produce codes for a DIMENSION2 character whose character set is
2392 CHARSET and whose position-codes are C1 and C2. Designation and 3559 CHARSET and whose position-codes are C1 and C2. Designation and
2393 invocation codes are also produced in advance if necessary. */ 3560 invocation codes are also produced in advance if necessary. */
2394 3561
2395#define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \ 3562#define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
2396 do { \ 3563 do { \
2397 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \ 3564 int id = CHARSET_ID (charset); \
3565 \
3566 if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \
3567 && id == charset_jisx0208) \
3568 { \
3569 id = charset_jisx0208_1978; \
3570 charset = CHARSET_FROM_ID (id); \
3571 } \
3572 \
3573 if (CODING_ISO_SINGLE_SHIFTING (coding)) \
2398 { \ 3574 { \
2399 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \ 3575 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \
2400 *dst++ = c1 & 0x7F, *dst++ = c2 & 0x7F; \ 3576 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
2401 else \ 3577 else \
2402 *dst++ = c1 | 0x80, *dst++ = c2 | 0x80; \ 3578 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
2403 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \ 3579 CODING_ISO_SINGLE_SHIFTING (coding) = 0; \
2404 break; \ 3580 break; \
2405 } \ 3581 } \
2406 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \ 3582 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \
2407 { \ 3583 { \
2408 *dst++ = c1 & 0x7F, *dst++= c2 & 0x7F; \ 3584 EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \
2409 break; \ 3585 break; \
2410 } \ 3586 } \
2411 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \ 3587 else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \
2412 { \ 3588 { \
2413 *dst++ = c1 | 0x80, *dst++= c2 | 0x80; \ 3589 EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \
2414 break; \ 3590 break; \
2415 } \ 3591 } \
2416 else \ 3592 else \
@@ -2418,73 +3594,49 @@ decode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
2418 must invoke it, or, at first, designate it to some graphic \ 3594 must invoke it, or, at first, designate it to some graphic \
2419 register. Then repeat the loop to actually produce the \ 3595 register. Then repeat the loop to actually produce the \
2420 character. */ \ 3596 character. */ \
2421 dst = encode_invocation_designation (charset, coding, dst); \ 3597 dst = encode_invocation_designation (charset, coding, dst, \
3598 &produced_chars); \
2422 } while (1) 3599 } while (1)
2423 3600
2424#define ENCODE_ISO_CHARACTER(c) \
2425 do { \
2426 int charset, c1, c2; \
2427 \
2428 SPLIT_CHAR (c, charset, c1, c2); \
2429 if (CHARSET_DEFINED_P (charset)) \
2430 { \
2431 if (CHARSET_DIMENSION (charset) == 1) \
2432 { \
2433 if (charset == CHARSET_ASCII \
2434 && coding->flags & CODING_FLAG_ISO_USE_ROMAN) \
2435 charset = charset_latin_jisx0201; \
2436 ENCODE_ISO_CHARACTER_DIMENSION1 (charset, c1); \
2437 } \
2438 else \
2439 { \
2440 if (charset == charset_jisx0208 \
2441 && coding->flags & CODING_FLAG_ISO_USE_OLDJIS) \
2442 charset = charset_jisx0208_1978; \
2443 ENCODE_ISO_CHARACTER_DIMENSION2 (charset, c1, c2); \
2444 } \
2445 } \
2446 else \
2447 { \
2448 *dst++ = c1; \
2449 if (c2 >= 0) \
2450 *dst++ = c2; \
2451 } \
2452 } while (0)
2453
2454
2455/* Instead of encoding character C, produce one or two `?'s. */
2456 3601
2457#define ENCODE_UNSAFE_CHARACTER(c) \ 3602#define ENCODE_ISO_CHARACTER(charset, c) \
2458 do { \ 3603 do { \
2459 ENCODE_ISO_CHARACTER (CODING_REPLACEMENT_CHARACTER); \ 3604 int code = ENCODE_CHAR ((charset),(c)); \
2460 if (CHARSET_WIDTH (CHAR_CHARSET (c)) > 1) \ 3605 \
2461 ENCODE_ISO_CHARACTER (CODING_REPLACEMENT_CHARACTER); \ 3606 if (CHARSET_DIMENSION (charset) == 1) \
3607 ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \
3608 else \
3609 ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \
2462 } while (0) 3610 } while (0)
2463 3611
2464 3612
2465/* Produce designation and invocation codes at a place pointed by DST 3613/* Produce designation and invocation codes at a place pointed by DST
2466 to use CHARSET. The element `spec.iso2022' of *CODING is updated. 3614 to use CHARSET. The element `spec.iso_2022' of *CODING is updated.
2467 Return new DST. */ 3615 Return new DST. */
2468 3616
2469unsigned char * 3617unsigned char *
2470encode_invocation_designation (charset, coding, dst) 3618encode_invocation_designation (charset, coding, dst, p_nchars)
2471 int charset; 3619 struct charset *charset;
2472 struct coding_system *coding; 3620 struct coding_system *coding;
2473 unsigned char *dst; 3621 unsigned char *dst;
3622 int *p_nchars;
2474{ 3623{
3624 int multibytep = coding->dst_multibyte;
3625 int produced_chars = *p_nchars;
2475 int reg; /* graphic register number */ 3626 int reg; /* graphic register number */
3627 int id = CHARSET_ID (charset);
2476 3628
2477 /* At first, check designations. */ 3629 /* At first, check designations. */
2478 for (reg = 0; reg < 4; reg++) 3630 for (reg = 0; reg < 4; reg++)
2479 if (charset == CODING_SPEC_ISO_DESIGNATION (coding, reg)) 3631 if (id == CODING_ISO_DESIGNATION (coding, reg))
2480 break; 3632 break;
2481 3633
2482 if (reg >= 4) 3634 if (reg >= 4)
2483 { 3635 {
2484 /* CHARSET is not yet designated to any graphic registers. */ 3636 /* CHARSET is not yet designated to any graphic registers. */
2485 /* At first check the requested designation. */ 3637 /* At first check the requested designation. */
2486 reg = CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset); 3638 reg = CODING_ISO_REQUEST (coding, id);
2487 if (reg == CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION) 3639 if (reg < 0)
2488 /* Since CHARSET requests no special designation, designate it 3640 /* Since CHARSET requests no special designation, designate it
2489 to graphic register 0. */ 3641 to graphic register 0. */
2490 reg = 0; 3642 reg = 0;
@@ -2492,8 +3644,8 @@ encode_invocation_designation (charset, coding, dst)
2492 ENCODE_DESIGNATION (charset, reg, coding); 3644 ENCODE_DESIGNATION (charset, reg, coding);
2493 } 3645 }
2494 3646
2495 if (CODING_SPEC_ISO_INVOCATION (coding, 0) != reg 3647 if (CODING_ISO_INVOCATION (coding, 0) != reg
2496 && CODING_SPEC_ISO_INVOCATION (coding, 1) != reg) 3648 && CODING_ISO_INVOCATION (coding, 1) != reg)
2497 { 3649 {
2498 /* Since the graphic register REG is not invoked to any graphic 3650 /* Since the graphic register REG is not invoked to any graphic
2499 planes, invoke it to graphic plane 0. */ 3651 planes, invoke it to graphic plane 0. */
@@ -2508,14 +3660,14 @@ encode_invocation_designation (charset, coding, dst)
2508 break; 3660 break;
2509 3661
2510 case 2: /* graphic register 2 */ 3662 case 2: /* graphic register 2 */
2511 if (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT) 3663 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)
2512 ENCODE_SINGLE_SHIFT_2; 3664 ENCODE_SINGLE_SHIFT_2;
2513 else 3665 else
2514 ENCODE_LOCKING_SHIFT_2; 3666 ENCODE_LOCKING_SHIFT_2;
2515 break; 3667 break;
2516 3668
2517 case 3: /* graphic register 3 */ 3669 case 3: /* graphic register 3 */
2518 if (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT) 3670 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)
2519 ENCODE_SINGLE_SHIFT_3; 3671 ENCODE_SINGLE_SHIFT_3;
2520 else 3672 else
2521 ENCODE_LOCKING_SHIFT_3; 3673 ENCODE_LOCKING_SHIFT_3;
@@ -2523,98 +3675,55 @@ encode_invocation_designation (charset, coding, dst)
2523 } 3675 }
2524 } 3676 }
2525 3677
3678 *p_nchars = produced_chars;
2526 return dst; 3679 return dst;
2527} 3680}
2528 3681
2529/* Produce 2-byte codes for encoded composition rule RULE. */ 3682/* The following three macros produce codes for indicating direction
2530 3683 of text. */
2531#define ENCODE_COMPOSITION_RULE(rule) \ 3684#define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
2532 do { \
2533 int gref, nref; \
2534 COMPOSITION_DECODE_RULE (rule, gref, nref); \
2535 *dst++ = 32 + 81 + gref; \
2536 *dst++ = 32 + nref; \
2537 } while (0)
2538
2539/* Produce codes for indicating the start of a composition sequence
2540 (ESC 0, ESC 3, or ESC 4). DATA points to an array of integers
2541 which specify information about the composition. See the comment
2542 in coding.h for the format of DATA. */
2543
2544#define ENCODE_COMPOSITION_START(coding, data) \
2545 do { \ 3685 do { \
2546 coding->composing = data[3]; \ 3686 if (CODING_ISO_FLAGS (coding) == CODING_ISO_FLAG_SEVEN_BITS) \
2547 *dst++ = ISO_CODE_ESC; \ 3687 EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '['); \
2548 if (coding->composing == COMPOSITION_RELATIVE) \
2549 *dst++ = '0'; \
2550 else \ 3688 else \
2551 { \ 3689 EMIT_ONE_BYTE (ISO_CODE_CSI); \
2552 *dst++ = (coding->composing == COMPOSITION_WITH_ALTCHARS \
2553 ? '3' : '4'); \
2554 coding->cmp_data_index = coding->cmp_data_start + 4; \
2555 coding->composition_rule_follows = 0; \
2556 } \
2557 } while (0) 3690 } while (0)
2558 3691
2559/* Produce codes for indicating the end of the current composition. */
2560 3692
2561#define ENCODE_COMPOSITION_END(coding, data) \ 3693#define ENCODE_DIRECTION_R2L() \
2562 do { \ 3694 do { \
2563 *dst++ = ISO_CODE_ESC; \ 3695 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
2564 *dst++ = '1'; \ 3696 EMIT_TWO_ASCII_BYTES ('2', ']'); \
2565 coding->cmp_data_start += data[0]; \
2566 coding->composing = COMPOSITION_NO; \
2567 if (coding->cmp_data_start == coding->cmp_data->used \
2568 && coding->cmp_data->next) \
2569 { \
2570 coding->cmp_data = coding->cmp_data->next; \
2571 coding->cmp_data_start = 0; \
2572 } \
2573 } while (0) 3697 } while (0)
2574 3698
2575/* Produce composition start sequence ESC 0. Here, this sequence
2576 doesn't mean the start of a new composition but means that we have
2577 just produced components (alternate chars and composition rules) of
2578 the composition and the actual text follows in SRC. */
2579 3699
2580#define ENCODE_COMPOSITION_FAKE_START(coding) \ 3700#define ENCODE_DIRECTION_L2R() \
2581 do { \ 3701 do { \
2582 *dst++ = ISO_CODE_ESC; \ 3702 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst); \
2583 *dst++ = '0'; \ 3703 EMIT_TWO_ASCII_BYTES ('0', ']'); \
2584 coding->composing = COMPOSITION_RELATIVE; \
2585 } while (0) 3704 } while (0)
2586 3705
2587/* The following three macros produce codes for indicating direction
2588 of text. */
2589#define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
2590 do { \
2591 if (coding->flags == CODING_FLAG_ISO_SEVEN_BITS) \
2592 *dst++ = ISO_CODE_ESC, *dst++ = '['; \
2593 else \
2594 *dst++ = ISO_CODE_CSI; \
2595 } while (0)
2596
2597#define ENCODE_DIRECTION_R2L \
2598 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst), *dst++ = '2', *dst++ = ']'
2599
2600#define ENCODE_DIRECTION_L2R \
2601 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst), *dst++ = '0', *dst++ = ']'
2602 3706
2603/* Produce codes for designation and invocation to reset the graphic 3707/* Produce codes for designation and invocation to reset the graphic
2604 planes and registers to initial state. */ 3708 planes and registers to initial state. */
2605#define ENCODE_RESET_PLANE_AND_REGISTER \ 3709#define ENCODE_RESET_PLANE_AND_REGISTER() \
2606 do { \ 3710 do { \
2607 int reg; \ 3711 int reg; \
2608 if (CODING_SPEC_ISO_INVOCATION (coding, 0) != 0) \ 3712 struct charset *charset; \
2609 ENCODE_SHIFT_IN; \ 3713 \
2610 for (reg = 0; reg < 4; reg++) \ 3714 if (CODING_ISO_INVOCATION (coding, 0) != 0) \
2611 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg) >= 0 \ 3715 ENCODE_SHIFT_IN; \
2612 && (CODING_SPEC_ISO_DESIGNATION (coding, reg) \ 3716 for (reg = 0; reg < 4; reg++) \
2613 != CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg))) \ 3717 if (CODING_ISO_INITIAL (coding, reg) >= 0 \
2614 ENCODE_DESIGNATION \ 3718 && (CODING_ISO_DESIGNATION (coding, reg) \
2615 (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg), reg, coding); \ 3719 != CODING_ISO_INITIAL (coding, reg))) \
3720 { \
3721 charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \
3722 ENCODE_DESIGNATION (charset, reg, coding); \
3723 } \
2616 } while (0) 3724 } while (0)
2617 3725
3726
2618/* Produce designation sequences of charsets in the line started from 3727/* Produce designation sequences of charsets in the line started from
2619 SRC to a place pointed by DST, and return updated DST. 3728 SRC to a place pointed by DST, and return updated DST.
2620 3729
@@ -2622,41 +3731,51 @@ encode_invocation_designation (charset, coding, dst)
2622 find all the necessary designations. */ 3731 find all the necessary designations. */
2623 3732
2624static unsigned char * 3733static unsigned char *
2625encode_designation_at_bol (coding, translation_table, src, src_end, dst) 3734encode_designation_at_bol (coding, charbuf, charbuf_end, dst)
2626 struct coding_system *coding; 3735 struct coding_system *coding;
2627 Lisp_Object translation_table; 3736 int *charbuf, *charbuf_end;
2628 const unsigned char *src, *src_end;
2629 unsigned char *dst; 3737 unsigned char *dst;
2630{ 3738{
2631 int charset, c, found = 0, reg; 3739 struct charset *charset;
2632 /* Table of charsets to be designated to each graphic register. */ 3740 /* Table of charsets to be designated to each graphic register. */
2633 int r[4]; 3741 int r[4];
3742 int c, found = 0, reg;
3743 int produced_chars = 0;
3744 int multibytep = coding->dst_multibyte;
3745 Lisp_Object attrs;
3746 Lisp_Object charset_list;
3747
3748 attrs = CODING_ID_ATTRS (coding->id);
3749 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
3750 if (EQ (charset_list, Qiso_2022))
3751 charset_list = Viso_2022_charset_list;
2634 3752
2635 for (reg = 0; reg < 4; reg++) 3753 for (reg = 0; reg < 4; reg++)
2636 r[reg] = -1; 3754 r[reg] = -1;
2637 3755
2638 while (found < 4) 3756 while (found < 4)
2639 { 3757 {
2640 ONE_MORE_CHAR (c); 3758 int id;
3759
3760 c = *charbuf++;
2641 if (c == '\n') 3761 if (c == '\n')
2642 break; 3762 break;
2643 3763 charset = char_charset (c, charset_list, NULL);
2644 charset = CHAR_CHARSET (c); 3764 id = CHARSET_ID (charset);
2645 reg = CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset); 3765 reg = CODING_ISO_REQUEST (coding, id);
2646 if (reg != CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION && r[reg] < 0) 3766 if (reg >= 0 && r[reg] < 0)
2647 { 3767 {
2648 found++; 3768 found++;
2649 r[reg] = charset; 3769 r[reg] = id;
2650 } 3770 }
2651 } 3771 }
2652 3772
2653 label_end_of_loop:
2654 if (found) 3773 if (found)
2655 { 3774 {
2656 for (reg = 0; reg < 4; reg++) 3775 for (reg = 0; reg < 4; reg++)
2657 if (r[reg] >= 0 3776 if (r[reg] >= 0
2658 && CODING_SPEC_ISO_DESIGNATION (coding, reg) != r[reg]) 3777 && CODING_ISO_DESIGNATION (coding, reg) != r[reg])
2659 ENCODE_DESIGNATION (r[reg], reg, coding); 3778 ENCODE_DESIGNATION (CHARSET_FROM_ID (r[reg]), reg, coding);
2660 } 3779 }
2661 3780
2662 return dst; 3781 return dst;
@@ -2664,188 +3783,160 @@ encode_designation_at_bol (coding, translation_table, src, src_end, dst)
2664 3783
2665/* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */ 3784/* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
2666 3785
2667static void 3786static int
2668encode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes) 3787encode_coding_iso_2022 (coding)
2669 struct coding_system *coding; 3788 struct coding_system *coding;
2670 const unsigned char *source;
2671 unsigned char *destination;
2672 int src_bytes, dst_bytes;
2673{ 3789{
2674 const unsigned char *src = source; 3790 int multibytep = coding->dst_multibyte;
2675 const unsigned char *src_end = source + src_bytes; 3791 int *charbuf = coding->charbuf;
2676 unsigned char *dst = destination; 3792 int *charbuf_end = charbuf + coding->charbuf_used;
2677 unsigned char *dst_end = destination + dst_bytes; 3793 unsigned char *dst = coding->destination + coding->produced;
2678 /* Since the maximum bytes produced by each loop is 20, we subtract 19 3794 unsigned char *dst_end = coding->destination + coding->dst_bytes;
2679 from DST_END to assure overflow checking is necessary only at the 3795 int safe_room = 16;
2680 head of loop. */ 3796 int bol_designation
2681 unsigned char *adjusted_dst_end = dst_end - 19; 3797 = (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATE_AT_BOL
2682 /* SRC_BASE remembers the start position in source in each loop. 3798 && CODING_ISO_BOL (coding));
2683 The loop will be exited when there's not enough source text to 3799 int produced_chars = 0;
2684 analyze multi-byte codes (within macro ONE_MORE_CHAR), or when 3800 Lisp_Object attrs, eol_type, charset_list;
2685 there's not enough destination area to produce encoded codes 3801 int ascii_compatible;
2686 (within macro EMIT_BYTES). */
2687 const unsigned char *src_base;
2688 int c; 3802 int c;
2689 Lisp_Object translation_table; 3803 int preferred_charset_id = -1;
2690 Lisp_Object safe_chars;
2691 3804
2692 if (coding->flags & CODING_FLAG_ISO_SAFE) 3805 CODING_GET_INFO (coding, attrs, charset_list);
2693 coding->mode |= CODING_MODE_INHIBIT_UNENCODABLE_CHAR; 3806 eol_type = CODING_ID_EOL_TYPE (coding->id);
3807 if (VECTORP (eol_type))
3808 eol_type = Qunix;
2694 3809
2695 safe_chars = coding_safe_chars (coding->symbol); 3810 setup_iso_safe_charsets (attrs);
3811 /* Charset list may have been changed. */
3812 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
3813 coding->safe_charsets = (char *) SDATA (CODING_ATTR_SAFE_CHARSETS(attrs));
2696 3814
2697 if (NILP (Venable_character_translation)) 3815 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
2698 translation_table = Qnil;
2699 else
2700 {
2701 translation_table = coding->translation_table_for_encode;
2702 if (NILP (translation_table))
2703 translation_table = Vstandard_translation_table_for_encode;
2704 }
2705 3816
2706 coding->consumed_char = 0; 3817 while (charbuf < charbuf_end)
2707 coding->errors = 0;
2708 while (1)
2709 { 3818 {
2710 src_base = src; 3819 ASSURE_DESTINATION (safe_room);
2711 3820
2712 if (dst >= (dst_bytes ? adjusted_dst_end : (src - 19))) 3821 if (bol_designation)
2713 { 3822 {
2714 coding->result = CODING_FINISH_INSUFFICIENT_DST; 3823 unsigned char *dst_prev = dst;
2715 break;
2716 }
2717 3824
2718 if (coding->flags & CODING_FLAG_ISO_DESIGNATE_AT_BOL
2719 && CODING_SPEC_ISO_BOL (coding))
2720 {
2721 /* We have to produce designation sequences if any now. */ 3825 /* We have to produce designation sequences if any now. */
2722 dst = encode_designation_at_bol (coding, translation_table, 3826 dst = encode_designation_at_bol (coding, charbuf, charbuf_end, dst);
2723 src, src_end, dst); 3827 bol_designation = 0;
2724 CODING_SPEC_ISO_BOL (coding) = 0; 3828 /* We are sure that designation sequences are all ASCII bytes. */
3829 produced_chars += dst - dst_prev;
2725 } 3830 }
2726 3831
2727 /* Check composition start and end. */ 3832 c = *charbuf++;
2728 if (coding->composing != COMPOSITION_DISABLED
2729 && coding->cmp_data_start < coding->cmp_data->used)
2730 {
2731 struct composition_data *cmp_data = coding->cmp_data;
2732 int *data = cmp_data->data + coding->cmp_data_start;
2733 int this_pos = cmp_data->char_offset + coding->consumed_char;
2734 3833
2735 if (coding->composing == COMPOSITION_RELATIVE) 3834 if (c < 0)
2736 { 3835 {
2737 if (this_pos == data[2]) 3836 /* Handle an annotation. */
2738 { 3837 switch (*charbuf)
2739 ENCODE_COMPOSITION_END (coding, data);
2740 cmp_data = coding->cmp_data;
2741 data = cmp_data->data + coding->cmp_data_start;
2742 }
2743 }
2744 else if (COMPOSING_P (coding))
2745 {
2746 /* COMPOSITION_WITH_ALTCHARS or COMPOSITION_WITH_RULE_ALTCHAR */
2747 if (coding->cmp_data_index == coding->cmp_data_start + data[0])
2748 /* We have consumed components of the composition.
2749 What follows in SRC is the composition's base
2750 text. */
2751 ENCODE_COMPOSITION_FAKE_START (coding);
2752 else
2753 {
2754 int c = cmp_data->data[coding->cmp_data_index++];
2755 if (coding->composition_rule_follows)
2756 {
2757 ENCODE_COMPOSITION_RULE (c);
2758 coding->composition_rule_follows = 0;
2759 }
2760 else
2761 {
2762 if (coding->mode & CODING_MODE_INHIBIT_UNENCODABLE_CHAR
2763 && ! CODING_SAFE_CHAR_P (safe_chars, c))
2764 ENCODE_UNSAFE_CHARACTER (c);
2765 else
2766 ENCODE_ISO_CHARACTER (c);
2767 if (coding->composing == COMPOSITION_WITH_RULE_ALTCHARS)
2768 coding->composition_rule_follows = 1;
2769 }
2770 continue;
2771 }
2772 }
2773 if (!COMPOSING_P (coding))
2774 { 3838 {
2775 if (this_pos == data[1]) 3839 case CODING_ANNOTATE_COMPOSITION_MASK:
2776 { 3840 /* Not yet implemented. */
2777 ENCODE_COMPOSITION_START (coding, data); 3841 break;
2778 continue; 3842 case CODING_ANNOTATE_CHARSET_MASK:
2779 } 3843 preferred_charset_id = charbuf[2];
3844 if (preferred_charset_id >= 0
3845 && NILP (Fmemq (make_number (preferred_charset_id),
3846 charset_list)))
3847 preferred_charset_id = -1;
3848 break;
3849 default:
3850 abort ();
2780 } 3851 }
3852 charbuf += -c - 1;
3853 continue;
2781 } 3854 }
2782 3855
2783 ONE_MORE_CHAR (c);
2784
2785 /* Now encode the character C. */ 3856 /* Now encode the character C. */
2786 if (c < 0x20 || c == 0x7F) 3857 if (c < 0x20 || c == 0x7F)
2787 { 3858 {
2788 if (c == '\r') 3859 if (c == '\n'
3860 || (c == '\r' && EQ (eol_type, Qmac)))
2789 { 3861 {
2790 if (! (coding->mode & CODING_MODE_SELECTIVE_DISPLAY)) 3862 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL)
3863 ENCODE_RESET_PLANE_AND_REGISTER ();
3864 if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_INIT_AT_BOL)
2791 { 3865 {
2792 if (coding->flags & CODING_FLAG_ISO_RESET_AT_CNTL) 3866 int i;
2793 ENCODE_RESET_PLANE_AND_REGISTER; 3867
2794 *dst++ = c; 3868 for (i = 0; i < 4; i++)
2795 continue; 3869 CODING_ISO_DESIGNATION (coding, i)
3870 = CODING_ISO_INITIAL (coding, i);
2796 } 3871 }
2797 /* fall down to treat '\r' as '\n' ... */ 3872 bol_designation
2798 c = '\n'; 3873 = CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATE_AT_BOL;
2799 }
2800 if (c == '\n')
2801 {
2802 if (coding->flags & CODING_FLAG_ISO_RESET_AT_EOL)
2803 ENCODE_RESET_PLANE_AND_REGISTER;
2804 if (coding->flags & CODING_FLAG_ISO_INIT_AT_BOL)
2805 bcopy (coding->spec.iso2022.initial_designation,
2806 coding->spec.iso2022.current_designation,
2807 sizeof coding->spec.iso2022.initial_designation);
2808 if (coding->eol_type == CODING_EOL_LF
2809 || coding->eol_type == CODING_EOL_UNDECIDED)
2810 *dst++ = ISO_CODE_LF;
2811 else if (coding->eol_type == CODING_EOL_CRLF)
2812 *dst++ = ISO_CODE_CR, *dst++ = ISO_CODE_LF;
2813 else
2814 *dst++ = ISO_CODE_CR;
2815 CODING_SPEC_ISO_BOL (coding) = 1;
2816 } 3874 }
3875 else if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_CNTL)
3876 ENCODE_RESET_PLANE_AND_REGISTER ();
3877 EMIT_ONE_ASCII_BYTE (c);
3878 }
3879 else if (ASCII_CHAR_P (c))
3880 {
3881 if (ascii_compatible)
3882 EMIT_ONE_ASCII_BYTE (c);
2817 else 3883 else
2818 { 3884 {
2819 if (coding->flags & CODING_FLAG_ISO_RESET_AT_CNTL) 3885 struct charset *charset = CHARSET_FROM_ID (charset_ascii);
2820 ENCODE_RESET_PLANE_AND_REGISTER; 3886 ENCODE_ISO_CHARACTER (charset, c);
2821 *dst++ = c;
2822 } 3887 }
2823 } 3888 }
2824 else if (ASCII_BYTE_P (c)) 3889 else if (CHAR_BYTE8_P (c))
2825 ENCODE_ISO_CHARACTER (c);
2826 else if (SINGLE_BYTE_CHAR_P (c))
2827 { 3890 {
2828 *dst++ = c; 3891 c = CHAR_TO_BYTE8 (c);
2829 coding->errors++; 3892 EMIT_ONE_BYTE (c);
2830 } 3893 }
2831 else if (coding->mode & CODING_MODE_INHIBIT_UNENCODABLE_CHAR
2832 && ! CODING_SAFE_CHAR_P (safe_chars, c))
2833 ENCODE_UNSAFE_CHARACTER (c);
2834 else 3894 else
2835 ENCODE_ISO_CHARACTER (c); 3895 {
3896 struct charset *charset;
2836 3897
2837 coding->consumed_char++; 3898 if (preferred_charset_id >= 0)
3899 {
3900 charset = CHARSET_FROM_ID (preferred_charset_id);
3901 if (! CHAR_CHARSET_P (c, charset))
3902 charset = char_charset (c, charset_list, NULL);
3903 }
3904 else
3905 charset = char_charset (c, charset_list, NULL);
3906 if (!charset)
3907 {
3908 if (coding->mode & CODING_MODE_SAFE_ENCODING)
3909 {
3910 c = CODING_INHIBIT_CHARACTER_SUBSTITUTION;
3911 charset = CHARSET_FROM_ID (charset_ascii);
3912 }
3913 else
3914 {
3915 c = coding->default_char;
3916 charset = char_charset (c, charset_list, NULL);
3917 }
3918 }
3919 ENCODE_ISO_CHARACTER (charset, c);
3920 }
2838 } 3921 }
2839 3922
2840 label_end_of_loop: 3923 if (coding->mode & CODING_MODE_LAST_BLOCK
2841 coding->consumed = src_base - source; 3924 && CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL)
2842 coding->produced = coding->produced_char = dst - destination; 3925 {
3926 ASSURE_DESTINATION (safe_room);
3927 ENCODE_RESET_PLANE_AND_REGISTER ();
3928 }
3929 record_conversion_result (coding, CODING_RESULT_SUCCESS);
3930 CODING_ISO_BOL (coding) = bol_designation;
3931 coding->produced_char += produced_chars;
3932 coding->produced = dst - coding->destination;
3933 return 0;
2843} 3934}
2844 3935
2845 3936
2846/*** 4. SJIS and BIG5 handlers ***/ 3937/*** 8,9. SJIS and BIG5 handlers ***/
2847 3938
2848/* Although SJIS and BIG5 are not ISO coding systems, they are used 3939/* Although SJIS and BIG5 are not ISO's coding system, they are used
2849 quite widely. So, for the moment, Emacs supports them in the bare 3940 quite widely. So, for the moment, Emacs supports them in the bare
2850 C code. But, in the future, they may be supported only by CCL. */ 3941 C code. But, in the future, they may be supported only by CCL. */
2851 3942
@@ -2854,12 +3945,12 @@ encode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
2854 as is. A character of charset katakana-jisx0201 is encoded by 3945 as is. A character of charset katakana-jisx0201 is encoded by
2855 "position-code + 0x80". A character of charset japanese-jisx0208 3946 "position-code + 0x80". A character of charset japanese-jisx0208
2856 is encoded in 2-byte but two position-codes are divided and shifted 3947 is encoded in 2-byte but two position-codes are divided and shifted
2857 so that it fits in the range below. 3948 so that it fit in the range below.
2858 3949
2859 --- CODE RANGE of SJIS --- 3950 --- CODE RANGE of SJIS ---
2860 (character set) (range) 3951 (character set) (range)
2861 ASCII 0x00 .. 0x7F 3952 ASCII 0x00 .. 0x7F
2862 KATAKANA-JISX0201 0xA1 .. 0xDF 3953 KATAKANA-JISX0201 0xA0 .. 0xDF
2863 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF 3954 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
2864 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC 3955 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
2865 ------------------------------- 3956 -------------------------------
@@ -2868,7 +3959,7 @@ encode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
2868 3959
2869/* BIG5 is a coding system encoding two character sets: ASCII and 3960/* BIG5 is a coding system encoding two character sets: ASCII and
2870 Big5. An ASCII character is encoded as is. Big5 is a two-byte 3961 Big5. An ASCII character is encoded as is. Big5 is a two-byte
2871 character set and is encoded in two bytes. 3962 character set and is encoded in two-byte.
2872 3963
2873 --- CODE RANGE of BIG5 --- 3964 --- CODE RANGE of BIG5 ---
2874 (character set) (range) 3965 (character set) (range)
@@ -2877,307 +3968,293 @@ encode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
2877 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE 3968 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
2878 -------------------------- 3969 --------------------------
2879 3970
2880 Since the number of characters in Big5 is larger than maximum 3971 */
2881 characters in Emacs' charset (96x96), it can't be handled as one
2882 charset. So, in Emacs, Big5 is divided into two: `charset-big5-1'
2883 and `charset-big5-2'. Both are DIMENSION2 and CHARS94. The former
2884 contains frequently used characters and the latter contains less
2885 frequently used characters. */
2886
2887/* Macros to decode or encode a character of Big5 in BIG5. B1 and B2
2888 are the 1st and 2nd position-codes of Big5 in BIG5 coding system.
2889 C1 and C2 are the 1st and 2nd position-codes of Emacs' internal
2890 format. CHARSET is `charset_big5_1' or `charset_big5_2'. */
2891
2892/* Number of Big5 characters which have the same code in 1st byte. */
2893#define BIG5_SAME_ROW (0xFF - 0xA1 + 0x7F - 0x40)
2894
2895#define DECODE_BIG5(b1, b2, charset, c1, c2) \
2896 do { \
2897 unsigned int temp \
2898 = (b1 - 0xA1) * BIG5_SAME_ROW + b2 - (b2 < 0x7F ? 0x40 : 0x62); \
2899 if (b1 < 0xC9) \
2900 charset = charset_big5_1; \
2901 else \
2902 { \
2903 charset = charset_big5_2; \
2904 temp -= (0xC9 - 0xA1) * BIG5_SAME_ROW; \
2905 } \
2906 c1 = temp / (0xFF - 0xA1) + 0x21; \
2907 c2 = temp % (0xFF - 0xA1) + 0x21; \
2908 } while (0)
2909
2910#define ENCODE_BIG5(charset, c1, c2, b1, b2) \
2911 do { \
2912 unsigned int temp = (c1 - 0x21) * (0xFF - 0xA1) + (c2 - 0x21); \
2913 if (charset == charset_big5_2) \
2914 temp += BIG5_SAME_ROW * (0xC9 - 0xA1); \
2915 b1 = temp / BIG5_SAME_ROW + 0xA1; \
2916 b2 = temp % BIG5_SAME_ROW; \
2917 b2 += b2 < 0x3F ? 0x40 : 0x62; \
2918 } while (0)
2919 3972
2920/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". 3973/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2921 Check if a text is encoded in SJIS. If it is, return 3974 Check if a text is encoded in SJIS. If it is, return
2922 CODING_CATEGORY_MASK_SJIS, else return 0. */ 3975 CATEGORY_MASK_SJIS, else return 0. */
2923 3976
2924static int 3977static int
2925detect_coding_sjis (src, src_end, multibytep) 3978detect_coding_sjis (coding, detect_info)
2926 unsigned char *src, *src_end; 3979 struct coding_system *coding;
2927 int multibytep; 3980 struct coding_detection_info *detect_info;
2928{ 3981{
3982 const unsigned char *src = coding->source, *src_base;
3983 const unsigned char *src_end = coding->source + coding->src_bytes;
3984 int multibytep = coding->src_multibyte;
3985 int consumed_chars = 0;
3986 int found = 0;
2929 int c; 3987 int c;
2930 /* Dummy for ONE_MORE_BYTE. */ 3988
2931 struct coding_system dummy_coding; 3989 detect_info->checked |= CATEGORY_MASK_SJIS;
2932 struct coding_system *coding = &dummy_coding; 3990 /* A coding system of this category is always ASCII compatible. */
3991 src += coding->head_ascii;
2933 3992
2934 while (1) 3993 while (1)
2935 { 3994 {
2936 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep, CODING_CATEGORY_MASK_SJIS); 3995 src_base = src;
3996 ONE_MORE_BYTE (c);
2937 if (c < 0x80) 3997 if (c < 0x80)
2938 continue; 3998 continue;
2939 if (c == 0x80 || c == 0xA0 || c > 0xEF) 3999 if ((c >= 0x81 && c <= 0x9F) || (c >= 0xE0 && c <= 0xEF))
2940 return 0;
2941 if (c <= 0x9F || c >= 0xE0)
2942 { 4000 {
2943 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep, 0); 4001 ONE_MORE_BYTE (c);
2944 if (c < 0x40 || c == 0x7F || c > 0xFC) 4002 if (c < 0x40 || c == 0x7F || c > 0xFC)
2945 return 0; 4003 break;
4004 found = CATEGORY_MASK_SJIS;
2946 } 4005 }
4006 else if (c >= 0xA0 && c < 0xE0)
4007 found = CATEGORY_MASK_SJIS;
4008 else
4009 break;
2947 } 4010 }
4011 detect_info->rejected |= CATEGORY_MASK_SJIS;
4012 return 0;
4013
4014 no_more_source:
4015 if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK)
4016 {
4017 detect_info->rejected |= CATEGORY_MASK_SJIS;
4018 return 0;
4019 }
4020 detect_info->found |= found;
4021 return 1;
2948} 4022}
2949 4023
2950/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". 4024/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2951 Check if a text is encoded in BIG5. If it is, return 4025 Check if a text is encoded in BIG5. If it is, return
2952 CODING_CATEGORY_MASK_BIG5, else return 0. */ 4026 CATEGORY_MASK_BIG5, else return 0. */
2953 4027
2954static int 4028static int
2955detect_coding_big5 (src, src_end, multibytep) 4029detect_coding_big5 (coding, detect_info)
2956 unsigned char *src, *src_end; 4030 struct coding_system *coding;
2957 int multibytep; 4031 struct coding_detection_info *detect_info;
2958{ 4032{
4033 const unsigned char *src = coding->source, *src_base;
4034 const unsigned char *src_end = coding->source + coding->src_bytes;
4035 int multibytep = coding->src_multibyte;
4036 int consumed_chars = 0;
4037 int found = 0;
2959 int c; 4038 int c;
2960 /* Dummy for ONE_MORE_BYTE. */ 4039
2961 struct coding_system dummy_coding; 4040 detect_info->checked |= CATEGORY_MASK_BIG5;
2962 struct coding_system *coding = &dummy_coding; 4041 /* A coding system of this category is always ASCII compatible. */
4042 src += coding->head_ascii;
2963 4043
2964 while (1) 4044 while (1)
2965 { 4045 {
2966 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep, CODING_CATEGORY_MASK_BIG5); 4046 src_base = src;
4047 ONE_MORE_BYTE (c);
2967 if (c < 0x80) 4048 if (c < 0x80)
2968 continue; 4049 continue;
2969 if (c < 0xA1 || c > 0xFE) 4050 if (c >= 0xA1)
2970 return 0; 4051 {
2971 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep, 0); 4052 ONE_MORE_BYTE (c);
2972 if (c < 0x40 || (c > 0x7F && c < 0xA1) || c > 0xFE) 4053 if (c < 0x40 || (c >= 0x7F && c <= 0xA0))
2973 return 0; 4054 return 0;
4055 found = CATEGORY_MASK_BIG5;
4056 }
4057 else
4058 break;
2974 } 4059 }
2975} 4060 detect_info->rejected |= CATEGORY_MASK_BIG5;
4061 return 0;
2976 4062
2977/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". 4063 no_more_source:
2978 Check if a text is encoded in UTF-8. If it is, return 4064 if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK)
2979 CODING_CATEGORY_MASK_UTF_8, else return 0. */ 4065 {
4066 detect_info->rejected |= CATEGORY_MASK_BIG5;
4067 return 0;
4068 }
4069 detect_info->found |= found;
4070 return 1;
4071}
2980 4072
2981#define UTF_8_1_OCTET_P(c) ((c) < 0x80) 4073/* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
2982#define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80) 4074 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
2983#define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
2984#define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
2985#define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
2986#define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
2987#define UTF_8_6_OCTET_LEADING_P(c) (((c) & 0xFE) == 0xFC)
2988 4075
2989static int 4076static void
2990detect_coding_utf_8 (src, src_end, multibytep) 4077decode_coding_sjis (coding)
2991 unsigned char *src, *src_end; 4078 struct coding_system *coding;
2992 int multibytep;
2993{ 4079{
2994 unsigned char c; 4080 const unsigned char *src = coding->source + coding->consumed;
2995 int seq_maybe_bytes; 4081 const unsigned char *src_end = coding->source + coding->src_bytes;
2996 /* Dummy for ONE_MORE_BYTE. */ 4082 const unsigned char *src_base;
2997 struct coding_system dummy_coding; 4083 int *charbuf = coding->charbuf + coding->charbuf_used;
2998 struct coding_system *coding = &dummy_coding; 4084 int *charbuf_end
4085 = coding->charbuf + coding->charbuf_size - MAX_ANNOTATION_LENGTH;
4086 int consumed_chars = 0, consumed_chars_base;
4087 int multibytep = coding->src_multibyte;
4088 struct charset *charset_roman, *charset_kanji, *charset_kana;
4089 struct charset *charset_kanji2;
4090 Lisp_Object attrs, charset_list, val;
4091 int char_offset = coding->produced_char;
4092 int last_offset = char_offset;
4093 int last_id = charset_ascii;
4094
4095 CODING_GET_INFO (coding, attrs, charset_list);
4096
4097 val = charset_list;
4098 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4099 charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4100 charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4101 charset_kanji2 = NILP (val) ? NULL : CHARSET_FROM_ID (XINT (XCAR (val)));
2999 4102
3000 while (1) 4103 while (1)
3001 { 4104 {
3002 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep, CODING_CATEGORY_MASK_UTF_8); 4105 int c, c1;
3003 if (UTF_8_1_OCTET_P (c)) 4106 struct charset *charset;
3004 continue;
3005 else if (UTF_8_2_OCTET_LEADING_P (c))
3006 seq_maybe_bytes = 1;
3007 else if (UTF_8_3_OCTET_LEADING_P (c))
3008 seq_maybe_bytes = 2;
3009 else if (UTF_8_4_OCTET_LEADING_P (c))
3010 seq_maybe_bytes = 3;
3011 else if (UTF_8_5_OCTET_LEADING_P (c))
3012 seq_maybe_bytes = 4;
3013 else if (UTF_8_6_OCTET_LEADING_P (c))
3014 seq_maybe_bytes = 5;
3015 else
3016 return 0;
3017
3018 do
3019 {
3020 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep, 0);
3021 if (!UTF_8_EXTRA_OCTET_P (c))
3022 return 0;
3023 seq_maybe_bytes--;
3024 }
3025 while (seq_maybe_bytes > 0);
3026 }
3027}
3028
3029/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
3030 Check if a text is encoded in UTF-16 Big Endian (endian == 1) or
3031 Little Endian (otherwise). If it is, return
3032 CODING_CATEGORY_MASK_UTF_16_BE or CODING_CATEGORY_MASK_UTF_16_LE,
3033 else return 0. */
3034
3035#define UTF_16_INVALID_P(val) \
3036 (((val) == 0xFFFE) \
3037 || ((val) == 0xFFFF))
3038 4107
3039#define UTF_16_HIGH_SURROGATE_P(val) \ 4108 src_base = src;
3040 (((val) & 0xD800) == 0xD800) 4109 consumed_chars_base = consumed_chars;
3041 4110
3042#define UTF_16_LOW_SURROGATE_P(val) \ 4111 if (charbuf >= charbuf_end)
3043 (((val) & 0xDC00) == 0xDC00) 4112 break;
3044 4113
3045static int 4114 ONE_MORE_BYTE (c);
3046detect_coding_utf_16 (src, src_end, multibytep) 4115 if (c < 0)
3047 unsigned char *src, *src_end; 4116 goto invalid_code;
3048 int multibytep; 4117 if (c < 0x80)
3049{ 4118 charset = charset_roman;
3050 unsigned char c1, c2; 4119 else if (c == 0x80 || c == 0xA0)
3051 /* Dummy for ONE_MORE_BYTE_CHECK_MULTIBYTE. */ 4120 goto invalid_code;
3052 struct coding_system dummy_coding; 4121 else if (c >= 0xA1 && c <= 0xDF)
3053 struct coding_system *coding = &dummy_coding; 4122 {
4123 /* SJIS -> JISX0201-Kana */
4124 c &= 0x7F;
4125 charset = charset_kana;
4126 }
4127 else if (c <= 0xEF)
4128 {
4129 /* SJIS -> JISX0208 */
4130 ONE_MORE_BYTE (c1);
4131 if (c1 < 0x40 || c1 == 0x7F || c1 > 0xFC)
4132 goto invalid_code;
4133 c = (c << 8) | c1;
4134 SJIS_TO_JIS (c);
4135 charset = charset_kanji;
4136 }
4137 else if (c <= 0xFC && charset_kanji2)
4138 {
4139 /* SJIS -> JISX0213-2 */
4140 ONE_MORE_BYTE (c1);
4141 if (c1 < 0x40 || c1 == 0x7F || c1 > 0xFC)
4142 goto invalid_code;
4143 c = (c << 8) | c1;
4144 SJIS_TO_JIS2 (c);
4145 charset = charset_kanji2;
4146 }
4147 else
4148 goto invalid_code;
4149 if (charset->id != charset_ascii
4150 && last_id != charset->id)
4151 {
4152 if (last_id != charset_ascii)
4153 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
4154 last_id = charset->id;
4155 last_offset = char_offset;
4156 }
4157 CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c);
4158 *charbuf++ = c;
4159 char_offset++;
4160 continue;
3054 4161
3055 ONE_MORE_BYTE_CHECK_MULTIBYTE (c1, multibytep, 0); 4162 invalid_code:
3056 ONE_MORE_BYTE_CHECK_MULTIBYTE (c2, multibytep, 0); 4163 src = src_base;
4164 consumed_chars = consumed_chars_base;
4165 ONE_MORE_BYTE (c);
4166 *charbuf++ = c < 0 ? -c : BYTE8_TO_CHAR (c);
4167 char_offset++;
4168 coding->errors++;
4169 }
3057 4170
3058 if ((c1 == 0xFF) && (c2 == 0xFE)) 4171 no_more_source:
3059 return CODING_CATEGORY_MASK_UTF_16_LE; 4172 if (last_id != charset_ascii)
3060 else if ((c1 == 0xFE) && (c2 == 0xFF)) 4173 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
3061 return CODING_CATEGORY_MASK_UTF_16_BE; 4174 coding->consumed_char += consumed_chars_base;
3062 return 0; 4175 coding->consumed = src_base - coding->source;
4176 coding->charbuf_used = charbuf - coding->charbuf;
3063} 4177}
3064 4178
3065/* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
3066 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
3067
3068static void 4179static void
3069decode_coding_sjis_big5 (coding, source, destination, 4180decode_coding_big5 (coding)
3070 src_bytes, dst_bytes, sjis_p)
3071 struct coding_system *coding; 4181 struct coding_system *coding;
3072 const unsigned char *source;
3073 unsigned char *destination;
3074 int src_bytes, dst_bytes;
3075 int sjis_p;
3076{ 4182{
3077 const unsigned char *src = source; 4183 const unsigned char *src = coding->source + coding->consumed;
3078 const unsigned char *src_end = source + src_bytes; 4184 const unsigned char *src_end = coding->source + coding->src_bytes;
3079 unsigned char *dst = destination;
3080 unsigned char *dst_end = destination + dst_bytes;
3081 /* SRC_BASE remembers the start position in source in each loop.
3082 The loop will be exited when there's not enough source code
3083 (within macro ONE_MORE_BYTE), or when there's not enough
3084 destination area to produce a character (within macro
3085 EMIT_CHAR). */
3086 const unsigned char *src_base; 4185 const unsigned char *src_base;
3087 Lisp_Object translation_table; 4186 int *charbuf = coding->charbuf + coding->charbuf_used;
4187 int *charbuf_end
4188 = coding->charbuf + coding->charbuf_size - MAX_ANNOTATION_LENGTH;
4189 int consumed_chars = 0, consumed_chars_base;
4190 int multibytep = coding->src_multibyte;
4191 struct charset *charset_roman, *charset_big5;
4192 Lisp_Object attrs, charset_list, val;
4193 int char_offset = coding->produced_char;
4194 int last_offset = char_offset;
4195 int last_id = charset_ascii;
4196
4197 CODING_GET_INFO (coding, attrs, charset_list);
4198 val = charset_list;
4199 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4200 charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val)));
3088 4201
3089 if (NILP (Venable_character_translation))
3090 translation_table = Qnil;
3091 else
3092 {
3093 translation_table = coding->translation_table_for_decode;
3094 if (NILP (translation_table))
3095 translation_table = Vstandard_translation_table_for_decode;
3096 }
3097
3098 coding->produced_char = 0;
3099 while (1) 4202 while (1)
3100 { 4203 {
3101 int c, charset, c1, c2 = 0; 4204 int c, c1;
4205 struct charset *charset;
3102 4206
3103 src_base = src; 4207 src_base = src;
3104 ONE_MORE_BYTE (c1); 4208 consumed_chars_base = consumed_chars;
4209
4210 if (charbuf >= charbuf_end)
4211 break;
4212
4213 ONE_MORE_BYTE (c);
3105 4214
3106 if (c1 < 0x80) 4215 if (c < 0)
4216 goto invalid_code;
4217 if (c < 0x80)
4218 charset = charset_roman;
4219 else
3107 { 4220 {
3108 charset = CHARSET_ASCII; 4221 /* BIG5 -> Big5 */
3109 if (c1 < 0x20) 4222 if (c < 0xA1 || c > 0xFE)
3110 { 4223 goto invalid_code;
3111 if (c1 == '\r') 4224 ONE_MORE_BYTE (c1);
3112 { 4225 if (c1 < 0x40 || (c1 > 0x7E && c1 < 0xA1) || c1 > 0xFE)
3113 if (coding->eol_type == CODING_EOL_CRLF) 4226 goto invalid_code;
3114 { 4227 c = c << 8 | c1;
3115 ONE_MORE_BYTE (c2); 4228 charset = charset_big5;
3116 if (c2 == '\n')
3117 c1 = c2;
3118 else
3119 /* To process C2 again, SRC is subtracted by 1. */
3120 src--;
3121 }
3122 else if (coding->eol_type == CODING_EOL_CR)
3123 c1 = '\n';
3124 }
3125 else if (c1 == '\n'
3126 && (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
3127 && (coding->eol_type == CODING_EOL_CR
3128 || coding->eol_type == CODING_EOL_CRLF))
3129 {
3130 coding->result = CODING_FINISH_INCONSISTENT_EOL;
3131 goto label_end_of_loop;
3132 }
3133 }
3134 } 4229 }
3135 else 4230 if (charset->id != charset_ascii
3136 { 4231 && last_id != charset->id)
3137 if (sjis_p) 4232 {
3138 { 4233 if (last_id != charset_ascii)
3139 if (c1 == 0x80 || c1 == 0xA0 || c1 > 0xEF) 4234 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
3140 goto label_invalid_code; 4235 last_id = charset->id;
3141 if (c1 <= 0x9F || c1 >= 0xE0) 4236 last_offset = char_offset;
3142 {
3143 /* SJIS -> JISX0208 */
3144 ONE_MORE_BYTE (c2);
3145 if (c2 < 0x40 || c2 == 0x7F || c2 > 0xFC)
3146 goto label_invalid_code;
3147 DECODE_SJIS (c1, c2, c1, c2);
3148 charset = charset_jisx0208;
3149 }
3150 else
3151 /* SJIS -> JISX0201-Kana */
3152 charset = charset_katakana_jisx0201;
3153 }
3154 else
3155 {
3156 /* BIG5 -> Big5 */
3157 if (c1 < 0xA0 || c1 > 0xFE)
3158 goto label_invalid_code;
3159 ONE_MORE_BYTE (c2);
3160 if (c2 < 0x40 || (c2 > 0x7E && c2 < 0xA1) || c2 > 0xFE)
3161 goto label_invalid_code;
3162 DECODE_BIG5 (c1, c2, charset, c1, c2);
3163 }
3164 } 4237 }
3165 4238 CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c);
3166 c = DECODE_ISO_CHARACTER (charset, c1, c2); 4239 *charbuf++ = c;
3167 EMIT_CHAR (c); 4240 char_offset++;
3168 continue; 4241 continue;
3169 4242
3170 label_invalid_code: 4243 invalid_code:
3171 coding->errors++;
3172 src = src_base; 4244 src = src_base;
3173 c = *src++; 4245 consumed_chars = consumed_chars_base;
3174 EMIT_CHAR (c); 4246 ONE_MORE_BYTE (c);
4247 *charbuf++ = c < 0 ? -c : BYTE8_TO_CHAR (c);
4248 char_offset++;
4249 coding->errors++;
3175 } 4250 }
3176 4251
3177 label_end_of_loop: 4252 no_more_source:
3178 coding->consumed = coding->consumed_char = src_base - source; 4253 if (last_id != charset_ascii)
3179 coding->produced = dst - destination; 4254 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
3180 return; 4255 coding->consumed_char += consumed_chars_base;
4256 coding->consumed = src_base - coding->source;
4257 coding->charbuf_used = charbuf - coding->charbuf;
3181} 4258}
3182 4259
3183/* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". 4260/* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
@@ -3188,825 +4265,1013 @@ decode_coding_sjis_big5 (coding, source, destination,
3188 charsets are produced without any encoding. If SJIS_P is 1, encode 4265 charsets are produced without any encoding. If SJIS_P is 1, encode
3189 SJIS text, else encode BIG5 text. */ 4266 SJIS text, else encode BIG5 text. */
3190 4267
3191static void 4268static int
3192encode_coding_sjis_big5 (coding, source, destination, 4269encode_coding_sjis (coding)
3193 src_bytes, dst_bytes, sjis_p)
3194 struct coding_system *coding; 4270 struct coding_system *coding;
3195 unsigned char *source, *destination;
3196 int src_bytes, dst_bytes;
3197 int sjis_p;
3198{ 4271{
3199 unsigned char *src = source; 4272 int multibytep = coding->dst_multibyte;
3200 unsigned char *src_end = source + src_bytes; 4273 int *charbuf = coding->charbuf;
3201 unsigned char *dst = destination; 4274 int *charbuf_end = charbuf + coding->charbuf_used;
3202 unsigned char *dst_end = destination + dst_bytes; 4275 unsigned char *dst = coding->destination + coding->produced;
3203 /* SRC_BASE remembers the start position in source in each loop. 4276 unsigned char *dst_end = coding->destination + coding->dst_bytes;
3204 The loop will be exited when there's not enough source text to 4277 int safe_room = 4;
3205 analyze multi-byte codes (within macro ONE_MORE_CHAR), or when 4278 int produced_chars = 0;
3206 there's not enough destination area to produce encoded codes 4279 Lisp_Object attrs, charset_list, val;
3207 (within macro EMIT_BYTES). */ 4280 int ascii_compatible;
3208 unsigned char *src_base; 4281 struct charset *charset_roman, *charset_kanji, *charset_kana;
3209 Lisp_Object translation_table; 4282 struct charset *charset_kanji2;
4283 int c;
3210 4284
3211 if (NILP (Venable_character_translation)) 4285 CODING_GET_INFO (coding, attrs, charset_list);
3212 translation_table = Qnil; 4286 val = charset_list;
3213 else 4287 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
3214 { 4288 charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
3215 translation_table = coding->translation_table_for_encode; 4289 charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
3216 if (NILP (translation_table)) 4290 charset_kanji2 = NILP (val) ? NULL : CHARSET_FROM_ID (XINT (XCAR (val)));
3217 translation_table = Vstandard_translation_table_for_encode;
3218 }
3219 4291
3220 while (1) 4292 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
3221 {
3222 int c, charset, c1, c2;
3223
3224 src_base = src;
3225 ONE_MORE_CHAR (c);
3226 4293
4294 while (charbuf < charbuf_end)
4295 {
4296 ASSURE_DESTINATION (safe_room);
4297 c = *charbuf++;
3227 /* Now encode the character C. */ 4298 /* Now encode the character C. */
3228 if (SINGLE_BYTE_CHAR_P (c)) 4299 if (ASCII_CHAR_P (c) && ascii_compatible)
4300 EMIT_ONE_ASCII_BYTE (c);
4301 else if (CHAR_BYTE8_P (c))
3229 { 4302 {
3230 switch (c) 4303 c = CHAR_TO_BYTE8 (c);
4304 EMIT_ONE_BYTE (c);
4305 }
4306 else
4307 {
4308 unsigned code;
4309 struct charset *charset = char_charset (c, charset_list, &code);
4310
4311 if (!charset)
3231 { 4312 {
3232 case '\r': 4313 if (coding->mode & CODING_MODE_SAFE_ENCODING)
3233 if (!(coding->mode & CODING_MODE_SELECTIVE_DISPLAY))
3234 { 4314 {
3235 EMIT_ONE_BYTE (c); 4315 code = CODING_INHIBIT_CHARACTER_SUBSTITUTION;
3236 break; 4316 charset = CHARSET_FROM_ID (charset_ascii);
3237 } 4317 }
3238 c = '\n'; 4318 else
3239 case '\n':
3240 if (coding->eol_type == CODING_EOL_CRLF)
3241 { 4319 {
3242 EMIT_TWO_BYTES ('\r', c); 4320 c = coding->default_char;
3243 break; 4321 charset = char_charset (c, charset_list, &code);
3244 } 4322 }
3245 else if (coding->eol_type == CODING_EOL_CR)
3246 c = '\r';
3247 default:
3248 EMIT_ONE_BYTE (c);
3249 } 4323 }
3250 } 4324 if (code == CHARSET_INVALID_CODE (charset))
3251 else 4325 abort ();
3252 { 4326 if (charset == charset_kanji)
3253 SPLIT_CHAR (c, charset, c1, c2);
3254 if (sjis_p)
3255 { 4327 {
3256 if (charset == charset_jisx0208 4328 int c1, c2;
3257 || charset == charset_jisx0208_1978) 4329 JIS_TO_SJIS (code);
4330 c1 = code >> 8, c2 = code & 0xFF;
4331 EMIT_TWO_BYTES (c1, c2);
4332 }
4333 else if (charset == charset_kana)
4334 EMIT_ONE_BYTE (code | 0x80);
4335 else if (charset_kanji2 && charset == charset_kanji2)
4336 {
4337 int c1, c2;
4338
4339 c1 = code >> 8;
4340 if (c1 == 0x21 || (c1 >= 0x23 && c1 < 0x25)
4341 || (c1 >= 0x2C && c1 <= 0x2F) || c1 >= 0x6E)
3258 { 4342 {
3259 ENCODE_SJIS (c1, c2, c1, c2); 4343 JIS_TO_SJIS2 (code);
4344 c1 = code >> 8, c2 = code & 0xFF;
3260 EMIT_TWO_BYTES (c1, c2); 4345 EMIT_TWO_BYTES (c1, c2);
3261 } 4346 }
3262 else if (charset == charset_katakana_jisx0201)
3263 EMIT_ONE_BYTE (c1 | 0x80);
3264 else if (charset == charset_latin_jisx0201)
3265 EMIT_ONE_BYTE (c1);
3266 else if (coding->mode & CODING_MODE_INHIBIT_UNENCODABLE_CHAR)
3267 {
3268 EMIT_ONE_BYTE (CODING_REPLACEMENT_CHARACTER);
3269 if (CHARSET_WIDTH (charset) > 1)
3270 EMIT_ONE_BYTE (CODING_REPLACEMENT_CHARACTER);
3271 }
3272 else 4347 else
3273 /* There's no way other than producing the internal 4348 EMIT_ONE_ASCII_BYTE (code & 0x7F);
3274 codes as is. */
3275 EMIT_BYTES (src_base, src);
3276 } 4349 }
3277 else 4350 else
4351 EMIT_ONE_ASCII_BYTE (code & 0x7F);
4352 }
4353 }
4354 record_conversion_result (coding, CODING_RESULT_SUCCESS);
4355 coding->produced_char += produced_chars;
4356 coding->produced = dst - coding->destination;
4357 return 0;
4358}
4359
4360static int
4361encode_coding_big5 (coding)
4362 struct coding_system *coding;
4363{
4364 int multibytep = coding->dst_multibyte;
4365 int *charbuf = coding->charbuf;
4366 int *charbuf_end = charbuf + coding->charbuf_used;
4367 unsigned char *dst = coding->destination + coding->produced;
4368 unsigned char *dst_end = coding->destination + coding->dst_bytes;
4369 int safe_room = 4;
4370 int produced_chars = 0;
4371 Lisp_Object attrs, charset_list, val;
4372 int ascii_compatible;
4373 struct charset *charset_roman, *charset_big5;
4374 int c;
4375
4376 CODING_GET_INFO (coding, attrs, charset_list);
4377 val = charset_list;
4378 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
4379 charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val)));
4380 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
4381
4382 while (charbuf < charbuf_end)
4383 {
4384 ASSURE_DESTINATION (safe_room);
4385 c = *charbuf++;
4386 /* Now encode the character C. */
4387 if (ASCII_CHAR_P (c) && ascii_compatible)
4388 EMIT_ONE_ASCII_BYTE (c);
4389 else if (CHAR_BYTE8_P (c))
4390 {
4391 c = CHAR_TO_BYTE8 (c);
4392 EMIT_ONE_BYTE (c);
4393 }
4394 else
4395 {
4396 unsigned code;
4397 struct charset *charset = char_charset (c, charset_list, &code);
4398
4399 if (! charset)
3278 { 4400 {
3279 if (charset == charset_big5_1 || charset == charset_big5_2) 4401 if (coding->mode & CODING_MODE_SAFE_ENCODING)
3280 { 4402 {
3281 ENCODE_BIG5 (charset, c1, c2, c1, c2); 4403 code = CODING_INHIBIT_CHARACTER_SUBSTITUTION;
3282 EMIT_TWO_BYTES (c1, c2); 4404 charset = CHARSET_FROM_ID (charset_ascii);
3283 } 4405 }
3284 else if (coding->mode & CODING_MODE_INHIBIT_UNENCODABLE_CHAR) 4406 else
3285 { 4407 {
3286 EMIT_ONE_BYTE (CODING_REPLACEMENT_CHARACTER); 4408 c = coding->default_char;
3287 if (CHARSET_WIDTH (charset) > 1) 4409 charset = char_charset (c, charset_list, &code);
3288 EMIT_ONE_BYTE (CODING_REPLACEMENT_CHARACTER);
3289 } 4410 }
3290 else
3291 /* There's no way other than producing the internal
3292 codes as is. */
3293 EMIT_BYTES (src_base, src);
3294 } 4411 }
4412 if (code == CHARSET_INVALID_CODE (charset))
4413 abort ();
4414 if (charset == charset_big5)
4415 {
4416 int c1, c2;
4417
4418 c1 = code >> 8, c2 = code & 0xFF;
4419 EMIT_TWO_BYTES (c1, c2);
4420 }
4421 else
4422 EMIT_ONE_ASCII_BYTE (code & 0x7F);
3295 } 4423 }
3296 coding->consumed_char++;
3297 } 4424 }
3298 4425 record_conversion_result (coding, CODING_RESULT_SUCCESS);
3299 label_end_of_loop: 4426 coding->produced_char += produced_chars;
3300 coding->consumed = src_base - source; 4427 coding->produced = dst - coding->destination;
3301 coding->produced = coding->produced_char = dst - destination; 4428 return 0;
3302} 4429}
3303 4430
3304 4431
3305/*** 5. CCL handlers ***/ 4432/*** 10. CCL handlers ***/
3306 4433
3307/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". 4434/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
3308 Check if a text is encoded in a coding system of which 4435 Check if a text is encoded in a coding system of which
3309 encoder/decoder are written in CCL program. If it is, return 4436 encoder/decoder are written in CCL program. If it is, return
3310 CODING_CATEGORY_MASK_CCL, else return 0. */ 4437 CATEGORY_MASK_CCL, else return 0. */
3311 4438
3312static int 4439static int
3313detect_coding_ccl (src, src_end, multibytep) 4440detect_coding_ccl (coding, detect_info)
3314 unsigned char *src, *src_end; 4441 struct coding_system *coding;
3315 int multibytep; 4442 struct coding_detection_info *detect_info;
3316{ 4443{
3317 unsigned char *valid; 4444 const unsigned char *src = coding->source, *src_base;
3318 int c; 4445 const unsigned char *src_end = coding->source + coding->src_bytes;
3319 /* Dummy for ONE_MORE_BYTE. */ 4446 int multibytep = coding->src_multibyte;
3320 struct coding_system dummy_coding; 4447 int consumed_chars = 0;
3321 struct coding_system *coding = &dummy_coding; 4448 int found = 0;
4449 unsigned char *valids;
4450 int head_ascii = coding->head_ascii;
4451 Lisp_Object attrs;
4452
4453 detect_info->checked |= CATEGORY_MASK_CCL;
4454
4455 coding = &coding_categories[coding_category_ccl];
4456 valids = CODING_CCL_VALIDS (coding);
4457 attrs = CODING_ID_ATTRS (coding->id);
4458 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
4459 src += head_ascii;
3322 4460
3323 /* No coding system is assigned to coding-category-ccl. */
3324 if (!coding_system_table[CODING_CATEGORY_IDX_CCL])
3325 return 0;
3326
3327 valid = coding_system_table[CODING_CATEGORY_IDX_CCL]->spec.ccl.valid_codes;
3328 while (1) 4461 while (1)
3329 { 4462 {
3330 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep, CODING_CATEGORY_MASK_CCL); 4463 int c;
3331 if (! valid[c]) 4464
3332 return 0; 4465 src_base = src;
4466 ONE_MORE_BYTE (c);
4467 if (c < 0 || ! valids[c])
4468 break;
4469 if ((valids[c] > 1))
4470 found = CATEGORY_MASK_CCL;
4471 }
4472 detect_info->rejected |= CATEGORY_MASK_CCL;
4473 return 0;
4474
4475 no_more_source:
4476 detect_info->found |= found;
4477 return 1;
4478}
4479
4480static void
4481decode_coding_ccl (coding)
4482 struct coding_system *coding;
4483{
4484 const unsigned char *src = coding->source + coding->consumed;
4485 const unsigned char *src_end = coding->source + coding->src_bytes;
4486 int *charbuf = coding->charbuf + coding->charbuf_used;
4487 int *charbuf_end = coding->charbuf + coding->charbuf_size;
4488 int consumed_chars = 0;
4489 int multibytep = coding->src_multibyte;
4490 struct ccl_program ccl;
4491 int source_charbuf[1024];
4492 int source_byteidx[1024];
4493 Lisp_Object attrs, charset_list;
4494
4495 CODING_GET_INFO (coding, attrs, charset_list);
4496 setup_ccl_program (&ccl, CODING_CCL_DECODER (coding));
4497
4498 while (src < src_end)
4499 {
4500 const unsigned char *p = src;
4501 int *source, *source_end;
4502 int i = 0;
4503
4504 if (multibytep)
4505 while (i < 1024 && p < src_end)
4506 {
4507 source_byteidx[i] = p - src;
4508 source_charbuf[i++] = STRING_CHAR_ADVANCE (p);
4509 }
4510 else
4511 while (i < 1024 && p < src_end)
4512 source_charbuf[i++] = *p++;
4513
4514 if (p == src_end && coding->mode & CODING_MODE_LAST_BLOCK)
4515 ccl.last_block = 1;
4516
4517 source = source_charbuf;
4518 source_end = source + i;
4519 while (source < source_end)
4520 {
4521 ccl_driver (&ccl, source, charbuf,
4522 source_end - source, charbuf_end - charbuf,
4523 charset_list);
4524 source += ccl.consumed;
4525 charbuf += ccl.produced;
4526 if (ccl.status != CCL_STAT_SUSPEND_BY_DST)
4527 break;
4528 }
4529 if (source < source_end)
4530 src += source_byteidx[source - source_charbuf];
4531 else
4532 src = p;
4533 consumed_chars += source - source_charbuf;
4534
4535 if (ccl.status != CCL_STAT_SUSPEND_BY_SRC
4536 && ccl.status != CODING_RESULT_INSUFFICIENT_SRC)
4537 break;
4538 }
4539
4540 switch (ccl.status)
4541 {
4542 case CCL_STAT_SUSPEND_BY_SRC:
4543 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC);
4544 break;
4545 case CCL_STAT_SUSPEND_BY_DST:
4546 break;
4547 case CCL_STAT_QUIT:
4548 case CCL_STAT_INVALID_CMD:
4549 record_conversion_result (coding, CODING_RESULT_INTERRUPT);
4550 break;
4551 default:
4552 record_conversion_result (coding, CODING_RESULT_SUCCESS);
4553 break;
4554 }
4555 coding->consumed_char += consumed_chars;
4556 coding->consumed = src - coding->source;
4557 coding->charbuf_used = charbuf - coding->charbuf;
4558}
4559
4560static int
4561encode_coding_ccl (coding)
4562 struct coding_system *coding;
4563{
4564 struct ccl_program ccl;
4565 int multibytep = coding->dst_multibyte;
4566 int *charbuf = coding->charbuf;
4567 int *charbuf_end = charbuf + coding->charbuf_used;
4568 unsigned char *dst = coding->destination + coding->produced;
4569 unsigned char *dst_end = coding->destination + coding->dst_bytes;
4570 int destination_charbuf[1024];
4571 int i, produced_chars = 0;
4572 Lisp_Object attrs, charset_list;
4573
4574 CODING_GET_INFO (coding, attrs, charset_list);
4575 setup_ccl_program (&ccl, CODING_CCL_ENCODER (coding));
4576
4577 ccl.last_block = coding->mode & CODING_MODE_LAST_BLOCK;
4578 ccl.dst_multibyte = coding->dst_multibyte;
4579
4580 while (charbuf < charbuf_end)
4581 {
4582 ccl_driver (&ccl, charbuf, destination_charbuf,
4583 charbuf_end - charbuf, 1024, charset_list);
4584 if (multibytep)
4585 {
4586 ASSURE_DESTINATION (ccl.produced * 2);
4587 for (i = 0; i < ccl.produced; i++)
4588 EMIT_ONE_BYTE (destination_charbuf[i] & 0xFF);
4589 }
4590 else
4591 {
4592 ASSURE_DESTINATION (ccl.produced);
4593 for (i = 0; i < ccl.produced; i++)
4594 *dst++ = destination_charbuf[i] & 0xFF;
4595 produced_chars += ccl.produced;
4596 }
4597 charbuf += ccl.consumed;
4598 if (ccl.status == CCL_STAT_QUIT
4599 || ccl.status == CCL_STAT_INVALID_CMD)
4600 break;
4601 }
4602
4603 switch (ccl.status)
4604 {
4605 case CCL_STAT_SUSPEND_BY_SRC:
4606 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC);
4607 break;
4608 case CCL_STAT_SUSPEND_BY_DST:
4609 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_DST);
4610 break;
4611 case CCL_STAT_QUIT:
4612 case CCL_STAT_INVALID_CMD:
4613 record_conversion_result (coding, CODING_RESULT_INTERRUPT);
4614 break;
4615 default:
4616 record_conversion_result (coding, CODING_RESULT_SUCCESS);
4617 break;
3333 } 4618 }
4619
4620 coding->produced_char += produced_chars;
4621 coding->produced = dst - coding->destination;
4622 return 0;
3334} 4623}
3335 4624
4625
3336 4626
3337/*** 6. End-of-line handlers ***/ 4627/*** 10, 11. no-conversion handlers ***/
3338 4628
3339/* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ 4629/* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
3340 4630
3341static void 4631static void
3342decode_eol (coding, source, destination, src_bytes, dst_bytes) 4632decode_coding_raw_text (coding)
3343 struct coding_system *coding; 4633 struct coding_system *coding;
3344 const unsigned char *source;
3345 unsigned char *destination;
3346 int src_bytes, dst_bytes;
3347{ 4634{
3348 const unsigned char *src = source; 4635 coding->chars_at_source = 1;
3349 unsigned char *dst = destination; 4636 coding->consumed_char = 0;
3350 const unsigned char *src_end = src + src_bytes; 4637 coding->consumed = 0;
3351 unsigned char *dst_end = dst + dst_bytes; 4638 record_conversion_result (coding, CODING_RESULT_SUCCESS);
3352 Lisp_Object translation_table; 4639}
3353 /* SRC_BASE remembers the start position in source in each loop. 4640
3354 The loop will be exited when there's not enough source code 4641static int
3355 (within macro ONE_MORE_BYTE), or when there's not enough 4642encode_coding_raw_text (coding)
3356 destination area to produce a character (within macro 4643 struct coding_system *coding;
3357 EMIT_CHAR). */ 4644{
3358 const unsigned char *src_base; 4645 int multibytep = coding->dst_multibyte;
4646 int *charbuf = coding->charbuf;
4647 int *charbuf_end = coding->charbuf + coding->charbuf_used;
4648 unsigned char *dst = coding->destination + coding->produced;
4649 unsigned char *dst_end = coding->destination + coding->dst_bytes;
4650 int produced_chars = 0;
3359 int c; 4651 int c;
3360 4652
3361 translation_table = Qnil; 4653 if (multibytep)
3362 switch (coding->eol_type) 4654 {
4655 int safe_room = MAX_MULTIBYTE_LENGTH * 2;
4656
4657 if (coding->src_multibyte)
4658 while (charbuf < charbuf_end)
4659 {
4660 ASSURE_DESTINATION (safe_room);
4661 c = *charbuf++;
4662 if (ASCII_CHAR_P (c))
4663 EMIT_ONE_ASCII_BYTE (c);
4664 else if (CHAR_BYTE8_P (c))
4665 {
4666 c = CHAR_TO_BYTE8 (c);
4667 EMIT_ONE_BYTE (c);
4668 }
4669 else
4670 {
4671 unsigned char str[MAX_MULTIBYTE_LENGTH], *p0 = str, *p1 = str;
4672
4673 CHAR_STRING_ADVANCE (c, p1);
4674 while (p0 < p1)
4675 {
4676 EMIT_ONE_BYTE (*p0);
4677 p0++;
4678 }
4679 }
4680 }
4681 else
4682 while (charbuf < charbuf_end)
4683 {
4684 ASSURE_DESTINATION (safe_room);
4685 c = *charbuf++;
4686 EMIT_ONE_BYTE (c);
4687 }
4688 }
4689 else
3363 { 4690 {
3364 case CODING_EOL_CRLF: 4691 if (coding->src_multibyte)
3365 while (1)
3366 { 4692 {
3367 src_base = src; 4693 int safe_room = MAX_MULTIBYTE_LENGTH;
3368 ONE_MORE_BYTE (c); 4694
3369 if (c == '\r') 4695 while (charbuf < charbuf_end)
3370 { 4696 {
3371 ONE_MORE_BYTE (c); 4697 ASSURE_DESTINATION (safe_room);
3372 if (c != '\n') 4698 c = *charbuf++;
3373 { 4699 if (ASCII_CHAR_P (c))
3374 src--; 4700 *dst++ = c;
3375 c = '\r'; 4701 else if (CHAR_BYTE8_P (c))
3376 } 4702 *dst++ = CHAR_TO_BYTE8 (c);
4703 else
4704 CHAR_STRING_ADVANCE (c, dst);
4705 produced_chars++;
3377 } 4706 }
3378 else if (c == '\n' 4707 }
3379 && (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)) 4708 else
4709 {
4710 ASSURE_DESTINATION (charbuf_end - charbuf);
4711 while (charbuf < charbuf_end && dst < dst_end)
4712 *dst++ = *charbuf++;
4713 produced_chars = dst - (coding->destination + coding->dst_bytes);
4714 }
4715 }
4716 record_conversion_result (coding, CODING_RESULT_SUCCESS);
4717 coding->produced_char += produced_chars;
4718 coding->produced = dst - coding->destination;
4719 return 0;
4720}
4721
4722/* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
4723 Check if a text is encoded in a charset-based coding system. If it
4724 is, return 1, else return 0. */
4725
4726static int
4727detect_coding_charset (coding, detect_info)
4728 struct coding_system *coding;
4729 struct coding_detection_info *detect_info;
4730{
4731 const unsigned char *src = coding->source, *src_base;
4732 const unsigned char *src_end = coding->source + coding->src_bytes;
4733 int multibytep = coding->src_multibyte;
4734 int consumed_chars = 0;
4735 Lisp_Object attrs, valids;
4736 int found = 0;
4737 int head_ascii = coding->head_ascii;
4738
4739 detect_info->checked |= CATEGORY_MASK_CHARSET;
4740
4741 coding = &coding_categories[coding_category_charset];
4742 attrs = CODING_ID_ATTRS (coding->id);
4743 valids = AREF (attrs, coding_attr_charset_valids);
4744
4745 if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
4746 src += head_ascii;
4747
4748 while (1)
4749 {
4750 int c;
4751 Lisp_Object val;
4752 struct charset *charset;
4753 int dim, idx;
4754
4755 src_base = src;
4756 ONE_MORE_BYTE (c);
4757 if (c < 0)
4758 continue;
4759 val = AREF (valids, c);
4760 if (NILP (val))
4761 break;
4762 if (c >= 0x80)
4763 found = CATEGORY_MASK_CHARSET;
4764 if (INTEGERP (val))
4765 {
4766 charset = CHARSET_FROM_ID (XFASTINT (val));
4767 dim = CHARSET_DIMENSION (charset);
4768 for (idx = 1; idx < dim; idx++)
3380 { 4769 {
3381 coding->result = CODING_FINISH_INCONSISTENT_EOL; 4770 if (src == src_end)
3382 goto label_end_of_loop; 4771 goto too_short;
4772 ONE_MORE_BYTE (c);
4773 if (c < charset->code_space[(dim - 1 - idx) * 2]
4774 || c > charset->code_space[(dim - 1 - idx) * 2 + 1])
4775 break;
3383 } 4776 }
3384 EMIT_CHAR (c); 4777 if (idx < dim)
4778 break;
3385 } 4779 }
3386 break; 4780 else
3387
3388 case CODING_EOL_CR:
3389 while (1)
3390 { 4781 {
3391 src_base = src; 4782 idx = 1;
3392 ONE_MORE_BYTE (c); 4783 for (; CONSP (val); val = XCDR (val))
3393 if (c == '\n')
3394 { 4784 {
3395 if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL) 4785 charset = CHARSET_FROM_ID (XFASTINT (XCAR (val)));
4786 dim = CHARSET_DIMENSION (charset);
4787 while (idx < dim)
4788 {
4789 if (src == src_end)
4790 goto too_short;
4791 ONE_MORE_BYTE (c);
4792 if (c < charset->code_space[(dim - 1 - idx) * 4]
4793 || c > charset->code_space[(dim - 1 - idx) * 4 + 1])
4794 break;
4795 idx++;
4796 }
4797 if (idx == dim)
3396 { 4798 {
3397 coding->result = CODING_FINISH_INCONSISTENT_EOL; 4799 val = Qnil;
3398 goto label_end_of_loop; 4800 break;
3399 } 4801 }
3400 } 4802 }
3401 else if (c == '\r') 4803 if (CONSP (val))
3402 c = '\n'; 4804 break;
3403 EMIT_CHAR (c);
3404 }
3405 break;
3406
3407 default: /* no need for EOL handling */
3408 while (1)
3409 {
3410 src_base = src;
3411 ONE_MORE_BYTE (c);
3412 EMIT_CHAR (c);
3413 } 4805 }
3414 } 4806 }
4807 too_short:
4808 detect_info->rejected |= CATEGORY_MASK_CHARSET;
4809 return 0;
3415 4810
3416 label_end_of_loop: 4811 no_more_source:
3417 coding->consumed = coding->consumed_char = src_base - source; 4812 detect_info->found |= found;
3418 coding->produced = dst - destination; 4813 return 1;
3419 return;
3420} 4814}
3421 4815
3422/* See "GENERAL NOTES about `encode_coding_XXX ()' functions". Encode
3423 format of end-of-line according to `coding->eol_type'. It also
3424 convert multibyte form 8-bit characters to unibyte if
3425 CODING->src_multibyte is nonzero. If `coding->mode &
3426 CODING_MODE_SELECTIVE_DISPLAY' is nonzero, code '\r' in source text
3427 also means end-of-line. */
3428
3429static void 4816static void
3430encode_eol (coding, source, destination, src_bytes, dst_bytes) 4817decode_coding_charset (coding)
3431 struct coding_system *coding; 4818 struct coding_system *coding;
3432 const unsigned char *source;
3433 unsigned char *destination;
3434 int src_bytes, dst_bytes;
3435{ 4819{
3436 const unsigned char *src = source; 4820 const unsigned char *src = coding->source + coding->consumed;
3437 unsigned char *dst = destination; 4821 const unsigned char *src_end = coding->source + coding->src_bytes;
3438 const unsigned char *src_end = src + src_bytes;
3439 unsigned char *dst_end = dst + dst_bytes;
3440 Lisp_Object translation_table;
3441 /* SRC_BASE remembers the start position in source in each loop.
3442 The loop will be exited when there's not enough source text to
3443 analyze multi-byte codes (within macro ONE_MORE_CHAR), or when
3444 there's not enough destination area to produce encoded codes
3445 (within macro EMIT_BYTES). */
3446 const unsigned char *src_base; 4822 const unsigned char *src_base;
3447 unsigned char *tmp; 4823 int *charbuf = coding->charbuf + coding->charbuf_used;
3448 int c; 4824 int *charbuf_end
3449 int selective_display = coding->mode & CODING_MODE_SELECTIVE_DISPLAY; 4825 = coding->charbuf + coding->charbuf_size - MAX_ANNOTATION_LENGTH;
4826 int consumed_chars = 0, consumed_chars_base;
4827 int multibytep = coding->src_multibyte;
4828 Lisp_Object attrs, charset_list, valids;
4829 int char_offset = coding->produced_char;
4830 int last_offset = char_offset;
4831 int last_id = charset_ascii;
4832
4833 CODING_GET_INFO (coding, attrs, charset_list);
4834 valids = AREF (attrs, coding_attr_charset_valids);
3450 4835
3451 translation_table = Qnil; 4836 while (1)
3452 if (coding->src_multibyte
3453 && *(src_end - 1) == LEADING_CODE_8_BIT_CONTROL)
3454 { 4837 {
3455 src_end--; 4838 int c;
3456 src_bytes--; 4839 Lisp_Object val;
3457 coding->result = CODING_FINISH_INSUFFICIENT_SRC; 4840 struct charset *charset;
3458 } 4841 int dim;
4842 int len = 1;
4843 unsigned code;
3459 4844
3460 if (coding->eol_type == CODING_EOL_CRLF)
3461 {
3462 while (src < src_end)
3463 {
3464 src_base = src;
3465 c = *src++;
3466 if (c >= 0x20)
3467 EMIT_ONE_BYTE (c);
3468 else if (c == '\n' || (c == '\r' && selective_display))
3469 EMIT_TWO_BYTES ('\r', '\n');
3470 else
3471 EMIT_ONE_BYTE (c);
3472 }
3473 src_base = src; 4845 src_base = src;
3474 label_end_of_loop: 4846 consumed_chars_base = consumed_chars;
3475 ; 4847
3476 } 4848 if (charbuf >= charbuf_end)
3477 else 4849 break;
3478 { 4850
3479 if (!dst_bytes || src_bytes <= dst_bytes) 4851 ONE_MORE_BYTE (c);
4852 if (c < 0)
4853 goto invalid_code;
4854 code = c;
4855
4856 val = AREF (valids, c);
4857 if (NILP (val))
4858 goto invalid_code;
4859 if (INTEGERP (val))
3480 { 4860 {
3481 safe_bcopy (src, dst, src_bytes); 4861 charset = CHARSET_FROM_ID (XFASTINT (val));
3482 src_base = src_end; 4862 dim = CHARSET_DIMENSION (charset);
3483 dst += src_bytes; 4863 while (len < dim)
4864 {
4865 ONE_MORE_BYTE (c);
4866 code = (code << 8) | c;
4867 len++;
4868 }
4869 CODING_DECODE_CHAR (coding, src, src_base, src_end,
4870 charset, code, c);
3484 } 4871 }
3485 else 4872 else
3486 { 4873 {
3487 if (coding->src_multibyte 4874 /* VAL is a list of charset IDs. It is assured that the
3488 && *(src + dst_bytes - 1) == LEADING_CODE_8_BIT_CONTROL) 4875 list is sorted by charset dimensions (smaller one
3489 dst_bytes--; 4876 comes first). */
3490 safe_bcopy (src, dst, dst_bytes); 4877 while (CONSP (val))
3491 src_base = src + dst_bytes; 4878 {
3492 dst = destination + dst_bytes; 4879 charset = CHARSET_FROM_ID (XFASTINT (XCAR (val)));
3493 coding->result = CODING_FINISH_INSUFFICIENT_DST; 4880 dim = CHARSET_DIMENSION (charset);
4881 while (len < dim)
4882 {
4883 ONE_MORE_BYTE (c);
4884 code = (code << 8) | c;
4885 len++;
4886 }
4887 CODING_DECODE_CHAR (coding, src, src_base,
4888 src_end, charset, code, c);
4889 if (c >= 0)
4890 break;
4891 val = XCDR (val);
4892 }
4893 }
4894 if (c < 0)
4895 goto invalid_code;
4896 if (charset->id != charset_ascii
4897 && last_id != charset->id)
4898 {
4899 if (last_id != charset_ascii)
4900 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
4901 last_id = charset->id;
4902 last_offset = char_offset;
3494 } 4903 }
3495 if (coding->eol_type == CODING_EOL_CR) 4904
4905 *charbuf++ = c;
4906 char_offset++;
4907 continue;
4908
4909 invalid_code:
4910 src = src_base;
4911 consumed_chars = consumed_chars_base;
4912 ONE_MORE_BYTE (c);
4913 *charbuf++ = c < 0 ? -c : ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c);
4914 char_offset++;
4915 coding->errors++;
4916 }
4917
4918 no_more_source:
4919 if (last_id != charset_ascii)
4920 ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id);
4921 coding->consumed_char += consumed_chars_base;
4922 coding->consumed = src_base - coding->source;
4923 coding->charbuf_used = charbuf - coding->charbuf;
4924}
4925
4926static int
4927encode_coding_charset (coding)
4928 struct coding_system *coding;
4929{
4930 int multibytep = coding->dst_multibyte;
4931 int *charbuf = coding->charbuf;
4932 int *charbuf_end = charbuf + coding->charbuf_used;
4933 unsigned char *dst = coding->destination + coding->produced;
4934 unsigned char *dst_end = coding->destination + coding->dst_bytes;
4935 int safe_room = MAX_MULTIBYTE_LENGTH;
4936 int produced_chars = 0;
4937 Lisp_Object attrs, charset_list;
4938 int ascii_compatible;
4939 int c;
4940
4941 CODING_GET_INFO (coding, attrs, charset_list);
4942 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
4943
4944 while (charbuf < charbuf_end)
4945 {
4946 struct charset *charset;
4947 unsigned code;
4948
4949 ASSURE_DESTINATION (safe_room);
4950 c = *charbuf++;
4951 if (ascii_compatible && ASCII_CHAR_P (c))
4952 EMIT_ONE_ASCII_BYTE (c);
4953 else if (CHAR_BYTE8_P (c))
3496 { 4954 {
3497 for (tmp = destination; tmp < dst; tmp++) 4955 c = CHAR_TO_BYTE8 (c);
3498 if (*tmp == '\n') *tmp = '\r'; 4956 EMIT_ONE_BYTE (c);
3499 } 4957 }
3500 else if (selective_display) 4958 else
3501 { 4959 {
3502 for (tmp = destination; tmp < dst; tmp++) 4960 charset = char_charset (c, charset_list, &code);
3503 if (*tmp == '\r') *tmp = '\n'; 4961 if (charset)
4962 {
4963 if (CHARSET_DIMENSION (charset) == 1)
4964 EMIT_ONE_BYTE (code);
4965 else if (CHARSET_DIMENSION (charset) == 2)
4966 EMIT_TWO_BYTES (code >> 8, code & 0xFF);
4967 else if (CHARSET_DIMENSION (charset) == 3)
4968 EMIT_THREE_BYTES (code >> 16, (code >> 8) & 0xFF, code & 0xFF);
4969 else
4970 EMIT_FOUR_BYTES (code >> 24, (code >> 16) & 0xFF,
4971 (code >> 8) & 0xFF, code & 0xFF);
4972 }
4973 else
4974 {
4975 if (coding->mode & CODING_MODE_SAFE_ENCODING)
4976 c = CODING_INHIBIT_CHARACTER_SUBSTITUTION;
4977 else
4978 c = coding->default_char;
4979 EMIT_ONE_BYTE (c);
4980 }
3504 } 4981 }
3505 } 4982 }
3506 if (coding->src_multibyte)
3507 dst = destination + str_as_unibyte (destination, dst - destination);
3508 4983
3509 coding->consumed = src_base - source; 4984 record_conversion_result (coding, CODING_RESULT_SUCCESS);
3510 coding->produced = dst - destination; 4985 coding->produced_char += produced_chars;
3511 coding->produced_char = coding->produced; 4986 coding->produced = dst - coding->destination;
4987 return 0;
3512} 4988}
3513 4989
3514 4990
3515/*** 7. C library functions ***/ 4991/*** 7. C library functions ***/
3516 4992
3517/* In Emacs Lisp, a coding system is represented by a Lisp symbol which 4993/* Setup coding context CODING from information about CODING_SYSTEM.
3518 has a property `coding-system'. The value of this property is a 4994 If CODING_SYSTEM is nil, `no-conversion' is assumed. If
3519 vector of length 5 (called the coding-vector). Among elements of 4995 CODING_SYSTEM is invalid, signal an error. */
3520 this vector, the first (element[0]) and the fifth (element[4])
3521 carry important information for decoding/encoding. Before
3522 decoding/encoding, this information should be set in fields of a
3523 structure of type `coding_system'.
3524
3525 The value of the property `coding-system' can be a symbol of another
3526 subsidiary coding-system. In that case, Emacs gets coding-vector
3527 from that symbol.
3528
3529 `element[0]' contains information to be set in `coding->type'. The
3530 value and its meaning is as follows:
3531
3532 0 -- coding_type_emacs_mule
3533 1 -- coding_type_sjis
3534 2 -- coding_type_iso2022
3535 3 -- coding_type_big5
3536 4 -- coding_type_ccl encoder/decoder written in CCL
3537 nil -- coding_type_no_conversion
3538 t -- coding_type_undecided (automatic conversion on decoding,
3539 no-conversion on encoding)
3540
3541 `element[4]' contains information to be set in `coding->flags' and
3542 `coding->spec'. The meaning varies by `coding->type'.
3543
3544 If `coding->type' is `coding_type_iso2022', element[4] is a vector
3545 of length 32 (of which the first 13 sub-elements are used now).
3546 Meanings of these sub-elements are:
3547
3548 sub-element[N] where N is 0 through 3: to be set in `coding->spec.iso2022'
3549 If the value is an integer of valid charset, the charset is
3550 assumed to be designated to graphic register N initially.
3551
3552 If the value is minus, it is a minus value of charset which
3553 reserves graphic register N, which means that the charset is
3554 not designated initially but should be designated to graphic
3555 register N just before encoding a character in that charset.
3556
3557 If the value is nil, graphic register N is never used on
3558 encoding.
3559
3560 sub-element[N] where N is 4 through 11: to be set in `coding->flags'
3561 Each value takes t or nil. See the section ISO2022 of
3562 `coding.h' for more information.
3563
3564 If `coding->type' is `coding_type_big5', element[4] is t to denote
3565 BIG5-ETen or nil to denote BIG5-HKU.
3566
3567 If `coding->type' takes the other value, element[4] is ignored.
3568
3569 Emacs Lisp's coding systems also carry information about format of
3570 end-of-line in a value of property `eol-type'. If the value is
3571 integer, 0 means CODING_EOL_LF, 1 means CODING_EOL_CRLF, and 2
3572 means CODING_EOL_CR. If it is not integer, it should be a vector
3573 of subsidiary coding systems of which property `eol-type' has one
3574 of the above values.
3575
3576*/
3577
3578/* Extract information for decoding/encoding from CODING_SYSTEM_SYMBOL
3579 and set it in CODING. If CODING_SYSTEM_SYMBOL is invalid, CODING
3580 is setup so that no conversion is necessary and return -1, else
3581 return 0. */
3582 4996
3583int 4997void
3584setup_coding_system (coding_system, coding) 4998setup_coding_system (coding_system, coding)
3585 Lisp_Object coding_system; 4999 Lisp_Object coding_system;
3586 struct coding_system *coding; 5000 struct coding_system *coding;
3587{ 5001{
3588 Lisp_Object coding_spec, coding_type, eol_type, plist; 5002 Lisp_Object attrs;
5003 Lisp_Object eol_type;
5004 Lisp_Object coding_type;
3589 Lisp_Object val; 5005 Lisp_Object val;
3590 5006
3591 /* At first, zero clear all members. */
3592 bzero (coding, sizeof (struct coding_system));
3593
3594 /* Initialize some fields required for all kinds of coding systems. */
3595 coding->symbol = coding_system;
3596 coding->heading_ascii = -1;
3597 coding->post_read_conversion = coding->pre_write_conversion = Qnil;
3598 coding->composing = COMPOSITION_DISABLED;
3599 coding->cmp_data = NULL;
3600
3601 if (NILP (coding_system)) 5007 if (NILP (coding_system))
3602 goto label_invalid_coding_system; 5008 coding_system = Qundecided;
3603 5009
3604 coding_spec = Fget (coding_system, Qcoding_system); 5010 CHECK_CODING_SYSTEM_GET_ID (coding_system, coding->id);
3605 5011
3606 if (!VECTORP (coding_spec) 5012 attrs = CODING_ID_ATTRS (coding->id);
3607 || XVECTOR (coding_spec)->size != 5 5013 eol_type = CODING_ID_EOL_TYPE (coding->id);
3608 || !CONSP (XVECTOR (coding_spec)->contents[3]))
3609 goto label_invalid_coding_system;
3610 5014
3611 eol_type = inhibit_eol_conversion ? Qnil : Fget (coding_system, Qeol_type); 5015 coding->mode = 0;
5016 coding->head_ascii = -1;
3612 if (VECTORP (eol_type)) 5017 if (VECTORP (eol_type))
5018 coding->common_flags = (CODING_REQUIRE_DECODING_MASK
5019 | CODING_REQUIRE_DETECTION_MASK);
5020 else if (! EQ (eol_type, Qunix))
5021 coding->common_flags = (CODING_REQUIRE_DECODING_MASK
5022 | CODING_REQUIRE_ENCODING_MASK);
5023 else
5024 coding->common_flags = 0;
5025 if (! NILP (CODING_ATTR_POST_READ (attrs)))
5026 coding->common_flags |= CODING_REQUIRE_DECODING_MASK;
5027 if (! NILP (CODING_ATTR_PRE_WRITE (attrs)))
5028 coding->common_flags |= CODING_REQUIRE_ENCODING_MASK;
5029 if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs)))
5030 coding->common_flags |= CODING_FOR_UNIBYTE_MASK;
5031
5032 val = CODING_ATTR_SAFE_CHARSETS (attrs);
5033 coding->max_charset_id = SCHARS (val) - 1;
5034 coding->safe_charsets = (char *) SDATA (val);
5035 coding->default_char = XINT (CODING_ATTR_DEFAULT_CHAR (attrs));
5036
5037 coding_type = CODING_ATTR_TYPE (attrs);
5038 if (EQ (coding_type, Qundecided))
3613 { 5039 {
3614 coding->eol_type = CODING_EOL_UNDECIDED; 5040 coding->detector = NULL;
3615 coding->common_flags = CODING_REQUIRE_DETECTION_MASK; 5041 coding->decoder = decode_coding_raw_text;
3616 if (system_eol_type != CODING_EOL_LF) 5042 coding->encoder = encode_coding_raw_text;
3617 coding->common_flags |= CODING_REQUIRE_ENCODING_MASK; 5043 coding->common_flags |= CODING_REQUIRE_DETECTION_MASK;
3618 } 5044 }
3619 else if (XFASTINT (eol_type) == 1) 5045 else if (EQ (coding_type, Qiso_2022))
3620 { 5046 {
3621 coding->eol_type = CODING_EOL_CRLF; 5047 int i;
5048 int flags = XINT (AREF (attrs, coding_attr_iso_flags));
5049
5050 /* Invoke graphic register 0 to plane 0. */
5051 CODING_ISO_INVOCATION (coding, 0) = 0;
5052 /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */
5053 CODING_ISO_INVOCATION (coding, 1)
5054 = (flags & CODING_ISO_FLAG_SEVEN_BITS ? -1 : 1);
5055 /* Setup the initial status of designation. */
5056 for (i = 0; i < 4; i++)
5057 CODING_ISO_DESIGNATION (coding, i) = CODING_ISO_INITIAL (coding, i);
5058 /* Not single shifting initially. */
5059 CODING_ISO_SINGLE_SHIFTING (coding) = 0;
5060 /* Beginning of buffer should also be regarded as bol. */
5061 CODING_ISO_BOL (coding) = 1;
5062 coding->detector = detect_coding_iso_2022;
5063 coding->decoder = decode_coding_iso_2022;
5064 coding->encoder = encode_coding_iso_2022;
5065 if (flags & CODING_ISO_FLAG_SAFE)
5066 coding->mode |= CODING_MODE_SAFE_ENCODING;
3622 coding->common_flags 5067 coding->common_flags
3623 = CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK; 5068 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK
5069 | CODING_REQUIRE_FLUSHING_MASK);
5070 if (flags & CODING_ISO_FLAG_COMPOSITION)
5071 coding->common_flags |= CODING_ANNOTATE_COMPOSITION_MASK;
5072 if (flags & CODING_ISO_FLAG_DESIGNATION)
5073 coding->common_flags |= CODING_ANNOTATE_CHARSET_MASK;
5074 if (flags & CODING_ISO_FLAG_FULL_SUPPORT)
5075 {
5076 setup_iso_safe_charsets (attrs);
5077 val = CODING_ATTR_SAFE_CHARSETS (attrs);
5078 coding->max_charset_id = SCHARS (val) - 1;
5079 coding->safe_charsets = (char *) SDATA (val);
5080 }
5081 CODING_ISO_FLAGS (coding) = flags;
3624 } 5082 }
3625 else if (XFASTINT (eol_type) == 2) 5083 else if (EQ (coding_type, Qcharset))
3626 { 5084 {
3627 coding->eol_type = CODING_EOL_CR; 5085 coding->detector = detect_coding_charset;
5086 coding->decoder = decode_coding_charset;
5087 coding->encoder = encode_coding_charset;
3628 coding->common_flags 5088 coding->common_flags
3629 = CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK; 5089 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
3630 } 5090 }
3631 else 5091 else if (EQ (coding_type, Qutf_8))
3632 { 5092 {
3633 coding->common_flags = 0; 5093 coding->detector = detect_coding_utf_8;
3634 coding->eol_type = CODING_EOL_LF; 5094 coding->decoder = decode_coding_utf_8;
5095 coding->encoder = encode_coding_utf_8;
5096 coding->common_flags
5097 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5098 }
5099 else if (EQ (coding_type, Qutf_16))
5100 {
5101 val = AREF (attrs, coding_attr_utf_16_bom);
5102 CODING_UTF_16_BOM (coding) = (CONSP (val) ? utf_16_detect_bom
5103 : EQ (val, Qt) ? utf_16_with_bom
5104 : utf_16_without_bom);
5105 val = AREF (attrs, coding_attr_utf_16_endian);
5106 CODING_UTF_16_ENDIAN (coding) = (EQ (val, Qbig) ? utf_16_big_endian
5107 : utf_16_little_endian);
5108 CODING_UTF_16_SURROGATE (coding) = 0;
5109 coding->detector = detect_coding_utf_16;
5110 coding->decoder = decode_coding_utf_16;
5111 coding->encoder = encode_coding_utf_16;
5112 coding->common_flags
5113 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5114 if (CODING_UTF_16_BOM (coding) == utf_16_detect_bom)
5115 coding->common_flags |= CODING_REQUIRE_DETECTION_MASK;
3635 } 5116 }
3636 5117 else if (EQ (coding_type, Qccl))
3637 coding_type = XVECTOR (coding_spec)->contents[0]; 5118 {
3638 /* Try short cut. */ 5119 coding->detector = detect_coding_ccl;
3639 if (SYMBOLP (coding_type)) 5120 coding->decoder = decode_coding_ccl;
5121 coding->encoder = encode_coding_ccl;
5122 coding->common_flags
5123 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK
5124 | CODING_REQUIRE_FLUSHING_MASK);
5125 }
5126 else if (EQ (coding_type, Qemacs_mule))
3640 { 5127 {
3641 if (EQ (coding_type, Qt)) 5128 coding->detector = detect_coding_emacs_mule;
5129 coding->decoder = decode_coding_emacs_mule;
5130 coding->encoder = encode_coding_emacs_mule;
5131 coding->common_flags
5132 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
5133 if (! NILP (AREF (attrs, coding_attr_emacs_mule_full))
5134 && ! EQ (CODING_ATTR_CHARSET_LIST (attrs), Vemacs_mule_charset_list))
3642 { 5135 {
3643 coding->type = coding_type_undecided; 5136 Lisp_Object tail, safe_charsets;
3644 coding->common_flags |= CODING_REQUIRE_DETECTION_MASK; 5137 int max_charset_id = 0;
5138
5139 for (tail = Vemacs_mule_charset_list; CONSP (tail);
5140 tail = XCDR (tail))
5141 if (max_charset_id < XFASTINT (XCAR (tail)))
5142 max_charset_id = XFASTINT (XCAR (tail));
5143 safe_charsets = Fmake_string (make_number (max_charset_id + 1),
5144 make_number (255));
5145 for (tail = Vemacs_mule_charset_list; CONSP (tail);
5146 tail = XCDR (tail))
5147 SSET (safe_charsets, XFASTINT (XCAR (tail)), 0);
5148 coding->max_charset_id = max_charset_id;
5149 coding->safe_charsets = (char *) SDATA (safe_charsets);
3645 } 5150 }
3646 else
3647 coding->type = coding_type_no_conversion;
3648 /* Initialize this member. Any thing other than
3649 CODING_CATEGORY_IDX_UTF_16_BE and
3650 CODING_CATEGORY_IDX_UTF_16_LE are ok because they have
3651 special treatment in detect_eol. */
3652 coding->category_idx = CODING_CATEGORY_IDX_EMACS_MULE;
3653
3654 return 0;
3655 } 5151 }
3656 5152 else if (EQ (coding_type, Qshift_jis))
3657 /* Get values of coding system properties:
3658 `post-read-conversion', `pre-write-conversion',
3659 `translation-table-for-decode', `translation-table-for-encode'. */
3660 plist = XVECTOR (coding_spec)->contents[3];
3661 /* Pre & post conversion functions should be disabled if
3662 inhibit_eol_conversion is nonzero. This is the case that a code
3663 conversion function is called while those functions are running. */
3664 if (! inhibit_pre_post_conversion)
3665 {
3666 coding->post_read_conversion = Fplist_get (plist, Qpost_read_conversion);
3667 coding->pre_write_conversion = Fplist_get (plist, Qpre_write_conversion);
3668 }
3669 val = Fplist_get (plist, Qtranslation_table_for_decode);
3670 if (SYMBOLP (val))
3671 val = Fget (val, Qtranslation_table_for_decode);
3672 coding->translation_table_for_decode = CHAR_TABLE_P (val) ? val : Qnil;
3673 val = Fplist_get (plist, Qtranslation_table_for_encode);
3674 if (SYMBOLP (val))
3675 val = Fget (val, Qtranslation_table_for_encode);
3676 coding->translation_table_for_encode = CHAR_TABLE_P (val) ? val : Qnil;
3677 val = Fplist_get (plist, Qcoding_category);
3678 if (!NILP (val))
3679 {
3680 val = Fget (val, Qcoding_category_index);
3681 if (INTEGERP (val))
3682 coding->category_idx = XINT (val);
3683 else
3684 goto label_invalid_coding_system;
3685 }
3686 else
3687 goto label_invalid_coding_system;
3688
3689 /* If the coding system has non-nil `composition' property, enable
3690 composition handling. */
3691 val = Fplist_get (plist, Qcomposition);
3692 if (!NILP (val))
3693 coding->composing = COMPOSITION_NO;
3694
3695 /* If the coding system is ascii-incompatible, record it in
3696 common_flags. */
3697 val = Fplist_get (plist, Qascii_incompatible);
3698 if (! NILP (val))
3699 coding->common_flags |= CODING_ASCII_INCOMPATIBLE_MASK;
3700
3701 switch (XFASTINT (coding_type))
3702 { 5153 {
3703 case 0: 5154 coding->detector = detect_coding_sjis;
3704 coding->type = coding_type_emacs_mule; 5155 coding->decoder = decode_coding_sjis;
5156 coding->encoder = encode_coding_sjis;
3705 coding->common_flags 5157 coding->common_flags
3706 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK; 5158 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
3707 if (!NILP (coding->post_read_conversion)) 5159 }
3708 coding->common_flags |= CODING_REQUIRE_DECODING_MASK; 5160 else if (EQ (coding_type, Qbig5))
3709 if (!NILP (coding->pre_write_conversion)) 5161 {
3710 coding->common_flags |= CODING_REQUIRE_ENCODING_MASK; 5162 coding->detector = detect_coding_big5;
3711 break; 5163 coding->decoder = decode_coding_big5;
3712 5164 coding->encoder = encode_coding_big5;
3713 case 1:
3714 coding->type = coding_type_sjis;
3715 coding->common_flags
3716 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
3717 break;
3718
3719 case 2:
3720 coding->type = coding_type_iso2022;
3721 coding->common_flags
3722 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
3723 {
3724 Lisp_Object val, temp;
3725 Lisp_Object *flags;
3726 int i, charset, reg_bits = 0;
3727
3728 val = XVECTOR (coding_spec)->contents[4];
3729
3730 if (!VECTORP (val) || XVECTOR (val)->size != 32)
3731 goto label_invalid_coding_system;
3732
3733 flags = XVECTOR (val)->contents;
3734 coding->flags
3735 = ((NILP (flags[4]) ? 0 : CODING_FLAG_ISO_SHORT_FORM)
3736 | (NILP (flags[5]) ? 0 : CODING_FLAG_ISO_RESET_AT_EOL)
3737 | (NILP (flags[6]) ? 0 : CODING_FLAG_ISO_RESET_AT_CNTL)
3738 | (NILP (flags[7]) ? 0 : CODING_FLAG_ISO_SEVEN_BITS)
3739 | (NILP (flags[8]) ? 0 : CODING_FLAG_ISO_LOCKING_SHIFT)
3740 | (NILP (flags[9]) ? 0 : CODING_FLAG_ISO_SINGLE_SHIFT)
3741 | (NILP (flags[10]) ? 0 : CODING_FLAG_ISO_USE_ROMAN)
3742 | (NILP (flags[11]) ? 0 : CODING_FLAG_ISO_USE_OLDJIS)
3743 | (NILP (flags[12]) ? 0 : CODING_FLAG_ISO_NO_DIRECTION)
3744 | (NILP (flags[13]) ? 0 : CODING_FLAG_ISO_INIT_AT_BOL)
3745 | (NILP (flags[14]) ? 0 : CODING_FLAG_ISO_DESIGNATE_AT_BOL)
3746 | (NILP (flags[15]) ? 0 : CODING_FLAG_ISO_SAFE)
3747 | (NILP (flags[16]) ? 0 : CODING_FLAG_ISO_LATIN_EXTRA)
3748 );
3749
3750 /* Invoke graphic register 0 to plane 0. */
3751 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0;
3752 /* Invoke graphic register 1 to plane 1 if we can use full 8-bit. */
3753 CODING_SPEC_ISO_INVOCATION (coding, 1)
3754 = (coding->flags & CODING_FLAG_ISO_SEVEN_BITS ? -1 : 1);
3755 /* Not single shifting at first. */
3756 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0;
3757 /* Beginning of buffer should also be regarded as bol. */
3758 CODING_SPEC_ISO_BOL (coding) = 1;
3759
3760 for (charset = 0; charset <= MAX_CHARSET; charset++)
3761 CODING_SPEC_ISO_REVISION_NUMBER (coding, charset) = 255;
3762 val = Vcharset_revision_alist;
3763 while (CONSP (val))
3764 {
3765 charset = get_charset_id (Fcar_safe (XCAR (val)));
3766 if (charset >= 0
3767 && (temp = Fcdr_safe (XCAR (val)), INTEGERP (temp))
3768 && (i = XINT (temp), (i >= 0 && (i + '@') < 128)))
3769 CODING_SPEC_ISO_REVISION_NUMBER (coding, charset) = i;
3770 val = XCDR (val);
3771 }
3772
3773 /* Checks FLAGS[REG] (REG = 0, 1, 2 3) and decide designations.
3774 FLAGS[REG] can be one of below:
3775 integer CHARSET: CHARSET occupies register I,
3776 t: designate nothing to REG initially, but can be used
3777 by any charsets,
3778 list of integer, nil, or t: designate the first
3779 element (if integer) to REG initially, the remaining
3780 elements (if integer) is designated to REG on request,
3781 if an element is t, REG can be used by any charsets,
3782 nil: REG is never used. */
3783 for (charset = 0; charset <= MAX_CHARSET; charset++)
3784 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3785 = CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION;
3786 for (i = 0; i < 4; i++)
3787 {
3788 if ((INTEGERP (flags[i])
3789 && (charset = XINT (flags[i]), CHARSET_VALID_P (charset)))
3790 || (charset = get_charset_id (flags[i])) >= 0)
3791 {
3792 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = charset;
3793 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) = i;
3794 }
3795 else if (EQ (flags[i], Qt))
3796 {
3797 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = -1;
3798 reg_bits |= 1 << i;
3799 coding->flags |= CODING_FLAG_ISO_DESIGNATION;
3800 }
3801 else if (CONSP (flags[i]))
3802 {
3803 Lisp_Object tail;
3804 tail = flags[i];
3805
3806 coding->flags |= CODING_FLAG_ISO_DESIGNATION;
3807 if ((INTEGERP (XCAR (tail))
3808 && (charset = XINT (XCAR (tail)),
3809 CHARSET_VALID_P (charset)))
3810 || (charset = get_charset_id (XCAR (tail))) >= 0)
3811 {
3812 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = charset;
3813 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) =i;
3814 }
3815 else
3816 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = -1;
3817 tail = XCDR (tail);
3818 while (CONSP (tail))
3819 {
3820 if ((INTEGERP (XCAR (tail))
3821 && (charset = XINT (XCAR (tail)),
3822 CHARSET_VALID_P (charset)))
3823 || (charset = get_charset_id (XCAR (tail))) >= 0)
3824 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3825 = i;
3826 else if (EQ (XCAR (tail), Qt))
3827 reg_bits |= 1 << i;
3828 tail = XCDR (tail);
3829 }
3830 }
3831 else
3832 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = -1;
3833
3834 CODING_SPEC_ISO_DESIGNATION (coding, i)
3835 = CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i);
3836 }
3837
3838 if (reg_bits && ! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT))
3839 {
3840 /* REG 1 can be used only by locking shift in 7-bit env. */
3841 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS)
3842 reg_bits &= ~2;
3843 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT))
3844 /* Without any shifting, only REG 0 and 1 can be used. */
3845 reg_bits &= 3;
3846 }
3847
3848 if (reg_bits)
3849 for (charset = 0; charset <= MAX_CHARSET; charset++)
3850 {
3851 if (CHARSET_DEFINED_P (charset)
3852 && (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3853 == CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION))
3854 {
3855 /* There exist some default graphic registers to be
3856 used by CHARSET. */
3857
3858 /* We had better avoid designating a charset of
3859 CHARS96 to REG 0 as far as possible. */
3860 if (CHARSET_CHARS (charset) == 96)
3861 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3862 = (reg_bits & 2
3863 ? 1 : (reg_bits & 4 ? 2 : (reg_bits & 8 ? 3 : 0)));
3864 else
3865 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3866 = (reg_bits & 1
3867 ? 0 : (reg_bits & 2 ? 1 : (reg_bits & 4 ? 2 : 3)));
3868 }
3869 }
3870 }
3871 coding->common_flags |= CODING_REQUIRE_FLUSHING_MASK;
3872 coding->spec.iso2022.last_invalid_designation_register = -1;
3873 break;
3874
3875 case 3:
3876 coding->type = coding_type_big5;
3877 coding->common_flags
3878 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
3879 coding->flags
3880 = (NILP (XVECTOR (coding_spec)->contents[4])
3881 ? CODING_FLAG_BIG5_HKU
3882 : CODING_FLAG_BIG5_ETEN);
3883 break;
3884
3885 case 4:
3886 coding->type = coding_type_ccl;
3887 coding->common_flags 5165 coding->common_flags
3888 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK; 5166 |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK);
3889 { 5167 }
3890 val = XVECTOR (coding_spec)->contents[4]; 5168 else /* EQ (coding_type, Qraw_text) */
3891 if (! CONSP (val) 5169 {
3892 || setup_ccl_program (&(coding->spec.ccl.decoder), 5170 coding->detector = NULL;
3893 XCAR (val)) < 0 5171 coding->decoder = decode_coding_raw_text;
3894 || setup_ccl_program (&(coding->spec.ccl.encoder), 5172 coding->encoder = encode_coding_raw_text;
3895 XCDR (val)) < 0) 5173 if (! EQ (eol_type, Qunix))
3896 goto label_invalid_coding_system; 5174 {
3897 5175 coding->common_flags |= CODING_REQUIRE_DECODING_MASK;
3898 bzero (coding->spec.ccl.valid_codes, 256); 5176 if (! VECTORP (eol_type))
3899 val = Fplist_get (plist, Qvalid_codes); 5177 coding->common_flags |= CODING_REQUIRE_ENCODING_MASK;
3900 if (CONSP (val)) 5178 }
3901 {
3902 Lisp_Object this;
3903
3904 for (; CONSP (val); val = XCDR (val))
3905 {
3906 this = XCAR (val);
3907 if (INTEGERP (this)
3908 && XINT (this) >= 0 && XINT (this) < 256)
3909 coding->spec.ccl.valid_codes[XINT (this)] = 1;
3910 else if (CONSP (this)
3911 && INTEGERP (XCAR (this))
3912 && INTEGERP (XCDR (this)))
3913 {
3914 int start = XINT (XCAR (this));
3915 int end = XINT (XCDR (this));
3916
3917 if (start >= 0 && start <= end && end < 256)
3918 while (start <= end)
3919 coding->spec.ccl.valid_codes[start++] = 1;
3920 }
3921 }
3922 }
3923 }
3924 coding->common_flags |= CODING_REQUIRE_FLUSHING_MASK;
3925 coding->spec.ccl.cr_carryover = 0;
3926 coding->spec.ccl.eight_bit_carryover[0] = 0;
3927 break;
3928
3929 case 5:
3930 coding->type = coding_type_raw_text;
3931 break;
3932 5179
3933 default:
3934 goto label_invalid_coding_system;
3935 } 5180 }
3936 return 0;
3937 5181
3938 label_invalid_coding_system: 5182 return;
3939 coding->type = coding_type_no_conversion;
3940 coding->category_idx = CODING_CATEGORY_IDX_BINARY;
3941 coding->common_flags = 0;
3942 coding->eol_type = CODING_EOL_UNDECIDED;
3943 coding->pre_write_conversion = coding->post_read_conversion = Qnil;
3944 return NILP (coding_system) ? 0 : -1;
3945} 5183}
3946 5184
3947/* Free memory blocks allocated for storing composition information. */ 5185/* Return a list of charsets supported by CODING. */
3948 5186
3949void 5187Lisp_Object
3950coding_free_composition_data (coding) 5188coding_charset_list (coding)
3951 struct coding_system *coding; 5189 struct coding_system *coding;
3952{ 5190{
3953 struct composition_data *cmp_data = coding->cmp_data, *next; 5191 Lisp_Object attrs, charset_list;
3954 5192
3955 if (!cmp_data) 5193 CODING_GET_INFO (coding, attrs, charset_list);
3956 return; 5194 if (EQ (CODING_ATTR_TYPE (attrs), Qiso_2022))
3957 /* Memory blocks are chained. At first, rewind to the first, then, 5195 {
3958 free blocks one by one. */ 5196 int flags = XINT (AREF (attrs, coding_attr_iso_flags));
3959 while (cmp_data->prev) 5197
3960 cmp_data = cmp_data->prev; 5198 if (flags & CODING_ISO_FLAG_FULL_SUPPORT)
3961 while (cmp_data) 5199 charset_list = Viso_2022_charset_list;
5200 }
5201 else if (EQ (CODING_ATTR_TYPE (attrs), Qemacs_mule))
3962 { 5202 {
3963 next = cmp_data->next; 5203 charset_list = Vemacs_mule_charset_list;
3964 xfree (cmp_data);
3965 cmp_data = next;
3966 } 5204 }
3967 coding->cmp_data = NULL; 5205 return charset_list;
3968} 5206}
3969 5207
3970/* Set `char_offset' member of all memory blocks pointed by
3971 coding->cmp_data to POS. */
3972 5208
3973void 5209/* Return raw-text or one of its subsidiaries that has the same
3974coding_adjust_composition_offset (coding, pos) 5210 eol_type as CODING-SYSTEM. */
3975 struct coding_system *coding; 5211
3976 int pos; 5212Lisp_Object
5213raw_text_coding_system (coding_system)
5214 Lisp_Object coding_system;
3977{ 5215{
3978 struct composition_data *cmp_data; 5216 Lisp_Object spec, attrs;
5217 Lisp_Object eol_type, raw_text_eol_type;
5218
5219 if (NILP (coding_system))
5220 return Qraw_text;
5221 spec = CODING_SYSTEM_SPEC (coding_system);
5222 attrs = AREF (spec, 0);
5223
5224 if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text))
5225 return coding_system;
3979 5226
3980 for (cmp_data = coding->cmp_data; cmp_data; cmp_data = cmp_data->next) 5227 eol_type = AREF (spec, 2);
3981 cmp_data->char_offset = pos; 5228 if (VECTORP (eol_type))
5229 return Qraw_text;
5230 spec = CODING_SYSTEM_SPEC (Qraw_text);
5231 raw_text_eol_type = AREF (spec, 2);
5232 return (EQ (eol_type, Qunix) ? AREF (raw_text_eol_type, 0)
5233 : EQ (eol_type, Qdos) ? AREF (raw_text_eol_type, 1)
5234 : AREF (raw_text_eol_type, 2));
3982} 5235}
3983 5236
3984/* Setup raw-text or one of its subsidiaries in the structure
3985 coding_system CODING according to the already setup value eol_type
3986 in CODING. CODING should be setup for some coding system in
3987 advance. */
3988 5237
3989void 5238/* If CODING_SYSTEM doesn't specify end-of-line format but PARENT
3990setup_raw_text_coding_system (coding) 5239 does, return one of the subsidiary that has the same eol-spec as
3991 struct coding_system *coding; 5240 PARENT. Otherwise, return CODING_SYSTEM. If PARENT is nil,
5241 inherit end-of-line format from the system's setting
5242 (system_eol_type). */
5243
5244Lisp_Object
5245coding_inherit_eol_type (coding_system, parent)
5246 Lisp_Object coding_system, parent;
3992{ 5247{
3993 if (coding->type != coding_type_raw_text) 5248 Lisp_Object spec, eol_type;
5249
5250 if (NILP (coding_system))
5251 coding_system = Qraw_text;
5252 spec = CODING_SYSTEM_SPEC (coding_system);
5253 eol_type = AREF (spec, 2);
5254 if (VECTORP (eol_type))
3994 { 5255 {
3995 coding->symbol = Qraw_text; 5256 Lisp_Object parent_eol_type;
3996 coding->type = coding_type_raw_text; 5257
3997 if (coding->eol_type != CODING_EOL_UNDECIDED) 5258 if (! NILP (parent))
3998 { 5259 {
3999 Lisp_Object subsidiaries; 5260 Lisp_Object parent_spec;
4000 subsidiaries = Fget (Qraw_text, Qeol_type);
4001 5261
4002 if (VECTORP (subsidiaries) 5262 parent_spec = CODING_SYSTEM_SPEC (parent);
4003 && XVECTOR (subsidiaries)->size == 3) 5263 parent_eol_type = AREF (parent_spec, 2);
4004 coding->symbol
4005 = XVECTOR (subsidiaries)->contents[coding->eol_type];
4006 } 5264 }
4007 setup_coding_system (coding->symbol, coding); 5265 else
4008 } 5266 parent_eol_type = system_eol_type;
4009 return; 5267 if (EQ (parent_eol_type, Qunix))
5268 coding_system = AREF (eol_type, 0);
5269 else if (EQ (parent_eol_type, Qdos))
5270 coding_system = AREF (eol_type, 1);
5271 else if (EQ (parent_eol_type, Qmac))
5272 coding_system = AREF (eol_type, 2);
5273 }
5274 return coding_system;
4010} 5275}
4011 5276
4012/* Emacs has a mechanism to automatically detect a coding system if it 5277/* Emacs has a mechanism to automatically detect a coding system if it
@@ -4059,14 +5324,14 @@ setup_raw_text_coding_system (coding)
4059 o coding-category-iso-7-else 5324 o coding-category-iso-7-else
4060 5325
4061 The category for a coding system which has the same code range 5326 The category for a coding system which has the same code range
4062 as ISO2022 of 7-bit environment but uses locking shift or 5327 as ISO2022 of 7-bit environemnt but uses locking shift or
4063 single shift functions. Assigned the coding-system (Lisp 5328 single shift functions. Assigned the coding-system (Lisp
4064 symbol) `iso-2022-7bit-lock' by default. 5329 symbol) `iso-2022-7bit-lock' by default.
4065 5330
4066 o coding-category-iso-8-else 5331 o coding-category-iso-8-else
4067 5332
4068 The category for a coding system which has the same code range 5333 The category for a coding system which has the same code range
4069 as ISO2022 of 8-bit environment but uses locking shift or 5334 as ISO2022 of 8-bit environemnt but uses locking shift or
4070 single shift functions. Assigned the coding-system (Lisp 5335 single shift functions. Assigned the coding-system (Lisp
4071 symbol) `iso-2022-8bit-ss2' by default. 5336 symbol) `iso-2022-8bit-ss2' by default.
4072 5337
@@ -4109,2479 +5374,1856 @@ setup_raw_text_coding_system (coding)
4109 `no-conversion' by default. 5374 `no-conversion' by default.
4110 5375
4111 Each of them is a Lisp symbol and the value is an actual 5376 Each of them is a Lisp symbol and the value is an actual
4112 `coding-system' (this is also a Lisp symbol) assigned by a user. 5377 `coding-system's (this is also a Lisp symbol) assigned by a user.
4113 What Emacs does actually is to detect a category of coding system. 5378 What Emacs does actually is to detect a category of coding system.
4114 Then, it uses a `coding-system' assigned to it. If Emacs can't 5379 Then, it uses a `coding-system' assigned to it. If Emacs can't
4115 decide a single possible category, it selects a category of the 5380 decide only one possible category, it selects a category of the
4116 highest priority. Priorities of categories are also specified by a 5381 highest priority. Priorities of categories are also specified by a
4117 user in a Lisp variable `coding-category-list'. 5382 user in a Lisp variable `coding-category-list'.
4118 5383
4119*/ 5384*/
4120 5385
4121static 5386#define EOL_SEEN_NONE 0
4122int ascii_skip_code[256]; 5387#define EOL_SEEN_LF 1
4123 5388#define EOL_SEEN_CR 2
4124/* Detect how a text of length SRC_BYTES pointed by SOURCE is encoded. 5389#define EOL_SEEN_CRLF 4
4125 If it detects possible coding systems, return an integer in which
4126 appropriate flag bits are set. Flag bits are defined by macros
4127 CODING_CATEGORY_MASK_XXX in `coding.h'. If PRIORITIES is non-NULL,
4128 it should point the table `coding_priorities'. In that case, only
4129 the flag bit for a coding system of the highest priority is set in
4130 the returned value. If MULTIBYTEP is nonzero, 8-bit codes of the
4131 range 0x80..0x9F are in multibyte form.
4132
4133 How many ASCII characters are at the head is returned as *SKIP. */
4134
4135static int
4136detect_coding_mask (source, src_bytes, priorities, skip, multibytep)
4137 unsigned char *source;
4138 int src_bytes, *priorities, *skip;
4139 int multibytep;
4140{
4141 register unsigned char c;
4142 unsigned char *src = source, *src_end = source + src_bytes;
4143 unsigned int mask, utf16_examined_p, iso2022_examined_p;
4144 int i;
4145 int null_byte_found;
4146 int latin_extra_code_state = 1;
4147
4148 /* At first, skip all ASCII characters and control characters except
4149 for three ISO2022 specific control characters. */
4150 ascii_skip_code[ISO_CODE_SO] = 0;
4151 ascii_skip_code[ISO_CODE_SI] = 0;
4152 ascii_skip_code[ISO_CODE_ESC] = 0;
4153
4154 label_loop_detect_coding:
4155 null_byte_found = 0;
4156 /* We stop this loop before the last byte because it may be a NULL
4157 anchor byte. */
4158 while (src < src_end - 1 && ascii_skip_code[*src])
4159 null_byte_found |= (! *src++);
4160 if (ascii_skip_code[*src])
4161 src++;
4162 else if (! null_byte_found)
4163 {
4164 unsigned char *p = src + 1;
4165 while (p < src_end - 1)
4166 null_byte_found |= (! *p++);
4167 }
4168 *skip = src - source;
4169
4170 if (src >= src_end)
4171 /* We found nothing other than ASCII (and NULL byte). There's
4172 nothing to do. */
4173 return 0;
4174
4175 c = *src;
4176 /* The text seems to be encoded in some multilingual coding system.
4177 Now, try to find in which coding system the text is encoded. */
4178 if (! null_byte_found && c < 0x80)
4179 {
4180 /* i.e. (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) */
4181 /* C is an ISO2022 specific control code of C0. */
4182 latin_extra_code_state = 1;
4183 mask = detect_coding_iso2022 (src, src_end, multibytep,
4184 &latin_extra_code_state);
4185 if (mask == 0)
4186 {
4187 /* No valid ISO2022 code follows C. Try again. */
4188 src++;
4189 if (c == ISO_CODE_ESC)
4190 ascii_skip_code[ISO_CODE_ESC] = 1;
4191 else
4192 ascii_skip_code[ISO_CODE_SO] = ascii_skip_code[ISO_CODE_SI] = 1;
4193 goto label_loop_detect_coding;
4194 }
4195 if (priorities)
4196 {
4197 for (i = 0; i < CODING_CATEGORY_IDX_MAX; i++)
4198 {
4199 if (mask & priorities[i])
4200 return priorities[i];
4201 }
4202 return CODING_CATEGORY_MASK_RAW_TEXT;
4203 }
4204 }
4205 else
4206 {
4207 int try;
4208
4209 if (multibytep && c == LEADING_CODE_8_BIT_CONTROL)
4210 c = src[1] - 0x20;
4211
4212 if (null_byte_found)
4213 {
4214 try = (CODING_CATEGORY_MASK_UTF_16_BE
4215 | CODING_CATEGORY_MASK_UTF_16_LE);
4216 }
4217 else if (c < 0xA0)
4218 {
4219 /* C is the first byte of SJIS character code,
4220 or a leading-code of Emacs' internal format (emacs-mule),
4221 or the first byte of UTF-16. */
4222 try = (CODING_CATEGORY_MASK_SJIS
4223 | CODING_CATEGORY_MASK_EMACS_MULE
4224 | CODING_CATEGORY_MASK_UTF_16_BE
4225 | CODING_CATEGORY_MASK_UTF_16_LE);
4226
4227 /* Or, if C is a special latin extra code,
4228 or is an ISO2022 specific control code of C1 (SS2 or SS3),
4229 or is an ISO2022 control-sequence-introducer (CSI),
4230 we should also consider the possibility of ISO2022 codings. */
4231 if ((latin_extra_code_state
4232 && VECTORP (Vlatin_extra_code_table)
4233 && !NILP (XVECTOR (Vlatin_extra_code_table)->contents[c]))
4234 || (c == ISO_CODE_SS2 || c == ISO_CODE_SS3)
4235 || (c == ISO_CODE_CSI
4236 && (src < src_end
4237 && (*src == ']'
4238 || ((*src == '0' || *src == '1' || *src == '2')
4239 && src + 1 < src_end
4240 && src[1] == ']')))))
4241 try |= (CODING_CATEGORY_MASK_ISO_8_ELSE
4242 | CODING_CATEGORY_MASK_ISO_8BIT);
4243 }
4244 else
4245 /* C is a character of ISO2022 in graphic plane right,
4246 or a SJIS's 1-byte character code (i.e. JISX0201),
4247 or the first byte of BIG5's 2-byte code,
4248 or the first byte of UTF-8/16. */
4249 try = (CODING_CATEGORY_MASK_ISO_8_ELSE
4250 | CODING_CATEGORY_MASK_ISO_8BIT
4251 | CODING_CATEGORY_MASK_SJIS
4252 | CODING_CATEGORY_MASK_BIG5
4253 | CODING_CATEGORY_MASK_UTF_8
4254 | CODING_CATEGORY_MASK_UTF_16_BE
4255 | CODING_CATEGORY_MASK_UTF_16_LE);
4256
4257 /* Or, we may have to consider the possibility of CCL. */
4258 if (! null_byte_found
4259 && coding_system_table[CODING_CATEGORY_IDX_CCL]
4260 && (coding_system_table[CODING_CATEGORY_IDX_CCL]
4261 ->spec.ccl.valid_codes)[c])
4262 try |= CODING_CATEGORY_MASK_CCL;
4263
4264 mask = 0;
4265 if (priorities)
4266 {
4267 /* At first try detection with Latin extra codes not-allowed.
4268 If no proper coding system is found because of Latin extra
4269 codes, try detection with Latin extra codes allowed. */
4270 latin_extra_code_state = 0;
4271 label_retry:
4272 utf16_examined_p = iso2022_examined_p = 0;
4273 for (i = 0; i < CODING_CATEGORY_IDX_MAX; i++)
4274 {
4275 if (!iso2022_examined_p
4276 && (priorities[i] & try & CODING_CATEGORY_MASK_ISO))
4277 {
4278 mask |= detect_coding_iso2022 (src, src_end, multibytep,
4279 &latin_extra_code_state);
4280 iso2022_examined_p = 1;
4281 }
4282 else if (priorities[i] & try & CODING_CATEGORY_MASK_SJIS)
4283 mask |= detect_coding_sjis (src, src_end, multibytep);
4284 else if (priorities[i] & try & CODING_CATEGORY_MASK_UTF_8)
4285 mask |= detect_coding_utf_8 (src, src_end, multibytep);
4286 else if (!utf16_examined_p
4287 && (priorities[i] & try &
4288 CODING_CATEGORY_MASK_UTF_16_BE_LE))
4289 {
4290 mask |= detect_coding_utf_16 (src, src_end, multibytep);
4291 utf16_examined_p = 1;
4292 }
4293 else if (priorities[i] & try & CODING_CATEGORY_MASK_BIG5)
4294 mask |= detect_coding_big5 (src, src_end, multibytep);
4295 else if (priorities[i] & try & CODING_CATEGORY_MASK_EMACS_MULE)
4296 mask |= detect_coding_emacs_mule (src, src_end, multibytep);
4297 else if (priorities[i] & try & CODING_CATEGORY_MASK_CCL)
4298 mask |= detect_coding_ccl (src, src_end, multibytep);
4299 else if (priorities[i] & CODING_CATEGORY_MASK_RAW_TEXT)
4300 {
4301 if (latin_extra_code_state == 1)
4302 {
4303 /* Detection of ISO-2022 based coding system
4304 failed because of Latin extra codes. Before
4305 falling back to raw-text, try again with
4306 Latin extra codes allowed. */
4307 latin_extra_code_state = 2;
4308 try = (mask | CODING_CATEGORY_MASK_ISO_8_ELSE
4309 | CODING_CATEGORY_MASK_ISO_8BIT);
4310 goto label_retry;
4311 }
4312 mask |= CODING_CATEGORY_MASK_RAW_TEXT;
4313 }
4314 else if (priorities[i] & CODING_CATEGORY_MASK_BINARY)
4315 {
4316 if (latin_extra_code_state == 1)
4317 {
4318 /* See the above comment. */
4319 latin_extra_code_state = 2;
4320 try = (mask | CODING_CATEGORY_MASK_ISO_8_ELSE
4321 | CODING_CATEGORY_MASK_ISO_8BIT);
4322 goto label_retry;
4323 }
4324 mask |= CODING_CATEGORY_MASK_BINARY;
4325 }
4326 if (mask & priorities[i])
4327 return priorities[i];
4328 }
4329 return CODING_CATEGORY_MASK_RAW_TEXT;
4330 }
4331 if (try & CODING_CATEGORY_MASK_ISO)
4332 mask |= detect_coding_iso2022 (src, src_end, multibytep,
4333 &latin_extra_code_state);
4334 if (try & CODING_CATEGORY_MASK_SJIS)
4335 mask |= detect_coding_sjis (src, src_end, multibytep);
4336 if (try & CODING_CATEGORY_MASK_BIG5)
4337 mask |= detect_coding_big5 (src, src_end, multibytep);
4338 if (try & CODING_CATEGORY_MASK_UTF_8)
4339 mask |= detect_coding_utf_8 (src, src_end, multibytep);
4340 if (try & CODING_CATEGORY_MASK_UTF_16_BE_LE)
4341 mask |= detect_coding_utf_16 (src, src_end, multibytep);
4342 if (try & CODING_CATEGORY_MASK_EMACS_MULE)
4343 mask |= detect_coding_emacs_mule (src, src_end, multibytep);
4344 if (try & CODING_CATEGORY_MASK_CCL)
4345 mask |= detect_coding_ccl (src, src_end, multibytep);
4346 }
4347 return (mask | CODING_CATEGORY_MASK_RAW_TEXT | CODING_CATEGORY_MASK_BINARY);
4348}
4349
4350/* Detect how a text of length SRC_BYTES pointed by SRC is encoded.
4351 The information of the detected coding system is set in CODING. */
4352
4353void
4354detect_coding (coding, src, src_bytes)
4355 struct coding_system *coding;
4356 const unsigned char *src;
4357 int src_bytes;
4358{
4359 unsigned int idx;
4360 int skip, mask;
4361 Lisp_Object val;
4362
4363 val = Vcoding_category_list;
4364 mask = detect_coding_mask (src, src_bytes, coding_priorities, &skip,
4365 coding->src_multibyte);
4366 coding->heading_ascii = skip;
4367
4368 if (!mask) return;
4369
4370 /* We found a single coding system of the highest priority in MASK. */
4371 idx = 0;
4372 while (mask && ! (mask & 1)) mask >>= 1, idx++;
4373 if (! mask)
4374 idx = CODING_CATEGORY_IDX_RAW_TEXT;
4375
4376 val = find_symbol_value (XVECTOR (Vcoding_category_table)->contents[idx]);
4377
4378 if (coding->eol_type != CODING_EOL_UNDECIDED)
4379 {
4380 Lisp_Object tmp;
4381
4382 tmp = Fget (val, Qeol_type);
4383 if (VECTORP (tmp))
4384 val = XVECTOR (tmp)->contents[coding->eol_type];
4385 }
4386
4387 /* Setup this new coding system while preserving some slots. */
4388 {
4389 int src_multibyte = coding->src_multibyte;
4390 int dst_multibyte = coding->dst_multibyte;
4391
4392 setup_coding_system (val, coding);
4393 coding->src_multibyte = src_multibyte;
4394 coding->dst_multibyte = dst_multibyte;
4395 coding->heading_ascii = skip;
4396 }
4397}
4398 5390
4399/* Detect how end-of-line of a text of length SRC_BYTES pointed by 5391/* Detect how end-of-line of a text of length SRC_BYTES pointed by
4400 SOURCE is encoded. Return one of CODING_EOL_LF, CODING_EOL_CRLF, 5392 SOURCE is encoded. If CATEGORY is one of
4401 CODING_EOL_CR, and CODING_EOL_UNDECIDED. 5393 coding_category_utf_16_XXXX, assume that CR and LF are encoded by
5394 two-byte, else they are encoded by one-byte.
4402 5395
4403 How many non-eol characters are at the head is returned as *SKIP. */ 5396 Return one of EOL_SEEN_XXX. */
4404 5397
4405#define MAX_EOL_CHECK_COUNT 3 5398#define MAX_EOL_CHECK_COUNT 3
4406 5399
4407static int 5400static int
4408detect_eol_type (source, src_bytes, skip) 5401detect_eol (source, src_bytes, category)
4409 const unsigned char *source; 5402 const unsigned char *source;
4410 int src_bytes, *skip; 5403 EMACS_INT src_bytes;
5404 enum coding_category category;
4411{ 5405{
4412 const unsigned char *src = source, *src_end = src + src_bytes; 5406 const unsigned char *src = source, *src_end = src + src_bytes;
4413 unsigned char c; 5407 unsigned char c;
4414 int total = 0; /* How many end-of-lines are found so far. */ 5408 int total = 0;
4415 int eol_type = CODING_EOL_UNDECIDED; 5409 int eol_seen = EOL_SEEN_NONE;
4416 int this_eol_type;
4417 5410
4418 *skip = 0; 5411 if ((1 << category) & CATEGORY_MASK_UTF_16)
4419
4420 while (src < src_end && total < MAX_EOL_CHECK_COUNT)
4421 { 5412 {
4422 c = *src++; 5413 int msb, lsb;
4423 if (c == '\n' || c == '\r') 5414
4424 { 5415 msb = category == (coding_category_utf_16_le
4425 if (*skip == 0) 5416 | coding_category_utf_16_le_nosig);
4426 *skip = src - 1 - source; 5417 lsb = 1 - msb;
4427 total++;
4428 if (c == '\n')
4429 this_eol_type = CODING_EOL_LF;
4430 else if (src >= src_end || *src != '\n')
4431 this_eol_type = CODING_EOL_CR;
4432 else
4433 this_eol_type = CODING_EOL_CRLF, src++;
4434 5418
4435 if (eol_type == CODING_EOL_UNDECIDED) 5419 while (src + 1 < src_end)
4436 /* This is the first end-of-line. */ 5420 {
4437 eol_type = this_eol_type; 5421 c = src[lsb];
4438 else if (eol_type != this_eol_type) 5422 if (src[msb] == 0 && (c == '\n' || c == '\r'))
4439 { 5423 {
4440 /* The found type is different from what found before. */ 5424 int this_eol;
4441 eol_type = CODING_EOL_INCONSISTENT; 5425
4442 break; 5426 if (c == '\n')
5427 this_eol = EOL_SEEN_LF;
5428 else if (src + 3 >= src_end
5429 || src[msb + 2] != 0
5430 || src[lsb + 2] != '\n')
5431 this_eol = EOL_SEEN_CR;
5432 else
5433 this_eol = EOL_SEEN_CRLF;
5434
5435 if (eol_seen == EOL_SEEN_NONE)
5436 /* This is the first end-of-line. */
5437 eol_seen = this_eol;
5438 else if (eol_seen != this_eol)
5439 {
5440 /* The found type is different from what found before. */
5441 eol_seen = EOL_SEEN_LF;
5442 break;
5443 }
5444 if (++total == MAX_EOL_CHECK_COUNT)
5445 break;
4443 } 5446 }
5447 src += 2;
4444 } 5448 }
4445 } 5449 }
4446
4447 if (*skip == 0)
4448 *skip = src_end - source;
4449 return eol_type;
4450}
4451
4452/* Like detect_eol_type, but detect EOL type in 2-octet
4453 big-endian/little-endian format for coding systems utf-16-be and
4454 utf-16-le. */
4455
4456static int
4457detect_eol_type_in_2_octet_form (source, src_bytes, skip, big_endian_p)
4458 const unsigned char *source;
4459 int src_bytes, *skip, big_endian_p;
4460{
4461 const unsigned char *src = source, *src_end = src + src_bytes;
4462 unsigned int c1, c2;
4463 int total = 0; /* How many end-of-lines are found so far. */
4464 int eol_type = CODING_EOL_UNDECIDED;
4465 int this_eol_type;
4466 int msb, lsb;
4467
4468 if (big_endian_p)
4469 msb = 0, lsb = 1;
4470 else 5450 else
4471 msb = 1, lsb = 0;
4472
4473 *skip = 0;
4474
4475 while ((src + 1) < src_end && total < MAX_EOL_CHECK_COUNT)
4476 { 5451 {
4477 c1 = (src[msb] << 8) | (src[lsb]); 5452 while (src < src_end)
4478 src += 2;
4479
4480 if (c1 == '\n' || c1 == '\r')
4481 { 5453 {
4482 if (*skip == 0) 5454 c = *src++;
4483 *skip = src - 2 - source; 5455 if (c == '\n' || c == '\r')
4484 total++;
4485 if (c1 == '\n')
4486 {
4487 this_eol_type = CODING_EOL_LF;
4488 }
4489 else
4490 { 5456 {
4491 if ((src + 1) >= src_end) 5457 int this_eol;
4492 { 5458
4493 this_eol_type = CODING_EOL_CR; 5459 if (c == '\n')
4494 } 5460 this_eol = EOL_SEEN_LF;
5461 else if (src >= src_end || *src != '\n')
5462 this_eol = EOL_SEEN_CR;
4495 else 5463 else
5464 this_eol = EOL_SEEN_CRLF, src++;
5465
5466 if (eol_seen == EOL_SEEN_NONE)
5467 /* This is the first end-of-line. */
5468 eol_seen = this_eol;
5469 else if (eol_seen != this_eol)
4496 { 5470 {
4497 c2 = (src[msb] << 8) | (src[lsb]); 5471 /* The found type is different from what found before. */
4498 if (c2 == '\n') 5472 eol_seen = EOL_SEEN_LF;
4499 this_eol_type = CODING_EOL_CRLF, src += 2; 5473 break;
4500 else
4501 this_eol_type = CODING_EOL_CR;
4502 } 5474 }
4503 } 5475 if (++total == MAX_EOL_CHECK_COUNT)
4504 5476 break;
4505 if (eol_type == CODING_EOL_UNDECIDED)
4506 /* This is the first end-of-line. */
4507 eol_type = this_eol_type;
4508 else if (eol_type != this_eol_type)
4509 {
4510 /* The found type is different from what found before. */
4511 eol_type = CODING_EOL_INCONSISTENT;
4512 break;
4513 } 5477 }
4514 } 5478 }
4515 } 5479 }
4516 5480 return eol_seen;
4517 if (*skip == 0)
4518 *skip = src_end - source;
4519 return eol_type;
4520} 5481}
4521 5482
4522/* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC
4523 is encoded. If it detects an appropriate format of end-of-line, it
4524 sets the information in *CODING. */
4525 5483
4526void 5484static Lisp_Object
4527detect_eol (coding, src, src_bytes) 5485adjust_coding_eol_type (coding, eol_seen)
4528 struct coding_system *coding; 5486 struct coding_system *coding;
4529 const unsigned char *src; 5487 int eol_seen;
4530 int src_bytes;
4531{ 5488{
4532 Lisp_Object val; 5489 Lisp_Object eol_type;
4533 int skip;
4534 int eol_type;
4535 5490
4536 switch (coding->category_idx) 5491 eol_type = CODING_ID_EOL_TYPE (coding->id);
5492 if (eol_seen & EOL_SEEN_LF)
4537 { 5493 {
4538 case CODING_CATEGORY_IDX_UTF_16_BE: 5494 coding->id = CODING_SYSTEM_ID (AREF (eol_type, 0));
4539 eol_type = detect_eol_type_in_2_octet_form (src, src_bytes, &skip, 1); 5495 eol_type = Qunix;
4540 break;
4541 case CODING_CATEGORY_IDX_UTF_16_LE:
4542 eol_type = detect_eol_type_in_2_octet_form (src, src_bytes, &skip, 0);
4543 break;
4544 default:
4545 eol_type = detect_eol_type (src, src_bytes, &skip);
4546 break;
4547 } 5496 }
4548 5497 else if (eol_seen & EOL_SEEN_CRLF)
4549 if (coding->heading_ascii > skip)
4550 coding->heading_ascii = skip;
4551 else
4552 skip = coding->heading_ascii;
4553
4554 if (eol_type == CODING_EOL_UNDECIDED)
4555 return;
4556 if (eol_type == CODING_EOL_INCONSISTENT)
4557 { 5498 {
4558#if 0 5499 coding->id = CODING_SYSTEM_ID (AREF (eol_type, 1));
4559 /* This code is suppressed until we find a better way to 5500 eol_type = Qdos;
4560 distinguish raw text file and binary file. */
4561
4562 /* If we have already detected that the coding is raw-text, the
4563 coding should actually be no-conversion. */
4564 if (coding->type == coding_type_raw_text)
4565 {
4566 setup_coding_system (Qno_conversion, coding);
4567 return;
4568 }
4569 /* Else, let's decode only text code anyway. */
4570#endif /* 0 */
4571 eol_type = CODING_EOL_LF;
4572 } 5501 }
4573 5502 else if (eol_seen & EOL_SEEN_CR)
4574 val = Fget (coding->symbol, Qeol_type);
4575 if (VECTORP (val) && XVECTOR (val)->size == 3)
4576 { 5503 {
4577 int src_multibyte = coding->src_multibyte; 5504 coding->id = CODING_SYSTEM_ID (AREF (eol_type, 2));
4578 int dst_multibyte = coding->dst_multibyte; 5505 eol_type = Qmac;
4579 struct composition_data *cmp_data = coding->cmp_data;
4580
4581 setup_coding_system (XVECTOR (val)->contents[eol_type], coding);
4582 coding->src_multibyte = src_multibyte;
4583 coding->dst_multibyte = dst_multibyte;
4584 coding->heading_ascii = skip;
4585 coding->cmp_data = cmp_data;
4586 } 5506 }
5507 return eol_type;
4587} 5508}
4588 5509
4589#define CONVERSION_BUFFER_EXTRA_ROOM 256 5510/* Detect how a text specified in CODING is encoded. If a coding
4590 5511 system is detected, update fields of CODING by the detected coding
4591#define DECODING_BUFFER_MAG(coding) \ 5512 system. */
4592 (coding->type == coding_type_iso2022 \
4593 ? 3 \
4594 : (coding->type == coding_type_ccl \
4595 ? coding->spec.ccl.decoder.buf_magnification \
4596 : 2))
4597
4598/* Return maximum size (bytes) of a buffer enough for decoding
4599 SRC_BYTES of text encoded in CODING. */
4600 5513
4601int 5514void
4602decoding_buffer_size (coding, src_bytes) 5515detect_coding (coding)
4603 struct coding_system *coding; 5516 struct coding_system *coding;
4604 int src_bytes;
4605{ 5517{
4606 return (src_bytes * DECODING_BUFFER_MAG (coding) 5518 const unsigned char *src, *src_end;
4607 + CONVERSION_BUFFER_EXTRA_ROOM);
4608}
4609 5519
4610/* Return maximum size (bytes) of a buffer enough for encoding 5520 coding->consumed = coding->consumed_char = 0;
4611 SRC_BYTES of text to CODING. */ 5521 coding->produced = coding->produced_char = 0;
5522 coding_set_source (coding);
4612 5523
4613int 5524 src_end = coding->source + coding->src_bytes;
4614encoding_buffer_size (coding, src_bytes)
4615 struct coding_system *coding;
4616 int src_bytes;
4617{
4618 int magnification;
4619 5525
4620 if (coding->type == coding_type_ccl) 5526 /* If we have not yet decided the text encoding type, detect it
5527 now. */
5528 if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding->id)), Qundecided))
4621 { 5529 {
4622 magnification = coding->spec.ccl.encoder.buf_magnification; 5530 int c, i;
4623 if (coding->eol_type == CODING_EOL_CRLF) 5531 struct coding_detection_info detect_info;
4624 magnification *= 2;
4625 }
4626 else if (CODING_REQUIRE_ENCODING (coding))
4627 magnification = 3;
4628 else
4629 magnification = 1;
4630
4631 return (src_bytes * magnification + CONVERSION_BUFFER_EXTRA_ROOM);
4632}
4633 5532
4634/* Working buffer for code conversion. */ 5533 detect_info.checked = detect_info.found = detect_info.rejected = 0;
4635struct conversion_buffer 5534 for (i = 0, src = coding->source; src < src_end; i++, src++)
4636{ 5535 {
4637 int size; /* size of data. */ 5536 c = *src;
4638 int on_stack; /* 1 if allocated by alloca. */ 5537 if (c & 0x80)
4639 unsigned char *data; 5538 break;
4640}; 5539 if (c < 0x20
5540 && (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)
5541 && ! inhibit_iso_escape_detection
5542 && ! detect_info.checked)
5543 {
5544 coding->head_ascii = src - (coding->source + coding->consumed);
5545 if (detect_coding_iso_2022 (coding, &detect_info))
5546 {
5547 /* We have scanned the whole data. */
5548 if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE))
5549 /* We didn't find an 8-bit code. */
5550 src = src_end;
5551 break;
5552 }
5553 }
5554 }
5555 coding->head_ascii = src - (coding->source + coding->consumed);
4641 5556
4642/* Allocate LEN bytes of memory for BUF (struct conversion_buffer). */ 5557 if (coding->head_ascii < coding->src_bytes
4643#define allocate_conversion_buffer(buf, len) \ 5558 || detect_info.found)
4644 do { \ 5559 {
4645 if (len < MAX_ALLOCA) \ 5560 enum coding_category category;
4646 { \ 5561 struct coding_system *this;
4647 buf.data = (unsigned char *) alloca (len); \
4648 buf.on_stack = 1; \
4649 } \
4650 else \
4651 { \
4652 buf.data = (unsigned char *) xmalloc (len); \
4653 buf.on_stack = 0; \
4654 } \
4655 buf.size = len; \
4656 } while (0)
4657 5562
4658/* Double the allocated memory for *BUF. */ 5563 if (coding->head_ascii == coding->src_bytes)
4659static void 5564 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
4660extend_conversion_buffer (buf) 5565 for (i = 0; i < coding_category_raw_text; i++)
4661 struct conversion_buffer *buf; 5566 {
4662{ 5567 category = coding_priorities[i];
4663 if (buf->on_stack) 5568 this = coding_categories + category;
4664 { 5569 if (detect_info.found & (1 << category))
4665 unsigned char *save = buf->data; 5570 break;
4666 buf->data = (unsigned char *) xmalloc (buf->size * 2); 5571 }
4667 bcopy (save, buf->data, buf->size); 5572 else
4668 buf->on_stack = 0; 5573 for (i = 0; i < coding_category_raw_text; i++)
5574 {
5575 category = coding_priorities[i];
5576 this = coding_categories + category;
5577 if (this->id < 0)
5578 {
5579 /* No coding system of this category is defined. */
5580 detect_info.rejected |= (1 << category);
5581 }
5582 else if (category >= coding_category_raw_text)
5583 continue;
5584 else if (detect_info.checked & (1 << category))
5585 {
5586 if (detect_info.found & (1 << category))
5587 break;
5588 }
5589 else if ((*(this->detector)) (coding, &detect_info)
5590 && detect_info.found & (1 << category))
5591 {
5592 if (category == coding_category_utf_16_auto)
5593 {
5594 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
5595 category = coding_category_utf_16_le;
5596 else
5597 category = coding_category_utf_16_be;
5598 }
5599 break;
5600 }
5601 }
5602
5603 if (i < coding_category_raw_text)
5604 setup_coding_system (CODING_ID_NAME (this->id), coding);
5605 else if (detect_info.rejected == CATEGORY_MASK_ANY)
5606 setup_coding_system (Qraw_text, coding);
5607 else if (detect_info.rejected)
5608 for (i = 0; i < coding_category_raw_text; i++)
5609 if (! (detect_info.rejected & (1 << coding_priorities[i])))
5610 {
5611 this = coding_categories + coding_priorities[i];
5612 setup_coding_system (CODING_ID_NAME (this->id), coding);
5613 break;
5614 }
5615 }
4669 } 5616 }
4670 else 5617 else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding->id)))
5618 == coding_category_utf_16_auto)
4671 { 5619 {
4672 buf->data = (unsigned char *) xrealloc (buf->data, buf->size * 2); 5620 Lisp_Object coding_systems;
5621 struct coding_detection_info detect_info;
5622
5623 coding_systems
5624 = AREF (CODING_ID_ATTRS (coding->id), coding_attr_utf_16_bom);
5625 detect_info.found = detect_info.rejected = 0;
5626 if (CONSP (coding_systems)
5627 && detect_coding_utf_16 (coding, &detect_info))
5628 {
5629 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
5630 setup_coding_system (XCAR (coding_systems), coding);
5631 else if (detect_info.found & CATEGORY_MASK_UTF_16_BE)
5632 setup_coding_system (XCDR (coding_systems), coding);
5633 }
4673 } 5634 }
4674 buf->size *= 2;
4675} 5635}
4676 5636
4677/* Free the allocated memory for BUF if it is not on stack. */
4678static void
4679free_conversion_buffer (buf)
4680 struct conversion_buffer *buf;
4681{
4682 if (!buf->on_stack)
4683 xfree (buf->data);
4684}
4685 5637
4686int 5638static void
4687ccl_coding_driver (coding, source, destination, src_bytes, dst_bytes, encodep) 5639decode_eol (coding)
4688 struct coding_system *coding; 5640 struct coding_system *coding;
4689 unsigned char *source, *destination;
4690 int src_bytes, dst_bytes, encodep;
4691{ 5641{
4692 struct ccl_program *ccl 5642 Lisp_Object eol_type;
4693 = encodep ? &coding->spec.ccl.encoder : &coding->spec.ccl.decoder; 5643 unsigned char *p, *pbeg, *pend;
4694 unsigned char *dst = destination; 5644
5645 eol_type = CODING_ID_EOL_TYPE (coding->id);
5646 if (EQ (eol_type, Qunix))
5647 return;
4695 5648
4696 ccl->suppress_error = coding->suppress_error; 5649 if (NILP (coding->dst_object))
4697 ccl->last_block = coding->mode & CODING_MODE_LAST_BLOCK; 5650 pbeg = coding->destination;
4698 if (encodep)
4699 {
4700 /* On encoding, EOL format is converted within ccl_driver. For
4701 that, setup proper information in the structure CCL. */
4702 ccl->eol_type = coding->eol_type;
4703 if (ccl->eol_type ==CODING_EOL_UNDECIDED)
4704 ccl->eol_type = CODING_EOL_LF;
4705 ccl->cr_consumed = coding->spec.ccl.cr_carryover;
4706 ccl->eight_bit_control = coding->dst_multibyte;
4707 }
4708 else 5651 else
4709 ccl->eight_bit_control = 1; 5652 pbeg = BYTE_POS_ADDR (coding->dst_pos_byte);
4710 ccl->multibyte = coding->src_multibyte; 5653 pend = pbeg + coding->produced;
4711 if (coding->spec.ccl.eight_bit_carryover[0] != 0)
4712 {
4713 /* Move carryover bytes to DESTINATION. */
4714 unsigned char *p = coding->spec.ccl.eight_bit_carryover;
4715 while (*p)
4716 *dst++ = *p++;
4717 coding->spec.ccl.eight_bit_carryover[0] = 0;
4718 if (dst_bytes)
4719 dst_bytes -= dst - destination;
4720 }
4721 5654
4722 coding->produced = (ccl_driver (ccl, source, dst, src_bytes, dst_bytes, 5655 if (VECTORP (eol_type))
4723 &(coding->consumed))
4724 + dst - destination);
4725
4726 if (encodep)
4727 { 5656 {
4728 coding->produced_char = coding->produced; 5657 int eol_seen = EOL_SEEN_NONE;
4729 coding->spec.ccl.cr_carryover = ccl->cr_consumed; 5658
5659 for (p = pbeg; p < pend; p++)
5660 {
5661 if (*p == '\n')
5662 eol_seen |= EOL_SEEN_LF;
5663 else if (*p == '\r')
5664 {
5665 if (p + 1 < pend && *(p + 1) == '\n')
5666 {
5667 eol_seen |= EOL_SEEN_CRLF;
5668 p++;
5669 }
5670 else
5671 eol_seen |= EOL_SEEN_CR;
5672 }
5673 }
5674 if (eol_seen != EOL_SEEN_NONE
5675 && eol_seen != EOL_SEEN_LF
5676 && eol_seen != EOL_SEEN_CRLF
5677 && eol_seen != EOL_SEEN_CR)
5678 eol_seen = EOL_SEEN_LF;
5679 if (eol_seen != EOL_SEEN_NONE)
5680 eol_type = adjust_coding_eol_type (coding, eol_seen);
4730 } 5681 }
4731 else if (!ccl->eight_bit_control) 5682
5683 if (EQ (eol_type, Qmac))
4732 { 5684 {
4733 /* The produced bytes forms a valid multibyte sequence. */ 5685 for (p = pbeg; p < pend; p++)
4734 coding->produced_char 5686 if (*p == '\r')
4735 = multibyte_chars_in_text (destination, coding->produced); 5687 *p = '\n';
4736 coding->spec.ccl.eight_bit_carryover[0] = 0;
4737 } 5688 }
4738 else 5689 else if (EQ (eol_type, Qdos))
4739 { 5690 {
4740 /* On decoding, the destination should always multibyte. But, 5691 int n = 0;
4741 CCL program might have been generated an invalid multibyte 5692
4742 sequence. Here we make such a sequence valid as 5693 if (NILP (coding->dst_object))
4743 multibyte. */ 5694 {
4744 int bytes 5695 /* Start deleting '\r' from the tail to minimize the memory
4745 = dst_bytes ? dst_bytes : source + coding->consumed - destination; 5696 movement. */
4746 5697 for (p = pend - 2; p >= pbeg; p--)
4747 if ((coding->consumed < src_bytes 5698 if (*p == '\r')
4748 || !ccl->last_block) 5699 {
4749 && coding->produced >= 1 5700 safe_bcopy ((char *) (p + 1), (char *) p, pend-- - p - 1);
4750 && destination[coding->produced - 1] >= 0x80) 5701 n++;
4751 { 5702 }
4752 /* We should not convert the tailing 8-bit codes to 5703 }
4753 multibyte form even if they doesn't form a valid 5704 else
4754 multibyte sequence. They may form a valid sequence in 5705 {
4755 the next call. */ 5706 int pos_byte = coding->dst_pos_byte;
4756 int carryover = 0; 5707 int pos = coding->dst_pos;
4757 5708 int pos_end = pos + coding->produced_char - 1;
4758 if (destination[coding->produced - 1] < 0xA0) 5709
4759 carryover = 1; 5710 while (pos < pos_end)
4760 else if (coding->produced >= 2)
4761 { 5711 {
4762 if (destination[coding->produced - 2] >= 0x80) 5712 p = BYTE_POS_ADDR (pos_byte);
5713 if (*p == '\r' && p[1] == '\n')
4763 { 5714 {
4764 if (destination[coding->produced - 2] < 0xA0) 5715 del_range_2 (pos, pos_byte, pos + 1, pos_byte + 1, 0);
4765 carryover = 2; 5716 n++;
4766 else if (coding->produced >= 3 5717 pos_end--;
4767 && destination[coding->produced - 3] >= 0x80
4768 && destination[coding->produced - 3] < 0xA0)
4769 carryover = 3;
4770 } 5718 }
4771 } 5719 pos++;
4772 if (carryover > 0) 5720 pos_byte += BYTES_BY_CHAR_HEAD (*p);
4773 {
4774 BCOPY_SHORT (destination + coding->produced - carryover,
4775 coding->spec.ccl.eight_bit_carryover,
4776 carryover);
4777 coding->spec.ccl.eight_bit_carryover[carryover] = 0;
4778 coding->produced -= carryover;
4779 } 5721 }
4780 } 5722 }
4781 coding->produced = str_as_multibyte (destination, bytes, 5723 coding->produced -= n;
4782 coding->produced, 5724 coding->produced_char -= n;
4783 &(coding->produced_char));
4784 } 5725 }
4785
4786 switch (ccl->status)
4787 {
4788 case CCL_STAT_SUSPEND_BY_SRC:
4789 coding->result = CODING_FINISH_INSUFFICIENT_SRC;
4790 break;
4791 case CCL_STAT_SUSPEND_BY_DST:
4792 coding->result = CODING_FINISH_INSUFFICIENT_DST;
4793 break;
4794 case CCL_STAT_QUIT:
4795 case CCL_STAT_INVALID_CMD:
4796 coding->result = CODING_FINISH_INTERRUPT;
4797 break;
4798 default:
4799 coding->result = CODING_FINISH_NORMAL;
4800 break;
4801 }
4802 return coding->result;
4803} 5726}
4804 5727
4805/* Decode EOL format of the text at PTR of BYTES length destructively
4806 according to CODING->eol_type. This is called after the CCL
4807 program produced a decoded text at PTR. If we do CRLF->LF
4808 conversion, update CODING->produced and CODING->produced_char. */
4809 5728
4810static void 5729/* Return a translation table (or list of them) from coding system
4811decode_eol_post_ccl (coding, ptr, bytes) 5730 attribute vector ATTRS for encoding (ENCODEP is nonzero) or
4812 struct coding_system *coding; 5731 decoding (ENCODEP is zero). */
4813 unsigned char *ptr;
4814 int bytes;
4815{
4816 Lisp_Object val, saved_coding_symbol;
4817 unsigned char *pend = ptr + bytes;
4818 int dummy;
4819 5732
4820 /* Remember the current coding system symbol. We set it back when 5733static Lisp_Object
4821 an inconsistent EOL is found so that `last-coding-system-used' is 5734get_translation_table (attrs, encodep, max_lookup)
4822 set to the coding system that doesn't specify EOL conversion. */ 5735 Lisp_Object attrs;
4823 saved_coding_symbol = coding->symbol; 5736 int encodep, *max_lookup;
5737{
5738 Lisp_Object standard, translation_table;
5739 Lisp_Object val;
4824 5740
4825 coding->spec.ccl.cr_carryover = 0; 5741 if (encodep)
4826 if (coding->eol_type == CODING_EOL_UNDECIDED) 5742 translation_table = CODING_ATTR_ENCODE_TBL (attrs),
5743 standard = Vstandard_translation_table_for_encode;
5744 else
5745 translation_table = CODING_ATTR_DECODE_TBL (attrs),
5746 standard = Vstandard_translation_table_for_decode;
5747 if (NILP (translation_table))
5748 translation_table = standard;
5749 else
4827 { 5750 {
4828 /* Here, to avoid the call of setup_coding_system, we directly 5751 if (SYMBOLP (translation_table))
4829 call detect_eol_type. */ 5752 translation_table = Fget (translation_table, Qtranslation_table);
4830 coding->eol_type = detect_eol_type (ptr, bytes, &dummy); 5753 else if (CONSP (translation_table))
4831 if (coding->eol_type == CODING_EOL_INCONSISTENT) 5754 {
4832 coding->eol_type = CODING_EOL_LF; 5755 translation_table = Fcopy_sequence (translation_table);
4833 if (coding->eol_type != CODING_EOL_UNDECIDED) 5756 for (val = translation_table; CONSP (val); val = XCDR (val))
5757 if (SYMBOLP (XCAR (val)))
5758 XSETCAR (val, Fget (XCAR (val), Qtranslation_table));
5759 }
5760 if (CHAR_TABLE_P (standard))
4834 { 5761 {
4835 val = Fget (coding->symbol, Qeol_type); 5762 if (CONSP (translation_table))
4836 if (VECTORP (val) && XVECTOR (val)->size == 3) 5763 translation_table = nconc2 (translation_table,
4837 coding->symbol = XVECTOR (val)->contents[coding->eol_type]; 5764 Fcons (standard, Qnil));
5765 else
5766 translation_table = Fcons (translation_table,
5767 Fcons (standard, Qnil));
4838 } 5768 }
4839 coding->mode |= CODING_MODE_INHIBIT_INCONSISTENT_EOL;
4840 } 5769 }
4841 5770
4842 if (coding->eol_type == CODING_EOL_LF 5771 if (max_lookup)
4843 || coding->eol_type == CODING_EOL_UNDECIDED)
4844 {
4845 /* We have nothing to do. */
4846 ptr = pend;
4847 }
4848 else if (coding->eol_type == CODING_EOL_CRLF)
4849 { 5772 {
4850 unsigned char *pstart = ptr, *p = ptr; 5773 *max_lookup = 1;
4851 5774 if (CHAR_TABLE_P (translation_table)
4852 if (! (coding->mode & CODING_MODE_LAST_BLOCK) 5775 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table)) > 1)
4853 && *(pend - 1) == '\r')
4854 { 5776 {
4855 /* If the last character is CR, we can't handle it here 5777 val = XCHAR_TABLE (translation_table)->extras[1];
4856 because LF will be in the not-yet-decoded source text. 5778 if (NATNUMP (val) && *max_lookup < XFASTINT (val))
4857 Record that the CR is not yet processed. */ 5779 *max_lookup = XFASTINT (val);
4858 coding->spec.ccl.cr_carryover = 1;
4859 coding->produced--;
4860 coding->produced_char--;
4861 pend--;
4862 } 5780 }
4863 while (ptr < pend) 5781 else if (CONSP (translation_table))
4864 { 5782 {
4865 if (*ptr == '\r') 5783 Lisp_Object tail, val;
4866 {
4867 if (ptr + 1 < pend && *(ptr + 1) == '\n')
4868 {
4869 *p++ = '\n';
4870 ptr += 2;
4871 }
4872 else
4873 {
4874 if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
4875 goto undo_eol_conversion;
4876 *p++ = *ptr++;
4877 }
4878 }
4879 else if (*ptr == '\n'
4880 && coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
4881 goto undo_eol_conversion;
4882 else
4883 *p++ = *ptr++;
4884 continue;
4885 5784
4886 undo_eol_conversion: 5785 for (tail = translation_table; CONSP (tail); tail = XCDR (tail))
4887 /* We have faced with inconsistent EOL format at PTR. 5786 if (CHAR_TABLE_P (XCAR (tail))
4888 Convert all LFs before PTR back to CRLFs. */ 5787 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail))) > 1)
4889 for (p--, ptr--; p >= pstart; p--) 5788 {
4890 { 5789 val = XCHAR_TABLE (XCAR (tail))->extras[1];
4891 if (*p == '\n') 5790 if (NATNUMP (val) && *max_lookup < XFASTINT (val))
4892 *ptr-- = '\n', *ptr-- = '\r'; 5791 *max_lookup = XFASTINT (val);
4893 else 5792 }
4894 *ptr-- = *p;
4895 }
4896 /* If carryover is recorded, cancel it because we don't
4897 convert CRLF anymore. */
4898 if (coding->spec.ccl.cr_carryover)
4899 {
4900 coding->spec.ccl.cr_carryover = 0;
4901 coding->produced++;
4902 coding->produced_char++;
4903 pend++;
4904 }
4905 p = ptr = pend;
4906 coding->eol_type = CODING_EOL_LF;
4907 coding->symbol = saved_coding_symbol;
4908 }
4909 if (p < pend)
4910 {
4911 /* As each two-byte sequence CRLF was converted to LF, (PEND
4912 - P) is the number of deleted characters. */
4913 coding->produced -= pend - p;
4914 coding->produced_char -= pend - p;
4915 } 5793 }
4916 } 5794 }
4917 else /* i.e. coding->eol_type == CODING_EOL_CR */ 5795 return translation_table;
5796}
5797
5798#define LOOKUP_TRANSLATION_TABLE(table, c, trans) \
5799 do { \
5800 trans = Qnil; \
5801 if (CHAR_TABLE_P (table)) \
5802 { \
5803 trans = CHAR_TABLE_REF (table, c); \
5804 if (CHARACTERP (trans)) \
5805 c = XFASTINT (trans), trans = Qnil; \
5806 } \
5807 else if (CONSP (table)) \
5808 { \
5809 Lisp_Object tail; \
5810 \
5811 for (tail = table; CONSP (tail); tail = XCDR (tail)) \
5812 if (CHAR_TABLE_P (XCAR (tail))) \
5813 { \
5814 trans = CHAR_TABLE_REF (XCAR (tail), c); \
5815 if (CHARACTERP (trans)) \
5816 c = XFASTINT (trans), trans = Qnil; \
5817 else if (! NILP (trans)) \
5818 break; \
5819 } \
5820 } \
5821 } while (0)
5822
5823
5824static Lisp_Object
5825get_translation (val, buf, buf_end, last_block, from_nchars, to_nchars)
5826 Lisp_Object val;
5827 int *buf, *buf_end;
5828 int last_block;
5829 int *from_nchars, *to_nchars;
5830{
5831 /* VAL is TO or (([FROM-CHAR ...] . TO) ...) where TO is TO-CHAR or
5832 [TO-CHAR ...]. */
5833 if (CONSP (val))
4918 { 5834 {
4919 unsigned char *p = ptr; 5835 Lisp_Object from, tail;
5836 int i, len;
4920 5837
4921 for (; ptr < pend; ptr++) 5838 for (tail = val; CONSP (tail); tail = XCDR (tail))
4922 { 5839 {
4923 if (*ptr == '\r') 5840 val = XCAR (tail);
4924 *ptr = '\n'; 5841 from = XCAR (val);
4925 else if (*ptr == '\n' 5842 len = ASIZE (from);
4926 && coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL) 5843 for (i = 0; i < len; i++)
4927 { 5844 {
4928 for (; p < ptr; p++) 5845 if (buf + i == buf_end)
4929 { 5846 {
4930 if (*p == '\n') 5847 if (! last_block)
4931 *p = '\r'; 5848 return Qt;
5849 break;
4932 } 5850 }
4933 ptr = pend; 5851 if (XINT (AREF (from, i)) != buf[i])
4934 coding->eol_type = CODING_EOL_LF; 5852 break;
4935 coding->symbol = saved_coding_symbol; 5853 }
5854 if (i == len)
5855 {
5856 val = XCDR (val);
5857 *from_nchars = len;
5858 break;
4936 } 5859 }
4937 } 5860 }
5861 if (! CONSP (tail))
5862 return Qnil;
4938 } 5863 }
5864 if (VECTORP (val))
5865 *buf = XINT (AREF (val, 0)), *to_nchars = ASIZE (val);
5866 else
5867 *buf = XINT (val);
5868 return val;
4939} 5869}
4940 5870
4941/* See "GENERAL NOTES about `decode_coding_XXX ()' functions". Before
4942 decoding, it may detect coding system and format of end-of-line if
4943 those are not yet decided. The source should be unibyte, the
4944 result is multibyte if CODING->dst_multibyte is nonzero, else
4945 unibyte. */
4946 5871
4947int 5872static int
4948decode_coding (coding, source, destination, src_bytes, dst_bytes) 5873produce_chars (coding, translation_table, last_block)
4949 struct coding_system *coding; 5874 struct coding_system *coding;
4950 const unsigned char *source; 5875 Lisp_Object translation_table;
4951 unsigned char *destination; 5876 int last_block;
4952 int src_bytes, dst_bytes;
4953{ 5877{
4954 int extra = 0; 5878 unsigned char *dst = coding->destination + coding->produced;
4955 5879 unsigned char *dst_end = coding->destination + coding->dst_bytes;
4956 if (coding->type == coding_type_undecided) 5880 int produced;
4957 detect_coding (coding, source, src_bytes); 5881 int produced_chars = 0;
5882 int carryover = 0;
4958 5883
4959 if (coding->eol_type == CODING_EOL_UNDECIDED 5884 if (! coding->chars_at_source)
4960 && coding->type != coding_type_ccl)
4961 { 5885 {
4962 detect_eol (coding, source, src_bytes); 5886 /* Characters are in coding->charbuf. */
4963 /* We had better recover the original eol format if we 5887 int *buf = coding->charbuf;
4964 encounter an inconsistent eol format while decoding. */ 5888 int *buf_end = buf + coding->charbuf_used;
4965 coding->mode |= CODING_MODE_INHIBIT_INCONSISTENT_EOL;
4966 }
4967 5889
4968 coding->produced = coding->produced_char = 0; 5890 if (BUFFERP (coding->src_object)
4969 coding->consumed = coding->consumed_char = 0; 5891 && EQ (coding->src_object, coding->dst_object))
4970 coding->errors = 0; 5892 dst_end = ((unsigned char *) coding->source) + coding->consumed;
4971 coding->result = CODING_FINISH_NORMAL;
4972 5893
4973 switch (coding->type) 5894 while (buf < buf_end)
4974 { 5895 {
4975 case coding_type_sjis: 5896 int c = *buf, i;
4976 decode_coding_sjis_big5 (coding, source, destination,
4977 src_bytes, dst_bytes, 1);
4978 break;
4979 5897
4980 case coding_type_iso2022: 5898 if (c >= 0)
4981 decode_coding_iso2022 (coding, source, destination, 5899 {
4982 src_bytes, dst_bytes); 5900 int from_nchars = 1, to_nchars = 1;
4983 break; 5901 Lisp_Object trans = Qnil;
4984 5902
4985 case coding_type_big5: 5903 LOOKUP_TRANSLATION_TABLE (translation_table, c, trans);
4986 decode_coding_sjis_big5 (coding, source, destination, 5904 if (! NILP (trans))
4987 src_bytes, dst_bytes, 0); 5905 {
4988 break; 5906 trans = get_translation (trans, buf, buf_end, last_block,
5907 &from_nchars, &to_nchars);
5908 if (EQ (trans, Qt))
5909 break;
5910 c = *buf;
5911 }
4989 5912
4990 case coding_type_emacs_mule: 5913 if (dst + MAX_MULTIBYTE_LENGTH * to_nchars > dst_end)
4991 decode_coding_emacs_mule (coding, source, destination, 5914 {
4992 src_bytes, dst_bytes); 5915 dst = alloc_destination (coding,
4993 break; 5916 buf_end - buf
5917 + MAX_MULTIBYTE_LENGTH * to_nchars,
5918 dst);
5919 dst_end = coding->destination + coding->dst_bytes;
5920 }
4994 5921
4995 case coding_type_ccl: 5922 for (i = 0; i < to_nchars; i++)
4996 if (coding->spec.ccl.cr_carryover) 5923 {
4997 { 5924 if (i > 0)
4998 /* Put the CR which was not processed by the previous call 5925 c = XINT (AREF (trans, i));
4999 of decode_eol_post_ccl in DESTINATION. It will be 5926 if (coding->dst_multibyte
5000 decoded together with the following LF by the call to 5927 || ! CHAR_BYTE8_P (c))
5001 decode_eol_post_ccl below. */ 5928 CHAR_STRING_ADVANCE (c, dst);
5002 *destination = '\r'; 5929 else
5003 coding->produced++; 5930 *dst++ = CHAR_TO_BYTE8 (c);
5004 coding->produced_char++; 5931 }
5005 dst_bytes--; 5932 produced_chars += to_nchars;
5006 extra = coding->spec.ccl.cr_carryover; 5933 *buf++ = to_nchars;
5007 } 5934 while (--from_nchars > 0)
5008 ccl_coding_driver (coding, source, destination + extra, 5935 *buf++ = 0;
5009 src_bytes, dst_bytes, 0); 5936 }
5010 if (coding->eol_type != CODING_EOL_LF) 5937 else
5011 { 5938 /* This is an annotation datum. (-C) is the length. */
5012 coding->produced += extra; 5939 buf += -c;
5013 coding->produced_char += extra;
5014 decode_eol_post_ccl (coding, destination, coding->produced);
5015 } 5940 }
5016 break; 5941 carryover = buf_end - buf;
5017
5018 default:
5019 decode_eol (coding, source, destination, src_bytes, dst_bytes);
5020 } 5942 }
5021 5943 else
5022 if (coding->result == CODING_FINISH_INSUFFICIENT_SRC
5023 && coding->mode & CODING_MODE_LAST_BLOCK
5024 && coding->consumed == src_bytes)
5025 coding->result = CODING_FINISH_NORMAL;
5026
5027 if (coding->mode & CODING_MODE_LAST_BLOCK
5028 && coding->result == CODING_FINISH_INSUFFICIENT_SRC)
5029 { 5944 {
5030 const unsigned char *src = source + coding->consumed; 5945 const unsigned char *src = coding->source;
5031 unsigned char *dst = destination + coding->produced; 5946 const unsigned char *src_end = src + coding->src_bytes;
5947 Lisp_Object eol_type;
5032 5948
5033 src_bytes -= coding->consumed; 5949 eol_type = CODING_ID_EOL_TYPE (coding->id);
5034 coding->errors++; 5950
5035 if (COMPOSING_P (coding)) 5951 if (coding->src_multibyte != coding->dst_multibyte)
5036 DECODE_COMPOSITION_END ('1');
5037 while (src_bytes--)
5038 { 5952 {
5039 int c = *src++; 5953 if (coding->src_multibyte)
5040 dst += CHAR_STRING (c, dst); 5954 {
5041 coding->produced_char++; 5955 int multibytep = 1;
5042 } 5956 int consumed_chars;
5043 coding->consumed = coding->consumed_char = src - source;
5044 coding->produced = dst - destination;
5045 coding->result = CODING_FINISH_NORMAL;
5046 }
5047 5957
5048 if (!coding->dst_multibyte) 5958 while (1)
5049 { 5959 {
5050 coding->produced = str_as_unibyte (destination, coding->produced); 5960 const unsigned char *src_base = src;
5051 coding->produced_char = coding->produced; 5961 int c;
5052 }
5053 5962
5054 return coding->result; 5963 ONE_MORE_BYTE (c);
5055} 5964 if (c == '\r')
5965 {
5966 if (EQ (eol_type, Qdos))
5967 {
5968 if (src == src_end)
5969 {
5970 record_conversion_result
5971 (coding, CODING_RESULT_INSUFFICIENT_SRC);
5972 goto no_more_source;
5973 }
5974 if (*src == '\n')
5975 c = *src++;
5976 }
5977 else if (EQ (eol_type, Qmac))
5978 c = '\n';
5979 }
5980 if (dst == dst_end)
5981 {
5982 coding->consumed = src - coding->source;
5983
5984 if (EQ (coding->src_object, coding->dst_object))
5985 dst_end = (unsigned char *) src;
5986 if (dst == dst_end)
5987 {
5988 dst = alloc_destination (coding, src_end - src + 1,
5989 dst);
5990 dst_end = coding->destination + coding->dst_bytes;
5991 coding_set_source (coding);
5992 src = coding->source + coding->consumed;
5993 src_end = coding->source + coding->src_bytes;
5994 }
5995 }
5996 *dst++ = c;
5997 produced_chars++;
5998 }
5999 no_more_source:
6000 ;
6001 }
6002 else
6003 while (src < src_end)
6004 {
6005 int multibytep = 1;
6006 int c = *src++;
5056 6007
5057/* See "GENERAL NOTES about `encode_coding_XXX ()' functions". The 6008 if (c == '\r')
5058 multibyteness of the source is CODING->src_multibyte, the 6009 {
5059 multibyteness of the result is always unibyte. */ 6010 if (EQ (eol_type, Qdos))
6011 {
6012 if (src < src_end
6013 && *src == '\n')
6014 c = *src++;
6015 }
6016 else if (EQ (eol_type, Qmac))
6017 c = '\n';
6018 }
6019 if (dst >= dst_end - 1)
6020 {
6021 coding->consumed = src - coding->source;
6022
6023 if (EQ (coding->src_object, coding->dst_object))
6024 dst_end = (unsigned char *) src;
6025 if (dst >= dst_end - 1)
6026 {
6027 dst = alloc_destination (coding, src_end - src + 2,
6028 dst);
6029 dst_end = coding->destination + coding->dst_bytes;
6030 coding_set_source (coding);
6031 src = coding->source + coding->consumed;
6032 src_end = coding->source + coding->src_bytes;
6033 }
6034 }
6035 EMIT_ONE_BYTE (c);
6036 }
6037 }
6038 else
6039 {
6040 if (!EQ (coding->src_object, coding->dst_object))
6041 {
6042 int require = coding->src_bytes - coding->dst_bytes;
5060 6043
5061int 6044 if (require > 0)
5062encode_coding (coding, source, destination, src_bytes, dst_bytes) 6045 {
5063 struct coding_system *coding; 6046 EMACS_INT offset = src - coding->source;
5064 const unsigned char *source;
5065 unsigned char *destination;
5066 int src_bytes, dst_bytes;
5067{
5068 coding->produced = coding->produced_char = 0;
5069 coding->consumed = coding->consumed_char = 0;
5070 coding->errors = 0;
5071 coding->result = CODING_FINISH_NORMAL;
5072 if (coding->eol_type == CODING_EOL_UNDECIDED)
5073 coding->eol_type = CODING_EOL_LF;
5074 6047
5075 switch (coding->type) 6048 dst = alloc_destination (coding, require, dst);
5076 { 6049 coding_set_source (coding);
5077 case coding_type_sjis: 6050 src = coding->source + offset;
5078 encode_coding_sjis_big5 (coding, source, destination, 6051 src_end = coding->source + coding->src_bytes;
5079 src_bytes, dst_bytes, 1); 6052 }
5080 break; 6053 }
6054 produced_chars = coding->src_chars;
6055 while (src < src_end)
6056 {
6057 int c = *src++;
5081 6058
5082 case coding_type_iso2022: 6059 if (c == '\r')
5083 encode_coding_iso2022 (coding, source, destination, 6060 {
5084 src_bytes, dst_bytes); 6061 if (EQ (eol_type, Qdos))
5085 break; 6062 {
6063 if (src < src_end
6064 && *src == '\n')
6065 c = *src++;
6066 produced_chars--;
6067 }
6068 else if (EQ (eol_type, Qmac))
6069 c = '\n';
6070 }
6071 *dst++ = c;
6072 }
6073 }
6074 coding->consumed = coding->src_bytes;
6075 coding->consumed_char = coding->src_chars;
6076 }
5086 6077
5087 case coding_type_big5: 6078 produced = dst - (coding->destination + coding->produced);
5088 encode_coding_sjis_big5 (coding, source, destination, 6079 if (BUFFERP (coding->dst_object) && produced_chars > 0)
5089 src_bytes, dst_bytes, 0); 6080 insert_from_gap (produced_chars, produced);
5090 break; 6081 coding->produced += produced;
6082 coding->produced_char += produced_chars;
6083 return carryover;
6084}
5091 6085
5092 case coding_type_emacs_mule: 6086/* Compose text in CODING->object according to the annotation data at
5093 encode_coding_emacs_mule (coding, source, destination, 6087 CHARBUF. CHARBUF is an array:
5094 src_bytes, dst_bytes); 6088 [ -LENGTH ANNOTATION_MASK FROM TO METHOD COMP_LEN [ COMPONENTS... ] ]
5095 break; 6089 */
5096 6090
5097 case coding_type_ccl: 6091static INLINE void
5098 ccl_coding_driver (coding, source, destination, 6092produce_composition (coding, charbuf, pos)
5099 src_bytes, dst_bytes, 1); 6093 struct coding_system *coding;
5100 break; 6094 int *charbuf;
6095 EMACS_INT pos;
6096{
6097 int len;
6098 EMACS_INT to;
6099 enum composition_method method;
6100 Lisp_Object components;
5101 6101
5102 default: 6102 len = -charbuf[0];
5103 encode_eol (coding, source, destination, src_bytes, dst_bytes); 6103 to = pos + charbuf[2];
5104 } 6104 if (to <= pos)
6105 return;
6106 method = (enum composition_method) (charbuf[3]);
5105 6107
5106 if (coding->mode & CODING_MODE_LAST_BLOCK 6108 if (method == COMPOSITION_RELATIVE)
5107 && coding->result == CODING_FINISH_INSUFFICIENT_SRC) 6109 components = Qnil;
6110 else if (method >= COMPOSITION_WITH_RULE
6111 && method <= COMPOSITION_WITH_RULE_ALTCHARS)
5108 { 6112 {
5109 const unsigned char *src = source + coding->consumed; 6113 Lisp_Object args[MAX_COMPOSITION_COMPONENTS * 2 - 1];
5110 unsigned char *dst = destination + coding->produced; 6114 int i;
5111 6115
5112 if (coding->type == coding_type_iso2022) 6116 len -= 4;
5113 ENCODE_RESET_PLANE_AND_REGISTER; 6117 charbuf += 4;
5114 if (COMPOSING_P (coding)) 6118 for (i = 0; i < len; i++)
5115 *dst++ = ISO_CODE_ESC, *dst++ = '1';
5116 if (coding->consumed < src_bytes)
5117 { 6119 {
5118 int len = src_bytes - coding->consumed; 6120 args[i] = make_number (charbuf[i]);
5119 6121 if (charbuf[i] < 0)
5120 BCOPY_SHORT (src, dst, len); 6122 return;
5121 if (coding->src_multibyte)
5122 len = str_as_unibyte (dst, len);
5123 dst += len;
5124 coding->consumed = src_bytes;
5125 } 6123 }
5126 coding->produced = coding->produced_char = dst - destination; 6124 components = (method == COMPOSITION_WITH_ALTCHARS
5127 coding->result = CODING_FINISH_NORMAL; 6125 ? Fstring (len, args) : Fvector (len, args));
5128 } 6126 }
5129 6127 else
5130 if (coding->result == CODING_FINISH_INSUFFICIENT_SRC 6128 return;
5131 && coding->consumed == src_bytes) 6129 compose_text (pos, to, components, Qnil, coding->dst_object);
5132 coding->result = CODING_FINISH_NORMAL;
5133
5134 return coding->result;
5135} 6130}
5136 6131
5137/* Scan text in the region between *BEG and *END (byte positions),
5138 skip characters which we don't have to decode by coding system
5139 CODING at the head and tail, then set *BEG and *END to the region
5140 of the text we actually have to convert. The caller should move
5141 the gap out of the region in advance if the region is from a
5142 buffer.
5143 6132
5144 If STR is not NULL, *BEG and *END are indices into STR. */ 6133/* Put `charset' property on text in CODING->object according to
6134 the annotation data at CHARBUF. CHARBUF is an array:
6135 [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ]
6136 */
5145 6137
5146static void 6138static INLINE void
5147shrink_decoding_region (beg, end, coding, str) 6139produce_charset (coding, charbuf, pos)
5148 int *beg, *end;
5149 struct coding_system *coding; 6140 struct coding_system *coding;
5150 unsigned char *str; 6141 int *charbuf;
6142 EMACS_INT pos;
5151{ 6143{
5152 unsigned char *begp_orig, *begp, *endp_orig, *endp, c; 6144 EMACS_INT from = pos - charbuf[2];
5153 int eol_conversion; 6145 struct charset *charset = CHARSET_FROM_ID (charbuf[3]);
5154 Lisp_Object translation_table;
5155 6146
5156 if (coding->type == coding_type_ccl 6147 Fput_text_property (make_number (from), make_number (pos),
5157 || coding->type == coding_type_undecided 6148 Qcharset, CHARSET_NAME (charset),
5158 || coding->eol_type != CODING_EOL_LF 6149 coding->dst_object);
5159 || !NILP (coding->post_read_conversion) 6150}
5160 || coding->composing != COMPOSITION_DISABLED)
5161 {
5162 /* We can't skip any data. */
5163 return;
5164 }
5165 if (coding->type == coding_type_no_conversion
5166 || coding->type == coding_type_raw_text
5167 || coding->type == coding_type_emacs_mule)
5168 {
5169 /* We need no conversion, but don't have to skip any data here.
5170 Decoding routine handles them effectively anyway. */
5171 return;
5172 }
5173 6151
5174 translation_table = coding->translation_table_for_decode;
5175 if (NILP (translation_table) && !NILP (Venable_character_translation))
5176 translation_table = Vstandard_translation_table_for_decode;
5177 if (CHAR_TABLE_P (translation_table))
5178 {
5179 int i;
5180 for (i = 0; i < 128; i++)
5181 if (!NILP (CHAR_TABLE_REF (translation_table, i)))
5182 break;
5183 if (i < 128)
5184 /* Some ASCII character should be translated. We give up
5185 shrinking. */
5186 return;
5187 }
5188 6152
5189 if (coding->heading_ascii >= 0) 6153#define CHARBUF_SIZE 0x4000
5190 /* Detection routine has already found how much we can skip at the
5191 head. */
5192 *beg += coding->heading_ascii;
5193 6154
5194 if (str) 6155#define ALLOC_CONVERSION_WORK_AREA(coding) \
5195 { 6156 do { \
5196 begp_orig = begp = str + *beg; 6157 int size = CHARBUF_SIZE;; \
5197 endp_orig = endp = str + *end; 6158 \
5198 } 6159 coding->charbuf = NULL; \
5199 else 6160 while (size > 1024) \
5200 { 6161 { \
5201 begp_orig = begp = BYTE_POS_ADDR (*beg); 6162 coding->charbuf = (int *) alloca (sizeof (int) * size); \
5202 endp_orig = endp = begp + *end - *beg; 6163 if (coding->charbuf) \
5203 } 6164 break; \
6165 size >>= 1; \
6166 } \
6167 if (! coding->charbuf) \
6168 { \
6169 record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \
6170 return coding->result; \
6171 } \
6172 coding->charbuf_size = size; \
6173 } while (0)
5204 6174
5205 eol_conversion = (coding->eol_type == CODING_EOL_CR
5206 || coding->eol_type == CODING_EOL_CRLF);
5207 6175
5208 switch (coding->type) 6176static void
6177produce_annotation (coding, pos)
6178 struct coding_system *coding;
6179 EMACS_INT pos;
6180{
6181 int *charbuf = coding->charbuf;
6182 int *charbuf_end = charbuf + coding->charbuf_used;
6183
6184 if (NILP (coding->dst_object))
6185 return;
6186
6187 while (charbuf < charbuf_end)
5209 { 6188 {
5210 case coding_type_sjis: 6189 if (*charbuf >= 0)
5211 case coding_type_big5: 6190 pos += *charbuf++;
5212 /* We can skip all ASCII characters at the head. */ 6191 else
5213 if (coding->heading_ascii < 0)
5214 { 6192 {
5215 if (eol_conversion) 6193 int len = -*charbuf;
5216 while (begp < endp && *begp < 0x80 && *begp != '\r') begp++; 6194 switch (charbuf[1])
5217 else 6195 {
5218 while (begp < endp && *begp < 0x80) begp++; 6196 case CODING_ANNOTATE_COMPOSITION_MASK:
6197 produce_composition (coding, charbuf, pos);
6198 break;
6199 case CODING_ANNOTATE_CHARSET_MASK:
6200 produce_charset (coding, charbuf, pos);
6201 break;
6202 default:
6203 abort ();
6204 }
6205 charbuf += len;
5219 } 6206 }
5220 /* We can skip all ASCII characters at the tail except for the 6207 }
5221 second byte of SJIS or BIG5 code. */ 6208}
5222 if (eol_conversion)
5223 while (begp < endp && endp[-1] < 0x80 && endp[-1] != '\r') endp--;
5224 else
5225 while (begp < endp && endp[-1] < 0x80) endp--;
5226 /* Do not consider LF as ascii if preceded by CR, since that
5227 confuses eol decoding. */
5228 if (begp < endp && endp < endp_orig && endp[-1] == '\r' && endp[0] == '\n')
5229 endp++;
5230 if (begp < endp && endp < endp_orig && endp[-1] >= 0x80)
5231 endp++;
5232 break;
5233 6209
5234 case coding_type_iso2022: 6210/* Decode the data at CODING->src_object into CODING->dst_object.
5235 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, 0) != CHARSET_ASCII) 6211 CODING->src_object is a buffer, a string, or nil.
5236 /* We can't skip any data. */ 6212 CODING->dst_object is a buffer.
5237 break;
5238 if (coding->heading_ascii < 0)
5239 {
5240 /* We can skip all ASCII characters at the head except for a
5241 few control codes. */
5242 while (begp < endp && (c = *begp) < 0x80
5243 && c != ISO_CODE_CR && c != ISO_CODE_SO
5244 && c != ISO_CODE_SI && c != ISO_CODE_ESC
5245 && (!eol_conversion || c != ISO_CODE_LF))
5246 begp++;
5247 }
5248 switch (coding->category_idx)
5249 {
5250 case CODING_CATEGORY_IDX_ISO_8_1:
5251 case CODING_CATEGORY_IDX_ISO_8_2:
5252 /* We can skip all ASCII characters at the tail. */
5253 if (eol_conversion)
5254 while (begp < endp && (c = endp[-1]) < 0x80 && c != '\r') endp--;
5255 else
5256 while (begp < endp && endp[-1] < 0x80) endp--;
5257 /* Do not consider LF as ascii if preceded by CR, since that
5258 confuses eol decoding. */
5259 if (begp < endp && endp < endp_orig && endp[-1] == '\r' && endp[0] == '\n')
5260 endp++;
5261 break;
5262 6213
5263 case CODING_CATEGORY_IDX_ISO_7: 6214 If CODING->src_object is a buffer, it must be the current buffer.
5264 case CODING_CATEGORY_IDX_ISO_7_TIGHT: 6215 In this case, if CODING->src_pos is positive, it is a position of
5265 { 6216 the source text in the buffer, otherwise, the source text is in the
5266 /* We can skip all characters at the tail except for 8-bit 6217 gap area of the buffer, and CODING->src_pos specifies the offset of
5267 codes and ESC and the following 2-byte at the tail. */ 6218 the text from GPT (which must be the same as PT). If this is the
5268 unsigned char *eight_bit = NULL; 6219 same buffer as CODING->dst_object, CODING->src_pos must be
6220 negative.
5269 6221
5270 if (eol_conversion) 6222 If CODING->src_object is a string, CODING->src_pos is an index to
5271 while (begp < endp 6223 that string.
5272 && (c = endp[-1]) != ISO_CODE_ESC && c != '\r')
5273 {
5274 if (!eight_bit && c & 0x80) eight_bit = endp;
5275 endp--;
5276 }
5277 else
5278 while (begp < endp
5279 && (c = endp[-1]) != ISO_CODE_ESC)
5280 {
5281 if (!eight_bit && c & 0x80) eight_bit = endp;
5282 endp--;
5283 }
5284 /* Do not consider LF as ascii if preceded by CR, since that
5285 confuses eol decoding. */
5286 if (begp < endp && endp < endp_orig
5287 && endp[-1] == '\r' && endp[0] == '\n')
5288 endp++;
5289 if (begp < endp && endp[-1] == ISO_CODE_ESC)
5290 {
5291 if (endp + 1 < endp_orig && end[0] == '(' && end[1] == 'B')
5292 /* This is an ASCII designation sequence. We can
5293 surely skip the tail. But, if we have
5294 encountered an 8-bit code, skip only the codes
5295 after that. */
5296 endp = eight_bit ? eight_bit : endp + 2;
5297 else
5298 /* Hmmm, we can't skip the tail. */
5299 endp = endp_orig;
5300 }
5301 else if (eight_bit)
5302 endp = eight_bit;
5303 }
5304 }
5305 break;
5306 6224
5307 default: 6225 If CODING->src_object is nil, CODING->source must already point to
5308 abort (); 6226 the non-relocatable memory area. In this case, CODING->src_pos is
5309 } 6227 an offset from CODING->source.
5310 *beg += begp - begp_orig;
5311 *end += endp - endp_orig;
5312 return;
5313}
5314 6228
5315/* Like shrink_decoding_region but for encoding. */ 6229 The decoded data is inserted at the current point of the buffer
6230 CODING->dst_object.
6231*/
5316 6232
5317static void 6233static int
5318shrink_encoding_region (beg, end, coding, str) 6234decode_coding (coding)
5319 int *beg, *end;
5320 struct coding_system *coding; 6235 struct coding_system *coding;
5321 unsigned char *str;
5322{ 6236{
5323 unsigned char *begp_orig, *begp, *endp_orig, *endp; 6237 Lisp_Object attrs;
5324 int eol_conversion; 6238 Lisp_Object undo_list;
5325 Lisp_Object translation_table; 6239 Lisp_Object translation_table;
6240 int carryover;
6241 int i;
5326 6242
5327 if (coding->type == coding_type_ccl 6243 if (BUFFERP (coding->src_object)
5328 || coding->eol_type == CODING_EOL_CRLF 6244 && coding->src_pos > 0
5329 || coding->eol_type == CODING_EOL_CR 6245 && coding->src_pos < GPT
5330 || (coding->cmp_data && coding->cmp_data->used > 0)) 6246 && coding->src_pos + coding->src_chars > GPT)
5331 { 6247 move_gap_both (coding->src_pos, coding->src_pos_byte);
5332 /* We can't skip any data. */ 6248
5333 return; 6249 undo_list = Qt;
5334 } 6250 if (BUFFERP (coding->dst_object))
5335 if (coding->type == coding_type_no_conversion
5336 || coding->type == coding_type_raw_text
5337 || coding->type == coding_type_emacs_mule
5338 || coding->type == coding_type_undecided)
5339 { 6251 {
5340 /* We need no conversion, but don't have to skip any data here. 6252 if (current_buffer != XBUFFER (coding->dst_object))
5341 Encoding routine handles them effectively anyway. */ 6253 set_buffer_internal (XBUFFER (coding->dst_object));
5342 return; 6254 if (GPT != PT)
6255 move_gap_both (PT, PT_BYTE);
6256 undo_list = current_buffer->undo_list;
6257 current_buffer->undo_list = Qt;
5343 } 6258 }
5344 6259
5345 translation_table = coding->translation_table_for_encode; 6260 coding->consumed = coding->consumed_char = 0;
5346 if (NILP (translation_table) && !NILP (Venable_character_translation)) 6261 coding->produced = coding->produced_char = 0;
5347 translation_table = Vstandard_translation_table_for_encode; 6262 coding->chars_at_source = 0;
5348 if (CHAR_TABLE_P (translation_table)) 6263 record_conversion_result (coding, CODING_RESULT_SUCCESS);
6264 coding->errors = 0;
6265
6266 ALLOC_CONVERSION_WORK_AREA (coding);
6267
6268 attrs = CODING_ID_ATTRS (coding->id);
6269 translation_table = get_translation_table (attrs, 0, NULL);
6270
6271 carryover = 0;
6272 do
5349 { 6273 {
5350 int i; 6274 EMACS_INT pos = coding->dst_pos + coding->produced_char;
5351 for (i = 0; i < 128; i++) 6275
5352 if (!NILP (CHAR_TABLE_REF (translation_table, i))) 6276 coding_set_source (coding);
5353 break; 6277 coding->annotated = 0;
5354 if (i < 128) 6278 coding->charbuf_used = carryover;
5355 /* Some ASCII character should be translated. We give up 6279 (*(coding->decoder)) (coding);
5356 shrinking. */ 6280 coding_set_destination (coding);
5357 return; 6281 carryover = produce_chars (coding, translation_table, 0);
6282 if (coding->annotated)
6283 produce_annotation (coding, pos);
6284 for (i = 0; i < carryover; i++)
6285 coding->charbuf[i]
6286 = coding->charbuf[coding->charbuf_used - carryover + i];
5358 } 6287 }
6288 while (coding->consumed < coding->src_bytes
6289 && (coding->result == CODING_RESULT_SUCCESS
6290 || coding->result == CODING_RESULT_INVALID_SRC));
5359 6291
5360 if (str) 6292 if (carryover > 0)
5361 { 6293 {
5362 begp_orig = begp = str + *beg; 6294 coding_set_destination (coding);
5363 endp_orig = endp = str + *end; 6295 coding->charbuf_used = carryover;
6296 produce_chars (coding, translation_table, 1);
5364 } 6297 }
5365 else 6298
6299 coding->carryover_bytes = 0;
6300 if (coding->consumed < coding->src_bytes)
5366 { 6301 {
5367 begp_orig = begp = BYTE_POS_ADDR (*beg); 6302 int nbytes = coding->src_bytes - coding->consumed;
5368 endp_orig = endp = begp + *end - *beg; 6303 const unsigned char *src;
5369 }
5370 6304
5371 eol_conversion = (coding->eol_type == CODING_EOL_CR 6305 coding_set_source (coding);
5372 || coding->eol_type == CODING_EOL_CRLF); 6306 coding_set_destination (coding);
6307 src = coding->source + coding->consumed;
5373 6308
5374 /* Here, we don't have to check coding->pre_write_conversion because 6309 if (coding->mode & CODING_MODE_LAST_BLOCK)
5375 the caller is expected to have handled it already. */
5376 switch (coding->type)
5377 {
5378 case coding_type_iso2022:
5379 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, 0) != CHARSET_ASCII)
5380 /* We can't skip any data. */
5381 break;
5382 if (coding->flags & CODING_FLAG_ISO_DESIGNATE_AT_BOL)
5383 { 6310 {
5384 unsigned char *bol = begp; 6311 /* Flush out unprocessed data as binary chars. We are sure
5385 while (begp < endp && *begp < 0x80) 6312 that the number of data is less than the size of
6313 coding->charbuf. */
6314 coding->charbuf_used = 0;
6315 while (nbytes-- > 0)
5386 { 6316 {
5387 begp++; 6317 int c = *src++;
5388 if (begp[-1] == '\n') 6318
5389 bol = begp; 6319 if (c & 0x80)
6320 c = BYTE8_TO_CHAR (c);
6321 coding->charbuf[coding->charbuf_used++] = c;
5390 } 6322 }
5391 begp = bol; 6323 produce_chars (coding, Qnil, 1);
5392 goto label_skip_tail;
5393 } 6324 }
5394 /* fall down ... */
5395
5396 case coding_type_sjis:
5397 case coding_type_big5:
5398 /* We can skip all ASCII characters at the head and tail. */
5399 if (eol_conversion)
5400 while (begp < endp && *begp < 0x80 && *begp != '\n') begp++;
5401 else
5402 while (begp < endp && *begp < 0x80) begp++;
5403 label_skip_tail:
5404 if (eol_conversion)
5405 while (begp < endp && endp[-1] < 0x80 && endp[-1] != '\n') endp--;
5406 else 6325 else
5407 while (begp < endp && *(endp - 1) < 0x80) endp--; 6326 {
5408 break; 6327 /* Record unprocessed bytes in coding->carryover. We are
5409 6328 sure that the number of data is less than the size of
5410 default: 6329 coding->carryover. */
5411 abort (); 6330 unsigned char *p = coding->carryover;
6331
6332 coding->carryover_bytes = nbytes;
6333 while (nbytes-- > 0)
6334 *p++ = *src++;
6335 }
6336 coding->consumed = coding->src_bytes;
5412 } 6337 }
5413 6338
5414 *beg += begp - begp_orig; 6339 if (! EQ (CODING_ID_EOL_TYPE (coding->id), Qunix))
5415 *end += endp - endp_orig; 6340 decode_eol (coding);
5416 return; 6341 if (BUFFERP (coding->dst_object))
6342 {
6343 current_buffer->undo_list = undo_list;
6344 record_insert (coding->dst_pos, coding->produced_char);
6345 }
6346 return coding->result;
5417} 6347}
5418 6348
5419/* As shrinking conversion region requires some overhead, we don't try
5420 shrinking if the length of conversion region is less than this
5421 value. */
5422static int shrink_conversion_region_threshhold = 1024;
5423
5424#define SHRINK_CONVERSION_REGION(beg, end, coding, str, encodep) \
5425 do { \
5426 if (*(end) - *(beg) > shrink_conversion_region_threshhold) \
5427 { \
5428 if (encodep) shrink_encoding_region (beg, end, coding, str); \
5429 else shrink_decoding_region (beg, end, coding, str); \
5430 } \
5431 } while (0)
5432
5433/* ARG is (CODING BUFFER ...) where CODING is what to be set in
5434 Vlast_coding_system_used and the remaining elements are buffers to
5435 kill. */
5436static Lisp_Object
5437code_convert_region_unwind (arg)
5438 Lisp_Object arg;
5439{
5440 struct gcpro gcpro1;
5441 GCPRO1 (arg);
5442
5443 inhibit_pre_post_conversion = 0;
5444 Vlast_coding_system_used = XCAR (arg);
5445 for (arg = XCDR (arg); CONSP (arg); arg = XCDR (arg))
5446 Fkill_buffer (XCAR (arg));
5447 6349
5448 UNGCPRO; 6350/* Extract an annotation datum from a composition starting at POS and
5449 return Qnil; 6351 ending before LIMIT of CODING->src_object (buffer or string), store
5450} 6352 the data in BUF, set *STOP to a starting position of the next
6353 composition (if any) or to LIMIT, and return the address of the
6354 next element of BUF.
5451 6355
5452/* Store information about all compositions in the range FROM and TO 6356 If such an annotation is not found, set *STOP to a starting
5453 of OBJ in memory blocks pointed by CODING->cmp_data. OBJ is a 6357 position of a composition after POS (if any) or to LIMIT, and
5454 buffer or a string, defaults to the current buffer. */ 6358 return BUF. */
5455 6359
5456void 6360static INLINE int *
5457coding_save_composition (coding, from, to, obj) 6361handle_composition_annotation (pos, limit, coding, buf, stop)
6362 EMACS_INT pos, limit;
5458 struct coding_system *coding; 6363 struct coding_system *coding;
5459 int from, to; 6364 int *buf;
5460 Lisp_Object obj; 6365 EMACS_INT *stop;
5461{ 6366{
6367 EMACS_INT start, end;
5462 Lisp_Object prop; 6368 Lisp_Object prop;
5463 int start, end;
5464 6369
5465 if (coding->composing == COMPOSITION_DISABLED) 6370 if (! find_composition (pos, limit, &start, &end, &prop, coding->src_object)
5466 return; 6371 || end > limit)
5467 if (!coding->cmp_data) 6372 *stop = limit;
5468 coding_allocate_composition_data (coding, from); 6373 else if (start > pos)
5469 if (!find_composition (from, to, &start, &end, &prop, obj) 6374 *stop = start;
5470 || end > to) 6375 else
5471 return;
5472 if (start < from
5473 && (!find_composition (end, to, &start, &end, &prop, obj)
5474 || end > to))
5475 return;
5476 coding->composing = COMPOSITION_NO;
5477 do
5478 { 6376 {
5479 if (COMPOSITION_VALID_P (start, end, prop)) 6377 if (start == pos)
5480 { 6378 {
6379 /* We found a composition. Store the corresponding
6380 annotation data in BUF. */
6381 int *head = buf;
5481 enum composition_method method = COMPOSITION_METHOD (prop); 6382 enum composition_method method = COMPOSITION_METHOD (prop);
5482 if (coding->cmp_data->used + COMPOSITION_DATA_MAX_BUNCH_LENGTH 6383 int nchars = COMPOSITION_LENGTH (prop);
5483 >= COMPOSITION_DATA_SIZE) 6384
5484 coding_allocate_composition_data (coding, from); 6385 ADD_COMPOSITION_DATA (buf, nchars, method);
5485 /* For relative composition, we remember start and end
5486 positions, for the other compositions, we also remember
5487 components. */
5488 CODING_ADD_COMPOSITION_START (coding, start - from, method);
5489 if (method != COMPOSITION_RELATIVE) 6386 if (method != COMPOSITION_RELATIVE)
5490 { 6387 {
5491 /* We must store a*/ 6388 Lisp_Object components;
5492 Lisp_Object val, ch; 6389 int len, i, i_byte;
5493 6390
5494 val = COMPOSITION_COMPONENTS (prop); 6391 components = COMPOSITION_COMPONENTS (prop);
5495 if (CONSP (val)) 6392 if (VECTORP (components))
5496 while (CONSP (val))
5497 {
5498 ch = XCAR (val), val = XCDR (val);
5499 CODING_ADD_COMPOSITION_COMPONENT (coding, XINT (ch));
5500 }
5501 else if (VECTORP (val) || STRINGP (val))
5502 { 6393 {
5503 int len = (VECTORP (val) 6394 len = XVECTOR (components)->size;
5504 ? XVECTOR (val)->size : SCHARS (val));
5505 int i;
5506 for (i = 0; i < len; i++) 6395 for (i = 0; i < len; i++)
6396 *buf++ = XINT (AREF (components, i));
6397 }
6398 else if (STRINGP (components))
6399 {
6400 len = SCHARS (components);
6401 i = i_byte = 0;
6402 while (i < len)
5507 { 6403 {
5508 ch = (STRINGP (val) 6404 FETCH_STRING_CHAR_ADVANCE (*buf, components, i, i_byte);
5509 ? Faref (val, make_number (i)) 6405 buf++;
5510 : XVECTOR (val)->contents[i]);
5511 CODING_ADD_COMPOSITION_COMPONENT (coding, XINT (ch));
5512 } 6406 }
5513 } 6407 }
5514 else /* INTEGERP (val) */ 6408 else if (INTEGERP (components))
5515 CODING_ADD_COMPOSITION_COMPONENT (coding, XINT (val)); 6409 {
6410 len = 1;
6411 *buf++ = XINT (components);
6412 }
6413 else if (CONSP (components))
6414 {
6415 for (len = 0; CONSP (components);
6416 len++, components = XCDR (components))
6417 *buf++ = XINT (XCAR (components));
6418 }
6419 else
6420 abort ();
6421 *head -= len;
5516 } 6422 }
5517 CODING_ADD_COMPOSITION_END (coding, end - from);
5518 } 6423 }
5519 start = end;
5520 }
5521 while (start < to
5522 && find_composition (start, to, &start, &end, &prop, obj)
5523 && end <= to);
5524 6424
5525 /* Make coding->cmp_data point to the first memory block. */ 6425 if (find_composition (end, limit, &start, &end, &prop,
5526 while (coding->cmp_data->prev) 6426 coding->src_object)
5527 coding->cmp_data = coding->cmp_data->prev; 6427 && end <= limit)
5528 coding->cmp_data_start = 0; 6428 *stop = start;
6429 else
6430 *stop = limit;
6431 }
6432 return buf;
5529} 6433}
5530 6434
5531/* Reflect the saved information about compositions to OBJ.
5532 CODING->cmp_data points to a memory block for the information. OBJ
5533 is a buffer or a string, defaults to the current buffer. */
5534 6435
5535void 6436/* Extract an annotation datum from a text property `charset' at POS of
5536coding_restore_composition (coding, obj) 6437 CODING->src_object (buffer of string), store the data in BUF, set
5537 struct coding_system *coding; 6438 *STOP to the position where the value of `charset' property changes
5538 Lisp_Object obj; 6439 (limiting by LIMIT), and return the address of the next element of
5539{ 6440 BUF.
5540 struct composition_data *cmp_data = coding->cmp_data;
5541
5542 if (!cmp_data)
5543 return;
5544
5545 while (cmp_data->prev)
5546 cmp_data = cmp_data->prev;
5547
5548 while (cmp_data)
5549 {
5550 int i;
5551 6441
5552 for (i = 0; i < cmp_data->used && cmp_data->data[i] > 0; 6442 If the property value is nil, set *STOP to the position where the
5553 i += cmp_data->data[i]) 6443 property value is non-nil (limiting by LIMIT), and return BUF. */
5554 {
5555 int *data = cmp_data->data + i;
5556 enum composition_method method = (enum composition_method) data[3];
5557 Lisp_Object components;
5558 6444
5559 if (data[0] < 0 || i + data[0] > cmp_data->used) 6445static INLINE int *
5560 /* Invalid composition data. */ 6446handle_charset_annotation (pos, limit, coding, buf, stop)
5561 break; 6447 EMACS_INT pos, limit;
6448 struct coding_system *coding;
6449 int *buf;
6450 EMACS_INT *stop;
6451{
6452 Lisp_Object val, next;
6453 int id;
5562 6454
5563 if (method == COMPOSITION_RELATIVE) 6455 val = Fget_text_property (make_number (pos), Qcharset, coding->src_object);
5564 components = Qnil; 6456 if (! NILP (val) && CHARSETP (val))
5565 else 6457 id = XINT (CHARSET_SYMBOL_ID (val));
5566 { 6458 else
5567 int len = data[0] - 4, j; 6459 id = -1;
5568 Lisp_Object args[MAX_COMPOSITION_COMPONENTS * 2 - 1]; 6460 ADD_CHARSET_DATA (buf, 0, id);
5569 6461 next = Fnext_single_property_change (make_number (pos), Qcharset,
5570 if (method == COMPOSITION_WITH_RULE_ALTCHARS 6462 coding->src_object,
5571 && len % 2 == 0) 6463 make_number (limit));
5572 len --; 6464 *stop = XINT (next);
5573 if (len < 1) 6465 return buf;
5574 /* Invalid composition data. */
5575 break;
5576 for (j = 0; j < len; j++)
5577 args[j] = make_number (data[4 + j]);
5578 components = (method == COMPOSITION_WITH_ALTCHARS
5579 ? Fstring (len, args)
5580 : Fvector (len, args));
5581 }
5582 compose_text (data[1], data[2], components, Qnil, obj);
5583 }
5584 cmp_data = cmp_data->next;
5585 }
5586} 6466}
5587 6467
5588/* Decode (if ENCODEP is zero) or encode (if ENCODEP is nonzero) the
5589 text from FROM to TO (byte positions are FROM_BYTE and TO_BYTE) by
5590 coding system CODING, and return the status code of code conversion
5591 (currently, this value has no meaning).
5592 6468
5593 How many characters (and bytes) are converted to how many 6469static void
5594 characters (and bytes) are recorded in members of the structure 6470consume_chars (coding, translation_table, max_lookup)
5595 CODING.
5596
5597 If REPLACE is nonzero, we do various things as if the original text
5598 is deleted and a new text is inserted. See the comments in
5599 replace_range (insdel.c) to know what we are doing.
5600
5601 If REPLACE is zero, it is assumed that the source text is unibyte.
5602 Otherwise, it is assumed that the source text is multibyte. */
5603
5604int
5605code_convert_region (from, from_byte, to, to_byte, coding, encodep, replace)
5606 int from, from_byte, to, to_byte, encodep, replace;
5607 struct coding_system *coding; 6471 struct coding_system *coding;
6472 Lisp_Object translation_table;
6473 int max_lookup;
5608{ 6474{
5609 int len = to - from, len_byte = to_byte - from_byte; 6475 int *buf = coding->charbuf;
5610 int nchars_del = 0, nbytes_del = 0; 6476 int *buf_end = coding->charbuf + coding->charbuf_size;
5611 int require, inserted, inserted_byte; 6477 const unsigned char *src = coding->source + coding->consumed;
5612 int head_skip, tail_skip, total_skip = 0; 6478 const unsigned char *src_end = coding->source + coding->src_bytes;
5613 Lisp_Object saved_coding_symbol; 6479 EMACS_INT pos = coding->src_pos + coding->consumed_char;
5614 int first = 1; 6480 EMACS_INT end_pos = coding->src_pos + coding->src_chars;
5615 unsigned char *src, *dst; 6481 int multibytep = coding->src_multibyte;
5616 Lisp_Object deletion; 6482 Lisp_Object eol_type;
5617 int orig_point = PT, orig_len = len; 6483 int c;
5618 int prev_Z; 6484 EMACS_INT stop, stop_composition, stop_charset;
5619 int multibyte_p = !NILP (current_buffer->enable_multibyte_characters); 6485 int *lookup_buf = NULL;
6486
6487 if (! NILP (translation_table))
6488 lookup_buf = alloca (sizeof (int) * max_lookup);
6489
6490 eol_type = CODING_ID_EOL_TYPE (coding->id);
6491 if (VECTORP (eol_type))
6492 eol_type = Qunix;
5620 6493
5621 deletion = Qnil; 6494 /* Note: composition handling is not yet implemented. */
5622 saved_coding_symbol = coding->symbol; 6495 coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
5623 6496
5624 if (from < PT && PT < to) 6497 if (NILP (coding->src_object))
6498 stop = stop_composition = stop_charset = end_pos;
6499 else
5625 { 6500 {
5626 TEMP_SET_PT_BOTH (from, from_byte); 6501 if (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK)
5627 orig_point = from; 6502 stop = stop_composition = pos;
6503 else
6504 stop = stop_composition = end_pos;
6505 if (coding->common_flags & CODING_ANNOTATE_CHARSET_MASK)
6506 stop = stop_charset = pos;
6507 else
6508 stop_charset = end_pos;
5628 } 6509 }
5629 6510
5630 if (replace) 6511 /* Compensate for CRLF and conversion. */
6512 buf_end -= 1 + MAX_ANNOTATION_LENGTH;
6513 while (buf < buf_end)
5631 { 6514 {
5632 int saved_from = from; 6515 Lisp_Object trans;
5633 int saved_inhibit_modification_hooks;
5634 6516
5635 prepare_to_modify_buffer (from, to, &from); 6517 if (pos == stop)
5636 if (saved_from != from)
5637 { 6518 {
5638 to = from + len; 6519 if (pos == end_pos)
5639 from_byte = CHAR_TO_BYTE (from), to_byte = CHAR_TO_BYTE (to); 6520 break;
5640 len_byte = to_byte - from_byte; 6521 if (pos == stop_composition)
6522 buf = handle_composition_annotation (pos, end_pos, coding,
6523 buf, &stop_composition);
6524 if (pos == stop_charset)
6525 buf = handle_charset_annotation (pos, end_pos, coding,
6526 buf, &stop_charset);
6527 stop = (stop_composition < stop_charset
6528 ? stop_composition : stop_charset);
5641 } 6529 }
5642 6530
5643 /* The code conversion routine can not preserve text properties 6531 if (! multibytep)
5644 for now. So, we must remove all text properties in the 6532 {
5645 region. Here, we must suppress all modification hooks. */ 6533 EMACS_INT bytes;
5646 saved_inhibit_modification_hooks = inhibit_modification_hooks;
5647 inhibit_modification_hooks = 1;
5648 Fset_text_properties (make_number (from), make_number (to), Qnil, Qnil);
5649 inhibit_modification_hooks = saved_inhibit_modification_hooks;
5650 }
5651
5652 coding->heading_ascii = 0;
5653
5654 if (! encodep && CODING_REQUIRE_DETECTION (coding))
5655 {
5656 /* We must detect encoding of text and eol format. */
5657 6534
5658 if (from < GPT && to > GPT) 6535 if (coding->encoder == encode_coding_raw_text)
5659 move_gap_both (from, from_byte); 6536 c = *src++, pos++;
5660 if (coding->type == coding_type_undecided) 6537 else if ((bytes = MULTIBYTE_LENGTH (src, src_end)) > 0)
6538 c = STRING_CHAR_ADVANCE (src), pos += bytes;
6539 else
6540 c = BYTE8_TO_CHAR (*src), src++, pos++;
6541 }
6542 else
6543 c = STRING_CHAR_ADVANCE (src), pos++;
6544 if ((c == '\r') && (coding->mode & CODING_MODE_SELECTIVE_DISPLAY))
6545 c = '\n';
6546 if (! EQ (eol_type, Qunix))
5661 { 6547 {
5662 detect_coding (coding, BYTE_POS_ADDR (from_byte), len_byte); 6548 if (c == '\n')
5663 if (coding->type == coding_type_undecided)
5664 { 6549 {
5665 /* It seems that the text contains only ASCII, but we 6550 if (EQ (eol_type, Qdos))
5666 should not leave it undecided because the deeper 6551 *buf++ = '\r';
5667 decoding routine (decode_coding) tries to detect the 6552 else
5668 encodings again in vain. */ 6553 c = '\r';
5669 coding->type = coding_type_emacs_mule;
5670 coding->category_idx = CODING_CATEGORY_IDX_EMACS_MULE;
5671 /* As emacs-mule decoder will handle composition, we
5672 need this setting to allocate coding->cmp_data
5673 later. */
5674 coding->composing = COMPOSITION_NO;
5675 } 6554 }
5676 } 6555 }
5677 if (coding->eol_type == CODING_EOL_UNDECIDED 6556
5678 && coding->type != coding_type_ccl) 6557 trans = Qnil;
6558 LOOKUP_TRANSLATION_TABLE (translation_table, c, trans);
6559 if (NILP (trans))
6560 *buf++ = c;
6561 else
5679 { 6562 {
5680 detect_eol (coding, BYTE_POS_ADDR (from_byte), len_byte); 6563 int from_nchars = 1, to_nchars = 1;
5681 if (coding->eol_type == CODING_EOL_UNDECIDED) 6564 int *lookup_buf_end;
5682 coding->eol_type = CODING_EOL_LF; 6565 const unsigned char *p = src;
5683 /* We had better recover the original eol format if we 6566 int i;
5684 encounter an inconsistent eol format while decoding. */ 6567
5685 coding->mode |= CODING_MODE_INHIBIT_INCONSISTENT_EOL; 6568 lookup_buf[0] = c;
6569 for (i = 1; i < max_lookup && p < src_end; i++)
6570 lookup_buf[i] = STRING_CHAR_ADVANCE (p);
6571 lookup_buf_end = lookup_buf + i;
6572 trans = get_translation (trans, lookup_buf, lookup_buf_end, 1,
6573 &from_nchars, &to_nchars);
6574 if (EQ (trans, Qt)
6575 || buf + to_nchars > buf_end)
6576 break;
6577 *buf++ = *lookup_buf;
6578 for (i = 1; i < to_nchars; i++)
6579 *buf++ = XINT (AREF (trans, i));
6580 for (i = 1; i < from_nchars; i++, pos++)
6581 src += MULTIBYTE_LENGTH_NO_CHECK (src);
5686 } 6582 }
5687 } 6583 }
5688 6584
5689 /* Now we convert the text. */ 6585 coding->consumed = src - coding->source;
5690 6586 coding->consumed_char = pos - coding->src_pos;
5691 /* For encoding, we must process pre-write-conversion in advance. */ 6587 coding->charbuf_used = buf - coding->charbuf;
5692 if (! inhibit_pre_post_conversion 6588 coding->chars_at_source = 0;
5693 && encodep 6589}
5694 && SYMBOLP (coding->pre_write_conversion)
5695 && ! NILP (Ffboundp (coding->pre_write_conversion)))
5696 {
5697 /* The function in pre-write-conversion may put a new text in a
5698 new buffer. */
5699 struct buffer *prev = current_buffer;
5700 Lisp_Object new;
5701
5702 record_unwind_protect (code_convert_region_unwind,
5703 Fcons (Vlast_coding_system_used, Qnil));
5704 /* We should not call any more pre-write/post-read-conversion
5705 functions while this pre-write-conversion is running. */
5706 inhibit_pre_post_conversion = 1;
5707 call2 (coding->pre_write_conversion,
5708 make_number (from), make_number (to));
5709 inhibit_pre_post_conversion = 0;
5710 /* Discard the unwind protect. */
5711 specpdl_ptr--;
5712 6590
5713 if (current_buffer != prev)
5714 {
5715 len = ZV - BEGV;
5716 new = Fcurrent_buffer ();
5717 set_buffer_internal_1 (prev);
5718 del_range_2 (from, from_byte, to, to_byte, 0);
5719 TEMP_SET_PT_BOTH (from, from_byte);
5720 insert_from_buffer (XBUFFER (new), 1, len, 0);
5721 Fkill_buffer (new);
5722 if (orig_point >= to)
5723 orig_point += len - orig_len;
5724 else if (orig_point > from)
5725 orig_point = from;
5726 orig_len = len;
5727 to = from + len;
5728 from_byte = CHAR_TO_BYTE (from);
5729 to_byte = CHAR_TO_BYTE (to);
5730 len_byte = to_byte - from_byte;
5731 TEMP_SET_PT_BOTH (from, from_byte);
5732 }
5733 }
5734 6591
5735 if (replace) 6592/* Encode the text at CODING->src_object into CODING->dst_object.
5736 { 6593 CODING->src_object is a buffer or a string.
5737 if (! EQ (current_buffer->undo_list, Qt)) 6594 CODING->dst_object is a buffer or nil.
5738 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
5739 else
5740 {
5741 nchars_del = to - from;
5742 nbytes_del = to_byte - from_byte;
5743 }
5744 }
5745 6595
5746 if (coding->composing != COMPOSITION_DISABLED) 6596 If CODING->src_object is a buffer, it must be the current buffer.
5747 { 6597 In this case, if CODING->src_pos is positive, it is a position of
5748 if (encodep) 6598 the source text in the buffer, otherwise. the source text is in the
5749 coding_save_composition (coding, from, to, Fcurrent_buffer ()); 6599 gap area of the buffer, and coding->src_pos specifies the offset of
5750 else 6600 the text from GPT (which must be the same as PT). If this is the
5751 coding_allocate_composition_data (coding, from); 6601 same buffer as CODING->dst_object, CODING->src_pos must be
5752 } 6602 negative and CODING should not have `pre-write-conversion'.
5753 6603
5754 /* Try to skip the heading and tailing ASCIIs. We can't skip them 6604 If CODING->src_object is a string, CODING should not have
5755 if we must run CCL program or there are compositions to 6605 `pre-write-conversion'.
5756 encode. */
5757 if (coding->type != coding_type_ccl
5758 && (! coding->cmp_data || coding->cmp_data->used == 0))
5759 {
5760 int from_byte_orig = from_byte, to_byte_orig = to_byte;
5761 6606
5762 if (from < GPT && GPT < to) 6607 If CODING->dst_object is a buffer, the encoded data is inserted at
5763 move_gap_both (from, from_byte); 6608 the current point of that buffer.
5764 SHRINK_CONVERSION_REGION (&from_byte, &to_byte, coding, NULL, encodep);
5765 if (from_byte == to_byte
5766 && (encodep || NILP (coding->post_read_conversion))
5767 && ! CODING_REQUIRE_FLUSHING (coding))
5768 {
5769 coding->produced = len_byte;
5770 coding->produced_char = len;
5771 if (!replace)
5772 /* We must record and adjust for this new text now. */
5773 adjust_after_insert (from, from_byte_orig, to, to_byte_orig, len);
5774 coding_free_composition_data (coding);
5775 return 0;
5776 }
5777
5778 head_skip = from_byte - from_byte_orig;
5779 tail_skip = to_byte_orig - to_byte;
5780 total_skip = head_skip + tail_skip;
5781 from += head_skip;
5782 to -= tail_skip;
5783 len -= total_skip; len_byte -= total_skip;
5784 }
5785
5786 /* For conversion, we must put the gap before the text in addition to
5787 making the gap larger for efficient decoding. The required gap
5788 size starts from 2000 which is the magic number used in make_gap.
5789 But, after one batch of conversion, it will be incremented if we
5790 find that it is not enough . */
5791 require = 2000;
5792
5793 if (GAP_SIZE < require)
5794 make_gap (require - GAP_SIZE);
5795 move_gap_both (from, from_byte);
5796
5797 inserted = inserted_byte = 0;
5798
5799 GAP_SIZE += len_byte;
5800 ZV -= len;
5801 Z -= len;
5802 ZV_BYTE -= len_byte;
5803 Z_BYTE -= len_byte;
5804
5805 if (GPT - BEG < BEG_UNCHANGED)
5806 BEG_UNCHANGED = GPT - BEG;
5807 if (Z - GPT < END_UNCHANGED)
5808 END_UNCHANGED = Z - GPT;
5809
5810 if (!encodep && coding->src_multibyte)
5811 {
5812 /* Decoding routines expects that the source text is unibyte.
5813 We must convert 8-bit characters of multibyte form to
5814 unibyte. */
5815 int len_byte_orig = len_byte;
5816 len_byte = str_as_unibyte (GAP_END_ADDR - len_byte, len_byte);
5817 if (len_byte < len_byte_orig)
5818 safe_bcopy (GAP_END_ADDR - len_byte_orig, GAP_END_ADDR - len_byte,
5819 len_byte);
5820 coding->src_multibyte = 0;
5821 }
5822
5823 for (;;)
5824 {
5825 int result;
5826
5827 /* The buffer memory is now:
5828 +--------+converted-text+---------+-------original-text-------+---+
5829 |<-from->|<--inserted-->|---------|<--------len_byte--------->|---|
5830 |<---------------------- GAP ----------------------->| */
5831 src = GAP_END_ADDR - len_byte;
5832 dst = GPT_ADDR + inserted_byte;
5833
5834 if (encodep)
5835 result = encode_coding (coding, src, dst, len_byte, 0);
5836 else
5837 {
5838 if (coding->composing != COMPOSITION_DISABLED)
5839 coding->cmp_data->char_offset = from + inserted;
5840 result = decode_coding (coding, src, dst, len_byte, 0);
5841 }
5842 6609
5843 /* The buffer memory is now: 6610 If CODING->dst_object is nil, the encoded data is placed at the
5844 +--------+-------converted-text----+--+------original-text----+---+ 6611 memory area specified by CODING->destination. */
5845 |<-from->|<-inserted->|<-produced->|--|<-(len_byte-consumed)->|---|
5846 |<---------------------- GAP ----------------------->| */
5847 6612
5848 inserted += coding->produced_char; 6613static int
5849 inserted_byte += coding->produced; 6614encode_coding (coding)
5850 len_byte -= coding->consumed; 6615 struct coding_system *coding;
6616{
6617 Lisp_Object attrs;
6618 Lisp_Object translation_table;
6619 int max_lookup;
5851 6620
5852 if (result == CODING_FINISH_INSUFFICIENT_CMP) 6621 attrs = CODING_ID_ATTRS (coding->id);
5853 { 6622 if (coding->encoder == encode_coding_raw_text)
5854 coding_allocate_composition_data (coding, from + inserted); 6623 translation_table = Qnil, max_lookup = 0;
5855 continue; 6624 else
5856 } 6625 translation_table = get_translation_table (attrs, 1, &max_lookup);
5857 6626
5858 src += coding->consumed; 6627 if (BUFFERP (coding->dst_object))
5859 dst += coding->produced; 6628 {
6629 set_buffer_internal (XBUFFER (coding->dst_object));
6630 coding->dst_multibyte
6631 = ! NILP (current_buffer->enable_multibyte_characters);
6632 }
5860 6633
5861 if (result == CODING_FINISH_NORMAL) 6634 coding->consumed = coding->consumed_char = 0;
5862 { 6635 coding->produced = coding->produced_char = 0;
5863 src += len_byte; 6636 record_conversion_result (coding, CODING_RESULT_SUCCESS);
5864 break; 6637 coding->errors = 0;
5865 }
5866 if (! encodep && result == CODING_FINISH_INCONSISTENT_EOL)
5867 {
5868 unsigned char *pend = dst, *p = pend - inserted_byte;
5869 Lisp_Object eol_type;
5870 6638
5871 /* Encode LFs back to the original eol format (CR or CRLF). */ 6639 ALLOC_CONVERSION_WORK_AREA (coding);
5872 if (coding->eol_type == CODING_EOL_CR)
5873 {
5874 while (p < pend) if (*p++ == '\n') p[-1] = '\r';
5875 }
5876 else
5877 {
5878 int count = 0;
5879 6640
5880 while (p < pend) if (*p++ == '\n') count++; 6641 do {
5881 if (src - dst < count) 6642 coding_set_source (coding);
5882 { 6643 consume_chars (coding, translation_table, max_lookup);
5883 /* We don't have sufficient room for encoding LFs 6644 coding_set_destination (coding);
5884 back to CRLF. We must record converted and 6645 (*(coding->encoder)) (coding);
5885 not-yet-converted text back to the buffer 6646 } while (coding->consumed_char < coding->src_chars);
5886 content, enlarge the gap, then record them out of
5887 the buffer contents again. */
5888 int add = len_byte + inserted_byte;
5889
5890 GAP_SIZE -= add;
5891 ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add;
5892 GPT += inserted_byte; GPT_BYTE += inserted_byte;
5893 make_gap (count - GAP_SIZE);
5894 GAP_SIZE += add;
5895 ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add;
5896 GPT -= inserted_byte; GPT_BYTE -= inserted_byte;
5897 /* Don't forget to update SRC, DST, and PEND. */
5898 src = GAP_END_ADDR - len_byte;
5899 dst = GPT_ADDR + inserted_byte;
5900 pend = dst;
5901 }
5902 inserted += count;
5903 inserted_byte += count;
5904 coding->produced += count;
5905 p = dst = pend + count;
5906 while (count)
5907 {
5908 *--p = *--pend;
5909 if (*p == '\n') count--, *--p = '\r';
5910 }
5911 }
5912 6647
5913 /* Suppress eol-format conversion in the further conversion. */ 6648 if (BUFFERP (coding->dst_object) && coding->produced_char > 0)
5914 coding->eol_type = CODING_EOL_LF; 6649 insert_from_gap (coding->produced_char, coding->produced);
5915 6650
5916 /* Set the coding system symbol to that for Unix-like EOL. */ 6651 return (coding->result);
5917 eol_type = Fget (saved_coding_symbol, Qeol_type); 6652}
5918 if (VECTORP (eol_type)
5919 && XVECTOR (eol_type)->size == 3
5920 && SYMBOLP (XVECTOR (eol_type)->contents[CODING_EOL_LF]))
5921 coding->symbol = XVECTOR (eol_type)->contents[CODING_EOL_LF];
5922 else
5923 coding->symbol = saved_coding_symbol;
5924 6653
5925 continue;
5926 }
5927 if (len_byte <= 0)
5928 {
5929 if (coding->type != coding_type_ccl
5930 || coding->mode & CODING_MODE_LAST_BLOCK)
5931 break;
5932 coding->mode |= CODING_MODE_LAST_BLOCK;
5933 continue;
5934 }
5935 if (result == CODING_FINISH_INSUFFICIENT_SRC)
5936 {
5937 /* The source text ends in invalid codes. Let's just
5938 make them valid buffer contents, and finish conversion. */
5939 if (multibyte_p)
5940 {
5941 unsigned char *start = dst;
5942 6654
5943 inserted += len_byte; 6655/* Name (or base name) of work buffer for code conversion. */
5944 while (len_byte--) 6656static Lisp_Object Vcode_conversion_workbuf_name;
5945 {
5946 int c = *src++;
5947 dst += CHAR_STRING (c, dst);
5948 }
5949 6657
5950 inserted_byte += dst - start; 6658/* A working buffer used by the top level conversion. Once it is
5951 } 6659 created, it is never destroyed. It has the name
5952 else 6660 Vcode_conversion_workbuf_name. The other working buffers are
5953 { 6661 destroyed after the use is finished, and their names are modified
5954 inserted += len_byte; 6662 versions of Vcode_conversion_workbuf_name. */
5955 inserted_byte += len_byte; 6663static Lisp_Object Vcode_conversion_reused_workbuf;
5956 while (len_byte--)
5957 *dst++ = *src++;
5958 }
5959 break;
5960 }
5961 if (result == CODING_FINISH_INTERRUPT)
5962 {
5963 /* The conversion procedure was interrupted by a user. */
5964 break;
5965 }
5966 /* Now RESULT == CODING_FINISH_INSUFFICIENT_DST */
5967 if (coding->consumed < 1)
5968 {
5969 /* It's quite strange to require more memory without
5970 consuming any bytes. Perhaps CCL program bug. */
5971 break;
5972 }
5973 if (first)
5974 {
5975 /* We have just done the first batch of conversion which was
5976 stopped because of insufficient gap. Let's reconsider the
5977 required gap size (i.e. SRT - DST) now.
5978 6664
5979 We have converted ORIG bytes (== coding->consumed) into 6665/* 1 iff Vcode_conversion_reused_workbuf is already in use. */
5980 NEW bytes (coding->produced). To convert the remaining 6666static int reused_workbuf_in_use;
5981 LEN bytes, we may need REQUIRE bytes of gap, where:
5982 REQUIRE + LEN_BYTE = LEN_BYTE * (NEW / ORIG)
5983 REQUIRE = LEN_BYTE * (NEW - ORIG) / ORIG
5984 Here, we are sure that NEW >= ORIG. */
5985 6667
5986 if (coding->produced <= coding->consumed)
5987 {
5988 /* This happens because of CCL-based coding system with
5989 eol-type CRLF. */
5990 require = 0;
5991 }
5992 else
5993 {
5994 float ratio = coding->produced - coding->consumed;
5995 ratio /= coding->consumed;
5996 require = len_byte * ratio;
5997 }
5998 first = 0;
5999 }
6000 if ((src - dst) < (require + 2000))
6001 {
6002 /* See the comment above the previous call of make_gap. */
6003 int add = len_byte + inserted_byte;
6004 6668
6005 GAP_SIZE -= add; 6669/* Return a working buffer of code convesion. MULTIBYTE specifies the
6006 ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add; 6670 multibyteness of returning buffer. */
6007 GPT += inserted_byte; GPT_BYTE += inserted_byte;
6008 make_gap (require + 2000);
6009 GAP_SIZE += add;
6010 ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add;
6011 GPT -= inserted_byte; GPT_BYTE -= inserted_byte;
6012 }
6013 }
6014 if (src - dst > 0) *dst = 0; /* Put an anchor. */
6015 6671
6016 if (encodep && coding->dst_multibyte) 6672static Lisp_Object
6017 { 6673make_conversion_work_buffer (multibyte)
6018 /* The output is unibyte. We must convert 8-bit characters to 6674 int multibyte;
6019 multibyte form. */ 6675{
6020 if (inserted_byte * 2 > GAP_SIZE) 6676 Lisp_Object name, workbuf;
6021 { 6677 struct buffer *current;
6022 GAP_SIZE -= inserted_byte;
6023 ZV += inserted_byte; Z += inserted_byte;
6024 ZV_BYTE += inserted_byte; Z_BYTE += inserted_byte;
6025 GPT += inserted_byte; GPT_BYTE += inserted_byte;
6026 make_gap (inserted_byte - GAP_SIZE);
6027 GAP_SIZE += inserted_byte;
6028 ZV -= inserted_byte; Z -= inserted_byte;
6029 ZV_BYTE -= inserted_byte; Z_BYTE -= inserted_byte;
6030 GPT -= inserted_byte; GPT_BYTE -= inserted_byte;
6031 }
6032 inserted_byte = str_to_multibyte (GPT_ADDR, GAP_SIZE, inserted_byte);
6033 }
6034 6678
6035 /* If we shrank the conversion area, adjust it now. */ 6679 if (reused_workbuf_in_use++)
6036 if (total_skip > 0)
6037 { 6680 {
6038 if (tail_skip > 0) 6681 name = Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name, Qnil);
6039 safe_bcopy (GAP_END_ADDR, GPT_ADDR + inserted_byte, tail_skip); 6682 workbuf = Fget_buffer_create (name);
6040 inserted += total_skip; inserted_byte += total_skip;
6041 GAP_SIZE += total_skip;
6042 GPT -= head_skip; GPT_BYTE -= head_skip;
6043 ZV -= total_skip; ZV_BYTE -= total_skip;
6044 Z -= total_skip; Z_BYTE -= total_skip;
6045 from -= head_skip; from_byte -= head_skip;
6046 to += tail_skip; to_byte += tail_skip;
6047 } 6683 }
6048
6049 prev_Z = Z;
6050 if (! EQ (current_buffer->undo_list, Qt))
6051 adjust_after_replace (from, from_byte, deletion, inserted, inserted_byte);
6052 else 6684 else
6053 adjust_after_replace_noundo (from, from_byte, nchars_del, nbytes_del,
6054 inserted, inserted_byte);
6055 inserted = Z - prev_Z;
6056
6057 if (!encodep && coding->cmp_data && coding->cmp_data->used)
6058 coding_restore_composition (coding, Fcurrent_buffer ());
6059 coding_free_composition_data (coding);
6060
6061 if (! inhibit_pre_post_conversion
6062 && ! encodep && ! NILP (coding->post_read_conversion))
6063 { 6685 {
6064 Lisp_Object val; 6686 name = Vcode_conversion_workbuf_name;
6065 Lisp_Object saved_coding_system; 6687 workbuf = Fget_buffer_create (name);
6688 if (NILP (Vcode_conversion_reused_workbuf))
6689 Vcode_conversion_reused_workbuf = workbuf;
6690 }
6691 current = current_buffer;
6692 set_buffer_internal (XBUFFER (workbuf));
6693 Ferase_buffer ();
6694 current_buffer->undo_list = Qt;
6695 current_buffer->enable_multibyte_characters = multibyte ? Qt : Qnil;
6696 set_buffer_internal (current);
6697 return workbuf;
6698}
6066 6699
6067 if (from != PT)
6068 TEMP_SET_PT_BOTH (from, from_byte);
6069 prev_Z = Z;
6070 record_unwind_protect (code_convert_region_unwind,
6071 Fcons (Vlast_coding_system_used, Qnil));
6072 saved_coding_system = Vlast_coding_system_used;
6073 Vlast_coding_system_used = coding->symbol;
6074 /* We should not call any more pre-write/post-read-conversion
6075 functions while this post-read-conversion is running. */
6076 inhibit_pre_post_conversion = 1;
6077 val = call1 (coding->post_read_conversion, make_number (inserted));
6078 inhibit_pre_post_conversion = 0;
6079 coding->symbol = Vlast_coding_system_used;
6080 Vlast_coding_system_used = saved_coding_system;
6081 /* Discard the unwind protect. */
6082 specpdl_ptr--;
6083 CHECK_NUMBER (val);
6084 inserted += Z - prev_Z;
6085 }
6086
6087 if (orig_point >= from)
6088 {
6089 if (orig_point >= from + orig_len)
6090 orig_point += inserted - orig_len;
6091 else
6092 orig_point = from;
6093 TEMP_SET_PT (orig_point);
6094 }
6095 6700
6096 if (replace) 6701static Lisp_Object
6702code_conversion_restore (arg)
6703 Lisp_Object arg;
6704{
6705 Lisp_Object current, workbuf;
6706 struct gcpro gcpro1;
6707
6708 GCPRO1 (arg);
6709 current = XCAR (arg);
6710 workbuf = XCDR (arg);
6711 if (! NILP (workbuf))
6097 { 6712 {
6098 signal_after_change (from, to - from, inserted); 6713 if (EQ (workbuf, Vcode_conversion_reused_workbuf))
6099 update_compositions (from, from + inserted, CHECK_BORDER); 6714 reused_workbuf_in_use = 0;
6715 else if (! NILP (Fbuffer_live_p (workbuf)))
6716 Fkill_buffer (workbuf);
6100 } 6717 }
6718 set_buffer_internal (XBUFFER (current));
6719 UNGCPRO;
6720 return Qnil;
6721}
6101 6722
6102 { 6723Lisp_Object
6103 coding->consumed = to_byte - from_byte; 6724code_conversion_save (with_work_buf, multibyte)
6104 coding->consumed_char = to - from; 6725 int with_work_buf, multibyte;
6105 coding->produced = inserted_byte; 6726{
6106 coding->produced_char = inserted; 6727 Lisp_Object workbuf = Qnil;
6107 }
6108 6728
6109 return 0; 6729 if (with_work_buf)
6730 workbuf = make_conversion_work_buffer (multibyte);
6731 record_unwind_protect (code_conversion_restore,
6732 Fcons (Fcurrent_buffer (), workbuf));
6733 return workbuf;
6110} 6734}
6111 6735
6112/* Name (or base name) of work buffer for code conversion. */ 6736int
6113static Lisp_Object Vcode_conversion_workbuf_name; 6737decode_coding_gap (coding, chars, bytes)
6738 struct coding_system *coding;
6739 EMACS_INT chars, bytes;
6740{
6741 int count = specpdl_ptr - specpdl;
6742 Lisp_Object attrs;
6743
6744 code_conversion_save (0, 0);
6745
6746 coding->src_object = Fcurrent_buffer ();
6747 coding->src_chars = chars;
6748 coding->src_bytes = bytes;
6749 coding->src_pos = -chars;
6750 coding->src_pos_byte = -bytes;
6751 coding->src_multibyte = chars < bytes;
6752 coding->dst_object = coding->src_object;
6753 coding->dst_pos = PT;
6754 coding->dst_pos_byte = PT_BYTE;
6755 coding->dst_multibyte = ! NILP (current_buffer->enable_multibyte_characters);
6114 6756
6115/* Set the current buffer to the working buffer prepared for 6757 if (CODING_REQUIRE_DETECTION (coding))
6116 code-conversion. MULTIBYTE specifies the multibyteness of the 6758 detect_coding (coding);
6117 buffer. Return the buffer we set if it must be killed after use.
6118 Otherwise return Qnil. */
6119 6759
6120static Lisp_Object 6760 coding->mode |= CODING_MODE_LAST_BLOCK;
6121set_conversion_work_buffer (multibyte) 6761 current_buffer->text->inhibit_shrinking = 1;
6122 int multibyte; 6762 decode_coding (coding);
6123{ 6763 current_buffer->text->inhibit_shrinking = 0;
6124 Lisp_Object buffer, buffer_to_kill;
6125 struct buffer *buf;
6126 6764
6127 buffer = Fget_buffer_create (Vcode_conversion_workbuf_name); 6765 attrs = CODING_ID_ATTRS (coding->id);
6128 buf = XBUFFER (buffer); 6766 if (! NILP (CODING_ATTR_POST_READ (attrs)))
6129 if (buf == current_buffer)
6130 { 6767 {
6131 /* As we are already in the work buffer, we must generate a new 6768 EMACS_INT prev_Z = Z, prev_Z_BYTE = Z_BYTE;
6132 buffer for the work. */ 6769 Lisp_Object val;
6133 Lisp_Object name;
6134 6770
6135 name = Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name, Qnil); 6771 TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte);
6136 buffer = buffer_to_kill = Fget_buffer_create (name); 6772 val = call1 (CODING_ATTR_POST_READ (attrs),
6137 buf = XBUFFER (buffer); 6773 make_number (coding->produced_char));
6774 CHECK_NATNUM (val);
6775 coding->produced_char += Z - prev_Z;
6776 coding->produced += Z_BYTE - prev_Z_BYTE;
6138 } 6777 }
6139 else 6778
6140 buffer_to_kill = Qnil; 6779 unbind_to (count, Qnil);
6141 6780 return coding->result;
6142 delete_all_overlays (buf);
6143 buf->directory = current_buffer->directory;
6144 buf->read_only = Qnil;
6145 buf->filename = Qnil;
6146 buf->undo_list = Qt;
6147 eassert (buf->overlays_before == NULL);
6148 eassert (buf->overlays_after == NULL);
6149 set_buffer_internal (buf);
6150 if (BEG != BEGV || Z != ZV)
6151 Fwiden ();
6152 del_range_2 (BEG, BEG_BYTE, Z, Z_BYTE, 0);
6153 buf->enable_multibyte_characters = multibyte ? Qt : Qnil;
6154 return buffer_to_kill;
6155} 6781}
6156 6782
6157Lisp_Object 6783int
6158run_pre_post_conversion_on_str (str, coding, encodep) 6784encode_coding_gap (coding, chars, bytes)
6159 Lisp_Object str;
6160 struct coding_system *coding; 6785 struct coding_system *coding;
6161 int encodep; 6786 EMACS_INT chars, bytes;
6162{ 6787{
6163 int count = SPECPDL_INDEX (); 6788 int count = specpdl_ptr - specpdl;
6164 struct gcpro gcpro1, gcpro2;
6165 int multibyte = STRING_MULTIBYTE (str);
6166 Lisp_Object old_deactivate_mark;
6167 Lisp_Object buffer_to_kill;
6168 Lisp_Object unwind_arg;
6169
6170 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
6171 /* It is not crucial to specbind this. */
6172 old_deactivate_mark = Vdeactivate_mark;
6173 GCPRO2 (str, old_deactivate_mark);
6174
6175 /* We must insert the contents of STR as is without
6176 unibyte<->multibyte conversion. For that, we adjust the
6177 multibyteness of the working buffer to that of STR. */
6178 buffer_to_kill = set_conversion_work_buffer (multibyte);
6179 if (NILP (buffer_to_kill))
6180 unwind_arg = Fcons (Vlast_coding_system_used, Qnil);
6181 else
6182 unwind_arg = list2 (Vlast_coding_system_used, buffer_to_kill);
6183 record_unwind_protect (code_convert_region_unwind, unwind_arg);
6184 6789
6185 insert_from_string (str, 0, 0, 6790 code_conversion_save (0, 0);
6186 SCHARS (str), SBYTES (str), 0);
6187 UNGCPRO;
6188 inhibit_pre_post_conversion = 1;
6189 if (encodep)
6190 {
6191 struct buffer *prev = current_buffer;
6192 6791
6193 call2 (coding->pre_write_conversion, make_number (BEG), make_number (Z)); 6792 coding->src_object = Fcurrent_buffer ();
6194 if (prev != current_buffer) 6793 coding->src_chars = chars;
6195 /* We must kill the current buffer too. */ 6794 coding->src_bytes = bytes;
6196 Fsetcdr (unwind_arg, Fcons (Fcurrent_buffer (), XCDR (unwind_arg))); 6795 coding->src_pos = -chars;
6197 } 6796 coding->src_pos_byte = -bytes;
6198 else 6797 coding->src_multibyte = chars < bytes;
6199 { 6798 coding->dst_object = coding->src_object;
6200 Vlast_coding_system_used = coding->symbol; 6799 coding->dst_pos = PT;
6201 TEMP_SET_PT_BOTH (BEG, BEG_BYTE); 6800 coding->dst_pos_byte = PT_BYTE;
6202 call1 (coding->post_read_conversion, make_number (Z - BEG)); 6801
6203 coding->symbol = Vlast_coding_system_used; 6802 encode_coding (coding);
6204 } 6803
6205 inhibit_pre_post_conversion = 0; 6804 unbind_to (count, Qnil);
6206 Vdeactivate_mark = old_deactivate_mark; 6805 return coding->result;
6207 str = make_buffer_string (BEG, Z, 1);
6208 return unbind_to (count, str);
6209} 6806}
6210 6807
6211 6808
6212/* Run pre-write-conversion function of CODING on NCHARS/NBYTES 6809/* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in
6213 text in *STR. *SIZE is the allocated bytes for STR. As it 6810 SRC_OBJECT into DST_OBJECT by coding context CODING.
6214 is intended that this function is called from encode_terminal_code, 6811
6215 the pre-write-conversion function is run by safe_call and thus 6812 SRC_OBJECT is a buffer, a string, or Qnil.
6216 "Error during redisplay: ..." is logged when an error occurs. 6813
6814 If it is a buffer, the text is at point of the buffer. FROM and TO
6815 are positions in the buffer.
6816
6817 If it is a string, the text is at the beginning of the string.
6818 FROM and TO are indices to the string.
6819
6820 If it is nil, the text is at coding->source. FROM and TO are
6821 indices to coding->source.
6217 6822
6218 Store the resulting text in *STR and set CODING->produced_char and 6823 DST_OBJECT is a buffer, Qt, or Qnil.
6219 CODING->produced to the number of characters and bytes 6824
6220 respectively. If the size of *STR is too small, enlarge it by 6825 If it is a buffer, the decoded text is inserted at point of the
6221 xrealloc and update *STR and *SIZE. */ 6826 buffer. If the buffer is the same as SRC_OBJECT, the source text
6827 is deleted.
6828
6829 If it is Qt, a string is made from the decoded text, and
6830 set in CODING->dst_object.
6831
6832 If it is Qnil, the decoded text is stored at CODING->destination.
6833 The caller must allocate CODING->dst_bytes bytes at
6834 CODING->destination by xmalloc. If the decoded text is longer than
6835 CODING->dst_bytes, CODING->destination is relocated by xrealloc.
6836 */
6222 6837
6223void 6838void
6224run_pre_write_conversin_on_c_str (str, size, nchars, nbytes, coding) 6839decode_coding_object (coding, src_object, from, from_byte, to, to_byte,
6225 unsigned char **str; 6840 dst_object)
6226 int *size, nchars, nbytes;
6227 struct coding_system *coding; 6841 struct coding_system *coding;
6842 Lisp_Object src_object;
6843 EMACS_INT from, from_byte, to, to_byte;
6844 Lisp_Object dst_object;
6228{ 6845{
6229 struct gcpro gcpro1, gcpro2; 6846 int count = specpdl_ptr - specpdl;
6230 struct buffer *cur = current_buffer; 6847 unsigned char *destination;
6231 struct buffer *prev; 6848 EMACS_INT dst_bytes;
6232 Lisp_Object old_deactivate_mark, old_last_coding_system_used; 6849 EMACS_INT chars = to - from;
6233 Lisp_Object args[3]; 6850 EMACS_INT bytes = to_byte - from_byte;
6234 Lisp_Object buffer_to_kill; 6851 Lisp_Object attrs;
6235 6852 Lisp_Object buffer;
6236 /* It is not crucial to specbind this. */ 6853 int saved_pt = -1, saved_pt_byte;
6237 old_deactivate_mark = Vdeactivate_mark; 6854 int need_marker_adjustment = 0;
6238 old_last_coding_system_used = Vlast_coding_system_used;
6239 GCPRO2 (old_deactivate_mark, old_last_coding_system_used);
6240
6241 /* We must insert the contents of STR as is without
6242 unibyte<->multibyte conversion. For that, we adjust the
6243 multibyteness of the working buffer to that of STR. */
6244 buffer_to_kill = set_conversion_work_buffer (coding->src_multibyte);
6245 insert_1_both (*str, nchars, nbytes, 0, 0, 0);
6246 UNGCPRO;
6247 inhibit_pre_post_conversion = 1;
6248 prev = current_buffer;
6249 args[0] = coding->pre_write_conversion;
6250 args[1] = make_number (BEG);
6251 args[2] = make_number (Z);
6252 safe_call (3, args);
6253 inhibit_pre_post_conversion = 0;
6254 Vdeactivate_mark = old_deactivate_mark;
6255 Vlast_coding_system_used = old_last_coding_system_used;
6256 coding->produced_char = Z - BEG;
6257 coding->produced = Z_BYTE - BEG_BYTE;
6258 if (coding->produced > *size)
6259 {
6260 *size = coding->produced;
6261 *str = xrealloc (*str, *size);
6262 }
6263 if (BEG < GPT && GPT < Z)
6264 move_gap (BEG);
6265 bcopy (BEG_ADDR, *str, coding->produced);
6266 coding->src_multibyte
6267 = ! NILP (current_buffer->enable_multibyte_characters);
6268 if (prev != current_buffer)
6269 Fkill_buffer (Fcurrent_buffer ());
6270 set_buffer_internal (cur);
6271 if (! NILP (buffer_to_kill))
6272 Fkill_buffer (buffer_to_kill);
6273}
6274 6855
6856 buffer = Fcurrent_buffer ();
6275 6857
6276Lisp_Object 6858 if (NILP (dst_object))
6277decode_coding_string (str, coding, nocopy) 6859 {
6278 Lisp_Object str; 6860 destination = coding->destination;
6279 struct coding_system *coding; 6861 dst_bytes = coding->dst_bytes;
6280 int nocopy; 6862 }
6281{
6282 int len;
6283 struct conversion_buffer buf;
6284 int from, to_byte;
6285 Lisp_Object saved_coding_symbol;
6286 int result;
6287 int require_decoding;
6288 int shrinked_bytes = 0;
6289 Lisp_Object newstr;
6290 int consumed, consumed_char, produced, produced_char;
6291
6292 from = 0;
6293 to_byte = SBYTES (str);
6294
6295 saved_coding_symbol = coding->symbol;
6296 coding->src_multibyte = STRING_MULTIBYTE (str);
6297 coding->dst_multibyte = 1;
6298 coding->heading_ascii = 0;
6299 6863
6300 if (CODING_REQUIRE_DETECTION (coding)) 6864 coding->src_object = src_object;
6865 coding->src_chars = chars;
6866 coding->src_bytes = bytes;
6867 coding->src_multibyte = chars < bytes;
6868
6869 if (STRINGP (src_object))
6870 {
6871 coding->src_pos = from;
6872 coding->src_pos_byte = from_byte;
6873 }
6874 else if (BUFFERP (src_object))
6301 { 6875 {
6302 /* See the comments in code_convert_region. */ 6876 set_buffer_internal (XBUFFER (src_object));
6303 if (coding->type == coding_type_undecided) 6877 if (from != GPT)
6878 move_gap_both (from, from_byte);
6879 if (EQ (src_object, dst_object))
6304 { 6880 {
6305 detect_coding (coding, SDATA (str), to_byte); 6881 struct Lisp_Marker *tail;
6306 if (coding->type == coding_type_undecided) 6882
6883 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
6307 { 6884 {
6308 coding->type = coding_type_emacs_mule; 6885 tail->need_adjustment
6309 coding->category_idx = CODING_CATEGORY_IDX_EMACS_MULE; 6886 = tail->charpos == (tail->insertion_type ? from : to);
6310 /* As emacs-mule decoder will handle composition, we 6887 need_marker_adjustment |= tail->need_adjustment;
6311 need this setting to allocate coding->cmp_data
6312 later. */
6313 coding->composing = COMPOSITION_NO;
6314 } 6888 }
6889 saved_pt = PT, saved_pt_byte = PT_BYTE;
6890 TEMP_SET_PT_BOTH (from, from_byte);
6891 del_range_both (from, from_byte, to, to_byte, 1);
6892 coding->src_pos = -chars;
6893 coding->src_pos_byte = -bytes;
6315 } 6894 }
6316 if (coding->eol_type == CODING_EOL_UNDECIDED 6895 else
6317 && coding->type != coding_type_ccl)
6318 { 6896 {
6319 saved_coding_symbol = coding->symbol; 6897 coding->src_pos = from;
6320 detect_eol (coding, SDATA (str), to_byte); 6898 coding->src_pos_byte = from_byte;
6321 if (coding->eol_type == CODING_EOL_UNDECIDED)
6322 coding->eol_type = CODING_EOL_LF;
6323 /* We had better recover the original eol format if we
6324 encounter an inconsistent eol format while decoding. */
6325 coding->mode |= CODING_MODE_INHIBIT_INCONSISTENT_EOL;
6326 } 6899 }
6327 } 6900 }
6328 6901
6329 if (coding->type == coding_type_no_conversion 6902 if (CODING_REQUIRE_DETECTION (coding))
6330 || coding->type == coding_type_raw_text) 6903 detect_coding (coding);
6331 coding->dst_multibyte = 0; 6904 attrs = CODING_ID_ATTRS (coding->id);
6332
6333 require_decoding = CODING_REQUIRE_DECODING (coding);
6334 6905
6335 if (STRING_MULTIBYTE (str)) 6906 if (EQ (dst_object, Qt)
6907 || (! NILP (CODING_ATTR_POST_READ (attrs))
6908 && NILP (dst_object)))
6336 { 6909 {
6337 /* Decoding routines expect the source text to be unibyte. */ 6910 coding->dst_object = code_conversion_save (1, 1);
6338 str = Fstring_as_unibyte (str); 6911 coding->dst_pos = BEG;
6339 to_byte = SBYTES (str); 6912 coding->dst_pos_byte = BEG_BYTE;
6340 nocopy = 1; 6913 coding->dst_multibyte = 1;
6341 coding->src_multibyte = 0;
6342 } 6914 }
6343 6915 else if (BUFFERP (dst_object))
6344 /* Try to skip the heading and tailing ASCIIs. */
6345 if (require_decoding && coding->type != coding_type_ccl)
6346 { 6916 {
6347 SHRINK_CONVERSION_REGION (&from, &to_byte, coding, SDATA (str), 6917 code_conversion_save (0, 0);
6348 0); 6918 coding->dst_object = dst_object;
6349 if (from == to_byte) 6919 coding->dst_pos = BUF_PT (XBUFFER (dst_object));
6350 require_decoding = 0; 6920 coding->dst_pos_byte = BUF_PT_BYTE (XBUFFER (dst_object));
6351 shrinked_bytes = from + (SBYTES (str) - to_byte); 6921 coding->dst_multibyte
6922 = ! NILP (XBUFFER (dst_object)->enable_multibyte_characters);
6352 } 6923 }
6353 6924 else
6354 if (!require_decoding
6355 && !(SYMBOLP (coding->post_read_conversion)
6356 && !NILP (Ffboundp (coding->post_read_conversion))))
6357 { 6925 {
6358 coding->consumed = SBYTES (str); 6926 code_conversion_save (0, 0);
6359 coding->consumed_char = SCHARS (str); 6927 coding->dst_object = Qnil;
6360 if (coding->dst_multibyte) 6928 coding->dst_multibyte = 1;
6361 {
6362 str = Fstring_as_multibyte (str);
6363 nocopy = 1;
6364 }
6365 coding->produced = SBYTES (str);
6366 coding->produced_char = SCHARS (str);
6367 return (nocopy ? str : Fcopy_sequence (str));
6368 } 6929 }
6369 6930
6370 if (coding->composing != COMPOSITION_DISABLED) 6931 decode_coding (coding);
6371 coding_allocate_composition_data (coding, from);
6372 len = decoding_buffer_size (coding, to_byte - from);
6373 allocate_conversion_buffer (buf, len);
6374 6932
6375 consumed = consumed_char = produced = produced_char = 0; 6933 if (BUFFERP (coding->dst_object))
6376 while (1) 6934 set_buffer_internal (XBUFFER (coding->dst_object));
6935
6936 if (! NILP (CODING_ATTR_POST_READ (attrs)))
6377 { 6937 {
6378 result = decode_coding (coding, SDATA (str) + from + consumed, 6938 struct gcpro gcpro1, gcpro2;
6379 buf.data + produced, to_byte - from - consumed, 6939 EMACS_INT prev_Z = Z, prev_Z_BYTE = Z_BYTE;
6380 buf.size - produced); 6940 Lisp_Object val;
6381 consumed += coding->consumed;
6382 consumed_char += coding->consumed_char;
6383 produced += coding->produced;
6384 produced_char += coding->produced_char;
6385 if (result == CODING_FINISH_NORMAL
6386 || result == CODING_FINISH_INTERRUPT
6387 || (result == CODING_FINISH_INSUFFICIENT_SRC
6388 && coding->consumed == 0))
6389 break;
6390 if (result == CODING_FINISH_INSUFFICIENT_CMP)
6391 coding_allocate_composition_data (coding, from + produced_char);
6392 else if (result == CODING_FINISH_INSUFFICIENT_DST)
6393 extend_conversion_buffer (&buf);
6394 else if (result == CODING_FINISH_INCONSISTENT_EOL)
6395 {
6396 Lisp_Object eol_type;
6397 6941
6398 /* Recover the original EOL format. */ 6942 TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte);
6399 if (coding->eol_type == CODING_EOL_CR) 6943 GCPRO2 (coding->src_object, coding->dst_object);
6400 { 6944 val = safe_call1 (CODING_ATTR_POST_READ (attrs),
6401 unsigned char *p; 6945 make_number (coding->produced_char));
6402 for (p = buf.data; p < buf.data + produced; p++) 6946 UNGCPRO;
6403 if (*p == '\n') *p = '\r'; 6947 CHECK_NATNUM (val);
6404 } 6948 coding->produced_char += Z - prev_Z;
6405 else if (coding->eol_type == CODING_EOL_CRLF) 6949 coding->produced += Z_BYTE - prev_Z_BYTE;
6950 }
6951
6952 if (EQ (dst_object, Qt))
6953 {
6954 coding->dst_object = Fbuffer_string ();
6955 }
6956 else if (NILP (dst_object) && BUFFERP (coding->dst_object))
6957 {
6958 set_buffer_internal (XBUFFER (coding->dst_object));
6959 if (dst_bytes < coding->produced)
6960 {
6961 destination
6962 = (unsigned char *) xrealloc (destination, coding->produced);
6963 if (! destination)
6406 { 6964 {
6407 int num_eol = 0; 6965 record_conversion_result (coding,
6408 unsigned char *p0, *p1; 6966 CODING_RESULT_INSUFFICIENT_DST);
6409 for (p0 = buf.data, p1 = p0 + produced; p0 < p1; p0++) 6967 unbind_to (count, Qnil);
6410 if (*p0 == '\n') num_eol++; 6968 return;
6411 if (produced + num_eol >= buf.size)
6412 extend_conversion_buffer (&buf);
6413 for (p0 = buf.data + produced, p1 = p0 + num_eol; p0 > buf.data;)
6414 {
6415 *--p1 = *--p0;
6416 if (*p0 == '\n') *--p1 = '\r';
6417 }
6418 produced += num_eol;
6419 produced_char += num_eol;
6420 } 6969 }
6421 /* Suppress eol-format conversion in the further conversion. */ 6970 if (BEGV < GPT && GPT < BEGV + coding->produced_char)
6422 coding->eol_type = CODING_EOL_LF; 6971 move_gap_both (BEGV, BEGV_BYTE);
6423 6972 bcopy (BEGV_ADDR, destination, coding->produced);
6424 /* Set the coding system symbol to that for Unix-like EOL. */ 6973 coding->destination = destination;
6425 eol_type = Fget (saved_coding_symbol, Qeol_type); 6974 }
6426 if (VECTORP (eol_type) 6975 }
6427 && XVECTOR (eol_type)->size == 3 6976
6428 && SYMBOLP (XVECTOR (eol_type)->contents[CODING_EOL_LF])) 6977 if (saved_pt >= 0)
6429 coding->symbol = XVECTOR (eol_type)->contents[CODING_EOL_LF]; 6978 {
6430 else 6979 /* This is the case of:
6431 coding->symbol = saved_coding_symbol; 6980 (BUFFERP (src_object) && EQ (src_object, dst_object))
6981 As we have moved PT while replacing the original buffer
6982 contents, we must recover it now. */
6983 set_buffer_internal (XBUFFER (src_object));
6984 if (saved_pt < from)
6985 TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte);
6986 else if (saved_pt < from + chars)
6987 TEMP_SET_PT_BOTH (from, from_byte);
6988 else if (! NILP (current_buffer->enable_multibyte_characters))
6989 TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars),
6990 saved_pt_byte + (coding->produced - bytes));
6991 else
6992 TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes),
6993 saved_pt_byte + (coding->produced - bytes));
6432 6994
6995 if (need_marker_adjustment)
6996 {
6997 struct Lisp_Marker *tail;
6433 6998
6999 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
7000 if (tail->need_adjustment)
7001 {
7002 tail->need_adjustment = 0;
7003 if (tail->insertion_type)
7004 {
7005 tail->bytepos = from_byte;
7006 tail->charpos = from;
7007 }
7008 else
7009 {
7010 tail->bytepos = from_byte + coding->produced;
7011 tail->charpos
7012 = (NILP (current_buffer->enable_multibyte_characters)
7013 ? tail->bytepos : from + coding->produced_char);
7014 }
7015 }
6434 } 7016 }
6435 } 7017 }
6436 7018
6437 coding->consumed = consumed; 7019 unbind_to (count, coding->dst_object);
6438 coding->consumed_char = consumed_char;
6439 coding->produced = produced;
6440 coding->produced_char = produced_char;
6441
6442 if (coding->dst_multibyte)
6443 newstr = make_uninit_multibyte_string (produced_char + shrinked_bytes,
6444 produced + shrinked_bytes);
6445 else
6446 newstr = make_uninit_string (produced + shrinked_bytes);
6447 if (from > 0)
6448 STRING_COPYIN (newstr, 0, SDATA (str), from);
6449 STRING_COPYIN (newstr, from, buf.data, produced);
6450 if (shrinked_bytes > from)
6451 STRING_COPYIN (newstr, from + produced,
6452 SDATA (str) + to_byte,
6453 shrinked_bytes - from);
6454 free_conversion_buffer (&buf);
6455
6456 coding->consumed += shrinked_bytes;
6457 coding->consumed_char += shrinked_bytes;
6458 coding->produced += shrinked_bytes;
6459 coding->produced_char += shrinked_bytes;
6460
6461 if (coding->cmp_data && coding->cmp_data->used)
6462 coding_restore_composition (coding, newstr);
6463 coding_free_composition_data (coding);
6464
6465 if (SYMBOLP (coding->post_read_conversion)
6466 && !NILP (Ffboundp (coding->post_read_conversion)))
6467 newstr = run_pre_post_conversion_on_str (newstr, coding, 0);
6468
6469 return newstr;
6470} 7020}
6471 7021
6472Lisp_Object 7022
6473encode_coding_string (str, coding, nocopy) 7023void
6474 Lisp_Object str; 7024encode_coding_object (coding, src_object, from, from_byte, to, to_byte,
7025 dst_object)
6475 struct coding_system *coding; 7026 struct coding_system *coding;
6476 int nocopy; 7027 Lisp_Object src_object;
7028 EMACS_INT from, from_byte, to, to_byte;
7029 Lisp_Object dst_object;
6477{ 7030{
6478 int len; 7031 int count = specpdl_ptr - specpdl;
6479 struct conversion_buffer buf; 7032 EMACS_INT chars = to - from;
6480 int from, to, to_byte; 7033 EMACS_INT bytes = to_byte - from_byte;
6481 int result; 7034 Lisp_Object attrs;
6482 int shrinked_bytes = 0; 7035 Lisp_Object buffer;
6483 Lisp_Object newstr; 7036 int saved_pt = -1, saved_pt_byte;
6484 int consumed, consumed_char, produced, produced_char; 7037 int need_marker_adjustment = 0;
7038 int kill_src_buffer = 0;
7039
7040 buffer = Fcurrent_buffer ();
7041
7042 coding->src_object = src_object;
7043 coding->src_chars = chars;
7044 coding->src_bytes = bytes;
7045 coding->src_multibyte = chars < bytes;
6485 7046
6486 if (SYMBOLP (coding->pre_write_conversion) 7047 attrs = CODING_ID_ATTRS (coding->id);
6487 && !NILP (Ffboundp (coding->pre_write_conversion))) 7048
7049 if (EQ (src_object, dst_object))
6488 { 7050 {
6489 str = run_pre_post_conversion_on_str (str, coding, 1); 7051 struct Lisp_Marker *tail;
6490 /* As STR is just newly generated, we don't have to copy it 7052
6491 anymore. */ 7053 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
6492 nocopy = 1; 7054 {
7055 tail->need_adjustment
7056 = tail->charpos == (tail->insertion_type ? from : to);
7057 need_marker_adjustment |= tail->need_adjustment;
7058 }
6493 } 7059 }
6494 7060
6495 from = 0; 7061 if (! NILP (CODING_ATTR_PRE_WRITE (attrs)))
6496 to = SCHARS (str); 7062 {
6497 to_byte = SBYTES (str); 7063 coding->src_object = code_conversion_save (1, coding->src_multibyte);
7064 set_buffer_internal (XBUFFER (coding->src_object));
7065 if (STRINGP (src_object))
7066 insert_from_string (src_object, from, from_byte, chars, bytes, 0);
7067 else if (BUFFERP (src_object))
7068 insert_from_buffer (XBUFFER (src_object), from, chars, 0);
7069 else
7070 insert_1_both (coding->source + from, chars, bytes, 0, 0, 0);
7071
7072 if (EQ (src_object, dst_object))
7073 {
7074 set_buffer_internal (XBUFFER (src_object));
7075 saved_pt = PT, saved_pt_byte = PT_BYTE;
7076 del_range_both (from, from_byte, to, to_byte, 1);
7077 set_buffer_internal (XBUFFER (coding->src_object));
7078 }
6498 7079
6499 /* Encoding routines determine the multibyteness of the source text 7080 {
6500 by coding->src_multibyte. */ 7081 Lisp_Object args[3];
6501 coding->src_multibyte = SCHARS (str) < SBYTES (str);
6502 coding->dst_multibyte = 0;
6503 if (! CODING_REQUIRE_ENCODING (coding))
6504 goto no_need_of_encoding;
6505 7082
6506 if (coding->composing != COMPOSITION_DISABLED) 7083 args[0] = CODING_ATTR_PRE_WRITE (attrs);
6507 coding_save_composition (coding, from, to, str); 7084 args[1] = make_number (BEG);
7085 args[2] = make_number (Z);
7086 safe_call (3, args);
7087 }
7088 if (XBUFFER (coding->src_object) != current_buffer)
7089 kill_src_buffer = 1;
7090 coding->src_object = Fcurrent_buffer ();
7091 if (BEG != GPT)
7092 move_gap_both (BEG, BEG_BYTE);
7093 coding->src_chars = Z - BEG;
7094 coding->src_bytes = Z_BYTE - BEG_BYTE;
7095 coding->src_pos = BEG;
7096 coding->src_pos_byte = BEG_BYTE;
7097 coding->src_multibyte = Z < Z_BYTE;
7098 }
7099 else if (STRINGP (src_object))
7100 {
7101 code_conversion_save (0, 0);
7102 coding->src_pos = from;
7103 coding->src_pos_byte = from_byte;
7104 }
7105 else if (BUFFERP (src_object))
7106 {
7107 code_conversion_save (0, 0);
7108 set_buffer_internal (XBUFFER (src_object));
7109 if (EQ (src_object, dst_object))
7110 {
7111 saved_pt = PT, saved_pt_byte = PT_BYTE;
7112 coding->src_object = del_range_1 (from, to, 1, 1);
7113 coding->src_pos = 0;
7114 coding->src_pos_byte = 0;
7115 }
7116 else
7117 {
7118 if (from < GPT && to >= GPT)
7119 move_gap_both (from, from_byte);
7120 coding->src_pos = from;
7121 coding->src_pos_byte = from_byte;
7122 }
7123 }
7124 else
7125 code_conversion_save (0, 0);
6508 7126
6509 /* Try to skip the heading and tailing ASCIIs. We can't skip them 7127 if (BUFFERP (dst_object))
6510 if we must run CCL program or there are compositions to
6511 encode. */
6512 coding->heading_ascii = 0;
6513 if (coding->type != coding_type_ccl
6514 && (! coding->cmp_data || coding->cmp_data->used == 0))
6515 { 7128 {
6516 SHRINK_CONVERSION_REGION (&from, &to_byte, coding, SDATA (str), 7129 coding->dst_object = dst_object;
6517 1); 7130 if (EQ (src_object, dst_object))
6518 if (from == to_byte) 7131 {
7132 coding->dst_pos = from;
7133 coding->dst_pos_byte = from_byte;
7134 }
7135 else
6519 { 7136 {
6520 coding_free_composition_data (coding); 7137 coding->dst_pos = BUF_PT (XBUFFER (dst_object));
6521 goto no_need_of_encoding; 7138 coding->dst_pos_byte = BUF_PT_BYTE (XBUFFER (dst_object));
6522 } 7139 }
6523 shrinked_bytes = from + (SBYTES (str) - to_byte); 7140 coding->dst_multibyte
7141 = ! NILP (XBUFFER (dst_object)->enable_multibyte_characters);
7142 }
7143 else if (EQ (dst_object, Qt))
7144 {
7145 coding->dst_object = Qnil;
7146 coding->dst_bytes = coding->src_chars;
7147 if (coding->dst_bytes == 0)
7148 coding->dst_bytes = 1;
7149 coding->destination = (unsigned char *) xmalloc (coding->dst_bytes);
7150 coding->dst_multibyte = 0;
7151 }
7152 else
7153 {
7154 coding->dst_object = Qnil;
7155 coding->dst_multibyte = 0;
6524 } 7156 }
6525 7157
6526 len = encoding_buffer_size (coding, to_byte - from); 7158 encode_coding (coding);
6527 allocate_conversion_buffer (buf, len);
6528 7159
6529 consumed = consumed_char = produced = produced_char = 0; 7160 if (EQ (dst_object, Qt))
6530 while (1)
6531 { 7161 {
6532 result = encode_coding (coding, SDATA (str) + from + consumed, 7162 if (BUFFERP (coding->dst_object))
6533 buf.data + produced, to_byte - from - consumed, 7163 coding->dst_object = Fbuffer_string ();
6534 buf.size - produced);
6535 consumed += coding->consumed;
6536 consumed_char += coding->consumed_char;
6537 produced += coding->produced;
6538 produced_char += coding->produced_char;
6539 if (result == CODING_FINISH_NORMAL
6540 || result == CODING_FINISH_INTERRUPT
6541 || (result == CODING_FINISH_INSUFFICIENT_SRC
6542 && coding->consumed == 0))
6543 break;
6544 /* Now result should be CODING_FINISH_INSUFFICIENT_DST. */
6545 extend_conversion_buffer (&buf);
6546 }
6547
6548 coding->consumed = consumed;
6549 coding->consumed_char = consumed_char;
6550 coding->produced = produced;
6551 coding->produced_char = produced_char;
6552
6553 newstr = make_uninit_string (produced + shrinked_bytes);
6554 if (from > 0)
6555 STRING_COPYIN (newstr, 0, SDATA (str), from);
6556 STRING_COPYIN (newstr, from, buf.data, produced);
6557 if (shrinked_bytes > from)
6558 STRING_COPYIN (newstr, from + produced,
6559 SDATA (str) + to_byte,
6560 shrinked_bytes - from);
6561
6562 free_conversion_buffer (&buf);
6563 coding_free_composition_data (coding);
6564
6565 return newstr;
6566
6567 no_need_of_encoding:
6568 coding->consumed = SBYTES (str);
6569 coding->consumed_char = SCHARS (str);
6570 if (STRING_MULTIBYTE (str))
6571 {
6572 if (nocopy)
6573 /* We are sure that STR doesn't contain a multibyte
6574 character. */
6575 STRING_SET_UNIBYTE (str);
6576 else 7164 else
6577 { 7165 {
6578 str = Fstring_as_unibyte (str); 7166 coding->dst_object
6579 nocopy = 1; 7167 = make_unibyte_string ((char *) coding->destination,
7168 coding->produced);
7169 xfree (coding->destination);
6580 } 7170 }
6581 } 7171 }
6582 coding->produced = SBYTES (str); 7172
6583 coding->produced_char = SCHARS (str); 7173 if (saved_pt >= 0)
6584 return (nocopy ? str : Fcopy_sequence (str)); 7174 {
7175 /* This is the case of:
7176 (BUFFERP (src_object) && EQ (src_object, dst_object))
7177 As we have moved PT while replacing the original buffer
7178 contents, we must recover it now. */
7179 set_buffer_internal (XBUFFER (src_object));
7180 if (saved_pt < from)
7181 TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte);
7182 else if (saved_pt < from + chars)
7183 TEMP_SET_PT_BOTH (from, from_byte);
7184 else if (! NILP (current_buffer->enable_multibyte_characters))
7185 TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars),
7186 saved_pt_byte + (coding->produced - bytes));
7187 else
7188 TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes),
7189 saved_pt_byte + (coding->produced - bytes));
7190
7191 if (need_marker_adjustment)
7192 {
7193 struct Lisp_Marker *tail;
7194
7195 for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next)
7196 if (tail->need_adjustment)
7197 {
7198 tail->need_adjustment = 0;
7199 if (tail->insertion_type)
7200 {
7201 tail->bytepos = from_byte;
7202 tail->charpos = from;
7203 }
7204 else
7205 {
7206 tail->bytepos = from_byte + coding->produced;
7207 tail->charpos
7208 = (NILP (current_buffer->enable_multibyte_characters)
7209 ? tail->bytepos : from + coding->produced_char);
7210 }
7211 }
7212 }
7213 }
7214
7215 if (kill_src_buffer)
7216 Fkill_buffer (coding->src_object);
7217 unbind_to (count, Qnil);
7218}
7219
7220
7221Lisp_Object
7222preferred_coding_system ()
7223{
7224 int id = coding_categories[coding_priorities[0]].id;
7225
7226 return CODING_ID_NAME (id);
6585} 7227}
6586 7228
6587 7229
@@ -6590,21 +7232,18 @@ encode_coding_string (str, coding, nocopy)
6590 7232
6591DEFUN ("coding-system-p", Fcoding_system_p, Scoding_system_p, 1, 1, 0, 7233DEFUN ("coding-system-p", Fcoding_system_p, Scoding_system_p, 1, 1, 0,
6592 doc: /* Return t if OBJECT is nil or a coding-system. 7234 doc: /* Return t if OBJECT is nil or a coding-system.
6593See the documentation of `make-coding-system' for information 7235See the documentation of `define-coding-system' for information
6594about coding-system objects. */) 7236about coding-system objects. */)
6595 (obj) 7237 (obj)
6596 Lisp_Object obj; 7238 Lisp_Object obj;
6597{ 7239{
6598 if (NILP (obj)) 7240 if (NILP (obj)
7241 || CODING_SYSTEM_ID (obj) >= 0)
6599 return Qt; 7242 return Qt;
6600 if (!SYMBOLP (obj)) 7243 if (! SYMBOLP (obj)
7244 || NILP (Fget (obj, Qcoding_system_define_form)))
6601 return Qnil; 7245 return Qnil;
6602 if (! NILP (Fget (obj, Qcoding_system_define_form))) 7246 return Qt;
6603 return Qt;
6604 /* Get coding-spec vector for OBJ. */
6605 obj = Fget (obj, Qcoding_system);
6606 return ((VECTORP (obj) && XVECTOR (obj)->size == 5)
6607 ? Qt : Qnil);
6608} 7247}
6609 7248
6610DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system, 7249DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system,
@@ -6648,9 +7287,9 @@ DEFUN ("check-coding-system", Fcheck_coding_system, Scheck_coding_system,
6648 1, 1, 0, 7287 1, 1, 0,
6649 doc: /* Check validity of CODING-SYSTEM. 7288 doc: /* Check validity of CODING-SYSTEM.
6650If valid, return CODING-SYSTEM, else signal a `coding-system-error' error. 7289If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
6651It is valid if it is nil or a symbol with a non-nil `coding-system' property. 7290It is valid if it is nil or a symbol defined as a coding system by the
6652The value of this property should be a vector of length 5. */) 7291function `define-coding-system'. */)
6653 (coding_system) 7292 (coding_system)
6654 Lisp_Object coding_system; 7293 Lisp_Object coding_system;
6655{ 7294{
6656 Lisp_Object define_form; 7295 Lisp_Object define_form;
@@ -6665,77 +7304,289 @@ The value of this property should be a vector of length 5. */)
6665 return coding_system; 7304 return coding_system;
6666 xsignal1 (Qcoding_system_error, coding_system); 7305 xsignal1 (Qcoding_system_error, coding_system);
6667} 7306}
7307
6668 7308
7309/* Detect how the bytes at SRC of length SRC_BYTES are encoded. If
7310 HIGHEST is nonzero, return the coding system of the highest
7311 priority among the detected coding systems. Otherwize return a
7312 list of detected coding systems sorted by their priorities. If
7313 MULTIBYTEP is nonzero, it is assumed that the bytes are in correct
7314 multibyte form but contains only ASCII and eight-bit chars.
7315 Otherwise, the bytes are raw bytes.
7316
7317 CODING-SYSTEM controls the detection as below:
7318
7319 If it is nil, detect both text-format and eol-format. If the
7320 text-format part of CODING-SYSTEM is already specified
7321 (e.g. `iso-latin-1'), detect only eol-format. If the eol-format
7322 part of CODING-SYSTEM is already specified (e.g. `undecided-unix'),
7323 detect only text-format. */
7324
6669Lisp_Object 7325Lisp_Object
6670detect_coding_system (src, src_bytes, highest, multibytep) 7326detect_coding_system (src, src_chars, src_bytes, highest, multibytep,
7327 coding_system)
6671 const unsigned char *src; 7328 const unsigned char *src;
6672 int src_bytes, highest; 7329 int src_chars, src_bytes, highest;
6673 int multibytep; 7330 int multibytep;
7331 Lisp_Object coding_system;
6674{ 7332{
6675 int coding_mask, eol_type; 7333 const unsigned char *src_end = src + src_bytes;
6676 Lisp_Object val, tmp; 7334 Lisp_Object attrs, eol_type;
6677 int dummy; 7335 Lisp_Object val;
7336 struct coding_system coding;
7337 int id;
7338 struct coding_detection_info detect_info;
7339 enum coding_category base_category;
6678 7340
6679 coding_mask = detect_coding_mask (src, src_bytes, NULL, &dummy, multibytep); 7341 if (NILP (coding_system))
6680 eol_type = detect_eol_type (src, src_bytes, &dummy); 7342 coding_system = Qundecided;
6681 if (eol_type == CODING_EOL_INCONSISTENT) 7343 setup_coding_system (coding_system, &coding);
6682 eol_type = CODING_EOL_UNDECIDED; 7344 attrs = CODING_ID_ATTRS (coding.id);
7345 eol_type = CODING_ID_EOL_TYPE (coding.id);
7346 coding_system = CODING_ATTR_BASE_NAME (attrs);
7347
7348 coding.source = src;
7349 coding.src_chars = src_chars;
7350 coding.src_bytes = src_bytes;
7351 coding.src_multibyte = multibytep;
7352 coding.consumed = 0;
7353 coding.mode |= CODING_MODE_LAST_BLOCK;
6683 7354
6684 if (!coding_mask) 7355 detect_info.checked = detect_info.found = detect_info.rejected = 0;
7356
7357 /* At first, detect text-format if necessary. */
7358 base_category = XINT (CODING_ATTR_CATEGORY (attrs));
7359 if (base_category == coding_category_undecided)
6685 { 7360 {
6686 val = Qundecided; 7361 enum coding_category category;
6687 if (eol_type != CODING_EOL_UNDECIDED) 7362 struct coding_system *this;
7363 int c, i;
7364
7365 /* Skip all ASCII bytes except for a few ISO2022 controls. */
7366 for (i = 0; src < src_end; i++, src++)
6688 { 7367 {
6689 Lisp_Object val2; 7368 c = *src;
6690 val2 = Fget (Qundecided, Qeol_type); 7369 if (c & 0x80)
6691 if (VECTORP (val2)) 7370 break;
6692 val = XVECTOR (val2)->contents[eol_type]; 7371 if (c < 0x20
7372 && (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)
7373 && ! inhibit_iso_escape_detection)
7374 {
7375 coding.head_ascii = src - coding.source;
7376 if (detect_coding_iso_2022 (&coding, &detect_info))
7377 {
7378 /* We have scanned the whole data. */
7379 if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE))
7380 /* We didn't find an 8-bit code. */
7381 src = src_end;
7382 break;
7383 }
7384 }
6693 } 7385 }
6694 return (highest ? val : Fcons (val, Qnil)); 7386 coding.head_ascii = src - coding.source;
6695 }
6696 7387
6697 /* At first, gather possible coding systems in VAL. */ 7388 if (src < src_end
6698 val = Qnil; 7389 || detect_info.found)
6699 for (tmp = Vcoding_category_list; CONSP (tmp); tmp = XCDR (tmp)) 7390 {
6700 { 7391 if (src == src_end)
6701 Lisp_Object category_val, category_index; 7392 /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */
7393 for (i = 0; i < coding_category_raw_text; i++)
7394 {
7395 category = coding_priorities[i];
7396 this = coding_categories + category;
7397 if (detect_info.found & (1 << category))
7398 break;
7399 }
7400 else
7401 for (i = 0; i < coding_category_raw_text; i++)
7402 {
7403 category = coding_priorities[i];
7404 this = coding_categories + category;
6702 7405
6703 category_index = Fget (XCAR (tmp), Qcoding_category_index); 7406 if (this->id < 0)
6704 category_val = Fsymbol_value (XCAR (tmp)); 7407 {
6705 if (!NILP (category_val) 7408 /* No coding system of this category is defined. */
6706 && NATNUMP (category_index) 7409 detect_info.rejected |= (1 << category);
6707 && (coding_mask & (1 << XFASTINT (category_index)))) 7410 }
7411 else if (category >= coding_category_raw_text)
7412 continue;
7413 else if (detect_info.checked & (1 << category))
7414 {
7415 if (highest
7416 && (detect_info.found & (1 << category)))
7417 break;
7418 }
7419 else
7420 {
7421 if ((*(this->detector)) (&coding, &detect_info)
7422 && highest
7423 && (detect_info.found & (1 << category)))
7424 {
7425 if (category == coding_category_utf_16_auto)
7426 {
7427 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
7428 category = coding_category_utf_16_le;
7429 else
7430 category = coding_category_utf_16_be;
7431 }
7432 break;
7433 }
7434 }
7435 }
7436 }
7437
7438 if (detect_info.rejected == CATEGORY_MASK_ANY)
6708 { 7439 {
6709 val = Fcons (category_val, val); 7440 detect_info.found = CATEGORY_MASK_RAW_TEXT;
6710 if (highest) 7441 id = coding_categories[coding_category_raw_text].id;
6711 break; 7442 val = Fcons (make_number (id), Qnil);
6712 } 7443 }
6713 } 7444 else if (! detect_info.rejected && ! detect_info.found)
6714 if (!highest) 7445 {
6715 val = Fnreverse (val); 7446 detect_info.found = CATEGORY_MASK_ANY;
7447 id = coding_categories[coding_category_undecided].id;
7448 val = Fcons (make_number (id), Qnil);
7449 }
7450 else if (highest)
7451 {
7452 if (detect_info.found)
7453 {
7454 detect_info.found = 1 << category;
7455 val = Fcons (make_number (this->id), Qnil);
7456 }
7457 else
7458 for (i = 0; i < coding_category_raw_text; i++)
7459 if (! (detect_info.rejected & (1 << coding_priorities[i])))
7460 {
7461 detect_info.found = 1 << coding_priorities[i];
7462 id = coding_categories[coding_priorities[i]].id;
7463 val = Fcons (make_number (id), Qnil);
7464 break;
7465 }
7466 }
7467 else
7468 {
7469 int mask = detect_info.rejected | detect_info.found;
7470 int found = 0;
7471 val = Qnil;
6716 7472
6717 /* Then, replace the elements with subsidiary coding systems. */ 7473 for (i = coding_category_raw_text - 1; i >= 0; i--)
6718 for (tmp = val; CONSP (tmp); tmp = XCDR (tmp)) 7474 {
7475 category = coding_priorities[i];
7476 if (! (mask & (1 << category)))
7477 {
7478 found |= 1 << category;
7479 id = coding_categories[category].id;
7480 if (id >= 0)
7481 val = Fcons (make_number (id), val);
7482 }
7483 }
7484 for (i = coding_category_raw_text - 1; i >= 0; i--)
7485 {
7486 category = coding_priorities[i];
7487 if (detect_info.found & (1 << category))
7488 {
7489 id = coding_categories[category].id;
7490 val = Fcons (make_number (id), val);
7491 }
7492 }
7493 detect_info.found |= found;
7494 }
7495 }
7496 else if (base_category == coding_category_utf_16_auto)
6719 { 7497 {
6720 if (eol_type != CODING_EOL_UNDECIDED 7498 if (detect_coding_utf_16 (&coding, &detect_info))
6721 && eol_type != CODING_EOL_INCONSISTENT)
6722 { 7499 {
6723 Lisp_Object eol; 7500 struct coding_system *this;
6724 eol = Fget (XCAR (tmp), Qeol_type); 7501
6725 if (VECTORP (eol)) 7502 if (detect_info.found & CATEGORY_MASK_UTF_16_LE)
6726 XSETCAR (tmp, XVECTOR (eol)->contents[eol_type]); 7503 this = coding_categories + coding_category_utf_16_le;
7504 else if (detect_info.found & CATEGORY_MASK_UTF_16_BE)
7505 this = coding_categories + coding_category_utf_16_be;
7506 else if (detect_info.rejected & CATEGORY_MASK_UTF_16_LE_NOSIG)
7507 this = coding_categories + coding_category_utf_16_be_nosig;
7508 else
7509 this = coding_categories + coding_category_utf_16_le_nosig;
7510 val = Fcons (make_number (this->id), Qnil);
6727 } 7511 }
6728 } 7512 }
7513 else
7514 {
7515 detect_info.found = 1 << XINT (CODING_ATTR_CATEGORY (attrs));
7516 val = Fcons (make_number (coding.id), Qnil);
7517 }
7518
7519 /* Then, detect eol-format if necessary. */
7520 {
7521 int normal_eol = -1, utf_16_be_eol = -1, utf_16_le_eol;
7522 Lisp_Object tail;
7523
7524 if (VECTORP (eol_type))
7525 {
7526 if (detect_info.found & ~CATEGORY_MASK_UTF_16)
7527 normal_eol = detect_eol (coding.source, src_bytes,
7528 coding_category_raw_text);
7529 if (detect_info.found & (CATEGORY_MASK_UTF_16_BE
7530 | CATEGORY_MASK_UTF_16_BE_NOSIG))
7531 utf_16_be_eol = detect_eol (coding.source, src_bytes,
7532 coding_category_utf_16_be);
7533 if (detect_info.found & (CATEGORY_MASK_UTF_16_LE
7534 | CATEGORY_MASK_UTF_16_LE_NOSIG))
7535 utf_16_le_eol = detect_eol (coding.source, src_bytes,
7536 coding_category_utf_16_le);
7537 }
7538 else
7539 {
7540 if (EQ (eol_type, Qunix))
7541 normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_LF;
7542 else if (EQ (eol_type, Qdos))
7543 normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CRLF;
7544 else
7545 normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CR;
7546 }
7547
7548 for (tail = val; CONSP (tail); tail = XCDR (tail))
7549 {
7550 enum coding_category category;
7551 int this_eol;
7552
7553 id = XINT (XCAR (tail));
7554 attrs = CODING_ID_ATTRS (id);
7555 category = XINT (CODING_ATTR_CATEGORY (attrs));
7556 eol_type = CODING_ID_EOL_TYPE (id);
7557 if (VECTORP (eol_type))
7558 {
7559 if (category == coding_category_utf_16_be
7560 || category == coding_category_utf_16_be_nosig)
7561 this_eol = utf_16_be_eol;
7562 else if (category == coding_category_utf_16_le
7563 || category == coding_category_utf_16_le_nosig)
7564 this_eol = utf_16_le_eol;
7565 else
7566 this_eol = normal_eol;
7567
7568 if (this_eol == EOL_SEEN_LF)
7569 XSETCAR (tail, AREF (eol_type, 0));
7570 else if (this_eol == EOL_SEEN_CRLF)
7571 XSETCAR (tail, AREF (eol_type, 1));
7572 else if (this_eol == EOL_SEEN_CR)
7573 XSETCAR (tail, AREF (eol_type, 2));
7574 else
7575 XSETCAR (tail, CODING_ID_NAME (id));
7576 }
7577 else
7578 XSETCAR (tail, CODING_ID_NAME (id));
7579 }
7580 }
7581
6729 return (highest ? XCAR (val) : val); 7582 return (highest ? XCAR (val) : val);
6730} 7583}
6731 7584
7585
6732DEFUN ("detect-coding-region", Fdetect_coding_region, Sdetect_coding_region, 7586DEFUN ("detect-coding-region", Fdetect_coding_region, Sdetect_coding_region,
6733 2, 3, 0, 7587 2, 3, 0,
6734 doc: /* Detect how the byte sequence in the region is encoded. 7588 doc: /* Detect coding system of the text in the region between START and END.
6735Return a list of possible coding systems used on decoding a byte 7589Return a list of possible coding systems ordered by priority.
6736sequence containing the bytes in the region between START and END when
6737the coding system `undecided' is specified. The list is ordered by
6738priority decided in the current language environment.
6739 7590
6740If only ASCII characters are found (except for such ISO-2022 control 7591If only ASCII characters are found (except for such ISO-2022 control
6741characters ISO-2022 as ESC), it returns a list of single element 7592characters ISO-2022 as ESC), it returns a list of single element
@@ -6749,7 +7600,6 @@ highest priority. */)
6749{ 7600{
6750 int from, to; 7601 int from, to;
6751 int from_byte, to_byte; 7602 int from_byte, to_byte;
6752 int include_anchor_byte = 0;
6753 7603
6754 CHECK_NUMBER_COERCE_MARKER (start); 7604 CHECK_NUMBER_COERCE_MARKER (start);
6755 CHECK_NUMBER_COERCE_MARKER (end); 7605 CHECK_NUMBER_COERCE_MARKER (end);
@@ -6761,29 +7611,19 @@ highest priority. */)
6761 7611
6762 if (from < GPT && to >= GPT) 7612 if (from < GPT && to >= GPT)
6763 move_gap_both (to, to_byte); 7613 move_gap_both (to, to_byte);
6764 /* If we an anchor byte `\0' follows the region, we include it in
6765 the detecting source. Then code detectors can handle the tailing
6766 byte sequence more accurately.
6767 7614
6768 Fix me: This is not a perfect solution. It is better that we
6769 add one more argument, say LAST_BLOCK, to all detect_coding_XXX.
6770 */
6771 if (to == Z || (to == GPT && GAP_SIZE > 0))
6772 include_anchor_byte = 1;
6773 return detect_coding_system (BYTE_POS_ADDR (from_byte), 7615 return detect_coding_system (BYTE_POS_ADDR (from_byte),
6774 to_byte - from_byte + include_anchor_byte, 7616 to - from, to_byte - from_byte,
6775 !NILP (highest), 7617 !NILP (highest),
6776 !NILP (current_buffer 7618 !NILP (current_buffer
6777 ->enable_multibyte_characters)); 7619 ->enable_multibyte_characters),
7620 Qnil);
6778} 7621}
6779 7622
6780DEFUN ("detect-coding-string", Fdetect_coding_string, Sdetect_coding_string, 7623DEFUN ("detect-coding-string", Fdetect_coding_string, Sdetect_coding_string,
6781 1, 2, 0, 7624 1, 2, 0,
6782 doc: /* Detect how the byte sequence in STRING is encoded. 7625 doc: /* Detect coding system of the text in STRING.
6783Return a list of possible coding systems used on decoding a byte 7626Return a list of possible coding systems ordered by priority.
6784sequence containing the bytes in STRING when the coding system
6785`undecided' is specified. The list is ordered by priority decided in
6786the current language environment.
6787 7627
6788If only ASCII characters are found (except for such ISO-2022 control 7628If only ASCII characters are found (except for such ISO-2022 control
6789characters ISO-2022 as ESC), it returns a list of single element 7629characters ISO-2022 as ESC), it returns a list of single element
@@ -6798,288 +7638,157 @@ highest priority. */)
6798 CHECK_STRING (string); 7638 CHECK_STRING (string);
6799 7639
6800 return detect_coding_system (SDATA (string), 7640 return detect_coding_system (SDATA (string),
6801 /* "+ 1" is to include the anchor byte 7641 SCHARS (string), SBYTES (string),
6802 `\0'. With this, code detectors can 7642 !NILP (highest), STRING_MULTIBYTE (string),
6803 handle the tailing bytes more 7643 Qnil);
6804 accurately. */
6805 SBYTES (string) + 1,
6806 !NILP (highest),
6807 STRING_MULTIBYTE (string));
6808} 7644}
6809 7645
6810/* Subroutine for Ffind_coding_systems_region_internal.
6811
6812 Return a list of coding systems that safely encode the multibyte
6813 text between P and PEND. SAFE_CODINGS, if non-nil, is an alist of
6814 possible coding systems. If it is nil, it means that we have not
6815 yet found any coding systems.
6816 7646
6817 WORK_TABLE a char-table of which element is set to t once the 7647static INLINE int
6818 element is looked up. 7648char_encodable_p (c, attrs)
6819 7649 int c;
6820 If a non-ASCII single byte char is found, set 7650 Lisp_Object attrs;
6821 *single_byte_char_found to 1. */
6822
6823static Lisp_Object
6824find_safe_codings (p, pend, safe_codings, work_table, single_byte_char_found)
6825 unsigned char *p, *pend;
6826 Lisp_Object safe_codings, work_table;
6827 int *single_byte_char_found;
6828{ 7651{
6829 int c, len; 7652 Lisp_Object tail;
6830 Lisp_Object val, ch; 7653 struct charset *charset;
6831 Lisp_Object prev, tail; 7654 Lisp_Object translation_table;
6832 7655
6833 if (NILP (safe_codings)) 7656 translation_table = CODING_ATTR_TRANS_TBL (attrs);
6834 goto done_safe_codings; 7657 if (! NILP (translation_table))
6835 while (p < pend) 7658 c = translate_char (translation_table, c);
7659 for (tail = CODING_ATTR_CHARSET_LIST (attrs);
7660 CONSP (tail); tail = XCDR (tail))
6836 { 7661 {
6837 c = STRING_CHAR_AND_LENGTH (p, pend - p, len); 7662 charset = CHARSET_FROM_ID (XINT (XCAR (tail)));
6838 p += len; 7663 if (CHAR_CHARSET_P (c, charset))
6839 if (ASCII_BYTE_P (c)) 7664 break;
6840 /* We can ignore ASCII characters here. */
6841 continue;
6842 if (SINGLE_BYTE_CHAR_P (c))
6843 *single_byte_char_found = 1;
6844 /* Check the safe coding systems for C. */
6845 ch = make_number (c);
6846 val = Faref (work_table, ch);
6847 if (EQ (val, Qt))
6848 /* This element was already checked. Ignore it. */
6849 continue;
6850 /* Remember that we checked this element. */
6851 Faset (work_table, ch, Qt);
6852
6853 for (prev = tail = safe_codings; CONSP (tail); tail = XCDR (tail))
6854 {
6855 Lisp_Object elt, translation_table, hash_table, accept_latin_extra;
6856 int encodable;
6857
6858 elt = XCAR (tail);
6859 if (CONSP (XCDR (elt)))
6860 {
6861 /* This entry has this format now:
6862 ( CODING SAFE-CHARS TRANSLATION-TABLE HASH-TABLE
6863 ACCEPT-LATIN-EXTRA ) */
6864 val = XCDR (elt);
6865 encodable = ! NILP (Faref (XCAR (val), ch));
6866 if (! encodable)
6867 {
6868 val = XCDR (val);
6869 translation_table = XCAR (val);
6870 hash_table = XCAR (XCDR (val));
6871 accept_latin_extra = XCAR (XCDR (XCDR (val)));
6872 }
6873 }
6874 else
6875 {
6876 /* This entry has this format now: ( CODING . SAFE-CHARS) */
6877 encodable = ! NILP (Faref (XCDR (elt), ch));
6878 if (! encodable)
6879 {
6880 /* Transform the format to:
6881 ( CODING SAFE-CHARS TRANSLATION-TABLE HASH-TABLE
6882 ACCEPT-LATIN-EXTRA ) */
6883 val = Fget (XCAR (elt), Qcoding_system);
6884 translation_table
6885 = Fplist_get (AREF (val, 3),
6886 Qtranslation_table_for_encode);
6887 if (SYMBOLP (translation_table))
6888 translation_table = Fget (translation_table,
6889 Qtranslation_table);
6890 hash_table
6891 = (CHAR_TABLE_P (translation_table)
6892 ? XCHAR_TABLE (translation_table)->extras[1]
6893 : Qnil);
6894 accept_latin_extra
6895 = ((EQ (AREF (val, 0), make_number (2))
6896 && VECTORP (AREF (val, 4)))
6897 ? AREF (AREF (val, 4), 16)
6898 : Qnil);
6899 XSETCAR (tail, list5 (XCAR (elt), XCDR (elt),
6900 translation_table, hash_table,
6901 accept_latin_extra));
6902 }
6903 }
6904
6905 if (! encodable
6906 && ((CHAR_TABLE_P (translation_table)
6907 && ! NILP (Faref (translation_table, ch)))
6908 || (HASH_TABLE_P (hash_table)
6909 && ! NILP (Fgethash (ch, hash_table, Qnil)))
6910 || (SINGLE_BYTE_CHAR_P (c)
6911 && ! NILP (accept_latin_extra)
6912 && VECTORP (Vlatin_extra_code_table)
6913 && ! NILP (AREF (Vlatin_extra_code_table, c)))))
6914 encodable = 1;
6915 if (encodable)
6916 prev = tail;
6917 else
6918 {
6919 /* Exclude this coding system from SAFE_CODINGS. */
6920 if (EQ (tail, safe_codings))
6921 {
6922 safe_codings = XCDR (safe_codings);
6923 if (NILP (safe_codings))
6924 goto done_safe_codings;
6925 }
6926 else
6927 XSETCDR (prev, XCDR (tail));
6928 }
6929 }
6930 } 7665 }
6931 7666 return (! NILP (tail));
6932 done_safe_codings:
6933 /* If the above loop was terminated before P reaches PEND, it means
6934 SAFE_CODINGS was set to nil. If we have not yet found an
6935 non-ASCII single-byte char, check it now. */
6936 if (! *single_byte_char_found)
6937 while (p < pend)
6938 {
6939 c = STRING_CHAR_AND_LENGTH (p, pend - p, len);
6940 p += len;
6941 if (! ASCII_BYTE_P (c)
6942 && SINGLE_BYTE_CHAR_P (c))
6943 {
6944 *single_byte_char_found = 1;
6945 break;
6946 }
6947 }
6948 return safe_codings;
6949} 7667}
6950 7668
7669
7670/* Return a list of coding systems that safely encode the text between
7671 START and END. If EXCLUDE is non-nil, it is a list of coding
7672 systems not to check. The returned list doesn't contain any such
7673 coding systems. In any case, if the text contains only ASCII or is
7674 unibyte, return t. */
7675
6951DEFUN ("find-coding-systems-region-internal", 7676DEFUN ("find-coding-systems-region-internal",
6952 Ffind_coding_systems_region_internal, 7677 Ffind_coding_systems_region_internal,
6953 Sfind_coding_systems_region_internal, 2, 2, 0, 7678 Sfind_coding_systems_region_internal, 2, 3, 0,
6954 doc: /* Internal use only. */) 7679 doc: /* Internal use only. */)
6955 (start, end) 7680 (start, end, exclude)
6956 Lisp_Object start, end; 7681 Lisp_Object start, end, exclude;
6957{ 7682{
6958 Lisp_Object work_table, safe_codings; 7683 Lisp_Object coding_attrs_list, safe_codings;
6959 int non_ascii_p = 0; 7684 EMACS_INT start_byte, end_byte;
6960 int single_byte_char_found = 0; 7685 const unsigned char *p, *pbeg, *pend;
6961 const unsigned char *p1, *p1end, *p2, *p2end, *p; 7686 int c;
7687 Lisp_Object tail, elt;
6962 7688
6963 if (STRINGP (start)) 7689 if (STRINGP (start))
6964 { 7690 {
6965 if (!STRING_MULTIBYTE (start)) 7691 if (!STRING_MULTIBYTE (start)
7692 || SCHARS (start) == SBYTES (start))
6966 return Qt; 7693 return Qt;
6967 p1 = SDATA (start), p1end = p1 + SBYTES (start); 7694 start_byte = 0;
6968 p2 = p2end = p1end; 7695 end_byte = SBYTES (start);
6969 if (SCHARS (start) != SBYTES (start))
6970 non_ascii_p = 1;
6971 } 7696 }
6972 else 7697 else
6973 { 7698 {
6974 int from, to, stop;
6975
6976 CHECK_NUMBER_COERCE_MARKER (start); 7699 CHECK_NUMBER_COERCE_MARKER (start);
6977 CHECK_NUMBER_COERCE_MARKER (end); 7700 CHECK_NUMBER_COERCE_MARKER (end);
6978 if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end)) 7701 if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end))
6979 args_out_of_range (start, end); 7702 args_out_of_range (start, end);
6980 if (NILP (current_buffer->enable_multibyte_characters)) 7703 if (NILP (current_buffer->enable_multibyte_characters))
6981 return Qt; 7704 return Qt;
6982 from = CHAR_TO_BYTE (XINT (start)); 7705 start_byte = CHAR_TO_BYTE (XINT (start));
6983 to = CHAR_TO_BYTE (XINT (end)); 7706 end_byte = CHAR_TO_BYTE (XINT (end));
6984 stop = from < GPT_BYTE && GPT_BYTE < to ? GPT_BYTE : to; 7707 if (XINT (end) - XINT (start) == end_byte - start_byte)
6985 p1 = BYTE_POS_ADDR (from), p1end = p1 + (stop - from); 7708 return Qt;
6986 if (stop == to)
6987 p2 = p2end = p1end;
6988 else
6989 p2 = BYTE_POS_ADDR (stop), p2end = p2 + (to - stop);
6990 if (XINT (end) - XINT (start) != to - from)
6991 non_ascii_p = 1;
6992 }
6993 7709
6994 if (!non_ascii_p) 7710 if (XINT (start) < GPT && XINT (end) > GPT)
6995 {
6996 /* We are sure that the text contains no multibyte character.
6997 Check if it contains eight-bit-graphic. */
6998 p = p1;
6999 for (p = p1; p < p1end && ASCII_BYTE_P (*p); p++);
7000 if (p == p1end)
7001 { 7711 {
7002 for (p = p2; p < p2end && ASCII_BYTE_P (*p); p++); 7712 if ((GPT - XINT (start)) < (XINT (end) - GPT))
7003 if (p == p2end) 7713 move_gap_both (XINT (start), start_byte);
7004 return Qt; 7714 else
7715 move_gap_both (XINT (end), end_byte);
7005 } 7716 }
7006 } 7717 }
7007 7718
7008 /* The text contains non-ASCII characters. */ 7719 coding_attrs_list = Qnil;
7720 for (tail = Vcoding_system_list; CONSP (tail); tail = XCDR (tail))
7721 if (NILP (exclude)
7722 || NILP (Fmemq (XCAR (tail), exclude)))
7723 {
7724 Lisp_Object attrs;
7009 7725
7010 work_table = Fmake_char_table (Qchar_coding_system, Qnil); 7726 attrs = AREF (CODING_SYSTEM_SPEC (XCAR (tail)), 0);
7011 safe_codings = Fcopy_sequence (XCDR (Vcoding_system_safe_chars)); 7727 if (EQ (XCAR (tail), CODING_ATTR_BASE_NAME (attrs))
7728 && ! EQ (CODING_ATTR_TYPE (attrs), Qundecided))
7729 {
7730 ASET (attrs, coding_attr_trans_tbl,
7731 get_translation_table (attrs, 1, NULL));
7732 coding_attrs_list = Fcons (attrs, coding_attrs_list);
7733 }
7734 }
7012 7735
7013 safe_codings = find_safe_codings (p1, p1end, safe_codings, work_table, 7736 if (STRINGP (start))
7014 &single_byte_char_found); 7737 p = pbeg = SDATA (start);
7015 if (p2 < p2end)
7016 safe_codings = find_safe_codings (p2, p2end, safe_codings, work_table,
7017 &single_byte_char_found);
7018 if (EQ (safe_codings, XCDR (Vcoding_system_safe_chars)))
7019 safe_codings = Qt;
7020 else 7738 else
7021 { 7739 p = pbeg = BYTE_POS_ADDR (start_byte);
7022 /* Turn safe_codings to a list of coding systems... */ 7740 pend = p + (end_byte - start_byte);
7023 Lisp_Object val;
7024
7025 if (single_byte_char_found)
7026 /* ... and append these for eight-bit chars. */
7027 val = Fcons (Qraw_text,
7028 Fcons (Qemacs_mule, Fcons (Qno_conversion, Qnil)));
7029 else
7030 /* ... and append generic coding systems. */
7031 val = Fcopy_sequence (XCAR (Vcoding_system_safe_chars));
7032
7033 for (; CONSP (safe_codings); safe_codings = XCDR (safe_codings))
7034 val = Fcons (XCAR (XCAR (safe_codings)), val);
7035 safe_codings = val;
7036 }
7037
7038 return safe_codings;
7039}
7040
7041
7042/* Search from position POS for such characters that are unencodable
7043 accoding to SAFE_CHARS, and return a list of their positions. P
7044 points where in the memory the character at POS exists. Limit the
7045 search at PEND or when Nth unencodable characters are found.
7046 7741
7047 If SAFE_CHARS is a char table, an element for an unencodable 7742 while (p < pend && ASCII_BYTE_P (*p)) p++;
7048 character is nil. 7743 while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--;
7049 7744
7050 If SAFE_CHARS is nil, all non-ASCII characters are unencodable.
7051
7052 Otherwise, SAFE_CHARS is t, and only eight-bit-contrl and
7053 eight-bit-graphic characters are unencodable. */
7054
7055static Lisp_Object
7056unencodable_char_position (safe_chars, pos, p, pend, n)
7057 Lisp_Object safe_chars;
7058 int pos;
7059 unsigned char *p, *pend;
7060 int n;
7061{
7062 Lisp_Object pos_list;
7063
7064 pos_list = Qnil;
7065 while (p < pend) 7745 while (p < pend)
7066 { 7746 {
7067 int len; 7747 if (ASCII_BYTE_P (*p))
7068 int c = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, len); 7748 p++;
7069 7749 else
7070 if (c >= 128
7071 && (CHAR_TABLE_P (safe_chars)
7072 ? NILP (CHAR_TABLE_REF (safe_chars, c))
7073 : (NILP (safe_chars) || c < 256)))
7074 { 7750 {
7075 pos_list = Fcons (make_number (pos), pos_list); 7751 c = STRING_CHAR_ADVANCE (p);
7076 if (--n <= 0) 7752
7077 break; 7753 charset_map_loaded = 0;
7754 for (tail = coding_attrs_list; CONSP (tail);)
7755 {
7756 elt = XCAR (tail);
7757 if (NILP (elt))
7758 tail = XCDR (tail);
7759 else if (char_encodable_p (c, elt))
7760 tail = XCDR (tail);
7761 else if (CONSP (XCDR (tail)))
7762 {
7763 XSETCAR (tail, XCAR (XCDR (tail)));
7764 XSETCDR (tail, XCDR (XCDR (tail)));
7765 }
7766 else
7767 {
7768 XSETCAR (tail, Qnil);
7769 tail = XCDR (tail);
7770 }
7771 }
7772 if (charset_map_loaded)
7773 {
7774 EMACS_INT p_offset = p - pbeg, pend_offset = pend - pbeg;
7775
7776 if (STRINGP (start))
7777 pbeg = SDATA (start);
7778 else
7779 pbeg = BYTE_POS_ADDR (start_byte);
7780 p = pbeg + p_offset;
7781 pend = pbeg + pend_offset;
7782 }
7078 } 7783 }
7079 pos++;
7080 p += len;
7081 } 7784 }
7082 return Fnreverse (pos_list); 7785
7786 safe_codings = list2 (Qraw_text, Qno_conversion);
7787 for (tail = coding_attrs_list; CONSP (tail); tail = XCDR (tail))
7788 if (! NILP (XCAR (tail)))
7789 safe_codings = Fcons (CODING_ATTR_BASE_NAME (XCAR (tail)), safe_codings);
7790
7791 return safe_codings;
7083} 7792}
7084 7793
7085 7794
@@ -7101,24 +7810,36 @@ to the string. */)
7101 Lisp_Object start, end, coding_system, count, string; 7810 Lisp_Object start, end, coding_system, count, string;
7102{ 7811{
7103 int n; 7812 int n;
7104 Lisp_Object safe_chars;
7105 struct coding_system coding; 7813 struct coding_system coding;
7814 Lisp_Object attrs, charset_list, translation_table;
7106 Lisp_Object positions; 7815 Lisp_Object positions;
7107 int from, to; 7816 int from, to;
7108 unsigned char *p, *pend; 7817 const unsigned char *p, *stop, *pend;
7818 int ascii_compatible;
7819
7820 setup_coding_system (Fcheck_coding_system (coding_system), &coding);
7821 attrs = CODING_ID_ATTRS (coding.id);
7822 if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text))
7823 return Qnil;
7824 ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs));
7825 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
7826 translation_table = get_translation_table (attrs, 1, NULL);
7109 7827
7110 if (NILP (string)) 7828 if (NILP (string))
7111 { 7829 {
7112 validate_region (&start, &end); 7830 validate_region (&start, &end);
7113 from = XINT (start); 7831 from = XINT (start);
7114 to = XINT (end); 7832 to = XINT (end);
7115 if (NILP (current_buffer->enable_multibyte_characters)) 7833 if (NILP (current_buffer->enable_multibyte_characters)
7834 || (ascii_compatible
7835 && (to - from) == (CHAR_TO_BYTE (to) - (CHAR_TO_BYTE (from)))))
7116 return Qnil; 7836 return Qnil;
7117 p = CHAR_POS_ADDR (from); 7837 p = CHAR_POS_ADDR (from);
7118 if (to == GPT) 7838 pend = CHAR_POS_ADDR (to);
7119 pend = GPT_ADDR; 7839 if (from < GPT && to >= GPT)
7840 stop = GPT_ADDR;
7120 else 7841 else
7121 pend = CHAR_POS_ADDR (to); 7842 stop = pend;
7122 } 7843 }
7123 else 7844 else
7124 { 7845 {
@@ -7133,11 +7854,11 @@ to the string. */)
7133 if (! STRING_MULTIBYTE (string)) 7854 if (! STRING_MULTIBYTE (string))
7134 return Qnil; 7855 return Qnil;
7135 p = SDATA (string) + string_char_to_byte (string, from); 7856 p = SDATA (string) + string_char_to_byte (string, from);
7136 pend = SDATA (string) + string_char_to_byte (string, to); 7857 stop = pend = SDATA (string) + string_char_to_byte (string, to);
7858 if (ascii_compatible && (to - from) == (pend - p))
7859 return Qnil;
7137 } 7860 }
7138 7861
7139 setup_coding_system (Fcheck_coding_system (coding_system), &coding);
7140
7141 if (NILP (count)) 7862 if (NILP (count))
7142 n = 1; 7863 n = 1;
7143 else 7864 else
@@ -7146,151 +7867,294 @@ to the string. */)
7146 n = XINT (count); 7867 n = XINT (count);
7147 } 7868 }
7148 7869
7149 if (coding.type == coding_type_no_conversion 7870 positions = Qnil;
7150 || coding.type == coding_type_raw_text) 7871 while (1)
7151 return Qnil; 7872 {
7873 int c;
7152 7874
7153 if (coding.type == coding_type_undecided) 7875 if (ascii_compatible)
7154 safe_chars = Qnil; 7876 while (p < stop && ASCII_BYTE_P (*p))
7155 else 7877 p++, from++;
7156 safe_chars = coding_safe_chars (coding_system); 7878 if (p >= stop)
7879 {
7880 if (p >= pend)
7881 break;
7882 stop = pend;
7883 p = GAP_END_ADDR;
7884 }
7885
7886 c = STRING_CHAR_ADVANCE (p);
7887 if (! (ASCII_CHAR_P (c) && ascii_compatible)
7888 && ! char_charset (translate_char (translation_table, c),
7889 charset_list, NULL))
7890 {
7891 positions = Fcons (make_number (from), positions);
7892 n--;
7893 if (n == 0)
7894 break;
7895 }
7896
7897 from++;
7898 }
7899
7900 return (NILP (count) ? Fcar (positions) : Fnreverse (positions));
7901}
7902
7903
7904DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region,
7905 Scheck_coding_systems_region, 3, 3, 0,
7906 doc: /* Check if the region is encodable by coding systems.
7907
7908START and END are buffer positions specifying the region.
7909CODING-SYSTEM-LIST is a list of coding systems to check.
7910
7911The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where
7912CODING-SYSTEM is a member of CODING-SYSTEM-LIst and can't encode the
7913whole region, POS0, POS1, ... are buffer positions where non-encodable
7914characters are found.
7915
7916If all coding systems in CODING-SYSTEM-LIST can encode the region, the
7917value is nil.
7918
7919START may be a string. In that case, check if the string is
7920encodable, and the value contains indices to the string instead of
7921buffer positions. END is ignored. */)
7922 (start, end, coding_system_list)
7923 Lisp_Object start, end, coding_system_list;
7924{
7925 Lisp_Object list;
7926 EMACS_INT start_byte, end_byte;
7927 int pos;
7928 const unsigned char *p, *pbeg, *pend;
7929 int c;
7930 Lisp_Object tail, elt, attrs;
7157 7931
7158 if (STRINGP (string) 7932 if (STRINGP (start))
7159 || from >= GPT || to <= GPT) 7933 {
7160 positions = unencodable_char_position (safe_chars, from, p, pend, n); 7934 if (!STRING_MULTIBYTE (start)
7935 && SCHARS (start) != SBYTES (start))
7936 return Qnil;
7937 start_byte = 0;
7938 end_byte = SBYTES (start);
7939 pos = 0;
7940 }
7161 else 7941 else
7162 { 7942 {
7163 Lisp_Object args[2]; 7943 CHECK_NUMBER_COERCE_MARKER (start);
7944 CHECK_NUMBER_COERCE_MARKER (end);
7945 if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end))
7946 args_out_of_range (start, end);
7947 if (NILP (current_buffer->enable_multibyte_characters))
7948 return Qnil;
7949 start_byte = CHAR_TO_BYTE (XINT (start));
7950 end_byte = CHAR_TO_BYTE (XINT (end));
7951 if (XINT (end) - XINT (start) == end_byte - start_byte)
7952 return Qt;
7164 7953
7165 args[0] = unencodable_char_position (safe_chars, from, p, GPT_ADDR, n); 7954 if (XINT (start) < GPT && XINT (end) > GPT)
7166 n -= XINT (Flength (args[0])); 7955 {
7167 if (n <= 0) 7956 if ((GPT - XINT (start)) < (XINT (end) - GPT))
7168 positions = args[0]; 7957 move_gap_both (XINT (start), start_byte);
7958 else
7959 move_gap_both (XINT (end), end_byte);
7960 }
7961 pos = XINT (start);
7962 }
7963
7964 list = Qnil;
7965 for (tail = coding_system_list; CONSP (tail); tail = XCDR (tail))
7966 {
7967 elt = XCAR (tail);
7968 attrs = AREF (CODING_SYSTEM_SPEC (elt), 0);
7969 ASET (attrs, coding_attr_trans_tbl,
7970 get_translation_table (attrs, 1, NULL));
7971 list = Fcons (Fcons (elt, Fcons (attrs, Qnil)), list);
7972 }
7973
7974 if (STRINGP (start))
7975 p = pbeg = SDATA (start);
7976 else
7977 p = pbeg = BYTE_POS_ADDR (start_byte);
7978 pend = p + (end_byte - start_byte);
7979
7980 while (p < pend && ASCII_BYTE_P (*p)) p++, pos++;
7981 while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--;
7982
7983 while (p < pend)
7984 {
7985 if (ASCII_BYTE_P (*p))
7986 p++;
7169 else 7987 else
7170 { 7988 {
7171 args[1] = unencodable_char_position (safe_chars, GPT, GAP_END_ADDR, 7989 c = STRING_CHAR_ADVANCE (p);
7172 pend, n); 7990
7173 positions = Fappend (2, args); 7991 charset_map_loaded = 0;
7992 for (tail = list; CONSP (tail); tail = XCDR (tail))
7993 {
7994 elt = XCDR (XCAR (tail));
7995 if (! char_encodable_p (c, XCAR (elt)))
7996 XSETCDR (elt, Fcons (make_number (pos), XCDR (elt)));
7997 }
7998 if (charset_map_loaded)
7999 {
8000 EMACS_INT p_offset = p - pbeg, pend_offset = pend - pbeg;
8001
8002 if (STRINGP (start))
8003 pbeg = SDATA (start);
8004 else
8005 pbeg = BYTE_POS_ADDR (start_byte);
8006 p = pbeg + p_offset;
8007 pend = pbeg + pend_offset;
8008 }
7174 } 8009 }
8010 pos++;
8011 }
8012
8013 tail = list;
8014 list = Qnil;
8015 for (; CONSP (tail); tail = XCDR (tail))
8016 {
8017 elt = XCAR (tail);
8018 if (CONSP (XCDR (XCDR (elt))))
8019 list = Fcons (Fcons (XCAR (elt), Fnreverse (XCDR (XCDR (elt)))),
8020 list);
7175 } 8021 }
7176 8022
7177 return (NILP (count) ? Fcar (positions) : positions); 8023 return list;
7178} 8024}
7179 8025
7180 8026
7181Lisp_Object 8027Lisp_Object
7182code_convert_region1 (start, end, coding_system, encodep) 8028code_convert_region (start, end, coding_system, dst_object, encodep, norecord)
7183 Lisp_Object start, end, coding_system; 8029 Lisp_Object start, end, coding_system, dst_object;
7184 int encodep; 8030 int encodep, norecord;
7185{ 8031{
7186 struct coding_system coding; 8032 struct coding_system coding;
7187 int from, to; 8033 EMACS_INT from, from_byte, to, to_byte;
8034 Lisp_Object src_object;
7188 8035
7189 CHECK_NUMBER_COERCE_MARKER (start); 8036 CHECK_NUMBER_COERCE_MARKER (start);
7190 CHECK_NUMBER_COERCE_MARKER (end); 8037 CHECK_NUMBER_COERCE_MARKER (end);
7191 CHECK_SYMBOL (coding_system); 8038 if (NILP (coding_system))
8039 coding_system = Qno_conversion;
8040 else
8041 CHECK_CODING_SYSTEM (coding_system);
8042 src_object = Fcurrent_buffer ();
8043 if (NILP (dst_object))
8044 dst_object = src_object;
8045 else if (! EQ (dst_object, Qt))
8046 CHECK_BUFFER (dst_object);
7192 8047
7193 validate_region (&start, &end); 8048 validate_region (&start, &end);
7194 from = XFASTINT (start); 8049 from = XFASTINT (start);
8050 from_byte = CHAR_TO_BYTE (from);
7195 to = XFASTINT (end); 8051 to = XFASTINT (end);
8052 to_byte = CHAR_TO_BYTE (to);
7196 8053
7197 if (NILP (coding_system)) 8054 setup_coding_system (coding_system, &coding);
7198 return make_number (to - from);
7199
7200 if (setup_coding_system (Fcheck_coding_system (coding_system), &coding) < 0)
7201 error ("Invalid coding system: %s", SDATA (SYMBOL_NAME (coding_system)));
7202
7203 coding.mode |= CODING_MODE_LAST_BLOCK; 8055 coding.mode |= CODING_MODE_LAST_BLOCK;
7204 coding.src_multibyte = coding.dst_multibyte 8056
7205 = !NILP (current_buffer->enable_multibyte_characters); 8057 if (encodep)
7206 code_convert_region (from, CHAR_TO_BYTE (from), to, CHAR_TO_BYTE (to), 8058 encode_coding_object (&coding, src_object, from, from_byte, to, to_byte,
7207 &coding, encodep, 1); 8059 dst_object);
7208 Vlast_coding_system_used = coding.symbol; 8060 else
7209 return make_number (coding.produced_char); 8061 decode_coding_object (&coding, src_object, from, from_byte, to, to_byte,
8062 dst_object);
8063 if (! norecord)
8064 Vlast_coding_system_used = CODING_ID_NAME (coding.id);
8065
8066 return (BUFFERP (dst_object)
8067 ? make_number (coding.produced_char)
8068 : coding.dst_object);
7210} 8069}
7211 8070
8071
7212DEFUN ("decode-coding-region", Fdecode_coding_region, Sdecode_coding_region, 8072DEFUN ("decode-coding-region", Fdecode_coding_region, Sdecode_coding_region,
7213 3, 3, "r\nzCoding system: ", 8073 3, 4, "r\nzCoding system: ",
7214 doc: /* Decode the current region from the specified coding system. 8074 doc: /* Decode the current region from the specified coding system.
7215When called from a program, takes three arguments: 8075When called from a program, takes four arguments:
7216START, END, and CODING-SYSTEM. START and END are buffer positions. 8076 START, END, CODING-SYSTEM, and DESTINATION.
8077START and END are buffer positions.
8078
8079Optional 4th arguments DESTINATION specifies where the decoded text goes.
8080If nil, the region between START and END is replaced by the decoded text.
8081If buffer, the decoded text is inserted in the buffer.
8082If t, the decoded text is returned.
8083
7217This function sets `last-coding-system-used' to the precise coding system 8084This function sets `last-coding-system-used' to the precise coding system
7218used (which may be different from CODING-SYSTEM if CODING-SYSTEM is 8085used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
7219not fully specified.) 8086not fully specified.)
7220It returns the length of the decoded text. */) 8087It returns the length of the decoded text. */)
7221 (start, end, coding_system) 8088 (start, end, coding_system, destination)
7222 Lisp_Object start, end, coding_system; 8089 Lisp_Object start, end, coding_system, destination;
7223{ 8090{
7224 return code_convert_region1 (start, end, coding_system, 0); 8091 return code_convert_region (start, end, coding_system, destination, 0, 0);
7225} 8092}
7226 8093
7227DEFUN ("encode-coding-region", Fencode_coding_region, Sencode_coding_region, 8094DEFUN ("encode-coding-region", Fencode_coding_region, Sencode_coding_region,
7228 3, 3, "r\nzCoding system: ", 8095 3, 4, "r\nzCoding system: ",
7229 doc: /* Encode the current region into the specified coding system. 8096 doc: /* Encode the current region by specified coding system.
7230When called from a program, takes three arguments: 8097When called from a program, takes three arguments:
7231START, END, and CODING-SYSTEM. START and END are buffer positions. 8098START, END, and CODING-SYSTEM. START and END are buffer positions.
8099
8100Optional 4th arguments DESTINATION specifies where the encoded text goes.
8101If nil, the region between START and END is replace by the encoded text.
8102If buffer, the encoded text is inserted in the buffer.
8103If t, the encoded text is returned.
8104
7232This function sets `last-coding-system-used' to the precise coding system 8105This function sets `last-coding-system-used' to the precise coding system
7233used (which may be different from CODING-SYSTEM if CODING-SYSTEM is 8106used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
7234not fully specified.) 8107not fully specified.)
7235It returns the length of the encoded text. */) 8108It returns the length of the encoded text. */)
7236 (start, end, coding_system) 8109 (start, end, coding_system, destination)
7237 Lisp_Object start, end, coding_system; 8110 Lisp_Object start, end, coding_system, destination;
7238{ 8111{
7239 return code_convert_region1 (start, end, coding_system, 1); 8112 return code_convert_region (start, end, coding_system, destination, 1, 0);
7240} 8113}
7241 8114
7242Lisp_Object 8115Lisp_Object
7243code_convert_string1 (string, coding_system, nocopy, encodep) 8116code_convert_string (string, coding_system, dst_object,
7244 Lisp_Object string, coding_system, nocopy; 8117 encodep, nocopy, norecord)
7245 int encodep; 8118 Lisp_Object string, coding_system, dst_object;
8119 int encodep, nocopy, norecord;
7246{ 8120{
7247 struct coding_system coding; 8121 struct coding_system coding;
8122 EMACS_INT chars, bytes;
7248 8123
7249 CHECK_STRING (string); 8124 CHECK_STRING (string);
7250 CHECK_SYMBOL (coding_system);
7251
7252 if (NILP (coding_system)) 8125 if (NILP (coding_system))
7253 return (NILP (nocopy) ? Fcopy_sequence (string) : string); 8126 {
8127 if (! norecord)
8128 Vlast_coding_system_used = Qno_conversion;
8129 if (NILP (dst_object))
8130 return (nocopy ? Fcopy_sequence (string) : string);
8131 }
7254 8132
7255 if (setup_coding_system (Fcheck_coding_system (coding_system), &coding) < 0) 8133 if (NILP (coding_system))
7256 error ("Invalid coding system: %s", SDATA (SYMBOL_NAME (coding_system))); 8134 coding_system = Qno_conversion;
8135 else
8136 CHECK_CODING_SYSTEM (coding_system);
8137 if (NILP (dst_object))
8138 dst_object = Qt;
8139 else if (! EQ (dst_object, Qt))
8140 CHECK_BUFFER (dst_object);
7257 8141
8142 setup_coding_system (coding_system, &coding);
7258 coding.mode |= CODING_MODE_LAST_BLOCK; 8143 coding.mode |= CODING_MODE_LAST_BLOCK;
7259 string = (encodep 8144 chars = SCHARS (string);
7260 ? encode_coding_string (string, &coding, !NILP (nocopy)) 8145 bytes = SBYTES (string);
7261 : decode_coding_string (string, &coding, !NILP (nocopy))); 8146 if (encodep)
7262 Vlast_coding_system_used = coding.symbol; 8147 encode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object);
8148 else
8149 decode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object);
8150 if (! norecord)
8151 Vlast_coding_system_used = CODING_ID_NAME (coding.id);
7263 8152
7264 return string; 8153 return (BUFFERP (dst_object)
8154 ? make_number (coding.produced_char)
8155 : coding.dst_object);
7265} 8156}
7266 8157
7267DEFUN ("decode-coding-string", Fdecode_coding_string, Sdecode_coding_string,
7268 2, 3, 0,
7269 doc: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
7270Optional arg NOCOPY non-nil means it is OK to return STRING itself
7271if the decoding operation is trivial.
7272This function sets `last-coding-system-used' to the precise coding system
7273used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
7274not fully specified.) */)
7275 (string, coding_system, nocopy)
7276 Lisp_Object string, coding_system, nocopy;
7277{
7278 return code_convert_string1 (string, coding_system, nocopy, 0);
7279}
7280
7281DEFUN ("encode-coding-string", Fencode_coding_string, Sencode_coding_string,
7282 2, 3, 0,
7283 doc: /* Encode STRING to CODING-SYSTEM, and return the result.
7284Optional arg NOCOPY non-nil means it is OK to return STRING itself
7285if the encoding operation is trivial.
7286This function sets `last-coding-system-used' to the precise coding system
7287used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
7288not fully specified.) */)
7289 (string, coding_system, nocopy)
7290 Lisp_Object string, coding_system, nocopy;
7291{
7292 return code_convert_string1 (string, coding_system, nocopy, 1);
7293}
7294 8158
7295/* Encode or decode STRING according to CODING_SYSTEM. 8159/* Encode or decode STRING according to CODING_SYSTEM.
7296 Do not set Vlast_coding_system_used. 8160 Do not set Vlast_coding_system_used.
@@ -7303,23 +8167,52 @@ code_convert_string_norecord (string, coding_system, encodep)
7303 Lisp_Object string, coding_system; 8167 Lisp_Object string, coding_system;
7304 int encodep; 8168 int encodep;
7305{ 8169{
7306 struct coding_system coding; 8170 return code_convert_string (string, coding_system, Qt, encodep, 0, 1);
8171}
7307 8172
7308 CHECK_STRING (string);
7309 CHECK_SYMBOL (coding_system);
7310 8173
7311 if (NILP (coding_system)) 8174DEFUN ("decode-coding-string", Fdecode_coding_string, Sdecode_coding_string,
7312 return string; 8175 2, 4, 0,
8176 doc: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
8177
8178Optional third arg NOCOPY non-nil means it is OK to return STRING itself
8179if the decoding operation is trivial.
7313 8180
7314 if (setup_coding_system (Fcheck_coding_system (coding_system), &coding) < 0) 8181Optional fourth arg BUFFER non-nil meant that the decoded text is
7315 error ("Invalid coding system: %s", SDATA (SYMBOL_NAME (coding_system))); 8182inserted in BUFFER instead of returned as a string. In this case,
8183the return value is BUFFER.
7316 8184
7317 coding.composing = COMPOSITION_DISABLED; 8185This function sets `last-coding-system-used' to the precise coding system
7318 coding.mode |= CODING_MODE_LAST_BLOCK; 8186used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
7319 return (encodep 8187not fully specified. */)
7320 ? encode_coding_string (string, &coding, 1) 8188 (string, coding_system, nocopy, buffer)
7321 : decode_coding_string (string, &coding, 1)); 8189 Lisp_Object string, coding_system, nocopy, buffer;
8190{
8191 return code_convert_string (string, coding_system, buffer,
8192 0, ! NILP (nocopy), 0);
8193}
8194
8195DEFUN ("encode-coding-string", Fencode_coding_string, Sencode_coding_string,
8196 2, 4, 0,
8197 doc: /* Encode STRING to CODING-SYSTEM, and return the result.
8198
8199Optional third arg NOCOPY non-nil means it is OK to return STRING
8200itself if the encoding operation is trivial.
8201
8202Optional fourth arg BUFFER non-nil meant that the encoded text is
8203inserted in BUFFER instead of returned as a string. In this case,
8204the return value is BUFFER.
8205
8206This function sets `last-coding-system-used' to the precise coding system
8207used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
8208not fully specified.) */)
8209 (string, coding_system, nocopy, buffer)
8210 Lisp_Object string, coding_system, nocopy, buffer;
8211{
8212 return code_convert_string (string, coding_system, buffer,
8213 1, ! NILP (nocopy), 1);
7322} 8214}
8215
7323 8216
7324DEFUN ("decode-sjis-char", Fdecode_sjis_char, Sdecode_sjis_char, 1, 1, 0, 8217DEFUN ("decode-sjis-char", Fdecode_sjis_char, Sdecode_sjis_char, 1, 1, 0,
7325 doc: /* Decode a Japanese character which has CODE in shift_jis encoding. 8218 doc: /* Decode a Japanese character which has CODE in shift_jis encoding.
@@ -7327,60 +8220,75 @@ Return the corresponding character. */)
7327 (code) 8220 (code)
7328 Lisp_Object code; 8221 Lisp_Object code;
7329{ 8222{
7330 unsigned char c1, c2, s1, s2; 8223 Lisp_Object spec, attrs, val;
7331 Lisp_Object val; 8224 struct charset *charset_roman, *charset_kanji, *charset_kana, *charset;
8225 int c;
7332 8226
7333 CHECK_NUMBER (code); 8227 CHECK_NATNUM (code);
7334 s1 = (XFASTINT (code)) >> 8, s2 = (XFASTINT (code)) & 0xFF; 8228 c = XFASTINT (code);
7335 if (s1 == 0) 8229 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec);
8230 attrs = AREF (spec, 0);
8231
8232 if (ASCII_BYTE_P (c)
8233 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
8234 return code;
8235
8236 val = CODING_ATTR_CHARSET_LIST (attrs);
8237 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
8238 charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
8239 charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val)));
8240
8241 if (c <= 0x7F)
8242 charset = charset_roman;
8243 else if (c >= 0xA0 && c < 0xDF)
7336 { 8244 {
7337 if (s2 < 0x80) 8245 charset = charset_kana;
7338 XSETFASTINT (val, s2); 8246 c -= 0x80;
7339 else if (s2 >= 0xA0 || s2 <= 0xDF)
7340 XSETFASTINT (val, MAKE_CHAR (charset_katakana_jisx0201, s2, 0));
7341 else
7342 error ("Invalid Shift JIS code: %x", XFASTINT (code));
7343 } 8247 }
7344 else 8248 else
7345 { 8249 {
7346 if ((s1 < 0x80 || (s1 > 0x9F && s1 < 0xE0) || s1 > 0xEF) 8250 int s1 = c >> 8, s2 = c & 0xFF;
7347 || (s2 < 0x40 || s2 == 0x7F || s2 > 0xFC)) 8251
7348 error ("Invalid Shift JIS code: %x", XFASTINT (code)); 8252 if (s1 < 0x81 || (s1 > 0x9F && s1 < 0xE0) || s1 > 0xEF
7349 DECODE_SJIS (s1, s2, c1, c2); 8253 || s2 < 0x40 || s2 == 0x7F || s2 > 0xFC)
7350 XSETFASTINT (val, MAKE_CHAR (charset_jisx0208, c1, c2)); 8254 error ("Invalid code: %d", code);
8255 SJIS_TO_JIS (c);
8256 charset = charset_kanji;
7351 } 8257 }
7352 return val; 8258 c = DECODE_CHAR (charset, c);
8259 if (c < 0)
8260 error ("Invalid code: %d", code);
8261 return make_number (c);
7353} 8262}
7354 8263
8264
7355DEFUN ("encode-sjis-char", Fencode_sjis_char, Sencode_sjis_char, 1, 1, 0, 8265DEFUN ("encode-sjis-char", Fencode_sjis_char, Sencode_sjis_char, 1, 1, 0,
7356 doc: /* Encode a Japanese character CH to shift_jis encoding. 8266 doc: /* Encode a Japanese character CH to shift_jis encoding.
7357Return the corresponding code in SJIS. */) 8267Return the corresponding code in SJIS. */)
7358 (ch) 8268 (ch)
7359 Lisp_Object ch; 8269 Lisp_Object ch;
7360{ 8270{
7361 int charset, c1, c2, s1, s2; 8271 Lisp_Object spec, attrs, charset_list;
7362 Lisp_Object val; 8272 int c;
8273 struct charset *charset;
8274 unsigned code;
7363 8275
7364 CHECK_NUMBER (ch); 8276 CHECK_CHARACTER (ch);
7365 SPLIT_CHAR (XFASTINT (ch), charset, c1, c2); 8277 c = XFASTINT (ch);
7366 if (charset == CHARSET_ASCII) 8278 CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec);
7367 { 8279 attrs = AREF (spec, 0);
7368 val = ch; 8280
7369 } 8281 if (ASCII_CHAR_P (c)
7370 else if (charset == charset_jisx0208 8282 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
7371 && c1 > 0x20 && c1 < 0x7F && c2 > 0x20 && c2 < 0x7F) 8283 return ch;
7372 { 8284
7373 ENCODE_SJIS (c1, c2, s1, s2); 8285 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
7374 XSETFASTINT (val, (s1 << 8) | s2); 8286 charset = char_charset (c, charset_list, &code);
7375 } 8287 if (code == CHARSET_INVALID_CODE (charset))
7376 else if (charset == charset_katakana_jisx0201 8288 error ("Can't encode by shift_jis encoding: %d", c);
7377 && c1 > 0x20 && c2 < 0xE0) 8289 JIS_TO_SJIS (code);
7378 { 8290
7379 XSETFASTINT (val, c1 | 0x80); 8291 return make_number (code);
7380 }
7381 else
7382 error ("Can't encode to shift_jis: %d", XFASTINT (ch));
7383 return val;
7384} 8292}
7385 8293
7386DEFUN ("decode-big5-char", Fdecode_big5_char, Sdecode_big5_char, 1, 1, 0, 8294DEFUN ("decode-big5-char", Fdecode_big5_char, Sdecode_big5_char, 1, 1, 0,
@@ -7389,27 +8297,37 @@ Return the corresponding character. */)
7389 (code) 8297 (code)
7390 Lisp_Object code; 8298 Lisp_Object code;
7391{ 8299{
7392 int charset; 8300 Lisp_Object spec, attrs, val;
7393 unsigned char b1, b2, c1, c2; 8301 struct charset *charset_roman, *charset_big5, *charset;
7394 Lisp_Object val; 8302 int c;
7395 8303
7396 CHECK_NUMBER (code); 8304 CHECK_NATNUM (code);
7397 b1 = (XFASTINT (code)) >> 8, b2 = (XFASTINT (code)) & 0xFF; 8305 c = XFASTINT (code);
7398 if (b1 == 0) 8306 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec);
7399 { 8307 attrs = AREF (spec, 0);
7400 if (b2 >= 0x80) 8308
7401 error ("Invalid BIG5 code: %x", XFASTINT (code)); 8309 if (ASCII_BYTE_P (c)
7402 val = code; 8310 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
7403 } 8311 return code;
8312
8313 val = CODING_ATTR_CHARSET_LIST (attrs);
8314 charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val);
8315 charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val)));
8316
8317 if (c <= 0x7F)
8318 charset = charset_roman;
7404 else 8319 else
7405 { 8320 {
7406 if ((b1 < 0xA1 || b1 > 0xFE) 8321 int b1 = c >> 8, b2 = c & 0x7F;
7407 || (b2 < 0x40 || (b2 > 0x7E && b2 < 0xA1) || b2 > 0xFE)) 8322 if (b1 < 0xA1 || b1 > 0xFE
7408 error ("Invalid BIG5 code: %x", XFASTINT (code)); 8323 || b2 < 0x40 || (b2 > 0x7E && b2 < 0xA1) || b2 > 0xFE)
7409 DECODE_BIG5 (b1, b2, charset, c1, c2); 8324 error ("Invalid code: %d", code);
7410 XSETFASTINT (val, MAKE_CHAR (charset, c1, c2)); 8325 charset = charset_big5;
7411 } 8326 }
7412 return val; 8327 c = DECODE_CHAR (charset, (unsigned )c);
8328 if (c < 0)
8329 error ("Invalid code: %d", code);
8330 return make_number (c);
7413} 8331}
7414 8332
7415DEFUN ("encode-big5-char", Fencode_big5_char, Sencode_big5_char, 1, 1, 0, 8333DEFUN ("encode-big5-char", Fencode_big5_char, Sencode_big5_char, 1, 1, 0,
@@ -7418,27 +8336,27 @@ Return the corresponding character code in Big5. */)
7418 (ch) 8336 (ch)
7419 Lisp_Object ch; 8337 Lisp_Object ch;
7420{ 8338{
7421 int charset, c1, c2, b1, b2; 8339 Lisp_Object spec, attrs, charset_list;
7422 Lisp_Object val; 8340 struct charset *charset;
7423 8341 int c;
7424 CHECK_NUMBER (ch); 8342 unsigned code;
7425 SPLIT_CHAR (XFASTINT (ch), charset, c1, c2); 8343
7426 if (charset == CHARSET_ASCII) 8344 CHECK_CHARACTER (ch);
7427 { 8345 c = XFASTINT (ch);
7428 val = ch; 8346 CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec);
7429 } 8347 attrs = AREF (spec, 0);
7430 else if ((charset == charset_big5_1 8348 if (ASCII_CHAR_P (c)
7431 && (XFASTINT (ch) >= 0x250a1 && XFASTINT (ch) <= 0x271ec)) 8349 && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)))
7432 || (charset == charset_big5_2 8350 return ch;
7433 && XFASTINT (ch) >= 0x290a1 && XFASTINT (ch) <= 0x2bdb2)) 8351
7434 { 8352 charset_list = CODING_ATTR_CHARSET_LIST (attrs);
7435 ENCODE_BIG5 (charset, c1, c2, b1, b2); 8353 charset = char_charset (c, charset_list, &code);
7436 XSETFASTINT (val, (b1 << 8) | b2); 8354 if (code == CHARSET_INVALID_CODE (charset))
7437 } 8355 error ("Can't encode by Big5 encoding: %d", c);
7438 else 8356
7439 error ("Can't encode to Big5: %d", XFASTINT (ch)); 8357 return make_number (code);
7440 return val;
7441} 8358}
8359
7442 8360
7443DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal, 8361DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal,
7444 Sset_terminal_coding_system_internal, 1, 2, 0, 8362 Sset_terminal_coding_system_internal, 1, 2, 0,
@@ -7451,17 +8369,16 @@ DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_intern
7451 CHECK_SYMBOL (coding_system); 8369 CHECK_SYMBOL (coding_system);
7452 setup_coding_system (Fcheck_coding_system (coding_system), terminal_coding); 8370 setup_coding_system (Fcheck_coding_system (coding_system), terminal_coding);
7453 /* We had better not send unsafe characters to terminal. */ 8371 /* We had better not send unsafe characters to terminal. */
7454 terminal_coding->mode |= CODING_MODE_INHIBIT_UNENCODABLE_CHAR; 8372 terminal_coding->mode |= CODING_MODE_SAFE_ENCODING;
7455 /* Character composition should be disabled. */ 8373 /* Characer composition should be disabled. */
7456 terminal_coding->composing = COMPOSITION_DISABLED; 8374 terminal_coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
7457 /* Error notification should be suppressed. */
7458 terminal_coding->suppress_error = 1;
7459 terminal_coding->src_multibyte = 1; 8375 terminal_coding->src_multibyte = 1;
7460 terminal_coding->dst_multibyte = 0; 8376 terminal_coding->dst_multibyte = 0;
7461 return Qnil; 8377 return Qnil;
7462} 8378}
7463 8379
7464DEFUN ("set-safe-terminal-coding-system-internal", Fset_safe_terminal_coding_system_internal, 8380DEFUN ("set-safe-terminal-coding-system-internal",
8381 Fset_safe_terminal_coding_system_internal,
7465 Sset_safe_terminal_coding_system_internal, 1, 1, 0, 8382 Sset_safe_terminal_coding_system_internal, 1, 1, 0,
7466 doc: /* Internal use only. */) 8383 doc: /* Internal use only. */)
7467 (coding_system) 8384 (coding_system)
@@ -7470,10 +8387,8 @@ DEFUN ("set-safe-terminal-coding-system-internal", Fset_safe_terminal_coding_sys
7470 CHECK_SYMBOL (coding_system); 8387 CHECK_SYMBOL (coding_system);
7471 setup_coding_system (Fcheck_coding_system (coding_system), 8388 setup_coding_system (Fcheck_coding_system (coding_system),
7472 &safe_terminal_coding); 8389 &safe_terminal_coding);
7473 /* Character composition should be disabled. */ 8390 /* Characer composition should be disabled. */
7474 safe_terminal_coding.composing = COMPOSITION_DISABLED; 8391 safe_terminal_coding.common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK;
7475 /* Error notification should be suppressed. */
7476 safe_terminal_coding.suppress_error = 1;
7477 safe_terminal_coding.src_multibyte = 1; 8392 safe_terminal_coding.src_multibyte = 1;
7478 safe_terminal_coding.dst_multibyte = 0; 8393 safe_terminal_coding.dst_multibyte = 0;
7479 return Qnil; 8394 return Qnil;
@@ -7487,7 +8402,12 @@ frame's terminal device. */)
7487 (terminal) 8402 (terminal)
7488 Lisp_Object terminal; 8403 Lisp_Object terminal;
7489{ 8404{
7490 return TERMINAL_TERMINAL_CODING (get_terminal (terminal, 1))->symbol; 8405 struct coding_system *terminal_coding
8406 = TERMINAL_TERMINAL_CODING (get_terminal (terminal, 1));
8407 Lisp_Object coding_system = CODING_ID_NAME (terminal_coding->id);
8408
8409 /* For backward compatibility, return nil if it is `undecided'. */
8410 return (! EQ (coding_system, Qundecided) ? coding_system : Qnil);
7491} 8411}
7492 8412
7493DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal, 8413DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal,
@@ -7499,23 +8419,22 @@ DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_intern
7499{ 8419{
7500 struct terminal *t = get_terminal (terminal, 1); 8420 struct terminal *t = get_terminal (terminal, 1);
7501 CHECK_SYMBOL (coding_system); 8421 CHECK_SYMBOL (coding_system);
7502
7503 setup_coding_system (Fcheck_coding_system (coding_system), 8422 setup_coding_system (Fcheck_coding_system (coding_system),
7504 TERMINAL_KEYBOARD_CODING (t)); 8423 TERMINAL_KEYBOARD_CODING (t));
7505 /* Character composition should be disabled. */ 8424 /* Characer composition should be disabled. */
7506 TERMINAL_KEYBOARD_CODING (t)->composing = COMPOSITION_DISABLED; 8425 TERMINAL_KEYBOARD_CODING (t)->common_flags
8426 &= ~CODING_ANNOTATE_COMPOSITION_MASK;
7507 return Qnil; 8427 return Qnil;
7508} 8428}
7509 8429
7510DEFUN ("keyboard-coding-system", Fkeyboard_coding_system, 8430DEFUN ("keyboard-coding-system",
7511 Skeyboard_coding_system, 0, 1, 0, 8431 Fkeyboard_coding_system, Skeyboard_coding_system, 0, 1, 0,
7512 doc: /* Return coding system for decoding keyboard input on TERMINAL. 8432 doc: /* Return coding system specified for decoding keyboard input. */)
7513TERMINAL may be a terminal id, a frame, or nil for the selected
7514frame's terminal device. */)
7515 (terminal) 8433 (terminal)
7516 Lisp_Object terminal; 8434 Lisp_Object terminal;
7517{ 8435{
7518 return TERMINAL_KEYBOARD_CODING (get_terminal (terminal, 1))->symbol; 8436 return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING
8437 (get_terminal (terminal, 1))->id);
7519} 8438}
7520 8439
7521 8440
@@ -7572,23 +8491,16 @@ usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
7572 operation = args[0]; 8491 operation = args[0];
7573 if (!SYMBOLP (operation) 8492 if (!SYMBOLP (operation)
7574 || !INTEGERP (target_idx = Fget (operation, Qtarget_idx))) 8493 || !INTEGERP (target_idx = Fget (operation, Qtarget_idx)))
7575 error ("Invalid first argument"); 8494 error ("Invalid first arguement");
7576 if (nargs < 1 + XINT (target_idx)) 8495 if (nargs < 1 + XINT (target_idx))
7577 error ("Too few arguments for operation: %s", 8496 error ("Too few arguments for operation: %s",
7578 SDATA (SYMBOL_NAME (operation))); 8497 SDATA (SYMBOL_NAME (operation)));
7579 /* For write-region, if the 6th argument (i.e. VISIT, the 5th
7580 argument to write-region) is string, it must be treated as a
7581 target file name. */
7582 if (EQ (operation, Qwrite_region)
7583 && nargs > 5
7584 && STRINGP (args[5]))
7585 target_idx = make_number (4);
7586 target = args[XINT (target_idx) + 1]; 8498 target = args[XINT (target_idx) + 1];
7587 if (!(STRINGP (target) 8499 if (!(STRINGP (target)
7588 || (EQ (operation, Qinsert_file_contents) && CONSP (target) 8500 || (EQ (operation, Qinsert_file_contents) && CONSP (target)
7589 && STRINGP (XCAR (target)) && BUFFERP (XCDR (target))) 8501 && STRINGP (XCAR (target)) && BUFFERP (XCDR (target)))
7590 || (EQ (operation, Qopen_network_stream) && INTEGERP (target)))) 8502 || (EQ (operation, Qopen_network_stream) && INTEGERP (target))))
7591 error ("Invalid argument %d", XINT (target_idx) + 1); 8503 error ("Invalid %dth argument", XINT (target_idx) + 1);
7592 if (CONSP (target)) 8504 if (CONSP (target))
7593 target = XCAR (target); 8505 target = XCAR (target);
7594 8506
@@ -7604,8 +8516,8 @@ usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
7604 for (; CONSP (chain); chain = XCDR (chain)) 8516 for (; CONSP (chain); chain = XCDR (chain))
7605 { 8517 {
7606 Lisp_Object elt; 8518 Lisp_Object elt;
7607 elt = XCAR (chain);
7608 8519
8520 elt = XCAR (chain);
7609 if (CONSP (elt) 8521 if (CONSP (elt)
7610 && ((STRINGP (target) 8522 && ((STRINGP (target)
7611 && STRINGP (XCAR (elt)) 8523 && STRINGP (XCAR (elt))
@@ -7638,103 +8550,801 @@ usage: (find-operation-coding-system OPERATION ARGUMENTS...) */)
7638 return Qnil; 8550 return Qnil;
7639} 8551}
7640 8552
7641DEFUN ("update-coding-systems-internal", Fupdate_coding_systems_internal, 8553DEFUN ("set-coding-system-priority", Fset_coding_system_priority,
7642 Supdate_coding_systems_internal, 0, 0, 0, 8554 Sset_coding_system_priority, 0, MANY, 0,
7643 doc: /* Update internal database for ISO2022 and CCL based coding systems. 8555 doc: /* Assign higher priority to the coding systems given as arguments.
7644When values of any coding categories are changed, you must 8556If multiple coding systems belongs to the same category,
7645call this function. */) 8557all but the first one are ignored.
7646 () 8558
8559usage: (set-coding-system-priority ...) */)
8560 (nargs, args)
8561 int nargs;
8562 Lisp_Object *args;
7647{ 8563{
7648 int i; 8564 int i, j;
8565 int changed[coding_category_max];
8566 enum coding_category priorities[coding_category_max];
8567
8568 bzero (changed, sizeof changed);
7649 8569
7650 for (i = CODING_CATEGORY_IDX_EMACS_MULE; i < CODING_CATEGORY_IDX_MAX; i++) 8570 for (i = j = 0; i < nargs; i++)
7651 { 8571 {
7652 Lisp_Object val; 8572 enum coding_category category;
8573 Lisp_Object spec, attrs;
7653 8574
7654 val = find_symbol_value (XVECTOR (Vcoding_category_table)->contents[i]); 8575 CHECK_CODING_SYSTEM_GET_SPEC (args[i], spec);
7655 if (!NILP (val)) 8576 attrs = AREF (spec, 0);
7656 { 8577 category = XINT (CODING_ATTR_CATEGORY (attrs));
7657 if (! coding_system_table[i]) 8578 if (changed[category])
7658 coding_system_table[i] = ((struct coding_system *) 8579 /* Ignore this coding system because a coding system of the
7659 xmalloc (sizeof (struct coding_system))); 8580 same category already had a higher priority. */
7660 setup_coding_system (val, coding_system_table[i]); 8581 continue;
7661 } 8582 changed[category] = 1;
7662 else if (coding_system_table[i]) 8583 priorities[j++] = category;
7663 { 8584 if (coding_categories[category].id >= 0
7664 xfree (coding_system_table[i]); 8585 && ! EQ (args[i], CODING_ID_NAME (coding_categories[category].id)))
7665 coding_system_table[i] = NULL; 8586 setup_coding_system (args[i], &coding_categories[category]);
7666 } 8587 Fset (AREF (Vcoding_category_table, category), args[i]);
8588 }
8589
8590 /* Now we have decided top J priorities. Reflect the order of the
8591 original priorities to the remaining priorities. */
8592
8593 for (i = j, j = 0; i < coding_category_max; i++, j++)
8594 {
8595 while (j < coding_category_max
8596 && changed[coding_priorities[j]])
8597 j++;
8598 if (j == coding_category_max)
8599 abort ();
8600 priorities[i] = coding_priorities[j];
7667 } 8601 }
7668 8602
8603 bcopy (priorities, coding_priorities, sizeof priorities);
8604
8605 /* Update `coding-category-list'. */
8606 Vcoding_category_list = Qnil;
8607 for (i = coding_category_max - 1; i >= 0; i--)
8608 Vcoding_category_list
8609 = Fcons (AREF (Vcoding_category_table, priorities[i]),
8610 Vcoding_category_list);
8611
7669 return Qnil; 8612 return Qnil;
7670} 8613}
7671 8614
7672DEFUN ("set-coding-priority-internal", Fset_coding_priority_internal, 8615DEFUN ("coding-system-priority-list", Fcoding_system_priority_list,
7673 Sset_coding_priority_internal, 0, 0, 0, 8616 Scoding_system_priority_list, 0, 1, 0,
7674 doc: /* Update internal database for the current value of `coding-category-list'. 8617 doc: /* Return a list of coding systems ordered by their priorities.
7675This function is internal use only. */) 8618HIGHESTP non-nil means just return the highest priority one. */)
7676 () 8619 (highestp)
8620 Lisp_Object highestp;
7677{ 8621{
7678 int i = 0, idx; 8622 int i;
7679 Lisp_Object val; 8623 Lisp_Object val;
7680 8624
7681 val = Vcoding_category_list; 8625 for (i = 0, val = Qnil; i < coding_category_max; i++)
7682
7683 while (CONSP (val) && i < CODING_CATEGORY_IDX_MAX)
7684 { 8626 {
7685 if (! SYMBOLP (XCAR (val))) 8627 enum coding_category category = coding_priorities[i];
7686 break; 8628 int id = coding_categories[category].id;
7687 idx = XFASTINT (Fget (XCAR (val), Qcoding_category_index)); 8629 Lisp_Object attrs;
7688 if (idx >= CODING_CATEGORY_IDX_MAX) 8630
7689 break; 8631 if (id < 0)
7690 coding_priorities[i++] = (1 << idx); 8632 continue;
7691 val = XCDR (val); 8633 attrs = CODING_ID_ATTRS (id);
8634 if (! NILP (highestp))
8635 return CODING_ATTR_BASE_NAME (attrs);
8636 val = Fcons (CODING_ATTR_BASE_NAME (attrs), val);
7692 } 8637 }
7693 /* If coding-category-list is valid and contains all coding 8638 return Fnreverse (val);
7694 categories, `i' should be CODING_CATEGORY_IDX_MAX now. If not, 8639}
7695 the following code saves Emacs from crashing. */
7696 while (i < CODING_CATEGORY_IDX_MAX)
7697 coding_priorities[i++] = CODING_CATEGORY_MASK_RAW_TEXT;
7698 8640
7699 return Qnil; 8641static char *suffixes[] = { "-unix", "-dos", "-mac" };
8642
8643static Lisp_Object
8644make_subsidiaries (base)
8645 Lisp_Object base;
8646{
8647 Lisp_Object subsidiaries;
8648 int base_name_len = SBYTES (SYMBOL_NAME (base));
8649 char *buf = (char *) alloca (base_name_len + 6);
8650 int i;
8651
8652 bcopy (SDATA (SYMBOL_NAME (base)), buf, base_name_len);
8653 subsidiaries = Fmake_vector (make_number (3), Qnil);
8654 for (i = 0; i < 3; i++)
8655 {
8656 bcopy (suffixes[i], buf + base_name_len, strlen (suffixes[i]) + 1);
8657 ASET (subsidiaries, i, intern (buf));
8658 }
8659 return subsidiaries;
7700} 8660}
7701 8661
8662
7702DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal, 8663DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal,
7703 Sdefine_coding_system_internal, 1, 1, 0, 8664 Sdefine_coding_system_internal, coding_arg_max, MANY, 0,
7704 doc: /* Register CODING-SYSTEM as a base coding system. 8665 doc: /* For internal use only.
7705This function is internal use only. */) 8666usage: (define-coding-system-internal ...) */)
7706 (coding_system) 8667 (nargs, args)
7707 Lisp_Object coding_system; 8668 int nargs;
8669 Lisp_Object *args;
7708{ 8670{
7709 Lisp_Object safe_chars, slot; 8671 Lisp_Object name;
8672 Lisp_Object spec_vec; /* [ ATTRS ALIASE EOL_TYPE ] */
8673 Lisp_Object attrs; /* Vector of attributes. */
8674 Lisp_Object eol_type;
8675 Lisp_Object aliases;
8676 Lisp_Object coding_type, charset_list, safe_charsets;
8677 enum coding_category category;
8678 Lisp_Object tail, val;
8679 int max_charset_id = 0;
8680 int i;
8681
8682 if (nargs < coding_arg_max)
8683 goto short_args;
8684
8685 attrs = Fmake_vector (make_number (coding_attr_last_index), Qnil);
8686
8687 name = args[coding_arg_name];
8688 CHECK_SYMBOL (name);
8689 CODING_ATTR_BASE_NAME (attrs) = name;
8690
8691 val = args[coding_arg_mnemonic];
8692 if (! STRINGP (val))
8693 CHECK_CHARACTER (val);
8694 CODING_ATTR_MNEMONIC (attrs) = val;
8695
8696 coding_type = args[coding_arg_coding_type];
8697 CHECK_SYMBOL (coding_type);
8698 CODING_ATTR_TYPE (attrs) = coding_type;
8699
8700 charset_list = args[coding_arg_charset_list];
8701 if (SYMBOLP (charset_list))
8702 {
8703 if (EQ (charset_list, Qiso_2022))
8704 {
8705 if (! EQ (coding_type, Qiso_2022))
8706 error ("Invalid charset-list");
8707 charset_list = Viso_2022_charset_list;
8708 }
8709 else if (EQ (charset_list, Qemacs_mule))
8710 {
8711 if (! EQ (coding_type, Qemacs_mule))
8712 error ("Invalid charset-list");
8713 charset_list = Vemacs_mule_charset_list;
8714 }
8715 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
8716 if (max_charset_id < XFASTINT (XCAR (tail)))
8717 max_charset_id = XFASTINT (XCAR (tail));
8718 }
8719 else
8720 {
8721 charset_list = Fcopy_sequence (charset_list);
8722 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
8723 {
8724 struct charset *charset;
8725
8726 val = XCAR (tail);
8727 CHECK_CHARSET_GET_CHARSET (val, charset);
8728 if (EQ (coding_type, Qiso_2022)
8729 ? CHARSET_ISO_FINAL (charset) < 0
8730 : EQ (coding_type, Qemacs_mule)
8731 ? CHARSET_EMACS_MULE_ID (charset) < 0
8732 : 0)
8733 error ("Can't handle charset `%s'",
8734 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
8735
8736 XSETCAR (tail, make_number (charset->id));
8737 if (max_charset_id < charset->id)
8738 max_charset_id = charset->id;
8739 }
8740 }
8741 CODING_ATTR_CHARSET_LIST (attrs) = charset_list;
8742
8743 safe_charsets = Fmake_string (make_number (max_charset_id + 1),
8744 make_number (255));
8745 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
8746 SSET (safe_charsets, XFASTINT (XCAR (tail)), 0);
8747 CODING_ATTR_SAFE_CHARSETS (attrs) = safe_charsets;
8748
8749 CODING_ATTR_ASCII_COMPAT (attrs) = args[coding_arg_ascii_compatible_p];
8750
8751 val = args[coding_arg_decode_translation_table];
8752 if (! CHAR_TABLE_P (val) && ! CONSP (val))
8753 CHECK_SYMBOL (val);
8754 CODING_ATTR_DECODE_TBL (attrs) = val;
8755
8756 val = args[coding_arg_encode_translation_table];
8757 if (! CHAR_TABLE_P (val) && ! CONSP (val))
8758 CHECK_SYMBOL (val);
8759 CODING_ATTR_ENCODE_TBL (attrs) = val;
8760
8761 val = args[coding_arg_post_read_conversion];
8762 CHECK_SYMBOL (val);
8763 CODING_ATTR_POST_READ (attrs) = val;
8764
8765 val = args[coding_arg_pre_write_conversion];
8766 CHECK_SYMBOL (val);
8767 CODING_ATTR_PRE_WRITE (attrs) = val;
8768
8769 val = args[coding_arg_default_char];
8770 if (NILP (val))
8771 CODING_ATTR_DEFAULT_CHAR (attrs) = make_number (' ');
8772 else
8773 {
8774 CHECK_CHARACTER (val);
8775 CODING_ATTR_DEFAULT_CHAR (attrs) = val;
8776 }
8777
8778 val = args[coding_arg_for_unibyte];
8779 CODING_ATTR_FOR_UNIBYTE (attrs) = NILP (val) ? Qnil : Qt;
8780
8781 val = args[coding_arg_plist];
8782 CHECK_LIST (val);
8783 CODING_ATTR_PLIST (attrs) = val;
8784
8785 if (EQ (coding_type, Qcharset))
8786 {
8787 /* Generate a lisp vector of 256 elements. Each element is nil,
8788 integer, or a list of charset IDs.
8789
8790 If Nth element is nil, the byte code N is invalid in this
8791 coding system.
8792
8793 If Nth element is a number NUM, N is the first byte of a
8794 charset whose ID is NUM.
8795
8796 If Nth element is a list of charset IDs, N is the first byte
8797 of one of them. The list is sorted by dimensions of the
8798 charsets. A charset of smaller dimension comes firtst. */
8799 val = Fmake_vector (make_number (256), Qnil);
8800
8801 for (tail = charset_list; CONSP (tail); tail = XCDR (tail))
8802 {
8803 struct charset *charset = CHARSET_FROM_ID (XFASTINT (XCAR (tail)));
8804 int dim = CHARSET_DIMENSION (charset);
8805 int idx = (dim - 1) * 4;
8806
8807 if (CHARSET_ASCII_COMPATIBLE_P (charset))
8808 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
8809
8810 for (i = charset->code_space[idx];
8811 i <= charset->code_space[idx + 1]; i++)
8812 {
8813 Lisp_Object tmp, tmp2;
8814 int dim2;
8815
8816 tmp = AREF (val, i);
8817 if (NILP (tmp))
8818 tmp = XCAR (tail);
8819 else if (NUMBERP (tmp))
8820 {
8821 dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp)));
8822 if (dim < dim2)
8823 tmp = Fcons (XCAR (tail), Fcons (tmp, Qnil));
8824 else
8825 tmp = Fcons (tmp, Fcons (XCAR (tail), Qnil));
8826 }
8827 else
8828 {
8829 for (tmp2 = tmp; CONSP (tmp2); tmp2 = XCDR (tmp2))
8830 {
8831 dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2))));
8832 if (dim < dim2)
8833 break;
8834 }
8835 if (NILP (tmp2))
8836 tmp = nconc2 (tmp, Fcons (XCAR (tail), Qnil));
8837 else
8838 {
8839 XSETCDR (tmp2, Fcons (XCAR (tmp2), XCDR (tmp2)));
8840 XSETCAR (tmp2, XCAR (tail));
8841 }
8842 }
8843 ASET (val, i, tmp);
8844 }
8845 }
8846 ASET (attrs, coding_attr_charset_valids, val);
8847 category = coding_category_charset;
8848 }
8849 else if (EQ (coding_type, Qccl))
8850 {
8851 Lisp_Object valids;
8852
8853 if (nargs < coding_arg_ccl_max)
8854 goto short_args;
8855
8856 val = args[coding_arg_ccl_decoder];
8857 CHECK_CCL_PROGRAM (val);
8858 if (VECTORP (val))
8859 val = Fcopy_sequence (val);
8860 ASET (attrs, coding_attr_ccl_decoder, val);
8861
8862 val = args[coding_arg_ccl_encoder];
8863 CHECK_CCL_PROGRAM (val);
8864 if (VECTORP (val))
8865 val = Fcopy_sequence (val);
8866 ASET (attrs, coding_attr_ccl_encoder, val);
8867
8868 val = args[coding_arg_ccl_valids];
8869 valids = Fmake_string (make_number (256), make_number (0));
8870 for (tail = val; !NILP (tail); tail = Fcdr (tail))
8871 {
8872 int from, to;
8873
8874 val = Fcar (tail);
8875 if (INTEGERP (val))
8876 {
8877 from = to = XINT (val);
8878 if (from < 0 || from > 255)
8879 args_out_of_range_3 (val, make_number (0), make_number (255));
8880 }
8881 else
8882 {
8883 CHECK_CONS (val);
8884 CHECK_NATNUM_CAR (val);
8885 CHECK_NATNUM_CDR (val);
8886 from = XINT (XCAR (val));
8887 if (from > 255)
8888 args_out_of_range_3 (XCAR (val),
8889 make_number (0), make_number (255));
8890 to = XINT (XCDR (val));
8891 if (to < from || to > 255)
8892 args_out_of_range_3 (XCDR (val),
8893 XCAR (val), make_number (255));
8894 }
8895 for (i = from; i <= to; i++)
8896 SSET (valids, i, 1);
8897 }
8898 ASET (attrs, coding_attr_ccl_valids, valids);
8899
8900 category = coding_category_ccl;
8901 }
8902 else if (EQ (coding_type, Qutf_16))
8903 {
8904 Lisp_Object bom, endian;
8905
8906 CODING_ATTR_ASCII_COMPAT (attrs) = Qnil;
8907
8908 if (nargs < coding_arg_utf16_max)
8909 goto short_args;
8910
8911 bom = args[coding_arg_utf16_bom];
8912 if (! NILP (bom) && ! EQ (bom, Qt))
8913 {
8914 CHECK_CONS (bom);
8915 val = XCAR (bom);
8916 CHECK_CODING_SYSTEM (val);
8917 val = XCDR (bom);
8918 CHECK_CODING_SYSTEM (val);
8919 }
8920 ASET (attrs, coding_attr_utf_16_bom, bom);
8921
8922 endian = args[coding_arg_utf16_endian];
8923 CHECK_SYMBOL (endian);
8924 if (NILP (endian))
8925 endian = Qbig;
8926 else if (! EQ (endian, Qbig) && ! EQ (endian, Qlittle))
8927 error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian)));
8928 ASET (attrs, coding_attr_utf_16_endian, endian);
8929
8930 category = (CONSP (bom)
8931 ? coding_category_utf_16_auto
8932 : NILP (bom)
8933 ? (EQ (endian, Qbig)
8934 ? coding_category_utf_16_be_nosig
8935 : coding_category_utf_16_le_nosig)
8936 : (EQ (endian, Qbig)
8937 ? coding_category_utf_16_be
8938 : coding_category_utf_16_le));
8939 }
8940 else if (EQ (coding_type, Qiso_2022))
8941 {
8942 Lisp_Object initial, reg_usage, request, flags;
8943 int i;
8944
8945 if (nargs < coding_arg_iso2022_max)
8946 goto short_args;
8947
8948 initial = Fcopy_sequence (args[coding_arg_iso2022_initial]);
8949 CHECK_VECTOR (initial);
8950 for (i = 0; i < 4; i++)
8951 {
8952 val = Faref (initial, make_number (i));
8953 if (! NILP (val))
8954 {
8955 struct charset *charset;
8956
8957 CHECK_CHARSET_GET_CHARSET (val, charset);
8958 ASET (initial, i, make_number (CHARSET_ID (charset)));
8959 if (i == 0 && CHARSET_ASCII_COMPATIBLE_P (charset))
8960 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
8961 }
8962 else
8963 ASET (initial, i, make_number (-1));
8964 }
8965
8966 reg_usage = args[coding_arg_iso2022_reg_usage];
8967 CHECK_CONS (reg_usage);
8968 CHECK_NUMBER_CAR (reg_usage);
8969 CHECK_NUMBER_CDR (reg_usage);
8970
8971 request = Fcopy_sequence (args[coding_arg_iso2022_request]);
8972 for (tail = request; ! NILP (tail); tail = Fcdr (tail))
8973 {
8974 int id;
8975 Lisp_Object tmp;
8976
8977 val = Fcar (tail);
8978 CHECK_CONS (val);
8979 tmp = XCAR (val);
8980 CHECK_CHARSET_GET_ID (tmp, id);
8981 CHECK_NATNUM_CDR (val);
8982 if (XINT (XCDR (val)) >= 4)
8983 error ("Invalid graphic register number: %d", XINT (XCDR (val)));
8984 XSETCAR (val, make_number (id));
8985 }
8986
8987 flags = args[coding_arg_iso2022_flags];
8988 CHECK_NATNUM (flags);
8989 i = XINT (flags);
8990 if (EQ (args[coding_arg_charset_list], Qiso_2022))
8991 flags = make_number (i | CODING_ISO_FLAG_FULL_SUPPORT);
8992
8993 ASET (attrs, coding_attr_iso_initial, initial);
8994 ASET (attrs, coding_attr_iso_usage, reg_usage);
8995 ASET (attrs, coding_attr_iso_request, request);
8996 ASET (attrs, coding_attr_iso_flags, flags);
8997 setup_iso_safe_charsets (attrs);
8998
8999 if (i & CODING_ISO_FLAG_SEVEN_BITS)
9000 category = ((i & (CODING_ISO_FLAG_LOCKING_SHIFT
9001 | CODING_ISO_FLAG_SINGLE_SHIFT))
9002 ? coding_category_iso_7_else
9003 : EQ (args[coding_arg_charset_list], Qiso_2022)
9004 ? coding_category_iso_7
9005 : coding_category_iso_7_tight);
9006 else
9007 {
9008 int id = XINT (AREF (initial, 1));
9009
9010 category = (((i & CODING_ISO_FLAG_LOCKING_SHIFT)
9011 || EQ (args[coding_arg_charset_list], Qiso_2022)
9012 || id < 0)
9013 ? coding_category_iso_8_else
9014 : (CHARSET_DIMENSION (CHARSET_FROM_ID (id)) == 1)
9015 ? coding_category_iso_8_1
9016 : coding_category_iso_8_2);
9017 }
9018 if (category != coding_category_iso_8_1
9019 && category != coding_category_iso_8_2)
9020 CODING_ATTR_ASCII_COMPAT (attrs) = Qnil;
9021 }
9022 else if (EQ (coding_type, Qemacs_mule))
9023 {
9024 if (EQ (args[coding_arg_charset_list], Qemacs_mule))
9025 ASET (attrs, coding_attr_emacs_mule_full, Qt);
9026 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
9027 category = coding_category_emacs_mule;
9028 }
9029 else if (EQ (coding_type, Qshift_jis))
9030 {
9031
9032 struct charset *charset;
7710 9033
7711 if (NILP (Fcheck_coding_system (coding_system))) 9034 if (XINT (Flength (charset_list)) != 3
7712 xsignal1 (Qcoding_system_error, coding_system); 9035 && XINT (Flength (charset_list)) != 4)
9036 error ("There should be three or four charsets");
7713 9037
7714 safe_chars = coding_safe_chars (coding_system); 9038 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
7715 if (! EQ (safe_chars, Qt) && ! CHAR_TABLE_P (safe_chars)) 9039 if (CHARSET_DIMENSION (charset) != 1)
7716 error ("No valid safe-chars property for %s", 9040 error ("Dimension of charset %s is not one",
7717 SDATA (SYMBOL_NAME (coding_system))); 9041 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9042 if (CHARSET_ASCII_COMPATIBLE_P (charset))
9043 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
7718 9044
7719 if (EQ (safe_chars, Qt)) 9045 charset_list = XCDR (charset_list);
9046 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9047 if (CHARSET_DIMENSION (charset) != 1)
9048 error ("Dimension of charset %s is not one",
9049 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9050
9051 charset_list = XCDR (charset_list);
9052 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9053 if (CHARSET_DIMENSION (charset) != 2)
9054 error ("Dimension of charset %s is not two",
9055 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9056
9057 charset_list = XCDR (charset_list);
9058 if (! NILP (charset_list))
9059 {
9060 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9061 if (CHARSET_DIMENSION (charset) != 2)
9062 error ("Dimension of charset %s is not two",
9063 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9064 }
9065
9066 category = coding_category_sjis;
9067 Vsjis_coding_system = name;
9068 }
9069 else if (EQ (coding_type, Qbig5))
7720 { 9070 {
7721 if (NILP (Fmemq (coding_system, XCAR (Vcoding_system_safe_chars)))) 9071 struct charset *charset;
7722 XSETCAR (Vcoding_system_safe_chars, 9072
7723 Fcons (coding_system, XCAR (Vcoding_system_safe_chars))); 9073 if (XINT (Flength (charset_list)) != 2)
9074 error ("There should be just two charsets");
9075
9076 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9077 if (CHARSET_DIMENSION (charset) != 1)
9078 error ("Dimension of charset %s is not one",
9079 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9080 if (CHARSET_ASCII_COMPATIBLE_P (charset))
9081 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
9082
9083 charset_list = XCDR (charset_list);
9084 charset = CHARSET_FROM_ID (XINT (XCAR (charset_list)));
9085 if (CHARSET_DIMENSION (charset) != 2)
9086 error ("Dimension of charset %s is not two",
9087 SDATA (SYMBOL_NAME (CHARSET_NAME (charset))));
9088
9089 category = coding_category_big5;
9090 Vbig5_coding_system = name;
7724 } 9091 }
9092 else if (EQ (coding_type, Qraw_text))
9093 {
9094 category = coding_category_raw_text;
9095 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
9096 }
9097 else if (EQ (coding_type, Qutf_8))
9098 {
9099 category = coding_category_utf_8;
9100 CODING_ATTR_ASCII_COMPAT (attrs) = Qt;
9101 }
9102 else if (EQ (coding_type, Qundecided))
9103 category = coding_category_undecided;
7725 else 9104 else
9105 error ("Invalid coding system type: %s",
9106 SDATA (SYMBOL_NAME (coding_type)));
9107
9108 CODING_ATTR_CATEGORY (attrs) = make_number (category);
9109 CODING_ATTR_PLIST (attrs)
9110 = Fcons (QCcategory, Fcons (AREF (Vcoding_category_table, category),
9111 CODING_ATTR_PLIST (attrs)));
9112 CODING_ATTR_PLIST (attrs)
9113 = Fcons (QCascii_compatible_p,
9114 Fcons (CODING_ATTR_ASCII_COMPAT (attrs),
9115 CODING_ATTR_PLIST (attrs)));
9116
9117 eol_type = args[coding_arg_eol_type];
9118 if (! NILP (eol_type)
9119 && ! EQ (eol_type, Qunix)
9120 && ! EQ (eol_type, Qdos)
9121 && ! EQ (eol_type, Qmac))
9122 error ("Invalid eol-type");
9123
9124 aliases = Fcons (name, Qnil);
9125
9126 if (NILP (eol_type))
9127 {
9128 eol_type = make_subsidiaries (name);
9129 for (i = 0; i < 3; i++)
9130 {
9131 Lisp_Object this_spec, this_name, this_aliases, this_eol_type;
9132
9133 this_name = AREF (eol_type, i);
9134 this_aliases = Fcons (this_name, Qnil);
9135 this_eol_type = (i == 0 ? Qunix : i == 1 ? Qdos : Qmac);
9136 this_spec = Fmake_vector (make_number (3), attrs);
9137 ASET (this_spec, 1, this_aliases);
9138 ASET (this_spec, 2, this_eol_type);
9139 Fputhash (this_name, this_spec, Vcoding_system_hash_table);
9140 Vcoding_system_list = Fcons (this_name, Vcoding_system_list);
9141 val = Fassoc (Fsymbol_name (this_name), Vcoding_system_alist);
9142 if (NILP (val))
9143 Vcoding_system_alist
9144 = Fcons (Fcons (Fsymbol_name (this_name), Qnil),
9145 Vcoding_system_alist);
9146 }
9147 }
9148
9149 spec_vec = Fmake_vector (make_number (3), attrs);
9150 ASET (spec_vec, 1, aliases);
9151 ASET (spec_vec, 2, eol_type);
9152
9153 Fputhash (name, spec_vec, Vcoding_system_hash_table);
9154 Vcoding_system_list = Fcons (name, Vcoding_system_list);
9155 val = Fassoc (Fsymbol_name (name), Vcoding_system_alist);
9156 if (NILP (val))
9157 Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (name), Qnil),
9158 Vcoding_system_alist);
9159
9160 {
9161 int id = coding_categories[category].id;
9162
9163 if (id < 0 || EQ (name, CODING_ID_NAME (id)))
9164 setup_coding_system (name, &coding_categories[category]);
9165 }
9166
9167 return Qnil;
9168
9169 short_args:
9170 return Fsignal (Qwrong_number_of_arguments,
9171 Fcons (intern ("define-coding-system-internal"),
9172 make_number (nargs)));
9173}
9174
9175
9176DEFUN ("coding-system-put", Fcoding_system_put, Scoding_system_put,
9177 3, 3, 0,
9178 doc: /* Change value in CODING-SYSTEM's property list PROP to VAL. */)
9179 (coding_system, prop, val)
9180 Lisp_Object coding_system, prop, val;
9181{
9182 Lisp_Object spec, attrs;
9183
9184 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
9185 attrs = AREF (spec, 0);
9186 if (EQ (prop, QCmnemonic))
7726 { 9187 {
7727 slot = Fassq (coding_system, XCDR (Vcoding_system_safe_chars)); 9188 if (! STRINGP (val))
7728 if (NILP (slot)) 9189 CHECK_CHARACTER (val);
7729 XSETCDR (Vcoding_system_safe_chars, 9190 CODING_ATTR_MNEMONIC (attrs) = val;
7730 nconc2 (XCDR (Vcoding_system_safe_chars), 9191 }
7731 Fcons (Fcons (coding_system, safe_chars), Qnil))); 9192 else if (EQ (prop, QCdefalut_char))
9193 {
9194 if (NILP (val))
9195 val = make_number (' ');
7732 else 9196 else
7733 XSETCDR (slot, safe_chars); 9197 CHECK_CHARACTER (val);
9198 CODING_ATTR_DEFAULT_CHAR (attrs) = val;
9199 }
9200 else if (EQ (prop, QCdecode_translation_table))
9201 {
9202 if (! CHAR_TABLE_P (val) && ! CONSP (val))
9203 CHECK_SYMBOL (val);
9204 CODING_ATTR_DECODE_TBL (attrs) = val;
9205 }
9206 else if (EQ (prop, QCencode_translation_table))
9207 {
9208 if (! CHAR_TABLE_P (val) && ! CONSP (val))
9209 CHECK_SYMBOL (val);
9210 CODING_ATTR_ENCODE_TBL (attrs) = val;
9211 }
9212 else if (EQ (prop, QCpost_read_conversion))
9213 {
9214 CHECK_SYMBOL (val);
9215 CODING_ATTR_POST_READ (attrs) = val;
9216 }
9217 else if (EQ (prop, QCpre_write_conversion))
9218 {
9219 CHECK_SYMBOL (val);
9220 CODING_ATTR_PRE_WRITE (attrs) = val;
9221 }
9222 else if (EQ (prop, QCascii_compatible_p))
9223 {
9224 CODING_ATTR_ASCII_COMPAT (attrs) = val;
9225 }
9226
9227 CODING_ATTR_PLIST (attrs)
9228 = Fplist_put (CODING_ATTR_PLIST (attrs), prop, val);
9229 return val;
9230}
9231
9232
9233DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias,
9234 Sdefine_coding_system_alias, 2, 2, 0,
9235 doc: /* Define ALIAS as an alias for CODING-SYSTEM. */)
9236 (alias, coding_system)
9237 Lisp_Object alias, coding_system;
9238{
9239 Lisp_Object spec, aliases, eol_type, val;
9240
9241 CHECK_SYMBOL (alias);
9242 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
9243 aliases = AREF (spec, 1);
9244 /* ALISES should be a list of length more than zero, and the first
9245 element is a base coding system. Append ALIAS at the tail of the
9246 list. */
9247 while (!NILP (XCDR (aliases)))
9248 aliases = XCDR (aliases);
9249 XSETCDR (aliases, Fcons (alias, Qnil));
9250
9251 eol_type = AREF (spec, 2);
9252 if (VECTORP (eol_type))
9253 {
9254 Lisp_Object subsidiaries;
9255 int i;
9256
9257 subsidiaries = make_subsidiaries (alias);
9258 for (i = 0; i < 3; i++)
9259 Fdefine_coding_system_alias (AREF (subsidiaries, i),
9260 AREF (eol_type, i));
7734 } 9261 }
9262
9263 Fputhash (alias, spec, Vcoding_system_hash_table);
9264 Vcoding_system_list = Fcons (alias, Vcoding_system_list);
9265 val = Fassoc (Fsymbol_name (alias), Vcoding_system_alist);
9266 if (NILP (val))
9267 Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (alias), Qnil),
9268 Vcoding_system_alist);
9269
7735 return Qnil; 9270 return Qnil;
7736} 9271}
7737 9272
9273DEFUN ("coding-system-base", Fcoding_system_base, Scoding_system_base,
9274 1, 1, 0,
9275 doc: /* Return the base of CODING-SYSTEM.
9276Any alias or subsidiary coding system is not a base coding system. */)
9277 (coding_system)
9278 Lisp_Object coding_system;
9279{
9280 Lisp_Object spec, attrs;
9281
9282 if (NILP (coding_system))
9283 return (Qno_conversion);
9284 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
9285 attrs = AREF (spec, 0);
9286 return CODING_ATTR_BASE_NAME (attrs);
9287}
9288
9289DEFUN ("coding-system-plist", Fcoding_system_plist, Scoding_system_plist,
9290 1, 1, 0,
9291 doc: "Return the property list of CODING-SYSTEM.")
9292 (coding_system)
9293 Lisp_Object coding_system;
9294{
9295 Lisp_Object spec, attrs;
9296
9297 if (NILP (coding_system))
9298 coding_system = Qno_conversion;
9299 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
9300 attrs = AREF (spec, 0);
9301 return CODING_ATTR_PLIST (attrs);
9302}
9303
9304
9305DEFUN ("coding-system-aliases", Fcoding_system_aliases, Scoding_system_aliases,
9306 1, 1, 0,
9307 doc: /* Return the list of aliases of CODING-SYSTEM. */)
9308 (coding_system)
9309 Lisp_Object coding_system;
9310{
9311 Lisp_Object spec;
9312
9313 if (NILP (coding_system))
9314 coding_system = Qno_conversion;
9315 CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec);
9316 return AREF (spec, 1);
9317}
9318
9319DEFUN ("coding-system-eol-type", Fcoding_system_eol_type,
9320 Scoding_system_eol_type, 1, 1, 0,
9321 doc: /* Return eol-type of CODING-SYSTEM.
9322An eol-type is integer 0, 1, 2, or a vector of coding systems.
9323
9324Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF,
9325and CR respectively.
9326
9327A vector value indicates that a format of end-of-line should be
9328detected automatically. Nth element of the vector is the subsidiary
9329coding system whose eol-type is N. */)
9330 (coding_system)
9331 Lisp_Object coding_system;
9332{
9333 Lisp_Object spec, eol_type;
9334 int n;
9335
9336 if (NILP (coding_system))
9337 coding_system = Qno_conversion;
9338 if (! CODING_SYSTEM_P (coding_system))
9339 return Qnil;
9340 spec = CODING_SYSTEM_SPEC (coding_system);
9341 eol_type = AREF (spec, 2);
9342 if (VECTORP (eol_type))
9343 return Fcopy_sequence (eol_type);
9344 n = EQ (eol_type, Qunix) ? 0 : EQ (eol_type, Qdos) ? 1 : 2;
9345 return make_number (n);
9346}
9347
7738#endif /* emacs */ 9348#endif /* emacs */
7739 9349
7740 9350
@@ -7745,20 +9355,11 @@ init_coding_once ()
7745{ 9355{
7746 int i; 9356 int i;
7747 9357
7748 /* Emacs' internal format specific initialize routine. */ 9358 for (i = 0; i < coding_category_max; i++)
7749 for (i = 0; i <= 0x20; i++) 9359 {
7750 emacs_code_class[i] = EMACS_control_code; 9360 coding_categories[i].id = -1;
7751 emacs_code_class[0x0A] = EMACS_linefeed_code; 9361 coding_priorities[i] = i;
7752 emacs_code_class[0x0D] = EMACS_carriage_return_code; 9362 }
7753 for (i = 0x21 ; i < 0x7F; i++)
7754 emacs_code_class[i] = EMACS_ascii_code;
7755 emacs_code_class[0x7F] = EMACS_control_code;
7756 for (i = 0x80; i < 0xFF; i++)
7757 emacs_code_class[i] = EMACS_invalid_code;
7758 emacs_code_class[LEADING_CODE_PRIVATE_11] = EMACS_leading_code_3;
7759 emacs_code_class[LEADING_CODE_PRIVATE_12] = EMACS_leading_code_3;
7760 emacs_code_class[LEADING_CODE_PRIVATE_21] = EMACS_leading_code_4;
7761 emacs_code_class[LEADING_CODE_PRIVATE_22] = EMACS_leading_code_4;
7762 9363
7763 /* ISO2022 specific initialize routine. */ 9364 /* ISO2022 specific initialize routine. */
7764 for (i = 0; i < 0x20; i++) 9365 for (i = 0; i < 0x20; i++)
@@ -7771,7 +9372,6 @@ init_coding_once ()
7771 iso_code_class[i] = ISO_graphic_plane_1; 9372 iso_code_class[i] = ISO_graphic_plane_1;
7772 iso_code_class[0x20] = iso_code_class[0x7F] = ISO_0x20_or_0x7F; 9373 iso_code_class[0x20] = iso_code_class[0x7F] = ISO_0x20_or_0x7F;
7773 iso_code_class[0xA0] = iso_code_class[0xFF] = ISO_0xA0_or_0xFF; 9374 iso_code_class[0xA0] = iso_code_class[0xFF] = ISO_0xA0_or_0xFF;
7774 iso_code_class[ISO_CODE_CR] = ISO_carriage_return;
7775 iso_code_class[ISO_CODE_SO] = ISO_shift_out; 9375 iso_code_class[ISO_CODE_SO] = ISO_shift_out;
7776 iso_code_class[ISO_CODE_SI] = ISO_shift_in; 9376 iso_code_class[ISO_CODE_SI] = ISO_shift_in;
7777 iso_code_class[ISO_CODE_SS2_7] = ISO_single_shift_2_7; 9377 iso_code_class[ISO_CODE_SS2_7] = ISO_single_shift_2_7;
@@ -7780,22 +9380,14 @@ init_coding_once ()
7780 iso_code_class[ISO_CODE_SS3] = ISO_single_shift_3; 9380 iso_code_class[ISO_CODE_SS3] = ISO_single_shift_3;
7781 iso_code_class[ISO_CODE_CSI] = ISO_control_sequence_introducer; 9381 iso_code_class[ISO_CODE_CSI] = ISO_control_sequence_introducer;
7782 9382
7783 setup_coding_system (Qnil, &safe_terminal_coding); 9383 for (i = 0; i < 256; i++)
7784 setup_coding_system (Qnil, &default_buffer_file_coding); 9384 {
7785 9385 emacs_mule_bytes[i] = 1;
7786 bzero (coding_system_table, sizeof coding_system_table); 9386 }
7787 9387 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_11] = 3;
7788 bzero (ascii_skip_code, sizeof ascii_skip_code); 9388 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_12] = 3;
7789 for (i = 0; i < 128; i++) 9389 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_21] = 4;
7790 ascii_skip_code[i] = 1; 9390 emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_22] = 4;
7791
7792#if defined (MSDOS) || defined (WINDOWSNT)
7793 system_eol_type = CODING_EOL_CRLF;
7794#else
7795 system_eol_type = CODING_EOL_LF;
7796#endif
7797
7798 inhibit_pre_post_conversion = 0;
7799} 9391}
7800 9392
7801#ifdef emacs 9393#ifdef emacs
@@ -7803,14 +9395,31 @@ init_coding_once ()
7803void 9395void
7804syms_of_coding () 9396syms_of_coding ()
7805{ 9397{
9398 staticpro (&Vcoding_system_hash_table);
9399 {
9400 Lisp_Object args[2];
9401 args[0] = QCtest;
9402 args[1] = Qeq;
9403 Vcoding_system_hash_table = Fmake_hash_table (2, args);
9404 }
9405
9406 staticpro (&Vsjis_coding_system);
9407 Vsjis_coding_system = Qnil;
9408
9409 staticpro (&Vbig5_coding_system);
9410 Vbig5_coding_system = Qnil;
9411
9412 staticpro (&Vcode_conversion_reused_workbuf);
9413 Vcode_conversion_reused_workbuf = Qnil;
9414
7806 staticpro (&Vcode_conversion_workbuf_name); 9415 staticpro (&Vcode_conversion_workbuf_name);
7807 Vcode_conversion_workbuf_name = build_string (" *code-conversion-work*"); 9416 Vcode_conversion_workbuf_name = build_string (" *code-conversion-work*");
7808 9417
7809 Qtarget_idx = intern ("target-idx"); 9418 reused_workbuf_in_use = 0;
7810 staticpro (&Qtarget_idx);
7811 9419
7812 Qcoding_system_history = intern ("coding-system-history"); 9420 DEFSYM (Qcharset, "charset");
7813 staticpro (&Qcoding_system_history); 9421 DEFSYM (Qtarget_idx, "target-idx");
9422 DEFSYM (Qcoding_system_history, "coding-system-history");
7814 Fset (Qcoding_system_history, Qnil); 9423 Fset (Qcoding_system_history, Qnil);
7815 9424
7816 /* Target FILENAME is the first argument. */ 9425 /* Target FILENAME is the first argument. */
@@ -7818,123 +9427,131 @@ syms_of_coding ()
7818 /* Target FILENAME is the third argument. */ 9427 /* Target FILENAME is the third argument. */
7819 Fput (Qwrite_region, Qtarget_idx, make_number (2)); 9428 Fput (Qwrite_region, Qtarget_idx, make_number (2));
7820 9429
7821 Qcall_process = intern ("call-process"); 9430 DEFSYM (Qcall_process, "call-process");
7822 staticpro (&Qcall_process);
7823 /* Target PROGRAM is the first argument. */ 9431 /* Target PROGRAM is the first argument. */
7824 Fput (Qcall_process, Qtarget_idx, make_number (0)); 9432 Fput (Qcall_process, Qtarget_idx, make_number (0));
7825 9433
7826 Qcall_process_region = intern ("call-process-region"); 9434 DEFSYM (Qcall_process_region, "call-process-region");
7827 staticpro (&Qcall_process_region);
7828 /* Target PROGRAM is the third argument. */ 9435 /* Target PROGRAM is the third argument. */
7829 Fput (Qcall_process_region, Qtarget_idx, make_number (2)); 9436 Fput (Qcall_process_region, Qtarget_idx, make_number (2));
7830 9437
7831 Qstart_process = intern ("start-process"); 9438 DEFSYM (Qstart_process, "start-process");
7832 staticpro (&Qstart_process);
7833 /* Target PROGRAM is the third argument. */ 9439 /* Target PROGRAM is the third argument. */
7834 Fput (Qstart_process, Qtarget_idx, make_number (2)); 9440 Fput (Qstart_process, Qtarget_idx, make_number (2));
7835 9441
7836 Qopen_network_stream = intern ("open-network-stream"); 9442 DEFSYM (Qopen_network_stream, "open-network-stream");
7837 staticpro (&Qopen_network_stream);
7838 /* Target SERVICE is the fourth argument. */ 9443 /* Target SERVICE is the fourth argument. */
7839 Fput (Qopen_network_stream, Qtarget_idx, make_number (3)); 9444 Fput (Qopen_network_stream, Qtarget_idx, make_number (3));
7840 9445
7841 Qcoding_system = intern ("coding-system"); 9446 DEFSYM (Qcoding_system, "coding-system");
7842 staticpro (&Qcoding_system); 9447 DEFSYM (Qcoding_aliases, "coding-aliases");
7843
7844 Qeol_type = intern ("eol-type");
7845 staticpro (&Qeol_type);
7846 9448
7847 Qbuffer_file_coding_system = intern ("buffer-file-coding-system"); 9449 DEFSYM (Qeol_type, "eol-type");
7848 staticpro (&Qbuffer_file_coding_system); 9450 DEFSYM (Qunix, "unix");
9451 DEFSYM (Qdos, "dos");
7849 9452
7850 Qpost_read_conversion = intern ("post-read-conversion"); 9453 DEFSYM (Qbuffer_file_coding_system, "buffer-file-coding-system");
7851 staticpro (&Qpost_read_conversion); 9454 DEFSYM (Qpost_read_conversion, "post-read-conversion");
9455 DEFSYM (Qpre_write_conversion, "pre-write-conversion");
9456 DEFSYM (Qdefault_char, "default-char");
9457 DEFSYM (Qundecided, "undecided");
9458 DEFSYM (Qno_conversion, "no-conversion");
9459 DEFSYM (Qraw_text, "raw-text");
7852 9460
7853 Qpre_write_conversion = intern ("pre-write-conversion"); 9461 DEFSYM (Qiso_2022, "iso-2022");
7854 staticpro (&Qpre_write_conversion);
7855 9462
7856 Qno_conversion = intern ("no-conversion"); 9463 DEFSYM (Qutf_8, "utf-8");
7857 staticpro (&Qno_conversion); 9464 DEFSYM (Qutf_8_emacs, "utf-8-emacs");
7858 9465
7859 Qundecided = intern ("undecided"); 9466 DEFSYM (Qutf_16, "utf-16");
7860 staticpro (&Qundecided); 9467 DEFSYM (Qbig, "big");
9468 DEFSYM (Qlittle, "little");
7861 9469
7862 Qcoding_system_p = intern ("coding-system-p"); 9470 DEFSYM (Qshift_jis, "shift-jis");
7863 staticpro (&Qcoding_system_p); 9471 DEFSYM (Qbig5, "big5");
7864 9472
7865 Qcoding_system_error = intern ("coding-system-error"); 9473 DEFSYM (Qcoding_system_p, "coding-system-p");
7866 staticpro (&Qcoding_system_error);
7867 9474
9475 DEFSYM (Qcoding_system_error, "coding-system-error");
7868 Fput (Qcoding_system_error, Qerror_conditions, 9476 Fput (Qcoding_system_error, Qerror_conditions,
7869 Fcons (Qcoding_system_error, Fcons (Qerror, Qnil))); 9477 Fcons (Qcoding_system_error, Fcons (Qerror, Qnil)));
7870 Fput (Qcoding_system_error, Qerror_message, 9478 Fput (Qcoding_system_error, Qerror_message,
7871 build_string ("Invalid coding system")); 9479 build_string ("Invalid coding system"));
7872 9480
7873 Qcoding_category = intern ("coding-category");
7874 staticpro (&Qcoding_category);
7875 Qcoding_category_index = intern ("coding-category-index");
7876 staticpro (&Qcoding_category_index);
7877
7878 Vcoding_category_table
7879 = Fmake_vector (make_number (CODING_CATEGORY_IDX_MAX), Qnil);
7880 staticpro (&Vcoding_category_table);
7881 {
7882 int i;
7883 for (i = 0; i < CODING_CATEGORY_IDX_MAX; i++)
7884 {
7885 XVECTOR (Vcoding_category_table)->contents[i]
7886 = intern (coding_category_name[i]);
7887 Fput (XVECTOR (Vcoding_category_table)->contents[i],
7888 Qcoding_category_index, make_number (i));
7889 }
7890 }
7891
7892 Vcoding_system_safe_chars = Fcons (Qnil, Qnil);
7893 staticpro (&Vcoding_system_safe_chars);
7894
7895 Qtranslation_table = intern ("translation-table");
7896 staticpro (&Qtranslation_table);
7897 Fput (Qtranslation_table, Qchar_table_extra_slots, make_number (2));
7898
7899 Qtranslation_table_id = intern ("translation-table-id");
7900 staticpro (&Qtranslation_table_id);
7901
7902 Qtranslation_table_for_decode = intern ("translation-table-for-decode");
7903 staticpro (&Qtranslation_table_for_decode);
7904
7905 Qtranslation_table_for_encode = intern ("translation-table-for-encode");
7906 staticpro (&Qtranslation_table_for_encode);
7907
7908 Qsafe_chars = intern ("safe-chars");
7909 staticpro (&Qsafe_chars);
7910
7911 Qchar_coding_system = intern ("char-coding-system");
7912 staticpro (&Qchar_coding_system);
7913
7914 /* Intern this now in case it isn't already done. 9481 /* Intern this now in case it isn't already done.
7915 Setting this variable twice is harmless. 9482 Setting this variable twice is harmless.
7916 But don't staticpro it here--that is done in alloc.c. */ 9483 But don't staticpro it here--that is done in alloc.c. */
7917 Qchar_table_extra_slots = intern ("char-table-extra-slots"); 9484 Qchar_table_extra_slots = intern ("char-table-extra-slots");
7918 Fput (Qsafe_chars, Qchar_table_extra_slots, make_number (0));
7919 Fput (Qchar_coding_system, Qchar_table_extra_slots, make_number (0));
7920
7921 Qvalid_codes = intern ("valid-codes");
7922 staticpro (&Qvalid_codes);
7923 9485
7924 Qascii_incompatible = intern ("ascii-incompatible"); 9486 DEFSYM (Qtranslation_table, "translation-table");
7925 staticpro (&Qascii_incompatible); 9487 Fput (Qtranslation_table, Qchar_table_extra_slots, make_number (2));
9488 DEFSYM (Qtranslation_table_id, "translation-table-id");
9489 DEFSYM (Qtranslation_table_for_decode, "translation-table-for-decode");
9490 DEFSYM (Qtranslation_table_for_encode, "translation-table-for-encode");
7926 9491
7927 Qemacs_mule = intern ("emacs-mule"); 9492 DEFSYM (Qvalid_codes, "valid-codes");
7928 staticpro (&Qemacs_mule);
7929 9493
7930 Qraw_text = intern ("raw-text"); 9494 DEFSYM (Qemacs_mule, "emacs-mule");
7931 staticpro (&Qraw_text);
7932 9495
7933 Qutf_8 = intern ("utf-8"); 9496 DEFSYM (QCcategory, ":category");
7934 staticpro (&Qutf_8); 9497 DEFSYM (QCmnemonic, ":mnemonic");
9498 DEFSYM (QCdefalut_char, ":default-char");
9499 DEFSYM (QCdecode_translation_table, ":decode-translation-table");
9500 DEFSYM (QCencode_translation_table, ":encode-translation-table");
9501 DEFSYM (QCpost_read_conversion, ":post-read-conversion");
9502 DEFSYM (QCpre_write_conversion, ":pre-write-conversion");
9503 DEFSYM (QCascii_compatible_p, ":ascii-compatible-p");
7935 9504
7936 Qcoding_system_define_form = intern ("coding-system-define-form"); 9505 Vcoding_category_table
7937 staticpro (&Qcoding_system_define_form); 9506 = Fmake_vector (make_number (coding_category_max), Qnil);
9507 staticpro (&Vcoding_category_table);
9508 /* Followings are target of code detection. */
9509 ASET (Vcoding_category_table, coding_category_iso_7,
9510 intern ("coding-category-iso-7"));
9511 ASET (Vcoding_category_table, coding_category_iso_7_tight,
9512 intern ("coding-category-iso-7-tight"));
9513 ASET (Vcoding_category_table, coding_category_iso_8_1,
9514 intern ("coding-category-iso-8-1"));
9515 ASET (Vcoding_category_table, coding_category_iso_8_2,
9516 intern ("coding-category-iso-8-2"));
9517 ASET (Vcoding_category_table, coding_category_iso_7_else,
9518 intern ("coding-category-iso-7-else"));
9519 ASET (Vcoding_category_table, coding_category_iso_8_else,
9520 intern ("coding-category-iso-8-else"));
9521 ASET (Vcoding_category_table, coding_category_utf_8,
9522 intern ("coding-category-utf-8"));
9523 ASET (Vcoding_category_table, coding_category_utf_16_be,
9524 intern ("coding-category-utf-16-be"));
9525 ASET (Vcoding_category_table, coding_category_utf_16_auto,
9526 intern ("coding-category-utf-16-auto"));
9527 ASET (Vcoding_category_table, coding_category_utf_16_le,
9528 intern ("coding-category-utf-16-le"));
9529 ASET (Vcoding_category_table, coding_category_utf_16_be_nosig,
9530 intern ("coding-category-utf-16-be-nosig"));
9531 ASET (Vcoding_category_table, coding_category_utf_16_le_nosig,
9532 intern ("coding-category-utf-16-le-nosig"));
9533 ASET (Vcoding_category_table, coding_category_charset,
9534 intern ("coding-category-charset"));
9535 ASET (Vcoding_category_table, coding_category_sjis,
9536 intern ("coding-category-sjis"));
9537 ASET (Vcoding_category_table, coding_category_big5,
9538 intern ("coding-category-big5"));
9539 ASET (Vcoding_category_table, coding_category_ccl,
9540 intern ("coding-category-ccl"));
9541 ASET (Vcoding_category_table, coding_category_emacs_mule,
9542 intern ("coding-category-emacs-mule"));
9543 /* Followings are NOT target of code detection. */
9544 ASET (Vcoding_category_table, coding_category_raw_text,
9545 intern ("coding-category-raw-text"));
9546 ASET (Vcoding_category_table, coding_category_undecided,
9547 intern ("coding-category-undecided"));
9548
9549 DEFSYM (Qinsufficient_source, "insufficient-source");
9550 DEFSYM (Qinconsistent_eol, "inconsistent-eol");
9551 DEFSYM (Qinvalid_source, "invalid-source");
9552 DEFSYM (Qinterrupted, "interrupted");
9553 DEFSYM (Qinsufficient_memory, "insufficient-memory");
9554 DEFSYM (Qcoding_system_define_form, "coding-system-define-form");
7938 9555
7939 defsubr (&Scoding_system_p); 9556 defsubr (&Scoding_system_p);
7940 defsubr (&Sread_coding_system); 9557 defsubr (&Sread_coding_system);
@@ -7944,6 +9561,7 @@ syms_of_coding ()
7944 defsubr (&Sdetect_coding_string); 9561 defsubr (&Sdetect_coding_string);
7945 defsubr (&Sfind_coding_systems_region_internal); 9562 defsubr (&Sfind_coding_systems_region_internal);
7946 defsubr (&Sunencodable_char_position); 9563 defsubr (&Sunencodable_char_position);
9564 defsubr (&Scheck_coding_systems_region);
7947 defsubr (&Sdecode_coding_region); 9565 defsubr (&Sdecode_coding_region);
7948 defsubr (&Sencode_coding_region); 9566 defsubr (&Sencode_coding_region);
7949 defsubr (&Sdecode_coding_string); 9567 defsubr (&Sdecode_coding_string);
@@ -7958,15 +9576,21 @@ syms_of_coding ()
7958 defsubr (&Sset_keyboard_coding_system_internal); 9576 defsubr (&Sset_keyboard_coding_system_internal);
7959 defsubr (&Skeyboard_coding_system); 9577 defsubr (&Skeyboard_coding_system);
7960 defsubr (&Sfind_operation_coding_system); 9578 defsubr (&Sfind_operation_coding_system);
7961 defsubr (&Supdate_coding_systems_internal); 9579 defsubr (&Sset_coding_system_priority);
7962 defsubr (&Sset_coding_priority_internal);
7963 defsubr (&Sdefine_coding_system_internal); 9580 defsubr (&Sdefine_coding_system_internal);
9581 defsubr (&Sdefine_coding_system_alias);
9582 defsubr (&Scoding_system_put);
9583 defsubr (&Scoding_system_base);
9584 defsubr (&Scoding_system_plist);
9585 defsubr (&Scoding_system_aliases);
9586 defsubr (&Scoding_system_eol_type);
9587 defsubr (&Scoding_system_priority_list);
7964 9588
7965 DEFVAR_LISP ("coding-system-list", &Vcoding_system_list, 9589 DEFVAR_LISP ("coding-system-list", &Vcoding_system_list,
7966 doc: /* List of coding systems. 9590 doc: /* List of coding systems.
7967 9591
7968Do not alter the value of this variable manually. This variable should be 9592Do not alter the value of this variable manually. This variable should be
7969updated by the functions `make-coding-system' and 9593updated by the functions `define-coding-system' and
7970`define-coding-system-alias'. */); 9594`define-coding-system-alias'. */);
7971 Vcoding_system_list = Qnil; 9595 Vcoding_system_list = Qnil;
7972 9596
@@ -7993,7 +9617,7 @@ Don't modify this variable directly, but use `set-coding-priority'. */);
7993 int i; 9617 int i;
7994 9618
7995 Vcoding_category_list = Qnil; 9619 Vcoding_category_list = Qnil;
7996 for (i = CODING_CATEGORY_IDX_MAX - 1; i >= 0; i--) 9620 for (i = coding_category_max - 1; i >= 0; i--)
7997 Vcoding_category_list 9621 Vcoding_category_list
7998 = Fcons (XVECTOR (Vcoding_category_table)->contents[i], 9622 = Fcons (XVECTOR (Vcoding_category_table)->contents[i],
7999 Vcoding_category_list); 9623 Vcoding_category_list);
@@ -8023,25 +9647,44 @@ the value of `buffer-file-coding-system' is used. */);
8023 Vcoding_system_for_write = Qnil; 9647 Vcoding_system_for_write = Qnil;
8024 9648
8025 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used, 9649 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used,
8026 doc: /* Coding system used in the latest file or process I/O. 9650 doc: /*
8027Also set by `encode-coding-region', `decode-coding-region', 9651Coding system used in the latest file or process I/O. */);
8028`encode-coding-string' and `decode-coding-string'. */);
8029 Vlast_coding_system_used = Qnil; 9652 Vlast_coding_system_used = Qnil;
8030 9653
9654 DEFVAR_LISP ("last-code-conversion-error", &Vlast_code_conversion_error,
9655 doc: /*
9656Error status of the last code conversion.
9657
9658When an error was detected in the last code conversion, this variable
9659is set to one of the following symbols.
9660 `insufficient-source'
9661 `inconsistent-eol'
9662 `invalid-source'
9663 `interrupted'
9664 `insufficient-memory'
9665When no error was detected, the value doesn't change. So, to check
9666the error status of a code conversion by this variable, you must
9667explicitly set this variable to nil before performing code
9668conversion. */);
9669 Vlast_code_conversion_error = Qnil;
9670
8031 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion, 9671 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion,
8032 doc: /* *Non-nil means always inhibit code conversion of end-of-line format. 9672 doc: /*
9673*Non-nil means always inhibit code conversion of end-of-line format.
8033See info node `Coding Systems' and info node `Text and Binary' concerning 9674See info node `Coding Systems' and info node `Text and Binary' concerning
8034such conversion. */); 9675such conversion. */);
8035 inhibit_eol_conversion = 0; 9676 inhibit_eol_conversion = 0;
8036 9677
8037 DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system, 9678 DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system,
8038 doc: /* Non-nil means process buffer inherits coding system of process output. 9679 doc: /*
9680Non-nil means process buffer inherits coding system of process output.
8039Bind it to t if the process output is to be treated as if it were a file 9681Bind it to t if the process output is to be treated as if it were a file
8040read from some filesystem. */); 9682read from some filesystem. */);
8041 inherit_process_coding_system = 0; 9683 inherit_process_coding_system = 0;
8042 9684
8043 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist, 9685 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist,
8044 doc: /* Alist to decide a coding system to use for a file I/O operation. 9686 doc: /*
9687Alist to decide a coding system to use for a file I/O operation.
8045The format is ((PATTERN . VAL) ...), 9688The format is ((PATTERN . VAL) ...),
8046where PATTERN is a regular expression matching a file name, 9689where PATTERN is a regular expression matching a file name,
8047VAL is a coding system, a cons of coding systems, or a function symbol. 9690VAL is a coding system, a cons of coding systems, or a function symbol.
@@ -8061,7 +9704,8 @@ and the variable `auto-coding-alist'. */);
8061 Vfile_coding_system_alist = Qnil; 9704 Vfile_coding_system_alist = Qnil;
8062 9705
8063 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist, 9706 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist,
8064 doc: /* Alist to decide a coding system to use for a process I/O operation. 9707 doc: /*
9708Alist to decide a coding system to use for a process I/O operation.
8065The format is ((PATTERN . VAL) ...), 9709The format is ((PATTERN . VAL) ...),
8066where PATTERN is a regular expression matching a program name, 9710where PATTERN is a regular expression matching a program name,
8067VAL is a coding system, a cons of coding systems, or a function symbol. 9711VAL is a coding system, a cons of coding systems, or a function symbol.
@@ -8076,7 +9720,8 @@ See also the function `find-operation-coding-system'. */);
8076 Vprocess_coding_system_alist = Qnil; 9720 Vprocess_coding_system_alist = Qnil;
8077 9721
8078 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist, 9722 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist,
8079 doc: /* Alist to decide a coding system to use for a network I/O operation. 9723 doc: /*
9724Alist to decide a coding system to use for a network I/O operation.
8080The format is ((PATTERN . VAL) ...), 9725The format is ((PATTERN . VAL) ...),
8081where PATTERN is a regular expression matching a network service name 9726where PATTERN is a regular expression matching a network service name
8082or is a port number to connect to, 9727or is a port number to connect to,
@@ -8098,23 +9743,28 @@ Also used for decoding keyboard input on X Window system. */);
8098 9743
8099 /* The eol mnemonics are reset in startup.el system-dependently. */ 9744 /* The eol mnemonics are reset in startup.el system-dependently. */
8100 DEFVAR_LISP ("eol-mnemonic-unix", &eol_mnemonic_unix, 9745 DEFVAR_LISP ("eol-mnemonic-unix", &eol_mnemonic_unix,
8101 doc: /* *String displayed in mode line for UNIX-like (LF) end-of-line format. */); 9746 doc: /*
9747*String displayed in mode line for UNIX-like (LF) end-of-line format. */);
8102 eol_mnemonic_unix = build_string (":"); 9748 eol_mnemonic_unix = build_string (":");
8103 9749
8104 DEFVAR_LISP ("eol-mnemonic-dos", &eol_mnemonic_dos, 9750 DEFVAR_LISP ("eol-mnemonic-dos", &eol_mnemonic_dos,
8105 doc: /* *String displayed in mode line for DOS-like (CRLF) end-of-line format. */); 9751 doc: /*
9752*String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
8106 eol_mnemonic_dos = build_string ("\\"); 9753 eol_mnemonic_dos = build_string ("\\");
8107 9754
8108 DEFVAR_LISP ("eol-mnemonic-mac", &eol_mnemonic_mac, 9755 DEFVAR_LISP ("eol-mnemonic-mac", &eol_mnemonic_mac,
8109 doc: /* *String displayed in mode line for MAC-like (CR) end-of-line format. */); 9756 doc: /*
9757*String displayed in mode line for MAC-like (CR) end-of-line format. */);
8110 eol_mnemonic_mac = build_string ("/"); 9758 eol_mnemonic_mac = build_string ("/");
8111 9759
8112 DEFVAR_LISP ("eol-mnemonic-undecided", &eol_mnemonic_undecided, 9760 DEFVAR_LISP ("eol-mnemonic-undecided", &eol_mnemonic_undecided,
8113 doc: /* *String displayed in mode line when end-of-line format is not yet determined. */); 9761 doc: /*
9762*String displayed in mode line when end-of-line format is not yet determined. */);
8114 eol_mnemonic_undecided = build_string (":"); 9763 eol_mnemonic_undecided = build_string (":");
8115 9764
8116 DEFVAR_LISP ("enable-character-translation", &Venable_character_translation, 9765 DEFVAR_LISP ("enable-character-translation", &Venable_character_translation,
8117 doc: /* *Non-nil enables character translation while encoding and decoding. */); 9766 doc: /*
9767*Non-nil enables character translation while encoding and decoding. */);
8118 Venable_character_translation = Qt; 9768 Venable_character_translation = Qt;
8119 9769
8120 DEFVAR_LISP ("standard-translation-table-for-decode", 9770 DEFVAR_LISP ("standard-translation-table-for-decode",
@@ -8127,11 +9777,12 @@ Also used for decoding keyboard input on X Window system. */);
8127 doc: /* Table for translating characters while encoding. */); 9777 doc: /* Table for translating characters while encoding. */);
8128 Vstandard_translation_table_for_encode = Qnil; 9778 Vstandard_translation_table_for_encode = Qnil;
8129 9779
8130 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_alist, 9780 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_table,
8131 doc: /* Alist of charsets vs revision numbers. 9781 doc: /* Alist of charsets vs revision numbers.
8132While encoding, if a charset (car part of an element) is found, 9782While encoding, if a charset (car part of an element) is found,
8133designate it with the escape sequence identifying revision (cdr part of the element). */); 9783designate it with the escape sequence identifying revision (cdr part
8134 Vcharset_revision_alist = Qnil; 9784of the element). */);
9785 Vcharset_revision_table = Qnil;
8135 9786
8136 DEFVAR_LISP ("default-process-coding-system", 9787 DEFVAR_LISP ("default-process-coding-system",
8137 &Vdefault_process_coding_system, 9788 &Vdefault_process_coding_system,
@@ -8141,7 +9792,8 @@ the cdr part is used for encoding a text to be sent to a process. */);
8141 Vdefault_process_coding_system = Qnil; 9792 Vdefault_process_coding_system = Qnil;
8142 9793
8143 DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table, 9794 DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table,
8144 doc: /* Table of extra Latin codes in the range 128..159 (inclusive). 9795 doc: /*
9796Table of extra Latin codes in the range 128..159 (inclusive).
8145This is a vector of length 256. 9797This is a vector of length 256.
8146If Nth element is non-nil, the existence of code N in a file 9798If Nth element is non-nil, the existence of code N in a file
8147\(or output of subprocess) doesn't prevent it to be detected as 9799\(or output of subprocess) doesn't prevent it to be detected as
@@ -8153,7 +9805,8 @@ Only 128th through 159th elements has a meaning. */);
8153 9805
8154 DEFVAR_LISP ("select-safe-coding-system-function", 9806 DEFVAR_LISP ("select-safe-coding-system-function",
8155 &Vselect_safe_coding_system_function, 9807 &Vselect_safe_coding_system_function,
8156 doc: /* Function to call to select safe coding system for encoding a text. 9808 doc: /*
9809Function to call to select safe coding system for encoding a text.
8157 9810
8158If set, this function is called to force a user to select a proper 9811If set, this function is called to force a user to select a proper
8159coding system which can encode the text in the case that a default 9812coding system which can encode the text in the case that a default
@@ -8175,7 +9828,8 @@ called even if `coding-system-for-write' is non-nil. The command
8175 9828
8176 DEFVAR_BOOL ("inhibit-iso-escape-detection", 9829 DEFVAR_BOOL ("inhibit-iso-escape-detection",
8177 &inhibit_iso_escape_detection, 9830 &inhibit_iso_escape_detection,
8178 doc: /* If non-nil, Emacs ignores ISO2022's escape sequence on code detection. 9831 doc: /*
9832If non-nil, Emacs ignores ISO2022's escape sequence on code detection.
8179 9833
8180By default, on reading a file, Emacs tries to detect how the text is 9834By default, on reading a file, Emacs tries to detect how the text is
8181encoded. This code detection is sensitive to escape sequences. If 9835encoded. This code detection is sensitive to escape sequences. If
@@ -8205,6 +9859,66 @@ escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argumen
8205This is applied to the result of input methods, not their input. See also 9859This is applied to the result of input methods, not their input. See also
8206`keyboard-translate-table'. */); 9860`keyboard-translate-table'. */);
8207 Vtranslation_table_for_input = Qnil; 9861 Vtranslation_table_for_input = Qnil;
9862
9863 {
9864 Lisp_Object args[coding_arg_max];
9865 Lisp_Object plist[16];
9866 int i;
9867
9868 for (i = 0; i < coding_arg_max; i++)
9869 args[i] = Qnil;
9870
9871 plist[0] = intern (":name");
9872 plist[1] = args[coding_arg_name] = Qno_conversion;
9873 plist[2] = intern (":mnemonic");
9874 plist[3] = args[coding_arg_mnemonic] = make_number ('=');
9875 plist[4] = intern (":coding-type");
9876 plist[5] = args[coding_arg_coding_type] = Qraw_text;
9877 plist[6] = intern (":ascii-compatible-p");
9878 plist[7] = args[coding_arg_ascii_compatible_p] = Qt;
9879 plist[8] = intern (":default-char");
9880 plist[9] = args[coding_arg_default_char] = make_number (0);
9881 plist[10] = intern (":for-unibyte");
9882 plist[11] = args[coding_arg_for_unibyte] = Qt;
9883 plist[12] = intern (":docstring");
9884 plist[13] = build_string ("Do no conversion.\n\
9885\n\
9886When you visit a file with this coding, the file is read into a\n\
9887unibyte buffer as is, thus each byte of a file is treated as a\n\
9888character.");
9889 plist[14] = intern (":eol-type");
9890 plist[15] = args[coding_arg_eol_type] = Qunix;
9891 args[coding_arg_plist] = Flist (16, plist);
9892 Fdefine_coding_system_internal (coding_arg_max, args);
9893
9894 plist[1] = args[coding_arg_name] = Qundecided;
9895 plist[3] = args[coding_arg_mnemonic] = make_number ('-');
9896 plist[5] = args[coding_arg_coding_type] = Qundecided;
9897 /* This is already set.
9898 plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */
9899 plist[8] = intern (":charset-list");
9900 plist[9] = args[coding_arg_charset_list] = Fcons (Qascii, Qnil);
9901 plist[11] = args[coding_arg_for_unibyte] = Qnil;
9902 plist[13] = build_string ("No conversion on encoding, automatic conversion on decoding.");
9903 plist[15] = args[coding_arg_eol_type] = Qnil;
9904 args[coding_arg_plist] = Flist (16, plist);
9905 Fdefine_coding_system_internal (coding_arg_max, args);
9906 }
9907
9908 setup_coding_system (Qno_conversion, &safe_terminal_coding);
9909
9910 {
9911 int i;
9912
9913 for (i = 0; i < coding_category_max; i++)
9914 Fset (AREF (Vcoding_category_table, i), Qno_conversion);
9915 }
9916#if defined (MSDOS) || defined (WINDOWSNT)
9917 system_eol_type = Qdos;
9918#else
9919 system_eol_type = Qunix;
9920#endif
9921 staticpro (&system_eol_type);
8208} 9922}
8209 9923
8210char * 9924char *