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