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 | ||
4ed46869 | 289 | #include "lisp.h" |
df7492f9 | 290 | #include "character.h" |
e5560ff7 | 291 | #include "buffer.h" |
4ed46869 KH |
292 | #include "charset.h" |
293 | #include "ccl.h" | |
df7492f9 | 294 | #include "composite.h" |
4ed46869 KH |
295 | #include "coding.h" |
296 | #include "window.h" | |
b8299c66 KL |
297 | #include "frame.h" |
298 | #include "termhooks.h" | |
4ed46869 | 299 | |
df7492f9 | 300 | Lisp_Object Vcoding_system_hash_table; |
4ed46869 | 301 | |
955cbe7b PE |
302 | static Lisp_Object Qcoding_system, Qeol_type; |
303 | static Lisp_Object Qcoding_aliases; | |
84cc1ab6 PE |
304 | Lisp_Object Qunix, Qdos; |
305 | static Lisp_Object Qmac; | |
4ed46869 | 306 | Lisp_Object Qbuffer_file_coding_system; |
955cbe7b PE |
307 | static Lisp_Object Qpost_read_conversion, Qpre_write_conversion; |
308 | static Lisp_Object Qdefault_char; | |
27901516 | 309 | Lisp_Object Qno_conversion, Qundecided; |
955cbe7b PE |
310 | Lisp_Object Qcharset, Qutf_8; |
311 | static Lisp_Object Qiso_2022; | |
312 | static Lisp_Object Qutf_16, Qshift_jis, Qbig5; | |
313 | static Lisp_Object Qbig, Qlittle; | |
314 | static Lisp_Object Qcoding_system_history; | |
315 | static Lisp_Object Qvalid_codes; | |
316 | static Lisp_Object QCcategory, QCmnemonic, QCdefault_char; | |
317 | static Lisp_Object QCdecode_translation_table, QCencode_translation_table; | |
318 | static Lisp_Object QCpost_read_conversion, QCpre_write_conversion; | |
319 | static Lisp_Object QCascii_compatible_p; | |
4ed46869 | 320 | |
387f6ba5 | 321 | Lisp_Object Qcall_process, Qcall_process_region; |
4ed46869 | 322 | Lisp_Object Qstart_process, Qopen_network_stream; |
955cbe7b | 323 | static Lisp_Object Qtarget_idx; |
4ed46869 | 324 | |
1af1a51a | 325 | static Lisp_Object Qinsufficient_source, Qinvalid_source, Qinterrupted; |
065e3595 | 326 | |
44e8490d KH |
327 | /* If a symbol has this property, evaluate the value to define the |
328 | symbol as a coding system. */ | |
329 | static Lisp_Object Qcoding_system_define_form; | |
330 | ||
fcbcfb64 KH |
331 | /* Format of end-of-line decided by system. This is Qunix on |
332 | Unix and Mac, Qdos on DOS/Windows. | |
333 | This has an effect only for external encoding (i.e. for output to | |
334 | file and process), not for in-buffer or Lisp string encoding. */ | |
335 | static Lisp_Object system_eol_type; | |
336 | ||
4ed46869 KH |
337 | #ifdef emacs |
338 | ||
4608c386 | 339 | Lisp_Object Qcoding_system_p, Qcoding_system_error; |
4ed46869 | 340 | |
d46c5b12 KH |
341 | /* Coding system emacs-mule and raw-text are for converting only |
342 | end-of-line format. */ | |
343 | Lisp_Object Qemacs_mule, Qraw_text; | |
8f924df7 | 344 | Lisp_Object Qutf_8_emacs; |
ecf488bc | 345 | |
53372c27 | 346 | #if defined (WINDOWSNT) || defined (CYGWIN) |
ba116008 DC |
347 | static Lisp_Object Qutf_16le; |
348 | #endif | |
349 | ||
4ed46869 KH |
350 | /* Coding-systems are handed between Emacs Lisp programs and C internal |
351 | routines by the following three variables. */ | |
c4825358 KH |
352 | /* Coding system to be used to encode text for terminal display when |
353 | terminal coding system is nil. */ | |
354 | struct coding_system safe_terminal_coding; | |
355 | ||
4ed46869 KH |
356 | #endif /* emacs */ |
357 | ||
f967223b KH |
358 | Lisp_Object Qtranslation_table; |
359 | Lisp_Object Qtranslation_table_id; | |
955cbe7b PE |
360 | static Lisp_Object Qtranslation_table_for_decode; |
361 | static Lisp_Object Qtranslation_table_for_encode; | |
4ed46869 | 362 | |
df7492f9 | 363 | /* Two special coding systems. */ |
74ab6df5 PE |
364 | static Lisp_Object Vsjis_coding_system; |
365 | static Lisp_Object Vbig5_coding_system; | |
df7492f9 | 366 | |
df7492f9 KH |
367 | /* ISO2022 section */ |
368 | ||
369 | #define CODING_ISO_INITIAL(coding, reg) \ | |
370 | (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \ | |
371 | coding_attr_iso_initial), \ | |
372 | reg))) | |
373 | ||
374 | ||
1b3b981b AS |
375 | #define CODING_ISO_REQUEST(coding, charset_id) \ |
376 | (((charset_id) <= (coding)->max_charset_id \ | |
377 | ? ((coding)->safe_charsets[charset_id] != 255 \ | |
378 | ? (coding)->safe_charsets[charset_id] \ | |
379 | : -1) \ | |
df7492f9 KH |
380 | : -1)) |
381 | ||
382 | ||
383 | #define CODING_ISO_FLAGS(coding) \ | |
384 | ((coding)->spec.iso_2022.flags) | |
385 | #define CODING_ISO_DESIGNATION(coding, reg) \ | |
386 | ((coding)->spec.iso_2022.current_designation[reg]) | |
387 | #define CODING_ISO_INVOCATION(coding, plane) \ | |
388 | ((coding)->spec.iso_2022.current_invocation[plane]) | |
389 | #define CODING_ISO_SINGLE_SHIFTING(coding) \ | |
390 | ((coding)->spec.iso_2022.single_shifting) | |
391 | #define CODING_ISO_BOL(coding) \ | |
392 | ((coding)->spec.iso_2022.bol) | |
393 | #define CODING_ISO_INVOKED_CHARSET(coding, plane) \ | |
394 | CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane))) | |
e951386e KH |
395 | #define CODING_ISO_CMP_STATUS(coding) \ |
396 | (&(coding)->spec.iso_2022.cmp_status) | |
397 | #define CODING_ISO_EXTSEGMENT_LEN(coding) \ | |
398 | ((coding)->spec.iso_2022.ctext_extended_segment_len) | |
399 | #define CODING_ISO_EMBEDDED_UTF_8(coding) \ | |
400 | ((coding)->spec.iso_2022.embedded_utf_8) | |
df7492f9 KH |
401 | |
402 | /* Control characters of ISO2022. */ | |
403 | /* code */ /* function */ | |
df7492f9 KH |
404 | #define ISO_CODE_SO 0x0E /* shift-out */ |
405 | #define ISO_CODE_SI 0x0F /* shift-in */ | |
406 | #define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */ | |
407 | #define ISO_CODE_ESC 0x1B /* escape */ | |
408 | #define ISO_CODE_SS2 0x8E /* single-shift-2 */ | |
409 | #define ISO_CODE_SS3 0x8F /* single-shift-3 */ | |
410 | #define ISO_CODE_CSI 0x9B /* control-sequence-introducer */ | |
411 | ||
412 | /* All code (1-byte) of ISO2022 is classified into one of the | |
413 | followings. */ | |
414 | enum iso_code_class_type | |
415 | { | |
416 | ISO_control_0, /* Control codes in the range | |
417 | 0x00..0x1F and 0x7F, except for the | |
418 | following 5 codes. */ | |
df7492f9 KH |
419 | ISO_shift_out, /* ISO_CODE_SO (0x0E) */ |
420 | ISO_shift_in, /* ISO_CODE_SI (0x0F) */ | |
421 | ISO_single_shift_2_7, /* ISO_CODE_SS2_7 (0x19) */ | |
a0d7415f | 422 | ISO_escape, /* ISO_CODE_ESC (0x1B) */ |
df7492f9 KH |
423 | ISO_control_1, /* Control codes in the range |
424 | 0x80..0x9F, except for the | |
425 | following 3 codes. */ | |
426 | ISO_single_shift_2, /* ISO_CODE_SS2 (0x8E) */ | |
427 | ISO_single_shift_3, /* ISO_CODE_SS3 (0x8F) */ | |
428 | ISO_control_sequence_introducer, /* ISO_CODE_CSI (0x9B) */ | |
429 | ISO_0x20_or_0x7F, /* Codes of the values 0x20 or 0x7F. */ | |
430 | ISO_graphic_plane_0, /* Graphic codes in the range 0x21..0x7E. */ | |
431 | ISO_0xA0_or_0xFF, /* Codes of the values 0xA0 or 0xFF. */ | |
432 | ISO_graphic_plane_1 /* Graphic codes in the range 0xA1..0xFE. */ | |
433 | }; | |
05e6f5dc | 434 | |
df7492f9 KH |
435 | /** The macros CODING_ISO_FLAG_XXX defines a flag bit of the |
436 | `iso-flags' attribute of an iso2022 coding system. */ | |
05e6f5dc | 437 | |
df7492f9 KH |
438 | /* If set, produce long-form designation sequence (e.g. ESC $ ( A) |
439 | instead of the correct short-form sequence (e.g. ESC $ A). */ | |
440 | #define CODING_ISO_FLAG_LONG_FORM 0x0001 | |
93dec019 | 441 | |
df7492f9 KH |
442 | /* If set, reset graphic planes and registers at end-of-line to the |
443 | initial state. */ | |
444 | #define CODING_ISO_FLAG_RESET_AT_EOL 0x0002 | |
05e6f5dc | 445 | |
df7492f9 KH |
446 | /* If set, reset graphic planes and registers before any control |
447 | characters to the initial state. */ | |
448 | #define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004 | |
05e6f5dc | 449 | |
df7492f9 KH |
450 | /* If set, encode by 7-bit environment. */ |
451 | #define CODING_ISO_FLAG_SEVEN_BITS 0x0008 | |
4ed46869 | 452 | |
df7492f9 KH |
453 | /* If set, use locking-shift function. */ |
454 | #define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010 | |
b73bfc1c | 455 | |
df7492f9 KH |
456 | /* If set, use single-shift function. Overwrite |
457 | CODING_ISO_FLAG_LOCKING_SHIFT. */ | |
458 | #define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020 | |
b73bfc1c | 459 | |
df7492f9 KH |
460 | /* If set, use designation escape sequence. */ |
461 | #define CODING_ISO_FLAG_DESIGNATION 0x0040 | |
b73bfc1c | 462 | |
df7492f9 KH |
463 | /* If set, produce revision number sequence. */ |
464 | #define CODING_ISO_FLAG_REVISION 0x0080 | |
b73bfc1c | 465 | |
df7492f9 KH |
466 | /* If set, produce ISO6429's direction specifying sequence. */ |
467 | #define CODING_ISO_FLAG_DIRECTION 0x0100 | |
f4dee582 | 468 | |
df7492f9 KH |
469 | /* If set, assume designation states are reset at beginning of line on |
470 | output. */ | |
471 | #define CODING_ISO_FLAG_INIT_AT_BOL 0x0200 | |
4ed46869 | 472 | |
df7492f9 KH |
473 | /* If set, designation sequence should be placed at beginning of line |
474 | on output. */ | |
475 | #define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400 | |
aa72b389 | 476 | |
ad1746f5 | 477 | /* If set, do not encode unsafe characters on output. */ |
df7492f9 | 478 | #define CODING_ISO_FLAG_SAFE 0x0800 |
aa72b389 | 479 | |
df7492f9 KH |
480 | /* If set, extra latin codes (128..159) are accepted as a valid code |
481 | on input. */ | |
482 | #define CODING_ISO_FLAG_LATIN_EXTRA 0x1000 | |
aa72b389 | 483 | |
df7492f9 | 484 | #define CODING_ISO_FLAG_COMPOSITION 0x2000 |
aa72b389 | 485 | |
5f58e762 | 486 | /* #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000 */ |
aa72b389 | 487 | |
bf16eb23 | 488 | #define CODING_ISO_FLAG_USE_ROMAN 0x8000 |
aa72b389 | 489 | |
bf16eb23 | 490 | #define CODING_ISO_FLAG_USE_OLDJIS 0x10000 |
aa72b389 | 491 | |
bf16eb23 | 492 | #define CODING_ISO_FLAG_FULL_SUPPORT 0x100000 |
aa72b389 | 493 | |
df7492f9 KH |
494 | /* A character to be produced on output if encoding of the original |
495 | character is prohibited by CODING_ISO_FLAG_SAFE. */ | |
496 | #define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?' | |
aa72b389 | 497 | |
a470d443 KH |
498 | /* UTF-8 section */ |
499 | #define CODING_UTF_8_BOM(coding) \ | |
500 | ((coding)->spec.utf_8_bom) | |
4ed46869 | 501 | |
df7492f9 KH |
502 | /* UTF-16 section */ |
503 | #define CODING_UTF_16_BOM(coding) \ | |
504 | ((coding)->spec.utf_16.bom) | |
4ed46869 | 505 | |
df7492f9 KH |
506 | #define CODING_UTF_16_ENDIAN(coding) \ |
507 | ((coding)->spec.utf_16.endian) | |
4ed46869 | 508 | |
df7492f9 KH |
509 | #define CODING_UTF_16_SURROGATE(coding) \ |
510 | ((coding)->spec.utf_16.surrogate) | |
4ed46869 | 511 | |
4ed46869 | 512 | |
df7492f9 KH |
513 | /* CCL section */ |
514 | #define CODING_CCL_DECODER(coding) \ | |
515 | AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder) | |
516 | #define CODING_CCL_ENCODER(coding) \ | |
517 | AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder) | |
518 | #define CODING_CCL_VALIDS(coding) \ | |
8f924df7 | 519 | (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids))) |
4ed46869 | 520 | |
5a936b46 | 521 | /* Index for each coding category in `coding_categories' */ |
4ed46869 | 522 | |
df7492f9 KH |
523 | enum coding_category |
524 | { | |
525 | coding_category_iso_7, | |
526 | coding_category_iso_7_tight, | |
527 | coding_category_iso_8_1, | |
528 | coding_category_iso_8_2, | |
529 | coding_category_iso_7_else, | |
530 | coding_category_iso_8_else, | |
a470d443 KH |
531 | coding_category_utf_8_auto, |
532 | coding_category_utf_8_nosig, | |
533 | coding_category_utf_8_sig, | |
df7492f9 KH |
534 | coding_category_utf_16_auto, |
535 | coding_category_utf_16_be, | |
536 | coding_category_utf_16_le, | |
537 | coding_category_utf_16_be_nosig, | |
538 | coding_category_utf_16_le_nosig, | |
539 | coding_category_charset, | |
540 | coding_category_sjis, | |
541 | coding_category_big5, | |
542 | coding_category_ccl, | |
543 | coding_category_emacs_mule, | |
544 | /* All above are targets of code detection. */ | |
545 | coding_category_raw_text, | |
546 | coding_category_undecided, | |
547 | coding_category_max | |
548 | }; | |
549 | ||
550 | /* Definitions of flag bits used in detect_coding_XXXX. */ | |
551 | #define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7) | |
552 | #define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight) | |
553 | #define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1) | |
554 | #define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2) | |
555 | #define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else) | |
556 | #define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else) | |
a470d443 KH |
557 | #define CATEGORY_MASK_UTF_8_AUTO (1 << coding_category_utf_8_auto) |
558 | #define CATEGORY_MASK_UTF_8_NOSIG (1 << coding_category_utf_8_nosig) | |
559 | #define CATEGORY_MASK_UTF_8_SIG (1 << coding_category_utf_8_sig) | |
b49a1807 | 560 | #define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto) |
df7492f9 KH |
561 | #define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be) |
562 | #define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le) | |
563 | #define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig) | |
564 | #define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig) | |
565 | #define CATEGORY_MASK_CHARSET (1 << coding_category_charset) | |
566 | #define CATEGORY_MASK_SJIS (1 << coding_category_sjis) | |
567 | #define CATEGORY_MASK_BIG5 (1 << coding_category_big5) | |
568 | #define CATEGORY_MASK_CCL (1 << coding_category_ccl) | |
569 | #define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule) | |
ff0dacd7 | 570 | #define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text) |
df7492f9 KH |
571 | |
572 | /* This value is returned if detect_coding_mask () find nothing other | |
573 | than ASCII characters. */ | |
574 | #define CATEGORY_MASK_ANY \ | |
575 | (CATEGORY_MASK_ISO_7 \ | |
576 | | CATEGORY_MASK_ISO_7_TIGHT \ | |
577 | | CATEGORY_MASK_ISO_8_1 \ | |
578 | | CATEGORY_MASK_ISO_8_2 \ | |
579 | | CATEGORY_MASK_ISO_7_ELSE \ | |
580 | | CATEGORY_MASK_ISO_8_ELSE \ | |
a470d443 KH |
581 | | CATEGORY_MASK_UTF_8_AUTO \ |
582 | | CATEGORY_MASK_UTF_8_NOSIG \ | |
583 | | CATEGORY_MASK_UTF_8_SIG \ | |
2f3cbb32 | 584 | | CATEGORY_MASK_UTF_16_AUTO \ |
df7492f9 KH |
585 | | CATEGORY_MASK_UTF_16_BE \ |
586 | | CATEGORY_MASK_UTF_16_LE \ | |
587 | | CATEGORY_MASK_UTF_16_BE_NOSIG \ | |
588 | | CATEGORY_MASK_UTF_16_LE_NOSIG \ | |
589 | | CATEGORY_MASK_CHARSET \ | |
590 | | CATEGORY_MASK_SJIS \ | |
591 | | CATEGORY_MASK_BIG5 \ | |
592 | | CATEGORY_MASK_CCL \ | |
593 | | CATEGORY_MASK_EMACS_MULE) | |
594 | ||
595 | ||
596 | #define CATEGORY_MASK_ISO_7BIT \ | |
597 | (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT) | |
598 | ||
599 | #define CATEGORY_MASK_ISO_8BIT \ | |
600 | (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2) | |
601 | ||
602 | #define CATEGORY_MASK_ISO_ELSE \ | |
603 | (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE) | |
604 | ||
605 | #define CATEGORY_MASK_ISO_ESCAPE \ | |
606 | (CATEGORY_MASK_ISO_7 \ | |
607 | | CATEGORY_MASK_ISO_7_TIGHT \ | |
608 | | CATEGORY_MASK_ISO_7_ELSE \ | |
609 | | CATEGORY_MASK_ISO_8_ELSE) | |
610 | ||
611 | #define CATEGORY_MASK_ISO \ | |
612 | ( CATEGORY_MASK_ISO_7BIT \ | |
613 | | CATEGORY_MASK_ISO_8BIT \ | |
614 | | CATEGORY_MASK_ISO_ELSE) | |
615 | ||
616 | #define CATEGORY_MASK_UTF_16 \ | |
2f3cbb32 KH |
617 | (CATEGORY_MASK_UTF_16_AUTO \ |
618 | | CATEGORY_MASK_UTF_16_BE \ | |
df7492f9 KH |
619 | | CATEGORY_MASK_UTF_16_LE \ |
620 | | CATEGORY_MASK_UTF_16_BE_NOSIG \ | |
621 | | CATEGORY_MASK_UTF_16_LE_NOSIG) | |
622 | ||
a470d443 KH |
623 | #define CATEGORY_MASK_UTF_8 \ |
624 | (CATEGORY_MASK_UTF_8_AUTO \ | |
625 | | CATEGORY_MASK_UTF_8_NOSIG \ | |
626 | | CATEGORY_MASK_UTF_8_SIG) | |
df7492f9 | 627 | |
df7492f9 | 628 | /* Table of coding categories (Lisp symbols). This variable is for |
ad1746f5 | 629 | internal use only. */ |
df7492f9 KH |
630 | static Lisp_Object Vcoding_category_table; |
631 | ||
632 | /* Table of coding-categories ordered by priority. */ | |
633 | static enum coding_category coding_priorities[coding_category_max]; | |
634 | ||
635 | /* Nth element is a coding context for the coding system bound to the | |
636 | Nth coding category. */ | |
637 | static struct coding_system coding_categories[coding_category_max]; | |
638 | ||
df7492f9 KH |
639 | /*** Commonly used macros and functions ***/ |
640 | ||
641 | #ifndef min | |
642 | #define min(a, b) ((a) < (b) ? (a) : (b)) | |
643 | #endif | |
644 | #ifndef max | |
645 | #define max(a, b) ((a) > (b) ? (a) : (b)) | |
646 | #endif | |
4ed46869 | 647 | |
24a73b0a KH |
648 | #define CODING_GET_INFO(coding, attrs, charset_list) \ |
649 | do { \ | |
650 | (attrs) = CODING_ID_ATTRS ((coding)->id); \ | |
651 | (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \ | |
df7492f9 | 652 | } while (0) |
4ed46869 | 653 | |
4ed46869 | 654 | |
df7492f9 KH |
655 | /* Safely get one byte from the source text pointed by SRC which ends |
656 | at SRC_END, and set C to that byte. If there are not enough bytes | |
f10fe38f PE |
657 | in the source, it jumps to 'no_more_source'. If MULTIBYTEP, |
658 | and a multibyte character is found at SRC, set C to the | |
065e3595 KH |
659 | negative value of the character code. The caller should declare |
660 | and set these variables appropriately in advance: | |
661 | src, src_end, multibytep */ | |
aa72b389 | 662 | |
065e3595 KH |
663 | #define ONE_MORE_BYTE(c) \ |
664 | do { \ | |
665 | if (src == src_end) \ | |
666 | { \ | |
667 | if (src_base < src) \ | |
668 | record_conversion_result \ | |
669 | (coding, CODING_RESULT_INSUFFICIENT_SRC); \ | |
670 | goto no_more_source; \ | |
671 | } \ | |
672 | c = *src++; \ | |
673 | if (multibytep && (c & 0x80)) \ | |
674 | { \ | |
675 | if ((c & 0xFE) == 0xC0) \ | |
676 | c = ((c & 1) << 6) | *src++; \ | |
677 | else \ | |
678 | { \ | |
35befdaa KH |
679 | src--; \ |
680 | c = - string_char (src, &src, NULL); \ | |
065e3595 KH |
681 | record_conversion_result \ |
682 | (coding, CODING_RESULT_INVALID_SRC); \ | |
683 | } \ | |
684 | } \ | |
685 | consumed_chars++; \ | |
aa72b389 KH |
686 | } while (0) |
687 | ||
f56a4450 | 688 | /* Safely get two bytes from the source text pointed by SRC which ends |
220eeac9 KH |
689 | at SRC_END, and set C1 and C2 to those bytes while skipping the |
690 | heading multibyte characters. If there are not enough bytes in the | |
f10fe38f | 691 | source, it jumps to 'no_more_source'. If MULTIBYTEP and |
220eeac9 KH |
692 | a multibyte character is found for C2, set C2 to the negative value |
693 | of the character code. The caller should declare and set these | |
694 | variables appropriately in advance: | |
f56a4450 KH |
695 | src, src_end, multibytep |
696 | It is intended that this macro is used in detect_coding_utf_16. */ | |
697 | ||
220eeac9 KH |
698 | #define TWO_MORE_BYTES(c1, c2) \ |
699 | do { \ | |
700 | do { \ | |
701 | if (src == src_end) \ | |
702 | goto no_more_source; \ | |
703 | c1 = *src++; \ | |
704 | if (multibytep && (c1 & 0x80)) \ | |
705 | { \ | |
706 | if ((c1 & 0xFE) == 0xC0) \ | |
707 | c1 = ((c1 & 1) << 6) | *src++; \ | |
708 | else \ | |
709 | { \ | |
710 | src += BYTES_BY_CHAR_HEAD (c1) - 1; \ | |
711 | c1 = -1; \ | |
712 | } \ | |
713 | } \ | |
714 | } while (c1 < 0); \ | |
715 | if (src == src_end) \ | |
716 | goto no_more_source; \ | |
717 | c2 = *src++; \ | |
718 | if (multibytep && (c2 & 0x80)) \ | |
719 | { \ | |
720 | if ((c2 & 0xFE) == 0xC0) \ | |
721 | c2 = ((c2 & 1) << 6) | *src++; \ | |
722 | else \ | |
723 | c2 = -1; \ | |
724 | } \ | |
f56a4450 KH |
725 | } while (0) |
726 | ||
aa72b389 | 727 | |
df7492f9 KH |
728 | /* Store a byte C in the place pointed by DST and increment DST to the |
729 | next free point, and increment PRODUCED_CHARS. The caller should | |
730 | assure that C is 0..127, and declare and set the variable `dst' | |
731 | appropriately in advance. | |
732 | */ | |
aa72b389 KH |
733 | |
734 | ||
df7492f9 KH |
735 | #define EMIT_ONE_ASCII_BYTE(c) \ |
736 | do { \ | |
737 | produced_chars++; \ | |
738 | *dst++ = (c); \ | |
b6871cc7 | 739 | } while (0) |
aa72b389 KH |
740 | |
741 | ||
ad1746f5 | 742 | /* Like EMIT_ONE_ASCII_BYTE but store two bytes; C1 and C2. */ |
aa72b389 | 743 | |
df7492f9 KH |
744 | #define EMIT_TWO_ASCII_BYTES(c1, c2) \ |
745 | do { \ | |
746 | produced_chars += 2; \ | |
747 | *dst++ = (c1), *dst++ = (c2); \ | |
748 | } while (0) | |
aa72b389 KH |
749 | |
750 | ||
df7492f9 | 751 | /* Store a byte C in the place pointed by DST and increment DST to the |
f10fe38f PE |
752 | next free point, and increment PRODUCED_CHARS. If MULTIBYTEP, |
753 | store in an appropriate multibyte form. The caller should | |
df7492f9 KH |
754 | declare and set the variables `dst' and `multibytep' appropriately |
755 | in advance. */ | |
756 | ||
757 | #define EMIT_ONE_BYTE(c) \ | |
758 | do { \ | |
759 | produced_chars++; \ | |
760 | if (multibytep) \ | |
761 | { \ | |
b25d760e | 762 | unsigned ch = (c); \ |
df7492f9 KH |
763 | if (ch >= 0x80) \ |
764 | ch = BYTE8_TO_CHAR (ch); \ | |
765 | CHAR_STRING_ADVANCE (ch, dst); \ | |
766 | } \ | |
767 | else \ | |
768 | *dst++ = (c); \ | |
aa72b389 | 769 | } while (0) |
aa72b389 | 770 | |
aa72b389 | 771 | |
df7492f9 | 772 | /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */ |
aa72b389 | 773 | |
e19c3639 KH |
774 | #define EMIT_TWO_BYTES(c1, c2) \ |
775 | do { \ | |
776 | produced_chars += 2; \ | |
777 | if (multibytep) \ | |
778 | { \ | |
b25d760e | 779 | unsigned ch; \ |
e19c3639 KH |
780 | \ |
781 | ch = (c1); \ | |
782 | if (ch >= 0x80) \ | |
783 | ch = BYTE8_TO_CHAR (ch); \ | |
784 | CHAR_STRING_ADVANCE (ch, dst); \ | |
785 | ch = (c2); \ | |
786 | if (ch >= 0x80) \ | |
787 | ch = BYTE8_TO_CHAR (ch); \ | |
788 | CHAR_STRING_ADVANCE (ch, dst); \ | |
789 | } \ | |
790 | else \ | |
791 | { \ | |
792 | *dst++ = (c1); \ | |
793 | *dst++ = (c2); \ | |
794 | } \ | |
aa72b389 KH |
795 | } while (0) |
796 | ||
797 | ||
df7492f9 KH |
798 | #define EMIT_THREE_BYTES(c1, c2, c3) \ |
799 | do { \ | |
800 | EMIT_ONE_BYTE (c1); \ | |
801 | EMIT_TWO_BYTES (c2, c3); \ | |
802 | } while (0) | |
aa72b389 | 803 | |
aa72b389 | 804 | |
df7492f9 KH |
805 | #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \ |
806 | do { \ | |
807 | EMIT_TWO_BYTES (c1, c2); \ | |
808 | EMIT_TWO_BYTES (c3, c4); \ | |
809 | } while (0) | |
aa72b389 | 810 | |
aa72b389 | 811 | |
065e3595 KH |
812 | static void |
813 | record_conversion_result (struct coding_system *coding, | |
814 | enum coding_result_code result) | |
815 | { | |
816 | coding->result = result; | |
817 | switch (result) | |
818 | { | |
819 | case CODING_RESULT_INSUFFICIENT_SRC: | |
820 | Vlast_code_conversion_error = Qinsufficient_source; | |
821 | break; | |
065e3595 KH |
822 | case CODING_RESULT_INVALID_SRC: |
823 | Vlast_code_conversion_error = Qinvalid_source; | |
824 | break; | |
825 | case CODING_RESULT_INTERRUPT: | |
826 | Vlast_code_conversion_error = Qinterrupted; | |
827 | break; | |
ebaf11b6 KH |
828 | case CODING_RESULT_INSUFFICIENT_DST: |
829 | /* Don't record this error in Vlast_code_conversion_error | |
830 | because it happens just temporarily and is resolved when the | |
831 | whole conversion is finished. */ | |
832 | break; | |
409ea3a1 AS |
833 | case CODING_RESULT_SUCCESS: |
834 | break; | |
35befdaa KH |
835 | default: |
836 | Vlast_code_conversion_error = intern ("Unknown error"); | |
065e3595 KH |
837 | } |
838 | } | |
839 | ||
5eb05ea3 KH |
840 | /* These wrapper macros are used to preserve validity of pointers into |
841 | buffer text across calls to decode_char, encode_char, etc, which | |
842 | could cause relocation of buffers if it loads a charset map, | |
843 | because loading a charset map allocates large structures. */ | |
844 | ||
df7492f9 KH |
845 | #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \ |
846 | do { \ | |
8f50130c | 847 | ptrdiff_t offset; \ |
5eb05ea3 | 848 | \ |
df7492f9 KH |
849 | charset_map_loaded = 0; \ |
850 | c = DECODE_CHAR (charset, code); \ | |
5eb05ea3 | 851 | if (charset_map_loaded \ |
c1892f11 | 852 | && (offset = coding_change_source (coding))) \ |
df7492f9 | 853 | { \ |
df7492f9 KH |
854 | src += offset; \ |
855 | src_base += offset; \ | |
856 | src_end += offset; \ | |
857 | } \ | |
aa72b389 KH |
858 | } while (0) |
859 | ||
5eb05ea3 KH |
860 | #define CODING_ENCODE_CHAR(coding, dst, dst_end, charset, c, code) \ |
861 | do { \ | |
8f50130c | 862 | ptrdiff_t offset; \ |
5eb05ea3 KH |
863 | \ |
864 | charset_map_loaded = 0; \ | |
865 | code = ENCODE_CHAR (charset, c); \ | |
866 | if (charset_map_loaded \ | |
c1892f11 | 867 | && (offset = coding_change_destination (coding))) \ |
5eb05ea3 KH |
868 | { \ |
869 | dst += offset; \ | |
870 | dst_end += offset; \ | |
871 | } \ | |
872 | } while (0) | |
873 | ||
874 | #define CODING_CHAR_CHARSET(coding, dst, dst_end, c, charset_list, code_return, charset) \ | |
875 | do { \ | |
8f50130c | 876 | ptrdiff_t offset; \ |
5eb05ea3 KH |
877 | \ |
878 | charset_map_loaded = 0; \ | |
879 | charset = char_charset (c, charset_list, code_return); \ | |
880 | if (charset_map_loaded \ | |
c1892f11 | 881 | && (offset = coding_change_destination (coding))) \ |
5eb05ea3 KH |
882 | { \ |
883 | dst += offset; \ | |
884 | dst_end += offset; \ | |
885 | } \ | |
886 | } while (0) | |
887 | ||
888 | #define CODING_CHAR_CHARSET_P(coding, dst, dst_end, c, charset, result) \ | |
889 | do { \ | |
8f50130c | 890 | ptrdiff_t offset; \ |
5eb05ea3 KH |
891 | \ |
892 | charset_map_loaded = 0; \ | |
893 | result = CHAR_CHARSET_P (c, charset); \ | |
894 | if (charset_map_loaded \ | |
c1892f11 | 895 | && (offset = coding_change_destination (coding))) \ |
5eb05ea3 KH |
896 | { \ |
897 | dst += offset; \ | |
898 | dst_end += offset; \ | |
899 | } \ | |
900 | } while (0) | |
901 | ||
aa72b389 | 902 | |
119852e7 KH |
903 | /* If there are at least BYTES length of room at dst, allocate memory |
904 | for coding->destination and update dst and dst_end. We don't have | |
905 | to take care of coding->source which will be relocated. It is | |
906 | handled by calling coding_set_source in encode_coding. */ | |
907 | ||
df7492f9 KH |
908 | #define ASSURE_DESTINATION(bytes) \ |
909 | do { \ | |
910 | if (dst + (bytes) >= dst_end) \ | |
911 | { \ | |
d311d28c | 912 | ptrdiff_t more_bytes = charbuf_end - charbuf + (bytes); \ |
df7492f9 KH |
913 | \ |
914 | dst = alloc_destination (coding, more_bytes, dst); \ | |
915 | dst_end = coding->destination + coding->dst_bytes; \ | |
916 | } \ | |
917 | } while (0) | |
aa72b389 | 918 | |
aa72b389 | 919 | |
db274c7a | 920 | /* Store multibyte form of the character C in P, and advance P to the |
eedec3ee EZ |
921 | end of the multibyte form. This used to be like CHAR_STRING_ADVANCE |
922 | without ever calling MAYBE_UNIFY_CHAR, but nowadays we don't call | |
923 | MAYBE_UNIFY_CHAR in CHAR_STRING_ADVANCE. */ | |
db274c7a | 924 | |
eedec3ee | 925 | #define CHAR_STRING_ADVANCE_NO_UNIFY(c, p) CHAR_STRING_ADVANCE(c, p) |
db274c7a KH |
926 | |
927 | /* Return the character code of character whose multibyte form is at | |
eedec3ee EZ |
928 | P, and advance P to the end of the multibyte form. This used to be |
929 | like STRING_CHAR_ADVANCE without ever calling MAYBE_UNIFY_CHAR, but | |
930 | nowadays STRING_CHAR_ADVANCE doesn't call MAYBE_UNIFY_CHAR. */ | |
db274c7a | 931 | |
eedec3ee | 932 | #define STRING_CHAR_ADVANCE_NO_UNIFY(p) STRING_CHAR_ADVANCE(p) |
aa72b389 | 933 | |
c1892f11 | 934 | /* Set coding->source from coding->src_object. */ |
5eb05ea3 | 935 | |
c1892f11 | 936 | static void |
971de7fb | 937 | coding_set_source (struct coding_system *coding) |
aa72b389 | 938 | { |
df7492f9 KH |
939 | if (BUFFERP (coding->src_object)) |
940 | { | |
2cb26057 | 941 | struct buffer *buf = XBUFFER (coding->src_object); |
aa72b389 | 942 | |
df7492f9 | 943 | if (coding->src_pos < 0) |
2cb26057 | 944 | coding->source = BUF_GAP_END_ADDR (buf) + coding->src_pos_byte; |
df7492f9 | 945 | else |
2cb26057 | 946 | coding->source = BUF_BYTE_ADDRESS (buf, coding->src_pos_byte); |
aa72b389 | 947 | } |
df7492f9 | 948 | else if (STRINGP (coding->src_object)) |
aa72b389 | 949 | { |
8f924df7 | 950 | coding->source = SDATA (coding->src_object) + coding->src_pos_byte; |
aa72b389 | 951 | } |
df7492f9 | 952 | else |
f38b440c PE |
953 | { |
954 | /* Otherwise, the source is C string and is never relocated | |
955 | automatically. Thus we don't have to update anything. */ | |
956 | } | |
df7492f9 | 957 | } |
aa72b389 | 958 | |
5eb05ea3 | 959 | |
c1892f11 PE |
960 | /* Set coding->source from coding->src_object, and return how many |
961 | bytes coding->source was changed. */ | |
5eb05ea3 | 962 | |
8f50130c | 963 | static ptrdiff_t |
c1892f11 | 964 | coding_change_source (struct coding_system *coding) |
df7492f9 | 965 | { |
c1892f11 PE |
966 | const unsigned char *orig = coding->source; |
967 | coding_set_source (coding); | |
968 | return coding->source - orig; | |
969 | } | |
970 | ||
5eb05ea3 | 971 | |
c1892f11 PE |
972 | /* Set coding->destination from coding->dst_object. */ |
973 | ||
974 | static void | |
975 | coding_set_destination (struct coding_system *coding) | |
976 | { | |
df7492f9 | 977 | if (BUFFERP (coding->dst_object)) |
aa72b389 | 978 | { |
a0241d01 | 979 | if (BUFFERP (coding->src_object) && coding->src_pos < 0) |
aa72b389 | 980 | { |
13818c30 | 981 | coding->destination = BEG_ADDR + coding->dst_pos_byte - BEG_BYTE; |
28f67a95 KH |
982 | coding->dst_bytes = (GAP_END_ADDR |
983 | - (coding->src_bytes - coding->consumed) | |
984 | - coding->destination); | |
aa72b389 | 985 | } |
df7492f9 | 986 | else |
28f67a95 KH |
987 | { |
988 | /* We are sure that coding->dst_pos_byte is before the gap | |
989 | of the buffer. */ | |
990 | coding->destination = (BUF_BEG_ADDR (XBUFFER (coding->dst_object)) | |
13818c30 | 991 | + coding->dst_pos_byte - BEG_BYTE); |
28f67a95 KH |
992 | coding->dst_bytes = (BUF_GAP_END_ADDR (XBUFFER (coding->dst_object)) |
993 | - coding->destination); | |
994 | } | |
df7492f9 KH |
995 | } |
996 | else | |
f38b440c PE |
997 | { |
998 | /* Otherwise, the destination is C string and is never relocated | |
999 | automatically. Thus we don't have to update anything. */ | |
1000 | } | |
c1892f11 PE |
1001 | } |
1002 | ||
1003 | ||
1004 | /* Set coding->destination from coding->dst_object, and return how | |
1005 | many bytes coding->destination was changed. */ | |
1006 | ||
1007 | static ptrdiff_t | |
1008 | coding_change_destination (struct coding_system *coding) | |
1009 | { | |
1010 | const unsigned char *orig = coding->destination; | |
1011 | coding_set_destination (coding); | |
5eb05ea3 | 1012 | return coding->destination - orig; |
df7492f9 KH |
1013 | } |
1014 | ||
1015 | ||
1016 | static void | |
d311d28c | 1017 | coding_alloc_by_realloc (struct coding_system *coding, ptrdiff_t bytes) |
df7492f9 | 1018 | { |
c9d624c6 | 1019 | if (STRING_BYTES_BOUND - coding->dst_bytes < bytes) |
d1f3d2af | 1020 | string_overflow (); |
38182d90 PE |
1021 | coding->destination = xrealloc (coding->destination, |
1022 | coding->dst_bytes + bytes); | |
df7492f9 KH |
1023 | coding->dst_bytes += bytes; |
1024 | } | |
1025 | ||
1026 | static void | |
cf84bb53 | 1027 | coding_alloc_by_making_gap (struct coding_system *coding, |
d311d28c | 1028 | ptrdiff_t gap_head_used, ptrdiff_t bytes) |
df7492f9 | 1029 | { |
db274c7a | 1030 | if (EQ (coding->src_object, coding->dst_object)) |
df7492f9 | 1031 | { |
db274c7a KH |
1032 | /* The gap may contain the produced data at the head and not-yet |
1033 | consumed data at the tail. To preserve those data, we at | |
1034 | first make the gap size to zero, then increase the gap | |
1035 | size. */ | |
d311d28c | 1036 | ptrdiff_t add = GAP_SIZE; |
db274c7a KH |
1037 | |
1038 | GPT += gap_head_used, GPT_BYTE += gap_head_used; | |
1039 | GAP_SIZE = 0; ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add; | |
df7492f9 KH |
1040 | make_gap (bytes); |
1041 | GAP_SIZE += add; ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add; | |
db274c7a | 1042 | GPT -= gap_head_used, GPT_BYTE -= gap_head_used; |
df7492f9 | 1043 | } |
730fff51 | 1044 | else |
eefd7278 | 1045 | make_gap_1 (XBUFFER (coding->dst_object), bytes); |
df7492f9 | 1046 | } |
8f924df7 | 1047 | |
df7492f9 KH |
1048 | |
1049 | static unsigned char * | |
d311d28c | 1050 | alloc_destination (struct coding_system *coding, ptrdiff_t nbytes, |
cf84bb53 | 1051 | unsigned char *dst) |
df7492f9 | 1052 | { |
d311d28c | 1053 | ptrdiff_t offset = dst - coding->destination; |
df7492f9 KH |
1054 | |
1055 | if (BUFFERP (coding->dst_object)) | |
db274c7a KH |
1056 | { |
1057 | struct buffer *buf = XBUFFER (coding->dst_object); | |
1058 | ||
1059 | coding_alloc_by_making_gap (coding, dst - BUF_GPT_ADDR (buf), nbytes); | |
1060 | } | |
aa72b389 | 1061 | else |
df7492f9 | 1062 | coding_alloc_by_realloc (coding, nbytes); |
df7492f9 KH |
1063 | coding_set_destination (coding); |
1064 | dst = coding->destination + offset; | |
1065 | return dst; | |
1066 | } | |
aa72b389 | 1067 | |
ff0dacd7 KH |
1068 | /** Macros for annotations. */ |
1069 | ||
ff0dacd7 KH |
1070 | /* An annotation data is stored in the array coding->charbuf in this |
1071 | format: | |
69a80ea3 | 1072 | [ -LENGTH ANNOTATION_MASK NCHARS ... ] |
ff0dacd7 KH |
1073 | LENGTH is the number of elements in the annotation. |
1074 | ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK. | |
69a80ea3 | 1075 | NCHARS is the number of characters in the text annotated. |
ff0dacd7 KH |
1076 | |
1077 | The format of the following elements depend on ANNOTATION_MASK. | |
1078 | ||
1079 | In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements | |
1080 | follows: | |
e951386e KH |
1081 | ... NBYTES METHOD [ COMPOSITION-COMPONENTS ... ] |
1082 | ||
1083 | NBYTES is the number of bytes specified in the header part of | |
1084 | old-style emacs-mule encoding, or 0 for the other kind of | |
1085 | composition. | |
1086 | ||
ff0dacd7 | 1087 | METHOD is one of enum composition_method. |
e951386e | 1088 | |
ad1746f5 | 1089 | Optional COMPOSITION-COMPONENTS are characters and composition |
ff0dacd7 KH |
1090 | rules. |
1091 | ||
1092 | In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID | |
e951386e KH |
1093 | follows. |
1094 | ||
1095 | If ANNOTATION_MASK is 0, this annotation is just a space holder to | |
1096 | recover from an invalid annotation, and should be skipped by | |
1097 | produce_annotation. */ | |
1098 | ||
1099 | /* Maximum length of the header of annotation data. */ | |
1100 | #define MAX_ANNOTATION_LENGTH 5 | |
ff0dacd7 | 1101 | |
69a80ea3 | 1102 | #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \ |
ff0dacd7 KH |
1103 | do { \ |
1104 | *(buf)++ = -(len); \ | |
1105 | *(buf)++ = (mask); \ | |
69a80ea3 | 1106 | *(buf)++ = (nchars); \ |
ff0dacd7 KH |
1107 | coding->annotated = 1; \ |
1108 | } while (0); | |
1109 | ||
e951386e | 1110 | #define ADD_COMPOSITION_DATA(buf, nchars, nbytes, method) \ |
69a80ea3 | 1111 | do { \ |
e951386e KH |
1112 | ADD_ANNOTATION_DATA (buf, 5, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \ |
1113 | *buf++ = nbytes; \ | |
69a80ea3 | 1114 | *buf++ = method; \ |
ff0dacd7 KH |
1115 | } while (0) |
1116 | ||
1117 | ||
69a80ea3 KH |
1118 | #define ADD_CHARSET_DATA(buf, nchars, id) \ |
1119 | do { \ | |
1120 | ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \ | |
1121 | *buf++ = id; \ | |
ff0dacd7 KH |
1122 | } while (0) |
1123 | ||
df7492f9 KH |
1124 | \f |
1125 | /*** 2. Emacs' internal format (emacs-utf-8) ***/ | |
1126 | ||
1127 | ||
1128 | ||
1129 | \f | |
1130 | /*** 3. UTF-8 ***/ | |
1131 | ||
1132 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
f10fe38f | 1133 | Return true if a text is encoded in UTF-8. */ |
df7492f9 KH |
1134 | |
1135 | #define UTF_8_1_OCTET_P(c) ((c) < 0x80) | |
1136 | #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80) | |
1137 | #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0) | |
1138 | #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0) | |
1139 | #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0) | |
1140 | #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8) | |
1141 | ||
a470d443 KH |
1142 | #define UTF_8_BOM_1 0xEF |
1143 | #define UTF_8_BOM_2 0xBB | |
1144 | #define UTF_8_BOM_3 0xBF | |
1145 | ||
f10fe38f | 1146 | static bool |
cf84bb53 JB |
1147 | detect_coding_utf_8 (struct coding_system *coding, |
1148 | struct coding_detection_info *detect_info) | |
df7492f9 | 1149 | { |
065e3595 | 1150 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 1151 | const unsigned char *src_end = coding->source + coding->src_bytes; |
f10fe38f | 1152 | bool multibytep = coding->src_multibyte; |
d311d28c | 1153 | ptrdiff_t consumed_chars = 0; |
f10fe38f PE |
1154 | bool bom_found = 0; |
1155 | bool found = 0; | |
df7492f9 | 1156 | |
ff0dacd7 | 1157 | detect_info->checked |= CATEGORY_MASK_UTF_8; |
df7492f9 KH |
1158 | /* A coding system of this category is always ASCII compatible. */ |
1159 | src += coding->head_ascii; | |
1160 | ||
1161 | while (1) | |
aa72b389 | 1162 | { |
df7492f9 | 1163 | int c, c1, c2, c3, c4; |
aa72b389 | 1164 | |
065e3595 | 1165 | src_base = src; |
df7492f9 | 1166 | ONE_MORE_BYTE (c); |
065e3595 | 1167 | if (c < 0 || UTF_8_1_OCTET_P (c)) |
df7492f9 KH |
1168 | continue; |
1169 | ONE_MORE_BYTE (c1); | |
065e3595 | 1170 | if (c1 < 0 || ! UTF_8_EXTRA_OCTET_P (c1)) |
df7492f9 KH |
1171 | break; |
1172 | if (UTF_8_2_OCTET_LEADING_P (c)) | |
aa72b389 | 1173 | { |
a470d443 | 1174 | found = 1; |
df7492f9 | 1175 | continue; |
aa72b389 | 1176 | } |
df7492f9 | 1177 | ONE_MORE_BYTE (c2); |
065e3595 | 1178 | if (c2 < 0 || ! UTF_8_EXTRA_OCTET_P (c2)) |
df7492f9 KH |
1179 | break; |
1180 | if (UTF_8_3_OCTET_LEADING_P (c)) | |
aa72b389 | 1181 | { |
a470d443 KH |
1182 | found = 1; |
1183 | if (src_base == coding->source | |
1184 | && c == UTF_8_BOM_1 && c1 == UTF_8_BOM_2 && c2 == UTF_8_BOM_3) | |
1185 | bom_found = 1; | |
df7492f9 | 1186 | continue; |
aa72b389 | 1187 | } |
df7492f9 | 1188 | ONE_MORE_BYTE (c3); |
065e3595 | 1189 | if (c3 < 0 || ! UTF_8_EXTRA_OCTET_P (c3)) |
df7492f9 KH |
1190 | break; |
1191 | if (UTF_8_4_OCTET_LEADING_P (c)) | |
aa72b389 | 1192 | { |
a470d443 | 1193 | found = 1; |
df7492f9 KH |
1194 | continue; |
1195 | } | |
1196 | ONE_MORE_BYTE (c4); | |
065e3595 | 1197 | if (c4 < 0 || ! UTF_8_EXTRA_OCTET_P (c4)) |
df7492f9 KH |
1198 | break; |
1199 | if (UTF_8_5_OCTET_LEADING_P (c)) | |
1200 | { | |
a470d443 | 1201 | found = 1; |
df7492f9 KH |
1202 | continue; |
1203 | } | |
1204 | break; | |
aa72b389 | 1205 | } |
ff0dacd7 | 1206 | detect_info->rejected |= CATEGORY_MASK_UTF_8; |
df7492f9 | 1207 | return 0; |
aa72b389 | 1208 | |
df7492f9 | 1209 | no_more_source: |
065e3595 | 1210 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) |
aa72b389 | 1211 | { |
ff0dacd7 | 1212 | detect_info->rejected |= CATEGORY_MASK_UTF_8; |
89528eb3 | 1213 | return 0; |
aa72b389 | 1214 | } |
a470d443 KH |
1215 | if (bom_found) |
1216 | { | |
1217 | /* The first character 0xFFFE doesn't necessarily mean a BOM. */ | |
1218 | detect_info->found |= CATEGORY_MASK_UTF_8_SIG | CATEGORY_MASK_UTF_8_NOSIG; | |
1219 | } | |
1220 | else | |
1221 | { | |
1222 | detect_info->rejected |= CATEGORY_MASK_UTF_8_SIG; | |
0e17387a KH |
1223 | if (found) |
1224 | detect_info->found |= CATEGORY_MASK_UTF_8_NOSIG; | |
a470d443 | 1225 | } |
ff0dacd7 | 1226 | return 1; |
aa72b389 KH |
1227 | } |
1228 | ||
4ed46869 | 1229 | |
b73bfc1c | 1230 | static void |
971de7fb | 1231 | decode_coding_utf_8 (struct coding_system *coding) |
b73bfc1c | 1232 | { |
8f924df7 KH |
1233 | const unsigned char *src = coding->source + coding->consumed; |
1234 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
1235 | const unsigned char *src_base; | |
69a80ea3 KH |
1236 | int *charbuf = coding->charbuf + coding->charbuf_used; |
1237 | int *charbuf_end = coding->charbuf + coding->charbuf_size; | |
d311d28c | 1238 | ptrdiff_t consumed_chars = 0, consumed_chars_base = 0; |
f10fe38f | 1239 | bool multibytep = coding->src_multibyte; |
a470d443 | 1240 | enum utf_bom_type bom = CODING_UTF_8_BOM (coding); |
f10fe38f PE |
1241 | bool eol_dos |
1242 | = !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); | |
119852e7 | 1243 | int byte_after_cr = -1; |
4ed46869 | 1244 | |
a470d443 KH |
1245 | if (bom != utf_without_bom) |
1246 | { | |
1247 | int c1, c2, c3; | |
1248 | ||
1249 | src_base = src; | |
1250 | ONE_MORE_BYTE (c1); | |
1251 | if (! UTF_8_3_OCTET_LEADING_P (c1)) | |
1252 | src = src_base; | |
1253 | else | |
1254 | { | |
159bd5a2 | 1255 | ONE_MORE_BYTE (c2); |
a470d443 KH |
1256 | if (! UTF_8_EXTRA_OCTET_P (c2)) |
1257 | src = src_base; | |
1258 | else | |
1259 | { | |
159bd5a2 | 1260 | ONE_MORE_BYTE (c3); |
a470d443 KH |
1261 | if (! UTF_8_EXTRA_OCTET_P (c3)) |
1262 | src = src_base; | |
1263 | else | |
1264 | { | |
1265 | if ((c1 != UTF_8_BOM_1) | |
1266 | || (c2 != UTF_8_BOM_2) || (c3 != UTF_8_BOM_3)) | |
1267 | src = src_base; | |
1268 | else | |
1269 | CODING_UTF_8_BOM (coding) = utf_without_bom; | |
1270 | } | |
1271 | } | |
1272 | } | |
1273 | } | |
1274 | CODING_UTF_8_BOM (coding) = utf_without_bom; | |
1275 | ||
df7492f9 | 1276 | while (1) |
b73bfc1c | 1277 | { |
df7492f9 | 1278 | int c, c1, c2, c3, c4, c5; |
ec6d2bb8 | 1279 | |
df7492f9 KH |
1280 | src_base = src; |
1281 | consumed_chars_base = consumed_chars; | |
4af310db | 1282 | |
df7492f9 | 1283 | if (charbuf >= charbuf_end) |
b71f6f73 KH |
1284 | { |
1285 | if (byte_after_cr >= 0) | |
1286 | src_base--; | |
1287 | break; | |
1288 | } | |
df7492f9 | 1289 | |
119852e7 KH |
1290 | if (byte_after_cr >= 0) |
1291 | c1 = byte_after_cr, byte_after_cr = -1; | |
1292 | else | |
1293 | ONE_MORE_BYTE (c1); | |
065e3595 KH |
1294 | if (c1 < 0) |
1295 | { | |
1296 | c = - c1; | |
1297 | } | |
1a4990fb | 1298 | else if (UTF_8_1_OCTET_P (c1)) |
df7492f9 | 1299 | { |
2735d060 | 1300 | if (eol_dos && c1 == '\r') |
119852e7 | 1301 | ONE_MORE_BYTE (byte_after_cr); |
df7492f9 | 1302 | c = c1; |
4af310db | 1303 | } |
df7492f9 | 1304 | else |
4af310db | 1305 | { |
df7492f9 | 1306 | ONE_MORE_BYTE (c2); |
065e3595 | 1307 | if (c2 < 0 || ! UTF_8_EXTRA_OCTET_P (c2)) |
df7492f9 KH |
1308 | goto invalid_code; |
1309 | if (UTF_8_2_OCTET_LEADING_P (c1)) | |
4af310db | 1310 | { |
b0edb2c5 DL |
1311 | c = ((c1 & 0x1F) << 6) | (c2 & 0x3F); |
1312 | /* Reject overlong sequences here and below. Encoders | |
1313 | producing them are incorrect, they can be misleading, | |
1314 | and they mess up read/write invariance. */ | |
1315 | if (c < 128) | |
1316 | goto invalid_code; | |
4af310db | 1317 | } |
df7492f9 | 1318 | else |
aa72b389 | 1319 | { |
df7492f9 | 1320 | ONE_MORE_BYTE (c3); |
065e3595 | 1321 | if (c3 < 0 || ! UTF_8_EXTRA_OCTET_P (c3)) |
df7492f9 KH |
1322 | goto invalid_code; |
1323 | if (UTF_8_3_OCTET_LEADING_P (c1)) | |
b0edb2c5 DL |
1324 | { |
1325 | c = (((c1 & 0xF) << 12) | |
1326 | | ((c2 & 0x3F) << 6) | (c3 & 0x3F)); | |
72fe1301 DL |
1327 | if (c < 0x800 |
1328 | || (c >= 0xd800 && c < 0xe000)) /* surrogates (invalid) */ | |
b0edb2c5 DL |
1329 | goto invalid_code; |
1330 | } | |
df7492f9 KH |
1331 | else |
1332 | { | |
1333 | ONE_MORE_BYTE (c4); | |
065e3595 | 1334 | if (c4 < 0 || ! UTF_8_EXTRA_OCTET_P (c4)) |
df7492f9 KH |
1335 | goto invalid_code; |
1336 | if (UTF_8_4_OCTET_LEADING_P (c1)) | |
b0edb2c5 | 1337 | { |
df7492f9 KH |
1338 | c = (((c1 & 0x7) << 18) | ((c2 & 0x3F) << 12) |
1339 | | ((c3 & 0x3F) << 6) | (c4 & 0x3F)); | |
b0edb2c5 DL |
1340 | if (c < 0x10000) |
1341 | goto invalid_code; | |
1342 | } | |
df7492f9 KH |
1343 | else |
1344 | { | |
1345 | ONE_MORE_BYTE (c5); | |
065e3595 | 1346 | if (c5 < 0 || ! UTF_8_EXTRA_OCTET_P (c5)) |
df7492f9 KH |
1347 | goto invalid_code; |
1348 | if (UTF_8_5_OCTET_LEADING_P (c1)) | |
1349 | { | |
1350 | c = (((c1 & 0x3) << 24) | ((c2 & 0x3F) << 18) | |
1351 | | ((c3 & 0x3F) << 12) | ((c4 & 0x3F) << 6) | |
1352 | | (c5 & 0x3F)); | |
b0edb2c5 | 1353 | if ((c > MAX_CHAR) || (c < 0x200000)) |
df7492f9 KH |
1354 | goto invalid_code; |
1355 | } | |
1356 | else | |
1357 | goto invalid_code; | |
1358 | } | |
1359 | } | |
aa72b389 | 1360 | } |
b73bfc1c | 1361 | } |
df7492f9 KH |
1362 | |
1363 | *charbuf++ = c; | |
1364 | continue; | |
1365 | ||
1366 | invalid_code: | |
1367 | src = src_base; | |
1368 | consumed_chars = consumed_chars_base; | |
1369 | ONE_MORE_BYTE (c); | |
1370 | *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); | |
1371 | coding->errors++; | |
aa72b389 KH |
1372 | } |
1373 | ||
df7492f9 KH |
1374 | no_more_source: |
1375 | coding->consumed_char += consumed_chars_base; | |
1376 | coding->consumed = src_base - coding->source; | |
1377 | coding->charbuf_used = charbuf - coding->charbuf; | |
1378 | } | |
1379 | ||
1380 | ||
f10fe38f | 1381 | static bool |
971de7fb | 1382 | encode_coding_utf_8 (struct coding_system *coding) |
df7492f9 | 1383 | { |
f10fe38f | 1384 | bool multibytep = coding->dst_multibyte; |
df7492f9 KH |
1385 | int *charbuf = coding->charbuf; |
1386 | int *charbuf_end = charbuf + coding->charbuf_used; | |
1387 | unsigned char *dst = coding->destination + coding->produced; | |
1388 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
d311d28c | 1389 | ptrdiff_t produced_chars = 0; |
df7492f9 KH |
1390 | int c; |
1391 | ||
a470d443 KH |
1392 | if (CODING_UTF_8_BOM (coding) == utf_with_bom) |
1393 | { | |
1394 | ASSURE_DESTINATION (3); | |
1395 | EMIT_THREE_BYTES (UTF_8_BOM_1, UTF_8_BOM_2, UTF_8_BOM_3); | |
1396 | CODING_UTF_8_BOM (coding) = utf_without_bom; | |
1397 | } | |
1398 | ||
df7492f9 | 1399 | if (multibytep) |
aa72b389 | 1400 | { |
df7492f9 KH |
1401 | int safe_room = MAX_MULTIBYTE_LENGTH * 2; |
1402 | ||
1403 | while (charbuf < charbuf_end) | |
b73bfc1c | 1404 | { |
df7492f9 | 1405 | unsigned char str[MAX_MULTIBYTE_LENGTH], *p, *pend = str; |
8f924df7 | 1406 | |
df7492f9 KH |
1407 | ASSURE_DESTINATION (safe_room); |
1408 | c = *charbuf++; | |
28f67a95 KH |
1409 | if (CHAR_BYTE8_P (c)) |
1410 | { | |
1411 | c = CHAR_TO_BYTE8 (c); | |
1412 | EMIT_ONE_BYTE (c); | |
1413 | } | |
1414 | else | |
1415 | { | |
db274c7a | 1416 | CHAR_STRING_ADVANCE_NO_UNIFY (c, pend); |
28f67a95 KH |
1417 | for (p = str; p < pend; p++) |
1418 | EMIT_ONE_BYTE (*p); | |
1419 | } | |
b73bfc1c | 1420 | } |
aa72b389 | 1421 | } |
df7492f9 KH |
1422 | else |
1423 | { | |
1424 | int safe_room = MAX_MULTIBYTE_LENGTH; | |
1425 | ||
1426 | while (charbuf < charbuf_end) | |
b73bfc1c | 1427 | { |
df7492f9 KH |
1428 | ASSURE_DESTINATION (safe_room); |
1429 | c = *charbuf++; | |
f03caae0 KH |
1430 | if (CHAR_BYTE8_P (c)) |
1431 | *dst++ = CHAR_TO_BYTE8 (c); | |
1432 | else | |
db274c7a | 1433 | CHAR_STRING_ADVANCE_NO_UNIFY (c, dst); |
df7492f9 | 1434 | produced_chars++; |
4ed46869 KH |
1435 | } |
1436 | } | |
065e3595 | 1437 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
1438 | coding->produced_char += produced_chars; |
1439 | coding->produced = dst - coding->destination; | |
1440 | return 0; | |
4ed46869 KH |
1441 | } |
1442 | ||
b73bfc1c | 1443 | |
df7492f9 | 1444 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". |
f10fe38f | 1445 | Return true if a text is encoded in one of UTF-16 based coding systems. */ |
aa72b389 | 1446 | |
df7492f9 KH |
1447 | #define UTF_16_HIGH_SURROGATE_P(val) \ |
1448 | (((val) & 0xFC00) == 0xD800) | |
1449 | ||
1450 | #define UTF_16_LOW_SURROGATE_P(val) \ | |
1451 | (((val) & 0xFC00) == 0xDC00) | |
93dec019 | 1452 | |
aa72b389 | 1453 | |
f10fe38f | 1454 | static bool |
cf84bb53 JB |
1455 | detect_coding_utf_16 (struct coding_system *coding, |
1456 | struct coding_detection_info *detect_info) | |
aa72b389 | 1457 | { |
ef1b0ba7 | 1458 | const unsigned char *src = coding->source; |
8f924df7 | 1459 | const unsigned char *src_end = coding->source + coding->src_bytes; |
f10fe38f | 1460 | bool multibytep = coding->src_multibyte; |
df7492f9 | 1461 | int c1, c2; |
aa72b389 | 1462 | |
ff0dacd7 | 1463 | detect_info->checked |= CATEGORY_MASK_UTF_16; |
ff0dacd7 | 1464 | if (coding->mode & CODING_MODE_LAST_BLOCK |
24a73b0a | 1465 | && (coding->src_chars & 1)) |
ff0dacd7 KH |
1466 | { |
1467 | detect_info->rejected |= CATEGORY_MASK_UTF_16; | |
1468 | return 0; | |
1469 | } | |
24a73b0a | 1470 | |
f56a4450 | 1471 | TWO_MORE_BYTES (c1, c2); |
df7492f9 | 1472 | if ((c1 == 0xFF) && (c2 == 0xFE)) |
aa72b389 | 1473 | { |
b49a1807 KH |
1474 | detect_info->found |= (CATEGORY_MASK_UTF_16_LE |
1475 | | CATEGORY_MASK_UTF_16_AUTO); | |
24a73b0a KH |
1476 | detect_info->rejected |= (CATEGORY_MASK_UTF_16_BE |
1477 | | CATEGORY_MASK_UTF_16_BE_NOSIG | |
1478 | | CATEGORY_MASK_UTF_16_LE_NOSIG); | |
aa72b389 | 1479 | } |
df7492f9 | 1480 | else if ((c1 == 0xFE) && (c2 == 0xFF)) |
ff0dacd7 | 1481 | { |
b49a1807 KH |
1482 | detect_info->found |= (CATEGORY_MASK_UTF_16_BE |
1483 | | CATEGORY_MASK_UTF_16_AUTO); | |
24a73b0a KH |
1484 | detect_info->rejected |= (CATEGORY_MASK_UTF_16_LE |
1485 | | CATEGORY_MASK_UTF_16_BE_NOSIG | |
1486 | | CATEGORY_MASK_UTF_16_LE_NOSIG); | |
1487 | } | |
220eeac9 | 1488 | else if (c2 < 0) |
f56a4450 KH |
1489 | { |
1490 | detect_info->rejected |= CATEGORY_MASK_UTF_16; | |
1491 | return 0; | |
1492 | } | |
2f3cbb32 | 1493 | else |
24a73b0a | 1494 | { |
2f3cbb32 KH |
1495 | /* We check the dispersion of Eth and Oth bytes where E is even and |
1496 | O is odd. If both are high, we assume binary data.*/ | |
1497 | unsigned char e[256], o[256]; | |
1498 | unsigned e_num = 1, o_num = 1; | |
1499 | ||
1500 | memset (e, 0, 256); | |
1501 | memset (o, 0, 256); | |
1502 | e[c1] = 1; | |
1503 | o[c2] = 1; | |
1504 | ||
cc13543e KH |
1505 | detect_info->rejected |= (CATEGORY_MASK_UTF_16_AUTO |
1506 | |CATEGORY_MASK_UTF_16_BE | |
1507 | | CATEGORY_MASK_UTF_16_LE); | |
2f3cbb32 | 1508 | |
7f1faf1c KH |
1509 | while ((detect_info->rejected & CATEGORY_MASK_UTF_16) |
1510 | != CATEGORY_MASK_UTF_16) | |
2f3cbb32 | 1511 | { |
f56a4450 | 1512 | TWO_MORE_BYTES (c1, c2); |
220eeac9 | 1513 | if (c2 < 0) |
f56a4450 | 1514 | break; |
2f3cbb32 KH |
1515 | if (! e[c1]) |
1516 | { | |
1517 | e[c1] = 1; | |
1518 | e_num++; | |
cc13543e KH |
1519 | if (e_num >= 128) |
1520 | detect_info->rejected |= CATEGORY_MASK_UTF_16_BE_NOSIG; | |
2f3cbb32 KH |
1521 | } |
1522 | if (! o[c2]) | |
1523 | { | |
977b85f4 | 1524 | o[c2] = 1; |
2f3cbb32 | 1525 | o_num++; |
cc13543e KH |
1526 | if (o_num >= 128) |
1527 | detect_info->rejected |= CATEGORY_MASK_UTF_16_LE_NOSIG; | |
2f3cbb32 KH |
1528 | } |
1529 | } | |
2f3cbb32 | 1530 | return 0; |
ff0dacd7 | 1531 | } |
2f3cbb32 | 1532 | |
df7492f9 | 1533 | no_more_source: |
ff0dacd7 | 1534 | return 1; |
df7492f9 | 1535 | } |
aa72b389 | 1536 | |
df7492f9 | 1537 | static void |
971de7fb | 1538 | decode_coding_utf_16 (struct coding_system *coding) |
df7492f9 | 1539 | { |
8f924df7 KH |
1540 | const unsigned char *src = coding->source + coding->consumed; |
1541 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
1542 | const unsigned char *src_base; | |
69a80ea3 | 1543 | int *charbuf = coding->charbuf + coding->charbuf_used; |
df80c7f0 KH |
1544 | /* We may produces at most 3 chars in one loop. */ |
1545 | int *charbuf_end = coding->charbuf + coding->charbuf_size - 2; | |
d311d28c | 1546 | ptrdiff_t consumed_chars = 0, consumed_chars_base = 0; |
f10fe38f | 1547 | bool multibytep = coding->src_multibyte; |
a470d443 | 1548 | enum utf_bom_type bom = CODING_UTF_16_BOM (coding); |
df7492f9 KH |
1549 | enum utf_16_endian_type endian = CODING_UTF_16_ENDIAN (coding); |
1550 | int surrogate = CODING_UTF_16_SURROGATE (coding); | |
f10fe38f PE |
1551 | bool eol_dos |
1552 | = !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); | |
119852e7 | 1553 | int byte_after_cr1 = -1, byte_after_cr2 = -1; |
df7492f9 | 1554 | |
a470d443 | 1555 | if (bom == utf_with_bom) |
aa72b389 | 1556 | { |
df7492f9 | 1557 | int c, c1, c2; |
4af310db | 1558 | |
aa72b389 | 1559 | src_base = src; |
df7492f9 KH |
1560 | ONE_MORE_BYTE (c1); |
1561 | ONE_MORE_BYTE (c2); | |
e19c3639 | 1562 | c = (c1 << 8) | c2; |
aa72b389 | 1563 | |
b49a1807 KH |
1564 | if (endian == utf_16_big_endian |
1565 | ? c != 0xFEFF : c != 0xFFFE) | |
aa72b389 | 1566 | { |
b49a1807 KH |
1567 | /* The first two bytes are not BOM. Treat them as bytes |
1568 | for a normal character. */ | |
1569 | src = src_base; | |
1570 | coding->errors++; | |
aa72b389 | 1571 | } |
a470d443 | 1572 | CODING_UTF_16_BOM (coding) = utf_without_bom; |
b49a1807 | 1573 | } |
a470d443 | 1574 | else if (bom == utf_detect_bom) |
b49a1807 KH |
1575 | { |
1576 | /* We have already tried to detect BOM and failed in | |
1577 | detect_coding. */ | |
a470d443 | 1578 | CODING_UTF_16_BOM (coding) = utf_without_bom; |
df7492f9 | 1579 | } |
aa72b389 | 1580 | |
df7492f9 KH |
1581 | while (1) |
1582 | { | |
1583 | int c, c1, c2; | |
1584 | ||
1585 | src_base = src; | |
1586 | consumed_chars_base = consumed_chars; | |
1587 | ||
df80c7f0 | 1588 | if (charbuf >= charbuf_end) |
b71f6f73 KH |
1589 | { |
1590 | if (byte_after_cr1 >= 0) | |
1591 | src_base -= 2; | |
1592 | break; | |
1593 | } | |
df7492f9 | 1594 | |
119852e7 KH |
1595 | if (byte_after_cr1 >= 0) |
1596 | c1 = byte_after_cr1, byte_after_cr1 = -1; | |
1597 | else | |
1598 | ONE_MORE_BYTE (c1); | |
065e3595 KH |
1599 | if (c1 < 0) |
1600 | { | |
1601 | *charbuf++ = -c1; | |
1602 | continue; | |
1603 | } | |
119852e7 KH |
1604 | if (byte_after_cr2 >= 0) |
1605 | c2 = byte_after_cr2, byte_after_cr2 = -1; | |
1606 | else | |
1607 | ONE_MORE_BYTE (c2); | |
065e3595 KH |
1608 | if (c2 < 0) |
1609 | { | |
1610 | *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1); | |
1611 | *charbuf++ = -c2; | |
1612 | continue; | |
1613 | } | |
df7492f9 | 1614 | c = (endian == utf_16_big_endian |
e19c3639 | 1615 | ? ((c1 << 8) | c2) : ((c2 << 8) | c1)); |
119852e7 | 1616 | |
df7492f9 | 1617 | if (surrogate) |
fd3ae0b9 | 1618 | { |
df7492f9 | 1619 | if (! UTF_16_LOW_SURROGATE_P (c)) |
fd3ae0b9 | 1620 | { |
df7492f9 KH |
1621 | if (endian == utf_16_big_endian) |
1622 | c1 = surrogate >> 8, c2 = surrogate & 0xFF; | |
1623 | else | |
1624 | c1 = surrogate & 0xFF, c2 = surrogate >> 8; | |
1625 | *charbuf++ = c1; | |
1626 | *charbuf++ = c2; | |
1627 | coding->errors++; | |
1628 | if (UTF_16_HIGH_SURROGATE_P (c)) | |
1629 | CODING_UTF_16_SURROGATE (coding) = surrogate = c; | |
fd3ae0b9 | 1630 | else |
df7492f9 | 1631 | *charbuf++ = c; |
fd3ae0b9 KH |
1632 | } |
1633 | else | |
df7492f9 KH |
1634 | { |
1635 | c = ((surrogate - 0xD800) << 10) | (c - 0xDC00); | |
1636 | CODING_UTF_16_SURROGATE (coding) = surrogate = 0; | |
29f7ffd0 | 1637 | *charbuf++ = 0x10000 + c; |
df7492f9 | 1638 | } |
fd3ae0b9 | 1639 | } |
aa72b389 | 1640 | else |
df7492f9 KH |
1641 | { |
1642 | if (UTF_16_HIGH_SURROGATE_P (c)) | |
1643 | CODING_UTF_16_SURROGATE (coding) = surrogate = c; | |
1644 | else | |
119852e7 | 1645 | { |
2735d060 | 1646 | if (eol_dos && c == '\r') |
119852e7 KH |
1647 | { |
1648 | ONE_MORE_BYTE (byte_after_cr1); | |
1649 | ONE_MORE_BYTE (byte_after_cr2); | |
1650 | } | |
1651 | *charbuf++ = c; | |
1652 | } | |
8f924df7 | 1653 | } |
aa72b389 | 1654 | } |
df7492f9 KH |
1655 | |
1656 | no_more_source: | |
1657 | coding->consumed_char += consumed_chars_base; | |
1658 | coding->consumed = src_base - coding->source; | |
1659 | coding->charbuf_used = charbuf - coding->charbuf; | |
aa72b389 | 1660 | } |
b73bfc1c | 1661 | |
f10fe38f | 1662 | static bool |
971de7fb | 1663 | encode_coding_utf_16 (struct coding_system *coding) |
df7492f9 | 1664 | { |
f10fe38f | 1665 | bool multibytep = coding->dst_multibyte; |
df7492f9 KH |
1666 | int *charbuf = coding->charbuf; |
1667 | int *charbuf_end = charbuf + coding->charbuf_used; | |
1668 | unsigned char *dst = coding->destination + coding->produced; | |
1669 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
1670 | int safe_room = 8; | |
a470d443 | 1671 | enum utf_bom_type bom = CODING_UTF_16_BOM (coding); |
f10fe38f | 1672 | bool big_endian = CODING_UTF_16_ENDIAN (coding) == utf_16_big_endian; |
d311d28c | 1673 | ptrdiff_t produced_chars = 0; |
df7492f9 | 1674 | int c; |
4ed46869 | 1675 | |
a470d443 | 1676 | if (bom != utf_without_bom) |
df7492f9 KH |
1677 | { |
1678 | ASSURE_DESTINATION (safe_room); | |
1679 | if (big_endian) | |
df7492f9 | 1680 | EMIT_TWO_BYTES (0xFE, 0xFF); |
880cf180 KH |
1681 | else |
1682 | EMIT_TWO_BYTES (0xFF, 0xFE); | |
a470d443 | 1683 | CODING_UTF_16_BOM (coding) = utf_without_bom; |
df7492f9 KH |
1684 | } |
1685 | ||
1686 | while (charbuf < charbuf_end) | |
1687 | { | |
1688 | ASSURE_DESTINATION (safe_room); | |
1689 | c = *charbuf++; | |
60afa08d | 1690 | if (c > MAX_UNICODE_CHAR) |
e19c3639 | 1691 | c = coding->default_char; |
df7492f9 KH |
1692 | |
1693 | if (c < 0x10000) | |
1694 | { | |
1695 | if (big_endian) | |
1696 | EMIT_TWO_BYTES (c >> 8, c & 0xFF); | |
1697 | else | |
1698 | EMIT_TWO_BYTES (c & 0xFF, c >> 8); | |
1699 | } | |
1700 | else | |
1701 | { | |
1702 | int c1, c2; | |
1703 | ||
1704 | c -= 0x10000; | |
1705 | c1 = (c >> 10) + 0xD800; | |
1706 | c2 = (c & 0x3FF) + 0xDC00; | |
1707 | if (big_endian) | |
1708 | EMIT_FOUR_BYTES (c1 >> 8, c1 & 0xFF, c2 >> 8, c2 & 0xFF); | |
1709 | else | |
1710 | EMIT_FOUR_BYTES (c1 & 0xFF, c1 >> 8, c2 & 0xFF, c2 >> 8); | |
1711 | } | |
1712 | } | |
065e3595 | 1713 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
1714 | coding->produced = dst - coding->destination; |
1715 | coding->produced_char += produced_chars; | |
1716 | return 0; | |
1717 | } | |
1718 | ||
1719 | \f | |
1720 | /*** 6. Old Emacs' internal format (emacs-mule) ***/ | |
1721 | ||
1722 | /* Emacs' internal format for representation of multiple character | |
1723 | sets is a kind of multi-byte encoding, i.e. characters are | |
1724 | represented by variable-length sequences of one-byte codes. | |
1725 | ||
1726 | ASCII characters and control characters (e.g. `tab', `newline') are | |
1727 | represented by one-byte sequences which are their ASCII codes, in | |
1728 | the range 0x00 through 0x7F. | |
1729 | ||
1730 | 8-bit characters of the range 0x80..0x9F are represented by | |
1731 | two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit | |
1732 | code + 0x20). | |
1733 | ||
1734 | 8-bit characters of the range 0xA0..0xFF are represented by | |
1735 | one-byte sequences which are their 8-bit code. | |
1736 | ||
1737 | The other characters are represented by a sequence of `base | |
1738 | leading-code', optional `extended leading-code', and one or two | |
1739 | `position-code's. The length of the sequence is determined by the | |
1740 | base leading-code. Leading-code takes the range 0x81 through 0x9D, | |
1741 | whereas extended leading-code and position-code take the range 0xA0 | |
1742 | through 0xFF. See `charset.h' for more details about leading-code | |
1743 | and position-code. | |
1744 | ||
1745 | --- CODE RANGE of Emacs' internal format --- | |
1746 | character set range | |
1747 | ------------- ----- | |
1748 | ascii 0x00..0x7F | |
1749 | eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF | |
1750 | eight-bit-graphic 0xA0..0xBF | |
1751 | ELSE 0x81..0x9D + [0xA0..0xFF]+ | |
1752 | --------------------------------------------- | |
1753 | ||
1754 | As this is the internal character representation, the format is | |
1755 | usually not used externally (i.e. in a file or in a data sent to a | |
1756 | process). But, it is possible to have a text externally in this | |
1757 | format (i.e. by encoding by the coding system `emacs-mule'). | |
1758 | ||
1759 | In that case, a sequence of one-byte codes has a slightly different | |
1760 | form. | |
1761 | ||
1762 | At first, all characters in eight-bit-control are represented by | |
1763 | one-byte sequences which are their 8-bit code. | |
1764 | ||
1765 | Next, character composition data are represented by the byte | |
1766 | sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ..., | |
1767 | where, | |
e951386e | 1768 | METHOD is 0xF2 plus one of composition method (enum |
df7492f9 KH |
1769 | composition_method), |
1770 | ||
1771 | BYTES is 0xA0 plus a byte length of this composition data, | |
1772 | ||
e951386e | 1773 | CHARS is 0xA0 plus a number of characters composed by this |
df7492f9 KH |
1774 | data, |
1775 | ||
ad1746f5 | 1776 | COMPONENTs are characters of multibyte form or composition |
df7492f9 KH |
1777 | rules encoded by two-byte of ASCII codes. |
1778 | ||
1779 | In addition, for backward compatibility, the following formats are | |
1780 | also recognized as composition data on decoding. | |
1781 | ||
1782 | 0x80 MSEQ ... | |
1783 | 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ | |
1784 | ||
1785 | Here, | |
1786 | MSEQ is a multibyte form but in these special format: | |
1787 | ASCII: 0xA0 ASCII_CODE+0x80, | |
1788 | other: LEADING_CODE+0x20 FOLLOWING-BYTE ..., | |
1789 | RULE is a one byte code of the range 0xA0..0xF0 that | |
1790 | represents a composition rule. | |
1791 | */ | |
1792 | ||
1793 | char emacs_mule_bytes[256]; | |
1794 | ||
e951386e KH |
1795 | |
1796 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
f10fe38f | 1797 | Return true if a text is encoded in 'emacs-mule'. */ |
e951386e | 1798 | |
f10fe38f | 1799 | static bool |
cf84bb53 JB |
1800 | detect_coding_emacs_mule (struct coding_system *coding, |
1801 | struct coding_detection_info *detect_info) | |
e951386e KH |
1802 | { |
1803 | const unsigned char *src = coding->source, *src_base; | |
1804 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
f10fe38f | 1805 | bool multibytep = coding->src_multibyte; |
d311d28c | 1806 | ptrdiff_t consumed_chars = 0; |
e951386e KH |
1807 | int c; |
1808 | int found = 0; | |
1809 | ||
1810 | detect_info->checked |= CATEGORY_MASK_EMACS_MULE; | |
1811 | /* A coding system of this category is always ASCII compatible. */ | |
1812 | src += coding->head_ascii; | |
1813 | ||
1814 | while (1) | |
1815 | { | |
1816 | src_base = src; | |
1817 | ONE_MORE_BYTE (c); | |
1818 | if (c < 0) | |
1819 | continue; | |
1820 | if (c == 0x80) | |
1821 | { | |
1822 | /* Perhaps the start of composite character. We simply skip | |
1823 | it because analyzing it is too heavy for detecting. But, | |
1824 | at least, we check that the composite character | |
1825 | constitutes of more than 4 bytes. */ | |
2735d060 | 1826 | const unsigned char *src_start; |
e951386e KH |
1827 | |
1828 | repeat: | |
2735d060 | 1829 | src_start = src; |
e951386e KH |
1830 | do |
1831 | { | |
1832 | ONE_MORE_BYTE (c); | |
1833 | } | |
1834 | while (c >= 0xA0); | |
1835 | ||
2735d060 | 1836 | if (src - src_start <= 4) |
e951386e KH |
1837 | break; |
1838 | found = CATEGORY_MASK_EMACS_MULE; | |
1839 | if (c == 0x80) | |
1840 | goto repeat; | |
1841 | } | |
1842 | ||
1843 | if (c < 0x80) | |
1844 | { | |
1845 | if (c < 0x20 | |
1846 | && (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)) | |
1847 | break; | |
1848 | } | |
1849 | else | |
1850 | { | |
396475b7 | 1851 | int more_bytes = emacs_mule_bytes[c] - 1; |
e951386e KH |
1852 | |
1853 | while (more_bytes > 0) | |
1854 | { | |
1855 | ONE_MORE_BYTE (c); | |
1856 | if (c < 0xA0) | |
1857 | { | |
1858 | src--; /* Unread the last byte. */ | |
1859 | break; | |
1860 | } | |
1861 | more_bytes--; | |
1862 | } | |
1863 | if (more_bytes != 0) | |
1864 | break; | |
1865 | found = CATEGORY_MASK_EMACS_MULE; | |
1866 | } | |
1867 | } | |
1868 | detect_info->rejected |= CATEGORY_MASK_EMACS_MULE; | |
1869 | return 0; | |
1870 | ||
1871 | no_more_source: | |
1872 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) | |
1873 | { | |
1874 | detect_info->rejected |= CATEGORY_MASK_EMACS_MULE; | |
1875 | return 0; | |
1876 | } | |
1877 | detect_info->found |= found; | |
1878 | return 1; | |
1879 | } | |
1880 | ||
1881 | ||
1882 | /* Parse emacs-mule multibyte sequence at SRC and return the decoded | |
1883 | character. If CMP_STATUS indicates that we must expect MSEQ or | |
1884 | RULE described above, decode it and return the negative value of | |
685ebdc8 | 1885 | the decoded character or rule. If an invalid byte is found, return |
e951386e KH |
1886 | -1. If SRC is too short, return -2. */ |
1887 | ||
e2f1bab9 | 1888 | static int |
cf84bb53 JB |
1889 | emacs_mule_char (struct coding_system *coding, const unsigned char *src, |
1890 | int *nbytes, int *nchars, int *id, | |
1891 | struct composition_status *cmp_status) | |
df7492f9 | 1892 | { |
8f924df7 KH |
1893 | const unsigned char *src_end = coding->source + coding->src_bytes; |
1894 | const unsigned char *src_base = src; | |
f10fe38f | 1895 | bool multibytep = coding->src_multibyte; |
2735d060 | 1896 | int charset_ID; |
df7492f9 KH |
1897 | unsigned code; |
1898 | int c; | |
1899 | int consumed_chars = 0; | |
f10fe38f | 1900 | bool mseq_found = 0; |
df7492f9 KH |
1901 | |
1902 | ONE_MORE_BYTE (c); | |
065e3595 | 1903 | if (c < 0) |
df7492f9 | 1904 | { |
065e3595 | 1905 | c = -c; |
2735d060 | 1906 | charset_ID = emacs_mule_charset[0]; |
065e3595 KH |
1907 | } |
1908 | else | |
1909 | { | |
4d41e8b7 KH |
1910 | if (c >= 0xA0) |
1911 | { | |
e951386e KH |
1912 | if (cmp_status->state != COMPOSING_NO |
1913 | && cmp_status->old_form) | |
4d41e8b7 | 1914 | { |
e951386e KH |
1915 | if (cmp_status->state == COMPOSING_CHAR) |
1916 | { | |
1917 | if (c == 0xA0) | |
1918 | { | |
1919 | ONE_MORE_BYTE (c); | |
1920 | c -= 0x80; | |
1921 | if (c < 0) | |
1922 | goto invalid_code; | |
1923 | } | |
1924 | else | |
1925 | c -= 0x20; | |
1926 | mseq_found = 1; | |
1927 | } | |
1928 | else | |
1929 | { | |
1930 | *nbytes = src - src_base; | |
1931 | *nchars = consumed_chars; | |
1932 | return -c; | |
1933 | } | |
4d41e8b7 KH |
1934 | } |
1935 | else | |
e951386e | 1936 | goto invalid_code; |
4d41e8b7 KH |
1937 | } |
1938 | ||
065e3595 | 1939 | switch (emacs_mule_bytes[c]) |
b73bfc1c | 1940 | { |
065e3595 | 1941 | case 2: |
2735d060 | 1942 | if ((charset_ID = emacs_mule_charset[c]) < 0) |
df7492f9 KH |
1943 | goto invalid_code; |
1944 | ONE_MORE_BYTE (c); | |
9ffd559c | 1945 | if (c < 0xA0) |
065e3595 | 1946 | goto invalid_code; |
df7492f9 | 1947 | code = c & 0x7F; |
065e3595 KH |
1948 | break; |
1949 | ||
1950 | case 3: | |
1951 | if (c == EMACS_MULE_LEADING_CODE_PRIVATE_11 | |
1952 | || c == EMACS_MULE_LEADING_CODE_PRIVATE_12) | |
1953 | { | |
1954 | ONE_MORE_BYTE (c); | |
2735d060 | 1955 | if (c < 0xA0 || (charset_ID = emacs_mule_charset[c]) < 0) |
065e3595 KH |
1956 | goto invalid_code; |
1957 | ONE_MORE_BYTE (c); | |
9ffd559c | 1958 | if (c < 0xA0) |
065e3595 KH |
1959 | goto invalid_code; |
1960 | code = c & 0x7F; | |
1961 | } | |
1962 | else | |
1963 | { | |
2735d060 | 1964 | if ((charset_ID = emacs_mule_charset[c]) < 0) |
065e3595 KH |
1965 | goto invalid_code; |
1966 | ONE_MORE_BYTE (c); | |
9ffd559c | 1967 | if (c < 0xA0) |
065e3595 KH |
1968 | goto invalid_code; |
1969 | code = (c & 0x7F) << 8; | |
1970 | ONE_MORE_BYTE (c); | |
9ffd559c | 1971 | if (c < 0xA0) |
065e3595 KH |
1972 | goto invalid_code; |
1973 | code |= c & 0x7F; | |
1974 | } | |
1975 | break; | |
1976 | ||
1977 | case 4: | |
1978 | ONE_MORE_BYTE (c); | |
2735d060 | 1979 | if (c < 0 || (charset_ID = emacs_mule_charset[c]) < 0) |
df7492f9 KH |
1980 | goto invalid_code; |
1981 | ONE_MORE_BYTE (c); | |
9ffd559c | 1982 | if (c < 0xA0) |
065e3595 | 1983 | goto invalid_code; |
781d7a48 | 1984 | code = (c & 0x7F) << 8; |
df7492f9 | 1985 | ONE_MORE_BYTE (c); |
9ffd559c | 1986 | if (c < 0xA0) |
065e3595 | 1987 | goto invalid_code; |
df7492f9 | 1988 | code |= c & 0x7F; |
065e3595 | 1989 | break; |
df7492f9 | 1990 | |
065e3595 KH |
1991 | case 1: |
1992 | code = c; | |
2735d060 | 1993 | charset_ID = ASCII_BYTE_P (code) ? charset_ascii : charset_eight_bit; |
065e3595 | 1994 | break; |
df7492f9 | 1995 | |
065e3595 | 1996 | default: |
1088b922 | 1997 | emacs_abort (); |
065e3595 | 1998 | } |
b84ae584 | 1999 | CODING_DECODE_CHAR (coding, src, src_base, src_end, |
2735d060 | 2000 | CHARSET_FROM_ID (charset_ID), code, c); |
065e3595 KH |
2001 | if (c < 0) |
2002 | goto invalid_code; | |
df7492f9 | 2003 | } |
df7492f9 KH |
2004 | *nbytes = src - src_base; |
2005 | *nchars = consumed_chars; | |
ff0dacd7 | 2006 | if (id) |
2735d060 | 2007 | *id = charset_ID; |
e951386e | 2008 | return (mseq_found ? -c : c); |
df7492f9 KH |
2009 | |
2010 | no_more_source: | |
2011 | return -2; | |
2012 | ||
2013 | invalid_code: | |
2014 | return -1; | |
2015 | } | |
2016 | ||
2017 | ||
e951386e | 2018 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ |
df7492f9 | 2019 | |
e951386e KH |
2020 | /* Handle these composition sequence ('|': the end of header elements, |
2021 | BYTES and CHARS >= 0xA0): | |
df7492f9 | 2022 | |
e951386e KH |
2023 | (1) relative composition: 0x80 0xF2 BYTES CHARS | CHAR ... |
2024 | (2) altchar composition: 0x80 0xF4 BYTES CHARS | ALT ... ALT CHAR ... | |
2025 | (3) alt&rule composition: 0x80 0xF5 BYTES CHARS | ALT RULE ... ALT CHAR ... | |
df7492f9 | 2026 | |
e951386e | 2027 | and these old form: |
1a4990fb | 2028 | |
e951386e KH |
2029 | (4) relative composition: 0x80 | MSEQ ... MSEQ |
2030 | (5) rulebase composition: 0x80 0xFF | MSEQ MRULE ... MSEQ | |
df7492f9 | 2031 | |
e951386e KH |
2032 | When the starter 0x80 and the following header elements are found, |
2033 | this annotation header is produced. | |
df7492f9 | 2034 | |
e951386e | 2035 | [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS NBYTES METHOD ] |
df7492f9 | 2036 | |
e951386e KH |
2037 | NCHARS is CHARS - 0xA0 for (1), (2), (3), and 0 for (4), (5). |
2038 | NBYTES is BYTES - 0xA0 for (1), (2), (3), and 0 for (4), (5). | |
df7492f9 | 2039 | |
e951386e KH |
2040 | Then, upon reading the following elements, these codes are produced |
2041 | until the composition end is found: | |
df7492f9 | 2042 | |
e951386e KH |
2043 | (1) CHAR ... CHAR |
2044 | (2) ALT ... ALT CHAR ... CHAR | |
2045 | (3) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT CHAR ... CHAR | |
2046 | (4) CHAR ... CHAR | |
2047 | (5) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR | |
4ed46869 | 2048 | |
e951386e KH |
2049 | When the composition end is found, LENGTH and NCHARS in the |
2050 | annotation header is updated as below: | |
b73bfc1c | 2051 | |
e951386e KH |
2052 | (1) LENGTH: unchanged, NCHARS: unchanged |
2053 | (2) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged | |
2054 | (3) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged | |
2055 | (4) LENGTH: unchanged, NCHARS: number of CHARs | |
2056 | (5) LENGTH: unchanged, NCHARS: number of CHARs | |
df7492f9 | 2057 | |
e951386e KH |
2058 | If an error is found while composing, the annotation header is |
2059 | changed to the original composition header (plus filler -1s) as | |
2060 | below: | |
2061 | ||
2062 | (1),(2),(3) [ 0x80 0xF2+METHOD BYTES CHARS -1 ] | |
2063 | (5) [ 0x80 0xFF -1 -1- -1 ] | |
2064 | ||
2065 | and the sequence [ -2 DECODED-RULE ] is changed to the original | |
2066 | byte sequence as below: | |
2067 | o the original byte sequence is B: [ B -1 ] | |
2068 | o the original byte sequence is B1 B2: [ B1 B2 ] | |
2069 | ||
2070 | Most of the routines are implemented by macros because many | |
2071 | variables and labels in the caller decode_coding_emacs_mule must be | |
2072 | accessible, and they are usually called just once (thus doesn't | |
2073 | increase the size of compiled object). */ | |
2074 | ||
2075 | /* Decode a composition rule represented by C as a component of | |
2076 | composition sequence of Emacs 20 style. Set RULE to the decoded | |
2077 | rule. */ | |
2078 | ||
2079 | #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(c, rule) \ | |
df7492f9 | 2080 | do { \ |
e951386e KH |
2081 | int gref, nref; \ |
2082 | \ | |
4d41e8b7 | 2083 | c -= 0xA0; \ |
df7492f9 KH |
2084 | if (c < 0 || c >= 81) \ |
2085 | goto invalid_code; \ | |
df7492f9 | 2086 | gref = c / 9, nref = c % 9; \ |
e951386e KH |
2087 | if (gref == 4) gref = 10; \ |
2088 | if (nref == 4) nref = 10; \ | |
2089 | rule = COMPOSITION_ENCODE_RULE (gref, nref); \ | |
df7492f9 KH |
2090 | } while (0) |
2091 | ||
2092 | ||
e951386e KH |
2093 | /* Decode a composition rule represented by C and the following byte |
2094 | at SRC as a component of composition sequence of Emacs 21 style. | |
2095 | Set RULE to the decoded rule. */ | |
781d7a48 | 2096 | |
e951386e | 2097 | #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(c, rule) \ |
781d7a48 KH |
2098 | do { \ |
2099 | int gref, nref; \ | |
e951386e KH |
2100 | \ |
2101 | gref = c - 0x20; \ | |
2102 | if (gref < 0 || gref >= 81) \ | |
781d7a48 | 2103 | goto invalid_code; \ |
e951386e KH |
2104 | ONE_MORE_BYTE (c); \ |
2105 | nref = c - 0x20; \ | |
2106 | if (nref < 0 || nref >= 81) \ | |
781d7a48 | 2107 | goto invalid_code; \ |
e951386e | 2108 | rule = COMPOSITION_ENCODE_RULE (gref, nref); \ |
781d7a48 KH |
2109 | } while (0) |
2110 | ||
2111 | ||
e951386e KH |
2112 | /* Start of Emacs 21 style format. The first three bytes at SRC are |
2113 | (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is the | |
2114 | byte length of this composition information, CHARS is the number of | |
2115 | characters composed by this composition. */ | |
2116 | ||
2117 | #define DECODE_EMACS_MULE_21_COMPOSITION() \ | |
aa72b389 | 2118 | do { \ |
781d7a48 | 2119 | enum composition_method method = c - 0xF2; \ |
df7492f9 | 2120 | int nbytes, nchars; \ |
e951386e | 2121 | \ |
df7492f9 | 2122 | ONE_MORE_BYTE (c); \ |
065e3595 KH |
2123 | if (c < 0) \ |
2124 | goto invalid_code; \ | |
df7492f9 | 2125 | nbytes = c - 0xA0; \ |
e951386e | 2126 | if (nbytes < 3 || (method == COMPOSITION_RELATIVE && nbytes != 4)) \ |
df7492f9 KH |
2127 | goto invalid_code; \ |
2128 | ONE_MORE_BYTE (c); \ | |
2129 | nchars = c - 0xA0; \ | |
e951386e KH |
2130 | if (nchars <= 0 || nchars >= MAX_COMPOSITION_COMPONENTS) \ |
2131 | goto invalid_code; \ | |
2132 | cmp_status->old_form = 0; \ | |
2133 | cmp_status->method = method; \ | |
2134 | if (method == COMPOSITION_RELATIVE) \ | |
2135 | cmp_status->state = COMPOSING_CHAR; \ | |
2136 | else \ | |
2137 | cmp_status->state = COMPOSING_COMPONENT_CHAR; \ | |
2138 | cmp_status->length = MAX_ANNOTATION_LENGTH; \ | |
2139 | cmp_status->nchars = nchars; \ | |
2140 | cmp_status->ncomps = nbytes - 4; \ | |
2141 | ADD_COMPOSITION_DATA (charbuf, nchars, nbytes, method); \ | |
aa72b389 | 2142 | } while (0) |
93dec019 | 2143 | |
aa72b389 | 2144 | |
e951386e KH |
2145 | /* Start of Emacs 20 style format for relative composition. */ |
2146 | ||
2147 | #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION() \ | |
2148 | do { \ | |
2149 | cmp_status->old_form = 1; \ | |
2150 | cmp_status->method = COMPOSITION_RELATIVE; \ | |
2151 | cmp_status->state = COMPOSING_CHAR; \ | |
2152 | cmp_status->length = MAX_ANNOTATION_LENGTH; \ | |
2153 | cmp_status->nchars = cmp_status->ncomps = 0; \ | |
2154 | ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \ | |
2155 | } while (0) | |
2156 | ||
2157 | ||
2158 | /* Start of Emacs 20 style format for rule-base composition. */ | |
2159 | ||
2160 | #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION() \ | |
2161 | do { \ | |
2162 | cmp_status->old_form = 1; \ | |
2163 | cmp_status->method = COMPOSITION_WITH_RULE; \ | |
2164 | cmp_status->state = COMPOSING_CHAR; \ | |
2165 | cmp_status->length = MAX_ANNOTATION_LENGTH; \ | |
2166 | cmp_status->nchars = cmp_status->ncomps = 0; \ | |
2167 | ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \ | |
df7492f9 KH |
2168 | } while (0) |
2169 | ||
2170 | ||
e951386e KH |
2171 | #define DECODE_EMACS_MULE_COMPOSITION_START() \ |
2172 | do { \ | |
2173 | const unsigned char *current_src = src; \ | |
2174 | \ | |
2175 | ONE_MORE_BYTE (c); \ | |
2176 | if (c < 0) \ | |
2177 | goto invalid_code; \ | |
2178 | if (c - 0xF2 >= COMPOSITION_RELATIVE \ | |
2179 | && c - 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS) \ | |
2180 | DECODE_EMACS_MULE_21_COMPOSITION (); \ | |
2181 | else if (c < 0xA0) \ | |
2182 | goto invalid_code; \ | |
2183 | else if (c < 0xC0) \ | |
2184 | { \ | |
2185 | DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (); \ | |
2186 | /* Re-read C as a composition component. */ \ | |
2187 | src = current_src; \ | |
2188 | } \ | |
2189 | else if (c == 0xFF) \ | |
2190 | DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (); \ | |
2191 | else \ | |
2192 | goto invalid_code; \ | |
2193 | } while (0) | |
2194 | ||
2195 | #define EMACS_MULE_COMPOSITION_END() \ | |
df7492f9 | 2196 | do { \ |
e951386e | 2197 | int idx = - cmp_status->length; \ |
4d41e8b7 | 2198 | \ |
e951386e KH |
2199 | if (cmp_status->old_form) \ |
2200 | charbuf[idx + 2] = cmp_status->nchars; \ | |
2201 | else if (cmp_status->method > COMPOSITION_RELATIVE) \ | |
2202 | charbuf[idx] = charbuf[idx + 2] - cmp_status->length; \ | |
2203 | cmp_status->state = COMPOSING_NO; \ | |
2204 | } while (0) | |
2205 | ||
2206 | ||
2207 | static int | |
cf84bb53 JB |
2208 | emacs_mule_finish_composition (int *charbuf, |
2209 | struct composition_status *cmp_status) | |
e951386e KH |
2210 | { |
2211 | int idx = - cmp_status->length; | |
2212 | int new_chars; | |
2213 | ||
2214 | if (cmp_status->old_form && cmp_status->nchars > 0) | |
2215 | { | |
2216 | charbuf[idx + 2] = cmp_status->nchars; | |
2217 | new_chars = 0; | |
2218 | if (cmp_status->method == COMPOSITION_WITH_RULE | |
2219 | && cmp_status->state == COMPOSING_CHAR) | |
2220 | { | |
2221 | /* The last rule was invalid. */ | |
2222 | int rule = charbuf[-1] + 0xA0; | |
2223 | ||
2224 | charbuf[-2] = BYTE8_TO_CHAR (rule); | |
2225 | charbuf[-1] = -1; | |
2226 | new_chars = 1; | |
2227 | } | |
2228 | } | |
2229 | else | |
2230 | { | |
2231 | charbuf[idx++] = BYTE8_TO_CHAR (0x80); | |
2232 | ||
2233 | if (cmp_status->method == COMPOSITION_WITH_RULE) | |
2234 | { | |
2235 | charbuf[idx++] = BYTE8_TO_CHAR (0xFF); | |
2236 | charbuf[idx++] = -3; | |
2237 | charbuf[idx++] = 0; | |
2238 | new_chars = 1; | |
2239 | } | |
2240 | else | |
2241 | { | |
2242 | int nchars = charbuf[idx + 1] + 0xA0; | |
2243 | int nbytes = charbuf[idx + 2] + 0xA0; | |
2244 | ||
2245 | charbuf[idx++] = BYTE8_TO_CHAR (0xF2 + cmp_status->method); | |
2246 | charbuf[idx++] = BYTE8_TO_CHAR (nbytes); | |
2247 | charbuf[idx++] = BYTE8_TO_CHAR (nchars); | |
2248 | charbuf[idx++] = -1; | |
2249 | new_chars = 4; | |
2250 | } | |
2251 | } | |
2252 | cmp_status->state = COMPOSING_NO; | |
2253 | return new_chars; | |
2254 | } | |
2255 | ||
2256 | #define EMACS_MULE_MAYBE_FINISH_COMPOSITION() \ | |
2257 | do { \ | |
2258 | if (cmp_status->state != COMPOSING_NO) \ | |
2259 | char_offset += emacs_mule_finish_composition (charbuf, cmp_status); \ | |
df7492f9 KH |
2260 | } while (0) |
2261 | ||
aa72b389 KH |
2262 | |
2263 | static void | |
971de7fb | 2264 | decode_coding_emacs_mule (struct coding_system *coding) |
aa72b389 | 2265 | { |
8f924df7 KH |
2266 | const unsigned char *src = coding->source + coding->consumed; |
2267 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
2268 | const unsigned char *src_base; | |
69a80ea3 | 2269 | int *charbuf = coding->charbuf + coding->charbuf_used; |
ad1746f5 KH |
2270 | /* We may produce two annotations (charset and composition) in one |
2271 | loop and one more charset annotation at the end. */ | |
69a80ea3 | 2272 | int *charbuf_end |
15cbd324 EZ |
2273 | = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 3) |
2274 | /* We can produce up to 2 characters in a loop. */ | |
2275 | - 1; | |
d311d28c | 2276 | ptrdiff_t consumed_chars = 0, consumed_chars_base; |
f10fe38f | 2277 | bool multibytep = coding->src_multibyte; |
d311d28c PE |
2278 | ptrdiff_t char_offset = coding->produced_char; |
2279 | ptrdiff_t last_offset = char_offset; | |
ff0dacd7 | 2280 | int last_id = charset_ascii; |
f10fe38f PE |
2281 | bool eol_dos |
2282 | = !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); | |
119852e7 | 2283 | int byte_after_cr = -1; |
e951386e | 2284 | struct composition_status *cmp_status = &coding->spec.emacs_mule.cmp_status; |
aa72b389 | 2285 | |
e951386e KH |
2286 | if (cmp_status->state != COMPOSING_NO) |
2287 | { | |
2288 | int i; | |
2289 | ||
15cbd324 | 2290 | if (charbuf_end - charbuf < cmp_status->length) |
1088b922 | 2291 | emacs_abort (); |
e951386e KH |
2292 | for (i = 0; i < cmp_status->length; i++) |
2293 | *charbuf++ = cmp_status->carryover[i]; | |
2294 | coding->annotated = 1; | |
2295 | } | |
2296 | ||
aa72b389 KH |
2297 | while (1) |
2298 | { | |
ee05f961 | 2299 | int c, id IF_LINT (= 0); |
df7492f9 | 2300 | |
aa72b389 | 2301 | src_base = src; |
df7492f9 KH |
2302 | consumed_chars_base = consumed_chars; |
2303 | ||
2304 | if (charbuf >= charbuf_end) | |
b71f6f73 KH |
2305 | { |
2306 | if (byte_after_cr >= 0) | |
2307 | src_base--; | |
2308 | break; | |
2309 | } | |
aa72b389 | 2310 | |
119852e7 KH |
2311 | if (byte_after_cr >= 0) |
2312 | c = byte_after_cr, byte_after_cr = -1; | |
2313 | else | |
2314 | ONE_MORE_BYTE (c); | |
e951386e KH |
2315 | |
2316 | if (c < 0 || c == 0x80) | |
065e3595 | 2317 | { |
e951386e KH |
2318 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); |
2319 | if (c < 0) | |
2320 | { | |
2321 | *charbuf++ = -c; | |
2322 | char_offset++; | |
2323 | } | |
2324 | else | |
2325 | DECODE_EMACS_MULE_COMPOSITION_START (); | |
2326 | continue; | |
065e3595 | 2327 | } |
e951386e KH |
2328 | |
2329 | if (c < 0x80) | |
aa72b389 | 2330 | { |
2735d060 | 2331 | if (eol_dos && c == '\r') |
119852e7 | 2332 | ONE_MORE_BYTE (byte_after_cr); |
e951386e KH |
2333 | id = charset_ascii; |
2334 | if (cmp_status->state != COMPOSING_NO) | |
2335 | { | |
2336 | if (cmp_status->old_form) | |
2337 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); | |
2338 | else if (cmp_status->state >= COMPOSING_COMPONENT_CHAR) | |
2339 | cmp_status->ncomps--; | |
2340 | } | |
2341 | } | |
2342 | else | |
2343 | { | |
ee05f961 | 2344 | int nchars IF_LINT (= 0), nbytes IF_LINT (= 0); |
75f80e63 EZ |
2345 | /* emacs_mule_char can load a charset map from a file, which |
2346 | allocates a large structure and might cause buffer text | |
2347 | to be relocated as result. Thus, we need to remember the | |
ad1746f5 | 2348 | original pointer to buffer text, and fix up all related |
75f80e63 EZ |
2349 | pointers after the call. */ |
2350 | const unsigned char *orig = coding->source; | |
d311d28c | 2351 | ptrdiff_t offset; |
e951386e KH |
2352 | |
2353 | c = emacs_mule_char (coding, src_base, &nbytes, &nchars, &id, | |
2354 | cmp_status); | |
75f80e63 EZ |
2355 | offset = coding->source - orig; |
2356 | if (offset) | |
2357 | { | |
2358 | src += offset; | |
2359 | src_base += offset; | |
2360 | src_end += offset; | |
2361 | } | |
e951386e KH |
2362 | if (c < 0) |
2363 | { | |
2364 | if (c == -1) | |
2365 | goto invalid_code; | |
2366 | if (c == -2) | |
2367 | break; | |
2368 | } | |
2369 | src = src_base + nbytes; | |
2370 | consumed_chars = consumed_chars_base + nchars; | |
2371 | if (cmp_status->state >= COMPOSING_COMPONENT_CHAR) | |
2372 | cmp_status->ncomps -= nchars; | |
2373 | } | |
2374 | ||
ad1746f5 | 2375 | /* Now if C >= 0, we found a normally encoded character, if C < |
e951386e KH |
2376 | 0, we found an old-style composition component character or |
2377 | rule. */ | |
2378 | ||
2379 | if (cmp_status->state == COMPOSING_NO) | |
2380 | { | |
2381 | if (last_id != id) | |
2382 | { | |
2383 | if (last_id != charset_ascii) | |
2384 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, | |
2385 | last_id); | |
2386 | last_id = id; | |
2387 | last_offset = char_offset; | |
2388 | } | |
df7492f9 KH |
2389 | *charbuf++ = c; |
2390 | char_offset++; | |
aa72b389 | 2391 | } |
e951386e | 2392 | else if (cmp_status->state == COMPOSING_CHAR) |
df7492f9 | 2393 | { |
e951386e KH |
2394 | if (cmp_status->old_form) |
2395 | { | |
2396 | if (c >= 0) | |
2397 | { | |
2398 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); | |
2399 | *charbuf++ = c; | |
2400 | char_offset++; | |
2401 | } | |
2402 | else | |
2403 | { | |
2404 | *charbuf++ = -c; | |
2405 | cmp_status->nchars++; | |
2406 | cmp_status->length++; | |
2407 | if (cmp_status->nchars == MAX_COMPOSITION_COMPONENTS) | |
2408 | EMACS_MULE_COMPOSITION_END (); | |
2409 | else if (cmp_status->method == COMPOSITION_WITH_RULE) | |
2410 | cmp_status->state = COMPOSING_RULE; | |
2411 | } | |
2412 | } | |
df7492f9 | 2413 | else |
e951386e KH |
2414 | { |
2415 | *charbuf++ = c; | |
2416 | cmp_status->length++; | |
2417 | cmp_status->nchars--; | |
2418 | if (cmp_status->nchars == 0) | |
2419 | EMACS_MULE_COMPOSITION_END (); | |
2420 | } | |
df7492f9 | 2421 | } |
e951386e | 2422 | else if (cmp_status->state == COMPOSING_RULE) |
df7492f9 | 2423 | { |
e951386e | 2424 | int rule; |
ff0dacd7 | 2425 | |
e951386e | 2426 | if (c >= 0) |
df7492f9 | 2427 | { |
e951386e KH |
2428 | EMACS_MULE_COMPOSITION_END (); |
2429 | *charbuf++ = c; | |
2430 | char_offset++; | |
df7492f9 | 2431 | } |
e951386e | 2432 | else |
ff0dacd7 | 2433 | { |
e951386e KH |
2434 | c = -c; |
2435 | DECODE_EMACS_MULE_COMPOSITION_RULE_20 (c, rule); | |
2436 | if (rule < 0) | |
2437 | goto invalid_code; | |
2438 | *charbuf++ = -2; | |
2439 | *charbuf++ = rule; | |
2440 | cmp_status->length += 2; | |
2441 | cmp_status->state = COMPOSING_CHAR; | |
ff0dacd7 | 2442 | } |
e951386e KH |
2443 | } |
2444 | else if (cmp_status->state == COMPOSING_COMPONENT_CHAR) | |
2445 | { | |
df7492f9 | 2446 | *charbuf++ = c; |
e951386e KH |
2447 | cmp_status->length++; |
2448 | if (cmp_status->ncomps == 0) | |
2449 | cmp_status->state = COMPOSING_CHAR; | |
2450 | else if (cmp_status->ncomps > 0) | |
2451 | { | |
2452 | if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS) | |
2453 | cmp_status->state = COMPOSING_COMPONENT_RULE; | |
2454 | } | |
2455 | else | |
2456 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); | |
df7492f9 | 2457 | } |
e951386e KH |
2458 | else /* COMPOSING_COMPONENT_RULE */ |
2459 | { | |
2460 | int rule; | |
2461 | ||
2462 | DECODE_EMACS_MULE_COMPOSITION_RULE_21 (c, rule); | |
2463 | if (rule < 0) | |
2464 | goto invalid_code; | |
2465 | *charbuf++ = -2; | |
2466 | *charbuf++ = rule; | |
2467 | cmp_status->length += 2; | |
2468 | cmp_status->ncomps--; | |
2469 | if (cmp_status->ncomps > 0) | |
2470 | cmp_status->state = COMPOSING_COMPONENT_CHAR; | |
2471 | else | |
2472 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); | |
2473 | } | |
2474 | continue; | |
2475 | ||
df7492f9 | 2476 | invalid_code: |
e951386e | 2477 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); |
df7492f9 KH |
2478 | src = src_base; |
2479 | consumed_chars = consumed_chars_base; | |
2480 | ONE_MORE_BYTE (c); | |
2481 | *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); | |
ff0dacd7 | 2482 | char_offset++; |
df7492f9 KH |
2483 | coding->errors++; |
2484 | } | |
2485 | ||
2486 | no_more_source: | |
e951386e KH |
2487 | if (cmp_status->state != COMPOSING_NO) |
2488 | { | |
2489 | if (coding->mode & CODING_MODE_LAST_BLOCK) | |
2490 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); | |
2491 | else | |
2492 | { | |
2493 | int i; | |
2494 | ||
2495 | charbuf -= cmp_status->length; | |
2496 | for (i = 0; i < cmp_status->length; i++) | |
2497 | cmp_status->carryover[i] = charbuf[i]; | |
2498 | } | |
2499 | } | |
ff0dacd7 | 2500 | if (last_id != charset_ascii) |
69a80ea3 | 2501 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
2502 | coding->consumed_char += consumed_chars_base; |
2503 | coding->consumed = src_base - coding->source; | |
2504 | coding->charbuf_used = charbuf - coding->charbuf; | |
2505 | } | |
2506 | ||
2507 | ||
2508 | #define EMACS_MULE_LEADING_CODES(id, codes) \ | |
2509 | do { \ | |
2510 | if (id < 0xA0) \ | |
2511 | codes[0] = id, codes[1] = 0; \ | |
2512 | else if (id < 0xE0) \ | |
2513 | codes[0] = 0x9A, codes[1] = id; \ | |
2514 | else if (id < 0xF0) \ | |
2515 | codes[0] = 0x9B, codes[1] = id; \ | |
2516 | else if (id < 0xF5) \ | |
2517 | codes[0] = 0x9C, codes[1] = id; \ | |
2518 | else \ | |
2519 | codes[0] = 0x9D, codes[1] = id; \ | |
2520 | } while (0); | |
2521 | ||
aa72b389 | 2522 | |
f10fe38f | 2523 | static bool |
971de7fb | 2524 | encode_coding_emacs_mule (struct coding_system *coding) |
df7492f9 | 2525 | { |
f10fe38f | 2526 | bool multibytep = coding->dst_multibyte; |
df7492f9 KH |
2527 | int *charbuf = coding->charbuf; |
2528 | int *charbuf_end = charbuf + coding->charbuf_used; | |
2529 | unsigned char *dst = coding->destination + coding->produced; | |
2530 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
2531 | int safe_room = 8; | |
d311d28c | 2532 | ptrdiff_t produced_chars = 0; |
24a73b0a | 2533 | Lisp_Object attrs, charset_list; |
df7492f9 | 2534 | int c; |
ff0dacd7 | 2535 | int preferred_charset_id = -1; |
df7492f9 | 2536 | |
24a73b0a | 2537 | CODING_GET_INFO (coding, attrs, charset_list); |
eccb6815 KH |
2538 | if (! EQ (charset_list, Vemacs_mule_charset_list)) |
2539 | { | |
4939150c PE |
2540 | charset_list = Vemacs_mule_charset_list; |
2541 | ASET (attrs, coding_attr_charset_list, charset_list); | |
eccb6815 | 2542 | } |
df7492f9 KH |
2543 | |
2544 | while (charbuf < charbuf_end) | |
2545 | { | |
2546 | ASSURE_DESTINATION (safe_room); | |
2547 | c = *charbuf++; | |
ff0dacd7 KH |
2548 | |
2549 | if (c < 0) | |
2550 | { | |
2551 | /* Handle an annotation. */ | |
2552 | switch (*charbuf) | |
2553 | { | |
2554 | case CODING_ANNOTATE_COMPOSITION_MASK: | |
2555 | /* Not yet implemented. */ | |
2556 | break; | |
2557 | case CODING_ANNOTATE_CHARSET_MASK: | |
2558 | preferred_charset_id = charbuf[3]; | |
2559 | if (preferred_charset_id >= 0 | |
2560 | && NILP (Fmemq (make_number (preferred_charset_id), | |
2561 | charset_list))) | |
2562 | preferred_charset_id = -1; | |
2563 | break; | |
2564 | default: | |
1088b922 | 2565 | emacs_abort (); |
ff0dacd7 KH |
2566 | } |
2567 | charbuf += -c - 1; | |
2568 | continue; | |
2569 | } | |
2570 | ||
df7492f9 KH |
2571 | if (ASCII_CHAR_P (c)) |
2572 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d KH |
2573 | else if (CHAR_BYTE8_P (c)) |
2574 | { | |
2575 | c = CHAR_TO_BYTE8 (c); | |
2576 | EMIT_ONE_BYTE (c); | |
2577 | } | |
df7492f9 | 2578 | else |
aa72b389 | 2579 | { |
df7492f9 KH |
2580 | struct charset *charset; |
2581 | unsigned code; | |
2582 | int dimension; | |
2583 | int emacs_mule_id; | |
2584 | unsigned char leading_codes[2]; | |
2585 | ||
ff0dacd7 KH |
2586 | if (preferred_charset_id >= 0) |
2587 | { | |
f10fe38f | 2588 | bool result; |
5eb05ea3 | 2589 | |
ff0dacd7 | 2590 | charset = CHARSET_FROM_ID (preferred_charset_id); |
5eb05ea3 KH |
2591 | CODING_CHAR_CHARSET_P (coding, dst, dst_end, c, charset, result); |
2592 | if (result) | |
905ca9d2 KH |
2593 | code = ENCODE_CHAR (charset, c); |
2594 | else | |
5eb05ea3 KH |
2595 | CODING_CHAR_CHARSET (coding, dst, dst_end, c, charset_list, |
2596 | &code, charset); | |
ff0dacd7 KH |
2597 | } |
2598 | else | |
5eb05ea3 KH |
2599 | CODING_CHAR_CHARSET (coding, dst, dst_end, c, charset_list, |
2600 | &code, charset); | |
df7492f9 KH |
2601 | if (! charset) |
2602 | { | |
2603 | c = coding->default_char; | |
2604 | if (ASCII_CHAR_P (c)) | |
2605 | { | |
2606 | EMIT_ONE_ASCII_BYTE (c); | |
2607 | continue; | |
2608 | } | |
5eb05ea3 KH |
2609 | CODING_CHAR_CHARSET (coding, dst, dst_end, c, charset_list, |
2610 | &code, charset); | |
df7492f9 KH |
2611 | } |
2612 | dimension = CHARSET_DIMENSION (charset); | |
2613 | emacs_mule_id = CHARSET_EMACS_MULE_ID (charset); | |
2614 | EMACS_MULE_LEADING_CODES (emacs_mule_id, leading_codes); | |
2615 | EMIT_ONE_BYTE (leading_codes[0]); | |
2616 | if (leading_codes[1]) | |
2617 | EMIT_ONE_BYTE (leading_codes[1]); | |
2618 | if (dimension == 1) | |
1fa663f9 | 2619 | EMIT_ONE_BYTE (code | 0x80); |
aa72b389 | 2620 | else |
df7492f9 | 2621 | { |
1fa663f9 | 2622 | code |= 0x8080; |
df7492f9 KH |
2623 | EMIT_ONE_BYTE (code >> 8); |
2624 | EMIT_ONE_BYTE (code & 0xFF); | |
2625 | } | |
aa72b389 | 2626 | } |
aa72b389 | 2627 | } |
065e3595 | 2628 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
2629 | coding->produced_char += produced_chars; |
2630 | coding->produced = dst - coding->destination; | |
2631 | return 0; | |
aa72b389 | 2632 | } |
b73bfc1c | 2633 | |
4ed46869 | 2634 | \f |
df7492f9 | 2635 | /*** 7. ISO2022 handlers ***/ |
4ed46869 KH |
2636 | |
2637 | /* The following note describes the coding system ISO2022 briefly. | |
39787efd | 2638 | Since the intention of this note is to help understand the |
5a936b46 | 2639 | functions in this file, some parts are NOT ACCURATE or are OVERLY |
39787efd | 2640 | SIMPLIFIED. For thorough understanding, please refer to the |
5a936b46 | 2641 | original document of ISO2022. This is equivalent to the standard |
cfb43547 | 2642 | ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*). |
4ed46869 KH |
2643 | |
2644 | ISO2022 provides many mechanisms to encode several character sets | |
cfb43547 | 2645 | in 7-bit and 8-bit environments. For 7-bit environments, all text |
39787efd KH |
2646 | is encoded using bytes less than 128. This may make the encoded |
2647 | text a little bit longer, but the text passes more easily through | |
cfb43547 | 2648 | several types of gateway, some of which strip off the MSB (Most |
8ca3766a | 2649 | Significant Bit). |
b73bfc1c | 2650 | |
cfb43547 DL |
2651 | There are two kinds of character sets: control character sets and |
2652 | graphic character sets. The former contain control characters such | |
4ed46869 | 2653 | as `newline' and `escape' to provide control functions (control |
39787efd | 2654 | functions are also provided by escape sequences). The latter |
cfb43547 | 2655 | contain graphic characters such as 'A' and '-'. Emacs recognizes |
4ed46869 KH |
2656 | two control character sets and many graphic character sets. |
2657 | ||
2658 | Graphic character sets are classified into one of the following | |
39787efd KH |
2659 | four classes, according to the number of bytes (DIMENSION) and |
2660 | number of characters in one dimension (CHARS) of the set: | |
2661 | - DIMENSION1_CHARS94 | |
2662 | - DIMENSION1_CHARS96 | |
2663 | - DIMENSION2_CHARS94 | |
2664 | - DIMENSION2_CHARS96 | |
2665 | ||
2666 | In addition, each character set is assigned an identification tag, | |
cfb43547 | 2667 | unique for each set, called the "final character" (denoted as <F> |
39787efd KH |
2668 | hereafter). The <F> of each character set is decided by ECMA(*) |
2669 | when it is registered in ISO. The code range of <F> is 0x30..0x7F | |
2670 | (0x30..0x3F are for private use only). | |
4ed46869 KH |
2671 | |
2672 | Note (*): ECMA = European Computer Manufacturers Association | |
2673 | ||
cfb43547 | 2674 | Here are examples of graphic character sets [NAME(<F>)]: |
4ed46869 KH |
2675 | o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ... |
2676 | o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ... | |
2677 | o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ... | |
2678 | o DIMENSION2_CHARS96 -- none for the moment | |
2679 | ||
39787efd | 2680 | A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR. |
4ed46869 KH |
2681 | C0 [0x00..0x1F] -- control character plane 0 |
2682 | GL [0x20..0x7F] -- graphic character plane 0 | |
2683 | C1 [0x80..0x9F] -- control character plane 1 | |
2684 | GR [0xA0..0xFF] -- graphic character plane 1 | |
2685 | ||
2686 | A control character set is directly designated and invoked to C0 or | |
39787efd KH |
2687 | C1 by an escape sequence. The most common case is that: |
2688 | - ISO646's control character set is designated/invoked to C0, and | |
2689 | - ISO6429's control character set is designated/invoked to C1, | |
2690 | and usually these designations/invocations are omitted in encoded | |
2691 | text. In a 7-bit environment, only C0 can be used, and a control | |
2692 | character for C1 is encoded by an appropriate escape sequence to | |
2693 | fit into the environment. All control characters for C1 are | |
2694 | defined to have corresponding escape sequences. | |
4ed46869 KH |
2695 | |
2696 | A graphic character set is at first designated to one of four | |
2697 | graphic registers (G0 through G3), then these graphic registers are | |
2698 | invoked to GL or GR. These designations and invocations can be | |
2699 | done independently. The most common case is that G0 is invoked to | |
39787efd KH |
2700 | GL, G1 is invoked to GR, and ASCII is designated to G0. Usually |
2701 | these invocations and designations are omitted in encoded text. | |
2702 | In a 7-bit environment, only GL can be used. | |
4ed46869 | 2703 | |
39787efd KH |
2704 | When a graphic character set of CHARS94 is invoked to GL, codes |
2705 | 0x20 and 0x7F of the GL area work as control characters SPACE and | |
2706 | DEL respectively, and codes 0xA0 and 0xFF of the GR area should not | |
2707 | be used. | |
4ed46869 KH |
2708 | |
2709 | There are two ways of invocation: locking-shift and single-shift. | |
2710 | With locking-shift, the invocation lasts until the next different | |
39787efd KH |
2711 | invocation, whereas with single-shift, the invocation affects the |
2712 | following character only and doesn't affect the locking-shift | |
2713 | state. Invocations are done by the following control characters or | |
2714 | escape sequences: | |
4ed46869 KH |
2715 | |
2716 | ---------------------------------------------------------------------- | |
39787efd | 2717 | abbrev function cntrl escape seq description |
4ed46869 | 2718 | ---------------------------------------------------------------------- |
39787efd KH |
2719 | SI/LS0 (shift-in) 0x0F none invoke G0 into GL |
2720 | SO/LS1 (shift-out) 0x0E none invoke G1 into GL | |
2721 | LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL | |
2722 | LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL | |
2723 | LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*) | |
2724 | LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*) | |
2725 | LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*) | |
2726 | SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char | |
2727 | SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char | |
4ed46869 | 2728 | ---------------------------------------------------------------------- |
39787efd KH |
2729 | (*) These are not used by any known coding system. |
2730 | ||
2731 | Control characters for these functions are defined by macros | |
2732 | ISO_CODE_XXX in `coding.h'. | |
4ed46869 | 2733 | |
39787efd | 2734 | Designations are done by the following escape sequences: |
4ed46869 KH |
2735 | ---------------------------------------------------------------------- |
2736 | escape sequence description | |
2737 | ---------------------------------------------------------------------- | |
2738 | ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0 | |
2739 | ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1 | |
2740 | ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2 | |
2741 | ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3 | |
2742 | ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*) | |
2743 | ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1 | |
2744 | ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2 | |
2745 | ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3 | |
2746 | ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**) | |
2747 | ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1 | |
2748 | ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2 | |
2749 | ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3 | |
2750 | ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*) | |
2751 | ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1 | |
2752 | ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2 | |
2753 | ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3 | |
2754 | ---------------------------------------------------------------------- | |
2755 | ||
2756 | In this list, "DIMENSION1_CHARS94<F>" means a graphic character set | |
39787efd | 2757 | of dimension 1, chars 94, and final character <F>, etc... |
4ed46869 KH |
2758 | |
2759 | Note (*): Although these designations are not allowed in ISO2022, | |
2760 | Emacs accepts them on decoding, and produces them on encoding | |
39787efd | 2761 | CHARS96 character sets in a coding system which is characterized as |
4ed46869 KH |
2762 | 7-bit environment, non-locking-shift, and non-single-shift. |
2763 | ||
2764 | Note (**): If <F> is '@', 'A', or 'B', the intermediate character | |
df7492f9 | 2765 | '(' must be omitted. We refer to this as "short-form" hereafter. |
4ed46869 | 2766 | |
cfb43547 | 2767 | Now you may notice that there are a lot of ways of encoding the |
39787efd KH |
2768 | same multilingual text in ISO2022. Actually, there exist many |
2769 | coding systems such as Compound Text (used in X11's inter client | |
8ca3766a DL |
2770 | communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR |
2771 | (used in Korean Internet), EUC (Extended UNIX Code, used in Asian | |
4ed46869 KH |
2772 | localized platforms), and all of these are variants of ISO2022. |
2773 | ||
2774 | In addition to the above, Emacs handles two more kinds of escape | |
2775 | sequences: ISO6429's direction specification and Emacs' private | |
2776 | sequence for specifying character composition. | |
2777 | ||
39787efd | 2778 | ISO6429's direction specification takes the following form: |
4ed46869 KH |
2779 | o CSI ']' -- end of the current direction |
2780 | o CSI '0' ']' -- end of the current direction | |
2781 | o CSI '1' ']' -- start of left-to-right text | |
2782 | o CSI '2' ']' -- start of right-to-left text | |
2783 | The control character CSI (0x9B: control sequence introducer) is | |
39787efd KH |
2784 | abbreviated to the escape sequence ESC '[' in a 7-bit environment. |
2785 | ||
2786 | Character composition specification takes the following form: | |
ec6d2bb8 KH |
2787 | o ESC '0' -- start relative composition |
2788 | o ESC '1' -- end composition | |
2789 | o ESC '2' -- start rule-base composition (*) | |
2790 | o ESC '3' -- start relative composition with alternate chars (**) | |
2791 | o ESC '4' -- start rule-base composition with alternate chars (**) | |
b73bfc1c | 2792 | Since these are not standard escape sequences of any ISO standard, |
cfb43547 | 2793 | the use of them with these meanings is restricted to Emacs only. |
ec6d2bb8 | 2794 | |
5a936b46 DL |
2795 | (*) This form is used only in Emacs 20.7 and older versions, |
2796 | but newer versions can safely decode it. | |
cfb43547 | 2797 | (**) This form is used only in Emacs 21.1 and newer versions, |
5a936b46 | 2798 | and older versions can't decode it. |
ec6d2bb8 | 2799 | |
cfb43547 | 2800 | Here's a list of example usages of these composition escape |
b73bfc1c | 2801 | sequences (categorized by `enum composition_method'). |
ec6d2bb8 | 2802 | |
b73bfc1c | 2803 | COMPOSITION_RELATIVE: |
ec6d2bb8 | 2804 | ESC 0 CHAR [ CHAR ] ESC 1 |
8ca3766a | 2805 | COMPOSITION_WITH_RULE: |
ec6d2bb8 | 2806 | ESC 2 CHAR [ RULE CHAR ] ESC 1 |
b73bfc1c | 2807 | COMPOSITION_WITH_ALTCHARS: |
ec6d2bb8 | 2808 | ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 |
b73bfc1c | 2809 | COMPOSITION_WITH_RULE_ALTCHARS: |
ec6d2bb8 | 2810 | ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */ |
4ed46869 | 2811 | |
74ab6df5 | 2812 | static enum iso_code_class_type iso_code_class[256]; |
4ed46869 | 2813 | |
df7492f9 KH |
2814 | #define SAFE_CHARSET_P(coding, id) \ |
2815 | ((id) <= (coding)->max_charset_id \ | |
1b3b981b | 2816 | && (coding)->safe_charsets[id] != 255) |
df7492f9 | 2817 | |
df7492f9 | 2818 | static void |
971de7fb | 2819 | setup_iso_safe_charsets (Lisp_Object attrs) |
df7492f9 KH |
2820 | { |
2821 | Lisp_Object charset_list, safe_charsets; | |
2822 | Lisp_Object request; | |
2823 | Lisp_Object reg_usage; | |
2824 | Lisp_Object tail; | |
d311d28c | 2825 | EMACS_INT reg94, reg96; |
df7492f9 KH |
2826 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); |
2827 | int max_charset_id; | |
2828 | ||
2829 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
2830 | if ((flags & CODING_ISO_FLAG_FULL_SUPPORT) | |
2831 | && ! EQ (charset_list, Viso_2022_charset_list)) | |
2832 | { | |
4939150c PE |
2833 | charset_list = Viso_2022_charset_list; |
2834 | ASET (attrs, coding_attr_charset_list, charset_list); | |
df7492f9 KH |
2835 | ASET (attrs, coding_attr_safe_charsets, Qnil); |
2836 | } | |
2837 | ||
2838 | if (STRINGP (AREF (attrs, coding_attr_safe_charsets))) | |
2839 | return; | |
2840 | ||
2841 | max_charset_id = 0; | |
2842 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) | |
2843 | { | |
2844 | int id = XINT (XCAR (tail)); | |
2845 | if (max_charset_id < id) | |
2846 | max_charset_id = id; | |
2847 | } | |
d46c5b12 | 2848 | |
1b3b981b AS |
2849 | safe_charsets = make_uninit_string (max_charset_id + 1); |
2850 | memset (SDATA (safe_charsets), 255, max_charset_id + 1); | |
df7492f9 KH |
2851 | request = AREF (attrs, coding_attr_iso_request); |
2852 | reg_usage = AREF (attrs, coding_attr_iso_usage); | |
2853 | reg94 = XINT (XCAR (reg_usage)); | |
2854 | reg96 = XINT (XCDR (reg_usage)); | |
2855 | ||
2856 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) | |
2857 | { | |
2858 | Lisp_Object id; | |
2859 | Lisp_Object reg; | |
2860 | struct charset *charset; | |
2861 | ||
2862 | id = XCAR (tail); | |
2863 | charset = CHARSET_FROM_ID (XINT (id)); | |
bf16eb23 | 2864 | reg = Fcdr (Fassq (id, request)); |
df7492f9 | 2865 | if (! NILP (reg)) |
8f924df7 | 2866 | SSET (safe_charsets, XINT (id), XINT (reg)); |
df7492f9 KH |
2867 | else if (charset->iso_chars_96) |
2868 | { | |
2869 | if (reg96 < 4) | |
8f924df7 | 2870 | SSET (safe_charsets, XINT (id), reg96); |
df7492f9 KH |
2871 | } |
2872 | else | |
2873 | { | |
2874 | if (reg94 < 4) | |
8f924df7 | 2875 | SSET (safe_charsets, XINT (id), reg94); |
df7492f9 KH |
2876 | } |
2877 | } | |
2878 | ASET (attrs, coding_attr_safe_charsets, safe_charsets); | |
2879 | } | |
d46c5b12 | 2880 | |
b6871cc7 | 2881 | |
4ed46869 | 2882 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". |
f10fe38f PE |
2883 | Return true if a text is encoded in one of ISO-2022 based coding |
2884 | systems. */ | |
4ed46869 | 2885 | |
f10fe38f | 2886 | static bool |
cf84bb53 JB |
2887 | detect_coding_iso_2022 (struct coding_system *coding, |
2888 | struct coding_detection_info *detect_info) | |
4ed46869 | 2889 | { |
8f924df7 KH |
2890 | const unsigned char *src = coding->source, *src_base = src; |
2891 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
f10fe38f PE |
2892 | bool multibytep = coding->src_multibyte; |
2893 | bool single_shifting = 0; | |
0e48bb22 | 2894 | int id; |
df7492f9 | 2895 | int c, c1; |
d311d28c | 2896 | ptrdiff_t consumed_chars = 0; |
df7492f9 | 2897 | int i; |
ff0dacd7 KH |
2898 | int rejected = 0; |
2899 | int found = 0; | |
cee53ed4 | 2900 | int composition_count = -1; |
ff0dacd7 KH |
2901 | |
2902 | detect_info->checked |= CATEGORY_MASK_ISO; | |
df7492f9 KH |
2903 | |
2904 | for (i = coding_category_iso_7; i <= coding_category_iso_8_else; i++) | |
2905 | { | |
2906 | struct coding_system *this = &(coding_categories[i]); | |
2907 | Lisp_Object attrs, val; | |
2908 | ||
c6b278e7 KH |
2909 | if (this->id < 0) |
2910 | continue; | |
df7492f9 KH |
2911 | attrs = CODING_ID_ATTRS (this->id); |
2912 | if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT | |
1b3b981b | 2913 | && ! EQ (CODING_ATTR_CHARSET_LIST (attrs), Viso_2022_charset_list)) |
df7492f9 KH |
2914 | setup_iso_safe_charsets (attrs); |
2915 | val = CODING_ATTR_SAFE_CHARSETS (attrs); | |
8f924df7 | 2916 | this->max_charset_id = SCHARS (val) - 1; |
1b3b981b | 2917 | this->safe_charsets = SDATA (val); |
df7492f9 KH |
2918 | } |
2919 | ||
2920 | /* A coding system of this category is always ASCII compatible. */ | |
2921 | src += coding->head_ascii; | |
3f003981 | 2922 | |
ff0dacd7 | 2923 | while (rejected != CATEGORY_MASK_ISO) |
4ed46869 | 2924 | { |
065e3595 | 2925 | src_base = src; |
df7492f9 | 2926 | ONE_MORE_BYTE (c); |
4ed46869 KH |
2927 | switch (c) |
2928 | { | |
2929 | case ISO_CODE_ESC: | |
74383408 KH |
2930 | if (inhibit_iso_escape_detection) |
2931 | break; | |
f46869e4 | 2932 | single_shifting = 0; |
df7492f9 | 2933 | ONE_MORE_BYTE (c); |
0e48bb22 | 2934 | if (c == 'N' || c == 'O') |
d46c5b12 | 2935 | { |
ae9ff118 | 2936 | /* ESC <Fe> for SS2 or SS3. */ |
ff0dacd7 KH |
2937 | single_shifting = 1; |
2938 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_8BIT; | |
4ed46869 | 2939 | } |
cee53ed4 KH |
2940 | else if (c == '1') |
2941 | { | |
2942 | /* End of composition. */ | |
2943 | if (composition_count < 0 | |
2944 | || composition_count > MAX_COMPOSITION_COMPONENTS) | |
2945 | /* Invalid */ | |
2946 | break; | |
2947 | composition_count = -1; | |
2948 | found |= CATEGORY_MASK_ISO; | |
2949 | } | |
ec6d2bb8 KH |
2950 | else if (c >= '0' && c <= '4') |
2951 | { | |
2952 | /* ESC <Fp> for start/end composition. */ | |
cee53ed4 | 2953 | composition_count = 0; |
ec6d2bb8 | 2954 | } |
bf9cdd4e | 2955 | else |
df7492f9 | 2956 | { |
0e48bb22 AS |
2957 | if (c >= '(' && c <= '/') |
2958 | { | |
2959 | /* Designation sequence for a charset of dimension 1. */ | |
2960 | ONE_MORE_BYTE (c1); | |
2961 | if (c1 < ' ' || c1 >= 0x80 | |
2962 | || (id = iso_charset_table[0][c >= ','][c1]) < 0) | |
2963 | /* Invalid designation sequence. Just ignore. */ | |
2964 | break; | |
2965 | } | |
2966 | else if (c == '$') | |
2967 | { | |
2968 | /* Designation sequence for a charset of dimension 2. */ | |
2969 | ONE_MORE_BYTE (c); | |
2970 | if (c >= '@' && c <= 'B') | |
2971 | /* Designation for JISX0208.1978, GB2312, or JISX0208. */ | |
2972 | id = iso_charset_table[1][0][c]; | |
2973 | else if (c >= '(' && c <= '/') | |
2974 | { | |
2975 | ONE_MORE_BYTE (c1); | |
2976 | if (c1 < ' ' || c1 >= 0x80 | |
2977 | || (id = iso_charset_table[1][c >= ','][c1]) < 0) | |
2978 | /* Invalid designation sequence. Just ignore. */ | |
2979 | break; | |
2980 | } | |
2981 | else | |
2982 | /* Invalid designation sequence. Just ignore it. */ | |
2983 | break; | |
2984 | } | |
2985 | else | |
2986 | { | |
2987 | /* Invalid escape sequence. Just ignore it. */ | |
2988 | break; | |
2989 | } | |
d46c5b12 | 2990 | |
0e48bb22 AS |
2991 | /* We found a valid designation sequence for CHARSET. */ |
2992 | rejected |= CATEGORY_MASK_ISO_8BIT; | |
2993 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7], | |
2994 | id)) | |
2995 | found |= CATEGORY_MASK_ISO_7; | |
2996 | else | |
2997 | rejected |= CATEGORY_MASK_ISO_7; | |
2998 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7_tight], | |
2999 | id)) | |
3000 | found |= CATEGORY_MASK_ISO_7_TIGHT; | |
3001 | else | |
3002 | rejected |= CATEGORY_MASK_ISO_7_TIGHT; | |
3003 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7_else], | |
3004 | id)) | |
3005 | found |= CATEGORY_MASK_ISO_7_ELSE; | |
3006 | else | |
3007 | rejected |= CATEGORY_MASK_ISO_7_ELSE; | |
3008 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_8_else], | |
3009 | id)) | |
3010 | found |= CATEGORY_MASK_ISO_8_ELSE; | |
3011 | else | |
3012 | rejected |= CATEGORY_MASK_ISO_8_ELSE; | |
3013 | } | |
4ed46869 KH |
3014 | break; |
3015 | ||
4ed46869 | 3016 | case ISO_CODE_SO: |
d46c5b12 | 3017 | case ISO_CODE_SI: |
ff0dacd7 | 3018 | /* Locking shift out/in. */ |
74383408 KH |
3019 | if (inhibit_iso_escape_detection) |
3020 | break; | |
f46869e4 | 3021 | single_shifting = 0; |
ff0dacd7 | 3022 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_8BIT; |
d46c5b12 KH |
3023 | break; |
3024 | ||
4ed46869 | 3025 | case ISO_CODE_CSI: |
ff0dacd7 | 3026 | /* Control sequence introducer. */ |
f46869e4 | 3027 | single_shifting = 0; |
ff0dacd7 KH |
3028 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE; |
3029 | found |= CATEGORY_MASK_ISO_8_ELSE; | |
3030 | goto check_extra_latin; | |
3031 | ||
4ed46869 KH |
3032 | case ISO_CODE_SS2: |
3033 | case ISO_CODE_SS3: | |
ff0dacd7 KH |
3034 | /* Single shift. */ |
3035 | if (inhibit_iso_escape_detection) | |
3036 | break; | |
75e2a253 | 3037 | single_shifting = 0; |
ff0dacd7 KH |
3038 | rejected |= CATEGORY_MASK_ISO_7BIT; |
3039 | if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1]) | |
3040 | & CODING_ISO_FLAG_SINGLE_SHIFT) | |
0e48bb22 AS |
3041 | { |
3042 | found |= CATEGORY_MASK_ISO_8_1; | |
3043 | single_shifting = 1; | |
3044 | } | |
ff0dacd7 KH |
3045 | if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_2]) |
3046 | & CODING_ISO_FLAG_SINGLE_SHIFT) | |
0e48bb22 AS |
3047 | { |
3048 | found |= CATEGORY_MASK_ISO_8_2; | |
3049 | single_shifting = 1; | |
3050 | } | |
75e2a253 KH |
3051 | if (single_shifting) |
3052 | break; | |
ba14c607 | 3053 | goto check_extra_latin; |
4ed46869 KH |
3054 | |
3055 | default: | |
065e3595 KH |
3056 | if (c < 0) |
3057 | continue; | |
4ed46869 | 3058 | if (c < 0x80) |
f46869e4 | 3059 | { |
cee53ed4 KH |
3060 | if (composition_count >= 0) |
3061 | composition_count++; | |
f46869e4 KH |
3062 | single_shifting = 0; |
3063 | break; | |
3064 | } | |
ff0dacd7 | 3065 | if (c >= 0xA0) |
c4825358 | 3066 | { |
ff0dacd7 KH |
3067 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE; |
3068 | found |= CATEGORY_MASK_ISO_8_1; | |
f46869e4 | 3069 | /* Check the length of succeeding codes of the range |
ff0dacd7 KH |
3070 | 0xA0..0FF. If the byte length is even, we include |
3071 | CATEGORY_MASK_ISO_8_2 in `found'. We can check this | |
3072 | only when we are not single shifting. */ | |
3073 | if (! single_shifting | |
3074 | && ! (rejected & CATEGORY_MASK_ISO_8_2)) | |
f46869e4 | 3075 | { |
2735d060 | 3076 | int len = 1; |
b73bfc1c KH |
3077 | while (src < src_end) |
3078 | { | |
d12bd917 | 3079 | src_base = src; |
df7492f9 | 3080 | ONE_MORE_BYTE (c); |
b73bfc1c | 3081 | if (c < 0xA0) |
d12bd917 KH |
3082 | { |
3083 | src = src_base; | |
3084 | break; | |
3085 | } | |
2735d060 | 3086 | len++; |
b73bfc1c KH |
3087 | } |
3088 | ||
2735d060 | 3089 | if (len & 1 && src < src_end) |
cee53ed4 KH |
3090 | { |
3091 | rejected |= CATEGORY_MASK_ISO_8_2; | |
3092 | if (composition_count >= 0) | |
2735d060 | 3093 | composition_count += len; |
cee53ed4 | 3094 | } |
f46869e4 | 3095 | else |
cee53ed4 KH |
3096 | { |
3097 | found |= CATEGORY_MASK_ISO_8_2; | |
3098 | if (composition_count >= 0) | |
2735d060 | 3099 | composition_count += len / 2; |
cee53ed4 | 3100 | } |
f46869e4 | 3101 | } |
ff0dacd7 | 3102 | break; |
4ed46869 | 3103 | } |
ba14c607 AS |
3104 | check_extra_latin: |
3105 | if (! VECTORP (Vlatin_extra_code_table) | |
3106 | || NILP (AREF (Vlatin_extra_code_table, c))) | |
3107 | { | |
3108 | rejected = CATEGORY_MASK_ISO; | |
3109 | break; | |
3110 | } | |
3111 | if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1]) | |
3112 | & CODING_ISO_FLAG_LATIN_EXTRA) | |
3113 | found |= CATEGORY_MASK_ISO_8_1; | |
3114 | else | |
3115 | rejected |= CATEGORY_MASK_ISO_8_1; | |
3116 | rejected |= CATEGORY_MASK_ISO_8_2; | |
3117 | break; | |
4ed46869 KH |
3118 | } |
3119 | } | |
ff0dacd7 KH |
3120 | detect_info->rejected |= CATEGORY_MASK_ISO; |
3121 | return 0; | |
4ed46869 | 3122 | |
df7492f9 | 3123 | no_more_source: |
ff0dacd7 KH |
3124 | detect_info->rejected |= rejected; |
3125 | detect_info->found |= (found & ~rejected); | |
df7492f9 | 3126 | return 1; |
4ed46869 | 3127 | } |
ec6d2bb8 | 3128 | |
4ed46869 | 3129 | |
134b9549 KH |
3130 | /* Set designation state into CODING. Set CHARS_96 to -1 if the |
3131 | escape sequence should be kept. */ | |
df7492f9 KH |
3132 | #define DECODE_DESIGNATION(reg, dim, chars_96, final) \ |
3133 | do { \ | |
3134 | int id, prev; \ | |
3135 | \ | |
3136 | if (final < '0' || final >= 128 \ | |
3137 | || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \ | |
3138 | || !SAFE_CHARSET_P (coding, id)) \ | |
3139 | { \ | |
3140 | CODING_ISO_DESIGNATION (coding, reg) = -2; \ | |
134b9549 KH |
3141 | chars_96 = -1; \ |
3142 | break; \ | |
df7492f9 KH |
3143 | } \ |
3144 | prev = CODING_ISO_DESIGNATION (coding, reg); \ | |
bf16eb23 KH |
3145 | if (id == charset_jisx0201_roman) \ |
3146 | { \ | |
3147 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \ | |
3148 | id = charset_ascii; \ | |
3149 | } \ | |
3150 | else if (id == charset_jisx0208_1978) \ | |
3151 | { \ | |
3152 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \ | |
3153 | id = charset_jisx0208; \ | |
3154 | } \ | |
df7492f9 KH |
3155 | CODING_ISO_DESIGNATION (coding, reg) = id; \ |
3156 | /* If there was an invalid designation to REG previously, and this \ | |
3157 | designation is ASCII to REG, we should keep this designation \ | |
3158 | sequence. */ \ | |
3159 | if (prev == -2 && id == charset_ascii) \ | |
134b9549 | 3160 | chars_96 = -1; \ |
4ed46869 KH |
3161 | } while (0) |
3162 | ||
d46c5b12 | 3163 | |
e951386e KH |
3164 | /* Handle these composition sequence (ALT: alternate char): |
3165 | ||
3166 | (1) relative composition: ESC 0 CHAR ... ESC 1 | |
3167 | (2) rulebase composition: ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1 | |
3168 | (3) altchar composition: ESC 3 ALT ... ALT ESC 0 CHAR ... ESC 1 | |
3169 | (4) alt&rule composition: ESC 4 ALT RULE ... ALT ESC 0 CHAR ... ESC 1 | |
3170 | ||
3171 | When the start sequence (ESC 0/2/3/4) is found, this annotation | |
3172 | header is produced. | |
3173 | ||
3174 | [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) 0 METHOD ] | |
3175 | ||
3176 | Then, upon reading CHAR or RULE (one or two bytes), these codes are | |
3177 | produced until the end sequence (ESC 1) is found: | |
3178 | ||
3179 | (1) CHAR ... CHAR | |
3180 | (2) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR | |
3181 | (3) ALT ... ALT -1 -1 CHAR ... CHAR | |
3182 | (4) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT -1 -1 CHAR ... CHAR | |
3183 | ||
3184 | When the end sequence (ESC 1) is found, LENGTH and NCHARS in the | |
3185 | annotation header is updated as below: | |
3186 | ||
3187 | (1) LENGTH: unchanged, NCHARS: number of CHARs | |
3188 | (2) LENGTH: unchanged, NCHARS: number of CHARs | |
3189 | (3) LENGTH: += number of ALTs + 2, NCHARS: number of CHARs | |
3190 | (4) LENGTH: += number of ALTs * 3, NCHARS: number of CHARs | |
3191 | ||
3192 | If an error is found while composing, the annotation header is | |
3193 | changed to: | |
3194 | ||
3195 | [ ESC '0'/'2'/'3'/'4' -2 0 ] | |
3196 | ||
3197 | and the sequence [ -2 DECODED-RULE ] is changed to the original | |
3198 | byte sequence as below: | |
3199 | o the original byte sequence is B: [ B -1 ] | |
3200 | o the original byte sequence is B1 B2: [ B1 B2 ] | |
3201 | and the sequence [ -1 -1 ] is changed to the original byte | |
3202 | sequence: | |
3203 | [ ESC '0' ] | |
3204 | */ | |
3205 | ||
3206 | /* Decode a composition rule C1 and maybe one more byte from the | |
66ebf983 | 3207 | source, and set RULE to the encoded composition rule. If the rule |
d5efd1d1 | 3208 | is invalid, goto invalid_code. */ |
e951386e | 3209 | |
66ebf983 | 3210 | #define DECODE_COMPOSITION_RULE(rule) \ |
e951386e KH |
3211 | do { \ |
3212 | rule = c1 - 32; \ | |
3213 | if (rule < 0) \ | |
d5efd1d1 | 3214 | goto invalid_code; \ |
e951386e KH |
3215 | if (rule < 81) /* old format (before ver.21) */ \ |
3216 | { \ | |
3217 | int gref = (rule) / 9; \ | |
3218 | int nref = (rule) % 9; \ | |
3219 | if (gref == 4) gref = 10; \ | |
3220 | if (nref == 4) nref = 10; \ | |
3221 | rule = COMPOSITION_ENCODE_RULE (gref, nref); \ | |
e951386e KH |
3222 | } \ |
3223 | else /* new format (after ver.21) */ \ | |
3224 | { \ | |
2735d060 | 3225 | int b; \ |
e951386e | 3226 | \ |
2735d060 | 3227 | ONE_MORE_BYTE (b); \ |
d5efd1d1 PE |
3228 | if (! COMPOSITION_ENCODE_RULE_VALID (rule - 81, b - 32)) \ |
3229 | goto invalid_code; \ | |
2735d060 | 3230 | rule = COMPOSITION_ENCODE_RULE (rule - 81, b - 32); \ |
d5efd1d1 | 3231 | rule += 0x100; /* Distinguish it from the old format. */ \ |
e951386e KH |
3232 | } \ |
3233 | } while (0) | |
3234 | ||
3235 | #define ENCODE_COMPOSITION_RULE(rule) \ | |
df7492f9 | 3236 | do { \ |
e951386e KH |
3237 | int gref = (rule % 0x100) / 12, nref = (rule % 0x100) % 12; \ |
3238 | \ | |
3239 | if (rule < 0x100) /* old format */ \ | |
df7492f9 | 3240 | { \ |
e951386e KH |
3241 | if (gref == 10) gref = 4; \ |
3242 | if (nref == 10) nref = 4; \ | |
3243 | charbuf[idx] = 32 + gref * 9 + nref; \ | |
3244 | charbuf[idx + 1] = -1; \ | |
3245 | new_chars++; \ | |
df7492f9 | 3246 | } \ |
e951386e | 3247 | else /* new format */ \ |
df7492f9 | 3248 | { \ |
e951386e KH |
3249 | charbuf[idx] = 32 + 81 + gref; \ |
3250 | charbuf[idx + 1] = 32 + nref; \ | |
3251 | new_chars += 2; \ | |
df7492f9 KH |
3252 | } \ |
3253 | } while (0) | |
3254 | ||
e951386e KH |
3255 | /* Finish the current composition as invalid. */ |
3256 | ||
e951386e | 3257 | static int |
971de7fb | 3258 | finish_composition (int *charbuf, struct composition_status *cmp_status) |
e951386e KH |
3259 | { |
3260 | int idx = - cmp_status->length; | |
3261 | int new_chars; | |
3262 | ||
3263 | /* Recover the original ESC sequence */ | |
3264 | charbuf[idx++] = ISO_CODE_ESC; | |
3265 | charbuf[idx++] = (cmp_status->method == COMPOSITION_RELATIVE ? '0' | |
3266 | : cmp_status->method == COMPOSITION_WITH_RULE ? '2' | |
3267 | : cmp_status->method == COMPOSITION_WITH_ALTCHARS ? '3' | |
3268 | /* cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS */ | |
3269 | : '4'); | |
3270 | charbuf[idx++] = -2; | |
3271 | charbuf[idx++] = 0; | |
3272 | charbuf[idx++] = -1; | |
3273 | new_chars = cmp_status->nchars; | |
3274 | if (cmp_status->method >= COMPOSITION_WITH_RULE) | |
3275 | for (; idx < 0; idx++) | |
3276 | { | |
3277 | int elt = charbuf[idx]; | |
3278 | ||
3279 | if (elt == -2) | |
3280 | { | |
3281 | ENCODE_COMPOSITION_RULE (charbuf[idx + 1]); | |
3282 | idx++; | |
3283 | } | |
3284 | else if (elt == -1) | |
3285 | { | |
3286 | charbuf[idx++] = ISO_CODE_ESC; | |
3287 | charbuf[idx] = '0'; | |
3288 | new_chars += 2; | |
3289 | } | |
3290 | } | |
3291 | cmp_status->state = COMPOSING_NO; | |
3292 | return new_chars; | |
3293 | } | |
3294 | ||
ad1746f5 | 3295 | /* If characters are under composition, finish the composition. */ |
e951386e KH |
3296 | #define MAYBE_FINISH_COMPOSITION() \ |
3297 | do { \ | |
3298 | if (cmp_status->state != COMPOSING_NO) \ | |
3299 | char_offset += finish_composition (charbuf, cmp_status); \ | |
3300 | } while (0) | |
d46c5b12 | 3301 | |
aa72b389 | 3302 | /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4. |
e951386e | 3303 | |
aa72b389 KH |
3304 | ESC 0 : relative composition : ESC 0 CHAR ... ESC 1 |
3305 | ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1 | |
df7492f9 KH |
3306 | ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1 |
3307 | ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1 | |
ec6d2bb8 | 3308 | |
e951386e KH |
3309 | Produce this annotation sequence now: |
3310 | ||
3311 | [ -LENGTH(==-4) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) METHOD ] | |
3312 | */ | |
3313 | ||
3314 | #define DECODE_COMPOSITION_START(c1) \ | |
3315 | do { \ | |
3316 | if (c1 == '0' \ | |
3317 | && ((cmp_status->state == COMPOSING_COMPONENT_CHAR \ | |
3318 | && cmp_status->method == COMPOSITION_WITH_ALTCHARS) \ | |
3319 | || (cmp_status->state == COMPOSING_COMPONENT_RULE \ | |
3320 | && cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS))) \ | |
3321 | { \ | |
3322 | *charbuf++ = -1; \ | |
3323 | *charbuf++= -1; \ | |
3324 | cmp_status->state = COMPOSING_CHAR; \ | |
3325 | cmp_status->length += 2; \ | |
3326 | } \ | |
3327 | else \ | |
3328 | { \ | |
3329 | MAYBE_FINISH_COMPOSITION (); \ | |
3330 | cmp_status->method = (c1 == '0' ? COMPOSITION_RELATIVE \ | |
3331 | : c1 == '2' ? COMPOSITION_WITH_RULE \ | |
3332 | : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \ | |
3333 | : COMPOSITION_WITH_RULE_ALTCHARS); \ | |
3334 | cmp_status->state \ | |
3335 | = (c1 <= '2' ? COMPOSING_CHAR : COMPOSING_COMPONENT_CHAR); \ | |
3336 | ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \ | |
3337 | cmp_status->length = MAX_ANNOTATION_LENGTH; \ | |
3338 | cmp_status->nchars = cmp_status->ncomps = 0; \ | |
3339 | coding->annotated = 1; \ | |
3340 | } \ | |
ec6d2bb8 KH |
3341 | } while (0) |
3342 | ||
ec6d2bb8 | 3343 | |
e951386e | 3344 | /* Handle composition end sequence ESC 1. */ |
df7492f9 KH |
3345 | |
3346 | #define DECODE_COMPOSITION_END() \ | |
ec6d2bb8 | 3347 | do { \ |
e951386e KH |
3348 | if (cmp_status->nchars == 0 \ |
3349 | || ((cmp_status->state == COMPOSING_CHAR) \ | |
3350 | == (cmp_status->method == COMPOSITION_WITH_RULE))) \ | |
ec6d2bb8 | 3351 | { \ |
e951386e KH |
3352 | MAYBE_FINISH_COMPOSITION (); \ |
3353 | goto invalid_code; \ | |
ec6d2bb8 | 3354 | } \ |
e951386e KH |
3355 | if (cmp_status->method == COMPOSITION_WITH_ALTCHARS) \ |
3356 | charbuf[- cmp_status->length] -= cmp_status->ncomps + 2; \ | |
3357 | else if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS) \ | |
3358 | charbuf[- cmp_status->length] -= cmp_status->ncomps * 3; \ | |
3359 | charbuf[- cmp_status->length + 2] = cmp_status->nchars; \ | |
3360 | char_offset += cmp_status->nchars; \ | |
3361 | cmp_status->state = COMPOSING_NO; \ | |
ec6d2bb8 KH |
3362 | } while (0) |
3363 | ||
e951386e | 3364 | /* Store a composition rule RULE in charbuf, and update cmp_status. */ |
df7492f9 | 3365 | |
e951386e KH |
3366 | #define STORE_COMPOSITION_RULE(rule) \ |
3367 | do { \ | |
3368 | *charbuf++ = -2; \ | |
3369 | *charbuf++ = rule; \ | |
3370 | cmp_status->length += 2; \ | |
3371 | cmp_status->state--; \ | |
3372 | } while (0) | |
ec6d2bb8 | 3373 | |
e951386e KH |
3374 | /* Store a composed char or a component char C in charbuf, and update |
3375 | cmp_status. */ | |
3376 | ||
3377 | #define STORE_COMPOSITION_CHAR(c) \ | |
ec6d2bb8 | 3378 | do { \ |
e951386e KH |
3379 | *charbuf++ = (c); \ |
3380 | cmp_status->length++; \ | |
3381 | if (cmp_status->state == COMPOSING_CHAR) \ | |
3382 | cmp_status->nchars++; \ | |
df7492f9 | 3383 | else \ |
e951386e KH |
3384 | cmp_status->ncomps++; \ |
3385 | if (cmp_status->method == COMPOSITION_WITH_RULE \ | |
3386 | || (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS \ | |
3387 | && cmp_status->state == COMPOSING_COMPONENT_CHAR)) \ | |
3388 | cmp_status->state++; \ | |
ec6d2bb8 | 3389 | } while (0) |
88993dfd | 3390 | |
d46c5b12 | 3391 | |
4ed46869 KH |
3392 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ |
3393 | ||
b73bfc1c | 3394 | static void |
971de7fb | 3395 | decode_coding_iso_2022 (struct coding_system *coding) |
4ed46869 | 3396 | { |
8f924df7 KH |
3397 | const unsigned char *src = coding->source + coding->consumed; |
3398 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
3399 | const unsigned char *src_base; | |
69a80ea3 | 3400 | int *charbuf = coding->charbuf + coding->charbuf_used; |
ad1746f5 KH |
3401 | /* We may produce two annotations (charset and composition) in one |
3402 | loop and one more charset annotation at the end. */ | |
ff0dacd7 | 3403 | int *charbuf_end |
df80c7f0 | 3404 | = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 3); |
d311d28c | 3405 | ptrdiff_t consumed_chars = 0, consumed_chars_base; |
f10fe38f | 3406 | bool multibytep = coding->src_multibyte; |
4ed46869 | 3407 | /* Charsets invoked to graphic plane 0 and 1 respectively. */ |
df7492f9 KH |
3408 | int charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); |
3409 | int charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1); | |
134b9549 | 3410 | int charset_id_2, charset_id_3; |
df7492f9 KH |
3411 | struct charset *charset; |
3412 | int c; | |
e951386e | 3413 | struct composition_status *cmp_status = CODING_ISO_CMP_STATUS (coding); |
66ebf983 | 3414 | Lisp_Object attrs = CODING_ID_ATTRS (coding->id); |
d311d28c PE |
3415 | ptrdiff_t char_offset = coding->produced_char; |
3416 | ptrdiff_t last_offset = char_offset; | |
ff0dacd7 | 3417 | int last_id = charset_ascii; |
f10fe38f PE |
3418 | bool eol_dos |
3419 | = !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); | |
119852e7 | 3420 | int byte_after_cr = -1; |
e951386e | 3421 | int i; |
df7492f9 | 3422 | |
df7492f9 | 3423 | setup_iso_safe_charsets (attrs); |
1b3b981b | 3424 | coding->safe_charsets = SDATA (CODING_ATTR_SAFE_CHARSETS (attrs)); |
b73bfc1c | 3425 | |
e951386e KH |
3426 | if (cmp_status->state != COMPOSING_NO) |
3427 | { | |
15cbd324 | 3428 | if (charbuf_end - charbuf < cmp_status->length) |
1088b922 | 3429 | emacs_abort (); |
e951386e KH |
3430 | for (i = 0; i < cmp_status->length; i++) |
3431 | *charbuf++ = cmp_status->carryover[i]; | |
3432 | coding->annotated = 1; | |
3433 | } | |
3434 | ||
b73bfc1c | 3435 | while (1) |
4ed46869 | 3436 | { |
cf299835 | 3437 | int c1, c2, c3; |
b73bfc1c KH |
3438 | |
3439 | src_base = src; | |
df7492f9 KH |
3440 | consumed_chars_base = consumed_chars; |
3441 | ||
3442 | if (charbuf >= charbuf_end) | |
b71f6f73 KH |
3443 | { |
3444 | if (byte_after_cr >= 0) | |
3445 | src_base--; | |
3446 | break; | |
3447 | } | |
df7492f9 | 3448 | |
119852e7 KH |
3449 | if (byte_after_cr >= 0) |
3450 | c1 = byte_after_cr, byte_after_cr = -1; | |
3451 | else | |
3452 | ONE_MORE_BYTE (c1); | |
065e3595 KH |
3453 | if (c1 < 0) |
3454 | goto invalid_code; | |
4ed46869 | 3455 | |
e951386e | 3456 | if (CODING_ISO_EXTSEGMENT_LEN (coding) > 0) |
4ed46869 | 3457 | { |
e951386e KH |
3458 | *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1); |
3459 | char_offset++; | |
3460 | CODING_ISO_EXTSEGMENT_LEN (coding)--; | |
3461 | continue; | |
3462 | } | |
3463 | ||
3464 | if (CODING_ISO_EMBEDDED_UTF_8 (coding)) | |
3465 | { | |
3466 | if (c1 == ISO_CODE_ESC) | |
ec6d2bb8 | 3467 | { |
e951386e KH |
3468 | if (src + 1 >= src_end) |
3469 | goto no_more_source; | |
3470 | *charbuf++ = ISO_CODE_ESC; | |
3471 | char_offset++; | |
3472 | if (src[0] == '%' && src[1] == '@') | |
df7492f9 | 3473 | { |
e951386e KH |
3474 | src += 2; |
3475 | consumed_chars += 2; | |
3476 | char_offset += 2; | |
3477 | /* We are sure charbuf can contain two more chars. */ | |
3478 | *charbuf++ = '%'; | |
3479 | *charbuf++ = '@'; | |
3480 | CODING_ISO_EMBEDDED_UTF_8 (coding) = 0; | |
df7492f9 | 3481 | } |
4ed46869 | 3482 | } |
e951386e KH |
3483 | else |
3484 | { | |
3485 | *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1); | |
3486 | char_offset++; | |
3487 | } | |
3488 | continue; | |
3489 | } | |
3490 | ||
3491 | if ((cmp_status->state == COMPOSING_RULE | |
3492 | || cmp_status->state == COMPOSING_COMPONENT_RULE) | |
3493 | && c1 != ISO_CODE_ESC) | |
3494 | { | |
66ebf983 | 3495 | int rule; |
e951386e | 3496 | |
66ebf983 | 3497 | DECODE_COMPOSITION_RULE (rule); |
e951386e KH |
3498 | STORE_COMPOSITION_RULE (rule); |
3499 | continue; | |
3500 | } | |
3501 | ||
3502 | /* We produce at most one character. */ | |
3503 | switch (iso_code_class [c1]) | |
3504 | { | |
3505 | case ISO_0x20_or_0x7F: | |
df7492f9 KH |
3506 | if (charset_id_0 < 0 |
3507 | || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0))) | |
781d7a48 KH |
3508 | /* This is SPACE or DEL. */ |
3509 | charset = CHARSET_FROM_ID (charset_ascii); | |
3510 | else | |
3511 | charset = CHARSET_FROM_ID (charset_id_0); | |
3512 | break; | |
4ed46869 KH |
3513 | |
3514 | case ISO_graphic_plane_0: | |
134b9549 KH |
3515 | if (charset_id_0 < 0) |
3516 | charset = CHARSET_FROM_ID (charset_ascii); | |
3517 | else | |
3518 | charset = CHARSET_FROM_ID (charset_id_0); | |
4ed46869 KH |
3519 | break; |
3520 | ||
3521 | case ISO_0xA0_or_0xFF: | |
df7492f9 KH |
3522 | if (charset_id_1 < 0 |
3523 | || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1)) | |
3524 | || CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) | |
3525 | goto invalid_code; | |
4ed46869 KH |
3526 | /* This is a graphic character, we fall down ... */ |
3527 | ||
3528 | case ISO_graphic_plane_1: | |
df7492f9 KH |
3529 | if (charset_id_1 < 0) |
3530 | goto invalid_code; | |
3531 | charset = CHARSET_FROM_ID (charset_id_1); | |
4ed46869 KH |
3532 | break; |
3533 | ||
df7492f9 | 3534 | case ISO_control_0: |
2735d060 | 3535 | if (eol_dos && c1 == '\r') |
119852e7 | 3536 | ONE_MORE_BYTE (byte_after_cr); |
df7492f9 KH |
3537 | MAYBE_FINISH_COMPOSITION (); |
3538 | charset = CHARSET_FROM_ID (charset_ascii); | |
4ed46869 KH |
3539 | break; |
3540 | ||
df7492f9 | 3541 | case ISO_control_1: |
df7492f9 KH |
3542 | goto invalid_code; |
3543 | ||
4ed46869 | 3544 | case ISO_shift_out: |
df7492f9 KH |
3545 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT) |
3546 | || CODING_ISO_DESIGNATION (coding, 1) < 0) | |
3547 | goto invalid_code; | |
3548 | CODING_ISO_INVOCATION (coding, 0) = 1; | |
3549 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3550 | continue; |
4ed46869 KH |
3551 | |
3552 | case ISO_shift_in: | |
df7492f9 KH |
3553 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT)) |
3554 | goto invalid_code; | |
3555 | CODING_ISO_INVOCATION (coding, 0) = 0; | |
3556 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3557 | continue; |
4ed46869 KH |
3558 | |
3559 | case ISO_single_shift_2_7: | |
a63dba42 KH |
3560 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS)) |
3561 | goto invalid_code; | |
4ed46869 | 3562 | case ISO_single_shift_2: |
df7492f9 KH |
3563 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)) |
3564 | goto invalid_code; | |
4ed46869 KH |
3565 | /* SS2 is handled as an escape sequence of ESC 'N' */ |
3566 | c1 = 'N'; | |
3567 | goto label_escape_sequence; | |
3568 | ||
3569 | case ISO_single_shift_3: | |
df7492f9 KH |
3570 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)) |
3571 | goto invalid_code; | |
4ed46869 KH |
3572 | /* SS2 is handled as an escape sequence of ESC 'O' */ |
3573 | c1 = 'O'; | |
3574 | goto label_escape_sequence; | |
3575 | ||
3576 | case ISO_control_sequence_introducer: | |
3577 | /* CSI is handled as an escape sequence of ESC '[' ... */ | |
3578 | c1 = '['; | |
3579 | goto label_escape_sequence; | |
3580 | ||
3581 | case ISO_escape: | |
3582 | ONE_MORE_BYTE (c1); | |
3583 | label_escape_sequence: | |
df7492f9 | 3584 | /* Escape sequences handled here are invocation, |
4ed46869 KH |
3585 | designation, direction specification, and character |
3586 | composition specification. */ | |
3587 | switch (c1) | |
3588 | { | |
3589 | case '&': /* revision of following character set */ | |
3590 | ONE_MORE_BYTE (c1); | |
3591 | if (!(c1 >= '@' && c1 <= '~')) | |
df7492f9 | 3592 | goto invalid_code; |
4ed46869 KH |
3593 | ONE_MORE_BYTE (c1); |
3594 | if (c1 != ISO_CODE_ESC) | |
df7492f9 | 3595 | goto invalid_code; |
4ed46869 KH |
3596 | ONE_MORE_BYTE (c1); |
3597 | goto label_escape_sequence; | |
3598 | ||
3599 | case '$': /* designation of 2-byte character set */ | |
df7492f9 KH |
3600 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION)) |
3601 | goto invalid_code; | |
134b9549 KH |
3602 | { |
3603 | int reg, chars96; | |
3604 | ||
3605 | ONE_MORE_BYTE (c1); | |
3606 | if (c1 >= '@' && c1 <= 'B') | |
3607 | { /* designation of JISX0208.1978, GB2312.1980, | |
88993dfd | 3608 | or JISX0208.1980 */ |
134b9549 KH |
3609 | reg = 0, chars96 = 0; |
3610 | } | |
3611 | else if (c1 >= 0x28 && c1 <= 0x2B) | |
3612 | { /* designation of DIMENSION2_CHARS94 character set */ | |
3613 | reg = c1 - 0x28, chars96 = 0; | |
3614 | ONE_MORE_BYTE (c1); | |
3615 | } | |
3616 | else if (c1 >= 0x2C && c1 <= 0x2F) | |
3617 | { /* designation of DIMENSION2_CHARS96 character set */ | |
3618 | reg = c1 - 0x2C, chars96 = 1; | |
3619 | ONE_MORE_BYTE (c1); | |
3620 | } | |
3621 | else | |
3622 | goto invalid_code; | |
3623 | DECODE_DESIGNATION (reg, 2, chars96, c1); | |
3624 | /* We must update these variables now. */ | |
3625 | if (reg == 0) | |
3626 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
3627 | else if (reg == 1) | |
3628 | charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1); | |
3629 | if (chars96 < 0) | |
3630 | goto invalid_code; | |
3631 | } | |
b73bfc1c | 3632 | continue; |
4ed46869 KH |
3633 | |
3634 | case 'n': /* invocation of locking-shift-2 */ | |
df7492f9 KH |
3635 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT) |
3636 | || CODING_ISO_DESIGNATION (coding, 2) < 0) | |
3637 | goto invalid_code; | |
3638 | CODING_ISO_INVOCATION (coding, 0) = 2; | |
3639 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3640 | continue; |
4ed46869 KH |
3641 | |
3642 | case 'o': /* invocation of locking-shift-3 */ | |
df7492f9 KH |
3643 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT) |
3644 | || CODING_ISO_DESIGNATION (coding, 3) < 0) | |
3645 | goto invalid_code; | |
3646 | CODING_ISO_INVOCATION (coding, 0) = 3; | |
3647 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3648 | continue; |
4ed46869 KH |
3649 | |
3650 | case 'N': /* invocation of single-shift-2 */ | |
df7492f9 KH |
3651 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
3652 | || CODING_ISO_DESIGNATION (coding, 2) < 0) | |
3653 | goto invalid_code; | |
134b9549 KH |
3654 | charset_id_2 = CODING_ISO_DESIGNATION (coding, 2); |
3655 | if (charset_id_2 < 0) | |
3656 | charset = CHARSET_FROM_ID (charset_ascii); | |
3657 | else | |
3658 | charset = CHARSET_FROM_ID (charset_id_2); | |
b73bfc1c | 3659 | ONE_MORE_BYTE (c1); |
e7046a18 | 3660 | if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0)) |
df7492f9 | 3661 | goto invalid_code; |
4ed46869 KH |
3662 | break; |
3663 | ||
3664 | case 'O': /* invocation of single-shift-3 */ | |
df7492f9 KH |
3665 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
3666 | || CODING_ISO_DESIGNATION (coding, 3) < 0) | |
3667 | goto invalid_code; | |
134b9549 KH |
3668 | charset_id_3 = CODING_ISO_DESIGNATION (coding, 3); |
3669 | if (charset_id_3 < 0) | |
3670 | charset = CHARSET_FROM_ID (charset_ascii); | |
3671 | else | |
3672 | charset = CHARSET_FROM_ID (charset_id_3); | |
b73bfc1c | 3673 | ONE_MORE_BYTE (c1); |
e7046a18 | 3674 | if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0)) |
df7492f9 | 3675 | goto invalid_code; |
4ed46869 KH |
3676 | break; |
3677 | ||
ec6d2bb8 | 3678 | case '0': case '2': case '3': case '4': /* start composition */ |
df7492f9 KH |
3679 | if (! (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK)) |
3680 | goto invalid_code; | |
e951386e KH |
3681 | if (last_id != charset_ascii) |
3682 | { | |
3683 | ADD_CHARSET_DATA (charbuf, char_offset- last_offset, last_id); | |
3684 | last_id = charset_ascii; | |
3685 | last_offset = char_offset; | |
3686 | } | |
ec6d2bb8 | 3687 | DECODE_COMPOSITION_START (c1); |
b73bfc1c | 3688 | continue; |
4ed46869 | 3689 | |
ec6d2bb8 | 3690 | case '1': /* end composition */ |
e951386e | 3691 | if (cmp_status->state == COMPOSING_NO) |
df7492f9 KH |
3692 | goto invalid_code; |
3693 | DECODE_COMPOSITION_END (); | |
b73bfc1c | 3694 | continue; |
4ed46869 KH |
3695 | |
3696 | case '[': /* specification of direction */ | |
de59072a | 3697 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DIRECTION)) |
df7492f9 | 3698 | goto invalid_code; |
4ed46869 | 3699 | /* For the moment, nested direction is not supported. |
d46c5b12 | 3700 | So, `coding->mode & CODING_MODE_DIRECTION' zero means |
ad1746f5 | 3701 | left-to-right, and nonzero means right-to-left. */ |
4ed46869 KH |
3702 | ONE_MORE_BYTE (c1); |
3703 | switch (c1) | |
3704 | { | |
3705 | case ']': /* end of the current direction */ | |
d46c5b12 | 3706 | coding->mode &= ~CODING_MODE_DIRECTION; |
4ed46869 KH |
3707 | |
3708 | case '0': /* end of the current direction */ | |
3709 | case '1': /* start of left-to-right direction */ | |
3710 | ONE_MORE_BYTE (c1); | |
3711 | if (c1 == ']') | |
d46c5b12 | 3712 | coding->mode &= ~CODING_MODE_DIRECTION; |
4ed46869 | 3713 | else |
df7492f9 | 3714 | goto invalid_code; |
4ed46869 KH |
3715 | break; |
3716 | ||
3717 | case '2': /* start of right-to-left direction */ | |
3718 | ONE_MORE_BYTE (c1); | |
3719 | if (c1 == ']') | |
d46c5b12 | 3720 | coding->mode |= CODING_MODE_DIRECTION; |
4ed46869 | 3721 | else |
df7492f9 | 3722 | goto invalid_code; |
4ed46869 KH |
3723 | break; |
3724 | ||
3725 | default: | |
df7492f9 | 3726 | goto invalid_code; |
4ed46869 | 3727 | } |
b73bfc1c | 3728 | continue; |
4ed46869 | 3729 | |
103e0180 | 3730 | case '%': |
103e0180 KH |
3731 | ONE_MORE_BYTE (c1); |
3732 | if (c1 == '/') | |
3733 | { | |
3734 | /* CTEXT extended segment: | |
3735 | ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES-- | |
3736 | We keep these bytes as is for the moment. | |
3737 | They may be decoded by post-read-conversion. */ | |
3738 | int dim, M, L; | |
4776e638 | 3739 | int size; |
8f924df7 | 3740 | |
103e0180 | 3741 | ONE_MORE_BYTE (dim); |
7a84eee5 | 3742 | if (dim < '0' || dim > '4') |
e951386e | 3743 | goto invalid_code; |
103e0180 | 3744 | ONE_MORE_BYTE (M); |
e951386e KH |
3745 | if (M < 128) |
3746 | goto invalid_code; | |
103e0180 | 3747 | ONE_MORE_BYTE (L); |
e951386e KH |
3748 | if (L < 128) |
3749 | goto invalid_code; | |
103e0180 | 3750 | size = ((M - 128) * 128) + (L - 128); |
e951386e | 3751 | if (charbuf + 6 > charbuf_end) |
4776e638 KH |
3752 | goto break_loop; |
3753 | *charbuf++ = ISO_CODE_ESC; | |
3754 | *charbuf++ = '%'; | |
3755 | *charbuf++ = '/'; | |
3756 | *charbuf++ = dim; | |
3757 | *charbuf++ = BYTE8_TO_CHAR (M); | |
3758 | *charbuf++ = BYTE8_TO_CHAR (L); | |
e951386e | 3759 | CODING_ISO_EXTSEGMENT_LEN (coding) = size; |
103e0180 KH |
3760 | } |
3761 | else if (c1 == 'G') | |
3762 | { | |
103e0180 KH |
3763 | /* XFree86 extension for embedding UTF-8 in CTEXT: |
3764 | ESC % G --UTF-8-BYTES-- ESC % @ | |
3765 | We keep these bytes as is for the moment. | |
3766 | They may be decoded by post-read-conversion. */ | |
e951386e | 3767 | if (charbuf + 3 > charbuf_end) |
4776e638 | 3768 | goto break_loop; |
e951386e KH |
3769 | *charbuf++ = ISO_CODE_ESC; |
3770 | *charbuf++ = '%'; | |
3771 | *charbuf++ = 'G'; | |
3772 | CODING_ISO_EMBEDDED_UTF_8 (coding) = 1; | |
103e0180 KH |
3773 | } |
3774 | else | |
4776e638 | 3775 | goto invalid_code; |
103e0180 | 3776 | continue; |
4776e638 | 3777 | break; |
103e0180 | 3778 | |
4ed46869 | 3779 | default: |
df7492f9 KH |
3780 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION)) |
3781 | goto invalid_code; | |
134b9549 KH |
3782 | { |
3783 | int reg, chars96; | |
3784 | ||
3785 | if (c1 >= 0x28 && c1 <= 0x2B) | |
3786 | { /* designation of DIMENSION1_CHARS94 character set */ | |
3787 | reg = c1 - 0x28, chars96 = 0; | |
3788 | ONE_MORE_BYTE (c1); | |
3789 | } | |
3790 | else if (c1 >= 0x2C && c1 <= 0x2F) | |
3791 | { /* designation of DIMENSION1_CHARS96 character set */ | |
3792 | reg = c1 - 0x2C, chars96 = 1; | |
3793 | ONE_MORE_BYTE (c1); | |
3794 | } | |
3795 | else | |
3796 | goto invalid_code; | |
3797 | DECODE_DESIGNATION (reg, 1, chars96, c1); | |
3798 | /* We must update these variables now. */ | |
3799 | if (reg == 0) | |
3800 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
3801 | else if (reg == 1) | |
3802 | charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1); | |
3803 | if (chars96 < 0) | |
3804 | goto invalid_code; | |
3805 | } | |
b73bfc1c | 3806 | continue; |
4ed46869 | 3807 | } |
413bb2db PE |
3808 | break; |
3809 | ||
3810 | default: | |
1088b922 | 3811 | emacs_abort (); |
b73bfc1c | 3812 | } |
4ed46869 | 3813 | |
e951386e KH |
3814 | if (cmp_status->state == COMPOSING_NO |
3815 | && charset->id != charset_ascii | |
ff0dacd7 KH |
3816 | && last_id != charset->id) |
3817 | { | |
3818 | if (last_id != charset_ascii) | |
69a80ea3 | 3819 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
ff0dacd7 KH |
3820 | last_id = charset->id; |
3821 | last_offset = char_offset; | |
3822 | } | |
3823 | ||
b73bfc1c | 3824 | /* Now we know CHARSET and 1st position code C1 of a character. |
cf299835 KH |
3825 | Produce a decoded character while getting 2nd and 3rd |
3826 | position codes C2, C3 if necessary. */ | |
df7492f9 | 3827 | if (CHARSET_DIMENSION (charset) > 1) |
b73bfc1c KH |
3828 | { |
3829 | ONE_MORE_BYTE (c2); | |
cf299835 KH |
3830 | if (c2 < 0x20 || (c2 >= 0x80 && c2 < 0xA0) |
3831 | || ((c1 & 0x80) != (c2 & 0x80))) | |
b73bfc1c | 3832 | /* C2 is not in a valid range. */ |
df7492f9 | 3833 | goto invalid_code; |
cf299835 KH |
3834 | if (CHARSET_DIMENSION (charset) == 2) |
3835 | c1 = (c1 << 8) | c2; | |
3836 | else | |
df7492f9 | 3837 | { |
cf299835 KH |
3838 | ONE_MORE_BYTE (c3); |
3839 | if (c3 < 0x20 || (c3 >= 0x80 && c3 < 0xA0) | |
3840 | || ((c1 & 0x80) != (c3 & 0x80))) | |
3841 | /* C3 is not in a valid range. */ | |
df7492f9 | 3842 | goto invalid_code; |
cf299835 | 3843 | c1 = (c1 << 16) | (c2 << 8) | c2; |
df7492f9 KH |
3844 | } |
3845 | } | |
cf299835 | 3846 | c1 &= 0x7F7F7F; |
df7492f9 KH |
3847 | CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c1, c); |
3848 | if (c < 0) | |
3849 | { | |
3850 | MAYBE_FINISH_COMPOSITION (); | |
3851 | for (; src_base < src; src_base++, char_offset++) | |
3852 | { | |
3853 | if (ASCII_BYTE_P (*src_base)) | |
3854 | *charbuf++ = *src_base; | |
3855 | else | |
3856 | *charbuf++ = BYTE8_TO_CHAR (*src_base); | |
3857 | } | |
3858 | } | |
e951386e | 3859 | else if (cmp_status->state == COMPOSING_NO) |
df7492f9 KH |
3860 | { |
3861 | *charbuf++ = c; | |
3862 | char_offset++; | |
4ed46869 | 3863 | } |
e951386e KH |
3864 | else if ((cmp_status->state == COMPOSING_CHAR |
3865 | ? cmp_status->nchars | |
3866 | : cmp_status->ncomps) | |
3867 | >= MAX_COMPOSITION_COMPONENTS) | |
781d7a48 | 3868 | { |
e951386e KH |
3869 | /* Too long composition. */ |
3870 | MAYBE_FINISH_COMPOSITION (); | |
3871 | *charbuf++ = c; | |
3872 | char_offset++; | |
4ed46869 | 3873 | } |
e951386e KH |
3874 | else |
3875 | STORE_COMPOSITION_CHAR (c); | |
4ed46869 KH |
3876 | continue; |
3877 | ||
df7492f9 KH |
3878 | invalid_code: |
3879 | MAYBE_FINISH_COMPOSITION (); | |
4ed46869 | 3880 | src = src_base; |
df7492f9 KH |
3881 | consumed_chars = consumed_chars_base; |
3882 | ONE_MORE_BYTE (c); | |
065e3595 | 3883 | *charbuf++ = c < 0 ? -c : ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 3884 | char_offset++; |
df7492f9 | 3885 | coding->errors++; |
4776e638 KH |
3886 | continue; |
3887 | ||
3888 | break_loop: | |
3889 | break; | |
4ed46869 | 3890 | } |
fb88bf2d | 3891 | |
df7492f9 | 3892 | no_more_source: |
e951386e KH |
3893 | if (cmp_status->state != COMPOSING_NO) |
3894 | { | |
3895 | if (coding->mode & CODING_MODE_LAST_BLOCK) | |
3896 | MAYBE_FINISH_COMPOSITION (); | |
3897 | else | |
3898 | { | |
3899 | charbuf -= cmp_status->length; | |
3900 | for (i = 0; i < cmp_status->length; i++) | |
3901 | cmp_status->carryover[i] = charbuf[i]; | |
3902 | } | |
3903 | } | |
3904 | else if (last_id != charset_ascii) | |
69a80ea3 | 3905 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
3906 | coding->consumed_char += consumed_chars_base; |
3907 | coding->consumed = src_base - coding->source; | |
3908 | coding->charbuf_used = charbuf - coding->charbuf; | |
4ed46869 KH |
3909 | } |
3910 | ||
b73bfc1c | 3911 | |
f4dee582 | 3912 | /* ISO2022 encoding stuff. */ |
4ed46869 KH |
3913 | |
3914 | /* | |
f4dee582 | 3915 | It is not enough to say just "ISO2022" on encoding, we have to |
df7492f9 | 3916 | specify more details. In Emacs, each coding system of ISO2022 |
4ed46869 | 3917 | variant has the following specifications: |
df7492f9 | 3918 | 1. Initial designation to G0 thru G3. |
4ed46869 KH |
3919 | 2. Allows short-form designation? |
3920 | 3. ASCII should be designated to G0 before control characters? | |
3921 | 4. ASCII should be designated to G0 at end of line? | |
3922 | 5. 7-bit environment or 8-bit environment? | |
3923 | 6. Use locking-shift? | |
3924 | 7. Use Single-shift? | |
3925 | And the following two are only for Japanese: | |
3926 | 8. Use ASCII in place of JIS0201-1976-Roman? | |
3927 | 9. Use JISX0208-1983 in place of JISX0208-1978? | |
df7492f9 KH |
3928 | These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits |
3929 | defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more | |
f4dee582 | 3930 | details. |
4ed46869 KH |
3931 | */ |
3932 | ||
3933 | /* Produce codes (escape sequence) for designating CHARSET to graphic | |
b73bfc1c KH |
3934 | register REG at DST, and increment DST. If <final-char> of CHARSET is |
3935 | '@', 'A', or 'B' and the coding system CODING allows, produce | |
3936 | designation sequence of short-form. */ | |
4ed46869 KH |
3937 | |
3938 | #define ENCODE_DESIGNATION(charset, reg, coding) \ | |
3939 | do { \ | |
df7492f9 | 3940 | unsigned char final_char = CHARSET_ISO_FINAL (charset); \ |
675e2c69 DN |
3941 | const char *intermediate_char_94 = "()*+"; \ |
3942 | const char *intermediate_char_96 = ",-./"; \ | |
df7492f9 | 3943 | int revision = -1; \ |
df7492f9 KH |
3944 | \ |
3945 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \ | |
c197f191 | 3946 | revision = CHARSET_ISO_REVISION (charset); \ |
df7492f9 KH |
3947 | \ |
3948 | if (revision >= 0) \ | |
70c22245 | 3949 | { \ |
df7492f9 KH |
3950 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \ |
3951 | EMIT_ONE_BYTE ('@' + revision); \ | |
4ed46869 | 3952 | } \ |
df7492f9 | 3953 | EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \ |
4ed46869 KH |
3954 | if (CHARSET_DIMENSION (charset) == 1) \ |
3955 | { \ | |
2735d060 | 3956 | int b; \ |
df7492f9 | 3957 | if (! CHARSET_ISO_CHARS_96 (charset)) \ |
2735d060 | 3958 | b = intermediate_char_94[reg]; \ |
4ed46869 | 3959 | else \ |
2735d060 PE |
3960 | b = intermediate_char_96[reg]; \ |
3961 | EMIT_ONE_ASCII_BYTE (b); \ | |
4ed46869 KH |
3962 | } \ |
3963 | else \ | |
3964 | { \ | |
df7492f9 KH |
3965 | EMIT_ONE_ASCII_BYTE ('$'); \ |
3966 | if (! CHARSET_ISO_CHARS_96 (charset)) \ | |
4ed46869 | 3967 | { \ |
df7492f9 | 3968 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \ |
b73bfc1c KH |
3969 | || reg != 0 \ |
3970 | || final_char < '@' || final_char > 'B') \ | |
df7492f9 | 3971 | EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \ |
4ed46869 KH |
3972 | } \ |
3973 | else \ | |
df7492f9 | 3974 | EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \ |
4ed46869 | 3975 | } \ |
df7492f9 KH |
3976 | EMIT_ONE_ASCII_BYTE (final_char); \ |
3977 | \ | |
3978 | CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \ | |
4ed46869 KH |
3979 | } while (0) |
3980 | ||
df7492f9 | 3981 | |
4ed46869 KH |
3982 | /* The following two macros produce codes (control character or escape |
3983 | sequence) for ISO2022 single-shift functions (single-shift-2 and | |
3984 | single-shift-3). */ | |
3985 | ||
df7492f9 KH |
3986 | #define ENCODE_SINGLE_SHIFT_2 \ |
3987 | do { \ | |
3988 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ | |
3989 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \ | |
3990 | else \ | |
3991 | EMIT_ONE_BYTE (ISO_CODE_SS2); \ | |
3992 | CODING_ISO_SINGLE_SHIFTING (coding) = 1; \ | |
4ed46869 KH |
3993 | } while (0) |
3994 | ||
df7492f9 KH |
3995 | |
3996 | #define ENCODE_SINGLE_SHIFT_3 \ | |
3997 | do { \ | |
3998 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ | |
3999 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \ | |
4000 | else \ | |
4001 | EMIT_ONE_BYTE (ISO_CODE_SS3); \ | |
4002 | CODING_ISO_SINGLE_SHIFTING (coding) = 1; \ | |
4ed46869 KH |
4003 | } while (0) |
4004 | ||
df7492f9 | 4005 | |
4ed46869 KH |
4006 | /* The following four macros produce codes (control character or |
4007 | escape sequence) for ISO2022 locking-shift functions (shift-in, | |
4008 | shift-out, locking-shift-2, and locking-shift-3). */ | |
4009 | ||
df7492f9 KH |
4010 | #define ENCODE_SHIFT_IN \ |
4011 | do { \ | |
4012 | EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \ | |
4013 | CODING_ISO_INVOCATION (coding, 0) = 0; \ | |
4ed46869 KH |
4014 | } while (0) |
4015 | ||
df7492f9 KH |
4016 | |
4017 | #define ENCODE_SHIFT_OUT \ | |
4018 | do { \ | |
4019 | EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \ | |
4020 | CODING_ISO_INVOCATION (coding, 0) = 1; \ | |
4ed46869 KH |
4021 | } while (0) |
4022 | ||
df7492f9 KH |
4023 | |
4024 | #define ENCODE_LOCKING_SHIFT_2 \ | |
4025 | do { \ | |
4026 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \ | |
4027 | CODING_ISO_INVOCATION (coding, 0) = 2; \ | |
4ed46869 KH |
4028 | } while (0) |
4029 | ||
df7492f9 KH |
4030 | |
4031 | #define ENCODE_LOCKING_SHIFT_3 \ | |
4032 | do { \ | |
4033 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \ | |
4034 | CODING_ISO_INVOCATION (coding, 0) = 3; \ | |
4ed46869 KH |
4035 | } while (0) |
4036 | ||
df7492f9 | 4037 | |
f4dee582 RS |
4038 | /* Produce codes for a DIMENSION1 character whose character set is |
4039 | CHARSET and whose position-code is C1. Designation and invocation | |
4ed46869 KH |
4040 | sequences are also produced in advance if necessary. */ |
4041 | ||
6e85d753 KH |
4042 | #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \ |
4043 | do { \ | |
df7492f9 | 4044 | int id = CHARSET_ID (charset); \ |
bf16eb23 KH |
4045 | \ |
4046 | if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \ | |
4047 | && id == charset_ascii) \ | |
4048 | { \ | |
4049 | id = charset_jisx0201_roman; \ | |
4050 | charset = CHARSET_FROM_ID (id); \ | |
4051 | } \ | |
4052 | \ | |
df7492f9 | 4053 | if (CODING_ISO_SINGLE_SHIFTING (coding)) \ |
6e85d753 | 4054 | { \ |
df7492f9 KH |
4055 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ |
4056 | EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \ | |
6e85d753 | 4057 | else \ |
df7492f9 KH |
4058 | EMIT_ONE_BYTE (c1 | 0x80); \ |
4059 | CODING_ISO_SINGLE_SHIFTING (coding) = 0; \ | |
6e85d753 KH |
4060 | break; \ |
4061 | } \ | |
df7492f9 | 4062 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \ |
6e85d753 | 4063 | { \ |
df7492f9 | 4064 | EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \ |
6e85d753 KH |
4065 | break; \ |
4066 | } \ | |
df7492f9 | 4067 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \ |
6e85d753 | 4068 | { \ |
df7492f9 | 4069 | EMIT_ONE_BYTE (c1 | 0x80); \ |
6e85d753 KH |
4070 | break; \ |
4071 | } \ | |
6e85d753 KH |
4072 | else \ |
4073 | /* Since CHARSET is not yet invoked to any graphic planes, we \ | |
4074 | must invoke it, or, at first, designate it to some graphic \ | |
4075 | register. Then repeat the loop to actually produce the \ | |
4076 | character. */ \ | |
df7492f9 KH |
4077 | dst = encode_invocation_designation (charset, coding, dst, \ |
4078 | &produced_chars); \ | |
4ed46869 KH |
4079 | } while (1) |
4080 | ||
df7492f9 | 4081 | |
f4dee582 RS |
4082 | /* Produce codes for a DIMENSION2 character whose character set is |
4083 | CHARSET and whose position-codes are C1 and C2. Designation and | |
4ed46869 KH |
4084 | invocation codes are also produced in advance if necessary. */ |
4085 | ||
6e85d753 KH |
4086 | #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \ |
4087 | do { \ | |
df7492f9 | 4088 | int id = CHARSET_ID (charset); \ |
bf16eb23 KH |
4089 | \ |
4090 | if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \ | |
4091 | && id == charset_jisx0208) \ | |
4092 | { \ | |
4093 | id = charset_jisx0208_1978; \ | |
4094 | charset = CHARSET_FROM_ID (id); \ | |
4095 | } \ | |
4096 | \ | |
df7492f9 | 4097 | if (CODING_ISO_SINGLE_SHIFTING (coding)) \ |
6e85d753 | 4098 | { \ |
df7492f9 KH |
4099 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ |
4100 | EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \ | |
6e85d753 | 4101 | else \ |
df7492f9 KH |
4102 | EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \ |
4103 | CODING_ISO_SINGLE_SHIFTING (coding) = 0; \ | |
6e85d753 KH |
4104 | break; \ |
4105 | } \ | |
df7492f9 | 4106 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \ |
6e85d753 | 4107 | { \ |
df7492f9 | 4108 | EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \ |
6e85d753 KH |
4109 | break; \ |
4110 | } \ | |
df7492f9 | 4111 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \ |
6e85d753 | 4112 | { \ |
df7492f9 | 4113 | EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \ |
6e85d753 KH |
4114 | break; \ |
4115 | } \ | |
6e85d753 KH |
4116 | else \ |
4117 | /* Since CHARSET is not yet invoked to any graphic planes, we \ | |
4118 | must invoke it, or, at first, designate it to some graphic \ | |
4119 | register. Then repeat the loop to actually produce the \ | |
4120 | character. */ \ | |
df7492f9 KH |
4121 | dst = encode_invocation_designation (charset, coding, dst, \ |
4122 | &produced_chars); \ | |
4ed46869 KH |
4123 | } while (1) |
4124 | ||
05e6f5dc | 4125 | |
df7492f9 KH |
4126 | #define ENCODE_ISO_CHARACTER(charset, c) \ |
4127 | do { \ | |
8f50130c | 4128 | unsigned code; \ |
5eb05ea3 | 4129 | CODING_ENCODE_CHAR (coding, dst, dst_end, (charset), (c), code); \ |
df7492f9 KH |
4130 | \ |
4131 | if (CHARSET_DIMENSION (charset) == 1) \ | |
4132 | ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \ | |
4133 | else \ | |
4134 | ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \ | |
84fbb8a0 | 4135 | } while (0) |
bdd9fb48 | 4136 | |
05e6f5dc | 4137 | |
4ed46869 | 4138 | /* Produce designation and invocation codes at a place pointed by DST |
df7492f9 | 4139 | to use CHARSET. The element `spec.iso_2022' of *CODING is updated. |
4ed46869 KH |
4140 | Return new DST. */ |
4141 | ||
e2f1bab9 | 4142 | static unsigned char * |
cf84bb53 JB |
4143 | encode_invocation_designation (struct charset *charset, |
4144 | struct coding_system *coding, | |
d311d28c | 4145 | unsigned char *dst, ptrdiff_t *p_nchars) |
4ed46869 | 4146 | { |
f10fe38f | 4147 | bool multibytep = coding->dst_multibyte; |
d311d28c | 4148 | ptrdiff_t produced_chars = *p_nchars; |
4ed46869 | 4149 | int reg; /* graphic register number */ |
df7492f9 | 4150 | int id = CHARSET_ID (charset); |
4ed46869 KH |
4151 | |
4152 | /* At first, check designations. */ | |
4153 | for (reg = 0; reg < 4; reg++) | |
df7492f9 | 4154 | if (id == CODING_ISO_DESIGNATION (coding, reg)) |
4ed46869 KH |
4155 | break; |
4156 | ||
4157 | if (reg >= 4) | |
4158 | { | |
4159 | /* CHARSET is not yet designated to any graphic registers. */ | |
4160 | /* At first check the requested designation. */ | |
df7492f9 KH |
4161 | reg = CODING_ISO_REQUEST (coding, id); |
4162 | if (reg < 0) | |
1ba9e4ab KH |
4163 | /* Since CHARSET requests no special designation, designate it |
4164 | to graphic register 0. */ | |
4ed46869 KH |
4165 | reg = 0; |
4166 | ||
4167 | ENCODE_DESIGNATION (charset, reg, coding); | |
4168 | } | |
4169 | ||
df7492f9 KH |
4170 | if (CODING_ISO_INVOCATION (coding, 0) != reg |
4171 | && CODING_ISO_INVOCATION (coding, 1) != reg) | |
4ed46869 KH |
4172 | { |
4173 | /* Since the graphic register REG is not invoked to any graphic | |
4174 | planes, invoke it to graphic plane 0. */ | |
4175 | switch (reg) | |
4176 | { | |
4177 | case 0: /* graphic register 0 */ | |
4178 | ENCODE_SHIFT_IN; | |
4179 | break; | |
4180 | ||
4181 | case 1: /* graphic register 1 */ | |
4182 | ENCODE_SHIFT_OUT; | |
4183 | break; | |
4184 | ||
4185 | case 2: /* graphic register 2 */ | |
df7492f9 | 4186 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
4ed46869 KH |
4187 | ENCODE_SINGLE_SHIFT_2; |
4188 | else | |
4189 | ENCODE_LOCKING_SHIFT_2; | |
4190 | break; | |
4191 | ||
4192 | case 3: /* graphic register 3 */ | |
df7492f9 | 4193 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
4ed46869 KH |
4194 | ENCODE_SINGLE_SHIFT_3; |
4195 | else | |
4196 | ENCODE_LOCKING_SHIFT_3; | |
4197 | break; | |
4198 | } | |
4199 | } | |
b73bfc1c | 4200 | |
df7492f9 | 4201 | *p_nchars = produced_chars; |
4ed46869 KH |
4202 | return dst; |
4203 | } | |
4204 | ||
4ed46869 KH |
4205 | |
4206 | /* Produce codes for designation and invocation to reset the graphic | |
4207 | planes and registers to initial state. */ | |
df7492f9 KH |
4208 | #define ENCODE_RESET_PLANE_AND_REGISTER() \ |
4209 | do { \ | |
4210 | int reg; \ | |
4211 | struct charset *charset; \ | |
4212 | \ | |
4213 | if (CODING_ISO_INVOCATION (coding, 0) != 0) \ | |
4214 | ENCODE_SHIFT_IN; \ | |
4215 | for (reg = 0; reg < 4; reg++) \ | |
4216 | if (CODING_ISO_INITIAL (coding, reg) >= 0 \ | |
4217 | && (CODING_ISO_DESIGNATION (coding, reg) \ | |
4218 | != CODING_ISO_INITIAL (coding, reg))) \ | |
4219 | { \ | |
4220 | charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \ | |
4221 | ENCODE_DESIGNATION (charset, reg, coding); \ | |
4222 | } \ | |
4ed46869 KH |
4223 | } while (0) |
4224 | ||
df7492f9 | 4225 | |
bdd9fb48 | 4226 | /* Produce designation sequences of charsets in the line started from |
5eb05ea3 KH |
4227 | CHARBUF to a place pointed by DST, and return the number of |
4228 | produced bytes. DST should not directly point a buffer text area | |
4229 | which may be relocated by char_charset call. | |
bdd9fb48 KH |
4230 | |
4231 | If the current block ends before any end-of-line, we may fail to | |
d46c5b12 KH |
4232 | find all the necessary designations. */ |
4233 | ||
6e6c82a4 | 4234 | static ptrdiff_t |
5eb05ea3 KH |
4235 | encode_designation_at_bol (struct coding_system *coding, |
4236 | int *charbuf, int *charbuf_end, | |
461c2ab9 | 4237 | unsigned char *dst) |
e0e989f6 | 4238 | { |
75a3b399 | 4239 | unsigned char *orig = dst; |
df7492f9 | 4240 | struct charset *charset; |
bdd9fb48 KH |
4241 | /* Table of charsets to be designated to each graphic register. */ |
4242 | int r[4]; | |
df7492f9 | 4243 | int c, found = 0, reg; |
d311d28c | 4244 | ptrdiff_t produced_chars = 0; |
f10fe38f | 4245 | bool multibytep = coding->dst_multibyte; |
df7492f9 KH |
4246 | Lisp_Object attrs; |
4247 | Lisp_Object charset_list; | |
4248 | ||
4249 | attrs = CODING_ID_ATTRS (coding->id); | |
4250 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
4251 | if (EQ (charset_list, Qiso_2022)) | |
4252 | charset_list = Viso_2022_charset_list; | |
bdd9fb48 KH |
4253 | |
4254 | for (reg = 0; reg < 4; reg++) | |
4255 | r[reg] = -1; | |
4256 | ||
5eb05ea3 | 4257 | while (charbuf < charbuf_end && found < 4) |
e0e989f6 | 4258 | { |
df7492f9 KH |
4259 | int id; |
4260 | ||
4261 | c = *charbuf++; | |
b73bfc1c KH |
4262 | if (c == '\n') |
4263 | break; | |
df7492f9 KH |
4264 | charset = char_charset (c, charset_list, NULL); |
4265 | id = CHARSET_ID (charset); | |
4266 | reg = CODING_ISO_REQUEST (coding, id); | |
4267 | if (reg >= 0 && r[reg] < 0) | |
bdd9fb48 KH |
4268 | { |
4269 | found++; | |
df7492f9 | 4270 | r[reg] = id; |
bdd9fb48 | 4271 | } |
bdd9fb48 KH |
4272 | } |
4273 | ||
4274 | if (found) | |
4275 | { | |
4276 | for (reg = 0; reg < 4; reg++) | |
4277 | if (r[reg] >= 0 | |
df7492f9 KH |
4278 | && CODING_ISO_DESIGNATION (coding, reg) != r[reg]) |
4279 | ENCODE_DESIGNATION (CHARSET_FROM_ID (r[reg]), reg, coding); | |
e0e989f6 | 4280 | } |
b73bfc1c | 4281 | |
5eb05ea3 | 4282 | return dst - orig; |
e0e989f6 KH |
4283 | } |
4284 | ||
4ed46869 KH |
4285 | /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */ |
4286 | ||
f10fe38f | 4287 | static bool |
971de7fb | 4288 | encode_coding_iso_2022 (struct coding_system *coding) |
4ed46869 | 4289 | { |
f10fe38f | 4290 | bool multibytep = coding->dst_multibyte; |
df7492f9 KH |
4291 | int *charbuf = coding->charbuf; |
4292 | int *charbuf_end = charbuf + coding->charbuf_used; | |
4293 | unsigned char *dst = coding->destination + coding->produced; | |
4294 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
4295 | int safe_room = 16; | |
f10fe38f | 4296 | bool bol_designation |
df7492f9 KH |
4297 | = (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATE_AT_BOL |
4298 | && CODING_ISO_BOL (coding)); | |
d311d28c | 4299 | ptrdiff_t produced_chars = 0; |
df7492f9 | 4300 | Lisp_Object attrs, eol_type, charset_list; |
f10fe38f | 4301 | bool ascii_compatible; |
b73bfc1c | 4302 | int c; |
ff0dacd7 | 4303 | int preferred_charset_id = -1; |
05e6f5dc | 4304 | |
24a73b0a | 4305 | CODING_GET_INFO (coding, attrs, charset_list); |
0a9564cb | 4306 | eol_type = inhibit_eol_conversion ? Qunix : CODING_ID_EOL_TYPE (coding->id); |
24a73b0a KH |
4307 | if (VECTORP (eol_type)) |
4308 | eol_type = Qunix; | |
4309 | ||
004068e4 | 4310 | setup_iso_safe_charsets (attrs); |
ff0dacd7 | 4311 | /* Charset list may have been changed. */ |
287c57d7 | 4312 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); |
1b3b981b | 4313 | coding->safe_charsets = SDATA (CODING_ATTR_SAFE_CHARSETS (attrs)); |
0eecad43 | 4314 | |
a552b35a KH |
4315 | ascii_compatible |
4316 | = (! NILP (CODING_ATTR_ASCII_COMPAT (attrs)) | |
4317 | && ! (CODING_ISO_FLAGS (coding) & (CODING_ISO_FLAG_DESIGNATION | |
4318 | | CODING_ISO_FLAG_LOCKING_SHIFT))); | |
bdd9fb48 | 4319 | |
df7492f9 | 4320 | while (charbuf < charbuf_end) |
4ed46869 | 4321 | { |
df7492f9 | 4322 | ASSURE_DESTINATION (safe_room); |
b73bfc1c | 4323 | |
df7492f9 | 4324 | if (bol_designation) |
b73bfc1c | 4325 | { |
bdd9fb48 | 4326 | /* We have to produce designation sequences if any now. */ |
5eb05ea3 KH |
4327 | unsigned char desig_buf[16]; |
4328 | int nbytes; | |
8f50130c | 4329 | ptrdiff_t offset; |
5eb05ea3 KH |
4330 | |
4331 | charset_map_loaded = 0; | |
4332 | nbytes = encode_designation_at_bol (coding, charbuf, charbuf_end, | |
4333 | desig_buf); | |
4334 | if (charset_map_loaded | |
c1892f11 | 4335 | && (offset = coding_change_destination (coding))) |
5eb05ea3 KH |
4336 | { |
4337 | dst += offset; | |
4338 | dst_end += offset; | |
4339 | } | |
4340 | memcpy (dst, desig_buf, nbytes); | |
4341 | dst += nbytes; | |
df7492f9 | 4342 | /* We are sure that designation sequences are all ASCII bytes. */ |
5eb05ea3 KH |
4343 | produced_chars += nbytes; |
4344 | bol_designation = 0; | |
4345 | ASSURE_DESTINATION (safe_room); | |
e0e989f6 KH |
4346 | } |
4347 | ||
df7492f9 | 4348 | c = *charbuf++; |
ec6d2bb8 | 4349 | |
ff0dacd7 KH |
4350 | if (c < 0) |
4351 | { | |
4352 | /* Handle an annotation. */ | |
4353 | switch (*charbuf) | |
ec6d2bb8 | 4354 | { |
ff0dacd7 KH |
4355 | case CODING_ANNOTATE_COMPOSITION_MASK: |
4356 | /* Not yet implemented. */ | |
4357 | break; | |
4358 | case CODING_ANNOTATE_CHARSET_MASK: | |
cf7dfdf5 | 4359 | preferred_charset_id = charbuf[2]; |
ff0dacd7 KH |
4360 | if (preferred_charset_id >= 0 |
4361 | && NILP (Fmemq (make_number (preferred_charset_id), | |
4362 | charset_list))) | |
4363 | preferred_charset_id = -1; | |
4364 | break; | |
4365 | default: | |
1088b922 | 4366 | emacs_abort (); |
4ed46869 | 4367 | } |
ff0dacd7 KH |
4368 | charbuf += -c - 1; |
4369 | continue; | |
4ed46869 | 4370 | } |
ec6d2bb8 | 4371 | |
b73bfc1c KH |
4372 | /* Now encode the character C. */ |
4373 | if (c < 0x20 || c == 0x7F) | |
4374 | { | |
df7492f9 KH |
4375 | if (c == '\n' |
4376 | || (c == '\r' && EQ (eol_type, Qmac))) | |
19a8d9e0 | 4377 | { |
df7492f9 KH |
4378 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL) |
4379 | ENCODE_RESET_PLANE_AND_REGISTER (); | |
4380 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_INIT_AT_BOL) | |
b73bfc1c | 4381 | { |
df7492f9 KH |
4382 | int i; |
4383 | ||
4384 | for (i = 0; i < 4; i++) | |
4385 | CODING_ISO_DESIGNATION (coding, i) | |
4386 | = CODING_ISO_INITIAL (coding, i); | |
b73bfc1c | 4387 | } |
f10fe38f PE |
4388 | bol_designation = ((CODING_ISO_FLAGS (coding) |
4389 | & CODING_ISO_FLAG_DESIGNATE_AT_BOL) | |
4390 | != 0); | |
19a8d9e0 | 4391 | } |
df7492f9 KH |
4392 | else if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_CNTL) |
4393 | ENCODE_RESET_PLANE_AND_REGISTER (); | |
4394 | EMIT_ONE_ASCII_BYTE (c); | |
4ed46869 | 4395 | } |
df7492f9 | 4396 | else if (ASCII_CHAR_P (c)) |
88993dfd | 4397 | { |
df7492f9 KH |
4398 | if (ascii_compatible) |
4399 | EMIT_ONE_ASCII_BYTE (c); | |
93dec019 | 4400 | else |
19a8d9e0 | 4401 | { |
bf16eb23 KH |
4402 | struct charset *charset = CHARSET_FROM_ID (charset_ascii); |
4403 | ENCODE_ISO_CHARACTER (charset, c); | |
19a8d9e0 | 4404 | } |
4ed46869 | 4405 | } |
16eafb5d | 4406 | else if (CHAR_BYTE8_P (c)) |
88993dfd | 4407 | { |
16eafb5d KH |
4408 | c = CHAR_TO_BYTE8 (c); |
4409 | EMIT_ONE_BYTE (c); | |
88993dfd | 4410 | } |
b73bfc1c | 4411 | else |
df7492f9 | 4412 | { |
ff0dacd7 | 4413 | struct charset *charset; |
b73bfc1c | 4414 | |
ff0dacd7 KH |
4415 | if (preferred_charset_id >= 0) |
4416 | { | |
f10fe38f | 4417 | bool result; |
5eb05ea3 | 4418 | |
ff0dacd7 | 4419 | charset = CHARSET_FROM_ID (preferred_charset_id); |
5eb05ea3 KH |
4420 | CODING_CHAR_CHARSET_P (coding, dst, dst_end, c, charset, result); |
4421 | if (! result) | |
4422 | CODING_CHAR_CHARSET (coding, dst, dst_end, c, charset_list, | |
4423 | NULL, charset); | |
ff0dacd7 KH |
4424 | } |
4425 | else | |
5eb05ea3 KH |
4426 | CODING_CHAR_CHARSET (coding, dst, dst_end, c, charset_list, |
4427 | NULL, charset); | |
df7492f9 KH |
4428 | if (!charset) |
4429 | { | |
41cbe562 KH |
4430 | if (coding->mode & CODING_MODE_SAFE_ENCODING) |
4431 | { | |
4432 | c = CODING_INHIBIT_CHARACTER_SUBSTITUTION; | |
4433 | charset = CHARSET_FROM_ID (charset_ascii); | |
4434 | } | |
4435 | else | |
4436 | { | |
4437 | c = coding->default_char; | |
5eb05ea3 KH |
4438 | CODING_CHAR_CHARSET (coding, dst, dst_end, c, |
4439 | charset_list, NULL, charset); | |
41cbe562 | 4440 | } |
df7492f9 KH |
4441 | } |
4442 | ENCODE_ISO_CHARACTER (charset, c); | |
4443 | } | |
84fbb8a0 | 4444 | } |
b73bfc1c | 4445 | |
df7492f9 KH |
4446 | if (coding->mode & CODING_MODE_LAST_BLOCK |
4447 | && CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL) | |
4448 | { | |
4449 | ASSURE_DESTINATION (safe_room); | |
4450 | ENCODE_RESET_PLANE_AND_REGISTER (); | |
4451 | } | |
065e3595 | 4452 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
4453 | CODING_ISO_BOL (coding) = bol_designation; |
4454 | coding->produced_char += produced_chars; | |
4455 | coding->produced = dst - coding->destination; | |
4456 | return 0; | |
4ed46869 KH |
4457 | } |
4458 | ||
4459 | \f | |
df7492f9 | 4460 | /*** 8,9. SJIS and BIG5 handlers ***/ |
4ed46869 | 4461 | |
df7492f9 | 4462 | /* Although SJIS and BIG5 are not ISO's coding system, they are used |
4ed46869 KH |
4463 | quite widely. So, for the moment, Emacs supports them in the bare |
4464 | C code. But, in the future, they may be supported only by CCL. */ | |
4465 | ||
4466 | /* SJIS is a coding system encoding three character sets: ASCII, right | |
4467 | half of JISX0201-Kana, and JISX0208. An ASCII character is encoded | |
4468 | as is. A character of charset katakana-jisx0201 is encoded by | |
4469 | "position-code + 0x80". A character of charset japanese-jisx0208 | |
4470 | is encoded in 2-byte but two position-codes are divided and shifted | |
df7492f9 | 4471 | so that it fit in the range below. |
4ed46869 KH |
4472 | |
4473 | --- CODE RANGE of SJIS --- | |
4474 | (character set) (range) | |
4475 | ASCII 0x00 .. 0x7F | |
df7492f9 | 4476 | KATAKANA-JISX0201 0xA0 .. 0xDF |
c28a9453 | 4477 | JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF |
d14d03ac | 4478 | (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC |
4ed46869 KH |
4479 | ------------------------------- |
4480 | ||
4481 | */ | |
4482 | ||
4483 | /* BIG5 is a coding system encoding two character sets: ASCII and | |
4484 | Big5. An ASCII character is encoded as is. Big5 is a two-byte | |
df7492f9 | 4485 | character set and is encoded in two-byte. |
4ed46869 KH |
4486 | |
4487 | --- CODE RANGE of BIG5 --- | |
4488 | (character set) (range) | |
4489 | ASCII 0x00 .. 0x7F | |
4490 | Big5 (1st byte) 0xA1 .. 0xFE | |
4491 | (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE | |
4492 | -------------------------- | |
4493 | ||
df7492f9 | 4494 | */ |
4ed46869 KH |
4495 | |
4496 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
f10fe38f | 4497 | Return true if a text is encoded in SJIS. */ |
4ed46869 | 4498 | |
f10fe38f | 4499 | static bool |
cf84bb53 JB |
4500 | detect_coding_sjis (struct coding_system *coding, |
4501 | struct coding_detection_info *detect_info) | |
4ed46869 | 4502 | { |
065e3595 | 4503 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 4504 | const unsigned char *src_end = coding->source + coding->src_bytes; |
f10fe38f | 4505 | bool multibytep = coding->src_multibyte; |
d311d28c | 4506 | ptrdiff_t consumed_chars = 0; |
df7492f9 | 4507 | int found = 0; |
b73bfc1c | 4508 | int c; |
f07190ca KH |
4509 | Lisp_Object attrs, charset_list; |
4510 | int max_first_byte_of_2_byte_code; | |
4511 | ||
4512 | CODING_GET_INFO (coding, attrs, charset_list); | |
4513 | max_first_byte_of_2_byte_code | |
4514 | = (XINT (Flength (charset_list)) > 3 ? 0xFC : 0xEF); | |
df7492f9 | 4515 | |
ff0dacd7 | 4516 | detect_info->checked |= CATEGORY_MASK_SJIS; |
df7492f9 KH |
4517 | /* A coding system of this category is always ASCII compatible. */ |
4518 | src += coding->head_ascii; | |
4ed46869 | 4519 | |
b73bfc1c | 4520 | while (1) |
4ed46869 | 4521 | { |
065e3595 | 4522 | src_base = src; |
df7492f9 | 4523 | ONE_MORE_BYTE (c); |
682169fe KH |
4524 | if (c < 0x80) |
4525 | continue; | |
f07190ca KH |
4526 | if ((c >= 0x81 && c <= 0x9F) |
4527 | || (c >= 0xE0 && c <= max_first_byte_of_2_byte_code)) | |
4ed46869 | 4528 | { |
df7492f9 | 4529 | ONE_MORE_BYTE (c); |
682169fe | 4530 | if (c < 0x40 || c == 0x7F || c > 0xFC) |
df7492f9 | 4531 | break; |
ff0dacd7 | 4532 | found = CATEGORY_MASK_SJIS; |
4ed46869 | 4533 | } |
df7492f9 | 4534 | else if (c >= 0xA0 && c < 0xE0) |
ff0dacd7 | 4535 | found = CATEGORY_MASK_SJIS; |
df7492f9 KH |
4536 | else |
4537 | break; | |
4ed46869 | 4538 | } |
ff0dacd7 | 4539 | detect_info->rejected |= CATEGORY_MASK_SJIS; |
df7492f9 KH |
4540 | return 0; |
4541 | ||
4542 | no_more_source: | |
065e3595 | 4543 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) |
89528eb3 | 4544 | { |
ff0dacd7 | 4545 | detect_info->rejected |= CATEGORY_MASK_SJIS; |
89528eb3 | 4546 | return 0; |
4ed46869 | 4547 | } |
ff0dacd7 KH |
4548 | detect_info->found |= found; |
4549 | return 1; | |
4ed46869 KH |
4550 | } |
4551 | ||
4552 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
f10fe38f | 4553 | Return true if a text is encoded in BIG5. */ |
4ed46869 | 4554 | |
f10fe38f | 4555 | static bool |
cf84bb53 JB |
4556 | detect_coding_big5 (struct coding_system *coding, |
4557 | struct coding_detection_info *detect_info) | |
4ed46869 | 4558 | { |
065e3595 | 4559 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 4560 | const unsigned char *src_end = coding->source + coding->src_bytes; |
f10fe38f | 4561 | bool multibytep = coding->src_multibyte; |
d311d28c | 4562 | ptrdiff_t consumed_chars = 0; |
df7492f9 | 4563 | int found = 0; |
b73bfc1c | 4564 | int c; |
fa42c37f | 4565 | |
ff0dacd7 | 4566 | detect_info->checked |= CATEGORY_MASK_BIG5; |
df7492f9 KH |
4567 | /* A coding system of this category is always ASCII compatible. */ |
4568 | src += coding->head_ascii; | |
fa42c37f | 4569 | |
b73bfc1c | 4570 | while (1) |
fa42c37f | 4571 | { |
065e3595 | 4572 | src_base = src; |
df7492f9 KH |
4573 | ONE_MORE_BYTE (c); |
4574 | if (c < 0x80) | |
fa42c37f | 4575 | continue; |
df7492f9 | 4576 | if (c >= 0xA1) |
fa42c37f | 4577 | { |
df7492f9 KH |
4578 | ONE_MORE_BYTE (c); |
4579 | if (c < 0x40 || (c >= 0x7F && c <= 0xA0)) | |
fa42c37f | 4580 | return 0; |
ff0dacd7 | 4581 | found = CATEGORY_MASK_BIG5; |
fa42c37f | 4582 | } |
df7492f9 KH |
4583 | else |
4584 | break; | |
fa42c37f | 4585 | } |
ff0dacd7 | 4586 | detect_info->rejected |= CATEGORY_MASK_BIG5; |
fa42c37f | 4587 | return 0; |
fa42c37f | 4588 | |
df7492f9 | 4589 | no_more_source: |
065e3595 | 4590 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) |
89528eb3 | 4591 | { |
ff0dacd7 | 4592 | detect_info->rejected |= CATEGORY_MASK_BIG5; |
89528eb3 KH |
4593 | return 0; |
4594 | } | |
ff0dacd7 KH |
4595 | detect_info->found |= found; |
4596 | return 1; | |
fa42c37f KH |
4597 | } |
4598 | ||
f10fe38f | 4599 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ |
fa42c37f | 4600 | |
b73bfc1c | 4601 | static void |
971de7fb | 4602 | decode_coding_sjis (struct coding_system *coding) |
4ed46869 | 4603 | { |
8f924df7 KH |
4604 | const unsigned char *src = coding->source + coding->consumed; |
4605 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
4606 | const unsigned char *src_base; | |
69a80ea3 | 4607 | int *charbuf = coding->charbuf + coding->charbuf_used; |
ad1746f5 | 4608 | /* We may produce one charset annotation in one loop and one more at |
df80c7f0 | 4609 | the end. */ |
69a80ea3 | 4610 | int *charbuf_end |
df80c7f0 | 4611 | = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 2); |
d311d28c | 4612 | ptrdiff_t consumed_chars = 0, consumed_chars_base; |
f10fe38f | 4613 | bool multibytep = coding->src_multibyte; |
df7492f9 | 4614 | struct charset *charset_roman, *charset_kanji, *charset_kana; |
57a47f8a | 4615 | struct charset *charset_kanji2; |
24a73b0a | 4616 | Lisp_Object attrs, charset_list, val; |
d311d28c PE |
4617 | ptrdiff_t char_offset = coding->produced_char; |
4618 | ptrdiff_t last_offset = char_offset; | |
ff0dacd7 | 4619 | int last_id = charset_ascii; |
f10fe38f PE |
4620 | bool eol_dos |
4621 | = !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); | |
119852e7 | 4622 | int byte_after_cr = -1; |
a5d301df | 4623 | |
24a73b0a | 4624 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4625 | |
4626 | val = charset_list; | |
4627 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
89528eb3 | 4628 | charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
57a47f8a KH |
4629 | charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
4630 | charset_kanji2 = NILP (val) ? NULL : CHARSET_FROM_ID (XINT (XCAR (val))); | |
fa42c37f | 4631 | |
b73bfc1c | 4632 | while (1) |
4ed46869 | 4633 | { |
df7492f9 | 4634 | int c, c1; |
24a73b0a | 4635 | struct charset *charset; |
fa42c37f | 4636 | |
b73bfc1c | 4637 | src_base = src; |
df7492f9 | 4638 | consumed_chars_base = consumed_chars; |
fa42c37f | 4639 | |
df7492f9 | 4640 | if (charbuf >= charbuf_end) |
b71f6f73 KH |
4641 | { |
4642 | if (byte_after_cr >= 0) | |
4643 | src_base--; | |
4644 | break; | |
4645 | } | |
df7492f9 | 4646 | |
119852e7 KH |
4647 | if (byte_after_cr >= 0) |
4648 | c = byte_after_cr, byte_after_cr = -1; | |
4649 | else | |
4650 | ONE_MORE_BYTE (c); | |
065e3595 KH |
4651 | if (c < 0) |
4652 | goto invalid_code; | |
24a73b0a | 4653 | if (c < 0x80) |
119852e7 | 4654 | { |
2735d060 | 4655 | if (eol_dos && c == '\r') |
119852e7 KH |
4656 | ONE_MORE_BYTE (byte_after_cr); |
4657 | charset = charset_roman; | |
4658 | } | |
57a47f8a | 4659 | else if (c == 0x80 || c == 0xA0) |
8e921c4b | 4660 | goto invalid_code; |
57a47f8a KH |
4661 | else if (c >= 0xA1 && c <= 0xDF) |
4662 | { | |
4663 | /* SJIS -> JISX0201-Kana */ | |
4664 | c &= 0x7F; | |
4665 | charset = charset_kana; | |
4666 | } | |
4667 | else if (c <= 0xEF) | |
df7492f9 | 4668 | { |
57a47f8a KH |
4669 | /* SJIS -> JISX0208 */ |
4670 | ONE_MORE_BYTE (c1); | |
4671 | if (c1 < 0x40 || c1 == 0x7F || c1 > 0xFC) | |
24a73b0a | 4672 | goto invalid_code; |
57a47f8a KH |
4673 | c = (c << 8) | c1; |
4674 | SJIS_TO_JIS (c); | |
4675 | charset = charset_kanji; | |
4676 | } | |
4677 | else if (c <= 0xFC && charset_kanji2) | |
4678 | { | |
c6876370 | 4679 | /* SJIS -> JISX0213-2 */ |
57a47f8a KH |
4680 | ONE_MORE_BYTE (c1); |
4681 | if (c1 < 0x40 || c1 == 0x7F || c1 > 0xFC) | |
24a73b0a | 4682 | goto invalid_code; |
57a47f8a KH |
4683 | c = (c << 8) | c1; |
4684 | SJIS_TO_JIS2 (c); | |
4685 | charset = charset_kanji2; | |
df7492f9 | 4686 | } |
57a47f8a KH |
4687 | else |
4688 | goto invalid_code; | |
24a73b0a KH |
4689 | if (charset->id != charset_ascii |
4690 | && last_id != charset->id) | |
4691 | { | |
4692 | if (last_id != charset_ascii) | |
69a80ea3 | 4693 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
24a73b0a KH |
4694 | last_id = charset->id; |
4695 | last_offset = char_offset; | |
4696 | } | |
4697 | CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c); | |
df7492f9 | 4698 | *charbuf++ = c; |
ff0dacd7 | 4699 | char_offset++; |
df7492f9 | 4700 | continue; |
b73bfc1c | 4701 | |
df7492f9 KH |
4702 | invalid_code: |
4703 | src = src_base; | |
4704 | consumed_chars = consumed_chars_base; | |
4705 | ONE_MORE_BYTE (c); | |
065e3595 | 4706 | *charbuf++ = c < 0 ? -c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 4707 | char_offset++; |
df7492f9 KH |
4708 | coding->errors++; |
4709 | } | |
fa42c37f | 4710 | |
df7492f9 | 4711 | no_more_source: |
ff0dacd7 | 4712 | if (last_id != charset_ascii) |
69a80ea3 | 4713 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
4714 | coding->consumed_char += consumed_chars_base; |
4715 | coding->consumed = src_base - coding->source; | |
4716 | coding->charbuf_used = charbuf - coding->charbuf; | |
fa42c37f KH |
4717 | } |
4718 | ||
b73bfc1c | 4719 | static void |
971de7fb | 4720 | decode_coding_big5 (struct coding_system *coding) |
4ed46869 | 4721 | { |
8f924df7 KH |
4722 | const unsigned char *src = coding->source + coding->consumed; |
4723 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
4724 | const unsigned char *src_base; | |
69a80ea3 | 4725 | int *charbuf = coding->charbuf + coding->charbuf_used; |
ad1746f5 | 4726 | /* We may produce one charset annotation in one loop and one more at |
df80c7f0 | 4727 | the end. */ |
69a80ea3 | 4728 | int *charbuf_end |
df80c7f0 | 4729 | = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 2); |
d311d28c | 4730 | ptrdiff_t consumed_chars = 0, consumed_chars_base; |
f10fe38f | 4731 | bool multibytep = coding->src_multibyte; |
df7492f9 | 4732 | struct charset *charset_roman, *charset_big5; |
24a73b0a | 4733 | Lisp_Object attrs, charset_list, val; |
d311d28c PE |
4734 | ptrdiff_t char_offset = coding->produced_char; |
4735 | ptrdiff_t last_offset = char_offset; | |
ff0dacd7 | 4736 | int last_id = charset_ascii; |
f10fe38f PE |
4737 | bool eol_dos |
4738 | = !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); | |
119852e7 | 4739 | int byte_after_cr = -1; |
df7492f9 | 4740 | |
24a73b0a | 4741 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4742 | val = charset_list; |
4743 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
4744 | charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val))); | |
4ed46869 | 4745 | |
b73bfc1c | 4746 | while (1) |
4ed46869 | 4747 | { |
df7492f9 | 4748 | int c, c1; |
24a73b0a | 4749 | struct charset *charset; |
b73bfc1c KH |
4750 | |
4751 | src_base = src; | |
df7492f9 KH |
4752 | consumed_chars_base = consumed_chars; |
4753 | ||
4754 | if (charbuf >= charbuf_end) | |
b71f6f73 KH |
4755 | { |
4756 | if (byte_after_cr >= 0) | |
4757 | src_base--; | |
4758 | break; | |
4759 | } | |
df7492f9 | 4760 | |
119852e7 | 4761 | if (byte_after_cr >= 0) |
14daee73 | 4762 | c = byte_after_cr, byte_after_cr = -1; |
119852e7 KH |
4763 | else |
4764 | ONE_MORE_BYTE (c); | |
b73bfc1c | 4765 | |
065e3595 KH |
4766 | if (c < 0) |
4767 | goto invalid_code; | |
24a73b0a | 4768 | if (c < 0x80) |
119852e7 | 4769 | { |
2735d060 | 4770 | if (eol_dos && c == '\r') |
119852e7 KH |
4771 | ONE_MORE_BYTE (byte_after_cr); |
4772 | charset = charset_roman; | |
4773 | } | |
24a73b0a | 4774 | else |
4ed46869 | 4775 | { |
24a73b0a KH |
4776 | /* BIG5 -> Big5 */ |
4777 | if (c < 0xA1 || c > 0xFE) | |
4778 | goto invalid_code; | |
4779 | ONE_MORE_BYTE (c1); | |
4780 | if (c1 < 0x40 || (c1 > 0x7E && c1 < 0xA1) || c1 > 0xFE) | |
4781 | goto invalid_code; | |
4782 | c = c << 8 | c1; | |
4783 | charset = charset_big5; | |
4ed46869 | 4784 | } |
24a73b0a KH |
4785 | if (charset->id != charset_ascii |
4786 | && last_id != charset->id) | |
df7492f9 | 4787 | { |
24a73b0a | 4788 | if (last_id != charset_ascii) |
69a80ea3 | 4789 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
24a73b0a KH |
4790 | last_id = charset->id; |
4791 | last_offset = char_offset; | |
4ed46869 | 4792 | } |
24a73b0a | 4793 | CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c); |
df7492f9 | 4794 | *charbuf++ = c; |
ff0dacd7 | 4795 | char_offset++; |
fb88bf2d KH |
4796 | continue; |
4797 | ||
df7492f9 | 4798 | invalid_code: |
4ed46869 | 4799 | src = src_base; |
df7492f9 KH |
4800 | consumed_chars = consumed_chars_base; |
4801 | ONE_MORE_BYTE (c); | |
065e3595 | 4802 | *charbuf++ = c < 0 ? -c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 4803 | char_offset++; |
df7492f9 | 4804 | coding->errors++; |
fb88bf2d | 4805 | } |
d46c5b12 | 4806 | |
df7492f9 | 4807 | no_more_source: |
ff0dacd7 | 4808 | if (last_id != charset_ascii) |
69a80ea3 | 4809 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
4810 | coding->consumed_char += consumed_chars_base; |
4811 | coding->consumed = src_base - coding->source; | |
4812 | coding->charbuf_used = charbuf - coding->charbuf; | |
4ed46869 KH |
4813 | } |
4814 | ||
4815 | /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". | |
b73bfc1c KH |
4816 | This function can encode charsets `ascii', `katakana-jisx0201', |
4817 | `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We | |
4818 | are sure that all these charsets are registered as official charset | |
4ed46869 | 4819 | (i.e. do not have extended leading-codes). Characters of other |
f10fe38f | 4820 | charsets are produced without any encoding. */ |
4ed46869 | 4821 | |
f10fe38f | 4822 | static bool |
971de7fb | 4823 | encode_coding_sjis (struct coding_system *coding) |
4ed46869 | 4824 | { |
f10fe38f | 4825 | bool multibytep = coding->dst_multibyte; |
df7492f9 KH |
4826 | int *charbuf = coding->charbuf; |
4827 | int *charbuf_end = charbuf + coding->charbuf_used; | |
4828 | unsigned char *dst = coding->destination + coding->produced; | |
4829 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
4830 | int safe_room = 4; | |
d311d28c | 4831 | ptrdiff_t produced_chars = 0; |
24a73b0a | 4832 | Lisp_Object attrs, charset_list, val; |
f10fe38f | 4833 | bool ascii_compatible; |
66ebf983 | 4834 | struct charset *charset_kanji, *charset_kana; |
57a47f8a | 4835 | struct charset *charset_kanji2; |
df7492f9 | 4836 | int c; |
a5d301df | 4837 | |
24a73b0a | 4838 | CODING_GET_INFO (coding, attrs, charset_list); |
66ebf983 | 4839 | val = XCDR (charset_list); |
df7492f9 | 4840 | charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
57a47f8a KH |
4841 | charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
4842 | charset_kanji2 = NILP (val) ? NULL : CHARSET_FROM_ID (XINT (XCAR (val))); | |
4ed46869 | 4843 | |
df7492f9 | 4844 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); |
93dec019 | 4845 | |
df7492f9 KH |
4846 | while (charbuf < charbuf_end) |
4847 | { | |
4848 | ASSURE_DESTINATION (safe_room); | |
4849 | c = *charbuf++; | |
b73bfc1c | 4850 | /* Now encode the character C. */ |
df7492f9 KH |
4851 | if (ASCII_CHAR_P (c) && ascii_compatible) |
4852 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d KH |
4853 | else if (CHAR_BYTE8_P (c)) |
4854 | { | |
4855 | c = CHAR_TO_BYTE8 (c); | |
4856 | EMIT_ONE_BYTE (c); | |
4857 | } | |
df7492f9 | 4858 | else |
b73bfc1c | 4859 | { |
df7492f9 | 4860 | unsigned code; |
5eb05ea3 KH |
4861 | struct charset *charset; |
4862 | CODING_CHAR_CHARSET (coding, dst, dst_end, c, charset_list, | |
4863 | &code, charset); | |
df7492f9 KH |
4864 | |
4865 | if (!charset) | |
4ed46869 | 4866 | { |
41cbe562 | 4867 | if (coding->mode & CODING_MODE_SAFE_ENCODING) |
b73bfc1c | 4868 | { |
41cbe562 KH |
4869 | code = CODING_INHIBIT_CHARACTER_SUBSTITUTION; |
4870 | charset = CHARSET_FROM_ID (charset_ascii); | |
b73bfc1c | 4871 | } |
41cbe562 | 4872 | else |
b73bfc1c | 4873 | { |
41cbe562 | 4874 | c = coding->default_char; |
5eb05ea3 KH |
4875 | CODING_CHAR_CHARSET (coding, dst, dst_end, c, |
4876 | charset_list, &code, charset); | |
b73bfc1c | 4877 | } |
b73bfc1c | 4878 | } |
df7492f9 | 4879 | if (code == CHARSET_INVALID_CODE (charset)) |
1088b922 | 4880 | emacs_abort (); |
df7492f9 KH |
4881 | if (charset == charset_kanji) |
4882 | { | |
4883 | int c1, c2; | |
4884 | JIS_TO_SJIS (code); | |
4885 | c1 = code >> 8, c2 = code & 0xFF; | |
4886 | EMIT_TWO_BYTES (c1, c2); | |
4887 | } | |
4888 | else if (charset == charset_kana) | |
4889 | EMIT_ONE_BYTE (code | 0x80); | |
57a47f8a KH |
4890 | else if (charset_kanji2 && charset == charset_kanji2) |
4891 | { | |
4892 | int c1, c2; | |
4893 | ||
4894 | c1 = code >> 8; | |
f07190ca KH |
4895 | if (c1 == 0x21 || (c1 >= 0x23 && c1 <= 0x25) |
4896 | || c1 == 0x28 | |
57a47f8a KH |
4897 | || (c1 >= 0x2C && c1 <= 0x2F) || c1 >= 0x6E) |
4898 | { | |
4899 | JIS_TO_SJIS2 (code); | |
4900 | c1 = code >> 8, c2 = code & 0xFF; | |
4901 | EMIT_TWO_BYTES (c1, c2); | |
4902 | } | |
4903 | else | |
4904 | EMIT_ONE_ASCII_BYTE (code & 0x7F); | |
4905 | } | |
df7492f9 KH |
4906 | else |
4907 | EMIT_ONE_ASCII_BYTE (code & 0x7F); | |
4908 | } | |
4909 | } | |
065e3595 | 4910 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
4911 | coding->produced_char += produced_chars; |
4912 | coding->produced = dst - coding->destination; | |
4913 | return 0; | |
4914 | } | |
4915 | ||
f10fe38f | 4916 | static bool |
971de7fb | 4917 | encode_coding_big5 (struct coding_system *coding) |
df7492f9 | 4918 | { |
f10fe38f | 4919 | bool multibytep = coding->dst_multibyte; |
df7492f9 KH |
4920 | int *charbuf = coding->charbuf; |
4921 | int *charbuf_end = charbuf + coding->charbuf_used; | |
4922 | unsigned char *dst = coding->destination + coding->produced; | |
4923 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
4924 | int safe_room = 4; | |
d311d28c | 4925 | ptrdiff_t produced_chars = 0; |
24a73b0a | 4926 | Lisp_Object attrs, charset_list, val; |
f10fe38f | 4927 | bool ascii_compatible; |
66ebf983 | 4928 | struct charset *charset_big5; |
df7492f9 KH |
4929 | int c; |
4930 | ||
24a73b0a | 4931 | CODING_GET_INFO (coding, attrs, charset_list); |
66ebf983 | 4932 | val = XCDR (charset_list); |
df7492f9 KH |
4933 | charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val))); |
4934 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); | |
4935 | ||
4936 | while (charbuf < charbuf_end) | |
4937 | { | |
4938 | ASSURE_DESTINATION (safe_room); | |
4939 | c = *charbuf++; | |
4940 | /* Now encode the character C. */ | |
4941 | if (ASCII_CHAR_P (c) && ascii_compatible) | |
4942 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d KH |
4943 | else if (CHAR_BYTE8_P (c)) |
4944 | { | |
4945 | c = CHAR_TO_BYTE8 (c); | |
4946 | EMIT_ONE_BYTE (c); | |
b73bfc1c KH |
4947 | } |
4948 | else | |
4949 | { | |
df7492f9 | 4950 | unsigned code; |
5eb05ea3 KH |
4951 | struct charset *charset; |
4952 | CODING_CHAR_CHARSET (coding, dst, dst_end, c, charset_list, | |
4953 | &code, charset); | |
df7492f9 KH |
4954 | |
4955 | if (! charset) | |
b73bfc1c | 4956 | { |
41cbe562 | 4957 | if (coding->mode & CODING_MODE_SAFE_ENCODING) |
b73bfc1c | 4958 | { |
41cbe562 KH |
4959 | code = CODING_INHIBIT_CHARACTER_SUBSTITUTION; |
4960 | charset = CHARSET_FROM_ID (charset_ascii); | |
b73bfc1c | 4961 | } |
41cbe562 | 4962 | else |
0eecad43 | 4963 | { |
41cbe562 | 4964 | c = coding->default_char; |
5eb05ea3 KH |
4965 | CODING_CHAR_CHARSET (coding, dst, dst_end, c, |
4966 | charset_list, &code, charset); | |
0eecad43 | 4967 | } |
4ed46869 | 4968 | } |
df7492f9 | 4969 | if (code == CHARSET_INVALID_CODE (charset)) |
1088b922 | 4970 | emacs_abort (); |
df7492f9 | 4971 | if (charset == charset_big5) |
b73bfc1c | 4972 | { |
df7492f9 KH |
4973 | int c1, c2; |
4974 | ||
4975 | c1 = code >> 8, c2 = code & 0xFF; | |
4976 | EMIT_TWO_BYTES (c1, c2); | |
b73bfc1c | 4977 | } |
df7492f9 KH |
4978 | else |
4979 | EMIT_ONE_ASCII_BYTE (code & 0x7F); | |
4ed46869 | 4980 | } |
4ed46869 | 4981 | } |
065e3595 | 4982 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
4983 | coding->produced_char += produced_chars; |
4984 | coding->produced = dst - coding->destination; | |
4985 | return 0; | |
4ed46869 KH |
4986 | } |
4987 | ||
4988 | \f | |
df7492f9 | 4989 | /*** 10. CCL handlers ***/ |
1397dc18 KH |
4990 | |
4991 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
f10fe38f PE |
4992 | Return true if a text is encoded in a coding system of which |
4993 | encoder/decoder are written in CCL program. */ | |
1397dc18 | 4994 | |
f10fe38f | 4995 | static bool |
cf84bb53 JB |
4996 | detect_coding_ccl (struct coding_system *coding, |
4997 | struct coding_detection_info *detect_info) | |
1397dc18 | 4998 | { |
065e3595 | 4999 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 5000 | const unsigned char *src_end = coding->source + coding->src_bytes; |
f10fe38f | 5001 | bool multibytep = coding->src_multibyte; |
d311d28c | 5002 | ptrdiff_t consumed_chars = 0; |
df7492f9 | 5003 | int found = 0; |
0e219d54 | 5004 | unsigned char *valids; |
d311d28c | 5005 | ptrdiff_t head_ascii = coding->head_ascii; |
df7492f9 KH |
5006 | Lisp_Object attrs; |
5007 | ||
ff0dacd7 KH |
5008 | detect_info->checked |= CATEGORY_MASK_CCL; |
5009 | ||
df7492f9 | 5010 | coding = &coding_categories[coding_category_ccl]; |
0e219d54 | 5011 | valids = CODING_CCL_VALIDS (coding); |
df7492f9 KH |
5012 | attrs = CODING_ID_ATTRS (coding->id); |
5013 | if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
5014 | src += head_ascii; | |
1397dc18 | 5015 | |
b73bfc1c | 5016 | while (1) |
1397dc18 | 5017 | { |
df7492f9 | 5018 | int c; |
065e3595 KH |
5019 | |
5020 | src_base = src; | |
df7492f9 | 5021 | ONE_MORE_BYTE (c); |
065e3595 | 5022 | if (c < 0 || ! valids[c]) |
df7492f9 | 5023 | break; |
ff0dacd7 KH |
5024 | if ((valids[c] > 1)) |
5025 | found = CATEGORY_MASK_CCL; | |
df7492f9 | 5026 | } |
ff0dacd7 | 5027 | detect_info->rejected |= CATEGORY_MASK_CCL; |
df7492f9 KH |
5028 | return 0; |
5029 | ||
5030 | no_more_source: | |
ff0dacd7 KH |
5031 | detect_info->found |= found; |
5032 | return 1; | |
df7492f9 KH |
5033 | } |
5034 | ||
5035 | static void | |
971de7fb | 5036 | decode_coding_ccl (struct coding_system *coding) |
df7492f9 | 5037 | { |
7c78e542 | 5038 | const unsigned char *src = coding->source + coding->consumed; |
8f924df7 | 5039 | const unsigned char *src_end = coding->source + coding->src_bytes; |
69a80ea3 KH |
5040 | int *charbuf = coding->charbuf + coding->charbuf_used; |
5041 | int *charbuf_end = coding->charbuf + coding->charbuf_size; | |
d311d28c | 5042 | ptrdiff_t consumed_chars = 0; |
f10fe38f | 5043 | bool multibytep = coding->src_multibyte; |
d0396581 | 5044 | struct ccl_program *ccl = &coding->spec.ccl->ccl; |
df7492f9 | 5045 | int source_charbuf[1024]; |
fbdc1721 | 5046 | int source_byteidx[1025]; |
24a73b0a | 5047 | Lisp_Object attrs, charset_list; |
df7492f9 | 5048 | |
24a73b0a | 5049 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 | 5050 | |
d0396581 | 5051 | while (1) |
df7492f9 | 5052 | { |
7c78e542 | 5053 | const unsigned char *p = src; |
95402d5f | 5054 | ptrdiff_t offset; |
df7492f9 KH |
5055 | int i = 0; |
5056 | ||
5057 | if (multibytep) | |
fbdc1721 KH |
5058 | { |
5059 | while (i < 1024 && p < src_end) | |
5060 | { | |
5061 | source_byteidx[i] = p - src; | |
5062 | source_charbuf[i++] = STRING_CHAR_ADVANCE (p); | |
5063 | } | |
5064 | source_byteidx[i] = p - src; | |
5065 | } | |
df7492f9 KH |
5066 | else |
5067 | while (i < 1024 && p < src_end) | |
5068 | source_charbuf[i++] = *p++; | |
8f924df7 | 5069 | |
df7492f9 | 5070 | if (p == src_end && coding->mode & CODING_MODE_LAST_BLOCK) |
d0396581 | 5071 | ccl->last_block = 1; |
95402d5f KH |
5072 | /* As ccl_driver calls DECODE_CHAR, buffer may be relocated. */ |
5073 | charset_map_loaded = 0; | |
d0396581 KH |
5074 | ccl_driver (ccl, source_charbuf, charbuf, i, charbuf_end - charbuf, |
5075 | charset_list); | |
95402d5f KH |
5076 | if (charset_map_loaded |
5077 | && (offset = coding_change_source (coding))) | |
5078 | { | |
5079 | p += offset; | |
5080 | src += offset; | |
5081 | src_end += offset; | |
5082 | } | |
d0396581 | 5083 | charbuf += ccl->produced; |
fbdc1721 | 5084 | if (multibytep) |
d0396581 | 5085 | src += source_byteidx[ccl->consumed]; |
df7492f9 | 5086 | else |
d0396581 KH |
5087 | src += ccl->consumed; |
5088 | consumed_chars += ccl->consumed; | |
5089 | if (p == src_end || ccl->status != CCL_STAT_SUSPEND_BY_SRC) | |
df7492f9 KH |
5090 | break; |
5091 | } | |
5092 | ||
d0396581 | 5093 | switch (ccl->status) |
df7492f9 KH |
5094 | { |
5095 | case CCL_STAT_SUSPEND_BY_SRC: | |
065e3595 | 5096 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC); |
df7492f9 KH |
5097 | break; |
5098 | case CCL_STAT_SUSPEND_BY_DST: | |
d0396581 | 5099 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_DST); |
df7492f9 KH |
5100 | break; |
5101 | case CCL_STAT_QUIT: | |
5102 | case CCL_STAT_INVALID_CMD: | |
065e3595 | 5103 | record_conversion_result (coding, CODING_RESULT_INTERRUPT); |
df7492f9 KH |
5104 | break; |
5105 | default: | |
065e3595 | 5106 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
5107 | break; |
5108 | } | |
5109 | coding->consumed_char += consumed_chars; | |
5110 | coding->consumed = src - coding->source; | |
5111 | coding->charbuf_used = charbuf - coding->charbuf; | |
5112 | } | |
5113 | ||
f10fe38f | 5114 | static bool |
971de7fb | 5115 | encode_coding_ccl (struct coding_system *coding) |
df7492f9 | 5116 | { |
fb608df3 | 5117 | struct ccl_program *ccl = &coding->spec.ccl->ccl; |
f10fe38f | 5118 | bool multibytep = coding->dst_multibyte; |
df7492f9 KH |
5119 | int *charbuf = coding->charbuf; |
5120 | int *charbuf_end = charbuf + coding->charbuf_used; | |
5121 | unsigned char *dst = coding->destination + coding->produced; | |
5122 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
df7492f9 | 5123 | int destination_charbuf[1024]; |
d311d28c | 5124 | ptrdiff_t produced_chars = 0; |
a53e2e89 | 5125 | int i; |
24a73b0a | 5126 | Lisp_Object attrs, charset_list; |
df7492f9 | 5127 | |
24a73b0a | 5128 | CODING_GET_INFO (coding, attrs, charset_list); |
fb608df3 KH |
5129 | if (coding->consumed_char == coding->src_chars |
5130 | && coding->mode & CODING_MODE_LAST_BLOCK) | |
5131 | ccl->last_block = 1; | |
df7492f9 | 5132 | |
76470ad1 | 5133 | do |
df7492f9 | 5134 | { |
95402d5f KH |
5135 | ptrdiff_t offset; |
5136 | ||
5137 | /* As ccl_driver calls DECODE_CHAR, buffer may be relocated. */ | |
5138 | charset_map_loaded = 0; | |
fb608df3 | 5139 | ccl_driver (ccl, charbuf, destination_charbuf, |
8cffd3e7 | 5140 | charbuf_end - charbuf, 1024, charset_list); |
95402d5f KH |
5141 | if (charset_map_loaded |
5142 | && (offset = coding_change_destination (coding))) | |
5143 | dst += offset; | |
df7492f9 | 5144 | if (multibytep) |
8cffd3e7 | 5145 | { |
fb608df3 KH |
5146 | ASSURE_DESTINATION (ccl->produced * 2); |
5147 | for (i = 0; i < ccl->produced; i++) | |
8cffd3e7 KH |
5148 | EMIT_ONE_BYTE (destination_charbuf[i] & 0xFF); |
5149 | } | |
df7492f9 KH |
5150 | else |
5151 | { | |
fb608df3 KH |
5152 | ASSURE_DESTINATION (ccl->produced); |
5153 | for (i = 0; i < ccl->produced; i++) | |
df7492f9 | 5154 | *dst++ = destination_charbuf[i] & 0xFF; |
fb608df3 | 5155 | produced_chars += ccl->produced; |
df7492f9 | 5156 | } |
fb608df3 KH |
5157 | charbuf += ccl->consumed; |
5158 | if (ccl->status == CCL_STAT_QUIT | |
5159 | || ccl->status == CCL_STAT_INVALID_CMD) | |
8cffd3e7 | 5160 | break; |
df7492f9 | 5161 | } |
76470ad1 | 5162 | while (charbuf < charbuf_end); |
df7492f9 | 5163 | |
fb608df3 | 5164 | switch (ccl->status) |
df7492f9 KH |
5165 | { |
5166 | case CCL_STAT_SUSPEND_BY_SRC: | |
065e3595 | 5167 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC); |
df7492f9 KH |
5168 | break; |
5169 | case CCL_STAT_SUSPEND_BY_DST: | |
065e3595 | 5170 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_DST); |
df7492f9 KH |
5171 | break; |
5172 | case CCL_STAT_QUIT: | |
5173 | case CCL_STAT_INVALID_CMD: | |
065e3595 | 5174 | record_conversion_result (coding, CODING_RESULT_INTERRUPT); |
df7492f9 KH |
5175 | break; |
5176 | default: | |
065e3595 | 5177 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 | 5178 | break; |
1397dc18 | 5179 | } |
df7492f9 KH |
5180 | |
5181 | coding->produced_char += produced_chars; | |
5182 | coding->produced = dst - coding->destination; | |
5183 | return 0; | |
1397dc18 KH |
5184 | } |
5185 | ||
5186 | \f | |
df7492f9 | 5187 | /*** 10, 11. no-conversion handlers ***/ |
4ed46869 | 5188 | |
b73bfc1c | 5189 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ |
4ed46869 | 5190 | |
b73bfc1c | 5191 | static void |
971de7fb | 5192 | decode_coding_raw_text (struct coding_system *coding) |
4ed46869 | 5193 | { |
f10fe38f PE |
5194 | bool eol_dos |
5195 | = !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); | |
119852e7 | 5196 | |
df7492f9 | 5197 | coding->chars_at_source = 1; |
119852e7 KH |
5198 | coding->consumed_char = coding->src_chars; |
5199 | coding->consumed = coding->src_bytes; | |
2735d060 | 5200 | if (eol_dos && coding->source[coding->src_bytes - 1] == '\r') |
119852e7 KH |
5201 | { |
5202 | coding->consumed_char--; | |
5203 | coding->consumed--; | |
5204 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC); | |
5205 | } | |
5206 | else | |
5207 | record_conversion_result (coding, CODING_RESULT_SUCCESS); | |
df7492f9 | 5208 | } |
4ed46869 | 5209 | |
f10fe38f | 5210 | static bool |
971de7fb | 5211 | encode_coding_raw_text (struct coding_system *coding) |
df7492f9 | 5212 | { |
f10fe38f | 5213 | bool multibytep = coding->dst_multibyte; |
df7492f9 KH |
5214 | int *charbuf = coding->charbuf; |
5215 | int *charbuf_end = coding->charbuf + coding->charbuf_used; | |
5216 | unsigned char *dst = coding->destination + coding->produced; | |
5217 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
d311d28c | 5218 | ptrdiff_t produced_chars = 0; |
b73bfc1c KH |
5219 | int c; |
5220 | ||
df7492f9 | 5221 | if (multibytep) |
b73bfc1c | 5222 | { |
df7492f9 | 5223 | int safe_room = MAX_MULTIBYTE_LENGTH * 2; |
4ed46869 | 5224 | |
df7492f9 KH |
5225 | if (coding->src_multibyte) |
5226 | while (charbuf < charbuf_end) | |
5227 | { | |
5228 | ASSURE_DESTINATION (safe_room); | |
5229 | c = *charbuf++; | |
5230 | if (ASCII_CHAR_P (c)) | |
5231 | EMIT_ONE_ASCII_BYTE (c); | |
5232 | else if (CHAR_BYTE8_P (c)) | |
5233 | { | |
5234 | c = CHAR_TO_BYTE8 (c); | |
5235 | EMIT_ONE_BYTE (c); | |
5236 | } | |
5237 | else | |
5238 | { | |
5239 | unsigned char str[MAX_MULTIBYTE_LENGTH], *p0 = str, *p1 = str; | |
93dec019 | 5240 | |
df7492f9 | 5241 | CHAR_STRING_ADVANCE (c, p1); |
8abc3f12 | 5242 | do |
9d123124 KH |
5243 | { |
5244 | EMIT_ONE_BYTE (*p0); | |
5245 | p0++; | |
5246 | } | |
8abc3f12 | 5247 | while (p0 < p1); |
df7492f9 KH |
5248 | } |
5249 | } | |
b73bfc1c | 5250 | else |
df7492f9 KH |
5251 | while (charbuf < charbuf_end) |
5252 | { | |
5253 | ASSURE_DESTINATION (safe_room); | |
5254 | c = *charbuf++; | |
5255 | EMIT_ONE_BYTE (c); | |
5256 | } | |
5257 | } | |
5258 | else | |
4ed46869 | 5259 | { |
df7492f9 | 5260 | if (coding->src_multibyte) |
d46c5b12 | 5261 | { |
df7492f9 KH |
5262 | int safe_room = MAX_MULTIBYTE_LENGTH; |
5263 | ||
5264 | while (charbuf < charbuf_end) | |
d46c5b12 | 5265 | { |
df7492f9 KH |
5266 | ASSURE_DESTINATION (safe_room); |
5267 | c = *charbuf++; | |
5268 | if (ASCII_CHAR_P (c)) | |
5269 | *dst++ = c; | |
5270 | else if (CHAR_BYTE8_P (c)) | |
5271 | *dst++ = CHAR_TO_BYTE8 (c); | |
b73bfc1c | 5272 | else |
df7492f9 | 5273 | CHAR_STRING_ADVANCE (c, dst); |
d46c5b12 KH |
5274 | } |
5275 | } | |
df7492f9 KH |
5276 | else |
5277 | { | |
5278 | ASSURE_DESTINATION (charbuf_end - charbuf); | |
5279 | while (charbuf < charbuf_end && dst < dst_end) | |
5280 | *dst++ = *charbuf++; | |
8f924df7 | 5281 | } |
319a3947 | 5282 | produced_chars = dst - (coding->destination + coding->produced); |
4ed46869 | 5283 | } |
065e3595 | 5284 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
a0ed9b27 | 5285 | coding->produced_char += produced_chars; |
df7492f9 KH |
5286 | coding->produced = dst - coding->destination; |
5287 | return 0; | |
4ed46869 KH |
5288 | } |
5289 | ||
ff0dacd7 | 5290 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". |
f10fe38f | 5291 | Return true if a text is encoded in a charset-based coding system. */ |
ff0dacd7 | 5292 | |
f10fe38f | 5293 | static bool |
cf84bb53 JB |
5294 | detect_coding_charset (struct coding_system *coding, |
5295 | struct coding_detection_info *detect_info) | |
1397dc18 | 5296 | { |
065e3595 | 5297 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 5298 | const unsigned char *src_end = coding->source + coding->src_bytes; |
f10fe38f | 5299 | bool multibytep = coding->src_multibyte; |
d311d28c | 5300 | ptrdiff_t consumed_chars = 0; |
07295713 | 5301 | Lisp_Object attrs, valids, name; |
584948ac | 5302 | int found = 0; |
d311d28c | 5303 | ptrdiff_t head_ascii = coding->head_ascii; |
f10fe38f | 5304 | bool check_latin_extra = 0; |
1397dc18 | 5305 | |
ff0dacd7 KH |
5306 | detect_info->checked |= CATEGORY_MASK_CHARSET; |
5307 | ||
df7492f9 KH |
5308 | coding = &coding_categories[coding_category_charset]; |
5309 | attrs = CODING_ID_ATTRS (coding->id); | |
5310 | valids = AREF (attrs, coding_attr_charset_valids); | |
07295713 | 5311 | name = CODING_ID_NAME (coding->id); |
51b59d79 | 5312 | if (strncmp (SSDATA (SYMBOL_NAME (name)), |
237aabf4 | 5313 | "iso-8859-", sizeof ("iso-8859-") - 1) == 0 |
51b59d79 | 5314 | || strncmp (SSDATA (SYMBOL_NAME (name)), |
237aabf4 | 5315 | "iso-latin-", sizeof ("iso-latin-") - 1) == 0) |
07295713 | 5316 | check_latin_extra = 1; |
237aabf4 | 5317 | |
df7492f9 | 5318 | if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) |
716b3fa0 | 5319 | src += head_ascii; |
1397dc18 | 5320 | |
b73bfc1c | 5321 | while (1) |
1397dc18 | 5322 | { |
df7492f9 | 5323 | int c; |
716b3fa0 KH |
5324 | Lisp_Object val; |
5325 | struct charset *charset; | |
5326 | int dim, idx; | |
1397dc18 | 5327 | |
065e3595 | 5328 | src_base = src; |
df7492f9 | 5329 | ONE_MORE_BYTE (c); |
065e3595 KH |
5330 | if (c < 0) |
5331 | continue; | |
716b3fa0 KH |
5332 | val = AREF (valids, c); |
5333 | if (NILP (val)) | |
df7492f9 | 5334 | break; |
584948ac | 5335 | if (c >= 0x80) |
07295713 KH |
5336 | { |
5337 | if (c < 0xA0 | |
237aabf4 JR |
5338 | && check_latin_extra |
5339 | && (!VECTORP (Vlatin_extra_code_table) | |
28be1ada | 5340 | || NILP (AREF (Vlatin_extra_code_table, c)))) |
07295713 KH |
5341 | break; |
5342 | found = CATEGORY_MASK_CHARSET; | |
5343 | } | |
716b3fa0 KH |
5344 | if (INTEGERP (val)) |
5345 | { | |
5346 | charset = CHARSET_FROM_ID (XFASTINT (val)); | |
5347 | dim = CHARSET_DIMENSION (charset); | |
5348 | for (idx = 1; idx < dim; idx++) | |
5349 | { | |
5350 | if (src == src_end) | |
5351 | goto too_short; | |
5352 | ONE_MORE_BYTE (c); | |
2f9442b8 PE |
5353 | if (c < charset->code_space[(dim - 1 - idx) * 4] |
5354 | || c > charset->code_space[(dim - 1 - idx) * 4 + 1]) | |
716b3fa0 KH |
5355 | break; |
5356 | } | |
5357 | if (idx < dim) | |
5358 | break; | |
5359 | } | |
5360 | else | |
5361 | { | |
5362 | idx = 1; | |
5363 | for (; CONSP (val); val = XCDR (val)) | |
5364 | { | |
5365 | charset = CHARSET_FROM_ID (XFASTINT (XCAR (val))); | |
5366 | dim = CHARSET_DIMENSION (charset); | |
5367 | while (idx < dim) | |
5368 | { | |
5369 | if (src == src_end) | |
5370 | goto too_short; | |
5371 | ONE_MORE_BYTE (c); | |
5372 | if (c < charset->code_space[(dim - 1 - idx) * 4] | |
5373 | || c > charset->code_space[(dim - 1 - idx) * 4 + 1]) | |
5374 | break; | |
5375 | idx++; | |
5376 | } | |
5377 | if (idx == dim) | |
5378 | { | |
5379 | val = Qnil; | |
5380 | break; | |
5381 | } | |
5382 | } | |
5383 | if (CONSP (val)) | |
5384 | break; | |
5385 | } | |
df7492f9 | 5386 | } |
716b3fa0 | 5387 | too_short: |
ff0dacd7 | 5388 | detect_info->rejected |= CATEGORY_MASK_CHARSET; |
df7492f9 | 5389 | return 0; |
4ed46869 | 5390 | |
df7492f9 | 5391 | no_more_source: |
ff0dacd7 KH |
5392 | detect_info->found |= found; |
5393 | return 1; | |
df7492f9 | 5394 | } |
b73bfc1c | 5395 | |
b73bfc1c | 5396 | static void |
971de7fb | 5397 | decode_coding_charset (struct coding_system *coding) |
4ed46869 | 5398 | { |
8f924df7 KH |
5399 | const unsigned char *src = coding->source + coding->consumed; |
5400 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
5401 | const unsigned char *src_base; | |
69a80ea3 | 5402 | int *charbuf = coding->charbuf + coding->charbuf_used; |
ad1746f5 | 5403 | /* We may produce one charset annotation in one loop and one more at |
df80c7f0 | 5404 | the end. */ |
69a80ea3 | 5405 | int *charbuf_end |
df80c7f0 | 5406 | = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 2); |
d311d28c | 5407 | ptrdiff_t consumed_chars = 0, consumed_chars_base; |
f10fe38f | 5408 | bool multibytep = coding->src_multibyte; |
66ebf983 PE |
5409 | Lisp_Object attrs = CODING_ID_ATTRS (coding->id); |
5410 | Lisp_Object valids; | |
d311d28c PE |
5411 | ptrdiff_t char_offset = coding->produced_char; |
5412 | ptrdiff_t last_offset = char_offset; | |
ff0dacd7 | 5413 | int last_id = charset_ascii; |
f10fe38f PE |
5414 | bool eol_dos |
5415 | = !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); | |
119852e7 | 5416 | int byte_after_cr = -1; |
df7492f9 | 5417 | |
4eb6d3f1 | 5418 | valids = AREF (attrs, coding_attr_charset_valids); |
b73bfc1c | 5419 | |
df7492f9 | 5420 | while (1) |
4ed46869 | 5421 | { |
4eb6d3f1 | 5422 | int c; |
24a73b0a KH |
5423 | Lisp_Object val; |
5424 | struct charset *charset; | |
5425 | int dim; | |
5426 | int len = 1; | |
5427 | unsigned code; | |
df7492f9 KH |
5428 | |
5429 | src_base = src; | |
5430 | consumed_chars_base = consumed_chars; | |
b73bfc1c | 5431 | |
df7492f9 | 5432 | if (charbuf >= charbuf_end) |
b71f6f73 KH |
5433 | { |
5434 | if (byte_after_cr >= 0) | |
5435 | src_base--; | |
5436 | break; | |
5437 | } | |
df7492f9 | 5438 | |
119852e7 KH |
5439 | if (byte_after_cr >= 0) |
5440 | { | |
5441 | c = byte_after_cr; | |
5442 | byte_after_cr = -1; | |
5443 | } | |
5444 | else | |
5445 | { | |
5446 | ONE_MORE_BYTE (c); | |
2735d060 | 5447 | if (eol_dos && c == '\r') |
119852e7 KH |
5448 | ONE_MORE_BYTE (byte_after_cr); |
5449 | } | |
065e3595 KH |
5450 | if (c < 0) |
5451 | goto invalid_code; | |
24a73b0a KH |
5452 | code = c; |
5453 | ||
5454 | val = AREF (valids, c); | |
1b17adfd | 5455 | if (! INTEGERP (val) && ! CONSP (val)) |
24a73b0a KH |
5456 | goto invalid_code; |
5457 | if (INTEGERP (val)) | |
d46c5b12 | 5458 | { |
24a73b0a KH |
5459 | charset = CHARSET_FROM_ID (XFASTINT (val)); |
5460 | dim = CHARSET_DIMENSION (charset); | |
5461 | while (len < dim) | |
b73bfc1c | 5462 | { |
24a73b0a KH |
5463 | ONE_MORE_BYTE (c); |
5464 | code = (code << 8) | c; | |
5465 | len++; | |
b73bfc1c | 5466 | } |
24a73b0a KH |
5467 | CODING_DECODE_CHAR (coding, src, src_base, src_end, |
5468 | charset, code, c); | |
d46c5b12 | 5469 | } |
df7492f9 | 5470 | else |
d46c5b12 | 5471 | { |
24a73b0a KH |
5472 | /* VAL is a list of charset IDs. It is assured that the |
5473 | list is sorted by charset dimensions (smaller one | |
5474 | comes first). */ | |
5475 | while (CONSP (val)) | |
4eb6d3f1 | 5476 | { |
24a73b0a | 5477 | charset = CHARSET_FROM_ID (XFASTINT (XCAR (val))); |
c7c66a95 | 5478 | dim = CHARSET_DIMENSION (charset); |
f9d71dcd | 5479 | while (len < dim) |
4eb6d3f1 | 5480 | { |
acb2a965 KH |
5481 | ONE_MORE_BYTE (c); |
5482 | code = (code << 8) | c; | |
f9d71dcd | 5483 | len++; |
4eb6d3f1 | 5484 | } |
24a73b0a KH |
5485 | CODING_DECODE_CHAR (coding, src, src_base, |
5486 | src_end, charset, code, c); | |
5487 | if (c >= 0) | |
5488 | break; | |
5489 | val = XCDR (val); | |
ff0dacd7 | 5490 | } |
d46c5b12 | 5491 | } |
24a73b0a KH |
5492 | if (c < 0) |
5493 | goto invalid_code; | |
5494 | if (charset->id != charset_ascii | |
5495 | && last_id != charset->id) | |
5496 | { | |
5497 | if (last_id != charset_ascii) | |
69a80ea3 | 5498 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
24a73b0a KH |
5499 | last_id = charset->id; |
5500 | last_offset = char_offset; | |
5501 | } | |
5502 | ||
df7492f9 | 5503 | *charbuf++ = c; |
ff0dacd7 | 5504 | char_offset++; |
df7492f9 KH |
5505 | continue; |
5506 | ||
5507 | invalid_code: | |
5508 | src = src_base; | |
5509 | consumed_chars = consumed_chars_base; | |
5510 | ONE_MORE_BYTE (c); | |
065e3595 | 5511 | *charbuf++ = c < 0 ? -c : ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 5512 | char_offset++; |
df7492f9 | 5513 | coding->errors++; |
4ed46869 KH |
5514 | } |
5515 | ||
df7492f9 | 5516 | no_more_source: |
ff0dacd7 | 5517 | if (last_id != charset_ascii) |
69a80ea3 | 5518 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
5519 | coding->consumed_char += consumed_chars_base; |
5520 | coding->consumed = src_base - coding->source; | |
5521 | coding->charbuf_used = charbuf - coding->charbuf; | |
4ed46869 KH |
5522 | } |
5523 | ||
f10fe38f | 5524 | static bool |
971de7fb | 5525 | encode_coding_charset (struct coding_system *coding) |
4ed46869 | 5526 | { |
f10fe38f | 5527 | bool multibytep = coding->dst_multibyte; |
df7492f9 KH |
5528 | int *charbuf = coding->charbuf; |
5529 | int *charbuf_end = charbuf + coding->charbuf_used; | |
5530 | unsigned char *dst = coding->destination + coding->produced; | |
5531 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
5532 | int safe_room = MAX_MULTIBYTE_LENGTH; | |
d311d28c | 5533 | ptrdiff_t produced_chars = 0; |
24a73b0a | 5534 | Lisp_Object attrs, charset_list; |
f10fe38f | 5535 | bool ascii_compatible; |
b73bfc1c | 5536 | int c; |
b73bfc1c | 5537 | |
24a73b0a | 5538 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 | 5539 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); |
fb88bf2d | 5540 | |
df7492f9 | 5541 | while (charbuf < charbuf_end) |
4ed46869 | 5542 | { |
4eb6d3f1 | 5543 | struct charset *charset; |
df7492f9 | 5544 | unsigned code; |
8f924df7 | 5545 | |
df7492f9 KH |
5546 | ASSURE_DESTINATION (safe_room); |
5547 | c = *charbuf++; | |
5548 | if (ascii_compatible && ASCII_CHAR_P (c)) | |
5549 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d | 5550 | else if (CHAR_BYTE8_P (c)) |
4ed46869 | 5551 | { |
16eafb5d KH |
5552 | c = CHAR_TO_BYTE8 (c); |
5553 | EMIT_ONE_BYTE (c); | |
d46c5b12 | 5554 | } |
d46c5b12 | 5555 | else |
b73bfc1c | 5556 | { |
5eb05ea3 KH |
5557 | CODING_CHAR_CHARSET (coding, dst, dst_end, c, charset_list, |
5558 | &code, charset); | |
5559 | ||
4eb6d3f1 KH |
5560 | if (charset) |
5561 | { | |
5562 | if (CHARSET_DIMENSION (charset) == 1) | |
5563 | EMIT_ONE_BYTE (code); | |
5564 | else if (CHARSET_DIMENSION (charset) == 2) | |
5565 | EMIT_TWO_BYTES (code >> 8, code & 0xFF); | |
5566 | else if (CHARSET_DIMENSION (charset) == 3) | |
5567 | EMIT_THREE_BYTES (code >> 16, (code >> 8) & 0xFF, code & 0xFF); | |
5568 | else | |
5569 | EMIT_FOUR_BYTES (code >> 24, (code >> 16) & 0xFF, | |
5570 | (code >> 8) & 0xFF, code & 0xFF); | |
5571 | } | |
5572 | else | |
41cbe562 KH |
5573 | { |
5574 | if (coding->mode & CODING_MODE_SAFE_ENCODING) | |
5575 | c = CODING_INHIBIT_CHARACTER_SUBSTITUTION; | |
5576 | else | |
5577 | c = coding->default_char; | |
5578 | EMIT_ONE_BYTE (c); | |
5579 | } | |
4ed46869 | 5580 | } |
4ed46869 KH |
5581 | } |
5582 | ||
065e3595 | 5583 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
5584 | coding->produced_char += produced_chars; |
5585 | coding->produced = dst - coding->destination; | |
5586 | return 0; | |
4ed46869 KH |
5587 | } |
5588 | ||
5589 | \f | |
1397dc18 | 5590 | /*** 7. C library functions ***/ |
4ed46869 | 5591 | |
df7492f9 KH |
5592 | /* Setup coding context CODING from information about CODING_SYSTEM. |
5593 | If CODING_SYSTEM is nil, `no-conversion' is assumed. If | |
5594 | CODING_SYSTEM is invalid, signal an error. */ | |
4ed46869 | 5595 | |
ec6d2bb8 | 5596 | void |
971de7fb | 5597 | setup_coding_system (Lisp_Object coding_system, struct coding_system *coding) |
4ed46869 | 5598 | { |
df7492f9 KH |
5599 | Lisp_Object attrs; |
5600 | Lisp_Object eol_type; | |
5601 | Lisp_Object coding_type; | |
4608c386 | 5602 | Lisp_Object val; |
4ed46869 | 5603 | |
df7492f9 | 5604 | if (NILP (coding_system)) |
ae6f73fa | 5605 | coding_system = Qundecided; |
c07c8e12 | 5606 | |
df7492f9 | 5607 | CHECK_CODING_SYSTEM_GET_ID (coding_system, coding->id); |
1f5dbf34 | 5608 | |
df7492f9 | 5609 | attrs = CODING_ID_ATTRS (coding->id); |
0a9564cb | 5610 | eol_type = inhibit_eol_conversion ? Qunix : CODING_ID_EOL_TYPE (coding->id); |
1f5dbf34 | 5611 | |
df7492f9 KH |
5612 | coding->mode = 0; |
5613 | coding->head_ascii = -1; | |
4a015c45 KH |
5614 | if (VECTORP (eol_type)) |
5615 | coding->common_flags = (CODING_REQUIRE_DECODING_MASK | |
5616 | | CODING_REQUIRE_DETECTION_MASK); | |
5617 | else if (! EQ (eol_type, Qunix)) | |
5618 | coding->common_flags = (CODING_REQUIRE_DECODING_MASK | |
5619 | | CODING_REQUIRE_ENCODING_MASK); | |
5620 | else | |
5621 | coding->common_flags = 0; | |
5e5c78be KH |
5622 | if (! NILP (CODING_ATTR_POST_READ (attrs))) |
5623 | coding->common_flags |= CODING_REQUIRE_DECODING_MASK; | |
5624 | if (! NILP (CODING_ATTR_PRE_WRITE (attrs))) | |
5625 | coding->common_flags |= CODING_REQUIRE_ENCODING_MASK; | |
8f924df7 KH |
5626 | if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs))) |
5627 | coding->common_flags |= CODING_FOR_UNIBYTE_MASK; | |
1f5dbf34 | 5628 | |
df7492f9 | 5629 | val = CODING_ATTR_SAFE_CHARSETS (attrs); |
8f924df7 | 5630 | coding->max_charset_id = SCHARS (val) - 1; |
1b3b981b | 5631 | coding->safe_charsets = SDATA (val); |
df7492f9 | 5632 | coding->default_char = XINT (CODING_ATTR_DEFAULT_CHAR (attrs)); |
624bda09 | 5633 | coding->carryover_bytes = 0; |
4608c386 | 5634 | |
df7492f9 KH |
5635 | coding_type = CODING_ATTR_TYPE (attrs); |
5636 | if (EQ (coding_type, Qundecided)) | |
d46c5b12 | 5637 | { |
df7492f9 KH |
5638 | coding->detector = NULL; |
5639 | coding->decoder = decode_coding_raw_text; | |
5640 | coding->encoder = encode_coding_raw_text; | |
5641 | coding->common_flags |= CODING_REQUIRE_DETECTION_MASK; | |
d46c5b12 | 5642 | } |
df7492f9 | 5643 | else if (EQ (coding_type, Qiso_2022)) |
d46c5b12 | 5644 | { |
df7492f9 KH |
5645 | int i; |
5646 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); | |
5647 | ||
5648 | /* Invoke graphic register 0 to plane 0. */ | |
5649 | CODING_ISO_INVOCATION (coding, 0) = 0; | |
5650 | /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */ | |
5651 | CODING_ISO_INVOCATION (coding, 1) | |
5652 | = (flags & CODING_ISO_FLAG_SEVEN_BITS ? -1 : 1); | |
5653 | /* Setup the initial status of designation. */ | |
5654 | for (i = 0; i < 4; i++) | |
5655 | CODING_ISO_DESIGNATION (coding, i) = CODING_ISO_INITIAL (coding, i); | |
5656 | /* Not single shifting initially. */ | |
5657 | CODING_ISO_SINGLE_SHIFTING (coding) = 0; | |
5658 | /* Beginning of buffer should also be regarded as bol. */ | |
5659 | CODING_ISO_BOL (coding) = 1; | |
5660 | coding->detector = detect_coding_iso_2022; | |
5661 | coding->decoder = decode_coding_iso_2022; | |
5662 | coding->encoder = encode_coding_iso_2022; | |
5663 | if (flags & CODING_ISO_FLAG_SAFE) | |
5664 | coding->mode |= CODING_MODE_SAFE_ENCODING; | |
d46c5b12 | 5665 | coding->common_flags |
df7492f9 KH |
5666 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK |
5667 | | CODING_REQUIRE_FLUSHING_MASK); | |
5668 | if (flags & CODING_ISO_FLAG_COMPOSITION) | |
5669 | coding->common_flags |= CODING_ANNOTATE_COMPOSITION_MASK; | |
ff0dacd7 KH |
5670 | if (flags & CODING_ISO_FLAG_DESIGNATION) |
5671 | coding->common_flags |= CODING_ANNOTATE_CHARSET_MASK; | |
df7492f9 KH |
5672 | if (flags & CODING_ISO_FLAG_FULL_SUPPORT) |
5673 | { | |
5674 | setup_iso_safe_charsets (attrs); | |
5675 | val = CODING_ATTR_SAFE_CHARSETS (attrs); | |
8f924df7 | 5676 | coding->max_charset_id = SCHARS (val) - 1; |
1b3b981b | 5677 | coding->safe_charsets = SDATA (val); |
df7492f9 KH |
5678 | } |
5679 | CODING_ISO_FLAGS (coding) = flags; | |
e951386e KH |
5680 | CODING_ISO_CMP_STATUS (coding)->state = COMPOSING_NO; |
5681 | CODING_ISO_CMP_STATUS (coding)->method = COMPOSITION_NO; | |
5682 | CODING_ISO_EXTSEGMENT_LEN (coding) = 0; | |
5683 | CODING_ISO_EMBEDDED_UTF_8 (coding) = 0; | |
d46c5b12 | 5684 | } |
df7492f9 | 5685 | else if (EQ (coding_type, Qcharset)) |
d46c5b12 | 5686 | { |
df7492f9 KH |
5687 | coding->detector = detect_coding_charset; |
5688 | coding->decoder = decode_coding_charset; | |
5689 | coding->encoder = encode_coding_charset; | |
d46c5b12 | 5690 | coding->common_flags |
df7492f9 | 5691 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
d46c5b12 | 5692 | } |
df7492f9 | 5693 | else if (EQ (coding_type, Qutf_8)) |
d46c5b12 | 5694 | { |
a470d443 KH |
5695 | val = AREF (attrs, coding_attr_utf_bom); |
5696 | CODING_UTF_8_BOM (coding) = (CONSP (val) ? utf_detect_bom | |
5697 | : EQ (val, Qt) ? utf_with_bom | |
5698 | : utf_without_bom); | |
df7492f9 KH |
5699 | coding->detector = detect_coding_utf_8; |
5700 | coding->decoder = decode_coding_utf_8; | |
5701 | coding->encoder = encode_coding_utf_8; | |
5702 | coding->common_flags | |
5703 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); | |
a470d443 KH |
5704 | if (CODING_UTF_8_BOM (coding) == utf_detect_bom) |
5705 | coding->common_flags |= CODING_REQUIRE_DETECTION_MASK; | |
df7492f9 KH |
5706 | } |
5707 | else if (EQ (coding_type, Qutf_16)) | |
5708 | { | |
a470d443 KH |
5709 | val = AREF (attrs, coding_attr_utf_bom); |
5710 | CODING_UTF_16_BOM (coding) = (CONSP (val) ? utf_detect_bom | |
5711 | : EQ (val, Qt) ? utf_with_bom | |
5712 | : utf_without_bom); | |
df7492f9 | 5713 | val = AREF (attrs, coding_attr_utf_16_endian); |
b49a1807 | 5714 | CODING_UTF_16_ENDIAN (coding) = (EQ (val, Qbig) ? utf_16_big_endian |
df7492f9 | 5715 | : utf_16_little_endian); |
e19c3639 | 5716 | CODING_UTF_16_SURROGATE (coding) = 0; |
df7492f9 KH |
5717 | coding->detector = detect_coding_utf_16; |
5718 | coding->decoder = decode_coding_utf_16; | |
5719 | coding->encoder = encode_coding_utf_16; | |
5720 | coding->common_flags | |
5721 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); | |
a470d443 | 5722 | if (CODING_UTF_16_BOM (coding) == utf_detect_bom) |
b49a1807 | 5723 | coding->common_flags |= CODING_REQUIRE_DETECTION_MASK; |
d46c5b12 | 5724 | } |
df7492f9 | 5725 | else if (EQ (coding_type, Qccl)) |
4ed46869 | 5726 | { |
df7492f9 KH |
5727 | coding->detector = detect_coding_ccl; |
5728 | coding->decoder = decode_coding_ccl; | |
5729 | coding->encoder = encode_coding_ccl; | |
c952af22 | 5730 | coding->common_flags |
df7492f9 KH |
5731 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK |
5732 | | CODING_REQUIRE_FLUSHING_MASK); | |
5733 | } | |
5734 | else if (EQ (coding_type, Qemacs_mule)) | |
5735 | { | |
5736 | coding->detector = detect_coding_emacs_mule; | |
5737 | coding->decoder = decode_coding_emacs_mule; | |
5738 | coding->encoder = encode_coding_emacs_mule; | |
c952af22 | 5739 | coding->common_flags |
df7492f9 KH |
5740 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
5741 | if (! NILP (AREF (attrs, coding_attr_emacs_mule_full)) | |
5742 | && ! EQ (CODING_ATTR_CHARSET_LIST (attrs), Vemacs_mule_charset_list)) | |
5743 | { | |
5744 | Lisp_Object tail, safe_charsets; | |
5745 | int max_charset_id = 0; | |
5746 | ||
5747 | for (tail = Vemacs_mule_charset_list; CONSP (tail); | |
5748 | tail = XCDR (tail)) | |
5749 | if (max_charset_id < XFASTINT (XCAR (tail))) | |
5750 | max_charset_id = XFASTINT (XCAR (tail)); | |
1b3b981b AS |
5751 | safe_charsets = make_uninit_string (max_charset_id + 1); |
5752 | memset (SDATA (safe_charsets), 255, max_charset_id + 1); | |
df7492f9 KH |
5753 | for (tail = Vemacs_mule_charset_list; CONSP (tail); |
5754 | tail = XCDR (tail)) | |
8f924df7 | 5755 | SSET (safe_charsets, XFASTINT (XCAR (tail)), 0); |
df7492f9 | 5756 | coding->max_charset_id = max_charset_id; |
1b3b981b | 5757 | coding->safe_charsets = SDATA (safe_charsets); |
df7492f9 | 5758 | } |
e951386e KH |
5759 | coding->spec.emacs_mule.cmp_status.state = COMPOSING_NO; |
5760 | coding->spec.emacs_mule.cmp_status.method = COMPOSITION_NO; | |
df7492f9 KH |
5761 | } |
5762 | else if (EQ (coding_type, Qshift_jis)) | |
5763 | { | |
5764 | coding->detector = detect_coding_sjis; | |
5765 | coding->decoder = decode_coding_sjis; | |
5766 | coding->encoder = encode_coding_sjis; | |
c952af22 | 5767 | coding->common_flags |
df7492f9 KH |
5768 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
5769 | } | |
5770 | else if (EQ (coding_type, Qbig5)) | |
5771 | { | |
5772 | coding->detector = detect_coding_big5; | |
5773 | coding->decoder = decode_coding_big5; | |
5774 | coding->encoder = encode_coding_big5; | |
c952af22 | 5775 | coding->common_flags |
df7492f9 KH |
5776 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
5777 | } | |
5778 | else /* EQ (coding_type, Qraw_text) */ | |
ec6d2bb8 | 5779 | { |
df7492f9 KH |
5780 | coding->detector = NULL; |
5781 | coding->decoder = decode_coding_raw_text; | |
5782 | coding->encoder = encode_coding_raw_text; | |
ea29edf2 KH |
5783 | if (! EQ (eol_type, Qunix)) |
5784 | { | |
5785 | coding->common_flags |= CODING_REQUIRE_DECODING_MASK; | |
5786 | if (! VECTORP (eol_type)) | |
5787 | coding->common_flags |= CODING_REQUIRE_ENCODING_MASK; | |
5788 | } | |
5789 | ||
4ed46869 | 5790 | } |
4ed46869 | 5791 | |
df7492f9 | 5792 | return; |
4ed46869 KH |
5793 | } |
5794 | ||
0ff61e78 KH |
5795 | /* Return a list of charsets supported by CODING. */ |
5796 | ||
5797 | Lisp_Object | |
971de7fb | 5798 | coding_charset_list (struct coding_system *coding) |
0ff61e78 | 5799 | { |
35befdaa | 5800 | Lisp_Object attrs, charset_list; |
0ff61e78 KH |
5801 | |
5802 | CODING_GET_INFO (coding, attrs, charset_list); | |
5803 | if (EQ (CODING_ATTR_TYPE (attrs), Qiso_2022)) | |
5804 | { | |
5805 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); | |
5806 | ||
5807 | if (flags & CODING_ISO_FLAG_FULL_SUPPORT) | |
5808 | charset_list = Viso_2022_charset_list; | |
5809 | } | |
5810 | else if (EQ (CODING_ATTR_TYPE (attrs), Qemacs_mule)) | |
5811 | { | |
5812 | charset_list = Vemacs_mule_charset_list; | |
5813 | } | |
5814 | return charset_list; | |
5815 | } | |
5816 | ||
5817 | ||
e9f91ece KH |
5818 | /* Return a list of charsets supported by CODING-SYSTEM. */ |
5819 | ||
5820 | Lisp_Object | |
971de7fb | 5821 | coding_system_charset_list (Lisp_Object coding_system) |
e9f91ece | 5822 | { |
d3411f89 | 5823 | ptrdiff_t id; |
e9f91ece KH |
5824 | Lisp_Object attrs, charset_list; |
5825 | ||
5826 | CHECK_CODING_SYSTEM_GET_ID (coding_system, id); | |
5827 | attrs = CODING_ID_ATTRS (id); | |
5828 | ||
5829 | if (EQ (CODING_ATTR_TYPE (attrs), Qiso_2022)) | |
5830 | { | |
5831 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); | |
5832 | ||
5833 | if (flags & CODING_ISO_FLAG_FULL_SUPPORT) | |
5834 | charset_list = Viso_2022_charset_list; | |
5835 | else | |
5836 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
5837 | } | |
5838 | else if (EQ (CODING_ATTR_TYPE (attrs), Qemacs_mule)) | |
5839 | { | |
5840 | charset_list = Vemacs_mule_charset_list; | |
5841 | } | |
5842 | else | |
5843 | { | |
5844 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
5845 | } | |
5846 | return charset_list; | |
5847 | } | |
5848 | ||
5849 | ||
df7492f9 KH |
5850 | /* Return raw-text or one of its subsidiaries that has the same |
5851 | eol_type as CODING-SYSTEM. */ | |
ec6d2bb8 | 5852 | |
df7492f9 | 5853 | Lisp_Object |
971de7fb | 5854 | raw_text_coding_system (Lisp_Object coding_system) |
ec6d2bb8 | 5855 | { |
0be8721c | 5856 | Lisp_Object spec, attrs; |
df7492f9 | 5857 | Lisp_Object eol_type, raw_text_eol_type; |
ec6d2bb8 | 5858 | |
d3e4cb56 KH |
5859 | if (NILP (coding_system)) |
5860 | return Qraw_text; | |
df7492f9 KH |
5861 | spec = CODING_SYSTEM_SPEC (coding_system); |
5862 | attrs = AREF (spec, 0); | |
ec6d2bb8 | 5863 | |
df7492f9 KH |
5864 | if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text)) |
5865 | return coding_system; | |
ec6d2bb8 | 5866 | |
df7492f9 KH |
5867 | eol_type = AREF (spec, 2); |
5868 | if (VECTORP (eol_type)) | |
5869 | return Qraw_text; | |
5870 | spec = CODING_SYSTEM_SPEC (Qraw_text); | |
5871 | raw_text_eol_type = AREF (spec, 2); | |
5872 | return (EQ (eol_type, Qunix) ? AREF (raw_text_eol_type, 0) | |
5873 | : EQ (eol_type, Qdos) ? AREF (raw_text_eol_type, 1) | |
5874 | : AREF (raw_text_eol_type, 2)); | |
ec6d2bb8 KH |
5875 | } |
5876 | ||
54f78171 | 5877 | |
1911a33b KH |
5878 | /* If CODING_SYSTEM doesn't specify end-of-line format, return one of |
5879 | the subsidiary that has the same eol-spec as PARENT (if it is not | |
5880 | nil and specifies end-of-line format) or the system's setting | |
fcbcfb64 | 5881 | (system_eol_type). */ |
df7492f9 KH |
5882 | |
5883 | Lisp_Object | |
971de7fb | 5884 | coding_inherit_eol_type (Lisp_Object coding_system, Lisp_Object parent) |
54f78171 | 5885 | { |
3e139625 | 5886 | Lisp_Object spec, eol_type; |
54f78171 | 5887 | |
d3e4cb56 KH |
5888 | if (NILP (coding_system)) |
5889 | coding_system = Qraw_text; | |
df7492f9 | 5890 | spec = CODING_SYSTEM_SPEC (coding_system); |
df7492f9 | 5891 | eol_type = AREF (spec, 2); |
fcbcfb64 | 5892 | if (VECTORP (eol_type)) |
df7492f9 | 5893 | { |
df7492f9 KH |
5894 | Lisp_Object parent_eol_type; |
5895 | ||
fcbcfb64 KH |
5896 | if (! NILP (parent)) |
5897 | { | |
5898 | Lisp_Object parent_spec; | |
5899 | ||
4a015c45 | 5900 | parent_spec = CODING_SYSTEM_SPEC (parent); |
fcbcfb64 | 5901 | parent_eol_type = AREF (parent_spec, 2); |
1911a33b | 5902 | if (VECTORP (parent_eol_type)) |
4628bef1 | 5903 | parent_eol_type = system_eol_type; |
fcbcfb64 KH |
5904 | } |
5905 | else | |
5906 | parent_eol_type = system_eol_type; | |
df7492f9 KH |
5907 | if (EQ (parent_eol_type, Qunix)) |
5908 | coding_system = AREF (eol_type, 0); | |
5909 | else if (EQ (parent_eol_type, Qdos)) | |
5910 | coding_system = AREF (eol_type, 1); | |
5911 | else if (EQ (parent_eol_type, Qmac)) | |
5912 | coding_system = AREF (eol_type, 2); | |
54f78171 | 5913 | } |
df7492f9 | 5914 | return coding_system; |
54f78171 KH |
5915 | } |
5916 | ||
fcaf8878 KH |
5917 | |
5918 | /* Check if text-conversion and eol-conversion of CODING_SYSTEM are | |
5919 | decided for writing to a process. If not, complement them, and | |
5920 | return a new coding system. */ | |
5921 | ||
5922 | Lisp_Object | |
4628bef1 | 5923 | complement_process_encoding_system (Lisp_Object coding_system) |
fcaf8878 | 5924 | { |
5886ec9c KH |
5925 | Lisp_Object coding_base = Qnil, eol_base = Qnil; |
5926 | Lisp_Object spec, attrs; | |
93d50df8 | 5927 | int i; |
fcaf8878 | 5928 | |
93d50df8 | 5929 | for (i = 0; i < 3; i++) |
fcaf8878 | 5930 | { |
93d50df8 KH |
5931 | if (i == 1) |
5932 | coding_system = CDR_SAFE (Vdefault_process_coding_system); | |
5933 | else if (i == 2) | |
5934 | coding_system = preferred_coding_system (); | |
5935 | spec = CODING_SYSTEM_SPEC (coding_system); | |
5936 | if (NILP (spec)) | |
5937 | continue; | |
5938 | attrs = AREF (spec, 0); | |
5939 | if (NILP (coding_base) && ! EQ (CODING_ATTR_TYPE (attrs), Qundecided)) | |
5940 | coding_base = CODING_ATTR_BASE_NAME (attrs); | |
5941 | if (NILP (eol_base) && ! VECTORP (AREF (spec, 2))) | |
5942 | eol_base = coding_system; | |
5943 | if (! NILP (coding_base) && ! NILP (eol_base)) | |
5944 | break; | |
fcaf8878 | 5945 | } |
fcaf8878 | 5946 | |
93d50df8 KH |
5947 | if (i > 0) |
5948 | /* The original CODING_SYSTEM didn't specify text-conversion or | |
5949 | eol-conversion. Be sure that we return a fully complemented | |
5950 | coding system. */ | |
5951 | coding_system = coding_inherit_eol_type (coding_base, eol_base); | |
5952 | return coding_system; | |
fcaf8878 KH |
5953 | } |
5954 | ||
5955 | ||
4ed46869 KH |
5956 | /* Emacs has a mechanism to automatically detect a coding system if it |
5957 | is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But, | |
5958 | it's impossible to distinguish some coding systems accurately | |
5959 | because they use the same range of codes. So, at first, coding | |
5960 | systems are categorized into 7, those are: | |
5961 | ||
0ef69138 | 5962 | o coding-category-emacs-mule |
4ed46869 KH |
5963 | |
5964 | The category for a coding system which has the same code range | |
5965 | as Emacs' internal format. Assigned the coding-system (Lisp | |
0ef69138 | 5966 | symbol) `emacs-mule' by default. |
4ed46869 KH |
5967 | |
5968 | o coding-category-sjis | |
5969 | ||
5970 | The category for a coding system which has the same code range | |
5971 | as SJIS. Assigned the coding-system (Lisp | |
7717c392 | 5972 | symbol) `japanese-shift-jis' by default. |
4ed46869 KH |
5973 | |
5974 | o coding-category-iso-7 | |
5975 | ||
5976 | The category for a coding system which has the same code range | |
7717c392 | 5977 | as ISO2022 of 7-bit environment. This doesn't use any locking |
d46c5b12 KH |
5978 | shift and single shift functions. This can encode/decode all |
5979 | charsets. Assigned the coding-system (Lisp symbol) | |
5980 | `iso-2022-7bit' by default. | |
5981 | ||
5982 | o coding-category-iso-7-tight | |
5983 | ||
5984 | Same as coding-category-iso-7 except that this can | |
5985 | encode/decode only the specified charsets. | |
4ed46869 KH |
5986 | |
5987 | o coding-category-iso-8-1 | |
5988 | ||
5989 | The category for a coding system which has the same code range | |
5990 | as ISO2022 of 8-bit environment and graphic plane 1 used only | |
7717c392 KH |
5991 | for DIMENSION1 charset. This doesn't use any locking shift |
5992 | and single shift functions. Assigned the coding-system (Lisp | |
5993 | symbol) `iso-latin-1' by default. | |
4ed46869 KH |
5994 | |
5995 | o coding-category-iso-8-2 | |
5996 | ||
5997 | The category for a coding system which has the same code range | |
5998 | as ISO2022 of 8-bit environment and graphic plane 1 used only | |
7717c392 KH |
5999 | for DIMENSION2 charset. This doesn't use any locking shift |
6000 | and single shift functions. Assigned the coding-system (Lisp | |
6001 | symbol) `japanese-iso-8bit' by default. | |
4ed46869 | 6002 | |
7717c392 | 6003 | o coding-category-iso-7-else |
4ed46869 KH |
6004 | |
6005 | The category for a coding system which has the same code range | |
ad1746f5 | 6006 | as ISO2022 of 7-bit environment but uses locking shift or |
7717c392 KH |
6007 | single shift functions. Assigned the coding-system (Lisp |
6008 | symbol) `iso-2022-7bit-lock' by default. | |
6009 | ||
6010 | o coding-category-iso-8-else | |
6011 | ||
6012 | The category for a coding system which has the same code range | |
ad1746f5 | 6013 | as ISO2022 of 8-bit environment but uses locking shift or |
7717c392 KH |
6014 | single shift functions. Assigned the coding-system (Lisp |
6015 | symbol) `iso-2022-8bit-ss2' by default. | |
4ed46869 KH |
6016 | |
6017 | o coding-category-big5 | |
6018 | ||
6019 | The category for a coding system which has the same code range | |
6020 | as BIG5. Assigned the coding-system (Lisp symbol) | |
e0e989f6 | 6021 | `cn-big5' by default. |
4ed46869 | 6022 | |
fa42c37f KH |
6023 | o coding-category-utf-8 |
6024 | ||
6025 | The category for a coding system which has the same code range | |
6e76ae91 | 6026 | as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp |
fa42c37f KH |
6027 | symbol) `utf-8' by default. |
6028 | ||
6029 | o coding-category-utf-16-be | |
6030 | ||
6031 | The category for a coding system in which a text has an | |
6032 | Unicode signature (cf. Unicode Standard) in the order of BIG | |
6033 | endian at the head. Assigned the coding-system (Lisp symbol) | |
6034 | `utf-16-be' by default. | |
6035 | ||
6036 | o coding-category-utf-16-le | |
6037 | ||
6038 | The category for a coding system in which a text has an | |
6039 | Unicode signature (cf. Unicode Standard) in the order of | |
6040 | LITTLE endian at the head. Assigned the coding-system (Lisp | |
6041 | symbol) `utf-16-le' by default. | |
6042 | ||
1397dc18 KH |
6043 | o coding-category-ccl |
6044 | ||
6045 | The category for a coding system of which encoder/decoder is | |
6046 | written in CCL programs. The default value is nil, i.e., no | |
6047 | coding system is assigned. | |
6048 | ||
4ed46869 KH |
6049 | o coding-category-binary |
6050 | ||
6051 | The category for a coding system not categorized in any of the | |
6052 | above. Assigned the coding-system (Lisp symbol) | |
e0e989f6 | 6053 | `no-conversion' by default. |
4ed46869 KH |
6054 | |
6055 | Each of them is a Lisp symbol and the value is an actual | |
df7492f9 | 6056 | `coding-system's (this is also a Lisp symbol) assigned by a user. |
4ed46869 KH |
6057 | What Emacs does actually is to detect a category of coding system. |
6058 | Then, it uses a `coding-system' assigned to it. If Emacs can't | |
df7492f9 | 6059 | decide only one possible category, it selects a category of the |
4ed46869 KH |
6060 | highest priority. Priorities of categories are also specified by a |
6061 | user in a Lisp variable `coding-category-list'. | |
6062 | ||
6063 | */ | |
6064 | ||
df7492f9 KH |
6065 | #define EOL_SEEN_NONE 0 |
6066 | #define EOL_SEEN_LF 1 | |
6067 | #define EOL_SEEN_CR 2 | |
6068 | #define EOL_SEEN_CRLF 4 | |
66cfb530 | 6069 | |
ff0dacd7 KH |
6070 | /* Detect how end-of-line of a text of length SRC_BYTES pointed by |
6071 | SOURCE is encoded. If CATEGORY is one of | |
6072 | coding_category_utf_16_XXXX, assume that CR and LF are encoded by | |
6073 | two-byte, else they are encoded by one-byte. | |
6074 | ||
6075 | Return one of EOL_SEEN_XXX. */ | |
4ed46869 | 6076 | |
bc4bc72a | 6077 | #define MAX_EOL_CHECK_COUNT 3 |
d46c5b12 KH |
6078 | |
6079 | static int | |
d311d28c | 6080 | detect_eol (const unsigned char *source, ptrdiff_t src_bytes, |
cf84bb53 | 6081 | enum coding_category category) |
4ed46869 | 6082 | { |
f6cbaf43 | 6083 | const unsigned char *src = source, *src_end = src + src_bytes; |
4ed46869 | 6084 | unsigned char c; |
df7492f9 KH |
6085 | int total = 0; |
6086 | int eol_seen = EOL_SEEN_NONE; | |
4ed46869 | 6087 | |
89528eb3 | 6088 | if ((1 << category) & CATEGORY_MASK_UTF_16) |
4ed46869 | 6089 | { |
f10fe38f PE |
6090 | bool msb = category == (coding_category_utf_16_le |
6091 | | coding_category_utf_16_le_nosig); | |
6092 | bool lsb = !msb; | |
fa42c37f | 6093 | |
df7492f9 | 6094 | while (src + 1 < src_end) |
fa42c37f | 6095 | { |
df7492f9 KH |
6096 | c = src[lsb]; |
6097 | if (src[msb] == 0 && (c == '\n' || c == '\r')) | |
fa42c37f | 6098 | { |
df7492f9 KH |
6099 | int this_eol; |
6100 | ||
6101 | if (c == '\n') | |
6102 | this_eol = EOL_SEEN_LF; | |
6103 | else if (src + 3 >= src_end | |
6104 | || src[msb + 2] != 0 | |
6105 | || src[lsb + 2] != '\n') | |
6106 | this_eol = EOL_SEEN_CR; | |
fa42c37f | 6107 | else |
75f4f1ac EZ |
6108 | { |
6109 | this_eol = EOL_SEEN_CRLF; | |
6110 | src += 2; | |
6111 | } | |
df7492f9 KH |
6112 | |
6113 | if (eol_seen == EOL_SEEN_NONE) | |
6114 | /* This is the first end-of-line. */ | |
6115 | eol_seen = this_eol; | |
6116 | else if (eol_seen != this_eol) | |
fa42c37f | 6117 | { |
75f4f1ac EZ |
6118 | /* The found type is different from what found before. |
6119 | Allow for stray ^M characters in DOS EOL files. */ | |
ef1b0ba7 SM |
6120 | if ((eol_seen == EOL_SEEN_CR && this_eol == EOL_SEEN_CRLF) |
6121 | || (eol_seen == EOL_SEEN_CRLF | |
6122 | && this_eol == EOL_SEEN_CR)) | |
75f4f1ac EZ |
6123 | eol_seen = EOL_SEEN_CRLF; |
6124 | else | |
6125 | { | |
6126 | eol_seen = EOL_SEEN_LF; | |
6127 | break; | |
6128 | } | |
fa42c37f | 6129 | } |
df7492f9 KH |
6130 | if (++total == MAX_EOL_CHECK_COUNT) |
6131 | break; | |
fa42c37f | 6132 | } |
df7492f9 | 6133 | src += 2; |
fa42c37f | 6134 | } |
bcf26d6a | 6135 | } |
d46c5b12 | 6136 | else |
ef1b0ba7 SM |
6137 | while (src < src_end) |
6138 | { | |
6139 | c = *src++; | |
6140 | if (c == '\n' || c == '\r') | |
6141 | { | |
6142 | int this_eol; | |
d46c5b12 | 6143 | |
ef1b0ba7 SM |
6144 | if (c == '\n') |
6145 | this_eol = EOL_SEEN_LF; | |
6146 | else if (src >= src_end || *src != '\n') | |
6147 | this_eol = EOL_SEEN_CR; | |
6148 | else | |
6149 | this_eol = EOL_SEEN_CRLF, src++; | |
d46c5b12 | 6150 | |
ef1b0ba7 SM |
6151 | if (eol_seen == EOL_SEEN_NONE) |
6152 | /* This is the first end-of-line. */ | |
6153 | eol_seen = this_eol; | |
6154 | else if (eol_seen != this_eol) | |
6155 | { | |
6156 | /* The found type is different from what found before. | |
6157 | Allow for stray ^M characters in DOS EOL files. */ | |
6158 | if ((eol_seen == EOL_SEEN_CR && this_eol == EOL_SEEN_CRLF) | |
6159 | || (eol_seen == EOL_SEEN_CRLF && this_eol == EOL_SEEN_CR)) | |
6160 | eol_seen = EOL_SEEN_CRLF; | |
6161 | else | |
6162 | { | |
6163 | eol_seen = EOL_SEEN_LF; | |
6164 | break; | |
6165 | } | |
6166 | } | |
6167 | if (++total == MAX_EOL_CHECK_COUNT) | |
6168 | break; | |
6169 | } | |
6170 | } | |
df7492f9 | 6171 | return eol_seen; |
73be902c KH |
6172 | } |
6173 | ||
df7492f9 | 6174 | |
24a73b0a | 6175 | static Lisp_Object |
971de7fb | 6176 | adjust_coding_eol_type (struct coding_system *coding, int eol_seen) |
73be902c | 6177 | { |
0be8721c | 6178 | Lisp_Object eol_type; |
8f924df7 | 6179 | |
df7492f9 KH |
6180 | eol_type = CODING_ID_EOL_TYPE (coding->id); |
6181 | if (eol_seen & EOL_SEEN_LF) | |
24a73b0a KH |
6182 | { |
6183 | coding->id = CODING_SYSTEM_ID (AREF (eol_type, 0)); | |
6184 | eol_type = Qunix; | |
6185 | } | |
6f197c07 | 6186 | else if (eol_seen & EOL_SEEN_CRLF) |
24a73b0a KH |
6187 | { |
6188 | coding->id = CODING_SYSTEM_ID (AREF (eol_type, 1)); | |
6189 | eol_type = Qdos; | |
6190 | } | |
6f197c07 | 6191 | else if (eol_seen & EOL_SEEN_CR) |
24a73b0a KH |
6192 | { |
6193 | coding->id = CODING_SYSTEM_ID (AREF (eol_type, 2)); | |
6194 | eol_type = Qmac; | |
6195 | } | |
6196 | return eol_type; | |
d46c5b12 | 6197 | } |
4ed46869 | 6198 | |
df7492f9 KH |
6199 | /* Detect how a text specified in CODING is encoded. If a coding |
6200 | system is detected, update fields of CODING by the detected coding | |
6201 | system. */ | |
0a28aafb | 6202 | |
74ab6df5 | 6203 | static void |
971de7fb | 6204 | detect_coding (struct coding_system *coding) |
d46c5b12 | 6205 | { |
8f924df7 | 6206 | const unsigned char *src, *src_end; |
f10fe38f | 6207 | unsigned int saved_mode = coding->mode; |
d46c5b12 | 6208 | |
df7492f9 KH |
6209 | coding->consumed = coding->consumed_char = 0; |
6210 | coding->produced = coding->produced_char = 0; | |
6211 | coding_set_source (coding); | |
1c3478b0 | 6212 | |
df7492f9 | 6213 | src_end = coding->source + coding->src_bytes; |
c0e16b14 | 6214 | coding->head_ascii = 0; |
1c3478b0 | 6215 | |
df7492f9 KH |
6216 | /* If we have not yet decided the text encoding type, detect it |
6217 | now. */ | |
6218 | if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding->id)), Qundecided)) | |
b73bfc1c | 6219 | { |
df7492f9 | 6220 | int c, i; |
6cb21a4f | 6221 | struct coding_detection_info detect_info; |
f10fe38f | 6222 | bool null_byte_found = 0, eight_bit_found = 0; |
df7492f9 | 6223 | |
6cb21a4f | 6224 | detect_info.checked = detect_info.found = detect_info.rejected = 0; |
2f3cbb32 | 6225 | for (src = coding->source; src < src_end; src++) |
d46c5b12 | 6226 | { |
df7492f9 | 6227 | c = *src; |
6cb21a4f | 6228 | if (c & 0x80) |
6cb21a4f | 6229 | { |
2f3cbb32 | 6230 | eight_bit_found = 1; |
2f3cbb32 KH |
6231 | if (null_byte_found) |
6232 | break; | |
6233 | } | |
6234 | else if (c < 0x20) | |
6235 | { | |
6236 | if ((c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) | |
6237 | && ! inhibit_iso_escape_detection | |
6238 | && ! detect_info.checked) | |
6cb21a4f | 6239 | { |
2f3cbb32 KH |
6240 | if (detect_coding_iso_2022 (coding, &detect_info)) |
6241 | { | |
6242 | /* We have scanned the whole data. */ | |
6243 | if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE)) | |
c0e16b14 KH |
6244 | { |
6245 | /* We didn't find an 8-bit code. We may | |
6246 | have found a null-byte, but it's very | |
ce5b453a | 6247 | rare that a binary file conforms to |
c0e16b14 KH |
6248 | ISO-2022. */ |
6249 | src = src_end; | |
6250 | coding->head_ascii = src - coding->source; | |
6251 | } | |
6252 | detect_info.rejected |= ~CATEGORY_MASK_ISO_ESCAPE; | |
2f3cbb32 KH |
6253 | break; |
6254 | } | |
6255 | } | |
97b1b294 | 6256 | else if (! c && !inhibit_null_byte_detection) |
2f3cbb32 KH |
6257 | { |
6258 | null_byte_found = 1; | |
6259 | if (eight_bit_found) | |
6260 | break; | |
6cb21a4f | 6261 | } |
c006c0c8 KH |
6262 | if (! eight_bit_found) |
6263 | coding->head_ascii++; | |
6cb21a4f | 6264 | } |
c006c0c8 | 6265 | else if (! eight_bit_found) |
c0e16b14 | 6266 | coding->head_ascii++; |
d46c5b12 | 6267 | } |
df7492f9 | 6268 | |
2f3cbb32 KH |
6269 | if (null_byte_found || eight_bit_found |
6270 | || coding->head_ascii < coding->src_bytes | |
6cb21a4f | 6271 | || detect_info.found) |
d46c5b12 | 6272 | { |
ff0dacd7 KH |
6273 | enum coding_category category; |
6274 | struct coding_system *this; | |
df7492f9 | 6275 | |
6cb21a4f KH |
6276 | if (coding->head_ascii == coding->src_bytes) |
6277 | /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */ | |
6278 | for (i = 0; i < coding_category_raw_text; i++) | |
6279 | { | |
6280 | category = coding_priorities[i]; | |
6281 | this = coding_categories + category; | |
6282 | if (detect_info.found & (1 << category)) | |
24a73b0a | 6283 | break; |
6cb21a4f KH |
6284 | } |
6285 | else | |
2f3cbb32 KH |
6286 | { |
6287 | if (null_byte_found) | |
ff0dacd7 | 6288 | { |
2f3cbb32 KH |
6289 | detect_info.checked |= ~CATEGORY_MASK_UTF_16; |
6290 | detect_info.rejected |= ~CATEGORY_MASK_UTF_16; | |
ff0dacd7 | 6291 | } |
2f3cbb32 KH |
6292 | for (i = 0; i < coding_category_raw_text; i++) |
6293 | { | |
6294 | category = coding_priorities[i]; | |
6295 | this = coding_categories + category; | |
0ba06a77 KH |
6296 | /* Some of this->detector (e.g. detect_coding_sjis) |
6297 | require this information. */ | |
6298 | coding->id = this->id; | |
2f3cbb32 KH |
6299 | if (this->id < 0) |
6300 | { | |
6301 | /* No coding system of this category is defined. */ | |
6302 | detect_info.rejected |= (1 << category); | |
6303 | } | |
6304 | else if (category >= coding_category_raw_text) | |
6305 | continue; | |
6306 | else if (detect_info.checked & (1 << category)) | |
6307 | { | |
6308 | if (detect_info.found & (1 << category)) | |
6309 | break; | |
6310 | } | |
6311 | else if ((*(this->detector)) (coding, &detect_info) | |
6312 | && detect_info.found & (1 << category)) | |
6313 | { | |
6314 | if (category == coding_category_utf_16_auto) | |
6315 | { | |
6316 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
6317 | category = coding_category_utf_16_le; | |
6318 | else | |
6319 | category = coding_category_utf_16_be; | |
6320 | } | |
6321 | break; | |
6322 | } | |
6323 | } | |
2f3cbb32 | 6324 | } |
c0e16b14 KH |
6325 | |
6326 | if (i < coding_category_raw_text) | |
6327 | setup_coding_system (CODING_ID_NAME (this->id), coding); | |
6328 | else if (null_byte_found) | |
6329 | setup_coding_system (Qno_conversion, coding); | |
6330 | else if ((detect_info.rejected & CATEGORY_MASK_ANY) | |
6331 | == CATEGORY_MASK_ANY) | |
6332 | setup_coding_system (Qraw_text, coding); | |
6333 | else if (detect_info.rejected) | |
6334 | for (i = 0; i < coding_category_raw_text; i++) | |
6335 | if (! (detect_info.rejected & (1 << coding_priorities[i]))) | |
6336 | { | |
6337 | this = coding_categories + coding_priorities[i]; | |
6338 | setup_coding_system (CODING_ID_NAME (this->id), coding); | |
6339 | break; | |
6340 | } | |
d46c5b12 | 6341 | } |
b73bfc1c | 6342 | } |
a470d443 KH |
6343 | else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding->id))) |
6344 | == coding_category_utf_8_auto) | |
6345 | { | |
6346 | Lisp_Object coding_systems; | |
6347 | struct coding_detection_info detect_info; | |
6348 | ||
6349 | coding_systems | |
6350 | = AREF (CODING_ID_ATTRS (coding->id), coding_attr_utf_bom); | |
6351 | detect_info.found = detect_info.rejected = 0; | |
6352 | coding->head_ascii = 0; | |
6353 | if (CONSP (coding_systems) | |
6354 | && detect_coding_utf_8 (coding, &detect_info)) | |
6355 | { | |
6356 | if (detect_info.found & CATEGORY_MASK_UTF_8_SIG) | |
6357 | setup_coding_system (XCAR (coding_systems), coding); | |
6358 | else | |
6359 | setup_coding_system (XCDR (coding_systems), coding); | |
6360 | } | |
6361 | } | |
24a73b0a KH |
6362 | else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding->id))) |
6363 | == coding_category_utf_16_auto) | |
b49a1807 KH |
6364 | { |
6365 | Lisp_Object coding_systems; | |
6366 | struct coding_detection_info detect_info; | |
6367 | ||
6368 | coding_systems | |
a470d443 | 6369 | = AREF (CODING_ID_ATTRS (coding->id), coding_attr_utf_bom); |
b49a1807 | 6370 | detect_info.found = detect_info.rejected = 0; |
a470d443 | 6371 | coding->head_ascii = 0; |
b49a1807 | 6372 | if (CONSP (coding_systems) |
24a73b0a | 6373 | && detect_coding_utf_16 (coding, &detect_info)) |
b49a1807 KH |
6374 | { |
6375 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
6376 | setup_coding_system (XCAR (coding_systems), coding); | |
24a73b0a | 6377 | else if (detect_info.found & CATEGORY_MASK_UTF_16_BE) |
b49a1807 KH |
6378 | setup_coding_system (XCDR (coding_systems), coding); |
6379 | } | |
6380 | } | |
73cce38d | 6381 | coding->mode = saved_mode; |
4ed46869 | 6382 | } |
4ed46869 | 6383 | |
d46c5b12 | 6384 | |
aaaf0b1e | 6385 | static void |
971de7fb | 6386 | decode_eol (struct coding_system *coding) |
aaaf0b1e | 6387 | { |
24a73b0a KH |
6388 | Lisp_Object eol_type; |
6389 | unsigned char *p, *pbeg, *pend; | |
3ed051d4 | 6390 | |
24a73b0a | 6391 | eol_type = CODING_ID_EOL_TYPE (coding->id); |
0a9564cb | 6392 | if (EQ (eol_type, Qunix) || inhibit_eol_conversion) |
24a73b0a KH |
6393 | return; |
6394 | ||
6395 | if (NILP (coding->dst_object)) | |
6396 | pbeg = coding->destination; | |
6397 | else | |
6398 | pbeg = BYTE_POS_ADDR (coding->dst_pos_byte); | |
6399 | pend = pbeg + coding->produced; | |
6400 | ||
6401 | if (VECTORP (eol_type)) | |
aaaf0b1e | 6402 | { |
df7492f9 | 6403 | int eol_seen = EOL_SEEN_NONE; |
4ed46869 | 6404 | |
24a73b0a | 6405 | for (p = pbeg; p < pend; p++) |
aaaf0b1e | 6406 | { |
df7492f9 KH |
6407 | if (*p == '\n') |
6408 | eol_seen |= EOL_SEEN_LF; | |
6409 | else if (*p == '\r') | |
aaaf0b1e | 6410 | { |
df7492f9 | 6411 | if (p + 1 < pend && *(p + 1) == '\n') |
aaaf0b1e | 6412 | { |
df7492f9 KH |
6413 | eol_seen |= EOL_SEEN_CRLF; |
6414 | p++; | |
aaaf0b1e | 6415 | } |
aaaf0b1e | 6416 | else |
df7492f9 | 6417 | eol_seen |= EOL_SEEN_CR; |
aaaf0b1e | 6418 | } |
aaaf0b1e | 6419 | } |
75f4f1ac EZ |
6420 | /* Handle DOS-style EOLs in a file with stray ^M characters. */ |
6421 | if ((eol_seen & EOL_SEEN_CRLF) != 0 | |
6422 | && (eol_seen & EOL_SEEN_CR) != 0 | |
6423 | && (eol_seen & EOL_SEEN_LF) == 0) | |
6424 | eol_seen = EOL_SEEN_CRLF; | |
6425 | else if (eol_seen != EOL_SEEN_NONE | |
24a73b0a KH |
6426 | && eol_seen != EOL_SEEN_LF |
6427 | && eol_seen != EOL_SEEN_CRLF | |
6428 | && eol_seen != EOL_SEEN_CR) | |
6429 | eol_seen = EOL_SEEN_LF; | |
df7492f9 | 6430 | if (eol_seen != EOL_SEEN_NONE) |
24a73b0a | 6431 | eol_type = adjust_coding_eol_type (coding, eol_seen); |
aaaf0b1e | 6432 | } |
d46c5b12 | 6433 | |
24a73b0a | 6434 | if (EQ (eol_type, Qmac)) |
27901516 | 6435 | { |
24a73b0a | 6436 | for (p = pbeg; p < pend; p++) |
df7492f9 KH |
6437 | if (*p == '\r') |
6438 | *p = '\n'; | |
4ed46869 | 6439 | } |
24a73b0a | 6440 | else if (EQ (eol_type, Qdos)) |
df7492f9 | 6441 | { |
d311d28c | 6442 | ptrdiff_t n = 0; |
b73bfc1c | 6443 | |
24a73b0a KH |
6444 | if (NILP (coding->dst_object)) |
6445 | { | |
4347441b KH |
6446 | /* Start deleting '\r' from the tail to minimize the memory |
6447 | movement. */ | |
24a73b0a KH |
6448 | for (p = pend - 2; p >= pbeg; p--) |
6449 | if (*p == '\r') | |
6450 | { | |
72af86bd | 6451 | memmove (p, p + 1, pend-- - p - 1); |
24a73b0a KH |
6452 | n++; |
6453 | } | |
6454 | } | |
6455 | else | |
6456 | { | |
d311d28c PE |
6457 | ptrdiff_t pos_byte = coding->dst_pos_byte; |
6458 | ptrdiff_t pos = coding->dst_pos; | |
6459 | ptrdiff_t pos_end = pos + coding->produced_char - 1; | |
4347441b KH |
6460 | |
6461 | while (pos < pos_end) | |
6462 | { | |
6463 | p = BYTE_POS_ADDR (pos_byte); | |
6464 | if (*p == '\r' && p[1] == '\n') | |
6465 | { | |
6466 | del_range_2 (pos, pos_byte, pos + 1, pos_byte + 1, 0); | |
6467 | n++; | |
6468 | pos_end--; | |
6469 | } | |
6470 | pos++; | |
69b8522d KH |
6471 | if (coding->dst_multibyte) |
6472 | pos_byte += BYTES_BY_CHAR_HEAD (*p); | |
6473 | else | |
6474 | pos_byte++; | |
4347441b | 6475 | } |
24a73b0a KH |
6476 | } |
6477 | coding->produced -= n; | |
6478 | coding->produced_char -= n; | |
aaaf0b1e | 6479 | } |
4ed46869 KH |
6480 | } |
6481 | ||
7d64c6ad | 6482 | |
a6f87d34 | 6483 | /* Return a translation table (or list of them) from coding system |
f10fe38f PE |
6484 | attribute vector ATTRS for encoding (if ENCODEP) or decoding (if |
6485 | not ENCODEP). */ | |
7d64c6ad | 6486 | |
e6a54062 | 6487 | static Lisp_Object |
f10fe38f | 6488 | get_translation_table (Lisp_Object attrs, bool encodep, int *max_lookup) |
7d64c6ad KH |
6489 | { |
6490 | Lisp_Object standard, translation_table; | |
09ee6fdd | 6491 | Lisp_Object val; |
7d64c6ad | 6492 | |
4bed5909 CY |
6493 | if (NILP (Venable_character_translation)) |
6494 | { | |
6495 | if (max_lookup) | |
6496 | *max_lookup = 0; | |
6497 | return Qnil; | |
6498 | } | |
7d64c6ad KH |
6499 | if (encodep) |
6500 | translation_table = CODING_ATTR_ENCODE_TBL (attrs), | |
6501 | standard = Vstandard_translation_table_for_encode; | |
6502 | else | |
6503 | translation_table = CODING_ATTR_DECODE_TBL (attrs), | |
6504 | standard = Vstandard_translation_table_for_decode; | |
7d64c6ad | 6505 | if (NILP (translation_table)) |
09ee6fdd KH |
6506 | translation_table = standard; |
6507 | else | |
a6f87d34 | 6508 | { |
09ee6fdd KH |
6509 | if (SYMBOLP (translation_table)) |
6510 | translation_table = Fget (translation_table, Qtranslation_table); | |
6511 | else if (CONSP (translation_table)) | |
6512 | { | |
6513 | translation_table = Fcopy_sequence (translation_table); | |
6514 | for (val = translation_table; CONSP (val); val = XCDR (val)) | |
6515 | if (SYMBOLP (XCAR (val))) | |
6516 | XSETCAR (val, Fget (XCAR (val), Qtranslation_table)); | |
6517 | } | |
6518 | if (CHAR_TABLE_P (standard)) | |
6519 | { | |
6520 | if (CONSP (translation_table)) | |
6521 | translation_table = nconc2 (translation_table, | |
6522 | Fcons (standard, Qnil)); | |
6523 | else | |
6524 | translation_table = Fcons (translation_table, | |
6525 | Fcons (standard, Qnil)); | |
6526 | } | |
a6f87d34 | 6527 | } |
2170c8f0 KH |
6528 | |
6529 | if (max_lookup) | |
09ee6fdd | 6530 | { |
2170c8f0 KH |
6531 | *max_lookup = 1; |
6532 | if (CHAR_TABLE_P (translation_table) | |
6533 | && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table)) > 1) | |
6534 | { | |
6535 | val = XCHAR_TABLE (translation_table)->extras[1]; | |
6536 | if (NATNUMP (val) && *max_lookup < XFASTINT (val)) | |
6537 | *max_lookup = XFASTINT (val); | |
6538 | } | |
6539 | else if (CONSP (translation_table)) | |
6540 | { | |
2735d060 | 6541 | Lisp_Object tail; |
09ee6fdd | 6542 | |
2170c8f0 KH |
6543 | for (tail = translation_table; CONSP (tail); tail = XCDR (tail)) |
6544 | if (CHAR_TABLE_P (XCAR (tail)) | |
6545 | && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail))) > 1) | |
6546 | { | |
2735d060 PE |
6547 | Lisp_Object tailval = XCHAR_TABLE (XCAR (tail))->extras[1]; |
6548 | if (NATNUMP (tailval) && *max_lookup < XFASTINT (tailval)) | |
6549 | *max_lookup = XFASTINT (tailval); | |
2170c8f0 KH |
6550 | } |
6551 | } | |
a6f87d34 | 6552 | } |
7d64c6ad KH |
6553 | return translation_table; |
6554 | } | |
6555 | ||
09ee6fdd KH |
6556 | #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \ |
6557 | do { \ | |
6558 | trans = Qnil; \ | |
6559 | if (CHAR_TABLE_P (table)) \ | |
6560 | { \ | |
6561 | trans = CHAR_TABLE_REF (table, c); \ | |
6562 | if (CHARACTERP (trans)) \ | |
6563 | c = XFASTINT (trans), trans = Qnil; \ | |
6564 | } \ | |
6565 | else if (CONSP (table)) \ | |
6566 | { \ | |
6567 | Lisp_Object tail; \ | |
6568 | \ | |
6569 | for (tail = table; CONSP (tail); tail = XCDR (tail)) \ | |
6570 | if (CHAR_TABLE_P (XCAR (tail))) \ | |
6571 | { \ | |
6572 | trans = CHAR_TABLE_REF (XCAR (tail), c); \ | |
6573 | if (CHARACTERP (trans)) \ | |
6574 | c = XFASTINT (trans), trans = Qnil; \ | |
6575 | else if (! NILP (trans)) \ | |
6576 | break; \ | |
6577 | } \ | |
6578 | } \ | |
e6a54062 KH |
6579 | } while (0) |
6580 | ||
7d64c6ad | 6581 | |
e951386e KH |
6582 | /* Return a translation of character(s) at BUF according to TRANS. |
6583 | TRANS is TO-CHAR or ((FROM . TO) ...) where | |
6584 | FROM = [FROM-CHAR ...], TO is TO-CHAR or [TO-CHAR ...]. | |
6585 | The return value is TO-CHAR or ([FROM-CHAR ...] . TO) if a | |
6586 | translation is found, and Qnil if not found.. | |
6587 | If BUF is too short to lookup characters in FROM, return Qt. */ | |
6588 | ||
69a80ea3 | 6589 | static Lisp_Object |
971de7fb | 6590 | get_translation (Lisp_Object trans, int *buf, int *buf_end) |
69a80ea3 | 6591 | { |
e951386e KH |
6592 | |
6593 | if (INTEGERP (trans)) | |
6594 | return trans; | |
6595 | for (; CONSP (trans); trans = XCDR (trans)) | |
69a80ea3 | 6596 | { |
e951386e KH |
6597 | Lisp_Object val = XCAR (trans); |
6598 | Lisp_Object from = XCAR (val); | |
2c6a9faa PE |
6599 | ptrdiff_t len = ASIZE (from); |
6600 | ptrdiff_t i; | |
69a80ea3 | 6601 | |
e951386e | 6602 | for (i = 0; i < len; i++) |
69a80ea3 | 6603 | { |
e951386e KH |
6604 | if (buf + i == buf_end) |
6605 | return Qt; | |
6606 | if (XINT (AREF (from, i)) != buf[i]) | |
6607 | break; | |
69a80ea3 | 6608 | } |
e951386e KH |
6609 | if (i == len) |
6610 | return val; | |
69a80ea3 | 6611 | } |
e951386e | 6612 | return Qnil; |
69a80ea3 KH |
6613 | } |
6614 | ||
6615 | ||
d46c5b12 | 6616 | static int |
cf84bb53 | 6617 | produce_chars (struct coding_system *coding, Lisp_Object translation_table, |
f10fe38f | 6618 | bool last_block) |
4ed46869 | 6619 | { |
df7492f9 KH |
6620 | unsigned char *dst = coding->destination + coding->produced; |
6621 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
d311d28c PE |
6622 | ptrdiff_t produced; |
6623 | ptrdiff_t produced_chars = 0; | |
69a80ea3 | 6624 | int carryover = 0; |
4ed46869 | 6625 | |
df7492f9 | 6626 | if (! coding->chars_at_source) |
4ed46869 | 6627 | { |
119852e7 | 6628 | /* Source characters are in coding->charbuf. */ |
fba4576f AS |
6629 | int *buf = coding->charbuf; |
6630 | int *buf_end = buf + coding->charbuf_used; | |
4ed46869 | 6631 | |
db274c7a KH |
6632 | if (EQ (coding->src_object, coding->dst_object)) |
6633 | { | |
6634 | coding_set_source (coding); | |
6635 | dst_end = ((unsigned char *) coding->source) + coding->consumed; | |
6636 | } | |
4ed46869 | 6637 | |
df7492f9 | 6638 | while (buf < buf_end) |
4ed46869 | 6639 | { |
27bb1ca4 PE |
6640 | int c = *buf; |
6641 | ptrdiff_t i; | |
bc4bc72a | 6642 | |
df7492f9 KH |
6643 | if (c >= 0) |
6644 | { | |
d311d28c | 6645 | ptrdiff_t from_nchars = 1, to_nchars = 1; |
69a80ea3 KH |
6646 | Lisp_Object trans = Qnil; |
6647 | ||
09ee6fdd | 6648 | LOOKUP_TRANSLATION_TABLE (translation_table, c, trans); |
e6a54062 | 6649 | if (! NILP (trans)) |
69a80ea3 | 6650 | { |
e951386e KH |
6651 | trans = get_translation (trans, buf, buf_end); |
6652 | if (INTEGERP (trans)) | |
6653 | c = XINT (trans); | |
6654 | else if (CONSP (trans)) | |
6655 | { | |
6656 | from_nchars = ASIZE (XCAR (trans)); | |
6657 | trans = XCDR (trans); | |
6658 | if (INTEGERP (trans)) | |
6659 | c = XINT (trans); | |
6660 | else | |
6661 | { | |
6662 | to_nchars = ASIZE (trans); | |
6663 | c = XINT (AREF (trans, 0)); | |
6664 | } | |
6665 | } | |
6666 | else if (EQ (trans, Qt) && ! last_block) | |
69a80ea3 | 6667 | break; |
69a80ea3 KH |
6668 | } |
6669 | ||
5d009b3a | 6670 | if ((dst_end - dst) / MAX_MULTIBYTE_LENGTH < to_nchars) |
69a80ea3 | 6671 | { |
5d009b3a PE |
6672 | if (((min (PTRDIFF_MAX, SIZE_MAX) - (buf_end - buf)) |
6673 | / MAX_MULTIBYTE_LENGTH) | |
6674 | < to_nchars) | |
6675 | memory_full (SIZE_MAX); | |
69a80ea3 KH |
6676 | dst = alloc_destination (coding, |
6677 | buf_end - buf | |
6678 | + MAX_MULTIBYTE_LENGTH * to_nchars, | |
6679 | dst); | |
db274c7a KH |
6680 | if (EQ (coding->src_object, coding->dst_object)) |
6681 | { | |
6682 | coding_set_source (coding); | |
e951386e KH |
6683 | dst_end = (((unsigned char *) coding->source) |
6684 | + coding->consumed); | |
db274c7a KH |
6685 | } |
6686 | else | |
6687 | dst_end = coding->destination + coding->dst_bytes; | |
69a80ea3 KH |
6688 | } |
6689 | ||
433f7f87 | 6690 | for (i = 0; i < to_nchars; i++) |
69a80ea3 | 6691 | { |
433f7f87 KH |
6692 | if (i > 0) |
6693 | c = XINT (AREF (trans, i)); | |
69a80ea3 KH |
6694 | if (coding->dst_multibyte |
6695 | || ! CHAR_BYTE8_P (c)) | |
db274c7a | 6696 | CHAR_STRING_ADVANCE_NO_UNIFY (c, dst); |
69a80ea3 KH |
6697 | else |
6698 | *dst++ = CHAR_TO_BYTE8 (c); | |
6699 | } | |
6700 | produced_chars += to_nchars; | |
e951386e | 6701 | buf += from_nchars; |
d46c5b12 | 6702 | } |
df7492f9 | 6703 | else |
69a80ea3 KH |
6704 | /* This is an annotation datum. (-C) is the length. */ |
6705 | buf += -c; | |
4ed46869 | 6706 | } |
69a80ea3 | 6707 | carryover = buf_end - buf; |
4ed46869 | 6708 | } |
fa42c37f | 6709 | else |
fa42c37f | 6710 | { |
119852e7 | 6711 | /* Source characters are at coding->source. */ |
8f924df7 | 6712 | const unsigned char *src = coding->source; |
119852e7 | 6713 | const unsigned char *src_end = src + coding->consumed; |
4ed46869 | 6714 | |
db274c7a KH |
6715 | if (EQ (coding->dst_object, coding->src_object)) |
6716 | dst_end = (unsigned char *) src; | |
df7492f9 | 6717 | if (coding->src_multibyte != coding->dst_multibyte) |
fa42c37f | 6718 | { |
df7492f9 | 6719 | if (coding->src_multibyte) |
fa42c37f | 6720 | { |
f10fe38f | 6721 | bool multibytep = 1; |
d311d28c | 6722 | ptrdiff_t consumed_chars = 0; |
d46c5b12 | 6723 | |
df7492f9 KH |
6724 | while (1) |
6725 | { | |
8f924df7 | 6726 | const unsigned char *src_base = src; |
df7492f9 | 6727 | int c; |
b73bfc1c | 6728 | |
df7492f9 | 6729 | ONE_MORE_BYTE (c); |
119852e7 | 6730 | if (dst == dst_end) |
df7492f9 | 6731 | { |
119852e7 KH |
6732 | if (EQ (coding->src_object, coding->dst_object)) |
6733 | dst_end = (unsigned char *) src; | |
6734 | if (dst == dst_end) | |
df7492f9 | 6735 | { |
d311d28c | 6736 | ptrdiff_t offset = src - coding->source; |
119852e7 KH |
6737 | |
6738 | dst = alloc_destination (coding, src_end - src + 1, | |
6739 | dst); | |
6740 | dst_end = coding->destination + coding->dst_bytes; | |
6741 | coding_set_source (coding); | |
6742 | src = coding->source + offset; | |
5c1ca13d | 6743 | src_end = coding->source + coding->consumed; |
db274c7a KH |
6744 | if (EQ (coding->src_object, coding->dst_object)) |
6745 | dst_end = (unsigned char *) src; | |
df7492f9 | 6746 | } |
df7492f9 KH |
6747 | } |
6748 | *dst++ = c; | |
6749 | produced_chars++; | |
6750 | } | |
6751 | no_more_source: | |
6752 | ; | |
fa42c37f KH |
6753 | } |
6754 | else | |
df7492f9 KH |
6755 | while (src < src_end) |
6756 | { | |
f10fe38f | 6757 | bool multibytep = 1; |
df7492f9 | 6758 | int c = *src++; |
b73bfc1c | 6759 | |
df7492f9 KH |
6760 | if (dst >= dst_end - 1) |
6761 | { | |
2c78b7e1 | 6762 | if (EQ (coding->src_object, coding->dst_object)) |
8f924df7 | 6763 | dst_end = (unsigned char *) src; |
2c78b7e1 KH |
6764 | if (dst >= dst_end - 1) |
6765 | { | |
d311d28c PE |
6766 | ptrdiff_t offset = src - coding->source; |
6767 | ptrdiff_t more_bytes; | |
119852e7 | 6768 | |
db274c7a KH |
6769 | if (EQ (coding->src_object, coding->dst_object)) |
6770 | more_bytes = ((src_end - src) / 2) + 2; | |
6771 | else | |
6772 | more_bytes = src_end - src + 2; | |
6773 | dst = alloc_destination (coding, more_bytes, dst); | |
2c78b7e1 KH |
6774 | dst_end = coding->destination + coding->dst_bytes; |
6775 | coding_set_source (coding); | |
119852e7 | 6776 | src = coding->source + offset; |
5c1ca13d | 6777 | src_end = coding->source + coding->consumed; |
db274c7a KH |
6778 | if (EQ (coding->src_object, coding->dst_object)) |
6779 | dst_end = (unsigned char *) src; | |
2c78b7e1 | 6780 | } |
df7492f9 KH |
6781 | } |
6782 | EMIT_ONE_BYTE (c); | |
6783 | } | |
d46c5b12 | 6784 | } |
df7492f9 KH |
6785 | else |
6786 | { | |
6787 | if (!EQ (coding->src_object, coding->dst_object)) | |
fa42c37f | 6788 | { |
d311d28c | 6789 | ptrdiff_t require = coding->src_bytes - coding->dst_bytes; |
4ed46869 | 6790 | |
df7492f9 | 6791 | if (require > 0) |
fa42c37f | 6792 | { |
d311d28c | 6793 | ptrdiff_t offset = src - coding->source; |
df7492f9 KH |
6794 | |
6795 | dst = alloc_destination (coding, require, dst); | |
6796 | coding_set_source (coding); | |
6797 | src = coding->source + offset; | |
5c1ca13d | 6798 | src_end = coding->source + coding->consumed; |
fa42c37f KH |
6799 | } |
6800 | } | |
119852e7 | 6801 | produced_chars = coding->consumed_char; |
df7492f9 | 6802 | while (src < src_end) |
14daee73 | 6803 | *dst++ = *src++; |
fa42c37f KH |
6804 | } |
6805 | } | |
6806 | ||
df7492f9 | 6807 | produced = dst - (coding->destination + coding->produced); |
284201e4 | 6808 | if (BUFFERP (coding->dst_object) && produced_chars > 0) |
df7492f9 KH |
6809 | insert_from_gap (produced_chars, produced); |
6810 | coding->produced += produced; | |
6811 | coding->produced_char += produced_chars; | |
69a80ea3 | 6812 | return carryover; |
fa42c37f KH |
6813 | } |
6814 | ||
ff0dacd7 KH |
6815 | /* Compose text in CODING->object according to the annotation data at |
6816 | CHARBUF. CHARBUF is an array: | |
e951386e | 6817 | [ -LENGTH ANNOTATION_MASK NCHARS NBYTES METHOD [ COMPONENTS... ] ] |
df7492f9 | 6818 | */ |
4ed46869 | 6819 | |
b0ab8123 | 6820 | static void |
d311d28c | 6821 | produce_composition (struct coding_system *coding, int *charbuf, ptrdiff_t pos) |
4ed46869 | 6822 | { |
df7492f9 | 6823 | int len; |
d311d28c | 6824 | ptrdiff_t to; |
df7492f9 | 6825 | enum composition_method method; |
df7492f9 | 6826 | Lisp_Object components; |
fa42c37f | 6827 | |
e951386e | 6828 | len = -charbuf[0] - MAX_ANNOTATION_LENGTH; |
69a80ea3 | 6829 | to = pos + charbuf[2]; |
e951386e | 6830 | method = (enum composition_method) (charbuf[4]); |
d46c5b12 | 6831 | |
df7492f9 KH |
6832 | if (method == COMPOSITION_RELATIVE) |
6833 | components = Qnil; | |
e951386e | 6834 | else |
d46c5b12 | 6835 | { |
df7492f9 | 6836 | Lisp_Object args[MAX_COMPOSITION_COMPONENTS * 2 - 1]; |
e951386e | 6837 | int i, j; |
b73bfc1c | 6838 | |
e951386e KH |
6839 | if (method == COMPOSITION_WITH_RULE) |
6840 | len = charbuf[2] * 3 - 2; | |
6841 | charbuf += MAX_ANNOTATION_LENGTH; | |
6842 | /* charbuf = [ CHRA ... CHAR] or [ CHAR -2 RULE ... CHAR ] */ | |
6843 | for (i = j = 0; i < len && charbuf[i] != -1; i++, j++) | |
9ffd559c | 6844 | { |
e951386e KH |
6845 | if (charbuf[i] >= 0) |
6846 | args[j] = make_number (charbuf[i]); | |
6847 | else | |
6848 | { | |
6849 | i++; | |
6850 | args[j] = make_number (charbuf[i] % 0x100); | |
6851 | } | |
9ffd559c | 6852 | } |
e951386e | 6853 | components = (i == j ? Fstring (j, args) : Fvector (j, args)); |
d46c5b12 | 6854 | } |
69a80ea3 | 6855 | compose_text (pos, to, components, Qnil, coding->dst_object); |
d46c5b12 KH |
6856 | } |
6857 | ||
d46c5b12 | 6858 | |
ff0dacd7 KH |
6859 | /* Put `charset' property on text in CODING->object according to |
6860 | the annotation data at CHARBUF. CHARBUF is an array: | |
69a80ea3 | 6861 | [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ] |
ff0dacd7 | 6862 | */ |
d46c5b12 | 6863 | |
b0ab8123 | 6864 | static void |
d311d28c | 6865 | produce_charset (struct coding_system *coding, int *charbuf, ptrdiff_t pos) |
d46c5b12 | 6866 | { |
d311d28c | 6867 | ptrdiff_t from = pos - charbuf[2]; |
69a80ea3 | 6868 | struct charset *charset = CHARSET_FROM_ID (charbuf[3]); |
b73bfc1c | 6869 | |
69a80ea3 | 6870 | Fput_text_property (make_number (from), make_number (pos), |
ff0dacd7 KH |
6871 | Qcharset, CHARSET_NAME (charset), |
6872 | coding->dst_object); | |
d46c5b12 KH |
6873 | } |
6874 | ||
d46c5b12 | 6875 | |
df7492f9 KH |
6876 | #define CHARBUF_SIZE 0x4000 |
6877 | ||
6878 | #define ALLOC_CONVERSION_WORK_AREA(coding) \ | |
6879 | do { \ | |
1af1a51a DA |
6880 | coding->charbuf = SAFE_ALLOCA (CHARBUF_SIZE * sizeof (int)); \ |
6881 | coding->charbuf_size = CHARBUF_SIZE; \ | |
df7492f9 | 6882 | } while (0) |
4ed46869 | 6883 | |
d46c5b12 KH |
6884 | |
6885 | static void | |
d311d28c | 6886 | produce_annotation (struct coding_system *coding, ptrdiff_t pos) |
d46c5b12 | 6887 | { |
df7492f9 KH |
6888 | int *charbuf = coding->charbuf; |
6889 | int *charbuf_end = charbuf + coding->charbuf_used; | |
d46c5b12 | 6890 | |
ff0dacd7 KH |
6891 | if (NILP (coding->dst_object)) |
6892 | return; | |
d46c5b12 | 6893 | |
df7492f9 | 6894 | while (charbuf < charbuf_end) |
a84f1519 | 6895 | { |
df7492f9 | 6896 | if (*charbuf >= 0) |
e951386e | 6897 | pos++, charbuf++; |
d46c5b12 | 6898 | else |
d46c5b12 | 6899 | { |
df7492f9 | 6900 | int len = -*charbuf; |
e951386e KH |
6901 | |
6902 | if (len > 2) | |
6903 | switch (charbuf[1]) | |
6904 | { | |
6905 | case CODING_ANNOTATE_COMPOSITION_MASK: | |
6906 | produce_composition (coding, charbuf, pos); | |
6907 | break; | |
6908 | case CODING_ANNOTATE_CHARSET_MASK: | |
6909 | produce_charset (coding, charbuf, pos); | |
6910 | break; | |
6911 | } | |
df7492f9 | 6912 | charbuf += len; |
d46c5b12 | 6913 | } |
a84f1519 | 6914 | } |
d46c5b12 KH |
6915 | } |
6916 | ||
df7492f9 KH |
6917 | /* Decode the data at CODING->src_object into CODING->dst_object. |
6918 | CODING->src_object is a buffer, a string, or nil. | |
6919 | CODING->dst_object is a buffer. | |
d46c5b12 | 6920 | |
df7492f9 KH |
6921 | If CODING->src_object is a buffer, it must be the current buffer. |
6922 | In this case, if CODING->src_pos is positive, it is a position of | |
6923 | the source text in the buffer, otherwise, the source text is in the | |
6924 | gap area of the buffer, and CODING->src_pos specifies the offset of | |
6925 | the text from GPT (which must be the same as PT). If this is the | |
6926 | same buffer as CODING->dst_object, CODING->src_pos must be | |
6927 | negative. | |
d46c5b12 | 6928 | |
b6828792 | 6929 | If CODING->src_object is a string, CODING->src_pos is an index to |
df7492f9 | 6930 | that string. |
d46c5b12 | 6931 | |
df7492f9 KH |
6932 | If CODING->src_object is nil, CODING->source must already point to |
6933 | the non-relocatable memory area. In this case, CODING->src_pos is | |
6934 | an offset from CODING->source. | |
73be902c | 6935 | |
df7492f9 KH |
6936 | The decoded data is inserted at the current point of the buffer |
6937 | CODING->dst_object. | |
6938 | */ | |
d46c5b12 | 6939 | |
f10fe38f | 6940 | static void |
971de7fb | 6941 | decode_coding (struct coding_system *coding) |
d46c5b12 | 6942 | { |
df7492f9 | 6943 | Lisp_Object attrs; |
24a73b0a | 6944 | Lisp_Object undo_list; |
7d64c6ad | 6945 | Lisp_Object translation_table; |
d0396581 | 6946 | struct ccl_spec cclspec; |
69a80ea3 KH |
6947 | int carryover; |
6948 | int i; | |
d46c5b12 | 6949 | |
1af1a51a DA |
6950 | USE_SAFE_ALLOCA; |
6951 | ||
df7492f9 KH |
6952 | if (BUFFERP (coding->src_object) |
6953 | && coding->src_pos > 0 | |
6954 | && coding->src_pos < GPT | |
6955 | && coding->src_pos + coding->src_chars > GPT) | |
6956 | move_gap_both (coding->src_pos, coding->src_pos_byte); | |
d46c5b12 | 6957 | |
24a73b0a | 6958 | undo_list = Qt; |
df7492f9 | 6959 | if (BUFFERP (coding->dst_object)) |
1c3478b0 | 6960 | { |
a3d794a1 | 6961 | set_buffer_internal (XBUFFER (coding->dst_object)); |
df7492f9 KH |
6962 | if (GPT != PT) |
6963 | move_gap_both (PT, PT_BYTE); | |
f48b82fd GR |
6964 | |
6965 | /* We must disable undo_list in order to record the whole insert | |
6966 | transaction via record_insert at the end. But doing so also | |
6967 | disables the recording of the first change to the undo_list. | |
6968 | Therefore we check for first change here and record it via | |
6969 | record_first_change if needed. */ | |
6970 | if (MODIFF <= SAVE_MODIFF) | |
6971 | record_first_change (); | |
6972 | ||
4b4deea2 | 6973 | undo_list = BVAR (current_buffer, undo_list); |
39eb03f1 | 6974 | bset_undo_list (current_buffer, Qt); |
1c3478b0 KH |
6975 | } |
6976 | ||
df7492f9 KH |
6977 | coding->consumed = coding->consumed_char = 0; |
6978 | coding->produced = coding->produced_char = 0; | |
6979 | coding->chars_at_source = 0; | |
065e3595 | 6980 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 | 6981 | coding->errors = 0; |
1c3478b0 | 6982 | |
df7492f9 KH |
6983 | ALLOC_CONVERSION_WORK_AREA (coding); |
6984 | ||
6985 | attrs = CODING_ID_ATTRS (coding->id); | |
2170c8f0 | 6986 | translation_table = get_translation_table (attrs, 0, NULL); |
df7492f9 | 6987 | |
69a80ea3 | 6988 | carryover = 0; |
d0396581 KH |
6989 | if (coding->decoder == decode_coding_ccl) |
6990 | { | |
6991 | coding->spec.ccl = &cclspec; | |
6992 | setup_ccl_program (&cclspec.ccl, CODING_CCL_DECODER (coding)); | |
6993 | } | |
df7492f9 | 6994 | do |
b73bfc1c | 6995 | { |
d311d28c | 6996 | ptrdiff_t pos = coding->dst_pos + coding->produced_char; |
69a80ea3 | 6997 | |
df7492f9 KH |
6998 | coding_set_source (coding); |
6999 | coding->annotated = 0; | |
69a80ea3 | 7000 | coding->charbuf_used = carryover; |
df7492f9 | 7001 | (*(coding->decoder)) (coding); |
df7492f9 | 7002 | coding_set_destination (coding); |
69a80ea3 | 7003 | carryover = produce_chars (coding, translation_table, 0); |
df7492f9 | 7004 | if (coding->annotated) |
69a80ea3 KH |
7005 | produce_annotation (coding, pos); |
7006 | for (i = 0; i < carryover; i++) | |
7007 | coding->charbuf[i] | |
7008 | = coding->charbuf[coding->charbuf_used - carryover + i]; | |
d46c5b12 | 7009 | } |
d0396581 KH |
7010 | while (coding->result == CODING_RESULT_INSUFFICIENT_DST |
7011 | || (coding->consumed < coding->src_bytes | |
7012 | && (coding->result == CODING_RESULT_SUCCESS | |
7013 | || coding->result == CODING_RESULT_INVALID_SRC))); | |
d46c5b12 | 7014 | |
69a80ea3 KH |
7015 | if (carryover > 0) |
7016 | { | |
7017 | coding_set_destination (coding); | |
7018 | coding->charbuf_used = carryover; | |
7019 | produce_chars (coding, translation_table, 1); | |
7020 | } | |
7021 | ||
df7492f9 KH |
7022 | coding->carryover_bytes = 0; |
7023 | if (coding->consumed < coding->src_bytes) | |
d46c5b12 | 7024 | { |
df7492f9 | 7025 | int nbytes = coding->src_bytes - coding->consumed; |
8f924df7 | 7026 | const unsigned char *src; |
df7492f9 KH |
7027 | |
7028 | coding_set_source (coding); | |
7029 | coding_set_destination (coding); | |
7030 | src = coding->source + coding->consumed; | |
7031 | ||
7032 | if (coding->mode & CODING_MODE_LAST_BLOCK) | |
1c3478b0 | 7033 | { |
df7492f9 KH |
7034 | /* Flush out unprocessed data as binary chars. We are sure |
7035 | that the number of data is less than the size of | |
7036 | coding->charbuf. */ | |
065e3595 | 7037 | coding->charbuf_used = 0; |
b2dab6c8 JR |
7038 | coding->chars_at_source = 0; |
7039 | ||
df7492f9 | 7040 | while (nbytes-- > 0) |
1c3478b0 | 7041 | { |
df7492f9 | 7042 | int c = *src++; |
98725083 | 7043 | |
1c91457d KH |
7044 | if (c & 0x80) |
7045 | c = BYTE8_TO_CHAR (c); | |
7046 | coding->charbuf[coding->charbuf_used++] = c; | |
1c3478b0 | 7047 | } |
f6cbaf43 | 7048 | produce_chars (coding, Qnil, 1); |
d46c5b12 | 7049 | } |
d46c5b12 | 7050 | else |
df7492f9 KH |
7051 | { |
7052 | /* Record unprocessed bytes in coding->carryover. We are | |
7053 | sure that the number of data is less than the size of | |
7054 | coding->carryover. */ | |
7055 | unsigned char *p = coding->carryover; | |
7056 | ||
f289d375 KH |
7057 | if (nbytes > sizeof coding->carryover) |
7058 | nbytes = sizeof coding->carryover; | |
df7492f9 KH |
7059 | coding->carryover_bytes = nbytes; |
7060 | while (nbytes-- > 0) | |
7061 | *p++ = *src++; | |
1c3478b0 | 7062 | } |
df7492f9 | 7063 | coding->consumed = coding->src_bytes; |
b73bfc1c | 7064 | } |
69f76525 | 7065 | |
0a9564cb EZ |
7066 | if (! EQ (CODING_ID_EOL_TYPE (coding->id), Qunix) |
7067 | && !inhibit_eol_conversion) | |
4347441b | 7068 | decode_eol (coding); |
24a73b0a KH |
7069 | if (BUFFERP (coding->dst_object)) |
7070 | { | |
39eb03f1 | 7071 | bset_undo_list (current_buffer, undo_list); |
24a73b0a KH |
7072 | record_insert (coding->dst_pos, coding->produced_char); |
7073 | } | |
1af1a51a DA |
7074 | |
7075 | SAFE_FREE (); | |
4ed46869 KH |
7076 | } |
7077 | ||
aaaf0b1e | 7078 | |
e1c23804 | 7079 | /* Extract an annotation datum from a composition starting at POS and |
ff0dacd7 KH |
7080 | ending before LIMIT of CODING->src_object (buffer or string), store |
7081 | the data in BUF, set *STOP to a starting position of the next | |
7082 | composition (if any) or to LIMIT, and return the address of the | |
7083 | next element of BUF. | |
7084 | ||
7085 | If such an annotation is not found, set *STOP to a starting | |
7086 | position of a composition after POS (if any) or to LIMIT, and | |
7087 | return BUF. */ | |
7088 | ||
b0ab8123 | 7089 | static int * |
d311d28c | 7090 | handle_composition_annotation (ptrdiff_t pos, ptrdiff_t limit, |
cf84bb53 | 7091 | struct coding_system *coding, int *buf, |
d311d28c | 7092 | ptrdiff_t *stop) |
aaaf0b1e | 7093 | { |
d311d28c | 7094 | ptrdiff_t start, end; |
ff0dacd7 | 7095 | Lisp_Object prop; |
aaaf0b1e | 7096 | |
ff0dacd7 KH |
7097 | if (! find_composition (pos, limit, &start, &end, &prop, coding->src_object) |
7098 | || end > limit) | |
7099 | *stop = limit; | |
7100 | else if (start > pos) | |
7101 | *stop = start; | |
7102 | else | |
aaaf0b1e | 7103 | { |
ff0dacd7 | 7104 | if (start == pos) |
aaaf0b1e | 7105 | { |
ff0dacd7 KH |
7106 | /* We found a composition. Store the corresponding |
7107 | annotation data in BUF. */ | |
7108 | int *head = buf; | |
7109 | enum composition_method method = COMPOSITION_METHOD (prop); | |
7110 | int nchars = COMPOSITION_LENGTH (prop); | |
7111 | ||
e951386e | 7112 | ADD_COMPOSITION_DATA (buf, nchars, 0, method); |
ff0dacd7 | 7113 | if (method != COMPOSITION_RELATIVE) |
aaaf0b1e | 7114 | { |
ff0dacd7 | 7115 | Lisp_Object components; |
2c6a9faa | 7116 | ptrdiff_t i, len, i_byte; |
ff0dacd7 KH |
7117 | |
7118 | components = COMPOSITION_COMPONENTS (prop); | |
7119 | if (VECTORP (components)) | |
aaaf0b1e | 7120 | { |
77b37c05 | 7121 | len = ASIZE (components); |
ff0dacd7 KH |
7122 | for (i = 0; i < len; i++) |
7123 | *buf++ = XINT (AREF (components, i)); | |
aaaf0b1e | 7124 | } |
ff0dacd7 | 7125 | else if (STRINGP (components)) |
aaaf0b1e | 7126 | { |
8f924df7 | 7127 | len = SCHARS (components); |
ff0dacd7 KH |
7128 | i = i_byte = 0; |
7129 | while (i < len) | |
7130 | { | |
7131 | FETCH_STRING_CHAR_ADVANCE (*buf, components, i, i_byte); | |
7132 | buf++; | |
7133 | } | |
7134 | } | |
7135 | else if (INTEGERP (components)) | |
7136 | { | |
7137 | len = 1; | |
7138 | *buf++ = XINT (components); | |
7139 | } | |
7140 | else if (CONSP (components)) | |
7141 | { | |
7142 | for (len = 0; CONSP (components); | |
7143 | len++, components = XCDR (components)) | |
7144 | *buf++ = XINT (XCAR (components)); | |
aaaf0b1e | 7145 | } |
aaaf0b1e | 7146 | else |
1088b922 | 7147 | emacs_abort (); |
ff0dacd7 | 7148 | *head -= len; |
aaaf0b1e | 7149 | } |
aaaf0b1e | 7150 | } |
ff0dacd7 KH |
7151 | |
7152 | if (find_composition (end, limit, &start, &end, &prop, | |
7153 | coding->src_object) | |
7154 | && end <= limit) | |
7155 | *stop = start; | |
7156 | else | |
7157 | *stop = limit; | |
aaaf0b1e | 7158 | } |
ff0dacd7 KH |
7159 | return buf; |
7160 | } | |
7161 | ||
7162 | ||
e1c23804 | 7163 | /* Extract an annotation datum from a text property `charset' at POS of |
ff0dacd7 KH |
7164 | CODING->src_object (buffer of string), store the data in BUF, set |
7165 | *STOP to the position where the value of `charset' property changes | |
7166 | (limiting by LIMIT), and return the address of the next element of | |
7167 | BUF. | |
7168 | ||
7169 | If the property value is nil, set *STOP to the position where the | |
7170 | property value is non-nil (limiting by LIMIT), and return BUF. */ | |
7171 | ||
b0ab8123 | 7172 | static int * |
d311d28c | 7173 | handle_charset_annotation (ptrdiff_t pos, ptrdiff_t limit, |
cf84bb53 | 7174 | struct coding_system *coding, int *buf, |
d311d28c | 7175 | ptrdiff_t *stop) |
ff0dacd7 KH |
7176 | { |
7177 | Lisp_Object val, next; | |
7178 | int id; | |
7179 | ||
7180 | val = Fget_text_property (make_number (pos), Qcharset, coding->src_object); | |
7181 | if (! NILP (val) && CHARSETP (val)) | |
7182 | id = XINT (CHARSET_SYMBOL_ID (val)); | |
7183 | else | |
7184 | id = -1; | |
69a80ea3 | 7185 | ADD_CHARSET_DATA (buf, 0, id); |
ff0dacd7 KH |
7186 | next = Fnext_single_property_change (make_number (pos), Qcharset, |
7187 | coding->src_object, | |
7188 | make_number (limit)); | |
7189 | *stop = XINT (next); | |
7190 | return buf; | |
7191 | } | |
7192 | ||
7193 | ||
df7492f9 | 7194 | static void |
cf84bb53 JB |
7195 | consume_chars (struct coding_system *coding, Lisp_Object translation_table, |
7196 | int max_lookup) | |
df7492f9 KH |
7197 | { |
7198 | int *buf = coding->charbuf; | |
ff0dacd7 | 7199 | int *buf_end = coding->charbuf + coding->charbuf_size; |
7c78e542 | 7200 | const unsigned char *src = coding->source + coding->consumed; |
4776e638 | 7201 | const unsigned char *src_end = coding->source + coding->src_bytes; |
d311d28c PE |
7202 | ptrdiff_t pos = coding->src_pos + coding->consumed_char; |
7203 | ptrdiff_t end_pos = coding->src_pos + coding->src_chars; | |
f10fe38f | 7204 | bool multibytep = coding->src_multibyte; |
df7492f9 KH |
7205 | Lisp_Object eol_type; |
7206 | int c; | |
d311d28c | 7207 | ptrdiff_t stop, stop_composition, stop_charset; |
09ee6fdd | 7208 | int *lookup_buf = NULL; |
433f7f87 KH |
7209 | |
7210 | if (! NILP (translation_table)) | |
09ee6fdd | 7211 | lookup_buf = alloca (sizeof (int) * max_lookup); |
88993dfd | 7212 | |
0a9564cb | 7213 | eol_type = inhibit_eol_conversion ? Qunix : CODING_ID_EOL_TYPE (coding->id); |
df7492f9 KH |
7214 | if (VECTORP (eol_type)) |
7215 | eol_type = Qunix; | |
88993dfd | 7216 | |
df7492f9 KH |
7217 | /* Note: composition handling is not yet implemented. */ |
7218 | coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK; | |
ec6d2bb8 | 7219 | |
0b5670c9 KH |
7220 | if (NILP (coding->src_object)) |
7221 | stop = stop_composition = stop_charset = end_pos; | |
ff0dacd7 | 7222 | else |
0b5670c9 KH |
7223 | { |
7224 | if (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK) | |
7225 | stop = stop_composition = pos; | |
7226 | else | |
7227 | stop = stop_composition = end_pos; | |
7228 | if (coding->common_flags & CODING_ANNOTATE_CHARSET_MASK) | |
7229 | stop = stop_charset = pos; | |
7230 | else | |
7231 | stop_charset = end_pos; | |
7232 | } | |
ec6d2bb8 | 7233 | |
24a73b0a | 7234 | /* Compensate for CRLF and conversion. */ |
ff0dacd7 | 7235 | buf_end -= 1 + MAX_ANNOTATION_LENGTH; |
df7492f9 | 7236 | while (buf < buf_end) |
aaaf0b1e | 7237 | { |
433f7f87 KH |
7238 | Lisp_Object trans; |
7239 | ||
df7492f9 | 7240 | if (pos == stop) |
ec6d2bb8 | 7241 | { |
df7492f9 KH |
7242 | if (pos == end_pos) |
7243 | break; | |
ff0dacd7 KH |
7244 | if (pos == stop_composition) |
7245 | buf = handle_composition_annotation (pos, end_pos, coding, | |
7246 | buf, &stop_composition); | |
7247 | if (pos == stop_charset) | |
7248 | buf = handle_charset_annotation (pos, end_pos, coding, | |
7249 | buf, &stop_charset); | |
7250 | stop = (stop_composition < stop_charset | |
7251 | ? stop_composition : stop_charset); | |
df7492f9 KH |
7252 | } |
7253 | ||
7254 | if (! multibytep) | |
4776e638 | 7255 | { |
d311d28c | 7256 | int bytes; |
aaaf0b1e | 7257 | |
4d1e6632 KH |
7258 | if (coding->encoder == encode_coding_raw_text |
7259 | || coding->encoder == encode_coding_ccl) | |
ea29edf2 KH |
7260 | c = *src++, pos++; |
7261 | else if ((bytes = MULTIBYTE_LENGTH (src, src_end)) > 0) | |
db274c7a | 7262 | c = STRING_CHAR_ADVANCE_NO_UNIFY (src), pos += bytes; |
4776e638 | 7263 | else |
f03caae0 | 7264 | c = BYTE8_TO_CHAR (*src), src++, pos++; |
4776e638 | 7265 | } |
df7492f9 | 7266 | else |
db274c7a | 7267 | c = STRING_CHAR_ADVANCE_NO_UNIFY (src), pos++; |
df7492f9 KH |
7268 | if ((c == '\r') && (coding->mode & CODING_MODE_SELECTIVE_DISPLAY)) |
7269 | c = '\n'; | |
7270 | if (! EQ (eol_type, Qunix)) | |
aaaf0b1e | 7271 | { |
df7492f9 | 7272 | if (c == '\n') |
aaaf0b1e | 7273 | { |
df7492f9 KH |
7274 | if (EQ (eol_type, Qdos)) |
7275 | *buf++ = '\r'; | |
7276 | else | |
7277 | c = '\r'; | |
aaaf0b1e KH |
7278 | } |
7279 | } | |
433f7f87 | 7280 | |
e6a54062 | 7281 | trans = Qnil; |
09ee6fdd | 7282 | LOOKUP_TRANSLATION_TABLE (translation_table, c, trans); |
e6a54062 | 7283 | if (NILP (trans)) |
433f7f87 KH |
7284 | *buf++ = c; |
7285 | else | |
7286 | { | |
2c6a9faa | 7287 | ptrdiff_t from_nchars = 1, to_nchars = 1; |
433f7f87 KH |
7288 | int *lookup_buf_end; |
7289 | const unsigned char *p = src; | |
7290 | int i; | |
7291 | ||
7292 | lookup_buf[0] = c; | |
7293 | for (i = 1; i < max_lookup && p < src_end; i++) | |
7294 | lookup_buf[i] = STRING_CHAR_ADVANCE (p); | |
7295 | lookup_buf_end = lookup_buf + i; | |
e951386e KH |
7296 | trans = get_translation (trans, lookup_buf, lookup_buf_end); |
7297 | if (INTEGERP (trans)) | |
7298 | c = XINT (trans); | |
7299 | else if (CONSP (trans)) | |
7300 | { | |
7301 | from_nchars = ASIZE (XCAR (trans)); | |
7302 | trans = XCDR (trans); | |
7303 | if (INTEGERP (trans)) | |
7304 | c = XINT (trans); | |
7305 | else | |
7306 | { | |
7307 | to_nchars = ASIZE (trans); | |
2c6a9faa | 7308 | if (buf_end - buf < to_nchars) |
e951386e KH |
7309 | break; |
7310 | c = XINT (AREF (trans, 0)); | |
7311 | } | |
7312 | } | |
7313 | else | |
433f7f87 | 7314 | break; |
e951386e | 7315 | *buf++ = c; |
433f7f87 KH |
7316 | for (i = 1; i < to_nchars; i++) |
7317 | *buf++ = XINT (AREF (trans, i)); | |
7318 | for (i = 1; i < from_nchars; i++, pos++) | |
7319 | src += MULTIBYTE_LENGTH_NO_CHECK (src); | |
7320 | } | |
aaaf0b1e | 7321 | } |
ec6d2bb8 | 7322 | |
df7492f9 KH |
7323 | coding->consumed = src - coding->source; |
7324 | coding->consumed_char = pos - coding->src_pos; | |
7325 | coding->charbuf_used = buf - coding->charbuf; | |
7326 | coding->chars_at_source = 0; | |
aaaf0b1e KH |
7327 | } |
7328 | ||
4ed46869 | 7329 | |
df7492f9 KH |
7330 | /* Encode the text at CODING->src_object into CODING->dst_object. |
7331 | CODING->src_object is a buffer or a string. | |
7332 | CODING->dst_object is a buffer or nil. | |
7333 | ||
7334 | If CODING->src_object is a buffer, it must be the current buffer. | |
7335 | In this case, if CODING->src_pos is positive, it is a position of | |
7336 | the source text in the buffer, otherwise. the source text is in the | |
7337 | gap area of the buffer, and coding->src_pos specifies the offset of | |
7338 | the text from GPT (which must be the same as PT). If this is the | |
7339 | same buffer as CODING->dst_object, CODING->src_pos must be | |
7340 | negative and CODING should not have `pre-write-conversion'. | |
7341 | ||
7342 | If CODING->src_object is a string, CODING should not have | |
7343 | `pre-write-conversion'. | |
7344 | ||
7345 | If CODING->dst_object is a buffer, the encoded data is inserted at | |
7346 | the current point of that buffer. | |
7347 | ||
7348 | If CODING->dst_object is nil, the encoded data is placed at the | |
7349 | memory area specified by CODING->destination. */ | |
7350 | ||
f10fe38f | 7351 | static void |
971de7fb | 7352 | encode_coding (struct coding_system *coding) |
4ed46869 | 7353 | { |
df7492f9 | 7354 | Lisp_Object attrs; |
7d64c6ad | 7355 | Lisp_Object translation_table; |
09ee6fdd | 7356 | int max_lookup; |
fb608df3 | 7357 | struct ccl_spec cclspec; |
9861e777 | 7358 | |
1af1a51a DA |
7359 | USE_SAFE_ALLOCA; |
7360 | ||
df7492f9 | 7361 | attrs = CODING_ID_ATTRS (coding->id); |
ea29edf2 KH |
7362 | if (coding->encoder == encode_coding_raw_text) |
7363 | translation_table = Qnil, max_lookup = 0; | |
7364 | else | |
7365 | translation_table = get_translation_table (attrs, 1, &max_lookup); | |
4ed46869 | 7366 | |
df7492f9 | 7367 | if (BUFFERP (coding->dst_object)) |
8844fa83 | 7368 | { |
df7492f9 KH |
7369 | set_buffer_internal (XBUFFER (coding->dst_object)); |
7370 | coding->dst_multibyte | |
4b4deea2 | 7371 | = ! NILP (BVAR (current_buffer, enable_multibyte_characters)); |
8844fa83 | 7372 | } |
4ed46869 | 7373 | |
b73bfc1c | 7374 | coding->consumed = coding->consumed_char = 0; |
df7492f9 | 7375 | coding->produced = coding->produced_char = 0; |
065e3595 | 7376 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
b73bfc1c | 7377 | coding->errors = 0; |
b73bfc1c | 7378 | |
df7492f9 | 7379 | ALLOC_CONVERSION_WORK_AREA (coding); |
4ed46869 | 7380 | |
fb608df3 KH |
7381 | if (coding->encoder == encode_coding_ccl) |
7382 | { | |
7383 | coding->spec.ccl = &cclspec; | |
7384 | setup_ccl_program (&cclspec.ccl, CODING_CCL_ENCODER (coding)); | |
7385 | } | |
df7492f9 KH |
7386 | do { |
7387 | coding_set_source (coding); | |
09ee6fdd | 7388 | consume_chars (coding, translation_table, max_lookup); |
df7492f9 KH |
7389 | coding_set_destination (coding); |
7390 | (*(coding->encoder)) (coding); | |
7391 | } while (coding->consumed_char < coding->src_chars); | |
7392 | ||
284201e4 | 7393 | if (BUFFERP (coding->dst_object) && coding->produced_char > 0) |
df7492f9 | 7394 | insert_from_gap (coding->produced_char, coding->produced); |
1af1a51a DA |
7395 | |
7396 | SAFE_FREE (); | |
ec6d2bb8 KH |
7397 | } |
7398 | ||
fb88bf2d | 7399 | |
24a73b0a KH |
7400 | /* Name (or base name) of work buffer for code conversion. */ |
7401 | static Lisp_Object Vcode_conversion_workbuf_name; | |
d46c5b12 | 7402 | |
24a73b0a KH |
7403 | /* A working buffer used by the top level conversion. Once it is |
7404 | created, it is never destroyed. It has the name | |
7405 | Vcode_conversion_workbuf_name. The other working buffers are | |
7406 | destroyed after the use is finished, and their names are modified | |
7407 | versions of Vcode_conversion_workbuf_name. */ | |
7408 | static Lisp_Object Vcode_conversion_reused_workbuf; | |
b73bfc1c | 7409 | |
f10fe38f PE |
7410 | /* True iff Vcode_conversion_reused_workbuf is already in use. */ |
7411 | static bool reused_workbuf_in_use; | |
4ed46869 | 7412 | |
24a73b0a | 7413 | |
ad1746f5 | 7414 | /* Return a working buffer of code conversion. MULTIBYTE specifies the |
24a73b0a | 7415 | multibyteness of returning buffer. */ |
b73bfc1c | 7416 | |
f6cbaf43 | 7417 | static Lisp_Object |
f10fe38f | 7418 | make_conversion_work_buffer (bool multibyte) |
df7492f9 | 7419 | { |
24a73b0a KH |
7420 | Lisp_Object name, workbuf; |
7421 | struct buffer *current; | |
4ed46869 | 7422 | |
f10fe38f | 7423 | if (reused_workbuf_in_use) |
065e3595 KH |
7424 | { |
7425 | name = Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name, Qnil); | |
7426 | workbuf = Fget_buffer_create (name); | |
7427 | } | |
df7492f9 | 7428 | else |
065e3595 | 7429 | { |
f10fe38f | 7430 | reused_workbuf_in_use = 1; |
159bd5a2 | 7431 | if (NILP (Fbuffer_live_p (Vcode_conversion_reused_workbuf))) |
a993c7a1 KH |
7432 | Vcode_conversion_reused_workbuf |
7433 | = Fget_buffer_create (Vcode_conversion_workbuf_name); | |
7434 | workbuf = Vcode_conversion_reused_workbuf; | |
065e3595 | 7435 | } |
24a73b0a KH |
7436 | current = current_buffer; |
7437 | set_buffer_internal (XBUFFER (workbuf)); | |
df36ff1f CY |
7438 | /* We can't allow modification hooks to run in the work buffer. For |
7439 | instance, directory_files_internal assumes that file decoding | |
7440 | doesn't compile new regexps. */ | |
7441 | Fset (Fmake_local_variable (Qinhibit_modification_hooks), Qt); | |
3ed051d4 | 7442 | Ferase_buffer (); |
39eb03f1 PE |
7443 | bset_undo_list (current_buffer, Qt); |
7444 | bset_enable_multibyte_characters (current_buffer, multibyte ? Qt : Qnil); | |
df7492f9 | 7445 | set_buffer_internal (current); |
24a73b0a | 7446 | return workbuf; |
df7492f9 | 7447 | } |
d46c5b12 | 7448 | |
24a73b0a | 7449 | |
4776e638 | 7450 | static Lisp_Object |
971de7fb | 7451 | code_conversion_restore (Lisp_Object arg) |
4776e638 | 7452 | { |
24a73b0a | 7453 | Lisp_Object current, workbuf; |
948bdcf3 | 7454 | struct gcpro gcpro1; |
24a73b0a | 7455 | |
948bdcf3 | 7456 | GCPRO1 (arg); |
24a73b0a KH |
7457 | current = XCAR (arg); |
7458 | workbuf = XCDR (arg); | |
7459 | if (! NILP (workbuf)) | |
7460 | { | |
7461 | if (EQ (workbuf, Vcode_conversion_reused_workbuf)) | |
7462 | reused_workbuf_in_use = 0; | |
d17337e5 | 7463 | else |
24a73b0a KH |
7464 | Fkill_buffer (workbuf); |
7465 | } | |
7466 | set_buffer_internal (XBUFFER (current)); | |
948bdcf3 | 7467 | UNGCPRO; |
4776e638 KH |
7468 | return Qnil; |
7469 | } | |
b73bfc1c | 7470 | |
24a73b0a | 7471 | Lisp_Object |
f10fe38f | 7472 | code_conversion_save (bool with_work_buf, bool multibyte) |
df7492f9 | 7473 | { |
24a73b0a | 7474 | Lisp_Object workbuf = Qnil; |
b73bfc1c | 7475 | |
4776e638 | 7476 | if (with_work_buf) |
24a73b0a KH |
7477 | workbuf = make_conversion_work_buffer (multibyte); |
7478 | record_unwind_protect (code_conversion_restore, | |
7479 | Fcons (Fcurrent_buffer (), workbuf)); | |
4776e638 | 7480 | return workbuf; |
df7492f9 | 7481 | } |
d46c5b12 | 7482 | |
f10fe38f | 7483 | void |
cf84bb53 | 7484 | decode_coding_gap (struct coding_system *coding, |
d311d28c | 7485 | ptrdiff_t chars, ptrdiff_t bytes) |
df7492f9 | 7486 | { |
d311d28c | 7487 | ptrdiff_t count = SPECPDL_INDEX (); |
5e5c78be | 7488 | Lisp_Object attrs; |
fb88bf2d | 7489 | |
24a73b0a | 7490 | code_conversion_save (0, 0); |
ec6d2bb8 | 7491 | |
24a73b0a | 7492 | coding->src_object = Fcurrent_buffer (); |
df7492f9 KH |
7493 | coding->src_chars = chars; |
7494 | coding->src_bytes = bytes; | |
7495 | coding->src_pos = -chars; | |
7496 | coding->src_pos_byte = -bytes; | |
7497 | coding->src_multibyte = chars < bytes; | |
24a73b0a | 7498 | coding->dst_object = coding->src_object; |
df7492f9 KH |
7499 | coding->dst_pos = PT; |
7500 | coding->dst_pos_byte = PT_BYTE; | |
4b4deea2 | 7501 | coding->dst_multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters)); |
4ed46869 | 7502 | |
df7492f9 KH |
7503 | if (CODING_REQUIRE_DETECTION (coding)) |
7504 | detect_coding (coding); | |
8f924df7 | 7505 | |
9286b333 | 7506 | coding->mode |= CODING_MODE_LAST_BLOCK; |
287c57d7 | 7507 | current_buffer->text->inhibit_shrinking = 1; |
df7492f9 | 7508 | decode_coding (coding); |
287c57d7 | 7509 | current_buffer->text->inhibit_shrinking = 0; |
d46c5b12 | 7510 | |
5e5c78be KH |
7511 | attrs = CODING_ID_ATTRS (coding->id); |
7512 | if (! NILP (CODING_ATTR_POST_READ (attrs))) | |
b73bfc1c | 7513 | { |
d311d28c | 7514 | ptrdiff_t prev_Z = Z, prev_Z_BYTE = Z_BYTE; |
5e5c78be KH |
7515 | Lisp_Object val; |
7516 | ||
7517 | TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte); | |
5e5c78be KH |
7518 | val = call1 (CODING_ATTR_POST_READ (attrs), |
7519 | make_number (coding->produced_char)); | |
5e5c78be KH |
7520 | CHECK_NATNUM (val); |
7521 | coding->produced_char += Z - prev_Z; | |
7522 | coding->produced += Z_BYTE - prev_Z_BYTE; | |
b73bfc1c | 7523 | } |
4ed46869 | 7524 | |
df7492f9 | 7525 | unbind_to (count, Qnil); |
b73bfc1c | 7526 | } |
52d41803 | 7527 | |
d46c5b12 | 7528 | |
df7492f9 KH |
7529 | /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in |
7530 | SRC_OBJECT into DST_OBJECT by coding context CODING. | |
b73bfc1c | 7531 | |
df7492f9 | 7532 | SRC_OBJECT is a buffer, a string, or Qnil. |
b73bfc1c | 7533 | |
df7492f9 KH |
7534 | If it is a buffer, the text is at point of the buffer. FROM and TO |
7535 | are positions in the buffer. | |
b73bfc1c | 7536 | |
df7492f9 KH |
7537 | If it is a string, the text is at the beginning of the string. |
7538 | FROM and TO are indices to the string. | |
4ed46869 | 7539 | |
df7492f9 KH |
7540 | If it is nil, the text is at coding->source. FROM and TO are |
7541 | indices to coding->source. | |
bb10be8b | 7542 | |
df7492f9 | 7543 | DST_OBJECT is a buffer, Qt, or Qnil. |
4ed46869 | 7544 | |
df7492f9 KH |
7545 | If it is a buffer, the decoded text is inserted at point of the |
7546 | buffer. If the buffer is the same as SRC_OBJECT, the source text | |
7547 | is deleted. | |
4ed46869 | 7548 | |
df7492f9 KH |
7549 | If it is Qt, a string is made from the decoded text, and |
7550 | set in CODING->dst_object. | |
d46c5b12 | 7551 | |
df7492f9 | 7552 | If it is Qnil, the decoded text is stored at CODING->destination. |
2cb26057 | 7553 | The caller must allocate CODING->dst_bytes bytes at |
df7492f9 KH |
7554 | CODING->destination by xmalloc. If the decoded text is longer than |
7555 | CODING->dst_bytes, CODING->destination is relocated by xrealloc. | |
7556 | */ | |
d46c5b12 | 7557 | |
df7492f9 | 7558 | void |
cf84bb53 JB |
7559 | decode_coding_object (struct coding_system *coding, |
7560 | Lisp_Object src_object, | |
d311d28c PE |
7561 | ptrdiff_t from, ptrdiff_t from_byte, |
7562 | ptrdiff_t to, ptrdiff_t to_byte, | |
cf84bb53 | 7563 | Lisp_Object dst_object) |
d46c5b12 | 7564 | { |
d311d28c | 7565 | ptrdiff_t count = SPECPDL_INDEX (); |
c4a63b12 | 7566 | unsigned char *destination IF_LINT (= NULL); |
d311d28c PE |
7567 | ptrdiff_t dst_bytes IF_LINT (= 0); |
7568 | ptrdiff_t chars = to - from; | |
7569 | ptrdiff_t bytes = to_byte - from_byte; | |
df7492f9 | 7570 | Lisp_Object attrs; |
f10fe38f PE |
7571 | ptrdiff_t saved_pt = -1, saved_pt_byte IF_LINT (= 0); |
7572 | bool need_marker_adjustment = 0; | |
b3bfad50 | 7573 | Lisp_Object old_deactivate_mark; |
d46c5b12 | 7574 | |
b3bfad50 | 7575 | old_deactivate_mark = Vdeactivate_mark; |
93dec019 | 7576 | |
df7492f9 | 7577 | if (NILP (dst_object)) |
d46c5b12 | 7578 | { |
df7492f9 KH |
7579 | destination = coding->destination; |
7580 | dst_bytes = coding->dst_bytes; | |
d46c5b12 | 7581 | } |
93dec019 | 7582 | |
df7492f9 KH |
7583 | coding->src_object = src_object; |
7584 | coding->src_chars = chars; | |
7585 | coding->src_bytes = bytes; | |
7586 | coding->src_multibyte = chars < bytes; | |
70ad9fc4 | 7587 | |
df7492f9 | 7588 | if (STRINGP (src_object)) |
d46c5b12 | 7589 | { |
df7492f9 KH |
7590 | coding->src_pos = from; |
7591 | coding->src_pos_byte = from_byte; | |
d46c5b12 | 7592 | } |
df7492f9 | 7593 | else if (BUFFERP (src_object)) |
88993dfd | 7594 | { |
df7492f9 KH |
7595 | set_buffer_internal (XBUFFER (src_object)); |
7596 | if (from != GPT) | |
7597 | move_gap_both (from, from_byte); | |
7598 | if (EQ (src_object, dst_object)) | |
fb88bf2d | 7599 | { |
64cedb0c KH |
7600 | struct Lisp_Marker *tail; |
7601 | ||
7602 | for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next) | |
7603 | { | |
7604 | tail->need_adjustment | |
7605 | = tail->charpos == (tail->insertion_type ? from : to); | |
7606 | need_marker_adjustment |= tail->need_adjustment; | |
7607 | } | |
4776e638 | 7608 | saved_pt = PT, saved_pt_byte = PT_BYTE; |
df7492f9 | 7609 | TEMP_SET_PT_BOTH (from, from_byte); |
f4a3cc44 | 7610 | current_buffer->text->inhibit_shrinking = 1; |
df7492f9 KH |
7611 | del_range_both (from, from_byte, to, to_byte, 1); |
7612 | coding->src_pos = -chars; | |
7613 | coding->src_pos_byte = -bytes; | |
fb88bf2d | 7614 | } |
df7492f9 | 7615 | else |
fb88bf2d | 7616 | { |
df7492f9 KH |
7617 | coding->src_pos = from; |
7618 | coding->src_pos_byte = from_byte; | |
fb88bf2d | 7619 | } |
88993dfd KH |
7620 | } |
7621 | ||
df7492f9 KH |
7622 | if (CODING_REQUIRE_DETECTION (coding)) |
7623 | detect_coding (coding); | |
7624 | attrs = CODING_ID_ATTRS (coding->id); | |
d46c5b12 | 7625 | |
2cb26057 KH |
7626 | if (EQ (dst_object, Qt) |
7627 | || (! NILP (CODING_ATTR_POST_READ (attrs)) | |
7628 | && NILP (dst_object))) | |
b73bfc1c | 7629 | { |
a1567c45 SM |
7630 | coding->dst_multibyte = !CODING_FOR_UNIBYTE (coding); |
7631 | coding->dst_object = code_conversion_save (1, coding->dst_multibyte); | |
df7492f9 KH |
7632 | coding->dst_pos = BEG; |
7633 | coding->dst_pos_byte = BEG_BYTE; | |
b73bfc1c | 7634 | } |
df7492f9 | 7635 | else if (BUFFERP (dst_object)) |
d46c5b12 | 7636 | { |
24a73b0a | 7637 | code_conversion_save (0, 0); |
df7492f9 KH |
7638 | coding->dst_object = dst_object; |
7639 | coding->dst_pos = BUF_PT (XBUFFER (dst_object)); | |
7640 | coding->dst_pos_byte = BUF_PT_BYTE (XBUFFER (dst_object)); | |
7641 | coding->dst_multibyte | |
4b4deea2 | 7642 | = ! NILP (BVAR (XBUFFER (dst_object), enable_multibyte_characters)); |
d46c5b12 KH |
7643 | } |
7644 | else | |
7645 | { | |
24a73b0a | 7646 | code_conversion_save (0, 0); |
df7492f9 | 7647 | coding->dst_object = Qnil; |
0154725e SM |
7648 | /* Most callers presume this will return a multibyte result, and they |
7649 | won't use `binary' or `raw-text' anyway, so let's not worry about | |
7650 | CODING_FOR_UNIBYTE. */ | |
bb555731 | 7651 | coding->dst_multibyte = 1; |
d46c5b12 KH |
7652 | } |
7653 | ||
df7492f9 | 7654 | decode_coding (coding); |
fa46990e | 7655 | |
df7492f9 KH |
7656 | if (BUFFERP (coding->dst_object)) |
7657 | set_buffer_internal (XBUFFER (coding->dst_object)); | |
d46c5b12 | 7658 | |
df7492f9 | 7659 | if (! NILP (CODING_ATTR_POST_READ (attrs))) |
d46c5b12 | 7660 | { |
b3bfad50 | 7661 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; |
d311d28c | 7662 | ptrdiff_t prev_Z = Z, prev_Z_BYTE = Z_BYTE; |
2b4f9037 | 7663 | Lisp_Object val; |
d46c5b12 | 7664 | |
c0cc7f7f | 7665 | TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte); |
b3bfad50 KH |
7666 | GCPRO5 (coding->src_object, coding->dst_object, src_object, dst_object, |
7667 | old_deactivate_mark); | |
d4850d67 KH |
7668 | val = safe_call1 (CODING_ATTR_POST_READ (attrs), |
7669 | make_number (coding->produced_char)); | |
df7492f9 KH |
7670 | UNGCPRO; |
7671 | CHECK_NATNUM (val); | |
7672 | coding->produced_char += Z - prev_Z; | |
7673 | coding->produced += Z_BYTE - prev_Z_BYTE; | |
d46c5b12 | 7674 | } |
de79a6a5 | 7675 | |
df7492f9 | 7676 | if (EQ (dst_object, Qt)) |
ec6d2bb8 | 7677 | { |
df7492f9 KH |
7678 | coding->dst_object = Fbuffer_string (); |
7679 | } | |
7680 | else if (NILP (dst_object) && BUFFERP (coding->dst_object)) | |
7681 | { | |
7682 | set_buffer_internal (XBUFFER (coding->dst_object)); | |
7683 | if (dst_bytes < coding->produced) | |
7684 | { | |
1af1a51a | 7685 | eassert (coding->produced > 0); |
b3bfad50 | 7686 | destination = xrealloc (destination, coding->produced); |
df7492f9 KH |
7687 | if (BEGV < GPT && GPT < BEGV + coding->produced_char) |
7688 | move_gap_both (BEGV, BEGV_BYTE); | |
72af86bd | 7689 | memcpy (destination, BEGV_ADDR, coding->produced); |
df7492f9 | 7690 | coding->destination = destination; |
d46c5b12 | 7691 | } |
ec6d2bb8 | 7692 | } |
b73bfc1c | 7693 | |
4776e638 KH |
7694 | if (saved_pt >= 0) |
7695 | { | |
7696 | /* This is the case of: | |
7697 | (BUFFERP (src_object) && EQ (src_object, dst_object)) | |
7698 | As we have moved PT while replacing the original buffer | |
7699 | contents, we must recover it now. */ | |
7700 | set_buffer_internal (XBUFFER (src_object)); | |
f4a3cc44 | 7701 | current_buffer->text->inhibit_shrinking = 0; |
4776e638 KH |
7702 | if (saved_pt < from) |
7703 | TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte); | |
7704 | else if (saved_pt < from + chars) | |
7705 | TEMP_SET_PT_BOTH (from, from_byte); | |
4b4deea2 | 7706 | else if (! NILP (BVAR (current_buffer, enable_multibyte_characters))) |
4776e638 KH |
7707 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars), |
7708 | saved_pt_byte + (coding->produced - bytes)); | |
7709 | else | |
7710 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes), | |
7711 | saved_pt_byte + (coding->produced - bytes)); | |
64cedb0c KH |
7712 | |
7713 | if (need_marker_adjustment) | |
7714 | { | |
7715 | struct Lisp_Marker *tail; | |
7716 | ||
7717 | for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next) | |
7718 | if (tail->need_adjustment) | |
7719 | { | |
7720 | tail->need_adjustment = 0; | |
7721 | if (tail->insertion_type) | |
7722 | { | |
7723 | tail->bytepos = from_byte; | |
7724 | tail->charpos = from; | |
7725 | } | |
7726 | else | |
7727 | { | |
7728 | tail->bytepos = from_byte + coding->produced; | |
7729 | tail->charpos | |
4b4deea2 | 7730 | = (NILP (BVAR (current_buffer, enable_multibyte_characters)) |
64cedb0c KH |
7731 | ? tail->bytepos : from + coding->produced_char); |
7732 | } | |
7733 | } | |
7734 | } | |
d46c5b12 | 7735 | } |
4776e638 | 7736 | |
b3bfad50 | 7737 | Vdeactivate_mark = old_deactivate_mark; |
065e3595 | 7738 | unbind_to (count, coding->dst_object); |
d46c5b12 KH |
7739 | } |
7740 | ||
d46c5b12 | 7741 | |
df7492f9 | 7742 | void |
cf84bb53 JB |
7743 | encode_coding_object (struct coding_system *coding, |
7744 | Lisp_Object src_object, | |
d311d28c PE |
7745 | ptrdiff_t from, ptrdiff_t from_byte, |
7746 | ptrdiff_t to, ptrdiff_t to_byte, | |
cf84bb53 | 7747 | Lisp_Object dst_object) |
d46c5b12 | 7748 | { |
d311d28c PE |
7749 | ptrdiff_t count = SPECPDL_INDEX (); |
7750 | ptrdiff_t chars = to - from; | |
7751 | ptrdiff_t bytes = to_byte - from_byte; | |
df7492f9 | 7752 | Lisp_Object attrs; |
f10fe38f PE |
7753 | ptrdiff_t saved_pt = -1, saved_pt_byte IF_LINT (= 0); |
7754 | bool need_marker_adjustment = 0; | |
7755 | bool kill_src_buffer = 0; | |
b3bfad50 | 7756 | Lisp_Object old_deactivate_mark; |
df7492f9 | 7757 | |
b3bfad50 | 7758 | old_deactivate_mark = Vdeactivate_mark; |
df7492f9 KH |
7759 | |
7760 | coding->src_object = src_object; | |
7761 | coding->src_chars = chars; | |
7762 | coding->src_bytes = bytes; | |
7763 | coding->src_multibyte = chars < bytes; | |
7764 | ||
7765 | attrs = CODING_ID_ATTRS (coding->id); | |
7766 | ||
64cedb0c KH |
7767 | if (EQ (src_object, dst_object)) |
7768 | { | |
7769 | struct Lisp_Marker *tail; | |
7770 | ||
7771 | for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next) | |
7772 | { | |
7773 | tail->need_adjustment | |
7774 | = tail->charpos == (tail->insertion_type ? from : to); | |
7775 | need_marker_adjustment |= tail->need_adjustment; | |
7776 | } | |
7777 | } | |
7778 | ||
df7492f9 | 7779 | if (! NILP (CODING_ATTR_PRE_WRITE (attrs))) |
6bac5b12 | 7780 | { |
24a73b0a | 7781 | coding->src_object = code_conversion_save (1, coding->src_multibyte); |
df7492f9 KH |
7782 | set_buffer_internal (XBUFFER (coding->src_object)); |
7783 | if (STRINGP (src_object)) | |
7784 | insert_from_string (src_object, from, from_byte, chars, bytes, 0); | |
7785 | else if (BUFFERP (src_object)) | |
7786 | insert_from_buffer (XBUFFER (src_object), from, chars, 0); | |
7787 | else | |
b68864e5 | 7788 | insert_1_both ((char *) coding->source + from, chars, bytes, 0, 0, 0); |
d46c5b12 | 7789 | |
df7492f9 KH |
7790 | if (EQ (src_object, dst_object)) |
7791 | { | |
7792 | set_buffer_internal (XBUFFER (src_object)); | |
4776e638 | 7793 | saved_pt = PT, saved_pt_byte = PT_BYTE; |
df7492f9 KH |
7794 | del_range_both (from, from_byte, to, to_byte, 1); |
7795 | set_buffer_internal (XBUFFER (coding->src_object)); | |
7796 | } | |
7797 | ||
d4850d67 | 7798 | { |
b3bfad50 | 7799 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; |
d4850d67 | 7800 | |
b3bfad50 KH |
7801 | GCPRO5 (coding->src_object, coding->dst_object, src_object, dst_object, |
7802 | old_deactivate_mark); | |
6cd7a139 DA |
7803 | safe_call2 (CODING_ATTR_PRE_WRITE (attrs), |
7804 | make_number (BEG), make_number (Z)); | |
b3bfad50 | 7805 | UNGCPRO; |
d4850d67 | 7806 | } |
c02d943b KH |
7807 | if (XBUFFER (coding->src_object) != current_buffer) |
7808 | kill_src_buffer = 1; | |
ac87bbef | 7809 | coding->src_object = Fcurrent_buffer (); |
df7492f9 KH |
7810 | if (BEG != GPT) |
7811 | move_gap_both (BEG, BEG_BYTE); | |
7812 | coding->src_chars = Z - BEG; | |
7813 | coding->src_bytes = Z_BYTE - BEG_BYTE; | |
7814 | coding->src_pos = BEG; | |
7815 | coding->src_pos_byte = BEG_BYTE; | |
7816 | coding->src_multibyte = Z < Z_BYTE; | |
7817 | } | |
7818 | else if (STRINGP (src_object)) | |
d46c5b12 | 7819 | { |
24a73b0a | 7820 | code_conversion_save (0, 0); |
df7492f9 KH |
7821 | coding->src_pos = from; |
7822 | coding->src_pos_byte = from_byte; | |
b73bfc1c | 7823 | } |
df7492f9 | 7824 | else if (BUFFERP (src_object)) |
b73bfc1c | 7825 | { |
24a73b0a | 7826 | code_conversion_save (0, 0); |
df7492f9 | 7827 | set_buffer_internal (XBUFFER (src_object)); |
df7492f9 | 7828 | if (EQ (src_object, dst_object)) |
d46c5b12 | 7829 | { |
4776e638 | 7830 | saved_pt = PT, saved_pt_byte = PT_BYTE; |
ff0dacd7 KH |
7831 | coding->src_object = del_range_1 (from, to, 1, 1); |
7832 | coding->src_pos = 0; | |
7833 | coding->src_pos_byte = 0; | |
d46c5b12 | 7834 | } |
df7492f9 | 7835 | else |
d46c5b12 | 7836 | { |
ff0dacd7 KH |
7837 | if (from < GPT && to >= GPT) |
7838 | move_gap_both (from, from_byte); | |
df7492f9 KH |
7839 | coding->src_pos = from; |
7840 | coding->src_pos_byte = from_byte; | |
d46c5b12 | 7841 | } |
d46c5b12 | 7842 | } |
4776e638 | 7843 | else |
24a73b0a | 7844 | code_conversion_save (0, 0); |
d46c5b12 | 7845 | |
df7492f9 | 7846 | if (BUFFERP (dst_object)) |
88993dfd | 7847 | { |
df7492f9 | 7848 | coding->dst_object = dst_object; |
28f67a95 KH |
7849 | if (EQ (src_object, dst_object)) |
7850 | { | |
7851 | coding->dst_pos = from; | |
7852 | coding->dst_pos_byte = from_byte; | |
7853 | } | |
7854 | else | |
7855 | { | |
319a3947 KH |
7856 | struct buffer *current = current_buffer; |
7857 | ||
7858 | set_buffer_temp (XBUFFER (dst_object)); | |
7859 | coding->dst_pos = PT; | |
7860 | coding->dst_pos_byte = PT_BYTE; | |
7861 | move_gap_both (coding->dst_pos, coding->dst_pos_byte); | |
7862 | set_buffer_temp (current); | |
28f67a95 | 7863 | } |
df7492f9 | 7864 | coding->dst_multibyte |
4b4deea2 | 7865 | = ! NILP (BVAR (XBUFFER (dst_object), enable_multibyte_characters)); |
88993dfd | 7866 | } |
df7492f9 | 7867 | else if (EQ (dst_object, Qt)) |
d46c5b12 | 7868 | { |
5d009b3a | 7869 | ptrdiff_t dst_bytes = max (1, coding->src_chars); |
df7492f9 | 7870 | coding->dst_object = Qnil; |
23f86fce | 7871 | coding->destination = xmalloc (dst_bytes); |
5d009b3a | 7872 | coding->dst_bytes = dst_bytes; |
df7492f9 | 7873 | coding->dst_multibyte = 0; |
d46c5b12 KH |
7874 | } |
7875 | else | |
7876 | { | |
df7492f9 KH |
7877 | coding->dst_object = Qnil; |
7878 | coding->dst_multibyte = 0; | |
d46c5b12 KH |
7879 | } |
7880 | ||
df7492f9 | 7881 | encode_coding (coding); |
d46c5b12 | 7882 | |
df7492f9 | 7883 | if (EQ (dst_object, Qt)) |
d46c5b12 | 7884 | { |
df7492f9 KH |
7885 | if (BUFFERP (coding->dst_object)) |
7886 | coding->dst_object = Fbuffer_string (); | |
7887 | else | |
d46c5b12 | 7888 | { |
df7492f9 KH |
7889 | coding->dst_object |
7890 | = make_unibyte_string ((char *) coding->destination, | |
7891 | coding->produced); | |
7892 | xfree (coding->destination); | |
d46c5b12 | 7893 | } |
4ed46869 | 7894 | } |
d46c5b12 | 7895 | |
4776e638 KH |
7896 | if (saved_pt >= 0) |
7897 | { | |
7898 | /* This is the case of: | |
7899 | (BUFFERP (src_object) && EQ (src_object, dst_object)) | |
7900 | As we have moved PT while replacing the original buffer | |
7901 | contents, we must recover it now. */ | |
7902 | set_buffer_internal (XBUFFER (src_object)); | |
7903 | if (saved_pt < from) | |
7904 | TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte); | |
7905 | else if (saved_pt < from + chars) | |
7906 | TEMP_SET_PT_BOTH (from, from_byte); | |
4b4deea2 | 7907 | else if (! NILP (BVAR (current_buffer, enable_multibyte_characters))) |
4776e638 KH |
7908 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars), |
7909 | saved_pt_byte + (coding->produced - bytes)); | |
d46c5b12 | 7910 | else |
4776e638 KH |
7911 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes), |
7912 | saved_pt_byte + (coding->produced - bytes)); | |
64cedb0c KH |
7913 | |
7914 | if (need_marker_adjustment) | |
7915 | { | |
7916 | struct Lisp_Marker *tail; | |
7917 | ||
7918 | for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next) | |
7919 | if (tail->need_adjustment) | |
7920 | { | |
7921 | tail->need_adjustment = 0; | |
7922 | if (tail->insertion_type) | |
7923 | { | |
7924 | tail->bytepos = from_byte; | |
7925 | tail->charpos = from; | |
7926 | } | |
7927 | else | |
7928 | { | |
7929 | tail->bytepos = from_byte + coding->produced; | |
7930 | tail->charpos | |
4b4deea2 | 7931 | = (NILP (BVAR (current_buffer, enable_multibyte_characters)) |
64cedb0c KH |
7932 | ? tail->bytepos : from + coding->produced_char); |
7933 | } | |
7934 | } | |
7935 | } | |
4776e638 KH |
7936 | } |
7937 | ||
c02d943b KH |
7938 | if (kill_src_buffer) |
7939 | Fkill_buffer (coding->src_object); | |
b3bfad50 KH |
7940 | |
7941 | Vdeactivate_mark = old_deactivate_mark; | |
df7492f9 | 7942 | unbind_to (count, Qnil); |
b73bfc1c KH |
7943 | } |
7944 | ||
df7492f9 | 7945 | |
b73bfc1c | 7946 | Lisp_Object |
971de7fb | 7947 | preferred_coding_system (void) |
b73bfc1c | 7948 | { |
df7492f9 | 7949 | int id = coding_categories[coding_priorities[0]].id; |
2391eaa4 | 7950 | |
df7492f9 | 7951 | return CODING_ID_NAME (id); |
4ed46869 KH |
7952 | } |
7953 | ||
7f590b0c | 7954 | #if defined (WINDOWSNT) || defined (CYGWIN) |
ba116008 DC |
7955 | |
7956 | Lisp_Object | |
7957 | from_unicode (Lisp_Object str) | |
7958 | { | |
7959 | CHECK_STRING (str); | |
7960 | if (!STRING_MULTIBYTE (str) && | |
7961 | SBYTES (str) & 1) | |
7962 | { | |
7963 | str = Fsubstring (str, make_number (0), make_number (-1)); | |
7964 | } | |
7965 | ||
7966 | return code_convert_string_norecord (str, Qutf_16le, 0); | |
7967 | } | |
7968 | ||
7969 | wchar_t * | |
7970 | to_unicode (Lisp_Object str, Lisp_Object *buf) | |
7971 | { | |
7972 | *buf = code_convert_string_norecord (str, Qutf_16le, 1); | |
7973 | /* We need to make a another copy (in addition to the one made by | |
7974 | code_convert_string_norecord) to ensure that the final string is | |
7975 | _doubly_ zero terminated --- that is, that the string is | |
7976 | terminated by two zero bytes and one utf-16le null character. | |
7977 | Because strings are already terminated with a single zero byte, | |
7978 | we just add one additional zero. */ | |
7979 | str = make_uninit_string (SBYTES (*buf) + 1); | |
7980 | memcpy (SDATA (str), SDATA (*buf), SBYTES (*buf)); | |
7981 | SDATA (str) [SBYTES (*buf)] = '\0'; | |
7982 | *buf = str; | |
7983 | return WCSDATA (*buf); | |
7984 | } | |
7f590b0c DC |
7985 | |
7986 | #endif /* WINDOWSNT || CYGWIN */ | |
ba116008 | 7987 | |
4ed46869 KH |
7988 | \f |
7989 | #ifdef emacs | |
1397dc18 | 7990 | /*** 8. Emacs Lisp library functions ***/ |
4ed46869 | 7991 | |
a7ca3326 | 7992 | DEFUN ("coding-system-p", Fcoding_system_p, Scoding_system_p, 1, 1, 0, |
48b0f3ae | 7993 | doc: /* Return t if OBJECT is nil or a coding-system. |
df7492f9 | 7994 | See the documentation of `define-coding-system' for information |
48b0f3ae | 7995 | about coding-system objects. */) |
5842a27b | 7996 | (Lisp_Object object) |
4ed46869 | 7997 | { |
d4a1d553 JB |
7998 | if (NILP (object) |
7999 | || CODING_SYSTEM_ID (object) >= 0) | |
44e8490d | 8000 | return Qt; |
d4a1d553 JB |
8001 | if (! SYMBOLP (object) |
8002 | || NILP (Fget (object, Qcoding_system_define_form))) | |
44e8490d KH |
8003 | return Qnil; |
8004 | return Qt; | |
4ed46869 KH |
8005 | } |
8006 | ||
a7ca3326 | 8007 | DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system, |
9d991de8 | 8008 | Sread_non_nil_coding_system, 1, 1, 0, |
48b0f3ae | 8009 | doc: /* Read a coding system from the minibuffer, prompting with string PROMPT. */) |
5842a27b | 8010 | (Lisp_Object prompt) |
4ed46869 | 8011 | { |
e0e989f6 | 8012 | Lisp_Object val; |
9d991de8 RS |
8013 | do |
8014 | { | |
4608c386 KH |
8015 | val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil, |
8016 | Qt, Qnil, Qcoding_system_history, Qnil, Qnil); | |
9d991de8 | 8017 | } |
8f924df7 | 8018 | while (SCHARS (val) == 0); |
e0e989f6 | 8019 | return (Fintern (val, Qnil)); |
4ed46869 KH |
8020 | } |
8021 | ||
a7ca3326 | 8022 | DEFUN ("read-coding-system", Fread_coding_system, Sread_coding_system, 1, 2, 0, |
48b0f3ae | 8023 | doc: /* Read a coding system from the minibuffer, prompting with string PROMPT. |
c7183fb8 GM |
8024 | If the user enters null input, return second argument DEFAULT-CODING-SYSTEM. |
8025 | Ignores case when completing coding systems (all Emacs coding systems | |
8026 | are lower-case). */) | |
5842a27b | 8027 | (Lisp_Object prompt, Lisp_Object default_coding_system) |
4ed46869 | 8028 | { |
f44d27ce | 8029 | Lisp_Object val; |
d311d28c | 8030 | ptrdiff_t count = SPECPDL_INDEX (); |
c7183fb8 | 8031 | |
9b787f3e | 8032 | if (SYMBOLP (default_coding_system)) |
57d25e6f | 8033 | default_coding_system = SYMBOL_NAME (default_coding_system); |
c7183fb8 | 8034 | specbind (Qcompletion_ignore_case, Qt); |
4608c386 | 8035 | val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil, |
9b787f3e RS |
8036 | Qt, Qnil, Qcoding_system_history, |
8037 | default_coding_system, Qnil); | |
c7183fb8 | 8038 | unbind_to (count, Qnil); |
8f924df7 | 8039 | return (SCHARS (val) == 0 ? Qnil : Fintern (val, Qnil)); |
4ed46869 KH |
8040 | } |
8041 | ||
a7ca3326 | 8042 | DEFUN ("check-coding-system", Fcheck_coding_system, Scheck_coding_system, |
4ed46869 | 8043 | 1, 1, 0, |
48b0f3ae | 8044 | doc: /* Check validity of CODING-SYSTEM. |
9ffd559c KH |
8045 | If valid, return CODING-SYSTEM, else signal a `coding-system-error' error. |
8046 | It is valid if it is nil or a symbol defined as a coding system by the | |
8047 | function `define-coding-system'. */) | |
5842a27b | 8048 | (Lisp_Object coding_system) |
4ed46869 | 8049 | { |
44e8490d KH |
8050 | Lisp_Object define_form; |
8051 | ||
8052 | define_form = Fget (coding_system, Qcoding_system_define_form); | |
8053 | if (! NILP (define_form)) | |
8054 | { | |
8055 | Fput (coding_system, Qcoding_system_define_form, Qnil); | |
8056 | safe_eval (define_form); | |
8057 | } | |
4ed46869 KH |
8058 | if (!NILP (Fcoding_system_p (coding_system))) |
8059 | return coding_system; | |
fcad4ec4 | 8060 | xsignal1 (Qcoding_system_error, coding_system); |
4ed46869 | 8061 | } |
df7492f9 | 8062 | |
3a73fa5d | 8063 | \f |
89528eb3 | 8064 | /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If |
f10fe38f | 8065 | HIGHEST, return the coding system of the highest |
ad1746f5 | 8066 | priority among the detected coding systems. Otherwise return a |
89528eb3 | 8067 | list of detected coding systems sorted by their priorities. If |
f10fe38f | 8068 | MULTIBYTEP, it is assumed that the bytes are in correct |
89528eb3 KH |
8069 | multibyte form but contains only ASCII and eight-bit chars. |
8070 | Otherwise, the bytes are raw bytes. | |
8071 | ||
8072 | CODING-SYSTEM controls the detection as below: | |
8073 | ||
8074 | If it is nil, detect both text-format and eol-format. If the | |
8075 | text-format part of CODING-SYSTEM is already specified | |
8076 | (e.g. `iso-latin-1'), detect only eol-format. If the eol-format | |
8077 | part of CODING-SYSTEM is already specified (e.g. `undecided-unix'), | |
8078 | detect only text-format. */ | |
8079 | ||
d46c5b12 | 8080 | Lisp_Object |
cf84bb53 | 8081 | detect_coding_system (const unsigned char *src, |
d311d28c | 8082 | ptrdiff_t src_chars, ptrdiff_t src_bytes, |
f10fe38f | 8083 | bool highest, bool multibytep, |
cf84bb53 | 8084 | Lisp_Object coding_system) |
4ed46869 | 8085 | { |
8f924df7 | 8086 | const unsigned char *src_end = src + src_bytes; |
df7492f9 | 8087 | Lisp_Object attrs, eol_type; |
4533845d | 8088 | Lisp_Object val = Qnil; |
df7492f9 | 8089 | struct coding_system coding; |
d3411f89 | 8090 | ptrdiff_t id; |
ff0dacd7 | 8091 | struct coding_detection_info detect_info; |
24a73b0a | 8092 | enum coding_category base_category; |
f10fe38f | 8093 | bool null_byte_found = 0, eight_bit_found = 0; |
b73bfc1c | 8094 | |
df7492f9 KH |
8095 | if (NILP (coding_system)) |
8096 | coding_system = Qundecided; | |
8097 | setup_coding_system (coding_system, &coding); | |
8098 | attrs = CODING_ID_ATTRS (coding.id); | |
8099 | eol_type = CODING_ID_EOL_TYPE (coding.id); | |
89528eb3 | 8100 | coding_system = CODING_ATTR_BASE_NAME (attrs); |
4ed46869 | 8101 | |
df7492f9 | 8102 | coding.source = src; |
24a73b0a | 8103 | coding.src_chars = src_chars; |
df7492f9 KH |
8104 | coding.src_bytes = src_bytes; |
8105 | coding.src_multibyte = multibytep; | |
8106 | coding.consumed = 0; | |
89528eb3 | 8107 | coding.mode |= CODING_MODE_LAST_BLOCK; |
c0e16b14 | 8108 | coding.head_ascii = 0; |
d46c5b12 | 8109 | |
ff0dacd7 | 8110 | detect_info.checked = detect_info.found = detect_info.rejected = 0; |
d46c5b12 | 8111 | |
89528eb3 | 8112 | /* At first, detect text-format if necessary. */ |
24a73b0a KH |
8113 | base_category = XINT (CODING_ATTR_CATEGORY (attrs)); |
8114 | if (base_category == coding_category_undecided) | |
4ed46869 | 8115 | { |
c4a63b12 PE |
8116 | enum coding_category category IF_LINT (= 0); |
8117 | struct coding_system *this IF_LINT (= NULL); | |
ff0dacd7 | 8118 | int c, i; |
88993dfd | 8119 | |
24a73b0a | 8120 | /* Skip all ASCII bytes except for a few ISO2022 controls. */ |
2f3cbb32 | 8121 | for (; src < src_end; src++) |
4ed46869 | 8122 | { |
df7492f9 | 8123 | c = *src; |
6cb21a4f | 8124 | if (c & 0x80) |
6cb21a4f | 8125 | { |
2f3cbb32 | 8126 | eight_bit_found = 1; |
2f3cbb32 KH |
8127 | if (null_byte_found) |
8128 | break; | |
8129 | } | |
c0e16b14 | 8130 | else if (c < 0x20) |
2f3cbb32 KH |
8131 | { |
8132 | if ((c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) | |
8133 | && ! inhibit_iso_escape_detection | |
8134 | && ! detect_info.checked) | |
6cb21a4f | 8135 | { |
2f3cbb32 KH |
8136 | if (detect_coding_iso_2022 (&coding, &detect_info)) |
8137 | { | |
8138 | /* We have scanned the whole data. */ | |
8139 | if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE)) | |
c0e16b14 KH |
8140 | { |
8141 | /* We didn't find an 8-bit code. We may | |
8142 | have found a null-byte, but it's very | |
8143 | rare that a binary file confirm to | |
8144 | ISO-2022. */ | |
8145 | src = src_end; | |
8146 | coding.head_ascii = src - coding.source; | |
8147 | } | |
8148 | detect_info.rejected |= ~CATEGORY_MASK_ISO_ESCAPE; | |
2f3cbb32 KH |
8149 | break; |
8150 | } | |
8151 | } | |
97b1b294 | 8152 | else if (! c && !inhibit_null_byte_detection) |
2f3cbb32 KH |
8153 | { |
8154 | null_byte_found = 1; | |
8155 | if (eight_bit_found) | |
8156 | break; | |
6cb21a4f | 8157 | } |
c006c0c8 KH |
8158 | if (! eight_bit_found) |
8159 | coding.head_ascii++; | |
6cb21a4f | 8160 | } |
c006c0c8 | 8161 | else if (! eight_bit_found) |
c0e16b14 | 8162 | coding.head_ascii++; |
4ed46869 | 8163 | } |
88993dfd | 8164 | |
2f3cbb32 KH |
8165 | if (null_byte_found || eight_bit_found |
8166 | || coding.head_ascii < coding.src_bytes | |
6cb21a4f KH |
8167 | || detect_info.found) |
8168 | { | |
2f3cbb32 | 8169 | if (coding.head_ascii == coding.src_bytes) |
6cb21a4f KH |
8170 | /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */ |
8171 | for (i = 0; i < coding_category_raw_text; i++) | |
ff0dacd7 | 8172 | { |
6cb21a4f | 8173 | category = coding_priorities[i]; |
c7266f4a | 8174 | this = coding_categories + category; |
6cb21a4f | 8175 | if (detect_info.found & (1 << category)) |
ff0dacd7 KH |
8176 | break; |
8177 | } | |
6cb21a4f | 8178 | else |
2f3cbb32 KH |
8179 | { |
8180 | if (null_byte_found) | |
8181 | { | |
8182 | detect_info.checked |= ~CATEGORY_MASK_UTF_16; | |
8183 | detect_info.rejected |= ~CATEGORY_MASK_UTF_16; | |
8184 | } | |
8185 | for (i = 0; i < coding_category_raw_text; i++) | |
8186 | { | |
8187 | category = coding_priorities[i]; | |
8188 | this = coding_categories + category; | |
6cb21a4f | 8189 | |
2f3cbb32 KH |
8190 | if (this->id < 0) |
8191 | { | |
8192 | /* No coding system of this category is defined. */ | |
8193 | detect_info.rejected |= (1 << category); | |
8194 | } | |
8195 | else if (category >= coding_category_raw_text) | |
8196 | continue; | |
8197 | else if (detect_info.checked & (1 << category)) | |
8198 | { | |
8199 | if (highest | |
8200 | && (detect_info.found & (1 << category))) | |
6cb21a4f | 8201 | break; |
2f3cbb32 KH |
8202 | } |
8203 | else if ((*(this->detector)) (&coding, &detect_info) | |
8204 | && highest | |
8205 | && (detect_info.found & (1 << category))) | |
8206 | { | |
8207 | if (category == coding_category_utf_16_auto) | |
8208 | { | |
8209 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
8210 | category = coding_category_utf_16_le; | |
8211 | else | |
8212 | category = coding_category_utf_16_be; | |
8213 | } | |
8214 | break; | |
8215 | } | |
8216 | } | |
8217 | } | |
6cb21a4f | 8218 | } |
ec6d2bb8 | 8219 | |
4cddb209 KH |
8220 | if ((detect_info.rejected & CATEGORY_MASK_ANY) == CATEGORY_MASK_ANY |
8221 | || null_byte_found) | |
ec6d2bb8 | 8222 | { |
ff0dacd7 | 8223 | detect_info.found = CATEGORY_MASK_RAW_TEXT; |
4cddb209 | 8224 | id = CODING_SYSTEM_ID (Qno_conversion); |
89528eb3 KH |
8225 | val = Fcons (make_number (id), Qnil); |
8226 | } | |
ff0dacd7 | 8227 | else if (! detect_info.rejected && ! detect_info.found) |
89528eb3 | 8228 | { |
ff0dacd7 | 8229 | detect_info.found = CATEGORY_MASK_ANY; |
89528eb3 KH |
8230 | id = coding_categories[coding_category_undecided].id; |
8231 | val = Fcons (make_number (id), Qnil); | |
8232 | } | |
8233 | else if (highest) | |
8234 | { | |
ff0dacd7 | 8235 | if (detect_info.found) |
ec6d2bb8 | 8236 | { |
ff0dacd7 KH |
8237 | detect_info.found = 1 << category; |
8238 | val = Fcons (make_number (this->id), Qnil); | |
8239 | } | |
8240 | else | |
8241 | for (i = 0; i < coding_category_raw_text; i++) | |
8242 | if (! (detect_info.rejected & (1 << coding_priorities[i]))) | |
8243 | { | |
8244 | detect_info.found = 1 << coding_priorities[i]; | |
8245 | id = coding_categories[coding_priorities[i]].id; | |
8246 | val = Fcons (make_number (id), Qnil); | |
8247 | break; | |
8248 | } | |
8249 | } | |
89528eb3 KH |
8250 | else |
8251 | { | |
ff0dacd7 KH |
8252 | int mask = detect_info.rejected | detect_info.found; |
8253 | int found = 0; | |
ec6d2bb8 | 8254 | |
89528eb3 | 8255 | for (i = coding_category_raw_text - 1; i >= 0; i--) |
ff0dacd7 KH |
8256 | { |
8257 | category = coding_priorities[i]; | |
8258 | if (! (mask & (1 << category))) | |
ec6d2bb8 | 8259 | { |
ff0dacd7 KH |
8260 | found |= 1 << category; |
8261 | id = coding_categories[category].id; | |
c7266f4a KH |
8262 | if (id >= 0) |
8263 | val = Fcons (make_number (id), val); | |
ff0dacd7 KH |
8264 | } |
8265 | } | |
8266 | for (i = coding_category_raw_text - 1; i >= 0; i--) | |
8267 | { | |
8268 | category = coding_priorities[i]; | |
8269 | if (detect_info.found & (1 << category)) | |
8270 | { | |
8271 | id = coding_categories[category].id; | |
8272 | val = Fcons (make_number (id), val); | |
ec6d2bb8 | 8273 | } |
ec6d2bb8 | 8274 | } |
ff0dacd7 | 8275 | detect_info.found |= found; |
ec6d2bb8 | 8276 | } |
ec6d2bb8 | 8277 | } |
a470d443 KH |
8278 | else if (base_category == coding_category_utf_8_auto) |
8279 | { | |
8280 | if (detect_coding_utf_8 (&coding, &detect_info)) | |
8281 | { | |
8282 | struct coding_system *this; | |
8283 | ||
8284 | if (detect_info.found & CATEGORY_MASK_UTF_8_SIG) | |
8285 | this = coding_categories + coding_category_utf_8_sig; | |
8286 | else | |
8287 | this = coding_categories + coding_category_utf_8_nosig; | |
8288 | val = Fcons (make_number (this->id), Qnil); | |
8289 | } | |
8290 | } | |
24a73b0a KH |
8291 | else if (base_category == coding_category_utf_16_auto) |
8292 | { | |
8293 | if (detect_coding_utf_16 (&coding, &detect_info)) | |
8294 | { | |
24a73b0a KH |
8295 | struct coding_system *this; |
8296 | ||
8297 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
8298 | this = coding_categories + coding_category_utf_16_le; | |
8299 | else if (detect_info.found & CATEGORY_MASK_UTF_16_BE) | |
8300 | this = coding_categories + coding_category_utf_16_be; | |
8301 | else if (detect_info.rejected & CATEGORY_MASK_UTF_16_LE_NOSIG) | |
8302 | this = coding_categories + coding_category_utf_16_be_nosig; | |
8303 | else | |
8304 | this = coding_categories + coding_category_utf_16_le_nosig; | |
8305 | val = Fcons (make_number (this->id), Qnil); | |
8306 | } | |
8307 | } | |
df7492f9 KH |
8308 | else |
8309 | { | |
ff0dacd7 | 8310 | detect_info.found = 1 << XINT (CODING_ATTR_CATEGORY (attrs)); |
89528eb3 | 8311 | val = Fcons (make_number (coding.id), Qnil); |
4ed46869 | 8312 | } |
df7492f9 | 8313 | |
89528eb3 | 8314 | /* Then, detect eol-format if necessary. */ |
df7492f9 | 8315 | { |
4533845d | 8316 | int normal_eol = -1, utf_16_be_eol = -1, utf_16_le_eol = -1; |
df7492f9 KH |
8317 | Lisp_Object tail; |
8318 | ||
89528eb3 KH |
8319 | if (VECTORP (eol_type)) |
8320 | { | |
ff0dacd7 | 8321 | if (detect_info.found & ~CATEGORY_MASK_UTF_16) |
2f3cbb32 KH |
8322 | { |
8323 | if (null_byte_found) | |
8324 | normal_eol = EOL_SEEN_LF; | |
8325 | else | |
8326 | normal_eol = detect_eol (coding.source, src_bytes, | |
8327 | coding_category_raw_text); | |
8328 | } | |
ff0dacd7 KH |
8329 | if (detect_info.found & (CATEGORY_MASK_UTF_16_BE |
8330 | | CATEGORY_MASK_UTF_16_BE_NOSIG)) | |
89528eb3 KH |
8331 | utf_16_be_eol = detect_eol (coding.source, src_bytes, |
8332 | coding_category_utf_16_be); | |
ff0dacd7 KH |
8333 | if (detect_info.found & (CATEGORY_MASK_UTF_16_LE |
8334 | | CATEGORY_MASK_UTF_16_LE_NOSIG)) | |
89528eb3 KH |
8335 | utf_16_le_eol = detect_eol (coding.source, src_bytes, |
8336 | coding_category_utf_16_le); | |
8337 | } | |
8338 | else | |
8339 | { | |
8340 | if (EQ (eol_type, Qunix)) | |
8341 | normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_LF; | |
8342 | else if (EQ (eol_type, Qdos)) | |
8343 | normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CRLF; | |
8344 | else | |
8345 | normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CR; | |
8346 | } | |
8347 | ||
df7492f9 KH |
8348 | for (tail = val; CONSP (tail); tail = XCDR (tail)) |
8349 | { | |
89528eb3 | 8350 | enum coding_category category; |
df7492f9 | 8351 | int this_eol; |
89528eb3 KH |
8352 | |
8353 | id = XINT (XCAR (tail)); | |
8354 | attrs = CODING_ID_ATTRS (id); | |
8355 | category = XINT (CODING_ATTR_CATEGORY (attrs)); | |
8356 | eol_type = CODING_ID_EOL_TYPE (id); | |
df7492f9 KH |
8357 | if (VECTORP (eol_type)) |
8358 | { | |
89528eb3 KH |
8359 | if (category == coding_category_utf_16_be |
8360 | || category == coding_category_utf_16_be_nosig) | |
8361 | this_eol = utf_16_be_eol; | |
8362 | else if (category == coding_category_utf_16_le | |
8363 | || category == coding_category_utf_16_le_nosig) | |
8364 | this_eol = utf_16_le_eol; | |
df7492f9 | 8365 | else |
89528eb3 KH |
8366 | this_eol = normal_eol; |
8367 | ||
df7492f9 KH |
8368 | if (this_eol == EOL_SEEN_LF) |
8369 | XSETCAR (tail, AREF (eol_type, 0)); | |
8370 | else if (this_eol == EOL_SEEN_CRLF) | |
8371 | XSETCAR (tail, AREF (eol_type, 1)); | |
8372 | else if (this_eol == EOL_SEEN_CR) | |
8373 | XSETCAR (tail, AREF (eol_type, 2)); | |
89528eb3 KH |
8374 | else |
8375 | XSETCAR (tail, CODING_ID_NAME (id)); | |
df7492f9 | 8376 | } |
89528eb3 KH |
8377 | else |
8378 | XSETCAR (tail, CODING_ID_NAME (id)); | |
df7492f9 KH |
8379 | } |
8380 | } | |
ec6d2bb8 | 8381 | |
4533845d | 8382 | return (highest ? (CONSP (val) ? XCAR (val) : Qnil) : val); |
ec6d2bb8 KH |
8383 | } |
8384 | ||
ec6d2bb8 | 8385 | |
d46c5b12 KH |
8386 | DEFUN ("detect-coding-region", Fdetect_coding_region, Sdetect_coding_region, |
8387 | 2, 3, 0, | |
48b0f3ae PJ |
8388 | doc: /* Detect coding system of the text in the region between START and END. |
8389 | Return a list of possible coding systems ordered by priority. | |
b811c52b KH |
8390 | The coding systems to try and their priorities follows what |
8391 | the function `coding-system-priority-list' (which see) returns. | |
ec6d2bb8 | 8392 | |
12e0131a | 8393 | If only ASCII characters are found (except for such ISO-2022 control |
d4a1d553 JB |
8394 | characters as ESC), it returns a list of single element `undecided' |
8395 | or its subsidiary coding system according to a detected end-of-line | |
8396 | format. | |
ec6d2bb8 | 8397 | |
48b0f3ae PJ |
8398 | If optional argument HIGHEST is non-nil, return the coding system of |
8399 | highest priority. */) | |
5842a27b | 8400 | (Lisp_Object start, Lisp_Object end, Lisp_Object highest) |
d46c5b12 | 8401 | { |
d311d28c PE |
8402 | ptrdiff_t from, to; |
8403 | ptrdiff_t from_byte, to_byte; | |
ec6d2bb8 | 8404 | |
d46c5b12 KH |
8405 | validate_region (&start, &end); |
8406 | from = XINT (start), to = XINT (end); | |
8407 | from_byte = CHAR_TO_BYTE (from); | |
8408 | to_byte = CHAR_TO_BYTE (to); | |
ec6d2bb8 | 8409 | |
d46c5b12 KH |
8410 | if (from < GPT && to >= GPT) |
8411 | move_gap_both (to, to_byte); | |
c210f766 | 8412 | |
d46c5b12 | 8413 | return detect_coding_system (BYTE_POS_ADDR (from_byte), |
24a73b0a | 8414 | to - from, to_byte - from_byte, |
0a28aafb | 8415 | !NILP (highest), |
4b4deea2 | 8416 | !NILP (BVAR (current_buffer |
5d8ea120 | 8417 | , enable_multibyte_characters)), |
df7492f9 | 8418 | Qnil); |
ec6d2bb8 KH |
8419 | } |
8420 | ||
d46c5b12 KH |
8421 | DEFUN ("detect-coding-string", Fdetect_coding_string, Sdetect_coding_string, |
8422 | 1, 2, 0, | |
48b0f3ae PJ |
8423 | doc: /* Detect coding system of the text in STRING. |
8424 | Return a list of possible coding systems ordered by priority. | |
67ceab9d KH |
8425 | The coding systems to try and their priorities follows what |
8426 | the function `coding-system-priority-list' (which see) returns. | |
fb88bf2d | 8427 | |
12e0131a | 8428 | If only ASCII characters are found (except for such ISO-2022 control |
d4a1d553 JB |
8429 | characters as ESC), it returns a list of single element `undecided' |
8430 | or its subsidiary coding system according to a detected end-of-line | |
8431 | format. | |
d46c5b12 | 8432 | |
48b0f3ae PJ |
8433 | If optional argument HIGHEST is non-nil, return the coding system of |
8434 | highest priority. */) | |
5842a27b | 8435 | (Lisp_Object string, Lisp_Object highest) |
d46c5b12 | 8436 | { |
b7826503 | 8437 | CHECK_STRING (string); |
b73bfc1c | 8438 | |
24a73b0a KH |
8439 | return detect_coding_system (SDATA (string), |
8440 | SCHARS (string), SBYTES (string), | |
8f924df7 | 8441 | !NILP (highest), STRING_MULTIBYTE (string), |
df7492f9 | 8442 | Qnil); |
4ed46869 | 8443 | } |
4ed46869 | 8444 | |
b73bfc1c | 8445 | |
b0ab8123 | 8446 | static bool |
971de7fb | 8447 | char_encodable_p (int c, Lisp_Object attrs) |
05e6f5dc | 8448 | { |
df7492f9 | 8449 | Lisp_Object tail; |
df7492f9 | 8450 | struct charset *charset; |
7d64c6ad | 8451 | Lisp_Object translation_table; |
d46c5b12 | 8452 | |
7d64c6ad | 8453 | translation_table = CODING_ATTR_TRANS_TBL (attrs); |
a6f87d34 | 8454 | if (! NILP (translation_table)) |
7d64c6ad | 8455 | c = translate_char (translation_table, c); |
df7492f9 KH |
8456 | for (tail = CODING_ATTR_CHARSET_LIST (attrs); |
8457 | CONSP (tail); tail = XCDR (tail)) | |
e133c8fa | 8458 | { |
df7492f9 KH |
8459 | charset = CHARSET_FROM_ID (XINT (XCAR (tail))); |
8460 | if (CHAR_CHARSET_P (c, charset)) | |
8461 | break; | |
e133c8fa | 8462 | } |
df7492f9 | 8463 | return (! NILP (tail)); |
05e6f5dc | 8464 | } |
83fa074f | 8465 | |
fb88bf2d | 8466 | |
df7492f9 KH |
8467 | /* Return a list of coding systems that safely encode the text between |
8468 | START and END. If EXCLUDE is non-nil, it is a list of coding | |
8469 | systems not to check. The returned list doesn't contain any such | |
48468dac | 8470 | coding systems. In any case, if the text contains only ASCII or is |
df7492f9 | 8471 | unibyte, return t. */ |
e077cc80 | 8472 | |
df7492f9 KH |
8473 | DEFUN ("find-coding-systems-region-internal", |
8474 | Ffind_coding_systems_region_internal, | |
8475 | Sfind_coding_systems_region_internal, 2, 3, 0, | |
8476 | doc: /* Internal use only. */) | |
5842a27b | 8477 | (Lisp_Object start, Lisp_Object end, Lisp_Object exclude) |
df7492f9 KH |
8478 | { |
8479 | Lisp_Object coding_attrs_list, safe_codings; | |
d311d28c | 8480 | ptrdiff_t start_byte, end_byte; |
7c78e542 | 8481 | const unsigned char *p, *pbeg, *pend; |
df7492f9 | 8482 | int c; |
0e727afa | 8483 | Lisp_Object tail, elt, work_table; |
d46c5b12 | 8484 | |
df7492f9 KH |
8485 | if (STRINGP (start)) |
8486 | { | |
8487 | if (!STRING_MULTIBYTE (start) | |
8f924df7 | 8488 | || SCHARS (start) == SBYTES (start)) |
df7492f9 KH |
8489 | return Qt; |
8490 | start_byte = 0; | |
8f924df7 | 8491 | end_byte = SBYTES (start); |
df7492f9 KH |
8492 | } |
8493 | else | |
d46c5b12 | 8494 | { |
df7492f9 KH |
8495 | CHECK_NUMBER_COERCE_MARKER (start); |
8496 | CHECK_NUMBER_COERCE_MARKER (end); | |
8497 | if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end)) | |
8498 | args_out_of_range (start, end); | |
4b4deea2 | 8499 | if (NILP (BVAR (current_buffer, enable_multibyte_characters))) |
df7492f9 KH |
8500 | return Qt; |
8501 | start_byte = CHAR_TO_BYTE (XINT (start)); | |
8502 | end_byte = CHAR_TO_BYTE (XINT (end)); | |
8503 | if (XINT (end) - XINT (start) == end_byte - start_byte) | |
8504 | return Qt; | |
d46c5b12 | 8505 | |
e1c23804 | 8506 | if (XINT (start) < GPT && XINT (end) > GPT) |
d46c5b12 | 8507 | { |
e1c23804 DL |
8508 | if ((GPT - XINT (start)) < (XINT (end) - GPT)) |
8509 | move_gap_both (XINT (start), start_byte); | |
df7492f9 | 8510 | else |
e1c23804 | 8511 | move_gap_both (XINT (end), end_byte); |
d46c5b12 KH |
8512 | } |
8513 | } | |
8514 | ||
df7492f9 KH |
8515 | coding_attrs_list = Qnil; |
8516 | for (tail = Vcoding_system_list; CONSP (tail); tail = XCDR (tail)) | |
8517 | if (NILP (exclude) | |
8518 | || NILP (Fmemq (XCAR (tail), exclude))) | |
8519 | { | |
8520 | Lisp_Object attrs; | |
d46c5b12 | 8521 | |
df7492f9 KH |
8522 | attrs = AREF (CODING_SYSTEM_SPEC (XCAR (tail)), 0); |
8523 | if (EQ (XCAR (tail), CODING_ATTR_BASE_NAME (attrs)) | |
8524 | && ! EQ (CODING_ATTR_TYPE (attrs), Qundecided)) | |
7d64c6ad KH |
8525 | { |
8526 | ASET (attrs, coding_attr_trans_tbl, | |
2170c8f0 | 8527 | get_translation_table (attrs, 1, NULL)); |
7d64c6ad KH |
8528 | coding_attrs_list = Fcons (attrs, coding_attrs_list); |
8529 | } | |
df7492f9 | 8530 | } |
d46c5b12 | 8531 | |
df7492f9 | 8532 | if (STRINGP (start)) |
8f924df7 | 8533 | p = pbeg = SDATA (start); |
df7492f9 KH |
8534 | else |
8535 | p = pbeg = BYTE_POS_ADDR (start_byte); | |
8536 | pend = p + (end_byte - start_byte); | |
b843d1ae | 8537 | |
df7492f9 KH |
8538 | while (p < pend && ASCII_BYTE_P (*p)) p++; |
8539 | while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--; | |
d46c5b12 | 8540 | |
0e727afa | 8541 | work_table = Fmake_char_table (Qnil, Qnil); |
05e6f5dc | 8542 | while (p < pend) |
72d1a715 | 8543 | { |
df7492f9 KH |
8544 | if (ASCII_BYTE_P (*p)) |
8545 | p++; | |
72d1a715 RS |
8546 | else |
8547 | { | |
df7492f9 | 8548 | c = STRING_CHAR_ADVANCE (p); |
0e727afa YM |
8549 | if (!NILP (char_table_ref (work_table, c))) |
8550 | /* This character was already checked. Ignore it. */ | |
8551 | continue; | |
12410ef1 | 8552 | |
df7492f9 KH |
8553 | charset_map_loaded = 0; |
8554 | for (tail = coding_attrs_list; CONSP (tail);) | |
8555 | { | |
8556 | elt = XCAR (tail); | |
8557 | if (NILP (elt)) | |
8558 | tail = XCDR (tail); | |
8559 | else if (char_encodable_p (c, elt)) | |
8560 | tail = XCDR (tail); | |
8561 | else if (CONSP (XCDR (tail))) | |
8562 | { | |
8563 | XSETCAR (tail, XCAR (XCDR (tail))); | |
8564 | XSETCDR (tail, XCDR (XCDR (tail))); | |
8565 | } | |
8566 | else | |
8567 | { | |
8568 | XSETCAR (tail, Qnil); | |
8569 | tail = XCDR (tail); | |
8570 | } | |
8571 | } | |
8572 | if (charset_map_loaded) | |
8573 | { | |
d311d28c | 8574 | ptrdiff_t p_offset = p - pbeg, pend_offset = pend - pbeg; |
05e6f5dc | 8575 | |
df7492f9 | 8576 | if (STRINGP (start)) |
8f924df7 | 8577 | pbeg = SDATA (start); |
df7492f9 KH |
8578 | else |
8579 | pbeg = BYTE_POS_ADDR (start_byte); | |
8580 | p = pbeg + p_offset; | |
8581 | pend = pbeg + pend_offset; | |
8582 | } | |
0e727afa | 8583 | char_table_set (work_table, c, Qt); |
df7492f9 | 8584 | } |
ec6d2bb8 | 8585 | } |
fb88bf2d | 8586 | |
988b3759 | 8587 | safe_codings = list2 (Qraw_text, Qno_conversion); |
df7492f9 KH |
8588 | for (tail = coding_attrs_list; CONSP (tail); tail = XCDR (tail)) |
8589 | if (! NILP (XCAR (tail))) | |
8590 | safe_codings = Fcons (CODING_ATTR_BASE_NAME (XCAR (tail)), safe_codings); | |
ec6d2bb8 | 8591 | |
05e6f5dc KH |
8592 | return safe_codings; |
8593 | } | |
4956c225 | 8594 | |
d46c5b12 | 8595 | |
8f924df7 KH |
8596 | DEFUN ("unencodable-char-position", Funencodable_char_position, |
8597 | Sunencodable_char_position, 3, 5, 0, | |
8598 | doc: /* | |
8599 | Return position of first un-encodable character in a region. | |
d4a1d553 | 8600 | START and END specify the region and CODING-SYSTEM specifies the |
8f924df7 | 8601 | encoding to check. Return nil if CODING-SYSTEM does encode the region. |
d46c5b12 | 8602 | |
8f924df7 KH |
8603 | If optional 4th argument COUNT is non-nil, it specifies at most how |
8604 | many un-encodable characters to search. In this case, the value is a | |
8605 | list of positions. | |
d46c5b12 | 8606 | |
8f924df7 KH |
8607 | If optional 5th argument STRING is non-nil, it is a string to search |
8608 | for un-encodable characters. In that case, START and END are indexes | |
8609 | to the string. */) | |
5842a27b | 8610 | (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object count, Lisp_Object string) |
8f924df7 | 8611 | { |
d311d28c | 8612 | EMACS_INT n; |
8f924df7 | 8613 | struct coding_system coding; |
7d64c6ad | 8614 | Lisp_Object attrs, charset_list, translation_table; |
8f924df7 | 8615 | Lisp_Object positions; |
d311d28c | 8616 | ptrdiff_t from, to; |
8f924df7 | 8617 | const unsigned char *p, *stop, *pend; |
f10fe38f | 8618 | bool ascii_compatible; |
fb88bf2d | 8619 | |
8f924df7 KH |
8620 | setup_coding_system (Fcheck_coding_system (coding_system), &coding); |
8621 | attrs = CODING_ID_ATTRS (coding.id); | |
8622 | if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text)) | |
8623 | return Qnil; | |
8624 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); | |
8625 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
2170c8f0 | 8626 | translation_table = get_translation_table (attrs, 1, NULL); |
fb88bf2d | 8627 | |
8f924df7 KH |
8628 | if (NILP (string)) |
8629 | { | |
8630 | validate_region (&start, &end); | |
8631 | from = XINT (start); | |
8632 | to = XINT (end); | |
4b4deea2 | 8633 | if (NILP (BVAR (current_buffer, enable_multibyte_characters)) |
8f924df7 KH |
8634 | || (ascii_compatible |
8635 | && (to - from) == (CHAR_TO_BYTE (to) - (CHAR_TO_BYTE (from))))) | |
8636 | return Qnil; | |
8637 | p = CHAR_POS_ADDR (from); | |
8638 | pend = CHAR_POS_ADDR (to); | |
8639 | if (from < GPT && to >= GPT) | |
8640 | stop = GPT_ADDR; | |
8641 | else | |
8642 | stop = pend; | |
8643 | } | |
8644 | else | |
8645 | { | |
8646 | CHECK_STRING (string); | |
8647 | CHECK_NATNUM (start); | |
8648 | CHECK_NATNUM (end); | |
d311d28c PE |
8649 | if (! (XINT (start) <= XINT (end) && XINT (end) <= SCHARS (string))) |
8650 | args_out_of_range_3 (string, start, end); | |
8f924df7 KH |
8651 | from = XINT (start); |
8652 | to = XINT (end); | |
8f924df7 KH |
8653 | if (! STRING_MULTIBYTE (string)) |
8654 | return Qnil; | |
8655 | p = SDATA (string) + string_char_to_byte (string, from); | |
8656 | stop = pend = SDATA (string) + string_char_to_byte (string, to); | |
8657 | if (ascii_compatible && (to - from) == (pend - p)) | |
8658 | return Qnil; | |
8659 | } | |
f2558efd | 8660 | |
8f924df7 KH |
8661 | if (NILP (count)) |
8662 | n = 1; | |
8663 | else | |
b73bfc1c | 8664 | { |
8f924df7 KH |
8665 | CHECK_NATNUM (count); |
8666 | n = XINT (count); | |
b73bfc1c KH |
8667 | } |
8668 | ||
8f924df7 | 8669 | positions = Qnil; |
3633e3aa | 8670 | charset_map_loaded = 0; |
8f924df7 | 8671 | while (1) |
d46c5b12 | 8672 | { |
8f924df7 | 8673 | int c; |
ec6d2bb8 | 8674 | |
8f924df7 KH |
8675 | if (ascii_compatible) |
8676 | while (p < stop && ASCII_BYTE_P (*p)) | |
8677 | p++, from++; | |
8678 | if (p >= stop) | |
0e79d667 | 8679 | { |
8f924df7 KH |
8680 | if (p >= pend) |
8681 | break; | |
8682 | stop = pend; | |
8683 | p = GAP_END_ADDR; | |
0e79d667 | 8684 | } |
ec6d2bb8 | 8685 | |
8f924df7 KH |
8686 | c = STRING_CHAR_ADVANCE (p); |
8687 | if (! (ASCII_CHAR_P (c) && ascii_compatible) | |
7d64c6ad KH |
8688 | && ! char_charset (translate_char (translation_table, c), |
8689 | charset_list, NULL)) | |
ec6d2bb8 | 8690 | { |
8f924df7 KH |
8691 | positions = Fcons (make_number (from), positions); |
8692 | n--; | |
8693 | if (n == 0) | |
8694 | break; | |
ec6d2bb8 KH |
8695 | } |
8696 | ||
8f924df7 | 8697 | from++; |
3633e3aa KH |
8698 | if (charset_map_loaded && NILP (string)) |
8699 | { | |
8700 | p = CHAR_POS_ADDR (from); | |
8701 | pend = CHAR_POS_ADDR (to); | |
8702 | if (from < GPT && to >= GPT) | |
8703 | stop = GPT_ADDR; | |
8704 | else | |
8705 | stop = pend; | |
8706 | charset_map_loaded = 0; | |
8707 | } | |
8f924df7 | 8708 | } |
d46c5b12 | 8709 | |
8f924df7 KH |
8710 | return (NILP (count) ? Fcar (positions) : Fnreverse (positions)); |
8711 | } | |
d46c5b12 | 8712 | |
d46c5b12 | 8713 | |
df7492f9 KH |
8714 | DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region, |
8715 | Scheck_coding_systems_region, 3, 3, 0, | |
8716 | doc: /* Check if the region is encodable by coding systems. | |
d46c5b12 | 8717 | |
df7492f9 KH |
8718 | START and END are buffer positions specifying the region. |
8719 | CODING-SYSTEM-LIST is a list of coding systems to check. | |
d46c5b12 | 8720 | |
df7492f9 | 8721 | The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where |
d4a1d553 | 8722 | CODING-SYSTEM is a member of CODING-SYSTEM-LIST and can't encode the |
df7492f9 KH |
8723 | whole region, POS0, POS1, ... are buffer positions where non-encodable |
8724 | characters are found. | |
93dec019 | 8725 | |
df7492f9 KH |
8726 | If all coding systems in CODING-SYSTEM-LIST can encode the region, the |
8727 | value is nil. | |
93dec019 | 8728 | |
df7492f9 KH |
8729 | START may be a string. In that case, check if the string is |
8730 | encodable, and the value contains indices to the string instead of | |
5704f39a KH |
8731 | buffer positions. END is ignored. |
8732 | ||
4c1958f4 | 8733 | If the current buffer (or START if it is a string) is unibyte, the value |
5704f39a | 8734 | is nil. */) |
5842a27b | 8735 | (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system_list) |
05e6f5dc | 8736 | { |
df7492f9 | 8737 | Lisp_Object list; |
d311d28c PE |
8738 | ptrdiff_t start_byte, end_byte; |
8739 | ptrdiff_t pos; | |
7c78e542 | 8740 | const unsigned char *p, *pbeg, *pend; |
df7492f9 | 8741 | int c; |
7d64c6ad | 8742 | Lisp_Object tail, elt, attrs; |
70ad9fc4 | 8743 | |
05e6f5dc KH |
8744 | if (STRINGP (start)) |
8745 | { | |
df7492f9 | 8746 | if (!STRING_MULTIBYTE (start) |
4c1958f4 | 8747 | || SCHARS (start) == SBYTES (start)) |
df7492f9 KH |
8748 | return Qnil; |
8749 | start_byte = 0; | |
8f924df7 | 8750 | end_byte = SBYTES (start); |
df7492f9 | 8751 | pos = 0; |
d46c5b12 | 8752 | } |
05e6f5dc | 8753 | else |
b73bfc1c | 8754 | { |
b7826503 PJ |
8755 | CHECK_NUMBER_COERCE_MARKER (start); |
8756 | CHECK_NUMBER_COERCE_MARKER (end); | |
05e6f5dc KH |
8757 | if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end)) |
8758 | args_out_of_range (start, end); | |
4b4deea2 | 8759 | if (NILP (BVAR (current_buffer, enable_multibyte_characters))) |
df7492f9 KH |
8760 | return Qnil; |
8761 | start_byte = CHAR_TO_BYTE (XINT (start)); | |
8762 | end_byte = CHAR_TO_BYTE (XINT (end)); | |
8763 | if (XINT (end) - XINT (start) == end_byte - start_byte) | |
5704f39a | 8764 | return Qnil; |
df7492f9 | 8765 | |
e1c23804 | 8766 | if (XINT (start) < GPT && XINT (end) > GPT) |
b73bfc1c | 8767 | { |
e1c23804 DL |
8768 | if ((GPT - XINT (start)) < (XINT (end) - GPT)) |
8769 | move_gap_both (XINT (start), start_byte); | |
df7492f9 | 8770 | else |
e1c23804 | 8771 | move_gap_both (XINT (end), end_byte); |
b73bfc1c | 8772 | } |
e1c23804 | 8773 | pos = XINT (start); |
b73bfc1c | 8774 | } |
7553d0e1 | 8775 | |
df7492f9 KH |
8776 | list = Qnil; |
8777 | for (tail = coding_system_list; CONSP (tail); tail = XCDR (tail)) | |
12410ef1 | 8778 | { |
df7492f9 | 8779 | elt = XCAR (tail); |
7d64c6ad | 8780 | attrs = AREF (CODING_SYSTEM_SPEC (elt), 0); |
2170c8f0 KH |
8781 | ASET (attrs, coding_attr_trans_tbl, |
8782 | get_translation_table (attrs, 1, NULL)); | |
7d64c6ad | 8783 | list = Fcons (Fcons (elt, Fcons (attrs, Qnil)), list); |
12410ef1 KH |
8784 | } |
8785 | ||
df7492f9 | 8786 | if (STRINGP (start)) |
8f924df7 | 8787 | p = pbeg = SDATA (start); |
72d1a715 | 8788 | else |
df7492f9 KH |
8789 | p = pbeg = BYTE_POS_ADDR (start_byte); |
8790 | pend = p + (end_byte - start_byte); | |
4ed46869 | 8791 | |
df7492f9 KH |
8792 | while (p < pend && ASCII_BYTE_P (*p)) p++, pos++; |
8793 | while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--; | |
ec6d2bb8 | 8794 | |
df7492f9 | 8795 | while (p < pend) |
d46c5b12 | 8796 | { |
df7492f9 KH |
8797 | if (ASCII_BYTE_P (*p)) |
8798 | p++; | |
e133c8fa | 8799 | else |
05e6f5dc | 8800 | { |
df7492f9 KH |
8801 | c = STRING_CHAR_ADVANCE (p); |
8802 | ||
8803 | charset_map_loaded = 0; | |
8804 | for (tail = list; CONSP (tail); tail = XCDR (tail)) | |
8805 | { | |
8806 | elt = XCDR (XCAR (tail)); | |
8807 | if (! char_encodable_p (c, XCAR (elt))) | |
8808 | XSETCDR (elt, Fcons (make_number (pos), XCDR (elt))); | |
8809 | } | |
8810 | if (charset_map_loaded) | |
8811 | { | |
d311d28c | 8812 | ptrdiff_t p_offset = p - pbeg, pend_offset = pend - pbeg; |
df7492f9 KH |
8813 | |
8814 | if (STRINGP (start)) | |
8f924df7 | 8815 | pbeg = SDATA (start); |
df7492f9 KH |
8816 | else |
8817 | pbeg = BYTE_POS_ADDR (start_byte); | |
8818 | p = pbeg + p_offset; | |
8819 | pend = pbeg + pend_offset; | |
8820 | } | |
05e6f5dc | 8821 | } |
df7492f9 | 8822 | pos++; |
d46c5b12 | 8823 | } |
4ed46869 | 8824 | |
df7492f9 KH |
8825 | tail = list; |
8826 | list = Qnil; | |
8827 | for (; CONSP (tail); tail = XCDR (tail)) | |
ec6d2bb8 | 8828 | { |
df7492f9 KH |
8829 | elt = XCAR (tail); |
8830 | if (CONSP (XCDR (XCDR (elt)))) | |
8831 | list = Fcons (Fcons (XCAR (elt), Fnreverse (XCDR (XCDR (elt)))), | |
8832 | list); | |
ec6d2bb8 | 8833 | } |
2b4f9037 | 8834 | |
df7492f9 | 8835 | return list; |
d46c5b12 KH |
8836 | } |
8837 | ||
3fd9494b | 8838 | |
74ab6df5 | 8839 | static Lisp_Object |
cf84bb53 JB |
8840 | code_convert_region (Lisp_Object start, Lisp_Object end, |
8841 | Lisp_Object coding_system, Lisp_Object dst_object, | |
f10fe38f | 8842 | bool encodep, bool norecord) |
4ed46869 | 8843 | { |
3a73fa5d | 8844 | struct coding_system coding; |
d311d28c | 8845 | ptrdiff_t from, from_byte, to, to_byte; |
df7492f9 | 8846 | Lisp_Object src_object; |
4ed46869 | 8847 | |
df7492f9 KH |
8848 | if (NILP (coding_system)) |
8849 | coding_system = Qno_conversion; | |
8850 | else | |
8851 | CHECK_CODING_SYSTEM (coding_system); | |
8852 | src_object = Fcurrent_buffer (); | |
8853 | if (NILP (dst_object)) | |
8854 | dst_object = src_object; | |
8855 | else if (! EQ (dst_object, Qt)) | |
8856 | CHECK_BUFFER (dst_object); | |
3a73fa5d | 8857 | |
d46c5b12 KH |
8858 | validate_region (&start, &end); |
8859 | from = XFASTINT (start); | |
df7492f9 | 8860 | from_byte = CHAR_TO_BYTE (from); |
d46c5b12 | 8861 | to = XFASTINT (end); |
df7492f9 | 8862 | to_byte = CHAR_TO_BYTE (to); |
764ca8da | 8863 | |
df7492f9 KH |
8864 | setup_coding_system (coding_system, &coding); |
8865 | coding.mode |= CODING_MODE_LAST_BLOCK; | |
ec6d2bb8 | 8866 | |
df7492f9 KH |
8867 | if (encodep) |
8868 | encode_coding_object (&coding, src_object, from, from_byte, to, to_byte, | |
8869 | dst_object); | |
8870 | else | |
8871 | decode_coding_object (&coding, src_object, from, from_byte, to, to_byte, | |
8872 | dst_object); | |
8873 | if (! norecord) | |
8874 | Vlast_coding_system_used = CODING_ID_NAME (coding.id); | |
ec6d2bb8 | 8875 | |
df7492f9 KH |
8876 | return (BUFFERP (dst_object) |
8877 | ? make_number (coding.produced_char) | |
8878 | : coding.dst_object); | |
4031e2bf | 8879 | } |
78108bcd | 8880 | |
4ed46869 | 8881 | |
4031e2bf | 8882 | DEFUN ("decode-coding-region", Fdecode_coding_region, Sdecode_coding_region, |
df7492f9 | 8883 | 3, 4, "r\nzCoding system: ", |
48b0f3ae | 8884 | doc: /* Decode the current region from the specified coding system. |
df7492f9 KH |
8885 | When called from a program, takes four arguments: |
8886 | START, END, CODING-SYSTEM, and DESTINATION. | |
8887 | START and END are buffer positions. | |
8844fa83 | 8888 | |
df7492f9 | 8889 | Optional 4th arguments DESTINATION specifies where the decoded text goes. |
2354b80b | 8890 | If nil, the region between START and END is replaced by the decoded text. |
1560f91a EZ |
8891 | If buffer, the decoded text is inserted in that buffer after point (point |
8892 | does not move). | |
446dcd75 | 8893 | In those cases, the length of the decoded text is returned. |
319a3947 | 8894 | If DESTINATION is t, the decoded text is returned. |
8844fa83 | 8895 | |
48b0f3ae PJ |
8896 | This function sets `last-coding-system-used' to the precise coding system |
8897 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
319a3947 | 8898 | not fully specified.) */) |
5842a27b | 8899 | (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object destination) |
4031e2bf | 8900 | { |
df7492f9 | 8901 | return code_convert_region (start, end, coding_system, destination, 0, 0); |
3a73fa5d | 8902 | } |
8844fa83 | 8903 | |
3a73fa5d | 8904 | DEFUN ("encode-coding-region", Fencode_coding_region, Sencode_coding_region, |
df7492f9 KH |
8905 | 3, 4, "r\nzCoding system: ", |
8906 | doc: /* Encode the current region by specified coding system. | |
d4a1d553 JB |
8907 | When called from a program, takes four arguments: |
8908 | START, END, CODING-SYSTEM and DESTINATION. | |
8909 | START and END are buffer positions. | |
d46c5b12 | 8910 | |
df7492f9 KH |
8911 | Optional 4th arguments DESTINATION specifies where the encoded text goes. |
8912 | If nil, the region between START and END is replace by the encoded text. | |
1560f91a EZ |
8913 | If buffer, the encoded text is inserted in that buffer after point (point |
8914 | does not move). | |
446dcd75 | 8915 | In those cases, the length of the encoded text is returned. |
319a3947 | 8916 | If DESTINATION is t, the encoded text is returned. |
2391eaa4 | 8917 | |
48b0f3ae PJ |
8918 | This function sets `last-coding-system-used' to the precise coding system |
8919 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
319a3947 | 8920 | not fully specified.) */) |
5842a27b | 8921 | (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object destination) |
3a73fa5d | 8922 | { |
df7492f9 | 8923 | return code_convert_region (start, end, coding_system, destination, 1, 0); |
b73bfc1c KH |
8924 | } |
8925 | ||
8926 | Lisp_Object | |
6f704c76 | 8927 | code_convert_string (Lisp_Object string, Lisp_Object coding_system, |
f10fe38f PE |
8928 | Lisp_Object dst_object, bool encodep, bool nocopy, |
8929 | bool norecord) | |
b73bfc1c | 8930 | { |
4031e2bf | 8931 | struct coding_system coding; |
d311d28c | 8932 | ptrdiff_t chars, bytes; |
ec6d2bb8 | 8933 | |
b7826503 | 8934 | CHECK_STRING (string); |
d46c5b12 | 8935 | if (NILP (coding_system)) |
4956c225 | 8936 | { |
df7492f9 KH |
8937 | if (! norecord) |
8938 | Vlast_coding_system_used = Qno_conversion; | |
8939 | if (NILP (dst_object)) | |
8940 | return (nocopy ? Fcopy_sequence (string) : string); | |
4956c225 | 8941 | } |
b73bfc1c | 8942 | |
df7492f9 KH |
8943 | if (NILP (coding_system)) |
8944 | coding_system = Qno_conversion; | |
8945 | else | |
8946 | CHECK_CODING_SYSTEM (coding_system); | |
8947 | if (NILP (dst_object)) | |
8948 | dst_object = Qt; | |
8949 | else if (! EQ (dst_object, Qt)) | |
8950 | CHECK_BUFFER (dst_object); | |
73be902c | 8951 | |
df7492f9 | 8952 | setup_coding_system (coding_system, &coding); |
d46c5b12 | 8953 | coding.mode |= CODING_MODE_LAST_BLOCK; |
8f924df7 KH |
8954 | chars = SCHARS (string); |
8955 | bytes = SBYTES (string); | |
df7492f9 KH |
8956 | if (encodep) |
8957 | encode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object); | |
8958 | else | |
8959 | decode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object); | |
8960 | if (! norecord) | |
8961 | Vlast_coding_system_used = CODING_ID_NAME (coding.id); | |
73be902c | 8962 | |
df7492f9 KH |
8963 | return (BUFFERP (dst_object) |
8964 | ? make_number (coding.produced_char) | |
8965 | : coding.dst_object); | |
4ed46869 | 8966 | } |
73be902c | 8967 | |
b73bfc1c | 8968 | |
ecec61c1 | 8969 | /* Encode or decode STRING according to CODING_SYSTEM. |
ec6d2bb8 | 8970 | Do not set Vlast_coding_system_used. |
4ed46869 | 8971 | |
ec6d2bb8 KH |
8972 | This function is called only from macros DECODE_FILE and |
8973 | ENCODE_FILE, thus we ignore character composition. */ | |
4ed46869 | 8974 | |
ecec61c1 | 8975 | Lisp_Object |
cf84bb53 | 8976 | code_convert_string_norecord (Lisp_Object string, Lisp_Object coding_system, |
f10fe38f | 8977 | bool encodep) |
4ed46869 | 8978 | { |
0be8721c | 8979 | return code_convert_string (string, coding_system, Qt, encodep, 0, 1); |
4ed46869 KH |
8980 | } |
8981 | ||
4ed46869 | 8982 | |
a7ca3326 | 8983 | DEFUN ("decode-coding-string", Fdecode_coding_string, Sdecode_coding_string, |
df7492f9 KH |
8984 | 2, 4, 0, |
8985 | doc: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result. | |
8986 | ||
8987 | Optional third arg NOCOPY non-nil means it is OK to return STRING itself | |
8988 | if the decoding operation is trivial. | |
ecec61c1 | 8989 | |
d4a1d553 | 8990 | Optional fourth arg BUFFER non-nil means that the decoded text is |
1560f91a EZ |
8991 | inserted in that buffer after point (point does not move). In this |
8992 | case, the return value is the length of the decoded text. | |
ecec61c1 | 8993 | |
df7492f9 KH |
8994 | This function sets `last-coding-system-used' to the precise coding system |
8995 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
d4a1d553 | 8996 | not fully specified.) */) |
5842a27b | 8997 | (Lisp_Object string, Lisp_Object coding_system, Lisp_Object nocopy, Lisp_Object buffer) |
4ed46869 | 8998 | { |
df7492f9 KH |
8999 | return code_convert_string (string, coding_system, buffer, |
9000 | 0, ! NILP (nocopy), 0); | |
4ed46869 KH |
9001 | } |
9002 | ||
df7492f9 KH |
9003 | DEFUN ("encode-coding-string", Fencode_coding_string, Sencode_coding_string, |
9004 | 2, 4, 0, | |
9005 | doc: /* Encode STRING to CODING-SYSTEM, and return the result. | |
9006 | ||
9007 | Optional third arg NOCOPY non-nil means it is OK to return STRING | |
9008 | itself if the encoding operation is trivial. | |
9009 | ||
d4a1d553 | 9010 | Optional fourth arg BUFFER non-nil means that the encoded text is |
1560f91a EZ |
9011 | inserted in that buffer after point (point does not move). In this |
9012 | case, the return value is the length of the encoded text. | |
df7492f9 KH |
9013 | |
9014 | This function sets `last-coding-system-used' to the precise coding system | |
9015 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
9016 | not fully specified.) */) | |
5842a27b | 9017 | (Lisp_Object string, Lisp_Object coding_system, Lisp_Object nocopy, Lisp_Object buffer) |
4ed46869 | 9018 | { |
df7492f9 | 9019 | return code_convert_string (string, coding_system, buffer, |
4550efdf | 9020 | 1, ! NILP (nocopy), 0); |
4ed46869 | 9021 | } |
df7492f9 | 9022 | |
3a73fa5d | 9023 | \f |
4ed46869 | 9024 | DEFUN ("decode-sjis-char", Fdecode_sjis_char, Sdecode_sjis_char, 1, 1, 0, |
48b0f3ae PJ |
9025 | doc: /* Decode a Japanese character which has CODE in shift_jis encoding. |
9026 | Return the corresponding character. */) | |
5842a27b | 9027 | (Lisp_Object code) |
4ed46869 | 9028 | { |
df7492f9 KH |
9029 | Lisp_Object spec, attrs, val; |
9030 | struct charset *charset_roman, *charset_kanji, *charset_kana, *charset; | |
5fdb398c PE |
9031 | EMACS_INT ch; |
9032 | int c; | |
4ed46869 | 9033 | |
df7492f9 | 9034 | CHECK_NATNUM (code); |
5fdb398c | 9035 | ch = XFASTINT (code); |
df7492f9 KH |
9036 | CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec); |
9037 | attrs = AREF (spec, 0); | |
4ed46869 | 9038 | |
5fdb398c | 9039 | if (ASCII_BYTE_P (ch) |
df7492f9 KH |
9040 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) |
9041 | return code; | |
4ed46869 | 9042 | |
df7492f9 KH |
9043 | val = CODING_ATTR_CHARSET_LIST (attrs); |
9044 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
004068e4 KH |
9045 | charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
9046 | charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))); | |
fa42c37f | 9047 | |
5fdb398c PE |
9048 | if (ch <= 0x7F) |
9049 | { | |
9050 | c = ch; | |
9051 | charset = charset_roman; | |
9052 | } | |
9053 | else if (ch >= 0xA0 && ch < 0xDF) | |
55ab7be3 | 9054 | { |
5fdb398c | 9055 | c = ch - 0x80; |
df7492f9 | 9056 | charset = charset_kana; |
4ed46869 | 9057 | } |
55ab7be3 | 9058 | else |
4ed46869 | 9059 | { |
5fdb398c PE |
9060 | EMACS_INT c1 = ch >> 8; |
9061 | int c2 = ch & 0xFF; | |
df7492f9 | 9062 | |
2735d060 PE |
9063 | if (c1 < 0x81 || (c1 > 0x9F && c1 < 0xE0) || c1 > 0xEF |
9064 | || c2 < 0x40 || c2 == 0x7F || c2 > 0xFC) | |
c2982e87 | 9065 | error ("Invalid code: %"pI"d", ch); |
5fdb398c | 9066 | c = ch; |
df7492f9 KH |
9067 | SJIS_TO_JIS (c); |
9068 | charset = charset_kanji; | |
4ed46869 | 9069 | } |
df7492f9 KH |
9070 | c = DECODE_CHAR (charset, c); |
9071 | if (c < 0) | |
c2982e87 | 9072 | error ("Invalid code: %"pI"d", ch); |
df7492f9 | 9073 | return make_number (c); |
93dec019 | 9074 | } |
4ed46869 | 9075 | |
48b0f3ae | 9076 | |
4ed46869 | 9077 | DEFUN ("encode-sjis-char", Fencode_sjis_char, Sencode_sjis_char, 1, 1, 0, |
8acf0c0e | 9078 | doc: /* Encode a Japanese character CH to shift_jis encoding. |
48b0f3ae | 9079 | Return the corresponding code in SJIS. */) |
5842a27b | 9080 | (Lisp_Object ch) |
4ed46869 | 9081 | { |
df7492f9 KH |
9082 | Lisp_Object spec, attrs, charset_list; |
9083 | int c; | |
9084 | struct charset *charset; | |
9085 | unsigned code; | |
48b0f3ae | 9086 | |
df7492f9 KH |
9087 | CHECK_CHARACTER (ch); |
9088 | c = XFASTINT (ch); | |
9089 | CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec); | |
9090 | attrs = AREF (spec, 0); | |
9091 | ||
9092 | if (ASCII_CHAR_P (c) | |
9093 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
9094 | return ch; | |
9095 | ||
9096 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
9097 | charset = char_charset (c, charset_list, &code); | |
9098 | if (code == CHARSET_INVALID_CODE (charset)) | |
e6c3da20 | 9099 | error ("Can't encode by shift_jis encoding: %c", c); |
df7492f9 KH |
9100 | JIS_TO_SJIS (code); |
9101 | ||
9102 | return make_number (code); | |
4ed46869 KH |
9103 | } |
9104 | ||
9105 | DEFUN ("decode-big5-char", Fdecode_big5_char, Sdecode_big5_char, 1, 1, 0, | |
48b0f3ae PJ |
9106 | doc: /* Decode a Big5 character which has CODE in BIG5 coding system. |
9107 | Return the corresponding character. */) | |
5842a27b | 9108 | (Lisp_Object code) |
d46c5b12 | 9109 | { |
df7492f9 KH |
9110 | Lisp_Object spec, attrs, val; |
9111 | struct charset *charset_roman, *charset_big5, *charset; | |
5fdb398c | 9112 | EMACS_INT ch; |
df7492f9 | 9113 | int c; |
6289dd10 | 9114 | |
df7492f9 | 9115 | CHECK_NATNUM (code); |
5fdb398c | 9116 | ch = XFASTINT (code); |
df7492f9 KH |
9117 | CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec); |
9118 | attrs = AREF (spec, 0); | |
4ed46869 | 9119 | |
5fdb398c | 9120 | if (ASCII_BYTE_P (ch) |
df7492f9 KH |
9121 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) |
9122 | return code; | |
6289dd10 | 9123 | |
df7492f9 KH |
9124 | val = CODING_ATTR_CHARSET_LIST (attrs); |
9125 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
9126 | charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val))); | |
c210f766 | 9127 | |
5fdb398c PE |
9128 | if (ch <= 0x7F) |
9129 | { | |
9130 | c = ch; | |
9131 | charset = charset_roman; | |
9132 | } | |
c28a9453 KH |
9133 | else |
9134 | { | |
5fdb398c PE |
9135 | EMACS_INT b1 = ch >> 8; |
9136 | int b2 = ch & 0x7F; | |
df7492f9 KH |
9137 | if (b1 < 0xA1 || b1 > 0xFE |
9138 | || b2 < 0x40 || (b2 > 0x7E && b2 < 0xA1) || b2 > 0xFE) | |
c2982e87 | 9139 | error ("Invalid code: %"pI"d", ch); |
5fdb398c | 9140 | c = ch; |
df7492f9 | 9141 | charset = charset_big5; |
c28a9453 | 9142 | } |
5fdb398c | 9143 | c = DECODE_CHAR (charset, c); |
df7492f9 | 9144 | if (c < 0) |
c2982e87 | 9145 | error ("Invalid code: %"pI"d", ch); |
df7492f9 | 9146 | return make_number (c); |
d46c5b12 | 9147 | } |
6289dd10 | 9148 | |
4ed46869 | 9149 | DEFUN ("encode-big5-char", Fencode_big5_char, Sencode_big5_char, 1, 1, 0, |
8acf0c0e | 9150 | doc: /* Encode the Big5 character CH to BIG5 coding system. |
48b0f3ae | 9151 | Return the corresponding character code in Big5. */) |
5842a27b | 9152 | (Lisp_Object ch) |
4ed46869 | 9153 | { |
df7492f9 KH |
9154 | Lisp_Object spec, attrs, charset_list; |
9155 | struct charset *charset; | |
9156 | int c; | |
9157 | unsigned code; | |
9158 | ||
9159 | CHECK_CHARACTER (ch); | |
9160 | c = XFASTINT (ch); | |
9161 | CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec); | |
9162 | attrs = AREF (spec, 0); | |
9163 | if (ASCII_CHAR_P (c) | |
9164 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
9165 | return ch; | |
9166 | ||
9167 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
9168 | charset = char_charset (c, charset_list, &code); | |
9169 | if (code == CHARSET_INVALID_CODE (charset)) | |
e6c3da20 | 9170 | error ("Can't encode by Big5 encoding: %c", c); |
df7492f9 KH |
9171 | |
9172 | return make_number (code); | |
4ed46869 | 9173 | } |
48b0f3ae | 9174 | |
3a73fa5d | 9175 | \f |
002fdb44 | 9176 | DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal, |
68bba4e4 | 9177 | Sset_terminal_coding_system_internal, 1, 2, 0, |
48b0f3ae | 9178 | doc: /* Internal use only. */) |
5842a27b | 9179 | (Lisp_Object coding_system, Lisp_Object terminal) |
4ed46869 | 9180 | { |
b18fad6d KH |
9181 | struct terminal *term = get_terminal (terminal, 1); |
9182 | struct coding_system *terminal_coding = TERMINAL_TERMINAL_CODING (term); | |
b7826503 | 9183 | CHECK_SYMBOL (coding_system); |
b8299c66 | 9184 | setup_coding_system (Fcheck_coding_system (coding_system), terminal_coding); |
70c22245 | 9185 | /* We had better not send unsafe characters to terminal. */ |
c73bd236 | 9186 | terminal_coding->mode |= CODING_MODE_SAFE_ENCODING; |
ad1746f5 | 9187 | /* Character composition should be disabled. */ |
c73bd236 | 9188 | terminal_coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK; |
b8299c66 KL |
9189 | terminal_coding->src_multibyte = 1; |
9190 | terminal_coding->dst_multibyte = 0; | |
3f22b86f PE |
9191 | tset_charset_list |
9192 | (term, (terminal_coding->common_flags & CODING_REQUIRE_ENCODING_MASK | |
9193 | ? coding_charset_list (terminal_coding) | |
9194 | : Fcons (make_number (charset_ascii), Qnil))); | |
4ed46869 KH |
9195 | return Qnil; |
9196 | } | |
9197 | ||
c4825358 KH |
9198 | DEFUN ("set-safe-terminal-coding-system-internal", |
9199 | Fset_safe_terminal_coding_system_internal, | |
48b0f3ae | 9200 | Sset_safe_terminal_coding_system_internal, 1, 1, 0, |
ddb67bdc | 9201 | doc: /* Internal use only. */) |
5842a27b | 9202 | (Lisp_Object coding_system) |
d46c5b12 | 9203 | { |
b7826503 | 9204 | CHECK_SYMBOL (coding_system); |
c4825358 KH |
9205 | setup_coding_system (Fcheck_coding_system (coding_system), |
9206 | &safe_terminal_coding); | |
ad1746f5 | 9207 | /* Character composition should be disabled. */ |
df7492f9 | 9208 | safe_terminal_coding.common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK; |
b73bfc1c KH |
9209 | safe_terminal_coding.src_multibyte = 1; |
9210 | safe_terminal_coding.dst_multibyte = 0; | |
c4825358 KH |
9211 | return Qnil; |
9212 | } | |
4ed46869 | 9213 | |
002fdb44 | 9214 | DEFUN ("terminal-coding-system", Fterminal_coding_system, |
68bba4e4 | 9215 | Sterminal_coding_system, 0, 1, 0, |
6ed8eeff | 9216 | doc: /* Return coding system specified for terminal output on the given terminal. |
708e05dc | 9217 | TERMINAL may be a terminal object, a frame, or nil for the selected |
6ed8eeff | 9218 | frame's terminal device. */) |
5842a27b | 9219 | (Lisp_Object terminal) |
4ed46869 | 9220 | { |
985773c9 MB |
9221 | struct coding_system *terminal_coding |
9222 | = TERMINAL_TERMINAL_CODING (get_terminal (terminal, 1)); | |
9223 | Lisp_Object coding_system = CODING_ID_NAME (terminal_coding->id); | |
ae6f73fa | 9224 | |
6d5eb5b0 | 9225 | /* For backward compatibility, return nil if it is `undecided'. */ |
f75c90a9 | 9226 | return (! EQ (coding_system, Qundecided) ? coding_system : Qnil); |
4ed46869 KH |
9227 | } |
9228 | ||
002fdb44 | 9229 | DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal, |
68bba4e4 | 9230 | Sset_keyboard_coding_system_internal, 1, 2, 0, |
48b0f3ae | 9231 | doc: /* Internal use only. */) |
5842a27b | 9232 | (Lisp_Object coding_system, Lisp_Object terminal) |
4ed46869 | 9233 | { |
6ed8eeff | 9234 | struct terminal *t = get_terminal (terminal, 1); |
b7826503 | 9235 | CHECK_SYMBOL (coding_system); |
624bda09 KH |
9236 | if (NILP (coding_system)) |
9237 | coding_system = Qno_conversion; | |
9238 | else | |
9239 | Fcheck_coding_system (coding_system); | |
9240 | setup_coding_system (coding_system, TERMINAL_KEYBOARD_CODING (t)); | |
ad1746f5 | 9241 | /* Character composition should be disabled. */ |
c73bd236 MB |
9242 | TERMINAL_KEYBOARD_CODING (t)->common_flags |
9243 | &= ~CODING_ANNOTATE_COMPOSITION_MASK; | |
4ed46869 KH |
9244 | return Qnil; |
9245 | } | |
9246 | ||
9247 | DEFUN ("keyboard-coding-system", | |
985773c9 | 9248 | Fkeyboard_coding_system, Skeyboard_coding_system, 0, 1, 0, |
48b0f3ae | 9249 | doc: /* Return coding system specified for decoding keyboard input. */) |
5842a27b | 9250 | (Lisp_Object terminal) |
4ed46869 | 9251 | { |
985773c9 MB |
9252 | return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING |
9253 | (get_terminal (terminal, 1))->id); | |
4ed46869 KH |
9254 | } |
9255 | ||
4ed46869 | 9256 | \f |
a7ca3326 | 9257 | DEFUN ("find-operation-coding-system", Ffind_operation_coding_system, |
a5d301df | 9258 | Sfind_operation_coding_system, 1, MANY, 0, |
48b0f3ae PJ |
9259 | doc: /* Choose a coding system for an operation based on the target name. |
9260 | The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM). | |
9261 | DECODING-SYSTEM is the coding system to use for decoding | |
9262 | \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system | |
9263 | for encoding (in case OPERATION does encoding). | |
05e6f5dc | 9264 | |
48b0f3ae PJ |
9265 | The first argument OPERATION specifies an I/O primitive: |
9266 | For file I/O, `insert-file-contents' or `write-region'. | |
9267 | For process I/O, `call-process', `call-process-region', or `start-process'. | |
9268 | For network I/O, `open-network-stream'. | |
05e6f5dc | 9269 | |
48b0f3ae PJ |
9270 | The remaining arguments should be the same arguments that were passed |
9271 | to the primitive. Depending on which primitive, one of those arguments | |
9272 | is selected as the TARGET. For example, if OPERATION does file I/O, | |
9273 | whichever argument specifies the file name is TARGET. | |
05e6f5dc | 9274 | |
48b0f3ae | 9275 | TARGET has a meaning which depends on OPERATION: |
b883cdb2 | 9276 | For file I/O, TARGET is a file name (except for the special case below). |
48b0f3ae | 9277 | For process I/O, TARGET is a process name. |
d4a1d553 | 9278 | For network I/O, TARGET is a service name or a port number. |
05e6f5dc | 9279 | |
d4a1d553 | 9280 | This function looks up what is specified for TARGET in |
48b0f3ae PJ |
9281 | `file-coding-system-alist', `process-coding-system-alist', |
9282 | or `network-coding-system-alist' depending on OPERATION. | |
9283 | They may specify a coding system, a cons of coding systems, | |
9284 | or a function symbol to call. | |
9285 | In the last case, we call the function with one argument, | |
9286 | which is a list of all the arguments given to this function. | |
1011c487 MB |
9287 | If the function can't decide a coding system, it can return |
9288 | `undecided' so that the normal code-detection is performed. | |
48b0f3ae | 9289 | |
b883cdb2 MB |
9290 | If OPERATION is `insert-file-contents', the argument corresponding to |
9291 | TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a | |
9292 | file name to look up, and BUFFER is a buffer that contains the file's | |
9293 | contents (not yet decoded). If `file-coding-system-alist' specifies a | |
9294 | function to call for FILENAME, that function should examine the | |
9295 | contents of BUFFER instead of reading the file. | |
9296 | ||
d918f936 | 9297 | usage: (find-operation-coding-system OPERATION ARGUMENTS...) */) |
f66c7cf8 | 9298 | (ptrdiff_t nargs, Lisp_Object *args) |
6b89e3aa | 9299 | { |
4ed46869 KH |
9300 | Lisp_Object operation, target_idx, target, val; |
9301 | register Lisp_Object chain; | |
177c0ea7 | 9302 | |
4ed46869 KH |
9303 | if (nargs < 2) |
9304 | error ("Too few arguments"); | |
9305 | operation = args[0]; | |
9306 | if (!SYMBOLP (operation) | |
d311d28c | 9307 | || (target_idx = Fget (operation, Qtarget_idx), !NATNUMP (target_idx))) |
3ed051d4 | 9308 | error ("Invalid first argument"); |
7b09a37a | 9309 | if (nargs <= 1 + XFASTINT (target_idx)) |
94dcfacf | 9310 | error ("Too few arguments for operation `%s'", |
8f924df7 | 9311 | SDATA (SYMBOL_NAME (operation))); |
c5101a77 | 9312 | target = args[XFASTINT (target_idx) + 1]; |
4ed46869 | 9313 | if (!(STRINGP (target) |
091a0ff0 KH |
9314 | || (EQ (operation, Qinsert_file_contents) && CONSP (target) |
9315 | && STRINGP (XCAR (target)) && BUFFERP (XCDR (target))) | |
4ed46869 | 9316 | || (EQ (operation, Qopen_network_stream) && INTEGERP (target)))) |
94dcfacf EZ |
9317 | error ("Invalid argument %"pI"d of operation `%s'", |
9318 | XFASTINT (target_idx) + 1, SDATA (SYMBOL_NAME (operation))); | |
091a0ff0 KH |
9319 | if (CONSP (target)) |
9320 | target = XCAR (target); | |
4ed46869 | 9321 | |
2e34157c RS |
9322 | chain = ((EQ (operation, Qinsert_file_contents) |
9323 | || EQ (operation, Qwrite_region)) | |
02ba4723 | 9324 | ? Vfile_coding_system_alist |
2e34157c | 9325 | : (EQ (operation, Qopen_network_stream) |
02ba4723 KH |
9326 | ? Vnetwork_coding_system_alist |
9327 | : Vprocess_coding_system_alist)); | |
4ed46869 KH |
9328 | if (NILP (chain)) |
9329 | return Qnil; | |
9330 | ||
03699b14 | 9331 | for (; CONSP (chain); chain = XCDR (chain)) |
6b89e3aa | 9332 | { |
f44d27ce | 9333 | Lisp_Object elt; |
6b89e3aa | 9334 | |
df7492f9 | 9335 | elt = XCAR (chain); |
4ed46869 KH |
9336 | if (CONSP (elt) |
9337 | && ((STRINGP (target) | |
03699b14 KR |
9338 | && STRINGP (XCAR (elt)) |
9339 | && fast_string_match (XCAR (elt), target) >= 0) | |
9340 | || (INTEGERP (target) && EQ (target, XCAR (elt))))) | |
6b89e3aa | 9341 | { |
03699b14 | 9342 | val = XCDR (elt); |
b19fd4c5 KH |
9343 | /* Here, if VAL is both a valid coding system and a valid |
9344 | function symbol, we return VAL as a coding system. */ | |
02ba4723 KH |
9345 | if (CONSP (val)) |
9346 | return val; | |
9347 | if (! SYMBOLP (val)) | |
9348 | return Qnil; | |
9349 | if (! NILP (Fcoding_system_p (val))) | |
9350 | return Fcons (val, val); | |
b19fd4c5 | 9351 | if (! NILP (Ffboundp (val))) |
6b89e3aa | 9352 | { |
e2b97060 MB |
9353 | /* We use call1 rather than safe_call1 |
9354 | so as to get bug reports about functions called here | |
9355 | which don't handle the current interface. */ | |
9356 | val = call1 (val, Flist (nargs, args)); | |
b19fd4c5 KH |
9357 | if (CONSP (val)) |
9358 | return val; | |
9359 | if (SYMBOLP (val) && ! NILP (Fcoding_system_p (val))) | |
9360 | return Fcons (val, val); | |
6b89e3aa | 9361 | } |
02ba4723 | 9362 | return Qnil; |
6b89e3aa KH |
9363 | } |
9364 | } | |
4ed46869 | 9365 | return Qnil; |
6b89e3aa KH |
9366 | } |
9367 | ||
df7492f9 | 9368 | DEFUN ("set-coding-system-priority", Fset_coding_system_priority, |
a3f6ee6d | 9369 | Sset_coding_system_priority, 0, MANY, 0, |
da7db224 | 9370 | doc: /* Assign higher priority to the coding systems given as arguments. |
d4a1d553 | 9371 | If multiple coding systems belong to the same category, |
a3181084 DL |
9372 | all but the first one are ignored. |
9373 | ||
d4a1d553 | 9374 | usage: (set-coding-system-priority &rest coding-systems) */) |
f66c7cf8 | 9375 | (ptrdiff_t nargs, Lisp_Object *args) |
df7492f9 | 9376 | { |
f66c7cf8 | 9377 | ptrdiff_t i, j; |
f10fe38f | 9378 | bool changed[coding_category_max]; |
df7492f9 KH |
9379 | enum coding_category priorities[coding_category_max]; |
9380 | ||
72af86bd | 9381 | memset (changed, 0, sizeof changed); |
6b89e3aa | 9382 | |
df7492f9 | 9383 | for (i = j = 0; i < nargs; i++) |
6b89e3aa | 9384 | { |
df7492f9 KH |
9385 | enum coding_category category; |
9386 | Lisp_Object spec, attrs; | |
6b89e3aa | 9387 | |
df7492f9 KH |
9388 | CHECK_CODING_SYSTEM_GET_SPEC (args[i], spec); |
9389 | attrs = AREF (spec, 0); | |
9390 | category = XINT (CODING_ATTR_CATEGORY (attrs)); | |
9391 | if (changed[category]) | |
9392 | /* Ignore this coding system because a coding system of the | |
9393 | same category already had a higher priority. */ | |
9394 | continue; | |
9395 | changed[category] = 1; | |
9396 | priorities[j++] = category; | |
9397 | if (coding_categories[category].id >= 0 | |
9398 | && ! EQ (args[i], CODING_ID_NAME (coding_categories[category].id))) | |
9399 | setup_coding_system (args[i], &coding_categories[category]); | |
ff563fce | 9400 | Fset (AREF (Vcoding_category_table, category), args[i]); |
df7492f9 | 9401 | } |
6b89e3aa | 9402 | |
df7492f9 KH |
9403 | /* Now we have decided top J priorities. Reflect the order of the |
9404 | original priorities to the remaining priorities. */ | |
6b89e3aa | 9405 | |
df7492f9 | 9406 | for (i = j, j = 0; i < coding_category_max; i++, j++) |
6b89e3aa | 9407 | { |
df7492f9 KH |
9408 | while (j < coding_category_max |
9409 | && changed[coding_priorities[j]]) | |
9410 | j++; | |
9411 | if (j == coding_category_max) | |
1088b922 | 9412 | emacs_abort (); |
df7492f9 KH |
9413 | priorities[i] = coding_priorities[j]; |
9414 | } | |
6b89e3aa | 9415 | |
72af86bd | 9416 | memcpy (coding_priorities, priorities, sizeof priorities); |
177c0ea7 | 9417 | |
ff563fce KH |
9418 | /* Update `coding-category-list'. */ |
9419 | Vcoding_category_list = Qnil; | |
c5101a77 | 9420 | for (i = coding_category_max; i-- > 0; ) |
ff563fce KH |
9421 | Vcoding_category_list |
9422 | = Fcons (AREF (Vcoding_category_table, priorities[i]), | |
9423 | Vcoding_category_list); | |
6b89e3aa | 9424 | |
df7492f9 | 9425 | return Qnil; |
6b89e3aa KH |
9426 | } |
9427 | ||
df7492f9 KH |
9428 | DEFUN ("coding-system-priority-list", Fcoding_system_priority_list, |
9429 | Scoding_system_priority_list, 0, 1, 0, | |
da7db224 | 9430 | doc: /* Return a list of coding systems ordered by their priorities. |
b811c52b KH |
9431 | The list contains a subset of coding systems; i.e. coding systems |
9432 | assigned to each coding category (see `coding-category-list'). | |
9433 | ||
da7db224 | 9434 | HIGHESTP non-nil means just return the highest priority one. */) |
5842a27b | 9435 | (Lisp_Object highestp) |
d46c5b12 KH |
9436 | { |
9437 | int i; | |
df7492f9 | 9438 | Lisp_Object val; |
6b89e3aa | 9439 | |
df7492f9 | 9440 | for (i = 0, val = Qnil; i < coding_category_max; i++) |
d46c5b12 | 9441 | { |
df7492f9 KH |
9442 | enum coding_category category = coding_priorities[i]; |
9443 | int id = coding_categories[category].id; | |
9444 | Lisp_Object attrs; | |
068a9dbd | 9445 | |
df7492f9 KH |
9446 | if (id < 0) |
9447 | continue; | |
9448 | attrs = CODING_ID_ATTRS (id); | |
9449 | if (! NILP (highestp)) | |
9450 | return CODING_ATTR_BASE_NAME (attrs); | |
9451 | val = Fcons (CODING_ATTR_BASE_NAME (attrs), val); | |
9452 | } | |
9453 | return Fnreverse (val); | |
9454 | } | |
068a9dbd | 9455 | |
91433552 | 9456 | static const char *const suffixes[] = { "-unix", "-dos", "-mac" }; |
068a9dbd KH |
9457 | |
9458 | static Lisp_Object | |
971de7fb | 9459 | make_subsidiaries (Lisp_Object base) |
068a9dbd | 9460 | { |
df7492f9 | 9461 | Lisp_Object subsidiaries; |
1bfdaf10 | 9462 | ptrdiff_t base_name_len = SBYTES (SYMBOL_NAME (base)); |
38182d90 | 9463 | char *buf = alloca (base_name_len + 6); |
df7492f9 | 9464 | int i; |
068a9dbd | 9465 | |
72af86bd | 9466 | memcpy (buf, SDATA (SYMBOL_NAME (base)), base_name_len); |
25721f5b | 9467 | subsidiaries = make_uninit_vector (3); |
df7492f9 | 9468 | for (i = 0; i < 3; i++) |
068a9dbd | 9469 | { |
1bfdaf10 | 9470 | strcpy (buf + base_name_len, suffixes[i]); |
df7492f9 | 9471 | ASET (subsidiaries, i, intern (buf)); |
068a9dbd | 9472 | } |
df7492f9 | 9473 | return subsidiaries; |
068a9dbd KH |
9474 | } |
9475 | ||
9476 | ||
df7492f9 KH |
9477 | DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal, |
9478 | Sdefine_coding_system_internal, coding_arg_max, MANY, 0, | |
1fcd6c8b DL |
9479 | doc: /* For internal use only. |
9480 | usage: (define-coding-system-internal ...) */) | |
f66c7cf8 | 9481 | (ptrdiff_t nargs, Lisp_Object *args) |
068a9dbd | 9482 | { |
df7492f9 KH |
9483 | Lisp_Object name; |
9484 | Lisp_Object spec_vec; /* [ ATTRS ALIASE EOL_TYPE ] */ | |
9485 | Lisp_Object attrs; /* Vector of attributes. */ | |
9486 | Lisp_Object eol_type; | |
9487 | Lisp_Object aliases; | |
9488 | Lisp_Object coding_type, charset_list, safe_charsets; | |
9489 | enum coding_category category; | |
9490 | Lisp_Object tail, val; | |
9491 | int max_charset_id = 0; | |
9492 | int i; | |
068a9dbd | 9493 | |
df7492f9 KH |
9494 | if (nargs < coding_arg_max) |
9495 | goto short_args; | |
068a9dbd | 9496 | |
df7492f9 | 9497 | attrs = Fmake_vector (make_number (coding_attr_last_index), Qnil); |
068a9dbd | 9498 | |
df7492f9 KH |
9499 | name = args[coding_arg_name]; |
9500 | CHECK_SYMBOL (name); | |
4939150c | 9501 | ASET (attrs, coding_attr_base_name, name); |
068a9dbd | 9502 | |
df7492f9 KH |
9503 | val = args[coding_arg_mnemonic]; |
9504 | if (! STRINGP (val)) | |
9505 | CHECK_CHARACTER (val); | |
4939150c | 9506 | ASET (attrs, coding_attr_mnemonic, val); |
068a9dbd | 9507 | |
df7492f9 KH |
9508 | coding_type = args[coding_arg_coding_type]; |
9509 | CHECK_SYMBOL (coding_type); | |
4939150c | 9510 | ASET (attrs, coding_attr_type, coding_type); |
068a9dbd | 9511 | |
df7492f9 KH |
9512 | charset_list = args[coding_arg_charset_list]; |
9513 | if (SYMBOLP (charset_list)) | |
9514 | { | |
9515 | if (EQ (charset_list, Qiso_2022)) | |
9516 | { | |
9517 | if (! EQ (coding_type, Qiso_2022)) | |
9518 | error ("Invalid charset-list"); | |
9519 | charset_list = Viso_2022_charset_list; | |
9520 | } | |
9521 | else if (EQ (charset_list, Qemacs_mule)) | |
9522 | { | |
9523 | if (! EQ (coding_type, Qemacs_mule)) | |
9524 | error ("Invalid charset-list"); | |
9525 | charset_list = Vemacs_mule_charset_list; | |
9526 | } | |
9527 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) | |
d311d28c PE |
9528 | { |
9529 | if (! RANGED_INTEGERP (0, XCAR (tail), INT_MAX - 1)) | |
9530 | error ("Invalid charset-list"); | |
9531 | if (max_charset_id < XFASTINT (XCAR (tail))) | |
9532 | max_charset_id = XFASTINT (XCAR (tail)); | |
9533 | } | |
df7492f9 | 9534 | } |
068a9dbd KH |
9535 | else |
9536 | { | |
df7492f9 | 9537 | charset_list = Fcopy_sequence (charset_list); |
985773c9 | 9538 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) |
068a9dbd | 9539 | { |
df7492f9 KH |
9540 | struct charset *charset; |
9541 | ||
985773c9 | 9542 | val = XCAR (tail); |
df7492f9 KH |
9543 | CHECK_CHARSET_GET_CHARSET (val, charset); |
9544 | if (EQ (coding_type, Qiso_2022) | |
9545 | ? CHARSET_ISO_FINAL (charset) < 0 | |
9546 | : EQ (coding_type, Qemacs_mule) | |
9547 | ? CHARSET_EMACS_MULE_ID (charset) < 0 | |
9548 | : 0) | |
9549 | error ("Can't handle charset `%s'", | |
8f924df7 | 9550 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
df7492f9 | 9551 | |
8f924df7 | 9552 | XSETCAR (tail, make_number (charset->id)); |
df7492f9 KH |
9553 | if (max_charset_id < charset->id) |
9554 | max_charset_id = charset->id; | |
068a9dbd KH |
9555 | } |
9556 | } | |
4939150c | 9557 | ASET (attrs, coding_attr_charset_list, charset_list); |
068a9dbd | 9558 | |
1b3b981b AS |
9559 | safe_charsets = make_uninit_string (max_charset_id + 1); |
9560 | memset (SDATA (safe_charsets), 255, max_charset_id + 1); | |
df7492f9 | 9561 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) |
8f924df7 | 9562 | SSET (safe_charsets, XFASTINT (XCAR (tail)), 0); |
4939150c | 9563 | ASET (attrs, coding_attr_safe_charsets, safe_charsets); |
068a9dbd | 9564 | |
4939150c | 9565 | ASET (attrs, coding_attr_ascii_compat, args[coding_arg_ascii_compatible_p]); |
3a73fa5d | 9566 | |
df7492f9 | 9567 | val = args[coding_arg_decode_translation_table]; |
a6f87d34 | 9568 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) |
7d64c6ad | 9569 | CHECK_SYMBOL (val); |
4939150c | 9570 | ASET (attrs, coding_attr_decode_tbl, val); |
3a73fa5d | 9571 | |
df7492f9 | 9572 | val = args[coding_arg_encode_translation_table]; |
a6f87d34 | 9573 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) |
7d64c6ad | 9574 | CHECK_SYMBOL (val); |
4939150c | 9575 | ASET (attrs, coding_attr_encode_tbl, val); |
d46c5b12 | 9576 | |
df7492f9 KH |
9577 | val = args[coding_arg_post_read_conversion]; |
9578 | CHECK_SYMBOL (val); | |
4939150c | 9579 | ASET (attrs, coding_attr_post_read, val); |
d46c5b12 | 9580 | |
df7492f9 KH |
9581 | val = args[coding_arg_pre_write_conversion]; |
9582 | CHECK_SYMBOL (val); | |
4939150c | 9583 | ASET (attrs, coding_attr_pre_write, val); |
3a73fa5d | 9584 | |
df7492f9 KH |
9585 | val = args[coding_arg_default_char]; |
9586 | if (NILP (val)) | |
4939150c | 9587 | ASET (attrs, coding_attr_default_char, make_number (' ')); |
df7492f9 KH |
9588 | else |
9589 | { | |
8f924df7 | 9590 | CHECK_CHARACTER (val); |
4939150c | 9591 | ASET (attrs, coding_attr_default_char, val); |
df7492f9 | 9592 | } |
4031e2bf | 9593 | |
8f924df7 | 9594 | val = args[coding_arg_for_unibyte]; |
4939150c | 9595 | ASET (attrs, coding_attr_for_unibyte, NILP (val) ? Qnil : Qt); |
3a73fa5d | 9596 | |
df7492f9 KH |
9597 | val = args[coding_arg_plist]; |
9598 | CHECK_LIST (val); | |
4939150c | 9599 | ASET (attrs, coding_attr_plist, val); |
3a73fa5d | 9600 | |
df7492f9 KH |
9601 | if (EQ (coding_type, Qcharset)) |
9602 | { | |
c7c66a95 KH |
9603 | /* Generate a lisp vector of 256 elements. Each element is nil, |
9604 | integer, or a list of charset IDs. | |
3a73fa5d | 9605 | |
c7c66a95 KH |
9606 | If Nth element is nil, the byte code N is invalid in this |
9607 | coding system. | |
4ed46869 | 9608 | |
c7c66a95 KH |
9609 | If Nth element is a number NUM, N is the first byte of a |
9610 | charset whose ID is NUM. | |
4ed46869 | 9611 | |
c7c66a95 KH |
9612 | If Nth element is a list of charset IDs, N is the first byte |
9613 | of one of them. The list is sorted by dimensions of the | |
ad1746f5 | 9614 | charsets. A charset of smaller dimension comes first. */ |
df7492f9 | 9615 | val = Fmake_vector (make_number (256), Qnil); |
4ed46869 | 9616 | |
5c99c2e6 | 9617 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) |
df7492f9 | 9618 | { |
c7c66a95 KH |
9619 | struct charset *charset = CHARSET_FROM_ID (XFASTINT (XCAR (tail))); |
9620 | int dim = CHARSET_DIMENSION (charset); | |
9621 | int idx = (dim - 1) * 4; | |
4ed46869 | 9622 | |
5c99c2e6 | 9623 | if (CHARSET_ASCII_COMPATIBLE_P (charset)) |
4939150c | 9624 | ASET (attrs, coding_attr_ascii_compat, Qt); |
4031e2bf | 9625 | |
15d143f7 KH |
9626 | for (i = charset->code_space[idx]; |
9627 | i <= charset->code_space[idx + 1]; i++) | |
9628 | { | |
c7c66a95 KH |
9629 | Lisp_Object tmp, tmp2; |
9630 | int dim2; | |
ec6d2bb8 | 9631 | |
c7c66a95 KH |
9632 | tmp = AREF (val, i); |
9633 | if (NILP (tmp)) | |
9634 | tmp = XCAR (tail); | |
9635 | else if (NUMBERP (tmp)) | |
9636 | { | |
9637 | dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp))); | |
9638 | if (dim < dim2) | |
c7c66a95 | 9639 | tmp = Fcons (XCAR (tail), Fcons (tmp, Qnil)); |
f9d71dcd KH |
9640 | else |
9641 | tmp = Fcons (tmp, Fcons (XCAR (tail), Qnil)); | |
c7c66a95 | 9642 | } |
15d143f7 | 9643 | else |
c7c66a95 KH |
9644 | { |
9645 | for (tmp2 = tmp; CONSP (tmp2); tmp2 = XCDR (tmp2)) | |
9646 | { | |
9647 | dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2)))); | |
9648 | if (dim < dim2) | |
9649 | break; | |
9650 | } | |
9651 | if (NILP (tmp2)) | |
9652 | tmp = nconc2 (tmp, Fcons (XCAR (tail), Qnil)); | |
9653 | else | |
9654 | { | |
9655 | XSETCDR (tmp2, Fcons (XCAR (tmp2), XCDR (tmp2))); | |
9656 | XSETCAR (tmp2, XCAR (tail)); | |
9657 | } | |
9658 | } | |
9659 | ASET (val, i, tmp); | |
15d143f7 | 9660 | } |
df7492f9 KH |
9661 | } |
9662 | ASET (attrs, coding_attr_charset_valids, val); | |
9663 | category = coding_category_charset; | |
9664 | } | |
9665 | else if (EQ (coding_type, Qccl)) | |
9666 | { | |
9667 | Lisp_Object valids; | |
ecec61c1 | 9668 | |
df7492f9 KH |
9669 | if (nargs < coding_arg_ccl_max) |
9670 | goto short_args; | |
ecec61c1 | 9671 | |
df7492f9 KH |
9672 | val = args[coding_arg_ccl_decoder]; |
9673 | CHECK_CCL_PROGRAM (val); | |
9674 | if (VECTORP (val)) | |
9675 | val = Fcopy_sequence (val); | |
9676 | ASET (attrs, coding_attr_ccl_decoder, val); | |
ecec61c1 | 9677 | |
df7492f9 KH |
9678 | val = args[coding_arg_ccl_encoder]; |
9679 | CHECK_CCL_PROGRAM (val); | |
9680 | if (VECTORP (val)) | |
9681 | val = Fcopy_sequence (val); | |
9682 | ASET (attrs, coding_attr_ccl_encoder, val); | |
ecec61c1 | 9683 | |
df7492f9 KH |
9684 | val = args[coding_arg_ccl_valids]; |
9685 | valids = Fmake_string (make_number (256), make_number (0)); | |
7d7bbefd | 9686 | for (tail = val; CONSP (tail); tail = XCDR (tail)) |
df7492f9 | 9687 | { |
8dcbea82 | 9688 | int from, to; |
ecec61c1 | 9689 | |
34348bd4 | 9690 | val = XCAR (tail); |
df7492f9 | 9691 | if (INTEGERP (val)) |
8dcbea82 | 9692 | { |
d311d28c | 9693 | if (! (0 <= XINT (val) && XINT (val) <= 255)) |
8dcbea82 | 9694 | args_out_of_range_3 (val, make_number (0), make_number (255)); |
d311d28c | 9695 | from = to = XINT (val); |
8dcbea82 | 9696 | } |
df7492f9 KH |
9697 | else |
9698 | { | |
df7492f9 | 9699 | CHECK_CONS (val); |
8f924df7 | 9700 | CHECK_NATNUM_CAR (val); |
d311d28c PE |
9701 | CHECK_NUMBER_CDR (val); |
9702 | if (XINT (XCAR (val)) > 255) | |
8dcbea82 KH |
9703 | args_out_of_range_3 (XCAR (val), |
9704 | make_number (0), make_number (255)); | |
d311d28c PE |
9705 | from = XINT (XCAR (val)); |
9706 | if (! (from <= XINT (XCDR (val)) && XINT (XCDR (val)) <= 255)) | |
8dcbea82 KH |
9707 | args_out_of_range_3 (XCDR (val), |
9708 | XCAR (val), make_number (255)); | |
d311d28c | 9709 | to = XINT (XCDR (val)); |
df7492f9 | 9710 | } |
8dcbea82 | 9711 | for (i = from; i <= to; i++) |
8f924df7 | 9712 | SSET (valids, i, 1); |
df7492f9 KH |
9713 | } |
9714 | ASET (attrs, coding_attr_ccl_valids, valids); | |
4ed46869 | 9715 | |
df7492f9 | 9716 | category = coding_category_ccl; |
55ab7be3 | 9717 | } |
df7492f9 | 9718 | else if (EQ (coding_type, Qutf_16)) |
55ab7be3 | 9719 | { |
df7492f9 | 9720 | Lisp_Object bom, endian; |
4ed46869 | 9721 | |
4939150c | 9722 | ASET (attrs, coding_attr_ascii_compat, Qnil); |
4ed46869 | 9723 | |
df7492f9 KH |
9724 | if (nargs < coding_arg_utf16_max) |
9725 | goto short_args; | |
4ed46869 | 9726 | |
df7492f9 KH |
9727 | bom = args[coding_arg_utf16_bom]; |
9728 | if (! NILP (bom) && ! EQ (bom, Qt)) | |
9729 | { | |
9730 | CHECK_CONS (bom); | |
8f924df7 KH |
9731 | val = XCAR (bom); |
9732 | CHECK_CODING_SYSTEM (val); | |
9733 | val = XCDR (bom); | |
9734 | CHECK_CODING_SYSTEM (val); | |
df7492f9 | 9735 | } |
a470d443 | 9736 | ASET (attrs, coding_attr_utf_bom, bom); |
df7492f9 KH |
9737 | |
9738 | endian = args[coding_arg_utf16_endian]; | |
b49a1807 KH |
9739 | CHECK_SYMBOL (endian); |
9740 | if (NILP (endian)) | |
9741 | endian = Qbig; | |
9742 | else if (! EQ (endian, Qbig) && ! EQ (endian, Qlittle)) | |
8f924df7 | 9743 | error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian))); |
df7492f9 KH |
9744 | ASET (attrs, coding_attr_utf_16_endian, endian); |
9745 | ||
9746 | category = (CONSP (bom) | |
9747 | ? coding_category_utf_16_auto | |
9748 | : NILP (bom) | |
b49a1807 | 9749 | ? (EQ (endian, Qbig) |
df7492f9 KH |
9750 | ? coding_category_utf_16_be_nosig |
9751 | : coding_category_utf_16_le_nosig) | |
b49a1807 | 9752 | : (EQ (endian, Qbig) |
df7492f9 KH |
9753 | ? coding_category_utf_16_be |
9754 | : coding_category_utf_16_le)); | |
9755 | } | |
9756 | else if (EQ (coding_type, Qiso_2022)) | |
9757 | { | |
9758 | Lisp_Object initial, reg_usage, request, flags; | |
1397dc18 | 9759 | |
df7492f9 KH |
9760 | if (nargs < coding_arg_iso2022_max) |
9761 | goto short_args; | |
9762 | ||
9763 | initial = Fcopy_sequence (args[coding_arg_iso2022_initial]); | |
9764 | CHECK_VECTOR (initial); | |
9765 | for (i = 0; i < 4; i++) | |
9766 | { | |
9a9d91d9 | 9767 | val = AREF (initial, i); |
df7492f9 KH |
9768 | if (! NILP (val)) |
9769 | { | |
584948ac KH |
9770 | struct charset *charset; |
9771 | ||
9772 | CHECK_CHARSET_GET_CHARSET (val, charset); | |
9773 | ASET (initial, i, make_number (CHARSET_ID (charset))); | |
9774 | if (i == 0 && CHARSET_ASCII_COMPATIBLE_P (charset)) | |
4939150c | 9775 | ASET (attrs, coding_attr_ascii_compat, Qt); |
df7492f9 KH |
9776 | } |
9777 | else | |
9778 | ASET (initial, i, make_number (-1)); | |
9779 | } | |
9780 | ||
9781 | reg_usage = args[coding_arg_iso2022_reg_usage]; | |
9782 | CHECK_CONS (reg_usage); | |
8f924df7 KH |
9783 | CHECK_NUMBER_CAR (reg_usage); |
9784 | CHECK_NUMBER_CDR (reg_usage); | |
df7492f9 KH |
9785 | |
9786 | request = Fcopy_sequence (args[coding_arg_iso2022_request]); | |
7d7bbefd | 9787 | for (tail = request; CONSP (tail); tail = XCDR (tail)) |
1397dc18 | 9788 | { |
df7492f9 | 9789 | int id; |
2735d060 | 9790 | Lisp_Object tmp1; |
df7492f9 | 9791 | |
34348bd4 | 9792 | val = XCAR (tail); |
df7492f9 | 9793 | CHECK_CONS (val); |
2735d060 PE |
9794 | tmp1 = XCAR (val); |
9795 | CHECK_CHARSET_GET_ID (tmp1, id); | |
8f924df7 | 9796 | CHECK_NATNUM_CDR (val); |
df7492f9 | 9797 | if (XINT (XCDR (val)) >= 4) |
c2982e87 | 9798 | error ("Invalid graphic register number: %"pI"d", XINT (XCDR (val))); |
8f924df7 | 9799 | XSETCAR (val, make_number (id)); |
1397dc18 | 9800 | } |
4ed46869 | 9801 | |
df7492f9 KH |
9802 | flags = args[coding_arg_iso2022_flags]; |
9803 | CHECK_NATNUM (flags); | |
d311d28c | 9804 | i = XINT (flags) & INT_MAX; |
df7492f9 | 9805 | if (EQ (args[coding_arg_charset_list], Qiso_2022)) |
d311d28c PE |
9806 | i |= CODING_ISO_FLAG_FULL_SUPPORT; |
9807 | flags = make_number (i); | |
df7492f9 KH |
9808 | |
9809 | ASET (attrs, coding_attr_iso_initial, initial); | |
9810 | ASET (attrs, coding_attr_iso_usage, reg_usage); | |
9811 | ASET (attrs, coding_attr_iso_request, request); | |
9812 | ASET (attrs, coding_attr_iso_flags, flags); | |
9813 | setup_iso_safe_charsets (attrs); | |
9814 | ||
9815 | if (i & CODING_ISO_FLAG_SEVEN_BITS) | |
9816 | category = ((i & (CODING_ISO_FLAG_LOCKING_SHIFT | |
9817 | | CODING_ISO_FLAG_SINGLE_SHIFT)) | |
9818 | ? coding_category_iso_7_else | |
9819 | : EQ (args[coding_arg_charset_list], Qiso_2022) | |
9820 | ? coding_category_iso_7 | |
9821 | : coding_category_iso_7_tight); | |
9822 | else | |
9823 | { | |
9824 | int id = XINT (AREF (initial, 1)); | |
9825 | ||
c6fb6e98 | 9826 | category = (((i & CODING_ISO_FLAG_LOCKING_SHIFT) |
df7492f9 KH |
9827 | || EQ (args[coding_arg_charset_list], Qiso_2022) |
9828 | || id < 0) | |
9829 | ? coding_category_iso_8_else | |
9830 | : (CHARSET_DIMENSION (CHARSET_FROM_ID (id)) == 1) | |
9831 | ? coding_category_iso_8_1 | |
9832 | : coding_category_iso_8_2); | |
9833 | } | |
0ce7886f KH |
9834 | if (category != coding_category_iso_8_1 |
9835 | && category != coding_category_iso_8_2) | |
4939150c | 9836 | ASET (attrs, coding_attr_ascii_compat, Qnil); |
df7492f9 KH |
9837 | } |
9838 | else if (EQ (coding_type, Qemacs_mule)) | |
c28a9453 | 9839 | { |
df7492f9 KH |
9840 | if (EQ (args[coding_arg_charset_list], Qemacs_mule)) |
9841 | ASET (attrs, coding_attr_emacs_mule_full, Qt); | |
4939150c | 9842 | ASET (attrs, coding_attr_ascii_compat, Qt); |
df7492f9 | 9843 | category = coding_category_emacs_mule; |
c28a9453 | 9844 | } |
df7492f9 | 9845 | else if (EQ (coding_type, Qshift_jis)) |
c28a9453 | 9846 | { |
df7492f9 KH |
9847 | |
9848 | struct charset *charset; | |
9849 | ||
7d64c6ad | 9850 | if (XINT (Flength (charset_list)) != 3 |
6e07c25f | 9851 | && XINT (Flength (charset_list)) != 4) |
7d64c6ad | 9852 | error ("There should be three or four charsets"); |
df7492f9 KH |
9853 | |
9854 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
9855 | if (CHARSET_DIMENSION (charset) != 1) | |
9856 | error ("Dimension of charset %s is not one", | |
8f924df7 | 9857 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
584948ac | 9858 | if (CHARSET_ASCII_COMPATIBLE_P (charset)) |
4939150c | 9859 | ASET (attrs, coding_attr_ascii_compat, Qt); |
df7492f9 KH |
9860 | |
9861 | charset_list = XCDR (charset_list); | |
9862 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
9863 | if (CHARSET_DIMENSION (charset) != 1) | |
9864 | error ("Dimension of charset %s is not one", | |
8f924df7 | 9865 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
df7492f9 KH |
9866 | |
9867 | charset_list = XCDR (charset_list); | |
9868 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
9869 | if (CHARSET_DIMENSION (charset) != 2) | |
7d64c6ad KH |
9870 | error ("Dimension of charset %s is not two", |
9871 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); | |
9872 | ||
9873 | charset_list = XCDR (charset_list); | |
2b917a06 KH |
9874 | if (! NILP (charset_list)) |
9875 | { | |
9876 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
9877 | if (CHARSET_DIMENSION (charset) != 2) | |
9878 | error ("Dimension of charset %s is not two", | |
9879 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); | |
9880 | } | |
df7492f9 KH |
9881 | |
9882 | category = coding_category_sjis; | |
9883 | Vsjis_coding_system = name; | |
c28a9453 | 9884 | } |
df7492f9 KH |
9885 | else if (EQ (coding_type, Qbig5)) |
9886 | { | |
9887 | struct charset *charset; | |
4ed46869 | 9888 | |
df7492f9 KH |
9889 | if (XINT (Flength (charset_list)) != 2) |
9890 | error ("There should be just two charsets"); | |
9891 | ||
9892 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
9893 | if (CHARSET_DIMENSION (charset) != 1) | |
9894 | error ("Dimension of charset %s is not one", | |
8f924df7 | 9895 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
584948ac | 9896 | if (CHARSET_ASCII_COMPATIBLE_P (charset)) |
4939150c | 9897 | ASET (attrs, coding_attr_ascii_compat, Qt); |
df7492f9 KH |
9898 | |
9899 | charset_list = XCDR (charset_list); | |
9900 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
9901 | if (CHARSET_DIMENSION (charset) != 2) | |
9902 | error ("Dimension of charset %s is not two", | |
8f924df7 | 9903 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
4ed46869 | 9904 | |
df7492f9 KH |
9905 | category = coding_category_big5; |
9906 | Vbig5_coding_system = name; | |
9907 | } | |
9908 | else if (EQ (coding_type, Qraw_text)) | |
c28a9453 | 9909 | { |
584948ac | 9910 | category = coding_category_raw_text; |
4939150c | 9911 | ASET (attrs, coding_attr_ascii_compat, Qt); |
c28a9453 | 9912 | } |
df7492f9 | 9913 | else if (EQ (coding_type, Qutf_8)) |
4ed46869 | 9914 | { |
a470d443 KH |
9915 | Lisp_Object bom; |
9916 | ||
a470d443 KH |
9917 | if (nargs < coding_arg_utf8_max) |
9918 | goto short_args; | |
9919 | ||
9920 | bom = args[coding_arg_utf8_bom]; | |
9921 | if (! NILP (bom) && ! EQ (bom, Qt)) | |
9922 | { | |
9923 | CHECK_CONS (bom); | |
9924 | val = XCAR (bom); | |
9925 | CHECK_CODING_SYSTEM (val); | |
9926 | val = XCDR (bom); | |
9927 | CHECK_CODING_SYSTEM (val); | |
9928 | } | |
9929 | ASET (attrs, coding_attr_utf_bom, bom); | |
0e5317f7 | 9930 | if (NILP (bom)) |
4939150c | 9931 | ASET (attrs, coding_attr_ascii_compat, Qt); |
a470d443 KH |
9932 | |
9933 | category = (CONSP (bom) ? coding_category_utf_8_auto | |
9934 | : NILP (bom) ? coding_category_utf_8_nosig | |
9935 | : coding_category_utf_8_sig); | |
4ed46869 | 9936 | } |
df7492f9 KH |
9937 | else if (EQ (coding_type, Qundecided)) |
9938 | category = coding_category_undecided; | |
4ed46869 | 9939 | else |
df7492f9 | 9940 | error ("Invalid coding system type: %s", |
8f924df7 | 9941 | SDATA (SYMBOL_NAME (coding_type))); |
4ed46869 | 9942 | |
4939150c PE |
9943 | ASET (attrs, coding_attr_category, make_number (category)); |
9944 | ASET (attrs, coding_attr_plist, | |
9945 | Fcons (QCcategory, | |
9946 | Fcons (AREF (Vcoding_category_table, category), | |
9947 | CODING_ATTR_PLIST (attrs)))); | |
9948 | ASET (attrs, coding_attr_plist, | |
9949 | Fcons (QCascii_compatible_p, | |
9950 | Fcons (CODING_ATTR_ASCII_COMPAT (attrs), | |
9951 | CODING_ATTR_PLIST (attrs)))); | |
c4825358 | 9952 | |
df7492f9 KH |
9953 | eol_type = args[coding_arg_eol_type]; |
9954 | if (! NILP (eol_type) | |
9955 | && ! EQ (eol_type, Qunix) | |
9956 | && ! EQ (eol_type, Qdos) | |
9957 | && ! EQ (eol_type, Qmac)) | |
9958 | error ("Invalid eol-type"); | |
4ed46869 | 9959 | |
df7492f9 | 9960 | aliases = Fcons (name, Qnil); |
4ed46869 | 9961 | |
df7492f9 KH |
9962 | if (NILP (eol_type)) |
9963 | { | |
9964 | eol_type = make_subsidiaries (name); | |
9965 | for (i = 0; i < 3; i++) | |
1397dc18 | 9966 | { |
df7492f9 KH |
9967 | Lisp_Object this_spec, this_name, this_aliases, this_eol_type; |
9968 | ||
9969 | this_name = AREF (eol_type, i); | |
9970 | this_aliases = Fcons (this_name, Qnil); | |
9971 | this_eol_type = (i == 0 ? Qunix : i == 1 ? Qdos : Qmac); | |
25721f5b DA |
9972 | this_spec = make_uninit_vector (3); |
9973 | ASET (this_spec, 0, attrs); | |
df7492f9 KH |
9974 | ASET (this_spec, 1, this_aliases); |
9975 | ASET (this_spec, 2, this_eol_type); | |
9976 | Fputhash (this_name, this_spec, Vcoding_system_hash_table); | |
9977 | Vcoding_system_list = Fcons (this_name, Vcoding_system_list); | |
583f71ca KH |
9978 | val = Fassoc (Fsymbol_name (this_name), Vcoding_system_alist); |
9979 | if (NILP (val)) | |
9980 | Vcoding_system_alist | |
9981 | = Fcons (Fcons (Fsymbol_name (this_name), Qnil), | |
9982 | Vcoding_system_alist); | |
1397dc18 | 9983 | } |
d46c5b12 | 9984 | } |
4ed46869 | 9985 | |
25721f5b DA |
9986 | spec_vec = make_uninit_vector (3); |
9987 | ASET (spec_vec, 0, attrs); | |
df7492f9 KH |
9988 | ASET (spec_vec, 1, aliases); |
9989 | ASET (spec_vec, 2, eol_type); | |
48b0f3ae | 9990 | |
df7492f9 KH |
9991 | Fputhash (name, spec_vec, Vcoding_system_hash_table); |
9992 | Vcoding_system_list = Fcons (name, Vcoding_system_list); | |
583f71ca KH |
9993 | val = Fassoc (Fsymbol_name (name), Vcoding_system_alist); |
9994 | if (NILP (val)) | |
9995 | Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (name), Qnil), | |
9996 | Vcoding_system_alist); | |
48b0f3ae | 9997 | |
df7492f9 KH |
9998 | { |
9999 | int id = coding_categories[category].id; | |
48b0f3ae | 10000 | |
df7492f9 KH |
10001 | if (id < 0 || EQ (name, CODING_ID_NAME (id))) |
10002 | setup_coding_system (name, &coding_categories[category]); | |
10003 | } | |
48b0f3ae | 10004 | |
d46c5b12 | 10005 | return Qnil; |
48b0f3ae | 10006 | |
df7492f9 KH |
10007 | short_args: |
10008 | return Fsignal (Qwrong_number_of_arguments, | |
10009 | Fcons (intern ("define-coding-system-internal"), | |
10010 | make_number (nargs))); | |
d46c5b12 | 10011 | } |
4ed46869 | 10012 | |
d6925f38 | 10013 | |
a6f87d34 KH |
10014 | DEFUN ("coding-system-put", Fcoding_system_put, Scoding_system_put, |
10015 | 3, 3, 0, | |
10016 | doc: /* Change value in CODING-SYSTEM's property list PROP to VAL. */) | |
5842a27b | 10017 | (Lisp_Object coding_system, Lisp_Object prop, Lisp_Object val) |
a6f87d34 | 10018 | { |
3dbe7859 | 10019 | Lisp_Object spec, attrs; |
a6f87d34 KH |
10020 | |
10021 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
10022 | attrs = AREF (spec, 0); | |
10023 | if (EQ (prop, QCmnemonic)) | |
10024 | { | |
10025 | if (! STRINGP (val)) | |
10026 | CHECK_CHARACTER (val); | |
4939150c | 10027 | ASET (attrs, coding_attr_mnemonic, val); |
a6f87d34 | 10028 | } |
2133e2d1 | 10029 | else if (EQ (prop, QCdefault_char)) |
a6f87d34 KH |
10030 | { |
10031 | if (NILP (val)) | |
10032 | val = make_number (' '); | |
10033 | else | |
10034 | CHECK_CHARACTER (val); | |
4939150c | 10035 | ASET (attrs, coding_attr_default_char, val); |
a6f87d34 KH |
10036 | } |
10037 | else if (EQ (prop, QCdecode_translation_table)) | |
10038 | { | |
10039 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) | |
10040 | CHECK_SYMBOL (val); | |
4939150c | 10041 | ASET (attrs, coding_attr_decode_tbl, val); |
a6f87d34 KH |
10042 | } |
10043 | else if (EQ (prop, QCencode_translation_table)) | |
10044 | { | |
10045 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) | |
10046 | CHECK_SYMBOL (val); | |
4939150c | 10047 | ASET (attrs, coding_attr_encode_tbl, val); |
a6f87d34 KH |
10048 | } |
10049 | else if (EQ (prop, QCpost_read_conversion)) | |
10050 | { | |
10051 | CHECK_SYMBOL (val); | |
4939150c | 10052 | ASET (attrs, coding_attr_post_read, val); |
a6f87d34 KH |
10053 | } |
10054 | else if (EQ (prop, QCpre_write_conversion)) | |
10055 | { | |
10056 | CHECK_SYMBOL (val); | |
4939150c | 10057 | ASET (attrs, coding_attr_pre_write, val); |
a6f87d34 | 10058 | } |
35befdaa KH |
10059 | else if (EQ (prop, QCascii_compatible_p)) |
10060 | { | |
4939150c | 10061 | ASET (attrs, coding_attr_ascii_compat, val); |
35befdaa | 10062 | } |
a6f87d34 | 10063 | |
4939150c PE |
10064 | ASET (attrs, coding_attr_plist, |
10065 | Fplist_put (CODING_ATTR_PLIST (attrs), prop, val)); | |
a6f87d34 KH |
10066 | return val; |
10067 | } | |
10068 | ||
10069 | ||
df7492f9 KH |
10070 | DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias, |
10071 | Sdefine_coding_system_alias, 2, 2, 0, | |
10072 | doc: /* Define ALIAS as an alias for CODING-SYSTEM. */) | |
5842a27b | 10073 | (Lisp_Object alias, Lisp_Object coding_system) |
66cfb530 | 10074 | { |
583f71ca | 10075 | Lisp_Object spec, aliases, eol_type, val; |
4ed46869 | 10076 | |
df7492f9 KH |
10077 | CHECK_SYMBOL (alias); |
10078 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
10079 | aliases = AREF (spec, 1); | |
d4a1d553 | 10080 | /* ALIASES should be a list of length more than zero, and the first |
d6925f38 KH |
10081 | element is a base coding system. Append ALIAS at the tail of the |
10082 | list. */ | |
df7492f9 KH |
10083 | while (!NILP (XCDR (aliases))) |
10084 | aliases = XCDR (aliases); | |
8f924df7 | 10085 | XSETCDR (aliases, Fcons (alias, Qnil)); |
4ed46869 | 10086 | |
df7492f9 KH |
10087 | eol_type = AREF (spec, 2); |
10088 | if (VECTORP (eol_type)) | |
4ed46869 | 10089 | { |
df7492f9 KH |
10090 | Lisp_Object subsidiaries; |
10091 | int i; | |
4ed46869 | 10092 | |
df7492f9 KH |
10093 | subsidiaries = make_subsidiaries (alias); |
10094 | for (i = 0; i < 3; i++) | |
10095 | Fdefine_coding_system_alias (AREF (subsidiaries, i), | |
10096 | AREF (eol_type, i)); | |
4ed46869 | 10097 | } |
df7492f9 KH |
10098 | |
10099 | Fputhash (alias, spec, Vcoding_system_hash_table); | |
d6925f38 | 10100 | Vcoding_system_list = Fcons (alias, Vcoding_system_list); |
583f71ca KH |
10101 | val = Fassoc (Fsymbol_name (alias), Vcoding_system_alist); |
10102 | if (NILP (val)) | |
10103 | Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (alias), Qnil), | |
10104 | Vcoding_system_alist); | |
66cfb530 | 10105 | |
4ed46869 KH |
10106 | return Qnil; |
10107 | } | |
10108 | ||
a7ca3326 | 10109 | DEFUN ("coding-system-base", Fcoding_system_base, Scoding_system_base, |
df7492f9 KH |
10110 | 1, 1, 0, |
10111 | doc: /* Return the base of CODING-SYSTEM. | |
da7db224 | 10112 | Any alias or subsidiary coding system is not a base coding system. */) |
5842a27b | 10113 | (Lisp_Object coding_system) |
d46c5b12 | 10114 | { |
df7492f9 | 10115 | Lisp_Object spec, attrs; |
d46c5b12 | 10116 | |
df7492f9 KH |
10117 | if (NILP (coding_system)) |
10118 | return (Qno_conversion); | |
10119 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
10120 | attrs = AREF (spec, 0); | |
10121 | return CODING_ATTR_BASE_NAME (attrs); | |
10122 | } | |
1397dc18 | 10123 | |
df7492f9 KH |
10124 | DEFUN ("coding-system-plist", Fcoding_system_plist, Scoding_system_plist, |
10125 | 1, 1, 0, | |
10126 | doc: "Return the property list of CODING-SYSTEM.") | |
5842a27b | 10127 | (Lisp_Object coding_system) |
df7492f9 KH |
10128 | { |
10129 | Lisp_Object spec, attrs; | |
1397dc18 | 10130 | |
df7492f9 KH |
10131 | if (NILP (coding_system)) |
10132 | coding_system = Qno_conversion; | |
10133 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
10134 | attrs = AREF (spec, 0); | |
10135 | return CODING_ATTR_PLIST (attrs); | |
d46c5b12 KH |
10136 | } |
10137 | ||
df7492f9 KH |
10138 | |
10139 | DEFUN ("coding-system-aliases", Fcoding_system_aliases, Scoding_system_aliases, | |
10140 | 1, 1, 0, | |
da7db224 | 10141 | doc: /* Return the list of aliases of CODING-SYSTEM. */) |
5842a27b | 10142 | (Lisp_Object coding_system) |
66cfb530 | 10143 | { |
df7492f9 | 10144 | Lisp_Object spec; |
84d60297 | 10145 | |
df7492f9 KH |
10146 | if (NILP (coding_system)) |
10147 | coding_system = Qno_conversion; | |
10148 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
da7db224 | 10149 | return AREF (spec, 1); |
df7492f9 | 10150 | } |
66cfb530 | 10151 | |
a7ca3326 | 10152 | DEFUN ("coding-system-eol-type", Fcoding_system_eol_type, |
df7492f9 KH |
10153 | Scoding_system_eol_type, 1, 1, 0, |
10154 | doc: /* Return eol-type of CODING-SYSTEM. | |
d4a1d553 | 10155 | An eol-type is an integer 0, 1, 2, or a vector of coding systems. |
66cfb530 | 10156 | |
df7492f9 KH |
10157 | Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF, |
10158 | and CR respectively. | |
66cfb530 | 10159 | |
df7492f9 KH |
10160 | A vector value indicates that a format of end-of-line should be |
10161 | detected automatically. Nth element of the vector is the subsidiary | |
10162 | coding system whose eol-type is N. */) | |
5842a27b | 10163 | (Lisp_Object coding_system) |
6b89e3aa | 10164 | { |
df7492f9 KH |
10165 | Lisp_Object spec, eol_type; |
10166 | int n; | |
6b89e3aa | 10167 | |
df7492f9 KH |
10168 | if (NILP (coding_system)) |
10169 | coding_system = Qno_conversion; | |
10170 | if (! CODING_SYSTEM_P (coding_system)) | |
10171 | return Qnil; | |
10172 | spec = CODING_SYSTEM_SPEC (coding_system); | |
10173 | eol_type = AREF (spec, 2); | |
10174 | if (VECTORP (eol_type)) | |
10175 | return Fcopy_sequence (eol_type); | |
10176 | n = EQ (eol_type, Qunix) ? 0 : EQ (eol_type, Qdos) ? 1 : 2; | |
10177 | return make_number (n); | |
6b89e3aa KH |
10178 | } |
10179 | ||
4ed46869 KH |
10180 | #endif /* emacs */ |
10181 | ||
10182 | \f | |
1397dc18 | 10183 | /*** 9. Post-amble ***/ |
4ed46869 | 10184 | |
dfcf069d | 10185 | void |
971de7fb | 10186 | init_coding_once (void) |
4ed46869 KH |
10187 | { |
10188 | int i; | |
10189 | ||
df7492f9 KH |
10190 | for (i = 0; i < coding_category_max; i++) |
10191 | { | |
10192 | coding_categories[i].id = -1; | |
10193 | coding_priorities[i] = i; | |
10194 | } | |
4ed46869 KH |
10195 | |
10196 | /* ISO2022 specific initialize routine. */ | |
10197 | for (i = 0; i < 0x20; i++) | |
b73bfc1c | 10198 | iso_code_class[i] = ISO_control_0; |
4ed46869 KH |
10199 | for (i = 0x21; i < 0x7F; i++) |
10200 | iso_code_class[i] = ISO_graphic_plane_0; | |
10201 | for (i = 0x80; i < 0xA0; i++) | |
b73bfc1c | 10202 | iso_code_class[i] = ISO_control_1; |
4ed46869 KH |
10203 | for (i = 0xA1; i < 0xFF; i++) |
10204 | iso_code_class[i] = ISO_graphic_plane_1; | |
10205 | iso_code_class[0x20] = iso_code_class[0x7F] = ISO_0x20_or_0x7F; | |
10206 | iso_code_class[0xA0] = iso_code_class[0xFF] = ISO_0xA0_or_0xFF; | |
4ed46869 KH |
10207 | iso_code_class[ISO_CODE_SO] = ISO_shift_out; |
10208 | iso_code_class[ISO_CODE_SI] = ISO_shift_in; | |
10209 | iso_code_class[ISO_CODE_SS2_7] = ISO_single_shift_2_7; | |
10210 | iso_code_class[ISO_CODE_ESC] = ISO_escape; | |
10211 | iso_code_class[ISO_CODE_SS2] = ISO_single_shift_2; | |
10212 | iso_code_class[ISO_CODE_SS3] = ISO_single_shift_3; | |
10213 | iso_code_class[ISO_CODE_CSI] = ISO_control_sequence_introducer; | |
10214 | ||
df7492f9 KH |
10215 | for (i = 0; i < 256; i++) |
10216 | { | |
10217 | emacs_mule_bytes[i] = 1; | |
10218 | } | |
7c78e542 KH |
10219 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_11] = 3; |
10220 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_12] = 3; | |
10221 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_21] = 4; | |
10222 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_22] = 4; | |
e0e989f6 KH |
10223 | } |
10224 | ||
10225 | #ifdef emacs | |
10226 | ||
dfcf069d | 10227 | void |
971de7fb | 10228 | syms_of_coding (void) |
e0e989f6 | 10229 | { |
df7492f9 | 10230 | staticpro (&Vcoding_system_hash_table); |
8f924df7 KH |
10231 | { |
10232 | Lisp_Object args[2]; | |
10233 | args[0] = QCtest; | |
10234 | args[1] = Qeq; | |
10235 | Vcoding_system_hash_table = Fmake_hash_table (2, args); | |
10236 | } | |
df7492f9 KH |
10237 | |
10238 | staticpro (&Vsjis_coding_system); | |
10239 | Vsjis_coding_system = Qnil; | |
e0e989f6 | 10240 | |
df7492f9 KH |
10241 | staticpro (&Vbig5_coding_system); |
10242 | Vbig5_coding_system = Qnil; | |
10243 | ||
24a73b0a KH |
10244 | staticpro (&Vcode_conversion_reused_workbuf); |
10245 | Vcode_conversion_reused_workbuf = Qnil; | |
10246 | ||
10247 | staticpro (&Vcode_conversion_workbuf_name); | |
2a0213a6 | 10248 | Vcode_conversion_workbuf_name = build_pure_c_string (" *code-conversion-work*"); |
e0e989f6 | 10249 | |
24a73b0a | 10250 | reused_workbuf_in_use = 0; |
df7492f9 KH |
10251 | |
10252 | DEFSYM (Qcharset, "charset"); | |
10253 | DEFSYM (Qtarget_idx, "target-idx"); | |
10254 | DEFSYM (Qcoding_system_history, "coding-system-history"); | |
bb0115a2 RS |
10255 | Fset (Qcoding_system_history, Qnil); |
10256 | ||
9ce27fde | 10257 | /* Target FILENAME is the first argument. */ |
e0e989f6 | 10258 | Fput (Qinsert_file_contents, Qtarget_idx, make_number (0)); |
9ce27fde | 10259 | /* Target FILENAME is the third argument. */ |
e0e989f6 KH |
10260 | Fput (Qwrite_region, Qtarget_idx, make_number (2)); |
10261 | ||
df7492f9 | 10262 | DEFSYM (Qcall_process, "call-process"); |
9ce27fde | 10263 | /* Target PROGRAM is the first argument. */ |
e0e989f6 KH |
10264 | Fput (Qcall_process, Qtarget_idx, make_number (0)); |
10265 | ||
df7492f9 | 10266 | DEFSYM (Qcall_process_region, "call-process-region"); |
9ce27fde | 10267 | /* Target PROGRAM is the third argument. */ |
e0e989f6 KH |
10268 | Fput (Qcall_process_region, Qtarget_idx, make_number (2)); |
10269 | ||
df7492f9 | 10270 | DEFSYM (Qstart_process, "start-process"); |
9ce27fde | 10271 | /* Target PROGRAM is the third argument. */ |
e0e989f6 KH |
10272 | Fput (Qstart_process, Qtarget_idx, make_number (2)); |
10273 | ||
df7492f9 | 10274 | DEFSYM (Qopen_network_stream, "open-network-stream"); |
9ce27fde | 10275 | /* Target SERVICE is the fourth argument. */ |
e0e989f6 KH |
10276 | Fput (Qopen_network_stream, Qtarget_idx, make_number (3)); |
10277 | ||
df7492f9 KH |
10278 | DEFSYM (Qcoding_system, "coding-system"); |
10279 | DEFSYM (Qcoding_aliases, "coding-aliases"); | |
4ed46869 | 10280 | |
df7492f9 KH |
10281 | DEFSYM (Qeol_type, "eol-type"); |
10282 | DEFSYM (Qunix, "unix"); | |
10283 | DEFSYM (Qdos, "dos"); | |
4b298d5a | 10284 | DEFSYM (Qmac, "mac"); |
4ed46869 | 10285 | |
df7492f9 KH |
10286 | DEFSYM (Qbuffer_file_coding_system, "buffer-file-coding-system"); |
10287 | DEFSYM (Qpost_read_conversion, "post-read-conversion"); | |
10288 | DEFSYM (Qpre_write_conversion, "pre-write-conversion"); | |
10289 | DEFSYM (Qdefault_char, "default-char"); | |
10290 | DEFSYM (Qundecided, "undecided"); | |
10291 | DEFSYM (Qno_conversion, "no-conversion"); | |
10292 | DEFSYM (Qraw_text, "raw-text"); | |
4ed46869 | 10293 | |
df7492f9 | 10294 | DEFSYM (Qiso_2022, "iso-2022"); |
4ed46869 | 10295 | |
df7492f9 | 10296 | DEFSYM (Qutf_8, "utf-8"); |
8f924df7 | 10297 | DEFSYM (Qutf_8_emacs, "utf-8-emacs"); |
27901516 | 10298 | |
7f590b0c | 10299 | #if defined (WINDOWSNT) || defined (CYGWIN) |
ba116008 DC |
10300 | /* No, not utf-16-le: that one has a BOM. */ |
10301 | DEFSYM (Qutf_16le, "utf-16le"); | |
10302 | #endif | |
10303 | ||
df7492f9 | 10304 | DEFSYM (Qutf_16, "utf-16"); |
df7492f9 KH |
10305 | DEFSYM (Qbig, "big"); |
10306 | DEFSYM (Qlittle, "little"); | |
27901516 | 10307 | |
df7492f9 KH |
10308 | DEFSYM (Qshift_jis, "shift-jis"); |
10309 | DEFSYM (Qbig5, "big5"); | |
4ed46869 | 10310 | |
df7492f9 | 10311 | DEFSYM (Qcoding_system_p, "coding-system-p"); |
4ed46869 | 10312 | |
df7492f9 | 10313 | DEFSYM (Qcoding_system_error, "coding-system-error"); |
4ed46869 | 10314 | Fput (Qcoding_system_error, Qerror_conditions, |
3438fe21 | 10315 | listn (CONSTYPE_PURE, 2, Qcoding_system_error, Qerror)); |
4ed46869 | 10316 | Fput (Qcoding_system_error, Qerror_message, |
2a0213a6 | 10317 | build_pure_c_string ("Invalid coding system")); |
4ed46869 | 10318 | |
05e6f5dc KH |
10319 | /* Intern this now in case it isn't already done. |
10320 | Setting this variable twice is harmless. | |
10321 | But don't staticpro it here--that is done in alloc.c. */ | |
d67b4f80 | 10322 | Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots"); |
70c22245 | 10323 | |
df7492f9 | 10324 | DEFSYM (Qtranslation_table, "translation-table"); |
433f7f87 | 10325 | Fput (Qtranslation_table, Qchar_table_extra_slots, make_number (2)); |
df7492f9 KH |
10326 | DEFSYM (Qtranslation_table_id, "translation-table-id"); |
10327 | DEFSYM (Qtranslation_table_for_decode, "translation-table-for-decode"); | |
10328 | DEFSYM (Qtranslation_table_for_encode, "translation-table-for-encode"); | |
1397dc18 | 10329 | |
df7492f9 | 10330 | DEFSYM (Qvalid_codes, "valid-codes"); |
9ce27fde | 10331 | |
df7492f9 | 10332 | DEFSYM (Qemacs_mule, "emacs-mule"); |
d46c5b12 | 10333 | |
01378f49 | 10334 | DEFSYM (QCcategory, ":category"); |
a6f87d34 | 10335 | DEFSYM (QCmnemonic, ":mnemonic"); |
2133e2d1 | 10336 | DEFSYM (QCdefault_char, ":default-char"); |
a6f87d34 KH |
10337 | DEFSYM (QCdecode_translation_table, ":decode-translation-table"); |
10338 | DEFSYM (QCencode_translation_table, ":encode-translation-table"); | |
10339 | DEFSYM (QCpost_read_conversion, ":post-read-conversion"); | |
10340 | DEFSYM (QCpre_write_conversion, ":pre-write-conversion"); | |
35befdaa | 10341 | DEFSYM (QCascii_compatible_p, ":ascii-compatible-p"); |
01378f49 | 10342 | |
df7492f9 KH |
10343 | Vcoding_category_table |
10344 | = Fmake_vector (make_number (coding_category_max), Qnil); | |
10345 | staticpro (&Vcoding_category_table); | |
10346 | /* Followings are target of code detection. */ | |
10347 | ASET (Vcoding_category_table, coding_category_iso_7, | |
d67b4f80 | 10348 | intern_c_string ("coding-category-iso-7")); |
df7492f9 | 10349 | ASET (Vcoding_category_table, coding_category_iso_7_tight, |
d67b4f80 | 10350 | intern_c_string ("coding-category-iso-7-tight")); |
df7492f9 | 10351 | ASET (Vcoding_category_table, coding_category_iso_8_1, |
d67b4f80 | 10352 | intern_c_string ("coding-category-iso-8-1")); |
df7492f9 | 10353 | ASET (Vcoding_category_table, coding_category_iso_8_2, |
d67b4f80 | 10354 | intern_c_string ("coding-category-iso-8-2")); |
df7492f9 | 10355 | ASET (Vcoding_category_table, coding_category_iso_7_else, |
d67b4f80 | 10356 | intern_c_string ("coding-category-iso-7-else")); |
df7492f9 | 10357 | ASET (Vcoding_category_table, coding_category_iso_8_else, |
d67b4f80 | 10358 | intern_c_string ("coding-category-iso-8-else")); |
a470d443 | 10359 | ASET (Vcoding_category_table, coding_category_utf_8_auto, |
d67b4f80 | 10360 | intern_c_string ("coding-category-utf-8-auto")); |
a470d443 | 10361 | ASET (Vcoding_category_table, coding_category_utf_8_nosig, |
d67b4f80 | 10362 | intern_c_string ("coding-category-utf-8")); |
a470d443 | 10363 | ASET (Vcoding_category_table, coding_category_utf_8_sig, |
d67b4f80 | 10364 | intern_c_string ("coding-category-utf-8-sig")); |
df7492f9 | 10365 | ASET (Vcoding_category_table, coding_category_utf_16_be, |
d67b4f80 | 10366 | intern_c_string ("coding-category-utf-16-be")); |
ff563fce | 10367 | ASET (Vcoding_category_table, coding_category_utf_16_auto, |
d67b4f80 | 10368 | intern_c_string ("coding-category-utf-16-auto")); |
df7492f9 | 10369 | ASET (Vcoding_category_table, coding_category_utf_16_le, |
d67b4f80 | 10370 | intern_c_string ("coding-category-utf-16-le")); |
df7492f9 | 10371 | ASET (Vcoding_category_table, coding_category_utf_16_be_nosig, |
d67b4f80 | 10372 | intern_c_string ("coding-category-utf-16-be-nosig")); |
df7492f9 | 10373 | ASET (Vcoding_category_table, coding_category_utf_16_le_nosig, |
d67b4f80 | 10374 | intern_c_string ("coding-category-utf-16-le-nosig")); |
df7492f9 | 10375 | ASET (Vcoding_category_table, coding_category_charset, |
d67b4f80 | 10376 | intern_c_string ("coding-category-charset")); |
df7492f9 | 10377 | ASET (Vcoding_category_table, coding_category_sjis, |
d67b4f80 | 10378 | intern_c_string ("coding-category-sjis")); |
df7492f9 | 10379 | ASET (Vcoding_category_table, coding_category_big5, |
d67b4f80 | 10380 | intern_c_string ("coding-category-big5")); |
df7492f9 | 10381 | ASET (Vcoding_category_table, coding_category_ccl, |
d67b4f80 | 10382 | intern_c_string ("coding-category-ccl")); |
df7492f9 | 10383 | ASET (Vcoding_category_table, coding_category_emacs_mule, |
d67b4f80 | 10384 | intern_c_string ("coding-category-emacs-mule")); |
df7492f9 KH |
10385 | /* Followings are NOT target of code detection. */ |
10386 | ASET (Vcoding_category_table, coding_category_raw_text, | |
d67b4f80 | 10387 | intern_c_string ("coding-category-raw-text")); |
df7492f9 | 10388 | ASET (Vcoding_category_table, coding_category_undecided, |
d67b4f80 | 10389 | intern_c_string ("coding-category-undecided")); |
ecf488bc | 10390 | |
065e3595 | 10391 | DEFSYM (Qinsufficient_source, "insufficient-source"); |
065e3595 KH |
10392 | DEFSYM (Qinvalid_source, "invalid-source"); |
10393 | DEFSYM (Qinterrupted, "interrupted"); | |
44e8490d | 10394 | DEFSYM (Qcoding_system_define_form, "coding-system-define-form"); |
065e3595 | 10395 | |
4ed46869 KH |
10396 | defsubr (&Scoding_system_p); |
10397 | defsubr (&Sread_coding_system); | |
10398 | defsubr (&Sread_non_nil_coding_system); | |
10399 | defsubr (&Scheck_coding_system); | |
10400 | defsubr (&Sdetect_coding_region); | |
d46c5b12 | 10401 | defsubr (&Sdetect_coding_string); |
05e6f5dc | 10402 | defsubr (&Sfind_coding_systems_region_internal); |
068a9dbd | 10403 | defsubr (&Sunencodable_char_position); |
df7492f9 | 10404 | defsubr (&Scheck_coding_systems_region); |
4ed46869 KH |
10405 | defsubr (&Sdecode_coding_region); |
10406 | defsubr (&Sencode_coding_region); | |
10407 | defsubr (&Sdecode_coding_string); | |
10408 | defsubr (&Sencode_coding_string); | |
10409 | defsubr (&Sdecode_sjis_char); | |
10410 | defsubr (&Sencode_sjis_char); | |
10411 | defsubr (&Sdecode_big5_char); | |
10412 | defsubr (&Sencode_big5_char); | |
1ba9e4ab | 10413 | defsubr (&Sset_terminal_coding_system_internal); |
c4825358 | 10414 | defsubr (&Sset_safe_terminal_coding_system_internal); |
4ed46869 | 10415 | defsubr (&Sterminal_coding_system); |
1ba9e4ab | 10416 | defsubr (&Sset_keyboard_coding_system_internal); |
4ed46869 | 10417 | defsubr (&Skeyboard_coding_system); |
a5d301df | 10418 | defsubr (&Sfind_operation_coding_system); |
df7492f9 | 10419 | defsubr (&Sset_coding_system_priority); |
6b89e3aa | 10420 | defsubr (&Sdefine_coding_system_internal); |
df7492f9 | 10421 | defsubr (&Sdefine_coding_system_alias); |
a6f87d34 | 10422 | defsubr (&Scoding_system_put); |
df7492f9 KH |
10423 | defsubr (&Scoding_system_base); |
10424 | defsubr (&Scoding_system_plist); | |
10425 | defsubr (&Scoding_system_aliases); | |
10426 | defsubr (&Scoding_system_eol_type); | |
10427 | defsubr (&Scoding_system_priority_list); | |
4ed46869 | 10428 | |
29208e82 | 10429 | DEFVAR_LISP ("coding-system-list", Vcoding_system_list, |
48b0f3ae PJ |
10430 | doc: /* List of coding systems. |
10431 | ||
10432 | Do not alter the value of this variable manually. This variable should be | |
df7492f9 | 10433 | updated by the functions `define-coding-system' and |
48b0f3ae | 10434 | `define-coding-system-alias'. */); |
4608c386 KH |
10435 | Vcoding_system_list = Qnil; |
10436 | ||
29208e82 | 10437 | DEFVAR_LISP ("coding-system-alist", Vcoding_system_alist, |
48b0f3ae PJ |
10438 | doc: /* Alist of coding system names. |
10439 | Each element is one element list of coding system name. | |
446dcd75 | 10440 | This variable is given to `completing-read' as COLLECTION argument. |
48b0f3ae PJ |
10441 | |
10442 | Do not alter the value of this variable manually. This variable should be | |
10443 | updated by the functions `make-coding-system' and | |
10444 | `define-coding-system-alias'. */); | |
4608c386 KH |
10445 | Vcoding_system_alist = Qnil; |
10446 | ||
29208e82 | 10447 | DEFVAR_LISP ("coding-category-list", Vcoding_category_list, |
48b0f3ae PJ |
10448 | doc: /* List of coding-categories (symbols) ordered by priority. |
10449 | ||
10450 | On detecting a coding system, Emacs tries code detection algorithms | |
10451 | associated with each coding-category one by one in this order. When | |
10452 | one algorithm agrees with a byte sequence of source text, the coding | |
0ec31faf KH |
10453 | system bound to the corresponding coding-category is selected. |
10454 | ||
448e17d6 | 10455 | Don't modify this variable directly, but use `set-coding-system-priority'. */); |
4ed46869 KH |
10456 | { |
10457 | int i; | |
10458 | ||
10459 | Vcoding_category_list = Qnil; | |
df7492f9 | 10460 | for (i = coding_category_max - 1; i >= 0; i--) |
4ed46869 | 10461 | Vcoding_category_list |
28be1ada | 10462 | = Fcons (AREF (Vcoding_category_table, i), |
d46c5b12 | 10463 | Vcoding_category_list); |
4ed46869 KH |
10464 | } |
10465 | ||
29208e82 | 10466 | DEFVAR_LISP ("coding-system-for-read", Vcoding_system_for_read, |
48b0f3ae PJ |
10467 | doc: /* Specify the coding system for read operations. |
10468 | It is useful to bind this variable with `let', but do not set it globally. | |
10469 | If the value is a coding system, it is used for decoding on read operation. | |
446dcd75 JB |
10470 | If not, an appropriate element is used from one of the coding system alists. |
10471 | There are three such tables: `file-coding-system-alist', | |
48b0f3ae | 10472 | `process-coding-system-alist', and `network-coding-system-alist'. */); |
4ed46869 KH |
10473 | Vcoding_system_for_read = Qnil; |
10474 | ||
29208e82 | 10475 | DEFVAR_LISP ("coding-system-for-write", Vcoding_system_for_write, |
48b0f3ae PJ |
10476 | doc: /* Specify the coding system for write operations. |
10477 | Programs bind this variable with `let', but you should not set it globally. | |
10478 | If the value is a coding system, it is used for encoding of output, | |
10479 | when writing it to a file and when sending it to a file or subprocess. | |
10480 | ||
10481 | If this does not specify a coding system, an appropriate element | |
446dcd75 JB |
10482 | is used from one of the coding system alists. |
10483 | There are three such tables: `file-coding-system-alist', | |
48b0f3ae PJ |
10484 | `process-coding-system-alist', and `network-coding-system-alist'. |
10485 | For output to files, if the above procedure does not specify a coding system, | |
10486 | the value of `buffer-file-coding-system' is used. */); | |
4ed46869 KH |
10487 | Vcoding_system_for_write = Qnil; |
10488 | ||
29208e82 | 10489 | DEFVAR_LISP ("last-coding-system-used", Vlast_coding_system_used, |
df7492f9 KH |
10490 | doc: /* |
10491 | Coding system used in the latest file or process I/O. */); | |
4ed46869 KH |
10492 | Vlast_coding_system_used = Qnil; |
10493 | ||
29208e82 | 10494 | DEFVAR_LISP ("last-code-conversion-error", Vlast_code_conversion_error, |
065e3595 KH |
10495 | doc: /* |
10496 | Error status of the last code conversion. | |
10497 | ||
10498 | When an error was detected in the last code conversion, this variable | |
10499 | is set to one of the following symbols. | |
10500 | `insufficient-source' | |
10501 | `inconsistent-eol' | |
10502 | `invalid-source' | |
10503 | `interrupted' | |
10504 | `insufficient-memory' | |
10505 | When no error was detected, the value doesn't change. So, to check | |
10506 | the error status of a code conversion by this variable, you must | |
10507 | explicitly set this variable to nil before performing code | |
10508 | conversion. */); | |
10509 | Vlast_code_conversion_error = Qnil; | |
10510 | ||
29208e82 | 10511 | DEFVAR_BOOL ("inhibit-eol-conversion", inhibit_eol_conversion, |
df7492f9 KH |
10512 | doc: /* |
10513 | *Non-nil means always inhibit code conversion of end-of-line format. | |
48b0f3ae PJ |
10514 | See info node `Coding Systems' and info node `Text and Binary' concerning |
10515 | such conversion. */); | |
9ce27fde KH |
10516 | inhibit_eol_conversion = 0; |
10517 | ||
29208e82 | 10518 | DEFVAR_BOOL ("inherit-process-coding-system", inherit_process_coding_system, |
df7492f9 KH |
10519 | doc: /* |
10520 | Non-nil means process buffer inherits coding system of process output. | |
48b0f3ae PJ |
10521 | Bind it to t if the process output is to be treated as if it were a file |
10522 | read from some filesystem. */); | |
ed29121d EZ |
10523 | inherit_process_coding_system = 0; |
10524 | ||
29208e82 | 10525 | DEFVAR_LISP ("file-coding-system-alist", Vfile_coding_system_alist, |
df7492f9 KH |
10526 | doc: /* |
10527 | Alist to decide a coding system to use for a file I/O operation. | |
48b0f3ae PJ |
10528 | The format is ((PATTERN . VAL) ...), |
10529 | where PATTERN is a regular expression matching a file name, | |
10530 | VAL is a coding system, a cons of coding systems, or a function symbol. | |
10531 | If VAL is a coding system, it is used for both decoding and encoding | |
10532 | the file contents. | |
10533 | If VAL is a cons of coding systems, the car part is used for decoding, | |
10534 | and the cdr part is used for encoding. | |
10535 | If VAL is a function symbol, the function must return a coding system | |
2c53e699 KH |
10536 | or a cons of coding systems which are used as above. The function is |
10537 | called with an argument that is a list of the arguments with which | |
5a0bbd9a KH |
10538 | `find-operation-coding-system' was called. If the function can't decide |
10539 | a coding system, it can return `undecided' so that the normal | |
10540 | code-detection is performed. | |
48b0f3ae PJ |
10541 | |
10542 | See also the function `find-operation-coding-system' | |
10543 | and the variable `auto-coding-alist'. */); | |
02ba4723 KH |
10544 | Vfile_coding_system_alist = Qnil; |
10545 | ||
29208e82 | 10546 | DEFVAR_LISP ("process-coding-system-alist", Vprocess_coding_system_alist, |
df7492f9 KH |
10547 | doc: /* |
10548 | Alist to decide a coding system to use for a process I/O operation. | |
48b0f3ae PJ |
10549 | The format is ((PATTERN . VAL) ...), |
10550 | where PATTERN is a regular expression matching a program name, | |
10551 | VAL is a coding system, a cons of coding systems, or a function symbol. | |
10552 | If VAL is a coding system, it is used for both decoding what received | |
10553 | from the program and encoding what sent to the program. | |
10554 | If VAL is a cons of coding systems, the car part is used for decoding, | |
10555 | and the cdr part is used for encoding. | |
10556 | If VAL is a function symbol, the function must return a coding system | |
10557 | or a cons of coding systems which are used as above. | |
10558 | ||
10559 | See also the function `find-operation-coding-system'. */); | |
02ba4723 KH |
10560 | Vprocess_coding_system_alist = Qnil; |
10561 | ||
29208e82 | 10562 | DEFVAR_LISP ("network-coding-system-alist", Vnetwork_coding_system_alist, |
df7492f9 KH |
10563 | doc: /* |
10564 | Alist to decide a coding system to use for a network I/O operation. | |
48b0f3ae PJ |
10565 | The format is ((PATTERN . VAL) ...), |
10566 | where PATTERN is a regular expression matching a network service name | |
10567 | or is a port number to connect to, | |
10568 | VAL is a coding system, a cons of coding systems, or a function symbol. | |
10569 | If VAL is a coding system, it is used for both decoding what received | |
10570 | from the network stream and encoding what sent to the network stream. | |
10571 | If VAL is a cons of coding systems, the car part is used for decoding, | |
10572 | and the cdr part is used for encoding. | |
10573 | If VAL is a function symbol, the function must return a coding system | |
10574 | or a cons of coding systems which are used as above. | |
10575 | ||
10576 | See also the function `find-operation-coding-system'. */); | |
02ba4723 | 10577 | Vnetwork_coding_system_alist = Qnil; |
4ed46869 | 10578 | |
29208e82 | 10579 | DEFVAR_LISP ("locale-coding-system", Vlocale_coding_system, |
75205970 RS |
10580 | doc: /* Coding system to use with system messages. |
10581 | Also used for decoding keyboard input on X Window system. */); | |
68c45bf0 PE |
10582 | Vlocale_coding_system = Qnil; |
10583 | ||
005f0d35 | 10584 | /* The eol mnemonics are reset in startup.el system-dependently. */ |
29208e82 | 10585 | DEFVAR_LISP ("eol-mnemonic-unix", eol_mnemonic_unix, |
df7492f9 KH |
10586 | doc: /* |
10587 | *String displayed in mode line for UNIX-like (LF) end-of-line format. */); | |
2a0213a6 | 10588 | eol_mnemonic_unix = build_pure_c_string (":"); |
4ed46869 | 10589 | |
29208e82 | 10590 | DEFVAR_LISP ("eol-mnemonic-dos", eol_mnemonic_dos, |
df7492f9 KH |
10591 | doc: /* |
10592 | *String displayed in mode line for DOS-like (CRLF) end-of-line format. */); | |
2a0213a6 | 10593 | eol_mnemonic_dos = build_pure_c_string ("\\"); |
4ed46869 | 10594 | |
29208e82 | 10595 | DEFVAR_LISP ("eol-mnemonic-mac", eol_mnemonic_mac, |
df7492f9 KH |
10596 | doc: /* |
10597 | *String displayed in mode line for MAC-like (CR) end-of-line format. */); | |
2a0213a6 | 10598 | eol_mnemonic_mac = build_pure_c_string ("/"); |
4ed46869 | 10599 | |
29208e82 | 10600 | DEFVAR_LISP ("eol-mnemonic-undecided", eol_mnemonic_undecided, |
df7492f9 KH |
10601 | doc: /* |
10602 | *String displayed in mode line when end-of-line format is not yet determined. */); | |
2a0213a6 | 10603 | eol_mnemonic_undecided = build_pure_c_string (":"); |
4ed46869 | 10604 | |
29208e82 | 10605 | DEFVAR_LISP ("enable-character-translation", Venable_character_translation, |
df7492f9 KH |
10606 | doc: /* |
10607 | *Non-nil enables character translation while encoding and decoding. */); | |
84fbb8a0 | 10608 | Venable_character_translation = Qt; |
bdd9fb48 | 10609 | |
f967223b | 10610 | DEFVAR_LISP ("standard-translation-table-for-decode", |
29208e82 | 10611 | Vstandard_translation_table_for_decode, |
48b0f3ae | 10612 | doc: /* Table for translating characters while decoding. */); |
f967223b | 10613 | Vstandard_translation_table_for_decode = Qnil; |
bdd9fb48 | 10614 | |
f967223b | 10615 | DEFVAR_LISP ("standard-translation-table-for-encode", |
29208e82 | 10616 | Vstandard_translation_table_for_encode, |
48b0f3ae | 10617 | doc: /* Table for translating characters while encoding. */); |
f967223b | 10618 | Vstandard_translation_table_for_encode = Qnil; |
4ed46869 | 10619 | |
29208e82 | 10620 | DEFVAR_LISP ("charset-revision-table", Vcharset_revision_table, |
48b0f3ae PJ |
10621 | doc: /* Alist of charsets vs revision numbers. |
10622 | While encoding, if a charset (car part of an element) is found, | |
df7492f9 KH |
10623 | designate it with the escape sequence identifying revision (cdr part |
10624 | of the element). */); | |
10625 | Vcharset_revision_table = Qnil; | |
02ba4723 KH |
10626 | |
10627 | DEFVAR_LISP ("default-process-coding-system", | |
29208e82 | 10628 | Vdefault_process_coding_system, |
48b0f3ae PJ |
10629 | doc: /* Cons of coding systems used for process I/O by default. |
10630 | The car part is used for decoding a process output, | |
10631 | the cdr part is used for encoding a text to be sent to a process. */); | |
02ba4723 | 10632 | Vdefault_process_coding_system = Qnil; |
c4825358 | 10633 | |
29208e82 | 10634 | DEFVAR_LISP ("latin-extra-code-table", Vlatin_extra_code_table, |
df7492f9 KH |
10635 | doc: /* |
10636 | Table of extra Latin codes in the range 128..159 (inclusive). | |
48b0f3ae PJ |
10637 | This is a vector of length 256. |
10638 | If Nth element is non-nil, the existence of code N in a file | |
10639 | \(or output of subprocess) doesn't prevent it to be detected as | |
10640 | a coding system of ISO 2022 variant which has a flag | |
10641 | `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file | |
10642 | or reading output of a subprocess. | |
446dcd75 | 10643 | Only 128th through 159th elements have a meaning. */); |
3f003981 | 10644 | Vlatin_extra_code_table = Fmake_vector (make_number (256), Qnil); |
d46c5b12 KH |
10645 | |
10646 | DEFVAR_LISP ("select-safe-coding-system-function", | |
29208e82 | 10647 | Vselect_safe_coding_system_function, |
df7492f9 KH |
10648 | doc: /* |
10649 | Function to call to select safe coding system for encoding a text. | |
48b0f3ae PJ |
10650 | |
10651 | If set, this function is called to force a user to select a proper | |
10652 | coding system which can encode the text in the case that a default | |
fdecf907 GM |
10653 | coding system used in each operation can't encode the text. The |
10654 | function should take care that the buffer is not modified while | |
10655 | the coding system is being selected. | |
48b0f3ae PJ |
10656 | |
10657 | The default value is `select-safe-coding-system' (which see). */); | |
d46c5b12 KH |
10658 | Vselect_safe_coding_system_function = Qnil; |
10659 | ||
5d5bf4d8 | 10660 | DEFVAR_BOOL ("coding-system-require-warning", |
29208e82 | 10661 | coding_system_require_warning, |
5d5bf4d8 | 10662 | doc: /* Internal use only. |
6b89e3aa KH |
10663 | If non-nil, on writing a file, `select-safe-coding-system-function' is |
10664 | called even if `coding-system-for-write' is non-nil. The command | |
10665 | `universal-coding-system-argument' binds this variable to t temporarily. */); | |
5d5bf4d8 KH |
10666 | coding_system_require_warning = 0; |
10667 | ||
10668 | ||
22ab2303 | 10669 | DEFVAR_BOOL ("inhibit-iso-escape-detection", |
29208e82 | 10670 | inhibit_iso_escape_detection, |
df7492f9 | 10671 | doc: /* |
97b1b294 | 10672 | If non-nil, Emacs ignores ISO-2022 escape sequences during code detection. |
48b0f3ae | 10673 | |
97b1b294 EZ |
10674 | When Emacs reads text, it tries to detect how the text is encoded. |
10675 | This code detection is sensitive to escape sequences. If Emacs sees | |
10676 | a valid ISO-2022 escape sequence, it assumes the text is encoded in one | |
10677 | of the ISO2022 encodings, and decodes text by the corresponding coding | |
10678 | system (e.g. `iso-2022-7bit'). | |
48b0f3ae PJ |
10679 | |
10680 | However, there may be a case that you want to read escape sequences in | |
10681 | a file as is. In such a case, you can set this variable to non-nil. | |
97b1b294 EZ |
10682 | Then the code detection will ignore any escape sequences, and no text is |
10683 | detected as encoded in some ISO-2022 encoding. The result is that all | |
48b0f3ae PJ |
10684 | escape sequences become visible in a buffer. |
10685 | ||
10686 | The default value is nil, and it is strongly recommended not to change | |
10687 | it. That is because many Emacs Lisp source files that contain | |
10688 | non-ASCII characters are encoded by the coding system `iso-2022-7bit' | |
10689 | in Emacs's distribution, and they won't be decoded correctly on | |
10690 | reading if you suppress escape sequence detection. | |
10691 | ||
10692 | The other way to read escape sequences in a file without decoding is | |
97b1b294 | 10693 | to explicitly specify some coding system that doesn't use ISO-2022 |
65e7ca35 | 10694 | escape sequence (e.g., `latin-1') on reading by \\[universal-coding-system-argument]. */); |
74383408 | 10695 | inhibit_iso_escape_detection = 0; |
002fdb44 | 10696 | |
97b1b294 | 10697 | DEFVAR_BOOL ("inhibit-null-byte-detection", |
29208e82 | 10698 | inhibit_null_byte_detection, |
97b1b294 EZ |
10699 | doc: /* If non-nil, Emacs ignores null bytes on code detection. |
10700 | By default, Emacs treats it as binary data, and does not attempt to | |
10701 | decode it. The effect is as if you specified `no-conversion' for | |
10702 | reading that text. | |
10703 | ||
10704 | Set this to non-nil when a regular text happens to include null bytes. | |
10705 | Examples are Index nodes of Info files and null-byte delimited output | |
10706 | from GNU Find and GNU Grep. Emacs will then ignore the null bytes and | |
10707 | decode text as usual. */); | |
10708 | inhibit_null_byte_detection = 0; | |
10709 | ||
29208e82 | 10710 | DEFVAR_LISP ("translation-table-for-input", Vtranslation_table_for_input, |
15c8f9d1 | 10711 | doc: /* Char table for translating self-inserting characters. |
446dcd75 | 10712 | This is applied to the result of input methods, not their input. |
8434d0b8 EZ |
10713 | See also `keyboard-translate-table'. |
10714 | ||
10715 | Use of this variable for character code unification was rendered | |
10716 | obsolete in Emacs 23.1 and later, since Unicode is now the basis of | |
10717 | internal character representation. */); | |
002fdb44 | 10718 | Vtranslation_table_for_input = Qnil; |
8f924df7 | 10719 | |
2c78b7e1 KH |
10720 | { |
10721 | Lisp_Object args[coding_arg_max]; | |
8f924df7 | 10722 | Lisp_Object plist[16]; |
2c78b7e1 KH |
10723 | int i; |
10724 | ||
10725 | for (i = 0; i < coding_arg_max; i++) | |
10726 | args[i] = Qnil; | |
10727 | ||
d67b4f80 | 10728 | plist[0] = intern_c_string (":name"); |
2c78b7e1 | 10729 | plist[1] = args[coding_arg_name] = Qno_conversion; |
d67b4f80 | 10730 | plist[2] = intern_c_string (":mnemonic"); |
2c78b7e1 | 10731 | plist[3] = args[coding_arg_mnemonic] = make_number ('='); |
d67b4f80 | 10732 | plist[4] = intern_c_string (":coding-type"); |
2c78b7e1 | 10733 | plist[5] = args[coding_arg_coding_type] = Qraw_text; |
d67b4f80 | 10734 | plist[6] = intern_c_string (":ascii-compatible-p"); |
2c78b7e1 | 10735 | plist[7] = args[coding_arg_ascii_compatible_p] = Qt; |
d67b4f80 | 10736 | plist[8] = intern_c_string (":default-char"); |
2c78b7e1 | 10737 | plist[9] = args[coding_arg_default_char] = make_number (0); |
d67b4f80 | 10738 | plist[10] = intern_c_string (":for-unibyte"); |
8f924df7 | 10739 | plist[11] = args[coding_arg_for_unibyte] = Qt; |
d67b4f80 | 10740 | plist[12] = intern_c_string (":docstring"); |
2a0213a6 | 10741 | plist[13] = build_pure_c_string ("Do no conversion.\n\ |
2c78b7e1 KH |
10742 | \n\ |
10743 | When you visit a file with this coding, the file is read into a\n\ | |
10744 | unibyte buffer as is, thus each byte of a file is treated as a\n\ | |
10745 | character."); | |
d67b4f80 | 10746 | plist[14] = intern_c_string (":eol-type"); |
8f924df7 KH |
10747 | plist[15] = args[coding_arg_eol_type] = Qunix; |
10748 | args[coding_arg_plist] = Flist (16, plist); | |
2c78b7e1 | 10749 | Fdefine_coding_system_internal (coding_arg_max, args); |
ae6f73fa KH |
10750 | |
10751 | plist[1] = args[coding_arg_name] = Qundecided; | |
10752 | plist[3] = args[coding_arg_mnemonic] = make_number ('-'); | |
10753 | plist[5] = args[coding_arg_coding_type] = Qundecided; | |
10754 | /* This is already set. | |
35befdaa | 10755 | plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */ |
d67b4f80 | 10756 | plist[8] = intern_c_string (":charset-list"); |
ae6f73fa KH |
10757 | plist[9] = args[coding_arg_charset_list] = Fcons (Qascii, Qnil); |
10758 | plist[11] = args[coding_arg_for_unibyte] = Qnil; | |
2a0213a6 | 10759 | plist[13] = build_pure_c_string ("No conversion on encoding, automatic conversion on decoding."); |
ae6f73fa KH |
10760 | plist[15] = args[coding_arg_eol_type] = Qnil; |
10761 | args[coding_arg_plist] = Flist (16, plist); | |
10762 | Fdefine_coding_system_internal (coding_arg_max, args); | |
2c78b7e1 KH |
10763 | } |
10764 | ||
2c78b7e1 | 10765 | setup_coding_system (Qno_conversion, &safe_terminal_coding); |
ff563fce KH |
10766 | |
10767 | { | |
10768 | int i; | |
10769 | ||
10770 | for (i = 0; i < coding_category_max; i++) | |
10771 | Fset (AREF (Vcoding_category_table, i), Qno_conversion); | |
10772 | } | |
1a4990fb | 10773 | #if defined (DOS_NT) |
fcbcfb64 KH |
10774 | system_eol_type = Qdos; |
10775 | #else | |
10776 | system_eol_type = Qunix; | |
10777 | #endif | |
10778 | staticpro (&system_eol_type); | |
4ed46869 KH |
10779 | } |
10780 | ||
68c45bf0 | 10781 | char * |
971de7fb | 10782 | emacs_strerror (int error_number) |
68c45bf0 PE |
10783 | { |
10784 | char *str; | |
10785 | ||
ca9c0567 | 10786 | synchronize_system_messages_locale (); |
68c45bf0 PE |
10787 | str = strerror (error_number); |
10788 | ||
10789 | if (! NILP (Vlocale_coding_system)) | |
10790 | { | |
10791 | Lisp_Object dec = code_convert_string_norecord (build_string (str), | |
10792 | Vlocale_coding_system, | |
10793 | 0); | |
51b59d79 | 10794 | str = SSDATA (dec); |
68c45bf0 PE |
10795 | } |
10796 | ||
10797 | return str; | |
10798 | } | |
10799 | ||
4ed46869 | 10800 | #endif /* emacs */ |