Commit | Line | Data |
---|---|---|
9542cb1f | 1 | /* Coding system handler (conversion, detection, etc). |
73b0cd50 | 2 | Copyright (C) 2001-2011 Free Software Foundation, Inc. |
7976eda0 | 3 | Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
5df4f04c | 4 | 2005, 2006, 2007, 2008, 2009, 2010, 2011 |
ce03bf76 KH |
5 | National Institute of Advanced Industrial Science and Technology (AIST) |
6 | Registration Number H14PRO021 | |
8f924df7 | 7 | Copyright (C) 2003 |
df7492f9 KH |
8 | National Institute of Advanced Industrial Science and Technology (AIST) |
9 | Registration Number H13PRO009 | |
4ed46869 | 10 | |
369314dc KH |
11 | This file is part of GNU Emacs. |
12 | ||
9ec0b715 | 13 | GNU Emacs is free software: you can redistribute it and/or modify |
369314dc | 14 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
15 | the Free Software Foundation, either version 3 of the License, or |
16 | (at your option) any later version. | |
4ed46869 | 17 | |
369314dc KH |
18 | GNU Emacs is distributed in the hope that it will be useful, |
19 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | GNU General Public License for more details. | |
4ed46869 | 22 | |
369314dc | 23 | You should have received a copy of the GNU General Public License |
9ec0b715 | 24 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
4ed46869 KH |
25 | |
26 | /*** TABLE OF CONTENTS *** | |
27 | ||
b73bfc1c | 28 | 0. General comments |
4ed46869 | 29 | 1. Preamble |
df7492f9 KH |
30 | 2. Emacs' internal format (emacs-utf-8) handlers |
31 | 3. UTF-8 handlers | |
32 | 4. UTF-16 handlers | |
33 | 5. Charset-base coding systems handlers | |
34 | 6. emacs-mule (old Emacs' internal format) handlers | |
35 | 7. ISO2022 handlers | |
36 | 8. Shift-JIS and BIG5 handlers | |
37 | 9. CCL handlers | |
38 | 10. C library functions | |
39 | 11. Emacs Lisp library functions | |
40 | 12. Postamble | |
4ed46869 KH |
41 | |
42 | */ | |
43 | ||
df7492f9 | 44 | /*** 0. General comments *** |
b73bfc1c KH |
45 | |
46 | ||
df7492f9 | 47 | CODING SYSTEM |
4ed46869 | 48 | |
5bad0796 DL |
49 | A coding system is an object for an encoding mechanism that contains |
50 | information about how to convert byte sequences to character | |
e19c3639 KH |
51 | sequences and vice versa. When we say "decode", it means converting |
52 | a byte sequence of a specific coding system into a character | |
53 | sequence that is represented by Emacs' internal coding system | |
54 | `emacs-utf-8', and when we say "encode", it means converting a | |
55 | character sequence of emacs-utf-8 to a byte sequence of a specific | |
0ef69138 | 56 | coding system. |
4ed46869 | 57 | |
e19c3639 KH |
58 | In Emacs Lisp, a coding system is represented by a Lisp symbol. In |
59 | C level, a coding system is represented by a vector of attributes | |
5bad0796 | 60 | stored in the hash table Vcharset_hash_table. The conversion from |
e19c3639 KH |
61 | coding system symbol to attributes vector is done by looking up |
62 | Vcharset_hash_table by the symbol. | |
4ed46869 | 63 | |
e19c3639 | 64 | Coding systems are classified into the following types depending on |
5bad0796 | 65 | the encoding mechanism. Here's a brief description of the types. |
4ed46869 | 66 | |
df7492f9 KH |
67 | o UTF-8 |
68 | ||
69 | o UTF-16 | |
70 | ||
71 | o Charset-base coding system | |
72 | ||
73 | A coding system defined by one or more (coded) character sets. | |
5bad0796 | 74 | Decoding and encoding are done by a code converter defined for each |
df7492f9 KH |
75 | character set. |
76 | ||
5bad0796 | 77 | o Old Emacs internal format (emacs-mule) |
df7492f9 | 78 | |
5bad0796 | 79 | The coding system adopted by old versions of Emacs (20 and 21). |
4ed46869 | 80 | |
df7492f9 | 81 | o ISO2022-base coding system |
4ed46869 KH |
82 | |
83 | The most famous coding system for multiple character sets. X's | |
df7492f9 KH |
84 | Compound Text, various EUCs (Extended Unix Code), and coding systems |
85 | used in the Internet communication such as ISO-2022-JP are all | |
86 | variants of ISO2022. | |
4ed46869 | 87 | |
df7492f9 | 88 | o SJIS (or Shift-JIS or MS-Kanji-Code) |
93dec019 | 89 | |
4ed46869 KH |
90 | A coding system to encode character sets: ASCII, JISX0201, and |
91 | JISX0208. Widely used for PC's in Japan. Details are described in | |
df7492f9 | 92 | section 8. |
4ed46869 | 93 | |
df7492f9 | 94 | o BIG5 |
4ed46869 | 95 | |
df7492f9 | 96 | A coding system to encode character sets: ASCII and Big5. Widely |
cfb43547 | 97 | used for Chinese (mainly in Taiwan and Hong Kong). Details are |
df7492f9 KH |
98 | described in section 8. In this file, when we write "big5" (all |
99 | lowercase), we mean the coding system, and when we write "Big5" | |
100 | (capitalized), we mean the character set. | |
4ed46869 | 101 | |
df7492f9 | 102 | o CCL |
27901516 | 103 | |
5bad0796 | 104 | If a user wants to decode/encode text encoded in a coding system |
df7492f9 KH |
105 | not listed above, he can supply a decoder and an encoder for it in |
106 | CCL (Code Conversion Language) programs. Emacs executes the CCL | |
107 | program while decoding/encoding. | |
27901516 | 108 | |
df7492f9 | 109 | o Raw-text |
4ed46869 | 110 | |
5a936b46 | 111 | A coding system for text containing raw eight-bit data. Emacs |
5bad0796 | 112 | treats each byte of source text as a character (except for |
df7492f9 | 113 | end-of-line conversion). |
4ed46869 | 114 | |
df7492f9 KH |
115 | o No-conversion |
116 | ||
117 | Like raw text, but don't do end-of-line conversion. | |
4ed46869 | 118 | |
4ed46869 | 119 | |
df7492f9 | 120 | END-OF-LINE FORMAT |
4ed46869 | 121 | |
5bad0796 | 122 | How text end-of-line is encoded depends on operating system. For |
df7492f9 | 123 | instance, Unix's format is just one byte of LF (line-feed) code, |
f4dee582 | 124 | whereas DOS's format is two-byte sequence of `carriage-return' and |
d46c5b12 KH |
125 | `line-feed' codes. MacOS's format is usually one byte of |
126 | `carriage-return'. | |
4ed46869 | 127 | |
cfb43547 | 128 | Since text character encoding and end-of-line encoding are |
df7492f9 KH |
129 | independent, any coding system described above can take any format |
130 | of end-of-line (except for no-conversion). | |
4ed46869 | 131 | |
e19c3639 KH |
132 | STRUCT CODING_SYSTEM |
133 | ||
134 | Before using a coding system for code conversion (i.e. decoding and | |
135 | encoding), we setup a structure of type `struct coding_system'. | |
136 | This structure keeps various information about a specific code | |
5bad0796 | 137 | conversion (e.g. the location of source and destination data). |
4ed46869 KH |
138 | |
139 | */ | |
140 | ||
df7492f9 KH |
141 | /* COMMON MACROS */ |
142 | ||
143 | ||
4ed46869 KH |
144 | /*** GENERAL NOTES on `detect_coding_XXX ()' functions *** |
145 | ||
df7492f9 | 146 | These functions check if a byte sequence specified as a source in |
ff0dacd7 KH |
147 | CODING conforms to the format of XXX, and update the members of |
148 | DETECT_INFO. | |
df7492f9 | 149 | |
ff0dacd7 | 150 | Return 1 if the byte sequence conforms to XXX, otherwise return 0. |
df7492f9 KH |
151 | |
152 | Below is the template of these functions. */ | |
153 | ||
4ed46869 | 154 | #if 0 |
df7492f9 | 155 | static int |
cf84bb53 JB |
156 | detect_coding_XXX (struct coding_system *coding, |
157 | struct coding_detection_info *detect_info) | |
4ed46869 | 158 | { |
f1d34bca MB |
159 | const unsigned char *src = coding->source; |
160 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
df7492f9 | 161 | int multibytep = coding->src_multibyte; |
ff0dacd7 | 162 | int consumed_chars = 0; |
df7492f9 KH |
163 | int found = 0; |
164 | ...; | |
165 | ||
166 | while (1) | |
167 | { | |
ad1746f5 | 168 | /* Get one byte from the source. If the source is exhausted, jump |
df7492f9 KH |
169 | to no_more_source:. */ |
170 | ONE_MORE_BYTE (c); | |
ff0dacd7 KH |
171 | |
172 | if (! __C_conforms_to_XXX___ (c)) | |
173 | break; | |
174 | if (! __C_strongly_suggests_XXX__ (c)) | |
175 | found = CATEGORY_MASK_XXX; | |
df7492f9 | 176 | } |
ff0dacd7 KH |
177 | /* The byte sequence is invalid for XXX. */ |
178 | detect_info->rejected |= CATEGORY_MASK_XXX; | |
df7492f9 | 179 | return 0; |
ff0dacd7 | 180 | |
df7492f9 | 181 | no_more_source: |
ad1746f5 | 182 | /* The source exhausted successfully. */ |
ff0dacd7 | 183 | detect_info->found |= found; |
df7492f9 | 184 | return 1; |
4ed46869 KH |
185 | } |
186 | #endif | |
187 | ||
188 | /*** GENERAL NOTES on `decode_coding_XXX ()' functions *** | |
189 | ||
df7492f9 KH |
190 | These functions decode a byte sequence specified as a source by |
191 | CODING. The resulting multibyte text goes to a place pointed to by | |
192 | CODING->charbuf, the length of which should not exceed | |
193 | CODING->charbuf_size; | |
d46c5b12 | 194 | |
df7492f9 KH |
195 | These functions set the information of original and decoded texts in |
196 | CODING->consumed, CODING->consumed_char, and CODING->charbuf_used. | |
197 | They also set CODING->result to one of CODING_RESULT_XXX indicating | |
198 | how the decoding is finished. | |
d46c5b12 | 199 | |
df7492f9 | 200 | Below is the template of these functions. */ |
d46c5b12 | 201 | |
4ed46869 | 202 | #if 0 |
b73bfc1c | 203 | static void |
cf84bb53 | 204 | decode_coding_XXXX (struct coding_system *coding) |
4ed46869 | 205 | { |
f1d34bca MB |
206 | const unsigned char *src = coding->source + coding->consumed; |
207 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
df7492f9 KH |
208 | /* SRC_BASE remembers the start position in source in each loop. |
209 | The loop will be exited when there's not enough source code, or | |
210 | when there's no room in CHARBUF for a decoded character. */ | |
f1d34bca | 211 | const unsigned char *src_base; |
df7492f9 | 212 | /* A buffer to produce decoded characters. */ |
69a80ea3 KH |
213 | int *charbuf = coding->charbuf + coding->charbuf_used; |
214 | int *charbuf_end = coding->charbuf + coding->charbuf_size; | |
df7492f9 KH |
215 | int multibytep = coding->src_multibyte; |
216 | ||
217 | while (1) | |
218 | { | |
219 | src_base = src; | |
220 | if (charbuf < charbuf_end) | |
221 | /* No more room to produce a decoded character. */ | |
222 | break; | |
223 | ONE_MORE_BYTE (c); | |
224 | /* Decode it. */ | |
225 | } | |
226 | ||
227 | no_more_source: | |
228 | if (src_base < src_end | |
229 | && coding->mode & CODING_MODE_LAST_BLOCK) | |
230 | /* If the source ends by partial bytes to construct a character, | |
231 | treat them as eight-bit raw data. */ | |
232 | while (src_base < src_end && charbuf < charbuf_end) | |
233 | *charbuf++ = *src_base++; | |
234 | /* Remember how many bytes and characters we consumed. If the | |
235 | source is multibyte, the bytes and chars are not identical. */ | |
236 | coding->consumed = coding->consumed_char = src_base - coding->source; | |
237 | /* Remember how many characters we produced. */ | |
238 | coding->charbuf_used = charbuf - coding->charbuf; | |
4ed46869 KH |
239 | } |
240 | #endif | |
241 | ||
242 | /*** GENERAL NOTES on `encode_coding_XXX ()' functions *** | |
243 | ||
df7492f9 KH |
244 | These functions encode SRC_BYTES length text at SOURCE of Emacs' |
245 | internal multibyte format by CODING. The resulting byte sequence | |
b73bfc1c KH |
246 | goes to a place pointed to by DESTINATION, the length of which |
247 | should not exceed DST_BYTES. | |
d46c5b12 | 248 | |
df7492f9 KH |
249 | These functions set the information of original and encoded texts in |
250 | the members produced, produced_char, consumed, and consumed_char of | |
251 | the structure *CODING. They also set the member result to one of | |
252 | CODING_RESULT_XXX indicating how the encoding finished. | |
d46c5b12 | 253 | |
df7492f9 KH |
254 | DST_BYTES zero means that source area and destination area are |
255 | overlapped, which means that we can produce a encoded text until it | |
256 | reaches at the head of not-yet-encoded source text. | |
d46c5b12 | 257 | |
df7492f9 | 258 | Below is a template of these functions. */ |
4ed46869 | 259 | #if 0 |
b73bfc1c | 260 | static void |
cf84bb53 | 261 | encode_coding_XXX (struct coding_system *coding) |
4ed46869 | 262 | { |
df7492f9 KH |
263 | int multibytep = coding->dst_multibyte; |
264 | int *charbuf = coding->charbuf; | |
265 | int *charbuf_end = charbuf->charbuf + coding->charbuf_used; | |
266 | unsigned char *dst = coding->destination + coding->produced; | |
267 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
268 | unsigned char *adjusted_dst_end = dst_end - _MAX_BYTES_PRODUCED_IN_LOOP_; | |
269 | int produced_chars = 0; | |
270 | ||
271 | for (; charbuf < charbuf_end && dst < adjusted_dst_end; charbuf++) | |
272 | { | |
273 | int c = *charbuf; | |
274 | /* Encode C into DST, and increment DST. */ | |
275 | } | |
276 | label_no_more_destination: | |
277 | /* How many chars and bytes we produced. */ | |
278 | coding->produced_char += produced_chars; | |
279 | coding->produced = dst - coding->destination; | |
4ed46869 KH |
280 | } |
281 | #endif | |
282 | ||
4ed46869 KH |
283 | \f |
284 | /*** 1. Preamble ***/ | |
285 | ||
68c45bf0 | 286 | #include <config.h> |
4ed46869 | 287 | #include <stdio.h> |
d7306fe6 | 288 | #include <setjmp.h> |
4ed46869 | 289 | |
4ed46869 KH |
290 | #include "lisp.h" |
291 | #include "buffer.h" | |
df7492f9 | 292 | #include "character.h" |
4ed46869 KH |
293 | #include "charset.h" |
294 | #include "ccl.h" | |
df7492f9 | 295 | #include "composite.h" |
4ed46869 KH |
296 | #include "coding.h" |
297 | #include "window.h" | |
b8299c66 KL |
298 | #include "frame.h" |
299 | #include "termhooks.h" | |
4ed46869 | 300 | |
df7492f9 | 301 | Lisp_Object Vcoding_system_hash_table; |
4ed46869 | 302 | |
df7492f9 | 303 | Lisp_Object Qcoding_system, Qcoding_aliases, Qeol_type; |
1965cb73 | 304 | Lisp_Object Qunix, Qdos; |
4ed46869 KH |
305 | Lisp_Object Qbuffer_file_coding_system; |
306 | Lisp_Object Qpost_read_conversion, Qpre_write_conversion; | |
df7492f9 | 307 | Lisp_Object Qdefault_char; |
27901516 | 308 | Lisp_Object Qno_conversion, Qundecided; |
df7492f9 | 309 | Lisp_Object Qcharset, Qiso_2022, Qutf_8, Qutf_16, Qshift_jis, Qbig5; |
b49a1807 | 310 | Lisp_Object Qbig, Qlittle; |
bb0115a2 | 311 | Lisp_Object Qcoding_system_history; |
1397dc18 | 312 | Lisp_Object Qvalid_codes; |
2133e2d1 | 313 | Lisp_Object QCcategory, QCmnemonic, QCdefault_char; |
a6f87d34 KH |
314 | Lisp_Object QCdecode_translation_table, QCencode_translation_table; |
315 | Lisp_Object QCpost_read_conversion, QCpre_write_conversion; | |
35befdaa | 316 | Lisp_Object QCascii_compatible_p; |
4ed46869 | 317 | |
387f6ba5 | 318 | Lisp_Object Qcall_process, Qcall_process_region; |
4ed46869 KH |
319 | Lisp_Object Qstart_process, Qopen_network_stream; |
320 | Lisp_Object Qtarget_idx; | |
321 | ||
065e3595 KH |
322 | Lisp_Object Qinsufficient_source, Qinconsistent_eol, Qinvalid_source; |
323 | Lisp_Object Qinterrupted, Qinsufficient_memory; | |
324 | ||
44e8490d KH |
325 | /* If a symbol has this property, evaluate the value to define the |
326 | symbol as a coding system. */ | |
327 | static Lisp_Object Qcoding_system_define_form; | |
328 | ||
fcbcfb64 KH |
329 | /* Format of end-of-line decided by system. This is Qunix on |
330 | Unix and Mac, Qdos on DOS/Windows. | |
331 | This has an effect only for external encoding (i.e. for output to | |
332 | file and process), not for in-buffer or Lisp string encoding. */ | |
333 | static Lisp_Object system_eol_type; | |
334 | ||
4ed46869 KH |
335 | #ifdef emacs |
336 | ||
4608c386 | 337 | Lisp_Object Qcoding_system_p, Qcoding_system_error; |
4ed46869 | 338 | |
d46c5b12 KH |
339 | /* Coding system emacs-mule and raw-text are for converting only |
340 | end-of-line format. */ | |
341 | Lisp_Object Qemacs_mule, Qraw_text; | |
8f924df7 | 342 | Lisp_Object Qutf_8_emacs; |
ecf488bc | 343 | |
4ed46869 KH |
344 | /* Coding-systems are handed between Emacs Lisp programs and C internal |
345 | routines by the following three variables. */ | |
c4825358 KH |
346 | /* Coding system to be used to encode text for terminal display when |
347 | terminal coding system is nil. */ | |
348 | struct coding_system safe_terminal_coding; | |
349 | ||
4ed46869 KH |
350 | #endif /* emacs */ |
351 | ||
f967223b KH |
352 | Lisp_Object Qtranslation_table; |
353 | Lisp_Object Qtranslation_table_id; | |
354 | Lisp_Object Qtranslation_table_for_decode; | |
355 | Lisp_Object Qtranslation_table_for_encode; | |
4ed46869 | 356 | |
df7492f9 KH |
357 | /* Two special coding systems. */ |
358 | Lisp_Object Vsjis_coding_system; | |
359 | Lisp_Object Vbig5_coding_system; | |
360 | ||
df7492f9 KH |
361 | /* ISO2022 section */ |
362 | ||
363 | #define CODING_ISO_INITIAL(coding, reg) \ | |
364 | (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \ | |
365 | coding_attr_iso_initial), \ | |
366 | reg))) | |
367 | ||
368 | ||
1b3b981b AS |
369 | #define CODING_ISO_REQUEST(coding, charset_id) \ |
370 | (((charset_id) <= (coding)->max_charset_id \ | |
371 | ? ((coding)->safe_charsets[charset_id] != 255 \ | |
372 | ? (coding)->safe_charsets[charset_id] \ | |
373 | : -1) \ | |
df7492f9 KH |
374 | : -1)) |
375 | ||
376 | ||
377 | #define CODING_ISO_FLAGS(coding) \ | |
378 | ((coding)->spec.iso_2022.flags) | |
379 | #define CODING_ISO_DESIGNATION(coding, reg) \ | |
380 | ((coding)->spec.iso_2022.current_designation[reg]) | |
381 | #define CODING_ISO_INVOCATION(coding, plane) \ | |
382 | ((coding)->spec.iso_2022.current_invocation[plane]) | |
383 | #define CODING_ISO_SINGLE_SHIFTING(coding) \ | |
384 | ((coding)->spec.iso_2022.single_shifting) | |
385 | #define CODING_ISO_BOL(coding) \ | |
386 | ((coding)->spec.iso_2022.bol) | |
387 | #define CODING_ISO_INVOKED_CHARSET(coding, plane) \ | |
388 | CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane))) | |
e951386e KH |
389 | #define CODING_ISO_CMP_STATUS(coding) \ |
390 | (&(coding)->spec.iso_2022.cmp_status) | |
391 | #define CODING_ISO_EXTSEGMENT_LEN(coding) \ | |
392 | ((coding)->spec.iso_2022.ctext_extended_segment_len) | |
393 | #define CODING_ISO_EMBEDDED_UTF_8(coding) \ | |
394 | ((coding)->spec.iso_2022.embedded_utf_8) | |
df7492f9 KH |
395 | |
396 | /* Control characters of ISO2022. */ | |
397 | /* code */ /* function */ | |
df7492f9 KH |
398 | #define ISO_CODE_SO 0x0E /* shift-out */ |
399 | #define ISO_CODE_SI 0x0F /* shift-in */ | |
400 | #define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */ | |
401 | #define ISO_CODE_ESC 0x1B /* escape */ | |
402 | #define ISO_CODE_SS2 0x8E /* single-shift-2 */ | |
403 | #define ISO_CODE_SS3 0x8F /* single-shift-3 */ | |
404 | #define ISO_CODE_CSI 0x9B /* control-sequence-introducer */ | |
405 | ||
406 | /* All code (1-byte) of ISO2022 is classified into one of the | |
407 | followings. */ | |
408 | enum iso_code_class_type | |
409 | { | |
410 | ISO_control_0, /* Control codes in the range | |
411 | 0x00..0x1F and 0x7F, except for the | |
412 | following 5 codes. */ | |
df7492f9 KH |
413 | ISO_shift_out, /* ISO_CODE_SO (0x0E) */ |
414 | ISO_shift_in, /* ISO_CODE_SI (0x0F) */ | |
415 | ISO_single_shift_2_7, /* ISO_CODE_SS2_7 (0x19) */ | |
416 | ISO_escape, /* ISO_CODE_SO (0x1B) */ | |
417 | ISO_control_1, /* Control codes in the range | |
418 | 0x80..0x9F, except for the | |
419 | following 3 codes. */ | |
420 | ISO_single_shift_2, /* ISO_CODE_SS2 (0x8E) */ | |
421 | ISO_single_shift_3, /* ISO_CODE_SS3 (0x8F) */ | |
422 | ISO_control_sequence_introducer, /* ISO_CODE_CSI (0x9B) */ | |
423 | ISO_0x20_or_0x7F, /* Codes of the values 0x20 or 0x7F. */ | |
424 | ISO_graphic_plane_0, /* Graphic codes in the range 0x21..0x7E. */ | |
425 | ISO_0xA0_or_0xFF, /* Codes of the values 0xA0 or 0xFF. */ | |
426 | ISO_graphic_plane_1 /* Graphic codes in the range 0xA1..0xFE. */ | |
427 | }; | |
05e6f5dc | 428 | |
df7492f9 KH |
429 | /** The macros CODING_ISO_FLAG_XXX defines a flag bit of the |
430 | `iso-flags' attribute of an iso2022 coding system. */ | |
05e6f5dc | 431 | |
df7492f9 KH |
432 | /* If set, produce long-form designation sequence (e.g. ESC $ ( A) |
433 | instead of the correct short-form sequence (e.g. ESC $ A). */ | |
434 | #define CODING_ISO_FLAG_LONG_FORM 0x0001 | |
93dec019 | 435 | |
df7492f9 KH |
436 | /* If set, reset graphic planes and registers at end-of-line to the |
437 | initial state. */ | |
438 | #define CODING_ISO_FLAG_RESET_AT_EOL 0x0002 | |
05e6f5dc | 439 | |
df7492f9 KH |
440 | /* If set, reset graphic planes and registers before any control |
441 | characters to the initial state. */ | |
442 | #define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004 | |
05e6f5dc | 443 | |
df7492f9 KH |
444 | /* If set, encode by 7-bit environment. */ |
445 | #define CODING_ISO_FLAG_SEVEN_BITS 0x0008 | |
4ed46869 | 446 | |
df7492f9 KH |
447 | /* If set, use locking-shift function. */ |
448 | #define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010 | |
b73bfc1c | 449 | |
df7492f9 KH |
450 | /* If set, use single-shift function. Overwrite |
451 | CODING_ISO_FLAG_LOCKING_SHIFT. */ | |
452 | #define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020 | |
b73bfc1c | 453 | |
df7492f9 KH |
454 | /* If set, use designation escape sequence. */ |
455 | #define CODING_ISO_FLAG_DESIGNATION 0x0040 | |
b73bfc1c | 456 | |
df7492f9 KH |
457 | /* If set, produce revision number sequence. */ |
458 | #define CODING_ISO_FLAG_REVISION 0x0080 | |
b73bfc1c | 459 | |
df7492f9 KH |
460 | /* If set, produce ISO6429's direction specifying sequence. */ |
461 | #define CODING_ISO_FLAG_DIRECTION 0x0100 | |
f4dee582 | 462 | |
df7492f9 KH |
463 | /* If set, assume designation states are reset at beginning of line on |
464 | output. */ | |
465 | #define CODING_ISO_FLAG_INIT_AT_BOL 0x0200 | |
4ed46869 | 466 | |
df7492f9 KH |
467 | /* If set, designation sequence should be placed at beginning of line |
468 | on output. */ | |
469 | #define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400 | |
aa72b389 | 470 | |
ad1746f5 | 471 | /* If set, do not encode unsafe characters on output. */ |
df7492f9 | 472 | #define CODING_ISO_FLAG_SAFE 0x0800 |
aa72b389 | 473 | |
df7492f9 KH |
474 | /* If set, extra latin codes (128..159) are accepted as a valid code |
475 | on input. */ | |
476 | #define CODING_ISO_FLAG_LATIN_EXTRA 0x1000 | |
aa72b389 | 477 | |
df7492f9 | 478 | #define CODING_ISO_FLAG_COMPOSITION 0x2000 |
aa72b389 | 479 | |
5f58e762 | 480 | /* #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000 */ |
aa72b389 | 481 | |
bf16eb23 | 482 | #define CODING_ISO_FLAG_USE_ROMAN 0x8000 |
aa72b389 | 483 | |
bf16eb23 | 484 | #define CODING_ISO_FLAG_USE_OLDJIS 0x10000 |
aa72b389 | 485 | |
bf16eb23 | 486 | #define CODING_ISO_FLAG_FULL_SUPPORT 0x100000 |
aa72b389 | 487 | |
df7492f9 KH |
488 | /* A character to be produced on output if encoding of the original |
489 | character is prohibited by CODING_ISO_FLAG_SAFE. */ | |
490 | #define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?' | |
aa72b389 | 491 | |
a470d443 KH |
492 | /* UTF-8 section */ |
493 | #define CODING_UTF_8_BOM(coding) \ | |
494 | ((coding)->spec.utf_8_bom) | |
4ed46869 | 495 | |
df7492f9 KH |
496 | /* UTF-16 section */ |
497 | #define CODING_UTF_16_BOM(coding) \ | |
498 | ((coding)->spec.utf_16.bom) | |
4ed46869 | 499 | |
df7492f9 KH |
500 | #define CODING_UTF_16_ENDIAN(coding) \ |
501 | ((coding)->spec.utf_16.endian) | |
4ed46869 | 502 | |
df7492f9 KH |
503 | #define CODING_UTF_16_SURROGATE(coding) \ |
504 | ((coding)->spec.utf_16.surrogate) | |
4ed46869 | 505 | |
4ed46869 | 506 | |
df7492f9 KH |
507 | /* CCL section */ |
508 | #define CODING_CCL_DECODER(coding) \ | |
509 | AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder) | |
510 | #define CODING_CCL_ENCODER(coding) \ | |
511 | AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder) | |
512 | #define CODING_CCL_VALIDS(coding) \ | |
8f924df7 | 513 | (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids))) |
4ed46869 | 514 | |
5a936b46 | 515 | /* Index for each coding category in `coding_categories' */ |
4ed46869 | 516 | |
df7492f9 KH |
517 | enum coding_category |
518 | { | |
519 | coding_category_iso_7, | |
520 | coding_category_iso_7_tight, | |
521 | coding_category_iso_8_1, | |
522 | coding_category_iso_8_2, | |
523 | coding_category_iso_7_else, | |
524 | coding_category_iso_8_else, | |
a470d443 KH |
525 | coding_category_utf_8_auto, |
526 | coding_category_utf_8_nosig, | |
527 | coding_category_utf_8_sig, | |
df7492f9 KH |
528 | coding_category_utf_16_auto, |
529 | coding_category_utf_16_be, | |
530 | coding_category_utf_16_le, | |
531 | coding_category_utf_16_be_nosig, | |
532 | coding_category_utf_16_le_nosig, | |
533 | coding_category_charset, | |
534 | coding_category_sjis, | |
535 | coding_category_big5, | |
536 | coding_category_ccl, | |
537 | coding_category_emacs_mule, | |
538 | /* All above are targets of code detection. */ | |
539 | coding_category_raw_text, | |
540 | coding_category_undecided, | |
541 | coding_category_max | |
542 | }; | |
543 | ||
544 | /* Definitions of flag bits used in detect_coding_XXXX. */ | |
545 | #define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7) | |
546 | #define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight) | |
547 | #define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1) | |
548 | #define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2) | |
549 | #define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else) | |
550 | #define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else) | |
a470d443 KH |
551 | #define CATEGORY_MASK_UTF_8_AUTO (1 << coding_category_utf_8_auto) |
552 | #define CATEGORY_MASK_UTF_8_NOSIG (1 << coding_category_utf_8_nosig) | |
553 | #define CATEGORY_MASK_UTF_8_SIG (1 << coding_category_utf_8_sig) | |
b49a1807 | 554 | #define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto) |
df7492f9 KH |
555 | #define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be) |
556 | #define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le) | |
557 | #define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig) | |
558 | #define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig) | |
559 | #define CATEGORY_MASK_CHARSET (1 << coding_category_charset) | |
560 | #define CATEGORY_MASK_SJIS (1 << coding_category_sjis) | |
561 | #define CATEGORY_MASK_BIG5 (1 << coding_category_big5) | |
562 | #define CATEGORY_MASK_CCL (1 << coding_category_ccl) | |
563 | #define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule) | |
ff0dacd7 | 564 | #define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text) |
df7492f9 KH |
565 | |
566 | /* This value is returned if detect_coding_mask () find nothing other | |
567 | than ASCII characters. */ | |
568 | #define CATEGORY_MASK_ANY \ | |
569 | (CATEGORY_MASK_ISO_7 \ | |
570 | | CATEGORY_MASK_ISO_7_TIGHT \ | |
571 | | CATEGORY_MASK_ISO_8_1 \ | |
572 | | CATEGORY_MASK_ISO_8_2 \ | |
573 | | CATEGORY_MASK_ISO_7_ELSE \ | |
574 | | CATEGORY_MASK_ISO_8_ELSE \ | |
a470d443 KH |
575 | | CATEGORY_MASK_UTF_8_AUTO \ |
576 | | CATEGORY_MASK_UTF_8_NOSIG \ | |
577 | | CATEGORY_MASK_UTF_8_SIG \ | |
2f3cbb32 | 578 | | CATEGORY_MASK_UTF_16_AUTO \ |
df7492f9 KH |
579 | | CATEGORY_MASK_UTF_16_BE \ |
580 | | CATEGORY_MASK_UTF_16_LE \ | |
581 | | CATEGORY_MASK_UTF_16_BE_NOSIG \ | |
582 | | CATEGORY_MASK_UTF_16_LE_NOSIG \ | |
583 | | CATEGORY_MASK_CHARSET \ | |
584 | | CATEGORY_MASK_SJIS \ | |
585 | | CATEGORY_MASK_BIG5 \ | |
586 | | CATEGORY_MASK_CCL \ | |
587 | | CATEGORY_MASK_EMACS_MULE) | |
588 | ||
589 | ||
590 | #define CATEGORY_MASK_ISO_7BIT \ | |
591 | (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT) | |
592 | ||
593 | #define CATEGORY_MASK_ISO_8BIT \ | |
594 | (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2) | |
595 | ||
596 | #define CATEGORY_MASK_ISO_ELSE \ | |
597 | (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE) | |
598 | ||
599 | #define CATEGORY_MASK_ISO_ESCAPE \ | |
600 | (CATEGORY_MASK_ISO_7 \ | |
601 | | CATEGORY_MASK_ISO_7_TIGHT \ | |
602 | | CATEGORY_MASK_ISO_7_ELSE \ | |
603 | | CATEGORY_MASK_ISO_8_ELSE) | |
604 | ||
605 | #define CATEGORY_MASK_ISO \ | |
606 | ( CATEGORY_MASK_ISO_7BIT \ | |
607 | | CATEGORY_MASK_ISO_8BIT \ | |
608 | | CATEGORY_MASK_ISO_ELSE) | |
609 | ||
610 | #define CATEGORY_MASK_UTF_16 \ | |
2f3cbb32 KH |
611 | (CATEGORY_MASK_UTF_16_AUTO \ |
612 | | CATEGORY_MASK_UTF_16_BE \ | |
df7492f9 KH |
613 | | CATEGORY_MASK_UTF_16_LE \ |
614 | | CATEGORY_MASK_UTF_16_BE_NOSIG \ | |
615 | | CATEGORY_MASK_UTF_16_LE_NOSIG) | |
616 | ||
a470d443 KH |
617 | #define CATEGORY_MASK_UTF_8 \ |
618 | (CATEGORY_MASK_UTF_8_AUTO \ | |
619 | | CATEGORY_MASK_UTF_8_NOSIG \ | |
620 | | CATEGORY_MASK_UTF_8_SIG) | |
df7492f9 | 621 | |
df7492f9 | 622 | /* Table of coding categories (Lisp symbols). This variable is for |
ad1746f5 | 623 | internal use only. */ |
df7492f9 KH |
624 | static Lisp_Object Vcoding_category_table; |
625 | ||
626 | /* Table of coding-categories ordered by priority. */ | |
627 | static enum coding_category coding_priorities[coding_category_max]; | |
628 | ||
629 | /* Nth element is a coding context for the coding system bound to the | |
630 | Nth coding category. */ | |
631 | static struct coding_system coding_categories[coding_category_max]; | |
632 | ||
df7492f9 KH |
633 | /*** Commonly used macros and functions ***/ |
634 | ||
635 | #ifndef min | |
636 | #define min(a, b) ((a) < (b) ? (a) : (b)) | |
637 | #endif | |
638 | #ifndef max | |
639 | #define max(a, b) ((a) > (b) ? (a) : (b)) | |
640 | #endif | |
4ed46869 | 641 | |
24a73b0a KH |
642 | #define CODING_GET_INFO(coding, attrs, charset_list) \ |
643 | do { \ | |
644 | (attrs) = CODING_ID_ATTRS ((coding)->id); \ | |
645 | (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \ | |
df7492f9 | 646 | } while (0) |
4ed46869 | 647 | |
4ed46869 | 648 | |
df7492f9 KH |
649 | /* Safely get one byte from the source text pointed by SRC which ends |
650 | at SRC_END, and set C to that byte. If there are not enough bytes | |
065e3595 KH |
651 | in the source, it jumps to `no_more_source'. If multibytep is |
652 | nonzero, and a multibyte character is found at SRC, set C to the | |
653 | negative value of the character code. The caller should declare | |
654 | and set these variables appropriately in advance: | |
655 | src, src_end, multibytep */ | |
aa72b389 | 656 | |
065e3595 KH |
657 | #define ONE_MORE_BYTE(c) \ |
658 | do { \ | |
659 | if (src == src_end) \ | |
660 | { \ | |
661 | if (src_base < src) \ | |
662 | record_conversion_result \ | |
663 | (coding, CODING_RESULT_INSUFFICIENT_SRC); \ | |
664 | goto no_more_source; \ | |
665 | } \ | |
666 | c = *src++; \ | |
667 | if (multibytep && (c & 0x80)) \ | |
668 | { \ | |
669 | if ((c & 0xFE) == 0xC0) \ | |
670 | c = ((c & 1) << 6) | *src++; \ | |
671 | else \ | |
672 | { \ | |
35befdaa KH |
673 | src--; \ |
674 | c = - string_char (src, &src, NULL); \ | |
065e3595 KH |
675 | record_conversion_result \ |
676 | (coding, CODING_RESULT_INVALID_SRC); \ | |
677 | } \ | |
678 | } \ | |
679 | consumed_chars++; \ | |
aa72b389 KH |
680 | } while (0) |
681 | ||
f56a4450 | 682 | /* Safely get two bytes from the source text pointed by SRC which ends |
220eeac9 KH |
683 | at SRC_END, and set C1 and C2 to those bytes while skipping the |
684 | heading multibyte characters. If there are not enough bytes in the | |
685 | source, it jumps to `no_more_source'. If multibytep is nonzero and | |
686 | a multibyte character is found for C2, set C2 to the negative value | |
687 | of the character code. The caller should declare and set these | |
688 | variables appropriately in advance: | |
f56a4450 KH |
689 | src, src_end, multibytep |
690 | It is intended that this macro is used in detect_coding_utf_16. */ | |
691 | ||
220eeac9 KH |
692 | #define TWO_MORE_BYTES(c1, c2) \ |
693 | do { \ | |
694 | do { \ | |
695 | if (src == src_end) \ | |
696 | goto no_more_source; \ | |
697 | c1 = *src++; \ | |
698 | if (multibytep && (c1 & 0x80)) \ | |
699 | { \ | |
700 | if ((c1 & 0xFE) == 0xC0) \ | |
701 | c1 = ((c1 & 1) << 6) | *src++; \ | |
702 | else \ | |
703 | { \ | |
704 | src += BYTES_BY_CHAR_HEAD (c1) - 1; \ | |
705 | c1 = -1; \ | |
706 | } \ | |
707 | } \ | |
708 | } while (c1 < 0); \ | |
709 | if (src == src_end) \ | |
710 | goto no_more_source; \ | |
711 | c2 = *src++; \ | |
712 | if (multibytep && (c2 & 0x80)) \ | |
713 | { \ | |
714 | if ((c2 & 0xFE) == 0xC0) \ | |
715 | c2 = ((c2 & 1) << 6) | *src++; \ | |
716 | else \ | |
717 | c2 = -1; \ | |
718 | } \ | |
f56a4450 KH |
719 | } while (0) |
720 | ||
aa72b389 | 721 | |
df7492f9 KH |
722 | /* Store a byte C in the place pointed by DST and increment DST to the |
723 | next free point, and increment PRODUCED_CHARS. The caller should | |
724 | assure that C is 0..127, and declare and set the variable `dst' | |
725 | appropriately in advance. | |
726 | */ | |
aa72b389 KH |
727 | |
728 | ||
df7492f9 KH |
729 | #define EMIT_ONE_ASCII_BYTE(c) \ |
730 | do { \ | |
731 | produced_chars++; \ | |
732 | *dst++ = (c); \ | |
b6871cc7 | 733 | } while (0) |
aa72b389 KH |
734 | |
735 | ||
ad1746f5 | 736 | /* Like EMIT_ONE_ASCII_BYTE but store two bytes; C1 and C2. */ |
aa72b389 | 737 | |
df7492f9 KH |
738 | #define EMIT_TWO_ASCII_BYTES(c1, c2) \ |
739 | do { \ | |
740 | produced_chars += 2; \ | |
741 | *dst++ = (c1), *dst++ = (c2); \ | |
742 | } while (0) | |
aa72b389 KH |
743 | |
744 | ||
df7492f9 KH |
745 | /* Store a byte C in the place pointed by DST and increment DST to the |
746 | next free point, and increment PRODUCED_CHARS. If MULTIBYTEP is | |
747 | nonzero, store in an appropriate multibyte from. The caller should | |
748 | declare and set the variables `dst' and `multibytep' appropriately | |
749 | in advance. */ | |
750 | ||
751 | #define EMIT_ONE_BYTE(c) \ | |
752 | do { \ | |
753 | produced_chars++; \ | |
754 | if (multibytep) \ | |
755 | { \ | |
756 | int ch = (c); \ | |
757 | if (ch >= 0x80) \ | |
758 | ch = BYTE8_TO_CHAR (ch); \ | |
759 | CHAR_STRING_ADVANCE (ch, dst); \ | |
760 | } \ | |
761 | else \ | |
762 | *dst++ = (c); \ | |
aa72b389 | 763 | } while (0) |
aa72b389 | 764 | |
aa72b389 | 765 | |
df7492f9 | 766 | /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */ |
aa72b389 | 767 | |
e19c3639 KH |
768 | #define EMIT_TWO_BYTES(c1, c2) \ |
769 | do { \ | |
770 | produced_chars += 2; \ | |
771 | if (multibytep) \ | |
772 | { \ | |
773 | int ch; \ | |
774 | \ | |
775 | ch = (c1); \ | |
776 | if (ch >= 0x80) \ | |
777 | ch = BYTE8_TO_CHAR (ch); \ | |
778 | CHAR_STRING_ADVANCE (ch, dst); \ | |
779 | ch = (c2); \ | |
780 | if (ch >= 0x80) \ | |
781 | ch = BYTE8_TO_CHAR (ch); \ | |
782 | CHAR_STRING_ADVANCE (ch, dst); \ | |
783 | } \ | |
784 | else \ | |
785 | { \ | |
786 | *dst++ = (c1); \ | |
787 | *dst++ = (c2); \ | |
788 | } \ | |
aa72b389 KH |
789 | } while (0) |
790 | ||
791 | ||
df7492f9 KH |
792 | #define EMIT_THREE_BYTES(c1, c2, c3) \ |
793 | do { \ | |
794 | EMIT_ONE_BYTE (c1); \ | |
795 | EMIT_TWO_BYTES (c2, c3); \ | |
796 | } while (0) | |
aa72b389 | 797 | |
aa72b389 | 798 | |
df7492f9 KH |
799 | #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \ |
800 | do { \ | |
801 | EMIT_TWO_BYTES (c1, c2); \ | |
802 | EMIT_TWO_BYTES (c3, c4); \ | |
803 | } while (0) | |
aa72b389 | 804 | |
aa72b389 | 805 | |
f6cbaf43 | 806 | /* Prototypes for static functions. */ |
f57e2426 J |
807 | static void record_conversion_result (struct coding_system *coding, |
808 | enum coding_result_code result); | |
809 | static int detect_coding_utf_8 (struct coding_system *, | |
810 | struct coding_detection_info *info); | |
811 | static void decode_coding_utf_8 (struct coding_system *); | |
812 | static int encode_coding_utf_8 (struct coding_system *); | |
813 | ||
814 | static int detect_coding_utf_16 (struct coding_system *, | |
815 | struct coding_detection_info *info); | |
816 | static void decode_coding_utf_16 (struct coding_system *); | |
817 | static int encode_coding_utf_16 (struct coding_system *); | |
818 | ||
819 | static int detect_coding_iso_2022 (struct coding_system *, | |
820 | struct coding_detection_info *info); | |
821 | static void decode_coding_iso_2022 (struct coding_system *); | |
822 | static int encode_coding_iso_2022 (struct coding_system *); | |
823 | ||
824 | static int detect_coding_emacs_mule (struct coding_system *, | |
825 | struct coding_detection_info *info); | |
826 | static void decode_coding_emacs_mule (struct coding_system *); | |
827 | static int encode_coding_emacs_mule (struct coding_system *); | |
828 | ||
829 | static int detect_coding_sjis (struct coding_system *, | |
830 | struct coding_detection_info *info); | |
831 | static void decode_coding_sjis (struct coding_system *); | |
832 | static int encode_coding_sjis (struct coding_system *); | |
833 | ||
834 | static int detect_coding_big5 (struct coding_system *, | |
835 | struct coding_detection_info *info); | |
836 | static void decode_coding_big5 (struct coding_system *); | |
837 | static int encode_coding_big5 (struct coding_system *); | |
838 | ||
839 | static int detect_coding_ccl (struct coding_system *, | |
840 | struct coding_detection_info *info); | |
841 | static void decode_coding_ccl (struct coding_system *); | |
842 | static int encode_coding_ccl (struct coding_system *); | |
843 | ||
844 | static void decode_coding_raw_text (struct coding_system *); | |
845 | static int encode_coding_raw_text (struct coding_system *); | |
846 | ||
847 | static void coding_set_source (struct coding_system *); | |
848 | static void coding_set_destination (struct coding_system *); | |
849 | static void coding_alloc_by_realloc (struct coding_system *, EMACS_INT); | |
850 | static void coding_alloc_by_making_gap (struct coding_system *, | |
851 | EMACS_INT, EMACS_INT); | |
852 | static unsigned char *alloc_destination (struct coding_system *, | |
853 | EMACS_INT, unsigned char *); | |
854 | static void setup_iso_safe_charsets (Lisp_Object); | |
855 | static unsigned char *encode_designation_at_bol (struct coding_system *, | |
856 | int *, int *, | |
857 | unsigned char *); | |
858 | static int detect_eol (const unsigned char *, | |
859 | EMACS_INT, enum coding_category); | |
860 | static Lisp_Object adjust_coding_eol_type (struct coding_system *, int); | |
861 | static void decode_eol (struct coding_system *); | |
862 | static Lisp_Object get_translation_table (Lisp_Object, int, int *); | |
863 | static Lisp_Object get_translation (Lisp_Object, int *, int *); | |
864 | static int produce_chars (struct coding_system *, Lisp_Object, int); | |
865 | static INLINE void produce_charset (struct coding_system *, int *, | |
866 | EMACS_INT); | |
867 | static void produce_annotation (struct coding_system *, EMACS_INT); | |
868 | static int decode_coding (struct coding_system *); | |
869 | static INLINE int *handle_composition_annotation (EMACS_INT, EMACS_INT, | |
870 | struct coding_system *, | |
871 | int *, EMACS_INT *); | |
872 | static INLINE int *handle_charset_annotation (EMACS_INT, EMACS_INT, | |
873 | struct coding_system *, | |
874 | int *, EMACS_INT *); | |
875 | static void consume_chars (struct coding_system *, Lisp_Object, int); | |
876 | static int encode_coding (struct coding_system *); | |
877 | static Lisp_Object make_conversion_work_buffer (int); | |
878 | static Lisp_Object code_conversion_restore (Lisp_Object); | |
879 | static INLINE int char_encodable_p (int, Lisp_Object); | |
880 | static Lisp_Object make_subsidiaries (Lisp_Object); | |
f6cbaf43 | 881 | |
065e3595 KH |
882 | static void |
883 | record_conversion_result (struct coding_system *coding, | |
884 | enum coding_result_code result) | |
885 | { | |
886 | coding->result = result; | |
887 | switch (result) | |
888 | { | |
889 | case CODING_RESULT_INSUFFICIENT_SRC: | |
890 | Vlast_code_conversion_error = Qinsufficient_source; | |
891 | break; | |
892 | case CODING_RESULT_INCONSISTENT_EOL: | |
893 | Vlast_code_conversion_error = Qinconsistent_eol; | |
894 | break; | |
895 | case CODING_RESULT_INVALID_SRC: | |
896 | Vlast_code_conversion_error = Qinvalid_source; | |
897 | break; | |
898 | case CODING_RESULT_INTERRUPT: | |
899 | Vlast_code_conversion_error = Qinterrupted; | |
900 | break; | |
901 | case CODING_RESULT_INSUFFICIENT_MEM: | |
902 | Vlast_code_conversion_error = Qinsufficient_memory; | |
903 | break; | |
ebaf11b6 KH |
904 | case CODING_RESULT_INSUFFICIENT_DST: |
905 | /* Don't record this error in Vlast_code_conversion_error | |
906 | because it happens just temporarily and is resolved when the | |
907 | whole conversion is finished. */ | |
908 | break; | |
409ea3a1 AS |
909 | case CODING_RESULT_SUCCESS: |
910 | break; | |
35befdaa KH |
911 | default: |
912 | Vlast_code_conversion_error = intern ("Unknown error"); | |
065e3595 KH |
913 | } |
914 | } | |
915 | ||
75f80e63 EZ |
916 | /* This wrapper macro is used to preserve validity of pointers into |
917 | buffer text across calls to decode_char, which could cause | |
918 | relocation of buffers if it loads a charset map, because loading a | |
919 | charset map allocates large structures. */ | |
df7492f9 KH |
920 | #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \ |
921 | do { \ | |
922 | charset_map_loaded = 0; \ | |
923 | c = DECODE_CHAR (charset, code); \ | |
924 | if (charset_map_loaded) \ | |
925 | { \ | |
8f924df7 | 926 | const unsigned char *orig = coding->source; \ |
df7492f9 KH |
927 | EMACS_INT offset; \ |
928 | \ | |
929 | coding_set_source (coding); \ | |
930 | offset = coding->source - orig; \ | |
931 | src += offset; \ | |
932 | src_base += offset; \ | |
933 | src_end += offset; \ | |
934 | } \ | |
aa72b389 KH |
935 | } while (0) |
936 | ||
937 | ||
119852e7 KH |
938 | /* If there are at least BYTES length of room at dst, allocate memory |
939 | for coding->destination and update dst and dst_end. We don't have | |
940 | to take care of coding->source which will be relocated. It is | |
941 | handled by calling coding_set_source in encode_coding. */ | |
942 | ||
df7492f9 KH |
943 | #define ASSURE_DESTINATION(bytes) \ |
944 | do { \ | |
945 | if (dst + (bytes) >= dst_end) \ | |
946 | { \ | |
947 | int more_bytes = charbuf_end - charbuf + (bytes); \ | |
948 | \ | |
949 | dst = alloc_destination (coding, more_bytes, dst); \ | |
950 | dst_end = coding->destination + coding->dst_bytes; \ | |
951 | } \ | |
952 | } while (0) | |
aa72b389 | 953 | |
aa72b389 | 954 | |
db274c7a KH |
955 | /* Store multibyte form of the character C in P, and advance P to the |
956 | end of the multibyte form. This is like CHAR_STRING_ADVANCE but it | |
957 | never calls MAYBE_UNIFY_CHAR. */ | |
958 | ||
959 | #define CHAR_STRING_ADVANCE_NO_UNIFY(c, p) \ | |
960 | do { \ | |
961 | if ((c) <= MAX_1_BYTE_CHAR) \ | |
962 | *(p)++ = (c); \ | |
963 | else if ((c) <= MAX_2_BYTE_CHAR) \ | |
964 | *(p)++ = (0xC0 | ((c) >> 6)), \ | |
965 | *(p)++ = (0x80 | ((c) & 0x3F)); \ | |
966 | else if ((c) <= MAX_3_BYTE_CHAR) \ | |
967 | *(p)++ = (0xE0 | ((c) >> 12)), \ | |
968 | *(p)++ = (0x80 | (((c) >> 6) & 0x3F)), \ | |
969 | *(p)++ = (0x80 | ((c) & 0x3F)); \ | |
970 | else if ((c) <= MAX_4_BYTE_CHAR) \ | |
971 | *(p)++ = (0xF0 | (c >> 18)), \ | |
972 | *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \ | |
973 | *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \ | |
974 | *(p)++ = (0x80 | (c & 0x3F)); \ | |
975 | else if ((c) <= MAX_5_BYTE_CHAR) \ | |
976 | *(p)++ = 0xF8, \ | |
977 | *(p)++ = (0x80 | ((c >> 18) & 0x0F)), \ | |
978 | *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \ | |
979 | *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \ | |
980 | *(p)++ = (0x80 | (c & 0x3F)); \ | |
981 | else \ | |
982 | (p) += BYTE8_STRING ((c) - 0x3FFF80, p); \ | |
983 | } while (0) | |
984 | ||
985 | ||
986 | /* Return the character code of character whose multibyte form is at | |
987 | P, and advance P to the end of the multibyte form. This is like | |
988 | STRING_CHAR_ADVANCE, but it never calls MAYBE_UNIFY_CHAR. */ | |
989 | ||
990 | #define STRING_CHAR_ADVANCE_NO_UNIFY(p) \ | |
991 | (!((p)[0] & 0x80) \ | |
992 | ? *(p)++ \ | |
993 | : ! ((p)[0] & 0x20) \ | |
994 | ? ((p) += 2, \ | |
995 | ((((p)[-2] & 0x1F) << 6) \ | |
996 | | ((p)[-1] & 0x3F) \ | |
997 | | ((unsigned char) ((p)[-2]) < 0xC2 ? 0x3FFF80 : 0))) \ | |
998 | : ! ((p)[0] & 0x10) \ | |
999 | ? ((p) += 3, \ | |
1000 | ((((p)[-3] & 0x0F) << 12) \ | |
1001 | | (((p)[-2] & 0x3F) << 6) \ | |
1002 | | ((p)[-1] & 0x3F))) \ | |
1003 | : ! ((p)[0] & 0x08) \ | |
1004 | ? ((p) += 4, \ | |
1005 | ((((p)[-4] & 0xF) << 18) \ | |
1006 | | (((p)[-3] & 0x3F) << 12) \ | |
1007 | | (((p)[-2] & 0x3F) << 6) \ | |
1008 | | ((p)[-1] & 0x3F))) \ | |
1009 | : ((p) += 5, \ | |
1010 | ((((p)[-4] & 0x3F) << 18) \ | |
1011 | | (((p)[-3] & 0x3F) << 12) \ | |
1012 | | (((p)[-2] & 0x3F) << 6) \ | |
1013 | | ((p)[-1] & 0x3F)))) | |
1014 | ||
aa72b389 | 1015 | |
df7492f9 | 1016 | static void |
971de7fb | 1017 | coding_set_source (struct coding_system *coding) |
aa72b389 | 1018 | { |
df7492f9 KH |
1019 | if (BUFFERP (coding->src_object)) |
1020 | { | |
2cb26057 | 1021 | struct buffer *buf = XBUFFER (coding->src_object); |
aa72b389 | 1022 | |
df7492f9 | 1023 | if (coding->src_pos < 0) |
2cb26057 | 1024 | coding->source = BUF_GAP_END_ADDR (buf) + coding->src_pos_byte; |
df7492f9 | 1025 | else |
2cb26057 | 1026 | coding->source = BUF_BYTE_ADDRESS (buf, coding->src_pos_byte); |
aa72b389 | 1027 | } |
df7492f9 | 1028 | else if (STRINGP (coding->src_object)) |
aa72b389 | 1029 | { |
8f924df7 | 1030 | coding->source = SDATA (coding->src_object) + coding->src_pos_byte; |
aa72b389 | 1031 | } |
df7492f9 | 1032 | else |
f38b440c PE |
1033 | { |
1034 | /* Otherwise, the source is C string and is never relocated | |
1035 | automatically. Thus we don't have to update anything. */ | |
1036 | } | |
df7492f9 | 1037 | } |
aa72b389 | 1038 | |
df7492f9 | 1039 | static void |
971de7fb | 1040 | coding_set_destination (struct coding_system *coding) |
df7492f9 KH |
1041 | { |
1042 | if (BUFFERP (coding->dst_object)) | |
aa72b389 | 1043 | { |
df7492f9 | 1044 | if (coding->src_pos < 0) |
aa72b389 | 1045 | { |
13818c30 | 1046 | coding->destination = BEG_ADDR + coding->dst_pos_byte - BEG_BYTE; |
28f67a95 KH |
1047 | coding->dst_bytes = (GAP_END_ADDR |
1048 | - (coding->src_bytes - coding->consumed) | |
1049 | - coding->destination); | |
aa72b389 | 1050 | } |
df7492f9 | 1051 | else |
28f67a95 KH |
1052 | { |
1053 | /* We are sure that coding->dst_pos_byte is before the gap | |
1054 | of the buffer. */ | |
1055 | coding->destination = (BUF_BEG_ADDR (XBUFFER (coding->dst_object)) | |
13818c30 | 1056 | + coding->dst_pos_byte - BEG_BYTE); |
28f67a95 KH |
1057 | coding->dst_bytes = (BUF_GAP_END_ADDR (XBUFFER (coding->dst_object)) |
1058 | - coding->destination); | |
1059 | } | |
df7492f9 KH |
1060 | } |
1061 | else | |
f38b440c PE |
1062 | { |
1063 | /* Otherwise, the destination is C string and is never relocated | |
1064 | automatically. Thus we don't have to update anything. */ | |
1065 | } | |
df7492f9 KH |
1066 | } |
1067 | ||
1068 | ||
1069 | static void | |
971de7fb | 1070 | coding_alloc_by_realloc (struct coding_system *coding, EMACS_INT bytes) |
df7492f9 KH |
1071 | { |
1072 | coding->destination = (unsigned char *) xrealloc (coding->destination, | |
1073 | coding->dst_bytes + bytes); | |
1074 | coding->dst_bytes += bytes; | |
1075 | } | |
1076 | ||
1077 | static void | |
cf84bb53 JB |
1078 | coding_alloc_by_making_gap (struct coding_system *coding, |
1079 | EMACS_INT gap_head_used, EMACS_INT bytes) | |
df7492f9 | 1080 | { |
db274c7a | 1081 | if (EQ (coding->src_object, coding->dst_object)) |
df7492f9 | 1082 | { |
db274c7a KH |
1083 | /* The gap may contain the produced data at the head and not-yet |
1084 | consumed data at the tail. To preserve those data, we at | |
1085 | first make the gap size to zero, then increase the gap | |
1086 | size. */ | |
1087 | EMACS_INT add = GAP_SIZE; | |
1088 | ||
1089 | GPT += gap_head_used, GPT_BYTE += gap_head_used; | |
1090 | GAP_SIZE = 0; ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add; | |
df7492f9 KH |
1091 | make_gap (bytes); |
1092 | GAP_SIZE += add; ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add; | |
db274c7a | 1093 | GPT -= gap_head_used, GPT_BYTE -= gap_head_used; |
df7492f9 | 1094 | } |
730fff51 | 1095 | else |
df7492f9 | 1096 | { |
2c78b7e1 KH |
1097 | Lisp_Object this_buffer; |
1098 | ||
1099 | this_buffer = Fcurrent_buffer (); | |
df7492f9 KH |
1100 | set_buffer_internal (XBUFFER (coding->dst_object)); |
1101 | make_gap (bytes); | |
1102 | set_buffer_internal (XBUFFER (this_buffer)); | |
aa72b389 | 1103 | } |
df7492f9 | 1104 | } |
8f924df7 | 1105 | |
df7492f9 KH |
1106 | |
1107 | static unsigned char * | |
cf84bb53 JB |
1108 | alloc_destination (struct coding_system *coding, EMACS_INT nbytes, |
1109 | unsigned char *dst) | |
df7492f9 KH |
1110 | { |
1111 | EMACS_INT offset = dst - coding->destination; | |
1112 | ||
1113 | if (BUFFERP (coding->dst_object)) | |
db274c7a KH |
1114 | { |
1115 | struct buffer *buf = XBUFFER (coding->dst_object); | |
1116 | ||
1117 | coding_alloc_by_making_gap (coding, dst - BUF_GPT_ADDR (buf), nbytes); | |
1118 | } | |
aa72b389 | 1119 | else |
df7492f9 | 1120 | coding_alloc_by_realloc (coding, nbytes); |
df7492f9 KH |
1121 | coding_set_destination (coding); |
1122 | dst = coding->destination + offset; | |
1123 | return dst; | |
1124 | } | |
aa72b389 | 1125 | |
ff0dacd7 KH |
1126 | /** Macros for annotations. */ |
1127 | ||
ff0dacd7 KH |
1128 | /* An annotation data is stored in the array coding->charbuf in this |
1129 | format: | |
69a80ea3 | 1130 | [ -LENGTH ANNOTATION_MASK NCHARS ... ] |
ff0dacd7 KH |
1131 | LENGTH is the number of elements in the annotation. |
1132 | ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK. | |
69a80ea3 | 1133 | NCHARS is the number of characters in the text annotated. |
ff0dacd7 KH |
1134 | |
1135 | The format of the following elements depend on ANNOTATION_MASK. | |
1136 | ||
1137 | In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements | |
1138 | follows: | |
e951386e KH |
1139 | ... NBYTES METHOD [ COMPOSITION-COMPONENTS ... ] |
1140 | ||
1141 | NBYTES is the number of bytes specified in the header part of | |
1142 | old-style emacs-mule encoding, or 0 for the other kind of | |
1143 | composition. | |
1144 | ||
ff0dacd7 | 1145 | METHOD is one of enum composition_method. |
e951386e | 1146 | |
ad1746f5 | 1147 | Optional COMPOSITION-COMPONENTS are characters and composition |
ff0dacd7 KH |
1148 | rules. |
1149 | ||
1150 | In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID | |
e951386e KH |
1151 | follows. |
1152 | ||
1153 | If ANNOTATION_MASK is 0, this annotation is just a space holder to | |
1154 | recover from an invalid annotation, and should be skipped by | |
1155 | produce_annotation. */ | |
1156 | ||
1157 | /* Maximum length of the header of annotation data. */ | |
1158 | #define MAX_ANNOTATION_LENGTH 5 | |
ff0dacd7 | 1159 | |
69a80ea3 | 1160 | #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \ |
ff0dacd7 KH |
1161 | do { \ |
1162 | *(buf)++ = -(len); \ | |
1163 | *(buf)++ = (mask); \ | |
69a80ea3 | 1164 | *(buf)++ = (nchars); \ |
ff0dacd7 KH |
1165 | coding->annotated = 1; \ |
1166 | } while (0); | |
1167 | ||
e951386e | 1168 | #define ADD_COMPOSITION_DATA(buf, nchars, nbytes, method) \ |
69a80ea3 | 1169 | do { \ |
e951386e KH |
1170 | ADD_ANNOTATION_DATA (buf, 5, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \ |
1171 | *buf++ = nbytes; \ | |
69a80ea3 | 1172 | *buf++ = method; \ |
ff0dacd7 KH |
1173 | } while (0) |
1174 | ||
1175 | ||
69a80ea3 KH |
1176 | #define ADD_CHARSET_DATA(buf, nchars, id) \ |
1177 | do { \ | |
1178 | ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \ | |
1179 | *buf++ = id; \ | |
ff0dacd7 KH |
1180 | } while (0) |
1181 | ||
df7492f9 KH |
1182 | \f |
1183 | /*** 2. Emacs' internal format (emacs-utf-8) ***/ | |
1184 | ||
1185 | ||
1186 | ||
1187 | \f | |
1188 | /*** 3. UTF-8 ***/ | |
1189 | ||
1190 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
ff0dacd7 KH |
1191 | Check if a text is encoded in UTF-8. If it is, return 1, else |
1192 | return 0. */ | |
df7492f9 KH |
1193 | |
1194 | #define UTF_8_1_OCTET_P(c) ((c) < 0x80) | |
1195 | #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80) | |
1196 | #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0) | |
1197 | #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0) | |
1198 | #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0) | |
1199 | #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8) | |
1200 | ||
a470d443 KH |
1201 | #define UTF_8_BOM_1 0xEF |
1202 | #define UTF_8_BOM_2 0xBB | |
1203 | #define UTF_8_BOM_3 0xBF | |
1204 | ||
df7492f9 | 1205 | static int |
cf84bb53 JB |
1206 | detect_coding_utf_8 (struct coding_system *coding, |
1207 | struct coding_detection_info *detect_info) | |
df7492f9 | 1208 | { |
065e3595 | 1209 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 1210 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
1211 | int multibytep = coding->src_multibyte; |
1212 | int consumed_chars = 0; | |
a470d443 | 1213 | int bom_found = 0; |
df7492f9 KH |
1214 | int found = 0; |
1215 | ||
ff0dacd7 | 1216 | detect_info->checked |= CATEGORY_MASK_UTF_8; |
df7492f9 KH |
1217 | /* A coding system of this category is always ASCII compatible. */ |
1218 | src += coding->head_ascii; | |
1219 | ||
1220 | while (1) | |
aa72b389 | 1221 | { |
df7492f9 | 1222 | int c, c1, c2, c3, c4; |
aa72b389 | 1223 | |
065e3595 | 1224 | src_base = src; |
df7492f9 | 1225 | ONE_MORE_BYTE (c); |
065e3595 | 1226 | if (c < 0 || UTF_8_1_OCTET_P (c)) |
df7492f9 KH |
1227 | continue; |
1228 | ONE_MORE_BYTE (c1); | |
065e3595 | 1229 | if (c1 < 0 || ! UTF_8_EXTRA_OCTET_P (c1)) |
df7492f9 KH |
1230 | break; |
1231 | if (UTF_8_2_OCTET_LEADING_P (c)) | |
aa72b389 | 1232 | { |
a470d443 | 1233 | found = 1; |
df7492f9 | 1234 | continue; |
aa72b389 | 1235 | } |
df7492f9 | 1236 | ONE_MORE_BYTE (c2); |
065e3595 | 1237 | if (c2 < 0 || ! UTF_8_EXTRA_OCTET_P (c2)) |
df7492f9 KH |
1238 | break; |
1239 | if (UTF_8_3_OCTET_LEADING_P (c)) | |
aa72b389 | 1240 | { |
a470d443 KH |
1241 | found = 1; |
1242 | if (src_base == coding->source | |
1243 | && c == UTF_8_BOM_1 && c1 == UTF_8_BOM_2 && c2 == UTF_8_BOM_3) | |
1244 | bom_found = 1; | |
df7492f9 | 1245 | continue; |
aa72b389 | 1246 | } |
df7492f9 | 1247 | ONE_MORE_BYTE (c3); |
065e3595 | 1248 | if (c3 < 0 || ! UTF_8_EXTRA_OCTET_P (c3)) |
df7492f9 KH |
1249 | break; |
1250 | if (UTF_8_4_OCTET_LEADING_P (c)) | |
aa72b389 | 1251 | { |
a470d443 | 1252 | found = 1; |
df7492f9 KH |
1253 | continue; |
1254 | } | |
1255 | ONE_MORE_BYTE (c4); | |
065e3595 | 1256 | if (c4 < 0 || ! UTF_8_EXTRA_OCTET_P (c4)) |
df7492f9 KH |
1257 | break; |
1258 | if (UTF_8_5_OCTET_LEADING_P (c)) | |
1259 | { | |
a470d443 | 1260 | found = 1; |
df7492f9 KH |
1261 | continue; |
1262 | } | |
1263 | break; | |
aa72b389 | 1264 | } |
ff0dacd7 | 1265 | detect_info->rejected |= CATEGORY_MASK_UTF_8; |
df7492f9 | 1266 | return 0; |
aa72b389 | 1267 | |
df7492f9 | 1268 | no_more_source: |
065e3595 | 1269 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) |
aa72b389 | 1270 | { |
ff0dacd7 | 1271 | detect_info->rejected |= CATEGORY_MASK_UTF_8; |
89528eb3 | 1272 | return 0; |
aa72b389 | 1273 | } |
a470d443 KH |
1274 | if (bom_found) |
1275 | { | |
1276 | /* The first character 0xFFFE doesn't necessarily mean a BOM. */ | |
1277 | detect_info->found |= CATEGORY_MASK_UTF_8_SIG | CATEGORY_MASK_UTF_8_NOSIG; | |
1278 | } | |
1279 | else | |
1280 | { | |
1281 | detect_info->rejected |= CATEGORY_MASK_UTF_8_SIG; | |
0e17387a KH |
1282 | if (found) |
1283 | detect_info->found |= CATEGORY_MASK_UTF_8_NOSIG; | |
a470d443 | 1284 | } |
ff0dacd7 | 1285 | return 1; |
aa72b389 KH |
1286 | } |
1287 | ||
4ed46869 | 1288 | |
b73bfc1c | 1289 | static void |
971de7fb | 1290 | decode_coding_utf_8 (struct coding_system *coding) |
b73bfc1c | 1291 | { |
8f924df7 KH |
1292 | const unsigned char *src = coding->source + coding->consumed; |
1293 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
1294 | const unsigned char *src_base; | |
69a80ea3 KH |
1295 | int *charbuf = coding->charbuf + coding->charbuf_used; |
1296 | int *charbuf_end = coding->charbuf + coding->charbuf_size; | |
453b38f0 | 1297 | int consumed_chars = 0, consumed_chars_base = 0; |
df7492f9 | 1298 | int multibytep = coding->src_multibyte; |
a470d443 | 1299 | enum utf_bom_type bom = CODING_UTF_8_BOM (coding); |
24a73b0a | 1300 | Lisp_Object attr, charset_list; |
2735d060 | 1301 | int eol_dos = |
0a9564cb | 1302 | !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); |
119852e7 | 1303 | int byte_after_cr = -1; |
4ed46869 | 1304 | |
24a73b0a | 1305 | CODING_GET_INFO (coding, attr, charset_list); |
df7492f9 | 1306 | |
a470d443 KH |
1307 | if (bom != utf_without_bom) |
1308 | { | |
1309 | int c1, c2, c3; | |
1310 | ||
1311 | src_base = src; | |
1312 | ONE_MORE_BYTE (c1); | |
1313 | if (! UTF_8_3_OCTET_LEADING_P (c1)) | |
1314 | src = src_base; | |
1315 | else | |
1316 | { | |
159bd5a2 | 1317 | ONE_MORE_BYTE (c2); |
a470d443 KH |
1318 | if (! UTF_8_EXTRA_OCTET_P (c2)) |
1319 | src = src_base; | |
1320 | else | |
1321 | { | |
159bd5a2 | 1322 | ONE_MORE_BYTE (c3); |
a470d443 KH |
1323 | if (! UTF_8_EXTRA_OCTET_P (c3)) |
1324 | src = src_base; | |
1325 | else | |
1326 | { | |
1327 | if ((c1 != UTF_8_BOM_1) | |
1328 | || (c2 != UTF_8_BOM_2) || (c3 != UTF_8_BOM_3)) | |
1329 | src = src_base; | |
1330 | else | |
1331 | CODING_UTF_8_BOM (coding) = utf_without_bom; | |
1332 | } | |
1333 | } | |
1334 | } | |
1335 | } | |
1336 | CODING_UTF_8_BOM (coding) = utf_without_bom; | |
1337 | ||
df7492f9 | 1338 | while (1) |
b73bfc1c | 1339 | { |
df7492f9 | 1340 | int c, c1, c2, c3, c4, c5; |
ec6d2bb8 | 1341 | |
df7492f9 KH |
1342 | src_base = src; |
1343 | consumed_chars_base = consumed_chars; | |
4af310db | 1344 | |
df7492f9 | 1345 | if (charbuf >= charbuf_end) |
b71f6f73 KH |
1346 | { |
1347 | if (byte_after_cr >= 0) | |
1348 | src_base--; | |
1349 | break; | |
1350 | } | |
df7492f9 | 1351 | |
119852e7 KH |
1352 | if (byte_after_cr >= 0) |
1353 | c1 = byte_after_cr, byte_after_cr = -1; | |
1354 | else | |
1355 | ONE_MORE_BYTE (c1); | |
065e3595 KH |
1356 | if (c1 < 0) |
1357 | { | |
1358 | c = - c1; | |
1359 | } | |
1a4990fb | 1360 | else if (UTF_8_1_OCTET_P (c1)) |
df7492f9 | 1361 | { |
2735d060 | 1362 | if (eol_dos && c1 == '\r') |
119852e7 | 1363 | ONE_MORE_BYTE (byte_after_cr); |
df7492f9 | 1364 | c = c1; |
4af310db | 1365 | } |
df7492f9 | 1366 | else |
4af310db | 1367 | { |
df7492f9 | 1368 | ONE_MORE_BYTE (c2); |
065e3595 | 1369 | if (c2 < 0 || ! UTF_8_EXTRA_OCTET_P (c2)) |
df7492f9 KH |
1370 | goto invalid_code; |
1371 | if (UTF_8_2_OCTET_LEADING_P (c1)) | |
4af310db | 1372 | { |
b0edb2c5 DL |
1373 | c = ((c1 & 0x1F) << 6) | (c2 & 0x3F); |
1374 | /* Reject overlong sequences here and below. Encoders | |
1375 | producing them are incorrect, they can be misleading, | |
1376 | and they mess up read/write invariance. */ | |
1377 | if (c < 128) | |
1378 | goto invalid_code; | |
4af310db | 1379 | } |
df7492f9 | 1380 | else |
aa72b389 | 1381 | { |
df7492f9 | 1382 | ONE_MORE_BYTE (c3); |
065e3595 | 1383 | if (c3 < 0 || ! UTF_8_EXTRA_OCTET_P (c3)) |
df7492f9 KH |
1384 | goto invalid_code; |
1385 | if (UTF_8_3_OCTET_LEADING_P (c1)) | |
b0edb2c5 DL |
1386 | { |
1387 | c = (((c1 & 0xF) << 12) | |
1388 | | ((c2 & 0x3F) << 6) | (c3 & 0x3F)); | |
72fe1301 DL |
1389 | if (c < 0x800 |
1390 | || (c >= 0xd800 && c < 0xe000)) /* surrogates (invalid) */ | |
b0edb2c5 DL |
1391 | goto invalid_code; |
1392 | } | |
df7492f9 KH |
1393 | else |
1394 | { | |
1395 | ONE_MORE_BYTE (c4); | |
065e3595 | 1396 | if (c4 < 0 || ! UTF_8_EXTRA_OCTET_P (c4)) |
df7492f9 KH |
1397 | goto invalid_code; |
1398 | if (UTF_8_4_OCTET_LEADING_P (c1)) | |
b0edb2c5 | 1399 | { |
df7492f9 KH |
1400 | c = (((c1 & 0x7) << 18) | ((c2 & 0x3F) << 12) |
1401 | | ((c3 & 0x3F) << 6) | (c4 & 0x3F)); | |
b0edb2c5 DL |
1402 | if (c < 0x10000) |
1403 | goto invalid_code; | |
1404 | } | |
df7492f9 KH |
1405 | else |
1406 | { | |
1407 | ONE_MORE_BYTE (c5); | |
065e3595 | 1408 | if (c5 < 0 || ! UTF_8_EXTRA_OCTET_P (c5)) |
df7492f9 KH |
1409 | goto invalid_code; |
1410 | if (UTF_8_5_OCTET_LEADING_P (c1)) | |
1411 | { | |
1412 | c = (((c1 & 0x3) << 24) | ((c2 & 0x3F) << 18) | |
1413 | | ((c3 & 0x3F) << 12) | ((c4 & 0x3F) << 6) | |
1414 | | (c5 & 0x3F)); | |
b0edb2c5 | 1415 | if ((c > MAX_CHAR) || (c < 0x200000)) |
df7492f9 KH |
1416 | goto invalid_code; |
1417 | } | |
1418 | else | |
1419 | goto invalid_code; | |
1420 | } | |
1421 | } | |
aa72b389 | 1422 | } |
b73bfc1c | 1423 | } |
df7492f9 KH |
1424 | |
1425 | *charbuf++ = c; | |
1426 | continue; | |
1427 | ||
1428 | invalid_code: | |
1429 | src = src_base; | |
1430 | consumed_chars = consumed_chars_base; | |
1431 | ONE_MORE_BYTE (c); | |
1432 | *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); | |
1433 | coding->errors++; | |
aa72b389 KH |
1434 | } |
1435 | ||
df7492f9 KH |
1436 | no_more_source: |
1437 | coding->consumed_char += consumed_chars_base; | |
1438 | coding->consumed = src_base - coding->source; | |
1439 | coding->charbuf_used = charbuf - coding->charbuf; | |
1440 | } | |
1441 | ||
1442 | ||
1443 | static int | |
971de7fb | 1444 | encode_coding_utf_8 (struct coding_system *coding) |
df7492f9 KH |
1445 | { |
1446 | int multibytep = coding->dst_multibyte; | |
1447 | int *charbuf = coding->charbuf; | |
1448 | int *charbuf_end = charbuf + coding->charbuf_used; | |
1449 | unsigned char *dst = coding->destination + coding->produced; | |
1450 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
e19c3639 | 1451 | int produced_chars = 0; |
df7492f9 KH |
1452 | int c; |
1453 | ||
a470d443 KH |
1454 | if (CODING_UTF_8_BOM (coding) == utf_with_bom) |
1455 | { | |
1456 | ASSURE_DESTINATION (3); | |
1457 | EMIT_THREE_BYTES (UTF_8_BOM_1, UTF_8_BOM_2, UTF_8_BOM_3); | |
1458 | CODING_UTF_8_BOM (coding) = utf_without_bom; | |
1459 | } | |
1460 | ||
df7492f9 | 1461 | if (multibytep) |
aa72b389 | 1462 | { |
df7492f9 KH |
1463 | int safe_room = MAX_MULTIBYTE_LENGTH * 2; |
1464 | ||
1465 | while (charbuf < charbuf_end) | |
b73bfc1c | 1466 | { |
df7492f9 | 1467 | unsigned char str[MAX_MULTIBYTE_LENGTH], *p, *pend = str; |
8f924df7 | 1468 | |
df7492f9 KH |
1469 | ASSURE_DESTINATION (safe_room); |
1470 | c = *charbuf++; | |
28f67a95 KH |
1471 | if (CHAR_BYTE8_P (c)) |
1472 | { | |
1473 | c = CHAR_TO_BYTE8 (c); | |
1474 | EMIT_ONE_BYTE (c); | |
1475 | } | |
1476 | else | |
1477 | { | |
db274c7a | 1478 | CHAR_STRING_ADVANCE_NO_UNIFY (c, pend); |
28f67a95 KH |
1479 | for (p = str; p < pend; p++) |
1480 | EMIT_ONE_BYTE (*p); | |
1481 | } | |
b73bfc1c | 1482 | } |
aa72b389 | 1483 | } |
df7492f9 KH |
1484 | else |
1485 | { | |
1486 | int safe_room = MAX_MULTIBYTE_LENGTH; | |
1487 | ||
1488 | while (charbuf < charbuf_end) | |
b73bfc1c | 1489 | { |
df7492f9 KH |
1490 | ASSURE_DESTINATION (safe_room); |
1491 | c = *charbuf++; | |
f03caae0 KH |
1492 | if (CHAR_BYTE8_P (c)) |
1493 | *dst++ = CHAR_TO_BYTE8 (c); | |
1494 | else | |
db274c7a | 1495 | CHAR_STRING_ADVANCE_NO_UNIFY (c, dst); |
df7492f9 | 1496 | produced_chars++; |
4ed46869 KH |
1497 | } |
1498 | } | |
065e3595 | 1499 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
1500 | coding->produced_char += produced_chars; |
1501 | coding->produced = dst - coding->destination; | |
1502 | return 0; | |
4ed46869 KH |
1503 | } |
1504 | ||
b73bfc1c | 1505 | |
df7492f9 | 1506 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". |
ff0dacd7 KH |
1507 | Check if a text is encoded in one of UTF-16 based coding systems. |
1508 | If it is, return 1, else return 0. */ | |
aa72b389 | 1509 | |
df7492f9 KH |
1510 | #define UTF_16_HIGH_SURROGATE_P(val) \ |
1511 | (((val) & 0xFC00) == 0xD800) | |
1512 | ||
1513 | #define UTF_16_LOW_SURROGATE_P(val) \ | |
1514 | (((val) & 0xFC00) == 0xDC00) | |
93dec019 | 1515 | |
aa72b389 | 1516 | |
df7492f9 | 1517 | static int |
cf84bb53 JB |
1518 | detect_coding_utf_16 (struct coding_system *coding, |
1519 | struct coding_detection_info *detect_info) | |
aa72b389 | 1520 | { |
ef1b0ba7 | 1521 | const unsigned char *src = coding->source; |
8f924df7 | 1522 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 | 1523 | int multibytep = coding->src_multibyte; |
df7492f9 | 1524 | int c1, c2; |
aa72b389 | 1525 | |
ff0dacd7 | 1526 | detect_info->checked |= CATEGORY_MASK_UTF_16; |
ff0dacd7 | 1527 | if (coding->mode & CODING_MODE_LAST_BLOCK |
24a73b0a | 1528 | && (coding->src_chars & 1)) |
ff0dacd7 KH |
1529 | { |
1530 | detect_info->rejected |= CATEGORY_MASK_UTF_16; | |
1531 | return 0; | |
1532 | } | |
24a73b0a | 1533 | |
f56a4450 | 1534 | TWO_MORE_BYTES (c1, c2); |
df7492f9 | 1535 | if ((c1 == 0xFF) && (c2 == 0xFE)) |
aa72b389 | 1536 | { |
b49a1807 KH |
1537 | detect_info->found |= (CATEGORY_MASK_UTF_16_LE |
1538 | | CATEGORY_MASK_UTF_16_AUTO); | |
24a73b0a KH |
1539 | detect_info->rejected |= (CATEGORY_MASK_UTF_16_BE |
1540 | | CATEGORY_MASK_UTF_16_BE_NOSIG | |
1541 | | CATEGORY_MASK_UTF_16_LE_NOSIG); | |
aa72b389 | 1542 | } |
df7492f9 | 1543 | else if ((c1 == 0xFE) && (c2 == 0xFF)) |
ff0dacd7 | 1544 | { |
b49a1807 KH |
1545 | detect_info->found |= (CATEGORY_MASK_UTF_16_BE |
1546 | | CATEGORY_MASK_UTF_16_AUTO); | |
24a73b0a KH |
1547 | detect_info->rejected |= (CATEGORY_MASK_UTF_16_LE |
1548 | | CATEGORY_MASK_UTF_16_BE_NOSIG | |
1549 | | CATEGORY_MASK_UTF_16_LE_NOSIG); | |
1550 | } | |
220eeac9 | 1551 | else if (c2 < 0) |
f56a4450 KH |
1552 | { |
1553 | detect_info->rejected |= CATEGORY_MASK_UTF_16; | |
1554 | return 0; | |
1555 | } | |
2f3cbb32 | 1556 | else |
24a73b0a | 1557 | { |
2f3cbb32 KH |
1558 | /* We check the dispersion of Eth and Oth bytes where E is even and |
1559 | O is odd. If both are high, we assume binary data.*/ | |
1560 | unsigned char e[256], o[256]; | |
1561 | unsigned e_num = 1, o_num = 1; | |
1562 | ||
1563 | memset (e, 0, 256); | |
1564 | memset (o, 0, 256); | |
1565 | e[c1] = 1; | |
1566 | o[c2] = 1; | |
1567 | ||
cc13543e KH |
1568 | detect_info->rejected |= (CATEGORY_MASK_UTF_16_AUTO |
1569 | |CATEGORY_MASK_UTF_16_BE | |
1570 | | CATEGORY_MASK_UTF_16_LE); | |
2f3cbb32 | 1571 | |
7f1faf1c KH |
1572 | while ((detect_info->rejected & CATEGORY_MASK_UTF_16) |
1573 | != CATEGORY_MASK_UTF_16) | |
2f3cbb32 | 1574 | { |
f56a4450 | 1575 | TWO_MORE_BYTES (c1, c2); |
220eeac9 | 1576 | if (c2 < 0) |
f56a4450 | 1577 | break; |
2f3cbb32 KH |
1578 | if (! e[c1]) |
1579 | { | |
1580 | e[c1] = 1; | |
1581 | e_num++; | |
cc13543e KH |
1582 | if (e_num >= 128) |
1583 | detect_info->rejected |= CATEGORY_MASK_UTF_16_BE_NOSIG; | |
2f3cbb32 KH |
1584 | } |
1585 | if (! o[c2]) | |
1586 | { | |
977b85f4 | 1587 | o[c2] = 1; |
2f3cbb32 | 1588 | o_num++; |
cc13543e KH |
1589 | if (o_num >= 128) |
1590 | detect_info->rejected |= CATEGORY_MASK_UTF_16_LE_NOSIG; | |
2f3cbb32 KH |
1591 | } |
1592 | } | |
2f3cbb32 | 1593 | return 0; |
ff0dacd7 | 1594 | } |
2f3cbb32 | 1595 | |
df7492f9 | 1596 | no_more_source: |
ff0dacd7 | 1597 | return 1; |
df7492f9 | 1598 | } |
aa72b389 | 1599 | |
df7492f9 | 1600 | static void |
971de7fb | 1601 | decode_coding_utf_16 (struct coding_system *coding) |
df7492f9 | 1602 | { |
8f924df7 KH |
1603 | const unsigned char *src = coding->source + coding->consumed; |
1604 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
1605 | const unsigned char *src_base; | |
69a80ea3 | 1606 | int *charbuf = coding->charbuf + coding->charbuf_used; |
df80c7f0 KH |
1607 | /* We may produces at most 3 chars in one loop. */ |
1608 | int *charbuf_end = coding->charbuf + coding->charbuf_size - 2; | |
3a8406e1 | 1609 | int consumed_chars = 0, consumed_chars_base = 0; |
df7492f9 | 1610 | int multibytep = coding->src_multibyte; |
a470d443 | 1611 | enum utf_bom_type bom = CODING_UTF_16_BOM (coding); |
df7492f9 KH |
1612 | enum utf_16_endian_type endian = CODING_UTF_16_ENDIAN (coding); |
1613 | int surrogate = CODING_UTF_16_SURROGATE (coding); | |
24a73b0a | 1614 | Lisp_Object attr, charset_list; |
2735d060 | 1615 | int eol_dos = |
0a9564cb | 1616 | !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); |
119852e7 | 1617 | int byte_after_cr1 = -1, byte_after_cr2 = -1; |
df7492f9 | 1618 | |
24a73b0a | 1619 | CODING_GET_INFO (coding, attr, charset_list); |
df7492f9 | 1620 | |
a470d443 | 1621 | if (bom == utf_with_bom) |
aa72b389 | 1622 | { |
df7492f9 | 1623 | int c, c1, c2; |
4af310db | 1624 | |
aa72b389 | 1625 | src_base = src; |
df7492f9 KH |
1626 | ONE_MORE_BYTE (c1); |
1627 | ONE_MORE_BYTE (c2); | |
e19c3639 | 1628 | c = (c1 << 8) | c2; |
aa72b389 | 1629 | |
b49a1807 KH |
1630 | if (endian == utf_16_big_endian |
1631 | ? c != 0xFEFF : c != 0xFFFE) | |
aa72b389 | 1632 | { |
b49a1807 KH |
1633 | /* The first two bytes are not BOM. Treat them as bytes |
1634 | for a normal character. */ | |
1635 | src = src_base; | |
1636 | coding->errors++; | |
aa72b389 | 1637 | } |
a470d443 | 1638 | CODING_UTF_16_BOM (coding) = utf_without_bom; |
b49a1807 | 1639 | } |
a470d443 | 1640 | else if (bom == utf_detect_bom) |
b49a1807 KH |
1641 | { |
1642 | /* We have already tried to detect BOM and failed in | |
1643 | detect_coding. */ | |
a470d443 | 1644 | CODING_UTF_16_BOM (coding) = utf_without_bom; |
df7492f9 | 1645 | } |
aa72b389 | 1646 | |
df7492f9 KH |
1647 | while (1) |
1648 | { | |
1649 | int c, c1, c2; | |
1650 | ||
1651 | src_base = src; | |
1652 | consumed_chars_base = consumed_chars; | |
1653 | ||
df80c7f0 | 1654 | if (charbuf >= charbuf_end) |
b71f6f73 KH |
1655 | { |
1656 | if (byte_after_cr1 >= 0) | |
1657 | src_base -= 2; | |
1658 | break; | |
1659 | } | |
df7492f9 | 1660 | |
119852e7 KH |
1661 | if (byte_after_cr1 >= 0) |
1662 | c1 = byte_after_cr1, byte_after_cr1 = -1; | |
1663 | else | |
1664 | ONE_MORE_BYTE (c1); | |
065e3595 KH |
1665 | if (c1 < 0) |
1666 | { | |
1667 | *charbuf++ = -c1; | |
1668 | continue; | |
1669 | } | |
119852e7 KH |
1670 | if (byte_after_cr2 >= 0) |
1671 | c2 = byte_after_cr2, byte_after_cr2 = -1; | |
1672 | else | |
1673 | ONE_MORE_BYTE (c2); | |
065e3595 KH |
1674 | if (c2 < 0) |
1675 | { | |
1676 | *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1); | |
1677 | *charbuf++ = -c2; | |
1678 | continue; | |
1679 | } | |
df7492f9 | 1680 | c = (endian == utf_16_big_endian |
e19c3639 | 1681 | ? ((c1 << 8) | c2) : ((c2 << 8) | c1)); |
119852e7 | 1682 | |
df7492f9 | 1683 | if (surrogate) |
fd3ae0b9 | 1684 | { |
df7492f9 | 1685 | if (! UTF_16_LOW_SURROGATE_P (c)) |
fd3ae0b9 | 1686 | { |
df7492f9 KH |
1687 | if (endian == utf_16_big_endian) |
1688 | c1 = surrogate >> 8, c2 = surrogate & 0xFF; | |
1689 | else | |
1690 | c1 = surrogate & 0xFF, c2 = surrogate >> 8; | |
1691 | *charbuf++ = c1; | |
1692 | *charbuf++ = c2; | |
1693 | coding->errors++; | |
1694 | if (UTF_16_HIGH_SURROGATE_P (c)) | |
1695 | CODING_UTF_16_SURROGATE (coding) = surrogate = c; | |
fd3ae0b9 | 1696 | else |
df7492f9 | 1697 | *charbuf++ = c; |
fd3ae0b9 KH |
1698 | } |
1699 | else | |
df7492f9 KH |
1700 | { |
1701 | c = ((surrogate - 0xD800) << 10) | (c - 0xDC00); | |
1702 | CODING_UTF_16_SURROGATE (coding) = surrogate = 0; | |
29f7ffd0 | 1703 | *charbuf++ = 0x10000 + c; |
df7492f9 | 1704 | } |
fd3ae0b9 | 1705 | } |
aa72b389 | 1706 | else |
df7492f9 KH |
1707 | { |
1708 | if (UTF_16_HIGH_SURROGATE_P (c)) | |
1709 | CODING_UTF_16_SURROGATE (coding) = surrogate = c; | |
1710 | else | |
119852e7 | 1711 | { |
2735d060 | 1712 | if (eol_dos && c == '\r') |
119852e7 KH |
1713 | { |
1714 | ONE_MORE_BYTE (byte_after_cr1); | |
1715 | ONE_MORE_BYTE (byte_after_cr2); | |
1716 | } | |
1717 | *charbuf++ = c; | |
1718 | } | |
8f924df7 | 1719 | } |
aa72b389 | 1720 | } |
df7492f9 KH |
1721 | |
1722 | no_more_source: | |
1723 | coding->consumed_char += consumed_chars_base; | |
1724 | coding->consumed = src_base - coding->source; | |
1725 | coding->charbuf_used = charbuf - coding->charbuf; | |
aa72b389 | 1726 | } |
b73bfc1c | 1727 | |
df7492f9 | 1728 | static int |
971de7fb | 1729 | encode_coding_utf_16 (struct coding_system *coding) |
df7492f9 KH |
1730 | { |
1731 | int multibytep = coding->dst_multibyte; | |
1732 | int *charbuf = coding->charbuf; | |
1733 | int *charbuf_end = charbuf + coding->charbuf_used; | |
1734 | unsigned char *dst = coding->destination + coding->produced; | |
1735 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
1736 | int safe_room = 8; | |
a470d443 | 1737 | enum utf_bom_type bom = CODING_UTF_16_BOM (coding); |
df7492f9 KH |
1738 | int big_endian = CODING_UTF_16_ENDIAN (coding) == utf_16_big_endian; |
1739 | int produced_chars = 0; | |
24a73b0a | 1740 | Lisp_Object attrs, charset_list; |
df7492f9 | 1741 | int c; |
4ed46869 | 1742 | |
24a73b0a | 1743 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 | 1744 | |
a470d443 | 1745 | if (bom != utf_without_bom) |
df7492f9 KH |
1746 | { |
1747 | ASSURE_DESTINATION (safe_room); | |
1748 | if (big_endian) | |
df7492f9 | 1749 | EMIT_TWO_BYTES (0xFE, 0xFF); |
880cf180 KH |
1750 | else |
1751 | EMIT_TWO_BYTES (0xFF, 0xFE); | |
a470d443 | 1752 | CODING_UTF_16_BOM (coding) = utf_without_bom; |
df7492f9 KH |
1753 | } |
1754 | ||
1755 | while (charbuf < charbuf_end) | |
1756 | { | |
1757 | ASSURE_DESTINATION (safe_room); | |
1758 | c = *charbuf++; | |
60afa08d | 1759 | if (c > MAX_UNICODE_CHAR) |
e19c3639 | 1760 | c = coding->default_char; |
df7492f9 KH |
1761 | |
1762 | if (c < 0x10000) | |
1763 | { | |
1764 | if (big_endian) | |
1765 | EMIT_TWO_BYTES (c >> 8, c & 0xFF); | |
1766 | else | |
1767 | EMIT_TWO_BYTES (c & 0xFF, c >> 8); | |
1768 | } | |
1769 | else | |
1770 | { | |
1771 | int c1, c2; | |
1772 | ||
1773 | c -= 0x10000; | |
1774 | c1 = (c >> 10) + 0xD800; | |
1775 | c2 = (c & 0x3FF) + 0xDC00; | |
1776 | if (big_endian) | |
1777 | EMIT_FOUR_BYTES (c1 >> 8, c1 & 0xFF, c2 >> 8, c2 & 0xFF); | |
1778 | else | |
1779 | EMIT_FOUR_BYTES (c1 & 0xFF, c1 >> 8, c2 & 0xFF, c2 >> 8); | |
1780 | } | |
1781 | } | |
065e3595 | 1782 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
1783 | coding->produced = dst - coding->destination; |
1784 | coding->produced_char += produced_chars; | |
1785 | return 0; | |
1786 | } | |
1787 | ||
1788 | \f | |
1789 | /*** 6. Old Emacs' internal format (emacs-mule) ***/ | |
1790 | ||
1791 | /* Emacs' internal format for representation of multiple character | |
1792 | sets is a kind of multi-byte encoding, i.e. characters are | |
1793 | represented by variable-length sequences of one-byte codes. | |
1794 | ||
1795 | ASCII characters and control characters (e.g. `tab', `newline') are | |
1796 | represented by one-byte sequences which are their ASCII codes, in | |
1797 | the range 0x00 through 0x7F. | |
1798 | ||
1799 | 8-bit characters of the range 0x80..0x9F are represented by | |
1800 | two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit | |
1801 | code + 0x20). | |
1802 | ||
1803 | 8-bit characters of the range 0xA0..0xFF are represented by | |
1804 | one-byte sequences which are their 8-bit code. | |
1805 | ||
1806 | The other characters are represented by a sequence of `base | |
1807 | leading-code', optional `extended leading-code', and one or two | |
1808 | `position-code's. The length of the sequence is determined by the | |
1809 | base leading-code. Leading-code takes the range 0x81 through 0x9D, | |
1810 | whereas extended leading-code and position-code take the range 0xA0 | |
1811 | through 0xFF. See `charset.h' for more details about leading-code | |
1812 | and position-code. | |
1813 | ||
1814 | --- CODE RANGE of Emacs' internal format --- | |
1815 | character set range | |
1816 | ------------- ----- | |
1817 | ascii 0x00..0x7F | |
1818 | eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF | |
1819 | eight-bit-graphic 0xA0..0xBF | |
1820 | ELSE 0x81..0x9D + [0xA0..0xFF]+ | |
1821 | --------------------------------------------- | |
1822 | ||
1823 | As this is the internal character representation, the format is | |
1824 | usually not used externally (i.e. in a file or in a data sent to a | |
1825 | process). But, it is possible to have a text externally in this | |
1826 | format (i.e. by encoding by the coding system `emacs-mule'). | |
1827 | ||
1828 | In that case, a sequence of one-byte codes has a slightly different | |
1829 | form. | |
1830 | ||
1831 | At first, all characters in eight-bit-control are represented by | |
1832 | one-byte sequences which are their 8-bit code. | |
1833 | ||
1834 | Next, character composition data are represented by the byte | |
1835 | sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ..., | |
1836 | where, | |
e951386e | 1837 | METHOD is 0xF2 plus one of composition method (enum |
df7492f9 KH |
1838 | composition_method), |
1839 | ||
1840 | BYTES is 0xA0 plus a byte length of this composition data, | |
1841 | ||
e951386e | 1842 | CHARS is 0xA0 plus a number of characters composed by this |
df7492f9 KH |
1843 | data, |
1844 | ||
ad1746f5 | 1845 | COMPONENTs are characters of multibyte form or composition |
df7492f9 KH |
1846 | rules encoded by two-byte of ASCII codes. |
1847 | ||
1848 | In addition, for backward compatibility, the following formats are | |
1849 | also recognized as composition data on decoding. | |
1850 | ||
1851 | 0x80 MSEQ ... | |
1852 | 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ | |
1853 | ||
1854 | Here, | |
1855 | MSEQ is a multibyte form but in these special format: | |
1856 | ASCII: 0xA0 ASCII_CODE+0x80, | |
1857 | other: LEADING_CODE+0x20 FOLLOWING-BYTE ..., | |
1858 | RULE is a one byte code of the range 0xA0..0xF0 that | |
1859 | represents a composition rule. | |
1860 | */ | |
1861 | ||
1862 | char emacs_mule_bytes[256]; | |
1863 | ||
e951386e KH |
1864 | |
1865 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
1866 | Check if a text is encoded in `emacs-mule'. If it is, return 1, | |
1867 | else return 0. */ | |
1868 | ||
1869 | static int | |
cf84bb53 JB |
1870 | detect_coding_emacs_mule (struct coding_system *coding, |
1871 | struct coding_detection_info *detect_info) | |
e951386e KH |
1872 | { |
1873 | const unsigned char *src = coding->source, *src_base; | |
1874 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
1875 | int multibytep = coding->src_multibyte; | |
1876 | int consumed_chars = 0; | |
1877 | int c; | |
1878 | int found = 0; | |
1879 | ||
1880 | detect_info->checked |= CATEGORY_MASK_EMACS_MULE; | |
1881 | /* A coding system of this category is always ASCII compatible. */ | |
1882 | src += coding->head_ascii; | |
1883 | ||
1884 | while (1) | |
1885 | { | |
1886 | src_base = src; | |
1887 | ONE_MORE_BYTE (c); | |
1888 | if (c < 0) | |
1889 | continue; | |
1890 | if (c == 0x80) | |
1891 | { | |
1892 | /* Perhaps the start of composite character. We simply skip | |
1893 | it because analyzing it is too heavy for detecting. But, | |
1894 | at least, we check that the composite character | |
1895 | constitutes of more than 4 bytes. */ | |
2735d060 | 1896 | const unsigned char *src_start; |
e951386e KH |
1897 | |
1898 | repeat: | |
2735d060 | 1899 | src_start = src; |
e951386e KH |
1900 | do |
1901 | { | |
1902 | ONE_MORE_BYTE (c); | |
1903 | } | |
1904 | while (c >= 0xA0); | |
1905 | ||
2735d060 | 1906 | if (src - src_start <= 4) |
e951386e KH |
1907 | break; |
1908 | found = CATEGORY_MASK_EMACS_MULE; | |
1909 | if (c == 0x80) | |
1910 | goto repeat; | |
1911 | } | |
1912 | ||
1913 | if (c < 0x80) | |
1914 | { | |
1915 | if (c < 0x20 | |
1916 | && (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)) | |
1917 | break; | |
1918 | } | |
1919 | else | |
1920 | { | |
396475b7 | 1921 | int more_bytes = emacs_mule_bytes[c] - 1; |
e951386e KH |
1922 | |
1923 | while (more_bytes > 0) | |
1924 | { | |
1925 | ONE_MORE_BYTE (c); | |
1926 | if (c < 0xA0) | |
1927 | { | |
1928 | src--; /* Unread the last byte. */ | |
1929 | break; | |
1930 | } | |
1931 | more_bytes--; | |
1932 | } | |
1933 | if (more_bytes != 0) | |
1934 | break; | |
1935 | found = CATEGORY_MASK_EMACS_MULE; | |
1936 | } | |
1937 | } | |
1938 | detect_info->rejected |= CATEGORY_MASK_EMACS_MULE; | |
1939 | return 0; | |
1940 | ||
1941 | no_more_source: | |
1942 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) | |
1943 | { | |
1944 | detect_info->rejected |= CATEGORY_MASK_EMACS_MULE; | |
1945 | return 0; | |
1946 | } | |
1947 | detect_info->found |= found; | |
1948 | return 1; | |
1949 | } | |
1950 | ||
1951 | ||
1952 | /* Parse emacs-mule multibyte sequence at SRC and return the decoded | |
1953 | character. If CMP_STATUS indicates that we must expect MSEQ or | |
1954 | RULE described above, decode it and return the negative value of | |
685ebdc8 | 1955 | the decoded character or rule. If an invalid byte is found, return |
e951386e KH |
1956 | -1. If SRC is too short, return -2. */ |
1957 | ||
e2f1bab9 | 1958 | static int |
cf84bb53 JB |
1959 | emacs_mule_char (struct coding_system *coding, const unsigned char *src, |
1960 | int *nbytes, int *nchars, int *id, | |
1961 | struct composition_status *cmp_status) | |
df7492f9 | 1962 | { |
8f924df7 KH |
1963 | const unsigned char *src_end = coding->source + coding->src_bytes; |
1964 | const unsigned char *src_base = src; | |
df7492f9 | 1965 | int multibytep = coding->src_multibyte; |
2735d060 | 1966 | int charset_ID; |
df7492f9 KH |
1967 | unsigned code; |
1968 | int c; | |
1969 | int consumed_chars = 0; | |
e951386e | 1970 | int mseq_found = 0; |
df7492f9 KH |
1971 | |
1972 | ONE_MORE_BYTE (c); | |
065e3595 | 1973 | if (c < 0) |
df7492f9 | 1974 | { |
065e3595 | 1975 | c = -c; |
2735d060 | 1976 | charset_ID = emacs_mule_charset[0]; |
065e3595 KH |
1977 | } |
1978 | else | |
1979 | { | |
4d41e8b7 KH |
1980 | if (c >= 0xA0) |
1981 | { | |
e951386e KH |
1982 | if (cmp_status->state != COMPOSING_NO |
1983 | && cmp_status->old_form) | |
4d41e8b7 | 1984 | { |
e951386e KH |
1985 | if (cmp_status->state == COMPOSING_CHAR) |
1986 | { | |
1987 | if (c == 0xA0) | |
1988 | { | |
1989 | ONE_MORE_BYTE (c); | |
1990 | c -= 0x80; | |
1991 | if (c < 0) | |
1992 | goto invalid_code; | |
1993 | } | |
1994 | else | |
1995 | c -= 0x20; | |
1996 | mseq_found = 1; | |
1997 | } | |
1998 | else | |
1999 | { | |
2000 | *nbytes = src - src_base; | |
2001 | *nchars = consumed_chars; | |
2002 | return -c; | |
2003 | } | |
4d41e8b7 KH |
2004 | } |
2005 | else | |
e951386e | 2006 | goto invalid_code; |
4d41e8b7 KH |
2007 | } |
2008 | ||
065e3595 | 2009 | switch (emacs_mule_bytes[c]) |
b73bfc1c | 2010 | { |
065e3595 | 2011 | case 2: |
2735d060 | 2012 | if ((charset_ID = emacs_mule_charset[c]) < 0) |
df7492f9 KH |
2013 | goto invalid_code; |
2014 | ONE_MORE_BYTE (c); | |
9ffd559c | 2015 | if (c < 0xA0) |
065e3595 | 2016 | goto invalid_code; |
df7492f9 | 2017 | code = c & 0x7F; |
065e3595 KH |
2018 | break; |
2019 | ||
2020 | case 3: | |
2021 | if (c == EMACS_MULE_LEADING_CODE_PRIVATE_11 | |
2022 | || c == EMACS_MULE_LEADING_CODE_PRIVATE_12) | |
2023 | { | |
2024 | ONE_MORE_BYTE (c); | |
2735d060 | 2025 | if (c < 0xA0 || (charset_ID = emacs_mule_charset[c]) < 0) |
065e3595 KH |
2026 | goto invalid_code; |
2027 | ONE_MORE_BYTE (c); | |
9ffd559c | 2028 | if (c < 0xA0) |
065e3595 KH |
2029 | goto invalid_code; |
2030 | code = c & 0x7F; | |
2031 | } | |
2032 | else | |
2033 | { | |
2735d060 | 2034 | if ((charset_ID = emacs_mule_charset[c]) < 0) |
065e3595 KH |
2035 | goto invalid_code; |
2036 | ONE_MORE_BYTE (c); | |
9ffd559c | 2037 | if (c < 0xA0) |
065e3595 KH |
2038 | goto invalid_code; |
2039 | code = (c & 0x7F) << 8; | |
2040 | ONE_MORE_BYTE (c); | |
9ffd559c | 2041 | if (c < 0xA0) |
065e3595 KH |
2042 | goto invalid_code; |
2043 | code |= c & 0x7F; | |
2044 | } | |
2045 | break; | |
2046 | ||
2047 | case 4: | |
2048 | ONE_MORE_BYTE (c); | |
2735d060 | 2049 | if (c < 0 || (charset_ID = emacs_mule_charset[c]) < 0) |
df7492f9 KH |
2050 | goto invalid_code; |
2051 | ONE_MORE_BYTE (c); | |
9ffd559c | 2052 | if (c < 0xA0) |
065e3595 | 2053 | goto invalid_code; |
781d7a48 | 2054 | code = (c & 0x7F) << 8; |
df7492f9 | 2055 | ONE_MORE_BYTE (c); |
9ffd559c | 2056 | if (c < 0xA0) |
065e3595 | 2057 | goto invalid_code; |
df7492f9 | 2058 | code |= c & 0x7F; |
065e3595 | 2059 | break; |
df7492f9 | 2060 | |
065e3595 KH |
2061 | case 1: |
2062 | code = c; | |
2735d060 | 2063 | charset_ID = ASCII_BYTE_P (code) ? charset_ascii : charset_eight_bit; |
065e3595 | 2064 | break; |
df7492f9 | 2065 | |
065e3595 KH |
2066 | default: |
2067 | abort (); | |
2068 | } | |
b84ae584 | 2069 | CODING_DECODE_CHAR (coding, src, src_base, src_end, |
2735d060 | 2070 | CHARSET_FROM_ID (charset_ID), code, c); |
065e3595 KH |
2071 | if (c < 0) |
2072 | goto invalid_code; | |
df7492f9 | 2073 | } |
df7492f9 KH |
2074 | *nbytes = src - src_base; |
2075 | *nchars = consumed_chars; | |
ff0dacd7 | 2076 | if (id) |
2735d060 | 2077 | *id = charset_ID; |
e951386e | 2078 | return (mseq_found ? -c : c); |
df7492f9 KH |
2079 | |
2080 | no_more_source: | |
2081 | return -2; | |
2082 | ||
2083 | invalid_code: | |
2084 | return -1; | |
2085 | } | |
2086 | ||
2087 | ||
e951386e | 2088 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ |
df7492f9 | 2089 | |
e951386e KH |
2090 | /* Handle these composition sequence ('|': the end of header elements, |
2091 | BYTES and CHARS >= 0xA0): | |
df7492f9 | 2092 | |
e951386e KH |
2093 | (1) relative composition: 0x80 0xF2 BYTES CHARS | CHAR ... |
2094 | (2) altchar composition: 0x80 0xF4 BYTES CHARS | ALT ... ALT CHAR ... | |
2095 | (3) alt&rule composition: 0x80 0xF5 BYTES CHARS | ALT RULE ... ALT CHAR ... | |
df7492f9 | 2096 | |
e951386e | 2097 | and these old form: |
1a4990fb | 2098 | |
e951386e KH |
2099 | (4) relative composition: 0x80 | MSEQ ... MSEQ |
2100 | (5) rulebase composition: 0x80 0xFF | MSEQ MRULE ... MSEQ | |
df7492f9 | 2101 | |
e951386e KH |
2102 | When the starter 0x80 and the following header elements are found, |
2103 | this annotation header is produced. | |
df7492f9 | 2104 | |
e951386e | 2105 | [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS NBYTES METHOD ] |
df7492f9 | 2106 | |
e951386e KH |
2107 | NCHARS is CHARS - 0xA0 for (1), (2), (3), and 0 for (4), (5). |
2108 | NBYTES is BYTES - 0xA0 for (1), (2), (3), and 0 for (4), (5). | |
df7492f9 | 2109 | |
e951386e KH |
2110 | Then, upon reading the following elements, these codes are produced |
2111 | until the composition end is found: | |
df7492f9 | 2112 | |
e951386e KH |
2113 | (1) CHAR ... CHAR |
2114 | (2) ALT ... ALT CHAR ... CHAR | |
2115 | (3) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT CHAR ... CHAR | |
2116 | (4) CHAR ... CHAR | |
2117 | (5) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR | |
4ed46869 | 2118 | |
e951386e KH |
2119 | When the composition end is found, LENGTH and NCHARS in the |
2120 | annotation header is updated as below: | |
b73bfc1c | 2121 | |
e951386e KH |
2122 | (1) LENGTH: unchanged, NCHARS: unchanged |
2123 | (2) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged | |
2124 | (3) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged | |
2125 | (4) LENGTH: unchanged, NCHARS: number of CHARs | |
2126 | (5) LENGTH: unchanged, NCHARS: number of CHARs | |
df7492f9 | 2127 | |
e951386e KH |
2128 | If an error is found while composing, the annotation header is |
2129 | changed to the original composition header (plus filler -1s) as | |
2130 | below: | |
2131 | ||
2132 | (1),(2),(3) [ 0x80 0xF2+METHOD BYTES CHARS -1 ] | |
2133 | (5) [ 0x80 0xFF -1 -1- -1 ] | |
2134 | ||
2135 | and the sequence [ -2 DECODED-RULE ] is changed to the original | |
2136 | byte sequence as below: | |
2137 | o the original byte sequence is B: [ B -1 ] | |
2138 | o the original byte sequence is B1 B2: [ B1 B2 ] | |
2139 | ||
2140 | Most of the routines are implemented by macros because many | |
2141 | variables and labels in the caller decode_coding_emacs_mule must be | |
2142 | accessible, and they are usually called just once (thus doesn't | |
2143 | increase the size of compiled object). */ | |
2144 | ||
2145 | /* Decode a composition rule represented by C as a component of | |
2146 | composition sequence of Emacs 20 style. Set RULE to the decoded | |
2147 | rule. */ | |
2148 | ||
2149 | #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(c, rule) \ | |
df7492f9 | 2150 | do { \ |
e951386e KH |
2151 | int gref, nref; \ |
2152 | \ | |
4d41e8b7 | 2153 | c -= 0xA0; \ |
df7492f9 KH |
2154 | if (c < 0 || c >= 81) \ |
2155 | goto invalid_code; \ | |
df7492f9 | 2156 | gref = c / 9, nref = c % 9; \ |
e951386e KH |
2157 | if (gref == 4) gref = 10; \ |
2158 | if (nref == 4) nref = 10; \ | |
2159 | rule = COMPOSITION_ENCODE_RULE (gref, nref); \ | |
df7492f9 KH |
2160 | } while (0) |
2161 | ||
2162 | ||
e951386e KH |
2163 | /* Decode a composition rule represented by C and the following byte |
2164 | at SRC as a component of composition sequence of Emacs 21 style. | |
2165 | Set RULE to the decoded rule. */ | |
781d7a48 | 2166 | |
e951386e | 2167 | #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(c, rule) \ |
781d7a48 KH |
2168 | do { \ |
2169 | int gref, nref; \ | |
e951386e KH |
2170 | \ |
2171 | gref = c - 0x20; \ | |
2172 | if (gref < 0 || gref >= 81) \ | |
781d7a48 | 2173 | goto invalid_code; \ |
e951386e KH |
2174 | ONE_MORE_BYTE (c); \ |
2175 | nref = c - 0x20; \ | |
2176 | if (nref < 0 || nref >= 81) \ | |
781d7a48 | 2177 | goto invalid_code; \ |
e951386e | 2178 | rule = COMPOSITION_ENCODE_RULE (gref, nref); \ |
781d7a48 KH |
2179 | } while (0) |
2180 | ||
2181 | ||
e951386e KH |
2182 | /* Start of Emacs 21 style format. The first three bytes at SRC are |
2183 | (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is the | |
2184 | byte length of this composition information, CHARS is the number of | |
2185 | characters composed by this composition. */ | |
2186 | ||
2187 | #define DECODE_EMACS_MULE_21_COMPOSITION() \ | |
aa72b389 | 2188 | do { \ |
781d7a48 | 2189 | enum composition_method method = c - 0xF2; \ |
df7492f9 | 2190 | int nbytes, nchars; \ |
e951386e | 2191 | \ |
df7492f9 | 2192 | ONE_MORE_BYTE (c); \ |
065e3595 KH |
2193 | if (c < 0) \ |
2194 | goto invalid_code; \ | |
df7492f9 | 2195 | nbytes = c - 0xA0; \ |
e951386e | 2196 | if (nbytes < 3 || (method == COMPOSITION_RELATIVE && nbytes != 4)) \ |
df7492f9 KH |
2197 | goto invalid_code; \ |
2198 | ONE_MORE_BYTE (c); \ | |
2199 | nchars = c - 0xA0; \ | |
e951386e KH |
2200 | if (nchars <= 0 || nchars >= MAX_COMPOSITION_COMPONENTS) \ |
2201 | goto invalid_code; \ | |
2202 | cmp_status->old_form = 0; \ | |
2203 | cmp_status->method = method; \ | |
2204 | if (method == COMPOSITION_RELATIVE) \ | |
2205 | cmp_status->state = COMPOSING_CHAR; \ | |
2206 | else \ | |
2207 | cmp_status->state = COMPOSING_COMPONENT_CHAR; \ | |
2208 | cmp_status->length = MAX_ANNOTATION_LENGTH; \ | |
2209 | cmp_status->nchars = nchars; \ | |
2210 | cmp_status->ncomps = nbytes - 4; \ | |
2211 | ADD_COMPOSITION_DATA (charbuf, nchars, nbytes, method); \ | |
aa72b389 | 2212 | } while (0) |
93dec019 | 2213 | |
aa72b389 | 2214 | |
e951386e KH |
2215 | /* Start of Emacs 20 style format for relative composition. */ |
2216 | ||
2217 | #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION() \ | |
2218 | do { \ | |
2219 | cmp_status->old_form = 1; \ | |
2220 | cmp_status->method = COMPOSITION_RELATIVE; \ | |
2221 | cmp_status->state = COMPOSING_CHAR; \ | |
2222 | cmp_status->length = MAX_ANNOTATION_LENGTH; \ | |
2223 | cmp_status->nchars = cmp_status->ncomps = 0; \ | |
2224 | ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \ | |
2225 | } while (0) | |
2226 | ||
2227 | ||
2228 | /* Start of Emacs 20 style format for rule-base composition. */ | |
2229 | ||
2230 | #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION() \ | |
2231 | do { \ | |
2232 | cmp_status->old_form = 1; \ | |
2233 | cmp_status->method = COMPOSITION_WITH_RULE; \ | |
2234 | cmp_status->state = COMPOSING_CHAR; \ | |
2235 | cmp_status->length = MAX_ANNOTATION_LENGTH; \ | |
2236 | cmp_status->nchars = cmp_status->ncomps = 0; \ | |
2237 | ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \ | |
df7492f9 KH |
2238 | } while (0) |
2239 | ||
2240 | ||
e951386e KH |
2241 | #define DECODE_EMACS_MULE_COMPOSITION_START() \ |
2242 | do { \ | |
2243 | const unsigned char *current_src = src; \ | |
2244 | \ | |
2245 | ONE_MORE_BYTE (c); \ | |
2246 | if (c < 0) \ | |
2247 | goto invalid_code; \ | |
2248 | if (c - 0xF2 >= COMPOSITION_RELATIVE \ | |
2249 | && c - 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS) \ | |
2250 | DECODE_EMACS_MULE_21_COMPOSITION (); \ | |
2251 | else if (c < 0xA0) \ | |
2252 | goto invalid_code; \ | |
2253 | else if (c < 0xC0) \ | |
2254 | { \ | |
2255 | DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (); \ | |
2256 | /* Re-read C as a composition component. */ \ | |
2257 | src = current_src; \ | |
2258 | } \ | |
2259 | else if (c == 0xFF) \ | |
2260 | DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (); \ | |
2261 | else \ | |
2262 | goto invalid_code; \ | |
2263 | } while (0) | |
2264 | ||
2265 | #define EMACS_MULE_COMPOSITION_END() \ | |
df7492f9 | 2266 | do { \ |
e951386e | 2267 | int idx = - cmp_status->length; \ |
4d41e8b7 | 2268 | \ |
e951386e KH |
2269 | if (cmp_status->old_form) \ |
2270 | charbuf[idx + 2] = cmp_status->nchars; \ | |
2271 | else if (cmp_status->method > COMPOSITION_RELATIVE) \ | |
2272 | charbuf[idx] = charbuf[idx + 2] - cmp_status->length; \ | |
2273 | cmp_status->state = COMPOSING_NO; \ | |
2274 | } while (0) | |
2275 | ||
2276 | ||
2277 | static int | |
cf84bb53 JB |
2278 | emacs_mule_finish_composition (int *charbuf, |
2279 | struct composition_status *cmp_status) | |
e951386e KH |
2280 | { |
2281 | int idx = - cmp_status->length; | |
2282 | int new_chars; | |
2283 | ||
2284 | if (cmp_status->old_form && cmp_status->nchars > 0) | |
2285 | { | |
2286 | charbuf[idx + 2] = cmp_status->nchars; | |
2287 | new_chars = 0; | |
2288 | if (cmp_status->method == COMPOSITION_WITH_RULE | |
2289 | && cmp_status->state == COMPOSING_CHAR) | |
2290 | { | |
2291 | /* The last rule was invalid. */ | |
2292 | int rule = charbuf[-1] + 0xA0; | |
2293 | ||
2294 | charbuf[-2] = BYTE8_TO_CHAR (rule); | |
2295 | charbuf[-1] = -1; | |
2296 | new_chars = 1; | |
2297 | } | |
2298 | } | |
2299 | else | |
2300 | { | |
2301 | charbuf[idx++] = BYTE8_TO_CHAR (0x80); | |
2302 | ||
2303 | if (cmp_status->method == COMPOSITION_WITH_RULE) | |
2304 | { | |
2305 | charbuf[idx++] = BYTE8_TO_CHAR (0xFF); | |
2306 | charbuf[idx++] = -3; | |
2307 | charbuf[idx++] = 0; | |
2308 | new_chars = 1; | |
2309 | } | |
2310 | else | |
2311 | { | |
2312 | int nchars = charbuf[idx + 1] + 0xA0; | |
2313 | int nbytes = charbuf[idx + 2] + 0xA0; | |
2314 | ||
2315 | charbuf[idx++] = BYTE8_TO_CHAR (0xF2 + cmp_status->method); | |
2316 | charbuf[idx++] = BYTE8_TO_CHAR (nbytes); | |
2317 | charbuf[idx++] = BYTE8_TO_CHAR (nchars); | |
2318 | charbuf[idx++] = -1; | |
2319 | new_chars = 4; | |
2320 | } | |
2321 | } | |
2322 | cmp_status->state = COMPOSING_NO; | |
2323 | return new_chars; | |
2324 | } | |
2325 | ||
2326 | #define EMACS_MULE_MAYBE_FINISH_COMPOSITION() \ | |
2327 | do { \ | |
2328 | if (cmp_status->state != COMPOSING_NO) \ | |
2329 | char_offset += emacs_mule_finish_composition (charbuf, cmp_status); \ | |
df7492f9 KH |
2330 | } while (0) |
2331 | ||
aa72b389 KH |
2332 | |
2333 | static void | |
971de7fb | 2334 | decode_coding_emacs_mule (struct coding_system *coding) |
aa72b389 | 2335 | { |
8f924df7 KH |
2336 | const unsigned char *src = coding->source + coding->consumed; |
2337 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
2338 | const unsigned char *src_base; | |
69a80ea3 | 2339 | int *charbuf = coding->charbuf + coding->charbuf_used; |
ad1746f5 KH |
2340 | /* We may produce two annotations (charset and composition) in one |
2341 | loop and one more charset annotation at the end. */ | |
69a80ea3 | 2342 | int *charbuf_end |
df80c7f0 | 2343 | = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 3); |
df7492f9 | 2344 | int consumed_chars = 0, consumed_chars_base; |
df7492f9 | 2345 | int multibytep = coding->src_multibyte; |
24a73b0a | 2346 | Lisp_Object attrs, charset_list; |
ff0dacd7 KH |
2347 | int char_offset = coding->produced_char; |
2348 | int last_offset = char_offset; | |
2349 | int last_id = charset_ascii; | |
2735d060 | 2350 | int eol_dos = |
0a9564cb | 2351 | !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); |
119852e7 | 2352 | int byte_after_cr = -1; |
e951386e | 2353 | struct composition_status *cmp_status = &coding->spec.emacs_mule.cmp_status; |
aa72b389 | 2354 | |
24a73b0a | 2355 | CODING_GET_INFO (coding, attrs, charset_list); |
aa72b389 | 2356 | |
e951386e KH |
2357 | if (cmp_status->state != COMPOSING_NO) |
2358 | { | |
2359 | int i; | |
2360 | ||
2361 | for (i = 0; i < cmp_status->length; i++) | |
2362 | *charbuf++ = cmp_status->carryover[i]; | |
2363 | coding->annotated = 1; | |
2364 | } | |
2365 | ||
aa72b389 KH |
2366 | while (1) |
2367 | { | |
ee05f961 | 2368 | int c, id IF_LINT (= 0); |
df7492f9 | 2369 | |
aa72b389 | 2370 | src_base = src; |
df7492f9 KH |
2371 | consumed_chars_base = consumed_chars; |
2372 | ||
2373 | if (charbuf >= charbuf_end) | |
b71f6f73 KH |
2374 | { |
2375 | if (byte_after_cr >= 0) | |
2376 | src_base--; | |
2377 | break; | |
2378 | } | |
aa72b389 | 2379 | |
119852e7 KH |
2380 | if (byte_after_cr >= 0) |
2381 | c = byte_after_cr, byte_after_cr = -1; | |
2382 | else | |
2383 | ONE_MORE_BYTE (c); | |
e951386e KH |
2384 | |
2385 | if (c < 0 || c == 0x80) | |
065e3595 | 2386 | { |
e951386e KH |
2387 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); |
2388 | if (c < 0) | |
2389 | { | |
2390 | *charbuf++ = -c; | |
2391 | char_offset++; | |
2392 | } | |
2393 | else | |
2394 | DECODE_EMACS_MULE_COMPOSITION_START (); | |
2395 | continue; | |
065e3595 | 2396 | } |
e951386e KH |
2397 | |
2398 | if (c < 0x80) | |
aa72b389 | 2399 | { |
2735d060 | 2400 | if (eol_dos && c == '\r') |
119852e7 | 2401 | ONE_MORE_BYTE (byte_after_cr); |
e951386e KH |
2402 | id = charset_ascii; |
2403 | if (cmp_status->state != COMPOSING_NO) | |
2404 | { | |
2405 | if (cmp_status->old_form) | |
2406 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); | |
2407 | else if (cmp_status->state >= COMPOSING_COMPONENT_CHAR) | |
2408 | cmp_status->ncomps--; | |
2409 | } | |
2410 | } | |
2411 | else | |
2412 | { | |
ee05f961 | 2413 | int nchars IF_LINT (= 0), nbytes IF_LINT (= 0); |
75f80e63 EZ |
2414 | /* emacs_mule_char can load a charset map from a file, which |
2415 | allocates a large structure and might cause buffer text | |
2416 | to be relocated as result. Thus, we need to remember the | |
ad1746f5 | 2417 | original pointer to buffer text, and fix up all related |
75f80e63 EZ |
2418 | pointers after the call. */ |
2419 | const unsigned char *orig = coding->source; | |
2420 | EMACS_INT offset; | |
e951386e KH |
2421 | |
2422 | c = emacs_mule_char (coding, src_base, &nbytes, &nchars, &id, | |
2423 | cmp_status); | |
75f80e63 EZ |
2424 | offset = coding->source - orig; |
2425 | if (offset) | |
2426 | { | |
2427 | src += offset; | |
2428 | src_base += offset; | |
2429 | src_end += offset; | |
2430 | } | |
e951386e KH |
2431 | if (c < 0) |
2432 | { | |
2433 | if (c == -1) | |
2434 | goto invalid_code; | |
2435 | if (c == -2) | |
2436 | break; | |
2437 | } | |
2438 | src = src_base + nbytes; | |
2439 | consumed_chars = consumed_chars_base + nchars; | |
2440 | if (cmp_status->state >= COMPOSING_COMPONENT_CHAR) | |
2441 | cmp_status->ncomps -= nchars; | |
2442 | } | |
2443 | ||
ad1746f5 | 2444 | /* Now if C >= 0, we found a normally encoded character, if C < |
e951386e KH |
2445 | 0, we found an old-style composition component character or |
2446 | rule. */ | |
2447 | ||
2448 | if (cmp_status->state == COMPOSING_NO) | |
2449 | { | |
2450 | if (last_id != id) | |
2451 | { | |
2452 | if (last_id != charset_ascii) | |
2453 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, | |
2454 | last_id); | |
2455 | last_id = id; | |
2456 | last_offset = char_offset; | |
2457 | } | |
df7492f9 KH |
2458 | *charbuf++ = c; |
2459 | char_offset++; | |
aa72b389 | 2460 | } |
e951386e | 2461 | else if (cmp_status->state == COMPOSING_CHAR) |
df7492f9 | 2462 | { |
e951386e KH |
2463 | if (cmp_status->old_form) |
2464 | { | |
2465 | if (c >= 0) | |
2466 | { | |
2467 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); | |
2468 | *charbuf++ = c; | |
2469 | char_offset++; | |
2470 | } | |
2471 | else | |
2472 | { | |
2473 | *charbuf++ = -c; | |
2474 | cmp_status->nchars++; | |
2475 | cmp_status->length++; | |
2476 | if (cmp_status->nchars == MAX_COMPOSITION_COMPONENTS) | |
2477 | EMACS_MULE_COMPOSITION_END (); | |
2478 | else if (cmp_status->method == COMPOSITION_WITH_RULE) | |
2479 | cmp_status->state = COMPOSING_RULE; | |
2480 | } | |
2481 | } | |
df7492f9 | 2482 | else |
e951386e KH |
2483 | { |
2484 | *charbuf++ = c; | |
2485 | cmp_status->length++; | |
2486 | cmp_status->nchars--; | |
2487 | if (cmp_status->nchars == 0) | |
2488 | EMACS_MULE_COMPOSITION_END (); | |
2489 | } | |
df7492f9 | 2490 | } |
e951386e | 2491 | else if (cmp_status->state == COMPOSING_RULE) |
df7492f9 | 2492 | { |
e951386e | 2493 | int rule; |
ff0dacd7 | 2494 | |
e951386e | 2495 | if (c >= 0) |
df7492f9 | 2496 | { |
e951386e KH |
2497 | EMACS_MULE_COMPOSITION_END (); |
2498 | *charbuf++ = c; | |
2499 | char_offset++; | |
df7492f9 | 2500 | } |
e951386e | 2501 | else |
ff0dacd7 | 2502 | { |
e951386e KH |
2503 | c = -c; |
2504 | DECODE_EMACS_MULE_COMPOSITION_RULE_20 (c, rule); | |
2505 | if (rule < 0) | |
2506 | goto invalid_code; | |
2507 | *charbuf++ = -2; | |
2508 | *charbuf++ = rule; | |
2509 | cmp_status->length += 2; | |
2510 | cmp_status->state = COMPOSING_CHAR; | |
ff0dacd7 | 2511 | } |
e951386e KH |
2512 | } |
2513 | else if (cmp_status->state == COMPOSING_COMPONENT_CHAR) | |
2514 | { | |
df7492f9 | 2515 | *charbuf++ = c; |
e951386e KH |
2516 | cmp_status->length++; |
2517 | if (cmp_status->ncomps == 0) | |
2518 | cmp_status->state = COMPOSING_CHAR; | |
2519 | else if (cmp_status->ncomps > 0) | |
2520 | { | |
2521 | if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS) | |
2522 | cmp_status->state = COMPOSING_COMPONENT_RULE; | |
2523 | } | |
2524 | else | |
2525 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); | |
df7492f9 | 2526 | } |
e951386e KH |
2527 | else /* COMPOSING_COMPONENT_RULE */ |
2528 | { | |
2529 | int rule; | |
2530 | ||
2531 | DECODE_EMACS_MULE_COMPOSITION_RULE_21 (c, rule); | |
2532 | if (rule < 0) | |
2533 | goto invalid_code; | |
2534 | *charbuf++ = -2; | |
2535 | *charbuf++ = rule; | |
2536 | cmp_status->length += 2; | |
2537 | cmp_status->ncomps--; | |
2538 | if (cmp_status->ncomps > 0) | |
2539 | cmp_status->state = COMPOSING_COMPONENT_CHAR; | |
2540 | else | |
2541 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); | |
2542 | } | |
2543 | continue; | |
2544 | ||
df7492f9 | 2545 | invalid_code: |
e951386e | 2546 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); |
df7492f9 KH |
2547 | src = src_base; |
2548 | consumed_chars = consumed_chars_base; | |
2549 | ONE_MORE_BYTE (c); | |
2550 | *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); | |
ff0dacd7 | 2551 | char_offset++; |
df7492f9 KH |
2552 | coding->errors++; |
2553 | } | |
2554 | ||
2555 | no_more_source: | |
e951386e KH |
2556 | if (cmp_status->state != COMPOSING_NO) |
2557 | { | |
2558 | if (coding->mode & CODING_MODE_LAST_BLOCK) | |
2559 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); | |
2560 | else | |
2561 | { | |
2562 | int i; | |
2563 | ||
2564 | charbuf -= cmp_status->length; | |
2565 | for (i = 0; i < cmp_status->length; i++) | |
2566 | cmp_status->carryover[i] = charbuf[i]; | |
2567 | } | |
2568 | } | |
ff0dacd7 | 2569 | if (last_id != charset_ascii) |
69a80ea3 | 2570 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
2571 | coding->consumed_char += consumed_chars_base; |
2572 | coding->consumed = src_base - coding->source; | |
2573 | coding->charbuf_used = charbuf - coding->charbuf; | |
2574 | } | |
2575 | ||
2576 | ||
2577 | #define EMACS_MULE_LEADING_CODES(id, codes) \ | |
2578 | do { \ | |
2579 | if (id < 0xA0) \ | |
2580 | codes[0] = id, codes[1] = 0; \ | |
2581 | else if (id < 0xE0) \ | |
2582 | codes[0] = 0x9A, codes[1] = id; \ | |
2583 | else if (id < 0xF0) \ | |
2584 | codes[0] = 0x9B, codes[1] = id; \ | |
2585 | else if (id < 0xF5) \ | |
2586 | codes[0] = 0x9C, codes[1] = id; \ | |
2587 | else \ | |
2588 | codes[0] = 0x9D, codes[1] = id; \ | |
2589 | } while (0); | |
2590 | ||
aa72b389 | 2591 | |
df7492f9 | 2592 | static int |
971de7fb | 2593 | encode_coding_emacs_mule (struct coding_system *coding) |
df7492f9 KH |
2594 | { |
2595 | int multibytep = coding->dst_multibyte; | |
2596 | int *charbuf = coding->charbuf; | |
2597 | int *charbuf_end = charbuf + coding->charbuf_used; | |
2598 | unsigned char *dst = coding->destination + coding->produced; | |
2599 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
2600 | int safe_room = 8; | |
df7492f9 | 2601 | int produced_chars = 0; |
24a73b0a | 2602 | Lisp_Object attrs, charset_list; |
df7492f9 | 2603 | int c; |
ff0dacd7 | 2604 | int preferred_charset_id = -1; |
df7492f9 | 2605 | |
24a73b0a | 2606 | CODING_GET_INFO (coding, attrs, charset_list); |
eccb6815 KH |
2607 | if (! EQ (charset_list, Vemacs_mule_charset_list)) |
2608 | { | |
2609 | CODING_ATTR_CHARSET_LIST (attrs) | |
2610 | = charset_list = Vemacs_mule_charset_list; | |
2611 | } | |
df7492f9 KH |
2612 | |
2613 | while (charbuf < charbuf_end) | |
2614 | { | |
2615 | ASSURE_DESTINATION (safe_room); | |
2616 | c = *charbuf++; | |
ff0dacd7 KH |
2617 | |
2618 | if (c < 0) | |
2619 | { | |
2620 | /* Handle an annotation. */ | |
2621 | switch (*charbuf) | |
2622 | { | |
2623 | case CODING_ANNOTATE_COMPOSITION_MASK: | |
2624 | /* Not yet implemented. */ | |
2625 | break; | |
2626 | case CODING_ANNOTATE_CHARSET_MASK: | |
2627 | preferred_charset_id = charbuf[3]; | |
2628 | if (preferred_charset_id >= 0 | |
2629 | && NILP (Fmemq (make_number (preferred_charset_id), | |
2630 | charset_list))) | |
2631 | preferred_charset_id = -1; | |
2632 | break; | |
2633 | default: | |
2634 | abort (); | |
2635 | } | |
2636 | charbuf += -c - 1; | |
2637 | continue; | |
2638 | } | |
2639 | ||
df7492f9 KH |
2640 | if (ASCII_CHAR_P (c)) |
2641 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d KH |
2642 | else if (CHAR_BYTE8_P (c)) |
2643 | { | |
2644 | c = CHAR_TO_BYTE8 (c); | |
2645 | EMIT_ONE_BYTE (c); | |
2646 | } | |
df7492f9 | 2647 | else |
aa72b389 | 2648 | { |
df7492f9 KH |
2649 | struct charset *charset; |
2650 | unsigned code; | |
2651 | int dimension; | |
2652 | int emacs_mule_id; | |
2653 | unsigned char leading_codes[2]; | |
2654 | ||
ff0dacd7 KH |
2655 | if (preferred_charset_id >= 0) |
2656 | { | |
2657 | charset = CHARSET_FROM_ID (preferred_charset_id); | |
905ca9d2 KH |
2658 | if (CHAR_CHARSET_P (c, charset)) |
2659 | code = ENCODE_CHAR (charset, c); | |
2660 | else | |
2661 | charset = char_charset (c, charset_list, &code); | |
ff0dacd7 KH |
2662 | } |
2663 | else | |
2664 | charset = char_charset (c, charset_list, &code); | |
df7492f9 KH |
2665 | if (! charset) |
2666 | { | |
2667 | c = coding->default_char; | |
2668 | if (ASCII_CHAR_P (c)) | |
2669 | { | |
2670 | EMIT_ONE_ASCII_BYTE (c); | |
2671 | continue; | |
2672 | } | |
2673 | charset = char_charset (c, charset_list, &code); | |
2674 | } | |
2675 | dimension = CHARSET_DIMENSION (charset); | |
2676 | emacs_mule_id = CHARSET_EMACS_MULE_ID (charset); | |
2677 | EMACS_MULE_LEADING_CODES (emacs_mule_id, leading_codes); | |
2678 | EMIT_ONE_BYTE (leading_codes[0]); | |
2679 | if (leading_codes[1]) | |
2680 | EMIT_ONE_BYTE (leading_codes[1]); | |
2681 | if (dimension == 1) | |
1fa663f9 | 2682 | EMIT_ONE_BYTE (code | 0x80); |
aa72b389 | 2683 | else |
df7492f9 | 2684 | { |
1fa663f9 | 2685 | code |= 0x8080; |
df7492f9 KH |
2686 | EMIT_ONE_BYTE (code >> 8); |
2687 | EMIT_ONE_BYTE (code & 0xFF); | |
2688 | } | |
aa72b389 | 2689 | } |
aa72b389 | 2690 | } |
065e3595 | 2691 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
2692 | coding->produced_char += produced_chars; |
2693 | coding->produced = dst - coding->destination; | |
2694 | return 0; | |
aa72b389 | 2695 | } |
b73bfc1c | 2696 | |
4ed46869 | 2697 | \f |
df7492f9 | 2698 | /*** 7. ISO2022 handlers ***/ |
4ed46869 KH |
2699 | |
2700 | /* The following note describes the coding system ISO2022 briefly. | |
39787efd | 2701 | Since the intention of this note is to help understand the |
5a936b46 | 2702 | functions in this file, some parts are NOT ACCURATE or are OVERLY |
39787efd | 2703 | SIMPLIFIED. For thorough understanding, please refer to the |
5a936b46 | 2704 | original document of ISO2022. This is equivalent to the standard |
cfb43547 | 2705 | ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*). |
4ed46869 KH |
2706 | |
2707 | ISO2022 provides many mechanisms to encode several character sets | |
cfb43547 | 2708 | in 7-bit and 8-bit environments. For 7-bit environments, all text |
39787efd KH |
2709 | is encoded using bytes less than 128. This may make the encoded |
2710 | text a little bit longer, but the text passes more easily through | |
cfb43547 | 2711 | several types of gateway, some of which strip off the MSB (Most |
8ca3766a | 2712 | Significant Bit). |
b73bfc1c | 2713 | |
cfb43547 DL |
2714 | There are two kinds of character sets: control character sets and |
2715 | graphic character sets. The former contain control characters such | |
4ed46869 | 2716 | as `newline' and `escape' to provide control functions (control |
39787efd | 2717 | functions are also provided by escape sequences). The latter |
cfb43547 | 2718 | contain graphic characters such as 'A' and '-'. Emacs recognizes |
4ed46869 KH |
2719 | two control character sets and many graphic character sets. |
2720 | ||
2721 | Graphic character sets are classified into one of the following | |
39787efd KH |
2722 | four classes, according to the number of bytes (DIMENSION) and |
2723 | number of characters in one dimension (CHARS) of the set: | |
2724 | - DIMENSION1_CHARS94 | |
2725 | - DIMENSION1_CHARS96 | |
2726 | - DIMENSION2_CHARS94 | |
2727 | - DIMENSION2_CHARS96 | |
2728 | ||
2729 | In addition, each character set is assigned an identification tag, | |
cfb43547 | 2730 | unique for each set, called the "final character" (denoted as <F> |
39787efd KH |
2731 | hereafter). The <F> of each character set is decided by ECMA(*) |
2732 | when it is registered in ISO. The code range of <F> is 0x30..0x7F | |
2733 | (0x30..0x3F are for private use only). | |
4ed46869 KH |
2734 | |
2735 | Note (*): ECMA = European Computer Manufacturers Association | |
2736 | ||
cfb43547 | 2737 | Here are examples of graphic character sets [NAME(<F>)]: |
4ed46869 KH |
2738 | o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ... |
2739 | o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ... | |
2740 | o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ... | |
2741 | o DIMENSION2_CHARS96 -- none for the moment | |
2742 | ||
39787efd | 2743 | A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR. |
4ed46869 KH |
2744 | C0 [0x00..0x1F] -- control character plane 0 |
2745 | GL [0x20..0x7F] -- graphic character plane 0 | |
2746 | C1 [0x80..0x9F] -- control character plane 1 | |
2747 | GR [0xA0..0xFF] -- graphic character plane 1 | |
2748 | ||
2749 | A control character set is directly designated and invoked to C0 or | |
39787efd KH |
2750 | C1 by an escape sequence. The most common case is that: |
2751 | - ISO646's control character set is designated/invoked to C0, and | |
2752 | - ISO6429's control character set is designated/invoked to C1, | |
2753 | and usually these designations/invocations are omitted in encoded | |
2754 | text. In a 7-bit environment, only C0 can be used, and a control | |
2755 | character for C1 is encoded by an appropriate escape sequence to | |
2756 | fit into the environment. All control characters for C1 are | |
2757 | defined to have corresponding escape sequences. | |
4ed46869 KH |
2758 | |
2759 | A graphic character set is at first designated to one of four | |
2760 | graphic registers (G0 through G3), then these graphic registers are | |
2761 | invoked to GL or GR. These designations and invocations can be | |
2762 | done independently. The most common case is that G0 is invoked to | |
39787efd KH |
2763 | GL, G1 is invoked to GR, and ASCII is designated to G0. Usually |
2764 | these invocations and designations are omitted in encoded text. | |
2765 | In a 7-bit environment, only GL can be used. | |
4ed46869 | 2766 | |
39787efd KH |
2767 | When a graphic character set of CHARS94 is invoked to GL, codes |
2768 | 0x20 and 0x7F of the GL area work as control characters SPACE and | |
2769 | DEL respectively, and codes 0xA0 and 0xFF of the GR area should not | |
2770 | be used. | |
4ed46869 KH |
2771 | |
2772 | There are two ways of invocation: locking-shift and single-shift. | |
2773 | With locking-shift, the invocation lasts until the next different | |
39787efd KH |
2774 | invocation, whereas with single-shift, the invocation affects the |
2775 | following character only and doesn't affect the locking-shift | |
2776 | state. Invocations are done by the following control characters or | |
2777 | escape sequences: | |
4ed46869 KH |
2778 | |
2779 | ---------------------------------------------------------------------- | |
39787efd | 2780 | abbrev function cntrl escape seq description |
4ed46869 | 2781 | ---------------------------------------------------------------------- |
39787efd KH |
2782 | SI/LS0 (shift-in) 0x0F none invoke G0 into GL |
2783 | SO/LS1 (shift-out) 0x0E none invoke G1 into GL | |
2784 | LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL | |
2785 | LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL | |
2786 | LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*) | |
2787 | LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*) | |
2788 | LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*) | |
2789 | SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char | |
2790 | SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char | |
4ed46869 | 2791 | ---------------------------------------------------------------------- |
39787efd KH |
2792 | (*) These are not used by any known coding system. |
2793 | ||
2794 | Control characters for these functions are defined by macros | |
2795 | ISO_CODE_XXX in `coding.h'. | |
4ed46869 | 2796 | |
39787efd | 2797 | Designations are done by the following escape sequences: |
4ed46869 KH |
2798 | ---------------------------------------------------------------------- |
2799 | escape sequence description | |
2800 | ---------------------------------------------------------------------- | |
2801 | ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0 | |
2802 | ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1 | |
2803 | ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2 | |
2804 | ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3 | |
2805 | ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*) | |
2806 | ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1 | |
2807 | ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2 | |
2808 | ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3 | |
2809 | ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**) | |
2810 | ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1 | |
2811 | ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2 | |
2812 | ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3 | |
2813 | ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*) | |
2814 | ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1 | |
2815 | ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2 | |
2816 | ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3 | |
2817 | ---------------------------------------------------------------------- | |
2818 | ||
2819 | In this list, "DIMENSION1_CHARS94<F>" means a graphic character set | |
39787efd | 2820 | of dimension 1, chars 94, and final character <F>, etc... |
4ed46869 KH |
2821 | |
2822 | Note (*): Although these designations are not allowed in ISO2022, | |
2823 | Emacs accepts them on decoding, and produces them on encoding | |
39787efd | 2824 | CHARS96 character sets in a coding system which is characterized as |
4ed46869 KH |
2825 | 7-bit environment, non-locking-shift, and non-single-shift. |
2826 | ||
2827 | Note (**): If <F> is '@', 'A', or 'B', the intermediate character | |
df7492f9 | 2828 | '(' must be omitted. We refer to this as "short-form" hereafter. |
4ed46869 | 2829 | |
cfb43547 | 2830 | Now you may notice that there are a lot of ways of encoding the |
39787efd KH |
2831 | same multilingual text in ISO2022. Actually, there exist many |
2832 | coding systems such as Compound Text (used in X11's inter client | |
8ca3766a DL |
2833 | communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR |
2834 | (used in Korean Internet), EUC (Extended UNIX Code, used in Asian | |
4ed46869 KH |
2835 | localized platforms), and all of these are variants of ISO2022. |
2836 | ||
2837 | In addition to the above, Emacs handles two more kinds of escape | |
2838 | sequences: ISO6429's direction specification and Emacs' private | |
2839 | sequence for specifying character composition. | |
2840 | ||
39787efd | 2841 | ISO6429's direction specification takes the following form: |
4ed46869 KH |
2842 | o CSI ']' -- end of the current direction |
2843 | o CSI '0' ']' -- end of the current direction | |
2844 | o CSI '1' ']' -- start of left-to-right text | |
2845 | o CSI '2' ']' -- start of right-to-left text | |
2846 | The control character CSI (0x9B: control sequence introducer) is | |
39787efd KH |
2847 | abbreviated to the escape sequence ESC '[' in a 7-bit environment. |
2848 | ||
2849 | Character composition specification takes the following form: | |
ec6d2bb8 KH |
2850 | o ESC '0' -- start relative composition |
2851 | o ESC '1' -- end composition | |
2852 | o ESC '2' -- start rule-base composition (*) | |
2853 | o ESC '3' -- start relative composition with alternate chars (**) | |
2854 | o ESC '4' -- start rule-base composition with alternate chars (**) | |
b73bfc1c | 2855 | Since these are not standard escape sequences of any ISO standard, |
cfb43547 | 2856 | the use of them with these meanings is restricted to Emacs only. |
ec6d2bb8 | 2857 | |
5a936b46 DL |
2858 | (*) This form is used only in Emacs 20.7 and older versions, |
2859 | but newer versions can safely decode it. | |
cfb43547 | 2860 | (**) This form is used only in Emacs 21.1 and newer versions, |
5a936b46 | 2861 | and older versions can't decode it. |
ec6d2bb8 | 2862 | |
cfb43547 | 2863 | Here's a list of example usages of these composition escape |
b73bfc1c | 2864 | sequences (categorized by `enum composition_method'). |
ec6d2bb8 | 2865 | |
b73bfc1c | 2866 | COMPOSITION_RELATIVE: |
ec6d2bb8 | 2867 | ESC 0 CHAR [ CHAR ] ESC 1 |
8ca3766a | 2868 | COMPOSITION_WITH_RULE: |
ec6d2bb8 | 2869 | ESC 2 CHAR [ RULE CHAR ] ESC 1 |
b73bfc1c | 2870 | COMPOSITION_WITH_ALTCHARS: |
ec6d2bb8 | 2871 | ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 |
b73bfc1c | 2872 | COMPOSITION_WITH_RULE_ALTCHARS: |
ec6d2bb8 | 2873 | ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */ |
4ed46869 KH |
2874 | |
2875 | enum iso_code_class_type iso_code_class[256]; | |
2876 | ||
df7492f9 KH |
2877 | #define SAFE_CHARSET_P(coding, id) \ |
2878 | ((id) <= (coding)->max_charset_id \ | |
1b3b981b | 2879 | && (coding)->safe_charsets[id] != 255) |
df7492f9 | 2880 | |
df7492f9 | 2881 | static void |
971de7fb | 2882 | setup_iso_safe_charsets (Lisp_Object attrs) |
df7492f9 KH |
2883 | { |
2884 | Lisp_Object charset_list, safe_charsets; | |
2885 | Lisp_Object request; | |
2886 | Lisp_Object reg_usage; | |
2887 | Lisp_Object tail; | |
2888 | int reg94, reg96; | |
2889 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); | |
2890 | int max_charset_id; | |
2891 | ||
2892 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
2893 | if ((flags & CODING_ISO_FLAG_FULL_SUPPORT) | |
2894 | && ! EQ (charset_list, Viso_2022_charset_list)) | |
2895 | { | |
2896 | CODING_ATTR_CHARSET_LIST (attrs) | |
2897 | = charset_list = Viso_2022_charset_list; | |
2898 | ASET (attrs, coding_attr_safe_charsets, Qnil); | |
2899 | } | |
2900 | ||
2901 | if (STRINGP (AREF (attrs, coding_attr_safe_charsets))) | |
2902 | return; | |
2903 | ||
2904 | max_charset_id = 0; | |
2905 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) | |
2906 | { | |
2907 | int id = XINT (XCAR (tail)); | |
2908 | if (max_charset_id < id) | |
2909 | max_charset_id = id; | |
2910 | } | |
d46c5b12 | 2911 | |
1b3b981b AS |
2912 | safe_charsets = make_uninit_string (max_charset_id + 1); |
2913 | memset (SDATA (safe_charsets), 255, max_charset_id + 1); | |
df7492f9 KH |
2914 | request = AREF (attrs, coding_attr_iso_request); |
2915 | reg_usage = AREF (attrs, coding_attr_iso_usage); | |
2916 | reg94 = XINT (XCAR (reg_usage)); | |
2917 | reg96 = XINT (XCDR (reg_usage)); | |
2918 | ||
2919 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) | |
2920 | { | |
2921 | Lisp_Object id; | |
2922 | Lisp_Object reg; | |
2923 | struct charset *charset; | |
2924 | ||
2925 | id = XCAR (tail); | |
2926 | charset = CHARSET_FROM_ID (XINT (id)); | |
bf16eb23 | 2927 | reg = Fcdr (Fassq (id, request)); |
df7492f9 | 2928 | if (! NILP (reg)) |
8f924df7 | 2929 | SSET (safe_charsets, XINT (id), XINT (reg)); |
df7492f9 KH |
2930 | else if (charset->iso_chars_96) |
2931 | { | |
2932 | if (reg96 < 4) | |
8f924df7 | 2933 | SSET (safe_charsets, XINT (id), reg96); |
df7492f9 KH |
2934 | } |
2935 | else | |
2936 | { | |
2937 | if (reg94 < 4) | |
8f924df7 | 2938 | SSET (safe_charsets, XINT (id), reg94); |
df7492f9 KH |
2939 | } |
2940 | } | |
2941 | ASET (attrs, coding_attr_safe_charsets, safe_charsets); | |
2942 | } | |
d46c5b12 | 2943 | |
b6871cc7 | 2944 | |
4ed46869 | 2945 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". |
ad1746f5 | 2946 | Check if a text is encoded in one of ISO-2022 based coding systems. |
ff0dacd7 | 2947 | If it is, return 1, else return 0. */ |
4ed46869 | 2948 | |
0a28aafb | 2949 | static int |
cf84bb53 JB |
2950 | detect_coding_iso_2022 (struct coding_system *coding, |
2951 | struct coding_detection_info *detect_info) | |
4ed46869 | 2952 | { |
8f924df7 KH |
2953 | const unsigned char *src = coding->source, *src_base = src; |
2954 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
df7492f9 | 2955 | int multibytep = coding->src_multibyte; |
ff0dacd7 | 2956 | int single_shifting = 0; |
0e48bb22 | 2957 | int id; |
df7492f9 KH |
2958 | int c, c1; |
2959 | int consumed_chars = 0; | |
2960 | int i; | |
ff0dacd7 KH |
2961 | int rejected = 0; |
2962 | int found = 0; | |
cee53ed4 | 2963 | int composition_count = -1; |
ff0dacd7 KH |
2964 | |
2965 | detect_info->checked |= CATEGORY_MASK_ISO; | |
df7492f9 KH |
2966 | |
2967 | for (i = coding_category_iso_7; i <= coding_category_iso_8_else; i++) | |
2968 | { | |
2969 | struct coding_system *this = &(coding_categories[i]); | |
2970 | Lisp_Object attrs, val; | |
2971 | ||
c6b278e7 KH |
2972 | if (this->id < 0) |
2973 | continue; | |
df7492f9 KH |
2974 | attrs = CODING_ID_ATTRS (this->id); |
2975 | if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT | |
1b3b981b | 2976 | && ! EQ (CODING_ATTR_CHARSET_LIST (attrs), Viso_2022_charset_list)) |
df7492f9 KH |
2977 | setup_iso_safe_charsets (attrs); |
2978 | val = CODING_ATTR_SAFE_CHARSETS (attrs); | |
8f924df7 | 2979 | this->max_charset_id = SCHARS (val) - 1; |
1b3b981b | 2980 | this->safe_charsets = SDATA (val); |
df7492f9 KH |
2981 | } |
2982 | ||
2983 | /* A coding system of this category is always ASCII compatible. */ | |
2984 | src += coding->head_ascii; | |
3f003981 | 2985 | |
ff0dacd7 | 2986 | while (rejected != CATEGORY_MASK_ISO) |
4ed46869 | 2987 | { |
065e3595 | 2988 | src_base = src; |
df7492f9 | 2989 | ONE_MORE_BYTE (c); |
4ed46869 KH |
2990 | switch (c) |
2991 | { | |
2992 | case ISO_CODE_ESC: | |
74383408 KH |
2993 | if (inhibit_iso_escape_detection) |
2994 | break; | |
f46869e4 | 2995 | single_shifting = 0; |
df7492f9 | 2996 | ONE_MORE_BYTE (c); |
0e48bb22 | 2997 | if (c == 'N' || c == 'O') |
d46c5b12 | 2998 | { |
ae9ff118 | 2999 | /* ESC <Fe> for SS2 or SS3. */ |
ff0dacd7 KH |
3000 | single_shifting = 1; |
3001 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_8BIT; | |
4ed46869 | 3002 | } |
cee53ed4 KH |
3003 | else if (c == '1') |
3004 | { | |
3005 | /* End of composition. */ | |
3006 | if (composition_count < 0 | |
3007 | || composition_count > MAX_COMPOSITION_COMPONENTS) | |
3008 | /* Invalid */ | |
3009 | break; | |
3010 | composition_count = -1; | |
3011 | found |= CATEGORY_MASK_ISO; | |
3012 | } | |
ec6d2bb8 KH |
3013 | else if (c >= '0' && c <= '4') |
3014 | { | |
3015 | /* ESC <Fp> for start/end composition. */ | |
cee53ed4 | 3016 | composition_count = 0; |
ec6d2bb8 | 3017 | } |
bf9cdd4e | 3018 | else |
df7492f9 | 3019 | { |
0e48bb22 AS |
3020 | if (c >= '(' && c <= '/') |
3021 | { | |
3022 | /* Designation sequence for a charset of dimension 1. */ | |
3023 | ONE_MORE_BYTE (c1); | |
3024 | if (c1 < ' ' || c1 >= 0x80 | |
3025 | || (id = iso_charset_table[0][c >= ','][c1]) < 0) | |
3026 | /* Invalid designation sequence. Just ignore. */ | |
3027 | break; | |
3028 | } | |
3029 | else if (c == '$') | |
3030 | { | |
3031 | /* Designation sequence for a charset of dimension 2. */ | |
3032 | ONE_MORE_BYTE (c); | |
3033 | if (c >= '@' && c <= 'B') | |
3034 | /* Designation for JISX0208.1978, GB2312, or JISX0208. */ | |
3035 | id = iso_charset_table[1][0][c]; | |
3036 | else if (c >= '(' && c <= '/') | |
3037 | { | |
3038 | ONE_MORE_BYTE (c1); | |
3039 | if (c1 < ' ' || c1 >= 0x80 | |
3040 | || (id = iso_charset_table[1][c >= ','][c1]) < 0) | |
3041 | /* Invalid designation sequence. Just ignore. */ | |
3042 | break; | |
3043 | } | |
3044 | else | |
3045 | /* Invalid designation sequence. Just ignore it. */ | |
3046 | break; | |
3047 | } | |
3048 | else | |
3049 | { | |
3050 | /* Invalid escape sequence. Just ignore it. */ | |
3051 | break; | |
3052 | } | |
d46c5b12 | 3053 | |
0e48bb22 AS |
3054 | /* We found a valid designation sequence for CHARSET. */ |
3055 | rejected |= CATEGORY_MASK_ISO_8BIT; | |
3056 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7], | |
3057 | id)) | |
3058 | found |= CATEGORY_MASK_ISO_7; | |
3059 | else | |
3060 | rejected |= CATEGORY_MASK_ISO_7; | |
3061 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7_tight], | |
3062 | id)) | |
3063 | found |= CATEGORY_MASK_ISO_7_TIGHT; | |
3064 | else | |
3065 | rejected |= CATEGORY_MASK_ISO_7_TIGHT; | |
3066 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7_else], | |
3067 | id)) | |
3068 | found |= CATEGORY_MASK_ISO_7_ELSE; | |
3069 | else | |
3070 | rejected |= CATEGORY_MASK_ISO_7_ELSE; | |
3071 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_8_else], | |
3072 | id)) | |
3073 | found |= CATEGORY_MASK_ISO_8_ELSE; | |
3074 | else | |
3075 | rejected |= CATEGORY_MASK_ISO_8_ELSE; | |
3076 | } | |
4ed46869 KH |
3077 | break; |
3078 | ||
4ed46869 | 3079 | case ISO_CODE_SO: |
d46c5b12 | 3080 | case ISO_CODE_SI: |
ff0dacd7 | 3081 | /* Locking shift out/in. */ |
74383408 KH |
3082 | if (inhibit_iso_escape_detection) |
3083 | break; | |
f46869e4 | 3084 | single_shifting = 0; |
ff0dacd7 | 3085 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_8BIT; |
d46c5b12 KH |
3086 | break; |
3087 | ||
4ed46869 | 3088 | case ISO_CODE_CSI: |
ff0dacd7 | 3089 | /* Control sequence introducer. */ |
f46869e4 | 3090 | single_shifting = 0; |
ff0dacd7 KH |
3091 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE; |
3092 | found |= CATEGORY_MASK_ISO_8_ELSE; | |
3093 | goto check_extra_latin; | |
3094 | ||
4ed46869 KH |
3095 | case ISO_CODE_SS2: |
3096 | case ISO_CODE_SS3: | |
ff0dacd7 KH |
3097 | /* Single shift. */ |
3098 | if (inhibit_iso_escape_detection) | |
3099 | break; | |
75e2a253 | 3100 | single_shifting = 0; |
ff0dacd7 KH |
3101 | rejected |= CATEGORY_MASK_ISO_7BIT; |
3102 | if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1]) | |
3103 | & CODING_ISO_FLAG_SINGLE_SHIFT) | |
0e48bb22 AS |
3104 | { |
3105 | found |= CATEGORY_MASK_ISO_8_1; | |
3106 | single_shifting = 1; | |
3107 | } | |
ff0dacd7 KH |
3108 | if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_2]) |
3109 | & CODING_ISO_FLAG_SINGLE_SHIFT) | |
0e48bb22 AS |
3110 | { |
3111 | found |= CATEGORY_MASK_ISO_8_2; | |
3112 | single_shifting = 1; | |
3113 | } | |
75e2a253 KH |
3114 | if (single_shifting) |
3115 | break; | |
0e48bb22 AS |
3116 | check_extra_latin: |
3117 | if (! VECTORP (Vlatin_extra_code_table) | |
3118 | || NILP (XVECTOR (Vlatin_extra_code_table)->contents[c])) | |
3119 | { | |
3120 | rejected = CATEGORY_MASK_ISO; | |
3121 | break; | |
3122 | } | |
3123 | if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1]) | |
3124 | & CODING_ISO_FLAG_LATIN_EXTRA) | |
3125 | found |= CATEGORY_MASK_ISO_8_1; | |
3126 | else | |
3127 | rejected |= CATEGORY_MASK_ISO_8_1; | |
3128 | rejected |= CATEGORY_MASK_ISO_8_2; | |
3129 | break; | |
4ed46869 KH |
3130 | |
3131 | default: | |
065e3595 KH |
3132 | if (c < 0) |
3133 | continue; | |
4ed46869 | 3134 | if (c < 0x80) |
f46869e4 | 3135 | { |
cee53ed4 KH |
3136 | if (composition_count >= 0) |
3137 | composition_count++; | |
f46869e4 KH |
3138 | single_shifting = 0; |
3139 | break; | |
3140 | } | |
ff0dacd7 | 3141 | if (c >= 0xA0) |
c4825358 | 3142 | { |
ff0dacd7 KH |
3143 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE; |
3144 | found |= CATEGORY_MASK_ISO_8_1; | |
f46869e4 | 3145 | /* Check the length of succeeding codes of the range |
ff0dacd7 KH |
3146 | 0xA0..0FF. If the byte length is even, we include |
3147 | CATEGORY_MASK_ISO_8_2 in `found'. We can check this | |
3148 | only when we are not single shifting. */ | |
3149 | if (! single_shifting | |
3150 | && ! (rejected & CATEGORY_MASK_ISO_8_2)) | |
f46869e4 | 3151 | { |
2735d060 | 3152 | int len = 1; |
b73bfc1c KH |
3153 | while (src < src_end) |
3154 | { | |
d12bd917 | 3155 | src_base = src; |
df7492f9 | 3156 | ONE_MORE_BYTE (c); |
b73bfc1c | 3157 | if (c < 0xA0) |
d12bd917 KH |
3158 | { |
3159 | src = src_base; | |
3160 | break; | |
3161 | } | |
2735d060 | 3162 | len++; |
b73bfc1c KH |
3163 | } |
3164 | ||
2735d060 | 3165 | if (len & 1 && src < src_end) |
cee53ed4 KH |
3166 | { |
3167 | rejected |= CATEGORY_MASK_ISO_8_2; | |
3168 | if (composition_count >= 0) | |
2735d060 | 3169 | composition_count += len; |
cee53ed4 | 3170 | } |
f46869e4 | 3171 | else |
cee53ed4 KH |
3172 | { |
3173 | found |= CATEGORY_MASK_ISO_8_2; | |
3174 | if (composition_count >= 0) | |
2735d060 | 3175 | composition_count += len / 2; |
cee53ed4 | 3176 | } |
f46869e4 | 3177 | } |
ff0dacd7 | 3178 | break; |
4ed46869 | 3179 | } |
4ed46869 KH |
3180 | } |
3181 | } | |
ff0dacd7 KH |
3182 | detect_info->rejected |= CATEGORY_MASK_ISO; |
3183 | return 0; | |
4ed46869 | 3184 | |
df7492f9 | 3185 | no_more_source: |
ff0dacd7 KH |
3186 | detect_info->rejected |= rejected; |
3187 | detect_info->found |= (found & ~rejected); | |
df7492f9 | 3188 | return 1; |
4ed46869 | 3189 | } |
ec6d2bb8 | 3190 | |
4ed46869 | 3191 | |
134b9549 KH |
3192 | /* Set designation state into CODING. Set CHARS_96 to -1 if the |
3193 | escape sequence should be kept. */ | |
df7492f9 KH |
3194 | #define DECODE_DESIGNATION(reg, dim, chars_96, final) \ |
3195 | do { \ | |
3196 | int id, prev; \ | |
3197 | \ | |
3198 | if (final < '0' || final >= 128 \ | |
3199 | || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \ | |
3200 | || !SAFE_CHARSET_P (coding, id)) \ | |
3201 | { \ | |
3202 | CODING_ISO_DESIGNATION (coding, reg) = -2; \ | |
134b9549 KH |
3203 | chars_96 = -1; \ |
3204 | break; \ | |
df7492f9 KH |
3205 | } \ |
3206 | prev = CODING_ISO_DESIGNATION (coding, reg); \ | |
bf16eb23 KH |
3207 | if (id == charset_jisx0201_roman) \ |
3208 | { \ | |
3209 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \ | |
3210 | id = charset_ascii; \ | |
3211 | } \ | |
3212 | else if (id == charset_jisx0208_1978) \ | |
3213 | { \ | |
3214 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \ | |
3215 | id = charset_jisx0208; \ | |
3216 | } \ | |
df7492f9 KH |
3217 | CODING_ISO_DESIGNATION (coding, reg) = id; \ |
3218 | /* If there was an invalid designation to REG previously, and this \ | |
3219 | designation is ASCII to REG, we should keep this designation \ | |
3220 | sequence. */ \ | |
3221 | if (prev == -2 && id == charset_ascii) \ | |
134b9549 | 3222 | chars_96 = -1; \ |
4ed46869 KH |
3223 | } while (0) |
3224 | ||
d46c5b12 | 3225 | |
e951386e KH |
3226 | /* Handle these composition sequence (ALT: alternate char): |
3227 | ||
3228 | (1) relative composition: ESC 0 CHAR ... ESC 1 | |
3229 | (2) rulebase composition: ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1 | |
3230 | (3) altchar composition: ESC 3 ALT ... ALT ESC 0 CHAR ... ESC 1 | |
3231 | (4) alt&rule composition: ESC 4 ALT RULE ... ALT ESC 0 CHAR ... ESC 1 | |
3232 | ||
3233 | When the start sequence (ESC 0/2/3/4) is found, this annotation | |
3234 | header is produced. | |
3235 | ||
3236 | [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) 0 METHOD ] | |
3237 | ||
3238 | Then, upon reading CHAR or RULE (one or two bytes), these codes are | |
3239 | produced until the end sequence (ESC 1) is found: | |
3240 | ||
3241 | (1) CHAR ... CHAR | |
3242 | (2) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR | |
3243 | (3) ALT ... ALT -1 -1 CHAR ... CHAR | |
3244 | (4) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT -1 -1 CHAR ... CHAR | |
3245 | ||
3246 | When the end sequence (ESC 1) is found, LENGTH and NCHARS in the | |
3247 | annotation header is updated as below: | |
3248 | ||
3249 | (1) LENGTH: unchanged, NCHARS: number of CHARs | |
3250 | (2) LENGTH: unchanged, NCHARS: number of CHARs | |
3251 | (3) LENGTH: += number of ALTs + 2, NCHARS: number of CHARs | |
3252 | (4) LENGTH: += number of ALTs * 3, NCHARS: number of CHARs | |
3253 | ||
3254 | If an error is found while composing, the annotation header is | |
3255 | changed to: | |
3256 | ||
3257 | [ ESC '0'/'2'/'3'/'4' -2 0 ] | |
3258 | ||
3259 | and the sequence [ -2 DECODED-RULE ] is changed to the original | |
3260 | byte sequence as below: | |
3261 | o the original byte sequence is B: [ B -1 ] | |
3262 | o the original byte sequence is B1 B2: [ B1 B2 ] | |
3263 | and the sequence [ -1 -1 ] is changed to the original byte | |
3264 | sequence: | |
3265 | [ ESC '0' ] | |
3266 | */ | |
3267 | ||
3268 | /* Decode a composition rule C1 and maybe one more byte from the | |
3269 | source, and set RULE to the encoded composition rule, NBYTES to the | |
3270 | length of the composition rule. If the rule is invalid, set RULE | |
3271 | to some negative value. */ | |
3272 | ||
3273 | #define DECODE_COMPOSITION_RULE(rule, nbytes) \ | |
3274 | do { \ | |
3275 | rule = c1 - 32; \ | |
3276 | if (rule < 0) \ | |
3277 | break; \ | |
3278 | if (rule < 81) /* old format (before ver.21) */ \ | |
3279 | { \ | |
3280 | int gref = (rule) / 9; \ | |
3281 | int nref = (rule) % 9; \ | |
3282 | if (gref == 4) gref = 10; \ | |
3283 | if (nref == 4) nref = 10; \ | |
3284 | rule = COMPOSITION_ENCODE_RULE (gref, nref); \ | |
3285 | nbytes = 1; \ | |
3286 | } \ | |
3287 | else /* new format (after ver.21) */ \ | |
3288 | { \ | |
2735d060 | 3289 | int b; \ |
e951386e | 3290 | \ |
2735d060 PE |
3291 | ONE_MORE_BYTE (b); \ |
3292 | rule = COMPOSITION_ENCODE_RULE (rule - 81, b - 32); \ | |
e951386e KH |
3293 | if (rule >= 0) \ |
3294 | rule += 0x100; /* to destinguish it from the old format */ \ | |
3295 | nbytes = 2; \ | |
3296 | } \ | |
3297 | } while (0) | |
3298 | ||
3299 | #define ENCODE_COMPOSITION_RULE(rule) \ | |
df7492f9 | 3300 | do { \ |
e951386e KH |
3301 | int gref = (rule % 0x100) / 12, nref = (rule % 0x100) % 12; \ |
3302 | \ | |
3303 | if (rule < 0x100) /* old format */ \ | |
df7492f9 | 3304 | { \ |
e951386e KH |
3305 | if (gref == 10) gref = 4; \ |
3306 | if (nref == 10) nref = 4; \ | |
3307 | charbuf[idx] = 32 + gref * 9 + nref; \ | |
3308 | charbuf[idx + 1] = -1; \ | |
3309 | new_chars++; \ | |
df7492f9 | 3310 | } \ |
e951386e | 3311 | else /* new format */ \ |
df7492f9 | 3312 | { \ |
e951386e KH |
3313 | charbuf[idx] = 32 + 81 + gref; \ |
3314 | charbuf[idx + 1] = 32 + nref; \ | |
3315 | new_chars += 2; \ | |
df7492f9 KH |
3316 | } \ |
3317 | } while (0) | |
3318 | ||
e951386e KH |
3319 | /* Finish the current composition as invalid. */ |
3320 | ||
f57e2426 | 3321 | static int finish_composition (int *, struct composition_status *); |
e951386e KH |
3322 | |
3323 | static int | |
971de7fb | 3324 | finish_composition (int *charbuf, struct composition_status *cmp_status) |
e951386e KH |
3325 | { |
3326 | int idx = - cmp_status->length; | |
3327 | int new_chars; | |
3328 | ||
3329 | /* Recover the original ESC sequence */ | |
3330 | charbuf[idx++] = ISO_CODE_ESC; | |
3331 | charbuf[idx++] = (cmp_status->method == COMPOSITION_RELATIVE ? '0' | |
3332 | : cmp_status->method == COMPOSITION_WITH_RULE ? '2' | |
3333 | : cmp_status->method == COMPOSITION_WITH_ALTCHARS ? '3' | |
3334 | /* cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS */ | |
3335 | : '4'); | |
3336 | charbuf[idx++] = -2; | |
3337 | charbuf[idx++] = 0; | |
3338 | charbuf[idx++] = -1; | |
3339 | new_chars = cmp_status->nchars; | |
3340 | if (cmp_status->method >= COMPOSITION_WITH_RULE) | |
3341 | for (; idx < 0; idx++) | |
3342 | { | |
3343 | int elt = charbuf[idx]; | |
3344 | ||
3345 | if (elt == -2) | |
3346 | { | |
3347 | ENCODE_COMPOSITION_RULE (charbuf[idx + 1]); | |
3348 | idx++; | |
3349 | } | |
3350 | else if (elt == -1) | |
3351 | { | |
3352 | charbuf[idx++] = ISO_CODE_ESC; | |
3353 | charbuf[idx] = '0'; | |
3354 | new_chars += 2; | |
3355 | } | |
3356 | } | |
3357 | cmp_status->state = COMPOSING_NO; | |
3358 | return new_chars; | |
3359 | } | |
3360 | ||
ad1746f5 | 3361 | /* If characters are under composition, finish the composition. */ |
e951386e KH |
3362 | #define MAYBE_FINISH_COMPOSITION() \ |
3363 | do { \ | |
3364 | if (cmp_status->state != COMPOSING_NO) \ | |
3365 | char_offset += finish_composition (charbuf, cmp_status); \ | |
3366 | } while (0) | |
d46c5b12 | 3367 | |
aa72b389 | 3368 | /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4. |
e951386e | 3369 | |
aa72b389 KH |
3370 | ESC 0 : relative composition : ESC 0 CHAR ... ESC 1 |
3371 | ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1 | |
df7492f9 KH |
3372 | ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1 |
3373 | ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1 | |
ec6d2bb8 | 3374 | |
e951386e KH |
3375 | Produce this annotation sequence now: |
3376 | ||
3377 | [ -LENGTH(==-4) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) METHOD ] | |
3378 | */ | |
3379 | ||
3380 | #define DECODE_COMPOSITION_START(c1) \ | |
3381 | do { \ | |
3382 | if (c1 == '0' \ | |
3383 | && ((cmp_status->state == COMPOSING_COMPONENT_CHAR \ | |
3384 | && cmp_status->method == COMPOSITION_WITH_ALTCHARS) \ | |
3385 | || (cmp_status->state == COMPOSING_COMPONENT_RULE \ | |
3386 | && cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS))) \ | |
3387 | { \ | |
3388 | *charbuf++ = -1; \ | |
3389 | *charbuf++= -1; \ | |
3390 | cmp_status->state = COMPOSING_CHAR; \ | |
3391 | cmp_status->length += 2; \ | |
3392 | } \ | |
3393 | else \ | |
3394 | { \ | |
3395 | MAYBE_FINISH_COMPOSITION (); \ | |
3396 | cmp_status->method = (c1 == '0' ? COMPOSITION_RELATIVE \ | |
3397 | : c1 == '2' ? COMPOSITION_WITH_RULE \ | |
3398 | : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \ | |
3399 | : COMPOSITION_WITH_RULE_ALTCHARS); \ | |
3400 | cmp_status->state \ | |
3401 | = (c1 <= '2' ? COMPOSING_CHAR : COMPOSING_COMPONENT_CHAR); \ | |
3402 | ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \ | |
3403 | cmp_status->length = MAX_ANNOTATION_LENGTH; \ | |
3404 | cmp_status->nchars = cmp_status->ncomps = 0; \ | |
3405 | coding->annotated = 1; \ | |
3406 | } \ | |
ec6d2bb8 KH |
3407 | } while (0) |
3408 | ||
ec6d2bb8 | 3409 | |
e951386e | 3410 | /* Handle composition end sequence ESC 1. */ |
df7492f9 KH |
3411 | |
3412 | #define DECODE_COMPOSITION_END() \ | |
ec6d2bb8 | 3413 | do { \ |
e951386e KH |
3414 | if (cmp_status->nchars == 0 \ |
3415 | || ((cmp_status->state == COMPOSING_CHAR) \ | |
3416 | == (cmp_status->method == COMPOSITION_WITH_RULE))) \ | |
ec6d2bb8 | 3417 | { \ |
e951386e KH |
3418 | MAYBE_FINISH_COMPOSITION (); \ |
3419 | goto invalid_code; \ | |
ec6d2bb8 | 3420 | } \ |
e951386e KH |
3421 | if (cmp_status->method == COMPOSITION_WITH_ALTCHARS) \ |
3422 | charbuf[- cmp_status->length] -= cmp_status->ncomps + 2; \ | |
3423 | else if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS) \ | |
3424 | charbuf[- cmp_status->length] -= cmp_status->ncomps * 3; \ | |
3425 | charbuf[- cmp_status->length + 2] = cmp_status->nchars; \ | |
3426 | char_offset += cmp_status->nchars; \ | |
3427 | cmp_status->state = COMPOSING_NO; \ | |
ec6d2bb8 KH |
3428 | } while (0) |
3429 | ||
e951386e | 3430 | /* Store a composition rule RULE in charbuf, and update cmp_status. */ |
df7492f9 | 3431 | |
e951386e KH |
3432 | #define STORE_COMPOSITION_RULE(rule) \ |
3433 | do { \ | |
3434 | *charbuf++ = -2; \ | |
3435 | *charbuf++ = rule; \ | |
3436 | cmp_status->length += 2; \ | |
3437 | cmp_status->state--; \ | |
3438 | } while (0) | |
ec6d2bb8 | 3439 | |
e951386e KH |
3440 | /* Store a composed char or a component char C in charbuf, and update |
3441 | cmp_status. */ | |
3442 | ||
3443 | #define STORE_COMPOSITION_CHAR(c) \ | |
ec6d2bb8 | 3444 | do { \ |
e951386e KH |
3445 | *charbuf++ = (c); \ |
3446 | cmp_status->length++; \ | |
3447 | if (cmp_status->state == COMPOSING_CHAR) \ | |
3448 | cmp_status->nchars++; \ | |
df7492f9 | 3449 | else \ |
e951386e KH |
3450 | cmp_status->ncomps++; \ |
3451 | if (cmp_status->method == COMPOSITION_WITH_RULE \ | |
3452 | || (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS \ | |
3453 | && cmp_status->state == COMPOSING_COMPONENT_CHAR)) \ | |
3454 | cmp_status->state++; \ | |
ec6d2bb8 | 3455 | } while (0) |
88993dfd | 3456 | |
d46c5b12 | 3457 | |
4ed46869 KH |
3458 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ |
3459 | ||
b73bfc1c | 3460 | static void |
971de7fb | 3461 | decode_coding_iso_2022 (struct coding_system *coding) |
4ed46869 | 3462 | { |
8f924df7 KH |
3463 | const unsigned char *src = coding->source + coding->consumed; |
3464 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
3465 | const unsigned char *src_base; | |
69a80ea3 | 3466 | int *charbuf = coding->charbuf + coding->charbuf_used; |
ad1746f5 KH |
3467 | /* We may produce two annotations (charset and composition) in one |
3468 | loop and one more charset annotation at the end. */ | |
ff0dacd7 | 3469 | int *charbuf_end |
df80c7f0 | 3470 | = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 3); |
df7492f9 | 3471 | int consumed_chars = 0, consumed_chars_base; |
df7492f9 | 3472 | int multibytep = coding->src_multibyte; |
4ed46869 | 3473 | /* Charsets invoked to graphic plane 0 and 1 respectively. */ |
df7492f9 KH |
3474 | int charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); |
3475 | int charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1); | |
134b9549 | 3476 | int charset_id_2, charset_id_3; |
df7492f9 KH |
3477 | struct charset *charset; |
3478 | int c; | |
e951386e | 3479 | struct composition_status *cmp_status = CODING_ISO_CMP_STATUS (coding); |
24a73b0a | 3480 | Lisp_Object attrs, charset_list; |
ff0dacd7 KH |
3481 | int char_offset = coding->produced_char; |
3482 | int last_offset = char_offset; | |
3483 | int last_id = charset_ascii; | |
2735d060 | 3484 | int eol_dos = |
0a9564cb | 3485 | !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); |
119852e7 | 3486 | int byte_after_cr = -1; |
e951386e | 3487 | int i; |
df7492f9 | 3488 | |
24a73b0a | 3489 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 | 3490 | setup_iso_safe_charsets (attrs); |
287c57d7 KH |
3491 | /* Charset list may have been changed. */ |
3492 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
1b3b981b | 3493 | coding->safe_charsets = SDATA (CODING_ATTR_SAFE_CHARSETS (attrs)); |
b73bfc1c | 3494 | |
e951386e KH |
3495 | if (cmp_status->state != COMPOSING_NO) |
3496 | { | |
3497 | for (i = 0; i < cmp_status->length; i++) | |
3498 | *charbuf++ = cmp_status->carryover[i]; | |
3499 | coding->annotated = 1; | |
3500 | } | |
3501 | ||
b73bfc1c | 3502 | while (1) |
4ed46869 | 3503 | { |
cf299835 | 3504 | int c1, c2, c3; |
b73bfc1c KH |
3505 | |
3506 | src_base = src; | |
df7492f9 KH |
3507 | consumed_chars_base = consumed_chars; |
3508 | ||
3509 | if (charbuf >= charbuf_end) | |
b71f6f73 KH |
3510 | { |
3511 | if (byte_after_cr >= 0) | |
3512 | src_base--; | |
3513 | break; | |
3514 | } | |
df7492f9 | 3515 | |
119852e7 KH |
3516 | if (byte_after_cr >= 0) |
3517 | c1 = byte_after_cr, byte_after_cr = -1; | |
3518 | else | |
3519 | ONE_MORE_BYTE (c1); | |
065e3595 KH |
3520 | if (c1 < 0) |
3521 | goto invalid_code; | |
4ed46869 | 3522 | |
e951386e | 3523 | if (CODING_ISO_EXTSEGMENT_LEN (coding) > 0) |
4ed46869 | 3524 | { |
e951386e KH |
3525 | *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1); |
3526 | char_offset++; | |
3527 | CODING_ISO_EXTSEGMENT_LEN (coding)--; | |
3528 | continue; | |
3529 | } | |
3530 | ||
3531 | if (CODING_ISO_EMBEDDED_UTF_8 (coding)) | |
3532 | { | |
3533 | if (c1 == ISO_CODE_ESC) | |
ec6d2bb8 | 3534 | { |
e951386e KH |
3535 | if (src + 1 >= src_end) |
3536 | goto no_more_source; | |
3537 | *charbuf++ = ISO_CODE_ESC; | |
3538 | char_offset++; | |
3539 | if (src[0] == '%' && src[1] == '@') | |
df7492f9 | 3540 | { |
e951386e KH |
3541 | src += 2; |
3542 | consumed_chars += 2; | |
3543 | char_offset += 2; | |
3544 | /* We are sure charbuf can contain two more chars. */ | |
3545 | *charbuf++ = '%'; | |
3546 | *charbuf++ = '@'; | |
3547 | CODING_ISO_EMBEDDED_UTF_8 (coding) = 0; | |
df7492f9 | 3548 | } |
4ed46869 | 3549 | } |
e951386e KH |
3550 | else |
3551 | { | |
3552 | *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1); | |
3553 | char_offset++; | |
3554 | } | |
3555 | continue; | |
3556 | } | |
3557 | ||
3558 | if ((cmp_status->state == COMPOSING_RULE | |
3559 | || cmp_status->state == COMPOSING_COMPONENT_RULE) | |
3560 | && c1 != ISO_CODE_ESC) | |
3561 | { | |
3562 | int rule, nbytes; | |
3563 | ||
3564 | DECODE_COMPOSITION_RULE (rule, nbytes); | |
3565 | if (rule < 0) | |
3566 | goto invalid_code; | |
3567 | STORE_COMPOSITION_RULE (rule); | |
3568 | continue; | |
3569 | } | |
3570 | ||
3571 | /* We produce at most one character. */ | |
3572 | switch (iso_code_class [c1]) | |
3573 | { | |
3574 | case ISO_0x20_or_0x7F: | |
df7492f9 KH |
3575 | if (charset_id_0 < 0 |
3576 | || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0))) | |
781d7a48 KH |
3577 | /* This is SPACE or DEL. */ |
3578 | charset = CHARSET_FROM_ID (charset_ascii); | |
3579 | else | |
3580 | charset = CHARSET_FROM_ID (charset_id_0); | |
3581 | break; | |
4ed46869 KH |
3582 | |
3583 | case ISO_graphic_plane_0: | |
134b9549 KH |
3584 | if (charset_id_0 < 0) |
3585 | charset = CHARSET_FROM_ID (charset_ascii); | |
3586 | else | |
3587 | charset = CHARSET_FROM_ID (charset_id_0); | |
4ed46869 KH |
3588 | break; |
3589 | ||
3590 | case ISO_0xA0_or_0xFF: | |
df7492f9 KH |
3591 | if (charset_id_1 < 0 |
3592 | || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1)) | |
3593 | || CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) | |
3594 | goto invalid_code; | |
4ed46869 KH |
3595 | /* This is a graphic character, we fall down ... */ |
3596 | ||
3597 | case ISO_graphic_plane_1: | |
df7492f9 KH |
3598 | if (charset_id_1 < 0) |
3599 | goto invalid_code; | |
3600 | charset = CHARSET_FROM_ID (charset_id_1); | |
4ed46869 KH |
3601 | break; |
3602 | ||
df7492f9 | 3603 | case ISO_control_0: |
2735d060 | 3604 | if (eol_dos && c1 == '\r') |
119852e7 | 3605 | ONE_MORE_BYTE (byte_after_cr); |
df7492f9 KH |
3606 | MAYBE_FINISH_COMPOSITION (); |
3607 | charset = CHARSET_FROM_ID (charset_ascii); | |
4ed46869 KH |
3608 | break; |
3609 | ||
df7492f9 | 3610 | case ISO_control_1: |
df7492f9 KH |
3611 | goto invalid_code; |
3612 | ||
4ed46869 | 3613 | case ISO_shift_out: |
df7492f9 KH |
3614 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT) |
3615 | || CODING_ISO_DESIGNATION (coding, 1) < 0) | |
3616 | goto invalid_code; | |
3617 | CODING_ISO_INVOCATION (coding, 0) = 1; | |
3618 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3619 | continue; |
4ed46869 KH |
3620 | |
3621 | case ISO_shift_in: | |
df7492f9 KH |
3622 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT)) |
3623 | goto invalid_code; | |
3624 | CODING_ISO_INVOCATION (coding, 0) = 0; | |
3625 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3626 | continue; |
4ed46869 KH |
3627 | |
3628 | case ISO_single_shift_2_7: | |
a63dba42 KH |
3629 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS)) |
3630 | goto invalid_code; | |
4ed46869 | 3631 | case ISO_single_shift_2: |
df7492f9 KH |
3632 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)) |
3633 | goto invalid_code; | |
4ed46869 KH |
3634 | /* SS2 is handled as an escape sequence of ESC 'N' */ |
3635 | c1 = 'N'; | |
3636 | goto label_escape_sequence; | |
3637 | ||
3638 | case ISO_single_shift_3: | |
df7492f9 KH |
3639 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)) |
3640 | goto invalid_code; | |
4ed46869 KH |
3641 | /* SS2 is handled as an escape sequence of ESC 'O' */ |
3642 | c1 = 'O'; | |
3643 | goto label_escape_sequence; | |
3644 | ||
3645 | case ISO_control_sequence_introducer: | |
3646 | /* CSI is handled as an escape sequence of ESC '[' ... */ | |
3647 | c1 = '['; | |
3648 | goto label_escape_sequence; | |
3649 | ||
3650 | case ISO_escape: | |
3651 | ONE_MORE_BYTE (c1); | |
3652 | label_escape_sequence: | |
df7492f9 | 3653 | /* Escape sequences handled here are invocation, |
4ed46869 KH |
3654 | designation, direction specification, and character |
3655 | composition specification. */ | |
3656 | switch (c1) | |
3657 | { | |
3658 | case '&': /* revision of following character set */ | |
3659 | ONE_MORE_BYTE (c1); | |
3660 | if (!(c1 >= '@' && c1 <= '~')) | |
df7492f9 | 3661 | goto invalid_code; |
4ed46869 KH |
3662 | ONE_MORE_BYTE (c1); |
3663 | if (c1 != ISO_CODE_ESC) | |
df7492f9 | 3664 | goto invalid_code; |
4ed46869 KH |
3665 | ONE_MORE_BYTE (c1); |
3666 | goto label_escape_sequence; | |
3667 | ||
3668 | case '$': /* designation of 2-byte character set */ | |
df7492f9 KH |
3669 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION)) |
3670 | goto invalid_code; | |
134b9549 KH |
3671 | { |
3672 | int reg, chars96; | |
3673 | ||
3674 | ONE_MORE_BYTE (c1); | |
3675 | if (c1 >= '@' && c1 <= 'B') | |
3676 | { /* designation of JISX0208.1978, GB2312.1980, | |
88993dfd | 3677 | or JISX0208.1980 */ |
134b9549 KH |
3678 | reg = 0, chars96 = 0; |
3679 | } | |
3680 | else if (c1 >= 0x28 && c1 <= 0x2B) | |
3681 | { /* designation of DIMENSION2_CHARS94 character set */ | |
3682 | reg = c1 - 0x28, chars96 = 0; | |
3683 | ONE_MORE_BYTE (c1); | |
3684 | } | |
3685 | else if (c1 >= 0x2C && c1 <= 0x2F) | |
3686 | { /* designation of DIMENSION2_CHARS96 character set */ | |
3687 | reg = c1 - 0x2C, chars96 = 1; | |
3688 | ONE_MORE_BYTE (c1); | |
3689 | } | |
3690 | else | |
3691 | goto invalid_code; | |
3692 | DECODE_DESIGNATION (reg, 2, chars96, c1); | |
3693 | /* We must update these variables now. */ | |
3694 | if (reg == 0) | |
3695 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
3696 | else if (reg == 1) | |
3697 | charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1); | |
3698 | if (chars96 < 0) | |
3699 | goto invalid_code; | |
3700 | } | |
b73bfc1c | 3701 | continue; |
4ed46869 KH |
3702 | |
3703 | case 'n': /* invocation of locking-shift-2 */ | |
df7492f9 KH |
3704 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT) |
3705 | || CODING_ISO_DESIGNATION (coding, 2) < 0) | |
3706 | goto invalid_code; | |
3707 | CODING_ISO_INVOCATION (coding, 0) = 2; | |
3708 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3709 | continue; |
4ed46869 KH |
3710 | |
3711 | case 'o': /* invocation of locking-shift-3 */ | |
df7492f9 KH |
3712 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT) |
3713 | || CODING_ISO_DESIGNATION (coding, 3) < 0) | |
3714 | goto invalid_code; | |
3715 | CODING_ISO_INVOCATION (coding, 0) = 3; | |
3716 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3717 | continue; |
4ed46869 KH |
3718 | |
3719 | case 'N': /* invocation of single-shift-2 */ | |
df7492f9 KH |
3720 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
3721 | || CODING_ISO_DESIGNATION (coding, 2) < 0) | |
3722 | goto invalid_code; | |
134b9549 KH |
3723 | charset_id_2 = CODING_ISO_DESIGNATION (coding, 2); |
3724 | if (charset_id_2 < 0) | |
3725 | charset = CHARSET_FROM_ID (charset_ascii); | |
3726 | else | |
3727 | charset = CHARSET_FROM_ID (charset_id_2); | |
b73bfc1c | 3728 | ONE_MORE_BYTE (c1); |
e7046a18 | 3729 | if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0)) |
df7492f9 | 3730 | goto invalid_code; |
4ed46869 KH |
3731 | break; |
3732 | ||
3733 | case 'O': /* invocation of single-shift-3 */ | |
df7492f9 KH |
3734 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
3735 | || CODING_ISO_DESIGNATION (coding, 3) < 0) | |
3736 | goto invalid_code; | |
134b9549 KH |
3737 | charset_id_3 = CODING_ISO_DESIGNATION (coding, 3); |
3738 | if (charset_id_3 < 0) | |
3739 | charset = CHARSET_FROM_ID (charset_ascii); | |
3740 | else | |
3741 | charset = CHARSET_FROM_ID (charset_id_3); | |
b73bfc1c | 3742 | ONE_MORE_BYTE (c1); |
e7046a18 | 3743 | if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0)) |
df7492f9 | 3744 | goto invalid_code; |
4ed46869 KH |
3745 | break; |
3746 | ||
ec6d2bb8 | 3747 | case '0': case '2': case '3': case '4': /* start composition */ |
df7492f9 KH |
3748 | if (! (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK)) |
3749 | goto invalid_code; | |
e951386e KH |
3750 | if (last_id != charset_ascii) |
3751 | { | |
3752 | ADD_CHARSET_DATA (charbuf, char_offset- last_offset, last_id); | |
3753 | last_id = charset_ascii; | |
3754 | last_offset = char_offset; | |
3755 | } | |
ec6d2bb8 | 3756 | DECODE_COMPOSITION_START (c1); |
b73bfc1c | 3757 | continue; |
4ed46869 | 3758 | |
ec6d2bb8 | 3759 | case '1': /* end composition */ |
e951386e | 3760 | if (cmp_status->state == COMPOSING_NO) |
df7492f9 KH |
3761 | goto invalid_code; |
3762 | DECODE_COMPOSITION_END (); | |
b73bfc1c | 3763 | continue; |
4ed46869 KH |
3764 | |
3765 | case '[': /* specification of direction */ | |
de59072a | 3766 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DIRECTION)) |
df7492f9 | 3767 | goto invalid_code; |
4ed46869 | 3768 | /* For the moment, nested direction is not supported. |
d46c5b12 | 3769 | So, `coding->mode & CODING_MODE_DIRECTION' zero means |
ad1746f5 | 3770 | left-to-right, and nonzero means right-to-left. */ |
4ed46869 KH |
3771 | ONE_MORE_BYTE (c1); |
3772 | switch (c1) | |
3773 | { | |
3774 | case ']': /* end of the current direction */ | |
d46c5b12 | 3775 | coding->mode &= ~CODING_MODE_DIRECTION; |
4ed46869 KH |
3776 | |
3777 | case '0': /* end of the current direction */ | |
3778 | case '1': /* start of left-to-right direction */ | |
3779 | ONE_MORE_BYTE (c1); | |
3780 | if (c1 == ']') | |
d46c5b12 | 3781 | coding->mode &= ~CODING_MODE_DIRECTION; |
4ed46869 | 3782 | else |
df7492f9 | 3783 | goto invalid_code; |
4ed46869 KH |
3784 | break; |
3785 | ||
3786 | case '2': /* start of right-to-left direction */ | |
3787 | ONE_MORE_BYTE (c1); | |
3788 | if (c1 == ']') | |
d46c5b12 | 3789 | coding->mode |= CODING_MODE_DIRECTION; |
4ed46869 | 3790 | else |
df7492f9 | 3791 | goto invalid_code; |
4ed46869 KH |
3792 | break; |
3793 | ||
3794 | default: | |
df7492f9 | 3795 | goto invalid_code; |
4ed46869 | 3796 | } |
b73bfc1c | 3797 | continue; |
4ed46869 | 3798 | |
103e0180 | 3799 | case '%': |
103e0180 KH |
3800 | ONE_MORE_BYTE (c1); |
3801 | if (c1 == '/') | |
3802 | { | |
3803 | /* CTEXT extended segment: | |
3804 | ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES-- | |
3805 | We keep these bytes as is for the moment. | |
3806 | They may be decoded by post-read-conversion. */ | |
3807 | int dim, M, L; | |
4776e638 | 3808 | int size; |
8f924df7 | 3809 | |
103e0180 | 3810 | ONE_MORE_BYTE (dim); |
7a84eee5 | 3811 | if (dim < '0' || dim > '4') |
e951386e | 3812 | goto invalid_code; |
103e0180 | 3813 | ONE_MORE_BYTE (M); |
e951386e KH |
3814 | if (M < 128) |
3815 | goto invalid_code; | |
103e0180 | 3816 | ONE_MORE_BYTE (L); |
e951386e KH |
3817 | if (L < 128) |
3818 | goto invalid_code; | |
103e0180 | 3819 | size = ((M - 128) * 128) + (L - 128); |
e951386e | 3820 | if (charbuf + 6 > charbuf_end) |
4776e638 KH |
3821 | goto break_loop; |
3822 | *charbuf++ = ISO_CODE_ESC; | |
3823 | *charbuf++ = '%'; | |
3824 | *charbuf++ = '/'; | |
3825 | *charbuf++ = dim; | |
3826 | *charbuf++ = BYTE8_TO_CHAR (M); | |
3827 | *charbuf++ = BYTE8_TO_CHAR (L); | |
e951386e | 3828 | CODING_ISO_EXTSEGMENT_LEN (coding) = size; |
103e0180 KH |
3829 | } |
3830 | else if (c1 == 'G') | |
3831 | { | |
103e0180 KH |
3832 | /* XFree86 extension for embedding UTF-8 in CTEXT: |
3833 | ESC % G --UTF-8-BYTES-- ESC % @ | |
3834 | We keep these bytes as is for the moment. | |
3835 | They may be decoded by post-read-conversion. */ | |
e951386e | 3836 | if (charbuf + 3 > charbuf_end) |
4776e638 | 3837 | goto break_loop; |
e951386e KH |
3838 | *charbuf++ = ISO_CODE_ESC; |
3839 | *charbuf++ = '%'; | |
3840 | *charbuf++ = 'G'; | |
3841 | CODING_ISO_EMBEDDED_UTF_8 (coding) = 1; | |
103e0180 KH |
3842 | } |
3843 | else | |
4776e638 | 3844 | goto invalid_code; |
103e0180 | 3845 | continue; |
4776e638 | 3846 | break; |
103e0180 | 3847 | |
4ed46869 | 3848 | default: |
df7492f9 KH |
3849 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION)) |
3850 | goto invalid_code; | |
134b9549 KH |
3851 | { |
3852 | int reg, chars96; | |
3853 | ||
3854 | if (c1 >= 0x28 && c1 <= 0x2B) | |
3855 | { /* designation of DIMENSION1_CHARS94 character set */ | |
3856 | reg = c1 - 0x28, chars96 = 0; | |
3857 | ONE_MORE_BYTE (c1); | |
3858 | } | |
3859 | else if (c1 >= 0x2C && c1 <= 0x2F) | |
3860 | { /* designation of DIMENSION1_CHARS96 character set */ | |
3861 | reg = c1 - 0x2C, chars96 = 1; | |
3862 | ONE_MORE_BYTE (c1); | |
3863 | } | |
3864 | else | |
3865 | goto invalid_code; | |
3866 | DECODE_DESIGNATION (reg, 1, chars96, c1); | |
3867 | /* We must update these variables now. */ | |
3868 | if (reg == 0) | |
3869 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
3870 | else if (reg == 1) | |
3871 | charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1); | |
3872 | if (chars96 < 0) | |
3873 | goto invalid_code; | |
3874 | } | |
b73bfc1c | 3875 | continue; |
4ed46869 | 3876 | } |
413bb2db PE |
3877 | break; |
3878 | ||
3879 | default: | |
3880 | abort (); | |
b73bfc1c | 3881 | } |
4ed46869 | 3882 | |
e951386e KH |
3883 | if (cmp_status->state == COMPOSING_NO |
3884 | && charset->id != charset_ascii | |
ff0dacd7 KH |
3885 | && last_id != charset->id) |
3886 | { | |
3887 | if (last_id != charset_ascii) | |
69a80ea3 | 3888 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
ff0dacd7 KH |
3889 | last_id = charset->id; |
3890 | last_offset = char_offset; | |
3891 | } | |
3892 | ||
b73bfc1c | 3893 | /* Now we know CHARSET and 1st position code C1 of a character. |
cf299835 KH |
3894 | Produce a decoded character while getting 2nd and 3rd |
3895 | position codes C2, C3 if necessary. */ | |
df7492f9 | 3896 | if (CHARSET_DIMENSION (charset) > 1) |
b73bfc1c KH |
3897 | { |
3898 | ONE_MORE_BYTE (c2); | |
cf299835 KH |
3899 | if (c2 < 0x20 || (c2 >= 0x80 && c2 < 0xA0) |
3900 | || ((c1 & 0x80) != (c2 & 0x80))) | |
b73bfc1c | 3901 | /* C2 is not in a valid range. */ |
df7492f9 | 3902 | goto invalid_code; |
cf299835 KH |
3903 | if (CHARSET_DIMENSION (charset) == 2) |
3904 | c1 = (c1 << 8) | c2; | |
3905 | else | |
df7492f9 | 3906 | { |
cf299835 KH |
3907 | ONE_MORE_BYTE (c3); |
3908 | if (c3 < 0x20 || (c3 >= 0x80 && c3 < 0xA0) | |
3909 | || ((c1 & 0x80) != (c3 & 0x80))) | |
3910 | /* C3 is not in a valid range. */ | |
df7492f9 | 3911 | goto invalid_code; |
cf299835 | 3912 | c1 = (c1 << 16) | (c2 << 8) | c2; |
df7492f9 KH |
3913 | } |
3914 | } | |
cf299835 | 3915 | c1 &= 0x7F7F7F; |
df7492f9 KH |
3916 | CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c1, c); |
3917 | if (c < 0) | |
3918 | { | |
3919 | MAYBE_FINISH_COMPOSITION (); | |
3920 | for (; src_base < src; src_base++, char_offset++) | |
3921 | { | |
3922 | if (ASCII_BYTE_P (*src_base)) | |
3923 | *charbuf++ = *src_base; | |
3924 | else | |
3925 | *charbuf++ = BYTE8_TO_CHAR (*src_base); | |
3926 | } | |
3927 | } | |
e951386e | 3928 | else if (cmp_status->state == COMPOSING_NO) |
df7492f9 KH |
3929 | { |
3930 | *charbuf++ = c; | |
3931 | char_offset++; | |
4ed46869 | 3932 | } |
e951386e KH |
3933 | else if ((cmp_status->state == COMPOSING_CHAR |
3934 | ? cmp_status->nchars | |
3935 | : cmp_status->ncomps) | |
3936 | >= MAX_COMPOSITION_COMPONENTS) | |
781d7a48 | 3937 | { |
e951386e KH |
3938 | /* Too long composition. */ |
3939 | MAYBE_FINISH_COMPOSITION (); | |
3940 | *charbuf++ = c; | |
3941 | char_offset++; | |
4ed46869 | 3942 | } |
e951386e KH |
3943 | else |
3944 | STORE_COMPOSITION_CHAR (c); | |
4ed46869 KH |
3945 | continue; |
3946 | ||
df7492f9 KH |
3947 | invalid_code: |
3948 | MAYBE_FINISH_COMPOSITION (); | |
4ed46869 | 3949 | src = src_base; |
df7492f9 KH |
3950 | consumed_chars = consumed_chars_base; |
3951 | ONE_MORE_BYTE (c); | |
065e3595 | 3952 | *charbuf++ = c < 0 ? -c : ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 3953 | char_offset++; |
df7492f9 | 3954 | coding->errors++; |
4776e638 KH |
3955 | continue; |
3956 | ||
3957 | break_loop: | |
3958 | break; | |
4ed46869 | 3959 | } |
fb88bf2d | 3960 | |
df7492f9 | 3961 | no_more_source: |
e951386e KH |
3962 | if (cmp_status->state != COMPOSING_NO) |
3963 | { | |
3964 | if (coding->mode & CODING_MODE_LAST_BLOCK) | |
3965 | MAYBE_FINISH_COMPOSITION (); | |
3966 | else | |
3967 | { | |
3968 | charbuf -= cmp_status->length; | |
3969 | for (i = 0; i < cmp_status->length; i++) | |
3970 | cmp_status->carryover[i] = charbuf[i]; | |
3971 | } | |
3972 | } | |
3973 | else if (last_id != charset_ascii) | |
69a80ea3 | 3974 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
3975 | coding->consumed_char += consumed_chars_base; |
3976 | coding->consumed = src_base - coding->source; | |
3977 | coding->charbuf_used = charbuf - coding->charbuf; | |
4ed46869 KH |
3978 | } |
3979 | ||
b73bfc1c | 3980 | |
f4dee582 | 3981 | /* ISO2022 encoding stuff. */ |
4ed46869 KH |
3982 | |
3983 | /* | |
f4dee582 | 3984 | It is not enough to say just "ISO2022" on encoding, we have to |
df7492f9 | 3985 | specify more details. In Emacs, each coding system of ISO2022 |
4ed46869 | 3986 | variant has the following specifications: |
df7492f9 | 3987 | 1. Initial designation to G0 thru G3. |
4ed46869 KH |
3988 | 2. Allows short-form designation? |
3989 | 3. ASCII should be designated to G0 before control characters? | |
3990 | 4. ASCII should be designated to G0 at end of line? | |
3991 | 5. 7-bit environment or 8-bit environment? | |
3992 | 6. Use locking-shift? | |
3993 | 7. Use Single-shift? | |
3994 | And the following two are only for Japanese: | |
3995 | 8. Use ASCII in place of JIS0201-1976-Roman? | |
3996 | 9. Use JISX0208-1983 in place of JISX0208-1978? | |
df7492f9 KH |
3997 | These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits |
3998 | defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more | |
f4dee582 | 3999 | details. |
4ed46869 KH |
4000 | */ |
4001 | ||
4002 | /* Produce codes (escape sequence) for designating CHARSET to graphic | |
b73bfc1c KH |
4003 | register REG at DST, and increment DST. If <final-char> of CHARSET is |
4004 | '@', 'A', or 'B' and the coding system CODING allows, produce | |
4005 | designation sequence of short-form. */ | |
4ed46869 KH |
4006 | |
4007 | #define ENCODE_DESIGNATION(charset, reg, coding) \ | |
4008 | do { \ | |
df7492f9 | 4009 | unsigned char final_char = CHARSET_ISO_FINAL (charset); \ |
675e2c69 DN |
4010 | const char *intermediate_char_94 = "()*+"; \ |
4011 | const char *intermediate_char_96 = ",-./"; \ | |
df7492f9 | 4012 | int revision = -1; \ |
df7492f9 KH |
4013 | \ |
4014 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \ | |
c197f191 | 4015 | revision = CHARSET_ISO_REVISION (charset); \ |
df7492f9 KH |
4016 | \ |
4017 | if (revision >= 0) \ | |
70c22245 | 4018 | { \ |
df7492f9 KH |
4019 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \ |
4020 | EMIT_ONE_BYTE ('@' + revision); \ | |
4ed46869 | 4021 | } \ |
df7492f9 | 4022 | EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \ |
4ed46869 KH |
4023 | if (CHARSET_DIMENSION (charset) == 1) \ |
4024 | { \ | |
2735d060 | 4025 | int b; \ |
df7492f9 | 4026 | if (! CHARSET_ISO_CHARS_96 (charset)) \ |
2735d060 | 4027 | b = intermediate_char_94[reg]; \ |
4ed46869 | 4028 | else \ |
2735d060 PE |
4029 | b = intermediate_char_96[reg]; \ |
4030 | EMIT_ONE_ASCII_BYTE (b); \ | |
4ed46869 KH |
4031 | } \ |
4032 | else \ | |
4033 | { \ | |
df7492f9 KH |
4034 | EMIT_ONE_ASCII_BYTE ('$'); \ |
4035 | if (! CHARSET_ISO_CHARS_96 (charset)) \ | |
4ed46869 | 4036 | { \ |
df7492f9 | 4037 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \ |
b73bfc1c KH |
4038 | || reg != 0 \ |
4039 | || final_char < '@' || final_char > 'B') \ | |
df7492f9 | 4040 | EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \ |
4ed46869 KH |
4041 | } \ |
4042 | else \ | |
df7492f9 | 4043 | EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \ |
4ed46869 | 4044 | } \ |
df7492f9 KH |
4045 | EMIT_ONE_ASCII_BYTE (final_char); \ |
4046 | \ | |
4047 | CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \ | |
4ed46869 KH |
4048 | } while (0) |
4049 | ||
df7492f9 | 4050 | |
4ed46869 KH |
4051 | /* The following two macros produce codes (control character or escape |
4052 | sequence) for ISO2022 single-shift functions (single-shift-2 and | |
4053 | single-shift-3). */ | |
4054 | ||
df7492f9 KH |
4055 | #define ENCODE_SINGLE_SHIFT_2 \ |
4056 | do { \ | |
4057 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ | |
4058 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \ | |
4059 | else \ | |
4060 | EMIT_ONE_BYTE (ISO_CODE_SS2); \ | |
4061 | CODING_ISO_SINGLE_SHIFTING (coding) = 1; \ | |
4ed46869 KH |
4062 | } while (0) |
4063 | ||
df7492f9 KH |
4064 | |
4065 | #define ENCODE_SINGLE_SHIFT_3 \ | |
4066 | do { \ | |
4067 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ | |
4068 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \ | |
4069 | else \ | |
4070 | EMIT_ONE_BYTE (ISO_CODE_SS3); \ | |
4071 | CODING_ISO_SINGLE_SHIFTING (coding) = 1; \ | |
4ed46869 KH |
4072 | } while (0) |
4073 | ||
df7492f9 | 4074 | |
4ed46869 KH |
4075 | /* The following four macros produce codes (control character or |
4076 | escape sequence) for ISO2022 locking-shift functions (shift-in, | |
4077 | shift-out, locking-shift-2, and locking-shift-3). */ | |
4078 | ||
df7492f9 KH |
4079 | #define ENCODE_SHIFT_IN \ |
4080 | do { \ | |
4081 | EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \ | |
4082 | CODING_ISO_INVOCATION (coding, 0) = 0; \ | |
4ed46869 KH |
4083 | } while (0) |
4084 | ||
df7492f9 KH |
4085 | |
4086 | #define ENCODE_SHIFT_OUT \ | |
4087 | do { \ | |
4088 | EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \ | |
4089 | CODING_ISO_INVOCATION (coding, 0) = 1; \ | |
4ed46869 KH |
4090 | } while (0) |
4091 | ||
df7492f9 KH |
4092 | |
4093 | #define ENCODE_LOCKING_SHIFT_2 \ | |
4094 | do { \ | |
4095 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \ | |
4096 | CODING_ISO_INVOCATION (coding, 0) = 2; \ | |
4ed46869 KH |
4097 | } while (0) |
4098 | ||
df7492f9 KH |
4099 | |
4100 | #define ENCODE_LOCKING_SHIFT_3 \ | |
4101 | do { \ | |
4102 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \ | |
4103 | CODING_ISO_INVOCATION (coding, 0) = 3; \ | |
4ed46869 KH |
4104 | } while (0) |
4105 | ||
df7492f9 | 4106 | |
f4dee582 RS |
4107 | /* Produce codes for a DIMENSION1 character whose character set is |
4108 | CHARSET and whose position-code is C1. Designation and invocation | |
4ed46869 KH |
4109 | sequences are also produced in advance if necessary. */ |
4110 | ||
6e85d753 KH |
4111 | #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \ |
4112 | do { \ | |
df7492f9 | 4113 | int id = CHARSET_ID (charset); \ |
bf16eb23 KH |
4114 | \ |
4115 | if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \ | |
4116 | && id == charset_ascii) \ | |
4117 | { \ | |
4118 | id = charset_jisx0201_roman; \ | |
4119 | charset = CHARSET_FROM_ID (id); \ | |
4120 | } \ | |
4121 | \ | |
df7492f9 | 4122 | if (CODING_ISO_SINGLE_SHIFTING (coding)) \ |
6e85d753 | 4123 | { \ |
df7492f9 KH |
4124 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ |
4125 | EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \ | |
6e85d753 | 4126 | else \ |
df7492f9 KH |
4127 | EMIT_ONE_BYTE (c1 | 0x80); \ |
4128 | CODING_ISO_SINGLE_SHIFTING (coding) = 0; \ | |
6e85d753 KH |
4129 | break; \ |
4130 | } \ | |
df7492f9 | 4131 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \ |
6e85d753 | 4132 | { \ |
df7492f9 | 4133 | EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \ |
6e85d753 KH |
4134 | break; \ |
4135 | } \ | |
df7492f9 | 4136 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \ |
6e85d753 | 4137 | { \ |
df7492f9 | 4138 | EMIT_ONE_BYTE (c1 | 0x80); \ |
6e85d753 KH |
4139 | break; \ |
4140 | } \ | |
6e85d753 KH |
4141 | else \ |
4142 | /* Since CHARSET is not yet invoked to any graphic planes, we \ | |
4143 | must invoke it, or, at first, designate it to some graphic \ | |
4144 | register. Then repeat the loop to actually produce the \ | |
4145 | character. */ \ | |
df7492f9 KH |
4146 | dst = encode_invocation_designation (charset, coding, dst, \ |
4147 | &produced_chars); \ | |
4ed46869 KH |
4148 | } while (1) |
4149 | ||
df7492f9 | 4150 | |
f4dee582 RS |
4151 | /* Produce codes for a DIMENSION2 character whose character set is |
4152 | CHARSET and whose position-codes are C1 and C2. Designation and | |
4ed46869 KH |
4153 | invocation codes are also produced in advance if necessary. */ |
4154 | ||
6e85d753 KH |
4155 | #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \ |
4156 | do { \ | |
df7492f9 | 4157 | int id = CHARSET_ID (charset); \ |
bf16eb23 KH |
4158 | \ |
4159 | if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \ | |
4160 | && id == charset_jisx0208) \ | |
4161 | { \ | |
4162 | id = charset_jisx0208_1978; \ | |
4163 | charset = CHARSET_FROM_ID (id); \ | |
4164 | } \ | |
4165 | \ | |
df7492f9 | 4166 | if (CODING_ISO_SINGLE_SHIFTING (coding)) \ |
6e85d753 | 4167 | { \ |
df7492f9 KH |
4168 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ |
4169 | EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \ | |
6e85d753 | 4170 | else \ |
df7492f9 KH |
4171 | EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \ |
4172 | CODING_ISO_SINGLE_SHIFTING (coding) = 0; \ | |
6e85d753 KH |
4173 | break; \ |
4174 | } \ | |
df7492f9 | 4175 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \ |
6e85d753 | 4176 | { \ |
df7492f9 | 4177 | EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \ |
6e85d753 KH |
4178 | break; \ |
4179 | } \ | |
df7492f9 | 4180 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \ |
6e85d753 | 4181 | { \ |
df7492f9 | 4182 | EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \ |
6e85d753 KH |
4183 | break; \ |
4184 | } \ | |
6e85d753 KH |
4185 | else \ |
4186 | /* Since CHARSET is not yet invoked to any graphic planes, we \ | |
4187 | must invoke it, or, at first, designate it to some graphic \ | |
4188 | register. Then repeat the loop to actually produce the \ | |
4189 | character. */ \ | |
df7492f9 KH |
4190 | dst = encode_invocation_designation (charset, coding, dst, \ |
4191 | &produced_chars); \ | |
4ed46869 KH |
4192 | } while (1) |
4193 | ||
05e6f5dc | 4194 | |
df7492f9 KH |
4195 | #define ENCODE_ISO_CHARACTER(charset, c) \ |
4196 | do { \ | |
1a4990fb | 4197 | int code = ENCODE_CHAR ((charset), (c)); \ |
df7492f9 KH |
4198 | \ |
4199 | if (CHARSET_DIMENSION (charset) == 1) \ | |
4200 | ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \ | |
4201 | else \ | |
4202 | ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \ | |
84fbb8a0 | 4203 | } while (0) |
bdd9fb48 | 4204 | |
05e6f5dc | 4205 | |
4ed46869 | 4206 | /* Produce designation and invocation codes at a place pointed by DST |
df7492f9 | 4207 | to use CHARSET. The element `spec.iso_2022' of *CODING is updated. |
4ed46869 KH |
4208 | Return new DST. */ |
4209 | ||
e2f1bab9 | 4210 | static unsigned char * |
cf84bb53 JB |
4211 | encode_invocation_designation (struct charset *charset, |
4212 | struct coding_system *coding, | |
4213 | unsigned char *dst, int *p_nchars) | |
4ed46869 | 4214 | { |
df7492f9 KH |
4215 | int multibytep = coding->dst_multibyte; |
4216 | int produced_chars = *p_nchars; | |
4ed46869 | 4217 | int reg; /* graphic register number */ |
df7492f9 | 4218 | int id = CHARSET_ID (charset); |
4ed46869 KH |
4219 | |
4220 | /* At first, check designations. */ | |
4221 | for (reg = 0; reg < 4; reg++) | |
df7492f9 | 4222 | if (id == CODING_ISO_DESIGNATION (coding, reg)) |
4ed46869 KH |
4223 | break; |
4224 | ||
4225 | if (reg >= 4) | |
4226 | { | |
4227 | /* CHARSET is not yet designated to any graphic registers. */ | |
4228 | /* At first check the requested designation. */ | |
df7492f9 KH |
4229 | reg = CODING_ISO_REQUEST (coding, id); |
4230 | if (reg < 0) | |
1ba9e4ab KH |
4231 | /* Since CHARSET requests no special designation, designate it |
4232 | to graphic register 0. */ | |
4ed46869 KH |
4233 | reg = 0; |
4234 | ||
4235 | ENCODE_DESIGNATION (charset, reg, coding); | |
4236 | } | |
4237 | ||
df7492f9 KH |
4238 | if (CODING_ISO_INVOCATION (coding, 0) != reg |
4239 | && CODING_ISO_INVOCATION (coding, 1) != reg) | |
4ed46869 KH |
4240 | { |
4241 | /* Since the graphic register REG is not invoked to any graphic | |
4242 | planes, invoke it to graphic plane 0. */ | |
4243 | switch (reg) | |
4244 | { | |
4245 | case 0: /* graphic register 0 */ | |
4246 | ENCODE_SHIFT_IN; | |
4247 | break; | |
4248 | ||
4249 | case 1: /* graphic register 1 */ | |
4250 | ENCODE_SHIFT_OUT; | |
4251 | break; | |
4252 | ||
4253 | case 2: /* graphic register 2 */ | |
df7492f9 | 4254 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
4ed46869 KH |
4255 | ENCODE_SINGLE_SHIFT_2; |
4256 | else | |
4257 | ENCODE_LOCKING_SHIFT_2; | |
4258 | break; | |
4259 | ||
4260 | case 3: /* graphic register 3 */ | |
df7492f9 | 4261 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
4ed46869 KH |
4262 | ENCODE_SINGLE_SHIFT_3; |
4263 | else | |
4264 | ENCODE_LOCKING_SHIFT_3; | |
4265 | break; | |
4266 | } | |
4267 | } | |
b73bfc1c | 4268 | |
df7492f9 | 4269 | *p_nchars = produced_chars; |
4ed46869 KH |
4270 | return dst; |
4271 | } | |
4272 | ||
4ed46869 KH |
4273 | |
4274 | /* Produce codes for designation and invocation to reset the graphic | |
4275 | planes and registers to initial state. */ | |
df7492f9 KH |
4276 | #define ENCODE_RESET_PLANE_AND_REGISTER() \ |
4277 | do { \ | |
4278 | int reg; \ | |
4279 | struct charset *charset; \ | |
4280 | \ | |
4281 | if (CODING_ISO_INVOCATION (coding, 0) != 0) \ | |
4282 | ENCODE_SHIFT_IN; \ | |
4283 | for (reg = 0; reg < 4; reg++) \ | |
4284 | if (CODING_ISO_INITIAL (coding, reg) >= 0 \ | |
4285 | && (CODING_ISO_DESIGNATION (coding, reg) \ | |
4286 | != CODING_ISO_INITIAL (coding, reg))) \ | |
4287 | { \ | |
4288 | charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \ | |
4289 | ENCODE_DESIGNATION (charset, reg, coding); \ | |
4290 | } \ | |
4ed46869 KH |
4291 | } while (0) |
4292 | ||
df7492f9 | 4293 | |
bdd9fb48 | 4294 | /* Produce designation sequences of charsets in the line started from |
b73bfc1c | 4295 | SRC to a place pointed by DST, and return updated DST. |
bdd9fb48 KH |
4296 | |
4297 | If the current block ends before any end-of-line, we may fail to | |
d46c5b12 KH |
4298 | find all the necessary designations. */ |
4299 | ||
b73bfc1c | 4300 | static unsigned char * |
cf84bb53 JB |
4301 | encode_designation_at_bol (struct coding_system *coding, int *charbuf, |
4302 | int *charbuf_end, unsigned char *dst) | |
e0e989f6 | 4303 | { |
df7492f9 | 4304 | struct charset *charset; |
bdd9fb48 KH |
4305 | /* Table of charsets to be designated to each graphic register. */ |
4306 | int r[4]; | |
df7492f9 KH |
4307 | int c, found = 0, reg; |
4308 | int produced_chars = 0; | |
4309 | int multibytep = coding->dst_multibyte; | |
4310 | Lisp_Object attrs; | |
4311 | Lisp_Object charset_list; | |
4312 | ||
4313 | attrs = CODING_ID_ATTRS (coding->id); | |
4314 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
4315 | if (EQ (charset_list, Qiso_2022)) | |
4316 | charset_list = Viso_2022_charset_list; | |
bdd9fb48 KH |
4317 | |
4318 | for (reg = 0; reg < 4; reg++) | |
4319 | r[reg] = -1; | |
4320 | ||
b73bfc1c | 4321 | while (found < 4) |
e0e989f6 | 4322 | { |
df7492f9 KH |
4323 | int id; |
4324 | ||
4325 | c = *charbuf++; | |
b73bfc1c KH |
4326 | if (c == '\n') |
4327 | break; | |
df7492f9 KH |
4328 | charset = char_charset (c, charset_list, NULL); |
4329 | id = CHARSET_ID (charset); | |
4330 | reg = CODING_ISO_REQUEST (coding, id); | |
4331 | if (reg >= 0 && r[reg] < 0) | |
bdd9fb48 KH |
4332 | { |
4333 | found++; | |
df7492f9 | 4334 | r[reg] = id; |
bdd9fb48 | 4335 | } |
bdd9fb48 KH |
4336 | } |
4337 | ||
4338 | if (found) | |
4339 | { | |
4340 | for (reg = 0; reg < 4; reg++) | |
4341 | if (r[reg] >= 0 | |
df7492f9 KH |
4342 | && CODING_ISO_DESIGNATION (coding, reg) != r[reg]) |
4343 | ENCODE_DESIGNATION (CHARSET_FROM_ID (r[reg]), reg, coding); | |
e0e989f6 | 4344 | } |
b73bfc1c KH |
4345 | |
4346 | return dst; | |
e0e989f6 KH |
4347 | } |
4348 | ||
4ed46869 KH |
4349 | /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */ |
4350 | ||
df7492f9 | 4351 | static int |
971de7fb | 4352 | encode_coding_iso_2022 (struct coding_system *coding) |
4ed46869 | 4353 | { |
df7492f9 KH |
4354 | int multibytep = coding->dst_multibyte; |
4355 | int *charbuf = coding->charbuf; | |
4356 | int *charbuf_end = charbuf + coding->charbuf_used; | |
4357 | unsigned char *dst = coding->destination + coding->produced; | |
4358 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
4359 | int safe_room = 16; | |
4360 | int bol_designation | |
4361 | = (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATE_AT_BOL | |
4362 | && CODING_ISO_BOL (coding)); | |
4363 | int produced_chars = 0; | |
4364 | Lisp_Object attrs, eol_type, charset_list; | |
4365 | int ascii_compatible; | |
b73bfc1c | 4366 | int c; |
ff0dacd7 | 4367 | int preferred_charset_id = -1; |
05e6f5dc | 4368 | |
24a73b0a | 4369 | CODING_GET_INFO (coding, attrs, charset_list); |
0a9564cb | 4370 | eol_type = inhibit_eol_conversion ? Qunix : CODING_ID_EOL_TYPE (coding->id); |
24a73b0a KH |
4371 | if (VECTORP (eol_type)) |
4372 | eol_type = Qunix; | |
4373 | ||
004068e4 | 4374 | setup_iso_safe_charsets (attrs); |
ff0dacd7 | 4375 | /* Charset list may have been changed. */ |
287c57d7 | 4376 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); |
1b3b981b | 4377 | coding->safe_charsets = SDATA (CODING_ATTR_SAFE_CHARSETS (attrs)); |
0eecad43 | 4378 | |
a552b35a KH |
4379 | ascii_compatible |
4380 | = (! NILP (CODING_ATTR_ASCII_COMPAT (attrs)) | |
4381 | && ! (CODING_ISO_FLAGS (coding) & (CODING_ISO_FLAG_DESIGNATION | |
4382 | | CODING_ISO_FLAG_LOCKING_SHIFT))); | |
bdd9fb48 | 4383 | |
df7492f9 | 4384 | while (charbuf < charbuf_end) |
4ed46869 | 4385 | { |
df7492f9 | 4386 | ASSURE_DESTINATION (safe_room); |
b73bfc1c | 4387 | |
df7492f9 | 4388 | if (bol_designation) |
b73bfc1c | 4389 | { |
df7492f9 | 4390 | unsigned char *dst_prev = dst; |
4ed46869 | 4391 | |
bdd9fb48 | 4392 | /* We have to produce designation sequences if any now. */ |
df7492f9 KH |
4393 | dst = encode_designation_at_bol (coding, charbuf, charbuf_end, dst); |
4394 | bol_designation = 0; | |
4395 | /* We are sure that designation sequences are all ASCII bytes. */ | |
4396 | produced_chars += dst - dst_prev; | |
e0e989f6 KH |
4397 | } |
4398 | ||
df7492f9 | 4399 | c = *charbuf++; |
ec6d2bb8 | 4400 | |
ff0dacd7 KH |
4401 | if (c < 0) |
4402 | { | |
4403 | /* Handle an annotation. */ | |
4404 | switch (*charbuf) | |
ec6d2bb8 | 4405 | { |
ff0dacd7 KH |
4406 | case CODING_ANNOTATE_COMPOSITION_MASK: |
4407 | /* Not yet implemented. */ | |
4408 | break; | |
4409 | case CODING_ANNOTATE_CHARSET_MASK: | |
cf7dfdf5 | 4410 | preferred_charset_id = charbuf[2]; |
ff0dacd7 KH |
4411 | if (preferred_charset_id >= 0 |
4412 | && NILP (Fmemq (make_number (preferred_charset_id), | |
4413 | charset_list))) | |
4414 | preferred_charset_id = -1; | |
4415 | break; | |
4416 | default: | |
4417 | abort (); | |
4ed46869 | 4418 | } |
ff0dacd7 KH |
4419 | charbuf += -c - 1; |
4420 | continue; | |
4ed46869 | 4421 | } |
ec6d2bb8 | 4422 | |
b73bfc1c KH |
4423 | /* Now encode the character C. */ |
4424 | if (c < 0x20 || c == 0x7F) | |
4425 | { | |
df7492f9 KH |
4426 | if (c == '\n' |
4427 | || (c == '\r' && EQ (eol_type, Qmac))) | |
19a8d9e0 | 4428 | { |
df7492f9 KH |
4429 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL) |
4430 | ENCODE_RESET_PLANE_AND_REGISTER (); | |
4431 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_INIT_AT_BOL) | |
b73bfc1c | 4432 | { |
df7492f9 KH |
4433 | int i; |
4434 | ||
4435 | for (i = 0; i < 4; i++) | |
4436 | CODING_ISO_DESIGNATION (coding, i) | |
4437 | = CODING_ISO_INITIAL (coding, i); | |
b73bfc1c | 4438 | } |
df7492f9 KH |
4439 | bol_designation |
4440 | = CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATE_AT_BOL; | |
19a8d9e0 | 4441 | } |
df7492f9 KH |
4442 | else if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_CNTL) |
4443 | ENCODE_RESET_PLANE_AND_REGISTER (); | |
4444 | EMIT_ONE_ASCII_BYTE (c); | |
4ed46869 | 4445 | } |
df7492f9 | 4446 | else if (ASCII_CHAR_P (c)) |
88993dfd | 4447 | { |
df7492f9 KH |
4448 | if (ascii_compatible) |
4449 | EMIT_ONE_ASCII_BYTE (c); | |
93dec019 | 4450 | else |
19a8d9e0 | 4451 | { |
bf16eb23 KH |
4452 | struct charset *charset = CHARSET_FROM_ID (charset_ascii); |
4453 | ENCODE_ISO_CHARACTER (charset, c); | |
19a8d9e0 | 4454 | } |
4ed46869 | 4455 | } |
16eafb5d | 4456 | else if (CHAR_BYTE8_P (c)) |
88993dfd | 4457 | { |
16eafb5d KH |
4458 | c = CHAR_TO_BYTE8 (c); |
4459 | EMIT_ONE_BYTE (c); | |
88993dfd | 4460 | } |
b73bfc1c | 4461 | else |
df7492f9 | 4462 | { |
ff0dacd7 | 4463 | struct charset *charset; |
b73bfc1c | 4464 | |
ff0dacd7 KH |
4465 | if (preferred_charset_id >= 0) |
4466 | { | |
4467 | charset = CHARSET_FROM_ID (preferred_charset_id); | |
4468 | if (! CHAR_CHARSET_P (c, charset)) | |
4469 | charset = char_charset (c, charset_list, NULL); | |
4470 | } | |
4471 | else | |
4472 | charset = char_charset (c, charset_list, NULL); | |
df7492f9 KH |
4473 | if (!charset) |
4474 | { | |
41cbe562 KH |
4475 | if (coding->mode & CODING_MODE_SAFE_ENCODING) |
4476 | { | |
4477 | c = CODING_INHIBIT_CHARACTER_SUBSTITUTION; | |
4478 | charset = CHARSET_FROM_ID (charset_ascii); | |
4479 | } | |
4480 | else | |
4481 | { | |
4482 | c = coding->default_char; | |
4483 | charset = char_charset (c, charset_list, NULL); | |
4484 | } | |
df7492f9 KH |
4485 | } |
4486 | ENCODE_ISO_CHARACTER (charset, c); | |
4487 | } | |
84fbb8a0 | 4488 | } |
b73bfc1c | 4489 | |
df7492f9 KH |
4490 | if (coding->mode & CODING_MODE_LAST_BLOCK |
4491 | && CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL) | |
4492 | { | |
4493 | ASSURE_DESTINATION (safe_room); | |
4494 | ENCODE_RESET_PLANE_AND_REGISTER (); | |
4495 | } | |
065e3595 | 4496 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
4497 | CODING_ISO_BOL (coding) = bol_designation; |
4498 | coding->produced_char += produced_chars; | |
4499 | coding->produced = dst - coding->destination; | |
4500 | return 0; | |
4ed46869 KH |
4501 | } |
4502 | ||
4503 | \f | |
df7492f9 | 4504 | /*** 8,9. SJIS and BIG5 handlers ***/ |
4ed46869 | 4505 | |
df7492f9 | 4506 | /* Although SJIS and BIG5 are not ISO's coding system, they are used |
4ed46869 KH |
4507 | quite widely. So, for the moment, Emacs supports them in the bare |
4508 | C code. But, in the future, they may be supported only by CCL. */ | |
4509 | ||
4510 | /* SJIS is a coding system encoding three character sets: ASCII, right | |
4511 | half of JISX0201-Kana, and JISX0208. An ASCII character is encoded | |
4512 | as is. A character of charset katakana-jisx0201 is encoded by | |
4513 | "position-code + 0x80". A character of charset japanese-jisx0208 | |
4514 | is encoded in 2-byte but two position-codes are divided and shifted | |
df7492f9 | 4515 | so that it fit in the range below. |
4ed46869 KH |
4516 | |
4517 | --- CODE RANGE of SJIS --- | |
4518 | (character set) (range) | |
4519 | ASCII 0x00 .. 0x7F | |
df7492f9 | 4520 | KATAKANA-JISX0201 0xA0 .. 0xDF |
c28a9453 | 4521 | JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF |
d14d03ac | 4522 | (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC |
4ed46869 KH |
4523 | ------------------------------- |
4524 | ||
4525 | */ | |
4526 | ||
4527 | /* BIG5 is a coding system encoding two character sets: ASCII and | |
4528 | Big5. An ASCII character is encoded as is. Big5 is a two-byte | |
df7492f9 | 4529 | character set and is encoded in two-byte. |
4ed46869 KH |
4530 | |
4531 | --- CODE RANGE of BIG5 --- | |
4532 | (character set) (range) | |
4533 | ASCII 0x00 .. 0x7F | |
4534 | Big5 (1st byte) 0xA1 .. 0xFE | |
4535 | (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE | |
4536 | -------------------------- | |
4537 | ||
df7492f9 | 4538 | */ |
4ed46869 KH |
4539 | |
4540 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
4541 | Check if a text is encoded in SJIS. If it is, return | |
df7492f9 | 4542 | CATEGORY_MASK_SJIS, else return 0. */ |
4ed46869 | 4543 | |
0a28aafb | 4544 | static int |
cf84bb53 JB |
4545 | detect_coding_sjis (struct coding_system *coding, |
4546 | struct coding_detection_info *detect_info) | |
4ed46869 | 4547 | { |
065e3595 | 4548 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 4549 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
4550 | int multibytep = coding->src_multibyte; |
4551 | int consumed_chars = 0; | |
4552 | int found = 0; | |
b73bfc1c | 4553 | int c; |
f07190ca KH |
4554 | Lisp_Object attrs, charset_list; |
4555 | int max_first_byte_of_2_byte_code; | |
4556 | ||
4557 | CODING_GET_INFO (coding, attrs, charset_list); | |
4558 | max_first_byte_of_2_byte_code | |
4559 | = (XINT (Flength (charset_list)) > 3 ? 0xFC : 0xEF); | |
df7492f9 | 4560 | |
ff0dacd7 | 4561 | detect_info->checked |= CATEGORY_MASK_SJIS; |
df7492f9 KH |
4562 | /* A coding system of this category is always ASCII compatible. */ |
4563 | src += coding->head_ascii; | |
4ed46869 | 4564 | |
b73bfc1c | 4565 | while (1) |
4ed46869 | 4566 | { |
065e3595 | 4567 | src_base = src; |
df7492f9 | 4568 | ONE_MORE_BYTE (c); |
682169fe KH |
4569 | if (c < 0x80) |
4570 | continue; | |
f07190ca KH |
4571 | if ((c >= 0x81 && c <= 0x9F) |
4572 | || (c >= 0xE0 && c <= max_first_byte_of_2_byte_code)) | |
4ed46869 | 4573 | { |
df7492f9 | 4574 | ONE_MORE_BYTE (c); |
682169fe | 4575 | if (c < 0x40 || c == 0x7F || c > 0xFC) |
df7492f9 | 4576 | break; |
ff0dacd7 | 4577 | found = CATEGORY_MASK_SJIS; |
4ed46869 | 4578 | } |
df7492f9 | 4579 | else if (c >= 0xA0 && c < 0xE0) |
ff0dacd7 | 4580 | found = CATEGORY_MASK_SJIS; |
df7492f9 KH |
4581 | else |
4582 | break; | |
4ed46869 | 4583 | } |
ff0dacd7 | 4584 | detect_info->rejected |= CATEGORY_MASK_SJIS; |
df7492f9 KH |
4585 | return 0; |
4586 | ||
4587 | no_more_source: | |
065e3595 | 4588 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) |
89528eb3 | 4589 | { |
ff0dacd7 | 4590 | detect_info->rejected |= CATEGORY_MASK_SJIS; |
89528eb3 | 4591 | return 0; |
4ed46869 | 4592 | } |
ff0dacd7 KH |
4593 | detect_info->found |= found; |
4594 | return 1; | |
4ed46869 KH |
4595 | } |
4596 | ||
4597 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
4598 | Check if a text is encoded in BIG5. If it is, return | |
df7492f9 | 4599 | CATEGORY_MASK_BIG5, else return 0. */ |
4ed46869 | 4600 | |
0a28aafb | 4601 | static int |
cf84bb53 JB |
4602 | detect_coding_big5 (struct coding_system *coding, |
4603 | struct coding_detection_info *detect_info) | |
4ed46869 | 4604 | { |
065e3595 | 4605 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 4606 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
4607 | int multibytep = coding->src_multibyte; |
4608 | int consumed_chars = 0; | |
4609 | int found = 0; | |
b73bfc1c | 4610 | int c; |
fa42c37f | 4611 | |
ff0dacd7 | 4612 | detect_info->checked |= CATEGORY_MASK_BIG5; |
df7492f9 KH |
4613 | /* A coding system of this category is always ASCII compatible. */ |
4614 | src += coding->head_ascii; | |
fa42c37f | 4615 | |
b73bfc1c | 4616 | while (1) |
fa42c37f | 4617 | { |
065e3595 | 4618 | src_base = src; |
df7492f9 KH |
4619 | ONE_MORE_BYTE (c); |
4620 | if (c < 0x80) | |
fa42c37f | 4621 | continue; |
df7492f9 | 4622 | if (c >= 0xA1) |
fa42c37f | 4623 | { |
df7492f9 KH |
4624 | ONE_MORE_BYTE (c); |
4625 | if (c < 0x40 || (c >= 0x7F && c <= 0xA0)) | |
fa42c37f | 4626 | return 0; |
ff0dacd7 | 4627 | found = CATEGORY_MASK_BIG5; |
fa42c37f | 4628 | } |
df7492f9 KH |
4629 | else |
4630 | break; | |
fa42c37f | 4631 | } |
ff0dacd7 | 4632 | detect_info->rejected |= CATEGORY_MASK_BIG5; |
fa42c37f | 4633 | return 0; |
fa42c37f | 4634 | |
df7492f9 | 4635 | no_more_source: |
065e3595 | 4636 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) |
89528eb3 | 4637 | { |
ff0dacd7 | 4638 | detect_info->rejected |= CATEGORY_MASK_BIG5; |
89528eb3 KH |
4639 | return 0; |
4640 | } | |
ff0dacd7 KH |
4641 | detect_info->found |= found; |
4642 | return 1; | |
fa42c37f KH |
4643 | } |
4644 | ||
4ed46869 KH |
4645 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". |
4646 | If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */ | |
fa42c37f | 4647 | |
b73bfc1c | 4648 | static void |
971de7fb | 4649 | decode_coding_sjis (struct coding_system *coding) |
4ed46869 | 4650 | { |
8f924df7 KH |
4651 | const unsigned char *src = coding->source + coding->consumed; |
4652 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
4653 | const unsigned char *src_base; | |
69a80ea3 | 4654 | int *charbuf = coding->charbuf + coding->charbuf_used; |
ad1746f5 | 4655 | /* We may produce one charset annotation in one loop and one more at |
df80c7f0 | 4656 | the end. */ |
69a80ea3 | 4657 | int *charbuf_end |
df80c7f0 | 4658 | = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 2); |
df7492f9 KH |
4659 | int consumed_chars = 0, consumed_chars_base; |
4660 | int multibytep = coding->src_multibyte; | |
4661 | struct charset *charset_roman, *charset_kanji, *charset_kana; | |
57a47f8a | 4662 | struct charset *charset_kanji2; |
24a73b0a | 4663 | Lisp_Object attrs, charset_list, val; |
ff0dacd7 KH |
4664 | int char_offset = coding->produced_char; |
4665 | int last_offset = char_offset; | |
4666 | int last_id = charset_ascii; | |
2735d060 | 4667 | int eol_dos = |
0a9564cb | 4668 | !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); |
119852e7 | 4669 | int byte_after_cr = -1; |
a5d301df | 4670 | |
24a73b0a | 4671 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4672 | |
4673 | val = charset_list; | |
4674 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
89528eb3 | 4675 | charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
57a47f8a KH |
4676 | charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
4677 | charset_kanji2 = NILP (val) ? NULL : CHARSET_FROM_ID (XINT (XCAR (val))); | |
fa42c37f | 4678 | |
b73bfc1c | 4679 | while (1) |
4ed46869 | 4680 | { |
df7492f9 | 4681 | int c, c1; |
24a73b0a | 4682 | struct charset *charset; |
fa42c37f | 4683 | |
b73bfc1c | 4684 | src_base = src; |
df7492f9 | 4685 | consumed_chars_base = consumed_chars; |
fa42c37f | 4686 | |
df7492f9 | 4687 | if (charbuf >= charbuf_end) |
b71f6f73 KH |
4688 | { |
4689 | if (byte_after_cr >= 0) | |
4690 | src_base--; | |
4691 | break; | |
4692 | } | |
df7492f9 | 4693 | |
119852e7 KH |
4694 | if (byte_after_cr >= 0) |
4695 | c = byte_after_cr, byte_after_cr = -1; | |
4696 | else | |
4697 | ONE_MORE_BYTE (c); | |
065e3595 KH |
4698 | if (c < 0) |
4699 | goto invalid_code; | |
24a73b0a | 4700 | if (c < 0x80) |
119852e7 | 4701 | { |
2735d060 | 4702 | if (eol_dos && c == '\r') |
119852e7 KH |
4703 | ONE_MORE_BYTE (byte_after_cr); |
4704 | charset = charset_roman; | |
4705 | } | |
57a47f8a | 4706 | else if (c == 0x80 || c == 0xA0) |
8e921c4b | 4707 | goto invalid_code; |
57a47f8a KH |
4708 | else if (c >= 0xA1 && c <= 0xDF) |
4709 | { | |
4710 | /* SJIS -> JISX0201-Kana */ | |
4711 | c &= 0x7F; | |
4712 | charset = charset_kana; | |
4713 | } | |
4714 | else if (c <= 0xEF) | |
df7492f9 | 4715 | { |
57a47f8a KH |
4716 | /* SJIS -> JISX0208 */ |
4717 | ONE_MORE_BYTE (c1); | |
4718 | if (c1 < 0x40 || c1 == 0x7F || c1 > 0xFC) | |
24a73b0a | 4719 | goto invalid_code; |
57a47f8a KH |
4720 | c = (c << 8) | c1; |
4721 | SJIS_TO_JIS (c); | |
4722 | charset = charset_kanji; | |
4723 | } | |
4724 | else if (c <= 0xFC && charset_kanji2) | |
4725 | { | |
c6876370 | 4726 | /* SJIS -> JISX0213-2 */ |
57a47f8a KH |
4727 | ONE_MORE_BYTE (c1); |
4728 | if (c1 < 0x40 || c1 == 0x7F || c1 > 0xFC) | |
24a73b0a | 4729 | goto invalid_code; |
57a47f8a KH |
4730 | c = (c << 8) | c1; |
4731 | SJIS_TO_JIS2 (c); | |
4732 | charset = charset_kanji2; | |
df7492f9 | 4733 | } |
57a47f8a KH |
4734 | else |
4735 | goto invalid_code; | |
24a73b0a KH |
4736 | if (charset->id != charset_ascii |
4737 | && last_id != charset->id) | |
4738 | { | |
4739 | if (last_id != charset_ascii) | |
69a80ea3 | 4740 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
24a73b0a KH |
4741 | last_id = charset->id; |
4742 | last_offset = char_offset; | |
4743 | } | |
4744 | CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c); | |
df7492f9 | 4745 | *charbuf++ = c; |
ff0dacd7 | 4746 | char_offset++; |
df7492f9 | 4747 | continue; |
b73bfc1c | 4748 | |
df7492f9 KH |
4749 | invalid_code: |
4750 | src = src_base; | |
4751 | consumed_chars = consumed_chars_base; | |
4752 | ONE_MORE_BYTE (c); | |
065e3595 | 4753 | *charbuf++ = c < 0 ? -c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 4754 | char_offset++; |
df7492f9 KH |
4755 | coding->errors++; |
4756 | } | |
fa42c37f | 4757 | |
df7492f9 | 4758 | no_more_source: |
ff0dacd7 | 4759 | if (last_id != charset_ascii) |
69a80ea3 | 4760 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
4761 | coding->consumed_char += consumed_chars_base; |
4762 | coding->consumed = src_base - coding->source; | |
4763 | coding->charbuf_used = charbuf - coding->charbuf; | |
fa42c37f KH |
4764 | } |
4765 | ||
b73bfc1c | 4766 | static void |
971de7fb | 4767 | decode_coding_big5 (struct coding_system *coding) |
4ed46869 | 4768 | { |
8f924df7 KH |
4769 | const unsigned char *src = coding->source + coding->consumed; |
4770 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
4771 | const unsigned char *src_base; | |
69a80ea3 | 4772 | int *charbuf = coding->charbuf + coding->charbuf_used; |
ad1746f5 | 4773 | /* We may produce one charset annotation in one loop and one more at |
df80c7f0 | 4774 | the end. */ |
69a80ea3 | 4775 | int *charbuf_end |
df80c7f0 | 4776 | = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 2); |
df7492f9 KH |
4777 | int consumed_chars = 0, consumed_chars_base; |
4778 | int multibytep = coding->src_multibyte; | |
4779 | struct charset *charset_roman, *charset_big5; | |
24a73b0a | 4780 | Lisp_Object attrs, charset_list, val; |
ff0dacd7 KH |
4781 | int char_offset = coding->produced_char; |
4782 | int last_offset = char_offset; | |
4783 | int last_id = charset_ascii; | |
2735d060 | 4784 | int eol_dos = |
0a9564cb | 4785 | !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); |
119852e7 | 4786 | int byte_after_cr = -1; |
df7492f9 | 4787 | |
24a73b0a | 4788 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4789 | val = charset_list; |
4790 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
4791 | charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val))); | |
4ed46869 | 4792 | |
b73bfc1c | 4793 | while (1) |
4ed46869 | 4794 | { |
df7492f9 | 4795 | int c, c1; |
24a73b0a | 4796 | struct charset *charset; |
b73bfc1c KH |
4797 | |
4798 | src_base = src; | |
df7492f9 KH |
4799 | consumed_chars_base = consumed_chars; |
4800 | ||
4801 | if (charbuf >= charbuf_end) | |
b71f6f73 KH |
4802 | { |
4803 | if (byte_after_cr >= 0) | |
4804 | src_base--; | |
4805 | break; | |
4806 | } | |
df7492f9 | 4807 | |
119852e7 | 4808 | if (byte_after_cr >= 0) |
14daee73 | 4809 | c = byte_after_cr, byte_after_cr = -1; |
119852e7 KH |
4810 | else |
4811 | ONE_MORE_BYTE (c); | |
b73bfc1c | 4812 | |
065e3595 KH |
4813 | if (c < 0) |
4814 | goto invalid_code; | |
24a73b0a | 4815 | if (c < 0x80) |
119852e7 | 4816 | { |
2735d060 | 4817 | if (eol_dos && c == '\r') |
119852e7 KH |
4818 | ONE_MORE_BYTE (byte_after_cr); |
4819 | charset = charset_roman; | |
4820 | } | |
24a73b0a | 4821 | else |
4ed46869 | 4822 | { |
24a73b0a KH |
4823 | /* BIG5 -> Big5 */ |
4824 | if (c < 0xA1 || c > 0xFE) | |
4825 | goto invalid_code; | |
4826 | ONE_MORE_BYTE (c1); | |
4827 | if (c1 < 0x40 || (c1 > 0x7E && c1 < 0xA1) || c1 > 0xFE) | |
4828 | goto invalid_code; | |
4829 | c = c << 8 | c1; | |
4830 | charset = charset_big5; | |
4ed46869 | 4831 | } |
24a73b0a KH |
4832 | if (charset->id != charset_ascii |
4833 | && last_id != charset->id) | |
df7492f9 | 4834 | { |
24a73b0a | 4835 | if (last_id != charset_ascii) |
69a80ea3 | 4836 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
24a73b0a KH |
4837 | last_id = charset->id; |
4838 | last_offset = char_offset; | |
4ed46869 | 4839 | } |
24a73b0a | 4840 | CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c); |
df7492f9 | 4841 | *charbuf++ = c; |
ff0dacd7 | 4842 | char_offset++; |
fb88bf2d KH |
4843 | continue; |
4844 | ||
df7492f9 | 4845 | invalid_code: |
4ed46869 | 4846 | src = src_base; |
df7492f9 KH |
4847 | consumed_chars = consumed_chars_base; |
4848 | ONE_MORE_BYTE (c); | |
065e3595 | 4849 | *charbuf++ = c < 0 ? -c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 4850 | char_offset++; |
df7492f9 | 4851 | coding->errors++; |
fb88bf2d | 4852 | } |
d46c5b12 | 4853 | |
df7492f9 | 4854 | no_more_source: |
ff0dacd7 | 4855 | if (last_id != charset_ascii) |
69a80ea3 | 4856 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
4857 | coding->consumed_char += consumed_chars_base; |
4858 | coding->consumed = src_base - coding->source; | |
4859 | coding->charbuf_used = charbuf - coding->charbuf; | |
4ed46869 KH |
4860 | } |
4861 | ||
4862 | /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". | |
b73bfc1c KH |
4863 | This function can encode charsets `ascii', `katakana-jisx0201', |
4864 | `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We | |
4865 | are sure that all these charsets are registered as official charset | |
4ed46869 KH |
4866 | (i.e. do not have extended leading-codes). Characters of other |
4867 | charsets are produced without any encoding. If SJIS_P is 1, encode | |
4868 | SJIS text, else encode BIG5 text. */ | |
4869 | ||
df7492f9 | 4870 | static int |
971de7fb | 4871 | encode_coding_sjis (struct coding_system *coding) |
4ed46869 | 4872 | { |
df7492f9 KH |
4873 | int multibytep = coding->dst_multibyte; |
4874 | int *charbuf = coding->charbuf; | |
4875 | int *charbuf_end = charbuf + coding->charbuf_used; | |
4876 | unsigned char *dst = coding->destination + coding->produced; | |
4877 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
4878 | int safe_room = 4; | |
4879 | int produced_chars = 0; | |
24a73b0a | 4880 | Lisp_Object attrs, charset_list, val; |
df7492f9 KH |
4881 | int ascii_compatible; |
4882 | struct charset *charset_roman, *charset_kanji, *charset_kana; | |
57a47f8a | 4883 | struct charset *charset_kanji2; |
df7492f9 | 4884 | int c; |
a5d301df | 4885 | |
24a73b0a | 4886 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4887 | val = charset_list; |
4888 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
4889 | charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
57a47f8a KH |
4890 | charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
4891 | charset_kanji2 = NILP (val) ? NULL : CHARSET_FROM_ID (XINT (XCAR (val))); | |
4ed46869 | 4892 | |
df7492f9 | 4893 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); |
93dec019 | 4894 | |
df7492f9 KH |
4895 | while (charbuf < charbuf_end) |
4896 | { | |
4897 | ASSURE_DESTINATION (safe_room); | |
4898 | c = *charbuf++; | |
b73bfc1c | 4899 | /* Now encode the character C. */ |
df7492f9 KH |
4900 | if (ASCII_CHAR_P (c) && ascii_compatible) |
4901 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d KH |
4902 | else if (CHAR_BYTE8_P (c)) |
4903 | { | |
4904 | c = CHAR_TO_BYTE8 (c); | |
4905 | EMIT_ONE_BYTE (c); | |
4906 | } | |
df7492f9 | 4907 | else |
b73bfc1c | 4908 | { |
df7492f9 KH |
4909 | unsigned code; |
4910 | struct charset *charset = char_charset (c, charset_list, &code); | |
4911 | ||
4912 | if (!charset) | |
4ed46869 | 4913 | { |
41cbe562 | 4914 | if (coding->mode & CODING_MODE_SAFE_ENCODING) |
b73bfc1c | 4915 | { |
41cbe562 KH |
4916 | code = CODING_INHIBIT_CHARACTER_SUBSTITUTION; |
4917 | charset = CHARSET_FROM_ID (charset_ascii); | |
b73bfc1c | 4918 | } |
41cbe562 | 4919 | else |
b73bfc1c | 4920 | { |
41cbe562 KH |
4921 | c = coding->default_char; |
4922 | charset = char_charset (c, charset_list, &code); | |
b73bfc1c | 4923 | } |
b73bfc1c | 4924 | } |
df7492f9 KH |
4925 | if (code == CHARSET_INVALID_CODE (charset)) |
4926 | abort (); | |
4927 | if (charset == charset_kanji) | |
4928 | { | |
4929 | int c1, c2; | |
4930 | JIS_TO_SJIS (code); | |
4931 | c1 = code >> 8, c2 = code & 0xFF; | |
4932 | EMIT_TWO_BYTES (c1, c2); | |
4933 | } | |
4934 | else if (charset == charset_kana) | |
4935 | EMIT_ONE_BYTE (code | 0x80); | |
57a47f8a KH |
4936 | else if (charset_kanji2 && charset == charset_kanji2) |
4937 | { | |
4938 | int c1, c2; | |
4939 | ||
4940 | c1 = code >> 8; | |
f07190ca KH |
4941 | if (c1 == 0x21 || (c1 >= 0x23 && c1 <= 0x25) |
4942 | || c1 == 0x28 | |
57a47f8a KH |
4943 | || (c1 >= 0x2C && c1 <= 0x2F) || c1 >= 0x6E) |
4944 | { | |
4945 | JIS_TO_SJIS2 (code); | |
4946 | c1 = code >> 8, c2 = code & 0xFF; | |
4947 | EMIT_TWO_BYTES (c1, c2); | |
4948 | } | |
4949 | else | |
4950 | EMIT_ONE_ASCII_BYTE (code & 0x7F); | |
4951 | } | |
df7492f9 KH |
4952 | else |
4953 | EMIT_ONE_ASCII_BYTE (code & 0x7F); | |
4954 | } | |
4955 | } | |
065e3595 | 4956 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
4957 | coding->produced_char += produced_chars; |
4958 | coding->produced = dst - coding->destination; | |
4959 | return 0; | |
4960 | } | |
4961 | ||
4962 | static int | |
971de7fb | 4963 | encode_coding_big5 (struct coding_system *coding) |
df7492f9 KH |
4964 | { |
4965 | int multibytep = coding->dst_multibyte; | |
4966 | int *charbuf = coding->charbuf; | |
4967 | int *charbuf_end = charbuf + coding->charbuf_used; | |
4968 | unsigned char *dst = coding->destination + coding->produced; | |
4969 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
4970 | int safe_room = 4; | |
4971 | int produced_chars = 0; | |
24a73b0a | 4972 | Lisp_Object attrs, charset_list, val; |
df7492f9 KH |
4973 | int ascii_compatible; |
4974 | struct charset *charset_roman, *charset_big5; | |
4975 | int c; | |
4976 | ||
24a73b0a | 4977 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4978 | val = charset_list; |
4979 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
4980 | charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val))); | |
4981 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); | |
4982 | ||
4983 | while (charbuf < charbuf_end) | |
4984 | { | |
4985 | ASSURE_DESTINATION (safe_room); | |
4986 | c = *charbuf++; | |
4987 | /* Now encode the character C. */ | |
4988 | if (ASCII_CHAR_P (c) && ascii_compatible) | |
4989 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d KH |
4990 | else if (CHAR_BYTE8_P (c)) |
4991 | { | |
4992 | c = CHAR_TO_BYTE8 (c); | |
4993 | EMIT_ONE_BYTE (c); | |
b73bfc1c KH |
4994 | } |
4995 | else | |
4996 | { | |
df7492f9 KH |
4997 | unsigned code; |
4998 | struct charset *charset = char_charset (c, charset_list, &code); | |
4999 | ||
5000 | if (! charset) | |
b73bfc1c | 5001 | { |
41cbe562 | 5002 | if (coding->mode & CODING_MODE_SAFE_ENCODING) |
b73bfc1c | 5003 | { |
41cbe562 KH |
5004 | code = CODING_INHIBIT_CHARACTER_SUBSTITUTION; |
5005 | charset = CHARSET_FROM_ID (charset_ascii); | |
b73bfc1c | 5006 | } |
41cbe562 | 5007 | else |
0eecad43 | 5008 | { |
41cbe562 KH |
5009 | c = coding->default_char; |
5010 | charset = char_charset (c, charset_list, &code); | |
0eecad43 | 5011 | } |
4ed46869 | 5012 | } |
df7492f9 KH |
5013 | if (code == CHARSET_INVALID_CODE (charset)) |
5014 | abort (); | |
5015 | if (charset == charset_big5) | |
b73bfc1c | 5016 | { |
df7492f9 KH |
5017 | int c1, c2; |
5018 | ||
5019 | c1 = code >> 8, c2 = code & 0xFF; | |
5020 | EMIT_TWO_BYTES (c1, c2); | |
b73bfc1c | 5021 | } |
df7492f9 KH |
5022 | else |
5023 | EMIT_ONE_ASCII_BYTE (code & 0x7F); | |
4ed46869 | 5024 | } |
4ed46869 | 5025 | } |
065e3595 | 5026 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
5027 | coding->produced_char += produced_chars; |
5028 | coding->produced = dst - coding->destination; | |
5029 | return 0; | |
4ed46869 KH |
5030 | } |
5031 | ||
5032 | \f | |
df7492f9 | 5033 | /*** 10. CCL handlers ***/ |
1397dc18 KH |
5034 | |
5035 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
5036 | Check if a text is encoded in a coding system of which | |
5037 | encoder/decoder are written in CCL program. If it is, return | |
df7492f9 | 5038 | CATEGORY_MASK_CCL, else return 0. */ |
1397dc18 | 5039 | |
0a28aafb | 5040 | static int |
cf84bb53 JB |
5041 | detect_coding_ccl (struct coding_system *coding, |
5042 | struct coding_detection_info *detect_info) | |
1397dc18 | 5043 | { |
065e3595 | 5044 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 5045 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
5046 | int multibytep = coding->src_multibyte; |
5047 | int consumed_chars = 0; | |
5048 | int found = 0; | |
0e219d54 | 5049 | unsigned char *valids; |
df7492f9 KH |
5050 | int head_ascii = coding->head_ascii; |
5051 | Lisp_Object attrs; | |
5052 | ||
ff0dacd7 KH |
5053 | detect_info->checked |= CATEGORY_MASK_CCL; |
5054 | ||
df7492f9 | 5055 | coding = &coding_categories[coding_category_ccl]; |
0e219d54 | 5056 | valids = CODING_CCL_VALIDS (coding); |
df7492f9 KH |
5057 | attrs = CODING_ID_ATTRS (coding->id); |
5058 | if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
5059 | src += head_ascii; | |
1397dc18 | 5060 | |
b73bfc1c | 5061 | while (1) |
1397dc18 | 5062 | { |
df7492f9 | 5063 | int c; |
065e3595 KH |
5064 | |
5065 | src_base = src; | |
df7492f9 | 5066 | ONE_MORE_BYTE (c); |
065e3595 | 5067 | if (c < 0 || ! valids[c]) |
df7492f9 | 5068 | break; |
ff0dacd7 KH |
5069 | if ((valids[c] > 1)) |
5070 | found = CATEGORY_MASK_CCL; | |
df7492f9 | 5071 | } |
ff0dacd7 | 5072 | detect_info->rejected |= CATEGORY_MASK_CCL; |
df7492f9 KH |
5073 | return 0; |
5074 | ||
5075 | no_more_source: | |
ff0dacd7 KH |
5076 | detect_info->found |= found; |
5077 | return 1; | |
df7492f9 KH |
5078 | } |
5079 | ||
5080 | static void | |
971de7fb | 5081 | decode_coding_ccl (struct coding_system *coding) |
df7492f9 | 5082 | { |
7c78e542 | 5083 | const unsigned char *src = coding->source + coding->consumed; |
8f924df7 | 5084 | const unsigned char *src_end = coding->source + coding->src_bytes; |
69a80ea3 KH |
5085 | int *charbuf = coding->charbuf + coding->charbuf_used; |
5086 | int *charbuf_end = coding->charbuf + coding->charbuf_size; | |
df7492f9 KH |
5087 | int consumed_chars = 0; |
5088 | int multibytep = coding->src_multibyte; | |
d0396581 | 5089 | struct ccl_program *ccl = &coding->spec.ccl->ccl; |
df7492f9 | 5090 | int source_charbuf[1024]; |
fbdc1721 | 5091 | int source_byteidx[1025]; |
24a73b0a | 5092 | Lisp_Object attrs, charset_list; |
df7492f9 | 5093 | |
24a73b0a | 5094 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 | 5095 | |
d0396581 | 5096 | while (1) |
df7492f9 | 5097 | { |
7c78e542 | 5098 | const unsigned char *p = src; |
df7492f9 KH |
5099 | int i = 0; |
5100 | ||
5101 | if (multibytep) | |
fbdc1721 KH |
5102 | { |
5103 | while (i < 1024 && p < src_end) | |
5104 | { | |
5105 | source_byteidx[i] = p - src; | |
5106 | source_charbuf[i++] = STRING_CHAR_ADVANCE (p); | |
5107 | } | |
5108 | source_byteidx[i] = p - src; | |
5109 | } | |
df7492f9 KH |
5110 | else |
5111 | while (i < 1024 && p < src_end) | |
5112 | source_charbuf[i++] = *p++; | |
8f924df7 | 5113 | |
df7492f9 | 5114 | if (p == src_end && coding->mode & CODING_MODE_LAST_BLOCK) |
d0396581 KH |
5115 | ccl->last_block = 1; |
5116 | ccl_driver (ccl, source_charbuf, charbuf, i, charbuf_end - charbuf, | |
5117 | charset_list); | |
5118 | charbuf += ccl->produced; | |
fbdc1721 | 5119 | if (multibytep) |
d0396581 | 5120 | src += source_byteidx[ccl->consumed]; |
df7492f9 | 5121 | else |
d0396581 KH |
5122 | src += ccl->consumed; |
5123 | consumed_chars += ccl->consumed; | |
5124 | if (p == src_end || ccl->status != CCL_STAT_SUSPEND_BY_SRC) | |
df7492f9 KH |
5125 | break; |
5126 | } | |
5127 | ||
d0396581 | 5128 | switch (ccl->status) |
df7492f9 KH |
5129 | { |
5130 | case CCL_STAT_SUSPEND_BY_SRC: | |
065e3595 | 5131 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC); |
df7492f9 KH |
5132 | break; |
5133 | case CCL_STAT_SUSPEND_BY_DST: | |
d0396581 | 5134 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_DST); |
df7492f9 KH |
5135 | break; |
5136 | case CCL_STAT_QUIT: | |
5137 | case CCL_STAT_INVALID_CMD: | |
065e3595 | 5138 | record_conversion_result (coding, CODING_RESULT_INTERRUPT); |
df7492f9 KH |
5139 | break; |
5140 | default: | |
065e3595 | 5141 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
5142 | break; |
5143 | } | |
5144 | coding->consumed_char += consumed_chars; | |
5145 | coding->consumed = src - coding->source; | |
5146 | coding->charbuf_used = charbuf - coding->charbuf; | |
5147 | } | |
5148 | ||
5149 | static int | |
971de7fb | 5150 | encode_coding_ccl (struct coding_system *coding) |
df7492f9 | 5151 | { |
fb608df3 | 5152 | struct ccl_program *ccl = &coding->spec.ccl->ccl; |
df7492f9 KH |
5153 | int multibytep = coding->dst_multibyte; |
5154 | int *charbuf = coding->charbuf; | |
5155 | int *charbuf_end = charbuf + coding->charbuf_used; | |
5156 | unsigned char *dst = coding->destination + coding->produced; | |
5157 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
df7492f9 KH |
5158 | int destination_charbuf[1024]; |
5159 | int i, produced_chars = 0; | |
24a73b0a | 5160 | Lisp_Object attrs, charset_list; |
df7492f9 | 5161 | |
24a73b0a | 5162 | CODING_GET_INFO (coding, attrs, charset_list); |
fb608df3 KH |
5163 | if (coding->consumed_char == coding->src_chars |
5164 | && coding->mode & CODING_MODE_LAST_BLOCK) | |
5165 | ccl->last_block = 1; | |
df7492f9 | 5166 | |
8cffd3e7 | 5167 | while (charbuf < charbuf_end) |
df7492f9 | 5168 | { |
fb608df3 | 5169 | ccl_driver (ccl, charbuf, destination_charbuf, |
8cffd3e7 | 5170 | charbuf_end - charbuf, 1024, charset_list); |
df7492f9 | 5171 | if (multibytep) |
8cffd3e7 | 5172 | { |
fb608df3 KH |
5173 | ASSURE_DESTINATION (ccl->produced * 2); |
5174 | for (i = 0; i < ccl->produced; i++) | |
8cffd3e7 KH |
5175 | EMIT_ONE_BYTE (destination_charbuf[i] & 0xFF); |
5176 | } | |
df7492f9 KH |
5177 | else |
5178 | { | |
fb608df3 KH |
5179 | ASSURE_DESTINATION (ccl->produced); |
5180 | for (i = 0; i < ccl->produced; i++) | |
df7492f9 | 5181 | *dst++ = destination_charbuf[i] & 0xFF; |
fb608df3 | 5182 | produced_chars += ccl->produced; |
df7492f9 | 5183 | } |
fb608df3 KH |
5184 | charbuf += ccl->consumed; |
5185 | if (ccl->status == CCL_STAT_QUIT | |
5186 | || ccl->status == CCL_STAT_INVALID_CMD) | |
8cffd3e7 | 5187 | break; |
df7492f9 KH |
5188 | } |
5189 | ||
fb608df3 | 5190 | switch (ccl->status) |
df7492f9 KH |
5191 | { |
5192 | case CCL_STAT_SUSPEND_BY_SRC: | |
065e3595 | 5193 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC); |
df7492f9 KH |
5194 | break; |
5195 | case CCL_STAT_SUSPEND_BY_DST: | |
065e3595 | 5196 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_DST); |
df7492f9 KH |
5197 | break; |
5198 | case CCL_STAT_QUIT: | |
5199 | case CCL_STAT_INVALID_CMD: | |
065e3595 | 5200 | record_conversion_result (coding, CODING_RESULT_INTERRUPT); |
df7492f9 KH |
5201 | break; |
5202 | default: | |
065e3595 | 5203 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 | 5204 | break; |
1397dc18 | 5205 | } |
df7492f9 KH |
5206 | |
5207 | coding->produced_char += produced_chars; | |
5208 | coding->produced = dst - coding->destination; | |
5209 | return 0; | |
1397dc18 KH |
5210 | } |
5211 | ||
df7492f9 | 5212 | |
1397dc18 | 5213 | \f |
df7492f9 | 5214 | /*** 10, 11. no-conversion handlers ***/ |
4ed46869 | 5215 | |
b73bfc1c | 5216 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ |
4ed46869 | 5217 | |
b73bfc1c | 5218 | static void |
971de7fb | 5219 | decode_coding_raw_text (struct coding_system *coding) |
4ed46869 | 5220 | { |
2735d060 | 5221 | int eol_dos = |
0a9564cb | 5222 | !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); |
119852e7 | 5223 | |
df7492f9 | 5224 | coding->chars_at_source = 1; |
119852e7 KH |
5225 | coding->consumed_char = coding->src_chars; |
5226 | coding->consumed = coding->src_bytes; | |
2735d060 | 5227 | if (eol_dos && coding->source[coding->src_bytes - 1] == '\r') |
119852e7 KH |
5228 | { |
5229 | coding->consumed_char--; | |
5230 | coding->consumed--; | |
5231 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC); | |
5232 | } | |
5233 | else | |
5234 | record_conversion_result (coding, CODING_RESULT_SUCCESS); | |
df7492f9 | 5235 | } |
4ed46869 | 5236 | |
df7492f9 | 5237 | static int |
971de7fb | 5238 | encode_coding_raw_text (struct coding_system *coding) |
df7492f9 KH |
5239 | { |
5240 | int multibytep = coding->dst_multibyte; | |
5241 | int *charbuf = coding->charbuf; | |
5242 | int *charbuf_end = coding->charbuf + coding->charbuf_used; | |
5243 | unsigned char *dst = coding->destination + coding->produced; | |
5244 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
a0ed9b27 | 5245 | int produced_chars = 0; |
b73bfc1c KH |
5246 | int c; |
5247 | ||
df7492f9 | 5248 | if (multibytep) |
b73bfc1c | 5249 | { |
df7492f9 | 5250 | int safe_room = MAX_MULTIBYTE_LENGTH * 2; |
4ed46869 | 5251 | |
df7492f9 KH |
5252 | if (coding->src_multibyte) |
5253 | while (charbuf < charbuf_end) | |
5254 | { | |
5255 | ASSURE_DESTINATION (safe_room); | |
5256 | c = *charbuf++; | |
5257 | if (ASCII_CHAR_P (c)) | |
5258 | EMIT_ONE_ASCII_BYTE (c); | |
5259 | else if (CHAR_BYTE8_P (c)) | |
5260 | { | |
5261 | c = CHAR_TO_BYTE8 (c); | |
5262 | EMIT_ONE_BYTE (c); | |
5263 | } | |
5264 | else | |
5265 | { | |
5266 | unsigned char str[MAX_MULTIBYTE_LENGTH], *p0 = str, *p1 = str; | |
93dec019 | 5267 | |
df7492f9 | 5268 | CHAR_STRING_ADVANCE (c, p1); |
8abc3f12 | 5269 | do |
9d123124 KH |
5270 | { |
5271 | EMIT_ONE_BYTE (*p0); | |
5272 | p0++; | |
5273 | } | |
8abc3f12 | 5274 | while (p0 < p1); |
df7492f9 KH |
5275 | } |
5276 | } | |
b73bfc1c | 5277 | else |
df7492f9 KH |
5278 | while (charbuf < charbuf_end) |
5279 | { | |
5280 | ASSURE_DESTINATION (safe_room); | |
5281 | c = *charbuf++; | |
5282 | EMIT_ONE_BYTE (c); | |
5283 | } | |
5284 | } | |
5285 | else | |
4ed46869 | 5286 | { |
df7492f9 | 5287 | if (coding->src_multibyte) |
d46c5b12 | 5288 | { |
df7492f9 KH |
5289 | int safe_room = MAX_MULTIBYTE_LENGTH; |
5290 | ||
5291 | while (charbuf < charbuf_end) | |
d46c5b12 | 5292 | { |
df7492f9 KH |
5293 | ASSURE_DESTINATION (safe_room); |
5294 | c = *charbuf++; | |
5295 | if (ASCII_CHAR_P (c)) | |
5296 | *dst++ = c; | |
5297 | else if (CHAR_BYTE8_P (c)) | |
5298 | *dst++ = CHAR_TO_BYTE8 (c); | |
b73bfc1c | 5299 | else |
df7492f9 | 5300 | CHAR_STRING_ADVANCE (c, dst); |
d46c5b12 KH |
5301 | } |
5302 | } | |
df7492f9 KH |
5303 | else |
5304 | { | |
5305 | ASSURE_DESTINATION (charbuf_end - charbuf); | |
5306 | while (charbuf < charbuf_end && dst < dst_end) | |
5307 | *dst++ = *charbuf++; | |
8f924df7 | 5308 | } |
319a3947 | 5309 | produced_chars = dst - (coding->destination + coding->produced); |
4ed46869 | 5310 | } |
065e3595 | 5311 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
a0ed9b27 | 5312 | coding->produced_char += produced_chars; |
df7492f9 KH |
5313 | coding->produced = dst - coding->destination; |
5314 | return 0; | |
4ed46869 KH |
5315 | } |
5316 | ||
ff0dacd7 KH |
5317 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". |
5318 | Check if a text is encoded in a charset-based coding system. If it | |
5319 | is, return 1, else return 0. */ | |
5320 | ||
0a28aafb | 5321 | static int |
cf84bb53 JB |
5322 | detect_coding_charset (struct coding_system *coding, |
5323 | struct coding_detection_info *detect_info) | |
1397dc18 | 5324 | { |
065e3595 | 5325 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 5326 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
5327 | int multibytep = coding->src_multibyte; |
5328 | int consumed_chars = 0; | |
07295713 | 5329 | Lisp_Object attrs, valids, name; |
584948ac | 5330 | int found = 0; |
716b3fa0 | 5331 | int head_ascii = coding->head_ascii; |
07295713 | 5332 | int check_latin_extra = 0; |
1397dc18 | 5333 | |
ff0dacd7 KH |
5334 | detect_info->checked |= CATEGORY_MASK_CHARSET; |
5335 | ||
df7492f9 KH |
5336 | coding = &coding_categories[coding_category_charset]; |
5337 | attrs = CODING_ID_ATTRS (coding->id); | |
5338 | valids = AREF (attrs, coding_attr_charset_valids); | |
07295713 | 5339 | name = CODING_ID_NAME (coding->id); |
51b59d79 | 5340 | if (strncmp (SSDATA (SYMBOL_NAME (name)), |
237aabf4 | 5341 | "iso-8859-", sizeof ("iso-8859-") - 1) == 0 |
51b59d79 | 5342 | || strncmp (SSDATA (SYMBOL_NAME (name)), |
237aabf4 | 5343 | "iso-latin-", sizeof ("iso-latin-") - 1) == 0) |
07295713 | 5344 | check_latin_extra = 1; |
237aabf4 | 5345 | |
df7492f9 | 5346 | if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) |
716b3fa0 | 5347 | src += head_ascii; |
1397dc18 | 5348 | |
b73bfc1c | 5349 | while (1) |
1397dc18 | 5350 | { |
df7492f9 | 5351 | int c; |
716b3fa0 KH |
5352 | Lisp_Object val; |
5353 | struct charset *charset; | |
5354 | int dim, idx; | |
1397dc18 | 5355 | |
065e3595 | 5356 | src_base = src; |
df7492f9 | 5357 | ONE_MORE_BYTE (c); |
065e3595 KH |
5358 | if (c < 0) |
5359 | continue; | |
716b3fa0 KH |
5360 | val = AREF (valids, c); |
5361 | if (NILP (val)) | |
df7492f9 | 5362 | break; |
584948ac | 5363 | if (c >= 0x80) |
07295713 KH |
5364 | { |
5365 | if (c < 0xA0 | |
237aabf4 JR |
5366 | && check_latin_extra |
5367 | && (!VECTORP (Vlatin_extra_code_table) | |
9f0526cb | 5368 | || NILP (XVECTOR (Vlatin_extra_code_table)->contents[c]))) |
07295713 KH |
5369 | break; |
5370 | found = CATEGORY_MASK_CHARSET; | |
5371 | } | |
716b3fa0 KH |
5372 | if (INTEGERP (val)) |
5373 | { | |
5374 | charset = CHARSET_FROM_ID (XFASTINT (val)); | |
5375 | dim = CHARSET_DIMENSION (charset); | |
5376 | for (idx = 1; idx < dim; idx++) | |
5377 | { | |
5378 | if (src == src_end) | |
5379 | goto too_short; | |
5380 | ONE_MORE_BYTE (c); | |
3ed051d4 | 5381 | if (c < charset->code_space[(dim - 1 - idx) * 2] |
716b3fa0 KH |
5382 | || c > charset->code_space[(dim - 1 - idx) * 2 + 1]) |
5383 | break; | |
5384 | } | |
5385 | if (idx < dim) | |
5386 | break; | |
5387 | } | |
5388 | else | |
5389 | { | |
5390 | idx = 1; | |
5391 | for (; CONSP (val); val = XCDR (val)) | |
5392 | { | |
5393 | charset = CHARSET_FROM_ID (XFASTINT (XCAR (val))); | |
5394 | dim = CHARSET_DIMENSION (charset); | |
5395 | while (idx < dim) | |
5396 | { | |
5397 | if (src == src_end) | |
5398 | goto too_short; | |
5399 | ONE_MORE_BYTE (c); | |
5400 | if (c < charset->code_space[(dim - 1 - idx) * 4] | |
5401 | || c > charset->code_space[(dim - 1 - idx) * 4 + 1]) | |
5402 | break; | |
5403 | idx++; | |
5404 | } | |
5405 | if (idx == dim) | |
5406 | { | |
5407 | val = Qnil; | |
5408 | break; | |
5409 | } | |
5410 | } | |
5411 | if (CONSP (val)) | |
5412 | break; | |
5413 | } | |
df7492f9 | 5414 | } |
716b3fa0 | 5415 | too_short: |
ff0dacd7 | 5416 | detect_info->rejected |= CATEGORY_MASK_CHARSET; |
df7492f9 | 5417 | return 0; |
4ed46869 | 5418 | |
df7492f9 | 5419 | no_more_source: |
ff0dacd7 KH |
5420 | detect_info->found |= found; |
5421 | return 1; | |
df7492f9 | 5422 | } |
b73bfc1c | 5423 | |
b73bfc1c | 5424 | static void |
971de7fb | 5425 | decode_coding_charset (struct coding_system *coding) |
4ed46869 | 5426 | { |
8f924df7 KH |
5427 | const unsigned char *src = coding->source + coding->consumed; |
5428 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
5429 | const unsigned char *src_base; | |
69a80ea3 | 5430 | int *charbuf = coding->charbuf + coding->charbuf_used; |
ad1746f5 | 5431 | /* We may produce one charset annotation in one loop and one more at |
df80c7f0 | 5432 | the end. */ |
69a80ea3 | 5433 | int *charbuf_end |
df80c7f0 | 5434 | = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 2); |
df7492f9 KH |
5435 | int consumed_chars = 0, consumed_chars_base; |
5436 | int multibytep = coding->src_multibyte; | |
24a73b0a | 5437 | Lisp_Object attrs, charset_list, valids; |
ff0dacd7 KH |
5438 | int char_offset = coding->produced_char; |
5439 | int last_offset = char_offset; | |
5440 | int last_id = charset_ascii; | |
2735d060 | 5441 | int eol_dos = |
0a9564cb | 5442 | !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); |
119852e7 | 5443 | int byte_after_cr = -1; |
df7492f9 | 5444 | |
24a73b0a | 5445 | CODING_GET_INFO (coding, attrs, charset_list); |
4eb6d3f1 | 5446 | valids = AREF (attrs, coding_attr_charset_valids); |
b73bfc1c | 5447 | |
df7492f9 | 5448 | while (1) |
4ed46869 | 5449 | { |
4eb6d3f1 | 5450 | int c; |
24a73b0a KH |
5451 | Lisp_Object val; |
5452 | struct charset *charset; | |
5453 | int dim; | |
5454 | int len = 1; | |
5455 | unsigned code; | |
df7492f9 KH |
5456 | |
5457 | src_base = src; | |
5458 | consumed_chars_base = consumed_chars; | |
b73bfc1c | 5459 | |
df7492f9 | 5460 | if (charbuf >= charbuf_end) |
b71f6f73 KH |
5461 | { |
5462 | if (byte_after_cr >= 0) | |
5463 | src_base--; | |
5464 | break; | |
5465 | } | |
df7492f9 | 5466 | |
119852e7 KH |
5467 | if (byte_after_cr >= 0) |
5468 | { | |
5469 | c = byte_after_cr; | |
5470 | byte_after_cr = -1; | |
5471 | } | |
5472 | else | |
5473 | { | |
5474 | ONE_MORE_BYTE (c); | |
2735d060 | 5475 | if (eol_dos && c == '\r') |
119852e7 KH |
5476 | ONE_MORE_BYTE (byte_after_cr); |
5477 | } | |
065e3595 KH |
5478 | if (c < 0) |
5479 | goto invalid_code; | |
24a73b0a KH |
5480 | code = c; |
5481 | ||
5482 | val = AREF (valids, c); | |
1b17adfd | 5483 | if (! INTEGERP (val) && ! CONSP (val)) |
24a73b0a KH |
5484 | goto invalid_code; |
5485 | if (INTEGERP (val)) | |
d46c5b12 | 5486 | { |
24a73b0a KH |
5487 | charset = CHARSET_FROM_ID (XFASTINT (val)); |
5488 | dim = CHARSET_DIMENSION (charset); | |
5489 | while (len < dim) | |
b73bfc1c | 5490 | { |
24a73b0a KH |
5491 | ONE_MORE_BYTE (c); |
5492 | code = (code << 8) | c; | |
5493 | len++; | |
b73bfc1c | 5494 | } |
24a73b0a KH |
5495 | CODING_DECODE_CHAR (coding, src, src_base, src_end, |
5496 | charset, code, c); | |
d46c5b12 | 5497 | } |
df7492f9 | 5498 | else |
d46c5b12 | 5499 | { |
24a73b0a KH |
5500 | /* VAL is a list of charset IDs. It is assured that the |
5501 | list is sorted by charset dimensions (smaller one | |
5502 | comes first). */ | |
5503 | while (CONSP (val)) | |
4eb6d3f1 | 5504 | { |
24a73b0a | 5505 | charset = CHARSET_FROM_ID (XFASTINT (XCAR (val))); |
c7c66a95 | 5506 | dim = CHARSET_DIMENSION (charset); |
f9d71dcd | 5507 | while (len < dim) |
4eb6d3f1 | 5508 | { |
acb2a965 KH |
5509 | ONE_MORE_BYTE (c); |
5510 | code = (code << 8) | c; | |
f9d71dcd | 5511 | len++; |
4eb6d3f1 | 5512 | } |
24a73b0a KH |
5513 | CODING_DECODE_CHAR (coding, src, src_base, |
5514 | src_end, charset, code, c); | |
5515 | if (c >= 0) | |
5516 | break; | |
5517 | val = XCDR (val); | |
ff0dacd7 | 5518 | } |
d46c5b12 | 5519 | } |
24a73b0a KH |
5520 | if (c < 0) |
5521 | goto invalid_code; | |
5522 | if (charset->id != charset_ascii | |
5523 | && last_id != charset->id) | |
5524 | { | |
5525 | if (last_id != charset_ascii) | |
69a80ea3 | 5526 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
24a73b0a KH |
5527 | last_id = charset->id; |
5528 | last_offset = char_offset; | |
5529 | } | |
5530 | ||
df7492f9 | 5531 | *charbuf++ = c; |
ff0dacd7 | 5532 | char_offset++; |
df7492f9 KH |
5533 | continue; |
5534 | ||
5535 | invalid_code: | |
5536 | src = src_base; | |
5537 | consumed_chars = consumed_chars_base; | |
5538 | ONE_MORE_BYTE (c); | |
065e3595 | 5539 | *charbuf++ = c < 0 ? -c : ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 5540 | char_offset++; |
df7492f9 | 5541 | coding->errors++; |
4ed46869 KH |
5542 | } |
5543 | ||
df7492f9 | 5544 | no_more_source: |
ff0dacd7 | 5545 | if (last_id != charset_ascii) |
69a80ea3 | 5546 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
5547 | coding->consumed_char += consumed_chars_base; |
5548 | coding->consumed = src_base - coding->source; | |
5549 | coding->charbuf_used = charbuf - coding->charbuf; | |
4ed46869 KH |
5550 | } |
5551 | ||
df7492f9 | 5552 | static int |
971de7fb | 5553 | encode_coding_charset (struct coding_system *coding) |
4ed46869 | 5554 | { |
df7492f9 KH |
5555 | int multibytep = coding->dst_multibyte; |
5556 | int *charbuf = coding->charbuf; | |
5557 | int *charbuf_end = charbuf + coding->charbuf_used; | |
5558 | unsigned char *dst = coding->destination + coding->produced; | |
5559 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
5560 | int safe_room = MAX_MULTIBYTE_LENGTH; | |
5561 | int produced_chars = 0; | |
24a73b0a | 5562 | Lisp_Object attrs, charset_list; |
df7492f9 | 5563 | int ascii_compatible; |
b73bfc1c | 5564 | int c; |
b73bfc1c | 5565 | |
24a73b0a | 5566 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 | 5567 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); |
fb88bf2d | 5568 | |
df7492f9 | 5569 | while (charbuf < charbuf_end) |
4ed46869 | 5570 | { |
4eb6d3f1 | 5571 | struct charset *charset; |
df7492f9 | 5572 | unsigned code; |
8f924df7 | 5573 | |
df7492f9 KH |
5574 | ASSURE_DESTINATION (safe_room); |
5575 | c = *charbuf++; | |
5576 | if (ascii_compatible && ASCII_CHAR_P (c)) | |
5577 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d | 5578 | else if (CHAR_BYTE8_P (c)) |
4ed46869 | 5579 | { |
16eafb5d KH |
5580 | c = CHAR_TO_BYTE8 (c); |
5581 | EMIT_ONE_BYTE (c); | |
d46c5b12 | 5582 | } |
d46c5b12 | 5583 | else |
b73bfc1c | 5584 | { |
4eb6d3f1 KH |
5585 | charset = char_charset (c, charset_list, &code); |
5586 | if (charset) | |
5587 | { | |
5588 | if (CHARSET_DIMENSION (charset) == 1) | |
5589 | EMIT_ONE_BYTE (code); | |
5590 | else if (CHARSET_DIMENSION (charset) == 2) | |
5591 | EMIT_TWO_BYTES (code >> 8, code & 0xFF); | |
5592 | else if (CHARSET_DIMENSION (charset) == 3) | |
5593 | EMIT_THREE_BYTES (code >> 16, (code >> 8) & 0xFF, code & 0xFF); | |
5594 | else | |
5595 | EMIT_FOUR_BYTES (code >> 24, (code >> 16) & 0xFF, | |
5596 | (code >> 8) & 0xFF, code & 0xFF); | |
5597 | } | |
5598 | else | |
41cbe562 KH |
5599 | { |
5600 | if (coding->mode & CODING_MODE_SAFE_ENCODING) | |
5601 | c = CODING_INHIBIT_CHARACTER_SUBSTITUTION; | |
5602 | else | |
5603 | c = coding->default_char; | |
5604 | EMIT_ONE_BYTE (c); | |
5605 | } | |
4ed46869 | 5606 | } |
4ed46869 KH |
5607 | } |
5608 | ||
065e3595 | 5609 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
5610 | coding->produced_char += produced_chars; |
5611 | coding->produced = dst - coding->destination; | |
5612 | return 0; | |
4ed46869 KH |
5613 | } |
5614 | ||
5615 | \f | |
1397dc18 | 5616 | /*** 7. C library functions ***/ |
4ed46869 | 5617 | |
df7492f9 KH |
5618 | /* Setup coding context CODING from information about CODING_SYSTEM. |
5619 | If CODING_SYSTEM is nil, `no-conversion' is assumed. If | |
5620 | CODING_SYSTEM is invalid, signal an error. */ | |
4ed46869 | 5621 | |
ec6d2bb8 | 5622 | void |
971de7fb | 5623 | setup_coding_system (Lisp_Object coding_system, struct coding_system *coding) |
4ed46869 | 5624 | { |
df7492f9 KH |
5625 | Lisp_Object attrs; |
5626 | Lisp_Object eol_type; | |
5627 | Lisp_Object coding_type; | |
4608c386 | 5628 | Lisp_Object val; |
4ed46869 | 5629 | |
df7492f9 | 5630 | if (NILP (coding_system)) |
ae6f73fa | 5631 | coding_system = Qundecided; |
c07c8e12 | 5632 | |
df7492f9 | 5633 | CHECK_CODING_SYSTEM_GET_ID (coding_system, coding->id); |
1f5dbf34 | 5634 | |
df7492f9 | 5635 | attrs = CODING_ID_ATTRS (coding->id); |
0a9564cb | 5636 | eol_type = inhibit_eol_conversion ? Qunix : CODING_ID_EOL_TYPE (coding->id); |
1f5dbf34 | 5637 | |
df7492f9 KH |
5638 | coding->mode = 0; |
5639 | coding->head_ascii = -1; | |
4a015c45 KH |
5640 | if (VECTORP (eol_type)) |
5641 | coding->common_flags = (CODING_REQUIRE_DECODING_MASK | |
5642 | | CODING_REQUIRE_DETECTION_MASK); | |
5643 | else if (! EQ (eol_type, Qunix)) | |
5644 | coding->common_flags = (CODING_REQUIRE_DECODING_MASK | |
5645 | | CODING_REQUIRE_ENCODING_MASK); | |
5646 | else | |
5647 | coding->common_flags = 0; | |
5e5c78be KH |
5648 | if (! NILP (CODING_ATTR_POST_READ (attrs))) |
5649 | coding->common_flags |= CODING_REQUIRE_DECODING_MASK; | |
5650 | if (! NILP (CODING_ATTR_PRE_WRITE (attrs))) | |
5651 | coding->common_flags |= CODING_REQUIRE_ENCODING_MASK; | |
8f924df7 KH |
5652 | if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs))) |
5653 | coding->common_flags |= CODING_FOR_UNIBYTE_MASK; | |
1f5dbf34 | 5654 | |
df7492f9 | 5655 | val = CODING_ATTR_SAFE_CHARSETS (attrs); |
8f924df7 | 5656 | coding->max_charset_id = SCHARS (val) - 1; |
1b3b981b | 5657 | coding->safe_charsets = SDATA (val); |
df7492f9 | 5658 | coding->default_char = XINT (CODING_ATTR_DEFAULT_CHAR (attrs)); |
624bda09 | 5659 | coding->carryover_bytes = 0; |
4608c386 | 5660 | |
df7492f9 KH |
5661 | coding_type = CODING_ATTR_TYPE (attrs); |
5662 | if (EQ (coding_type, Qundecided)) | |
d46c5b12 | 5663 | { |
df7492f9 KH |
5664 | coding->detector = NULL; |
5665 | coding->decoder = decode_coding_raw_text; | |
5666 | coding->encoder = encode_coding_raw_text; | |
5667 | coding->common_flags |= CODING_REQUIRE_DETECTION_MASK; | |
d46c5b12 | 5668 | } |
df7492f9 | 5669 | else if (EQ (coding_type, Qiso_2022)) |
d46c5b12 | 5670 | { |
df7492f9 KH |
5671 | int i; |
5672 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); | |
5673 | ||
5674 | /* Invoke graphic register 0 to plane 0. */ | |
5675 | CODING_ISO_INVOCATION (coding, 0) = 0; | |
5676 | /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */ | |
5677 | CODING_ISO_INVOCATION (coding, 1) | |
5678 | = (flags & CODING_ISO_FLAG_SEVEN_BITS ? -1 : 1); | |
5679 | /* Setup the initial status of designation. */ | |
5680 | for (i = 0; i < 4; i++) | |
5681 | CODING_ISO_DESIGNATION (coding, i) = CODING_ISO_INITIAL (coding, i); | |
5682 | /* Not single shifting initially. */ | |
5683 | CODING_ISO_SINGLE_SHIFTING (coding) = 0; | |
5684 | /* Beginning of buffer should also be regarded as bol. */ | |
5685 | CODING_ISO_BOL (coding) = 1; | |
5686 | coding->detector = detect_coding_iso_2022; | |
5687 | coding->decoder = decode_coding_iso_2022; | |
5688 | coding->encoder = encode_coding_iso_2022; | |
5689 | if (flags & CODING_ISO_FLAG_SAFE) | |
5690 | coding->mode |= CODING_MODE_SAFE_ENCODING; | |
d46c5b12 | 5691 | coding->common_flags |
df7492f9 KH |
5692 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK |
5693 | | CODING_REQUIRE_FLUSHING_MASK); | |
5694 | if (flags & CODING_ISO_FLAG_COMPOSITION) | |
5695 | coding->common_flags |= CODING_ANNOTATE_COMPOSITION_MASK; | |
ff0dacd7 KH |
5696 | if (flags & CODING_ISO_FLAG_DESIGNATION) |
5697 | coding->common_flags |= CODING_ANNOTATE_CHARSET_MASK; | |
df7492f9 KH |
5698 | if (flags & CODING_ISO_FLAG_FULL_SUPPORT) |
5699 | { | |
5700 | setup_iso_safe_charsets (attrs); | |
5701 | val = CODING_ATTR_SAFE_CHARSETS (attrs); | |
8f924df7 | 5702 | coding->max_charset_id = SCHARS (val) - 1; |
1b3b981b | 5703 | coding->safe_charsets = SDATA (val); |
df7492f9 KH |
5704 | } |
5705 | CODING_ISO_FLAGS (coding) = flags; | |
e951386e KH |
5706 | CODING_ISO_CMP_STATUS (coding)->state = COMPOSING_NO; |
5707 | CODING_ISO_CMP_STATUS (coding)->method = COMPOSITION_NO; | |
5708 | CODING_ISO_EXTSEGMENT_LEN (coding) = 0; | |
5709 | CODING_ISO_EMBEDDED_UTF_8 (coding) = 0; | |
d46c5b12 | 5710 | } |
df7492f9 | 5711 | else if (EQ (coding_type, Qcharset)) |
d46c5b12 | 5712 | { |
df7492f9 KH |
5713 | coding->detector = detect_coding_charset; |
5714 | coding->decoder = decode_coding_charset; | |
5715 | coding->encoder = encode_coding_charset; | |
d46c5b12 | 5716 | coding->common_flags |
df7492f9 | 5717 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
d46c5b12 | 5718 | } |
df7492f9 | 5719 | else if (EQ (coding_type, Qutf_8)) |
d46c5b12 | 5720 | { |
a470d443 KH |
5721 | val = AREF (attrs, coding_attr_utf_bom); |
5722 | CODING_UTF_8_BOM (coding) = (CONSP (val) ? utf_detect_bom | |
5723 | : EQ (val, Qt) ? utf_with_bom | |
5724 | : utf_without_bom); | |
df7492f9 KH |
5725 | coding->detector = detect_coding_utf_8; |
5726 | coding->decoder = decode_coding_utf_8; | |
5727 | coding->encoder = encode_coding_utf_8; | |
5728 | coding->common_flags | |
5729 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); | |
a470d443 KH |
5730 | if (CODING_UTF_8_BOM (coding) == utf_detect_bom) |
5731 | coding->common_flags |= CODING_REQUIRE_DETECTION_MASK; | |
df7492f9 KH |
5732 | } |
5733 | else if (EQ (coding_type, Qutf_16)) | |
5734 | { | |
a470d443 KH |
5735 | val = AREF (attrs, coding_attr_utf_bom); |
5736 | CODING_UTF_16_BOM (coding) = (CONSP (val) ? utf_detect_bom | |
5737 | : EQ (val, Qt) ? utf_with_bom | |
5738 | : utf_without_bom); | |
df7492f9 | 5739 | val = AREF (attrs, coding_attr_utf_16_endian); |
b49a1807 | 5740 | CODING_UTF_16_ENDIAN (coding) = (EQ (val, Qbig) ? utf_16_big_endian |
df7492f9 | 5741 | : utf_16_little_endian); |
e19c3639 | 5742 | CODING_UTF_16_SURROGATE (coding) = 0; |
df7492f9 KH |
5743 | coding->detector = detect_coding_utf_16; |
5744 | coding->decoder = decode_coding_utf_16; | |
5745 | coding->encoder = encode_coding_utf_16; | |
5746 | coding->common_flags | |
5747 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); | |
a470d443 | 5748 | if (CODING_UTF_16_BOM (coding) == utf_detect_bom) |
b49a1807 | 5749 | coding->common_flags |= CODING_REQUIRE_DETECTION_MASK; |
d46c5b12 | 5750 | } |
df7492f9 | 5751 | else if (EQ (coding_type, Qccl)) |
4ed46869 | 5752 | { |
df7492f9 KH |
5753 | coding->detector = detect_coding_ccl; |
5754 | coding->decoder = decode_coding_ccl; | |
5755 | coding->encoder = encode_coding_ccl; | |
c952af22 | 5756 | coding->common_flags |
df7492f9 KH |
5757 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK |
5758 | | CODING_REQUIRE_FLUSHING_MASK); | |
5759 | } | |
5760 | else if (EQ (coding_type, Qemacs_mule)) | |
5761 | { | |
5762 | coding->detector = detect_coding_emacs_mule; | |
5763 | coding->decoder = decode_coding_emacs_mule; | |
5764 | coding->encoder = encode_coding_emacs_mule; | |
c952af22 | 5765 | coding->common_flags |
df7492f9 | 5766 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
e951386e | 5767 | coding->spec.emacs_mule.full_support = 1; |
df7492f9 KH |
5768 | if (! NILP (AREF (attrs, coding_attr_emacs_mule_full)) |
5769 | && ! EQ (CODING_ATTR_CHARSET_LIST (attrs), Vemacs_mule_charset_list)) | |
5770 | { | |
5771 | Lisp_Object tail, safe_charsets; | |
5772 | int max_charset_id = 0; | |
5773 | ||
5774 | for (tail = Vemacs_mule_charset_list; CONSP (tail); | |
5775 | tail = XCDR (tail)) | |
5776 | if (max_charset_id < XFASTINT (XCAR (tail))) | |
5777 | max_charset_id = XFASTINT (XCAR (tail)); | |
1b3b981b AS |
5778 | safe_charsets = make_uninit_string (max_charset_id + 1); |
5779 | memset (SDATA (safe_charsets), 255, max_charset_id + 1); | |
df7492f9 KH |
5780 | for (tail = Vemacs_mule_charset_list; CONSP (tail); |
5781 | tail = XCDR (tail)) | |
8f924df7 | 5782 | SSET (safe_charsets, XFASTINT (XCAR (tail)), 0); |
df7492f9 | 5783 | coding->max_charset_id = max_charset_id; |
1b3b981b | 5784 | coding->safe_charsets = SDATA (safe_charsets); |
e951386e | 5785 | coding->spec.emacs_mule.full_support = 1; |
df7492f9 | 5786 | } |
e951386e KH |
5787 | coding->spec.emacs_mule.cmp_status.state = COMPOSING_NO; |
5788 | coding->spec.emacs_mule.cmp_status.method = COMPOSITION_NO; | |
df7492f9 KH |
5789 | } |
5790 | else if (EQ (coding_type, Qshift_jis)) | |
5791 | { | |
5792 | coding->detector = detect_coding_sjis; | |
5793 | coding->decoder = decode_coding_sjis; | |
5794 | coding->encoder = encode_coding_sjis; | |
c952af22 | 5795 | coding->common_flags |
df7492f9 KH |
5796 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
5797 | } | |
5798 | else if (EQ (coding_type, Qbig5)) | |
5799 | { | |
5800 | coding->detector = detect_coding_big5; | |
5801 | coding->decoder = decode_coding_big5; | |
5802 | coding->encoder = encode_coding_big5; | |
c952af22 | 5803 | coding->common_flags |
df7492f9 KH |
5804 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
5805 | } | |
5806 | else /* EQ (coding_type, Qraw_text) */ | |
ec6d2bb8 | 5807 | { |
df7492f9 KH |
5808 | coding->detector = NULL; |
5809 | coding->decoder = decode_coding_raw_text; | |
5810 | coding->encoder = encode_coding_raw_text; | |
ea29edf2 KH |
5811 | if (! EQ (eol_type, Qunix)) |
5812 | { | |
5813 | coding->common_flags |= CODING_REQUIRE_DECODING_MASK; | |
5814 | if (! VECTORP (eol_type)) | |
5815 | coding->common_flags |= CODING_REQUIRE_ENCODING_MASK; | |
5816 | } | |
5817 | ||
4ed46869 | 5818 | } |
4ed46869 | 5819 | |
df7492f9 | 5820 | return; |
4ed46869 KH |
5821 | } |
5822 | ||
0ff61e78 KH |
5823 | /* Return a list of charsets supported by CODING. */ |
5824 | ||
5825 | Lisp_Object | |
971de7fb | 5826 | coding_charset_list (struct coding_system *coding) |
0ff61e78 | 5827 | { |
35befdaa | 5828 | Lisp_Object attrs, charset_list; |
0ff61e78 KH |
5829 | |
5830 | CODING_GET_INFO (coding, attrs, charset_list); | |
5831 | if (EQ (CODING_ATTR_TYPE (attrs), Qiso_2022)) | |
5832 | { | |
5833 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); | |
5834 | ||
5835 | if (flags & CODING_ISO_FLAG_FULL_SUPPORT) | |
5836 | charset_list = Viso_2022_charset_list; | |
5837 | } | |
5838 | else if (EQ (CODING_ATTR_TYPE (attrs), Qemacs_mule)) | |
5839 | { | |
5840 | charset_list = Vemacs_mule_charset_list; | |
5841 | } | |
5842 | return charset_list; | |
5843 | } | |
5844 | ||
5845 | ||
e9f91ece KH |
5846 | /* Return a list of charsets supported by CODING-SYSTEM. */ |
5847 | ||
5848 | Lisp_Object | |
971de7fb | 5849 | coding_system_charset_list (Lisp_Object coding_system) |
e9f91ece KH |
5850 | { |
5851 | int id; | |
5852 | Lisp_Object attrs, charset_list; | |
5853 | ||
5854 | CHECK_CODING_SYSTEM_GET_ID (coding_system, id); | |
5855 | attrs = CODING_ID_ATTRS (id); | |
5856 | ||
5857 | if (EQ (CODING_ATTR_TYPE (attrs), Qiso_2022)) | |
5858 | { | |
5859 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); | |
5860 | ||
5861 | if (flags & CODING_ISO_FLAG_FULL_SUPPORT) | |
5862 | charset_list = Viso_2022_charset_list; | |
5863 | else | |
5864 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
5865 | } | |
5866 | else if (EQ (CODING_ATTR_TYPE (attrs), Qemacs_mule)) | |
5867 | { | |
5868 | charset_list = Vemacs_mule_charset_list; | |
5869 | } | |
5870 | else | |
5871 | { | |
5872 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
5873 | } | |
5874 | return charset_list; | |
5875 | } | |
5876 | ||
5877 | ||
df7492f9 KH |
5878 | /* Return raw-text or one of its subsidiaries that has the same |
5879 | eol_type as CODING-SYSTEM. */ | |
ec6d2bb8 | 5880 | |
df7492f9 | 5881 | Lisp_Object |
971de7fb | 5882 | raw_text_coding_system (Lisp_Object coding_system) |
ec6d2bb8 | 5883 | { |
0be8721c | 5884 | Lisp_Object spec, attrs; |
df7492f9 | 5885 | Lisp_Object eol_type, raw_text_eol_type; |
ec6d2bb8 | 5886 | |
d3e4cb56 KH |
5887 | if (NILP (coding_system)) |
5888 | return Qraw_text; | |
df7492f9 KH |
5889 | spec = CODING_SYSTEM_SPEC (coding_system); |
5890 | attrs = AREF (spec, 0); | |
ec6d2bb8 | 5891 | |
df7492f9 KH |
5892 | if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text)) |
5893 | return coding_system; | |
ec6d2bb8 | 5894 | |
df7492f9 KH |
5895 | eol_type = AREF (spec, 2); |
5896 | if (VECTORP (eol_type)) | |
5897 | return Qraw_text; | |
5898 | spec = CODING_SYSTEM_SPEC (Qraw_text); | |
5899 | raw_text_eol_type = AREF (spec, 2); | |
5900 | return (EQ (eol_type, Qunix) ? AREF (raw_text_eol_type, 0) | |
5901 | : EQ (eol_type, Qdos) ? AREF (raw_text_eol_type, 1) | |
5902 | : AREF (raw_text_eol_type, 2)); | |
ec6d2bb8 KH |
5903 | } |
5904 | ||
54f78171 | 5905 | |
1911a33b KH |
5906 | /* If CODING_SYSTEM doesn't specify end-of-line format, return one of |
5907 | the subsidiary that has the same eol-spec as PARENT (if it is not | |
5908 | nil and specifies end-of-line format) or the system's setting | |
fcbcfb64 | 5909 | (system_eol_type). */ |
df7492f9 KH |
5910 | |
5911 | Lisp_Object | |
971de7fb | 5912 | coding_inherit_eol_type (Lisp_Object coding_system, Lisp_Object parent) |
54f78171 | 5913 | { |
3e139625 | 5914 | Lisp_Object spec, eol_type; |
54f78171 | 5915 | |
d3e4cb56 KH |
5916 | if (NILP (coding_system)) |
5917 | coding_system = Qraw_text; | |
df7492f9 | 5918 | spec = CODING_SYSTEM_SPEC (coding_system); |
df7492f9 | 5919 | eol_type = AREF (spec, 2); |
fcbcfb64 | 5920 | if (VECTORP (eol_type)) |
df7492f9 | 5921 | { |
df7492f9 KH |
5922 | Lisp_Object parent_eol_type; |
5923 | ||
fcbcfb64 KH |
5924 | if (! NILP (parent)) |
5925 | { | |
5926 | Lisp_Object parent_spec; | |
5927 | ||
4a015c45 | 5928 | parent_spec = CODING_SYSTEM_SPEC (parent); |
fcbcfb64 | 5929 | parent_eol_type = AREF (parent_spec, 2); |
1911a33b | 5930 | if (VECTORP (parent_eol_type)) |
4628bef1 | 5931 | parent_eol_type = system_eol_type; |
fcbcfb64 KH |
5932 | } |
5933 | else | |
5934 | parent_eol_type = system_eol_type; | |
df7492f9 KH |
5935 | if (EQ (parent_eol_type, Qunix)) |
5936 | coding_system = AREF (eol_type, 0); | |
5937 | else if (EQ (parent_eol_type, Qdos)) | |
5938 | coding_system = AREF (eol_type, 1); | |
5939 | else if (EQ (parent_eol_type, Qmac)) | |
5940 | coding_system = AREF (eol_type, 2); | |
54f78171 | 5941 | } |
df7492f9 | 5942 | return coding_system; |
54f78171 KH |
5943 | } |
5944 | ||
fcaf8878 KH |
5945 | |
5946 | /* Check if text-conversion and eol-conversion of CODING_SYSTEM are | |
5947 | decided for writing to a process. If not, complement them, and | |
5948 | return a new coding system. */ | |
5949 | ||
5950 | Lisp_Object | |
4628bef1 | 5951 | complement_process_encoding_system (Lisp_Object coding_system) |
fcaf8878 | 5952 | { |
5886ec9c KH |
5953 | Lisp_Object coding_base = Qnil, eol_base = Qnil; |
5954 | Lisp_Object spec, attrs; | |
93d50df8 | 5955 | int i; |
fcaf8878 | 5956 | |
93d50df8 | 5957 | for (i = 0; i < 3; i++) |
fcaf8878 | 5958 | { |
93d50df8 KH |
5959 | if (i == 1) |
5960 | coding_system = CDR_SAFE (Vdefault_process_coding_system); | |
5961 | else if (i == 2) | |
5962 | coding_system = preferred_coding_system (); | |
5963 | spec = CODING_SYSTEM_SPEC (coding_system); | |
5964 | if (NILP (spec)) | |
5965 | continue; | |
5966 | attrs = AREF (spec, 0); | |
5967 | if (NILP (coding_base) && ! EQ (CODING_ATTR_TYPE (attrs), Qundecided)) | |
5968 | coding_base = CODING_ATTR_BASE_NAME (attrs); | |
5969 | if (NILP (eol_base) && ! VECTORP (AREF (spec, 2))) | |
5970 | eol_base = coding_system; | |
5971 | if (! NILP (coding_base) && ! NILP (eol_base)) | |
5972 | break; | |
fcaf8878 | 5973 | } |
fcaf8878 | 5974 | |
93d50df8 KH |
5975 | if (i > 0) |
5976 | /* The original CODING_SYSTEM didn't specify text-conversion or | |
5977 | eol-conversion. Be sure that we return a fully complemented | |
5978 | coding system. */ | |
5979 | coding_system = coding_inherit_eol_type (coding_base, eol_base); | |
5980 | return coding_system; | |
fcaf8878 KH |
5981 | } |
5982 | ||
5983 | ||
4ed46869 KH |
5984 | /* Emacs has a mechanism to automatically detect a coding system if it |
5985 | is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But, | |
5986 | it's impossible to distinguish some coding systems accurately | |
5987 | because they use the same range of codes. So, at first, coding | |
5988 | systems are categorized into 7, those are: | |
5989 | ||
0ef69138 | 5990 | o coding-category-emacs-mule |
4ed46869 KH |
5991 | |
5992 | The category for a coding system which has the same code range | |
5993 | as Emacs' internal format. Assigned the coding-system (Lisp | |
0ef69138 | 5994 | symbol) `emacs-mule' by default. |
4ed46869 KH |
5995 | |
5996 | o coding-category-sjis | |
5997 | ||
5998 | The category for a coding system which has the same code range | |
5999 | as SJIS. Assigned the coding-system (Lisp | |
7717c392 | 6000 | symbol) `japanese-shift-jis' by default. |
4ed46869 KH |
6001 | |
6002 | o coding-category-iso-7 | |
6003 | ||
6004 | The category for a coding system which has the same code range | |
7717c392 | 6005 | as ISO2022 of 7-bit environment. This doesn't use any locking |
d46c5b12 KH |
6006 | shift and single shift functions. This can encode/decode all |
6007 | charsets. Assigned the coding-system (Lisp symbol) | |
6008 | `iso-2022-7bit' by default. | |
6009 | ||
6010 | o coding-category-iso-7-tight | |
6011 | ||
6012 | Same as coding-category-iso-7 except that this can | |
6013 | encode/decode only the specified charsets. | |
4ed46869 KH |
6014 | |
6015 | o coding-category-iso-8-1 | |
6016 | ||
6017 | The category for a coding system which has the same code range | |
6018 | as ISO2022 of 8-bit environment and graphic plane 1 used only | |
7717c392 KH |
6019 | for DIMENSION1 charset. This doesn't use any locking shift |
6020 | and single shift functions. Assigned the coding-system (Lisp | |
6021 | symbol) `iso-latin-1' by default. | |
4ed46869 KH |
6022 | |
6023 | o coding-category-iso-8-2 | |
6024 | ||
6025 | The category for a coding system which has the same code range | |
6026 | as ISO2022 of 8-bit environment and graphic plane 1 used only | |
7717c392 KH |
6027 | for DIMENSION2 charset. This doesn't use any locking shift |
6028 | and single shift functions. Assigned the coding-system (Lisp | |
6029 | symbol) `japanese-iso-8bit' by default. | |
4ed46869 | 6030 | |
7717c392 | 6031 | o coding-category-iso-7-else |
4ed46869 KH |
6032 | |
6033 | The category for a coding system which has the same code range | |
ad1746f5 | 6034 | as ISO2022 of 7-bit environment but uses locking shift or |
7717c392 KH |
6035 | single shift functions. Assigned the coding-system (Lisp |
6036 | symbol) `iso-2022-7bit-lock' by default. | |
6037 | ||
6038 | o coding-category-iso-8-else | |
6039 | ||
6040 | The category for a coding system which has the same code range | |
ad1746f5 | 6041 | as ISO2022 of 8-bit environment but uses locking shift or |
7717c392 KH |
6042 | single shift functions. Assigned the coding-system (Lisp |
6043 | symbol) `iso-2022-8bit-ss2' by default. | |
4ed46869 KH |
6044 | |
6045 | o coding-category-big5 | |
6046 | ||
6047 | The category for a coding system which has the same code range | |
6048 | as BIG5. Assigned the coding-system (Lisp symbol) | |
e0e989f6 | 6049 | `cn-big5' by default. |
4ed46869 | 6050 | |
fa42c37f KH |
6051 | o coding-category-utf-8 |
6052 | ||
6053 | The category for a coding system which has the same code range | |
6e76ae91 | 6054 | as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp |
fa42c37f KH |
6055 | symbol) `utf-8' by default. |
6056 | ||
6057 | o coding-category-utf-16-be | |
6058 | ||
6059 | The category for a coding system in which a text has an | |
6060 | Unicode signature (cf. Unicode Standard) in the order of BIG | |
6061 | endian at the head. Assigned the coding-system (Lisp symbol) | |
6062 | `utf-16-be' by default. | |
6063 | ||
6064 | o coding-category-utf-16-le | |
6065 | ||
6066 | The category for a coding system in which a text has an | |
6067 | Unicode signature (cf. Unicode Standard) in the order of | |
6068 | LITTLE endian at the head. Assigned the coding-system (Lisp | |
6069 | symbol) `utf-16-le' by default. | |
6070 | ||
1397dc18 KH |
6071 | o coding-category-ccl |
6072 | ||
6073 | The category for a coding system of which encoder/decoder is | |
6074 | written in CCL programs. The default value is nil, i.e., no | |
6075 | coding system is assigned. | |
6076 | ||
4ed46869 KH |
6077 | o coding-category-binary |
6078 | ||
6079 | The category for a coding system not categorized in any of the | |
6080 | above. Assigned the coding-system (Lisp symbol) | |
e0e989f6 | 6081 | `no-conversion' by default. |
4ed46869 KH |
6082 | |
6083 | Each of them is a Lisp symbol and the value is an actual | |
df7492f9 | 6084 | `coding-system's (this is also a Lisp symbol) assigned by a user. |
4ed46869 KH |
6085 | What Emacs does actually is to detect a category of coding system. |
6086 | Then, it uses a `coding-system' assigned to it. If Emacs can't | |
df7492f9 | 6087 | decide only one possible category, it selects a category of the |
4ed46869 KH |
6088 | highest priority. Priorities of categories are also specified by a |
6089 | user in a Lisp variable `coding-category-list'. | |
6090 | ||
6091 | */ | |
6092 | ||
df7492f9 KH |
6093 | #define EOL_SEEN_NONE 0 |
6094 | #define EOL_SEEN_LF 1 | |
6095 | #define EOL_SEEN_CR 2 | |
6096 | #define EOL_SEEN_CRLF 4 | |
66cfb530 | 6097 | |
ff0dacd7 KH |
6098 | /* Detect how end-of-line of a text of length SRC_BYTES pointed by |
6099 | SOURCE is encoded. If CATEGORY is one of | |
6100 | coding_category_utf_16_XXXX, assume that CR and LF are encoded by | |
6101 | two-byte, else they are encoded by one-byte. | |
6102 | ||
6103 | Return one of EOL_SEEN_XXX. */ | |
4ed46869 | 6104 | |
bc4bc72a | 6105 | #define MAX_EOL_CHECK_COUNT 3 |
d46c5b12 KH |
6106 | |
6107 | static int | |
cf84bb53 JB |
6108 | detect_eol (const unsigned char *source, EMACS_INT src_bytes, |
6109 | enum coding_category category) | |
4ed46869 | 6110 | { |
f6cbaf43 | 6111 | const unsigned char *src = source, *src_end = src + src_bytes; |
4ed46869 | 6112 | unsigned char c; |
df7492f9 KH |
6113 | int total = 0; |
6114 | int eol_seen = EOL_SEEN_NONE; | |
4ed46869 | 6115 | |
89528eb3 | 6116 | if ((1 << category) & CATEGORY_MASK_UTF_16) |
4ed46869 | 6117 | { |
df7492f9 | 6118 | int msb, lsb; |
fa42c37f | 6119 | |
89528eb3 KH |
6120 | msb = category == (coding_category_utf_16_le |
6121 | | coding_category_utf_16_le_nosig); | |
df7492f9 | 6122 | lsb = 1 - msb; |
fa42c37f | 6123 | |
df7492f9 | 6124 | while (src + 1 < src_end) |
fa42c37f | 6125 | { |
df7492f9 KH |
6126 | c = src[lsb]; |
6127 | if (src[msb] == 0 && (c == '\n' || c == '\r')) | |
fa42c37f | 6128 | { |
df7492f9 KH |
6129 | int this_eol; |
6130 | ||
6131 | if (c == '\n') | |
6132 | this_eol = EOL_SEEN_LF; | |
6133 | else if (src + 3 >= src_end | |
6134 | || src[msb + 2] != 0 | |
6135 | || src[lsb + 2] != '\n') | |
6136 | this_eol = EOL_SEEN_CR; | |
fa42c37f | 6137 | else |
75f4f1ac EZ |
6138 | { |
6139 | this_eol = EOL_SEEN_CRLF; | |
6140 | src += 2; | |
6141 | } | |
df7492f9 KH |
6142 | |
6143 | if (eol_seen == EOL_SEEN_NONE) | |
6144 | /* This is the first end-of-line. */ | |
6145 | eol_seen = this_eol; | |
6146 | else if (eol_seen != this_eol) | |
fa42c37f | 6147 | { |
75f4f1ac EZ |
6148 | /* The found type is different from what found before. |
6149 | Allow for stray ^M characters in DOS EOL files. */ | |
ef1b0ba7 SM |
6150 | if ((eol_seen == EOL_SEEN_CR && this_eol == EOL_SEEN_CRLF) |
6151 | || (eol_seen == EOL_SEEN_CRLF | |
6152 | && this_eol == EOL_SEEN_CR)) | |
75f4f1ac EZ |
6153 | eol_seen = EOL_SEEN_CRLF; |
6154 | else | |
6155 | { | |
6156 | eol_seen = EOL_SEEN_LF; | |
6157 | break; | |
6158 | } | |
fa42c37f | 6159 | } |
df7492f9 KH |
6160 | if (++total == MAX_EOL_CHECK_COUNT) |
6161 | break; | |
fa42c37f | 6162 | } |
df7492f9 | 6163 | src += 2; |
fa42c37f | 6164 | } |
bcf26d6a | 6165 | } |
d46c5b12 | 6166 | else |
ef1b0ba7 SM |
6167 | while (src < src_end) |
6168 | { | |
6169 | c = *src++; | |
6170 | if (c == '\n' || c == '\r') | |
6171 | { | |
6172 | int this_eol; | |
d46c5b12 | 6173 | |
ef1b0ba7 SM |
6174 | if (c == '\n') |
6175 | this_eol = EOL_SEEN_LF; | |
6176 | else if (src >= src_end || *src != '\n') | |
6177 | this_eol = EOL_SEEN_CR; | |
6178 | else | |
6179 | this_eol = EOL_SEEN_CRLF, src++; | |
d46c5b12 | 6180 | |
ef1b0ba7 SM |
6181 | if (eol_seen == EOL_SEEN_NONE) |
6182 | /* This is the first end-of-line. */ | |
6183 | eol_seen = this_eol; | |
6184 | else if (eol_seen != this_eol) | |
6185 | { | |
6186 | /* The found type is different from what found before. | |
6187 | Allow for stray ^M characters in DOS EOL files. */ | |
6188 | if ((eol_seen == EOL_SEEN_CR && this_eol == EOL_SEEN_CRLF) | |
6189 | || (eol_seen == EOL_SEEN_CRLF && this_eol == EOL_SEEN_CR)) | |
6190 | eol_seen = EOL_SEEN_CRLF; | |
6191 | else | |
6192 | { | |
6193 | eol_seen = EOL_SEEN_LF; | |
6194 | break; | |
6195 | } | |
6196 | } | |
6197 | if (++total == MAX_EOL_CHECK_COUNT) | |
6198 | break; | |
6199 | } | |
6200 | } | |
df7492f9 | 6201 | return eol_seen; |
73be902c KH |
6202 | } |
6203 | ||
df7492f9 | 6204 | |
24a73b0a | 6205 | static Lisp_Object |
971de7fb | 6206 | adjust_coding_eol_type (struct coding_system *coding, int eol_seen) |
73be902c | 6207 | { |
0be8721c | 6208 | Lisp_Object eol_type; |
8f924df7 | 6209 | |
df7492f9 KH |
6210 | eol_type = CODING_ID_EOL_TYPE (coding->id); |
6211 | if (eol_seen & EOL_SEEN_LF) | |
24a73b0a KH |
6212 | { |
6213 | coding->id = CODING_SYSTEM_ID (AREF (eol_type, 0)); | |
6214 | eol_type = Qunix; | |
6215 | } | |
6f197c07 | 6216 | else if (eol_seen & EOL_SEEN_CRLF) |
24a73b0a KH |
6217 | { |
6218 | coding->id = CODING_SYSTEM_ID (AREF (eol_type, 1)); | |
6219 | eol_type = Qdos; | |
6220 | } | |
6f197c07 | 6221 | else if (eol_seen & EOL_SEEN_CR) |
24a73b0a KH |
6222 | { |
6223 | coding->id = CODING_SYSTEM_ID (AREF (eol_type, 2)); | |
6224 | eol_type = Qmac; | |
6225 | } | |
6226 | return eol_type; | |
d46c5b12 | 6227 | } |
4ed46869 | 6228 | |
df7492f9 KH |
6229 | /* Detect how a text specified in CODING is encoded. If a coding |
6230 | system is detected, update fields of CODING by the detected coding | |
6231 | system. */ | |
0a28aafb | 6232 | |
df7492f9 | 6233 | void |
971de7fb | 6234 | detect_coding (struct coding_system *coding) |
d46c5b12 | 6235 | { |
8f924df7 | 6236 | const unsigned char *src, *src_end; |
73cce38d | 6237 | int saved_mode = coding->mode; |
d46c5b12 | 6238 | |
df7492f9 KH |
6239 | coding->consumed = coding->consumed_char = 0; |
6240 | coding->produced = coding->produced_char = 0; | |
6241 | coding_set_source (coding); | |
1c3478b0 | 6242 | |
df7492f9 | 6243 | src_end = coding->source + coding->src_bytes; |
c0e16b14 | 6244 | coding->head_ascii = 0; |
1c3478b0 | 6245 | |
df7492f9 KH |
6246 | /* If we have not yet decided the text encoding type, detect it |
6247 | now. */ | |
6248 | if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding->id)), Qundecided)) | |
b73bfc1c | 6249 | { |
df7492f9 | 6250 | int c, i; |
6cb21a4f | 6251 | struct coding_detection_info detect_info; |
2f3cbb32 | 6252 | int null_byte_found = 0, eight_bit_found = 0; |
df7492f9 | 6253 | |
6cb21a4f | 6254 | detect_info.checked = detect_info.found = detect_info.rejected = 0; |
2f3cbb32 | 6255 | for (src = coding->source; src < src_end; src++) |
d46c5b12 | 6256 | { |
df7492f9 | 6257 | c = *src; |
6cb21a4f | 6258 | if (c & 0x80) |
6cb21a4f | 6259 | { |
2f3cbb32 | 6260 | eight_bit_found = 1; |
2f3cbb32 KH |
6261 | if (null_byte_found) |
6262 | break; | |
6263 | } | |
6264 | else if (c < 0x20) | |
6265 | { | |
6266 | if ((c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) | |
6267 | && ! inhibit_iso_escape_detection | |
6268 | && ! detect_info.checked) | |
6cb21a4f | 6269 | { |
2f3cbb32 KH |
6270 | if (detect_coding_iso_2022 (coding, &detect_info)) |
6271 | { | |
6272 | /* We have scanned the whole data. */ | |
6273 | if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE)) | |
c0e16b14 KH |
6274 | { |
6275 | /* We didn't find an 8-bit code. We may | |
6276 | have found a null-byte, but it's very | |
ce5b453a | 6277 | rare that a binary file conforms to |
c0e16b14 KH |
6278 | ISO-2022. */ |
6279 | src = src_end; | |
6280 | coding->head_ascii = src - coding->source; | |
6281 | } | |
6282 | detect_info.rejected |= ~CATEGORY_MASK_ISO_ESCAPE; | |
2f3cbb32 KH |
6283 | break; |
6284 | } | |
6285 | } | |
97b1b294 | 6286 | else if (! c && !inhibit_null_byte_detection) |
2f3cbb32 KH |
6287 | { |
6288 | null_byte_found = 1; | |
6289 | if (eight_bit_found) | |
6290 | break; | |
6cb21a4f | 6291 | } |
c006c0c8 KH |
6292 | if (! eight_bit_found) |
6293 | coding->head_ascii++; | |
6cb21a4f | 6294 | } |
c006c0c8 | 6295 | else if (! eight_bit_found) |
c0e16b14 | 6296 | coding->head_ascii++; |
d46c5b12 | 6297 | } |
df7492f9 | 6298 | |
2f3cbb32 KH |
6299 | if (null_byte_found || eight_bit_found |
6300 | || coding->head_ascii < coding->src_bytes | |
6cb21a4f | 6301 | || detect_info.found) |
d46c5b12 | 6302 | { |
ff0dacd7 KH |
6303 | enum coding_category category; |
6304 | struct coding_system *this; | |
df7492f9 | 6305 | |
6cb21a4f KH |
6306 | if (coding->head_ascii == coding->src_bytes) |
6307 | /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */ | |
6308 | for (i = 0; i < coding_category_raw_text; i++) | |
6309 | { | |
6310 | category = coding_priorities[i]; | |
6311 | this = coding_categories + category; | |
6312 | if (detect_info.found & (1 << category)) | |
24a73b0a | 6313 | break; |
6cb21a4f KH |
6314 | } |
6315 | else | |
2f3cbb32 KH |
6316 | { |
6317 | if (null_byte_found) | |
ff0dacd7 | 6318 | { |
2f3cbb32 KH |
6319 | detect_info.checked |= ~CATEGORY_MASK_UTF_16; |
6320 | detect_info.rejected |= ~CATEGORY_MASK_UTF_16; | |
ff0dacd7 | 6321 | } |
2f3cbb32 KH |
6322 | for (i = 0; i < coding_category_raw_text; i++) |
6323 | { | |
6324 | category = coding_priorities[i]; | |
6325 | this = coding_categories + category; | |
6326 | if (this->id < 0) | |
6327 | { | |
6328 | /* No coding system of this category is defined. */ | |
6329 | detect_info.rejected |= (1 << category); | |
6330 | } | |
6331 | else if (category >= coding_category_raw_text) | |
6332 | continue; | |
6333 | else if (detect_info.checked & (1 << category)) | |
6334 | { | |
6335 | if (detect_info.found & (1 << category)) | |
6336 | break; | |
6337 | } | |
6338 | else if ((*(this->detector)) (coding, &detect_info) | |
6339 | && detect_info.found & (1 << category)) | |
6340 | { | |
6341 | if (category == coding_category_utf_16_auto) | |
6342 | { | |
6343 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
6344 | category = coding_category_utf_16_le; | |
6345 | else | |
6346 | category = coding_category_utf_16_be; | |
6347 | } | |
6348 | break; | |
6349 | } | |
6350 | } | |
2f3cbb32 | 6351 | } |
c0e16b14 KH |
6352 | |
6353 | if (i < coding_category_raw_text) | |
6354 | setup_coding_system (CODING_ID_NAME (this->id), coding); | |
6355 | else if (null_byte_found) | |
6356 | setup_coding_system (Qno_conversion, coding); | |
6357 | else if ((detect_info.rejected & CATEGORY_MASK_ANY) | |
6358 | == CATEGORY_MASK_ANY) | |
6359 | setup_coding_system (Qraw_text, coding); | |
6360 | else if (detect_info.rejected) | |
6361 | for (i = 0; i < coding_category_raw_text; i++) | |
6362 | if (! (detect_info.rejected & (1 << coding_priorities[i]))) | |
6363 | { | |
6364 | this = coding_categories + coding_priorities[i]; | |
6365 | setup_coding_system (CODING_ID_NAME (this->id), coding); | |
6366 | break; | |
6367 | } | |
d46c5b12 | 6368 | } |
b73bfc1c | 6369 | } |
a470d443 KH |
6370 | else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding->id))) |
6371 | == coding_category_utf_8_auto) | |
6372 | { | |
6373 | Lisp_Object coding_systems; | |
6374 | struct coding_detection_info detect_info; | |
6375 | ||
6376 | coding_systems | |
6377 | = AREF (CODING_ID_ATTRS (coding->id), coding_attr_utf_bom); | |
6378 | detect_info.found = detect_info.rejected = 0; | |
6379 | coding->head_ascii = 0; | |
6380 | if (CONSP (coding_systems) | |
6381 | && detect_coding_utf_8 (coding, &detect_info)) | |
6382 | { | |
6383 | if (detect_info.found & CATEGORY_MASK_UTF_8_SIG) | |
6384 | setup_coding_system (XCAR (coding_systems), coding); | |
6385 | else | |
6386 | setup_coding_system (XCDR (coding_systems), coding); | |
6387 | } | |
6388 | } | |
24a73b0a KH |
6389 | else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding->id))) |
6390 | == coding_category_utf_16_auto) | |
b49a1807 KH |
6391 | { |
6392 | Lisp_Object coding_systems; | |
6393 | struct coding_detection_info detect_info; | |
6394 | ||
6395 | coding_systems | |
a470d443 | 6396 | = AREF (CODING_ID_ATTRS (coding->id), coding_attr_utf_bom); |
b49a1807 | 6397 | detect_info.found = detect_info.rejected = 0; |
a470d443 | 6398 | coding->head_ascii = 0; |
b49a1807 | 6399 | if (CONSP (coding_systems) |
24a73b0a | 6400 | && detect_coding_utf_16 (coding, &detect_info)) |
b49a1807 KH |
6401 | { |
6402 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
6403 | setup_coding_system (XCAR (coding_systems), coding); | |
24a73b0a | 6404 | else if (detect_info.found & CATEGORY_MASK_UTF_16_BE) |
b49a1807 KH |
6405 | setup_coding_system (XCDR (coding_systems), coding); |
6406 | } | |
6407 | } | |
73cce38d | 6408 | coding->mode = saved_mode; |
4ed46869 | 6409 | } |
4ed46869 | 6410 | |
d46c5b12 | 6411 | |
aaaf0b1e | 6412 | static void |
971de7fb | 6413 | decode_eol (struct coding_system *coding) |
aaaf0b1e | 6414 | { |
24a73b0a KH |
6415 | Lisp_Object eol_type; |
6416 | unsigned char *p, *pbeg, *pend; | |
3ed051d4 | 6417 | |
24a73b0a | 6418 | eol_type = CODING_ID_EOL_TYPE (coding->id); |
0a9564cb | 6419 | if (EQ (eol_type, Qunix) || inhibit_eol_conversion) |
24a73b0a KH |
6420 | return; |
6421 | ||
6422 | if (NILP (coding->dst_object)) | |
6423 | pbeg = coding->destination; | |
6424 | else | |
6425 | pbeg = BYTE_POS_ADDR (coding->dst_pos_byte); | |
6426 | pend = pbeg + coding->produced; | |
6427 | ||
6428 | if (VECTORP (eol_type)) | |
aaaf0b1e | 6429 | { |
df7492f9 | 6430 | int eol_seen = EOL_SEEN_NONE; |
4ed46869 | 6431 | |
24a73b0a | 6432 | for (p = pbeg; p < pend; p++) |
aaaf0b1e | 6433 | { |
df7492f9 KH |
6434 | if (*p == '\n') |
6435 | eol_seen |= EOL_SEEN_LF; | |
6436 | else if (*p == '\r') | |
aaaf0b1e | 6437 | { |
df7492f9 | 6438 | if (p + 1 < pend && *(p + 1) == '\n') |
aaaf0b1e | 6439 | { |
df7492f9 KH |
6440 | eol_seen |= EOL_SEEN_CRLF; |
6441 | p++; | |
aaaf0b1e | 6442 | } |
aaaf0b1e | 6443 | else |
df7492f9 | 6444 | eol_seen |= EOL_SEEN_CR; |
aaaf0b1e | 6445 | } |
aaaf0b1e | 6446 | } |
75f4f1ac EZ |
6447 | /* Handle DOS-style EOLs in a file with stray ^M characters. */ |
6448 | if ((eol_seen & EOL_SEEN_CRLF) != 0 | |
6449 | && (eol_seen & EOL_SEEN_CR) != 0 | |
6450 | && (eol_seen & EOL_SEEN_LF) == 0) | |
6451 | eol_seen = EOL_SEEN_CRLF; | |
6452 | else if (eol_seen != EOL_SEEN_NONE | |
24a73b0a KH |
6453 | && eol_seen != EOL_SEEN_LF |
6454 | && eol_seen != EOL_SEEN_CRLF | |
6455 | && eol_seen != EOL_SEEN_CR) | |
6456 | eol_seen = EOL_SEEN_LF; | |
df7492f9 | 6457 | if (eol_seen != EOL_SEEN_NONE) |
24a73b0a | 6458 | eol_type = adjust_coding_eol_type (coding, eol_seen); |
aaaf0b1e | 6459 | } |
d46c5b12 | 6460 | |
24a73b0a | 6461 | if (EQ (eol_type, Qmac)) |
27901516 | 6462 | { |
24a73b0a | 6463 | for (p = pbeg; p < pend; p++) |
df7492f9 KH |
6464 | if (*p == '\r') |
6465 | *p = '\n'; | |
4ed46869 | 6466 | } |
24a73b0a | 6467 | else if (EQ (eol_type, Qdos)) |
df7492f9 | 6468 | { |
24a73b0a | 6469 | int n = 0; |
b73bfc1c | 6470 | |
24a73b0a KH |
6471 | if (NILP (coding->dst_object)) |
6472 | { | |
4347441b KH |
6473 | /* Start deleting '\r' from the tail to minimize the memory |
6474 | movement. */ | |
24a73b0a KH |
6475 | for (p = pend - 2; p >= pbeg; p--) |
6476 | if (*p == '\r') | |
6477 | { | |
72af86bd | 6478 | memmove (p, p + 1, pend-- - p - 1); |
24a73b0a KH |
6479 | n++; |
6480 | } | |
6481 | } | |
6482 | else | |
6483 | { | |
4347441b KH |
6484 | int pos_byte = coding->dst_pos_byte; |
6485 | int pos = coding->dst_pos; | |
6486 | int pos_end = pos + coding->produced_char - 1; | |
6487 | ||
6488 | while (pos < pos_end) | |
6489 | { | |
6490 | p = BYTE_POS_ADDR (pos_byte); | |
6491 | if (*p == '\r' && p[1] == '\n') | |
6492 | { | |
6493 | del_range_2 (pos, pos_byte, pos + 1, pos_byte + 1, 0); | |
6494 | n++; | |
6495 | pos_end--; | |
6496 | } | |
6497 | pos++; | |
69b8522d KH |
6498 | if (coding->dst_multibyte) |
6499 | pos_byte += BYTES_BY_CHAR_HEAD (*p); | |
6500 | else | |
6501 | pos_byte++; | |
4347441b | 6502 | } |
24a73b0a KH |
6503 | } |
6504 | coding->produced -= n; | |
6505 | coding->produced_char -= n; | |
aaaf0b1e | 6506 | } |
4ed46869 KH |
6507 | } |
6508 | ||
7d64c6ad | 6509 | |
a6f87d34 KH |
6510 | /* Return a translation table (or list of them) from coding system |
6511 | attribute vector ATTRS for encoding (ENCODEP is nonzero) or | |
6512 | decoding (ENCODEP is zero). */ | |
7d64c6ad | 6513 | |
e6a54062 | 6514 | static Lisp_Object |
971de7fb | 6515 | get_translation_table (Lisp_Object attrs, int encodep, int *max_lookup) |
7d64c6ad KH |
6516 | { |
6517 | Lisp_Object standard, translation_table; | |
09ee6fdd | 6518 | Lisp_Object val; |
7d64c6ad | 6519 | |
4bed5909 CY |
6520 | if (NILP (Venable_character_translation)) |
6521 | { | |
6522 | if (max_lookup) | |
6523 | *max_lookup = 0; | |
6524 | return Qnil; | |
6525 | } | |
7d64c6ad KH |
6526 | if (encodep) |
6527 | translation_table = CODING_ATTR_ENCODE_TBL (attrs), | |
6528 | standard = Vstandard_translation_table_for_encode; | |
6529 | else | |
6530 | translation_table = CODING_ATTR_DECODE_TBL (attrs), | |
6531 | standard = Vstandard_translation_table_for_decode; | |
7d64c6ad | 6532 | if (NILP (translation_table)) |
09ee6fdd KH |
6533 | translation_table = standard; |
6534 | else | |
a6f87d34 | 6535 | { |
09ee6fdd KH |
6536 | if (SYMBOLP (translation_table)) |
6537 | translation_table = Fget (translation_table, Qtranslation_table); | |
6538 | else if (CONSP (translation_table)) | |
6539 | { | |
6540 | translation_table = Fcopy_sequence (translation_table); | |
6541 | for (val = translation_table; CONSP (val); val = XCDR (val)) | |
6542 | if (SYMBOLP (XCAR (val))) | |
6543 | XSETCAR (val, Fget (XCAR (val), Qtranslation_table)); | |
6544 | } | |
6545 | if (CHAR_TABLE_P (standard)) | |
6546 | { | |
6547 | if (CONSP (translation_table)) | |
6548 | translation_table = nconc2 (translation_table, | |
6549 | Fcons (standard, Qnil)); | |
6550 | else | |
6551 | translation_table = Fcons (translation_table, | |
6552 | Fcons (standard, Qnil)); | |
6553 | } | |
a6f87d34 | 6554 | } |
2170c8f0 KH |
6555 | |
6556 | if (max_lookup) | |
09ee6fdd | 6557 | { |
2170c8f0 KH |
6558 | *max_lookup = 1; |
6559 | if (CHAR_TABLE_P (translation_table) | |
6560 | && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table)) > 1) | |
6561 | { | |
6562 | val = XCHAR_TABLE (translation_table)->extras[1]; | |
6563 | if (NATNUMP (val) && *max_lookup < XFASTINT (val)) | |
6564 | *max_lookup = XFASTINT (val); | |
6565 | } | |
6566 | else if (CONSP (translation_table)) | |
6567 | { | |
2735d060 | 6568 | Lisp_Object tail; |
09ee6fdd | 6569 | |
2170c8f0 KH |
6570 | for (tail = translation_table; CONSP (tail); tail = XCDR (tail)) |
6571 | if (CHAR_TABLE_P (XCAR (tail)) | |
6572 | && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail))) > 1) | |
6573 | { | |
2735d060 PE |
6574 | Lisp_Object tailval = XCHAR_TABLE (XCAR (tail))->extras[1]; |
6575 | if (NATNUMP (tailval) && *max_lookup < XFASTINT (tailval)) | |
6576 | *max_lookup = XFASTINT (tailval); | |
2170c8f0 KH |
6577 | } |
6578 | } | |
a6f87d34 | 6579 | } |
7d64c6ad KH |
6580 | return translation_table; |
6581 | } | |
6582 | ||
09ee6fdd KH |
6583 | #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \ |
6584 | do { \ | |
6585 | trans = Qnil; \ | |
6586 | if (CHAR_TABLE_P (table)) \ | |
6587 | { \ | |
6588 | trans = CHAR_TABLE_REF (table, c); \ | |
6589 | if (CHARACTERP (trans)) \ | |
6590 | c = XFASTINT (trans), trans = Qnil; \ | |
6591 | } \ | |
6592 | else if (CONSP (table)) \ | |
6593 | { \ | |
6594 | Lisp_Object tail; \ | |
6595 | \ | |
6596 | for (tail = table; CONSP (tail); tail = XCDR (tail)) \ | |
6597 | if (CHAR_TABLE_P (XCAR (tail))) \ | |
6598 | { \ | |
6599 | trans = CHAR_TABLE_REF (XCAR (tail), c); \ | |
6600 | if (CHARACTERP (trans)) \ | |
6601 | c = XFASTINT (trans), trans = Qnil; \ | |
6602 | else if (! NILP (trans)) \ | |
6603 | break; \ | |
6604 | } \ | |
6605 | } \ | |
e6a54062 KH |
6606 | } while (0) |
6607 | ||
7d64c6ad | 6608 | |
e951386e KH |
6609 | /* Return a translation of character(s) at BUF according to TRANS. |
6610 | TRANS is TO-CHAR or ((FROM . TO) ...) where | |
6611 | FROM = [FROM-CHAR ...], TO is TO-CHAR or [TO-CHAR ...]. | |
6612 | The return value is TO-CHAR or ([FROM-CHAR ...] . TO) if a | |
6613 | translation is found, and Qnil if not found.. | |
6614 | If BUF is too short to lookup characters in FROM, return Qt. */ | |
6615 | ||
69a80ea3 | 6616 | static Lisp_Object |
971de7fb | 6617 | get_translation (Lisp_Object trans, int *buf, int *buf_end) |
69a80ea3 | 6618 | { |
e951386e KH |
6619 | |
6620 | if (INTEGERP (trans)) | |
6621 | return trans; | |
6622 | for (; CONSP (trans); trans = XCDR (trans)) | |
69a80ea3 | 6623 | { |
e951386e KH |
6624 | Lisp_Object val = XCAR (trans); |
6625 | Lisp_Object from = XCAR (val); | |
6626 | int len = ASIZE (from); | |
6627 | int i; | |
69a80ea3 | 6628 | |
e951386e | 6629 | for (i = 0; i < len; i++) |
69a80ea3 | 6630 | { |
e951386e KH |
6631 | if (buf + i == buf_end) |
6632 | return Qt; | |
6633 | if (XINT (AREF (from, i)) != buf[i]) | |
6634 | break; | |
69a80ea3 | 6635 | } |
e951386e KH |
6636 | if (i == len) |
6637 | return val; | |
69a80ea3 | 6638 | } |
e951386e | 6639 | return Qnil; |
69a80ea3 KH |
6640 | } |
6641 | ||
6642 | ||
d46c5b12 | 6643 | static int |
cf84bb53 JB |
6644 | produce_chars (struct coding_system *coding, Lisp_Object translation_table, |
6645 | int last_block) | |
4ed46869 | 6646 | { |
df7492f9 KH |
6647 | unsigned char *dst = coding->destination + coding->produced; |
6648 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
119852e7 KH |
6649 | EMACS_INT produced; |
6650 | EMACS_INT produced_chars = 0; | |
69a80ea3 | 6651 | int carryover = 0; |
4ed46869 | 6652 | |
df7492f9 | 6653 | if (! coding->chars_at_source) |
4ed46869 | 6654 | { |
119852e7 | 6655 | /* Source characters are in coding->charbuf. */ |
fba4576f AS |
6656 | int *buf = coding->charbuf; |
6657 | int *buf_end = buf + coding->charbuf_used; | |
4ed46869 | 6658 | |
db274c7a KH |
6659 | if (EQ (coding->src_object, coding->dst_object)) |
6660 | { | |
6661 | coding_set_source (coding); | |
6662 | dst_end = ((unsigned char *) coding->source) + coding->consumed; | |
6663 | } | |
4ed46869 | 6664 | |
df7492f9 | 6665 | while (buf < buf_end) |
4ed46869 | 6666 | { |
69a80ea3 | 6667 | int c = *buf, i; |
bc4bc72a | 6668 | |
df7492f9 KH |
6669 | if (c >= 0) |
6670 | { | |
69a80ea3 KH |
6671 | int from_nchars = 1, to_nchars = 1; |
6672 | Lisp_Object trans = Qnil; | |
6673 | ||
09ee6fdd | 6674 | LOOKUP_TRANSLATION_TABLE (translation_table, c, trans); |
e6a54062 | 6675 | if (! NILP (trans)) |
69a80ea3 | 6676 | { |
e951386e KH |
6677 | trans = get_translation (trans, buf, buf_end); |
6678 | if (INTEGERP (trans)) | |
6679 | c = XINT (trans); | |
6680 | else if (CONSP (trans)) | |
6681 | { | |
6682 | from_nchars = ASIZE (XCAR (trans)); | |
6683 | trans = XCDR (trans); | |
6684 | if (INTEGERP (trans)) | |
6685 | c = XINT (trans); | |
6686 | else | |
6687 | { | |
6688 | to_nchars = ASIZE (trans); | |
6689 | c = XINT (AREF (trans, 0)); | |
6690 | } | |
6691 | } | |
6692 | else if (EQ (trans, Qt) && ! last_block) | |
69a80ea3 | 6693 | break; |
69a80ea3 KH |
6694 | } |
6695 | ||
6696 | if (dst + MAX_MULTIBYTE_LENGTH * to_nchars > dst_end) | |
6697 | { | |
6698 | dst = alloc_destination (coding, | |
6699 | buf_end - buf | |
6700 | + MAX_MULTIBYTE_LENGTH * to_nchars, | |
6701 | dst); | |
db274c7a KH |
6702 | if (EQ (coding->src_object, coding->dst_object)) |
6703 | { | |
6704 | coding_set_source (coding); | |
e951386e KH |
6705 | dst_end = (((unsigned char *) coding->source) |
6706 | + coding->consumed); | |
db274c7a KH |
6707 | } |
6708 | else | |
6709 | dst_end = coding->destination + coding->dst_bytes; | |
69a80ea3 KH |
6710 | } |
6711 | ||
433f7f87 | 6712 | for (i = 0; i < to_nchars; i++) |
69a80ea3 | 6713 | { |
433f7f87 KH |
6714 | if (i > 0) |
6715 | c = XINT (AREF (trans, i)); | |
69a80ea3 KH |
6716 | if (coding->dst_multibyte |
6717 | || ! CHAR_BYTE8_P (c)) | |
db274c7a | 6718 | CHAR_STRING_ADVANCE_NO_UNIFY (c, dst); |
69a80ea3 KH |
6719 | else |
6720 | *dst++ = CHAR_TO_BYTE8 (c); | |
6721 | } | |
6722 | produced_chars += to_nchars; | |
e951386e | 6723 | buf += from_nchars; |
d46c5b12 | 6724 | } |
df7492f9 | 6725 | else |
69a80ea3 KH |
6726 | /* This is an annotation datum. (-C) is the length. */ |
6727 | buf += -c; | |
4ed46869 | 6728 | } |
69a80ea3 | 6729 | carryover = buf_end - buf; |
4ed46869 | 6730 | } |
fa42c37f | 6731 | else |
fa42c37f | 6732 | { |
119852e7 | 6733 | /* Source characters are at coding->source. */ |
8f924df7 | 6734 | const unsigned char *src = coding->source; |
119852e7 | 6735 | const unsigned char *src_end = src + coding->consumed; |
4ed46869 | 6736 | |
db274c7a KH |
6737 | if (EQ (coding->dst_object, coding->src_object)) |
6738 | dst_end = (unsigned char *) src; | |
df7492f9 | 6739 | if (coding->src_multibyte != coding->dst_multibyte) |
fa42c37f | 6740 | { |
df7492f9 | 6741 | if (coding->src_multibyte) |
fa42c37f | 6742 | { |
71c81426 | 6743 | int multibytep = 1; |
4533845d | 6744 | EMACS_INT consumed_chars = 0; |
d46c5b12 | 6745 | |
df7492f9 KH |
6746 | while (1) |
6747 | { | |
8f924df7 | 6748 | const unsigned char *src_base = src; |
df7492f9 | 6749 | int c; |
b73bfc1c | 6750 | |
df7492f9 | 6751 | ONE_MORE_BYTE (c); |
119852e7 | 6752 | if (dst == dst_end) |
df7492f9 | 6753 | { |
119852e7 KH |
6754 | if (EQ (coding->src_object, coding->dst_object)) |
6755 | dst_end = (unsigned char *) src; | |
6756 | if (dst == dst_end) | |
df7492f9 | 6757 | { |
119852e7 KH |
6758 | EMACS_INT offset = src - coding->source; |
6759 | ||
6760 | dst = alloc_destination (coding, src_end - src + 1, | |
6761 | dst); | |
6762 | dst_end = coding->destination + coding->dst_bytes; | |
6763 | coding_set_source (coding); | |
6764 | src = coding->source + offset; | |
6765 | src_end = coding->source + coding->src_bytes; | |
db274c7a KH |
6766 | if (EQ (coding->src_object, coding->dst_object)) |
6767 | dst_end = (unsigned char *) src; | |
df7492f9 | 6768 | } |
df7492f9 KH |
6769 | } |
6770 | *dst++ = c; | |
6771 | produced_chars++; | |
6772 | } | |
6773 | no_more_source: | |
6774 | ; | |
fa42c37f KH |
6775 | } |
6776 | else | |
df7492f9 KH |
6777 | while (src < src_end) |
6778 | { | |
71c81426 | 6779 | int multibytep = 1; |
df7492f9 | 6780 | int c = *src++; |
b73bfc1c | 6781 | |
df7492f9 KH |
6782 | if (dst >= dst_end - 1) |
6783 | { | |
2c78b7e1 | 6784 | if (EQ (coding->src_object, coding->dst_object)) |
8f924df7 | 6785 | dst_end = (unsigned char *) src; |
2c78b7e1 KH |
6786 | if (dst >= dst_end - 1) |
6787 | { | |
119852e7 | 6788 | EMACS_INT offset = src - coding->source; |
db274c7a | 6789 | EMACS_INT more_bytes; |
119852e7 | 6790 | |
db274c7a KH |
6791 | if (EQ (coding->src_object, coding->dst_object)) |
6792 | more_bytes = ((src_end - src) / 2) + 2; | |
6793 | else | |
6794 | more_bytes = src_end - src + 2; | |
6795 | dst = alloc_destination (coding, more_bytes, dst); | |
2c78b7e1 KH |
6796 | dst_end = coding->destination + coding->dst_bytes; |
6797 | coding_set_source (coding); | |
119852e7 | 6798 | src = coding->source + offset; |
2c78b7e1 | 6799 | src_end = coding->source + coding->src_bytes; |
db274c7a KH |
6800 | if (EQ (coding->src_object, coding->dst_object)) |
6801 | dst_end = (unsigned char *) src; | |
2c78b7e1 | 6802 | } |
df7492f9 KH |
6803 | } |
6804 | EMIT_ONE_BYTE (c); | |
6805 | } | |
d46c5b12 | 6806 | } |
df7492f9 KH |
6807 | else |
6808 | { | |
6809 | if (!EQ (coding->src_object, coding->dst_object)) | |
fa42c37f | 6810 | { |
119852e7 | 6811 | EMACS_INT require = coding->src_bytes - coding->dst_bytes; |
4ed46869 | 6812 | |
df7492f9 | 6813 | if (require > 0) |
fa42c37f | 6814 | { |
df7492f9 KH |
6815 | EMACS_INT offset = src - coding->source; |
6816 | ||
6817 | dst = alloc_destination (coding, require, dst); | |
6818 | coding_set_source (coding); | |
6819 | src = coding->source + offset; | |
6820 | src_end = coding->source + coding->src_bytes; | |
fa42c37f KH |
6821 | } |
6822 | } | |
119852e7 | 6823 | produced_chars = coding->consumed_char; |
df7492f9 | 6824 | while (src < src_end) |
14daee73 | 6825 | *dst++ = *src++; |
fa42c37f KH |
6826 | } |
6827 | } | |
6828 | ||
df7492f9 | 6829 | produced = dst - (coding->destination + coding->produced); |
284201e4 | 6830 | if (BUFFERP (coding->dst_object) && produced_chars > 0) |
df7492f9 KH |
6831 | insert_from_gap (produced_chars, produced); |
6832 | coding->produced += produced; | |
6833 | coding->produced_char += produced_chars; | |
69a80ea3 | 6834 | return carryover; |
fa42c37f KH |
6835 | } |
6836 | ||
ff0dacd7 KH |
6837 | /* Compose text in CODING->object according to the annotation data at |
6838 | CHARBUF. CHARBUF is an array: | |
e951386e | 6839 | [ -LENGTH ANNOTATION_MASK NCHARS NBYTES METHOD [ COMPONENTS... ] ] |
df7492f9 | 6840 | */ |
4ed46869 | 6841 | |
df7492f9 | 6842 | static INLINE void |
971de7fb | 6843 | produce_composition (struct coding_system *coding, int *charbuf, EMACS_INT pos) |
4ed46869 | 6844 | { |
df7492f9 | 6845 | int len; |
69a80ea3 | 6846 | EMACS_INT to; |
df7492f9 | 6847 | enum composition_method method; |
df7492f9 | 6848 | Lisp_Object components; |
fa42c37f | 6849 | |
e951386e | 6850 | len = -charbuf[0] - MAX_ANNOTATION_LENGTH; |
69a80ea3 | 6851 | to = pos + charbuf[2]; |
e951386e | 6852 | method = (enum composition_method) (charbuf[4]); |
d46c5b12 | 6853 | |
df7492f9 KH |
6854 | if (method == COMPOSITION_RELATIVE) |
6855 | components = Qnil; | |
e951386e | 6856 | else |
d46c5b12 | 6857 | { |
df7492f9 | 6858 | Lisp_Object args[MAX_COMPOSITION_COMPONENTS * 2 - 1]; |
e951386e | 6859 | int i, j; |
b73bfc1c | 6860 | |
e951386e KH |
6861 | if (method == COMPOSITION_WITH_RULE) |
6862 | len = charbuf[2] * 3 - 2; | |
6863 | charbuf += MAX_ANNOTATION_LENGTH; | |
6864 | /* charbuf = [ CHRA ... CHAR] or [ CHAR -2 RULE ... CHAR ] */ | |
6865 | for (i = j = 0; i < len && charbuf[i] != -1; i++, j++) | |
9ffd559c | 6866 | { |
e951386e KH |
6867 | if (charbuf[i] >= 0) |
6868 | args[j] = make_number (charbuf[i]); | |
6869 | else | |
6870 | { | |
6871 | i++; | |
6872 | args[j] = make_number (charbuf[i] % 0x100); | |
6873 | } | |
9ffd559c | 6874 | } |
e951386e | 6875 | components = (i == j ? Fstring (j, args) : Fvector (j, args)); |
d46c5b12 | 6876 | } |
69a80ea3 | 6877 | compose_text (pos, to, components, Qnil, coding->dst_object); |
d46c5b12 KH |
6878 | } |
6879 | ||
d46c5b12 | 6880 | |
ff0dacd7 KH |
6881 | /* Put `charset' property on text in CODING->object according to |
6882 | the annotation data at CHARBUF. CHARBUF is an array: | |
69a80ea3 | 6883 | [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ] |
ff0dacd7 | 6884 | */ |
d46c5b12 | 6885 | |
ff0dacd7 | 6886 | static INLINE void |
971de7fb | 6887 | produce_charset (struct coding_system *coding, int *charbuf, EMACS_INT pos) |
d46c5b12 | 6888 | { |
69a80ea3 KH |
6889 | EMACS_INT from = pos - charbuf[2]; |
6890 | struct charset *charset = CHARSET_FROM_ID (charbuf[3]); | |
b73bfc1c | 6891 | |
69a80ea3 | 6892 | Fput_text_property (make_number (from), make_number (pos), |
ff0dacd7 KH |
6893 | Qcharset, CHARSET_NAME (charset), |
6894 | coding->dst_object); | |
d46c5b12 KH |
6895 | } |
6896 | ||
d46c5b12 | 6897 | |
df7492f9 KH |
6898 | #define CHARBUF_SIZE 0x4000 |
6899 | ||
6900 | #define ALLOC_CONVERSION_WORK_AREA(coding) \ | |
6901 | do { \ | |
8510724d | 6902 | int size = CHARBUF_SIZE; \ |
df7492f9 KH |
6903 | \ |
6904 | coding->charbuf = NULL; \ | |
6905 | while (size > 1024) \ | |
6906 | { \ | |
6907 | coding->charbuf = (int *) alloca (sizeof (int) * size); \ | |
6908 | if (coding->charbuf) \ | |
6909 | break; \ | |
6910 | size >>= 1; \ | |
6911 | } \ | |
6912 | if (! coding->charbuf) \ | |
6913 | { \ | |
065e3595 | 6914 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \ |
df7492f9 KH |
6915 | return coding->result; \ |
6916 | } \ | |
6917 | coding->charbuf_size = size; \ | |
6918 | } while (0) | |
4ed46869 | 6919 | |
d46c5b12 KH |
6920 | |
6921 | static void | |
971de7fb | 6922 | produce_annotation (struct coding_system *coding, EMACS_INT pos) |
d46c5b12 | 6923 | { |
df7492f9 KH |
6924 | int *charbuf = coding->charbuf; |
6925 | int *charbuf_end = charbuf + coding->charbuf_used; | |
d46c5b12 | 6926 | |
ff0dacd7 KH |
6927 | if (NILP (coding->dst_object)) |
6928 | return; | |
d46c5b12 | 6929 | |
df7492f9 | 6930 | while (charbuf < charbuf_end) |
a84f1519 | 6931 | { |
df7492f9 | 6932 | if (*charbuf >= 0) |
e951386e | 6933 | pos++, charbuf++; |
d46c5b12 | 6934 | else |
d46c5b12 | 6935 | { |
df7492f9 | 6936 | int len = -*charbuf; |
e951386e KH |
6937 | |
6938 | if (len > 2) | |
6939 | switch (charbuf[1]) | |
6940 | { | |
6941 | case CODING_ANNOTATE_COMPOSITION_MASK: | |
6942 | produce_composition (coding, charbuf, pos); | |
6943 | break; | |
6944 | case CODING_ANNOTATE_CHARSET_MASK: | |
6945 | produce_charset (coding, charbuf, pos); | |
6946 | break; | |
6947 | } | |
df7492f9 | 6948 | charbuf += len; |
d46c5b12 | 6949 | } |
a84f1519 | 6950 | } |
d46c5b12 KH |
6951 | } |
6952 | ||
df7492f9 KH |
6953 | /* Decode the data at CODING->src_object into CODING->dst_object. |
6954 | CODING->src_object is a buffer, a string, or nil. | |
6955 | CODING->dst_object is a buffer. | |
d46c5b12 | 6956 | |
df7492f9 KH |
6957 | If CODING->src_object is a buffer, it must be the current buffer. |
6958 | In this case, if CODING->src_pos is positive, it is a position of | |
6959 | the source text in the buffer, otherwise, the source text is in the | |
6960 | gap area of the buffer, and CODING->src_pos specifies the offset of | |
6961 | the text from GPT (which must be the same as PT). If this is the | |
6962 | same buffer as CODING->dst_object, CODING->src_pos must be | |
6963 | negative. | |
d46c5b12 | 6964 | |
b6828792 | 6965 | If CODING->src_object is a string, CODING->src_pos is an index to |
df7492f9 | 6966 | that string. |
d46c5b12 | 6967 | |
df7492f9 KH |
6968 | If CODING->src_object is nil, CODING->source must already point to |
6969 | the non-relocatable memory area. In this case, CODING->src_pos is | |
6970 | an offset from CODING->source. | |
73be902c | 6971 | |
df7492f9 KH |
6972 | The decoded data is inserted at the current point of the buffer |
6973 | CODING->dst_object. | |
6974 | */ | |
d46c5b12 | 6975 | |
df7492f9 | 6976 | static int |
971de7fb | 6977 | decode_coding (struct coding_system *coding) |
d46c5b12 | 6978 | { |
df7492f9 | 6979 | Lisp_Object attrs; |
24a73b0a | 6980 | Lisp_Object undo_list; |
7d64c6ad | 6981 | Lisp_Object translation_table; |
d0396581 | 6982 | struct ccl_spec cclspec; |
69a80ea3 KH |
6983 | int carryover; |
6984 | int i; | |
d46c5b12 | 6985 | |
df7492f9 KH |
6986 | if (BUFFERP (coding->src_object) |
6987 | && coding->src_pos > 0 | |
6988 | && coding->src_pos < GPT | |
6989 | && coding->src_pos + coding->src_chars > GPT) | |
6990 | move_gap_both (coding->src_pos, coding->src_pos_byte); | |
d46c5b12 | 6991 | |
24a73b0a | 6992 | undo_list = Qt; |
df7492f9 | 6993 | if (BUFFERP (coding->dst_object)) |
1c3478b0 | 6994 | { |
df7492f9 KH |
6995 | if (current_buffer != XBUFFER (coding->dst_object)) |
6996 | set_buffer_internal (XBUFFER (coding->dst_object)); | |
6997 | if (GPT != PT) | |
6998 | move_gap_both (PT, PT_BYTE); | |
4b4deea2 TT |
6999 | undo_list = BVAR (current_buffer, undo_list); |
7000 | BVAR (current_buffer, undo_list) = Qt; | |
1c3478b0 KH |
7001 | } |
7002 | ||
df7492f9 KH |
7003 | coding->consumed = coding->consumed_char = 0; |
7004 | coding->produced = coding->produced_char = 0; | |
7005 | coding->chars_at_source = 0; | |
065e3595 | 7006 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 | 7007 | coding->errors = 0; |
1c3478b0 | 7008 | |
df7492f9 KH |
7009 | ALLOC_CONVERSION_WORK_AREA (coding); |
7010 | ||
7011 | attrs = CODING_ID_ATTRS (coding->id); | |
2170c8f0 | 7012 | translation_table = get_translation_table (attrs, 0, NULL); |
df7492f9 | 7013 | |
69a80ea3 | 7014 | carryover = 0; |
d0396581 KH |
7015 | if (coding->decoder == decode_coding_ccl) |
7016 | { | |
7017 | coding->spec.ccl = &cclspec; | |
7018 | setup_ccl_program (&cclspec.ccl, CODING_CCL_DECODER (coding)); | |
7019 | } | |
df7492f9 | 7020 | do |
b73bfc1c | 7021 | { |
69a80ea3 KH |
7022 | EMACS_INT pos = coding->dst_pos + coding->produced_char; |
7023 | ||
df7492f9 KH |
7024 | coding_set_source (coding); |
7025 | coding->annotated = 0; | |
69a80ea3 | 7026 | coding->charbuf_used = carryover; |
df7492f9 | 7027 | (*(coding->decoder)) (coding); |
df7492f9 | 7028 | coding_set_destination (coding); |
69a80ea3 | 7029 | carryover = produce_chars (coding, translation_table, 0); |
df7492f9 | 7030 | if (coding->annotated) |
69a80ea3 KH |
7031 | produce_annotation (coding, pos); |
7032 | for (i = 0; i < carryover; i++) | |
7033 | coding->charbuf[i] | |
7034 | = coding->charbuf[coding->charbuf_used - carryover + i]; | |
d46c5b12 | 7035 | } |
d0396581 KH |
7036 | while (coding->result == CODING_RESULT_INSUFFICIENT_DST |
7037 | || (coding->consumed < coding->src_bytes | |
7038 | && (coding->result == CODING_RESULT_SUCCESS | |
7039 | || coding->result == CODING_RESULT_INVALID_SRC))); | |
d46c5b12 | 7040 | |
69a80ea3 KH |
7041 | if (carryover > 0) |
7042 | { | |
7043 | coding_set_destination (coding); | |
7044 | coding->charbuf_used = carryover; | |
7045 | produce_chars (coding, translation_table, 1); | |
7046 | } | |
7047 | ||
df7492f9 KH |
7048 | coding->carryover_bytes = 0; |
7049 | if (coding->consumed < coding->src_bytes) | |
d46c5b12 | 7050 | { |
df7492f9 | 7051 | int nbytes = coding->src_bytes - coding->consumed; |
8f924df7 | 7052 | const unsigned char *src; |
df7492f9 KH |
7053 | |
7054 | coding_set_source (coding); | |
7055 | coding_set_destination (coding); | |
7056 | src = coding->source + coding->consumed; | |
7057 | ||
7058 | if (coding->mode & CODING_MODE_LAST_BLOCK) | |
1c3478b0 | 7059 | { |
df7492f9 KH |
7060 | /* Flush out unprocessed data as binary chars. We are sure |
7061 | that the number of data is less than the size of | |
7062 | coding->charbuf. */ | |
065e3595 | 7063 | coding->charbuf_used = 0; |
b2dab6c8 JR |
7064 | coding->chars_at_source = 0; |
7065 | ||
df7492f9 | 7066 | while (nbytes-- > 0) |
1c3478b0 | 7067 | { |
df7492f9 | 7068 | int c = *src++; |
98725083 | 7069 | |
1c91457d KH |
7070 | if (c & 0x80) |
7071 | c = BYTE8_TO_CHAR (c); | |
7072 | coding->charbuf[coding->charbuf_used++] = c; | |
1c3478b0 | 7073 | } |
f6cbaf43 | 7074 | produce_chars (coding, Qnil, 1); |
d46c5b12 | 7075 | } |
d46c5b12 | 7076 | else |
df7492f9 KH |
7077 | { |
7078 | /* Record unprocessed bytes in coding->carryover. We are | |
7079 | sure that the number of data is less than the size of | |
7080 | coding->carryover. */ | |
7081 | unsigned char *p = coding->carryover; | |
7082 | ||
f289d375 KH |
7083 | if (nbytes > sizeof coding->carryover) |
7084 | nbytes = sizeof coding->carryover; | |
df7492f9 KH |
7085 | coding->carryover_bytes = nbytes; |
7086 | while (nbytes-- > 0) | |
7087 | *p++ = *src++; | |
1c3478b0 | 7088 | } |
df7492f9 | 7089 | coding->consumed = coding->src_bytes; |
b73bfc1c | 7090 | } |
69f76525 | 7091 | |
0a9564cb EZ |
7092 | if (! EQ (CODING_ID_EOL_TYPE (coding->id), Qunix) |
7093 | && !inhibit_eol_conversion) | |
4347441b | 7094 | decode_eol (coding); |
24a73b0a KH |
7095 | if (BUFFERP (coding->dst_object)) |
7096 | { | |
4b4deea2 | 7097 | BVAR (current_buffer, undo_list) = undo_list; |
24a73b0a KH |
7098 | record_insert (coding->dst_pos, coding->produced_char); |
7099 | } | |
73be902c | 7100 | return coding->result; |
4ed46869 KH |
7101 | } |
7102 | ||
aaaf0b1e | 7103 | |
e1c23804 | 7104 | /* Extract an annotation datum from a composition starting at POS and |
ff0dacd7 KH |
7105 | ending before LIMIT of CODING->src_object (buffer or string), store |
7106 | the data in BUF, set *STOP to a starting position of the next | |
7107 | composition (if any) or to LIMIT, and return the address of the | |
7108 | next element of BUF. | |
7109 | ||
7110 | If such an annotation is not found, set *STOP to a starting | |
7111 | position of a composition after POS (if any) or to LIMIT, and | |
7112 | return BUF. */ | |
7113 | ||
7114 | static INLINE int * | |
cf84bb53 JB |
7115 | handle_composition_annotation (EMACS_INT pos, EMACS_INT limit, |
7116 | struct coding_system *coding, int *buf, | |
7117 | EMACS_INT *stop) | |
aaaf0b1e | 7118 | { |
ff0dacd7 KH |
7119 | EMACS_INT start, end; |
7120 | Lisp_Object prop; | |
aaaf0b1e | 7121 | |
ff0dacd7 KH |
7122 | if (! find_composition (pos, limit, &start, &end, &prop, coding->src_object) |
7123 | || end > limit) | |
7124 | *stop = limit; | |
7125 | else if (start > pos) | |
7126 | *stop = start; | |
7127 | else | |
aaaf0b1e | 7128 | { |
ff0dacd7 | 7129 | if (start == pos) |
aaaf0b1e | 7130 | { |
ff0dacd7 KH |
7131 | /* We found a composition. Store the corresponding |
7132 | annotation data in BUF. */ | |
7133 | int *head = buf; | |
7134 | enum composition_method method = COMPOSITION_METHOD (prop); | |
7135 | int nchars = COMPOSITION_LENGTH (prop); | |
7136 | ||
e951386e | 7137 | ADD_COMPOSITION_DATA (buf, nchars, 0, method); |
ff0dacd7 | 7138 | if (method != COMPOSITION_RELATIVE) |
aaaf0b1e | 7139 | { |
ff0dacd7 KH |
7140 | Lisp_Object components; |
7141 | int len, i, i_byte; | |
7142 | ||
7143 | components = COMPOSITION_COMPONENTS (prop); | |
7144 | if (VECTORP (components)) | |
aaaf0b1e | 7145 | { |
ff0dacd7 KH |
7146 | len = XVECTOR (components)->size; |
7147 | for (i = 0; i < len; i++) | |
7148 | *buf++ = XINT (AREF (components, i)); | |
aaaf0b1e | 7149 | } |
ff0dacd7 | 7150 | else if (STRINGP (components)) |
aaaf0b1e | 7151 | { |
8f924df7 | 7152 | len = SCHARS (components); |
ff0dacd7 KH |
7153 | i = i_byte = 0; |
7154 | while (i < len) | |
7155 | { | |
7156 | FETCH_STRING_CHAR_ADVANCE (*buf, components, i, i_byte); | |
7157 | buf++; | |
7158 | } | |
7159 | } | |
7160 | else if (INTEGERP (components)) | |
7161 | { | |
7162 | len = 1; | |
7163 | *buf++ = XINT (components); | |
7164 | } | |
7165 | else if (CONSP (components)) | |
7166 | { | |
7167 | for (len = 0; CONSP (components); | |
7168 | len++, components = XCDR (components)) | |
7169 | *buf++ = XINT (XCAR (components)); | |
aaaf0b1e | 7170 | } |
aaaf0b1e | 7171 | else |
ff0dacd7 KH |
7172 | abort (); |
7173 | *head -= len; | |
aaaf0b1e | 7174 | } |
aaaf0b1e | 7175 | } |
ff0dacd7 KH |
7176 | |
7177 | if (find_composition (end, limit, &start, &end, &prop, | |
7178 | coding->src_object) | |
7179 | && end <= limit) | |
7180 | *stop = start; | |
7181 | else | |
7182 | *stop = limit; | |
aaaf0b1e | 7183 | } |
ff0dacd7 KH |
7184 | return buf; |
7185 | } | |
7186 | ||
7187 | ||
e1c23804 | 7188 | /* Extract an annotation datum from a text property `charset' at POS of |
ff0dacd7 KH |
7189 | CODING->src_object (buffer of string), store the data in BUF, set |
7190 | *STOP to the position where the value of `charset' property changes | |
7191 | (limiting by LIMIT), and return the address of the next element of | |
7192 | BUF. | |
7193 | ||
7194 | If the property value is nil, set *STOP to the position where the | |
7195 | property value is non-nil (limiting by LIMIT), and return BUF. */ | |
7196 | ||
7197 | static INLINE int * | |
cf84bb53 JB |
7198 | handle_charset_annotation (EMACS_INT pos, EMACS_INT limit, |
7199 | struct coding_system *coding, int *buf, | |
7200 | EMACS_INT *stop) | |
ff0dacd7 KH |
7201 | { |
7202 | Lisp_Object val, next; | |
7203 | int id; | |
7204 | ||
7205 | val = Fget_text_property (make_number (pos), Qcharset, coding->src_object); | |
7206 | if (! NILP (val) && CHARSETP (val)) | |
7207 | id = XINT (CHARSET_SYMBOL_ID (val)); | |
7208 | else | |
7209 | id = -1; | |
69a80ea3 | 7210 | ADD_CHARSET_DATA (buf, 0, id); |
ff0dacd7 KH |
7211 | next = Fnext_single_property_change (make_number (pos), Qcharset, |
7212 | coding->src_object, | |
7213 | make_number (limit)); | |
7214 | *stop = XINT (next); | |
7215 | return buf; | |
7216 | } | |
7217 | ||
7218 | ||
df7492f9 | 7219 | static void |
cf84bb53 JB |
7220 | consume_chars (struct coding_system *coding, Lisp_Object translation_table, |
7221 | int max_lookup) | |
df7492f9 KH |
7222 | { |
7223 | int *buf = coding->charbuf; | |
ff0dacd7 | 7224 | int *buf_end = coding->charbuf + coding->charbuf_size; |
7c78e542 | 7225 | const unsigned char *src = coding->source + coding->consumed; |
4776e638 | 7226 | const unsigned char *src_end = coding->source + coding->src_bytes; |
ff0dacd7 KH |
7227 | EMACS_INT pos = coding->src_pos + coding->consumed_char; |
7228 | EMACS_INT end_pos = coding->src_pos + coding->src_chars; | |
df7492f9 KH |
7229 | int multibytep = coding->src_multibyte; |
7230 | Lisp_Object eol_type; | |
7231 | int c; | |
ff0dacd7 | 7232 | EMACS_INT stop, stop_composition, stop_charset; |
09ee6fdd | 7233 | int *lookup_buf = NULL; |
433f7f87 KH |
7234 | |
7235 | if (! NILP (translation_table)) | |
09ee6fdd | 7236 | lookup_buf = alloca (sizeof (int) * max_lookup); |
88993dfd | 7237 | |
0a9564cb | 7238 | eol_type = inhibit_eol_conversion ? Qunix : CODING_ID_EOL_TYPE (coding->id); |
df7492f9 KH |
7239 | if (VECTORP (eol_type)) |
7240 | eol_type = Qunix; | |
88993dfd | 7241 | |
df7492f9 KH |
7242 | /* Note: composition handling is not yet implemented. */ |
7243 | coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK; | |
ec6d2bb8 | 7244 | |
0b5670c9 KH |
7245 | if (NILP (coding->src_object)) |
7246 | stop = stop_composition = stop_charset = end_pos; | |
ff0dacd7 | 7247 | else |
0b5670c9 KH |
7248 | { |
7249 | if (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK) | |
7250 | stop = stop_composition = pos; | |
7251 | else | |
7252 | stop = stop_composition = end_pos; | |
7253 | if (coding->common_flags & CODING_ANNOTATE_CHARSET_MASK) | |
7254 | stop = stop_charset = pos; | |
7255 | else | |
7256 | stop_charset = end_pos; | |
7257 | } | |
ec6d2bb8 | 7258 | |
24a73b0a | 7259 | /* Compensate for CRLF and conversion. */ |
ff0dacd7 | 7260 | buf_end -= 1 + MAX_ANNOTATION_LENGTH; |
df7492f9 | 7261 | while (buf < buf_end) |
aaaf0b1e | 7262 | { |
433f7f87 KH |
7263 | Lisp_Object trans; |
7264 | ||
df7492f9 | 7265 | if (pos == stop) |
ec6d2bb8 | 7266 | { |
df7492f9 KH |
7267 | if (pos == end_pos) |
7268 | break; | |
ff0dacd7 KH |
7269 | if (pos == stop_composition) |
7270 | buf = handle_composition_annotation (pos, end_pos, coding, | |
7271 | buf, &stop_composition); | |
7272 | if (pos == stop_charset) | |
7273 | buf = handle_charset_annotation (pos, end_pos, coding, | |
7274 | buf, &stop_charset); | |
7275 | stop = (stop_composition < stop_charset | |
7276 | ? stop_composition : stop_charset); | |
df7492f9 KH |
7277 | } |
7278 | ||
7279 | if (! multibytep) | |
4776e638 | 7280 | { |
d3e4cb56 | 7281 | EMACS_INT bytes; |
aaaf0b1e | 7282 | |
4d1e6632 KH |
7283 | if (coding->encoder == encode_coding_raw_text |
7284 | || coding->encoder == encode_coding_ccl) | |
ea29edf2 KH |
7285 | c = *src++, pos++; |
7286 | else if ((bytes = MULTIBYTE_LENGTH (src, src_end)) > 0) | |
db274c7a | 7287 | c = STRING_CHAR_ADVANCE_NO_UNIFY (src), pos += bytes; |
4776e638 | 7288 | else |
f03caae0 | 7289 | c = BYTE8_TO_CHAR (*src), src++, pos++; |
4776e638 | 7290 | } |
df7492f9 | 7291 | else |
db274c7a | 7292 | c = STRING_CHAR_ADVANCE_NO_UNIFY (src), pos++; |
df7492f9 KH |
7293 | if ((c == '\r') && (coding->mode & CODING_MODE_SELECTIVE_DISPLAY)) |
7294 | c = '\n'; | |
7295 | if (! EQ (eol_type, Qunix)) | |
aaaf0b1e | 7296 | { |
df7492f9 | 7297 | if (c == '\n') |
aaaf0b1e | 7298 | { |
df7492f9 KH |
7299 | if (EQ (eol_type, Qdos)) |
7300 | *buf++ = '\r'; | |
7301 | else | |
7302 | c = '\r'; | |
aaaf0b1e KH |
7303 | } |
7304 | } | |
433f7f87 | 7305 | |
e6a54062 | 7306 | trans = Qnil; |
09ee6fdd | 7307 | LOOKUP_TRANSLATION_TABLE (translation_table, c, trans); |
e6a54062 | 7308 | if (NILP (trans)) |
433f7f87 KH |
7309 | *buf++ = c; |
7310 | else | |
7311 | { | |
7312 | int from_nchars = 1, to_nchars = 1; | |
7313 | int *lookup_buf_end; | |
7314 | const unsigned char *p = src; | |
7315 | int i; | |
7316 | ||
7317 | lookup_buf[0] = c; | |
7318 | for (i = 1; i < max_lookup && p < src_end; i++) | |
7319 | lookup_buf[i] = STRING_CHAR_ADVANCE (p); | |
7320 | lookup_buf_end = lookup_buf + i; | |
e951386e KH |
7321 | trans = get_translation (trans, lookup_buf, lookup_buf_end); |
7322 | if (INTEGERP (trans)) | |
7323 | c = XINT (trans); | |
7324 | else if (CONSP (trans)) | |
7325 | { | |
7326 | from_nchars = ASIZE (XCAR (trans)); | |
7327 | trans = XCDR (trans); | |
7328 | if (INTEGERP (trans)) | |
7329 | c = XINT (trans); | |
7330 | else | |
7331 | { | |
7332 | to_nchars = ASIZE (trans); | |
7333 | if (buf + to_nchars > buf_end) | |
7334 | break; | |
7335 | c = XINT (AREF (trans, 0)); | |
7336 | } | |
7337 | } | |
7338 | else | |
433f7f87 | 7339 | break; |
e951386e | 7340 | *buf++ = c; |
433f7f87 KH |
7341 | for (i = 1; i < to_nchars; i++) |
7342 | *buf++ = XINT (AREF (trans, i)); | |
7343 | for (i = 1; i < from_nchars; i++, pos++) | |
7344 | src += MULTIBYTE_LENGTH_NO_CHECK (src); | |
7345 | } | |
aaaf0b1e | 7346 | } |
ec6d2bb8 | 7347 | |
df7492f9 KH |
7348 | coding->consumed = src - coding->source; |
7349 | coding->consumed_char = pos - coding->src_pos; | |
7350 | coding->charbuf_used = buf - coding->charbuf; | |
7351 | coding->chars_at_source = 0; | |
aaaf0b1e KH |
7352 | } |
7353 | ||
4ed46869 | 7354 | |
df7492f9 KH |
7355 | /* Encode the text at CODING->src_object into CODING->dst_object. |
7356 | CODING->src_object is a buffer or a string. | |
7357 | CODING->dst_object is a buffer or nil. | |
7358 | ||
7359 | If CODING->src_object is a buffer, it must be the current buffer. | |
7360 | In this case, if CODING->src_pos is positive, it is a position of | |
7361 | the source text in the buffer, otherwise. the source text is in the | |
7362 | gap area of the buffer, and coding->src_pos specifies the offset of | |
7363 | the text from GPT (which must be the same as PT). If this is the | |
7364 | same buffer as CODING->dst_object, CODING->src_pos must be | |
7365 | negative and CODING should not have `pre-write-conversion'. | |
7366 | ||
7367 | If CODING->src_object is a string, CODING should not have | |
7368 | `pre-write-conversion'. | |
7369 | ||
7370 | If CODING->dst_object is a buffer, the encoded data is inserted at | |
7371 | the current point of that buffer. | |
7372 | ||
7373 | If CODING->dst_object is nil, the encoded data is placed at the | |
7374 | memory area specified by CODING->destination. */ | |
7375 | ||
7376 | static int | |
971de7fb | 7377 | encode_coding (struct coding_system *coding) |
4ed46869 | 7378 | { |
df7492f9 | 7379 | Lisp_Object attrs; |
7d64c6ad | 7380 | Lisp_Object translation_table; |
09ee6fdd | 7381 | int max_lookup; |
fb608df3 | 7382 | struct ccl_spec cclspec; |
9861e777 | 7383 | |
df7492f9 | 7384 | attrs = CODING_ID_ATTRS (coding->id); |
ea29edf2 KH |
7385 | if (coding->encoder == encode_coding_raw_text) |
7386 | translation_table = Qnil, max_lookup = 0; | |
7387 | else | |
7388 | translation_table = get_translation_table (attrs, 1, &max_lookup); | |
4ed46869 | 7389 | |
df7492f9 | 7390 | if (BUFFERP (coding->dst_object)) |
8844fa83 | 7391 | { |
df7492f9 KH |
7392 | set_buffer_internal (XBUFFER (coding->dst_object)); |
7393 | coding->dst_multibyte | |
4b4deea2 | 7394 | = ! NILP (BVAR (current_buffer, enable_multibyte_characters)); |
8844fa83 | 7395 | } |
4ed46869 | 7396 | |
b73bfc1c | 7397 | coding->consumed = coding->consumed_char = 0; |
df7492f9 | 7398 | coding->produced = coding->produced_char = 0; |
065e3595 | 7399 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
b73bfc1c | 7400 | coding->errors = 0; |
b73bfc1c | 7401 | |
df7492f9 | 7402 | ALLOC_CONVERSION_WORK_AREA (coding); |
4ed46869 | 7403 | |
fb608df3 KH |
7404 | if (coding->encoder == encode_coding_ccl) |
7405 | { | |
7406 | coding->spec.ccl = &cclspec; | |
7407 | setup_ccl_program (&cclspec.ccl, CODING_CCL_ENCODER (coding)); | |
7408 | } | |
df7492f9 KH |
7409 | do { |
7410 | coding_set_source (coding); | |
09ee6fdd | 7411 | consume_chars (coding, translation_table, max_lookup); |
df7492f9 KH |
7412 | coding_set_destination (coding); |
7413 | (*(coding->encoder)) (coding); | |
7414 | } while (coding->consumed_char < coding->src_chars); | |
7415 | ||
284201e4 | 7416 | if (BUFFERP (coding->dst_object) && coding->produced_char > 0) |
df7492f9 KH |
7417 | insert_from_gap (coding->produced_char, coding->produced); |
7418 | ||
7419 | return (coding->result); | |
ec6d2bb8 KH |
7420 | } |
7421 | ||
fb88bf2d | 7422 | |
24a73b0a KH |
7423 | /* Name (or base name) of work buffer for code conversion. */ |
7424 | static Lisp_Object Vcode_conversion_workbuf_name; | |
d46c5b12 | 7425 | |
24a73b0a KH |
7426 | /* A working buffer used by the top level conversion. Once it is |
7427 | created, it is never destroyed. It has the name | |
7428 | Vcode_conversion_workbuf_name. The other working buffers are | |
7429 | destroyed after the use is finished, and their names are modified | |
7430 | versions of Vcode_conversion_workbuf_name. */ | |
7431 | static Lisp_Object Vcode_conversion_reused_workbuf; | |
b73bfc1c | 7432 | |
24a73b0a KH |
7433 | /* 1 iff Vcode_conversion_reused_workbuf is already in use. */ |
7434 | static int reused_workbuf_in_use; | |
4ed46869 | 7435 | |
24a73b0a | 7436 | |
ad1746f5 | 7437 | /* Return a working buffer of code conversion. MULTIBYTE specifies the |
24a73b0a | 7438 | multibyteness of returning buffer. */ |
b73bfc1c | 7439 | |
f6cbaf43 | 7440 | static Lisp_Object |
971de7fb | 7441 | make_conversion_work_buffer (int multibyte) |
df7492f9 | 7442 | { |
24a73b0a KH |
7443 | Lisp_Object name, workbuf; |
7444 | struct buffer *current; | |
4ed46869 | 7445 | |
24a73b0a | 7446 | if (reused_workbuf_in_use++) |
065e3595 KH |
7447 | { |
7448 | name = Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name, Qnil); | |
7449 | workbuf = Fget_buffer_create (name); | |
7450 | } | |
df7492f9 | 7451 | else |
065e3595 | 7452 | { |
159bd5a2 | 7453 | if (NILP (Fbuffer_live_p (Vcode_conversion_reused_workbuf))) |
a993c7a1 KH |
7454 | Vcode_conversion_reused_workbuf |
7455 | = Fget_buffer_create (Vcode_conversion_workbuf_name); | |
7456 | workbuf = Vcode_conversion_reused_workbuf; | |
065e3595 | 7457 | } |
24a73b0a KH |
7458 | current = current_buffer; |
7459 | set_buffer_internal (XBUFFER (workbuf)); | |
df36ff1f CY |
7460 | /* We can't allow modification hooks to run in the work buffer. For |
7461 | instance, directory_files_internal assumes that file decoding | |
7462 | doesn't compile new regexps. */ | |
7463 | Fset (Fmake_local_variable (Qinhibit_modification_hooks), Qt); | |
3ed051d4 | 7464 | Ferase_buffer (); |
4b4deea2 TT |
7465 | BVAR (current_buffer, undo_list) = Qt; |
7466 | BVAR (current_buffer, enable_multibyte_characters) = multibyte ? Qt : Qnil; | |
df7492f9 | 7467 | set_buffer_internal (current); |
24a73b0a | 7468 | return workbuf; |
df7492f9 | 7469 | } |
d46c5b12 | 7470 | |
24a73b0a | 7471 | |
4776e638 | 7472 | static Lisp_Object |
971de7fb | 7473 | code_conversion_restore (Lisp_Object arg) |
4776e638 | 7474 | { |
24a73b0a | 7475 | Lisp_Object current, workbuf; |
948bdcf3 | 7476 | struct gcpro gcpro1; |
24a73b0a | 7477 | |
948bdcf3 | 7478 | GCPRO1 (arg); |
24a73b0a KH |
7479 | current = XCAR (arg); |
7480 | workbuf = XCDR (arg); | |
7481 | if (! NILP (workbuf)) | |
7482 | { | |
7483 | if (EQ (workbuf, Vcode_conversion_reused_workbuf)) | |
7484 | reused_workbuf_in_use = 0; | |
7485 | else if (! NILP (Fbuffer_live_p (workbuf))) | |
7486 | Fkill_buffer (workbuf); | |
7487 | } | |
7488 | set_buffer_internal (XBUFFER (current)); | |
948bdcf3 | 7489 | UNGCPRO; |
4776e638 KH |
7490 | return Qnil; |
7491 | } | |
b73bfc1c | 7492 | |
24a73b0a | 7493 | Lisp_Object |
971de7fb | 7494 | code_conversion_save (int with_work_buf, int multibyte) |
df7492f9 | 7495 | { |
24a73b0a | 7496 | Lisp_Object workbuf = Qnil; |
b73bfc1c | 7497 | |
4776e638 | 7498 | if (with_work_buf) |
24a73b0a KH |
7499 | workbuf = make_conversion_work_buffer (multibyte); |
7500 | record_unwind_protect (code_conversion_restore, | |
7501 | Fcons (Fcurrent_buffer (), workbuf)); | |
4776e638 | 7502 | return workbuf; |
df7492f9 | 7503 | } |
d46c5b12 | 7504 | |
df7492f9 | 7505 | int |
cf84bb53 JB |
7506 | decode_coding_gap (struct coding_system *coding, |
7507 | EMACS_INT chars, EMACS_INT bytes) | |
df7492f9 | 7508 | { |
1a4990fb | 7509 | int count = SPECPDL_INDEX (); |
5e5c78be | 7510 | Lisp_Object attrs; |
fb88bf2d | 7511 | |
24a73b0a | 7512 | code_conversion_save (0, 0); |
ec6d2bb8 | 7513 | |
24a73b0a | 7514 | coding->src_object = Fcurrent_buffer (); |
df7492f9 KH |
7515 | coding->src_chars = chars; |
7516 | coding->src_bytes = bytes; | |
7517 | coding->src_pos = -chars; | |
7518 | coding->src_pos_byte = -bytes; | |
7519 | coding->src_multibyte = chars < bytes; | |
24a73b0a | 7520 | coding->dst_object = coding->src_object; |
df7492f9 KH |
7521 | coding->dst_pos = PT; |
7522 | coding->dst_pos_byte = PT_BYTE; | |
4b4deea2 | 7523 | coding->dst_multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters)); |
4ed46869 | 7524 | |
df7492f9 KH |
7525 | if (CODING_REQUIRE_DETECTION (coding)) |
7526 | detect_coding (coding); | |
8f924df7 | 7527 | |
9286b333 | 7528 | coding->mode |= CODING_MODE_LAST_BLOCK; |
287c57d7 | 7529 | current_buffer->text->inhibit_shrinking = 1; |
df7492f9 | 7530 | decode_coding (coding); |
287c57d7 | 7531 | current_buffer->text->inhibit_shrinking = 0; |
d46c5b12 | 7532 | |
5e5c78be KH |
7533 | attrs = CODING_ID_ATTRS (coding->id); |
7534 | if (! NILP (CODING_ATTR_POST_READ (attrs))) | |
b73bfc1c | 7535 | { |
5e5c78be KH |
7536 | EMACS_INT prev_Z = Z, prev_Z_BYTE = Z_BYTE; |
7537 | Lisp_Object val; | |
7538 | ||
7539 | TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte); | |
5e5c78be KH |
7540 | val = call1 (CODING_ATTR_POST_READ (attrs), |
7541 | make_number (coding->produced_char)); | |
5e5c78be KH |
7542 | CHECK_NATNUM (val); |
7543 | coding->produced_char += Z - prev_Z; | |
7544 | coding->produced += Z_BYTE - prev_Z_BYTE; | |
b73bfc1c | 7545 | } |
4ed46869 | 7546 | |
df7492f9 | 7547 | unbind_to (count, Qnil); |
b73bfc1c KH |
7548 | return coding->result; |
7549 | } | |
52d41803 | 7550 | |
4ed46869 | 7551 | int |
cf84bb53 JB |
7552 | encode_coding_gap (struct coding_system *coding, |
7553 | EMACS_INT chars, EMACS_INT bytes) | |
4ed46869 | 7554 | { |
1a4990fb | 7555 | int count = SPECPDL_INDEX (); |
4ed46869 | 7556 | |
24a73b0a | 7557 | code_conversion_save (0, 0); |
4ed46869 | 7558 | |
24a73b0a | 7559 | coding->src_object = Fcurrent_buffer (); |
df7492f9 KH |
7560 | coding->src_chars = chars; |
7561 | coding->src_bytes = bytes; | |
7562 | coding->src_pos = -chars; | |
7563 | coding->src_pos_byte = -bytes; | |
7564 | coding->src_multibyte = chars < bytes; | |
7565 | coding->dst_object = coding->src_object; | |
7566 | coding->dst_pos = PT; | |
7567 | coding->dst_pos_byte = PT_BYTE; | |
4ed46869 | 7568 | |
df7492f9 | 7569 | encode_coding (coding); |
b73bfc1c | 7570 | |
df7492f9 KH |
7571 | unbind_to (count, Qnil); |
7572 | return coding->result; | |
7573 | } | |
4ed46869 | 7574 | |
d46c5b12 | 7575 | |
df7492f9 KH |
7576 | /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in |
7577 | SRC_OBJECT into DST_OBJECT by coding context CODING. | |
b73bfc1c | 7578 | |
df7492f9 | 7579 | SRC_OBJECT is a buffer, a string, or Qnil. |
b73bfc1c | 7580 | |
df7492f9 KH |
7581 | If it is a buffer, the text is at point of the buffer. FROM and TO |
7582 | are positions in the buffer. | |
b73bfc1c | 7583 | |
df7492f9 KH |
7584 | If it is a string, the text is at the beginning of the string. |
7585 | FROM and TO are indices to the string. | |
4ed46869 | 7586 | |
df7492f9 KH |
7587 | If it is nil, the text is at coding->source. FROM and TO are |
7588 | indices to coding->source. | |
bb10be8b | 7589 | |
df7492f9 | 7590 | DST_OBJECT is a buffer, Qt, or Qnil. |
4ed46869 | 7591 | |
df7492f9 KH |
7592 | If it is a buffer, the decoded text is inserted at point of the |
7593 | buffer. If the buffer is the same as SRC_OBJECT, the source text | |
7594 | is deleted. | |
4ed46869 | 7595 | |
df7492f9 KH |
7596 | If it is Qt, a string is made from the decoded text, and |
7597 | set in CODING->dst_object. | |
d46c5b12 | 7598 | |
df7492f9 | 7599 | If it is Qnil, the decoded text is stored at CODING->destination. |
2cb26057 | 7600 | The caller must allocate CODING->dst_bytes bytes at |
df7492f9 KH |
7601 | CODING->destination by xmalloc. If the decoded text is longer than |
7602 | CODING->dst_bytes, CODING->destination is relocated by xrealloc. | |
7603 | */ | |
d46c5b12 | 7604 | |
df7492f9 | 7605 | void |
cf84bb53 JB |
7606 | decode_coding_object (struct coding_system *coding, |
7607 | Lisp_Object src_object, | |
7608 | EMACS_INT from, EMACS_INT from_byte, | |
7609 | EMACS_INT to, EMACS_INT to_byte, | |
7610 | Lisp_Object dst_object) | |
d46c5b12 | 7611 | { |
1a4990fb | 7612 | int count = SPECPDL_INDEX (); |
c4a63b12 PE |
7613 | unsigned char *destination IF_LINT (= NULL); |
7614 | EMACS_INT dst_bytes IF_LINT (= 0); | |
df7492f9 KH |
7615 | EMACS_INT chars = to - from; |
7616 | EMACS_INT bytes = to_byte - from_byte; | |
7617 | Lisp_Object attrs; | |
c4a63b12 | 7618 | int saved_pt = -1, saved_pt_byte IF_LINT (= 0); |
64cedb0c | 7619 | int need_marker_adjustment = 0; |
b3bfad50 | 7620 | Lisp_Object old_deactivate_mark; |
d46c5b12 | 7621 | |
b3bfad50 | 7622 | old_deactivate_mark = Vdeactivate_mark; |
93dec019 | 7623 | |
df7492f9 | 7624 | if (NILP (dst_object)) |
d46c5b12 | 7625 | { |
df7492f9 KH |
7626 | destination = coding->destination; |
7627 | dst_bytes = coding->dst_bytes; | |
d46c5b12 | 7628 | } |
93dec019 | 7629 | |
df7492f9 KH |
7630 | coding->src_object = src_object; |
7631 | coding->src_chars = chars; | |
7632 | coding->src_bytes = bytes; | |
7633 | coding->src_multibyte = chars < bytes; | |
70ad9fc4 | 7634 | |
df7492f9 | 7635 | if (STRINGP (src_object)) |
d46c5b12 | 7636 | { |
df7492f9 KH |
7637 | coding->src_pos = from; |
7638 | coding->src_pos_byte = from_byte; | |
d46c5b12 | 7639 | } |
df7492f9 | 7640 | else if (BUFFERP (src_object)) |
88993dfd | 7641 | { |
df7492f9 KH |
7642 | set_buffer_internal (XBUFFER (src_object)); |
7643 | if (from != GPT) | |
7644 | move_gap_both (from, from_byte); | |
7645 | if (EQ (src_object, dst_object)) | |
fb88bf2d | 7646 | { |
64cedb0c KH |
7647 | struct Lisp_Marker *tail; |
7648 | ||
7649 | for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next) | |
7650 | { | |
7651 | tail->need_adjustment | |
7652 | = tail->charpos == (tail->insertion_type ? from : to); | |
7653 | need_marker_adjustment |= tail->need_adjustment; | |
7654 | } | |
4776e638 | 7655 | saved_pt = PT, saved_pt_byte = PT_BYTE; |
df7492f9 | 7656 | TEMP_SET_PT_BOTH (from, from_byte); |
f4a3cc44 | 7657 | current_buffer->text->inhibit_shrinking = 1; |
df7492f9 KH |
7658 | del_range_both (from, from_byte, to, to_byte, 1); |
7659 | coding->src_pos = -chars; | |
7660 | coding->src_pos_byte = -bytes; | |
fb88bf2d | 7661 | } |
df7492f9 | 7662 | else |
fb88bf2d | 7663 | { |
df7492f9 KH |
7664 | coding->src_pos = from; |
7665 | coding->src_pos_byte = from_byte; | |
fb88bf2d | 7666 | } |
88993dfd KH |
7667 | } |
7668 | ||
df7492f9 KH |
7669 | if (CODING_REQUIRE_DETECTION (coding)) |
7670 | detect_coding (coding); | |
7671 | attrs = CODING_ID_ATTRS (coding->id); | |
d46c5b12 | 7672 | |
2cb26057 KH |
7673 | if (EQ (dst_object, Qt) |
7674 | || (! NILP (CODING_ATTR_POST_READ (attrs)) | |
7675 | && NILP (dst_object))) | |
b73bfc1c | 7676 | { |
a1567c45 SM |
7677 | coding->dst_multibyte = !CODING_FOR_UNIBYTE (coding); |
7678 | coding->dst_object = code_conversion_save (1, coding->dst_multibyte); | |
df7492f9 KH |
7679 | coding->dst_pos = BEG; |
7680 | coding->dst_pos_byte = BEG_BYTE; | |
b73bfc1c | 7681 | } |
df7492f9 | 7682 | else if (BUFFERP (dst_object)) |
d46c5b12 | 7683 | { |
24a73b0a | 7684 | code_conversion_save (0, 0); |
df7492f9 KH |
7685 | coding->dst_object = dst_object; |
7686 | coding->dst_pos = BUF_PT (XBUFFER (dst_object)); | |
7687 | coding->dst_pos_byte = BUF_PT_BYTE (XBUFFER (dst_object)); | |
7688 | coding->dst_multibyte | |
4b4deea2 | 7689 | = ! NILP (BVAR (XBUFFER (dst_object), enable_multibyte_characters)); |
d46c5b12 KH |
7690 | } |
7691 | else | |
7692 | { | |
24a73b0a | 7693 | code_conversion_save (0, 0); |
df7492f9 | 7694 | coding->dst_object = Qnil; |
0154725e SM |
7695 | /* Most callers presume this will return a multibyte result, and they |
7696 | won't use `binary' or `raw-text' anyway, so let's not worry about | |
7697 | CODING_FOR_UNIBYTE. */ | |
bb555731 | 7698 | coding->dst_multibyte = 1; |
d46c5b12 KH |
7699 | } |
7700 | ||
df7492f9 | 7701 | decode_coding (coding); |
fa46990e | 7702 | |
df7492f9 KH |
7703 | if (BUFFERP (coding->dst_object)) |
7704 | set_buffer_internal (XBUFFER (coding->dst_object)); | |
d46c5b12 | 7705 | |
df7492f9 | 7706 | if (! NILP (CODING_ATTR_POST_READ (attrs))) |
d46c5b12 | 7707 | { |
b3bfad50 | 7708 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; |
df7492f9 | 7709 | EMACS_INT prev_Z = Z, prev_Z_BYTE = Z_BYTE; |
2b4f9037 | 7710 | Lisp_Object val; |
d46c5b12 | 7711 | |
c0cc7f7f | 7712 | TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte); |
b3bfad50 KH |
7713 | GCPRO5 (coding->src_object, coding->dst_object, src_object, dst_object, |
7714 | old_deactivate_mark); | |
d4850d67 KH |
7715 | val = safe_call1 (CODING_ATTR_POST_READ (attrs), |
7716 | make_number (coding->produced_char)); | |
df7492f9 KH |
7717 | UNGCPRO; |
7718 | CHECK_NATNUM (val); | |
7719 | coding->produced_char += Z - prev_Z; | |
7720 | coding->produced += Z_BYTE - prev_Z_BYTE; | |
d46c5b12 | 7721 | } |
de79a6a5 | 7722 | |
df7492f9 | 7723 | if (EQ (dst_object, Qt)) |
ec6d2bb8 | 7724 | { |
df7492f9 KH |
7725 | coding->dst_object = Fbuffer_string (); |
7726 | } | |
7727 | else if (NILP (dst_object) && BUFFERP (coding->dst_object)) | |
7728 | { | |
7729 | set_buffer_internal (XBUFFER (coding->dst_object)); | |
7730 | if (dst_bytes < coding->produced) | |
7731 | { | |
b3bfad50 | 7732 | destination = xrealloc (destination, coding->produced); |
df7492f9 KH |
7733 | if (! destination) |
7734 | { | |
065e3595 | 7735 | record_conversion_result (coding, |
ebaf11b6 | 7736 | CODING_RESULT_INSUFFICIENT_MEM); |
df7492f9 KH |
7737 | unbind_to (count, Qnil); |
7738 | return; | |
7739 | } | |
7740 | if (BEGV < GPT && GPT < BEGV + coding->produced_char) | |
7741 | move_gap_both (BEGV, BEGV_BYTE); | |
72af86bd | 7742 | memcpy (destination, BEGV_ADDR, coding->produced); |
df7492f9 | 7743 | coding->destination = destination; |
d46c5b12 | 7744 | } |
ec6d2bb8 | 7745 | } |
b73bfc1c | 7746 | |
4776e638 KH |
7747 | if (saved_pt >= 0) |
7748 | { | |
7749 | /* This is the case of: | |
7750 | (BUFFERP (src_object) && EQ (src_object, dst_object)) | |
7751 | As we have moved PT while replacing the original buffer | |
7752 | contents, we must recover it now. */ | |
7753 | set_buffer_internal (XBUFFER (src_object)); | |
f4a3cc44 | 7754 | current_buffer->text->inhibit_shrinking = 0; |
4776e638 KH |
7755 | if (saved_pt < from) |
7756 | TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte); | |
7757 | else if (saved_pt < from + chars) | |
7758 | TEMP_SET_PT_BOTH (from, from_byte); | |
4b4deea2 | 7759 | else if (! NILP (BVAR (current_buffer, enable_multibyte_characters))) |
4776e638 KH |
7760 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars), |
7761 | saved_pt_byte + (coding->produced - bytes)); | |
7762 | else | |
7763 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes), | |
7764 | saved_pt_byte + (coding->produced - bytes)); | |
64cedb0c KH |
7765 | |
7766 | if (need_marker_adjustment) | |
7767 | { | |
7768 | struct Lisp_Marker *tail; | |
7769 | ||
7770 | for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next) | |
7771 | if (tail->need_adjustment) | |
7772 | { | |
7773 | tail->need_adjustment = 0; | |
7774 | if (tail->insertion_type) | |
7775 | { | |
7776 | tail->bytepos = from_byte; | |
7777 | tail->charpos = from; | |
7778 | } | |
7779 | else | |
7780 | { | |
7781 | tail->bytepos = from_byte + coding->produced; | |
7782 | tail->charpos | |
4b4deea2 | 7783 | = (NILP (BVAR (current_buffer, enable_multibyte_characters)) |
64cedb0c KH |
7784 | ? tail->bytepos : from + coding->produced_char); |
7785 | } | |
7786 | } | |
7787 | } | |
d46c5b12 | 7788 | } |
4776e638 | 7789 | |
b3bfad50 | 7790 | Vdeactivate_mark = old_deactivate_mark; |
065e3595 | 7791 | unbind_to (count, coding->dst_object); |
d46c5b12 KH |
7792 | } |
7793 | ||
d46c5b12 | 7794 | |
df7492f9 | 7795 | void |
cf84bb53 JB |
7796 | encode_coding_object (struct coding_system *coding, |
7797 | Lisp_Object src_object, | |
7798 | EMACS_INT from, EMACS_INT from_byte, | |
7799 | EMACS_INT to, EMACS_INT to_byte, | |
7800 | Lisp_Object dst_object) | |
d46c5b12 | 7801 | { |
1a4990fb | 7802 | int count = SPECPDL_INDEX (); |
df7492f9 KH |
7803 | EMACS_INT chars = to - from; |
7804 | EMACS_INT bytes = to_byte - from_byte; | |
7805 | Lisp_Object attrs; | |
c4a63b12 | 7806 | int saved_pt = -1, saved_pt_byte IF_LINT (= 0); |
64cedb0c | 7807 | int need_marker_adjustment = 0; |
c02d943b | 7808 | int kill_src_buffer = 0; |
b3bfad50 | 7809 | Lisp_Object old_deactivate_mark; |
df7492f9 | 7810 | |
b3bfad50 | 7811 | old_deactivate_mark = Vdeactivate_mark; |
df7492f9 KH |
7812 | |
7813 | coding->src_object = src_object; | |
7814 | coding->src_chars = chars; | |
7815 | coding->src_bytes = bytes; | |
7816 | coding->src_multibyte = chars < bytes; | |
7817 | ||
7818 | attrs = CODING_ID_ATTRS (coding->id); | |
7819 | ||
64cedb0c KH |
7820 | if (EQ (src_object, dst_object)) |
7821 | { | |
7822 | struct Lisp_Marker *tail; | |
7823 | ||
7824 | for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next) | |
7825 | { | |
7826 | tail->need_adjustment | |
7827 | = tail->charpos == (tail->insertion_type ? from : to); | |
7828 | need_marker_adjustment |= tail->need_adjustment; | |
7829 | } | |
7830 | } | |
7831 | ||
df7492f9 | 7832 | if (! NILP (CODING_ATTR_PRE_WRITE (attrs))) |
6bac5b12 | 7833 | { |
24a73b0a | 7834 | coding->src_object = code_conversion_save (1, coding->src_multibyte); |
df7492f9 KH |
7835 | set_buffer_internal (XBUFFER (coding->src_object)); |
7836 | if (STRINGP (src_object)) | |
7837 | insert_from_string (src_object, from, from_byte, chars, bytes, 0); | |
7838 | else if (BUFFERP (src_object)) | |
7839 | insert_from_buffer (XBUFFER (src_object), from, chars, 0); | |
7840 | else | |
b68864e5 | 7841 | insert_1_both ((char *) coding->source + from, chars, bytes, 0, 0, 0); |
d46c5b12 | 7842 | |
df7492f9 KH |
7843 | if (EQ (src_object, dst_object)) |
7844 | { | |
7845 | set_buffer_internal (XBUFFER (src_object)); | |
4776e638 | 7846 | saved_pt = PT, saved_pt_byte = PT_BYTE; |
df7492f9 KH |
7847 | del_range_both (from, from_byte, to, to_byte, 1); |
7848 | set_buffer_internal (XBUFFER (coding->src_object)); | |
7849 | } | |
7850 | ||
d4850d67 KH |
7851 | { |
7852 | Lisp_Object args[3]; | |
b3bfad50 | 7853 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; |
d4850d67 | 7854 | |
b3bfad50 KH |
7855 | GCPRO5 (coding->src_object, coding->dst_object, src_object, dst_object, |
7856 | old_deactivate_mark); | |
d4850d67 KH |
7857 | args[0] = CODING_ATTR_PRE_WRITE (attrs); |
7858 | args[1] = make_number (BEG); | |
7859 | args[2] = make_number (Z); | |
7860 | safe_call (3, args); | |
b3bfad50 | 7861 | UNGCPRO; |
d4850d67 | 7862 | } |
c02d943b KH |
7863 | if (XBUFFER (coding->src_object) != current_buffer) |
7864 | kill_src_buffer = 1; | |
ac87bbef | 7865 | coding->src_object = Fcurrent_buffer (); |
df7492f9 KH |
7866 | if (BEG != GPT) |
7867 | move_gap_both (BEG, BEG_BYTE); | |
7868 | coding->src_chars = Z - BEG; | |
7869 | coding->src_bytes = Z_BYTE - BEG_BYTE; | |
7870 | coding->src_pos = BEG; | |
7871 | coding->src_pos_byte = BEG_BYTE; | |
7872 | coding->src_multibyte = Z < Z_BYTE; | |
7873 | } | |
7874 | else if (STRINGP (src_object)) | |
d46c5b12 | 7875 | { |
24a73b0a | 7876 | code_conversion_save (0, 0); |
df7492f9 KH |
7877 | coding->src_pos = from; |
7878 | coding->src_pos_byte = from_byte; | |
b73bfc1c | 7879 | } |
df7492f9 | 7880 | else if (BUFFERP (src_object)) |
b73bfc1c | 7881 | { |
24a73b0a | 7882 | code_conversion_save (0, 0); |
df7492f9 | 7883 | set_buffer_internal (XBUFFER (src_object)); |
df7492f9 | 7884 | if (EQ (src_object, dst_object)) |
d46c5b12 | 7885 | { |
4776e638 | 7886 | saved_pt = PT, saved_pt_byte = PT_BYTE; |
ff0dacd7 KH |
7887 | coding->src_object = del_range_1 (from, to, 1, 1); |
7888 | coding->src_pos = 0; | |
7889 | coding->src_pos_byte = 0; | |
d46c5b12 | 7890 | } |
df7492f9 | 7891 | else |
d46c5b12 | 7892 | { |
ff0dacd7 KH |
7893 | if (from < GPT && to >= GPT) |
7894 | move_gap_both (from, from_byte); | |
df7492f9 KH |
7895 | coding->src_pos = from; |
7896 | coding->src_pos_byte = from_byte; | |
d46c5b12 | 7897 | } |
d46c5b12 | 7898 | } |
4776e638 | 7899 | else |
24a73b0a | 7900 | code_conversion_save (0, 0); |
d46c5b12 | 7901 | |
df7492f9 | 7902 | if (BUFFERP (dst_object)) |
88993dfd | 7903 | { |
df7492f9 | 7904 | coding->dst_object = dst_object; |
28f67a95 KH |
7905 | if (EQ (src_object, dst_object)) |
7906 | { | |
7907 | coding->dst_pos = from; | |
7908 | coding->dst_pos_byte = from_byte; | |
7909 | } | |
7910 | else | |
7911 | { | |
319a3947 KH |
7912 | struct buffer *current = current_buffer; |
7913 | ||
7914 | set_buffer_temp (XBUFFER (dst_object)); | |
7915 | coding->dst_pos = PT; | |
7916 | coding->dst_pos_byte = PT_BYTE; | |
7917 | move_gap_both (coding->dst_pos, coding->dst_pos_byte); | |
7918 | set_buffer_temp (current); | |
28f67a95 | 7919 | } |
df7492f9 | 7920 | coding->dst_multibyte |
4b4deea2 | 7921 | = ! NILP (BVAR (XBUFFER (dst_object), enable_multibyte_characters)); |
88993dfd | 7922 | } |
df7492f9 | 7923 | else if (EQ (dst_object, Qt)) |
d46c5b12 | 7924 | { |
df7492f9 | 7925 | coding->dst_object = Qnil; |
df7492f9 | 7926 | coding->dst_bytes = coding->src_chars; |
ac87bbef KH |
7927 | if (coding->dst_bytes == 0) |
7928 | coding->dst_bytes = 1; | |
7929 | coding->destination = (unsigned char *) xmalloc (coding->dst_bytes); | |
df7492f9 | 7930 | coding->dst_multibyte = 0; |
d46c5b12 KH |
7931 | } |
7932 | else | |
7933 | { | |
df7492f9 KH |
7934 | coding->dst_object = Qnil; |
7935 | coding->dst_multibyte = 0; | |
d46c5b12 KH |
7936 | } |
7937 | ||
df7492f9 | 7938 | encode_coding (coding); |
d46c5b12 | 7939 | |
df7492f9 | 7940 | if (EQ (dst_object, Qt)) |
d46c5b12 | 7941 | { |
df7492f9 KH |
7942 | if (BUFFERP (coding->dst_object)) |
7943 | coding->dst_object = Fbuffer_string (); | |
7944 | else | |
d46c5b12 | 7945 | { |
df7492f9 KH |
7946 | coding->dst_object |
7947 | = make_unibyte_string ((char *) coding->destination, | |
7948 | coding->produced); | |
7949 | xfree (coding->destination); | |
d46c5b12 | 7950 | } |
4ed46869 | 7951 | } |
d46c5b12 | 7952 | |
4776e638 KH |
7953 | if (saved_pt >= 0) |
7954 | { | |
7955 | /* This is the case of: | |
7956 | (BUFFERP (src_object) && EQ (src_object, dst_object)) | |
7957 | As we have moved PT while replacing the original buffer | |
7958 | contents, we must recover it now. */ | |
7959 | set_buffer_internal (XBUFFER (src_object)); | |
7960 | if (saved_pt < from) | |
7961 | TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte); | |
7962 | else if (saved_pt < from + chars) | |
7963 | TEMP_SET_PT_BOTH (from, from_byte); | |
4b4deea2 | 7964 | else if (! NILP (BVAR (current_buffer, enable_multibyte_characters))) |
4776e638 KH |
7965 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars), |
7966 | saved_pt_byte + (coding->produced - bytes)); | |
d46c5b12 | 7967 | else |
4776e638 KH |
7968 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes), |
7969 | saved_pt_byte + (coding->produced - bytes)); | |
64cedb0c KH |
7970 | |
7971 | if (need_marker_adjustment) | |
7972 | { | |
7973 | struct Lisp_Marker *tail; | |
7974 | ||
7975 | for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next) | |
7976 | if (tail->need_adjustment) | |
7977 | { | |
7978 | tail->need_adjustment = 0; | |
7979 | if (tail->insertion_type) | |
7980 | { | |
7981 | tail->bytepos = from_byte; | |
7982 | tail->charpos = from; | |
7983 | } | |
7984 | else | |
7985 | { | |
7986 | tail->bytepos = from_byte + coding->produced; | |
7987 | tail->charpos | |
4b4deea2 | 7988 | = (NILP (BVAR (current_buffer, enable_multibyte_characters)) |
64cedb0c KH |
7989 | ? tail->bytepos : from + coding->produced_char); |
7990 | } | |
7991 | } | |
7992 | } | |
4776e638 KH |
7993 | } |
7994 | ||
c02d943b KH |
7995 | if (kill_src_buffer) |
7996 | Fkill_buffer (coding->src_object); | |
b3bfad50 KH |
7997 | |
7998 | Vdeactivate_mark = old_deactivate_mark; | |
df7492f9 | 7999 | unbind_to (count, Qnil); |
b73bfc1c KH |
8000 | } |
8001 | ||
df7492f9 | 8002 | |
b73bfc1c | 8003 | Lisp_Object |
971de7fb | 8004 | preferred_coding_system (void) |
b73bfc1c | 8005 | { |
df7492f9 | 8006 | int id = coding_categories[coding_priorities[0]].id; |
2391eaa4 | 8007 | |
df7492f9 | 8008 | return CODING_ID_NAME (id); |
4ed46869 KH |
8009 | } |
8010 | ||
8011 | \f | |
8012 | #ifdef emacs | |
1397dc18 | 8013 | /*** 8. Emacs Lisp library functions ***/ |
4ed46869 | 8014 | |
4ed46869 | 8015 | DEFUN ("coding-system-p", Fcoding_system_p, Scoding_system_p, 1, 1, 0, |
48b0f3ae | 8016 | doc: /* Return t if OBJECT is nil or a coding-system. |
df7492f9 | 8017 | See the documentation of `define-coding-system' for information |
48b0f3ae | 8018 | about coding-system objects. */) |
5842a27b | 8019 | (Lisp_Object object) |
4ed46869 | 8020 | { |
d4a1d553 JB |
8021 | if (NILP (object) |
8022 | || CODING_SYSTEM_ID (object) >= 0) | |
44e8490d | 8023 | return Qt; |
d4a1d553 JB |
8024 | if (! SYMBOLP (object) |
8025 | || NILP (Fget (object, Qcoding_system_define_form))) | |
44e8490d KH |
8026 | return Qnil; |
8027 | return Qt; | |
4ed46869 KH |
8028 | } |
8029 | ||
9d991de8 RS |
8030 | DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system, |
8031 | Sread_non_nil_coding_system, 1, 1, 0, | |
48b0f3ae | 8032 | doc: /* Read a coding system from the minibuffer, prompting with string PROMPT. */) |
5842a27b | 8033 | (Lisp_Object prompt) |
4ed46869 | 8034 | { |
e0e989f6 | 8035 | Lisp_Object val; |
9d991de8 RS |
8036 | do |
8037 | { | |
4608c386 KH |
8038 | val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil, |
8039 | Qt, Qnil, Qcoding_system_history, Qnil, Qnil); | |
9d991de8 | 8040 | } |
8f924df7 | 8041 | while (SCHARS (val) == 0); |
e0e989f6 | 8042 | return (Fintern (val, Qnil)); |
4ed46869 KH |
8043 | } |
8044 | ||
9b787f3e | 8045 | DEFUN ("read-coding-system", Fread_coding_system, Sread_coding_system, 1, 2, 0, |
48b0f3ae | 8046 | doc: /* Read a coding system from the minibuffer, prompting with string PROMPT. |
c7183fb8 GM |
8047 | If the user enters null input, return second argument DEFAULT-CODING-SYSTEM. |
8048 | Ignores case when completing coding systems (all Emacs coding systems | |
8049 | are lower-case). */) | |
5842a27b | 8050 | (Lisp_Object prompt, Lisp_Object default_coding_system) |
4ed46869 | 8051 | { |
f44d27ce | 8052 | Lisp_Object val; |
c7183fb8 GM |
8053 | int count = SPECPDL_INDEX (); |
8054 | ||
9b787f3e | 8055 | if (SYMBOLP (default_coding_system)) |
57d25e6f | 8056 | default_coding_system = SYMBOL_NAME (default_coding_system); |
c7183fb8 | 8057 | specbind (Qcompletion_ignore_case, Qt); |
4608c386 | 8058 | val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil, |
9b787f3e RS |
8059 | Qt, Qnil, Qcoding_system_history, |
8060 | default_coding_system, Qnil); | |
c7183fb8 | 8061 | unbind_to (count, Qnil); |
8f924df7 | 8062 | return (SCHARS (val) == 0 ? Qnil : Fintern (val, Qnil)); |
4ed46869 KH |
8063 | } |
8064 | ||
8065 | DEFUN ("check-coding-system", Fcheck_coding_system, Scheck_coding_system, | |
8066 | 1, 1, 0, | |
48b0f3ae | 8067 | doc: /* Check validity of CODING-SYSTEM. |
9ffd559c KH |
8068 | If valid, return CODING-SYSTEM, else signal a `coding-system-error' error. |
8069 | It is valid if it is nil or a symbol defined as a coding system by the | |
8070 | function `define-coding-system'. */) | |
5842a27b | 8071 | (Lisp_Object coding_system) |
4ed46869 | 8072 | { |
44e8490d KH |
8073 | Lisp_Object define_form; |
8074 | ||
8075 | define_form = Fget (coding_system, Qcoding_system_define_form); | |
8076 | if (! NILP (define_form)) | |
8077 | { | |
8078 | Fput (coding_system, Qcoding_system_define_form, Qnil); | |
8079 | safe_eval (define_form); | |
8080 | } | |
4ed46869 KH |
8081 | if (!NILP (Fcoding_system_p (coding_system))) |
8082 | return coding_system; | |
fcad4ec4 | 8083 | xsignal1 (Qcoding_system_error, coding_system); |
4ed46869 | 8084 | } |
df7492f9 | 8085 | |
3a73fa5d | 8086 | \f |
89528eb3 KH |
8087 | /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If |
8088 | HIGHEST is nonzero, return the coding system of the highest | |
ad1746f5 | 8089 | priority among the detected coding systems. Otherwise return a |
89528eb3 KH |
8090 | list of detected coding systems sorted by their priorities. If |
8091 | MULTIBYTEP is nonzero, it is assumed that the bytes are in correct | |
8092 | multibyte form but contains only ASCII and eight-bit chars. | |
8093 | Otherwise, the bytes are raw bytes. | |
8094 | ||
8095 | CODING-SYSTEM controls the detection as below: | |
8096 | ||
8097 | If it is nil, detect both text-format and eol-format. If the | |
8098 | text-format part of CODING-SYSTEM is already specified | |
8099 | (e.g. `iso-latin-1'), detect only eol-format. If the eol-format | |
8100 | part of CODING-SYSTEM is already specified (e.g. `undecided-unix'), | |
8101 | detect only text-format. */ | |
8102 | ||
d46c5b12 | 8103 | Lisp_Object |
cf84bb53 JB |
8104 | detect_coding_system (const unsigned char *src, |
8105 | EMACS_INT src_chars, EMACS_INT src_bytes, | |
8106 | int highest, int multibytep, | |
8107 | Lisp_Object coding_system) | |
4ed46869 | 8108 | { |
8f924df7 | 8109 | const unsigned char *src_end = src + src_bytes; |
df7492f9 | 8110 | Lisp_Object attrs, eol_type; |
4533845d | 8111 | Lisp_Object val = Qnil; |
df7492f9 | 8112 | struct coding_system coding; |
89528eb3 | 8113 | int id; |
ff0dacd7 | 8114 | struct coding_detection_info detect_info; |
24a73b0a | 8115 | enum coding_category base_category; |
2f3cbb32 | 8116 | int null_byte_found = 0, eight_bit_found = 0; |
b73bfc1c | 8117 | |
df7492f9 KH |
8118 | if (NILP (coding_system)) |
8119 | coding_system = Qundecided; | |
8120 | setup_coding_system (coding_system, &coding); | |
8121 | attrs = CODING_ID_ATTRS (coding.id); | |
8122 | eol_type = CODING_ID_EOL_TYPE (coding.id); | |
89528eb3 | 8123 | coding_system = CODING_ATTR_BASE_NAME (attrs); |
4ed46869 | 8124 | |
df7492f9 | 8125 | coding.source = src; |
24a73b0a | 8126 | coding.src_chars = src_chars; |
df7492f9 KH |
8127 | coding.src_bytes = src_bytes; |
8128 | coding.src_multibyte = multibytep; | |
8129 | coding.consumed = 0; | |
89528eb3 | 8130 | coding.mode |= CODING_MODE_LAST_BLOCK; |
c0e16b14 | 8131 | coding.head_ascii = 0; |
d46c5b12 | 8132 | |
ff0dacd7 | 8133 | detect_info.checked = detect_info.found = detect_info.rejected = 0; |
d46c5b12 | 8134 | |
89528eb3 | 8135 | /* At first, detect text-format if necessary. */ |
24a73b0a KH |
8136 | base_category = XINT (CODING_ATTR_CATEGORY (attrs)); |
8137 | if (base_category == coding_category_undecided) | |
4ed46869 | 8138 | { |
c4a63b12 PE |
8139 | enum coding_category category IF_LINT (= 0); |
8140 | struct coding_system *this IF_LINT (= NULL); | |
ff0dacd7 | 8141 | int c, i; |
88993dfd | 8142 | |
24a73b0a | 8143 | /* Skip all ASCII bytes except for a few ISO2022 controls. */ |
2f3cbb32 | 8144 | for (; src < src_end; src++) |
4ed46869 | 8145 | { |
df7492f9 | 8146 | c = *src; |
6cb21a4f | 8147 | if (c & 0x80) |
6cb21a4f | 8148 | { |
2f3cbb32 | 8149 | eight_bit_found = 1; |
2f3cbb32 KH |
8150 | if (null_byte_found) |
8151 | break; | |
8152 | } | |
c0e16b14 | 8153 | else if (c < 0x20) |
2f3cbb32 KH |
8154 | { |
8155 | if ((c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) | |
8156 | && ! inhibit_iso_escape_detection | |
8157 | && ! detect_info.checked) | |
6cb21a4f | 8158 | { |
2f3cbb32 KH |
8159 | if (detect_coding_iso_2022 (&coding, &detect_info)) |
8160 | { | |
8161 | /* We have scanned the whole data. */ | |
8162 | if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE)) | |
c0e16b14 KH |
8163 | { |
8164 | /* We didn't find an 8-bit code. We may | |
8165 | have found a null-byte, but it's very | |
8166 | rare that a binary file confirm to | |
8167 | ISO-2022. */ | |
8168 | src = src_end; | |
8169 | coding.head_ascii = src - coding.source; | |
8170 | } | |
8171 | detect_info.rejected |= ~CATEGORY_MASK_ISO_ESCAPE; | |
2f3cbb32 KH |
8172 | break; |
8173 | } | |
8174 | } | |
97b1b294 | 8175 | else if (! c && !inhibit_null_byte_detection) |
2f3cbb32 KH |
8176 | { |
8177 | null_byte_found = 1; | |
8178 | if (eight_bit_found) | |
8179 | break; | |
6cb21a4f | 8180 | } |
c006c0c8 KH |
8181 | if (! eight_bit_found) |
8182 | coding.head_ascii++; | |
6cb21a4f | 8183 | } |
c006c0c8 | 8184 | else if (! eight_bit_found) |
c0e16b14 | 8185 | coding.head_ascii++; |
4ed46869 | 8186 | } |
88993dfd | 8187 | |
2f3cbb32 KH |
8188 | if (null_byte_found || eight_bit_found |
8189 | || coding.head_ascii < coding.src_bytes | |
6cb21a4f KH |
8190 | || detect_info.found) |
8191 | { | |
2f3cbb32 | 8192 | if (coding.head_ascii == coding.src_bytes) |
6cb21a4f KH |
8193 | /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */ |
8194 | for (i = 0; i < coding_category_raw_text; i++) | |
ff0dacd7 | 8195 | { |
6cb21a4f | 8196 | category = coding_priorities[i]; |
c7266f4a | 8197 | this = coding_categories + category; |
6cb21a4f | 8198 | if (detect_info.found & (1 << category)) |
ff0dacd7 KH |
8199 | break; |
8200 | } | |
6cb21a4f | 8201 | else |
2f3cbb32 KH |
8202 | { |
8203 | if (null_byte_found) | |
8204 | { | |
8205 | detect_info.checked |= ~CATEGORY_MASK_UTF_16; | |
8206 | detect_info.rejected |= ~CATEGORY_MASK_UTF_16; | |
8207 | } | |
8208 | for (i = 0; i < coding_category_raw_text; i++) | |
8209 | { | |
8210 | category = coding_priorities[i]; | |
8211 | this = coding_categories + category; | |
6cb21a4f | 8212 | |
2f3cbb32 KH |
8213 | if (this->id < 0) |
8214 | { | |
8215 | /* No coding system of this category is defined. */ | |
8216 | detect_info.rejected |= (1 << category); | |
8217 | } | |
8218 | else if (category >= coding_category_raw_text) | |
8219 | continue; | |
8220 | else if (detect_info.checked & (1 << category)) | |
8221 | { | |
8222 | if (highest | |
8223 | && (detect_info.found & (1 << category))) | |
6cb21a4f | 8224 | break; |
2f3cbb32 KH |
8225 | } |
8226 | else if ((*(this->detector)) (&coding, &detect_info) | |
8227 | && highest | |
8228 | && (detect_info.found & (1 << category))) | |
8229 | { | |
8230 | if (category == coding_category_utf_16_auto) | |
8231 | { | |
8232 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
8233 | category = coding_category_utf_16_le; | |
8234 | else | |
8235 | category = coding_category_utf_16_be; | |
8236 | } | |
8237 | break; | |
8238 | } | |
8239 | } | |
8240 | } | |
6cb21a4f | 8241 | } |
ec6d2bb8 | 8242 | |
4cddb209 KH |
8243 | if ((detect_info.rejected & CATEGORY_MASK_ANY) == CATEGORY_MASK_ANY |
8244 | || null_byte_found) | |
ec6d2bb8 | 8245 | { |
ff0dacd7 | 8246 | detect_info.found = CATEGORY_MASK_RAW_TEXT; |
4cddb209 | 8247 | id = CODING_SYSTEM_ID (Qno_conversion); |
89528eb3 KH |
8248 | val = Fcons (make_number (id), Qnil); |
8249 | } | |
ff0dacd7 | 8250 | else if (! detect_info.rejected && ! detect_info.found) |
89528eb3 | 8251 | { |
ff0dacd7 | 8252 | detect_info.found = CATEGORY_MASK_ANY; |
89528eb3 KH |
8253 | id = coding_categories[coding_category_undecided].id; |
8254 | val = Fcons (make_number (id), Qnil); | |
8255 | } | |
8256 | else if (highest) | |
8257 | { | |
ff0dacd7 | 8258 | if (detect_info.found) |
ec6d2bb8 | 8259 | { |
ff0dacd7 KH |
8260 | detect_info.found = 1 << category; |
8261 | val = Fcons (make_number (this->id), Qnil); | |
8262 | } | |
8263 | else | |
8264 | for (i = 0; i < coding_category_raw_text; i++) | |
8265 | if (! (detect_info.rejected & (1 << coding_priorities[i]))) | |
8266 | { | |
8267 | detect_info.found = 1 << coding_priorities[i]; | |
8268 | id = coding_categories[coding_priorities[i]].id; | |
8269 | val = Fcons (make_number (id), Qnil); | |
8270 | break; | |
8271 | } | |
8272 | } | |
89528eb3 KH |
8273 | else |
8274 | { | |
ff0dacd7 KH |
8275 | int mask = detect_info.rejected | detect_info.found; |
8276 | int found = 0; | |
ec6d2bb8 | 8277 | |
89528eb3 | 8278 | for (i = coding_category_raw_text - 1; i >= 0; i--) |
ff0dacd7 KH |
8279 | { |
8280 | category = coding_priorities[i]; | |
8281 | if (! (mask & (1 << category))) | |
ec6d2bb8 | 8282 | { |
ff0dacd7 KH |
8283 | found |= 1 << category; |
8284 | id = coding_categories[category].id; | |
c7266f4a KH |
8285 | if (id >= 0) |
8286 | val = Fcons (make_number (id), val); | |
ff0dacd7 KH |
8287 | } |
8288 | } | |
8289 | for (i = coding_category_raw_text - 1; i >= 0; i--) | |
8290 | { | |
8291 | category = coding_priorities[i]; | |
8292 | if (detect_info.found & (1 << category)) | |
8293 | { | |
8294 | id = coding_categories[category].id; | |
8295 | val = Fcons (make_number (id), val); | |
ec6d2bb8 | 8296 | } |
ec6d2bb8 | 8297 | } |
ff0dacd7 | 8298 | detect_info.found |= found; |
ec6d2bb8 | 8299 | } |
ec6d2bb8 | 8300 | } |
a470d443 KH |
8301 | else if (base_category == coding_category_utf_8_auto) |
8302 | { | |
8303 | if (detect_coding_utf_8 (&coding, &detect_info)) | |
8304 | { | |
8305 | struct coding_system *this; | |
8306 | ||
8307 | if (detect_info.found & CATEGORY_MASK_UTF_8_SIG) | |
8308 | this = coding_categories + coding_category_utf_8_sig; | |
8309 | else | |
8310 | this = coding_categories + coding_category_utf_8_nosig; | |
8311 | val = Fcons (make_number (this->id), Qnil); | |
8312 | } | |
8313 | } | |
24a73b0a KH |
8314 | else if (base_category == coding_category_utf_16_auto) |
8315 | { | |
8316 | if (detect_coding_utf_16 (&coding, &detect_info)) | |
8317 | { | |
24a73b0a KH |
8318 | struct coding_system *this; |
8319 | ||
8320 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
8321 | this = coding_categories + coding_category_utf_16_le; | |
8322 | else if (detect_info.found & CATEGORY_MASK_UTF_16_BE) | |
8323 | this = coding_categories + coding_category_utf_16_be; | |
8324 | else if (detect_info.rejected & CATEGORY_MASK_UTF_16_LE_NOSIG) | |
8325 | this = coding_categories + coding_category_utf_16_be_nosig; | |
8326 | else | |
8327 | this = coding_categories + coding_category_utf_16_le_nosig; | |
8328 | val = Fcons (make_number (this->id), Qnil); | |
8329 | } | |
8330 | } | |
df7492f9 KH |
8331 | else |
8332 | { | |
ff0dacd7 | 8333 | detect_info.found = 1 << XINT (CODING_ATTR_CATEGORY (attrs)); |
89528eb3 | 8334 | val = Fcons (make_number (coding.id), Qnil); |
4ed46869 | 8335 | } |
df7492f9 | 8336 | |
89528eb3 | 8337 | /* Then, detect eol-format if necessary. */ |
df7492f9 | 8338 | { |
4533845d | 8339 | int normal_eol = -1, utf_16_be_eol = -1, utf_16_le_eol = -1; |
df7492f9 KH |
8340 | Lisp_Object tail; |
8341 | ||
89528eb3 KH |
8342 | if (VECTORP (eol_type)) |
8343 | { | |
ff0dacd7 | 8344 | if (detect_info.found & ~CATEGORY_MASK_UTF_16) |
2f3cbb32 KH |
8345 | { |
8346 | if (null_byte_found) | |
8347 | normal_eol = EOL_SEEN_LF; | |
8348 | else | |
8349 | normal_eol = detect_eol (coding.source, src_bytes, | |
8350 | coding_category_raw_text); | |
8351 | } | |
ff0dacd7 KH |
8352 | if (detect_info.found & (CATEGORY_MASK_UTF_16_BE |
8353 | | CATEGORY_MASK_UTF_16_BE_NOSIG)) | |
89528eb3 KH |
8354 | utf_16_be_eol = detect_eol (coding.source, src_bytes, |
8355 | coding_category_utf_16_be); | |
ff0dacd7 KH |
8356 | if (detect_info.found & (CATEGORY_MASK_UTF_16_LE |
8357 | | CATEGORY_MASK_UTF_16_LE_NOSIG)) | |
89528eb3 KH |
8358 | utf_16_le_eol = detect_eol (coding.source, src_bytes, |
8359 | coding_category_utf_16_le); | |
8360 | } | |
8361 | else | |
8362 | { | |
8363 | if (EQ (eol_type, Qunix)) | |
8364 | normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_LF; | |
8365 | else if (EQ (eol_type, Qdos)) | |
8366 | normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CRLF; | |
8367 | else | |
8368 | normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CR; | |
8369 | } | |
8370 | ||
df7492f9 KH |
8371 | for (tail = val; CONSP (tail); tail = XCDR (tail)) |
8372 | { | |
89528eb3 | 8373 | enum coding_category category; |
df7492f9 | 8374 | int this_eol; |
89528eb3 KH |
8375 | |
8376 | id = XINT (XCAR (tail)); | |
8377 | attrs = CODING_ID_ATTRS (id); | |
8378 | category = XINT (CODING_ATTR_CATEGORY (attrs)); | |
8379 | eol_type = CODING_ID_EOL_TYPE (id); | |
df7492f9 KH |
8380 | if (VECTORP (eol_type)) |
8381 | { | |
89528eb3 KH |
8382 | if (category == coding_category_utf_16_be |
8383 | || category == coding_category_utf_16_be_nosig) | |
8384 | this_eol = utf_16_be_eol; | |
8385 | else if (category == coding_category_utf_16_le | |
8386 | || category == coding_category_utf_16_le_nosig) | |
8387 | this_eol = utf_16_le_eol; | |
df7492f9 | 8388 | else |
89528eb3 KH |
8389 | this_eol = normal_eol; |
8390 | ||
df7492f9 KH |
8391 | if (this_eol == EOL_SEEN_LF) |
8392 | XSETCAR (tail, AREF (eol_type, 0)); | |
8393 | else if (this_eol == EOL_SEEN_CRLF) | |
8394 | XSETCAR (tail, AREF (eol_type, 1)); | |
8395 | else if (this_eol == EOL_SEEN_CR) | |
8396 | XSETCAR (tail, AREF (eol_type, 2)); | |
89528eb3 KH |
8397 | else |
8398 | XSETCAR (tail, CODING_ID_NAME (id)); | |
df7492f9 | 8399 | } |
89528eb3 KH |
8400 | else |
8401 | XSETCAR (tail, CODING_ID_NAME (id)); | |
df7492f9 KH |
8402 | } |
8403 | } | |
ec6d2bb8 | 8404 | |
4533845d | 8405 | return (highest ? (CONSP (val) ? XCAR (val) : Qnil) : val); |
ec6d2bb8 KH |
8406 | } |
8407 | ||
ec6d2bb8 | 8408 | |
d46c5b12 KH |
8409 | DEFUN ("detect-coding-region", Fdetect_coding_region, Sdetect_coding_region, |
8410 | 2, 3, 0, | |
48b0f3ae PJ |
8411 | doc: /* Detect coding system of the text in the region between START and END. |
8412 | Return a list of possible coding systems ordered by priority. | |
b811c52b KH |
8413 | The coding systems to try and their priorities follows what |
8414 | the function `coding-system-priority-list' (which see) returns. | |
ec6d2bb8 | 8415 | |
12e0131a | 8416 | If only ASCII characters are found (except for such ISO-2022 control |
d4a1d553 JB |
8417 | characters as ESC), it returns a list of single element `undecided' |
8418 | or its subsidiary coding system according to a detected end-of-line | |
8419 | format. | |
ec6d2bb8 | 8420 | |
48b0f3ae PJ |
8421 | If optional argument HIGHEST is non-nil, return the coding system of |
8422 | highest priority. */) | |
5842a27b | 8423 | (Lisp_Object start, Lisp_Object end, Lisp_Object highest) |
d46c5b12 KH |
8424 | { |
8425 | int from, to; | |
8426 | int from_byte, to_byte; | |
ec6d2bb8 | 8427 | |
b7826503 PJ |
8428 | CHECK_NUMBER_COERCE_MARKER (start); |
8429 | CHECK_NUMBER_COERCE_MARKER (end); | |
ec6d2bb8 | 8430 | |
d46c5b12 KH |
8431 | validate_region (&start, &end); |
8432 | from = XINT (start), to = XINT (end); | |
8433 | from_byte = CHAR_TO_BYTE (from); | |
8434 | to_byte = CHAR_TO_BYTE (to); | |
ec6d2bb8 | 8435 | |
d46c5b12 KH |
8436 | if (from < GPT && to >= GPT) |
8437 | move_gap_both (to, to_byte); | |
c210f766 | 8438 | |
d46c5b12 | 8439 | return detect_coding_system (BYTE_POS_ADDR (from_byte), |
24a73b0a | 8440 | to - from, to_byte - from_byte, |
0a28aafb | 8441 | !NILP (highest), |
4b4deea2 | 8442 | !NILP (BVAR (current_buffer |
5d8ea120 | 8443 | , enable_multibyte_characters)), |
df7492f9 | 8444 | Qnil); |
ec6d2bb8 KH |
8445 | } |
8446 | ||
d46c5b12 KH |
8447 | DEFUN ("detect-coding-string", Fdetect_coding_string, Sdetect_coding_string, |
8448 | 1, 2, 0, | |
48b0f3ae PJ |
8449 | doc: /* Detect coding system of the text in STRING. |
8450 | Return a list of possible coding systems ordered by priority. | |
67ceab9d KH |
8451 | The coding systems to try and their priorities follows what |
8452 | the function `coding-system-priority-list' (which see) returns. | |
fb88bf2d | 8453 | |
12e0131a | 8454 | If only ASCII characters are found (except for such ISO-2022 control |
d4a1d553 JB |
8455 | characters as ESC), it returns a list of single element `undecided' |
8456 | or its subsidiary coding system according to a detected end-of-line | |
8457 | format. | |
d46c5b12 | 8458 | |
48b0f3ae PJ |
8459 | If optional argument HIGHEST is non-nil, return the coding system of |
8460 | highest priority. */) | |
5842a27b | 8461 | (Lisp_Object string, Lisp_Object highest) |
d46c5b12 | 8462 | { |
b7826503 | 8463 | CHECK_STRING (string); |
b73bfc1c | 8464 | |
24a73b0a KH |
8465 | return detect_coding_system (SDATA (string), |
8466 | SCHARS (string), SBYTES (string), | |
8f924df7 | 8467 | !NILP (highest), STRING_MULTIBYTE (string), |
df7492f9 | 8468 | Qnil); |
4ed46869 | 8469 | } |
4ed46869 | 8470 | |
b73bfc1c | 8471 | |
df7492f9 | 8472 | static INLINE int |
971de7fb | 8473 | char_encodable_p (int c, Lisp_Object attrs) |
05e6f5dc | 8474 | { |
df7492f9 | 8475 | Lisp_Object tail; |
df7492f9 | 8476 | struct charset *charset; |
7d64c6ad | 8477 | Lisp_Object translation_table; |
d46c5b12 | 8478 | |
7d64c6ad | 8479 | translation_table = CODING_ATTR_TRANS_TBL (attrs); |
a6f87d34 | 8480 | if (! NILP (translation_table)) |
7d64c6ad | 8481 | c = translate_char (translation_table, c); |
df7492f9 KH |
8482 | for (tail = CODING_ATTR_CHARSET_LIST (attrs); |
8483 | CONSP (tail); tail = XCDR (tail)) | |
e133c8fa | 8484 | { |
df7492f9 KH |
8485 | charset = CHARSET_FROM_ID (XINT (XCAR (tail))); |
8486 | if (CHAR_CHARSET_P (c, charset)) | |
8487 | break; | |
e133c8fa | 8488 | } |
df7492f9 | 8489 | return (! NILP (tail)); |
05e6f5dc | 8490 | } |
83fa074f | 8491 | |
fb88bf2d | 8492 | |
df7492f9 KH |
8493 | /* Return a list of coding systems that safely encode the text between |
8494 | START and END. If EXCLUDE is non-nil, it is a list of coding | |
8495 | systems not to check. The returned list doesn't contain any such | |
48468dac | 8496 | coding systems. In any case, if the text contains only ASCII or is |
df7492f9 | 8497 | unibyte, return t. */ |
e077cc80 | 8498 | |
df7492f9 KH |
8499 | DEFUN ("find-coding-systems-region-internal", |
8500 | Ffind_coding_systems_region_internal, | |
8501 | Sfind_coding_systems_region_internal, 2, 3, 0, | |
8502 | doc: /* Internal use only. */) | |
5842a27b | 8503 | (Lisp_Object start, Lisp_Object end, Lisp_Object exclude) |
df7492f9 KH |
8504 | { |
8505 | Lisp_Object coding_attrs_list, safe_codings; | |
8506 | EMACS_INT start_byte, end_byte; | |
7c78e542 | 8507 | const unsigned char *p, *pbeg, *pend; |
df7492f9 | 8508 | int c; |
0e727afa | 8509 | Lisp_Object tail, elt, work_table; |
d46c5b12 | 8510 | |
df7492f9 KH |
8511 | if (STRINGP (start)) |
8512 | { | |
8513 | if (!STRING_MULTIBYTE (start) | |
8f924df7 | 8514 | || SCHARS (start) == SBYTES (start)) |
df7492f9 KH |
8515 | return Qt; |
8516 | start_byte = 0; | |
8f924df7 | 8517 | end_byte = SBYTES (start); |
df7492f9 KH |
8518 | } |
8519 | else | |
d46c5b12 | 8520 | { |
df7492f9 KH |
8521 | CHECK_NUMBER_COERCE_MARKER (start); |
8522 | CHECK_NUMBER_COERCE_MARKER (end); | |
8523 | if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end)) | |
8524 | args_out_of_range (start, end); | |
4b4deea2 | 8525 | if (NILP (BVAR (current_buffer, enable_multibyte_characters))) |
df7492f9 KH |
8526 | return Qt; |
8527 | start_byte = CHAR_TO_BYTE (XINT (start)); | |
8528 | end_byte = CHAR_TO_BYTE (XINT (end)); | |
8529 | if (XINT (end) - XINT (start) == end_byte - start_byte) | |
8530 | return Qt; | |
d46c5b12 | 8531 | |
e1c23804 | 8532 | if (XINT (start) < GPT && XINT (end) > GPT) |
d46c5b12 | 8533 | { |
e1c23804 DL |
8534 | if ((GPT - XINT (start)) < (XINT (end) - GPT)) |
8535 | move_gap_both (XINT (start), start_byte); | |
df7492f9 | 8536 | else |
e1c23804 | 8537 | move_gap_both (XINT (end), end_byte); |
d46c5b12 KH |
8538 | } |
8539 | } | |
8540 | ||
df7492f9 KH |
8541 | coding_attrs_list = Qnil; |
8542 | for (tail = Vcoding_system_list; CONSP (tail); tail = XCDR (tail)) | |
8543 | if (NILP (exclude) | |
8544 | || NILP (Fmemq (XCAR (tail), exclude))) | |
8545 | { | |
8546 | Lisp_Object attrs; | |
d46c5b12 | 8547 | |
df7492f9 KH |
8548 | attrs = AREF (CODING_SYSTEM_SPEC (XCAR (tail)), 0); |
8549 | if (EQ (XCAR (tail), CODING_ATTR_BASE_NAME (attrs)) | |
8550 | && ! EQ (CODING_ATTR_TYPE (attrs), Qundecided)) | |
7d64c6ad KH |
8551 | { |
8552 | ASET (attrs, coding_attr_trans_tbl, | |
2170c8f0 | 8553 | get_translation_table (attrs, 1, NULL)); |
7d64c6ad KH |
8554 | coding_attrs_list = Fcons (attrs, coding_attrs_list); |
8555 | } | |
df7492f9 | 8556 | } |
d46c5b12 | 8557 | |
df7492f9 | 8558 | if (STRINGP (start)) |
8f924df7 | 8559 | p = pbeg = SDATA (start); |
df7492f9 KH |
8560 | else |
8561 | p = pbeg = BYTE_POS_ADDR (start_byte); | |
8562 | pend = p + (end_byte - start_byte); | |
b843d1ae | 8563 | |
df7492f9 KH |
8564 | while (p < pend && ASCII_BYTE_P (*p)) p++; |
8565 | while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--; | |
d46c5b12 | 8566 | |
0e727afa | 8567 | work_table = Fmake_char_table (Qnil, Qnil); |
05e6f5dc | 8568 | while (p < pend) |
72d1a715 | 8569 | { |
df7492f9 KH |
8570 | if (ASCII_BYTE_P (*p)) |
8571 | p++; | |
72d1a715 RS |
8572 | else |
8573 | { | |
df7492f9 | 8574 | c = STRING_CHAR_ADVANCE (p); |
0e727afa YM |
8575 | if (!NILP (char_table_ref (work_table, c))) |
8576 | /* This character was already checked. Ignore it. */ | |
8577 | continue; | |
12410ef1 | 8578 | |
df7492f9 KH |
8579 | charset_map_loaded = 0; |
8580 | for (tail = coding_attrs_list; CONSP (tail);) | |
8581 | { | |
8582 | elt = XCAR (tail); | |
8583 | if (NILP (elt)) | |
8584 | tail = XCDR (tail); | |
8585 | else if (char_encodable_p (c, elt)) | |
8586 | tail = XCDR (tail); | |
8587 | else if (CONSP (XCDR (tail))) | |
8588 | { | |
8589 | XSETCAR (tail, XCAR (XCDR (tail))); | |
8590 | XSETCDR (tail, XCDR (XCDR (tail))); | |
8591 | } | |
8592 | else | |
8593 | { | |
8594 | XSETCAR (tail, Qnil); | |
8595 | tail = XCDR (tail); | |
8596 | } | |
8597 | } | |
8598 | if (charset_map_loaded) | |
8599 | { | |
8600 | EMACS_INT p_offset = p - pbeg, pend_offset = pend - pbeg; | |
05e6f5dc | 8601 | |
df7492f9 | 8602 | if (STRINGP (start)) |
8f924df7 | 8603 | pbeg = SDATA (start); |
df7492f9 KH |
8604 | else |
8605 | pbeg = BYTE_POS_ADDR (start_byte); | |
8606 | p = pbeg + p_offset; | |
8607 | pend = pbeg + pend_offset; | |
8608 | } | |
0e727afa | 8609 | char_table_set (work_table, c, Qt); |
df7492f9 | 8610 | } |
ec6d2bb8 | 8611 | } |
fb88bf2d | 8612 | |
988b3759 | 8613 | safe_codings = list2 (Qraw_text, Qno_conversion); |
df7492f9 KH |
8614 | for (tail = coding_attrs_list; CONSP (tail); tail = XCDR (tail)) |
8615 | if (! NILP (XCAR (tail))) | |
8616 | safe_codings = Fcons (CODING_ATTR_BASE_NAME (XCAR (tail)), safe_codings); | |
ec6d2bb8 | 8617 | |
05e6f5dc KH |
8618 | return safe_codings; |
8619 | } | |
4956c225 | 8620 | |
d46c5b12 | 8621 | |
8f924df7 KH |
8622 | DEFUN ("unencodable-char-position", Funencodable_char_position, |
8623 | Sunencodable_char_position, 3, 5, 0, | |
8624 | doc: /* | |
8625 | Return position of first un-encodable character in a region. | |
d4a1d553 | 8626 | START and END specify the region and CODING-SYSTEM specifies the |
8f924df7 | 8627 | encoding to check. Return nil if CODING-SYSTEM does encode the region. |
d46c5b12 | 8628 | |
8f924df7 KH |
8629 | If optional 4th argument COUNT is non-nil, it specifies at most how |
8630 | many un-encodable characters to search. In this case, the value is a | |
8631 | list of positions. | |
d46c5b12 | 8632 | |
8f924df7 KH |
8633 | If optional 5th argument STRING is non-nil, it is a string to search |
8634 | for un-encodable characters. In that case, START and END are indexes | |
8635 | to the string. */) | |
5842a27b | 8636 | (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object count, Lisp_Object string) |
8f924df7 KH |
8637 | { |
8638 | int n; | |
8639 | struct coding_system coding; | |
7d64c6ad | 8640 | Lisp_Object attrs, charset_list, translation_table; |
8f924df7 KH |
8641 | Lisp_Object positions; |
8642 | int from, to; | |
8643 | const unsigned char *p, *stop, *pend; | |
8644 | int ascii_compatible; | |
fb88bf2d | 8645 | |
8f924df7 KH |
8646 | setup_coding_system (Fcheck_coding_system (coding_system), &coding); |
8647 | attrs = CODING_ID_ATTRS (coding.id); | |
8648 | if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text)) | |
8649 | return Qnil; | |
8650 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); | |
8651 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
2170c8f0 | 8652 | translation_table = get_translation_table (attrs, 1, NULL); |
fb88bf2d | 8653 | |
8f924df7 KH |
8654 | if (NILP (string)) |
8655 | { | |
8656 | validate_region (&start, &end); | |
8657 | from = XINT (start); | |
8658 | to = XINT (end); | |
4b4deea2 | 8659 | if (NILP (BVAR (current_buffer, enable_multibyte_characters)) |
8f924df7 KH |
8660 | || (ascii_compatible |
8661 | && (to - from) == (CHAR_TO_BYTE (to) - (CHAR_TO_BYTE (from))))) | |
8662 | return Qnil; | |
8663 | p = CHAR_POS_ADDR (from); | |
8664 | pend = CHAR_POS_ADDR (to); | |
8665 | if (from < GPT && to >= GPT) | |
8666 | stop = GPT_ADDR; | |
8667 | else | |
8668 | stop = pend; | |
8669 | } | |
8670 | else | |
8671 | { | |
8672 | CHECK_STRING (string); | |
8673 | CHECK_NATNUM (start); | |
8674 | CHECK_NATNUM (end); | |
8675 | from = XINT (start); | |
8676 | to = XINT (end); | |
8677 | if (from > to | |
8678 | || to > SCHARS (string)) | |
8679 | args_out_of_range_3 (string, start, end); | |
8680 | if (! STRING_MULTIBYTE (string)) | |
8681 | return Qnil; | |
8682 | p = SDATA (string) + string_char_to_byte (string, from); | |
8683 | stop = pend = SDATA (string) + string_char_to_byte (string, to); | |
8684 | if (ascii_compatible && (to - from) == (pend - p)) | |
8685 | return Qnil; | |
8686 | } | |
f2558efd | 8687 | |
8f924df7 KH |
8688 | if (NILP (count)) |
8689 | n = 1; | |
8690 | else | |
b73bfc1c | 8691 | { |
8f924df7 KH |
8692 | CHECK_NATNUM (count); |
8693 | n = XINT (count); | |
b73bfc1c KH |
8694 | } |
8695 | ||
8f924df7 KH |
8696 | positions = Qnil; |
8697 | while (1) | |
d46c5b12 | 8698 | { |
8f924df7 | 8699 | int c; |
ec6d2bb8 | 8700 | |
8f924df7 KH |
8701 | if (ascii_compatible) |
8702 | while (p < stop && ASCII_BYTE_P (*p)) | |
8703 | p++, from++; | |
8704 | if (p >= stop) | |
0e79d667 | 8705 | { |
8f924df7 KH |
8706 | if (p >= pend) |
8707 | break; | |
8708 | stop = pend; | |
8709 | p = GAP_END_ADDR; | |
0e79d667 | 8710 | } |
ec6d2bb8 | 8711 | |
8f924df7 KH |
8712 | c = STRING_CHAR_ADVANCE (p); |
8713 | if (! (ASCII_CHAR_P (c) && ascii_compatible) | |
7d64c6ad KH |
8714 | && ! char_charset (translate_char (translation_table, c), |
8715 | charset_list, NULL)) | |
ec6d2bb8 | 8716 | { |
8f924df7 KH |
8717 | positions = Fcons (make_number (from), positions); |
8718 | n--; | |
8719 | if (n == 0) | |
8720 | break; | |
ec6d2bb8 KH |
8721 | } |
8722 | ||
8f924df7 KH |
8723 | from++; |
8724 | } | |
d46c5b12 | 8725 | |
8f924df7 KH |
8726 | return (NILP (count) ? Fcar (positions) : Fnreverse (positions)); |
8727 | } | |
d46c5b12 | 8728 | |
d46c5b12 | 8729 | |
df7492f9 KH |
8730 | DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region, |
8731 | Scheck_coding_systems_region, 3, 3, 0, | |
8732 | doc: /* Check if the region is encodable by coding systems. | |
d46c5b12 | 8733 | |
df7492f9 KH |
8734 | START and END are buffer positions specifying the region. |
8735 | CODING-SYSTEM-LIST is a list of coding systems to check. | |
d46c5b12 | 8736 | |
df7492f9 | 8737 | The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where |
d4a1d553 | 8738 | CODING-SYSTEM is a member of CODING-SYSTEM-LIST and can't encode the |
df7492f9 KH |
8739 | whole region, POS0, POS1, ... are buffer positions where non-encodable |
8740 | characters are found. | |
93dec019 | 8741 | |
df7492f9 KH |
8742 | If all coding systems in CODING-SYSTEM-LIST can encode the region, the |
8743 | value is nil. | |
93dec019 | 8744 | |
df7492f9 KH |
8745 | START may be a string. In that case, check if the string is |
8746 | encodable, and the value contains indices to the string instead of | |
5704f39a KH |
8747 | buffer positions. END is ignored. |
8748 | ||
4c1958f4 | 8749 | If the current buffer (or START if it is a string) is unibyte, the value |
5704f39a | 8750 | is nil. */) |
5842a27b | 8751 | (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system_list) |
05e6f5dc | 8752 | { |
df7492f9 KH |
8753 | Lisp_Object list; |
8754 | EMACS_INT start_byte, end_byte; | |
8755 | int pos; | |
7c78e542 | 8756 | const unsigned char *p, *pbeg, *pend; |
df7492f9 | 8757 | int c; |
7d64c6ad | 8758 | Lisp_Object tail, elt, attrs; |
70ad9fc4 | 8759 | |
05e6f5dc KH |
8760 | if (STRINGP (start)) |
8761 | { | |
df7492f9 | 8762 | if (!STRING_MULTIBYTE (start) |
4c1958f4 | 8763 | || SCHARS (start) == SBYTES (start)) |
df7492f9 KH |
8764 | return Qnil; |
8765 | start_byte = 0; | |
8f924df7 | 8766 | end_byte = SBYTES (start); |
df7492f9 | 8767 | pos = 0; |
d46c5b12 | 8768 | } |
05e6f5dc | 8769 | else |
b73bfc1c | 8770 | { |
b7826503 PJ |
8771 | CHECK_NUMBER_COERCE_MARKER (start); |
8772 | CHECK_NUMBER_COERCE_MARKER (end); | |
05e6f5dc KH |
8773 | if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end)) |
8774 | args_out_of_range (start, end); | |
4b4deea2 | 8775 | if (NILP (BVAR (current_buffer, enable_multibyte_characters))) |
df7492f9 KH |
8776 | return Qnil; |
8777 | start_byte = CHAR_TO_BYTE (XINT (start)); | |
8778 | end_byte = CHAR_TO_BYTE (XINT (end)); | |
8779 | if (XINT (end) - XINT (start) == end_byte - start_byte) | |
5704f39a | 8780 | return Qnil; |
df7492f9 | 8781 | |
e1c23804 | 8782 | if (XINT (start) < GPT && XINT (end) > GPT) |
b73bfc1c | 8783 | { |
e1c23804 DL |
8784 | if ((GPT - XINT (start)) < (XINT (end) - GPT)) |
8785 | move_gap_both (XINT (start), start_byte); | |
df7492f9 | 8786 | else |
e1c23804 | 8787 | move_gap_both (XINT (end), end_byte); |
b73bfc1c | 8788 | } |
e1c23804 | 8789 | pos = XINT (start); |
b73bfc1c | 8790 | } |
7553d0e1 | 8791 | |
df7492f9 KH |
8792 | list = Qnil; |
8793 | for (tail = coding_system_list; CONSP (tail); tail = XCDR (tail)) | |
12410ef1 | 8794 | { |
df7492f9 | 8795 | elt = XCAR (tail); |
7d64c6ad | 8796 | attrs = AREF (CODING_SYSTEM_SPEC (elt), 0); |
2170c8f0 KH |
8797 | ASET (attrs, coding_attr_trans_tbl, |
8798 | get_translation_table (attrs, 1, NULL)); | |
7d64c6ad | 8799 | list = Fcons (Fcons (elt, Fcons (attrs, Qnil)), list); |
12410ef1 KH |
8800 | } |
8801 | ||
df7492f9 | 8802 | if (STRINGP (start)) |
8f924df7 | 8803 | p = pbeg = SDATA (start); |
72d1a715 | 8804 | else |
df7492f9 KH |
8805 | p = pbeg = BYTE_POS_ADDR (start_byte); |
8806 | pend = p + (end_byte - start_byte); | |
4ed46869 | 8807 | |
df7492f9 KH |
8808 | while (p < pend && ASCII_BYTE_P (*p)) p++, pos++; |
8809 | while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--; | |
ec6d2bb8 | 8810 | |
df7492f9 | 8811 | while (p < pend) |
d46c5b12 | 8812 | { |
df7492f9 KH |
8813 | if (ASCII_BYTE_P (*p)) |
8814 | p++; | |
e133c8fa | 8815 | else |
05e6f5dc | 8816 | { |
df7492f9 KH |
8817 | c = STRING_CHAR_ADVANCE (p); |
8818 | ||
8819 | charset_map_loaded = 0; | |
8820 | for (tail = list; CONSP (tail); tail = XCDR (tail)) | |
8821 | { | |
8822 | elt = XCDR (XCAR (tail)); | |
8823 | if (! char_encodable_p (c, XCAR (elt))) | |
8824 | XSETCDR (elt, Fcons (make_number (pos), XCDR (elt))); | |
8825 | } | |
8826 | if (charset_map_loaded) | |
8827 | { | |
8828 | EMACS_INT p_offset = p - pbeg, pend_offset = pend - pbeg; | |
8829 | ||
8830 | if (STRINGP (start)) | |
8f924df7 | 8831 | pbeg = SDATA (start); |
df7492f9 KH |
8832 | else |
8833 | pbeg = BYTE_POS_ADDR (start_byte); | |
8834 | p = pbeg + p_offset; | |
8835 | pend = pbeg + pend_offset; | |
8836 | } | |
05e6f5dc | 8837 | } |
df7492f9 | 8838 | pos++; |
d46c5b12 | 8839 | } |
4ed46869 | 8840 | |
df7492f9 KH |
8841 | tail = list; |
8842 | list = Qnil; | |
8843 | for (; CONSP (tail); tail = XCDR (tail)) | |
ec6d2bb8 | 8844 | { |
df7492f9 KH |
8845 | elt = XCAR (tail); |
8846 | if (CONSP (XCDR (XCDR (elt)))) | |
8847 | list = Fcons (Fcons (XCAR (elt), Fnreverse (XCDR (XCDR (elt)))), | |
8848 | list); | |
ec6d2bb8 | 8849 | } |
2b4f9037 | 8850 | |
df7492f9 | 8851 | return list; |
d46c5b12 KH |
8852 | } |
8853 | ||
3fd9494b | 8854 | |
b73bfc1c | 8855 | Lisp_Object |
cf84bb53 JB |
8856 | code_convert_region (Lisp_Object start, Lisp_Object end, |
8857 | Lisp_Object coding_system, Lisp_Object dst_object, | |
8858 | int encodep, int norecord) | |
4ed46869 | 8859 | { |
3a73fa5d | 8860 | struct coding_system coding; |
df7492f9 KH |
8861 | EMACS_INT from, from_byte, to, to_byte; |
8862 | Lisp_Object src_object; | |
4ed46869 | 8863 | |
b7826503 PJ |
8864 | CHECK_NUMBER_COERCE_MARKER (start); |
8865 | CHECK_NUMBER_COERCE_MARKER (end); | |
df7492f9 KH |
8866 | if (NILP (coding_system)) |
8867 | coding_system = Qno_conversion; | |
8868 | else | |
8869 | CHECK_CODING_SYSTEM (coding_system); | |
8870 | src_object = Fcurrent_buffer (); | |
8871 | if (NILP (dst_object)) | |
8872 | dst_object = src_object; | |
8873 | else if (! EQ (dst_object, Qt)) | |
8874 | CHECK_BUFFER (dst_object); | |
3a73fa5d | 8875 | |
d46c5b12 KH |
8876 | validate_region (&start, &end); |
8877 | from = XFASTINT (start); | |
df7492f9 | 8878 | from_byte = CHAR_TO_BYTE (from); |
d46c5b12 | 8879 | to = XFASTINT (end); |
df7492f9 | 8880 | to_byte = CHAR_TO_BYTE (to); |
764ca8da | 8881 | |
df7492f9 KH |
8882 | setup_coding_system (coding_system, &coding); |
8883 | coding.mode |= CODING_MODE_LAST_BLOCK; | |
ec6d2bb8 | 8884 | |
df7492f9 KH |
8885 | if (encodep) |
8886 | encode_coding_object (&coding, src_object, from, from_byte, to, to_byte, | |
8887 | dst_object); | |
8888 | else | |
8889 | decode_coding_object (&coding, src_object, from, from_byte, to, to_byte, | |
8890 | dst_object); | |
8891 | if (! norecord) | |
8892 | Vlast_coding_system_used = CODING_ID_NAME (coding.id); | |
ec6d2bb8 | 8893 | |
df7492f9 KH |
8894 | return (BUFFERP (dst_object) |
8895 | ? make_number (coding.produced_char) | |
8896 | : coding.dst_object); | |
4031e2bf | 8897 | } |
78108bcd | 8898 | |
4ed46869 | 8899 | |
4031e2bf | 8900 | DEFUN ("decode-coding-region", Fdecode_coding_region, Sdecode_coding_region, |
df7492f9 | 8901 | 3, 4, "r\nzCoding system: ", |
48b0f3ae | 8902 | doc: /* Decode the current region from the specified coding system. |
df7492f9 KH |
8903 | When called from a program, takes four arguments: |
8904 | START, END, CODING-SYSTEM, and DESTINATION. | |
8905 | START and END are buffer positions. | |
8844fa83 | 8906 | |
df7492f9 | 8907 | Optional 4th arguments DESTINATION specifies where the decoded text goes. |
2354b80b | 8908 | If nil, the region between START and END is replaced by the decoded text. |
1560f91a EZ |
8909 | If buffer, the decoded text is inserted in that buffer after point (point |
8910 | does not move). | |
446dcd75 | 8911 | In those cases, the length of the decoded text is returned. |
319a3947 | 8912 | If DESTINATION is t, the decoded text is returned. |
8844fa83 | 8913 | |
48b0f3ae PJ |
8914 | This function sets `last-coding-system-used' to the precise coding system |
8915 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
319a3947 | 8916 | not fully specified.) */) |
5842a27b | 8917 | (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object destination) |
4031e2bf | 8918 | { |
df7492f9 | 8919 | return code_convert_region (start, end, coding_system, destination, 0, 0); |
3a73fa5d | 8920 | } |
8844fa83 | 8921 | |
3a73fa5d | 8922 | DEFUN ("encode-coding-region", Fencode_coding_region, Sencode_coding_region, |
df7492f9 KH |
8923 | 3, 4, "r\nzCoding system: ", |
8924 | doc: /* Encode the current region by specified coding system. | |
d4a1d553 JB |
8925 | When called from a program, takes four arguments: |
8926 | START, END, CODING-SYSTEM and DESTINATION. | |
8927 | START and END are buffer positions. | |
d46c5b12 | 8928 | |
df7492f9 KH |
8929 | Optional 4th arguments DESTINATION specifies where the encoded text goes. |
8930 | If nil, the region between START and END is replace by the encoded text. | |
1560f91a EZ |
8931 | If buffer, the encoded text is inserted in that buffer after point (point |
8932 | does not move). | |
446dcd75 | 8933 | In those cases, the length of the encoded text is returned. |
319a3947 | 8934 | If DESTINATION is t, the encoded text is returned. |
2391eaa4 | 8935 | |
48b0f3ae PJ |
8936 | This function sets `last-coding-system-used' to the precise coding system |
8937 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
319a3947 | 8938 | not fully specified.) */) |
5842a27b | 8939 | (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object destination) |
3a73fa5d | 8940 | { |
df7492f9 | 8941 | return code_convert_region (start, end, coding_system, destination, 1, 0); |
b73bfc1c KH |
8942 | } |
8943 | ||
8944 | Lisp_Object | |
6f704c76 DN |
8945 | code_convert_string (Lisp_Object string, Lisp_Object coding_system, |
8946 | Lisp_Object dst_object, int encodep, int nocopy, int norecord) | |
b73bfc1c | 8947 | { |
4031e2bf | 8948 | struct coding_system coding; |
df7492f9 | 8949 | EMACS_INT chars, bytes; |
ec6d2bb8 | 8950 | |
b7826503 | 8951 | CHECK_STRING (string); |
d46c5b12 | 8952 | if (NILP (coding_system)) |
4956c225 | 8953 | { |
df7492f9 KH |
8954 | if (! norecord) |
8955 | Vlast_coding_system_used = Qno_conversion; | |
8956 | if (NILP (dst_object)) | |
8957 | return (nocopy ? Fcopy_sequence (string) : string); | |
4956c225 | 8958 | } |
b73bfc1c | 8959 | |
df7492f9 KH |
8960 | if (NILP (coding_system)) |
8961 | coding_system = Qno_conversion; | |
8962 | else | |
8963 | CHECK_CODING_SYSTEM (coding_system); | |
8964 | if (NILP (dst_object)) | |
8965 | dst_object = Qt; | |
8966 | else if (! EQ (dst_object, Qt)) | |
8967 | CHECK_BUFFER (dst_object); | |
73be902c | 8968 | |
df7492f9 | 8969 | setup_coding_system (coding_system, &coding); |
d46c5b12 | 8970 | coding.mode |= CODING_MODE_LAST_BLOCK; |
8f924df7 KH |
8971 | chars = SCHARS (string); |
8972 | bytes = SBYTES (string); | |
df7492f9 KH |
8973 | if (encodep) |
8974 | encode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object); | |
8975 | else | |
8976 | decode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object); | |
8977 | if (! norecord) | |
8978 | Vlast_coding_system_used = CODING_ID_NAME (coding.id); | |
73be902c | 8979 | |
df7492f9 KH |
8980 | return (BUFFERP (dst_object) |
8981 | ? make_number (coding.produced_char) | |
8982 | : coding.dst_object); | |
4ed46869 | 8983 | } |
73be902c | 8984 | |
b73bfc1c | 8985 | |
ecec61c1 | 8986 | /* Encode or decode STRING according to CODING_SYSTEM. |
ec6d2bb8 | 8987 | Do not set Vlast_coding_system_used. |
4ed46869 | 8988 | |
ec6d2bb8 KH |
8989 | This function is called only from macros DECODE_FILE and |
8990 | ENCODE_FILE, thus we ignore character composition. */ | |
4ed46869 | 8991 | |
ecec61c1 | 8992 | Lisp_Object |
cf84bb53 JB |
8993 | code_convert_string_norecord (Lisp_Object string, Lisp_Object coding_system, |
8994 | int encodep) | |
4ed46869 | 8995 | { |
0be8721c | 8996 | return code_convert_string (string, coding_system, Qt, encodep, 0, 1); |
4ed46869 KH |
8997 | } |
8998 | ||
4ed46869 | 8999 | |
df7492f9 KH |
9000 | DEFUN ("decode-coding-string", Fdecode_coding_string, Sdecode_coding_string, |
9001 | 2, 4, 0, | |
9002 | doc: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result. | |
9003 | ||
9004 | Optional third arg NOCOPY non-nil means it is OK to return STRING itself | |
9005 | if the decoding operation is trivial. | |
ecec61c1 | 9006 | |
d4a1d553 | 9007 | Optional fourth arg BUFFER non-nil means that the decoded text is |
1560f91a EZ |
9008 | inserted in that buffer after point (point does not move). In this |
9009 | case, the return value is the length of the decoded text. | |
ecec61c1 | 9010 | |
df7492f9 KH |
9011 | This function sets `last-coding-system-used' to the precise coding system |
9012 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
d4a1d553 | 9013 | not fully specified.) */) |
5842a27b | 9014 | (Lisp_Object string, Lisp_Object coding_system, Lisp_Object nocopy, Lisp_Object buffer) |
4ed46869 | 9015 | { |
df7492f9 KH |
9016 | return code_convert_string (string, coding_system, buffer, |
9017 | 0, ! NILP (nocopy), 0); | |
4ed46869 KH |
9018 | } |
9019 | ||
df7492f9 KH |
9020 | DEFUN ("encode-coding-string", Fencode_coding_string, Sencode_coding_string, |
9021 | 2, 4, 0, | |
9022 | doc: /* Encode STRING to CODING-SYSTEM, and return the result. | |
9023 | ||
9024 | Optional third arg NOCOPY non-nil means it is OK to return STRING | |
9025 | itself if the encoding operation is trivial. | |
9026 | ||
d4a1d553 | 9027 | Optional fourth arg BUFFER non-nil means that the encoded text is |
1560f91a EZ |
9028 | inserted in that buffer after point (point does not move). In this |
9029 | case, the return value is the length of the encoded text. | |
df7492f9 KH |
9030 | |
9031 | This function sets `last-coding-system-used' to the precise coding system | |
9032 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
9033 | not fully specified.) */) | |
5842a27b | 9034 | (Lisp_Object string, Lisp_Object coding_system, Lisp_Object nocopy, Lisp_Object buffer) |
4ed46869 | 9035 | { |
df7492f9 | 9036 | return code_convert_string (string, coding_system, buffer, |
c197f191 | 9037 | 1, ! NILP (nocopy), 1); |
4ed46869 | 9038 | } |
df7492f9 | 9039 | |
3a73fa5d | 9040 | \f |
4ed46869 | 9041 | DEFUN ("decode-sjis-char", Fdecode_sjis_char, Sdecode_sjis_char, 1, 1, 0, |
48b0f3ae PJ |
9042 | doc: /* Decode a Japanese character which has CODE in shift_jis encoding. |
9043 | Return the corresponding character. */) | |
5842a27b | 9044 | (Lisp_Object code) |
4ed46869 | 9045 | { |
df7492f9 KH |
9046 | Lisp_Object spec, attrs, val; |
9047 | struct charset *charset_roman, *charset_kanji, *charset_kana, *charset; | |
9048 | int c; | |
4ed46869 | 9049 | |
df7492f9 KH |
9050 | CHECK_NATNUM (code); |
9051 | c = XFASTINT (code); | |
9052 | CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec); | |
9053 | attrs = AREF (spec, 0); | |
4ed46869 | 9054 | |
df7492f9 KH |
9055 | if (ASCII_BYTE_P (c) |
9056 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
9057 | return code; | |
4ed46869 | 9058 | |
df7492f9 KH |
9059 | val = CODING_ATTR_CHARSET_LIST (attrs); |
9060 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
004068e4 KH |
9061 | charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
9062 | charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))); | |
fa42c37f | 9063 | |
df7492f9 KH |
9064 | if (c <= 0x7F) |
9065 | charset = charset_roman; | |
9066 | else if (c >= 0xA0 && c < 0xDF) | |
55ab7be3 | 9067 | { |
df7492f9 KH |
9068 | charset = charset_kana; |
9069 | c -= 0x80; | |
4ed46869 | 9070 | } |
55ab7be3 | 9071 | else |
4ed46869 | 9072 | { |
2735d060 | 9073 | int c1 = c >> 8, c2 = c & 0xFF; |
df7492f9 | 9074 | |
2735d060 PE |
9075 | if (c1 < 0x81 || (c1 > 0x9F && c1 < 0xE0) || c1 > 0xEF |
9076 | || c2 < 0x40 || c2 == 0x7F || c2 > 0xFC) | |
df7492f9 KH |
9077 | error ("Invalid code: %d", code); |
9078 | SJIS_TO_JIS (c); | |
9079 | charset = charset_kanji; | |
4ed46869 | 9080 | } |
df7492f9 KH |
9081 | c = DECODE_CHAR (charset, c); |
9082 | if (c < 0) | |
9083 | error ("Invalid code: %d", code); | |
9084 | return make_number (c); | |
93dec019 | 9085 | } |
4ed46869 | 9086 | |
48b0f3ae | 9087 | |
4ed46869 | 9088 | DEFUN ("encode-sjis-char", Fencode_sjis_char, Sencode_sjis_char, 1, 1, 0, |
8acf0c0e | 9089 | doc: /* Encode a Japanese character CH to shift_jis encoding. |
48b0f3ae | 9090 | Return the corresponding code in SJIS. */) |
5842a27b | 9091 | (Lisp_Object ch) |
4ed46869 | 9092 | { |
df7492f9 KH |
9093 | Lisp_Object spec, attrs, charset_list; |
9094 | int c; | |
9095 | struct charset *charset; | |
9096 | unsigned code; | |
48b0f3ae | 9097 | |
df7492f9 KH |
9098 | CHECK_CHARACTER (ch); |
9099 | c = XFASTINT (ch); | |
9100 | CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec); | |
9101 | attrs = AREF (spec, 0); | |
9102 | ||
9103 | if (ASCII_CHAR_P (c) | |
9104 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
9105 | return ch; | |
9106 | ||
9107 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
9108 | charset = char_charset (c, charset_list, &code); | |
9109 | if (code == CHARSET_INVALID_CODE (charset)) | |
9110 | error ("Can't encode by shift_jis encoding: %d", c); | |
9111 | JIS_TO_SJIS (code); | |
9112 | ||
9113 | return make_number (code); | |
4ed46869 KH |
9114 | } |
9115 | ||
9116 | DEFUN ("decode-big5-char", Fdecode_big5_char, Sdecode_big5_char, 1, 1, 0, | |
48b0f3ae PJ |
9117 | doc: /* Decode a Big5 character which has CODE in BIG5 coding system. |
9118 | Return the corresponding character. */) | |
5842a27b | 9119 | (Lisp_Object code) |
d46c5b12 | 9120 | { |
df7492f9 KH |
9121 | Lisp_Object spec, attrs, val; |
9122 | struct charset *charset_roman, *charset_big5, *charset; | |
9123 | int c; | |
6289dd10 | 9124 | |
df7492f9 KH |
9125 | CHECK_NATNUM (code); |
9126 | c = XFASTINT (code); | |
9127 | CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec); | |
9128 | attrs = AREF (spec, 0); | |
4ed46869 | 9129 | |
df7492f9 KH |
9130 | if (ASCII_BYTE_P (c) |
9131 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
9132 | return code; | |
6289dd10 | 9133 | |
df7492f9 KH |
9134 | val = CODING_ATTR_CHARSET_LIST (attrs); |
9135 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
9136 | charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val))); | |
c210f766 | 9137 | |
df7492f9 KH |
9138 | if (c <= 0x7F) |
9139 | charset = charset_roman; | |
c28a9453 KH |
9140 | else |
9141 | { | |
df7492f9 KH |
9142 | int b1 = c >> 8, b2 = c & 0x7F; |
9143 | if (b1 < 0xA1 || b1 > 0xFE | |
9144 | || b2 < 0x40 || (b2 > 0x7E && b2 < 0xA1) || b2 > 0xFE) | |
9145 | error ("Invalid code: %d", code); | |
9146 | charset = charset_big5; | |
c28a9453 | 9147 | } |
df7492f9 KH |
9148 | c = DECODE_CHAR (charset, (unsigned )c); |
9149 | if (c < 0) | |
9150 | error ("Invalid code: %d", code); | |
9151 | return make_number (c); | |
d46c5b12 | 9152 | } |
6289dd10 | 9153 | |
4ed46869 | 9154 | DEFUN ("encode-big5-char", Fencode_big5_char, Sencode_big5_char, 1, 1, 0, |
8acf0c0e | 9155 | doc: /* Encode the Big5 character CH to BIG5 coding system. |
48b0f3ae | 9156 | Return the corresponding character code in Big5. */) |
5842a27b | 9157 | (Lisp_Object ch) |
4ed46869 | 9158 | { |
df7492f9 KH |
9159 | Lisp_Object spec, attrs, charset_list; |
9160 | struct charset *charset; | |
9161 | int c; | |
9162 | unsigned code; | |
9163 | ||
9164 | CHECK_CHARACTER (ch); | |
9165 | c = XFASTINT (ch); | |
9166 | CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec); | |
9167 | attrs = AREF (spec, 0); | |
9168 | if (ASCII_CHAR_P (c) | |
9169 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
9170 | return ch; | |
9171 | ||
9172 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
9173 | charset = char_charset (c, charset_list, &code); | |
9174 | if (code == CHARSET_INVALID_CODE (charset)) | |
9175 | error ("Can't encode by Big5 encoding: %d", c); | |
9176 | ||
9177 | return make_number (code); | |
4ed46869 | 9178 | } |
48b0f3ae | 9179 | |
3a73fa5d | 9180 | \f |
002fdb44 | 9181 | DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal, |
68bba4e4 | 9182 | Sset_terminal_coding_system_internal, 1, 2, 0, |
48b0f3ae | 9183 | doc: /* Internal use only. */) |
5842a27b | 9184 | (Lisp_Object coding_system, Lisp_Object terminal) |
4ed46869 | 9185 | { |
b18fad6d KH |
9186 | struct terminal *term = get_terminal (terminal, 1); |
9187 | struct coding_system *terminal_coding = TERMINAL_TERMINAL_CODING (term); | |
b7826503 | 9188 | CHECK_SYMBOL (coding_system); |
b8299c66 | 9189 | setup_coding_system (Fcheck_coding_system (coding_system), terminal_coding); |
70c22245 | 9190 | /* We had better not send unsafe characters to terminal. */ |
c73bd236 | 9191 | terminal_coding->mode |= CODING_MODE_SAFE_ENCODING; |
ad1746f5 | 9192 | /* Character composition should be disabled. */ |
c73bd236 | 9193 | terminal_coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK; |
b8299c66 KL |
9194 | terminal_coding->src_multibyte = 1; |
9195 | terminal_coding->dst_multibyte = 0; | |
b18fad6d KH |
9196 | if (terminal_coding->common_flags & CODING_REQUIRE_ENCODING_MASK) |
9197 | term->charset_list = coding_charset_list (terminal_coding); | |
9198 | else | |
6b4bb703 | 9199 | term->charset_list = Fcons (make_number (charset_ascii), Qnil); |
4ed46869 KH |
9200 | return Qnil; |
9201 | } | |
9202 | ||
c4825358 KH |
9203 | DEFUN ("set-safe-terminal-coding-system-internal", |
9204 | Fset_safe_terminal_coding_system_internal, | |
48b0f3ae | 9205 | Sset_safe_terminal_coding_system_internal, 1, 1, 0, |
ddb67bdc | 9206 | doc: /* Internal use only. */) |
5842a27b | 9207 | (Lisp_Object coding_system) |
d46c5b12 | 9208 | { |
b7826503 | 9209 | CHECK_SYMBOL (coding_system); |
c4825358 KH |
9210 | setup_coding_system (Fcheck_coding_system (coding_system), |
9211 | &safe_terminal_coding); | |
ad1746f5 | 9212 | /* Character composition should be disabled. */ |
df7492f9 | 9213 | safe_terminal_coding.common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK; |
b73bfc1c KH |
9214 | safe_terminal_coding.src_multibyte = 1; |
9215 | safe_terminal_coding.dst_multibyte = 0; | |
c4825358 KH |
9216 | return Qnil; |
9217 | } | |
4ed46869 | 9218 | |
002fdb44 | 9219 | DEFUN ("terminal-coding-system", Fterminal_coding_system, |
68bba4e4 | 9220 | Sterminal_coding_system, 0, 1, 0, |
6ed8eeff | 9221 | doc: /* Return coding system specified for terminal output on the given terminal. |
708e05dc | 9222 | TERMINAL may be a terminal object, a frame, or nil for the selected |
6ed8eeff | 9223 | frame's terminal device. */) |
5842a27b | 9224 | (Lisp_Object terminal) |
4ed46869 | 9225 | { |
985773c9 MB |
9226 | struct coding_system *terminal_coding |
9227 | = TERMINAL_TERMINAL_CODING (get_terminal (terminal, 1)); | |
9228 | Lisp_Object coding_system = CODING_ID_NAME (terminal_coding->id); | |
ae6f73fa | 9229 | |
ae6f73fa | 9230 | /* For backward compatibility, return nil if it is `undecided'. */ |
f75c90a9 | 9231 | return (! EQ (coding_system, Qundecided) ? coding_system : Qnil); |
4ed46869 KH |
9232 | } |
9233 | ||
002fdb44 | 9234 | DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal, |
68bba4e4 | 9235 | Sset_keyboard_coding_system_internal, 1, 2, 0, |
48b0f3ae | 9236 | doc: /* Internal use only. */) |
5842a27b | 9237 | (Lisp_Object coding_system, Lisp_Object terminal) |
4ed46869 | 9238 | { |
6ed8eeff | 9239 | struct terminal *t = get_terminal (terminal, 1); |
b7826503 | 9240 | CHECK_SYMBOL (coding_system); |
624bda09 KH |
9241 | if (NILP (coding_system)) |
9242 | coding_system = Qno_conversion; | |
9243 | else | |
9244 | Fcheck_coding_system (coding_system); | |
9245 | setup_coding_system (coding_system, TERMINAL_KEYBOARD_CODING (t)); | |
ad1746f5 | 9246 | /* Character composition should be disabled. */ |
c73bd236 MB |
9247 | TERMINAL_KEYBOARD_CODING (t)->common_flags |
9248 | &= ~CODING_ANNOTATE_COMPOSITION_MASK; | |
4ed46869 KH |
9249 | return Qnil; |
9250 | } | |
9251 | ||
9252 | DEFUN ("keyboard-coding-system", | |
985773c9 | 9253 | Fkeyboard_coding_system, Skeyboard_coding_system, 0, 1, 0, |
48b0f3ae | 9254 | doc: /* Return coding system specified for decoding keyboard input. */) |
5842a27b | 9255 | (Lisp_Object terminal) |
4ed46869 | 9256 | { |
985773c9 MB |
9257 | return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING |
9258 | (get_terminal (terminal, 1))->id); | |
4ed46869 KH |
9259 | } |
9260 | ||
4ed46869 | 9261 | \f |
a5d301df KH |
9262 | DEFUN ("find-operation-coding-system", Ffind_operation_coding_system, |
9263 | Sfind_operation_coding_system, 1, MANY, 0, | |
48b0f3ae PJ |
9264 | doc: /* Choose a coding system for an operation based on the target name. |
9265 | The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM). | |
9266 | DECODING-SYSTEM is the coding system to use for decoding | |
9267 | \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system | |
9268 | for encoding (in case OPERATION does encoding). | |
05e6f5dc | 9269 | |
48b0f3ae PJ |
9270 | The first argument OPERATION specifies an I/O primitive: |
9271 | For file I/O, `insert-file-contents' or `write-region'. | |
9272 | For process I/O, `call-process', `call-process-region', or `start-process'. | |
9273 | For network I/O, `open-network-stream'. | |
05e6f5dc | 9274 | |
48b0f3ae PJ |
9275 | The remaining arguments should be the same arguments that were passed |
9276 | to the primitive. Depending on which primitive, one of those arguments | |
9277 | is selected as the TARGET. For example, if OPERATION does file I/O, | |
9278 | whichever argument specifies the file name is TARGET. | |
05e6f5dc | 9279 | |
48b0f3ae | 9280 | TARGET has a meaning which depends on OPERATION: |
b883cdb2 | 9281 | For file I/O, TARGET is a file name (except for the special case below). |
48b0f3ae | 9282 | For process I/O, TARGET is a process name. |
d4a1d553 | 9283 | For network I/O, TARGET is a service name or a port number. |
05e6f5dc | 9284 | |
d4a1d553 | 9285 | This function looks up what is specified for TARGET in |
48b0f3ae PJ |
9286 | `file-coding-system-alist', `process-coding-system-alist', |
9287 | or `network-coding-system-alist' depending on OPERATION. | |
9288 | They may specify a coding system, a cons of coding systems, | |
9289 | or a function symbol to call. | |
9290 | In the last case, we call the function with one argument, | |
9291 | which is a list of all the arguments given to this function. | |
1011c487 MB |
9292 | If the function can't decide a coding system, it can return |
9293 | `undecided' so that the normal code-detection is performed. | |
48b0f3ae | 9294 | |
b883cdb2 MB |
9295 | If OPERATION is `insert-file-contents', the argument corresponding to |
9296 | TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a | |
9297 | file name to look up, and BUFFER is a buffer that contains the file's | |
9298 | contents (not yet decoded). If `file-coding-system-alist' specifies a | |
9299 | function to call for FILENAME, that function should examine the | |
9300 | contents of BUFFER instead of reading the file. | |
9301 | ||
d918f936 | 9302 | usage: (find-operation-coding-system OPERATION ARGUMENTS...) */) |
c5101a77 | 9303 | (size_t nargs, Lisp_Object *args) |
6b89e3aa | 9304 | { |
4ed46869 KH |
9305 | Lisp_Object operation, target_idx, target, val; |
9306 | register Lisp_Object chain; | |
177c0ea7 | 9307 | |
4ed46869 KH |
9308 | if (nargs < 2) |
9309 | error ("Too few arguments"); | |
9310 | operation = args[0]; | |
9311 | if (!SYMBOLP (operation) | |
c5101a77 | 9312 | || !NATNUMP (target_idx = Fget (operation, Qtarget_idx))) |
3ed051d4 | 9313 | error ("Invalid first argument"); |
c5101a77 | 9314 | if (nargs < 1 + XFASTINT (target_idx)) |
4ed46869 | 9315 | error ("Too few arguments for operation: %s", |
8f924df7 | 9316 | SDATA (SYMBOL_NAME (operation))); |
c5101a77 | 9317 | target = args[XFASTINT (target_idx) + 1]; |
4ed46869 | 9318 | if (!(STRINGP (target) |
091a0ff0 KH |
9319 | || (EQ (operation, Qinsert_file_contents) && CONSP (target) |
9320 | && STRINGP (XCAR (target)) && BUFFERP (XCDR (target))) | |
4ed46869 | 9321 | || (EQ (operation, Qopen_network_stream) && INTEGERP (target)))) |
c5101a77 | 9322 | error ("Invalid %dth argument", XFASTINT (target_idx) + 1); |
091a0ff0 KH |
9323 | if (CONSP (target)) |
9324 | target = XCAR (target); | |
4ed46869 | 9325 | |
2e34157c RS |
9326 | chain = ((EQ (operation, Qinsert_file_contents) |
9327 | || EQ (operation, Qwrite_region)) | |
02ba4723 | 9328 | ? Vfile_coding_system_alist |
2e34157c | 9329 | : (EQ (operation, Qopen_network_stream) |
02ba4723 KH |
9330 | ? Vnetwork_coding_system_alist |
9331 | : Vprocess_coding_system_alist)); | |
4ed46869 KH |
9332 | if (NILP (chain)) |
9333 | return Qnil; | |
9334 | ||
03699b14 | 9335 | for (; CONSP (chain); chain = XCDR (chain)) |
6b89e3aa | 9336 | { |
f44d27ce | 9337 | Lisp_Object elt; |
6b89e3aa | 9338 | |
df7492f9 | 9339 | elt = XCAR (chain); |
4ed46869 KH |
9340 | if (CONSP (elt) |
9341 | && ((STRINGP (target) | |
03699b14 KR |
9342 | && STRINGP (XCAR (elt)) |
9343 | && fast_string_match (XCAR (elt), target) >= 0) | |
9344 | || (INTEGERP (target) && EQ (target, XCAR (elt))))) | |
6b89e3aa | 9345 | { |
03699b14 | 9346 | val = XCDR (elt); |
b19fd4c5 KH |
9347 | /* Here, if VAL is both a valid coding system and a valid |
9348 | function symbol, we return VAL as a coding system. */ | |
02ba4723 KH |
9349 | if (CONSP (val)) |
9350 | return val; | |
9351 | if (! SYMBOLP (val)) | |
9352 | return Qnil; | |
9353 | if (! NILP (Fcoding_system_p (val))) | |
9354 | return Fcons (val, val); | |
b19fd4c5 | 9355 | if (! NILP (Ffboundp (val))) |
6b89e3aa | 9356 | { |
e2b97060 MB |
9357 | /* We use call1 rather than safe_call1 |
9358 | so as to get bug reports about functions called here | |
9359 | which don't handle the current interface. */ | |
9360 | val = call1 (val, Flist (nargs, args)); | |
b19fd4c5 KH |
9361 | if (CONSP (val)) |
9362 | return val; | |
9363 | if (SYMBOLP (val) && ! NILP (Fcoding_system_p (val))) | |
9364 | return Fcons (val, val); | |
6b89e3aa | 9365 | } |
02ba4723 | 9366 | return Qnil; |
6b89e3aa KH |
9367 | } |
9368 | } | |
4ed46869 | 9369 | return Qnil; |
6b89e3aa KH |
9370 | } |
9371 | ||
df7492f9 | 9372 | DEFUN ("set-coding-system-priority", Fset_coding_system_priority, |
a3f6ee6d | 9373 | Sset_coding_system_priority, 0, MANY, 0, |
da7db224 | 9374 | doc: /* Assign higher priority to the coding systems given as arguments. |
d4a1d553 | 9375 | If multiple coding systems belong to the same category, |
a3181084 DL |
9376 | all but the first one are ignored. |
9377 | ||
d4a1d553 | 9378 | usage: (set-coding-system-priority &rest coding-systems) */) |
c5101a77 | 9379 | (size_t nargs, Lisp_Object *args) |
df7492f9 | 9380 | { |
c5101a77 | 9381 | size_t i, j; |
df7492f9 KH |
9382 | int changed[coding_category_max]; |
9383 | enum coding_category priorities[coding_category_max]; | |
9384 | ||
72af86bd | 9385 | memset (changed, 0, sizeof changed); |
6b89e3aa | 9386 | |
df7492f9 | 9387 | for (i = j = 0; i < nargs; i++) |
6b89e3aa | 9388 | { |
df7492f9 KH |
9389 | enum coding_category category; |
9390 | Lisp_Object spec, attrs; | |
6b89e3aa | 9391 | |
df7492f9 KH |
9392 | CHECK_CODING_SYSTEM_GET_SPEC (args[i], spec); |
9393 | attrs = AREF (spec, 0); | |
9394 | category = XINT (CODING_ATTR_CATEGORY (attrs)); | |
9395 | if (changed[category]) | |
9396 | /* Ignore this coding system because a coding system of the | |
9397 | same category already had a higher priority. */ | |
9398 | continue; | |
9399 | changed[category] = 1; | |
9400 | priorities[j++] = category; | |
9401 | if (coding_categories[category].id >= 0 | |
9402 | && ! EQ (args[i], CODING_ID_NAME (coding_categories[category].id))) | |
9403 | setup_coding_system (args[i], &coding_categories[category]); | |
ff563fce | 9404 | Fset (AREF (Vcoding_category_table, category), args[i]); |
df7492f9 | 9405 | } |
6b89e3aa | 9406 | |
df7492f9 KH |
9407 | /* Now we have decided top J priorities. Reflect the order of the |
9408 | original priorities to the remaining priorities. */ | |
6b89e3aa | 9409 | |
df7492f9 | 9410 | for (i = j, j = 0; i < coding_category_max; i++, j++) |
6b89e3aa | 9411 | { |
df7492f9 KH |
9412 | while (j < coding_category_max |
9413 | && changed[coding_priorities[j]]) | |
9414 | j++; | |
9415 | if (j == coding_category_max) | |
9416 | abort (); | |
9417 | priorities[i] = coding_priorities[j]; | |
9418 | } | |
6b89e3aa | 9419 | |
72af86bd | 9420 | memcpy (coding_priorities, priorities, sizeof priorities); |
177c0ea7 | 9421 | |
ff563fce KH |
9422 | /* Update `coding-category-list'. */ |
9423 | Vcoding_category_list = Qnil; | |
c5101a77 | 9424 | for (i = coding_category_max; i-- > 0; ) |
ff563fce KH |
9425 | Vcoding_category_list |
9426 | = Fcons (AREF (Vcoding_category_table, priorities[i]), | |
9427 | Vcoding_category_list); | |
6b89e3aa | 9428 | |
df7492f9 | 9429 | return Qnil; |
6b89e3aa KH |
9430 | } |
9431 | ||
df7492f9 KH |
9432 | DEFUN ("coding-system-priority-list", Fcoding_system_priority_list, |
9433 | Scoding_system_priority_list, 0, 1, 0, | |
da7db224 | 9434 | doc: /* Return a list of coding systems ordered by their priorities. |
b811c52b KH |
9435 | The list contains a subset of coding systems; i.e. coding systems |
9436 | assigned to each coding category (see `coding-category-list'). | |
9437 | ||
da7db224 | 9438 | HIGHESTP non-nil means just return the highest priority one. */) |
5842a27b | 9439 | (Lisp_Object highestp) |
d46c5b12 KH |
9440 | { |
9441 | int i; | |
df7492f9 | 9442 | Lisp_Object val; |
6b89e3aa | 9443 | |
df7492f9 | 9444 | for (i = 0, val = Qnil; i < coding_category_max; i++) |
d46c5b12 | 9445 | { |
df7492f9 KH |
9446 | enum coding_category category = coding_priorities[i]; |
9447 | int id = coding_categories[category].id; | |
9448 | Lisp_Object attrs; | |
068a9dbd | 9449 | |
df7492f9 KH |
9450 | if (id < 0) |
9451 | continue; | |
9452 | attrs = CODING_ID_ATTRS (id); | |
9453 | if (! NILP (highestp)) | |
9454 | return CODING_ATTR_BASE_NAME (attrs); | |
9455 | val = Fcons (CODING_ATTR_BASE_NAME (attrs), val); | |
9456 | } | |
9457 | return Fnreverse (val); | |
9458 | } | |
068a9dbd | 9459 | |
91433552 | 9460 | static const char *const suffixes[] = { "-unix", "-dos", "-mac" }; |
068a9dbd KH |
9461 | |
9462 | static Lisp_Object | |
971de7fb | 9463 | make_subsidiaries (Lisp_Object base) |
068a9dbd | 9464 | { |
df7492f9 | 9465 | Lisp_Object subsidiaries; |
8f924df7 | 9466 | int base_name_len = SBYTES (SYMBOL_NAME (base)); |
df7492f9 KH |
9467 | char *buf = (char *) alloca (base_name_len + 6); |
9468 | int i; | |
068a9dbd | 9469 | |
72af86bd | 9470 | memcpy (buf, SDATA (SYMBOL_NAME (base)), base_name_len); |
df7492f9 KH |
9471 | subsidiaries = Fmake_vector (make_number (3), Qnil); |
9472 | for (i = 0; i < 3; i++) | |
068a9dbd | 9473 | { |
72af86bd | 9474 | memcpy (buf + base_name_len, suffixes[i], strlen (suffixes[i]) + 1); |
df7492f9 | 9475 | ASET (subsidiaries, i, intern (buf)); |
068a9dbd | 9476 | } |
df7492f9 | 9477 | return subsidiaries; |
068a9dbd KH |
9478 | } |
9479 | ||
9480 | ||
df7492f9 KH |
9481 | DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal, |
9482 | Sdefine_coding_system_internal, coding_arg_max, MANY, 0, | |
1fcd6c8b DL |
9483 | doc: /* For internal use only. |
9484 | usage: (define-coding-system-internal ...) */) | |
c5101a77 | 9485 | (size_t nargs, Lisp_Object *args) |
068a9dbd | 9486 | { |
df7492f9 KH |
9487 | Lisp_Object name; |
9488 | Lisp_Object spec_vec; /* [ ATTRS ALIASE EOL_TYPE ] */ | |
9489 | Lisp_Object attrs; /* Vector of attributes. */ | |
9490 | Lisp_Object eol_type; | |
9491 | Lisp_Object aliases; | |
9492 | Lisp_Object coding_type, charset_list, safe_charsets; | |
9493 | enum coding_category category; | |
9494 | Lisp_Object tail, val; | |
9495 | int max_charset_id = 0; | |
9496 | int i; | |
068a9dbd | 9497 | |
df7492f9 KH |
9498 | if (nargs < coding_arg_max) |
9499 | goto short_args; | |
068a9dbd | 9500 | |
df7492f9 | 9501 | attrs = Fmake_vector (make_number (coding_attr_last_index), Qnil); |
068a9dbd | 9502 | |
df7492f9 KH |
9503 | name = args[coding_arg_name]; |
9504 | CHECK_SYMBOL (name); | |
9505 | CODING_ATTR_BASE_NAME (attrs) = name; | |
068a9dbd | 9506 | |
df7492f9 KH |
9507 | val = args[coding_arg_mnemonic]; |
9508 | if (! STRINGP (val)) | |
9509 | CHECK_CHARACTER (val); | |
9510 | CODING_ATTR_MNEMONIC (attrs) = val; | |
068a9dbd | 9511 | |
df7492f9 KH |
9512 | coding_type = args[coding_arg_coding_type]; |
9513 | CHECK_SYMBOL (coding_type); | |
9514 | CODING_ATTR_TYPE (attrs) = coding_type; | |
068a9dbd | 9515 | |
df7492f9 KH |
9516 | charset_list = args[coding_arg_charset_list]; |
9517 | if (SYMBOLP (charset_list)) | |
9518 | { | |
9519 | if (EQ (charset_list, Qiso_2022)) | |
9520 | { | |
9521 | if (! EQ (coding_type, Qiso_2022)) | |
9522 | error ("Invalid charset-list"); | |
9523 | charset_list = Viso_2022_charset_list; | |
9524 | } | |
9525 | else if (EQ (charset_list, Qemacs_mule)) | |
9526 | { | |
9527 | if (! EQ (coding_type, Qemacs_mule)) | |
9528 | error ("Invalid charset-list"); | |
9529 | charset_list = Vemacs_mule_charset_list; | |
9530 | } | |
9531 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) | |
9532 | if (max_charset_id < XFASTINT (XCAR (tail))) | |
9533 | max_charset_id = XFASTINT (XCAR (tail)); | |
9534 | } | |
068a9dbd KH |
9535 | else |
9536 | { | |
df7492f9 | 9537 | charset_list = Fcopy_sequence (charset_list); |
985773c9 | 9538 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) |
068a9dbd | 9539 | { |
df7492f9 KH |
9540 | struct charset *charset; |
9541 | ||
985773c9 | 9542 | val = XCAR (tail); |
df7492f9 KH |
9543 | CHECK_CHARSET_GET_CHARSET (val, charset); |
9544 | if (EQ (coding_type, Qiso_2022) | |
9545 | ? CHARSET_ISO_FINAL (charset) < 0 | |
9546 | : EQ (coding_type, Qemacs_mule) | |
9547 | ? CHARSET_EMACS_MULE_ID (charset) < 0 | |
9548 | : 0) | |
9549 | error ("Can't handle charset `%s'", | |
8f924df7 | 9550 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
df7492f9 | 9551 | |
8f924df7 | 9552 | XSETCAR (tail, make_number (charset->id)); |
df7492f9 KH |
9553 | if (max_charset_id < charset->id) |
9554 | max_charset_id = charset->id; | |
068a9dbd KH |
9555 | } |
9556 | } | |
df7492f9 | 9557 | CODING_ATTR_CHARSET_LIST (attrs) = charset_list; |
068a9dbd | 9558 | |
1b3b981b AS |
9559 | safe_charsets = make_uninit_string (max_charset_id + 1); |
9560 | memset (SDATA (safe_charsets), 255, max_charset_id + 1); | |
df7492f9 | 9561 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) |
8f924df7 | 9562 | SSET (safe_charsets, XFASTINT (XCAR (tail)), 0); |
df7492f9 | 9563 | CODING_ATTR_SAFE_CHARSETS (attrs) = safe_charsets; |
068a9dbd | 9564 | |
584948ac | 9565 | CODING_ATTR_ASCII_COMPAT (attrs) = args[coding_arg_ascii_compatible_p]; |
3a73fa5d | 9566 | |
df7492f9 | 9567 | val = args[coding_arg_decode_translation_table]; |
a6f87d34 | 9568 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) |
7d64c6ad | 9569 | CHECK_SYMBOL (val); |
df7492f9 | 9570 | CODING_ATTR_DECODE_TBL (attrs) = val; |
3a73fa5d | 9571 | |
df7492f9 | 9572 | val = args[coding_arg_encode_translation_table]; |
a6f87d34 | 9573 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) |
7d64c6ad | 9574 | CHECK_SYMBOL (val); |
df7492f9 | 9575 | CODING_ATTR_ENCODE_TBL (attrs) = val; |
d46c5b12 | 9576 | |
df7492f9 KH |
9577 | val = args[coding_arg_post_read_conversion]; |
9578 | CHECK_SYMBOL (val); | |
9579 | CODING_ATTR_POST_READ (attrs) = val; | |
d46c5b12 | 9580 | |
df7492f9 KH |
9581 | val = args[coding_arg_pre_write_conversion]; |
9582 | CHECK_SYMBOL (val); | |
9583 | CODING_ATTR_PRE_WRITE (attrs) = val; | |
3a73fa5d | 9584 | |
df7492f9 KH |
9585 | val = args[coding_arg_default_char]; |
9586 | if (NILP (val)) | |
9587 | CODING_ATTR_DEFAULT_CHAR (attrs) = make_number (' '); | |
9588 | else | |
9589 | { | |
8f924df7 | 9590 | CHECK_CHARACTER (val); |
df7492f9 KH |
9591 | CODING_ATTR_DEFAULT_CHAR (attrs) = val; |
9592 | } | |
4031e2bf | 9593 | |
8f924df7 KH |
9594 | val = args[coding_arg_for_unibyte]; |
9595 | CODING_ATTR_FOR_UNIBYTE (attrs) = NILP (val) ? Qnil : Qt; | |
3a73fa5d | 9596 | |
df7492f9 KH |
9597 | val = args[coding_arg_plist]; |
9598 | CHECK_LIST (val); | |
9599 | CODING_ATTR_PLIST (attrs) = val; | |
3a73fa5d | 9600 | |
df7492f9 KH |
9601 | if (EQ (coding_type, Qcharset)) |
9602 | { | |
c7c66a95 KH |
9603 | /* Generate a lisp vector of 256 elements. Each element is nil, |
9604 | integer, or a list of charset IDs. | |
3a73fa5d | 9605 | |
c7c66a95 KH |
9606 | If Nth element is nil, the byte code N is invalid in this |
9607 | coding system. | |
4ed46869 | 9608 | |
c7c66a95 KH |
9609 | If Nth element is a number NUM, N is the first byte of a |
9610 | charset whose ID is NUM. | |
4ed46869 | 9611 | |
c7c66a95 KH |
9612 | If Nth element is a list of charset IDs, N is the first byte |
9613 | of one of them. The list is sorted by dimensions of the | |
ad1746f5 | 9614 | charsets. A charset of smaller dimension comes first. */ |
df7492f9 | 9615 | val = Fmake_vector (make_number (256), Qnil); |
4ed46869 | 9616 | |
5c99c2e6 | 9617 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) |
df7492f9 | 9618 | { |
c7c66a95 KH |
9619 | struct charset *charset = CHARSET_FROM_ID (XFASTINT (XCAR (tail))); |
9620 | int dim = CHARSET_DIMENSION (charset); | |
9621 | int idx = (dim - 1) * 4; | |
4ed46869 | 9622 | |
5c99c2e6 | 9623 | if (CHARSET_ASCII_COMPATIBLE_P (charset)) |
584948ac | 9624 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; |
4031e2bf | 9625 | |
15d143f7 KH |
9626 | for (i = charset->code_space[idx]; |
9627 | i <= charset->code_space[idx + 1]; i++) | |
9628 | { | |
c7c66a95 KH |
9629 | Lisp_Object tmp, tmp2; |
9630 | int dim2; | |
ec6d2bb8 | 9631 | |
c7c66a95 KH |
9632 | tmp = AREF (val, i); |
9633 | if (NILP (tmp)) | |
9634 | tmp = XCAR (tail); | |
9635 | else if (NUMBERP (tmp)) | |
9636 | { | |
9637 | dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp))); | |
9638 | if (dim < dim2) | |
c7c66a95 | 9639 | tmp = Fcons (XCAR (tail), Fcons (tmp, Qnil)); |
f9d71dcd KH |
9640 | else |
9641 | tmp = Fcons (tmp, Fcons (XCAR (tail), Qnil)); | |
c7c66a95 | 9642 | } |
15d143f7 | 9643 | else |
c7c66a95 KH |
9644 | { |
9645 | for (tmp2 = tmp; CONSP (tmp2); tmp2 = XCDR (tmp2)) | |
9646 | { | |
9647 | dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2)))); | |
9648 | if (dim < dim2) | |
9649 | break; | |
9650 | } | |
9651 | if (NILP (tmp2)) | |
9652 | tmp = nconc2 (tmp, Fcons (XCAR (tail), Qnil)); | |
9653 | else | |
9654 | { | |
9655 | XSETCDR (tmp2, Fcons (XCAR (tmp2), XCDR (tmp2))); | |
9656 | XSETCAR (tmp2, XCAR (tail)); | |
9657 | } | |
9658 | } | |
9659 | ASET (val, i, tmp); | |
15d143f7 | 9660 | } |
df7492f9 KH |
9661 | } |
9662 | ASET (attrs, coding_attr_charset_valids, val); | |
9663 | category = coding_category_charset; | |
9664 | } | |
9665 | else if (EQ (coding_type, Qccl)) | |
9666 | { | |
9667 | Lisp_Object valids; | |
ecec61c1 | 9668 | |
df7492f9 KH |
9669 | if (nargs < coding_arg_ccl_max) |
9670 | goto short_args; | |
ecec61c1 | 9671 | |
df7492f9 KH |
9672 | val = args[coding_arg_ccl_decoder]; |
9673 | CHECK_CCL_PROGRAM (val); | |
9674 | if (VECTORP (val)) | |
9675 | val = Fcopy_sequence (val); | |
9676 | ASET (attrs, coding_attr_ccl_decoder, val); | |
ecec61c1 | 9677 | |
df7492f9 KH |
9678 | val = args[coding_arg_ccl_encoder]; |
9679 | CHECK_CCL_PROGRAM (val); | |
9680 | if (VECTORP (val)) | |
9681 | val = Fcopy_sequence (val); | |
9682 | ASET (attrs, coding_attr_ccl_encoder, val); | |
ecec61c1 | 9683 | |
df7492f9 KH |
9684 | val = args[coding_arg_ccl_valids]; |
9685 | valids = Fmake_string (make_number (256), make_number (0)); | |
9686 | for (tail = val; !NILP (tail); tail = Fcdr (tail)) | |
9687 | { | |
8dcbea82 | 9688 | int from, to; |
ecec61c1 | 9689 | |
df7492f9 KH |
9690 | val = Fcar (tail); |
9691 | if (INTEGERP (val)) | |
8dcbea82 KH |
9692 | { |
9693 | from = to = XINT (val); | |
9694 | if (from < 0 || from > 255) | |
9695 | args_out_of_range_3 (val, make_number (0), make_number (255)); | |
9696 | } | |
df7492f9 KH |
9697 | else |
9698 | { | |
df7492f9 | 9699 | CHECK_CONS (val); |
8f924df7 KH |
9700 | CHECK_NATNUM_CAR (val); |
9701 | CHECK_NATNUM_CDR (val); | |
df7492f9 | 9702 | from = XINT (XCAR (val)); |
8f924df7 | 9703 | if (from > 255) |
8dcbea82 KH |
9704 | args_out_of_range_3 (XCAR (val), |
9705 | make_number (0), make_number (255)); | |
df7492f9 | 9706 | to = XINT (XCDR (val)); |
8dcbea82 KH |
9707 | if (to < from || to > 255) |
9708 | args_out_of_range_3 (XCDR (val), | |
9709 | XCAR (val), make_number (255)); | |
df7492f9 | 9710 | } |
8dcbea82 | 9711 | for (i = from; i <= to; i++) |
8f924df7 | 9712 | SSET (valids, i, 1); |
df7492f9 KH |
9713 | } |
9714 | ASET (attrs, coding_attr_ccl_valids, valids); | |
4ed46869 | 9715 | |
df7492f9 | 9716 | category = coding_category_ccl; |
55ab7be3 | 9717 | } |
df7492f9 | 9718 | else if (EQ (coding_type, Qutf_16)) |
55ab7be3 | 9719 | { |
df7492f9 | 9720 | Lisp_Object bom, endian; |
4ed46869 | 9721 | |
584948ac | 9722 | CODING_ATTR_ASCII_COMPAT (attrs) = Qnil; |
4ed46869 | 9723 | |
df7492f9 KH |
9724 | if (nargs < coding_arg_utf16_max) |
9725 | goto short_args; | |
4ed46869 | 9726 | |
df7492f9 KH |
9727 | bom = args[coding_arg_utf16_bom]; |
9728 | if (! NILP (bom) && ! EQ (bom, Qt)) | |
9729 | { | |
9730 | CHECK_CONS (bom); | |
8f924df7 KH |
9731 | val = XCAR (bom); |
9732 | CHECK_CODING_SYSTEM (val); | |
9733 | val = XCDR (bom); | |
9734 | CHECK_CODING_SYSTEM (val); | |
df7492f9 | 9735 | } |
a470d443 | 9736 | ASET (attrs, coding_attr_utf_bom, bom); |
df7492f9 KH |
9737 | |
9738 | endian = args[coding_arg_utf16_endian]; | |
b49a1807 KH |
9739 | CHECK_SYMBOL (endian); |
9740 | if (NILP (endian)) | |
9741 | endian = Qbig; | |
9742 | else if (! EQ (endian, Qbig) && ! EQ (endian, Qlittle)) | |
8f924df7 | 9743 | error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian))); |
df7492f9 KH |
9744 | ASET (attrs, coding_attr_utf_16_endian, endian); |
9745 | ||
9746 | category = (CONSP (bom) | |
9747 | ? coding_category_utf_16_auto | |
9748 | : NILP (bom) | |
b49a1807 | 9749 | ? (EQ (endian, Qbig) |
df7492f9 KH |
9750 | ? coding_category_utf_16_be_nosig |
9751 | : coding_category_utf_16_le_nosig) | |
b49a1807 | 9752 | : (EQ (endian, Qbig) |
df7492f9 KH |
9753 | ? coding_category_utf_16_be |
9754 | : coding_category_utf_16_le)); | |
9755 | } | |
9756 | else if (EQ (coding_type, Qiso_2022)) | |
9757 | { | |
9758 | Lisp_Object initial, reg_usage, request, flags; | |
1397dc18 | 9759 | |
df7492f9 KH |
9760 | if (nargs < coding_arg_iso2022_max) |
9761 | goto short_args; | |
9762 | ||
9763 | initial = Fcopy_sequence (args[coding_arg_iso2022_initial]); | |
9764 | CHECK_VECTOR (initial); | |
9765 | for (i = 0; i < 4; i++) | |
9766 | { | |
9767 | val = Faref (initial, make_number (i)); | |
9768 | if (! NILP (val)) | |
9769 | { | |
584948ac KH |
9770 | struct charset *charset; |
9771 | ||
9772 | CHECK_CHARSET_GET_CHARSET (val, charset); | |
9773 | ASET (initial, i, make_number (CHARSET_ID (charset))); | |
9774 | if (i == 0 && CHARSET_ASCII_COMPATIBLE_P (charset)) | |
9775 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; | |
df7492f9 KH |
9776 | } |
9777 | else | |
9778 | ASET (initial, i, make_number (-1)); | |
9779 | } | |
9780 | ||
9781 | reg_usage = args[coding_arg_iso2022_reg_usage]; | |
9782 | CHECK_CONS (reg_usage); | |
8f924df7 KH |
9783 | CHECK_NUMBER_CAR (reg_usage); |
9784 | CHECK_NUMBER_CDR (reg_usage); | |
df7492f9 KH |
9785 | |
9786 | request = Fcopy_sequence (args[coding_arg_iso2022_request]); | |
9787 | for (tail = request; ! NILP (tail); tail = Fcdr (tail)) | |
1397dc18 | 9788 | { |
df7492f9 | 9789 | int id; |
2735d060 | 9790 | Lisp_Object tmp1; |
df7492f9 KH |
9791 | |
9792 | val = Fcar (tail); | |
9793 | CHECK_CONS (val); | |
2735d060 PE |
9794 | tmp1 = XCAR (val); |
9795 | CHECK_CHARSET_GET_ID (tmp1, id); | |
8f924df7 | 9796 | CHECK_NATNUM_CDR (val); |
df7492f9 KH |
9797 | if (XINT (XCDR (val)) >= 4) |
9798 | error ("Invalid graphic register number: %d", XINT (XCDR (val))); | |
8f924df7 | 9799 | XSETCAR (val, make_number (id)); |
1397dc18 | 9800 | } |
4ed46869 | 9801 | |
df7492f9 KH |
9802 | flags = args[coding_arg_iso2022_flags]; |
9803 | CHECK_NATNUM (flags); | |
9804 | i = XINT (flags); | |
9805 | if (EQ (args[coding_arg_charset_list], Qiso_2022)) | |
9806 | flags = make_number (i | CODING_ISO_FLAG_FULL_SUPPORT); | |
9807 | ||
9808 | ASET (attrs, coding_attr_iso_initial, initial); | |
9809 | ASET (attrs, coding_attr_iso_usage, reg_usage); | |
9810 | ASET (attrs, coding_attr_iso_request, request); | |
9811 | ASET (attrs, coding_attr_iso_flags, flags); | |
9812 | setup_iso_safe_charsets (attrs); | |
9813 | ||
9814 | if (i & CODING_ISO_FLAG_SEVEN_BITS) | |
9815 | category = ((i & (CODING_ISO_FLAG_LOCKING_SHIFT | |
9816 | | CODING_ISO_FLAG_SINGLE_SHIFT)) | |
9817 | ? coding_category_iso_7_else | |
9818 | : EQ (args[coding_arg_charset_list], Qiso_2022) | |
9819 | ? coding_category_iso_7 | |
9820 | : coding_category_iso_7_tight); | |
9821 | else | |
9822 | { | |
9823 | int id = XINT (AREF (initial, 1)); | |
9824 | ||
c6fb6e98 | 9825 | category = (((i & CODING_ISO_FLAG_LOCKING_SHIFT) |
df7492f9 KH |
9826 | || EQ (args[coding_arg_charset_list], Qiso_2022) |
9827 | || id < 0) | |
9828 | ? coding_category_iso_8_else | |
9829 | : (CHARSET_DIMENSION (CHARSET_FROM_ID (id)) == 1) | |
9830 | ? coding_category_iso_8_1 | |
9831 | : coding_category_iso_8_2); | |
9832 | } | |
0ce7886f KH |
9833 | if (category != coding_category_iso_8_1 |
9834 | && category != coding_category_iso_8_2) | |
9835 | CODING_ATTR_ASCII_COMPAT (attrs) = Qnil; | |
df7492f9 KH |
9836 | } |
9837 | else if (EQ (coding_type, Qemacs_mule)) | |
c28a9453 | 9838 | { |
df7492f9 KH |
9839 | if (EQ (args[coding_arg_charset_list], Qemacs_mule)) |
9840 | ASET (attrs, coding_attr_emacs_mule_full, Qt); | |
584948ac | 9841 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; |
df7492f9 | 9842 | category = coding_category_emacs_mule; |
c28a9453 | 9843 | } |
df7492f9 | 9844 | else if (EQ (coding_type, Qshift_jis)) |
c28a9453 | 9845 | { |
df7492f9 KH |
9846 | |
9847 | struct charset *charset; | |
9848 | ||
7d64c6ad | 9849 | if (XINT (Flength (charset_list)) != 3 |
6e07c25f | 9850 | && XINT (Flength (charset_list)) != 4) |
7d64c6ad | 9851 | error ("There should be three or four charsets"); |
df7492f9 KH |
9852 | |
9853 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
9854 | if (CHARSET_DIMENSION (charset) != 1) | |
9855 | error ("Dimension of charset %s is not one", | |
8f924df7 | 9856 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
584948ac KH |
9857 | if (CHARSET_ASCII_COMPATIBLE_P (charset)) |
9858 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; | |
df7492f9 KH |
9859 | |
9860 | charset_list = XCDR (charset_list); | |
9861 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
9862 | if (CHARSET_DIMENSION (charset) != 1) | |
9863 | error ("Dimension of charset %s is not one", | |
8f924df7 | 9864 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
df7492f9 KH |
9865 | |
9866 | charset_list = XCDR (charset_list); | |
9867 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
9868 | if (CHARSET_DIMENSION (charset) != 2) | |
7d64c6ad KH |
9869 | error ("Dimension of charset %s is not two", |
9870 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); | |
9871 | ||
9872 | charset_list = XCDR (charset_list); | |
2b917a06 KH |
9873 | if (! NILP (charset_list)) |
9874 | { | |
9875 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
9876 | if (CHARSET_DIMENSION (charset) != 2) | |
9877 | error ("Dimension of charset %s is not two", | |
9878 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); | |
9879 | } | |
df7492f9 KH |
9880 | |
9881 | category = coding_category_sjis; | |
9882 | Vsjis_coding_system = name; | |
c28a9453 | 9883 | } |
df7492f9 KH |
9884 | else if (EQ (coding_type, Qbig5)) |
9885 | { | |
9886 | struct charset *charset; | |
4ed46869 | 9887 | |
df7492f9 KH |
9888 | if (XINT (Flength (charset_list)) != 2) |
9889 | error ("There should be just two charsets"); | |
9890 | ||
9891 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
9892 | if (CHARSET_DIMENSION (charset) != 1) | |
9893 | error ("Dimension of charset %s is not one", | |
8f924df7 | 9894 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
584948ac KH |
9895 | if (CHARSET_ASCII_COMPATIBLE_P (charset)) |
9896 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; | |
df7492f9 KH |
9897 | |
9898 | charset_list = XCDR (charset_list); | |
9899 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
9900 | if (CHARSET_DIMENSION (charset) != 2) | |
9901 | error ("Dimension of charset %s is not two", | |
8f924df7 | 9902 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
4ed46869 | 9903 | |
df7492f9 KH |
9904 | category = coding_category_big5; |
9905 | Vbig5_coding_system = name; | |
9906 | } | |
9907 | else if (EQ (coding_type, Qraw_text)) | |
c28a9453 | 9908 | { |
584948ac KH |
9909 | category = coding_category_raw_text; |
9910 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; | |
c28a9453 | 9911 | } |
df7492f9 | 9912 | else if (EQ (coding_type, Qutf_8)) |
4ed46869 | 9913 | { |
a470d443 KH |
9914 | Lisp_Object bom; |
9915 | ||
584948ac | 9916 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; |
a470d443 KH |
9917 | |
9918 | if (nargs < coding_arg_utf8_max) | |
9919 | goto short_args; | |
9920 | ||
9921 | bom = args[coding_arg_utf8_bom]; | |
9922 | if (! NILP (bom) && ! EQ (bom, Qt)) | |
9923 | { | |
9924 | CHECK_CONS (bom); | |
9925 | val = XCAR (bom); | |
9926 | CHECK_CODING_SYSTEM (val); | |
9927 | val = XCDR (bom); | |
9928 | CHECK_CODING_SYSTEM (val); | |
9929 | } | |
9930 | ASET (attrs, coding_attr_utf_bom, bom); | |
9931 | ||
9932 | category = (CONSP (bom) ? coding_category_utf_8_auto | |
9933 | : NILP (bom) ? coding_category_utf_8_nosig | |
9934 | : coding_category_utf_8_sig); | |
4ed46869 | 9935 | } |
df7492f9 KH |
9936 | else if (EQ (coding_type, Qundecided)) |
9937 | category = coding_category_undecided; | |
4ed46869 | 9938 | else |
df7492f9 | 9939 | error ("Invalid coding system type: %s", |
8f924df7 | 9940 | SDATA (SYMBOL_NAME (coding_type))); |
4ed46869 | 9941 | |
df7492f9 | 9942 | CODING_ATTR_CATEGORY (attrs) = make_number (category); |
01378f49 KH |
9943 | CODING_ATTR_PLIST (attrs) |
9944 | = Fcons (QCcategory, Fcons (AREF (Vcoding_category_table, category), | |
9945 | CODING_ATTR_PLIST (attrs))); | |
35befdaa | 9946 | CODING_ATTR_PLIST (attrs) |
3ed051d4 | 9947 | = Fcons (QCascii_compatible_p, |
35befdaa KH |
9948 | Fcons (CODING_ATTR_ASCII_COMPAT (attrs), |
9949 | CODING_ATTR_PLIST (attrs))); | |
c4825358 | 9950 | |
df7492f9 KH |
9951 | eol_type = args[coding_arg_eol_type]; |
9952 | if (! NILP (eol_type) | |
9953 | && ! EQ (eol_type, Qunix) | |
9954 | && ! EQ (eol_type, Qdos) | |
9955 | && ! EQ (eol_type, Qmac)) | |
9956 | error ("Invalid eol-type"); | |
4ed46869 | 9957 | |
df7492f9 | 9958 | aliases = Fcons (name, Qnil); |
4ed46869 | 9959 | |
df7492f9 KH |
9960 | if (NILP (eol_type)) |
9961 | { | |
9962 | eol_type = make_subsidiaries (name); | |
9963 | for (i = 0; i < 3; i++) | |
1397dc18 | 9964 | { |
df7492f9 KH |
9965 | Lisp_Object this_spec, this_name, this_aliases, this_eol_type; |
9966 | ||
9967 | this_name = AREF (eol_type, i); | |
9968 | this_aliases = Fcons (this_name, Qnil); | |
9969 | this_eol_type = (i == 0 ? Qunix : i == 1 ? Qdos : Qmac); | |
9970 | this_spec = Fmake_vector (make_number (3), attrs); | |
9971 | ASET (this_spec, 1, this_aliases); | |
9972 | ASET (this_spec, 2, this_eol_type); | |
9973 | Fputhash (this_name, this_spec, Vcoding_system_hash_table); | |
9974 | Vcoding_system_list = Fcons (this_name, Vcoding_system_list); | |
583f71ca KH |
9975 | val = Fassoc (Fsymbol_name (this_name), Vcoding_system_alist); |
9976 | if (NILP (val)) | |
9977 | Vcoding_system_alist | |
9978 | = Fcons (Fcons (Fsymbol_name (this_name), Qnil), | |
9979 | Vcoding_system_alist); | |
1397dc18 | 9980 | } |
d46c5b12 | 9981 | } |
4ed46869 | 9982 | |
df7492f9 KH |
9983 | spec_vec = Fmake_vector (make_number (3), attrs); |
9984 | ASET (spec_vec, 1, aliases); | |
9985 | ASET (spec_vec, 2, eol_type); | |
48b0f3ae | 9986 | |
df7492f9 KH |
9987 | Fputhash (name, spec_vec, Vcoding_system_hash_table); |
9988 | Vcoding_system_list = Fcons (name, Vcoding_system_list); | |
583f71ca KH |
9989 | val = Fassoc (Fsymbol_name (name), Vcoding_system_alist); |
9990 | if (NILP (val)) | |
9991 | Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (name), Qnil), | |
9992 | Vcoding_system_alist); | |
48b0f3ae | 9993 | |
df7492f9 KH |
9994 | { |
9995 | int id = coding_categories[category].id; | |
48b0f3ae | 9996 | |
df7492f9 KH |
9997 | if (id < 0 || EQ (name, CODING_ID_NAME (id))) |
9998 | setup_coding_system (name, &coding_categories[category]); | |
9999 | } | |
48b0f3ae | 10000 | |
d46c5b12 | 10001 | return Qnil; |
48b0f3ae | 10002 | |
df7492f9 KH |
10003 | short_args: |
10004 | return Fsignal (Qwrong_number_of_arguments, | |
10005 | Fcons (intern ("define-coding-system-internal"), | |
10006 | make_number (nargs))); | |
d46c5b12 | 10007 | } |
4ed46869 | 10008 | |
d6925f38 | 10009 | |
a6f87d34 KH |
10010 | DEFUN ("coding-system-put", Fcoding_system_put, Scoding_system_put, |
10011 | 3, 3, 0, | |
10012 | doc: /* Change value in CODING-SYSTEM's property list PROP to VAL. */) | |
5842a27b | 10013 | (Lisp_Object coding_system, Lisp_Object prop, Lisp_Object val) |
a6f87d34 | 10014 | { |
3dbe7859 | 10015 | Lisp_Object spec, attrs; |
a6f87d34 KH |
10016 | |
10017 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
10018 | attrs = AREF (spec, 0); | |
10019 | if (EQ (prop, QCmnemonic)) | |
10020 | { | |
10021 | if (! STRINGP (val)) | |
10022 | CHECK_CHARACTER (val); | |
10023 | CODING_ATTR_MNEMONIC (attrs) = val; | |
10024 | } | |
2133e2d1 | 10025 | else if (EQ (prop, QCdefault_char)) |
a6f87d34 KH |
10026 | { |
10027 | if (NILP (val)) | |
10028 | val = make_number (' '); | |
10029 | else | |
10030 | CHECK_CHARACTER (val); | |
10031 | CODING_ATTR_DEFAULT_CHAR (attrs) = val; | |
10032 | } | |
10033 | else if (EQ (prop, QCdecode_translation_table)) | |
10034 | { | |
10035 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) | |
10036 | CHECK_SYMBOL (val); | |
10037 | CODING_ATTR_DECODE_TBL (attrs) = val; | |
10038 | } | |
10039 | else if (EQ (prop, QCencode_translation_table)) | |
10040 | { | |
10041 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) | |
10042 | CHECK_SYMBOL (val); | |
10043 | CODING_ATTR_ENCODE_TBL (attrs) = val; | |
10044 | } | |
10045 | else if (EQ (prop, QCpost_read_conversion)) | |
10046 | { | |
10047 | CHECK_SYMBOL (val); | |
10048 | CODING_ATTR_POST_READ (attrs) = val; | |
10049 | } | |
10050 | else if (EQ (prop, QCpre_write_conversion)) | |
10051 | { | |
10052 | CHECK_SYMBOL (val); | |
10053 | CODING_ATTR_PRE_WRITE (attrs) = val; | |
10054 | } | |
35befdaa KH |
10055 | else if (EQ (prop, QCascii_compatible_p)) |
10056 | { | |
10057 | CODING_ATTR_ASCII_COMPAT (attrs) = val; | |
10058 | } | |
a6f87d34 KH |
10059 | |
10060 | CODING_ATTR_PLIST (attrs) | |
10061 | = Fplist_put (CODING_ATTR_PLIST (attrs), prop, val); | |
10062 | return val; | |
10063 | } | |
10064 | ||
10065 | ||
df7492f9 KH |
10066 | DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias, |
10067 | Sdefine_coding_system_alias, 2, 2, 0, | |
10068 | doc: /* Define ALIAS as an alias for CODING-SYSTEM. */) | |
5842a27b | 10069 | (Lisp_Object alias, Lisp_Object coding_system) |
66cfb530 | 10070 | { |
583f71ca | 10071 | Lisp_Object spec, aliases, eol_type, val; |
4ed46869 | 10072 | |
df7492f9 KH |
10073 | CHECK_SYMBOL (alias); |
10074 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
10075 | aliases = AREF (spec, 1); | |
d4a1d553 | 10076 | /* ALIASES should be a list of length more than zero, and the first |
d6925f38 KH |
10077 | element is a base coding system. Append ALIAS at the tail of the |
10078 | list. */ | |
df7492f9 KH |
10079 | while (!NILP (XCDR (aliases))) |
10080 | aliases = XCDR (aliases); | |
8f924df7 | 10081 | XSETCDR (aliases, Fcons (alias, Qnil)); |
4ed46869 | 10082 | |
df7492f9 KH |
10083 | eol_type = AREF (spec, 2); |
10084 | if (VECTORP (eol_type)) | |
4ed46869 | 10085 | { |
df7492f9 KH |
10086 | Lisp_Object subsidiaries; |
10087 | int i; | |
4ed46869 | 10088 | |
df7492f9 KH |
10089 | subsidiaries = make_subsidiaries (alias); |
10090 | for (i = 0; i < 3; i++) | |
10091 | Fdefine_coding_system_alias (AREF (subsidiaries, i), | |
10092 | AREF (eol_type, i)); | |
4ed46869 | 10093 | } |
df7492f9 KH |
10094 | |
10095 | Fputhash (alias, spec, Vcoding_system_hash_table); | |
d6925f38 | 10096 | Vcoding_system_list = Fcons (alias, Vcoding_system_list); |
583f71ca KH |
10097 | val = Fassoc (Fsymbol_name (alias), Vcoding_system_alist); |
10098 | if (NILP (val)) | |
10099 | Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (alias), Qnil), | |
10100 | Vcoding_system_alist); | |
66cfb530 | 10101 | |
4ed46869 KH |
10102 | return Qnil; |
10103 | } | |
10104 | ||
df7492f9 KH |
10105 | DEFUN ("coding-system-base", Fcoding_system_base, Scoding_system_base, |
10106 | 1, 1, 0, | |
10107 | doc: /* Return the base of CODING-SYSTEM. | |
da7db224 | 10108 | Any alias or subsidiary coding system is not a base coding system. */) |
5842a27b | 10109 | (Lisp_Object coding_system) |
d46c5b12 | 10110 | { |
df7492f9 | 10111 | Lisp_Object spec, attrs; |
d46c5b12 | 10112 | |
df7492f9 KH |
10113 | if (NILP (coding_system)) |
10114 | return (Qno_conversion); | |
10115 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
10116 | attrs = AREF (spec, 0); | |
10117 | return CODING_ATTR_BASE_NAME (attrs); | |
10118 | } | |
1397dc18 | 10119 | |
df7492f9 KH |
10120 | DEFUN ("coding-system-plist", Fcoding_system_plist, Scoding_system_plist, |
10121 | 1, 1, 0, | |
10122 | doc: "Return the property list of CODING-SYSTEM.") | |
5842a27b | 10123 | (Lisp_Object coding_system) |
df7492f9 KH |
10124 | { |
10125 | Lisp_Object spec, attrs; | |
1397dc18 | 10126 | |
df7492f9 KH |
10127 | if (NILP (coding_system)) |
10128 | coding_system = Qno_conversion; | |
10129 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
10130 | attrs = AREF (spec, 0); | |
10131 | return CODING_ATTR_PLIST (attrs); | |
d46c5b12 KH |
10132 | } |
10133 | ||
df7492f9 KH |
10134 | |
10135 | DEFUN ("coding-system-aliases", Fcoding_system_aliases, Scoding_system_aliases, | |
10136 | 1, 1, 0, | |
da7db224 | 10137 | doc: /* Return the list of aliases of CODING-SYSTEM. */) |
5842a27b | 10138 | (Lisp_Object coding_system) |
66cfb530 | 10139 | { |
df7492f9 | 10140 | Lisp_Object spec; |
84d60297 | 10141 | |
df7492f9 KH |
10142 | if (NILP (coding_system)) |
10143 | coding_system = Qno_conversion; | |
10144 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
da7db224 | 10145 | return AREF (spec, 1); |
df7492f9 | 10146 | } |
66cfb530 | 10147 | |
df7492f9 KH |
10148 | DEFUN ("coding-system-eol-type", Fcoding_system_eol_type, |
10149 | Scoding_system_eol_type, 1, 1, 0, | |
10150 | doc: /* Return eol-type of CODING-SYSTEM. | |
d4a1d553 | 10151 | An eol-type is an integer 0, 1, 2, or a vector of coding systems. |
66cfb530 | 10152 | |
df7492f9 KH |
10153 | Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF, |
10154 | and CR respectively. | |
66cfb530 | 10155 | |
df7492f9 KH |
10156 | A vector value indicates that a format of end-of-line should be |
10157 | detected automatically. Nth element of the vector is the subsidiary | |
10158 | coding system whose eol-type is N. */) | |
5842a27b | 10159 | (Lisp_Object coding_system) |
6b89e3aa | 10160 | { |
df7492f9 KH |
10161 | Lisp_Object spec, eol_type; |
10162 | int n; | |
6b89e3aa | 10163 | |
df7492f9 KH |
10164 | if (NILP (coding_system)) |
10165 | coding_system = Qno_conversion; | |
10166 | if (! CODING_SYSTEM_P (coding_system)) | |
10167 | return Qnil; | |
10168 | spec = CODING_SYSTEM_SPEC (coding_system); | |
10169 | eol_type = AREF (spec, 2); | |
10170 | if (VECTORP (eol_type)) | |
10171 | return Fcopy_sequence (eol_type); | |
10172 | n = EQ (eol_type, Qunix) ? 0 : EQ (eol_type, Qdos) ? 1 : 2; | |
10173 | return make_number (n); | |
6b89e3aa KH |
10174 | } |
10175 | ||
4ed46869 KH |
10176 | #endif /* emacs */ |
10177 | ||
10178 | \f | |
1397dc18 | 10179 | /*** 9. Post-amble ***/ |
4ed46869 | 10180 | |
dfcf069d | 10181 | void |
971de7fb | 10182 | init_coding_once (void) |
4ed46869 KH |
10183 | { |
10184 | int i; | |
10185 | ||
df7492f9 KH |
10186 | for (i = 0; i < coding_category_max; i++) |
10187 | { | |
10188 | coding_categories[i].id = -1; | |
10189 | coding_priorities[i] = i; | |
10190 | } | |
4ed46869 KH |
10191 | |
10192 | /* ISO2022 specific initialize routine. */ | |
10193 | for (i = 0; i < 0x20; i++) | |
b73bfc1c | 10194 | iso_code_class[i] = ISO_control_0; |
4ed46869 KH |
10195 | for (i = 0x21; i < 0x7F; i++) |
10196 | iso_code_class[i] = ISO_graphic_plane_0; | |
10197 | for (i = 0x80; i < 0xA0; i++) | |
b73bfc1c | 10198 | iso_code_class[i] = ISO_control_1; |
4ed46869 KH |
10199 | for (i = 0xA1; i < 0xFF; i++) |
10200 | iso_code_class[i] = ISO_graphic_plane_1; | |
10201 | iso_code_class[0x20] = iso_code_class[0x7F] = ISO_0x20_or_0x7F; | |
10202 | iso_code_class[0xA0] = iso_code_class[0xFF] = ISO_0xA0_or_0xFF; | |
4ed46869 KH |
10203 | iso_code_class[ISO_CODE_SO] = ISO_shift_out; |
10204 | iso_code_class[ISO_CODE_SI] = ISO_shift_in; | |
10205 | iso_code_class[ISO_CODE_SS2_7] = ISO_single_shift_2_7; | |
10206 | iso_code_class[ISO_CODE_ESC] = ISO_escape; | |
10207 | iso_code_class[ISO_CODE_SS2] = ISO_single_shift_2; | |
10208 | iso_code_class[ISO_CODE_SS3] = ISO_single_shift_3; | |
10209 | iso_code_class[ISO_CODE_CSI] = ISO_control_sequence_introducer; | |
10210 | ||
df7492f9 KH |
10211 | for (i = 0; i < 256; i++) |
10212 | { | |
10213 | emacs_mule_bytes[i] = 1; | |
10214 | } | |
7c78e542 KH |
10215 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_11] = 3; |
10216 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_12] = 3; | |
10217 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_21] = 4; | |
10218 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_22] = 4; | |
e0e989f6 KH |
10219 | } |
10220 | ||
10221 | #ifdef emacs | |
10222 | ||
dfcf069d | 10223 | void |
971de7fb | 10224 | syms_of_coding (void) |
e0e989f6 | 10225 | { |
df7492f9 | 10226 | staticpro (&Vcoding_system_hash_table); |
8f924df7 KH |
10227 | { |
10228 | Lisp_Object args[2]; | |
10229 | args[0] = QCtest; | |
10230 | args[1] = Qeq; | |
10231 | Vcoding_system_hash_table = Fmake_hash_table (2, args); | |
10232 | } | |
df7492f9 KH |
10233 | |
10234 | staticpro (&Vsjis_coding_system); | |
10235 | Vsjis_coding_system = Qnil; | |
e0e989f6 | 10236 | |
df7492f9 KH |
10237 | staticpro (&Vbig5_coding_system); |
10238 | Vbig5_coding_system = Qnil; | |
10239 | ||
24a73b0a KH |
10240 | staticpro (&Vcode_conversion_reused_workbuf); |
10241 | Vcode_conversion_reused_workbuf = Qnil; | |
10242 | ||
10243 | staticpro (&Vcode_conversion_workbuf_name); | |
d67b4f80 | 10244 | Vcode_conversion_workbuf_name = make_pure_c_string (" *code-conversion-work*"); |
e0e989f6 | 10245 | |
24a73b0a | 10246 | reused_workbuf_in_use = 0; |
df7492f9 KH |
10247 | |
10248 | DEFSYM (Qcharset, "charset"); | |
10249 | DEFSYM (Qtarget_idx, "target-idx"); | |
10250 | DEFSYM (Qcoding_system_history, "coding-system-history"); | |
bb0115a2 RS |
10251 | Fset (Qcoding_system_history, Qnil); |
10252 | ||
9ce27fde | 10253 | /* Target FILENAME is the first argument. */ |
e0e989f6 | 10254 | Fput (Qinsert_file_contents, Qtarget_idx, make_number (0)); |
9ce27fde | 10255 | /* Target FILENAME is the third argument. */ |
e0e989f6 KH |
10256 | Fput (Qwrite_region, Qtarget_idx, make_number (2)); |
10257 | ||
df7492f9 | 10258 | DEFSYM (Qcall_process, "call-process"); |
9ce27fde | 10259 | /* Target PROGRAM is the first argument. */ |
e0e989f6 KH |
10260 | Fput (Qcall_process, Qtarget_idx, make_number (0)); |
10261 | ||
df7492f9 | 10262 | DEFSYM (Qcall_process_region, "call-process-region"); |
9ce27fde | 10263 | /* Target PROGRAM is the third argument. */ |
e0e989f6 KH |
10264 | Fput (Qcall_process_region, Qtarget_idx, make_number (2)); |
10265 | ||
df7492f9 | 10266 | DEFSYM (Qstart_process, "start-process"); |
9ce27fde | 10267 | /* Target PROGRAM is the third argument. */ |
e0e989f6 KH |
10268 | Fput (Qstart_process, Qtarget_idx, make_number (2)); |
10269 | ||
df7492f9 | 10270 | DEFSYM (Qopen_network_stream, "open-network-stream"); |
9ce27fde | 10271 | /* Target SERVICE is the fourth argument. */ |
e0e989f6 KH |
10272 | Fput (Qopen_network_stream, Qtarget_idx, make_number (3)); |
10273 | ||
df7492f9 KH |
10274 | DEFSYM (Qcoding_system, "coding-system"); |
10275 | DEFSYM (Qcoding_aliases, "coding-aliases"); | |
4ed46869 | 10276 | |
df7492f9 KH |
10277 | DEFSYM (Qeol_type, "eol-type"); |
10278 | DEFSYM (Qunix, "unix"); | |
10279 | DEFSYM (Qdos, "dos"); | |
4ed46869 | 10280 | |
df7492f9 KH |
10281 | DEFSYM (Qbuffer_file_coding_system, "buffer-file-coding-system"); |
10282 | DEFSYM (Qpost_read_conversion, "post-read-conversion"); | |
10283 | DEFSYM (Qpre_write_conversion, "pre-write-conversion"); | |
10284 | DEFSYM (Qdefault_char, "default-char"); | |
10285 | DEFSYM (Qundecided, "undecided"); | |
10286 | DEFSYM (Qno_conversion, "no-conversion"); | |
10287 | DEFSYM (Qraw_text, "raw-text"); | |
4ed46869 | 10288 | |
df7492f9 | 10289 | DEFSYM (Qiso_2022, "iso-2022"); |
4ed46869 | 10290 | |
df7492f9 | 10291 | DEFSYM (Qutf_8, "utf-8"); |
8f924df7 | 10292 | DEFSYM (Qutf_8_emacs, "utf-8-emacs"); |
27901516 | 10293 | |
df7492f9 | 10294 | DEFSYM (Qutf_16, "utf-16"); |
df7492f9 KH |
10295 | DEFSYM (Qbig, "big"); |
10296 | DEFSYM (Qlittle, "little"); | |
27901516 | 10297 | |
df7492f9 KH |
10298 | DEFSYM (Qshift_jis, "shift-jis"); |
10299 | DEFSYM (Qbig5, "big5"); | |
4ed46869 | 10300 | |
df7492f9 | 10301 | DEFSYM (Qcoding_system_p, "coding-system-p"); |
4ed46869 | 10302 | |
df7492f9 | 10303 | DEFSYM (Qcoding_system_error, "coding-system-error"); |
4ed46869 | 10304 | Fput (Qcoding_system_error, Qerror_conditions, |
d67b4f80 | 10305 | pure_cons (Qcoding_system_error, pure_cons (Qerror, Qnil))); |
4ed46869 | 10306 | Fput (Qcoding_system_error, Qerror_message, |
d67b4f80 | 10307 | make_pure_c_string ("Invalid coding system")); |
4ed46869 | 10308 | |
05e6f5dc KH |
10309 | /* Intern this now in case it isn't already done. |
10310 | Setting this variable twice is harmless. | |
10311 | But don't staticpro it here--that is done in alloc.c. */ | |
d67b4f80 | 10312 | Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots"); |
70c22245 | 10313 | |
df7492f9 | 10314 | DEFSYM (Qtranslation_table, "translation-table"); |
433f7f87 | 10315 | Fput (Qtranslation_table, Qchar_table_extra_slots, make_number (2)); |
df7492f9 KH |
10316 | DEFSYM (Qtranslation_table_id, "translation-table-id"); |
10317 | DEFSYM (Qtranslation_table_for_decode, "translation-table-for-decode"); | |
10318 | DEFSYM (Qtranslation_table_for_encode, "translation-table-for-encode"); | |
1397dc18 | 10319 | |
df7492f9 | 10320 | DEFSYM (Qvalid_codes, "valid-codes"); |
9ce27fde | 10321 | |
df7492f9 | 10322 | DEFSYM (Qemacs_mule, "emacs-mule"); |
d46c5b12 | 10323 | |
01378f49 | 10324 | DEFSYM (QCcategory, ":category"); |
a6f87d34 | 10325 | DEFSYM (QCmnemonic, ":mnemonic"); |
2133e2d1 | 10326 | DEFSYM (QCdefault_char, ":default-char"); |
a6f87d34 KH |
10327 | DEFSYM (QCdecode_translation_table, ":decode-translation-table"); |
10328 | DEFSYM (QCencode_translation_table, ":encode-translation-table"); | |
10329 | DEFSYM (QCpost_read_conversion, ":post-read-conversion"); | |
10330 | DEFSYM (QCpre_write_conversion, ":pre-write-conversion"); | |
35befdaa | 10331 | DEFSYM (QCascii_compatible_p, ":ascii-compatible-p"); |
01378f49 | 10332 | |
df7492f9 KH |
10333 | Vcoding_category_table |
10334 | = Fmake_vector (make_number (coding_category_max), Qnil); | |
10335 | staticpro (&Vcoding_category_table); | |
10336 | /* Followings are target of code detection. */ | |
10337 | ASET (Vcoding_category_table, coding_category_iso_7, | |
d67b4f80 | 10338 | intern_c_string ("coding-category-iso-7")); |
df7492f9 | 10339 | ASET (Vcoding_category_table, coding_category_iso_7_tight, |
d67b4f80 | 10340 | intern_c_string ("coding-category-iso-7-tight")); |
df7492f9 | 10341 | ASET (Vcoding_category_table, coding_category_iso_8_1, |
d67b4f80 | 10342 | intern_c_string ("coding-category-iso-8-1")); |
df7492f9 | 10343 | ASET (Vcoding_category_table, coding_category_iso_8_2, |
d67b4f80 | 10344 | intern_c_string ("coding-category-iso-8-2")); |
df7492f9 | 10345 | ASET (Vcoding_category_table, coding_category_iso_7_else, |
d67b4f80 | 10346 | intern_c_string ("coding-category-iso-7-else")); |
df7492f9 | 10347 | ASET (Vcoding_category_table, coding_category_iso_8_else, |
d67b4f80 | 10348 | intern_c_string ("coding-category-iso-8-else")); |
a470d443 | 10349 | ASET (Vcoding_category_table, coding_category_utf_8_auto, |
d67b4f80 | 10350 | intern_c_string ("coding-category-utf-8-auto")); |
a470d443 | 10351 | ASET (Vcoding_category_table, coding_category_utf_8_nosig, |
d67b4f80 | 10352 | intern_c_string ("coding-category-utf-8")); |
a470d443 | 10353 | ASET (Vcoding_category_table, coding_category_utf_8_sig, |
d67b4f80 | 10354 | intern_c_string ("coding-category-utf-8-sig")); |
df7492f9 | 10355 | ASET (Vcoding_category_table, coding_category_utf_16_be, |
d67b4f80 | 10356 | intern_c_string ("coding-category-utf-16-be")); |
ff563fce | 10357 | ASET (Vcoding_category_table, coding_category_utf_16_auto, |
d67b4f80 | 10358 | intern_c_string ("coding-category-utf-16-auto")); |
df7492f9 | 10359 | ASET (Vcoding_category_table, coding_category_utf_16_le, |
d67b4f80 | 10360 | intern_c_string ("coding-category-utf-16-le")); |
df7492f9 | 10361 | ASET (Vcoding_category_table, coding_category_utf_16_be_nosig, |
d67b4f80 | 10362 | intern_c_string ("coding-category-utf-16-be-nosig")); |
df7492f9 | 10363 | ASET (Vcoding_category_table, coding_category_utf_16_le_nosig, |
d67b4f80 | 10364 | intern_c_string ("coding-category-utf-16-le-nosig")); |
df7492f9 | 10365 | ASET (Vcoding_category_table, coding_category_charset, |
d67b4f80 | 10366 | intern_c_string ("coding-category-charset")); |
df7492f9 | 10367 | ASET (Vcoding_category_table, coding_category_sjis, |
d67b4f80 | 10368 | intern_c_string ("coding-category-sjis")); |
df7492f9 | 10369 | ASET (Vcoding_category_table, coding_category_big5, |
d67b4f80 | 10370 | intern_c_string ("coding-category-big5")); |
df7492f9 | 10371 | ASET (Vcoding_category_table, coding_category_ccl, |
d67b4f80 | 10372 | intern_c_string ("coding-category-ccl")); |
df7492f9 | 10373 | ASET (Vcoding_category_table, coding_category_emacs_mule, |
d67b4f80 | 10374 | intern_c_string ("coding-category-emacs-mule")); |
df7492f9 KH |
10375 | /* Followings are NOT target of code detection. */ |
10376 | ASET (Vcoding_category_table, coding_category_raw_text, | |
d67b4f80 | 10377 | intern_c_string ("coding-category-raw-text")); |
df7492f9 | 10378 | ASET (Vcoding_category_table, coding_category_undecided, |
d67b4f80 | 10379 | intern_c_string ("coding-category-undecided")); |
ecf488bc | 10380 | |
065e3595 KH |
10381 | DEFSYM (Qinsufficient_source, "insufficient-source"); |
10382 | DEFSYM (Qinconsistent_eol, "inconsistent-eol"); | |
10383 | DEFSYM (Qinvalid_source, "invalid-source"); | |
10384 | DEFSYM (Qinterrupted, "interrupted"); | |
10385 | DEFSYM (Qinsufficient_memory, "insufficient-memory"); | |
44e8490d | 10386 | DEFSYM (Qcoding_system_define_form, "coding-system-define-form"); |
065e3595 | 10387 | |
4ed46869 KH |
10388 | defsubr (&Scoding_system_p); |
10389 | defsubr (&Sread_coding_system); | |
10390 | defsubr (&Sread_non_nil_coding_system); | |
10391 | defsubr (&Scheck_coding_system); | |
10392 | defsubr (&Sdetect_coding_region); | |
d46c5b12 | 10393 | defsubr (&Sdetect_coding_string); |
05e6f5dc | 10394 | defsubr (&Sfind_coding_systems_region_internal); |
068a9dbd | 10395 | defsubr (&Sunencodable_char_position); |
df7492f9 | 10396 | defsubr (&Scheck_coding_systems_region); |
4ed46869 KH |
10397 | defsubr (&Sdecode_coding_region); |
10398 | defsubr (&Sencode_coding_region); | |
10399 | defsubr (&Sdecode_coding_string); | |
10400 | defsubr (&Sencode_coding_string); | |
10401 | defsubr (&Sdecode_sjis_char); | |
10402 | defsubr (&Sencode_sjis_char); | |
10403 | defsubr (&Sdecode_big5_char); | |
10404 | defsubr (&Sencode_big5_char); | |
1ba9e4ab | 10405 | defsubr (&Sset_terminal_coding_system_internal); |
c4825358 | 10406 | defsubr (&Sset_safe_terminal_coding_system_internal); |
4ed46869 | 10407 | defsubr (&Sterminal_coding_system); |
1ba9e4ab | 10408 | defsubr (&Sset_keyboard_coding_system_internal); |
4ed46869 | 10409 | defsubr (&Skeyboard_coding_system); |
a5d301df | 10410 | defsubr (&Sfind_operation_coding_system); |
df7492f9 | 10411 | defsubr (&Sset_coding_system_priority); |
6b89e3aa | 10412 | defsubr (&Sdefine_coding_system_internal); |
df7492f9 | 10413 | defsubr (&Sdefine_coding_system_alias); |
a6f87d34 | 10414 | defsubr (&Scoding_system_put); |
df7492f9 KH |
10415 | defsubr (&Scoding_system_base); |
10416 | defsubr (&Scoding_system_plist); | |
10417 | defsubr (&Scoding_system_aliases); | |
10418 | defsubr (&Scoding_system_eol_type); | |
10419 | defsubr (&Scoding_system_priority_list); | |
4ed46869 | 10420 | |
29208e82 | 10421 | DEFVAR_LISP ("coding-system-list", Vcoding_system_list, |
48b0f3ae PJ |
10422 | doc: /* List of coding systems. |
10423 | ||
10424 | Do not alter the value of this variable manually. This variable should be | |
df7492f9 | 10425 | updated by the functions `define-coding-system' and |
48b0f3ae | 10426 | `define-coding-system-alias'. */); |
4608c386 KH |
10427 | Vcoding_system_list = Qnil; |
10428 | ||
29208e82 | 10429 | DEFVAR_LISP ("coding-system-alist", Vcoding_system_alist, |
48b0f3ae PJ |
10430 | doc: /* Alist of coding system names. |
10431 | Each element is one element list of coding system name. | |
446dcd75 | 10432 | This variable is given to `completing-read' as COLLECTION argument. |
48b0f3ae PJ |
10433 | |
10434 | Do not alter the value of this variable manually. This variable should be | |
10435 | updated by the functions `make-coding-system' and | |
10436 | `define-coding-system-alias'. */); | |
4608c386 KH |
10437 | Vcoding_system_alist = Qnil; |
10438 | ||
29208e82 | 10439 | DEFVAR_LISP ("coding-category-list", Vcoding_category_list, |
48b0f3ae PJ |
10440 | doc: /* List of coding-categories (symbols) ordered by priority. |
10441 | ||
10442 | On detecting a coding system, Emacs tries code detection algorithms | |
10443 | associated with each coding-category one by one in this order. When | |
10444 | one algorithm agrees with a byte sequence of source text, the coding | |
0ec31faf KH |
10445 | system bound to the corresponding coding-category is selected. |
10446 | ||
448e17d6 | 10447 | Don't modify this variable directly, but use `set-coding-system-priority'. */); |
4ed46869 KH |
10448 | { |
10449 | int i; | |
10450 | ||
10451 | Vcoding_category_list = Qnil; | |
df7492f9 | 10452 | for (i = coding_category_max - 1; i >= 0; i--) |
4ed46869 | 10453 | Vcoding_category_list |
d46c5b12 KH |
10454 | = Fcons (XVECTOR (Vcoding_category_table)->contents[i], |
10455 | Vcoding_category_list); | |
4ed46869 KH |
10456 | } |
10457 | ||
29208e82 | 10458 | DEFVAR_LISP ("coding-system-for-read", Vcoding_system_for_read, |
48b0f3ae PJ |
10459 | doc: /* Specify the coding system for read operations. |
10460 | It is useful to bind this variable with `let', but do not set it globally. | |
10461 | If the value is a coding system, it is used for decoding on read operation. | |
446dcd75 JB |
10462 | If not, an appropriate element is used from one of the coding system alists. |
10463 | There are three such tables: `file-coding-system-alist', | |
48b0f3ae | 10464 | `process-coding-system-alist', and `network-coding-system-alist'. */); |
4ed46869 KH |
10465 | Vcoding_system_for_read = Qnil; |
10466 | ||
29208e82 | 10467 | DEFVAR_LISP ("coding-system-for-write", Vcoding_system_for_write, |
48b0f3ae PJ |
10468 | doc: /* Specify the coding system for write operations. |
10469 | Programs bind this variable with `let', but you should not set it globally. | |
10470 | If the value is a coding system, it is used for encoding of output, | |
10471 | when writing it to a file and when sending it to a file or subprocess. | |
10472 | ||
10473 | If this does not specify a coding system, an appropriate element | |
446dcd75 JB |
10474 | is used from one of the coding system alists. |
10475 | There are three such tables: `file-coding-system-alist', | |
48b0f3ae PJ |
10476 | `process-coding-system-alist', and `network-coding-system-alist'. |
10477 | For output to files, if the above procedure does not specify a coding system, | |
10478 | the value of `buffer-file-coding-system' is used. */); | |
4ed46869 KH |
10479 | Vcoding_system_for_write = Qnil; |
10480 | ||
29208e82 | 10481 | DEFVAR_LISP ("last-coding-system-used", Vlast_coding_system_used, |
df7492f9 KH |
10482 | doc: /* |
10483 | Coding system used in the latest file or process I/O. */); | |
4ed46869 KH |
10484 | Vlast_coding_system_used = Qnil; |
10485 | ||
29208e82 | 10486 | DEFVAR_LISP ("last-code-conversion-error", Vlast_code_conversion_error, |
065e3595 KH |
10487 | doc: /* |
10488 | Error status of the last code conversion. | |
10489 | ||
10490 | When an error was detected in the last code conversion, this variable | |
10491 | is set to one of the following symbols. | |
10492 | `insufficient-source' | |
10493 | `inconsistent-eol' | |
10494 | `invalid-source' | |
10495 | `interrupted' | |
10496 | `insufficient-memory' | |
10497 | When no error was detected, the value doesn't change. So, to check | |
10498 | the error status of a code conversion by this variable, you must | |
10499 | explicitly set this variable to nil before performing code | |
10500 | conversion. */); | |
10501 | Vlast_code_conversion_error = Qnil; | |
10502 | ||
29208e82 | 10503 | DEFVAR_BOOL ("inhibit-eol-conversion", inhibit_eol_conversion, |
df7492f9 KH |
10504 | doc: /* |
10505 | *Non-nil means always inhibit code conversion of end-of-line format. | |
48b0f3ae PJ |
10506 | See info node `Coding Systems' and info node `Text and Binary' concerning |
10507 | such conversion. */); | |
9ce27fde KH |
10508 | inhibit_eol_conversion = 0; |
10509 | ||
29208e82 | 10510 | DEFVAR_BOOL ("inherit-process-coding-system", inherit_process_coding_system, |
df7492f9 KH |
10511 | doc: /* |
10512 | Non-nil means process buffer inherits coding system of process output. | |
48b0f3ae PJ |
10513 | Bind it to t if the process output is to be treated as if it were a file |
10514 | read from some filesystem. */); | |
ed29121d EZ |
10515 | inherit_process_coding_system = 0; |
10516 | ||
29208e82 | 10517 | DEFVAR_LISP ("file-coding-system-alist", Vfile_coding_system_alist, |
df7492f9 KH |
10518 | doc: /* |
10519 | Alist to decide a coding system to use for a file I/O operation. | |
48b0f3ae PJ |
10520 | The format is ((PATTERN . VAL) ...), |
10521 | where PATTERN is a regular expression matching a file name, | |
10522 | VAL is a coding system, a cons of coding systems, or a function symbol. | |
10523 | If VAL is a coding system, it is used for both decoding and encoding | |
10524 | the file contents. | |
10525 | If VAL is a cons of coding systems, the car part is used for decoding, | |
10526 | and the cdr part is used for encoding. | |
10527 | If VAL is a function symbol, the function must return a coding system | |
2c53e699 KH |
10528 | or a cons of coding systems which are used as above. The function is |
10529 | called with an argument that is a list of the arguments with which | |
5a0bbd9a KH |
10530 | `find-operation-coding-system' was called. If the function can't decide |
10531 | a coding system, it can return `undecided' so that the normal | |
10532 | code-detection is performed. | |
48b0f3ae PJ |
10533 | |
10534 | See also the function `find-operation-coding-system' | |
10535 | and the variable `auto-coding-alist'. */); | |
02ba4723 KH |
10536 | Vfile_coding_system_alist = Qnil; |
10537 | ||
29208e82 | 10538 | DEFVAR_LISP ("process-coding-system-alist", Vprocess_coding_system_alist, |
df7492f9 KH |
10539 | doc: /* |
10540 | Alist to decide a coding system to use for a process I/O operation. | |
48b0f3ae PJ |
10541 | The format is ((PATTERN . VAL) ...), |
10542 | where PATTERN is a regular expression matching a program name, | |
10543 | VAL is a coding system, a cons of coding systems, or a function symbol. | |
10544 | If VAL is a coding system, it is used for both decoding what received | |
10545 | from the program and encoding what sent to the program. | |
10546 | If VAL is a cons of coding systems, the car part is used for decoding, | |
10547 | and the cdr part is used for encoding. | |
10548 | If VAL is a function symbol, the function must return a coding system | |
10549 | or a cons of coding systems which are used as above. | |
10550 | ||
10551 | See also the function `find-operation-coding-system'. */); | |
02ba4723 KH |
10552 | Vprocess_coding_system_alist = Qnil; |
10553 | ||
29208e82 | 10554 | DEFVAR_LISP ("network-coding-system-alist", Vnetwork_coding_system_alist, |
df7492f9 KH |
10555 | doc: /* |
10556 | Alist to decide a coding system to use for a network I/O operation. | |
48b0f3ae PJ |
10557 | The format is ((PATTERN . VAL) ...), |
10558 | where PATTERN is a regular expression matching a network service name | |
10559 | or is a port number to connect to, | |
10560 | VAL is a coding system, a cons of coding systems, or a function symbol. | |
10561 | If VAL is a coding system, it is used for both decoding what received | |
10562 | from the network stream and encoding what sent to the network stream. | |
10563 | If VAL is a cons of coding systems, the car part is used for decoding, | |
10564 | and the cdr part is used for encoding. | |
10565 | If VAL is a function symbol, the function must return a coding system | |
10566 | or a cons of coding systems which are used as above. | |
10567 | ||
10568 | See also the function `find-operation-coding-system'. */); | |
02ba4723 | 10569 | Vnetwork_coding_system_alist = Qnil; |
4ed46869 | 10570 | |
29208e82 | 10571 | DEFVAR_LISP ("locale-coding-system", Vlocale_coding_system, |
75205970 RS |
10572 | doc: /* Coding system to use with system messages. |
10573 | Also used for decoding keyboard input on X Window system. */); | |
68c45bf0 PE |
10574 | Vlocale_coding_system = Qnil; |
10575 | ||
005f0d35 | 10576 | /* The eol mnemonics are reset in startup.el system-dependently. */ |
29208e82 | 10577 | DEFVAR_LISP ("eol-mnemonic-unix", eol_mnemonic_unix, |
df7492f9 KH |
10578 | doc: /* |
10579 | *String displayed in mode line for UNIX-like (LF) end-of-line format. */); | |
d67b4f80 | 10580 | eol_mnemonic_unix = make_pure_c_string (":"); |
4ed46869 | 10581 | |
29208e82 | 10582 | DEFVAR_LISP ("eol-mnemonic-dos", eol_mnemonic_dos, |
df7492f9 KH |
10583 | doc: /* |
10584 | *String displayed in mode line for DOS-like (CRLF) end-of-line format. */); | |
d67b4f80 | 10585 | eol_mnemonic_dos = make_pure_c_string ("\\"); |
4ed46869 | 10586 | |
29208e82 | 10587 | DEFVAR_LISP ("eol-mnemonic-mac", eol_mnemonic_mac, |
df7492f9 KH |
10588 | doc: /* |
10589 | *String displayed in mode line for MAC-like (CR) end-of-line format. */); | |
d67b4f80 | 10590 | eol_mnemonic_mac = make_pure_c_string ("/"); |
4ed46869 | 10591 | |
29208e82 | 10592 | DEFVAR_LISP ("eol-mnemonic-undecided", eol_mnemonic_undecided, |
df7492f9 KH |
10593 | doc: /* |
10594 | *String displayed in mode line when end-of-line format is not yet determined. */); | |
d67b4f80 | 10595 | eol_mnemonic_undecided = make_pure_c_string (":"); |
4ed46869 | 10596 | |
29208e82 | 10597 | DEFVAR_LISP ("enable-character-translation", Venable_character_translation, |
df7492f9 KH |
10598 | doc: /* |
10599 | *Non-nil enables character translation while encoding and decoding. */); | |
84fbb8a0 | 10600 | Venable_character_translation = Qt; |
bdd9fb48 | 10601 | |
f967223b | 10602 | DEFVAR_LISP ("standard-translation-table-for-decode", |
29208e82 | 10603 | Vstandard_translation_table_for_decode, |
48b0f3ae | 10604 | doc: /* Table for translating characters while decoding. */); |
f967223b | 10605 | Vstandard_translation_table_for_decode = Qnil; |
bdd9fb48 | 10606 | |
f967223b | 10607 | DEFVAR_LISP ("standard-translation-table-for-encode", |
29208e82 | 10608 | Vstandard_translation_table_for_encode, |
48b0f3ae | 10609 | doc: /* Table for translating characters while encoding. */); |
f967223b | 10610 | Vstandard_translation_table_for_encode = Qnil; |
4ed46869 | 10611 | |
29208e82 | 10612 | DEFVAR_LISP ("charset-revision-table", Vcharset_revision_table, |
48b0f3ae PJ |
10613 | doc: /* Alist of charsets vs revision numbers. |
10614 | While encoding, if a charset (car part of an element) is found, | |
df7492f9 KH |
10615 | designate it with the escape sequence identifying revision (cdr part |
10616 | of the element). */); | |
10617 | Vcharset_revision_table = Qnil; | |
02ba4723 KH |
10618 | |
10619 | DEFVAR_LISP ("default-process-coding-system", | |
29208e82 | 10620 | Vdefault_process_coding_system, |
48b0f3ae PJ |
10621 | doc: /* Cons of coding systems used for process I/O by default. |
10622 | The car part is used for decoding a process output, | |
10623 | the cdr part is used for encoding a text to be sent to a process. */); | |
02ba4723 | 10624 | Vdefault_process_coding_system = Qnil; |
c4825358 | 10625 | |
29208e82 | 10626 | DEFVAR_LISP ("latin-extra-code-table", Vlatin_extra_code_table, |
df7492f9 KH |
10627 | doc: /* |
10628 | Table of extra Latin codes in the range 128..159 (inclusive). | |
48b0f3ae PJ |
10629 | This is a vector of length 256. |
10630 | If Nth element is non-nil, the existence of code N in a file | |
10631 | \(or output of subprocess) doesn't prevent it to be detected as | |
10632 | a coding system of ISO 2022 variant which has a flag | |
10633 | `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file | |
10634 | or reading output of a subprocess. | |
446dcd75 | 10635 | Only 128th through 159th elements have a meaning. */); |
3f003981 | 10636 | Vlatin_extra_code_table = Fmake_vector (make_number (256), Qnil); |
d46c5b12 KH |
10637 | |
10638 | DEFVAR_LISP ("select-safe-coding-system-function", | |
29208e82 | 10639 | Vselect_safe_coding_system_function, |
df7492f9 KH |
10640 | doc: /* |
10641 | Function to call to select safe coding system for encoding a text. | |
48b0f3ae PJ |
10642 | |
10643 | If set, this function is called to force a user to select a proper | |
10644 | coding system which can encode the text in the case that a default | |
fdecf907 GM |
10645 | coding system used in each operation can't encode the text. The |
10646 | function should take care that the buffer is not modified while | |
10647 | the coding system is being selected. | |
48b0f3ae PJ |
10648 | |
10649 | The default value is `select-safe-coding-system' (which see). */); | |
d46c5b12 KH |
10650 | Vselect_safe_coding_system_function = Qnil; |
10651 | ||
5d5bf4d8 | 10652 | DEFVAR_BOOL ("coding-system-require-warning", |
29208e82 | 10653 | coding_system_require_warning, |
5d5bf4d8 | 10654 | doc: /* Internal use only. |
6b89e3aa KH |
10655 | If non-nil, on writing a file, `select-safe-coding-system-function' is |
10656 | called even if `coding-system-for-write' is non-nil. The command | |
10657 | `universal-coding-system-argument' binds this variable to t temporarily. */); | |
5d5bf4d8 KH |
10658 | coding_system_require_warning = 0; |
10659 | ||
10660 | ||
22ab2303 | 10661 | DEFVAR_BOOL ("inhibit-iso-escape-detection", |
29208e82 | 10662 | inhibit_iso_escape_detection, |
df7492f9 | 10663 | doc: /* |
97b1b294 | 10664 | If non-nil, Emacs ignores ISO-2022 escape sequences during code detection. |
48b0f3ae | 10665 | |
97b1b294 EZ |
10666 | When Emacs reads text, it tries to detect how the text is encoded. |
10667 | This code detection is sensitive to escape sequences. If Emacs sees | |
10668 | a valid ISO-2022 escape sequence, it assumes the text is encoded in one | |
10669 | of the ISO2022 encodings, and decodes text by the corresponding coding | |
10670 | system (e.g. `iso-2022-7bit'). | |
48b0f3ae PJ |
10671 | |
10672 | However, there may be a case that you want to read escape sequences in | |
10673 | a file as is. In such a case, you can set this variable to non-nil. | |
97b1b294 EZ |
10674 | Then the code detection will ignore any escape sequences, and no text is |
10675 | detected as encoded in some ISO-2022 encoding. The result is that all | |
48b0f3ae PJ |
10676 | escape sequences become visible in a buffer. |
10677 | ||
10678 | The default value is nil, and it is strongly recommended not to change | |
10679 | it. That is because many Emacs Lisp source files that contain | |
10680 | non-ASCII characters are encoded by the coding system `iso-2022-7bit' | |
10681 | in Emacs's distribution, and they won't be decoded correctly on | |
10682 | reading if you suppress escape sequence detection. | |
10683 | ||
10684 | The other way to read escape sequences in a file without decoding is | |
97b1b294 | 10685 | to explicitly specify some coding system that doesn't use ISO-2022 |
48b0f3ae | 10686 | escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */); |
74383408 | 10687 | inhibit_iso_escape_detection = 0; |
002fdb44 | 10688 | |
97b1b294 | 10689 | DEFVAR_BOOL ("inhibit-null-byte-detection", |
29208e82 | 10690 | inhibit_null_byte_detection, |
97b1b294 EZ |
10691 | doc: /* If non-nil, Emacs ignores null bytes on code detection. |
10692 | By default, Emacs treats it as binary data, and does not attempt to | |
10693 | decode it. The effect is as if you specified `no-conversion' for | |
10694 | reading that text. | |
10695 | ||
10696 | Set this to non-nil when a regular text happens to include null bytes. | |
10697 | Examples are Index nodes of Info files and null-byte delimited output | |
10698 | from GNU Find and GNU Grep. Emacs will then ignore the null bytes and | |
10699 | decode text as usual. */); | |
10700 | inhibit_null_byte_detection = 0; | |
10701 | ||
29208e82 | 10702 | DEFVAR_LISP ("translation-table-for-input", Vtranslation_table_for_input, |
15c8f9d1 | 10703 | doc: /* Char table for translating self-inserting characters. |
446dcd75 | 10704 | This is applied to the result of input methods, not their input. |
8434d0b8 EZ |
10705 | See also `keyboard-translate-table'. |
10706 | ||
10707 | Use of this variable for character code unification was rendered | |
10708 | obsolete in Emacs 23.1 and later, since Unicode is now the basis of | |
10709 | internal character representation. */); | |
002fdb44 | 10710 | Vtranslation_table_for_input = Qnil; |
8f924df7 | 10711 | |
2c78b7e1 KH |
10712 | { |
10713 | Lisp_Object args[coding_arg_max]; | |
8f924df7 | 10714 | Lisp_Object plist[16]; |
2c78b7e1 KH |
10715 | int i; |
10716 | ||
10717 | for (i = 0; i < coding_arg_max; i++) | |
10718 | args[i] = Qnil; | |
10719 | ||
d67b4f80 | 10720 | plist[0] = intern_c_string (":name"); |
2c78b7e1 | 10721 | plist[1] = args[coding_arg_name] = Qno_conversion; |
d67b4f80 | 10722 | plist[2] = intern_c_string (":mnemonic"); |
2c78b7e1 | 10723 | plist[3] = args[coding_arg_mnemonic] = make_number ('='); |
d67b4f80 | 10724 | plist[4] = intern_c_string (":coding-type"); |
2c78b7e1 | 10725 | plist[5] = args[coding_arg_coding_type] = Qraw_text; |
d67b4f80 | 10726 | plist[6] = intern_c_string (":ascii-compatible-p"); |
2c78b7e1 | 10727 | plist[7] = args[coding_arg_ascii_compatible_p] = Qt; |
d67b4f80 | 10728 | plist[8] = intern_c_string (":default-char"); |
2c78b7e1 | 10729 | plist[9] = args[coding_arg_default_char] = make_number (0); |
d67b4f80 | 10730 | plist[10] = intern_c_string (":for-unibyte"); |
8f924df7 | 10731 | plist[11] = args[coding_arg_for_unibyte] = Qt; |
d67b4f80 DN |
10732 | plist[12] = intern_c_string (":docstring"); |
10733 | plist[13] = make_pure_c_string ("Do no conversion.\n\ | |
2c78b7e1 KH |
10734 | \n\ |
10735 | When you visit a file with this coding, the file is read into a\n\ | |
10736 | unibyte buffer as is, thus each byte of a file is treated as a\n\ | |
10737 | character."); | |
d67b4f80 | 10738 | plist[14] = intern_c_string (":eol-type"); |
8f924df7 KH |
10739 | plist[15] = args[coding_arg_eol_type] = Qunix; |
10740 | args[coding_arg_plist] = Flist (16, plist); | |
2c78b7e1 | 10741 | Fdefine_coding_system_internal (coding_arg_max, args); |
ae6f73fa KH |
10742 | |
10743 | plist[1] = args[coding_arg_name] = Qundecided; | |
10744 | plist[3] = args[coding_arg_mnemonic] = make_number ('-'); | |
10745 | plist[5] = args[coding_arg_coding_type] = Qundecided; | |
10746 | /* This is already set. | |
35befdaa | 10747 | plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */ |
d67b4f80 | 10748 | plist[8] = intern_c_string (":charset-list"); |
ae6f73fa KH |
10749 | plist[9] = args[coding_arg_charset_list] = Fcons (Qascii, Qnil); |
10750 | plist[11] = args[coding_arg_for_unibyte] = Qnil; | |
d67b4f80 | 10751 | plist[13] = make_pure_c_string ("No conversion on encoding, automatic conversion on decoding."); |
ae6f73fa KH |
10752 | plist[15] = args[coding_arg_eol_type] = Qnil; |
10753 | args[coding_arg_plist] = Flist (16, plist); | |
10754 | Fdefine_coding_system_internal (coding_arg_max, args); | |
2c78b7e1 KH |
10755 | } |
10756 | ||
2c78b7e1 | 10757 | setup_coding_system (Qno_conversion, &safe_terminal_coding); |
ff563fce KH |
10758 | |
10759 | { | |
10760 | int i; | |
10761 | ||
10762 | for (i = 0; i < coding_category_max; i++) | |
10763 | Fset (AREF (Vcoding_category_table, i), Qno_conversion); | |
10764 | } | |
1a4990fb | 10765 | #if defined (DOS_NT) |
fcbcfb64 KH |
10766 | system_eol_type = Qdos; |
10767 | #else | |
10768 | system_eol_type = Qunix; | |
10769 | #endif | |
10770 | staticpro (&system_eol_type); | |
4ed46869 KH |
10771 | } |
10772 | ||
68c45bf0 | 10773 | char * |
971de7fb | 10774 | emacs_strerror (int error_number) |
68c45bf0 PE |
10775 | { |
10776 | char *str; | |
10777 | ||
ca9c0567 | 10778 | synchronize_system_messages_locale (); |
68c45bf0 PE |
10779 | str = strerror (error_number); |
10780 | ||
10781 | if (! NILP (Vlocale_coding_system)) | |
10782 | { | |
10783 | Lisp_Object dec = code_convert_string_norecord (build_string (str), | |
10784 | Vlocale_coding_system, | |
10785 | 0); | |
51b59d79 | 10786 | str = SSDATA (dec); |
68c45bf0 PE |
10787 | } |
10788 | ||
10789 | return str; | |
10790 | } | |
10791 | ||
4ed46869 | 10792 | #endif /* emacs */ |