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