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