Commit | Line | Data |
---|---|---|
4ed46869 | 1 | /* Coding system handler (conversion, detection, and etc). |
4a2f9c6a | 2 | Copyright (C) 1995, 1997, 1998 Electrotechnical Laboratory, JAPAN. |
203cb916 | 3 | Licensed to the Free Software Foundation. |
4ed46869 | 4 | |
369314dc KH |
5 | This file is part of GNU Emacs. |
6 | ||
7 | GNU Emacs is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
4ed46869 | 11 | |
369314dc KH |
12 | GNU Emacs is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
4ed46869 | 16 | |
369314dc KH |
17 | You should have received a copy of the GNU General Public License |
18 | along with GNU Emacs; see the file COPYING. If not, write to | |
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
4ed46869 KH |
21 | |
22 | /*** TABLE OF CONTENTS *** | |
23 | ||
24 | 1. Preamble | |
0ef69138 | 25 | 2. Emacs' internal format (emacs-mule) handlers |
4ed46869 KH |
26 | 3. ISO2022 handlers |
27 | 4. Shift-JIS and BIG5 handlers | |
1397dc18 KH |
28 | 5. CCL handlers |
29 | 6. End-of-line handlers | |
30 | 7. C library functions | |
31 | 8. Emacs Lisp library functions | |
32 | 9. Post-amble | |
4ed46869 KH |
33 | |
34 | */ | |
35 | ||
36 | /*** GENERAL NOTE on CODING SYSTEM *** | |
37 | ||
38 | Coding system is an encoding mechanism of one or more character | |
39 | sets. Here's a list of coding systems which Emacs can handle. When | |
40 | we say "decode", it means converting some other coding system to | |
0ef69138 KH |
41 | Emacs' internal format (emacs-internal), and when we say "encode", |
42 | it means converting the coding system emacs-mule to some other | |
43 | coding system. | |
4ed46869 | 44 | |
0ef69138 | 45 | 0. Emacs' internal format (emacs-mule) |
4ed46869 KH |
46 | |
47 | Emacs itself holds a multi-lingual character in a buffer and a string | |
f4dee582 | 48 | in a special format. Details are described in section 2. |
4ed46869 KH |
49 | |
50 | 1. ISO2022 | |
51 | ||
52 | The most famous coding system for multiple character sets. X's | |
f4dee582 RS |
53 | Compound Text, various EUCs (Extended Unix Code), and coding |
54 | systems used in Internet communication such as ISO-2022-JP are | |
55 | all variants of ISO2022. Details are described in section 3. | |
4ed46869 KH |
56 | |
57 | 2. SJIS (or Shift-JIS or MS-Kanji-Code) | |
58 | ||
59 | A coding system to encode character sets: ASCII, JISX0201, and | |
60 | JISX0208. Widely used for PC's in Japan. Details are described in | |
f4dee582 | 61 | section 4. |
4ed46869 KH |
62 | |
63 | 3. BIG5 | |
64 | ||
65 | A coding system to encode character sets: ASCII and Big5. Widely | |
66 | used by Chinese (mainly in Taiwan and Hong Kong). Details are | |
f4dee582 RS |
67 | described in section 4. In this file, when we write "BIG5" |
68 | (all uppercase), we mean the coding system, and when we write | |
69 | "Big5" (capitalized), we mean the character set. | |
4ed46869 | 70 | |
27901516 KH |
71 | 4. Raw text |
72 | ||
4608c386 KH |
73 | A coding system for a text containing random 8-bit code. Emacs does |
74 | no code conversion on such a text except for end-of-line format. | |
27901516 KH |
75 | |
76 | 5. Other | |
4ed46869 | 77 | |
f4dee582 | 78 | If a user wants to read/write a text encoded in a coding system not |
4ed46869 KH |
79 | listed above, he can supply a decoder and an encoder for it in CCL |
80 | (Code Conversion Language) programs. Emacs executes the CCL program | |
81 | while reading/writing. | |
82 | ||
d46c5b12 KH |
83 | Emacs represents a coding system by a Lisp symbol that has a property |
84 | `coding-system'. But, before actually using the coding system, the | |
4ed46869 | 85 | information about it is set in a structure of type `struct |
f4dee582 | 86 | coding_system' for rapid processing. See section 6 for more details. |
4ed46869 KH |
87 | |
88 | */ | |
89 | ||
90 | /*** GENERAL NOTES on END-OF-LINE FORMAT *** | |
91 | ||
92 | How end-of-line of a text is encoded depends on a system. For | |
93 | instance, Unix's format is just one byte of `line-feed' code, | |
f4dee582 | 94 | whereas DOS's format is two-byte sequence of `carriage-return' and |
d46c5b12 KH |
95 | `line-feed' codes. MacOS's format is usually one byte of |
96 | `carriage-return'. | |
4ed46869 | 97 | |
f4dee582 RS |
98 | Since text characters encoding and end-of-line encoding are |
99 | independent, any coding system described above can take | |
4ed46869 | 100 | any format of end-of-line. So, Emacs has information of format of |
f4dee582 | 101 | end-of-line in each coding-system. See section 6 for more details. |
4ed46869 KH |
102 | |
103 | */ | |
104 | ||
105 | /*** GENERAL NOTES on `detect_coding_XXX ()' functions *** | |
106 | ||
107 | These functions check if a text between SRC and SRC_END is encoded | |
108 | in the coding system category XXX. Each returns an integer value in | |
109 | which appropriate flag bits for the category XXX is set. The flag | |
110 | bits are defined in macros CODING_CATEGORY_MASK_XXX. Below is the | |
111 | template of these functions. */ | |
112 | #if 0 | |
113 | int | |
0ef69138 | 114 | detect_coding_emacs_mule (src, src_end) |
4ed46869 KH |
115 | unsigned char *src, *src_end; |
116 | { | |
117 | ... | |
118 | } | |
119 | #endif | |
120 | ||
121 | /*** GENERAL NOTES on `decode_coding_XXX ()' functions *** | |
122 | ||
123 | These functions decode SRC_BYTES length text at SOURCE encoded in | |
0ef69138 | 124 | CODING to Emacs' internal format (emacs-mule). The resulting text |
d46c5b12 KH |
125 | goes to a place pointed to by DESTINATION, the length of which |
126 | should not exceed DST_BYTES. These functions set the information of | |
127 | original and decoded texts in the members produced, produced_char, | |
128 | consumed, and consumed_char of the structure *CODING. | |
129 | ||
130 | The return value is an integer (CODING_FINISH_XXX) indicating how | |
131 | the decoding finished. | |
132 | ||
133 | DST_BYTES zero means that source area and destination area are | |
134 | overlapped, which means that we can produce a decoded text until it | |
135 | reaches at the head of not-yet-decoded source text. | |
136 | ||
137 | Below is a template of these functions. */ | |
4ed46869 | 138 | #if 0 |
d46c5b12 | 139 | decode_coding_XXX (coding, source, destination, src_bytes, dst_bytes) |
4ed46869 KH |
140 | struct coding_system *coding; |
141 | unsigned char *source, *destination; | |
142 | int src_bytes, dst_bytes; | |
4ed46869 KH |
143 | { |
144 | ... | |
145 | } | |
146 | #endif | |
147 | ||
148 | /*** GENERAL NOTES on `encode_coding_XXX ()' functions *** | |
149 | ||
0ef69138 KH |
150 | These functions encode SRC_BYTES length text at SOURCE of Emacs' |
151 | internal format (emacs-mule) to CODING. The resulting text goes to | |
f4dee582 | 152 | a place pointed to by DESTINATION, the length of which should not |
d46c5b12 KH |
153 | exceed DST_BYTES. These functions set the information of |
154 | original and encoded texts in the members produced, produced_char, | |
155 | consumed, and consumed_char of the structure *CODING. | |
156 | ||
157 | The return value is an integer (CODING_FINISH_XXX) indicating how | |
158 | the encoding finished. | |
159 | ||
160 | DST_BYTES zero means that source area and destination area are | |
161 | overlapped, which means that we can produce a decoded text until it | |
162 | reaches at the head of not-yet-decoded source text. | |
163 | ||
164 | Below is a template of these functions. */ | |
4ed46869 | 165 | #if 0 |
d46c5b12 | 166 | encode_coding_XXX (coding, source, destination, src_bytes, dst_bytes) |
4ed46869 KH |
167 | struct coding_system *coding; |
168 | unsigned char *source, *destination; | |
169 | int src_bytes, dst_bytes; | |
4ed46869 KH |
170 | { |
171 | ... | |
172 | } | |
173 | #endif | |
174 | ||
175 | /*** COMMONLY USED MACROS ***/ | |
176 | ||
177 | /* The following three macros ONE_MORE_BYTE, TWO_MORE_BYTES, and | |
178 | THREE_MORE_BYTES safely get one, two, and three bytes from the | |
179 | source text respectively. If there are not enough bytes in the | |
180 | source, they jump to `label_end_of_loop'. The caller should set | |
181 | variables `src' and `src_end' to appropriate areas in advance. */ | |
182 | ||
183 | #define ONE_MORE_BYTE(c1) \ | |
184 | do { \ | |
185 | if (src < src_end) \ | |
186 | c1 = *src++; \ | |
187 | else \ | |
188 | goto label_end_of_loop; \ | |
189 | } while (0) | |
190 | ||
191 | #define TWO_MORE_BYTES(c1, c2) \ | |
192 | do { \ | |
193 | if (src + 1 < src_end) \ | |
194 | c1 = *src++, c2 = *src++; \ | |
195 | else \ | |
196 | goto label_end_of_loop; \ | |
197 | } while (0) | |
198 | ||
199 | #define THREE_MORE_BYTES(c1, c2, c3) \ | |
200 | do { \ | |
201 | if (src + 2 < src_end) \ | |
202 | c1 = *src++, c2 = *src++, c3 = *src++; \ | |
203 | else \ | |
204 | goto label_end_of_loop; \ | |
205 | } while (0) | |
206 | ||
207 | /* The following three macros DECODE_CHARACTER_ASCII, | |
208 | DECODE_CHARACTER_DIMENSION1, and DECODE_CHARACTER_DIMENSION2 put | |
209 | the multi-byte form of a character of each class at the place | |
210 | pointed by `dst'. The caller should set the variable `dst' to | |
211 | point to an appropriate area and the variable `coding' to point to | |
212 | the coding-system of the currently decoding text in advance. */ | |
213 | ||
214 | /* Decode one ASCII character C. */ | |
215 | ||
de79a6a5 KH |
216 | #define DECODE_CHARACTER_ASCII(c) \ |
217 | do { \ | |
218 | if (COMPOSING_P (coding->composing)) \ | |
219 | { \ | |
220 | *dst++ = 0xA0, *dst++ = (c) | 0x80; \ | |
221 | coding->composed_chars++; \ | |
222 | } \ | |
223 | else \ | |
224 | { \ | |
225 | *dst++ = (c); \ | |
226 | coding->produced_char++; \ | |
227 | } \ | |
4ed46869 KH |
228 | } while (0) |
229 | ||
f4dee582 | 230 | /* Decode one DIMENSION1 character whose charset is CHARSET and whose |
4ed46869 KH |
231 | position-code is C. */ |
232 | ||
233 | #define DECODE_CHARACTER_DIMENSION1(charset, c) \ | |
234 | do { \ | |
235 | unsigned char leading_code = CHARSET_LEADING_CODE_BASE (charset); \ | |
236 | if (COMPOSING_P (coding->composing)) \ | |
de79a6a5 KH |
237 | { \ |
238 | *dst++ = leading_code + 0x20; \ | |
239 | coding->composed_chars++; \ | |
240 | } \ | |
4ed46869 | 241 | else \ |
d46c5b12 KH |
242 | { \ |
243 | *dst++ = leading_code; \ | |
244 | coding->produced_char++; \ | |
245 | } \ | |
4ed46869 KH |
246 | if (leading_code = CHARSET_LEADING_CODE_EXT (charset)) \ |
247 | *dst++ = leading_code; \ | |
248 | *dst++ = (c) | 0x80; \ | |
249 | } while (0) | |
250 | ||
f4dee582 | 251 | /* Decode one DIMENSION2 character whose charset is CHARSET and whose |
4ed46869 KH |
252 | position-codes are C1 and C2. */ |
253 | ||
254 | #define DECODE_CHARACTER_DIMENSION2(charset, c1, c2) \ | |
255 | do { \ | |
256 | DECODE_CHARACTER_DIMENSION1 (charset, c1); \ | |
257 | *dst++ = (c2) | 0x80; \ | |
258 | } while (0) | |
259 | ||
260 | \f | |
261 | /*** 1. Preamble ***/ | |
262 | ||
263 | #include <stdio.h> | |
264 | ||
265 | #ifdef emacs | |
266 | ||
267 | #include <config.h> | |
268 | #include "lisp.h" | |
269 | #include "buffer.h" | |
270 | #include "charset.h" | |
271 | #include "ccl.h" | |
272 | #include "coding.h" | |
273 | #include "window.h" | |
274 | ||
275 | #else /* not emacs */ | |
276 | ||
277 | #include "mulelib.h" | |
278 | ||
279 | #endif /* not emacs */ | |
280 | ||
281 | Lisp_Object Qcoding_system, Qeol_type; | |
282 | Lisp_Object Qbuffer_file_coding_system; | |
283 | Lisp_Object Qpost_read_conversion, Qpre_write_conversion; | |
27901516 | 284 | Lisp_Object Qno_conversion, Qundecided; |
bb0115a2 | 285 | Lisp_Object Qcoding_system_history; |
70c22245 | 286 | Lisp_Object Qsafe_charsets; |
1397dc18 | 287 | Lisp_Object Qvalid_codes; |
4ed46869 KH |
288 | |
289 | extern Lisp_Object Qinsert_file_contents, Qwrite_region; | |
290 | Lisp_Object Qcall_process, Qcall_process_region, Qprocess_argument; | |
291 | Lisp_Object Qstart_process, Qopen_network_stream; | |
292 | Lisp_Object Qtarget_idx; | |
293 | ||
d46c5b12 KH |
294 | Lisp_Object Vselect_safe_coding_system_function; |
295 | ||
4ed46869 KH |
296 | /* Mnemonic character of each format of end-of-line. */ |
297 | int eol_mnemonic_unix, eol_mnemonic_dos, eol_mnemonic_mac; | |
298 | /* Mnemonic character to indicate format of end-of-line is not yet | |
299 | decided. */ | |
300 | int eol_mnemonic_undecided; | |
301 | ||
9ce27fde KH |
302 | /* Format of end-of-line decided by system. This is CODING_EOL_LF on |
303 | Unix, CODING_EOL_CRLF on DOS/Windows, and CODING_EOL_CR on Mac. */ | |
304 | int system_eol_type; | |
305 | ||
4ed46869 KH |
306 | #ifdef emacs |
307 | ||
4608c386 KH |
308 | Lisp_Object Vcoding_system_list, Vcoding_system_alist; |
309 | ||
310 | Lisp_Object Qcoding_system_p, Qcoding_system_error; | |
4ed46869 | 311 | |
d46c5b12 KH |
312 | /* Coding system emacs-mule and raw-text are for converting only |
313 | end-of-line format. */ | |
314 | Lisp_Object Qemacs_mule, Qraw_text; | |
9ce27fde | 315 | |
4ed46869 KH |
316 | /* Coding-systems are handed between Emacs Lisp programs and C internal |
317 | routines by the following three variables. */ | |
318 | /* Coding-system for reading files and receiving data from process. */ | |
319 | Lisp_Object Vcoding_system_for_read; | |
320 | /* Coding-system for writing files and sending data to process. */ | |
321 | Lisp_Object Vcoding_system_for_write; | |
322 | /* Coding-system actually used in the latest I/O. */ | |
323 | Lisp_Object Vlast_coding_system_used; | |
324 | ||
c4825358 | 325 | /* A vector of length 256 which contains information about special |
94487c4e | 326 | Latin codes (especially for dealing with Microsoft codes). */ |
3f003981 | 327 | Lisp_Object Vlatin_extra_code_table; |
c4825358 | 328 | |
9ce27fde KH |
329 | /* Flag to inhibit code conversion of end-of-line format. */ |
330 | int inhibit_eol_conversion; | |
331 | ||
ed29121d EZ |
332 | /* Flag to make buffer-file-coding-system inherit from process-coding. */ |
333 | int inherit_process_coding_system; | |
334 | ||
c4825358 | 335 | /* Coding system to be used to encode text for terminal display. */ |
4ed46869 KH |
336 | struct coding_system terminal_coding; |
337 | ||
c4825358 KH |
338 | /* Coding system to be used to encode text for terminal display when |
339 | terminal coding system is nil. */ | |
340 | struct coding_system safe_terminal_coding; | |
341 | ||
342 | /* Coding system of what is sent from terminal keyboard. */ | |
4ed46869 KH |
343 | struct coding_system keyboard_coding; |
344 | ||
6bc51348 KH |
345 | /* Default coding system to be used to write a file. */ |
346 | struct coding_system default_buffer_file_coding; | |
347 | ||
02ba4723 KH |
348 | Lisp_Object Vfile_coding_system_alist; |
349 | Lisp_Object Vprocess_coding_system_alist; | |
350 | Lisp_Object Vnetwork_coding_system_alist; | |
4ed46869 KH |
351 | |
352 | #endif /* emacs */ | |
353 | ||
d46c5b12 | 354 | Lisp_Object Qcoding_category, Qcoding_category_index; |
4ed46869 KH |
355 | |
356 | /* List of symbols `coding-category-xxx' ordered by priority. */ | |
357 | Lisp_Object Vcoding_category_list; | |
358 | ||
d46c5b12 KH |
359 | /* Table of coding categories (Lisp symbols). */ |
360 | Lisp_Object Vcoding_category_table; | |
4ed46869 KH |
361 | |
362 | /* Table of names of symbol for each coding-category. */ | |
363 | char *coding_category_name[CODING_CATEGORY_IDX_MAX] = { | |
0ef69138 | 364 | "coding-category-emacs-mule", |
4ed46869 KH |
365 | "coding-category-sjis", |
366 | "coding-category-iso-7", | |
d46c5b12 | 367 | "coding-category-iso-7-tight", |
4ed46869 KH |
368 | "coding-category-iso-8-1", |
369 | "coding-category-iso-8-2", | |
7717c392 KH |
370 | "coding-category-iso-7-else", |
371 | "coding-category-iso-8-else", | |
89fa8b36 | 372 | "coding-category-ccl", |
4ed46869 | 373 | "coding-category-big5", |
27901516 | 374 | "coding-category-raw-text", |
89fa8b36 | 375 | "coding-category-binary" |
4ed46869 KH |
376 | }; |
377 | ||
66cfb530 | 378 | /* Table of pointers to coding systems corresponding to each coding |
d46c5b12 KH |
379 | categories. */ |
380 | struct coding_system *coding_system_table[CODING_CATEGORY_IDX_MAX]; | |
381 | ||
66cfb530 KH |
382 | /* Table of coding category masks. Nth element is a mask for a coding |
383 | cateogry of which priority is Nth. */ | |
384 | static | |
385 | int coding_priorities[CODING_CATEGORY_IDX_MAX]; | |
386 | ||
f967223b KH |
387 | /* Flag to tell if we look up translation table on character code |
388 | conversion. */ | |
84fbb8a0 | 389 | Lisp_Object Venable_character_translation; |
f967223b KH |
390 | /* Standard translation table to look up on decoding (reading). */ |
391 | Lisp_Object Vstandard_translation_table_for_decode; | |
392 | /* Standard translation table to look up on encoding (writing). */ | |
393 | Lisp_Object Vstandard_translation_table_for_encode; | |
84fbb8a0 | 394 | |
f967223b KH |
395 | Lisp_Object Qtranslation_table; |
396 | Lisp_Object Qtranslation_table_id; | |
397 | Lisp_Object Qtranslation_table_for_decode; | |
398 | Lisp_Object Qtranslation_table_for_encode; | |
4ed46869 KH |
399 | |
400 | /* Alist of charsets vs revision number. */ | |
401 | Lisp_Object Vcharset_revision_alist; | |
402 | ||
02ba4723 KH |
403 | /* Default coding systems used for process I/O. */ |
404 | Lisp_Object Vdefault_process_coding_system; | |
405 | ||
4ed46869 | 406 | \f |
0ef69138 | 407 | /*** 2. Emacs internal format (emacs-mule) handlers ***/ |
4ed46869 KH |
408 | |
409 | /* Emacs' internal format for encoding multiple character sets is a | |
f4dee582 RS |
410 | kind of multi-byte encoding, i.e. characters are encoded by |
411 | variable-length sequences of one-byte codes. ASCII characters | |
412 | and control characters (e.g. `tab', `newline') are represented by | |
413 | one-byte sequences which are their ASCII codes, in the range 0x00 | |
414 | through 0x7F. The other characters are represented by a sequence | |
415 | of `base leading-code', optional `extended leading-code', and one | |
416 | or two `position-code's. The length of the sequence is determined | |
417 | by the base leading-code. Leading-code takes the range 0x80 | |
418 | through 0x9F, whereas extended leading-code and position-code take | |
419 | the range 0xA0 through 0xFF. See `charset.h' for more details | |
420 | about leading-code and position-code. | |
421 | ||
422 | There's one exception to this rule. Special leading-code | |
4ed46869 KH |
423 | `leading-code-composition' denotes that the following several |
424 | characters should be composed into one character. Leading-codes of | |
425 | components (except for ASCII) are added 0x20. An ASCII character | |
426 | component is represented by a 2-byte sequence of `0xA0' and | |
f4dee582 RS |
427 | `ASCII-code + 0x80'. See also the comments in `charset.h' for the |
428 | details of composite character. Hence, we can summarize the code | |
4ed46869 KH |
429 | range as follows: |
430 | ||
431 | --- CODE RANGE of Emacs' internal format --- | |
432 | (character set) (range) | |
433 | ASCII 0x00 .. 0x7F | |
434 | ELSE (1st byte) 0x80 .. 0x9F | |
435 | (rest bytes) 0xA0 .. 0xFF | |
436 | --------------------------------------------- | |
437 | ||
438 | */ | |
439 | ||
440 | enum emacs_code_class_type emacs_code_class[256]; | |
441 | ||
442 | /* Go to the next statement only if *SRC is accessible and the code is | |
443 | greater than 0xA0. */ | |
444 | #define CHECK_CODE_RANGE_A0_FF \ | |
445 | do { \ | |
446 | if (src >= src_end) \ | |
447 | goto label_end_of_switch; \ | |
448 | else if (*src++ < 0xA0) \ | |
449 | return 0; \ | |
450 | } while (0) | |
451 | ||
452 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
453 | Check if a text is encoded in Emacs' internal format. If it is, | |
d46c5b12 | 454 | return CODING_CATEGORY_MASK_EMACS_MULE, else return 0. */ |
4ed46869 KH |
455 | |
456 | int | |
0ef69138 | 457 | detect_coding_emacs_mule (src, src_end) |
4ed46869 KH |
458 | unsigned char *src, *src_end; |
459 | { | |
460 | unsigned char c; | |
461 | int composing = 0; | |
462 | ||
463 | while (src < src_end) | |
464 | { | |
465 | c = *src++; | |
466 | ||
467 | if (composing) | |
468 | { | |
469 | if (c < 0xA0) | |
470 | composing = 0; | |
471 | else | |
472 | c -= 0x20; | |
473 | } | |
474 | ||
475 | switch (emacs_code_class[c]) | |
476 | { | |
477 | case EMACS_ascii_code: | |
478 | case EMACS_linefeed_code: | |
479 | break; | |
480 | ||
481 | case EMACS_control_code: | |
482 | if (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) | |
483 | return 0; | |
484 | break; | |
485 | ||
486 | case EMACS_invalid_code: | |
487 | return 0; | |
488 | ||
489 | case EMACS_leading_code_composition: /* c == 0x80 */ | |
490 | if (composing) | |
491 | CHECK_CODE_RANGE_A0_FF; | |
492 | else | |
493 | composing = 1; | |
494 | break; | |
495 | ||
496 | case EMACS_leading_code_4: | |
497 | CHECK_CODE_RANGE_A0_FF; | |
498 | /* fall down to check it two more times ... */ | |
499 | ||
500 | case EMACS_leading_code_3: | |
501 | CHECK_CODE_RANGE_A0_FF; | |
502 | /* fall down to check it one more time ... */ | |
503 | ||
504 | case EMACS_leading_code_2: | |
505 | CHECK_CODE_RANGE_A0_FF; | |
506 | break; | |
507 | ||
508 | default: | |
509 | label_end_of_switch: | |
510 | break; | |
511 | } | |
512 | } | |
0ef69138 | 513 | return CODING_CATEGORY_MASK_EMACS_MULE; |
4ed46869 KH |
514 | } |
515 | ||
516 | \f | |
517 | /*** 3. ISO2022 handlers ***/ | |
518 | ||
519 | /* The following note describes the coding system ISO2022 briefly. | |
f4dee582 RS |
520 | Since the intention of this note is to help in understanding of |
521 | the programs in this file, some parts are NOT ACCURATE or OVERLY | |
4ed46869 KH |
522 | SIMPLIFIED. For the thorough understanding, please refer to the |
523 | original document of ISO2022. | |
524 | ||
525 | ISO2022 provides many mechanisms to encode several character sets | |
f4dee582 | 526 | in 7-bit and 8-bit environment. If one chooses 7-bite environment, |
4ed46869 | 527 | all text is encoded by codes of less than 128. This may make the |
f4dee582 RS |
528 | encoded text a little bit longer, but the text gets more stability |
529 | to pass through several gateways (some of them strip off the MSB). | |
4ed46869 | 530 | |
f4dee582 | 531 | There are two kinds of character set: control character set and |
4ed46869 KH |
532 | graphic character set. The former contains control characters such |
533 | as `newline' and `escape' to provide control functions (control | |
f4dee582 | 534 | functions are provided also by escape sequences). The latter |
4ed46869 KH |
535 | contains graphic characters such as ' A' and '-'. Emacs recognizes |
536 | two control character sets and many graphic character sets. | |
537 | ||
538 | Graphic character sets are classified into one of the following | |
539 | four classes, DIMENSION1_CHARS94, DIMENSION1_CHARS96, | |
540 | DIMENSION2_CHARS94, DIMENSION2_CHARS96 according to the number of | |
541 | bytes (DIMENSION) and the number of characters in one dimension | |
542 | (CHARS) of the set. In addition, each character set is assigned an | |
543 | identification tag (called "final character" and denoted as <F> | |
544 | here after) which is unique in each class. <F> of each character | |
545 | set is decided by ECMA(*) when it is registered in ISO. Code range | |
546 | of <F> is 0x30..0x7F (0x30..0x3F are for private use only). | |
547 | ||
548 | Note (*): ECMA = European Computer Manufacturers Association | |
549 | ||
550 | Here are examples of graphic character set [NAME(<F>)]: | |
551 | o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ... | |
552 | o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ... | |
553 | o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ... | |
554 | o DIMENSION2_CHARS96 -- none for the moment | |
555 | ||
556 | A code area (1byte=8bits) is divided into 4 areas, C0, GL, C1, and GR. | |
557 | C0 [0x00..0x1F] -- control character plane 0 | |
558 | GL [0x20..0x7F] -- graphic character plane 0 | |
559 | C1 [0x80..0x9F] -- control character plane 1 | |
560 | GR [0xA0..0xFF] -- graphic character plane 1 | |
561 | ||
562 | A control character set is directly designated and invoked to C0 or | |
563 | C1 by an escape sequence. The most common case is that ISO646's | |
564 | control character set is designated/invoked to C0 and ISO6429's | |
565 | control character set is designated/invoked to C1, and usually | |
566 | these designations/invocations are omitted in a coded text. With | |
567 | 7-bit environment, only C0 can be used, and a control character for | |
568 | C1 is encoded by an appropriate escape sequence to fit in the | |
569 | environment. All control characters for C1 are defined the | |
570 | corresponding escape sequences. | |
571 | ||
572 | A graphic character set is at first designated to one of four | |
573 | graphic registers (G0 through G3), then these graphic registers are | |
574 | invoked to GL or GR. These designations and invocations can be | |
575 | done independently. The most common case is that G0 is invoked to | |
576 | GL, G1 is invoked to GR, and ASCII is designated to G0, and usually | |
577 | these invocations and designations are omitted in a coded text. | |
578 | With 7-bit environment, only GL can be used. | |
579 | ||
580 | When a graphic character set of CHARS94 is invoked to GL, code 0x20 | |
581 | and 0x7F of GL area work as control characters SPACE and DEL | |
582 | respectively, and code 0xA0 and 0xFF of GR area should not be used. | |
583 | ||
584 | There are two ways of invocation: locking-shift and single-shift. | |
585 | With locking-shift, the invocation lasts until the next different | |
586 | invocation, whereas with single-shift, the invocation works only | |
587 | for the following character and doesn't affect locking-shift. | |
588 | Invocations are done by the following control characters or escape | |
589 | sequences. | |
590 | ||
591 | ---------------------------------------------------------------------- | |
592 | function control char escape sequence description | |
593 | ---------------------------------------------------------------------- | |
594 | SI (shift-in) 0x0F none invoke G0 to GL | |
10bff6f1 | 595 | SO (shift-out) 0x0E none invoke G1 to GL |
4ed46869 KH |
596 | LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL |
597 | LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL | |
598 | SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 into GL | |
599 | SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 into GL | |
600 | ---------------------------------------------------------------------- | |
601 | The first four are for locking-shift. Control characters for these | |
602 | functions are defined by macros ISO_CODE_XXX in `coding.h'. | |
603 | ||
604 | Designations are done by the following escape sequences. | |
605 | ---------------------------------------------------------------------- | |
606 | escape sequence description | |
607 | ---------------------------------------------------------------------- | |
608 | ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0 | |
609 | ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1 | |
610 | ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2 | |
611 | ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3 | |
612 | ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*) | |
613 | ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1 | |
614 | ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2 | |
615 | ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3 | |
616 | ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**) | |
617 | ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1 | |
618 | ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2 | |
619 | ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3 | |
620 | ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*) | |
621 | ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1 | |
622 | ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2 | |
623 | ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3 | |
624 | ---------------------------------------------------------------------- | |
625 | ||
626 | In this list, "DIMENSION1_CHARS94<F>" means a graphic character set | |
627 | of dimension 1, chars 94, and final character <F>, and etc. | |
628 | ||
629 | Note (*): Although these designations are not allowed in ISO2022, | |
630 | Emacs accepts them on decoding, and produces them on encoding | |
631 | CHARS96 character set in a coding system which is characterized as | |
632 | 7-bit environment, non-locking-shift, and non-single-shift. | |
633 | ||
634 | Note (**): If <F> is '@', 'A', or 'B', the intermediate character | |
635 | '(' can be omitted. We call this as "short-form" here after. | |
636 | ||
637 | Now you may notice that there are a lot of ways for encoding the | |
f4dee582 | 638 | same multilingual text in ISO2022. Actually, there exists many |
4ed46869 KH |
639 | coding systems such as Compound Text (used in X's inter client |
640 | communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR | |
641 | (used in Korean Internet), EUC (Extended UNIX Code, used in Asian | |
642 | localized platforms), and all of these are variants of ISO2022. | |
643 | ||
644 | In addition to the above, Emacs handles two more kinds of escape | |
645 | sequences: ISO6429's direction specification and Emacs' private | |
646 | sequence for specifying character composition. | |
647 | ||
648 | ISO6429's direction specification takes the following format: | |
649 | o CSI ']' -- end of the current direction | |
650 | o CSI '0' ']' -- end of the current direction | |
651 | o CSI '1' ']' -- start of left-to-right text | |
652 | o CSI '2' ']' -- start of right-to-left text | |
653 | The control character CSI (0x9B: control sequence introducer) is | |
654 | abbreviated to the escape sequence ESC '[' in 7-bit environment. | |
655 | ||
656 | Character composition specification takes the following format: | |
657 | o ESC '0' -- start character composition | |
658 | o ESC '1' -- end character composition | |
659 | Since these are not standard escape sequences of any ISO, the use | |
660 | of them for these meaning is restricted to Emacs only. */ | |
661 | ||
662 | enum iso_code_class_type iso_code_class[256]; | |
663 | ||
f024b6aa RS |
664 | #define CHARSET_OK(idx, charset) \ |
665 | (coding_system_table[idx] \ | |
666 | && (coding_system_table[idx]->safe_charsets[charset] \ | |
667 | || (CODING_SPEC_ISO_REQUESTED_DESIGNATION \ | |
668 | (coding_system_table[idx], charset) \ | |
669 | != CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION))) | |
d46c5b12 KH |
670 | |
671 | #define SHIFT_OUT_OK(idx) \ | |
672 | (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding_system_table[idx], 1) >= 0) | |
673 | ||
4ed46869 KH |
674 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". |
675 | Check if a text is encoded in ISO2022. If it is, returns an | |
676 | integer in which appropriate flag bits any of: | |
677 | CODING_CATEGORY_MASK_ISO_7 | |
d46c5b12 | 678 | CODING_CATEGORY_MASK_ISO_7_TIGHT |
4ed46869 KH |
679 | CODING_CATEGORY_MASK_ISO_8_1 |
680 | CODING_CATEGORY_MASK_ISO_8_2 | |
7717c392 KH |
681 | CODING_CATEGORY_MASK_ISO_7_ELSE |
682 | CODING_CATEGORY_MASK_ISO_8_ELSE | |
4ed46869 KH |
683 | are set. If a code which should never appear in ISO2022 is found, |
684 | returns 0. */ | |
685 | ||
686 | int | |
687 | detect_coding_iso2022 (src, src_end) | |
688 | unsigned char *src, *src_end; | |
689 | { | |
d46c5b12 KH |
690 | int mask = CODING_CATEGORY_MASK_ISO; |
691 | int mask_found = 0; | |
f46869e4 | 692 | int reg[4], shift_out = 0, single_shifting = 0; |
d46c5b12 | 693 | int c, c1, i, charset; |
3f003981 | 694 | |
d46c5b12 | 695 | reg[0] = CHARSET_ASCII, reg[1] = reg[2] = reg[3] = -1; |
3f003981 | 696 | while (mask && src < src_end) |
4ed46869 KH |
697 | { |
698 | c = *src++; | |
699 | switch (c) | |
700 | { | |
701 | case ISO_CODE_ESC: | |
f46869e4 | 702 | single_shifting = 0; |
e0e989f6 | 703 | if (src >= src_end) |
4ed46869 KH |
704 | break; |
705 | c = *src++; | |
d46c5b12 | 706 | if (c >= '(' && c <= '/') |
4ed46869 | 707 | { |
bf9cdd4e KH |
708 | /* Designation sequence for a charset of dimension 1. */ |
709 | if (src >= src_end) | |
710 | break; | |
d46c5b12 KH |
711 | c1 = *src++; |
712 | if (c1 < ' ' || c1 >= 0x80 | |
713 | || (charset = iso_charset_table[0][c >= ','][c1]) < 0) | |
714 | /* Invalid designation sequence. Just ignore. */ | |
715 | break; | |
716 | reg[(c - '(') % 4] = charset; | |
bf9cdd4e KH |
717 | } |
718 | else if (c == '$') | |
719 | { | |
720 | /* Designation sequence for a charset of dimension 2. */ | |
721 | if (src >= src_end) | |
722 | break; | |
723 | c = *src++; | |
724 | if (c >= '@' && c <= 'B') | |
725 | /* Designation for JISX0208.1978, GB2312, or JISX0208. */ | |
d46c5b12 | 726 | reg[0] = charset = iso_charset_table[1][0][c]; |
bf9cdd4e | 727 | else if (c >= '(' && c <= '/') |
bcf26d6a | 728 | { |
bf9cdd4e KH |
729 | if (src >= src_end) |
730 | break; | |
d46c5b12 KH |
731 | c1 = *src++; |
732 | if (c1 < ' ' || c1 >= 0x80 | |
733 | || (charset = iso_charset_table[1][c >= ','][c1]) < 0) | |
734 | /* Invalid designation sequence. Just ignore. */ | |
735 | break; | |
736 | reg[(c - '(') % 4] = charset; | |
bcf26d6a | 737 | } |
bf9cdd4e | 738 | else |
d46c5b12 KH |
739 | /* Invalid designation sequence. Just ignore. */ |
740 | break; | |
741 | } | |
ae9ff118 | 742 | else if (c == 'N' || c == 'O') |
d46c5b12 | 743 | { |
ae9ff118 KH |
744 | /* ESC <Fe> for SS2 or SS3. */ |
745 | mask &= CODING_CATEGORY_MASK_ISO_7_ELSE; | |
d46c5b12 | 746 | break; |
4ed46869 | 747 | } |
bf9cdd4e | 748 | else if (c == '0' || c == '1' || c == '2') |
ae9ff118 | 749 | /* ESC <Fp> for start/end composition. Just ignore. */ |
d46c5b12 | 750 | break; |
bf9cdd4e | 751 | else |
d46c5b12 KH |
752 | /* Invalid escape sequence. Just ignore. */ |
753 | break; | |
754 | ||
755 | /* We found a valid designation sequence for CHARSET. */ | |
756 | mask &= ~CODING_CATEGORY_MASK_ISO_8BIT; | |
757 | if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7, charset)) | |
758 | mask_found |= CODING_CATEGORY_MASK_ISO_7; | |
759 | else | |
760 | mask &= ~CODING_CATEGORY_MASK_ISO_7; | |
761 | if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7_TIGHT, charset)) | |
762 | mask_found |= CODING_CATEGORY_MASK_ISO_7_TIGHT; | |
763 | else | |
764 | mask &= ~CODING_CATEGORY_MASK_ISO_7_TIGHT; | |
ae9ff118 KH |
765 | if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7_ELSE, charset)) |
766 | mask_found |= CODING_CATEGORY_MASK_ISO_7_ELSE; | |
767 | else | |
d46c5b12 | 768 | mask &= ~CODING_CATEGORY_MASK_ISO_7_ELSE; |
ae9ff118 KH |
769 | if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_8_ELSE, charset)) |
770 | mask_found |= CODING_CATEGORY_MASK_ISO_8_ELSE; | |
771 | else | |
d46c5b12 | 772 | mask &= ~CODING_CATEGORY_MASK_ISO_8_ELSE; |
4ed46869 KH |
773 | break; |
774 | ||
4ed46869 | 775 | case ISO_CODE_SO: |
f46869e4 | 776 | single_shifting = 0; |
d46c5b12 KH |
777 | if (shift_out == 0 |
778 | && (reg[1] >= 0 | |
779 | || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_7_ELSE) | |
780 | || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_8_ELSE))) | |
781 | { | |
782 | /* Locking shift out. */ | |
783 | mask &= ~CODING_CATEGORY_MASK_ISO_7BIT; | |
784 | mask_found |= CODING_CATEGORY_MASK_ISO_SHIFT; | |
785 | } | |
e0e989f6 KH |
786 | break; |
787 | ||
d46c5b12 | 788 | case ISO_CODE_SI: |
f46869e4 | 789 | single_shifting = 0; |
d46c5b12 KH |
790 | if (shift_out == 1) |
791 | { | |
792 | /* Locking shift in. */ | |
793 | mask &= ~CODING_CATEGORY_MASK_ISO_7BIT; | |
794 | mask_found |= CODING_CATEGORY_MASK_ISO_SHIFT; | |
795 | } | |
796 | break; | |
797 | ||
4ed46869 | 798 | case ISO_CODE_CSI: |
f46869e4 | 799 | single_shifting = 0; |
4ed46869 KH |
800 | case ISO_CODE_SS2: |
801 | case ISO_CODE_SS3: | |
3f003981 KH |
802 | { |
803 | int newmask = CODING_CATEGORY_MASK_ISO_8_ELSE; | |
804 | ||
70c22245 KH |
805 | if (c != ISO_CODE_CSI) |
806 | { | |
d46c5b12 KH |
807 | if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_1]->flags |
808 | & CODING_FLAG_ISO_SINGLE_SHIFT) | |
70c22245 | 809 | newmask |= CODING_CATEGORY_MASK_ISO_8_1; |
d46c5b12 KH |
810 | if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_2]->flags |
811 | & CODING_FLAG_ISO_SINGLE_SHIFT) | |
70c22245 | 812 | newmask |= CODING_CATEGORY_MASK_ISO_8_2; |
f46869e4 | 813 | single_shifting = 1; |
70c22245 | 814 | } |
3f003981 KH |
815 | if (VECTORP (Vlatin_extra_code_table) |
816 | && !NILP (XVECTOR (Vlatin_extra_code_table)->contents[c])) | |
817 | { | |
d46c5b12 KH |
818 | if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_1]->flags |
819 | & CODING_FLAG_ISO_LATIN_EXTRA) | |
3f003981 | 820 | newmask |= CODING_CATEGORY_MASK_ISO_8_1; |
d46c5b12 KH |
821 | if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_2]->flags |
822 | & CODING_FLAG_ISO_LATIN_EXTRA) | |
3f003981 KH |
823 | newmask |= CODING_CATEGORY_MASK_ISO_8_2; |
824 | } | |
825 | mask &= newmask; | |
d46c5b12 | 826 | mask_found |= newmask; |
3f003981 KH |
827 | } |
828 | break; | |
4ed46869 KH |
829 | |
830 | default: | |
831 | if (c < 0x80) | |
f46869e4 KH |
832 | { |
833 | single_shifting = 0; | |
834 | break; | |
835 | } | |
4ed46869 | 836 | else if (c < 0xA0) |
c4825358 | 837 | { |
f46869e4 | 838 | single_shifting = 0; |
3f003981 KH |
839 | if (VECTORP (Vlatin_extra_code_table) |
840 | && !NILP (XVECTOR (Vlatin_extra_code_table)->contents[c])) | |
c4825358 | 841 | { |
3f003981 KH |
842 | int newmask = 0; |
843 | ||
d46c5b12 KH |
844 | if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_1]->flags |
845 | & CODING_FLAG_ISO_LATIN_EXTRA) | |
3f003981 | 846 | newmask |= CODING_CATEGORY_MASK_ISO_8_1; |
d46c5b12 KH |
847 | if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_2]->flags |
848 | & CODING_FLAG_ISO_LATIN_EXTRA) | |
3f003981 KH |
849 | newmask |= CODING_CATEGORY_MASK_ISO_8_2; |
850 | mask &= newmask; | |
d46c5b12 | 851 | mask_found |= newmask; |
c4825358 | 852 | } |
3f003981 KH |
853 | else |
854 | return 0; | |
c4825358 | 855 | } |
4ed46869 KH |
856 | else |
857 | { | |
7717c392 | 858 | unsigned char *src_begin = src; |
4ed46869 | 859 | |
d46c5b12 | 860 | mask &= ~(CODING_CATEGORY_MASK_ISO_7BIT |
7717c392 | 861 | | CODING_CATEGORY_MASK_ISO_7_ELSE); |
d46c5b12 | 862 | mask_found |= CODING_CATEGORY_MASK_ISO_8_1; |
f46869e4 KH |
863 | /* Check the length of succeeding codes of the range |
864 | 0xA0..0FF. If the byte length is odd, we exclude | |
865 | CODING_CATEGORY_MASK_ISO_8_2. We can check this only | |
866 | when we are not single shifting. */ | |
867 | if (!single_shifting) | |
868 | { | |
869 | while (src < src_end && *src >= 0xA0) | |
870 | src++; | |
871 | if ((src - src_begin - 1) & 1 && src < src_end) | |
872 | mask &= ~CODING_CATEGORY_MASK_ISO_8_2; | |
873 | else | |
874 | mask_found |= CODING_CATEGORY_MASK_ISO_8_2; | |
875 | } | |
4ed46869 KH |
876 | } |
877 | break; | |
878 | } | |
879 | } | |
880 | ||
d46c5b12 | 881 | return (mask & mask_found); |
4ed46869 KH |
882 | } |
883 | ||
884 | /* Decode a character of which charset is CHARSET and the 1st position | |
bdd9fb48 | 885 | code is C1. If dimension of CHARSET is 2, the 2nd position code is |
4ed46869 KH |
886 | fetched from SRC and set to C2. If CHARSET is negative, it means |
887 | that we are decoding ill formed text, and what we can do is just to | |
888 | read C1 as is. */ | |
889 | ||
bdd9fb48 KH |
890 | #define DECODE_ISO_CHARACTER(charset, c1) \ |
891 | do { \ | |
892 | int c_alt, charset_alt = (charset); \ | |
893 | if (COMPOSING_HEAD_P (coding->composing)) \ | |
894 | { \ | |
895 | *dst++ = LEADING_CODE_COMPOSITION; \ | |
896 | if (COMPOSING_WITH_RULE_P (coding->composing)) \ | |
897 | /* To tell composition rules are embeded. */ \ | |
898 | *dst++ = 0xFF; \ | |
899 | coding->composing += 2; \ | |
900 | } \ | |
85bbb134 | 901 | if (charset_alt >= 0) \ |
bdd9fb48 | 902 | { \ |
85bbb134 | 903 | if (CHARSET_DIMENSION (charset_alt) == 2) \ |
70c22245 KH |
904 | { \ |
905 | ONE_MORE_BYTE (c2); \ | |
906 | if (iso_code_class[(c2) & 0x7F] != ISO_0x20_or_0x7F \ | |
907 | && iso_code_class[(c2) & 0x7F] != ISO_graphic_plane_0) \ | |
908 | { \ | |
909 | src--; \ | |
85bbb134 | 910 | charset_alt = CHARSET_ASCII; \ |
70c22245 KH |
911 | } \ |
912 | } \ | |
84fbb8a0 KH |
913 | if (!NILP (translation_table) \ |
914 | && ((c_alt = translate_char (translation_table, \ | |
85bbb134 | 915 | -1, charset_alt, c1, c2)) >= 0)) \ |
bdd9fb48 KH |
916 | SPLIT_CHAR (c_alt, charset_alt, c1, c2); \ |
917 | } \ | |
918 | if (charset_alt == CHARSET_ASCII || charset_alt < 0) \ | |
919 | DECODE_CHARACTER_ASCII (c1); \ | |
920 | else if (CHARSET_DIMENSION (charset_alt) == 1) \ | |
921 | DECODE_CHARACTER_DIMENSION1 (charset_alt, c1); \ | |
922 | else \ | |
923 | DECODE_CHARACTER_DIMENSION2 (charset_alt, c1, c2); \ | |
924 | if (COMPOSING_WITH_RULE_P (coding->composing)) \ | |
925 | /* To tell a composition rule follows. */ \ | |
926 | coding->composing = COMPOSING_WITH_RULE_RULE; \ | |
4ed46869 KH |
927 | } while (0) |
928 | ||
929 | /* Set designation state into CODING. */ | |
d46c5b12 KH |
930 | #define DECODE_DESIGNATION(reg, dimension, chars, final_char) \ |
931 | do { \ | |
932 | int charset = ISO_CHARSET_TABLE (make_number (dimension), \ | |
933 | make_number (chars), \ | |
934 | make_number (final_char)); \ | |
935 | if (charset >= 0 \ | |
704c5781 KH |
936 | && (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) == reg \ |
937 | || coding->safe_charsets[charset])) \ | |
d46c5b12 KH |
938 | { \ |
939 | if (coding->spec.iso2022.last_invalid_designation_register == 0 \ | |
940 | && reg == 0 \ | |
941 | && charset == CHARSET_ASCII) \ | |
942 | { \ | |
943 | /* We should insert this designation sequence as is so \ | |
944 | that it is surely written back to a file. */ \ | |
945 | coding->spec.iso2022.last_invalid_designation_register = -1; \ | |
946 | goto label_invalid_code; \ | |
947 | } \ | |
948 | coding->spec.iso2022.last_invalid_designation_register = -1; \ | |
949 | if ((coding->mode & CODING_MODE_DIRECTION) \ | |
950 | && CHARSET_REVERSE_CHARSET (charset) >= 0) \ | |
951 | charset = CHARSET_REVERSE_CHARSET (charset); \ | |
952 | CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \ | |
953 | } \ | |
954 | else \ | |
955 | { \ | |
956 | coding->spec.iso2022.last_invalid_designation_register = reg; \ | |
957 | goto label_invalid_code; \ | |
958 | } \ | |
4ed46869 KH |
959 | } while (0) |
960 | ||
88993dfd KH |
961 | /* Return 0 if there's a valid composing sequence starting at SRC and |
962 | ending before SRC_END, else return -1. */ | |
d46c5b12 | 963 | |
84fbb8a0 KH |
964 | int |
965 | check_composing_code (coding, src, src_end) | |
d46c5b12 KH |
966 | struct coding_system *coding; |
967 | unsigned char *src, *src_end; | |
968 | { | |
d46c5b12 KH |
969 | int charset, c, c1, dim; |
970 | ||
971 | while (src < src_end) | |
972 | { | |
88993dfd KH |
973 | c = *src++; |
974 | if (c >= 0x20) | |
975 | continue; | |
976 | if (c != ISO_CODE_ESC || src >= src_end) | |
977 | return -1; | |
978 | c = *src++; | |
979 | if (c == '1') /* end of compsition */ | |
980 | return 0; | |
981 | if (src + 2 >= src_end | |
982 | || !coding->flags & CODING_FLAG_ISO_DESIGNATION) | |
983 | return -1; | |
984 | ||
985 | dim = (c == '$'); | |
986 | if (dim == 1) | |
987 | c = (*src >= '@' && *src <= 'B') ? '(' : *src++; | |
988 | if (c >= '(' && c <= '/') | |
d46c5b12 | 989 | { |
88993dfd KH |
990 | c1 = *src++; |
991 | if ((c1 < ' ' || c1 >= 0x80) | |
992 | || (charset = iso_charset_table[dim][c >= ','][c1]) < 0 | |
993 | || ! coding->safe_charsets[charset] | |
994 | || (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) | |
995 | == CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION)) | |
996 | return -1; | |
d46c5b12 | 997 | } |
88993dfd KH |
998 | else |
999 | return -1; | |
d46c5b12 | 1000 | } |
88993dfd KH |
1001 | |
1002 | /* We have not found the sequence "ESC 1". */ | |
1003 | return -1; | |
d46c5b12 KH |
1004 | } |
1005 | ||
4ed46869 KH |
1006 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ |
1007 | ||
1008 | int | |
d46c5b12 | 1009 | decode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes) |
4ed46869 KH |
1010 | struct coding_system *coding; |
1011 | unsigned char *source, *destination; | |
1012 | int src_bytes, dst_bytes; | |
4ed46869 KH |
1013 | { |
1014 | unsigned char *src = source; | |
1015 | unsigned char *src_end = source + src_bytes; | |
1016 | unsigned char *dst = destination; | |
1017 | unsigned char *dst_end = destination + dst_bytes; | |
1018 | /* Since the maximum bytes produced by each loop is 7, we subtract 6 | |
1019 | from DST_END to assure that overflow checking is necessary only | |
1020 | at the head of loop. */ | |
1021 | unsigned char *adjusted_dst_end = dst_end - 6; | |
1022 | int charset; | |
1023 | /* Charsets invoked to graphic plane 0 and 1 respectively. */ | |
1024 | int charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0); | |
1025 | int charset1 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 1); | |
84fbb8a0 | 1026 | Lisp_Object translation_table |
f967223b | 1027 | = coding->translation_table_for_decode; |
d46c5b12 | 1028 | int result = CODING_FINISH_NORMAL; |
bdd9fb48 | 1029 | |
84fbb8a0 | 1030 | if (!NILP (Venable_character_translation) && NILP (translation_table)) |
f967223b | 1031 | translation_table = Vstandard_translation_table_for_decode; |
4ed46869 | 1032 | |
d46c5b12 | 1033 | coding->produced_char = 0; |
de79a6a5 | 1034 | coding->composed_chars = 0; |
fb88bf2d | 1035 | coding->fake_multibyte = 0; |
d46c5b12 KH |
1036 | while (src < src_end && (dst_bytes |
1037 | ? (dst < adjusted_dst_end) | |
1038 | : (dst < src - 6))) | |
4ed46869 KH |
1039 | { |
1040 | /* SRC_BASE remembers the start position in source in each loop. | |
1041 | The loop will be exited when there's not enough source text | |
1042 | to analyze long escape sequence or 2-byte code (within macros | |
1043 | ONE_MORE_BYTE or TWO_MORE_BYTES). In that case, SRC is reset | |
1044 | to SRC_BASE before exiting. */ | |
1045 | unsigned char *src_base = src; | |
bdd9fb48 | 1046 | int c1 = *src++, c2; |
4ed46869 KH |
1047 | |
1048 | switch (iso_code_class [c1]) | |
1049 | { | |
1050 | case ISO_0x20_or_0x7F: | |
1051 | if (!coding->composing | |
1052 | && (charset0 < 0 || CHARSET_CHARS (charset0) == 94)) | |
1053 | { | |
1054 | /* This is SPACE or DEL. */ | |
1055 | *dst++ = c1; | |
d46c5b12 | 1056 | coding->produced_char++; |
4ed46869 KH |
1057 | break; |
1058 | } | |
1059 | /* This is a graphic character, we fall down ... */ | |
1060 | ||
1061 | case ISO_graphic_plane_0: | |
1062 | if (coding->composing == COMPOSING_WITH_RULE_RULE) | |
1063 | { | |
1064 | /* This is a composition rule. */ | |
1065 | *dst++ = c1 | 0x80; | |
1066 | coding->composing = COMPOSING_WITH_RULE_TAIL; | |
1067 | } | |
1068 | else | |
1069 | DECODE_ISO_CHARACTER (charset0, c1); | |
1070 | break; | |
1071 | ||
1072 | case ISO_0xA0_or_0xFF: | |
d46c5b12 KH |
1073 | if (charset1 < 0 || CHARSET_CHARS (charset1) == 94 |
1074 | || coding->flags & CODING_FLAG_ISO_SEVEN_BITS) | |
fb88bf2d | 1075 | goto label_invalid_code; |
4ed46869 KH |
1076 | /* This is a graphic character, we fall down ... */ |
1077 | ||
1078 | case ISO_graphic_plane_1: | |
d46c5b12 | 1079 | if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) |
fb88bf2d | 1080 | goto label_invalid_code; |
d46c5b12 KH |
1081 | else |
1082 | DECODE_ISO_CHARACTER (charset1, c1); | |
4ed46869 KH |
1083 | break; |
1084 | ||
1085 | case ISO_control_code: | |
1086 | /* All ISO2022 control characters in this class have the | |
1087 | same representation in Emacs internal format. */ | |
d46c5b12 KH |
1088 | if (c1 == '\n' |
1089 | && (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL) | |
1090 | && (coding->eol_type == CODING_EOL_CR | |
1091 | || coding->eol_type == CODING_EOL_CRLF)) | |
1092 | { | |
1093 | result = CODING_FINISH_INCONSISTENT_EOL; | |
1094 | goto label_end_of_loop_2; | |
1095 | } | |
4ed46869 | 1096 | *dst++ = c1; |
d46c5b12 | 1097 | coding->produced_char++; |
4ed46869 KH |
1098 | break; |
1099 | ||
1100 | case ISO_carriage_return: | |
1101 | if (coding->eol_type == CODING_EOL_CR) | |
d46c5b12 | 1102 | *dst++ = '\n'; |
4ed46869 KH |
1103 | else if (coding->eol_type == CODING_EOL_CRLF) |
1104 | { | |
1105 | ONE_MORE_BYTE (c1); | |
1106 | if (c1 == ISO_CODE_LF) | |
1107 | *dst++ = '\n'; | |
1108 | else | |
1109 | { | |
d46c5b12 KH |
1110 | if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL) |
1111 | { | |
1112 | result = CODING_FINISH_INCONSISTENT_EOL; | |
1113 | goto label_end_of_loop_2; | |
1114 | } | |
4ed46869 | 1115 | src--; |
d46c5b12 | 1116 | *dst++ = '\r'; |
4ed46869 KH |
1117 | } |
1118 | } | |
1119 | else | |
d46c5b12 KH |
1120 | *dst++ = c1; |
1121 | coding->produced_char++; | |
4ed46869 KH |
1122 | break; |
1123 | ||
1124 | case ISO_shift_out: | |
d46c5b12 KH |
1125 | if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT) |
1126 | || CODING_SPEC_ISO_DESIGNATION (coding, 1) < 0) | |
1127 | goto label_invalid_code; | |
4ed46869 KH |
1128 | CODING_SPEC_ISO_INVOCATION (coding, 0) = 1; |
1129 | charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0); | |
1130 | break; | |
1131 | ||
1132 | case ISO_shift_in: | |
d46c5b12 KH |
1133 | if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT)) |
1134 | goto label_invalid_code; | |
4ed46869 KH |
1135 | CODING_SPEC_ISO_INVOCATION (coding, 0) = 0; |
1136 | charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0); | |
1137 | break; | |
1138 | ||
1139 | case ISO_single_shift_2_7: | |
1140 | case ISO_single_shift_2: | |
d46c5b12 KH |
1141 | if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT)) |
1142 | goto label_invalid_code; | |
4ed46869 KH |
1143 | /* SS2 is handled as an escape sequence of ESC 'N' */ |
1144 | c1 = 'N'; | |
1145 | goto label_escape_sequence; | |
1146 | ||
1147 | case ISO_single_shift_3: | |
d46c5b12 KH |
1148 | if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT)) |
1149 | goto label_invalid_code; | |
4ed46869 KH |
1150 | /* SS2 is handled as an escape sequence of ESC 'O' */ |
1151 | c1 = 'O'; | |
1152 | goto label_escape_sequence; | |
1153 | ||
1154 | case ISO_control_sequence_introducer: | |
1155 | /* CSI is handled as an escape sequence of ESC '[' ... */ | |
1156 | c1 = '['; | |
1157 | goto label_escape_sequence; | |
1158 | ||
1159 | case ISO_escape: | |
1160 | ONE_MORE_BYTE (c1); | |
1161 | label_escape_sequence: | |
1162 | /* Escape sequences handled by Emacs are invocation, | |
1163 | designation, direction specification, and character | |
1164 | composition specification. */ | |
1165 | switch (c1) | |
1166 | { | |
1167 | case '&': /* revision of following character set */ | |
1168 | ONE_MORE_BYTE (c1); | |
1169 | if (!(c1 >= '@' && c1 <= '~')) | |
d46c5b12 | 1170 | goto label_invalid_code; |
4ed46869 KH |
1171 | ONE_MORE_BYTE (c1); |
1172 | if (c1 != ISO_CODE_ESC) | |
d46c5b12 | 1173 | goto label_invalid_code; |
4ed46869 KH |
1174 | ONE_MORE_BYTE (c1); |
1175 | goto label_escape_sequence; | |
1176 | ||
1177 | case '$': /* designation of 2-byte character set */ | |
d46c5b12 KH |
1178 | if (! (coding->flags & CODING_FLAG_ISO_DESIGNATION)) |
1179 | goto label_invalid_code; | |
4ed46869 KH |
1180 | ONE_MORE_BYTE (c1); |
1181 | if (c1 >= '@' && c1 <= 'B') | |
1182 | { /* designation of JISX0208.1978, GB2312.1980, | |
88993dfd | 1183 | or JISX0208.1980 */ |
4ed46869 KH |
1184 | DECODE_DESIGNATION (0, 2, 94, c1); |
1185 | } | |
1186 | else if (c1 >= 0x28 && c1 <= 0x2B) | |
1187 | { /* designation of DIMENSION2_CHARS94 character set */ | |
1188 | ONE_MORE_BYTE (c2); | |
1189 | DECODE_DESIGNATION (c1 - 0x28, 2, 94, c2); | |
1190 | } | |
1191 | else if (c1 >= 0x2C && c1 <= 0x2F) | |
1192 | { /* designation of DIMENSION2_CHARS96 character set */ | |
1193 | ONE_MORE_BYTE (c2); | |
1194 | DECODE_DESIGNATION (c1 - 0x2C, 2, 96, c2); | |
1195 | } | |
1196 | else | |
d46c5b12 | 1197 | goto label_invalid_code; |
4ed46869 KH |
1198 | break; |
1199 | ||
1200 | case 'n': /* invocation of locking-shift-2 */ | |
d46c5b12 KH |
1201 | if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT) |
1202 | || CODING_SPEC_ISO_DESIGNATION (coding, 2) < 0) | |
1203 | goto label_invalid_code; | |
4ed46869 | 1204 | CODING_SPEC_ISO_INVOCATION (coding, 0) = 2; |
e0e989f6 | 1205 | charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0); |
4ed46869 KH |
1206 | break; |
1207 | ||
1208 | case 'o': /* invocation of locking-shift-3 */ | |
d46c5b12 KH |
1209 | if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT) |
1210 | || CODING_SPEC_ISO_DESIGNATION (coding, 3) < 0) | |
1211 | goto label_invalid_code; | |
4ed46869 | 1212 | CODING_SPEC_ISO_INVOCATION (coding, 0) = 3; |
e0e989f6 | 1213 | charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0); |
4ed46869 KH |
1214 | break; |
1215 | ||
1216 | case 'N': /* invocation of single-shift-2 */ | |
d46c5b12 KH |
1217 | if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT) |
1218 | || CODING_SPEC_ISO_DESIGNATION (coding, 2) < 0) | |
1219 | goto label_invalid_code; | |
4ed46869 KH |
1220 | ONE_MORE_BYTE (c1); |
1221 | charset = CODING_SPEC_ISO_DESIGNATION (coding, 2); | |
1222 | DECODE_ISO_CHARACTER (charset, c1); | |
1223 | break; | |
1224 | ||
1225 | case 'O': /* invocation of single-shift-3 */ | |
d46c5b12 KH |
1226 | if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT) |
1227 | || CODING_SPEC_ISO_DESIGNATION (coding, 3) < 0) | |
1228 | goto label_invalid_code; | |
4ed46869 KH |
1229 | ONE_MORE_BYTE (c1); |
1230 | charset = CODING_SPEC_ISO_DESIGNATION (coding, 3); | |
1231 | DECODE_ISO_CHARACTER (charset, c1); | |
1232 | break; | |
1233 | ||
d46c5b12 KH |
1234 | case '0': case '2': /* start composing */ |
1235 | /* Before processing composing, we must be sure that all | |
1236 | characters being composed are supported by CODING. | |
88993dfd KH |
1237 | If not, we must give up composing. */ |
1238 | if (check_composing_code (coding, src, src_end) == 0) | |
1239 | { | |
1240 | /* We are looking at a valid composition sequence. */ | |
1241 | coding->composing = (c1 == '0' | |
1242 | ? COMPOSING_NO_RULE_HEAD | |
1243 | : COMPOSING_WITH_RULE_HEAD); | |
1244 | coding->composed_chars = 0; | |
1245 | } | |
1246 | else | |
1247 | { | |
1248 | *dst++ = ISO_CODE_ESC; | |
1249 | *dst++ = c1; | |
1250 | coding->produced_char += 2; | |
1251 | } | |
4ed46869 KH |
1252 | break; |
1253 | ||
1254 | case '1': /* end composing */ | |
88993dfd KH |
1255 | if (!coding->composing) |
1256 | { | |
1257 | *dst++ = ISO_CODE_ESC; | |
1258 | *dst++ = c1; | |
1259 | coding->produced_char += 2; | |
1260 | break; | |
1261 | } | |
1262 | ||
de79a6a5 KH |
1263 | if (coding->composed_chars > 0) |
1264 | { | |
1265 | if (coding->composed_chars == 1) | |
1266 | { | |
1267 | unsigned char *this_char_start = dst; | |
1268 | int this_bytes; | |
1269 | ||
1270 | /* Only one character is in the composing | |
1271 | sequence. Make it a normal character. */ | |
1272 | while (*--this_char_start != LEADING_CODE_COMPOSITION); | |
1273 | dst = (this_char_start | |
1274 | + (coding->composing == COMPOSING_NO_RULE_TAIL | |
1275 | ? 1 : 2)); | |
1276 | *dst -= 0x20; | |
1277 | if (*dst == 0x80) | |
1278 | *++dst &= 0x7F; | |
1279 | this_bytes = BYTES_BY_CHAR_HEAD (*dst); | |
1280 | while (this_bytes--) *this_char_start++ = *dst++; | |
1281 | dst = this_char_start; | |
1282 | } | |
1283 | coding->produced_char++; | |
1284 | } | |
4ed46869 | 1285 | coding->composing = COMPOSING_NO; |
4ed46869 KH |
1286 | break; |
1287 | ||
1288 | case '[': /* specification of direction */ | |
d46c5b12 KH |
1289 | if (coding->flags & CODING_FLAG_ISO_NO_DIRECTION) |
1290 | goto label_invalid_code; | |
4ed46869 | 1291 | /* For the moment, nested direction is not supported. |
d46c5b12 KH |
1292 | So, `coding->mode & CODING_MODE_DIRECTION' zero means |
1293 | left-to-right, and nozero means right-to-left. */ | |
4ed46869 KH |
1294 | ONE_MORE_BYTE (c1); |
1295 | switch (c1) | |
1296 | { | |
1297 | case ']': /* end of the current direction */ | |
d46c5b12 | 1298 | coding->mode &= ~CODING_MODE_DIRECTION; |
4ed46869 KH |
1299 | |
1300 | case '0': /* end of the current direction */ | |
1301 | case '1': /* start of left-to-right direction */ | |
1302 | ONE_MORE_BYTE (c1); | |
1303 | if (c1 == ']') | |
d46c5b12 | 1304 | coding->mode &= ~CODING_MODE_DIRECTION; |
4ed46869 | 1305 | else |
d46c5b12 | 1306 | goto label_invalid_code; |
4ed46869 KH |
1307 | break; |
1308 | ||
1309 | case '2': /* start of right-to-left direction */ | |
1310 | ONE_MORE_BYTE (c1); | |
1311 | if (c1 == ']') | |
d46c5b12 | 1312 | coding->mode |= CODING_MODE_DIRECTION; |
4ed46869 | 1313 | else |
d46c5b12 | 1314 | goto label_invalid_code; |
4ed46869 KH |
1315 | break; |
1316 | ||
1317 | default: | |
d46c5b12 | 1318 | goto label_invalid_code; |
4ed46869 KH |
1319 | } |
1320 | break; | |
1321 | ||
1322 | default: | |
d46c5b12 KH |
1323 | if (! (coding->flags & CODING_FLAG_ISO_DESIGNATION)) |
1324 | goto label_invalid_code; | |
4ed46869 KH |
1325 | if (c1 >= 0x28 && c1 <= 0x2B) |
1326 | { /* designation of DIMENSION1_CHARS94 character set */ | |
1327 | ONE_MORE_BYTE (c2); | |
1328 | DECODE_DESIGNATION (c1 - 0x28, 1, 94, c2); | |
1329 | } | |
1330 | else if (c1 >= 0x2C && c1 <= 0x2F) | |
1331 | { /* designation of DIMENSION1_CHARS96 character set */ | |
1332 | ONE_MORE_BYTE (c2); | |
1333 | DECODE_DESIGNATION (c1 - 0x2C, 1, 96, c2); | |
1334 | } | |
1335 | else | |
1336 | { | |
d46c5b12 | 1337 | goto label_invalid_code; |
4ed46869 KH |
1338 | } |
1339 | } | |
1340 | /* We must update these variables now. */ | |
1341 | charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0); | |
1342 | charset1 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 1); | |
1343 | break; | |
1344 | ||
d46c5b12 | 1345 | label_invalid_code: |
d46c5b12 KH |
1346 | while (src_base < src) |
1347 | *dst++ = *src_base++; | |
fb88bf2d | 1348 | coding->fake_multibyte = 1; |
4ed46869 KH |
1349 | } |
1350 | continue; | |
1351 | ||
1352 | label_end_of_loop: | |
d46c5b12 KH |
1353 | result = CODING_FINISH_INSUFFICIENT_SRC; |
1354 | label_end_of_loop_2: | |
4ed46869 KH |
1355 | src = src_base; |
1356 | break; | |
1357 | } | |
1358 | ||
fb88bf2d | 1359 | if (src < src_end) |
4ed46869 | 1360 | { |
fb88bf2d KH |
1361 | if (result == CODING_FINISH_NORMAL) |
1362 | result = CODING_FINISH_INSUFFICIENT_DST; | |
1363 | else if (result != CODING_FINISH_INCONSISTENT_EOL | |
1364 | && coding->mode & CODING_MODE_LAST_BLOCK) | |
1365 | { | |
1366 | /* This is the last block of the text to be decoded. We had | |
1367 | better just flush out all remaining codes in the text | |
1368 | although they are not valid characters. */ | |
1369 | src_bytes = src_end - src; | |
1370 | if (dst_bytes && (dst_end - dst < src_bytes)) | |
1371 | src_bytes = dst_end - dst; | |
1372 | bcopy (src, dst, src_bytes); | |
1373 | dst += src_bytes; | |
1374 | src += src_bytes; | |
1375 | coding->fake_multibyte = 1; | |
1376 | } | |
4ed46869 | 1377 | } |
fb88bf2d | 1378 | |
d46c5b12 KH |
1379 | coding->consumed = coding->consumed_char = src - source; |
1380 | coding->produced = dst - destination; | |
1381 | return result; | |
4ed46869 KH |
1382 | } |
1383 | ||
f4dee582 | 1384 | /* ISO2022 encoding stuff. */ |
4ed46869 KH |
1385 | |
1386 | /* | |
f4dee582 | 1387 | It is not enough to say just "ISO2022" on encoding, we have to |
d46c5b12 | 1388 | specify more details. In Emacs, each coding system of ISO2022 |
4ed46869 KH |
1389 | variant has the following specifications: |
1390 | 1. Initial designation to G0 thru G3. | |
1391 | 2. Allows short-form designation? | |
1392 | 3. ASCII should be designated to G0 before control characters? | |
1393 | 4. ASCII should be designated to G0 at end of line? | |
1394 | 5. 7-bit environment or 8-bit environment? | |
1395 | 6. Use locking-shift? | |
1396 | 7. Use Single-shift? | |
1397 | And the following two are only for Japanese: | |
1398 | 8. Use ASCII in place of JIS0201-1976-Roman? | |
1399 | 9. Use JISX0208-1983 in place of JISX0208-1978? | |
1400 | These specifications are encoded in `coding->flags' as flag bits | |
1401 | defined by macros CODING_FLAG_ISO_XXX. See `coding.h' for more | |
f4dee582 | 1402 | details. |
4ed46869 KH |
1403 | */ |
1404 | ||
1405 | /* Produce codes (escape sequence) for designating CHARSET to graphic | |
1406 | register REG. If <final-char> of CHARSET is '@', 'A', or 'B' and | |
1407 | the coding system CODING allows, produce designation sequence of | |
1408 | short-form. */ | |
1409 | ||
1410 | #define ENCODE_DESIGNATION(charset, reg, coding) \ | |
1411 | do { \ | |
1412 | unsigned char final_char = CHARSET_ISO_FINAL_CHAR (charset); \ | |
1413 | char *intermediate_char_94 = "()*+"; \ | |
1414 | char *intermediate_char_96 = ",-./"; \ | |
70c22245 KH |
1415 | int revision = CODING_SPEC_ISO_REVISION_NUMBER(coding, charset); \ |
1416 | if (revision < 255) \ | |
1417 | { \ | |
4ed46869 KH |
1418 | *dst++ = ISO_CODE_ESC; \ |
1419 | *dst++ = '&'; \ | |
70c22245 | 1420 | *dst++ = '@' + revision; \ |
4ed46869 KH |
1421 | } \ |
1422 | *dst++ = ISO_CODE_ESC; \ | |
1423 | if (CHARSET_DIMENSION (charset) == 1) \ | |
1424 | { \ | |
1425 | if (CHARSET_CHARS (charset) == 94) \ | |
1426 | *dst++ = (unsigned char) (intermediate_char_94[reg]); \ | |
1427 | else \ | |
1428 | *dst++ = (unsigned char) (intermediate_char_96[reg]); \ | |
1429 | } \ | |
1430 | else \ | |
1431 | { \ | |
1432 | *dst++ = '$'; \ | |
1433 | if (CHARSET_CHARS (charset) == 94) \ | |
1434 | { \ | |
1435 | if (! (coding->flags & CODING_FLAG_ISO_SHORT_FORM) \ | |
1436 | || reg != 0 \ | |
1437 | || final_char < '@' || final_char > 'B') \ | |
1438 | *dst++ = (unsigned char) (intermediate_char_94[reg]); \ | |
1439 | } \ | |
1440 | else \ | |
1441 | *dst++ = (unsigned char) (intermediate_char_96[reg]); \ | |
1442 | } \ | |
1443 | *dst++ = final_char; \ | |
1444 | CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \ | |
1445 | } while (0) | |
1446 | ||
1447 | /* The following two macros produce codes (control character or escape | |
1448 | sequence) for ISO2022 single-shift functions (single-shift-2 and | |
1449 | single-shift-3). */ | |
1450 | ||
1451 | #define ENCODE_SINGLE_SHIFT_2 \ | |
1452 | do { \ | |
1453 | if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \ | |
1454 | *dst++ = ISO_CODE_ESC, *dst++ = 'N'; \ | |
1455 | else \ | |
fb88bf2d KH |
1456 | { \ |
1457 | *dst++ = ISO_CODE_SS2; \ | |
1458 | coding->fake_multibyte = 1; \ | |
1459 | } \ | |
4ed46869 KH |
1460 | CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \ |
1461 | } while (0) | |
1462 | ||
fb88bf2d KH |
1463 | #define ENCODE_SINGLE_SHIFT_3 \ |
1464 | do { \ | |
4ed46869 | 1465 | if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \ |
fb88bf2d KH |
1466 | *dst++ = ISO_CODE_ESC, *dst++ = 'O'; \ |
1467 | else \ | |
1468 | { \ | |
1469 | *dst++ = ISO_CODE_SS3; \ | |
1470 | coding->fake_multibyte = 1; \ | |
1471 | } \ | |
4ed46869 KH |
1472 | CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \ |
1473 | } while (0) | |
1474 | ||
1475 | /* The following four macros produce codes (control character or | |
1476 | escape sequence) for ISO2022 locking-shift functions (shift-in, | |
1477 | shift-out, locking-shift-2, and locking-shift-3). */ | |
1478 | ||
1479 | #define ENCODE_SHIFT_IN \ | |
1480 | do { \ | |
1481 | *dst++ = ISO_CODE_SI; \ | |
1482 | CODING_SPEC_ISO_INVOCATION (coding, 0) = 0; \ | |
1483 | } while (0) | |
1484 | ||
1485 | #define ENCODE_SHIFT_OUT \ | |
1486 | do { \ | |
1487 | *dst++ = ISO_CODE_SO; \ | |
1488 | CODING_SPEC_ISO_INVOCATION (coding, 0) = 1; \ | |
1489 | } while (0) | |
1490 | ||
1491 | #define ENCODE_LOCKING_SHIFT_2 \ | |
1492 | do { \ | |
1493 | *dst++ = ISO_CODE_ESC, *dst++ = 'n'; \ | |
1494 | CODING_SPEC_ISO_INVOCATION (coding, 0) = 2; \ | |
1495 | } while (0) | |
1496 | ||
1497 | #define ENCODE_LOCKING_SHIFT_3 \ | |
1498 | do { \ | |
1499 | *dst++ = ISO_CODE_ESC, *dst++ = 'o'; \ | |
1500 | CODING_SPEC_ISO_INVOCATION (coding, 0) = 3; \ | |
1501 | } while (0) | |
1502 | ||
f4dee582 RS |
1503 | /* Produce codes for a DIMENSION1 character whose character set is |
1504 | CHARSET and whose position-code is C1. Designation and invocation | |
4ed46869 KH |
1505 | sequences are also produced in advance if necessary. */ |
1506 | ||
1507 | ||
6e85d753 KH |
1508 | #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \ |
1509 | do { \ | |
1510 | if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \ | |
1511 | { \ | |
1512 | if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \ | |
1513 | *dst++ = c1 & 0x7F; \ | |
1514 | else \ | |
1515 | *dst++ = c1 | 0x80; \ | |
1516 | CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \ | |
1517 | break; \ | |
1518 | } \ | |
1519 | else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \ | |
1520 | { \ | |
1521 | *dst++ = c1 & 0x7F; \ | |
1522 | break; \ | |
1523 | } \ | |
1524 | else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \ | |
1525 | { \ | |
1526 | *dst++ = c1 | 0x80; \ | |
1527 | break; \ | |
1528 | } \ | |
1529 | else if (coding->flags & CODING_FLAG_ISO_SAFE \ | |
70c22245 | 1530 | && !coding->safe_charsets[charset]) \ |
6e85d753 KH |
1531 | { \ |
1532 | /* We should not encode this character, instead produce one or \ | |
1533 | two `?'s. */ \ | |
1534 | *dst++ = CODING_INHIBIT_CHARACTER_SUBSTITUTION; \ | |
1535 | if (CHARSET_WIDTH (charset) == 2) \ | |
1536 | *dst++ = CODING_INHIBIT_CHARACTER_SUBSTITUTION; \ | |
1537 | break; \ | |
1538 | } \ | |
1539 | else \ | |
1540 | /* Since CHARSET is not yet invoked to any graphic planes, we \ | |
1541 | must invoke it, or, at first, designate it to some graphic \ | |
1542 | register. Then repeat the loop to actually produce the \ | |
1543 | character. */ \ | |
1544 | dst = encode_invocation_designation (charset, coding, dst); \ | |
4ed46869 KH |
1545 | } while (1) |
1546 | ||
f4dee582 RS |
1547 | /* Produce codes for a DIMENSION2 character whose character set is |
1548 | CHARSET and whose position-codes are C1 and C2. Designation and | |
4ed46869 KH |
1549 | invocation codes are also produced in advance if necessary. */ |
1550 | ||
6e85d753 KH |
1551 | #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \ |
1552 | do { \ | |
1553 | if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \ | |
1554 | { \ | |
1555 | if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \ | |
1556 | *dst++ = c1 & 0x7F, *dst++ = c2 & 0x7F; \ | |
1557 | else \ | |
1558 | *dst++ = c1 | 0x80, *dst++ = c2 | 0x80; \ | |
1559 | CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \ | |
1560 | break; \ | |
1561 | } \ | |
1562 | else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \ | |
1563 | { \ | |
1564 | *dst++ = c1 & 0x7F, *dst++= c2 & 0x7F; \ | |
1565 | break; \ | |
1566 | } \ | |
1567 | else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \ | |
1568 | { \ | |
1569 | *dst++ = c1 | 0x80, *dst++= c2 | 0x80; \ | |
1570 | break; \ | |
1571 | } \ | |
1572 | else if (coding->flags & CODING_FLAG_ISO_SAFE \ | |
70c22245 | 1573 | && !coding->safe_charsets[charset]) \ |
6e85d753 KH |
1574 | { \ |
1575 | /* We should not encode this character, instead produce one or \ | |
1576 | two `?'s. */ \ | |
1577 | *dst++ = CODING_INHIBIT_CHARACTER_SUBSTITUTION; \ | |
1578 | if (CHARSET_WIDTH (charset) == 2) \ | |
1579 | *dst++ = CODING_INHIBIT_CHARACTER_SUBSTITUTION; \ | |
1580 | break; \ | |
1581 | } \ | |
1582 | else \ | |
1583 | /* Since CHARSET is not yet invoked to any graphic planes, we \ | |
1584 | must invoke it, or, at first, designate it to some graphic \ | |
1585 | register. Then repeat the loop to actually produce the \ | |
1586 | character. */ \ | |
1587 | dst = encode_invocation_designation (charset, coding, dst); \ | |
4ed46869 KH |
1588 | } while (1) |
1589 | ||
84fbb8a0 KH |
1590 | #define ENCODE_ISO_CHARACTER(charset, c1, c2) \ |
1591 | do { \ | |
1592 | int c_alt, charset_alt; \ | |
1593 | if (!NILP (translation_table) \ | |
1594 | && ((c_alt = translate_char (translation_table, -1, \ | |
1595 | charset, c1, c2)) \ | |
1596 | >= 0)) \ | |
1597 | SPLIT_CHAR (c_alt, charset_alt, c1, c2); \ | |
1598 | else \ | |
1599 | charset_alt = charset; \ | |
1600 | if (CHARSET_DIMENSION (charset_alt) == 1) \ | |
1601 | { \ | |
1602 | if (charset == CHARSET_ASCII \ | |
1603 | && coding->flags & CODING_FLAG_ISO_USE_ROMAN) \ | |
1604 | charset_alt = charset_latin_jisx0201; \ | |
1605 | ENCODE_ISO_CHARACTER_DIMENSION1 (charset_alt, c1); \ | |
1606 | } \ | |
1607 | else \ | |
1608 | { \ | |
1609 | if (charset == charset_jisx0208 \ | |
1610 | && coding->flags & CODING_FLAG_ISO_USE_OLDJIS) \ | |
1611 | charset_alt = charset_jisx0208_1978; \ | |
1612 | ENCODE_ISO_CHARACTER_DIMENSION2 (charset_alt, c1, c2); \ | |
1613 | } \ | |
1614 | if (! COMPOSING_P (coding->composing)) \ | |
1615 | coding->consumed_char++; \ | |
1616 | } while (0) | |
bdd9fb48 | 1617 | |
4ed46869 KH |
1618 | /* Produce designation and invocation codes at a place pointed by DST |
1619 | to use CHARSET. The element `spec.iso2022' of *CODING is updated. | |
1620 | Return new DST. */ | |
1621 | ||
1622 | unsigned char * | |
1623 | encode_invocation_designation (charset, coding, dst) | |
1624 | int charset; | |
1625 | struct coding_system *coding; | |
1626 | unsigned char *dst; | |
1627 | { | |
1628 | int reg; /* graphic register number */ | |
1629 | ||
1630 | /* At first, check designations. */ | |
1631 | for (reg = 0; reg < 4; reg++) | |
1632 | if (charset == CODING_SPEC_ISO_DESIGNATION (coding, reg)) | |
1633 | break; | |
1634 | ||
1635 | if (reg >= 4) | |
1636 | { | |
1637 | /* CHARSET is not yet designated to any graphic registers. */ | |
1638 | /* At first check the requested designation. */ | |
1639 | reg = CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset); | |
1ba9e4ab KH |
1640 | if (reg == CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION) |
1641 | /* Since CHARSET requests no special designation, designate it | |
1642 | to graphic register 0. */ | |
4ed46869 KH |
1643 | reg = 0; |
1644 | ||
1645 | ENCODE_DESIGNATION (charset, reg, coding); | |
1646 | } | |
1647 | ||
1648 | if (CODING_SPEC_ISO_INVOCATION (coding, 0) != reg | |
1649 | && CODING_SPEC_ISO_INVOCATION (coding, 1) != reg) | |
1650 | { | |
1651 | /* Since the graphic register REG is not invoked to any graphic | |
1652 | planes, invoke it to graphic plane 0. */ | |
1653 | switch (reg) | |
1654 | { | |
1655 | case 0: /* graphic register 0 */ | |
1656 | ENCODE_SHIFT_IN; | |
1657 | break; | |
1658 | ||
1659 | case 1: /* graphic register 1 */ | |
1660 | ENCODE_SHIFT_OUT; | |
1661 | break; | |
1662 | ||
1663 | case 2: /* graphic register 2 */ | |
1664 | if (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT) | |
1665 | ENCODE_SINGLE_SHIFT_2; | |
1666 | else | |
1667 | ENCODE_LOCKING_SHIFT_2; | |
1668 | break; | |
1669 | ||
1670 | case 3: /* graphic register 3 */ | |
1671 | if (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT) | |
1672 | ENCODE_SINGLE_SHIFT_3; | |
1673 | else | |
1674 | ENCODE_LOCKING_SHIFT_3; | |
1675 | break; | |
1676 | } | |
1677 | } | |
1678 | return dst; | |
1679 | } | |
1680 | ||
1681 | /* The following two macros produce codes for indicating composition. */ | |
1682 | #define ENCODE_COMPOSITION_NO_RULE_START *dst++ = ISO_CODE_ESC, *dst++ = '0' | |
1683 | #define ENCODE_COMPOSITION_WITH_RULE_START *dst++ = ISO_CODE_ESC, *dst++ = '2' | |
1684 | #define ENCODE_COMPOSITION_END *dst++ = ISO_CODE_ESC, *dst++ = '1' | |
1685 | ||
1686 | /* The following three macros produce codes for indicating direction | |
1687 | of text. */ | |
1688 | #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \ | |
1689 | do { \ | |
1690 | if (coding->flags == CODING_FLAG_ISO_SEVEN_BITS) \ | |
1691 | *dst++ = ISO_CODE_ESC, *dst++ = '['; \ | |
1692 | else \ | |
1693 | *dst++ = ISO_CODE_CSI; \ | |
1694 | } while (0) | |
1695 | ||
1696 | #define ENCODE_DIRECTION_R2L \ | |
1697 | ENCODE_CONTROL_SEQUENCE_INTRODUCER, *dst++ = '2', *dst++ = ']' | |
1698 | ||
1699 | #define ENCODE_DIRECTION_L2R \ | |
1700 | ENCODE_CONTROL_SEQUENCE_INTRODUCER, *dst++ = '0', *dst++ = ']' | |
1701 | ||
1702 | /* Produce codes for designation and invocation to reset the graphic | |
1703 | planes and registers to initial state. */ | |
e0e989f6 KH |
1704 | #define ENCODE_RESET_PLANE_AND_REGISTER \ |
1705 | do { \ | |
1706 | int reg; \ | |
1707 | if (CODING_SPEC_ISO_INVOCATION (coding, 0) != 0) \ | |
1708 | ENCODE_SHIFT_IN; \ | |
1709 | for (reg = 0; reg < 4; reg++) \ | |
1710 | if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg) >= 0 \ | |
1711 | && (CODING_SPEC_ISO_DESIGNATION (coding, reg) \ | |
1712 | != CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg))) \ | |
1713 | ENCODE_DESIGNATION \ | |
1714 | (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg), reg, coding); \ | |
4ed46869 KH |
1715 | } while (0) |
1716 | ||
bdd9fb48 | 1717 | /* Produce designation sequences of charsets in the line started from |
d46c5b12 | 1718 | SRC to a place pointed by *DSTP, and update DSTP. |
bdd9fb48 KH |
1719 | |
1720 | If the current block ends before any end-of-line, we may fail to | |
d46c5b12 KH |
1721 | find all the necessary designations. */ |
1722 | ||
dfcf069d | 1723 | void |
bdd9fb48 | 1724 | encode_designation_at_bol (coding, table, src, src_end, dstp) |
e0e989f6 | 1725 | struct coding_system *coding; |
bdd9fb48 | 1726 | Lisp_Object table; |
e0e989f6 KH |
1727 | unsigned char *src, *src_end, **dstp; |
1728 | { | |
bdd9fb48 KH |
1729 | int charset, c, found = 0, reg; |
1730 | /* Table of charsets to be designated to each graphic register. */ | |
1731 | int r[4]; | |
1732 | unsigned char *dst = *dstp; | |
1733 | ||
1734 | for (reg = 0; reg < 4; reg++) | |
1735 | r[reg] = -1; | |
1736 | ||
1737 | while (src < src_end && *src != '\n' && found < 4) | |
e0e989f6 | 1738 | { |
bdd9fb48 KH |
1739 | int bytes = BYTES_BY_CHAR_HEAD (*src); |
1740 | ||
1741 | if (NILP (table)) | |
1742 | charset = CHARSET_AT (src); | |
1743 | else | |
e0e989f6 | 1744 | { |
35cb8686 RS |
1745 | int c_alt; |
1746 | unsigned char c1, c2; | |
bdd9fb48 KH |
1747 | |
1748 | SPLIT_STRING(src, bytes, charset, c1, c2); | |
84fbb8a0 | 1749 | if ((c_alt = translate_char (table, -1, charset, c1, c2)) >= 0) |
bdd9fb48 | 1750 | charset = CHAR_CHARSET (c_alt); |
e0e989f6 | 1751 | } |
bdd9fb48 | 1752 | |
e0e989f6 | 1753 | reg = CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset); |
d46c5b12 | 1754 | if (reg != CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION && r[reg] < 0) |
bdd9fb48 KH |
1755 | { |
1756 | found++; | |
1757 | r[reg] = charset; | |
1758 | } | |
1759 | ||
1760 | src += bytes; | |
1761 | } | |
1762 | ||
1763 | if (found) | |
1764 | { | |
1765 | for (reg = 0; reg < 4; reg++) | |
1766 | if (r[reg] >= 0 | |
1767 | && CODING_SPEC_ISO_DESIGNATION (coding, reg) != r[reg]) | |
1768 | ENCODE_DESIGNATION (r[reg], reg, coding); | |
1769 | *dstp = dst; | |
e0e989f6 | 1770 | } |
e0e989f6 KH |
1771 | } |
1772 | ||
4ed46869 KH |
1773 | /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */ |
1774 | ||
1775 | int | |
d46c5b12 | 1776 | encode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes) |
4ed46869 KH |
1777 | struct coding_system *coding; |
1778 | unsigned char *source, *destination; | |
1779 | int src_bytes, dst_bytes; | |
4ed46869 KH |
1780 | { |
1781 | unsigned char *src = source; | |
1782 | unsigned char *src_end = source + src_bytes; | |
1783 | unsigned char *dst = destination; | |
1784 | unsigned char *dst_end = destination + dst_bytes; | |
e0e989f6 | 1785 | /* Since the maximum bytes produced by each loop is 20, we subtract 19 |
4ed46869 KH |
1786 | from DST_END to assure overflow checking is necessary only at the |
1787 | head of loop. */ | |
e0e989f6 | 1788 | unsigned char *adjusted_dst_end = dst_end - 19; |
84fbb8a0 | 1789 | Lisp_Object translation_table |
f967223b | 1790 | = coding->translation_table_for_encode; |
d46c5b12 | 1791 | int result = CODING_FINISH_NORMAL; |
bdd9fb48 | 1792 | |
84fbb8a0 | 1793 | if (!NILP (Venable_character_translation) && NILP (translation_table)) |
f967223b | 1794 | translation_table = Vstandard_translation_table_for_encode; |
4ed46869 | 1795 | |
d46c5b12 | 1796 | coding->consumed_char = 0; |
fb88bf2d | 1797 | coding->fake_multibyte = 0; |
d46c5b12 KH |
1798 | while (src < src_end && (dst_bytes |
1799 | ? (dst < adjusted_dst_end) | |
1800 | : (dst < src - 19))) | |
4ed46869 KH |
1801 | { |
1802 | /* SRC_BASE remembers the start position in source in each loop. | |
1803 | The loop will be exited when there's not enough source text | |
1804 | to analyze multi-byte codes (within macros ONE_MORE_BYTE, | |
1805 | TWO_MORE_BYTES, and THREE_MORE_BYTES). In that case, SRC is | |
1806 | reset to SRC_BASE before exiting. */ | |
1807 | unsigned char *src_base = src; | |
bdd9fb48 | 1808 | int charset, c1, c2, c3, c4; |
4ed46869 | 1809 | |
e0e989f6 KH |
1810 | if (coding->flags & CODING_FLAG_ISO_DESIGNATE_AT_BOL |
1811 | && CODING_SPEC_ISO_BOL (coding)) | |
1812 | { | |
bdd9fb48 | 1813 | /* We have to produce designation sequences if any now. */ |
84fbb8a0 | 1814 | encode_designation_at_bol (coding, translation_table, |
bdd9fb48 | 1815 | src, src_end, &dst); |
e0e989f6 KH |
1816 | CODING_SPEC_ISO_BOL (coding) = 0; |
1817 | } | |
1818 | ||
1819 | c1 = *src++; | |
4ed46869 | 1820 | /* If we are seeing a component of a composite character, we are |
d46c5b12 KH |
1821 | seeing a leading-code encoded irregularly for composition, or |
1822 | a composition rule if composing with rule. We must set C1 to | |
1823 | a normal leading-code or an ASCII code. If we are not seeing | |
1824 | a composite character, we must reset composition, | |
1825 | designation, and invocation states. */ | |
4ed46869 KH |
1826 | if (COMPOSING_P (coding->composing)) |
1827 | { | |
1828 | if (c1 < 0xA0) | |
1829 | { | |
1830 | /* We are not in a composite character any longer. */ | |
1831 | coding->composing = COMPOSING_NO; | |
d46c5b12 | 1832 | ENCODE_RESET_PLANE_AND_REGISTER; |
4ed46869 KH |
1833 | ENCODE_COMPOSITION_END; |
1834 | } | |
1835 | else | |
1836 | { | |
1837 | if (coding->composing == COMPOSING_WITH_RULE_RULE) | |
1838 | { | |
1839 | *dst++ = c1 & 0x7F; | |
1840 | coding->composing = COMPOSING_WITH_RULE_HEAD; | |
1841 | continue; | |
1842 | } | |
1843 | else if (coding->composing == COMPOSING_WITH_RULE_HEAD) | |
1844 | coding->composing = COMPOSING_WITH_RULE_RULE; | |
1845 | if (c1 == 0xA0) | |
1846 | { | |
1847 | /* This is an ASCII component. */ | |
1848 | ONE_MORE_BYTE (c1); | |
1849 | c1 &= 0x7F; | |
1850 | } | |
1851 | else | |
1852 | /* This is a leading-code of non ASCII component. */ | |
1853 | c1 -= 0x20; | |
1854 | } | |
1855 | } | |
1856 | ||
1857 | /* Now encode one character. C1 is a control character, an | |
1858 | ASCII character, or a leading-code of multi-byte character. */ | |
1859 | switch (emacs_code_class[c1]) | |
1860 | { | |
1861 | case EMACS_ascii_code: | |
bdd9fb48 | 1862 | ENCODE_ISO_CHARACTER (CHARSET_ASCII, c1, /* dummy */ c2); |
4ed46869 KH |
1863 | break; |
1864 | ||
1865 | case EMACS_control_code: | |
1866 | if (coding->flags & CODING_FLAG_ISO_RESET_AT_CNTL) | |
e0e989f6 | 1867 | ENCODE_RESET_PLANE_AND_REGISTER; |
4ed46869 | 1868 | *dst++ = c1; |
d46c5b12 | 1869 | coding->consumed_char++; |
4ed46869 KH |
1870 | break; |
1871 | ||
1872 | case EMACS_carriage_return_code: | |
d46c5b12 | 1873 | if (! (coding->mode & CODING_MODE_SELECTIVE_DISPLAY)) |
4ed46869 KH |
1874 | { |
1875 | if (coding->flags & CODING_FLAG_ISO_RESET_AT_CNTL) | |
e0e989f6 | 1876 | ENCODE_RESET_PLANE_AND_REGISTER; |
4ed46869 | 1877 | *dst++ = c1; |
d46c5b12 | 1878 | coding->consumed_char++; |
4ed46869 KH |
1879 | break; |
1880 | } | |
1881 | /* fall down to treat '\r' as '\n' ... */ | |
1882 | ||
1883 | case EMACS_linefeed_code: | |
1884 | if (coding->flags & CODING_FLAG_ISO_RESET_AT_EOL) | |
e0e989f6 KH |
1885 | ENCODE_RESET_PLANE_AND_REGISTER; |
1886 | if (coding->flags & CODING_FLAG_ISO_INIT_AT_BOL) | |
1887 | bcopy (coding->spec.iso2022.initial_designation, | |
1888 | coding->spec.iso2022.current_designation, | |
1889 | sizeof coding->spec.iso2022.initial_designation); | |
4ed46869 | 1890 | if (coding->eol_type == CODING_EOL_LF |
0ef69138 | 1891 | || coding->eol_type == CODING_EOL_UNDECIDED) |
4ed46869 KH |
1892 | *dst++ = ISO_CODE_LF; |
1893 | else if (coding->eol_type == CODING_EOL_CRLF) | |
1894 | *dst++ = ISO_CODE_CR, *dst++ = ISO_CODE_LF; | |
1895 | else | |
1896 | *dst++ = ISO_CODE_CR; | |
e0e989f6 | 1897 | CODING_SPEC_ISO_BOL (coding) = 1; |
d46c5b12 | 1898 | coding->consumed_char++; |
4ed46869 KH |
1899 | break; |
1900 | ||
1901 | case EMACS_leading_code_2: | |
1902 | ONE_MORE_BYTE (c2); | |
19a8d9e0 KH |
1903 | if (c2 < 0xA0) |
1904 | { | |
1905 | /* invalid sequence */ | |
1906 | *dst++ = c1; | |
38cf95df RS |
1907 | src--; |
1908 | coding->consumed_char++; | |
19a8d9e0 KH |
1909 | } |
1910 | else | |
1911 | ENCODE_ISO_CHARACTER (c1, c2, /* dummy */ c3); | |
4ed46869 KH |
1912 | break; |
1913 | ||
1914 | case EMACS_leading_code_3: | |
1915 | TWO_MORE_BYTES (c2, c3); | |
19a8d9e0 KH |
1916 | if (c2 < 0xA0 || c3 < 0xA0) |
1917 | { | |
1918 | /* invalid sequence */ | |
1919 | *dst++ = c1; | |
38cf95df RS |
1920 | src -= 2; |
1921 | coding->consumed_char++; | |
19a8d9e0 KH |
1922 | } |
1923 | else if (c1 < LEADING_CODE_PRIVATE_11) | |
bdd9fb48 | 1924 | ENCODE_ISO_CHARACTER (c1, c2, c3); |
4ed46869 | 1925 | else |
bdd9fb48 | 1926 | ENCODE_ISO_CHARACTER (c2, c3, /* dummy */ c4); |
4ed46869 KH |
1927 | break; |
1928 | ||
1929 | case EMACS_leading_code_4: | |
1930 | THREE_MORE_BYTES (c2, c3, c4); | |
19a8d9e0 KH |
1931 | if (c2 < 0xA0 || c3 < 0xA0 || c4 < 0xA0) |
1932 | { | |
1933 | /* invalid sequence */ | |
1934 | *dst++ = c1; | |
38cf95df RS |
1935 | src -= 3; |
1936 | coding->consumed_char++; | |
19a8d9e0 KH |
1937 | } |
1938 | else | |
1939 | ENCODE_ISO_CHARACTER (c2, c3, c4); | |
4ed46869 KH |
1940 | break; |
1941 | ||
1942 | case EMACS_leading_code_composition: | |
19a8d9e0 KH |
1943 | ONE_MORE_BYTE (c2); |
1944 | if (c2 < 0xA0) | |
1945 | { | |
1946 | /* invalid sequence */ | |
1947 | *dst++ = c1; | |
38cf95df RS |
1948 | src--; |
1949 | coding->consumed_char++; | |
19a8d9e0 KH |
1950 | } |
1951 | else if (c2 == 0xFF) | |
4ed46869 | 1952 | { |
d46c5b12 | 1953 | ENCODE_RESET_PLANE_AND_REGISTER; |
4ed46869 KH |
1954 | coding->composing = COMPOSING_WITH_RULE_HEAD; |
1955 | ENCODE_COMPOSITION_WITH_RULE_START; | |
d46c5b12 | 1956 | coding->consumed_char++; |
4ed46869 KH |
1957 | } |
1958 | else | |
1959 | { | |
d46c5b12 | 1960 | ENCODE_RESET_PLANE_AND_REGISTER; |
4ed46869 KH |
1961 | /* Rewind one byte because it is a character code of |
1962 | composition elements. */ | |
1963 | src--; | |
1964 | coding->composing = COMPOSING_NO_RULE_HEAD; | |
1965 | ENCODE_COMPOSITION_NO_RULE_START; | |
d46c5b12 | 1966 | coding->consumed_char++; |
4ed46869 KH |
1967 | } |
1968 | break; | |
1969 | ||
1970 | case EMACS_invalid_code: | |
1971 | *dst++ = c1; | |
d46c5b12 | 1972 | coding->consumed_char++; |
4ed46869 KH |
1973 | break; |
1974 | } | |
1975 | continue; | |
1976 | label_end_of_loop: | |
d46c5b12 KH |
1977 | result = CODING_FINISH_INSUFFICIENT_SRC; |
1978 | src = src_base; | |
4ed46869 KH |
1979 | break; |
1980 | } | |
1981 | ||
49cb52b4 KH |
1982 | if (src < src_end && result == CODING_FINISH_NORMAL) |
1983 | result = CODING_FINISH_INSUFFICIENT_DST; | |
1984 | ||
1985 | /* If this is the last block of the text to be encoded, we must | |
1986 | reset graphic planes and registers to the initial state, and | |
1987 | flush out the carryover if any. */ | |
1988 | if (coding->mode & CODING_MODE_LAST_BLOCK) | |
84fbb8a0 KH |
1989 | { |
1990 | ENCODE_RESET_PLANE_AND_REGISTER; | |
1991 | if (COMPOSING_P (coding->composing)) | |
1992 | ENCODE_COMPOSITION_END; | |
88993dfd KH |
1993 | if (result == CODING_FINISH_INSUFFICIENT_SRC) |
1994 | { | |
1995 | while (src < src_end && dst < dst_end) | |
1996 | *dst++ = *src++; | |
1997 | } | |
84fbb8a0 | 1998 | } |
d46c5b12 KH |
1999 | coding->consumed = src - source; |
2000 | coding->produced = coding->produced_char = dst - destination; | |
2001 | return result; | |
4ed46869 KH |
2002 | } |
2003 | ||
2004 | \f | |
2005 | /*** 4. SJIS and BIG5 handlers ***/ | |
2006 | ||
f4dee582 | 2007 | /* Although SJIS and BIG5 are not ISO's coding system, they are used |
4ed46869 KH |
2008 | quite widely. So, for the moment, Emacs supports them in the bare |
2009 | C code. But, in the future, they may be supported only by CCL. */ | |
2010 | ||
2011 | /* SJIS is a coding system encoding three character sets: ASCII, right | |
2012 | half of JISX0201-Kana, and JISX0208. An ASCII character is encoded | |
2013 | as is. A character of charset katakana-jisx0201 is encoded by | |
2014 | "position-code + 0x80". A character of charset japanese-jisx0208 | |
2015 | is encoded in 2-byte but two position-codes are divided and shifted | |
2016 | so that it fit in the range below. | |
2017 | ||
2018 | --- CODE RANGE of SJIS --- | |
2019 | (character set) (range) | |
2020 | ASCII 0x00 .. 0x7F | |
2021 | KATAKANA-JISX0201 0xA0 .. 0xDF | |
54f78171 | 2022 | JISX0208 (1st byte) 0x80 .. 0x9F and 0xE0 .. 0xEF |
4ed46869 KH |
2023 | (2nd byte) 0x40 .. 0xFF |
2024 | ------------------------------- | |
2025 | ||
2026 | */ | |
2027 | ||
2028 | /* BIG5 is a coding system encoding two character sets: ASCII and | |
2029 | Big5. An ASCII character is encoded as is. Big5 is a two-byte | |
2030 | character set and is encoded in two-byte. | |
2031 | ||
2032 | --- CODE RANGE of BIG5 --- | |
2033 | (character set) (range) | |
2034 | ASCII 0x00 .. 0x7F | |
2035 | Big5 (1st byte) 0xA1 .. 0xFE | |
2036 | (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE | |
2037 | -------------------------- | |
2038 | ||
2039 | Since the number of characters in Big5 is larger than maximum | |
2040 | characters in Emacs' charset (96x96), it can't be handled as one | |
2041 | charset. So, in Emacs, Big5 is divided into two: `charset-big5-1' | |
2042 | and `charset-big5-2'. Both are DIMENSION2 and CHARS94. The former | |
2043 | contains frequently used characters and the latter contains less | |
2044 | frequently used characters. */ | |
2045 | ||
2046 | /* Macros to decode or encode a character of Big5 in BIG5. B1 and B2 | |
2047 | are the 1st and 2nd position-codes of Big5 in BIG5 coding system. | |
2048 | C1 and C2 are the 1st and 2nd position-codes of of Emacs' internal | |
2049 | format. CHARSET is `charset_big5_1' or `charset_big5_2'. */ | |
2050 | ||
2051 | /* Number of Big5 characters which have the same code in 1st byte. */ | |
2052 | #define BIG5_SAME_ROW (0xFF - 0xA1 + 0x7F - 0x40) | |
2053 | ||
2054 | #define DECODE_BIG5(b1, b2, charset, c1, c2) \ | |
2055 | do { \ | |
2056 | unsigned int temp \ | |
2057 | = (b1 - 0xA1) * BIG5_SAME_ROW + b2 - (b2 < 0x7F ? 0x40 : 0x62); \ | |
2058 | if (b1 < 0xC9) \ | |
2059 | charset = charset_big5_1; \ | |
2060 | else \ | |
2061 | { \ | |
2062 | charset = charset_big5_2; \ | |
2063 | temp -= (0xC9 - 0xA1) * BIG5_SAME_ROW; \ | |
2064 | } \ | |
2065 | c1 = temp / (0xFF - 0xA1) + 0x21; \ | |
2066 | c2 = temp % (0xFF - 0xA1) + 0x21; \ | |
2067 | } while (0) | |
2068 | ||
2069 | #define ENCODE_BIG5(charset, c1, c2, b1, b2) \ | |
2070 | do { \ | |
2071 | unsigned int temp = (c1 - 0x21) * (0xFF - 0xA1) + (c2 - 0x21); \ | |
2072 | if (charset == charset_big5_2) \ | |
2073 | temp += BIG5_SAME_ROW * (0xC9 - 0xA1); \ | |
2074 | b1 = temp / BIG5_SAME_ROW + 0xA1; \ | |
2075 | b2 = temp % BIG5_SAME_ROW; \ | |
2076 | b2 += b2 < 0x3F ? 0x40 : 0x62; \ | |
2077 | } while (0) | |
2078 | ||
a5d301df KH |
2079 | #define DECODE_SJIS_BIG5_CHARACTER(charset, c1, c2) \ |
2080 | do { \ | |
2081 | int c_alt, charset_alt = (charset); \ | |
84fbb8a0 KH |
2082 | if (!NILP (translation_table) \ |
2083 | && ((c_alt = translate_char (translation_table, \ | |
2084 | -1, (charset), c1, c2)) >= 0)) \ | |
a5d301df KH |
2085 | SPLIT_CHAR (c_alt, charset_alt, c1, c2); \ |
2086 | if (charset_alt == CHARSET_ASCII || charset_alt < 0) \ | |
2087 | DECODE_CHARACTER_ASCII (c1); \ | |
2088 | else if (CHARSET_DIMENSION (charset_alt) == 1) \ | |
2089 | DECODE_CHARACTER_DIMENSION1 (charset_alt, c1); \ | |
2090 | else \ | |
2091 | DECODE_CHARACTER_DIMENSION2 (charset_alt, c1, c2); \ | |
2092 | } while (0) | |
2093 | ||
84fbb8a0 KH |
2094 | #define ENCODE_SJIS_BIG5_CHARACTER(charset, c1, c2) \ |
2095 | do { \ | |
2096 | int c_alt, charset_alt; \ | |
2097 | if (!NILP (translation_table) \ | |
2098 | && ((c_alt = translate_char (translation_table, -1, \ | |
2099 | charset, c1, c2)) \ | |
2100 | >= 0)) \ | |
2101 | SPLIT_CHAR (c_alt, charset_alt, c1, c2); \ | |
2102 | else \ | |
2103 | charset_alt = charset; \ | |
2104 | if (charset_alt == charset_ascii) \ | |
2105 | *dst++ = c1; \ | |
2106 | else if (CHARSET_DIMENSION (charset_alt) == 1) \ | |
2107 | { \ | |
2108 | if (sjis_p && charset_alt == charset_katakana_jisx0201) \ | |
2109 | *dst++ = c1; \ | |
2110 | else \ | |
2111 | { \ | |
2112 | *dst++ = charset_alt, *dst++ = c1; \ | |
2113 | coding->fake_multibyte = 1; \ | |
2114 | } \ | |
2115 | } \ | |
2116 | else \ | |
2117 | { \ | |
2118 | c1 &= 0x7F, c2 &= 0x7F; \ | |
2119 | if (sjis_p && charset_alt == charset_jisx0208) \ | |
2120 | { \ | |
2121 | unsigned char s1, s2; \ | |
2122 | \ | |
2123 | ENCODE_SJIS (c1, c2, s1, s2); \ | |
2124 | *dst++ = s1, *dst++ = s2; \ | |
2125 | coding->fake_multibyte = 1; \ | |
2126 | } \ | |
2127 | else if (!sjis_p \ | |
2128 | && (charset_alt == charset_big5_1 \ | |
2129 | || charset_alt == charset_big5_2)) \ | |
2130 | { \ | |
2131 | unsigned char b1, b2; \ | |
2132 | \ | |
2133 | ENCODE_BIG5 (charset_alt, c1, c2, b1, b2); \ | |
2134 | *dst++ = b1, *dst++ = b2; \ | |
2135 | } \ | |
2136 | else \ | |
2137 | { \ | |
2138 | *dst++ = charset_alt, *dst++ = c1, *dst++ = c2; \ | |
2139 | coding->fake_multibyte = 1; \ | |
2140 | } \ | |
2141 | } \ | |
2142 | coding->consumed_char++; \ | |
a5d301df KH |
2143 | } while (0); |
2144 | ||
4ed46869 KH |
2145 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". |
2146 | Check if a text is encoded in SJIS. If it is, return | |
2147 | CODING_CATEGORY_MASK_SJIS, else return 0. */ | |
2148 | ||
2149 | int | |
2150 | detect_coding_sjis (src, src_end) | |
2151 | unsigned char *src, *src_end; | |
2152 | { | |
2153 | unsigned char c; | |
2154 | ||
2155 | while (src < src_end) | |
2156 | { | |
2157 | c = *src++; | |
4ed46869 KH |
2158 | if ((c >= 0x80 && c < 0xA0) || c >= 0xE0) |
2159 | { | |
2160 | if (src < src_end && *src++ < 0x40) | |
2161 | return 0; | |
2162 | } | |
2163 | } | |
2164 | return CODING_CATEGORY_MASK_SJIS; | |
2165 | } | |
2166 | ||
2167 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
2168 | Check if a text is encoded in BIG5. If it is, return | |
2169 | CODING_CATEGORY_MASK_BIG5, else return 0. */ | |
2170 | ||
2171 | int | |
2172 | detect_coding_big5 (src, src_end) | |
2173 | unsigned char *src, *src_end; | |
2174 | { | |
2175 | unsigned char c; | |
2176 | ||
2177 | while (src < src_end) | |
2178 | { | |
2179 | c = *src++; | |
4ed46869 KH |
2180 | if (c >= 0xA1) |
2181 | { | |
2182 | if (src >= src_end) | |
2183 | break; | |
2184 | c = *src++; | |
2185 | if (c < 0x40 || (c >= 0x7F && c <= 0xA0)) | |
2186 | return 0; | |
2187 | } | |
2188 | } | |
2189 | return CODING_CATEGORY_MASK_BIG5; | |
2190 | } | |
2191 | ||
2192 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". | |
2193 | If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */ | |
2194 | ||
2195 | int | |
2196 | decode_coding_sjis_big5 (coding, source, destination, | |
d46c5b12 | 2197 | src_bytes, dst_bytes, sjis_p) |
4ed46869 KH |
2198 | struct coding_system *coding; |
2199 | unsigned char *source, *destination; | |
2200 | int src_bytes, dst_bytes; | |
4ed46869 KH |
2201 | int sjis_p; |
2202 | { | |
2203 | unsigned char *src = source; | |
2204 | unsigned char *src_end = source + src_bytes; | |
2205 | unsigned char *dst = destination; | |
2206 | unsigned char *dst_end = destination + dst_bytes; | |
2207 | /* Since the maximum bytes produced by each loop is 4, we subtract 3 | |
2208 | from DST_END to assure overflow checking is necessary only at the | |
2209 | head of loop. */ | |
2210 | unsigned char *adjusted_dst_end = dst_end - 3; | |
84fbb8a0 | 2211 | Lisp_Object translation_table |
f967223b | 2212 | = coding->translation_table_for_decode; |
d46c5b12 | 2213 | int result = CODING_FINISH_NORMAL; |
a5d301df | 2214 | |
84fbb8a0 | 2215 | if (!NILP (Venable_character_translation) && NILP (translation_table)) |
f967223b | 2216 | translation_table = Vstandard_translation_table_for_decode; |
4ed46869 | 2217 | |
d46c5b12 | 2218 | coding->produced_char = 0; |
fb88bf2d | 2219 | coding->fake_multibyte = 0; |
d46c5b12 KH |
2220 | while (src < src_end && (dst_bytes |
2221 | ? (dst < adjusted_dst_end) | |
2222 | : (dst < src - 3))) | |
4ed46869 KH |
2223 | { |
2224 | /* SRC_BASE remembers the start position in source in each loop. | |
2225 | The loop will be exited when there's not enough source text | |
2226 | to analyze two-byte character (within macro ONE_MORE_BYTE). | |
2227 | In that case, SRC is reset to SRC_BASE before exiting. */ | |
2228 | unsigned char *src_base = src; | |
2229 | unsigned char c1 = *src++, c2, c3, c4; | |
2230 | ||
d46c5b12 | 2231 | if (c1 < 0x20) |
4ed46869 | 2232 | { |
d46c5b12 | 2233 | if (c1 == '\r') |
4ed46869 | 2234 | { |
d46c5b12 KH |
2235 | if (coding->eol_type == CODING_EOL_CRLF) |
2236 | { | |
2237 | ONE_MORE_BYTE (c2); | |
2238 | if (c2 == '\n') | |
2239 | *dst++ = c2; | |
2240 | else if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL) | |
2241 | { | |
2242 | result = CODING_FINISH_INCONSISTENT_EOL; | |
2243 | goto label_end_of_loop_2; | |
2244 | } | |
2245 | else | |
2246 | /* To process C2 again, SRC is subtracted by 1. */ | |
2247 | *dst++ = c1, src--; | |
2248 | } | |
2249 | else if (coding->eol_type == CODING_EOL_CR) | |
2250 | *dst++ = '\n'; | |
4ed46869 | 2251 | else |
d46c5b12 KH |
2252 | *dst++ = c1; |
2253 | } | |
2254 | else if (c1 == '\n' | |
2255 | && (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL) | |
2256 | && (coding->eol_type == CODING_EOL_CR | |
2257 | || coding->eol_type == CODING_EOL_CRLF)) | |
2258 | { | |
2259 | result = CODING_FINISH_INCONSISTENT_EOL; | |
2260 | goto label_end_of_loop_2; | |
4ed46869 KH |
2261 | } |
2262 | else | |
2263 | *dst++ = c1; | |
d46c5b12 | 2264 | coding->produced_char++; |
4ed46869 | 2265 | } |
a5d301df KH |
2266 | else if (c1 < 0x80) |
2267 | DECODE_SJIS_BIG5_CHARACTER (charset_ascii, c1, /* dummy */ c2); | |
54f78171 | 2268 | else |
4ed46869 | 2269 | { |
4ed46869 KH |
2270 | if (sjis_p) |
2271 | { | |
54f78171 | 2272 | if (c1 < 0xA0 || (c1 >= 0xE0 && c1 < 0xF0)) |
fb88bf2d | 2273 | { |
54f78171 KH |
2274 | /* SJIS -> JISX0208 */ |
2275 | ONE_MORE_BYTE (c2); | |
2276 | if (c2 >= 0x40) | |
2277 | { | |
2278 | DECODE_SJIS (c1, c2, c3, c4); | |
2279 | DECODE_SJIS_BIG5_CHARACTER (charset_jisx0208, c3, c4); | |
2280 | } | |
2281 | else | |
2282 | goto label_invalid_code_2; | |
fb88bf2d | 2283 | } |
54f78171 KH |
2284 | else if (c1 < 0xE0) |
2285 | /* SJIS -> JISX0201-Kana */ | |
2286 | DECODE_SJIS_BIG5_CHARACTER (charset_katakana_jisx0201, c1, | |
2287 | /* dummy */ c2); | |
fb88bf2d | 2288 | else |
54f78171 | 2289 | goto label_invalid_code_1; |
4ed46869 | 2290 | } |
fb88bf2d | 2291 | else |
fb88bf2d | 2292 | { |
54f78171 KH |
2293 | /* BIG5 -> Big5 */ |
2294 | if (c1 >= 0xA1 && c1 <= 0xFE) | |
fb88bf2d | 2295 | { |
54f78171 KH |
2296 | ONE_MORE_BYTE (c2); |
2297 | if ((c2 >= 0x40 && c2 <= 0x7E) || (c2 >= 0xA1 && c2 <= 0xFE)) | |
2298 | { | |
2299 | int charset; | |
4ed46869 | 2300 | |
54f78171 KH |
2301 | DECODE_BIG5 (c1, c2, charset, c3, c4); |
2302 | DECODE_SJIS_BIG5_CHARACTER (charset, c3, c4); | |
2303 | } | |
2304 | else | |
2305 | goto label_invalid_code_2; | |
fb88bf2d KH |
2306 | } |
2307 | else | |
54f78171 | 2308 | goto label_invalid_code_1; |
4ed46869 KH |
2309 | } |
2310 | } | |
2311 | continue; | |
2312 | ||
fb88bf2d KH |
2313 | label_invalid_code_1: |
2314 | *dst++ = c1; | |
2315 | coding->produced_char++; | |
2316 | coding->fake_multibyte = 1; | |
2317 | continue; | |
2318 | ||
2319 | label_invalid_code_2: | |
2320 | *dst++ = c1; *dst++= c2; | |
2321 | coding->produced_char += 2; | |
2322 | coding->fake_multibyte = 1; | |
2323 | continue; | |
2324 | ||
4ed46869 | 2325 | label_end_of_loop: |
d46c5b12 KH |
2326 | result = CODING_FINISH_INSUFFICIENT_SRC; |
2327 | label_end_of_loop_2: | |
4ed46869 KH |
2328 | src = src_base; |
2329 | break; | |
2330 | } | |
2331 | ||
fb88bf2d KH |
2332 | if (src < src_end) |
2333 | { | |
2334 | if (result == CODING_FINISH_NORMAL) | |
2335 | result = CODING_FINISH_INSUFFICIENT_DST; | |
2336 | else if (result != CODING_FINISH_INCONSISTENT_EOL | |
2337 | && coding->mode & CODING_MODE_LAST_BLOCK) | |
2338 | { | |
2339 | src_bytes = src_end - src; | |
2340 | if (dst_bytes && (dst_end - dst < src_bytes)) | |
2341 | src_bytes = dst_end - dst; | |
2342 | bcopy (dst, src, src_bytes); | |
2343 | src += src_bytes; | |
2344 | dst += src_bytes; | |
2345 | coding->fake_multibyte = 1; | |
2346 | } | |
2347 | } | |
d46c5b12 KH |
2348 | |
2349 | coding->consumed = coding->consumed_char = src - source; | |
2350 | coding->produced = dst - destination; | |
2351 | return result; | |
4ed46869 KH |
2352 | } |
2353 | ||
2354 | /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". | |
2355 | This function can encode `charset_ascii', `charset_katakana_jisx0201', | |
2356 | `charset_jisx0208', `charset_big5_1', and `charset_big5-2'. We are | |
2357 | sure that all these charsets are registered as official charset | |
2358 | (i.e. do not have extended leading-codes). Characters of other | |
2359 | charsets are produced without any encoding. If SJIS_P is 1, encode | |
2360 | SJIS text, else encode BIG5 text. */ | |
2361 | ||
2362 | int | |
2363 | encode_coding_sjis_big5 (coding, source, destination, | |
d46c5b12 | 2364 | src_bytes, dst_bytes, sjis_p) |
4ed46869 KH |
2365 | struct coding_system *coding; |
2366 | unsigned char *source, *destination; | |
2367 | int src_bytes, dst_bytes; | |
4ed46869 KH |
2368 | int sjis_p; |
2369 | { | |
2370 | unsigned char *src = source; | |
2371 | unsigned char *src_end = source + src_bytes; | |
2372 | unsigned char *dst = destination; | |
2373 | unsigned char *dst_end = destination + dst_bytes; | |
2374 | /* Since the maximum bytes produced by each loop is 2, we subtract 1 | |
2375 | from DST_END to assure overflow checking is necessary only at the | |
2376 | head of loop. */ | |
2377 | unsigned char *adjusted_dst_end = dst_end - 1; | |
84fbb8a0 | 2378 | Lisp_Object translation_table |
f967223b | 2379 | = coding->translation_table_for_encode; |
d46c5b12 | 2380 | int result = CODING_FINISH_NORMAL; |
a5d301df | 2381 | |
84fbb8a0 | 2382 | if (!NILP (Venable_character_translation) && NILP (translation_table)) |
f967223b | 2383 | translation_table = Vstandard_translation_table_for_encode; |
4ed46869 | 2384 | |
d46c5b12 | 2385 | coding->consumed_char = 0; |
fb88bf2d | 2386 | coding->fake_multibyte = 0; |
d46c5b12 KH |
2387 | while (src < src_end && (dst_bytes |
2388 | ? (dst < adjusted_dst_end) | |
2389 | : (dst < src - 1))) | |
4ed46869 KH |
2390 | { |
2391 | /* SRC_BASE remembers the start position in source in each loop. | |
2392 | The loop will be exited when there's not enough source text | |
2393 | to analyze multi-byte codes (within macros ONE_MORE_BYTE and | |
2394 | TWO_MORE_BYTES). In that case, SRC is reset to SRC_BASE | |
2395 | before exiting. */ | |
2396 | unsigned char *src_base = src; | |
2397 | unsigned char c1 = *src++, c2, c3, c4; | |
2398 | ||
2399 | if (coding->composing) | |
2400 | { | |
2401 | if (c1 == 0xA0) | |
2402 | { | |
2403 | ONE_MORE_BYTE (c1); | |
2404 | c1 &= 0x7F; | |
2405 | } | |
2406 | else if (c1 >= 0xA0) | |
2407 | c1 -= 0x20; | |
2408 | else | |
2409 | coding->composing = 0; | |
2410 | } | |
2411 | ||
2412 | switch (emacs_code_class[c1]) | |
2413 | { | |
2414 | case EMACS_ascii_code: | |
a5d301df KH |
2415 | ENCODE_SJIS_BIG5_CHARACTER (charset_ascii, c1, /* dummy */ c2); |
2416 | break; | |
2417 | ||
4ed46869 KH |
2418 | case EMACS_control_code: |
2419 | *dst++ = c1; | |
d46c5b12 | 2420 | coding->consumed_char++; |
4ed46869 KH |
2421 | break; |
2422 | ||
2423 | case EMACS_carriage_return_code: | |
d46c5b12 | 2424 | if (! (coding->mode & CODING_MODE_SELECTIVE_DISPLAY)) |
4ed46869 KH |
2425 | { |
2426 | *dst++ = c1; | |
d46c5b12 | 2427 | coding->consumed_char++; |
4ed46869 KH |
2428 | break; |
2429 | } | |
2430 | /* fall down to treat '\r' as '\n' ... */ | |
2431 | ||
2432 | case EMACS_linefeed_code: | |
2433 | if (coding->eol_type == CODING_EOL_LF | |
0ef69138 | 2434 | || coding->eol_type == CODING_EOL_UNDECIDED) |
4ed46869 KH |
2435 | *dst++ = '\n'; |
2436 | else if (coding->eol_type == CODING_EOL_CRLF) | |
2437 | *dst++ = '\r', *dst++ = '\n'; | |
2438 | else | |
2439 | *dst++ = '\r'; | |
d46c5b12 | 2440 | coding->consumed_char++; |
4ed46869 KH |
2441 | break; |
2442 | ||
2443 | case EMACS_leading_code_2: | |
2444 | ONE_MORE_BYTE (c2); | |
a5d301df | 2445 | ENCODE_SJIS_BIG5_CHARACTER (c1, c2, /* dummy */ c3); |
4ed46869 KH |
2446 | break; |
2447 | ||
2448 | case EMACS_leading_code_3: | |
2449 | TWO_MORE_BYTES (c2, c3); | |
a5d301df | 2450 | ENCODE_SJIS_BIG5_CHARACTER (c1, c2, c3); |
4ed46869 KH |
2451 | break; |
2452 | ||
2453 | case EMACS_leading_code_4: | |
2454 | THREE_MORE_BYTES (c2, c3, c4); | |
a5d301df | 2455 | ENCODE_SJIS_BIG5_CHARACTER (c2, c3, c4); |
4ed46869 KH |
2456 | break; |
2457 | ||
2458 | case EMACS_leading_code_composition: | |
2459 | coding->composing = 1; | |
2460 | break; | |
2461 | ||
2462 | default: /* i.e. case EMACS_invalid_code: */ | |
2463 | *dst++ = c1; | |
d46c5b12 | 2464 | coding->consumed_char++; |
4ed46869 KH |
2465 | } |
2466 | continue; | |
2467 | ||
2468 | label_end_of_loop: | |
d46c5b12 KH |
2469 | result = CODING_FINISH_INSUFFICIENT_SRC; |
2470 | src = src_base; | |
4ed46869 KH |
2471 | break; |
2472 | } | |
2473 | ||
d46c5b12 KH |
2474 | if (result == CODING_FINISH_NORMAL |
2475 | && src < src_end) | |
2476 | result = CODING_FINISH_INSUFFICIENT_DST; | |
2477 | coding->consumed = src - source; | |
2478 | coding->produced = coding->produced_char = dst - destination; | |
2479 | return result; | |
4ed46869 KH |
2480 | } |
2481 | ||
2482 | \f | |
1397dc18 KH |
2483 | /*** 5. CCL handlers ***/ |
2484 | ||
2485 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
2486 | Check if a text is encoded in a coding system of which | |
2487 | encoder/decoder are written in CCL program. If it is, return | |
2488 | CODING_CATEGORY_MASK_CCL, else return 0. */ | |
2489 | ||
2490 | int | |
2491 | detect_coding_ccl (src, src_end) | |
2492 | unsigned char *src, *src_end; | |
2493 | { | |
2494 | unsigned char *valid; | |
2495 | ||
2496 | /* No coding system is assigned to coding-category-ccl. */ | |
2497 | if (!coding_system_table[CODING_CATEGORY_IDX_CCL]) | |
2498 | return 0; | |
2499 | ||
2500 | valid = coding_system_table[CODING_CATEGORY_IDX_CCL]->spec.ccl.valid_codes; | |
2501 | while (src < src_end) | |
2502 | { | |
2503 | if (! valid[*src]) return 0; | |
2504 | src++; | |
2505 | } | |
2506 | return CODING_CATEGORY_MASK_CCL; | |
2507 | } | |
2508 | ||
2509 | \f | |
2510 | /*** 6. End-of-line handlers ***/ | |
4ed46869 KH |
2511 | |
2512 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". | |
2513 | This function is called only when `coding->eol_type' is | |
2514 | CODING_EOL_CRLF or CODING_EOL_CR. */ | |
2515 | ||
dfcf069d | 2516 | int |
d46c5b12 | 2517 | decode_eol (coding, source, destination, src_bytes, dst_bytes) |
4ed46869 KH |
2518 | struct coding_system *coding; |
2519 | unsigned char *source, *destination; | |
2520 | int src_bytes, dst_bytes; | |
4ed46869 KH |
2521 | { |
2522 | unsigned char *src = source; | |
2523 | unsigned char *src_end = source + src_bytes; | |
2524 | unsigned char *dst = destination; | |
2525 | unsigned char *dst_end = destination + dst_bytes; | |
fb88bf2d | 2526 | unsigned char c; |
d46c5b12 KH |
2527 | int result = CODING_FINISH_NORMAL; |
2528 | ||
fb88bf2d KH |
2529 | coding->fake_multibyte = 0; |
2530 | ||
d46c5b12 KH |
2531 | if (src_bytes <= 0) |
2532 | return result; | |
4ed46869 KH |
2533 | |
2534 | switch (coding->eol_type) | |
2535 | { | |
2536 | case CODING_EOL_CRLF: | |
2537 | { | |
2538 | /* Since the maximum bytes produced by each loop is 2, we | |
2539 | subtract 1 from DST_END to assure overflow checking is | |
2540 | necessary only at the head of loop. */ | |
2541 | unsigned char *adjusted_dst_end = dst_end - 1; | |
2542 | ||
d46c5b12 KH |
2543 | while (src < src_end && (dst_bytes |
2544 | ? (dst < adjusted_dst_end) | |
2545 | : (dst < src - 1))) | |
4ed46869 KH |
2546 | { |
2547 | unsigned char *src_base = src; | |
fb88bf2d KH |
2548 | |
2549 | c = *src++; | |
4ed46869 KH |
2550 | if (c == '\r') |
2551 | { | |
2552 | ONE_MORE_BYTE (c); | |
fdfcf19d KH |
2553 | if (c == '\n') |
2554 | *dst++ = c; | |
2555 | else | |
d46c5b12 KH |
2556 | { |
2557 | if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL) | |
2558 | { | |
2559 | result = CODING_FINISH_INCONSISTENT_EOL; | |
2560 | goto label_end_of_loop_2; | |
2561 | } | |
fdfcf19d | 2562 | src--; |
d46c5b12 | 2563 | *dst++ = '\r'; |
fb88bf2d KH |
2564 | if (BASE_LEADING_CODE_P (c)) |
2565 | coding->fake_multibyte = 1; | |
d46c5b12 | 2566 | } |
4ed46869 | 2567 | } |
d46c5b12 KH |
2568 | else if (c == '\n' |
2569 | && (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)) | |
2570 | { | |
2571 | result = CODING_FINISH_INCONSISTENT_EOL; | |
2572 | goto label_end_of_loop_2; | |
2573 | } | |
4ed46869 | 2574 | else |
fb88bf2d KH |
2575 | { |
2576 | *dst++ = c; | |
2577 | if (BASE_LEADING_CODE_P (c)) | |
2578 | coding->fake_multibyte = 1; | |
2579 | } | |
4ed46869 KH |
2580 | continue; |
2581 | ||
2582 | label_end_of_loop: | |
d46c5b12 KH |
2583 | result = CODING_FINISH_INSUFFICIENT_SRC; |
2584 | label_end_of_loop_2: | |
4ed46869 KH |
2585 | src = src_base; |
2586 | break; | |
2587 | } | |
fdfcf19d KH |
2588 | if (src < src_end) |
2589 | { | |
2590 | if (result == CODING_FINISH_NORMAL) | |
2591 | result = CODING_FINISH_INSUFFICIENT_DST; | |
2592 | else if (result != CODING_FINISH_INCONSISTENT_EOL | |
2593 | && coding->mode & CODING_MODE_LAST_BLOCK) | |
2594 | { | |
2595 | /* This is the last block of the text to be decoded. | |
2596 | We flush out all remaining codes. */ | |
2597 | src_bytes = src_end - src; | |
2598 | if (dst_bytes && (dst_end - dst < src_bytes)) | |
2599 | src_bytes = dst_end - dst; | |
2600 | bcopy (src, dst, src_bytes); | |
2601 | dst += src_bytes; | |
2602 | src += src_bytes; | |
2603 | } | |
2604 | } | |
4ed46869 | 2605 | } |
d46c5b12 | 2606 | break; |
4ed46869 KH |
2607 | |
2608 | case CODING_EOL_CR: | |
d46c5b12 KH |
2609 | if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL) |
2610 | { | |
fb88bf2d KH |
2611 | while (src < src_end) |
2612 | { | |
2613 | if ((c = *src++) == '\n') | |
2614 | break; | |
2615 | if (BASE_LEADING_CODE_P (c)) | |
2616 | coding->fake_multibyte = 1; | |
2617 | } | |
d46c5b12 KH |
2618 | if (*--src == '\n') |
2619 | { | |
2620 | src_bytes = src - source; | |
2621 | result = CODING_FINISH_INCONSISTENT_EOL; | |
2622 | } | |
2623 | } | |
2624 | if (dst_bytes && src_bytes > dst_bytes) | |
2625 | { | |
2626 | result = CODING_FINISH_INSUFFICIENT_DST; | |
2627 | src_bytes = dst_bytes; | |
2628 | } | |
2629 | if (dst_bytes) | |
2630 | bcopy (source, destination, src_bytes); | |
2631 | else | |
2632 | safe_bcopy (source, destination, src_bytes); | |
2633 | src = source + src_bytes; | |
2634 | while (src_bytes--) if (*dst++ == '\r') dst[-1] = '\n'; | |
4ed46869 KH |
2635 | break; |
2636 | ||
2637 | default: /* i.e. case: CODING_EOL_LF */ | |
d46c5b12 KH |
2638 | if (dst_bytes && src_bytes > dst_bytes) |
2639 | { | |
2640 | result = CODING_FINISH_INSUFFICIENT_DST; | |
2641 | src_bytes = dst_bytes; | |
2642 | } | |
2643 | if (dst_bytes) | |
2644 | bcopy (source, destination, src_bytes); | |
2645 | else | |
2646 | safe_bcopy (source, destination, src_bytes); | |
2647 | src += src_bytes; | |
993824c9 | 2648 | dst += src_bytes; |
fb88bf2d | 2649 | coding->fake_multibyte = 1; |
4ed46869 KH |
2650 | break; |
2651 | } | |
2652 | ||
d46c5b12 KH |
2653 | coding->consumed = coding->consumed_char = src - source; |
2654 | coding->produced = coding->produced_char = dst - destination; | |
2655 | return result; | |
4ed46869 KH |
2656 | } |
2657 | ||
2658 | /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". Encode | |
2659 | format of end-of-line according to `coding->eol_type'. If | |
d46c5b12 KH |
2660 | `coding->mode & CODING_MODE_SELECTIVE_DISPLAY' is nonzero, code |
2661 | '\r' in source text also means end-of-line. */ | |
4ed46869 | 2662 | |
dfcf069d | 2663 | int |
d46c5b12 | 2664 | encode_eol (coding, source, destination, src_bytes, dst_bytes) |
4ed46869 KH |
2665 | struct coding_system *coding; |
2666 | unsigned char *source, *destination; | |
2667 | int src_bytes, dst_bytes; | |
4ed46869 KH |
2668 | { |
2669 | unsigned char *src = source; | |
2670 | unsigned char *dst = destination; | |
d46c5b12 | 2671 | int result = CODING_FINISH_NORMAL; |
4ed46869 | 2672 | |
fb88bf2d KH |
2673 | coding->fake_multibyte = 0; |
2674 | ||
d46c5b12 KH |
2675 | if (coding->eol_type == CODING_EOL_CRLF) |
2676 | { | |
2677 | unsigned char c; | |
2678 | unsigned char *src_end = source + src_bytes; | |
2679 | unsigned char *dst_end = destination + dst_bytes; | |
2680 | /* Since the maximum bytes produced by each loop is 2, we | |
2681 | subtract 1 from DST_END to assure overflow checking is | |
2682 | necessary only at the head of loop. */ | |
2683 | unsigned char *adjusted_dst_end = dst_end - 1; | |
2684 | ||
2685 | while (src < src_end && (dst_bytes | |
2686 | ? (dst < adjusted_dst_end) | |
2687 | : (dst < src - 1))) | |
2688 | { | |
2689 | c = *src++; | |
2690 | if (c == '\n' | |
2691 | || (c == '\r' && (coding->mode & CODING_MODE_SELECTIVE_DISPLAY))) | |
2692 | *dst++ = '\r', *dst++ = '\n'; | |
2693 | else | |
fb88bf2d KH |
2694 | { |
2695 | *dst++ = c; | |
2696 | if (BASE_LEADING_CODE_P (c)) | |
2697 | coding->fake_multibyte = 1; | |
2698 | } | |
d46c5b12 KH |
2699 | } |
2700 | if (src < src_end) | |
2701 | result = CODING_FINISH_INSUFFICIENT_DST; | |
2702 | } | |
2703 | else | |
4ed46869 | 2704 | { |
fb88bf2d KH |
2705 | unsigned char c; |
2706 | ||
d46c5b12 | 2707 | if (dst_bytes && src_bytes > dst_bytes) |
4ed46869 | 2708 | { |
d46c5b12 KH |
2709 | src_bytes = dst_bytes; |
2710 | result = CODING_FINISH_INSUFFICIENT_DST; | |
2711 | } | |
2712 | if (dst_bytes) | |
2713 | bcopy (source, destination, src_bytes); | |
2714 | else | |
993824c9 RS |
2715 | safe_bcopy (source, destination, src_bytes); |
2716 | dst_bytes = src_bytes; | |
2717 | if (coding->eol_type == CODING_EOL_CR) | |
d46c5b12 KH |
2718 | { |
2719 | while (src_bytes--) | |
fb88bf2d KH |
2720 | { |
2721 | if ((c = *dst++) == '\n') | |
2722 | dst[-1] = '\r'; | |
2723 | else if (BASE_LEADING_CODE_P (c)) | |
993824c9 | 2724 | coding->fake_multibyte = 1; |
fb88bf2d | 2725 | } |
d46c5b12 | 2726 | } |
fb88bf2d | 2727 | else |
d46c5b12 | 2728 | { |
fb88bf2d KH |
2729 | if (coding->mode & CODING_MODE_SELECTIVE_DISPLAY) |
2730 | { | |
2731 | while (src_bytes--) | |
2732 | if (*dst++ == '\r') dst[-1] = '\n'; | |
2733 | } | |
2734 | coding->fake_multibyte = 1; | |
4ed46869 | 2735 | } |
fb88bf2d KH |
2736 | src = source + dst_bytes; |
2737 | dst = destination + dst_bytes; | |
4ed46869 KH |
2738 | } |
2739 | ||
d46c5b12 KH |
2740 | coding->consumed = coding->consumed_char = src - source; |
2741 | coding->produced = coding->produced_char = dst - destination; | |
2742 | return result; | |
4ed46869 KH |
2743 | } |
2744 | ||
2745 | \f | |
1397dc18 | 2746 | /*** 7. C library functions ***/ |
4ed46869 KH |
2747 | |
2748 | /* In Emacs Lisp, coding system is represented by a Lisp symbol which | |
2749 | has a property `coding-system'. The value of this property is a | |
2750 | vector of length 5 (called as coding-vector). Among elements of | |
2751 | this vector, the first (element[0]) and the fifth (element[4]) | |
2752 | carry important information for decoding/encoding. Before | |
2753 | decoding/encoding, this information should be set in fields of a | |
2754 | structure of type `coding_system'. | |
2755 | ||
2756 | A value of property `coding-system' can be a symbol of another | |
2757 | subsidiary coding-system. In that case, Emacs gets coding-vector | |
2758 | from that symbol. | |
2759 | ||
2760 | `element[0]' contains information to be set in `coding->type'. The | |
2761 | value and its meaning is as follows: | |
2762 | ||
0ef69138 KH |
2763 | 0 -- coding_type_emacs_mule |
2764 | 1 -- coding_type_sjis | |
2765 | 2 -- coding_type_iso2022 | |
2766 | 3 -- coding_type_big5 | |
2767 | 4 -- coding_type_ccl encoder/decoder written in CCL | |
2768 | nil -- coding_type_no_conversion | |
2769 | t -- coding_type_undecided (automatic conversion on decoding, | |
2770 | no-conversion on encoding) | |
4ed46869 KH |
2771 | |
2772 | `element[4]' contains information to be set in `coding->flags' and | |
2773 | `coding->spec'. The meaning varies by `coding->type'. | |
2774 | ||
2775 | If `coding->type' is `coding_type_iso2022', element[4] is a vector | |
2776 | of length 32 (of which the first 13 sub-elements are used now). | |
2777 | Meanings of these sub-elements are: | |
2778 | ||
2779 | sub-element[N] where N is 0 through 3: to be set in `coding->spec.iso2022' | |
2780 | If the value is an integer of valid charset, the charset is | |
2781 | assumed to be designated to graphic register N initially. | |
2782 | ||
2783 | If the value is minus, it is a minus value of charset which | |
2784 | reserves graphic register N, which means that the charset is | |
2785 | not designated initially but should be designated to graphic | |
2786 | register N just before encoding a character in that charset. | |
2787 | ||
2788 | If the value is nil, graphic register N is never used on | |
2789 | encoding. | |
2790 | ||
2791 | sub-element[N] where N is 4 through 11: to be set in `coding->flags' | |
2792 | Each value takes t or nil. See the section ISO2022 of | |
2793 | `coding.h' for more information. | |
2794 | ||
2795 | If `coding->type' is `coding_type_big5', element[4] is t to denote | |
2796 | BIG5-ETen or nil to denote BIG5-HKU. | |
2797 | ||
2798 | If `coding->type' takes the other value, element[4] is ignored. | |
2799 | ||
2800 | Emacs Lisp's coding system also carries information about format of | |
2801 | end-of-line in a value of property `eol-type'. If the value is | |
2802 | integer, 0 means CODING_EOL_LF, 1 means CODING_EOL_CRLF, and 2 | |
2803 | means CODING_EOL_CR. If it is not integer, it should be a vector | |
2804 | of subsidiary coding systems of which property `eol-type' has one | |
2805 | of above values. | |
2806 | ||
2807 | */ | |
2808 | ||
2809 | /* Extract information for decoding/encoding from CODING_SYSTEM_SYMBOL | |
2810 | and set it in CODING. If CODING_SYSTEM_SYMBOL is invalid, CODING | |
2811 | is setup so that no conversion is necessary and return -1, else | |
2812 | return 0. */ | |
2813 | ||
2814 | int | |
e0e989f6 KH |
2815 | setup_coding_system (coding_system, coding) |
2816 | Lisp_Object coding_system; | |
4ed46869 KH |
2817 | struct coding_system *coding; |
2818 | { | |
d46c5b12 | 2819 | Lisp_Object coding_spec, coding_type, eol_type, plist; |
4608c386 | 2820 | Lisp_Object val; |
70c22245 | 2821 | int i; |
4ed46869 | 2822 | |
d46c5b12 | 2823 | /* Initialize some fields required for all kinds of coding systems. */ |
774324d6 | 2824 | coding->symbol = coding_system; |
d46c5b12 KH |
2825 | coding->common_flags = 0; |
2826 | coding->mode = 0; | |
2827 | coding->heading_ascii = -1; | |
2828 | coding->post_read_conversion = coding->pre_write_conversion = Qnil; | |
4608c386 KH |
2829 | coding_spec = Fget (coding_system, Qcoding_system); |
2830 | if (!VECTORP (coding_spec) | |
2831 | || XVECTOR (coding_spec)->size != 5 | |
2832 | || !CONSP (XVECTOR (coding_spec)->contents[3])) | |
4ed46869 | 2833 | goto label_invalid_coding_system; |
4608c386 | 2834 | |
d46c5b12 KH |
2835 | eol_type = inhibit_eol_conversion ? Qnil : Fget (coding_system, Qeol_type); |
2836 | if (VECTORP (eol_type)) | |
2837 | { | |
2838 | coding->eol_type = CODING_EOL_UNDECIDED; | |
2839 | coding->common_flags = CODING_REQUIRE_DETECTION_MASK; | |
2840 | } | |
2841 | else if (XFASTINT (eol_type) == 1) | |
2842 | { | |
2843 | coding->eol_type = CODING_EOL_CRLF; | |
2844 | coding->common_flags | |
2845 | = CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK; | |
2846 | } | |
2847 | else if (XFASTINT (eol_type) == 2) | |
2848 | { | |
2849 | coding->eol_type = CODING_EOL_CR; | |
2850 | coding->common_flags | |
2851 | = CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK; | |
2852 | } | |
2853 | else | |
2854 | coding->eol_type = CODING_EOL_LF; | |
2855 | ||
2856 | coding_type = XVECTOR (coding_spec)->contents[0]; | |
2857 | /* Try short cut. */ | |
2858 | if (SYMBOLP (coding_type)) | |
2859 | { | |
2860 | if (EQ (coding_type, Qt)) | |
2861 | { | |
2862 | coding->type = coding_type_undecided; | |
2863 | coding->common_flags |= CODING_REQUIRE_DETECTION_MASK; | |
2864 | } | |
2865 | else | |
2866 | coding->type = coding_type_no_conversion; | |
2867 | return 0; | |
2868 | } | |
2869 | ||
2870 | /* Initialize remaining fields. */ | |
2871 | coding->composing = 0; | |
d46c5b12 KH |
2872 | |
2873 | /* Get values of coding system properties: | |
2874 | `post-read-conversion', `pre-write-conversion', | |
f967223b | 2875 | `translation-table-for-decode', `translation-table-for-encode'. */ |
4608c386 KH |
2876 | plist = XVECTOR (coding_spec)->contents[3]; |
2877 | coding->post_read_conversion = Fplist_get (plist, Qpost_read_conversion); | |
2878 | coding->pre_write_conversion = Fplist_get (plist, Qpre_write_conversion); | |
f967223b | 2879 | val = Fplist_get (plist, Qtranslation_table_for_decode); |
4608c386 | 2880 | if (SYMBOLP (val)) |
f967223b KH |
2881 | val = Fget (val, Qtranslation_table_for_decode); |
2882 | coding->translation_table_for_decode = CHAR_TABLE_P (val) ? val : Qnil; | |
2883 | val = Fplist_get (plist, Qtranslation_table_for_encode); | |
4608c386 | 2884 | if (SYMBOLP (val)) |
f967223b KH |
2885 | val = Fget (val, Qtranslation_table_for_encode); |
2886 | coding->translation_table_for_encode = CHAR_TABLE_P (val) ? val : Qnil; | |
d46c5b12 KH |
2887 | val = Fplist_get (plist, Qcoding_category); |
2888 | if (!NILP (val)) | |
2889 | { | |
2890 | val = Fget (val, Qcoding_category_index); | |
2891 | if (INTEGERP (val)) | |
2892 | coding->category_idx = XINT (val); | |
2893 | else | |
2894 | goto label_invalid_coding_system; | |
2895 | } | |
2896 | else | |
2897 | goto label_invalid_coding_system; | |
4608c386 | 2898 | |
70c22245 KH |
2899 | val = Fplist_get (plist, Qsafe_charsets); |
2900 | if (EQ (val, Qt)) | |
2901 | { | |
2902 | for (i = 0; i <= MAX_CHARSET; i++) | |
2903 | coding->safe_charsets[i] = 1; | |
2904 | } | |
2905 | else | |
2906 | { | |
2907 | bzero (coding->safe_charsets, MAX_CHARSET + 1); | |
2908 | while (CONSP (val)) | |
2909 | { | |
2910 | if ((i = get_charset_id (XCONS (val)->car)) >= 0) | |
2911 | coding->safe_charsets[i] = 1; | |
2912 | val = XCONS (val)->cdr; | |
2913 | } | |
2914 | } | |
2915 | ||
d46c5b12 | 2916 | switch (XFASTINT (coding_type)) |
4ed46869 KH |
2917 | { |
2918 | case 0: | |
0ef69138 | 2919 | coding->type = coding_type_emacs_mule; |
c952af22 KH |
2920 | if (!NILP (coding->post_read_conversion)) |
2921 | coding->common_flags |= CODING_REQUIRE_DECODING_MASK; | |
2922 | if (!NILP (coding->pre_write_conversion)) | |
2923 | coding->common_flags |= CODING_REQUIRE_ENCODING_MASK; | |
4ed46869 KH |
2924 | break; |
2925 | ||
2926 | case 1: | |
2927 | coding->type = coding_type_sjis; | |
c952af22 KH |
2928 | coding->common_flags |
2929 | |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK; | |
4ed46869 KH |
2930 | break; |
2931 | ||
2932 | case 2: | |
2933 | coding->type = coding_type_iso2022; | |
c952af22 KH |
2934 | coding->common_flags |
2935 | |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK; | |
4ed46869 | 2936 | { |
70c22245 | 2937 | Lisp_Object val, temp; |
4ed46869 | 2938 | Lisp_Object *flags; |
d46c5b12 | 2939 | int i, charset, reg_bits = 0; |
4ed46869 | 2940 | |
4608c386 | 2941 | val = XVECTOR (coding_spec)->contents[4]; |
f44d27ce | 2942 | |
4ed46869 KH |
2943 | if (!VECTORP (val) || XVECTOR (val)->size != 32) |
2944 | goto label_invalid_coding_system; | |
2945 | ||
2946 | flags = XVECTOR (val)->contents; | |
2947 | coding->flags | |
2948 | = ((NILP (flags[4]) ? 0 : CODING_FLAG_ISO_SHORT_FORM) | |
2949 | | (NILP (flags[5]) ? 0 : CODING_FLAG_ISO_RESET_AT_EOL) | |
2950 | | (NILP (flags[6]) ? 0 : CODING_FLAG_ISO_RESET_AT_CNTL) | |
2951 | | (NILP (flags[7]) ? 0 : CODING_FLAG_ISO_SEVEN_BITS) | |
2952 | | (NILP (flags[8]) ? 0 : CODING_FLAG_ISO_LOCKING_SHIFT) | |
2953 | | (NILP (flags[9]) ? 0 : CODING_FLAG_ISO_SINGLE_SHIFT) | |
2954 | | (NILP (flags[10]) ? 0 : CODING_FLAG_ISO_USE_ROMAN) | |
2955 | | (NILP (flags[11]) ? 0 : CODING_FLAG_ISO_USE_OLDJIS) | |
e0e989f6 KH |
2956 | | (NILP (flags[12]) ? 0 : CODING_FLAG_ISO_NO_DIRECTION) |
2957 | | (NILP (flags[13]) ? 0 : CODING_FLAG_ISO_INIT_AT_BOL) | |
c4825358 KH |
2958 | | (NILP (flags[14]) ? 0 : CODING_FLAG_ISO_DESIGNATE_AT_BOL) |
2959 | | (NILP (flags[15]) ? 0 : CODING_FLAG_ISO_SAFE) | |
3f003981 | 2960 | | (NILP (flags[16]) ? 0 : CODING_FLAG_ISO_LATIN_EXTRA) |
c4825358 | 2961 | ); |
4ed46869 KH |
2962 | |
2963 | /* Invoke graphic register 0 to plane 0. */ | |
2964 | CODING_SPEC_ISO_INVOCATION (coding, 0) = 0; | |
2965 | /* Invoke graphic register 1 to plane 1 if we can use full 8-bit. */ | |
2966 | CODING_SPEC_ISO_INVOCATION (coding, 1) | |
2967 | = (coding->flags & CODING_FLAG_ISO_SEVEN_BITS ? -1 : 1); | |
2968 | /* Not single shifting at first. */ | |
6e85d753 | 2969 | CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; |
e0e989f6 | 2970 | /* Beginning of buffer should also be regarded as bol. */ |
6e85d753 | 2971 | CODING_SPEC_ISO_BOL (coding) = 1; |
4ed46869 | 2972 | |
70c22245 KH |
2973 | for (charset = 0; charset <= MAX_CHARSET; charset++) |
2974 | CODING_SPEC_ISO_REVISION_NUMBER (coding, charset) = 255; | |
2975 | val = Vcharset_revision_alist; | |
2976 | while (CONSP (val)) | |
2977 | { | |
2978 | charset = get_charset_id (Fcar_safe (XCONS (val)->car)); | |
2979 | if (charset >= 0 | |
2980 | && (temp = Fcdr_safe (XCONS (val)->car), INTEGERP (temp)) | |
2981 | && (i = XINT (temp), (i >= 0 && (i + '@') < 128))) | |
2982 | CODING_SPEC_ISO_REVISION_NUMBER (coding, charset) = i; | |
2983 | val = XCONS (val)->cdr; | |
2984 | } | |
2985 | ||
4ed46869 KH |
2986 | /* Checks FLAGS[REG] (REG = 0, 1, 2 3) and decide designations. |
2987 | FLAGS[REG] can be one of below: | |
2988 | integer CHARSET: CHARSET occupies register I, | |
2989 | t: designate nothing to REG initially, but can be used | |
2990 | by any charsets, | |
2991 | list of integer, nil, or t: designate the first | |
2992 | element (if integer) to REG initially, the remaining | |
2993 | elements (if integer) is designated to REG on request, | |
d46c5b12 | 2994 | if an element is t, REG can be used by any charsets, |
4ed46869 | 2995 | nil: REG is never used. */ |
467e7675 | 2996 | for (charset = 0; charset <= MAX_CHARSET; charset++) |
1ba9e4ab KH |
2997 | CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) |
2998 | = CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION; | |
4ed46869 KH |
2999 | for (i = 0; i < 4; i++) |
3000 | { | |
3001 | if (INTEGERP (flags[i]) | |
e0e989f6 KH |
3002 | && (charset = XINT (flags[i]), CHARSET_VALID_P (charset)) |
3003 | || (charset = get_charset_id (flags[i])) >= 0) | |
4ed46869 KH |
3004 | { |
3005 | CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = charset; | |
3006 | CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) = i; | |
3007 | } | |
3008 | else if (EQ (flags[i], Qt)) | |
3009 | { | |
3010 | CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = -1; | |
d46c5b12 KH |
3011 | reg_bits |= 1 << i; |
3012 | coding->flags |= CODING_FLAG_ISO_DESIGNATION; | |
4ed46869 KH |
3013 | } |
3014 | else if (CONSP (flags[i])) | |
3015 | { | |
84d60297 RS |
3016 | Lisp_Object tail; |
3017 | tail = flags[i]; | |
4ed46869 | 3018 | |
d46c5b12 | 3019 | coding->flags |= CODING_FLAG_ISO_DESIGNATION; |
4ed46869 KH |
3020 | if (INTEGERP (XCONS (tail)->car) |
3021 | && (charset = XINT (XCONS (tail)->car), | |
e0e989f6 KH |
3022 | CHARSET_VALID_P (charset)) |
3023 | || (charset = get_charset_id (XCONS (tail)->car)) >= 0) | |
4ed46869 KH |
3024 | { |
3025 | CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = charset; | |
3026 | CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) =i; | |
3027 | } | |
3028 | else | |
3029 | CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = -1; | |
3030 | tail = XCONS (tail)->cdr; | |
3031 | while (CONSP (tail)) | |
3032 | { | |
3033 | if (INTEGERP (XCONS (tail)->car) | |
3034 | && (charset = XINT (XCONS (tail)->car), | |
e0e989f6 KH |
3035 | CHARSET_VALID_P (charset)) |
3036 | || (charset = get_charset_id (XCONS (tail)->car)) >= 0) | |
70c22245 KH |
3037 | CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) |
3038 | = i; | |
4ed46869 | 3039 | else if (EQ (XCONS (tail)->car, Qt)) |
d46c5b12 | 3040 | reg_bits |= 1 << i; |
4ed46869 KH |
3041 | tail = XCONS (tail)->cdr; |
3042 | } | |
3043 | } | |
3044 | else | |
3045 | CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = -1; | |
3046 | ||
3047 | CODING_SPEC_ISO_DESIGNATION (coding, i) | |
3048 | = CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i); | |
3049 | } | |
3050 | ||
d46c5b12 | 3051 | if (reg_bits && ! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT)) |
4ed46869 KH |
3052 | { |
3053 | /* REG 1 can be used only by locking shift in 7-bit env. */ | |
3054 | if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) | |
d46c5b12 | 3055 | reg_bits &= ~2; |
4ed46869 KH |
3056 | if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT)) |
3057 | /* Without any shifting, only REG 0 and 1 can be used. */ | |
d46c5b12 | 3058 | reg_bits &= 3; |
4ed46869 KH |
3059 | } |
3060 | ||
d46c5b12 KH |
3061 | if (reg_bits) |
3062 | for (charset = 0; charset <= MAX_CHARSET; charset++) | |
6e85d753 | 3063 | { |
d46c5b12 KH |
3064 | if (CHARSET_VALID_P (charset)) |
3065 | { | |
3066 | /* There exist some default graphic registers to be | |
3067 | used CHARSET. */ | |
3068 | ||
3069 | /* We had better avoid designating a charset of | |
3070 | CHARS96 to REG 0 as far as possible. */ | |
3071 | if (CHARSET_CHARS (charset) == 96) | |
3072 | CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) | |
3073 | = (reg_bits & 2 | |
3074 | ? 1 : (reg_bits & 4 ? 2 : (reg_bits & 8 ? 3 : 0))); | |
3075 | else | |
3076 | CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) | |
3077 | = (reg_bits & 1 | |
3078 | ? 0 : (reg_bits & 2 ? 1 : (reg_bits & 4 ? 2 : 3))); | |
3079 | } | |
6e85d753 | 3080 | } |
4ed46869 | 3081 | } |
c952af22 | 3082 | coding->common_flags |= CODING_REQUIRE_FLUSHING_MASK; |
d46c5b12 | 3083 | coding->spec.iso2022.last_invalid_designation_register = -1; |
4ed46869 KH |
3084 | break; |
3085 | ||
3086 | case 3: | |
3087 | coding->type = coding_type_big5; | |
c952af22 KH |
3088 | coding->common_flags |
3089 | |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK; | |
4ed46869 | 3090 | coding->flags |
4608c386 | 3091 | = (NILP (XVECTOR (coding_spec)->contents[4]) |
4ed46869 KH |
3092 | ? CODING_FLAG_BIG5_HKU |
3093 | : CODING_FLAG_BIG5_ETEN); | |
3094 | break; | |
3095 | ||
3096 | case 4: | |
3097 | coding->type = coding_type_ccl; | |
c952af22 KH |
3098 | coding->common_flags |
3099 | |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK; | |
4ed46869 | 3100 | { |
84d60297 | 3101 | Lisp_Object val; |
d21ca14d KH |
3102 | Lisp_Object decoder, encoder; |
3103 | ||
84d60297 | 3104 | val = XVECTOR (coding_spec)->contents[4]; |
4ed46869 | 3105 | if (CONSP (val) |
d21ca14d KH |
3106 | && SYMBOLP (XCONS (val)->car) |
3107 | && !NILP (decoder = Fget (XCONS (val)->car, Qccl_program_idx)) | |
f82423d7 | 3108 | && !NILP (decoder = Fcdr (Faref (Vccl_program_table, decoder))) |
d21ca14d KH |
3109 | && SYMBOLP (XCONS (val)->cdr) |
3110 | && !NILP (encoder = Fget (XCONS (val)->cdr, Qccl_program_idx)) | |
f82423d7 | 3111 | && !NILP (encoder = Fcdr (Faref (Vccl_program_table, encoder)))) |
4ed46869 | 3112 | { |
d21ca14d KH |
3113 | setup_ccl_program (&(coding->spec.ccl.decoder), decoder); |
3114 | setup_ccl_program (&(coding->spec.ccl.encoder), encoder); | |
4ed46869 KH |
3115 | } |
3116 | else | |
3117 | goto label_invalid_coding_system; | |
1397dc18 KH |
3118 | |
3119 | bzero (coding->spec.ccl.valid_codes, 256); | |
3120 | val = Fplist_get (plist, Qvalid_codes); | |
3121 | if (CONSP (val)) | |
3122 | { | |
3123 | Lisp_Object this; | |
3124 | ||
7b179c2d | 3125 | for (; CONSP (val); val = XCONS (val)->cdr) |
1397dc18 | 3126 | { |
7b179c2d | 3127 | this = XCONS (val)->car; |
1397dc18 KH |
3128 | if (INTEGERP (this) |
3129 | && XINT (this) >= 0 && XINT (this) < 256) | |
3130 | coding->spec.ccl.valid_codes[XINT (this)] = 1; | |
3131 | else if (CONSP (this) | |
3132 | && INTEGERP (XCONS (this)->car) | |
3133 | && INTEGERP (XCONS (this)->cdr)) | |
3134 | { | |
3135 | int start = XINT (XCONS (this)->car); | |
3136 | int end = XINT (XCONS (this)->cdr); | |
3137 | ||
3138 | if (start >= 0 && start <= end && end < 256) | |
3139 | while (start < end) | |
3140 | coding->spec.ccl.valid_codes[start++] = 1; | |
3141 | } | |
3142 | } | |
3143 | } | |
4ed46869 | 3144 | } |
c952af22 | 3145 | coding->common_flags |= CODING_REQUIRE_FLUSHING_MASK; |
4ed46869 KH |
3146 | break; |
3147 | ||
27901516 KH |
3148 | case 5: |
3149 | coding->type = coding_type_raw_text; | |
3150 | break; | |
3151 | ||
4ed46869 | 3152 | default: |
d46c5b12 | 3153 | goto label_invalid_coding_system; |
4ed46869 KH |
3154 | } |
3155 | return 0; | |
3156 | ||
3157 | label_invalid_coding_system: | |
3158 | coding->type = coding_type_no_conversion; | |
d46c5b12 | 3159 | coding->category_idx = CODING_CATEGORY_IDX_BINARY; |
c952af22 | 3160 | coding->common_flags = 0; |
dec137e5 | 3161 | coding->eol_type = CODING_EOL_LF; |
d46c5b12 | 3162 | coding->pre_write_conversion = coding->post_read_conversion = Qnil; |
4ed46869 KH |
3163 | return -1; |
3164 | } | |
3165 | ||
54f78171 KH |
3166 | /* Setup raw-text or one of its subsidiaries in the structure |
3167 | coding_system CODING according to the already setup value eol_type | |
3168 | in CODING. CODING should be setup for some coding system in | |
3169 | advance. */ | |
3170 | ||
3171 | void | |
3172 | setup_raw_text_coding_system (coding) | |
3173 | struct coding_system *coding; | |
3174 | { | |
3175 | if (coding->type != coding_type_raw_text) | |
3176 | { | |
3177 | coding->symbol = Qraw_text; | |
3178 | coding->type = coding_type_raw_text; | |
3179 | if (coding->eol_type != CODING_EOL_UNDECIDED) | |
3180 | { | |
84d60297 RS |
3181 | Lisp_Object subsidiaries; |
3182 | subsidiaries = Fget (Qraw_text, Qeol_type); | |
54f78171 KH |
3183 | |
3184 | if (VECTORP (subsidiaries) | |
3185 | && XVECTOR (subsidiaries)->size == 3) | |
3186 | coding->symbol | |
3187 | = XVECTOR (subsidiaries)->contents[coding->eol_type]; | |
3188 | } | |
3189 | } | |
3190 | return; | |
3191 | } | |
3192 | ||
4ed46869 KH |
3193 | /* Emacs has a mechanism to automatically detect a coding system if it |
3194 | is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But, | |
3195 | it's impossible to distinguish some coding systems accurately | |
3196 | because they use the same range of codes. So, at first, coding | |
3197 | systems are categorized into 7, those are: | |
3198 | ||
0ef69138 | 3199 | o coding-category-emacs-mule |
4ed46869 KH |
3200 | |
3201 | The category for a coding system which has the same code range | |
3202 | as Emacs' internal format. Assigned the coding-system (Lisp | |
0ef69138 | 3203 | symbol) `emacs-mule' by default. |
4ed46869 KH |
3204 | |
3205 | o coding-category-sjis | |
3206 | ||
3207 | The category for a coding system which has the same code range | |
3208 | as SJIS. Assigned the coding-system (Lisp | |
7717c392 | 3209 | symbol) `japanese-shift-jis' by default. |
4ed46869 KH |
3210 | |
3211 | o coding-category-iso-7 | |
3212 | ||
3213 | The category for a coding system which has the same code range | |
7717c392 | 3214 | as ISO2022 of 7-bit environment. This doesn't use any locking |
d46c5b12 KH |
3215 | shift and single shift functions. This can encode/decode all |
3216 | charsets. Assigned the coding-system (Lisp symbol) | |
3217 | `iso-2022-7bit' by default. | |
3218 | ||
3219 | o coding-category-iso-7-tight | |
3220 | ||
3221 | Same as coding-category-iso-7 except that this can | |
3222 | encode/decode only the specified charsets. | |
4ed46869 KH |
3223 | |
3224 | o coding-category-iso-8-1 | |
3225 | ||
3226 | The category for a coding system which has the same code range | |
3227 | as ISO2022 of 8-bit environment and graphic plane 1 used only | |
7717c392 KH |
3228 | for DIMENSION1 charset. This doesn't use any locking shift |
3229 | and single shift functions. Assigned the coding-system (Lisp | |
3230 | symbol) `iso-latin-1' by default. | |
4ed46869 KH |
3231 | |
3232 | o coding-category-iso-8-2 | |
3233 | ||
3234 | The category for a coding system which has the same code range | |
3235 | as ISO2022 of 8-bit environment and graphic plane 1 used only | |
7717c392 KH |
3236 | for DIMENSION2 charset. This doesn't use any locking shift |
3237 | and single shift functions. Assigned the coding-system (Lisp | |
3238 | symbol) `japanese-iso-8bit' by default. | |
4ed46869 | 3239 | |
7717c392 | 3240 | o coding-category-iso-7-else |
4ed46869 KH |
3241 | |
3242 | The category for a coding system which has the same code range | |
7717c392 KH |
3243 | as ISO2022 of 7-bit environemnt but uses locking shift or |
3244 | single shift functions. Assigned the coding-system (Lisp | |
3245 | symbol) `iso-2022-7bit-lock' by default. | |
3246 | ||
3247 | o coding-category-iso-8-else | |
3248 | ||
3249 | The category for a coding system which has the same code range | |
3250 | as ISO2022 of 8-bit environemnt but uses locking shift or | |
3251 | single shift functions. Assigned the coding-system (Lisp | |
3252 | symbol) `iso-2022-8bit-ss2' by default. | |
4ed46869 KH |
3253 | |
3254 | o coding-category-big5 | |
3255 | ||
3256 | The category for a coding system which has the same code range | |
3257 | as BIG5. Assigned the coding-system (Lisp symbol) | |
e0e989f6 | 3258 | `cn-big5' by default. |
4ed46869 | 3259 | |
1397dc18 KH |
3260 | o coding-category-ccl |
3261 | ||
3262 | The category for a coding system of which encoder/decoder is | |
3263 | written in CCL programs. The default value is nil, i.e., no | |
3264 | coding system is assigned. | |
3265 | ||
4ed46869 KH |
3266 | o coding-category-binary |
3267 | ||
3268 | The category for a coding system not categorized in any of the | |
3269 | above. Assigned the coding-system (Lisp symbol) | |
e0e989f6 | 3270 | `no-conversion' by default. |
4ed46869 KH |
3271 | |
3272 | Each of them is a Lisp symbol and the value is an actual | |
3273 | `coding-system's (this is also a Lisp symbol) assigned by a user. | |
3274 | What Emacs does actually is to detect a category of coding system. | |
3275 | Then, it uses a `coding-system' assigned to it. If Emacs can't | |
3276 | decide only one possible category, it selects a category of the | |
3277 | highest priority. Priorities of categories are also specified by a | |
3278 | user in a Lisp variable `coding-category-list'. | |
3279 | ||
3280 | */ | |
3281 | ||
66cfb530 KH |
3282 | static |
3283 | int ascii_skip_code[256]; | |
3284 | ||
d46c5b12 | 3285 | /* Detect how a text of length SRC_BYTES pointed by SOURCE is encoded. |
4ed46869 KH |
3286 | If it detects possible coding systems, return an integer in which |
3287 | appropriate flag bits are set. Flag bits are defined by macros | |
d46c5b12 | 3288 | CODING_CATEGORY_MASK_XXX in `coding.h'. |
4ed46869 | 3289 | |
d46c5b12 KH |
3290 | How many ASCII characters are at the head is returned as *SKIP. */ |
3291 | ||
3292 | static int | |
3293 | detect_coding_mask (source, src_bytes, priorities, skip) | |
3294 | unsigned char *source; | |
3295 | int src_bytes, *priorities, *skip; | |
4ed46869 KH |
3296 | { |
3297 | register unsigned char c; | |
d46c5b12 | 3298 | unsigned char *src = source, *src_end = source + src_bytes; |
66cfb530 | 3299 | unsigned int mask; |
d46c5b12 | 3300 | int i; |
4ed46869 KH |
3301 | |
3302 | /* At first, skip all ASCII characters and control characters except | |
3303 | for three ISO2022 specific control characters. */ | |
66cfb530 KH |
3304 | ascii_skip_code[ISO_CODE_SO] = 0; |
3305 | ascii_skip_code[ISO_CODE_SI] = 0; | |
3306 | ascii_skip_code[ISO_CODE_ESC] = 0; | |
3307 | ||
bcf26d6a | 3308 | label_loop_detect_coding: |
66cfb530 | 3309 | while (src < src_end && ascii_skip_code[*src]) src++; |
d46c5b12 | 3310 | *skip = src - source; |
4ed46869 KH |
3311 | |
3312 | if (src >= src_end) | |
3313 | /* We found nothing other than ASCII. There's nothing to do. */ | |
d46c5b12 | 3314 | return 0; |
4ed46869 | 3315 | |
8a8147d6 | 3316 | c = *src; |
4ed46869 KH |
3317 | /* The text seems to be encoded in some multilingual coding system. |
3318 | Now, try to find in which coding system the text is encoded. */ | |
3319 | if (c < 0x80) | |
bcf26d6a KH |
3320 | { |
3321 | /* i.e. (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) */ | |
3322 | /* C is an ISO2022 specific control code of C0. */ | |
3323 | mask = detect_coding_iso2022 (src, src_end); | |
1b2af4b0 | 3324 | if (mask == 0) |
d46c5b12 KH |
3325 | { |
3326 | /* No valid ISO2022 code follows C. Try again. */ | |
3327 | src++; | |
66cfb530 KH |
3328 | if (c == ISO_CODE_ESC) |
3329 | ascii_skip_code[ISO_CODE_ESC] = 1; | |
3330 | else | |
3331 | ascii_skip_code[ISO_CODE_SO] = ascii_skip_code[ISO_CODE_SI] = 1; | |
d46c5b12 KH |
3332 | goto label_loop_detect_coding; |
3333 | } | |
3334 | if (priorities) | |
3335 | goto label_return_highest_only; | |
bcf26d6a | 3336 | } |
d46c5b12 | 3337 | else |
c4825358 | 3338 | { |
d46c5b12 | 3339 | int try; |
4ed46869 | 3340 | |
d46c5b12 KH |
3341 | if (c < 0xA0) |
3342 | { | |
3343 | /* C is the first byte of SJIS character code, | |
3344 | or a leading-code of Emacs' internal format (emacs-mule). */ | |
3345 | try = CODING_CATEGORY_MASK_SJIS | CODING_CATEGORY_MASK_EMACS_MULE; | |
3346 | ||
3347 | /* Or, if C is a special latin extra code, | |
3348 | or is an ISO2022 specific control code of C1 (SS2 or SS3), | |
3349 | or is an ISO2022 control-sequence-introducer (CSI), | |
3350 | we should also consider the possibility of ISO2022 codings. */ | |
3351 | if ((VECTORP (Vlatin_extra_code_table) | |
3352 | && !NILP (XVECTOR (Vlatin_extra_code_table)->contents[c])) | |
3353 | || (c == ISO_CODE_SS2 || c == ISO_CODE_SS3) | |
3354 | || (c == ISO_CODE_CSI | |
3355 | && (src < src_end | |
3356 | && (*src == ']' | |
3357 | || ((*src == '0' || *src == '1' || *src == '2') | |
3358 | && src + 1 < src_end | |
3359 | && src[1] == ']'))))) | |
3360 | try |= (CODING_CATEGORY_MASK_ISO_8_ELSE | |
3361 | | CODING_CATEGORY_MASK_ISO_8BIT); | |
3362 | } | |
c4825358 | 3363 | else |
d46c5b12 KH |
3364 | /* C is a character of ISO2022 in graphic plane right, |
3365 | or a SJIS's 1-byte character code (i.e. JISX0201), | |
3366 | or the first byte of BIG5's 2-byte code. */ | |
3367 | try = (CODING_CATEGORY_MASK_ISO_8_ELSE | |
3368 | | CODING_CATEGORY_MASK_ISO_8BIT | |
3369 | | CODING_CATEGORY_MASK_SJIS | |
3370 | | CODING_CATEGORY_MASK_BIG5); | |
3371 | ||
1397dc18 KH |
3372 | /* Or, we may have to consider the possibility of CCL. */ |
3373 | if (coding_system_table[CODING_CATEGORY_IDX_CCL] | |
3374 | && (coding_system_table[CODING_CATEGORY_IDX_CCL] | |
3375 | ->spec.ccl.valid_codes)[c]) | |
3376 | try |= CODING_CATEGORY_MASK_CCL; | |
3377 | ||
d46c5b12 KH |
3378 | mask = 0; |
3379 | if (priorities) | |
3380 | { | |
3381 | for (i = 0; i < CODING_CATEGORY_IDX_MAX; i++) | |
3382 | { | |
5ab13dd0 | 3383 | if (priorities[i] & try & CODING_CATEGORY_MASK_ISO) |
d46c5b12 | 3384 | mask = detect_coding_iso2022 (src, src_end); |
5ab13dd0 | 3385 | else if (priorities[i] & try & CODING_CATEGORY_MASK_SJIS) |
d46c5b12 | 3386 | mask = detect_coding_sjis (src, src_end); |
5ab13dd0 | 3387 | else if (priorities[i] & try & CODING_CATEGORY_MASK_BIG5) |
d46c5b12 | 3388 | mask = detect_coding_big5 (src, src_end); |
5ab13dd0 | 3389 | else if (priorities[i] & try & CODING_CATEGORY_MASK_EMACS_MULE) |
d46c5b12 | 3390 | mask = detect_coding_emacs_mule (src, src_end); |
89fa8b36 | 3391 | else if (priorities[i] & try & CODING_CATEGORY_MASK_CCL) |
1397dc18 | 3392 | mask = detect_coding_ccl (src, src_end); |
5ab13dd0 RS |
3393 | else if (priorities[i] & CODING_CATEGORY_MASK_RAW_TEXT) |
3394 | mask = CODING_CATEGORY_MASK_RAW_TEXT; | |
3395 | else if (priorities[i] & CODING_CATEGORY_MASK_BINARY) | |
3396 | mask = CODING_CATEGORY_MASK_BINARY; | |
d46c5b12 KH |
3397 | if (mask) |
3398 | goto label_return_highest_only; | |
3399 | } | |
3400 | return CODING_CATEGORY_MASK_RAW_TEXT; | |
3401 | } | |
3402 | if (try & CODING_CATEGORY_MASK_ISO) | |
3403 | mask |= detect_coding_iso2022 (src, src_end); | |
3404 | if (try & CODING_CATEGORY_MASK_SJIS) | |
3405 | mask |= detect_coding_sjis (src, src_end); | |
3406 | if (try & CODING_CATEGORY_MASK_BIG5) | |
3407 | mask |= detect_coding_big5 (src, src_end); | |
3408 | if (try & CODING_CATEGORY_MASK_EMACS_MULE) | |
1397dc18 KH |
3409 | mask |= detect_coding_emacs_mule (src, src_end); |
3410 | if (try & CODING_CATEGORY_MASK_CCL) | |
3411 | mask |= detect_coding_ccl (src, src_end); | |
c4825358 | 3412 | } |
5ab13dd0 | 3413 | return (mask | CODING_CATEGORY_MASK_RAW_TEXT | CODING_CATEGORY_MASK_BINARY); |
d46c5b12 KH |
3414 | |
3415 | label_return_highest_only: | |
3416 | for (i = 0; i < CODING_CATEGORY_IDX_MAX; i++) | |
3417 | { | |
3418 | if (mask & priorities[i]) | |
3419 | return priorities[i]; | |
3420 | } | |
3421 | return CODING_CATEGORY_MASK_RAW_TEXT; | |
4ed46869 KH |
3422 | } |
3423 | ||
3424 | /* Detect how a text of length SRC_BYTES pointed by SRC is encoded. | |
3425 | The information of the detected coding system is set in CODING. */ | |
3426 | ||
3427 | void | |
3428 | detect_coding (coding, src, src_bytes) | |
3429 | struct coding_system *coding; | |
3430 | unsigned char *src; | |
3431 | int src_bytes; | |
3432 | { | |
d46c5b12 KH |
3433 | unsigned int idx; |
3434 | int skip, mask, i; | |
84d60297 | 3435 | Lisp_Object val; |
4ed46869 | 3436 | |
84d60297 | 3437 | val = Vcoding_category_list; |
66cfb530 | 3438 | mask = detect_coding_mask (src, src_bytes, coding_priorities, &skip); |
d46c5b12 | 3439 | coding->heading_ascii = skip; |
4ed46869 | 3440 | |
d46c5b12 KH |
3441 | if (!mask) return; |
3442 | ||
3443 | /* We found a single coding system of the highest priority in MASK. */ | |
3444 | idx = 0; | |
3445 | while (mask && ! (mask & 1)) mask >>= 1, idx++; | |
3446 | if (! mask) | |
3447 | idx = CODING_CATEGORY_IDX_RAW_TEXT; | |
4ed46869 | 3448 | |
d46c5b12 KH |
3449 | val = XSYMBOL (XVECTOR (Vcoding_category_table)->contents[idx])->value; |
3450 | ||
3451 | if (coding->eol_type != CODING_EOL_UNDECIDED) | |
27901516 | 3452 | { |
84d60297 | 3453 | Lisp_Object tmp; |
d46c5b12 | 3454 | |
84d60297 | 3455 | tmp = Fget (val, Qeol_type); |
d46c5b12 KH |
3456 | if (VECTORP (tmp)) |
3457 | val = XVECTOR (tmp)->contents[coding->eol_type]; | |
4ed46869 | 3458 | } |
d46c5b12 KH |
3459 | setup_coding_system (val, coding); |
3460 | /* Set this again because setup_coding_system reset this member. */ | |
3461 | coding->heading_ascii = skip; | |
4ed46869 KH |
3462 | } |
3463 | ||
d46c5b12 KH |
3464 | /* Detect how end-of-line of a text of length SRC_BYTES pointed by |
3465 | SOURCE is encoded. Return one of CODING_EOL_LF, CODING_EOL_CRLF, | |
3466 | CODING_EOL_CR, and CODING_EOL_UNDECIDED. | |
3467 | ||
3468 | How many non-eol characters are at the head is returned as *SKIP. */ | |
4ed46869 | 3469 | |
bc4bc72a RS |
3470 | #define MAX_EOL_CHECK_COUNT 3 |
3471 | ||
d46c5b12 KH |
3472 | static int |
3473 | detect_eol_type (source, src_bytes, skip) | |
3474 | unsigned char *source; | |
3475 | int src_bytes, *skip; | |
4ed46869 | 3476 | { |
d46c5b12 | 3477 | unsigned char *src = source, *src_end = src + src_bytes; |
4ed46869 | 3478 | unsigned char c; |
bc4bc72a RS |
3479 | int total = 0; /* How many end-of-lines are found so far. */ |
3480 | int eol_type = CODING_EOL_UNDECIDED; | |
3481 | int this_eol_type; | |
4ed46869 | 3482 | |
d46c5b12 KH |
3483 | *skip = 0; |
3484 | ||
bc4bc72a | 3485 | while (src < src_end && total < MAX_EOL_CHECK_COUNT) |
4ed46869 KH |
3486 | { |
3487 | c = *src++; | |
bc4bc72a | 3488 | if (c == '\n' || c == '\r') |
4ed46869 | 3489 | { |
d46c5b12 KH |
3490 | if (*skip == 0) |
3491 | *skip = src - 1 - source; | |
bc4bc72a RS |
3492 | total++; |
3493 | if (c == '\n') | |
3494 | this_eol_type = CODING_EOL_LF; | |
3495 | else if (src >= src_end || *src != '\n') | |
3496 | this_eol_type = CODING_EOL_CR; | |
4ed46869 | 3497 | else |
bc4bc72a RS |
3498 | this_eol_type = CODING_EOL_CRLF, src++; |
3499 | ||
3500 | if (eol_type == CODING_EOL_UNDECIDED) | |
3501 | /* This is the first end-of-line. */ | |
3502 | eol_type = this_eol_type; | |
3503 | else if (eol_type != this_eol_type) | |
d46c5b12 KH |
3504 | { |
3505 | /* The found type is different from what found before. */ | |
3506 | eol_type = CODING_EOL_INCONSISTENT; | |
3507 | break; | |
3508 | } | |
4ed46869 KH |
3509 | } |
3510 | } | |
bc4bc72a | 3511 | |
d46c5b12 KH |
3512 | if (*skip == 0) |
3513 | *skip = src_end - source; | |
85a02ca4 | 3514 | return eol_type; |
4ed46869 KH |
3515 | } |
3516 | ||
3517 | /* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC | |
3518 | is encoded. If it detects an appropriate format of end-of-line, it | |
3519 | sets the information in *CODING. */ | |
3520 | ||
3521 | void | |
3522 | detect_eol (coding, src, src_bytes) | |
3523 | struct coding_system *coding; | |
3524 | unsigned char *src; | |
3525 | int src_bytes; | |
3526 | { | |
4608c386 | 3527 | Lisp_Object val; |
d46c5b12 KH |
3528 | int skip; |
3529 | int eol_type = detect_eol_type (src, src_bytes, &skip); | |
3530 | ||
3531 | if (coding->heading_ascii > skip) | |
3532 | coding->heading_ascii = skip; | |
3533 | else | |
3534 | skip = coding->heading_ascii; | |
4ed46869 | 3535 | |
0ef69138 | 3536 | if (eol_type == CODING_EOL_UNDECIDED) |
4ed46869 | 3537 | return; |
27901516 KH |
3538 | if (eol_type == CODING_EOL_INCONSISTENT) |
3539 | { | |
3540 | #if 0 | |
3541 | /* This code is suppressed until we find a better way to | |
992f23f2 | 3542 | distinguish raw text file and binary file. */ |
27901516 KH |
3543 | |
3544 | /* If we have already detected that the coding is raw-text, the | |
3545 | coding should actually be no-conversion. */ | |
3546 | if (coding->type == coding_type_raw_text) | |
3547 | { | |
3548 | setup_coding_system (Qno_conversion, coding); | |
3549 | return; | |
3550 | } | |
3551 | /* Else, let's decode only text code anyway. */ | |
3552 | #endif /* 0 */ | |
1b2af4b0 | 3553 | eol_type = CODING_EOL_LF; |
27901516 KH |
3554 | } |
3555 | ||
4608c386 | 3556 | val = Fget (coding->symbol, Qeol_type); |
4ed46869 | 3557 | if (VECTORP (val) && XVECTOR (val)->size == 3) |
d46c5b12 KH |
3558 | { |
3559 | setup_coding_system (XVECTOR (val)->contents[eol_type], coding); | |
3560 | coding->heading_ascii = skip; | |
3561 | } | |
3562 | } | |
3563 | ||
3564 | #define CONVERSION_BUFFER_EXTRA_ROOM 256 | |
3565 | ||
3566 | #define DECODING_BUFFER_MAG(coding) \ | |
3567 | (coding->type == coding_type_iso2022 \ | |
3568 | ? 3 \ | |
3569 | : ((coding->type == coding_type_sjis || coding->type == coding_type_big5) \ | |
3570 | ? 2 \ | |
3571 | : (coding->type == coding_type_raw_text \ | |
3572 | ? 1 \ | |
3573 | : (coding->type == coding_type_ccl \ | |
3574 | ? coding->spec.ccl.decoder.buf_magnification \ | |
3575 | : 2)))) | |
3576 | ||
3577 | /* Return maximum size (bytes) of a buffer enough for decoding | |
3578 | SRC_BYTES of text encoded in CODING. */ | |
3579 | ||
3580 | int | |
3581 | decoding_buffer_size (coding, src_bytes) | |
3582 | struct coding_system *coding; | |
3583 | int src_bytes; | |
3584 | { | |
3585 | return (src_bytes * DECODING_BUFFER_MAG (coding) | |
3586 | + CONVERSION_BUFFER_EXTRA_ROOM); | |
3587 | } | |
3588 | ||
3589 | /* Return maximum size (bytes) of a buffer enough for encoding | |
3590 | SRC_BYTES of text to CODING. */ | |
3591 | ||
3592 | int | |
3593 | encoding_buffer_size (coding, src_bytes) | |
3594 | struct coding_system *coding; | |
3595 | int src_bytes; | |
3596 | { | |
3597 | int magnification; | |
3598 | ||
3599 | if (coding->type == coding_type_ccl) | |
3600 | magnification = coding->spec.ccl.encoder.buf_magnification; | |
3601 | else | |
3602 | magnification = 3; | |
3603 | ||
3604 | return (src_bytes * magnification + CONVERSION_BUFFER_EXTRA_ROOM); | |
3605 | } | |
3606 | ||
3607 | #ifndef MINIMUM_CONVERSION_BUFFER_SIZE | |
3608 | #define MINIMUM_CONVERSION_BUFFER_SIZE 1024 | |
3609 | #endif | |
3610 | ||
3611 | char *conversion_buffer; | |
3612 | int conversion_buffer_size; | |
3613 | ||
3614 | /* Return a pointer to a SIZE bytes of buffer to be used for encoding | |
3615 | or decoding. Sufficient memory is allocated automatically. If we | |
3616 | run out of memory, return NULL. */ | |
3617 | ||
3618 | char * | |
3619 | get_conversion_buffer (size) | |
3620 | int size; | |
3621 | { | |
3622 | if (size > conversion_buffer_size) | |
3623 | { | |
3624 | char *buf; | |
3625 | int real_size = conversion_buffer_size * 2; | |
3626 | ||
3627 | while (real_size < size) real_size *= 2; | |
3628 | buf = (char *) xmalloc (real_size); | |
3629 | xfree (conversion_buffer); | |
3630 | conversion_buffer = buf; | |
3631 | conversion_buffer_size = real_size; | |
3632 | } | |
3633 | return conversion_buffer; | |
3634 | } | |
3635 | ||
3636 | int | |
3637 | ccl_coding_driver (coding, source, destination, src_bytes, dst_bytes, encodep) | |
3638 | struct coding_system *coding; | |
3639 | unsigned char *source, *destination; | |
3640 | int src_bytes, dst_bytes, encodep; | |
3641 | { | |
3642 | struct ccl_program *ccl | |
3643 | = encodep ? &coding->spec.ccl.encoder : &coding->spec.ccl.decoder; | |
3644 | int result; | |
3645 | ||
ae9ff118 | 3646 | ccl->last_block = coding->mode & CODING_MODE_LAST_BLOCK; |
7b179c2d | 3647 | |
d46c5b12 KH |
3648 | coding->produced = ccl_driver (ccl, source, destination, |
3649 | src_bytes, dst_bytes, &(coding->consumed)); | |
69f76525 KH |
3650 | coding->produced_char |
3651 | = multibyte_chars_in_text (destination, coding->produced); | |
3652 | coding->consumed_char | |
3653 | = multibyte_chars_in_text (source, coding->consumed); | |
3654 | ||
d46c5b12 KH |
3655 | switch (ccl->status) |
3656 | { | |
3657 | case CCL_STAT_SUSPEND_BY_SRC: | |
3658 | result = CODING_FINISH_INSUFFICIENT_SRC; | |
3659 | break; | |
3660 | case CCL_STAT_SUSPEND_BY_DST: | |
3661 | result = CODING_FINISH_INSUFFICIENT_DST; | |
3662 | break; | |
9864ebce KH |
3663 | case CCL_STAT_QUIT: |
3664 | case CCL_STAT_INVALID_CMD: | |
3665 | result = CODING_FINISH_INTERRUPT; | |
3666 | break; | |
d46c5b12 KH |
3667 | default: |
3668 | result = CODING_FINISH_NORMAL; | |
3669 | break; | |
3670 | } | |
3671 | return result; | |
4ed46869 KH |
3672 | } |
3673 | ||
3674 | /* See "GENERAL NOTES about `decode_coding_XXX ()' functions". Before | |
3675 | decoding, it may detect coding system and format of end-of-line if | |
3676 | those are not yet decided. */ | |
3677 | ||
3678 | int | |
d46c5b12 | 3679 | decode_coding (coding, source, destination, src_bytes, dst_bytes) |
4ed46869 KH |
3680 | struct coding_system *coding; |
3681 | unsigned char *source, *destination; | |
3682 | int src_bytes, dst_bytes; | |
4ed46869 | 3683 | { |
d46c5b12 | 3684 | int result; |
4ed46869 | 3685 | |
d4e57bcd KH |
3686 | if (src_bytes <= 0 |
3687 | && ! (coding->mode & CODING_MODE_LAST_BLOCK | |
3688 | && CODING_REQUIRE_FLUSHING (coding))) | |
4ed46869 | 3689 | { |
d46c5b12 KH |
3690 | coding->produced = coding->produced_char = 0; |
3691 | coding->consumed = coding->consumed_char = 0; | |
fb88bf2d | 3692 | coding->fake_multibyte = 0; |
d46c5b12 | 3693 | return CODING_FINISH_NORMAL; |
4ed46869 KH |
3694 | } |
3695 | ||
0ef69138 | 3696 | if (coding->type == coding_type_undecided) |
4ed46869 KH |
3697 | detect_coding (coding, source, src_bytes); |
3698 | ||
0ef69138 | 3699 | if (coding->eol_type == CODING_EOL_UNDECIDED) |
4ed46869 KH |
3700 | detect_eol (coding, source, src_bytes); |
3701 | ||
4ed46869 KH |
3702 | switch (coding->type) |
3703 | { | |
0ef69138 KH |
3704 | case coding_type_emacs_mule: |
3705 | case coding_type_undecided: | |
27901516 | 3706 | case coding_type_raw_text: |
4ed46869 | 3707 | if (coding->eol_type == CODING_EOL_LF |
0ef69138 | 3708 | || coding->eol_type == CODING_EOL_UNDECIDED) |
4ed46869 | 3709 | goto label_no_conversion; |
d46c5b12 | 3710 | result = decode_eol (coding, source, destination, src_bytes, dst_bytes); |
4ed46869 KH |
3711 | break; |
3712 | ||
3713 | case coding_type_sjis: | |
d46c5b12 KH |
3714 | result = decode_coding_sjis_big5 (coding, source, destination, |
3715 | src_bytes, dst_bytes, 1); | |
4ed46869 KH |
3716 | break; |
3717 | ||
3718 | case coding_type_iso2022: | |
d46c5b12 KH |
3719 | result = decode_coding_iso2022 (coding, source, destination, |
3720 | src_bytes, dst_bytes); | |
4ed46869 KH |
3721 | break; |
3722 | ||
3723 | case coding_type_big5: | |
d46c5b12 KH |
3724 | result = decode_coding_sjis_big5 (coding, source, destination, |
3725 | src_bytes, dst_bytes, 0); | |
4ed46869 KH |
3726 | break; |
3727 | ||
3728 | case coding_type_ccl: | |
d46c5b12 KH |
3729 | result = ccl_coding_driver (coding, source, destination, |
3730 | src_bytes, dst_bytes, 0); | |
3731 | break; | |
3732 | ||
3733 | default: /* i.e. case coding_type_no_conversion: */ | |
3734 | label_no_conversion: | |
3735 | if (dst_bytes && src_bytes > dst_bytes) | |
3736 | { | |
3737 | coding->produced = dst_bytes; | |
3738 | result = CODING_FINISH_INSUFFICIENT_DST; | |
3739 | } | |
3740 | else | |
3741 | { | |
3742 | coding->produced = src_bytes; | |
3743 | result = CODING_FINISH_NORMAL; | |
3744 | } | |
3745 | if (dst_bytes) | |
3746 | bcopy (source, destination, coding->produced); | |
3747 | else | |
3748 | safe_bcopy (source, destination, coding->produced); | |
fb88bf2d | 3749 | coding->fake_multibyte = 1; |
d46c5b12 KH |
3750 | coding->consumed |
3751 | = coding->consumed_char = coding->produced_char = coding->produced; | |
4ed46869 KH |
3752 | break; |
3753 | } | |
3754 | ||
d46c5b12 | 3755 | return result; |
4ed46869 KH |
3756 | } |
3757 | ||
3758 | /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". */ | |
3759 | ||
3760 | int | |
d46c5b12 | 3761 | encode_coding (coding, source, destination, src_bytes, dst_bytes) |
4ed46869 KH |
3762 | struct coding_system *coding; |
3763 | unsigned char *source, *destination; | |
3764 | int src_bytes, dst_bytes; | |
4ed46869 | 3765 | { |
d46c5b12 | 3766 | int result; |
4ed46869 | 3767 | |
d4e57bcd KH |
3768 | if (src_bytes <= 0 |
3769 | && ! (coding->mode & CODING_MODE_LAST_BLOCK | |
3770 | && CODING_REQUIRE_FLUSHING (coding))) | |
4ed46869 | 3771 | { |
d46c5b12 KH |
3772 | coding->produced = coding->produced_char = 0; |
3773 | coding->consumed = coding->consumed_char = 0; | |
fb88bf2d | 3774 | coding->fake_multibyte = 0; |
d46c5b12 KH |
3775 | return CODING_FINISH_NORMAL; |
3776 | } | |
4ed46869 | 3777 | |
d46c5b12 KH |
3778 | switch (coding->type) |
3779 | { | |
0ef69138 KH |
3780 | case coding_type_emacs_mule: |
3781 | case coding_type_undecided: | |
27901516 | 3782 | case coding_type_raw_text: |
4ed46869 | 3783 | if (coding->eol_type == CODING_EOL_LF |
0ef69138 | 3784 | || coding->eol_type == CODING_EOL_UNDECIDED) |
4ed46869 | 3785 | goto label_no_conversion; |
d46c5b12 | 3786 | result = encode_eol (coding, source, destination, src_bytes, dst_bytes); |
4ed46869 KH |
3787 | break; |
3788 | ||
3789 | case coding_type_sjis: | |
d46c5b12 KH |
3790 | result = encode_coding_sjis_big5 (coding, source, destination, |
3791 | src_bytes, dst_bytes, 1); | |
4ed46869 KH |
3792 | break; |
3793 | ||
3794 | case coding_type_iso2022: | |
d46c5b12 KH |
3795 | result = encode_coding_iso2022 (coding, source, destination, |
3796 | src_bytes, dst_bytes); | |
4ed46869 KH |
3797 | break; |
3798 | ||
3799 | case coding_type_big5: | |
d46c5b12 KH |
3800 | result = encode_coding_sjis_big5 (coding, source, destination, |
3801 | src_bytes, dst_bytes, 0); | |
4ed46869 KH |
3802 | break; |
3803 | ||
3804 | case coding_type_ccl: | |
d46c5b12 KH |
3805 | result = ccl_coding_driver (coding, source, destination, |
3806 | src_bytes, dst_bytes, 1); | |
3807 | break; | |
3808 | ||
3809 | default: /* i.e. case coding_type_no_conversion: */ | |
3810 | label_no_conversion: | |
3811 | if (dst_bytes && src_bytes > dst_bytes) | |
3812 | { | |
3813 | coding->produced = dst_bytes; | |
3814 | result = CODING_FINISH_INSUFFICIENT_DST; | |
3815 | } | |
3816 | else | |
3817 | { | |
3818 | coding->produced = src_bytes; | |
3819 | result = CODING_FINISH_NORMAL; | |
3820 | } | |
3821 | if (dst_bytes) | |
3822 | bcopy (source, destination, coding->produced); | |
3823 | else | |
3824 | safe_bcopy (source, destination, coding->produced); | |
3825 | if (coding->mode & CODING_MODE_SELECTIVE_DISPLAY) | |
3826 | { | |
3827 | unsigned char *p = destination, *pend = p + coding->produced; | |
3828 | while (p < pend) | |
3829 | if (*p++ == '\015') p[-1] = '\n'; | |
3830 | } | |
fb88bf2d | 3831 | coding->fake_multibyte = 1; |
d46c5b12 KH |
3832 | coding->consumed |
3833 | = coding->consumed_char = coding->produced_char = coding->produced; | |
4ed46869 KH |
3834 | break; |
3835 | } | |
3836 | ||
d46c5b12 | 3837 | return result; |
4ed46869 KH |
3838 | } |
3839 | ||
fb88bf2d KH |
3840 | /* Scan text in the region between *BEG and *END (byte positions), |
3841 | skip characters which we don't have to decode by coding system | |
3842 | CODING at the head and tail, then set *BEG and *END to the region | |
3843 | of the text we actually have to convert. The caller should move | |
3844 | the gap out of the region in advance. | |
4ed46869 | 3845 | |
d46c5b12 KH |
3846 | If STR is not NULL, *BEG and *END are indices into STR. */ |
3847 | ||
3848 | static void | |
3849 | shrink_decoding_region (beg, end, coding, str) | |
3850 | int *beg, *end; | |
3851 | struct coding_system *coding; | |
3852 | unsigned char *str; | |
3853 | { | |
fb88bf2d | 3854 | unsigned char *begp_orig, *begp, *endp_orig, *endp, c; |
d46c5b12 | 3855 | int eol_conversion; |
88993dfd | 3856 | Lisp_Object translation_table; |
d46c5b12 KH |
3857 | |
3858 | if (coding->type == coding_type_ccl | |
3859 | || coding->type == coding_type_undecided | |
3860 | || !NILP (coding->post_read_conversion)) | |
3861 | { | |
3862 | /* We can't skip any data. */ | |
3863 | return; | |
3864 | } | |
3865 | else if (coding->type == coding_type_no_conversion) | |
3866 | { | |
fb88bf2d KH |
3867 | /* We need no conversion, but don't have to skip any data here. |
3868 | Decoding routine handles them effectively anyway. */ | |
d46c5b12 KH |
3869 | return; |
3870 | } | |
3871 | ||
88993dfd KH |
3872 | translation_table = coding->translation_table_for_decode; |
3873 | if (NILP (translation_table) && !NILP (Venable_character_translation)) | |
3874 | translation_table = Vstandard_translation_table_for_decode; | |
3875 | if (CHAR_TABLE_P (translation_table)) | |
3876 | { | |
3877 | int i; | |
3878 | for (i = 0; i < 128; i++) | |
3879 | if (!NILP (CHAR_TABLE_REF (translation_table, i))) | |
3880 | break; | |
3881 | if (i < 128) | |
3882 | /* Some ASCII character should be tranlsated. We give up | |
3883 | shrinking. */ | |
3884 | return; | |
3885 | } | |
3886 | ||
aa60dea6 KH |
3887 | eol_conversion = (coding->eol_type != CODING_EOL_LF); |
3888 | ||
3889 | if ((! eol_conversion) && (coding->heading_ascii >= 0)) | |
d46c5b12 KH |
3890 | /* Detection routine has already found how much we can skip at the |
3891 | head. */ | |
3892 | *beg += coding->heading_ascii; | |
3893 | ||
3894 | if (str) | |
3895 | { | |
3896 | begp_orig = begp = str + *beg; | |
3897 | endp_orig = endp = str + *end; | |
3898 | } | |
3899 | else | |
3900 | { | |
fb88bf2d | 3901 | begp_orig = begp = BYTE_POS_ADDR (*beg); |
d46c5b12 KH |
3902 | endp_orig = endp = begp + *end - *beg; |
3903 | } | |
3904 | ||
d46c5b12 KH |
3905 | switch (coding->type) |
3906 | { | |
3907 | case coding_type_emacs_mule: | |
3908 | case coding_type_raw_text: | |
3909 | if (eol_conversion) | |
3910 | { | |
3911 | if (coding->heading_ascii < 0) | |
fb88bf2d | 3912 | while (begp < endp && *begp != '\r' && *begp < 0x80) begp++; |
ee59c65f | 3913 | while (begp < endp && endp[-1] != '\r' && endp[-1] < 0x80) |
fb88bf2d | 3914 | endp--; |
ee59c65f RS |
3915 | /* Do not consider LF as ascii if preceded by CR, since that |
3916 | confuses eol decoding. */ | |
3917 | if (begp < endp && endp < endp_orig && endp[-1] == '\r' && endp[0] == '\n') | |
3918 | endp++; | |
d46c5b12 KH |
3919 | } |
3920 | else | |
3921 | begp = endp; | |
3922 | break; | |
3923 | ||
3924 | case coding_type_sjis: | |
3925 | case coding_type_big5: | |
3926 | /* We can skip all ASCII characters at the head. */ | |
3927 | if (coding->heading_ascii < 0) | |
3928 | { | |
3929 | if (eol_conversion) | |
de9d083c | 3930 | while (begp < endp && *begp < 0x80 && *begp != '\r') begp++; |
d46c5b12 KH |
3931 | else |
3932 | while (begp < endp && *begp < 0x80) begp++; | |
3933 | } | |
3934 | /* We can skip all ASCII characters at the tail except for the | |
3935 | second byte of SJIS or BIG5 code. */ | |
3936 | if (eol_conversion) | |
de9d083c | 3937 | while (begp < endp && endp[-1] < 0x80 && endp[-1] != '\r') endp--; |
d46c5b12 KH |
3938 | else |
3939 | while (begp < endp && endp[-1] < 0x80) endp--; | |
ee59c65f RS |
3940 | /* Do not consider LF as ascii if preceded by CR, since that |
3941 | confuses eol decoding. */ | |
3942 | if (begp < endp && endp < endp_orig && endp[-1] == '\r' && endp[0] == '\n') | |
3943 | endp++; | |
d46c5b12 KH |
3944 | if (begp < endp && endp < endp_orig && endp[-1] >= 0x80) |
3945 | endp++; | |
3946 | break; | |
3947 | ||
3948 | default: /* i.e. case coding_type_iso2022: */ | |
3949 | if (coding->heading_ascii < 0) | |
3950 | { | |
d46c5b12 KH |
3951 | /* We can skip all ASCII characters at the head except for a |
3952 | few control codes. */ | |
3953 | while (begp < endp && (c = *begp) < 0x80 | |
3954 | && c != ISO_CODE_CR && c != ISO_CODE_SO | |
3955 | && c != ISO_CODE_SI && c != ISO_CODE_ESC | |
3956 | && (!eol_conversion || c != ISO_CODE_LF)) | |
3957 | begp++; | |
3958 | } | |
3959 | switch (coding->category_idx) | |
3960 | { | |
3961 | case CODING_CATEGORY_IDX_ISO_8_1: | |
3962 | case CODING_CATEGORY_IDX_ISO_8_2: | |
3963 | /* We can skip all ASCII characters at the tail. */ | |
3964 | if (eol_conversion) | |
de9d083c | 3965 | while (begp < endp && (c = endp[-1]) < 0x80 && c != '\r') endp--; |
d46c5b12 KH |
3966 | else |
3967 | while (begp < endp && endp[-1] < 0x80) endp--; | |
ee59c65f RS |
3968 | /* Do not consider LF as ascii if preceded by CR, since that |
3969 | confuses eol decoding. */ | |
3970 | if (begp < endp && endp < endp_orig && endp[-1] == '\r' && endp[0] == '\n') | |
3971 | endp++; | |
d46c5b12 KH |
3972 | break; |
3973 | ||
3974 | case CODING_CATEGORY_IDX_ISO_7: | |
3975 | case CODING_CATEGORY_IDX_ISO_7_TIGHT: | |
de79a6a5 KH |
3976 | { |
3977 | /* We can skip all charactes at the tail except for 8-bit | |
3978 | codes and ESC and the following 2-byte at the tail. */ | |
3979 | unsigned char *eight_bit = NULL; | |
3980 | ||
3981 | if (eol_conversion) | |
3982 | while (begp < endp | |
3983 | && (c = endp[-1]) != ISO_CODE_ESC && c != '\r') | |
3984 | { | |
3985 | if (!eight_bit && c & 0x80) eight_bit = endp; | |
3986 | endp--; | |
3987 | } | |
3988 | else | |
3989 | while (begp < endp | |
3990 | && (c = endp[-1]) != ISO_CODE_ESC) | |
3991 | { | |
3992 | if (!eight_bit && c & 0x80) eight_bit = endp; | |
3993 | endp--; | |
3994 | } | |
3995 | /* Do not consider LF as ascii if preceded by CR, since that | |
3996 | confuses eol decoding. */ | |
3997 | if (begp < endp && endp < endp_orig | |
3998 | && endp[-1] == '\r' && endp[0] == '\n') | |
3999 | endp++; | |
4000 | if (begp < endp && endp[-1] == ISO_CODE_ESC) | |
4001 | { | |
4002 | if (endp + 1 < endp_orig && end[0] == '(' && end[1] == 'B') | |
4003 | /* This is an ASCII designation sequence. We can | |
4004 | surely skip the tail. But, if we have | |
4005 | encountered an 8-bit code, skip only the codes | |
4006 | after that. */ | |
4007 | endp = eight_bit ? eight_bit : endp + 2; | |
4008 | else | |
4009 | /* Hmmm, we can't skip the tail. */ | |
4010 | endp = endp_orig; | |
4011 | } | |
4012 | else if (eight_bit) | |
4013 | endp = eight_bit; | |
4014 | } | |
d46c5b12 KH |
4015 | } |
4016 | } | |
4017 | *beg += begp - begp_orig; | |
4018 | *end += endp - endp_orig; | |
4019 | return; | |
4020 | } | |
4021 | ||
4022 | /* Like shrink_decoding_region but for encoding. */ | |
4023 | ||
4024 | static void | |
4025 | shrink_encoding_region (beg, end, coding, str) | |
4026 | int *beg, *end; | |
4027 | struct coding_system *coding; | |
4028 | unsigned char *str; | |
4029 | { | |
4030 | unsigned char *begp_orig, *begp, *endp_orig, *endp; | |
4031 | int eol_conversion; | |
88993dfd | 4032 | Lisp_Object translation_table; |
d46c5b12 KH |
4033 | |
4034 | if (coding->type == coding_type_ccl) | |
4035 | /* We can't skip any data. */ | |
4036 | return; | |
4037 | else if (coding->type == coding_type_no_conversion) | |
4038 | { | |
4039 | /* We need no conversion. */ | |
4040 | *beg = *end; | |
4041 | return; | |
4042 | } | |
4043 | ||
88993dfd KH |
4044 | translation_table = coding->translation_table_for_encode; |
4045 | if (NILP (translation_table) && !NILP (Venable_character_translation)) | |
4046 | translation_table = Vstandard_translation_table_for_encode; | |
4047 | if (CHAR_TABLE_P (translation_table)) | |
4048 | { | |
4049 | int i; | |
4050 | for (i = 0; i < 128; i++) | |
4051 | if (!NILP (CHAR_TABLE_REF (translation_table, i))) | |
4052 | break; | |
4053 | if (i < 128) | |
4054 | /* Some ASCII character should be tranlsated. We give up | |
4055 | shrinking. */ | |
4056 | return; | |
4057 | } | |
4058 | ||
d46c5b12 KH |
4059 | if (str) |
4060 | { | |
4061 | begp_orig = begp = str + *beg; | |
4062 | endp_orig = endp = str + *end; | |
4063 | } | |
4064 | else | |
4065 | { | |
fb88bf2d | 4066 | begp_orig = begp = BYTE_POS_ADDR (*beg); |
d46c5b12 KH |
4067 | endp_orig = endp = begp + *end - *beg; |
4068 | } | |
4069 | ||
4070 | eol_conversion = (coding->eol_type == CODING_EOL_CR | |
4071 | || coding->eol_type == CODING_EOL_CRLF); | |
4072 | ||
4073 | /* Here, we don't have to check coding->pre_write_conversion because | |
4074 | the caller is expected to have handled it already. */ | |
4075 | switch (coding->type) | |
4076 | { | |
4077 | case coding_type_undecided: | |
4078 | case coding_type_emacs_mule: | |
4079 | case coding_type_raw_text: | |
4080 | if (eol_conversion) | |
4081 | { | |
4082 | while (begp < endp && *begp != '\n') begp++; | |
4083 | while (begp < endp && endp[-1] != '\n') endp--; | |
4084 | } | |
4085 | else | |
4086 | begp = endp; | |
4087 | break; | |
4088 | ||
4089 | case coding_type_iso2022: | |
4090 | if (coding->flags & CODING_FLAG_ISO_DESIGNATE_AT_BOL) | |
4091 | { | |
4092 | unsigned char *bol = begp; | |
4093 | while (begp < endp && *begp < 0x80) | |
4094 | { | |
4095 | begp++; | |
4096 | if (begp[-1] == '\n') | |
4097 | bol = begp; | |
4098 | } | |
4099 | begp = bol; | |
4100 | goto label_skip_tail; | |
4101 | } | |
4102 | /* fall down ... */ | |
4103 | ||
4104 | default: | |
4105 | /* We can skip all ASCII characters at the head and tail. */ | |
4106 | if (eol_conversion) | |
4107 | while (begp < endp && *begp < 0x80 && *begp != '\n') begp++; | |
4108 | else | |
4109 | while (begp < endp && *begp < 0x80) begp++; | |
4110 | label_skip_tail: | |
4111 | if (eol_conversion) | |
4112 | while (begp < endp && endp[-1] < 0x80 && endp[-1] != '\n') endp--; | |
4113 | else | |
4114 | while (begp < endp && *(endp - 1) < 0x80) endp--; | |
4115 | break; | |
4116 | } | |
4117 | ||
4118 | *beg += begp - begp_orig; | |
4119 | *end += endp - endp_orig; | |
4120 | return; | |
4121 | } | |
4122 | ||
88993dfd KH |
4123 | /* As shrinking conversion region requires some overhead, we don't try |
4124 | shrinking if the length of conversion region is less than this | |
4125 | value. */ | |
4126 | static int shrink_conversion_region_threshhold = 1024; | |
4127 | ||
4128 | #define SHRINK_CONVERSION_REGION(beg, end, coding, str, encodep) \ | |
4129 | do { \ | |
4130 | if (*(end) - *(beg) > shrink_conversion_region_threshhold) \ | |
4131 | { \ | |
4132 | if (encodep) shrink_encoding_region (beg, end, coding, str); \ | |
4133 | else shrink_decoding_region (beg, end, coding, str); \ | |
4134 | } \ | |
4135 | } while (0) | |
4136 | ||
d46c5b12 | 4137 | /* Decode (if ENCODEP is zero) or encode (if ENCODEP is nonzero) the |
fb88bf2d KH |
4138 | text from FROM to TO (byte positions are FROM_BYTE and TO_BYTE) by |
4139 | coding system CODING, and return the status code of code conversion | |
4140 | (currently, this value has no meaning). | |
4141 | ||
4142 | How many characters (and bytes) are converted to how many | |
4143 | characters (and bytes) are recorded in members of the structure | |
4144 | CODING. | |
d46c5b12 | 4145 | |
6e44253b | 4146 | If REPLACE is nonzero, we do various things as if the original text |
d46c5b12 | 4147 | is deleted and a new text is inserted. See the comments in |
6e44253b | 4148 | replace_range (insdel.c) to know what we are doing. */ |
4ed46869 KH |
4149 | |
4150 | int | |
6e44253b KH |
4151 | code_convert_region (from, from_byte, to, to_byte, coding, encodep, replace) |
4152 | int from, from_byte, to, to_byte, encodep, replace; | |
4ed46869 | 4153 | struct coding_system *coding; |
4ed46869 | 4154 | { |
fb88bf2d KH |
4155 | int len = to - from, len_byte = to_byte - from_byte; |
4156 | int require, inserted, inserted_byte; | |
12410ef1 | 4157 | int head_skip, tail_skip, total_skip; |
84d60297 | 4158 | Lisp_Object saved_coding_symbol; |
fb88bf2d KH |
4159 | int multibyte = !NILP (current_buffer->enable_multibyte_characters); |
4160 | int first = 1; | |
4161 | int fake_multibyte = 0; | |
4162 | unsigned char *src, *dst; | |
84d60297 RS |
4163 | Lisp_Object deletion; |
4164 | ||
4165 | deletion = Qnil; | |
4166 | saved_coding_symbol = Qnil; | |
d46c5b12 | 4167 | |
83fa074f KH |
4168 | if (from < PT && PT < to) |
4169 | SET_PT_BOTH (from, from_byte); | |
4170 | ||
6e44253b | 4171 | if (replace) |
d46c5b12 | 4172 | { |
fb88bf2d KH |
4173 | int saved_from = from; |
4174 | ||
d46c5b12 | 4175 | prepare_to_modify_buffer (from, to, &from); |
fb88bf2d KH |
4176 | if (saved_from != from) |
4177 | { | |
4178 | to = from + len; | |
4179 | if (multibyte) | |
4180 | from_byte = CHAR_TO_BYTE (from), to_byte = CHAR_TO_BYTE (to); | |
4181 | else | |
4182 | from_byte = from, to_byte = to; | |
4183 | len_byte = to_byte - from_byte; | |
4184 | } | |
d46c5b12 | 4185 | } |
d46c5b12 KH |
4186 | |
4187 | if (! encodep && CODING_REQUIRE_DETECTION (coding)) | |
4188 | { | |
12410ef1 | 4189 | /* We must detect encoding of text and eol format. */ |
d46c5b12 KH |
4190 | |
4191 | if (from < GPT && to > GPT) | |
4192 | move_gap_both (from, from_byte); | |
4193 | if (coding->type == coding_type_undecided) | |
4194 | { | |
fb88bf2d | 4195 | detect_coding (coding, BYTE_POS_ADDR (from_byte), len_byte); |
d46c5b12 | 4196 | if (coding->type == coding_type_undecided) |
12410ef1 KH |
4197 | /* It seems that the text contains only ASCII, but we |
4198 | should not left it undecided because the deeper | |
4199 | decoding routine (decode_coding) tries to detect the | |
4200 | encodings again in vain. */ | |
d46c5b12 KH |
4201 | coding->type = coding_type_emacs_mule; |
4202 | } | |
4203 | if (coding->eol_type == CODING_EOL_UNDECIDED) | |
4204 | { | |
4205 | saved_coding_symbol = coding->symbol; | |
4206 | detect_eol (coding, BYTE_POS_ADDR (from_byte), len_byte); | |
4207 | if (coding->eol_type == CODING_EOL_UNDECIDED) | |
4208 | coding->eol_type = CODING_EOL_LF; | |
4209 | /* We had better recover the original eol format if we | |
4210 | encounter an inconsitent eol format while decoding. */ | |
4211 | coding->mode |= CODING_MODE_INHIBIT_INCONSISTENT_EOL; | |
4212 | } | |
4213 | } | |
4214 | ||
fb88bf2d KH |
4215 | coding->consumed_char = len, coding->consumed = len_byte; |
4216 | ||
d46c5b12 KH |
4217 | if (encodep |
4218 | ? ! CODING_REQUIRE_ENCODING (coding) | |
4219 | : ! CODING_REQUIRE_DECODING (coding)) | |
fb88bf2d KH |
4220 | { |
4221 | coding->produced = len_byte; | |
12410ef1 KH |
4222 | if (multibyte |
4223 | && ! replace | |
4224 | /* See the comment of the member heading_ascii in coding.h. */ | |
4225 | && coding->heading_ascii < len_byte) | |
fb88bf2d | 4226 | { |
6e44253b KH |
4227 | /* We still may have to combine byte at the head and the |
4228 | tail of the text in the region. */ | |
12410ef1 | 4229 | if (from < GPT && GPT < to) |
6e44253b | 4230 | move_gap_both (to, to_byte); |
12410ef1 KH |
4231 | len = multibyte_chars_in_text (BYTE_POS_ADDR (from_byte), len_byte); |
4232 | adjust_after_insert (from, from_byte, to, to_byte, len); | |
4233 | coding->produced_char = len; | |
fb88bf2d KH |
4234 | } |
4235 | else | |
68e3a8f1 AS |
4236 | { |
4237 | if (!replace) | |
4238 | adjust_after_insert (from, from_byte, to, to_byte, len_byte); | |
4239 | coding->produced_char = len_byte; | |
4240 | } | |
fb88bf2d KH |
4241 | return 0; |
4242 | } | |
d46c5b12 KH |
4243 | |
4244 | /* Now we convert the text. */ | |
4245 | ||
4246 | /* For encoding, we must process pre-write-conversion in advance. */ | |
4247 | if (encodep | |
d46c5b12 KH |
4248 | && ! NILP (coding->pre_write_conversion) |
4249 | && SYMBOLP (coding->pre_write_conversion) | |
4250 | && ! NILP (Ffboundp (coding->pre_write_conversion))) | |
4251 | { | |
2b4f9037 KH |
4252 | /* The function in pre-write-conversion may put a new text in a |
4253 | new buffer. */ | |
d46c5b12 KH |
4254 | struct buffer *prev = current_buffer, *new; |
4255 | ||
b39f748c AS |
4256 | call2 (coding->pre_write_conversion, |
4257 | make_number (from), make_number (to)); | |
d46c5b12 KH |
4258 | if (current_buffer != prev) |
4259 | { | |
4260 | len = ZV - BEGV; | |
4261 | new = current_buffer; | |
4262 | set_buffer_internal_1 (prev); | |
ddbc19ff | 4263 | del_range_2 (from, from_byte, to, to_byte); |
d46c5b12 KH |
4264 | insert_from_buffer (new, BEG, len, 0); |
4265 | to = from + len; | |
fb88bf2d | 4266 | to_byte = multibyte ? CHAR_TO_BYTE (to) : to; |
d46c5b12 KH |
4267 | len_byte = to_byte - from_byte; |
4268 | } | |
4269 | } | |
4270 | ||
12410ef1 KH |
4271 | if (replace) |
4272 | deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1); | |
4273 | ||
d46c5b12 | 4274 | /* Try to skip the heading and tailing ASCIIs. */ |
12410ef1 KH |
4275 | { |
4276 | int from_byte_orig = from_byte, to_byte_orig = to_byte; | |
4277 | ||
4278 | if (from < GPT && GPT < to) | |
4279 | move_gap_both (from, from_byte); | |
88993dfd | 4280 | SHRINK_CONVERSION_REGION (&from_byte, &to_byte, coding, NULL, encodep); |
d4e57bcd KH |
4281 | if (from_byte == to_byte |
4282 | && ! (coding->mode & CODING_MODE_LAST_BLOCK | |
4283 | && CODING_REQUIRE_FLUSHING (coding))) | |
12410ef1 KH |
4284 | { |
4285 | coding->produced = len_byte; | |
4286 | coding->produced_char = multibyte ? len : len_byte; | |
4287 | if (!replace) | |
4288 | /* We must record and adjust for this new text now. */ | |
4289 | adjust_after_insert (from, from_byte_orig, to, to_byte_orig, len); | |
4290 | return 0; | |
4291 | } | |
fb88bf2d | 4292 | |
12410ef1 KH |
4293 | head_skip = from_byte - from_byte_orig; |
4294 | tail_skip = to_byte_orig - to_byte; | |
4295 | total_skip = head_skip + tail_skip; | |
4296 | from += head_skip; | |
4297 | to -= tail_skip; | |
4298 | len -= total_skip; len_byte -= total_skip; | |
4299 | } | |
d46c5b12 | 4300 | |
88993dfd | 4301 | /* The code conversion routine can not preserve text properties for |
55d8d769 KH |
4302 | now. So, we must remove all text properties in the region. |
4303 | Here, we must suppress all modification hooks. */ | |
88993dfd | 4304 | if (replace) |
55d8d769 KH |
4305 | { |
4306 | int saved_inhibit_modification_hooks = inhibit_modification_hooks; | |
4307 | inhibit_modification_hooks = 1; | |
4308 | Fset_text_properties (make_number (from), make_number (to), Qnil, Qnil); | |
4309 | inhibit_modification_hooks = saved_inhibit_modification_hooks; | |
4310 | } | |
88993dfd | 4311 | |
fb88bf2d KH |
4312 | /* For converion, we must put the gap before the text in addition to |
4313 | making the gap larger for efficient decoding. The required gap | |
4314 | size starts from 2000 which is the magic number used in make_gap. | |
4315 | But, after one batch of conversion, it will be incremented if we | |
4316 | find that it is not enough . */ | |
d46c5b12 KH |
4317 | require = 2000; |
4318 | ||
4319 | if (GAP_SIZE < require) | |
4320 | make_gap (require - GAP_SIZE); | |
4321 | move_gap_both (from, from_byte); | |
4322 | ||
d46c5b12 | 4323 | inserted = inserted_byte = 0; |
fb88bf2d KH |
4324 | src = GAP_END_ADDR, dst = GPT_ADDR; |
4325 | ||
4326 | GAP_SIZE += len_byte; | |
4327 | ZV -= len; | |
4328 | Z -= len; | |
4329 | ZV_BYTE -= len_byte; | |
4330 | Z_BYTE -= len_byte; | |
4331 | ||
f2558efd KH |
4332 | if (GPT - BEG < beg_unchanged) |
4333 | beg_unchanged = GPT - BEG; | |
4334 | if (Z - GPT < end_unchanged) | |
4335 | end_unchanged = Z - GPT; | |
4336 | ||
d46c5b12 KH |
4337 | for (;;) |
4338 | { | |
fb88bf2d | 4339 | int result; |
d46c5b12 KH |
4340 | |
4341 | /* The buffer memory is changed from: | |
fb88bf2d KH |
4342 | +--------+converted-text+---------+-------original-text------+---+ |
4343 | |<-from->|<--inserted-->|---------|<-----------len---------->|---| | |
4344 | |<------------------- GAP_SIZE -------------------->| */ | |
d46c5b12 | 4345 | if (encodep) |
fb88bf2d | 4346 | result = encode_coding (coding, src, dst, len_byte, 0); |
d46c5b12 | 4347 | else |
fb88bf2d | 4348 | result = decode_coding (coding, src, dst, len_byte, 0); |
d46c5b12 KH |
4349 | /* to: |
4350 | +--------+-------converted-text--------+--+---original-text--+---+ | |
fb88bf2d KH |
4351 | |<-from->|<--inserted-->|<--produced-->|--|<-(len-consumed)->|---| |
4352 | |<------------------- GAP_SIZE -------------------->| */ | |
4353 | if (coding->fake_multibyte) | |
4354 | fake_multibyte = 1; | |
d46c5b12 | 4355 | |
fb88bf2d KH |
4356 | if (!encodep && !multibyte) |
4357 | coding->produced_char = coding->produced; | |
d46c5b12 KH |
4358 | inserted += coding->produced_char; |
4359 | inserted_byte += coding->produced; | |
d46c5b12 | 4360 | len_byte -= coding->consumed; |
fb88bf2d KH |
4361 | src += coding->consumed; |
4362 | dst += inserted_byte; | |
d46c5b12 | 4363 | |
9864ebce KH |
4364 | if (result == CODING_FINISH_NORMAL) |
4365 | { | |
4366 | src += len_byte; | |
4367 | break; | |
4368 | } | |
d46c5b12 KH |
4369 | if (! encodep && result == CODING_FINISH_INCONSISTENT_EOL) |
4370 | { | |
fb88bf2d | 4371 | unsigned char *pend = dst, *p = pend - inserted_byte; |
d46c5b12 KH |
4372 | |
4373 | /* Encode LFs back to the original eol format (CR or CRLF). */ | |
4374 | if (coding->eol_type == CODING_EOL_CR) | |
4375 | { | |
4376 | while (p < pend) if (*p++ == '\n') p[-1] = '\r'; | |
4377 | } | |
4378 | else | |
4379 | { | |
d46c5b12 KH |
4380 | int count = 0; |
4381 | ||
fb88bf2d KH |
4382 | while (p < pend) if (*p++ == '\n') count++; |
4383 | if (src - dst < count) | |
d46c5b12 | 4384 | { |
fb88bf2d KH |
4385 | /* We don't have sufficient room for putting LFs |
4386 | back to CRLF. We must record converted and | |
4387 | not-yet-converted text back to the buffer | |
4388 | content, enlarge the gap, then record them out of | |
4389 | the buffer contents again. */ | |
4390 | int add = len_byte + inserted_byte; | |
4391 | ||
4392 | GAP_SIZE -= add; | |
4393 | ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add; | |
4394 | GPT += inserted_byte; GPT_BYTE += inserted_byte; | |
4395 | make_gap (count - GAP_SIZE); | |
4396 | GAP_SIZE += add; | |
4397 | ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add; | |
4398 | GPT -= inserted_byte; GPT_BYTE -= inserted_byte; | |
4399 | /* Don't forget to update SRC, DST, and PEND. */ | |
4400 | src = GAP_END_ADDR - len_byte; | |
4401 | dst = GPT_ADDR + inserted_byte; | |
4402 | pend = dst; | |
d46c5b12 | 4403 | } |
d46c5b12 KH |
4404 | inserted += count; |
4405 | inserted_byte += count; | |
fb88bf2d KH |
4406 | coding->produced += count; |
4407 | p = dst = pend + count; | |
4408 | while (count) | |
4409 | { | |
4410 | *--p = *--pend; | |
4411 | if (*p == '\n') count--, *--p = '\r'; | |
4412 | } | |
d46c5b12 KH |
4413 | } |
4414 | ||
4415 | /* Suppress eol-format conversion in the further conversion. */ | |
4416 | coding->eol_type = CODING_EOL_LF; | |
4417 | ||
4418 | /* Restore the original symbol. */ | |
4419 | coding->symbol = saved_coding_symbol; | |
fb88bf2d KH |
4420 | |
4421 | continue; | |
d46c5b12 KH |
4422 | } |
4423 | if (len_byte <= 0) | |
4424 | break; | |
4425 | if (result == CODING_FINISH_INSUFFICIENT_SRC) | |
4426 | { | |
4427 | /* The source text ends in invalid codes. Let's just | |
4428 | make them valid buffer contents, and finish conversion. */ | |
fb88bf2d | 4429 | inserted += len_byte; |
d46c5b12 | 4430 | inserted_byte += len_byte; |
fb88bf2d | 4431 | while (len_byte--) |
ee59c65f | 4432 | *dst++ = *src++; |
fb88bf2d | 4433 | fake_multibyte = 1; |
d46c5b12 KH |
4434 | break; |
4435 | } | |
9864ebce KH |
4436 | if (result == CODING_FINISH_INTERRUPT) |
4437 | { | |
4438 | /* The conversion procedure was interrupted by a user. */ | |
4439 | fake_multibyte = 1; | |
4440 | break; | |
4441 | } | |
4442 | /* Now RESULT == CODING_FINISH_INSUFFICIENT_DST */ | |
4443 | if (coding->consumed < 1) | |
4444 | { | |
4445 | /* It's quite strange to require more memory without | |
4446 | consuming any bytes. Perhaps CCL program bug. */ | |
4447 | fake_multibyte = 1; | |
4448 | break; | |
4449 | } | |
fb88bf2d KH |
4450 | if (first) |
4451 | { | |
4452 | /* We have just done the first batch of conversion which was | |
4453 | stoped because of insufficient gap. Let's reconsider the | |
4454 | required gap size (i.e. SRT - DST) now. | |
4455 | ||
4456 | We have converted ORIG bytes (== coding->consumed) into | |
4457 | NEW bytes (coding->produced). To convert the remaining | |
4458 | LEN bytes, we may need REQUIRE bytes of gap, where: | |
4459 | REQUIRE + LEN_BYTE = LEN_BYTE * (NEW / ORIG) | |
4460 | REQUIRE = LEN_BYTE * (NEW - ORIG) / ORIG | |
4461 | Here, we are sure that NEW >= ORIG. */ | |
6e44253b KH |
4462 | float ratio = coding->produced - coding->consumed; |
4463 | ratio /= coding->consumed; | |
4464 | require = len_byte * ratio; | |
fb88bf2d KH |
4465 | first = 0; |
4466 | } | |
4467 | if ((src - dst) < (require + 2000)) | |
4468 | { | |
4469 | /* See the comment above the previous call of make_gap. */ | |
4470 | int add = len_byte + inserted_byte; | |
4471 | ||
4472 | GAP_SIZE -= add; | |
4473 | ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add; | |
4474 | GPT += inserted_byte; GPT_BYTE += inserted_byte; | |
4475 | make_gap (require + 2000); | |
4476 | GAP_SIZE += add; | |
4477 | ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add; | |
4478 | GPT -= inserted_byte; GPT_BYTE -= inserted_byte; | |
4479 | /* Don't forget to update SRC, DST. */ | |
4480 | src = GAP_END_ADDR - len_byte; | |
4481 | dst = GPT_ADDR + inserted_byte; | |
4482 | } | |
d46c5b12 | 4483 | } |
fb88bf2d KH |
4484 | if (src - dst > 0) *dst = 0; /* Put an anchor. */ |
4485 | ||
2b4f9037 | 4486 | if (multibyte |
88993dfd KH |
4487 | && (encodep |
4488 | || fake_multibyte | |
4489 | || (to - from) != (to_byte - from_byte))) | |
2b4f9037 | 4490 | inserted = multibyte_chars_in_text (GPT_ADDR, inserted_byte); |
7553d0e1 | 4491 | |
12410ef1 KH |
4492 | /* If we have shrinked the conversion area, adjust it now. */ |
4493 | if (total_skip > 0) | |
4494 | { | |
4495 | if (tail_skip > 0) | |
4496 | safe_bcopy (GAP_END_ADDR, GPT_ADDR + inserted_byte, tail_skip); | |
4497 | inserted += total_skip; inserted_byte += total_skip; | |
4498 | GAP_SIZE += total_skip; | |
4499 | GPT -= head_skip; GPT_BYTE -= head_skip; | |
4500 | ZV -= total_skip; ZV_BYTE -= total_skip; | |
4501 | Z -= total_skip; Z_BYTE -= total_skip; | |
4502 | from -= head_skip; from_byte -= head_skip; | |
4503 | to += tail_skip; to_byte += tail_skip; | |
4504 | } | |
4505 | ||
4506 | adjust_after_replace (from, from_byte, deletion, inserted, inserted_byte); | |
4ed46869 | 4507 | |
2b4f9037 | 4508 | if (! encodep && ! NILP (coding->post_read_conversion)) |
d46c5b12 | 4509 | { |
2b4f9037 KH |
4510 | Lisp_Object val; |
4511 | int orig_inserted = inserted, pos = PT; | |
4ed46869 | 4512 | |
2b4f9037 KH |
4513 | if (from != pos) |
4514 | temp_set_point_both (current_buffer, from, from_byte); | |
4515 | val = call1 (coding->post_read_conversion, make_number (inserted)); | |
4516 | if (! NILP (val)) | |
d46c5b12 | 4517 | { |
2b4f9037 KH |
4518 | CHECK_NUMBER (val, 0); |
4519 | inserted = XFASTINT (val); | |
d46c5b12 | 4520 | } |
2b4f9037 KH |
4521 | if (pos >= from + orig_inserted) |
4522 | temp_set_point (current_buffer, pos + (inserted - orig_inserted)); | |
d46c5b12 | 4523 | } |
4ed46869 | 4524 | |
2b4f9037 KH |
4525 | signal_after_change (from, to - from, inserted); |
4526 | ||
fb88bf2d | 4527 | { |
12410ef1 KH |
4528 | coding->consumed = to_byte - from_byte; |
4529 | coding->consumed_char = to - from; | |
4530 | coding->produced = inserted_byte; | |
4531 | coding->produced_char = inserted; | |
fb88bf2d | 4532 | } |
7553d0e1 | 4533 | |
fb88bf2d | 4534 | return 0; |
d46c5b12 KH |
4535 | } |
4536 | ||
4537 | Lisp_Object | |
4538 | code_convert_string (str, coding, encodep, nocopy) | |
4539 | Lisp_Object str; | |
4ed46869 | 4540 | struct coding_system *coding; |
d46c5b12 | 4541 | int encodep, nocopy; |
4ed46869 | 4542 | { |
d46c5b12 KH |
4543 | int len; |
4544 | char *buf; | |
fc932ac6 RS |
4545 | int from = 0, to = XSTRING (str)->size; |
4546 | int to_byte = STRING_BYTES (XSTRING (str)); | |
d46c5b12 | 4547 | struct gcpro gcpro1; |
84d60297 | 4548 | Lisp_Object saved_coding_symbol; |
d46c5b12 | 4549 | int result; |
4ed46869 | 4550 | |
84d60297 | 4551 | saved_coding_symbol = Qnil; |
d46c5b12 KH |
4552 | if (encodep && !NILP (coding->pre_write_conversion) |
4553 | || !encodep && !NILP (coding->post_read_conversion)) | |
4554 | { | |
4555 | /* Since we have to call Lisp functions which assume target text | |
4556 | is in a buffer, after setting a temporary buffer, call | |
4557 | code_convert_region. */ | |
4558 | int count = specpdl_ptr - specpdl; | |
4559 | struct buffer *prev = current_buffer; | |
4560 | ||
4561 | record_unwind_protect (Fset_buffer, Fcurrent_buffer ()); | |
4562 | temp_output_buffer_setup (" *code-converting-work*"); | |
4563 | set_buffer_internal (XBUFFER (Vstandard_output)); | |
4564 | if (encodep) | |
4565 | insert_from_string (str, 0, 0, to, to_byte, 0); | |
4566 | else | |
4567 | { | |
4568 | /* We must insert the contents of STR as is without | |
4569 | unibyte<->multibyte conversion. */ | |
4570 | current_buffer->enable_multibyte_characters = Qnil; | |
4571 | insert_from_string (str, 0, 0, to_byte, to_byte, 0); | |
4572 | current_buffer->enable_multibyte_characters = Qt; | |
4573 | } | |
fb88bf2d | 4574 | code_convert_region (BEGV, BEGV_BYTE, ZV, ZV_BYTE, coding, encodep, 1); |
d46c5b12 KH |
4575 | if (encodep) |
4576 | /* We must return the buffer contents as unibyte string. */ | |
4577 | current_buffer->enable_multibyte_characters = Qnil; | |
4578 | str = make_buffer_string (BEGV, ZV, 0); | |
4579 | set_buffer_internal (prev); | |
4580 | return unbind_to (count, str); | |
4581 | } | |
4ed46869 | 4582 | |
d46c5b12 KH |
4583 | if (! encodep && CODING_REQUIRE_DETECTION (coding)) |
4584 | { | |
4585 | /* See the comments in code_convert_region. */ | |
4586 | if (coding->type == coding_type_undecided) | |
4587 | { | |
4588 | detect_coding (coding, XSTRING (str)->data, to_byte); | |
4589 | if (coding->type == coding_type_undecided) | |
4590 | coding->type = coding_type_emacs_mule; | |
4591 | } | |
4592 | if (coding->eol_type == CODING_EOL_UNDECIDED) | |
4593 | { | |
4594 | saved_coding_symbol = coding->symbol; | |
4595 | detect_eol (coding, XSTRING (str)->data, to_byte); | |
4596 | if (coding->eol_type == CODING_EOL_UNDECIDED) | |
4597 | coding->eol_type = CODING_EOL_LF; | |
4598 | /* We had better recover the original eol format if we | |
4599 | encounter an inconsitent eol format while decoding. */ | |
4600 | coding->mode |= CODING_MODE_INHIBIT_INCONSISTENT_EOL; | |
4601 | } | |
4602 | } | |
4ed46869 | 4603 | |
d46c5b12 KH |
4604 | if (encodep |
4605 | ? ! CODING_REQUIRE_ENCODING (coding) | |
4606 | : ! CODING_REQUIRE_DECODING (coding)) | |
4607 | from = to_byte; | |
4608 | else | |
4609 | { | |
4610 | /* Try to skip the heading and tailing ASCIIs. */ | |
88993dfd KH |
4611 | SHRINK_CONVERSION_REGION (&from, &to_byte, coding, XSTRING (str)->data, |
4612 | encodep); | |
d46c5b12 | 4613 | } |
de79a6a5 | 4614 | if (from == to_byte) |
d46c5b12 | 4615 | return (nocopy ? str : Fcopy_sequence (str)); |
4ed46869 | 4616 | |
d46c5b12 KH |
4617 | if (encodep) |
4618 | len = encoding_buffer_size (coding, to_byte - from); | |
4619 | else | |
4620 | len = decoding_buffer_size (coding, to_byte - from); | |
fc932ac6 | 4621 | len += from + STRING_BYTES (XSTRING (str)) - to_byte; |
d46c5b12 KH |
4622 | GCPRO1 (str); |
4623 | buf = get_conversion_buffer (len); | |
4624 | UNGCPRO; | |
4ed46869 | 4625 | |
d46c5b12 KH |
4626 | if (from > 0) |
4627 | bcopy (XSTRING (str)->data, buf, from); | |
4628 | result = (encodep | |
4629 | ? encode_coding (coding, XSTRING (str)->data + from, | |
4630 | buf + from, to_byte - from, len) | |
4631 | : decode_coding (coding, XSTRING (str)->data + from, | |
f30cc612 | 4632 | buf + from, to_byte - from, len)); |
d46c5b12 | 4633 | if (! encodep && result == CODING_FINISH_INCONSISTENT_EOL) |
4ed46869 | 4634 | { |
d46c5b12 KH |
4635 | /* We simple try to decode the whole string again but without |
4636 | eol-conversion this time. */ | |
4637 | coding->eol_type = CODING_EOL_LF; | |
4638 | coding->symbol = saved_coding_symbol; | |
4639 | return code_convert_string (str, coding, encodep, nocopy); | |
4ed46869 | 4640 | } |
d46c5b12 KH |
4641 | |
4642 | bcopy (XSTRING (str)->data + to_byte, buf + from + coding->produced, | |
fc932ac6 | 4643 | STRING_BYTES (XSTRING (str)) - to_byte); |
d46c5b12 | 4644 | |
fc932ac6 | 4645 | len = from + STRING_BYTES (XSTRING (str)) - to_byte; |
d46c5b12 KH |
4646 | if (encodep) |
4647 | str = make_unibyte_string (buf, len + coding->produced); | |
4648 | else | |
826bfb8b KH |
4649 | { |
4650 | int chars= (coding->fake_multibyte | |
4651 | ? multibyte_chars_in_text (buf + from, coding->produced) | |
4652 | : coding->produced_char); | |
4653 | str = make_multibyte_string (buf, len + chars, len + coding->produced); | |
4654 | } | |
4655 | ||
d46c5b12 | 4656 | return str; |
4ed46869 KH |
4657 | } |
4658 | ||
4659 | \f | |
4660 | #ifdef emacs | |
1397dc18 | 4661 | /*** 8. Emacs Lisp library functions ***/ |
4ed46869 | 4662 | |
4ed46869 KH |
4663 | DEFUN ("coding-system-p", Fcoding_system_p, Scoding_system_p, 1, 1, 0, |
4664 | "Return t if OBJECT is nil or a coding-system.\n\ | |
3a73fa5d RS |
4665 | See the documentation of `make-coding-system' for information\n\ |
4666 | about coding-system objects.") | |
4ed46869 KH |
4667 | (obj) |
4668 | Lisp_Object obj; | |
4669 | { | |
4608c386 KH |
4670 | if (NILP (obj)) |
4671 | return Qt; | |
4672 | if (!SYMBOLP (obj)) | |
4673 | return Qnil; | |
4674 | /* Get coding-spec vector for OBJ. */ | |
4675 | obj = Fget (obj, Qcoding_system); | |
4676 | return ((VECTORP (obj) && XVECTOR (obj)->size == 5) | |
4677 | ? Qt : Qnil); | |
4ed46869 KH |
4678 | } |
4679 | ||
9d991de8 RS |
4680 | DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system, |
4681 | Sread_non_nil_coding_system, 1, 1, 0, | |
e0e989f6 | 4682 | "Read a coding system from the minibuffer, prompting with string PROMPT.") |
4ed46869 KH |
4683 | (prompt) |
4684 | Lisp_Object prompt; | |
4685 | { | |
e0e989f6 | 4686 | Lisp_Object val; |
9d991de8 RS |
4687 | do |
4688 | { | |
4608c386 KH |
4689 | val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil, |
4690 | Qt, Qnil, Qcoding_system_history, Qnil, Qnil); | |
9d991de8 RS |
4691 | } |
4692 | while (XSTRING (val)->size == 0); | |
e0e989f6 | 4693 | return (Fintern (val, Qnil)); |
4ed46869 KH |
4694 | } |
4695 | ||
9b787f3e RS |
4696 | DEFUN ("read-coding-system", Fread_coding_system, Sread_coding_system, 1, 2, 0, |
4697 | "Read a coding system from the minibuffer, prompting with string PROMPT.\n\ | |
4698 | If the user enters null input, return second argument DEFAULT-CODING-SYSTEM.") | |
4699 | (prompt, default_coding_system) | |
4700 | Lisp_Object prompt, default_coding_system; | |
4ed46869 | 4701 | { |
f44d27ce | 4702 | Lisp_Object val; |
9b787f3e RS |
4703 | if (SYMBOLP (default_coding_system)) |
4704 | XSETSTRING (default_coding_system, XSYMBOL (default_coding_system)->name); | |
4608c386 | 4705 | val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil, |
9b787f3e RS |
4706 | Qt, Qnil, Qcoding_system_history, |
4707 | default_coding_system, Qnil); | |
e0e989f6 | 4708 | return (XSTRING (val)->size == 0 ? Qnil : Fintern (val, Qnil)); |
4ed46869 KH |
4709 | } |
4710 | ||
4711 | DEFUN ("check-coding-system", Fcheck_coding_system, Scheck_coding_system, | |
4712 | 1, 1, 0, | |
4713 | "Check validity of CODING-SYSTEM.\n\ | |
3a73fa5d RS |
4714 | If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.\n\ |
4715 | It is valid if it is a symbol with a non-nil `coding-system' property.\n\ | |
4ed46869 KH |
4716 | The value of property should be a vector of length 5.") |
4717 | (coding_system) | |
4718 | Lisp_Object coding_system; | |
4719 | { | |
4720 | CHECK_SYMBOL (coding_system, 0); | |
4721 | if (!NILP (Fcoding_system_p (coding_system))) | |
4722 | return coding_system; | |
4723 | while (1) | |
02ba4723 | 4724 | Fsignal (Qcoding_system_error, Fcons (coding_system, Qnil)); |
4ed46869 | 4725 | } |
3a73fa5d | 4726 | \f |
d46c5b12 KH |
4727 | Lisp_Object |
4728 | detect_coding_system (src, src_bytes, highest) | |
4729 | unsigned char *src; | |
4730 | int src_bytes, highest; | |
4ed46869 KH |
4731 | { |
4732 | int coding_mask, eol_type; | |
d46c5b12 KH |
4733 | Lisp_Object val, tmp; |
4734 | int dummy; | |
4ed46869 | 4735 | |
d46c5b12 KH |
4736 | coding_mask = detect_coding_mask (src, src_bytes, NULL, &dummy); |
4737 | eol_type = detect_eol_type (src, src_bytes, &dummy); | |
4738 | if (eol_type == CODING_EOL_INCONSISTENT) | |
25b02698 | 4739 | eol_type = CODING_EOL_UNDECIDED; |
4ed46869 | 4740 | |
d46c5b12 | 4741 | if (!coding_mask) |
4ed46869 | 4742 | { |
27901516 | 4743 | val = Qundecided; |
d46c5b12 | 4744 | if (eol_type != CODING_EOL_UNDECIDED) |
4ed46869 | 4745 | { |
f44d27ce RS |
4746 | Lisp_Object val2; |
4747 | val2 = Fget (Qundecided, Qeol_type); | |
4ed46869 KH |
4748 | if (VECTORP (val2)) |
4749 | val = XVECTOR (val2)->contents[eol_type]; | |
4750 | } | |
80e803b4 | 4751 | return (highest ? val : Fcons (val, Qnil)); |
4ed46869 | 4752 | } |
4ed46869 | 4753 | |
d46c5b12 KH |
4754 | /* At first, gather possible coding systems in VAL. */ |
4755 | val = Qnil; | |
4756 | for (tmp = Vcoding_category_list; !NILP (tmp); tmp = XCONS (tmp)->cdr) | |
4ed46869 | 4757 | { |
d46c5b12 KH |
4758 | int idx |
4759 | = XFASTINT (Fget (XCONS (tmp)->car, Qcoding_category_index)); | |
4760 | if (coding_mask & (1 << idx)) | |
4ed46869 | 4761 | { |
d46c5b12 KH |
4762 | val = Fcons (Fsymbol_value (XCONS (tmp)->car), val); |
4763 | if (highest) | |
4764 | break; | |
4ed46869 KH |
4765 | } |
4766 | } | |
d46c5b12 KH |
4767 | if (!highest) |
4768 | val = Fnreverse (val); | |
4ed46869 | 4769 | |
65059037 | 4770 | /* Then, replace the elements with subsidiary coding systems. */ |
d46c5b12 | 4771 | for (tmp = val; !NILP (tmp); tmp = XCONS (tmp)->cdr) |
4ed46869 | 4772 | { |
65059037 RS |
4773 | if (eol_type != CODING_EOL_UNDECIDED |
4774 | && eol_type != CODING_EOL_INCONSISTENT) | |
4ed46869 | 4775 | { |
d46c5b12 KH |
4776 | Lisp_Object eol; |
4777 | eol = Fget (XCONS (tmp)->car, Qeol_type); | |
4778 | if (VECTORP (eol)) | |
4779 | XCONS (tmp)->car = XVECTOR (eol)->contents[eol_type]; | |
4ed46869 KH |
4780 | } |
4781 | } | |
d46c5b12 KH |
4782 | return (highest ? XCONS (val)->car : val); |
4783 | } | |
4ed46869 | 4784 | |
d46c5b12 KH |
4785 | DEFUN ("detect-coding-region", Fdetect_coding_region, Sdetect_coding_region, |
4786 | 2, 3, 0, | |
4787 | "Detect coding system of the text in the region between START and END.\n\ | |
4788 | Return a list of possible coding systems ordered by priority.\n\ | |
4789 | \n\ | |
80e803b4 KH |
4790 | If only ASCII characters are found, it returns a list of single element\n\ |
4791 | `undecided' or its subsidiary coding system according to a detected\n\ | |
4792 | end-of-line format.\n\ | |
d46c5b12 KH |
4793 | \n\ |
4794 | If optional argument HIGHEST is non-nil, return the coding system of\n\ | |
4795 | highest priority.") | |
4796 | (start, end, highest) | |
4797 | Lisp_Object start, end, highest; | |
4798 | { | |
4799 | int from, to; | |
4800 | int from_byte, to_byte; | |
6289dd10 | 4801 | |
d46c5b12 KH |
4802 | CHECK_NUMBER_COERCE_MARKER (start, 0); |
4803 | CHECK_NUMBER_COERCE_MARKER (end, 1); | |
4ed46869 | 4804 | |
d46c5b12 KH |
4805 | validate_region (&start, &end); |
4806 | from = XINT (start), to = XINT (end); | |
4807 | from_byte = CHAR_TO_BYTE (from); | |
4808 | to_byte = CHAR_TO_BYTE (to); | |
6289dd10 | 4809 | |
d46c5b12 KH |
4810 | if (from < GPT && to >= GPT) |
4811 | move_gap_both (to, to_byte); | |
4ed46869 | 4812 | |
d46c5b12 KH |
4813 | return detect_coding_system (BYTE_POS_ADDR (from_byte), |
4814 | to_byte - from_byte, | |
4815 | !NILP (highest)); | |
4816 | } | |
6289dd10 | 4817 | |
d46c5b12 KH |
4818 | DEFUN ("detect-coding-string", Fdetect_coding_string, Sdetect_coding_string, |
4819 | 1, 2, 0, | |
4820 | "Detect coding system of the text in STRING.\n\ | |
4821 | Return a list of possible coding systems ordered by priority.\n\ | |
4822 | \n\ | |
80e803b4 KH |
4823 | If only ASCII characters are found, it returns a list of single element\n\ |
4824 | `undecided' or its subsidiary coding system according to a detected\n\ | |
4825 | end-of-line format.\n\ | |
d46c5b12 KH |
4826 | \n\ |
4827 | If optional argument HIGHEST is non-nil, return the coding system of\n\ | |
4828 | highest priority.") | |
4829 | (string, highest) | |
4830 | Lisp_Object string, highest; | |
4831 | { | |
4832 | CHECK_STRING (string, 0); | |
4ed46869 | 4833 | |
d46c5b12 | 4834 | return detect_coding_system (XSTRING (string)->data, |
fc932ac6 | 4835 | STRING_BYTES (XSTRING (string)), |
d46c5b12 | 4836 | !NILP (highest)); |
4ed46869 KH |
4837 | } |
4838 | ||
4031e2bf KH |
4839 | Lisp_Object |
4840 | code_convert_region1 (start, end, coding_system, encodep) | |
d46c5b12 | 4841 | Lisp_Object start, end, coding_system; |
4031e2bf | 4842 | int encodep; |
3a73fa5d RS |
4843 | { |
4844 | struct coding_system coding; | |
4031e2bf | 4845 | int from, to, len; |
3a73fa5d | 4846 | |
d46c5b12 KH |
4847 | CHECK_NUMBER_COERCE_MARKER (start, 0); |
4848 | CHECK_NUMBER_COERCE_MARKER (end, 1); | |
3a73fa5d RS |
4849 | CHECK_SYMBOL (coding_system, 2); |
4850 | ||
d46c5b12 KH |
4851 | validate_region (&start, &end); |
4852 | from = XFASTINT (start); | |
4853 | to = XFASTINT (end); | |
4854 | ||
3a73fa5d | 4855 | if (NILP (coding_system)) |
d46c5b12 KH |
4856 | return make_number (to - from); |
4857 | ||
3a73fa5d | 4858 | if (setup_coding_system (Fcheck_coding_system (coding_system), &coding) < 0) |
d46c5b12 | 4859 | error ("Invalid coding system: %s", XSYMBOL (coding_system)->name->data); |
3a73fa5d | 4860 | |
d46c5b12 | 4861 | coding.mode |= CODING_MODE_LAST_BLOCK; |
fb88bf2d KH |
4862 | code_convert_region (from, CHAR_TO_BYTE (from), to, CHAR_TO_BYTE (to), |
4863 | &coding, encodep, 1); | |
f072a3e8 | 4864 | Vlast_coding_system_used = coding.symbol; |
fb88bf2d | 4865 | return make_number (coding.produced_char); |
4031e2bf KH |
4866 | } |
4867 | ||
4868 | DEFUN ("decode-coding-region", Fdecode_coding_region, Sdecode_coding_region, | |
4869 | 3, 3, "r\nzCoding system: ", | |
4870 | "Decode the current region by specified coding system.\n\ | |
4871 | When called from a program, takes three arguments:\n\ | |
4872 | START, END, and CODING-SYSTEM. START and END are buffer positions.\n\ | |
f072a3e8 RS |
4873 | This function sets `last-coding-system-used' to the precise coding system\n\ |
4874 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is\n\ | |
4875 | not fully specified.)\n\ | |
4876 | It returns the length of the decoded text.") | |
4031e2bf KH |
4877 | (start, end, coding_system) |
4878 | Lisp_Object start, end, coding_system; | |
4879 | { | |
4880 | return code_convert_region1 (start, end, coding_system, 0); | |
3a73fa5d RS |
4881 | } |
4882 | ||
4883 | DEFUN ("encode-coding-region", Fencode_coding_region, Sencode_coding_region, | |
4884 | 3, 3, "r\nzCoding system: ", | |
d46c5b12 | 4885 | "Encode the current region by specified coding system.\n\ |
3a73fa5d | 4886 | When called from a program, takes three arguments:\n\ |
d46c5b12 | 4887 | START, END, and CODING-SYSTEM. START and END are buffer positions.\n\ |
f072a3e8 RS |
4888 | This function sets `last-coding-system-used' to the precise coding system\n\ |
4889 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is\n\ | |
4890 | not fully specified.)\n\ | |
4891 | It returns the length of the encoded text.") | |
d46c5b12 KH |
4892 | (start, end, coding_system) |
4893 | Lisp_Object start, end, coding_system; | |
3a73fa5d | 4894 | { |
4031e2bf KH |
4895 | return code_convert_region1 (start, end, coding_system, 1); |
4896 | } | |
3a73fa5d | 4897 | |
4031e2bf KH |
4898 | Lisp_Object |
4899 | code_convert_string1 (string, coding_system, nocopy, encodep) | |
4900 | Lisp_Object string, coding_system, nocopy; | |
4901 | int encodep; | |
4902 | { | |
4903 | struct coding_system coding; | |
3a73fa5d | 4904 | |
4031e2bf KH |
4905 | CHECK_STRING (string, 0); |
4906 | CHECK_SYMBOL (coding_system, 1); | |
4ed46869 | 4907 | |
d46c5b12 | 4908 | if (NILP (coding_system)) |
4031e2bf | 4909 | return (NILP (nocopy) ? Fcopy_sequence (string) : string); |
4ed46869 | 4910 | |
d46c5b12 KH |
4911 | if (setup_coding_system (Fcheck_coding_system (coding_system), &coding) < 0) |
4912 | error ("Invalid coding system: %s", XSYMBOL (coding_system)->name->data); | |
5f1cd180 | 4913 | |
d46c5b12 | 4914 | coding.mode |= CODING_MODE_LAST_BLOCK; |
f072a3e8 | 4915 | Vlast_coding_system_used = coding.symbol; |
4031e2bf | 4916 | return code_convert_string (string, &coding, encodep, !NILP (nocopy)); |
4ed46869 KH |
4917 | } |
4918 | ||
4ed46869 | 4919 | DEFUN ("decode-coding-string", Fdecode_coding_string, Sdecode_coding_string, |
e0e989f6 KH |
4920 | 2, 3, 0, |
4921 | "Decode STRING which is encoded in CODING-SYSTEM, and return the result.\n\ | |
fe487a71 | 4922 | Optional arg NOCOPY non-nil means it is ok to return STRING itself\n\ |
f072a3e8 RS |
4923 | if the decoding operation is trivial.\n\ |
4924 | This function sets `last-coding-system-used' to the precise coding system\n\ | |
4925 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is\n\ | |
4926 | not fully specified.)") | |
e0e989f6 KH |
4927 | (string, coding_system, nocopy) |
4928 | Lisp_Object string, coding_system, nocopy; | |
4ed46869 | 4929 | { |
f072a3e8 | 4930 | return code_convert_string1 (string, coding_system, nocopy, 0); |
4ed46869 KH |
4931 | } |
4932 | ||
4933 | DEFUN ("encode-coding-string", Fencode_coding_string, Sencode_coding_string, | |
e0e989f6 KH |
4934 | 2, 3, 0, |
4935 | "Encode STRING to CODING-SYSTEM, and return the result.\n\ | |
fe487a71 | 4936 | Optional arg NOCOPY non-nil means it is ok to return STRING itself\n\ |
f072a3e8 RS |
4937 | if the encoding operation is trivial.\n\ |
4938 | This function sets `last-coding-system-used' to the precise coding system\n\ | |
4939 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is\n\ | |
4940 | not fully specified.)") | |
e0e989f6 KH |
4941 | (string, coding_system, nocopy) |
4942 | Lisp_Object string, coding_system, nocopy; | |
4ed46869 | 4943 | { |
f072a3e8 | 4944 | return code_convert_string1 (string, coding_system, nocopy, 1); |
4ed46869 | 4945 | } |
4031e2bf | 4946 | |
ecec61c1 KH |
4947 | /* Encode or decode STRING according to CODING_SYSTEM. |
4948 | Do not set Vlast_coding_system_used. */ | |
4949 | ||
4950 | Lisp_Object | |
4951 | code_convert_string_norecord (string, coding_system, encodep) | |
4952 | Lisp_Object string, coding_system; | |
4953 | int encodep; | |
4954 | { | |
4955 | struct coding_system coding; | |
4956 | ||
4957 | CHECK_STRING (string, 0); | |
4958 | CHECK_SYMBOL (coding_system, 1); | |
4959 | ||
4960 | if (NILP (coding_system)) | |
4961 | return string; | |
4962 | ||
4963 | if (setup_coding_system (Fcheck_coding_system (coding_system), &coding) < 0) | |
4964 | error ("Invalid coding system: %s", XSYMBOL (coding_system)->name->data); | |
4965 | ||
4966 | coding.mode |= CODING_MODE_LAST_BLOCK; | |
4967 | return code_convert_string (string, &coding, encodep, Qt); | |
4968 | } | |
3a73fa5d | 4969 | \f |
4ed46869 | 4970 | DEFUN ("decode-sjis-char", Fdecode_sjis_char, Sdecode_sjis_char, 1, 1, 0, |
e0e989f6 | 4971 | "Decode a JISX0208 character of shift-jis encoding.\n\ |
4ed46869 KH |
4972 | CODE is the character code in SJIS.\n\ |
4973 | Return the corresponding character.") | |
4974 | (code) | |
4975 | Lisp_Object code; | |
4976 | { | |
4977 | unsigned char c1, c2, s1, s2; | |
4978 | Lisp_Object val; | |
4979 | ||
4980 | CHECK_NUMBER (code, 0); | |
4981 | s1 = (XFASTINT (code)) >> 8, s2 = (XFASTINT (code)) & 0xFF; | |
4982 | DECODE_SJIS (s1, s2, c1, c2); | |
4983 | XSETFASTINT (val, MAKE_NON_ASCII_CHAR (charset_jisx0208, c1, c2)); | |
4984 | return val; | |
4985 | } | |
4986 | ||
4987 | DEFUN ("encode-sjis-char", Fencode_sjis_char, Sencode_sjis_char, 1, 1, 0, | |
d46c5b12 | 4988 | "Encode a JISX0208 character CHAR to SJIS coding system.\n\ |
4ed46869 KH |
4989 | Return the corresponding character code in SJIS.") |
4990 | (ch) | |
4991 | Lisp_Object ch; | |
4992 | { | |
bcf26d6a | 4993 | int charset, c1, c2, s1, s2; |
4ed46869 KH |
4994 | Lisp_Object val; |
4995 | ||
4996 | CHECK_NUMBER (ch, 0); | |
4997 | SPLIT_CHAR (XFASTINT (ch), charset, c1, c2); | |
4998 | if (charset == charset_jisx0208) | |
4999 | { | |
5000 | ENCODE_SJIS (c1, c2, s1, s2); | |
bcf26d6a | 5001 | XSETFASTINT (val, (s1 << 8) | s2); |
4ed46869 KH |
5002 | } |
5003 | else | |
5004 | XSETFASTINT (val, 0); | |
5005 | return val; | |
5006 | } | |
5007 | ||
5008 | DEFUN ("decode-big5-char", Fdecode_big5_char, Sdecode_big5_char, 1, 1, 0, | |
d46c5b12 | 5009 | "Decode a Big5 character CODE of BIG5 coding system.\n\ |
4ed46869 KH |
5010 | CODE is the character code in BIG5.\n\ |
5011 | Return the corresponding character.") | |
5012 | (code) | |
5013 | Lisp_Object code; | |
5014 | { | |
5015 | int charset; | |
5016 | unsigned char b1, b2, c1, c2; | |
5017 | Lisp_Object val; | |
5018 | ||
5019 | CHECK_NUMBER (code, 0); | |
5020 | b1 = (XFASTINT (code)) >> 8, b2 = (XFASTINT (code)) & 0xFF; | |
5021 | DECODE_BIG5 (b1, b2, charset, c1, c2); | |
5022 | XSETFASTINT (val, MAKE_NON_ASCII_CHAR (charset, c1, c2)); | |
5023 | return val; | |
5024 | } | |
5025 | ||
5026 | DEFUN ("encode-big5-char", Fencode_big5_char, Sencode_big5_char, 1, 1, 0, | |
d46c5b12 | 5027 | "Encode the Big5 character CHAR to BIG5 coding system.\n\ |
4ed46869 KH |
5028 | Return the corresponding character code in Big5.") |
5029 | (ch) | |
5030 | Lisp_Object ch; | |
5031 | { | |
bcf26d6a | 5032 | int charset, c1, c2, b1, b2; |
4ed46869 KH |
5033 | Lisp_Object val; |
5034 | ||
5035 | CHECK_NUMBER (ch, 0); | |
5036 | SPLIT_CHAR (XFASTINT (ch), charset, c1, c2); | |
5037 | if (charset == charset_big5_1 || charset == charset_big5_2) | |
5038 | { | |
5039 | ENCODE_BIG5 (charset, c1, c2, b1, b2); | |
bcf26d6a | 5040 | XSETFASTINT (val, (b1 << 8) | b2); |
4ed46869 KH |
5041 | } |
5042 | else | |
5043 | XSETFASTINT (val, 0); | |
5044 | return val; | |
5045 | } | |
3a73fa5d | 5046 | \f |
1ba9e4ab KH |
5047 | DEFUN ("set-terminal-coding-system-internal", |
5048 | Fset_terminal_coding_system_internal, | |
5049 | Sset_terminal_coding_system_internal, 1, 1, 0, "") | |
4ed46869 KH |
5050 | (coding_system) |
5051 | Lisp_Object coding_system; | |
5052 | { | |
5053 | CHECK_SYMBOL (coding_system, 0); | |
5054 | setup_coding_system (Fcheck_coding_system (coding_system), &terminal_coding); | |
70c22245 | 5055 | /* We had better not send unsafe characters to terminal. */ |
6e85d753 KH |
5056 | terminal_coding.flags |= CODING_FLAG_ISO_SAFE; |
5057 | ||
4ed46869 KH |
5058 | return Qnil; |
5059 | } | |
5060 | ||
c4825358 KH |
5061 | DEFUN ("set-safe-terminal-coding-system-internal", |
5062 | Fset_safe_terminal_coding_system_internal, | |
5063 | Sset_safe_terminal_coding_system_internal, 1, 1, 0, "") | |
5064 | (coding_system) | |
5065 | Lisp_Object coding_system; | |
5066 | { | |
5067 | CHECK_SYMBOL (coding_system, 0); | |
5068 | setup_coding_system (Fcheck_coding_system (coding_system), | |
5069 | &safe_terminal_coding); | |
5070 | return Qnil; | |
5071 | } | |
5072 | ||
4ed46869 KH |
5073 | DEFUN ("terminal-coding-system", |
5074 | Fterminal_coding_system, Sterminal_coding_system, 0, 0, 0, | |
3a73fa5d | 5075 | "Return coding system specified for terminal output.") |
4ed46869 KH |
5076 | () |
5077 | { | |
5078 | return terminal_coding.symbol; | |
5079 | } | |
5080 | ||
1ba9e4ab KH |
5081 | DEFUN ("set-keyboard-coding-system-internal", |
5082 | Fset_keyboard_coding_system_internal, | |
5083 | Sset_keyboard_coding_system_internal, 1, 1, 0, "") | |
4ed46869 KH |
5084 | (coding_system) |
5085 | Lisp_Object coding_system; | |
5086 | { | |
5087 | CHECK_SYMBOL (coding_system, 0); | |
5088 | setup_coding_system (Fcheck_coding_system (coding_system), &keyboard_coding); | |
5089 | return Qnil; | |
5090 | } | |
5091 | ||
5092 | DEFUN ("keyboard-coding-system", | |
5093 | Fkeyboard_coding_system, Skeyboard_coding_system, 0, 0, 0, | |
3a73fa5d | 5094 | "Return coding system specified for decoding keyboard input.") |
4ed46869 KH |
5095 | () |
5096 | { | |
5097 | return keyboard_coding.symbol; | |
5098 | } | |
5099 | ||
5100 | \f | |
a5d301df KH |
5101 | DEFUN ("find-operation-coding-system", Ffind_operation_coding_system, |
5102 | Sfind_operation_coding_system, 1, MANY, 0, | |
5103 | "Choose a coding system for an operation based on the target name.\n\ | |
69f76525 | 5104 | The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).\n\ |
9ce27fde KH |
5105 | DECODING-SYSTEM is the coding system to use for decoding\n\ |
5106 | \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system\n\ | |
5107 | for encoding (in case OPERATION does encoding).\n\ | |
ccdb79f5 RS |
5108 | \n\ |
5109 | The first argument OPERATION specifies an I/O primitive:\n\ | |
5110 | For file I/O, `insert-file-contents' or `write-region'.\n\ | |
5111 | For process I/O, `call-process', `call-process-region', or `start-process'.\n\ | |
5112 | For network I/O, `open-network-stream'.\n\ | |
5113 | \n\ | |
5114 | The remaining arguments should be the same arguments that were passed\n\ | |
5115 | to the primitive. Depending on which primitive, one of those arguments\n\ | |
5116 | is selected as the TARGET. For example, if OPERATION does file I/O,\n\ | |
5117 | whichever argument specifies the file name is TARGET.\n\ | |
5118 | \n\ | |
5119 | TARGET has a meaning which depends on OPERATION:\n\ | |
4ed46869 KH |
5120 | For file I/O, TARGET is a file name.\n\ |
5121 | For process I/O, TARGET is a process name.\n\ | |
5122 | For network I/O, TARGET is a service name or a port number\n\ | |
5123 | \n\ | |
02ba4723 KH |
5124 | This function looks up what specified for TARGET in,\n\ |
5125 | `file-coding-system-alist', `process-coding-system-alist',\n\ | |
5126 | or `network-coding-system-alist' depending on OPERATION.\n\ | |
5127 | They may specify a coding system, a cons of coding systems,\n\ | |
5128 | or a function symbol to call.\n\ | |
5129 | In the last case, we call the function with one argument,\n\ | |
9ce27fde | 5130 | which is a list of all the arguments given to this function.") |
4ed46869 KH |
5131 | (nargs, args) |
5132 | int nargs; | |
5133 | Lisp_Object *args; | |
5134 | { | |
5135 | Lisp_Object operation, target_idx, target, val; | |
5136 | register Lisp_Object chain; | |
5137 | ||
5138 | if (nargs < 2) | |
5139 | error ("Too few arguments"); | |
5140 | operation = args[0]; | |
5141 | if (!SYMBOLP (operation) | |
5142 | || !INTEGERP (target_idx = Fget (operation, Qtarget_idx))) | |
5143 | error ("Invalid first arguement"); | |
5144 | if (nargs < 1 + XINT (target_idx)) | |
5145 | error ("Too few arguments for operation: %s", | |
5146 | XSYMBOL (operation)->name->data); | |
5147 | target = args[XINT (target_idx) + 1]; | |
5148 | if (!(STRINGP (target) | |
5149 | || (EQ (operation, Qopen_network_stream) && INTEGERP (target)))) | |
5150 | error ("Invalid %dth argument", XINT (target_idx) + 1); | |
5151 | ||
2e34157c RS |
5152 | chain = ((EQ (operation, Qinsert_file_contents) |
5153 | || EQ (operation, Qwrite_region)) | |
02ba4723 | 5154 | ? Vfile_coding_system_alist |
2e34157c | 5155 | : (EQ (operation, Qopen_network_stream) |
02ba4723 KH |
5156 | ? Vnetwork_coding_system_alist |
5157 | : Vprocess_coding_system_alist)); | |
4ed46869 KH |
5158 | if (NILP (chain)) |
5159 | return Qnil; | |
5160 | ||
02ba4723 | 5161 | for (; CONSP (chain); chain = XCONS (chain)->cdr) |
4ed46869 | 5162 | { |
f44d27ce RS |
5163 | Lisp_Object elt; |
5164 | elt = XCONS (chain)->car; | |
4ed46869 KH |
5165 | |
5166 | if (CONSP (elt) | |
5167 | && ((STRINGP (target) | |
5168 | && STRINGP (XCONS (elt)->car) | |
5169 | && fast_string_match (XCONS (elt)->car, target) >= 0) | |
5170 | || (INTEGERP (target) && EQ (target, XCONS (elt)->car)))) | |
02ba4723 KH |
5171 | { |
5172 | val = XCONS (elt)->cdr; | |
b19fd4c5 KH |
5173 | /* Here, if VAL is both a valid coding system and a valid |
5174 | function symbol, we return VAL as a coding system. */ | |
02ba4723 KH |
5175 | if (CONSP (val)) |
5176 | return val; | |
5177 | if (! SYMBOLP (val)) | |
5178 | return Qnil; | |
5179 | if (! NILP (Fcoding_system_p (val))) | |
5180 | return Fcons (val, val); | |
b19fd4c5 KH |
5181 | if (! NILP (Ffboundp (val))) |
5182 | { | |
5183 | val = call1 (val, Flist (nargs, args)); | |
5184 | if (CONSP (val)) | |
5185 | return val; | |
5186 | if (SYMBOLP (val) && ! NILP (Fcoding_system_p (val))) | |
5187 | return Fcons (val, val); | |
5188 | } | |
02ba4723 KH |
5189 | return Qnil; |
5190 | } | |
4ed46869 KH |
5191 | } |
5192 | return Qnil; | |
5193 | } | |
5194 | ||
1397dc18 KH |
5195 | DEFUN ("update-coding-systems-internal", Fupdate_coding_systems_internal, |
5196 | Supdate_coding_systems_internal, 0, 0, 0, | |
5197 | "Update internal database for ISO2022 and CCL based coding systems.\n\ | |
d46c5b12 KH |
5198 | When values of the following coding categories are changed, you must\n\ |
5199 | call this function:\n\ | |
5200 | coding-category-iso-7, coding-category-iso-7-tight,\n\ | |
5201 | coding-category-iso-8-1, coding-category-iso-8-2,\n\ | |
1397dc18 KH |
5202 | coding-category-iso-7-else, coding-category-iso-8-else,\n\ |
5203 | coding-category-ccl") | |
d46c5b12 KH |
5204 | () |
5205 | { | |
5206 | int i; | |
5207 | ||
1397dc18 | 5208 | for (i = CODING_CATEGORY_IDX_ISO_7; i <= CODING_CATEGORY_IDX_CCL; i++) |
d46c5b12 | 5209 | { |
1397dc18 KH |
5210 | Lisp_Object val; |
5211 | ||
5212 | val = XSYMBOL (XVECTOR (Vcoding_category_table)->contents[i])->value; | |
5213 | if (!NILP (val)) | |
5214 | { | |
5215 | if (! coding_system_table[i]) | |
5216 | coding_system_table[i] = ((struct coding_system *) | |
5217 | xmalloc (sizeof (struct coding_system))); | |
5218 | setup_coding_system (val, coding_system_table[i]); | |
5219 | } | |
5220 | else if (coding_system_table[i]) | |
5221 | { | |
5222 | xfree (coding_system_table[i]); | |
5223 | coding_system_table[i] = NULL; | |
5224 | } | |
d46c5b12 | 5225 | } |
1397dc18 | 5226 | |
d46c5b12 KH |
5227 | return Qnil; |
5228 | } | |
5229 | ||
66cfb530 KH |
5230 | DEFUN ("set-coding-priority-internal", Fset_coding_priority_internal, |
5231 | Sset_coding_priority_internal, 0, 0, 0, | |
5232 | "Update internal database for the current value of `coding-category-list'.\n\ | |
5233 | This function is internal use only.") | |
5234 | () | |
5235 | { | |
5236 | int i = 0, idx; | |
84d60297 RS |
5237 | Lisp_Object val; |
5238 | ||
5239 | val = Vcoding_category_list; | |
66cfb530 KH |
5240 | |
5241 | while (CONSP (val) && i < CODING_CATEGORY_IDX_MAX) | |
5242 | { | |
5243 | if (! SYMBOLP (XCONS (val)->car)) | |
5244 | break; | |
5245 | idx = XFASTINT (Fget (XCONS (val)->car, Qcoding_category_index)); | |
5246 | if (idx >= CODING_CATEGORY_IDX_MAX) | |
5247 | break; | |
5248 | coding_priorities[i++] = (1 << idx); | |
5249 | val = XCONS (val)->cdr; | |
5250 | } | |
5251 | /* If coding-category-list is valid and contains all coding | |
5252 | categories, `i' should be CODING_CATEGORY_IDX_MAX now. If not, | |
5253 | the following code saves Emacs from craching. */ | |
5254 | while (i < CODING_CATEGORY_IDX_MAX) | |
5255 | coding_priorities[i++] = CODING_CATEGORY_MASK_RAW_TEXT; | |
5256 | ||
5257 | return Qnil; | |
5258 | } | |
5259 | ||
4ed46869 KH |
5260 | #endif /* emacs */ |
5261 | ||
5262 | \f | |
1397dc18 | 5263 | /*** 9. Post-amble ***/ |
4ed46869 | 5264 | |
6d74c3aa KH |
5265 | void |
5266 | init_coding () | |
5267 | { | |
5268 | conversion_buffer = (char *) xmalloc (MINIMUM_CONVERSION_BUFFER_SIZE); | |
5269 | } | |
5270 | ||
dfcf069d | 5271 | void |
4ed46869 KH |
5272 | init_coding_once () |
5273 | { | |
5274 | int i; | |
5275 | ||
0ef69138 | 5276 | /* Emacs' internal format specific initialize routine. */ |
4ed46869 KH |
5277 | for (i = 0; i <= 0x20; i++) |
5278 | emacs_code_class[i] = EMACS_control_code; | |
5279 | emacs_code_class[0x0A] = EMACS_linefeed_code; | |
5280 | emacs_code_class[0x0D] = EMACS_carriage_return_code; | |
5281 | for (i = 0x21 ; i < 0x7F; i++) | |
5282 | emacs_code_class[i] = EMACS_ascii_code; | |
5283 | emacs_code_class[0x7F] = EMACS_control_code; | |
5284 | emacs_code_class[0x80] = EMACS_leading_code_composition; | |
5285 | for (i = 0x81; i < 0xFF; i++) | |
5286 | emacs_code_class[i] = EMACS_invalid_code; | |
5287 | emacs_code_class[LEADING_CODE_PRIVATE_11] = EMACS_leading_code_3; | |
5288 | emacs_code_class[LEADING_CODE_PRIVATE_12] = EMACS_leading_code_3; | |
5289 | emacs_code_class[LEADING_CODE_PRIVATE_21] = EMACS_leading_code_4; | |
5290 | emacs_code_class[LEADING_CODE_PRIVATE_22] = EMACS_leading_code_4; | |
5291 | ||
5292 | /* ISO2022 specific initialize routine. */ | |
5293 | for (i = 0; i < 0x20; i++) | |
5294 | iso_code_class[i] = ISO_control_code; | |
5295 | for (i = 0x21; i < 0x7F; i++) | |
5296 | iso_code_class[i] = ISO_graphic_plane_0; | |
5297 | for (i = 0x80; i < 0xA0; i++) | |
5298 | iso_code_class[i] = ISO_control_code; | |
5299 | for (i = 0xA1; i < 0xFF; i++) | |
5300 | iso_code_class[i] = ISO_graphic_plane_1; | |
5301 | iso_code_class[0x20] = iso_code_class[0x7F] = ISO_0x20_or_0x7F; | |
5302 | iso_code_class[0xA0] = iso_code_class[0xFF] = ISO_0xA0_or_0xFF; | |
5303 | iso_code_class[ISO_CODE_CR] = ISO_carriage_return; | |
5304 | iso_code_class[ISO_CODE_SO] = ISO_shift_out; | |
5305 | iso_code_class[ISO_CODE_SI] = ISO_shift_in; | |
5306 | iso_code_class[ISO_CODE_SS2_7] = ISO_single_shift_2_7; | |
5307 | iso_code_class[ISO_CODE_ESC] = ISO_escape; | |
5308 | iso_code_class[ISO_CODE_SS2] = ISO_single_shift_2; | |
5309 | iso_code_class[ISO_CODE_SS3] = ISO_single_shift_3; | |
5310 | iso_code_class[ISO_CODE_CSI] = ISO_control_sequence_introducer; | |
5311 | ||
e0e989f6 | 5312 | conversion_buffer_size = MINIMUM_CONVERSION_BUFFER_SIZE; |
e0e989f6 KH |
5313 | |
5314 | setup_coding_system (Qnil, &keyboard_coding); | |
5315 | setup_coding_system (Qnil, &terminal_coding); | |
c4825358 | 5316 | setup_coding_system (Qnil, &safe_terminal_coding); |
6bc51348 | 5317 | setup_coding_system (Qnil, &default_buffer_file_coding); |
9ce27fde | 5318 | |
d46c5b12 KH |
5319 | bzero (coding_system_table, sizeof coding_system_table); |
5320 | ||
66cfb530 KH |
5321 | bzero (ascii_skip_code, sizeof ascii_skip_code); |
5322 | for (i = 0; i < 128; i++) | |
5323 | ascii_skip_code[i] = 1; | |
5324 | ||
9ce27fde KH |
5325 | #if defined (MSDOS) || defined (WINDOWSNT) |
5326 | system_eol_type = CODING_EOL_CRLF; | |
5327 | #else | |
5328 | system_eol_type = CODING_EOL_LF; | |
5329 | #endif | |
e0e989f6 KH |
5330 | } |
5331 | ||
5332 | #ifdef emacs | |
5333 | ||
dfcf069d | 5334 | void |
e0e989f6 KH |
5335 | syms_of_coding () |
5336 | { | |
5337 | Qtarget_idx = intern ("target-idx"); | |
5338 | staticpro (&Qtarget_idx); | |
5339 | ||
bb0115a2 RS |
5340 | Qcoding_system_history = intern ("coding-system-history"); |
5341 | staticpro (&Qcoding_system_history); | |
5342 | Fset (Qcoding_system_history, Qnil); | |
5343 | ||
9ce27fde | 5344 | /* Target FILENAME is the first argument. */ |
e0e989f6 | 5345 | Fput (Qinsert_file_contents, Qtarget_idx, make_number (0)); |
9ce27fde | 5346 | /* Target FILENAME is the third argument. */ |
e0e989f6 KH |
5347 | Fput (Qwrite_region, Qtarget_idx, make_number (2)); |
5348 | ||
5349 | Qcall_process = intern ("call-process"); | |
5350 | staticpro (&Qcall_process); | |
9ce27fde | 5351 | /* Target PROGRAM is the first argument. */ |
e0e989f6 KH |
5352 | Fput (Qcall_process, Qtarget_idx, make_number (0)); |
5353 | ||
5354 | Qcall_process_region = intern ("call-process-region"); | |
5355 | staticpro (&Qcall_process_region); | |
9ce27fde | 5356 | /* Target PROGRAM is the third argument. */ |
e0e989f6 KH |
5357 | Fput (Qcall_process_region, Qtarget_idx, make_number (2)); |
5358 | ||
5359 | Qstart_process = intern ("start-process"); | |
5360 | staticpro (&Qstart_process); | |
9ce27fde | 5361 | /* Target PROGRAM is the third argument. */ |
e0e989f6 KH |
5362 | Fput (Qstart_process, Qtarget_idx, make_number (2)); |
5363 | ||
5364 | Qopen_network_stream = intern ("open-network-stream"); | |
5365 | staticpro (&Qopen_network_stream); | |
9ce27fde | 5366 | /* Target SERVICE is the fourth argument. */ |
e0e989f6 KH |
5367 | Fput (Qopen_network_stream, Qtarget_idx, make_number (3)); |
5368 | ||
4ed46869 KH |
5369 | Qcoding_system = intern ("coding-system"); |
5370 | staticpro (&Qcoding_system); | |
5371 | ||
5372 | Qeol_type = intern ("eol-type"); | |
5373 | staticpro (&Qeol_type); | |
5374 | ||
5375 | Qbuffer_file_coding_system = intern ("buffer-file-coding-system"); | |
5376 | staticpro (&Qbuffer_file_coding_system); | |
5377 | ||
5378 | Qpost_read_conversion = intern ("post-read-conversion"); | |
5379 | staticpro (&Qpost_read_conversion); | |
5380 | ||
5381 | Qpre_write_conversion = intern ("pre-write-conversion"); | |
5382 | staticpro (&Qpre_write_conversion); | |
5383 | ||
27901516 KH |
5384 | Qno_conversion = intern ("no-conversion"); |
5385 | staticpro (&Qno_conversion); | |
5386 | ||
5387 | Qundecided = intern ("undecided"); | |
5388 | staticpro (&Qundecided); | |
5389 | ||
4ed46869 KH |
5390 | Qcoding_system_p = intern ("coding-system-p"); |
5391 | staticpro (&Qcoding_system_p); | |
5392 | ||
5393 | Qcoding_system_error = intern ("coding-system-error"); | |
5394 | staticpro (&Qcoding_system_error); | |
5395 | ||
5396 | Fput (Qcoding_system_error, Qerror_conditions, | |
5397 | Fcons (Qcoding_system_error, Fcons (Qerror, Qnil))); | |
5398 | Fput (Qcoding_system_error, Qerror_message, | |
9ce27fde | 5399 | build_string ("Invalid coding system")); |
4ed46869 | 5400 | |
d46c5b12 KH |
5401 | Qcoding_category = intern ("coding-category"); |
5402 | staticpro (&Qcoding_category); | |
4ed46869 KH |
5403 | Qcoding_category_index = intern ("coding-category-index"); |
5404 | staticpro (&Qcoding_category_index); | |
5405 | ||
d46c5b12 KH |
5406 | Vcoding_category_table |
5407 | = Fmake_vector (make_number (CODING_CATEGORY_IDX_MAX), Qnil); | |
5408 | staticpro (&Vcoding_category_table); | |
4ed46869 KH |
5409 | { |
5410 | int i; | |
5411 | for (i = 0; i < CODING_CATEGORY_IDX_MAX; i++) | |
5412 | { | |
d46c5b12 KH |
5413 | XVECTOR (Vcoding_category_table)->contents[i] |
5414 | = intern (coding_category_name[i]); | |
5415 | Fput (XVECTOR (Vcoding_category_table)->contents[i], | |
5416 | Qcoding_category_index, make_number (i)); | |
4ed46869 KH |
5417 | } |
5418 | } | |
5419 | ||
f967223b KH |
5420 | Qtranslation_table = intern ("translation-table"); |
5421 | staticpro (&Qtranslation_table); | |
1397dc18 | 5422 | Fput (Qtranslation_table, Qchar_table_extra_slots, make_number (1)); |
bdd9fb48 | 5423 | |
f967223b KH |
5424 | Qtranslation_table_id = intern ("translation-table-id"); |
5425 | staticpro (&Qtranslation_table_id); | |
84fbb8a0 | 5426 | |
f967223b KH |
5427 | Qtranslation_table_for_decode = intern ("translation-table-for-decode"); |
5428 | staticpro (&Qtranslation_table_for_decode); | |
a5d301df | 5429 | |
f967223b KH |
5430 | Qtranslation_table_for_encode = intern ("translation-table-for-encode"); |
5431 | staticpro (&Qtranslation_table_for_encode); | |
a5d301df | 5432 | |
70c22245 KH |
5433 | Qsafe_charsets = intern ("safe-charsets"); |
5434 | staticpro (&Qsafe_charsets); | |
5435 | ||
1397dc18 KH |
5436 | Qvalid_codes = intern ("valid-codes"); |
5437 | staticpro (&Qvalid_codes); | |
5438 | ||
9ce27fde KH |
5439 | Qemacs_mule = intern ("emacs-mule"); |
5440 | staticpro (&Qemacs_mule); | |
5441 | ||
d46c5b12 KH |
5442 | Qraw_text = intern ("raw-text"); |
5443 | staticpro (&Qraw_text); | |
5444 | ||
4ed46869 KH |
5445 | defsubr (&Scoding_system_p); |
5446 | defsubr (&Sread_coding_system); | |
5447 | defsubr (&Sread_non_nil_coding_system); | |
5448 | defsubr (&Scheck_coding_system); | |
5449 | defsubr (&Sdetect_coding_region); | |
d46c5b12 | 5450 | defsubr (&Sdetect_coding_string); |
4ed46869 KH |
5451 | defsubr (&Sdecode_coding_region); |
5452 | defsubr (&Sencode_coding_region); | |
5453 | defsubr (&Sdecode_coding_string); | |
5454 | defsubr (&Sencode_coding_string); | |
5455 | defsubr (&Sdecode_sjis_char); | |
5456 | defsubr (&Sencode_sjis_char); | |
5457 | defsubr (&Sdecode_big5_char); | |
5458 | defsubr (&Sencode_big5_char); | |
1ba9e4ab | 5459 | defsubr (&Sset_terminal_coding_system_internal); |
c4825358 | 5460 | defsubr (&Sset_safe_terminal_coding_system_internal); |
4ed46869 | 5461 | defsubr (&Sterminal_coding_system); |
1ba9e4ab | 5462 | defsubr (&Sset_keyboard_coding_system_internal); |
4ed46869 | 5463 | defsubr (&Skeyboard_coding_system); |
a5d301df | 5464 | defsubr (&Sfind_operation_coding_system); |
1397dc18 | 5465 | defsubr (&Supdate_coding_systems_internal); |
66cfb530 | 5466 | defsubr (&Sset_coding_priority_internal); |
4ed46869 | 5467 | |
4608c386 KH |
5468 | DEFVAR_LISP ("coding-system-list", &Vcoding_system_list, |
5469 | "List of coding systems.\n\ | |
5470 | \n\ | |
5471 | Do not alter the value of this variable manually. This variable should be\n\ | |
5472 | updated by the functions `make-coding-system' and\n\ | |
5473 | `define-coding-system-alias'."); | |
5474 | Vcoding_system_list = Qnil; | |
5475 | ||
5476 | DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist, | |
5477 | "Alist of coding system names.\n\ | |
5478 | Each element is one element list of coding system name.\n\ | |
5479 | This variable is given to `completing-read' as TABLE argument.\n\ | |
5480 | \n\ | |
5481 | Do not alter the value of this variable manually. This variable should be\n\ | |
5482 | updated by the functions `make-coding-system' and\n\ | |
5483 | `define-coding-system-alias'."); | |
5484 | Vcoding_system_alist = Qnil; | |
5485 | ||
4ed46869 KH |
5486 | DEFVAR_LISP ("coding-category-list", &Vcoding_category_list, |
5487 | "List of coding-categories (symbols) ordered by priority."); | |
5488 | { | |
5489 | int i; | |
5490 | ||
5491 | Vcoding_category_list = Qnil; | |
5492 | for (i = CODING_CATEGORY_IDX_MAX - 1; i >= 0; i--) | |
5493 | Vcoding_category_list | |
d46c5b12 KH |
5494 | = Fcons (XVECTOR (Vcoding_category_table)->contents[i], |
5495 | Vcoding_category_list); | |
4ed46869 KH |
5496 | } |
5497 | ||
5498 | DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read, | |
10bff6f1 | 5499 | "Specify the coding system for read operations.\n\ |
2ebb362d | 5500 | It is useful to bind this variable with `let', but do not set it globally.\n\ |
4ed46869 | 5501 | If the value is a coding system, it is used for decoding on read operation.\n\ |
a67a9c66 | 5502 | If not, an appropriate element is used from one of the coding system alists:\n\ |
10bff6f1 | 5503 | There are three such tables, `file-coding-system-alist',\n\ |
a67a9c66 | 5504 | `process-coding-system-alist', and `network-coding-system-alist'."); |
4ed46869 KH |
5505 | Vcoding_system_for_read = Qnil; |
5506 | ||
5507 | DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write, | |
10bff6f1 | 5508 | "Specify the coding system for write operations.\n\ |
2ebb362d | 5509 | It is useful to bind this variable with `let', but do not set it globally.\n\ |
4ed46869 | 5510 | If the value is a coding system, it is used for encoding on write operation.\n\ |
a67a9c66 | 5511 | If not, an appropriate element is used from one of the coding system alists:\n\ |
10bff6f1 | 5512 | There are three such tables, `file-coding-system-alist',\n\ |
a67a9c66 | 5513 | `process-coding-system-alist', and `network-coding-system-alist'."); |
4ed46869 KH |
5514 | Vcoding_system_for_write = Qnil; |
5515 | ||
5516 | DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used, | |
a67a9c66 | 5517 | "Coding system used in the latest file or process I/O."); |
4ed46869 KH |
5518 | Vlast_coding_system_used = Qnil; |
5519 | ||
9ce27fde KH |
5520 | DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion, |
5521 | "*Non-nil inhibit code conversion of end-of-line format in any cases."); | |
5522 | inhibit_eol_conversion = 0; | |
5523 | ||
ed29121d EZ |
5524 | DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system, |
5525 | "Non-nil means process buffer inherits coding system of process output.\n\ | |
5526 | Bind it to t if the process output is to be treated as if it were a file\n\ | |
5527 | read from some filesystem."); | |
5528 | inherit_process_coding_system = 0; | |
5529 | ||
02ba4723 KH |
5530 | DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist, |
5531 | "Alist to decide a coding system to use for a file I/O operation.\n\ | |
5532 | The format is ((PATTERN . VAL) ...),\n\ | |
5533 | where PATTERN is a regular expression matching a file name,\n\ | |
5534 | VAL is a coding system, a cons of coding systems, or a function symbol.\n\ | |
5535 | If VAL is a coding system, it is used for both decoding and encoding\n\ | |
5536 | the file contents.\n\ | |
5537 | If VAL is a cons of coding systems, the car part is used for decoding,\n\ | |
5538 | and the cdr part is used for encoding.\n\ | |
5539 | If VAL is a function symbol, the function must return a coding system\n\ | |
5540 | or a cons of coding systems which are used as above.\n\ | |
e0e989f6 | 5541 | \n\ |
a85a871a | 5542 | See also the function `find-operation-coding-system'\n\ |
eda284ac | 5543 | and the variable `auto-coding-alist'."); |
02ba4723 KH |
5544 | Vfile_coding_system_alist = Qnil; |
5545 | ||
5546 | DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist, | |
5547 | "Alist to decide a coding system to use for a process I/O operation.\n\ | |
5548 | The format is ((PATTERN . VAL) ...),\n\ | |
5549 | where PATTERN is a regular expression matching a program name,\n\ | |
5550 | VAL is a coding system, a cons of coding systems, or a function symbol.\n\ | |
5551 | If VAL is a coding system, it is used for both decoding what received\n\ | |
5552 | from the program and encoding what sent to the program.\n\ | |
5553 | If VAL is a cons of coding systems, the car part is used for decoding,\n\ | |
5554 | and the cdr part is used for encoding.\n\ | |
5555 | If VAL is a function symbol, the function must return a coding system\n\ | |
5556 | or a cons of coding systems which are used as above.\n\ | |
4ed46869 | 5557 | \n\ |
9ce27fde | 5558 | See also the function `find-operation-coding-system'."); |
02ba4723 KH |
5559 | Vprocess_coding_system_alist = Qnil; |
5560 | ||
5561 | DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist, | |
5562 | "Alist to decide a coding system to use for a network I/O operation.\n\ | |
5563 | The format is ((PATTERN . VAL) ...),\n\ | |
5564 | where PATTERN is a regular expression matching a network service name\n\ | |
5565 | or is a port number to connect to,\n\ | |
5566 | VAL is a coding system, a cons of coding systems, or a function symbol.\n\ | |
5567 | If VAL is a coding system, it is used for both decoding what received\n\ | |
5568 | from the network stream and encoding what sent to the network stream.\n\ | |
5569 | If VAL is a cons of coding systems, the car part is used for decoding,\n\ | |
5570 | and the cdr part is used for encoding.\n\ | |
5571 | If VAL is a function symbol, the function must return a coding system\n\ | |
5572 | or a cons of coding systems which are used as above.\n\ | |
4ed46869 | 5573 | \n\ |
9ce27fde | 5574 | See also the function `find-operation-coding-system'."); |
02ba4723 | 5575 | Vnetwork_coding_system_alist = Qnil; |
4ed46869 KH |
5576 | |
5577 | DEFVAR_INT ("eol-mnemonic-unix", &eol_mnemonic_unix, | |
5578 | "Mnemonic character indicating UNIX-like end-of-line format (i.e. LF) ."); | |
458822a0 | 5579 | eol_mnemonic_unix = ':'; |
4ed46869 KH |
5580 | |
5581 | DEFVAR_INT ("eol-mnemonic-dos", &eol_mnemonic_dos, | |
5582 | "Mnemonic character indicating DOS-like end-of-line format (i.e. CRLF)."); | |
458822a0 | 5583 | eol_mnemonic_dos = '\\'; |
4ed46869 KH |
5584 | |
5585 | DEFVAR_INT ("eol-mnemonic-mac", &eol_mnemonic_mac, | |
5586 | "Mnemonic character indicating MAC-like end-of-line format (i.e. CR)."); | |
458822a0 | 5587 | eol_mnemonic_mac = '/'; |
4ed46869 KH |
5588 | |
5589 | DEFVAR_INT ("eol-mnemonic-undecided", &eol_mnemonic_undecided, | |
5590 | "Mnemonic character indicating end-of-line format is not yet decided."); | |
458822a0 | 5591 | eol_mnemonic_undecided = ':'; |
4ed46869 | 5592 | |
84fbb8a0 | 5593 | DEFVAR_LISP ("enable-character-translation", &Venable_character_translation, |
f967223b | 5594 | "*Non-nil enables character translation while encoding and decoding."); |
84fbb8a0 | 5595 | Venable_character_translation = Qt; |
bdd9fb48 | 5596 | |
f967223b KH |
5597 | DEFVAR_LISP ("standard-translation-table-for-decode", |
5598 | &Vstandard_translation_table_for_decode, | |
84fbb8a0 | 5599 | "Table for translating characters while decoding."); |
f967223b | 5600 | Vstandard_translation_table_for_decode = Qnil; |
bdd9fb48 | 5601 | |
f967223b KH |
5602 | DEFVAR_LISP ("standard-translation-table-for-encode", |
5603 | &Vstandard_translation_table_for_encode, | |
84fbb8a0 | 5604 | "Table for translationg characters while encoding."); |
f967223b | 5605 | Vstandard_translation_table_for_encode = Qnil; |
4ed46869 KH |
5606 | |
5607 | DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_alist, | |
5608 | "Alist of charsets vs revision numbers.\n\ | |
5609 | While encoding, if a charset (car part of an element) is found,\n\ | |
5610 | designate it with the escape sequence identifing revision (cdr part of the element)."); | |
5611 | Vcharset_revision_alist = Qnil; | |
02ba4723 KH |
5612 | |
5613 | DEFVAR_LISP ("default-process-coding-system", | |
5614 | &Vdefault_process_coding_system, | |
5615 | "Cons of coding systems used for process I/O by default.\n\ | |
5616 | The car part is used for decoding a process output,\n\ | |
5617 | the cdr part is used for encoding a text to be sent to a process."); | |
5618 | Vdefault_process_coding_system = Qnil; | |
c4825358 | 5619 | |
3f003981 KH |
5620 | DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table, |
5621 | "Table of extra Latin codes in the range 128..159 (inclusive).\n\ | |
c4825358 KH |
5622 | This is a vector of length 256.\n\ |
5623 | If Nth element is non-nil, the existence of code N in a file\n\ | |
bb0115a2 | 5624 | \(or output of subprocess) doesn't prevent it to be detected as\n\ |
3f003981 KH |
5625 | a coding system of ISO 2022 variant which has a flag\n\ |
5626 | `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file\n\ | |
c4825358 KH |
5627 | or reading output of a subprocess.\n\ |
5628 | Only 128th through 159th elements has a meaning."); | |
3f003981 | 5629 | Vlatin_extra_code_table = Fmake_vector (make_number (256), Qnil); |
d46c5b12 KH |
5630 | |
5631 | DEFVAR_LISP ("select-safe-coding-system-function", | |
5632 | &Vselect_safe_coding_system_function, | |
5633 | "Function to call to select safe coding system for encoding a text.\n\ | |
5634 | \n\ | |
5635 | If set, this function is called to force a user to select a proper\n\ | |
5636 | coding system which can encode the text in the case that a default\n\ | |
5637 | coding system used in each operation can't encode the text.\n\ | |
5638 | \n\ | |
a85a871a | 5639 | The default value is `select-safe-coding-system' (which see)."); |
d46c5b12 KH |
5640 | Vselect_safe_coding_system_function = Qnil; |
5641 | ||
4ed46869 KH |
5642 | } |
5643 | ||
5644 | #endif /* emacs */ |