Commit | Line | Data |
---|---|---|
9542cb1f | 1 | /* Coding system handler (conversion, detection, etc). |
ab422c4d | 2 | Copyright (C) 2001-2013 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 | |
34809aa6 EZ |
58 | In Emacs Lisp, a coding system is represented by a Lisp symbol. On |
59 | the 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 | |
f10fe38f | 150 | Return true if the byte sequence conforms to XXX. |
df7492f9 KH |
151 | |
152 | Below is the template of these functions. */ | |
153 | ||
4ed46869 | 154 | #if 0 |
f10fe38f | 155 | static bool |
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; | |
f10fe38f | 161 | bool multibytep = coding->src_multibyte; |
d311d28c | 162 | ptrdiff_t 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; | |
f10fe38f | 215 | bool multibytep = coding->src_multibyte; |
df7492f9 KH |
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 | { |
f10fe38f | 263 | bool multibytep = coding->dst_multibyte; |
df7492f9 KH |
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_; | |
d311d28c | 269 | ptrdiff_t produced_chars = 0; |
df7492f9 KH |
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 KH |
287 | #include <stdio.h> |
288 | ||
819e2da9 DC |
289 | #ifdef HAVE_WCHAR_H |
290 | #include <wchar.h> | |
291 | #endif /* HAVE_WCHAR_H */ | |
292 | ||
4ed46869 | 293 | #include "lisp.h" |
df7492f9 | 294 | #include "character.h" |
e5560ff7 | 295 | #include "buffer.h" |
4ed46869 KH |
296 | #include "charset.h" |
297 | #include "ccl.h" | |
df7492f9 | 298 | #include "composite.h" |
4ed46869 KH |
299 | #include "coding.h" |
300 | #include "window.h" | |
b8299c66 KL |
301 | #include "frame.h" |
302 | #include "termhooks.h" | |
4ed46869 | 303 | |
df7492f9 | 304 | Lisp_Object Vcoding_system_hash_table; |
4ed46869 | 305 | |
955cbe7b PE |
306 | static Lisp_Object Qcoding_system, Qeol_type; |
307 | static Lisp_Object Qcoding_aliases; | |
84cc1ab6 PE |
308 | Lisp_Object Qunix, Qdos; |
309 | static Lisp_Object Qmac; | |
4ed46869 | 310 | Lisp_Object Qbuffer_file_coding_system; |
955cbe7b PE |
311 | static Lisp_Object Qpost_read_conversion, Qpre_write_conversion; |
312 | static Lisp_Object Qdefault_char; | |
27901516 | 313 | Lisp_Object Qno_conversion, Qundecided; |
955cbe7b PE |
314 | Lisp_Object Qcharset, Qutf_8; |
315 | static Lisp_Object Qiso_2022; | |
316 | static Lisp_Object Qutf_16, Qshift_jis, Qbig5; | |
317 | static Lisp_Object Qbig, Qlittle; | |
318 | static Lisp_Object Qcoding_system_history; | |
319 | static Lisp_Object Qvalid_codes; | |
320 | static Lisp_Object QCcategory, QCmnemonic, QCdefault_char; | |
321 | static Lisp_Object QCdecode_translation_table, QCencode_translation_table; | |
322 | static Lisp_Object QCpost_read_conversion, QCpre_write_conversion; | |
323 | static Lisp_Object QCascii_compatible_p; | |
4ed46869 | 324 | |
387f6ba5 | 325 | Lisp_Object Qcall_process, Qcall_process_region; |
4ed46869 | 326 | Lisp_Object Qstart_process, Qopen_network_stream; |
955cbe7b | 327 | static Lisp_Object Qtarget_idx; |
4ed46869 | 328 | |
1af1a51a | 329 | static Lisp_Object Qinsufficient_source, Qinvalid_source, Qinterrupted; |
065e3595 | 330 | |
44e8490d KH |
331 | /* If a symbol has this property, evaluate the value to define the |
332 | symbol as a coding system. */ | |
333 | static Lisp_Object Qcoding_system_define_form; | |
334 | ||
fcbcfb64 KH |
335 | /* Format of end-of-line decided by system. This is Qunix on |
336 | Unix and Mac, Qdos on DOS/Windows. | |
337 | This has an effect only for external encoding (i.e. for output to | |
338 | file and process), not for in-buffer or Lisp string encoding. */ | |
339 | static Lisp_Object system_eol_type; | |
340 | ||
4ed46869 KH |
341 | #ifdef emacs |
342 | ||
4608c386 | 343 | Lisp_Object Qcoding_system_p, Qcoding_system_error; |
4ed46869 | 344 | |
d46c5b12 KH |
345 | /* Coding system emacs-mule and raw-text are for converting only |
346 | end-of-line format. */ | |
347 | Lisp_Object Qemacs_mule, Qraw_text; | |
8f924df7 | 348 | Lisp_Object Qutf_8_emacs; |
ecf488bc | 349 | |
53372c27 | 350 | #if defined (WINDOWSNT) || defined (CYGWIN) |
ba116008 DC |
351 | static Lisp_Object Qutf_16le; |
352 | #endif | |
353 | ||
4ed46869 KH |
354 | /* Coding-systems are handed between Emacs Lisp programs and C internal |
355 | routines by the following three variables. */ | |
c4825358 KH |
356 | /* Coding system to be used to encode text for terminal display when |
357 | terminal coding system is nil. */ | |
358 | struct coding_system safe_terminal_coding; | |
359 | ||
4ed46869 KH |
360 | #endif /* emacs */ |
361 | ||
f967223b KH |
362 | Lisp_Object Qtranslation_table; |
363 | Lisp_Object Qtranslation_table_id; | |
955cbe7b PE |
364 | static Lisp_Object Qtranslation_table_for_decode; |
365 | static Lisp_Object Qtranslation_table_for_encode; | |
4ed46869 | 366 | |
df7492f9 | 367 | /* Two special coding systems. */ |
74ab6df5 PE |
368 | static Lisp_Object Vsjis_coding_system; |
369 | static Lisp_Object Vbig5_coding_system; | |
df7492f9 | 370 | |
df7492f9 KH |
371 | /* ISO2022 section */ |
372 | ||
373 | #define CODING_ISO_INITIAL(coding, reg) \ | |
374 | (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \ | |
375 | coding_attr_iso_initial), \ | |
376 | reg))) | |
377 | ||
378 | ||
1b3b981b AS |
379 | #define CODING_ISO_REQUEST(coding, charset_id) \ |
380 | (((charset_id) <= (coding)->max_charset_id \ | |
381 | ? ((coding)->safe_charsets[charset_id] != 255 \ | |
382 | ? (coding)->safe_charsets[charset_id] \ | |
383 | : -1) \ | |
df7492f9 KH |
384 | : -1)) |
385 | ||
386 | ||
387 | #define CODING_ISO_FLAGS(coding) \ | |
388 | ((coding)->spec.iso_2022.flags) | |
389 | #define CODING_ISO_DESIGNATION(coding, reg) \ | |
390 | ((coding)->spec.iso_2022.current_designation[reg]) | |
391 | #define CODING_ISO_INVOCATION(coding, plane) \ | |
392 | ((coding)->spec.iso_2022.current_invocation[plane]) | |
393 | #define CODING_ISO_SINGLE_SHIFTING(coding) \ | |
394 | ((coding)->spec.iso_2022.single_shifting) | |
395 | #define CODING_ISO_BOL(coding) \ | |
396 | ((coding)->spec.iso_2022.bol) | |
397 | #define CODING_ISO_INVOKED_CHARSET(coding, plane) \ | |
398 | CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane))) | |
e951386e KH |
399 | #define CODING_ISO_CMP_STATUS(coding) \ |
400 | (&(coding)->spec.iso_2022.cmp_status) | |
401 | #define CODING_ISO_EXTSEGMENT_LEN(coding) \ | |
402 | ((coding)->spec.iso_2022.ctext_extended_segment_len) | |
403 | #define CODING_ISO_EMBEDDED_UTF_8(coding) \ | |
404 | ((coding)->spec.iso_2022.embedded_utf_8) | |
df7492f9 KH |
405 | |
406 | /* Control characters of ISO2022. */ | |
407 | /* code */ /* function */ | |
df7492f9 KH |
408 | #define ISO_CODE_SO 0x0E /* shift-out */ |
409 | #define ISO_CODE_SI 0x0F /* shift-in */ | |
410 | #define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */ | |
411 | #define ISO_CODE_ESC 0x1B /* escape */ | |
412 | #define ISO_CODE_SS2 0x8E /* single-shift-2 */ | |
413 | #define ISO_CODE_SS3 0x8F /* single-shift-3 */ | |
414 | #define ISO_CODE_CSI 0x9B /* control-sequence-introducer */ | |
415 | ||
416 | /* All code (1-byte) of ISO2022 is classified into one of the | |
417 | followings. */ | |
418 | enum iso_code_class_type | |
419 | { | |
420 | ISO_control_0, /* Control codes in the range | |
421 | 0x00..0x1F and 0x7F, except for the | |
422 | following 5 codes. */ | |
df7492f9 KH |
423 | ISO_shift_out, /* ISO_CODE_SO (0x0E) */ |
424 | ISO_shift_in, /* ISO_CODE_SI (0x0F) */ | |
425 | ISO_single_shift_2_7, /* ISO_CODE_SS2_7 (0x19) */ | |
a0d7415f | 426 | ISO_escape, /* ISO_CODE_ESC (0x1B) */ |
df7492f9 KH |
427 | ISO_control_1, /* Control codes in the range |
428 | 0x80..0x9F, except for the | |
429 | following 3 codes. */ | |
430 | ISO_single_shift_2, /* ISO_CODE_SS2 (0x8E) */ | |
431 | ISO_single_shift_3, /* ISO_CODE_SS3 (0x8F) */ | |
432 | ISO_control_sequence_introducer, /* ISO_CODE_CSI (0x9B) */ | |
433 | ISO_0x20_or_0x7F, /* Codes of the values 0x20 or 0x7F. */ | |
434 | ISO_graphic_plane_0, /* Graphic codes in the range 0x21..0x7E. */ | |
435 | ISO_0xA0_or_0xFF, /* Codes of the values 0xA0 or 0xFF. */ | |
436 | ISO_graphic_plane_1 /* Graphic codes in the range 0xA1..0xFE. */ | |
437 | }; | |
05e6f5dc | 438 | |
df7492f9 KH |
439 | /** The macros CODING_ISO_FLAG_XXX defines a flag bit of the |
440 | `iso-flags' attribute of an iso2022 coding system. */ | |
05e6f5dc | 441 | |
df7492f9 KH |
442 | /* If set, produce long-form designation sequence (e.g. ESC $ ( A) |
443 | instead of the correct short-form sequence (e.g. ESC $ A). */ | |
444 | #define CODING_ISO_FLAG_LONG_FORM 0x0001 | |
93dec019 | 445 | |
df7492f9 KH |
446 | /* If set, reset graphic planes and registers at end-of-line to the |
447 | initial state. */ | |
448 | #define CODING_ISO_FLAG_RESET_AT_EOL 0x0002 | |
05e6f5dc | 449 | |
df7492f9 KH |
450 | /* If set, reset graphic planes and registers before any control |
451 | characters to the initial state. */ | |
452 | #define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004 | |
05e6f5dc | 453 | |
df7492f9 KH |
454 | /* If set, encode by 7-bit environment. */ |
455 | #define CODING_ISO_FLAG_SEVEN_BITS 0x0008 | |
4ed46869 | 456 | |
df7492f9 KH |
457 | /* If set, use locking-shift function. */ |
458 | #define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010 | |
b73bfc1c | 459 | |
df7492f9 KH |
460 | /* If set, use single-shift function. Overwrite |
461 | CODING_ISO_FLAG_LOCKING_SHIFT. */ | |
462 | #define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020 | |
b73bfc1c | 463 | |
df7492f9 KH |
464 | /* If set, use designation escape sequence. */ |
465 | #define CODING_ISO_FLAG_DESIGNATION 0x0040 | |
b73bfc1c | 466 | |
df7492f9 KH |
467 | /* If set, produce revision number sequence. */ |
468 | #define CODING_ISO_FLAG_REVISION 0x0080 | |
b73bfc1c | 469 | |
df7492f9 KH |
470 | /* If set, produce ISO6429's direction specifying sequence. */ |
471 | #define CODING_ISO_FLAG_DIRECTION 0x0100 | |
f4dee582 | 472 | |
df7492f9 KH |
473 | /* If set, assume designation states are reset at beginning of line on |
474 | output. */ | |
475 | #define CODING_ISO_FLAG_INIT_AT_BOL 0x0200 | |
4ed46869 | 476 | |
df7492f9 KH |
477 | /* If set, designation sequence should be placed at beginning of line |
478 | on output. */ | |
479 | #define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400 | |
aa72b389 | 480 | |
ad1746f5 | 481 | /* If set, do not encode unsafe characters on output. */ |
df7492f9 | 482 | #define CODING_ISO_FLAG_SAFE 0x0800 |
aa72b389 | 483 | |
df7492f9 KH |
484 | /* If set, extra latin codes (128..159) are accepted as a valid code |
485 | on input. */ | |
486 | #define CODING_ISO_FLAG_LATIN_EXTRA 0x1000 | |
aa72b389 | 487 | |
df7492f9 | 488 | #define CODING_ISO_FLAG_COMPOSITION 0x2000 |
aa72b389 | 489 | |
5f58e762 | 490 | /* #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000 */ |
aa72b389 | 491 | |
bf16eb23 | 492 | #define CODING_ISO_FLAG_USE_ROMAN 0x8000 |
aa72b389 | 493 | |
bf16eb23 | 494 | #define CODING_ISO_FLAG_USE_OLDJIS 0x10000 |
aa72b389 | 495 | |
99107004 KH |
496 | #define CODING_ISO_FLAG_LEVEL_4 0x20000 |
497 | ||
bf16eb23 | 498 | #define CODING_ISO_FLAG_FULL_SUPPORT 0x100000 |
aa72b389 | 499 | |
df7492f9 KH |
500 | /* A character to be produced on output if encoding of the original |
501 | character is prohibited by CODING_ISO_FLAG_SAFE. */ | |
502 | #define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?' | |
aa72b389 | 503 | |
a470d443 KH |
504 | /* UTF-8 section */ |
505 | #define CODING_UTF_8_BOM(coding) \ | |
506 | ((coding)->spec.utf_8_bom) | |
4ed46869 | 507 | |
df7492f9 KH |
508 | /* UTF-16 section */ |
509 | #define CODING_UTF_16_BOM(coding) \ | |
510 | ((coding)->spec.utf_16.bom) | |
4ed46869 | 511 | |
df7492f9 KH |
512 | #define CODING_UTF_16_ENDIAN(coding) \ |
513 | ((coding)->spec.utf_16.endian) | |
4ed46869 | 514 | |
df7492f9 KH |
515 | #define CODING_UTF_16_SURROGATE(coding) \ |
516 | ((coding)->spec.utf_16.surrogate) | |
4ed46869 | 517 | |
4ed46869 | 518 | |
df7492f9 KH |
519 | /* CCL section */ |
520 | #define CODING_CCL_DECODER(coding) \ | |
521 | AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder) | |
522 | #define CODING_CCL_ENCODER(coding) \ | |
523 | AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder) | |
524 | #define CODING_CCL_VALIDS(coding) \ | |
8f924df7 | 525 | (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids))) |
4ed46869 | 526 | |
5a936b46 | 527 | /* Index for each coding category in `coding_categories' */ |
4ed46869 | 528 | |
df7492f9 KH |
529 | enum coding_category |
530 | { | |
531 | coding_category_iso_7, | |
532 | coding_category_iso_7_tight, | |
533 | coding_category_iso_8_1, | |
534 | coding_category_iso_8_2, | |
535 | coding_category_iso_7_else, | |
536 | coding_category_iso_8_else, | |
a470d443 KH |
537 | coding_category_utf_8_auto, |
538 | coding_category_utf_8_nosig, | |
539 | coding_category_utf_8_sig, | |
df7492f9 KH |
540 | coding_category_utf_16_auto, |
541 | coding_category_utf_16_be, | |
542 | coding_category_utf_16_le, | |
543 | coding_category_utf_16_be_nosig, | |
544 | coding_category_utf_16_le_nosig, | |
545 | coding_category_charset, | |
546 | coding_category_sjis, | |
547 | coding_category_big5, | |
548 | coding_category_ccl, | |
549 | coding_category_emacs_mule, | |
550 | /* All above are targets of code detection. */ | |
551 | coding_category_raw_text, | |
552 | coding_category_undecided, | |
553 | coding_category_max | |
554 | }; | |
555 | ||
556 | /* Definitions of flag bits used in detect_coding_XXXX. */ | |
557 | #define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7) | |
558 | #define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight) | |
559 | #define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1) | |
560 | #define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2) | |
561 | #define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else) | |
562 | #define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else) | |
a470d443 KH |
563 | #define CATEGORY_MASK_UTF_8_AUTO (1 << coding_category_utf_8_auto) |
564 | #define CATEGORY_MASK_UTF_8_NOSIG (1 << coding_category_utf_8_nosig) | |
565 | #define CATEGORY_MASK_UTF_8_SIG (1 << coding_category_utf_8_sig) | |
b49a1807 | 566 | #define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto) |
df7492f9 KH |
567 | #define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be) |
568 | #define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le) | |
569 | #define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig) | |
570 | #define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig) | |
571 | #define CATEGORY_MASK_CHARSET (1 << coding_category_charset) | |
572 | #define CATEGORY_MASK_SJIS (1 << coding_category_sjis) | |
573 | #define CATEGORY_MASK_BIG5 (1 << coding_category_big5) | |
574 | #define CATEGORY_MASK_CCL (1 << coding_category_ccl) | |
575 | #define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule) | |
ff0dacd7 | 576 | #define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text) |
df7492f9 KH |
577 | |
578 | /* This value is returned if detect_coding_mask () find nothing other | |
579 | than ASCII characters. */ | |
580 | #define CATEGORY_MASK_ANY \ | |
581 | (CATEGORY_MASK_ISO_7 \ | |
582 | | CATEGORY_MASK_ISO_7_TIGHT \ | |
583 | | CATEGORY_MASK_ISO_8_1 \ | |
584 | | CATEGORY_MASK_ISO_8_2 \ | |
585 | | CATEGORY_MASK_ISO_7_ELSE \ | |
586 | | CATEGORY_MASK_ISO_8_ELSE \ | |
a470d443 KH |
587 | | CATEGORY_MASK_UTF_8_AUTO \ |
588 | | CATEGORY_MASK_UTF_8_NOSIG \ | |
589 | | CATEGORY_MASK_UTF_8_SIG \ | |
2f3cbb32 | 590 | | CATEGORY_MASK_UTF_16_AUTO \ |
df7492f9 KH |
591 | | CATEGORY_MASK_UTF_16_BE \ |
592 | | CATEGORY_MASK_UTF_16_LE \ | |
593 | | CATEGORY_MASK_UTF_16_BE_NOSIG \ | |
594 | | CATEGORY_MASK_UTF_16_LE_NOSIG \ | |
595 | | CATEGORY_MASK_CHARSET \ | |
596 | | CATEGORY_MASK_SJIS \ | |
597 | | CATEGORY_MASK_BIG5 \ | |
598 | | CATEGORY_MASK_CCL \ | |
599 | | CATEGORY_MASK_EMACS_MULE) | |
600 | ||
601 | ||
602 | #define CATEGORY_MASK_ISO_7BIT \ | |
603 | (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT) | |
604 | ||
605 | #define CATEGORY_MASK_ISO_8BIT \ | |
606 | (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2) | |
607 | ||
608 | #define CATEGORY_MASK_ISO_ELSE \ | |
609 | (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE) | |
610 | ||
611 | #define CATEGORY_MASK_ISO_ESCAPE \ | |
612 | (CATEGORY_MASK_ISO_7 \ | |
613 | | CATEGORY_MASK_ISO_7_TIGHT \ | |
614 | | CATEGORY_MASK_ISO_7_ELSE \ | |
615 | | CATEGORY_MASK_ISO_8_ELSE) | |
616 | ||
617 | #define CATEGORY_MASK_ISO \ | |
618 | ( CATEGORY_MASK_ISO_7BIT \ | |
619 | | CATEGORY_MASK_ISO_8BIT \ | |
620 | | CATEGORY_MASK_ISO_ELSE) | |
621 | ||
622 | #define CATEGORY_MASK_UTF_16 \ | |
2f3cbb32 KH |
623 | (CATEGORY_MASK_UTF_16_AUTO \ |
624 | | CATEGORY_MASK_UTF_16_BE \ | |
df7492f9 KH |
625 | | CATEGORY_MASK_UTF_16_LE \ |
626 | | CATEGORY_MASK_UTF_16_BE_NOSIG \ | |
627 | | CATEGORY_MASK_UTF_16_LE_NOSIG) | |
628 | ||
a470d443 KH |
629 | #define CATEGORY_MASK_UTF_8 \ |
630 | (CATEGORY_MASK_UTF_8_AUTO \ | |
631 | | CATEGORY_MASK_UTF_8_NOSIG \ | |
632 | | CATEGORY_MASK_UTF_8_SIG) | |
df7492f9 | 633 | |
df7492f9 | 634 | /* Table of coding categories (Lisp symbols). This variable is for |
ad1746f5 | 635 | internal use only. */ |
df7492f9 KH |
636 | static Lisp_Object Vcoding_category_table; |
637 | ||
638 | /* Table of coding-categories ordered by priority. */ | |
639 | static enum coding_category coding_priorities[coding_category_max]; | |
640 | ||
641 | /* Nth element is a coding context for the coding system bound to the | |
642 | Nth coding category. */ | |
643 | static struct coding_system coding_categories[coding_category_max]; | |
644 | ||
df7492f9 KH |
645 | /*** Commonly used macros and functions ***/ |
646 | ||
647 | #ifndef min | |
648 | #define min(a, b) ((a) < (b) ? (a) : (b)) | |
649 | #endif | |
650 | #ifndef max | |
651 | #define max(a, b) ((a) > (b) ? (a) : (b)) | |
652 | #endif | |
4ed46869 | 653 | |
9c90cc06 PE |
654 | /* Encode a flag that can be nil, something else, or t as -1, 0, 1. */ |
655 | ||
656 | static int | |
657 | encode_inhibit_flag (Lisp_Object flag) | |
658 | { | |
659 | return NILP (flag) ? -1 : EQ (flag, Qt); | |
660 | } | |
661 | ||
662 | /* True if the value of ENCODED_FLAG says a flag should be treated as set. | |
663 | 1 means yes, -1 means no, 0 means ask the user variable VAR. */ | |
664 | ||
665 | static bool | |
666 | inhibit_flag (int encoded_flag, bool var) | |
667 | { | |
668 | return 0 < encoded_flag + var; | |
669 | } | |
670 | ||
24a73b0a KH |
671 | #define CODING_GET_INFO(coding, attrs, charset_list) \ |
672 | do { \ | |
673 | (attrs) = CODING_ID_ATTRS ((coding)->id); \ | |
674 | (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \ | |
df7492f9 | 675 | } while (0) |
4ed46869 | 676 | |
84575e67 PE |
677 | static void |
678 | CHECK_NATNUM_CAR (Lisp_Object x) | |
679 | { | |
680 | Lisp_Object tmp = XCAR (x); | |
681 | CHECK_NATNUM (tmp); | |
682 | XSETCAR (x, tmp); | |
683 | } | |
684 | ||
685 | static void | |
686 | CHECK_NATNUM_CDR (Lisp_Object x) | |
687 | { | |
688 | Lisp_Object tmp = XCDR (x); | |
689 | CHECK_NATNUM (tmp); | |
690 | XSETCDR (x, tmp); | |
691 | } | |
692 | ||
4ed46869 | 693 | |
df7492f9 KH |
694 | /* Safely get one byte from the source text pointed by SRC which ends |
695 | at SRC_END, and set C to that byte. If there are not enough bytes | |
f10fe38f PE |
696 | in the source, it jumps to 'no_more_source'. If MULTIBYTEP, |
697 | and a multibyte character is found at SRC, set C to the | |
065e3595 KH |
698 | negative value of the character code. The caller should declare |
699 | and set these variables appropriately in advance: | |
700 | src, src_end, multibytep */ | |
aa72b389 | 701 | |
065e3595 KH |
702 | #define ONE_MORE_BYTE(c) \ |
703 | do { \ | |
704 | if (src == src_end) \ | |
705 | { \ | |
706 | if (src_base < src) \ | |
707 | record_conversion_result \ | |
708 | (coding, CODING_RESULT_INSUFFICIENT_SRC); \ | |
709 | goto no_more_source; \ | |
710 | } \ | |
711 | c = *src++; \ | |
712 | if (multibytep && (c & 0x80)) \ | |
713 | { \ | |
714 | if ((c & 0xFE) == 0xC0) \ | |
715 | c = ((c & 1) << 6) | *src++; \ | |
716 | else \ | |
717 | { \ | |
35befdaa KH |
718 | src--; \ |
719 | c = - string_char (src, &src, NULL); \ | |
065e3595 KH |
720 | record_conversion_result \ |
721 | (coding, CODING_RESULT_INVALID_SRC); \ | |
722 | } \ | |
723 | } \ | |
724 | consumed_chars++; \ | |
aa72b389 KH |
725 | } while (0) |
726 | ||
f56a4450 | 727 | /* Safely get two bytes from the source text pointed by SRC which ends |
220eeac9 KH |
728 | at SRC_END, and set C1 and C2 to those bytes while skipping the |
729 | heading multibyte characters. If there are not enough bytes in the | |
f10fe38f | 730 | source, it jumps to 'no_more_source'. If MULTIBYTEP and |
220eeac9 KH |
731 | a multibyte character is found for C2, set C2 to the negative value |
732 | of the character code. The caller should declare and set these | |
733 | variables appropriately in advance: | |
f56a4450 KH |
734 | src, src_end, multibytep |
735 | It is intended that this macro is used in detect_coding_utf_16. */ | |
736 | ||
220eeac9 KH |
737 | #define TWO_MORE_BYTES(c1, c2) \ |
738 | do { \ | |
739 | do { \ | |
740 | if (src == src_end) \ | |
741 | goto no_more_source; \ | |
742 | c1 = *src++; \ | |
743 | if (multibytep && (c1 & 0x80)) \ | |
744 | { \ | |
745 | if ((c1 & 0xFE) == 0xC0) \ | |
746 | c1 = ((c1 & 1) << 6) | *src++; \ | |
747 | else \ | |
748 | { \ | |
749 | src += BYTES_BY_CHAR_HEAD (c1) - 1; \ | |
750 | c1 = -1; \ | |
751 | } \ | |
752 | } \ | |
753 | } while (c1 < 0); \ | |
754 | if (src == src_end) \ | |
755 | goto no_more_source; \ | |
756 | c2 = *src++; \ | |
757 | if (multibytep && (c2 & 0x80)) \ | |
758 | { \ | |
759 | if ((c2 & 0xFE) == 0xC0) \ | |
760 | c2 = ((c2 & 1) << 6) | *src++; \ | |
761 | else \ | |
762 | c2 = -1; \ | |
763 | } \ | |
f56a4450 KH |
764 | } while (0) |
765 | ||
aa72b389 | 766 | |
df7492f9 KH |
767 | /* Store a byte C in the place pointed by DST and increment DST to the |
768 | next free point, and increment PRODUCED_CHARS. The caller should | |
769 | assure that C is 0..127, and declare and set the variable `dst' | |
770 | appropriately in advance. | |
771 | */ | |
aa72b389 KH |
772 | |
773 | ||
df7492f9 KH |
774 | #define EMIT_ONE_ASCII_BYTE(c) \ |
775 | do { \ | |
776 | produced_chars++; \ | |
777 | *dst++ = (c); \ | |
b6871cc7 | 778 | } while (0) |
aa72b389 KH |
779 | |
780 | ||
ad1746f5 | 781 | /* Like EMIT_ONE_ASCII_BYTE but store two bytes; C1 and C2. */ |
aa72b389 | 782 | |
df7492f9 KH |
783 | #define EMIT_TWO_ASCII_BYTES(c1, c2) \ |
784 | do { \ | |
785 | produced_chars += 2; \ | |
786 | *dst++ = (c1), *dst++ = (c2); \ | |
787 | } while (0) | |
aa72b389 KH |
788 | |
789 | ||
df7492f9 | 790 | /* Store a byte C in the place pointed by DST and increment DST to the |
f10fe38f PE |
791 | next free point, and increment PRODUCED_CHARS. If MULTIBYTEP, |
792 | store in an appropriate multibyte form. The caller should | |
df7492f9 KH |
793 | declare and set the variables `dst' and `multibytep' appropriately |
794 | in advance. */ | |
795 | ||
796 | #define EMIT_ONE_BYTE(c) \ | |
797 | do { \ | |
798 | produced_chars++; \ | |
799 | if (multibytep) \ | |
800 | { \ | |
b25d760e | 801 | unsigned ch = (c); \ |
df7492f9 KH |
802 | if (ch >= 0x80) \ |
803 | ch = BYTE8_TO_CHAR (ch); \ | |
804 | CHAR_STRING_ADVANCE (ch, dst); \ | |
805 | } \ | |
806 | else \ | |
807 | *dst++ = (c); \ | |
aa72b389 | 808 | } while (0) |
aa72b389 | 809 | |
aa72b389 | 810 | |
df7492f9 | 811 | /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */ |
aa72b389 | 812 | |
e19c3639 KH |
813 | #define EMIT_TWO_BYTES(c1, c2) \ |
814 | do { \ | |
815 | produced_chars += 2; \ | |
816 | if (multibytep) \ | |
817 | { \ | |
b25d760e | 818 | unsigned ch; \ |
e19c3639 KH |
819 | \ |
820 | ch = (c1); \ | |
821 | if (ch >= 0x80) \ | |
822 | ch = BYTE8_TO_CHAR (ch); \ | |
823 | CHAR_STRING_ADVANCE (ch, dst); \ | |
824 | ch = (c2); \ | |
825 | if (ch >= 0x80) \ | |
826 | ch = BYTE8_TO_CHAR (ch); \ | |
827 | CHAR_STRING_ADVANCE (ch, dst); \ | |
828 | } \ | |
829 | else \ | |
830 | { \ | |
831 | *dst++ = (c1); \ | |
832 | *dst++ = (c2); \ | |
833 | } \ | |
aa72b389 KH |
834 | } while (0) |
835 | ||
836 | ||
df7492f9 KH |
837 | #define EMIT_THREE_BYTES(c1, c2, c3) \ |
838 | do { \ | |
839 | EMIT_ONE_BYTE (c1); \ | |
840 | EMIT_TWO_BYTES (c2, c3); \ | |
841 | } while (0) | |
aa72b389 | 842 | |
aa72b389 | 843 | |
df7492f9 KH |
844 | #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \ |
845 | do { \ | |
846 | EMIT_TWO_BYTES (c1, c2); \ | |
847 | EMIT_TWO_BYTES (c3, c4); \ | |
848 | } while (0) | |
aa72b389 | 849 | |
aa72b389 | 850 | |
065e3595 KH |
851 | static void |
852 | record_conversion_result (struct coding_system *coding, | |
853 | enum coding_result_code result) | |
854 | { | |
855 | coding->result = result; | |
856 | switch (result) | |
857 | { | |
858 | case CODING_RESULT_INSUFFICIENT_SRC: | |
859 | Vlast_code_conversion_error = Qinsufficient_source; | |
860 | break; | |
065e3595 KH |
861 | case CODING_RESULT_INVALID_SRC: |
862 | Vlast_code_conversion_error = Qinvalid_source; | |
863 | break; | |
864 | case CODING_RESULT_INTERRUPT: | |
865 | Vlast_code_conversion_error = Qinterrupted; | |
866 | break; | |
ebaf11b6 KH |
867 | case CODING_RESULT_INSUFFICIENT_DST: |
868 | /* Don't record this error in Vlast_code_conversion_error | |
869 | because it happens just temporarily and is resolved when the | |
870 | whole conversion is finished. */ | |
871 | break; | |
409ea3a1 AS |
872 | case CODING_RESULT_SUCCESS: |
873 | break; | |
35befdaa KH |
874 | default: |
875 | Vlast_code_conversion_error = intern ("Unknown error"); | |
065e3595 KH |
876 | } |
877 | } | |
878 | ||
5eb05ea3 KH |
879 | /* These wrapper macros are used to preserve validity of pointers into |
880 | buffer text across calls to decode_char, encode_char, etc, which | |
881 | could cause relocation of buffers if it loads a charset map, | |
882 | because loading a charset map allocates large structures. */ | |
883 | ||
df7492f9 KH |
884 | #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \ |
885 | do { \ | |
8f50130c | 886 | ptrdiff_t offset; \ |
5eb05ea3 | 887 | \ |
df7492f9 KH |
888 | charset_map_loaded = 0; \ |
889 | c = DECODE_CHAR (charset, code); \ | |
5eb05ea3 | 890 | if (charset_map_loaded \ |
c1892f11 | 891 | && (offset = coding_change_source (coding))) \ |
df7492f9 | 892 | { \ |
df7492f9 KH |
893 | src += offset; \ |
894 | src_base += offset; \ | |
895 | src_end += offset; \ | |
896 | } \ | |
aa72b389 KH |
897 | } while (0) |
898 | ||
5eb05ea3 KH |
899 | #define CODING_ENCODE_CHAR(coding, dst, dst_end, charset, c, code) \ |
900 | do { \ | |
8f50130c | 901 | ptrdiff_t offset; \ |
5eb05ea3 KH |
902 | \ |
903 | charset_map_loaded = 0; \ | |
904 | code = ENCODE_CHAR (charset, c); \ | |
905 | if (charset_map_loaded \ | |
c1892f11 | 906 | && (offset = coding_change_destination (coding))) \ |
5eb05ea3 KH |
907 | { \ |
908 | dst += offset; \ | |
909 | dst_end += offset; \ | |
910 | } \ | |
911 | } while (0) | |
912 | ||
913 | #define CODING_CHAR_CHARSET(coding, dst, dst_end, c, charset_list, code_return, charset) \ | |
914 | do { \ | |
8f50130c | 915 | ptrdiff_t offset; \ |
5eb05ea3 KH |
916 | \ |
917 | charset_map_loaded = 0; \ | |
918 | charset = char_charset (c, charset_list, code_return); \ | |
919 | if (charset_map_loaded \ | |
c1892f11 | 920 | && (offset = coding_change_destination (coding))) \ |
5eb05ea3 KH |
921 | { \ |
922 | dst += offset; \ | |
923 | dst_end += offset; \ | |
924 | } \ | |
925 | } while (0) | |
926 | ||
927 | #define CODING_CHAR_CHARSET_P(coding, dst, dst_end, c, charset, result) \ | |
928 | do { \ | |
8f50130c | 929 | ptrdiff_t offset; \ |
5eb05ea3 KH |
930 | \ |
931 | charset_map_loaded = 0; \ | |
932 | result = CHAR_CHARSET_P (c, charset); \ | |
933 | if (charset_map_loaded \ | |
c1892f11 | 934 | && (offset = coding_change_destination (coding))) \ |
5eb05ea3 KH |
935 | { \ |
936 | dst += offset; \ | |
937 | dst_end += offset; \ | |
938 | } \ | |
939 | } while (0) | |
940 | ||
aa72b389 | 941 | |
119852e7 KH |
942 | /* If there are at least BYTES length of room at dst, allocate memory |
943 | for coding->destination and update dst and dst_end. We don't have | |
944 | to take care of coding->source which will be relocated. It is | |
945 | handled by calling coding_set_source in encode_coding. */ | |
946 | ||
df7492f9 KH |
947 | #define ASSURE_DESTINATION(bytes) \ |
948 | do { \ | |
949 | if (dst + (bytes) >= dst_end) \ | |
950 | { \ | |
d311d28c | 951 | ptrdiff_t more_bytes = charbuf_end - charbuf + (bytes); \ |
df7492f9 KH |
952 | \ |
953 | dst = alloc_destination (coding, more_bytes, dst); \ | |
954 | dst_end = coding->destination + coding->dst_bytes; \ | |
955 | } \ | |
956 | } while (0) | |
aa72b389 | 957 | |
aa72b389 | 958 | |
db274c7a | 959 | /* Store multibyte form of the character C in P, and advance P to the |
eedec3ee EZ |
960 | end of the multibyte form. This used to be like CHAR_STRING_ADVANCE |
961 | without ever calling MAYBE_UNIFY_CHAR, but nowadays we don't call | |
962 | MAYBE_UNIFY_CHAR in CHAR_STRING_ADVANCE. */ | |
db274c7a | 963 | |
eedec3ee | 964 | #define CHAR_STRING_ADVANCE_NO_UNIFY(c, p) CHAR_STRING_ADVANCE(c, p) |
db274c7a KH |
965 | |
966 | /* Return the character code of character whose multibyte form is at | |
eedec3ee EZ |
967 | P, and advance P to the end of the multibyte form. This used to be |
968 | like STRING_CHAR_ADVANCE without ever calling MAYBE_UNIFY_CHAR, but | |
969 | nowadays STRING_CHAR_ADVANCE doesn't call MAYBE_UNIFY_CHAR. */ | |
db274c7a | 970 | |
eedec3ee | 971 | #define STRING_CHAR_ADVANCE_NO_UNIFY(p) STRING_CHAR_ADVANCE(p) |
aa72b389 | 972 | |
c1892f11 | 973 | /* Set coding->source from coding->src_object. */ |
5eb05ea3 | 974 | |
c1892f11 | 975 | static void |
971de7fb | 976 | coding_set_source (struct coding_system *coding) |
aa72b389 | 977 | { |
df7492f9 KH |
978 | if (BUFFERP (coding->src_object)) |
979 | { | |
2cb26057 | 980 | struct buffer *buf = XBUFFER (coding->src_object); |
aa72b389 | 981 | |
df7492f9 | 982 | if (coding->src_pos < 0) |
2cb26057 | 983 | coding->source = BUF_GAP_END_ADDR (buf) + coding->src_pos_byte; |
df7492f9 | 984 | else |
2cb26057 | 985 | coding->source = BUF_BYTE_ADDRESS (buf, coding->src_pos_byte); |
aa72b389 | 986 | } |
df7492f9 | 987 | else if (STRINGP (coding->src_object)) |
aa72b389 | 988 | { |
8f924df7 | 989 | coding->source = SDATA (coding->src_object) + coding->src_pos_byte; |
aa72b389 | 990 | } |
df7492f9 | 991 | else |
f38b440c PE |
992 | { |
993 | /* Otherwise, the source is C string and is never relocated | |
994 | automatically. Thus we don't have to update anything. */ | |
995 | } | |
df7492f9 | 996 | } |
aa72b389 | 997 | |
5eb05ea3 | 998 | |
c1892f11 PE |
999 | /* Set coding->source from coding->src_object, and return how many |
1000 | bytes coding->source was changed. */ | |
5eb05ea3 | 1001 | |
8f50130c | 1002 | static ptrdiff_t |
c1892f11 | 1003 | coding_change_source (struct coding_system *coding) |
df7492f9 | 1004 | { |
c1892f11 PE |
1005 | const unsigned char *orig = coding->source; |
1006 | coding_set_source (coding); | |
1007 | return coding->source - orig; | |
1008 | } | |
1009 | ||
5eb05ea3 | 1010 | |
c1892f11 PE |
1011 | /* Set coding->destination from coding->dst_object. */ |
1012 | ||
1013 | static void | |
1014 | coding_set_destination (struct coding_system *coding) | |
1015 | { | |
df7492f9 | 1016 | if (BUFFERP (coding->dst_object)) |
aa72b389 | 1017 | { |
a0241d01 | 1018 | if (BUFFERP (coding->src_object) && coding->src_pos < 0) |
aa72b389 | 1019 | { |
13818c30 | 1020 | coding->destination = BEG_ADDR + coding->dst_pos_byte - BEG_BYTE; |
28f67a95 KH |
1021 | coding->dst_bytes = (GAP_END_ADDR |
1022 | - (coding->src_bytes - coding->consumed) | |
1023 | - coding->destination); | |
aa72b389 | 1024 | } |
df7492f9 | 1025 | else |
28f67a95 KH |
1026 | { |
1027 | /* We are sure that coding->dst_pos_byte is before the gap | |
1028 | of the buffer. */ | |
1029 | coding->destination = (BUF_BEG_ADDR (XBUFFER (coding->dst_object)) | |
13818c30 | 1030 | + coding->dst_pos_byte - BEG_BYTE); |
28f67a95 KH |
1031 | coding->dst_bytes = (BUF_GAP_END_ADDR (XBUFFER (coding->dst_object)) |
1032 | - coding->destination); | |
1033 | } | |
df7492f9 KH |
1034 | } |
1035 | else | |
f38b440c PE |
1036 | { |
1037 | /* Otherwise, the destination is C string and is never relocated | |
1038 | automatically. Thus we don't have to update anything. */ | |
1039 | } | |
c1892f11 PE |
1040 | } |
1041 | ||
1042 | ||
1043 | /* Set coding->destination from coding->dst_object, and return how | |
1044 | many bytes coding->destination was changed. */ | |
1045 | ||
1046 | static ptrdiff_t | |
1047 | coding_change_destination (struct coding_system *coding) | |
1048 | { | |
1049 | const unsigned char *orig = coding->destination; | |
1050 | coding_set_destination (coding); | |
5eb05ea3 | 1051 | return coding->destination - orig; |
df7492f9 KH |
1052 | } |
1053 | ||
1054 | ||
1055 | static void | |
d311d28c | 1056 | coding_alloc_by_realloc (struct coding_system *coding, ptrdiff_t bytes) |
df7492f9 | 1057 | { |
c9d624c6 | 1058 | if (STRING_BYTES_BOUND - coding->dst_bytes < bytes) |
d1f3d2af | 1059 | string_overflow (); |
38182d90 PE |
1060 | coding->destination = xrealloc (coding->destination, |
1061 | coding->dst_bytes + bytes); | |
df7492f9 KH |
1062 | coding->dst_bytes += bytes; |
1063 | } | |
1064 | ||
1065 | static void | |
cf84bb53 | 1066 | coding_alloc_by_making_gap (struct coding_system *coding, |
d311d28c | 1067 | ptrdiff_t gap_head_used, ptrdiff_t bytes) |
df7492f9 | 1068 | { |
db274c7a | 1069 | if (EQ (coding->src_object, coding->dst_object)) |
df7492f9 | 1070 | { |
db274c7a KH |
1071 | /* The gap may contain the produced data at the head and not-yet |
1072 | consumed data at the tail. To preserve those data, we at | |
1073 | first make the gap size to zero, then increase the gap | |
1074 | size. */ | |
d311d28c | 1075 | ptrdiff_t add = GAP_SIZE; |
db274c7a KH |
1076 | |
1077 | GPT += gap_head_used, GPT_BYTE += gap_head_used; | |
1078 | GAP_SIZE = 0; ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add; | |
df7492f9 KH |
1079 | make_gap (bytes); |
1080 | GAP_SIZE += add; ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add; | |
db274c7a | 1081 | GPT -= gap_head_used, GPT_BYTE -= gap_head_used; |
df7492f9 | 1082 | } |
730fff51 | 1083 | else |
eefd7278 | 1084 | make_gap_1 (XBUFFER (coding->dst_object), bytes); |
df7492f9 | 1085 | } |
8f924df7 | 1086 | |
df7492f9 KH |
1087 | |
1088 | static unsigned char * | |
d311d28c | 1089 | alloc_destination (struct coding_system *coding, ptrdiff_t nbytes, |
cf84bb53 | 1090 | unsigned char *dst) |
df7492f9 | 1091 | { |
d311d28c | 1092 | ptrdiff_t offset = dst - coding->destination; |
df7492f9 KH |
1093 | |
1094 | if (BUFFERP (coding->dst_object)) | |
db274c7a KH |
1095 | { |
1096 | struct buffer *buf = XBUFFER (coding->dst_object); | |
1097 | ||
1098 | coding_alloc_by_making_gap (coding, dst - BUF_GPT_ADDR (buf), nbytes); | |
1099 | } | |
aa72b389 | 1100 | else |
df7492f9 | 1101 | coding_alloc_by_realloc (coding, nbytes); |
df7492f9 KH |
1102 | coding_set_destination (coding); |
1103 | dst = coding->destination + offset; | |
1104 | return dst; | |
1105 | } | |
aa72b389 | 1106 | |
ff0dacd7 KH |
1107 | /** Macros for annotations. */ |
1108 | ||
ff0dacd7 KH |
1109 | /* An annotation data is stored in the array coding->charbuf in this |
1110 | format: | |
69a80ea3 | 1111 | [ -LENGTH ANNOTATION_MASK NCHARS ... ] |
ff0dacd7 KH |
1112 | LENGTH is the number of elements in the annotation. |
1113 | ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK. | |
69a80ea3 | 1114 | NCHARS is the number of characters in the text annotated. |
ff0dacd7 KH |
1115 | |
1116 | The format of the following elements depend on ANNOTATION_MASK. | |
1117 | ||
1118 | In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements | |
1119 | follows: | |
e951386e KH |
1120 | ... NBYTES METHOD [ COMPOSITION-COMPONENTS ... ] |
1121 | ||
1122 | NBYTES is the number of bytes specified in the header part of | |
1123 | old-style emacs-mule encoding, or 0 for the other kind of | |
1124 | composition. | |
1125 | ||
ff0dacd7 | 1126 | METHOD is one of enum composition_method. |
e951386e | 1127 | |
ad1746f5 | 1128 | Optional COMPOSITION-COMPONENTS are characters and composition |
ff0dacd7 KH |
1129 | rules. |
1130 | ||
1131 | In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID | |
e951386e KH |
1132 | follows. |
1133 | ||
1134 | If ANNOTATION_MASK is 0, this annotation is just a space holder to | |
1135 | recover from an invalid annotation, and should be skipped by | |
1136 | produce_annotation. */ | |
1137 | ||
1138 | /* Maximum length of the header of annotation data. */ | |
1139 | #define MAX_ANNOTATION_LENGTH 5 | |
ff0dacd7 | 1140 | |
69a80ea3 | 1141 | #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \ |
ff0dacd7 KH |
1142 | do { \ |
1143 | *(buf)++ = -(len); \ | |
1144 | *(buf)++ = (mask); \ | |
69a80ea3 | 1145 | *(buf)++ = (nchars); \ |
ff0dacd7 KH |
1146 | coding->annotated = 1; \ |
1147 | } while (0); | |
1148 | ||
e951386e | 1149 | #define ADD_COMPOSITION_DATA(buf, nchars, nbytes, method) \ |
69a80ea3 | 1150 | do { \ |
e951386e KH |
1151 | ADD_ANNOTATION_DATA (buf, 5, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \ |
1152 | *buf++ = nbytes; \ | |
69a80ea3 | 1153 | *buf++ = method; \ |
ff0dacd7 KH |
1154 | } while (0) |
1155 | ||
1156 | ||
69a80ea3 KH |
1157 | #define ADD_CHARSET_DATA(buf, nchars, id) \ |
1158 | do { \ | |
1159 | ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \ | |
1160 | *buf++ = id; \ | |
ff0dacd7 KH |
1161 | } while (0) |
1162 | ||
e6d2f155 KH |
1163 | |
1164 | /* Bitmasks for coding->eol_seen. */ | |
1165 | ||
1166 | #define EOL_SEEN_NONE 0 | |
1167 | #define EOL_SEEN_LF 1 | |
1168 | #define EOL_SEEN_CR 2 | |
1169 | #define EOL_SEEN_CRLF 4 | |
1170 | ||
df7492f9 KH |
1171 | \f |
1172 | /*** 2. Emacs' internal format (emacs-utf-8) ***/ | |
1173 | ||
1174 | ||
1175 | ||
1176 | \f | |
1177 | /*** 3. UTF-8 ***/ | |
1178 | ||
1179 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
f10fe38f | 1180 | Return true if a text is encoded in UTF-8. */ |
df7492f9 KH |
1181 | |
1182 | #define UTF_8_1_OCTET_P(c) ((c) < 0x80) | |
1183 | #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80) | |
1184 | #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0) | |
1185 | #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0) | |
1186 | #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0) | |
1187 | #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8) | |
1188 | ||
a470d443 KH |
1189 | #define UTF_8_BOM_1 0xEF |
1190 | #define UTF_8_BOM_2 0xBB | |
1191 | #define UTF_8_BOM_3 0xBF | |
1192 | ||
e6d2f155 KH |
1193 | /* Unlike the other detect_coding_XXX, this function counts number of |
1194 | characters and check EOL format. */ | |
1195 | ||
f10fe38f | 1196 | static bool |
cf84bb53 JB |
1197 | detect_coding_utf_8 (struct coding_system *coding, |
1198 | struct coding_detection_info *detect_info) | |
df7492f9 | 1199 | { |
065e3595 | 1200 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 1201 | const unsigned char *src_end = coding->source + coding->src_bytes; |
f10fe38f | 1202 | bool multibytep = coding->src_multibyte; |
d311d28c | 1203 | ptrdiff_t consumed_chars = 0; |
f10fe38f | 1204 | bool bom_found = 0; |
e6d2f155 KH |
1205 | int nchars = coding->head_ascii; |
1206 | int eol_seen = coding->eol_seen; | |
df7492f9 | 1207 | |
ff0dacd7 | 1208 | detect_info->checked |= CATEGORY_MASK_UTF_8; |
df7492f9 | 1209 | /* A coding system of this category is always ASCII compatible. */ |
e6d2f155 KH |
1210 | src += nchars; |
1211 | ||
1212 | if (src == coding->source /* BOM should be at the head. */ | |
1213 | && src + 3 < src_end /* BOM is 3-byte long. */ | |
1214 | && src[0] == UTF_8_BOM_1 | |
1215 | && src[1] == UTF_8_BOM_2 | |
1216 | && src[2] == UTF_8_BOM_3) | |
1217 | { | |
1218 | bom_found = 1; | |
1219 | src += 3; | |
1220 | nchars++; | |
1221 | } | |
df7492f9 KH |
1222 | |
1223 | while (1) | |
aa72b389 | 1224 | { |
df7492f9 | 1225 | int c, c1, c2, c3, c4; |
aa72b389 | 1226 | |
065e3595 | 1227 | src_base = src; |
df7492f9 | 1228 | ONE_MORE_BYTE (c); |
065e3595 | 1229 | if (c < 0 || UTF_8_1_OCTET_P (c)) |
e6d2f155 KH |
1230 | { |
1231 | nchars++; | |
1232 | if (c == '\r') | |
1233 | { | |
1234 | if (src < src_end && *src == '\n') | |
1235 | { | |
1236 | eol_seen |= EOL_SEEN_CRLF; | |
1237 | src++; | |
1238 | nchars++; | |
1239 | } | |
1240 | else | |
1241 | eol_seen |= EOL_SEEN_CR; | |
1242 | } | |
1243 | else if (c == '\n') | |
1244 | eol_seen |= EOL_SEEN_LF; | |
1245 | continue; | |
1246 | } | |
df7492f9 | 1247 | ONE_MORE_BYTE (c1); |
065e3595 | 1248 | if (c1 < 0 || ! UTF_8_EXTRA_OCTET_P (c1)) |
df7492f9 KH |
1249 | break; |
1250 | if (UTF_8_2_OCTET_LEADING_P (c)) | |
aa72b389 | 1251 | { |
e6d2f155 | 1252 | nchars++; |
df7492f9 | 1253 | continue; |
aa72b389 | 1254 | } |
df7492f9 | 1255 | ONE_MORE_BYTE (c2); |
065e3595 | 1256 | if (c2 < 0 || ! UTF_8_EXTRA_OCTET_P (c2)) |
df7492f9 KH |
1257 | break; |
1258 | if (UTF_8_3_OCTET_LEADING_P (c)) | |
aa72b389 | 1259 | { |
e6d2f155 | 1260 | nchars++; |
df7492f9 | 1261 | continue; |
aa72b389 | 1262 | } |
df7492f9 | 1263 | ONE_MORE_BYTE (c3); |
065e3595 | 1264 | if (c3 < 0 || ! UTF_8_EXTRA_OCTET_P (c3)) |
df7492f9 KH |
1265 | break; |
1266 | if (UTF_8_4_OCTET_LEADING_P (c)) | |
aa72b389 | 1267 | { |
e6d2f155 | 1268 | nchars++; |
df7492f9 KH |
1269 | continue; |
1270 | } | |
1271 | ONE_MORE_BYTE (c4); | |
065e3595 | 1272 | if (c4 < 0 || ! UTF_8_EXTRA_OCTET_P (c4)) |
df7492f9 KH |
1273 | break; |
1274 | if (UTF_8_5_OCTET_LEADING_P (c)) | |
1275 | { | |
e6d2f155 | 1276 | nchars++; |
df7492f9 KH |
1277 | continue; |
1278 | } | |
1279 | break; | |
aa72b389 | 1280 | } |
ff0dacd7 | 1281 | detect_info->rejected |= CATEGORY_MASK_UTF_8; |
df7492f9 | 1282 | return 0; |
aa72b389 | 1283 | |
df7492f9 | 1284 | no_more_source: |
065e3595 | 1285 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) |
aa72b389 | 1286 | { |
ff0dacd7 | 1287 | detect_info->rejected |= CATEGORY_MASK_UTF_8; |
89528eb3 | 1288 | return 0; |
aa72b389 | 1289 | } |
a470d443 KH |
1290 | if (bom_found) |
1291 | { | |
1292 | /* The first character 0xFFFE doesn't necessarily mean a BOM. */ | |
e6d2f155 | 1293 | detect_info->found |= CATEGORY_MASK_UTF_8_AUTO | CATEGORY_MASK_UTF_8_SIG | CATEGORY_MASK_UTF_8_NOSIG; |
a470d443 KH |
1294 | } |
1295 | else | |
1296 | { | |
1297 | detect_info->rejected |= CATEGORY_MASK_UTF_8_SIG; | |
e6d2f155 KH |
1298 | if (nchars < src_end - coding->source) |
1299 | /* The found characters are less than source bytes, which | |
1300 | means that we found a valid non-ASCII characters. */ | |
1301 | detect_info->found |= CATEGORY_MASK_UTF_8_AUTO | CATEGORY_MASK_UTF_8_NOSIG; | |
a470d443 | 1302 | } |
e6d2f155 | 1303 | coding->detected_utf8_chars = nchars; |
ff0dacd7 | 1304 | return 1; |
aa72b389 KH |
1305 | } |
1306 | ||
4ed46869 | 1307 | |
b73bfc1c | 1308 | static void |
971de7fb | 1309 | decode_coding_utf_8 (struct coding_system *coding) |
b73bfc1c | 1310 | { |
8f924df7 KH |
1311 | const unsigned char *src = coding->source + coding->consumed; |
1312 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
1313 | const unsigned char *src_base; | |
69a80ea3 KH |
1314 | int *charbuf = coding->charbuf + coding->charbuf_used; |
1315 | int *charbuf_end = coding->charbuf + coding->charbuf_size; | |
d311d28c | 1316 | ptrdiff_t consumed_chars = 0, consumed_chars_base = 0; |
f10fe38f | 1317 | bool multibytep = coding->src_multibyte; |
a470d443 | 1318 | enum utf_bom_type bom = CODING_UTF_8_BOM (coding); |
f10fe38f PE |
1319 | bool eol_dos |
1320 | = !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); | |
119852e7 | 1321 | int byte_after_cr = -1; |
4ed46869 | 1322 | |
a470d443 KH |
1323 | if (bom != utf_without_bom) |
1324 | { | |
1325 | int c1, c2, c3; | |
1326 | ||
1327 | src_base = src; | |
1328 | ONE_MORE_BYTE (c1); | |
1329 | if (! UTF_8_3_OCTET_LEADING_P (c1)) | |
1330 | src = src_base; | |
1331 | else | |
1332 | { | |
159bd5a2 | 1333 | ONE_MORE_BYTE (c2); |
a470d443 KH |
1334 | if (! UTF_8_EXTRA_OCTET_P (c2)) |
1335 | src = src_base; | |
1336 | else | |
1337 | { | |
159bd5a2 | 1338 | ONE_MORE_BYTE (c3); |
a470d443 KH |
1339 | if (! UTF_8_EXTRA_OCTET_P (c3)) |
1340 | src = src_base; | |
1341 | else | |
1342 | { | |
1343 | if ((c1 != UTF_8_BOM_1) | |
1344 | || (c2 != UTF_8_BOM_2) || (c3 != UTF_8_BOM_3)) | |
1345 | src = src_base; | |
1346 | else | |
1347 | CODING_UTF_8_BOM (coding) = utf_without_bom; | |
1348 | } | |
1349 | } | |
1350 | } | |
1351 | } | |
1352 | CODING_UTF_8_BOM (coding) = utf_without_bom; | |
1353 | ||
df7492f9 | 1354 | while (1) |
b73bfc1c | 1355 | { |
df7492f9 | 1356 | int c, c1, c2, c3, c4, c5; |
ec6d2bb8 | 1357 | |
df7492f9 KH |
1358 | src_base = src; |
1359 | consumed_chars_base = consumed_chars; | |
4af310db | 1360 | |
df7492f9 | 1361 | if (charbuf >= charbuf_end) |
b71f6f73 KH |
1362 | { |
1363 | if (byte_after_cr >= 0) | |
1364 | src_base--; | |
1365 | break; | |
1366 | } | |
df7492f9 | 1367 | |
a1aeeffe RS |
1368 | /* In the simple case, rapidly handle ordinary characters */ |
1369 | if (multibytep && ! eol_dos | |
1370 | && charbuf < charbuf_end - 6 && src < src_end - 6) | |
1371 | { | |
1372 | while (charbuf < charbuf_end - 6 && src < src_end - 6) | |
1373 | { | |
1374 | c1 = *src; | |
1375 | if (c1 & 0x80) | |
1376 | break; | |
1377 | src++; | |
1378 | consumed_chars++; | |
1379 | *charbuf++ = c1; | |
1380 | ||
1381 | c1 = *src; | |
1382 | if (c1 & 0x80) | |
1383 | break; | |
1384 | src++; | |
1385 | consumed_chars++; | |
1386 | *charbuf++ = c1; | |
1387 | ||
1388 | c1 = *src; | |
1389 | if (c1 & 0x80) | |
1390 | break; | |
1391 | src++; | |
1392 | consumed_chars++; | |
1393 | *charbuf++ = c1; | |
1394 | ||
1395 | c1 = *src; | |
1396 | if (c1 & 0x80) | |
1397 | break; | |
1398 | src++; | |
1399 | consumed_chars++; | |
1400 | *charbuf++ = c1; | |
1401 | } | |
1402 | /* If we handled at least one character, restart the main loop. */ | |
1403 | if (src != src_base) | |
1404 | continue; | |
1405 | } | |
1406 | ||
119852e7 KH |
1407 | if (byte_after_cr >= 0) |
1408 | c1 = byte_after_cr, byte_after_cr = -1; | |
1409 | else | |
1410 | ONE_MORE_BYTE (c1); | |
065e3595 KH |
1411 | if (c1 < 0) |
1412 | { | |
1413 | c = - c1; | |
1414 | } | |
1a4990fb | 1415 | else if (UTF_8_1_OCTET_P (c1)) |
df7492f9 | 1416 | { |
2735d060 | 1417 | if (eol_dos && c1 == '\r') |
119852e7 | 1418 | ONE_MORE_BYTE (byte_after_cr); |
df7492f9 | 1419 | c = c1; |
4af310db | 1420 | } |
df7492f9 | 1421 | else |
4af310db | 1422 | { |
df7492f9 | 1423 | ONE_MORE_BYTE (c2); |
065e3595 | 1424 | if (c2 < 0 || ! UTF_8_EXTRA_OCTET_P (c2)) |
df7492f9 KH |
1425 | goto invalid_code; |
1426 | if (UTF_8_2_OCTET_LEADING_P (c1)) | |
4af310db | 1427 | { |
b0edb2c5 DL |
1428 | c = ((c1 & 0x1F) << 6) | (c2 & 0x3F); |
1429 | /* Reject overlong sequences here and below. Encoders | |
1430 | producing them are incorrect, they can be misleading, | |
1431 | and they mess up read/write invariance. */ | |
1432 | if (c < 128) | |
1433 | goto invalid_code; | |
4af310db | 1434 | } |
df7492f9 | 1435 | else |
aa72b389 | 1436 | { |
df7492f9 | 1437 | ONE_MORE_BYTE (c3); |
065e3595 | 1438 | if (c3 < 0 || ! UTF_8_EXTRA_OCTET_P (c3)) |
df7492f9 KH |
1439 | goto invalid_code; |
1440 | if (UTF_8_3_OCTET_LEADING_P (c1)) | |
b0edb2c5 DL |
1441 | { |
1442 | c = (((c1 & 0xF) << 12) | |
1443 | | ((c2 & 0x3F) << 6) | (c3 & 0x3F)); | |
72fe1301 DL |
1444 | if (c < 0x800 |
1445 | || (c >= 0xd800 && c < 0xe000)) /* surrogates (invalid) */ | |
b0edb2c5 DL |
1446 | goto invalid_code; |
1447 | } | |
df7492f9 KH |
1448 | else |
1449 | { | |
1450 | ONE_MORE_BYTE (c4); | |
065e3595 | 1451 | if (c4 < 0 || ! UTF_8_EXTRA_OCTET_P (c4)) |
df7492f9 KH |
1452 | goto invalid_code; |
1453 | if (UTF_8_4_OCTET_LEADING_P (c1)) | |
b0edb2c5 | 1454 | { |
df7492f9 KH |
1455 | c = (((c1 & 0x7) << 18) | ((c2 & 0x3F) << 12) |
1456 | | ((c3 & 0x3F) << 6) | (c4 & 0x3F)); | |
b0edb2c5 DL |
1457 | if (c < 0x10000) |
1458 | goto invalid_code; | |
1459 | } | |
df7492f9 KH |
1460 | else |
1461 | { | |
1462 | ONE_MORE_BYTE (c5); | |
065e3595 | 1463 | if (c5 < 0 || ! UTF_8_EXTRA_OCTET_P (c5)) |
df7492f9 KH |
1464 | goto invalid_code; |
1465 | if (UTF_8_5_OCTET_LEADING_P (c1)) | |
1466 | { | |
1467 | c = (((c1 & 0x3) << 24) | ((c2 & 0x3F) << 18) | |
1468 | | ((c3 & 0x3F) << 12) | ((c4 & 0x3F) << 6) | |
1469 | | (c5 & 0x3F)); | |
b0edb2c5 | 1470 | if ((c > MAX_CHAR) || (c < 0x200000)) |
df7492f9 KH |
1471 | goto invalid_code; |
1472 | } | |
1473 | else | |
1474 | goto invalid_code; | |
1475 | } | |
1476 | } | |
aa72b389 | 1477 | } |
b73bfc1c | 1478 | } |
df7492f9 KH |
1479 | |
1480 | *charbuf++ = c; | |
1481 | continue; | |
1482 | ||
1483 | invalid_code: | |
1484 | src = src_base; | |
1485 | consumed_chars = consumed_chars_base; | |
1486 | ONE_MORE_BYTE (c); | |
1487 | *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); | |
1488 | coding->errors++; | |
aa72b389 KH |
1489 | } |
1490 | ||
df7492f9 KH |
1491 | no_more_source: |
1492 | coding->consumed_char += consumed_chars_base; | |
1493 | coding->consumed = src_base - coding->source; | |
1494 | coding->charbuf_used = charbuf - coding->charbuf; | |
1495 | } | |
1496 | ||
1497 | ||
f10fe38f | 1498 | static bool |
971de7fb | 1499 | encode_coding_utf_8 (struct coding_system *coding) |
df7492f9 | 1500 | { |
f10fe38f | 1501 | bool multibytep = coding->dst_multibyte; |
df7492f9 KH |
1502 | int *charbuf = coding->charbuf; |
1503 | int *charbuf_end = charbuf + coding->charbuf_used; | |
1504 | unsigned char *dst = coding->destination + coding->produced; | |
1505 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
d311d28c | 1506 | ptrdiff_t produced_chars = 0; |
df7492f9 KH |
1507 | int c; |
1508 | ||
a470d443 KH |
1509 | if (CODING_UTF_8_BOM (coding) == utf_with_bom) |
1510 | { | |
1511 | ASSURE_DESTINATION (3); | |
1512 | EMIT_THREE_BYTES (UTF_8_BOM_1, UTF_8_BOM_2, UTF_8_BOM_3); | |
1513 | CODING_UTF_8_BOM (coding) = utf_without_bom; | |
1514 | } | |
1515 | ||
df7492f9 | 1516 | if (multibytep) |
aa72b389 | 1517 | { |
df7492f9 KH |
1518 | int safe_room = MAX_MULTIBYTE_LENGTH * 2; |
1519 | ||
1520 | while (charbuf < charbuf_end) | |
b73bfc1c | 1521 | { |
df7492f9 | 1522 | unsigned char str[MAX_MULTIBYTE_LENGTH], *p, *pend = str; |
8f924df7 | 1523 | |
df7492f9 KH |
1524 | ASSURE_DESTINATION (safe_room); |
1525 | c = *charbuf++; | |
28f67a95 KH |
1526 | if (CHAR_BYTE8_P (c)) |
1527 | { | |
1528 | c = CHAR_TO_BYTE8 (c); | |
1529 | EMIT_ONE_BYTE (c); | |
1530 | } | |
1531 | else | |
1532 | { | |
db274c7a | 1533 | CHAR_STRING_ADVANCE_NO_UNIFY (c, pend); |
28f67a95 KH |
1534 | for (p = str; p < pend; p++) |
1535 | EMIT_ONE_BYTE (*p); | |
1536 | } | |
b73bfc1c | 1537 | } |
aa72b389 | 1538 | } |
df7492f9 KH |
1539 | else |
1540 | { | |
1541 | int safe_room = MAX_MULTIBYTE_LENGTH; | |
1542 | ||
1543 | while (charbuf < charbuf_end) | |
b73bfc1c | 1544 | { |
df7492f9 KH |
1545 | ASSURE_DESTINATION (safe_room); |
1546 | c = *charbuf++; | |
f03caae0 KH |
1547 | if (CHAR_BYTE8_P (c)) |
1548 | *dst++ = CHAR_TO_BYTE8 (c); | |
1549 | else | |
db274c7a | 1550 | CHAR_STRING_ADVANCE_NO_UNIFY (c, dst); |
df7492f9 | 1551 | produced_chars++; |
4ed46869 KH |
1552 | } |
1553 | } | |
065e3595 | 1554 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
1555 | coding->produced_char += produced_chars; |
1556 | coding->produced = dst - coding->destination; | |
1557 | return 0; | |
4ed46869 KH |
1558 | } |
1559 | ||
b73bfc1c | 1560 | |
df7492f9 | 1561 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". |
f10fe38f | 1562 | Return true if a text is encoded in one of UTF-16 based coding systems. */ |
aa72b389 | 1563 | |
df7492f9 KH |
1564 | #define UTF_16_HIGH_SURROGATE_P(val) \ |
1565 | (((val) & 0xFC00) == 0xD800) | |
1566 | ||
1567 | #define UTF_16_LOW_SURROGATE_P(val) \ | |
1568 | (((val) & 0xFC00) == 0xDC00) | |
93dec019 | 1569 | |
aa72b389 | 1570 | |
f10fe38f | 1571 | static bool |
cf84bb53 JB |
1572 | detect_coding_utf_16 (struct coding_system *coding, |
1573 | struct coding_detection_info *detect_info) | |
aa72b389 | 1574 | { |
ef1b0ba7 | 1575 | const unsigned char *src = coding->source; |
8f924df7 | 1576 | const unsigned char *src_end = coding->source + coding->src_bytes; |
f10fe38f | 1577 | bool multibytep = coding->src_multibyte; |
df7492f9 | 1578 | int c1, c2; |
aa72b389 | 1579 | |
ff0dacd7 | 1580 | detect_info->checked |= CATEGORY_MASK_UTF_16; |
ff0dacd7 | 1581 | if (coding->mode & CODING_MODE_LAST_BLOCK |
24a73b0a | 1582 | && (coding->src_chars & 1)) |
ff0dacd7 KH |
1583 | { |
1584 | detect_info->rejected |= CATEGORY_MASK_UTF_16; | |
1585 | return 0; | |
1586 | } | |
24a73b0a | 1587 | |
f56a4450 | 1588 | TWO_MORE_BYTES (c1, c2); |
df7492f9 | 1589 | if ((c1 == 0xFF) && (c2 == 0xFE)) |
aa72b389 | 1590 | { |
b49a1807 KH |
1591 | detect_info->found |= (CATEGORY_MASK_UTF_16_LE |
1592 | | CATEGORY_MASK_UTF_16_AUTO); | |
24a73b0a KH |
1593 | detect_info->rejected |= (CATEGORY_MASK_UTF_16_BE |
1594 | | CATEGORY_MASK_UTF_16_BE_NOSIG | |
1595 | | CATEGORY_MASK_UTF_16_LE_NOSIG); | |
aa72b389 | 1596 | } |
df7492f9 | 1597 | else if ((c1 == 0xFE) && (c2 == 0xFF)) |
ff0dacd7 | 1598 | { |
b49a1807 KH |
1599 | detect_info->found |= (CATEGORY_MASK_UTF_16_BE |
1600 | | CATEGORY_MASK_UTF_16_AUTO); | |
24a73b0a KH |
1601 | detect_info->rejected |= (CATEGORY_MASK_UTF_16_LE |
1602 | | CATEGORY_MASK_UTF_16_BE_NOSIG | |
1603 | | CATEGORY_MASK_UTF_16_LE_NOSIG); | |
1604 | } | |
220eeac9 | 1605 | else if (c2 < 0) |
f56a4450 KH |
1606 | { |
1607 | detect_info->rejected |= CATEGORY_MASK_UTF_16; | |
1608 | return 0; | |
1609 | } | |
2f3cbb32 | 1610 | else |
24a73b0a | 1611 | { |
2f3cbb32 KH |
1612 | /* We check the dispersion of Eth and Oth bytes where E is even and |
1613 | O is odd. If both are high, we assume binary data.*/ | |
1614 | unsigned char e[256], o[256]; | |
1615 | unsigned e_num = 1, o_num = 1; | |
1616 | ||
1617 | memset (e, 0, 256); | |
1618 | memset (o, 0, 256); | |
1619 | e[c1] = 1; | |
1620 | o[c2] = 1; | |
1621 | ||
cc13543e KH |
1622 | detect_info->rejected |= (CATEGORY_MASK_UTF_16_AUTO |
1623 | |CATEGORY_MASK_UTF_16_BE | |
1624 | | CATEGORY_MASK_UTF_16_LE); | |
2f3cbb32 | 1625 | |
7f1faf1c KH |
1626 | while ((detect_info->rejected & CATEGORY_MASK_UTF_16) |
1627 | != CATEGORY_MASK_UTF_16) | |
2f3cbb32 | 1628 | { |
f56a4450 | 1629 | TWO_MORE_BYTES (c1, c2); |
220eeac9 | 1630 | if (c2 < 0) |
f56a4450 | 1631 | break; |
2f3cbb32 KH |
1632 | if (! e[c1]) |
1633 | { | |
1634 | e[c1] = 1; | |
1635 | e_num++; | |
cc13543e KH |
1636 | if (e_num >= 128) |
1637 | detect_info->rejected |= CATEGORY_MASK_UTF_16_BE_NOSIG; | |
2f3cbb32 KH |
1638 | } |
1639 | if (! o[c2]) | |
1640 | { | |
977b85f4 | 1641 | o[c2] = 1; |
2f3cbb32 | 1642 | o_num++; |
cc13543e KH |
1643 | if (o_num >= 128) |
1644 | detect_info->rejected |= CATEGORY_MASK_UTF_16_LE_NOSIG; | |
2f3cbb32 KH |
1645 | } |
1646 | } | |
2f3cbb32 | 1647 | return 0; |
ff0dacd7 | 1648 | } |
2f3cbb32 | 1649 | |
df7492f9 | 1650 | no_more_source: |
ff0dacd7 | 1651 | return 1; |
df7492f9 | 1652 | } |
aa72b389 | 1653 | |
df7492f9 | 1654 | static void |
971de7fb | 1655 | decode_coding_utf_16 (struct coding_system *coding) |
df7492f9 | 1656 | { |
8f924df7 KH |
1657 | const unsigned char *src = coding->source + coding->consumed; |
1658 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
1659 | const unsigned char *src_base; | |
69a80ea3 | 1660 | int *charbuf = coding->charbuf + coding->charbuf_used; |
df80c7f0 KH |
1661 | /* We may produces at most 3 chars in one loop. */ |
1662 | int *charbuf_end = coding->charbuf + coding->charbuf_size - 2; | |
d311d28c | 1663 | ptrdiff_t consumed_chars = 0, consumed_chars_base = 0; |
f10fe38f | 1664 | bool multibytep = coding->src_multibyte; |
a470d443 | 1665 | enum utf_bom_type bom = CODING_UTF_16_BOM (coding); |
df7492f9 KH |
1666 | enum utf_16_endian_type endian = CODING_UTF_16_ENDIAN (coding); |
1667 | int surrogate = CODING_UTF_16_SURROGATE (coding); | |
f10fe38f PE |
1668 | bool eol_dos |
1669 | = !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); | |
119852e7 | 1670 | int byte_after_cr1 = -1, byte_after_cr2 = -1; |
df7492f9 | 1671 | |
a470d443 | 1672 | if (bom == utf_with_bom) |
aa72b389 | 1673 | { |
df7492f9 | 1674 | int c, c1, c2; |
4af310db | 1675 | |
aa72b389 | 1676 | src_base = src; |
df7492f9 KH |
1677 | ONE_MORE_BYTE (c1); |
1678 | ONE_MORE_BYTE (c2); | |
e19c3639 | 1679 | c = (c1 << 8) | c2; |
aa72b389 | 1680 | |
b49a1807 KH |
1681 | if (endian == utf_16_big_endian |
1682 | ? c != 0xFEFF : c != 0xFFFE) | |
aa72b389 | 1683 | { |
b49a1807 KH |
1684 | /* The first two bytes are not BOM. Treat them as bytes |
1685 | for a normal character. */ | |
1686 | src = src_base; | |
1687 | coding->errors++; | |
aa72b389 | 1688 | } |
a470d443 | 1689 | CODING_UTF_16_BOM (coding) = utf_without_bom; |
b49a1807 | 1690 | } |
a470d443 | 1691 | else if (bom == utf_detect_bom) |
b49a1807 KH |
1692 | { |
1693 | /* We have already tried to detect BOM and failed in | |
1694 | detect_coding. */ | |
a470d443 | 1695 | CODING_UTF_16_BOM (coding) = utf_without_bom; |
df7492f9 | 1696 | } |
aa72b389 | 1697 | |
df7492f9 KH |
1698 | while (1) |
1699 | { | |
1700 | int c, c1, c2; | |
1701 | ||
1702 | src_base = src; | |
1703 | consumed_chars_base = consumed_chars; | |
1704 | ||
df80c7f0 | 1705 | if (charbuf >= charbuf_end) |
b71f6f73 KH |
1706 | { |
1707 | if (byte_after_cr1 >= 0) | |
1708 | src_base -= 2; | |
1709 | break; | |
1710 | } | |
df7492f9 | 1711 | |
119852e7 KH |
1712 | if (byte_after_cr1 >= 0) |
1713 | c1 = byte_after_cr1, byte_after_cr1 = -1; | |
1714 | else | |
1715 | ONE_MORE_BYTE (c1); | |
065e3595 KH |
1716 | if (c1 < 0) |
1717 | { | |
1718 | *charbuf++ = -c1; | |
1719 | continue; | |
1720 | } | |
119852e7 KH |
1721 | if (byte_after_cr2 >= 0) |
1722 | c2 = byte_after_cr2, byte_after_cr2 = -1; | |
1723 | else | |
1724 | ONE_MORE_BYTE (c2); | |
065e3595 KH |
1725 | if (c2 < 0) |
1726 | { | |
1727 | *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1); | |
1728 | *charbuf++ = -c2; | |
1729 | continue; | |
1730 | } | |
df7492f9 | 1731 | c = (endian == utf_16_big_endian |
e19c3639 | 1732 | ? ((c1 << 8) | c2) : ((c2 << 8) | c1)); |
119852e7 | 1733 | |
df7492f9 | 1734 | if (surrogate) |
fd3ae0b9 | 1735 | { |
df7492f9 | 1736 | if (! UTF_16_LOW_SURROGATE_P (c)) |
fd3ae0b9 | 1737 | { |
df7492f9 KH |
1738 | if (endian == utf_16_big_endian) |
1739 | c1 = surrogate >> 8, c2 = surrogate & 0xFF; | |
1740 | else | |
1741 | c1 = surrogate & 0xFF, c2 = surrogate >> 8; | |
1742 | *charbuf++ = c1; | |
1743 | *charbuf++ = c2; | |
1744 | coding->errors++; | |
1745 | if (UTF_16_HIGH_SURROGATE_P (c)) | |
1746 | CODING_UTF_16_SURROGATE (coding) = surrogate = c; | |
fd3ae0b9 | 1747 | else |
df7492f9 | 1748 | *charbuf++ = c; |
fd3ae0b9 KH |
1749 | } |
1750 | else | |
df7492f9 KH |
1751 | { |
1752 | c = ((surrogate - 0xD800) << 10) | (c - 0xDC00); | |
1753 | CODING_UTF_16_SURROGATE (coding) = surrogate = 0; | |
29f7ffd0 | 1754 | *charbuf++ = 0x10000 + c; |
df7492f9 | 1755 | } |
fd3ae0b9 | 1756 | } |
aa72b389 | 1757 | else |
df7492f9 KH |
1758 | { |
1759 | if (UTF_16_HIGH_SURROGATE_P (c)) | |
1760 | CODING_UTF_16_SURROGATE (coding) = surrogate = c; | |
1761 | else | |
119852e7 | 1762 | { |
2735d060 | 1763 | if (eol_dos && c == '\r') |
119852e7 KH |
1764 | { |
1765 | ONE_MORE_BYTE (byte_after_cr1); | |
1766 | ONE_MORE_BYTE (byte_after_cr2); | |
1767 | } | |
1768 | *charbuf++ = c; | |
1769 | } | |
8f924df7 | 1770 | } |
aa72b389 | 1771 | } |
df7492f9 KH |
1772 | |
1773 | no_more_source: | |
1774 | coding->consumed_char += consumed_chars_base; | |
1775 | coding->consumed = src_base - coding->source; | |
1776 | coding->charbuf_used = charbuf - coding->charbuf; | |
aa72b389 | 1777 | } |
b73bfc1c | 1778 | |
f10fe38f | 1779 | static bool |
971de7fb | 1780 | encode_coding_utf_16 (struct coding_system *coding) |
df7492f9 | 1781 | { |
f10fe38f | 1782 | bool multibytep = coding->dst_multibyte; |
df7492f9 KH |
1783 | int *charbuf = coding->charbuf; |
1784 | int *charbuf_end = charbuf + coding->charbuf_used; | |
1785 | unsigned char *dst = coding->destination + coding->produced; | |
1786 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
1787 | int safe_room = 8; | |
a470d443 | 1788 | enum utf_bom_type bom = CODING_UTF_16_BOM (coding); |
f10fe38f | 1789 | bool big_endian = CODING_UTF_16_ENDIAN (coding) == utf_16_big_endian; |
d311d28c | 1790 | ptrdiff_t produced_chars = 0; |
df7492f9 | 1791 | int c; |
4ed46869 | 1792 | |
a470d443 | 1793 | if (bom != utf_without_bom) |
df7492f9 KH |
1794 | { |
1795 | ASSURE_DESTINATION (safe_room); | |
1796 | if (big_endian) | |
df7492f9 | 1797 | EMIT_TWO_BYTES (0xFE, 0xFF); |
880cf180 KH |
1798 | else |
1799 | EMIT_TWO_BYTES (0xFF, 0xFE); | |
a470d443 | 1800 | CODING_UTF_16_BOM (coding) = utf_without_bom; |
df7492f9 KH |
1801 | } |
1802 | ||
1803 | while (charbuf < charbuf_end) | |
1804 | { | |
1805 | ASSURE_DESTINATION (safe_room); | |
1806 | c = *charbuf++; | |
60afa08d | 1807 | if (c > MAX_UNICODE_CHAR) |
e19c3639 | 1808 | c = coding->default_char; |
df7492f9 KH |
1809 | |
1810 | if (c < 0x10000) | |
1811 | { | |
1812 | if (big_endian) | |
1813 | EMIT_TWO_BYTES (c >> 8, c & 0xFF); | |
1814 | else | |
1815 | EMIT_TWO_BYTES (c & 0xFF, c >> 8); | |
1816 | } | |
1817 | else | |
1818 | { | |
1819 | int c1, c2; | |
1820 | ||
1821 | c -= 0x10000; | |
1822 | c1 = (c >> 10) + 0xD800; | |
1823 | c2 = (c & 0x3FF) + 0xDC00; | |
1824 | if (big_endian) | |
1825 | EMIT_FOUR_BYTES (c1 >> 8, c1 & 0xFF, c2 >> 8, c2 & 0xFF); | |
1826 | else | |
1827 | EMIT_FOUR_BYTES (c1 & 0xFF, c1 >> 8, c2 & 0xFF, c2 >> 8); | |
1828 | } | |
1829 | } | |
065e3595 | 1830 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
1831 | coding->produced = dst - coding->destination; |
1832 | coding->produced_char += produced_chars; | |
1833 | return 0; | |
1834 | } | |
1835 | ||
1836 | \f | |
1837 | /*** 6. Old Emacs' internal format (emacs-mule) ***/ | |
1838 | ||
1839 | /* Emacs' internal format for representation of multiple character | |
1840 | sets is a kind of multi-byte encoding, i.e. characters are | |
1841 | represented by variable-length sequences of one-byte codes. | |
1842 | ||
1843 | ASCII characters and control characters (e.g. `tab', `newline') are | |
1844 | represented by one-byte sequences which are their ASCII codes, in | |
1845 | the range 0x00 through 0x7F. | |
1846 | ||
1847 | 8-bit characters of the range 0x80..0x9F are represented by | |
1848 | two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit | |
1849 | code + 0x20). | |
1850 | ||
1851 | 8-bit characters of the range 0xA0..0xFF are represented by | |
1852 | one-byte sequences which are their 8-bit code. | |
1853 | ||
1854 | The other characters are represented by a sequence of `base | |
1855 | leading-code', optional `extended leading-code', and one or two | |
1856 | `position-code's. The length of the sequence is determined by the | |
1857 | base leading-code. Leading-code takes the range 0x81 through 0x9D, | |
1858 | whereas extended leading-code and position-code take the range 0xA0 | |
1859 | through 0xFF. See `charset.h' for more details about leading-code | |
1860 | and position-code. | |
1861 | ||
1862 | --- CODE RANGE of Emacs' internal format --- | |
1863 | character set range | |
1864 | ------------- ----- | |
1865 | ascii 0x00..0x7F | |
1866 | eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF | |
1867 | eight-bit-graphic 0xA0..0xBF | |
1868 | ELSE 0x81..0x9D + [0xA0..0xFF]+ | |
1869 | --------------------------------------------- | |
1870 | ||
1871 | As this is the internal character representation, the format is | |
1872 | usually not used externally (i.e. in a file or in a data sent to a | |
1873 | process). But, it is possible to have a text externally in this | |
1874 | format (i.e. by encoding by the coding system `emacs-mule'). | |
1875 | ||
1876 | In that case, a sequence of one-byte codes has a slightly different | |
1877 | form. | |
1878 | ||
1879 | At first, all characters in eight-bit-control are represented by | |
1880 | one-byte sequences which are their 8-bit code. | |
1881 | ||
1882 | Next, character composition data are represented by the byte | |
1883 | sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ..., | |
1884 | where, | |
e951386e | 1885 | METHOD is 0xF2 plus one of composition method (enum |
df7492f9 KH |
1886 | composition_method), |
1887 | ||
1888 | BYTES is 0xA0 plus a byte length of this composition data, | |
1889 | ||
e951386e | 1890 | CHARS is 0xA0 plus a number of characters composed by this |
df7492f9 KH |
1891 | data, |
1892 | ||
ad1746f5 | 1893 | COMPONENTs are characters of multibyte form or composition |
df7492f9 KH |
1894 | rules encoded by two-byte of ASCII codes. |
1895 | ||
1896 | In addition, for backward compatibility, the following formats are | |
1897 | also recognized as composition data on decoding. | |
1898 | ||
1899 | 0x80 MSEQ ... | |
1900 | 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ | |
1901 | ||
1902 | Here, | |
1903 | MSEQ is a multibyte form but in these special format: | |
1904 | ASCII: 0xA0 ASCII_CODE+0x80, | |
1905 | other: LEADING_CODE+0x20 FOLLOWING-BYTE ..., | |
1906 | RULE is a one byte code of the range 0xA0..0xF0 that | |
1907 | represents a composition rule. | |
1908 | */ | |
1909 | ||
1910 | char emacs_mule_bytes[256]; | |
1911 | ||
e951386e KH |
1912 | |
1913 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
f10fe38f | 1914 | Return true if a text is encoded in 'emacs-mule'. */ |
e951386e | 1915 | |
f10fe38f | 1916 | static bool |
cf84bb53 JB |
1917 | detect_coding_emacs_mule (struct coding_system *coding, |
1918 | struct coding_detection_info *detect_info) | |
e951386e KH |
1919 | { |
1920 | const unsigned char *src = coding->source, *src_base; | |
1921 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
f10fe38f | 1922 | bool multibytep = coding->src_multibyte; |
d311d28c | 1923 | ptrdiff_t consumed_chars = 0; |
e951386e KH |
1924 | int c; |
1925 | int found = 0; | |
1926 | ||
1927 | detect_info->checked |= CATEGORY_MASK_EMACS_MULE; | |
1928 | /* A coding system of this category is always ASCII compatible. */ | |
1929 | src += coding->head_ascii; | |
1930 | ||
1931 | while (1) | |
1932 | { | |
1933 | src_base = src; | |
1934 | ONE_MORE_BYTE (c); | |
1935 | if (c < 0) | |
1936 | continue; | |
1937 | if (c == 0x80) | |
1938 | { | |
1939 | /* Perhaps the start of composite character. We simply skip | |
1940 | it because analyzing it is too heavy for detecting. But, | |
1941 | at least, we check that the composite character | |
1942 | constitutes of more than 4 bytes. */ | |
2735d060 | 1943 | const unsigned char *src_start; |
e951386e KH |
1944 | |
1945 | repeat: | |
2735d060 | 1946 | src_start = src; |
e951386e KH |
1947 | do |
1948 | { | |
1949 | ONE_MORE_BYTE (c); | |
1950 | } | |
1951 | while (c >= 0xA0); | |
1952 | ||
2735d060 | 1953 | if (src - src_start <= 4) |
e951386e KH |
1954 | break; |
1955 | found = CATEGORY_MASK_EMACS_MULE; | |
1956 | if (c == 0x80) | |
1957 | goto repeat; | |
1958 | } | |
1959 | ||
1960 | if (c < 0x80) | |
1961 | { | |
1962 | if (c < 0x20 | |
1963 | && (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)) | |
1964 | break; | |
1965 | } | |
1966 | else | |
1967 | { | |
396475b7 | 1968 | int more_bytes = emacs_mule_bytes[c] - 1; |
e951386e KH |
1969 | |
1970 | while (more_bytes > 0) | |
1971 | { | |
1972 | ONE_MORE_BYTE (c); | |
1973 | if (c < 0xA0) | |
1974 | { | |
1975 | src--; /* Unread the last byte. */ | |
1976 | break; | |
1977 | } | |
1978 | more_bytes--; | |
1979 | } | |
1980 | if (more_bytes != 0) | |
1981 | break; | |
1982 | found = CATEGORY_MASK_EMACS_MULE; | |
1983 | } | |
1984 | } | |
1985 | detect_info->rejected |= CATEGORY_MASK_EMACS_MULE; | |
1986 | return 0; | |
1987 | ||
1988 | no_more_source: | |
1989 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) | |
1990 | { | |
1991 | detect_info->rejected |= CATEGORY_MASK_EMACS_MULE; | |
1992 | return 0; | |
1993 | } | |
1994 | detect_info->found |= found; | |
1995 | return 1; | |
1996 | } | |
1997 | ||
1998 | ||
1999 | /* Parse emacs-mule multibyte sequence at SRC and return the decoded | |
2000 | character. If CMP_STATUS indicates that we must expect MSEQ or | |
2001 | RULE described above, decode it and return the negative value of | |
685ebdc8 | 2002 | the decoded character or rule. If an invalid byte is found, return |
e951386e KH |
2003 | -1. If SRC is too short, return -2. */ |
2004 | ||
e2f1bab9 | 2005 | static int |
cf84bb53 JB |
2006 | emacs_mule_char (struct coding_system *coding, const unsigned char *src, |
2007 | int *nbytes, int *nchars, int *id, | |
2008 | struct composition_status *cmp_status) | |
df7492f9 | 2009 | { |
8f924df7 KH |
2010 | const unsigned char *src_end = coding->source + coding->src_bytes; |
2011 | const unsigned char *src_base = src; | |
f10fe38f | 2012 | bool multibytep = coding->src_multibyte; |
2735d060 | 2013 | int charset_ID; |
df7492f9 KH |
2014 | unsigned code; |
2015 | int c; | |
2016 | int consumed_chars = 0; | |
f10fe38f | 2017 | bool mseq_found = 0; |
df7492f9 KH |
2018 | |
2019 | ONE_MORE_BYTE (c); | |
065e3595 | 2020 | if (c < 0) |
df7492f9 | 2021 | { |
065e3595 | 2022 | c = -c; |
2735d060 | 2023 | charset_ID = emacs_mule_charset[0]; |
065e3595 KH |
2024 | } |
2025 | else | |
2026 | { | |
4d41e8b7 KH |
2027 | if (c >= 0xA0) |
2028 | { | |
e951386e KH |
2029 | if (cmp_status->state != COMPOSING_NO |
2030 | && cmp_status->old_form) | |
4d41e8b7 | 2031 | { |
e951386e KH |
2032 | if (cmp_status->state == COMPOSING_CHAR) |
2033 | { | |
2034 | if (c == 0xA0) | |
2035 | { | |
2036 | ONE_MORE_BYTE (c); | |
2037 | c -= 0x80; | |
2038 | if (c < 0) | |
2039 | goto invalid_code; | |
2040 | } | |
2041 | else | |
2042 | c -= 0x20; | |
2043 | mseq_found = 1; | |
2044 | } | |
2045 | else | |
2046 | { | |
2047 | *nbytes = src - src_base; | |
2048 | *nchars = consumed_chars; | |
2049 | return -c; | |
2050 | } | |
4d41e8b7 KH |
2051 | } |
2052 | else | |
e951386e | 2053 | goto invalid_code; |
4d41e8b7 KH |
2054 | } |
2055 | ||
065e3595 | 2056 | switch (emacs_mule_bytes[c]) |
b73bfc1c | 2057 | { |
065e3595 | 2058 | case 2: |
2735d060 | 2059 | if ((charset_ID = emacs_mule_charset[c]) < 0) |
df7492f9 KH |
2060 | goto invalid_code; |
2061 | ONE_MORE_BYTE (c); | |
9ffd559c | 2062 | if (c < 0xA0) |
065e3595 | 2063 | goto invalid_code; |
df7492f9 | 2064 | code = c & 0x7F; |
065e3595 KH |
2065 | break; |
2066 | ||
2067 | case 3: | |
2068 | if (c == EMACS_MULE_LEADING_CODE_PRIVATE_11 | |
2069 | || c == EMACS_MULE_LEADING_CODE_PRIVATE_12) | |
2070 | { | |
2071 | ONE_MORE_BYTE (c); | |
2735d060 | 2072 | if (c < 0xA0 || (charset_ID = emacs_mule_charset[c]) < 0) |
065e3595 KH |
2073 | goto invalid_code; |
2074 | ONE_MORE_BYTE (c); | |
9ffd559c | 2075 | if (c < 0xA0) |
065e3595 KH |
2076 | goto invalid_code; |
2077 | code = c & 0x7F; | |
2078 | } | |
2079 | else | |
2080 | { | |
2735d060 | 2081 | if ((charset_ID = emacs_mule_charset[c]) < 0) |
065e3595 KH |
2082 | goto invalid_code; |
2083 | ONE_MORE_BYTE (c); | |
9ffd559c | 2084 | if (c < 0xA0) |
065e3595 KH |
2085 | goto invalid_code; |
2086 | code = (c & 0x7F) << 8; | |
2087 | ONE_MORE_BYTE (c); | |
9ffd559c | 2088 | if (c < 0xA0) |
065e3595 KH |
2089 | goto invalid_code; |
2090 | code |= c & 0x7F; | |
2091 | } | |
2092 | break; | |
2093 | ||
2094 | case 4: | |
2095 | ONE_MORE_BYTE (c); | |
2735d060 | 2096 | if (c < 0 || (charset_ID = emacs_mule_charset[c]) < 0) |
df7492f9 KH |
2097 | goto invalid_code; |
2098 | ONE_MORE_BYTE (c); | |
9ffd559c | 2099 | if (c < 0xA0) |
065e3595 | 2100 | goto invalid_code; |
781d7a48 | 2101 | code = (c & 0x7F) << 8; |
df7492f9 | 2102 | ONE_MORE_BYTE (c); |
9ffd559c | 2103 | if (c < 0xA0) |
065e3595 | 2104 | goto invalid_code; |
df7492f9 | 2105 | code |= c & 0x7F; |
065e3595 | 2106 | break; |
df7492f9 | 2107 | |
065e3595 KH |
2108 | case 1: |
2109 | code = c; | |
2735d060 | 2110 | charset_ID = ASCII_BYTE_P (code) ? charset_ascii : charset_eight_bit; |
065e3595 | 2111 | break; |
df7492f9 | 2112 | |
065e3595 | 2113 | default: |
1088b922 | 2114 | emacs_abort (); |
065e3595 | 2115 | } |
b84ae584 | 2116 | CODING_DECODE_CHAR (coding, src, src_base, src_end, |
2735d060 | 2117 | CHARSET_FROM_ID (charset_ID), code, c); |
065e3595 KH |
2118 | if (c < 0) |
2119 | goto invalid_code; | |
df7492f9 | 2120 | } |
df7492f9 KH |
2121 | *nbytes = src - src_base; |
2122 | *nchars = consumed_chars; | |
ff0dacd7 | 2123 | if (id) |
2735d060 | 2124 | *id = charset_ID; |
e951386e | 2125 | return (mseq_found ? -c : c); |
df7492f9 KH |
2126 | |
2127 | no_more_source: | |
2128 | return -2; | |
2129 | ||
2130 | invalid_code: | |
2131 | return -1; | |
2132 | } | |
2133 | ||
2134 | ||
e951386e | 2135 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ |
df7492f9 | 2136 | |
e951386e KH |
2137 | /* Handle these composition sequence ('|': the end of header elements, |
2138 | BYTES and CHARS >= 0xA0): | |
df7492f9 | 2139 | |
e951386e KH |
2140 | (1) relative composition: 0x80 0xF2 BYTES CHARS | CHAR ... |
2141 | (2) altchar composition: 0x80 0xF4 BYTES CHARS | ALT ... ALT CHAR ... | |
2142 | (3) alt&rule composition: 0x80 0xF5 BYTES CHARS | ALT RULE ... ALT CHAR ... | |
df7492f9 | 2143 | |
e951386e | 2144 | and these old form: |
1a4990fb | 2145 | |
e951386e KH |
2146 | (4) relative composition: 0x80 | MSEQ ... MSEQ |
2147 | (5) rulebase composition: 0x80 0xFF | MSEQ MRULE ... MSEQ | |
df7492f9 | 2148 | |
e951386e KH |
2149 | When the starter 0x80 and the following header elements are found, |
2150 | this annotation header is produced. | |
df7492f9 | 2151 | |
e951386e | 2152 | [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS NBYTES METHOD ] |
df7492f9 | 2153 | |
e951386e KH |
2154 | NCHARS is CHARS - 0xA0 for (1), (2), (3), and 0 for (4), (5). |
2155 | NBYTES is BYTES - 0xA0 for (1), (2), (3), and 0 for (4), (5). | |
df7492f9 | 2156 | |
e951386e KH |
2157 | Then, upon reading the following elements, these codes are produced |
2158 | until the composition end is found: | |
df7492f9 | 2159 | |
e951386e KH |
2160 | (1) CHAR ... CHAR |
2161 | (2) ALT ... ALT CHAR ... CHAR | |
2162 | (3) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT CHAR ... CHAR | |
2163 | (4) CHAR ... CHAR | |
2164 | (5) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR | |
4ed46869 | 2165 | |
e951386e KH |
2166 | When the composition end is found, LENGTH and NCHARS in the |
2167 | annotation header is updated as below: | |
b73bfc1c | 2168 | |
e951386e KH |
2169 | (1) LENGTH: unchanged, NCHARS: unchanged |
2170 | (2) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged | |
2171 | (3) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged | |
2172 | (4) LENGTH: unchanged, NCHARS: number of CHARs | |
2173 | (5) LENGTH: unchanged, NCHARS: number of CHARs | |
df7492f9 | 2174 | |
e951386e KH |
2175 | If an error is found while composing, the annotation header is |
2176 | changed to the original composition header (plus filler -1s) as | |
2177 | below: | |
2178 | ||
2179 | (1),(2),(3) [ 0x80 0xF2+METHOD BYTES CHARS -1 ] | |
2180 | (5) [ 0x80 0xFF -1 -1- -1 ] | |
2181 | ||
2182 | and the sequence [ -2 DECODED-RULE ] is changed to the original | |
2183 | byte sequence as below: | |
2184 | o the original byte sequence is B: [ B -1 ] | |
2185 | o the original byte sequence is B1 B2: [ B1 B2 ] | |
2186 | ||
2187 | Most of the routines are implemented by macros because many | |
2188 | variables and labels in the caller decode_coding_emacs_mule must be | |
2189 | accessible, and they are usually called just once (thus doesn't | |
2190 | increase the size of compiled object). */ | |
2191 | ||
2192 | /* Decode a composition rule represented by C as a component of | |
2193 | composition sequence of Emacs 20 style. Set RULE to the decoded | |
2194 | rule. */ | |
2195 | ||
2196 | #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(c, rule) \ | |
df7492f9 | 2197 | do { \ |
e951386e KH |
2198 | int gref, nref; \ |
2199 | \ | |
4d41e8b7 | 2200 | c -= 0xA0; \ |
df7492f9 KH |
2201 | if (c < 0 || c >= 81) \ |
2202 | goto invalid_code; \ | |
df7492f9 | 2203 | gref = c / 9, nref = c % 9; \ |
e951386e KH |
2204 | if (gref == 4) gref = 10; \ |
2205 | if (nref == 4) nref = 10; \ | |
2206 | rule = COMPOSITION_ENCODE_RULE (gref, nref); \ | |
df7492f9 KH |
2207 | } while (0) |
2208 | ||
2209 | ||
e951386e KH |
2210 | /* Decode a composition rule represented by C and the following byte |
2211 | at SRC as a component of composition sequence of Emacs 21 style. | |
2212 | Set RULE to the decoded rule. */ | |
781d7a48 | 2213 | |
e951386e | 2214 | #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(c, rule) \ |
781d7a48 KH |
2215 | do { \ |
2216 | int gref, nref; \ | |
e951386e KH |
2217 | \ |
2218 | gref = c - 0x20; \ | |
2219 | if (gref < 0 || gref >= 81) \ | |
781d7a48 | 2220 | goto invalid_code; \ |
e951386e KH |
2221 | ONE_MORE_BYTE (c); \ |
2222 | nref = c - 0x20; \ | |
2223 | if (nref < 0 || nref >= 81) \ | |
781d7a48 | 2224 | goto invalid_code; \ |
e951386e | 2225 | rule = COMPOSITION_ENCODE_RULE (gref, nref); \ |
781d7a48 KH |
2226 | } while (0) |
2227 | ||
2228 | ||
e951386e KH |
2229 | /* Start of Emacs 21 style format. The first three bytes at SRC are |
2230 | (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is the | |
2231 | byte length of this composition information, CHARS is the number of | |
2232 | characters composed by this composition. */ | |
2233 | ||
2234 | #define DECODE_EMACS_MULE_21_COMPOSITION() \ | |
aa72b389 | 2235 | do { \ |
781d7a48 | 2236 | enum composition_method method = c - 0xF2; \ |
df7492f9 | 2237 | int nbytes, nchars; \ |
e951386e | 2238 | \ |
df7492f9 | 2239 | ONE_MORE_BYTE (c); \ |
065e3595 KH |
2240 | if (c < 0) \ |
2241 | goto invalid_code; \ | |
df7492f9 | 2242 | nbytes = c - 0xA0; \ |
e951386e | 2243 | if (nbytes < 3 || (method == COMPOSITION_RELATIVE && nbytes != 4)) \ |
df7492f9 KH |
2244 | goto invalid_code; \ |
2245 | ONE_MORE_BYTE (c); \ | |
2246 | nchars = c - 0xA0; \ | |
e951386e KH |
2247 | if (nchars <= 0 || nchars >= MAX_COMPOSITION_COMPONENTS) \ |
2248 | goto invalid_code; \ | |
2249 | cmp_status->old_form = 0; \ | |
2250 | cmp_status->method = method; \ | |
2251 | if (method == COMPOSITION_RELATIVE) \ | |
2252 | cmp_status->state = COMPOSING_CHAR; \ | |
2253 | else \ | |
2254 | cmp_status->state = COMPOSING_COMPONENT_CHAR; \ | |
2255 | cmp_status->length = MAX_ANNOTATION_LENGTH; \ | |
2256 | cmp_status->nchars = nchars; \ | |
2257 | cmp_status->ncomps = nbytes - 4; \ | |
2258 | ADD_COMPOSITION_DATA (charbuf, nchars, nbytes, method); \ | |
aa72b389 | 2259 | } while (0) |
93dec019 | 2260 | |
aa72b389 | 2261 | |
e951386e KH |
2262 | /* Start of Emacs 20 style format for relative composition. */ |
2263 | ||
2264 | #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION() \ | |
2265 | do { \ | |
2266 | cmp_status->old_form = 1; \ | |
2267 | cmp_status->method = COMPOSITION_RELATIVE; \ | |
2268 | cmp_status->state = COMPOSING_CHAR; \ | |
2269 | cmp_status->length = MAX_ANNOTATION_LENGTH; \ | |
2270 | cmp_status->nchars = cmp_status->ncomps = 0; \ | |
2271 | ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \ | |
2272 | } while (0) | |
2273 | ||
2274 | ||
2275 | /* Start of Emacs 20 style format for rule-base composition. */ | |
2276 | ||
2277 | #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION() \ | |
2278 | do { \ | |
2279 | cmp_status->old_form = 1; \ | |
2280 | cmp_status->method = COMPOSITION_WITH_RULE; \ | |
2281 | cmp_status->state = COMPOSING_CHAR; \ | |
2282 | cmp_status->length = MAX_ANNOTATION_LENGTH; \ | |
2283 | cmp_status->nchars = cmp_status->ncomps = 0; \ | |
2284 | ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \ | |
df7492f9 KH |
2285 | } while (0) |
2286 | ||
2287 | ||
e951386e KH |
2288 | #define DECODE_EMACS_MULE_COMPOSITION_START() \ |
2289 | do { \ | |
2290 | const unsigned char *current_src = src; \ | |
2291 | \ | |
2292 | ONE_MORE_BYTE (c); \ | |
2293 | if (c < 0) \ | |
2294 | goto invalid_code; \ | |
2295 | if (c - 0xF2 >= COMPOSITION_RELATIVE \ | |
2296 | && c - 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS) \ | |
2297 | DECODE_EMACS_MULE_21_COMPOSITION (); \ | |
2298 | else if (c < 0xA0) \ | |
2299 | goto invalid_code; \ | |
2300 | else if (c < 0xC0) \ | |
2301 | { \ | |
2302 | DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (); \ | |
2303 | /* Re-read C as a composition component. */ \ | |
2304 | src = current_src; \ | |
2305 | } \ | |
2306 | else if (c == 0xFF) \ | |
2307 | DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (); \ | |
2308 | else \ | |
2309 | goto invalid_code; \ | |
2310 | } while (0) | |
2311 | ||
2312 | #define EMACS_MULE_COMPOSITION_END() \ | |
df7492f9 | 2313 | do { \ |
e951386e | 2314 | int idx = - cmp_status->length; \ |
4d41e8b7 | 2315 | \ |
e951386e KH |
2316 | if (cmp_status->old_form) \ |
2317 | charbuf[idx + 2] = cmp_status->nchars; \ | |
2318 | else if (cmp_status->method > COMPOSITION_RELATIVE) \ | |
2319 | charbuf[idx] = charbuf[idx + 2] - cmp_status->length; \ | |
2320 | cmp_status->state = COMPOSING_NO; \ | |
2321 | } while (0) | |
2322 | ||
2323 | ||
2324 | static int | |
cf84bb53 JB |
2325 | emacs_mule_finish_composition (int *charbuf, |
2326 | struct composition_status *cmp_status) | |
e951386e KH |
2327 | { |
2328 | int idx = - cmp_status->length; | |
2329 | int new_chars; | |
2330 | ||
2331 | if (cmp_status->old_form && cmp_status->nchars > 0) | |
2332 | { | |
2333 | charbuf[idx + 2] = cmp_status->nchars; | |
2334 | new_chars = 0; | |
2335 | if (cmp_status->method == COMPOSITION_WITH_RULE | |
2336 | && cmp_status->state == COMPOSING_CHAR) | |
2337 | { | |
2338 | /* The last rule was invalid. */ | |
2339 | int rule = charbuf[-1] + 0xA0; | |
2340 | ||
2341 | charbuf[-2] = BYTE8_TO_CHAR (rule); | |
2342 | charbuf[-1] = -1; | |
2343 | new_chars = 1; | |
2344 | } | |
2345 | } | |
2346 | else | |
2347 | { | |
2348 | charbuf[idx++] = BYTE8_TO_CHAR (0x80); | |
2349 | ||
2350 | if (cmp_status->method == COMPOSITION_WITH_RULE) | |
2351 | { | |
2352 | charbuf[idx++] = BYTE8_TO_CHAR (0xFF); | |
2353 | charbuf[idx++] = -3; | |
2354 | charbuf[idx++] = 0; | |
2355 | new_chars = 1; | |
2356 | } | |
2357 | else | |
2358 | { | |
2359 | int nchars = charbuf[idx + 1] + 0xA0; | |
2360 | int nbytes = charbuf[idx + 2] + 0xA0; | |
2361 | ||
2362 | charbuf[idx++] = BYTE8_TO_CHAR (0xF2 + cmp_status->method); | |
2363 | charbuf[idx++] = BYTE8_TO_CHAR (nbytes); | |
2364 | charbuf[idx++] = BYTE8_TO_CHAR (nchars); | |
2365 | charbuf[idx++] = -1; | |
2366 | new_chars = 4; | |
2367 | } | |
2368 | } | |
2369 | cmp_status->state = COMPOSING_NO; | |
2370 | return new_chars; | |
2371 | } | |
2372 | ||
2373 | #define EMACS_MULE_MAYBE_FINISH_COMPOSITION() \ | |
2374 | do { \ | |
2375 | if (cmp_status->state != COMPOSING_NO) \ | |
2376 | char_offset += emacs_mule_finish_composition (charbuf, cmp_status); \ | |
df7492f9 KH |
2377 | } while (0) |
2378 | ||
aa72b389 KH |
2379 | |
2380 | static void | |
971de7fb | 2381 | decode_coding_emacs_mule (struct coding_system *coding) |
aa72b389 | 2382 | { |
8f924df7 KH |
2383 | const unsigned char *src = coding->source + coding->consumed; |
2384 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
2385 | const unsigned char *src_base; | |
69a80ea3 | 2386 | int *charbuf = coding->charbuf + coding->charbuf_used; |
ad1746f5 KH |
2387 | /* We may produce two annotations (charset and composition) in one |
2388 | loop and one more charset annotation at the end. */ | |
69a80ea3 | 2389 | int *charbuf_end |
15cbd324 EZ |
2390 | = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 3) |
2391 | /* We can produce up to 2 characters in a loop. */ | |
2392 | - 1; | |
d311d28c | 2393 | ptrdiff_t consumed_chars = 0, consumed_chars_base; |
f10fe38f | 2394 | bool multibytep = coding->src_multibyte; |
d311d28c PE |
2395 | ptrdiff_t char_offset = coding->produced_char; |
2396 | ptrdiff_t last_offset = char_offset; | |
ff0dacd7 | 2397 | int last_id = charset_ascii; |
f10fe38f PE |
2398 | bool eol_dos |
2399 | = !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); | |
119852e7 | 2400 | int byte_after_cr = -1; |
e951386e | 2401 | struct composition_status *cmp_status = &coding->spec.emacs_mule.cmp_status; |
aa72b389 | 2402 | |
e951386e KH |
2403 | if (cmp_status->state != COMPOSING_NO) |
2404 | { | |
2405 | int i; | |
2406 | ||
15cbd324 | 2407 | if (charbuf_end - charbuf < cmp_status->length) |
1088b922 | 2408 | emacs_abort (); |
e951386e KH |
2409 | for (i = 0; i < cmp_status->length; i++) |
2410 | *charbuf++ = cmp_status->carryover[i]; | |
2411 | coding->annotated = 1; | |
2412 | } | |
2413 | ||
aa72b389 KH |
2414 | while (1) |
2415 | { | |
ee05f961 | 2416 | int c, id IF_LINT (= 0); |
df7492f9 | 2417 | |
aa72b389 | 2418 | src_base = src; |
df7492f9 KH |
2419 | consumed_chars_base = consumed_chars; |
2420 | ||
2421 | if (charbuf >= charbuf_end) | |
b71f6f73 KH |
2422 | { |
2423 | if (byte_after_cr >= 0) | |
2424 | src_base--; | |
2425 | break; | |
2426 | } | |
aa72b389 | 2427 | |
119852e7 KH |
2428 | if (byte_after_cr >= 0) |
2429 | c = byte_after_cr, byte_after_cr = -1; | |
2430 | else | |
2431 | ONE_MORE_BYTE (c); | |
e951386e KH |
2432 | |
2433 | if (c < 0 || c == 0x80) | |
065e3595 | 2434 | { |
e951386e KH |
2435 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); |
2436 | if (c < 0) | |
2437 | { | |
2438 | *charbuf++ = -c; | |
2439 | char_offset++; | |
2440 | } | |
2441 | else | |
2442 | DECODE_EMACS_MULE_COMPOSITION_START (); | |
2443 | continue; | |
065e3595 | 2444 | } |
e951386e KH |
2445 | |
2446 | if (c < 0x80) | |
aa72b389 | 2447 | { |
2735d060 | 2448 | if (eol_dos && c == '\r') |
119852e7 | 2449 | ONE_MORE_BYTE (byte_after_cr); |
e951386e KH |
2450 | id = charset_ascii; |
2451 | if (cmp_status->state != COMPOSING_NO) | |
2452 | { | |
2453 | if (cmp_status->old_form) | |
2454 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); | |
2455 | else if (cmp_status->state >= COMPOSING_COMPONENT_CHAR) | |
2456 | cmp_status->ncomps--; | |
2457 | } | |
2458 | } | |
2459 | else | |
2460 | { | |
ee05f961 | 2461 | int nchars IF_LINT (= 0), nbytes IF_LINT (= 0); |
75f80e63 EZ |
2462 | /* emacs_mule_char can load a charset map from a file, which |
2463 | allocates a large structure and might cause buffer text | |
2464 | to be relocated as result. Thus, we need to remember the | |
ad1746f5 | 2465 | original pointer to buffer text, and fix up all related |
75f80e63 EZ |
2466 | pointers after the call. */ |
2467 | const unsigned char *orig = coding->source; | |
d311d28c | 2468 | ptrdiff_t offset; |
e951386e KH |
2469 | |
2470 | c = emacs_mule_char (coding, src_base, &nbytes, &nchars, &id, | |
2471 | cmp_status); | |
75f80e63 EZ |
2472 | offset = coding->source - orig; |
2473 | if (offset) | |
2474 | { | |
2475 | src += offset; | |
2476 | src_base += offset; | |
2477 | src_end += offset; | |
2478 | } | |
e951386e KH |
2479 | if (c < 0) |
2480 | { | |
2481 | if (c == -1) | |
2482 | goto invalid_code; | |
2483 | if (c == -2) | |
2484 | break; | |
2485 | } | |
2486 | src = src_base + nbytes; | |
2487 | consumed_chars = consumed_chars_base + nchars; | |
2488 | if (cmp_status->state >= COMPOSING_COMPONENT_CHAR) | |
2489 | cmp_status->ncomps -= nchars; | |
2490 | } | |
2491 | ||
ad1746f5 | 2492 | /* Now if C >= 0, we found a normally encoded character, if C < |
e951386e KH |
2493 | 0, we found an old-style composition component character or |
2494 | rule. */ | |
2495 | ||
2496 | if (cmp_status->state == COMPOSING_NO) | |
2497 | { | |
2498 | if (last_id != id) | |
2499 | { | |
2500 | if (last_id != charset_ascii) | |
2501 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, | |
2502 | last_id); | |
2503 | last_id = id; | |
2504 | last_offset = char_offset; | |
2505 | } | |
df7492f9 KH |
2506 | *charbuf++ = c; |
2507 | char_offset++; | |
aa72b389 | 2508 | } |
e951386e | 2509 | else if (cmp_status->state == COMPOSING_CHAR) |
df7492f9 | 2510 | { |
e951386e KH |
2511 | if (cmp_status->old_form) |
2512 | { | |
2513 | if (c >= 0) | |
2514 | { | |
2515 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); | |
2516 | *charbuf++ = c; | |
2517 | char_offset++; | |
2518 | } | |
2519 | else | |
2520 | { | |
2521 | *charbuf++ = -c; | |
2522 | cmp_status->nchars++; | |
2523 | cmp_status->length++; | |
2524 | if (cmp_status->nchars == MAX_COMPOSITION_COMPONENTS) | |
2525 | EMACS_MULE_COMPOSITION_END (); | |
2526 | else if (cmp_status->method == COMPOSITION_WITH_RULE) | |
2527 | cmp_status->state = COMPOSING_RULE; | |
2528 | } | |
2529 | } | |
df7492f9 | 2530 | else |
e951386e KH |
2531 | { |
2532 | *charbuf++ = c; | |
2533 | cmp_status->length++; | |
2534 | cmp_status->nchars--; | |
2535 | if (cmp_status->nchars == 0) | |
2536 | EMACS_MULE_COMPOSITION_END (); | |
2537 | } | |
df7492f9 | 2538 | } |
e951386e | 2539 | else if (cmp_status->state == COMPOSING_RULE) |
df7492f9 | 2540 | { |
e951386e | 2541 | int rule; |
ff0dacd7 | 2542 | |
e951386e | 2543 | if (c >= 0) |
df7492f9 | 2544 | { |
e951386e KH |
2545 | EMACS_MULE_COMPOSITION_END (); |
2546 | *charbuf++ = c; | |
2547 | char_offset++; | |
df7492f9 | 2548 | } |
e951386e | 2549 | else |
ff0dacd7 | 2550 | { |
e951386e KH |
2551 | c = -c; |
2552 | DECODE_EMACS_MULE_COMPOSITION_RULE_20 (c, rule); | |
2553 | if (rule < 0) | |
2554 | goto invalid_code; | |
2555 | *charbuf++ = -2; | |
2556 | *charbuf++ = rule; | |
2557 | cmp_status->length += 2; | |
2558 | cmp_status->state = COMPOSING_CHAR; | |
ff0dacd7 | 2559 | } |
e951386e KH |
2560 | } |
2561 | else if (cmp_status->state == COMPOSING_COMPONENT_CHAR) | |
2562 | { | |
df7492f9 | 2563 | *charbuf++ = c; |
e951386e KH |
2564 | cmp_status->length++; |
2565 | if (cmp_status->ncomps == 0) | |
2566 | cmp_status->state = COMPOSING_CHAR; | |
2567 | else if (cmp_status->ncomps > 0) | |
2568 | { | |
2569 | if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS) | |
2570 | cmp_status->state = COMPOSING_COMPONENT_RULE; | |
2571 | } | |
2572 | else | |
2573 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); | |
df7492f9 | 2574 | } |
e951386e KH |
2575 | else /* COMPOSING_COMPONENT_RULE */ |
2576 | { | |
2577 | int rule; | |
2578 | ||
2579 | DECODE_EMACS_MULE_COMPOSITION_RULE_21 (c, rule); | |
2580 | if (rule < 0) | |
2581 | goto invalid_code; | |
2582 | *charbuf++ = -2; | |
2583 | *charbuf++ = rule; | |
2584 | cmp_status->length += 2; | |
2585 | cmp_status->ncomps--; | |
2586 | if (cmp_status->ncomps > 0) | |
2587 | cmp_status->state = COMPOSING_COMPONENT_CHAR; | |
2588 | else | |
2589 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); | |
2590 | } | |
2591 | continue; | |
2592 | ||
df7492f9 | 2593 | invalid_code: |
e951386e | 2594 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); |
df7492f9 KH |
2595 | src = src_base; |
2596 | consumed_chars = consumed_chars_base; | |
2597 | ONE_MORE_BYTE (c); | |
2598 | *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); | |
ff0dacd7 | 2599 | char_offset++; |
df7492f9 KH |
2600 | coding->errors++; |
2601 | } | |
2602 | ||
2603 | no_more_source: | |
e951386e KH |
2604 | if (cmp_status->state != COMPOSING_NO) |
2605 | { | |
2606 | if (coding->mode & CODING_MODE_LAST_BLOCK) | |
2607 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); | |
2608 | else | |
2609 | { | |
2610 | int i; | |
2611 | ||
2612 | charbuf -= cmp_status->length; | |
2613 | for (i = 0; i < cmp_status->length; i++) | |
2614 | cmp_status->carryover[i] = charbuf[i]; | |
2615 | } | |
2616 | } | |
ff0dacd7 | 2617 | if (last_id != charset_ascii) |
69a80ea3 | 2618 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
2619 | coding->consumed_char += consumed_chars_base; |
2620 | coding->consumed = src_base - coding->source; | |
2621 | coding->charbuf_used = charbuf - coding->charbuf; | |
2622 | } | |
2623 | ||
2624 | ||
2625 | #define EMACS_MULE_LEADING_CODES(id, codes) \ | |
2626 | do { \ | |
2627 | if (id < 0xA0) \ | |
2628 | codes[0] = id, codes[1] = 0; \ | |
2629 | else if (id < 0xE0) \ | |
2630 | codes[0] = 0x9A, codes[1] = id; \ | |
2631 | else if (id < 0xF0) \ | |
2632 | codes[0] = 0x9B, codes[1] = id; \ | |
2633 | else if (id < 0xF5) \ | |
2634 | codes[0] = 0x9C, codes[1] = id; \ | |
2635 | else \ | |
2636 | codes[0] = 0x9D, codes[1] = id; \ | |
2637 | } while (0); | |
2638 | ||
aa72b389 | 2639 | |
f10fe38f | 2640 | static bool |
971de7fb | 2641 | encode_coding_emacs_mule (struct coding_system *coding) |
df7492f9 | 2642 | { |
f10fe38f | 2643 | bool multibytep = coding->dst_multibyte; |
df7492f9 KH |
2644 | int *charbuf = coding->charbuf; |
2645 | int *charbuf_end = charbuf + coding->charbuf_used; | |
2646 | unsigned char *dst = coding->destination + coding->produced; | |
2647 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
2648 | int safe_room = 8; | |
d311d28c | 2649 | ptrdiff_t produced_chars = 0; |
24a73b0a | 2650 | Lisp_Object attrs, charset_list; |
df7492f9 | 2651 | int c; |
ff0dacd7 | 2652 | int preferred_charset_id = -1; |
df7492f9 | 2653 | |
24a73b0a | 2654 | CODING_GET_INFO (coding, attrs, charset_list); |
eccb6815 KH |
2655 | if (! EQ (charset_list, Vemacs_mule_charset_list)) |
2656 | { | |
4939150c PE |
2657 | charset_list = Vemacs_mule_charset_list; |
2658 | ASET (attrs, coding_attr_charset_list, charset_list); | |
eccb6815 | 2659 | } |
df7492f9 KH |
2660 | |
2661 | while (charbuf < charbuf_end) | |
2662 | { | |
2663 | ASSURE_DESTINATION (safe_room); | |
2664 | c = *charbuf++; | |
ff0dacd7 KH |
2665 | |
2666 | if (c < 0) | |
2667 | { | |
2668 | /* Handle an annotation. */ | |
2669 | switch (*charbuf) | |
2670 | { | |
2671 | case CODING_ANNOTATE_COMPOSITION_MASK: | |
2672 | /* Not yet implemented. */ | |
2673 | break; | |
2674 | case CODING_ANNOTATE_CHARSET_MASK: | |
2675 | preferred_charset_id = charbuf[3]; | |
2676 | if (preferred_charset_id >= 0 | |
2677 | && NILP (Fmemq (make_number (preferred_charset_id), | |
2678 | charset_list))) | |
2679 | preferred_charset_id = -1; | |
2680 | break; | |
2681 | default: | |
1088b922 | 2682 | emacs_abort (); |
ff0dacd7 KH |
2683 | } |
2684 | charbuf += -c - 1; | |
2685 | continue; | |
2686 | } | |
2687 | ||
df7492f9 KH |
2688 | if (ASCII_CHAR_P (c)) |
2689 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d KH |
2690 | else if (CHAR_BYTE8_P (c)) |
2691 | { | |
2692 | c = CHAR_TO_BYTE8 (c); | |
2693 | EMIT_ONE_BYTE (c); | |
2694 | } | |
df7492f9 | 2695 | else |
aa72b389 | 2696 | { |
df7492f9 KH |
2697 | struct charset *charset; |
2698 | unsigned code; | |
2699 | int dimension; | |
2700 | int emacs_mule_id; | |
2701 | unsigned char leading_codes[2]; | |
2702 | ||
ff0dacd7 KH |
2703 | if (preferred_charset_id >= 0) |
2704 | { | |
f10fe38f | 2705 | bool result; |
5eb05ea3 | 2706 | |
ff0dacd7 | 2707 | charset = CHARSET_FROM_ID (preferred_charset_id); |
5eb05ea3 KH |
2708 | CODING_CHAR_CHARSET_P (coding, dst, dst_end, c, charset, result); |
2709 | if (result) | |
905ca9d2 KH |
2710 | code = ENCODE_CHAR (charset, c); |
2711 | else | |
5eb05ea3 KH |
2712 | CODING_CHAR_CHARSET (coding, dst, dst_end, c, charset_list, |
2713 | &code, charset); | |
ff0dacd7 KH |
2714 | } |
2715 | else | |
5eb05ea3 KH |
2716 | CODING_CHAR_CHARSET (coding, dst, dst_end, c, charset_list, |
2717 | &code, charset); | |
df7492f9 KH |
2718 | if (! charset) |
2719 | { | |
2720 | c = coding->default_char; | |
2721 | if (ASCII_CHAR_P (c)) | |
2722 | { | |
2723 | EMIT_ONE_ASCII_BYTE (c); | |
2724 | continue; | |
2725 | } | |
5eb05ea3 KH |
2726 | CODING_CHAR_CHARSET (coding, dst, dst_end, c, charset_list, |
2727 | &code, charset); | |
df7492f9 KH |
2728 | } |
2729 | dimension = CHARSET_DIMENSION (charset); | |
2730 | emacs_mule_id = CHARSET_EMACS_MULE_ID (charset); | |
2731 | EMACS_MULE_LEADING_CODES (emacs_mule_id, leading_codes); | |
2732 | EMIT_ONE_BYTE (leading_codes[0]); | |
2733 | if (leading_codes[1]) | |
2734 | EMIT_ONE_BYTE (leading_codes[1]); | |
2735 | if (dimension == 1) | |
1fa663f9 | 2736 | EMIT_ONE_BYTE (code | 0x80); |
aa72b389 | 2737 | else |
df7492f9 | 2738 | { |
1fa663f9 | 2739 | code |= 0x8080; |
df7492f9 KH |
2740 | EMIT_ONE_BYTE (code >> 8); |
2741 | EMIT_ONE_BYTE (code & 0xFF); | |
2742 | } | |
aa72b389 | 2743 | } |
aa72b389 | 2744 | } |
065e3595 | 2745 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
2746 | coding->produced_char += produced_chars; |
2747 | coding->produced = dst - coding->destination; | |
2748 | return 0; | |
aa72b389 | 2749 | } |
b73bfc1c | 2750 | |
4ed46869 | 2751 | \f |
df7492f9 | 2752 | /*** 7. ISO2022 handlers ***/ |
4ed46869 KH |
2753 | |
2754 | /* The following note describes the coding system ISO2022 briefly. | |
39787efd | 2755 | Since the intention of this note is to help understand the |
5a936b46 | 2756 | functions in this file, some parts are NOT ACCURATE or are OVERLY |
39787efd | 2757 | SIMPLIFIED. For thorough understanding, please refer to the |
5a936b46 | 2758 | original document of ISO2022. This is equivalent to the standard |
cfb43547 | 2759 | ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*). |
4ed46869 KH |
2760 | |
2761 | ISO2022 provides many mechanisms to encode several character sets | |
cfb43547 | 2762 | in 7-bit and 8-bit environments. For 7-bit environments, all text |
39787efd KH |
2763 | is encoded using bytes less than 128. This may make the encoded |
2764 | text a little bit longer, but the text passes more easily through | |
cfb43547 | 2765 | several types of gateway, some of which strip off the MSB (Most |
8ca3766a | 2766 | Significant Bit). |
b73bfc1c | 2767 | |
cfb43547 DL |
2768 | There are two kinds of character sets: control character sets and |
2769 | graphic character sets. The former contain control characters such | |
4ed46869 | 2770 | as `newline' and `escape' to provide control functions (control |
39787efd | 2771 | functions are also provided by escape sequences). The latter |
cfb43547 | 2772 | contain graphic characters such as 'A' and '-'. Emacs recognizes |
4ed46869 KH |
2773 | two control character sets and many graphic character sets. |
2774 | ||
2775 | Graphic character sets are classified into one of the following | |
39787efd KH |
2776 | four classes, according to the number of bytes (DIMENSION) and |
2777 | number of characters in one dimension (CHARS) of the set: | |
2778 | - DIMENSION1_CHARS94 | |
2779 | - DIMENSION1_CHARS96 | |
2780 | - DIMENSION2_CHARS94 | |
2781 | - DIMENSION2_CHARS96 | |
2782 | ||
2783 | In addition, each character set is assigned an identification tag, | |
cfb43547 | 2784 | unique for each set, called the "final character" (denoted as <F> |
39787efd KH |
2785 | hereafter). The <F> of each character set is decided by ECMA(*) |
2786 | when it is registered in ISO. The code range of <F> is 0x30..0x7F | |
2787 | (0x30..0x3F are for private use only). | |
4ed46869 KH |
2788 | |
2789 | Note (*): ECMA = European Computer Manufacturers Association | |
2790 | ||
cfb43547 | 2791 | Here are examples of graphic character sets [NAME(<F>)]: |
4ed46869 KH |
2792 | o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ... |
2793 | o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ... | |
2794 | o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ... | |
2795 | o DIMENSION2_CHARS96 -- none for the moment | |
2796 | ||
39787efd | 2797 | A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR. |
4ed46869 KH |
2798 | C0 [0x00..0x1F] -- control character plane 0 |
2799 | GL [0x20..0x7F] -- graphic character plane 0 | |
2800 | C1 [0x80..0x9F] -- control character plane 1 | |
2801 | GR [0xA0..0xFF] -- graphic character plane 1 | |
2802 | ||
2803 | A control character set is directly designated and invoked to C0 or | |
39787efd KH |
2804 | C1 by an escape sequence. The most common case is that: |
2805 | - ISO646's control character set is designated/invoked to C0, and | |
2806 | - ISO6429's control character set is designated/invoked to C1, | |
2807 | and usually these designations/invocations are omitted in encoded | |
2808 | text. In a 7-bit environment, only C0 can be used, and a control | |
2809 | character for C1 is encoded by an appropriate escape sequence to | |
2810 | fit into the environment. All control characters for C1 are | |
2811 | defined to have corresponding escape sequences. | |
4ed46869 KH |
2812 | |
2813 | A graphic character set is at first designated to one of four | |
2814 | graphic registers (G0 through G3), then these graphic registers are | |
2815 | invoked to GL or GR. These designations and invocations can be | |
2816 | done independently. The most common case is that G0 is invoked to | |
39787efd KH |
2817 | GL, G1 is invoked to GR, and ASCII is designated to G0. Usually |
2818 | these invocations and designations are omitted in encoded text. | |
2819 | In a 7-bit environment, only GL can be used. | |
4ed46869 | 2820 | |
39787efd KH |
2821 | When a graphic character set of CHARS94 is invoked to GL, codes |
2822 | 0x20 and 0x7F of the GL area work as control characters SPACE and | |
2823 | DEL respectively, and codes 0xA0 and 0xFF of the GR area should not | |
2824 | be used. | |
4ed46869 KH |
2825 | |
2826 | There are two ways of invocation: locking-shift and single-shift. | |
2827 | With locking-shift, the invocation lasts until the next different | |
39787efd KH |
2828 | invocation, whereas with single-shift, the invocation affects the |
2829 | following character only and doesn't affect the locking-shift | |
2830 | state. Invocations are done by the following control characters or | |
2831 | escape sequences: | |
4ed46869 KH |
2832 | |
2833 | ---------------------------------------------------------------------- | |
39787efd | 2834 | abbrev function cntrl escape seq description |
4ed46869 | 2835 | ---------------------------------------------------------------------- |
39787efd KH |
2836 | SI/LS0 (shift-in) 0x0F none invoke G0 into GL |
2837 | SO/LS1 (shift-out) 0x0E none invoke G1 into GL | |
2838 | LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL | |
2839 | LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL | |
2840 | LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*) | |
2841 | LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*) | |
2842 | LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*) | |
2843 | SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char | |
2844 | SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char | |
4ed46869 | 2845 | ---------------------------------------------------------------------- |
39787efd KH |
2846 | (*) These are not used by any known coding system. |
2847 | ||
2848 | Control characters for these functions are defined by macros | |
2849 | ISO_CODE_XXX in `coding.h'. | |
4ed46869 | 2850 | |
39787efd | 2851 | Designations are done by the following escape sequences: |
4ed46869 KH |
2852 | ---------------------------------------------------------------------- |
2853 | escape sequence description | |
2854 | ---------------------------------------------------------------------- | |
2855 | ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0 | |
2856 | ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1 | |
2857 | ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2 | |
2858 | ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3 | |
2859 | ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*) | |
2860 | ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1 | |
2861 | ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2 | |
2862 | ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3 | |
2863 | ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**) | |
2864 | ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1 | |
2865 | ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2 | |
2866 | ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3 | |
2867 | ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*) | |
2868 | ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1 | |
2869 | ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2 | |
2870 | ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3 | |
2871 | ---------------------------------------------------------------------- | |
2872 | ||
2873 | In this list, "DIMENSION1_CHARS94<F>" means a graphic character set | |
39787efd | 2874 | of dimension 1, chars 94, and final character <F>, etc... |
4ed46869 KH |
2875 | |
2876 | Note (*): Although these designations are not allowed in ISO2022, | |
2877 | Emacs accepts them on decoding, and produces them on encoding | |
39787efd | 2878 | CHARS96 character sets in a coding system which is characterized as |
4ed46869 KH |
2879 | 7-bit environment, non-locking-shift, and non-single-shift. |
2880 | ||
2881 | Note (**): If <F> is '@', 'A', or 'B', the intermediate character | |
df7492f9 | 2882 | '(' must be omitted. We refer to this as "short-form" hereafter. |
4ed46869 | 2883 | |
cfb43547 | 2884 | Now you may notice that there are a lot of ways of encoding the |
39787efd KH |
2885 | same multilingual text in ISO2022. Actually, there exist many |
2886 | coding systems such as Compound Text (used in X11's inter client | |
8ca3766a DL |
2887 | communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR |
2888 | (used in Korean Internet), EUC (Extended UNIX Code, used in Asian | |
4ed46869 KH |
2889 | localized platforms), and all of these are variants of ISO2022. |
2890 | ||
2891 | In addition to the above, Emacs handles two more kinds of escape | |
2892 | sequences: ISO6429's direction specification and Emacs' private | |
2893 | sequence for specifying character composition. | |
2894 | ||
39787efd | 2895 | ISO6429's direction specification takes the following form: |
4ed46869 KH |
2896 | o CSI ']' -- end of the current direction |
2897 | o CSI '0' ']' -- end of the current direction | |
2898 | o CSI '1' ']' -- start of left-to-right text | |
2899 | o CSI '2' ']' -- start of right-to-left text | |
2900 | The control character CSI (0x9B: control sequence introducer) is | |
39787efd KH |
2901 | abbreviated to the escape sequence ESC '[' in a 7-bit environment. |
2902 | ||
2903 | Character composition specification takes the following form: | |
ec6d2bb8 KH |
2904 | o ESC '0' -- start relative composition |
2905 | o ESC '1' -- end composition | |
2906 | o ESC '2' -- start rule-base composition (*) | |
2907 | o ESC '3' -- start relative composition with alternate chars (**) | |
2908 | o ESC '4' -- start rule-base composition with alternate chars (**) | |
b73bfc1c | 2909 | Since these are not standard escape sequences of any ISO standard, |
cfb43547 | 2910 | the use of them with these meanings is restricted to Emacs only. |
ec6d2bb8 | 2911 | |
5a936b46 DL |
2912 | (*) This form is used only in Emacs 20.7 and older versions, |
2913 | but newer versions can safely decode it. | |
cfb43547 | 2914 | (**) This form is used only in Emacs 21.1 and newer versions, |
5a936b46 | 2915 | and older versions can't decode it. |
ec6d2bb8 | 2916 | |
cfb43547 | 2917 | Here's a list of example usages of these composition escape |
b73bfc1c | 2918 | sequences (categorized by `enum composition_method'). |
ec6d2bb8 | 2919 | |
b73bfc1c | 2920 | COMPOSITION_RELATIVE: |
ec6d2bb8 | 2921 | ESC 0 CHAR [ CHAR ] ESC 1 |
8ca3766a | 2922 | COMPOSITION_WITH_RULE: |
ec6d2bb8 | 2923 | ESC 2 CHAR [ RULE CHAR ] ESC 1 |
b73bfc1c | 2924 | COMPOSITION_WITH_ALTCHARS: |
ec6d2bb8 | 2925 | ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 |
b73bfc1c | 2926 | COMPOSITION_WITH_RULE_ALTCHARS: |
ec6d2bb8 | 2927 | ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */ |
4ed46869 | 2928 | |
74ab6df5 | 2929 | static enum iso_code_class_type iso_code_class[256]; |
4ed46869 | 2930 | |
df7492f9 KH |
2931 | #define SAFE_CHARSET_P(coding, id) \ |
2932 | ((id) <= (coding)->max_charset_id \ | |
1b3b981b | 2933 | && (coding)->safe_charsets[id] != 255) |
df7492f9 | 2934 | |
df7492f9 | 2935 | static void |
971de7fb | 2936 | setup_iso_safe_charsets (Lisp_Object attrs) |
df7492f9 KH |
2937 | { |
2938 | Lisp_Object charset_list, safe_charsets; | |
2939 | Lisp_Object request; | |
2940 | Lisp_Object reg_usage; | |
2941 | Lisp_Object tail; | |
d311d28c | 2942 | EMACS_INT reg94, reg96; |
df7492f9 KH |
2943 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); |
2944 | int max_charset_id; | |
2945 | ||
2946 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
2947 | if ((flags & CODING_ISO_FLAG_FULL_SUPPORT) | |
2948 | && ! EQ (charset_list, Viso_2022_charset_list)) | |
2949 | { | |
4939150c PE |
2950 | charset_list = Viso_2022_charset_list; |
2951 | ASET (attrs, coding_attr_charset_list, charset_list); | |
df7492f9 KH |
2952 | ASET (attrs, coding_attr_safe_charsets, Qnil); |
2953 | } | |
2954 | ||
2955 | if (STRINGP (AREF (attrs, coding_attr_safe_charsets))) | |
2956 | return; | |
2957 | ||
2958 | max_charset_id = 0; | |
2959 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) | |
2960 | { | |
2961 | int id = XINT (XCAR (tail)); | |
2962 | if (max_charset_id < id) | |
2963 | max_charset_id = id; | |
2964 | } | |
d46c5b12 | 2965 | |
1b3b981b AS |
2966 | safe_charsets = make_uninit_string (max_charset_id + 1); |
2967 | memset (SDATA (safe_charsets), 255, max_charset_id + 1); | |
df7492f9 KH |
2968 | request = AREF (attrs, coding_attr_iso_request); |
2969 | reg_usage = AREF (attrs, coding_attr_iso_usage); | |
2970 | reg94 = XINT (XCAR (reg_usage)); | |
2971 | reg96 = XINT (XCDR (reg_usage)); | |
2972 | ||
2973 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) | |
2974 | { | |
2975 | Lisp_Object id; | |
2976 | Lisp_Object reg; | |
2977 | struct charset *charset; | |
2978 | ||
2979 | id = XCAR (tail); | |
2980 | charset = CHARSET_FROM_ID (XINT (id)); | |
bf16eb23 | 2981 | reg = Fcdr (Fassq (id, request)); |
df7492f9 | 2982 | if (! NILP (reg)) |
8f924df7 | 2983 | SSET (safe_charsets, XINT (id), XINT (reg)); |
df7492f9 KH |
2984 | else if (charset->iso_chars_96) |
2985 | { | |
2986 | if (reg96 < 4) | |
8f924df7 | 2987 | SSET (safe_charsets, XINT (id), reg96); |
df7492f9 KH |
2988 | } |
2989 | else | |
2990 | { | |
2991 | if (reg94 < 4) | |
8f924df7 | 2992 | SSET (safe_charsets, XINT (id), reg94); |
df7492f9 KH |
2993 | } |
2994 | } | |
2995 | ASET (attrs, coding_attr_safe_charsets, safe_charsets); | |
2996 | } | |
d46c5b12 | 2997 | |
b6871cc7 | 2998 | |
4ed46869 | 2999 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". |
f10fe38f PE |
3000 | Return true if a text is encoded in one of ISO-2022 based coding |
3001 | systems. */ | |
4ed46869 | 3002 | |
f10fe38f | 3003 | static bool |
cf84bb53 JB |
3004 | detect_coding_iso_2022 (struct coding_system *coding, |
3005 | struct coding_detection_info *detect_info) | |
4ed46869 | 3006 | { |
8f924df7 KH |
3007 | const unsigned char *src = coding->source, *src_base = src; |
3008 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
f10fe38f PE |
3009 | bool multibytep = coding->src_multibyte; |
3010 | bool single_shifting = 0; | |
0e48bb22 | 3011 | int id; |
df7492f9 | 3012 | int c, c1; |
d311d28c | 3013 | ptrdiff_t consumed_chars = 0; |
df7492f9 | 3014 | int i; |
ff0dacd7 KH |
3015 | int rejected = 0; |
3016 | int found = 0; | |
cee53ed4 | 3017 | int composition_count = -1; |
ff0dacd7 KH |
3018 | |
3019 | detect_info->checked |= CATEGORY_MASK_ISO; | |
df7492f9 KH |
3020 | |
3021 | for (i = coding_category_iso_7; i <= coding_category_iso_8_else; i++) | |
3022 | { | |
3023 | struct coding_system *this = &(coding_categories[i]); | |
3024 | Lisp_Object attrs, val; | |
3025 | ||
c6b278e7 KH |
3026 | if (this->id < 0) |
3027 | continue; | |
df7492f9 KH |
3028 | attrs = CODING_ID_ATTRS (this->id); |
3029 | if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT | |
1b3b981b | 3030 | && ! EQ (CODING_ATTR_CHARSET_LIST (attrs), Viso_2022_charset_list)) |
df7492f9 KH |
3031 | setup_iso_safe_charsets (attrs); |
3032 | val = CODING_ATTR_SAFE_CHARSETS (attrs); | |
8f924df7 | 3033 | this->max_charset_id = SCHARS (val) - 1; |
1b3b981b | 3034 | this->safe_charsets = SDATA (val); |
df7492f9 KH |
3035 | } |
3036 | ||
3037 | /* A coding system of this category is always ASCII compatible. */ | |
3038 | src += coding->head_ascii; | |
3f003981 | 3039 | |
ff0dacd7 | 3040 | while (rejected != CATEGORY_MASK_ISO) |
4ed46869 | 3041 | { |
065e3595 | 3042 | src_base = src; |
df7492f9 | 3043 | ONE_MORE_BYTE (c); |
4ed46869 KH |
3044 | switch (c) |
3045 | { | |
3046 | case ISO_CODE_ESC: | |
74383408 KH |
3047 | if (inhibit_iso_escape_detection) |
3048 | break; | |
f46869e4 | 3049 | single_shifting = 0; |
df7492f9 | 3050 | ONE_MORE_BYTE (c); |
0e48bb22 | 3051 | if (c == 'N' || c == 'O') |
d46c5b12 | 3052 | { |
ae9ff118 | 3053 | /* ESC <Fe> for SS2 or SS3. */ |
ff0dacd7 KH |
3054 | single_shifting = 1; |
3055 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_8BIT; | |
4ed46869 | 3056 | } |
cee53ed4 KH |
3057 | else if (c == '1') |
3058 | { | |
3059 | /* End of composition. */ | |
3060 | if (composition_count < 0 | |
3061 | || composition_count > MAX_COMPOSITION_COMPONENTS) | |
3062 | /* Invalid */ | |
3063 | break; | |
3064 | composition_count = -1; | |
3065 | found |= CATEGORY_MASK_ISO; | |
3066 | } | |
ec6d2bb8 KH |
3067 | else if (c >= '0' && c <= '4') |
3068 | { | |
3069 | /* ESC <Fp> for start/end composition. */ | |
cee53ed4 | 3070 | composition_count = 0; |
ec6d2bb8 | 3071 | } |
bf9cdd4e | 3072 | else |
df7492f9 | 3073 | { |
0e48bb22 AS |
3074 | if (c >= '(' && c <= '/') |
3075 | { | |
3076 | /* Designation sequence for a charset of dimension 1. */ | |
3077 | ONE_MORE_BYTE (c1); | |
3078 | if (c1 < ' ' || c1 >= 0x80 | |
3079 | || (id = iso_charset_table[0][c >= ','][c1]) < 0) | |
3080 | /* Invalid designation sequence. Just ignore. */ | |
3081 | break; | |
3082 | } | |
3083 | else if (c == '$') | |
3084 | { | |
3085 | /* Designation sequence for a charset of dimension 2. */ | |
3086 | ONE_MORE_BYTE (c); | |
3087 | if (c >= '@' && c <= 'B') | |
3088 | /* Designation for JISX0208.1978, GB2312, or JISX0208. */ | |
3089 | id = iso_charset_table[1][0][c]; | |
3090 | else if (c >= '(' && c <= '/') | |
3091 | { | |
3092 | ONE_MORE_BYTE (c1); | |
3093 | if (c1 < ' ' || c1 >= 0x80 | |
3094 | || (id = iso_charset_table[1][c >= ','][c1]) < 0) | |
3095 | /* Invalid designation sequence. Just ignore. */ | |
3096 | break; | |
3097 | } | |
3098 | else | |
3099 | /* Invalid designation sequence. Just ignore it. */ | |
3100 | break; | |
3101 | } | |
3102 | else | |
3103 | { | |
3104 | /* Invalid escape sequence. Just ignore it. */ | |
3105 | break; | |
3106 | } | |
d46c5b12 | 3107 | |
0e48bb22 AS |
3108 | /* We found a valid designation sequence for CHARSET. */ |
3109 | rejected |= CATEGORY_MASK_ISO_8BIT; | |
3110 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7], | |
3111 | id)) | |
3112 | found |= CATEGORY_MASK_ISO_7; | |
3113 | else | |
3114 | rejected |= CATEGORY_MASK_ISO_7; | |
3115 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7_tight], | |
3116 | id)) | |
3117 | found |= CATEGORY_MASK_ISO_7_TIGHT; | |
3118 | else | |
3119 | rejected |= CATEGORY_MASK_ISO_7_TIGHT; | |
3120 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7_else], | |
3121 | id)) | |
3122 | found |= CATEGORY_MASK_ISO_7_ELSE; | |
3123 | else | |
3124 | rejected |= CATEGORY_MASK_ISO_7_ELSE; | |
3125 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_8_else], | |
3126 | id)) | |
3127 | found |= CATEGORY_MASK_ISO_8_ELSE; | |
3128 | else | |
3129 | rejected |= CATEGORY_MASK_ISO_8_ELSE; | |
3130 | } | |
4ed46869 KH |
3131 | break; |
3132 | ||
4ed46869 | 3133 | case ISO_CODE_SO: |
d46c5b12 | 3134 | case ISO_CODE_SI: |
ff0dacd7 | 3135 | /* Locking shift out/in. */ |
74383408 KH |
3136 | if (inhibit_iso_escape_detection) |
3137 | break; | |
f46869e4 | 3138 | single_shifting = 0; |
ff0dacd7 | 3139 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_8BIT; |
d46c5b12 KH |
3140 | break; |
3141 | ||
4ed46869 | 3142 | case ISO_CODE_CSI: |
ff0dacd7 | 3143 | /* Control sequence introducer. */ |
f46869e4 | 3144 | single_shifting = 0; |
ff0dacd7 KH |
3145 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE; |
3146 | found |= CATEGORY_MASK_ISO_8_ELSE; | |
3147 | goto check_extra_latin; | |
3148 | ||
4ed46869 KH |
3149 | case ISO_CODE_SS2: |
3150 | case ISO_CODE_SS3: | |
ff0dacd7 KH |
3151 | /* Single shift. */ |
3152 | if (inhibit_iso_escape_detection) | |
3153 | break; | |
75e2a253 | 3154 | single_shifting = 0; |
ff0dacd7 KH |
3155 | rejected |= CATEGORY_MASK_ISO_7BIT; |
3156 | if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1]) | |
3157 | & CODING_ISO_FLAG_SINGLE_SHIFT) | |
0e48bb22 AS |
3158 | { |
3159 | found |= CATEGORY_MASK_ISO_8_1; | |
3160 | single_shifting = 1; | |
3161 | } | |
ff0dacd7 KH |
3162 | if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_2]) |
3163 | & CODING_ISO_FLAG_SINGLE_SHIFT) | |
0e48bb22 AS |
3164 | { |
3165 | found |= CATEGORY_MASK_ISO_8_2; | |
3166 | single_shifting = 1; | |
3167 | } | |
75e2a253 KH |
3168 | if (single_shifting) |
3169 | break; | |
ba14c607 | 3170 | goto check_extra_latin; |
4ed46869 KH |
3171 | |
3172 | default: | |
065e3595 KH |
3173 | if (c < 0) |
3174 | continue; | |
4ed46869 | 3175 | if (c < 0x80) |
f46869e4 | 3176 | { |
cee53ed4 KH |
3177 | if (composition_count >= 0) |
3178 | composition_count++; | |
f46869e4 KH |
3179 | single_shifting = 0; |
3180 | break; | |
3181 | } | |
ff0dacd7 | 3182 | if (c >= 0xA0) |
c4825358 | 3183 | { |
ff0dacd7 KH |
3184 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE; |
3185 | found |= CATEGORY_MASK_ISO_8_1; | |
f46869e4 | 3186 | /* Check the length of succeeding codes of the range |
ff0dacd7 KH |
3187 | 0xA0..0FF. If the byte length is even, we include |
3188 | CATEGORY_MASK_ISO_8_2 in `found'. We can check this | |
3189 | only when we are not single shifting. */ | |
3190 | if (! single_shifting | |
3191 | && ! (rejected & CATEGORY_MASK_ISO_8_2)) | |
f46869e4 | 3192 | { |
2735d060 | 3193 | int len = 1; |
b73bfc1c KH |
3194 | while (src < src_end) |
3195 | { | |
d12bd917 | 3196 | src_base = src; |
df7492f9 | 3197 | ONE_MORE_BYTE (c); |
b73bfc1c | 3198 | if (c < 0xA0) |
d12bd917 KH |
3199 | { |
3200 | src = src_base; | |
3201 | break; | |
3202 | } | |
2735d060 | 3203 | len++; |
b73bfc1c KH |
3204 | } |
3205 | ||
2735d060 | 3206 | if (len & 1 && src < src_end) |
cee53ed4 KH |
3207 | { |
3208 | rejected |= CATEGORY_MASK_ISO_8_2; | |
3209 | if (composition_count >= 0) | |
2735d060 | 3210 | composition_count += len; |
cee53ed4 | 3211 | } |
f46869e4 | 3212 | else |
cee53ed4 KH |
3213 | { |
3214 | found |= CATEGORY_MASK_ISO_8_2; | |
3215 | if (composition_count >= 0) | |
2735d060 | 3216 | composition_count += len / 2; |
cee53ed4 | 3217 | } |
f46869e4 | 3218 | } |
ff0dacd7 | 3219 | break; |
4ed46869 | 3220 | } |
ba14c607 AS |
3221 | check_extra_latin: |
3222 | if (! VECTORP (Vlatin_extra_code_table) | |
3223 | || NILP (AREF (Vlatin_extra_code_table, c))) | |
3224 | { | |
3225 | rejected = CATEGORY_MASK_ISO; | |
3226 | break; | |
3227 | } | |
3228 | if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1]) | |
3229 | & CODING_ISO_FLAG_LATIN_EXTRA) | |
3230 | found |= CATEGORY_MASK_ISO_8_1; | |
3231 | else | |
3232 | rejected |= CATEGORY_MASK_ISO_8_1; | |
3233 | rejected |= CATEGORY_MASK_ISO_8_2; | |
3234 | break; | |
4ed46869 KH |
3235 | } |
3236 | } | |
ff0dacd7 KH |
3237 | detect_info->rejected |= CATEGORY_MASK_ISO; |
3238 | return 0; | |
4ed46869 | 3239 | |
df7492f9 | 3240 | no_more_source: |
ff0dacd7 KH |
3241 | detect_info->rejected |= rejected; |
3242 | detect_info->found |= (found & ~rejected); | |
df7492f9 | 3243 | return 1; |
4ed46869 | 3244 | } |
ec6d2bb8 | 3245 | |
4ed46869 | 3246 | |
134b9549 KH |
3247 | /* Set designation state into CODING. Set CHARS_96 to -1 if the |
3248 | escape sequence should be kept. */ | |
df7492f9 KH |
3249 | #define DECODE_DESIGNATION(reg, dim, chars_96, final) \ |
3250 | do { \ | |
3251 | int id, prev; \ | |
3252 | \ | |
3253 | if (final < '0' || final >= 128 \ | |
3254 | || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \ | |
3255 | || !SAFE_CHARSET_P (coding, id)) \ | |
3256 | { \ | |
3257 | CODING_ISO_DESIGNATION (coding, reg) = -2; \ | |
134b9549 KH |
3258 | chars_96 = -1; \ |
3259 | break; \ | |
df7492f9 KH |
3260 | } \ |
3261 | prev = CODING_ISO_DESIGNATION (coding, reg); \ | |
bf16eb23 KH |
3262 | if (id == charset_jisx0201_roman) \ |
3263 | { \ | |
3264 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \ | |
3265 | id = charset_ascii; \ | |
3266 | } \ | |
3267 | else if (id == charset_jisx0208_1978) \ | |
3268 | { \ | |
3269 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \ | |
3270 | id = charset_jisx0208; \ | |
3271 | } \ | |
df7492f9 KH |
3272 | CODING_ISO_DESIGNATION (coding, reg) = id; \ |
3273 | /* If there was an invalid designation to REG previously, and this \ | |
3274 | designation is ASCII to REG, we should keep this designation \ | |
3275 | sequence. */ \ | |
3276 | if (prev == -2 && id == charset_ascii) \ | |
134b9549 | 3277 | chars_96 = -1; \ |
4ed46869 KH |
3278 | } while (0) |
3279 | ||
d46c5b12 | 3280 | |
e951386e KH |
3281 | /* Handle these composition sequence (ALT: alternate char): |
3282 | ||
3283 | (1) relative composition: ESC 0 CHAR ... ESC 1 | |
3284 | (2) rulebase composition: ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1 | |
3285 | (3) altchar composition: ESC 3 ALT ... ALT ESC 0 CHAR ... ESC 1 | |
3286 | (4) alt&rule composition: ESC 4 ALT RULE ... ALT ESC 0 CHAR ... ESC 1 | |
3287 | ||
3288 | When the start sequence (ESC 0/2/3/4) is found, this annotation | |
3289 | header is produced. | |
3290 | ||
3291 | [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) 0 METHOD ] | |
3292 | ||
3293 | Then, upon reading CHAR or RULE (one or two bytes), these codes are | |
3294 | produced until the end sequence (ESC 1) is found: | |
3295 | ||
3296 | (1) CHAR ... CHAR | |
3297 | (2) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR | |
3298 | (3) ALT ... ALT -1 -1 CHAR ... CHAR | |
3299 | (4) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT -1 -1 CHAR ... CHAR | |
3300 | ||
3301 | When the end sequence (ESC 1) is found, LENGTH and NCHARS in the | |
3302 | annotation header is updated as below: | |
3303 | ||
3304 | (1) LENGTH: unchanged, NCHARS: number of CHARs | |
3305 | (2) LENGTH: unchanged, NCHARS: number of CHARs | |
3306 | (3) LENGTH: += number of ALTs + 2, NCHARS: number of CHARs | |
3307 | (4) LENGTH: += number of ALTs * 3, NCHARS: number of CHARs | |
3308 | ||
3309 | If an error is found while composing, the annotation header is | |
3310 | changed to: | |
3311 | ||
3312 | [ ESC '0'/'2'/'3'/'4' -2 0 ] | |
3313 | ||
3314 | and the sequence [ -2 DECODED-RULE ] is changed to the original | |
3315 | byte sequence as below: | |
3316 | o the original byte sequence is B: [ B -1 ] | |
3317 | o the original byte sequence is B1 B2: [ B1 B2 ] | |
3318 | and the sequence [ -1 -1 ] is changed to the original byte | |
3319 | sequence: | |
3320 | [ ESC '0' ] | |
3321 | */ | |
3322 | ||
3323 | /* Decode a composition rule C1 and maybe one more byte from the | |
66ebf983 | 3324 | source, and set RULE to the encoded composition rule. If the rule |
d5efd1d1 | 3325 | is invalid, goto invalid_code. */ |
e951386e | 3326 | |
66ebf983 | 3327 | #define DECODE_COMPOSITION_RULE(rule) \ |
e951386e KH |
3328 | do { \ |
3329 | rule = c1 - 32; \ | |
3330 | if (rule < 0) \ | |
d5efd1d1 | 3331 | goto invalid_code; \ |
e951386e KH |
3332 | if (rule < 81) /* old format (before ver.21) */ \ |
3333 | { \ | |
3334 | int gref = (rule) / 9; \ | |
3335 | int nref = (rule) % 9; \ | |
3336 | if (gref == 4) gref = 10; \ | |
3337 | if (nref == 4) nref = 10; \ | |
3338 | rule = COMPOSITION_ENCODE_RULE (gref, nref); \ | |
e951386e KH |
3339 | } \ |
3340 | else /* new format (after ver.21) */ \ | |
3341 | { \ | |
2735d060 | 3342 | int b; \ |
e951386e | 3343 | \ |
2735d060 | 3344 | ONE_MORE_BYTE (b); \ |
d5efd1d1 PE |
3345 | if (! COMPOSITION_ENCODE_RULE_VALID (rule - 81, b - 32)) \ |
3346 | goto invalid_code; \ | |
2735d060 | 3347 | rule = COMPOSITION_ENCODE_RULE (rule - 81, b - 32); \ |
d5efd1d1 | 3348 | rule += 0x100; /* Distinguish it from the old format. */ \ |
e951386e KH |
3349 | } \ |
3350 | } while (0) | |
3351 | ||
3352 | #define ENCODE_COMPOSITION_RULE(rule) \ | |
df7492f9 | 3353 | do { \ |
e951386e KH |
3354 | int gref = (rule % 0x100) / 12, nref = (rule % 0x100) % 12; \ |
3355 | \ | |
3356 | if (rule < 0x100) /* old format */ \ | |
df7492f9 | 3357 | { \ |
e951386e KH |
3358 | if (gref == 10) gref = 4; \ |
3359 | if (nref == 10) nref = 4; \ | |
3360 | charbuf[idx] = 32 + gref * 9 + nref; \ | |
3361 | charbuf[idx + 1] = -1; \ | |
3362 | new_chars++; \ | |
df7492f9 | 3363 | } \ |
e951386e | 3364 | else /* new format */ \ |
df7492f9 | 3365 | { \ |
e951386e KH |
3366 | charbuf[idx] = 32 + 81 + gref; \ |
3367 | charbuf[idx + 1] = 32 + nref; \ | |
3368 | new_chars += 2; \ | |
df7492f9 KH |
3369 | } \ |
3370 | } while (0) | |
3371 | ||
e951386e KH |
3372 | /* Finish the current composition as invalid. */ |
3373 | ||
e951386e | 3374 | static int |
971de7fb | 3375 | finish_composition (int *charbuf, struct composition_status *cmp_status) |
e951386e KH |
3376 | { |
3377 | int idx = - cmp_status->length; | |
3378 | int new_chars; | |
3379 | ||
3380 | /* Recover the original ESC sequence */ | |
3381 | charbuf[idx++] = ISO_CODE_ESC; | |
3382 | charbuf[idx++] = (cmp_status->method == COMPOSITION_RELATIVE ? '0' | |
3383 | : cmp_status->method == COMPOSITION_WITH_RULE ? '2' | |
3384 | : cmp_status->method == COMPOSITION_WITH_ALTCHARS ? '3' | |
3385 | /* cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS */ | |
3386 | : '4'); | |
3387 | charbuf[idx++] = -2; | |
3388 | charbuf[idx++] = 0; | |
3389 | charbuf[idx++] = -1; | |
3390 | new_chars = cmp_status->nchars; | |
3391 | if (cmp_status->method >= COMPOSITION_WITH_RULE) | |
3392 | for (; idx < 0; idx++) | |
3393 | { | |
3394 | int elt = charbuf[idx]; | |
3395 | ||
3396 | if (elt == -2) | |
3397 | { | |
3398 | ENCODE_COMPOSITION_RULE (charbuf[idx + 1]); | |
3399 | idx++; | |
3400 | } | |
3401 | else if (elt == -1) | |
3402 | { | |
3403 | charbuf[idx++] = ISO_CODE_ESC; | |
3404 | charbuf[idx] = '0'; | |
3405 | new_chars += 2; | |
3406 | } | |
3407 | } | |
3408 | cmp_status->state = COMPOSING_NO; | |
3409 | return new_chars; | |
3410 | } | |
3411 | ||
ad1746f5 | 3412 | /* If characters are under composition, finish the composition. */ |
e951386e KH |
3413 | #define MAYBE_FINISH_COMPOSITION() \ |
3414 | do { \ | |
3415 | if (cmp_status->state != COMPOSING_NO) \ | |
3416 | char_offset += finish_composition (charbuf, cmp_status); \ | |
3417 | } while (0) | |
d46c5b12 | 3418 | |
aa72b389 | 3419 | /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4. |
e951386e | 3420 | |
aa72b389 KH |
3421 | ESC 0 : relative composition : ESC 0 CHAR ... ESC 1 |
3422 | ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1 | |
df7492f9 KH |
3423 | ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1 |
3424 | ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1 | |
ec6d2bb8 | 3425 | |
e951386e KH |
3426 | Produce this annotation sequence now: |
3427 | ||
3428 | [ -LENGTH(==-4) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) METHOD ] | |
3429 | */ | |
3430 | ||
3431 | #define DECODE_COMPOSITION_START(c1) \ | |
3432 | do { \ | |
3433 | if (c1 == '0' \ | |
3434 | && ((cmp_status->state == COMPOSING_COMPONENT_CHAR \ | |
3435 | && cmp_status->method == COMPOSITION_WITH_ALTCHARS) \ | |
3436 | || (cmp_status->state == COMPOSING_COMPONENT_RULE \ | |
3437 | && cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS))) \ | |
3438 | { \ | |
3439 | *charbuf++ = -1; \ | |
3440 | *charbuf++= -1; \ | |
3441 | cmp_status->state = COMPOSING_CHAR; \ | |
3442 | cmp_status->length += 2; \ | |
3443 | } \ | |
3444 | else \ | |
3445 | { \ | |
3446 | MAYBE_FINISH_COMPOSITION (); \ | |
3447 | cmp_status->method = (c1 == '0' ? COMPOSITION_RELATIVE \ | |
3448 | : c1 == '2' ? COMPOSITION_WITH_RULE \ | |
3449 | : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \ | |
3450 | : COMPOSITION_WITH_RULE_ALTCHARS); \ | |
3451 | cmp_status->state \ | |
3452 | = (c1 <= '2' ? COMPOSING_CHAR : COMPOSING_COMPONENT_CHAR); \ | |
3453 | ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \ | |
3454 | cmp_status->length = MAX_ANNOTATION_LENGTH; \ | |
3455 | cmp_status->nchars = cmp_status->ncomps = 0; \ | |
3456 | coding->annotated = 1; \ | |
3457 | } \ | |
ec6d2bb8 KH |
3458 | } while (0) |
3459 | ||
ec6d2bb8 | 3460 | |
e951386e | 3461 | /* Handle composition end sequence ESC 1. */ |
df7492f9 KH |
3462 | |
3463 | #define DECODE_COMPOSITION_END() \ | |
ec6d2bb8 | 3464 | do { \ |
e951386e KH |
3465 | if (cmp_status->nchars == 0 \ |
3466 | || ((cmp_status->state == COMPOSING_CHAR) \ | |
3467 | == (cmp_status->method == COMPOSITION_WITH_RULE))) \ | |
ec6d2bb8 | 3468 | { \ |
e951386e KH |
3469 | MAYBE_FINISH_COMPOSITION (); \ |
3470 | goto invalid_code; \ | |
ec6d2bb8 | 3471 | } \ |
e951386e KH |
3472 | if (cmp_status->method == COMPOSITION_WITH_ALTCHARS) \ |
3473 | charbuf[- cmp_status->length] -= cmp_status->ncomps + 2; \ | |
3474 | else if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS) \ | |
3475 | charbuf[- cmp_status->length] -= cmp_status->ncomps * 3; \ | |
3476 | charbuf[- cmp_status->length + 2] = cmp_status->nchars; \ | |
3477 | char_offset += cmp_status->nchars; \ | |
3478 | cmp_status->state = COMPOSING_NO; \ | |
ec6d2bb8 KH |
3479 | } while (0) |
3480 | ||
e951386e | 3481 | /* Store a composition rule RULE in charbuf, and update cmp_status. */ |
df7492f9 | 3482 | |
e951386e KH |
3483 | #define STORE_COMPOSITION_RULE(rule) \ |
3484 | do { \ | |
3485 | *charbuf++ = -2; \ | |
3486 | *charbuf++ = rule; \ | |
3487 | cmp_status->length += 2; \ | |
3488 | cmp_status->state--; \ | |
3489 | } while (0) | |
ec6d2bb8 | 3490 | |
e951386e KH |
3491 | /* Store a composed char or a component char C in charbuf, and update |
3492 | cmp_status. */ | |
3493 | ||
3494 | #define STORE_COMPOSITION_CHAR(c) \ | |
ec6d2bb8 | 3495 | do { \ |
e951386e KH |
3496 | *charbuf++ = (c); \ |
3497 | cmp_status->length++; \ | |
3498 | if (cmp_status->state == COMPOSING_CHAR) \ | |
3499 | cmp_status->nchars++; \ | |
df7492f9 | 3500 | else \ |
e951386e KH |
3501 | cmp_status->ncomps++; \ |
3502 | if (cmp_status->method == COMPOSITION_WITH_RULE \ | |
3503 | || (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS \ | |
3504 | && cmp_status->state == COMPOSING_COMPONENT_CHAR)) \ | |
3505 | cmp_status->state++; \ | |
ec6d2bb8 | 3506 | } while (0) |
88993dfd | 3507 | |
d46c5b12 | 3508 | |
4ed46869 KH |
3509 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ |
3510 | ||
b73bfc1c | 3511 | static void |
971de7fb | 3512 | decode_coding_iso_2022 (struct coding_system *coding) |
4ed46869 | 3513 | { |
8f924df7 KH |
3514 | const unsigned char *src = coding->source + coding->consumed; |
3515 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
3516 | const unsigned char *src_base; | |
69a80ea3 | 3517 | int *charbuf = coding->charbuf + coding->charbuf_used; |
ad1746f5 KH |
3518 | /* We may produce two annotations (charset and composition) in one |
3519 | loop and one more charset annotation at the end. */ | |
ff0dacd7 | 3520 | int *charbuf_end |
df80c7f0 | 3521 | = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 3); |
d311d28c | 3522 | ptrdiff_t consumed_chars = 0, consumed_chars_base; |
f10fe38f | 3523 | bool multibytep = coding->src_multibyte; |
4ed46869 | 3524 | /* Charsets invoked to graphic plane 0 and 1 respectively. */ |
df7492f9 KH |
3525 | int charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); |
3526 | int charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1); | |
134b9549 | 3527 | int charset_id_2, charset_id_3; |
df7492f9 KH |
3528 | struct charset *charset; |
3529 | int c; | |
e951386e | 3530 | struct composition_status *cmp_status = CODING_ISO_CMP_STATUS (coding); |
66ebf983 | 3531 | Lisp_Object attrs = CODING_ID_ATTRS (coding->id); |
d311d28c PE |
3532 | ptrdiff_t char_offset = coding->produced_char; |
3533 | ptrdiff_t last_offset = char_offset; | |
ff0dacd7 | 3534 | int last_id = charset_ascii; |
f10fe38f PE |
3535 | bool eol_dos |
3536 | = !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); | |
119852e7 | 3537 | int byte_after_cr = -1; |
e951386e | 3538 | int i; |
df7492f9 | 3539 | |
df7492f9 | 3540 | setup_iso_safe_charsets (attrs); |
1b3b981b | 3541 | coding->safe_charsets = SDATA (CODING_ATTR_SAFE_CHARSETS (attrs)); |
b73bfc1c | 3542 | |
e951386e KH |
3543 | if (cmp_status->state != COMPOSING_NO) |
3544 | { | |
15cbd324 | 3545 | if (charbuf_end - charbuf < cmp_status->length) |
1088b922 | 3546 | emacs_abort (); |
e951386e KH |
3547 | for (i = 0; i < cmp_status->length; i++) |
3548 | *charbuf++ = cmp_status->carryover[i]; | |
3549 | coding->annotated = 1; | |
3550 | } | |
3551 | ||
b73bfc1c | 3552 | while (1) |
4ed46869 | 3553 | { |
cf299835 | 3554 | int c1, c2, c3; |
b73bfc1c KH |
3555 | |
3556 | src_base = src; | |
df7492f9 KH |
3557 | consumed_chars_base = consumed_chars; |
3558 | ||
3559 | if (charbuf >= charbuf_end) | |
b71f6f73 KH |
3560 | { |
3561 | if (byte_after_cr >= 0) | |
3562 | src_base--; | |
3563 | break; | |
3564 | } | |
df7492f9 | 3565 | |
119852e7 KH |
3566 | if (byte_after_cr >= 0) |
3567 | c1 = byte_after_cr, byte_after_cr = -1; | |
3568 | else | |
3569 | ONE_MORE_BYTE (c1); | |
065e3595 KH |
3570 | if (c1 < 0) |
3571 | goto invalid_code; | |
4ed46869 | 3572 | |
e951386e | 3573 | if (CODING_ISO_EXTSEGMENT_LEN (coding) > 0) |
4ed46869 | 3574 | { |
e951386e KH |
3575 | *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1); |
3576 | char_offset++; | |
3577 | CODING_ISO_EXTSEGMENT_LEN (coding)--; | |
3578 | continue; | |
3579 | } | |
3580 | ||
3581 | if (CODING_ISO_EMBEDDED_UTF_8 (coding)) | |
3582 | { | |
3583 | if (c1 == ISO_CODE_ESC) | |
ec6d2bb8 | 3584 | { |
e951386e KH |
3585 | if (src + 1 >= src_end) |
3586 | goto no_more_source; | |
3587 | *charbuf++ = ISO_CODE_ESC; | |
3588 | char_offset++; | |
3589 | if (src[0] == '%' && src[1] == '@') | |
df7492f9 | 3590 | { |
e951386e KH |
3591 | src += 2; |
3592 | consumed_chars += 2; | |
3593 | char_offset += 2; | |
3594 | /* We are sure charbuf can contain two more chars. */ | |
3595 | *charbuf++ = '%'; | |
3596 | *charbuf++ = '@'; | |
3597 | CODING_ISO_EMBEDDED_UTF_8 (coding) = 0; | |
df7492f9 | 3598 | } |
4ed46869 | 3599 | } |
e951386e KH |
3600 | else |
3601 | { | |
3602 | *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1); | |
3603 | char_offset++; | |
3604 | } | |
3605 | continue; | |
3606 | } | |
3607 | ||
3608 | if ((cmp_status->state == COMPOSING_RULE | |
3609 | || cmp_status->state == COMPOSING_COMPONENT_RULE) | |
3610 | && c1 != ISO_CODE_ESC) | |
3611 | { | |
66ebf983 | 3612 | int rule; |
e951386e | 3613 | |
66ebf983 | 3614 | DECODE_COMPOSITION_RULE (rule); |
e951386e KH |
3615 | STORE_COMPOSITION_RULE (rule); |
3616 | continue; | |
3617 | } | |
3618 | ||
3619 | /* We produce at most one character. */ | |
3620 | switch (iso_code_class [c1]) | |
3621 | { | |
3622 | case ISO_0x20_or_0x7F: | |
df7492f9 KH |
3623 | if (charset_id_0 < 0 |
3624 | || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0))) | |
781d7a48 KH |
3625 | /* This is SPACE or DEL. */ |
3626 | charset = CHARSET_FROM_ID (charset_ascii); | |
3627 | else | |
3628 | charset = CHARSET_FROM_ID (charset_id_0); | |
3629 | break; | |
4ed46869 KH |
3630 | |
3631 | case ISO_graphic_plane_0: | |
134b9549 KH |
3632 | if (charset_id_0 < 0) |
3633 | charset = CHARSET_FROM_ID (charset_ascii); | |
3634 | else | |
3635 | charset = CHARSET_FROM_ID (charset_id_0); | |
4ed46869 KH |
3636 | break; |
3637 | ||
3638 | case ISO_0xA0_or_0xFF: | |
df7492f9 KH |
3639 | if (charset_id_1 < 0 |
3640 | || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1)) | |
3641 | || CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) | |
3642 | goto invalid_code; | |
4ed46869 KH |
3643 | /* This is a graphic character, we fall down ... */ |
3644 | ||
3645 | case ISO_graphic_plane_1: | |
df7492f9 KH |
3646 | if (charset_id_1 < 0) |
3647 | goto invalid_code; | |
3648 | charset = CHARSET_FROM_ID (charset_id_1); | |
4ed46869 KH |
3649 | break; |
3650 | ||
df7492f9 | 3651 | case ISO_control_0: |
2735d060 | 3652 | if (eol_dos && c1 == '\r') |
119852e7 | 3653 | ONE_MORE_BYTE (byte_after_cr); |
df7492f9 KH |
3654 | MAYBE_FINISH_COMPOSITION (); |
3655 | charset = CHARSET_FROM_ID (charset_ascii); | |
4ed46869 KH |
3656 | break; |
3657 | ||
df7492f9 | 3658 | case ISO_control_1: |
df7492f9 KH |
3659 | goto invalid_code; |
3660 | ||
4ed46869 | 3661 | case ISO_shift_out: |
df7492f9 KH |
3662 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT) |
3663 | || CODING_ISO_DESIGNATION (coding, 1) < 0) | |
3664 | goto invalid_code; | |
3665 | CODING_ISO_INVOCATION (coding, 0) = 1; | |
3666 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3667 | continue; |
4ed46869 KH |
3668 | |
3669 | case ISO_shift_in: | |
df7492f9 KH |
3670 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT)) |
3671 | goto invalid_code; | |
3672 | CODING_ISO_INVOCATION (coding, 0) = 0; | |
3673 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3674 | continue; |
4ed46869 KH |
3675 | |
3676 | case ISO_single_shift_2_7: | |
a63dba42 KH |
3677 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS)) |
3678 | goto invalid_code; | |
4ed46869 | 3679 | case ISO_single_shift_2: |
df7492f9 KH |
3680 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)) |
3681 | goto invalid_code; | |
4ed46869 KH |
3682 | /* SS2 is handled as an escape sequence of ESC 'N' */ |
3683 | c1 = 'N'; | |
3684 | goto label_escape_sequence; | |
3685 | ||
3686 | case ISO_single_shift_3: | |
df7492f9 KH |
3687 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)) |
3688 | goto invalid_code; | |
4ed46869 KH |
3689 | /* SS2 is handled as an escape sequence of ESC 'O' */ |
3690 | c1 = 'O'; | |
3691 | goto label_escape_sequence; | |
3692 | ||
3693 | case ISO_control_sequence_introducer: | |
3694 | /* CSI is handled as an escape sequence of ESC '[' ... */ | |
3695 | c1 = '['; | |
3696 | goto label_escape_sequence; | |
3697 | ||
3698 | case ISO_escape: | |
3699 | ONE_MORE_BYTE (c1); | |
3700 | label_escape_sequence: | |
df7492f9 | 3701 | /* Escape sequences handled here are invocation, |
4ed46869 KH |
3702 | designation, direction specification, and character |
3703 | composition specification. */ | |
3704 | switch (c1) | |
3705 | { | |
3706 | case '&': /* revision of following character set */ | |
3707 | ONE_MORE_BYTE (c1); | |
3708 | if (!(c1 >= '@' && c1 <= '~')) | |
df7492f9 | 3709 | goto invalid_code; |
4ed46869 KH |
3710 | ONE_MORE_BYTE (c1); |
3711 | if (c1 != ISO_CODE_ESC) | |
df7492f9 | 3712 | goto invalid_code; |
4ed46869 KH |
3713 | ONE_MORE_BYTE (c1); |
3714 | goto label_escape_sequence; | |
3715 | ||
3716 | case '$': /* designation of 2-byte character set */ | |
df7492f9 KH |
3717 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION)) |
3718 | goto invalid_code; | |
134b9549 KH |
3719 | { |
3720 | int reg, chars96; | |
3721 | ||
3722 | ONE_MORE_BYTE (c1); | |
3723 | if (c1 >= '@' && c1 <= 'B') | |
3724 | { /* designation of JISX0208.1978, GB2312.1980, | |
88993dfd | 3725 | or JISX0208.1980 */ |
134b9549 KH |
3726 | reg = 0, chars96 = 0; |
3727 | } | |
3728 | else if (c1 >= 0x28 && c1 <= 0x2B) | |
3729 | { /* designation of DIMENSION2_CHARS94 character set */ | |
3730 | reg = c1 - 0x28, chars96 = 0; | |
3731 | ONE_MORE_BYTE (c1); | |
3732 | } | |
3733 | else if (c1 >= 0x2C && c1 <= 0x2F) | |
3734 | { /* designation of DIMENSION2_CHARS96 character set */ | |
3735 | reg = c1 - 0x2C, chars96 = 1; | |
3736 | ONE_MORE_BYTE (c1); | |
3737 | } | |
3738 | else | |
3739 | goto invalid_code; | |
3740 | DECODE_DESIGNATION (reg, 2, chars96, c1); | |
3741 | /* We must update these variables now. */ | |
3742 | if (reg == 0) | |
3743 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
3744 | else if (reg == 1) | |
3745 | charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1); | |
3746 | if (chars96 < 0) | |
3747 | goto invalid_code; | |
3748 | } | |
b73bfc1c | 3749 | continue; |
4ed46869 KH |
3750 | |
3751 | case 'n': /* invocation of locking-shift-2 */ | |
df7492f9 KH |
3752 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT) |
3753 | || CODING_ISO_DESIGNATION (coding, 2) < 0) | |
3754 | goto invalid_code; | |
3755 | CODING_ISO_INVOCATION (coding, 0) = 2; | |
3756 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3757 | continue; |
4ed46869 KH |
3758 | |
3759 | case 'o': /* invocation of locking-shift-3 */ | |
df7492f9 KH |
3760 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT) |
3761 | || CODING_ISO_DESIGNATION (coding, 3) < 0) | |
3762 | goto invalid_code; | |
3763 | CODING_ISO_INVOCATION (coding, 0) = 3; | |
3764 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3765 | continue; |
4ed46869 KH |
3766 | |
3767 | case 'N': /* invocation of single-shift-2 */ | |
df7492f9 KH |
3768 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
3769 | || CODING_ISO_DESIGNATION (coding, 2) < 0) | |
3770 | goto invalid_code; | |
134b9549 KH |
3771 | charset_id_2 = CODING_ISO_DESIGNATION (coding, 2); |
3772 | if (charset_id_2 < 0) | |
3773 | charset = CHARSET_FROM_ID (charset_ascii); | |
3774 | else | |
3775 | charset = CHARSET_FROM_ID (charset_id_2); | |
b73bfc1c | 3776 | ONE_MORE_BYTE (c1); |
99107004 | 3777 | if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0) |
0efe47a8 KH |
3778 | || (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) |
3779 | && ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LEVEL_4) | |
3780 | ? c1 >= 0x80 : c1 < 0x80))) | |
df7492f9 | 3781 | goto invalid_code; |
4ed46869 KH |
3782 | break; |
3783 | ||
3784 | case 'O': /* invocation of single-shift-3 */ | |
df7492f9 KH |
3785 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
3786 | || CODING_ISO_DESIGNATION (coding, 3) < 0) | |
3787 | goto invalid_code; | |
134b9549 KH |
3788 | charset_id_3 = CODING_ISO_DESIGNATION (coding, 3); |
3789 | if (charset_id_3 < 0) | |
3790 | charset = CHARSET_FROM_ID (charset_ascii); | |
3791 | else | |
3792 | charset = CHARSET_FROM_ID (charset_id_3); | |
b73bfc1c | 3793 | ONE_MORE_BYTE (c1); |
99107004 | 3794 | if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0) |
0efe47a8 KH |
3795 | || (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) |
3796 | && ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LEVEL_4) | |
3797 | ? c1 >= 0x80 : c1 < 0x80))) | |
df7492f9 | 3798 | goto invalid_code; |
4ed46869 KH |
3799 | break; |
3800 | ||
ec6d2bb8 | 3801 | case '0': case '2': case '3': case '4': /* start composition */ |
df7492f9 KH |
3802 | if (! (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK)) |
3803 | goto invalid_code; | |
e951386e KH |
3804 | if (last_id != charset_ascii) |
3805 | { | |
3806 | ADD_CHARSET_DATA (charbuf, char_offset- last_offset, last_id); | |
3807 | last_id = charset_ascii; | |
3808 | last_offset = char_offset; | |
3809 | } | |
ec6d2bb8 | 3810 | DECODE_COMPOSITION_START (c1); |
b73bfc1c | 3811 | continue; |
4ed46869 | 3812 | |
ec6d2bb8 | 3813 | case '1': /* end composition */ |
e951386e | 3814 | if (cmp_status->state == COMPOSING_NO) |
df7492f9 KH |
3815 | goto invalid_code; |
3816 | DECODE_COMPOSITION_END (); | |
b73bfc1c | 3817 | continue; |
4ed46869 KH |
3818 | |
3819 | case '[': /* specification of direction */ | |
de59072a | 3820 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DIRECTION)) |
df7492f9 | 3821 | goto invalid_code; |
4ed46869 | 3822 | /* For the moment, nested direction is not supported. |
d46c5b12 | 3823 | So, `coding->mode & CODING_MODE_DIRECTION' zero means |
ad1746f5 | 3824 | left-to-right, and nonzero means right-to-left. */ |
4ed46869 KH |
3825 | ONE_MORE_BYTE (c1); |
3826 | switch (c1) | |
3827 | { | |
3828 | case ']': /* end of the current direction */ | |
d46c5b12 | 3829 | coding->mode &= ~CODING_MODE_DIRECTION; |
4ed46869 KH |
3830 | |
3831 | case '0': /* end of the current direction */ | |
3832 | case '1': /* start of left-to-right direction */ | |
3833 | ONE_MORE_BYTE (c1); | |
3834 | if (c1 == ']') | |
d46c5b12 | 3835 | coding->mode &= ~CODING_MODE_DIRECTION; |
4ed46869 | 3836 | else |
df7492f9 | 3837 | goto invalid_code; |
4ed46869 KH |
3838 | break; |
3839 | ||
3840 | case '2': /* start of right-to-left direction */ | |
3841 | ONE_MORE_BYTE (c1); | |
3842 | if (c1 == ']') | |
d46c5b12 | 3843 | coding->mode |= CODING_MODE_DIRECTION; |
4ed46869 | 3844 | else |
df7492f9 | 3845 | goto invalid_code; |
4ed46869 KH |
3846 | break; |
3847 | ||
3848 | default: | |
df7492f9 | 3849 | goto invalid_code; |
4ed46869 | 3850 | } |
b73bfc1c | 3851 | continue; |
4ed46869 | 3852 | |
103e0180 | 3853 | case '%': |
103e0180 KH |
3854 | ONE_MORE_BYTE (c1); |
3855 | if (c1 == '/') | |
3856 | { | |
3857 | /* CTEXT extended segment: | |
3858 | ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES-- | |
3859 | We keep these bytes as is for the moment. | |
3860 | They may be decoded by post-read-conversion. */ | |
3861 | int dim, M, L; | |
4776e638 | 3862 | int size; |
8f924df7 | 3863 | |
103e0180 | 3864 | ONE_MORE_BYTE (dim); |
7a84eee5 | 3865 | if (dim < '0' || dim > '4') |
e951386e | 3866 | goto invalid_code; |
103e0180 | 3867 | ONE_MORE_BYTE (M); |
e951386e KH |
3868 | if (M < 128) |
3869 | goto invalid_code; | |
103e0180 | 3870 | ONE_MORE_BYTE (L); |
e951386e KH |
3871 | if (L < 128) |
3872 | goto invalid_code; | |
103e0180 | 3873 | size = ((M - 128) * 128) + (L - 128); |
e951386e | 3874 | if (charbuf + 6 > charbuf_end) |
4776e638 KH |
3875 | goto break_loop; |
3876 | *charbuf++ = ISO_CODE_ESC; | |
3877 | *charbuf++ = '%'; | |
3878 | *charbuf++ = '/'; | |
3879 | *charbuf++ = dim; | |
3880 | *charbuf++ = BYTE8_TO_CHAR (M); | |
3881 | *charbuf++ = BYTE8_TO_CHAR (L); | |
e951386e | 3882 | CODING_ISO_EXTSEGMENT_LEN (coding) = size; |
103e0180 KH |
3883 | } |
3884 | else if (c1 == 'G') | |
3885 | { | |
103e0180 KH |
3886 | /* XFree86 extension for embedding UTF-8 in CTEXT: |
3887 | ESC % G --UTF-8-BYTES-- ESC % @ | |
3888 | We keep these bytes as is for the moment. | |
3889 | They may be decoded by post-read-conversion. */ | |
e951386e | 3890 | if (charbuf + 3 > charbuf_end) |
4776e638 | 3891 | goto break_loop; |
e951386e KH |
3892 | *charbuf++ = ISO_CODE_ESC; |
3893 | *charbuf++ = '%'; | |
3894 | *charbuf++ = 'G'; | |
3895 | CODING_ISO_EMBEDDED_UTF_8 (coding) = 1; | |
103e0180 KH |
3896 | } |
3897 | else | |
4776e638 | 3898 | goto invalid_code; |
103e0180 | 3899 | continue; |
4776e638 | 3900 | break; |
103e0180 | 3901 | |
4ed46869 | 3902 | default: |
df7492f9 KH |
3903 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION)) |
3904 | goto invalid_code; | |
134b9549 KH |
3905 | { |
3906 | int reg, chars96; | |
3907 | ||
3908 | if (c1 >= 0x28 && c1 <= 0x2B) | |
3909 | { /* designation of DIMENSION1_CHARS94 character set */ | |
3910 | reg = c1 - 0x28, chars96 = 0; | |
3911 | ONE_MORE_BYTE (c1); | |
3912 | } | |
3913 | else if (c1 >= 0x2C && c1 <= 0x2F) | |
3914 | { /* designation of DIMENSION1_CHARS96 character set */ | |
3915 | reg = c1 - 0x2C, chars96 = 1; | |
3916 | ONE_MORE_BYTE (c1); | |
3917 | } | |
3918 | else | |
3919 | goto invalid_code; | |
3920 | DECODE_DESIGNATION (reg, 1, chars96, c1); | |
3921 | /* We must update these variables now. */ | |
3922 | if (reg == 0) | |
3923 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
3924 | else if (reg == 1) | |
3925 | charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1); | |
3926 | if (chars96 < 0) | |
3927 | goto invalid_code; | |
3928 | } | |
b73bfc1c | 3929 | continue; |
4ed46869 | 3930 | } |
413bb2db PE |
3931 | break; |
3932 | ||
3933 | default: | |
1088b922 | 3934 | emacs_abort (); |
b73bfc1c | 3935 | } |
4ed46869 | 3936 | |
e951386e KH |
3937 | if (cmp_status->state == COMPOSING_NO |
3938 | && charset->id != charset_ascii | |
ff0dacd7 KH |
3939 | && last_id != charset->id) |
3940 | { | |
3941 | if (last_id != charset_ascii) | |
69a80ea3 | 3942 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
ff0dacd7 KH |
3943 | last_id = charset->id; |
3944 | last_offset = char_offset; | |
3945 | } | |
3946 | ||
b73bfc1c | 3947 | /* Now we know CHARSET and 1st position code C1 of a character. |
cf299835 KH |
3948 | Produce a decoded character while getting 2nd and 3rd |
3949 | position codes C2, C3 if necessary. */ | |
df7492f9 | 3950 | if (CHARSET_DIMENSION (charset) > 1) |
b73bfc1c KH |
3951 | { |
3952 | ONE_MORE_BYTE (c2); | |
cf299835 KH |
3953 | if (c2 < 0x20 || (c2 >= 0x80 && c2 < 0xA0) |
3954 | || ((c1 & 0x80) != (c2 & 0x80))) | |
b73bfc1c | 3955 | /* C2 is not in a valid range. */ |
df7492f9 | 3956 | goto invalid_code; |
cf299835 KH |
3957 | if (CHARSET_DIMENSION (charset) == 2) |
3958 | c1 = (c1 << 8) | c2; | |
3959 | else | |
df7492f9 | 3960 | { |
cf299835 KH |
3961 | ONE_MORE_BYTE (c3); |
3962 | if (c3 < 0x20 || (c3 >= 0x80 && c3 < 0xA0) | |
3963 | || ((c1 & 0x80) != (c3 & 0x80))) | |
3964 | /* C3 is not in a valid range. */ | |
df7492f9 | 3965 | goto invalid_code; |
cf299835 | 3966 | c1 = (c1 << 16) | (c2 << 8) | c2; |
df7492f9 KH |
3967 | } |
3968 | } | |
cf299835 | 3969 | c1 &= 0x7F7F7F; |
df7492f9 KH |
3970 | CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c1, c); |
3971 | if (c < 0) | |
3972 | { | |
3973 | MAYBE_FINISH_COMPOSITION (); | |
3974 | for (; src_base < src; src_base++, char_offset++) | |
3975 | { | |
3976 | if (ASCII_BYTE_P (*src_base)) | |
3977 | *charbuf++ = *src_base; | |
3978 | else | |
3979 | *charbuf++ = BYTE8_TO_CHAR (*src_base); | |
3980 | } | |
3981 | } | |
e951386e | 3982 | else if (cmp_status->state == COMPOSING_NO) |
df7492f9 KH |
3983 | { |
3984 | *charbuf++ = c; | |
3985 | char_offset++; | |
4ed46869 | 3986 | } |
e951386e KH |
3987 | else if ((cmp_status->state == COMPOSING_CHAR |
3988 | ? cmp_status->nchars | |
3989 | : cmp_status->ncomps) | |
3990 | >= MAX_COMPOSITION_COMPONENTS) | |
781d7a48 | 3991 | { |
e951386e KH |
3992 | /* Too long composition. */ |
3993 | MAYBE_FINISH_COMPOSITION (); | |
3994 | *charbuf++ = c; | |
3995 | char_offset++; | |
4ed46869 | 3996 | } |
e951386e KH |
3997 | else |
3998 | STORE_COMPOSITION_CHAR (c); | |
4ed46869 KH |
3999 | continue; |
4000 | ||
df7492f9 KH |
4001 | invalid_code: |
4002 | MAYBE_FINISH_COMPOSITION (); | |
4ed46869 | 4003 | src = src_base; |
df7492f9 KH |
4004 | consumed_chars = consumed_chars_base; |
4005 | ONE_MORE_BYTE (c); | |
065e3595 | 4006 | *charbuf++ = c < 0 ? -c : ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 4007 | char_offset++; |
df7492f9 | 4008 | coding->errors++; |
ec509856 KH |
4009 | /* Reset the invocation and designation status to the safest |
4010 | one; i.e. designate ASCII to the graphic register 0, and | |
4011 | invoke that register to the graphic plane 0. This typically | |
4012 | helps the case that an designation sequence for ASCII "ESC ( | |
4013 | B" is somehow broken (e.g. broken by a newline). */ | |
4014 | CODING_ISO_INVOCATION (coding, 0) = 0; | |
4015 | CODING_ISO_DESIGNATION (coding, 0) = charset_ascii; | |
4016 | charset_id_0 = charset_ascii; | |
4776e638 KH |
4017 | continue; |
4018 | ||
4019 | break_loop: | |
4020 | break; | |
4ed46869 | 4021 | } |
fb88bf2d | 4022 | |
df7492f9 | 4023 | no_more_source: |
e951386e KH |
4024 | if (cmp_status->state != COMPOSING_NO) |
4025 | { | |
4026 | if (coding->mode & CODING_MODE_LAST_BLOCK) | |
4027 | MAYBE_FINISH_COMPOSITION (); | |
4028 | else | |
4029 | { | |
4030 | charbuf -= cmp_status->length; | |
4031 | for (i = 0; i < cmp_status->length; i++) | |
4032 | cmp_status->carryover[i] = charbuf[i]; | |
4033 | } | |
4034 | } | |
4035 | else if (last_id != charset_ascii) | |
69a80ea3 | 4036 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
4037 | coding->consumed_char += consumed_chars_base; |
4038 | coding->consumed = src_base - coding->source; | |
4039 | coding->charbuf_used = charbuf - coding->charbuf; | |
4ed46869 KH |
4040 | } |
4041 | ||
b73bfc1c | 4042 | |
f4dee582 | 4043 | /* ISO2022 encoding stuff. */ |
4ed46869 KH |
4044 | |
4045 | /* | |
f4dee582 | 4046 | It is not enough to say just "ISO2022" on encoding, we have to |
df7492f9 | 4047 | specify more details. In Emacs, each coding system of ISO2022 |
4ed46869 | 4048 | variant has the following specifications: |
df7492f9 | 4049 | 1. Initial designation to G0 thru G3. |
4ed46869 KH |
4050 | 2. Allows short-form designation? |
4051 | 3. ASCII should be designated to G0 before control characters? | |
4052 | 4. ASCII should be designated to G0 at end of line? | |
4053 | 5. 7-bit environment or 8-bit environment? | |
4054 | 6. Use locking-shift? | |
4055 | 7. Use Single-shift? | |
4056 | And the following two are only for Japanese: | |
4057 | 8. Use ASCII in place of JIS0201-1976-Roman? | |
4058 | 9. Use JISX0208-1983 in place of JISX0208-1978? | |
df7492f9 KH |
4059 | These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits |
4060 | defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more | |
f4dee582 | 4061 | details. |
4ed46869 KH |
4062 | */ |
4063 | ||
4064 | /* Produce codes (escape sequence) for designating CHARSET to graphic | |
b73bfc1c KH |
4065 | register REG at DST, and increment DST. If <final-char> of CHARSET is |
4066 | '@', 'A', or 'B' and the coding system CODING allows, produce | |
4067 | designation sequence of short-form. */ | |
4ed46869 KH |
4068 | |
4069 | #define ENCODE_DESIGNATION(charset, reg, coding) \ | |
4070 | do { \ | |
df7492f9 | 4071 | unsigned char final_char = CHARSET_ISO_FINAL (charset); \ |
675e2c69 DN |
4072 | const char *intermediate_char_94 = "()*+"; \ |
4073 | const char *intermediate_char_96 = ",-./"; \ | |
df7492f9 | 4074 | int revision = -1; \ |
df7492f9 KH |
4075 | \ |
4076 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \ | |
c197f191 | 4077 | revision = CHARSET_ISO_REVISION (charset); \ |
df7492f9 KH |
4078 | \ |
4079 | if (revision >= 0) \ | |
70c22245 | 4080 | { \ |
df7492f9 KH |
4081 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \ |
4082 | EMIT_ONE_BYTE ('@' + revision); \ | |
4ed46869 | 4083 | } \ |
df7492f9 | 4084 | EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \ |
4ed46869 KH |
4085 | if (CHARSET_DIMENSION (charset) == 1) \ |
4086 | { \ | |
2735d060 | 4087 | int b; \ |
df7492f9 | 4088 | if (! CHARSET_ISO_CHARS_96 (charset)) \ |
2735d060 | 4089 | b = intermediate_char_94[reg]; \ |
4ed46869 | 4090 | else \ |
2735d060 PE |
4091 | b = intermediate_char_96[reg]; \ |
4092 | EMIT_ONE_ASCII_BYTE (b); \ | |
4ed46869 KH |
4093 | } \ |
4094 | else \ | |
4095 | { \ | |
df7492f9 KH |
4096 | EMIT_ONE_ASCII_BYTE ('$'); \ |
4097 | if (! CHARSET_ISO_CHARS_96 (charset)) \ | |
4ed46869 | 4098 | { \ |
df7492f9 | 4099 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \ |
b73bfc1c KH |
4100 | || reg != 0 \ |
4101 | || final_char < '@' || final_char > 'B') \ | |
df7492f9 | 4102 | EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \ |
4ed46869 KH |
4103 | } \ |
4104 | else \ | |
df7492f9 | 4105 | EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \ |
4ed46869 | 4106 | } \ |
df7492f9 KH |
4107 | EMIT_ONE_ASCII_BYTE (final_char); \ |
4108 | \ | |
4109 | CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \ | |
4ed46869 KH |
4110 | } while (0) |
4111 | ||
df7492f9 | 4112 | |
4ed46869 KH |
4113 | /* The following two macros produce codes (control character or escape |
4114 | sequence) for ISO2022 single-shift functions (single-shift-2 and | |
4115 | single-shift-3). */ | |
4116 | ||
df7492f9 KH |
4117 | #define ENCODE_SINGLE_SHIFT_2 \ |
4118 | do { \ | |
4119 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ | |
4120 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \ | |
4121 | else \ | |
4122 | EMIT_ONE_BYTE (ISO_CODE_SS2); \ | |
4123 | CODING_ISO_SINGLE_SHIFTING (coding) = 1; \ | |
4ed46869 KH |
4124 | } while (0) |
4125 | ||
df7492f9 KH |
4126 | |
4127 | #define ENCODE_SINGLE_SHIFT_3 \ | |
4128 | do { \ | |
4129 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ | |
4130 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \ | |
4131 | else \ | |
4132 | EMIT_ONE_BYTE (ISO_CODE_SS3); \ | |
4133 | CODING_ISO_SINGLE_SHIFTING (coding) = 1; \ | |
4ed46869 KH |
4134 | } while (0) |
4135 | ||
df7492f9 | 4136 | |
4ed46869 KH |
4137 | /* The following four macros produce codes (control character or |
4138 | escape sequence) for ISO2022 locking-shift functions (shift-in, | |
4139 | shift-out, locking-shift-2, and locking-shift-3). */ | |
4140 | ||
df7492f9 KH |
4141 | #define ENCODE_SHIFT_IN \ |
4142 | do { \ | |
4143 | EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \ | |
4144 | CODING_ISO_INVOCATION (coding, 0) = 0; \ | |
4ed46869 KH |
4145 | } while (0) |
4146 | ||
df7492f9 KH |
4147 | |
4148 | #define ENCODE_SHIFT_OUT \ | |
4149 | do { \ | |
4150 | EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \ | |
4151 | CODING_ISO_INVOCATION (coding, 0) = 1; \ | |
4ed46869 KH |
4152 | } while (0) |
4153 | ||
df7492f9 KH |
4154 | |
4155 | #define ENCODE_LOCKING_SHIFT_2 \ | |
4156 | do { \ | |
4157 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \ | |
4158 | CODING_ISO_INVOCATION (coding, 0) = 2; \ | |
4ed46869 KH |
4159 | } while (0) |
4160 | ||
df7492f9 KH |
4161 | |
4162 | #define ENCODE_LOCKING_SHIFT_3 \ | |
4163 | do { \ | |
4164 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \ | |
4165 | CODING_ISO_INVOCATION (coding, 0) = 3; \ | |
4ed46869 KH |
4166 | } while (0) |
4167 | ||
df7492f9 | 4168 | |
f4dee582 RS |
4169 | /* Produce codes for a DIMENSION1 character whose character set is |
4170 | CHARSET and whose position-code is C1. Designation and invocation | |
4ed46869 KH |
4171 | sequences are also produced in advance if necessary. */ |
4172 | ||
6e85d753 KH |
4173 | #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \ |
4174 | do { \ | |
df7492f9 | 4175 | int id = CHARSET_ID (charset); \ |
bf16eb23 KH |
4176 | \ |
4177 | if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \ | |
4178 | && id == charset_ascii) \ | |
4179 | { \ | |
4180 | id = charset_jisx0201_roman; \ | |
4181 | charset = CHARSET_FROM_ID (id); \ | |
4182 | } \ | |
4183 | \ | |
df7492f9 | 4184 | if (CODING_ISO_SINGLE_SHIFTING (coding)) \ |
6e85d753 | 4185 | { \ |
df7492f9 KH |
4186 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ |
4187 | EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \ | |
6e85d753 | 4188 | else \ |
df7492f9 KH |
4189 | EMIT_ONE_BYTE (c1 | 0x80); \ |
4190 | CODING_ISO_SINGLE_SHIFTING (coding) = 0; \ | |
6e85d753 KH |
4191 | break; \ |
4192 | } \ | |
df7492f9 | 4193 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \ |
6e85d753 | 4194 | { \ |
df7492f9 | 4195 | EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \ |
6e85d753 KH |
4196 | break; \ |
4197 | } \ | |
df7492f9 | 4198 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \ |
6e85d753 | 4199 | { \ |
df7492f9 | 4200 | EMIT_ONE_BYTE (c1 | 0x80); \ |
6e85d753 KH |
4201 | break; \ |
4202 | } \ | |
6e85d753 KH |
4203 | else \ |
4204 | /* Since CHARSET is not yet invoked to any graphic planes, we \ | |
4205 | must invoke it, or, at first, designate it to some graphic \ | |
4206 | register. Then repeat the loop to actually produce the \ | |
4207 | character. */ \ | |
df7492f9 KH |
4208 | dst = encode_invocation_designation (charset, coding, dst, \ |
4209 | &produced_chars); \ | |
4ed46869 KH |
4210 | } while (1) |
4211 | ||
df7492f9 | 4212 | |
f4dee582 RS |
4213 | /* Produce codes for a DIMENSION2 character whose character set is |
4214 | CHARSET and whose position-codes are C1 and C2. Designation and | |
4ed46869 KH |
4215 | invocation codes are also produced in advance if necessary. */ |
4216 | ||
6e85d753 KH |
4217 | #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \ |
4218 | do { \ | |
df7492f9 | 4219 | int id = CHARSET_ID (charset); \ |
bf16eb23 KH |
4220 | \ |
4221 | if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \ | |
4222 | && id == charset_jisx0208) \ | |
4223 | { \ | |
4224 | id = charset_jisx0208_1978; \ | |
4225 | charset = CHARSET_FROM_ID (id); \ | |
4226 | } \ | |
4227 | \ | |
df7492f9 | 4228 | if (CODING_ISO_SINGLE_SHIFTING (coding)) \ |
6e85d753 | 4229 | { \ |
df7492f9 KH |
4230 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ |
4231 | EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \ | |
6e85d753 | 4232 | else \ |
df7492f9 KH |
4233 | EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \ |
4234 | CODING_ISO_SINGLE_SHIFTING (coding) = 0; \ | |
6e85d753 KH |
4235 | break; \ |
4236 | } \ | |
df7492f9 | 4237 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \ |
6e85d753 | 4238 | { \ |
df7492f9 | 4239 | EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \ |
6e85d753 KH |
4240 | break; \ |
4241 | } \ | |
df7492f9 | 4242 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \ |
6e85d753 | 4243 | { \ |
df7492f9 | 4244 | EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \ |
6e85d753 KH |
4245 | break; \ |
4246 | } \ | |
6e85d753 KH |
4247 | else \ |
4248 | /* Since CHARSET is not yet invoked to any graphic planes, we \ | |
4249 | must invoke it, or, at first, designate it to some graphic \ | |
4250 | register. Then repeat the loop to actually produce the \ | |
4251 | character. */ \ | |
df7492f9 KH |
4252 | dst = encode_invocation_designation (charset, coding, dst, \ |
4253 | &produced_chars); \ | |
4ed46869 KH |
4254 | } while (1) |
4255 | ||
05e6f5dc | 4256 | |
df7492f9 KH |
4257 | #define ENCODE_ISO_CHARACTER(charset, c) \ |
4258 | do { \ | |
8f50130c | 4259 | unsigned code; \ |
5eb05ea3 | 4260 | CODING_ENCODE_CHAR (coding, dst, dst_end, (charset), (c), code); \ |
df7492f9 KH |
4261 | \ |
4262 | if (CHARSET_DIMENSION (charset) == 1) \ | |
4263 | ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \ | |
4264 | else \ | |
4265 | ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \ | |
84fbb8a0 | 4266 | } while (0) |
bdd9fb48 | 4267 | |
05e6f5dc | 4268 | |
4ed46869 | 4269 | /* Produce designation and invocation codes at a place pointed by DST |
df7492f9 | 4270 | to use CHARSET. The element `spec.iso_2022' of *CODING is updated. |
4ed46869 KH |
4271 | Return new DST. */ |
4272 | ||
e2f1bab9 | 4273 | static unsigned char * |
cf84bb53 JB |
4274 | encode_invocation_designation (struct charset *charset, |
4275 | struct coding_system *coding, | |
d311d28c | 4276 | unsigned char *dst, ptrdiff_t *p_nchars) |
4ed46869 | 4277 | { |
f10fe38f | 4278 | bool multibytep = coding->dst_multibyte; |
d311d28c | 4279 | ptrdiff_t produced_chars = *p_nchars; |
4ed46869 | 4280 | int reg; /* graphic register number */ |
df7492f9 | 4281 | int id = CHARSET_ID (charset); |
4ed46869 KH |
4282 | |
4283 | /* At first, check designations. */ | |
4284 | for (reg = 0; reg < 4; reg++) | |
df7492f9 | 4285 | if (id == CODING_ISO_DESIGNATION (coding, reg)) |
4ed46869 KH |
4286 | break; |
4287 | ||
4288 | if (reg >= 4) | |
4289 | { | |
4290 | /* CHARSET is not yet designated to any graphic registers. */ | |
4291 | /* At first check the requested designation. */ | |
df7492f9 KH |
4292 | reg = CODING_ISO_REQUEST (coding, id); |
4293 | if (reg < 0) | |
1ba9e4ab KH |
4294 | /* Since CHARSET requests no special designation, designate it |
4295 | to graphic register 0. */ | |
4ed46869 KH |
4296 | reg = 0; |
4297 | ||
4298 | ENCODE_DESIGNATION (charset, reg, coding); | |
4299 | } | |
4300 | ||
df7492f9 KH |
4301 | if (CODING_ISO_INVOCATION (coding, 0) != reg |
4302 | && CODING_ISO_INVOCATION (coding, 1) != reg) | |
4ed46869 KH |
4303 | { |
4304 | /* Since the graphic register REG is not invoked to any graphic | |
4305 | planes, invoke it to graphic plane 0. */ | |
4306 | switch (reg) | |
4307 | { | |
4308 | case 0: /* graphic register 0 */ | |
4309 | ENCODE_SHIFT_IN; | |
4310 | break; | |
4311 | ||
4312 | case 1: /* graphic register 1 */ | |
4313 | ENCODE_SHIFT_OUT; | |
4314 | break; | |
4315 | ||
4316 | case 2: /* graphic register 2 */ | |
df7492f9 | 4317 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
4ed46869 KH |
4318 | ENCODE_SINGLE_SHIFT_2; |
4319 | else | |
4320 | ENCODE_LOCKING_SHIFT_2; | |
4321 | break; | |
4322 | ||
4323 | case 3: /* graphic register 3 */ | |
df7492f9 | 4324 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
4ed46869 KH |
4325 | ENCODE_SINGLE_SHIFT_3; |
4326 | else | |
4327 | ENCODE_LOCKING_SHIFT_3; | |
4328 | break; | |
4329 | } | |
4330 | } | |
b73bfc1c | 4331 | |
df7492f9 | 4332 | *p_nchars = produced_chars; |
4ed46869 KH |
4333 | return dst; |
4334 | } | |
4335 | ||
4ed46869 KH |
4336 | |
4337 | /* Produce codes for designation and invocation to reset the graphic | |
4338 | planes and registers to initial state. */ | |
df7492f9 KH |
4339 | #define ENCODE_RESET_PLANE_AND_REGISTER() \ |
4340 | do { \ | |
4341 | int reg; \ | |
4342 | struct charset *charset; \ | |
4343 | \ | |
4344 | if (CODING_ISO_INVOCATION (coding, 0) != 0) \ | |
4345 | ENCODE_SHIFT_IN; \ | |
4346 | for (reg = 0; reg < 4; reg++) \ | |
4347 | if (CODING_ISO_INITIAL (coding, reg) >= 0 \ | |
4348 | && (CODING_ISO_DESIGNATION (coding, reg) \ | |
4349 | != CODING_ISO_INITIAL (coding, reg))) \ | |
4350 | { \ | |
4351 | charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \ | |
4352 | ENCODE_DESIGNATION (charset, reg, coding); \ | |
4353 | } \ | |
4ed46869 KH |
4354 | } while (0) |
4355 | ||
df7492f9 | 4356 | |
bdd9fb48 | 4357 | /* Produce designation sequences of charsets in the line started from |
5eb05ea3 KH |
4358 | CHARBUF to a place pointed by DST, and return the number of |
4359 | produced bytes. DST should not directly point a buffer text area | |
4360 | which may be relocated by char_charset call. | |
bdd9fb48 KH |
4361 | |
4362 | If the current block ends before any end-of-line, we may fail to | |
d46c5b12 KH |
4363 | find all the necessary designations. */ |
4364 | ||
6e6c82a4 | 4365 | static ptrdiff_t |
5eb05ea3 KH |
4366 | encode_designation_at_bol (struct coding_system *coding, |
4367 | int *charbuf, int *charbuf_end, | |
461c2ab9 | 4368 | unsigned char *dst) |
e0e989f6 | 4369 | { |
75a3b399 | 4370 | unsigned char *orig = dst; |
df7492f9 | 4371 | struct charset *charset; |
bdd9fb48 KH |
4372 | /* Table of charsets to be designated to each graphic register. */ |
4373 | int r[4]; | |
df7492f9 | 4374 | int c, found = 0, reg; |
d311d28c | 4375 | ptrdiff_t produced_chars = 0; |
f10fe38f | 4376 | bool multibytep = coding->dst_multibyte; |
df7492f9 KH |
4377 | Lisp_Object attrs; |
4378 | Lisp_Object charset_list; | |
4379 | ||
4380 | attrs = CODING_ID_ATTRS (coding->id); | |
4381 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
4382 | if (EQ (charset_list, Qiso_2022)) | |
4383 | charset_list = Viso_2022_charset_list; | |
bdd9fb48 KH |
4384 | |
4385 | for (reg = 0; reg < 4; reg++) | |
4386 | r[reg] = -1; | |
4387 | ||
5eb05ea3 | 4388 | while (charbuf < charbuf_end && found < 4) |
e0e989f6 | 4389 | { |
df7492f9 KH |
4390 | int id; |
4391 | ||
4392 | c = *charbuf++; | |
b73bfc1c KH |
4393 | if (c == '\n') |
4394 | break; | |
df7492f9 KH |
4395 | charset = char_charset (c, charset_list, NULL); |
4396 | id = CHARSET_ID (charset); | |
4397 | reg = CODING_ISO_REQUEST (coding, id); | |
4398 | if (reg >= 0 && r[reg] < 0) | |
bdd9fb48 KH |
4399 | { |
4400 | found++; | |
df7492f9 | 4401 | r[reg] = id; |
bdd9fb48 | 4402 | } |
bdd9fb48 KH |
4403 | } |
4404 | ||
4405 | if (found) | |
4406 | { | |
4407 | for (reg = 0; reg < 4; reg++) | |
4408 | if (r[reg] >= 0 | |
df7492f9 KH |
4409 | && CODING_ISO_DESIGNATION (coding, reg) != r[reg]) |
4410 | ENCODE_DESIGNATION (CHARSET_FROM_ID (r[reg]), reg, coding); | |
e0e989f6 | 4411 | } |
b73bfc1c | 4412 | |
5eb05ea3 | 4413 | return dst - orig; |
e0e989f6 KH |
4414 | } |
4415 | ||
4ed46869 KH |
4416 | /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */ |
4417 | ||
f10fe38f | 4418 | static bool |
971de7fb | 4419 | encode_coding_iso_2022 (struct coding_system *coding) |
4ed46869 | 4420 | { |
f10fe38f | 4421 | bool multibytep = coding->dst_multibyte; |
df7492f9 KH |
4422 | int *charbuf = coding->charbuf; |
4423 | int *charbuf_end = charbuf + coding->charbuf_used; | |
4424 | unsigned char *dst = coding->destination + coding->produced; | |
4425 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
4426 | int safe_room = 16; | |
f10fe38f | 4427 | bool bol_designation |
df7492f9 KH |
4428 | = (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATE_AT_BOL |
4429 | && CODING_ISO_BOL (coding)); | |
d311d28c | 4430 | ptrdiff_t produced_chars = 0; |
df7492f9 | 4431 | Lisp_Object attrs, eol_type, charset_list; |
f10fe38f | 4432 | bool ascii_compatible; |
b73bfc1c | 4433 | int c; |
ff0dacd7 | 4434 | int preferred_charset_id = -1; |
05e6f5dc | 4435 | |
24a73b0a | 4436 | CODING_GET_INFO (coding, attrs, charset_list); |
0a9564cb | 4437 | eol_type = inhibit_eol_conversion ? Qunix : CODING_ID_EOL_TYPE (coding->id); |
24a73b0a KH |
4438 | if (VECTORP (eol_type)) |
4439 | eol_type = Qunix; | |
4440 | ||
004068e4 | 4441 | setup_iso_safe_charsets (attrs); |
ff0dacd7 | 4442 | /* Charset list may have been changed. */ |
287c57d7 | 4443 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); |
1b3b981b | 4444 | coding->safe_charsets = SDATA (CODING_ATTR_SAFE_CHARSETS (attrs)); |
0eecad43 | 4445 | |
a552b35a KH |
4446 | ascii_compatible |
4447 | = (! NILP (CODING_ATTR_ASCII_COMPAT (attrs)) | |
4448 | && ! (CODING_ISO_FLAGS (coding) & (CODING_ISO_FLAG_DESIGNATION | |
4449 | | CODING_ISO_FLAG_LOCKING_SHIFT))); | |
bdd9fb48 | 4450 | |
df7492f9 | 4451 | while (charbuf < charbuf_end) |
4ed46869 | 4452 | { |
df7492f9 | 4453 | ASSURE_DESTINATION (safe_room); |
b73bfc1c | 4454 | |
df7492f9 | 4455 | if (bol_designation) |
b73bfc1c | 4456 | { |
bdd9fb48 | 4457 | /* We have to produce designation sequences if any now. */ |
5eb05ea3 KH |
4458 | unsigned char desig_buf[16]; |
4459 | int nbytes; | |
8f50130c | 4460 | ptrdiff_t offset; |
5eb05ea3 KH |
4461 | |
4462 | charset_map_loaded = 0; | |
4463 | nbytes = encode_designation_at_bol (coding, charbuf, charbuf_end, | |
4464 | desig_buf); | |
4465 | if (charset_map_loaded | |
c1892f11 | 4466 | && (offset = coding_change_destination (coding))) |
5eb05ea3 KH |
4467 | { |
4468 | dst += offset; | |
4469 | dst_end += offset; | |
4470 | } | |
4471 | memcpy (dst, desig_buf, nbytes); | |
4472 | dst += nbytes; | |
df7492f9 | 4473 | /* We are sure that designation sequences are all ASCII bytes. */ |
5eb05ea3 KH |
4474 | produced_chars += nbytes; |
4475 | bol_designation = 0; | |
4476 | ASSURE_DESTINATION (safe_room); | |
e0e989f6 KH |
4477 | } |
4478 | ||
df7492f9 | 4479 | c = *charbuf++; |
ec6d2bb8 | 4480 | |
ff0dacd7 KH |
4481 | if (c < 0) |
4482 | { | |
4483 | /* Handle an annotation. */ | |
4484 | switch (*charbuf) | |
ec6d2bb8 | 4485 | { |
ff0dacd7 KH |
4486 | case CODING_ANNOTATE_COMPOSITION_MASK: |
4487 | /* Not yet implemented. */ | |
4488 | break; | |
4489 | case CODING_ANNOTATE_CHARSET_MASK: | |
cf7dfdf5 | 4490 | preferred_charset_id = charbuf[2]; |
ff0dacd7 KH |
4491 | if (preferred_charset_id >= 0 |
4492 | && NILP (Fmemq (make_number (preferred_charset_id), | |
4493 | charset_list))) | |
4494 | preferred_charset_id = -1; | |
4495 | break; | |
4496 | default: | |
1088b922 | 4497 | emacs_abort (); |
4ed46869 | 4498 | } |
ff0dacd7 KH |
4499 | charbuf += -c - 1; |
4500 | continue; | |
4ed46869 | 4501 | } |
ec6d2bb8 | 4502 | |
b73bfc1c KH |
4503 | /* Now encode the character C. */ |
4504 | if (c < 0x20 || c == 0x7F) | |
4505 | { | |
df7492f9 KH |
4506 | if (c == '\n' |
4507 | || (c == '\r' && EQ (eol_type, Qmac))) | |
19a8d9e0 | 4508 | { |
df7492f9 KH |
4509 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL) |
4510 | ENCODE_RESET_PLANE_AND_REGISTER (); | |
4511 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_INIT_AT_BOL) | |
b73bfc1c | 4512 | { |
df7492f9 KH |
4513 | int i; |
4514 | ||
4515 | for (i = 0; i < 4; i++) | |
4516 | CODING_ISO_DESIGNATION (coding, i) | |
4517 | = CODING_ISO_INITIAL (coding, i); | |
b73bfc1c | 4518 | } |
f10fe38f PE |
4519 | bol_designation = ((CODING_ISO_FLAGS (coding) |
4520 | & CODING_ISO_FLAG_DESIGNATE_AT_BOL) | |
4521 | != 0); | |
19a8d9e0 | 4522 | } |
df7492f9 KH |
4523 | else if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_CNTL) |
4524 | ENCODE_RESET_PLANE_AND_REGISTER (); | |
4525 | EMIT_ONE_ASCII_BYTE (c); | |
4ed46869 | 4526 | } |
df7492f9 | 4527 | else if (ASCII_CHAR_P (c)) |
88993dfd | 4528 | { |
df7492f9 KH |
4529 | if (ascii_compatible) |
4530 | EMIT_ONE_ASCII_BYTE (c); | |
93dec019 | 4531 | else |
19a8d9e0 | 4532 | { |
bf16eb23 KH |
4533 | struct charset *charset = CHARSET_FROM_ID (charset_ascii); |
4534 | ENCODE_ISO_CHARACTER (charset, c); | |
19a8d9e0 | 4535 | } |
4ed46869 | 4536 | } |
16eafb5d | 4537 | else if (CHAR_BYTE8_P (c)) |
88993dfd | 4538 | { |
16eafb5d KH |
4539 | c = CHAR_TO_BYTE8 (c); |
4540 | EMIT_ONE_BYTE (c); | |
88993dfd | 4541 | } |
b73bfc1c | 4542 | else |
df7492f9 | 4543 | { |
ff0dacd7 | 4544 | struct charset *charset; |
b73bfc1c | 4545 | |
ff0dacd7 KH |
4546 | if (preferred_charset_id >= 0) |
4547 | { | |
f10fe38f | 4548 | bool result; |
5eb05ea3 | 4549 | |
ff0dacd7 | 4550 | charset = CHARSET_FROM_ID (preferred_charset_id); |
5eb05ea3 KH |
4551 | CODING_CHAR_CHARSET_P (coding, dst, dst_end, c, charset, result); |
4552 | if (! result) | |
4553 | CODING_CHAR_CHARSET (coding, dst, dst_end, c, charset_list, | |
4554 | NULL, charset); | |
ff0dacd7 KH |
4555 | } |
4556 | else | |
5eb05ea3 KH |
4557 | CODING_CHAR_CHARSET (coding, dst, dst_end, c, charset_list, |
4558 | NULL, charset); | |
df7492f9 KH |
4559 | if (!charset) |
4560 | { | |
41cbe562 KH |
4561 | if (coding->mode & CODING_MODE_SAFE_ENCODING) |
4562 | { | |
4563 | c = CODING_INHIBIT_CHARACTER_SUBSTITUTION; | |
4564 | charset = CHARSET_FROM_ID (charset_ascii); | |
4565 | } | |
4566 | else | |
4567 | { | |
4568 | c = coding->default_char; | |
5eb05ea3 KH |
4569 | CODING_CHAR_CHARSET (coding, dst, dst_end, c, |
4570 | charset_list, NULL, charset); | |
41cbe562 | 4571 | } |
df7492f9 KH |
4572 | } |
4573 | ENCODE_ISO_CHARACTER (charset, c); | |
4574 | } | |
84fbb8a0 | 4575 | } |
b73bfc1c | 4576 | |
df7492f9 KH |
4577 | if (coding->mode & CODING_MODE_LAST_BLOCK |
4578 | && CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL) | |
4579 | { | |
4580 | ASSURE_DESTINATION (safe_room); | |
4581 | ENCODE_RESET_PLANE_AND_REGISTER (); | |
4582 | } | |
065e3595 | 4583 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
4584 | CODING_ISO_BOL (coding) = bol_designation; |
4585 | coding->produced_char += produced_chars; | |
4586 | coding->produced = dst - coding->destination; | |
4587 | return 0; | |
4ed46869 KH |
4588 | } |
4589 | ||
4590 | \f | |
df7492f9 | 4591 | /*** 8,9. SJIS and BIG5 handlers ***/ |
4ed46869 | 4592 | |
df7492f9 | 4593 | /* Although SJIS and BIG5 are not ISO's coding system, they are used |
4ed46869 KH |
4594 | quite widely. So, for the moment, Emacs supports them in the bare |
4595 | C code. But, in the future, they may be supported only by CCL. */ | |
4596 | ||
4597 | /* SJIS is a coding system encoding three character sets: ASCII, right | |
4598 | half of JISX0201-Kana, and JISX0208. An ASCII character is encoded | |
4599 | as is. A character of charset katakana-jisx0201 is encoded by | |
4600 | "position-code + 0x80". A character of charset japanese-jisx0208 | |
4601 | is encoded in 2-byte but two position-codes are divided and shifted | |
df7492f9 | 4602 | so that it fit in the range below. |
4ed46869 KH |
4603 | |
4604 | --- CODE RANGE of SJIS --- | |
4605 | (character set) (range) | |
4606 | ASCII 0x00 .. 0x7F | |
df7492f9 | 4607 | KATAKANA-JISX0201 0xA0 .. 0xDF |
c28a9453 | 4608 | JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF |
d14d03ac | 4609 | (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC |
4ed46869 KH |
4610 | ------------------------------- |
4611 | ||
4612 | */ | |
4613 | ||
4614 | /* BIG5 is a coding system encoding two character sets: ASCII and | |
4615 | Big5. An ASCII character is encoded as is. Big5 is a two-byte | |
df7492f9 | 4616 | character set and is encoded in two-byte. |
4ed46869 KH |
4617 | |
4618 | --- CODE RANGE of BIG5 --- | |
4619 | (character set) (range) | |
4620 | ASCII 0x00 .. 0x7F | |
4621 | Big5 (1st byte) 0xA1 .. 0xFE | |
4622 | (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE | |
4623 | -------------------------- | |
4624 | ||
df7492f9 | 4625 | */ |
4ed46869 KH |
4626 | |
4627 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
f10fe38f | 4628 | Return true if a text is encoded in SJIS. */ |
4ed46869 | 4629 | |
f10fe38f | 4630 | static bool |
cf84bb53 JB |
4631 | detect_coding_sjis (struct coding_system *coding, |
4632 | struct coding_detection_info *detect_info) | |
4ed46869 | 4633 | { |
065e3595 | 4634 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 4635 | const unsigned char *src_end = coding->source + coding->src_bytes; |
f10fe38f | 4636 | bool multibytep = coding->src_multibyte; |
d311d28c | 4637 | ptrdiff_t consumed_chars = 0; |
df7492f9 | 4638 | int found = 0; |
b73bfc1c | 4639 | int c; |
f07190ca KH |
4640 | Lisp_Object attrs, charset_list; |
4641 | int max_first_byte_of_2_byte_code; | |
4642 | ||
4643 | CODING_GET_INFO (coding, attrs, charset_list); | |
4644 | max_first_byte_of_2_byte_code | |
4645 | = (XINT (Flength (charset_list)) > 3 ? 0xFC : 0xEF); | |
df7492f9 | 4646 | |
ff0dacd7 | 4647 | detect_info->checked |= CATEGORY_MASK_SJIS; |
df7492f9 KH |
4648 | /* A coding system of this category is always ASCII compatible. */ |
4649 | src += coding->head_ascii; | |
4ed46869 | 4650 | |
b73bfc1c | 4651 | while (1) |
4ed46869 | 4652 | { |
065e3595 | 4653 | src_base = src; |
df7492f9 | 4654 | ONE_MORE_BYTE (c); |
682169fe KH |
4655 | if (c < 0x80) |
4656 | continue; | |
f07190ca KH |
4657 | if ((c >= 0x81 && c <= 0x9F) |
4658 | || (c >= 0xE0 && c <= max_first_byte_of_2_byte_code)) | |
4ed46869 | 4659 | { |
df7492f9 | 4660 | ONE_MORE_BYTE (c); |
682169fe | 4661 | if (c < 0x40 || c == 0x7F || c > 0xFC) |
df7492f9 | 4662 | break; |
ff0dacd7 | 4663 | found = CATEGORY_MASK_SJIS; |
4ed46869 | 4664 | } |
df7492f9 | 4665 | else if (c >= 0xA0 && c < 0xE0) |
ff0dacd7 | 4666 | found = CATEGORY_MASK_SJIS; |
df7492f9 KH |
4667 | else |
4668 | break; | |
4ed46869 | 4669 | } |
ff0dacd7 | 4670 | detect_info->rejected |= CATEGORY_MASK_SJIS; |
df7492f9 KH |
4671 | return 0; |
4672 | ||
4673 | no_more_source: | |
065e3595 | 4674 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) |
89528eb3 | 4675 | { |
ff0dacd7 | 4676 | detect_info->rejected |= CATEGORY_MASK_SJIS; |
89528eb3 | 4677 | return 0; |
4ed46869 | 4678 | } |
ff0dacd7 KH |
4679 | detect_info->found |= found; |
4680 | return 1; | |
4ed46869 KH |
4681 | } |
4682 | ||
4683 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
f10fe38f | 4684 | Return true if a text is encoded in BIG5. */ |
4ed46869 | 4685 | |
f10fe38f | 4686 | static bool |
cf84bb53 JB |
4687 | detect_coding_big5 (struct coding_system *coding, |
4688 | struct coding_detection_info *detect_info) | |
4ed46869 | 4689 | { |
065e3595 | 4690 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 4691 | const unsigned char *src_end = coding->source + coding->src_bytes; |
f10fe38f | 4692 | bool multibytep = coding->src_multibyte; |
d311d28c | 4693 | ptrdiff_t consumed_chars = 0; |
df7492f9 | 4694 | int found = 0; |
b73bfc1c | 4695 | int c; |
fa42c37f | 4696 | |
ff0dacd7 | 4697 | detect_info->checked |= CATEGORY_MASK_BIG5; |
df7492f9 KH |
4698 | /* A coding system of this category is always ASCII compatible. */ |
4699 | src += coding->head_ascii; | |
fa42c37f | 4700 | |
b73bfc1c | 4701 | while (1) |
fa42c37f | 4702 | { |
065e3595 | 4703 | src_base = src; |
df7492f9 KH |
4704 | ONE_MORE_BYTE (c); |
4705 | if (c < 0x80) | |
fa42c37f | 4706 | continue; |
df7492f9 | 4707 | if (c >= 0xA1) |
fa42c37f | 4708 | { |
df7492f9 KH |
4709 | ONE_MORE_BYTE (c); |
4710 | if (c < 0x40 || (c >= 0x7F && c <= 0xA0)) | |
fa42c37f | 4711 | return 0; |
ff0dacd7 | 4712 | found = CATEGORY_MASK_BIG5; |
fa42c37f | 4713 | } |
df7492f9 KH |
4714 | else |
4715 | break; | |
fa42c37f | 4716 | } |
ff0dacd7 | 4717 | detect_info->rejected |= CATEGORY_MASK_BIG5; |
fa42c37f | 4718 | return 0; |
fa42c37f | 4719 | |
df7492f9 | 4720 | no_more_source: |
065e3595 | 4721 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) |
89528eb3 | 4722 | { |
ff0dacd7 | 4723 | detect_info->rejected |= CATEGORY_MASK_BIG5; |
89528eb3 KH |
4724 | return 0; |
4725 | } | |
ff0dacd7 KH |
4726 | detect_info->found |= found; |
4727 | return 1; | |
fa42c37f KH |
4728 | } |
4729 | ||
f10fe38f | 4730 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ |
fa42c37f | 4731 | |
b73bfc1c | 4732 | static void |
971de7fb | 4733 | decode_coding_sjis (struct coding_system *coding) |
4ed46869 | 4734 | { |
8f924df7 KH |
4735 | const unsigned char *src = coding->source + coding->consumed; |
4736 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
4737 | const unsigned char *src_base; | |
69a80ea3 | 4738 | int *charbuf = coding->charbuf + coding->charbuf_used; |
ad1746f5 | 4739 | /* We may produce one charset annotation in one loop and one more at |
df80c7f0 | 4740 | the end. */ |
69a80ea3 | 4741 | int *charbuf_end |
df80c7f0 | 4742 | = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 2); |
d311d28c | 4743 | ptrdiff_t consumed_chars = 0, consumed_chars_base; |
f10fe38f | 4744 | bool multibytep = coding->src_multibyte; |
df7492f9 | 4745 | struct charset *charset_roman, *charset_kanji, *charset_kana; |
57a47f8a | 4746 | struct charset *charset_kanji2; |
24a73b0a | 4747 | Lisp_Object attrs, charset_list, val; |
d311d28c PE |
4748 | ptrdiff_t char_offset = coding->produced_char; |
4749 | ptrdiff_t last_offset = char_offset; | |
ff0dacd7 | 4750 | int last_id = charset_ascii; |
f10fe38f PE |
4751 | bool eol_dos |
4752 | = !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); | |
119852e7 | 4753 | int byte_after_cr = -1; |
a5d301df | 4754 | |
24a73b0a | 4755 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4756 | |
4757 | val = charset_list; | |
4758 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
89528eb3 | 4759 | charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
57a47f8a KH |
4760 | charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
4761 | charset_kanji2 = NILP (val) ? NULL : CHARSET_FROM_ID (XINT (XCAR (val))); | |
fa42c37f | 4762 | |
b73bfc1c | 4763 | while (1) |
4ed46869 | 4764 | { |
df7492f9 | 4765 | int c, c1; |
24a73b0a | 4766 | struct charset *charset; |
fa42c37f | 4767 | |
b73bfc1c | 4768 | src_base = src; |
df7492f9 | 4769 | consumed_chars_base = consumed_chars; |
fa42c37f | 4770 | |
df7492f9 | 4771 | if (charbuf >= charbuf_end) |
b71f6f73 KH |
4772 | { |
4773 | if (byte_after_cr >= 0) | |
4774 | src_base--; | |
4775 | break; | |
4776 | } | |
df7492f9 | 4777 | |
119852e7 KH |
4778 | if (byte_after_cr >= 0) |
4779 | c = byte_after_cr, byte_after_cr = -1; | |
4780 | else | |
4781 | ONE_MORE_BYTE (c); | |
065e3595 KH |
4782 | if (c < 0) |
4783 | goto invalid_code; | |
24a73b0a | 4784 | if (c < 0x80) |
119852e7 | 4785 | { |
2735d060 | 4786 | if (eol_dos && c == '\r') |
119852e7 KH |
4787 | ONE_MORE_BYTE (byte_after_cr); |
4788 | charset = charset_roman; | |
4789 | } | |
57a47f8a | 4790 | else if (c == 0x80 || c == 0xA0) |
8e921c4b | 4791 | goto invalid_code; |
57a47f8a KH |
4792 | else if (c >= 0xA1 && c <= 0xDF) |
4793 | { | |
4794 | /* SJIS -> JISX0201-Kana */ | |
4795 | c &= 0x7F; | |
4796 | charset = charset_kana; | |
4797 | } | |
4798 | else if (c <= 0xEF) | |
df7492f9 | 4799 | { |
57a47f8a KH |
4800 | /* SJIS -> JISX0208 */ |
4801 | ONE_MORE_BYTE (c1); | |
4802 | if (c1 < 0x40 || c1 == 0x7F || c1 > 0xFC) | |
24a73b0a | 4803 | goto invalid_code; |
57a47f8a KH |
4804 | c = (c << 8) | c1; |
4805 | SJIS_TO_JIS (c); | |
4806 | charset = charset_kanji; | |
4807 | } | |
4808 | else if (c <= 0xFC && charset_kanji2) | |
4809 | { | |
c6876370 | 4810 | /* SJIS -> JISX0213-2 */ |
57a47f8a KH |
4811 | ONE_MORE_BYTE (c1); |
4812 | if (c1 < 0x40 || c1 == 0x7F || c1 > 0xFC) | |
24a73b0a | 4813 | goto invalid_code; |
57a47f8a KH |
4814 | c = (c << 8) | c1; |
4815 | SJIS_TO_JIS2 (c); | |
4816 | charset = charset_kanji2; | |
df7492f9 | 4817 | } |
57a47f8a KH |
4818 | else |
4819 | goto invalid_code; | |
24a73b0a KH |
4820 | if (charset->id != charset_ascii |
4821 | && last_id != charset->id) | |
4822 | { | |
4823 | if (last_id != charset_ascii) | |
69a80ea3 | 4824 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
24a73b0a KH |
4825 | last_id = charset->id; |
4826 | last_offset = char_offset; | |
4827 | } | |
4828 | CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c); | |
df7492f9 | 4829 | *charbuf++ = c; |
ff0dacd7 | 4830 | char_offset++; |
df7492f9 | 4831 | continue; |
b73bfc1c | 4832 | |
df7492f9 KH |
4833 | invalid_code: |
4834 | src = src_base; | |
4835 | consumed_chars = consumed_chars_base; | |
4836 | ONE_MORE_BYTE (c); | |
065e3595 | 4837 | *charbuf++ = c < 0 ? -c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 4838 | char_offset++; |
df7492f9 KH |
4839 | coding->errors++; |
4840 | } | |
fa42c37f | 4841 | |
df7492f9 | 4842 | no_more_source: |
ff0dacd7 | 4843 | if (last_id != charset_ascii) |
69a80ea3 | 4844 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
4845 | coding->consumed_char += consumed_chars_base; |
4846 | coding->consumed = src_base - coding->source; | |
4847 | coding->charbuf_used = charbuf - coding->charbuf; | |
fa42c37f KH |
4848 | } |
4849 | ||
b73bfc1c | 4850 | static void |
971de7fb | 4851 | decode_coding_big5 (struct coding_system *coding) |
4ed46869 | 4852 | { |
8f924df7 KH |
4853 | const unsigned char *src = coding->source + coding->consumed; |
4854 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
4855 | const unsigned char *src_base; | |
69a80ea3 | 4856 | int *charbuf = coding->charbuf + coding->charbuf_used; |
ad1746f5 | 4857 | /* We may produce one charset annotation in one loop and one more at |
df80c7f0 | 4858 | the end. */ |
69a80ea3 | 4859 | int *charbuf_end |
df80c7f0 | 4860 | = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 2); |
d311d28c | 4861 | ptrdiff_t consumed_chars = 0, consumed_chars_base; |
f10fe38f | 4862 | bool multibytep = coding->src_multibyte; |
df7492f9 | 4863 | struct charset *charset_roman, *charset_big5; |
24a73b0a | 4864 | Lisp_Object attrs, charset_list, val; |
d311d28c PE |
4865 | ptrdiff_t char_offset = coding->produced_char; |
4866 | ptrdiff_t last_offset = char_offset; | |
ff0dacd7 | 4867 | int last_id = charset_ascii; |
f10fe38f PE |
4868 | bool eol_dos |
4869 | = !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); | |
119852e7 | 4870 | int byte_after_cr = -1; |
df7492f9 | 4871 | |
24a73b0a | 4872 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4873 | val = charset_list; |
4874 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
4875 | charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val))); | |
4ed46869 | 4876 | |
b73bfc1c | 4877 | while (1) |
4ed46869 | 4878 | { |
df7492f9 | 4879 | int c, c1; |
24a73b0a | 4880 | struct charset *charset; |
b73bfc1c KH |
4881 | |
4882 | src_base = src; | |
df7492f9 KH |
4883 | consumed_chars_base = consumed_chars; |
4884 | ||
4885 | if (charbuf >= charbuf_end) | |
b71f6f73 KH |
4886 | { |
4887 | if (byte_after_cr >= 0) | |
4888 | src_base--; | |
4889 | break; | |
4890 | } | |
df7492f9 | 4891 | |
119852e7 | 4892 | if (byte_after_cr >= 0) |
14daee73 | 4893 | c = byte_after_cr, byte_after_cr = -1; |
119852e7 KH |
4894 | else |
4895 | ONE_MORE_BYTE (c); | |
b73bfc1c | 4896 | |
065e3595 KH |
4897 | if (c < 0) |
4898 | goto invalid_code; | |
24a73b0a | 4899 | if (c < 0x80) |
119852e7 | 4900 | { |
2735d060 | 4901 | if (eol_dos && c == '\r') |
119852e7 KH |
4902 | ONE_MORE_BYTE (byte_after_cr); |
4903 | charset = charset_roman; | |
4904 | } | |
24a73b0a | 4905 | else |
4ed46869 | 4906 | { |
24a73b0a KH |
4907 | /* BIG5 -> Big5 */ |
4908 | if (c < 0xA1 || c > 0xFE) | |
4909 | goto invalid_code; | |
4910 | ONE_MORE_BYTE (c1); | |
4911 | if (c1 < 0x40 || (c1 > 0x7E && c1 < 0xA1) || c1 > 0xFE) | |
4912 | goto invalid_code; | |
4913 | c = c << 8 | c1; | |
4914 | charset = charset_big5; | |
4ed46869 | 4915 | } |
24a73b0a KH |
4916 | if (charset->id != charset_ascii |
4917 | && last_id != charset->id) | |
df7492f9 | 4918 | { |
24a73b0a | 4919 | if (last_id != charset_ascii) |
69a80ea3 | 4920 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
24a73b0a KH |
4921 | last_id = charset->id; |
4922 | last_offset = char_offset; | |
4ed46869 | 4923 | } |
24a73b0a | 4924 | CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c); |
df7492f9 | 4925 | *charbuf++ = c; |
ff0dacd7 | 4926 | char_offset++; |
fb88bf2d KH |
4927 | continue; |
4928 | ||
df7492f9 | 4929 | invalid_code: |
4ed46869 | 4930 | src = src_base; |
df7492f9 KH |
4931 | consumed_chars = consumed_chars_base; |
4932 | ONE_MORE_BYTE (c); | |
065e3595 | 4933 | *charbuf++ = c < 0 ? -c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 4934 | char_offset++; |
df7492f9 | 4935 | coding->errors++; |
fb88bf2d | 4936 | } |
d46c5b12 | 4937 | |
df7492f9 | 4938 | no_more_source: |
ff0dacd7 | 4939 | if (last_id != charset_ascii) |
69a80ea3 | 4940 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
4941 | coding->consumed_char += consumed_chars_base; |
4942 | coding->consumed = src_base - coding->source; | |
4943 | coding->charbuf_used = charbuf - coding->charbuf; | |
4ed46869 KH |
4944 | } |
4945 | ||
4946 | /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". | |
b73bfc1c KH |
4947 | This function can encode charsets `ascii', `katakana-jisx0201', |
4948 | `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We | |
4949 | are sure that all these charsets are registered as official charset | |
4ed46869 | 4950 | (i.e. do not have extended leading-codes). Characters of other |
f10fe38f | 4951 | charsets are produced without any encoding. */ |
4ed46869 | 4952 | |
f10fe38f | 4953 | static bool |
971de7fb | 4954 | encode_coding_sjis (struct coding_system *coding) |
4ed46869 | 4955 | { |
f10fe38f | 4956 | bool multibytep = coding->dst_multibyte; |
df7492f9 KH |
4957 | int *charbuf = coding->charbuf; |
4958 | int *charbuf_end = charbuf + coding->charbuf_used; | |
4959 | unsigned char *dst = coding->destination + coding->produced; | |
4960 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
4961 | int safe_room = 4; | |
d311d28c | 4962 | ptrdiff_t produced_chars = 0; |
24a73b0a | 4963 | Lisp_Object attrs, charset_list, val; |
f10fe38f | 4964 | bool ascii_compatible; |
66ebf983 | 4965 | struct charset *charset_kanji, *charset_kana; |
57a47f8a | 4966 | struct charset *charset_kanji2; |
df7492f9 | 4967 | int c; |
a5d301df | 4968 | |
24a73b0a | 4969 | CODING_GET_INFO (coding, attrs, charset_list); |
66ebf983 | 4970 | val = XCDR (charset_list); |
df7492f9 | 4971 | charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
57a47f8a KH |
4972 | charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
4973 | charset_kanji2 = NILP (val) ? NULL : CHARSET_FROM_ID (XINT (XCAR (val))); | |
4ed46869 | 4974 | |
df7492f9 | 4975 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); |
93dec019 | 4976 | |
df7492f9 KH |
4977 | while (charbuf < charbuf_end) |
4978 | { | |
4979 | ASSURE_DESTINATION (safe_room); | |
4980 | c = *charbuf++; | |
b73bfc1c | 4981 | /* Now encode the character C. */ |
df7492f9 KH |
4982 | if (ASCII_CHAR_P (c) && ascii_compatible) |
4983 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d KH |
4984 | else if (CHAR_BYTE8_P (c)) |
4985 | { | |
4986 | c = CHAR_TO_BYTE8 (c); | |
4987 | EMIT_ONE_BYTE (c); | |
4988 | } | |
df7492f9 | 4989 | else |
b73bfc1c | 4990 | { |
df7492f9 | 4991 | unsigned code; |
5eb05ea3 KH |
4992 | struct charset *charset; |
4993 | CODING_CHAR_CHARSET (coding, dst, dst_end, c, charset_list, | |
4994 | &code, charset); | |
df7492f9 KH |
4995 | |
4996 | if (!charset) | |
4ed46869 | 4997 | { |
41cbe562 | 4998 | if (coding->mode & CODING_MODE_SAFE_ENCODING) |
b73bfc1c | 4999 | { |
41cbe562 KH |
5000 | code = CODING_INHIBIT_CHARACTER_SUBSTITUTION; |
5001 | charset = CHARSET_FROM_ID (charset_ascii); | |
b73bfc1c | 5002 | } |
41cbe562 | 5003 | else |
b73bfc1c | 5004 | { |
41cbe562 | 5005 | c = coding->default_char; |
5eb05ea3 KH |
5006 | CODING_CHAR_CHARSET (coding, dst, dst_end, c, |
5007 | charset_list, &code, charset); | |
b73bfc1c | 5008 | } |
b73bfc1c | 5009 | } |
df7492f9 | 5010 | if (code == CHARSET_INVALID_CODE (charset)) |
1088b922 | 5011 | emacs_abort (); |
df7492f9 KH |
5012 | if (charset == charset_kanji) |
5013 | { | |
5014 | int c1, c2; | |
5015 | JIS_TO_SJIS (code); | |
5016 | c1 = code >> 8, c2 = code & 0xFF; | |
5017 | EMIT_TWO_BYTES (c1, c2); | |
5018 | } | |
5019 | else if (charset == charset_kana) | |
5020 | EMIT_ONE_BYTE (code | 0x80); | |
57a47f8a KH |
5021 | else if (charset_kanji2 && charset == charset_kanji2) |
5022 | { | |
5023 | int c1, c2; | |
5024 | ||
5025 | c1 = code >> 8; | |
f07190ca KH |
5026 | if (c1 == 0x21 || (c1 >= 0x23 && c1 <= 0x25) |
5027 | || c1 == 0x28 | |
57a47f8a KH |
5028 | || (c1 >= 0x2C && c1 <= 0x2F) || c1 >= 0x6E) |
5029 | { | |
5030 | JIS_TO_SJIS2 (code); | |
5031 | c1 = code >> 8, c2 = code & 0xFF; | |
5032 | EMIT_TWO_BYTES (c1, c2); | |
5033 | } | |
5034 | else | |
5035 | EMIT_ONE_ASCII_BYTE (code & 0x7F); | |
5036 | } | |
df7492f9 KH |
5037 | else |
5038 | EMIT_ONE_ASCII_BYTE (code & 0x7F); | |
5039 | } | |
5040 | } | |
065e3595 | 5041 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
5042 | coding->produced_char += produced_chars; |
5043 | coding->produced = dst - coding->destination; | |
5044 | return 0; | |
5045 | } | |
5046 | ||
f10fe38f | 5047 | static bool |
971de7fb | 5048 | encode_coding_big5 (struct coding_system *coding) |
df7492f9 | 5049 | { |
f10fe38f | 5050 | bool multibytep = coding->dst_multibyte; |
df7492f9 KH |
5051 | int *charbuf = coding->charbuf; |
5052 | int *charbuf_end = charbuf + coding->charbuf_used; | |
5053 | unsigned char *dst = coding->destination + coding->produced; | |
5054 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
5055 | int safe_room = 4; | |
d311d28c | 5056 | ptrdiff_t produced_chars = 0; |
24a73b0a | 5057 | Lisp_Object attrs, charset_list, val; |
f10fe38f | 5058 | bool ascii_compatible; |
66ebf983 | 5059 | struct charset *charset_big5; |
df7492f9 KH |
5060 | int c; |
5061 | ||
24a73b0a | 5062 | CODING_GET_INFO (coding, attrs, charset_list); |
66ebf983 | 5063 | val = XCDR (charset_list); |
df7492f9 KH |
5064 | charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val))); |
5065 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); | |
5066 | ||
5067 | while (charbuf < charbuf_end) | |
5068 | { | |
5069 | ASSURE_DESTINATION (safe_room); | |
5070 | c = *charbuf++; | |
5071 | /* Now encode the character C. */ | |
5072 | if (ASCII_CHAR_P (c) && ascii_compatible) | |
5073 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d KH |
5074 | else if (CHAR_BYTE8_P (c)) |
5075 | { | |
5076 | c = CHAR_TO_BYTE8 (c); | |
5077 | EMIT_ONE_BYTE (c); | |
b73bfc1c KH |
5078 | } |
5079 | else | |
5080 | { | |
df7492f9 | 5081 | unsigned code; |
5eb05ea3 KH |
5082 | struct charset *charset; |
5083 | CODING_CHAR_CHARSET (coding, dst, dst_end, c, charset_list, | |
5084 | &code, charset); | |
df7492f9 KH |
5085 | |
5086 | if (! charset) | |
b73bfc1c | 5087 | { |
41cbe562 | 5088 | if (coding->mode & CODING_MODE_SAFE_ENCODING) |
b73bfc1c | 5089 | { |
41cbe562 KH |
5090 | code = CODING_INHIBIT_CHARACTER_SUBSTITUTION; |
5091 | charset = CHARSET_FROM_ID (charset_ascii); | |
b73bfc1c | 5092 | } |
41cbe562 | 5093 | else |
0eecad43 | 5094 | { |
41cbe562 | 5095 | c = coding->default_char; |
5eb05ea3 KH |
5096 | CODING_CHAR_CHARSET (coding, dst, dst_end, c, |
5097 | charset_list, &code, charset); | |
0eecad43 | 5098 | } |
4ed46869 | 5099 | } |
df7492f9 | 5100 | if (code == CHARSET_INVALID_CODE (charset)) |
1088b922 | 5101 | emacs_abort (); |
df7492f9 | 5102 | if (charset == charset_big5) |
b73bfc1c | 5103 | { |
df7492f9 KH |
5104 | int c1, c2; |
5105 | ||
5106 | c1 = code >> 8, c2 = code & 0xFF; | |
5107 | EMIT_TWO_BYTES (c1, c2); | |
b73bfc1c | 5108 | } |
df7492f9 KH |
5109 | else |
5110 | EMIT_ONE_ASCII_BYTE (code & 0x7F); | |
4ed46869 | 5111 | } |
4ed46869 | 5112 | } |
065e3595 | 5113 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
5114 | coding->produced_char += produced_chars; |
5115 | coding->produced = dst - coding->destination; | |
5116 | return 0; | |
4ed46869 KH |
5117 | } |
5118 | ||
5119 | \f | |
df7492f9 | 5120 | /*** 10. CCL handlers ***/ |
1397dc18 KH |
5121 | |
5122 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
f10fe38f PE |
5123 | Return true if a text is encoded in a coding system of which |
5124 | encoder/decoder are written in CCL program. */ | |
1397dc18 | 5125 | |
f10fe38f | 5126 | static bool |
cf84bb53 JB |
5127 | detect_coding_ccl (struct coding_system *coding, |
5128 | struct coding_detection_info *detect_info) | |
1397dc18 | 5129 | { |
065e3595 | 5130 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 5131 | const unsigned char *src_end = coding->source + coding->src_bytes; |
f10fe38f | 5132 | bool multibytep = coding->src_multibyte; |
d311d28c | 5133 | ptrdiff_t consumed_chars = 0; |
df7492f9 | 5134 | int found = 0; |
0e219d54 | 5135 | unsigned char *valids; |
d311d28c | 5136 | ptrdiff_t head_ascii = coding->head_ascii; |
df7492f9 KH |
5137 | Lisp_Object attrs; |
5138 | ||
ff0dacd7 KH |
5139 | detect_info->checked |= CATEGORY_MASK_CCL; |
5140 | ||
df7492f9 | 5141 | coding = &coding_categories[coding_category_ccl]; |
0e219d54 | 5142 | valids = CODING_CCL_VALIDS (coding); |
df7492f9 KH |
5143 | attrs = CODING_ID_ATTRS (coding->id); |
5144 | if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
5145 | src += head_ascii; | |
1397dc18 | 5146 | |
b73bfc1c | 5147 | while (1) |
1397dc18 | 5148 | { |
df7492f9 | 5149 | int c; |
065e3595 KH |
5150 | |
5151 | src_base = src; | |
df7492f9 | 5152 | ONE_MORE_BYTE (c); |
065e3595 | 5153 | if (c < 0 || ! valids[c]) |
df7492f9 | 5154 | break; |
ff0dacd7 KH |
5155 | if ((valids[c] > 1)) |
5156 | found = CATEGORY_MASK_CCL; | |
df7492f9 | 5157 | } |
ff0dacd7 | 5158 | detect_info->rejected |= CATEGORY_MASK_CCL; |
df7492f9 KH |
5159 | return 0; |
5160 | ||
5161 | no_more_source: | |
ff0dacd7 KH |
5162 | detect_info->found |= found; |
5163 | return 1; | |
df7492f9 KH |
5164 | } |
5165 | ||
5166 | static void | |
971de7fb | 5167 | decode_coding_ccl (struct coding_system *coding) |
df7492f9 | 5168 | { |
7c78e542 | 5169 | const unsigned char *src = coding->source + coding->consumed; |
8f924df7 | 5170 | const unsigned char *src_end = coding->source + coding->src_bytes; |
69a80ea3 KH |
5171 | int *charbuf = coding->charbuf + coding->charbuf_used; |
5172 | int *charbuf_end = coding->charbuf + coding->charbuf_size; | |
d311d28c | 5173 | ptrdiff_t consumed_chars = 0; |
f10fe38f | 5174 | bool multibytep = coding->src_multibyte; |
d0396581 | 5175 | struct ccl_program *ccl = &coding->spec.ccl->ccl; |
df7492f9 | 5176 | int source_charbuf[1024]; |
fbdc1721 | 5177 | int source_byteidx[1025]; |
24a73b0a | 5178 | Lisp_Object attrs, charset_list; |
df7492f9 | 5179 | |
24a73b0a | 5180 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 | 5181 | |
d0396581 | 5182 | while (1) |
df7492f9 | 5183 | { |
7c78e542 | 5184 | const unsigned char *p = src; |
95402d5f | 5185 | ptrdiff_t offset; |
df7492f9 KH |
5186 | int i = 0; |
5187 | ||
5188 | if (multibytep) | |
fbdc1721 KH |
5189 | { |
5190 | while (i < 1024 && p < src_end) | |
5191 | { | |
5192 | source_byteidx[i] = p - src; | |
5193 | source_charbuf[i++] = STRING_CHAR_ADVANCE (p); | |
5194 | } | |
5195 | source_byteidx[i] = p - src; | |
5196 | } | |
df7492f9 KH |
5197 | else |
5198 | while (i < 1024 && p < src_end) | |
5199 | source_charbuf[i++] = *p++; | |
8f924df7 | 5200 | |
df7492f9 | 5201 | if (p == src_end && coding->mode & CODING_MODE_LAST_BLOCK) |
d0396581 | 5202 | ccl->last_block = 1; |
95402d5f KH |
5203 | /* As ccl_driver calls DECODE_CHAR, buffer may be relocated. */ |
5204 | charset_map_loaded = 0; | |
d0396581 KH |
5205 | ccl_driver (ccl, source_charbuf, charbuf, i, charbuf_end - charbuf, |
5206 | charset_list); | |
95402d5f KH |
5207 | if (charset_map_loaded |
5208 | && (offset = coding_change_source (coding))) | |
5209 | { | |
5210 | p += offset; | |
5211 | src += offset; | |
5212 | src_end += offset; | |
5213 | } | |
d0396581 | 5214 | charbuf += ccl->produced; |
fbdc1721 | 5215 | if (multibytep) |
d0396581 | 5216 | src += source_byteidx[ccl->consumed]; |
df7492f9 | 5217 | else |
d0396581 KH |
5218 | src += ccl->consumed; |
5219 | consumed_chars += ccl->consumed; | |
5220 | if (p == src_end || ccl->status != CCL_STAT_SUSPEND_BY_SRC) | |
df7492f9 KH |
5221 | break; |
5222 | } | |
5223 | ||
d0396581 | 5224 | switch (ccl->status) |
df7492f9 KH |
5225 | { |
5226 | case CCL_STAT_SUSPEND_BY_SRC: | |
065e3595 | 5227 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC); |
df7492f9 KH |
5228 | break; |
5229 | case CCL_STAT_SUSPEND_BY_DST: | |
d0396581 | 5230 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_DST); |
df7492f9 KH |
5231 | break; |
5232 | case CCL_STAT_QUIT: | |
5233 | case CCL_STAT_INVALID_CMD: | |
065e3595 | 5234 | record_conversion_result (coding, CODING_RESULT_INTERRUPT); |
df7492f9 KH |
5235 | break; |
5236 | default: | |
065e3595 | 5237 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
5238 | break; |
5239 | } | |
5240 | coding->consumed_char += consumed_chars; | |
5241 | coding->consumed = src - coding->source; | |
5242 | coding->charbuf_used = charbuf - coding->charbuf; | |
5243 | } | |
5244 | ||
f10fe38f | 5245 | static bool |
971de7fb | 5246 | encode_coding_ccl (struct coding_system *coding) |
df7492f9 | 5247 | { |
fb608df3 | 5248 | struct ccl_program *ccl = &coding->spec.ccl->ccl; |
f10fe38f | 5249 | bool multibytep = coding->dst_multibyte; |
df7492f9 KH |
5250 | int *charbuf = coding->charbuf; |
5251 | int *charbuf_end = charbuf + coding->charbuf_used; | |
5252 | unsigned char *dst = coding->destination + coding->produced; | |
5253 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
df7492f9 | 5254 | int destination_charbuf[1024]; |
d311d28c | 5255 | ptrdiff_t produced_chars = 0; |
a53e2e89 | 5256 | int i; |
24a73b0a | 5257 | Lisp_Object attrs, charset_list; |
df7492f9 | 5258 | |
24a73b0a | 5259 | CODING_GET_INFO (coding, attrs, charset_list); |
fb608df3 KH |
5260 | if (coding->consumed_char == coding->src_chars |
5261 | && coding->mode & CODING_MODE_LAST_BLOCK) | |
5262 | ccl->last_block = 1; | |
df7492f9 | 5263 | |
76470ad1 | 5264 | do |
df7492f9 | 5265 | { |
95402d5f KH |
5266 | ptrdiff_t offset; |
5267 | ||
5268 | /* As ccl_driver calls DECODE_CHAR, buffer may be relocated. */ | |
5269 | charset_map_loaded = 0; | |
fb608df3 | 5270 | ccl_driver (ccl, charbuf, destination_charbuf, |
8cffd3e7 | 5271 | charbuf_end - charbuf, 1024, charset_list); |
95402d5f KH |
5272 | if (charset_map_loaded |
5273 | && (offset = coding_change_destination (coding))) | |
5274 | dst += offset; | |
df7492f9 | 5275 | if (multibytep) |
8cffd3e7 | 5276 | { |
fb608df3 KH |
5277 | ASSURE_DESTINATION (ccl->produced * 2); |
5278 | for (i = 0; i < ccl->produced; i++) | |
8cffd3e7 KH |
5279 | EMIT_ONE_BYTE (destination_charbuf[i] & 0xFF); |
5280 | } | |
df7492f9 KH |
5281 | else |
5282 | { | |
fb608df3 KH |
5283 | ASSURE_DESTINATION (ccl->produced); |
5284 | for (i = 0; i < ccl->produced; i++) | |
df7492f9 | 5285 | *dst++ = destination_charbuf[i] & 0xFF; |
fb608df3 | 5286 | produced_chars += ccl->produced; |
df7492f9 | 5287 | } |
fb608df3 KH |
5288 | charbuf += ccl->consumed; |
5289 | if (ccl->status == CCL_STAT_QUIT | |
5290 | || ccl->status == CCL_STAT_INVALID_CMD) | |
8cffd3e7 | 5291 | break; |
df7492f9 | 5292 | } |
76470ad1 | 5293 | while (charbuf < charbuf_end); |
df7492f9 | 5294 | |
fb608df3 | 5295 | switch (ccl->status) |
df7492f9 KH |
5296 | { |
5297 | case CCL_STAT_SUSPEND_BY_SRC: | |
065e3595 | 5298 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC); |
df7492f9 KH |
5299 | break; |
5300 | case CCL_STAT_SUSPEND_BY_DST: | |
065e3595 | 5301 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_DST); |
df7492f9 KH |
5302 | break; |
5303 | case CCL_STAT_QUIT: | |
5304 | case CCL_STAT_INVALID_CMD: | |
065e3595 | 5305 | record_conversion_result (coding, CODING_RESULT_INTERRUPT); |
df7492f9 KH |
5306 | break; |
5307 | default: | |
065e3595 | 5308 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 | 5309 | break; |
1397dc18 | 5310 | } |
df7492f9 KH |
5311 | |
5312 | coding->produced_char += produced_chars; | |
5313 | coding->produced = dst - coding->destination; | |
5314 | return 0; | |
1397dc18 KH |
5315 | } |
5316 | ||
5317 | \f | |
df7492f9 | 5318 | /*** 10, 11. no-conversion handlers ***/ |
4ed46869 | 5319 | |
b73bfc1c | 5320 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ |
4ed46869 | 5321 | |
b73bfc1c | 5322 | static void |
971de7fb | 5323 | decode_coding_raw_text (struct coding_system *coding) |
4ed46869 | 5324 | { |
f10fe38f PE |
5325 | bool eol_dos |
5326 | = !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); | |
119852e7 | 5327 | |
df7492f9 | 5328 | coding->chars_at_source = 1; |
119852e7 KH |
5329 | coding->consumed_char = coding->src_chars; |
5330 | coding->consumed = coding->src_bytes; | |
2735d060 | 5331 | if (eol_dos && coding->source[coding->src_bytes - 1] == '\r') |
119852e7 KH |
5332 | { |
5333 | coding->consumed_char--; | |
5334 | coding->consumed--; | |
5335 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC); | |
5336 | } | |
5337 | else | |
5338 | record_conversion_result (coding, CODING_RESULT_SUCCESS); | |
df7492f9 | 5339 | } |
4ed46869 | 5340 | |
f10fe38f | 5341 | static bool |
971de7fb | 5342 | encode_coding_raw_text (struct coding_system *coding) |
df7492f9 | 5343 | { |
f10fe38f | 5344 | bool multibytep = coding->dst_multibyte; |
df7492f9 KH |
5345 | int *charbuf = coding->charbuf; |
5346 | int *charbuf_end = coding->charbuf + coding->charbuf_used; | |
5347 | unsigned char *dst = coding->destination + coding->produced; | |
5348 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
d311d28c | 5349 | ptrdiff_t produced_chars = 0; |
b73bfc1c KH |
5350 | int c; |
5351 | ||
df7492f9 | 5352 | if (multibytep) |
b73bfc1c | 5353 | { |
df7492f9 | 5354 | int safe_room = MAX_MULTIBYTE_LENGTH * 2; |
4ed46869 | 5355 | |
df7492f9 KH |
5356 | if (coding->src_multibyte) |
5357 | while (charbuf < charbuf_end) | |
5358 | { | |
5359 | ASSURE_DESTINATION (safe_room); | |
5360 | c = *charbuf++; | |
5361 | if (ASCII_CHAR_P (c)) | |
5362 | EMIT_ONE_ASCII_BYTE (c); | |
5363 | else if (CHAR_BYTE8_P (c)) | |
5364 | { | |
5365 | c = CHAR_TO_BYTE8 (c); | |
5366 | EMIT_ONE_BYTE (c); | |
5367 | } | |
5368 | else | |
5369 | { | |
5370 | unsigned char str[MAX_MULTIBYTE_LENGTH], *p0 = str, *p1 = str; | |
93dec019 | 5371 | |
df7492f9 | 5372 | CHAR_STRING_ADVANCE (c, p1); |
8abc3f12 | 5373 | do |
9d123124 KH |
5374 | { |
5375 | EMIT_ONE_BYTE (*p0); | |
5376 | p0++; | |
5377 | } | |
8abc3f12 | 5378 | while (p0 < p1); |
df7492f9 KH |
5379 | } |
5380 | } | |
b73bfc1c | 5381 | else |
df7492f9 KH |
5382 | while (charbuf < charbuf_end) |
5383 | { | |
5384 | ASSURE_DESTINATION (safe_room); | |
5385 | c = *charbuf++; | |
5386 | EMIT_ONE_BYTE (c); | |
5387 | } | |
5388 | } | |
5389 | else | |
4ed46869 | 5390 | { |
df7492f9 | 5391 | if (coding->src_multibyte) |
d46c5b12 | 5392 | { |
df7492f9 KH |
5393 | int safe_room = MAX_MULTIBYTE_LENGTH; |
5394 | ||
5395 | while (charbuf < charbuf_end) | |
d46c5b12 | 5396 | { |
df7492f9 KH |
5397 | ASSURE_DESTINATION (safe_room); |
5398 | c = *charbuf++; | |
5399 | if (ASCII_CHAR_P (c)) | |
5400 | *dst++ = c; | |
5401 | else if (CHAR_BYTE8_P (c)) | |
5402 | *dst++ = CHAR_TO_BYTE8 (c); | |
b73bfc1c | 5403 | else |
df7492f9 | 5404 | CHAR_STRING_ADVANCE (c, dst); |
d46c5b12 KH |
5405 | } |
5406 | } | |
df7492f9 KH |
5407 | else |
5408 | { | |
5409 | ASSURE_DESTINATION (charbuf_end - charbuf); | |
5410 | while (charbuf < charbuf_end && dst < dst_end) | |
5411 | *dst++ = *charbuf++; | |
8f924df7 | 5412 | } |
319a3947 | 5413 | produced_chars = dst - (coding->destination + coding->produced); |
4ed46869 | 5414 | } |
065e3595 | 5415 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
a0ed9b27 | 5416 | coding->produced_char += produced_chars; |
df7492f9 KH |
5417 | coding->produced = dst - coding->destination; |
5418 | return 0; | |
4ed46869 KH |
5419 | } |
5420 | ||
ff0dacd7 | 5421 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". |
f10fe38f | 5422 | Return true if a text is encoded in a charset-based coding system. */ |
ff0dacd7 | 5423 | |
f10fe38f | 5424 | static bool |
cf84bb53 JB |
5425 | detect_coding_charset (struct coding_system *coding, |
5426 | struct coding_detection_info *detect_info) | |
1397dc18 | 5427 | { |
065e3595 | 5428 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 5429 | const unsigned char *src_end = coding->source + coding->src_bytes; |
f10fe38f | 5430 | bool multibytep = coding->src_multibyte; |
d311d28c | 5431 | ptrdiff_t consumed_chars = 0; |
07295713 | 5432 | Lisp_Object attrs, valids, name; |
584948ac | 5433 | int found = 0; |
d311d28c | 5434 | ptrdiff_t head_ascii = coding->head_ascii; |
f10fe38f | 5435 | bool check_latin_extra = 0; |
1397dc18 | 5436 | |
ff0dacd7 KH |
5437 | detect_info->checked |= CATEGORY_MASK_CHARSET; |
5438 | ||
df7492f9 KH |
5439 | coding = &coding_categories[coding_category_charset]; |
5440 | attrs = CODING_ID_ATTRS (coding->id); | |
5441 | valids = AREF (attrs, coding_attr_charset_valids); | |
07295713 | 5442 | name = CODING_ID_NAME (coding->id); |
51b59d79 | 5443 | if (strncmp (SSDATA (SYMBOL_NAME (name)), |
237aabf4 | 5444 | "iso-8859-", sizeof ("iso-8859-") - 1) == 0 |
51b59d79 | 5445 | || strncmp (SSDATA (SYMBOL_NAME (name)), |
237aabf4 | 5446 | "iso-latin-", sizeof ("iso-latin-") - 1) == 0) |
07295713 | 5447 | check_latin_extra = 1; |
237aabf4 | 5448 | |
df7492f9 | 5449 | if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) |
716b3fa0 | 5450 | src += head_ascii; |
1397dc18 | 5451 | |
b73bfc1c | 5452 | while (1) |
1397dc18 | 5453 | { |
df7492f9 | 5454 | int c; |
716b3fa0 KH |
5455 | Lisp_Object val; |
5456 | struct charset *charset; | |
5457 | int dim, idx; | |
1397dc18 | 5458 | |
065e3595 | 5459 | src_base = src; |
df7492f9 | 5460 | ONE_MORE_BYTE (c); |
065e3595 KH |
5461 | if (c < 0) |
5462 | continue; | |
716b3fa0 KH |
5463 | val = AREF (valids, c); |
5464 | if (NILP (val)) | |
df7492f9 | 5465 | break; |
584948ac | 5466 | if (c >= 0x80) |
07295713 KH |
5467 | { |
5468 | if (c < 0xA0 | |
237aabf4 JR |
5469 | && check_latin_extra |
5470 | && (!VECTORP (Vlatin_extra_code_table) | |
28be1ada | 5471 | || NILP (AREF (Vlatin_extra_code_table, c)))) |
07295713 KH |
5472 | break; |
5473 | found = CATEGORY_MASK_CHARSET; | |
5474 | } | |
716b3fa0 KH |
5475 | if (INTEGERP (val)) |
5476 | { | |
5477 | charset = CHARSET_FROM_ID (XFASTINT (val)); | |
5478 | dim = CHARSET_DIMENSION (charset); | |
5479 | for (idx = 1; idx < dim; idx++) | |
5480 | { | |
5481 | if (src == src_end) | |
5482 | goto too_short; | |
5483 | ONE_MORE_BYTE (c); | |
2f9442b8 PE |
5484 | if (c < charset->code_space[(dim - 1 - idx) * 4] |
5485 | || c > charset->code_space[(dim - 1 - idx) * 4 + 1]) | |
716b3fa0 KH |
5486 | break; |
5487 | } | |
5488 | if (idx < dim) | |
5489 | break; | |
5490 | } | |
5491 | else | |
5492 | { | |
5493 | idx = 1; | |
5494 | for (; CONSP (val); val = XCDR (val)) | |
5495 | { | |
5496 | charset = CHARSET_FROM_ID (XFASTINT (XCAR (val))); | |
5497 | dim = CHARSET_DIMENSION (charset); | |
5498 | while (idx < dim) | |
5499 | { | |
5500 | if (src == src_end) | |
5501 | goto too_short; | |
5502 | ONE_MORE_BYTE (c); | |
5503 | if (c < charset->code_space[(dim - 1 - idx) * 4] | |
5504 | || c > charset->code_space[(dim - 1 - idx) * 4 + 1]) | |
5505 | break; | |
5506 | idx++; | |
5507 | } | |
5508 | if (idx == dim) | |
5509 | { | |
5510 | val = Qnil; | |
5511 | break; | |
5512 | } | |
5513 | } | |
5514 | if (CONSP (val)) | |
5515 | break; | |
5516 | } | |
df7492f9 | 5517 | } |
716b3fa0 | 5518 | too_short: |
ff0dacd7 | 5519 | detect_info->rejected |= CATEGORY_MASK_CHARSET; |
df7492f9 | 5520 | return 0; |
4ed46869 | 5521 | |
df7492f9 | 5522 | no_more_source: |
ff0dacd7 KH |
5523 | detect_info->found |= found; |
5524 | return 1; | |
df7492f9 | 5525 | } |
b73bfc1c | 5526 | |
b73bfc1c | 5527 | static void |
971de7fb | 5528 | decode_coding_charset (struct coding_system *coding) |
4ed46869 | 5529 | { |
8f924df7 KH |
5530 | const unsigned char *src = coding->source + coding->consumed; |
5531 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
5532 | const unsigned char *src_base; | |
69a80ea3 | 5533 | int *charbuf = coding->charbuf + coding->charbuf_used; |
ad1746f5 | 5534 | /* We may produce one charset annotation in one loop and one more at |
df80c7f0 | 5535 | the end. */ |
69a80ea3 | 5536 | int *charbuf_end |
df80c7f0 | 5537 | = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 2); |
d311d28c | 5538 | ptrdiff_t consumed_chars = 0, consumed_chars_base; |
f10fe38f | 5539 | bool multibytep = coding->src_multibyte; |
66ebf983 PE |
5540 | Lisp_Object attrs = CODING_ID_ATTRS (coding->id); |
5541 | Lisp_Object valids; | |
d311d28c PE |
5542 | ptrdiff_t char_offset = coding->produced_char; |
5543 | ptrdiff_t last_offset = char_offset; | |
ff0dacd7 | 5544 | int last_id = charset_ascii; |
f10fe38f PE |
5545 | bool eol_dos |
5546 | = !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); | |
119852e7 | 5547 | int byte_after_cr = -1; |
df7492f9 | 5548 | |
4eb6d3f1 | 5549 | valids = AREF (attrs, coding_attr_charset_valids); |
b73bfc1c | 5550 | |
df7492f9 | 5551 | while (1) |
4ed46869 | 5552 | { |
4eb6d3f1 | 5553 | int c; |
24a73b0a KH |
5554 | Lisp_Object val; |
5555 | struct charset *charset; | |
5556 | int dim; | |
5557 | int len = 1; | |
5558 | unsigned code; | |
df7492f9 KH |
5559 | |
5560 | src_base = src; | |
5561 | consumed_chars_base = consumed_chars; | |
b73bfc1c | 5562 | |
df7492f9 | 5563 | if (charbuf >= charbuf_end) |
b71f6f73 KH |
5564 | { |
5565 | if (byte_after_cr >= 0) | |
5566 | src_base--; | |
5567 | break; | |
5568 | } | |
df7492f9 | 5569 | |
119852e7 KH |
5570 | if (byte_after_cr >= 0) |
5571 | { | |
5572 | c = byte_after_cr; | |
5573 | byte_after_cr = -1; | |
5574 | } | |
5575 | else | |
5576 | { | |
5577 | ONE_MORE_BYTE (c); | |
2735d060 | 5578 | if (eol_dos && c == '\r') |
119852e7 KH |
5579 | ONE_MORE_BYTE (byte_after_cr); |
5580 | } | |
065e3595 KH |
5581 | if (c < 0) |
5582 | goto invalid_code; | |
24a73b0a KH |
5583 | code = c; |
5584 | ||
5585 | val = AREF (valids, c); | |
1b17adfd | 5586 | if (! INTEGERP (val) && ! CONSP (val)) |
24a73b0a KH |
5587 | goto invalid_code; |
5588 | if (INTEGERP (val)) | |
d46c5b12 | 5589 | { |
24a73b0a KH |
5590 | charset = CHARSET_FROM_ID (XFASTINT (val)); |
5591 | dim = CHARSET_DIMENSION (charset); | |
5592 | while (len < dim) | |
b73bfc1c | 5593 | { |
24a73b0a KH |
5594 | ONE_MORE_BYTE (c); |
5595 | code = (code << 8) | c; | |
5596 | len++; | |
b73bfc1c | 5597 | } |
24a73b0a KH |
5598 | CODING_DECODE_CHAR (coding, src, src_base, src_end, |
5599 | charset, code, c); | |
d46c5b12 | 5600 | } |
df7492f9 | 5601 | else |
d46c5b12 | 5602 | { |
24a73b0a KH |
5603 | /* VAL is a list of charset IDs. It is assured that the |
5604 | list is sorted by charset dimensions (smaller one | |
5605 | comes first). */ | |
5606 | while (CONSP (val)) | |
4eb6d3f1 | 5607 | { |
24a73b0a | 5608 | charset = CHARSET_FROM_ID (XFASTINT (XCAR (val))); |
c7c66a95 | 5609 | dim = CHARSET_DIMENSION (charset); |
f9d71dcd | 5610 | while (len < dim) |
4eb6d3f1 | 5611 | { |
acb2a965 KH |
5612 | ONE_MORE_BYTE (c); |
5613 | code = (code << 8) | c; | |
f9d71dcd | 5614 | len++; |
4eb6d3f1 | 5615 | } |
24a73b0a KH |
5616 | CODING_DECODE_CHAR (coding, src, src_base, |
5617 | src_end, charset, code, c); | |
5618 | if (c >= 0) | |
5619 | break; | |
5620 | val = XCDR (val); | |
ff0dacd7 | 5621 | } |
d46c5b12 | 5622 | } |
24a73b0a KH |
5623 | if (c < 0) |
5624 | goto invalid_code; | |
5625 | if (charset->id != charset_ascii | |
5626 | && last_id != charset->id) | |
5627 | { | |
5628 | if (last_id != charset_ascii) | |
69a80ea3 | 5629 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
24a73b0a KH |
5630 | last_id = charset->id; |
5631 | last_offset = char_offset; | |
5632 | } | |
5633 | ||
df7492f9 | 5634 | *charbuf++ = c; |
ff0dacd7 | 5635 | char_offset++; |
df7492f9 KH |
5636 | continue; |
5637 | ||
5638 | invalid_code: | |
5639 | src = src_base; | |
5640 | consumed_chars = consumed_chars_base; | |
5641 | ONE_MORE_BYTE (c); | |
065e3595 | 5642 | *charbuf++ = c < 0 ? -c : ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 5643 | char_offset++; |
df7492f9 | 5644 | coding->errors++; |
4ed46869 KH |
5645 | } |
5646 | ||
df7492f9 | 5647 | no_more_source: |
ff0dacd7 | 5648 | if (last_id != charset_ascii) |
69a80ea3 | 5649 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
5650 | coding->consumed_char += consumed_chars_base; |
5651 | coding->consumed = src_base - coding->source; | |
5652 | coding->charbuf_used = charbuf - coding->charbuf; | |
4ed46869 KH |
5653 | } |
5654 | ||
f10fe38f | 5655 | static bool |
971de7fb | 5656 | encode_coding_charset (struct coding_system *coding) |
4ed46869 | 5657 | { |
f10fe38f | 5658 | bool multibytep = coding->dst_multibyte; |
df7492f9 KH |
5659 | int *charbuf = coding->charbuf; |
5660 | int *charbuf_end = charbuf + coding->charbuf_used; | |
5661 | unsigned char *dst = coding->destination + coding->produced; | |
5662 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
5663 | int safe_room = MAX_MULTIBYTE_LENGTH; | |
d311d28c | 5664 | ptrdiff_t produced_chars = 0; |
24a73b0a | 5665 | Lisp_Object attrs, charset_list; |
f10fe38f | 5666 | bool ascii_compatible; |
b73bfc1c | 5667 | int c; |
b73bfc1c | 5668 | |
24a73b0a | 5669 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 | 5670 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); |
fb88bf2d | 5671 | |
df7492f9 | 5672 | while (charbuf < charbuf_end) |
4ed46869 | 5673 | { |
4eb6d3f1 | 5674 | struct charset *charset; |
df7492f9 | 5675 | unsigned code; |
8f924df7 | 5676 | |
df7492f9 KH |
5677 | ASSURE_DESTINATION (safe_room); |
5678 | c = *charbuf++; | |
5679 | if (ascii_compatible && ASCII_CHAR_P (c)) | |
5680 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d | 5681 | else if (CHAR_BYTE8_P (c)) |
4ed46869 | 5682 | { |
16eafb5d KH |
5683 | c = CHAR_TO_BYTE8 (c); |
5684 | EMIT_ONE_BYTE (c); | |
d46c5b12 | 5685 | } |
d46c5b12 | 5686 | else |
b73bfc1c | 5687 | { |
5eb05ea3 KH |
5688 | CODING_CHAR_CHARSET (coding, dst, dst_end, c, charset_list, |
5689 | &code, charset); | |
5690 | ||
4eb6d3f1 KH |
5691 | if (charset) |
5692 | { | |
5693 | if (CHARSET_DIMENSION (charset) == 1) | |
5694 | EMIT_ONE_BYTE (code); | |
5695 | else if (CHARSET_DIMENSION (charset) == 2) | |
5696 | EMIT_TWO_BYTES (code >> 8, code & 0xFF); | |
5697 | else if (CHARSET_DIMENSION (charset) == 3) | |
5698 | EMIT_THREE_BYTES (code >> 16, (code >> 8) & 0xFF, code & 0xFF); | |
5699 | else | |
5700 | EMIT_FOUR_BYTES (code >> 24, (code >> 16) & 0xFF, | |
5701 | (code >> 8) & 0xFF, code & 0xFF); | |
5702 | } | |
5703 | else | |
41cbe562 KH |
5704 | { |
5705 | if (coding->mode & CODING_MODE_SAFE_ENCODING) | |
5706 | c = CODING_INHIBIT_CHARACTER_SUBSTITUTION; | |
5707 | else | |
5708 | c = coding->default_char; | |
5709 | EMIT_ONE_BYTE (c); | |
5710 | } | |
4ed46869 | 5711 | } |
4ed46869 KH |
5712 | } |
5713 | ||
065e3595 | 5714 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
5715 | coding->produced_char += produced_chars; |
5716 | coding->produced = dst - coding->destination; | |
5717 | return 0; | |
4ed46869 KH |
5718 | } |
5719 | ||
5720 | \f | |
1397dc18 | 5721 | /*** 7. C library functions ***/ |
4ed46869 | 5722 | |
df7492f9 KH |
5723 | /* Setup coding context CODING from information about CODING_SYSTEM. |
5724 | If CODING_SYSTEM is nil, `no-conversion' is assumed. If | |
5725 | CODING_SYSTEM is invalid, signal an error. */ | |
4ed46869 | 5726 | |
ec6d2bb8 | 5727 | void |
971de7fb | 5728 | setup_coding_system (Lisp_Object coding_system, struct coding_system *coding) |
4ed46869 | 5729 | { |
df7492f9 KH |
5730 | Lisp_Object attrs; |
5731 | Lisp_Object eol_type; | |
5732 | Lisp_Object coding_type; | |
4608c386 | 5733 | Lisp_Object val; |
4ed46869 | 5734 | |
df7492f9 | 5735 | if (NILP (coding_system)) |
ae6f73fa | 5736 | coding_system = Qundecided; |
c07c8e12 | 5737 | |
df7492f9 | 5738 | CHECK_CODING_SYSTEM_GET_ID (coding_system, coding->id); |
1f5dbf34 | 5739 | |
df7492f9 | 5740 | attrs = CODING_ID_ATTRS (coding->id); |
0a9564cb | 5741 | eol_type = inhibit_eol_conversion ? Qunix : CODING_ID_EOL_TYPE (coding->id); |
1f5dbf34 | 5742 | |
df7492f9 | 5743 | coding->mode = 0; |
4a015c45 KH |
5744 | if (VECTORP (eol_type)) |
5745 | coding->common_flags = (CODING_REQUIRE_DECODING_MASK | |
5746 | | CODING_REQUIRE_DETECTION_MASK); | |
5747 | else if (! EQ (eol_type, Qunix)) | |
5748 | coding->common_flags = (CODING_REQUIRE_DECODING_MASK | |
5749 | | CODING_REQUIRE_ENCODING_MASK); | |
5750 | else | |
5751 | coding->common_flags = 0; | |
5e5c78be KH |
5752 | if (! NILP (CODING_ATTR_POST_READ (attrs))) |
5753 | coding->common_flags |= CODING_REQUIRE_DECODING_MASK; | |
5754 | if (! NILP (CODING_ATTR_PRE_WRITE (attrs))) | |
5755 | coding->common_flags |= CODING_REQUIRE_ENCODING_MASK; | |
8f924df7 KH |
5756 | if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs))) |
5757 | coding->common_flags |= CODING_FOR_UNIBYTE_MASK; | |
1f5dbf34 | 5758 | |
df7492f9 | 5759 | val = CODING_ATTR_SAFE_CHARSETS (attrs); |
8f924df7 | 5760 | coding->max_charset_id = SCHARS (val) - 1; |
1b3b981b | 5761 | coding->safe_charsets = SDATA (val); |
df7492f9 | 5762 | coding->default_char = XINT (CODING_ATTR_DEFAULT_CHAR (attrs)); |
624bda09 | 5763 | coding->carryover_bytes = 0; |
4608c386 | 5764 | |
df7492f9 KH |
5765 | coding_type = CODING_ATTR_TYPE (attrs); |
5766 | if (EQ (coding_type, Qundecided)) | |
d46c5b12 | 5767 | { |
df7492f9 KH |
5768 | coding->detector = NULL; |
5769 | coding->decoder = decode_coding_raw_text; | |
5770 | coding->encoder = encode_coding_raw_text; | |
5771 | coding->common_flags |= CODING_REQUIRE_DETECTION_MASK; | |
c3a7b3f4 | 5772 | coding->spec.undecided.inhibit_nbd |
9c90cc06 PE |
5773 | = (encode_inhibit_flag |
5774 | (AREF (attrs, coding_attr_undecided_inhibit_null_byte_detection))); | |
c3a7b3f4 | 5775 | coding->spec.undecided.inhibit_ied |
9c90cc06 PE |
5776 | = (encode_inhibit_flag |
5777 | (AREF (attrs, coding_attr_undecided_inhibit_iso_escape_detection))); | |
c3a7b3f4 KH |
5778 | coding->spec.undecided.prefer_utf_8 |
5779 | = ! NILP (AREF (attrs, coding_attr_undecided_prefer_utf_8)); | |
d46c5b12 | 5780 | } |
df7492f9 | 5781 | else if (EQ (coding_type, Qiso_2022)) |
d46c5b12 | 5782 | { |
df7492f9 KH |
5783 | int i; |
5784 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); | |
5785 | ||
5786 | /* Invoke graphic register 0 to plane 0. */ | |
5787 | CODING_ISO_INVOCATION (coding, 0) = 0; | |
5788 | /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */ | |
5789 | CODING_ISO_INVOCATION (coding, 1) | |
5790 | = (flags & CODING_ISO_FLAG_SEVEN_BITS ? -1 : 1); | |
5791 | /* Setup the initial status of designation. */ | |
5792 | for (i = 0; i < 4; i++) | |
5793 | CODING_ISO_DESIGNATION (coding, i) = CODING_ISO_INITIAL (coding, i); | |
5794 | /* Not single shifting initially. */ | |
5795 | CODING_ISO_SINGLE_SHIFTING (coding) = 0; | |
5796 | /* Beginning of buffer should also be regarded as bol. */ | |
5797 | CODING_ISO_BOL (coding) = 1; | |
5798 | coding->detector = detect_coding_iso_2022; | |
5799 | coding->decoder = decode_coding_iso_2022; | |
5800 | coding->encoder = encode_coding_iso_2022; | |
5801 | if (flags & CODING_ISO_FLAG_SAFE) | |
5802 | coding->mode |= CODING_MODE_SAFE_ENCODING; | |
d46c5b12 | 5803 | coding->common_flags |
df7492f9 KH |
5804 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK |
5805 | | CODING_REQUIRE_FLUSHING_MASK); | |
5806 | if (flags & CODING_ISO_FLAG_COMPOSITION) | |
5807 | coding->common_flags |= CODING_ANNOTATE_COMPOSITION_MASK; | |
ff0dacd7 KH |
5808 | if (flags & CODING_ISO_FLAG_DESIGNATION) |
5809 | coding->common_flags |= CODING_ANNOTATE_CHARSET_MASK; | |
df7492f9 KH |
5810 | if (flags & CODING_ISO_FLAG_FULL_SUPPORT) |
5811 | { | |
5812 | setup_iso_safe_charsets (attrs); | |
5813 | val = CODING_ATTR_SAFE_CHARSETS (attrs); | |
8f924df7 | 5814 | coding->max_charset_id = SCHARS (val) - 1; |
1b3b981b | 5815 | coding->safe_charsets = SDATA (val); |
df7492f9 KH |
5816 | } |
5817 | CODING_ISO_FLAGS (coding) = flags; | |
e951386e KH |
5818 | CODING_ISO_CMP_STATUS (coding)->state = COMPOSING_NO; |
5819 | CODING_ISO_CMP_STATUS (coding)->method = COMPOSITION_NO; | |
5820 | CODING_ISO_EXTSEGMENT_LEN (coding) = 0; | |
5821 | CODING_ISO_EMBEDDED_UTF_8 (coding) = 0; | |
d46c5b12 | 5822 | } |
df7492f9 | 5823 | else if (EQ (coding_type, Qcharset)) |
d46c5b12 | 5824 | { |
df7492f9 KH |
5825 | coding->detector = detect_coding_charset; |
5826 | coding->decoder = decode_coding_charset; | |
5827 | coding->encoder = encode_coding_charset; | |
d46c5b12 | 5828 | coding->common_flags |
df7492f9 | 5829 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
d46c5b12 | 5830 | } |
df7492f9 | 5831 | else if (EQ (coding_type, Qutf_8)) |
d46c5b12 | 5832 | { |
a470d443 KH |
5833 | val = AREF (attrs, coding_attr_utf_bom); |
5834 | CODING_UTF_8_BOM (coding) = (CONSP (val) ? utf_detect_bom | |
5835 | : EQ (val, Qt) ? utf_with_bom | |
5836 | : utf_without_bom); | |
df7492f9 KH |
5837 | coding->detector = detect_coding_utf_8; |
5838 | coding->decoder = decode_coding_utf_8; | |
5839 | coding->encoder = encode_coding_utf_8; | |
5840 | coding->common_flags | |
5841 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); | |
a470d443 KH |
5842 | if (CODING_UTF_8_BOM (coding) == utf_detect_bom) |
5843 | coding->common_flags |= CODING_REQUIRE_DETECTION_MASK; | |
df7492f9 KH |
5844 | } |
5845 | else if (EQ (coding_type, Qutf_16)) | |
5846 | { | |
a470d443 KH |
5847 | val = AREF (attrs, coding_attr_utf_bom); |
5848 | CODING_UTF_16_BOM (coding) = (CONSP (val) ? utf_detect_bom | |
5849 | : EQ (val, Qt) ? utf_with_bom | |
5850 | : utf_without_bom); | |
df7492f9 | 5851 | val = AREF (attrs, coding_attr_utf_16_endian); |
b49a1807 | 5852 | CODING_UTF_16_ENDIAN (coding) = (EQ (val, Qbig) ? utf_16_big_endian |
df7492f9 | 5853 | : utf_16_little_endian); |
e19c3639 | 5854 | CODING_UTF_16_SURROGATE (coding) = 0; |
df7492f9 KH |
5855 | coding->detector = detect_coding_utf_16; |
5856 | coding->decoder = decode_coding_utf_16; | |
5857 | coding->encoder = encode_coding_utf_16; | |
5858 | coding->common_flags | |
5859 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); | |
a470d443 | 5860 | if (CODING_UTF_16_BOM (coding) == utf_detect_bom) |
b49a1807 | 5861 | coding->common_flags |= CODING_REQUIRE_DETECTION_MASK; |
d46c5b12 | 5862 | } |
df7492f9 | 5863 | else if (EQ (coding_type, Qccl)) |
4ed46869 | 5864 | { |
df7492f9 KH |
5865 | coding->detector = detect_coding_ccl; |
5866 | coding->decoder = decode_coding_ccl; | |
5867 | coding->encoder = encode_coding_ccl; | |
c952af22 | 5868 | coding->common_flags |
df7492f9 KH |
5869 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK |
5870 | | CODING_REQUIRE_FLUSHING_MASK); | |
5871 | } | |
5872 | else if (EQ (coding_type, Qemacs_mule)) | |
5873 | { | |
5874 | coding->detector = detect_coding_emacs_mule; | |
5875 | coding->decoder = decode_coding_emacs_mule; | |
5876 | coding->encoder = encode_coding_emacs_mule; | |
c952af22 | 5877 | coding->common_flags |
df7492f9 KH |
5878 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
5879 | if (! NILP (AREF (attrs, coding_attr_emacs_mule_full)) | |
5880 | && ! EQ (CODING_ATTR_CHARSET_LIST (attrs), Vemacs_mule_charset_list)) | |
5881 | { | |
5882 | Lisp_Object tail, safe_charsets; | |
5883 | int max_charset_id = 0; | |
5884 | ||
5885 | for (tail = Vemacs_mule_charset_list; CONSP (tail); | |
5886 | tail = XCDR (tail)) | |
5887 | if (max_charset_id < XFASTINT (XCAR (tail))) | |
5888 | max_charset_id = XFASTINT (XCAR (tail)); | |
1b3b981b AS |
5889 | safe_charsets = make_uninit_string (max_charset_id + 1); |
5890 | memset (SDATA (safe_charsets), 255, max_charset_id + 1); | |
df7492f9 KH |
5891 | for (tail = Vemacs_mule_charset_list; CONSP (tail); |
5892 | tail = XCDR (tail)) | |
8f924df7 | 5893 | SSET (safe_charsets, XFASTINT (XCAR (tail)), 0); |
df7492f9 | 5894 | coding->max_charset_id = max_charset_id; |
1b3b981b | 5895 | coding->safe_charsets = SDATA (safe_charsets); |
df7492f9 | 5896 | } |
e951386e KH |
5897 | coding->spec.emacs_mule.cmp_status.state = COMPOSING_NO; |
5898 | coding->spec.emacs_mule.cmp_status.method = COMPOSITION_NO; | |
df7492f9 KH |
5899 | } |
5900 | else if (EQ (coding_type, Qshift_jis)) | |
5901 | { | |
5902 | coding->detector = detect_coding_sjis; | |
5903 | coding->decoder = decode_coding_sjis; | |
5904 | coding->encoder = encode_coding_sjis; | |
c952af22 | 5905 | coding->common_flags |
df7492f9 KH |
5906 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
5907 | } | |
5908 | else if (EQ (coding_type, Qbig5)) | |
5909 | { | |
5910 | coding->detector = detect_coding_big5; | |
5911 | coding->decoder = decode_coding_big5; | |
5912 | coding->encoder = encode_coding_big5; | |
c952af22 | 5913 | coding->common_flags |
df7492f9 KH |
5914 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
5915 | } | |
5916 | else /* EQ (coding_type, Qraw_text) */ | |
ec6d2bb8 | 5917 | { |
df7492f9 KH |
5918 | coding->detector = NULL; |
5919 | coding->decoder = decode_coding_raw_text; | |
5920 | coding->encoder = encode_coding_raw_text; | |
ea29edf2 KH |
5921 | if (! EQ (eol_type, Qunix)) |
5922 | { | |
5923 | coding->common_flags |= CODING_REQUIRE_DECODING_MASK; | |
5924 | if (! VECTORP (eol_type)) | |
5925 | coding->common_flags |= CODING_REQUIRE_ENCODING_MASK; | |
5926 | } | |
5927 | ||
4ed46869 | 5928 | } |
4ed46869 | 5929 | |
df7492f9 | 5930 | return; |
4ed46869 KH |
5931 | } |
5932 | ||
0ff61e78 KH |
5933 | /* Return a list of charsets supported by CODING. */ |
5934 | ||
5935 | Lisp_Object | |
971de7fb | 5936 | coding_charset_list (struct coding_system *coding) |
0ff61e78 | 5937 | { |
35befdaa | 5938 | Lisp_Object attrs, charset_list; |
0ff61e78 KH |
5939 | |
5940 | CODING_GET_INFO (coding, attrs, charset_list); | |
5941 | if (EQ (CODING_ATTR_TYPE (attrs), Qiso_2022)) | |
5942 | { | |
5943 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); | |
5944 | ||
5945 | if (flags & CODING_ISO_FLAG_FULL_SUPPORT) | |
5946 | charset_list = Viso_2022_charset_list; | |
5947 | } | |
5948 | else if (EQ (CODING_ATTR_TYPE (attrs), Qemacs_mule)) | |
5949 | { | |
5950 | charset_list = Vemacs_mule_charset_list; | |
5951 | } | |
5952 | return charset_list; | |
5953 | } | |
5954 | ||
5955 | ||
e9f91ece KH |
5956 | /* Return a list of charsets supported by CODING-SYSTEM. */ |
5957 | ||
5958 | Lisp_Object | |
971de7fb | 5959 | coding_system_charset_list (Lisp_Object coding_system) |
e9f91ece | 5960 | { |
d3411f89 | 5961 | ptrdiff_t id; |
e9f91ece KH |
5962 | Lisp_Object attrs, charset_list; |
5963 | ||
5964 | CHECK_CODING_SYSTEM_GET_ID (coding_system, id); | |
5965 | attrs = CODING_ID_ATTRS (id); | |
5966 | ||
5967 | if (EQ (CODING_ATTR_TYPE (attrs), Qiso_2022)) | |
5968 | { | |
5969 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); | |
5970 | ||
5971 | if (flags & CODING_ISO_FLAG_FULL_SUPPORT) | |
5972 | charset_list = Viso_2022_charset_list; | |
5973 | else | |
5974 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
5975 | } | |
5976 | else if (EQ (CODING_ATTR_TYPE (attrs), Qemacs_mule)) | |
5977 | { | |
5978 | charset_list = Vemacs_mule_charset_list; | |
5979 | } | |
5980 | else | |
5981 | { | |
5982 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
5983 | } | |
5984 | return charset_list; | |
5985 | } | |
5986 | ||
5987 | ||
df7492f9 KH |
5988 | /* Return raw-text or one of its subsidiaries that has the same |
5989 | eol_type as CODING-SYSTEM. */ | |
ec6d2bb8 | 5990 | |
df7492f9 | 5991 | Lisp_Object |
971de7fb | 5992 | raw_text_coding_system (Lisp_Object coding_system) |
ec6d2bb8 | 5993 | { |
0be8721c | 5994 | Lisp_Object spec, attrs; |
df7492f9 | 5995 | Lisp_Object eol_type, raw_text_eol_type; |
ec6d2bb8 | 5996 | |
d3e4cb56 KH |
5997 | if (NILP (coding_system)) |
5998 | return Qraw_text; | |
df7492f9 KH |
5999 | spec = CODING_SYSTEM_SPEC (coding_system); |
6000 | attrs = AREF (spec, 0); | |
ec6d2bb8 | 6001 | |
df7492f9 KH |
6002 | if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text)) |
6003 | return coding_system; | |
ec6d2bb8 | 6004 | |
df7492f9 KH |
6005 | eol_type = AREF (spec, 2); |
6006 | if (VECTORP (eol_type)) | |
6007 | return Qraw_text; | |
6008 | spec = CODING_SYSTEM_SPEC (Qraw_text); | |
6009 | raw_text_eol_type = AREF (spec, 2); | |
6010 | return (EQ (eol_type, Qunix) ? AREF (raw_text_eol_type, 0) | |
6011 | : EQ (eol_type, Qdos) ? AREF (raw_text_eol_type, 1) | |
6012 | : AREF (raw_text_eol_type, 2)); | |
ec6d2bb8 KH |
6013 | } |
6014 | ||
54f78171 | 6015 | |
1911a33b KH |
6016 | /* If CODING_SYSTEM doesn't specify end-of-line format, return one of |
6017 | the subsidiary that has the same eol-spec as PARENT (if it is not | |
6018 | nil and specifies end-of-line format) or the system's setting | |
fcbcfb64 | 6019 | (system_eol_type). */ |
df7492f9 KH |
6020 | |
6021 | Lisp_Object | |
971de7fb | 6022 | coding_inherit_eol_type (Lisp_Object coding_system, Lisp_Object parent) |
54f78171 | 6023 | { |
3e139625 | 6024 | Lisp_Object spec, eol_type; |
54f78171 | 6025 | |
d3e4cb56 KH |
6026 | if (NILP (coding_system)) |
6027 | coding_system = Qraw_text; | |
df7492f9 | 6028 | spec = CODING_SYSTEM_SPEC (coding_system); |
df7492f9 | 6029 | eol_type = AREF (spec, 2); |
fcbcfb64 | 6030 | if (VECTORP (eol_type)) |
df7492f9 | 6031 | { |
df7492f9 KH |
6032 | Lisp_Object parent_eol_type; |
6033 | ||
fcbcfb64 KH |
6034 | if (! NILP (parent)) |
6035 | { | |
6036 | Lisp_Object parent_spec; | |
6037 | ||
4a015c45 | 6038 | parent_spec = CODING_SYSTEM_SPEC (parent); |
fcbcfb64 | 6039 | parent_eol_type = AREF (parent_spec, 2); |
1911a33b | 6040 | if (VECTORP (parent_eol_type)) |
4628bef1 | 6041 | parent_eol_type = system_eol_type; |
fcbcfb64 KH |
6042 | } |
6043 | else | |
6044 | parent_eol_type = system_eol_type; | |
df7492f9 KH |
6045 | if (EQ (parent_eol_type, Qunix)) |
6046 | coding_system = AREF (eol_type, 0); | |
6047 | else if (EQ (parent_eol_type, Qdos)) | |
6048 | coding_system = AREF (eol_type, 1); | |
6049 | else if (EQ (parent_eol_type, Qmac)) | |
6050 | coding_system = AREF (eol_type, 2); | |
54f78171 | 6051 | } |
df7492f9 | 6052 | return coding_system; |
54f78171 KH |
6053 | } |
6054 | ||
fcaf8878 KH |
6055 | |
6056 | /* Check if text-conversion and eol-conversion of CODING_SYSTEM are | |
6057 | decided for writing to a process. If not, complement them, and | |
6058 | return a new coding system. */ | |
6059 | ||
6060 | Lisp_Object | |
4628bef1 | 6061 | complement_process_encoding_system (Lisp_Object coding_system) |
fcaf8878 | 6062 | { |
5886ec9c KH |
6063 | Lisp_Object coding_base = Qnil, eol_base = Qnil; |
6064 | Lisp_Object spec, attrs; | |
93d50df8 | 6065 | int i; |
fcaf8878 | 6066 | |
93d50df8 | 6067 | for (i = 0; i < 3; i++) |
fcaf8878 | 6068 | { |
93d50df8 KH |
6069 | if (i == 1) |
6070 | coding_system = CDR_SAFE (Vdefault_process_coding_system); | |
6071 | else if (i == 2) | |
6072 | coding_system = preferred_coding_system (); | |
6073 | spec = CODING_SYSTEM_SPEC (coding_system); | |
6074 | if (NILP (spec)) | |
6075 | continue; | |
6076 | attrs = AREF (spec, 0); | |
6077 | if (NILP (coding_base) && ! EQ (CODING_ATTR_TYPE (attrs), Qundecided)) | |
6078 | coding_base = CODING_ATTR_BASE_NAME (attrs); | |
6079 | if (NILP (eol_base) && ! VECTORP (AREF (spec, 2))) | |
6080 | eol_base = coding_system; | |
6081 | if (! NILP (coding_base) && ! NILP (eol_base)) | |
6082 | break; | |
fcaf8878 | 6083 | } |
fcaf8878 | 6084 | |
93d50df8 KH |
6085 | if (i > 0) |
6086 | /* The original CODING_SYSTEM didn't specify text-conversion or | |
6087 | eol-conversion. Be sure that we return a fully complemented | |
6088 | coding system. */ | |
6089 | coding_system = coding_inherit_eol_type (coding_base, eol_base); | |
6090 | return coding_system; | |
fcaf8878 KH |
6091 | } |
6092 | ||
6093 | ||
4ed46869 KH |
6094 | /* Emacs has a mechanism to automatically detect a coding system if it |
6095 | is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But, | |
6096 | it's impossible to distinguish some coding systems accurately | |
6097 | because they use the same range of codes. So, at first, coding | |
6098 | systems are categorized into 7, those are: | |
6099 | ||
0ef69138 | 6100 | o coding-category-emacs-mule |
4ed46869 KH |
6101 | |
6102 | The category for a coding system which has the same code range | |
6103 | as Emacs' internal format. Assigned the coding-system (Lisp | |
0ef69138 | 6104 | symbol) `emacs-mule' by default. |
4ed46869 KH |
6105 | |
6106 | o coding-category-sjis | |
6107 | ||
6108 | The category for a coding system which has the same code range | |
6109 | as SJIS. Assigned the coding-system (Lisp | |
7717c392 | 6110 | symbol) `japanese-shift-jis' by default. |
4ed46869 KH |
6111 | |
6112 | o coding-category-iso-7 | |
6113 | ||
6114 | The category for a coding system which has the same code range | |
7717c392 | 6115 | as ISO2022 of 7-bit environment. This doesn't use any locking |
d46c5b12 KH |
6116 | shift and single shift functions. This can encode/decode all |
6117 | charsets. Assigned the coding-system (Lisp symbol) | |
6118 | `iso-2022-7bit' by default. | |
6119 | ||
6120 | o coding-category-iso-7-tight | |
6121 | ||
6122 | Same as coding-category-iso-7 except that this can | |
6123 | encode/decode only the specified charsets. | |
4ed46869 KH |
6124 | |
6125 | o coding-category-iso-8-1 | |
6126 | ||
6127 | The category for a coding system which has the same code range | |
6128 | as ISO2022 of 8-bit environment and graphic plane 1 used only | |
7717c392 KH |
6129 | for DIMENSION1 charset. This doesn't use any locking shift |
6130 | and single shift functions. Assigned the coding-system (Lisp | |
6131 | symbol) `iso-latin-1' by default. | |
4ed46869 KH |
6132 | |
6133 | o coding-category-iso-8-2 | |
6134 | ||
6135 | The category for a coding system which has the same code range | |
6136 | as ISO2022 of 8-bit environment and graphic plane 1 used only | |
7717c392 KH |
6137 | for DIMENSION2 charset. This doesn't use any locking shift |
6138 | and single shift functions. Assigned the coding-system (Lisp | |
6139 | symbol) `japanese-iso-8bit' by default. | |
4ed46869 | 6140 | |
7717c392 | 6141 | o coding-category-iso-7-else |
4ed46869 KH |
6142 | |
6143 | The category for a coding system which has the same code range | |
ad1746f5 | 6144 | as ISO2022 of 7-bit environment but uses locking shift or |
7717c392 KH |
6145 | single shift functions. Assigned the coding-system (Lisp |
6146 | symbol) `iso-2022-7bit-lock' by default. | |
6147 | ||
6148 | o coding-category-iso-8-else | |
6149 | ||
6150 | The category for a coding system which has the same code range | |
ad1746f5 | 6151 | as ISO2022 of 8-bit environment but uses locking shift or |
7717c392 KH |
6152 | single shift functions. Assigned the coding-system (Lisp |
6153 | symbol) `iso-2022-8bit-ss2' by default. | |
4ed46869 KH |
6154 | |
6155 | o coding-category-big5 | |
6156 | ||
6157 | The category for a coding system which has the same code range | |
6158 | as BIG5. Assigned the coding-system (Lisp symbol) | |
e0e989f6 | 6159 | `cn-big5' by default. |
4ed46869 | 6160 | |
fa42c37f KH |
6161 | o coding-category-utf-8 |
6162 | ||
6163 | The category for a coding system which has the same code range | |
6e76ae91 | 6164 | as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp |
fa42c37f KH |
6165 | symbol) `utf-8' by default. |
6166 | ||
6167 | o coding-category-utf-16-be | |
6168 | ||
6169 | The category for a coding system in which a text has an | |
6170 | Unicode signature (cf. Unicode Standard) in the order of BIG | |
6171 | endian at the head. Assigned the coding-system (Lisp symbol) | |
6172 | `utf-16-be' by default. | |
6173 | ||
6174 | o coding-category-utf-16-le | |
6175 | ||
6176 | The category for a coding system in which a text has an | |
6177 | Unicode signature (cf. Unicode Standard) in the order of | |
6178 | LITTLE endian at the head. Assigned the coding-system (Lisp | |
6179 | symbol) `utf-16-le' by default. | |
6180 | ||
1397dc18 KH |
6181 | o coding-category-ccl |
6182 | ||
6183 | The category for a coding system of which encoder/decoder is | |
6184 | written in CCL programs. The default value is nil, i.e., no | |
6185 | coding system is assigned. | |
6186 | ||
4ed46869 KH |
6187 | o coding-category-binary |
6188 | ||
6189 | The category for a coding system not categorized in any of the | |
6190 | above. Assigned the coding-system (Lisp symbol) | |
e0e989f6 | 6191 | `no-conversion' by default. |
4ed46869 KH |
6192 | |
6193 | Each of them is a Lisp symbol and the value is an actual | |
df7492f9 | 6194 | `coding-system's (this is also a Lisp symbol) assigned by a user. |
4ed46869 KH |
6195 | What Emacs does actually is to detect a category of coding system. |
6196 | Then, it uses a `coding-system' assigned to it. If Emacs can't | |
df7492f9 | 6197 | decide only one possible category, it selects a category of the |
4ed46869 KH |
6198 | highest priority. Priorities of categories are also specified by a |
6199 | user in a Lisp variable `coding-category-list'. | |
6200 | ||
6201 | */ | |
6202 | ||
251e9147 KH |
6203 | static Lisp_Object adjust_coding_eol_type (struct coding_system *coding, |
6204 | int eol_seen); | |
8a44e6d1 KH |
6205 | |
6206 | ||
251e9147 | 6207 | /* Return the number of ASCII characters at the head of the source. |
e6d2f155 KH |
6208 | By side effects, set coding->head_ascii and update |
6209 | coding->eol_seen. The value of coding->eol_seen is "logical or" of | |
6210 | EOL_SEEN_LF, EOL_SEEN_CR, and EOL_SEEN_CRLF, but the value is | |
6211 | reliable only when all the source bytes are ASCII. */ | |
8a44e6d1 | 6212 | |
251e9147 KH |
6213 | static int |
6214 | check_ascii (struct coding_system *coding) | |
8a44e6d1 KH |
6215 | { |
6216 | const unsigned char *src, *end; | |
6217 | Lisp_Object eol_type = CODING_ID_EOL_TYPE (coding->id); | |
e6d2f155 | 6218 | int eol_seen = coding->eol_seen; |
8a44e6d1 | 6219 | |
8a44e6d1 KH |
6220 | coding_set_source (coding); |
6221 | src = coding->source; | |
6222 | end = src + coding->src_bytes; | |
6223 | ||
251e9147 | 6224 | if (inhibit_eol_conversion |
e6d2f155 | 6225 | || SYMBOLP (eol_type)) |
8a44e6d1 KH |
6226 | { |
6227 | /* We don't have to check EOL format. */ | |
e6d2f155 | 6228 | while (src < end && !( *src & 0x80)) |
251e9147 | 6229 | { |
e6d2f155 KH |
6230 | if (*src++ == '\n') |
6231 | eol_seen |= EOL_SEEN_LF; | |
251e9147 | 6232 | } |
8a44e6d1 KH |
6233 | } |
6234 | else | |
6235 | { | |
251e9147 | 6236 | end--; /* We look ahead one byte for "CR LF". */ |
8a44e6d1 KH |
6237 | while (src < end) |
6238 | { | |
6239 | int c = *src; | |
6240 | ||
6241 | if (c & 0x80) | |
6242 | break; | |
6243 | src++; | |
251e9147 KH |
6244 | if (c == '\r') |
6245 | { | |
6246 | if (*src == '\n') | |
6247 | { | |
6248 | eol_seen |= EOL_SEEN_CRLF; | |
6249 | src++; | |
6250 | } | |
6251 | else | |
6252 | eol_seen |= EOL_SEEN_CR; | |
6253 | } | |
6254 | else if (c == '\n') | |
6255 | eol_seen |= EOL_SEEN_LF; | |
6256 | } | |
6257 | if (src == end) | |
6258 | { | |
6259 | int c = *src; | |
6260 | ||
6261 | /* All bytes but the last one C are ASCII. */ | |
6262 | if (! (c & 0x80)) | |
6263 | { | |
6264 | if (c == '\r') | |
6265 | eol_seen |= EOL_SEEN_CR; | |
6266 | else if (c == '\n') | |
6267 | eol_seen |= EOL_SEEN_LF; | |
6268 | src++; | |
6269 | } | |
6270 | } | |
6271 | } | |
6272 | coding->head_ascii = src - coding->source; | |
6273 | coding->eol_seen = eol_seen; | |
6274 | return (coding->head_ascii); | |
6275 | } | |
6276 | ||
6277 | ||
8ead3901 | 6278 | /* Return the number of characters at the source if all the bytes are |
251e9147 KH |
6279 | valid UTF-8 (of Unicode range). Otherwise, return -1. By side |
6280 | effects, update coding->eol_seen. The value of coding->eol_seen is | |
6281 | "logical or" of EOL_SEEN_LF, EOL_SEEN_CR, and EOL_SEEN_CRLF, but | |
6282 | the value is reliable only when all the source bytes are valid | |
6283 | UTF-8. */ | |
6284 | ||
6285 | static int | |
6286 | check_utf_8 (struct coding_system *coding) | |
6287 | { | |
6288 | const unsigned char *src, *end; | |
e6d2f155 | 6289 | int eol_seen; |
251e9147 KH |
6290 | int nchars = coding->head_ascii; |
6291 | ||
6292 | if (coding->head_ascii < 0) | |
6293 | check_ascii (coding); | |
6294 | else | |
6295 | coding_set_source (coding); | |
6296 | src = coding->source + coding->head_ascii; | |
6297 | /* We look ahead one byte for CR LF. */ | |
6298 | end = coding->source + coding->src_bytes - 1; | |
e6d2f155 | 6299 | eol_seen = coding->eol_seen; |
251e9147 KH |
6300 | while (src < end) |
6301 | { | |
6302 | int c = *src; | |
6303 | ||
6304 | if (UTF_8_1_OCTET_P (*src)) | |
6305 | { | |
6306 | src++; | |
8a44e6d1 KH |
6307 | if (c < 0x20) |
6308 | { | |
6309 | if (c == '\r') | |
6310 | { | |
6311 | if (*src == '\n') | |
6312 | { | |
6313 | eol_seen |= EOL_SEEN_CRLF; | |
6314 | src++; | |
251e9147 | 6315 | nchars++; |
8a44e6d1 KH |
6316 | } |
6317 | else | |
6318 | eol_seen |= EOL_SEEN_CR; | |
6319 | } | |
6320 | else if (c == '\n') | |
6321 | eol_seen |= EOL_SEEN_LF; | |
6322 | } | |
6323 | } | |
251e9147 | 6324 | else if (UTF_8_2_OCTET_LEADING_P (c)) |
8a44e6d1 | 6325 | { |
251e9147 KH |
6326 | if (c < 0xC2 /* overlong sequence */ |
6327 | || src + 1 >= end | |
6328 | || ! UTF_8_EXTRA_OCTET_P (src[1])) | |
6329 | return -1; | |
6330 | src += 2; | |
8a44e6d1 | 6331 | } |
251e9147 KH |
6332 | else if (UTF_8_3_OCTET_LEADING_P (c)) |
6333 | { | |
6334 | if (src + 2 >= end | |
6335 | || ! (UTF_8_EXTRA_OCTET_P (src[1]) | |
6336 | && UTF_8_EXTRA_OCTET_P (src[2]))) | |
6337 | return -1; | |
6338 | c = (((c & 0xF) << 12) | |
6339 | | ((src[1] & 0x3F) << 6) | (src[2] & 0x3F)); | |
6340 | if (c < 0x800 /* overlong sequence */ | |
6341 | || (c >= 0xd800 && c < 0xe000)) /* surrogates (invalid) */ | |
6342 | return -1; | |
6343 | src += 3; | |
6344 | } | |
6345 | else if (UTF_8_4_OCTET_LEADING_P (c)) | |
6346 | { | |
6347 | if (src + 3 >= end | |
6348 | || ! (UTF_8_EXTRA_OCTET_P (src[1]) | |
6349 | && UTF_8_EXTRA_OCTET_P (src[2]) | |
6350 | && UTF_8_EXTRA_OCTET_P (src[3]))) | |
6351 | return -1; | |
6352 | c = (((c & 0x7) << 18) | ((src[1] & 0x3F) << 12) | |
6353 | | ((src[2] & 0x3F) << 6) | (src[3] & 0x3F)); | |
6354 | if (c < 0x10000 /* overlong sequence */ | |
6355 | || c >= 0x110000) /* non-Unicode character */ | |
6356 | return -1; | |
6357 | src += 4; | |
6358 | } | |
6359 | else | |
6360 | return -1; | |
6361 | nchars++; | |
6362 | } | |
6363 | ||
6364 | if (src == end) | |
6365 | { | |
6366 | if (! UTF_8_1_OCTET_P (*src)) | |
6367 | return -1; | |
6368 | nchars++; | |
6369 | if (*src == '\r') | |
6370 | eol_seen |= EOL_SEEN_CR; | |
6371 | else if (*src == '\n') | |
6372 | eol_seen |= EOL_SEEN_LF; | |
8a44e6d1 | 6373 | } |
8a44e6d1 | 6374 | coding->eol_seen = eol_seen; |
251e9147 | 6375 | return nchars; |
8a44e6d1 KH |
6376 | } |
6377 | ||
6378 | ||
ff0dacd7 KH |
6379 | /* Detect how end-of-line of a text of length SRC_BYTES pointed by |
6380 | SOURCE is encoded. If CATEGORY is one of | |
6381 | coding_category_utf_16_XXXX, assume that CR and LF are encoded by | |
6382 | two-byte, else they are encoded by one-byte. | |
6383 | ||
6384 | Return one of EOL_SEEN_XXX. */ | |
4ed46869 | 6385 | |
bc4bc72a | 6386 | #define MAX_EOL_CHECK_COUNT 3 |
d46c5b12 KH |
6387 | |
6388 | static int | |
d311d28c | 6389 | detect_eol (const unsigned char *source, ptrdiff_t src_bytes, |
cf84bb53 | 6390 | enum coding_category category) |
4ed46869 | 6391 | { |
f6cbaf43 | 6392 | const unsigned char *src = source, *src_end = src + src_bytes; |
4ed46869 | 6393 | unsigned char c; |
df7492f9 KH |
6394 | int total = 0; |
6395 | int eol_seen = EOL_SEEN_NONE; | |
4ed46869 | 6396 | |
89528eb3 | 6397 | if ((1 << category) & CATEGORY_MASK_UTF_16) |
4ed46869 | 6398 | { |
f10fe38f PE |
6399 | bool msb = category == (coding_category_utf_16_le |
6400 | | coding_category_utf_16_le_nosig); | |
6401 | bool lsb = !msb; | |
fa42c37f | 6402 | |
df7492f9 | 6403 | while (src + 1 < src_end) |
fa42c37f | 6404 | { |
df7492f9 KH |
6405 | c = src[lsb]; |
6406 | if (src[msb] == 0 && (c == '\n' || c == '\r')) | |
fa42c37f | 6407 | { |
df7492f9 KH |
6408 | int this_eol; |
6409 | ||
6410 | if (c == '\n') | |
6411 | this_eol = EOL_SEEN_LF; | |
6412 | else if (src + 3 >= src_end | |
6413 | || src[msb + 2] != 0 | |
6414 | || src[lsb + 2] != '\n') | |
6415 | this_eol = EOL_SEEN_CR; | |
fa42c37f | 6416 | else |
75f4f1ac EZ |
6417 | { |
6418 | this_eol = EOL_SEEN_CRLF; | |
6419 | src += 2; | |
6420 | } | |
df7492f9 KH |
6421 | |
6422 | if (eol_seen == EOL_SEEN_NONE) | |
6423 | /* This is the first end-of-line. */ | |
6424 | eol_seen = this_eol; | |
6425 | else if (eol_seen != this_eol) | |
fa42c37f | 6426 | { |
75f4f1ac EZ |
6427 | /* The found type is different from what found before. |
6428 | Allow for stray ^M characters in DOS EOL files. */ | |
ef1b0ba7 SM |
6429 | if ((eol_seen == EOL_SEEN_CR && this_eol == EOL_SEEN_CRLF) |
6430 | || (eol_seen == EOL_SEEN_CRLF | |
6431 | && this_eol == EOL_SEEN_CR)) | |
75f4f1ac EZ |
6432 | eol_seen = EOL_SEEN_CRLF; |
6433 | else | |
6434 | { | |
6435 | eol_seen = EOL_SEEN_LF; | |
6436 | break; | |
6437 | } | |
fa42c37f | 6438 | } |
df7492f9 KH |
6439 | if (++total == MAX_EOL_CHECK_COUNT) |
6440 | break; | |
fa42c37f | 6441 | } |
df7492f9 | 6442 | src += 2; |
fa42c37f | 6443 | } |
bcf26d6a | 6444 | } |
d46c5b12 | 6445 | else |
ef1b0ba7 SM |
6446 | while (src < src_end) |
6447 | { | |
6448 | c = *src++; | |
6449 | if (c == '\n' || c == '\r') | |
6450 | { | |
6451 | int this_eol; | |
d46c5b12 | 6452 | |
ef1b0ba7 SM |
6453 | if (c == '\n') |
6454 | this_eol = EOL_SEEN_LF; | |
6455 | else if (src >= src_end || *src != '\n') | |
6456 | this_eol = EOL_SEEN_CR; | |
6457 | else | |
6458 | this_eol = EOL_SEEN_CRLF, src++; | |
d46c5b12 | 6459 | |
ef1b0ba7 SM |
6460 | if (eol_seen == EOL_SEEN_NONE) |
6461 | /* This is the first end-of-line. */ | |
6462 | eol_seen = this_eol; | |
6463 | else if (eol_seen != this_eol) | |
6464 | { | |
6465 | /* The found type is different from what found before. | |
6466 | Allow for stray ^M characters in DOS EOL files. */ | |
6467 | if ((eol_seen == EOL_SEEN_CR && this_eol == EOL_SEEN_CRLF) | |
6468 | || (eol_seen == EOL_SEEN_CRLF && this_eol == EOL_SEEN_CR)) | |
6469 | eol_seen = EOL_SEEN_CRLF; | |
6470 | else | |
6471 | { | |
6472 | eol_seen = EOL_SEEN_LF; | |
6473 | break; | |
6474 | } | |
6475 | } | |
6476 | if (++total == MAX_EOL_CHECK_COUNT) | |
6477 | break; | |
6478 | } | |
6479 | } | |
df7492f9 | 6480 | return eol_seen; |
73be902c KH |
6481 | } |
6482 | ||
df7492f9 | 6483 | |
24a73b0a | 6484 | static Lisp_Object |
971de7fb | 6485 | adjust_coding_eol_type (struct coding_system *coding, int eol_seen) |
73be902c | 6486 | { |
0be8721c | 6487 | Lisp_Object eol_type; |
8f924df7 | 6488 | |
df7492f9 | 6489 | eol_type = CODING_ID_EOL_TYPE (coding->id); |
251e9147 KH |
6490 | if (! VECTORP (eol_type)) |
6491 | /* Already adjusted. */ | |
6492 | return eol_type; | |
df7492f9 | 6493 | if (eol_seen & EOL_SEEN_LF) |
24a73b0a KH |
6494 | { |
6495 | coding->id = CODING_SYSTEM_ID (AREF (eol_type, 0)); | |
6496 | eol_type = Qunix; | |
6497 | } | |
6f197c07 | 6498 | else if (eol_seen & EOL_SEEN_CRLF) |
24a73b0a KH |
6499 | { |
6500 | coding->id = CODING_SYSTEM_ID (AREF (eol_type, 1)); | |
6501 | eol_type = Qdos; | |
6502 | } | |
6f197c07 | 6503 | else if (eol_seen & EOL_SEEN_CR) |
24a73b0a KH |
6504 | { |
6505 | coding->id = CODING_SYSTEM_ID (AREF (eol_type, 2)); | |
6506 | eol_type = Qmac; | |
6507 | } | |
6508 | return eol_type; | |
d46c5b12 | 6509 | } |
4ed46869 | 6510 | |
df7492f9 KH |
6511 | /* Detect how a text specified in CODING is encoded. If a coding |
6512 | system is detected, update fields of CODING by the detected coding | |
6513 | system. */ | |
0a28aafb | 6514 | |
74ab6df5 | 6515 | static void |
971de7fb | 6516 | detect_coding (struct coding_system *coding) |
d46c5b12 | 6517 | { |
8f924df7 | 6518 | const unsigned char *src, *src_end; |
f10fe38f | 6519 | unsigned int saved_mode = coding->mode; |
e6d2f155 KH |
6520 | Lisp_Object found = Qnil; |
6521 | Lisp_Object eol_type = CODING_ID_EOL_TYPE (coding->id); | |
d46c5b12 | 6522 | |
df7492f9 KH |
6523 | coding->consumed = coding->consumed_char = 0; |
6524 | coding->produced = coding->produced_char = 0; | |
6525 | coding_set_source (coding); | |
1c3478b0 | 6526 | |
df7492f9 | 6527 | src_end = coding->source + coding->src_bytes; |
1c3478b0 | 6528 | |
e6d2f155 | 6529 | coding->eol_seen = EOL_SEEN_NONE; |
df7492f9 KH |
6530 | /* If we have not yet decided the text encoding type, detect it |
6531 | now. */ | |
6532 | if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding->id)), Qundecided)) | |
b73bfc1c | 6533 | { |
df7492f9 | 6534 | int c, i; |
6cb21a4f | 6535 | struct coding_detection_info detect_info; |
f10fe38f | 6536 | bool null_byte_found = 0, eight_bit_found = 0; |
9c90cc06 PE |
6537 | bool inhibit_nbd = inhibit_flag (coding->spec.undecided.inhibit_nbd, |
6538 | inhibit_null_byte_detection); | |
6539 | bool inhibit_ied = inhibit_flag (coding->spec.undecided.inhibit_ied, | |
6540 | inhibit_iso_escape_detection); | |
6541 | bool prefer_utf_8 = coding->spec.undecided.prefer_utf_8; | |
df7492f9 | 6542 | |
8a44e6d1 | 6543 | coding->head_ascii = 0; |
6cb21a4f | 6544 | detect_info.checked = detect_info.found = detect_info.rejected = 0; |
2f3cbb32 | 6545 | for (src = coding->source; src < src_end; src++) |
d46c5b12 | 6546 | { |
df7492f9 | 6547 | c = *src; |
6cb21a4f | 6548 | if (c & 0x80) |
6cb21a4f | 6549 | { |
2f3cbb32 | 6550 | eight_bit_found = 1; |
2f3cbb32 KH |
6551 | if (null_byte_found) |
6552 | break; | |
6553 | } | |
6554 | else if (c < 0x20) | |
6555 | { | |
6556 | if ((c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) | |
c3a7b3f4 | 6557 | && ! inhibit_ied |
2f3cbb32 | 6558 | && ! detect_info.checked) |
6cb21a4f | 6559 | { |
2f3cbb32 KH |
6560 | if (detect_coding_iso_2022 (coding, &detect_info)) |
6561 | { | |
6562 | /* We have scanned the whole data. */ | |
6563 | if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE)) | |
c0e16b14 KH |
6564 | { |
6565 | /* We didn't find an 8-bit code. We may | |
6566 | have found a null-byte, but it's very | |
ce5b453a | 6567 | rare that a binary file conforms to |
c0e16b14 KH |
6568 | ISO-2022. */ |
6569 | src = src_end; | |
6570 | coding->head_ascii = src - coding->source; | |
6571 | } | |
6572 | detect_info.rejected |= ~CATEGORY_MASK_ISO_ESCAPE; | |
2f3cbb32 KH |
6573 | break; |
6574 | } | |
6575 | } | |
c3a7b3f4 | 6576 | else if (! c && !inhibit_nbd) |
2f3cbb32 KH |
6577 | { |
6578 | null_byte_found = 1; | |
6579 | if (eight_bit_found) | |
6580 | break; | |
6cb21a4f | 6581 | } |
8a44e6d1 KH |
6582 | else if (! disable_ascii_optimization |
6583 | && ! inhibit_eol_conversion) | |
6584 | { | |
6585 | if (c == '\r') | |
6586 | { | |
6587 | if (src < src_end && src[1] == '\n') | |
6588 | { | |
6589 | coding->eol_seen |= EOL_SEEN_CRLF; | |
6590 | src++; | |
251e9147 KH |
6591 | if (! eight_bit_found) |
6592 | coding->head_ascii++; | |
8a44e6d1 KH |
6593 | } |
6594 | else | |
6595 | coding->eol_seen |= EOL_SEEN_CR; | |
6596 | } | |
6597 | else if (c == '\n') | |
6598 | { | |
6599 | coding->eol_seen |= EOL_SEEN_LF; | |
6600 | } | |
6601 | } | |
6602 | ||
c006c0c8 KH |
6603 | if (! eight_bit_found) |
6604 | coding->head_ascii++; | |
6cb21a4f | 6605 | } |
c006c0c8 | 6606 | else if (! eight_bit_found) |
c0e16b14 | 6607 | coding->head_ascii++; |
d46c5b12 | 6608 | } |
df7492f9 | 6609 | |
2f3cbb32 KH |
6610 | if (null_byte_found || eight_bit_found |
6611 | || coding->head_ascii < coding->src_bytes | |
6cb21a4f | 6612 | || detect_info.found) |
d46c5b12 | 6613 | { |
ff0dacd7 KH |
6614 | enum coding_category category; |
6615 | struct coding_system *this; | |
df7492f9 | 6616 | |
6cb21a4f KH |
6617 | if (coding->head_ascii == coding->src_bytes) |
6618 | /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */ | |
6619 | for (i = 0; i < coding_category_raw_text; i++) | |
6620 | { | |
6621 | category = coding_priorities[i]; | |
6622 | this = coding_categories + category; | |
6623 | if (detect_info.found & (1 << category)) | |
24a73b0a | 6624 | break; |
6cb21a4f KH |
6625 | } |
6626 | else | |
2f3cbb32 KH |
6627 | { |
6628 | if (null_byte_found) | |
ff0dacd7 | 6629 | { |
2f3cbb32 KH |
6630 | detect_info.checked |= ~CATEGORY_MASK_UTF_16; |
6631 | detect_info.rejected |= ~CATEGORY_MASK_UTF_16; | |
ff0dacd7 | 6632 | } |
c3a7b3f4 KH |
6633 | else if (prefer_utf_8 |
6634 | && detect_coding_utf_8 (coding, &detect_info)) | |
6635 | { | |
6636 | detect_info.checked |= ~CATEGORY_MASK_UTF_8; | |
6637 | detect_info.rejected |= ~CATEGORY_MASK_UTF_8; | |
6638 | } | |
2f3cbb32 KH |
6639 | for (i = 0; i < coding_category_raw_text; i++) |
6640 | { | |
6641 | category = coding_priorities[i]; | |
6642 | this = coding_categories + category; | |
0ba06a77 KH |
6643 | /* Some of this->detector (e.g. detect_coding_sjis) |
6644 | require this information. */ | |
6645 | coding->id = this->id; | |
2f3cbb32 KH |
6646 | if (this->id < 0) |
6647 | { | |
6648 | /* No coding system of this category is defined. */ | |
6649 | detect_info.rejected |= (1 << category); | |
6650 | } | |
6651 | else if (category >= coding_category_raw_text) | |
6652 | continue; | |
6653 | else if (detect_info.checked & (1 << category)) | |
6654 | { | |
6655 | if (detect_info.found & (1 << category)) | |
6656 | break; | |
6657 | } | |
6658 | else if ((*(this->detector)) (coding, &detect_info) | |
6659 | && detect_info.found & (1 << category)) | |
e6d2f155 | 6660 | break; |
2f3cbb32 | 6661 | } |
2f3cbb32 | 6662 | } |
c0e16b14 KH |
6663 | |
6664 | if (i < coding_category_raw_text) | |
e6d2f155 KH |
6665 | { |
6666 | if (category == coding_category_utf_8_auto) | |
6667 | { | |
6668 | Lisp_Object coding_systems; | |
6669 | ||
6670 | coding_systems = AREF (CODING_ID_ATTRS (this->id), | |
6671 | coding_attr_utf_bom); | |
6672 | if (CONSP (coding_systems)) | |
6673 | { | |
6674 | if (detect_info.found & CATEGORY_MASK_UTF_8_SIG) | |
6675 | found = XCAR (coding_systems); | |
6676 | else | |
6677 | found = XCDR (coding_systems); | |
6678 | } | |
6679 | else | |
6680 | found = CODING_ID_NAME (this->id); | |
6681 | } | |
6682 | else if (category == coding_category_utf_16_auto) | |
6683 | { | |
6684 | Lisp_Object coding_systems; | |
6685 | ||
6686 | coding_systems = AREF (CODING_ID_ATTRS (this->id), | |
6687 | coding_attr_utf_bom); | |
6688 | if (CONSP (coding_systems)) | |
6689 | { | |
6690 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
6691 | found = XCAR (coding_systems); | |
6692 | else if (detect_info.found & CATEGORY_MASK_UTF_16_BE) | |
6693 | found = XCDR (coding_systems); | |
6694 | } | |
6695 | else | |
6696 | found = CODING_ID_NAME (this->id); | |
6697 | } | |
6698 | else | |
6699 | found = CODING_ID_NAME (this->id); | |
6700 | } | |
c0e16b14 | 6701 | else if (null_byte_found) |
e6d2f155 | 6702 | found = Qno_conversion; |
c0e16b14 KH |
6703 | else if ((detect_info.rejected & CATEGORY_MASK_ANY) |
6704 | == CATEGORY_MASK_ANY) | |
e6d2f155 | 6705 | found = Qraw_text; |
c0e16b14 KH |
6706 | else if (detect_info.rejected) |
6707 | for (i = 0; i < coding_category_raw_text; i++) | |
6708 | if (! (detect_info.rejected & (1 << coding_priorities[i]))) | |
6709 | { | |
6710 | this = coding_categories + coding_priorities[i]; | |
e6d2f155 | 6711 | found = CODING_ID_NAME (this->id); |
c0e16b14 KH |
6712 | break; |
6713 | } | |
d46c5b12 | 6714 | } |
b73bfc1c | 6715 | } |
a470d443 KH |
6716 | else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding->id))) |
6717 | == coding_category_utf_8_auto) | |
6718 | { | |
6719 | Lisp_Object coding_systems; | |
6720 | struct coding_detection_info detect_info; | |
6721 | ||
6722 | coding_systems | |
6723 | = AREF (CODING_ID_ATTRS (coding->id), coding_attr_utf_bom); | |
6724 | detect_info.found = detect_info.rejected = 0; | |
251e9147 | 6725 | if (check_ascii (coding) == coding->src_bytes) |
c230dd7d | 6726 | { |
e6d2f155 KH |
6727 | if (CONSP (coding_systems)) |
6728 | found = XCDR (coding_systems); | |
c230dd7d | 6729 | } |
8a44e6d1 | 6730 | else |
a470d443 | 6731 | { |
8a44e6d1 KH |
6732 | if (CONSP (coding_systems) |
6733 | && detect_coding_utf_8 (coding, &detect_info)) | |
6734 | { | |
6735 | if (detect_info.found & CATEGORY_MASK_UTF_8_SIG) | |
e6d2f155 | 6736 | found = XCAR (coding_systems); |
8a44e6d1 | 6737 | else |
e6d2f155 | 6738 | found = XCDR (coding_systems); |
8a44e6d1 | 6739 | } |
a470d443 KH |
6740 | } |
6741 | } | |
24a73b0a KH |
6742 | else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding->id))) |
6743 | == coding_category_utf_16_auto) | |
b49a1807 KH |
6744 | { |
6745 | Lisp_Object coding_systems; | |
6746 | struct coding_detection_info detect_info; | |
6747 | ||
6748 | coding_systems | |
a470d443 | 6749 | = AREF (CODING_ID_ATTRS (coding->id), coding_attr_utf_bom); |
b49a1807 | 6750 | detect_info.found = detect_info.rejected = 0; |
a470d443 | 6751 | coding->head_ascii = 0; |
b49a1807 | 6752 | if (CONSP (coding_systems) |
24a73b0a | 6753 | && detect_coding_utf_16 (coding, &detect_info)) |
b49a1807 KH |
6754 | { |
6755 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
e6d2f155 | 6756 | found = XCAR (coding_systems); |
24a73b0a | 6757 | else if (detect_info.found & CATEGORY_MASK_UTF_16_BE) |
e6d2f155 | 6758 | found = XCDR (coding_systems); |
b49a1807 KH |
6759 | } |
6760 | } | |
e6d2f155 KH |
6761 | |
6762 | if (! NILP (found)) | |
6763 | { | |
6764 | int specified_eol = (VECTORP (eol_type) ? EOL_SEEN_NONE | |
6765 | : EQ (eol_type, Qdos) ? EOL_SEEN_CRLF | |
6766 | : EQ (eol_type, Qmac) ? EOL_SEEN_CR | |
6767 | : EOL_SEEN_LF); | |
6768 | ||
6769 | setup_coding_system (found, coding); | |
6770 | if (specified_eol != EOL_SEEN_NONE) | |
6771 | adjust_coding_eol_type (coding, specified_eol); | |
6772 | } | |
6773 | ||
73cce38d | 6774 | coding->mode = saved_mode; |
4ed46869 | 6775 | } |
4ed46869 | 6776 | |
d46c5b12 | 6777 | |
aaaf0b1e | 6778 | static void |
971de7fb | 6779 | decode_eol (struct coding_system *coding) |
aaaf0b1e | 6780 | { |
24a73b0a KH |
6781 | Lisp_Object eol_type; |
6782 | unsigned char *p, *pbeg, *pend; | |
3ed051d4 | 6783 | |
24a73b0a | 6784 | eol_type = CODING_ID_EOL_TYPE (coding->id); |
0a9564cb | 6785 | if (EQ (eol_type, Qunix) || inhibit_eol_conversion) |
24a73b0a KH |
6786 | return; |
6787 | ||
6788 | if (NILP (coding->dst_object)) | |
6789 | pbeg = coding->destination; | |
6790 | else | |
6791 | pbeg = BYTE_POS_ADDR (coding->dst_pos_byte); | |
6792 | pend = pbeg + coding->produced; | |
6793 | ||
6794 | if (VECTORP (eol_type)) | |
aaaf0b1e | 6795 | { |
df7492f9 | 6796 | int eol_seen = EOL_SEEN_NONE; |
4ed46869 | 6797 | |
24a73b0a | 6798 | for (p = pbeg; p < pend; p++) |
aaaf0b1e | 6799 | { |
df7492f9 KH |
6800 | if (*p == '\n') |
6801 | eol_seen |= EOL_SEEN_LF; | |
6802 | else if (*p == '\r') | |
aaaf0b1e | 6803 | { |
df7492f9 | 6804 | if (p + 1 < pend && *(p + 1) == '\n') |
aaaf0b1e | 6805 | { |
df7492f9 KH |
6806 | eol_seen |= EOL_SEEN_CRLF; |
6807 | p++; | |
aaaf0b1e | 6808 | } |
aaaf0b1e | 6809 | else |
df7492f9 | 6810 | eol_seen |= EOL_SEEN_CR; |
aaaf0b1e | 6811 | } |
aaaf0b1e | 6812 | } |
75f4f1ac EZ |
6813 | /* Handle DOS-style EOLs in a file with stray ^M characters. */ |
6814 | if ((eol_seen & EOL_SEEN_CRLF) != 0 | |
6815 | && (eol_seen & EOL_SEEN_CR) != 0 | |
6816 | && (eol_seen & EOL_SEEN_LF) == 0) | |
6817 | eol_seen = EOL_SEEN_CRLF; | |
6818 | else if (eol_seen != EOL_SEEN_NONE | |
24a73b0a KH |
6819 | && eol_seen != EOL_SEEN_LF |
6820 | && eol_seen != EOL_SEEN_CRLF | |
6821 | && eol_seen != EOL_SEEN_CR) | |
6822 | eol_seen = EOL_SEEN_LF; | |
df7492f9 | 6823 | if (eol_seen != EOL_SEEN_NONE) |
24a73b0a | 6824 | eol_type = adjust_coding_eol_type (coding, eol_seen); |
aaaf0b1e | 6825 | } |
d46c5b12 | 6826 | |
24a73b0a | 6827 | if (EQ (eol_type, Qmac)) |
27901516 | 6828 | { |
24a73b0a | 6829 | for (p = pbeg; p < pend; p++) |
df7492f9 KH |
6830 | if (*p == '\r') |
6831 | *p = '\n'; | |
4ed46869 | 6832 | } |
24a73b0a | 6833 | else if (EQ (eol_type, Qdos)) |
df7492f9 | 6834 | { |
d311d28c | 6835 | ptrdiff_t n = 0; |
b73bfc1c | 6836 | |
24a73b0a KH |
6837 | if (NILP (coding->dst_object)) |
6838 | { | |
4347441b KH |
6839 | /* Start deleting '\r' from the tail to minimize the memory |
6840 | movement. */ | |
24a73b0a KH |
6841 | for (p = pend - 2; p >= pbeg; p--) |
6842 | if (*p == '\r') | |
6843 | { | |
72af86bd | 6844 | memmove (p, p + 1, pend-- - p - 1); |
24a73b0a KH |
6845 | n++; |
6846 | } | |
6847 | } | |
6848 | else | |
6849 | { | |
d311d28c PE |
6850 | ptrdiff_t pos_byte = coding->dst_pos_byte; |
6851 | ptrdiff_t pos = coding->dst_pos; | |
6852 | ptrdiff_t pos_end = pos + coding->produced_char - 1; | |
4347441b KH |
6853 | |
6854 | while (pos < pos_end) | |
6855 | { | |
6856 | p = BYTE_POS_ADDR (pos_byte); | |
6857 | if (*p == '\r' && p[1] == '\n') | |
6858 | { | |
6859 | del_range_2 (pos, pos_byte, pos + 1, pos_byte + 1, 0); | |
6860 | n++; | |
6861 | pos_end--; | |
6862 | } | |
6863 | pos++; | |
69b8522d KH |
6864 | if (coding->dst_multibyte) |
6865 | pos_byte += BYTES_BY_CHAR_HEAD (*p); | |
6866 | else | |
6867 | pos_byte++; | |
4347441b | 6868 | } |
24a73b0a KH |
6869 | } |
6870 | coding->produced -= n; | |
6871 | coding->produced_char -= n; | |
aaaf0b1e | 6872 | } |
4ed46869 KH |
6873 | } |
6874 | ||
7d64c6ad | 6875 | |
a6f87d34 | 6876 | /* Return a translation table (or list of them) from coding system |
f10fe38f PE |
6877 | attribute vector ATTRS for encoding (if ENCODEP) or decoding (if |
6878 | not ENCODEP). */ | |
7d64c6ad | 6879 | |
e6a54062 | 6880 | static Lisp_Object |
f10fe38f | 6881 | get_translation_table (Lisp_Object attrs, bool encodep, int *max_lookup) |
7d64c6ad KH |
6882 | { |
6883 | Lisp_Object standard, translation_table; | |
09ee6fdd | 6884 | Lisp_Object val; |
7d64c6ad | 6885 | |
4bed5909 CY |
6886 | if (NILP (Venable_character_translation)) |
6887 | { | |
6888 | if (max_lookup) | |
6889 | *max_lookup = 0; | |
6890 | return Qnil; | |
6891 | } | |
7d64c6ad KH |
6892 | if (encodep) |
6893 | translation_table = CODING_ATTR_ENCODE_TBL (attrs), | |
6894 | standard = Vstandard_translation_table_for_encode; | |
6895 | else | |
6896 | translation_table = CODING_ATTR_DECODE_TBL (attrs), | |
6897 | standard = Vstandard_translation_table_for_decode; | |
7d64c6ad | 6898 | if (NILP (translation_table)) |
09ee6fdd KH |
6899 | translation_table = standard; |
6900 | else | |
a6f87d34 | 6901 | { |
09ee6fdd KH |
6902 | if (SYMBOLP (translation_table)) |
6903 | translation_table = Fget (translation_table, Qtranslation_table); | |
6904 | else if (CONSP (translation_table)) | |
6905 | { | |
6906 | translation_table = Fcopy_sequence (translation_table); | |
6907 | for (val = translation_table; CONSP (val); val = XCDR (val)) | |
6908 | if (SYMBOLP (XCAR (val))) | |
6909 | XSETCAR (val, Fget (XCAR (val), Qtranslation_table)); | |
6910 | } | |
6911 | if (CHAR_TABLE_P (standard)) | |
6912 | { | |
6913 | if (CONSP (translation_table)) | |
6c6f1994 | 6914 | translation_table = nconc2 (translation_table, list1 (standard)); |
09ee6fdd | 6915 | else |
6c6f1994 | 6916 | translation_table = list2 (translation_table, standard); |
09ee6fdd | 6917 | } |
a6f87d34 | 6918 | } |
2170c8f0 KH |
6919 | |
6920 | if (max_lookup) | |
09ee6fdd | 6921 | { |
2170c8f0 KH |
6922 | *max_lookup = 1; |
6923 | if (CHAR_TABLE_P (translation_table) | |
6924 | && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table)) > 1) | |
6925 | { | |
6926 | val = XCHAR_TABLE (translation_table)->extras[1]; | |
6927 | if (NATNUMP (val) && *max_lookup < XFASTINT (val)) | |
6928 | *max_lookup = XFASTINT (val); | |
6929 | } | |
6930 | else if (CONSP (translation_table)) | |
6931 | { | |
2735d060 | 6932 | Lisp_Object tail; |
09ee6fdd | 6933 | |
2170c8f0 KH |
6934 | for (tail = translation_table; CONSP (tail); tail = XCDR (tail)) |
6935 | if (CHAR_TABLE_P (XCAR (tail)) | |
6936 | && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail))) > 1) | |
6937 | { | |
2735d060 PE |
6938 | Lisp_Object tailval = XCHAR_TABLE (XCAR (tail))->extras[1]; |
6939 | if (NATNUMP (tailval) && *max_lookup < XFASTINT (tailval)) | |
6940 | *max_lookup = XFASTINT (tailval); | |
2170c8f0 KH |
6941 | } |
6942 | } | |
a6f87d34 | 6943 | } |
7d64c6ad KH |
6944 | return translation_table; |
6945 | } | |
6946 | ||
09ee6fdd KH |
6947 | #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \ |
6948 | do { \ | |
6949 | trans = Qnil; \ | |
6950 | if (CHAR_TABLE_P (table)) \ | |
6951 | { \ | |
6952 | trans = CHAR_TABLE_REF (table, c); \ | |
6953 | if (CHARACTERP (trans)) \ | |
6954 | c = XFASTINT (trans), trans = Qnil; \ | |
6955 | } \ | |
6956 | else if (CONSP (table)) \ | |
6957 | { \ | |
6958 | Lisp_Object tail; \ | |
6959 | \ | |
6960 | for (tail = table; CONSP (tail); tail = XCDR (tail)) \ | |
6961 | if (CHAR_TABLE_P (XCAR (tail))) \ | |
6962 | { \ | |
6963 | trans = CHAR_TABLE_REF (XCAR (tail), c); \ | |
6964 | if (CHARACTERP (trans)) \ | |
6965 | c = XFASTINT (trans), trans = Qnil; \ | |
6966 | else if (! NILP (trans)) \ | |
6967 | break; \ | |
6968 | } \ | |
6969 | } \ | |
e6a54062 KH |
6970 | } while (0) |
6971 | ||
7d64c6ad | 6972 | |
e951386e KH |
6973 | /* Return a translation of character(s) at BUF according to TRANS. |
6974 | TRANS is TO-CHAR or ((FROM . TO) ...) where | |
6975 | FROM = [FROM-CHAR ...], TO is TO-CHAR or [TO-CHAR ...]. | |
6976 | The return value is TO-CHAR or ([FROM-CHAR ...] . TO) if a | |
6977 | translation is found, and Qnil if not found.. | |
6978 | If BUF is too short to lookup characters in FROM, return Qt. */ | |
6979 | ||
69a80ea3 | 6980 | static Lisp_Object |
971de7fb | 6981 | get_translation (Lisp_Object trans, int *buf, int *buf_end) |
69a80ea3 | 6982 | { |
e951386e KH |
6983 | |
6984 | if (INTEGERP (trans)) | |
6985 | return trans; | |
6986 | for (; CONSP (trans); trans = XCDR (trans)) | |
69a80ea3 | 6987 | { |
e951386e KH |
6988 | Lisp_Object val = XCAR (trans); |
6989 | Lisp_Object from = XCAR (val); | |
2c6a9faa PE |
6990 | ptrdiff_t len = ASIZE (from); |
6991 | ptrdiff_t i; | |
69a80ea3 | 6992 | |
e951386e | 6993 | for (i = 0; i < len; i++) |
69a80ea3 | 6994 | { |
e951386e KH |
6995 | if (buf + i == buf_end) |
6996 | return Qt; | |
6997 | if (XINT (AREF (from, i)) != buf[i]) | |
6998 | break; | |
69a80ea3 | 6999 | } |
e951386e KH |
7000 | if (i == len) |
7001 | return val; | |
69a80ea3 | 7002 | } |
e951386e | 7003 | return Qnil; |
69a80ea3 KH |
7004 | } |
7005 | ||
7006 | ||
d46c5b12 | 7007 | static int |
cf84bb53 | 7008 | produce_chars (struct coding_system *coding, Lisp_Object translation_table, |
f10fe38f | 7009 | bool last_block) |
4ed46869 | 7010 | { |
df7492f9 KH |
7011 | unsigned char *dst = coding->destination + coding->produced; |
7012 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
d311d28c PE |
7013 | ptrdiff_t produced; |
7014 | ptrdiff_t produced_chars = 0; | |
69a80ea3 | 7015 | int carryover = 0; |
4ed46869 | 7016 | |
df7492f9 | 7017 | if (! coding->chars_at_source) |
4ed46869 | 7018 | { |
119852e7 | 7019 | /* Source characters are in coding->charbuf. */ |
fba4576f AS |
7020 | int *buf = coding->charbuf; |
7021 | int *buf_end = buf + coding->charbuf_used; | |
4ed46869 | 7022 | |
db274c7a KH |
7023 | if (EQ (coding->src_object, coding->dst_object)) |
7024 | { | |
7025 | coding_set_source (coding); | |
7026 | dst_end = ((unsigned char *) coding->source) + coding->consumed; | |
7027 | } | |
4ed46869 | 7028 | |
df7492f9 | 7029 | while (buf < buf_end) |
4ed46869 | 7030 | { |
27bb1ca4 PE |
7031 | int c = *buf; |
7032 | ptrdiff_t i; | |
bc4bc72a | 7033 | |
df7492f9 KH |
7034 | if (c >= 0) |
7035 | { | |
d311d28c | 7036 | ptrdiff_t from_nchars = 1, to_nchars = 1; |
69a80ea3 KH |
7037 | Lisp_Object trans = Qnil; |
7038 | ||
09ee6fdd | 7039 | LOOKUP_TRANSLATION_TABLE (translation_table, c, trans); |
e6a54062 | 7040 | if (! NILP (trans)) |
69a80ea3 | 7041 | { |
e951386e KH |
7042 | trans = get_translation (trans, buf, buf_end); |
7043 | if (INTEGERP (trans)) | |
7044 | c = XINT (trans); | |
7045 | else if (CONSP (trans)) | |
7046 | { | |
7047 | from_nchars = ASIZE (XCAR (trans)); | |
7048 | trans = XCDR (trans); | |
7049 | if (INTEGERP (trans)) | |
7050 | c = XINT (trans); | |
7051 | else | |
7052 | { | |
7053 | to_nchars = ASIZE (trans); | |
7054 | c = XINT (AREF (trans, 0)); | |
7055 | } | |
7056 | } | |
7057 | else if (EQ (trans, Qt) && ! last_block) | |
69a80ea3 | 7058 | break; |
69a80ea3 KH |
7059 | } |
7060 | ||
5d009b3a | 7061 | if ((dst_end - dst) / MAX_MULTIBYTE_LENGTH < to_nchars) |
69a80ea3 | 7062 | { |
5d009b3a PE |
7063 | if (((min (PTRDIFF_MAX, SIZE_MAX) - (buf_end - buf)) |
7064 | / MAX_MULTIBYTE_LENGTH) | |
7065 | < to_nchars) | |
7066 | memory_full (SIZE_MAX); | |
69a80ea3 KH |
7067 | dst = alloc_destination (coding, |
7068 | buf_end - buf | |
7069 | + MAX_MULTIBYTE_LENGTH * to_nchars, | |
7070 | dst); | |
db274c7a KH |
7071 | if (EQ (coding->src_object, coding->dst_object)) |
7072 | { | |
7073 | coding_set_source (coding); | |
e951386e KH |
7074 | dst_end = (((unsigned char *) coding->source) |
7075 | + coding->consumed); | |
db274c7a KH |
7076 | } |
7077 | else | |
7078 | dst_end = coding->destination + coding->dst_bytes; | |
69a80ea3 KH |
7079 | } |
7080 | ||
433f7f87 | 7081 | for (i = 0; i < to_nchars; i++) |
69a80ea3 | 7082 | { |
433f7f87 KH |
7083 | if (i > 0) |
7084 | c = XINT (AREF (trans, i)); | |
69a80ea3 KH |
7085 | if (coding->dst_multibyte |
7086 | || ! CHAR_BYTE8_P (c)) | |
db274c7a | 7087 | CHAR_STRING_ADVANCE_NO_UNIFY (c, dst); |
69a80ea3 KH |
7088 | else |
7089 | *dst++ = CHAR_TO_BYTE8 (c); | |
7090 | } | |
7091 | produced_chars += to_nchars; | |
e951386e | 7092 | buf += from_nchars; |
d46c5b12 | 7093 | } |
df7492f9 | 7094 | else |
69a80ea3 KH |
7095 | /* This is an annotation datum. (-C) is the length. */ |
7096 | buf += -c; | |
4ed46869 | 7097 | } |
69a80ea3 | 7098 | carryover = buf_end - buf; |
4ed46869 | 7099 | } |
fa42c37f | 7100 | else |
fa42c37f | 7101 | { |
119852e7 | 7102 | /* Source characters are at coding->source. */ |
8f924df7 | 7103 | const unsigned char *src = coding->source; |
119852e7 | 7104 | const unsigned char *src_end = src + coding->consumed; |
4ed46869 | 7105 | |
db274c7a KH |
7106 | if (EQ (coding->dst_object, coding->src_object)) |
7107 | dst_end = (unsigned char *) src; | |
df7492f9 | 7108 | if (coding->src_multibyte != coding->dst_multibyte) |
fa42c37f | 7109 | { |
df7492f9 | 7110 | if (coding->src_multibyte) |
fa42c37f | 7111 | { |
f10fe38f | 7112 | bool multibytep = 1; |
d311d28c | 7113 | ptrdiff_t consumed_chars = 0; |
d46c5b12 | 7114 | |
df7492f9 KH |
7115 | while (1) |
7116 | { | |
8f924df7 | 7117 | const unsigned char *src_base = src; |
df7492f9 | 7118 | int c; |
b73bfc1c | 7119 | |
df7492f9 | 7120 | ONE_MORE_BYTE (c); |
119852e7 | 7121 | if (dst == dst_end) |
df7492f9 | 7122 | { |
119852e7 KH |
7123 | if (EQ (coding->src_object, coding->dst_object)) |
7124 | dst_end = (unsigned char *) src; | |
7125 | if (dst == dst_end) | |
df7492f9 | 7126 | { |
d311d28c | 7127 | ptrdiff_t offset = src - coding->source; |
119852e7 KH |
7128 | |
7129 | dst = alloc_destination (coding, src_end - src + 1, | |
7130 | dst); | |
7131 | dst_end = coding->destination + coding->dst_bytes; | |
7132 | coding_set_source (coding); | |
7133 | src = coding->source + offset; | |
5c1ca13d | 7134 | src_end = coding->source + coding->consumed; |
db274c7a KH |
7135 | if (EQ (coding->src_object, coding->dst_object)) |
7136 | dst_end = (unsigned char *) src; | |
df7492f9 | 7137 | } |
df7492f9 KH |
7138 | } |
7139 | *dst++ = c; | |
7140 | produced_chars++; | |
7141 | } | |
7142 | no_more_source: | |
7143 | ; | |
fa42c37f KH |
7144 | } |
7145 | else | |
df7492f9 KH |
7146 | while (src < src_end) |
7147 | { | |
f10fe38f | 7148 | bool multibytep = 1; |
df7492f9 | 7149 | int c = *src++; |
b73bfc1c | 7150 | |
df7492f9 KH |
7151 | if (dst >= dst_end - 1) |
7152 | { | |
2c78b7e1 | 7153 | if (EQ (coding->src_object, coding->dst_object)) |
8f924df7 | 7154 | dst_end = (unsigned char *) src; |
2c78b7e1 KH |
7155 | if (dst >= dst_end - 1) |
7156 | { | |
d311d28c PE |
7157 | ptrdiff_t offset = src - coding->source; |
7158 | ptrdiff_t more_bytes; | |
119852e7 | 7159 | |
db274c7a KH |
7160 | if (EQ (coding->src_object, coding->dst_object)) |
7161 | more_bytes = ((src_end - src) / 2) + 2; | |
7162 | else | |
7163 | more_bytes = src_end - src + 2; | |
7164 | dst = alloc_destination (coding, more_bytes, dst); | |
2c78b7e1 KH |
7165 | dst_end = coding->destination + coding->dst_bytes; |
7166 | coding_set_source (coding); | |
119852e7 | 7167 | src = coding->source + offset; |
5c1ca13d | 7168 | src_end = coding->source + coding->consumed; |
db274c7a KH |
7169 | if (EQ (coding->src_object, coding->dst_object)) |
7170 | dst_end = (unsigned char *) src; | |
2c78b7e1 | 7171 | } |
df7492f9 KH |
7172 | } |
7173 | EMIT_ONE_BYTE (c); | |
7174 | } | |
d46c5b12 | 7175 | } |
df7492f9 KH |
7176 | else |
7177 | { | |
7178 | if (!EQ (coding->src_object, coding->dst_object)) | |
fa42c37f | 7179 | { |
d311d28c | 7180 | ptrdiff_t require = coding->src_bytes - coding->dst_bytes; |
4ed46869 | 7181 | |
df7492f9 | 7182 | if (require > 0) |
fa42c37f | 7183 | { |
d311d28c | 7184 | ptrdiff_t offset = src - coding->source; |
df7492f9 KH |
7185 | |
7186 | dst = alloc_destination (coding, require, dst); | |
7187 | coding_set_source (coding); | |
7188 | src = coding->source + offset; | |
5c1ca13d | 7189 | src_end = coding->source + coding->consumed; |
fa42c37f KH |
7190 | } |
7191 | } | |
119852e7 | 7192 | produced_chars = coding->consumed_char; |
df7492f9 | 7193 | while (src < src_end) |
14daee73 | 7194 | *dst++ = *src++; |
fa42c37f KH |
7195 | } |
7196 | } | |
7197 | ||
df7492f9 | 7198 | produced = dst - (coding->destination + coding->produced); |
284201e4 | 7199 | if (BUFFERP (coding->dst_object) && produced_chars > 0) |
8a44e6d1 | 7200 | insert_from_gap (produced_chars, produced, 0); |
df7492f9 KH |
7201 | coding->produced += produced; |
7202 | coding->produced_char += produced_chars; | |
69a80ea3 | 7203 | return carryover; |
fa42c37f KH |
7204 | } |
7205 | ||
ff0dacd7 KH |
7206 | /* Compose text in CODING->object according to the annotation data at |
7207 | CHARBUF. CHARBUF is an array: | |
e951386e | 7208 | [ -LENGTH ANNOTATION_MASK NCHARS NBYTES METHOD [ COMPONENTS... ] ] |
df7492f9 | 7209 | */ |
4ed46869 | 7210 | |
b0ab8123 | 7211 | static void |
d311d28c | 7212 | produce_composition (struct coding_system *coding, int *charbuf, ptrdiff_t pos) |
4ed46869 | 7213 | { |
df7492f9 | 7214 | int len; |
d311d28c | 7215 | ptrdiff_t to; |
df7492f9 | 7216 | enum composition_method method; |
df7492f9 | 7217 | Lisp_Object components; |
fa42c37f | 7218 | |
e951386e | 7219 | len = -charbuf[0] - MAX_ANNOTATION_LENGTH; |
69a80ea3 | 7220 | to = pos + charbuf[2]; |
e951386e | 7221 | method = (enum composition_method) (charbuf[4]); |
d46c5b12 | 7222 | |
df7492f9 KH |
7223 | if (method == COMPOSITION_RELATIVE) |
7224 | components = Qnil; | |
e951386e | 7225 | else |
d46c5b12 | 7226 | { |
df7492f9 | 7227 | Lisp_Object args[MAX_COMPOSITION_COMPONENTS * 2 - 1]; |
e951386e | 7228 | int i, j; |
b73bfc1c | 7229 | |
e951386e KH |
7230 | if (method == COMPOSITION_WITH_RULE) |
7231 | len = charbuf[2] * 3 - 2; | |
7232 | charbuf += MAX_ANNOTATION_LENGTH; | |
7233 | /* charbuf = [ CHRA ... CHAR] or [ CHAR -2 RULE ... CHAR ] */ | |
7234 | for (i = j = 0; i < len && charbuf[i] != -1; i++, j++) | |
9ffd559c | 7235 | { |
e951386e KH |
7236 | if (charbuf[i] >= 0) |
7237 | args[j] = make_number (charbuf[i]); | |
7238 | else | |
7239 | { | |
7240 | i++; | |
7241 | args[j] = make_number (charbuf[i] % 0x100); | |
7242 | } | |
9ffd559c | 7243 | } |
e951386e | 7244 | components = (i == j ? Fstring (j, args) : Fvector (j, args)); |
d46c5b12 | 7245 | } |
69a80ea3 | 7246 | compose_text (pos, to, components, Qnil, coding->dst_object); |
d46c5b12 KH |
7247 | } |
7248 | ||
d46c5b12 | 7249 | |
ff0dacd7 KH |
7250 | /* Put `charset' property on text in CODING->object according to |
7251 | the annotation data at CHARBUF. CHARBUF is an array: | |
69a80ea3 | 7252 | [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ] |
ff0dacd7 | 7253 | */ |
d46c5b12 | 7254 | |
b0ab8123 | 7255 | static void |
d311d28c | 7256 | produce_charset (struct coding_system *coding, int *charbuf, ptrdiff_t pos) |
d46c5b12 | 7257 | { |
d311d28c | 7258 | ptrdiff_t from = pos - charbuf[2]; |
69a80ea3 | 7259 | struct charset *charset = CHARSET_FROM_ID (charbuf[3]); |
b73bfc1c | 7260 | |
69a80ea3 | 7261 | Fput_text_property (make_number (from), make_number (pos), |
ff0dacd7 KH |
7262 | Qcharset, CHARSET_NAME (charset), |
7263 | coding->dst_object); | |
d46c5b12 KH |
7264 | } |
7265 | ||
d46c5b12 | 7266 | |
df7492f9 KH |
7267 | #define CHARBUF_SIZE 0x4000 |
7268 | ||
7269 | #define ALLOC_CONVERSION_WORK_AREA(coding) \ | |
7270 | do { \ | |
1af1a51a DA |
7271 | coding->charbuf = SAFE_ALLOCA (CHARBUF_SIZE * sizeof (int)); \ |
7272 | coding->charbuf_size = CHARBUF_SIZE; \ | |
df7492f9 | 7273 | } while (0) |
4ed46869 | 7274 | |
d46c5b12 KH |
7275 | |
7276 | static void | |
d311d28c | 7277 | produce_annotation (struct coding_system *coding, ptrdiff_t pos) |
d46c5b12 | 7278 | { |
df7492f9 KH |
7279 | int *charbuf = coding->charbuf; |
7280 | int *charbuf_end = charbuf + coding->charbuf_used; | |
d46c5b12 | 7281 | |
ff0dacd7 KH |
7282 | if (NILP (coding->dst_object)) |
7283 | return; | |
d46c5b12 | 7284 | |
df7492f9 | 7285 | while (charbuf < charbuf_end) |
a84f1519 | 7286 | { |
df7492f9 | 7287 | if (*charbuf >= 0) |
e951386e | 7288 | pos++, charbuf++; |
d46c5b12 | 7289 | else |
d46c5b12 | 7290 | { |
df7492f9 | 7291 | int len = -*charbuf; |
e951386e KH |
7292 | |
7293 | if (len > 2) | |
7294 | switch (charbuf[1]) | |
7295 | { | |
7296 | case CODING_ANNOTATE_COMPOSITION_MASK: | |
7297 | produce_composition (coding, charbuf, pos); | |
7298 | break; | |
7299 | case CODING_ANNOTATE_CHARSET_MASK: | |
7300 | produce_charset (coding, charbuf, pos); | |
7301 | break; | |
7302 | } | |
df7492f9 | 7303 | charbuf += len; |
d46c5b12 | 7304 | } |
a84f1519 | 7305 | } |
d46c5b12 KH |
7306 | } |
7307 | ||
df7492f9 KH |
7308 | /* Decode the data at CODING->src_object into CODING->dst_object. |
7309 | CODING->src_object is a buffer, a string, or nil. | |
7310 | CODING->dst_object is a buffer. | |
d46c5b12 | 7311 | |
df7492f9 KH |
7312 | If CODING->src_object is a buffer, it must be the current buffer. |
7313 | In this case, if CODING->src_pos is positive, it is a position of | |
7314 | the source text in the buffer, otherwise, the source text is in the | |
7315 | gap area of the buffer, and CODING->src_pos specifies the offset of | |
7316 | the text from GPT (which must be the same as PT). If this is the | |
7317 | same buffer as CODING->dst_object, CODING->src_pos must be | |
7318 | negative. | |
d46c5b12 | 7319 | |
b6828792 | 7320 | If CODING->src_object is a string, CODING->src_pos is an index to |
df7492f9 | 7321 | that string. |
d46c5b12 | 7322 | |
df7492f9 KH |
7323 | If CODING->src_object is nil, CODING->source must already point to |
7324 | the non-relocatable memory area. In this case, CODING->src_pos is | |
7325 | an offset from CODING->source. | |
73be902c | 7326 | |
df7492f9 KH |
7327 | The decoded data is inserted at the current point of the buffer |
7328 | CODING->dst_object. | |
7329 | */ | |
d46c5b12 | 7330 | |
f10fe38f | 7331 | static void |
971de7fb | 7332 | decode_coding (struct coding_system *coding) |
d46c5b12 | 7333 | { |
df7492f9 | 7334 | Lisp_Object attrs; |
24a73b0a | 7335 | Lisp_Object undo_list; |
7d64c6ad | 7336 | Lisp_Object translation_table; |
d0396581 | 7337 | struct ccl_spec cclspec; |
69a80ea3 KH |
7338 | int carryover; |
7339 | int i; | |
d46c5b12 | 7340 | |
1af1a51a DA |
7341 | USE_SAFE_ALLOCA; |
7342 | ||
df7492f9 KH |
7343 | if (BUFFERP (coding->src_object) |
7344 | && coding->src_pos > 0 | |
7345 | && coding->src_pos < GPT | |
7346 | && coding->src_pos + coding->src_chars > GPT) | |
7347 | move_gap_both (coding->src_pos, coding->src_pos_byte); | |
d46c5b12 | 7348 | |
24a73b0a | 7349 | undo_list = Qt; |
df7492f9 | 7350 | if (BUFFERP (coding->dst_object)) |
1c3478b0 | 7351 | { |
a3d794a1 | 7352 | set_buffer_internal (XBUFFER (coding->dst_object)); |
df7492f9 KH |
7353 | if (GPT != PT) |
7354 | move_gap_both (PT, PT_BYTE); | |
f48b82fd GR |
7355 | |
7356 | /* We must disable undo_list in order to record the whole insert | |
7357 | transaction via record_insert at the end. But doing so also | |
7358 | disables the recording of the first change to the undo_list. | |
7359 | Therefore we check for first change here and record it via | |
7360 | record_first_change if needed. */ | |
7361 | if (MODIFF <= SAVE_MODIFF) | |
7362 | record_first_change (); | |
7363 | ||
4b4deea2 | 7364 | undo_list = BVAR (current_buffer, undo_list); |
39eb03f1 | 7365 | bset_undo_list (current_buffer, Qt); |
1c3478b0 KH |
7366 | } |
7367 | ||
df7492f9 KH |
7368 | coding->consumed = coding->consumed_char = 0; |
7369 | coding->produced = coding->produced_char = 0; | |
7370 | coding->chars_at_source = 0; | |
065e3595 | 7371 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 | 7372 | coding->errors = 0; |
1c3478b0 | 7373 | |
df7492f9 KH |
7374 | ALLOC_CONVERSION_WORK_AREA (coding); |
7375 | ||
7376 | attrs = CODING_ID_ATTRS (coding->id); | |
2170c8f0 | 7377 | translation_table = get_translation_table (attrs, 0, NULL); |
df7492f9 | 7378 | |
69a80ea3 | 7379 | carryover = 0; |
d0396581 KH |
7380 | if (coding->decoder == decode_coding_ccl) |
7381 | { | |
7382 | coding->spec.ccl = &cclspec; | |
7383 | setup_ccl_program (&cclspec.ccl, CODING_CCL_DECODER (coding)); | |
7384 | } | |
df7492f9 | 7385 | do |
b73bfc1c | 7386 | { |
d311d28c | 7387 | ptrdiff_t pos = coding->dst_pos + coding->produced_char; |
69a80ea3 | 7388 | |
df7492f9 KH |
7389 | coding_set_source (coding); |
7390 | coding->annotated = 0; | |
69a80ea3 | 7391 | coding->charbuf_used = carryover; |
df7492f9 | 7392 | (*(coding->decoder)) (coding); |
df7492f9 | 7393 | coding_set_destination (coding); |
69a80ea3 | 7394 | carryover = produce_chars (coding, translation_table, 0); |
df7492f9 | 7395 | if (coding->annotated) |
69a80ea3 KH |
7396 | produce_annotation (coding, pos); |
7397 | for (i = 0; i < carryover; i++) | |
7398 | coding->charbuf[i] | |
7399 | = coding->charbuf[coding->charbuf_used - carryover + i]; | |
d46c5b12 | 7400 | } |
d0396581 KH |
7401 | while (coding->result == CODING_RESULT_INSUFFICIENT_DST |
7402 | || (coding->consumed < coding->src_bytes | |
7403 | && (coding->result == CODING_RESULT_SUCCESS | |
7404 | || coding->result == CODING_RESULT_INVALID_SRC))); | |
d46c5b12 | 7405 | |
69a80ea3 KH |
7406 | if (carryover > 0) |
7407 | { | |
7408 | coding_set_destination (coding); | |
7409 | coding->charbuf_used = carryover; | |
7410 | produce_chars (coding, translation_table, 1); | |
7411 | } | |
7412 | ||
df7492f9 KH |
7413 | coding->carryover_bytes = 0; |
7414 | if (coding->consumed < coding->src_bytes) | |
d46c5b12 | 7415 | { |
df7492f9 | 7416 | int nbytes = coding->src_bytes - coding->consumed; |
8f924df7 | 7417 | const unsigned char *src; |
df7492f9 KH |
7418 | |
7419 | coding_set_source (coding); | |
7420 | coding_set_destination (coding); | |
7421 | src = coding->source + coding->consumed; | |
7422 | ||
7423 | if (coding->mode & CODING_MODE_LAST_BLOCK) | |
1c3478b0 | 7424 | { |
df7492f9 KH |
7425 | /* Flush out unprocessed data as binary chars. We are sure |
7426 | that the number of data is less than the size of | |
7427 | coding->charbuf. */ | |
065e3595 | 7428 | coding->charbuf_used = 0; |
b2dab6c8 JR |
7429 | coding->chars_at_source = 0; |
7430 | ||
df7492f9 | 7431 | while (nbytes-- > 0) |
1c3478b0 | 7432 | { |
df7492f9 | 7433 | int c = *src++; |
98725083 | 7434 | |
1c91457d KH |
7435 | if (c & 0x80) |
7436 | c = BYTE8_TO_CHAR (c); | |
7437 | coding->charbuf[coding->charbuf_used++] = c; | |
1c3478b0 | 7438 | } |
f6cbaf43 | 7439 | produce_chars (coding, Qnil, 1); |
d46c5b12 | 7440 | } |
d46c5b12 | 7441 | else |
df7492f9 KH |
7442 | { |
7443 | /* Record unprocessed bytes in coding->carryover. We are | |
7444 | sure that the number of data is less than the size of | |
7445 | coding->carryover. */ | |
7446 | unsigned char *p = coding->carryover; | |
7447 | ||
f289d375 KH |
7448 | if (nbytes > sizeof coding->carryover) |
7449 | nbytes = sizeof coding->carryover; | |
df7492f9 KH |
7450 | coding->carryover_bytes = nbytes; |
7451 | while (nbytes-- > 0) | |
7452 | *p++ = *src++; | |
1c3478b0 | 7453 | } |
df7492f9 | 7454 | coding->consumed = coding->src_bytes; |
b73bfc1c | 7455 | } |
69f76525 | 7456 | |
0a9564cb EZ |
7457 | if (! EQ (CODING_ID_EOL_TYPE (coding->id), Qunix) |
7458 | && !inhibit_eol_conversion) | |
4347441b | 7459 | decode_eol (coding); |
24a73b0a KH |
7460 | if (BUFFERP (coding->dst_object)) |
7461 | { | |
39eb03f1 | 7462 | bset_undo_list (current_buffer, undo_list); |
24a73b0a KH |
7463 | record_insert (coding->dst_pos, coding->produced_char); |
7464 | } | |
1af1a51a DA |
7465 | |
7466 | SAFE_FREE (); | |
4ed46869 KH |
7467 | } |
7468 | ||
aaaf0b1e | 7469 | |
e1c23804 | 7470 | /* Extract an annotation datum from a composition starting at POS and |
ff0dacd7 KH |
7471 | ending before LIMIT of CODING->src_object (buffer or string), store |
7472 | the data in BUF, set *STOP to a starting position of the next | |
7473 | composition (if any) or to LIMIT, and return the address of the | |
7474 | next element of BUF. | |
7475 | ||
7476 | If such an annotation is not found, set *STOP to a starting | |
7477 | position of a composition after POS (if any) or to LIMIT, and | |
7478 | return BUF. */ | |
7479 | ||
b0ab8123 | 7480 | static int * |
d311d28c | 7481 | handle_composition_annotation (ptrdiff_t pos, ptrdiff_t limit, |
cf84bb53 | 7482 | struct coding_system *coding, int *buf, |
d311d28c | 7483 | ptrdiff_t *stop) |
aaaf0b1e | 7484 | { |
d311d28c | 7485 | ptrdiff_t start, end; |
ff0dacd7 | 7486 | Lisp_Object prop; |
aaaf0b1e | 7487 | |
ff0dacd7 KH |
7488 | if (! find_composition (pos, limit, &start, &end, &prop, coding->src_object) |
7489 | || end > limit) | |
7490 | *stop = limit; | |
7491 | else if (start > pos) | |
7492 | *stop = start; | |
7493 | else | |
aaaf0b1e | 7494 | { |
ff0dacd7 | 7495 | if (start == pos) |
aaaf0b1e | 7496 | { |
ff0dacd7 KH |
7497 | /* We found a composition. Store the corresponding |
7498 | annotation data in BUF. */ | |
7499 | int *head = buf; | |
75c59fb3 | 7500 | enum composition_method method = composition_method (prop); |
ff0dacd7 KH |
7501 | int nchars = COMPOSITION_LENGTH (prop); |
7502 | ||
e951386e | 7503 | ADD_COMPOSITION_DATA (buf, nchars, 0, method); |
ff0dacd7 | 7504 | if (method != COMPOSITION_RELATIVE) |
aaaf0b1e | 7505 | { |
ff0dacd7 | 7506 | Lisp_Object components; |
2c6a9faa | 7507 | ptrdiff_t i, len, i_byte; |
ff0dacd7 KH |
7508 | |
7509 | components = COMPOSITION_COMPONENTS (prop); | |
7510 | if (VECTORP (components)) | |
aaaf0b1e | 7511 | { |
77b37c05 | 7512 | len = ASIZE (components); |
ff0dacd7 KH |
7513 | for (i = 0; i < len; i++) |
7514 | *buf++ = XINT (AREF (components, i)); | |
aaaf0b1e | 7515 | } |
ff0dacd7 | 7516 | else if (STRINGP (components)) |
aaaf0b1e | 7517 | { |
8f924df7 | 7518 | len = SCHARS (components); |
ff0dacd7 KH |
7519 | i = i_byte = 0; |
7520 | while (i < len) | |
7521 | { | |
7522 | FETCH_STRING_CHAR_ADVANCE (*buf, components, i, i_byte); | |
7523 | buf++; | |
7524 | } | |
7525 | } | |
7526 | else if (INTEGERP (components)) | |
7527 | { | |
7528 | len = 1; | |
7529 | *buf++ = XINT (components); | |
7530 | } | |
7531 | else if (CONSP (components)) | |
7532 | { | |
7533 | for (len = 0; CONSP (components); | |
7534 | len++, components = XCDR (components)) | |
7535 | *buf++ = XINT (XCAR (components)); | |
aaaf0b1e | 7536 | } |
aaaf0b1e | 7537 | else |
1088b922 | 7538 | emacs_abort (); |
ff0dacd7 | 7539 | *head -= len; |
aaaf0b1e | 7540 | } |
aaaf0b1e | 7541 | } |
ff0dacd7 KH |
7542 | |
7543 | if (find_composition (end, limit, &start, &end, &prop, | |
7544 | coding->src_object) | |
7545 | && end <= limit) | |
7546 | *stop = start; | |
7547 | else | |
7548 | *stop = limit; | |
aaaf0b1e | 7549 | } |
ff0dacd7 KH |
7550 | return buf; |
7551 | } | |
7552 | ||
7553 | ||
e1c23804 | 7554 | /* Extract an annotation datum from a text property `charset' at POS of |
ff0dacd7 KH |
7555 | CODING->src_object (buffer of string), store the data in BUF, set |
7556 | *STOP to the position where the value of `charset' property changes | |
7557 | (limiting by LIMIT), and return the address of the next element of | |
7558 | BUF. | |
7559 | ||
7560 | If the property value is nil, set *STOP to the position where the | |
7561 | property value is non-nil (limiting by LIMIT), and return BUF. */ | |
7562 | ||
b0ab8123 | 7563 | static int * |
d311d28c | 7564 | handle_charset_annotation (ptrdiff_t pos, ptrdiff_t limit, |
cf84bb53 | 7565 | struct coding_system *coding, int *buf, |
d311d28c | 7566 | ptrdiff_t *stop) |
ff0dacd7 KH |
7567 | { |
7568 | Lisp_Object val, next; | |
7569 | int id; | |
7570 | ||
7571 | val = Fget_text_property (make_number (pos), Qcharset, coding->src_object); | |
7572 | if (! NILP (val) && CHARSETP (val)) | |
7573 | id = XINT (CHARSET_SYMBOL_ID (val)); | |
7574 | else | |
7575 | id = -1; | |
69a80ea3 | 7576 | ADD_CHARSET_DATA (buf, 0, id); |
ff0dacd7 KH |
7577 | next = Fnext_single_property_change (make_number (pos), Qcharset, |
7578 | coding->src_object, | |
7579 | make_number (limit)); | |
7580 | *stop = XINT (next); | |
7581 | return buf; | |
7582 | } | |
7583 | ||
7584 | ||
df7492f9 | 7585 | static void |
cf84bb53 JB |
7586 | consume_chars (struct coding_system *coding, Lisp_Object translation_table, |
7587 | int max_lookup) | |
df7492f9 KH |
7588 | { |
7589 | int *buf = coding->charbuf; | |
ff0dacd7 | 7590 | int *buf_end = coding->charbuf + coding->charbuf_size; |
7c78e542 | 7591 | const unsigned char *src = coding->source + coding->consumed; |
4776e638 | 7592 | const unsigned char *src_end = coding->source + coding->src_bytes; |
d311d28c PE |
7593 | ptrdiff_t pos = coding->src_pos + coding->consumed_char; |
7594 | ptrdiff_t end_pos = coding->src_pos + coding->src_chars; | |
f10fe38f | 7595 | bool multibytep = coding->src_multibyte; |
df7492f9 KH |
7596 | Lisp_Object eol_type; |
7597 | int c; | |
d311d28c | 7598 | ptrdiff_t stop, stop_composition, stop_charset; |
09ee6fdd | 7599 | int *lookup_buf = NULL; |
433f7f87 KH |
7600 | |
7601 | if (! NILP (translation_table)) | |
09ee6fdd | 7602 | lookup_buf = alloca (sizeof (int) * max_lookup); |
88993dfd | 7603 | |
0a9564cb | 7604 | eol_type = inhibit_eol_conversion ? Qunix : CODING_ID_EOL_TYPE (coding->id); |
df7492f9 KH |
7605 | if (VECTORP (eol_type)) |
7606 | eol_type = Qunix; | |
88993dfd | 7607 | |
df7492f9 KH |
7608 | /* Note: composition handling is not yet implemented. */ |
7609 | coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK; | |
ec6d2bb8 | 7610 | |
0b5670c9 KH |
7611 | if (NILP (coding->src_object)) |
7612 | stop = stop_composition = stop_charset = end_pos; | |
ff0dacd7 | 7613 | else |
0b5670c9 KH |
7614 | { |
7615 | if (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK) | |
7616 | stop = stop_composition = pos; | |
7617 | else | |
7618 | stop = stop_composition = end_pos; | |
7619 | if (coding->common_flags & CODING_ANNOTATE_CHARSET_MASK) | |
7620 | stop = stop_charset = pos; | |
7621 | else | |
7622 | stop_charset = end_pos; | |
7623 | } | |
ec6d2bb8 | 7624 | |
24a73b0a | 7625 | /* Compensate for CRLF and conversion. */ |
ff0dacd7 | 7626 | buf_end -= 1 + MAX_ANNOTATION_LENGTH; |
df7492f9 | 7627 | while (buf < buf_end) |
aaaf0b1e | 7628 | { |
433f7f87 KH |
7629 | Lisp_Object trans; |
7630 | ||
df7492f9 | 7631 | if (pos == stop) |
ec6d2bb8 | 7632 | { |
df7492f9 KH |
7633 | if (pos == end_pos) |
7634 | break; | |
ff0dacd7 KH |
7635 | if (pos == stop_composition) |
7636 | buf = handle_composition_annotation (pos, end_pos, coding, | |
7637 | buf, &stop_composition); | |
7638 | if (pos == stop_charset) | |
7639 | buf = handle_charset_annotation (pos, end_pos, coding, | |
7640 | buf, &stop_charset); | |
7641 | stop = (stop_composition < stop_charset | |
7642 | ? stop_composition : stop_charset); | |
df7492f9 KH |
7643 | } |
7644 | ||
7645 | if (! multibytep) | |
4776e638 | 7646 | { |
d311d28c | 7647 | int bytes; |
aaaf0b1e | 7648 | |
4d1e6632 KH |
7649 | if (coding->encoder == encode_coding_raw_text |
7650 | || coding->encoder == encode_coding_ccl) | |
ea29edf2 KH |
7651 | c = *src++, pos++; |
7652 | else if ((bytes = MULTIBYTE_LENGTH (src, src_end)) > 0) | |
db274c7a | 7653 | c = STRING_CHAR_ADVANCE_NO_UNIFY (src), pos += bytes; |
4776e638 | 7654 | else |
f03caae0 | 7655 | c = BYTE8_TO_CHAR (*src), src++, pos++; |
4776e638 | 7656 | } |
df7492f9 | 7657 | else |
db274c7a | 7658 | c = STRING_CHAR_ADVANCE_NO_UNIFY (src), pos++; |
df7492f9 KH |
7659 | if ((c == '\r') && (coding->mode & CODING_MODE_SELECTIVE_DISPLAY)) |
7660 | c = '\n'; | |
7661 | if (! EQ (eol_type, Qunix)) | |
aaaf0b1e | 7662 | { |
df7492f9 | 7663 | if (c == '\n') |
aaaf0b1e | 7664 | { |
df7492f9 KH |
7665 | if (EQ (eol_type, Qdos)) |
7666 | *buf++ = '\r'; | |
7667 | else | |
7668 | c = '\r'; | |
aaaf0b1e KH |
7669 | } |
7670 | } | |
433f7f87 | 7671 | |
e6a54062 | 7672 | trans = Qnil; |
09ee6fdd | 7673 | LOOKUP_TRANSLATION_TABLE (translation_table, c, trans); |
e6a54062 | 7674 | if (NILP (trans)) |
433f7f87 KH |
7675 | *buf++ = c; |
7676 | else | |
7677 | { | |
2c6a9faa | 7678 | ptrdiff_t from_nchars = 1, to_nchars = 1; |
433f7f87 KH |
7679 | int *lookup_buf_end; |
7680 | const unsigned char *p = src; | |
7681 | int i; | |
7682 | ||
7683 | lookup_buf[0] = c; | |
7684 | for (i = 1; i < max_lookup && p < src_end; i++) | |
7685 | lookup_buf[i] = STRING_CHAR_ADVANCE (p); | |
7686 | lookup_buf_end = lookup_buf + i; | |
e951386e KH |
7687 | trans = get_translation (trans, lookup_buf, lookup_buf_end); |
7688 | if (INTEGERP (trans)) | |
7689 | c = XINT (trans); | |
7690 | else if (CONSP (trans)) | |
7691 | { | |
7692 | from_nchars = ASIZE (XCAR (trans)); | |
7693 | trans = XCDR (trans); | |
7694 | if (INTEGERP (trans)) | |
7695 | c = XINT (trans); | |
7696 | else | |
7697 | { | |
7698 | to_nchars = ASIZE (trans); | |
2c6a9faa | 7699 | if (buf_end - buf < to_nchars) |
e951386e KH |
7700 | break; |
7701 | c = XINT (AREF (trans, 0)); | |
7702 | } | |
7703 | } | |
7704 | else | |
433f7f87 | 7705 | break; |
e951386e | 7706 | *buf++ = c; |
433f7f87 KH |
7707 | for (i = 1; i < to_nchars; i++) |
7708 | *buf++ = XINT (AREF (trans, i)); | |
7709 | for (i = 1; i < from_nchars; i++, pos++) | |
7710 | src += MULTIBYTE_LENGTH_NO_CHECK (src); | |
7711 | } | |
aaaf0b1e | 7712 | } |
ec6d2bb8 | 7713 | |
df7492f9 KH |
7714 | coding->consumed = src - coding->source; |
7715 | coding->consumed_char = pos - coding->src_pos; | |
7716 | coding->charbuf_used = buf - coding->charbuf; | |
7717 | coding->chars_at_source = 0; | |
aaaf0b1e KH |
7718 | } |
7719 | ||
4ed46869 | 7720 | |
df7492f9 KH |
7721 | /* Encode the text at CODING->src_object into CODING->dst_object. |
7722 | CODING->src_object is a buffer or a string. | |
7723 | CODING->dst_object is a buffer or nil. | |
7724 | ||
7725 | If CODING->src_object is a buffer, it must be the current buffer. | |
7726 | In this case, if CODING->src_pos is positive, it is a position of | |
7727 | the source text in the buffer, otherwise. the source text is in the | |
7728 | gap area of the buffer, and coding->src_pos specifies the offset of | |
7729 | the text from GPT (which must be the same as PT). If this is the | |
7730 | same buffer as CODING->dst_object, CODING->src_pos must be | |
7731 | negative and CODING should not have `pre-write-conversion'. | |
7732 | ||
7733 | If CODING->src_object is a string, CODING should not have | |
7734 | `pre-write-conversion'. | |
7735 | ||
7736 | If CODING->dst_object is a buffer, the encoded data is inserted at | |
7737 | the current point of that buffer. | |
7738 | ||
7739 | If CODING->dst_object is nil, the encoded data is placed at the | |
7740 | memory area specified by CODING->destination. */ | |
7741 | ||
f10fe38f | 7742 | static void |
971de7fb | 7743 | encode_coding (struct coding_system *coding) |
4ed46869 | 7744 | { |
df7492f9 | 7745 | Lisp_Object attrs; |
7d64c6ad | 7746 | Lisp_Object translation_table; |
09ee6fdd | 7747 | int max_lookup; |
fb608df3 | 7748 | struct ccl_spec cclspec; |
9861e777 | 7749 | |
1af1a51a DA |
7750 | USE_SAFE_ALLOCA; |
7751 | ||
df7492f9 | 7752 | attrs = CODING_ID_ATTRS (coding->id); |
ea29edf2 KH |
7753 | if (coding->encoder == encode_coding_raw_text) |
7754 | translation_table = Qnil, max_lookup = 0; | |
7755 | else | |
7756 | translation_table = get_translation_table (attrs, 1, &max_lookup); | |
4ed46869 | 7757 | |
df7492f9 | 7758 | if (BUFFERP (coding->dst_object)) |
8844fa83 | 7759 | { |
df7492f9 KH |
7760 | set_buffer_internal (XBUFFER (coding->dst_object)); |
7761 | coding->dst_multibyte | |
4b4deea2 | 7762 | = ! NILP (BVAR (current_buffer, enable_multibyte_characters)); |
8844fa83 | 7763 | } |
4ed46869 | 7764 | |
b73bfc1c | 7765 | coding->consumed = coding->consumed_char = 0; |
df7492f9 | 7766 | coding->produced = coding->produced_char = 0; |
065e3595 | 7767 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
b73bfc1c | 7768 | coding->errors = 0; |
b73bfc1c | 7769 | |
df7492f9 | 7770 | ALLOC_CONVERSION_WORK_AREA (coding); |
4ed46869 | 7771 | |
fb608df3 KH |
7772 | if (coding->encoder == encode_coding_ccl) |
7773 | { | |
7774 | coding->spec.ccl = &cclspec; | |
7775 | setup_ccl_program (&cclspec.ccl, CODING_CCL_ENCODER (coding)); | |
7776 | } | |
df7492f9 KH |
7777 | do { |
7778 | coding_set_source (coding); | |
09ee6fdd | 7779 | consume_chars (coding, translation_table, max_lookup); |
df7492f9 KH |
7780 | coding_set_destination (coding); |
7781 | (*(coding->encoder)) (coding); | |
7782 | } while (coding->consumed_char < coding->src_chars); | |
7783 | ||
284201e4 | 7784 | if (BUFFERP (coding->dst_object) && coding->produced_char > 0) |
8a44e6d1 | 7785 | insert_from_gap (coding->produced_char, coding->produced, 0); |
1af1a51a DA |
7786 | |
7787 | SAFE_FREE (); | |
ec6d2bb8 KH |
7788 | } |
7789 | ||
fb88bf2d | 7790 | |
24a73b0a KH |
7791 | /* Name (or base name) of work buffer for code conversion. */ |
7792 | static Lisp_Object Vcode_conversion_workbuf_name; | |
d46c5b12 | 7793 | |
24a73b0a KH |
7794 | /* A working buffer used by the top level conversion. Once it is |
7795 | created, it is never destroyed. It has the name | |
7796 | Vcode_conversion_workbuf_name. The other working buffers are | |
7797 | destroyed after the use is finished, and their names are modified | |
7798 | versions of Vcode_conversion_workbuf_name. */ | |
7799 | static Lisp_Object Vcode_conversion_reused_workbuf; | |
b73bfc1c | 7800 | |
f10fe38f PE |
7801 | /* True iff Vcode_conversion_reused_workbuf is already in use. */ |
7802 | static bool reused_workbuf_in_use; | |
4ed46869 | 7803 | |
24a73b0a | 7804 | |
ad1746f5 | 7805 | /* Return a working buffer of code conversion. MULTIBYTE specifies the |
24a73b0a | 7806 | multibyteness of returning buffer. */ |
b73bfc1c | 7807 | |
f6cbaf43 | 7808 | static Lisp_Object |
f10fe38f | 7809 | make_conversion_work_buffer (bool multibyte) |
df7492f9 | 7810 | { |
24a73b0a KH |
7811 | Lisp_Object name, workbuf; |
7812 | struct buffer *current; | |
4ed46869 | 7813 | |
f10fe38f | 7814 | if (reused_workbuf_in_use) |
065e3595 KH |
7815 | { |
7816 | name = Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name, Qnil); | |
7817 | workbuf = Fget_buffer_create (name); | |
7818 | } | |
df7492f9 | 7819 | else |
065e3595 | 7820 | { |
f10fe38f | 7821 | reused_workbuf_in_use = 1; |
159bd5a2 | 7822 | if (NILP (Fbuffer_live_p (Vcode_conversion_reused_workbuf))) |
a993c7a1 KH |
7823 | Vcode_conversion_reused_workbuf |
7824 | = Fget_buffer_create (Vcode_conversion_workbuf_name); | |
7825 | workbuf = Vcode_conversion_reused_workbuf; | |
065e3595 | 7826 | } |
24a73b0a KH |
7827 | current = current_buffer; |
7828 | set_buffer_internal (XBUFFER (workbuf)); | |
df36ff1f CY |
7829 | /* We can't allow modification hooks to run in the work buffer. For |
7830 | instance, directory_files_internal assumes that file decoding | |
7831 | doesn't compile new regexps. */ | |
7832 | Fset (Fmake_local_variable (Qinhibit_modification_hooks), Qt); | |
3ed051d4 | 7833 | Ferase_buffer (); |
39eb03f1 PE |
7834 | bset_undo_list (current_buffer, Qt); |
7835 | bset_enable_multibyte_characters (current_buffer, multibyte ? Qt : Qnil); | |
df7492f9 | 7836 | set_buffer_internal (current); |
24a73b0a | 7837 | return workbuf; |
df7492f9 | 7838 | } |
d46c5b12 | 7839 | |
24a73b0a | 7840 | |
27e498e6 | 7841 | static void |
971de7fb | 7842 | code_conversion_restore (Lisp_Object arg) |
4776e638 | 7843 | { |
24a73b0a | 7844 | Lisp_Object current, workbuf; |
948bdcf3 | 7845 | struct gcpro gcpro1; |
24a73b0a | 7846 | |
948bdcf3 | 7847 | GCPRO1 (arg); |
24a73b0a KH |
7848 | current = XCAR (arg); |
7849 | workbuf = XCDR (arg); | |
7850 | if (! NILP (workbuf)) | |
7851 | { | |
7852 | if (EQ (workbuf, Vcode_conversion_reused_workbuf)) | |
7853 | reused_workbuf_in_use = 0; | |
d17337e5 | 7854 | else |
24a73b0a KH |
7855 | Fkill_buffer (workbuf); |
7856 | } | |
7857 | set_buffer_internal (XBUFFER (current)); | |
948bdcf3 | 7858 | UNGCPRO; |
4776e638 | 7859 | } |
b73bfc1c | 7860 | |
24a73b0a | 7861 | Lisp_Object |
f10fe38f | 7862 | code_conversion_save (bool with_work_buf, bool multibyte) |
df7492f9 | 7863 | { |
24a73b0a | 7864 | Lisp_Object workbuf = Qnil; |
b73bfc1c | 7865 | |
4776e638 | 7866 | if (with_work_buf) |
24a73b0a KH |
7867 | workbuf = make_conversion_work_buffer (multibyte); |
7868 | record_unwind_protect (code_conversion_restore, | |
7869 | Fcons (Fcurrent_buffer (), workbuf)); | |
4776e638 | 7870 | return workbuf; |
df7492f9 | 7871 | } |
d46c5b12 | 7872 | |
f10fe38f | 7873 | void |
cf84bb53 | 7874 | decode_coding_gap (struct coding_system *coding, |
d311d28c | 7875 | ptrdiff_t chars, ptrdiff_t bytes) |
df7492f9 | 7876 | { |
d311d28c | 7877 | ptrdiff_t count = SPECPDL_INDEX (); |
5e5c78be | 7878 | Lisp_Object attrs; |
fb88bf2d | 7879 | |
24a73b0a | 7880 | coding->src_object = Fcurrent_buffer (); |
df7492f9 KH |
7881 | coding->src_chars = chars; |
7882 | coding->src_bytes = bytes; | |
7883 | coding->src_pos = -chars; | |
7884 | coding->src_pos_byte = -bytes; | |
7885 | coding->src_multibyte = chars < bytes; | |
24a73b0a | 7886 | coding->dst_object = coding->src_object; |
df7492f9 KH |
7887 | coding->dst_pos = PT; |
7888 | coding->dst_pos_byte = PT_BYTE; | |
4b4deea2 | 7889 | coding->dst_multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters)); |
4ed46869 | 7890 | |
e6d2f155 KH |
7891 | coding->head_ascii = -1; |
7892 | coding->detected_utf8_chars = -1; | |
7893 | coding->eol_seen = EOL_SEEN_NONE; | |
df7492f9 KH |
7894 | if (CODING_REQUIRE_DETECTION (coding)) |
7895 | detect_coding (coding); | |
c230dd7d | 7896 | attrs = CODING_ID_ATTRS (coding->id); |
251e9147 KH |
7897 | if (! disable_ascii_optimization |
7898 | && ! coding->src_multibyte | |
7899 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)) | |
7900 | && NILP (CODING_ATTR_POST_READ (attrs)) | |
7901 | && NILP (get_translation_table (attrs, 0, NULL))) | |
7902 | { | |
7903 | chars = coding->head_ascii; | |
7904 | if (chars < 0) | |
7905 | chars = check_ascii (coding); | |
7906 | if (chars != bytes) | |
7907 | { | |
e6d2f155 | 7908 | /* There exists a non-ASCII byte. */ |
251e9147 | 7909 | if (EQ (CODING_ATTR_TYPE (attrs), Qutf_8)) |
e6d2f155 KH |
7910 | { |
7911 | if (coding->detected_utf8_chars >= 0) | |
7912 | chars = coding->detected_utf8_chars; | |
7913 | else | |
7914 | chars = check_utf_8 (coding); | |
7915 | if (CODING_UTF_8_BOM (coding) != utf_without_bom | |
7916 | && coding->head_ascii == 0 | |
7917 | && coding->source[0] == UTF_8_BOM_1 | |
7918 | && coding->source[1] == UTF_8_BOM_2 | |
7919 | && coding->source[2] == UTF_8_BOM_3) | |
7920 | { | |
7921 | chars--; | |
7922 | bytes -= 3; | |
7923 | coding->src_bytes -= 3; | |
7924 | } | |
7925 | } | |
251e9147 KH |
7926 | else |
7927 | chars = -1; | |
7928 | } | |
7929 | if (chars >= 0) | |
c230dd7d | 7930 | { |
e6d2f155 | 7931 | Lisp_Object eol_type; |
251e9147 | 7932 | |
e6d2f155 KH |
7933 | eol_type = CODING_ID_EOL_TYPE (coding->id); |
7934 | if (VECTORP (eol_type)) | |
7935 | { | |
7936 | if (coding->eol_seen != EOL_SEEN_NONE) | |
7937 | eol_type = adjust_coding_eol_type (coding, coding->eol_seen); | |
7938 | } | |
7939 | if (EQ (eol_type, Qmac)) | |
8a44e6d1 KH |
7940 | { |
7941 | unsigned char *src_end = GAP_END_ADDR; | |
cded56c1 | 7942 | unsigned char *src = src_end - coding->src_bytes; |
c230dd7d | 7943 | |
8a44e6d1 KH |
7944 | while (src < src_end) |
7945 | { | |
7946 | if (*src++ == '\r') | |
7947 | src[-1] = '\n'; | |
7948 | } | |
7949 | } | |
e6d2f155 | 7950 | else if (EQ (eol_type, Qdos)) |
c230dd7d | 7951 | { |
8a44e6d1 KH |
7952 | unsigned char *src = GAP_END_ADDR; |
7953 | unsigned char *src_beg = src - coding->src_bytes; | |
7954 | unsigned char *dst = src; | |
251e9147 | 7955 | ptrdiff_t diff; |
8a44e6d1 KH |
7956 | |
7957 | while (src_beg < src) | |
7958 | { | |
7959 | *--dst = *--src; | |
9e63b4a5 | 7960 | if (*src == '\n' && src > src_beg && src[-1] == '\r') |
8a44e6d1 KH |
7961 | src--; |
7962 | } | |
251e9147 KH |
7963 | diff = dst - src; |
7964 | bytes -= diff; | |
7965 | chars -= diff; | |
c230dd7d | 7966 | } |
251e9147 KH |
7967 | coding->produced = bytes; |
7968 | coding->produced_char = chars; | |
7969 | insert_from_gap (chars, bytes, 1); | |
c230dd7d KH |
7970 | return; |
7971 | } | |
7972 | } | |
c230dd7d | 7973 | code_conversion_save (0, 0); |
8f924df7 | 7974 | |
9286b333 | 7975 | coding->mode |= CODING_MODE_LAST_BLOCK; |
287c57d7 | 7976 | current_buffer->text->inhibit_shrinking = 1; |
df7492f9 | 7977 | decode_coding (coding); |
287c57d7 | 7978 | current_buffer->text->inhibit_shrinking = 0; |
d46c5b12 | 7979 | |
5e5c78be | 7980 | if (! NILP (CODING_ATTR_POST_READ (attrs))) |
b73bfc1c | 7981 | { |
d311d28c | 7982 | ptrdiff_t prev_Z = Z, prev_Z_BYTE = Z_BYTE; |
5e5c78be KH |
7983 | Lisp_Object val; |
7984 | ||
7985 | TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte); | |
5e5c78be KH |
7986 | val = call1 (CODING_ATTR_POST_READ (attrs), |
7987 | make_number (coding->produced_char)); | |
5e5c78be KH |
7988 | CHECK_NATNUM (val); |
7989 | coding->produced_char += Z - prev_Z; | |
7990 | coding->produced += Z_BYTE - prev_Z_BYTE; | |
b73bfc1c | 7991 | } |
4ed46869 | 7992 | |
df7492f9 | 7993 | unbind_to (count, Qnil); |
b73bfc1c | 7994 | } |
52d41803 | 7995 | |
d46c5b12 | 7996 | |
df7492f9 KH |
7997 | /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in |
7998 | SRC_OBJECT into DST_OBJECT by coding context CODING. | |
b73bfc1c | 7999 | |
df7492f9 | 8000 | SRC_OBJECT is a buffer, a string, or Qnil. |
b73bfc1c | 8001 | |
df7492f9 KH |
8002 | If it is a buffer, the text is at point of the buffer. FROM and TO |
8003 | are positions in the buffer. | |
b73bfc1c | 8004 | |
df7492f9 KH |
8005 | If it is a string, the text is at the beginning of the string. |
8006 | FROM and TO are indices to the string. | |
4ed46869 | 8007 | |
df7492f9 KH |
8008 | If it is nil, the text is at coding->source. FROM and TO are |
8009 | indices to coding->source. | |
bb10be8b | 8010 | |
df7492f9 | 8011 | DST_OBJECT is a buffer, Qt, or Qnil. |
4ed46869 | 8012 | |
df7492f9 KH |
8013 | If it is a buffer, the decoded text is inserted at point of the |
8014 | buffer. If the buffer is the same as SRC_OBJECT, the source text | |
8015 | is deleted. | |
4ed46869 | 8016 | |
df7492f9 KH |
8017 | If it is Qt, a string is made from the decoded text, and |
8018 | set in CODING->dst_object. | |
d46c5b12 | 8019 | |
df7492f9 | 8020 | If it is Qnil, the decoded text is stored at CODING->destination. |
2cb26057 | 8021 | The caller must allocate CODING->dst_bytes bytes at |
df7492f9 KH |
8022 | CODING->destination by xmalloc. If the decoded text is longer than |
8023 | CODING->dst_bytes, CODING->destination is relocated by xrealloc. | |
8024 | */ | |
d46c5b12 | 8025 | |
df7492f9 | 8026 | void |
cf84bb53 JB |
8027 | decode_coding_object (struct coding_system *coding, |
8028 | Lisp_Object src_object, | |
d311d28c PE |
8029 | ptrdiff_t from, ptrdiff_t from_byte, |
8030 | ptrdiff_t to, ptrdiff_t to_byte, | |
cf84bb53 | 8031 | Lisp_Object dst_object) |
d46c5b12 | 8032 | { |
d311d28c | 8033 | ptrdiff_t count = SPECPDL_INDEX (); |
c4a63b12 | 8034 | unsigned char *destination IF_LINT (= NULL); |
d311d28c PE |
8035 | ptrdiff_t dst_bytes IF_LINT (= 0); |
8036 | ptrdiff_t chars = to - from; | |
8037 | ptrdiff_t bytes = to_byte - from_byte; | |
df7492f9 | 8038 | Lisp_Object attrs; |
f10fe38f PE |
8039 | ptrdiff_t saved_pt = -1, saved_pt_byte IF_LINT (= 0); |
8040 | bool need_marker_adjustment = 0; | |
b3bfad50 | 8041 | Lisp_Object old_deactivate_mark; |
d46c5b12 | 8042 | |
b3bfad50 | 8043 | old_deactivate_mark = Vdeactivate_mark; |
93dec019 | 8044 | |
df7492f9 | 8045 | if (NILP (dst_object)) |
d46c5b12 | 8046 | { |
df7492f9 KH |
8047 | destination = coding->destination; |
8048 | dst_bytes = coding->dst_bytes; | |
d46c5b12 | 8049 | } |
93dec019 | 8050 | |
df7492f9 KH |
8051 | coding->src_object = src_object; |
8052 | coding->src_chars = chars; | |
8053 | coding->src_bytes = bytes; | |
8054 | coding->src_multibyte = chars < bytes; | |
70ad9fc4 | 8055 | |
df7492f9 | 8056 | if (STRINGP (src_object)) |
d46c5b12 | 8057 | { |
df7492f9 KH |
8058 | coding->src_pos = from; |
8059 | coding->src_pos_byte = from_byte; | |
d46c5b12 | 8060 | } |
df7492f9 | 8061 | else if (BUFFERP (src_object)) |
88993dfd | 8062 | { |
df7492f9 KH |
8063 | set_buffer_internal (XBUFFER (src_object)); |
8064 | if (from != GPT) | |
8065 | move_gap_both (from, from_byte); | |
8066 | if (EQ (src_object, dst_object)) | |
fb88bf2d | 8067 | { |
64cedb0c KH |
8068 | struct Lisp_Marker *tail; |
8069 | ||
8070 | for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next) | |
8071 | { | |
8072 | tail->need_adjustment | |
8073 | = tail->charpos == (tail->insertion_type ? from : to); | |
8074 | need_marker_adjustment |= tail->need_adjustment; | |
8075 | } | |
4776e638 | 8076 | saved_pt = PT, saved_pt_byte = PT_BYTE; |
df7492f9 | 8077 | TEMP_SET_PT_BOTH (from, from_byte); |
f4a3cc44 | 8078 | current_buffer->text->inhibit_shrinking = 1; |
df7492f9 KH |
8079 | del_range_both (from, from_byte, to, to_byte, 1); |
8080 | coding->src_pos = -chars; | |
8081 | coding->src_pos_byte = -bytes; | |
fb88bf2d | 8082 | } |
df7492f9 | 8083 | else |
fb88bf2d | 8084 | { |
df7492f9 KH |
8085 | coding->src_pos = from; |
8086 | coding->src_pos_byte = from_byte; | |
fb88bf2d | 8087 | } |
88993dfd KH |
8088 | } |
8089 | ||
df7492f9 KH |
8090 | if (CODING_REQUIRE_DETECTION (coding)) |
8091 | detect_coding (coding); | |
8092 | attrs = CODING_ID_ATTRS (coding->id); | |
d46c5b12 | 8093 | |
2cb26057 KH |
8094 | if (EQ (dst_object, Qt) |
8095 | || (! NILP (CODING_ATTR_POST_READ (attrs)) | |
8096 | && NILP (dst_object))) | |
b73bfc1c | 8097 | { |
a1567c45 SM |
8098 | coding->dst_multibyte = !CODING_FOR_UNIBYTE (coding); |
8099 | coding->dst_object = code_conversion_save (1, coding->dst_multibyte); | |
df7492f9 KH |
8100 | coding->dst_pos = BEG; |
8101 | coding->dst_pos_byte = BEG_BYTE; | |
b73bfc1c | 8102 | } |
df7492f9 | 8103 | else if (BUFFERP (dst_object)) |
d46c5b12 | 8104 | { |
24a73b0a | 8105 | code_conversion_save (0, 0); |
df7492f9 KH |
8106 | coding->dst_object = dst_object; |
8107 | coding->dst_pos = BUF_PT (XBUFFER (dst_object)); | |
8108 | coding->dst_pos_byte = BUF_PT_BYTE (XBUFFER (dst_object)); | |
8109 | coding->dst_multibyte | |
4b4deea2 | 8110 | = ! NILP (BVAR (XBUFFER (dst_object), enable_multibyte_characters)); |
d46c5b12 KH |
8111 | } |
8112 | else | |
8113 | { | |
24a73b0a | 8114 | code_conversion_save (0, 0); |
df7492f9 | 8115 | coding->dst_object = Qnil; |
0154725e SM |
8116 | /* Most callers presume this will return a multibyte result, and they |
8117 | won't use `binary' or `raw-text' anyway, so let's not worry about | |
8118 | CODING_FOR_UNIBYTE. */ | |
bb555731 | 8119 | coding->dst_multibyte = 1; |
d46c5b12 KH |
8120 | } |
8121 | ||
df7492f9 | 8122 | decode_coding (coding); |
fa46990e | 8123 | |
df7492f9 KH |
8124 | if (BUFFERP (coding->dst_object)) |
8125 | set_buffer_internal (XBUFFER (coding->dst_object)); | |
d46c5b12 | 8126 | |
df7492f9 | 8127 | if (! NILP (CODING_ATTR_POST_READ (attrs))) |
d46c5b12 | 8128 | { |
b3bfad50 | 8129 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; |
d311d28c | 8130 | ptrdiff_t prev_Z = Z, prev_Z_BYTE = Z_BYTE; |
2b4f9037 | 8131 | Lisp_Object val; |
d46c5b12 | 8132 | |
c0cc7f7f | 8133 | TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte); |
b3bfad50 KH |
8134 | GCPRO5 (coding->src_object, coding->dst_object, src_object, dst_object, |
8135 | old_deactivate_mark); | |
d4850d67 KH |
8136 | val = safe_call1 (CODING_ATTR_POST_READ (attrs), |
8137 | make_number (coding->produced_char)); | |
df7492f9 KH |
8138 | UNGCPRO; |
8139 | CHECK_NATNUM (val); | |
8140 | coding->produced_char += Z - prev_Z; | |
8141 | coding->produced += Z_BYTE - prev_Z_BYTE; | |
d46c5b12 | 8142 | } |
de79a6a5 | 8143 | |
df7492f9 | 8144 | if (EQ (dst_object, Qt)) |
ec6d2bb8 | 8145 | { |
df7492f9 KH |
8146 | coding->dst_object = Fbuffer_string (); |
8147 | } | |
8148 | else if (NILP (dst_object) && BUFFERP (coding->dst_object)) | |
8149 | { | |
8150 | set_buffer_internal (XBUFFER (coding->dst_object)); | |
8151 | if (dst_bytes < coding->produced) | |
8152 | { | |
1af1a51a | 8153 | eassert (coding->produced > 0); |
b3bfad50 | 8154 | destination = xrealloc (destination, coding->produced); |
df7492f9 KH |
8155 | if (BEGV < GPT && GPT < BEGV + coding->produced_char) |
8156 | move_gap_both (BEGV, BEGV_BYTE); | |
72af86bd | 8157 | memcpy (destination, BEGV_ADDR, coding->produced); |
df7492f9 | 8158 | coding->destination = destination; |
d46c5b12 | 8159 | } |
ec6d2bb8 | 8160 | } |
b73bfc1c | 8161 | |
4776e638 KH |
8162 | if (saved_pt >= 0) |
8163 | { | |
8164 | /* This is the case of: | |
8165 | (BUFFERP (src_object) && EQ (src_object, dst_object)) | |
8166 | As we have moved PT while replacing the original buffer | |
8167 | contents, we must recover it now. */ | |
8168 | set_buffer_internal (XBUFFER (src_object)); | |
f4a3cc44 | 8169 | current_buffer->text->inhibit_shrinking = 0; |
4776e638 KH |
8170 | if (saved_pt < from) |
8171 | TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte); | |
8172 | else if (saved_pt < from + chars) | |
8173 | TEMP_SET_PT_BOTH (from, from_byte); | |
4b4deea2 | 8174 | else if (! NILP (BVAR (current_buffer, enable_multibyte_characters))) |
4776e638 KH |
8175 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars), |
8176 | saved_pt_byte + (coding->produced - bytes)); | |
8177 | else | |
8178 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes), | |
8179 | saved_pt_byte + (coding->produced - bytes)); | |
64cedb0c KH |
8180 | |
8181 | if (need_marker_adjustment) | |
8182 | { | |
8183 | struct Lisp_Marker *tail; | |
8184 | ||
8185 | for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next) | |
8186 | if (tail->need_adjustment) | |
8187 | { | |
8188 | tail->need_adjustment = 0; | |
8189 | if (tail->insertion_type) | |
8190 | { | |
8191 | tail->bytepos = from_byte; | |
8192 | tail->charpos = from; | |
8193 | } | |
8194 | else | |
8195 | { | |
8196 | tail->bytepos = from_byte + coding->produced; | |
8197 | tail->charpos | |
4b4deea2 | 8198 | = (NILP (BVAR (current_buffer, enable_multibyte_characters)) |
64cedb0c KH |
8199 | ? tail->bytepos : from + coding->produced_char); |
8200 | } | |
8201 | } | |
8202 | } | |
d46c5b12 | 8203 | } |
4776e638 | 8204 | |
b3bfad50 | 8205 | Vdeactivate_mark = old_deactivate_mark; |
065e3595 | 8206 | unbind_to (count, coding->dst_object); |
d46c5b12 KH |
8207 | } |
8208 | ||
d46c5b12 | 8209 | |
df7492f9 | 8210 | void |
cf84bb53 JB |
8211 | encode_coding_object (struct coding_system *coding, |
8212 | Lisp_Object src_object, | |
d311d28c PE |
8213 | ptrdiff_t from, ptrdiff_t from_byte, |
8214 | ptrdiff_t to, ptrdiff_t to_byte, | |
cf84bb53 | 8215 | Lisp_Object dst_object) |
d46c5b12 | 8216 | { |
d311d28c PE |
8217 | ptrdiff_t count = SPECPDL_INDEX (); |
8218 | ptrdiff_t chars = to - from; | |
8219 | ptrdiff_t bytes = to_byte - from_byte; | |
df7492f9 | 8220 | Lisp_Object attrs; |
f10fe38f PE |
8221 | ptrdiff_t saved_pt = -1, saved_pt_byte IF_LINT (= 0); |
8222 | bool need_marker_adjustment = 0; | |
8223 | bool kill_src_buffer = 0; | |
b3bfad50 | 8224 | Lisp_Object old_deactivate_mark; |
df7492f9 | 8225 | |
b3bfad50 | 8226 | old_deactivate_mark = Vdeactivate_mark; |
df7492f9 KH |
8227 | |
8228 | coding->src_object = src_object; | |
8229 | coding->src_chars = chars; | |
8230 | coding->src_bytes = bytes; | |
8231 | coding->src_multibyte = chars < bytes; | |
8232 | ||
8233 | attrs = CODING_ID_ATTRS (coding->id); | |
8234 | ||
64cedb0c KH |
8235 | if (EQ (src_object, dst_object)) |
8236 | { | |
8237 | struct Lisp_Marker *tail; | |
8238 | ||
8239 | for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next) | |
8240 | { | |
8241 | tail->need_adjustment | |
8242 | = tail->charpos == (tail->insertion_type ? from : to); | |
8243 | need_marker_adjustment |= tail->need_adjustment; | |
8244 | } | |
8245 | } | |
8246 | ||
df7492f9 | 8247 | if (! NILP (CODING_ATTR_PRE_WRITE (attrs))) |
6bac5b12 | 8248 | { |
24a73b0a | 8249 | coding->src_object = code_conversion_save (1, coding->src_multibyte); |
df7492f9 KH |
8250 | set_buffer_internal (XBUFFER (coding->src_object)); |
8251 | if (STRINGP (src_object)) | |
8252 | insert_from_string (src_object, from, from_byte, chars, bytes, 0); | |
8253 | else if (BUFFERP (src_object)) | |
8254 | insert_from_buffer (XBUFFER (src_object), from, chars, 0); | |
8255 | else | |
b68864e5 | 8256 | insert_1_both ((char *) coding->source + from, chars, bytes, 0, 0, 0); |
d46c5b12 | 8257 | |
df7492f9 KH |
8258 | if (EQ (src_object, dst_object)) |
8259 | { | |
8260 | set_buffer_internal (XBUFFER (src_object)); | |
4776e638 | 8261 | saved_pt = PT, saved_pt_byte = PT_BYTE; |
df7492f9 KH |
8262 | del_range_both (from, from_byte, to, to_byte, 1); |
8263 | set_buffer_internal (XBUFFER (coding->src_object)); | |
8264 | } | |
8265 | ||
d4850d67 | 8266 | { |
b3bfad50 | 8267 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; |
d4850d67 | 8268 | |
b3bfad50 KH |
8269 | GCPRO5 (coding->src_object, coding->dst_object, src_object, dst_object, |
8270 | old_deactivate_mark); | |
6cd7a139 DA |
8271 | safe_call2 (CODING_ATTR_PRE_WRITE (attrs), |
8272 | make_number (BEG), make_number (Z)); | |
b3bfad50 | 8273 | UNGCPRO; |
d4850d67 | 8274 | } |
c02d943b KH |
8275 | if (XBUFFER (coding->src_object) != current_buffer) |
8276 | kill_src_buffer = 1; | |
ac87bbef | 8277 | coding->src_object = Fcurrent_buffer (); |
df7492f9 KH |
8278 | if (BEG != GPT) |
8279 | move_gap_both (BEG, BEG_BYTE); | |
8280 | coding->src_chars = Z - BEG; | |
8281 | coding->src_bytes = Z_BYTE - BEG_BYTE; | |
8282 | coding->src_pos = BEG; | |
8283 | coding->src_pos_byte = BEG_BYTE; | |
8284 | coding->src_multibyte = Z < Z_BYTE; | |
8285 | } | |
8286 | else if (STRINGP (src_object)) | |
d46c5b12 | 8287 | { |
24a73b0a | 8288 | code_conversion_save (0, 0); |
df7492f9 KH |
8289 | coding->src_pos = from; |
8290 | coding->src_pos_byte = from_byte; | |
b73bfc1c | 8291 | } |
df7492f9 | 8292 | else if (BUFFERP (src_object)) |
b73bfc1c | 8293 | { |
24a73b0a | 8294 | code_conversion_save (0, 0); |
df7492f9 | 8295 | set_buffer_internal (XBUFFER (src_object)); |
df7492f9 | 8296 | if (EQ (src_object, dst_object)) |
d46c5b12 | 8297 | { |
4776e638 | 8298 | saved_pt = PT, saved_pt_byte = PT_BYTE; |
ff0dacd7 KH |
8299 | coding->src_object = del_range_1 (from, to, 1, 1); |
8300 | coding->src_pos = 0; | |
8301 | coding->src_pos_byte = 0; | |
d46c5b12 | 8302 | } |
df7492f9 | 8303 | else |
d46c5b12 | 8304 | { |
ff0dacd7 KH |
8305 | if (from < GPT && to >= GPT) |
8306 | move_gap_both (from, from_byte); | |
df7492f9 KH |
8307 | coding->src_pos = from; |
8308 | coding->src_pos_byte = from_byte; | |
d46c5b12 | 8309 | } |
d46c5b12 | 8310 | } |
4776e638 | 8311 | else |
24a73b0a | 8312 | code_conversion_save (0, 0); |
d46c5b12 | 8313 | |
df7492f9 | 8314 | if (BUFFERP (dst_object)) |
88993dfd | 8315 | { |
df7492f9 | 8316 | coding->dst_object = dst_object; |
28f67a95 KH |
8317 | if (EQ (src_object, dst_object)) |
8318 | { | |
8319 | coding->dst_pos = from; | |
8320 | coding->dst_pos_byte = from_byte; | |
8321 | } | |
8322 | else | |
8323 | { | |
319a3947 KH |
8324 | struct buffer *current = current_buffer; |
8325 | ||
8326 | set_buffer_temp (XBUFFER (dst_object)); | |
8327 | coding->dst_pos = PT; | |
8328 | coding->dst_pos_byte = PT_BYTE; | |
8329 | move_gap_both (coding->dst_pos, coding->dst_pos_byte); | |
8330 | set_buffer_temp (current); | |
28f67a95 | 8331 | } |
df7492f9 | 8332 | coding->dst_multibyte |
4b4deea2 | 8333 | = ! NILP (BVAR (XBUFFER (dst_object), enable_multibyte_characters)); |
88993dfd | 8334 | } |
df7492f9 | 8335 | else if (EQ (dst_object, Qt)) |
d46c5b12 | 8336 | { |
5d009b3a | 8337 | ptrdiff_t dst_bytes = max (1, coding->src_chars); |
df7492f9 | 8338 | coding->dst_object = Qnil; |
23f86fce | 8339 | coding->destination = xmalloc (dst_bytes); |
5d009b3a | 8340 | coding->dst_bytes = dst_bytes; |
df7492f9 | 8341 | coding->dst_multibyte = 0; |
d46c5b12 KH |
8342 | } |
8343 | else | |
8344 | { | |
df7492f9 KH |
8345 | coding->dst_object = Qnil; |
8346 | coding->dst_multibyte = 0; | |
d46c5b12 KH |
8347 | } |
8348 | ||
df7492f9 | 8349 | encode_coding (coding); |
d46c5b12 | 8350 | |
df7492f9 | 8351 | if (EQ (dst_object, Qt)) |
d46c5b12 | 8352 | { |
df7492f9 KH |
8353 | if (BUFFERP (coding->dst_object)) |
8354 | coding->dst_object = Fbuffer_string (); | |
8355 | else | |
d46c5b12 | 8356 | { |
df7492f9 KH |
8357 | coding->dst_object |
8358 | = make_unibyte_string ((char *) coding->destination, | |
8359 | coding->produced); | |
8360 | xfree (coding->destination); | |
d46c5b12 | 8361 | } |
4ed46869 | 8362 | } |
d46c5b12 | 8363 | |
4776e638 KH |
8364 | if (saved_pt >= 0) |
8365 | { | |
8366 | /* This is the case of: | |
8367 | (BUFFERP (src_object) && EQ (src_object, dst_object)) | |
8368 | As we have moved PT while replacing the original buffer | |
8369 | contents, we must recover it now. */ | |
8370 | set_buffer_internal (XBUFFER (src_object)); | |
8371 | if (saved_pt < from) | |
8372 | TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte); | |
8373 | else if (saved_pt < from + chars) | |
8374 | TEMP_SET_PT_BOTH (from, from_byte); | |
4b4deea2 | 8375 | else if (! NILP (BVAR (current_buffer, enable_multibyte_characters))) |
4776e638 KH |
8376 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars), |
8377 | saved_pt_byte + (coding->produced - bytes)); | |
d46c5b12 | 8378 | else |
4776e638 KH |
8379 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes), |
8380 | saved_pt_byte + (coding->produced - bytes)); | |
64cedb0c KH |
8381 | |
8382 | if (need_marker_adjustment) | |
8383 | { | |
8384 | struct Lisp_Marker *tail; | |
8385 | ||
8386 | for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next) | |
8387 | if (tail->need_adjustment) | |
8388 | { | |
8389 | tail->need_adjustment = 0; | |
8390 | if (tail->insertion_type) | |
8391 | { | |
8392 | tail->bytepos = from_byte; | |
8393 | tail->charpos = from; | |
8394 | } | |
8395 | else | |
8396 | { | |
8397 | tail->bytepos = from_byte + coding->produced; | |
8398 | tail->charpos | |
4b4deea2 | 8399 | = (NILP (BVAR (current_buffer, enable_multibyte_characters)) |
64cedb0c KH |
8400 | ? tail->bytepos : from + coding->produced_char); |
8401 | } | |
8402 | } | |
8403 | } | |
4776e638 KH |
8404 | } |
8405 | ||
c02d943b KH |
8406 | if (kill_src_buffer) |
8407 | Fkill_buffer (coding->src_object); | |
b3bfad50 KH |
8408 | |
8409 | Vdeactivate_mark = old_deactivate_mark; | |
df7492f9 | 8410 | unbind_to (count, Qnil); |
b73bfc1c KH |
8411 | } |
8412 | ||
df7492f9 | 8413 | |
b73bfc1c | 8414 | Lisp_Object |
971de7fb | 8415 | preferred_coding_system (void) |
b73bfc1c | 8416 | { |
df7492f9 | 8417 | int id = coding_categories[coding_priorities[0]].id; |
2391eaa4 | 8418 | |
df7492f9 | 8419 | return CODING_ID_NAME (id); |
4ed46869 KH |
8420 | } |
8421 | ||
7f590b0c | 8422 | #if defined (WINDOWSNT) || defined (CYGWIN) |
ba116008 DC |
8423 | |
8424 | Lisp_Object | |
8425 | from_unicode (Lisp_Object str) | |
8426 | { | |
8427 | CHECK_STRING (str); | |
8428 | if (!STRING_MULTIBYTE (str) && | |
8429 | SBYTES (str) & 1) | |
8430 | { | |
8431 | str = Fsubstring (str, make_number (0), make_number (-1)); | |
8432 | } | |
8433 | ||
8434 | return code_convert_string_norecord (str, Qutf_16le, 0); | |
8435 | } | |
8436 | ||
819e2da9 DC |
8437 | Lisp_Object |
8438 | from_unicode_buffer (const wchar_t* wstr) | |
8439 | { | |
8440 | return from_unicode ( | |
8441 | make_unibyte_string ( | |
8442 | (char*) wstr, | |
8443 | /* we get one of the two final 0 bytes for free. */ | |
8444 | 1 + sizeof (wchar_t) * wcslen (wstr))); | |
8445 | } | |
8446 | ||
ba116008 DC |
8447 | wchar_t * |
8448 | to_unicode (Lisp_Object str, Lisp_Object *buf) | |
8449 | { | |
8450 | *buf = code_convert_string_norecord (str, Qutf_16le, 1); | |
bc989a58 | 8451 | /* We need to make another copy (in addition to the one made by |
ba116008 DC |
8452 | code_convert_string_norecord) to ensure that the final string is |
8453 | _doubly_ zero terminated --- that is, that the string is | |
8454 | terminated by two zero bytes and one utf-16le null character. | |
8455 | Because strings are already terminated with a single zero byte, | |
8456 | we just add one additional zero. */ | |
8457 | str = make_uninit_string (SBYTES (*buf) + 1); | |
8458 | memcpy (SDATA (str), SDATA (*buf), SBYTES (*buf)); | |
8459 | SDATA (str) [SBYTES (*buf)] = '\0'; | |
8460 | *buf = str; | |
8461 | return WCSDATA (*buf); | |
8462 | } | |
7f590b0c DC |
8463 | |
8464 | #endif /* WINDOWSNT || CYGWIN */ | |
ba116008 | 8465 | |
4ed46869 KH |
8466 | \f |
8467 | #ifdef emacs | |
1397dc18 | 8468 | /*** 8. Emacs Lisp library functions ***/ |
4ed46869 | 8469 | |
a7ca3326 | 8470 | DEFUN ("coding-system-p", Fcoding_system_p, Scoding_system_p, 1, 1, 0, |
48b0f3ae | 8471 | doc: /* Return t if OBJECT is nil or a coding-system. |
df7492f9 | 8472 | See the documentation of `define-coding-system' for information |
48b0f3ae | 8473 | about coding-system objects. */) |
5842a27b | 8474 | (Lisp_Object object) |
4ed46869 | 8475 | { |
d4a1d553 JB |
8476 | if (NILP (object) |
8477 | || CODING_SYSTEM_ID (object) >= 0) | |
44e8490d | 8478 | return Qt; |
d4a1d553 JB |
8479 | if (! SYMBOLP (object) |
8480 | || NILP (Fget (object, Qcoding_system_define_form))) | |
44e8490d KH |
8481 | return Qnil; |
8482 | return Qt; | |
4ed46869 KH |
8483 | } |
8484 | ||
a7ca3326 | 8485 | DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system, |
9d991de8 | 8486 | Sread_non_nil_coding_system, 1, 1, 0, |
48b0f3ae | 8487 | doc: /* Read a coding system from the minibuffer, prompting with string PROMPT. */) |
5842a27b | 8488 | (Lisp_Object prompt) |
4ed46869 | 8489 | { |
e0e989f6 | 8490 | Lisp_Object val; |
9d991de8 RS |
8491 | do |
8492 | { | |
4608c386 KH |
8493 | val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil, |
8494 | Qt, Qnil, Qcoding_system_history, Qnil, Qnil); | |
9d991de8 | 8495 | } |
8f924df7 | 8496 | while (SCHARS (val) == 0); |
e0e989f6 | 8497 | return (Fintern (val, Qnil)); |
4ed46869 KH |
8498 | } |
8499 | ||
a7ca3326 | 8500 | DEFUN ("read-coding-system", Fread_coding_system, Sread_coding_system, 1, 2, 0, |
48b0f3ae | 8501 | doc: /* Read a coding system from the minibuffer, prompting with string PROMPT. |
c7183fb8 GM |
8502 | If the user enters null input, return second argument DEFAULT-CODING-SYSTEM. |
8503 | Ignores case when completing coding systems (all Emacs coding systems | |
8504 | are lower-case). */) | |
5842a27b | 8505 | (Lisp_Object prompt, Lisp_Object default_coding_system) |
4ed46869 | 8506 | { |
f44d27ce | 8507 | Lisp_Object val; |
d311d28c | 8508 | ptrdiff_t count = SPECPDL_INDEX (); |
c7183fb8 | 8509 | |
9b787f3e | 8510 | if (SYMBOLP (default_coding_system)) |
57d25e6f | 8511 | default_coding_system = SYMBOL_NAME (default_coding_system); |
c7183fb8 | 8512 | specbind (Qcompletion_ignore_case, Qt); |
4608c386 | 8513 | val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil, |
9b787f3e RS |
8514 | Qt, Qnil, Qcoding_system_history, |
8515 | default_coding_system, Qnil); | |
c7183fb8 | 8516 | unbind_to (count, Qnil); |
8f924df7 | 8517 | return (SCHARS (val) == 0 ? Qnil : Fintern (val, Qnil)); |
4ed46869 KH |
8518 | } |
8519 | ||
a7ca3326 | 8520 | DEFUN ("check-coding-system", Fcheck_coding_system, Scheck_coding_system, |
4ed46869 | 8521 | 1, 1, 0, |
48b0f3ae | 8522 | doc: /* Check validity of CODING-SYSTEM. |
9ffd559c KH |
8523 | If valid, return CODING-SYSTEM, else signal a `coding-system-error' error. |
8524 | It is valid if it is nil or a symbol defined as a coding system by the | |
8525 | function `define-coding-system'. */) | |
5842a27b | 8526 | (Lisp_Object coding_system) |
4ed46869 | 8527 | { |
44e8490d KH |
8528 | Lisp_Object define_form; |
8529 | ||
8530 | define_form = Fget (coding_system, Qcoding_system_define_form); | |
8531 | if (! NILP (define_form)) | |
8532 | { | |
8533 | Fput (coding_system, Qcoding_system_define_form, Qnil); | |
8534 | safe_eval (define_form); | |
8535 | } | |
4ed46869 KH |
8536 | if (!NILP (Fcoding_system_p (coding_system))) |
8537 | return coding_system; | |
fcad4ec4 | 8538 | xsignal1 (Qcoding_system_error, coding_system); |
4ed46869 | 8539 | } |
df7492f9 | 8540 | |
3a73fa5d | 8541 | \f |
89528eb3 | 8542 | /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If |
f10fe38f | 8543 | HIGHEST, return the coding system of the highest |
ad1746f5 | 8544 | priority among the detected coding systems. Otherwise return a |
89528eb3 | 8545 | list of detected coding systems sorted by their priorities. If |
f10fe38f | 8546 | MULTIBYTEP, it is assumed that the bytes are in correct |
89528eb3 KH |
8547 | multibyte form but contains only ASCII and eight-bit chars. |
8548 | Otherwise, the bytes are raw bytes. | |
8549 | ||
8550 | CODING-SYSTEM controls the detection as below: | |
8551 | ||
8552 | If it is nil, detect both text-format and eol-format. If the | |
8553 | text-format part of CODING-SYSTEM is already specified | |
8554 | (e.g. `iso-latin-1'), detect only eol-format. If the eol-format | |
8555 | part of CODING-SYSTEM is already specified (e.g. `undecided-unix'), | |
8556 | detect only text-format. */ | |
8557 | ||
d46c5b12 | 8558 | Lisp_Object |
cf84bb53 | 8559 | detect_coding_system (const unsigned char *src, |
d311d28c | 8560 | ptrdiff_t src_chars, ptrdiff_t src_bytes, |
f10fe38f | 8561 | bool highest, bool multibytep, |
cf84bb53 | 8562 | Lisp_Object coding_system) |
4ed46869 | 8563 | { |
8f924df7 | 8564 | const unsigned char *src_end = src + src_bytes; |
df7492f9 | 8565 | Lisp_Object attrs, eol_type; |
4533845d | 8566 | Lisp_Object val = Qnil; |
df7492f9 | 8567 | struct coding_system coding; |
d3411f89 | 8568 | ptrdiff_t id; |
ff0dacd7 | 8569 | struct coding_detection_info detect_info; |
24a73b0a | 8570 | enum coding_category base_category; |
f10fe38f | 8571 | bool null_byte_found = 0, eight_bit_found = 0; |
b73bfc1c | 8572 | |
df7492f9 KH |
8573 | if (NILP (coding_system)) |
8574 | coding_system = Qundecided; | |
8575 | setup_coding_system (coding_system, &coding); | |
8576 | attrs = CODING_ID_ATTRS (coding.id); | |
8577 | eol_type = CODING_ID_EOL_TYPE (coding.id); | |
89528eb3 | 8578 | coding_system = CODING_ATTR_BASE_NAME (attrs); |
4ed46869 | 8579 | |
df7492f9 | 8580 | coding.source = src; |
24a73b0a | 8581 | coding.src_chars = src_chars; |
df7492f9 KH |
8582 | coding.src_bytes = src_bytes; |
8583 | coding.src_multibyte = multibytep; | |
8584 | coding.consumed = 0; | |
89528eb3 | 8585 | coding.mode |= CODING_MODE_LAST_BLOCK; |
c0e16b14 | 8586 | coding.head_ascii = 0; |
d46c5b12 | 8587 | |
ff0dacd7 | 8588 | detect_info.checked = detect_info.found = detect_info.rejected = 0; |
d46c5b12 | 8589 | |
89528eb3 | 8590 | /* At first, detect text-format if necessary. */ |
24a73b0a KH |
8591 | base_category = XINT (CODING_ATTR_CATEGORY (attrs)); |
8592 | if (base_category == coding_category_undecided) | |
4ed46869 | 8593 | { |
c4a63b12 PE |
8594 | enum coding_category category IF_LINT (= 0); |
8595 | struct coding_system *this IF_LINT (= NULL); | |
ff0dacd7 | 8596 | int c, i; |
9c90cc06 PE |
8597 | bool inhibit_nbd = inhibit_flag (coding.spec.undecided.inhibit_nbd, |
8598 | inhibit_null_byte_detection); | |
8599 | bool inhibit_ied = inhibit_flag (coding.spec.undecided.inhibit_ied, | |
8600 | inhibit_iso_escape_detection); | |
8601 | bool prefer_utf_8 = coding.spec.undecided.prefer_utf_8; | |
88993dfd | 8602 | |
24a73b0a | 8603 | /* Skip all ASCII bytes except for a few ISO2022 controls. */ |
2f3cbb32 | 8604 | for (; src < src_end; src++) |
4ed46869 | 8605 | { |
df7492f9 | 8606 | c = *src; |
6cb21a4f | 8607 | if (c & 0x80) |
6cb21a4f | 8608 | { |
2f3cbb32 | 8609 | eight_bit_found = 1; |
2f3cbb32 KH |
8610 | if (null_byte_found) |
8611 | break; | |
8612 | } | |
c0e16b14 | 8613 | else if (c < 0x20) |
2f3cbb32 KH |
8614 | { |
8615 | if ((c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) | |
c3a7b3f4 | 8616 | && ! inhibit_ied |
2f3cbb32 | 8617 | && ! detect_info.checked) |
6cb21a4f | 8618 | { |
2f3cbb32 KH |
8619 | if (detect_coding_iso_2022 (&coding, &detect_info)) |
8620 | { | |
8621 | /* We have scanned the whole data. */ | |
8622 | if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE)) | |
c0e16b14 KH |
8623 | { |
8624 | /* We didn't find an 8-bit code. We may | |
8625 | have found a null-byte, but it's very | |
8626 | rare that a binary file confirm to | |
8627 | ISO-2022. */ | |
8628 | src = src_end; | |
8629 | coding.head_ascii = src - coding.source; | |
8630 | } | |
8631 | detect_info.rejected |= ~CATEGORY_MASK_ISO_ESCAPE; | |
2f3cbb32 KH |
8632 | break; |
8633 | } | |
8634 | } | |
c3a7b3f4 | 8635 | else if (! c && !inhibit_nbd) |
2f3cbb32 KH |
8636 | { |
8637 | null_byte_found = 1; | |
8638 | if (eight_bit_found) | |
8639 | break; | |
6cb21a4f | 8640 | } |
c006c0c8 KH |
8641 | if (! eight_bit_found) |
8642 | coding.head_ascii++; | |
6cb21a4f | 8643 | } |
c006c0c8 | 8644 | else if (! eight_bit_found) |
c0e16b14 | 8645 | coding.head_ascii++; |
4ed46869 | 8646 | } |
88993dfd | 8647 | |
2f3cbb32 KH |
8648 | if (null_byte_found || eight_bit_found |
8649 | || coding.head_ascii < coding.src_bytes | |
6cb21a4f KH |
8650 | || detect_info.found) |
8651 | { | |
2f3cbb32 | 8652 | if (coding.head_ascii == coding.src_bytes) |
6cb21a4f KH |
8653 | /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */ |
8654 | for (i = 0; i < coding_category_raw_text; i++) | |
ff0dacd7 | 8655 | { |
6cb21a4f | 8656 | category = coding_priorities[i]; |
c7266f4a | 8657 | this = coding_categories + category; |
6cb21a4f | 8658 | if (detect_info.found & (1 << category)) |
ff0dacd7 KH |
8659 | break; |
8660 | } | |
6cb21a4f | 8661 | else |
2f3cbb32 KH |
8662 | { |
8663 | if (null_byte_found) | |
8664 | { | |
8665 | detect_info.checked |= ~CATEGORY_MASK_UTF_16; | |
8666 | detect_info.rejected |= ~CATEGORY_MASK_UTF_16; | |
8667 | } | |
c3a7b3f4 KH |
8668 | else if (prefer_utf_8 |
8669 | && detect_coding_utf_8 (&coding, &detect_info)) | |
8670 | { | |
8671 | detect_info.checked |= ~CATEGORY_MASK_UTF_8; | |
8672 | detect_info.rejected |= ~CATEGORY_MASK_UTF_8; | |
8673 | } | |
2f3cbb32 KH |
8674 | for (i = 0; i < coding_category_raw_text; i++) |
8675 | { | |
8676 | category = coding_priorities[i]; | |
8677 | this = coding_categories + category; | |
6cb21a4f | 8678 | |
2f3cbb32 KH |
8679 | if (this->id < 0) |
8680 | { | |
8681 | /* No coding system of this category is defined. */ | |
8682 | detect_info.rejected |= (1 << category); | |
8683 | } | |
8684 | else if (category >= coding_category_raw_text) | |
8685 | continue; | |
8686 | else if (detect_info.checked & (1 << category)) | |
8687 | { | |
8688 | if (highest | |
8689 | && (detect_info.found & (1 << category))) | |
6cb21a4f | 8690 | break; |
2f3cbb32 KH |
8691 | } |
8692 | else if ((*(this->detector)) (&coding, &detect_info) | |
8693 | && highest | |
8694 | && (detect_info.found & (1 << category))) | |
8695 | { | |
8696 | if (category == coding_category_utf_16_auto) | |
8697 | { | |
8698 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
8699 | category = coding_category_utf_16_le; | |
8700 | else | |
8701 | category = coding_category_utf_16_be; | |
8702 | } | |
8703 | break; | |
8704 | } | |
8705 | } | |
8706 | } | |
6cb21a4f | 8707 | } |
ec6d2bb8 | 8708 | |
4cddb209 KH |
8709 | if ((detect_info.rejected & CATEGORY_MASK_ANY) == CATEGORY_MASK_ANY |
8710 | || null_byte_found) | |
ec6d2bb8 | 8711 | { |
ff0dacd7 | 8712 | detect_info.found = CATEGORY_MASK_RAW_TEXT; |
4cddb209 | 8713 | id = CODING_SYSTEM_ID (Qno_conversion); |
6c6f1994 | 8714 | val = list1 (make_number (id)); |
89528eb3 | 8715 | } |
ff0dacd7 | 8716 | else if (! detect_info.rejected && ! detect_info.found) |
89528eb3 | 8717 | { |
ff0dacd7 | 8718 | detect_info.found = CATEGORY_MASK_ANY; |
89528eb3 | 8719 | id = coding_categories[coding_category_undecided].id; |
6c6f1994 | 8720 | val = list1 (make_number (id)); |
89528eb3 KH |
8721 | } |
8722 | else if (highest) | |
8723 | { | |
ff0dacd7 | 8724 | if (detect_info.found) |
ec6d2bb8 | 8725 | { |
ff0dacd7 | 8726 | detect_info.found = 1 << category; |
6c6f1994 | 8727 | val = list1 (make_number (this->id)); |
ff0dacd7 KH |
8728 | } |
8729 | else | |
8730 | for (i = 0; i < coding_category_raw_text; i++) | |
8731 | if (! (detect_info.rejected & (1 << coding_priorities[i]))) | |
8732 | { | |
8733 | detect_info.found = 1 << coding_priorities[i]; | |
8734 | id = coding_categories[coding_priorities[i]].id; | |
6c6f1994 | 8735 | val = list1 (make_number (id)); |
ff0dacd7 KH |
8736 | break; |
8737 | } | |
8738 | } | |
89528eb3 KH |
8739 | else |
8740 | { | |
ff0dacd7 KH |
8741 | int mask = detect_info.rejected | detect_info.found; |
8742 | int found = 0; | |
ec6d2bb8 | 8743 | |
89528eb3 | 8744 | for (i = coding_category_raw_text - 1; i >= 0; i--) |
ff0dacd7 KH |
8745 | { |
8746 | category = coding_priorities[i]; | |
8747 | if (! (mask & (1 << category))) | |
ec6d2bb8 | 8748 | { |
ff0dacd7 KH |
8749 | found |= 1 << category; |
8750 | id = coding_categories[category].id; | |
c7266f4a | 8751 | if (id >= 0) |
6c6f1994 | 8752 | val = list1 (make_number (id)); |
ff0dacd7 KH |
8753 | } |
8754 | } | |
8755 | for (i = coding_category_raw_text - 1; i >= 0; i--) | |
8756 | { | |
8757 | category = coding_priorities[i]; | |
8758 | if (detect_info.found & (1 << category)) | |
8759 | { | |
8760 | id = coding_categories[category].id; | |
8761 | val = Fcons (make_number (id), val); | |
ec6d2bb8 | 8762 | } |
ec6d2bb8 | 8763 | } |
ff0dacd7 | 8764 | detect_info.found |= found; |
ec6d2bb8 | 8765 | } |
ec6d2bb8 | 8766 | } |
a470d443 KH |
8767 | else if (base_category == coding_category_utf_8_auto) |
8768 | { | |
8769 | if (detect_coding_utf_8 (&coding, &detect_info)) | |
8770 | { | |
8771 | struct coding_system *this; | |
8772 | ||
8773 | if (detect_info.found & CATEGORY_MASK_UTF_8_SIG) | |
8774 | this = coding_categories + coding_category_utf_8_sig; | |
8775 | else | |
8776 | this = coding_categories + coding_category_utf_8_nosig; | |
6c6f1994 | 8777 | val = list1 (make_number (this->id)); |
a470d443 KH |
8778 | } |
8779 | } | |
24a73b0a KH |
8780 | else if (base_category == coding_category_utf_16_auto) |
8781 | { | |
8782 | if (detect_coding_utf_16 (&coding, &detect_info)) | |
8783 | { | |
24a73b0a KH |
8784 | struct coding_system *this; |
8785 | ||
8786 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
8787 | this = coding_categories + coding_category_utf_16_le; | |
8788 | else if (detect_info.found & CATEGORY_MASK_UTF_16_BE) | |
8789 | this = coding_categories + coding_category_utf_16_be; | |
8790 | else if (detect_info.rejected & CATEGORY_MASK_UTF_16_LE_NOSIG) | |
8791 | this = coding_categories + coding_category_utf_16_be_nosig; | |
8792 | else | |
8793 | this = coding_categories + coding_category_utf_16_le_nosig; | |
6c6f1994 | 8794 | val = list1 (make_number (this->id)); |
24a73b0a KH |
8795 | } |
8796 | } | |
df7492f9 KH |
8797 | else |
8798 | { | |
ff0dacd7 | 8799 | detect_info.found = 1 << XINT (CODING_ATTR_CATEGORY (attrs)); |
6c6f1994 | 8800 | val = list1 (make_number (coding.id)); |
4ed46869 | 8801 | } |
df7492f9 | 8802 | |
89528eb3 | 8803 | /* Then, detect eol-format if necessary. */ |
df7492f9 | 8804 | { |
4533845d | 8805 | int normal_eol = -1, utf_16_be_eol = -1, utf_16_le_eol = -1; |
df7492f9 KH |
8806 | Lisp_Object tail; |
8807 | ||
89528eb3 KH |
8808 | if (VECTORP (eol_type)) |
8809 | { | |
ff0dacd7 | 8810 | if (detect_info.found & ~CATEGORY_MASK_UTF_16) |
2f3cbb32 KH |
8811 | { |
8812 | if (null_byte_found) | |
8813 | normal_eol = EOL_SEEN_LF; | |
8814 | else | |
8815 | normal_eol = detect_eol (coding.source, src_bytes, | |
8816 | coding_category_raw_text); | |
8817 | } | |
ff0dacd7 KH |
8818 | if (detect_info.found & (CATEGORY_MASK_UTF_16_BE |
8819 | | CATEGORY_MASK_UTF_16_BE_NOSIG)) | |
89528eb3 KH |
8820 | utf_16_be_eol = detect_eol (coding.source, src_bytes, |
8821 | coding_category_utf_16_be); | |
ff0dacd7 KH |
8822 | if (detect_info.found & (CATEGORY_MASK_UTF_16_LE |
8823 | | CATEGORY_MASK_UTF_16_LE_NOSIG)) | |
89528eb3 KH |
8824 | utf_16_le_eol = detect_eol (coding.source, src_bytes, |
8825 | coding_category_utf_16_le); | |
8826 | } | |
8827 | else | |
8828 | { | |
8829 | if (EQ (eol_type, Qunix)) | |
8830 | normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_LF; | |
8831 | else if (EQ (eol_type, Qdos)) | |
8832 | normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CRLF; | |
8833 | else | |
8834 | normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CR; | |
8835 | } | |
8836 | ||
df7492f9 KH |
8837 | for (tail = val; CONSP (tail); tail = XCDR (tail)) |
8838 | { | |
89528eb3 | 8839 | enum coding_category category; |
df7492f9 | 8840 | int this_eol; |
89528eb3 KH |
8841 | |
8842 | id = XINT (XCAR (tail)); | |
8843 | attrs = CODING_ID_ATTRS (id); | |
8844 | category = XINT (CODING_ATTR_CATEGORY (attrs)); | |
8845 | eol_type = CODING_ID_EOL_TYPE (id); | |
df7492f9 KH |
8846 | if (VECTORP (eol_type)) |
8847 | { | |
89528eb3 KH |
8848 | if (category == coding_category_utf_16_be |
8849 | || category == coding_category_utf_16_be_nosig) | |
8850 | this_eol = utf_16_be_eol; | |
8851 | else if (category == coding_category_utf_16_le | |
8852 | || category == coding_category_utf_16_le_nosig) | |
8853 | this_eol = utf_16_le_eol; | |
df7492f9 | 8854 | else |
89528eb3 KH |
8855 | this_eol = normal_eol; |
8856 | ||
df7492f9 KH |
8857 | if (this_eol == EOL_SEEN_LF) |
8858 | XSETCAR (tail, AREF (eol_type, 0)); | |
8859 | else if (this_eol == EOL_SEEN_CRLF) | |
8860 | XSETCAR (tail, AREF (eol_type, 1)); | |
8861 | else if (this_eol == EOL_SEEN_CR) | |
8862 | XSETCAR (tail, AREF (eol_type, 2)); | |
89528eb3 KH |
8863 | else |
8864 | XSETCAR (tail, CODING_ID_NAME (id)); | |
df7492f9 | 8865 | } |
89528eb3 KH |
8866 | else |
8867 | XSETCAR (tail, CODING_ID_NAME (id)); | |
df7492f9 KH |
8868 | } |
8869 | } | |
ec6d2bb8 | 8870 | |
4533845d | 8871 | return (highest ? (CONSP (val) ? XCAR (val) : Qnil) : val); |
ec6d2bb8 KH |
8872 | } |
8873 | ||
ec6d2bb8 | 8874 | |
d46c5b12 KH |
8875 | DEFUN ("detect-coding-region", Fdetect_coding_region, Sdetect_coding_region, |
8876 | 2, 3, 0, | |
48b0f3ae PJ |
8877 | doc: /* Detect coding system of the text in the region between START and END. |
8878 | Return a list of possible coding systems ordered by priority. | |
b811c52b KH |
8879 | The coding systems to try and their priorities follows what |
8880 | the function `coding-system-priority-list' (which see) returns. | |
ec6d2bb8 | 8881 | |
12e0131a | 8882 | If only ASCII characters are found (except for such ISO-2022 control |
d4a1d553 JB |
8883 | characters as ESC), it returns a list of single element `undecided' |
8884 | or its subsidiary coding system according to a detected end-of-line | |
8885 | format. | |
ec6d2bb8 | 8886 | |
48b0f3ae PJ |
8887 | If optional argument HIGHEST is non-nil, return the coding system of |
8888 | highest priority. */) | |
5842a27b | 8889 | (Lisp_Object start, Lisp_Object end, Lisp_Object highest) |
d46c5b12 | 8890 | { |
d311d28c PE |
8891 | ptrdiff_t from, to; |
8892 | ptrdiff_t from_byte, to_byte; | |
ec6d2bb8 | 8893 | |
d46c5b12 KH |
8894 | validate_region (&start, &end); |
8895 | from = XINT (start), to = XINT (end); | |
8896 | from_byte = CHAR_TO_BYTE (from); | |
8897 | to_byte = CHAR_TO_BYTE (to); | |
ec6d2bb8 | 8898 | |
d46c5b12 KH |
8899 | if (from < GPT && to >= GPT) |
8900 | move_gap_both (to, to_byte); | |
c210f766 | 8901 | |
d46c5b12 | 8902 | return detect_coding_system (BYTE_POS_ADDR (from_byte), |
24a73b0a | 8903 | to - from, to_byte - from_byte, |
0a28aafb | 8904 | !NILP (highest), |
4b4deea2 | 8905 | !NILP (BVAR (current_buffer |
5d8ea120 | 8906 | , enable_multibyte_characters)), |
df7492f9 | 8907 | Qnil); |
ec6d2bb8 KH |
8908 | } |
8909 | ||
d46c5b12 KH |
8910 | DEFUN ("detect-coding-string", Fdetect_coding_string, Sdetect_coding_string, |
8911 | 1, 2, 0, | |
48b0f3ae PJ |
8912 | doc: /* Detect coding system of the text in STRING. |
8913 | Return a list of possible coding systems ordered by priority. | |
67ceab9d KH |
8914 | The coding systems to try and their priorities follows what |
8915 | the function `coding-system-priority-list' (which see) returns. | |
fb88bf2d | 8916 | |
12e0131a | 8917 | If only ASCII characters are found (except for such ISO-2022 control |
d4a1d553 JB |
8918 | characters as ESC), it returns a list of single element `undecided' |
8919 | or its subsidiary coding system according to a detected end-of-line | |
8920 | format. | |
d46c5b12 | 8921 | |
48b0f3ae PJ |
8922 | If optional argument HIGHEST is non-nil, return the coding system of |
8923 | highest priority. */) | |
5842a27b | 8924 | (Lisp_Object string, Lisp_Object highest) |
d46c5b12 | 8925 | { |
b7826503 | 8926 | CHECK_STRING (string); |
b73bfc1c | 8927 | |
24a73b0a KH |
8928 | return detect_coding_system (SDATA (string), |
8929 | SCHARS (string), SBYTES (string), | |
8f924df7 | 8930 | !NILP (highest), STRING_MULTIBYTE (string), |
df7492f9 | 8931 | Qnil); |
4ed46869 | 8932 | } |
4ed46869 | 8933 | |
b73bfc1c | 8934 | |
b0ab8123 | 8935 | static bool |
971de7fb | 8936 | char_encodable_p (int c, Lisp_Object attrs) |
05e6f5dc | 8937 | { |
df7492f9 | 8938 | Lisp_Object tail; |
df7492f9 | 8939 | struct charset *charset; |
7d64c6ad | 8940 | Lisp_Object translation_table; |
d46c5b12 | 8941 | |
7d64c6ad | 8942 | translation_table = CODING_ATTR_TRANS_TBL (attrs); |
a6f87d34 | 8943 | if (! NILP (translation_table)) |
7d64c6ad | 8944 | c = translate_char (translation_table, c); |
df7492f9 KH |
8945 | for (tail = CODING_ATTR_CHARSET_LIST (attrs); |
8946 | CONSP (tail); tail = XCDR (tail)) | |
e133c8fa | 8947 | { |
df7492f9 KH |
8948 | charset = CHARSET_FROM_ID (XINT (XCAR (tail))); |
8949 | if (CHAR_CHARSET_P (c, charset)) | |
8950 | break; | |
e133c8fa | 8951 | } |
df7492f9 | 8952 | return (! NILP (tail)); |
05e6f5dc | 8953 | } |
83fa074f | 8954 | |
fb88bf2d | 8955 | |
df7492f9 KH |
8956 | /* Return a list of coding systems that safely encode the text between |
8957 | START and END. If EXCLUDE is non-nil, it is a list of coding | |
8958 | systems not to check. The returned list doesn't contain any such | |
48468dac | 8959 | coding systems. In any case, if the text contains only ASCII or is |
df7492f9 | 8960 | unibyte, return t. */ |
e077cc80 | 8961 | |
df7492f9 KH |
8962 | DEFUN ("find-coding-systems-region-internal", |
8963 | Ffind_coding_systems_region_internal, | |
8964 | Sfind_coding_systems_region_internal, 2, 3, 0, | |
8965 | doc: /* Internal use only. */) | |
5842a27b | 8966 | (Lisp_Object start, Lisp_Object end, Lisp_Object exclude) |
df7492f9 KH |
8967 | { |
8968 | Lisp_Object coding_attrs_list, safe_codings; | |
d311d28c | 8969 | ptrdiff_t start_byte, end_byte; |
7c78e542 | 8970 | const unsigned char *p, *pbeg, *pend; |
df7492f9 | 8971 | int c; |
0e727afa | 8972 | Lisp_Object tail, elt, work_table; |
d46c5b12 | 8973 | |
df7492f9 KH |
8974 | if (STRINGP (start)) |
8975 | { | |
8976 | if (!STRING_MULTIBYTE (start) | |
8f924df7 | 8977 | || SCHARS (start) == SBYTES (start)) |
df7492f9 KH |
8978 | return Qt; |
8979 | start_byte = 0; | |
8f924df7 | 8980 | end_byte = SBYTES (start); |
df7492f9 KH |
8981 | } |
8982 | else | |
d46c5b12 | 8983 | { |
df7492f9 KH |
8984 | CHECK_NUMBER_COERCE_MARKER (start); |
8985 | CHECK_NUMBER_COERCE_MARKER (end); | |
8986 | if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end)) | |
8987 | args_out_of_range (start, end); | |
4b4deea2 | 8988 | if (NILP (BVAR (current_buffer, enable_multibyte_characters))) |
df7492f9 KH |
8989 | return Qt; |
8990 | start_byte = CHAR_TO_BYTE (XINT (start)); | |
8991 | end_byte = CHAR_TO_BYTE (XINT (end)); | |
8992 | if (XINT (end) - XINT (start) == end_byte - start_byte) | |
8993 | return Qt; | |
d46c5b12 | 8994 | |
e1c23804 | 8995 | if (XINT (start) < GPT && XINT (end) > GPT) |
d46c5b12 | 8996 | { |
e1c23804 DL |
8997 | if ((GPT - XINT (start)) < (XINT (end) - GPT)) |
8998 | move_gap_both (XINT (start), start_byte); | |
df7492f9 | 8999 | else |
e1c23804 | 9000 | move_gap_both (XINT (end), end_byte); |
d46c5b12 KH |
9001 | } |
9002 | } | |
9003 | ||
df7492f9 KH |
9004 | coding_attrs_list = Qnil; |
9005 | for (tail = Vcoding_system_list; CONSP (tail); tail = XCDR (tail)) | |
9006 | if (NILP (exclude) | |
9007 | || NILP (Fmemq (XCAR (tail), exclude))) | |
9008 | { | |
9009 | Lisp_Object attrs; | |
d46c5b12 | 9010 | |
df7492f9 | 9011 | attrs = AREF (CODING_SYSTEM_SPEC (XCAR (tail)), 0); |
c3a7b3f4 | 9012 | if (EQ (XCAR (tail), CODING_ATTR_BASE_NAME (attrs))) |
7d64c6ad KH |
9013 | { |
9014 | ASET (attrs, coding_attr_trans_tbl, | |
2170c8f0 | 9015 | get_translation_table (attrs, 1, NULL)); |
7d64c6ad KH |
9016 | coding_attrs_list = Fcons (attrs, coding_attrs_list); |
9017 | } | |
df7492f9 | 9018 | } |
d46c5b12 | 9019 | |
df7492f9 | 9020 | if (STRINGP (start)) |
8f924df7 | 9021 | p = pbeg = SDATA (start); |
df7492f9 KH |
9022 | else |
9023 | p = pbeg = BYTE_POS_ADDR (start_byte); | |
9024 | pend = p + (end_byte - start_byte); | |
b843d1ae | 9025 | |
df7492f9 KH |
9026 | while (p < pend && ASCII_BYTE_P (*p)) p++; |
9027 | while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--; | |
d46c5b12 | 9028 | |
0e727afa | 9029 | work_table = Fmake_char_table (Qnil, Qnil); |
05e6f5dc | 9030 | while (p < pend) |
72d1a715 | 9031 | { |
df7492f9 KH |
9032 | if (ASCII_BYTE_P (*p)) |
9033 | p++; | |
72d1a715 RS |
9034 | else |
9035 | { | |
df7492f9 | 9036 | c = STRING_CHAR_ADVANCE (p); |
0e727afa YM |
9037 | if (!NILP (char_table_ref (work_table, c))) |
9038 | /* This character was already checked. Ignore it. */ | |
9039 | continue; | |
12410ef1 | 9040 | |
df7492f9 KH |
9041 | charset_map_loaded = 0; |
9042 | for (tail = coding_attrs_list; CONSP (tail);) | |
9043 | { | |
9044 | elt = XCAR (tail); | |
9045 | if (NILP (elt)) | |
9046 | tail = XCDR (tail); | |
9047 | else if (char_encodable_p (c, elt)) | |
9048 | tail = XCDR (tail); | |
9049 | else if (CONSP (XCDR (tail))) | |
9050 | { | |
9051 | XSETCAR (tail, XCAR (XCDR (tail))); | |
9052 | XSETCDR (tail, XCDR (XCDR (tail))); | |
9053 | } | |
9054 | else | |
9055 | { | |
9056 | XSETCAR (tail, Qnil); | |
9057 | tail = XCDR (tail); | |
9058 | } | |
9059 | } | |
9060 | if (charset_map_loaded) | |
9061 | { | |
d311d28c | 9062 | ptrdiff_t p_offset = p - pbeg, pend_offset = pend - pbeg; |
05e6f5dc | 9063 | |
df7492f9 | 9064 | if (STRINGP (start)) |
8f924df7 | 9065 | pbeg = SDATA (start); |
df7492f9 KH |
9066 | else |
9067 | pbeg = BYTE_POS_ADDR (start_byte); | |
9068 | p = pbeg + p_offset; | |
9069 | pend = pbeg + pend_offset; | |
9070 | } | |
0e727afa | 9071 | char_table_set (work_table, c, Qt); |
df7492f9 | 9072 | } |
ec6d2bb8 | 9073 | } |
fb88bf2d | 9074 | |
988b3759 | 9075 | safe_codings = list2 (Qraw_text, Qno_conversion); |
df7492f9 KH |
9076 | for (tail = coding_attrs_list; CONSP (tail); tail = XCDR (tail)) |
9077 | if (! NILP (XCAR (tail))) | |
9078 | safe_codings = Fcons (CODING_ATTR_BASE_NAME (XCAR (tail)), safe_codings); | |
ec6d2bb8 | 9079 | |
05e6f5dc KH |
9080 | return safe_codings; |
9081 | } | |
4956c225 | 9082 | |
d46c5b12 | 9083 | |
8f924df7 KH |
9084 | DEFUN ("unencodable-char-position", Funencodable_char_position, |
9085 | Sunencodable_char_position, 3, 5, 0, | |
9086 | doc: /* | |
9087 | Return position of first un-encodable character in a region. | |
d4a1d553 | 9088 | START and END specify the region and CODING-SYSTEM specifies the |
8f924df7 | 9089 | encoding to check. Return nil if CODING-SYSTEM does encode the region. |
d46c5b12 | 9090 | |
8f924df7 KH |
9091 | If optional 4th argument COUNT is non-nil, it specifies at most how |
9092 | many un-encodable characters to search. In this case, the value is a | |
9093 | list of positions. | |
d46c5b12 | 9094 | |
8f924df7 KH |
9095 | If optional 5th argument STRING is non-nil, it is a string to search |
9096 | for un-encodable characters. In that case, START and END are indexes | |
9097 | to the string. */) | |
5842a27b | 9098 | (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object count, Lisp_Object string) |
8f924df7 | 9099 | { |
d311d28c | 9100 | EMACS_INT n; |
8f924df7 | 9101 | struct coding_system coding; |
7d64c6ad | 9102 | Lisp_Object attrs, charset_list, translation_table; |
8f924df7 | 9103 | Lisp_Object positions; |
d311d28c | 9104 | ptrdiff_t from, to; |
8f924df7 | 9105 | const unsigned char *p, *stop, *pend; |
f10fe38f | 9106 | bool ascii_compatible; |
fb88bf2d | 9107 | |
8f924df7 KH |
9108 | setup_coding_system (Fcheck_coding_system (coding_system), &coding); |
9109 | attrs = CODING_ID_ATTRS (coding.id); | |
9110 | if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text)) | |
9111 | return Qnil; | |
9112 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); | |
9113 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
2170c8f0 | 9114 | translation_table = get_translation_table (attrs, 1, NULL); |
fb88bf2d | 9115 | |
8f924df7 KH |
9116 | if (NILP (string)) |
9117 | { | |
9118 | validate_region (&start, &end); | |
9119 | from = XINT (start); | |
9120 | to = XINT (end); | |
4b4deea2 | 9121 | if (NILP (BVAR (current_buffer, enable_multibyte_characters)) |
8f924df7 KH |
9122 | || (ascii_compatible |
9123 | && (to - from) == (CHAR_TO_BYTE (to) - (CHAR_TO_BYTE (from))))) | |
9124 | return Qnil; | |
9125 | p = CHAR_POS_ADDR (from); | |
9126 | pend = CHAR_POS_ADDR (to); | |
9127 | if (from < GPT && to >= GPT) | |
9128 | stop = GPT_ADDR; | |
9129 | else | |
9130 | stop = pend; | |
9131 | } | |
9132 | else | |
9133 | { | |
9134 | CHECK_STRING (string); | |
9135 | CHECK_NATNUM (start); | |
9136 | CHECK_NATNUM (end); | |
d311d28c PE |
9137 | if (! (XINT (start) <= XINT (end) && XINT (end) <= SCHARS (string))) |
9138 | args_out_of_range_3 (string, start, end); | |
8f924df7 KH |
9139 | from = XINT (start); |
9140 | to = XINT (end); | |
8f924df7 KH |
9141 | if (! STRING_MULTIBYTE (string)) |
9142 | return Qnil; | |
9143 | p = SDATA (string) + string_char_to_byte (string, from); | |
9144 | stop = pend = SDATA (string) + string_char_to_byte (string, to); | |
9145 | if (ascii_compatible && (to - from) == (pend - p)) | |
9146 | return Qnil; | |
9147 | } | |
f2558efd | 9148 | |
8f924df7 KH |
9149 | if (NILP (count)) |
9150 | n = 1; | |
9151 | else | |
b73bfc1c | 9152 | { |
8f924df7 KH |
9153 | CHECK_NATNUM (count); |
9154 | n = XINT (count); | |
b73bfc1c KH |
9155 | } |
9156 | ||
8f924df7 | 9157 | positions = Qnil; |
3633e3aa | 9158 | charset_map_loaded = 0; |
8f924df7 | 9159 | while (1) |
d46c5b12 | 9160 | { |
8f924df7 | 9161 | int c; |
ec6d2bb8 | 9162 | |
8f924df7 KH |
9163 | if (ascii_compatible) |
9164 | while (p < stop && ASCII_BYTE_P (*p)) | |
9165 | p++, from++; | |
9166 | if (p >= stop) | |
0e79d667 | 9167 | { |
8f924df7 KH |
9168 | if (p >= pend) |
9169 | break; | |
9170 | stop = pend; | |
9171 | p = GAP_END_ADDR; | |
0e79d667 | 9172 | } |
ec6d2bb8 | 9173 | |
8f924df7 KH |
9174 | c = STRING_CHAR_ADVANCE (p); |
9175 | if (! (ASCII_CHAR_P (c) && ascii_compatible) | |
7d64c6ad KH |
9176 | && ! char_charset (translate_char (translation_table, c), |
9177 | charset_list, NULL)) | |
ec6d2bb8 | 9178 | { |
8f924df7 KH |
9179 | positions = Fcons (make_number (from), positions); |
9180 | n--; | |
9181 | if (n == 0) | |
9182 | break; | |
ec6d2bb8 KH |
9183 | } |
9184 | ||
8f924df7 | 9185 | from++; |
3633e3aa KH |
9186 | if (charset_map_loaded && NILP (string)) |
9187 | { | |
9188 | p = CHAR_POS_ADDR (from); | |
9189 | pend = CHAR_POS_ADDR (to); | |
9190 | if (from < GPT && to >= GPT) | |
9191 | stop = GPT_ADDR; | |
9192 | else | |
9193 | stop = pend; | |
9194 | charset_map_loaded = 0; | |
9195 | } | |
8f924df7 | 9196 | } |
d46c5b12 | 9197 | |
8f924df7 KH |
9198 | return (NILP (count) ? Fcar (positions) : Fnreverse (positions)); |
9199 | } | |
d46c5b12 | 9200 | |
d46c5b12 | 9201 | |
df7492f9 KH |
9202 | DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region, |
9203 | Scheck_coding_systems_region, 3, 3, 0, | |
9204 | doc: /* Check if the region is encodable by coding systems. | |
d46c5b12 | 9205 | |
df7492f9 KH |
9206 | START and END are buffer positions specifying the region. |
9207 | CODING-SYSTEM-LIST is a list of coding systems to check. | |
d46c5b12 | 9208 | |
df7492f9 | 9209 | The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where |
d4a1d553 | 9210 | CODING-SYSTEM is a member of CODING-SYSTEM-LIST and can't encode the |
df7492f9 KH |
9211 | whole region, POS0, POS1, ... are buffer positions where non-encodable |
9212 | characters are found. | |
93dec019 | 9213 | |
df7492f9 KH |
9214 | If all coding systems in CODING-SYSTEM-LIST can encode the region, the |
9215 | value is nil. | |
93dec019 | 9216 | |
df7492f9 KH |
9217 | START may be a string. In that case, check if the string is |
9218 | encodable, and the value contains indices to the string instead of | |
5704f39a KH |
9219 | buffer positions. END is ignored. |
9220 | ||
4c1958f4 | 9221 | If the current buffer (or START if it is a string) is unibyte, the value |
5704f39a | 9222 | is nil. */) |
5842a27b | 9223 | (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system_list) |
05e6f5dc | 9224 | { |
df7492f9 | 9225 | Lisp_Object list; |
d311d28c PE |
9226 | ptrdiff_t start_byte, end_byte; |
9227 | ptrdiff_t pos; | |
7c78e542 | 9228 | const unsigned char *p, *pbeg, *pend; |
df7492f9 | 9229 | int c; |
7d64c6ad | 9230 | Lisp_Object tail, elt, attrs; |
70ad9fc4 | 9231 | |
05e6f5dc KH |
9232 | if (STRINGP (start)) |
9233 | { | |
df7492f9 | 9234 | if (!STRING_MULTIBYTE (start) |
4c1958f4 | 9235 | || SCHARS (start) == SBYTES (start)) |
df7492f9 KH |
9236 | return Qnil; |
9237 | start_byte = 0; | |
8f924df7 | 9238 | end_byte = SBYTES (start); |
df7492f9 | 9239 | pos = 0; |
d46c5b12 | 9240 | } |
05e6f5dc | 9241 | else |
b73bfc1c | 9242 | { |
b7826503 PJ |
9243 | CHECK_NUMBER_COERCE_MARKER (start); |
9244 | CHECK_NUMBER_COERCE_MARKER (end); | |
05e6f5dc KH |
9245 | if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end)) |
9246 | args_out_of_range (start, end); | |
4b4deea2 | 9247 | if (NILP (BVAR (current_buffer, enable_multibyte_characters))) |
df7492f9 KH |
9248 | return Qnil; |
9249 | start_byte = CHAR_TO_BYTE (XINT (start)); | |
9250 | end_byte = CHAR_TO_BYTE (XINT (end)); | |
9251 | if (XINT (end) - XINT (start) == end_byte - start_byte) | |
5704f39a | 9252 | return Qnil; |
df7492f9 | 9253 | |
e1c23804 | 9254 | if (XINT (start) < GPT && XINT (end) > GPT) |
b73bfc1c | 9255 | { |
e1c23804 DL |
9256 | if ((GPT - XINT (start)) < (XINT (end) - GPT)) |
9257 | move_gap_both (XINT (start), start_byte); | |
df7492f9 | 9258 | else |
e1c23804 | 9259 | move_gap_both (XINT (end), end_byte); |
b73bfc1c | 9260 | } |
e1c23804 | 9261 | pos = XINT (start); |
b73bfc1c | 9262 | } |
7553d0e1 | 9263 | |
df7492f9 KH |
9264 | list = Qnil; |
9265 | for (tail = coding_system_list; CONSP (tail); tail = XCDR (tail)) | |
12410ef1 | 9266 | { |
df7492f9 | 9267 | elt = XCAR (tail); |
7d64c6ad | 9268 | attrs = AREF (CODING_SYSTEM_SPEC (elt), 0); |
2170c8f0 KH |
9269 | ASET (attrs, coding_attr_trans_tbl, |
9270 | get_translation_table (attrs, 1, NULL)); | |
6c6f1994 | 9271 | list = Fcons (list2 (elt, attrs), list); |
12410ef1 KH |
9272 | } |
9273 | ||
df7492f9 | 9274 | if (STRINGP (start)) |
8f924df7 | 9275 | p = pbeg = SDATA (start); |
72d1a715 | 9276 | else |
df7492f9 KH |
9277 | p = pbeg = BYTE_POS_ADDR (start_byte); |
9278 | pend = p + (end_byte - start_byte); | |
4ed46869 | 9279 | |
df7492f9 KH |
9280 | while (p < pend && ASCII_BYTE_P (*p)) p++, pos++; |
9281 | while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--; | |
ec6d2bb8 | 9282 | |
df7492f9 | 9283 | while (p < pend) |
d46c5b12 | 9284 | { |
df7492f9 KH |
9285 | if (ASCII_BYTE_P (*p)) |
9286 | p++; | |
e133c8fa | 9287 | else |
05e6f5dc | 9288 | { |
df7492f9 KH |
9289 | c = STRING_CHAR_ADVANCE (p); |
9290 | ||
9291 | charset_map_loaded = 0; | |
9292 | for (tail = list; CONSP (tail); tail = XCDR (tail)) | |
9293 | { | |
9294 | elt = XCDR (XCAR (tail)); | |
9295 | if (! char_encodable_p (c, XCAR (elt))) | |
9296 | XSETCDR (elt, Fcons (make_number (pos), XCDR (elt))); | |
9297 | } | |
9298 | if (charset_map_loaded) | |
9299 | { | |
d311d28c | 9300 | ptrdiff_t p_offset = p - pbeg, pend_offset = pend - pbeg; |
df7492f9 KH |
9301 | |
9302 | if (STRINGP (start)) | |
8f924df7 | 9303 | pbeg = SDATA (start); |
df7492f9 KH |
9304 | else |
9305 | pbeg = BYTE_POS_ADDR (start_byte); | |
9306 | p = pbeg + p_offset; | |
9307 | pend = pbeg + pend_offset; | |
9308 | } | |
05e6f5dc | 9309 | } |
df7492f9 | 9310 | pos++; |
d46c5b12 | 9311 | } |
4ed46869 | 9312 | |
df7492f9 KH |
9313 | tail = list; |
9314 | list = Qnil; | |
9315 | for (; CONSP (tail); tail = XCDR (tail)) | |
ec6d2bb8 | 9316 | { |
df7492f9 KH |
9317 | elt = XCAR (tail); |
9318 | if (CONSP (XCDR (XCDR (elt)))) | |
9319 | list = Fcons (Fcons (XCAR (elt), Fnreverse (XCDR (XCDR (elt)))), | |
9320 | list); | |
ec6d2bb8 | 9321 | } |
2b4f9037 | 9322 | |
df7492f9 | 9323 | return list; |
d46c5b12 KH |
9324 | } |
9325 | ||
3fd9494b | 9326 | |
74ab6df5 | 9327 | static Lisp_Object |
cf84bb53 JB |
9328 | code_convert_region (Lisp_Object start, Lisp_Object end, |
9329 | Lisp_Object coding_system, Lisp_Object dst_object, | |
f10fe38f | 9330 | bool encodep, bool norecord) |
4ed46869 | 9331 | { |
3a73fa5d | 9332 | struct coding_system coding; |
d311d28c | 9333 | ptrdiff_t from, from_byte, to, to_byte; |
df7492f9 | 9334 | Lisp_Object src_object; |
4ed46869 | 9335 | |
df7492f9 KH |
9336 | if (NILP (coding_system)) |
9337 | coding_system = Qno_conversion; | |
9338 | else | |
9339 | CHECK_CODING_SYSTEM (coding_system); | |
9340 | src_object = Fcurrent_buffer (); | |
9341 | if (NILP (dst_object)) | |
9342 | dst_object = src_object; | |
9343 | else if (! EQ (dst_object, Qt)) | |
9344 | CHECK_BUFFER (dst_object); | |
3a73fa5d | 9345 | |
d46c5b12 KH |
9346 | validate_region (&start, &end); |
9347 | from = XFASTINT (start); | |
df7492f9 | 9348 | from_byte = CHAR_TO_BYTE (from); |
d46c5b12 | 9349 | to = XFASTINT (end); |
df7492f9 | 9350 | to_byte = CHAR_TO_BYTE (to); |
764ca8da | 9351 | |
df7492f9 KH |
9352 | setup_coding_system (coding_system, &coding); |
9353 | coding.mode |= CODING_MODE_LAST_BLOCK; | |
ec6d2bb8 | 9354 | |
df7492f9 KH |
9355 | if (encodep) |
9356 | encode_coding_object (&coding, src_object, from, from_byte, to, to_byte, | |
9357 | dst_object); | |
9358 | else | |
9359 | decode_coding_object (&coding, src_object, from, from_byte, to, to_byte, | |
9360 | dst_object); | |
9361 | if (! norecord) | |
9362 | Vlast_coding_system_used = CODING_ID_NAME (coding.id); | |
ec6d2bb8 | 9363 | |
df7492f9 KH |
9364 | return (BUFFERP (dst_object) |
9365 | ? make_number (coding.produced_char) | |
9366 | : coding.dst_object); | |
4031e2bf | 9367 | } |
78108bcd | 9368 | |
4ed46869 | 9369 | |
4031e2bf | 9370 | DEFUN ("decode-coding-region", Fdecode_coding_region, Sdecode_coding_region, |
df7492f9 | 9371 | 3, 4, "r\nzCoding system: ", |
48b0f3ae | 9372 | doc: /* Decode the current region from the specified coding system. |
df7492f9 KH |
9373 | When called from a program, takes four arguments: |
9374 | START, END, CODING-SYSTEM, and DESTINATION. | |
9375 | START and END are buffer positions. | |
8844fa83 | 9376 | |
df7492f9 | 9377 | Optional 4th arguments DESTINATION specifies where the decoded text goes. |
2354b80b | 9378 | If nil, the region between START and END is replaced by the decoded text. |
1560f91a EZ |
9379 | If buffer, the decoded text is inserted in that buffer after point (point |
9380 | does not move). | |
446dcd75 | 9381 | In those cases, the length of the decoded text is returned. |
319a3947 | 9382 | If DESTINATION is t, the decoded text is returned. |
8844fa83 | 9383 | |
48b0f3ae PJ |
9384 | This function sets `last-coding-system-used' to the precise coding system |
9385 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
319a3947 | 9386 | not fully specified.) */) |
5842a27b | 9387 | (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object destination) |
4031e2bf | 9388 | { |
df7492f9 | 9389 | return code_convert_region (start, end, coding_system, destination, 0, 0); |
3a73fa5d | 9390 | } |
8844fa83 | 9391 | |
3a73fa5d | 9392 | DEFUN ("encode-coding-region", Fencode_coding_region, Sencode_coding_region, |
df7492f9 KH |
9393 | 3, 4, "r\nzCoding system: ", |
9394 | doc: /* Encode the current region by specified coding system. | |
d4a1d553 JB |
9395 | When called from a program, takes four arguments: |
9396 | START, END, CODING-SYSTEM and DESTINATION. | |
9397 | START and END are buffer positions. | |
d46c5b12 | 9398 | |
df7492f9 KH |
9399 | Optional 4th arguments DESTINATION specifies where the encoded text goes. |
9400 | If nil, the region between START and END is replace by the encoded text. | |
1560f91a EZ |
9401 | If buffer, the encoded text is inserted in that buffer after point (point |
9402 | does not move). | |
446dcd75 | 9403 | In those cases, the length of the encoded text is returned. |
319a3947 | 9404 | If DESTINATION is t, the encoded text is returned. |
2391eaa4 | 9405 | |
48b0f3ae PJ |
9406 | This function sets `last-coding-system-used' to the precise coding system |
9407 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
319a3947 | 9408 | not fully specified.) */) |
5842a27b | 9409 | (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object destination) |
3a73fa5d | 9410 | { |
df7492f9 | 9411 | return code_convert_region (start, end, coding_system, destination, 1, 0); |
b73bfc1c KH |
9412 | } |
9413 | ||
9414 | Lisp_Object | |
6f704c76 | 9415 | code_convert_string (Lisp_Object string, Lisp_Object coding_system, |
f10fe38f PE |
9416 | Lisp_Object dst_object, bool encodep, bool nocopy, |
9417 | bool norecord) | |
b73bfc1c | 9418 | { |
4031e2bf | 9419 | struct coding_system coding; |
d311d28c | 9420 | ptrdiff_t chars, bytes; |
ec6d2bb8 | 9421 | |
b7826503 | 9422 | CHECK_STRING (string); |
d46c5b12 | 9423 | if (NILP (coding_system)) |
4956c225 | 9424 | { |
df7492f9 KH |
9425 | if (! norecord) |
9426 | Vlast_coding_system_used = Qno_conversion; | |
9427 | if (NILP (dst_object)) | |
9428 | return (nocopy ? Fcopy_sequence (string) : string); | |
4956c225 | 9429 | } |
b73bfc1c | 9430 | |
df7492f9 KH |
9431 | if (NILP (coding_system)) |
9432 | coding_system = Qno_conversion; | |
9433 | else | |
9434 | CHECK_CODING_SYSTEM (coding_system); | |
9435 | if (NILP (dst_object)) | |
9436 | dst_object = Qt; | |
9437 | else if (! EQ (dst_object, Qt)) | |
9438 | CHECK_BUFFER (dst_object); | |
73be902c | 9439 | |
df7492f9 | 9440 | setup_coding_system (coding_system, &coding); |
d46c5b12 | 9441 | coding.mode |= CODING_MODE_LAST_BLOCK; |
8f924df7 KH |
9442 | chars = SCHARS (string); |
9443 | bytes = SBYTES (string); | |
df7492f9 KH |
9444 | if (encodep) |
9445 | encode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object); | |
9446 | else | |
9447 | decode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object); | |
9448 | if (! norecord) | |
9449 | Vlast_coding_system_used = CODING_ID_NAME (coding.id); | |
73be902c | 9450 | |
df7492f9 KH |
9451 | return (BUFFERP (dst_object) |
9452 | ? make_number (coding.produced_char) | |
9453 | : coding.dst_object); | |
4ed46869 | 9454 | } |
73be902c | 9455 | |
b73bfc1c | 9456 | |
ecec61c1 | 9457 | /* Encode or decode STRING according to CODING_SYSTEM. |
ec6d2bb8 | 9458 | Do not set Vlast_coding_system_used. |
4ed46869 | 9459 | |
ec6d2bb8 KH |
9460 | This function is called only from macros DECODE_FILE and |
9461 | ENCODE_FILE, thus we ignore character composition. */ | |
4ed46869 | 9462 | |
ecec61c1 | 9463 | Lisp_Object |
cf84bb53 | 9464 | code_convert_string_norecord (Lisp_Object string, Lisp_Object coding_system, |
f10fe38f | 9465 | bool encodep) |
4ed46869 | 9466 | { |
0be8721c | 9467 | return code_convert_string (string, coding_system, Qt, encodep, 0, 1); |
4ed46869 KH |
9468 | } |
9469 | ||
4ed46869 | 9470 | |
a7ca3326 | 9471 | DEFUN ("decode-coding-string", Fdecode_coding_string, Sdecode_coding_string, |
df7492f9 KH |
9472 | 2, 4, 0, |
9473 | doc: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result. | |
9474 | ||
9475 | Optional third arg NOCOPY non-nil means it is OK to return STRING itself | |
9476 | if the decoding operation is trivial. | |
ecec61c1 | 9477 | |
d4a1d553 | 9478 | Optional fourth arg BUFFER non-nil means that the decoded text is |
1560f91a EZ |
9479 | inserted in that buffer after point (point does not move). In this |
9480 | case, the return value is the length of the decoded text. | |
ecec61c1 | 9481 | |
df7492f9 KH |
9482 | This function sets `last-coding-system-used' to the precise coding system |
9483 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
d4a1d553 | 9484 | not fully specified.) */) |
5842a27b | 9485 | (Lisp_Object string, Lisp_Object coding_system, Lisp_Object nocopy, Lisp_Object buffer) |
4ed46869 | 9486 | { |
df7492f9 KH |
9487 | return code_convert_string (string, coding_system, buffer, |
9488 | 0, ! NILP (nocopy), 0); | |
4ed46869 KH |
9489 | } |
9490 | ||
df7492f9 KH |
9491 | DEFUN ("encode-coding-string", Fencode_coding_string, Sencode_coding_string, |
9492 | 2, 4, 0, | |
9493 | doc: /* Encode STRING to CODING-SYSTEM, and return the result. | |
9494 | ||
9495 | Optional third arg NOCOPY non-nil means it is OK to return STRING | |
9496 | itself if the encoding operation is trivial. | |
9497 | ||
d4a1d553 | 9498 | Optional fourth arg BUFFER non-nil means that the encoded text is |
1560f91a EZ |
9499 | inserted in that buffer after point (point does not move). In this |
9500 | case, the return value is the length of the encoded text. | |
df7492f9 KH |
9501 | |
9502 | This function sets `last-coding-system-used' to the precise coding system | |
9503 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
9504 | not fully specified.) */) | |
5842a27b | 9505 | (Lisp_Object string, Lisp_Object coding_system, Lisp_Object nocopy, Lisp_Object buffer) |
4ed46869 | 9506 | { |
df7492f9 | 9507 | return code_convert_string (string, coding_system, buffer, |
4550efdf | 9508 | 1, ! NILP (nocopy), 0); |
4ed46869 | 9509 | } |
df7492f9 | 9510 | |
3a73fa5d | 9511 | \f |
4ed46869 | 9512 | DEFUN ("decode-sjis-char", Fdecode_sjis_char, Sdecode_sjis_char, 1, 1, 0, |
48b0f3ae PJ |
9513 | doc: /* Decode a Japanese character which has CODE in shift_jis encoding. |
9514 | Return the corresponding character. */) | |
5842a27b | 9515 | (Lisp_Object code) |
4ed46869 | 9516 | { |
df7492f9 KH |
9517 | Lisp_Object spec, attrs, val; |
9518 | struct charset *charset_roman, *charset_kanji, *charset_kana, *charset; | |
5fdb398c PE |
9519 | EMACS_INT ch; |
9520 | int c; | |
4ed46869 | 9521 | |
df7492f9 | 9522 | CHECK_NATNUM (code); |
5fdb398c | 9523 | ch = XFASTINT (code); |
df7492f9 KH |
9524 | CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec); |
9525 | attrs = AREF (spec, 0); | |
4ed46869 | 9526 | |
5fdb398c | 9527 | if (ASCII_BYTE_P (ch) |
df7492f9 KH |
9528 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) |
9529 | return code; | |
4ed46869 | 9530 | |
df7492f9 KH |
9531 | val = CODING_ATTR_CHARSET_LIST (attrs); |
9532 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
004068e4 KH |
9533 | charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
9534 | charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))); | |
fa42c37f | 9535 | |
5fdb398c PE |
9536 | if (ch <= 0x7F) |
9537 | { | |
9538 | c = ch; | |
9539 | charset = charset_roman; | |
9540 | } | |
9541 | else if (ch >= 0xA0 && ch < 0xDF) | |
55ab7be3 | 9542 | { |
5fdb398c | 9543 | c = ch - 0x80; |
df7492f9 | 9544 | charset = charset_kana; |
4ed46869 | 9545 | } |
55ab7be3 | 9546 | else |
4ed46869 | 9547 | { |
5fdb398c PE |
9548 | EMACS_INT c1 = ch >> 8; |
9549 | int c2 = ch & 0xFF; | |
df7492f9 | 9550 | |
2735d060 PE |
9551 | if (c1 < 0x81 || (c1 > 0x9F && c1 < 0xE0) || c1 > 0xEF |
9552 | || c2 < 0x40 || c2 == 0x7F || c2 > 0xFC) | |
c2982e87 | 9553 | error ("Invalid code: %"pI"d", ch); |
5fdb398c | 9554 | c = ch; |
df7492f9 KH |
9555 | SJIS_TO_JIS (c); |
9556 | charset = charset_kanji; | |
4ed46869 | 9557 | } |
df7492f9 KH |
9558 | c = DECODE_CHAR (charset, c); |
9559 | if (c < 0) | |
c2982e87 | 9560 | error ("Invalid code: %"pI"d", ch); |
df7492f9 | 9561 | return make_number (c); |
93dec019 | 9562 | } |
4ed46869 | 9563 | |
48b0f3ae | 9564 | |
4ed46869 | 9565 | DEFUN ("encode-sjis-char", Fencode_sjis_char, Sencode_sjis_char, 1, 1, 0, |
8acf0c0e | 9566 | doc: /* Encode a Japanese character CH to shift_jis encoding. |
48b0f3ae | 9567 | Return the corresponding code in SJIS. */) |
5842a27b | 9568 | (Lisp_Object ch) |
4ed46869 | 9569 | { |
df7492f9 KH |
9570 | Lisp_Object spec, attrs, charset_list; |
9571 | int c; | |
9572 | struct charset *charset; | |
9573 | unsigned code; | |
48b0f3ae | 9574 | |
df7492f9 KH |
9575 | CHECK_CHARACTER (ch); |
9576 | c = XFASTINT (ch); | |
9577 | CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec); | |
9578 | attrs = AREF (spec, 0); | |
9579 | ||
9580 | if (ASCII_CHAR_P (c) | |
9581 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
9582 | return ch; | |
9583 | ||
9584 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
9585 | charset = char_charset (c, charset_list, &code); | |
9586 | if (code == CHARSET_INVALID_CODE (charset)) | |
e6c3da20 | 9587 | error ("Can't encode by shift_jis encoding: %c", c); |
df7492f9 KH |
9588 | JIS_TO_SJIS (code); |
9589 | ||
9590 | return make_number (code); | |
4ed46869 KH |
9591 | } |
9592 | ||
9593 | DEFUN ("decode-big5-char", Fdecode_big5_char, Sdecode_big5_char, 1, 1, 0, | |
48b0f3ae PJ |
9594 | doc: /* Decode a Big5 character which has CODE in BIG5 coding system. |
9595 | Return the corresponding character. */) | |
5842a27b | 9596 | (Lisp_Object code) |
d46c5b12 | 9597 | { |
df7492f9 KH |
9598 | Lisp_Object spec, attrs, val; |
9599 | struct charset *charset_roman, *charset_big5, *charset; | |
5fdb398c | 9600 | EMACS_INT ch; |
df7492f9 | 9601 | int c; |
6289dd10 | 9602 | |
df7492f9 | 9603 | CHECK_NATNUM (code); |
5fdb398c | 9604 | ch = XFASTINT (code); |
df7492f9 KH |
9605 | CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec); |
9606 | attrs = AREF (spec, 0); | |
4ed46869 | 9607 | |
5fdb398c | 9608 | if (ASCII_BYTE_P (ch) |
df7492f9 KH |
9609 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) |
9610 | return code; | |
6289dd10 | 9611 | |
df7492f9 KH |
9612 | val = CODING_ATTR_CHARSET_LIST (attrs); |
9613 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
9614 | charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val))); | |
c210f766 | 9615 | |
5fdb398c PE |
9616 | if (ch <= 0x7F) |
9617 | { | |
9618 | c = ch; | |
9619 | charset = charset_roman; | |
9620 | } | |
c28a9453 KH |
9621 | else |
9622 | { | |
5fdb398c PE |
9623 | EMACS_INT b1 = ch >> 8; |
9624 | int b2 = ch & 0x7F; | |
df7492f9 KH |
9625 | if (b1 < 0xA1 || b1 > 0xFE |
9626 | || b2 < 0x40 || (b2 > 0x7E && b2 < 0xA1) || b2 > 0xFE) | |
c2982e87 | 9627 | error ("Invalid code: %"pI"d", ch); |
5fdb398c | 9628 | c = ch; |
df7492f9 | 9629 | charset = charset_big5; |
c28a9453 | 9630 | } |
5fdb398c | 9631 | c = DECODE_CHAR (charset, c); |
df7492f9 | 9632 | if (c < 0) |
c2982e87 | 9633 | error ("Invalid code: %"pI"d", ch); |
df7492f9 | 9634 | return make_number (c); |
d46c5b12 | 9635 | } |
6289dd10 | 9636 | |
4ed46869 | 9637 | DEFUN ("encode-big5-char", Fencode_big5_char, Sencode_big5_char, 1, 1, 0, |
8acf0c0e | 9638 | doc: /* Encode the Big5 character CH to BIG5 coding system. |
48b0f3ae | 9639 | Return the corresponding character code in Big5. */) |
5842a27b | 9640 | (Lisp_Object ch) |
4ed46869 | 9641 | { |
df7492f9 KH |
9642 | Lisp_Object spec, attrs, charset_list; |
9643 | struct charset *charset; | |
9644 | int c; | |
9645 | unsigned code; | |
9646 | ||
9647 | CHECK_CHARACTER (ch); | |
9648 | c = XFASTINT (ch); | |
9649 | CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec); | |
9650 | attrs = AREF (spec, 0); | |
9651 | if (ASCII_CHAR_P (c) | |
9652 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
9653 | return ch; | |
9654 | ||
9655 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
9656 | charset = char_charset (c, charset_list, &code); | |
9657 | if (code == CHARSET_INVALID_CODE (charset)) | |
e6c3da20 | 9658 | error ("Can't encode by Big5 encoding: %c", c); |
df7492f9 KH |
9659 | |
9660 | return make_number (code); | |
4ed46869 | 9661 | } |
48b0f3ae | 9662 | |
3a73fa5d | 9663 | \f |
002fdb44 | 9664 | DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal, |
68bba4e4 | 9665 | Sset_terminal_coding_system_internal, 1, 2, 0, |
48b0f3ae | 9666 | doc: /* Internal use only. */) |
5842a27b | 9667 | (Lisp_Object coding_system, Lisp_Object terminal) |
4ed46869 | 9668 | { |
b18fad6d KH |
9669 | struct terminal *term = get_terminal (terminal, 1); |
9670 | struct coding_system *terminal_coding = TERMINAL_TERMINAL_CODING (term); | |
b7826503 | 9671 | CHECK_SYMBOL (coding_system); |
b8299c66 | 9672 | setup_coding_system (Fcheck_coding_system (coding_system), terminal_coding); |
70c22245 | 9673 | /* We had better not send unsafe characters to terminal. */ |
c73bd236 | 9674 | terminal_coding->mode |= CODING_MODE_SAFE_ENCODING; |
ad1746f5 | 9675 | /* Character composition should be disabled. */ |
c73bd236 | 9676 | terminal_coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK; |
b8299c66 KL |
9677 | terminal_coding->src_multibyte = 1; |
9678 | terminal_coding->dst_multibyte = 0; | |
3f22b86f PE |
9679 | tset_charset_list |
9680 | (term, (terminal_coding->common_flags & CODING_REQUIRE_ENCODING_MASK | |
9681 | ? coding_charset_list (terminal_coding) | |
6c6f1994 | 9682 | : list1 (make_number (charset_ascii)))); |
4ed46869 KH |
9683 | return Qnil; |
9684 | } | |
9685 | ||
c4825358 KH |
9686 | DEFUN ("set-safe-terminal-coding-system-internal", |
9687 | Fset_safe_terminal_coding_system_internal, | |
48b0f3ae | 9688 | Sset_safe_terminal_coding_system_internal, 1, 1, 0, |
ddb67bdc | 9689 | doc: /* Internal use only. */) |
5842a27b | 9690 | (Lisp_Object coding_system) |
d46c5b12 | 9691 | { |
b7826503 | 9692 | CHECK_SYMBOL (coding_system); |
c4825358 KH |
9693 | setup_coding_system (Fcheck_coding_system (coding_system), |
9694 | &safe_terminal_coding); | |
ad1746f5 | 9695 | /* Character composition should be disabled. */ |
df7492f9 | 9696 | safe_terminal_coding.common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK; |
b73bfc1c KH |
9697 | safe_terminal_coding.src_multibyte = 1; |
9698 | safe_terminal_coding.dst_multibyte = 0; | |
c4825358 KH |
9699 | return Qnil; |
9700 | } | |
4ed46869 | 9701 | |
002fdb44 | 9702 | DEFUN ("terminal-coding-system", Fterminal_coding_system, |
68bba4e4 | 9703 | Sterminal_coding_system, 0, 1, 0, |
6ed8eeff | 9704 | doc: /* Return coding system specified for terminal output on the given terminal. |
708e05dc | 9705 | TERMINAL may be a terminal object, a frame, or nil for the selected |
6ed8eeff | 9706 | frame's terminal device. */) |
5842a27b | 9707 | (Lisp_Object terminal) |
4ed46869 | 9708 | { |
985773c9 MB |
9709 | struct coding_system *terminal_coding |
9710 | = TERMINAL_TERMINAL_CODING (get_terminal (terminal, 1)); | |
9711 | Lisp_Object coding_system = CODING_ID_NAME (terminal_coding->id); | |
ae6f73fa | 9712 | |
6d5eb5b0 | 9713 | /* For backward compatibility, return nil if it is `undecided'. */ |
f75c90a9 | 9714 | return (! EQ (coding_system, Qundecided) ? coding_system : Qnil); |
4ed46869 KH |
9715 | } |
9716 | ||
002fdb44 | 9717 | DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal, |
68bba4e4 | 9718 | Sset_keyboard_coding_system_internal, 1, 2, 0, |
48b0f3ae | 9719 | doc: /* Internal use only. */) |
5842a27b | 9720 | (Lisp_Object coding_system, Lisp_Object terminal) |
4ed46869 | 9721 | { |
6ed8eeff | 9722 | struct terminal *t = get_terminal (terminal, 1); |
b7826503 | 9723 | CHECK_SYMBOL (coding_system); |
624bda09 KH |
9724 | if (NILP (coding_system)) |
9725 | coding_system = Qno_conversion; | |
9726 | else | |
9727 | Fcheck_coding_system (coding_system); | |
9728 | setup_coding_system (coding_system, TERMINAL_KEYBOARD_CODING (t)); | |
ad1746f5 | 9729 | /* Character composition should be disabled. */ |
c73bd236 MB |
9730 | TERMINAL_KEYBOARD_CODING (t)->common_flags |
9731 | &= ~CODING_ANNOTATE_COMPOSITION_MASK; | |
4ed46869 KH |
9732 | return Qnil; |
9733 | } | |
9734 | ||
9735 | DEFUN ("keyboard-coding-system", | |
985773c9 | 9736 | Fkeyboard_coding_system, Skeyboard_coding_system, 0, 1, 0, |
48b0f3ae | 9737 | doc: /* Return coding system specified for decoding keyboard input. */) |
5842a27b | 9738 | (Lisp_Object terminal) |
4ed46869 | 9739 | { |
985773c9 MB |
9740 | return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING |
9741 | (get_terminal (terminal, 1))->id); | |
4ed46869 KH |
9742 | } |
9743 | ||
4ed46869 | 9744 | \f |
a7ca3326 | 9745 | DEFUN ("find-operation-coding-system", Ffind_operation_coding_system, |
a5d301df | 9746 | Sfind_operation_coding_system, 1, MANY, 0, |
48b0f3ae PJ |
9747 | doc: /* Choose a coding system for an operation based on the target name. |
9748 | The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM). | |
9749 | DECODING-SYSTEM is the coding system to use for decoding | |
9750 | \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system | |
9751 | for encoding (in case OPERATION does encoding). | |
05e6f5dc | 9752 | |
48b0f3ae PJ |
9753 | The first argument OPERATION specifies an I/O primitive: |
9754 | For file I/O, `insert-file-contents' or `write-region'. | |
9755 | For process I/O, `call-process', `call-process-region', or `start-process'. | |
9756 | For network I/O, `open-network-stream'. | |
05e6f5dc | 9757 | |
48b0f3ae PJ |
9758 | The remaining arguments should be the same arguments that were passed |
9759 | to the primitive. Depending on which primitive, one of those arguments | |
9760 | is selected as the TARGET. For example, if OPERATION does file I/O, | |
9761 | whichever argument specifies the file name is TARGET. | |
05e6f5dc | 9762 | |
48b0f3ae | 9763 | TARGET has a meaning which depends on OPERATION: |
b883cdb2 | 9764 | For file I/O, TARGET is a file name (except for the special case below). |
48b0f3ae | 9765 | For process I/O, TARGET is a process name. |
d4a1d553 | 9766 | For network I/O, TARGET is a service name or a port number. |
05e6f5dc | 9767 | |
d4a1d553 | 9768 | This function looks up what is specified for TARGET in |
48b0f3ae PJ |
9769 | `file-coding-system-alist', `process-coding-system-alist', |
9770 | or `network-coding-system-alist' depending on OPERATION. | |
9771 | They may specify a coding system, a cons of coding systems, | |
9772 | or a function symbol to call. | |
9773 | In the last case, we call the function with one argument, | |
9774 | which is a list of all the arguments given to this function. | |
1011c487 MB |
9775 | If the function can't decide a coding system, it can return |
9776 | `undecided' so that the normal code-detection is performed. | |
48b0f3ae | 9777 | |
b883cdb2 MB |
9778 | If OPERATION is `insert-file-contents', the argument corresponding to |
9779 | TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a | |
9780 | file name to look up, and BUFFER is a buffer that contains the file's | |
9781 | contents (not yet decoded). If `file-coding-system-alist' specifies a | |
9782 | function to call for FILENAME, that function should examine the | |
9783 | contents of BUFFER instead of reading the file. | |
9784 | ||
d918f936 | 9785 | usage: (find-operation-coding-system OPERATION ARGUMENTS...) */) |
f66c7cf8 | 9786 | (ptrdiff_t nargs, Lisp_Object *args) |
6b89e3aa | 9787 | { |
4ed46869 KH |
9788 | Lisp_Object operation, target_idx, target, val; |
9789 | register Lisp_Object chain; | |
177c0ea7 | 9790 | |
4ed46869 KH |
9791 | if (nargs < 2) |
9792 | error ("Too few arguments"); | |
9793 | operation = args[0]; | |
9794 | if (!SYMBOLP (operation) | |
d311d28c | 9795 | || (target_idx = Fget (operation, Qtarget_idx), !NATNUMP (target_idx))) |
3ed051d4 | 9796 | error ("Invalid first argument"); |
7b09a37a | 9797 | if (nargs <= 1 + XFASTINT (target_idx)) |
94dcfacf | 9798 | error ("Too few arguments for operation `%s'", |
8f924df7 | 9799 | SDATA (SYMBOL_NAME (operation))); |
c5101a77 | 9800 | target = args[XFASTINT (target_idx) + 1]; |
4ed46869 | 9801 | if (!(STRINGP (target) |
091a0ff0 KH |
9802 | || (EQ (operation, Qinsert_file_contents) && CONSP (target) |
9803 | && STRINGP (XCAR (target)) && BUFFERP (XCDR (target))) | |
4ed46869 | 9804 | || (EQ (operation, Qopen_network_stream) && INTEGERP (target)))) |
94dcfacf EZ |
9805 | error ("Invalid argument %"pI"d of operation `%s'", |
9806 | XFASTINT (target_idx) + 1, SDATA (SYMBOL_NAME (operation))); | |
091a0ff0 KH |
9807 | if (CONSP (target)) |
9808 | target = XCAR (target); | |
4ed46869 | 9809 | |
2e34157c RS |
9810 | chain = ((EQ (operation, Qinsert_file_contents) |
9811 | || EQ (operation, Qwrite_region)) | |
02ba4723 | 9812 | ? Vfile_coding_system_alist |
2e34157c | 9813 | : (EQ (operation, Qopen_network_stream) |
02ba4723 KH |
9814 | ? Vnetwork_coding_system_alist |
9815 | : Vprocess_coding_system_alist)); | |
4ed46869 KH |
9816 | if (NILP (chain)) |
9817 | return Qnil; | |
9818 | ||
03699b14 | 9819 | for (; CONSP (chain); chain = XCDR (chain)) |
6b89e3aa | 9820 | { |
f44d27ce | 9821 | Lisp_Object elt; |
6b89e3aa | 9822 | |
df7492f9 | 9823 | elt = XCAR (chain); |
4ed46869 KH |
9824 | if (CONSP (elt) |
9825 | && ((STRINGP (target) | |
03699b14 KR |
9826 | && STRINGP (XCAR (elt)) |
9827 | && fast_string_match (XCAR (elt), target) >= 0) | |
9828 | || (INTEGERP (target) && EQ (target, XCAR (elt))))) | |
6b89e3aa | 9829 | { |
03699b14 | 9830 | val = XCDR (elt); |
b19fd4c5 KH |
9831 | /* Here, if VAL is both a valid coding system and a valid |
9832 | function symbol, we return VAL as a coding system. */ | |
02ba4723 KH |
9833 | if (CONSP (val)) |
9834 | return val; | |
9835 | if (! SYMBOLP (val)) | |
9836 | return Qnil; | |
9837 | if (! NILP (Fcoding_system_p (val))) | |
9838 | return Fcons (val, val); | |
b19fd4c5 | 9839 | if (! NILP (Ffboundp (val))) |
6b89e3aa | 9840 | { |
e2b97060 MB |
9841 | /* We use call1 rather than safe_call1 |
9842 | so as to get bug reports about functions called here | |
9843 | which don't handle the current interface. */ | |
9844 | val = call1 (val, Flist (nargs, args)); | |
b19fd4c5 KH |
9845 | if (CONSP (val)) |
9846 | return val; | |
9847 | if (SYMBOLP (val) && ! NILP (Fcoding_system_p (val))) | |
9848 | return Fcons (val, val); | |
6b89e3aa | 9849 | } |
02ba4723 | 9850 | return Qnil; |
6b89e3aa KH |
9851 | } |
9852 | } | |
4ed46869 | 9853 | return Qnil; |
6b89e3aa KH |
9854 | } |
9855 | ||
df7492f9 | 9856 | DEFUN ("set-coding-system-priority", Fset_coding_system_priority, |
a3f6ee6d | 9857 | Sset_coding_system_priority, 0, MANY, 0, |
da7db224 | 9858 | doc: /* Assign higher priority to the coding systems given as arguments. |
d4a1d553 | 9859 | If multiple coding systems belong to the same category, |
a3181084 DL |
9860 | all but the first one are ignored. |
9861 | ||
d4a1d553 | 9862 | usage: (set-coding-system-priority &rest coding-systems) */) |
f66c7cf8 | 9863 | (ptrdiff_t nargs, Lisp_Object *args) |
df7492f9 | 9864 | { |
f66c7cf8 | 9865 | ptrdiff_t i, j; |
f10fe38f | 9866 | bool changed[coding_category_max]; |
df7492f9 KH |
9867 | enum coding_category priorities[coding_category_max]; |
9868 | ||
72af86bd | 9869 | memset (changed, 0, sizeof changed); |
6b89e3aa | 9870 | |
df7492f9 | 9871 | for (i = j = 0; i < nargs; i++) |
6b89e3aa | 9872 | { |
df7492f9 KH |
9873 | enum coding_category category; |
9874 | Lisp_Object spec, attrs; | |
6b89e3aa | 9875 | |
df7492f9 KH |
9876 | CHECK_CODING_SYSTEM_GET_SPEC (args[i], spec); |
9877 | attrs = AREF (spec, 0); | |
9878 | category = XINT (CODING_ATTR_CATEGORY (attrs)); | |
9879 | if (changed[category]) | |
9880 | /* Ignore this coding system because a coding system of the | |
9881 | same category already had a higher priority. */ | |
9882 | continue; | |
9883 | changed[category] = 1; | |
9884 | priorities[j++] = category; | |
9885 | if (coding_categories[category].id >= 0 | |
9886 | && ! EQ (args[i], CODING_ID_NAME (coding_categories[category].id))) | |
9887 | setup_coding_system (args[i], &coding_categories[category]); | |
ff563fce | 9888 | Fset (AREF (Vcoding_category_table, category), args[i]); |
df7492f9 | 9889 | } |
6b89e3aa | 9890 | |
df7492f9 KH |
9891 | /* Now we have decided top J priorities. Reflect the order of the |
9892 | original priorities to the remaining priorities. */ | |
6b89e3aa | 9893 | |
df7492f9 | 9894 | for (i = j, j = 0; i < coding_category_max; i++, j++) |
6b89e3aa | 9895 | { |
df7492f9 KH |
9896 | while (j < coding_category_max |
9897 | && changed[coding_priorities[j]]) | |
9898 | j++; | |
9899 | if (j == coding_category_max) | |
1088b922 | 9900 | emacs_abort (); |
df7492f9 KH |
9901 | priorities[i] = coding_priorities[j]; |
9902 | } | |
6b89e3aa | 9903 | |
72af86bd | 9904 | memcpy (coding_priorities, priorities, sizeof priorities); |
177c0ea7 | 9905 | |
ff563fce KH |
9906 | /* Update `coding-category-list'. */ |
9907 | Vcoding_category_list = Qnil; | |
c5101a77 | 9908 | for (i = coding_category_max; i-- > 0; ) |
ff563fce KH |
9909 | Vcoding_category_list |
9910 | = Fcons (AREF (Vcoding_category_table, priorities[i]), | |
9911 | Vcoding_category_list); | |
6b89e3aa | 9912 | |
df7492f9 | 9913 | return Qnil; |
6b89e3aa KH |
9914 | } |
9915 | ||
df7492f9 KH |
9916 | DEFUN ("coding-system-priority-list", Fcoding_system_priority_list, |
9917 | Scoding_system_priority_list, 0, 1, 0, | |
da7db224 | 9918 | doc: /* Return a list of coding systems ordered by their priorities. |
b811c52b KH |
9919 | The list contains a subset of coding systems; i.e. coding systems |
9920 | assigned to each coding category (see `coding-category-list'). | |
9921 | ||
da7db224 | 9922 | HIGHESTP non-nil means just return the highest priority one. */) |
5842a27b | 9923 | (Lisp_Object highestp) |
d46c5b12 KH |
9924 | { |
9925 | int i; | |
df7492f9 | 9926 | Lisp_Object val; |
6b89e3aa | 9927 | |
df7492f9 | 9928 | for (i = 0, val = Qnil; i < coding_category_max; i++) |
d46c5b12 | 9929 | { |
df7492f9 KH |
9930 | enum coding_category category = coding_priorities[i]; |
9931 | int id = coding_categories[category].id; | |
9932 | Lisp_Object attrs; | |
068a9dbd | 9933 | |
df7492f9 KH |
9934 | if (id < 0) |
9935 | continue; | |
9936 | attrs = CODING_ID_ATTRS (id); | |
9937 | if (! NILP (highestp)) | |
9938 | return CODING_ATTR_BASE_NAME (attrs); | |
9939 | val = Fcons (CODING_ATTR_BASE_NAME (attrs), val); | |
9940 | } | |
9941 | return Fnreverse (val); | |
9942 | } | |
068a9dbd | 9943 | |
91433552 | 9944 | static const char *const suffixes[] = { "-unix", "-dos", "-mac" }; |
068a9dbd KH |
9945 | |
9946 | static Lisp_Object | |
971de7fb | 9947 | make_subsidiaries (Lisp_Object base) |
068a9dbd | 9948 | { |
df7492f9 | 9949 | Lisp_Object subsidiaries; |
1bfdaf10 | 9950 | ptrdiff_t base_name_len = SBYTES (SYMBOL_NAME (base)); |
38182d90 | 9951 | char *buf = alloca (base_name_len + 6); |
df7492f9 | 9952 | int i; |
068a9dbd | 9953 | |
72af86bd | 9954 | memcpy (buf, SDATA (SYMBOL_NAME (base)), base_name_len); |
25721f5b | 9955 | subsidiaries = make_uninit_vector (3); |
df7492f9 | 9956 | for (i = 0; i < 3; i++) |
068a9dbd | 9957 | { |
1bfdaf10 | 9958 | strcpy (buf + base_name_len, suffixes[i]); |
df7492f9 | 9959 | ASET (subsidiaries, i, intern (buf)); |
068a9dbd | 9960 | } |
df7492f9 | 9961 | return subsidiaries; |
068a9dbd KH |
9962 | } |
9963 | ||
9964 | ||
df7492f9 KH |
9965 | DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal, |
9966 | Sdefine_coding_system_internal, coding_arg_max, MANY, 0, | |
1fcd6c8b DL |
9967 | doc: /* For internal use only. |
9968 | usage: (define-coding-system-internal ...) */) | |
f66c7cf8 | 9969 | (ptrdiff_t nargs, Lisp_Object *args) |
068a9dbd | 9970 | { |
df7492f9 KH |
9971 | Lisp_Object name; |
9972 | Lisp_Object spec_vec; /* [ ATTRS ALIASE EOL_TYPE ] */ | |
9973 | Lisp_Object attrs; /* Vector of attributes. */ | |
9974 | Lisp_Object eol_type; | |
9975 | Lisp_Object aliases; | |
9976 | Lisp_Object coding_type, charset_list, safe_charsets; | |
9977 | enum coding_category category; | |
9978 | Lisp_Object tail, val; | |
9979 | int max_charset_id = 0; | |
9980 | int i; | |
068a9dbd | 9981 | |
df7492f9 KH |
9982 | if (nargs < coding_arg_max) |
9983 | goto short_args; | |
068a9dbd | 9984 | |
df7492f9 | 9985 | attrs = Fmake_vector (make_number (coding_attr_last_index), Qnil); |
068a9dbd | 9986 | |
df7492f9 KH |
9987 | name = args[coding_arg_name]; |
9988 | CHECK_SYMBOL (name); | |
4939150c | 9989 | ASET (attrs, coding_attr_base_name, name); |
068a9dbd | 9990 | |
df7492f9 KH |
9991 | val = args[coding_arg_mnemonic]; |
9992 | if (! STRINGP (val)) | |
9993 | CHECK_CHARACTER (val); | |
4939150c | 9994 | ASET (attrs, coding_attr_mnemonic, val); |
068a9dbd | 9995 | |
df7492f9 KH |
9996 | coding_type = args[coding_arg_coding_type]; |
9997 | CHECK_SYMBOL (coding_type); | |
4939150c | 9998 | ASET (attrs, coding_attr_type, coding_type); |
068a9dbd | 9999 | |
df7492f9 KH |
10000 | charset_list = args[coding_arg_charset_list]; |
10001 | if (SYMBOLP (charset_list)) | |
10002 | { | |
10003 | if (EQ (charset_list, Qiso_2022)) | |
10004 | { | |
10005 | if (! EQ (coding_type, Qiso_2022)) | |
10006 | error ("Invalid charset-list"); | |
10007 | charset_list = Viso_2022_charset_list; | |
10008 | } | |
10009 | else if (EQ (charset_list, Qemacs_mule)) | |
10010 | { | |
10011 | if (! EQ (coding_type, Qemacs_mule)) | |
10012 | error ("Invalid charset-list"); | |
10013 | charset_list = Vemacs_mule_charset_list; | |
10014 | } | |
10015 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) | |
d311d28c PE |
10016 | { |
10017 | if (! RANGED_INTEGERP (0, XCAR (tail), INT_MAX - 1)) | |
10018 | error ("Invalid charset-list"); | |
10019 | if (max_charset_id < XFASTINT (XCAR (tail))) | |
10020 | max_charset_id = XFASTINT (XCAR (tail)); | |
10021 | } | |
df7492f9 | 10022 | } |
068a9dbd KH |
10023 | else |
10024 | { | |
df7492f9 | 10025 | charset_list = Fcopy_sequence (charset_list); |
985773c9 | 10026 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) |
068a9dbd | 10027 | { |
df7492f9 KH |
10028 | struct charset *charset; |
10029 | ||
985773c9 | 10030 | val = XCAR (tail); |
df7492f9 KH |
10031 | CHECK_CHARSET_GET_CHARSET (val, charset); |
10032 | if (EQ (coding_type, Qiso_2022) | |
10033 | ? CHARSET_ISO_FINAL (charset) < 0 | |
10034 | : EQ (coding_type, Qemacs_mule) | |
10035 | ? CHARSET_EMACS_MULE_ID (charset) < 0 | |
10036 | : 0) | |
10037 | error ("Can't handle charset `%s'", | |
8f924df7 | 10038 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
df7492f9 | 10039 | |
8f924df7 | 10040 | XSETCAR (tail, make_number (charset->id)); |
df7492f9 KH |
10041 | if (max_charset_id < charset->id) |
10042 | max_charset_id = charset->id; | |
068a9dbd KH |
10043 | } |
10044 | } | |
4939150c | 10045 | ASET (attrs, coding_attr_charset_list, charset_list); |
068a9dbd | 10046 | |
1b3b981b AS |
10047 | safe_charsets = make_uninit_string (max_charset_id + 1); |
10048 | memset (SDATA (safe_charsets), 255, max_charset_id + 1); | |
df7492f9 | 10049 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) |
8f924df7 | 10050 | SSET (safe_charsets, XFASTINT (XCAR (tail)), 0); |
4939150c | 10051 | ASET (attrs, coding_attr_safe_charsets, safe_charsets); |
068a9dbd | 10052 | |
4939150c | 10053 | ASET (attrs, coding_attr_ascii_compat, args[coding_arg_ascii_compatible_p]); |
3a73fa5d | 10054 | |
df7492f9 | 10055 | val = args[coding_arg_decode_translation_table]; |
a6f87d34 | 10056 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) |
7d64c6ad | 10057 | CHECK_SYMBOL (val); |
4939150c | 10058 | ASET (attrs, coding_attr_decode_tbl, val); |
3a73fa5d | 10059 | |
df7492f9 | 10060 | val = args[coding_arg_encode_translation_table]; |
a6f87d34 | 10061 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) |
7d64c6ad | 10062 | CHECK_SYMBOL (val); |
4939150c | 10063 | ASET (attrs, coding_attr_encode_tbl, val); |
d46c5b12 | 10064 | |
df7492f9 KH |
10065 | val = args[coding_arg_post_read_conversion]; |
10066 | CHECK_SYMBOL (val); | |
4939150c | 10067 | ASET (attrs, coding_attr_post_read, val); |
d46c5b12 | 10068 | |
df7492f9 KH |
10069 | val = args[coding_arg_pre_write_conversion]; |
10070 | CHECK_SYMBOL (val); | |
4939150c | 10071 | ASET (attrs, coding_attr_pre_write, val); |
3a73fa5d | 10072 | |
df7492f9 KH |
10073 | val = args[coding_arg_default_char]; |
10074 | if (NILP (val)) | |
4939150c | 10075 | ASET (attrs, coding_attr_default_char, make_number (' ')); |
df7492f9 KH |
10076 | else |
10077 | { | |
8f924df7 | 10078 | CHECK_CHARACTER (val); |
4939150c | 10079 | ASET (attrs, coding_attr_default_char, val); |
df7492f9 | 10080 | } |
4031e2bf | 10081 | |
8f924df7 | 10082 | val = args[coding_arg_for_unibyte]; |
4939150c | 10083 | ASET (attrs, coding_attr_for_unibyte, NILP (val) ? Qnil : Qt); |
3a73fa5d | 10084 | |
df7492f9 KH |
10085 | val = args[coding_arg_plist]; |
10086 | CHECK_LIST (val); | |
4939150c | 10087 | ASET (attrs, coding_attr_plist, val); |
3a73fa5d | 10088 | |
df7492f9 KH |
10089 | if (EQ (coding_type, Qcharset)) |
10090 | { | |
c7c66a95 KH |
10091 | /* Generate a lisp vector of 256 elements. Each element is nil, |
10092 | integer, or a list of charset IDs. | |
3a73fa5d | 10093 | |
c7c66a95 KH |
10094 | If Nth element is nil, the byte code N is invalid in this |
10095 | coding system. | |
4ed46869 | 10096 | |
c7c66a95 KH |
10097 | If Nth element is a number NUM, N is the first byte of a |
10098 | charset whose ID is NUM. | |
4ed46869 | 10099 | |
c7c66a95 KH |
10100 | If Nth element is a list of charset IDs, N is the first byte |
10101 | of one of them. The list is sorted by dimensions of the | |
ad1746f5 | 10102 | charsets. A charset of smaller dimension comes first. */ |
df7492f9 | 10103 | val = Fmake_vector (make_number (256), Qnil); |
4ed46869 | 10104 | |
5c99c2e6 | 10105 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) |
df7492f9 | 10106 | { |
c7c66a95 KH |
10107 | struct charset *charset = CHARSET_FROM_ID (XFASTINT (XCAR (tail))); |
10108 | int dim = CHARSET_DIMENSION (charset); | |
10109 | int idx = (dim - 1) * 4; | |
4ed46869 | 10110 | |
5c99c2e6 | 10111 | if (CHARSET_ASCII_COMPATIBLE_P (charset)) |
4939150c | 10112 | ASET (attrs, coding_attr_ascii_compat, Qt); |
4031e2bf | 10113 | |
15d143f7 KH |
10114 | for (i = charset->code_space[idx]; |
10115 | i <= charset->code_space[idx + 1]; i++) | |
10116 | { | |
c7c66a95 KH |
10117 | Lisp_Object tmp, tmp2; |
10118 | int dim2; | |
ec6d2bb8 | 10119 | |
c7c66a95 KH |
10120 | tmp = AREF (val, i); |
10121 | if (NILP (tmp)) | |
10122 | tmp = XCAR (tail); | |
10123 | else if (NUMBERP (tmp)) | |
10124 | { | |
10125 | dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp))); | |
10126 | if (dim < dim2) | |
6c6f1994 | 10127 | tmp = list2 (XCAR (tail), tmp); |
f9d71dcd | 10128 | else |
6c6f1994 | 10129 | tmp = list2 (tmp, XCAR (tail)); |
c7c66a95 | 10130 | } |
15d143f7 | 10131 | else |
c7c66a95 KH |
10132 | { |
10133 | for (tmp2 = tmp; CONSP (tmp2); tmp2 = XCDR (tmp2)) | |
10134 | { | |
10135 | dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2)))); | |
10136 | if (dim < dim2) | |
10137 | break; | |
10138 | } | |
10139 | if (NILP (tmp2)) | |
6c6f1994 | 10140 | tmp = nconc2 (tmp, list1 (XCAR (tail))); |
c7c66a95 KH |
10141 | else |
10142 | { | |
10143 | XSETCDR (tmp2, Fcons (XCAR (tmp2), XCDR (tmp2))); | |
10144 | XSETCAR (tmp2, XCAR (tail)); | |
10145 | } | |
10146 | } | |
10147 | ASET (val, i, tmp); | |
15d143f7 | 10148 | } |
df7492f9 KH |
10149 | } |
10150 | ASET (attrs, coding_attr_charset_valids, val); | |
10151 | category = coding_category_charset; | |
10152 | } | |
10153 | else if (EQ (coding_type, Qccl)) | |
10154 | { | |
10155 | Lisp_Object valids; | |
ecec61c1 | 10156 | |
df7492f9 KH |
10157 | if (nargs < coding_arg_ccl_max) |
10158 | goto short_args; | |
ecec61c1 | 10159 | |
df7492f9 KH |
10160 | val = args[coding_arg_ccl_decoder]; |
10161 | CHECK_CCL_PROGRAM (val); | |
10162 | if (VECTORP (val)) | |
10163 | val = Fcopy_sequence (val); | |
10164 | ASET (attrs, coding_attr_ccl_decoder, val); | |
ecec61c1 | 10165 | |
df7492f9 KH |
10166 | val = args[coding_arg_ccl_encoder]; |
10167 | CHECK_CCL_PROGRAM (val); | |
10168 | if (VECTORP (val)) | |
10169 | val = Fcopy_sequence (val); | |
10170 | ASET (attrs, coding_attr_ccl_encoder, val); | |
ecec61c1 | 10171 | |
df7492f9 KH |
10172 | val = args[coding_arg_ccl_valids]; |
10173 | valids = Fmake_string (make_number (256), make_number (0)); | |
7d7bbefd | 10174 | for (tail = val; CONSP (tail); tail = XCDR (tail)) |
df7492f9 | 10175 | { |
8dcbea82 | 10176 | int from, to; |
ecec61c1 | 10177 | |
34348bd4 | 10178 | val = XCAR (tail); |
df7492f9 | 10179 | if (INTEGERP (val)) |
8dcbea82 | 10180 | { |
d311d28c | 10181 | if (! (0 <= XINT (val) && XINT (val) <= 255)) |
8dcbea82 | 10182 | args_out_of_range_3 (val, make_number (0), make_number (255)); |
d311d28c | 10183 | from = to = XINT (val); |
8dcbea82 | 10184 | } |
df7492f9 KH |
10185 | else |
10186 | { | |
df7492f9 | 10187 | CHECK_CONS (val); |
8f924df7 | 10188 | CHECK_NATNUM_CAR (val); |
d311d28c PE |
10189 | CHECK_NUMBER_CDR (val); |
10190 | if (XINT (XCAR (val)) > 255) | |
8dcbea82 KH |
10191 | args_out_of_range_3 (XCAR (val), |
10192 | make_number (0), make_number (255)); | |
d311d28c PE |
10193 | from = XINT (XCAR (val)); |
10194 | if (! (from <= XINT (XCDR (val)) && XINT (XCDR (val)) <= 255)) | |
8dcbea82 KH |
10195 | args_out_of_range_3 (XCDR (val), |
10196 | XCAR (val), make_number (255)); | |
d311d28c | 10197 | to = XINT (XCDR (val)); |
df7492f9 | 10198 | } |
8dcbea82 | 10199 | for (i = from; i <= to; i++) |
8f924df7 | 10200 | SSET (valids, i, 1); |
df7492f9 KH |
10201 | } |
10202 | ASET (attrs, coding_attr_ccl_valids, valids); | |
4ed46869 | 10203 | |
df7492f9 | 10204 | category = coding_category_ccl; |
55ab7be3 | 10205 | } |
df7492f9 | 10206 | else if (EQ (coding_type, Qutf_16)) |
55ab7be3 | 10207 | { |
df7492f9 | 10208 | Lisp_Object bom, endian; |
4ed46869 | 10209 | |
4939150c | 10210 | ASET (attrs, coding_attr_ascii_compat, Qnil); |
4ed46869 | 10211 | |
df7492f9 KH |
10212 | if (nargs < coding_arg_utf16_max) |
10213 | goto short_args; | |
4ed46869 | 10214 | |
df7492f9 KH |
10215 | bom = args[coding_arg_utf16_bom]; |
10216 | if (! NILP (bom) && ! EQ (bom, Qt)) | |
10217 | { | |
10218 | CHECK_CONS (bom); | |
8f924df7 KH |
10219 | val = XCAR (bom); |
10220 | CHECK_CODING_SYSTEM (val); | |
10221 | val = XCDR (bom); | |
10222 | CHECK_CODING_SYSTEM (val); | |
df7492f9 | 10223 | } |
a470d443 | 10224 | ASET (attrs, coding_attr_utf_bom, bom); |
df7492f9 KH |
10225 | |
10226 | endian = args[coding_arg_utf16_endian]; | |
b49a1807 KH |
10227 | CHECK_SYMBOL (endian); |
10228 | if (NILP (endian)) | |
10229 | endian = Qbig; | |
10230 | else if (! EQ (endian, Qbig) && ! EQ (endian, Qlittle)) | |
8f924df7 | 10231 | error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian))); |
df7492f9 KH |
10232 | ASET (attrs, coding_attr_utf_16_endian, endian); |
10233 | ||
10234 | category = (CONSP (bom) | |
10235 | ? coding_category_utf_16_auto | |
10236 | : NILP (bom) | |
b49a1807 | 10237 | ? (EQ (endian, Qbig) |
df7492f9 KH |
10238 | ? coding_category_utf_16_be_nosig |
10239 | : coding_category_utf_16_le_nosig) | |
b49a1807 | 10240 | : (EQ (endian, Qbig) |
df7492f9 KH |
10241 | ? coding_category_utf_16_be |
10242 | : coding_category_utf_16_le)); | |
10243 | } | |
10244 | else if (EQ (coding_type, Qiso_2022)) | |
10245 | { | |
10246 | Lisp_Object initial, reg_usage, request, flags; | |
1397dc18 | 10247 | |
df7492f9 KH |
10248 | if (nargs < coding_arg_iso2022_max) |
10249 | goto short_args; | |
10250 | ||
10251 | initial = Fcopy_sequence (args[coding_arg_iso2022_initial]); | |
10252 | CHECK_VECTOR (initial); | |
10253 | for (i = 0; i < 4; i++) | |
10254 | { | |
9a9d91d9 | 10255 | val = AREF (initial, i); |
df7492f9 KH |
10256 | if (! NILP (val)) |
10257 | { | |
584948ac KH |
10258 | struct charset *charset; |
10259 | ||
10260 | CHECK_CHARSET_GET_CHARSET (val, charset); | |
10261 | ASET (initial, i, make_number (CHARSET_ID (charset))); | |
10262 | if (i == 0 && CHARSET_ASCII_COMPATIBLE_P (charset)) | |
4939150c | 10263 | ASET (attrs, coding_attr_ascii_compat, Qt); |
df7492f9 KH |
10264 | } |
10265 | else | |
10266 | ASET (initial, i, make_number (-1)); | |
10267 | } | |
10268 | ||
10269 | reg_usage = args[coding_arg_iso2022_reg_usage]; | |
10270 | CHECK_CONS (reg_usage); | |
8f924df7 KH |
10271 | CHECK_NUMBER_CAR (reg_usage); |
10272 | CHECK_NUMBER_CDR (reg_usage); | |
df7492f9 KH |
10273 | |
10274 | request = Fcopy_sequence (args[coding_arg_iso2022_request]); | |
7d7bbefd | 10275 | for (tail = request; CONSP (tail); tail = XCDR (tail)) |
1397dc18 | 10276 | { |
df7492f9 | 10277 | int id; |
2735d060 | 10278 | Lisp_Object tmp1; |
df7492f9 | 10279 | |
34348bd4 | 10280 | val = XCAR (tail); |
df7492f9 | 10281 | CHECK_CONS (val); |
2735d060 PE |
10282 | tmp1 = XCAR (val); |
10283 | CHECK_CHARSET_GET_ID (tmp1, id); | |
8f924df7 | 10284 | CHECK_NATNUM_CDR (val); |
df7492f9 | 10285 | if (XINT (XCDR (val)) >= 4) |
c2982e87 | 10286 | error ("Invalid graphic register number: %"pI"d", XINT (XCDR (val))); |
8f924df7 | 10287 | XSETCAR (val, make_number (id)); |
1397dc18 | 10288 | } |
4ed46869 | 10289 | |
df7492f9 KH |
10290 | flags = args[coding_arg_iso2022_flags]; |
10291 | CHECK_NATNUM (flags); | |
d311d28c | 10292 | i = XINT (flags) & INT_MAX; |
df7492f9 | 10293 | if (EQ (args[coding_arg_charset_list], Qiso_2022)) |
d311d28c PE |
10294 | i |= CODING_ISO_FLAG_FULL_SUPPORT; |
10295 | flags = make_number (i); | |
df7492f9 KH |
10296 | |
10297 | ASET (attrs, coding_attr_iso_initial, initial); | |
10298 | ASET (attrs, coding_attr_iso_usage, reg_usage); | |
10299 | ASET (attrs, coding_attr_iso_request, request); | |
10300 | ASET (attrs, coding_attr_iso_flags, flags); | |
10301 | setup_iso_safe_charsets (attrs); | |
10302 | ||
10303 | if (i & CODING_ISO_FLAG_SEVEN_BITS) | |
10304 | category = ((i & (CODING_ISO_FLAG_LOCKING_SHIFT | |
10305 | | CODING_ISO_FLAG_SINGLE_SHIFT)) | |
10306 | ? coding_category_iso_7_else | |
10307 | : EQ (args[coding_arg_charset_list], Qiso_2022) | |
10308 | ? coding_category_iso_7 | |
10309 | : coding_category_iso_7_tight); | |
10310 | else | |
10311 | { | |
10312 | int id = XINT (AREF (initial, 1)); | |
10313 | ||
c6fb6e98 | 10314 | category = (((i & CODING_ISO_FLAG_LOCKING_SHIFT) |
df7492f9 KH |
10315 | || EQ (args[coding_arg_charset_list], Qiso_2022) |
10316 | || id < 0) | |
10317 | ? coding_category_iso_8_else | |
10318 | : (CHARSET_DIMENSION (CHARSET_FROM_ID (id)) == 1) | |
10319 | ? coding_category_iso_8_1 | |
10320 | : coding_category_iso_8_2); | |
10321 | } | |
0ce7886f KH |
10322 | if (category != coding_category_iso_8_1 |
10323 | && category != coding_category_iso_8_2) | |
4939150c | 10324 | ASET (attrs, coding_attr_ascii_compat, Qnil); |
df7492f9 KH |
10325 | } |
10326 | else if (EQ (coding_type, Qemacs_mule)) | |
c28a9453 | 10327 | { |
df7492f9 KH |
10328 | if (EQ (args[coding_arg_charset_list], Qemacs_mule)) |
10329 | ASET (attrs, coding_attr_emacs_mule_full, Qt); | |
4939150c | 10330 | ASET (attrs, coding_attr_ascii_compat, Qt); |
df7492f9 | 10331 | category = coding_category_emacs_mule; |
c28a9453 | 10332 | } |
df7492f9 | 10333 | else if (EQ (coding_type, Qshift_jis)) |
c28a9453 | 10334 | { |
df7492f9 KH |
10335 | |
10336 | struct charset *charset; | |
10337 | ||
7d64c6ad | 10338 | if (XINT (Flength (charset_list)) != 3 |
6e07c25f | 10339 | && XINT (Flength (charset_list)) != 4) |
7d64c6ad | 10340 | error ("There should be three or four charsets"); |
df7492f9 KH |
10341 | |
10342 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
10343 | if (CHARSET_DIMENSION (charset) != 1) | |
10344 | error ("Dimension of charset %s is not one", | |
8f924df7 | 10345 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
584948ac | 10346 | if (CHARSET_ASCII_COMPATIBLE_P (charset)) |
4939150c | 10347 | ASET (attrs, coding_attr_ascii_compat, Qt); |
df7492f9 KH |
10348 | |
10349 | charset_list = XCDR (charset_list); | |
10350 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
10351 | if (CHARSET_DIMENSION (charset) != 1) | |
10352 | error ("Dimension of charset %s is not one", | |
8f924df7 | 10353 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
df7492f9 KH |
10354 | |
10355 | charset_list = XCDR (charset_list); | |
10356 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
10357 | if (CHARSET_DIMENSION (charset) != 2) | |
7d64c6ad KH |
10358 | error ("Dimension of charset %s is not two", |
10359 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); | |
10360 | ||
10361 | charset_list = XCDR (charset_list); | |
2b917a06 KH |
10362 | if (! NILP (charset_list)) |
10363 | { | |
10364 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
10365 | if (CHARSET_DIMENSION (charset) != 2) | |
10366 | error ("Dimension of charset %s is not two", | |
10367 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); | |
10368 | } | |
df7492f9 KH |
10369 | |
10370 | category = coding_category_sjis; | |
10371 | Vsjis_coding_system = name; | |
c28a9453 | 10372 | } |
df7492f9 KH |
10373 | else if (EQ (coding_type, Qbig5)) |
10374 | { | |
10375 | struct charset *charset; | |
4ed46869 | 10376 | |
df7492f9 KH |
10377 | if (XINT (Flength (charset_list)) != 2) |
10378 | error ("There should be just two charsets"); | |
10379 | ||
10380 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
10381 | if (CHARSET_DIMENSION (charset) != 1) | |
10382 | error ("Dimension of charset %s is not one", | |
8f924df7 | 10383 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
584948ac | 10384 | if (CHARSET_ASCII_COMPATIBLE_P (charset)) |
4939150c | 10385 | ASET (attrs, coding_attr_ascii_compat, Qt); |
df7492f9 KH |
10386 | |
10387 | charset_list = XCDR (charset_list); | |
10388 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
10389 | if (CHARSET_DIMENSION (charset) != 2) | |
10390 | error ("Dimension of charset %s is not two", | |
8f924df7 | 10391 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
4ed46869 | 10392 | |
df7492f9 KH |
10393 | category = coding_category_big5; |
10394 | Vbig5_coding_system = name; | |
10395 | } | |
10396 | else if (EQ (coding_type, Qraw_text)) | |
c28a9453 | 10397 | { |
584948ac | 10398 | category = coding_category_raw_text; |
4939150c | 10399 | ASET (attrs, coding_attr_ascii_compat, Qt); |
c28a9453 | 10400 | } |
df7492f9 | 10401 | else if (EQ (coding_type, Qutf_8)) |
4ed46869 | 10402 | { |
a470d443 KH |
10403 | Lisp_Object bom; |
10404 | ||
a470d443 KH |
10405 | if (nargs < coding_arg_utf8_max) |
10406 | goto short_args; | |
10407 | ||
10408 | bom = args[coding_arg_utf8_bom]; | |
10409 | if (! NILP (bom) && ! EQ (bom, Qt)) | |
10410 | { | |
10411 | CHECK_CONS (bom); | |
10412 | val = XCAR (bom); | |
10413 | CHECK_CODING_SYSTEM (val); | |
10414 | val = XCDR (bom); | |
10415 | CHECK_CODING_SYSTEM (val); | |
10416 | } | |
10417 | ASET (attrs, coding_attr_utf_bom, bom); | |
0e5317f7 | 10418 | if (NILP (bom)) |
4939150c | 10419 | ASET (attrs, coding_attr_ascii_compat, Qt); |
a470d443 KH |
10420 | |
10421 | category = (CONSP (bom) ? coding_category_utf_8_auto | |
10422 | : NILP (bom) ? coding_category_utf_8_nosig | |
10423 | : coding_category_utf_8_sig); | |
4ed46869 | 10424 | } |
df7492f9 | 10425 | else if (EQ (coding_type, Qundecided)) |
c3a7b3f4 KH |
10426 | { |
10427 | if (nargs < coding_arg_undecided_max) | |
10428 | goto short_args; | |
10429 | ASET (attrs, coding_attr_undecided_inhibit_null_byte_detection, | |
10430 | args[coding_arg_undecided_inhibit_null_byte_detection]); | |
10431 | ASET (attrs, coding_attr_undecided_inhibit_iso_escape_detection, | |
10432 | args[coding_arg_undecided_inhibit_iso_escape_detection]); | |
10433 | ASET (attrs, coding_attr_undecided_prefer_utf_8, | |
10434 | args[coding_arg_undecided_prefer_utf_8]); | |
10435 | category = coding_category_undecided; | |
10436 | } | |
4ed46869 | 10437 | else |
df7492f9 | 10438 | error ("Invalid coding system type: %s", |
8f924df7 | 10439 | SDATA (SYMBOL_NAME (coding_type))); |
4ed46869 | 10440 | |
4939150c PE |
10441 | ASET (attrs, coding_attr_category, make_number (category)); |
10442 | ASET (attrs, coding_attr_plist, | |
10443 | Fcons (QCcategory, | |
10444 | Fcons (AREF (Vcoding_category_table, category), | |
10445 | CODING_ATTR_PLIST (attrs)))); | |
10446 | ASET (attrs, coding_attr_plist, | |
10447 | Fcons (QCascii_compatible_p, | |
10448 | Fcons (CODING_ATTR_ASCII_COMPAT (attrs), | |
10449 | CODING_ATTR_PLIST (attrs)))); | |
c4825358 | 10450 | |
df7492f9 KH |
10451 | eol_type = args[coding_arg_eol_type]; |
10452 | if (! NILP (eol_type) | |
10453 | && ! EQ (eol_type, Qunix) | |
10454 | && ! EQ (eol_type, Qdos) | |
10455 | && ! EQ (eol_type, Qmac)) | |
10456 | error ("Invalid eol-type"); | |
4ed46869 | 10457 | |
6c6f1994 | 10458 | aliases = list1 (name); |
4ed46869 | 10459 | |
df7492f9 KH |
10460 | if (NILP (eol_type)) |
10461 | { | |
10462 | eol_type = make_subsidiaries (name); | |
10463 | for (i = 0; i < 3; i++) | |
1397dc18 | 10464 | { |
df7492f9 KH |
10465 | Lisp_Object this_spec, this_name, this_aliases, this_eol_type; |
10466 | ||
10467 | this_name = AREF (eol_type, i); | |
6c6f1994 | 10468 | this_aliases = list1 (this_name); |
df7492f9 | 10469 | this_eol_type = (i == 0 ? Qunix : i == 1 ? Qdos : Qmac); |
25721f5b DA |
10470 | this_spec = make_uninit_vector (3); |
10471 | ASET (this_spec, 0, attrs); | |
df7492f9 KH |
10472 | ASET (this_spec, 1, this_aliases); |
10473 | ASET (this_spec, 2, this_eol_type); | |
10474 | Fputhash (this_name, this_spec, Vcoding_system_hash_table); | |
10475 | Vcoding_system_list = Fcons (this_name, Vcoding_system_list); | |
583f71ca KH |
10476 | val = Fassoc (Fsymbol_name (this_name), Vcoding_system_alist); |
10477 | if (NILP (val)) | |
10478 | Vcoding_system_alist | |
10479 | = Fcons (Fcons (Fsymbol_name (this_name), Qnil), | |
10480 | Vcoding_system_alist); | |
1397dc18 | 10481 | } |
d46c5b12 | 10482 | } |
4ed46869 | 10483 | |
25721f5b DA |
10484 | spec_vec = make_uninit_vector (3); |
10485 | ASET (spec_vec, 0, attrs); | |
df7492f9 KH |
10486 | ASET (spec_vec, 1, aliases); |
10487 | ASET (spec_vec, 2, eol_type); | |
48b0f3ae | 10488 | |
df7492f9 KH |
10489 | Fputhash (name, spec_vec, Vcoding_system_hash_table); |
10490 | Vcoding_system_list = Fcons (name, Vcoding_system_list); | |
583f71ca KH |
10491 | val = Fassoc (Fsymbol_name (name), Vcoding_system_alist); |
10492 | if (NILP (val)) | |
10493 | Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (name), Qnil), | |
10494 | Vcoding_system_alist); | |
48b0f3ae | 10495 | |
df7492f9 KH |
10496 | { |
10497 | int id = coding_categories[category].id; | |
48b0f3ae | 10498 | |
df7492f9 KH |
10499 | if (id < 0 || EQ (name, CODING_ID_NAME (id))) |
10500 | setup_coding_system (name, &coding_categories[category]); | |
10501 | } | |
48b0f3ae | 10502 | |
d46c5b12 | 10503 | return Qnil; |
48b0f3ae | 10504 | |
df7492f9 KH |
10505 | short_args: |
10506 | return Fsignal (Qwrong_number_of_arguments, | |
10507 | Fcons (intern ("define-coding-system-internal"), | |
10508 | make_number (nargs))); | |
d46c5b12 | 10509 | } |
4ed46869 | 10510 | |
d6925f38 | 10511 | |
a6f87d34 KH |
10512 | DEFUN ("coding-system-put", Fcoding_system_put, Scoding_system_put, |
10513 | 3, 3, 0, | |
10514 | doc: /* Change value in CODING-SYSTEM's property list PROP to VAL. */) | |
5842a27b | 10515 | (Lisp_Object coding_system, Lisp_Object prop, Lisp_Object val) |
a6f87d34 | 10516 | { |
3dbe7859 | 10517 | Lisp_Object spec, attrs; |
a6f87d34 KH |
10518 | |
10519 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
10520 | attrs = AREF (spec, 0); | |
10521 | if (EQ (prop, QCmnemonic)) | |
10522 | { | |
10523 | if (! STRINGP (val)) | |
10524 | CHECK_CHARACTER (val); | |
4939150c | 10525 | ASET (attrs, coding_attr_mnemonic, val); |
a6f87d34 | 10526 | } |
2133e2d1 | 10527 | else if (EQ (prop, QCdefault_char)) |
a6f87d34 KH |
10528 | { |
10529 | if (NILP (val)) | |
10530 | val = make_number (' '); | |
10531 | else | |
10532 | CHECK_CHARACTER (val); | |
4939150c | 10533 | ASET (attrs, coding_attr_default_char, val); |
a6f87d34 KH |
10534 | } |
10535 | else if (EQ (prop, QCdecode_translation_table)) | |
10536 | { | |
10537 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) | |
10538 | CHECK_SYMBOL (val); | |
4939150c | 10539 | ASET (attrs, coding_attr_decode_tbl, val); |
a6f87d34 KH |
10540 | } |
10541 | else if (EQ (prop, QCencode_translation_table)) | |
10542 | { | |
10543 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) | |
10544 | CHECK_SYMBOL (val); | |
4939150c | 10545 | ASET (attrs, coding_attr_encode_tbl, val); |
a6f87d34 KH |
10546 | } |
10547 | else if (EQ (prop, QCpost_read_conversion)) | |
10548 | { | |
10549 | CHECK_SYMBOL (val); | |
4939150c | 10550 | ASET (attrs, coding_attr_post_read, val); |
a6f87d34 KH |
10551 | } |
10552 | else if (EQ (prop, QCpre_write_conversion)) | |
10553 | { | |
10554 | CHECK_SYMBOL (val); | |
4939150c | 10555 | ASET (attrs, coding_attr_pre_write, val); |
a6f87d34 | 10556 | } |
35befdaa KH |
10557 | else if (EQ (prop, QCascii_compatible_p)) |
10558 | { | |
4939150c | 10559 | ASET (attrs, coding_attr_ascii_compat, val); |
35befdaa | 10560 | } |
a6f87d34 | 10561 | |
4939150c PE |
10562 | ASET (attrs, coding_attr_plist, |
10563 | Fplist_put (CODING_ATTR_PLIST (attrs), prop, val)); | |
a6f87d34 KH |
10564 | return val; |
10565 | } | |
10566 | ||
10567 | ||
df7492f9 KH |
10568 | DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias, |
10569 | Sdefine_coding_system_alias, 2, 2, 0, | |
10570 | doc: /* Define ALIAS as an alias for CODING-SYSTEM. */) | |
5842a27b | 10571 | (Lisp_Object alias, Lisp_Object coding_system) |
66cfb530 | 10572 | { |
583f71ca | 10573 | Lisp_Object spec, aliases, eol_type, val; |
4ed46869 | 10574 | |
df7492f9 KH |
10575 | CHECK_SYMBOL (alias); |
10576 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
10577 | aliases = AREF (spec, 1); | |
d4a1d553 | 10578 | /* ALIASES should be a list of length more than zero, and the first |
d6925f38 KH |
10579 | element is a base coding system. Append ALIAS at the tail of the |
10580 | list. */ | |
df7492f9 KH |
10581 | while (!NILP (XCDR (aliases))) |
10582 | aliases = XCDR (aliases); | |
6c6f1994 | 10583 | XSETCDR (aliases, list1 (alias)); |
4ed46869 | 10584 | |
df7492f9 KH |
10585 | eol_type = AREF (spec, 2); |
10586 | if (VECTORP (eol_type)) | |
4ed46869 | 10587 | { |
df7492f9 KH |
10588 | Lisp_Object subsidiaries; |
10589 | int i; | |
4ed46869 | 10590 | |
df7492f9 KH |
10591 | subsidiaries = make_subsidiaries (alias); |
10592 | for (i = 0; i < 3; i++) | |
10593 | Fdefine_coding_system_alias (AREF (subsidiaries, i), | |
10594 | AREF (eol_type, i)); | |
4ed46869 | 10595 | } |
df7492f9 KH |
10596 | |
10597 | Fputhash (alias, spec, Vcoding_system_hash_table); | |
d6925f38 | 10598 | Vcoding_system_list = Fcons (alias, Vcoding_system_list); |
583f71ca KH |
10599 | val = Fassoc (Fsymbol_name (alias), Vcoding_system_alist); |
10600 | if (NILP (val)) | |
10601 | Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (alias), Qnil), | |
10602 | Vcoding_system_alist); | |
66cfb530 | 10603 | |
4ed46869 KH |
10604 | return Qnil; |
10605 | } | |
10606 | ||
a7ca3326 | 10607 | DEFUN ("coding-system-base", Fcoding_system_base, Scoding_system_base, |
df7492f9 KH |
10608 | 1, 1, 0, |
10609 | doc: /* Return the base of CODING-SYSTEM. | |
da7db224 | 10610 | Any alias or subsidiary coding system is not a base coding system. */) |
5842a27b | 10611 | (Lisp_Object coding_system) |
d46c5b12 | 10612 | { |
df7492f9 | 10613 | Lisp_Object spec, attrs; |
d46c5b12 | 10614 | |
df7492f9 KH |
10615 | if (NILP (coding_system)) |
10616 | return (Qno_conversion); | |
10617 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
10618 | attrs = AREF (spec, 0); | |
10619 | return CODING_ATTR_BASE_NAME (attrs); | |
10620 | } | |
1397dc18 | 10621 | |
df7492f9 KH |
10622 | DEFUN ("coding-system-plist", Fcoding_system_plist, Scoding_system_plist, |
10623 | 1, 1, 0, | |
10624 | doc: "Return the property list of CODING-SYSTEM.") | |
5842a27b | 10625 | (Lisp_Object coding_system) |
df7492f9 KH |
10626 | { |
10627 | Lisp_Object spec, attrs; | |
1397dc18 | 10628 | |
df7492f9 KH |
10629 | if (NILP (coding_system)) |
10630 | coding_system = Qno_conversion; | |
10631 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
10632 | attrs = AREF (spec, 0); | |
10633 | return CODING_ATTR_PLIST (attrs); | |
d46c5b12 KH |
10634 | } |
10635 | ||
df7492f9 KH |
10636 | |
10637 | DEFUN ("coding-system-aliases", Fcoding_system_aliases, Scoding_system_aliases, | |
10638 | 1, 1, 0, | |
da7db224 | 10639 | doc: /* Return the list of aliases of CODING-SYSTEM. */) |
5842a27b | 10640 | (Lisp_Object coding_system) |
66cfb530 | 10641 | { |
df7492f9 | 10642 | Lisp_Object spec; |
84d60297 | 10643 | |
df7492f9 KH |
10644 | if (NILP (coding_system)) |
10645 | coding_system = Qno_conversion; | |
10646 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
da7db224 | 10647 | return AREF (spec, 1); |
df7492f9 | 10648 | } |
66cfb530 | 10649 | |
a7ca3326 | 10650 | DEFUN ("coding-system-eol-type", Fcoding_system_eol_type, |
df7492f9 KH |
10651 | Scoding_system_eol_type, 1, 1, 0, |
10652 | doc: /* Return eol-type of CODING-SYSTEM. | |
d4a1d553 | 10653 | An eol-type is an integer 0, 1, 2, or a vector of coding systems. |
66cfb530 | 10654 | |
df7492f9 KH |
10655 | Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF, |
10656 | and CR respectively. | |
66cfb530 | 10657 | |
df7492f9 KH |
10658 | A vector value indicates that a format of end-of-line should be |
10659 | detected automatically. Nth element of the vector is the subsidiary | |
10660 | coding system whose eol-type is N. */) | |
5842a27b | 10661 | (Lisp_Object coding_system) |
6b89e3aa | 10662 | { |
df7492f9 KH |
10663 | Lisp_Object spec, eol_type; |
10664 | int n; | |
6b89e3aa | 10665 | |
df7492f9 KH |
10666 | if (NILP (coding_system)) |
10667 | coding_system = Qno_conversion; | |
10668 | if (! CODING_SYSTEM_P (coding_system)) | |
10669 | return Qnil; | |
10670 | spec = CODING_SYSTEM_SPEC (coding_system); | |
10671 | eol_type = AREF (spec, 2); | |
10672 | if (VECTORP (eol_type)) | |
10673 | return Fcopy_sequence (eol_type); | |
10674 | n = EQ (eol_type, Qunix) ? 0 : EQ (eol_type, Qdos) ? 1 : 2; | |
10675 | return make_number (n); | |
6b89e3aa KH |
10676 | } |
10677 | ||
4ed46869 KH |
10678 | #endif /* emacs */ |
10679 | ||
10680 | \f | |
1397dc18 | 10681 | /*** 9. Post-amble ***/ |
4ed46869 | 10682 | |
dfcf069d | 10683 | void |
971de7fb | 10684 | init_coding_once (void) |
4ed46869 KH |
10685 | { |
10686 | int i; | |
10687 | ||
df7492f9 KH |
10688 | for (i = 0; i < coding_category_max; i++) |
10689 | { | |
10690 | coding_categories[i].id = -1; | |
10691 | coding_priorities[i] = i; | |
10692 | } | |
4ed46869 KH |
10693 | |
10694 | /* ISO2022 specific initialize routine. */ | |
10695 | for (i = 0; i < 0x20; i++) | |
b73bfc1c | 10696 | iso_code_class[i] = ISO_control_0; |
4ed46869 KH |
10697 | for (i = 0x21; i < 0x7F; i++) |
10698 | iso_code_class[i] = ISO_graphic_plane_0; | |
10699 | for (i = 0x80; i < 0xA0; i++) | |
b73bfc1c | 10700 | iso_code_class[i] = ISO_control_1; |
4ed46869 KH |
10701 | for (i = 0xA1; i < 0xFF; i++) |
10702 | iso_code_class[i] = ISO_graphic_plane_1; | |
10703 | iso_code_class[0x20] = iso_code_class[0x7F] = ISO_0x20_or_0x7F; | |
10704 | iso_code_class[0xA0] = iso_code_class[0xFF] = ISO_0xA0_or_0xFF; | |
4ed46869 KH |
10705 | iso_code_class[ISO_CODE_SO] = ISO_shift_out; |
10706 | iso_code_class[ISO_CODE_SI] = ISO_shift_in; | |
10707 | iso_code_class[ISO_CODE_SS2_7] = ISO_single_shift_2_7; | |
10708 | iso_code_class[ISO_CODE_ESC] = ISO_escape; | |
10709 | iso_code_class[ISO_CODE_SS2] = ISO_single_shift_2; | |
10710 | iso_code_class[ISO_CODE_SS3] = ISO_single_shift_3; | |
10711 | iso_code_class[ISO_CODE_CSI] = ISO_control_sequence_introducer; | |
10712 | ||
df7492f9 KH |
10713 | for (i = 0; i < 256; i++) |
10714 | { | |
10715 | emacs_mule_bytes[i] = 1; | |
10716 | } | |
7c78e542 KH |
10717 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_11] = 3; |
10718 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_12] = 3; | |
10719 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_21] = 4; | |
10720 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_22] = 4; | |
e0e989f6 KH |
10721 | } |
10722 | ||
10723 | #ifdef emacs | |
10724 | ||
dfcf069d | 10725 | void |
971de7fb | 10726 | syms_of_coding (void) |
e0e989f6 | 10727 | { |
df7492f9 | 10728 | staticpro (&Vcoding_system_hash_table); |
8f924df7 KH |
10729 | { |
10730 | Lisp_Object args[2]; | |
10731 | args[0] = QCtest; | |
10732 | args[1] = Qeq; | |
10733 | Vcoding_system_hash_table = Fmake_hash_table (2, args); | |
10734 | } | |
df7492f9 KH |
10735 | |
10736 | staticpro (&Vsjis_coding_system); | |
10737 | Vsjis_coding_system = Qnil; | |
e0e989f6 | 10738 | |
df7492f9 KH |
10739 | staticpro (&Vbig5_coding_system); |
10740 | Vbig5_coding_system = Qnil; | |
10741 | ||
24a73b0a KH |
10742 | staticpro (&Vcode_conversion_reused_workbuf); |
10743 | Vcode_conversion_reused_workbuf = Qnil; | |
10744 | ||
10745 | staticpro (&Vcode_conversion_workbuf_name); | |
2a0213a6 | 10746 | Vcode_conversion_workbuf_name = build_pure_c_string (" *code-conversion-work*"); |
e0e989f6 | 10747 | |
24a73b0a | 10748 | reused_workbuf_in_use = 0; |
df7492f9 KH |
10749 | |
10750 | DEFSYM (Qcharset, "charset"); | |
10751 | DEFSYM (Qtarget_idx, "target-idx"); | |
10752 | DEFSYM (Qcoding_system_history, "coding-system-history"); | |
bb0115a2 RS |
10753 | Fset (Qcoding_system_history, Qnil); |
10754 | ||
9ce27fde | 10755 | /* Target FILENAME is the first argument. */ |
e0e989f6 | 10756 | Fput (Qinsert_file_contents, Qtarget_idx, make_number (0)); |
9ce27fde | 10757 | /* Target FILENAME is the third argument. */ |
e0e989f6 KH |
10758 | Fput (Qwrite_region, Qtarget_idx, make_number (2)); |
10759 | ||
df7492f9 | 10760 | DEFSYM (Qcall_process, "call-process"); |
9ce27fde | 10761 | /* Target PROGRAM is the first argument. */ |
e0e989f6 KH |
10762 | Fput (Qcall_process, Qtarget_idx, make_number (0)); |
10763 | ||
df7492f9 | 10764 | DEFSYM (Qcall_process_region, "call-process-region"); |
9ce27fde | 10765 | /* Target PROGRAM is the third argument. */ |
e0e989f6 KH |
10766 | Fput (Qcall_process_region, Qtarget_idx, make_number (2)); |
10767 | ||
df7492f9 | 10768 | DEFSYM (Qstart_process, "start-process"); |
9ce27fde | 10769 | /* Target PROGRAM is the third argument. */ |
e0e989f6 KH |
10770 | Fput (Qstart_process, Qtarget_idx, make_number (2)); |
10771 | ||
df7492f9 | 10772 | DEFSYM (Qopen_network_stream, "open-network-stream"); |
9ce27fde | 10773 | /* Target SERVICE is the fourth argument. */ |
e0e989f6 KH |
10774 | Fput (Qopen_network_stream, Qtarget_idx, make_number (3)); |
10775 | ||
df7492f9 KH |
10776 | DEFSYM (Qcoding_system, "coding-system"); |
10777 | DEFSYM (Qcoding_aliases, "coding-aliases"); | |
4ed46869 | 10778 | |
df7492f9 KH |
10779 | DEFSYM (Qeol_type, "eol-type"); |
10780 | DEFSYM (Qunix, "unix"); | |
10781 | DEFSYM (Qdos, "dos"); | |
4b298d5a | 10782 | DEFSYM (Qmac, "mac"); |
4ed46869 | 10783 | |
df7492f9 KH |
10784 | DEFSYM (Qbuffer_file_coding_system, "buffer-file-coding-system"); |
10785 | DEFSYM (Qpost_read_conversion, "post-read-conversion"); | |
10786 | DEFSYM (Qpre_write_conversion, "pre-write-conversion"); | |
10787 | DEFSYM (Qdefault_char, "default-char"); | |
10788 | DEFSYM (Qundecided, "undecided"); | |
10789 | DEFSYM (Qno_conversion, "no-conversion"); | |
10790 | DEFSYM (Qraw_text, "raw-text"); | |
4ed46869 | 10791 | |
df7492f9 | 10792 | DEFSYM (Qiso_2022, "iso-2022"); |
4ed46869 | 10793 | |
df7492f9 | 10794 | DEFSYM (Qutf_8, "utf-8"); |
8f924df7 | 10795 | DEFSYM (Qutf_8_emacs, "utf-8-emacs"); |
27901516 | 10796 | |
7f590b0c | 10797 | #if defined (WINDOWSNT) || defined (CYGWIN) |
ba116008 DC |
10798 | /* No, not utf-16-le: that one has a BOM. */ |
10799 | DEFSYM (Qutf_16le, "utf-16le"); | |
10800 | #endif | |
10801 | ||
df7492f9 | 10802 | DEFSYM (Qutf_16, "utf-16"); |
df7492f9 KH |
10803 | DEFSYM (Qbig, "big"); |
10804 | DEFSYM (Qlittle, "little"); | |
27901516 | 10805 | |
df7492f9 KH |
10806 | DEFSYM (Qshift_jis, "shift-jis"); |
10807 | DEFSYM (Qbig5, "big5"); | |
4ed46869 | 10808 | |
df7492f9 | 10809 | DEFSYM (Qcoding_system_p, "coding-system-p"); |
4ed46869 | 10810 | |
df7492f9 | 10811 | DEFSYM (Qcoding_system_error, "coding-system-error"); |
4ed46869 | 10812 | Fput (Qcoding_system_error, Qerror_conditions, |
3438fe21 | 10813 | listn (CONSTYPE_PURE, 2, Qcoding_system_error, Qerror)); |
4ed46869 | 10814 | Fput (Qcoding_system_error, Qerror_message, |
2a0213a6 | 10815 | build_pure_c_string ("Invalid coding system")); |
4ed46869 | 10816 | |
df7492f9 | 10817 | DEFSYM (Qtranslation_table, "translation-table"); |
433f7f87 | 10818 | Fput (Qtranslation_table, Qchar_table_extra_slots, make_number (2)); |
df7492f9 KH |
10819 | DEFSYM (Qtranslation_table_id, "translation-table-id"); |
10820 | DEFSYM (Qtranslation_table_for_decode, "translation-table-for-decode"); | |
10821 | DEFSYM (Qtranslation_table_for_encode, "translation-table-for-encode"); | |
1397dc18 | 10822 | |
df7492f9 | 10823 | DEFSYM (Qvalid_codes, "valid-codes"); |
9ce27fde | 10824 | |
df7492f9 | 10825 | DEFSYM (Qemacs_mule, "emacs-mule"); |
d46c5b12 | 10826 | |
01378f49 | 10827 | DEFSYM (QCcategory, ":category"); |
a6f87d34 | 10828 | DEFSYM (QCmnemonic, ":mnemonic"); |
2133e2d1 | 10829 | DEFSYM (QCdefault_char, ":default-char"); |
a6f87d34 KH |
10830 | DEFSYM (QCdecode_translation_table, ":decode-translation-table"); |
10831 | DEFSYM (QCencode_translation_table, ":encode-translation-table"); | |
10832 | DEFSYM (QCpost_read_conversion, ":post-read-conversion"); | |
10833 | DEFSYM (QCpre_write_conversion, ":pre-write-conversion"); | |
35befdaa | 10834 | DEFSYM (QCascii_compatible_p, ":ascii-compatible-p"); |
01378f49 | 10835 | |
df7492f9 KH |
10836 | Vcoding_category_table |
10837 | = Fmake_vector (make_number (coding_category_max), Qnil); | |
10838 | staticpro (&Vcoding_category_table); | |
10839 | /* Followings are target of code detection. */ | |
10840 | ASET (Vcoding_category_table, coding_category_iso_7, | |
d67b4f80 | 10841 | intern_c_string ("coding-category-iso-7")); |
df7492f9 | 10842 | ASET (Vcoding_category_table, coding_category_iso_7_tight, |
d67b4f80 | 10843 | intern_c_string ("coding-category-iso-7-tight")); |
df7492f9 | 10844 | ASET (Vcoding_category_table, coding_category_iso_8_1, |
d67b4f80 | 10845 | intern_c_string ("coding-category-iso-8-1")); |
df7492f9 | 10846 | ASET (Vcoding_category_table, coding_category_iso_8_2, |
d67b4f80 | 10847 | intern_c_string ("coding-category-iso-8-2")); |
df7492f9 | 10848 | ASET (Vcoding_category_table, coding_category_iso_7_else, |
d67b4f80 | 10849 | intern_c_string ("coding-category-iso-7-else")); |
df7492f9 | 10850 | ASET (Vcoding_category_table, coding_category_iso_8_else, |
d67b4f80 | 10851 | intern_c_string ("coding-category-iso-8-else")); |
a470d443 | 10852 | ASET (Vcoding_category_table, coding_category_utf_8_auto, |
d67b4f80 | 10853 | intern_c_string ("coding-category-utf-8-auto")); |
a470d443 | 10854 | ASET (Vcoding_category_table, coding_category_utf_8_nosig, |
d67b4f80 | 10855 | intern_c_string ("coding-category-utf-8")); |
a470d443 | 10856 | ASET (Vcoding_category_table, coding_category_utf_8_sig, |
d67b4f80 | 10857 | intern_c_string ("coding-category-utf-8-sig")); |
df7492f9 | 10858 | ASET (Vcoding_category_table, coding_category_utf_16_be, |
d67b4f80 | 10859 | intern_c_string ("coding-category-utf-16-be")); |
ff563fce | 10860 | ASET (Vcoding_category_table, coding_category_utf_16_auto, |
d67b4f80 | 10861 | intern_c_string ("coding-category-utf-16-auto")); |
df7492f9 | 10862 | ASET (Vcoding_category_table, coding_category_utf_16_le, |
d67b4f80 | 10863 | intern_c_string ("coding-category-utf-16-le")); |
df7492f9 | 10864 | ASET (Vcoding_category_table, coding_category_utf_16_be_nosig, |
d67b4f80 | 10865 | intern_c_string ("coding-category-utf-16-be-nosig")); |
df7492f9 | 10866 | ASET (Vcoding_category_table, coding_category_utf_16_le_nosig, |
d67b4f80 | 10867 | intern_c_string ("coding-category-utf-16-le-nosig")); |
df7492f9 | 10868 | ASET (Vcoding_category_table, coding_category_charset, |
d67b4f80 | 10869 | intern_c_string ("coding-category-charset")); |
df7492f9 | 10870 | ASET (Vcoding_category_table, coding_category_sjis, |
d67b4f80 | 10871 | intern_c_string ("coding-category-sjis")); |
df7492f9 | 10872 | ASET (Vcoding_category_table, coding_category_big5, |
d67b4f80 | 10873 | intern_c_string ("coding-category-big5")); |
df7492f9 | 10874 | ASET (Vcoding_category_table, coding_category_ccl, |
d67b4f80 | 10875 | intern_c_string ("coding-category-ccl")); |
df7492f9 | 10876 | ASET (Vcoding_category_table, coding_category_emacs_mule, |
d67b4f80 | 10877 | intern_c_string ("coding-category-emacs-mule")); |
df7492f9 KH |
10878 | /* Followings are NOT target of code detection. */ |
10879 | ASET (Vcoding_category_table, coding_category_raw_text, | |
d67b4f80 | 10880 | intern_c_string ("coding-category-raw-text")); |
df7492f9 | 10881 | ASET (Vcoding_category_table, coding_category_undecided, |
d67b4f80 | 10882 | intern_c_string ("coding-category-undecided")); |
ecf488bc | 10883 | |
065e3595 | 10884 | DEFSYM (Qinsufficient_source, "insufficient-source"); |
065e3595 KH |
10885 | DEFSYM (Qinvalid_source, "invalid-source"); |
10886 | DEFSYM (Qinterrupted, "interrupted"); | |
44e8490d | 10887 | DEFSYM (Qcoding_system_define_form, "coding-system-define-form"); |
065e3595 | 10888 | |
4ed46869 KH |
10889 | defsubr (&Scoding_system_p); |
10890 | defsubr (&Sread_coding_system); | |
10891 | defsubr (&Sread_non_nil_coding_system); | |
10892 | defsubr (&Scheck_coding_system); | |
10893 | defsubr (&Sdetect_coding_region); | |
d46c5b12 | 10894 | defsubr (&Sdetect_coding_string); |
05e6f5dc | 10895 | defsubr (&Sfind_coding_systems_region_internal); |
068a9dbd | 10896 | defsubr (&Sunencodable_char_position); |
df7492f9 | 10897 | defsubr (&Scheck_coding_systems_region); |
4ed46869 KH |
10898 | defsubr (&Sdecode_coding_region); |
10899 | defsubr (&Sencode_coding_region); | |
10900 | defsubr (&Sdecode_coding_string); | |
10901 | defsubr (&Sencode_coding_string); | |
10902 | defsubr (&Sdecode_sjis_char); | |
10903 | defsubr (&Sencode_sjis_char); | |
10904 | defsubr (&Sdecode_big5_char); | |
10905 | defsubr (&Sencode_big5_char); | |
1ba9e4ab | 10906 | defsubr (&Sset_terminal_coding_system_internal); |
c4825358 | 10907 | defsubr (&Sset_safe_terminal_coding_system_internal); |
4ed46869 | 10908 | defsubr (&Sterminal_coding_system); |
1ba9e4ab | 10909 | defsubr (&Sset_keyboard_coding_system_internal); |
4ed46869 | 10910 | defsubr (&Skeyboard_coding_system); |
a5d301df | 10911 | defsubr (&Sfind_operation_coding_system); |
df7492f9 | 10912 | defsubr (&Sset_coding_system_priority); |
6b89e3aa | 10913 | defsubr (&Sdefine_coding_system_internal); |
df7492f9 | 10914 | defsubr (&Sdefine_coding_system_alias); |
a6f87d34 | 10915 | defsubr (&Scoding_system_put); |
df7492f9 KH |
10916 | defsubr (&Scoding_system_base); |
10917 | defsubr (&Scoding_system_plist); | |
10918 | defsubr (&Scoding_system_aliases); | |
10919 | defsubr (&Scoding_system_eol_type); | |
10920 | defsubr (&Scoding_system_priority_list); | |
4ed46869 | 10921 | |
29208e82 | 10922 | DEFVAR_LISP ("coding-system-list", Vcoding_system_list, |
48b0f3ae PJ |
10923 | doc: /* List of coding systems. |
10924 | ||
10925 | Do not alter the value of this variable manually. This variable should be | |
df7492f9 | 10926 | updated by the functions `define-coding-system' and |
48b0f3ae | 10927 | `define-coding-system-alias'. */); |
4608c386 KH |
10928 | Vcoding_system_list = Qnil; |
10929 | ||
29208e82 | 10930 | DEFVAR_LISP ("coding-system-alist", Vcoding_system_alist, |
48b0f3ae PJ |
10931 | doc: /* Alist of coding system names. |
10932 | Each element is one element list of coding system name. | |
446dcd75 | 10933 | This variable is given to `completing-read' as COLLECTION argument. |
48b0f3ae PJ |
10934 | |
10935 | Do not alter the value of this variable manually. This variable should be | |
10936 | updated by the functions `make-coding-system' and | |
10937 | `define-coding-system-alias'. */); | |
4608c386 KH |
10938 | Vcoding_system_alist = Qnil; |
10939 | ||
29208e82 | 10940 | DEFVAR_LISP ("coding-category-list", Vcoding_category_list, |
48b0f3ae PJ |
10941 | doc: /* List of coding-categories (symbols) ordered by priority. |
10942 | ||
10943 | On detecting a coding system, Emacs tries code detection algorithms | |
10944 | associated with each coding-category one by one in this order. When | |
10945 | one algorithm agrees with a byte sequence of source text, the coding | |
0ec31faf KH |
10946 | system bound to the corresponding coding-category is selected. |
10947 | ||
448e17d6 | 10948 | Don't modify this variable directly, but use `set-coding-system-priority'. */); |
4ed46869 KH |
10949 | { |
10950 | int i; | |
10951 | ||
10952 | Vcoding_category_list = Qnil; | |
df7492f9 | 10953 | for (i = coding_category_max - 1; i >= 0; i--) |
4ed46869 | 10954 | Vcoding_category_list |
28be1ada | 10955 | = Fcons (AREF (Vcoding_category_table, i), |
d46c5b12 | 10956 | Vcoding_category_list); |
4ed46869 KH |
10957 | } |
10958 | ||
29208e82 | 10959 | DEFVAR_LISP ("coding-system-for-read", Vcoding_system_for_read, |
48b0f3ae PJ |
10960 | doc: /* Specify the coding system for read operations. |
10961 | It is useful to bind this variable with `let', but do not set it globally. | |
10962 | If the value is a coding system, it is used for decoding on read operation. | |
446dcd75 JB |
10963 | If not, an appropriate element is used from one of the coding system alists. |
10964 | There are three such tables: `file-coding-system-alist', | |
48b0f3ae | 10965 | `process-coding-system-alist', and `network-coding-system-alist'. */); |
4ed46869 KH |
10966 | Vcoding_system_for_read = Qnil; |
10967 | ||
29208e82 | 10968 | DEFVAR_LISP ("coding-system-for-write", Vcoding_system_for_write, |
48b0f3ae PJ |
10969 | doc: /* Specify the coding system for write operations. |
10970 | Programs bind this variable with `let', but you should not set it globally. | |
10971 | If the value is a coding system, it is used for encoding of output, | |
10972 | when writing it to a file and when sending it to a file or subprocess. | |
10973 | ||
10974 | If this does not specify a coding system, an appropriate element | |
446dcd75 JB |
10975 | is used from one of the coding system alists. |
10976 | There are three such tables: `file-coding-system-alist', | |
48b0f3ae PJ |
10977 | `process-coding-system-alist', and `network-coding-system-alist'. |
10978 | For output to files, if the above procedure does not specify a coding system, | |
10979 | the value of `buffer-file-coding-system' is used. */); | |
4ed46869 KH |
10980 | Vcoding_system_for_write = Qnil; |
10981 | ||
29208e82 | 10982 | DEFVAR_LISP ("last-coding-system-used", Vlast_coding_system_used, |
df7492f9 KH |
10983 | doc: /* |
10984 | Coding system used in the latest file or process I/O. */); | |
4ed46869 KH |
10985 | Vlast_coding_system_used = Qnil; |
10986 | ||
29208e82 | 10987 | DEFVAR_LISP ("last-code-conversion-error", Vlast_code_conversion_error, |
065e3595 KH |
10988 | doc: /* |
10989 | Error status of the last code conversion. | |
10990 | ||
10991 | When an error was detected in the last code conversion, this variable | |
10992 | is set to one of the following symbols. | |
10993 | `insufficient-source' | |
10994 | `inconsistent-eol' | |
10995 | `invalid-source' | |
10996 | `interrupted' | |
10997 | `insufficient-memory' | |
10998 | When no error was detected, the value doesn't change. So, to check | |
10999 | the error status of a code conversion by this variable, you must | |
11000 | explicitly set this variable to nil before performing code | |
11001 | conversion. */); | |
11002 | Vlast_code_conversion_error = Qnil; | |
11003 | ||
29208e82 | 11004 | DEFVAR_BOOL ("inhibit-eol-conversion", inhibit_eol_conversion, |
df7492f9 KH |
11005 | doc: /* |
11006 | *Non-nil means always inhibit code conversion of end-of-line format. | |
48b0f3ae PJ |
11007 | See info node `Coding Systems' and info node `Text and Binary' concerning |
11008 | such conversion. */); | |
9ce27fde KH |
11009 | inhibit_eol_conversion = 0; |
11010 | ||
29208e82 | 11011 | DEFVAR_BOOL ("inherit-process-coding-system", inherit_process_coding_system, |
df7492f9 KH |
11012 | doc: /* |
11013 | Non-nil means process buffer inherits coding system of process output. | |
48b0f3ae PJ |
11014 | Bind it to t if the process output is to be treated as if it were a file |
11015 | read from some filesystem. */); | |
ed29121d EZ |
11016 | inherit_process_coding_system = 0; |
11017 | ||
29208e82 | 11018 | DEFVAR_LISP ("file-coding-system-alist", Vfile_coding_system_alist, |
df7492f9 KH |
11019 | doc: /* |
11020 | Alist to decide a coding system to use for a file I/O operation. | |
48b0f3ae PJ |
11021 | The format is ((PATTERN . VAL) ...), |
11022 | where PATTERN is a regular expression matching a file name, | |
11023 | VAL is a coding system, a cons of coding systems, or a function symbol. | |
11024 | If VAL is a coding system, it is used for both decoding and encoding | |
11025 | the file contents. | |
11026 | If VAL is a cons of coding systems, the car part is used for decoding, | |
11027 | and the cdr part is used for encoding. | |
11028 | If VAL is a function symbol, the function must return a coding system | |
2c53e699 KH |
11029 | or a cons of coding systems which are used as above. The function is |
11030 | called with an argument that is a list of the arguments with which | |
5a0bbd9a KH |
11031 | `find-operation-coding-system' was called. If the function can't decide |
11032 | a coding system, it can return `undecided' so that the normal | |
11033 | code-detection is performed. | |
48b0f3ae PJ |
11034 | |
11035 | See also the function `find-operation-coding-system' | |
11036 | and the variable `auto-coding-alist'. */); | |
02ba4723 KH |
11037 | Vfile_coding_system_alist = Qnil; |
11038 | ||
29208e82 | 11039 | DEFVAR_LISP ("process-coding-system-alist", Vprocess_coding_system_alist, |
df7492f9 KH |
11040 | doc: /* |
11041 | Alist to decide a coding system to use for a process I/O operation. | |
48b0f3ae PJ |
11042 | The format is ((PATTERN . VAL) ...), |
11043 | where PATTERN is a regular expression matching a program name, | |
11044 | VAL is a coding system, a cons of coding systems, or a function symbol. | |
11045 | If VAL is a coding system, it is used for both decoding what received | |
11046 | from the program and encoding what sent to the program. | |
11047 | If VAL is a cons of coding systems, the car part is used for decoding, | |
11048 | and the cdr part is used for encoding. | |
11049 | If VAL is a function symbol, the function must return a coding system | |
11050 | or a cons of coding systems which are used as above. | |
11051 | ||
11052 | See also the function `find-operation-coding-system'. */); | |
02ba4723 KH |
11053 | Vprocess_coding_system_alist = Qnil; |
11054 | ||
29208e82 | 11055 | DEFVAR_LISP ("network-coding-system-alist", Vnetwork_coding_system_alist, |
df7492f9 KH |
11056 | doc: /* |
11057 | Alist to decide a coding system to use for a network I/O operation. | |
48b0f3ae PJ |
11058 | The format is ((PATTERN . VAL) ...), |
11059 | where PATTERN is a regular expression matching a network service name | |
11060 | or is a port number to connect to, | |
11061 | VAL is a coding system, a cons of coding systems, or a function symbol. | |
11062 | If VAL is a coding system, it is used for both decoding what received | |
11063 | from the network stream and encoding what sent to the network stream. | |
11064 | If VAL is a cons of coding systems, the car part is used for decoding, | |
11065 | and the cdr part is used for encoding. | |
11066 | If VAL is a function symbol, the function must return a coding system | |
11067 | or a cons of coding systems which are used as above. | |
11068 | ||
11069 | See also the function `find-operation-coding-system'. */); | |
02ba4723 | 11070 | Vnetwork_coding_system_alist = Qnil; |
4ed46869 | 11071 | |
29208e82 | 11072 | DEFVAR_LISP ("locale-coding-system", Vlocale_coding_system, |
75205970 RS |
11073 | doc: /* Coding system to use with system messages. |
11074 | Also used for decoding keyboard input on X Window system. */); | |
68c45bf0 PE |
11075 | Vlocale_coding_system = Qnil; |
11076 | ||
005f0d35 | 11077 | /* The eol mnemonics are reset in startup.el system-dependently. */ |
29208e82 | 11078 | DEFVAR_LISP ("eol-mnemonic-unix", eol_mnemonic_unix, |
df7492f9 KH |
11079 | doc: /* |
11080 | *String displayed in mode line for UNIX-like (LF) end-of-line format. */); | |
2a0213a6 | 11081 | eol_mnemonic_unix = build_pure_c_string (":"); |
4ed46869 | 11082 | |
29208e82 | 11083 | DEFVAR_LISP ("eol-mnemonic-dos", eol_mnemonic_dos, |
df7492f9 KH |
11084 | doc: /* |
11085 | *String displayed in mode line for DOS-like (CRLF) end-of-line format. */); | |
2a0213a6 | 11086 | eol_mnemonic_dos = build_pure_c_string ("\\"); |
4ed46869 | 11087 | |
29208e82 | 11088 | DEFVAR_LISP ("eol-mnemonic-mac", eol_mnemonic_mac, |
df7492f9 KH |
11089 | doc: /* |
11090 | *String displayed in mode line for MAC-like (CR) end-of-line format. */); | |
2a0213a6 | 11091 | eol_mnemonic_mac = build_pure_c_string ("/"); |
4ed46869 | 11092 | |
29208e82 | 11093 | DEFVAR_LISP ("eol-mnemonic-undecided", eol_mnemonic_undecided, |
df7492f9 KH |
11094 | doc: /* |
11095 | *String displayed in mode line when end-of-line format is not yet determined. */); | |
2a0213a6 | 11096 | eol_mnemonic_undecided = build_pure_c_string (":"); |
4ed46869 | 11097 | |
29208e82 | 11098 | DEFVAR_LISP ("enable-character-translation", Venable_character_translation, |
df7492f9 KH |
11099 | doc: /* |
11100 | *Non-nil enables character translation while encoding and decoding. */); | |
84fbb8a0 | 11101 | Venable_character_translation = Qt; |
bdd9fb48 | 11102 | |
f967223b | 11103 | DEFVAR_LISP ("standard-translation-table-for-decode", |
29208e82 | 11104 | Vstandard_translation_table_for_decode, |
48b0f3ae | 11105 | doc: /* Table for translating characters while decoding. */); |
f967223b | 11106 | Vstandard_translation_table_for_decode = Qnil; |
bdd9fb48 | 11107 | |
f967223b | 11108 | DEFVAR_LISP ("standard-translation-table-for-encode", |
29208e82 | 11109 | Vstandard_translation_table_for_encode, |
48b0f3ae | 11110 | doc: /* Table for translating characters while encoding. */); |
f967223b | 11111 | Vstandard_translation_table_for_encode = Qnil; |
4ed46869 | 11112 | |
29208e82 | 11113 | DEFVAR_LISP ("charset-revision-table", Vcharset_revision_table, |
48b0f3ae PJ |
11114 | doc: /* Alist of charsets vs revision numbers. |
11115 | While encoding, if a charset (car part of an element) is found, | |
df7492f9 KH |
11116 | designate it with the escape sequence identifying revision (cdr part |
11117 | of the element). */); | |
11118 | Vcharset_revision_table = Qnil; | |
02ba4723 KH |
11119 | |
11120 | DEFVAR_LISP ("default-process-coding-system", | |
29208e82 | 11121 | Vdefault_process_coding_system, |
48b0f3ae PJ |
11122 | doc: /* Cons of coding systems used for process I/O by default. |
11123 | The car part is used for decoding a process output, | |
11124 | the cdr part is used for encoding a text to be sent to a process. */); | |
02ba4723 | 11125 | Vdefault_process_coding_system = Qnil; |
c4825358 | 11126 | |
29208e82 | 11127 | DEFVAR_LISP ("latin-extra-code-table", Vlatin_extra_code_table, |
df7492f9 KH |
11128 | doc: /* |
11129 | Table of extra Latin codes in the range 128..159 (inclusive). | |
48b0f3ae PJ |
11130 | This is a vector of length 256. |
11131 | If Nth element is non-nil, the existence of code N in a file | |
11132 | \(or output of subprocess) doesn't prevent it to be detected as | |
11133 | a coding system of ISO 2022 variant which has a flag | |
11134 | `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file | |
11135 | or reading output of a subprocess. | |
446dcd75 | 11136 | Only 128th through 159th elements have a meaning. */); |
3f003981 | 11137 | Vlatin_extra_code_table = Fmake_vector (make_number (256), Qnil); |
d46c5b12 KH |
11138 | |
11139 | DEFVAR_LISP ("select-safe-coding-system-function", | |
29208e82 | 11140 | Vselect_safe_coding_system_function, |
df7492f9 KH |
11141 | doc: /* |
11142 | Function to call to select safe coding system for encoding a text. | |
48b0f3ae PJ |
11143 | |
11144 | If set, this function is called to force a user to select a proper | |
11145 | coding system which can encode the text in the case that a default | |
fdecf907 GM |
11146 | coding system used in each operation can't encode the text. The |
11147 | function should take care that the buffer is not modified while | |
11148 | the coding system is being selected. | |
48b0f3ae PJ |
11149 | |
11150 | The default value is `select-safe-coding-system' (which see). */); | |
d46c5b12 KH |
11151 | Vselect_safe_coding_system_function = Qnil; |
11152 | ||
5d5bf4d8 | 11153 | DEFVAR_BOOL ("coding-system-require-warning", |
29208e82 | 11154 | coding_system_require_warning, |
5d5bf4d8 | 11155 | doc: /* Internal use only. |
6b89e3aa KH |
11156 | If non-nil, on writing a file, `select-safe-coding-system-function' is |
11157 | called even if `coding-system-for-write' is non-nil. The command | |
11158 | `universal-coding-system-argument' binds this variable to t temporarily. */); | |
5d5bf4d8 KH |
11159 | coding_system_require_warning = 0; |
11160 | ||
11161 | ||
22ab2303 | 11162 | DEFVAR_BOOL ("inhibit-iso-escape-detection", |
29208e82 | 11163 | inhibit_iso_escape_detection, |
df7492f9 | 11164 | doc: /* |
97b1b294 | 11165 | If non-nil, Emacs ignores ISO-2022 escape sequences during code detection. |
48b0f3ae | 11166 | |
97b1b294 EZ |
11167 | When Emacs reads text, it tries to detect how the text is encoded. |
11168 | This code detection is sensitive to escape sequences. If Emacs sees | |
11169 | a valid ISO-2022 escape sequence, it assumes the text is encoded in one | |
11170 | of the ISO2022 encodings, and decodes text by the corresponding coding | |
11171 | system (e.g. `iso-2022-7bit'). | |
48b0f3ae PJ |
11172 | |
11173 | However, there may be a case that you want to read escape sequences in | |
11174 | a file as is. In such a case, you can set this variable to non-nil. | |
97b1b294 EZ |
11175 | Then the code detection will ignore any escape sequences, and no text is |
11176 | detected as encoded in some ISO-2022 encoding. The result is that all | |
48b0f3ae PJ |
11177 | escape sequences become visible in a buffer. |
11178 | ||
11179 | The default value is nil, and it is strongly recommended not to change | |
11180 | it. That is because many Emacs Lisp source files that contain | |
11181 | non-ASCII characters are encoded by the coding system `iso-2022-7bit' | |
11182 | in Emacs's distribution, and they won't be decoded correctly on | |
11183 | reading if you suppress escape sequence detection. | |
11184 | ||
11185 | The other way to read escape sequences in a file without decoding is | |
97b1b294 | 11186 | to explicitly specify some coding system that doesn't use ISO-2022 |
65e7ca35 | 11187 | escape sequence (e.g., `latin-1') on reading by \\[universal-coding-system-argument]. */); |
74383408 | 11188 | inhibit_iso_escape_detection = 0; |
002fdb44 | 11189 | |
97b1b294 | 11190 | DEFVAR_BOOL ("inhibit-null-byte-detection", |
29208e82 | 11191 | inhibit_null_byte_detection, |
97b1b294 EZ |
11192 | doc: /* If non-nil, Emacs ignores null bytes on code detection. |
11193 | By default, Emacs treats it as binary data, and does not attempt to | |
11194 | decode it. The effect is as if you specified `no-conversion' for | |
11195 | reading that text. | |
11196 | ||
11197 | Set this to non-nil when a regular text happens to include null bytes. | |
11198 | Examples are Index nodes of Info files and null-byte delimited output | |
11199 | from GNU Find and GNU Grep. Emacs will then ignore the null bytes and | |
11200 | decode text as usual. */); | |
11201 | inhibit_null_byte_detection = 0; | |
11202 | ||
8a44e6d1 KH |
11203 | DEFVAR_BOOL ("disable-ascii-optimization", disable_ascii_optimization, |
11204 | doc: /* If non-nil, Emacs does not optimize code decoder for ASCII files. | |
11205 | Internal use only. Removed after the experimental optimizer gets stable. */); | |
8bc369d4 | 11206 | disable_ascii_optimization = 0; |
8a44e6d1 | 11207 | |
29208e82 | 11208 | DEFVAR_LISP ("translation-table-for-input", Vtranslation_table_for_input, |
15c8f9d1 | 11209 | doc: /* Char table for translating self-inserting characters. |
446dcd75 | 11210 | This is applied to the result of input methods, not their input. |
8434d0b8 EZ |
11211 | See also `keyboard-translate-table'. |
11212 | ||
11213 | Use of this variable for character code unification was rendered | |
11214 | obsolete in Emacs 23.1 and later, since Unicode is now the basis of | |
11215 | internal character representation. */); | |
002fdb44 | 11216 | Vtranslation_table_for_input = Qnil; |
8f924df7 | 11217 | |
2c78b7e1 | 11218 | { |
c3a7b3f4 | 11219 | Lisp_Object args[coding_arg_undecided_max]; |
8f924df7 | 11220 | Lisp_Object plist[16]; |
2c78b7e1 KH |
11221 | int i; |
11222 | ||
c3a7b3f4 | 11223 | for (i = 0; i < coding_arg_undecided_max; i++) |
2c78b7e1 KH |
11224 | args[i] = Qnil; |
11225 | ||
d67b4f80 | 11226 | plist[0] = intern_c_string (":name"); |
2c78b7e1 | 11227 | plist[1] = args[coding_arg_name] = Qno_conversion; |
d67b4f80 | 11228 | plist[2] = intern_c_string (":mnemonic"); |
2c78b7e1 | 11229 | plist[3] = args[coding_arg_mnemonic] = make_number ('='); |
d67b4f80 | 11230 | plist[4] = intern_c_string (":coding-type"); |
2c78b7e1 | 11231 | plist[5] = args[coding_arg_coding_type] = Qraw_text; |
d67b4f80 | 11232 | plist[6] = intern_c_string (":ascii-compatible-p"); |
2c78b7e1 | 11233 | plist[7] = args[coding_arg_ascii_compatible_p] = Qt; |
d67b4f80 | 11234 | plist[8] = intern_c_string (":default-char"); |
2c78b7e1 | 11235 | plist[9] = args[coding_arg_default_char] = make_number (0); |
d67b4f80 | 11236 | plist[10] = intern_c_string (":for-unibyte"); |
8f924df7 | 11237 | plist[11] = args[coding_arg_for_unibyte] = Qt; |
d67b4f80 | 11238 | plist[12] = intern_c_string (":docstring"); |
2a0213a6 | 11239 | plist[13] = build_pure_c_string ("Do no conversion.\n\ |
2c78b7e1 KH |
11240 | \n\ |
11241 | When you visit a file with this coding, the file is read into a\n\ | |
11242 | unibyte buffer as is, thus each byte of a file is treated as a\n\ | |
11243 | character."); | |
d67b4f80 | 11244 | plist[14] = intern_c_string (":eol-type"); |
8f924df7 KH |
11245 | plist[15] = args[coding_arg_eol_type] = Qunix; |
11246 | args[coding_arg_plist] = Flist (16, plist); | |
2c78b7e1 | 11247 | Fdefine_coding_system_internal (coding_arg_max, args); |
ae6f73fa KH |
11248 | |
11249 | plist[1] = args[coding_arg_name] = Qundecided; | |
11250 | plist[3] = args[coding_arg_mnemonic] = make_number ('-'); | |
11251 | plist[5] = args[coding_arg_coding_type] = Qundecided; | |
11252 | /* This is already set. | |
35befdaa | 11253 | plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */ |
d67b4f80 | 11254 | plist[8] = intern_c_string (":charset-list"); |
ae6f73fa KH |
11255 | plist[9] = args[coding_arg_charset_list] = Fcons (Qascii, Qnil); |
11256 | plist[11] = args[coding_arg_for_unibyte] = Qnil; | |
2a0213a6 | 11257 | plist[13] = build_pure_c_string ("No conversion on encoding, automatic conversion on decoding."); |
ae6f73fa KH |
11258 | plist[15] = args[coding_arg_eol_type] = Qnil; |
11259 | args[coding_arg_plist] = Flist (16, plist); | |
f9a74c4c EZ |
11260 | args[coding_arg_undecided_inhibit_null_byte_detection] = make_number (0); |
11261 | args[coding_arg_undecided_inhibit_iso_escape_detection] = make_number (0); | |
c3a7b3f4 | 11262 | Fdefine_coding_system_internal (coding_arg_undecided_max, args); |
2c78b7e1 KH |
11263 | } |
11264 | ||
2c78b7e1 | 11265 | setup_coding_system (Qno_conversion, &safe_terminal_coding); |
ff563fce KH |
11266 | |
11267 | { | |
11268 | int i; | |
11269 | ||
11270 | for (i = 0; i < coding_category_max; i++) | |
11271 | Fset (AREF (Vcoding_category_table, i), Qno_conversion); | |
11272 | } | |
1a4990fb | 11273 | #if defined (DOS_NT) |
fcbcfb64 KH |
11274 | system_eol_type = Qdos; |
11275 | #else | |
11276 | system_eol_type = Qunix; | |
11277 | #endif | |
11278 | staticpro (&system_eol_type); | |
4ed46869 KH |
11279 | } |
11280 | ||
68c45bf0 | 11281 | char * |
971de7fb | 11282 | emacs_strerror (int error_number) |
68c45bf0 PE |
11283 | { |
11284 | char *str; | |
11285 | ||
ca9c0567 | 11286 | synchronize_system_messages_locale (); |
68c45bf0 PE |
11287 | str = strerror (error_number); |
11288 | ||
11289 | if (! NILP (Vlocale_coding_system)) | |
11290 | { | |
11291 | Lisp_Object dec = code_convert_string_norecord (build_string (str), | |
11292 | Vlocale_coding_system, | |
11293 | 0); | |
51b59d79 | 11294 | str = SSDATA (dec); |
68c45bf0 PE |
11295 | } |
11296 | ||
11297 | return str; | |
11298 | } | |
11299 | ||
4ed46869 | 11300 | #endif /* emacs */ |