Commit | Line | Data |
---|---|---|
9542cb1f | 1 | /* Coding system handler (conversion, detection, etc). |
73b0cd50 | 2 | Copyright (C) 2001-2011 Free Software Foundation, Inc. |
7976eda0 | 3 | Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
5df4f04c | 4 | 2005, 2006, 2007, 2008, 2009, 2010, 2011 |
ce03bf76 KH |
5 | National Institute of Advanced Industrial Science and Technology (AIST) |
6 | Registration Number H14PRO021 | |
8f924df7 | 7 | Copyright (C) 2003 |
df7492f9 KH |
8 | National Institute of Advanced Industrial Science and Technology (AIST) |
9 | Registration Number H13PRO009 | |
4ed46869 | 10 | |
369314dc KH |
11 | This file is part of GNU Emacs. |
12 | ||
9ec0b715 | 13 | GNU Emacs is free software: you can redistribute it and/or modify |
369314dc | 14 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
15 | the Free Software Foundation, either version 3 of the License, or |
16 | (at your option) any later version. | |
4ed46869 | 17 | |
369314dc KH |
18 | GNU Emacs is distributed in the hope that it will be useful, |
19 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | GNU General Public License for more details. | |
4ed46869 | 22 | |
369314dc | 23 | You should have received a copy of the GNU General Public License |
9ec0b715 | 24 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
4ed46869 KH |
25 | |
26 | /*** TABLE OF CONTENTS *** | |
27 | ||
b73bfc1c | 28 | 0. General comments |
4ed46869 | 29 | 1. Preamble |
df7492f9 KH |
30 | 2. Emacs' internal format (emacs-utf-8) handlers |
31 | 3. UTF-8 handlers | |
32 | 4. UTF-16 handlers | |
33 | 5. Charset-base coding systems handlers | |
34 | 6. emacs-mule (old Emacs' internal format) handlers | |
35 | 7. ISO2022 handlers | |
36 | 8. Shift-JIS and BIG5 handlers | |
37 | 9. CCL handlers | |
38 | 10. C library functions | |
39 | 11. Emacs Lisp library functions | |
40 | 12. Postamble | |
4ed46869 KH |
41 | |
42 | */ | |
43 | ||
df7492f9 | 44 | /*** 0. General comments *** |
b73bfc1c KH |
45 | |
46 | ||
df7492f9 | 47 | CODING SYSTEM |
4ed46869 | 48 | |
5bad0796 DL |
49 | A coding system is an object for an encoding mechanism that contains |
50 | information about how to convert byte sequences to character | |
e19c3639 KH |
51 | sequences and vice versa. When we say "decode", it means converting |
52 | a byte sequence of a specific coding system into a character | |
53 | sequence that is represented by Emacs' internal coding system | |
54 | `emacs-utf-8', and when we say "encode", it means converting a | |
55 | character sequence of emacs-utf-8 to a byte sequence of a specific | |
0ef69138 | 56 | coding system. |
4ed46869 | 57 | |
e19c3639 KH |
58 | In Emacs Lisp, a coding system is represented by a Lisp symbol. In |
59 | C level, a coding system is represented by a vector of attributes | |
5bad0796 | 60 | stored in the hash table Vcharset_hash_table. The conversion from |
e19c3639 KH |
61 | coding system symbol to attributes vector is done by looking up |
62 | Vcharset_hash_table by the symbol. | |
4ed46869 | 63 | |
e19c3639 | 64 | Coding systems are classified into the following types depending on |
5bad0796 | 65 | the encoding mechanism. Here's a brief description of the types. |
4ed46869 | 66 | |
df7492f9 KH |
67 | o UTF-8 |
68 | ||
69 | o UTF-16 | |
70 | ||
71 | o Charset-base coding system | |
72 | ||
73 | A coding system defined by one or more (coded) character sets. | |
5bad0796 | 74 | Decoding and encoding are done by a code converter defined for each |
df7492f9 KH |
75 | character set. |
76 | ||
5bad0796 | 77 | o Old Emacs internal format (emacs-mule) |
df7492f9 | 78 | |
5bad0796 | 79 | The coding system adopted by old versions of Emacs (20 and 21). |
4ed46869 | 80 | |
df7492f9 | 81 | o ISO2022-base coding system |
4ed46869 KH |
82 | |
83 | The most famous coding system for multiple character sets. X's | |
df7492f9 KH |
84 | Compound Text, various EUCs (Extended Unix Code), and coding systems |
85 | used in the Internet communication such as ISO-2022-JP are all | |
86 | variants of ISO2022. | |
4ed46869 | 87 | |
df7492f9 | 88 | o SJIS (or Shift-JIS or MS-Kanji-Code) |
93dec019 | 89 | |
4ed46869 KH |
90 | A coding system to encode character sets: ASCII, JISX0201, and |
91 | JISX0208. Widely used for PC's in Japan. Details are described in | |
df7492f9 | 92 | section 8. |
4ed46869 | 93 | |
df7492f9 | 94 | o BIG5 |
4ed46869 | 95 | |
df7492f9 | 96 | A coding system to encode character sets: ASCII and Big5. Widely |
cfb43547 | 97 | used for Chinese (mainly in Taiwan and Hong Kong). Details are |
df7492f9 KH |
98 | described in section 8. In this file, when we write "big5" (all |
99 | lowercase), we mean the coding system, and when we write "Big5" | |
100 | (capitalized), we mean the character set. | |
4ed46869 | 101 | |
df7492f9 | 102 | o CCL |
27901516 | 103 | |
5bad0796 | 104 | If a user wants to decode/encode text encoded in a coding system |
df7492f9 KH |
105 | not listed above, he can supply a decoder and an encoder for it in |
106 | CCL (Code Conversion Language) programs. Emacs executes the CCL | |
107 | program while decoding/encoding. | |
27901516 | 108 | |
df7492f9 | 109 | o Raw-text |
4ed46869 | 110 | |
5a936b46 | 111 | A coding system for text containing raw eight-bit data. Emacs |
5bad0796 | 112 | treats each byte of source text as a character (except for |
df7492f9 | 113 | end-of-line conversion). |
4ed46869 | 114 | |
df7492f9 KH |
115 | o No-conversion |
116 | ||
117 | Like raw text, but don't do end-of-line conversion. | |
4ed46869 | 118 | |
4ed46869 | 119 | |
df7492f9 | 120 | END-OF-LINE FORMAT |
4ed46869 | 121 | |
5bad0796 | 122 | How text end-of-line is encoded depends on operating system. For |
df7492f9 | 123 | instance, Unix's format is just one byte of LF (line-feed) code, |
f4dee582 | 124 | whereas DOS's format is two-byte sequence of `carriage-return' and |
d46c5b12 KH |
125 | `line-feed' codes. MacOS's format is usually one byte of |
126 | `carriage-return'. | |
4ed46869 | 127 | |
cfb43547 | 128 | Since text character encoding and end-of-line encoding are |
df7492f9 KH |
129 | independent, any coding system described above can take any format |
130 | of end-of-line (except for no-conversion). | |
4ed46869 | 131 | |
e19c3639 KH |
132 | STRUCT CODING_SYSTEM |
133 | ||
134 | Before using a coding system for code conversion (i.e. decoding and | |
135 | encoding), we setup a structure of type `struct coding_system'. | |
136 | This structure keeps various information about a specific code | |
5bad0796 | 137 | conversion (e.g. the location of source and destination data). |
4ed46869 KH |
138 | |
139 | */ | |
140 | ||
df7492f9 KH |
141 | /* COMMON MACROS */ |
142 | ||
143 | ||
4ed46869 KH |
144 | /*** GENERAL NOTES on `detect_coding_XXX ()' functions *** |
145 | ||
df7492f9 | 146 | These functions check if a byte sequence specified as a source in |
ff0dacd7 KH |
147 | CODING conforms to the format of XXX, and update the members of |
148 | DETECT_INFO. | |
df7492f9 | 149 | |
ff0dacd7 | 150 | Return 1 if the byte sequence conforms to XXX, otherwise return 0. |
df7492f9 KH |
151 | |
152 | Below is the template of these functions. */ | |
153 | ||
4ed46869 | 154 | #if 0 |
df7492f9 | 155 | static int |
cf84bb53 JB |
156 | detect_coding_XXX (struct coding_system *coding, |
157 | struct coding_detection_info *detect_info) | |
4ed46869 | 158 | { |
f1d34bca MB |
159 | const unsigned char *src = coding->source; |
160 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
df7492f9 | 161 | int multibytep = coding->src_multibyte; |
ff0dacd7 | 162 | int consumed_chars = 0; |
df7492f9 KH |
163 | int found = 0; |
164 | ...; | |
165 | ||
166 | while (1) | |
167 | { | |
ad1746f5 | 168 | /* Get one byte from the source. If the source is exhausted, jump |
df7492f9 KH |
169 | to no_more_source:. */ |
170 | ONE_MORE_BYTE (c); | |
ff0dacd7 KH |
171 | |
172 | if (! __C_conforms_to_XXX___ (c)) | |
173 | break; | |
174 | if (! __C_strongly_suggests_XXX__ (c)) | |
175 | found = CATEGORY_MASK_XXX; | |
df7492f9 | 176 | } |
ff0dacd7 KH |
177 | /* The byte sequence is invalid for XXX. */ |
178 | detect_info->rejected |= CATEGORY_MASK_XXX; | |
df7492f9 | 179 | return 0; |
ff0dacd7 | 180 | |
df7492f9 | 181 | no_more_source: |
ad1746f5 | 182 | /* The source exhausted successfully. */ |
ff0dacd7 | 183 | detect_info->found |= found; |
df7492f9 | 184 | return 1; |
4ed46869 KH |
185 | } |
186 | #endif | |
187 | ||
188 | /*** GENERAL NOTES on `decode_coding_XXX ()' functions *** | |
189 | ||
df7492f9 KH |
190 | These functions decode a byte sequence specified as a source by |
191 | CODING. The resulting multibyte text goes to a place pointed to by | |
192 | CODING->charbuf, the length of which should not exceed | |
193 | CODING->charbuf_size; | |
d46c5b12 | 194 | |
df7492f9 KH |
195 | These functions set the information of original and decoded texts in |
196 | CODING->consumed, CODING->consumed_char, and CODING->charbuf_used. | |
197 | They also set CODING->result to one of CODING_RESULT_XXX indicating | |
198 | how the decoding is finished. | |
d46c5b12 | 199 | |
df7492f9 | 200 | Below is the template of these functions. */ |
d46c5b12 | 201 | |
4ed46869 | 202 | #if 0 |
b73bfc1c | 203 | static void |
cf84bb53 | 204 | decode_coding_XXXX (struct coding_system *coding) |
4ed46869 | 205 | { |
f1d34bca MB |
206 | const unsigned char *src = coding->source + coding->consumed; |
207 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
df7492f9 KH |
208 | /* SRC_BASE remembers the start position in source in each loop. |
209 | The loop will be exited when there's not enough source code, or | |
210 | when there's no room in CHARBUF for a decoded character. */ | |
f1d34bca | 211 | const unsigned char *src_base; |
df7492f9 | 212 | /* A buffer to produce decoded characters. */ |
69a80ea3 KH |
213 | int *charbuf = coding->charbuf + coding->charbuf_used; |
214 | int *charbuf_end = coding->charbuf + coding->charbuf_size; | |
df7492f9 KH |
215 | int multibytep = coding->src_multibyte; |
216 | ||
217 | while (1) | |
218 | { | |
219 | src_base = src; | |
220 | if (charbuf < charbuf_end) | |
221 | /* No more room to produce a decoded character. */ | |
222 | break; | |
223 | ONE_MORE_BYTE (c); | |
224 | /* Decode it. */ | |
225 | } | |
226 | ||
227 | no_more_source: | |
228 | if (src_base < src_end | |
229 | && coding->mode & CODING_MODE_LAST_BLOCK) | |
230 | /* If the source ends by partial bytes to construct a character, | |
231 | treat them as eight-bit raw data. */ | |
232 | while (src_base < src_end && charbuf < charbuf_end) | |
233 | *charbuf++ = *src_base++; | |
234 | /* Remember how many bytes and characters we consumed. If the | |
235 | source is multibyte, the bytes and chars are not identical. */ | |
236 | coding->consumed = coding->consumed_char = src_base - coding->source; | |
237 | /* Remember how many characters we produced. */ | |
238 | coding->charbuf_used = charbuf - coding->charbuf; | |
4ed46869 KH |
239 | } |
240 | #endif | |
241 | ||
242 | /*** GENERAL NOTES on `encode_coding_XXX ()' functions *** | |
243 | ||
df7492f9 KH |
244 | These functions encode SRC_BYTES length text at SOURCE of Emacs' |
245 | internal multibyte format by CODING. The resulting byte sequence | |
b73bfc1c KH |
246 | goes to a place pointed to by DESTINATION, the length of which |
247 | should not exceed DST_BYTES. | |
d46c5b12 | 248 | |
df7492f9 KH |
249 | These functions set the information of original and encoded texts in |
250 | the members produced, produced_char, consumed, and consumed_char of | |
251 | the structure *CODING. They also set the member result to one of | |
252 | CODING_RESULT_XXX indicating how the encoding finished. | |
d46c5b12 | 253 | |
df7492f9 KH |
254 | DST_BYTES zero means that source area and destination area are |
255 | overlapped, which means that we can produce a encoded text until it | |
256 | reaches at the head of not-yet-encoded source text. | |
d46c5b12 | 257 | |
df7492f9 | 258 | Below is a template of these functions. */ |
4ed46869 | 259 | #if 0 |
b73bfc1c | 260 | static void |
cf84bb53 | 261 | encode_coding_XXX (struct coding_system *coding) |
4ed46869 | 262 | { |
df7492f9 KH |
263 | int multibytep = coding->dst_multibyte; |
264 | int *charbuf = coding->charbuf; | |
265 | int *charbuf_end = charbuf->charbuf + coding->charbuf_used; | |
266 | unsigned char *dst = coding->destination + coding->produced; | |
267 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
268 | unsigned char *adjusted_dst_end = dst_end - _MAX_BYTES_PRODUCED_IN_LOOP_; | |
269 | int produced_chars = 0; | |
270 | ||
271 | for (; charbuf < charbuf_end && dst < adjusted_dst_end; charbuf++) | |
272 | { | |
273 | int c = *charbuf; | |
274 | /* Encode C into DST, and increment DST. */ | |
275 | } | |
276 | label_no_more_destination: | |
277 | /* How many chars and bytes we produced. */ | |
278 | coding->produced_char += produced_chars; | |
279 | coding->produced = dst - coding->destination; | |
4ed46869 KH |
280 | } |
281 | #endif | |
282 | ||
4ed46869 KH |
283 | \f |
284 | /*** 1. Preamble ***/ | |
285 | ||
68c45bf0 | 286 | #include <config.h> |
4ed46869 | 287 | #include <stdio.h> |
d7306fe6 | 288 | #include <setjmp.h> |
4ed46869 | 289 | |
4ed46869 KH |
290 | #include "lisp.h" |
291 | #include "buffer.h" | |
df7492f9 | 292 | #include "character.h" |
4ed46869 KH |
293 | #include "charset.h" |
294 | #include "ccl.h" | |
df7492f9 | 295 | #include "composite.h" |
4ed46869 KH |
296 | #include "coding.h" |
297 | #include "window.h" | |
b8299c66 KL |
298 | #include "frame.h" |
299 | #include "termhooks.h" | |
4ed46869 | 300 | |
df7492f9 | 301 | Lisp_Object Vcoding_system_hash_table; |
4ed46869 | 302 | |
df7492f9 | 303 | Lisp_Object Qcoding_system, Qcoding_aliases, Qeol_type; |
1965cb73 | 304 | Lisp_Object Qunix, Qdos; |
4ed46869 KH |
305 | Lisp_Object Qbuffer_file_coding_system; |
306 | Lisp_Object Qpost_read_conversion, Qpre_write_conversion; | |
df7492f9 | 307 | Lisp_Object Qdefault_char; |
27901516 | 308 | Lisp_Object Qno_conversion, Qundecided; |
df7492f9 | 309 | Lisp_Object Qcharset, Qiso_2022, Qutf_8, Qutf_16, Qshift_jis, Qbig5; |
b49a1807 | 310 | Lisp_Object Qbig, Qlittle; |
bb0115a2 | 311 | Lisp_Object Qcoding_system_history; |
1397dc18 | 312 | Lisp_Object Qvalid_codes; |
2133e2d1 | 313 | Lisp_Object QCcategory, QCmnemonic, QCdefault_char; |
a6f87d34 KH |
314 | Lisp_Object QCdecode_translation_table, QCencode_translation_table; |
315 | Lisp_Object QCpost_read_conversion, QCpre_write_conversion; | |
35befdaa | 316 | Lisp_Object QCascii_compatible_p; |
4ed46869 | 317 | |
387f6ba5 | 318 | Lisp_Object Qcall_process, Qcall_process_region; |
4ed46869 KH |
319 | Lisp_Object Qstart_process, Qopen_network_stream; |
320 | Lisp_Object Qtarget_idx; | |
321 | ||
065e3595 KH |
322 | Lisp_Object Qinsufficient_source, Qinconsistent_eol, Qinvalid_source; |
323 | Lisp_Object Qinterrupted, Qinsufficient_memory; | |
324 | ||
44e8490d KH |
325 | /* If a symbol has this property, evaluate the value to define the |
326 | symbol as a coding system. */ | |
327 | static Lisp_Object Qcoding_system_define_form; | |
328 | ||
fcbcfb64 KH |
329 | /* Format of end-of-line decided by system. This is Qunix on |
330 | Unix and Mac, Qdos on DOS/Windows. | |
331 | This has an effect only for external encoding (i.e. for output to | |
332 | file and process), not for in-buffer or Lisp string encoding. */ | |
333 | static Lisp_Object system_eol_type; | |
334 | ||
4ed46869 KH |
335 | #ifdef emacs |
336 | ||
4608c386 | 337 | Lisp_Object Qcoding_system_p, Qcoding_system_error; |
4ed46869 | 338 | |
d46c5b12 KH |
339 | /* Coding system emacs-mule and raw-text are for converting only |
340 | end-of-line format. */ | |
341 | Lisp_Object Qemacs_mule, Qraw_text; | |
8f924df7 | 342 | Lisp_Object Qutf_8_emacs; |
ecf488bc | 343 | |
4ed46869 KH |
344 | /* Coding-systems are handed between Emacs Lisp programs and C internal |
345 | routines by the following three variables. */ | |
c4825358 KH |
346 | /* Coding system to be used to encode text for terminal display when |
347 | terminal coding system is nil. */ | |
348 | struct coding_system safe_terminal_coding; | |
349 | ||
4ed46869 KH |
350 | #endif /* emacs */ |
351 | ||
f967223b KH |
352 | Lisp_Object Qtranslation_table; |
353 | Lisp_Object Qtranslation_table_id; | |
354 | Lisp_Object Qtranslation_table_for_decode; | |
355 | Lisp_Object Qtranslation_table_for_encode; | |
4ed46869 | 356 | |
df7492f9 KH |
357 | /* Two special coding systems. */ |
358 | Lisp_Object Vsjis_coding_system; | |
359 | Lisp_Object Vbig5_coding_system; | |
360 | ||
df7492f9 KH |
361 | /* ISO2022 section */ |
362 | ||
363 | #define CODING_ISO_INITIAL(coding, reg) \ | |
364 | (XINT (AREF (AREF (CODING_ID_ATTRS ((coding)->id), \ | |
365 | coding_attr_iso_initial), \ | |
366 | reg))) | |
367 | ||
368 | ||
1b3b981b AS |
369 | #define CODING_ISO_REQUEST(coding, charset_id) \ |
370 | (((charset_id) <= (coding)->max_charset_id \ | |
371 | ? ((coding)->safe_charsets[charset_id] != 255 \ | |
372 | ? (coding)->safe_charsets[charset_id] \ | |
373 | : -1) \ | |
df7492f9 KH |
374 | : -1)) |
375 | ||
376 | ||
377 | #define CODING_ISO_FLAGS(coding) \ | |
378 | ((coding)->spec.iso_2022.flags) | |
379 | #define CODING_ISO_DESIGNATION(coding, reg) \ | |
380 | ((coding)->spec.iso_2022.current_designation[reg]) | |
381 | #define CODING_ISO_INVOCATION(coding, plane) \ | |
382 | ((coding)->spec.iso_2022.current_invocation[plane]) | |
383 | #define CODING_ISO_SINGLE_SHIFTING(coding) \ | |
384 | ((coding)->spec.iso_2022.single_shifting) | |
385 | #define CODING_ISO_BOL(coding) \ | |
386 | ((coding)->spec.iso_2022.bol) | |
387 | #define CODING_ISO_INVOKED_CHARSET(coding, plane) \ | |
388 | CODING_ISO_DESIGNATION ((coding), CODING_ISO_INVOCATION ((coding), (plane))) | |
e951386e KH |
389 | #define CODING_ISO_CMP_STATUS(coding) \ |
390 | (&(coding)->spec.iso_2022.cmp_status) | |
391 | #define CODING_ISO_EXTSEGMENT_LEN(coding) \ | |
392 | ((coding)->spec.iso_2022.ctext_extended_segment_len) | |
393 | #define CODING_ISO_EMBEDDED_UTF_8(coding) \ | |
394 | ((coding)->spec.iso_2022.embedded_utf_8) | |
df7492f9 KH |
395 | |
396 | /* Control characters of ISO2022. */ | |
397 | /* code */ /* function */ | |
df7492f9 KH |
398 | #define ISO_CODE_SO 0x0E /* shift-out */ |
399 | #define ISO_CODE_SI 0x0F /* shift-in */ | |
400 | #define ISO_CODE_SS2_7 0x19 /* single-shift-2 for 7-bit code */ | |
401 | #define ISO_CODE_ESC 0x1B /* escape */ | |
402 | #define ISO_CODE_SS2 0x8E /* single-shift-2 */ | |
403 | #define ISO_CODE_SS3 0x8F /* single-shift-3 */ | |
404 | #define ISO_CODE_CSI 0x9B /* control-sequence-introducer */ | |
405 | ||
406 | /* All code (1-byte) of ISO2022 is classified into one of the | |
407 | followings. */ | |
408 | enum iso_code_class_type | |
409 | { | |
410 | ISO_control_0, /* Control codes in the range | |
411 | 0x00..0x1F and 0x7F, except for the | |
412 | following 5 codes. */ | |
df7492f9 KH |
413 | ISO_shift_out, /* ISO_CODE_SO (0x0E) */ |
414 | ISO_shift_in, /* ISO_CODE_SI (0x0F) */ | |
415 | ISO_single_shift_2_7, /* ISO_CODE_SS2_7 (0x19) */ | |
416 | ISO_escape, /* ISO_CODE_SO (0x1B) */ | |
417 | ISO_control_1, /* Control codes in the range | |
418 | 0x80..0x9F, except for the | |
419 | following 3 codes. */ | |
420 | ISO_single_shift_2, /* ISO_CODE_SS2 (0x8E) */ | |
421 | ISO_single_shift_3, /* ISO_CODE_SS3 (0x8F) */ | |
422 | ISO_control_sequence_introducer, /* ISO_CODE_CSI (0x9B) */ | |
423 | ISO_0x20_or_0x7F, /* Codes of the values 0x20 or 0x7F. */ | |
424 | ISO_graphic_plane_0, /* Graphic codes in the range 0x21..0x7E. */ | |
425 | ISO_0xA0_or_0xFF, /* Codes of the values 0xA0 or 0xFF. */ | |
426 | ISO_graphic_plane_1 /* Graphic codes in the range 0xA1..0xFE. */ | |
427 | }; | |
05e6f5dc | 428 | |
df7492f9 KH |
429 | /** The macros CODING_ISO_FLAG_XXX defines a flag bit of the |
430 | `iso-flags' attribute of an iso2022 coding system. */ | |
05e6f5dc | 431 | |
df7492f9 KH |
432 | /* If set, produce long-form designation sequence (e.g. ESC $ ( A) |
433 | instead of the correct short-form sequence (e.g. ESC $ A). */ | |
434 | #define CODING_ISO_FLAG_LONG_FORM 0x0001 | |
93dec019 | 435 | |
df7492f9 KH |
436 | /* If set, reset graphic planes and registers at end-of-line to the |
437 | initial state. */ | |
438 | #define CODING_ISO_FLAG_RESET_AT_EOL 0x0002 | |
05e6f5dc | 439 | |
df7492f9 KH |
440 | /* If set, reset graphic planes and registers before any control |
441 | characters to the initial state. */ | |
442 | #define CODING_ISO_FLAG_RESET_AT_CNTL 0x0004 | |
05e6f5dc | 443 | |
df7492f9 KH |
444 | /* If set, encode by 7-bit environment. */ |
445 | #define CODING_ISO_FLAG_SEVEN_BITS 0x0008 | |
4ed46869 | 446 | |
df7492f9 KH |
447 | /* If set, use locking-shift function. */ |
448 | #define CODING_ISO_FLAG_LOCKING_SHIFT 0x0010 | |
b73bfc1c | 449 | |
df7492f9 KH |
450 | /* If set, use single-shift function. Overwrite |
451 | CODING_ISO_FLAG_LOCKING_SHIFT. */ | |
452 | #define CODING_ISO_FLAG_SINGLE_SHIFT 0x0020 | |
b73bfc1c | 453 | |
df7492f9 KH |
454 | /* If set, use designation escape sequence. */ |
455 | #define CODING_ISO_FLAG_DESIGNATION 0x0040 | |
b73bfc1c | 456 | |
df7492f9 KH |
457 | /* If set, produce revision number sequence. */ |
458 | #define CODING_ISO_FLAG_REVISION 0x0080 | |
b73bfc1c | 459 | |
df7492f9 KH |
460 | /* If set, produce ISO6429's direction specifying sequence. */ |
461 | #define CODING_ISO_FLAG_DIRECTION 0x0100 | |
f4dee582 | 462 | |
df7492f9 KH |
463 | /* If set, assume designation states are reset at beginning of line on |
464 | output. */ | |
465 | #define CODING_ISO_FLAG_INIT_AT_BOL 0x0200 | |
4ed46869 | 466 | |
df7492f9 KH |
467 | /* If set, designation sequence should be placed at beginning of line |
468 | on output. */ | |
469 | #define CODING_ISO_FLAG_DESIGNATE_AT_BOL 0x0400 | |
aa72b389 | 470 | |
ad1746f5 | 471 | /* If set, do not encode unsafe characters on output. */ |
df7492f9 | 472 | #define CODING_ISO_FLAG_SAFE 0x0800 |
aa72b389 | 473 | |
df7492f9 KH |
474 | /* If set, extra latin codes (128..159) are accepted as a valid code |
475 | on input. */ | |
476 | #define CODING_ISO_FLAG_LATIN_EXTRA 0x1000 | |
aa72b389 | 477 | |
df7492f9 | 478 | #define CODING_ISO_FLAG_COMPOSITION 0x2000 |
aa72b389 | 479 | |
5f58e762 | 480 | /* #define CODING_ISO_FLAG_EUC_TW_SHIFT 0x4000 */ |
aa72b389 | 481 | |
bf16eb23 | 482 | #define CODING_ISO_FLAG_USE_ROMAN 0x8000 |
aa72b389 | 483 | |
bf16eb23 | 484 | #define CODING_ISO_FLAG_USE_OLDJIS 0x10000 |
aa72b389 | 485 | |
bf16eb23 | 486 | #define CODING_ISO_FLAG_FULL_SUPPORT 0x100000 |
aa72b389 | 487 | |
df7492f9 KH |
488 | /* A character to be produced on output if encoding of the original |
489 | character is prohibited by CODING_ISO_FLAG_SAFE. */ | |
490 | #define CODING_INHIBIT_CHARACTER_SUBSTITUTION '?' | |
aa72b389 | 491 | |
a470d443 KH |
492 | /* UTF-8 section */ |
493 | #define CODING_UTF_8_BOM(coding) \ | |
494 | ((coding)->spec.utf_8_bom) | |
4ed46869 | 495 | |
df7492f9 KH |
496 | /* UTF-16 section */ |
497 | #define CODING_UTF_16_BOM(coding) \ | |
498 | ((coding)->spec.utf_16.bom) | |
4ed46869 | 499 | |
df7492f9 KH |
500 | #define CODING_UTF_16_ENDIAN(coding) \ |
501 | ((coding)->spec.utf_16.endian) | |
4ed46869 | 502 | |
df7492f9 KH |
503 | #define CODING_UTF_16_SURROGATE(coding) \ |
504 | ((coding)->spec.utf_16.surrogate) | |
4ed46869 | 505 | |
4ed46869 | 506 | |
df7492f9 KH |
507 | /* CCL section */ |
508 | #define CODING_CCL_DECODER(coding) \ | |
509 | AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_decoder) | |
510 | #define CODING_CCL_ENCODER(coding) \ | |
511 | AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_encoder) | |
512 | #define CODING_CCL_VALIDS(coding) \ | |
8f924df7 | 513 | (SDATA (AREF (CODING_ID_ATTRS ((coding)->id), coding_attr_ccl_valids))) |
4ed46869 | 514 | |
5a936b46 | 515 | /* Index for each coding category in `coding_categories' */ |
4ed46869 | 516 | |
df7492f9 KH |
517 | enum coding_category |
518 | { | |
519 | coding_category_iso_7, | |
520 | coding_category_iso_7_tight, | |
521 | coding_category_iso_8_1, | |
522 | coding_category_iso_8_2, | |
523 | coding_category_iso_7_else, | |
524 | coding_category_iso_8_else, | |
a470d443 KH |
525 | coding_category_utf_8_auto, |
526 | coding_category_utf_8_nosig, | |
527 | coding_category_utf_8_sig, | |
df7492f9 KH |
528 | coding_category_utf_16_auto, |
529 | coding_category_utf_16_be, | |
530 | coding_category_utf_16_le, | |
531 | coding_category_utf_16_be_nosig, | |
532 | coding_category_utf_16_le_nosig, | |
533 | coding_category_charset, | |
534 | coding_category_sjis, | |
535 | coding_category_big5, | |
536 | coding_category_ccl, | |
537 | coding_category_emacs_mule, | |
538 | /* All above are targets of code detection. */ | |
539 | coding_category_raw_text, | |
540 | coding_category_undecided, | |
541 | coding_category_max | |
542 | }; | |
543 | ||
544 | /* Definitions of flag bits used in detect_coding_XXXX. */ | |
545 | #define CATEGORY_MASK_ISO_7 (1 << coding_category_iso_7) | |
546 | #define CATEGORY_MASK_ISO_7_TIGHT (1 << coding_category_iso_7_tight) | |
547 | #define CATEGORY_MASK_ISO_8_1 (1 << coding_category_iso_8_1) | |
548 | #define CATEGORY_MASK_ISO_8_2 (1 << coding_category_iso_8_2) | |
549 | #define CATEGORY_MASK_ISO_7_ELSE (1 << coding_category_iso_7_else) | |
550 | #define CATEGORY_MASK_ISO_8_ELSE (1 << coding_category_iso_8_else) | |
a470d443 KH |
551 | #define CATEGORY_MASK_UTF_8_AUTO (1 << coding_category_utf_8_auto) |
552 | #define CATEGORY_MASK_UTF_8_NOSIG (1 << coding_category_utf_8_nosig) | |
553 | #define CATEGORY_MASK_UTF_8_SIG (1 << coding_category_utf_8_sig) | |
b49a1807 | 554 | #define CATEGORY_MASK_UTF_16_AUTO (1 << coding_category_utf_16_auto) |
df7492f9 KH |
555 | #define CATEGORY_MASK_UTF_16_BE (1 << coding_category_utf_16_be) |
556 | #define CATEGORY_MASK_UTF_16_LE (1 << coding_category_utf_16_le) | |
557 | #define CATEGORY_MASK_UTF_16_BE_NOSIG (1 << coding_category_utf_16_be_nosig) | |
558 | #define CATEGORY_MASK_UTF_16_LE_NOSIG (1 << coding_category_utf_16_le_nosig) | |
559 | #define CATEGORY_MASK_CHARSET (1 << coding_category_charset) | |
560 | #define CATEGORY_MASK_SJIS (1 << coding_category_sjis) | |
561 | #define CATEGORY_MASK_BIG5 (1 << coding_category_big5) | |
562 | #define CATEGORY_MASK_CCL (1 << coding_category_ccl) | |
563 | #define CATEGORY_MASK_EMACS_MULE (1 << coding_category_emacs_mule) | |
ff0dacd7 | 564 | #define CATEGORY_MASK_RAW_TEXT (1 << coding_category_raw_text) |
df7492f9 KH |
565 | |
566 | /* This value is returned if detect_coding_mask () find nothing other | |
567 | than ASCII characters. */ | |
568 | #define CATEGORY_MASK_ANY \ | |
569 | (CATEGORY_MASK_ISO_7 \ | |
570 | | CATEGORY_MASK_ISO_7_TIGHT \ | |
571 | | CATEGORY_MASK_ISO_8_1 \ | |
572 | | CATEGORY_MASK_ISO_8_2 \ | |
573 | | CATEGORY_MASK_ISO_7_ELSE \ | |
574 | | CATEGORY_MASK_ISO_8_ELSE \ | |
a470d443 KH |
575 | | CATEGORY_MASK_UTF_8_AUTO \ |
576 | | CATEGORY_MASK_UTF_8_NOSIG \ | |
577 | | CATEGORY_MASK_UTF_8_SIG \ | |
2f3cbb32 | 578 | | CATEGORY_MASK_UTF_16_AUTO \ |
df7492f9 KH |
579 | | CATEGORY_MASK_UTF_16_BE \ |
580 | | CATEGORY_MASK_UTF_16_LE \ | |
581 | | CATEGORY_MASK_UTF_16_BE_NOSIG \ | |
582 | | CATEGORY_MASK_UTF_16_LE_NOSIG \ | |
583 | | CATEGORY_MASK_CHARSET \ | |
584 | | CATEGORY_MASK_SJIS \ | |
585 | | CATEGORY_MASK_BIG5 \ | |
586 | | CATEGORY_MASK_CCL \ | |
587 | | CATEGORY_MASK_EMACS_MULE) | |
588 | ||
589 | ||
590 | #define CATEGORY_MASK_ISO_7BIT \ | |
591 | (CATEGORY_MASK_ISO_7 | CATEGORY_MASK_ISO_7_TIGHT) | |
592 | ||
593 | #define CATEGORY_MASK_ISO_8BIT \ | |
594 | (CATEGORY_MASK_ISO_8_1 | CATEGORY_MASK_ISO_8_2) | |
595 | ||
596 | #define CATEGORY_MASK_ISO_ELSE \ | |
597 | (CATEGORY_MASK_ISO_7_ELSE | CATEGORY_MASK_ISO_8_ELSE) | |
598 | ||
599 | #define CATEGORY_MASK_ISO_ESCAPE \ | |
600 | (CATEGORY_MASK_ISO_7 \ | |
601 | | CATEGORY_MASK_ISO_7_TIGHT \ | |
602 | | CATEGORY_MASK_ISO_7_ELSE \ | |
603 | | CATEGORY_MASK_ISO_8_ELSE) | |
604 | ||
605 | #define CATEGORY_MASK_ISO \ | |
606 | ( CATEGORY_MASK_ISO_7BIT \ | |
607 | | CATEGORY_MASK_ISO_8BIT \ | |
608 | | CATEGORY_MASK_ISO_ELSE) | |
609 | ||
610 | #define CATEGORY_MASK_UTF_16 \ | |
2f3cbb32 KH |
611 | (CATEGORY_MASK_UTF_16_AUTO \ |
612 | | CATEGORY_MASK_UTF_16_BE \ | |
df7492f9 KH |
613 | | CATEGORY_MASK_UTF_16_LE \ |
614 | | CATEGORY_MASK_UTF_16_BE_NOSIG \ | |
615 | | CATEGORY_MASK_UTF_16_LE_NOSIG) | |
616 | ||
a470d443 KH |
617 | #define CATEGORY_MASK_UTF_8 \ |
618 | (CATEGORY_MASK_UTF_8_AUTO \ | |
619 | | CATEGORY_MASK_UTF_8_NOSIG \ | |
620 | | CATEGORY_MASK_UTF_8_SIG) | |
df7492f9 | 621 | |
df7492f9 | 622 | /* Table of coding categories (Lisp symbols). This variable is for |
ad1746f5 | 623 | internal use only. */ |
df7492f9 KH |
624 | static Lisp_Object Vcoding_category_table; |
625 | ||
626 | /* Table of coding-categories ordered by priority. */ | |
627 | static enum coding_category coding_priorities[coding_category_max]; | |
628 | ||
629 | /* Nth element is a coding context for the coding system bound to the | |
630 | Nth coding category. */ | |
631 | static struct coding_system coding_categories[coding_category_max]; | |
632 | ||
df7492f9 KH |
633 | /*** Commonly used macros and functions ***/ |
634 | ||
635 | #ifndef min | |
636 | #define min(a, b) ((a) < (b) ? (a) : (b)) | |
637 | #endif | |
638 | #ifndef max | |
639 | #define max(a, b) ((a) > (b) ? (a) : (b)) | |
640 | #endif | |
4ed46869 | 641 | |
24a73b0a KH |
642 | #define CODING_GET_INFO(coding, attrs, charset_list) \ |
643 | do { \ | |
644 | (attrs) = CODING_ID_ATTRS ((coding)->id); \ | |
645 | (charset_list) = CODING_ATTR_CHARSET_LIST (attrs); \ | |
df7492f9 | 646 | } while (0) |
4ed46869 | 647 | |
4ed46869 | 648 | |
df7492f9 KH |
649 | /* Safely get one byte from the source text pointed by SRC which ends |
650 | at SRC_END, and set C to that byte. If there are not enough bytes | |
065e3595 KH |
651 | in the source, it jumps to `no_more_source'. If multibytep is |
652 | nonzero, and a multibyte character is found at SRC, set C to the | |
653 | negative value of the character code. The caller should declare | |
654 | and set these variables appropriately in advance: | |
655 | src, src_end, multibytep */ | |
aa72b389 | 656 | |
065e3595 KH |
657 | #define ONE_MORE_BYTE(c) \ |
658 | do { \ | |
659 | if (src == src_end) \ | |
660 | { \ | |
661 | if (src_base < src) \ | |
662 | record_conversion_result \ | |
663 | (coding, CODING_RESULT_INSUFFICIENT_SRC); \ | |
664 | goto no_more_source; \ | |
665 | } \ | |
666 | c = *src++; \ | |
667 | if (multibytep && (c & 0x80)) \ | |
668 | { \ | |
669 | if ((c & 0xFE) == 0xC0) \ | |
670 | c = ((c & 1) << 6) | *src++; \ | |
671 | else \ | |
672 | { \ | |
35befdaa KH |
673 | src--; \ |
674 | c = - string_char (src, &src, NULL); \ | |
065e3595 KH |
675 | record_conversion_result \ |
676 | (coding, CODING_RESULT_INVALID_SRC); \ | |
677 | } \ | |
678 | } \ | |
679 | consumed_chars++; \ | |
aa72b389 KH |
680 | } while (0) |
681 | ||
f56a4450 | 682 | /* Safely get two bytes from the source text pointed by SRC which ends |
220eeac9 KH |
683 | at SRC_END, and set C1 and C2 to those bytes while skipping the |
684 | heading multibyte characters. If there are not enough bytes in the | |
685 | source, it jumps to `no_more_source'. If multibytep is nonzero and | |
686 | a multibyte character is found for C2, set C2 to the negative value | |
687 | of the character code. The caller should declare and set these | |
688 | variables appropriately in advance: | |
f56a4450 KH |
689 | src, src_end, multibytep |
690 | It is intended that this macro is used in detect_coding_utf_16. */ | |
691 | ||
220eeac9 KH |
692 | #define TWO_MORE_BYTES(c1, c2) \ |
693 | do { \ | |
694 | do { \ | |
695 | if (src == src_end) \ | |
696 | goto no_more_source; \ | |
697 | c1 = *src++; \ | |
698 | if (multibytep && (c1 & 0x80)) \ | |
699 | { \ | |
700 | if ((c1 & 0xFE) == 0xC0) \ | |
701 | c1 = ((c1 & 1) << 6) | *src++; \ | |
702 | else \ | |
703 | { \ | |
704 | src += BYTES_BY_CHAR_HEAD (c1) - 1; \ | |
705 | c1 = -1; \ | |
706 | } \ | |
707 | } \ | |
708 | } while (c1 < 0); \ | |
709 | if (src == src_end) \ | |
710 | goto no_more_source; \ | |
711 | c2 = *src++; \ | |
712 | if (multibytep && (c2 & 0x80)) \ | |
713 | { \ | |
714 | if ((c2 & 0xFE) == 0xC0) \ | |
715 | c2 = ((c2 & 1) << 6) | *src++; \ | |
716 | else \ | |
717 | c2 = -1; \ | |
718 | } \ | |
f56a4450 KH |
719 | } while (0) |
720 | ||
aa72b389 | 721 | |
df7492f9 KH |
722 | /* Store a byte C in the place pointed by DST and increment DST to the |
723 | next free point, and increment PRODUCED_CHARS. The caller should | |
724 | assure that C is 0..127, and declare and set the variable `dst' | |
725 | appropriately in advance. | |
726 | */ | |
aa72b389 KH |
727 | |
728 | ||
df7492f9 KH |
729 | #define EMIT_ONE_ASCII_BYTE(c) \ |
730 | do { \ | |
731 | produced_chars++; \ | |
732 | *dst++ = (c); \ | |
b6871cc7 | 733 | } while (0) |
aa72b389 KH |
734 | |
735 | ||
ad1746f5 | 736 | /* Like EMIT_ONE_ASCII_BYTE but store two bytes; C1 and C2. */ |
aa72b389 | 737 | |
df7492f9 KH |
738 | #define EMIT_TWO_ASCII_BYTES(c1, c2) \ |
739 | do { \ | |
740 | produced_chars += 2; \ | |
741 | *dst++ = (c1), *dst++ = (c2); \ | |
742 | } while (0) | |
aa72b389 KH |
743 | |
744 | ||
df7492f9 KH |
745 | /* Store a byte C in the place pointed by DST and increment DST to the |
746 | next free point, and increment PRODUCED_CHARS. If MULTIBYTEP is | |
747 | nonzero, store in an appropriate multibyte from. The caller should | |
748 | declare and set the variables `dst' and `multibytep' appropriately | |
749 | in advance. */ | |
750 | ||
751 | #define EMIT_ONE_BYTE(c) \ | |
752 | do { \ | |
753 | produced_chars++; \ | |
754 | if (multibytep) \ | |
755 | { \ | |
756 | int ch = (c); \ | |
757 | if (ch >= 0x80) \ | |
758 | ch = BYTE8_TO_CHAR (ch); \ | |
759 | CHAR_STRING_ADVANCE (ch, dst); \ | |
760 | } \ | |
761 | else \ | |
762 | *dst++ = (c); \ | |
aa72b389 | 763 | } while (0) |
aa72b389 | 764 | |
aa72b389 | 765 | |
df7492f9 | 766 | /* Like EMIT_ONE_BYTE, but emit two bytes; C1 and C2. */ |
aa72b389 | 767 | |
e19c3639 KH |
768 | #define EMIT_TWO_BYTES(c1, c2) \ |
769 | do { \ | |
770 | produced_chars += 2; \ | |
771 | if (multibytep) \ | |
772 | { \ | |
773 | int ch; \ | |
774 | \ | |
775 | ch = (c1); \ | |
776 | if (ch >= 0x80) \ | |
777 | ch = BYTE8_TO_CHAR (ch); \ | |
778 | CHAR_STRING_ADVANCE (ch, dst); \ | |
779 | ch = (c2); \ | |
780 | if (ch >= 0x80) \ | |
781 | ch = BYTE8_TO_CHAR (ch); \ | |
782 | CHAR_STRING_ADVANCE (ch, dst); \ | |
783 | } \ | |
784 | else \ | |
785 | { \ | |
786 | *dst++ = (c1); \ | |
787 | *dst++ = (c2); \ | |
788 | } \ | |
aa72b389 KH |
789 | } while (0) |
790 | ||
791 | ||
df7492f9 KH |
792 | #define EMIT_THREE_BYTES(c1, c2, c3) \ |
793 | do { \ | |
794 | EMIT_ONE_BYTE (c1); \ | |
795 | EMIT_TWO_BYTES (c2, c3); \ | |
796 | } while (0) | |
aa72b389 | 797 | |
aa72b389 | 798 | |
df7492f9 KH |
799 | #define EMIT_FOUR_BYTES(c1, c2, c3, c4) \ |
800 | do { \ | |
801 | EMIT_TWO_BYTES (c1, c2); \ | |
802 | EMIT_TWO_BYTES (c3, c4); \ | |
803 | } while (0) | |
aa72b389 | 804 | |
aa72b389 | 805 | |
f6cbaf43 | 806 | /* Prototypes for static functions. */ |
f57e2426 J |
807 | static void record_conversion_result (struct coding_system *coding, |
808 | enum coding_result_code result); | |
809 | static int detect_coding_utf_8 (struct coding_system *, | |
810 | struct coding_detection_info *info); | |
811 | static void decode_coding_utf_8 (struct coding_system *); | |
812 | static int encode_coding_utf_8 (struct coding_system *); | |
813 | ||
814 | static int detect_coding_utf_16 (struct coding_system *, | |
815 | struct coding_detection_info *info); | |
816 | static void decode_coding_utf_16 (struct coding_system *); | |
817 | static int encode_coding_utf_16 (struct coding_system *); | |
818 | ||
819 | static int detect_coding_iso_2022 (struct coding_system *, | |
820 | struct coding_detection_info *info); | |
821 | static void decode_coding_iso_2022 (struct coding_system *); | |
822 | static int encode_coding_iso_2022 (struct coding_system *); | |
823 | ||
824 | static int detect_coding_emacs_mule (struct coding_system *, | |
825 | struct coding_detection_info *info); | |
826 | static void decode_coding_emacs_mule (struct coding_system *); | |
827 | static int encode_coding_emacs_mule (struct coding_system *); | |
828 | ||
829 | static int detect_coding_sjis (struct coding_system *, | |
830 | struct coding_detection_info *info); | |
831 | static void decode_coding_sjis (struct coding_system *); | |
832 | static int encode_coding_sjis (struct coding_system *); | |
833 | ||
834 | static int detect_coding_big5 (struct coding_system *, | |
835 | struct coding_detection_info *info); | |
836 | static void decode_coding_big5 (struct coding_system *); | |
837 | static int encode_coding_big5 (struct coding_system *); | |
838 | ||
839 | static int detect_coding_ccl (struct coding_system *, | |
840 | struct coding_detection_info *info); | |
841 | static void decode_coding_ccl (struct coding_system *); | |
842 | static int encode_coding_ccl (struct coding_system *); | |
843 | ||
844 | static void decode_coding_raw_text (struct coding_system *); | |
845 | static int encode_coding_raw_text (struct coding_system *); | |
846 | ||
847 | static void coding_set_source (struct coding_system *); | |
848 | static void coding_set_destination (struct coding_system *); | |
849 | static void coding_alloc_by_realloc (struct coding_system *, EMACS_INT); | |
850 | static void coding_alloc_by_making_gap (struct coding_system *, | |
851 | EMACS_INT, EMACS_INT); | |
852 | static unsigned char *alloc_destination (struct coding_system *, | |
853 | EMACS_INT, unsigned char *); | |
854 | static void setup_iso_safe_charsets (Lisp_Object); | |
855 | static unsigned char *encode_designation_at_bol (struct coding_system *, | |
461c2ab9 | 856 | int *, unsigned char *); |
f57e2426 J |
857 | static int detect_eol (const unsigned char *, |
858 | EMACS_INT, enum coding_category); | |
859 | static Lisp_Object adjust_coding_eol_type (struct coding_system *, int); | |
860 | static void decode_eol (struct coding_system *); | |
861 | static Lisp_Object get_translation_table (Lisp_Object, int, int *); | |
862 | static Lisp_Object get_translation (Lisp_Object, int *, int *); | |
863 | static int produce_chars (struct coding_system *, Lisp_Object, int); | |
864 | static INLINE void produce_charset (struct coding_system *, int *, | |
865 | EMACS_INT); | |
866 | static void produce_annotation (struct coding_system *, EMACS_INT); | |
867 | static int decode_coding (struct coding_system *); | |
868 | static INLINE int *handle_composition_annotation (EMACS_INT, EMACS_INT, | |
869 | struct coding_system *, | |
870 | int *, EMACS_INT *); | |
871 | static INLINE int *handle_charset_annotation (EMACS_INT, EMACS_INT, | |
872 | struct coding_system *, | |
873 | int *, EMACS_INT *); | |
874 | static void consume_chars (struct coding_system *, Lisp_Object, int); | |
875 | static int encode_coding (struct coding_system *); | |
876 | static Lisp_Object make_conversion_work_buffer (int); | |
877 | static Lisp_Object code_conversion_restore (Lisp_Object); | |
878 | static INLINE int char_encodable_p (int, Lisp_Object); | |
879 | static Lisp_Object make_subsidiaries (Lisp_Object); | |
f6cbaf43 | 880 | |
065e3595 KH |
881 | static void |
882 | record_conversion_result (struct coding_system *coding, | |
883 | enum coding_result_code result) | |
884 | { | |
885 | coding->result = result; | |
886 | switch (result) | |
887 | { | |
888 | case CODING_RESULT_INSUFFICIENT_SRC: | |
889 | Vlast_code_conversion_error = Qinsufficient_source; | |
890 | break; | |
891 | case CODING_RESULT_INCONSISTENT_EOL: | |
892 | Vlast_code_conversion_error = Qinconsistent_eol; | |
893 | break; | |
894 | case CODING_RESULT_INVALID_SRC: | |
895 | Vlast_code_conversion_error = Qinvalid_source; | |
896 | break; | |
897 | case CODING_RESULT_INTERRUPT: | |
898 | Vlast_code_conversion_error = Qinterrupted; | |
899 | break; | |
900 | case CODING_RESULT_INSUFFICIENT_MEM: | |
901 | Vlast_code_conversion_error = Qinsufficient_memory; | |
902 | break; | |
ebaf11b6 KH |
903 | case CODING_RESULT_INSUFFICIENT_DST: |
904 | /* Don't record this error in Vlast_code_conversion_error | |
905 | because it happens just temporarily and is resolved when the | |
906 | whole conversion is finished. */ | |
907 | break; | |
409ea3a1 AS |
908 | case CODING_RESULT_SUCCESS: |
909 | break; | |
35befdaa KH |
910 | default: |
911 | Vlast_code_conversion_error = intern ("Unknown error"); | |
065e3595 KH |
912 | } |
913 | } | |
914 | ||
75f80e63 EZ |
915 | /* This wrapper macro is used to preserve validity of pointers into |
916 | buffer text across calls to decode_char, which could cause | |
917 | relocation of buffers if it loads a charset map, because loading a | |
918 | charset map allocates large structures. */ | |
df7492f9 KH |
919 | #define CODING_DECODE_CHAR(coding, src, src_base, src_end, charset, code, c) \ |
920 | do { \ | |
921 | charset_map_loaded = 0; \ | |
922 | c = DECODE_CHAR (charset, code); \ | |
923 | if (charset_map_loaded) \ | |
924 | { \ | |
8f924df7 | 925 | const unsigned char *orig = coding->source; \ |
df7492f9 KH |
926 | EMACS_INT offset; \ |
927 | \ | |
928 | coding_set_source (coding); \ | |
929 | offset = coding->source - orig; \ | |
930 | src += offset; \ | |
931 | src_base += offset; \ | |
932 | src_end += offset; \ | |
933 | } \ | |
aa72b389 KH |
934 | } while (0) |
935 | ||
936 | ||
119852e7 KH |
937 | /* If there are at least BYTES length of room at dst, allocate memory |
938 | for coding->destination and update dst and dst_end. We don't have | |
939 | to take care of coding->source which will be relocated. It is | |
940 | handled by calling coding_set_source in encode_coding. */ | |
941 | ||
df7492f9 KH |
942 | #define ASSURE_DESTINATION(bytes) \ |
943 | do { \ | |
944 | if (dst + (bytes) >= dst_end) \ | |
945 | { \ | |
946 | int more_bytes = charbuf_end - charbuf + (bytes); \ | |
947 | \ | |
948 | dst = alloc_destination (coding, more_bytes, dst); \ | |
949 | dst_end = coding->destination + coding->dst_bytes; \ | |
950 | } \ | |
951 | } while (0) | |
aa72b389 | 952 | |
aa72b389 | 953 | |
db274c7a KH |
954 | /* Store multibyte form of the character C in P, and advance P to the |
955 | end of the multibyte form. This is like CHAR_STRING_ADVANCE but it | |
956 | never calls MAYBE_UNIFY_CHAR. */ | |
957 | ||
958 | #define CHAR_STRING_ADVANCE_NO_UNIFY(c, p) \ | |
959 | do { \ | |
960 | if ((c) <= MAX_1_BYTE_CHAR) \ | |
961 | *(p)++ = (c); \ | |
962 | else if ((c) <= MAX_2_BYTE_CHAR) \ | |
963 | *(p)++ = (0xC0 | ((c) >> 6)), \ | |
964 | *(p)++ = (0x80 | ((c) & 0x3F)); \ | |
965 | else if ((c) <= MAX_3_BYTE_CHAR) \ | |
966 | *(p)++ = (0xE0 | ((c) >> 12)), \ | |
967 | *(p)++ = (0x80 | (((c) >> 6) & 0x3F)), \ | |
968 | *(p)++ = (0x80 | ((c) & 0x3F)); \ | |
969 | else if ((c) <= MAX_4_BYTE_CHAR) \ | |
970 | *(p)++ = (0xF0 | (c >> 18)), \ | |
971 | *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \ | |
972 | *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \ | |
973 | *(p)++ = (0x80 | (c & 0x3F)); \ | |
974 | else if ((c) <= MAX_5_BYTE_CHAR) \ | |
975 | *(p)++ = 0xF8, \ | |
976 | *(p)++ = (0x80 | ((c >> 18) & 0x0F)), \ | |
977 | *(p)++ = (0x80 | ((c >> 12) & 0x3F)), \ | |
978 | *(p)++ = (0x80 | ((c >> 6) & 0x3F)), \ | |
979 | *(p)++ = (0x80 | (c & 0x3F)); \ | |
980 | else \ | |
981 | (p) += BYTE8_STRING ((c) - 0x3FFF80, p); \ | |
982 | } while (0) | |
983 | ||
984 | ||
985 | /* Return the character code of character whose multibyte form is at | |
986 | P, and advance P to the end of the multibyte form. This is like | |
987 | STRING_CHAR_ADVANCE, but it never calls MAYBE_UNIFY_CHAR. */ | |
988 | ||
989 | #define STRING_CHAR_ADVANCE_NO_UNIFY(p) \ | |
990 | (!((p)[0] & 0x80) \ | |
991 | ? *(p)++ \ | |
992 | : ! ((p)[0] & 0x20) \ | |
993 | ? ((p) += 2, \ | |
994 | ((((p)[-2] & 0x1F) << 6) \ | |
995 | | ((p)[-1] & 0x3F) \ | |
996 | | ((unsigned char) ((p)[-2]) < 0xC2 ? 0x3FFF80 : 0))) \ | |
997 | : ! ((p)[0] & 0x10) \ | |
998 | ? ((p) += 3, \ | |
999 | ((((p)[-3] & 0x0F) << 12) \ | |
1000 | | (((p)[-2] & 0x3F) << 6) \ | |
1001 | | ((p)[-1] & 0x3F))) \ | |
1002 | : ! ((p)[0] & 0x08) \ | |
1003 | ? ((p) += 4, \ | |
1004 | ((((p)[-4] & 0xF) << 18) \ | |
1005 | | (((p)[-3] & 0x3F) << 12) \ | |
1006 | | (((p)[-2] & 0x3F) << 6) \ | |
1007 | | ((p)[-1] & 0x3F))) \ | |
1008 | : ((p) += 5, \ | |
1009 | ((((p)[-4] & 0x3F) << 18) \ | |
1010 | | (((p)[-3] & 0x3F) << 12) \ | |
1011 | | (((p)[-2] & 0x3F) << 6) \ | |
1012 | | ((p)[-1] & 0x3F)))) | |
1013 | ||
aa72b389 | 1014 | |
df7492f9 | 1015 | static void |
971de7fb | 1016 | coding_set_source (struct coding_system *coding) |
aa72b389 | 1017 | { |
df7492f9 KH |
1018 | if (BUFFERP (coding->src_object)) |
1019 | { | |
2cb26057 | 1020 | struct buffer *buf = XBUFFER (coding->src_object); |
aa72b389 | 1021 | |
df7492f9 | 1022 | if (coding->src_pos < 0) |
2cb26057 | 1023 | coding->source = BUF_GAP_END_ADDR (buf) + coding->src_pos_byte; |
df7492f9 | 1024 | else |
2cb26057 | 1025 | coding->source = BUF_BYTE_ADDRESS (buf, coding->src_pos_byte); |
aa72b389 | 1026 | } |
df7492f9 | 1027 | else if (STRINGP (coding->src_object)) |
aa72b389 | 1028 | { |
8f924df7 | 1029 | coding->source = SDATA (coding->src_object) + coding->src_pos_byte; |
aa72b389 | 1030 | } |
df7492f9 | 1031 | else |
f38b440c PE |
1032 | { |
1033 | /* Otherwise, the source is C string and is never relocated | |
1034 | automatically. Thus we don't have to update anything. */ | |
1035 | } | |
df7492f9 | 1036 | } |
aa72b389 | 1037 | |
df7492f9 | 1038 | static void |
971de7fb | 1039 | coding_set_destination (struct coding_system *coding) |
df7492f9 KH |
1040 | { |
1041 | if (BUFFERP (coding->dst_object)) | |
aa72b389 | 1042 | { |
df7492f9 | 1043 | if (coding->src_pos < 0) |
aa72b389 | 1044 | { |
13818c30 | 1045 | coding->destination = BEG_ADDR + coding->dst_pos_byte - BEG_BYTE; |
28f67a95 KH |
1046 | coding->dst_bytes = (GAP_END_ADDR |
1047 | - (coding->src_bytes - coding->consumed) | |
1048 | - coding->destination); | |
aa72b389 | 1049 | } |
df7492f9 | 1050 | else |
28f67a95 KH |
1051 | { |
1052 | /* We are sure that coding->dst_pos_byte is before the gap | |
1053 | of the buffer. */ | |
1054 | coding->destination = (BUF_BEG_ADDR (XBUFFER (coding->dst_object)) | |
13818c30 | 1055 | + coding->dst_pos_byte - BEG_BYTE); |
28f67a95 KH |
1056 | coding->dst_bytes = (BUF_GAP_END_ADDR (XBUFFER (coding->dst_object)) |
1057 | - coding->destination); | |
1058 | } | |
df7492f9 KH |
1059 | } |
1060 | else | |
f38b440c PE |
1061 | { |
1062 | /* Otherwise, the destination is C string and is never relocated | |
1063 | automatically. Thus we don't have to update anything. */ | |
1064 | } | |
df7492f9 KH |
1065 | } |
1066 | ||
1067 | ||
1068 | static void | |
971de7fb | 1069 | coding_alloc_by_realloc (struct coding_system *coding, EMACS_INT bytes) |
df7492f9 KH |
1070 | { |
1071 | coding->destination = (unsigned char *) xrealloc (coding->destination, | |
1072 | coding->dst_bytes + bytes); | |
1073 | coding->dst_bytes += bytes; | |
1074 | } | |
1075 | ||
1076 | static void | |
cf84bb53 JB |
1077 | coding_alloc_by_making_gap (struct coding_system *coding, |
1078 | EMACS_INT gap_head_used, EMACS_INT bytes) | |
df7492f9 | 1079 | { |
db274c7a | 1080 | if (EQ (coding->src_object, coding->dst_object)) |
df7492f9 | 1081 | { |
db274c7a KH |
1082 | /* The gap may contain the produced data at the head and not-yet |
1083 | consumed data at the tail. To preserve those data, we at | |
1084 | first make the gap size to zero, then increase the gap | |
1085 | size. */ | |
1086 | EMACS_INT add = GAP_SIZE; | |
1087 | ||
1088 | GPT += gap_head_used, GPT_BYTE += gap_head_used; | |
1089 | GAP_SIZE = 0; ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add; | |
df7492f9 KH |
1090 | make_gap (bytes); |
1091 | GAP_SIZE += add; ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add; | |
db274c7a | 1092 | GPT -= gap_head_used, GPT_BYTE -= gap_head_used; |
df7492f9 | 1093 | } |
730fff51 | 1094 | else |
df7492f9 | 1095 | { |
2c78b7e1 KH |
1096 | Lisp_Object this_buffer; |
1097 | ||
1098 | this_buffer = Fcurrent_buffer (); | |
df7492f9 KH |
1099 | set_buffer_internal (XBUFFER (coding->dst_object)); |
1100 | make_gap (bytes); | |
1101 | set_buffer_internal (XBUFFER (this_buffer)); | |
aa72b389 | 1102 | } |
df7492f9 | 1103 | } |
8f924df7 | 1104 | |
df7492f9 KH |
1105 | |
1106 | static unsigned char * | |
cf84bb53 JB |
1107 | alloc_destination (struct coding_system *coding, EMACS_INT nbytes, |
1108 | unsigned char *dst) | |
df7492f9 KH |
1109 | { |
1110 | EMACS_INT offset = dst - coding->destination; | |
1111 | ||
1112 | if (BUFFERP (coding->dst_object)) | |
db274c7a KH |
1113 | { |
1114 | struct buffer *buf = XBUFFER (coding->dst_object); | |
1115 | ||
1116 | coding_alloc_by_making_gap (coding, dst - BUF_GPT_ADDR (buf), nbytes); | |
1117 | } | |
aa72b389 | 1118 | else |
df7492f9 | 1119 | coding_alloc_by_realloc (coding, nbytes); |
df7492f9 KH |
1120 | coding_set_destination (coding); |
1121 | dst = coding->destination + offset; | |
1122 | return dst; | |
1123 | } | |
aa72b389 | 1124 | |
ff0dacd7 KH |
1125 | /** Macros for annotations. */ |
1126 | ||
ff0dacd7 KH |
1127 | /* An annotation data is stored in the array coding->charbuf in this |
1128 | format: | |
69a80ea3 | 1129 | [ -LENGTH ANNOTATION_MASK NCHARS ... ] |
ff0dacd7 KH |
1130 | LENGTH is the number of elements in the annotation. |
1131 | ANNOTATION_MASK is one of CODING_ANNOTATE_XXX_MASK. | |
69a80ea3 | 1132 | NCHARS is the number of characters in the text annotated. |
ff0dacd7 KH |
1133 | |
1134 | The format of the following elements depend on ANNOTATION_MASK. | |
1135 | ||
1136 | In the case of CODING_ANNOTATE_COMPOSITION_MASK, these elements | |
1137 | follows: | |
e951386e KH |
1138 | ... NBYTES METHOD [ COMPOSITION-COMPONENTS ... ] |
1139 | ||
1140 | NBYTES is the number of bytes specified in the header part of | |
1141 | old-style emacs-mule encoding, or 0 for the other kind of | |
1142 | composition. | |
1143 | ||
ff0dacd7 | 1144 | METHOD is one of enum composition_method. |
e951386e | 1145 | |
ad1746f5 | 1146 | Optional COMPOSITION-COMPONENTS are characters and composition |
ff0dacd7 KH |
1147 | rules. |
1148 | ||
1149 | In the case of CODING_ANNOTATE_CHARSET_MASK, one element CHARSET-ID | |
e951386e KH |
1150 | follows. |
1151 | ||
1152 | If ANNOTATION_MASK is 0, this annotation is just a space holder to | |
1153 | recover from an invalid annotation, and should be skipped by | |
1154 | produce_annotation. */ | |
1155 | ||
1156 | /* Maximum length of the header of annotation data. */ | |
1157 | #define MAX_ANNOTATION_LENGTH 5 | |
ff0dacd7 | 1158 | |
69a80ea3 | 1159 | #define ADD_ANNOTATION_DATA(buf, len, mask, nchars) \ |
ff0dacd7 KH |
1160 | do { \ |
1161 | *(buf)++ = -(len); \ | |
1162 | *(buf)++ = (mask); \ | |
69a80ea3 | 1163 | *(buf)++ = (nchars); \ |
ff0dacd7 KH |
1164 | coding->annotated = 1; \ |
1165 | } while (0); | |
1166 | ||
e951386e | 1167 | #define ADD_COMPOSITION_DATA(buf, nchars, nbytes, method) \ |
69a80ea3 | 1168 | do { \ |
e951386e KH |
1169 | ADD_ANNOTATION_DATA (buf, 5, CODING_ANNOTATE_COMPOSITION_MASK, nchars); \ |
1170 | *buf++ = nbytes; \ | |
69a80ea3 | 1171 | *buf++ = method; \ |
ff0dacd7 KH |
1172 | } while (0) |
1173 | ||
1174 | ||
69a80ea3 KH |
1175 | #define ADD_CHARSET_DATA(buf, nchars, id) \ |
1176 | do { \ | |
1177 | ADD_ANNOTATION_DATA (buf, 4, CODING_ANNOTATE_CHARSET_MASK, nchars); \ | |
1178 | *buf++ = id; \ | |
ff0dacd7 KH |
1179 | } while (0) |
1180 | ||
df7492f9 KH |
1181 | \f |
1182 | /*** 2. Emacs' internal format (emacs-utf-8) ***/ | |
1183 | ||
1184 | ||
1185 | ||
1186 | \f | |
1187 | /*** 3. UTF-8 ***/ | |
1188 | ||
1189 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
ff0dacd7 KH |
1190 | Check if a text is encoded in UTF-8. If it is, return 1, else |
1191 | return 0. */ | |
df7492f9 KH |
1192 | |
1193 | #define UTF_8_1_OCTET_P(c) ((c) < 0x80) | |
1194 | #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80) | |
1195 | #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0) | |
1196 | #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0) | |
1197 | #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0) | |
1198 | #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8) | |
1199 | ||
a470d443 KH |
1200 | #define UTF_8_BOM_1 0xEF |
1201 | #define UTF_8_BOM_2 0xBB | |
1202 | #define UTF_8_BOM_3 0xBF | |
1203 | ||
df7492f9 | 1204 | static int |
cf84bb53 JB |
1205 | detect_coding_utf_8 (struct coding_system *coding, |
1206 | struct coding_detection_info *detect_info) | |
df7492f9 | 1207 | { |
065e3595 | 1208 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 1209 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
1210 | int multibytep = coding->src_multibyte; |
1211 | int consumed_chars = 0; | |
a470d443 | 1212 | int bom_found = 0; |
df7492f9 KH |
1213 | int found = 0; |
1214 | ||
ff0dacd7 | 1215 | detect_info->checked |= CATEGORY_MASK_UTF_8; |
df7492f9 KH |
1216 | /* A coding system of this category is always ASCII compatible. */ |
1217 | src += coding->head_ascii; | |
1218 | ||
1219 | while (1) | |
aa72b389 | 1220 | { |
df7492f9 | 1221 | int c, c1, c2, c3, c4; |
aa72b389 | 1222 | |
065e3595 | 1223 | src_base = src; |
df7492f9 | 1224 | ONE_MORE_BYTE (c); |
065e3595 | 1225 | if (c < 0 || UTF_8_1_OCTET_P (c)) |
df7492f9 KH |
1226 | continue; |
1227 | ONE_MORE_BYTE (c1); | |
065e3595 | 1228 | if (c1 < 0 || ! UTF_8_EXTRA_OCTET_P (c1)) |
df7492f9 KH |
1229 | break; |
1230 | if (UTF_8_2_OCTET_LEADING_P (c)) | |
aa72b389 | 1231 | { |
a470d443 | 1232 | found = 1; |
df7492f9 | 1233 | continue; |
aa72b389 | 1234 | } |
df7492f9 | 1235 | ONE_MORE_BYTE (c2); |
065e3595 | 1236 | if (c2 < 0 || ! UTF_8_EXTRA_OCTET_P (c2)) |
df7492f9 KH |
1237 | break; |
1238 | if (UTF_8_3_OCTET_LEADING_P (c)) | |
aa72b389 | 1239 | { |
a470d443 KH |
1240 | found = 1; |
1241 | if (src_base == coding->source | |
1242 | && c == UTF_8_BOM_1 && c1 == UTF_8_BOM_2 && c2 == UTF_8_BOM_3) | |
1243 | bom_found = 1; | |
df7492f9 | 1244 | continue; |
aa72b389 | 1245 | } |
df7492f9 | 1246 | ONE_MORE_BYTE (c3); |
065e3595 | 1247 | if (c3 < 0 || ! UTF_8_EXTRA_OCTET_P (c3)) |
df7492f9 KH |
1248 | break; |
1249 | if (UTF_8_4_OCTET_LEADING_P (c)) | |
aa72b389 | 1250 | { |
a470d443 | 1251 | found = 1; |
df7492f9 KH |
1252 | continue; |
1253 | } | |
1254 | ONE_MORE_BYTE (c4); | |
065e3595 | 1255 | if (c4 < 0 || ! UTF_8_EXTRA_OCTET_P (c4)) |
df7492f9 KH |
1256 | break; |
1257 | if (UTF_8_5_OCTET_LEADING_P (c)) | |
1258 | { | |
a470d443 | 1259 | found = 1; |
df7492f9 KH |
1260 | continue; |
1261 | } | |
1262 | break; | |
aa72b389 | 1263 | } |
ff0dacd7 | 1264 | detect_info->rejected |= CATEGORY_MASK_UTF_8; |
df7492f9 | 1265 | return 0; |
aa72b389 | 1266 | |
df7492f9 | 1267 | no_more_source: |
065e3595 | 1268 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) |
aa72b389 | 1269 | { |
ff0dacd7 | 1270 | detect_info->rejected |= CATEGORY_MASK_UTF_8; |
89528eb3 | 1271 | return 0; |
aa72b389 | 1272 | } |
a470d443 KH |
1273 | if (bom_found) |
1274 | { | |
1275 | /* The first character 0xFFFE doesn't necessarily mean a BOM. */ | |
1276 | detect_info->found |= CATEGORY_MASK_UTF_8_SIG | CATEGORY_MASK_UTF_8_NOSIG; | |
1277 | } | |
1278 | else | |
1279 | { | |
1280 | detect_info->rejected |= CATEGORY_MASK_UTF_8_SIG; | |
0e17387a KH |
1281 | if (found) |
1282 | detect_info->found |= CATEGORY_MASK_UTF_8_NOSIG; | |
a470d443 | 1283 | } |
ff0dacd7 | 1284 | return 1; |
aa72b389 KH |
1285 | } |
1286 | ||
4ed46869 | 1287 | |
b73bfc1c | 1288 | static void |
971de7fb | 1289 | decode_coding_utf_8 (struct coding_system *coding) |
b73bfc1c | 1290 | { |
8f924df7 KH |
1291 | const unsigned char *src = coding->source + coding->consumed; |
1292 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
1293 | const unsigned char *src_base; | |
69a80ea3 KH |
1294 | int *charbuf = coding->charbuf + coding->charbuf_used; |
1295 | int *charbuf_end = coding->charbuf + coding->charbuf_size; | |
453b38f0 | 1296 | int consumed_chars = 0, consumed_chars_base = 0; |
df7492f9 | 1297 | int multibytep = coding->src_multibyte; |
a470d443 | 1298 | enum utf_bom_type bom = CODING_UTF_8_BOM (coding); |
24a73b0a | 1299 | Lisp_Object attr, charset_list; |
2735d060 | 1300 | int eol_dos = |
0a9564cb | 1301 | !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); |
119852e7 | 1302 | int byte_after_cr = -1; |
4ed46869 | 1303 | |
24a73b0a | 1304 | CODING_GET_INFO (coding, attr, charset_list); |
df7492f9 | 1305 | |
a470d443 KH |
1306 | if (bom != utf_without_bom) |
1307 | { | |
1308 | int c1, c2, c3; | |
1309 | ||
1310 | src_base = src; | |
1311 | ONE_MORE_BYTE (c1); | |
1312 | if (! UTF_8_3_OCTET_LEADING_P (c1)) | |
1313 | src = src_base; | |
1314 | else | |
1315 | { | |
159bd5a2 | 1316 | ONE_MORE_BYTE (c2); |
a470d443 KH |
1317 | if (! UTF_8_EXTRA_OCTET_P (c2)) |
1318 | src = src_base; | |
1319 | else | |
1320 | { | |
159bd5a2 | 1321 | ONE_MORE_BYTE (c3); |
a470d443 KH |
1322 | if (! UTF_8_EXTRA_OCTET_P (c3)) |
1323 | src = src_base; | |
1324 | else | |
1325 | { | |
1326 | if ((c1 != UTF_8_BOM_1) | |
1327 | || (c2 != UTF_8_BOM_2) || (c3 != UTF_8_BOM_3)) | |
1328 | src = src_base; | |
1329 | else | |
1330 | CODING_UTF_8_BOM (coding) = utf_without_bom; | |
1331 | } | |
1332 | } | |
1333 | } | |
1334 | } | |
1335 | CODING_UTF_8_BOM (coding) = utf_without_bom; | |
1336 | ||
df7492f9 | 1337 | while (1) |
b73bfc1c | 1338 | { |
df7492f9 | 1339 | int c, c1, c2, c3, c4, c5; |
ec6d2bb8 | 1340 | |
df7492f9 KH |
1341 | src_base = src; |
1342 | consumed_chars_base = consumed_chars; | |
4af310db | 1343 | |
df7492f9 | 1344 | if (charbuf >= charbuf_end) |
b71f6f73 KH |
1345 | { |
1346 | if (byte_after_cr >= 0) | |
1347 | src_base--; | |
1348 | break; | |
1349 | } | |
df7492f9 | 1350 | |
119852e7 KH |
1351 | if (byte_after_cr >= 0) |
1352 | c1 = byte_after_cr, byte_after_cr = -1; | |
1353 | else | |
1354 | ONE_MORE_BYTE (c1); | |
065e3595 KH |
1355 | if (c1 < 0) |
1356 | { | |
1357 | c = - c1; | |
1358 | } | |
1a4990fb | 1359 | else if (UTF_8_1_OCTET_P (c1)) |
df7492f9 | 1360 | { |
2735d060 | 1361 | if (eol_dos && c1 == '\r') |
119852e7 | 1362 | ONE_MORE_BYTE (byte_after_cr); |
df7492f9 | 1363 | c = c1; |
4af310db | 1364 | } |
df7492f9 | 1365 | else |
4af310db | 1366 | { |
df7492f9 | 1367 | ONE_MORE_BYTE (c2); |
065e3595 | 1368 | if (c2 < 0 || ! UTF_8_EXTRA_OCTET_P (c2)) |
df7492f9 KH |
1369 | goto invalid_code; |
1370 | if (UTF_8_2_OCTET_LEADING_P (c1)) | |
4af310db | 1371 | { |
b0edb2c5 DL |
1372 | c = ((c1 & 0x1F) << 6) | (c2 & 0x3F); |
1373 | /* Reject overlong sequences here and below. Encoders | |
1374 | producing them are incorrect, they can be misleading, | |
1375 | and they mess up read/write invariance. */ | |
1376 | if (c < 128) | |
1377 | goto invalid_code; | |
4af310db | 1378 | } |
df7492f9 | 1379 | else |
aa72b389 | 1380 | { |
df7492f9 | 1381 | ONE_MORE_BYTE (c3); |
065e3595 | 1382 | if (c3 < 0 || ! UTF_8_EXTRA_OCTET_P (c3)) |
df7492f9 KH |
1383 | goto invalid_code; |
1384 | if (UTF_8_3_OCTET_LEADING_P (c1)) | |
b0edb2c5 DL |
1385 | { |
1386 | c = (((c1 & 0xF) << 12) | |
1387 | | ((c2 & 0x3F) << 6) | (c3 & 0x3F)); | |
72fe1301 DL |
1388 | if (c < 0x800 |
1389 | || (c >= 0xd800 && c < 0xe000)) /* surrogates (invalid) */ | |
b0edb2c5 DL |
1390 | goto invalid_code; |
1391 | } | |
df7492f9 KH |
1392 | else |
1393 | { | |
1394 | ONE_MORE_BYTE (c4); | |
065e3595 | 1395 | if (c4 < 0 || ! UTF_8_EXTRA_OCTET_P (c4)) |
df7492f9 KH |
1396 | goto invalid_code; |
1397 | if (UTF_8_4_OCTET_LEADING_P (c1)) | |
b0edb2c5 | 1398 | { |
df7492f9 KH |
1399 | c = (((c1 & 0x7) << 18) | ((c2 & 0x3F) << 12) |
1400 | | ((c3 & 0x3F) << 6) | (c4 & 0x3F)); | |
b0edb2c5 DL |
1401 | if (c < 0x10000) |
1402 | goto invalid_code; | |
1403 | } | |
df7492f9 KH |
1404 | else |
1405 | { | |
1406 | ONE_MORE_BYTE (c5); | |
065e3595 | 1407 | if (c5 < 0 || ! UTF_8_EXTRA_OCTET_P (c5)) |
df7492f9 KH |
1408 | goto invalid_code; |
1409 | if (UTF_8_5_OCTET_LEADING_P (c1)) | |
1410 | { | |
1411 | c = (((c1 & 0x3) << 24) | ((c2 & 0x3F) << 18) | |
1412 | | ((c3 & 0x3F) << 12) | ((c4 & 0x3F) << 6) | |
1413 | | (c5 & 0x3F)); | |
b0edb2c5 | 1414 | if ((c > MAX_CHAR) || (c < 0x200000)) |
df7492f9 KH |
1415 | goto invalid_code; |
1416 | } | |
1417 | else | |
1418 | goto invalid_code; | |
1419 | } | |
1420 | } | |
aa72b389 | 1421 | } |
b73bfc1c | 1422 | } |
df7492f9 KH |
1423 | |
1424 | *charbuf++ = c; | |
1425 | continue; | |
1426 | ||
1427 | invalid_code: | |
1428 | src = src_base; | |
1429 | consumed_chars = consumed_chars_base; | |
1430 | ONE_MORE_BYTE (c); | |
1431 | *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); | |
1432 | coding->errors++; | |
aa72b389 KH |
1433 | } |
1434 | ||
df7492f9 KH |
1435 | no_more_source: |
1436 | coding->consumed_char += consumed_chars_base; | |
1437 | coding->consumed = src_base - coding->source; | |
1438 | coding->charbuf_used = charbuf - coding->charbuf; | |
1439 | } | |
1440 | ||
1441 | ||
1442 | static int | |
971de7fb | 1443 | encode_coding_utf_8 (struct coding_system *coding) |
df7492f9 KH |
1444 | { |
1445 | int multibytep = coding->dst_multibyte; | |
1446 | int *charbuf = coding->charbuf; | |
1447 | int *charbuf_end = charbuf + coding->charbuf_used; | |
1448 | unsigned char *dst = coding->destination + coding->produced; | |
1449 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
e19c3639 | 1450 | int produced_chars = 0; |
df7492f9 KH |
1451 | int c; |
1452 | ||
a470d443 KH |
1453 | if (CODING_UTF_8_BOM (coding) == utf_with_bom) |
1454 | { | |
1455 | ASSURE_DESTINATION (3); | |
1456 | EMIT_THREE_BYTES (UTF_8_BOM_1, UTF_8_BOM_2, UTF_8_BOM_3); | |
1457 | CODING_UTF_8_BOM (coding) = utf_without_bom; | |
1458 | } | |
1459 | ||
df7492f9 | 1460 | if (multibytep) |
aa72b389 | 1461 | { |
df7492f9 KH |
1462 | int safe_room = MAX_MULTIBYTE_LENGTH * 2; |
1463 | ||
1464 | while (charbuf < charbuf_end) | |
b73bfc1c | 1465 | { |
df7492f9 | 1466 | unsigned char str[MAX_MULTIBYTE_LENGTH], *p, *pend = str; |
8f924df7 | 1467 | |
df7492f9 KH |
1468 | ASSURE_DESTINATION (safe_room); |
1469 | c = *charbuf++; | |
28f67a95 KH |
1470 | if (CHAR_BYTE8_P (c)) |
1471 | { | |
1472 | c = CHAR_TO_BYTE8 (c); | |
1473 | EMIT_ONE_BYTE (c); | |
1474 | } | |
1475 | else | |
1476 | { | |
db274c7a | 1477 | CHAR_STRING_ADVANCE_NO_UNIFY (c, pend); |
28f67a95 KH |
1478 | for (p = str; p < pend; p++) |
1479 | EMIT_ONE_BYTE (*p); | |
1480 | } | |
b73bfc1c | 1481 | } |
aa72b389 | 1482 | } |
df7492f9 KH |
1483 | else |
1484 | { | |
1485 | int safe_room = MAX_MULTIBYTE_LENGTH; | |
1486 | ||
1487 | while (charbuf < charbuf_end) | |
b73bfc1c | 1488 | { |
df7492f9 KH |
1489 | ASSURE_DESTINATION (safe_room); |
1490 | c = *charbuf++; | |
f03caae0 KH |
1491 | if (CHAR_BYTE8_P (c)) |
1492 | *dst++ = CHAR_TO_BYTE8 (c); | |
1493 | else | |
db274c7a | 1494 | CHAR_STRING_ADVANCE_NO_UNIFY (c, dst); |
df7492f9 | 1495 | produced_chars++; |
4ed46869 KH |
1496 | } |
1497 | } | |
065e3595 | 1498 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
1499 | coding->produced_char += produced_chars; |
1500 | coding->produced = dst - coding->destination; | |
1501 | return 0; | |
4ed46869 KH |
1502 | } |
1503 | ||
b73bfc1c | 1504 | |
df7492f9 | 1505 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". |
ff0dacd7 KH |
1506 | Check if a text is encoded in one of UTF-16 based coding systems. |
1507 | If it is, return 1, else return 0. */ | |
aa72b389 | 1508 | |
df7492f9 KH |
1509 | #define UTF_16_HIGH_SURROGATE_P(val) \ |
1510 | (((val) & 0xFC00) == 0xD800) | |
1511 | ||
1512 | #define UTF_16_LOW_SURROGATE_P(val) \ | |
1513 | (((val) & 0xFC00) == 0xDC00) | |
93dec019 | 1514 | |
aa72b389 | 1515 | |
df7492f9 | 1516 | static int |
cf84bb53 JB |
1517 | detect_coding_utf_16 (struct coding_system *coding, |
1518 | struct coding_detection_info *detect_info) | |
aa72b389 | 1519 | { |
ef1b0ba7 | 1520 | const unsigned char *src = coding->source; |
8f924df7 | 1521 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 | 1522 | int multibytep = coding->src_multibyte; |
df7492f9 | 1523 | int c1, c2; |
aa72b389 | 1524 | |
ff0dacd7 | 1525 | detect_info->checked |= CATEGORY_MASK_UTF_16; |
ff0dacd7 | 1526 | if (coding->mode & CODING_MODE_LAST_BLOCK |
24a73b0a | 1527 | && (coding->src_chars & 1)) |
ff0dacd7 KH |
1528 | { |
1529 | detect_info->rejected |= CATEGORY_MASK_UTF_16; | |
1530 | return 0; | |
1531 | } | |
24a73b0a | 1532 | |
f56a4450 | 1533 | TWO_MORE_BYTES (c1, c2); |
df7492f9 | 1534 | if ((c1 == 0xFF) && (c2 == 0xFE)) |
aa72b389 | 1535 | { |
b49a1807 KH |
1536 | detect_info->found |= (CATEGORY_MASK_UTF_16_LE |
1537 | | CATEGORY_MASK_UTF_16_AUTO); | |
24a73b0a KH |
1538 | detect_info->rejected |= (CATEGORY_MASK_UTF_16_BE |
1539 | | CATEGORY_MASK_UTF_16_BE_NOSIG | |
1540 | | CATEGORY_MASK_UTF_16_LE_NOSIG); | |
aa72b389 | 1541 | } |
df7492f9 | 1542 | else if ((c1 == 0xFE) && (c2 == 0xFF)) |
ff0dacd7 | 1543 | { |
b49a1807 KH |
1544 | detect_info->found |= (CATEGORY_MASK_UTF_16_BE |
1545 | | CATEGORY_MASK_UTF_16_AUTO); | |
24a73b0a KH |
1546 | detect_info->rejected |= (CATEGORY_MASK_UTF_16_LE |
1547 | | CATEGORY_MASK_UTF_16_BE_NOSIG | |
1548 | | CATEGORY_MASK_UTF_16_LE_NOSIG); | |
1549 | } | |
220eeac9 | 1550 | else if (c2 < 0) |
f56a4450 KH |
1551 | { |
1552 | detect_info->rejected |= CATEGORY_MASK_UTF_16; | |
1553 | return 0; | |
1554 | } | |
2f3cbb32 | 1555 | else |
24a73b0a | 1556 | { |
2f3cbb32 KH |
1557 | /* We check the dispersion of Eth and Oth bytes where E is even and |
1558 | O is odd. If both are high, we assume binary data.*/ | |
1559 | unsigned char e[256], o[256]; | |
1560 | unsigned e_num = 1, o_num = 1; | |
1561 | ||
1562 | memset (e, 0, 256); | |
1563 | memset (o, 0, 256); | |
1564 | e[c1] = 1; | |
1565 | o[c2] = 1; | |
1566 | ||
cc13543e KH |
1567 | detect_info->rejected |= (CATEGORY_MASK_UTF_16_AUTO |
1568 | |CATEGORY_MASK_UTF_16_BE | |
1569 | | CATEGORY_MASK_UTF_16_LE); | |
2f3cbb32 | 1570 | |
7f1faf1c KH |
1571 | while ((detect_info->rejected & CATEGORY_MASK_UTF_16) |
1572 | != CATEGORY_MASK_UTF_16) | |
2f3cbb32 | 1573 | { |
f56a4450 | 1574 | TWO_MORE_BYTES (c1, c2); |
220eeac9 | 1575 | if (c2 < 0) |
f56a4450 | 1576 | break; |
2f3cbb32 KH |
1577 | if (! e[c1]) |
1578 | { | |
1579 | e[c1] = 1; | |
1580 | e_num++; | |
cc13543e KH |
1581 | if (e_num >= 128) |
1582 | detect_info->rejected |= CATEGORY_MASK_UTF_16_BE_NOSIG; | |
2f3cbb32 KH |
1583 | } |
1584 | if (! o[c2]) | |
1585 | { | |
977b85f4 | 1586 | o[c2] = 1; |
2f3cbb32 | 1587 | o_num++; |
cc13543e KH |
1588 | if (o_num >= 128) |
1589 | detect_info->rejected |= CATEGORY_MASK_UTF_16_LE_NOSIG; | |
2f3cbb32 KH |
1590 | } |
1591 | } | |
2f3cbb32 | 1592 | return 0; |
ff0dacd7 | 1593 | } |
2f3cbb32 | 1594 | |
df7492f9 | 1595 | no_more_source: |
ff0dacd7 | 1596 | return 1; |
df7492f9 | 1597 | } |
aa72b389 | 1598 | |
df7492f9 | 1599 | static void |
971de7fb | 1600 | decode_coding_utf_16 (struct coding_system *coding) |
df7492f9 | 1601 | { |
8f924df7 KH |
1602 | const unsigned char *src = coding->source + coding->consumed; |
1603 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
1604 | const unsigned char *src_base; | |
69a80ea3 | 1605 | int *charbuf = coding->charbuf + coding->charbuf_used; |
df80c7f0 KH |
1606 | /* We may produces at most 3 chars in one loop. */ |
1607 | int *charbuf_end = coding->charbuf + coding->charbuf_size - 2; | |
3a8406e1 | 1608 | int consumed_chars = 0, consumed_chars_base = 0; |
df7492f9 | 1609 | int multibytep = coding->src_multibyte; |
a470d443 | 1610 | enum utf_bom_type bom = CODING_UTF_16_BOM (coding); |
df7492f9 KH |
1611 | enum utf_16_endian_type endian = CODING_UTF_16_ENDIAN (coding); |
1612 | int surrogate = CODING_UTF_16_SURROGATE (coding); | |
24a73b0a | 1613 | Lisp_Object attr, charset_list; |
2735d060 | 1614 | int eol_dos = |
0a9564cb | 1615 | !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); |
119852e7 | 1616 | int byte_after_cr1 = -1, byte_after_cr2 = -1; |
df7492f9 | 1617 | |
24a73b0a | 1618 | CODING_GET_INFO (coding, attr, charset_list); |
df7492f9 | 1619 | |
a470d443 | 1620 | if (bom == utf_with_bom) |
aa72b389 | 1621 | { |
df7492f9 | 1622 | int c, c1, c2; |
4af310db | 1623 | |
aa72b389 | 1624 | src_base = src; |
df7492f9 KH |
1625 | ONE_MORE_BYTE (c1); |
1626 | ONE_MORE_BYTE (c2); | |
e19c3639 | 1627 | c = (c1 << 8) | c2; |
aa72b389 | 1628 | |
b49a1807 KH |
1629 | if (endian == utf_16_big_endian |
1630 | ? c != 0xFEFF : c != 0xFFFE) | |
aa72b389 | 1631 | { |
b49a1807 KH |
1632 | /* The first two bytes are not BOM. Treat them as bytes |
1633 | for a normal character. */ | |
1634 | src = src_base; | |
1635 | coding->errors++; | |
aa72b389 | 1636 | } |
a470d443 | 1637 | CODING_UTF_16_BOM (coding) = utf_without_bom; |
b49a1807 | 1638 | } |
a470d443 | 1639 | else if (bom == utf_detect_bom) |
b49a1807 KH |
1640 | { |
1641 | /* We have already tried to detect BOM and failed in | |
1642 | detect_coding. */ | |
a470d443 | 1643 | CODING_UTF_16_BOM (coding) = utf_without_bom; |
df7492f9 | 1644 | } |
aa72b389 | 1645 | |
df7492f9 KH |
1646 | while (1) |
1647 | { | |
1648 | int c, c1, c2; | |
1649 | ||
1650 | src_base = src; | |
1651 | consumed_chars_base = consumed_chars; | |
1652 | ||
df80c7f0 | 1653 | if (charbuf >= charbuf_end) |
b71f6f73 KH |
1654 | { |
1655 | if (byte_after_cr1 >= 0) | |
1656 | src_base -= 2; | |
1657 | break; | |
1658 | } | |
df7492f9 | 1659 | |
119852e7 KH |
1660 | if (byte_after_cr1 >= 0) |
1661 | c1 = byte_after_cr1, byte_after_cr1 = -1; | |
1662 | else | |
1663 | ONE_MORE_BYTE (c1); | |
065e3595 KH |
1664 | if (c1 < 0) |
1665 | { | |
1666 | *charbuf++ = -c1; | |
1667 | continue; | |
1668 | } | |
119852e7 KH |
1669 | if (byte_after_cr2 >= 0) |
1670 | c2 = byte_after_cr2, byte_after_cr2 = -1; | |
1671 | else | |
1672 | ONE_MORE_BYTE (c2); | |
065e3595 KH |
1673 | if (c2 < 0) |
1674 | { | |
1675 | *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1); | |
1676 | *charbuf++ = -c2; | |
1677 | continue; | |
1678 | } | |
df7492f9 | 1679 | c = (endian == utf_16_big_endian |
e19c3639 | 1680 | ? ((c1 << 8) | c2) : ((c2 << 8) | c1)); |
119852e7 | 1681 | |
df7492f9 | 1682 | if (surrogate) |
fd3ae0b9 | 1683 | { |
df7492f9 | 1684 | if (! UTF_16_LOW_SURROGATE_P (c)) |
fd3ae0b9 | 1685 | { |
df7492f9 KH |
1686 | if (endian == utf_16_big_endian) |
1687 | c1 = surrogate >> 8, c2 = surrogate & 0xFF; | |
1688 | else | |
1689 | c1 = surrogate & 0xFF, c2 = surrogate >> 8; | |
1690 | *charbuf++ = c1; | |
1691 | *charbuf++ = c2; | |
1692 | coding->errors++; | |
1693 | if (UTF_16_HIGH_SURROGATE_P (c)) | |
1694 | CODING_UTF_16_SURROGATE (coding) = surrogate = c; | |
fd3ae0b9 | 1695 | else |
df7492f9 | 1696 | *charbuf++ = c; |
fd3ae0b9 KH |
1697 | } |
1698 | else | |
df7492f9 KH |
1699 | { |
1700 | c = ((surrogate - 0xD800) << 10) | (c - 0xDC00); | |
1701 | CODING_UTF_16_SURROGATE (coding) = surrogate = 0; | |
29f7ffd0 | 1702 | *charbuf++ = 0x10000 + c; |
df7492f9 | 1703 | } |
fd3ae0b9 | 1704 | } |
aa72b389 | 1705 | else |
df7492f9 KH |
1706 | { |
1707 | if (UTF_16_HIGH_SURROGATE_P (c)) | |
1708 | CODING_UTF_16_SURROGATE (coding) = surrogate = c; | |
1709 | else | |
119852e7 | 1710 | { |
2735d060 | 1711 | if (eol_dos && c == '\r') |
119852e7 KH |
1712 | { |
1713 | ONE_MORE_BYTE (byte_after_cr1); | |
1714 | ONE_MORE_BYTE (byte_after_cr2); | |
1715 | } | |
1716 | *charbuf++ = c; | |
1717 | } | |
8f924df7 | 1718 | } |
aa72b389 | 1719 | } |
df7492f9 KH |
1720 | |
1721 | no_more_source: | |
1722 | coding->consumed_char += consumed_chars_base; | |
1723 | coding->consumed = src_base - coding->source; | |
1724 | coding->charbuf_used = charbuf - coding->charbuf; | |
aa72b389 | 1725 | } |
b73bfc1c | 1726 | |
df7492f9 | 1727 | static int |
971de7fb | 1728 | encode_coding_utf_16 (struct coding_system *coding) |
df7492f9 KH |
1729 | { |
1730 | int multibytep = coding->dst_multibyte; | |
1731 | int *charbuf = coding->charbuf; | |
1732 | int *charbuf_end = charbuf + coding->charbuf_used; | |
1733 | unsigned char *dst = coding->destination + coding->produced; | |
1734 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
1735 | int safe_room = 8; | |
a470d443 | 1736 | enum utf_bom_type bom = CODING_UTF_16_BOM (coding); |
df7492f9 KH |
1737 | int big_endian = CODING_UTF_16_ENDIAN (coding) == utf_16_big_endian; |
1738 | int produced_chars = 0; | |
24a73b0a | 1739 | Lisp_Object attrs, charset_list; |
df7492f9 | 1740 | int c; |
4ed46869 | 1741 | |
24a73b0a | 1742 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 | 1743 | |
a470d443 | 1744 | if (bom != utf_without_bom) |
df7492f9 KH |
1745 | { |
1746 | ASSURE_DESTINATION (safe_room); | |
1747 | if (big_endian) | |
df7492f9 | 1748 | EMIT_TWO_BYTES (0xFE, 0xFF); |
880cf180 KH |
1749 | else |
1750 | EMIT_TWO_BYTES (0xFF, 0xFE); | |
a470d443 | 1751 | CODING_UTF_16_BOM (coding) = utf_without_bom; |
df7492f9 KH |
1752 | } |
1753 | ||
1754 | while (charbuf < charbuf_end) | |
1755 | { | |
1756 | ASSURE_DESTINATION (safe_room); | |
1757 | c = *charbuf++; | |
60afa08d | 1758 | if (c > MAX_UNICODE_CHAR) |
e19c3639 | 1759 | c = coding->default_char; |
df7492f9 KH |
1760 | |
1761 | if (c < 0x10000) | |
1762 | { | |
1763 | if (big_endian) | |
1764 | EMIT_TWO_BYTES (c >> 8, c & 0xFF); | |
1765 | else | |
1766 | EMIT_TWO_BYTES (c & 0xFF, c >> 8); | |
1767 | } | |
1768 | else | |
1769 | { | |
1770 | int c1, c2; | |
1771 | ||
1772 | c -= 0x10000; | |
1773 | c1 = (c >> 10) + 0xD800; | |
1774 | c2 = (c & 0x3FF) + 0xDC00; | |
1775 | if (big_endian) | |
1776 | EMIT_FOUR_BYTES (c1 >> 8, c1 & 0xFF, c2 >> 8, c2 & 0xFF); | |
1777 | else | |
1778 | EMIT_FOUR_BYTES (c1 & 0xFF, c1 >> 8, c2 & 0xFF, c2 >> 8); | |
1779 | } | |
1780 | } | |
065e3595 | 1781 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
1782 | coding->produced = dst - coding->destination; |
1783 | coding->produced_char += produced_chars; | |
1784 | return 0; | |
1785 | } | |
1786 | ||
1787 | \f | |
1788 | /*** 6. Old Emacs' internal format (emacs-mule) ***/ | |
1789 | ||
1790 | /* Emacs' internal format for representation of multiple character | |
1791 | sets is a kind of multi-byte encoding, i.e. characters are | |
1792 | represented by variable-length sequences of one-byte codes. | |
1793 | ||
1794 | ASCII characters and control characters (e.g. `tab', `newline') are | |
1795 | represented by one-byte sequences which are their ASCII codes, in | |
1796 | the range 0x00 through 0x7F. | |
1797 | ||
1798 | 8-bit characters of the range 0x80..0x9F are represented by | |
1799 | two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit | |
1800 | code + 0x20). | |
1801 | ||
1802 | 8-bit characters of the range 0xA0..0xFF are represented by | |
1803 | one-byte sequences which are their 8-bit code. | |
1804 | ||
1805 | The other characters are represented by a sequence of `base | |
1806 | leading-code', optional `extended leading-code', and one or two | |
1807 | `position-code's. The length of the sequence is determined by the | |
1808 | base leading-code. Leading-code takes the range 0x81 through 0x9D, | |
1809 | whereas extended leading-code and position-code take the range 0xA0 | |
1810 | through 0xFF. See `charset.h' for more details about leading-code | |
1811 | and position-code. | |
1812 | ||
1813 | --- CODE RANGE of Emacs' internal format --- | |
1814 | character set range | |
1815 | ------------- ----- | |
1816 | ascii 0x00..0x7F | |
1817 | eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF | |
1818 | eight-bit-graphic 0xA0..0xBF | |
1819 | ELSE 0x81..0x9D + [0xA0..0xFF]+ | |
1820 | --------------------------------------------- | |
1821 | ||
1822 | As this is the internal character representation, the format is | |
1823 | usually not used externally (i.e. in a file or in a data sent to a | |
1824 | process). But, it is possible to have a text externally in this | |
1825 | format (i.e. by encoding by the coding system `emacs-mule'). | |
1826 | ||
1827 | In that case, a sequence of one-byte codes has a slightly different | |
1828 | form. | |
1829 | ||
1830 | At first, all characters in eight-bit-control are represented by | |
1831 | one-byte sequences which are their 8-bit code. | |
1832 | ||
1833 | Next, character composition data are represented by the byte | |
1834 | sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ..., | |
1835 | where, | |
e951386e | 1836 | METHOD is 0xF2 plus one of composition method (enum |
df7492f9 KH |
1837 | composition_method), |
1838 | ||
1839 | BYTES is 0xA0 plus a byte length of this composition data, | |
1840 | ||
e951386e | 1841 | CHARS is 0xA0 plus a number of characters composed by this |
df7492f9 KH |
1842 | data, |
1843 | ||
ad1746f5 | 1844 | COMPONENTs are characters of multibyte form or composition |
df7492f9 KH |
1845 | rules encoded by two-byte of ASCII codes. |
1846 | ||
1847 | In addition, for backward compatibility, the following formats are | |
1848 | also recognized as composition data on decoding. | |
1849 | ||
1850 | 0x80 MSEQ ... | |
1851 | 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ | |
1852 | ||
1853 | Here, | |
1854 | MSEQ is a multibyte form but in these special format: | |
1855 | ASCII: 0xA0 ASCII_CODE+0x80, | |
1856 | other: LEADING_CODE+0x20 FOLLOWING-BYTE ..., | |
1857 | RULE is a one byte code of the range 0xA0..0xF0 that | |
1858 | represents a composition rule. | |
1859 | */ | |
1860 | ||
1861 | char emacs_mule_bytes[256]; | |
1862 | ||
e951386e KH |
1863 | |
1864 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
1865 | Check if a text is encoded in `emacs-mule'. If it is, return 1, | |
1866 | else return 0. */ | |
1867 | ||
1868 | static int | |
cf84bb53 JB |
1869 | detect_coding_emacs_mule (struct coding_system *coding, |
1870 | struct coding_detection_info *detect_info) | |
e951386e KH |
1871 | { |
1872 | const unsigned char *src = coding->source, *src_base; | |
1873 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
1874 | int multibytep = coding->src_multibyte; | |
1875 | int consumed_chars = 0; | |
1876 | int c; | |
1877 | int found = 0; | |
1878 | ||
1879 | detect_info->checked |= CATEGORY_MASK_EMACS_MULE; | |
1880 | /* A coding system of this category is always ASCII compatible. */ | |
1881 | src += coding->head_ascii; | |
1882 | ||
1883 | while (1) | |
1884 | { | |
1885 | src_base = src; | |
1886 | ONE_MORE_BYTE (c); | |
1887 | if (c < 0) | |
1888 | continue; | |
1889 | if (c == 0x80) | |
1890 | { | |
1891 | /* Perhaps the start of composite character. We simply skip | |
1892 | it because analyzing it is too heavy for detecting. But, | |
1893 | at least, we check that the composite character | |
1894 | constitutes of more than 4 bytes. */ | |
2735d060 | 1895 | const unsigned char *src_start; |
e951386e KH |
1896 | |
1897 | repeat: | |
2735d060 | 1898 | src_start = src; |
e951386e KH |
1899 | do |
1900 | { | |
1901 | ONE_MORE_BYTE (c); | |
1902 | } | |
1903 | while (c >= 0xA0); | |
1904 | ||
2735d060 | 1905 | if (src - src_start <= 4) |
e951386e KH |
1906 | break; |
1907 | found = CATEGORY_MASK_EMACS_MULE; | |
1908 | if (c == 0x80) | |
1909 | goto repeat; | |
1910 | } | |
1911 | ||
1912 | if (c < 0x80) | |
1913 | { | |
1914 | if (c < 0x20 | |
1915 | && (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)) | |
1916 | break; | |
1917 | } | |
1918 | else | |
1919 | { | |
396475b7 | 1920 | int more_bytes = emacs_mule_bytes[c] - 1; |
e951386e KH |
1921 | |
1922 | while (more_bytes > 0) | |
1923 | { | |
1924 | ONE_MORE_BYTE (c); | |
1925 | if (c < 0xA0) | |
1926 | { | |
1927 | src--; /* Unread the last byte. */ | |
1928 | break; | |
1929 | } | |
1930 | more_bytes--; | |
1931 | } | |
1932 | if (more_bytes != 0) | |
1933 | break; | |
1934 | found = CATEGORY_MASK_EMACS_MULE; | |
1935 | } | |
1936 | } | |
1937 | detect_info->rejected |= CATEGORY_MASK_EMACS_MULE; | |
1938 | return 0; | |
1939 | ||
1940 | no_more_source: | |
1941 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) | |
1942 | { | |
1943 | detect_info->rejected |= CATEGORY_MASK_EMACS_MULE; | |
1944 | return 0; | |
1945 | } | |
1946 | detect_info->found |= found; | |
1947 | return 1; | |
1948 | } | |
1949 | ||
1950 | ||
1951 | /* Parse emacs-mule multibyte sequence at SRC and return the decoded | |
1952 | character. If CMP_STATUS indicates that we must expect MSEQ or | |
1953 | RULE described above, decode it and return the negative value of | |
685ebdc8 | 1954 | the decoded character or rule. If an invalid byte is found, return |
e951386e KH |
1955 | -1. If SRC is too short, return -2. */ |
1956 | ||
e2f1bab9 | 1957 | static int |
cf84bb53 JB |
1958 | emacs_mule_char (struct coding_system *coding, const unsigned char *src, |
1959 | int *nbytes, int *nchars, int *id, | |
1960 | struct composition_status *cmp_status) | |
df7492f9 | 1961 | { |
8f924df7 KH |
1962 | const unsigned char *src_end = coding->source + coding->src_bytes; |
1963 | const unsigned char *src_base = src; | |
df7492f9 | 1964 | int multibytep = coding->src_multibyte; |
2735d060 | 1965 | int charset_ID; |
df7492f9 KH |
1966 | unsigned code; |
1967 | int c; | |
1968 | int consumed_chars = 0; | |
e951386e | 1969 | int mseq_found = 0; |
df7492f9 KH |
1970 | |
1971 | ONE_MORE_BYTE (c); | |
065e3595 | 1972 | if (c < 0) |
df7492f9 | 1973 | { |
065e3595 | 1974 | c = -c; |
2735d060 | 1975 | charset_ID = emacs_mule_charset[0]; |
065e3595 KH |
1976 | } |
1977 | else | |
1978 | { | |
4d41e8b7 KH |
1979 | if (c >= 0xA0) |
1980 | { | |
e951386e KH |
1981 | if (cmp_status->state != COMPOSING_NO |
1982 | && cmp_status->old_form) | |
4d41e8b7 | 1983 | { |
e951386e KH |
1984 | if (cmp_status->state == COMPOSING_CHAR) |
1985 | { | |
1986 | if (c == 0xA0) | |
1987 | { | |
1988 | ONE_MORE_BYTE (c); | |
1989 | c -= 0x80; | |
1990 | if (c < 0) | |
1991 | goto invalid_code; | |
1992 | } | |
1993 | else | |
1994 | c -= 0x20; | |
1995 | mseq_found = 1; | |
1996 | } | |
1997 | else | |
1998 | { | |
1999 | *nbytes = src - src_base; | |
2000 | *nchars = consumed_chars; | |
2001 | return -c; | |
2002 | } | |
4d41e8b7 KH |
2003 | } |
2004 | else | |
e951386e | 2005 | goto invalid_code; |
4d41e8b7 KH |
2006 | } |
2007 | ||
065e3595 | 2008 | switch (emacs_mule_bytes[c]) |
b73bfc1c | 2009 | { |
065e3595 | 2010 | case 2: |
2735d060 | 2011 | if ((charset_ID = emacs_mule_charset[c]) < 0) |
df7492f9 KH |
2012 | goto invalid_code; |
2013 | ONE_MORE_BYTE (c); | |
9ffd559c | 2014 | if (c < 0xA0) |
065e3595 | 2015 | goto invalid_code; |
df7492f9 | 2016 | code = c & 0x7F; |
065e3595 KH |
2017 | break; |
2018 | ||
2019 | case 3: | |
2020 | if (c == EMACS_MULE_LEADING_CODE_PRIVATE_11 | |
2021 | || c == EMACS_MULE_LEADING_CODE_PRIVATE_12) | |
2022 | { | |
2023 | ONE_MORE_BYTE (c); | |
2735d060 | 2024 | if (c < 0xA0 || (charset_ID = emacs_mule_charset[c]) < 0) |
065e3595 KH |
2025 | goto invalid_code; |
2026 | ONE_MORE_BYTE (c); | |
9ffd559c | 2027 | if (c < 0xA0) |
065e3595 KH |
2028 | goto invalid_code; |
2029 | code = c & 0x7F; | |
2030 | } | |
2031 | else | |
2032 | { | |
2735d060 | 2033 | if ((charset_ID = emacs_mule_charset[c]) < 0) |
065e3595 KH |
2034 | goto invalid_code; |
2035 | ONE_MORE_BYTE (c); | |
9ffd559c | 2036 | if (c < 0xA0) |
065e3595 KH |
2037 | goto invalid_code; |
2038 | code = (c & 0x7F) << 8; | |
2039 | ONE_MORE_BYTE (c); | |
9ffd559c | 2040 | if (c < 0xA0) |
065e3595 KH |
2041 | goto invalid_code; |
2042 | code |= c & 0x7F; | |
2043 | } | |
2044 | break; | |
2045 | ||
2046 | case 4: | |
2047 | ONE_MORE_BYTE (c); | |
2735d060 | 2048 | if (c < 0 || (charset_ID = emacs_mule_charset[c]) < 0) |
df7492f9 KH |
2049 | goto invalid_code; |
2050 | ONE_MORE_BYTE (c); | |
9ffd559c | 2051 | if (c < 0xA0) |
065e3595 | 2052 | goto invalid_code; |
781d7a48 | 2053 | code = (c & 0x7F) << 8; |
df7492f9 | 2054 | ONE_MORE_BYTE (c); |
9ffd559c | 2055 | if (c < 0xA0) |
065e3595 | 2056 | goto invalid_code; |
df7492f9 | 2057 | code |= c & 0x7F; |
065e3595 | 2058 | break; |
df7492f9 | 2059 | |
065e3595 KH |
2060 | case 1: |
2061 | code = c; | |
2735d060 | 2062 | charset_ID = ASCII_BYTE_P (code) ? charset_ascii : charset_eight_bit; |
065e3595 | 2063 | break; |
df7492f9 | 2064 | |
065e3595 KH |
2065 | default: |
2066 | abort (); | |
2067 | } | |
b84ae584 | 2068 | CODING_DECODE_CHAR (coding, src, src_base, src_end, |
2735d060 | 2069 | CHARSET_FROM_ID (charset_ID), code, c); |
065e3595 KH |
2070 | if (c < 0) |
2071 | goto invalid_code; | |
df7492f9 | 2072 | } |
df7492f9 KH |
2073 | *nbytes = src - src_base; |
2074 | *nchars = consumed_chars; | |
ff0dacd7 | 2075 | if (id) |
2735d060 | 2076 | *id = charset_ID; |
e951386e | 2077 | return (mseq_found ? -c : c); |
df7492f9 KH |
2078 | |
2079 | no_more_source: | |
2080 | return -2; | |
2081 | ||
2082 | invalid_code: | |
2083 | return -1; | |
2084 | } | |
2085 | ||
2086 | ||
e951386e | 2087 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ |
df7492f9 | 2088 | |
e951386e KH |
2089 | /* Handle these composition sequence ('|': the end of header elements, |
2090 | BYTES and CHARS >= 0xA0): | |
df7492f9 | 2091 | |
e951386e KH |
2092 | (1) relative composition: 0x80 0xF2 BYTES CHARS | CHAR ... |
2093 | (2) altchar composition: 0x80 0xF4 BYTES CHARS | ALT ... ALT CHAR ... | |
2094 | (3) alt&rule composition: 0x80 0xF5 BYTES CHARS | ALT RULE ... ALT CHAR ... | |
df7492f9 | 2095 | |
e951386e | 2096 | and these old form: |
1a4990fb | 2097 | |
e951386e KH |
2098 | (4) relative composition: 0x80 | MSEQ ... MSEQ |
2099 | (5) rulebase composition: 0x80 0xFF | MSEQ MRULE ... MSEQ | |
df7492f9 | 2100 | |
e951386e KH |
2101 | When the starter 0x80 and the following header elements are found, |
2102 | this annotation header is produced. | |
df7492f9 | 2103 | |
e951386e | 2104 | [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS NBYTES METHOD ] |
df7492f9 | 2105 | |
e951386e KH |
2106 | NCHARS is CHARS - 0xA0 for (1), (2), (3), and 0 for (4), (5). |
2107 | NBYTES is BYTES - 0xA0 for (1), (2), (3), and 0 for (4), (5). | |
df7492f9 | 2108 | |
e951386e KH |
2109 | Then, upon reading the following elements, these codes are produced |
2110 | until the composition end is found: | |
df7492f9 | 2111 | |
e951386e KH |
2112 | (1) CHAR ... CHAR |
2113 | (2) ALT ... ALT CHAR ... CHAR | |
2114 | (3) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT CHAR ... CHAR | |
2115 | (4) CHAR ... CHAR | |
2116 | (5) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR | |
4ed46869 | 2117 | |
e951386e KH |
2118 | When the composition end is found, LENGTH and NCHARS in the |
2119 | annotation header is updated as below: | |
b73bfc1c | 2120 | |
e951386e KH |
2121 | (1) LENGTH: unchanged, NCHARS: unchanged |
2122 | (2) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged | |
2123 | (3) LENGTH: length of the whole sequence minus NCHARS, NCHARS: unchanged | |
2124 | (4) LENGTH: unchanged, NCHARS: number of CHARs | |
2125 | (5) LENGTH: unchanged, NCHARS: number of CHARs | |
df7492f9 | 2126 | |
e951386e KH |
2127 | If an error is found while composing, the annotation header is |
2128 | changed to the original composition header (plus filler -1s) as | |
2129 | below: | |
2130 | ||
2131 | (1),(2),(3) [ 0x80 0xF2+METHOD BYTES CHARS -1 ] | |
2132 | (5) [ 0x80 0xFF -1 -1- -1 ] | |
2133 | ||
2134 | and the sequence [ -2 DECODED-RULE ] is changed to the original | |
2135 | byte sequence as below: | |
2136 | o the original byte sequence is B: [ B -1 ] | |
2137 | o the original byte sequence is B1 B2: [ B1 B2 ] | |
2138 | ||
2139 | Most of the routines are implemented by macros because many | |
2140 | variables and labels in the caller decode_coding_emacs_mule must be | |
2141 | accessible, and they are usually called just once (thus doesn't | |
2142 | increase the size of compiled object). */ | |
2143 | ||
2144 | /* Decode a composition rule represented by C as a component of | |
2145 | composition sequence of Emacs 20 style. Set RULE to the decoded | |
2146 | rule. */ | |
2147 | ||
2148 | #define DECODE_EMACS_MULE_COMPOSITION_RULE_20(c, rule) \ | |
df7492f9 | 2149 | do { \ |
e951386e KH |
2150 | int gref, nref; \ |
2151 | \ | |
4d41e8b7 | 2152 | c -= 0xA0; \ |
df7492f9 KH |
2153 | if (c < 0 || c >= 81) \ |
2154 | goto invalid_code; \ | |
df7492f9 | 2155 | gref = c / 9, nref = c % 9; \ |
e951386e KH |
2156 | if (gref == 4) gref = 10; \ |
2157 | if (nref == 4) nref = 10; \ | |
2158 | rule = COMPOSITION_ENCODE_RULE (gref, nref); \ | |
df7492f9 KH |
2159 | } while (0) |
2160 | ||
2161 | ||
e951386e KH |
2162 | /* Decode a composition rule represented by C and the following byte |
2163 | at SRC as a component of composition sequence of Emacs 21 style. | |
2164 | Set RULE to the decoded rule. */ | |
781d7a48 | 2165 | |
e951386e | 2166 | #define DECODE_EMACS_MULE_COMPOSITION_RULE_21(c, rule) \ |
781d7a48 KH |
2167 | do { \ |
2168 | int gref, nref; \ | |
e951386e KH |
2169 | \ |
2170 | gref = c - 0x20; \ | |
2171 | if (gref < 0 || gref >= 81) \ | |
781d7a48 | 2172 | goto invalid_code; \ |
e951386e KH |
2173 | ONE_MORE_BYTE (c); \ |
2174 | nref = c - 0x20; \ | |
2175 | if (nref < 0 || nref >= 81) \ | |
781d7a48 | 2176 | goto invalid_code; \ |
e951386e | 2177 | rule = COMPOSITION_ENCODE_RULE (gref, nref); \ |
781d7a48 KH |
2178 | } while (0) |
2179 | ||
2180 | ||
e951386e KH |
2181 | /* Start of Emacs 21 style format. The first three bytes at SRC are |
2182 | (METHOD - 0xF2), (BYTES - 0xA0), (CHARS - 0xA0), where BYTES is the | |
2183 | byte length of this composition information, CHARS is the number of | |
2184 | characters composed by this composition. */ | |
2185 | ||
2186 | #define DECODE_EMACS_MULE_21_COMPOSITION() \ | |
aa72b389 | 2187 | do { \ |
781d7a48 | 2188 | enum composition_method method = c - 0xF2; \ |
df7492f9 | 2189 | int nbytes, nchars; \ |
e951386e | 2190 | \ |
df7492f9 | 2191 | ONE_MORE_BYTE (c); \ |
065e3595 KH |
2192 | if (c < 0) \ |
2193 | goto invalid_code; \ | |
df7492f9 | 2194 | nbytes = c - 0xA0; \ |
e951386e | 2195 | if (nbytes < 3 || (method == COMPOSITION_RELATIVE && nbytes != 4)) \ |
df7492f9 KH |
2196 | goto invalid_code; \ |
2197 | ONE_MORE_BYTE (c); \ | |
2198 | nchars = c - 0xA0; \ | |
e951386e KH |
2199 | if (nchars <= 0 || nchars >= MAX_COMPOSITION_COMPONENTS) \ |
2200 | goto invalid_code; \ | |
2201 | cmp_status->old_form = 0; \ | |
2202 | cmp_status->method = method; \ | |
2203 | if (method == COMPOSITION_RELATIVE) \ | |
2204 | cmp_status->state = COMPOSING_CHAR; \ | |
2205 | else \ | |
2206 | cmp_status->state = COMPOSING_COMPONENT_CHAR; \ | |
2207 | cmp_status->length = MAX_ANNOTATION_LENGTH; \ | |
2208 | cmp_status->nchars = nchars; \ | |
2209 | cmp_status->ncomps = nbytes - 4; \ | |
2210 | ADD_COMPOSITION_DATA (charbuf, nchars, nbytes, method); \ | |
aa72b389 | 2211 | } while (0) |
93dec019 | 2212 | |
aa72b389 | 2213 | |
e951386e KH |
2214 | /* Start of Emacs 20 style format for relative composition. */ |
2215 | ||
2216 | #define DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION() \ | |
2217 | do { \ | |
2218 | cmp_status->old_form = 1; \ | |
2219 | cmp_status->method = COMPOSITION_RELATIVE; \ | |
2220 | cmp_status->state = COMPOSING_CHAR; \ | |
2221 | cmp_status->length = MAX_ANNOTATION_LENGTH; \ | |
2222 | cmp_status->nchars = cmp_status->ncomps = 0; \ | |
2223 | ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \ | |
2224 | } while (0) | |
2225 | ||
2226 | ||
2227 | /* Start of Emacs 20 style format for rule-base composition. */ | |
2228 | ||
2229 | #define DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION() \ | |
2230 | do { \ | |
2231 | cmp_status->old_form = 1; \ | |
2232 | cmp_status->method = COMPOSITION_WITH_RULE; \ | |
2233 | cmp_status->state = COMPOSING_CHAR; \ | |
2234 | cmp_status->length = MAX_ANNOTATION_LENGTH; \ | |
2235 | cmp_status->nchars = cmp_status->ncomps = 0; \ | |
2236 | ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \ | |
df7492f9 KH |
2237 | } while (0) |
2238 | ||
2239 | ||
e951386e KH |
2240 | #define DECODE_EMACS_MULE_COMPOSITION_START() \ |
2241 | do { \ | |
2242 | const unsigned char *current_src = src; \ | |
2243 | \ | |
2244 | ONE_MORE_BYTE (c); \ | |
2245 | if (c < 0) \ | |
2246 | goto invalid_code; \ | |
2247 | if (c - 0xF2 >= COMPOSITION_RELATIVE \ | |
2248 | && c - 0xF2 <= COMPOSITION_WITH_RULE_ALTCHARS) \ | |
2249 | DECODE_EMACS_MULE_21_COMPOSITION (); \ | |
2250 | else if (c < 0xA0) \ | |
2251 | goto invalid_code; \ | |
2252 | else if (c < 0xC0) \ | |
2253 | { \ | |
2254 | DECODE_EMACS_MULE_20_RELATIVE_COMPOSITION (); \ | |
2255 | /* Re-read C as a composition component. */ \ | |
2256 | src = current_src; \ | |
2257 | } \ | |
2258 | else if (c == 0xFF) \ | |
2259 | DECODE_EMACS_MULE_20_RULEBASE_COMPOSITION (); \ | |
2260 | else \ | |
2261 | goto invalid_code; \ | |
2262 | } while (0) | |
2263 | ||
2264 | #define EMACS_MULE_COMPOSITION_END() \ | |
df7492f9 | 2265 | do { \ |
e951386e | 2266 | int idx = - cmp_status->length; \ |
4d41e8b7 | 2267 | \ |
e951386e KH |
2268 | if (cmp_status->old_form) \ |
2269 | charbuf[idx + 2] = cmp_status->nchars; \ | |
2270 | else if (cmp_status->method > COMPOSITION_RELATIVE) \ | |
2271 | charbuf[idx] = charbuf[idx + 2] - cmp_status->length; \ | |
2272 | cmp_status->state = COMPOSING_NO; \ | |
2273 | } while (0) | |
2274 | ||
2275 | ||
2276 | static int | |
cf84bb53 JB |
2277 | emacs_mule_finish_composition (int *charbuf, |
2278 | struct composition_status *cmp_status) | |
e951386e KH |
2279 | { |
2280 | int idx = - cmp_status->length; | |
2281 | int new_chars; | |
2282 | ||
2283 | if (cmp_status->old_form && cmp_status->nchars > 0) | |
2284 | { | |
2285 | charbuf[idx + 2] = cmp_status->nchars; | |
2286 | new_chars = 0; | |
2287 | if (cmp_status->method == COMPOSITION_WITH_RULE | |
2288 | && cmp_status->state == COMPOSING_CHAR) | |
2289 | { | |
2290 | /* The last rule was invalid. */ | |
2291 | int rule = charbuf[-1] + 0xA0; | |
2292 | ||
2293 | charbuf[-2] = BYTE8_TO_CHAR (rule); | |
2294 | charbuf[-1] = -1; | |
2295 | new_chars = 1; | |
2296 | } | |
2297 | } | |
2298 | else | |
2299 | { | |
2300 | charbuf[idx++] = BYTE8_TO_CHAR (0x80); | |
2301 | ||
2302 | if (cmp_status->method == COMPOSITION_WITH_RULE) | |
2303 | { | |
2304 | charbuf[idx++] = BYTE8_TO_CHAR (0xFF); | |
2305 | charbuf[idx++] = -3; | |
2306 | charbuf[idx++] = 0; | |
2307 | new_chars = 1; | |
2308 | } | |
2309 | else | |
2310 | { | |
2311 | int nchars = charbuf[idx + 1] + 0xA0; | |
2312 | int nbytes = charbuf[idx + 2] + 0xA0; | |
2313 | ||
2314 | charbuf[idx++] = BYTE8_TO_CHAR (0xF2 + cmp_status->method); | |
2315 | charbuf[idx++] = BYTE8_TO_CHAR (nbytes); | |
2316 | charbuf[idx++] = BYTE8_TO_CHAR (nchars); | |
2317 | charbuf[idx++] = -1; | |
2318 | new_chars = 4; | |
2319 | } | |
2320 | } | |
2321 | cmp_status->state = COMPOSING_NO; | |
2322 | return new_chars; | |
2323 | } | |
2324 | ||
2325 | #define EMACS_MULE_MAYBE_FINISH_COMPOSITION() \ | |
2326 | do { \ | |
2327 | if (cmp_status->state != COMPOSING_NO) \ | |
2328 | char_offset += emacs_mule_finish_composition (charbuf, cmp_status); \ | |
df7492f9 KH |
2329 | } while (0) |
2330 | ||
aa72b389 KH |
2331 | |
2332 | static void | |
971de7fb | 2333 | decode_coding_emacs_mule (struct coding_system *coding) |
aa72b389 | 2334 | { |
8f924df7 KH |
2335 | const unsigned char *src = coding->source + coding->consumed; |
2336 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
2337 | const unsigned char *src_base; | |
69a80ea3 | 2338 | int *charbuf = coding->charbuf + coding->charbuf_used; |
ad1746f5 KH |
2339 | /* We may produce two annotations (charset and composition) in one |
2340 | loop and one more charset annotation at the end. */ | |
69a80ea3 | 2341 | int *charbuf_end |
df80c7f0 | 2342 | = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 3); |
df7492f9 | 2343 | int consumed_chars = 0, consumed_chars_base; |
df7492f9 | 2344 | int multibytep = coding->src_multibyte; |
24a73b0a | 2345 | Lisp_Object attrs, charset_list; |
ff0dacd7 KH |
2346 | int char_offset = coding->produced_char; |
2347 | int last_offset = char_offset; | |
2348 | int last_id = charset_ascii; | |
2735d060 | 2349 | int eol_dos = |
0a9564cb | 2350 | !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); |
119852e7 | 2351 | int byte_after_cr = -1; |
e951386e | 2352 | struct composition_status *cmp_status = &coding->spec.emacs_mule.cmp_status; |
aa72b389 | 2353 | |
24a73b0a | 2354 | CODING_GET_INFO (coding, attrs, charset_list); |
aa72b389 | 2355 | |
e951386e KH |
2356 | if (cmp_status->state != COMPOSING_NO) |
2357 | { | |
2358 | int i; | |
2359 | ||
2360 | for (i = 0; i < cmp_status->length; i++) | |
2361 | *charbuf++ = cmp_status->carryover[i]; | |
2362 | coding->annotated = 1; | |
2363 | } | |
2364 | ||
aa72b389 KH |
2365 | while (1) |
2366 | { | |
ee05f961 | 2367 | int c, id IF_LINT (= 0); |
df7492f9 | 2368 | |
aa72b389 | 2369 | src_base = src; |
df7492f9 KH |
2370 | consumed_chars_base = consumed_chars; |
2371 | ||
2372 | if (charbuf >= charbuf_end) | |
b71f6f73 KH |
2373 | { |
2374 | if (byte_after_cr >= 0) | |
2375 | src_base--; | |
2376 | break; | |
2377 | } | |
aa72b389 | 2378 | |
119852e7 KH |
2379 | if (byte_after_cr >= 0) |
2380 | c = byte_after_cr, byte_after_cr = -1; | |
2381 | else | |
2382 | ONE_MORE_BYTE (c); | |
e951386e KH |
2383 | |
2384 | if (c < 0 || c == 0x80) | |
065e3595 | 2385 | { |
e951386e KH |
2386 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); |
2387 | if (c < 0) | |
2388 | { | |
2389 | *charbuf++ = -c; | |
2390 | char_offset++; | |
2391 | } | |
2392 | else | |
2393 | DECODE_EMACS_MULE_COMPOSITION_START (); | |
2394 | continue; | |
065e3595 | 2395 | } |
e951386e KH |
2396 | |
2397 | if (c < 0x80) | |
aa72b389 | 2398 | { |
2735d060 | 2399 | if (eol_dos && c == '\r') |
119852e7 | 2400 | ONE_MORE_BYTE (byte_after_cr); |
e951386e KH |
2401 | id = charset_ascii; |
2402 | if (cmp_status->state != COMPOSING_NO) | |
2403 | { | |
2404 | if (cmp_status->old_form) | |
2405 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); | |
2406 | else if (cmp_status->state >= COMPOSING_COMPONENT_CHAR) | |
2407 | cmp_status->ncomps--; | |
2408 | } | |
2409 | } | |
2410 | else | |
2411 | { | |
ee05f961 | 2412 | int nchars IF_LINT (= 0), nbytes IF_LINT (= 0); |
75f80e63 EZ |
2413 | /* emacs_mule_char can load a charset map from a file, which |
2414 | allocates a large structure and might cause buffer text | |
2415 | to be relocated as result. Thus, we need to remember the | |
ad1746f5 | 2416 | original pointer to buffer text, and fix up all related |
75f80e63 EZ |
2417 | pointers after the call. */ |
2418 | const unsigned char *orig = coding->source; | |
2419 | EMACS_INT offset; | |
e951386e KH |
2420 | |
2421 | c = emacs_mule_char (coding, src_base, &nbytes, &nchars, &id, | |
2422 | cmp_status); | |
75f80e63 EZ |
2423 | offset = coding->source - orig; |
2424 | if (offset) | |
2425 | { | |
2426 | src += offset; | |
2427 | src_base += offset; | |
2428 | src_end += offset; | |
2429 | } | |
e951386e KH |
2430 | if (c < 0) |
2431 | { | |
2432 | if (c == -1) | |
2433 | goto invalid_code; | |
2434 | if (c == -2) | |
2435 | break; | |
2436 | } | |
2437 | src = src_base + nbytes; | |
2438 | consumed_chars = consumed_chars_base + nchars; | |
2439 | if (cmp_status->state >= COMPOSING_COMPONENT_CHAR) | |
2440 | cmp_status->ncomps -= nchars; | |
2441 | } | |
2442 | ||
ad1746f5 | 2443 | /* Now if C >= 0, we found a normally encoded character, if C < |
e951386e KH |
2444 | 0, we found an old-style composition component character or |
2445 | rule. */ | |
2446 | ||
2447 | if (cmp_status->state == COMPOSING_NO) | |
2448 | { | |
2449 | if (last_id != id) | |
2450 | { | |
2451 | if (last_id != charset_ascii) | |
2452 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, | |
2453 | last_id); | |
2454 | last_id = id; | |
2455 | last_offset = char_offset; | |
2456 | } | |
df7492f9 KH |
2457 | *charbuf++ = c; |
2458 | char_offset++; | |
aa72b389 | 2459 | } |
e951386e | 2460 | else if (cmp_status->state == COMPOSING_CHAR) |
df7492f9 | 2461 | { |
e951386e KH |
2462 | if (cmp_status->old_form) |
2463 | { | |
2464 | if (c >= 0) | |
2465 | { | |
2466 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); | |
2467 | *charbuf++ = c; | |
2468 | char_offset++; | |
2469 | } | |
2470 | else | |
2471 | { | |
2472 | *charbuf++ = -c; | |
2473 | cmp_status->nchars++; | |
2474 | cmp_status->length++; | |
2475 | if (cmp_status->nchars == MAX_COMPOSITION_COMPONENTS) | |
2476 | EMACS_MULE_COMPOSITION_END (); | |
2477 | else if (cmp_status->method == COMPOSITION_WITH_RULE) | |
2478 | cmp_status->state = COMPOSING_RULE; | |
2479 | } | |
2480 | } | |
df7492f9 | 2481 | else |
e951386e KH |
2482 | { |
2483 | *charbuf++ = c; | |
2484 | cmp_status->length++; | |
2485 | cmp_status->nchars--; | |
2486 | if (cmp_status->nchars == 0) | |
2487 | EMACS_MULE_COMPOSITION_END (); | |
2488 | } | |
df7492f9 | 2489 | } |
e951386e | 2490 | else if (cmp_status->state == COMPOSING_RULE) |
df7492f9 | 2491 | { |
e951386e | 2492 | int rule; |
ff0dacd7 | 2493 | |
e951386e | 2494 | if (c >= 0) |
df7492f9 | 2495 | { |
e951386e KH |
2496 | EMACS_MULE_COMPOSITION_END (); |
2497 | *charbuf++ = c; | |
2498 | char_offset++; | |
df7492f9 | 2499 | } |
e951386e | 2500 | else |
ff0dacd7 | 2501 | { |
e951386e KH |
2502 | c = -c; |
2503 | DECODE_EMACS_MULE_COMPOSITION_RULE_20 (c, rule); | |
2504 | if (rule < 0) | |
2505 | goto invalid_code; | |
2506 | *charbuf++ = -2; | |
2507 | *charbuf++ = rule; | |
2508 | cmp_status->length += 2; | |
2509 | cmp_status->state = COMPOSING_CHAR; | |
ff0dacd7 | 2510 | } |
e951386e KH |
2511 | } |
2512 | else if (cmp_status->state == COMPOSING_COMPONENT_CHAR) | |
2513 | { | |
df7492f9 | 2514 | *charbuf++ = c; |
e951386e KH |
2515 | cmp_status->length++; |
2516 | if (cmp_status->ncomps == 0) | |
2517 | cmp_status->state = COMPOSING_CHAR; | |
2518 | else if (cmp_status->ncomps > 0) | |
2519 | { | |
2520 | if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS) | |
2521 | cmp_status->state = COMPOSING_COMPONENT_RULE; | |
2522 | } | |
2523 | else | |
2524 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); | |
df7492f9 | 2525 | } |
e951386e KH |
2526 | else /* COMPOSING_COMPONENT_RULE */ |
2527 | { | |
2528 | int rule; | |
2529 | ||
2530 | DECODE_EMACS_MULE_COMPOSITION_RULE_21 (c, rule); | |
2531 | if (rule < 0) | |
2532 | goto invalid_code; | |
2533 | *charbuf++ = -2; | |
2534 | *charbuf++ = rule; | |
2535 | cmp_status->length += 2; | |
2536 | cmp_status->ncomps--; | |
2537 | if (cmp_status->ncomps > 0) | |
2538 | cmp_status->state = COMPOSING_COMPONENT_CHAR; | |
2539 | else | |
2540 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); | |
2541 | } | |
2542 | continue; | |
2543 | ||
df7492f9 | 2544 | invalid_code: |
e951386e | 2545 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); |
df7492f9 KH |
2546 | src = src_base; |
2547 | consumed_chars = consumed_chars_base; | |
2548 | ONE_MORE_BYTE (c); | |
2549 | *charbuf++ = ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); | |
ff0dacd7 | 2550 | char_offset++; |
df7492f9 KH |
2551 | coding->errors++; |
2552 | } | |
2553 | ||
2554 | no_more_source: | |
e951386e KH |
2555 | if (cmp_status->state != COMPOSING_NO) |
2556 | { | |
2557 | if (coding->mode & CODING_MODE_LAST_BLOCK) | |
2558 | EMACS_MULE_MAYBE_FINISH_COMPOSITION (); | |
2559 | else | |
2560 | { | |
2561 | int i; | |
2562 | ||
2563 | charbuf -= cmp_status->length; | |
2564 | for (i = 0; i < cmp_status->length; i++) | |
2565 | cmp_status->carryover[i] = charbuf[i]; | |
2566 | } | |
2567 | } | |
ff0dacd7 | 2568 | if (last_id != charset_ascii) |
69a80ea3 | 2569 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
2570 | coding->consumed_char += consumed_chars_base; |
2571 | coding->consumed = src_base - coding->source; | |
2572 | coding->charbuf_used = charbuf - coding->charbuf; | |
2573 | } | |
2574 | ||
2575 | ||
2576 | #define EMACS_MULE_LEADING_CODES(id, codes) \ | |
2577 | do { \ | |
2578 | if (id < 0xA0) \ | |
2579 | codes[0] = id, codes[1] = 0; \ | |
2580 | else if (id < 0xE0) \ | |
2581 | codes[0] = 0x9A, codes[1] = id; \ | |
2582 | else if (id < 0xF0) \ | |
2583 | codes[0] = 0x9B, codes[1] = id; \ | |
2584 | else if (id < 0xF5) \ | |
2585 | codes[0] = 0x9C, codes[1] = id; \ | |
2586 | else \ | |
2587 | codes[0] = 0x9D, codes[1] = id; \ | |
2588 | } while (0); | |
2589 | ||
aa72b389 | 2590 | |
df7492f9 | 2591 | static int |
971de7fb | 2592 | encode_coding_emacs_mule (struct coding_system *coding) |
df7492f9 KH |
2593 | { |
2594 | int multibytep = coding->dst_multibyte; | |
2595 | int *charbuf = coding->charbuf; | |
2596 | int *charbuf_end = charbuf + coding->charbuf_used; | |
2597 | unsigned char *dst = coding->destination + coding->produced; | |
2598 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
2599 | int safe_room = 8; | |
df7492f9 | 2600 | int produced_chars = 0; |
24a73b0a | 2601 | Lisp_Object attrs, charset_list; |
df7492f9 | 2602 | int c; |
ff0dacd7 | 2603 | int preferred_charset_id = -1; |
df7492f9 | 2604 | |
24a73b0a | 2605 | CODING_GET_INFO (coding, attrs, charset_list); |
eccb6815 KH |
2606 | if (! EQ (charset_list, Vemacs_mule_charset_list)) |
2607 | { | |
2608 | CODING_ATTR_CHARSET_LIST (attrs) | |
2609 | = charset_list = Vemacs_mule_charset_list; | |
2610 | } | |
df7492f9 KH |
2611 | |
2612 | while (charbuf < charbuf_end) | |
2613 | { | |
2614 | ASSURE_DESTINATION (safe_room); | |
2615 | c = *charbuf++; | |
ff0dacd7 KH |
2616 | |
2617 | if (c < 0) | |
2618 | { | |
2619 | /* Handle an annotation. */ | |
2620 | switch (*charbuf) | |
2621 | { | |
2622 | case CODING_ANNOTATE_COMPOSITION_MASK: | |
2623 | /* Not yet implemented. */ | |
2624 | break; | |
2625 | case CODING_ANNOTATE_CHARSET_MASK: | |
2626 | preferred_charset_id = charbuf[3]; | |
2627 | if (preferred_charset_id >= 0 | |
2628 | && NILP (Fmemq (make_number (preferred_charset_id), | |
2629 | charset_list))) | |
2630 | preferred_charset_id = -1; | |
2631 | break; | |
2632 | default: | |
2633 | abort (); | |
2634 | } | |
2635 | charbuf += -c - 1; | |
2636 | continue; | |
2637 | } | |
2638 | ||
df7492f9 KH |
2639 | if (ASCII_CHAR_P (c)) |
2640 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d KH |
2641 | else if (CHAR_BYTE8_P (c)) |
2642 | { | |
2643 | c = CHAR_TO_BYTE8 (c); | |
2644 | EMIT_ONE_BYTE (c); | |
2645 | } | |
df7492f9 | 2646 | else |
aa72b389 | 2647 | { |
df7492f9 KH |
2648 | struct charset *charset; |
2649 | unsigned code; | |
2650 | int dimension; | |
2651 | int emacs_mule_id; | |
2652 | unsigned char leading_codes[2]; | |
2653 | ||
ff0dacd7 KH |
2654 | if (preferred_charset_id >= 0) |
2655 | { | |
2656 | charset = CHARSET_FROM_ID (preferred_charset_id); | |
905ca9d2 KH |
2657 | if (CHAR_CHARSET_P (c, charset)) |
2658 | code = ENCODE_CHAR (charset, c); | |
2659 | else | |
2660 | charset = char_charset (c, charset_list, &code); | |
ff0dacd7 KH |
2661 | } |
2662 | else | |
2663 | charset = char_charset (c, charset_list, &code); | |
df7492f9 KH |
2664 | if (! charset) |
2665 | { | |
2666 | c = coding->default_char; | |
2667 | if (ASCII_CHAR_P (c)) | |
2668 | { | |
2669 | EMIT_ONE_ASCII_BYTE (c); | |
2670 | continue; | |
2671 | } | |
2672 | charset = char_charset (c, charset_list, &code); | |
2673 | } | |
2674 | dimension = CHARSET_DIMENSION (charset); | |
2675 | emacs_mule_id = CHARSET_EMACS_MULE_ID (charset); | |
2676 | EMACS_MULE_LEADING_CODES (emacs_mule_id, leading_codes); | |
2677 | EMIT_ONE_BYTE (leading_codes[0]); | |
2678 | if (leading_codes[1]) | |
2679 | EMIT_ONE_BYTE (leading_codes[1]); | |
2680 | if (dimension == 1) | |
1fa663f9 | 2681 | EMIT_ONE_BYTE (code | 0x80); |
aa72b389 | 2682 | else |
df7492f9 | 2683 | { |
1fa663f9 | 2684 | code |= 0x8080; |
df7492f9 KH |
2685 | EMIT_ONE_BYTE (code >> 8); |
2686 | EMIT_ONE_BYTE (code & 0xFF); | |
2687 | } | |
aa72b389 | 2688 | } |
aa72b389 | 2689 | } |
065e3595 | 2690 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
2691 | coding->produced_char += produced_chars; |
2692 | coding->produced = dst - coding->destination; | |
2693 | return 0; | |
aa72b389 | 2694 | } |
b73bfc1c | 2695 | |
4ed46869 | 2696 | \f |
df7492f9 | 2697 | /*** 7. ISO2022 handlers ***/ |
4ed46869 KH |
2698 | |
2699 | /* The following note describes the coding system ISO2022 briefly. | |
39787efd | 2700 | Since the intention of this note is to help understand the |
5a936b46 | 2701 | functions in this file, some parts are NOT ACCURATE or are OVERLY |
39787efd | 2702 | SIMPLIFIED. For thorough understanding, please refer to the |
5a936b46 | 2703 | original document of ISO2022. This is equivalent to the standard |
cfb43547 | 2704 | ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*). |
4ed46869 KH |
2705 | |
2706 | ISO2022 provides many mechanisms to encode several character sets | |
cfb43547 | 2707 | in 7-bit and 8-bit environments. For 7-bit environments, all text |
39787efd KH |
2708 | is encoded using bytes less than 128. This may make the encoded |
2709 | text a little bit longer, but the text passes more easily through | |
cfb43547 | 2710 | several types of gateway, some of which strip off the MSB (Most |
8ca3766a | 2711 | Significant Bit). |
b73bfc1c | 2712 | |
cfb43547 DL |
2713 | There are two kinds of character sets: control character sets and |
2714 | graphic character sets. The former contain control characters such | |
4ed46869 | 2715 | as `newline' and `escape' to provide control functions (control |
39787efd | 2716 | functions are also provided by escape sequences). The latter |
cfb43547 | 2717 | contain graphic characters such as 'A' and '-'. Emacs recognizes |
4ed46869 KH |
2718 | two control character sets and many graphic character sets. |
2719 | ||
2720 | Graphic character sets are classified into one of the following | |
39787efd KH |
2721 | four classes, according to the number of bytes (DIMENSION) and |
2722 | number of characters in one dimension (CHARS) of the set: | |
2723 | - DIMENSION1_CHARS94 | |
2724 | - DIMENSION1_CHARS96 | |
2725 | - DIMENSION2_CHARS94 | |
2726 | - DIMENSION2_CHARS96 | |
2727 | ||
2728 | In addition, each character set is assigned an identification tag, | |
cfb43547 | 2729 | unique for each set, called the "final character" (denoted as <F> |
39787efd KH |
2730 | hereafter). The <F> of each character set is decided by ECMA(*) |
2731 | when it is registered in ISO. The code range of <F> is 0x30..0x7F | |
2732 | (0x30..0x3F are for private use only). | |
4ed46869 KH |
2733 | |
2734 | Note (*): ECMA = European Computer Manufacturers Association | |
2735 | ||
cfb43547 | 2736 | Here are examples of graphic character sets [NAME(<F>)]: |
4ed46869 KH |
2737 | o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ... |
2738 | o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ... | |
2739 | o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ... | |
2740 | o DIMENSION2_CHARS96 -- none for the moment | |
2741 | ||
39787efd | 2742 | A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR. |
4ed46869 KH |
2743 | C0 [0x00..0x1F] -- control character plane 0 |
2744 | GL [0x20..0x7F] -- graphic character plane 0 | |
2745 | C1 [0x80..0x9F] -- control character plane 1 | |
2746 | GR [0xA0..0xFF] -- graphic character plane 1 | |
2747 | ||
2748 | A control character set is directly designated and invoked to C0 or | |
39787efd KH |
2749 | C1 by an escape sequence. The most common case is that: |
2750 | - ISO646's control character set is designated/invoked to C0, and | |
2751 | - ISO6429's control character set is designated/invoked to C1, | |
2752 | and usually these designations/invocations are omitted in encoded | |
2753 | text. In a 7-bit environment, only C0 can be used, and a control | |
2754 | character for C1 is encoded by an appropriate escape sequence to | |
2755 | fit into the environment. All control characters for C1 are | |
2756 | defined to have corresponding escape sequences. | |
4ed46869 KH |
2757 | |
2758 | A graphic character set is at first designated to one of four | |
2759 | graphic registers (G0 through G3), then these graphic registers are | |
2760 | invoked to GL or GR. These designations and invocations can be | |
2761 | done independently. The most common case is that G0 is invoked to | |
39787efd KH |
2762 | GL, G1 is invoked to GR, and ASCII is designated to G0. Usually |
2763 | these invocations and designations are omitted in encoded text. | |
2764 | In a 7-bit environment, only GL can be used. | |
4ed46869 | 2765 | |
39787efd KH |
2766 | When a graphic character set of CHARS94 is invoked to GL, codes |
2767 | 0x20 and 0x7F of the GL area work as control characters SPACE and | |
2768 | DEL respectively, and codes 0xA0 and 0xFF of the GR area should not | |
2769 | be used. | |
4ed46869 KH |
2770 | |
2771 | There are two ways of invocation: locking-shift and single-shift. | |
2772 | With locking-shift, the invocation lasts until the next different | |
39787efd KH |
2773 | invocation, whereas with single-shift, the invocation affects the |
2774 | following character only and doesn't affect the locking-shift | |
2775 | state. Invocations are done by the following control characters or | |
2776 | escape sequences: | |
4ed46869 KH |
2777 | |
2778 | ---------------------------------------------------------------------- | |
39787efd | 2779 | abbrev function cntrl escape seq description |
4ed46869 | 2780 | ---------------------------------------------------------------------- |
39787efd KH |
2781 | SI/LS0 (shift-in) 0x0F none invoke G0 into GL |
2782 | SO/LS1 (shift-out) 0x0E none invoke G1 into GL | |
2783 | LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL | |
2784 | LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL | |
2785 | LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*) | |
2786 | LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*) | |
2787 | LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*) | |
2788 | SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char | |
2789 | SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char | |
4ed46869 | 2790 | ---------------------------------------------------------------------- |
39787efd KH |
2791 | (*) These are not used by any known coding system. |
2792 | ||
2793 | Control characters for these functions are defined by macros | |
2794 | ISO_CODE_XXX in `coding.h'. | |
4ed46869 | 2795 | |
39787efd | 2796 | Designations are done by the following escape sequences: |
4ed46869 KH |
2797 | ---------------------------------------------------------------------- |
2798 | escape sequence description | |
2799 | ---------------------------------------------------------------------- | |
2800 | ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0 | |
2801 | ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1 | |
2802 | ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2 | |
2803 | ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3 | |
2804 | ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*) | |
2805 | ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1 | |
2806 | ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2 | |
2807 | ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3 | |
2808 | ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**) | |
2809 | ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1 | |
2810 | ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2 | |
2811 | ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3 | |
2812 | ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*) | |
2813 | ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1 | |
2814 | ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2 | |
2815 | ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3 | |
2816 | ---------------------------------------------------------------------- | |
2817 | ||
2818 | In this list, "DIMENSION1_CHARS94<F>" means a graphic character set | |
39787efd | 2819 | of dimension 1, chars 94, and final character <F>, etc... |
4ed46869 KH |
2820 | |
2821 | Note (*): Although these designations are not allowed in ISO2022, | |
2822 | Emacs accepts them on decoding, and produces them on encoding | |
39787efd | 2823 | CHARS96 character sets in a coding system which is characterized as |
4ed46869 KH |
2824 | 7-bit environment, non-locking-shift, and non-single-shift. |
2825 | ||
2826 | Note (**): If <F> is '@', 'A', or 'B', the intermediate character | |
df7492f9 | 2827 | '(' must be omitted. We refer to this as "short-form" hereafter. |
4ed46869 | 2828 | |
cfb43547 | 2829 | Now you may notice that there are a lot of ways of encoding the |
39787efd KH |
2830 | same multilingual text in ISO2022. Actually, there exist many |
2831 | coding systems such as Compound Text (used in X11's inter client | |
8ca3766a DL |
2832 | communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR |
2833 | (used in Korean Internet), EUC (Extended UNIX Code, used in Asian | |
4ed46869 KH |
2834 | localized platforms), and all of these are variants of ISO2022. |
2835 | ||
2836 | In addition to the above, Emacs handles two more kinds of escape | |
2837 | sequences: ISO6429's direction specification and Emacs' private | |
2838 | sequence for specifying character composition. | |
2839 | ||
39787efd | 2840 | ISO6429's direction specification takes the following form: |
4ed46869 KH |
2841 | o CSI ']' -- end of the current direction |
2842 | o CSI '0' ']' -- end of the current direction | |
2843 | o CSI '1' ']' -- start of left-to-right text | |
2844 | o CSI '2' ']' -- start of right-to-left text | |
2845 | The control character CSI (0x9B: control sequence introducer) is | |
39787efd KH |
2846 | abbreviated to the escape sequence ESC '[' in a 7-bit environment. |
2847 | ||
2848 | Character composition specification takes the following form: | |
ec6d2bb8 KH |
2849 | o ESC '0' -- start relative composition |
2850 | o ESC '1' -- end composition | |
2851 | o ESC '2' -- start rule-base composition (*) | |
2852 | o ESC '3' -- start relative composition with alternate chars (**) | |
2853 | o ESC '4' -- start rule-base composition with alternate chars (**) | |
b73bfc1c | 2854 | Since these are not standard escape sequences of any ISO standard, |
cfb43547 | 2855 | the use of them with these meanings is restricted to Emacs only. |
ec6d2bb8 | 2856 | |
5a936b46 DL |
2857 | (*) This form is used only in Emacs 20.7 and older versions, |
2858 | but newer versions can safely decode it. | |
cfb43547 | 2859 | (**) This form is used only in Emacs 21.1 and newer versions, |
5a936b46 | 2860 | and older versions can't decode it. |
ec6d2bb8 | 2861 | |
cfb43547 | 2862 | Here's a list of example usages of these composition escape |
b73bfc1c | 2863 | sequences (categorized by `enum composition_method'). |
ec6d2bb8 | 2864 | |
b73bfc1c | 2865 | COMPOSITION_RELATIVE: |
ec6d2bb8 | 2866 | ESC 0 CHAR [ CHAR ] ESC 1 |
8ca3766a | 2867 | COMPOSITION_WITH_RULE: |
ec6d2bb8 | 2868 | ESC 2 CHAR [ RULE CHAR ] ESC 1 |
b73bfc1c | 2869 | COMPOSITION_WITH_ALTCHARS: |
ec6d2bb8 | 2870 | ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 |
b73bfc1c | 2871 | COMPOSITION_WITH_RULE_ALTCHARS: |
ec6d2bb8 | 2872 | ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */ |
4ed46869 KH |
2873 | |
2874 | enum iso_code_class_type iso_code_class[256]; | |
2875 | ||
df7492f9 KH |
2876 | #define SAFE_CHARSET_P(coding, id) \ |
2877 | ((id) <= (coding)->max_charset_id \ | |
1b3b981b | 2878 | && (coding)->safe_charsets[id] != 255) |
df7492f9 | 2879 | |
df7492f9 | 2880 | static void |
971de7fb | 2881 | setup_iso_safe_charsets (Lisp_Object attrs) |
df7492f9 KH |
2882 | { |
2883 | Lisp_Object charset_list, safe_charsets; | |
2884 | Lisp_Object request; | |
2885 | Lisp_Object reg_usage; | |
2886 | Lisp_Object tail; | |
2887 | int reg94, reg96; | |
2888 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); | |
2889 | int max_charset_id; | |
2890 | ||
2891 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
2892 | if ((flags & CODING_ISO_FLAG_FULL_SUPPORT) | |
2893 | && ! EQ (charset_list, Viso_2022_charset_list)) | |
2894 | { | |
2895 | CODING_ATTR_CHARSET_LIST (attrs) | |
2896 | = charset_list = Viso_2022_charset_list; | |
2897 | ASET (attrs, coding_attr_safe_charsets, Qnil); | |
2898 | } | |
2899 | ||
2900 | if (STRINGP (AREF (attrs, coding_attr_safe_charsets))) | |
2901 | return; | |
2902 | ||
2903 | max_charset_id = 0; | |
2904 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) | |
2905 | { | |
2906 | int id = XINT (XCAR (tail)); | |
2907 | if (max_charset_id < id) | |
2908 | max_charset_id = id; | |
2909 | } | |
d46c5b12 | 2910 | |
1b3b981b AS |
2911 | safe_charsets = make_uninit_string (max_charset_id + 1); |
2912 | memset (SDATA (safe_charsets), 255, max_charset_id + 1); | |
df7492f9 KH |
2913 | request = AREF (attrs, coding_attr_iso_request); |
2914 | reg_usage = AREF (attrs, coding_attr_iso_usage); | |
2915 | reg94 = XINT (XCAR (reg_usage)); | |
2916 | reg96 = XINT (XCDR (reg_usage)); | |
2917 | ||
2918 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) | |
2919 | { | |
2920 | Lisp_Object id; | |
2921 | Lisp_Object reg; | |
2922 | struct charset *charset; | |
2923 | ||
2924 | id = XCAR (tail); | |
2925 | charset = CHARSET_FROM_ID (XINT (id)); | |
bf16eb23 | 2926 | reg = Fcdr (Fassq (id, request)); |
df7492f9 | 2927 | if (! NILP (reg)) |
8f924df7 | 2928 | SSET (safe_charsets, XINT (id), XINT (reg)); |
df7492f9 KH |
2929 | else if (charset->iso_chars_96) |
2930 | { | |
2931 | if (reg96 < 4) | |
8f924df7 | 2932 | SSET (safe_charsets, XINT (id), reg96); |
df7492f9 KH |
2933 | } |
2934 | else | |
2935 | { | |
2936 | if (reg94 < 4) | |
8f924df7 | 2937 | SSET (safe_charsets, XINT (id), reg94); |
df7492f9 KH |
2938 | } |
2939 | } | |
2940 | ASET (attrs, coding_attr_safe_charsets, safe_charsets); | |
2941 | } | |
d46c5b12 | 2942 | |
b6871cc7 | 2943 | |
4ed46869 | 2944 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". |
ad1746f5 | 2945 | Check if a text is encoded in one of ISO-2022 based coding systems. |
ff0dacd7 | 2946 | If it is, return 1, else return 0. */ |
4ed46869 | 2947 | |
0a28aafb | 2948 | static int |
cf84bb53 JB |
2949 | detect_coding_iso_2022 (struct coding_system *coding, |
2950 | struct coding_detection_info *detect_info) | |
4ed46869 | 2951 | { |
8f924df7 KH |
2952 | const unsigned char *src = coding->source, *src_base = src; |
2953 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
df7492f9 | 2954 | int multibytep = coding->src_multibyte; |
ff0dacd7 | 2955 | int single_shifting = 0; |
0e48bb22 | 2956 | int id; |
df7492f9 KH |
2957 | int c, c1; |
2958 | int consumed_chars = 0; | |
2959 | int i; | |
ff0dacd7 KH |
2960 | int rejected = 0; |
2961 | int found = 0; | |
cee53ed4 | 2962 | int composition_count = -1; |
ff0dacd7 KH |
2963 | |
2964 | detect_info->checked |= CATEGORY_MASK_ISO; | |
df7492f9 KH |
2965 | |
2966 | for (i = coding_category_iso_7; i <= coding_category_iso_8_else; i++) | |
2967 | { | |
2968 | struct coding_system *this = &(coding_categories[i]); | |
2969 | Lisp_Object attrs, val; | |
2970 | ||
c6b278e7 KH |
2971 | if (this->id < 0) |
2972 | continue; | |
df7492f9 KH |
2973 | attrs = CODING_ID_ATTRS (this->id); |
2974 | if (CODING_ISO_FLAGS (this) & CODING_ISO_FLAG_FULL_SUPPORT | |
1b3b981b | 2975 | && ! EQ (CODING_ATTR_CHARSET_LIST (attrs), Viso_2022_charset_list)) |
df7492f9 KH |
2976 | setup_iso_safe_charsets (attrs); |
2977 | val = CODING_ATTR_SAFE_CHARSETS (attrs); | |
8f924df7 | 2978 | this->max_charset_id = SCHARS (val) - 1; |
1b3b981b | 2979 | this->safe_charsets = SDATA (val); |
df7492f9 KH |
2980 | } |
2981 | ||
2982 | /* A coding system of this category is always ASCII compatible. */ | |
2983 | src += coding->head_ascii; | |
3f003981 | 2984 | |
ff0dacd7 | 2985 | while (rejected != CATEGORY_MASK_ISO) |
4ed46869 | 2986 | { |
065e3595 | 2987 | src_base = src; |
df7492f9 | 2988 | ONE_MORE_BYTE (c); |
4ed46869 KH |
2989 | switch (c) |
2990 | { | |
2991 | case ISO_CODE_ESC: | |
74383408 KH |
2992 | if (inhibit_iso_escape_detection) |
2993 | break; | |
f46869e4 | 2994 | single_shifting = 0; |
df7492f9 | 2995 | ONE_MORE_BYTE (c); |
0e48bb22 | 2996 | if (c == 'N' || c == 'O') |
d46c5b12 | 2997 | { |
ae9ff118 | 2998 | /* ESC <Fe> for SS2 or SS3. */ |
ff0dacd7 KH |
2999 | single_shifting = 1; |
3000 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_8BIT; | |
4ed46869 | 3001 | } |
cee53ed4 KH |
3002 | else if (c == '1') |
3003 | { | |
3004 | /* End of composition. */ | |
3005 | if (composition_count < 0 | |
3006 | || composition_count > MAX_COMPOSITION_COMPONENTS) | |
3007 | /* Invalid */ | |
3008 | break; | |
3009 | composition_count = -1; | |
3010 | found |= CATEGORY_MASK_ISO; | |
3011 | } | |
ec6d2bb8 KH |
3012 | else if (c >= '0' && c <= '4') |
3013 | { | |
3014 | /* ESC <Fp> for start/end composition. */ | |
cee53ed4 | 3015 | composition_count = 0; |
ec6d2bb8 | 3016 | } |
bf9cdd4e | 3017 | else |
df7492f9 | 3018 | { |
0e48bb22 AS |
3019 | if (c >= '(' && c <= '/') |
3020 | { | |
3021 | /* Designation sequence for a charset of dimension 1. */ | |
3022 | ONE_MORE_BYTE (c1); | |
3023 | if (c1 < ' ' || c1 >= 0x80 | |
3024 | || (id = iso_charset_table[0][c >= ','][c1]) < 0) | |
3025 | /* Invalid designation sequence. Just ignore. */ | |
3026 | break; | |
3027 | } | |
3028 | else if (c == '$') | |
3029 | { | |
3030 | /* Designation sequence for a charset of dimension 2. */ | |
3031 | ONE_MORE_BYTE (c); | |
3032 | if (c >= '@' && c <= 'B') | |
3033 | /* Designation for JISX0208.1978, GB2312, or JISX0208. */ | |
3034 | id = iso_charset_table[1][0][c]; | |
3035 | else if (c >= '(' && c <= '/') | |
3036 | { | |
3037 | ONE_MORE_BYTE (c1); | |
3038 | if (c1 < ' ' || c1 >= 0x80 | |
3039 | || (id = iso_charset_table[1][c >= ','][c1]) < 0) | |
3040 | /* Invalid designation sequence. Just ignore. */ | |
3041 | break; | |
3042 | } | |
3043 | else | |
3044 | /* Invalid designation sequence. Just ignore it. */ | |
3045 | break; | |
3046 | } | |
3047 | else | |
3048 | { | |
3049 | /* Invalid escape sequence. Just ignore it. */ | |
3050 | break; | |
3051 | } | |
d46c5b12 | 3052 | |
0e48bb22 AS |
3053 | /* We found a valid designation sequence for CHARSET. */ |
3054 | rejected |= CATEGORY_MASK_ISO_8BIT; | |
3055 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7], | |
3056 | id)) | |
3057 | found |= CATEGORY_MASK_ISO_7; | |
3058 | else | |
3059 | rejected |= CATEGORY_MASK_ISO_7; | |
3060 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7_tight], | |
3061 | id)) | |
3062 | found |= CATEGORY_MASK_ISO_7_TIGHT; | |
3063 | else | |
3064 | rejected |= CATEGORY_MASK_ISO_7_TIGHT; | |
3065 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_7_else], | |
3066 | id)) | |
3067 | found |= CATEGORY_MASK_ISO_7_ELSE; | |
3068 | else | |
3069 | rejected |= CATEGORY_MASK_ISO_7_ELSE; | |
3070 | if (SAFE_CHARSET_P (&coding_categories[coding_category_iso_8_else], | |
3071 | id)) | |
3072 | found |= CATEGORY_MASK_ISO_8_ELSE; | |
3073 | else | |
3074 | rejected |= CATEGORY_MASK_ISO_8_ELSE; | |
3075 | } | |
4ed46869 KH |
3076 | break; |
3077 | ||
4ed46869 | 3078 | case ISO_CODE_SO: |
d46c5b12 | 3079 | case ISO_CODE_SI: |
ff0dacd7 | 3080 | /* Locking shift out/in. */ |
74383408 KH |
3081 | if (inhibit_iso_escape_detection) |
3082 | break; | |
f46869e4 | 3083 | single_shifting = 0; |
ff0dacd7 | 3084 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_8BIT; |
d46c5b12 KH |
3085 | break; |
3086 | ||
4ed46869 | 3087 | case ISO_CODE_CSI: |
ff0dacd7 | 3088 | /* Control sequence introducer. */ |
f46869e4 | 3089 | single_shifting = 0; |
ff0dacd7 KH |
3090 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE; |
3091 | found |= CATEGORY_MASK_ISO_8_ELSE; | |
3092 | goto check_extra_latin; | |
3093 | ||
4ed46869 KH |
3094 | case ISO_CODE_SS2: |
3095 | case ISO_CODE_SS3: | |
ff0dacd7 KH |
3096 | /* Single shift. */ |
3097 | if (inhibit_iso_escape_detection) | |
3098 | break; | |
75e2a253 | 3099 | single_shifting = 0; |
ff0dacd7 KH |
3100 | rejected |= CATEGORY_MASK_ISO_7BIT; |
3101 | if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1]) | |
3102 | & CODING_ISO_FLAG_SINGLE_SHIFT) | |
0e48bb22 AS |
3103 | { |
3104 | found |= CATEGORY_MASK_ISO_8_1; | |
3105 | single_shifting = 1; | |
3106 | } | |
ff0dacd7 KH |
3107 | if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_2]) |
3108 | & CODING_ISO_FLAG_SINGLE_SHIFT) | |
0e48bb22 AS |
3109 | { |
3110 | found |= CATEGORY_MASK_ISO_8_2; | |
3111 | single_shifting = 1; | |
3112 | } | |
75e2a253 KH |
3113 | if (single_shifting) |
3114 | break; | |
0e48bb22 AS |
3115 | check_extra_latin: |
3116 | if (! VECTORP (Vlatin_extra_code_table) | |
3117 | || NILP (XVECTOR (Vlatin_extra_code_table)->contents[c])) | |
3118 | { | |
3119 | rejected = CATEGORY_MASK_ISO; | |
3120 | break; | |
3121 | } | |
3122 | if (CODING_ISO_FLAGS (&coding_categories[coding_category_iso_8_1]) | |
3123 | & CODING_ISO_FLAG_LATIN_EXTRA) | |
3124 | found |= CATEGORY_MASK_ISO_8_1; | |
3125 | else | |
3126 | rejected |= CATEGORY_MASK_ISO_8_1; | |
3127 | rejected |= CATEGORY_MASK_ISO_8_2; | |
3128 | break; | |
4ed46869 KH |
3129 | |
3130 | default: | |
065e3595 KH |
3131 | if (c < 0) |
3132 | continue; | |
4ed46869 | 3133 | if (c < 0x80) |
f46869e4 | 3134 | { |
cee53ed4 KH |
3135 | if (composition_count >= 0) |
3136 | composition_count++; | |
f46869e4 KH |
3137 | single_shifting = 0; |
3138 | break; | |
3139 | } | |
ff0dacd7 | 3140 | if (c >= 0xA0) |
c4825358 | 3141 | { |
ff0dacd7 KH |
3142 | rejected |= CATEGORY_MASK_ISO_7BIT | CATEGORY_MASK_ISO_7_ELSE; |
3143 | found |= CATEGORY_MASK_ISO_8_1; | |
f46869e4 | 3144 | /* Check the length of succeeding codes of the range |
ff0dacd7 KH |
3145 | 0xA0..0FF. If the byte length is even, we include |
3146 | CATEGORY_MASK_ISO_8_2 in `found'. We can check this | |
3147 | only when we are not single shifting. */ | |
3148 | if (! single_shifting | |
3149 | && ! (rejected & CATEGORY_MASK_ISO_8_2)) | |
f46869e4 | 3150 | { |
2735d060 | 3151 | int len = 1; |
b73bfc1c KH |
3152 | while (src < src_end) |
3153 | { | |
d12bd917 | 3154 | src_base = src; |
df7492f9 | 3155 | ONE_MORE_BYTE (c); |
b73bfc1c | 3156 | if (c < 0xA0) |
d12bd917 KH |
3157 | { |
3158 | src = src_base; | |
3159 | break; | |
3160 | } | |
2735d060 | 3161 | len++; |
b73bfc1c KH |
3162 | } |
3163 | ||
2735d060 | 3164 | if (len & 1 && src < src_end) |
cee53ed4 KH |
3165 | { |
3166 | rejected |= CATEGORY_MASK_ISO_8_2; | |
3167 | if (composition_count >= 0) | |
2735d060 | 3168 | composition_count += len; |
cee53ed4 | 3169 | } |
f46869e4 | 3170 | else |
cee53ed4 KH |
3171 | { |
3172 | found |= CATEGORY_MASK_ISO_8_2; | |
3173 | if (composition_count >= 0) | |
2735d060 | 3174 | composition_count += len / 2; |
cee53ed4 | 3175 | } |
f46869e4 | 3176 | } |
ff0dacd7 | 3177 | break; |
4ed46869 | 3178 | } |
4ed46869 KH |
3179 | } |
3180 | } | |
ff0dacd7 KH |
3181 | detect_info->rejected |= CATEGORY_MASK_ISO; |
3182 | return 0; | |
4ed46869 | 3183 | |
df7492f9 | 3184 | no_more_source: |
ff0dacd7 KH |
3185 | detect_info->rejected |= rejected; |
3186 | detect_info->found |= (found & ~rejected); | |
df7492f9 | 3187 | return 1; |
4ed46869 | 3188 | } |
ec6d2bb8 | 3189 | |
4ed46869 | 3190 | |
134b9549 KH |
3191 | /* Set designation state into CODING. Set CHARS_96 to -1 if the |
3192 | escape sequence should be kept. */ | |
df7492f9 KH |
3193 | #define DECODE_DESIGNATION(reg, dim, chars_96, final) \ |
3194 | do { \ | |
3195 | int id, prev; \ | |
3196 | \ | |
3197 | if (final < '0' || final >= 128 \ | |
3198 | || ((id = ISO_CHARSET_TABLE (dim, chars_96, final)) < 0) \ | |
3199 | || !SAFE_CHARSET_P (coding, id)) \ | |
3200 | { \ | |
3201 | CODING_ISO_DESIGNATION (coding, reg) = -2; \ | |
134b9549 KH |
3202 | chars_96 = -1; \ |
3203 | break; \ | |
df7492f9 KH |
3204 | } \ |
3205 | prev = CODING_ISO_DESIGNATION (coding, reg); \ | |
bf16eb23 KH |
3206 | if (id == charset_jisx0201_roman) \ |
3207 | { \ | |
3208 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \ | |
3209 | id = charset_ascii; \ | |
3210 | } \ | |
3211 | else if (id == charset_jisx0208_1978) \ | |
3212 | { \ | |
3213 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \ | |
3214 | id = charset_jisx0208; \ | |
3215 | } \ | |
df7492f9 KH |
3216 | CODING_ISO_DESIGNATION (coding, reg) = id; \ |
3217 | /* If there was an invalid designation to REG previously, and this \ | |
3218 | designation is ASCII to REG, we should keep this designation \ | |
3219 | sequence. */ \ | |
3220 | if (prev == -2 && id == charset_ascii) \ | |
134b9549 | 3221 | chars_96 = -1; \ |
4ed46869 KH |
3222 | } while (0) |
3223 | ||
d46c5b12 | 3224 | |
e951386e KH |
3225 | /* Handle these composition sequence (ALT: alternate char): |
3226 | ||
3227 | (1) relative composition: ESC 0 CHAR ... ESC 1 | |
3228 | (2) rulebase composition: ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1 | |
3229 | (3) altchar composition: ESC 3 ALT ... ALT ESC 0 CHAR ... ESC 1 | |
3230 | (4) alt&rule composition: ESC 4 ALT RULE ... ALT ESC 0 CHAR ... ESC 1 | |
3231 | ||
3232 | When the start sequence (ESC 0/2/3/4) is found, this annotation | |
3233 | header is produced. | |
3234 | ||
3235 | [ -LENGTH(==-5) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) 0 METHOD ] | |
3236 | ||
3237 | Then, upon reading CHAR or RULE (one or two bytes), these codes are | |
3238 | produced until the end sequence (ESC 1) is found: | |
3239 | ||
3240 | (1) CHAR ... CHAR | |
3241 | (2) CHAR -2 DECODED-RULE CHAR -2 DECODED-RULE ... CHAR | |
3242 | (3) ALT ... ALT -1 -1 CHAR ... CHAR | |
3243 | (4) ALT -2 DECODED-RULE ALT -2 DECODED-RULE ... ALT -1 -1 CHAR ... CHAR | |
3244 | ||
3245 | When the end sequence (ESC 1) is found, LENGTH and NCHARS in the | |
3246 | annotation header is updated as below: | |
3247 | ||
3248 | (1) LENGTH: unchanged, NCHARS: number of CHARs | |
3249 | (2) LENGTH: unchanged, NCHARS: number of CHARs | |
3250 | (3) LENGTH: += number of ALTs + 2, NCHARS: number of CHARs | |
3251 | (4) LENGTH: += number of ALTs * 3, NCHARS: number of CHARs | |
3252 | ||
3253 | If an error is found while composing, the annotation header is | |
3254 | changed to: | |
3255 | ||
3256 | [ ESC '0'/'2'/'3'/'4' -2 0 ] | |
3257 | ||
3258 | and the sequence [ -2 DECODED-RULE ] is changed to the original | |
3259 | byte sequence as below: | |
3260 | o the original byte sequence is B: [ B -1 ] | |
3261 | o the original byte sequence is B1 B2: [ B1 B2 ] | |
3262 | and the sequence [ -1 -1 ] is changed to the original byte | |
3263 | sequence: | |
3264 | [ ESC '0' ] | |
3265 | */ | |
3266 | ||
3267 | /* Decode a composition rule C1 and maybe one more byte from the | |
3268 | source, and set RULE to the encoded composition rule, NBYTES to the | |
3269 | length of the composition rule. If the rule is invalid, set RULE | |
3270 | to some negative value. */ | |
3271 | ||
3272 | #define DECODE_COMPOSITION_RULE(rule, nbytes) \ | |
3273 | do { \ | |
3274 | rule = c1 - 32; \ | |
3275 | if (rule < 0) \ | |
3276 | break; \ | |
3277 | if (rule < 81) /* old format (before ver.21) */ \ | |
3278 | { \ | |
3279 | int gref = (rule) / 9; \ | |
3280 | int nref = (rule) % 9; \ | |
3281 | if (gref == 4) gref = 10; \ | |
3282 | if (nref == 4) nref = 10; \ | |
3283 | rule = COMPOSITION_ENCODE_RULE (gref, nref); \ | |
3284 | nbytes = 1; \ | |
3285 | } \ | |
3286 | else /* new format (after ver.21) */ \ | |
3287 | { \ | |
2735d060 | 3288 | int b; \ |
e951386e | 3289 | \ |
2735d060 PE |
3290 | ONE_MORE_BYTE (b); \ |
3291 | rule = COMPOSITION_ENCODE_RULE (rule - 81, b - 32); \ | |
e951386e KH |
3292 | if (rule >= 0) \ |
3293 | rule += 0x100; /* to destinguish it from the old format */ \ | |
3294 | nbytes = 2; \ | |
3295 | } \ | |
3296 | } while (0) | |
3297 | ||
3298 | #define ENCODE_COMPOSITION_RULE(rule) \ | |
df7492f9 | 3299 | do { \ |
e951386e KH |
3300 | int gref = (rule % 0x100) / 12, nref = (rule % 0x100) % 12; \ |
3301 | \ | |
3302 | if (rule < 0x100) /* old format */ \ | |
df7492f9 | 3303 | { \ |
e951386e KH |
3304 | if (gref == 10) gref = 4; \ |
3305 | if (nref == 10) nref = 4; \ | |
3306 | charbuf[idx] = 32 + gref * 9 + nref; \ | |
3307 | charbuf[idx + 1] = -1; \ | |
3308 | new_chars++; \ | |
df7492f9 | 3309 | } \ |
e951386e | 3310 | else /* new format */ \ |
df7492f9 | 3311 | { \ |
e951386e KH |
3312 | charbuf[idx] = 32 + 81 + gref; \ |
3313 | charbuf[idx + 1] = 32 + nref; \ | |
3314 | new_chars += 2; \ | |
df7492f9 KH |
3315 | } \ |
3316 | } while (0) | |
3317 | ||
e951386e KH |
3318 | /* Finish the current composition as invalid. */ |
3319 | ||
f57e2426 | 3320 | static int finish_composition (int *, struct composition_status *); |
e951386e KH |
3321 | |
3322 | static int | |
971de7fb | 3323 | finish_composition (int *charbuf, struct composition_status *cmp_status) |
e951386e KH |
3324 | { |
3325 | int idx = - cmp_status->length; | |
3326 | int new_chars; | |
3327 | ||
3328 | /* Recover the original ESC sequence */ | |
3329 | charbuf[idx++] = ISO_CODE_ESC; | |
3330 | charbuf[idx++] = (cmp_status->method == COMPOSITION_RELATIVE ? '0' | |
3331 | : cmp_status->method == COMPOSITION_WITH_RULE ? '2' | |
3332 | : cmp_status->method == COMPOSITION_WITH_ALTCHARS ? '3' | |
3333 | /* cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS */ | |
3334 | : '4'); | |
3335 | charbuf[idx++] = -2; | |
3336 | charbuf[idx++] = 0; | |
3337 | charbuf[idx++] = -1; | |
3338 | new_chars = cmp_status->nchars; | |
3339 | if (cmp_status->method >= COMPOSITION_WITH_RULE) | |
3340 | for (; idx < 0; idx++) | |
3341 | { | |
3342 | int elt = charbuf[idx]; | |
3343 | ||
3344 | if (elt == -2) | |
3345 | { | |
3346 | ENCODE_COMPOSITION_RULE (charbuf[idx + 1]); | |
3347 | idx++; | |
3348 | } | |
3349 | else if (elt == -1) | |
3350 | { | |
3351 | charbuf[idx++] = ISO_CODE_ESC; | |
3352 | charbuf[idx] = '0'; | |
3353 | new_chars += 2; | |
3354 | } | |
3355 | } | |
3356 | cmp_status->state = COMPOSING_NO; | |
3357 | return new_chars; | |
3358 | } | |
3359 | ||
ad1746f5 | 3360 | /* If characters are under composition, finish the composition. */ |
e951386e KH |
3361 | #define MAYBE_FINISH_COMPOSITION() \ |
3362 | do { \ | |
3363 | if (cmp_status->state != COMPOSING_NO) \ | |
3364 | char_offset += finish_composition (charbuf, cmp_status); \ | |
3365 | } while (0) | |
d46c5b12 | 3366 | |
aa72b389 | 3367 | /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4. |
e951386e | 3368 | |
aa72b389 KH |
3369 | ESC 0 : relative composition : ESC 0 CHAR ... ESC 1 |
3370 | ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1 | |
df7492f9 KH |
3371 | ESC 3 : altchar composition : ESC 3 CHAR ... ESC 0 CHAR ... ESC 1 |
3372 | ESC 4 : alt&rule composition : ESC 4 CHAR RULE ... CHAR ESC 0 CHAR ... ESC 1 | |
ec6d2bb8 | 3373 | |
e951386e KH |
3374 | Produce this annotation sequence now: |
3375 | ||
3376 | [ -LENGTH(==-4) CODING_ANNOTATE_COMPOSITION_MASK NCHARS(==0) METHOD ] | |
3377 | */ | |
3378 | ||
3379 | #define DECODE_COMPOSITION_START(c1) \ | |
3380 | do { \ | |
3381 | if (c1 == '0' \ | |
3382 | && ((cmp_status->state == COMPOSING_COMPONENT_CHAR \ | |
3383 | && cmp_status->method == COMPOSITION_WITH_ALTCHARS) \ | |
3384 | || (cmp_status->state == COMPOSING_COMPONENT_RULE \ | |
3385 | && cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS))) \ | |
3386 | { \ | |
3387 | *charbuf++ = -1; \ | |
3388 | *charbuf++= -1; \ | |
3389 | cmp_status->state = COMPOSING_CHAR; \ | |
3390 | cmp_status->length += 2; \ | |
3391 | } \ | |
3392 | else \ | |
3393 | { \ | |
3394 | MAYBE_FINISH_COMPOSITION (); \ | |
3395 | cmp_status->method = (c1 == '0' ? COMPOSITION_RELATIVE \ | |
3396 | : c1 == '2' ? COMPOSITION_WITH_RULE \ | |
3397 | : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \ | |
3398 | : COMPOSITION_WITH_RULE_ALTCHARS); \ | |
3399 | cmp_status->state \ | |
3400 | = (c1 <= '2' ? COMPOSING_CHAR : COMPOSING_COMPONENT_CHAR); \ | |
3401 | ADD_COMPOSITION_DATA (charbuf, 0, 0, cmp_status->method); \ | |
3402 | cmp_status->length = MAX_ANNOTATION_LENGTH; \ | |
3403 | cmp_status->nchars = cmp_status->ncomps = 0; \ | |
3404 | coding->annotated = 1; \ | |
3405 | } \ | |
ec6d2bb8 KH |
3406 | } while (0) |
3407 | ||
ec6d2bb8 | 3408 | |
e951386e | 3409 | /* Handle composition end sequence ESC 1. */ |
df7492f9 KH |
3410 | |
3411 | #define DECODE_COMPOSITION_END() \ | |
ec6d2bb8 | 3412 | do { \ |
e951386e KH |
3413 | if (cmp_status->nchars == 0 \ |
3414 | || ((cmp_status->state == COMPOSING_CHAR) \ | |
3415 | == (cmp_status->method == COMPOSITION_WITH_RULE))) \ | |
ec6d2bb8 | 3416 | { \ |
e951386e KH |
3417 | MAYBE_FINISH_COMPOSITION (); \ |
3418 | goto invalid_code; \ | |
ec6d2bb8 | 3419 | } \ |
e951386e KH |
3420 | if (cmp_status->method == COMPOSITION_WITH_ALTCHARS) \ |
3421 | charbuf[- cmp_status->length] -= cmp_status->ncomps + 2; \ | |
3422 | else if (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS) \ | |
3423 | charbuf[- cmp_status->length] -= cmp_status->ncomps * 3; \ | |
3424 | charbuf[- cmp_status->length + 2] = cmp_status->nchars; \ | |
3425 | char_offset += cmp_status->nchars; \ | |
3426 | cmp_status->state = COMPOSING_NO; \ | |
ec6d2bb8 KH |
3427 | } while (0) |
3428 | ||
e951386e | 3429 | /* Store a composition rule RULE in charbuf, and update cmp_status. */ |
df7492f9 | 3430 | |
e951386e KH |
3431 | #define STORE_COMPOSITION_RULE(rule) \ |
3432 | do { \ | |
3433 | *charbuf++ = -2; \ | |
3434 | *charbuf++ = rule; \ | |
3435 | cmp_status->length += 2; \ | |
3436 | cmp_status->state--; \ | |
3437 | } while (0) | |
ec6d2bb8 | 3438 | |
e951386e KH |
3439 | /* Store a composed char or a component char C in charbuf, and update |
3440 | cmp_status. */ | |
3441 | ||
3442 | #define STORE_COMPOSITION_CHAR(c) \ | |
ec6d2bb8 | 3443 | do { \ |
e951386e KH |
3444 | *charbuf++ = (c); \ |
3445 | cmp_status->length++; \ | |
3446 | if (cmp_status->state == COMPOSING_CHAR) \ | |
3447 | cmp_status->nchars++; \ | |
df7492f9 | 3448 | else \ |
e951386e KH |
3449 | cmp_status->ncomps++; \ |
3450 | if (cmp_status->method == COMPOSITION_WITH_RULE \ | |
3451 | || (cmp_status->method == COMPOSITION_WITH_RULE_ALTCHARS \ | |
3452 | && cmp_status->state == COMPOSING_COMPONENT_CHAR)) \ | |
3453 | cmp_status->state++; \ | |
ec6d2bb8 | 3454 | } while (0) |
88993dfd | 3455 | |
d46c5b12 | 3456 | |
4ed46869 KH |
3457 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ |
3458 | ||
b73bfc1c | 3459 | static void |
971de7fb | 3460 | decode_coding_iso_2022 (struct coding_system *coding) |
4ed46869 | 3461 | { |
8f924df7 KH |
3462 | const unsigned char *src = coding->source + coding->consumed; |
3463 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
3464 | const unsigned char *src_base; | |
69a80ea3 | 3465 | int *charbuf = coding->charbuf + coding->charbuf_used; |
ad1746f5 KH |
3466 | /* We may produce two annotations (charset and composition) in one |
3467 | loop and one more charset annotation at the end. */ | |
ff0dacd7 | 3468 | int *charbuf_end |
df80c7f0 | 3469 | = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 3); |
df7492f9 | 3470 | int consumed_chars = 0, consumed_chars_base; |
df7492f9 | 3471 | int multibytep = coding->src_multibyte; |
4ed46869 | 3472 | /* Charsets invoked to graphic plane 0 and 1 respectively. */ |
df7492f9 KH |
3473 | int charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); |
3474 | int charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1); | |
134b9549 | 3475 | int charset_id_2, charset_id_3; |
df7492f9 KH |
3476 | struct charset *charset; |
3477 | int c; | |
e951386e | 3478 | struct composition_status *cmp_status = CODING_ISO_CMP_STATUS (coding); |
24a73b0a | 3479 | Lisp_Object attrs, charset_list; |
ff0dacd7 KH |
3480 | int char_offset = coding->produced_char; |
3481 | int last_offset = char_offset; | |
3482 | int last_id = charset_ascii; | |
2735d060 | 3483 | int eol_dos = |
0a9564cb | 3484 | !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); |
119852e7 | 3485 | int byte_after_cr = -1; |
e951386e | 3486 | int i; |
df7492f9 | 3487 | |
24a73b0a | 3488 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 | 3489 | setup_iso_safe_charsets (attrs); |
287c57d7 KH |
3490 | /* Charset list may have been changed. */ |
3491 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
1b3b981b | 3492 | coding->safe_charsets = SDATA (CODING_ATTR_SAFE_CHARSETS (attrs)); |
b73bfc1c | 3493 | |
e951386e KH |
3494 | if (cmp_status->state != COMPOSING_NO) |
3495 | { | |
3496 | for (i = 0; i < cmp_status->length; i++) | |
3497 | *charbuf++ = cmp_status->carryover[i]; | |
3498 | coding->annotated = 1; | |
3499 | } | |
3500 | ||
b73bfc1c | 3501 | while (1) |
4ed46869 | 3502 | { |
cf299835 | 3503 | int c1, c2, c3; |
b73bfc1c KH |
3504 | |
3505 | src_base = src; | |
df7492f9 KH |
3506 | consumed_chars_base = consumed_chars; |
3507 | ||
3508 | if (charbuf >= charbuf_end) | |
b71f6f73 KH |
3509 | { |
3510 | if (byte_after_cr >= 0) | |
3511 | src_base--; | |
3512 | break; | |
3513 | } | |
df7492f9 | 3514 | |
119852e7 KH |
3515 | if (byte_after_cr >= 0) |
3516 | c1 = byte_after_cr, byte_after_cr = -1; | |
3517 | else | |
3518 | ONE_MORE_BYTE (c1); | |
065e3595 KH |
3519 | if (c1 < 0) |
3520 | goto invalid_code; | |
4ed46869 | 3521 | |
e951386e | 3522 | if (CODING_ISO_EXTSEGMENT_LEN (coding) > 0) |
4ed46869 | 3523 | { |
e951386e KH |
3524 | *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1); |
3525 | char_offset++; | |
3526 | CODING_ISO_EXTSEGMENT_LEN (coding)--; | |
3527 | continue; | |
3528 | } | |
3529 | ||
3530 | if (CODING_ISO_EMBEDDED_UTF_8 (coding)) | |
3531 | { | |
3532 | if (c1 == ISO_CODE_ESC) | |
ec6d2bb8 | 3533 | { |
e951386e KH |
3534 | if (src + 1 >= src_end) |
3535 | goto no_more_source; | |
3536 | *charbuf++ = ISO_CODE_ESC; | |
3537 | char_offset++; | |
3538 | if (src[0] == '%' && src[1] == '@') | |
df7492f9 | 3539 | { |
e951386e KH |
3540 | src += 2; |
3541 | consumed_chars += 2; | |
3542 | char_offset += 2; | |
3543 | /* We are sure charbuf can contain two more chars. */ | |
3544 | *charbuf++ = '%'; | |
3545 | *charbuf++ = '@'; | |
3546 | CODING_ISO_EMBEDDED_UTF_8 (coding) = 0; | |
df7492f9 | 3547 | } |
4ed46869 | 3548 | } |
e951386e KH |
3549 | else |
3550 | { | |
3551 | *charbuf++ = ASCII_BYTE_P (c1) ? c1 : BYTE8_TO_CHAR (c1); | |
3552 | char_offset++; | |
3553 | } | |
3554 | continue; | |
3555 | } | |
3556 | ||
3557 | if ((cmp_status->state == COMPOSING_RULE | |
3558 | || cmp_status->state == COMPOSING_COMPONENT_RULE) | |
3559 | && c1 != ISO_CODE_ESC) | |
3560 | { | |
3561 | int rule, nbytes; | |
3562 | ||
3563 | DECODE_COMPOSITION_RULE (rule, nbytes); | |
3564 | if (rule < 0) | |
3565 | goto invalid_code; | |
3566 | STORE_COMPOSITION_RULE (rule); | |
3567 | continue; | |
3568 | } | |
3569 | ||
3570 | /* We produce at most one character. */ | |
3571 | switch (iso_code_class [c1]) | |
3572 | { | |
3573 | case ISO_0x20_or_0x7F: | |
df7492f9 KH |
3574 | if (charset_id_0 < 0 |
3575 | || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_0))) | |
781d7a48 KH |
3576 | /* This is SPACE or DEL. */ |
3577 | charset = CHARSET_FROM_ID (charset_ascii); | |
3578 | else | |
3579 | charset = CHARSET_FROM_ID (charset_id_0); | |
3580 | break; | |
4ed46869 KH |
3581 | |
3582 | case ISO_graphic_plane_0: | |
134b9549 KH |
3583 | if (charset_id_0 < 0) |
3584 | charset = CHARSET_FROM_ID (charset_ascii); | |
3585 | else | |
3586 | charset = CHARSET_FROM_ID (charset_id_0); | |
4ed46869 KH |
3587 | break; |
3588 | ||
3589 | case ISO_0xA0_or_0xFF: | |
df7492f9 KH |
3590 | if (charset_id_1 < 0 |
3591 | || ! CHARSET_ISO_CHARS_96 (CHARSET_FROM_ID (charset_id_1)) | |
3592 | || CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) | |
3593 | goto invalid_code; | |
4ed46869 KH |
3594 | /* This is a graphic character, we fall down ... */ |
3595 | ||
3596 | case ISO_graphic_plane_1: | |
df7492f9 KH |
3597 | if (charset_id_1 < 0) |
3598 | goto invalid_code; | |
3599 | charset = CHARSET_FROM_ID (charset_id_1); | |
4ed46869 KH |
3600 | break; |
3601 | ||
df7492f9 | 3602 | case ISO_control_0: |
2735d060 | 3603 | if (eol_dos && c1 == '\r') |
119852e7 | 3604 | ONE_MORE_BYTE (byte_after_cr); |
df7492f9 KH |
3605 | MAYBE_FINISH_COMPOSITION (); |
3606 | charset = CHARSET_FROM_ID (charset_ascii); | |
4ed46869 KH |
3607 | break; |
3608 | ||
df7492f9 | 3609 | case ISO_control_1: |
df7492f9 KH |
3610 | goto invalid_code; |
3611 | ||
4ed46869 | 3612 | case ISO_shift_out: |
df7492f9 KH |
3613 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT) |
3614 | || CODING_ISO_DESIGNATION (coding, 1) < 0) | |
3615 | goto invalid_code; | |
3616 | CODING_ISO_INVOCATION (coding, 0) = 1; | |
3617 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3618 | continue; |
4ed46869 KH |
3619 | |
3620 | case ISO_shift_in: | |
df7492f9 KH |
3621 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT)) |
3622 | goto invalid_code; | |
3623 | CODING_ISO_INVOCATION (coding, 0) = 0; | |
3624 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3625 | continue; |
4ed46869 KH |
3626 | |
3627 | case ISO_single_shift_2_7: | |
a63dba42 KH |
3628 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS)) |
3629 | goto invalid_code; | |
4ed46869 | 3630 | case ISO_single_shift_2: |
df7492f9 KH |
3631 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)) |
3632 | goto invalid_code; | |
4ed46869 KH |
3633 | /* SS2 is handled as an escape sequence of ESC 'N' */ |
3634 | c1 = 'N'; | |
3635 | goto label_escape_sequence; | |
3636 | ||
3637 | case ISO_single_shift_3: | |
df7492f9 KH |
3638 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT)) |
3639 | goto invalid_code; | |
4ed46869 KH |
3640 | /* SS2 is handled as an escape sequence of ESC 'O' */ |
3641 | c1 = 'O'; | |
3642 | goto label_escape_sequence; | |
3643 | ||
3644 | case ISO_control_sequence_introducer: | |
3645 | /* CSI is handled as an escape sequence of ESC '[' ... */ | |
3646 | c1 = '['; | |
3647 | goto label_escape_sequence; | |
3648 | ||
3649 | case ISO_escape: | |
3650 | ONE_MORE_BYTE (c1); | |
3651 | label_escape_sequence: | |
df7492f9 | 3652 | /* Escape sequences handled here are invocation, |
4ed46869 KH |
3653 | designation, direction specification, and character |
3654 | composition specification. */ | |
3655 | switch (c1) | |
3656 | { | |
3657 | case '&': /* revision of following character set */ | |
3658 | ONE_MORE_BYTE (c1); | |
3659 | if (!(c1 >= '@' && c1 <= '~')) | |
df7492f9 | 3660 | goto invalid_code; |
4ed46869 KH |
3661 | ONE_MORE_BYTE (c1); |
3662 | if (c1 != ISO_CODE_ESC) | |
df7492f9 | 3663 | goto invalid_code; |
4ed46869 KH |
3664 | ONE_MORE_BYTE (c1); |
3665 | goto label_escape_sequence; | |
3666 | ||
3667 | case '$': /* designation of 2-byte character set */ | |
df7492f9 KH |
3668 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION)) |
3669 | goto invalid_code; | |
134b9549 KH |
3670 | { |
3671 | int reg, chars96; | |
3672 | ||
3673 | ONE_MORE_BYTE (c1); | |
3674 | if (c1 >= '@' && c1 <= 'B') | |
3675 | { /* designation of JISX0208.1978, GB2312.1980, | |
88993dfd | 3676 | or JISX0208.1980 */ |
134b9549 KH |
3677 | reg = 0, chars96 = 0; |
3678 | } | |
3679 | else if (c1 >= 0x28 && c1 <= 0x2B) | |
3680 | { /* designation of DIMENSION2_CHARS94 character set */ | |
3681 | reg = c1 - 0x28, chars96 = 0; | |
3682 | ONE_MORE_BYTE (c1); | |
3683 | } | |
3684 | else if (c1 >= 0x2C && c1 <= 0x2F) | |
3685 | { /* designation of DIMENSION2_CHARS96 character set */ | |
3686 | reg = c1 - 0x2C, chars96 = 1; | |
3687 | ONE_MORE_BYTE (c1); | |
3688 | } | |
3689 | else | |
3690 | goto invalid_code; | |
3691 | DECODE_DESIGNATION (reg, 2, chars96, c1); | |
3692 | /* We must update these variables now. */ | |
3693 | if (reg == 0) | |
3694 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
3695 | else if (reg == 1) | |
3696 | charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1); | |
3697 | if (chars96 < 0) | |
3698 | goto invalid_code; | |
3699 | } | |
b73bfc1c | 3700 | continue; |
4ed46869 KH |
3701 | |
3702 | case 'n': /* invocation of locking-shift-2 */ | |
df7492f9 KH |
3703 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT) |
3704 | || CODING_ISO_DESIGNATION (coding, 2) < 0) | |
3705 | goto invalid_code; | |
3706 | CODING_ISO_INVOCATION (coding, 0) = 2; | |
3707 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3708 | continue; |
4ed46869 KH |
3709 | |
3710 | case 'o': /* invocation of locking-shift-3 */ | |
df7492f9 KH |
3711 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LOCKING_SHIFT) |
3712 | || CODING_ISO_DESIGNATION (coding, 3) < 0) | |
3713 | goto invalid_code; | |
3714 | CODING_ISO_INVOCATION (coding, 0) = 3; | |
3715 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
b73bfc1c | 3716 | continue; |
4ed46869 KH |
3717 | |
3718 | case 'N': /* invocation of single-shift-2 */ | |
df7492f9 KH |
3719 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
3720 | || CODING_ISO_DESIGNATION (coding, 2) < 0) | |
3721 | goto invalid_code; | |
134b9549 KH |
3722 | charset_id_2 = CODING_ISO_DESIGNATION (coding, 2); |
3723 | if (charset_id_2 < 0) | |
3724 | charset = CHARSET_FROM_ID (charset_ascii); | |
3725 | else | |
3726 | charset = CHARSET_FROM_ID (charset_id_2); | |
b73bfc1c | 3727 | ONE_MORE_BYTE (c1); |
e7046a18 | 3728 | if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0)) |
df7492f9 | 3729 | goto invalid_code; |
4ed46869 KH |
3730 | break; |
3731 | ||
3732 | case 'O': /* invocation of single-shift-3 */ | |
df7492f9 KH |
3733 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
3734 | || CODING_ISO_DESIGNATION (coding, 3) < 0) | |
3735 | goto invalid_code; | |
134b9549 KH |
3736 | charset_id_3 = CODING_ISO_DESIGNATION (coding, 3); |
3737 | if (charset_id_3 < 0) | |
3738 | charset = CHARSET_FROM_ID (charset_ascii); | |
3739 | else | |
3740 | charset = CHARSET_FROM_ID (charset_id_3); | |
b73bfc1c | 3741 | ONE_MORE_BYTE (c1); |
e7046a18 | 3742 | if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0)) |
df7492f9 | 3743 | goto invalid_code; |
4ed46869 KH |
3744 | break; |
3745 | ||
ec6d2bb8 | 3746 | case '0': case '2': case '3': case '4': /* start composition */ |
df7492f9 KH |
3747 | if (! (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK)) |
3748 | goto invalid_code; | |
e951386e KH |
3749 | if (last_id != charset_ascii) |
3750 | { | |
3751 | ADD_CHARSET_DATA (charbuf, char_offset- last_offset, last_id); | |
3752 | last_id = charset_ascii; | |
3753 | last_offset = char_offset; | |
3754 | } | |
ec6d2bb8 | 3755 | DECODE_COMPOSITION_START (c1); |
b73bfc1c | 3756 | continue; |
4ed46869 | 3757 | |
ec6d2bb8 | 3758 | case '1': /* end composition */ |
e951386e | 3759 | if (cmp_status->state == COMPOSING_NO) |
df7492f9 KH |
3760 | goto invalid_code; |
3761 | DECODE_COMPOSITION_END (); | |
b73bfc1c | 3762 | continue; |
4ed46869 KH |
3763 | |
3764 | case '[': /* specification of direction */ | |
de59072a | 3765 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DIRECTION)) |
df7492f9 | 3766 | goto invalid_code; |
4ed46869 | 3767 | /* For the moment, nested direction is not supported. |
d46c5b12 | 3768 | So, `coding->mode & CODING_MODE_DIRECTION' zero means |
ad1746f5 | 3769 | left-to-right, and nonzero means right-to-left. */ |
4ed46869 KH |
3770 | ONE_MORE_BYTE (c1); |
3771 | switch (c1) | |
3772 | { | |
3773 | case ']': /* end of the current direction */ | |
d46c5b12 | 3774 | coding->mode &= ~CODING_MODE_DIRECTION; |
4ed46869 KH |
3775 | |
3776 | case '0': /* end of the current direction */ | |
3777 | case '1': /* start of left-to-right direction */ | |
3778 | ONE_MORE_BYTE (c1); | |
3779 | if (c1 == ']') | |
d46c5b12 | 3780 | coding->mode &= ~CODING_MODE_DIRECTION; |
4ed46869 | 3781 | else |
df7492f9 | 3782 | goto invalid_code; |
4ed46869 KH |
3783 | break; |
3784 | ||
3785 | case '2': /* start of right-to-left direction */ | |
3786 | ONE_MORE_BYTE (c1); | |
3787 | if (c1 == ']') | |
d46c5b12 | 3788 | coding->mode |= CODING_MODE_DIRECTION; |
4ed46869 | 3789 | else |
df7492f9 | 3790 | goto invalid_code; |
4ed46869 KH |
3791 | break; |
3792 | ||
3793 | default: | |
df7492f9 | 3794 | goto invalid_code; |
4ed46869 | 3795 | } |
b73bfc1c | 3796 | continue; |
4ed46869 | 3797 | |
103e0180 | 3798 | case '%': |
103e0180 KH |
3799 | ONE_MORE_BYTE (c1); |
3800 | if (c1 == '/') | |
3801 | { | |
3802 | /* CTEXT extended segment: | |
3803 | ESC % / [0-4] M L --ENCODING-NAME-- \002 --BYTES-- | |
3804 | We keep these bytes as is for the moment. | |
3805 | They may be decoded by post-read-conversion. */ | |
3806 | int dim, M, L; | |
4776e638 | 3807 | int size; |
8f924df7 | 3808 | |
103e0180 | 3809 | ONE_MORE_BYTE (dim); |
7a84eee5 | 3810 | if (dim < '0' || dim > '4') |
e951386e | 3811 | goto invalid_code; |
103e0180 | 3812 | ONE_MORE_BYTE (M); |
e951386e KH |
3813 | if (M < 128) |
3814 | goto invalid_code; | |
103e0180 | 3815 | ONE_MORE_BYTE (L); |
e951386e KH |
3816 | if (L < 128) |
3817 | goto invalid_code; | |
103e0180 | 3818 | size = ((M - 128) * 128) + (L - 128); |
e951386e | 3819 | if (charbuf + 6 > charbuf_end) |
4776e638 KH |
3820 | goto break_loop; |
3821 | *charbuf++ = ISO_CODE_ESC; | |
3822 | *charbuf++ = '%'; | |
3823 | *charbuf++ = '/'; | |
3824 | *charbuf++ = dim; | |
3825 | *charbuf++ = BYTE8_TO_CHAR (M); | |
3826 | *charbuf++ = BYTE8_TO_CHAR (L); | |
e951386e | 3827 | CODING_ISO_EXTSEGMENT_LEN (coding) = size; |
103e0180 KH |
3828 | } |
3829 | else if (c1 == 'G') | |
3830 | { | |
103e0180 KH |
3831 | /* XFree86 extension for embedding UTF-8 in CTEXT: |
3832 | ESC % G --UTF-8-BYTES-- ESC % @ | |
3833 | We keep these bytes as is for the moment. | |
3834 | They may be decoded by post-read-conversion. */ | |
e951386e | 3835 | if (charbuf + 3 > charbuf_end) |
4776e638 | 3836 | goto break_loop; |
e951386e KH |
3837 | *charbuf++ = ISO_CODE_ESC; |
3838 | *charbuf++ = '%'; | |
3839 | *charbuf++ = 'G'; | |
3840 | CODING_ISO_EMBEDDED_UTF_8 (coding) = 1; | |
103e0180 KH |
3841 | } |
3842 | else | |
4776e638 | 3843 | goto invalid_code; |
103e0180 | 3844 | continue; |
4776e638 | 3845 | break; |
103e0180 | 3846 | |
4ed46869 | 3847 | default: |
df7492f9 KH |
3848 | if (! (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATION)) |
3849 | goto invalid_code; | |
134b9549 KH |
3850 | { |
3851 | int reg, chars96; | |
3852 | ||
3853 | if (c1 >= 0x28 && c1 <= 0x2B) | |
3854 | { /* designation of DIMENSION1_CHARS94 character set */ | |
3855 | reg = c1 - 0x28, chars96 = 0; | |
3856 | ONE_MORE_BYTE (c1); | |
3857 | } | |
3858 | else if (c1 >= 0x2C && c1 <= 0x2F) | |
3859 | { /* designation of DIMENSION1_CHARS96 character set */ | |
3860 | reg = c1 - 0x2C, chars96 = 1; | |
3861 | ONE_MORE_BYTE (c1); | |
3862 | } | |
3863 | else | |
3864 | goto invalid_code; | |
3865 | DECODE_DESIGNATION (reg, 1, chars96, c1); | |
3866 | /* We must update these variables now. */ | |
3867 | if (reg == 0) | |
3868 | charset_id_0 = CODING_ISO_INVOKED_CHARSET (coding, 0); | |
3869 | else if (reg == 1) | |
3870 | charset_id_1 = CODING_ISO_INVOKED_CHARSET (coding, 1); | |
3871 | if (chars96 < 0) | |
3872 | goto invalid_code; | |
3873 | } | |
b73bfc1c | 3874 | continue; |
4ed46869 | 3875 | } |
413bb2db PE |
3876 | break; |
3877 | ||
3878 | default: | |
3879 | abort (); | |
b73bfc1c | 3880 | } |
4ed46869 | 3881 | |
e951386e KH |
3882 | if (cmp_status->state == COMPOSING_NO |
3883 | && charset->id != charset_ascii | |
ff0dacd7 KH |
3884 | && last_id != charset->id) |
3885 | { | |
3886 | if (last_id != charset_ascii) | |
69a80ea3 | 3887 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
ff0dacd7 KH |
3888 | last_id = charset->id; |
3889 | last_offset = char_offset; | |
3890 | } | |
3891 | ||
b73bfc1c | 3892 | /* Now we know CHARSET and 1st position code C1 of a character. |
cf299835 KH |
3893 | Produce a decoded character while getting 2nd and 3rd |
3894 | position codes C2, C3 if necessary. */ | |
df7492f9 | 3895 | if (CHARSET_DIMENSION (charset) > 1) |
b73bfc1c KH |
3896 | { |
3897 | ONE_MORE_BYTE (c2); | |
cf299835 KH |
3898 | if (c2 < 0x20 || (c2 >= 0x80 && c2 < 0xA0) |
3899 | || ((c1 & 0x80) != (c2 & 0x80))) | |
b73bfc1c | 3900 | /* C2 is not in a valid range. */ |
df7492f9 | 3901 | goto invalid_code; |
cf299835 KH |
3902 | if (CHARSET_DIMENSION (charset) == 2) |
3903 | c1 = (c1 << 8) | c2; | |
3904 | else | |
df7492f9 | 3905 | { |
cf299835 KH |
3906 | ONE_MORE_BYTE (c3); |
3907 | if (c3 < 0x20 || (c3 >= 0x80 && c3 < 0xA0) | |
3908 | || ((c1 & 0x80) != (c3 & 0x80))) | |
3909 | /* C3 is not in a valid range. */ | |
df7492f9 | 3910 | goto invalid_code; |
cf299835 | 3911 | c1 = (c1 << 16) | (c2 << 8) | c2; |
df7492f9 KH |
3912 | } |
3913 | } | |
cf299835 | 3914 | c1 &= 0x7F7F7F; |
df7492f9 KH |
3915 | CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c1, c); |
3916 | if (c < 0) | |
3917 | { | |
3918 | MAYBE_FINISH_COMPOSITION (); | |
3919 | for (; src_base < src; src_base++, char_offset++) | |
3920 | { | |
3921 | if (ASCII_BYTE_P (*src_base)) | |
3922 | *charbuf++ = *src_base; | |
3923 | else | |
3924 | *charbuf++ = BYTE8_TO_CHAR (*src_base); | |
3925 | } | |
3926 | } | |
e951386e | 3927 | else if (cmp_status->state == COMPOSING_NO) |
df7492f9 KH |
3928 | { |
3929 | *charbuf++ = c; | |
3930 | char_offset++; | |
4ed46869 | 3931 | } |
e951386e KH |
3932 | else if ((cmp_status->state == COMPOSING_CHAR |
3933 | ? cmp_status->nchars | |
3934 | : cmp_status->ncomps) | |
3935 | >= MAX_COMPOSITION_COMPONENTS) | |
781d7a48 | 3936 | { |
e951386e KH |
3937 | /* Too long composition. */ |
3938 | MAYBE_FINISH_COMPOSITION (); | |
3939 | *charbuf++ = c; | |
3940 | char_offset++; | |
4ed46869 | 3941 | } |
e951386e KH |
3942 | else |
3943 | STORE_COMPOSITION_CHAR (c); | |
4ed46869 KH |
3944 | continue; |
3945 | ||
df7492f9 KH |
3946 | invalid_code: |
3947 | MAYBE_FINISH_COMPOSITION (); | |
4ed46869 | 3948 | src = src_base; |
df7492f9 KH |
3949 | consumed_chars = consumed_chars_base; |
3950 | ONE_MORE_BYTE (c); | |
065e3595 | 3951 | *charbuf++ = c < 0 ? -c : ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 3952 | char_offset++; |
df7492f9 | 3953 | coding->errors++; |
4776e638 KH |
3954 | continue; |
3955 | ||
3956 | break_loop: | |
3957 | break; | |
4ed46869 | 3958 | } |
fb88bf2d | 3959 | |
df7492f9 | 3960 | no_more_source: |
e951386e KH |
3961 | if (cmp_status->state != COMPOSING_NO) |
3962 | { | |
3963 | if (coding->mode & CODING_MODE_LAST_BLOCK) | |
3964 | MAYBE_FINISH_COMPOSITION (); | |
3965 | else | |
3966 | { | |
3967 | charbuf -= cmp_status->length; | |
3968 | for (i = 0; i < cmp_status->length; i++) | |
3969 | cmp_status->carryover[i] = charbuf[i]; | |
3970 | } | |
3971 | } | |
3972 | else if (last_id != charset_ascii) | |
69a80ea3 | 3973 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
3974 | coding->consumed_char += consumed_chars_base; |
3975 | coding->consumed = src_base - coding->source; | |
3976 | coding->charbuf_used = charbuf - coding->charbuf; | |
4ed46869 KH |
3977 | } |
3978 | ||
b73bfc1c | 3979 | |
f4dee582 | 3980 | /* ISO2022 encoding stuff. */ |
4ed46869 KH |
3981 | |
3982 | /* | |
f4dee582 | 3983 | It is not enough to say just "ISO2022" on encoding, we have to |
df7492f9 | 3984 | specify more details. In Emacs, each coding system of ISO2022 |
4ed46869 | 3985 | variant has the following specifications: |
df7492f9 | 3986 | 1. Initial designation to G0 thru G3. |
4ed46869 KH |
3987 | 2. Allows short-form designation? |
3988 | 3. ASCII should be designated to G0 before control characters? | |
3989 | 4. ASCII should be designated to G0 at end of line? | |
3990 | 5. 7-bit environment or 8-bit environment? | |
3991 | 6. Use locking-shift? | |
3992 | 7. Use Single-shift? | |
3993 | And the following two are only for Japanese: | |
3994 | 8. Use ASCII in place of JIS0201-1976-Roman? | |
3995 | 9. Use JISX0208-1983 in place of JISX0208-1978? | |
df7492f9 KH |
3996 | These specifications are encoded in CODING_ISO_FLAGS (coding) as flag bits |
3997 | defined by macros CODING_ISO_FLAG_XXX. See `coding.h' for more | |
f4dee582 | 3998 | details. |
4ed46869 KH |
3999 | */ |
4000 | ||
4001 | /* Produce codes (escape sequence) for designating CHARSET to graphic | |
b73bfc1c KH |
4002 | register REG at DST, and increment DST. If <final-char> of CHARSET is |
4003 | '@', 'A', or 'B' and the coding system CODING allows, produce | |
4004 | designation sequence of short-form. */ | |
4ed46869 KH |
4005 | |
4006 | #define ENCODE_DESIGNATION(charset, reg, coding) \ | |
4007 | do { \ | |
df7492f9 | 4008 | unsigned char final_char = CHARSET_ISO_FINAL (charset); \ |
675e2c69 DN |
4009 | const char *intermediate_char_94 = "()*+"; \ |
4010 | const char *intermediate_char_96 = ",-./"; \ | |
df7492f9 | 4011 | int revision = -1; \ |
df7492f9 KH |
4012 | \ |
4013 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_REVISION) \ | |
c197f191 | 4014 | revision = CHARSET_ISO_REVISION (charset); \ |
df7492f9 KH |
4015 | \ |
4016 | if (revision >= 0) \ | |
70c22245 | 4017 | { \ |
df7492f9 KH |
4018 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, '&'); \ |
4019 | EMIT_ONE_BYTE ('@' + revision); \ | |
4ed46869 | 4020 | } \ |
df7492f9 | 4021 | EMIT_ONE_ASCII_BYTE (ISO_CODE_ESC); \ |
4ed46869 KH |
4022 | if (CHARSET_DIMENSION (charset) == 1) \ |
4023 | { \ | |
2735d060 | 4024 | int b; \ |
df7492f9 | 4025 | if (! CHARSET_ISO_CHARS_96 (charset)) \ |
2735d060 | 4026 | b = intermediate_char_94[reg]; \ |
4ed46869 | 4027 | else \ |
2735d060 PE |
4028 | b = intermediate_char_96[reg]; \ |
4029 | EMIT_ONE_ASCII_BYTE (b); \ | |
4ed46869 KH |
4030 | } \ |
4031 | else \ | |
4032 | { \ | |
df7492f9 KH |
4033 | EMIT_ONE_ASCII_BYTE ('$'); \ |
4034 | if (! CHARSET_ISO_CHARS_96 (charset)) \ | |
4ed46869 | 4035 | { \ |
df7492f9 | 4036 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_LONG_FORM \ |
b73bfc1c KH |
4037 | || reg != 0 \ |
4038 | || final_char < '@' || final_char > 'B') \ | |
df7492f9 | 4039 | EMIT_ONE_ASCII_BYTE (intermediate_char_94[reg]); \ |
4ed46869 KH |
4040 | } \ |
4041 | else \ | |
df7492f9 | 4042 | EMIT_ONE_ASCII_BYTE (intermediate_char_96[reg]); \ |
4ed46869 | 4043 | } \ |
df7492f9 KH |
4044 | EMIT_ONE_ASCII_BYTE (final_char); \ |
4045 | \ | |
4046 | CODING_ISO_DESIGNATION (coding, reg) = CHARSET_ID (charset); \ | |
4ed46869 KH |
4047 | } while (0) |
4048 | ||
df7492f9 | 4049 | |
4ed46869 KH |
4050 | /* The following two macros produce codes (control character or escape |
4051 | sequence) for ISO2022 single-shift functions (single-shift-2 and | |
4052 | single-shift-3). */ | |
4053 | ||
df7492f9 KH |
4054 | #define ENCODE_SINGLE_SHIFT_2 \ |
4055 | do { \ | |
4056 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ | |
4057 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'N'); \ | |
4058 | else \ | |
4059 | EMIT_ONE_BYTE (ISO_CODE_SS2); \ | |
4060 | CODING_ISO_SINGLE_SHIFTING (coding) = 1; \ | |
4ed46869 KH |
4061 | } while (0) |
4062 | ||
df7492f9 KH |
4063 | |
4064 | #define ENCODE_SINGLE_SHIFT_3 \ | |
4065 | do { \ | |
4066 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ | |
4067 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'O'); \ | |
4068 | else \ | |
4069 | EMIT_ONE_BYTE (ISO_CODE_SS3); \ | |
4070 | CODING_ISO_SINGLE_SHIFTING (coding) = 1; \ | |
4ed46869 KH |
4071 | } while (0) |
4072 | ||
df7492f9 | 4073 | |
4ed46869 KH |
4074 | /* The following four macros produce codes (control character or |
4075 | escape sequence) for ISO2022 locking-shift functions (shift-in, | |
4076 | shift-out, locking-shift-2, and locking-shift-3). */ | |
4077 | ||
df7492f9 KH |
4078 | #define ENCODE_SHIFT_IN \ |
4079 | do { \ | |
4080 | EMIT_ONE_ASCII_BYTE (ISO_CODE_SI); \ | |
4081 | CODING_ISO_INVOCATION (coding, 0) = 0; \ | |
4ed46869 KH |
4082 | } while (0) |
4083 | ||
df7492f9 KH |
4084 | |
4085 | #define ENCODE_SHIFT_OUT \ | |
4086 | do { \ | |
4087 | EMIT_ONE_ASCII_BYTE (ISO_CODE_SO); \ | |
4088 | CODING_ISO_INVOCATION (coding, 0) = 1; \ | |
4ed46869 KH |
4089 | } while (0) |
4090 | ||
df7492f9 KH |
4091 | |
4092 | #define ENCODE_LOCKING_SHIFT_2 \ | |
4093 | do { \ | |
4094 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \ | |
4095 | CODING_ISO_INVOCATION (coding, 0) = 2; \ | |
4ed46869 KH |
4096 | } while (0) |
4097 | ||
df7492f9 KH |
4098 | |
4099 | #define ENCODE_LOCKING_SHIFT_3 \ | |
4100 | do { \ | |
4101 | EMIT_TWO_ASCII_BYTES (ISO_CODE_ESC, 'n'); \ | |
4102 | CODING_ISO_INVOCATION (coding, 0) = 3; \ | |
4ed46869 KH |
4103 | } while (0) |
4104 | ||
df7492f9 | 4105 | |
f4dee582 RS |
4106 | /* Produce codes for a DIMENSION1 character whose character set is |
4107 | CHARSET and whose position-code is C1. Designation and invocation | |
4ed46869 KH |
4108 | sequences are also produced in advance if necessary. */ |
4109 | ||
6e85d753 KH |
4110 | #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \ |
4111 | do { \ | |
df7492f9 | 4112 | int id = CHARSET_ID (charset); \ |
bf16eb23 KH |
4113 | \ |
4114 | if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_ROMAN) \ | |
4115 | && id == charset_ascii) \ | |
4116 | { \ | |
4117 | id = charset_jisx0201_roman; \ | |
4118 | charset = CHARSET_FROM_ID (id); \ | |
4119 | } \ | |
4120 | \ | |
df7492f9 | 4121 | if (CODING_ISO_SINGLE_SHIFTING (coding)) \ |
6e85d753 | 4122 | { \ |
df7492f9 KH |
4123 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ |
4124 | EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \ | |
6e85d753 | 4125 | else \ |
df7492f9 KH |
4126 | EMIT_ONE_BYTE (c1 | 0x80); \ |
4127 | CODING_ISO_SINGLE_SHIFTING (coding) = 0; \ | |
6e85d753 KH |
4128 | break; \ |
4129 | } \ | |
df7492f9 | 4130 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \ |
6e85d753 | 4131 | { \ |
df7492f9 | 4132 | EMIT_ONE_ASCII_BYTE (c1 & 0x7F); \ |
6e85d753 KH |
4133 | break; \ |
4134 | } \ | |
df7492f9 | 4135 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \ |
6e85d753 | 4136 | { \ |
df7492f9 | 4137 | EMIT_ONE_BYTE (c1 | 0x80); \ |
6e85d753 KH |
4138 | break; \ |
4139 | } \ | |
6e85d753 KH |
4140 | else \ |
4141 | /* Since CHARSET is not yet invoked to any graphic planes, we \ | |
4142 | must invoke it, or, at first, designate it to some graphic \ | |
4143 | register. Then repeat the loop to actually produce the \ | |
4144 | character. */ \ | |
df7492f9 KH |
4145 | dst = encode_invocation_designation (charset, coding, dst, \ |
4146 | &produced_chars); \ | |
4ed46869 KH |
4147 | } while (1) |
4148 | ||
df7492f9 | 4149 | |
f4dee582 RS |
4150 | /* Produce codes for a DIMENSION2 character whose character set is |
4151 | CHARSET and whose position-codes are C1 and C2. Designation and | |
4ed46869 KH |
4152 | invocation codes are also produced in advance if necessary. */ |
4153 | ||
6e85d753 KH |
4154 | #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \ |
4155 | do { \ | |
df7492f9 | 4156 | int id = CHARSET_ID (charset); \ |
bf16eb23 KH |
4157 | \ |
4158 | if ((CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_USE_OLDJIS) \ | |
4159 | && id == charset_jisx0208) \ | |
4160 | { \ | |
4161 | id = charset_jisx0208_1978; \ | |
4162 | charset = CHARSET_FROM_ID (id); \ | |
4163 | } \ | |
4164 | \ | |
df7492f9 | 4165 | if (CODING_ISO_SINGLE_SHIFTING (coding)) \ |
6e85d753 | 4166 | { \ |
df7492f9 KH |
4167 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SEVEN_BITS) \ |
4168 | EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \ | |
6e85d753 | 4169 | else \ |
df7492f9 KH |
4170 | EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \ |
4171 | CODING_ISO_SINGLE_SHIFTING (coding) = 0; \ | |
6e85d753 KH |
4172 | break; \ |
4173 | } \ | |
df7492f9 | 4174 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 0)) \ |
6e85d753 | 4175 | { \ |
df7492f9 | 4176 | EMIT_TWO_ASCII_BYTES ((c1) & 0x7F, (c2) & 0x7F); \ |
6e85d753 KH |
4177 | break; \ |
4178 | } \ | |
df7492f9 | 4179 | else if (id == CODING_ISO_INVOKED_CHARSET (coding, 1)) \ |
6e85d753 | 4180 | { \ |
df7492f9 | 4181 | EMIT_TWO_BYTES ((c1) | 0x80, (c2) | 0x80); \ |
6e85d753 KH |
4182 | break; \ |
4183 | } \ | |
6e85d753 KH |
4184 | else \ |
4185 | /* Since CHARSET is not yet invoked to any graphic planes, we \ | |
4186 | must invoke it, or, at first, designate it to some graphic \ | |
4187 | register. Then repeat the loop to actually produce the \ | |
4188 | character. */ \ | |
df7492f9 KH |
4189 | dst = encode_invocation_designation (charset, coding, dst, \ |
4190 | &produced_chars); \ | |
4ed46869 KH |
4191 | } while (1) |
4192 | ||
05e6f5dc | 4193 | |
df7492f9 KH |
4194 | #define ENCODE_ISO_CHARACTER(charset, c) \ |
4195 | do { \ | |
1a4990fb | 4196 | int code = ENCODE_CHAR ((charset), (c)); \ |
df7492f9 KH |
4197 | \ |
4198 | if (CHARSET_DIMENSION (charset) == 1) \ | |
4199 | ENCODE_ISO_CHARACTER_DIMENSION1 ((charset), code); \ | |
4200 | else \ | |
4201 | ENCODE_ISO_CHARACTER_DIMENSION2 ((charset), code >> 8, code & 0xFF); \ | |
84fbb8a0 | 4202 | } while (0) |
bdd9fb48 | 4203 | |
05e6f5dc | 4204 | |
4ed46869 | 4205 | /* Produce designation and invocation codes at a place pointed by DST |
df7492f9 | 4206 | to use CHARSET. The element `spec.iso_2022' of *CODING is updated. |
4ed46869 KH |
4207 | Return new DST. */ |
4208 | ||
e2f1bab9 | 4209 | static unsigned char * |
cf84bb53 JB |
4210 | encode_invocation_designation (struct charset *charset, |
4211 | struct coding_system *coding, | |
4212 | unsigned char *dst, int *p_nchars) | |
4ed46869 | 4213 | { |
df7492f9 KH |
4214 | int multibytep = coding->dst_multibyte; |
4215 | int produced_chars = *p_nchars; | |
4ed46869 | 4216 | int reg; /* graphic register number */ |
df7492f9 | 4217 | int id = CHARSET_ID (charset); |
4ed46869 KH |
4218 | |
4219 | /* At first, check designations. */ | |
4220 | for (reg = 0; reg < 4; reg++) | |
df7492f9 | 4221 | if (id == CODING_ISO_DESIGNATION (coding, reg)) |
4ed46869 KH |
4222 | break; |
4223 | ||
4224 | if (reg >= 4) | |
4225 | { | |
4226 | /* CHARSET is not yet designated to any graphic registers. */ | |
4227 | /* At first check the requested designation. */ | |
df7492f9 KH |
4228 | reg = CODING_ISO_REQUEST (coding, id); |
4229 | if (reg < 0) | |
1ba9e4ab KH |
4230 | /* Since CHARSET requests no special designation, designate it |
4231 | to graphic register 0. */ | |
4ed46869 KH |
4232 | reg = 0; |
4233 | ||
4234 | ENCODE_DESIGNATION (charset, reg, coding); | |
4235 | } | |
4236 | ||
df7492f9 KH |
4237 | if (CODING_ISO_INVOCATION (coding, 0) != reg |
4238 | && CODING_ISO_INVOCATION (coding, 1) != reg) | |
4ed46869 KH |
4239 | { |
4240 | /* Since the graphic register REG is not invoked to any graphic | |
4241 | planes, invoke it to graphic plane 0. */ | |
4242 | switch (reg) | |
4243 | { | |
4244 | case 0: /* graphic register 0 */ | |
4245 | ENCODE_SHIFT_IN; | |
4246 | break; | |
4247 | ||
4248 | case 1: /* graphic register 1 */ | |
4249 | ENCODE_SHIFT_OUT; | |
4250 | break; | |
4251 | ||
4252 | case 2: /* graphic register 2 */ | |
df7492f9 | 4253 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
4ed46869 KH |
4254 | ENCODE_SINGLE_SHIFT_2; |
4255 | else | |
4256 | ENCODE_LOCKING_SHIFT_2; | |
4257 | break; | |
4258 | ||
4259 | case 3: /* graphic register 3 */ | |
df7492f9 | 4260 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_SINGLE_SHIFT) |
4ed46869 KH |
4261 | ENCODE_SINGLE_SHIFT_3; |
4262 | else | |
4263 | ENCODE_LOCKING_SHIFT_3; | |
4264 | break; | |
4265 | } | |
4266 | } | |
b73bfc1c | 4267 | |
df7492f9 | 4268 | *p_nchars = produced_chars; |
4ed46869 KH |
4269 | return dst; |
4270 | } | |
4271 | ||
4ed46869 KH |
4272 | |
4273 | /* Produce codes for designation and invocation to reset the graphic | |
4274 | planes and registers to initial state. */ | |
df7492f9 KH |
4275 | #define ENCODE_RESET_PLANE_AND_REGISTER() \ |
4276 | do { \ | |
4277 | int reg; \ | |
4278 | struct charset *charset; \ | |
4279 | \ | |
4280 | if (CODING_ISO_INVOCATION (coding, 0) != 0) \ | |
4281 | ENCODE_SHIFT_IN; \ | |
4282 | for (reg = 0; reg < 4; reg++) \ | |
4283 | if (CODING_ISO_INITIAL (coding, reg) >= 0 \ | |
4284 | && (CODING_ISO_DESIGNATION (coding, reg) \ | |
4285 | != CODING_ISO_INITIAL (coding, reg))) \ | |
4286 | { \ | |
4287 | charset = CHARSET_FROM_ID (CODING_ISO_INITIAL (coding, reg)); \ | |
4288 | ENCODE_DESIGNATION (charset, reg, coding); \ | |
4289 | } \ | |
4ed46869 KH |
4290 | } while (0) |
4291 | ||
df7492f9 | 4292 | |
bdd9fb48 | 4293 | /* Produce designation sequences of charsets in the line started from |
b73bfc1c | 4294 | SRC to a place pointed by DST, and return updated DST. |
bdd9fb48 KH |
4295 | |
4296 | If the current block ends before any end-of-line, we may fail to | |
d46c5b12 KH |
4297 | find all the necessary designations. */ |
4298 | ||
b73bfc1c | 4299 | static unsigned char * |
cf84bb53 | 4300 | encode_designation_at_bol (struct coding_system *coding, int *charbuf, |
461c2ab9 | 4301 | unsigned char *dst) |
e0e989f6 | 4302 | { |
df7492f9 | 4303 | struct charset *charset; |
bdd9fb48 KH |
4304 | /* Table of charsets to be designated to each graphic register. */ |
4305 | int r[4]; | |
df7492f9 KH |
4306 | int c, found = 0, reg; |
4307 | int produced_chars = 0; | |
4308 | int multibytep = coding->dst_multibyte; | |
4309 | Lisp_Object attrs; | |
4310 | Lisp_Object charset_list; | |
4311 | ||
4312 | attrs = CODING_ID_ATTRS (coding->id); | |
4313 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
4314 | if (EQ (charset_list, Qiso_2022)) | |
4315 | charset_list = Viso_2022_charset_list; | |
bdd9fb48 KH |
4316 | |
4317 | for (reg = 0; reg < 4; reg++) | |
4318 | r[reg] = -1; | |
4319 | ||
b73bfc1c | 4320 | while (found < 4) |
e0e989f6 | 4321 | { |
df7492f9 KH |
4322 | int id; |
4323 | ||
4324 | c = *charbuf++; | |
b73bfc1c KH |
4325 | if (c == '\n') |
4326 | break; | |
df7492f9 KH |
4327 | charset = char_charset (c, charset_list, NULL); |
4328 | id = CHARSET_ID (charset); | |
4329 | reg = CODING_ISO_REQUEST (coding, id); | |
4330 | if (reg >= 0 && r[reg] < 0) | |
bdd9fb48 KH |
4331 | { |
4332 | found++; | |
df7492f9 | 4333 | r[reg] = id; |
bdd9fb48 | 4334 | } |
bdd9fb48 KH |
4335 | } |
4336 | ||
4337 | if (found) | |
4338 | { | |
4339 | for (reg = 0; reg < 4; reg++) | |
4340 | if (r[reg] >= 0 | |
df7492f9 KH |
4341 | && CODING_ISO_DESIGNATION (coding, reg) != r[reg]) |
4342 | ENCODE_DESIGNATION (CHARSET_FROM_ID (r[reg]), reg, coding); | |
e0e989f6 | 4343 | } |
b73bfc1c KH |
4344 | |
4345 | return dst; | |
e0e989f6 KH |
4346 | } |
4347 | ||
4ed46869 KH |
4348 | /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */ |
4349 | ||
df7492f9 | 4350 | static int |
971de7fb | 4351 | encode_coding_iso_2022 (struct coding_system *coding) |
4ed46869 | 4352 | { |
df7492f9 KH |
4353 | int multibytep = coding->dst_multibyte; |
4354 | int *charbuf = coding->charbuf; | |
4355 | int *charbuf_end = charbuf + coding->charbuf_used; | |
4356 | unsigned char *dst = coding->destination + coding->produced; | |
4357 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
4358 | int safe_room = 16; | |
4359 | int bol_designation | |
4360 | = (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATE_AT_BOL | |
4361 | && CODING_ISO_BOL (coding)); | |
4362 | int produced_chars = 0; | |
4363 | Lisp_Object attrs, eol_type, charset_list; | |
4364 | int ascii_compatible; | |
b73bfc1c | 4365 | int c; |
ff0dacd7 | 4366 | int preferred_charset_id = -1; |
05e6f5dc | 4367 | |
24a73b0a | 4368 | CODING_GET_INFO (coding, attrs, charset_list); |
0a9564cb | 4369 | eol_type = inhibit_eol_conversion ? Qunix : CODING_ID_EOL_TYPE (coding->id); |
24a73b0a KH |
4370 | if (VECTORP (eol_type)) |
4371 | eol_type = Qunix; | |
4372 | ||
004068e4 | 4373 | setup_iso_safe_charsets (attrs); |
ff0dacd7 | 4374 | /* Charset list may have been changed. */ |
287c57d7 | 4375 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); |
1b3b981b | 4376 | coding->safe_charsets = SDATA (CODING_ATTR_SAFE_CHARSETS (attrs)); |
0eecad43 | 4377 | |
a552b35a KH |
4378 | ascii_compatible |
4379 | = (! NILP (CODING_ATTR_ASCII_COMPAT (attrs)) | |
4380 | && ! (CODING_ISO_FLAGS (coding) & (CODING_ISO_FLAG_DESIGNATION | |
4381 | | CODING_ISO_FLAG_LOCKING_SHIFT))); | |
bdd9fb48 | 4382 | |
df7492f9 | 4383 | while (charbuf < charbuf_end) |
4ed46869 | 4384 | { |
df7492f9 | 4385 | ASSURE_DESTINATION (safe_room); |
b73bfc1c | 4386 | |
df7492f9 | 4387 | if (bol_designation) |
b73bfc1c | 4388 | { |
df7492f9 | 4389 | unsigned char *dst_prev = dst; |
4ed46869 | 4390 | |
bdd9fb48 | 4391 | /* We have to produce designation sequences if any now. */ |
461c2ab9 | 4392 | dst = encode_designation_at_bol (coding, charbuf, dst); |
df7492f9 KH |
4393 | bol_designation = 0; |
4394 | /* We are sure that designation sequences are all ASCII bytes. */ | |
4395 | produced_chars += dst - dst_prev; | |
e0e989f6 KH |
4396 | } |
4397 | ||
df7492f9 | 4398 | c = *charbuf++; |
ec6d2bb8 | 4399 | |
ff0dacd7 KH |
4400 | if (c < 0) |
4401 | { | |
4402 | /* Handle an annotation. */ | |
4403 | switch (*charbuf) | |
ec6d2bb8 | 4404 | { |
ff0dacd7 KH |
4405 | case CODING_ANNOTATE_COMPOSITION_MASK: |
4406 | /* Not yet implemented. */ | |
4407 | break; | |
4408 | case CODING_ANNOTATE_CHARSET_MASK: | |
cf7dfdf5 | 4409 | preferred_charset_id = charbuf[2]; |
ff0dacd7 KH |
4410 | if (preferred_charset_id >= 0 |
4411 | && NILP (Fmemq (make_number (preferred_charset_id), | |
4412 | charset_list))) | |
4413 | preferred_charset_id = -1; | |
4414 | break; | |
4415 | default: | |
4416 | abort (); | |
4ed46869 | 4417 | } |
ff0dacd7 KH |
4418 | charbuf += -c - 1; |
4419 | continue; | |
4ed46869 | 4420 | } |
ec6d2bb8 | 4421 | |
b73bfc1c KH |
4422 | /* Now encode the character C. */ |
4423 | if (c < 0x20 || c == 0x7F) | |
4424 | { | |
df7492f9 KH |
4425 | if (c == '\n' |
4426 | || (c == '\r' && EQ (eol_type, Qmac))) | |
19a8d9e0 | 4427 | { |
df7492f9 KH |
4428 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL) |
4429 | ENCODE_RESET_PLANE_AND_REGISTER (); | |
4430 | if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_INIT_AT_BOL) | |
b73bfc1c | 4431 | { |
df7492f9 KH |
4432 | int i; |
4433 | ||
4434 | for (i = 0; i < 4; i++) | |
4435 | CODING_ISO_DESIGNATION (coding, i) | |
4436 | = CODING_ISO_INITIAL (coding, i); | |
b73bfc1c | 4437 | } |
df7492f9 KH |
4438 | bol_designation |
4439 | = CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_DESIGNATE_AT_BOL; | |
19a8d9e0 | 4440 | } |
df7492f9 KH |
4441 | else if (CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_CNTL) |
4442 | ENCODE_RESET_PLANE_AND_REGISTER (); | |
4443 | EMIT_ONE_ASCII_BYTE (c); | |
4ed46869 | 4444 | } |
df7492f9 | 4445 | else if (ASCII_CHAR_P (c)) |
88993dfd | 4446 | { |
df7492f9 KH |
4447 | if (ascii_compatible) |
4448 | EMIT_ONE_ASCII_BYTE (c); | |
93dec019 | 4449 | else |
19a8d9e0 | 4450 | { |
bf16eb23 KH |
4451 | struct charset *charset = CHARSET_FROM_ID (charset_ascii); |
4452 | ENCODE_ISO_CHARACTER (charset, c); | |
19a8d9e0 | 4453 | } |
4ed46869 | 4454 | } |
16eafb5d | 4455 | else if (CHAR_BYTE8_P (c)) |
88993dfd | 4456 | { |
16eafb5d KH |
4457 | c = CHAR_TO_BYTE8 (c); |
4458 | EMIT_ONE_BYTE (c); | |
88993dfd | 4459 | } |
b73bfc1c | 4460 | else |
df7492f9 | 4461 | { |
ff0dacd7 | 4462 | struct charset *charset; |
b73bfc1c | 4463 | |
ff0dacd7 KH |
4464 | if (preferred_charset_id >= 0) |
4465 | { | |
4466 | charset = CHARSET_FROM_ID (preferred_charset_id); | |
4467 | if (! CHAR_CHARSET_P (c, charset)) | |
4468 | charset = char_charset (c, charset_list, NULL); | |
4469 | } | |
4470 | else | |
4471 | charset = char_charset (c, charset_list, NULL); | |
df7492f9 KH |
4472 | if (!charset) |
4473 | { | |
41cbe562 KH |
4474 | if (coding->mode & CODING_MODE_SAFE_ENCODING) |
4475 | { | |
4476 | c = CODING_INHIBIT_CHARACTER_SUBSTITUTION; | |
4477 | charset = CHARSET_FROM_ID (charset_ascii); | |
4478 | } | |
4479 | else | |
4480 | { | |
4481 | c = coding->default_char; | |
4482 | charset = char_charset (c, charset_list, NULL); | |
4483 | } | |
df7492f9 KH |
4484 | } |
4485 | ENCODE_ISO_CHARACTER (charset, c); | |
4486 | } | |
84fbb8a0 | 4487 | } |
b73bfc1c | 4488 | |
df7492f9 KH |
4489 | if (coding->mode & CODING_MODE_LAST_BLOCK |
4490 | && CODING_ISO_FLAGS (coding) & CODING_ISO_FLAG_RESET_AT_EOL) | |
4491 | { | |
4492 | ASSURE_DESTINATION (safe_room); | |
4493 | ENCODE_RESET_PLANE_AND_REGISTER (); | |
4494 | } | |
065e3595 | 4495 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
4496 | CODING_ISO_BOL (coding) = bol_designation; |
4497 | coding->produced_char += produced_chars; | |
4498 | coding->produced = dst - coding->destination; | |
4499 | return 0; | |
4ed46869 KH |
4500 | } |
4501 | ||
4502 | \f | |
df7492f9 | 4503 | /*** 8,9. SJIS and BIG5 handlers ***/ |
4ed46869 | 4504 | |
df7492f9 | 4505 | /* Although SJIS and BIG5 are not ISO's coding system, they are used |
4ed46869 KH |
4506 | quite widely. So, for the moment, Emacs supports them in the bare |
4507 | C code. But, in the future, they may be supported only by CCL. */ | |
4508 | ||
4509 | /* SJIS is a coding system encoding three character sets: ASCII, right | |
4510 | half of JISX0201-Kana, and JISX0208. An ASCII character is encoded | |
4511 | as is. A character of charset katakana-jisx0201 is encoded by | |
4512 | "position-code + 0x80". A character of charset japanese-jisx0208 | |
4513 | is encoded in 2-byte but two position-codes are divided and shifted | |
df7492f9 | 4514 | so that it fit in the range below. |
4ed46869 KH |
4515 | |
4516 | --- CODE RANGE of SJIS --- | |
4517 | (character set) (range) | |
4518 | ASCII 0x00 .. 0x7F | |
df7492f9 | 4519 | KATAKANA-JISX0201 0xA0 .. 0xDF |
c28a9453 | 4520 | JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF |
d14d03ac | 4521 | (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC |
4ed46869 KH |
4522 | ------------------------------- |
4523 | ||
4524 | */ | |
4525 | ||
4526 | /* BIG5 is a coding system encoding two character sets: ASCII and | |
4527 | Big5. An ASCII character is encoded as is. Big5 is a two-byte | |
df7492f9 | 4528 | character set and is encoded in two-byte. |
4ed46869 KH |
4529 | |
4530 | --- CODE RANGE of BIG5 --- | |
4531 | (character set) (range) | |
4532 | ASCII 0x00 .. 0x7F | |
4533 | Big5 (1st byte) 0xA1 .. 0xFE | |
4534 | (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE | |
4535 | -------------------------- | |
4536 | ||
df7492f9 | 4537 | */ |
4ed46869 KH |
4538 | |
4539 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
4540 | Check if a text is encoded in SJIS. If it is, return | |
df7492f9 | 4541 | CATEGORY_MASK_SJIS, else return 0. */ |
4ed46869 | 4542 | |
0a28aafb | 4543 | static int |
cf84bb53 JB |
4544 | detect_coding_sjis (struct coding_system *coding, |
4545 | struct coding_detection_info *detect_info) | |
4ed46869 | 4546 | { |
065e3595 | 4547 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 4548 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
4549 | int multibytep = coding->src_multibyte; |
4550 | int consumed_chars = 0; | |
4551 | int found = 0; | |
b73bfc1c | 4552 | int c; |
f07190ca KH |
4553 | Lisp_Object attrs, charset_list; |
4554 | int max_first_byte_of_2_byte_code; | |
4555 | ||
4556 | CODING_GET_INFO (coding, attrs, charset_list); | |
4557 | max_first_byte_of_2_byte_code | |
4558 | = (XINT (Flength (charset_list)) > 3 ? 0xFC : 0xEF); | |
df7492f9 | 4559 | |
ff0dacd7 | 4560 | detect_info->checked |= CATEGORY_MASK_SJIS; |
df7492f9 KH |
4561 | /* A coding system of this category is always ASCII compatible. */ |
4562 | src += coding->head_ascii; | |
4ed46869 | 4563 | |
b73bfc1c | 4564 | while (1) |
4ed46869 | 4565 | { |
065e3595 | 4566 | src_base = src; |
df7492f9 | 4567 | ONE_MORE_BYTE (c); |
682169fe KH |
4568 | if (c < 0x80) |
4569 | continue; | |
f07190ca KH |
4570 | if ((c >= 0x81 && c <= 0x9F) |
4571 | || (c >= 0xE0 && c <= max_first_byte_of_2_byte_code)) | |
4ed46869 | 4572 | { |
df7492f9 | 4573 | ONE_MORE_BYTE (c); |
682169fe | 4574 | if (c < 0x40 || c == 0x7F || c > 0xFC) |
df7492f9 | 4575 | break; |
ff0dacd7 | 4576 | found = CATEGORY_MASK_SJIS; |
4ed46869 | 4577 | } |
df7492f9 | 4578 | else if (c >= 0xA0 && c < 0xE0) |
ff0dacd7 | 4579 | found = CATEGORY_MASK_SJIS; |
df7492f9 KH |
4580 | else |
4581 | break; | |
4ed46869 | 4582 | } |
ff0dacd7 | 4583 | detect_info->rejected |= CATEGORY_MASK_SJIS; |
df7492f9 KH |
4584 | return 0; |
4585 | ||
4586 | no_more_source: | |
065e3595 | 4587 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) |
89528eb3 | 4588 | { |
ff0dacd7 | 4589 | detect_info->rejected |= CATEGORY_MASK_SJIS; |
89528eb3 | 4590 | return 0; |
4ed46869 | 4591 | } |
ff0dacd7 KH |
4592 | detect_info->found |= found; |
4593 | return 1; | |
4ed46869 KH |
4594 | } |
4595 | ||
4596 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
4597 | Check if a text is encoded in BIG5. If it is, return | |
df7492f9 | 4598 | CATEGORY_MASK_BIG5, else return 0. */ |
4ed46869 | 4599 | |
0a28aafb | 4600 | static int |
cf84bb53 JB |
4601 | detect_coding_big5 (struct coding_system *coding, |
4602 | struct coding_detection_info *detect_info) | |
4ed46869 | 4603 | { |
065e3595 | 4604 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 4605 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
4606 | int multibytep = coding->src_multibyte; |
4607 | int consumed_chars = 0; | |
4608 | int found = 0; | |
b73bfc1c | 4609 | int c; |
fa42c37f | 4610 | |
ff0dacd7 | 4611 | detect_info->checked |= CATEGORY_MASK_BIG5; |
df7492f9 KH |
4612 | /* A coding system of this category is always ASCII compatible. */ |
4613 | src += coding->head_ascii; | |
fa42c37f | 4614 | |
b73bfc1c | 4615 | while (1) |
fa42c37f | 4616 | { |
065e3595 | 4617 | src_base = src; |
df7492f9 KH |
4618 | ONE_MORE_BYTE (c); |
4619 | if (c < 0x80) | |
fa42c37f | 4620 | continue; |
df7492f9 | 4621 | if (c >= 0xA1) |
fa42c37f | 4622 | { |
df7492f9 KH |
4623 | ONE_MORE_BYTE (c); |
4624 | if (c < 0x40 || (c >= 0x7F && c <= 0xA0)) | |
fa42c37f | 4625 | return 0; |
ff0dacd7 | 4626 | found = CATEGORY_MASK_BIG5; |
fa42c37f | 4627 | } |
df7492f9 KH |
4628 | else |
4629 | break; | |
fa42c37f | 4630 | } |
ff0dacd7 | 4631 | detect_info->rejected |= CATEGORY_MASK_BIG5; |
fa42c37f | 4632 | return 0; |
fa42c37f | 4633 | |
df7492f9 | 4634 | no_more_source: |
065e3595 | 4635 | if (src_base < src && coding->mode & CODING_MODE_LAST_BLOCK) |
89528eb3 | 4636 | { |
ff0dacd7 | 4637 | detect_info->rejected |= CATEGORY_MASK_BIG5; |
89528eb3 KH |
4638 | return 0; |
4639 | } | |
ff0dacd7 KH |
4640 | detect_info->found |= found; |
4641 | return 1; | |
fa42c37f KH |
4642 | } |
4643 | ||
4ed46869 KH |
4644 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". |
4645 | If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */ | |
fa42c37f | 4646 | |
b73bfc1c | 4647 | static void |
971de7fb | 4648 | decode_coding_sjis (struct coding_system *coding) |
4ed46869 | 4649 | { |
8f924df7 KH |
4650 | const unsigned char *src = coding->source + coding->consumed; |
4651 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
4652 | const unsigned char *src_base; | |
69a80ea3 | 4653 | int *charbuf = coding->charbuf + coding->charbuf_used; |
ad1746f5 | 4654 | /* We may produce one charset annotation in one loop and one more at |
df80c7f0 | 4655 | the end. */ |
69a80ea3 | 4656 | int *charbuf_end |
df80c7f0 | 4657 | = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 2); |
df7492f9 KH |
4658 | int consumed_chars = 0, consumed_chars_base; |
4659 | int multibytep = coding->src_multibyte; | |
4660 | struct charset *charset_roman, *charset_kanji, *charset_kana; | |
57a47f8a | 4661 | struct charset *charset_kanji2; |
24a73b0a | 4662 | Lisp_Object attrs, charset_list, val; |
ff0dacd7 KH |
4663 | int char_offset = coding->produced_char; |
4664 | int last_offset = char_offset; | |
4665 | int last_id = charset_ascii; | |
2735d060 | 4666 | int eol_dos = |
0a9564cb | 4667 | !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); |
119852e7 | 4668 | int byte_after_cr = -1; |
a5d301df | 4669 | |
24a73b0a | 4670 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4671 | |
4672 | val = charset_list; | |
4673 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
89528eb3 | 4674 | charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
57a47f8a KH |
4675 | charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
4676 | charset_kanji2 = NILP (val) ? NULL : CHARSET_FROM_ID (XINT (XCAR (val))); | |
fa42c37f | 4677 | |
b73bfc1c | 4678 | while (1) |
4ed46869 | 4679 | { |
df7492f9 | 4680 | int c, c1; |
24a73b0a | 4681 | struct charset *charset; |
fa42c37f | 4682 | |
b73bfc1c | 4683 | src_base = src; |
df7492f9 | 4684 | consumed_chars_base = consumed_chars; |
fa42c37f | 4685 | |
df7492f9 | 4686 | if (charbuf >= charbuf_end) |
b71f6f73 KH |
4687 | { |
4688 | if (byte_after_cr >= 0) | |
4689 | src_base--; | |
4690 | break; | |
4691 | } | |
df7492f9 | 4692 | |
119852e7 KH |
4693 | if (byte_after_cr >= 0) |
4694 | c = byte_after_cr, byte_after_cr = -1; | |
4695 | else | |
4696 | ONE_MORE_BYTE (c); | |
065e3595 KH |
4697 | if (c < 0) |
4698 | goto invalid_code; | |
24a73b0a | 4699 | if (c < 0x80) |
119852e7 | 4700 | { |
2735d060 | 4701 | if (eol_dos && c == '\r') |
119852e7 KH |
4702 | ONE_MORE_BYTE (byte_after_cr); |
4703 | charset = charset_roman; | |
4704 | } | |
57a47f8a | 4705 | else if (c == 0x80 || c == 0xA0) |
8e921c4b | 4706 | goto invalid_code; |
57a47f8a KH |
4707 | else if (c >= 0xA1 && c <= 0xDF) |
4708 | { | |
4709 | /* SJIS -> JISX0201-Kana */ | |
4710 | c &= 0x7F; | |
4711 | charset = charset_kana; | |
4712 | } | |
4713 | else if (c <= 0xEF) | |
df7492f9 | 4714 | { |
57a47f8a KH |
4715 | /* SJIS -> JISX0208 */ |
4716 | ONE_MORE_BYTE (c1); | |
4717 | if (c1 < 0x40 || c1 == 0x7F || c1 > 0xFC) | |
24a73b0a | 4718 | goto invalid_code; |
57a47f8a KH |
4719 | c = (c << 8) | c1; |
4720 | SJIS_TO_JIS (c); | |
4721 | charset = charset_kanji; | |
4722 | } | |
4723 | else if (c <= 0xFC && charset_kanji2) | |
4724 | { | |
c6876370 | 4725 | /* SJIS -> JISX0213-2 */ |
57a47f8a KH |
4726 | ONE_MORE_BYTE (c1); |
4727 | if (c1 < 0x40 || c1 == 0x7F || c1 > 0xFC) | |
24a73b0a | 4728 | goto invalid_code; |
57a47f8a KH |
4729 | c = (c << 8) | c1; |
4730 | SJIS_TO_JIS2 (c); | |
4731 | charset = charset_kanji2; | |
df7492f9 | 4732 | } |
57a47f8a KH |
4733 | else |
4734 | goto invalid_code; | |
24a73b0a KH |
4735 | if (charset->id != charset_ascii |
4736 | && last_id != charset->id) | |
4737 | { | |
4738 | if (last_id != charset_ascii) | |
69a80ea3 | 4739 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
24a73b0a KH |
4740 | last_id = charset->id; |
4741 | last_offset = char_offset; | |
4742 | } | |
4743 | CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c); | |
df7492f9 | 4744 | *charbuf++ = c; |
ff0dacd7 | 4745 | char_offset++; |
df7492f9 | 4746 | continue; |
b73bfc1c | 4747 | |
df7492f9 KH |
4748 | invalid_code: |
4749 | src = src_base; | |
4750 | consumed_chars = consumed_chars_base; | |
4751 | ONE_MORE_BYTE (c); | |
065e3595 | 4752 | *charbuf++ = c < 0 ? -c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 4753 | char_offset++; |
df7492f9 KH |
4754 | coding->errors++; |
4755 | } | |
fa42c37f | 4756 | |
df7492f9 | 4757 | no_more_source: |
ff0dacd7 | 4758 | if (last_id != charset_ascii) |
69a80ea3 | 4759 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
4760 | coding->consumed_char += consumed_chars_base; |
4761 | coding->consumed = src_base - coding->source; | |
4762 | coding->charbuf_used = charbuf - coding->charbuf; | |
fa42c37f KH |
4763 | } |
4764 | ||
b73bfc1c | 4765 | static void |
971de7fb | 4766 | decode_coding_big5 (struct coding_system *coding) |
4ed46869 | 4767 | { |
8f924df7 KH |
4768 | const unsigned char *src = coding->source + coding->consumed; |
4769 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
4770 | const unsigned char *src_base; | |
69a80ea3 | 4771 | int *charbuf = coding->charbuf + coding->charbuf_used; |
ad1746f5 | 4772 | /* We may produce one charset annotation in one loop and one more at |
df80c7f0 | 4773 | the end. */ |
69a80ea3 | 4774 | int *charbuf_end |
df80c7f0 | 4775 | = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 2); |
df7492f9 KH |
4776 | int consumed_chars = 0, consumed_chars_base; |
4777 | int multibytep = coding->src_multibyte; | |
4778 | struct charset *charset_roman, *charset_big5; | |
24a73b0a | 4779 | Lisp_Object attrs, charset_list, val; |
ff0dacd7 KH |
4780 | int char_offset = coding->produced_char; |
4781 | int last_offset = char_offset; | |
4782 | int last_id = charset_ascii; | |
2735d060 | 4783 | int eol_dos = |
0a9564cb | 4784 | !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); |
119852e7 | 4785 | int byte_after_cr = -1; |
df7492f9 | 4786 | |
24a73b0a | 4787 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4788 | val = charset_list; |
4789 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
4790 | charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val))); | |
4ed46869 | 4791 | |
b73bfc1c | 4792 | while (1) |
4ed46869 | 4793 | { |
df7492f9 | 4794 | int c, c1; |
24a73b0a | 4795 | struct charset *charset; |
b73bfc1c KH |
4796 | |
4797 | src_base = src; | |
df7492f9 KH |
4798 | consumed_chars_base = consumed_chars; |
4799 | ||
4800 | if (charbuf >= charbuf_end) | |
b71f6f73 KH |
4801 | { |
4802 | if (byte_after_cr >= 0) | |
4803 | src_base--; | |
4804 | break; | |
4805 | } | |
df7492f9 | 4806 | |
119852e7 | 4807 | if (byte_after_cr >= 0) |
14daee73 | 4808 | c = byte_after_cr, byte_after_cr = -1; |
119852e7 KH |
4809 | else |
4810 | ONE_MORE_BYTE (c); | |
b73bfc1c | 4811 | |
065e3595 KH |
4812 | if (c < 0) |
4813 | goto invalid_code; | |
24a73b0a | 4814 | if (c < 0x80) |
119852e7 | 4815 | { |
2735d060 | 4816 | if (eol_dos && c == '\r') |
119852e7 KH |
4817 | ONE_MORE_BYTE (byte_after_cr); |
4818 | charset = charset_roman; | |
4819 | } | |
24a73b0a | 4820 | else |
4ed46869 | 4821 | { |
24a73b0a KH |
4822 | /* BIG5 -> Big5 */ |
4823 | if (c < 0xA1 || c > 0xFE) | |
4824 | goto invalid_code; | |
4825 | ONE_MORE_BYTE (c1); | |
4826 | if (c1 < 0x40 || (c1 > 0x7E && c1 < 0xA1) || c1 > 0xFE) | |
4827 | goto invalid_code; | |
4828 | c = c << 8 | c1; | |
4829 | charset = charset_big5; | |
4ed46869 | 4830 | } |
24a73b0a KH |
4831 | if (charset->id != charset_ascii |
4832 | && last_id != charset->id) | |
df7492f9 | 4833 | { |
24a73b0a | 4834 | if (last_id != charset_ascii) |
69a80ea3 | 4835 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
24a73b0a KH |
4836 | last_id = charset->id; |
4837 | last_offset = char_offset; | |
4ed46869 | 4838 | } |
24a73b0a | 4839 | CODING_DECODE_CHAR (coding, src, src_base, src_end, charset, c, c); |
df7492f9 | 4840 | *charbuf++ = c; |
ff0dacd7 | 4841 | char_offset++; |
fb88bf2d KH |
4842 | continue; |
4843 | ||
df7492f9 | 4844 | invalid_code: |
4ed46869 | 4845 | src = src_base; |
df7492f9 KH |
4846 | consumed_chars = consumed_chars_base; |
4847 | ONE_MORE_BYTE (c); | |
065e3595 | 4848 | *charbuf++ = c < 0 ? -c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 4849 | char_offset++; |
df7492f9 | 4850 | coding->errors++; |
fb88bf2d | 4851 | } |
d46c5b12 | 4852 | |
df7492f9 | 4853 | no_more_source: |
ff0dacd7 | 4854 | if (last_id != charset_ascii) |
69a80ea3 | 4855 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
4856 | coding->consumed_char += consumed_chars_base; |
4857 | coding->consumed = src_base - coding->source; | |
4858 | coding->charbuf_used = charbuf - coding->charbuf; | |
4ed46869 KH |
4859 | } |
4860 | ||
4861 | /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". | |
b73bfc1c KH |
4862 | This function can encode charsets `ascii', `katakana-jisx0201', |
4863 | `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We | |
4864 | are sure that all these charsets are registered as official charset | |
4ed46869 KH |
4865 | (i.e. do not have extended leading-codes). Characters of other |
4866 | charsets are produced without any encoding. If SJIS_P is 1, encode | |
4867 | SJIS text, else encode BIG5 text. */ | |
4868 | ||
df7492f9 | 4869 | static int |
971de7fb | 4870 | encode_coding_sjis (struct coding_system *coding) |
4ed46869 | 4871 | { |
df7492f9 KH |
4872 | int multibytep = coding->dst_multibyte; |
4873 | int *charbuf = coding->charbuf; | |
4874 | int *charbuf_end = charbuf + coding->charbuf_used; | |
4875 | unsigned char *dst = coding->destination + coding->produced; | |
4876 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
4877 | int safe_room = 4; | |
4878 | int produced_chars = 0; | |
24a73b0a | 4879 | Lisp_Object attrs, charset_list, val; |
df7492f9 KH |
4880 | int ascii_compatible; |
4881 | struct charset *charset_roman, *charset_kanji, *charset_kana; | |
57a47f8a | 4882 | struct charset *charset_kanji2; |
df7492f9 | 4883 | int c; |
a5d301df | 4884 | |
24a73b0a | 4885 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4886 | val = charset_list; |
4887 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
4888 | charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
57a47f8a KH |
4889 | charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
4890 | charset_kanji2 = NILP (val) ? NULL : CHARSET_FROM_ID (XINT (XCAR (val))); | |
4ed46869 | 4891 | |
df7492f9 | 4892 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); |
93dec019 | 4893 | |
df7492f9 KH |
4894 | while (charbuf < charbuf_end) |
4895 | { | |
4896 | ASSURE_DESTINATION (safe_room); | |
4897 | c = *charbuf++; | |
b73bfc1c | 4898 | /* Now encode the character C. */ |
df7492f9 KH |
4899 | if (ASCII_CHAR_P (c) && ascii_compatible) |
4900 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d KH |
4901 | else if (CHAR_BYTE8_P (c)) |
4902 | { | |
4903 | c = CHAR_TO_BYTE8 (c); | |
4904 | EMIT_ONE_BYTE (c); | |
4905 | } | |
df7492f9 | 4906 | else |
b73bfc1c | 4907 | { |
df7492f9 KH |
4908 | unsigned code; |
4909 | struct charset *charset = char_charset (c, charset_list, &code); | |
4910 | ||
4911 | if (!charset) | |
4ed46869 | 4912 | { |
41cbe562 | 4913 | if (coding->mode & CODING_MODE_SAFE_ENCODING) |
b73bfc1c | 4914 | { |
41cbe562 KH |
4915 | code = CODING_INHIBIT_CHARACTER_SUBSTITUTION; |
4916 | charset = CHARSET_FROM_ID (charset_ascii); | |
b73bfc1c | 4917 | } |
41cbe562 | 4918 | else |
b73bfc1c | 4919 | { |
41cbe562 KH |
4920 | c = coding->default_char; |
4921 | charset = char_charset (c, charset_list, &code); | |
b73bfc1c | 4922 | } |
b73bfc1c | 4923 | } |
df7492f9 KH |
4924 | if (code == CHARSET_INVALID_CODE (charset)) |
4925 | abort (); | |
4926 | if (charset == charset_kanji) | |
4927 | { | |
4928 | int c1, c2; | |
4929 | JIS_TO_SJIS (code); | |
4930 | c1 = code >> 8, c2 = code & 0xFF; | |
4931 | EMIT_TWO_BYTES (c1, c2); | |
4932 | } | |
4933 | else if (charset == charset_kana) | |
4934 | EMIT_ONE_BYTE (code | 0x80); | |
57a47f8a KH |
4935 | else if (charset_kanji2 && charset == charset_kanji2) |
4936 | { | |
4937 | int c1, c2; | |
4938 | ||
4939 | c1 = code >> 8; | |
f07190ca KH |
4940 | if (c1 == 0x21 || (c1 >= 0x23 && c1 <= 0x25) |
4941 | || c1 == 0x28 | |
57a47f8a KH |
4942 | || (c1 >= 0x2C && c1 <= 0x2F) || c1 >= 0x6E) |
4943 | { | |
4944 | JIS_TO_SJIS2 (code); | |
4945 | c1 = code >> 8, c2 = code & 0xFF; | |
4946 | EMIT_TWO_BYTES (c1, c2); | |
4947 | } | |
4948 | else | |
4949 | EMIT_ONE_ASCII_BYTE (code & 0x7F); | |
4950 | } | |
df7492f9 KH |
4951 | else |
4952 | EMIT_ONE_ASCII_BYTE (code & 0x7F); | |
4953 | } | |
4954 | } | |
065e3595 | 4955 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
4956 | coding->produced_char += produced_chars; |
4957 | coding->produced = dst - coding->destination; | |
4958 | return 0; | |
4959 | } | |
4960 | ||
4961 | static int | |
971de7fb | 4962 | encode_coding_big5 (struct coding_system *coding) |
df7492f9 KH |
4963 | { |
4964 | int multibytep = coding->dst_multibyte; | |
4965 | int *charbuf = coding->charbuf; | |
4966 | int *charbuf_end = charbuf + coding->charbuf_used; | |
4967 | unsigned char *dst = coding->destination + coding->produced; | |
4968 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
4969 | int safe_room = 4; | |
4970 | int produced_chars = 0; | |
24a73b0a | 4971 | Lisp_Object attrs, charset_list, val; |
df7492f9 KH |
4972 | int ascii_compatible; |
4973 | struct charset *charset_roman, *charset_big5; | |
4974 | int c; | |
4975 | ||
24a73b0a | 4976 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 KH |
4977 | val = charset_list; |
4978 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
4979 | charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val))); | |
4980 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); | |
4981 | ||
4982 | while (charbuf < charbuf_end) | |
4983 | { | |
4984 | ASSURE_DESTINATION (safe_room); | |
4985 | c = *charbuf++; | |
4986 | /* Now encode the character C. */ | |
4987 | if (ASCII_CHAR_P (c) && ascii_compatible) | |
4988 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d KH |
4989 | else if (CHAR_BYTE8_P (c)) |
4990 | { | |
4991 | c = CHAR_TO_BYTE8 (c); | |
4992 | EMIT_ONE_BYTE (c); | |
b73bfc1c KH |
4993 | } |
4994 | else | |
4995 | { | |
df7492f9 KH |
4996 | unsigned code; |
4997 | struct charset *charset = char_charset (c, charset_list, &code); | |
4998 | ||
4999 | if (! charset) | |
b73bfc1c | 5000 | { |
41cbe562 | 5001 | if (coding->mode & CODING_MODE_SAFE_ENCODING) |
b73bfc1c | 5002 | { |
41cbe562 KH |
5003 | code = CODING_INHIBIT_CHARACTER_SUBSTITUTION; |
5004 | charset = CHARSET_FROM_ID (charset_ascii); | |
b73bfc1c | 5005 | } |
41cbe562 | 5006 | else |
0eecad43 | 5007 | { |
41cbe562 KH |
5008 | c = coding->default_char; |
5009 | charset = char_charset (c, charset_list, &code); | |
0eecad43 | 5010 | } |
4ed46869 | 5011 | } |
df7492f9 KH |
5012 | if (code == CHARSET_INVALID_CODE (charset)) |
5013 | abort (); | |
5014 | if (charset == charset_big5) | |
b73bfc1c | 5015 | { |
df7492f9 KH |
5016 | int c1, c2; |
5017 | ||
5018 | c1 = code >> 8, c2 = code & 0xFF; | |
5019 | EMIT_TWO_BYTES (c1, c2); | |
b73bfc1c | 5020 | } |
df7492f9 KH |
5021 | else |
5022 | EMIT_ONE_ASCII_BYTE (code & 0x7F); | |
4ed46869 | 5023 | } |
4ed46869 | 5024 | } |
065e3595 | 5025 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
5026 | coding->produced_char += produced_chars; |
5027 | coding->produced = dst - coding->destination; | |
5028 | return 0; | |
4ed46869 KH |
5029 | } |
5030 | ||
5031 | \f | |
df7492f9 | 5032 | /*** 10. CCL handlers ***/ |
1397dc18 KH |
5033 | |
5034 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | |
5035 | Check if a text is encoded in a coding system of which | |
5036 | encoder/decoder are written in CCL program. If it is, return | |
df7492f9 | 5037 | CATEGORY_MASK_CCL, else return 0. */ |
1397dc18 | 5038 | |
0a28aafb | 5039 | static int |
cf84bb53 JB |
5040 | detect_coding_ccl (struct coding_system *coding, |
5041 | struct coding_detection_info *detect_info) | |
1397dc18 | 5042 | { |
065e3595 | 5043 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 5044 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
5045 | int multibytep = coding->src_multibyte; |
5046 | int consumed_chars = 0; | |
5047 | int found = 0; | |
0e219d54 | 5048 | unsigned char *valids; |
df7492f9 KH |
5049 | int head_ascii = coding->head_ascii; |
5050 | Lisp_Object attrs; | |
5051 | ||
ff0dacd7 KH |
5052 | detect_info->checked |= CATEGORY_MASK_CCL; |
5053 | ||
df7492f9 | 5054 | coding = &coding_categories[coding_category_ccl]; |
0e219d54 | 5055 | valids = CODING_CCL_VALIDS (coding); |
df7492f9 KH |
5056 | attrs = CODING_ID_ATTRS (coding->id); |
5057 | if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
5058 | src += head_ascii; | |
1397dc18 | 5059 | |
b73bfc1c | 5060 | while (1) |
1397dc18 | 5061 | { |
df7492f9 | 5062 | int c; |
065e3595 KH |
5063 | |
5064 | src_base = src; | |
df7492f9 | 5065 | ONE_MORE_BYTE (c); |
065e3595 | 5066 | if (c < 0 || ! valids[c]) |
df7492f9 | 5067 | break; |
ff0dacd7 KH |
5068 | if ((valids[c] > 1)) |
5069 | found = CATEGORY_MASK_CCL; | |
df7492f9 | 5070 | } |
ff0dacd7 | 5071 | detect_info->rejected |= CATEGORY_MASK_CCL; |
df7492f9 KH |
5072 | return 0; |
5073 | ||
5074 | no_more_source: | |
ff0dacd7 KH |
5075 | detect_info->found |= found; |
5076 | return 1; | |
df7492f9 KH |
5077 | } |
5078 | ||
5079 | static void | |
971de7fb | 5080 | decode_coding_ccl (struct coding_system *coding) |
df7492f9 | 5081 | { |
7c78e542 | 5082 | const unsigned char *src = coding->source + coding->consumed; |
8f924df7 | 5083 | const unsigned char *src_end = coding->source + coding->src_bytes; |
69a80ea3 KH |
5084 | int *charbuf = coding->charbuf + coding->charbuf_used; |
5085 | int *charbuf_end = coding->charbuf + coding->charbuf_size; | |
df7492f9 KH |
5086 | int consumed_chars = 0; |
5087 | int multibytep = coding->src_multibyte; | |
d0396581 | 5088 | struct ccl_program *ccl = &coding->spec.ccl->ccl; |
df7492f9 | 5089 | int source_charbuf[1024]; |
fbdc1721 | 5090 | int source_byteidx[1025]; |
24a73b0a | 5091 | Lisp_Object attrs, charset_list; |
df7492f9 | 5092 | |
24a73b0a | 5093 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 | 5094 | |
d0396581 | 5095 | while (1) |
df7492f9 | 5096 | { |
7c78e542 | 5097 | const unsigned char *p = src; |
df7492f9 KH |
5098 | int i = 0; |
5099 | ||
5100 | if (multibytep) | |
fbdc1721 KH |
5101 | { |
5102 | while (i < 1024 && p < src_end) | |
5103 | { | |
5104 | source_byteidx[i] = p - src; | |
5105 | source_charbuf[i++] = STRING_CHAR_ADVANCE (p); | |
5106 | } | |
5107 | source_byteidx[i] = p - src; | |
5108 | } | |
df7492f9 KH |
5109 | else |
5110 | while (i < 1024 && p < src_end) | |
5111 | source_charbuf[i++] = *p++; | |
8f924df7 | 5112 | |
df7492f9 | 5113 | if (p == src_end && coding->mode & CODING_MODE_LAST_BLOCK) |
d0396581 KH |
5114 | ccl->last_block = 1; |
5115 | ccl_driver (ccl, source_charbuf, charbuf, i, charbuf_end - charbuf, | |
5116 | charset_list); | |
5117 | charbuf += ccl->produced; | |
fbdc1721 | 5118 | if (multibytep) |
d0396581 | 5119 | src += source_byteidx[ccl->consumed]; |
df7492f9 | 5120 | else |
d0396581 KH |
5121 | src += ccl->consumed; |
5122 | consumed_chars += ccl->consumed; | |
5123 | if (p == src_end || ccl->status != CCL_STAT_SUSPEND_BY_SRC) | |
df7492f9 KH |
5124 | break; |
5125 | } | |
5126 | ||
d0396581 | 5127 | switch (ccl->status) |
df7492f9 KH |
5128 | { |
5129 | case CCL_STAT_SUSPEND_BY_SRC: | |
065e3595 | 5130 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC); |
df7492f9 KH |
5131 | break; |
5132 | case CCL_STAT_SUSPEND_BY_DST: | |
d0396581 | 5133 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_DST); |
df7492f9 KH |
5134 | break; |
5135 | case CCL_STAT_QUIT: | |
5136 | case CCL_STAT_INVALID_CMD: | |
065e3595 | 5137 | record_conversion_result (coding, CODING_RESULT_INTERRUPT); |
df7492f9 KH |
5138 | break; |
5139 | default: | |
065e3595 | 5140 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
5141 | break; |
5142 | } | |
5143 | coding->consumed_char += consumed_chars; | |
5144 | coding->consumed = src - coding->source; | |
5145 | coding->charbuf_used = charbuf - coding->charbuf; | |
5146 | } | |
5147 | ||
5148 | static int | |
971de7fb | 5149 | encode_coding_ccl (struct coding_system *coding) |
df7492f9 | 5150 | { |
fb608df3 | 5151 | struct ccl_program *ccl = &coding->spec.ccl->ccl; |
df7492f9 KH |
5152 | int multibytep = coding->dst_multibyte; |
5153 | int *charbuf = coding->charbuf; | |
5154 | int *charbuf_end = charbuf + coding->charbuf_used; | |
5155 | unsigned char *dst = coding->destination + coding->produced; | |
5156 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
df7492f9 KH |
5157 | int destination_charbuf[1024]; |
5158 | int i, produced_chars = 0; | |
24a73b0a | 5159 | Lisp_Object attrs, charset_list; |
df7492f9 | 5160 | |
24a73b0a | 5161 | CODING_GET_INFO (coding, attrs, charset_list); |
fb608df3 KH |
5162 | if (coding->consumed_char == coding->src_chars |
5163 | && coding->mode & CODING_MODE_LAST_BLOCK) | |
5164 | ccl->last_block = 1; | |
df7492f9 | 5165 | |
8cffd3e7 | 5166 | while (charbuf < charbuf_end) |
df7492f9 | 5167 | { |
fb608df3 | 5168 | ccl_driver (ccl, charbuf, destination_charbuf, |
8cffd3e7 | 5169 | charbuf_end - charbuf, 1024, charset_list); |
df7492f9 | 5170 | if (multibytep) |
8cffd3e7 | 5171 | { |
fb608df3 KH |
5172 | ASSURE_DESTINATION (ccl->produced * 2); |
5173 | for (i = 0; i < ccl->produced; i++) | |
8cffd3e7 KH |
5174 | EMIT_ONE_BYTE (destination_charbuf[i] & 0xFF); |
5175 | } | |
df7492f9 KH |
5176 | else |
5177 | { | |
fb608df3 KH |
5178 | ASSURE_DESTINATION (ccl->produced); |
5179 | for (i = 0; i < ccl->produced; i++) | |
df7492f9 | 5180 | *dst++ = destination_charbuf[i] & 0xFF; |
fb608df3 | 5181 | produced_chars += ccl->produced; |
df7492f9 | 5182 | } |
fb608df3 KH |
5183 | charbuf += ccl->consumed; |
5184 | if (ccl->status == CCL_STAT_QUIT | |
5185 | || ccl->status == CCL_STAT_INVALID_CMD) | |
8cffd3e7 | 5186 | break; |
df7492f9 KH |
5187 | } |
5188 | ||
fb608df3 | 5189 | switch (ccl->status) |
df7492f9 KH |
5190 | { |
5191 | case CCL_STAT_SUSPEND_BY_SRC: | |
065e3595 | 5192 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC); |
df7492f9 KH |
5193 | break; |
5194 | case CCL_STAT_SUSPEND_BY_DST: | |
065e3595 | 5195 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_DST); |
df7492f9 KH |
5196 | break; |
5197 | case CCL_STAT_QUIT: | |
5198 | case CCL_STAT_INVALID_CMD: | |
065e3595 | 5199 | record_conversion_result (coding, CODING_RESULT_INTERRUPT); |
df7492f9 KH |
5200 | break; |
5201 | default: | |
065e3595 | 5202 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 | 5203 | break; |
1397dc18 | 5204 | } |
df7492f9 KH |
5205 | |
5206 | coding->produced_char += produced_chars; | |
5207 | coding->produced = dst - coding->destination; | |
5208 | return 0; | |
1397dc18 KH |
5209 | } |
5210 | ||
df7492f9 | 5211 | |
1397dc18 | 5212 | \f |
df7492f9 | 5213 | /*** 10, 11. no-conversion handlers ***/ |
4ed46869 | 5214 | |
b73bfc1c | 5215 | /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */ |
4ed46869 | 5216 | |
b73bfc1c | 5217 | static void |
971de7fb | 5218 | decode_coding_raw_text (struct coding_system *coding) |
4ed46869 | 5219 | { |
2735d060 | 5220 | int eol_dos = |
0a9564cb | 5221 | !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); |
119852e7 | 5222 | |
df7492f9 | 5223 | coding->chars_at_source = 1; |
119852e7 KH |
5224 | coding->consumed_char = coding->src_chars; |
5225 | coding->consumed = coding->src_bytes; | |
2735d060 | 5226 | if (eol_dos && coding->source[coding->src_bytes - 1] == '\r') |
119852e7 KH |
5227 | { |
5228 | coding->consumed_char--; | |
5229 | coding->consumed--; | |
5230 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_SRC); | |
5231 | } | |
5232 | else | |
5233 | record_conversion_result (coding, CODING_RESULT_SUCCESS); | |
df7492f9 | 5234 | } |
4ed46869 | 5235 | |
df7492f9 | 5236 | static int |
971de7fb | 5237 | encode_coding_raw_text (struct coding_system *coding) |
df7492f9 KH |
5238 | { |
5239 | int multibytep = coding->dst_multibyte; | |
5240 | int *charbuf = coding->charbuf; | |
5241 | int *charbuf_end = coding->charbuf + coding->charbuf_used; | |
5242 | unsigned char *dst = coding->destination + coding->produced; | |
5243 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
a0ed9b27 | 5244 | int produced_chars = 0; |
b73bfc1c KH |
5245 | int c; |
5246 | ||
df7492f9 | 5247 | if (multibytep) |
b73bfc1c | 5248 | { |
df7492f9 | 5249 | int safe_room = MAX_MULTIBYTE_LENGTH * 2; |
4ed46869 | 5250 | |
df7492f9 KH |
5251 | if (coding->src_multibyte) |
5252 | while (charbuf < charbuf_end) | |
5253 | { | |
5254 | ASSURE_DESTINATION (safe_room); | |
5255 | c = *charbuf++; | |
5256 | if (ASCII_CHAR_P (c)) | |
5257 | EMIT_ONE_ASCII_BYTE (c); | |
5258 | else if (CHAR_BYTE8_P (c)) | |
5259 | { | |
5260 | c = CHAR_TO_BYTE8 (c); | |
5261 | EMIT_ONE_BYTE (c); | |
5262 | } | |
5263 | else | |
5264 | { | |
5265 | unsigned char str[MAX_MULTIBYTE_LENGTH], *p0 = str, *p1 = str; | |
93dec019 | 5266 | |
df7492f9 | 5267 | CHAR_STRING_ADVANCE (c, p1); |
8abc3f12 | 5268 | do |
9d123124 KH |
5269 | { |
5270 | EMIT_ONE_BYTE (*p0); | |
5271 | p0++; | |
5272 | } | |
8abc3f12 | 5273 | while (p0 < p1); |
df7492f9 KH |
5274 | } |
5275 | } | |
b73bfc1c | 5276 | else |
df7492f9 KH |
5277 | while (charbuf < charbuf_end) |
5278 | { | |
5279 | ASSURE_DESTINATION (safe_room); | |
5280 | c = *charbuf++; | |
5281 | EMIT_ONE_BYTE (c); | |
5282 | } | |
5283 | } | |
5284 | else | |
4ed46869 | 5285 | { |
df7492f9 | 5286 | if (coding->src_multibyte) |
d46c5b12 | 5287 | { |
df7492f9 KH |
5288 | int safe_room = MAX_MULTIBYTE_LENGTH; |
5289 | ||
5290 | while (charbuf < charbuf_end) | |
d46c5b12 | 5291 | { |
df7492f9 KH |
5292 | ASSURE_DESTINATION (safe_room); |
5293 | c = *charbuf++; | |
5294 | if (ASCII_CHAR_P (c)) | |
5295 | *dst++ = c; | |
5296 | else if (CHAR_BYTE8_P (c)) | |
5297 | *dst++ = CHAR_TO_BYTE8 (c); | |
b73bfc1c | 5298 | else |
df7492f9 | 5299 | CHAR_STRING_ADVANCE (c, dst); |
d46c5b12 KH |
5300 | } |
5301 | } | |
df7492f9 KH |
5302 | else |
5303 | { | |
5304 | ASSURE_DESTINATION (charbuf_end - charbuf); | |
5305 | while (charbuf < charbuf_end && dst < dst_end) | |
5306 | *dst++ = *charbuf++; | |
8f924df7 | 5307 | } |
319a3947 | 5308 | produced_chars = dst - (coding->destination + coding->produced); |
4ed46869 | 5309 | } |
065e3595 | 5310 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
a0ed9b27 | 5311 | coding->produced_char += produced_chars; |
df7492f9 KH |
5312 | coding->produced = dst - coding->destination; |
5313 | return 0; | |
4ed46869 KH |
5314 | } |
5315 | ||
ff0dacd7 KH |
5316 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". |
5317 | Check if a text is encoded in a charset-based coding system. If it | |
5318 | is, return 1, else return 0. */ | |
5319 | ||
0a28aafb | 5320 | static int |
cf84bb53 JB |
5321 | detect_coding_charset (struct coding_system *coding, |
5322 | struct coding_detection_info *detect_info) | |
1397dc18 | 5323 | { |
065e3595 | 5324 | const unsigned char *src = coding->source, *src_base; |
8f924df7 | 5325 | const unsigned char *src_end = coding->source + coding->src_bytes; |
df7492f9 KH |
5326 | int multibytep = coding->src_multibyte; |
5327 | int consumed_chars = 0; | |
07295713 | 5328 | Lisp_Object attrs, valids, name; |
584948ac | 5329 | int found = 0; |
716b3fa0 | 5330 | int head_ascii = coding->head_ascii; |
07295713 | 5331 | int check_latin_extra = 0; |
1397dc18 | 5332 | |
ff0dacd7 KH |
5333 | detect_info->checked |= CATEGORY_MASK_CHARSET; |
5334 | ||
df7492f9 KH |
5335 | coding = &coding_categories[coding_category_charset]; |
5336 | attrs = CODING_ID_ATTRS (coding->id); | |
5337 | valids = AREF (attrs, coding_attr_charset_valids); | |
07295713 | 5338 | name = CODING_ID_NAME (coding->id); |
51b59d79 | 5339 | if (strncmp (SSDATA (SYMBOL_NAME (name)), |
237aabf4 | 5340 | "iso-8859-", sizeof ("iso-8859-") - 1) == 0 |
51b59d79 | 5341 | || strncmp (SSDATA (SYMBOL_NAME (name)), |
237aabf4 | 5342 | "iso-latin-", sizeof ("iso-latin-") - 1) == 0) |
07295713 | 5343 | check_latin_extra = 1; |
237aabf4 | 5344 | |
df7492f9 | 5345 | if (! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) |
716b3fa0 | 5346 | src += head_ascii; |
1397dc18 | 5347 | |
b73bfc1c | 5348 | while (1) |
1397dc18 | 5349 | { |
df7492f9 | 5350 | int c; |
716b3fa0 KH |
5351 | Lisp_Object val; |
5352 | struct charset *charset; | |
5353 | int dim, idx; | |
1397dc18 | 5354 | |
065e3595 | 5355 | src_base = src; |
df7492f9 | 5356 | ONE_MORE_BYTE (c); |
065e3595 KH |
5357 | if (c < 0) |
5358 | continue; | |
716b3fa0 KH |
5359 | val = AREF (valids, c); |
5360 | if (NILP (val)) | |
df7492f9 | 5361 | break; |
584948ac | 5362 | if (c >= 0x80) |
07295713 KH |
5363 | { |
5364 | if (c < 0xA0 | |
237aabf4 JR |
5365 | && check_latin_extra |
5366 | && (!VECTORP (Vlatin_extra_code_table) | |
9f0526cb | 5367 | || NILP (XVECTOR (Vlatin_extra_code_table)->contents[c]))) |
07295713 KH |
5368 | break; |
5369 | found = CATEGORY_MASK_CHARSET; | |
5370 | } | |
716b3fa0 KH |
5371 | if (INTEGERP (val)) |
5372 | { | |
5373 | charset = CHARSET_FROM_ID (XFASTINT (val)); | |
5374 | dim = CHARSET_DIMENSION (charset); | |
5375 | for (idx = 1; idx < dim; idx++) | |
5376 | { | |
5377 | if (src == src_end) | |
5378 | goto too_short; | |
5379 | ONE_MORE_BYTE (c); | |
3ed051d4 | 5380 | if (c < charset->code_space[(dim - 1 - idx) * 2] |
716b3fa0 KH |
5381 | || c > charset->code_space[(dim - 1 - idx) * 2 + 1]) |
5382 | break; | |
5383 | } | |
5384 | if (idx < dim) | |
5385 | break; | |
5386 | } | |
5387 | else | |
5388 | { | |
5389 | idx = 1; | |
5390 | for (; CONSP (val); val = XCDR (val)) | |
5391 | { | |
5392 | charset = CHARSET_FROM_ID (XFASTINT (XCAR (val))); | |
5393 | dim = CHARSET_DIMENSION (charset); | |
5394 | while (idx < dim) | |
5395 | { | |
5396 | if (src == src_end) | |
5397 | goto too_short; | |
5398 | ONE_MORE_BYTE (c); | |
5399 | if (c < charset->code_space[(dim - 1 - idx) * 4] | |
5400 | || c > charset->code_space[(dim - 1 - idx) * 4 + 1]) | |
5401 | break; | |
5402 | idx++; | |
5403 | } | |
5404 | if (idx == dim) | |
5405 | { | |
5406 | val = Qnil; | |
5407 | break; | |
5408 | } | |
5409 | } | |
5410 | if (CONSP (val)) | |
5411 | break; | |
5412 | } | |
df7492f9 | 5413 | } |
716b3fa0 | 5414 | too_short: |
ff0dacd7 | 5415 | detect_info->rejected |= CATEGORY_MASK_CHARSET; |
df7492f9 | 5416 | return 0; |
4ed46869 | 5417 | |
df7492f9 | 5418 | no_more_source: |
ff0dacd7 KH |
5419 | detect_info->found |= found; |
5420 | return 1; | |
df7492f9 | 5421 | } |
b73bfc1c | 5422 | |
b73bfc1c | 5423 | static void |
971de7fb | 5424 | decode_coding_charset (struct coding_system *coding) |
4ed46869 | 5425 | { |
8f924df7 KH |
5426 | const unsigned char *src = coding->source + coding->consumed; |
5427 | const unsigned char *src_end = coding->source + coding->src_bytes; | |
5428 | const unsigned char *src_base; | |
69a80ea3 | 5429 | int *charbuf = coding->charbuf + coding->charbuf_used; |
ad1746f5 | 5430 | /* We may produce one charset annotation in one loop and one more at |
df80c7f0 | 5431 | the end. */ |
69a80ea3 | 5432 | int *charbuf_end |
df80c7f0 | 5433 | = coding->charbuf + coding->charbuf_size - (MAX_ANNOTATION_LENGTH * 2); |
df7492f9 KH |
5434 | int consumed_chars = 0, consumed_chars_base; |
5435 | int multibytep = coding->src_multibyte; | |
24a73b0a | 5436 | Lisp_Object attrs, charset_list, valids; |
ff0dacd7 KH |
5437 | int char_offset = coding->produced_char; |
5438 | int last_offset = char_offset; | |
5439 | int last_id = charset_ascii; | |
2735d060 | 5440 | int eol_dos = |
0a9564cb | 5441 | !inhibit_eol_conversion && EQ (CODING_ID_EOL_TYPE (coding->id), Qdos); |
119852e7 | 5442 | int byte_after_cr = -1; |
df7492f9 | 5443 | |
24a73b0a | 5444 | CODING_GET_INFO (coding, attrs, charset_list); |
4eb6d3f1 | 5445 | valids = AREF (attrs, coding_attr_charset_valids); |
b73bfc1c | 5446 | |
df7492f9 | 5447 | while (1) |
4ed46869 | 5448 | { |
4eb6d3f1 | 5449 | int c; |
24a73b0a KH |
5450 | Lisp_Object val; |
5451 | struct charset *charset; | |
5452 | int dim; | |
5453 | int len = 1; | |
5454 | unsigned code; | |
df7492f9 KH |
5455 | |
5456 | src_base = src; | |
5457 | consumed_chars_base = consumed_chars; | |
b73bfc1c | 5458 | |
df7492f9 | 5459 | if (charbuf >= charbuf_end) |
b71f6f73 KH |
5460 | { |
5461 | if (byte_after_cr >= 0) | |
5462 | src_base--; | |
5463 | break; | |
5464 | } | |
df7492f9 | 5465 | |
119852e7 KH |
5466 | if (byte_after_cr >= 0) |
5467 | { | |
5468 | c = byte_after_cr; | |
5469 | byte_after_cr = -1; | |
5470 | } | |
5471 | else | |
5472 | { | |
5473 | ONE_MORE_BYTE (c); | |
2735d060 | 5474 | if (eol_dos && c == '\r') |
119852e7 KH |
5475 | ONE_MORE_BYTE (byte_after_cr); |
5476 | } | |
065e3595 KH |
5477 | if (c < 0) |
5478 | goto invalid_code; | |
24a73b0a KH |
5479 | code = c; |
5480 | ||
5481 | val = AREF (valids, c); | |
1b17adfd | 5482 | if (! INTEGERP (val) && ! CONSP (val)) |
24a73b0a KH |
5483 | goto invalid_code; |
5484 | if (INTEGERP (val)) | |
d46c5b12 | 5485 | { |
24a73b0a KH |
5486 | charset = CHARSET_FROM_ID (XFASTINT (val)); |
5487 | dim = CHARSET_DIMENSION (charset); | |
5488 | while (len < dim) | |
b73bfc1c | 5489 | { |
24a73b0a KH |
5490 | ONE_MORE_BYTE (c); |
5491 | code = (code << 8) | c; | |
5492 | len++; | |
b73bfc1c | 5493 | } |
24a73b0a KH |
5494 | CODING_DECODE_CHAR (coding, src, src_base, src_end, |
5495 | charset, code, c); | |
d46c5b12 | 5496 | } |
df7492f9 | 5497 | else |
d46c5b12 | 5498 | { |
24a73b0a KH |
5499 | /* VAL is a list of charset IDs. It is assured that the |
5500 | list is sorted by charset dimensions (smaller one | |
5501 | comes first). */ | |
5502 | while (CONSP (val)) | |
4eb6d3f1 | 5503 | { |
24a73b0a | 5504 | charset = CHARSET_FROM_ID (XFASTINT (XCAR (val))); |
c7c66a95 | 5505 | dim = CHARSET_DIMENSION (charset); |
f9d71dcd | 5506 | while (len < dim) |
4eb6d3f1 | 5507 | { |
acb2a965 KH |
5508 | ONE_MORE_BYTE (c); |
5509 | code = (code << 8) | c; | |
f9d71dcd | 5510 | len++; |
4eb6d3f1 | 5511 | } |
24a73b0a KH |
5512 | CODING_DECODE_CHAR (coding, src, src_base, |
5513 | src_end, charset, code, c); | |
5514 | if (c >= 0) | |
5515 | break; | |
5516 | val = XCDR (val); | |
ff0dacd7 | 5517 | } |
d46c5b12 | 5518 | } |
24a73b0a KH |
5519 | if (c < 0) |
5520 | goto invalid_code; | |
5521 | if (charset->id != charset_ascii | |
5522 | && last_id != charset->id) | |
5523 | { | |
5524 | if (last_id != charset_ascii) | |
69a80ea3 | 5525 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
24a73b0a KH |
5526 | last_id = charset->id; |
5527 | last_offset = char_offset; | |
5528 | } | |
5529 | ||
df7492f9 | 5530 | *charbuf++ = c; |
ff0dacd7 | 5531 | char_offset++; |
df7492f9 KH |
5532 | continue; |
5533 | ||
5534 | invalid_code: | |
5535 | src = src_base; | |
5536 | consumed_chars = consumed_chars_base; | |
5537 | ONE_MORE_BYTE (c); | |
065e3595 | 5538 | *charbuf++ = c < 0 ? -c : ASCII_BYTE_P (c) ? c : BYTE8_TO_CHAR (c); |
ff0dacd7 | 5539 | char_offset++; |
df7492f9 | 5540 | coding->errors++; |
4ed46869 KH |
5541 | } |
5542 | ||
df7492f9 | 5543 | no_more_source: |
ff0dacd7 | 5544 | if (last_id != charset_ascii) |
69a80ea3 | 5545 | ADD_CHARSET_DATA (charbuf, char_offset - last_offset, last_id); |
df7492f9 KH |
5546 | coding->consumed_char += consumed_chars_base; |
5547 | coding->consumed = src_base - coding->source; | |
5548 | coding->charbuf_used = charbuf - coding->charbuf; | |
4ed46869 KH |
5549 | } |
5550 | ||
df7492f9 | 5551 | static int |
971de7fb | 5552 | encode_coding_charset (struct coding_system *coding) |
4ed46869 | 5553 | { |
df7492f9 KH |
5554 | int multibytep = coding->dst_multibyte; |
5555 | int *charbuf = coding->charbuf; | |
5556 | int *charbuf_end = charbuf + coding->charbuf_used; | |
5557 | unsigned char *dst = coding->destination + coding->produced; | |
5558 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
5559 | int safe_room = MAX_MULTIBYTE_LENGTH; | |
5560 | int produced_chars = 0; | |
24a73b0a | 5561 | Lisp_Object attrs, charset_list; |
df7492f9 | 5562 | int ascii_compatible; |
b73bfc1c | 5563 | int c; |
b73bfc1c | 5564 | |
24a73b0a | 5565 | CODING_GET_INFO (coding, attrs, charset_list); |
df7492f9 | 5566 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); |
fb88bf2d | 5567 | |
df7492f9 | 5568 | while (charbuf < charbuf_end) |
4ed46869 | 5569 | { |
4eb6d3f1 | 5570 | struct charset *charset; |
df7492f9 | 5571 | unsigned code; |
8f924df7 | 5572 | |
df7492f9 KH |
5573 | ASSURE_DESTINATION (safe_room); |
5574 | c = *charbuf++; | |
5575 | if (ascii_compatible && ASCII_CHAR_P (c)) | |
5576 | EMIT_ONE_ASCII_BYTE (c); | |
16eafb5d | 5577 | else if (CHAR_BYTE8_P (c)) |
4ed46869 | 5578 | { |
16eafb5d KH |
5579 | c = CHAR_TO_BYTE8 (c); |
5580 | EMIT_ONE_BYTE (c); | |
d46c5b12 | 5581 | } |
d46c5b12 | 5582 | else |
b73bfc1c | 5583 | { |
4eb6d3f1 KH |
5584 | charset = char_charset (c, charset_list, &code); |
5585 | if (charset) | |
5586 | { | |
5587 | if (CHARSET_DIMENSION (charset) == 1) | |
5588 | EMIT_ONE_BYTE (code); | |
5589 | else if (CHARSET_DIMENSION (charset) == 2) | |
5590 | EMIT_TWO_BYTES (code >> 8, code & 0xFF); | |
5591 | else if (CHARSET_DIMENSION (charset) == 3) | |
5592 | EMIT_THREE_BYTES (code >> 16, (code >> 8) & 0xFF, code & 0xFF); | |
5593 | else | |
5594 | EMIT_FOUR_BYTES (code >> 24, (code >> 16) & 0xFF, | |
5595 | (code >> 8) & 0xFF, code & 0xFF); | |
5596 | } | |
5597 | else | |
41cbe562 KH |
5598 | { |
5599 | if (coding->mode & CODING_MODE_SAFE_ENCODING) | |
5600 | c = CODING_INHIBIT_CHARACTER_SUBSTITUTION; | |
5601 | else | |
5602 | c = coding->default_char; | |
5603 | EMIT_ONE_BYTE (c); | |
5604 | } | |
4ed46869 | 5605 | } |
4ed46869 KH |
5606 | } |
5607 | ||
065e3595 | 5608 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 KH |
5609 | coding->produced_char += produced_chars; |
5610 | coding->produced = dst - coding->destination; | |
5611 | return 0; | |
4ed46869 KH |
5612 | } |
5613 | ||
5614 | \f | |
1397dc18 | 5615 | /*** 7. C library functions ***/ |
4ed46869 | 5616 | |
df7492f9 KH |
5617 | /* Setup coding context CODING from information about CODING_SYSTEM. |
5618 | If CODING_SYSTEM is nil, `no-conversion' is assumed. If | |
5619 | CODING_SYSTEM is invalid, signal an error. */ | |
4ed46869 | 5620 | |
ec6d2bb8 | 5621 | void |
971de7fb | 5622 | setup_coding_system (Lisp_Object coding_system, struct coding_system *coding) |
4ed46869 | 5623 | { |
df7492f9 KH |
5624 | Lisp_Object attrs; |
5625 | Lisp_Object eol_type; | |
5626 | Lisp_Object coding_type; | |
4608c386 | 5627 | Lisp_Object val; |
4ed46869 | 5628 | |
df7492f9 | 5629 | if (NILP (coding_system)) |
ae6f73fa | 5630 | coding_system = Qundecided; |
c07c8e12 | 5631 | |
df7492f9 | 5632 | CHECK_CODING_SYSTEM_GET_ID (coding_system, coding->id); |
1f5dbf34 | 5633 | |
df7492f9 | 5634 | attrs = CODING_ID_ATTRS (coding->id); |
0a9564cb | 5635 | eol_type = inhibit_eol_conversion ? Qunix : CODING_ID_EOL_TYPE (coding->id); |
1f5dbf34 | 5636 | |
df7492f9 KH |
5637 | coding->mode = 0; |
5638 | coding->head_ascii = -1; | |
4a015c45 KH |
5639 | if (VECTORP (eol_type)) |
5640 | coding->common_flags = (CODING_REQUIRE_DECODING_MASK | |
5641 | | CODING_REQUIRE_DETECTION_MASK); | |
5642 | else if (! EQ (eol_type, Qunix)) | |
5643 | coding->common_flags = (CODING_REQUIRE_DECODING_MASK | |
5644 | | CODING_REQUIRE_ENCODING_MASK); | |
5645 | else | |
5646 | coding->common_flags = 0; | |
5e5c78be KH |
5647 | if (! NILP (CODING_ATTR_POST_READ (attrs))) |
5648 | coding->common_flags |= CODING_REQUIRE_DECODING_MASK; | |
5649 | if (! NILP (CODING_ATTR_PRE_WRITE (attrs))) | |
5650 | coding->common_flags |= CODING_REQUIRE_ENCODING_MASK; | |
8f924df7 KH |
5651 | if (! NILP (CODING_ATTR_FOR_UNIBYTE (attrs))) |
5652 | coding->common_flags |= CODING_FOR_UNIBYTE_MASK; | |
1f5dbf34 | 5653 | |
df7492f9 | 5654 | val = CODING_ATTR_SAFE_CHARSETS (attrs); |
8f924df7 | 5655 | coding->max_charset_id = SCHARS (val) - 1; |
1b3b981b | 5656 | coding->safe_charsets = SDATA (val); |
df7492f9 | 5657 | coding->default_char = XINT (CODING_ATTR_DEFAULT_CHAR (attrs)); |
624bda09 | 5658 | coding->carryover_bytes = 0; |
4608c386 | 5659 | |
df7492f9 KH |
5660 | coding_type = CODING_ATTR_TYPE (attrs); |
5661 | if (EQ (coding_type, Qundecided)) | |
d46c5b12 | 5662 | { |
df7492f9 KH |
5663 | coding->detector = NULL; |
5664 | coding->decoder = decode_coding_raw_text; | |
5665 | coding->encoder = encode_coding_raw_text; | |
5666 | coding->common_flags |= CODING_REQUIRE_DETECTION_MASK; | |
d46c5b12 | 5667 | } |
df7492f9 | 5668 | else if (EQ (coding_type, Qiso_2022)) |
d46c5b12 | 5669 | { |
df7492f9 KH |
5670 | int i; |
5671 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); | |
5672 | ||
5673 | /* Invoke graphic register 0 to plane 0. */ | |
5674 | CODING_ISO_INVOCATION (coding, 0) = 0; | |
5675 | /* Invoke graphic register 1 to plane 1 if we can use 8-bit. */ | |
5676 | CODING_ISO_INVOCATION (coding, 1) | |
5677 | = (flags & CODING_ISO_FLAG_SEVEN_BITS ? -1 : 1); | |
5678 | /* Setup the initial status of designation. */ | |
5679 | for (i = 0; i < 4; i++) | |
5680 | CODING_ISO_DESIGNATION (coding, i) = CODING_ISO_INITIAL (coding, i); | |
5681 | /* Not single shifting initially. */ | |
5682 | CODING_ISO_SINGLE_SHIFTING (coding) = 0; | |
5683 | /* Beginning of buffer should also be regarded as bol. */ | |
5684 | CODING_ISO_BOL (coding) = 1; | |
5685 | coding->detector = detect_coding_iso_2022; | |
5686 | coding->decoder = decode_coding_iso_2022; | |
5687 | coding->encoder = encode_coding_iso_2022; | |
5688 | if (flags & CODING_ISO_FLAG_SAFE) | |
5689 | coding->mode |= CODING_MODE_SAFE_ENCODING; | |
d46c5b12 | 5690 | coding->common_flags |
df7492f9 KH |
5691 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK |
5692 | | CODING_REQUIRE_FLUSHING_MASK); | |
5693 | if (flags & CODING_ISO_FLAG_COMPOSITION) | |
5694 | coding->common_flags |= CODING_ANNOTATE_COMPOSITION_MASK; | |
ff0dacd7 KH |
5695 | if (flags & CODING_ISO_FLAG_DESIGNATION) |
5696 | coding->common_flags |= CODING_ANNOTATE_CHARSET_MASK; | |
df7492f9 KH |
5697 | if (flags & CODING_ISO_FLAG_FULL_SUPPORT) |
5698 | { | |
5699 | setup_iso_safe_charsets (attrs); | |
5700 | val = CODING_ATTR_SAFE_CHARSETS (attrs); | |
8f924df7 | 5701 | coding->max_charset_id = SCHARS (val) - 1; |
1b3b981b | 5702 | coding->safe_charsets = SDATA (val); |
df7492f9 KH |
5703 | } |
5704 | CODING_ISO_FLAGS (coding) = flags; | |
e951386e KH |
5705 | CODING_ISO_CMP_STATUS (coding)->state = COMPOSING_NO; |
5706 | CODING_ISO_CMP_STATUS (coding)->method = COMPOSITION_NO; | |
5707 | CODING_ISO_EXTSEGMENT_LEN (coding) = 0; | |
5708 | CODING_ISO_EMBEDDED_UTF_8 (coding) = 0; | |
d46c5b12 | 5709 | } |
df7492f9 | 5710 | else if (EQ (coding_type, Qcharset)) |
d46c5b12 | 5711 | { |
df7492f9 KH |
5712 | coding->detector = detect_coding_charset; |
5713 | coding->decoder = decode_coding_charset; | |
5714 | coding->encoder = encode_coding_charset; | |
d46c5b12 | 5715 | coding->common_flags |
df7492f9 | 5716 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
d46c5b12 | 5717 | } |
df7492f9 | 5718 | else if (EQ (coding_type, Qutf_8)) |
d46c5b12 | 5719 | { |
a470d443 KH |
5720 | val = AREF (attrs, coding_attr_utf_bom); |
5721 | CODING_UTF_8_BOM (coding) = (CONSP (val) ? utf_detect_bom | |
5722 | : EQ (val, Qt) ? utf_with_bom | |
5723 | : utf_without_bom); | |
df7492f9 KH |
5724 | coding->detector = detect_coding_utf_8; |
5725 | coding->decoder = decode_coding_utf_8; | |
5726 | coding->encoder = encode_coding_utf_8; | |
5727 | coding->common_flags | |
5728 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); | |
a470d443 KH |
5729 | if (CODING_UTF_8_BOM (coding) == utf_detect_bom) |
5730 | coding->common_flags |= CODING_REQUIRE_DETECTION_MASK; | |
df7492f9 KH |
5731 | } |
5732 | else if (EQ (coding_type, Qutf_16)) | |
5733 | { | |
a470d443 KH |
5734 | val = AREF (attrs, coding_attr_utf_bom); |
5735 | CODING_UTF_16_BOM (coding) = (CONSP (val) ? utf_detect_bom | |
5736 | : EQ (val, Qt) ? utf_with_bom | |
5737 | : utf_without_bom); | |
df7492f9 | 5738 | val = AREF (attrs, coding_attr_utf_16_endian); |
b49a1807 | 5739 | CODING_UTF_16_ENDIAN (coding) = (EQ (val, Qbig) ? utf_16_big_endian |
df7492f9 | 5740 | : utf_16_little_endian); |
e19c3639 | 5741 | CODING_UTF_16_SURROGATE (coding) = 0; |
df7492f9 KH |
5742 | coding->detector = detect_coding_utf_16; |
5743 | coding->decoder = decode_coding_utf_16; | |
5744 | coding->encoder = encode_coding_utf_16; | |
5745 | coding->common_flags | |
5746 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); | |
a470d443 | 5747 | if (CODING_UTF_16_BOM (coding) == utf_detect_bom) |
b49a1807 | 5748 | coding->common_flags |= CODING_REQUIRE_DETECTION_MASK; |
d46c5b12 | 5749 | } |
df7492f9 | 5750 | else if (EQ (coding_type, Qccl)) |
4ed46869 | 5751 | { |
df7492f9 KH |
5752 | coding->detector = detect_coding_ccl; |
5753 | coding->decoder = decode_coding_ccl; | |
5754 | coding->encoder = encode_coding_ccl; | |
c952af22 | 5755 | coding->common_flags |
df7492f9 KH |
5756 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK |
5757 | | CODING_REQUIRE_FLUSHING_MASK); | |
5758 | } | |
5759 | else if (EQ (coding_type, Qemacs_mule)) | |
5760 | { | |
5761 | coding->detector = detect_coding_emacs_mule; | |
5762 | coding->decoder = decode_coding_emacs_mule; | |
5763 | coding->encoder = encode_coding_emacs_mule; | |
c952af22 | 5764 | coding->common_flags |
df7492f9 | 5765 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
e951386e | 5766 | coding->spec.emacs_mule.full_support = 1; |
df7492f9 KH |
5767 | if (! NILP (AREF (attrs, coding_attr_emacs_mule_full)) |
5768 | && ! EQ (CODING_ATTR_CHARSET_LIST (attrs), Vemacs_mule_charset_list)) | |
5769 | { | |
5770 | Lisp_Object tail, safe_charsets; | |
5771 | int max_charset_id = 0; | |
5772 | ||
5773 | for (tail = Vemacs_mule_charset_list; CONSP (tail); | |
5774 | tail = XCDR (tail)) | |
5775 | if (max_charset_id < XFASTINT (XCAR (tail))) | |
5776 | max_charset_id = XFASTINT (XCAR (tail)); | |
1b3b981b AS |
5777 | safe_charsets = make_uninit_string (max_charset_id + 1); |
5778 | memset (SDATA (safe_charsets), 255, max_charset_id + 1); | |
df7492f9 KH |
5779 | for (tail = Vemacs_mule_charset_list; CONSP (tail); |
5780 | tail = XCDR (tail)) | |
8f924df7 | 5781 | SSET (safe_charsets, XFASTINT (XCAR (tail)), 0); |
df7492f9 | 5782 | coding->max_charset_id = max_charset_id; |
1b3b981b | 5783 | coding->safe_charsets = SDATA (safe_charsets); |
e951386e | 5784 | coding->spec.emacs_mule.full_support = 1; |
df7492f9 | 5785 | } |
e951386e KH |
5786 | coding->spec.emacs_mule.cmp_status.state = COMPOSING_NO; |
5787 | coding->spec.emacs_mule.cmp_status.method = COMPOSITION_NO; | |
df7492f9 KH |
5788 | } |
5789 | else if (EQ (coding_type, Qshift_jis)) | |
5790 | { | |
5791 | coding->detector = detect_coding_sjis; | |
5792 | coding->decoder = decode_coding_sjis; | |
5793 | coding->encoder = encode_coding_sjis; | |
c952af22 | 5794 | coding->common_flags |
df7492f9 KH |
5795 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
5796 | } | |
5797 | else if (EQ (coding_type, Qbig5)) | |
5798 | { | |
5799 | coding->detector = detect_coding_big5; | |
5800 | coding->decoder = decode_coding_big5; | |
5801 | coding->encoder = encode_coding_big5; | |
c952af22 | 5802 | coding->common_flags |
df7492f9 KH |
5803 | |= (CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK); |
5804 | } | |
5805 | else /* EQ (coding_type, Qraw_text) */ | |
ec6d2bb8 | 5806 | { |
df7492f9 KH |
5807 | coding->detector = NULL; |
5808 | coding->decoder = decode_coding_raw_text; | |
5809 | coding->encoder = encode_coding_raw_text; | |
ea29edf2 KH |
5810 | if (! EQ (eol_type, Qunix)) |
5811 | { | |
5812 | coding->common_flags |= CODING_REQUIRE_DECODING_MASK; | |
5813 | if (! VECTORP (eol_type)) | |
5814 | coding->common_flags |= CODING_REQUIRE_ENCODING_MASK; | |
5815 | } | |
5816 | ||
4ed46869 | 5817 | } |
4ed46869 | 5818 | |
df7492f9 | 5819 | return; |
4ed46869 KH |
5820 | } |
5821 | ||
0ff61e78 KH |
5822 | /* Return a list of charsets supported by CODING. */ |
5823 | ||
5824 | Lisp_Object | |
971de7fb | 5825 | coding_charset_list (struct coding_system *coding) |
0ff61e78 | 5826 | { |
35befdaa | 5827 | Lisp_Object attrs, charset_list; |
0ff61e78 KH |
5828 | |
5829 | CODING_GET_INFO (coding, attrs, charset_list); | |
5830 | if (EQ (CODING_ATTR_TYPE (attrs), Qiso_2022)) | |
5831 | { | |
5832 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); | |
5833 | ||
5834 | if (flags & CODING_ISO_FLAG_FULL_SUPPORT) | |
5835 | charset_list = Viso_2022_charset_list; | |
5836 | } | |
5837 | else if (EQ (CODING_ATTR_TYPE (attrs), Qemacs_mule)) | |
5838 | { | |
5839 | charset_list = Vemacs_mule_charset_list; | |
5840 | } | |
5841 | return charset_list; | |
5842 | } | |
5843 | ||
5844 | ||
e9f91ece KH |
5845 | /* Return a list of charsets supported by CODING-SYSTEM. */ |
5846 | ||
5847 | Lisp_Object | |
971de7fb | 5848 | coding_system_charset_list (Lisp_Object coding_system) |
e9f91ece KH |
5849 | { |
5850 | int id; | |
5851 | Lisp_Object attrs, charset_list; | |
5852 | ||
5853 | CHECK_CODING_SYSTEM_GET_ID (coding_system, id); | |
5854 | attrs = CODING_ID_ATTRS (id); | |
5855 | ||
5856 | if (EQ (CODING_ATTR_TYPE (attrs), Qiso_2022)) | |
5857 | { | |
5858 | int flags = XINT (AREF (attrs, coding_attr_iso_flags)); | |
5859 | ||
5860 | if (flags & CODING_ISO_FLAG_FULL_SUPPORT) | |
5861 | charset_list = Viso_2022_charset_list; | |
5862 | else | |
5863 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
5864 | } | |
5865 | else if (EQ (CODING_ATTR_TYPE (attrs), Qemacs_mule)) | |
5866 | { | |
5867 | charset_list = Vemacs_mule_charset_list; | |
5868 | } | |
5869 | else | |
5870 | { | |
5871 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
5872 | } | |
5873 | return charset_list; | |
5874 | } | |
5875 | ||
5876 | ||
df7492f9 KH |
5877 | /* Return raw-text or one of its subsidiaries that has the same |
5878 | eol_type as CODING-SYSTEM. */ | |
ec6d2bb8 | 5879 | |
df7492f9 | 5880 | Lisp_Object |
971de7fb | 5881 | raw_text_coding_system (Lisp_Object coding_system) |
ec6d2bb8 | 5882 | { |
0be8721c | 5883 | Lisp_Object spec, attrs; |
df7492f9 | 5884 | Lisp_Object eol_type, raw_text_eol_type; |
ec6d2bb8 | 5885 | |
d3e4cb56 KH |
5886 | if (NILP (coding_system)) |
5887 | return Qraw_text; | |
df7492f9 KH |
5888 | spec = CODING_SYSTEM_SPEC (coding_system); |
5889 | attrs = AREF (spec, 0); | |
ec6d2bb8 | 5890 | |
df7492f9 KH |
5891 | if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text)) |
5892 | return coding_system; | |
ec6d2bb8 | 5893 | |
df7492f9 KH |
5894 | eol_type = AREF (spec, 2); |
5895 | if (VECTORP (eol_type)) | |
5896 | return Qraw_text; | |
5897 | spec = CODING_SYSTEM_SPEC (Qraw_text); | |
5898 | raw_text_eol_type = AREF (spec, 2); | |
5899 | return (EQ (eol_type, Qunix) ? AREF (raw_text_eol_type, 0) | |
5900 | : EQ (eol_type, Qdos) ? AREF (raw_text_eol_type, 1) | |
5901 | : AREF (raw_text_eol_type, 2)); | |
ec6d2bb8 KH |
5902 | } |
5903 | ||
54f78171 | 5904 | |
1911a33b KH |
5905 | /* If CODING_SYSTEM doesn't specify end-of-line format, return one of |
5906 | the subsidiary that has the same eol-spec as PARENT (if it is not | |
5907 | nil and specifies end-of-line format) or the system's setting | |
fcbcfb64 | 5908 | (system_eol_type). */ |
df7492f9 KH |
5909 | |
5910 | Lisp_Object | |
971de7fb | 5911 | coding_inherit_eol_type (Lisp_Object coding_system, Lisp_Object parent) |
54f78171 | 5912 | { |
3e139625 | 5913 | Lisp_Object spec, eol_type; |
54f78171 | 5914 | |
d3e4cb56 KH |
5915 | if (NILP (coding_system)) |
5916 | coding_system = Qraw_text; | |
df7492f9 | 5917 | spec = CODING_SYSTEM_SPEC (coding_system); |
df7492f9 | 5918 | eol_type = AREF (spec, 2); |
fcbcfb64 | 5919 | if (VECTORP (eol_type)) |
df7492f9 | 5920 | { |
df7492f9 KH |
5921 | Lisp_Object parent_eol_type; |
5922 | ||
fcbcfb64 KH |
5923 | if (! NILP (parent)) |
5924 | { | |
5925 | Lisp_Object parent_spec; | |
5926 | ||
4a015c45 | 5927 | parent_spec = CODING_SYSTEM_SPEC (parent); |
fcbcfb64 | 5928 | parent_eol_type = AREF (parent_spec, 2); |
1911a33b | 5929 | if (VECTORP (parent_eol_type)) |
4628bef1 | 5930 | parent_eol_type = system_eol_type; |
fcbcfb64 KH |
5931 | } |
5932 | else | |
5933 | parent_eol_type = system_eol_type; | |
df7492f9 KH |
5934 | if (EQ (parent_eol_type, Qunix)) |
5935 | coding_system = AREF (eol_type, 0); | |
5936 | else if (EQ (parent_eol_type, Qdos)) | |
5937 | coding_system = AREF (eol_type, 1); | |
5938 | else if (EQ (parent_eol_type, Qmac)) | |
5939 | coding_system = AREF (eol_type, 2); | |
54f78171 | 5940 | } |
df7492f9 | 5941 | return coding_system; |
54f78171 KH |
5942 | } |
5943 | ||
fcaf8878 KH |
5944 | |
5945 | /* Check if text-conversion and eol-conversion of CODING_SYSTEM are | |
5946 | decided for writing to a process. If not, complement them, and | |
5947 | return a new coding system. */ | |
5948 | ||
5949 | Lisp_Object | |
4628bef1 | 5950 | complement_process_encoding_system (Lisp_Object coding_system) |
fcaf8878 | 5951 | { |
5886ec9c KH |
5952 | Lisp_Object coding_base = Qnil, eol_base = Qnil; |
5953 | Lisp_Object spec, attrs; | |
93d50df8 | 5954 | int i; |
fcaf8878 | 5955 | |
93d50df8 | 5956 | for (i = 0; i < 3; i++) |
fcaf8878 | 5957 | { |
93d50df8 KH |
5958 | if (i == 1) |
5959 | coding_system = CDR_SAFE (Vdefault_process_coding_system); | |
5960 | else if (i == 2) | |
5961 | coding_system = preferred_coding_system (); | |
5962 | spec = CODING_SYSTEM_SPEC (coding_system); | |
5963 | if (NILP (spec)) | |
5964 | continue; | |
5965 | attrs = AREF (spec, 0); | |
5966 | if (NILP (coding_base) && ! EQ (CODING_ATTR_TYPE (attrs), Qundecided)) | |
5967 | coding_base = CODING_ATTR_BASE_NAME (attrs); | |
5968 | if (NILP (eol_base) && ! VECTORP (AREF (spec, 2))) | |
5969 | eol_base = coding_system; | |
5970 | if (! NILP (coding_base) && ! NILP (eol_base)) | |
5971 | break; | |
fcaf8878 | 5972 | } |
fcaf8878 | 5973 | |
93d50df8 KH |
5974 | if (i > 0) |
5975 | /* The original CODING_SYSTEM didn't specify text-conversion or | |
5976 | eol-conversion. Be sure that we return a fully complemented | |
5977 | coding system. */ | |
5978 | coding_system = coding_inherit_eol_type (coding_base, eol_base); | |
5979 | return coding_system; | |
fcaf8878 KH |
5980 | } |
5981 | ||
5982 | ||
4ed46869 KH |
5983 | /* Emacs has a mechanism to automatically detect a coding system if it |
5984 | is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But, | |
5985 | it's impossible to distinguish some coding systems accurately | |
5986 | because they use the same range of codes. So, at first, coding | |
5987 | systems are categorized into 7, those are: | |
5988 | ||
0ef69138 | 5989 | o coding-category-emacs-mule |
4ed46869 KH |
5990 | |
5991 | The category for a coding system which has the same code range | |
5992 | as Emacs' internal format. Assigned the coding-system (Lisp | |
0ef69138 | 5993 | symbol) `emacs-mule' by default. |
4ed46869 KH |
5994 | |
5995 | o coding-category-sjis | |
5996 | ||
5997 | The category for a coding system which has the same code range | |
5998 | as SJIS. Assigned the coding-system (Lisp | |
7717c392 | 5999 | symbol) `japanese-shift-jis' by default. |
4ed46869 KH |
6000 | |
6001 | o coding-category-iso-7 | |
6002 | ||
6003 | The category for a coding system which has the same code range | |
7717c392 | 6004 | as ISO2022 of 7-bit environment. This doesn't use any locking |
d46c5b12 KH |
6005 | shift and single shift functions. This can encode/decode all |
6006 | charsets. Assigned the coding-system (Lisp symbol) | |
6007 | `iso-2022-7bit' by default. | |
6008 | ||
6009 | o coding-category-iso-7-tight | |
6010 | ||
6011 | Same as coding-category-iso-7 except that this can | |
6012 | encode/decode only the specified charsets. | |
4ed46869 KH |
6013 | |
6014 | o coding-category-iso-8-1 | |
6015 | ||
6016 | The category for a coding system which has the same code range | |
6017 | as ISO2022 of 8-bit environment and graphic plane 1 used only | |
7717c392 KH |
6018 | for DIMENSION1 charset. This doesn't use any locking shift |
6019 | and single shift functions. Assigned the coding-system (Lisp | |
6020 | symbol) `iso-latin-1' by default. | |
4ed46869 KH |
6021 | |
6022 | o coding-category-iso-8-2 | |
6023 | ||
6024 | The category for a coding system which has the same code range | |
6025 | as ISO2022 of 8-bit environment and graphic plane 1 used only | |
7717c392 KH |
6026 | for DIMENSION2 charset. This doesn't use any locking shift |
6027 | and single shift functions. Assigned the coding-system (Lisp | |
6028 | symbol) `japanese-iso-8bit' by default. | |
4ed46869 | 6029 | |
7717c392 | 6030 | o coding-category-iso-7-else |
4ed46869 KH |
6031 | |
6032 | The category for a coding system which has the same code range | |
ad1746f5 | 6033 | as ISO2022 of 7-bit environment but uses locking shift or |
7717c392 KH |
6034 | single shift functions. Assigned the coding-system (Lisp |
6035 | symbol) `iso-2022-7bit-lock' by default. | |
6036 | ||
6037 | o coding-category-iso-8-else | |
6038 | ||
6039 | The category for a coding system which has the same code range | |
ad1746f5 | 6040 | as ISO2022 of 8-bit environment but uses locking shift or |
7717c392 KH |
6041 | single shift functions. Assigned the coding-system (Lisp |
6042 | symbol) `iso-2022-8bit-ss2' by default. | |
4ed46869 KH |
6043 | |
6044 | o coding-category-big5 | |
6045 | ||
6046 | The category for a coding system which has the same code range | |
6047 | as BIG5. Assigned the coding-system (Lisp symbol) | |
e0e989f6 | 6048 | `cn-big5' by default. |
4ed46869 | 6049 | |
fa42c37f KH |
6050 | o coding-category-utf-8 |
6051 | ||
6052 | The category for a coding system which has the same code range | |
6e76ae91 | 6053 | as UTF-8 (cf. RFC3629). Assigned the coding-system (Lisp |
fa42c37f KH |
6054 | symbol) `utf-8' by default. |
6055 | ||
6056 | o coding-category-utf-16-be | |
6057 | ||
6058 | The category for a coding system in which a text has an | |
6059 | Unicode signature (cf. Unicode Standard) in the order of BIG | |
6060 | endian at the head. Assigned the coding-system (Lisp symbol) | |
6061 | `utf-16-be' by default. | |
6062 | ||
6063 | o coding-category-utf-16-le | |
6064 | ||
6065 | The category for a coding system in which a text has an | |
6066 | Unicode signature (cf. Unicode Standard) in the order of | |
6067 | LITTLE endian at the head. Assigned the coding-system (Lisp | |
6068 | symbol) `utf-16-le' by default. | |
6069 | ||
1397dc18 KH |
6070 | o coding-category-ccl |
6071 | ||
6072 | The category for a coding system of which encoder/decoder is | |
6073 | written in CCL programs. The default value is nil, i.e., no | |
6074 | coding system is assigned. | |
6075 | ||
4ed46869 KH |
6076 | o coding-category-binary |
6077 | ||
6078 | The category for a coding system not categorized in any of the | |
6079 | above. Assigned the coding-system (Lisp symbol) | |
e0e989f6 | 6080 | `no-conversion' by default. |
4ed46869 KH |
6081 | |
6082 | Each of them is a Lisp symbol and the value is an actual | |
df7492f9 | 6083 | `coding-system's (this is also a Lisp symbol) assigned by a user. |
4ed46869 KH |
6084 | What Emacs does actually is to detect a category of coding system. |
6085 | Then, it uses a `coding-system' assigned to it. If Emacs can't | |
df7492f9 | 6086 | decide only one possible category, it selects a category of the |
4ed46869 KH |
6087 | highest priority. Priorities of categories are also specified by a |
6088 | user in a Lisp variable `coding-category-list'. | |
6089 | ||
6090 | */ | |
6091 | ||
df7492f9 KH |
6092 | #define EOL_SEEN_NONE 0 |
6093 | #define EOL_SEEN_LF 1 | |
6094 | #define EOL_SEEN_CR 2 | |
6095 | #define EOL_SEEN_CRLF 4 | |
66cfb530 | 6096 | |
ff0dacd7 KH |
6097 | /* Detect how end-of-line of a text of length SRC_BYTES pointed by |
6098 | SOURCE is encoded. If CATEGORY is one of | |
6099 | coding_category_utf_16_XXXX, assume that CR and LF are encoded by | |
6100 | two-byte, else they are encoded by one-byte. | |
6101 | ||
6102 | Return one of EOL_SEEN_XXX. */ | |
4ed46869 | 6103 | |
bc4bc72a | 6104 | #define MAX_EOL_CHECK_COUNT 3 |
d46c5b12 KH |
6105 | |
6106 | static int | |
cf84bb53 JB |
6107 | detect_eol (const unsigned char *source, EMACS_INT src_bytes, |
6108 | enum coding_category category) | |
4ed46869 | 6109 | { |
f6cbaf43 | 6110 | const unsigned char *src = source, *src_end = src + src_bytes; |
4ed46869 | 6111 | unsigned char c; |
df7492f9 KH |
6112 | int total = 0; |
6113 | int eol_seen = EOL_SEEN_NONE; | |
4ed46869 | 6114 | |
89528eb3 | 6115 | if ((1 << category) & CATEGORY_MASK_UTF_16) |
4ed46869 | 6116 | { |
df7492f9 | 6117 | int msb, lsb; |
fa42c37f | 6118 | |
89528eb3 KH |
6119 | msb = category == (coding_category_utf_16_le |
6120 | | coding_category_utf_16_le_nosig); | |
df7492f9 | 6121 | lsb = 1 - msb; |
fa42c37f | 6122 | |
df7492f9 | 6123 | while (src + 1 < src_end) |
fa42c37f | 6124 | { |
df7492f9 KH |
6125 | c = src[lsb]; |
6126 | if (src[msb] == 0 && (c == '\n' || c == '\r')) | |
fa42c37f | 6127 | { |
df7492f9 KH |
6128 | int this_eol; |
6129 | ||
6130 | if (c == '\n') | |
6131 | this_eol = EOL_SEEN_LF; | |
6132 | else if (src + 3 >= src_end | |
6133 | || src[msb + 2] != 0 | |
6134 | || src[lsb + 2] != '\n') | |
6135 | this_eol = EOL_SEEN_CR; | |
fa42c37f | 6136 | else |
75f4f1ac EZ |
6137 | { |
6138 | this_eol = EOL_SEEN_CRLF; | |
6139 | src += 2; | |
6140 | } | |
df7492f9 KH |
6141 | |
6142 | if (eol_seen == EOL_SEEN_NONE) | |
6143 | /* This is the first end-of-line. */ | |
6144 | eol_seen = this_eol; | |
6145 | else if (eol_seen != this_eol) | |
fa42c37f | 6146 | { |
75f4f1ac EZ |
6147 | /* The found type is different from what found before. |
6148 | Allow for stray ^M characters in DOS EOL files. */ | |
ef1b0ba7 SM |
6149 | if ((eol_seen == EOL_SEEN_CR && this_eol == EOL_SEEN_CRLF) |
6150 | || (eol_seen == EOL_SEEN_CRLF | |
6151 | && this_eol == EOL_SEEN_CR)) | |
75f4f1ac EZ |
6152 | eol_seen = EOL_SEEN_CRLF; |
6153 | else | |
6154 | { | |
6155 | eol_seen = EOL_SEEN_LF; | |
6156 | break; | |
6157 | } | |
fa42c37f | 6158 | } |
df7492f9 KH |
6159 | if (++total == MAX_EOL_CHECK_COUNT) |
6160 | break; | |
fa42c37f | 6161 | } |
df7492f9 | 6162 | src += 2; |
fa42c37f | 6163 | } |
bcf26d6a | 6164 | } |
d46c5b12 | 6165 | else |
ef1b0ba7 SM |
6166 | while (src < src_end) |
6167 | { | |
6168 | c = *src++; | |
6169 | if (c == '\n' || c == '\r') | |
6170 | { | |
6171 | int this_eol; | |
d46c5b12 | 6172 | |
ef1b0ba7 SM |
6173 | if (c == '\n') |
6174 | this_eol = EOL_SEEN_LF; | |
6175 | else if (src >= src_end || *src != '\n') | |
6176 | this_eol = EOL_SEEN_CR; | |
6177 | else | |
6178 | this_eol = EOL_SEEN_CRLF, src++; | |
d46c5b12 | 6179 | |
ef1b0ba7 SM |
6180 | if (eol_seen == EOL_SEEN_NONE) |
6181 | /* This is the first end-of-line. */ | |
6182 | eol_seen = this_eol; | |
6183 | else if (eol_seen != this_eol) | |
6184 | { | |
6185 | /* The found type is different from what found before. | |
6186 | Allow for stray ^M characters in DOS EOL files. */ | |
6187 | if ((eol_seen == EOL_SEEN_CR && this_eol == EOL_SEEN_CRLF) | |
6188 | || (eol_seen == EOL_SEEN_CRLF && this_eol == EOL_SEEN_CR)) | |
6189 | eol_seen = EOL_SEEN_CRLF; | |
6190 | else | |
6191 | { | |
6192 | eol_seen = EOL_SEEN_LF; | |
6193 | break; | |
6194 | } | |
6195 | } | |
6196 | if (++total == MAX_EOL_CHECK_COUNT) | |
6197 | break; | |
6198 | } | |
6199 | } | |
df7492f9 | 6200 | return eol_seen; |
73be902c KH |
6201 | } |
6202 | ||
df7492f9 | 6203 | |
24a73b0a | 6204 | static Lisp_Object |
971de7fb | 6205 | adjust_coding_eol_type (struct coding_system *coding, int eol_seen) |
73be902c | 6206 | { |
0be8721c | 6207 | Lisp_Object eol_type; |
8f924df7 | 6208 | |
df7492f9 KH |
6209 | eol_type = CODING_ID_EOL_TYPE (coding->id); |
6210 | if (eol_seen & EOL_SEEN_LF) | |
24a73b0a KH |
6211 | { |
6212 | coding->id = CODING_SYSTEM_ID (AREF (eol_type, 0)); | |
6213 | eol_type = Qunix; | |
6214 | } | |
6f197c07 | 6215 | else if (eol_seen & EOL_SEEN_CRLF) |
24a73b0a KH |
6216 | { |
6217 | coding->id = CODING_SYSTEM_ID (AREF (eol_type, 1)); | |
6218 | eol_type = Qdos; | |
6219 | } | |
6f197c07 | 6220 | else if (eol_seen & EOL_SEEN_CR) |
24a73b0a KH |
6221 | { |
6222 | coding->id = CODING_SYSTEM_ID (AREF (eol_type, 2)); | |
6223 | eol_type = Qmac; | |
6224 | } | |
6225 | return eol_type; | |
d46c5b12 | 6226 | } |
4ed46869 | 6227 | |
df7492f9 KH |
6228 | /* Detect how a text specified in CODING is encoded. If a coding |
6229 | system is detected, update fields of CODING by the detected coding | |
6230 | system. */ | |
0a28aafb | 6231 | |
df7492f9 | 6232 | void |
971de7fb | 6233 | detect_coding (struct coding_system *coding) |
d46c5b12 | 6234 | { |
8f924df7 | 6235 | const unsigned char *src, *src_end; |
73cce38d | 6236 | int saved_mode = coding->mode; |
d46c5b12 | 6237 | |
df7492f9 KH |
6238 | coding->consumed = coding->consumed_char = 0; |
6239 | coding->produced = coding->produced_char = 0; | |
6240 | coding_set_source (coding); | |
1c3478b0 | 6241 | |
df7492f9 | 6242 | src_end = coding->source + coding->src_bytes; |
c0e16b14 | 6243 | coding->head_ascii = 0; |
1c3478b0 | 6244 | |
df7492f9 KH |
6245 | /* If we have not yet decided the text encoding type, detect it |
6246 | now. */ | |
6247 | if (EQ (CODING_ATTR_TYPE (CODING_ID_ATTRS (coding->id)), Qundecided)) | |
b73bfc1c | 6248 | { |
df7492f9 | 6249 | int c, i; |
6cb21a4f | 6250 | struct coding_detection_info detect_info; |
2f3cbb32 | 6251 | int null_byte_found = 0, eight_bit_found = 0; |
df7492f9 | 6252 | |
6cb21a4f | 6253 | detect_info.checked = detect_info.found = detect_info.rejected = 0; |
2f3cbb32 | 6254 | for (src = coding->source; src < src_end; src++) |
d46c5b12 | 6255 | { |
df7492f9 | 6256 | c = *src; |
6cb21a4f | 6257 | if (c & 0x80) |
6cb21a4f | 6258 | { |
2f3cbb32 | 6259 | eight_bit_found = 1; |
2f3cbb32 KH |
6260 | if (null_byte_found) |
6261 | break; | |
6262 | } | |
6263 | else if (c < 0x20) | |
6264 | { | |
6265 | if ((c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) | |
6266 | && ! inhibit_iso_escape_detection | |
6267 | && ! detect_info.checked) | |
6cb21a4f | 6268 | { |
2f3cbb32 KH |
6269 | if (detect_coding_iso_2022 (coding, &detect_info)) |
6270 | { | |
6271 | /* We have scanned the whole data. */ | |
6272 | if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE)) | |
c0e16b14 KH |
6273 | { |
6274 | /* We didn't find an 8-bit code. We may | |
6275 | have found a null-byte, but it's very | |
ce5b453a | 6276 | rare that a binary file conforms to |
c0e16b14 KH |
6277 | ISO-2022. */ |
6278 | src = src_end; | |
6279 | coding->head_ascii = src - coding->source; | |
6280 | } | |
6281 | detect_info.rejected |= ~CATEGORY_MASK_ISO_ESCAPE; | |
2f3cbb32 KH |
6282 | break; |
6283 | } | |
6284 | } | |
97b1b294 | 6285 | else if (! c && !inhibit_null_byte_detection) |
2f3cbb32 KH |
6286 | { |
6287 | null_byte_found = 1; | |
6288 | if (eight_bit_found) | |
6289 | break; | |
6cb21a4f | 6290 | } |
c006c0c8 KH |
6291 | if (! eight_bit_found) |
6292 | coding->head_ascii++; | |
6cb21a4f | 6293 | } |
c006c0c8 | 6294 | else if (! eight_bit_found) |
c0e16b14 | 6295 | coding->head_ascii++; |
d46c5b12 | 6296 | } |
df7492f9 | 6297 | |
2f3cbb32 KH |
6298 | if (null_byte_found || eight_bit_found |
6299 | || coding->head_ascii < coding->src_bytes | |
6cb21a4f | 6300 | || detect_info.found) |
d46c5b12 | 6301 | { |
ff0dacd7 KH |
6302 | enum coding_category category; |
6303 | struct coding_system *this; | |
df7492f9 | 6304 | |
6cb21a4f KH |
6305 | if (coding->head_ascii == coding->src_bytes) |
6306 | /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */ | |
6307 | for (i = 0; i < coding_category_raw_text; i++) | |
6308 | { | |
6309 | category = coding_priorities[i]; | |
6310 | this = coding_categories + category; | |
6311 | if (detect_info.found & (1 << category)) | |
24a73b0a | 6312 | break; |
6cb21a4f KH |
6313 | } |
6314 | else | |
2f3cbb32 KH |
6315 | { |
6316 | if (null_byte_found) | |
ff0dacd7 | 6317 | { |
2f3cbb32 KH |
6318 | detect_info.checked |= ~CATEGORY_MASK_UTF_16; |
6319 | detect_info.rejected |= ~CATEGORY_MASK_UTF_16; | |
ff0dacd7 | 6320 | } |
2f3cbb32 KH |
6321 | for (i = 0; i < coding_category_raw_text; i++) |
6322 | { | |
6323 | category = coding_priorities[i]; | |
6324 | this = coding_categories + category; | |
6325 | if (this->id < 0) | |
6326 | { | |
6327 | /* No coding system of this category is defined. */ | |
6328 | detect_info.rejected |= (1 << category); | |
6329 | } | |
6330 | else if (category >= coding_category_raw_text) | |
6331 | continue; | |
6332 | else if (detect_info.checked & (1 << category)) | |
6333 | { | |
6334 | if (detect_info.found & (1 << category)) | |
6335 | break; | |
6336 | } | |
6337 | else if ((*(this->detector)) (coding, &detect_info) | |
6338 | && detect_info.found & (1 << category)) | |
6339 | { | |
6340 | if (category == coding_category_utf_16_auto) | |
6341 | { | |
6342 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
6343 | category = coding_category_utf_16_le; | |
6344 | else | |
6345 | category = coding_category_utf_16_be; | |
6346 | } | |
6347 | break; | |
6348 | } | |
6349 | } | |
2f3cbb32 | 6350 | } |
c0e16b14 KH |
6351 | |
6352 | if (i < coding_category_raw_text) | |
6353 | setup_coding_system (CODING_ID_NAME (this->id), coding); | |
6354 | else if (null_byte_found) | |
6355 | setup_coding_system (Qno_conversion, coding); | |
6356 | else if ((detect_info.rejected & CATEGORY_MASK_ANY) | |
6357 | == CATEGORY_MASK_ANY) | |
6358 | setup_coding_system (Qraw_text, coding); | |
6359 | else if (detect_info.rejected) | |
6360 | for (i = 0; i < coding_category_raw_text; i++) | |
6361 | if (! (detect_info.rejected & (1 << coding_priorities[i]))) | |
6362 | { | |
6363 | this = coding_categories + coding_priorities[i]; | |
6364 | setup_coding_system (CODING_ID_NAME (this->id), coding); | |
6365 | break; | |
6366 | } | |
d46c5b12 | 6367 | } |
b73bfc1c | 6368 | } |
a470d443 KH |
6369 | else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding->id))) |
6370 | == coding_category_utf_8_auto) | |
6371 | { | |
6372 | Lisp_Object coding_systems; | |
6373 | struct coding_detection_info detect_info; | |
6374 | ||
6375 | coding_systems | |
6376 | = AREF (CODING_ID_ATTRS (coding->id), coding_attr_utf_bom); | |
6377 | detect_info.found = detect_info.rejected = 0; | |
6378 | coding->head_ascii = 0; | |
6379 | if (CONSP (coding_systems) | |
6380 | && detect_coding_utf_8 (coding, &detect_info)) | |
6381 | { | |
6382 | if (detect_info.found & CATEGORY_MASK_UTF_8_SIG) | |
6383 | setup_coding_system (XCAR (coding_systems), coding); | |
6384 | else | |
6385 | setup_coding_system (XCDR (coding_systems), coding); | |
6386 | } | |
6387 | } | |
24a73b0a KH |
6388 | else if (XINT (CODING_ATTR_CATEGORY (CODING_ID_ATTRS (coding->id))) |
6389 | == coding_category_utf_16_auto) | |
b49a1807 KH |
6390 | { |
6391 | Lisp_Object coding_systems; | |
6392 | struct coding_detection_info detect_info; | |
6393 | ||
6394 | coding_systems | |
a470d443 | 6395 | = AREF (CODING_ID_ATTRS (coding->id), coding_attr_utf_bom); |
b49a1807 | 6396 | detect_info.found = detect_info.rejected = 0; |
a470d443 | 6397 | coding->head_ascii = 0; |
b49a1807 | 6398 | if (CONSP (coding_systems) |
24a73b0a | 6399 | && detect_coding_utf_16 (coding, &detect_info)) |
b49a1807 KH |
6400 | { |
6401 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
6402 | setup_coding_system (XCAR (coding_systems), coding); | |
24a73b0a | 6403 | else if (detect_info.found & CATEGORY_MASK_UTF_16_BE) |
b49a1807 KH |
6404 | setup_coding_system (XCDR (coding_systems), coding); |
6405 | } | |
6406 | } | |
73cce38d | 6407 | coding->mode = saved_mode; |
4ed46869 | 6408 | } |
4ed46869 | 6409 | |
d46c5b12 | 6410 | |
aaaf0b1e | 6411 | static void |
971de7fb | 6412 | decode_eol (struct coding_system *coding) |
aaaf0b1e | 6413 | { |
24a73b0a KH |
6414 | Lisp_Object eol_type; |
6415 | unsigned char *p, *pbeg, *pend; | |
3ed051d4 | 6416 | |
24a73b0a | 6417 | eol_type = CODING_ID_EOL_TYPE (coding->id); |
0a9564cb | 6418 | if (EQ (eol_type, Qunix) || inhibit_eol_conversion) |
24a73b0a KH |
6419 | return; |
6420 | ||
6421 | if (NILP (coding->dst_object)) | |
6422 | pbeg = coding->destination; | |
6423 | else | |
6424 | pbeg = BYTE_POS_ADDR (coding->dst_pos_byte); | |
6425 | pend = pbeg + coding->produced; | |
6426 | ||
6427 | if (VECTORP (eol_type)) | |
aaaf0b1e | 6428 | { |
df7492f9 | 6429 | int eol_seen = EOL_SEEN_NONE; |
4ed46869 | 6430 | |
24a73b0a | 6431 | for (p = pbeg; p < pend; p++) |
aaaf0b1e | 6432 | { |
df7492f9 KH |
6433 | if (*p == '\n') |
6434 | eol_seen |= EOL_SEEN_LF; | |
6435 | else if (*p == '\r') | |
aaaf0b1e | 6436 | { |
df7492f9 | 6437 | if (p + 1 < pend && *(p + 1) == '\n') |
aaaf0b1e | 6438 | { |
df7492f9 KH |
6439 | eol_seen |= EOL_SEEN_CRLF; |
6440 | p++; | |
aaaf0b1e | 6441 | } |
aaaf0b1e | 6442 | else |
df7492f9 | 6443 | eol_seen |= EOL_SEEN_CR; |
aaaf0b1e | 6444 | } |
aaaf0b1e | 6445 | } |
75f4f1ac EZ |
6446 | /* Handle DOS-style EOLs in a file with stray ^M characters. */ |
6447 | if ((eol_seen & EOL_SEEN_CRLF) != 0 | |
6448 | && (eol_seen & EOL_SEEN_CR) != 0 | |
6449 | && (eol_seen & EOL_SEEN_LF) == 0) | |
6450 | eol_seen = EOL_SEEN_CRLF; | |
6451 | else if (eol_seen != EOL_SEEN_NONE | |
24a73b0a KH |
6452 | && eol_seen != EOL_SEEN_LF |
6453 | && eol_seen != EOL_SEEN_CRLF | |
6454 | && eol_seen != EOL_SEEN_CR) | |
6455 | eol_seen = EOL_SEEN_LF; | |
df7492f9 | 6456 | if (eol_seen != EOL_SEEN_NONE) |
24a73b0a | 6457 | eol_type = adjust_coding_eol_type (coding, eol_seen); |
aaaf0b1e | 6458 | } |
d46c5b12 | 6459 | |
24a73b0a | 6460 | if (EQ (eol_type, Qmac)) |
27901516 | 6461 | { |
24a73b0a | 6462 | for (p = pbeg; p < pend; p++) |
df7492f9 KH |
6463 | if (*p == '\r') |
6464 | *p = '\n'; | |
4ed46869 | 6465 | } |
24a73b0a | 6466 | else if (EQ (eol_type, Qdos)) |
df7492f9 | 6467 | { |
24a73b0a | 6468 | int n = 0; |
b73bfc1c | 6469 | |
24a73b0a KH |
6470 | if (NILP (coding->dst_object)) |
6471 | { | |
4347441b KH |
6472 | /* Start deleting '\r' from the tail to minimize the memory |
6473 | movement. */ | |
24a73b0a KH |
6474 | for (p = pend - 2; p >= pbeg; p--) |
6475 | if (*p == '\r') | |
6476 | { | |
72af86bd | 6477 | memmove (p, p + 1, pend-- - p - 1); |
24a73b0a KH |
6478 | n++; |
6479 | } | |
6480 | } | |
6481 | else | |
6482 | { | |
4347441b KH |
6483 | int pos_byte = coding->dst_pos_byte; |
6484 | int pos = coding->dst_pos; | |
6485 | int pos_end = pos + coding->produced_char - 1; | |
6486 | ||
6487 | while (pos < pos_end) | |
6488 | { | |
6489 | p = BYTE_POS_ADDR (pos_byte); | |
6490 | if (*p == '\r' && p[1] == '\n') | |
6491 | { | |
6492 | del_range_2 (pos, pos_byte, pos + 1, pos_byte + 1, 0); | |
6493 | n++; | |
6494 | pos_end--; | |
6495 | } | |
6496 | pos++; | |
69b8522d KH |
6497 | if (coding->dst_multibyte) |
6498 | pos_byte += BYTES_BY_CHAR_HEAD (*p); | |
6499 | else | |
6500 | pos_byte++; | |
4347441b | 6501 | } |
24a73b0a KH |
6502 | } |
6503 | coding->produced -= n; | |
6504 | coding->produced_char -= n; | |
aaaf0b1e | 6505 | } |
4ed46869 KH |
6506 | } |
6507 | ||
7d64c6ad | 6508 | |
a6f87d34 KH |
6509 | /* Return a translation table (or list of them) from coding system |
6510 | attribute vector ATTRS for encoding (ENCODEP is nonzero) or | |
6511 | decoding (ENCODEP is zero). */ | |
7d64c6ad | 6512 | |
e6a54062 | 6513 | static Lisp_Object |
971de7fb | 6514 | get_translation_table (Lisp_Object attrs, int encodep, int *max_lookup) |
7d64c6ad KH |
6515 | { |
6516 | Lisp_Object standard, translation_table; | |
09ee6fdd | 6517 | Lisp_Object val; |
7d64c6ad | 6518 | |
4bed5909 CY |
6519 | if (NILP (Venable_character_translation)) |
6520 | { | |
6521 | if (max_lookup) | |
6522 | *max_lookup = 0; | |
6523 | return Qnil; | |
6524 | } | |
7d64c6ad KH |
6525 | if (encodep) |
6526 | translation_table = CODING_ATTR_ENCODE_TBL (attrs), | |
6527 | standard = Vstandard_translation_table_for_encode; | |
6528 | else | |
6529 | translation_table = CODING_ATTR_DECODE_TBL (attrs), | |
6530 | standard = Vstandard_translation_table_for_decode; | |
7d64c6ad | 6531 | if (NILP (translation_table)) |
09ee6fdd KH |
6532 | translation_table = standard; |
6533 | else | |
a6f87d34 | 6534 | { |
09ee6fdd KH |
6535 | if (SYMBOLP (translation_table)) |
6536 | translation_table = Fget (translation_table, Qtranslation_table); | |
6537 | else if (CONSP (translation_table)) | |
6538 | { | |
6539 | translation_table = Fcopy_sequence (translation_table); | |
6540 | for (val = translation_table; CONSP (val); val = XCDR (val)) | |
6541 | if (SYMBOLP (XCAR (val))) | |
6542 | XSETCAR (val, Fget (XCAR (val), Qtranslation_table)); | |
6543 | } | |
6544 | if (CHAR_TABLE_P (standard)) | |
6545 | { | |
6546 | if (CONSP (translation_table)) | |
6547 | translation_table = nconc2 (translation_table, | |
6548 | Fcons (standard, Qnil)); | |
6549 | else | |
6550 | translation_table = Fcons (translation_table, | |
6551 | Fcons (standard, Qnil)); | |
6552 | } | |
a6f87d34 | 6553 | } |
2170c8f0 KH |
6554 | |
6555 | if (max_lookup) | |
09ee6fdd | 6556 | { |
2170c8f0 KH |
6557 | *max_lookup = 1; |
6558 | if (CHAR_TABLE_P (translation_table) | |
6559 | && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (translation_table)) > 1) | |
6560 | { | |
6561 | val = XCHAR_TABLE (translation_table)->extras[1]; | |
6562 | if (NATNUMP (val) && *max_lookup < XFASTINT (val)) | |
6563 | *max_lookup = XFASTINT (val); | |
6564 | } | |
6565 | else if (CONSP (translation_table)) | |
6566 | { | |
2735d060 | 6567 | Lisp_Object tail; |
09ee6fdd | 6568 | |
2170c8f0 KH |
6569 | for (tail = translation_table; CONSP (tail); tail = XCDR (tail)) |
6570 | if (CHAR_TABLE_P (XCAR (tail)) | |
6571 | && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (XCAR (tail))) > 1) | |
6572 | { | |
2735d060 PE |
6573 | Lisp_Object tailval = XCHAR_TABLE (XCAR (tail))->extras[1]; |
6574 | if (NATNUMP (tailval) && *max_lookup < XFASTINT (tailval)) | |
6575 | *max_lookup = XFASTINT (tailval); | |
2170c8f0 KH |
6576 | } |
6577 | } | |
a6f87d34 | 6578 | } |
7d64c6ad KH |
6579 | return translation_table; |
6580 | } | |
6581 | ||
09ee6fdd KH |
6582 | #define LOOKUP_TRANSLATION_TABLE(table, c, trans) \ |
6583 | do { \ | |
6584 | trans = Qnil; \ | |
6585 | if (CHAR_TABLE_P (table)) \ | |
6586 | { \ | |
6587 | trans = CHAR_TABLE_REF (table, c); \ | |
6588 | if (CHARACTERP (trans)) \ | |
6589 | c = XFASTINT (trans), trans = Qnil; \ | |
6590 | } \ | |
6591 | else if (CONSP (table)) \ | |
6592 | { \ | |
6593 | Lisp_Object tail; \ | |
6594 | \ | |
6595 | for (tail = table; CONSP (tail); tail = XCDR (tail)) \ | |
6596 | if (CHAR_TABLE_P (XCAR (tail))) \ | |
6597 | { \ | |
6598 | trans = CHAR_TABLE_REF (XCAR (tail), c); \ | |
6599 | if (CHARACTERP (trans)) \ | |
6600 | c = XFASTINT (trans), trans = Qnil; \ | |
6601 | else if (! NILP (trans)) \ | |
6602 | break; \ | |
6603 | } \ | |
6604 | } \ | |
e6a54062 KH |
6605 | } while (0) |
6606 | ||
7d64c6ad | 6607 | |
e951386e KH |
6608 | /* Return a translation of character(s) at BUF according to TRANS. |
6609 | TRANS is TO-CHAR or ((FROM . TO) ...) where | |
6610 | FROM = [FROM-CHAR ...], TO is TO-CHAR or [TO-CHAR ...]. | |
6611 | The return value is TO-CHAR or ([FROM-CHAR ...] . TO) if a | |
6612 | translation is found, and Qnil if not found.. | |
6613 | If BUF is too short to lookup characters in FROM, return Qt. */ | |
6614 | ||
69a80ea3 | 6615 | static Lisp_Object |
971de7fb | 6616 | get_translation (Lisp_Object trans, int *buf, int *buf_end) |
69a80ea3 | 6617 | { |
e951386e KH |
6618 | |
6619 | if (INTEGERP (trans)) | |
6620 | return trans; | |
6621 | for (; CONSP (trans); trans = XCDR (trans)) | |
69a80ea3 | 6622 | { |
e951386e KH |
6623 | Lisp_Object val = XCAR (trans); |
6624 | Lisp_Object from = XCAR (val); | |
6625 | int len = ASIZE (from); | |
6626 | int i; | |
69a80ea3 | 6627 | |
e951386e | 6628 | for (i = 0; i < len; i++) |
69a80ea3 | 6629 | { |
e951386e KH |
6630 | if (buf + i == buf_end) |
6631 | return Qt; | |
6632 | if (XINT (AREF (from, i)) != buf[i]) | |
6633 | break; | |
69a80ea3 | 6634 | } |
e951386e KH |
6635 | if (i == len) |
6636 | return val; | |
69a80ea3 | 6637 | } |
e951386e | 6638 | return Qnil; |
69a80ea3 KH |
6639 | } |
6640 | ||
6641 | ||
d46c5b12 | 6642 | static int |
cf84bb53 JB |
6643 | produce_chars (struct coding_system *coding, Lisp_Object translation_table, |
6644 | int last_block) | |
4ed46869 | 6645 | { |
df7492f9 KH |
6646 | unsigned char *dst = coding->destination + coding->produced; |
6647 | unsigned char *dst_end = coding->destination + coding->dst_bytes; | |
119852e7 KH |
6648 | EMACS_INT produced; |
6649 | EMACS_INT produced_chars = 0; | |
69a80ea3 | 6650 | int carryover = 0; |
4ed46869 | 6651 | |
df7492f9 | 6652 | if (! coding->chars_at_source) |
4ed46869 | 6653 | { |
119852e7 | 6654 | /* Source characters are in coding->charbuf. */ |
fba4576f AS |
6655 | int *buf = coding->charbuf; |
6656 | int *buf_end = buf + coding->charbuf_used; | |
4ed46869 | 6657 | |
db274c7a KH |
6658 | if (EQ (coding->src_object, coding->dst_object)) |
6659 | { | |
6660 | coding_set_source (coding); | |
6661 | dst_end = ((unsigned char *) coding->source) + coding->consumed; | |
6662 | } | |
4ed46869 | 6663 | |
df7492f9 | 6664 | while (buf < buf_end) |
4ed46869 | 6665 | { |
69a80ea3 | 6666 | int c = *buf, i; |
bc4bc72a | 6667 | |
df7492f9 KH |
6668 | if (c >= 0) |
6669 | { | |
69a80ea3 KH |
6670 | int from_nchars = 1, to_nchars = 1; |
6671 | Lisp_Object trans = Qnil; | |
6672 | ||
09ee6fdd | 6673 | LOOKUP_TRANSLATION_TABLE (translation_table, c, trans); |
e6a54062 | 6674 | if (! NILP (trans)) |
69a80ea3 | 6675 | { |
e951386e KH |
6676 | trans = get_translation (trans, buf, buf_end); |
6677 | if (INTEGERP (trans)) | |
6678 | c = XINT (trans); | |
6679 | else if (CONSP (trans)) | |
6680 | { | |
6681 | from_nchars = ASIZE (XCAR (trans)); | |
6682 | trans = XCDR (trans); | |
6683 | if (INTEGERP (trans)) | |
6684 | c = XINT (trans); | |
6685 | else | |
6686 | { | |
6687 | to_nchars = ASIZE (trans); | |
6688 | c = XINT (AREF (trans, 0)); | |
6689 | } | |
6690 | } | |
6691 | else if (EQ (trans, Qt) && ! last_block) | |
69a80ea3 | 6692 | break; |
69a80ea3 KH |
6693 | } |
6694 | ||
6695 | if (dst + MAX_MULTIBYTE_LENGTH * to_nchars > dst_end) | |
6696 | { | |
6697 | dst = alloc_destination (coding, | |
6698 | buf_end - buf | |
6699 | + MAX_MULTIBYTE_LENGTH * to_nchars, | |
6700 | dst); | |
db274c7a KH |
6701 | if (EQ (coding->src_object, coding->dst_object)) |
6702 | { | |
6703 | coding_set_source (coding); | |
e951386e KH |
6704 | dst_end = (((unsigned char *) coding->source) |
6705 | + coding->consumed); | |
db274c7a KH |
6706 | } |
6707 | else | |
6708 | dst_end = coding->destination + coding->dst_bytes; | |
69a80ea3 KH |
6709 | } |
6710 | ||
433f7f87 | 6711 | for (i = 0; i < to_nchars; i++) |
69a80ea3 | 6712 | { |
433f7f87 KH |
6713 | if (i > 0) |
6714 | c = XINT (AREF (trans, i)); | |
69a80ea3 KH |
6715 | if (coding->dst_multibyte |
6716 | || ! CHAR_BYTE8_P (c)) | |
db274c7a | 6717 | CHAR_STRING_ADVANCE_NO_UNIFY (c, dst); |
69a80ea3 KH |
6718 | else |
6719 | *dst++ = CHAR_TO_BYTE8 (c); | |
6720 | } | |
6721 | produced_chars += to_nchars; | |
e951386e | 6722 | buf += from_nchars; |
d46c5b12 | 6723 | } |
df7492f9 | 6724 | else |
69a80ea3 KH |
6725 | /* This is an annotation datum. (-C) is the length. */ |
6726 | buf += -c; | |
4ed46869 | 6727 | } |
69a80ea3 | 6728 | carryover = buf_end - buf; |
4ed46869 | 6729 | } |
fa42c37f | 6730 | else |
fa42c37f | 6731 | { |
119852e7 | 6732 | /* Source characters are at coding->source. */ |
8f924df7 | 6733 | const unsigned char *src = coding->source; |
119852e7 | 6734 | const unsigned char *src_end = src + coding->consumed; |
4ed46869 | 6735 | |
db274c7a KH |
6736 | if (EQ (coding->dst_object, coding->src_object)) |
6737 | dst_end = (unsigned char *) src; | |
df7492f9 | 6738 | if (coding->src_multibyte != coding->dst_multibyte) |
fa42c37f | 6739 | { |
df7492f9 | 6740 | if (coding->src_multibyte) |
fa42c37f | 6741 | { |
71c81426 | 6742 | int multibytep = 1; |
4533845d | 6743 | EMACS_INT consumed_chars = 0; |
d46c5b12 | 6744 | |
df7492f9 KH |
6745 | while (1) |
6746 | { | |
8f924df7 | 6747 | const unsigned char *src_base = src; |
df7492f9 | 6748 | int c; |
b73bfc1c | 6749 | |
df7492f9 | 6750 | ONE_MORE_BYTE (c); |
119852e7 | 6751 | if (dst == dst_end) |
df7492f9 | 6752 | { |
119852e7 KH |
6753 | if (EQ (coding->src_object, coding->dst_object)) |
6754 | dst_end = (unsigned char *) src; | |
6755 | if (dst == dst_end) | |
df7492f9 | 6756 | { |
119852e7 KH |
6757 | EMACS_INT offset = src - coding->source; |
6758 | ||
6759 | dst = alloc_destination (coding, src_end - src + 1, | |
6760 | dst); | |
6761 | dst_end = coding->destination + coding->dst_bytes; | |
6762 | coding_set_source (coding); | |
6763 | src = coding->source + offset; | |
6764 | src_end = coding->source + coding->src_bytes; | |
db274c7a KH |
6765 | if (EQ (coding->src_object, coding->dst_object)) |
6766 | dst_end = (unsigned char *) src; | |
df7492f9 | 6767 | } |
df7492f9 KH |
6768 | } |
6769 | *dst++ = c; | |
6770 | produced_chars++; | |
6771 | } | |
6772 | no_more_source: | |
6773 | ; | |
fa42c37f KH |
6774 | } |
6775 | else | |
df7492f9 KH |
6776 | while (src < src_end) |
6777 | { | |
71c81426 | 6778 | int multibytep = 1; |
df7492f9 | 6779 | int c = *src++; |
b73bfc1c | 6780 | |
df7492f9 KH |
6781 | if (dst >= dst_end - 1) |
6782 | { | |
2c78b7e1 | 6783 | if (EQ (coding->src_object, coding->dst_object)) |
8f924df7 | 6784 | dst_end = (unsigned char *) src; |
2c78b7e1 KH |
6785 | if (dst >= dst_end - 1) |
6786 | { | |
119852e7 | 6787 | EMACS_INT offset = src - coding->source; |
db274c7a | 6788 | EMACS_INT more_bytes; |
119852e7 | 6789 | |
db274c7a KH |
6790 | if (EQ (coding->src_object, coding->dst_object)) |
6791 | more_bytes = ((src_end - src) / 2) + 2; | |
6792 | else | |
6793 | more_bytes = src_end - src + 2; | |
6794 | dst = alloc_destination (coding, more_bytes, dst); | |
2c78b7e1 KH |
6795 | dst_end = coding->destination + coding->dst_bytes; |
6796 | coding_set_source (coding); | |
119852e7 | 6797 | src = coding->source + offset; |
2c78b7e1 | 6798 | src_end = coding->source + coding->src_bytes; |
db274c7a KH |
6799 | if (EQ (coding->src_object, coding->dst_object)) |
6800 | dst_end = (unsigned char *) src; | |
2c78b7e1 | 6801 | } |
df7492f9 KH |
6802 | } |
6803 | EMIT_ONE_BYTE (c); | |
6804 | } | |
d46c5b12 | 6805 | } |
df7492f9 KH |
6806 | else |
6807 | { | |
6808 | if (!EQ (coding->src_object, coding->dst_object)) | |
fa42c37f | 6809 | { |
119852e7 | 6810 | EMACS_INT require = coding->src_bytes - coding->dst_bytes; |
4ed46869 | 6811 | |
df7492f9 | 6812 | if (require > 0) |
fa42c37f | 6813 | { |
df7492f9 KH |
6814 | EMACS_INT offset = src - coding->source; |
6815 | ||
6816 | dst = alloc_destination (coding, require, dst); | |
6817 | coding_set_source (coding); | |
6818 | src = coding->source + offset; | |
6819 | src_end = coding->source + coding->src_bytes; | |
fa42c37f KH |
6820 | } |
6821 | } | |
119852e7 | 6822 | produced_chars = coding->consumed_char; |
df7492f9 | 6823 | while (src < src_end) |
14daee73 | 6824 | *dst++ = *src++; |
fa42c37f KH |
6825 | } |
6826 | } | |
6827 | ||
df7492f9 | 6828 | produced = dst - (coding->destination + coding->produced); |
284201e4 | 6829 | if (BUFFERP (coding->dst_object) && produced_chars > 0) |
df7492f9 KH |
6830 | insert_from_gap (produced_chars, produced); |
6831 | coding->produced += produced; | |
6832 | coding->produced_char += produced_chars; | |
69a80ea3 | 6833 | return carryover; |
fa42c37f KH |
6834 | } |
6835 | ||
ff0dacd7 KH |
6836 | /* Compose text in CODING->object according to the annotation data at |
6837 | CHARBUF. CHARBUF is an array: | |
e951386e | 6838 | [ -LENGTH ANNOTATION_MASK NCHARS NBYTES METHOD [ COMPONENTS... ] ] |
df7492f9 | 6839 | */ |
4ed46869 | 6840 | |
df7492f9 | 6841 | static INLINE void |
971de7fb | 6842 | produce_composition (struct coding_system *coding, int *charbuf, EMACS_INT pos) |
4ed46869 | 6843 | { |
df7492f9 | 6844 | int len; |
69a80ea3 | 6845 | EMACS_INT to; |
df7492f9 | 6846 | enum composition_method method; |
df7492f9 | 6847 | Lisp_Object components; |
fa42c37f | 6848 | |
e951386e | 6849 | len = -charbuf[0] - MAX_ANNOTATION_LENGTH; |
69a80ea3 | 6850 | to = pos + charbuf[2]; |
e951386e | 6851 | method = (enum composition_method) (charbuf[4]); |
d46c5b12 | 6852 | |
df7492f9 KH |
6853 | if (method == COMPOSITION_RELATIVE) |
6854 | components = Qnil; | |
e951386e | 6855 | else |
d46c5b12 | 6856 | { |
df7492f9 | 6857 | Lisp_Object args[MAX_COMPOSITION_COMPONENTS * 2 - 1]; |
e951386e | 6858 | int i, j; |
b73bfc1c | 6859 | |
e951386e KH |
6860 | if (method == COMPOSITION_WITH_RULE) |
6861 | len = charbuf[2] * 3 - 2; | |
6862 | charbuf += MAX_ANNOTATION_LENGTH; | |
6863 | /* charbuf = [ CHRA ... CHAR] or [ CHAR -2 RULE ... CHAR ] */ | |
6864 | for (i = j = 0; i < len && charbuf[i] != -1; i++, j++) | |
9ffd559c | 6865 | { |
e951386e KH |
6866 | if (charbuf[i] >= 0) |
6867 | args[j] = make_number (charbuf[i]); | |
6868 | else | |
6869 | { | |
6870 | i++; | |
6871 | args[j] = make_number (charbuf[i] % 0x100); | |
6872 | } | |
9ffd559c | 6873 | } |
e951386e | 6874 | components = (i == j ? Fstring (j, args) : Fvector (j, args)); |
d46c5b12 | 6875 | } |
69a80ea3 | 6876 | compose_text (pos, to, components, Qnil, coding->dst_object); |
d46c5b12 KH |
6877 | } |
6878 | ||
d46c5b12 | 6879 | |
ff0dacd7 KH |
6880 | /* Put `charset' property on text in CODING->object according to |
6881 | the annotation data at CHARBUF. CHARBUF is an array: | |
69a80ea3 | 6882 | [ -LENGTH ANNOTATION_MASK NCHARS CHARSET-ID ] |
ff0dacd7 | 6883 | */ |
d46c5b12 | 6884 | |
ff0dacd7 | 6885 | static INLINE void |
971de7fb | 6886 | produce_charset (struct coding_system *coding, int *charbuf, EMACS_INT pos) |
d46c5b12 | 6887 | { |
69a80ea3 KH |
6888 | EMACS_INT from = pos - charbuf[2]; |
6889 | struct charset *charset = CHARSET_FROM_ID (charbuf[3]); | |
b73bfc1c | 6890 | |
69a80ea3 | 6891 | Fput_text_property (make_number (from), make_number (pos), |
ff0dacd7 KH |
6892 | Qcharset, CHARSET_NAME (charset), |
6893 | coding->dst_object); | |
d46c5b12 KH |
6894 | } |
6895 | ||
d46c5b12 | 6896 | |
df7492f9 KH |
6897 | #define CHARBUF_SIZE 0x4000 |
6898 | ||
6899 | #define ALLOC_CONVERSION_WORK_AREA(coding) \ | |
6900 | do { \ | |
8510724d | 6901 | int size = CHARBUF_SIZE; \ |
df7492f9 KH |
6902 | \ |
6903 | coding->charbuf = NULL; \ | |
6904 | while (size > 1024) \ | |
6905 | { \ | |
6906 | coding->charbuf = (int *) alloca (sizeof (int) * size); \ | |
6907 | if (coding->charbuf) \ | |
6908 | break; \ | |
6909 | size >>= 1; \ | |
6910 | } \ | |
6911 | if (! coding->charbuf) \ | |
6912 | { \ | |
065e3595 | 6913 | record_conversion_result (coding, CODING_RESULT_INSUFFICIENT_MEM); \ |
df7492f9 KH |
6914 | return coding->result; \ |
6915 | } \ | |
6916 | coding->charbuf_size = size; \ | |
6917 | } while (0) | |
4ed46869 | 6918 | |
d46c5b12 KH |
6919 | |
6920 | static void | |
971de7fb | 6921 | produce_annotation (struct coding_system *coding, EMACS_INT pos) |
d46c5b12 | 6922 | { |
df7492f9 KH |
6923 | int *charbuf = coding->charbuf; |
6924 | int *charbuf_end = charbuf + coding->charbuf_used; | |
d46c5b12 | 6925 | |
ff0dacd7 KH |
6926 | if (NILP (coding->dst_object)) |
6927 | return; | |
d46c5b12 | 6928 | |
df7492f9 | 6929 | while (charbuf < charbuf_end) |
a84f1519 | 6930 | { |
df7492f9 | 6931 | if (*charbuf >= 0) |
e951386e | 6932 | pos++, charbuf++; |
d46c5b12 | 6933 | else |
d46c5b12 | 6934 | { |
df7492f9 | 6935 | int len = -*charbuf; |
e951386e KH |
6936 | |
6937 | if (len > 2) | |
6938 | switch (charbuf[1]) | |
6939 | { | |
6940 | case CODING_ANNOTATE_COMPOSITION_MASK: | |
6941 | produce_composition (coding, charbuf, pos); | |
6942 | break; | |
6943 | case CODING_ANNOTATE_CHARSET_MASK: | |
6944 | produce_charset (coding, charbuf, pos); | |
6945 | break; | |
6946 | } | |
df7492f9 | 6947 | charbuf += len; |
d46c5b12 | 6948 | } |
a84f1519 | 6949 | } |
d46c5b12 KH |
6950 | } |
6951 | ||
df7492f9 KH |
6952 | /* Decode the data at CODING->src_object into CODING->dst_object. |
6953 | CODING->src_object is a buffer, a string, or nil. | |
6954 | CODING->dst_object is a buffer. | |
d46c5b12 | 6955 | |
df7492f9 KH |
6956 | If CODING->src_object is a buffer, it must be the current buffer. |
6957 | In this case, if CODING->src_pos is positive, it is a position of | |
6958 | the source text in the buffer, otherwise, the source text is in the | |
6959 | gap area of the buffer, and CODING->src_pos specifies the offset of | |
6960 | the text from GPT (which must be the same as PT). If this is the | |
6961 | same buffer as CODING->dst_object, CODING->src_pos must be | |
6962 | negative. | |
d46c5b12 | 6963 | |
b6828792 | 6964 | If CODING->src_object is a string, CODING->src_pos is an index to |
df7492f9 | 6965 | that string. |
d46c5b12 | 6966 | |
df7492f9 KH |
6967 | If CODING->src_object is nil, CODING->source must already point to |
6968 | the non-relocatable memory area. In this case, CODING->src_pos is | |
6969 | an offset from CODING->source. | |
73be902c | 6970 | |
df7492f9 KH |
6971 | The decoded data is inserted at the current point of the buffer |
6972 | CODING->dst_object. | |
6973 | */ | |
d46c5b12 | 6974 | |
df7492f9 | 6975 | static int |
971de7fb | 6976 | decode_coding (struct coding_system *coding) |
d46c5b12 | 6977 | { |
df7492f9 | 6978 | Lisp_Object attrs; |
24a73b0a | 6979 | Lisp_Object undo_list; |
7d64c6ad | 6980 | Lisp_Object translation_table; |
d0396581 | 6981 | struct ccl_spec cclspec; |
69a80ea3 KH |
6982 | int carryover; |
6983 | int i; | |
d46c5b12 | 6984 | |
df7492f9 KH |
6985 | if (BUFFERP (coding->src_object) |
6986 | && coding->src_pos > 0 | |
6987 | && coding->src_pos < GPT | |
6988 | && coding->src_pos + coding->src_chars > GPT) | |
6989 | move_gap_both (coding->src_pos, coding->src_pos_byte); | |
d46c5b12 | 6990 | |
24a73b0a | 6991 | undo_list = Qt; |
df7492f9 | 6992 | if (BUFFERP (coding->dst_object)) |
1c3478b0 | 6993 | { |
df7492f9 KH |
6994 | if (current_buffer != XBUFFER (coding->dst_object)) |
6995 | set_buffer_internal (XBUFFER (coding->dst_object)); | |
6996 | if (GPT != PT) | |
6997 | move_gap_both (PT, PT_BYTE); | |
4b4deea2 TT |
6998 | undo_list = BVAR (current_buffer, undo_list); |
6999 | BVAR (current_buffer, undo_list) = Qt; | |
1c3478b0 KH |
7000 | } |
7001 | ||
df7492f9 KH |
7002 | coding->consumed = coding->consumed_char = 0; |
7003 | coding->produced = coding->produced_char = 0; | |
7004 | coding->chars_at_source = 0; | |
065e3595 | 7005 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
df7492f9 | 7006 | coding->errors = 0; |
1c3478b0 | 7007 | |
df7492f9 KH |
7008 | ALLOC_CONVERSION_WORK_AREA (coding); |
7009 | ||
7010 | attrs = CODING_ID_ATTRS (coding->id); | |
2170c8f0 | 7011 | translation_table = get_translation_table (attrs, 0, NULL); |
df7492f9 | 7012 | |
69a80ea3 | 7013 | carryover = 0; |
d0396581 KH |
7014 | if (coding->decoder == decode_coding_ccl) |
7015 | { | |
7016 | coding->spec.ccl = &cclspec; | |
7017 | setup_ccl_program (&cclspec.ccl, CODING_CCL_DECODER (coding)); | |
7018 | } | |
df7492f9 | 7019 | do |
b73bfc1c | 7020 | { |
69a80ea3 KH |
7021 | EMACS_INT pos = coding->dst_pos + coding->produced_char; |
7022 | ||
df7492f9 KH |
7023 | coding_set_source (coding); |
7024 | coding->annotated = 0; | |
69a80ea3 | 7025 | coding->charbuf_used = carryover; |
df7492f9 | 7026 | (*(coding->decoder)) (coding); |
df7492f9 | 7027 | coding_set_destination (coding); |
69a80ea3 | 7028 | carryover = produce_chars (coding, translation_table, 0); |
df7492f9 | 7029 | if (coding->annotated) |
69a80ea3 KH |
7030 | produce_annotation (coding, pos); |
7031 | for (i = 0; i < carryover; i++) | |
7032 | coding->charbuf[i] | |
7033 | = coding->charbuf[coding->charbuf_used - carryover + i]; | |
d46c5b12 | 7034 | } |
d0396581 KH |
7035 | while (coding->result == CODING_RESULT_INSUFFICIENT_DST |
7036 | || (coding->consumed < coding->src_bytes | |
7037 | && (coding->result == CODING_RESULT_SUCCESS | |
7038 | || coding->result == CODING_RESULT_INVALID_SRC))); | |
d46c5b12 | 7039 | |
69a80ea3 KH |
7040 | if (carryover > 0) |
7041 | { | |
7042 | coding_set_destination (coding); | |
7043 | coding->charbuf_used = carryover; | |
7044 | produce_chars (coding, translation_table, 1); | |
7045 | } | |
7046 | ||
df7492f9 KH |
7047 | coding->carryover_bytes = 0; |
7048 | if (coding->consumed < coding->src_bytes) | |
d46c5b12 | 7049 | { |
df7492f9 | 7050 | int nbytes = coding->src_bytes - coding->consumed; |
8f924df7 | 7051 | const unsigned char *src; |
df7492f9 KH |
7052 | |
7053 | coding_set_source (coding); | |
7054 | coding_set_destination (coding); | |
7055 | src = coding->source + coding->consumed; | |
7056 | ||
7057 | if (coding->mode & CODING_MODE_LAST_BLOCK) | |
1c3478b0 | 7058 | { |
df7492f9 KH |
7059 | /* Flush out unprocessed data as binary chars. We are sure |
7060 | that the number of data is less than the size of | |
7061 | coding->charbuf. */ | |
065e3595 | 7062 | coding->charbuf_used = 0; |
b2dab6c8 JR |
7063 | coding->chars_at_source = 0; |
7064 | ||
df7492f9 | 7065 | while (nbytes-- > 0) |
1c3478b0 | 7066 | { |
df7492f9 | 7067 | int c = *src++; |
98725083 | 7068 | |
1c91457d KH |
7069 | if (c & 0x80) |
7070 | c = BYTE8_TO_CHAR (c); | |
7071 | coding->charbuf[coding->charbuf_used++] = c; | |
1c3478b0 | 7072 | } |
f6cbaf43 | 7073 | produce_chars (coding, Qnil, 1); |
d46c5b12 | 7074 | } |
d46c5b12 | 7075 | else |
df7492f9 KH |
7076 | { |
7077 | /* Record unprocessed bytes in coding->carryover. We are | |
7078 | sure that the number of data is less than the size of | |
7079 | coding->carryover. */ | |
7080 | unsigned char *p = coding->carryover; | |
7081 | ||
f289d375 KH |
7082 | if (nbytes > sizeof coding->carryover) |
7083 | nbytes = sizeof coding->carryover; | |
df7492f9 KH |
7084 | coding->carryover_bytes = nbytes; |
7085 | while (nbytes-- > 0) | |
7086 | *p++ = *src++; | |
1c3478b0 | 7087 | } |
df7492f9 | 7088 | coding->consumed = coding->src_bytes; |
b73bfc1c | 7089 | } |
69f76525 | 7090 | |
0a9564cb EZ |
7091 | if (! EQ (CODING_ID_EOL_TYPE (coding->id), Qunix) |
7092 | && !inhibit_eol_conversion) | |
4347441b | 7093 | decode_eol (coding); |
24a73b0a KH |
7094 | if (BUFFERP (coding->dst_object)) |
7095 | { | |
4b4deea2 | 7096 | BVAR (current_buffer, undo_list) = undo_list; |
24a73b0a KH |
7097 | record_insert (coding->dst_pos, coding->produced_char); |
7098 | } | |
73be902c | 7099 | return coding->result; |
4ed46869 KH |
7100 | } |
7101 | ||
aaaf0b1e | 7102 | |
e1c23804 | 7103 | /* Extract an annotation datum from a composition starting at POS and |
ff0dacd7 KH |
7104 | ending before LIMIT of CODING->src_object (buffer or string), store |
7105 | the data in BUF, set *STOP to a starting position of the next | |
7106 | composition (if any) or to LIMIT, and return the address of the | |
7107 | next element of BUF. | |
7108 | ||
7109 | If such an annotation is not found, set *STOP to a starting | |
7110 | position of a composition after POS (if any) or to LIMIT, and | |
7111 | return BUF. */ | |
7112 | ||
7113 | static INLINE int * | |
cf84bb53 JB |
7114 | handle_composition_annotation (EMACS_INT pos, EMACS_INT limit, |
7115 | struct coding_system *coding, int *buf, | |
7116 | EMACS_INT *stop) | |
aaaf0b1e | 7117 | { |
ff0dacd7 KH |
7118 | EMACS_INT start, end; |
7119 | Lisp_Object prop; | |
aaaf0b1e | 7120 | |
ff0dacd7 KH |
7121 | if (! find_composition (pos, limit, &start, &end, &prop, coding->src_object) |
7122 | || end > limit) | |
7123 | *stop = limit; | |
7124 | else if (start > pos) | |
7125 | *stop = start; | |
7126 | else | |
aaaf0b1e | 7127 | { |
ff0dacd7 | 7128 | if (start == pos) |
aaaf0b1e | 7129 | { |
ff0dacd7 KH |
7130 | /* We found a composition. Store the corresponding |
7131 | annotation data in BUF. */ | |
7132 | int *head = buf; | |
7133 | enum composition_method method = COMPOSITION_METHOD (prop); | |
7134 | int nchars = COMPOSITION_LENGTH (prop); | |
7135 | ||
e951386e | 7136 | ADD_COMPOSITION_DATA (buf, nchars, 0, method); |
ff0dacd7 | 7137 | if (method != COMPOSITION_RELATIVE) |
aaaf0b1e | 7138 | { |
ff0dacd7 KH |
7139 | Lisp_Object components; |
7140 | int len, i, i_byte; | |
7141 | ||
7142 | components = COMPOSITION_COMPONENTS (prop); | |
7143 | if (VECTORP (components)) | |
aaaf0b1e | 7144 | { |
ff0dacd7 KH |
7145 | len = XVECTOR (components)->size; |
7146 | for (i = 0; i < len; i++) | |
7147 | *buf++ = XINT (AREF (components, i)); | |
aaaf0b1e | 7148 | } |
ff0dacd7 | 7149 | else if (STRINGP (components)) |
aaaf0b1e | 7150 | { |
8f924df7 | 7151 | len = SCHARS (components); |
ff0dacd7 KH |
7152 | i = i_byte = 0; |
7153 | while (i < len) | |
7154 | { | |
7155 | FETCH_STRING_CHAR_ADVANCE (*buf, components, i, i_byte); | |
7156 | buf++; | |
7157 | } | |
7158 | } | |
7159 | else if (INTEGERP (components)) | |
7160 | { | |
7161 | len = 1; | |
7162 | *buf++ = XINT (components); | |
7163 | } | |
7164 | else if (CONSP (components)) | |
7165 | { | |
7166 | for (len = 0; CONSP (components); | |
7167 | len++, components = XCDR (components)) | |
7168 | *buf++ = XINT (XCAR (components)); | |
aaaf0b1e | 7169 | } |
aaaf0b1e | 7170 | else |
ff0dacd7 KH |
7171 | abort (); |
7172 | *head -= len; | |
aaaf0b1e | 7173 | } |
aaaf0b1e | 7174 | } |
ff0dacd7 KH |
7175 | |
7176 | if (find_composition (end, limit, &start, &end, &prop, | |
7177 | coding->src_object) | |
7178 | && end <= limit) | |
7179 | *stop = start; | |
7180 | else | |
7181 | *stop = limit; | |
aaaf0b1e | 7182 | } |
ff0dacd7 KH |
7183 | return buf; |
7184 | } | |
7185 | ||
7186 | ||
e1c23804 | 7187 | /* Extract an annotation datum from a text property `charset' at POS of |
ff0dacd7 KH |
7188 | CODING->src_object (buffer of string), store the data in BUF, set |
7189 | *STOP to the position where the value of `charset' property changes | |
7190 | (limiting by LIMIT), and return the address of the next element of | |
7191 | BUF. | |
7192 | ||
7193 | If the property value is nil, set *STOP to the position where the | |
7194 | property value is non-nil (limiting by LIMIT), and return BUF. */ | |
7195 | ||
7196 | static INLINE int * | |
cf84bb53 JB |
7197 | handle_charset_annotation (EMACS_INT pos, EMACS_INT limit, |
7198 | struct coding_system *coding, int *buf, | |
7199 | EMACS_INT *stop) | |
ff0dacd7 KH |
7200 | { |
7201 | Lisp_Object val, next; | |
7202 | int id; | |
7203 | ||
7204 | val = Fget_text_property (make_number (pos), Qcharset, coding->src_object); | |
7205 | if (! NILP (val) && CHARSETP (val)) | |
7206 | id = XINT (CHARSET_SYMBOL_ID (val)); | |
7207 | else | |
7208 | id = -1; | |
69a80ea3 | 7209 | ADD_CHARSET_DATA (buf, 0, id); |
ff0dacd7 KH |
7210 | next = Fnext_single_property_change (make_number (pos), Qcharset, |
7211 | coding->src_object, | |
7212 | make_number (limit)); | |
7213 | *stop = XINT (next); | |
7214 | return buf; | |
7215 | } | |
7216 | ||
7217 | ||
df7492f9 | 7218 | static void |
cf84bb53 JB |
7219 | consume_chars (struct coding_system *coding, Lisp_Object translation_table, |
7220 | int max_lookup) | |
df7492f9 KH |
7221 | { |
7222 | int *buf = coding->charbuf; | |
ff0dacd7 | 7223 | int *buf_end = coding->charbuf + coding->charbuf_size; |
7c78e542 | 7224 | const unsigned char *src = coding->source + coding->consumed; |
4776e638 | 7225 | const unsigned char *src_end = coding->source + coding->src_bytes; |
ff0dacd7 KH |
7226 | EMACS_INT pos = coding->src_pos + coding->consumed_char; |
7227 | EMACS_INT end_pos = coding->src_pos + coding->src_chars; | |
df7492f9 KH |
7228 | int multibytep = coding->src_multibyte; |
7229 | Lisp_Object eol_type; | |
7230 | int c; | |
ff0dacd7 | 7231 | EMACS_INT stop, stop_composition, stop_charset; |
09ee6fdd | 7232 | int *lookup_buf = NULL; |
433f7f87 KH |
7233 | |
7234 | if (! NILP (translation_table)) | |
09ee6fdd | 7235 | lookup_buf = alloca (sizeof (int) * max_lookup); |
88993dfd | 7236 | |
0a9564cb | 7237 | eol_type = inhibit_eol_conversion ? Qunix : CODING_ID_EOL_TYPE (coding->id); |
df7492f9 KH |
7238 | if (VECTORP (eol_type)) |
7239 | eol_type = Qunix; | |
88993dfd | 7240 | |
df7492f9 KH |
7241 | /* Note: composition handling is not yet implemented. */ |
7242 | coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK; | |
ec6d2bb8 | 7243 | |
0b5670c9 KH |
7244 | if (NILP (coding->src_object)) |
7245 | stop = stop_composition = stop_charset = end_pos; | |
ff0dacd7 | 7246 | else |
0b5670c9 KH |
7247 | { |
7248 | if (coding->common_flags & CODING_ANNOTATE_COMPOSITION_MASK) | |
7249 | stop = stop_composition = pos; | |
7250 | else | |
7251 | stop = stop_composition = end_pos; | |
7252 | if (coding->common_flags & CODING_ANNOTATE_CHARSET_MASK) | |
7253 | stop = stop_charset = pos; | |
7254 | else | |
7255 | stop_charset = end_pos; | |
7256 | } | |
ec6d2bb8 | 7257 | |
24a73b0a | 7258 | /* Compensate for CRLF and conversion. */ |
ff0dacd7 | 7259 | buf_end -= 1 + MAX_ANNOTATION_LENGTH; |
df7492f9 | 7260 | while (buf < buf_end) |
aaaf0b1e | 7261 | { |
433f7f87 KH |
7262 | Lisp_Object trans; |
7263 | ||
df7492f9 | 7264 | if (pos == stop) |
ec6d2bb8 | 7265 | { |
df7492f9 KH |
7266 | if (pos == end_pos) |
7267 | break; | |
ff0dacd7 KH |
7268 | if (pos == stop_composition) |
7269 | buf = handle_composition_annotation (pos, end_pos, coding, | |
7270 | buf, &stop_composition); | |
7271 | if (pos == stop_charset) | |
7272 | buf = handle_charset_annotation (pos, end_pos, coding, | |
7273 | buf, &stop_charset); | |
7274 | stop = (stop_composition < stop_charset | |
7275 | ? stop_composition : stop_charset); | |
df7492f9 KH |
7276 | } |
7277 | ||
7278 | if (! multibytep) | |
4776e638 | 7279 | { |
d3e4cb56 | 7280 | EMACS_INT bytes; |
aaaf0b1e | 7281 | |
4d1e6632 KH |
7282 | if (coding->encoder == encode_coding_raw_text |
7283 | || coding->encoder == encode_coding_ccl) | |
ea29edf2 KH |
7284 | c = *src++, pos++; |
7285 | else if ((bytes = MULTIBYTE_LENGTH (src, src_end)) > 0) | |
db274c7a | 7286 | c = STRING_CHAR_ADVANCE_NO_UNIFY (src), pos += bytes; |
4776e638 | 7287 | else |
f03caae0 | 7288 | c = BYTE8_TO_CHAR (*src), src++, pos++; |
4776e638 | 7289 | } |
df7492f9 | 7290 | else |
db274c7a | 7291 | c = STRING_CHAR_ADVANCE_NO_UNIFY (src), pos++; |
df7492f9 KH |
7292 | if ((c == '\r') && (coding->mode & CODING_MODE_SELECTIVE_DISPLAY)) |
7293 | c = '\n'; | |
7294 | if (! EQ (eol_type, Qunix)) | |
aaaf0b1e | 7295 | { |
df7492f9 | 7296 | if (c == '\n') |
aaaf0b1e | 7297 | { |
df7492f9 KH |
7298 | if (EQ (eol_type, Qdos)) |
7299 | *buf++ = '\r'; | |
7300 | else | |
7301 | c = '\r'; | |
aaaf0b1e KH |
7302 | } |
7303 | } | |
433f7f87 | 7304 | |
e6a54062 | 7305 | trans = Qnil; |
09ee6fdd | 7306 | LOOKUP_TRANSLATION_TABLE (translation_table, c, trans); |
e6a54062 | 7307 | if (NILP (trans)) |
433f7f87 KH |
7308 | *buf++ = c; |
7309 | else | |
7310 | { | |
7311 | int from_nchars = 1, to_nchars = 1; | |
7312 | int *lookup_buf_end; | |
7313 | const unsigned char *p = src; | |
7314 | int i; | |
7315 | ||
7316 | lookup_buf[0] = c; | |
7317 | for (i = 1; i < max_lookup && p < src_end; i++) | |
7318 | lookup_buf[i] = STRING_CHAR_ADVANCE (p); | |
7319 | lookup_buf_end = lookup_buf + i; | |
e951386e KH |
7320 | trans = get_translation (trans, lookup_buf, lookup_buf_end); |
7321 | if (INTEGERP (trans)) | |
7322 | c = XINT (trans); | |
7323 | else if (CONSP (trans)) | |
7324 | { | |
7325 | from_nchars = ASIZE (XCAR (trans)); | |
7326 | trans = XCDR (trans); | |
7327 | if (INTEGERP (trans)) | |
7328 | c = XINT (trans); | |
7329 | else | |
7330 | { | |
7331 | to_nchars = ASIZE (trans); | |
7332 | if (buf + to_nchars > buf_end) | |
7333 | break; | |
7334 | c = XINT (AREF (trans, 0)); | |
7335 | } | |
7336 | } | |
7337 | else | |
433f7f87 | 7338 | break; |
e951386e | 7339 | *buf++ = c; |
433f7f87 KH |
7340 | for (i = 1; i < to_nchars; i++) |
7341 | *buf++ = XINT (AREF (trans, i)); | |
7342 | for (i = 1; i < from_nchars; i++, pos++) | |
7343 | src += MULTIBYTE_LENGTH_NO_CHECK (src); | |
7344 | } | |
aaaf0b1e | 7345 | } |
ec6d2bb8 | 7346 | |
df7492f9 KH |
7347 | coding->consumed = src - coding->source; |
7348 | coding->consumed_char = pos - coding->src_pos; | |
7349 | coding->charbuf_used = buf - coding->charbuf; | |
7350 | coding->chars_at_source = 0; | |
aaaf0b1e KH |
7351 | } |
7352 | ||
4ed46869 | 7353 | |
df7492f9 KH |
7354 | /* Encode the text at CODING->src_object into CODING->dst_object. |
7355 | CODING->src_object is a buffer or a string. | |
7356 | CODING->dst_object is a buffer or nil. | |
7357 | ||
7358 | If CODING->src_object is a buffer, it must be the current buffer. | |
7359 | In this case, if CODING->src_pos is positive, it is a position of | |
7360 | the source text in the buffer, otherwise. the source text is in the | |
7361 | gap area of the buffer, and coding->src_pos specifies the offset of | |
7362 | the text from GPT (which must be the same as PT). If this is the | |
7363 | same buffer as CODING->dst_object, CODING->src_pos must be | |
7364 | negative and CODING should not have `pre-write-conversion'. | |
7365 | ||
7366 | If CODING->src_object is a string, CODING should not have | |
7367 | `pre-write-conversion'. | |
7368 | ||
7369 | If CODING->dst_object is a buffer, the encoded data is inserted at | |
7370 | the current point of that buffer. | |
7371 | ||
7372 | If CODING->dst_object is nil, the encoded data is placed at the | |
7373 | memory area specified by CODING->destination. */ | |
7374 | ||
7375 | static int | |
971de7fb | 7376 | encode_coding (struct coding_system *coding) |
4ed46869 | 7377 | { |
df7492f9 | 7378 | Lisp_Object attrs; |
7d64c6ad | 7379 | Lisp_Object translation_table; |
09ee6fdd | 7380 | int max_lookup; |
fb608df3 | 7381 | struct ccl_spec cclspec; |
9861e777 | 7382 | |
df7492f9 | 7383 | attrs = CODING_ID_ATTRS (coding->id); |
ea29edf2 KH |
7384 | if (coding->encoder == encode_coding_raw_text) |
7385 | translation_table = Qnil, max_lookup = 0; | |
7386 | else | |
7387 | translation_table = get_translation_table (attrs, 1, &max_lookup); | |
4ed46869 | 7388 | |
df7492f9 | 7389 | if (BUFFERP (coding->dst_object)) |
8844fa83 | 7390 | { |
df7492f9 KH |
7391 | set_buffer_internal (XBUFFER (coding->dst_object)); |
7392 | coding->dst_multibyte | |
4b4deea2 | 7393 | = ! NILP (BVAR (current_buffer, enable_multibyte_characters)); |
8844fa83 | 7394 | } |
4ed46869 | 7395 | |
b73bfc1c | 7396 | coding->consumed = coding->consumed_char = 0; |
df7492f9 | 7397 | coding->produced = coding->produced_char = 0; |
065e3595 | 7398 | record_conversion_result (coding, CODING_RESULT_SUCCESS); |
b73bfc1c | 7399 | coding->errors = 0; |
b73bfc1c | 7400 | |
df7492f9 | 7401 | ALLOC_CONVERSION_WORK_AREA (coding); |
4ed46869 | 7402 | |
fb608df3 KH |
7403 | if (coding->encoder == encode_coding_ccl) |
7404 | { | |
7405 | coding->spec.ccl = &cclspec; | |
7406 | setup_ccl_program (&cclspec.ccl, CODING_CCL_ENCODER (coding)); | |
7407 | } | |
df7492f9 KH |
7408 | do { |
7409 | coding_set_source (coding); | |
09ee6fdd | 7410 | consume_chars (coding, translation_table, max_lookup); |
df7492f9 KH |
7411 | coding_set_destination (coding); |
7412 | (*(coding->encoder)) (coding); | |
7413 | } while (coding->consumed_char < coding->src_chars); | |
7414 | ||
284201e4 | 7415 | if (BUFFERP (coding->dst_object) && coding->produced_char > 0) |
df7492f9 KH |
7416 | insert_from_gap (coding->produced_char, coding->produced); |
7417 | ||
7418 | return (coding->result); | |
ec6d2bb8 KH |
7419 | } |
7420 | ||
fb88bf2d | 7421 | |
24a73b0a KH |
7422 | /* Name (or base name) of work buffer for code conversion. */ |
7423 | static Lisp_Object Vcode_conversion_workbuf_name; | |
d46c5b12 | 7424 | |
24a73b0a KH |
7425 | /* A working buffer used by the top level conversion. Once it is |
7426 | created, it is never destroyed. It has the name | |
7427 | Vcode_conversion_workbuf_name. The other working buffers are | |
7428 | destroyed after the use is finished, and their names are modified | |
7429 | versions of Vcode_conversion_workbuf_name. */ | |
7430 | static Lisp_Object Vcode_conversion_reused_workbuf; | |
b73bfc1c | 7431 | |
24a73b0a KH |
7432 | /* 1 iff Vcode_conversion_reused_workbuf is already in use. */ |
7433 | static int reused_workbuf_in_use; | |
4ed46869 | 7434 | |
24a73b0a | 7435 | |
ad1746f5 | 7436 | /* Return a working buffer of code conversion. MULTIBYTE specifies the |
24a73b0a | 7437 | multibyteness of returning buffer. */ |
b73bfc1c | 7438 | |
f6cbaf43 | 7439 | static Lisp_Object |
971de7fb | 7440 | make_conversion_work_buffer (int multibyte) |
df7492f9 | 7441 | { |
24a73b0a KH |
7442 | Lisp_Object name, workbuf; |
7443 | struct buffer *current; | |
4ed46869 | 7444 | |
24a73b0a | 7445 | if (reused_workbuf_in_use++) |
065e3595 KH |
7446 | { |
7447 | name = Fgenerate_new_buffer_name (Vcode_conversion_workbuf_name, Qnil); | |
7448 | workbuf = Fget_buffer_create (name); | |
7449 | } | |
df7492f9 | 7450 | else |
065e3595 | 7451 | { |
159bd5a2 | 7452 | if (NILP (Fbuffer_live_p (Vcode_conversion_reused_workbuf))) |
a993c7a1 KH |
7453 | Vcode_conversion_reused_workbuf |
7454 | = Fget_buffer_create (Vcode_conversion_workbuf_name); | |
7455 | workbuf = Vcode_conversion_reused_workbuf; | |
065e3595 | 7456 | } |
24a73b0a KH |
7457 | current = current_buffer; |
7458 | set_buffer_internal (XBUFFER (workbuf)); | |
df36ff1f CY |
7459 | /* We can't allow modification hooks to run in the work buffer. For |
7460 | instance, directory_files_internal assumes that file decoding | |
7461 | doesn't compile new regexps. */ | |
7462 | Fset (Fmake_local_variable (Qinhibit_modification_hooks), Qt); | |
3ed051d4 | 7463 | Ferase_buffer (); |
4b4deea2 TT |
7464 | BVAR (current_buffer, undo_list) = Qt; |
7465 | BVAR (current_buffer, enable_multibyte_characters) = multibyte ? Qt : Qnil; | |
df7492f9 | 7466 | set_buffer_internal (current); |
24a73b0a | 7467 | return workbuf; |
df7492f9 | 7468 | } |
d46c5b12 | 7469 | |
24a73b0a | 7470 | |
4776e638 | 7471 | static Lisp_Object |
971de7fb | 7472 | code_conversion_restore (Lisp_Object arg) |
4776e638 | 7473 | { |
24a73b0a | 7474 | Lisp_Object current, workbuf; |
948bdcf3 | 7475 | struct gcpro gcpro1; |
24a73b0a | 7476 | |
948bdcf3 | 7477 | GCPRO1 (arg); |
24a73b0a KH |
7478 | current = XCAR (arg); |
7479 | workbuf = XCDR (arg); | |
7480 | if (! NILP (workbuf)) | |
7481 | { | |
7482 | if (EQ (workbuf, Vcode_conversion_reused_workbuf)) | |
7483 | reused_workbuf_in_use = 0; | |
7484 | else if (! NILP (Fbuffer_live_p (workbuf))) | |
7485 | Fkill_buffer (workbuf); | |
7486 | } | |
7487 | set_buffer_internal (XBUFFER (current)); | |
948bdcf3 | 7488 | UNGCPRO; |
4776e638 KH |
7489 | return Qnil; |
7490 | } | |
b73bfc1c | 7491 | |
24a73b0a | 7492 | Lisp_Object |
971de7fb | 7493 | code_conversion_save (int with_work_buf, int multibyte) |
df7492f9 | 7494 | { |
24a73b0a | 7495 | Lisp_Object workbuf = Qnil; |
b73bfc1c | 7496 | |
4776e638 | 7497 | if (with_work_buf) |
24a73b0a KH |
7498 | workbuf = make_conversion_work_buffer (multibyte); |
7499 | record_unwind_protect (code_conversion_restore, | |
7500 | Fcons (Fcurrent_buffer (), workbuf)); | |
4776e638 | 7501 | return workbuf; |
df7492f9 | 7502 | } |
d46c5b12 | 7503 | |
df7492f9 | 7504 | int |
cf84bb53 JB |
7505 | decode_coding_gap (struct coding_system *coding, |
7506 | EMACS_INT chars, EMACS_INT bytes) | |
df7492f9 | 7507 | { |
1a4990fb | 7508 | int count = SPECPDL_INDEX (); |
5e5c78be | 7509 | Lisp_Object attrs; |
fb88bf2d | 7510 | |
24a73b0a | 7511 | code_conversion_save (0, 0); |
ec6d2bb8 | 7512 | |
24a73b0a | 7513 | coding->src_object = Fcurrent_buffer (); |
df7492f9 KH |
7514 | coding->src_chars = chars; |
7515 | coding->src_bytes = bytes; | |
7516 | coding->src_pos = -chars; | |
7517 | coding->src_pos_byte = -bytes; | |
7518 | coding->src_multibyte = chars < bytes; | |
24a73b0a | 7519 | coding->dst_object = coding->src_object; |
df7492f9 KH |
7520 | coding->dst_pos = PT; |
7521 | coding->dst_pos_byte = PT_BYTE; | |
4b4deea2 | 7522 | coding->dst_multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters)); |
4ed46869 | 7523 | |
df7492f9 KH |
7524 | if (CODING_REQUIRE_DETECTION (coding)) |
7525 | detect_coding (coding); | |
8f924df7 | 7526 | |
9286b333 | 7527 | coding->mode |= CODING_MODE_LAST_BLOCK; |
287c57d7 | 7528 | current_buffer->text->inhibit_shrinking = 1; |
df7492f9 | 7529 | decode_coding (coding); |
287c57d7 | 7530 | current_buffer->text->inhibit_shrinking = 0; |
d46c5b12 | 7531 | |
5e5c78be KH |
7532 | attrs = CODING_ID_ATTRS (coding->id); |
7533 | if (! NILP (CODING_ATTR_POST_READ (attrs))) | |
b73bfc1c | 7534 | { |
5e5c78be KH |
7535 | EMACS_INT prev_Z = Z, prev_Z_BYTE = Z_BYTE; |
7536 | Lisp_Object val; | |
7537 | ||
7538 | TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte); | |
5e5c78be KH |
7539 | val = call1 (CODING_ATTR_POST_READ (attrs), |
7540 | make_number (coding->produced_char)); | |
5e5c78be KH |
7541 | CHECK_NATNUM (val); |
7542 | coding->produced_char += Z - prev_Z; | |
7543 | coding->produced += Z_BYTE - prev_Z_BYTE; | |
b73bfc1c | 7544 | } |
4ed46869 | 7545 | |
df7492f9 | 7546 | unbind_to (count, Qnil); |
b73bfc1c KH |
7547 | return coding->result; |
7548 | } | |
52d41803 | 7549 | |
4ed46869 | 7550 | int |
cf84bb53 JB |
7551 | encode_coding_gap (struct coding_system *coding, |
7552 | EMACS_INT chars, EMACS_INT bytes) | |
4ed46869 | 7553 | { |
1a4990fb | 7554 | int count = SPECPDL_INDEX (); |
4ed46869 | 7555 | |
24a73b0a | 7556 | code_conversion_save (0, 0); |
4ed46869 | 7557 | |
24a73b0a | 7558 | coding->src_object = Fcurrent_buffer (); |
df7492f9 KH |
7559 | coding->src_chars = chars; |
7560 | coding->src_bytes = bytes; | |
7561 | coding->src_pos = -chars; | |
7562 | coding->src_pos_byte = -bytes; | |
7563 | coding->src_multibyte = chars < bytes; | |
7564 | coding->dst_object = coding->src_object; | |
7565 | coding->dst_pos = PT; | |
7566 | coding->dst_pos_byte = PT_BYTE; | |
4ed46869 | 7567 | |
df7492f9 | 7568 | encode_coding (coding); |
b73bfc1c | 7569 | |
df7492f9 KH |
7570 | unbind_to (count, Qnil); |
7571 | return coding->result; | |
7572 | } | |
4ed46869 | 7573 | |
d46c5b12 | 7574 | |
df7492f9 KH |
7575 | /* Decode the text in the range FROM/FROM_BYTE and TO/TO_BYTE in |
7576 | SRC_OBJECT into DST_OBJECT by coding context CODING. | |
b73bfc1c | 7577 | |
df7492f9 | 7578 | SRC_OBJECT is a buffer, a string, or Qnil. |
b73bfc1c | 7579 | |
df7492f9 KH |
7580 | If it is a buffer, the text is at point of the buffer. FROM and TO |
7581 | are positions in the buffer. | |
b73bfc1c | 7582 | |
df7492f9 KH |
7583 | If it is a string, the text is at the beginning of the string. |
7584 | FROM and TO are indices to the string. | |
4ed46869 | 7585 | |
df7492f9 KH |
7586 | If it is nil, the text is at coding->source. FROM and TO are |
7587 | indices to coding->source. | |
bb10be8b | 7588 | |
df7492f9 | 7589 | DST_OBJECT is a buffer, Qt, or Qnil. |
4ed46869 | 7590 | |
df7492f9 KH |
7591 | If it is a buffer, the decoded text is inserted at point of the |
7592 | buffer. If the buffer is the same as SRC_OBJECT, the source text | |
7593 | is deleted. | |
4ed46869 | 7594 | |
df7492f9 KH |
7595 | If it is Qt, a string is made from the decoded text, and |
7596 | set in CODING->dst_object. | |
d46c5b12 | 7597 | |
df7492f9 | 7598 | If it is Qnil, the decoded text is stored at CODING->destination. |
2cb26057 | 7599 | The caller must allocate CODING->dst_bytes bytes at |
df7492f9 KH |
7600 | CODING->destination by xmalloc. If the decoded text is longer than |
7601 | CODING->dst_bytes, CODING->destination is relocated by xrealloc. | |
7602 | */ | |
d46c5b12 | 7603 | |
df7492f9 | 7604 | void |
cf84bb53 JB |
7605 | decode_coding_object (struct coding_system *coding, |
7606 | Lisp_Object src_object, | |
7607 | EMACS_INT from, EMACS_INT from_byte, | |
7608 | EMACS_INT to, EMACS_INT to_byte, | |
7609 | Lisp_Object dst_object) | |
d46c5b12 | 7610 | { |
1a4990fb | 7611 | int count = SPECPDL_INDEX (); |
c4a63b12 PE |
7612 | unsigned char *destination IF_LINT (= NULL); |
7613 | EMACS_INT dst_bytes IF_LINT (= 0); | |
df7492f9 KH |
7614 | EMACS_INT chars = to - from; |
7615 | EMACS_INT bytes = to_byte - from_byte; | |
7616 | Lisp_Object attrs; | |
c4a63b12 | 7617 | int saved_pt = -1, saved_pt_byte IF_LINT (= 0); |
64cedb0c | 7618 | int need_marker_adjustment = 0; |
b3bfad50 | 7619 | Lisp_Object old_deactivate_mark; |
d46c5b12 | 7620 | |
b3bfad50 | 7621 | old_deactivate_mark = Vdeactivate_mark; |
93dec019 | 7622 | |
df7492f9 | 7623 | if (NILP (dst_object)) |
d46c5b12 | 7624 | { |
df7492f9 KH |
7625 | destination = coding->destination; |
7626 | dst_bytes = coding->dst_bytes; | |
d46c5b12 | 7627 | } |
93dec019 | 7628 | |
df7492f9 KH |
7629 | coding->src_object = src_object; |
7630 | coding->src_chars = chars; | |
7631 | coding->src_bytes = bytes; | |
7632 | coding->src_multibyte = chars < bytes; | |
70ad9fc4 | 7633 | |
df7492f9 | 7634 | if (STRINGP (src_object)) |
d46c5b12 | 7635 | { |
df7492f9 KH |
7636 | coding->src_pos = from; |
7637 | coding->src_pos_byte = from_byte; | |
d46c5b12 | 7638 | } |
df7492f9 | 7639 | else if (BUFFERP (src_object)) |
88993dfd | 7640 | { |
df7492f9 KH |
7641 | set_buffer_internal (XBUFFER (src_object)); |
7642 | if (from != GPT) | |
7643 | move_gap_both (from, from_byte); | |
7644 | if (EQ (src_object, dst_object)) | |
fb88bf2d | 7645 | { |
64cedb0c KH |
7646 | struct Lisp_Marker *tail; |
7647 | ||
7648 | for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next) | |
7649 | { | |
7650 | tail->need_adjustment | |
7651 | = tail->charpos == (tail->insertion_type ? from : to); | |
7652 | need_marker_adjustment |= tail->need_adjustment; | |
7653 | } | |
4776e638 | 7654 | saved_pt = PT, saved_pt_byte = PT_BYTE; |
df7492f9 | 7655 | TEMP_SET_PT_BOTH (from, from_byte); |
f4a3cc44 | 7656 | current_buffer->text->inhibit_shrinking = 1; |
df7492f9 KH |
7657 | del_range_both (from, from_byte, to, to_byte, 1); |
7658 | coding->src_pos = -chars; | |
7659 | coding->src_pos_byte = -bytes; | |
fb88bf2d | 7660 | } |
df7492f9 | 7661 | else |
fb88bf2d | 7662 | { |
df7492f9 KH |
7663 | coding->src_pos = from; |
7664 | coding->src_pos_byte = from_byte; | |
fb88bf2d | 7665 | } |
88993dfd KH |
7666 | } |
7667 | ||
df7492f9 KH |
7668 | if (CODING_REQUIRE_DETECTION (coding)) |
7669 | detect_coding (coding); | |
7670 | attrs = CODING_ID_ATTRS (coding->id); | |
d46c5b12 | 7671 | |
2cb26057 KH |
7672 | if (EQ (dst_object, Qt) |
7673 | || (! NILP (CODING_ATTR_POST_READ (attrs)) | |
7674 | && NILP (dst_object))) | |
b73bfc1c | 7675 | { |
a1567c45 SM |
7676 | coding->dst_multibyte = !CODING_FOR_UNIBYTE (coding); |
7677 | coding->dst_object = code_conversion_save (1, coding->dst_multibyte); | |
df7492f9 KH |
7678 | coding->dst_pos = BEG; |
7679 | coding->dst_pos_byte = BEG_BYTE; | |
b73bfc1c | 7680 | } |
df7492f9 | 7681 | else if (BUFFERP (dst_object)) |
d46c5b12 | 7682 | { |
24a73b0a | 7683 | code_conversion_save (0, 0); |
df7492f9 KH |
7684 | coding->dst_object = dst_object; |
7685 | coding->dst_pos = BUF_PT (XBUFFER (dst_object)); | |
7686 | coding->dst_pos_byte = BUF_PT_BYTE (XBUFFER (dst_object)); | |
7687 | coding->dst_multibyte | |
4b4deea2 | 7688 | = ! NILP (BVAR (XBUFFER (dst_object), enable_multibyte_characters)); |
d46c5b12 KH |
7689 | } |
7690 | else | |
7691 | { | |
24a73b0a | 7692 | code_conversion_save (0, 0); |
df7492f9 | 7693 | coding->dst_object = Qnil; |
0154725e SM |
7694 | /* Most callers presume this will return a multibyte result, and they |
7695 | won't use `binary' or `raw-text' anyway, so let's not worry about | |
7696 | CODING_FOR_UNIBYTE. */ | |
bb555731 | 7697 | coding->dst_multibyte = 1; |
d46c5b12 KH |
7698 | } |
7699 | ||
df7492f9 | 7700 | decode_coding (coding); |
fa46990e | 7701 | |
df7492f9 KH |
7702 | if (BUFFERP (coding->dst_object)) |
7703 | set_buffer_internal (XBUFFER (coding->dst_object)); | |
d46c5b12 | 7704 | |
df7492f9 | 7705 | if (! NILP (CODING_ATTR_POST_READ (attrs))) |
d46c5b12 | 7706 | { |
b3bfad50 | 7707 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; |
df7492f9 | 7708 | EMACS_INT prev_Z = Z, prev_Z_BYTE = Z_BYTE; |
2b4f9037 | 7709 | Lisp_Object val; |
d46c5b12 | 7710 | |
c0cc7f7f | 7711 | TEMP_SET_PT_BOTH (coding->dst_pos, coding->dst_pos_byte); |
b3bfad50 KH |
7712 | GCPRO5 (coding->src_object, coding->dst_object, src_object, dst_object, |
7713 | old_deactivate_mark); | |
d4850d67 KH |
7714 | val = safe_call1 (CODING_ATTR_POST_READ (attrs), |
7715 | make_number (coding->produced_char)); | |
df7492f9 KH |
7716 | UNGCPRO; |
7717 | CHECK_NATNUM (val); | |
7718 | coding->produced_char += Z - prev_Z; | |
7719 | coding->produced += Z_BYTE - prev_Z_BYTE; | |
d46c5b12 | 7720 | } |
de79a6a5 | 7721 | |
df7492f9 | 7722 | if (EQ (dst_object, Qt)) |
ec6d2bb8 | 7723 | { |
df7492f9 KH |
7724 | coding->dst_object = Fbuffer_string (); |
7725 | } | |
7726 | else if (NILP (dst_object) && BUFFERP (coding->dst_object)) | |
7727 | { | |
7728 | set_buffer_internal (XBUFFER (coding->dst_object)); | |
7729 | if (dst_bytes < coding->produced) | |
7730 | { | |
b3bfad50 | 7731 | destination = xrealloc (destination, coding->produced); |
df7492f9 KH |
7732 | if (! destination) |
7733 | { | |
065e3595 | 7734 | record_conversion_result (coding, |
ebaf11b6 | 7735 | CODING_RESULT_INSUFFICIENT_MEM); |
df7492f9 KH |
7736 | unbind_to (count, Qnil); |
7737 | return; | |
7738 | } | |
7739 | if (BEGV < GPT && GPT < BEGV + coding->produced_char) | |
7740 | move_gap_both (BEGV, BEGV_BYTE); | |
72af86bd | 7741 | memcpy (destination, BEGV_ADDR, coding->produced); |
df7492f9 | 7742 | coding->destination = destination; |
d46c5b12 | 7743 | } |
ec6d2bb8 | 7744 | } |
b73bfc1c | 7745 | |
4776e638 KH |
7746 | if (saved_pt >= 0) |
7747 | { | |
7748 | /* This is the case of: | |
7749 | (BUFFERP (src_object) && EQ (src_object, dst_object)) | |
7750 | As we have moved PT while replacing the original buffer | |
7751 | contents, we must recover it now. */ | |
7752 | set_buffer_internal (XBUFFER (src_object)); | |
f4a3cc44 | 7753 | current_buffer->text->inhibit_shrinking = 0; |
4776e638 KH |
7754 | if (saved_pt < from) |
7755 | TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte); | |
7756 | else if (saved_pt < from + chars) | |
7757 | TEMP_SET_PT_BOTH (from, from_byte); | |
4b4deea2 | 7758 | else if (! NILP (BVAR (current_buffer, enable_multibyte_characters))) |
4776e638 KH |
7759 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars), |
7760 | saved_pt_byte + (coding->produced - bytes)); | |
7761 | else | |
7762 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes), | |
7763 | saved_pt_byte + (coding->produced - bytes)); | |
64cedb0c KH |
7764 | |
7765 | if (need_marker_adjustment) | |
7766 | { | |
7767 | struct Lisp_Marker *tail; | |
7768 | ||
7769 | for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next) | |
7770 | if (tail->need_adjustment) | |
7771 | { | |
7772 | tail->need_adjustment = 0; | |
7773 | if (tail->insertion_type) | |
7774 | { | |
7775 | tail->bytepos = from_byte; | |
7776 | tail->charpos = from; | |
7777 | } | |
7778 | else | |
7779 | { | |
7780 | tail->bytepos = from_byte + coding->produced; | |
7781 | tail->charpos | |
4b4deea2 | 7782 | = (NILP (BVAR (current_buffer, enable_multibyte_characters)) |
64cedb0c KH |
7783 | ? tail->bytepos : from + coding->produced_char); |
7784 | } | |
7785 | } | |
7786 | } | |
d46c5b12 | 7787 | } |
4776e638 | 7788 | |
b3bfad50 | 7789 | Vdeactivate_mark = old_deactivate_mark; |
065e3595 | 7790 | unbind_to (count, coding->dst_object); |
d46c5b12 KH |
7791 | } |
7792 | ||
d46c5b12 | 7793 | |
df7492f9 | 7794 | void |
cf84bb53 JB |
7795 | encode_coding_object (struct coding_system *coding, |
7796 | Lisp_Object src_object, | |
7797 | EMACS_INT from, EMACS_INT from_byte, | |
7798 | EMACS_INT to, EMACS_INT to_byte, | |
7799 | Lisp_Object dst_object) | |
d46c5b12 | 7800 | { |
1a4990fb | 7801 | int count = SPECPDL_INDEX (); |
df7492f9 KH |
7802 | EMACS_INT chars = to - from; |
7803 | EMACS_INT bytes = to_byte - from_byte; | |
7804 | Lisp_Object attrs; | |
c4a63b12 | 7805 | int saved_pt = -1, saved_pt_byte IF_LINT (= 0); |
64cedb0c | 7806 | int need_marker_adjustment = 0; |
c02d943b | 7807 | int kill_src_buffer = 0; |
b3bfad50 | 7808 | Lisp_Object old_deactivate_mark; |
df7492f9 | 7809 | |
b3bfad50 | 7810 | old_deactivate_mark = Vdeactivate_mark; |
df7492f9 KH |
7811 | |
7812 | coding->src_object = src_object; | |
7813 | coding->src_chars = chars; | |
7814 | coding->src_bytes = bytes; | |
7815 | coding->src_multibyte = chars < bytes; | |
7816 | ||
7817 | attrs = CODING_ID_ATTRS (coding->id); | |
7818 | ||
64cedb0c KH |
7819 | if (EQ (src_object, dst_object)) |
7820 | { | |
7821 | struct Lisp_Marker *tail; | |
7822 | ||
7823 | for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next) | |
7824 | { | |
7825 | tail->need_adjustment | |
7826 | = tail->charpos == (tail->insertion_type ? from : to); | |
7827 | need_marker_adjustment |= tail->need_adjustment; | |
7828 | } | |
7829 | } | |
7830 | ||
df7492f9 | 7831 | if (! NILP (CODING_ATTR_PRE_WRITE (attrs))) |
6bac5b12 | 7832 | { |
24a73b0a | 7833 | coding->src_object = code_conversion_save (1, coding->src_multibyte); |
df7492f9 KH |
7834 | set_buffer_internal (XBUFFER (coding->src_object)); |
7835 | if (STRINGP (src_object)) | |
7836 | insert_from_string (src_object, from, from_byte, chars, bytes, 0); | |
7837 | else if (BUFFERP (src_object)) | |
7838 | insert_from_buffer (XBUFFER (src_object), from, chars, 0); | |
7839 | else | |
b68864e5 | 7840 | insert_1_both ((char *) coding->source + from, chars, bytes, 0, 0, 0); |
d46c5b12 | 7841 | |
df7492f9 KH |
7842 | if (EQ (src_object, dst_object)) |
7843 | { | |
7844 | set_buffer_internal (XBUFFER (src_object)); | |
4776e638 | 7845 | saved_pt = PT, saved_pt_byte = PT_BYTE; |
df7492f9 KH |
7846 | del_range_both (from, from_byte, to, to_byte, 1); |
7847 | set_buffer_internal (XBUFFER (coding->src_object)); | |
7848 | } | |
7849 | ||
d4850d67 KH |
7850 | { |
7851 | Lisp_Object args[3]; | |
b3bfad50 | 7852 | struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; |
d4850d67 | 7853 | |
b3bfad50 KH |
7854 | GCPRO5 (coding->src_object, coding->dst_object, src_object, dst_object, |
7855 | old_deactivate_mark); | |
d4850d67 KH |
7856 | args[0] = CODING_ATTR_PRE_WRITE (attrs); |
7857 | args[1] = make_number (BEG); | |
7858 | args[2] = make_number (Z); | |
7859 | safe_call (3, args); | |
b3bfad50 | 7860 | UNGCPRO; |
d4850d67 | 7861 | } |
c02d943b KH |
7862 | if (XBUFFER (coding->src_object) != current_buffer) |
7863 | kill_src_buffer = 1; | |
ac87bbef | 7864 | coding->src_object = Fcurrent_buffer (); |
df7492f9 KH |
7865 | if (BEG != GPT) |
7866 | move_gap_both (BEG, BEG_BYTE); | |
7867 | coding->src_chars = Z - BEG; | |
7868 | coding->src_bytes = Z_BYTE - BEG_BYTE; | |
7869 | coding->src_pos = BEG; | |
7870 | coding->src_pos_byte = BEG_BYTE; | |
7871 | coding->src_multibyte = Z < Z_BYTE; | |
7872 | } | |
7873 | else if (STRINGP (src_object)) | |
d46c5b12 | 7874 | { |
24a73b0a | 7875 | code_conversion_save (0, 0); |
df7492f9 KH |
7876 | coding->src_pos = from; |
7877 | coding->src_pos_byte = from_byte; | |
b73bfc1c | 7878 | } |
df7492f9 | 7879 | else if (BUFFERP (src_object)) |
b73bfc1c | 7880 | { |
24a73b0a | 7881 | code_conversion_save (0, 0); |
df7492f9 | 7882 | set_buffer_internal (XBUFFER (src_object)); |
df7492f9 | 7883 | if (EQ (src_object, dst_object)) |
d46c5b12 | 7884 | { |
4776e638 | 7885 | saved_pt = PT, saved_pt_byte = PT_BYTE; |
ff0dacd7 KH |
7886 | coding->src_object = del_range_1 (from, to, 1, 1); |
7887 | coding->src_pos = 0; | |
7888 | coding->src_pos_byte = 0; | |
d46c5b12 | 7889 | } |
df7492f9 | 7890 | else |
d46c5b12 | 7891 | { |
ff0dacd7 KH |
7892 | if (from < GPT && to >= GPT) |
7893 | move_gap_both (from, from_byte); | |
df7492f9 KH |
7894 | coding->src_pos = from; |
7895 | coding->src_pos_byte = from_byte; | |
d46c5b12 | 7896 | } |
d46c5b12 | 7897 | } |
4776e638 | 7898 | else |
24a73b0a | 7899 | code_conversion_save (0, 0); |
d46c5b12 | 7900 | |
df7492f9 | 7901 | if (BUFFERP (dst_object)) |
88993dfd | 7902 | { |
df7492f9 | 7903 | coding->dst_object = dst_object; |
28f67a95 KH |
7904 | if (EQ (src_object, dst_object)) |
7905 | { | |
7906 | coding->dst_pos = from; | |
7907 | coding->dst_pos_byte = from_byte; | |
7908 | } | |
7909 | else | |
7910 | { | |
319a3947 KH |
7911 | struct buffer *current = current_buffer; |
7912 | ||
7913 | set_buffer_temp (XBUFFER (dst_object)); | |
7914 | coding->dst_pos = PT; | |
7915 | coding->dst_pos_byte = PT_BYTE; | |
7916 | move_gap_both (coding->dst_pos, coding->dst_pos_byte); | |
7917 | set_buffer_temp (current); | |
28f67a95 | 7918 | } |
df7492f9 | 7919 | coding->dst_multibyte |
4b4deea2 | 7920 | = ! NILP (BVAR (XBUFFER (dst_object), enable_multibyte_characters)); |
88993dfd | 7921 | } |
df7492f9 | 7922 | else if (EQ (dst_object, Qt)) |
d46c5b12 | 7923 | { |
df7492f9 | 7924 | coding->dst_object = Qnil; |
df7492f9 | 7925 | coding->dst_bytes = coding->src_chars; |
ac87bbef KH |
7926 | if (coding->dst_bytes == 0) |
7927 | coding->dst_bytes = 1; | |
7928 | coding->destination = (unsigned char *) xmalloc (coding->dst_bytes); | |
df7492f9 | 7929 | coding->dst_multibyte = 0; |
d46c5b12 KH |
7930 | } |
7931 | else | |
7932 | { | |
df7492f9 KH |
7933 | coding->dst_object = Qnil; |
7934 | coding->dst_multibyte = 0; | |
d46c5b12 KH |
7935 | } |
7936 | ||
df7492f9 | 7937 | encode_coding (coding); |
d46c5b12 | 7938 | |
df7492f9 | 7939 | if (EQ (dst_object, Qt)) |
d46c5b12 | 7940 | { |
df7492f9 KH |
7941 | if (BUFFERP (coding->dst_object)) |
7942 | coding->dst_object = Fbuffer_string (); | |
7943 | else | |
d46c5b12 | 7944 | { |
df7492f9 KH |
7945 | coding->dst_object |
7946 | = make_unibyte_string ((char *) coding->destination, | |
7947 | coding->produced); | |
7948 | xfree (coding->destination); | |
d46c5b12 | 7949 | } |
4ed46869 | 7950 | } |
d46c5b12 | 7951 | |
4776e638 KH |
7952 | if (saved_pt >= 0) |
7953 | { | |
7954 | /* This is the case of: | |
7955 | (BUFFERP (src_object) && EQ (src_object, dst_object)) | |
7956 | As we have moved PT while replacing the original buffer | |
7957 | contents, we must recover it now. */ | |
7958 | set_buffer_internal (XBUFFER (src_object)); | |
7959 | if (saved_pt < from) | |
7960 | TEMP_SET_PT_BOTH (saved_pt, saved_pt_byte); | |
7961 | else if (saved_pt < from + chars) | |
7962 | TEMP_SET_PT_BOTH (from, from_byte); | |
4b4deea2 | 7963 | else if (! NILP (BVAR (current_buffer, enable_multibyte_characters))) |
4776e638 KH |
7964 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced_char - chars), |
7965 | saved_pt_byte + (coding->produced - bytes)); | |
d46c5b12 | 7966 | else |
4776e638 KH |
7967 | TEMP_SET_PT_BOTH (saved_pt + (coding->produced - bytes), |
7968 | saved_pt_byte + (coding->produced - bytes)); | |
64cedb0c KH |
7969 | |
7970 | if (need_marker_adjustment) | |
7971 | { | |
7972 | struct Lisp_Marker *tail; | |
7973 | ||
7974 | for (tail = BUF_MARKERS (current_buffer); tail; tail = tail->next) | |
7975 | if (tail->need_adjustment) | |
7976 | { | |
7977 | tail->need_adjustment = 0; | |
7978 | if (tail->insertion_type) | |
7979 | { | |
7980 | tail->bytepos = from_byte; | |
7981 | tail->charpos = from; | |
7982 | } | |
7983 | else | |
7984 | { | |
7985 | tail->bytepos = from_byte + coding->produced; | |
7986 | tail->charpos | |
4b4deea2 | 7987 | = (NILP (BVAR (current_buffer, enable_multibyte_characters)) |
64cedb0c KH |
7988 | ? tail->bytepos : from + coding->produced_char); |
7989 | } | |
7990 | } | |
7991 | } | |
4776e638 KH |
7992 | } |
7993 | ||
c02d943b KH |
7994 | if (kill_src_buffer) |
7995 | Fkill_buffer (coding->src_object); | |
b3bfad50 KH |
7996 | |
7997 | Vdeactivate_mark = old_deactivate_mark; | |
df7492f9 | 7998 | unbind_to (count, Qnil); |
b73bfc1c KH |
7999 | } |
8000 | ||
df7492f9 | 8001 | |
b73bfc1c | 8002 | Lisp_Object |
971de7fb | 8003 | preferred_coding_system (void) |
b73bfc1c | 8004 | { |
df7492f9 | 8005 | int id = coding_categories[coding_priorities[0]].id; |
2391eaa4 | 8006 | |
df7492f9 | 8007 | return CODING_ID_NAME (id); |
4ed46869 KH |
8008 | } |
8009 | ||
8010 | \f | |
8011 | #ifdef emacs | |
1397dc18 | 8012 | /*** 8. Emacs Lisp library functions ***/ |
4ed46869 | 8013 | |
4ed46869 | 8014 | DEFUN ("coding-system-p", Fcoding_system_p, Scoding_system_p, 1, 1, 0, |
48b0f3ae | 8015 | doc: /* Return t if OBJECT is nil or a coding-system. |
df7492f9 | 8016 | See the documentation of `define-coding-system' for information |
48b0f3ae | 8017 | about coding-system objects. */) |
5842a27b | 8018 | (Lisp_Object object) |
4ed46869 | 8019 | { |
d4a1d553 JB |
8020 | if (NILP (object) |
8021 | || CODING_SYSTEM_ID (object) >= 0) | |
44e8490d | 8022 | return Qt; |
d4a1d553 JB |
8023 | if (! SYMBOLP (object) |
8024 | || NILP (Fget (object, Qcoding_system_define_form))) | |
44e8490d KH |
8025 | return Qnil; |
8026 | return Qt; | |
4ed46869 KH |
8027 | } |
8028 | ||
9d991de8 RS |
8029 | DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system, |
8030 | Sread_non_nil_coding_system, 1, 1, 0, | |
48b0f3ae | 8031 | doc: /* Read a coding system from the minibuffer, prompting with string PROMPT. */) |
5842a27b | 8032 | (Lisp_Object prompt) |
4ed46869 | 8033 | { |
e0e989f6 | 8034 | Lisp_Object val; |
9d991de8 RS |
8035 | do |
8036 | { | |
4608c386 KH |
8037 | val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil, |
8038 | Qt, Qnil, Qcoding_system_history, Qnil, Qnil); | |
9d991de8 | 8039 | } |
8f924df7 | 8040 | while (SCHARS (val) == 0); |
e0e989f6 | 8041 | return (Fintern (val, Qnil)); |
4ed46869 KH |
8042 | } |
8043 | ||
9b787f3e | 8044 | DEFUN ("read-coding-system", Fread_coding_system, Sread_coding_system, 1, 2, 0, |
48b0f3ae | 8045 | doc: /* Read a coding system from the minibuffer, prompting with string PROMPT. |
c7183fb8 GM |
8046 | If the user enters null input, return second argument DEFAULT-CODING-SYSTEM. |
8047 | Ignores case when completing coding systems (all Emacs coding systems | |
8048 | are lower-case). */) | |
5842a27b | 8049 | (Lisp_Object prompt, Lisp_Object default_coding_system) |
4ed46869 | 8050 | { |
f44d27ce | 8051 | Lisp_Object val; |
c7183fb8 GM |
8052 | int count = SPECPDL_INDEX (); |
8053 | ||
9b787f3e | 8054 | if (SYMBOLP (default_coding_system)) |
57d25e6f | 8055 | default_coding_system = SYMBOL_NAME (default_coding_system); |
c7183fb8 | 8056 | specbind (Qcompletion_ignore_case, Qt); |
4608c386 | 8057 | val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil, |
9b787f3e RS |
8058 | Qt, Qnil, Qcoding_system_history, |
8059 | default_coding_system, Qnil); | |
c7183fb8 | 8060 | unbind_to (count, Qnil); |
8f924df7 | 8061 | return (SCHARS (val) == 0 ? Qnil : Fintern (val, Qnil)); |
4ed46869 KH |
8062 | } |
8063 | ||
8064 | DEFUN ("check-coding-system", Fcheck_coding_system, Scheck_coding_system, | |
8065 | 1, 1, 0, | |
48b0f3ae | 8066 | doc: /* Check validity of CODING-SYSTEM. |
9ffd559c KH |
8067 | If valid, return CODING-SYSTEM, else signal a `coding-system-error' error. |
8068 | It is valid if it is nil or a symbol defined as a coding system by the | |
8069 | function `define-coding-system'. */) | |
5842a27b | 8070 | (Lisp_Object coding_system) |
4ed46869 | 8071 | { |
44e8490d KH |
8072 | Lisp_Object define_form; |
8073 | ||
8074 | define_form = Fget (coding_system, Qcoding_system_define_form); | |
8075 | if (! NILP (define_form)) | |
8076 | { | |
8077 | Fput (coding_system, Qcoding_system_define_form, Qnil); | |
8078 | safe_eval (define_form); | |
8079 | } | |
4ed46869 KH |
8080 | if (!NILP (Fcoding_system_p (coding_system))) |
8081 | return coding_system; | |
fcad4ec4 | 8082 | xsignal1 (Qcoding_system_error, coding_system); |
4ed46869 | 8083 | } |
df7492f9 | 8084 | |
3a73fa5d | 8085 | \f |
89528eb3 KH |
8086 | /* Detect how the bytes at SRC of length SRC_BYTES are encoded. If |
8087 | HIGHEST is nonzero, return the coding system of the highest | |
ad1746f5 | 8088 | priority among the detected coding systems. Otherwise return a |
89528eb3 KH |
8089 | list of detected coding systems sorted by their priorities. If |
8090 | MULTIBYTEP is nonzero, it is assumed that the bytes are in correct | |
8091 | multibyte form but contains only ASCII and eight-bit chars. | |
8092 | Otherwise, the bytes are raw bytes. | |
8093 | ||
8094 | CODING-SYSTEM controls the detection as below: | |
8095 | ||
8096 | If it is nil, detect both text-format and eol-format. If the | |
8097 | text-format part of CODING-SYSTEM is already specified | |
8098 | (e.g. `iso-latin-1'), detect only eol-format. If the eol-format | |
8099 | part of CODING-SYSTEM is already specified (e.g. `undecided-unix'), | |
8100 | detect only text-format. */ | |
8101 | ||
d46c5b12 | 8102 | Lisp_Object |
cf84bb53 JB |
8103 | detect_coding_system (const unsigned char *src, |
8104 | EMACS_INT src_chars, EMACS_INT src_bytes, | |
8105 | int highest, int multibytep, | |
8106 | Lisp_Object coding_system) | |
4ed46869 | 8107 | { |
8f924df7 | 8108 | const unsigned char *src_end = src + src_bytes; |
df7492f9 | 8109 | Lisp_Object attrs, eol_type; |
4533845d | 8110 | Lisp_Object val = Qnil; |
df7492f9 | 8111 | struct coding_system coding; |
89528eb3 | 8112 | int id; |
ff0dacd7 | 8113 | struct coding_detection_info detect_info; |
24a73b0a | 8114 | enum coding_category base_category; |
2f3cbb32 | 8115 | int null_byte_found = 0, eight_bit_found = 0; |
b73bfc1c | 8116 | |
df7492f9 KH |
8117 | if (NILP (coding_system)) |
8118 | coding_system = Qundecided; | |
8119 | setup_coding_system (coding_system, &coding); | |
8120 | attrs = CODING_ID_ATTRS (coding.id); | |
8121 | eol_type = CODING_ID_EOL_TYPE (coding.id); | |
89528eb3 | 8122 | coding_system = CODING_ATTR_BASE_NAME (attrs); |
4ed46869 | 8123 | |
df7492f9 | 8124 | coding.source = src; |
24a73b0a | 8125 | coding.src_chars = src_chars; |
df7492f9 KH |
8126 | coding.src_bytes = src_bytes; |
8127 | coding.src_multibyte = multibytep; | |
8128 | coding.consumed = 0; | |
89528eb3 | 8129 | coding.mode |= CODING_MODE_LAST_BLOCK; |
c0e16b14 | 8130 | coding.head_ascii = 0; |
d46c5b12 | 8131 | |
ff0dacd7 | 8132 | detect_info.checked = detect_info.found = detect_info.rejected = 0; |
d46c5b12 | 8133 | |
89528eb3 | 8134 | /* At first, detect text-format if necessary. */ |
24a73b0a KH |
8135 | base_category = XINT (CODING_ATTR_CATEGORY (attrs)); |
8136 | if (base_category == coding_category_undecided) | |
4ed46869 | 8137 | { |
c4a63b12 PE |
8138 | enum coding_category category IF_LINT (= 0); |
8139 | struct coding_system *this IF_LINT (= NULL); | |
ff0dacd7 | 8140 | int c, i; |
88993dfd | 8141 | |
24a73b0a | 8142 | /* Skip all ASCII bytes except for a few ISO2022 controls. */ |
2f3cbb32 | 8143 | for (; src < src_end; src++) |
4ed46869 | 8144 | { |
df7492f9 | 8145 | c = *src; |
6cb21a4f | 8146 | if (c & 0x80) |
6cb21a4f | 8147 | { |
2f3cbb32 | 8148 | eight_bit_found = 1; |
2f3cbb32 KH |
8149 | if (null_byte_found) |
8150 | break; | |
8151 | } | |
c0e16b14 | 8152 | else if (c < 0x20) |
2f3cbb32 KH |
8153 | { |
8154 | if ((c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) | |
8155 | && ! inhibit_iso_escape_detection | |
8156 | && ! detect_info.checked) | |
6cb21a4f | 8157 | { |
2f3cbb32 KH |
8158 | if (detect_coding_iso_2022 (&coding, &detect_info)) |
8159 | { | |
8160 | /* We have scanned the whole data. */ | |
8161 | if (! (detect_info.rejected & CATEGORY_MASK_ISO_7_ELSE)) | |
c0e16b14 KH |
8162 | { |
8163 | /* We didn't find an 8-bit code. We may | |
8164 | have found a null-byte, but it's very | |
8165 | rare that a binary file confirm to | |
8166 | ISO-2022. */ | |
8167 | src = src_end; | |
8168 | coding.head_ascii = src - coding.source; | |
8169 | } | |
8170 | detect_info.rejected |= ~CATEGORY_MASK_ISO_ESCAPE; | |
2f3cbb32 KH |
8171 | break; |
8172 | } | |
8173 | } | |
97b1b294 | 8174 | else if (! c && !inhibit_null_byte_detection) |
2f3cbb32 KH |
8175 | { |
8176 | null_byte_found = 1; | |
8177 | if (eight_bit_found) | |
8178 | break; | |
6cb21a4f | 8179 | } |
c006c0c8 KH |
8180 | if (! eight_bit_found) |
8181 | coding.head_ascii++; | |
6cb21a4f | 8182 | } |
c006c0c8 | 8183 | else if (! eight_bit_found) |
c0e16b14 | 8184 | coding.head_ascii++; |
4ed46869 | 8185 | } |
88993dfd | 8186 | |
2f3cbb32 KH |
8187 | if (null_byte_found || eight_bit_found |
8188 | || coding.head_ascii < coding.src_bytes | |
6cb21a4f KH |
8189 | || detect_info.found) |
8190 | { | |
2f3cbb32 | 8191 | if (coding.head_ascii == coding.src_bytes) |
6cb21a4f KH |
8192 | /* As all bytes are 7-bit, we can ignore non-ISO-2022 codings. */ |
8193 | for (i = 0; i < coding_category_raw_text; i++) | |
ff0dacd7 | 8194 | { |
6cb21a4f | 8195 | category = coding_priorities[i]; |
c7266f4a | 8196 | this = coding_categories + category; |
6cb21a4f | 8197 | if (detect_info.found & (1 << category)) |
ff0dacd7 KH |
8198 | break; |
8199 | } | |
6cb21a4f | 8200 | else |
2f3cbb32 KH |
8201 | { |
8202 | if (null_byte_found) | |
8203 | { | |
8204 | detect_info.checked |= ~CATEGORY_MASK_UTF_16; | |
8205 | detect_info.rejected |= ~CATEGORY_MASK_UTF_16; | |
8206 | } | |
8207 | for (i = 0; i < coding_category_raw_text; i++) | |
8208 | { | |
8209 | category = coding_priorities[i]; | |
8210 | this = coding_categories + category; | |
6cb21a4f | 8211 | |
2f3cbb32 KH |
8212 | if (this->id < 0) |
8213 | { | |
8214 | /* No coding system of this category is defined. */ | |
8215 | detect_info.rejected |= (1 << category); | |
8216 | } | |
8217 | else if (category >= coding_category_raw_text) | |
8218 | continue; | |
8219 | else if (detect_info.checked & (1 << category)) | |
8220 | { | |
8221 | if (highest | |
8222 | && (detect_info.found & (1 << category))) | |
6cb21a4f | 8223 | break; |
2f3cbb32 KH |
8224 | } |
8225 | else if ((*(this->detector)) (&coding, &detect_info) | |
8226 | && highest | |
8227 | && (detect_info.found & (1 << category))) | |
8228 | { | |
8229 | if (category == coding_category_utf_16_auto) | |
8230 | { | |
8231 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
8232 | category = coding_category_utf_16_le; | |
8233 | else | |
8234 | category = coding_category_utf_16_be; | |
8235 | } | |
8236 | break; | |
8237 | } | |
8238 | } | |
8239 | } | |
6cb21a4f | 8240 | } |
ec6d2bb8 | 8241 | |
4cddb209 KH |
8242 | if ((detect_info.rejected & CATEGORY_MASK_ANY) == CATEGORY_MASK_ANY |
8243 | || null_byte_found) | |
ec6d2bb8 | 8244 | { |
ff0dacd7 | 8245 | detect_info.found = CATEGORY_MASK_RAW_TEXT; |
4cddb209 | 8246 | id = CODING_SYSTEM_ID (Qno_conversion); |
89528eb3 KH |
8247 | val = Fcons (make_number (id), Qnil); |
8248 | } | |
ff0dacd7 | 8249 | else if (! detect_info.rejected && ! detect_info.found) |
89528eb3 | 8250 | { |
ff0dacd7 | 8251 | detect_info.found = CATEGORY_MASK_ANY; |
89528eb3 KH |
8252 | id = coding_categories[coding_category_undecided].id; |
8253 | val = Fcons (make_number (id), Qnil); | |
8254 | } | |
8255 | else if (highest) | |
8256 | { | |
ff0dacd7 | 8257 | if (detect_info.found) |
ec6d2bb8 | 8258 | { |
ff0dacd7 KH |
8259 | detect_info.found = 1 << category; |
8260 | val = Fcons (make_number (this->id), Qnil); | |
8261 | } | |
8262 | else | |
8263 | for (i = 0; i < coding_category_raw_text; i++) | |
8264 | if (! (detect_info.rejected & (1 << coding_priorities[i]))) | |
8265 | { | |
8266 | detect_info.found = 1 << coding_priorities[i]; | |
8267 | id = coding_categories[coding_priorities[i]].id; | |
8268 | val = Fcons (make_number (id), Qnil); | |
8269 | break; | |
8270 | } | |
8271 | } | |
89528eb3 KH |
8272 | else |
8273 | { | |
ff0dacd7 KH |
8274 | int mask = detect_info.rejected | detect_info.found; |
8275 | int found = 0; | |
ec6d2bb8 | 8276 | |
89528eb3 | 8277 | for (i = coding_category_raw_text - 1; i >= 0; i--) |
ff0dacd7 KH |
8278 | { |
8279 | category = coding_priorities[i]; | |
8280 | if (! (mask & (1 << category))) | |
ec6d2bb8 | 8281 | { |
ff0dacd7 KH |
8282 | found |= 1 << category; |
8283 | id = coding_categories[category].id; | |
c7266f4a KH |
8284 | if (id >= 0) |
8285 | val = Fcons (make_number (id), val); | |
ff0dacd7 KH |
8286 | } |
8287 | } | |
8288 | for (i = coding_category_raw_text - 1; i >= 0; i--) | |
8289 | { | |
8290 | category = coding_priorities[i]; | |
8291 | if (detect_info.found & (1 << category)) | |
8292 | { | |
8293 | id = coding_categories[category].id; | |
8294 | val = Fcons (make_number (id), val); | |
ec6d2bb8 | 8295 | } |
ec6d2bb8 | 8296 | } |
ff0dacd7 | 8297 | detect_info.found |= found; |
ec6d2bb8 | 8298 | } |
ec6d2bb8 | 8299 | } |
a470d443 KH |
8300 | else if (base_category == coding_category_utf_8_auto) |
8301 | { | |
8302 | if (detect_coding_utf_8 (&coding, &detect_info)) | |
8303 | { | |
8304 | struct coding_system *this; | |
8305 | ||
8306 | if (detect_info.found & CATEGORY_MASK_UTF_8_SIG) | |
8307 | this = coding_categories + coding_category_utf_8_sig; | |
8308 | else | |
8309 | this = coding_categories + coding_category_utf_8_nosig; | |
8310 | val = Fcons (make_number (this->id), Qnil); | |
8311 | } | |
8312 | } | |
24a73b0a KH |
8313 | else if (base_category == coding_category_utf_16_auto) |
8314 | { | |
8315 | if (detect_coding_utf_16 (&coding, &detect_info)) | |
8316 | { | |
24a73b0a KH |
8317 | struct coding_system *this; |
8318 | ||
8319 | if (detect_info.found & CATEGORY_MASK_UTF_16_LE) | |
8320 | this = coding_categories + coding_category_utf_16_le; | |
8321 | else if (detect_info.found & CATEGORY_MASK_UTF_16_BE) | |
8322 | this = coding_categories + coding_category_utf_16_be; | |
8323 | else if (detect_info.rejected & CATEGORY_MASK_UTF_16_LE_NOSIG) | |
8324 | this = coding_categories + coding_category_utf_16_be_nosig; | |
8325 | else | |
8326 | this = coding_categories + coding_category_utf_16_le_nosig; | |
8327 | val = Fcons (make_number (this->id), Qnil); | |
8328 | } | |
8329 | } | |
df7492f9 KH |
8330 | else |
8331 | { | |
ff0dacd7 | 8332 | detect_info.found = 1 << XINT (CODING_ATTR_CATEGORY (attrs)); |
89528eb3 | 8333 | val = Fcons (make_number (coding.id), Qnil); |
4ed46869 | 8334 | } |
df7492f9 | 8335 | |
89528eb3 | 8336 | /* Then, detect eol-format if necessary. */ |
df7492f9 | 8337 | { |
4533845d | 8338 | int normal_eol = -1, utf_16_be_eol = -1, utf_16_le_eol = -1; |
df7492f9 KH |
8339 | Lisp_Object tail; |
8340 | ||
89528eb3 KH |
8341 | if (VECTORP (eol_type)) |
8342 | { | |
ff0dacd7 | 8343 | if (detect_info.found & ~CATEGORY_MASK_UTF_16) |
2f3cbb32 KH |
8344 | { |
8345 | if (null_byte_found) | |
8346 | normal_eol = EOL_SEEN_LF; | |
8347 | else | |
8348 | normal_eol = detect_eol (coding.source, src_bytes, | |
8349 | coding_category_raw_text); | |
8350 | } | |
ff0dacd7 KH |
8351 | if (detect_info.found & (CATEGORY_MASK_UTF_16_BE |
8352 | | CATEGORY_MASK_UTF_16_BE_NOSIG)) | |
89528eb3 KH |
8353 | utf_16_be_eol = detect_eol (coding.source, src_bytes, |
8354 | coding_category_utf_16_be); | |
ff0dacd7 KH |
8355 | if (detect_info.found & (CATEGORY_MASK_UTF_16_LE |
8356 | | CATEGORY_MASK_UTF_16_LE_NOSIG)) | |
89528eb3 KH |
8357 | utf_16_le_eol = detect_eol (coding.source, src_bytes, |
8358 | coding_category_utf_16_le); | |
8359 | } | |
8360 | else | |
8361 | { | |
8362 | if (EQ (eol_type, Qunix)) | |
8363 | normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_LF; | |
8364 | else if (EQ (eol_type, Qdos)) | |
8365 | normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CRLF; | |
8366 | else | |
8367 | normal_eol = utf_16_be_eol = utf_16_le_eol = EOL_SEEN_CR; | |
8368 | } | |
8369 | ||
df7492f9 KH |
8370 | for (tail = val; CONSP (tail); tail = XCDR (tail)) |
8371 | { | |
89528eb3 | 8372 | enum coding_category category; |
df7492f9 | 8373 | int this_eol; |
89528eb3 KH |
8374 | |
8375 | id = XINT (XCAR (tail)); | |
8376 | attrs = CODING_ID_ATTRS (id); | |
8377 | category = XINT (CODING_ATTR_CATEGORY (attrs)); | |
8378 | eol_type = CODING_ID_EOL_TYPE (id); | |
df7492f9 KH |
8379 | if (VECTORP (eol_type)) |
8380 | { | |
89528eb3 KH |
8381 | if (category == coding_category_utf_16_be |
8382 | || category == coding_category_utf_16_be_nosig) | |
8383 | this_eol = utf_16_be_eol; | |
8384 | else if (category == coding_category_utf_16_le | |
8385 | || category == coding_category_utf_16_le_nosig) | |
8386 | this_eol = utf_16_le_eol; | |
df7492f9 | 8387 | else |
89528eb3 KH |
8388 | this_eol = normal_eol; |
8389 | ||
df7492f9 KH |
8390 | if (this_eol == EOL_SEEN_LF) |
8391 | XSETCAR (tail, AREF (eol_type, 0)); | |
8392 | else if (this_eol == EOL_SEEN_CRLF) | |
8393 | XSETCAR (tail, AREF (eol_type, 1)); | |
8394 | else if (this_eol == EOL_SEEN_CR) | |
8395 | XSETCAR (tail, AREF (eol_type, 2)); | |
89528eb3 KH |
8396 | else |
8397 | XSETCAR (tail, CODING_ID_NAME (id)); | |
df7492f9 | 8398 | } |
89528eb3 KH |
8399 | else |
8400 | XSETCAR (tail, CODING_ID_NAME (id)); | |
df7492f9 KH |
8401 | } |
8402 | } | |
ec6d2bb8 | 8403 | |
4533845d | 8404 | return (highest ? (CONSP (val) ? XCAR (val) : Qnil) : val); |
ec6d2bb8 KH |
8405 | } |
8406 | ||
ec6d2bb8 | 8407 | |
d46c5b12 KH |
8408 | DEFUN ("detect-coding-region", Fdetect_coding_region, Sdetect_coding_region, |
8409 | 2, 3, 0, | |
48b0f3ae PJ |
8410 | doc: /* Detect coding system of the text in the region between START and END. |
8411 | Return a list of possible coding systems ordered by priority. | |
b811c52b KH |
8412 | The coding systems to try and their priorities follows what |
8413 | the function `coding-system-priority-list' (which see) returns. | |
ec6d2bb8 | 8414 | |
12e0131a | 8415 | If only ASCII characters are found (except for such ISO-2022 control |
d4a1d553 JB |
8416 | characters as ESC), it returns a list of single element `undecided' |
8417 | or its subsidiary coding system according to a detected end-of-line | |
8418 | format. | |
ec6d2bb8 | 8419 | |
48b0f3ae PJ |
8420 | If optional argument HIGHEST is non-nil, return the coding system of |
8421 | highest priority. */) | |
5842a27b | 8422 | (Lisp_Object start, Lisp_Object end, Lisp_Object highest) |
d46c5b12 KH |
8423 | { |
8424 | int from, to; | |
8425 | int from_byte, to_byte; | |
ec6d2bb8 | 8426 | |
b7826503 PJ |
8427 | CHECK_NUMBER_COERCE_MARKER (start); |
8428 | CHECK_NUMBER_COERCE_MARKER (end); | |
ec6d2bb8 | 8429 | |
d46c5b12 KH |
8430 | validate_region (&start, &end); |
8431 | from = XINT (start), to = XINT (end); | |
8432 | from_byte = CHAR_TO_BYTE (from); | |
8433 | to_byte = CHAR_TO_BYTE (to); | |
ec6d2bb8 | 8434 | |
d46c5b12 KH |
8435 | if (from < GPT && to >= GPT) |
8436 | move_gap_both (to, to_byte); | |
c210f766 | 8437 | |
d46c5b12 | 8438 | return detect_coding_system (BYTE_POS_ADDR (from_byte), |
24a73b0a | 8439 | to - from, to_byte - from_byte, |
0a28aafb | 8440 | !NILP (highest), |
4b4deea2 | 8441 | !NILP (BVAR (current_buffer |
5d8ea120 | 8442 | , enable_multibyte_characters)), |
df7492f9 | 8443 | Qnil); |
ec6d2bb8 KH |
8444 | } |
8445 | ||
d46c5b12 KH |
8446 | DEFUN ("detect-coding-string", Fdetect_coding_string, Sdetect_coding_string, |
8447 | 1, 2, 0, | |
48b0f3ae PJ |
8448 | doc: /* Detect coding system of the text in STRING. |
8449 | Return a list of possible coding systems ordered by priority. | |
67ceab9d KH |
8450 | The coding systems to try and their priorities follows what |
8451 | the function `coding-system-priority-list' (which see) returns. | |
fb88bf2d | 8452 | |
12e0131a | 8453 | If only ASCII characters are found (except for such ISO-2022 control |
d4a1d553 JB |
8454 | characters as ESC), it returns a list of single element `undecided' |
8455 | or its subsidiary coding system according to a detected end-of-line | |
8456 | format. | |
d46c5b12 | 8457 | |
48b0f3ae PJ |
8458 | If optional argument HIGHEST is non-nil, return the coding system of |
8459 | highest priority. */) | |
5842a27b | 8460 | (Lisp_Object string, Lisp_Object highest) |
d46c5b12 | 8461 | { |
b7826503 | 8462 | CHECK_STRING (string); |
b73bfc1c | 8463 | |
24a73b0a KH |
8464 | return detect_coding_system (SDATA (string), |
8465 | SCHARS (string), SBYTES (string), | |
8f924df7 | 8466 | !NILP (highest), STRING_MULTIBYTE (string), |
df7492f9 | 8467 | Qnil); |
4ed46869 | 8468 | } |
4ed46869 | 8469 | |
b73bfc1c | 8470 | |
df7492f9 | 8471 | static INLINE int |
971de7fb | 8472 | char_encodable_p (int c, Lisp_Object attrs) |
05e6f5dc | 8473 | { |
df7492f9 | 8474 | Lisp_Object tail; |
df7492f9 | 8475 | struct charset *charset; |
7d64c6ad | 8476 | Lisp_Object translation_table; |
d46c5b12 | 8477 | |
7d64c6ad | 8478 | translation_table = CODING_ATTR_TRANS_TBL (attrs); |
a6f87d34 | 8479 | if (! NILP (translation_table)) |
7d64c6ad | 8480 | c = translate_char (translation_table, c); |
df7492f9 KH |
8481 | for (tail = CODING_ATTR_CHARSET_LIST (attrs); |
8482 | CONSP (tail); tail = XCDR (tail)) | |
e133c8fa | 8483 | { |
df7492f9 KH |
8484 | charset = CHARSET_FROM_ID (XINT (XCAR (tail))); |
8485 | if (CHAR_CHARSET_P (c, charset)) | |
8486 | break; | |
e133c8fa | 8487 | } |
df7492f9 | 8488 | return (! NILP (tail)); |
05e6f5dc | 8489 | } |
83fa074f | 8490 | |
fb88bf2d | 8491 | |
df7492f9 KH |
8492 | /* Return a list of coding systems that safely encode the text between |
8493 | START and END. If EXCLUDE is non-nil, it is a list of coding | |
8494 | systems not to check. The returned list doesn't contain any such | |
48468dac | 8495 | coding systems. In any case, if the text contains only ASCII or is |
df7492f9 | 8496 | unibyte, return t. */ |
e077cc80 | 8497 | |
df7492f9 KH |
8498 | DEFUN ("find-coding-systems-region-internal", |
8499 | Ffind_coding_systems_region_internal, | |
8500 | Sfind_coding_systems_region_internal, 2, 3, 0, | |
8501 | doc: /* Internal use only. */) | |
5842a27b | 8502 | (Lisp_Object start, Lisp_Object end, Lisp_Object exclude) |
df7492f9 KH |
8503 | { |
8504 | Lisp_Object coding_attrs_list, safe_codings; | |
8505 | EMACS_INT start_byte, end_byte; | |
7c78e542 | 8506 | const unsigned char *p, *pbeg, *pend; |
df7492f9 | 8507 | int c; |
0e727afa | 8508 | Lisp_Object tail, elt, work_table; |
d46c5b12 | 8509 | |
df7492f9 KH |
8510 | if (STRINGP (start)) |
8511 | { | |
8512 | if (!STRING_MULTIBYTE (start) | |
8f924df7 | 8513 | || SCHARS (start) == SBYTES (start)) |
df7492f9 KH |
8514 | return Qt; |
8515 | start_byte = 0; | |
8f924df7 | 8516 | end_byte = SBYTES (start); |
df7492f9 KH |
8517 | } |
8518 | else | |
d46c5b12 | 8519 | { |
df7492f9 KH |
8520 | CHECK_NUMBER_COERCE_MARKER (start); |
8521 | CHECK_NUMBER_COERCE_MARKER (end); | |
8522 | if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end)) | |
8523 | args_out_of_range (start, end); | |
4b4deea2 | 8524 | if (NILP (BVAR (current_buffer, enable_multibyte_characters))) |
df7492f9 KH |
8525 | return Qt; |
8526 | start_byte = CHAR_TO_BYTE (XINT (start)); | |
8527 | end_byte = CHAR_TO_BYTE (XINT (end)); | |
8528 | if (XINT (end) - XINT (start) == end_byte - start_byte) | |
8529 | return Qt; | |
d46c5b12 | 8530 | |
e1c23804 | 8531 | if (XINT (start) < GPT && XINT (end) > GPT) |
d46c5b12 | 8532 | { |
e1c23804 DL |
8533 | if ((GPT - XINT (start)) < (XINT (end) - GPT)) |
8534 | move_gap_both (XINT (start), start_byte); | |
df7492f9 | 8535 | else |
e1c23804 | 8536 | move_gap_both (XINT (end), end_byte); |
d46c5b12 KH |
8537 | } |
8538 | } | |
8539 | ||
df7492f9 KH |
8540 | coding_attrs_list = Qnil; |
8541 | for (tail = Vcoding_system_list; CONSP (tail); tail = XCDR (tail)) | |
8542 | if (NILP (exclude) | |
8543 | || NILP (Fmemq (XCAR (tail), exclude))) | |
8544 | { | |
8545 | Lisp_Object attrs; | |
d46c5b12 | 8546 | |
df7492f9 KH |
8547 | attrs = AREF (CODING_SYSTEM_SPEC (XCAR (tail)), 0); |
8548 | if (EQ (XCAR (tail), CODING_ATTR_BASE_NAME (attrs)) | |
8549 | && ! EQ (CODING_ATTR_TYPE (attrs), Qundecided)) | |
7d64c6ad KH |
8550 | { |
8551 | ASET (attrs, coding_attr_trans_tbl, | |
2170c8f0 | 8552 | get_translation_table (attrs, 1, NULL)); |
7d64c6ad KH |
8553 | coding_attrs_list = Fcons (attrs, coding_attrs_list); |
8554 | } | |
df7492f9 | 8555 | } |
d46c5b12 | 8556 | |
df7492f9 | 8557 | if (STRINGP (start)) |
8f924df7 | 8558 | p = pbeg = SDATA (start); |
df7492f9 KH |
8559 | else |
8560 | p = pbeg = BYTE_POS_ADDR (start_byte); | |
8561 | pend = p + (end_byte - start_byte); | |
b843d1ae | 8562 | |
df7492f9 KH |
8563 | while (p < pend && ASCII_BYTE_P (*p)) p++; |
8564 | while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--; | |
d46c5b12 | 8565 | |
0e727afa | 8566 | work_table = Fmake_char_table (Qnil, Qnil); |
05e6f5dc | 8567 | while (p < pend) |
72d1a715 | 8568 | { |
df7492f9 KH |
8569 | if (ASCII_BYTE_P (*p)) |
8570 | p++; | |
72d1a715 RS |
8571 | else |
8572 | { | |
df7492f9 | 8573 | c = STRING_CHAR_ADVANCE (p); |
0e727afa YM |
8574 | if (!NILP (char_table_ref (work_table, c))) |
8575 | /* This character was already checked. Ignore it. */ | |
8576 | continue; | |
12410ef1 | 8577 | |
df7492f9 KH |
8578 | charset_map_loaded = 0; |
8579 | for (tail = coding_attrs_list; CONSP (tail);) | |
8580 | { | |
8581 | elt = XCAR (tail); | |
8582 | if (NILP (elt)) | |
8583 | tail = XCDR (tail); | |
8584 | else if (char_encodable_p (c, elt)) | |
8585 | tail = XCDR (tail); | |
8586 | else if (CONSP (XCDR (tail))) | |
8587 | { | |
8588 | XSETCAR (tail, XCAR (XCDR (tail))); | |
8589 | XSETCDR (tail, XCDR (XCDR (tail))); | |
8590 | } | |
8591 | else | |
8592 | { | |
8593 | XSETCAR (tail, Qnil); | |
8594 | tail = XCDR (tail); | |
8595 | } | |
8596 | } | |
8597 | if (charset_map_loaded) | |
8598 | { | |
8599 | EMACS_INT p_offset = p - pbeg, pend_offset = pend - pbeg; | |
05e6f5dc | 8600 | |
df7492f9 | 8601 | if (STRINGP (start)) |
8f924df7 | 8602 | pbeg = SDATA (start); |
df7492f9 KH |
8603 | else |
8604 | pbeg = BYTE_POS_ADDR (start_byte); | |
8605 | p = pbeg + p_offset; | |
8606 | pend = pbeg + pend_offset; | |
8607 | } | |
0e727afa | 8608 | char_table_set (work_table, c, Qt); |
df7492f9 | 8609 | } |
ec6d2bb8 | 8610 | } |
fb88bf2d | 8611 | |
988b3759 | 8612 | safe_codings = list2 (Qraw_text, Qno_conversion); |
df7492f9 KH |
8613 | for (tail = coding_attrs_list; CONSP (tail); tail = XCDR (tail)) |
8614 | if (! NILP (XCAR (tail))) | |
8615 | safe_codings = Fcons (CODING_ATTR_BASE_NAME (XCAR (tail)), safe_codings); | |
ec6d2bb8 | 8616 | |
05e6f5dc KH |
8617 | return safe_codings; |
8618 | } | |
4956c225 | 8619 | |
d46c5b12 | 8620 | |
8f924df7 KH |
8621 | DEFUN ("unencodable-char-position", Funencodable_char_position, |
8622 | Sunencodable_char_position, 3, 5, 0, | |
8623 | doc: /* | |
8624 | Return position of first un-encodable character in a region. | |
d4a1d553 | 8625 | START and END specify the region and CODING-SYSTEM specifies the |
8f924df7 | 8626 | encoding to check. Return nil if CODING-SYSTEM does encode the region. |
d46c5b12 | 8627 | |
8f924df7 KH |
8628 | If optional 4th argument COUNT is non-nil, it specifies at most how |
8629 | many un-encodable characters to search. In this case, the value is a | |
8630 | list of positions. | |
d46c5b12 | 8631 | |
8f924df7 KH |
8632 | If optional 5th argument STRING is non-nil, it is a string to search |
8633 | for un-encodable characters. In that case, START and END are indexes | |
8634 | to the string. */) | |
5842a27b | 8635 | (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object count, Lisp_Object string) |
8f924df7 KH |
8636 | { |
8637 | int n; | |
8638 | struct coding_system coding; | |
7d64c6ad | 8639 | Lisp_Object attrs, charset_list, translation_table; |
8f924df7 KH |
8640 | Lisp_Object positions; |
8641 | int from, to; | |
8642 | const unsigned char *p, *stop, *pend; | |
8643 | int ascii_compatible; | |
fb88bf2d | 8644 | |
8f924df7 KH |
8645 | setup_coding_system (Fcheck_coding_system (coding_system), &coding); |
8646 | attrs = CODING_ID_ATTRS (coding.id); | |
8647 | if (EQ (CODING_ATTR_TYPE (attrs), Qraw_text)) | |
8648 | return Qnil; | |
8649 | ascii_compatible = ! NILP (CODING_ATTR_ASCII_COMPAT (attrs)); | |
8650 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
2170c8f0 | 8651 | translation_table = get_translation_table (attrs, 1, NULL); |
fb88bf2d | 8652 | |
8f924df7 KH |
8653 | if (NILP (string)) |
8654 | { | |
8655 | validate_region (&start, &end); | |
8656 | from = XINT (start); | |
8657 | to = XINT (end); | |
4b4deea2 | 8658 | if (NILP (BVAR (current_buffer, enable_multibyte_characters)) |
8f924df7 KH |
8659 | || (ascii_compatible |
8660 | && (to - from) == (CHAR_TO_BYTE (to) - (CHAR_TO_BYTE (from))))) | |
8661 | return Qnil; | |
8662 | p = CHAR_POS_ADDR (from); | |
8663 | pend = CHAR_POS_ADDR (to); | |
8664 | if (from < GPT && to >= GPT) | |
8665 | stop = GPT_ADDR; | |
8666 | else | |
8667 | stop = pend; | |
8668 | } | |
8669 | else | |
8670 | { | |
8671 | CHECK_STRING (string); | |
8672 | CHECK_NATNUM (start); | |
8673 | CHECK_NATNUM (end); | |
8674 | from = XINT (start); | |
8675 | to = XINT (end); | |
8676 | if (from > to | |
8677 | || to > SCHARS (string)) | |
8678 | args_out_of_range_3 (string, start, end); | |
8679 | if (! STRING_MULTIBYTE (string)) | |
8680 | return Qnil; | |
8681 | p = SDATA (string) + string_char_to_byte (string, from); | |
8682 | stop = pend = SDATA (string) + string_char_to_byte (string, to); | |
8683 | if (ascii_compatible && (to - from) == (pend - p)) | |
8684 | return Qnil; | |
8685 | } | |
f2558efd | 8686 | |
8f924df7 KH |
8687 | if (NILP (count)) |
8688 | n = 1; | |
8689 | else | |
b73bfc1c | 8690 | { |
8f924df7 KH |
8691 | CHECK_NATNUM (count); |
8692 | n = XINT (count); | |
b73bfc1c KH |
8693 | } |
8694 | ||
8f924df7 KH |
8695 | positions = Qnil; |
8696 | while (1) | |
d46c5b12 | 8697 | { |
8f924df7 | 8698 | int c; |
ec6d2bb8 | 8699 | |
8f924df7 KH |
8700 | if (ascii_compatible) |
8701 | while (p < stop && ASCII_BYTE_P (*p)) | |
8702 | p++, from++; | |
8703 | if (p >= stop) | |
0e79d667 | 8704 | { |
8f924df7 KH |
8705 | if (p >= pend) |
8706 | break; | |
8707 | stop = pend; | |
8708 | p = GAP_END_ADDR; | |
0e79d667 | 8709 | } |
ec6d2bb8 | 8710 | |
8f924df7 KH |
8711 | c = STRING_CHAR_ADVANCE (p); |
8712 | if (! (ASCII_CHAR_P (c) && ascii_compatible) | |
7d64c6ad KH |
8713 | && ! char_charset (translate_char (translation_table, c), |
8714 | charset_list, NULL)) | |
ec6d2bb8 | 8715 | { |
8f924df7 KH |
8716 | positions = Fcons (make_number (from), positions); |
8717 | n--; | |
8718 | if (n == 0) | |
8719 | break; | |
ec6d2bb8 KH |
8720 | } |
8721 | ||
8f924df7 KH |
8722 | from++; |
8723 | } | |
d46c5b12 | 8724 | |
8f924df7 KH |
8725 | return (NILP (count) ? Fcar (positions) : Fnreverse (positions)); |
8726 | } | |
d46c5b12 | 8727 | |
d46c5b12 | 8728 | |
df7492f9 KH |
8729 | DEFUN ("check-coding-systems-region", Fcheck_coding_systems_region, |
8730 | Scheck_coding_systems_region, 3, 3, 0, | |
8731 | doc: /* Check if the region is encodable by coding systems. | |
d46c5b12 | 8732 | |
df7492f9 KH |
8733 | START and END are buffer positions specifying the region. |
8734 | CODING-SYSTEM-LIST is a list of coding systems to check. | |
d46c5b12 | 8735 | |
df7492f9 | 8736 | The value is an alist ((CODING-SYSTEM POS0 POS1 ...) ...), where |
d4a1d553 | 8737 | CODING-SYSTEM is a member of CODING-SYSTEM-LIST and can't encode the |
df7492f9 KH |
8738 | whole region, POS0, POS1, ... are buffer positions where non-encodable |
8739 | characters are found. | |
93dec019 | 8740 | |
df7492f9 KH |
8741 | If all coding systems in CODING-SYSTEM-LIST can encode the region, the |
8742 | value is nil. | |
93dec019 | 8743 | |
df7492f9 KH |
8744 | START may be a string. In that case, check if the string is |
8745 | encodable, and the value contains indices to the string instead of | |
5704f39a KH |
8746 | buffer positions. END is ignored. |
8747 | ||
4c1958f4 | 8748 | If the current buffer (or START if it is a string) is unibyte, the value |
5704f39a | 8749 | is nil. */) |
5842a27b | 8750 | (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system_list) |
05e6f5dc | 8751 | { |
df7492f9 KH |
8752 | Lisp_Object list; |
8753 | EMACS_INT start_byte, end_byte; | |
8754 | int pos; | |
7c78e542 | 8755 | const unsigned char *p, *pbeg, *pend; |
df7492f9 | 8756 | int c; |
7d64c6ad | 8757 | Lisp_Object tail, elt, attrs; |
70ad9fc4 | 8758 | |
05e6f5dc KH |
8759 | if (STRINGP (start)) |
8760 | { | |
df7492f9 | 8761 | if (!STRING_MULTIBYTE (start) |
4c1958f4 | 8762 | || SCHARS (start) == SBYTES (start)) |
df7492f9 KH |
8763 | return Qnil; |
8764 | start_byte = 0; | |
8f924df7 | 8765 | end_byte = SBYTES (start); |
df7492f9 | 8766 | pos = 0; |
d46c5b12 | 8767 | } |
05e6f5dc | 8768 | else |
b73bfc1c | 8769 | { |
b7826503 PJ |
8770 | CHECK_NUMBER_COERCE_MARKER (start); |
8771 | CHECK_NUMBER_COERCE_MARKER (end); | |
05e6f5dc KH |
8772 | if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end)) |
8773 | args_out_of_range (start, end); | |
4b4deea2 | 8774 | if (NILP (BVAR (current_buffer, enable_multibyte_characters))) |
df7492f9 KH |
8775 | return Qnil; |
8776 | start_byte = CHAR_TO_BYTE (XINT (start)); | |
8777 | end_byte = CHAR_TO_BYTE (XINT (end)); | |
8778 | if (XINT (end) - XINT (start) == end_byte - start_byte) | |
5704f39a | 8779 | return Qnil; |
df7492f9 | 8780 | |
e1c23804 | 8781 | if (XINT (start) < GPT && XINT (end) > GPT) |
b73bfc1c | 8782 | { |
e1c23804 DL |
8783 | if ((GPT - XINT (start)) < (XINT (end) - GPT)) |
8784 | move_gap_both (XINT (start), start_byte); | |
df7492f9 | 8785 | else |
e1c23804 | 8786 | move_gap_both (XINT (end), end_byte); |
b73bfc1c | 8787 | } |
e1c23804 | 8788 | pos = XINT (start); |
b73bfc1c | 8789 | } |
7553d0e1 | 8790 | |
df7492f9 KH |
8791 | list = Qnil; |
8792 | for (tail = coding_system_list; CONSP (tail); tail = XCDR (tail)) | |
12410ef1 | 8793 | { |
df7492f9 | 8794 | elt = XCAR (tail); |
7d64c6ad | 8795 | attrs = AREF (CODING_SYSTEM_SPEC (elt), 0); |
2170c8f0 KH |
8796 | ASET (attrs, coding_attr_trans_tbl, |
8797 | get_translation_table (attrs, 1, NULL)); | |
7d64c6ad | 8798 | list = Fcons (Fcons (elt, Fcons (attrs, Qnil)), list); |
12410ef1 KH |
8799 | } |
8800 | ||
df7492f9 | 8801 | if (STRINGP (start)) |
8f924df7 | 8802 | p = pbeg = SDATA (start); |
72d1a715 | 8803 | else |
df7492f9 KH |
8804 | p = pbeg = BYTE_POS_ADDR (start_byte); |
8805 | pend = p + (end_byte - start_byte); | |
4ed46869 | 8806 | |
df7492f9 KH |
8807 | while (p < pend && ASCII_BYTE_P (*p)) p++, pos++; |
8808 | while (p < pend && ASCII_BYTE_P (*(pend - 1))) pend--; | |
ec6d2bb8 | 8809 | |
df7492f9 | 8810 | while (p < pend) |
d46c5b12 | 8811 | { |
df7492f9 KH |
8812 | if (ASCII_BYTE_P (*p)) |
8813 | p++; | |
e133c8fa | 8814 | else |
05e6f5dc | 8815 | { |
df7492f9 KH |
8816 | c = STRING_CHAR_ADVANCE (p); |
8817 | ||
8818 | charset_map_loaded = 0; | |
8819 | for (tail = list; CONSP (tail); tail = XCDR (tail)) | |
8820 | { | |
8821 | elt = XCDR (XCAR (tail)); | |
8822 | if (! char_encodable_p (c, XCAR (elt))) | |
8823 | XSETCDR (elt, Fcons (make_number (pos), XCDR (elt))); | |
8824 | } | |
8825 | if (charset_map_loaded) | |
8826 | { | |
8827 | EMACS_INT p_offset = p - pbeg, pend_offset = pend - pbeg; | |
8828 | ||
8829 | if (STRINGP (start)) | |
8f924df7 | 8830 | pbeg = SDATA (start); |
df7492f9 KH |
8831 | else |
8832 | pbeg = BYTE_POS_ADDR (start_byte); | |
8833 | p = pbeg + p_offset; | |
8834 | pend = pbeg + pend_offset; | |
8835 | } | |
05e6f5dc | 8836 | } |
df7492f9 | 8837 | pos++; |
d46c5b12 | 8838 | } |
4ed46869 | 8839 | |
df7492f9 KH |
8840 | tail = list; |
8841 | list = Qnil; | |
8842 | for (; CONSP (tail); tail = XCDR (tail)) | |
ec6d2bb8 | 8843 | { |
df7492f9 KH |
8844 | elt = XCAR (tail); |
8845 | if (CONSP (XCDR (XCDR (elt)))) | |
8846 | list = Fcons (Fcons (XCAR (elt), Fnreverse (XCDR (XCDR (elt)))), | |
8847 | list); | |
ec6d2bb8 | 8848 | } |
2b4f9037 | 8849 | |
df7492f9 | 8850 | return list; |
d46c5b12 KH |
8851 | } |
8852 | ||
3fd9494b | 8853 | |
b73bfc1c | 8854 | Lisp_Object |
cf84bb53 JB |
8855 | code_convert_region (Lisp_Object start, Lisp_Object end, |
8856 | Lisp_Object coding_system, Lisp_Object dst_object, | |
8857 | int encodep, int norecord) | |
4ed46869 | 8858 | { |
3a73fa5d | 8859 | struct coding_system coding; |
df7492f9 KH |
8860 | EMACS_INT from, from_byte, to, to_byte; |
8861 | Lisp_Object src_object; | |
4ed46869 | 8862 | |
b7826503 PJ |
8863 | CHECK_NUMBER_COERCE_MARKER (start); |
8864 | CHECK_NUMBER_COERCE_MARKER (end); | |
df7492f9 KH |
8865 | if (NILP (coding_system)) |
8866 | coding_system = Qno_conversion; | |
8867 | else | |
8868 | CHECK_CODING_SYSTEM (coding_system); | |
8869 | src_object = Fcurrent_buffer (); | |
8870 | if (NILP (dst_object)) | |
8871 | dst_object = src_object; | |
8872 | else if (! EQ (dst_object, Qt)) | |
8873 | CHECK_BUFFER (dst_object); | |
3a73fa5d | 8874 | |
d46c5b12 KH |
8875 | validate_region (&start, &end); |
8876 | from = XFASTINT (start); | |
df7492f9 | 8877 | from_byte = CHAR_TO_BYTE (from); |
d46c5b12 | 8878 | to = XFASTINT (end); |
df7492f9 | 8879 | to_byte = CHAR_TO_BYTE (to); |
764ca8da | 8880 | |
df7492f9 KH |
8881 | setup_coding_system (coding_system, &coding); |
8882 | coding.mode |= CODING_MODE_LAST_BLOCK; | |
ec6d2bb8 | 8883 | |
df7492f9 KH |
8884 | if (encodep) |
8885 | encode_coding_object (&coding, src_object, from, from_byte, to, to_byte, | |
8886 | dst_object); | |
8887 | else | |
8888 | decode_coding_object (&coding, src_object, from, from_byte, to, to_byte, | |
8889 | dst_object); | |
8890 | if (! norecord) | |
8891 | Vlast_coding_system_used = CODING_ID_NAME (coding.id); | |
ec6d2bb8 | 8892 | |
df7492f9 KH |
8893 | return (BUFFERP (dst_object) |
8894 | ? make_number (coding.produced_char) | |
8895 | : coding.dst_object); | |
4031e2bf | 8896 | } |
78108bcd | 8897 | |
4ed46869 | 8898 | |
4031e2bf | 8899 | DEFUN ("decode-coding-region", Fdecode_coding_region, Sdecode_coding_region, |
df7492f9 | 8900 | 3, 4, "r\nzCoding system: ", |
48b0f3ae | 8901 | doc: /* Decode the current region from the specified coding system. |
df7492f9 KH |
8902 | When called from a program, takes four arguments: |
8903 | START, END, CODING-SYSTEM, and DESTINATION. | |
8904 | START and END are buffer positions. | |
8844fa83 | 8905 | |
df7492f9 | 8906 | Optional 4th arguments DESTINATION specifies where the decoded text goes. |
2354b80b | 8907 | If nil, the region between START and END is replaced by the decoded text. |
1560f91a EZ |
8908 | If buffer, the decoded text is inserted in that buffer after point (point |
8909 | does not move). | |
446dcd75 | 8910 | In those cases, the length of the decoded text is returned. |
319a3947 | 8911 | If DESTINATION is t, the decoded text is returned. |
8844fa83 | 8912 | |
48b0f3ae PJ |
8913 | This function sets `last-coding-system-used' to the precise coding system |
8914 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
319a3947 | 8915 | not fully specified.) */) |
5842a27b | 8916 | (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object destination) |
4031e2bf | 8917 | { |
df7492f9 | 8918 | return code_convert_region (start, end, coding_system, destination, 0, 0); |
3a73fa5d | 8919 | } |
8844fa83 | 8920 | |
3a73fa5d | 8921 | DEFUN ("encode-coding-region", Fencode_coding_region, Sencode_coding_region, |
df7492f9 KH |
8922 | 3, 4, "r\nzCoding system: ", |
8923 | doc: /* Encode the current region by specified coding system. | |
d4a1d553 JB |
8924 | When called from a program, takes four arguments: |
8925 | START, END, CODING-SYSTEM and DESTINATION. | |
8926 | START and END are buffer positions. | |
d46c5b12 | 8927 | |
df7492f9 KH |
8928 | Optional 4th arguments DESTINATION specifies where the encoded text goes. |
8929 | If nil, the region between START and END is replace by the encoded text. | |
1560f91a EZ |
8930 | If buffer, the encoded text is inserted in that buffer after point (point |
8931 | does not move). | |
446dcd75 | 8932 | In those cases, the length of the encoded text is returned. |
319a3947 | 8933 | If DESTINATION is t, the encoded text is returned. |
2391eaa4 | 8934 | |
48b0f3ae PJ |
8935 | This function sets `last-coding-system-used' to the precise coding system |
8936 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
319a3947 | 8937 | not fully specified.) */) |
5842a27b | 8938 | (Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object destination) |
3a73fa5d | 8939 | { |
df7492f9 | 8940 | return code_convert_region (start, end, coding_system, destination, 1, 0); |
b73bfc1c KH |
8941 | } |
8942 | ||
8943 | Lisp_Object | |
6f704c76 DN |
8944 | code_convert_string (Lisp_Object string, Lisp_Object coding_system, |
8945 | Lisp_Object dst_object, int encodep, int nocopy, int norecord) | |
b73bfc1c | 8946 | { |
4031e2bf | 8947 | struct coding_system coding; |
df7492f9 | 8948 | EMACS_INT chars, bytes; |
ec6d2bb8 | 8949 | |
b7826503 | 8950 | CHECK_STRING (string); |
d46c5b12 | 8951 | if (NILP (coding_system)) |
4956c225 | 8952 | { |
df7492f9 KH |
8953 | if (! norecord) |
8954 | Vlast_coding_system_used = Qno_conversion; | |
8955 | if (NILP (dst_object)) | |
8956 | return (nocopy ? Fcopy_sequence (string) : string); | |
4956c225 | 8957 | } |
b73bfc1c | 8958 | |
df7492f9 KH |
8959 | if (NILP (coding_system)) |
8960 | coding_system = Qno_conversion; | |
8961 | else | |
8962 | CHECK_CODING_SYSTEM (coding_system); | |
8963 | if (NILP (dst_object)) | |
8964 | dst_object = Qt; | |
8965 | else if (! EQ (dst_object, Qt)) | |
8966 | CHECK_BUFFER (dst_object); | |
73be902c | 8967 | |
df7492f9 | 8968 | setup_coding_system (coding_system, &coding); |
d46c5b12 | 8969 | coding.mode |= CODING_MODE_LAST_BLOCK; |
8f924df7 KH |
8970 | chars = SCHARS (string); |
8971 | bytes = SBYTES (string); | |
df7492f9 KH |
8972 | if (encodep) |
8973 | encode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object); | |
8974 | else | |
8975 | decode_coding_object (&coding, string, 0, 0, chars, bytes, dst_object); | |
8976 | if (! norecord) | |
8977 | Vlast_coding_system_used = CODING_ID_NAME (coding.id); | |
73be902c | 8978 | |
df7492f9 KH |
8979 | return (BUFFERP (dst_object) |
8980 | ? make_number (coding.produced_char) | |
8981 | : coding.dst_object); | |
4ed46869 | 8982 | } |
73be902c | 8983 | |
b73bfc1c | 8984 | |
ecec61c1 | 8985 | /* Encode or decode STRING according to CODING_SYSTEM. |
ec6d2bb8 | 8986 | Do not set Vlast_coding_system_used. |
4ed46869 | 8987 | |
ec6d2bb8 KH |
8988 | This function is called only from macros DECODE_FILE and |
8989 | ENCODE_FILE, thus we ignore character composition. */ | |
4ed46869 | 8990 | |
ecec61c1 | 8991 | Lisp_Object |
cf84bb53 JB |
8992 | code_convert_string_norecord (Lisp_Object string, Lisp_Object coding_system, |
8993 | int encodep) | |
4ed46869 | 8994 | { |
0be8721c | 8995 | return code_convert_string (string, coding_system, Qt, encodep, 0, 1); |
4ed46869 KH |
8996 | } |
8997 | ||
4ed46869 | 8998 | |
df7492f9 KH |
8999 | DEFUN ("decode-coding-string", Fdecode_coding_string, Sdecode_coding_string, |
9000 | 2, 4, 0, | |
9001 | doc: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result. | |
9002 | ||
9003 | Optional third arg NOCOPY non-nil means it is OK to return STRING itself | |
9004 | if the decoding operation is trivial. | |
ecec61c1 | 9005 | |
d4a1d553 | 9006 | Optional fourth arg BUFFER non-nil means that the decoded text is |
1560f91a EZ |
9007 | inserted in that buffer after point (point does not move). In this |
9008 | case, the return value is the length of the decoded text. | |
ecec61c1 | 9009 | |
df7492f9 KH |
9010 | This function sets `last-coding-system-used' to the precise coding system |
9011 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
d4a1d553 | 9012 | not fully specified.) */) |
5842a27b | 9013 | (Lisp_Object string, Lisp_Object coding_system, Lisp_Object nocopy, Lisp_Object buffer) |
4ed46869 | 9014 | { |
df7492f9 KH |
9015 | return code_convert_string (string, coding_system, buffer, |
9016 | 0, ! NILP (nocopy), 0); | |
4ed46869 KH |
9017 | } |
9018 | ||
df7492f9 KH |
9019 | DEFUN ("encode-coding-string", Fencode_coding_string, Sencode_coding_string, |
9020 | 2, 4, 0, | |
9021 | doc: /* Encode STRING to CODING-SYSTEM, and return the result. | |
9022 | ||
9023 | Optional third arg NOCOPY non-nil means it is OK to return STRING | |
9024 | itself if the encoding operation is trivial. | |
9025 | ||
d4a1d553 | 9026 | Optional fourth arg BUFFER non-nil means that the encoded text is |
1560f91a EZ |
9027 | inserted in that buffer after point (point does not move). In this |
9028 | case, the return value is the length of the encoded text. | |
df7492f9 KH |
9029 | |
9030 | This function sets `last-coding-system-used' to the precise coding system | |
9031 | used (which may be different from CODING-SYSTEM if CODING-SYSTEM is | |
9032 | not fully specified.) */) | |
5842a27b | 9033 | (Lisp_Object string, Lisp_Object coding_system, Lisp_Object nocopy, Lisp_Object buffer) |
4ed46869 | 9034 | { |
df7492f9 | 9035 | return code_convert_string (string, coding_system, buffer, |
c197f191 | 9036 | 1, ! NILP (nocopy), 1); |
4ed46869 | 9037 | } |
df7492f9 | 9038 | |
3a73fa5d | 9039 | \f |
4ed46869 | 9040 | DEFUN ("decode-sjis-char", Fdecode_sjis_char, Sdecode_sjis_char, 1, 1, 0, |
48b0f3ae PJ |
9041 | doc: /* Decode a Japanese character which has CODE in shift_jis encoding. |
9042 | Return the corresponding character. */) | |
5842a27b | 9043 | (Lisp_Object code) |
4ed46869 | 9044 | { |
df7492f9 KH |
9045 | Lisp_Object spec, attrs, val; |
9046 | struct charset *charset_roman, *charset_kanji, *charset_kana, *charset; | |
9047 | int c; | |
4ed46869 | 9048 | |
df7492f9 KH |
9049 | CHECK_NATNUM (code); |
9050 | c = XFASTINT (code); | |
9051 | CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec); | |
9052 | attrs = AREF (spec, 0); | |
4ed46869 | 9053 | |
df7492f9 KH |
9054 | if (ASCII_BYTE_P (c) |
9055 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
9056 | return code; | |
4ed46869 | 9057 | |
df7492f9 KH |
9058 | val = CODING_ATTR_CHARSET_LIST (attrs); |
9059 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
004068e4 KH |
9060 | charset_kana = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); |
9061 | charset_kanji = CHARSET_FROM_ID (XINT (XCAR (val))); | |
fa42c37f | 9062 | |
df7492f9 KH |
9063 | if (c <= 0x7F) |
9064 | charset = charset_roman; | |
9065 | else if (c >= 0xA0 && c < 0xDF) | |
55ab7be3 | 9066 | { |
df7492f9 KH |
9067 | charset = charset_kana; |
9068 | c -= 0x80; | |
4ed46869 | 9069 | } |
55ab7be3 | 9070 | else |
4ed46869 | 9071 | { |
2735d060 | 9072 | int c1 = c >> 8, c2 = c & 0xFF; |
df7492f9 | 9073 | |
2735d060 PE |
9074 | if (c1 < 0x81 || (c1 > 0x9F && c1 < 0xE0) || c1 > 0xEF |
9075 | || c2 < 0x40 || c2 == 0x7F || c2 > 0xFC) | |
df7492f9 KH |
9076 | error ("Invalid code: %d", code); |
9077 | SJIS_TO_JIS (c); | |
9078 | charset = charset_kanji; | |
4ed46869 | 9079 | } |
df7492f9 KH |
9080 | c = DECODE_CHAR (charset, c); |
9081 | if (c < 0) | |
9082 | error ("Invalid code: %d", code); | |
9083 | return make_number (c); | |
93dec019 | 9084 | } |
4ed46869 | 9085 | |
48b0f3ae | 9086 | |
4ed46869 | 9087 | DEFUN ("encode-sjis-char", Fencode_sjis_char, Sencode_sjis_char, 1, 1, 0, |
8acf0c0e | 9088 | doc: /* Encode a Japanese character CH to shift_jis encoding. |
48b0f3ae | 9089 | Return the corresponding code in SJIS. */) |
5842a27b | 9090 | (Lisp_Object ch) |
4ed46869 | 9091 | { |
df7492f9 KH |
9092 | Lisp_Object spec, attrs, charset_list; |
9093 | int c; | |
9094 | struct charset *charset; | |
9095 | unsigned code; | |
48b0f3ae | 9096 | |
df7492f9 KH |
9097 | CHECK_CHARACTER (ch); |
9098 | c = XFASTINT (ch); | |
9099 | CHECK_CODING_SYSTEM_GET_SPEC (Vsjis_coding_system, spec); | |
9100 | attrs = AREF (spec, 0); | |
9101 | ||
9102 | if (ASCII_CHAR_P (c) | |
9103 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
9104 | return ch; | |
9105 | ||
9106 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
9107 | charset = char_charset (c, charset_list, &code); | |
9108 | if (code == CHARSET_INVALID_CODE (charset)) | |
9109 | error ("Can't encode by shift_jis encoding: %d", c); | |
9110 | JIS_TO_SJIS (code); | |
9111 | ||
9112 | return make_number (code); | |
4ed46869 KH |
9113 | } |
9114 | ||
9115 | DEFUN ("decode-big5-char", Fdecode_big5_char, Sdecode_big5_char, 1, 1, 0, | |
48b0f3ae PJ |
9116 | doc: /* Decode a Big5 character which has CODE in BIG5 coding system. |
9117 | Return the corresponding character. */) | |
5842a27b | 9118 | (Lisp_Object code) |
d46c5b12 | 9119 | { |
df7492f9 KH |
9120 | Lisp_Object spec, attrs, val; |
9121 | struct charset *charset_roman, *charset_big5, *charset; | |
9122 | int c; | |
6289dd10 | 9123 | |
df7492f9 KH |
9124 | CHECK_NATNUM (code); |
9125 | c = XFASTINT (code); | |
9126 | CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec); | |
9127 | attrs = AREF (spec, 0); | |
4ed46869 | 9128 | |
df7492f9 KH |
9129 | if (ASCII_BYTE_P (c) |
9130 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
9131 | return code; | |
6289dd10 | 9132 | |
df7492f9 KH |
9133 | val = CODING_ATTR_CHARSET_LIST (attrs); |
9134 | charset_roman = CHARSET_FROM_ID (XINT (XCAR (val))), val = XCDR (val); | |
9135 | charset_big5 = CHARSET_FROM_ID (XINT (XCAR (val))); | |
c210f766 | 9136 | |
df7492f9 KH |
9137 | if (c <= 0x7F) |
9138 | charset = charset_roman; | |
c28a9453 KH |
9139 | else |
9140 | { | |
df7492f9 KH |
9141 | int b1 = c >> 8, b2 = c & 0x7F; |
9142 | if (b1 < 0xA1 || b1 > 0xFE | |
9143 | || b2 < 0x40 || (b2 > 0x7E && b2 < 0xA1) || b2 > 0xFE) | |
9144 | error ("Invalid code: %d", code); | |
9145 | charset = charset_big5; | |
c28a9453 | 9146 | } |
df7492f9 KH |
9147 | c = DECODE_CHAR (charset, (unsigned )c); |
9148 | if (c < 0) | |
9149 | error ("Invalid code: %d", code); | |
9150 | return make_number (c); | |
d46c5b12 | 9151 | } |
6289dd10 | 9152 | |
4ed46869 | 9153 | DEFUN ("encode-big5-char", Fencode_big5_char, Sencode_big5_char, 1, 1, 0, |
8acf0c0e | 9154 | doc: /* Encode the Big5 character CH to BIG5 coding system. |
48b0f3ae | 9155 | Return the corresponding character code in Big5. */) |
5842a27b | 9156 | (Lisp_Object ch) |
4ed46869 | 9157 | { |
df7492f9 KH |
9158 | Lisp_Object spec, attrs, charset_list; |
9159 | struct charset *charset; | |
9160 | int c; | |
9161 | unsigned code; | |
9162 | ||
9163 | CHECK_CHARACTER (ch); | |
9164 | c = XFASTINT (ch); | |
9165 | CHECK_CODING_SYSTEM_GET_SPEC (Vbig5_coding_system, spec); | |
9166 | attrs = AREF (spec, 0); | |
9167 | if (ASCII_CHAR_P (c) | |
9168 | && ! NILP (CODING_ATTR_ASCII_COMPAT (attrs))) | |
9169 | return ch; | |
9170 | ||
9171 | charset_list = CODING_ATTR_CHARSET_LIST (attrs); | |
9172 | charset = char_charset (c, charset_list, &code); | |
9173 | if (code == CHARSET_INVALID_CODE (charset)) | |
9174 | error ("Can't encode by Big5 encoding: %d", c); | |
9175 | ||
9176 | return make_number (code); | |
4ed46869 | 9177 | } |
48b0f3ae | 9178 | |
3a73fa5d | 9179 | \f |
002fdb44 | 9180 | DEFUN ("set-terminal-coding-system-internal", Fset_terminal_coding_system_internal, |
68bba4e4 | 9181 | Sset_terminal_coding_system_internal, 1, 2, 0, |
48b0f3ae | 9182 | doc: /* Internal use only. */) |
5842a27b | 9183 | (Lisp_Object coding_system, Lisp_Object terminal) |
4ed46869 | 9184 | { |
b18fad6d KH |
9185 | struct terminal *term = get_terminal (terminal, 1); |
9186 | struct coding_system *terminal_coding = TERMINAL_TERMINAL_CODING (term); | |
b7826503 | 9187 | CHECK_SYMBOL (coding_system); |
b8299c66 | 9188 | setup_coding_system (Fcheck_coding_system (coding_system), terminal_coding); |
70c22245 | 9189 | /* We had better not send unsafe characters to terminal. */ |
c73bd236 | 9190 | terminal_coding->mode |= CODING_MODE_SAFE_ENCODING; |
ad1746f5 | 9191 | /* Character composition should be disabled. */ |
c73bd236 | 9192 | terminal_coding->common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK; |
b8299c66 KL |
9193 | terminal_coding->src_multibyte = 1; |
9194 | terminal_coding->dst_multibyte = 0; | |
b18fad6d KH |
9195 | if (terminal_coding->common_flags & CODING_REQUIRE_ENCODING_MASK) |
9196 | term->charset_list = coding_charset_list (terminal_coding); | |
9197 | else | |
6b4bb703 | 9198 | term->charset_list = Fcons (make_number (charset_ascii), Qnil); |
4ed46869 KH |
9199 | return Qnil; |
9200 | } | |
9201 | ||
c4825358 KH |
9202 | DEFUN ("set-safe-terminal-coding-system-internal", |
9203 | Fset_safe_terminal_coding_system_internal, | |
48b0f3ae | 9204 | Sset_safe_terminal_coding_system_internal, 1, 1, 0, |
ddb67bdc | 9205 | doc: /* Internal use only. */) |
5842a27b | 9206 | (Lisp_Object coding_system) |
d46c5b12 | 9207 | { |
b7826503 | 9208 | CHECK_SYMBOL (coding_system); |
c4825358 KH |
9209 | setup_coding_system (Fcheck_coding_system (coding_system), |
9210 | &safe_terminal_coding); | |
ad1746f5 | 9211 | /* Character composition should be disabled. */ |
df7492f9 | 9212 | safe_terminal_coding.common_flags &= ~CODING_ANNOTATE_COMPOSITION_MASK; |
b73bfc1c KH |
9213 | safe_terminal_coding.src_multibyte = 1; |
9214 | safe_terminal_coding.dst_multibyte = 0; | |
c4825358 KH |
9215 | return Qnil; |
9216 | } | |
4ed46869 | 9217 | |
002fdb44 | 9218 | DEFUN ("terminal-coding-system", Fterminal_coding_system, |
68bba4e4 | 9219 | Sterminal_coding_system, 0, 1, 0, |
6ed8eeff | 9220 | doc: /* Return coding system specified for terminal output on the given terminal. |
708e05dc | 9221 | TERMINAL may be a terminal object, a frame, or nil for the selected |
6ed8eeff | 9222 | frame's terminal device. */) |
5842a27b | 9223 | (Lisp_Object terminal) |
4ed46869 | 9224 | { |
985773c9 MB |
9225 | struct coding_system *terminal_coding |
9226 | = TERMINAL_TERMINAL_CODING (get_terminal (terminal, 1)); | |
9227 | Lisp_Object coding_system = CODING_ID_NAME (terminal_coding->id); | |
ae6f73fa | 9228 | |
ae6f73fa | 9229 | /* For backward compatibility, return nil if it is `undecided'. */ |
f75c90a9 | 9230 | return (! EQ (coding_system, Qundecided) ? coding_system : Qnil); |
4ed46869 KH |
9231 | } |
9232 | ||
002fdb44 | 9233 | DEFUN ("set-keyboard-coding-system-internal", Fset_keyboard_coding_system_internal, |
68bba4e4 | 9234 | Sset_keyboard_coding_system_internal, 1, 2, 0, |
48b0f3ae | 9235 | doc: /* Internal use only. */) |
5842a27b | 9236 | (Lisp_Object coding_system, Lisp_Object terminal) |
4ed46869 | 9237 | { |
6ed8eeff | 9238 | struct terminal *t = get_terminal (terminal, 1); |
b7826503 | 9239 | CHECK_SYMBOL (coding_system); |
624bda09 KH |
9240 | if (NILP (coding_system)) |
9241 | coding_system = Qno_conversion; | |
9242 | else | |
9243 | Fcheck_coding_system (coding_system); | |
9244 | setup_coding_system (coding_system, TERMINAL_KEYBOARD_CODING (t)); | |
ad1746f5 | 9245 | /* Character composition should be disabled. */ |
c73bd236 MB |
9246 | TERMINAL_KEYBOARD_CODING (t)->common_flags |
9247 | &= ~CODING_ANNOTATE_COMPOSITION_MASK; | |
4ed46869 KH |
9248 | return Qnil; |
9249 | } | |
9250 | ||
9251 | DEFUN ("keyboard-coding-system", | |
985773c9 | 9252 | Fkeyboard_coding_system, Skeyboard_coding_system, 0, 1, 0, |
48b0f3ae | 9253 | doc: /* Return coding system specified for decoding keyboard input. */) |
5842a27b | 9254 | (Lisp_Object terminal) |
4ed46869 | 9255 | { |
985773c9 MB |
9256 | return CODING_ID_NAME (TERMINAL_KEYBOARD_CODING |
9257 | (get_terminal (terminal, 1))->id); | |
4ed46869 KH |
9258 | } |
9259 | ||
4ed46869 | 9260 | \f |
a5d301df KH |
9261 | DEFUN ("find-operation-coding-system", Ffind_operation_coding_system, |
9262 | Sfind_operation_coding_system, 1, MANY, 0, | |
48b0f3ae PJ |
9263 | doc: /* Choose a coding system for an operation based on the target name. |
9264 | The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM). | |
9265 | DECODING-SYSTEM is the coding system to use for decoding | |
9266 | \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system | |
9267 | for encoding (in case OPERATION does encoding). | |
05e6f5dc | 9268 | |
48b0f3ae PJ |
9269 | The first argument OPERATION specifies an I/O primitive: |
9270 | For file I/O, `insert-file-contents' or `write-region'. | |
9271 | For process I/O, `call-process', `call-process-region', or `start-process'. | |
9272 | For network I/O, `open-network-stream'. | |
05e6f5dc | 9273 | |
48b0f3ae PJ |
9274 | The remaining arguments should be the same arguments that were passed |
9275 | to the primitive. Depending on which primitive, one of those arguments | |
9276 | is selected as the TARGET. For example, if OPERATION does file I/O, | |
9277 | whichever argument specifies the file name is TARGET. | |
05e6f5dc | 9278 | |
48b0f3ae | 9279 | TARGET has a meaning which depends on OPERATION: |
b883cdb2 | 9280 | For file I/O, TARGET is a file name (except for the special case below). |
48b0f3ae | 9281 | For process I/O, TARGET is a process name. |
d4a1d553 | 9282 | For network I/O, TARGET is a service name or a port number. |
05e6f5dc | 9283 | |
d4a1d553 | 9284 | This function looks up what is specified for TARGET in |
48b0f3ae PJ |
9285 | `file-coding-system-alist', `process-coding-system-alist', |
9286 | or `network-coding-system-alist' depending on OPERATION. | |
9287 | They may specify a coding system, a cons of coding systems, | |
9288 | or a function symbol to call. | |
9289 | In the last case, we call the function with one argument, | |
9290 | which is a list of all the arguments given to this function. | |
1011c487 MB |
9291 | If the function can't decide a coding system, it can return |
9292 | `undecided' so that the normal code-detection is performed. | |
48b0f3ae | 9293 | |
b883cdb2 MB |
9294 | If OPERATION is `insert-file-contents', the argument corresponding to |
9295 | TARGET may be a cons (FILENAME . BUFFER). In that case, FILENAME is a | |
9296 | file name to look up, and BUFFER is a buffer that contains the file's | |
9297 | contents (not yet decoded). If `file-coding-system-alist' specifies a | |
9298 | function to call for FILENAME, that function should examine the | |
9299 | contents of BUFFER instead of reading the file. | |
9300 | ||
d918f936 | 9301 | usage: (find-operation-coding-system OPERATION ARGUMENTS...) */) |
c5101a77 | 9302 | (size_t nargs, Lisp_Object *args) |
6b89e3aa | 9303 | { |
4ed46869 KH |
9304 | Lisp_Object operation, target_idx, target, val; |
9305 | register Lisp_Object chain; | |
177c0ea7 | 9306 | |
4ed46869 KH |
9307 | if (nargs < 2) |
9308 | error ("Too few arguments"); | |
9309 | operation = args[0]; | |
9310 | if (!SYMBOLP (operation) | |
c5101a77 | 9311 | || !NATNUMP (target_idx = Fget (operation, Qtarget_idx))) |
3ed051d4 | 9312 | error ("Invalid first argument"); |
c5101a77 | 9313 | if (nargs < 1 + XFASTINT (target_idx)) |
4ed46869 | 9314 | error ("Too few arguments for operation: %s", |
8f924df7 | 9315 | SDATA (SYMBOL_NAME (operation))); |
c5101a77 | 9316 | target = args[XFASTINT (target_idx) + 1]; |
4ed46869 | 9317 | if (!(STRINGP (target) |
091a0ff0 KH |
9318 | || (EQ (operation, Qinsert_file_contents) && CONSP (target) |
9319 | && STRINGP (XCAR (target)) && BUFFERP (XCDR (target))) | |
4ed46869 | 9320 | || (EQ (operation, Qopen_network_stream) && INTEGERP (target)))) |
c5101a77 | 9321 | error ("Invalid %dth argument", XFASTINT (target_idx) + 1); |
091a0ff0 KH |
9322 | if (CONSP (target)) |
9323 | target = XCAR (target); | |
4ed46869 | 9324 | |
2e34157c RS |
9325 | chain = ((EQ (operation, Qinsert_file_contents) |
9326 | || EQ (operation, Qwrite_region)) | |
02ba4723 | 9327 | ? Vfile_coding_system_alist |
2e34157c | 9328 | : (EQ (operation, Qopen_network_stream) |
02ba4723 KH |
9329 | ? Vnetwork_coding_system_alist |
9330 | : Vprocess_coding_system_alist)); | |
4ed46869 KH |
9331 | if (NILP (chain)) |
9332 | return Qnil; | |
9333 | ||
03699b14 | 9334 | for (; CONSP (chain); chain = XCDR (chain)) |
6b89e3aa | 9335 | { |
f44d27ce | 9336 | Lisp_Object elt; |
6b89e3aa | 9337 | |
df7492f9 | 9338 | elt = XCAR (chain); |
4ed46869 KH |
9339 | if (CONSP (elt) |
9340 | && ((STRINGP (target) | |
03699b14 KR |
9341 | && STRINGP (XCAR (elt)) |
9342 | && fast_string_match (XCAR (elt), target) >= 0) | |
9343 | || (INTEGERP (target) && EQ (target, XCAR (elt))))) | |
6b89e3aa | 9344 | { |
03699b14 | 9345 | val = XCDR (elt); |
b19fd4c5 KH |
9346 | /* Here, if VAL is both a valid coding system and a valid |
9347 | function symbol, we return VAL as a coding system. */ | |
02ba4723 KH |
9348 | if (CONSP (val)) |
9349 | return val; | |
9350 | if (! SYMBOLP (val)) | |
9351 | return Qnil; | |
9352 | if (! NILP (Fcoding_system_p (val))) | |
9353 | return Fcons (val, val); | |
b19fd4c5 | 9354 | if (! NILP (Ffboundp (val))) |
6b89e3aa | 9355 | { |
e2b97060 MB |
9356 | /* We use call1 rather than safe_call1 |
9357 | so as to get bug reports about functions called here | |
9358 | which don't handle the current interface. */ | |
9359 | val = call1 (val, Flist (nargs, args)); | |
b19fd4c5 KH |
9360 | if (CONSP (val)) |
9361 | return val; | |
9362 | if (SYMBOLP (val) && ! NILP (Fcoding_system_p (val))) | |
9363 | return Fcons (val, val); | |
6b89e3aa | 9364 | } |
02ba4723 | 9365 | return Qnil; |
6b89e3aa KH |
9366 | } |
9367 | } | |
4ed46869 | 9368 | return Qnil; |
6b89e3aa KH |
9369 | } |
9370 | ||
df7492f9 | 9371 | DEFUN ("set-coding-system-priority", Fset_coding_system_priority, |
a3f6ee6d | 9372 | Sset_coding_system_priority, 0, MANY, 0, |
da7db224 | 9373 | doc: /* Assign higher priority to the coding systems given as arguments. |
d4a1d553 | 9374 | If multiple coding systems belong to the same category, |
a3181084 DL |
9375 | all but the first one are ignored. |
9376 | ||
d4a1d553 | 9377 | usage: (set-coding-system-priority &rest coding-systems) */) |
c5101a77 | 9378 | (size_t nargs, Lisp_Object *args) |
df7492f9 | 9379 | { |
c5101a77 | 9380 | size_t i, j; |
df7492f9 KH |
9381 | int changed[coding_category_max]; |
9382 | enum coding_category priorities[coding_category_max]; | |
9383 | ||
72af86bd | 9384 | memset (changed, 0, sizeof changed); |
6b89e3aa | 9385 | |
df7492f9 | 9386 | for (i = j = 0; i < nargs; i++) |
6b89e3aa | 9387 | { |
df7492f9 KH |
9388 | enum coding_category category; |
9389 | Lisp_Object spec, attrs; | |
6b89e3aa | 9390 | |
df7492f9 KH |
9391 | CHECK_CODING_SYSTEM_GET_SPEC (args[i], spec); |
9392 | attrs = AREF (spec, 0); | |
9393 | category = XINT (CODING_ATTR_CATEGORY (attrs)); | |
9394 | if (changed[category]) | |
9395 | /* Ignore this coding system because a coding system of the | |
9396 | same category already had a higher priority. */ | |
9397 | continue; | |
9398 | changed[category] = 1; | |
9399 | priorities[j++] = category; | |
9400 | if (coding_categories[category].id >= 0 | |
9401 | && ! EQ (args[i], CODING_ID_NAME (coding_categories[category].id))) | |
9402 | setup_coding_system (args[i], &coding_categories[category]); | |
ff563fce | 9403 | Fset (AREF (Vcoding_category_table, category), args[i]); |
df7492f9 | 9404 | } |
6b89e3aa | 9405 | |
df7492f9 KH |
9406 | /* Now we have decided top J priorities. Reflect the order of the |
9407 | original priorities to the remaining priorities. */ | |
6b89e3aa | 9408 | |
df7492f9 | 9409 | for (i = j, j = 0; i < coding_category_max; i++, j++) |
6b89e3aa | 9410 | { |
df7492f9 KH |
9411 | while (j < coding_category_max |
9412 | && changed[coding_priorities[j]]) | |
9413 | j++; | |
9414 | if (j == coding_category_max) | |
9415 | abort (); | |
9416 | priorities[i] = coding_priorities[j]; | |
9417 | } | |
6b89e3aa | 9418 | |
72af86bd | 9419 | memcpy (coding_priorities, priorities, sizeof priorities); |
177c0ea7 | 9420 | |
ff563fce KH |
9421 | /* Update `coding-category-list'. */ |
9422 | Vcoding_category_list = Qnil; | |
c5101a77 | 9423 | for (i = coding_category_max; i-- > 0; ) |
ff563fce KH |
9424 | Vcoding_category_list |
9425 | = Fcons (AREF (Vcoding_category_table, priorities[i]), | |
9426 | Vcoding_category_list); | |
6b89e3aa | 9427 | |
df7492f9 | 9428 | return Qnil; |
6b89e3aa KH |
9429 | } |
9430 | ||
df7492f9 KH |
9431 | DEFUN ("coding-system-priority-list", Fcoding_system_priority_list, |
9432 | Scoding_system_priority_list, 0, 1, 0, | |
da7db224 | 9433 | doc: /* Return a list of coding systems ordered by their priorities. |
b811c52b KH |
9434 | The list contains a subset of coding systems; i.e. coding systems |
9435 | assigned to each coding category (see `coding-category-list'). | |
9436 | ||
da7db224 | 9437 | HIGHESTP non-nil means just return the highest priority one. */) |
5842a27b | 9438 | (Lisp_Object highestp) |
d46c5b12 KH |
9439 | { |
9440 | int i; | |
df7492f9 | 9441 | Lisp_Object val; |
6b89e3aa | 9442 | |
df7492f9 | 9443 | for (i = 0, val = Qnil; i < coding_category_max; i++) |
d46c5b12 | 9444 | { |
df7492f9 KH |
9445 | enum coding_category category = coding_priorities[i]; |
9446 | int id = coding_categories[category].id; | |
9447 | Lisp_Object attrs; | |
068a9dbd | 9448 | |
df7492f9 KH |
9449 | if (id < 0) |
9450 | continue; | |
9451 | attrs = CODING_ID_ATTRS (id); | |
9452 | if (! NILP (highestp)) | |
9453 | return CODING_ATTR_BASE_NAME (attrs); | |
9454 | val = Fcons (CODING_ATTR_BASE_NAME (attrs), val); | |
9455 | } | |
9456 | return Fnreverse (val); | |
9457 | } | |
068a9dbd | 9458 | |
91433552 | 9459 | static const char *const suffixes[] = { "-unix", "-dos", "-mac" }; |
068a9dbd KH |
9460 | |
9461 | static Lisp_Object | |
971de7fb | 9462 | make_subsidiaries (Lisp_Object base) |
068a9dbd | 9463 | { |
df7492f9 | 9464 | Lisp_Object subsidiaries; |
8f924df7 | 9465 | int base_name_len = SBYTES (SYMBOL_NAME (base)); |
df7492f9 KH |
9466 | char *buf = (char *) alloca (base_name_len + 6); |
9467 | int i; | |
068a9dbd | 9468 | |
72af86bd | 9469 | memcpy (buf, SDATA (SYMBOL_NAME (base)), base_name_len); |
df7492f9 KH |
9470 | subsidiaries = Fmake_vector (make_number (3), Qnil); |
9471 | for (i = 0; i < 3; i++) | |
068a9dbd | 9472 | { |
72af86bd | 9473 | memcpy (buf + base_name_len, suffixes[i], strlen (suffixes[i]) + 1); |
df7492f9 | 9474 | ASET (subsidiaries, i, intern (buf)); |
068a9dbd | 9475 | } |
df7492f9 | 9476 | return subsidiaries; |
068a9dbd KH |
9477 | } |
9478 | ||
9479 | ||
df7492f9 KH |
9480 | DEFUN ("define-coding-system-internal", Fdefine_coding_system_internal, |
9481 | Sdefine_coding_system_internal, coding_arg_max, MANY, 0, | |
1fcd6c8b DL |
9482 | doc: /* For internal use only. |
9483 | usage: (define-coding-system-internal ...) */) | |
c5101a77 | 9484 | (size_t nargs, Lisp_Object *args) |
068a9dbd | 9485 | { |
df7492f9 KH |
9486 | Lisp_Object name; |
9487 | Lisp_Object spec_vec; /* [ ATTRS ALIASE EOL_TYPE ] */ | |
9488 | Lisp_Object attrs; /* Vector of attributes. */ | |
9489 | Lisp_Object eol_type; | |
9490 | Lisp_Object aliases; | |
9491 | Lisp_Object coding_type, charset_list, safe_charsets; | |
9492 | enum coding_category category; | |
9493 | Lisp_Object tail, val; | |
9494 | int max_charset_id = 0; | |
9495 | int i; | |
068a9dbd | 9496 | |
df7492f9 KH |
9497 | if (nargs < coding_arg_max) |
9498 | goto short_args; | |
068a9dbd | 9499 | |
df7492f9 | 9500 | attrs = Fmake_vector (make_number (coding_attr_last_index), Qnil); |
068a9dbd | 9501 | |
df7492f9 KH |
9502 | name = args[coding_arg_name]; |
9503 | CHECK_SYMBOL (name); | |
9504 | CODING_ATTR_BASE_NAME (attrs) = name; | |
068a9dbd | 9505 | |
df7492f9 KH |
9506 | val = args[coding_arg_mnemonic]; |
9507 | if (! STRINGP (val)) | |
9508 | CHECK_CHARACTER (val); | |
9509 | CODING_ATTR_MNEMONIC (attrs) = val; | |
068a9dbd | 9510 | |
df7492f9 KH |
9511 | coding_type = args[coding_arg_coding_type]; |
9512 | CHECK_SYMBOL (coding_type); | |
9513 | CODING_ATTR_TYPE (attrs) = coding_type; | |
068a9dbd | 9514 | |
df7492f9 KH |
9515 | charset_list = args[coding_arg_charset_list]; |
9516 | if (SYMBOLP (charset_list)) | |
9517 | { | |
9518 | if (EQ (charset_list, Qiso_2022)) | |
9519 | { | |
9520 | if (! EQ (coding_type, Qiso_2022)) | |
9521 | error ("Invalid charset-list"); | |
9522 | charset_list = Viso_2022_charset_list; | |
9523 | } | |
9524 | else if (EQ (charset_list, Qemacs_mule)) | |
9525 | { | |
9526 | if (! EQ (coding_type, Qemacs_mule)) | |
9527 | error ("Invalid charset-list"); | |
9528 | charset_list = Vemacs_mule_charset_list; | |
9529 | } | |
9530 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) | |
9531 | if (max_charset_id < XFASTINT (XCAR (tail))) | |
9532 | max_charset_id = XFASTINT (XCAR (tail)); | |
9533 | } | |
068a9dbd KH |
9534 | else |
9535 | { | |
df7492f9 | 9536 | charset_list = Fcopy_sequence (charset_list); |
985773c9 | 9537 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) |
068a9dbd | 9538 | { |
df7492f9 KH |
9539 | struct charset *charset; |
9540 | ||
985773c9 | 9541 | val = XCAR (tail); |
df7492f9 KH |
9542 | CHECK_CHARSET_GET_CHARSET (val, charset); |
9543 | if (EQ (coding_type, Qiso_2022) | |
9544 | ? CHARSET_ISO_FINAL (charset) < 0 | |
9545 | : EQ (coding_type, Qemacs_mule) | |
9546 | ? CHARSET_EMACS_MULE_ID (charset) < 0 | |
9547 | : 0) | |
9548 | error ("Can't handle charset `%s'", | |
8f924df7 | 9549 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
df7492f9 | 9550 | |
8f924df7 | 9551 | XSETCAR (tail, make_number (charset->id)); |
df7492f9 KH |
9552 | if (max_charset_id < charset->id) |
9553 | max_charset_id = charset->id; | |
068a9dbd KH |
9554 | } |
9555 | } | |
df7492f9 | 9556 | CODING_ATTR_CHARSET_LIST (attrs) = charset_list; |
068a9dbd | 9557 | |
1b3b981b AS |
9558 | safe_charsets = make_uninit_string (max_charset_id + 1); |
9559 | memset (SDATA (safe_charsets), 255, max_charset_id + 1); | |
df7492f9 | 9560 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) |
8f924df7 | 9561 | SSET (safe_charsets, XFASTINT (XCAR (tail)), 0); |
df7492f9 | 9562 | CODING_ATTR_SAFE_CHARSETS (attrs) = safe_charsets; |
068a9dbd | 9563 | |
584948ac | 9564 | CODING_ATTR_ASCII_COMPAT (attrs) = args[coding_arg_ascii_compatible_p]; |
3a73fa5d | 9565 | |
df7492f9 | 9566 | val = args[coding_arg_decode_translation_table]; |
a6f87d34 | 9567 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) |
7d64c6ad | 9568 | CHECK_SYMBOL (val); |
df7492f9 | 9569 | CODING_ATTR_DECODE_TBL (attrs) = val; |
3a73fa5d | 9570 | |
df7492f9 | 9571 | val = args[coding_arg_encode_translation_table]; |
a6f87d34 | 9572 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) |
7d64c6ad | 9573 | CHECK_SYMBOL (val); |
df7492f9 | 9574 | CODING_ATTR_ENCODE_TBL (attrs) = val; |
d46c5b12 | 9575 | |
df7492f9 KH |
9576 | val = args[coding_arg_post_read_conversion]; |
9577 | CHECK_SYMBOL (val); | |
9578 | CODING_ATTR_POST_READ (attrs) = val; | |
d46c5b12 | 9579 | |
df7492f9 KH |
9580 | val = args[coding_arg_pre_write_conversion]; |
9581 | CHECK_SYMBOL (val); | |
9582 | CODING_ATTR_PRE_WRITE (attrs) = val; | |
3a73fa5d | 9583 | |
df7492f9 KH |
9584 | val = args[coding_arg_default_char]; |
9585 | if (NILP (val)) | |
9586 | CODING_ATTR_DEFAULT_CHAR (attrs) = make_number (' '); | |
9587 | else | |
9588 | { | |
8f924df7 | 9589 | CHECK_CHARACTER (val); |
df7492f9 KH |
9590 | CODING_ATTR_DEFAULT_CHAR (attrs) = val; |
9591 | } | |
4031e2bf | 9592 | |
8f924df7 KH |
9593 | val = args[coding_arg_for_unibyte]; |
9594 | CODING_ATTR_FOR_UNIBYTE (attrs) = NILP (val) ? Qnil : Qt; | |
3a73fa5d | 9595 | |
df7492f9 KH |
9596 | val = args[coding_arg_plist]; |
9597 | CHECK_LIST (val); | |
9598 | CODING_ATTR_PLIST (attrs) = val; | |
3a73fa5d | 9599 | |
df7492f9 KH |
9600 | if (EQ (coding_type, Qcharset)) |
9601 | { | |
c7c66a95 KH |
9602 | /* Generate a lisp vector of 256 elements. Each element is nil, |
9603 | integer, or a list of charset IDs. | |
3a73fa5d | 9604 | |
c7c66a95 KH |
9605 | If Nth element is nil, the byte code N is invalid in this |
9606 | coding system. | |
4ed46869 | 9607 | |
c7c66a95 KH |
9608 | If Nth element is a number NUM, N is the first byte of a |
9609 | charset whose ID is NUM. | |
4ed46869 | 9610 | |
c7c66a95 KH |
9611 | If Nth element is a list of charset IDs, N is the first byte |
9612 | of one of them. The list is sorted by dimensions of the | |
ad1746f5 | 9613 | charsets. A charset of smaller dimension comes first. */ |
df7492f9 | 9614 | val = Fmake_vector (make_number (256), Qnil); |
4ed46869 | 9615 | |
5c99c2e6 | 9616 | for (tail = charset_list; CONSP (tail); tail = XCDR (tail)) |
df7492f9 | 9617 | { |
c7c66a95 KH |
9618 | struct charset *charset = CHARSET_FROM_ID (XFASTINT (XCAR (tail))); |
9619 | int dim = CHARSET_DIMENSION (charset); | |
9620 | int idx = (dim - 1) * 4; | |
4ed46869 | 9621 | |
5c99c2e6 | 9622 | if (CHARSET_ASCII_COMPATIBLE_P (charset)) |
584948ac | 9623 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; |
4031e2bf | 9624 | |
15d143f7 KH |
9625 | for (i = charset->code_space[idx]; |
9626 | i <= charset->code_space[idx + 1]; i++) | |
9627 | { | |
c7c66a95 KH |
9628 | Lisp_Object tmp, tmp2; |
9629 | int dim2; | |
ec6d2bb8 | 9630 | |
c7c66a95 KH |
9631 | tmp = AREF (val, i); |
9632 | if (NILP (tmp)) | |
9633 | tmp = XCAR (tail); | |
9634 | else if (NUMBERP (tmp)) | |
9635 | { | |
9636 | dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (tmp))); | |
9637 | if (dim < dim2) | |
c7c66a95 | 9638 | tmp = Fcons (XCAR (tail), Fcons (tmp, Qnil)); |
f9d71dcd KH |
9639 | else |
9640 | tmp = Fcons (tmp, Fcons (XCAR (tail), Qnil)); | |
c7c66a95 | 9641 | } |
15d143f7 | 9642 | else |
c7c66a95 KH |
9643 | { |
9644 | for (tmp2 = tmp; CONSP (tmp2); tmp2 = XCDR (tmp2)) | |
9645 | { | |
9646 | dim2 = CHARSET_DIMENSION (CHARSET_FROM_ID (XFASTINT (XCAR (tmp2)))); | |
9647 | if (dim < dim2) | |
9648 | break; | |
9649 | } | |
9650 | if (NILP (tmp2)) | |
9651 | tmp = nconc2 (tmp, Fcons (XCAR (tail), Qnil)); | |
9652 | else | |
9653 | { | |
9654 | XSETCDR (tmp2, Fcons (XCAR (tmp2), XCDR (tmp2))); | |
9655 | XSETCAR (tmp2, XCAR (tail)); | |
9656 | } | |
9657 | } | |
9658 | ASET (val, i, tmp); | |
15d143f7 | 9659 | } |
df7492f9 KH |
9660 | } |
9661 | ASET (attrs, coding_attr_charset_valids, val); | |
9662 | category = coding_category_charset; | |
9663 | } | |
9664 | else if (EQ (coding_type, Qccl)) | |
9665 | { | |
9666 | Lisp_Object valids; | |
ecec61c1 | 9667 | |
df7492f9 KH |
9668 | if (nargs < coding_arg_ccl_max) |
9669 | goto short_args; | |
ecec61c1 | 9670 | |
df7492f9 KH |
9671 | val = args[coding_arg_ccl_decoder]; |
9672 | CHECK_CCL_PROGRAM (val); | |
9673 | if (VECTORP (val)) | |
9674 | val = Fcopy_sequence (val); | |
9675 | ASET (attrs, coding_attr_ccl_decoder, val); | |
ecec61c1 | 9676 | |
df7492f9 KH |
9677 | val = args[coding_arg_ccl_encoder]; |
9678 | CHECK_CCL_PROGRAM (val); | |
9679 | if (VECTORP (val)) | |
9680 | val = Fcopy_sequence (val); | |
9681 | ASET (attrs, coding_attr_ccl_encoder, val); | |
ecec61c1 | 9682 | |
df7492f9 KH |
9683 | val = args[coding_arg_ccl_valids]; |
9684 | valids = Fmake_string (make_number (256), make_number (0)); | |
9685 | for (tail = val; !NILP (tail); tail = Fcdr (tail)) | |
9686 | { | |
8dcbea82 | 9687 | int from, to; |
ecec61c1 | 9688 | |
df7492f9 KH |
9689 | val = Fcar (tail); |
9690 | if (INTEGERP (val)) | |
8dcbea82 KH |
9691 | { |
9692 | from = to = XINT (val); | |
9693 | if (from < 0 || from > 255) | |
9694 | args_out_of_range_3 (val, make_number (0), make_number (255)); | |
9695 | } | |
df7492f9 KH |
9696 | else |
9697 | { | |
df7492f9 | 9698 | CHECK_CONS (val); |
8f924df7 KH |
9699 | CHECK_NATNUM_CAR (val); |
9700 | CHECK_NATNUM_CDR (val); | |
df7492f9 | 9701 | from = XINT (XCAR (val)); |
8f924df7 | 9702 | if (from > 255) |
8dcbea82 KH |
9703 | args_out_of_range_3 (XCAR (val), |
9704 | make_number (0), make_number (255)); | |
df7492f9 | 9705 | to = XINT (XCDR (val)); |
8dcbea82 KH |
9706 | if (to < from || to > 255) |
9707 | args_out_of_range_3 (XCDR (val), | |
9708 | XCAR (val), make_number (255)); | |
df7492f9 | 9709 | } |
8dcbea82 | 9710 | for (i = from; i <= to; i++) |
8f924df7 | 9711 | SSET (valids, i, 1); |
df7492f9 KH |
9712 | } |
9713 | ASET (attrs, coding_attr_ccl_valids, valids); | |
4ed46869 | 9714 | |
df7492f9 | 9715 | category = coding_category_ccl; |
55ab7be3 | 9716 | } |
df7492f9 | 9717 | else if (EQ (coding_type, Qutf_16)) |
55ab7be3 | 9718 | { |
df7492f9 | 9719 | Lisp_Object bom, endian; |
4ed46869 | 9720 | |
584948ac | 9721 | CODING_ATTR_ASCII_COMPAT (attrs) = Qnil; |
4ed46869 | 9722 | |
df7492f9 KH |
9723 | if (nargs < coding_arg_utf16_max) |
9724 | goto short_args; | |
4ed46869 | 9725 | |
df7492f9 KH |
9726 | bom = args[coding_arg_utf16_bom]; |
9727 | if (! NILP (bom) && ! EQ (bom, Qt)) | |
9728 | { | |
9729 | CHECK_CONS (bom); | |
8f924df7 KH |
9730 | val = XCAR (bom); |
9731 | CHECK_CODING_SYSTEM (val); | |
9732 | val = XCDR (bom); | |
9733 | CHECK_CODING_SYSTEM (val); | |
df7492f9 | 9734 | } |
a470d443 | 9735 | ASET (attrs, coding_attr_utf_bom, bom); |
df7492f9 KH |
9736 | |
9737 | endian = args[coding_arg_utf16_endian]; | |
b49a1807 KH |
9738 | CHECK_SYMBOL (endian); |
9739 | if (NILP (endian)) | |
9740 | endian = Qbig; | |
9741 | else if (! EQ (endian, Qbig) && ! EQ (endian, Qlittle)) | |
8f924df7 | 9742 | error ("Invalid endian: %s", SDATA (SYMBOL_NAME (endian))); |
df7492f9 KH |
9743 | ASET (attrs, coding_attr_utf_16_endian, endian); |
9744 | ||
9745 | category = (CONSP (bom) | |
9746 | ? coding_category_utf_16_auto | |
9747 | : NILP (bom) | |
b49a1807 | 9748 | ? (EQ (endian, Qbig) |
df7492f9 KH |
9749 | ? coding_category_utf_16_be_nosig |
9750 | : coding_category_utf_16_le_nosig) | |
b49a1807 | 9751 | : (EQ (endian, Qbig) |
df7492f9 KH |
9752 | ? coding_category_utf_16_be |
9753 | : coding_category_utf_16_le)); | |
9754 | } | |
9755 | else if (EQ (coding_type, Qiso_2022)) | |
9756 | { | |
9757 | Lisp_Object initial, reg_usage, request, flags; | |
1397dc18 | 9758 | |
df7492f9 KH |
9759 | if (nargs < coding_arg_iso2022_max) |
9760 | goto short_args; | |
9761 | ||
9762 | initial = Fcopy_sequence (args[coding_arg_iso2022_initial]); | |
9763 | CHECK_VECTOR (initial); | |
9764 | for (i = 0; i < 4; i++) | |
9765 | { | |
9766 | val = Faref (initial, make_number (i)); | |
9767 | if (! NILP (val)) | |
9768 | { | |
584948ac KH |
9769 | struct charset *charset; |
9770 | ||
9771 | CHECK_CHARSET_GET_CHARSET (val, charset); | |
9772 | ASET (initial, i, make_number (CHARSET_ID (charset))); | |
9773 | if (i == 0 && CHARSET_ASCII_COMPATIBLE_P (charset)) | |
9774 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; | |
df7492f9 KH |
9775 | } |
9776 | else | |
9777 | ASET (initial, i, make_number (-1)); | |
9778 | } | |
9779 | ||
9780 | reg_usage = args[coding_arg_iso2022_reg_usage]; | |
9781 | CHECK_CONS (reg_usage); | |
8f924df7 KH |
9782 | CHECK_NUMBER_CAR (reg_usage); |
9783 | CHECK_NUMBER_CDR (reg_usage); | |
df7492f9 KH |
9784 | |
9785 | request = Fcopy_sequence (args[coding_arg_iso2022_request]); | |
9786 | for (tail = request; ! NILP (tail); tail = Fcdr (tail)) | |
1397dc18 | 9787 | { |
df7492f9 | 9788 | int id; |
2735d060 | 9789 | Lisp_Object tmp1; |
df7492f9 KH |
9790 | |
9791 | val = Fcar (tail); | |
9792 | CHECK_CONS (val); | |
2735d060 PE |
9793 | tmp1 = XCAR (val); |
9794 | CHECK_CHARSET_GET_ID (tmp1, id); | |
8f924df7 | 9795 | CHECK_NATNUM_CDR (val); |
df7492f9 KH |
9796 | if (XINT (XCDR (val)) >= 4) |
9797 | error ("Invalid graphic register number: %d", XINT (XCDR (val))); | |
8f924df7 | 9798 | XSETCAR (val, make_number (id)); |
1397dc18 | 9799 | } |
4ed46869 | 9800 | |
df7492f9 KH |
9801 | flags = args[coding_arg_iso2022_flags]; |
9802 | CHECK_NATNUM (flags); | |
9803 | i = XINT (flags); | |
9804 | if (EQ (args[coding_arg_charset_list], Qiso_2022)) | |
9805 | flags = make_number (i | CODING_ISO_FLAG_FULL_SUPPORT); | |
9806 | ||
9807 | ASET (attrs, coding_attr_iso_initial, initial); | |
9808 | ASET (attrs, coding_attr_iso_usage, reg_usage); | |
9809 | ASET (attrs, coding_attr_iso_request, request); | |
9810 | ASET (attrs, coding_attr_iso_flags, flags); | |
9811 | setup_iso_safe_charsets (attrs); | |
9812 | ||
9813 | if (i & CODING_ISO_FLAG_SEVEN_BITS) | |
9814 | category = ((i & (CODING_ISO_FLAG_LOCKING_SHIFT | |
9815 | | CODING_ISO_FLAG_SINGLE_SHIFT)) | |
9816 | ? coding_category_iso_7_else | |
9817 | : EQ (args[coding_arg_charset_list], Qiso_2022) | |
9818 | ? coding_category_iso_7 | |
9819 | : coding_category_iso_7_tight); | |
9820 | else | |
9821 | { | |
9822 | int id = XINT (AREF (initial, 1)); | |
9823 | ||
c6fb6e98 | 9824 | category = (((i & CODING_ISO_FLAG_LOCKING_SHIFT) |
df7492f9 KH |
9825 | || EQ (args[coding_arg_charset_list], Qiso_2022) |
9826 | || id < 0) | |
9827 | ? coding_category_iso_8_else | |
9828 | : (CHARSET_DIMENSION (CHARSET_FROM_ID (id)) == 1) | |
9829 | ? coding_category_iso_8_1 | |
9830 | : coding_category_iso_8_2); | |
9831 | } | |
0ce7886f KH |
9832 | if (category != coding_category_iso_8_1 |
9833 | && category != coding_category_iso_8_2) | |
9834 | CODING_ATTR_ASCII_COMPAT (attrs) = Qnil; | |
df7492f9 KH |
9835 | } |
9836 | else if (EQ (coding_type, Qemacs_mule)) | |
c28a9453 | 9837 | { |
df7492f9 KH |
9838 | if (EQ (args[coding_arg_charset_list], Qemacs_mule)) |
9839 | ASET (attrs, coding_attr_emacs_mule_full, Qt); | |
584948ac | 9840 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; |
df7492f9 | 9841 | category = coding_category_emacs_mule; |
c28a9453 | 9842 | } |
df7492f9 | 9843 | else if (EQ (coding_type, Qshift_jis)) |
c28a9453 | 9844 | { |
df7492f9 KH |
9845 | |
9846 | struct charset *charset; | |
9847 | ||
7d64c6ad | 9848 | if (XINT (Flength (charset_list)) != 3 |
6e07c25f | 9849 | && XINT (Flength (charset_list)) != 4) |
7d64c6ad | 9850 | error ("There should be three or four charsets"); |
df7492f9 KH |
9851 | |
9852 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
9853 | if (CHARSET_DIMENSION (charset) != 1) | |
9854 | error ("Dimension of charset %s is not one", | |
8f924df7 | 9855 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
584948ac KH |
9856 | if (CHARSET_ASCII_COMPATIBLE_P (charset)) |
9857 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; | |
df7492f9 KH |
9858 | |
9859 | charset_list = XCDR (charset_list); | |
9860 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
9861 | if (CHARSET_DIMENSION (charset) != 1) | |
9862 | error ("Dimension of charset %s is not one", | |
8f924df7 | 9863 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
df7492f9 KH |
9864 | |
9865 | charset_list = XCDR (charset_list); | |
9866 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
9867 | if (CHARSET_DIMENSION (charset) != 2) | |
7d64c6ad KH |
9868 | error ("Dimension of charset %s is not two", |
9869 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); | |
9870 | ||
9871 | charset_list = XCDR (charset_list); | |
2b917a06 KH |
9872 | if (! NILP (charset_list)) |
9873 | { | |
9874 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
9875 | if (CHARSET_DIMENSION (charset) != 2) | |
9876 | error ("Dimension of charset %s is not two", | |
9877 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); | |
9878 | } | |
df7492f9 KH |
9879 | |
9880 | category = coding_category_sjis; | |
9881 | Vsjis_coding_system = name; | |
c28a9453 | 9882 | } |
df7492f9 KH |
9883 | else if (EQ (coding_type, Qbig5)) |
9884 | { | |
9885 | struct charset *charset; | |
4ed46869 | 9886 | |
df7492f9 KH |
9887 | if (XINT (Flength (charset_list)) != 2) |
9888 | error ("There should be just two charsets"); | |
9889 | ||
9890 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
9891 | if (CHARSET_DIMENSION (charset) != 1) | |
9892 | error ("Dimension of charset %s is not one", | |
8f924df7 | 9893 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
584948ac KH |
9894 | if (CHARSET_ASCII_COMPATIBLE_P (charset)) |
9895 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; | |
df7492f9 KH |
9896 | |
9897 | charset_list = XCDR (charset_list); | |
9898 | charset = CHARSET_FROM_ID (XINT (XCAR (charset_list))); | |
9899 | if (CHARSET_DIMENSION (charset) != 2) | |
9900 | error ("Dimension of charset %s is not two", | |
8f924df7 | 9901 | SDATA (SYMBOL_NAME (CHARSET_NAME (charset)))); |
4ed46869 | 9902 | |
df7492f9 KH |
9903 | category = coding_category_big5; |
9904 | Vbig5_coding_system = name; | |
9905 | } | |
9906 | else if (EQ (coding_type, Qraw_text)) | |
c28a9453 | 9907 | { |
584948ac KH |
9908 | category = coding_category_raw_text; |
9909 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; | |
c28a9453 | 9910 | } |
df7492f9 | 9911 | else if (EQ (coding_type, Qutf_8)) |
4ed46869 | 9912 | { |
a470d443 KH |
9913 | Lisp_Object bom; |
9914 | ||
584948ac | 9915 | CODING_ATTR_ASCII_COMPAT (attrs) = Qt; |
a470d443 KH |
9916 | |
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); | |
9930 | ||
9931 | category = (CONSP (bom) ? coding_category_utf_8_auto | |
9932 | : NILP (bom) ? coding_category_utf_8_nosig | |
9933 | : coding_category_utf_8_sig); | |
4ed46869 | 9934 | } |
df7492f9 KH |
9935 | else if (EQ (coding_type, Qundecided)) |
9936 | category = coding_category_undecided; | |
4ed46869 | 9937 | else |
df7492f9 | 9938 | error ("Invalid coding system type: %s", |
8f924df7 | 9939 | SDATA (SYMBOL_NAME (coding_type))); |
4ed46869 | 9940 | |
df7492f9 | 9941 | CODING_ATTR_CATEGORY (attrs) = make_number (category); |
01378f49 KH |
9942 | CODING_ATTR_PLIST (attrs) |
9943 | = Fcons (QCcategory, Fcons (AREF (Vcoding_category_table, category), | |
9944 | CODING_ATTR_PLIST (attrs))); | |
35befdaa | 9945 | CODING_ATTR_PLIST (attrs) |
3ed051d4 | 9946 | = Fcons (QCascii_compatible_p, |
35befdaa KH |
9947 | Fcons (CODING_ATTR_ASCII_COMPAT (attrs), |
9948 | CODING_ATTR_PLIST (attrs))); | |
c4825358 | 9949 | |
df7492f9 KH |
9950 | eol_type = args[coding_arg_eol_type]; |
9951 | if (! NILP (eol_type) | |
9952 | && ! EQ (eol_type, Qunix) | |
9953 | && ! EQ (eol_type, Qdos) | |
9954 | && ! EQ (eol_type, Qmac)) | |
9955 | error ("Invalid eol-type"); | |
4ed46869 | 9956 | |
df7492f9 | 9957 | aliases = Fcons (name, Qnil); |
4ed46869 | 9958 | |
df7492f9 KH |
9959 | if (NILP (eol_type)) |
9960 | { | |
9961 | eol_type = make_subsidiaries (name); | |
9962 | for (i = 0; i < 3; i++) | |
1397dc18 | 9963 | { |
df7492f9 KH |
9964 | Lisp_Object this_spec, this_name, this_aliases, this_eol_type; |
9965 | ||
9966 | this_name = AREF (eol_type, i); | |
9967 | this_aliases = Fcons (this_name, Qnil); | |
9968 | this_eol_type = (i == 0 ? Qunix : i == 1 ? Qdos : Qmac); | |
9969 | this_spec = Fmake_vector (make_number (3), attrs); | |
9970 | ASET (this_spec, 1, this_aliases); | |
9971 | ASET (this_spec, 2, this_eol_type); | |
9972 | Fputhash (this_name, this_spec, Vcoding_system_hash_table); | |
9973 | Vcoding_system_list = Fcons (this_name, Vcoding_system_list); | |
583f71ca KH |
9974 | val = Fassoc (Fsymbol_name (this_name), Vcoding_system_alist); |
9975 | if (NILP (val)) | |
9976 | Vcoding_system_alist | |
9977 | = Fcons (Fcons (Fsymbol_name (this_name), Qnil), | |
9978 | Vcoding_system_alist); | |
1397dc18 | 9979 | } |
d46c5b12 | 9980 | } |
4ed46869 | 9981 | |
df7492f9 KH |
9982 | spec_vec = Fmake_vector (make_number (3), attrs); |
9983 | ASET (spec_vec, 1, aliases); | |
9984 | ASET (spec_vec, 2, eol_type); | |
48b0f3ae | 9985 | |
df7492f9 KH |
9986 | Fputhash (name, spec_vec, Vcoding_system_hash_table); |
9987 | Vcoding_system_list = Fcons (name, Vcoding_system_list); | |
583f71ca KH |
9988 | val = Fassoc (Fsymbol_name (name), Vcoding_system_alist); |
9989 | if (NILP (val)) | |
9990 | Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (name), Qnil), | |
9991 | Vcoding_system_alist); | |
48b0f3ae | 9992 | |
df7492f9 KH |
9993 | { |
9994 | int id = coding_categories[category].id; | |
48b0f3ae | 9995 | |
df7492f9 KH |
9996 | if (id < 0 || EQ (name, CODING_ID_NAME (id))) |
9997 | setup_coding_system (name, &coding_categories[category]); | |
9998 | } | |
48b0f3ae | 9999 | |
d46c5b12 | 10000 | return Qnil; |
48b0f3ae | 10001 | |
df7492f9 KH |
10002 | short_args: |
10003 | return Fsignal (Qwrong_number_of_arguments, | |
10004 | Fcons (intern ("define-coding-system-internal"), | |
10005 | make_number (nargs))); | |
d46c5b12 | 10006 | } |
4ed46869 | 10007 | |
d6925f38 | 10008 | |
a6f87d34 KH |
10009 | DEFUN ("coding-system-put", Fcoding_system_put, Scoding_system_put, |
10010 | 3, 3, 0, | |
10011 | doc: /* Change value in CODING-SYSTEM's property list PROP to VAL. */) | |
5842a27b | 10012 | (Lisp_Object coding_system, Lisp_Object prop, Lisp_Object val) |
a6f87d34 | 10013 | { |
3dbe7859 | 10014 | Lisp_Object spec, attrs; |
a6f87d34 KH |
10015 | |
10016 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
10017 | attrs = AREF (spec, 0); | |
10018 | if (EQ (prop, QCmnemonic)) | |
10019 | { | |
10020 | if (! STRINGP (val)) | |
10021 | CHECK_CHARACTER (val); | |
10022 | CODING_ATTR_MNEMONIC (attrs) = val; | |
10023 | } | |
2133e2d1 | 10024 | else if (EQ (prop, QCdefault_char)) |
a6f87d34 KH |
10025 | { |
10026 | if (NILP (val)) | |
10027 | val = make_number (' '); | |
10028 | else | |
10029 | CHECK_CHARACTER (val); | |
10030 | CODING_ATTR_DEFAULT_CHAR (attrs) = val; | |
10031 | } | |
10032 | else if (EQ (prop, QCdecode_translation_table)) | |
10033 | { | |
10034 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) | |
10035 | CHECK_SYMBOL (val); | |
10036 | CODING_ATTR_DECODE_TBL (attrs) = val; | |
10037 | } | |
10038 | else if (EQ (prop, QCencode_translation_table)) | |
10039 | { | |
10040 | if (! CHAR_TABLE_P (val) && ! CONSP (val)) | |
10041 | CHECK_SYMBOL (val); | |
10042 | CODING_ATTR_ENCODE_TBL (attrs) = val; | |
10043 | } | |
10044 | else if (EQ (prop, QCpost_read_conversion)) | |
10045 | { | |
10046 | CHECK_SYMBOL (val); | |
10047 | CODING_ATTR_POST_READ (attrs) = val; | |
10048 | } | |
10049 | else if (EQ (prop, QCpre_write_conversion)) | |
10050 | { | |
10051 | CHECK_SYMBOL (val); | |
10052 | CODING_ATTR_PRE_WRITE (attrs) = val; | |
10053 | } | |
35befdaa KH |
10054 | else if (EQ (prop, QCascii_compatible_p)) |
10055 | { | |
10056 | CODING_ATTR_ASCII_COMPAT (attrs) = val; | |
10057 | } | |
a6f87d34 KH |
10058 | |
10059 | CODING_ATTR_PLIST (attrs) | |
10060 | = Fplist_put (CODING_ATTR_PLIST (attrs), prop, val); | |
10061 | return val; | |
10062 | } | |
10063 | ||
10064 | ||
df7492f9 KH |
10065 | DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias, |
10066 | Sdefine_coding_system_alias, 2, 2, 0, | |
10067 | doc: /* Define ALIAS as an alias for CODING-SYSTEM. */) | |
5842a27b | 10068 | (Lisp_Object alias, Lisp_Object coding_system) |
66cfb530 | 10069 | { |
583f71ca | 10070 | Lisp_Object spec, aliases, eol_type, val; |
4ed46869 | 10071 | |
df7492f9 KH |
10072 | CHECK_SYMBOL (alias); |
10073 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
10074 | aliases = AREF (spec, 1); | |
d4a1d553 | 10075 | /* ALIASES should be a list of length more than zero, and the first |
d6925f38 KH |
10076 | element is a base coding system. Append ALIAS at the tail of the |
10077 | list. */ | |
df7492f9 KH |
10078 | while (!NILP (XCDR (aliases))) |
10079 | aliases = XCDR (aliases); | |
8f924df7 | 10080 | XSETCDR (aliases, Fcons (alias, Qnil)); |
4ed46869 | 10081 | |
df7492f9 KH |
10082 | eol_type = AREF (spec, 2); |
10083 | if (VECTORP (eol_type)) | |
4ed46869 | 10084 | { |
df7492f9 KH |
10085 | Lisp_Object subsidiaries; |
10086 | int i; | |
4ed46869 | 10087 | |
df7492f9 KH |
10088 | subsidiaries = make_subsidiaries (alias); |
10089 | for (i = 0; i < 3; i++) | |
10090 | Fdefine_coding_system_alias (AREF (subsidiaries, i), | |
10091 | AREF (eol_type, i)); | |
4ed46869 | 10092 | } |
df7492f9 KH |
10093 | |
10094 | Fputhash (alias, spec, Vcoding_system_hash_table); | |
d6925f38 | 10095 | Vcoding_system_list = Fcons (alias, Vcoding_system_list); |
583f71ca KH |
10096 | val = Fassoc (Fsymbol_name (alias), Vcoding_system_alist); |
10097 | if (NILP (val)) | |
10098 | Vcoding_system_alist = Fcons (Fcons (Fsymbol_name (alias), Qnil), | |
10099 | Vcoding_system_alist); | |
66cfb530 | 10100 | |
4ed46869 KH |
10101 | return Qnil; |
10102 | } | |
10103 | ||
df7492f9 KH |
10104 | DEFUN ("coding-system-base", Fcoding_system_base, Scoding_system_base, |
10105 | 1, 1, 0, | |
10106 | doc: /* Return the base of CODING-SYSTEM. | |
da7db224 | 10107 | Any alias or subsidiary coding system is not a base coding system. */) |
5842a27b | 10108 | (Lisp_Object coding_system) |
d46c5b12 | 10109 | { |
df7492f9 | 10110 | Lisp_Object spec, attrs; |
d46c5b12 | 10111 | |
df7492f9 KH |
10112 | if (NILP (coding_system)) |
10113 | return (Qno_conversion); | |
10114 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
10115 | attrs = AREF (spec, 0); | |
10116 | return CODING_ATTR_BASE_NAME (attrs); | |
10117 | } | |
1397dc18 | 10118 | |
df7492f9 KH |
10119 | DEFUN ("coding-system-plist", Fcoding_system_plist, Scoding_system_plist, |
10120 | 1, 1, 0, | |
10121 | doc: "Return the property list of CODING-SYSTEM.") | |
5842a27b | 10122 | (Lisp_Object coding_system) |
df7492f9 KH |
10123 | { |
10124 | Lisp_Object spec, attrs; | |
1397dc18 | 10125 | |
df7492f9 KH |
10126 | if (NILP (coding_system)) |
10127 | coding_system = Qno_conversion; | |
10128 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
10129 | attrs = AREF (spec, 0); | |
10130 | return CODING_ATTR_PLIST (attrs); | |
d46c5b12 KH |
10131 | } |
10132 | ||
df7492f9 KH |
10133 | |
10134 | DEFUN ("coding-system-aliases", Fcoding_system_aliases, Scoding_system_aliases, | |
10135 | 1, 1, 0, | |
da7db224 | 10136 | doc: /* Return the list of aliases of CODING-SYSTEM. */) |
5842a27b | 10137 | (Lisp_Object coding_system) |
66cfb530 | 10138 | { |
df7492f9 | 10139 | Lisp_Object spec; |
84d60297 | 10140 | |
df7492f9 KH |
10141 | if (NILP (coding_system)) |
10142 | coding_system = Qno_conversion; | |
10143 | CHECK_CODING_SYSTEM_GET_SPEC (coding_system, spec); | |
da7db224 | 10144 | return AREF (spec, 1); |
df7492f9 | 10145 | } |
66cfb530 | 10146 | |
df7492f9 KH |
10147 | DEFUN ("coding-system-eol-type", Fcoding_system_eol_type, |
10148 | Scoding_system_eol_type, 1, 1, 0, | |
10149 | doc: /* Return eol-type of CODING-SYSTEM. | |
d4a1d553 | 10150 | An eol-type is an integer 0, 1, 2, or a vector of coding systems. |
66cfb530 | 10151 | |
df7492f9 KH |
10152 | Integer values 0, 1, and 2 indicate a format of end-of-line; LF, CRLF, |
10153 | and CR respectively. | |
66cfb530 | 10154 | |
df7492f9 KH |
10155 | A vector value indicates that a format of end-of-line should be |
10156 | detected automatically. Nth element of the vector is the subsidiary | |
10157 | coding system whose eol-type is N. */) | |
5842a27b | 10158 | (Lisp_Object coding_system) |
6b89e3aa | 10159 | { |
df7492f9 KH |
10160 | Lisp_Object spec, eol_type; |
10161 | int n; | |
6b89e3aa | 10162 | |
df7492f9 KH |
10163 | if (NILP (coding_system)) |
10164 | coding_system = Qno_conversion; | |
10165 | if (! CODING_SYSTEM_P (coding_system)) | |
10166 | return Qnil; | |
10167 | spec = CODING_SYSTEM_SPEC (coding_system); | |
10168 | eol_type = AREF (spec, 2); | |
10169 | if (VECTORP (eol_type)) | |
10170 | return Fcopy_sequence (eol_type); | |
10171 | n = EQ (eol_type, Qunix) ? 0 : EQ (eol_type, Qdos) ? 1 : 2; | |
10172 | return make_number (n); | |
6b89e3aa KH |
10173 | } |
10174 | ||
4ed46869 KH |
10175 | #endif /* emacs */ |
10176 | ||
10177 | \f | |
1397dc18 | 10178 | /*** 9. Post-amble ***/ |
4ed46869 | 10179 | |
dfcf069d | 10180 | void |
971de7fb | 10181 | init_coding_once (void) |
4ed46869 KH |
10182 | { |
10183 | int i; | |
10184 | ||
df7492f9 KH |
10185 | for (i = 0; i < coding_category_max; i++) |
10186 | { | |
10187 | coding_categories[i].id = -1; | |
10188 | coding_priorities[i] = i; | |
10189 | } | |
4ed46869 KH |
10190 | |
10191 | /* ISO2022 specific initialize routine. */ | |
10192 | for (i = 0; i < 0x20; i++) | |
b73bfc1c | 10193 | iso_code_class[i] = ISO_control_0; |
4ed46869 KH |
10194 | for (i = 0x21; i < 0x7F; i++) |
10195 | iso_code_class[i] = ISO_graphic_plane_0; | |
10196 | for (i = 0x80; i < 0xA0; i++) | |
b73bfc1c | 10197 | iso_code_class[i] = ISO_control_1; |
4ed46869 KH |
10198 | for (i = 0xA1; i < 0xFF; i++) |
10199 | iso_code_class[i] = ISO_graphic_plane_1; | |
10200 | iso_code_class[0x20] = iso_code_class[0x7F] = ISO_0x20_or_0x7F; | |
10201 | iso_code_class[0xA0] = iso_code_class[0xFF] = ISO_0xA0_or_0xFF; | |
4ed46869 KH |
10202 | iso_code_class[ISO_CODE_SO] = ISO_shift_out; |
10203 | iso_code_class[ISO_CODE_SI] = ISO_shift_in; | |
10204 | iso_code_class[ISO_CODE_SS2_7] = ISO_single_shift_2_7; | |
10205 | iso_code_class[ISO_CODE_ESC] = ISO_escape; | |
10206 | iso_code_class[ISO_CODE_SS2] = ISO_single_shift_2; | |
10207 | iso_code_class[ISO_CODE_SS3] = ISO_single_shift_3; | |
10208 | iso_code_class[ISO_CODE_CSI] = ISO_control_sequence_introducer; | |
10209 | ||
df7492f9 KH |
10210 | for (i = 0; i < 256; i++) |
10211 | { | |
10212 | emacs_mule_bytes[i] = 1; | |
10213 | } | |
7c78e542 KH |
10214 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_11] = 3; |
10215 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_12] = 3; | |
10216 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_21] = 4; | |
10217 | emacs_mule_bytes[EMACS_MULE_LEADING_CODE_PRIVATE_22] = 4; | |
e0e989f6 KH |
10218 | } |
10219 | ||
10220 | #ifdef emacs | |
10221 | ||
dfcf069d | 10222 | void |
971de7fb | 10223 | syms_of_coding (void) |
e0e989f6 | 10224 | { |
df7492f9 | 10225 | staticpro (&Vcoding_system_hash_table); |
8f924df7 KH |
10226 | { |
10227 | Lisp_Object args[2]; | |
10228 | args[0] = QCtest; | |
10229 | args[1] = Qeq; | |
10230 | Vcoding_system_hash_table = Fmake_hash_table (2, args); | |
10231 | } | |
df7492f9 KH |
10232 | |
10233 | staticpro (&Vsjis_coding_system); | |
10234 | Vsjis_coding_system = Qnil; | |
e0e989f6 | 10235 | |
df7492f9 KH |
10236 | staticpro (&Vbig5_coding_system); |
10237 | Vbig5_coding_system = Qnil; | |
10238 | ||
24a73b0a KH |
10239 | staticpro (&Vcode_conversion_reused_workbuf); |
10240 | Vcode_conversion_reused_workbuf = Qnil; | |
10241 | ||
10242 | staticpro (&Vcode_conversion_workbuf_name); | |
d67b4f80 | 10243 | Vcode_conversion_workbuf_name = make_pure_c_string (" *code-conversion-work*"); |
e0e989f6 | 10244 | |
24a73b0a | 10245 | reused_workbuf_in_use = 0; |
df7492f9 KH |
10246 | |
10247 | DEFSYM (Qcharset, "charset"); | |
10248 | DEFSYM (Qtarget_idx, "target-idx"); | |
10249 | DEFSYM (Qcoding_system_history, "coding-system-history"); | |
bb0115a2 RS |
10250 | Fset (Qcoding_system_history, Qnil); |
10251 | ||
9ce27fde | 10252 | /* Target FILENAME is the first argument. */ |
e0e989f6 | 10253 | Fput (Qinsert_file_contents, Qtarget_idx, make_number (0)); |
9ce27fde | 10254 | /* Target FILENAME is the third argument. */ |
e0e989f6 KH |
10255 | Fput (Qwrite_region, Qtarget_idx, make_number (2)); |
10256 | ||
df7492f9 | 10257 | DEFSYM (Qcall_process, "call-process"); |
9ce27fde | 10258 | /* Target PROGRAM is the first argument. */ |
e0e989f6 KH |
10259 | Fput (Qcall_process, Qtarget_idx, make_number (0)); |
10260 | ||
df7492f9 | 10261 | DEFSYM (Qcall_process_region, "call-process-region"); |
9ce27fde | 10262 | /* Target PROGRAM is the third argument. */ |
e0e989f6 KH |
10263 | Fput (Qcall_process_region, Qtarget_idx, make_number (2)); |
10264 | ||
df7492f9 | 10265 | DEFSYM (Qstart_process, "start-process"); |
9ce27fde | 10266 | /* Target PROGRAM is the third argument. */ |
e0e989f6 KH |
10267 | Fput (Qstart_process, Qtarget_idx, make_number (2)); |
10268 | ||
df7492f9 | 10269 | DEFSYM (Qopen_network_stream, "open-network-stream"); |
9ce27fde | 10270 | /* Target SERVICE is the fourth argument. */ |
e0e989f6 KH |
10271 | Fput (Qopen_network_stream, Qtarget_idx, make_number (3)); |
10272 | ||
df7492f9 KH |
10273 | DEFSYM (Qcoding_system, "coding-system"); |
10274 | DEFSYM (Qcoding_aliases, "coding-aliases"); | |
4ed46869 | 10275 | |
df7492f9 KH |
10276 | DEFSYM (Qeol_type, "eol-type"); |
10277 | DEFSYM (Qunix, "unix"); | |
10278 | DEFSYM (Qdos, "dos"); | |
4ed46869 | 10279 | |
df7492f9 KH |
10280 | DEFSYM (Qbuffer_file_coding_system, "buffer-file-coding-system"); |
10281 | DEFSYM (Qpost_read_conversion, "post-read-conversion"); | |
10282 | DEFSYM (Qpre_write_conversion, "pre-write-conversion"); | |
10283 | DEFSYM (Qdefault_char, "default-char"); | |
10284 | DEFSYM (Qundecided, "undecided"); | |
10285 | DEFSYM (Qno_conversion, "no-conversion"); | |
10286 | DEFSYM (Qraw_text, "raw-text"); | |
4ed46869 | 10287 | |
df7492f9 | 10288 | DEFSYM (Qiso_2022, "iso-2022"); |
4ed46869 | 10289 | |
df7492f9 | 10290 | DEFSYM (Qutf_8, "utf-8"); |
8f924df7 | 10291 | DEFSYM (Qutf_8_emacs, "utf-8-emacs"); |
27901516 | 10292 | |
df7492f9 | 10293 | DEFSYM (Qutf_16, "utf-16"); |
df7492f9 KH |
10294 | DEFSYM (Qbig, "big"); |
10295 | DEFSYM (Qlittle, "little"); | |
27901516 | 10296 | |
df7492f9 KH |
10297 | DEFSYM (Qshift_jis, "shift-jis"); |
10298 | DEFSYM (Qbig5, "big5"); | |
4ed46869 | 10299 | |
df7492f9 | 10300 | DEFSYM (Qcoding_system_p, "coding-system-p"); |
4ed46869 | 10301 | |
df7492f9 | 10302 | DEFSYM (Qcoding_system_error, "coding-system-error"); |
4ed46869 | 10303 | Fput (Qcoding_system_error, Qerror_conditions, |
d67b4f80 | 10304 | pure_cons (Qcoding_system_error, pure_cons (Qerror, Qnil))); |
4ed46869 | 10305 | Fput (Qcoding_system_error, Qerror_message, |
d67b4f80 | 10306 | make_pure_c_string ("Invalid coding system")); |
4ed46869 | 10307 | |
05e6f5dc KH |
10308 | /* Intern this now in case it isn't already done. |
10309 | Setting this variable twice is harmless. | |
10310 | But don't staticpro it here--that is done in alloc.c. */ | |
d67b4f80 | 10311 | Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots"); |
70c22245 | 10312 | |
df7492f9 | 10313 | DEFSYM (Qtranslation_table, "translation-table"); |
433f7f87 | 10314 | Fput (Qtranslation_table, Qchar_table_extra_slots, make_number (2)); |
df7492f9 KH |
10315 | DEFSYM (Qtranslation_table_id, "translation-table-id"); |
10316 | DEFSYM (Qtranslation_table_for_decode, "translation-table-for-decode"); | |
10317 | DEFSYM (Qtranslation_table_for_encode, "translation-table-for-encode"); | |
1397dc18 | 10318 | |
df7492f9 | 10319 | DEFSYM (Qvalid_codes, "valid-codes"); |
9ce27fde | 10320 | |
df7492f9 | 10321 | DEFSYM (Qemacs_mule, "emacs-mule"); |
d46c5b12 | 10322 | |
01378f49 | 10323 | DEFSYM (QCcategory, ":category"); |
a6f87d34 | 10324 | DEFSYM (QCmnemonic, ":mnemonic"); |
2133e2d1 | 10325 | DEFSYM (QCdefault_char, ":default-char"); |
a6f87d34 KH |
10326 | DEFSYM (QCdecode_translation_table, ":decode-translation-table"); |
10327 | DEFSYM (QCencode_translation_table, ":encode-translation-table"); | |
10328 | DEFSYM (QCpost_read_conversion, ":post-read-conversion"); | |
10329 | DEFSYM (QCpre_write_conversion, ":pre-write-conversion"); | |
35befdaa | 10330 | DEFSYM (QCascii_compatible_p, ":ascii-compatible-p"); |
01378f49 | 10331 | |
df7492f9 KH |
10332 | Vcoding_category_table |
10333 | = Fmake_vector (make_number (coding_category_max), Qnil); | |
10334 | staticpro (&Vcoding_category_table); | |
10335 | /* Followings are target of code detection. */ | |
10336 | ASET (Vcoding_category_table, coding_category_iso_7, | |
d67b4f80 | 10337 | intern_c_string ("coding-category-iso-7")); |
df7492f9 | 10338 | ASET (Vcoding_category_table, coding_category_iso_7_tight, |
d67b4f80 | 10339 | intern_c_string ("coding-category-iso-7-tight")); |
df7492f9 | 10340 | ASET (Vcoding_category_table, coding_category_iso_8_1, |
d67b4f80 | 10341 | intern_c_string ("coding-category-iso-8-1")); |
df7492f9 | 10342 | ASET (Vcoding_category_table, coding_category_iso_8_2, |
d67b4f80 | 10343 | intern_c_string ("coding-category-iso-8-2")); |
df7492f9 | 10344 | ASET (Vcoding_category_table, coding_category_iso_7_else, |
d67b4f80 | 10345 | intern_c_string ("coding-category-iso-7-else")); |
df7492f9 | 10346 | ASET (Vcoding_category_table, coding_category_iso_8_else, |
d67b4f80 | 10347 | intern_c_string ("coding-category-iso-8-else")); |
a470d443 | 10348 | ASET (Vcoding_category_table, coding_category_utf_8_auto, |
d67b4f80 | 10349 | intern_c_string ("coding-category-utf-8-auto")); |
a470d443 | 10350 | ASET (Vcoding_category_table, coding_category_utf_8_nosig, |
d67b4f80 | 10351 | intern_c_string ("coding-category-utf-8")); |
a470d443 | 10352 | ASET (Vcoding_category_table, coding_category_utf_8_sig, |
d67b4f80 | 10353 | intern_c_string ("coding-category-utf-8-sig")); |
df7492f9 | 10354 | ASET (Vcoding_category_table, coding_category_utf_16_be, |
d67b4f80 | 10355 | intern_c_string ("coding-category-utf-16-be")); |
ff563fce | 10356 | ASET (Vcoding_category_table, coding_category_utf_16_auto, |
d67b4f80 | 10357 | intern_c_string ("coding-category-utf-16-auto")); |
df7492f9 | 10358 | ASET (Vcoding_category_table, coding_category_utf_16_le, |
d67b4f80 | 10359 | intern_c_string ("coding-category-utf-16-le")); |
df7492f9 | 10360 | ASET (Vcoding_category_table, coding_category_utf_16_be_nosig, |
d67b4f80 | 10361 | intern_c_string ("coding-category-utf-16-be-nosig")); |
df7492f9 | 10362 | ASET (Vcoding_category_table, coding_category_utf_16_le_nosig, |
d67b4f80 | 10363 | intern_c_string ("coding-category-utf-16-le-nosig")); |
df7492f9 | 10364 | ASET (Vcoding_category_table, coding_category_charset, |
d67b4f80 | 10365 | intern_c_string ("coding-category-charset")); |
df7492f9 | 10366 | ASET (Vcoding_category_table, coding_category_sjis, |
d67b4f80 | 10367 | intern_c_string ("coding-category-sjis")); |
df7492f9 | 10368 | ASET (Vcoding_category_table, coding_category_big5, |
d67b4f80 | 10369 | intern_c_string ("coding-category-big5")); |
df7492f9 | 10370 | ASET (Vcoding_category_table, coding_category_ccl, |
d67b4f80 | 10371 | intern_c_string ("coding-category-ccl")); |
df7492f9 | 10372 | ASET (Vcoding_category_table, coding_category_emacs_mule, |
d67b4f80 | 10373 | intern_c_string ("coding-category-emacs-mule")); |
df7492f9 KH |
10374 | /* Followings are NOT target of code detection. */ |
10375 | ASET (Vcoding_category_table, coding_category_raw_text, | |
d67b4f80 | 10376 | intern_c_string ("coding-category-raw-text")); |
df7492f9 | 10377 | ASET (Vcoding_category_table, coding_category_undecided, |
d67b4f80 | 10378 | intern_c_string ("coding-category-undecided")); |
ecf488bc | 10379 | |
065e3595 KH |
10380 | DEFSYM (Qinsufficient_source, "insufficient-source"); |
10381 | DEFSYM (Qinconsistent_eol, "inconsistent-eol"); | |
10382 | DEFSYM (Qinvalid_source, "invalid-source"); | |
10383 | DEFSYM (Qinterrupted, "interrupted"); | |
10384 | DEFSYM (Qinsufficient_memory, "insufficient-memory"); | |
44e8490d | 10385 | DEFSYM (Qcoding_system_define_form, "coding-system-define-form"); |
065e3595 | 10386 | |
4ed46869 KH |
10387 | defsubr (&Scoding_system_p); |
10388 | defsubr (&Sread_coding_system); | |
10389 | defsubr (&Sread_non_nil_coding_system); | |
10390 | defsubr (&Scheck_coding_system); | |
10391 | defsubr (&Sdetect_coding_region); | |
d46c5b12 | 10392 | defsubr (&Sdetect_coding_string); |
05e6f5dc | 10393 | defsubr (&Sfind_coding_systems_region_internal); |
068a9dbd | 10394 | defsubr (&Sunencodable_char_position); |
df7492f9 | 10395 | defsubr (&Scheck_coding_systems_region); |
4ed46869 KH |
10396 | defsubr (&Sdecode_coding_region); |
10397 | defsubr (&Sencode_coding_region); | |
10398 | defsubr (&Sdecode_coding_string); | |
10399 | defsubr (&Sencode_coding_string); | |
10400 | defsubr (&Sdecode_sjis_char); | |
10401 | defsubr (&Sencode_sjis_char); | |
10402 | defsubr (&Sdecode_big5_char); | |
10403 | defsubr (&Sencode_big5_char); | |
1ba9e4ab | 10404 | defsubr (&Sset_terminal_coding_system_internal); |
c4825358 | 10405 | defsubr (&Sset_safe_terminal_coding_system_internal); |
4ed46869 | 10406 | defsubr (&Sterminal_coding_system); |
1ba9e4ab | 10407 | defsubr (&Sset_keyboard_coding_system_internal); |
4ed46869 | 10408 | defsubr (&Skeyboard_coding_system); |
a5d301df | 10409 | defsubr (&Sfind_operation_coding_system); |
df7492f9 | 10410 | defsubr (&Sset_coding_system_priority); |
6b89e3aa | 10411 | defsubr (&Sdefine_coding_system_internal); |
df7492f9 | 10412 | defsubr (&Sdefine_coding_system_alias); |
a6f87d34 | 10413 | defsubr (&Scoding_system_put); |
df7492f9 KH |
10414 | defsubr (&Scoding_system_base); |
10415 | defsubr (&Scoding_system_plist); | |
10416 | defsubr (&Scoding_system_aliases); | |
10417 | defsubr (&Scoding_system_eol_type); | |
10418 | defsubr (&Scoding_system_priority_list); | |
4ed46869 | 10419 | |
29208e82 | 10420 | DEFVAR_LISP ("coding-system-list", Vcoding_system_list, |
48b0f3ae PJ |
10421 | doc: /* List of coding systems. |
10422 | ||
10423 | Do not alter the value of this variable manually. This variable should be | |
df7492f9 | 10424 | updated by the functions `define-coding-system' and |
48b0f3ae | 10425 | `define-coding-system-alias'. */); |
4608c386 KH |
10426 | Vcoding_system_list = Qnil; |
10427 | ||
29208e82 | 10428 | DEFVAR_LISP ("coding-system-alist", Vcoding_system_alist, |
48b0f3ae PJ |
10429 | doc: /* Alist of coding system names. |
10430 | Each element is one element list of coding system name. | |
446dcd75 | 10431 | This variable is given to `completing-read' as COLLECTION argument. |
48b0f3ae PJ |
10432 | |
10433 | Do not alter the value of this variable manually. This variable should be | |
10434 | updated by the functions `make-coding-system' and | |
10435 | `define-coding-system-alias'. */); | |
4608c386 KH |
10436 | Vcoding_system_alist = Qnil; |
10437 | ||
29208e82 | 10438 | DEFVAR_LISP ("coding-category-list", Vcoding_category_list, |
48b0f3ae PJ |
10439 | doc: /* List of coding-categories (symbols) ordered by priority. |
10440 | ||
10441 | On detecting a coding system, Emacs tries code detection algorithms | |
10442 | associated with each coding-category one by one in this order. When | |
10443 | one algorithm agrees with a byte sequence of source text, the coding | |
0ec31faf KH |
10444 | system bound to the corresponding coding-category is selected. |
10445 | ||
448e17d6 | 10446 | Don't modify this variable directly, but use `set-coding-system-priority'. */); |
4ed46869 KH |
10447 | { |
10448 | int i; | |
10449 | ||
10450 | Vcoding_category_list = Qnil; | |
df7492f9 | 10451 | for (i = coding_category_max - 1; i >= 0; i--) |
4ed46869 | 10452 | Vcoding_category_list |
d46c5b12 KH |
10453 | = Fcons (XVECTOR (Vcoding_category_table)->contents[i], |
10454 | Vcoding_category_list); | |
4ed46869 KH |
10455 | } |
10456 | ||
29208e82 | 10457 | DEFVAR_LISP ("coding-system-for-read", Vcoding_system_for_read, |
48b0f3ae PJ |
10458 | doc: /* Specify the coding system for read operations. |
10459 | It is useful to bind this variable with `let', but do not set it globally. | |
10460 | If the value is a coding system, it is used for decoding on read operation. | |
446dcd75 JB |
10461 | If not, an appropriate element is used from one of the coding system alists. |
10462 | There are three such tables: `file-coding-system-alist', | |
48b0f3ae | 10463 | `process-coding-system-alist', and `network-coding-system-alist'. */); |
4ed46869 KH |
10464 | Vcoding_system_for_read = Qnil; |
10465 | ||
29208e82 | 10466 | DEFVAR_LISP ("coding-system-for-write", Vcoding_system_for_write, |
48b0f3ae PJ |
10467 | doc: /* Specify the coding system for write operations. |
10468 | Programs bind this variable with `let', but you should not set it globally. | |
10469 | If the value is a coding system, it is used for encoding of output, | |
10470 | when writing it to a file and when sending it to a file or subprocess. | |
10471 | ||
10472 | If this does not specify a coding system, an appropriate element | |
446dcd75 JB |
10473 | is used from one of the coding system alists. |
10474 | There are three such tables: `file-coding-system-alist', | |
48b0f3ae PJ |
10475 | `process-coding-system-alist', and `network-coding-system-alist'. |
10476 | For output to files, if the above procedure does not specify a coding system, | |
10477 | the value of `buffer-file-coding-system' is used. */); | |
4ed46869 KH |
10478 | Vcoding_system_for_write = Qnil; |
10479 | ||
29208e82 | 10480 | DEFVAR_LISP ("last-coding-system-used", Vlast_coding_system_used, |
df7492f9 KH |
10481 | doc: /* |
10482 | Coding system used in the latest file or process I/O. */); | |
4ed46869 KH |
10483 | Vlast_coding_system_used = Qnil; |
10484 | ||
29208e82 | 10485 | DEFVAR_LISP ("last-code-conversion-error", Vlast_code_conversion_error, |
065e3595 KH |
10486 | doc: /* |
10487 | Error status of the last code conversion. | |
10488 | ||
10489 | When an error was detected in the last code conversion, this variable | |
10490 | is set to one of the following symbols. | |
10491 | `insufficient-source' | |
10492 | `inconsistent-eol' | |
10493 | `invalid-source' | |
10494 | `interrupted' | |
10495 | `insufficient-memory' | |
10496 | When no error was detected, the value doesn't change. So, to check | |
10497 | the error status of a code conversion by this variable, you must | |
10498 | explicitly set this variable to nil before performing code | |
10499 | conversion. */); | |
10500 | Vlast_code_conversion_error = Qnil; | |
10501 | ||
29208e82 | 10502 | DEFVAR_BOOL ("inhibit-eol-conversion", inhibit_eol_conversion, |
df7492f9 KH |
10503 | doc: /* |
10504 | *Non-nil means always inhibit code conversion of end-of-line format. | |
48b0f3ae PJ |
10505 | See info node `Coding Systems' and info node `Text and Binary' concerning |
10506 | such conversion. */); | |
9ce27fde KH |
10507 | inhibit_eol_conversion = 0; |
10508 | ||
29208e82 | 10509 | DEFVAR_BOOL ("inherit-process-coding-system", inherit_process_coding_system, |
df7492f9 KH |
10510 | doc: /* |
10511 | Non-nil means process buffer inherits coding system of process output. | |
48b0f3ae PJ |
10512 | Bind it to t if the process output is to be treated as if it were a file |
10513 | read from some filesystem. */); | |
ed29121d EZ |
10514 | inherit_process_coding_system = 0; |
10515 | ||
29208e82 | 10516 | DEFVAR_LISP ("file-coding-system-alist", Vfile_coding_system_alist, |
df7492f9 KH |
10517 | doc: /* |
10518 | Alist to decide a coding system to use for a file I/O operation. | |
48b0f3ae PJ |
10519 | The format is ((PATTERN . VAL) ...), |
10520 | where PATTERN is a regular expression matching a file name, | |
10521 | VAL is a coding system, a cons of coding systems, or a function symbol. | |
10522 | If VAL is a coding system, it is used for both decoding and encoding | |
10523 | the file contents. | |
10524 | If VAL is a cons of coding systems, the car part is used for decoding, | |
10525 | and the cdr part is used for encoding. | |
10526 | If VAL is a function symbol, the function must return a coding system | |
2c53e699 KH |
10527 | or a cons of coding systems which are used as above. The function is |
10528 | called with an argument that is a list of the arguments with which | |
5a0bbd9a KH |
10529 | `find-operation-coding-system' was called. If the function can't decide |
10530 | a coding system, it can return `undecided' so that the normal | |
10531 | code-detection is performed. | |
48b0f3ae PJ |
10532 | |
10533 | See also the function `find-operation-coding-system' | |
10534 | and the variable `auto-coding-alist'. */); | |
02ba4723 KH |
10535 | Vfile_coding_system_alist = Qnil; |
10536 | ||
29208e82 | 10537 | DEFVAR_LISP ("process-coding-system-alist", Vprocess_coding_system_alist, |
df7492f9 KH |
10538 | doc: /* |
10539 | Alist to decide a coding system to use for a process I/O operation. | |
48b0f3ae PJ |
10540 | The format is ((PATTERN . VAL) ...), |
10541 | where PATTERN is a regular expression matching a program name, | |
10542 | VAL is a coding system, a cons of coding systems, or a function symbol. | |
10543 | If VAL is a coding system, it is used for both decoding what received | |
10544 | from the program and encoding what sent to the program. | |
10545 | If VAL is a cons of coding systems, the car part is used for decoding, | |
10546 | and the cdr part is used for encoding. | |
10547 | If VAL is a function symbol, the function must return a coding system | |
10548 | or a cons of coding systems which are used as above. | |
10549 | ||
10550 | See also the function `find-operation-coding-system'. */); | |
02ba4723 KH |
10551 | Vprocess_coding_system_alist = Qnil; |
10552 | ||
29208e82 | 10553 | DEFVAR_LISP ("network-coding-system-alist", Vnetwork_coding_system_alist, |
df7492f9 KH |
10554 | doc: /* |
10555 | Alist to decide a coding system to use for a network I/O operation. | |
48b0f3ae PJ |
10556 | The format is ((PATTERN . VAL) ...), |
10557 | where PATTERN is a regular expression matching a network service name | |
10558 | or is a port number to connect to, | |
10559 | VAL is a coding system, a cons of coding systems, or a function symbol. | |
10560 | If VAL is a coding system, it is used for both decoding what received | |
10561 | from the network stream and encoding what sent to the network stream. | |
10562 | If VAL is a cons of coding systems, the car part is used for decoding, | |
10563 | and the cdr part is used for encoding. | |
10564 | If VAL is a function symbol, the function must return a coding system | |
10565 | or a cons of coding systems which are used as above. | |
10566 | ||
10567 | See also the function `find-operation-coding-system'. */); | |
02ba4723 | 10568 | Vnetwork_coding_system_alist = Qnil; |
4ed46869 | 10569 | |
29208e82 | 10570 | DEFVAR_LISP ("locale-coding-system", Vlocale_coding_system, |
75205970 RS |
10571 | doc: /* Coding system to use with system messages. |
10572 | Also used for decoding keyboard input on X Window system. */); | |
68c45bf0 PE |
10573 | Vlocale_coding_system = Qnil; |
10574 | ||
005f0d35 | 10575 | /* The eol mnemonics are reset in startup.el system-dependently. */ |
29208e82 | 10576 | DEFVAR_LISP ("eol-mnemonic-unix", eol_mnemonic_unix, |
df7492f9 KH |
10577 | doc: /* |
10578 | *String displayed in mode line for UNIX-like (LF) end-of-line format. */); | |
d67b4f80 | 10579 | eol_mnemonic_unix = make_pure_c_string (":"); |
4ed46869 | 10580 | |
29208e82 | 10581 | DEFVAR_LISP ("eol-mnemonic-dos", eol_mnemonic_dos, |
df7492f9 KH |
10582 | doc: /* |
10583 | *String displayed in mode line for DOS-like (CRLF) end-of-line format. */); | |
d67b4f80 | 10584 | eol_mnemonic_dos = make_pure_c_string ("\\"); |
4ed46869 | 10585 | |
29208e82 | 10586 | DEFVAR_LISP ("eol-mnemonic-mac", eol_mnemonic_mac, |
df7492f9 KH |
10587 | doc: /* |
10588 | *String displayed in mode line for MAC-like (CR) end-of-line format. */); | |
d67b4f80 | 10589 | eol_mnemonic_mac = make_pure_c_string ("/"); |
4ed46869 | 10590 | |
29208e82 | 10591 | DEFVAR_LISP ("eol-mnemonic-undecided", eol_mnemonic_undecided, |
df7492f9 KH |
10592 | doc: /* |
10593 | *String displayed in mode line when end-of-line format is not yet determined. */); | |
d67b4f80 | 10594 | eol_mnemonic_undecided = make_pure_c_string (":"); |
4ed46869 | 10595 | |
29208e82 | 10596 | DEFVAR_LISP ("enable-character-translation", Venable_character_translation, |
df7492f9 KH |
10597 | doc: /* |
10598 | *Non-nil enables character translation while encoding and decoding. */); | |
84fbb8a0 | 10599 | Venable_character_translation = Qt; |
bdd9fb48 | 10600 | |
f967223b | 10601 | DEFVAR_LISP ("standard-translation-table-for-decode", |
29208e82 | 10602 | Vstandard_translation_table_for_decode, |
48b0f3ae | 10603 | doc: /* Table for translating characters while decoding. */); |
f967223b | 10604 | Vstandard_translation_table_for_decode = Qnil; |
bdd9fb48 | 10605 | |
f967223b | 10606 | DEFVAR_LISP ("standard-translation-table-for-encode", |
29208e82 | 10607 | Vstandard_translation_table_for_encode, |
48b0f3ae | 10608 | doc: /* Table for translating characters while encoding. */); |
f967223b | 10609 | Vstandard_translation_table_for_encode = Qnil; |
4ed46869 | 10610 | |
29208e82 | 10611 | DEFVAR_LISP ("charset-revision-table", Vcharset_revision_table, |
48b0f3ae PJ |
10612 | doc: /* Alist of charsets vs revision numbers. |
10613 | While encoding, if a charset (car part of an element) is found, | |
df7492f9 KH |
10614 | designate it with the escape sequence identifying revision (cdr part |
10615 | of the element). */); | |
10616 | Vcharset_revision_table = Qnil; | |
02ba4723 KH |
10617 | |
10618 | DEFVAR_LISP ("default-process-coding-system", | |
29208e82 | 10619 | Vdefault_process_coding_system, |
48b0f3ae PJ |
10620 | doc: /* Cons of coding systems used for process I/O by default. |
10621 | The car part is used for decoding a process output, | |
10622 | the cdr part is used for encoding a text to be sent to a process. */); | |
02ba4723 | 10623 | Vdefault_process_coding_system = Qnil; |
c4825358 | 10624 | |
29208e82 | 10625 | DEFVAR_LISP ("latin-extra-code-table", Vlatin_extra_code_table, |
df7492f9 KH |
10626 | doc: /* |
10627 | Table of extra Latin codes in the range 128..159 (inclusive). | |
48b0f3ae PJ |
10628 | This is a vector of length 256. |
10629 | If Nth element is non-nil, the existence of code N in a file | |
10630 | \(or output of subprocess) doesn't prevent it to be detected as | |
10631 | a coding system of ISO 2022 variant which has a flag | |
10632 | `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file | |
10633 | or reading output of a subprocess. | |
446dcd75 | 10634 | Only 128th through 159th elements have a meaning. */); |
3f003981 | 10635 | Vlatin_extra_code_table = Fmake_vector (make_number (256), Qnil); |
d46c5b12 KH |
10636 | |
10637 | DEFVAR_LISP ("select-safe-coding-system-function", | |
29208e82 | 10638 | Vselect_safe_coding_system_function, |
df7492f9 KH |
10639 | doc: /* |
10640 | Function to call to select safe coding system for encoding a text. | |
48b0f3ae PJ |
10641 | |
10642 | If set, this function is called to force a user to select a proper | |
10643 | coding system which can encode the text in the case that a default | |
fdecf907 GM |
10644 | coding system used in each operation can't encode the text. The |
10645 | function should take care that the buffer is not modified while | |
10646 | the coding system is being selected. | |
48b0f3ae PJ |
10647 | |
10648 | The default value is `select-safe-coding-system' (which see). */); | |
d46c5b12 KH |
10649 | Vselect_safe_coding_system_function = Qnil; |
10650 | ||
5d5bf4d8 | 10651 | DEFVAR_BOOL ("coding-system-require-warning", |
29208e82 | 10652 | coding_system_require_warning, |
5d5bf4d8 | 10653 | doc: /* Internal use only. |
6b89e3aa KH |
10654 | If non-nil, on writing a file, `select-safe-coding-system-function' is |
10655 | called even if `coding-system-for-write' is non-nil. The command | |
10656 | `universal-coding-system-argument' binds this variable to t temporarily. */); | |
5d5bf4d8 KH |
10657 | coding_system_require_warning = 0; |
10658 | ||
10659 | ||
22ab2303 | 10660 | DEFVAR_BOOL ("inhibit-iso-escape-detection", |
29208e82 | 10661 | inhibit_iso_escape_detection, |
df7492f9 | 10662 | doc: /* |
97b1b294 | 10663 | If non-nil, Emacs ignores ISO-2022 escape sequences during code detection. |
48b0f3ae | 10664 | |
97b1b294 EZ |
10665 | When Emacs reads text, it tries to detect how the text is encoded. |
10666 | This code detection is sensitive to escape sequences. If Emacs sees | |
10667 | a valid ISO-2022 escape sequence, it assumes the text is encoded in one | |
10668 | of the ISO2022 encodings, and decodes text by the corresponding coding | |
10669 | system (e.g. `iso-2022-7bit'). | |
48b0f3ae PJ |
10670 | |
10671 | However, there may be a case that you want to read escape sequences in | |
10672 | a file as is. In such a case, you can set this variable to non-nil. | |
97b1b294 EZ |
10673 | Then the code detection will ignore any escape sequences, and no text is |
10674 | detected as encoded in some ISO-2022 encoding. The result is that all | |
48b0f3ae PJ |
10675 | escape sequences become visible in a buffer. |
10676 | ||
10677 | The default value is nil, and it is strongly recommended not to change | |
10678 | it. That is because many Emacs Lisp source files that contain | |
10679 | non-ASCII characters are encoded by the coding system `iso-2022-7bit' | |
10680 | in Emacs's distribution, and they won't be decoded correctly on | |
10681 | reading if you suppress escape sequence detection. | |
10682 | ||
10683 | The other way to read escape sequences in a file without decoding is | |
97b1b294 | 10684 | to explicitly specify some coding system that doesn't use ISO-2022 |
48b0f3ae | 10685 | escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */); |
74383408 | 10686 | inhibit_iso_escape_detection = 0; |
002fdb44 | 10687 | |
97b1b294 | 10688 | DEFVAR_BOOL ("inhibit-null-byte-detection", |
29208e82 | 10689 | inhibit_null_byte_detection, |
97b1b294 EZ |
10690 | doc: /* If non-nil, Emacs ignores null bytes on code detection. |
10691 | By default, Emacs treats it as binary data, and does not attempt to | |
10692 | decode it. The effect is as if you specified `no-conversion' for | |
10693 | reading that text. | |
10694 | ||
10695 | Set this to non-nil when a regular text happens to include null bytes. | |
10696 | Examples are Index nodes of Info files and null-byte delimited output | |
10697 | from GNU Find and GNU Grep. Emacs will then ignore the null bytes and | |
10698 | decode text as usual. */); | |
10699 | inhibit_null_byte_detection = 0; | |
10700 | ||
29208e82 | 10701 | DEFVAR_LISP ("translation-table-for-input", Vtranslation_table_for_input, |
15c8f9d1 | 10702 | doc: /* Char table for translating self-inserting characters. |
446dcd75 | 10703 | This is applied to the result of input methods, not their input. |
8434d0b8 EZ |
10704 | See also `keyboard-translate-table'. |
10705 | ||
10706 | Use of this variable for character code unification was rendered | |
10707 | obsolete in Emacs 23.1 and later, since Unicode is now the basis of | |
10708 | internal character representation. */); | |
002fdb44 | 10709 | Vtranslation_table_for_input = Qnil; |
8f924df7 | 10710 | |
2c78b7e1 KH |
10711 | { |
10712 | Lisp_Object args[coding_arg_max]; | |
8f924df7 | 10713 | Lisp_Object plist[16]; |
2c78b7e1 KH |
10714 | int i; |
10715 | ||
10716 | for (i = 0; i < coding_arg_max; i++) | |
10717 | args[i] = Qnil; | |
10718 | ||
d67b4f80 | 10719 | plist[0] = intern_c_string (":name"); |
2c78b7e1 | 10720 | plist[1] = args[coding_arg_name] = Qno_conversion; |
d67b4f80 | 10721 | plist[2] = intern_c_string (":mnemonic"); |
2c78b7e1 | 10722 | plist[3] = args[coding_arg_mnemonic] = make_number ('='); |
d67b4f80 | 10723 | plist[4] = intern_c_string (":coding-type"); |
2c78b7e1 | 10724 | plist[5] = args[coding_arg_coding_type] = Qraw_text; |
d67b4f80 | 10725 | plist[6] = intern_c_string (":ascii-compatible-p"); |
2c78b7e1 | 10726 | plist[7] = args[coding_arg_ascii_compatible_p] = Qt; |
d67b4f80 | 10727 | plist[8] = intern_c_string (":default-char"); |
2c78b7e1 | 10728 | plist[9] = args[coding_arg_default_char] = make_number (0); |
d67b4f80 | 10729 | plist[10] = intern_c_string (":for-unibyte"); |
8f924df7 | 10730 | plist[11] = args[coding_arg_for_unibyte] = Qt; |
d67b4f80 DN |
10731 | plist[12] = intern_c_string (":docstring"); |
10732 | plist[13] = make_pure_c_string ("Do no conversion.\n\ | |
2c78b7e1 KH |
10733 | \n\ |
10734 | When you visit a file with this coding, the file is read into a\n\ | |
10735 | unibyte buffer as is, thus each byte of a file is treated as a\n\ | |
10736 | character."); | |
d67b4f80 | 10737 | plist[14] = intern_c_string (":eol-type"); |
8f924df7 KH |
10738 | plist[15] = args[coding_arg_eol_type] = Qunix; |
10739 | args[coding_arg_plist] = Flist (16, plist); | |
2c78b7e1 | 10740 | Fdefine_coding_system_internal (coding_arg_max, args); |
ae6f73fa KH |
10741 | |
10742 | plist[1] = args[coding_arg_name] = Qundecided; | |
10743 | plist[3] = args[coding_arg_mnemonic] = make_number ('-'); | |
10744 | plist[5] = args[coding_arg_coding_type] = Qundecided; | |
10745 | /* This is already set. | |
35befdaa | 10746 | plist[7] = args[coding_arg_ascii_compatible_p] = Qt; */ |
d67b4f80 | 10747 | plist[8] = intern_c_string (":charset-list"); |
ae6f73fa KH |
10748 | plist[9] = args[coding_arg_charset_list] = Fcons (Qascii, Qnil); |
10749 | plist[11] = args[coding_arg_for_unibyte] = Qnil; | |
d67b4f80 | 10750 | plist[13] = make_pure_c_string ("No conversion on encoding, automatic conversion on decoding."); |
ae6f73fa KH |
10751 | plist[15] = args[coding_arg_eol_type] = Qnil; |
10752 | args[coding_arg_plist] = Flist (16, plist); | |
10753 | Fdefine_coding_system_internal (coding_arg_max, args); | |
2c78b7e1 KH |
10754 | } |
10755 | ||
2c78b7e1 | 10756 | setup_coding_system (Qno_conversion, &safe_terminal_coding); |
ff563fce KH |
10757 | |
10758 | { | |
10759 | int i; | |
10760 | ||
10761 | for (i = 0; i < coding_category_max; i++) | |
10762 | Fset (AREF (Vcoding_category_table, i), Qno_conversion); | |
10763 | } | |
1a4990fb | 10764 | #if defined (DOS_NT) |
fcbcfb64 KH |
10765 | system_eol_type = Qdos; |
10766 | #else | |
10767 | system_eol_type = Qunix; | |
10768 | #endif | |
10769 | staticpro (&system_eol_type); | |
4ed46869 KH |
10770 | } |
10771 | ||
68c45bf0 | 10772 | char * |
971de7fb | 10773 | emacs_strerror (int error_number) |
68c45bf0 PE |
10774 | { |
10775 | char *str; | |
10776 | ||
ca9c0567 | 10777 | synchronize_system_messages_locale (); |
68c45bf0 PE |
10778 | str = strerror (error_number); |
10779 | ||
10780 | if (! NILP (Vlocale_coding_system)) | |
10781 | { | |
10782 | Lisp_Object dec = code_convert_string_norecord (build_string (str), | |
10783 | Vlocale_coding_system, | |
10784 | 0); | |
51b59d79 | 10785 | str = SSDATA (dec); |
68c45bf0 PE |
10786 | } |
10787 | ||
10788 | return str; | |
10789 | } | |
10790 | ||
4ed46869 | 10791 | #endif /* emacs */ |