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