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